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" 17d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/ADT/STLExtras.h" 1836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include "llvm/ADT/SmallSet.h" 19cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines#include "llvm/Analysis/InstructionSimplify.h" 20e81561909d128c6e2d8033cb5465a49b2596b26aBill Wendling#include "llvm/Analysis/LoopInfo.h" 21e4ba75f43e2ab1480d119d2d4eb878256274e0fbChandler Carruth#include "llvm/Analysis/TargetTransformInfo.h" 220b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/DataLayout.h" 2336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include "llvm/IR/Dominators.h" 240b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/IntrinsicInst.h" 250b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/LLVMContext.h" 26c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick#include "llvm/Support/Debug.h" 27d152d03a476b8d0d4b26577db26e2ba76034b0f3Andrew Trick 2836f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begemanusing namespace llvm; 2936f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman 3019e5ada58a8a07d3d6d67312550f6d0791d84c3cGabor Greif/// ReuseOrCreateCast - Arrange for there to be a cast of V to Ty at IP, 31485c43fc478d5e16c55e14cb2586b56cc1c4c91fDan Gohman/// reusing an existing cast if a suitable one exists, moving an existing 32485c43fc478d5e16c55e14cb2586b56cc1c4c91fDan Gohman/// cast if a suitable one exists but isn't in the right place, or 3319e5ada58a8a07d3d6d67312550f6d0791d84c3cGabor Greif/// creating a new one. 34db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris LattnerValue *SCEVExpander::ReuseOrCreateCast(Value *V, Type *Ty, 35485c43fc478d5e16c55e14cb2586b56cc1c4c91fDan Gohman Instruction::CastOps Op, 36485c43fc478d5e16c55e14cb2586b56cc1c4c91fDan Gohman BasicBlock::iterator IP) { 37919a503458c18472984c7c37b305110c9ba3a782Rafael Espindola // This function must be called with the builder having a valid insertion 38919a503458c18472984c7c37b305110c9ba3a782Rafael Espindola // point. It doesn't need to be the actual IP where the uses of the returned 39919a503458c18472984c7c37b305110c9ba3a782Rafael Espindola // cast will be added, but it must dominate such IP. 4023b6ec906a081259dad4672ec386ddfb52cd0d9fRafael Espindola // We use this precondition to produce a cast that will dominate all its 4123b6ec906a081259dad4672ec386ddfb52cd0d9fRafael Espindola // uses. In particular, this is crucial for the case where the builder's 4223b6ec906a081259dad4672ec386ddfb52cd0d9fRafael Espindola // insertion point *is* the point where we were asked to put the cast. 43c8e41c591741b3da1077f7000274ad040bef8002Sylvestre Ledru // Since we don't know the builder's insertion point is actually 44919a503458c18472984c7c37b305110c9ba3a782Rafael Espindola // where the uses will be added (only that it dominates it), we are 45919a503458c18472984c7c37b305110c9ba3a782Rafael Espindola // not allowed to move it. 46919a503458c18472984c7c37b305110c9ba3a782Rafael Espindola BasicBlock::iterator BIP = Builder.GetInsertPoint(); 47919a503458c18472984c7c37b305110c9ba3a782Rafael Espindola 48dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines Instruction *Ret = nullptr; 49ef4c80e07baf02dd2a8f08db49c5634a06d3ca1eRafael Espindola 50485c43fc478d5e16c55e14cb2586b56cc1c4c91fDan Gohman // Check to see if there is already a cast! 5136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines for (User *U : V->users()) 52f64f9cf7ec39ae91bca84dc6ad3c8fc3343b358aGabor Greif if (U->getType() == Ty) 5319e5ada58a8a07d3d6d67312550f6d0791d84c3cGabor Greif if (CastInst *CI = dyn_cast<CastInst>(U)) 54485c43fc478d5e16c55e14cb2586b56cc1c4c91fDan Gohman if (CI->getOpcode() == Op) { 55b84d540bc9d70de00b791d0e0deab410d6ba3ad5Rafael Espindola // If the cast isn't where we want it, create a new cast at IP. 56b84d540bc9d70de00b791d0e0deab410d6ba3ad5Rafael Espindola // Likewise, do not reuse a cast at BIP because it must dominate 57b84d540bc9d70de00b791d0e0deab410d6ba3ad5Rafael Espindola // instructions that might be inserted before BIP. 58919a503458c18472984c7c37b305110c9ba3a782Rafael Espindola if (BasicBlock::iterator(CI) != IP || BIP == IP) { 59485c43fc478d5e16c55e14cb2586b56cc1c4c91fDan Gohman // Create a new cast, and leave the old cast in place in case 60485c43fc478d5e16c55e14cb2586b56cc1c4c91fDan Gohman // it is being used as an insert point. Clear its operand 61485c43fc478d5e16c55e14cb2586b56cc1c4c91fDan Gohman // so that it doesn't hold anything live. 6223b6ec906a081259dad4672ec386ddfb52cd0d9fRafael Espindola Ret = CastInst::Create(Op, V, Ty, "", IP); 6323b6ec906a081259dad4672ec386ddfb52cd0d9fRafael Espindola Ret->takeName(CI); 6423b6ec906a081259dad4672ec386ddfb52cd0d9fRafael Espindola CI->replaceAllUsesWith(Ret); 65485c43fc478d5e16c55e14cb2586b56cc1c4c91fDan Gohman CI->setOperand(0, UndefValue::get(V->getType())); 6623b6ec906a081259dad4672ec386ddfb52cd0d9fRafael Espindola break; 67485c43fc478d5e16c55e14cb2586b56cc1c4c91fDan Gohman } 6823b6ec906a081259dad4672ec386ddfb52cd0d9fRafael Espindola Ret = CI; 6923b6ec906a081259dad4672ec386ddfb52cd0d9fRafael Espindola break; 70485c43fc478d5e16c55e14cb2586b56cc1c4c91fDan Gohman } 71485c43fc478d5e16c55e14cb2586b56cc1c4c91fDan Gohman 72485c43fc478d5e16c55e14cb2586b56cc1c4c91fDan Gohman // Create a new cast. 7323b6ec906a081259dad4672ec386ddfb52cd0d9fRafael Espindola if (!Ret) 7423b6ec906a081259dad4672ec386ddfb52cd0d9fRafael Espindola Ret = CastInst::Create(Op, V, Ty, V->getName(), IP); 7523b6ec906a081259dad4672ec386ddfb52cd0d9fRafael Espindola 7623b6ec906a081259dad4672ec386ddfb52cd0d9fRafael Espindola // We assert at the end of the function since IP might point to an 7723b6ec906a081259dad4672ec386ddfb52cd0d9fRafael Espindola // instruction with different dominance properties than a cast 7823b6ec906a081259dad4672ec386ddfb52cd0d9fRafael Espindola // (an invoke for example) and not dominate BIP (but the cast does). 7923b6ec906a081259dad4672ec386ddfb52cd0d9fRafael Espindola assert(SE.DT->dominates(Ret, BIP)); 8023b6ec906a081259dad4672ec386ddfb52cd0d9fRafael Espindola 8123b6ec906a081259dad4672ec386ddfb52cd0d9fRafael Espindola rememberInstruction(Ret); 8223b6ec906a081259dad4672ec386ddfb52cd0d9fRafael Espindola return Ret; 83485c43fc478d5e16c55e14cb2586b56cc1c4c91fDan Gohman} 84485c43fc478d5e16c55e14cb2586b56cc1c4c91fDan Gohman 85267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman/// InsertNoopCastOfTo - Insert a cast of V to the specified type, 86267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman/// which must be possible with a noop cast, doing what we can to share 87267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman/// the casts. 88db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris LattnerValue *SCEVExpander::InsertNoopCastOfTo(Value *V, Type *Ty) { 89267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman Instruction::CastOps Op = CastInst::getCastOpcode(V, false, Ty, false); 90267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman assert((Op == Instruction::BitCast || 91267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman Op == Instruction::PtrToInt || 92267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman Op == Instruction::IntToPtr) && 93267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman "InsertNoopCastOfTo cannot perform non-noop casts!"); 94267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman assert(SE.getTypeSizeInBits(V->getType()) == SE.getTypeSizeInBits(Ty) && 95267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman "InsertNoopCastOfTo cannot change sizes!"); 96267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman 972d1be87ee40a4a0241d94448173879d9df2bc5b3Dan Gohman // Short-circuit unnecessary bitcasts. 9819154f457696f286b4a597179fa07e3b38ea0310Andrew Trick if (Op == Instruction::BitCast) { 9919154f457696f286b4a597179fa07e3b38ea0310Andrew Trick if (V->getType() == Ty) 10019154f457696f286b4a597179fa07e3b38ea0310Andrew Trick return V; 10119154f457696f286b4a597179fa07e3b38ea0310Andrew Trick if (CastInst *CI = dyn_cast<CastInst>(V)) { 10219154f457696f286b4a597179fa07e3b38ea0310Andrew Trick if (CI->getOperand(0)->getType() == Ty) 10319154f457696f286b4a597179fa07e3b38ea0310Andrew Trick return CI->getOperand(0); 10419154f457696f286b4a597179fa07e3b38ea0310Andrew Trick } 10519154f457696f286b4a597179fa07e3b38ea0310Andrew Trick } 106f04fa483b83227c570bc58e1684ea096430a6697Dan Gohman // Short-circuit unnecessary inttoptr<->ptrtoint casts. 107267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman if ((Op == Instruction::PtrToInt || Op == Instruction::IntToPtr) && 10880dcdee0f48820ecea86c15768324945bb0d68d1Dan Gohman SE.getTypeSizeInBits(Ty) == SE.getTypeSizeInBits(V->getType())) { 109af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman if (CastInst *CI = dyn_cast<CastInst>(V)) 110af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman if ((CI->getOpcode() == Instruction::PtrToInt || 111af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman CI->getOpcode() == Instruction::IntToPtr) && 112af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman SE.getTypeSizeInBits(CI->getType()) == 113af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman SE.getTypeSizeInBits(CI->getOperand(0)->getType())) 114af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman return CI->getOperand(0); 11580dcdee0f48820ecea86c15768324945bb0d68d1Dan Gohman if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) 11680dcdee0f48820ecea86c15768324945bb0d68d1Dan Gohman if ((CE->getOpcode() == Instruction::PtrToInt || 11780dcdee0f48820ecea86c15768324945bb0d68d1Dan Gohman CE->getOpcode() == Instruction::IntToPtr) && 11880dcdee0f48820ecea86c15768324945bb0d68d1Dan Gohman SE.getTypeSizeInBits(CE->getType()) == 11980dcdee0f48820ecea86c15768324945bb0d68d1Dan Gohman SE.getTypeSizeInBits(CE->getOperand(0)->getType())) 12080dcdee0f48820ecea86c15768324945bb0d68d1Dan Gohman return CE->getOperand(0); 12180dcdee0f48820ecea86c15768324945bb0d68d1Dan Gohman } 122f04fa483b83227c570bc58e1684ea096430a6697Dan Gohman 123485c43fc478d5e16c55e14cb2586b56cc1c4c91fDan Gohman // Fold a cast of a constant. 124ca1a4bebb3928ba18fb8751e2f0c0f88fad54cfdChris Lattner if (Constant *C = dyn_cast<Constant>(V)) 125baf3c404409d5e47b13984a7f95bfbd6d1f2e79eOwen Anderson return ConstantExpr::getCast(Op, C, Ty); 1264c0d5d5db876b0628bdf6a2174263a1c0a9130e2Dan Gohman 127485c43fc478d5e16c55e14cb2586b56cc1c4c91fDan Gohman // Cast the argument at the beginning of the entry block, after 128485c43fc478d5e16c55e14cb2586b56cc1c4c91fDan Gohman // any bitcasts of other arguments. 129ca1a4bebb3928ba18fb8751e2f0c0f88fad54cfdChris Lattner if (Argument *A = dyn_cast<Argument>(V)) { 130485c43fc478d5e16c55e14cb2586b56cc1c4c91fDan Gohman BasicBlock::iterator IP = A->getParent()->getEntryBlock().begin(); 131485c43fc478d5e16c55e14cb2586b56cc1c4c91fDan Gohman while ((isa<BitCastInst>(IP) && 132485c43fc478d5e16c55e14cb2586b56cc1c4c91fDan Gohman isa<Argument>(cast<BitCastInst>(IP)->getOperand(0)) && 133485c43fc478d5e16c55e14cb2586b56cc1c4c91fDan Gohman cast<BitCastInst>(IP)->getOperand(0) != A) || 134a4c86ab073d4b7a36477fc7c54c9d52499f04586Bill Wendling isa<DbgInfoIntrinsic>(IP) || 135a4c86ab073d4b7a36477fc7c54c9d52499f04586Bill Wendling isa<LandingPadInst>(IP)) 136485c43fc478d5e16c55e14cb2586b56cc1c4c91fDan Gohman ++IP; 137485c43fc478d5e16c55e14cb2586b56cc1c4c91fDan Gohman return ReuseOrCreateCast(A, Ty, Op, IP); 138ca1a4bebb3928ba18fb8751e2f0c0f88fad54cfdChris Lattner } 1393913187bf6135e6a71260c65eed664095c8f9ce9Wojciech Matyjewicz 140485c43fc478d5e16c55e14cb2586b56cc1c4c91fDan Gohman // Cast the instruction immediately after the instruction. 141ca1a4bebb3928ba18fb8751e2f0c0f88fad54cfdChris Lattner Instruction *I = cast<Instruction>(V); 142ca1a4bebb3928ba18fb8751e2f0c0f88fad54cfdChris Lattner BasicBlock::iterator IP = I; ++IP; 143ca1a4bebb3928ba18fb8751e2f0c0f88fad54cfdChris Lattner if (InvokeInst *II = dyn_cast<InvokeInst>(I)) 144ca1a4bebb3928ba18fb8751e2f0c0f88fad54cfdChris Lattner IP = II->getNormalDest()->begin(); 145ef4c80e07baf02dd2a8f08db49c5634a06d3ca1eRafael Espindola while (isa<PHINode>(IP) || isa<LandingPadInst>(IP)) 146a4c86ab073d4b7a36477fc7c54c9d52499f04586Bill Wendling ++IP; 147485c43fc478d5e16c55e14cb2586b56cc1c4c91fDan Gohman return ReuseOrCreateCast(I, Ty, Op, IP); 148ca1a4bebb3928ba18fb8751e2f0c0f88fad54cfdChris Lattner} 149ca1a4bebb3928ba18fb8751e2f0c0f88fad54cfdChris Lattner 1507fec90ebf4ebe7aa73a6dd7d275c255587c041adChris Lattner/// InsertBinop - Insert the specified binary operator, doing a small amount 1517fec90ebf4ebe7aa73a6dd7d275c255587c041adChris Lattner/// of work to avoid inserting an obviously redundant operation. 152267a385342f2e7388f178b327dd87c5f29afd51bDan GohmanValue *SCEVExpander::InsertBinop(Instruction::BinaryOps Opcode, 153267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman Value *LHS, Value *RHS) { 1540f0eb18addc94bf3d8179fbce162c8c4622b9866Dan Gohman // Fold a binop with constant operands. 1550f0eb18addc94bf3d8179fbce162c8c4622b9866Dan Gohman if (Constant *CLHS = dyn_cast<Constant>(LHS)) 1560f0eb18addc94bf3d8179fbce162c8c4622b9866Dan Gohman if (Constant *CRHS = dyn_cast<Constant>(RHS)) 157baf3c404409d5e47b13984a7f95bfbd6d1f2e79eOwen Anderson return ConstantExpr::get(Opcode, CLHS, CRHS); 1580f0eb18addc94bf3d8179fbce162c8c4622b9866Dan Gohman 1597fec90ebf4ebe7aa73a6dd7d275c255587c041adChris Lattner // Do a quick scan to see if we have this binop nearby. If so, reuse it. 1607fec90ebf4ebe7aa73a6dd7d275c255587c041adChris Lattner unsigned ScanLimit = 6; 161267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman BasicBlock::iterator BlockBegin = Builder.GetInsertBlock()->begin(); 162267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman // Scanning starts from the last instruction before the insertion point. 163267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman BasicBlock::iterator IP = Builder.GetInsertPoint(); 164267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman if (IP != BlockBegin) { 1658a08769bad43a22fae2845bb0ba0fd1266cd55c8Wojciech Matyjewicz --IP; 1668a08769bad43a22fae2845bb0ba0fd1266cd55c8Wojciech Matyjewicz for (; ScanLimit; --IP, --ScanLimit) { 1678d50ea760322630dbb9ab6436ca356b279adee10Dale Johannesen // Don't count dbg.value against the ScanLimit, to avoid perturbing the 1688d50ea760322630dbb9ab6436ca356b279adee10Dale Johannesen // generated code. 1698d50ea760322630dbb9ab6436ca356b279adee10Dale Johannesen if (isa<DbgInfoIntrinsic>(IP)) 1708d50ea760322630dbb9ab6436ca356b279adee10Dale Johannesen ScanLimit++; 1715be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman if (IP->getOpcode() == (unsigned)Opcode && IP->getOperand(0) == LHS && 1725be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman IP->getOperand(1) == RHS) 1735be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman return IP; 1748a08769bad43a22fae2845bb0ba0fd1266cd55c8Wojciech Matyjewicz if (IP == BlockBegin) break; 1758a08769bad43a22fae2845bb0ba0fd1266cd55c8Wojciech Matyjewicz } 1767fec90ebf4ebe7aa73a6dd7d275c255587c041adChris Lattner } 177267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman 178087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman // Save the original insertion point so we can restore it when we're done. 179d4278821665aa97f5fc0d19a32ff1fb39a22d395Benjamin Kramer DebugLoc Loc = Builder.GetInsertPoint()->getDebugLoc(); 180d4278821665aa97f5fc0d19a32ff1fb39a22d395Benjamin Kramer BuilderType::InsertPointGuard Guard(Builder); 181087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman 182087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman // Move the insertion point out of as many loops as we can. 183087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman while (const Loop *L = SE.LI->getLoopFor(Builder.GetInsertBlock())) { 184087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman if (!L->isLoopInvariant(LHS) || !L->isLoopInvariant(RHS)) break; 185087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman BasicBlock *Preheader = L->getLoopPreheader(); 186087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman if (!Preheader) break; 187087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman 188087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman // Ok, move up a level. 189087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman Builder.SetInsertPoint(Preheader, Preheader->getTerminator()); 190087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman } 191087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman 1928a08769bad43a22fae2845bb0ba0fd1266cd55c8Wojciech Matyjewicz // If we haven't found this binop, insert it. 193a9390a4d5f5d568059a80970d22194b165d097a7Benjamin Kramer Instruction *BO = cast<Instruction>(Builder.CreateBinOp(Opcode, LHS, RHS)); 194d4278821665aa97f5fc0d19a32ff1fb39a22d395Benjamin Kramer BO->setDebugLoc(Loc); 195a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman rememberInstruction(BO); 196087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman 197cf5ab820227dedd77fb91d0904b6dc3694a7c196Dan Gohman return BO; 1987fec90ebf4ebe7aa73a6dd7d275c255587c041adChris Lattner} 1997fec90ebf4ebe7aa73a6dd7d275c255587c041adChris Lattner 2004a4f767235dac50965fc266fb822d9a2e37d5212Dan Gohman/// FactorOutConstant - Test if S is divisible by Factor, using signed 201453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman/// division. If so, update S with Factor divided out and return true. 2023f46a3abeedba8d517b4182de34c821d752db058Dan Gohman/// S need not be evenly divisible if a reasonable remainder can be 2034a4f767235dac50965fc266fb822d9a2e37d5212Dan Gohman/// computed. 204453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman/// TODO: When ScalarEvolution gets a SCEVSDivExpr, this can be made 205453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman/// unnecessary; in its place, just signed-divide Ops[i] by the scale and 206453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman/// check to see if the divide was folded. 2070bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohmanstatic bool FactorOutConstant(const SCEV *&S, 2080bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman const SCEV *&Remainder, 209c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman const SCEV *Factor, 210c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman ScalarEvolution &SE, 21136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines const DataLayout *DL) { 212453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman // Everything is divisible by one. 213c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman if (Factor->isOne()) 214c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman return true; 215c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman 216c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman // x/x == 1. 217c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman if (S == Factor) { 218deff621abdd48bd70434bd4d7ef30f08ddba1cd8Dan Gohman S = SE.getConstant(S->getType(), 1); 219453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman return true; 220c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman } 221453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman 222453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman // For a Constant, check for a multiple of the given factor. 2234a4f767235dac50965fc266fb822d9a2e37d5212Dan Gohman if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) { 224c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman // 0/x == 0. 225c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman if (C->isZero()) 226453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman return true; 227c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman // Check for divisibility. 228c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman if (const SCEVConstant *FC = dyn_cast<SCEVConstant>(Factor)) { 229c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman ConstantInt *CI = 230c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman ConstantInt::get(SE.getContext(), 231c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman C->getValue()->getValue().sdiv( 232c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman FC->getValue()->getValue())); 233c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman // If the quotient is zero and the remainder is non-zero, reject 234c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman // the value at this scale. It will be considered for subsequent 235c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman // smaller scales. 236c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman if (!CI->isZero()) { 237c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman const SCEV *Div = SE.getConstant(CI); 238c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman S = Div; 239c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman Remainder = 240c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman SE.getAddExpr(Remainder, 241c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman SE.getConstant(C->getValue()->getValue().srem( 242c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman FC->getValue()->getValue()))); 243c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman return true; 244c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman } 245453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman } 2464a4f767235dac50965fc266fb822d9a2e37d5212Dan Gohman } 247453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman 248453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman // In a Mul, check if there is a constant operand which is a multiple 249453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman // of the given factor. 250c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(S)) { 25136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if (DL) { 2523574eca1b02600bac4e625297f4ecf745f4c4f32Micah Villmow // With DataLayout, the size is known. Check if there is a constant 253c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman // operand which is a multiple of the given factor. If so, we can 254c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman // factor it. 255c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman const SCEVConstant *FC = cast<SCEVConstant>(Factor); 256c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman if (const SCEVConstant *C = dyn_cast<SCEVConstant>(M->getOperand(0))) 257c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman if (!C->getValue()->getValue().srem(FC->getValue()->getValue())) { 258f9e64729afec646fe93b51417e66e552e8e630a4Dan Gohman SmallVector<const SCEV *, 4> NewMulOps(M->op_begin(), M->op_end()); 259c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman NewMulOps[0] = 260c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman SE.getConstant(C->getValue()->getValue().sdiv( 261c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman FC->getValue()->getValue())); 262c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman S = SE.getMulExpr(NewMulOps); 263c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman return true; 264c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman } 265c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman } else { 2663574eca1b02600bac4e625297f4ecf745f4c4f32Micah Villmow // Without DataLayout, check if Factor can be factored out of any of the 267c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman // Mul's operands. If so, we can just remove it. 268c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) { 269c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman const SCEV *SOp = M->getOperand(i); 270deff621abdd48bd70434bd4d7ef30f08ddba1cd8Dan Gohman const SCEV *Remainder = SE.getConstant(SOp->getType(), 0); 27136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if (FactorOutConstant(SOp, Remainder, Factor, SE, DL) && 272c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman Remainder->isZero()) { 273f9e64729afec646fe93b51417e66e552e8e630a4Dan Gohman SmallVector<const SCEV *, 4> NewMulOps(M->op_begin(), M->op_end()); 274c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman NewMulOps[i] = SOp; 275c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman S = SE.getMulExpr(NewMulOps); 276c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman return true; 277c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman } 278453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman } 279c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman } 280c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman } 281453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman 282453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman // In an AddRec, check if both start and step are divisible. 283453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(S)) { 2840bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman const SCEV *Step = A->getStepRecurrence(SE); 285deff621abdd48bd70434bd4d7ef30f08ddba1cd8Dan Gohman const SCEV *StepRem = SE.getConstant(Step->getType(), 0); 28636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if (!FactorOutConstant(Step, StepRem, Factor, SE, DL)) 2874a4f767235dac50965fc266fb822d9a2e37d5212Dan Gohman return false; 2884a4f767235dac50965fc266fb822d9a2e37d5212Dan Gohman if (!StepRem->isZero()) 2894a4f767235dac50965fc266fb822d9a2e37d5212Dan Gohman return false; 2900bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman const SCEV *Start = A->getStart(); 29136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if (!FactorOutConstant(Start, Remainder, Factor, SE, DL)) 292453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman return false; 2936f71dd765ae9e1d1d0ba01d98a05627ffe3bfc8aAndrew Trick S = SE.getAddRecExpr(Start, Step, A->getLoop(), 2946f71dd765ae9e1d1d0ba01d98a05627ffe3bfc8aAndrew Trick A->getNoWrapFlags(SCEV::FlagNW)); 295453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman return true; 296453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman } 297453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman 298453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman return false; 299453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman} 300453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman 301c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman/// SimplifyAddOperands - Sort and simplify a list of add operands. NumAddRecs 302c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman/// is the number of SCEVAddRecExprs present, which are kept at the end of 303c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman/// the list. 304c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman/// 305c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohmanstatic void SimplifyAddOperands(SmallVectorImpl<const SCEV *> &Ops, 306db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner Type *Ty, 307c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman ScalarEvolution &SE) { 308c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman unsigned NumAddRecs = 0; 309c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman for (unsigned i = Ops.size(); i > 0 && isa<SCEVAddRecExpr>(Ops[i-1]); --i) 310c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman ++NumAddRecs; 311c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman // Group Ops into non-addrecs and addrecs. 312c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman SmallVector<const SCEV *, 8> NoAddRecs(Ops.begin(), Ops.end() - NumAddRecs); 313c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman SmallVector<const SCEV *, 8> AddRecs(Ops.end() - NumAddRecs, Ops.end()); 314c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman // Let ScalarEvolution sort and simplify the non-addrecs list. 315c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman const SCEV *Sum = NoAddRecs.empty() ? 316deff621abdd48bd70434bd4d7ef30f08ddba1cd8Dan Gohman SE.getConstant(Ty, 0) : 317c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman SE.getAddExpr(NoAddRecs); 318c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman // If it returned an add, use the operands. Otherwise it simplified 319c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman // the sum into a single value, so just use that. 320f9e64729afec646fe93b51417e66e552e8e630a4Dan Gohman Ops.clear(); 321c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Sum)) 322403a8cdda5e76ea689693de16474650b4b0df818Dan Gohman Ops.append(Add->op_begin(), Add->op_end()); 323f9e64729afec646fe93b51417e66e552e8e630a4Dan Gohman else if (!Sum->isZero()) 324f9e64729afec646fe93b51417e66e552e8e630a4Dan Gohman Ops.push_back(Sum); 325c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman // Then append the addrecs. 326403a8cdda5e76ea689693de16474650b4b0df818Dan Gohman Ops.append(AddRecs.begin(), AddRecs.end()); 327c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman} 328c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman 329c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman/// SplitAddRecs - Flatten a list of add operands, moving addrec start values 330c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman/// out to the top level. For example, convert {a + b,+,c} to a, b, {0,+,d}. 331c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman/// This helps expose more opportunities for folding parts of the expressions 332c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman/// into GEP indices. 333c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman/// 334c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohmanstatic void SplitAddRecs(SmallVectorImpl<const SCEV *> &Ops, 335db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner Type *Ty, 336c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman ScalarEvolution &SE) { 337c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman // Find the addrecs. 338c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman SmallVector<const SCEV *, 8> AddRecs; 339c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman for (unsigned i = 0, e = Ops.size(); i != e; ++i) 340c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman while (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(Ops[i])) { 341c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman const SCEV *Start = A->getStart(); 342c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman if (Start->isZero()) break; 343deff621abdd48bd70434bd4d7ef30f08ddba1cd8Dan Gohman const SCEV *Zero = SE.getConstant(Ty, 0); 344c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman AddRecs.push_back(SE.getAddRecExpr(Zero, 345c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman A->getStepRecurrence(SE), 3463228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick A->getLoop(), 3476f71dd765ae9e1d1d0ba01d98a05627ffe3bfc8aAndrew Trick A->getNoWrapFlags(SCEV::FlagNW))); 348c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Start)) { 349c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman Ops[i] = Zero; 350403a8cdda5e76ea689693de16474650b4b0df818Dan Gohman Ops.append(Add->op_begin(), Add->op_end()); 351c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman e += Add->getNumOperands(); 352c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman } else { 353c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman Ops[i] = Start; 354c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman } 355c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman } 356c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman if (!AddRecs.empty()) { 357c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman // Add the addrecs onto the end of the list. 358403a8cdda5e76ea689693de16474650b4b0df818Dan Gohman Ops.append(AddRecs.begin(), AddRecs.end()); 359c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman // Resort the operand list, moving any constants to the front. 360c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman SimplifyAddOperands(Ops, Ty, SE); 361c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman } 362c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman} 363c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman 3644c0d5d5db876b0628bdf6a2174263a1c0a9130e2Dan Gohman/// expandAddToGEP - Expand an addition expression with a pointer type into 3654c0d5d5db876b0628bdf6a2174263a1c0a9130e2Dan Gohman/// a GEP instead of using ptrtoint+arithmetic+inttoptr. This helps 3664c0d5d5db876b0628bdf6a2174263a1c0a9130e2Dan Gohman/// BasicAliasAnalysis and other passes analyze the result. See the rules 3674c0d5d5db876b0628bdf6a2174263a1c0a9130e2Dan Gohman/// for getelementptr vs. inttoptr in 3684c0d5d5db876b0628bdf6a2174263a1c0a9130e2Dan Gohman/// http://llvm.org/docs/LangRef.html#pointeraliasing 3694c0d5d5db876b0628bdf6a2174263a1c0a9130e2Dan Gohman/// for details. 37013c5e35222afe0895f0c5e68aa9f22f134ea437aDan Gohman/// 3713abf905b50f33340ae81913da81b0eda96fa4616Dan Gohman/// Design note: The correctness of using getelementptr here depends on 3724c0d5d5db876b0628bdf6a2174263a1c0a9130e2Dan Gohman/// ScalarEvolution not recognizing inttoptr and ptrtoint operators, as 3734c0d5d5db876b0628bdf6a2174263a1c0a9130e2Dan Gohman/// they may introduce pointer arithmetic which may not be safely converted 3744c0d5d5db876b0628bdf6a2174263a1c0a9130e2Dan Gohman/// into getelementptr. 375453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman/// 376453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman/// Design note: It might seem desirable for this function to be more 377453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman/// loop-aware. If some of the indices are loop-invariant while others 378453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman/// aren't, it might seem desirable to emit multiple GEPs, keeping the 379453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman/// loop-invariant portions of the overall computation outside the loop. 380453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman/// However, there are a few reasons this is not done here. Hoisting simple 381453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman/// arithmetic is a low-level optimization that often isn't very 382453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman/// important until late in the optimization process. In fact, passes 383453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman/// like InstructionCombining will combine GEPs, even if it means 384453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman/// pushing loop-invariant computation down into loops, so even if the 385453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman/// GEPs were split here, the work would quickly be undone. The 386453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman/// LoopStrengthReduction pass, which is usually run quite late (and 387453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman/// after the last InstructionCombining pass), takes care of hoisting 388453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman/// loop-invariant portions of expressions, after considering what 389453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman/// can be folded using target addressing modes. 390453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman/// 3910bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan GohmanValue *SCEVExpander::expandAddToGEP(const SCEV *const *op_begin, 3920bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman const SCEV *const *op_end, 393db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner PointerType *PTy, 394db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner Type *Ty, 3955be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman Value *V) { 396db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner Type *ElTy = PTy->getElementType(); 3975be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman SmallVector<Value *, 4> GepIndices; 3980bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman SmallVector<const SCEV *, 8> Ops(op_begin, op_end); 3995be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman bool AnyNonZeroIndices = false; 4005be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman 401c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman // Split AddRecs up into parts as either of the parts may be usable 402c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman // without the other. 403c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman SplitAddRecs(Ops, Ty, SE); 404c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman 40536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines Type *IntPtrTy = SE.DL 40636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines ? SE.DL->getIntPtrType(PTy) 40714807bd8c801f976c999e5a6699f31ee9642021aMatt Arsenault : Type::getInt64Ty(PTy->getContext()); 40814807bd8c801f976c999e5a6699f31ee9642021aMatt Arsenault 409eb35699e642f2c021f04154bdb4bd90a1afb3baaBob Wilson // Descend down the pointer's type and attempt to convert the other 4105be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman // operands into GEP indices, at each level. The first index in a GEP 4115be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman // indexes into the array implied by the pointer operand; the rest of 4125be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman // the indices index into the element or field type selected by the 4135be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman // preceding index. 4145be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman for (;;) { 415c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman // If the scale size is not 0, attempt to factor out a scale for 416c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman // array indexing. 4170bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman SmallVector<const SCEV *, 8> ScaledOps; 418150dfa87113e51881c75676edd8e3549eb702d99Dan Gohman if (ElTy->isSized()) { 41914807bd8c801f976c999e5a6699f31ee9642021aMatt Arsenault const SCEV *ElSize = SE.getSizeOfExpr(IntPtrTy, ElTy); 420150dfa87113e51881c75676edd8e3549eb702d99Dan Gohman if (!ElSize->isZero()) { 421150dfa87113e51881c75676edd8e3549eb702d99Dan Gohman SmallVector<const SCEV *, 8> NewOps; 422150dfa87113e51881c75676edd8e3549eb702d99Dan Gohman for (unsigned i = 0, e = Ops.size(); i != e; ++i) { 423150dfa87113e51881c75676edd8e3549eb702d99Dan Gohman const SCEV *Op = Ops[i]; 424deff621abdd48bd70434bd4d7ef30f08ddba1cd8Dan Gohman const SCEV *Remainder = SE.getConstant(Ty, 0); 42536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if (FactorOutConstant(Op, Remainder, ElSize, SE, SE.DL)) { 426150dfa87113e51881c75676edd8e3549eb702d99Dan Gohman // Op now has ElSize factored out. 427150dfa87113e51881c75676edd8e3549eb702d99Dan Gohman ScaledOps.push_back(Op); 428150dfa87113e51881c75676edd8e3549eb702d99Dan Gohman if (!Remainder->isZero()) 429150dfa87113e51881c75676edd8e3549eb702d99Dan Gohman NewOps.push_back(Remainder); 430150dfa87113e51881c75676edd8e3549eb702d99Dan Gohman AnyNonZeroIndices = true; 431150dfa87113e51881c75676edd8e3549eb702d99Dan Gohman } else { 432150dfa87113e51881c75676edd8e3549eb702d99Dan Gohman // The operand was not divisible, so add it to the list of operands 433150dfa87113e51881c75676edd8e3549eb702d99Dan Gohman // we'll scan next iteration. 434150dfa87113e51881c75676edd8e3549eb702d99Dan Gohman NewOps.push_back(Ops[i]); 435150dfa87113e51881c75676edd8e3549eb702d99Dan Gohman } 436150dfa87113e51881c75676edd8e3549eb702d99Dan Gohman } 437150dfa87113e51881c75676edd8e3549eb702d99Dan Gohman // If we made any changes, update Ops. 438150dfa87113e51881c75676edd8e3549eb702d99Dan Gohman if (!ScaledOps.empty()) { 439150dfa87113e51881c75676edd8e3549eb702d99Dan Gohman Ops = NewOps; 440150dfa87113e51881c75676edd8e3549eb702d99Dan Gohman SimplifyAddOperands(Ops, Ty, SE); 4415be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman } 442c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman } 4435be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman } 444c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman 445c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman // Record the scaled array index for this level of the type. If 446c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman // we didn't find any operands that could be factored, tentatively 447c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman // assume that element zero was selected (since the zero offset 448c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman // would obviously be folded away). 4495be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman Value *Scaled = ScaledOps.empty() ? 450a7235ea7245028a0723e8ab7fd011386b3900777Owen Anderson Constant::getNullValue(Ty) : 4515be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman expandCodeFor(SE.getAddExpr(ScaledOps), Ty); 4525be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman GepIndices.push_back(Scaled); 4535be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman 4545be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman // Collect struct field index operands. 455db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner while (StructType *STy = dyn_cast<StructType>(ElTy)) { 456c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman bool FoundFieldNo = false; 457c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman // An empty struct has no fields. 458c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman if (STy->getNumElements() == 0) break; 45936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if (SE.DL) { 4603574eca1b02600bac4e625297f4ecf745f4c4f32Micah Villmow // With DataLayout, field offsets are known. See if a constant offset 461c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman // falls within any of the struct fields. 462c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman if (Ops.empty()) break; 4635be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[0])) 4645be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman if (SE.getTypeSizeInBits(C->getType()) <= 64) { 46536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines const StructLayout &SL = *SE.DL->getStructLayout(STy); 4665be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman uint64_t FullOffset = C->getValue()->getZExtValue(); 4675be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman if (FullOffset < SL.getSizeInBytes()) { 4685be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman unsigned ElIdx = SL.getElementContainingOffset(FullOffset); 4691d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson GepIndices.push_back( 4701d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson ConstantInt::get(Type::getInt32Ty(Ty->getContext()), ElIdx)); 4715be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman ElTy = STy->getTypeAtIndex(ElIdx); 4725be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman Ops[0] = 4736de29f8d960505421d61c80cdb738e16720b6c0eDan Gohman SE.getConstant(Ty, FullOffset - SL.getElementOffset(ElIdx)); 4745be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman AnyNonZeroIndices = true; 475c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman FoundFieldNo = true; 4765be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman } 4775be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman } 478c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman } else { 4793574eca1b02600bac4e625297f4ecf745f4c4f32Micah Villmow // Without DataLayout, just check for an offsetof expression of the 480c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman // appropriate struct type. 481c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman for (unsigned i = 0, e = Ops.size(); i != e; ++i) 4820f5efe56258f8cd6ceff4d7955a5d80144cd9cb0Dan Gohman if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(Ops[i])) { 483db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner Type *CTy; 4840f5efe56258f8cd6ceff4d7955a5d80144cd9cb0Dan Gohman Constant *FieldNo; 4854f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman if (U->isOffsetOf(CTy, FieldNo) && CTy == STy) { 4860f5efe56258f8cd6ceff4d7955a5d80144cd9cb0Dan Gohman GepIndices.push_back(FieldNo); 4870f5efe56258f8cd6ceff4d7955a5d80144cd9cb0Dan Gohman ElTy = 4880f5efe56258f8cd6ceff4d7955a5d80144cd9cb0Dan Gohman STy->getTypeAtIndex(cast<ConstantInt>(FieldNo)->getZExtValue()); 489c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman Ops[i] = SE.getConstant(Ty, 0); 490c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman AnyNonZeroIndices = true; 491c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman FoundFieldNo = true; 492c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman break; 493c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman } 4940f5efe56258f8cd6ceff4d7955a5d80144cd9cb0Dan Gohman } 4955be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman } 496c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman // If no struct field offsets were found, tentatively assume that 497c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman // field zero was selected (since the zero offset would obviously 498c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman // be folded away). 499c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman if (!FoundFieldNo) { 500c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman ElTy = STy->getTypeAtIndex(0u); 501c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman GepIndices.push_back( 502c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman Constant::getNullValue(Type::getInt32Ty(Ty->getContext()))); 503c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman } 504c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman } 5055be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman 506db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner if (ArrayType *ATy = dyn_cast<ArrayType>(ElTy)) 5075be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman ElTy = ATy->getElementType(); 508c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman else 509c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman break; 5105be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman } 5115be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman 5123f46a3abeedba8d517b4182de34c821d752db058Dan Gohman // If none of the operands were convertible to proper GEP indices, cast 5135be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman // the base to i8* and do an ugly getelementptr with that. It's still 5145be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman // better than ptrtoint+arithmetic+inttoptr at least. 5155be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman if (!AnyNonZeroIndices) { 516c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman // Cast the base to i8*. 5175be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman V = InsertNoopCastOfTo(V, 518ac53a0b272452013124bfc70480aea5e41b60f40Duncan Sands Type::getInt8PtrTy(Ty->getContext(), PTy->getAddressSpace())); 519c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman 520705b48d960dff1a96ac40d0cf932eb465b9a550aRafael Espindola assert(!isa<Instruction>(V) || 521c9ae8cc24c70dda33b68cacf01d2feeeb836f6f2Rafael Espindola SE.DT->dominates(cast<Instruction>(V), Builder.GetInsertPoint())); 5224b04578d65e38cdb5077de2498889e4a174ccdfdRafael Espindola 523c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman // Expand the operands for a plain byte offset. 52492fcdcac543653a62949fe9e5a7bd008500c1380Dan Gohman Value *Idx = expandCodeFor(SE.getAddExpr(Ops), Ty); 5255be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman 5265be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman // Fold a GEP with constant operands. 5275be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman if (Constant *CLHS = dyn_cast<Constant>(V)) 5285be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman if (Constant *CRHS = dyn_cast<Constant>(Idx)) 529dab3d29605a5c83db41b28176273ef55961120c1Jay Foad return ConstantExpr::getGetElementPtr(CLHS, CRHS); 5305be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman 5315be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman // Do a quick scan to see if we have this GEP nearby. If so, reuse it. 5325be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman unsigned ScanLimit = 6; 533267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman BasicBlock::iterator BlockBegin = Builder.GetInsertBlock()->begin(); 534267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman // Scanning starts from the last instruction before the insertion point. 535267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman BasicBlock::iterator IP = Builder.GetInsertPoint(); 536267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman if (IP != BlockBegin) { 5375be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman --IP; 5385be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman for (; ScanLimit; --IP, --ScanLimit) { 5398d50ea760322630dbb9ab6436ca356b279adee10Dale Johannesen // Don't count dbg.value against the ScanLimit, to avoid perturbing the 5408d50ea760322630dbb9ab6436ca356b279adee10Dale Johannesen // generated code. 5418d50ea760322630dbb9ab6436ca356b279adee10Dale Johannesen if (isa<DbgInfoIntrinsic>(IP)) 5428d50ea760322630dbb9ab6436ca356b279adee10Dale Johannesen ScanLimit++; 5435be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman if (IP->getOpcode() == Instruction::GetElementPtr && 5445be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman IP->getOperand(0) == V && IP->getOperand(1) == Idx) 5455be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman return IP; 5465be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman if (IP == BlockBegin) break; 5475be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman } 5485be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman } 5495be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman 550087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman // Save the original insertion point so we can restore it when we're done. 551d4278821665aa97f5fc0d19a32ff1fb39a22d395Benjamin Kramer BuilderType::InsertPointGuard Guard(Builder); 552087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman 553087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman // Move the insertion point out of as many loops as we can. 554087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman while (const Loop *L = SE.LI->getLoopFor(Builder.GetInsertBlock())) { 555087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman if (!L->isLoopInvariant(V) || !L->isLoopInvariant(Idx)) break; 556087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman BasicBlock *Preheader = L->getLoopPreheader(); 557087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman if (!Preheader) break; 558087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman 559087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman // Ok, move up a level. 560087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman Builder.SetInsertPoint(Preheader, Preheader->getTerminator()); 561087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman } 562087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman 563c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman // Emit a GEP. 564c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman Value *GEP = Builder.CreateGEP(V, Idx, "uglygep"); 565a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman rememberInstruction(GEP); 566087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman 5675be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman return GEP; 5685be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman } 5695be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman 570087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman // Save the original insertion point so we can restore it when we're done. 571341562b9fb6574b1c9492e52cec24106cd31ce51Benjamin Kramer BuilderType::InsertPoint SaveInsertPt = Builder.saveIP(); 572087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman 573087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman // Move the insertion point out of as many loops as we can. 574087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman while (const Loop *L = SE.LI->getLoopFor(Builder.GetInsertBlock())) { 575087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman if (!L->isLoopInvariant(V)) break; 576087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman 577087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman bool AnyIndexNotLoopInvariant = false; 578087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman for (SmallVectorImpl<Value *>::const_iterator I = GepIndices.begin(), 579087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman E = GepIndices.end(); I != E; ++I) 580087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman if (!L->isLoopInvariant(*I)) { 581087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman AnyIndexNotLoopInvariant = true; 582087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman break; 583087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman } 584087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman if (AnyIndexNotLoopInvariant) 585087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman break; 586087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman 587087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman BasicBlock *Preheader = L->getLoopPreheader(); 588087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman if (!Preheader) break; 589087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman 590087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman // Ok, move up a level. 591087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman Builder.SetInsertPoint(Preheader, Preheader->getTerminator()); 592087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman } 593087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman 594d6aa02de1076c801ac41295156a2379637976918Dan Gohman // Insert a pretty getelementptr. Note that this GEP is not marked inbounds, 595d6aa02de1076c801ac41295156a2379637976918Dan Gohman // because ScalarEvolution may have changed the address arithmetic to 596d6aa02de1076c801ac41295156a2379637976918Dan Gohman // compute a value which is beyond the end of the allocated object. 597a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman Value *Casted = V; 598a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman if (V->getType() != PTy) 599a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman Casted = InsertNoopCastOfTo(Casted, PTy); 600a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman Value *GEP = Builder.CreateGEP(Casted, 6010a2a60ace9b79164b71794ce7ff981171c61e442Jay Foad GepIndices, 602267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman "scevgep"); 6035be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman Ops.push_back(SE.getUnknown(GEP)); 604a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman rememberInstruction(GEP); 605087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman 606341562b9fb6574b1c9492e52cec24106cd31ce51Benjamin Kramer // Restore the original insert point. 607341562b9fb6574b1c9492e52cec24106cd31ce51Benjamin Kramer Builder.restoreIP(SaveInsertPt); 608341562b9fb6574b1c9492e52cec24106cd31ce51Benjamin Kramer 6095be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman return expand(SE.getAddExpr(Ops)); 6105be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman} 6115be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman 612087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman/// PickMostRelevantLoop - Given two loops pick the one that's most relevant for 613087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman/// SCEV expansion. If they are nested, this is the most nested. If they are 614087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman/// neighboring, pick the later. 615087bd1e3a12893873761736bf0f905a350e9e708Dan Gohmanstatic const Loop *PickMostRelevantLoop(const Loop *A, const Loop *B, 616087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman DominatorTree &DT) { 617087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman if (!A) return B; 618087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman if (!B) return A; 619087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman if (A->contains(B)) return B; 620087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman if (B->contains(A)) return A; 621087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman if (DT.dominates(A->getHeader(), B->getHeader())) return B; 622087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman if (DT.dominates(B->getHeader(), A->getHeader())) return A; 623087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman return A; // Arbitrarily break the tie. 624087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman} 625087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman 6269c9fcfc719158a46cb2e41b66d7dc1a63cd48d74Dan Gohman/// getRelevantLoop - Get the most relevant loop associated with the given 627087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman/// expression, according to PickMostRelevantLoop. 6289c9fcfc719158a46cb2e41b66d7dc1a63cd48d74Dan Gohmanconst Loop *SCEVExpander::getRelevantLoop(const SCEV *S) { 6299c9fcfc719158a46cb2e41b66d7dc1a63cd48d74Dan Gohman // Test whether we've already computed the most relevant loop for this SCEV. 6309c9fcfc719158a46cb2e41b66d7dc1a63cd48d74Dan Gohman std::pair<DenseMap<const SCEV *, const Loop *>::iterator, bool> Pair = 631dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines RelevantLoops.insert(std::make_pair(S, nullptr)); 6329c9fcfc719158a46cb2e41b66d7dc1a63cd48d74Dan Gohman if (!Pair.second) 6339c9fcfc719158a46cb2e41b66d7dc1a63cd48d74Dan Gohman return Pair.first->second; 6349c9fcfc719158a46cb2e41b66d7dc1a63cd48d74Dan Gohman 635087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman if (isa<SCEVConstant>(S)) 6369c9fcfc719158a46cb2e41b66d7dc1a63cd48d74Dan Gohman // A constant has no relevant loops. 637dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines return nullptr; 638087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) { 639087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman if (const Instruction *I = dyn_cast<Instruction>(U->getValue())) 6409c9fcfc719158a46cb2e41b66d7dc1a63cd48d74Dan Gohman return Pair.first->second = SE.LI->getLoopFor(I->getParent()); 6419c9fcfc719158a46cb2e41b66d7dc1a63cd48d74Dan Gohman // A non-instruction has no relevant loops. 642dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines return nullptr; 643087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman } 644087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman if (const SCEVNAryExpr *N = dyn_cast<SCEVNAryExpr>(S)) { 645dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines const Loop *L = nullptr; 646087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) 647087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman L = AR->getLoop(); 648087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman for (SCEVNAryExpr::op_iterator I = N->op_begin(), E = N->op_end(); 649087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman I != E; ++I) 6509c9fcfc719158a46cb2e41b66d7dc1a63cd48d74Dan Gohman L = PickMostRelevantLoop(L, getRelevantLoop(*I), *SE.DT); 6519c9fcfc719158a46cb2e41b66d7dc1a63cd48d74Dan Gohman return RelevantLoops[N] = L; 6529c9fcfc719158a46cb2e41b66d7dc1a63cd48d74Dan Gohman } 6539c9fcfc719158a46cb2e41b66d7dc1a63cd48d74Dan Gohman if (const SCEVCastExpr *C = dyn_cast<SCEVCastExpr>(S)) { 6549c9fcfc719158a46cb2e41b66d7dc1a63cd48d74Dan Gohman const Loop *Result = getRelevantLoop(C->getOperand()); 6559c9fcfc719158a46cb2e41b66d7dc1a63cd48d74Dan Gohman return RelevantLoops[C] = Result; 6569c9fcfc719158a46cb2e41b66d7dc1a63cd48d74Dan Gohman } 6579c9fcfc719158a46cb2e41b66d7dc1a63cd48d74Dan Gohman if (const SCEVUDivExpr *D = dyn_cast<SCEVUDivExpr>(S)) { 6589c9fcfc719158a46cb2e41b66d7dc1a63cd48d74Dan Gohman const Loop *Result = 6599c9fcfc719158a46cb2e41b66d7dc1a63cd48d74Dan Gohman PickMostRelevantLoop(getRelevantLoop(D->getLHS()), 6609c9fcfc719158a46cb2e41b66d7dc1a63cd48d74Dan Gohman getRelevantLoop(D->getRHS()), 6619c9fcfc719158a46cb2e41b66d7dc1a63cd48d74Dan Gohman *SE.DT); 6629c9fcfc719158a46cb2e41b66d7dc1a63cd48d74Dan Gohman return RelevantLoops[D] = Result; 663087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman } 664087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman llvm_unreachable("Unexpected SCEV type!"); 665087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman} 666c4f7ec85ecb760fff2b702c6deb06506b968ba4fDan Gohman 667b35798347ea87b8b6d36155b211016a7769f01abDan Gohmannamespace { 668b35798347ea87b8b6d36155b211016a7769f01abDan Gohman 669087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman/// LoopCompare - Compare loops by PickMostRelevantLoop. 670087bd1e3a12893873761736bf0f905a350e9e708Dan Gohmanclass LoopCompare { 671087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman DominatorTree &DT; 672087bd1e3a12893873761736bf0f905a350e9e708Dan Gohmanpublic: 673087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman explicit LoopCompare(DominatorTree &dt) : DT(dt) {} 674087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman 675087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman bool operator()(std::pair<const Loop *, const SCEV *> LHS, 676087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman std::pair<const Loop *, const SCEV *> RHS) const { 677bb5d92741b2835eff25e4524bc6a5b0fb4fda855Dan Gohman // Keep pointer operands sorted at the end. 678bb5d92741b2835eff25e4524bc6a5b0fb4fda855Dan Gohman if (LHS.second->getType()->isPointerTy() != 679bb5d92741b2835eff25e4524bc6a5b0fb4fda855Dan Gohman RHS.second->getType()->isPointerTy()) 680bb5d92741b2835eff25e4524bc6a5b0fb4fda855Dan Gohman return LHS.second->getType()->isPointerTy(); 681bb5d92741b2835eff25e4524bc6a5b0fb4fda855Dan Gohman 682087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman // Compare loops with PickMostRelevantLoop. 683087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman if (LHS.first != RHS.first) 684087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman return PickMostRelevantLoop(LHS.first, RHS.first, DT) != LHS.first; 685087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman 686087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman // If one operand is a non-constant negative and the other is not, 687087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman // put the non-constant negative on the right so that a sub can 688087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman // be used instead of a negate and add. 689f8fd841a4bb7a59f81cf4642169e8251e039acfeAndrew Trick if (LHS.second->isNonConstantNegative()) { 690f8fd841a4bb7a59f81cf4642169e8251e039acfeAndrew Trick if (!RHS.second->isNonConstantNegative()) 691087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman return false; 692f8fd841a4bb7a59f81cf4642169e8251e039acfeAndrew Trick } else if (RHS.second->isNonConstantNegative()) 693087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman return true; 694087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman 695087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman // Otherwise they are equivalent according to this comparison. 696087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman return false; 697c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman } 698087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman}; 6995be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman 700b35798347ea87b8b6d36155b211016a7769f01abDan Gohman} 701b35798347ea87b8b6d36155b211016a7769f01abDan Gohman 702087bd1e3a12893873761736bf0f905a350e9e708Dan GohmanValue *SCEVExpander::visitAddExpr(const SCEVAddExpr *S) { 703db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner Type *Ty = SE.getEffectiveSCEVType(S->getType()); 704e24fa64d52330626553298f56ba5aa702624c282Dan Gohman 705087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman // Collect all the add operands in a loop, along with their associated loops. 706087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman // Iterate in reverse so that constants are emitted last, all else equal, and 707087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman // so that pointer operands are inserted first, which the code below relies on 708087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman // to form more involved GEPs. 709087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman SmallVector<std::pair<const Loop *, const SCEV *>, 8> OpsAndLoops; 710087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman for (std::reverse_iterator<SCEVAddExpr::op_iterator> I(S->op_end()), 711087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman E(S->op_begin()); I != E; ++I) 7129c9fcfc719158a46cb2e41b66d7dc1a63cd48d74Dan Gohman OpsAndLoops.push_back(std::make_pair(getRelevantLoop(*I), *I)); 713087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman 714087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman // Sort by loop. Use a stable sort so that constants follow non-constants and 715087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman // pointer operands precede non-pointer operands. 716087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman std::stable_sort(OpsAndLoops.begin(), OpsAndLoops.end(), LoopCompare(*SE.DT)); 717087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman 718087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman // Emit instructions to add all the operands. Hoist as much as possible 719087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman // out of loops, and form meaningful getelementptrs where possible. 720dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines Value *Sum = nullptr; 721087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman for (SmallVectorImpl<std::pair<const Loop *, const SCEV *> >::iterator 722087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman I = OpsAndLoops.begin(), E = OpsAndLoops.end(); I != E; ) { 723087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman const Loop *CurLoop = I->first; 724087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman const SCEV *Op = I->second; 725087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman if (!Sum) { 726087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman // This is the first operand. Just expand it. 727087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman Sum = expand(Op); 728087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman ++I; 729db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner } else if (PointerType *PTy = dyn_cast<PointerType>(Sum->getType())) { 730087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman // The running sum expression is a pointer. Try to form a getelementptr 731087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman // at this level with that as the base. 732087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman SmallVector<const SCEV *, 4> NewOps; 733bb5d92741b2835eff25e4524bc6a5b0fb4fda855Dan Gohman for (; I != E && I->first == CurLoop; ++I) { 734bb5d92741b2835eff25e4524bc6a5b0fb4fda855Dan Gohman // If the operand is SCEVUnknown and not instructions, peek through 735bb5d92741b2835eff25e4524bc6a5b0fb4fda855Dan Gohman // it, to enable more of it to be folded into the GEP. 736bb5d92741b2835eff25e4524bc6a5b0fb4fda855Dan Gohman const SCEV *X = I->second; 737bb5d92741b2835eff25e4524bc6a5b0fb4fda855Dan Gohman if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(X)) 738bb5d92741b2835eff25e4524bc6a5b0fb4fda855Dan Gohman if (!isa<Instruction>(U->getValue())) 739bb5d92741b2835eff25e4524bc6a5b0fb4fda855Dan Gohman X = SE.getSCEV(U->getValue()); 740bb5d92741b2835eff25e4524bc6a5b0fb4fda855Dan Gohman NewOps.push_back(X); 741bb5d92741b2835eff25e4524bc6a5b0fb4fda855Dan Gohman } 742087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman Sum = expandAddToGEP(NewOps.begin(), NewOps.end(), PTy, Ty, Sum); 743db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner } else if (PointerType *PTy = dyn_cast<PointerType>(Op->getType())) { 744087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman // The running sum is an integer, and there's a pointer at this level. 745f8d0578e4cbd5922696c92f5068c5513d8e8d60eDan Gohman // Try to form a getelementptr. If the running sum is instructions, 746f8d0578e4cbd5922696c92f5068c5513d8e8d60eDan Gohman // use a SCEVUnknown to avoid re-analyzing them. 747087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman SmallVector<const SCEV *, 4> NewOps; 748f8d0578e4cbd5922696c92f5068c5513d8e8d60eDan Gohman NewOps.push_back(isa<Instruction>(Sum) ? SE.getUnknown(Sum) : 749f8d0578e4cbd5922696c92f5068c5513d8e8d60eDan Gohman SE.getSCEV(Sum)); 750087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman for (++I; I != E && I->first == CurLoop; ++I) 751087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman NewOps.push_back(I->second); 752087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman Sum = expandAddToGEP(NewOps.begin(), NewOps.end(), PTy, Ty, expand(Op)); 753f8fd841a4bb7a59f81cf4642169e8251e039acfeAndrew Trick } else if (Op->isNonConstantNegative()) { 754087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman // Instead of doing a negate and add, just do a subtract. 755a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman Value *W = expandCodeFor(SE.getNegativeSCEV(Op), Ty); 756087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman Sum = InsertNoopCastOfTo(Sum, Ty); 757087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman Sum = InsertBinop(Instruction::Sub, Sum, W); 758087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman ++I; 759a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman } else { 760087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman // A simple add. 761a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman Value *W = expandCodeFor(Op, Ty); 762087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman Sum = InsertNoopCastOfTo(Sum, Ty); 763087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman // Canonicalize a constant to the RHS. 764087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman if (isa<Constant>(Sum)) std::swap(Sum, W); 765087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman Sum = InsertBinop(Instruction::Add, Sum, W); 766087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman ++I; 767a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman } 7682d1be87ee40a4a0241d94448173879d9df2bc5b3Dan Gohman } 769087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman 770087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman return Sum; 771e24fa64d52330626553298f56ba5aa702624c282Dan Gohman} 7725be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman 773890f92b744fb074465bc2b7006ee753a181f62a4Dan GohmanValue *SCEVExpander::visitMulExpr(const SCEVMulExpr *S) { 774db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner Type *Ty = SE.getEffectiveSCEVType(S->getType()); 775087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman 776087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman // Collect all the mul operands in a loop, along with their associated loops. 777087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman // Iterate in reverse so that constants are emitted last, all else equal. 778087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman SmallVector<std::pair<const Loop *, const SCEV *>, 8> OpsAndLoops; 779087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman for (std::reverse_iterator<SCEVMulExpr::op_iterator> I(S->op_end()), 780087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman E(S->op_begin()); I != E; ++I) 7819c9fcfc719158a46cb2e41b66d7dc1a63cd48d74Dan Gohman OpsAndLoops.push_back(std::make_pair(getRelevantLoop(*I), *I)); 782087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman 783087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman // Sort by loop. Use a stable sort so that constants follow non-constants. 784087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman std::stable_sort(OpsAndLoops.begin(), OpsAndLoops.end(), LoopCompare(*SE.DT)); 785087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman 786087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman // Emit instructions to mul all the operands. Hoist as much as possible 787087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman // out of loops. 788dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines Value *Prod = nullptr; 789087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman for (SmallVectorImpl<std::pair<const Loop *, const SCEV *> >::iterator 790087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman I = OpsAndLoops.begin(), E = OpsAndLoops.end(); I != E; ) { 791087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman const SCEV *Op = I->second; 792087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman if (!Prod) { 793087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman // This is the first operand. Just expand it. 794087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman Prod = expand(Op); 795087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman ++I; 796087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman } else if (Op->isAllOnesValue()) { 797087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman // Instead of doing a multiply by negative one, just do a negate. 798087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman Prod = InsertNoopCastOfTo(Prod, Ty); 799087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman Prod = InsertBinop(Instruction::Sub, Constant::getNullValue(Ty), Prod); 800087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman ++I; 801087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman } else { 802087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman // A simple mul. 803087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman Value *W = expandCodeFor(Op, Ty); 804087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman Prod = InsertNoopCastOfTo(Prod, Ty); 805087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman // Canonicalize a constant to the RHS. 806087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman if (isa<Constant>(Prod)) std::swap(Prod, W); 807087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman Prod = InsertBinop(Instruction::Mul, Prod, W); 808087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman ++I; 809087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman } 8102d1be87ee40a4a0241d94448173879d9df2bc5b3Dan Gohman } 8112d1be87ee40a4a0241d94448173879d9df2bc5b3Dan Gohman 812087bd1e3a12893873761736bf0f905a350e9e708Dan Gohman return Prod; 81336f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman} 81436f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman 815890f92b744fb074465bc2b7006ee753a181f62a4Dan GohmanValue *SCEVExpander::visitUDivExpr(const SCEVUDivExpr *S) { 816db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner Type *Ty = SE.getEffectiveSCEVType(S->getType()); 8172d1be87ee40a4a0241d94448173879d9df2bc5b3Dan Gohman 81892fcdcac543653a62949fe9e5a7bd008500c1380Dan Gohman Value *LHS = expandCodeFor(S->getLHS(), Ty); 819890f92b744fb074465bc2b7006ee753a181f62a4Dan Gohman if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(S->getRHS())) { 8206177fd4fcee4d82692c47e33754ffe285c38cc69Nick Lewycky const APInt &RHS = SC->getValue()->getValue(); 8216177fd4fcee4d82692c47e33754ffe285c38cc69Nick Lewycky if (RHS.isPowerOf2()) 8226177fd4fcee4d82692c47e33754ffe285c38cc69Nick Lewycky return InsertBinop(Instruction::LShr, LHS, 823eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson ConstantInt::get(Ty, RHS.logBase2())); 8246177fd4fcee4d82692c47e33754ffe285c38cc69Nick Lewycky } 8256177fd4fcee4d82692c47e33754ffe285c38cc69Nick Lewycky 82692fcdcac543653a62949fe9e5a7bd008500c1380Dan Gohman Value *RHS = expandCodeFor(S->getRHS(), Ty); 827267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman return InsertBinop(Instruction::UDiv, LHS, RHS); 8286177fd4fcee4d82692c47e33754ffe285c38cc69Nick Lewycky} 8296177fd4fcee4d82692c47e33754ffe285c38cc69Nick Lewycky 830453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman/// Move parts of Base into Rest to leave Base with the minimal 831453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman/// expression that provides a pointer operand suitable for a 832453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman/// GEP expansion. 8330bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohmanstatic void ExposePointerBase(const SCEV *&Base, const SCEV *&Rest, 834453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman ScalarEvolution &SE) { 835453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman while (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(Base)) { 836453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman Base = A->getStart(); 837453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman Rest = SE.getAddExpr(Rest, 838deff621abdd48bd70434bd4d7ef30f08ddba1cd8Dan Gohman SE.getAddRecExpr(SE.getConstant(A->getType(), 0), 839453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman A->getStepRecurrence(SE), 8403228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick A->getLoop(), 8416f71dd765ae9e1d1d0ba01d98a05627ffe3bfc8aAndrew Trick A->getNoWrapFlags(SCEV::FlagNW))); 842453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman } 843453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(Base)) { 844453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman Base = A->getOperand(A->getNumOperands()-1); 8450bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman SmallVector<const SCEV *, 8> NewAddOps(A->op_begin(), A->op_end()); 846453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman NewAddOps.back() = Rest; 847453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman Rest = SE.getAddExpr(NewAddOps); 848453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman ExposePointerBase(Base, Rest, SE); 849453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman } 850453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman} 851453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman 852c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick/// Determine if this is a well-behaved chain of instructions leading back to 853c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick/// the PHI. If so, it may be reused by expanded expressions. 854c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trickbool SCEVExpander::isNormalAddRecExprPHI(PHINode *PN, Instruction *IncV, 855c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick const Loop *L) { 856c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick if (IncV->getNumOperands() == 0 || isa<PHINode>(IncV) || 857c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick (isa<CastInst>(IncV) && !isa<BitCastInst>(IncV))) 858c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick return false; 859c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick // If any of the operands don't dominate the insert position, bail. 860c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick // Addrec operands are always loop-invariant, so this can only happen 861c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick // if there are instructions which haven't been hoisted. 862c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick if (L == IVIncInsertLoop) { 863c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick for (User::op_iterator OI = IncV->op_begin()+1, 864c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick OE = IncV->op_end(); OI != OE; ++OI) 865c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick if (Instruction *OInst = dyn_cast<Instruction>(OI)) 866c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick if (!SE.DT->dominates(OInst, IVIncInsertPos)) 867c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick return false; 868c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick } 869c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick // Advance to the next instruction. 870c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick IncV = dyn_cast<Instruction>(IncV->getOperand(0)); 871c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick if (!IncV) 872c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick return false; 873c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick 874c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick if (IncV->mayHaveSideEffects()) 875c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick return false; 876c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick 877c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick if (IncV != PN) 878c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick return true; 879c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick 880c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick return isNormalAddRecExprPHI(PN, IncV, L); 881c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick} 882c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick 883b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick/// getIVIncOperand returns an induction variable increment's induction 884b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick/// variable operand. 885b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick/// 886b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick/// If allowScale is set, any type of GEP is allowed as long as the nonIV 887b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick/// operands dominate InsertPos. 888b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick/// 889b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick/// If allowScale is not set, ensure that a GEP increment conforms to one of the 890b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick/// simple patterns generated by getAddRecExprPHILiterally and 891b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick/// expandAddtoGEP. If the pattern isn't recognized, return NULL. 892b5c26ef9da16052597d59a412eaae32098aa1be0Andrew TrickInstruction *SCEVExpander::getIVIncOperand(Instruction *IncV, 893b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick Instruction *InsertPos, 894b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick bool allowScale) { 895b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick if (IncV == InsertPos) 896dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines return nullptr; 89764925c55c65f9345a69fb67db07aa62cfb723577Andrew Trick 898c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick switch (IncV->getOpcode()) { 899b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick default: 900dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines return nullptr; 901c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick // Check for a simple Add/Sub or GEP of a loop invariant step. 902c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick case Instruction::Add: 903b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick case Instruction::Sub: { 904b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick Instruction *OInst = dyn_cast<Instruction>(IncV->getOperand(1)); 905c9ae8cc24c70dda33b68cacf01d2feeeb836f6f2Rafael Espindola if (!OInst || SE.DT->dominates(OInst, InsertPos)) 906b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick return dyn_cast<Instruction>(IncV->getOperand(0)); 907dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines return nullptr; 908b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick } 909c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick case Instruction::BitCast: 910b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick return dyn_cast<Instruction>(IncV->getOperand(0)); 911b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick case Instruction::GetElementPtr: 912c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick for (Instruction::op_iterator I = IncV->op_begin()+1, E = IncV->op_end(); 913c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick I != E; ++I) { 914c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick if (isa<Constant>(*I)) 915c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick continue; 916b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick if (Instruction *OInst = dyn_cast<Instruction>(*I)) { 917c9ae8cc24c70dda33b68cacf01d2feeeb836f6f2Rafael Espindola if (!SE.DT->dominates(OInst, InsertPos)) 918dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines return nullptr; 919b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick } 920b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick if (allowScale) { 921b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick // allow any kind of GEP as long as it can be hoisted. 922b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick continue; 923b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick } 924b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick // This must be a pointer addition of constants (pretty), which is already 925b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick // handled, or some number of address-size elements (ugly). Ugly geps 926b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick // have 2 operands. i1* is used by the expander to represent an 927b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick // address-size element. 928c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick if (IncV->getNumOperands() != 2) 929dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines return nullptr; 930365c9f1ff55bef134c6b9707f7df44d680ddabeaAndrew Trick unsigned AS = cast<PointerType>(IncV->getType())->getAddressSpace(); 931c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick if (IncV->getType() != Type::getInt1PtrTy(SE.getContext(), AS) 932c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick && IncV->getType() != Type::getInt8PtrTy(SE.getContext(), AS)) 933dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines return nullptr; 934c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick break; 935c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick } 936b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick return dyn_cast<Instruction>(IncV->getOperand(0)); 937c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick } 938b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick} 939b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick 940b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick/// hoistStep - Attempt to hoist a simple IV increment above InsertPos to make 941b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick/// it available to other uses in this loop. Recursively hoist any operands, 942b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick/// until we reach a value that dominates InsertPos. 943b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trickbool SCEVExpander::hoistIVInc(Instruction *IncV, Instruction *InsertPos) { 944c9ae8cc24c70dda33b68cacf01d2feeeb836f6f2Rafael Espindola if (SE.DT->dominates(IncV, InsertPos)) 945b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick return true; 946b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick 947b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick // InsertPos must itself dominate IncV so that IncV's new position satisfies 948b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick // its existing users. 9493de8ad8bf9081fcb7ec0600fd2a8d1ce809f556dAndrew Trick if (isa<PHINode>(InsertPos) 9503de8ad8bf9081fcb7ec0600fd2a8d1ce809f556dAndrew Trick || !SE.DT->dominates(InsertPos->getParent(), IncV->getParent())) 951c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick return false; 952b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick 953b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick // Check that the chain of IV operands leading back to Phi can be hoisted. 954b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick SmallVector<Instruction*, 4> IVIncs; 955b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick for(;;) { 956b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick Instruction *Oper = getIVIncOperand(IncV, InsertPos, /*allowScale*/true); 957b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick if (!Oper) 958b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick return false; 959b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick // IncV is safe to hoist. 960b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick IVIncs.push_back(IncV); 961b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick IncV = Oper; 962c9ae8cc24c70dda33b68cacf01d2feeeb836f6f2Rafael Espindola if (SE.DT->dominates(IncV, InsertPos)) 963b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick break; 964b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick } 965b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick for (SmallVectorImpl<Instruction*>::reverse_iterator I = IVIncs.rbegin(), 966b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick E = IVIncs.rend(); I != E; ++I) { 967b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick (*I)->moveBefore(InsertPos); 968b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick } 969b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick return true; 970b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick} 971b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick 972b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick/// Determine if this cyclic phi is in a form that would have been generated by 973b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick/// LSR. We don't care if the phi was actually expanded in this pass, as long 974b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick/// as it is in a low-cost form, for example, no implied multiplication. This 975b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick/// should match any patterns generated by getAddRecExprPHILiterally and 976b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick/// expandAddtoGEP. 977b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trickbool SCEVExpander::isExpandedAddRecExprPHI(PHINode *PN, Instruction *IncV, 978b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick const Loop *L) { 979b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick for(Instruction *IVOper = IncV; 980b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick (IVOper = getIVIncOperand(IVOper, L->getLoopPreheader()->getTerminator(), 981b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick /*allowScale=*/false));) { 982b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick if (IVOper == PN) 983b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick return true; 984c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick } 985b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick return false; 986c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick} 987c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick 988553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick/// expandIVInc - Expand an IV increment at Builder's current InsertPos. 989553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick/// Typically this is the LatchBlock terminator or IVIncInsertPos, but we may 990553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick/// need to materialize IV increments elsewhere to handle difficult situations. 991553fe05f236f46fe27b7fcfa822b06367d50183eAndrew TrickValue *SCEVExpander::expandIVInc(PHINode *PN, Value *StepV, const Loop *L, 992553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick Type *ExpandTy, Type *IntTy, 993553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick bool useSubtract) { 994553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick Value *IncV; 995553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick // If the PHI is a pointer, use a GEP, otherwise use an add or sub. 996553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick if (ExpandTy->isPointerTy()) { 997553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick PointerType *GEPPtrTy = cast<PointerType>(ExpandTy); 998553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick // If the step isn't constant, don't use an implicitly scaled GEP, because 999553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick // that would require a multiply inside the loop. 1000553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick if (!isa<ConstantInt>(StepV)) 1001553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick GEPPtrTy = PointerType::get(Type::getInt1Ty(SE.getContext()), 1002553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick GEPPtrTy->getAddressSpace()); 1003553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick const SCEV *const StepArray[1] = { SE.getSCEV(StepV) }; 1004553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick IncV = expandAddToGEP(StepArray, StepArray+1, GEPPtrTy, IntTy, PN); 1005553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick if (IncV->getType() != PN->getType()) { 1006553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick IncV = Builder.CreateBitCast(IncV, PN->getType()); 1007553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick rememberInstruction(IncV); 1008553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick } 1009553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick } else { 1010553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick IncV = useSubtract ? 1011553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick Builder.CreateSub(PN, StepV, Twine(IVName) + ".iv.next") : 1012553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick Builder.CreateAdd(PN, StepV, Twine(IVName) + ".iv.next"); 1013553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick rememberInstruction(IncV); 1014553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick } 1015553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick return IncV; 1016553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick} 1017553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick 101836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// \brief Hoist the addrec instruction chain rooted in the loop phi above the 101936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// position. This routine assumes that this is possible (has been checked). 102036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesstatic void hoistBeforePos(DominatorTree *DT, Instruction *InstToHoist, 102136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines Instruction *Pos, PHINode *LoopPhi) { 102236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines do { 102336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if (DT->dominates(InstToHoist, Pos)) 102436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines break; 102536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // Make sure the increment is where we want it. But don't move it 102636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // down past a potential existing post-inc user. 102736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines InstToHoist->moveBefore(Pos); 102836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines Pos = InstToHoist; 102936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines InstToHoist = cast<Instruction>(InstToHoist->getOperand(0)); 103036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines } while (InstToHoist != LoopPhi); 103136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines} 103236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 103336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// \brief Check whether we can cheaply express the requested SCEV in terms of 103436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// the available PHI SCEV by truncation and/or invertion of the step. 103536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesstatic bool canBeCheaplyTransformed(ScalarEvolution &SE, 103636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines const SCEVAddRecExpr *Phi, 103736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines const SCEVAddRecExpr *Requested, 103836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines bool &InvertStep) { 103936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines Type *PhiTy = SE.getEffectiveSCEVType(Phi->getType()); 104036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines Type *RequestedTy = SE.getEffectiveSCEVType(Requested->getType()); 104136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 104236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if (RequestedTy->getIntegerBitWidth() > PhiTy->getIntegerBitWidth()) 104336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines return false; 104436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 104536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // Try truncate it if necessary. 104636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines Phi = dyn_cast<SCEVAddRecExpr>(SE.getTruncateOrNoop(Phi, RequestedTy)); 104736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if (!Phi) 104836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines return false; 104936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 105036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // Check whether truncation will help. 105136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if (Phi == Requested) { 105236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines InvertStep = false; 105336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines return true; 105436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines } 105536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 105636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // Check whether inverting will help: {R,+,-1} == R - {0,+,1}. 105736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if (SE.getAddExpr(Requested->getStart(), 105836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines SE.getNegativeSCEV(Requested)) == Phi) { 105936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines InvertStep = true; 106036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines return true; 106136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines } 106236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 106336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines return false; 106436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines} 106536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 1066a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman/// getAddRecExprPHILiterally - Helper for expandAddRecExprLiterally. Expand 1067a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman/// the base addrec, which is the addrec without any non-loop-dominating 1068a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman/// values, and return the PHI. 1069a10756ee657a4d43a48cca5c166919093930ed6bDan GohmanPHINode * 1070a10756ee657a4d43a48cca5c166919093930ed6bDan GohmanSCEVExpander::getAddRecExprPHILiterally(const SCEVAddRecExpr *Normalized, 1071a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman const Loop *L, 1072db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner Type *ExpandTy, 107336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines Type *IntTy, 107436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines Type *&TruncTy, 107536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines bool &InvertStep) { 107693a896e2e36480d55de3bab53e68581e08526344Benjamin Kramer assert((!IVIncInsertLoop||IVIncInsertPos) && "Uninitialized insert position"); 1077d152d03a476b8d0d4b26577db26e2ba76034b0f3Andrew Trick 1078a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman // Reuse a previously-inserted PHI, if present. 1079c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick BasicBlock *LatchBlock = L->getLoopLatch(); 1080c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick if (LatchBlock) { 1081dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines PHINode *AddRecPhiMatch = nullptr; 1082dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines Instruction *IncV = nullptr; 1083dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines TruncTy = nullptr; 108436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines InvertStep = false; 108536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 108636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // Only try partially matching scevs that need truncation and/or 108736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // step-inversion if we know this loop is outside the current loop. 108836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines bool TryNonMatchingSCEV = IVIncInsertLoop && 108936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines SE.DT->properlyDominates(LatchBlock, IVIncInsertLoop->getHeader()); 109036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 1091c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick for (BasicBlock::iterator I = L->getHeader()->begin(); 1092c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick PHINode *PN = dyn_cast<PHINode>(I); ++I) { 109336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if (!SE.isSCEVable(PN->getType())) 109436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines continue; 109536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 109636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines const SCEVAddRecExpr *PhiSCEV = dyn_cast<SCEVAddRecExpr>(SE.getSCEV(PN)); 109736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if (!PhiSCEV) 1098c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick continue; 1099c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick 110036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines bool IsMatchingSCEV = PhiSCEV == Normalized; 110136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // We only handle truncation and inversion of phi recurrences for the 110236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // expanded expression if the expanded expression's loop dominates the 110336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // loop we insert to. Check now, so we can bail out early. 110436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if (!IsMatchingSCEV && !TryNonMatchingSCEV) 110536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines continue; 110636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 110736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines Instruction *TempIncV = 110836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines cast<Instruction>(PN->getIncomingValueForBlock(LatchBlock)); 1109c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick 111036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // Check whether we can reuse this PHI node. 1111c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick if (LSRMode) { 111236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if (!isExpandedAddRecExprPHI(PN, TempIncV, L)) 1113c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick continue; 111436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if (L == IVIncInsertLoop && !hoistIVInc(TempIncV, IVIncInsertPos)) 1115b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick continue; 111636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines } else { 111736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if (!isNormalAddRecExprPHI(PN, TempIncV, L)) 1118c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick continue; 1119c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick } 112036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 112136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // Stop if we have found an exact match SCEV. 112236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if (IsMatchingSCEV) { 112336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines IncV = TempIncV; 1124dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines TruncTy = nullptr; 112536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines InvertStep = false; 112636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines AddRecPhiMatch = PN; 112736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines break; 112836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines } 112936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 113036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // Try whether the phi can be translated into the requested form 113136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // (truncated and/or offset by a constant). 113236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if ((!TruncTy || InvertStep) && 113336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines canBeCheaplyTransformed(SE, PhiSCEV, Normalized, InvertStep)) { 113436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // Record the phi node. But don't stop we might find an exact match 113536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // later. 113636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines AddRecPhiMatch = PN; 113736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines IncV = TempIncV; 113836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines TruncTy = SE.getEffectiveSCEVType(Normalized->getType()); 113936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines } 114036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines } 114136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 114236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if (AddRecPhiMatch) { 114336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // Potentially, move the increment. We have made sure in 114436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // isExpandedAddRecExprPHI or hoistIVInc that this is possible. 114536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if (L == IVIncInsertLoop) 114636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines hoistBeforePos(SE.DT, IncV, IVIncInsertPos, AddRecPhiMatch); 114736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 1148c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick // Ok, the add recurrence looks usable. 1149c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick // Remember this PHI, even in post-inc mode. 115036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines InsertedValues.insert(AddRecPhiMatch); 1151c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick // Remember the increment. 1152c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick rememberInstruction(IncV); 115336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines return AddRecPhiMatch; 1154c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick } 1155c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick } 1156a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman 1157a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman // Save the original insertion point so we can restore it when we're done. 1158d4278821665aa97f5fc0d19a32ff1fb39a22d395Benjamin Kramer BuilderType::InsertPointGuard Guard(Builder); 1159a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman 1160ba3c0bc364a31bfbcc570504aae2172914b1fe9fAndrew Trick // Another AddRec may need to be recursively expanded below. For example, if 1161ba3c0bc364a31bfbcc570504aae2172914b1fe9fAndrew Trick // this AddRec is quadratic, the StepV may itself be an AddRec in this 1162ba3c0bc364a31bfbcc570504aae2172914b1fe9fAndrew Trick // loop. Remove this loop from the PostIncLoops set before expanding such 1163ba3c0bc364a31bfbcc570504aae2172914b1fe9fAndrew Trick // AddRecs. Otherwise, we cannot find a valid position for the step 1164ba3c0bc364a31bfbcc570504aae2172914b1fe9fAndrew Trick // (i.e. StepV can never dominate its loop header). Ideally, we could do 1165ba3c0bc364a31bfbcc570504aae2172914b1fe9fAndrew Trick // SavedIncLoops.swap(PostIncLoops), but we generally have a single element, 1166ba3c0bc364a31bfbcc570504aae2172914b1fe9fAndrew Trick // so it's not worth implementing SmallPtrSet::swap. 1167ba3c0bc364a31bfbcc570504aae2172914b1fe9fAndrew Trick PostIncLoopSet SavedPostIncLoops = PostIncLoops; 1168ba3c0bc364a31bfbcc570504aae2172914b1fe9fAndrew Trick PostIncLoops.clear(); 1169ba3c0bc364a31bfbcc570504aae2172914b1fe9fAndrew Trick 1170a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman // Expand code for the start value. 1171a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman Value *StartV = expandCodeFor(Normalized->getStart(), ExpandTy, 1172a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman L->getHeader()->begin()); 1173a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman 1174d152d03a476b8d0d4b26577db26e2ba76034b0f3Andrew Trick // StartV must be hoisted into L's preheader to dominate the new phi. 117593a896e2e36480d55de3bab53e68581e08526344Benjamin Kramer assert(!isa<Instruction>(StartV) || 117693a896e2e36480d55de3bab53e68581e08526344Benjamin Kramer SE.DT->properlyDominates(cast<Instruction>(StartV)->getParent(), 117793a896e2e36480d55de3bab53e68581e08526344Benjamin Kramer L->getHeader())); 1178d152d03a476b8d0d4b26577db26e2ba76034b0f3Andrew Trick 1179553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick // Expand code for the step value. Do this before creating the PHI so that PHI 1180553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick // reuse code doesn't see an incomplete PHI. 1181a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman const SCEV *Step = Normalized->getStepRecurrence(SE); 1182553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick // If the stride is negative, insert a sub instead of an add for the increment 1183553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick // (unless it's a constant, because subtracts of constants are canonicalized 1184553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick // to adds). 1185f8fd841a4bb7a59f81cf4642169e8251e039acfeAndrew Trick bool useSubtract = !ExpandTy->isPointerTy() && Step->isNonConstantNegative(); 1186553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick if (useSubtract) 1187a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman Step = SE.getNegativeSCEV(Step); 1188553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick // Expand the step somewhere that dominates the loop header. 1189a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman Value *StepV = expandCodeFor(Step, IntTy, L->getHeader()->begin()); 1190a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman 1191a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman // Create the PHI. 1192d8b4fb4aab4d6fedb2b14bed1b846451b17bde7cJay Foad BasicBlock *Header = L->getHeader(); 1193d8b4fb4aab4d6fedb2b14bed1b846451b17bde7cJay Foad Builder.SetInsertPoint(Header, Header->begin()); 1194d8b4fb4aab4d6fedb2b14bed1b846451b17bde7cJay Foad pred_iterator HPB = pred_begin(Header), HPE = pred_end(Header); 11955e7645be4c9dd2193add44d30b5fef8036d7a3ceAndrew Trick PHINode *PN = Builder.CreatePHI(ExpandTy, std::distance(HPB, HPE), 1196dc8e546048db2f7ff5656742b2b26975098a11a0Andrew Trick Twine(IVName) + ".iv"); 1197a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman rememberInstruction(PN); 1198a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman 1199a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman // Create the step instructions and populate the PHI. 1200d8b4fb4aab4d6fedb2b14bed1b846451b17bde7cJay Foad for (pred_iterator HPI = HPB; HPI != HPE; ++HPI) { 1201a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman BasicBlock *Pred = *HPI; 1202a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman 1203a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman // Add a start value. 1204a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman if (!L->contains(Pred)) { 1205a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman PN->addIncoming(StartV, Pred); 1206a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman continue; 1207a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman } 1208a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman 1209553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick // Create a step value and add it to the PHI. 1210553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick // If IVIncInsertLoop is non-null and equal to the addrec's loop, insert the 1211553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick // instructions at IVIncInsertPos. 1212a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman Instruction *InsertPos = L == IVIncInsertLoop ? 1213a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman IVIncInsertPos : Pred->getTerminator(); 1214c5ecbdc1896f1cc089372feef3191ace2f840898Devang Patel Builder.SetInsertPoint(InsertPos); 1215553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick Value *IncV = expandIVInc(PN, StepV, L, ExpandTy, IntTy, useSubtract); 1216409443b1c6415e55c2bd4f0662e14cbc52d16686Andrew Trick if (isa<OverflowingBinaryOperator>(IncV)) { 1217409443b1c6415e55c2bd4f0662e14cbc52d16686Andrew Trick if (Normalized->getNoWrapFlags(SCEV::FlagNUW)) 1218409443b1c6415e55c2bd4f0662e14cbc52d16686Andrew Trick cast<BinaryOperator>(IncV)->setHasNoUnsignedWrap(); 1219409443b1c6415e55c2bd4f0662e14cbc52d16686Andrew Trick if (Normalized->getNoWrapFlags(SCEV::FlagNSW)) 1220409443b1c6415e55c2bd4f0662e14cbc52d16686Andrew Trick cast<BinaryOperator>(IncV)->setHasNoSignedWrap(); 1221409443b1c6415e55c2bd4f0662e14cbc52d16686Andrew Trick } 1222a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman PN->addIncoming(IncV, Pred); 1223a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman } 1224a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman 1225ba3c0bc364a31bfbcc570504aae2172914b1fe9fAndrew Trick // After expanding subexpressions, restore the PostIncLoops set so the caller 1226ba3c0bc364a31bfbcc570504aae2172914b1fe9fAndrew Trick // can ensure that IVIncrement dominates the current uses. 1227ba3c0bc364a31bfbcc570504aae2172914b1fe9fAndrew Trick PostIncLoops = SavedPostIncLoops; 1228ba3c0bc364a31bfbcc570504aae2172914b1fe9fAndrew Trick 1229a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman // Remember this PHI, even in post-inc mode. 1230a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman InsertedValues.insert(PN); 1231a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman 1232a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman return PN; 1233a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman} 1234a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman 1235a10756ee657a4d43a48cca5c166919093930ed6bDan GohmanValue *SCEVExpander::expandAddRecExprLiterally(const SCEVAddRecExpr *S) { 1236db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner Type *STy = S->getType(); 1237db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner Type *IntTy = SE.getEffectiveSCEVType(STy); 1238a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman const Loop *L = S->getLoop(); 1239a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman 1240a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman // Determine a normalized form of this expression, which is the expression 1241a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman // before any post-inc adjustment is made. 1242a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman const SCEVAddRecExpr *Normalized = S; 1243448db1cdef5872713ef77beffacf502ae3450cd7Dan Gohman if (PostIncLoops.count(L)) { 1244448db1cdef5872713ef77beffacf502ae3450cd7Dan Gohman PostIncLoopSet Loops; 1245448db1cdef5872713ef77beffacf502ae3450cd7Dan Gohman Loops.insert(L); 1246448db1cdef5872713ef77beffacf502ae3450cd7Dan Gohman Normalized = 1247dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines cast<SCEVAddRecExpr>(TransformForPostIncUse(Normalize, S, nullptr, 1248dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines nullptr, Loops, SE, *SE.DT)); 1249a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman } 1250a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman 1251a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman // Strip off any non-loop-dominating component from the addrec start. 1252a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman const SCEV *Start = Normalized->getStart(); 1253dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines const SCEV *PostLoopOffset = nullptr; 1254dc0e8fb9f9512622f55f73e1a434caa5c0915694Dan Gohman if (!SE.properlyDominates(Start, L->getHeader())) { 1255a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman PostLoopOffset = Start; 1256deff621abdd48bd70434bd4d7ef30f08ddba1cd8Dan Gohman Start = SE.getConstant(Normalized->getType(), 0); 12573228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick Normalized = cast<SCEVAddRecExpr>( 12583228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick SE.getAddRecExpr(Start, Normalized->getStepRecurrence(SE), 12593228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick Normalized->getLoop(), 12606f71dd765ae9e1d1d0ba01d98a05627ffe3bfc8aAndrew Trick Normalized->getNoWrapFlags(SCEV::FlagNW))); 1261a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman } 1262a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman 1263a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman // Strip off any non-loop-dominating component from the addrec step. 1264a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman const SCEV *Step = Normalized->getStepRecurrence(SE); 1265dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines const SCEV *PostLoopScale = nullptr; 1266dc0e8fb9f9512622f55f73e1a434caa5c0915694Dan Gohman if (!SE.dominates(Step, L->getHeader())) { 1267a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman PostLoopScale = Step; 1268deff621abdd48bd70434bd4d7ef30f08ddba1cd8Dan Gohman Step = SE.getConstant(Normalized->getType(), 1); 1269a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman Normalized = 12706f71dd765ae9e1d1d0ba01d98a05627ffe3bfc8aAndrew Trick cast<SCEVAddRecExpr>(SE.getAddRecExpr( 12716f71dd765ae9e1d1d0ba01d98a05627ffe3bfc8aAndrew Trick Start, Step, Normalized->getLoop(), 12726f71dd765ae9e1d1d0ba01d98a05627ffe3bfc8aAndrew Trick Normalized->getNoWrapFlags(SCEV::FlagNW))); 1273a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman } 1274a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman 1275a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman // Expand the core addrec. If we need post-loop scaling, force it to 1276a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman // expand to an integer type to avoid the need for additional casting. 1277db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner Type *ExpandTy = PostLoopScale ? IntTy : STy; 127836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // In some cases, we decide to reuse an existing phi node but need to truncate 127936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // it and/or invert the step. 1280dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines Type *TruncTy = nullptr; 128136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines bool InvertStep = false; 128236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines PHINode *PN = getAddRecExprPHILiterally(Normalized, L, ExpandTy, IntTy, 128336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines TruncTy, InvertStep); 1284a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman 12853f46a3abeedba8d517b4182de34c821d752db058Dan Gohman // Accommodate post-inc mode, if necessary. 1286a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman Value *Result; 1287448db1cdef5872713ef77beffacf502ae3450cd7Dan Gohman if (!PostIncLoops.count(L)) 1288a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman Result = PN; 1289a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman else { 1290a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman // In PostInc mode, use the post-incremented value. 1291a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman BasicBlock *LatchBlock = L->getLoopLatch(); 1292a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman assert(LatchBlock && "PostInc mode requires a unique loop latch!"); 1293a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman Result = PN->getIncomingValueForBlock(LatchBlock); 129448ba0e45ed68689ce7b384578e6272410e4e23feAndrew Trick 129548ba0e45ed68689ce7b384578e6272410e4e23feAndrew Trick // For an expansion to use the postinc form, the client must call 129648ba0e45ed68689ce7b384578e6272410e4e23feAndrew Trick // expandCodeFor with an InsertPoint that is either outside the PostIncLoop 129748ba0e45ed68689ce7b384578e6272410e4e23feAndrew Trick // or dominated by IVIncInsertPos. 1298553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick if (isa<Instruction>(Result) 1299553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick && !SE.DT->dominates(cast<Instruction>(Result), 1300553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick Builder.GetInsertPoint())) { 1301553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick // The induction variable's postinc expansion does not dominate this use. 1302553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick // IVUsers tries to prevent this case, so it is rare. However, it can 1303553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick // happen when an IVUser outside the loop is not dominated by the latch 1304553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick // block. Adjusting IVIncInsertPos before expansion begins cannot handle 1305553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick // all cases. Consider a phi outide whose operand is replaced during 1306553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick // expansion with the value of the postinc user. Without fundamentally 1307553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick // changing the way postinc users are tracked, the only remedy is 1308553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick // inserting an extra IV increment. StepV might fold into PostLoopOffset, 1309553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick // but hopefully expandCodeFor handles that. 1310553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick bool useSubtract = 1311f8fd841a4bb7a59f81cf4642169e8251e039acfeAndrew Trick !ExpandTy->isPointerTy() && Step->isNonConstantNegative(); 1312553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick if (useSubtract) 1313553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick Step = SE.getNegativeSCEV(Step); 1314d4278821665aa97f5fc0d19a32ff1fb39a22d395Benjamin Kramer Value *StepV; 1315d4278821665aa97f5fc0d19a32ff1fb39a22d395Benjamin Kramer { 1316d4278821665aa97f5fc0d19a32ff1fb39a22d395Benjamin Kramer // Expand the step somewhere that dominates the loop header. 1317d4278821665aa97f5fc0d19a32ff1fb39a22d395Benjamin Kramer BuilderType::InsertPointGuard Guard(Builder); 1318d4278821665aa97f5fc0d19a32ff1fb39a22d395Benjamin Kramer StepV = expandCodeFor(Step, IntTy, L->getHeader()->begin()); 1319d4278821665aa97f5fc0d19a32ff1fb39a22d395Benjamin Kramer } 1320553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick Result = expandIVInc(PN, StepV, L, ExpandTy, IntTy, useSubtract); 1321553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick } 1322a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman } 1323a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman 132436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // We have decided to reuse an induction variable of a dominating loop. Apply 132536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // truncation and/or invertion of the step. 132636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if (TruncTy) { 132736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines Type *ResTy = Result->getType(); 132836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // Normalize the result type. 132936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if (ResTy != SE.getEffectiveSCEVType(ResTy)) 133036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines Result = InsertNoopCastOfTo(Result, SE.getEffectiveSCEVType(ResTy)); 133136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // Truncate the result. 133236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if (TruncTy != Result->getType()) { 133336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines Result = Builder.CreateTrunc(Result, TruncTy); 133436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines rememberInstruction(Result); 133536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines } 133636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // Invert the result. 133736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if (InvertStep) { 133836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines Result = Builder.CreateSub(expandCodeFor(Normalized->getStart(), TruncTy), 133936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines Result); 134036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines rememberInstruction(Result); 134136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines } 134236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines } 134336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 1344a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman // Re-apply any non-loop-dominating scale. 1345a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman if (PostLoopScale) { 13464d4bbaf997c16f9e79503bd640306d784efd090eAndrew Trick assert(S->isAffine() && "Can't linearly scale non-affine recurrences."); 13470a799ab15801d4ebf68eeb151d6375a799c87d9aDan Gohman Result = InsertNoopCastOfTo(Result, IntTy); 1348a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman Result = Builder.CreateMul(Result, 1349a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman expandCodeFor(PostLoopScale, IntTy)); 1350a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman rememberInstruction(Result); 1351a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman } 1352a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman 1353a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman // Re-apply any non-loop-dominating offset. 1354a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman if (PostLoopOffset) { 1355db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner if (PointerType *PTy = dyn_cast<PointerType>(ExpandTy)) { 1356a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman const SCEV *const OffsetArray[1] = { PostLoopOffset }; 1357a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman Result = expandAddToGEP(OffsetArray, OffsetArray+1, PTy, IntTy, Result); 1358a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman } else { 13590a799ab15801d4ebf68eeb151d6375a799c87d9aDan Gohman Result = InsertNoopCastOfTo(Result, IntTy); 1360a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman Result = Builder.CreateAdd(Result, 1361a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman expandCodeFor(PostLoopOffset, IntTy)); 1362a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman rememberInstruction(Result); 1363a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman } 1364a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman } 1365a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman 1366a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman return Result; 1367a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman} 1368a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman 1369890f92b744fb074465bc2b7006ee753a181f62a4Dan GohmanValue *SCEVExpander::visitAddRecExpr(const SCEVAddRecExpr *S) { 1370a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman if (!CanonicalMode) return expandAddRecExprLiterally(S); 1371a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman 1372db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner Type *Ty = SE.getEffectiveSCEVType(S->getType()); 137336f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman const Loop *L = S->getLoop(); 137436f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman 13754d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman // First check for an existing canonical IV in a suitable type. 1376dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines PHINode *CanonicalIV = nullptr; 13774d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman if (PHINode *PN = L->getCanonicalInductionVariable()) 1378133e295b363c89b6a3da7aba3ac5ae6332429e46Dan Gohman if (SE.getTypeSizeInBits(PN->getType()) >= SE.getTypeSizeInBits(Ty)) 13794d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman CanonicalIV = PN; 13804d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman 13814d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman // Rewrite an AddRec in terms of the canonical induction variable, if 13824d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman // its type is more narrow. 13834d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman if (CanonicalIV && 13844d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman SE.getTypeSizeInBits(CanonicalIV->getType()) > 13854d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman SE.getTypeSizeInBits(Ty)) { 1386f9e64729afec646fe93b51417e66e552e8e630a4Dan Gohman SmallVector<const SCEV *, 4> NewOps(S->getNumOperands()); 1387f9e64729afec646fe93b51417e66e552e8e630a4Dan Gohman for (unsigned i = 0, e = S->getNumOperands(); i != e; ++i) 1388f9e64729afec646fe93b51417e66e552e8e630a4Dan Gohman NewOps[i] = SE.getAnyExtendExpr(S->op_begin()[i], CanonicalIV->getType()); 13893228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick Value *V = expand(SE.getAddRecExpr(NewOps, S->getLoop(), 13906f71dd765ae9e1d1d0ba01d98a05627ffe3bfc8aAndrew Trick S->getNoWrapFlags(SCEV::FlagNW))); 13914d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman BasicBlock::iterator NewInsertPt = 139236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines std::next(BasicBlock::iterator(cast<Instruction>(V))); 1393d4278821665aa97f5fc0d19a32ff1fb39a22d395Benjamin Kramer BuilderType::InsertPointGuard Guard(Builder); 1394a4c86ab073d4b7a36477fc7c54c9d52499f04586Bill Wendling while (isa<PHINode>(NewInsertPt) || isa<DbgInfoIntrinsic>(NewInsertPt) || 1395a4c86ab073d4b7a36477fc7c54c9d52499f04586Bill Wendling isa<LandingPadInst>(NewInsertPt)) 139608f55d02da2cd24f3b4297259ef6429b8bf1ef97Jim Grosbach ++NewInsertPt; 1397dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines V = expandCodeFor(SE.getTruncateExpr(SE.getUnknown(V), Ty), nullptr, 13984d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman NewInsertPt); 13994d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman return V; 14004d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman } 14014d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman 140236f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman // {X,+,F} --> X + {0,+,F} 1403cfeb6a450632f2a6cd05302633c8c2b8c90cfdfdDan Gohman if (!S->getStart()->isZero()) { 1404f9e64729afec646fe93b51417e66e552e8e630a4Dan Gohman SmallVector<const SCEV *, 4> NewOps(S->op_begin(), S->op_end()); 1405deff621abdd48bd70434bd4d7ef30f08ddba1cd8Dan Gohman NewOps[0] = SE.getConstant(Ty, 0); 14066f71dd765ae9e1d1d0ba01d98a05627ffe3bfc8aAndrew Trick const SCEV *Rest = SE.getAddRecExpr(NewOps, L, 14076f71dd765ae9e1d1d0ba01d98a05627ffe3bfc8aAndrew Trick S->getNoWrapFlags(SCEV::FlagNW)); 1408453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman 1409453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman // Turn things like ptrtoint+arithmetic+inttoptr into GEP. See the 1410453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman // comments on expandAddToGEP for details. 1411c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman const SCEV *Base = S->getStart(); 1412c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman const SCEV *RestArray[1] = { Rest }; 1413c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman // Dig into the expression to find the pointer base for a GEP. 1414c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman ExposePointerBase(Base, RestArray[0], SE); 1415c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman // If we found a pointer, expand the AddRec with a GEP. 1416db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner if (PointerType *PTy = dyn_cast<PointerType>(Base->getType())) { 1417c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman // Make sure the Base isn't something exotic, such as a multiplied 1418c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman // or divided pointer value. In those cases, the result type isn't 1419c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman // actually a pointer type. 1420c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman if (!isa<SCEVMulExpr>(Base) && !isa<SCEVUDivExpr>(Base)) { 1421c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman Value *StartV = expand(Base); 1422c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman assert(StartV->getType() == PTy && "Pointer type mismatch for GEP!"); 1423c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman return expandAddToGEP(RestArray, RestArray+1, PTy, Ty, StartV); 1424453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman } 1425453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman } 1426453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman 142740a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman // Just do a normal add. Pre-expand the operands to suppress folding. 142840a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman return expand(SE.getAddExpr(SE.getUnknown(expand(S->getStart())), 142940a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman SE.getUnknown(expand(Rest)))); 143036f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman } 143136f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman 14326ebfd72f37325a1ebceb53e5ecad524d359d8d0bDan Gohman // If we don't yet have a canonical IV, create one. 14336ebfd72f37325a1ebceb53e5ecad524d359d8d0bDan Gohman if (!CanonicalIV) { 143436f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman // Create and insert the PHI node for the induction variable in the 143536f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman // specified loop. 143636f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman BasicBlock *Header = L->getHeader(); 1437d8b4fb4aab4d6fedb2b14bed1b846451b17bde7cJay Foad pred_iterator HPB = pred_begin(Header), HPE = pred_end(Header); 14383ecfc861b4365f341c5c969b40e1afccde676e6fJay Foad CanonicalIV = PHINode::Create(Ty, std::distance(HPB, HPE), "indvar", 14393ecfc861b4365f341c5c969b40e1afccde676e6fJay Foad Header->begin()); 14406ebfd72f37325a1ebceb53e5ecad524d359d8d0bDan Gohman rememberInstruction(CanonicalIV); 144136f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman 144219046ec19c4931f025cd4730eb43e9203ada6bb7Hal Finkel SmallSet<BasicBlock *, 4> PredSeen; 1443eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson Constant *One = ConstantInt::get(Ty, 1); 1444d8b4fb4aab4d6fedb2b14bed1b846451b17bde7cJay Foad for (pred_iterator HPI = HPB; HPI != HPE; ++HPI) { 14457656018c2268285907cfdc106071462a01a73878Gabor Greif BasicBlock *HP = *HPI; 144619046ec19c4931f025cd4730eb43e9203ada6bb7Hal Finkel if (!PredSeen.insert(HP)) 144719046ec19c4931f025cd4730eb43e9203ada6bb7Hal Finkel continue; 144819046ec19c4931f025cd4730eb43e9203ada6bb7Hal Finkel 14497656018c2268285907cfdc106071462a01a73878Gabor Greif if (L->contains(HP)) { 14503abf905b50f33340ae81913da81b0eda96fa4616Dan Gohman // Insert a unit add instruction right before the terminator 14513abf905b50f33340ae81913da81b0eda96fa4616Dan Gohman // corresponding to the back-edge. 14526ebfd72f37325a1ebceb53e5ecad524d359d8d0bDan Gohman Instruction *Add = BinaryOperator::CreateAdd(CanonicalIV, One, 14536ebfd72f37325a1ebceb53e5ecad524d359d8d0bDan Gohman "indvar.next", 14546ebfd72f37325a1ebceb53e5ecad524d359d8d0bDan Gohman HP->getTerminator()); 1455df3ad6697bb849e514c0881ca8700ea36678cbdaDevang Patel Add->setDebugLoc(HP->getTerminator()->getDebugLoc()); 1456a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman rememberInstruction(Add); 14576ebfd72f37325a1ebceb53e5ecad524d359d8d0bDan Gohman CanonicalIV->addIncoming(Add, HP); 145883d577490b1473213b6973236110c28e59127956Dan Gohman } else { 14596ebfd72f37325a1ebceb53e5ecad524d359d8d0bDan Gohman CanonicalIV->addIncoming(Constant::getNullValue(Ty), HP); 146083d577490b1473213b6973236110c28e59127956Dan Gohman } 14617656018c2268285907cfdc106071462a01a73878Gabor Greif } 146236f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman } 146336f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman 14646ebfd72f37325a1ebceb53e5ecad524d359d8d0bDan Gohman // {0,+,1} --> Insert a canonical induction variable into the loop! 14656ebfd72f37325a1ebceb53e5ecad524d359d8d0bDan Gohman if (S->isAffine() && S->getOperand(1)->isOne()) { 14666ebfd72f37325a1ebceb53e5ecad524d359d8d0bDan Gohman assert(Ty == SE.getEffectiveSCEVType(CanonicalIV->getType()) && 14676ebfd72f37325a1ebceb53e5ecad524d359d8d0bDan Gohman "IVs with types different from the canonical IV should " 14686ebfd72f37325a1ebceb53e5ecad524d359d8d0bDan Gohman "already have been handled!"); 14696ebfd72f37325a1ebceb53e5ecad524d359d8d0bDan Gohman return CanonicalIV; 14706ebfd72f37325a1ebceb53e5ecad524d359d8d0bDan Gohman } 14716ebfd72f37325a1ebceb53e5ecad524d359d8d0bDan Gohman 14724d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman // {0,+,F} --> {0,+,1} * F 147336f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman 1474df14a04b5ccbbe6a46c2ccb93e27b12a36ff163eChris Lattner // If this is a simple linear addrec, emit it now as a special case. 147540a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman if (S->isAffine()) // {0,+,F} --> i*F 147640a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman return 147740a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman expand(SE.getTruncateOrNoop( 14786ebfd72f37325a1ebceb53e5ecad524d359d8d0bDan Gohman SE.getMulExpr(SE.getUnknown(CanonicalIV), 147940a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman SE.getNoopOrAnyExtend(S->getOperand(1), 14806ebfd72f37325a1ebceb53e5ecad524d359d8d0bDan Gohman CanonicalIV->getType())), 148140a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman Ty)); 148236f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman 148336f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman // If this is a chain of recurrences, turn it into a closed form, using the 148436f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman // folders, then expandCodeFor the closed form. This allows the folders to 148536f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman // simplify the expression without having to build a bunch of special code 148636f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman // into this folder. 14876ebfd72f37325a1ebceb53e5ecad524d359d8d0bDan Gohman const SCEV *IH = SE.getUnknown(CanonicalIV); // Get I as a "symbolic" SCEV. 148836f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman 14894d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman // Promote S up to the canonical IV type, if the cast is foldable. 14900bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman const SCEV *NewS = S; 14916ebfd72f37325a1ebceb53e5ecad524d359d8d0bDan Gohman const SCEV *Ext = SE.getNoopOrAnyExtend(S, CanonicalIV->getType()); 14924d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman if (isa<SCEVAddRecExpr>(Ext)) 14934d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman NewS = Ext; 14944d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman 14950bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman const SCEV *V = cast<SCEVAddRecExpr>(NewS)->evaluateAtIteration(IH, SE); 1496e81561909d128c6e2d8033cb5465a49b2596b26aBill Wendling //cerr << "Evaluated: " << *this << "\n to: " << *V << "\n"; 149736f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman 14984d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman // Truncate the result down to the original type, if needed. 14990bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman const SCEV *T = SE.getTruncateOrNoop(V, Ty); 1500469f3cdc13851914f3a766cbd8f701cf8431cacaDan Gohman return expand(T); 150136f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman} 150296fea337d27357e9b62abbf3d2d5ce29f1c8e870Anton Korobeynikov 1503890f92b744fb074465bc2b7006ee753a181f62a4Dan GohmanValue *SCEVExpander::visitTruncateExpr(const SCEVTruncateExpr *S) { 1504db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner Type *Ty = SE.getEffectiveSCEVType(S->getType()); 150592fcdcac543653a62949fe9e5a7bd008500c1380Dan Gohman Value *V = expandCodeFor(S->getOperand(), 150692fcdcac543653a62949fe9e5a7bd008500c1380Dan Gohman SE.getEffectiveSCEVType(S->getOperand()->getType())); 1507a9390a4d5f5d568059a80970d22194b165d097a7Benjamin Kramer Value *I = Builder.CreateTrunc(V, Ty); 1508a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman rememberInstruction(I); 1509cf5ab820227dedd77fb91d0904b6dc3694a7c196Dan Gohman return I; 151011f6d3b478c4fa09d126833c57fbac1d795ead31Dan Gohman} 151111f6d3b478c4fa09d126833c57fbac1d795ead31Dan Gohman 1512890f92b744fb074465bc2b7006ee753a181f62a4Dan GohmanValue *SCEVExpander::visitZeroExtendExpr(const SCEVZeroExtendExpr *S) { 1513db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner Type *Ty = SE.getEffectiveSCEVType(S->getType()); 151492fcdcac543653a62949fe9e5a7bd008500c1380Dan Gohman Value *V = expandCodeFor(S->getOperand(), 151592fcdcac543653a62949fe9e5a7bd008500c1380Dan Gohman SE.getEffectiveSCEVType(S->getOperand()->getType())); 1516a9390a4d5f5d568059a80970d22194b165d097a7Benjamin Kramer Value *I = Builder.CreateZExt(V, Ty); 1517a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman rememberInstruction(I); 1518cf5ab820227dedd77fb91d0904b6dc3694a7c196Dan Gohman return I; 151911f6d3b478c4fa09d126833c57fbac1d795ead31Dan Gohman} 152011f6d3b478c4fa09d126833c57fbac1d795ead31Dan Gohman 1521890f92b744fb074465bc2b7006ee753a181f62a4Dan GohmanValue *SCEVExpander::visitSignExtendExpr(const SCEVSignExtendExpr *S) { 1522db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner Type *Ty = SE.getEffectiveSCEVType(S->getType()); 152392fcdcac543653a62949fe9e5a7bd008500c1380Dan Gohman Value *V = expandCodeFor(S->getOperand(), 152492fcdcac543653a62949fe9e5a7bd008500c1380Dan Gohman SE.getEffectiveSCEVType(S->getOperand()->getType())); 1525a9390a4d5f5d568059a80970d22194b165d097a7Benjamin Kramer Value *I = Builder.CreateSExt(V, Ty); 1526a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman rememberInstruction(I); 1527cf5ab820227dedd77fb91d0904b6dc3694a7c196Dan Gohman return I; 152811f6d3b478c4fa09d126833c57fbac1d795ead31Dan Gohman} 152911f6d3b478c4fa09d126833c57fbac1d795ead31Dan Gohman 1530890f92b744fb074465bc2b7006ee753a181f62a4Dan GohmanValue *SCEVExpander::visitSMaxExpr(const SCEVSMaxExpr *S) { 15310196dc5733bf3c6a71f7de1f631f2c43e5809fd9Dan Gohman Value *LHS = expand(S->getOperand(S->getNumOperands()-1)); 1532db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner Type *Ty = LHS->getType(); 15330196dc5733bf3c6a71f7de1f631f2c43e5809fd9Dan Gohman for (int i = S->getNumOperands()-2; i >= 0; --i) { 15340196dc5733bf3c6a71f7de1f631f2c43e5809fd9Dan Gohman // In the case of mixed integer and pointer types, do the 15350196dc5733bf3c6a71f7de1f631f2c43e5809fd9Dan Gohman // rest of the comparisons as integer. 15360196dc5733bf3c6a71f7de1f631f2c43e5809fd9Dan Gohman if (S->getOperand(i)->getType() != Ty) { 15370196dc5733bf3c6a71f7de1f631f2c43e5809fd9Dan Gohman Ty = SE.getEffectiveSCEVType(Ty); 15380196dc5733bf3c6a71f7de1f631f2c43e5809fd9Dan Gohman LHS = InsertNoopCastOfTo(LHS, Ty); 15390196dc5733bf3c6a71f7de1f631f2c43e5809fd9Dan Gohman } 154092fcdcac543653a62949fe9e5a7bd008500c1380Dan Gohman Value *RHS = expandCodeFor(S->getOperand(i), Ty); 1541a9390a4d5f5d568059a80970d22194b165d097a7Benjamin Kramer Value *ICmp = Builder.CreateICmpSGT(LHS, RHS); 1542a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman rememberInstruction(ICmp); 1543267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman Value *Sel = Builder.CreateSelect(ICmp, LHS, RHS, "smax"); 1544a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman rememberInstruction(Sel); 1545cf5ab820227dedd77fb91d0904b6dc3694a7c196Dan Gohman LHS = Sel; 1546c54c561c9f7270c055dd7ba75a3a003b771a42d9Nick Lewycky } 15470196dc5733bf3c6a71f7de1f631f2c43e5809fd9Dan Gohman // In the case of mixed integer and pointer types, cast the 15480196dc5733bf3c6a71f7de1f631f2c43e5809fd9Dan Gohman // final result back to the pointer type. 15490196dc5733bf3c6a71f7de1f631f2c43e5809fd9Dan Gohman if (LHS->getType() != S->getType()) 15500196dc5733bf3c6a71f7de1f631f2c43e5809fd9Dan Gohman LHS = InsertNoopCastOfTo(LHS, S->getType()); 1551c54c561c9f7270c055dd7ba75a3a003b771a42d9Nick Lewycky return LHS; 1552c54c561c9f7270c055dd7ba75a3a003b771a42d9Nick Lewycky} 1553c54c561c9f7270c055dd7ba75a3a003b771a42d9Nick Lewycky 1554890f92b744fb074465bc2b7006ee753a181f62a4Dan GohmanValue *SCEVExpander::visitUMaxExpr(const SCEVUMaxExpr *S) { 15550196dc5733bf3c6a71f7de1f631f2c43e5809fd9Dan Gohman Value *LHS = expand(S->getOperand(S->getNumOperands()-1)); 1556db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner Type *Ty = LHS->getType(); 15570196dc5733bf3c6a71f7de1f631f2c43e5809fd9Dan Gohman for (int i = S->getNumOperands()-2; i >= 0; --i) { 15580196dc5733bf3c6a71f7de1f631f2c43e5809fd9Dan Gohman // In the case of mixed integer and pointer types, do the 15590196dc5733bf3c6a71f7de1f631f2c43e5809fd9Dan Gohman // rest of the comparisons as integer. 15600196dc5733bf3c6a71f7de1f631f2c43e5809fd9Dan Gohman if (S->getOperand(i)->getType() != Ty) { 15610196dc5733bf3c6a71f7de1f631f2c43e5809fd9Dan Gohman Ty = SE.getEffectiveSCEVType(Ty); 15620196dc5733bf3c6a71f7de1f631f2c43e5809fd9Dan Gohman LHS = InsertNoopCastOfTo(LHS, Ty); 15630196dc5733bf3c6a71f7de1f631f2c43e5809fd9Dan Gohman } 156492fcdcac543653a62949fe9e5a7bd008500c1380Dan Gohman Value *RHS = expandCodeFor(S->getOperand(i), Ty); 1565a9390a4d5f5d568059a80970d22194b165d097a7Benjamin Kramer Value *ICmp = Builder.CreateICmpUGT(LHS, RHS); 1566a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman rememberInstruction(ICmp); 1567267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman Value *Sel = Builder.CreateSelect(ICmp, LHS, RHS, "umax"); 1568a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman rememberInstruction(Sel); 1569cf5ab820227dedd77fb91d0904b6dc3694a7c196Dan Gohman LHS = Sel; 15703e6307698084e7adfc10b739442ae29742beefd0Nick Lewycky } 15710196dc5733bf3c6a71f7de1f631f2c43e5809fd9Dan Gohman // In the case of mixed integer and pointer types, cast the 15720196dc5733bf3c6a71f7de1f631f2c43e5809fd9Dan Gohman // final result back to the pointer type. 15730196dc5733bf3c6a71f7de1f631f2c43e5809fd9Dan Gohman if (LHS->getType() != S->getType()) 15740196dc5733bf3c6a71f7de1f631f2c43e5809fd9Dan Gohman LHS = InsertNoopCastOfTo(LHS, S->getType()); 15753e6307698084e7adfc10b739442ae29742beefd0Nick Lewycky return LHS; 15763e6307698084e7adfc10b739442ae29742beefd0Nick Lewycky} 15773e6307698084e7adfc10b739442ae29742beefd0Nick Lewycky 1578db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris LattnerValue *SCEVExpander::expandCodeFor(const SCEV *SH, Type *Ty, 1579b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick Instruction *IP) { 15806c7ed6b54949949806797bafdf545fbfecb2cef5Dan Gohman Builder.SetInsertPoint(IP->getParent(), IP); 15816c7ed6b54949949806797bafdf545fbfecb2cef5Dan Gohman return expandCodeFor(SH, Ty); 15826c7ed6b54949949806797bafdf545fbfecb2cef5Dan Gohman} 15836c7ed6b54949949806797bafdf545fbfecb2cef5Dan Gohman 1584db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris LattnerValue *SCEVExpander::expandCodeFor(const SCEV *SH, Type *Ty) { 158511f6d3b478c4fa09d126833c57fbac1d795ead31Dan Gohman // Expand the code for this SCEV. 15862d1be87ee40a4a0241d94448173879d9df2bc5b3Dan Gohman Value *V = expand(SH); 15875be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman if (Ty) { 15885be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman assert(SE.getTypeSizeInBits(Ty) == SE.getTypeSizeInBits(SH->getType()) && 15895be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman "non-trivial casts should be done with the SCEVs directly!"); 15905be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman V = InsertNoopCastOfTo(V, Ty); 15915be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman } 15925be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman return V; 159311f6d3b478c4fa09d126833c57fbac1d795ead31Dan Gohman} 159411f6d3b478c4fa09d126833c57fbac1d795ead31Dan Gohman 1595890f92b744fb074465bc2b7006ee753a181f62a4Dan GohmanValue *SCEVExpander::expand(const SCEV *S) { 159640a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman // Compute an insertion point for this SCEV object. Hoist the instructions 159740a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman // as far out in the loop nest as possible. 1598267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman Instruction *InsertPt = Builder.GetInsertPoint(); 1599267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman for (Loop *L = SE.LI->getLoopFor(Builder.GetInsertBlock()); ; 160040a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman L = L->getParentLoop()) 160117ead4ff4baceb2c5503f233d0288d363ae44165Dan Gohman if (SE.isLoopInvariant(S, L)) { 160240a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman if (!L) break; 1603e059ee832ca36d65a5fb87b0ac5bcdb0490b15cbDan Gohman if (BasicBlock *Preheader = L->getLoopPreheader()) 160440a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman InsertPt = Preheader->getTerminator(); 16050f8cd56bfdd32af4edb253654db02fb3143b25a8Andrew Trick else { 16060f8cd56bfdd32af4edb253654db02fb3143b25a8Andrew Trick // LSR sets the insertion point for AddRec start/step values to the 16070f8cd56bfdd32af4edb253654db02fb3143b25a8Andrew Trick // block start to simplify value reuse, even though it's an invalid 16080f8cd56bfdd32af4edb253654db02fb3143b25a8Andrew Trick // position. SCEVExpander must correct for this in all cases. 16090f8cd56bfdd32af4edb253654db02fb3143b25a8Andrew Trick InsertPt = L->getHeader()->getFirstInsertionPt(); 16100f8cd56bfdd32af4edb253654db02fb3143b25a8Andrew Trick } 161140a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman } else { 161240a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman // If the SCEV is computable at this level, insert it into the header 161340a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman // after the PHIs (and after any other instructions that we've inserted 161440a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman // there) so that it is guaranteed to dominate any user inside the loop. 16155b6f42f57e730c2d968c313a27fa505a3c3e5efaBill Wendling if (L && SE.hasComputableLoopEvolution(S, L) && !PostIncLoops.count(L)) 16165b6f42f57e730c2d968c313a27fa505a3c3e5efaBill Wendling InsertPt = L->getHeader()->getFirstInsertionPt(); 1617b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick while (InsertPt != Builder.GetInsertPoint() 1618b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick && (isInsertedInstruction(InsertPt) 1619b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick || isa<DbgInfoIntrinsic>(InsertPt))) { 162036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines InsertPt = std::next(BasicBlock::iterator(InsertPt)); 1621b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick } 162240a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman break; 162340a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman } 162440a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman 1625667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman // Check to see if we already expanded this here. 16261ba5769676bb14078ddbdb9760523619726800c0Andrew Trick std::map<std::pair<const SCEV *, Instruction *>, TrackingVH<Value> >::iterator 16271ba5769676bb14078ddbdb9760523619726800c0Andrew Trick I = InsertedExpressions.find(std::make_pair(S, InsertPt)); 1628267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman if (I != InsertedExpressions.end()) 1629667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman return I->second; 1630267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman 1631d4278821665aa97f5fc0d19a32ff1fb39a22d395Benjamin Kramer BuilderType::InsertPointGuard Guard(Builder); 1632267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman Builder.SetInsertPoint(InsertPt->getParent(), InsertPt); 1633667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman 1634667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman // Expand the expression into instructions. 163596fea337d27357e9b62abbf3d2d5ce29f1c8e870Anton Korobeynikov Value *V = visit(S); 163640a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman 1637667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman // Remember the expanded value for this SCEV at this location. 163848ba0e45ed68689ce7b384578e6272410e4e23feAndrew Trick // 163948ba0e45ed68689ce7b384578e6272410e4e23feAndrew Trick // This is independent of PostIncLoops. The mapped value simply materializes 164048ba0e45ed68689ce7b384578e6272410e4e23feAndrew Trick // the expression at this insertion point. If the mapped value happened to be 164136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // a postinc expansion, it could be reused by a non-postinc user, but only if 164248ba0e45ed68689ce7b384578e6272410e4e23feAndrew Trick // its insertion point was already at the head of the loop. 164348ba0e45ed68689ce7b384578e6272410e4e23feAndrew Trick InsertedExpressions[std::make_pair(S, InsertPt)] = V; 164496fea337d27357e9b62abbf3d2d5ce29f1c8e870Anton Korobeynikov return V; 164596fea337d27357e9b62abbf3d2d5ce29f1c8e870Anton Korobeynikov} 16461d09de3eca23267855e28297fcb40de3632ea47bDan Gohman 16471d826a76f591afea445489b9a5485c345e66bf87Dan Gohmanvoid SCEVExpander::rememberInstruction(Value *I) { 164825fcaff409f5c4c6da08f148ffb9404a71e8e4a7Dan Gohman if (!PostIncLoops.empty()) 164925fcaff409f5c4c6da08f148ffb9404a71e8e4a7Dan Gohman InsertedPostIncValues.insert(I); 165025fcaff409f5c4c6da08f148ffb9404a71e8e4a7Dan Gohman else 16511d826a76f591afea445489b9a5485c345e66bf87Dan Gohman InsertedValues.insert(I); 16521d826a76f591afea445489b9a5485c345e66bf87Dan Gohman} 16531d826a76f591afea445489b9a5485c345e66bf87Dan Gohman 16541d09de3eca23267855e28297fcb40de3632ea47bDan Gohman/// getOrInsertCanonicalInductionVariable - This method returns the 16551d09de3eca23267855e28297fcb40de3632ea47bDan Gohman/// canonical induction variable of the specified type for the specified 16561d09de3eca23267855e28297fcb40de3632ea47bDan Gohman/// loop (inserting one if there is none). A canonical induction variable 16571d09de3eca23267855e28297fcb40de3632ea47bDan Gohman/// starts at zero and steps by one on each iteration. 16587c58dbd88c36c5d6c411ea6c046ddcff4c5841e9Dan GohmanPHINode * 16591d09de3eca23267855e28297fcb40de3632ea47bDan GohmanSCEVExpander::getOrInsertCanonicalInductionVariable(const Loop *L, 1660db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner Type *Ty) { 1661b0bc6c361da9009e8414efde317d9bbff755f6c0Duncan Sands assert(Ty->isIntegerTy() && "Can only insert integer induction variables!"); 1662133e295b363c89b6a3da7aba3ac5ae6332429e46Dan Gohman 1663133e295b363c89b6a3da7aba3ac5ae6332429e46Dan Gohman // Build a SCEV for {0,+,1}<L>. 16643228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick // Conservatively use FlagAnyWrap for now. 1665deff621abdd48bd70434bd4d7ef30f08ddba1cd8Dan Gohman const SCEV *H = SE.getAddRecExpr(SE.getConstant(Ty, 0), 16663228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick SE.getConstant(Ty, 1), L, SCEV::FlagAnyWrap); 1667133e295b363c89b6a3da7aba3ac5ae6332429e46Dan Gohman 1668133e295b363c89b6a3da7aba3ac5ae6332429e46Dan Gohman // Emit code for it. 1669d4278821665aa97f5fc0d19a32ff1fb39a22d395Benjamin Kramer BuilderType::InsertPointGuard Guard(Builder); 1670dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines PHINode *V = cast<PHINode>(expandCodeFor(H, nullptr, 1671dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines L->getHeader()->begin())); 1672133e295b363c89b6a3da7aba3ac5ae6332429e46Dan Gohman 167340a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman return V; 16741d09de3eca23267855e28297fcb40de3632ea47bDan Gohman} 1675204494149b6f846e8f173f525b129f5508076049Andrew Trick 1676204494149b6f846e8f173f525b129f5508076049Andrew Trick/// replaceCongruentIVs - Check for congruent phis in this loop header and 1677204494149b6f846e8f173f525b129f5508076049Andrew Trick/// replace them with their most canonical representative. Return the number of 1678204494149b6f846e8f173f525b129f5508076049Andrew Trick/// phis eliminated. 1679204494149b6f846e8f173f525b129f5508076049Andrew Trick/// 1680204494149b6f846e8f173f525b129f5508076049Andrew Trick/// This does not depend on any SCEVExpander state but should be used in 1681204494149b6f846e8f173f525b129f5508076049Andrew Trick/// the same context that SCEVExpander is used. 1682204494149b6f846e8f173f525b129f5508076049Andrew Trickunsigned SCEVExpander::replaceCongruentIVs(Loop *L, const DominatorTree *DT, 1683a04a4a79ea365d1ba96ed4b5861e879b267162e2Nadav Rotem SmallVectorImpl<WeakVH> &DeadInsts, 1684e4ba75f43e2ab1480d119d2d4eb878256274e0fbChandler Carruth const TargetTransformInfo *TTI) { 1685ee98aa87434d9d49a8e4dab41d873888ac9c4805Andrew Trick // Find integer phis in order of increasing width. 1686ee98aa87434d9d49a8e4dab41d873888ac9c4805Andrew Trick SmallVector<PHINode*, 8> Phis; 1687ee98aa87434d9d49a8e4dab41d873888ac9c4805Andrew Trick for (BasicBlock::iterator I = L->getHeader()->begin(); 1688ee98aa87434d9d49a8e4dab41d873888ac9c4805Andrew Trick PHINode *Phi = dyn_cast<PHINode>(I); ++I) { 1689ee98aa87434d9d49a8e4dab41d873888ac9c4805Andrew Trick Phis.push_back(Phi); 1690ee98aa87434d9d49a8e4dab41d873888ac9c4805Andrew Trick } 1691e4ba75f43e2ab1480d119d2d4eb878256274e0fbChandler Carruth if (TTI) 169236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines std::sort(Phis.begin(), Phis.end(), [](Value *LHS, Value *RHS) { 169336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // Put pointers at the back and make sure pointer < pointer = false. 169436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy()) 169536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines return RHS->getType()->isIntegerTy() && !LHS->getType()->isIntegerTy(); 169636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines return RHS->getType()->getPrimitiveSizeInBits() < 169736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines LHS->getType()->getPrimitiveSizeInBits(); 169836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines }); 1699ee98aa87434d9d49a8e4dab41d873888ac9c4805Andrew Trick 1700204494149b6f846e8f173f525b129f5508076049Andrew Trick unsigned NumElim = 0; 1701204494149b6f846e8f173f525b129f5508076049Andrew Trick DenseMap<const SCEV *, PHINode *> ExprToIVMap; 1702ee98aa87434d9d49a8e4dab41d873888ac9c4805Andrew Trick // Process phis from wide to narrow. Mapping wide phis to the their truncation 1703ee98aa87434d9d49a8e4dab41d873888ac9c4805Andrew Trick // so narrow phis can reuse them. 1704ee98aa87434d9d49a8e4dab41d873888ac9c4805Andrew Trick for (SmallVectorImpl<PHINode*>::const_iterator PIter = Phis.begin(), 1705ee98aa87434d9d49a8e4dab41d873888ac9c4805Andrew Trick PEnd = Phis.end(); PIter != PEnd; ++PIter) { 1706ee98aa87434d9d49a8e4dab41d873888ac9c4805Andrew Trick PHINode *Phi = *PIter; 1707ee98aa87434d9d49a8e4dab41d873888ac9c4805Andrew Trick 1708239fd44f7a68aae4b2e8b6f1738ef9e8fd4ddc01Benjamin Kramer // Fold constant phis. They may be congruent to other constant phis and 1709239fd44f7a68aae4b2e8b6f1738ef9e8fd4ddc01Benjamin Kramer // would confuse the logic below that expects proper IVs. 1710cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines if (Value *V = SimplifyInstruction(Phi, SE.DL, SE.TLI, SE.DT)) { 1711239fd44f7a68aae4b2e8b6f1738ef9e8fd4ddc01Benjamin Kramer Phi->replaceAllUsesWith(V); 1712239fd44f7a68aae4b2e8b6f1738ef9e8fd4ddc01Benjamin Kramer DeadInsts.push_back(Phi); 1713239fd44f7a68aae4b2e8b6f1738ef9e8fd4ddc01Benjamin Kramer ++NumElim; 1714239fd44f7a68aae4b2e8b6f1738ef9e8fd4ddc01Benjamin Kramer DEBUG_WITH_TYPE(DebugType, dbgs() 1715239fd44f7a68aae4b2e8b6f1738ef9e8fd4ddc01Benjamin Kramer << "INDVARS: Eliminated constant iv: " << *Phi << '\n'); 1716239fd44f7a68aae4b2e8b6f1738ef9e8fd4ddc01Benjamin Kramer continue; 1717239fd44f7a68aae4b2e8b6f1738ef9e8fd4ddc01Benjamin Kramer } 1718239fd44f7a68aae4b2e8b6f1738ef9e8fd4ddc01Benjamin Kramer 1719204494149b6f846e8f173f525b129f5508076049Andrew Trick if (!SE.isSCEVable(Phi->getType())) 1720204494149b6f846e8f173f525b129f5508076049Andrew Trick continue; 1721204494149b6f846e8f173f525b129f5508076049Andrew Trick 1722204494149b6f846e8f173f525b129f5508076049Andrew Trick PHINode *&OrigPhiRef = ExprToIVMap[SE.getSCEV(Phi)]; 1723204494149b6f846e8f173f525b129f5508076049Andrew Trick if (!OrigPhiRef) { 1724204494149b6f846e8f173f525b129f5508076049Andrew Trick OrigPhiRef = Phi; 1725e4ba75f43e2ab1480d119d2d4eb878256274e0fbChandler Carruth if (Phi->getType()->isIntegerTy() && TTI 1726e4ba75f43e2ab1480d119d2d4eb878256274e0fbChandler Carruth && TTI->isTruncateFree(Phi->getType(), Phis.back()->getType())) { 1727ee98aa87434d9d49a8e4dab41d873888ac9c4805Andrew Trick // This phi can be freely truncated to the narrowest phi type. Map the 1728ee98aa87434d9d49a8e4dab41d873888ac9c4805Andrew Trick // truncated expression to it so it will be reused for narrow types. 1729ee98aa87434d9d49a8e4dab41d873888ac9c4805Andrew Trick const SCEV *TruncExpr = 1730ee98aa87434d9d49a8e4dab41d873888ac9c4805Andrew Trick SE.getTruncateExpr(SE.getSCEV(Phi), Phis.back()->getType()); 1731ee98aa87434d9d49a8e4dab41d873888ac9c4805Andrew Trick ExprToIVMap[TruncExpr] = Phi; 1732ee98aa87434d9d49a8e4dab41d873888ac9c4805Andrew Trick } 1733204494149b6f846e8f173f525b129f5508076049Andrew Trick continue; 1734204494149b6f846e8f173f525b129f5508076049Andrew Trick } 1735204494149b6f846e8f173f525b129f5508076049Andrew Trick 1736ee98aa87434d9d49a8e4dab41d873888ac9c4805Andrew Trick // Replacing a pointer phi with an integer phi or vice-versa doesn't make 1737ee98aa87434d9d49a8e4dab41d873888ac9c4805Andrew Trick // sense. 1738ee98aa87434d9d49a8e4dab41d873888ac9c4805Andrew Trick if (OrigPhiRef->getType()->isPointerTy() != Phi->getType()->isPointerTy()) 1739204494149b6f846e8f173f525b129f5508076049Andrew Trick continue; 1740204494149b6f846e8f173f525b129f5508076049Andrew Trick 1741204494149b6f846e8f173f525b129f5508076049Andrew Trick if (BasicBlock *LatchBlock = L->getLoopLatch()) { 1742204494149b6f846e8f173f525b129f5508076049Andrew Trick Instruction *OrigInc = 1743204494149b6f846e8f173f525b129f5508076049Andrew Trick cast<Instruction>(OrigPhiRef->getIncomingValueForBlock(LatchBlock)); 1744204494149b6f846e8f173f525b129f5508076049Andrew Trick Instruction *IsomorphicInc = 1745204494149b6f846e8f173f525b129f5508076049Andrew Trick cast<Instruction>(Phi->getIncomingValueForBlock(LatchBlock)); 1746204494149b6f846e8f173f525b129f5508076049Andrew Trick 1747ee98aa87434d9d49a8e4dab41d873888ac9c4805Andrew Trick // If this phi has the same width but is more canonical, replace the 1748b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick // original with it. As part of the "more canonical" determination, 1749b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick // respect a prior decision to use an IV chain. 1750ee98aa87434d9d49a8e4dab41d873888ac9c4805Andrew Trick if (OrigPhiRef->getType() == Phi->getType() 1751b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick && !(ChainedPhis.count(Phi) 1752b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick || isExpandedAddRecExprPHI(OrigPhiRef, OrigInc, L)) 1753b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick && (ChainedPhis.count(Phi) 1754b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick || isExpandedAddRecExprPHI(Phi, IsomorphicInc, L))) { 1755204494149b6f846e8f173f525b129f5508076049Andrew Trick std::swap(OrigPhiRef, Phi); 1756204494149b6f846e8f173f525b129f5508076049Andrew Trick std::swap(OrigInc, IsomorphicInc); 1757204494149b6f846e8f173f525b129f5508076049Andrew Trick } 1758204494149b6f846e8f173f525b129f5508076049Andrew Trick // Replacing the congruent phi is sufficient because acyclic redundancy 1759204494149b6f846e8f173f525b129f5508076049Andrew Trick // elimination, CSE/GVN, should handle the rest. However, once SCEV proves 1760204494149b6f846e8f173f525b129f5508076049Andrew Trick // that a phi is congruent, it's often the head of an IV user cycle that 1761139f333f88fb9e0c39dfa3b56fd2a26b33837b70Andrew Trick // is isomorphic with the original phi. It's worth eagerly cleaning up the 1762139f333f88fb9e0c39dfa3b56fd2a26b33837b70Andrew Trick // common case of a single IV increment so that DeleteDeadPHIs can remove 1763139f333f88fb9e0c39dfa3b56fd2a26b33837b70Andrew Trick // cycles that had postinc uses. 1764ee98aa87434d9d49a8e4dab41d873888ac9c4805Andrew Trick const SCEV *TruncExpr = SE.getTruncateOrNoop(SE.getSCEV(OrigInc), 1765ee98aa87434d9d49a8e4dab41d873888ac9c4805Andrew Trick IsomorphicInc->getType()); 1766ee98aa87434d9d49a8e4dab41d873888ac9c4805Andrew Trick if (OrigInc != IsomorphicInc 176764925c55c65f9345a69fb67db07aa62cfb723577Andrew Trick && TruncExpr == SE.getSCEV(IsomorphicInc) 1768b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick && ((isa<PHINode>(OrigInc) && isa<PHINode>(IsomorphicInc)) 1769b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick || hoistIVInc(OrigInc, IsomorphicInc))) { 1770204494149b6f846e8f173f525b129f5508076049Andrew Trick DEBUG_WITH_TYPE(DebugType, dbgs() 1771204494149b6f846e8f173f525b129f5508076049Andrew Trick << "INDVARS: Eliminated congruent iv.inc: " 1772204494149b6f846e8f173f525b129f5508076049Andrew Trick << *IsomorphicInc << '\n'); 1773ee98aa87434d9d49a8e4dab41d873888ac9c4805Andrew Trick Value *NewInc = OrigInc; 1774ee98aa87434d9d49a8e4dab41d873888ac9c4805Andrew Trick if (OrigInc->getType() != IsomorphicInc->getType()) { 1775dd1f22f25d8496b10cfddedb63d674dd39897bb0Andrew Trick Instruction *IP = isa<PHINode>(OrigInc) 1776dd1f22f25d8496b10cfddedb63d674dd39897bb0Andrew Trick ? (Instruction*)L->getHeader()->getFirstInsertionPt() 1777dd1f22f25d8496b10cfddedb63d674dd39897bb0Andrew Trick : OrigInc->getNextNode(); 1778dd1f22f25d8496b10cfddedb63d674dd39897bb0Andrew Trick IRBuilder<> Builder(IP); 1779ee98aa87434d9d49a8e4dab41d873888ac9c4805Andrew Trick Builder.SetCurrentDebugLocation(IsomorphicInc->getDebugLoc()); 1780ee98aa87434d9d49a8e4dab41d873888ac9c4805Andrew Trick NewInc = Builder. 1781ee98aa87434d9d49a8e4dab41d873888ac9c4805Andrew Trick CreateTruncOrBitCast(OrigInc, IsomorphicInc->getType(), IVName); 1782ee98aa87434d9d49a8e4dab41d873888ac9c4805Andrew Trick } 1783ee98aa87434d9d49a8e4dab41d873888ac9c4805Andrew Trick IsomorphicInc->replaceAllUsesWith(NewInc); 1784204494149b6f846e8f173f525b129f5508076049Andrew Trick DeadInsts.push_back(IsomorphicInc); 1785204494149b6f846e8f173f525b129f5508076049Andrew Trick } 1786204494149b6f846e8f173f525b129f5508076049Andrew Trick } 1787204494149b6f846e8f173f525b129f5508076049Andrew Trick DEBUG_WITH_TYPE(DebugType, dbgs() 1788204494149b6f846e8f173f525b129f5508076049Andrew Trick << "INDVARS: Eliminated congruent iv: " << *Phi << '\n'); 1789204494149b6f846e8f173f525b129f5508076049Andrew Trick ++NumElim; 1790ee98aa87434d9d49a8e4dab41d873888ac9c4805Andrew Trick Value *NewIV = OrigPhiRef; 1791ee98aa87434d9d49a8e4dab41d873888ac9c4805Andrew Trick if (OrigPhiRef->getType() != Phi->getType()) { 1792ee98aa87434d9d49a8e4dab41d873888ac9c4805Andrew Trick IRBuilder<> Builder(L->getHeader()->getFirstInsertionPt()); 1793ee98aa87434d9d49a8e4dab41d873888ac9c4805Andrew Trick Builder.SetCurrentDebugLocation(Phi->getDebugLoc()); 1794ee98aa87434d9d49a8e4dab41d873888ac9c4805Andrew Trick NewIV = Builder.CreateTruncOrBitCast(OrigPhiRef, Phi->getType(), IVName); 1795ee98aa87434d9d49a8e4dab41d873888ac9c4805Andrew Trick } 1796ee98aa87434d9d49a8e4dab41d873888ac9c4805Andrew Trick Phi->replaceAllUsesWith(NewIV); 1797204494149b6f846e8f173f525b129f5508076049Andrew Trick DeadInsts.push_back(Phi); 1798204494149b6f846e8f173f525b129f5508076049Andrew Trick } 1799204494149b6f846e8f173f525b129f5508076049Andrew Trick return NumElim; 1800204494149b6f846e8f173f525b129f5508076049Andrew Trick} 1801e08c32249fca32cd7b122024a4ca252fcb235694Andrew Trick 1802e08c32249fca32cd7b122024a4ca252fcb235694Andrew Tricknamespace { 1803e08c32249fca32cd7b122024a4ca252fcb235694Andrew Trick// Search for a SCEV subexpression that is not safe to expand. Any expression 1804e08c32249fca32cd7b122024a4ca252fcb235694Andrew Trick// that may expand to a !isSafeToSpeculativelyExecute value is unsafe, namely 1805e08c32249fca32cd7b122024a4ca252fcb235694Andrew Trick// UDiv expressions. We don't know if the UDiv is derived from an IR divide 1806e08c32249fca32cd7b122024a4ca252fcb235694Andrew Trick// instruction, but the important thing is that we prove the denominator is 1807e08c32249fca32cd7b122024a4ca252fcb235694Andrew Trick// nonzero before expansion. 1808e08c32249fca32cd7b122024a4ca252fcb235694Andrew Trick// 1809e08c32249fca32cd7b122024a4ca252fcb235694Andrew Trick// IVUsers already checks that IV-derived expressions are safe. So this check is 1810e08c32249fca32cd7b122024a4ca252fcb235694Andrew Trick// only needed when the expression includes some subexpression that is not IV 1811e08c32249fca32cd7b122024a4ca252fcb235694Andrew Trick// derived. 1812e08c32249fca32cd7b122024a4ca252fcb235694Andrew Trick// 1813e08c32249fca32cd7b122024a4ca252fcb235694Andrew Trick// Currently, we only allow division by a nonzero constant here. If this is 1814e08c32249fca32cd7b122024a4ca252fcb235694Andrew Trick// inadequate, we could easily allow division by SCEVUnknown by using 1815e08c32249fca32cd7b122024a4ca252fcb235694Andrew Trick// ValueTracking to check isKnownNonZero(). 18164d4bbaf997c16f9e79503bd640306d784efd090eAndrew Trick// 18174d4bbaf997c16f9e79503bd640306d784efd090eAndrew Trick// We cannot generally expand recurrences unless the step dominates the loop 18184d4bbaf997c16f9e79503bd640306d784efd090eAndrew Trick// header. The expander handles the special case of affine recurrences by 18194d4bbaf997c16f9e79503bd640306d784efd090eAndrew Trick// scaling the recurrence outside the loop, but this technique isn't generally 18204d4bbaf997c16f9e79503bd640306d784efd090eAndrew Trick// applicable. Expanding a nested recurrence outside a loop requires computing 18214d4bbaf997c16f9e79503bd640306d784efd090eAndrew Trick// binomial coefficients. This could be done, but the recurrence has to be in a 18224d4bbaf997c16f9e79503bd640306d784efd090eAndrew Trick// perfectly reduced form, which can't be guaranteed. 1823e08c32249fca32cd7b122024a4ca252fcb235694Andrew Trickstruct SCEVFindUnsafe { 18244d4bbaf997c16f9e79503bd640306d784efd090eAndrew Trick ScalarEvolution &SE; 1825e08c32249fca32cd7b122024a4ca252fcb235694Andrew Trick bool IsUnsafe; 1826e08c32249fca32cd7b122024a4ca252fcb235694Andrew Trick 18274d4bbaf997c16f9e79503bd640306d784efd090eAndrew Trick SCEVFindUnsafe(ScalarEvolution &se): SE(se), IsUnsafe(false) {} 1828e08c32249fca32cd7b122024a4ca252fcb235694Andrew Trick 1829e08c32249fca32cd7b122024a4ca252fcb235694Andrew Trick bool follow(const SCEV *S) { 18304d4bbaf997c16f9e79503bd640306d784efd090eAndrew Trick if (const SCEVUDivExpr *D = dyn_cast<SCEVUDivExpr>(S)) { 18314d4bbaf997c16f9e79503bd640306d784efd090eAndrew Trick const SCEVConstant *SC = dyn_cast<SCEVConstant>(D->getRHS()); 18324d4bbaf997c16f9e79503bd640306d784efd090eAndrew Trick if (!SC || SC->getValue()->isZero()) { 18334d4bbaf997c16f9e79503bd640306d784efd090eAndrew Trick IsUnsafe = true; 18344d4bbaf997c16f9e79503bd640306d784efd090eAndrew Trick return false; 18354d4bbaf997c16f9e79503bd640306d784efd090eAndrew Trick } 18364d4bbaf997c16f9e79503bd640306d784efd090eAndrew Trick } 18374d4bbaf997c16f9e79503bd640306d784efd090eAndrew Trick if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) { 18384d4bbaf997c16f9e79503bd640306d784efd090eAndrew Trick const SCEV *Step = AR->getStepRecurrence(SE); 18394d4bbaf997c16f9e79503bd640306d784efd090eAndrew Trick if (!AR->isAffine() && !SE.dominates(Step, AR->getLoop()->getHeader())) { 18404d4bbaf997c16f9e79503bd640306d784efd090eAndrew Trick IsUnsafe = true; 18414d4bbaf997c16f9e79503bd640306d784efd090eAndrew Trick return false; 18424d4bbaf997c16f9e79503bd640306d784efd090eAndrew Trick } 18434d4bbaf997c16f9e79503bd640306d784efd090eAndrew Trick } 18444d4bbaf997c16f9e79503bd640306d784efd090eAndrew Trick return true; 1845e08c32249fca32cd7b122024a4ca252fcb235694Andrew Trick } 1846e08c32249fca32cd7b122024a4ca252fcb235694Andrew Trick bool isDone() const { return IsUnsafe; } 1847e08c32249fca32cd7b122024a4ca252fcb235694Andrew Trick}; 1848e08c32249fca32cd7b122024a4ca252fcb235694Andrew Trick} 1849e08c32249fca32cd7b122024a4ca252fcb235694Andrew Trick 1850e08c32249fca32cd7b122024a4ca252fcb235694Andrew Tricknamespace llvm { 18514d4bbaf997c16f9e79503bd640306d784efd090eAndrew Trickbool isSafeToExpand(const SCEV *S, ScalarEvolution &SE) { 18524d4bbaf997c16f9e79503bd640306d784efd090eAndrew Trick SCEVFindUnsafe Search(SE); 1853e08c32249fca32cd7b122024a4ca252fcb235694Andrew Trick visitAll(S, Search); 1854e08c32249fca32cd7b122024a4ca252fcb235694Andrew Trick return !Search.IsUnsafe; 1855e08c32249fca32cd7b122024a4ca252fcb235694Andrew Trick} 1856e08c32249fca32cd7b122024a4ca252fcb235694Andrew Trick} 1857