LoopStrengthReduce.cpp revision 441a38993e89104c424e00c95cce63c7351f4fc3
12d1be87ee40a4a0241d94448173879d9df2bc5b3Dan Gohman//===- LoopStrengthReduce.cpp - Strength Reduce IVs in Loops --------------===// 2fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman// 3eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman// The LLVM Compiler Infrastructure 4eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman// 54ee451de366474b9c228b4e5fa573795a715216dChris Lattner// This file is distributed under the University of Illinois Open Source 64ee451de366474b9c228b4e5fa573795a715216dChris Lattner// License. See LICENSE.TXT for details. 7fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman// 8eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman//===----------------------------------------------------------------------===// 9eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman// 10cec8f9da54d3017e27c46ced38b859136655c198Dan Gohman// This transformation analyzes and transforms the induction variables (and 11cec8f9da54d3017e27c46ced38b859136655c198Dan Gohman// computations derived from them) into forms suitable for efficient execution 12cec8f9da54d3017e27c46ced38b859136655c198Dan Gohman// on the target. 13cec8f9da54d3017e27c46ced38b859136655c198Dan Gohman// 14eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman// This pass performs a strength reduction on array references inside loops that 15cec8f9da54d3017e27c46ced38b859136655c198Dan Gohman// have as one or more of their components the loop induction variable, it 16cec8f9da54d3017e27c46ced38b859136655c198Dan Gohman// rewrites expressions to take advantage of scaled-index addressing modes 17cec8f9da54d3017e27c46ced38b859136655c198Dan Gohman// available on the target, and it performs a variety of other optimizations 18cec8f9da54d3017e27c46ced38b859136655c198Dan Gohman// related to loop induction variables. 19eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman// 20572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman// Terminology note: this code has a lot of handling for "post-increment" or 21572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman// "post-inc" users. This is not talking about post-increment addressing modes; 22572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman// it is instead talking about code like this: 23572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman// 24572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman// %i = phi [ 0, %entry ], [ %i.next, %latch ] 25572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman// ... 26572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman// %i.next = add %i, 1 27572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman// %c = icmp eq %i.next, %n 28572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman// 29572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman// The SCEV for %i is {0,+,1}<%L>. The SCEV for %i.next is {1,+,1}<%L>, however 30572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman// it's useful to think about these as the same register, with some uses using 31572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman// the value of the register before the add and some using // it after. In this 32572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman// example, the icmp is a post-increment user, since it uses %i.next, which is 33572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman// the value of the induction variable after the increment. The other common 34572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman// case of post-increment users is users outside the loop. 35572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman// 36572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman// TODO: More sophistication in the way Formulae are generated and filtered. 37572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman// 38572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman// TODO: Handle multiple loops at a time. 39572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman// 40572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman// TODO: Should TargetLowering::AddrMode::BaseGV be changed to a ConstantExpr 41572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman// instead of a GlobalValue? 42572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman// 43572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman// TODO: When truncation is free, truncate ICmp users' operands to make it a 44572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman// smaller encoding (on x86 at least). 45572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman// 46572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman// TODO: When a negated register is used by an add (such as in a list of 47572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman// multiple base registers, or as the increment expression in an addrec), 48572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman// we may not actually need both reg and (-1 * reg) in registers; the 49572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman// negation can be implemented by using a sub instead of an add. The 50572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman// lack of support for taking this into consideration when making 51572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman// register pressure decisions is partly worked around by the "Special" 52572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman// use kind. 53572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman// 54eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman//===----------------------------------------------------------------------===// 55eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman 56be3e5212e23edc9210f774fac18d801de252e906Chris Lattner#define DEBUG_TYPE "loop-reduce" 57eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman#include "llvm/Transforms/Scalar.h" 58eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman#include "llvm/Constants.h" 59eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman#include "llvm/Instructions.h" 60e5b01bea7b9b7dce7c24484d2d915b0c118d4d07Dan Gohman#include "llvm/IntrinsicInst.h" 612f3c9b7562bcdc1795b2bd0ca28b283a8e972826Jeff Cohen#include "llvm/DerivedTypes.h" 6281db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman#include "llvm/Analysis/IVUsers.h" 63572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman#include "llvm/Analysis/Dominators.h" 640f54dcbf07c69e41ecaa6b4fbf0d94956d8e9ff5Devang Patel#include "llvm/Analysis/LoopPass.h" 65169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman#include "llvm/Analysis/ScalarEvolutionExpander.h" 66e0391beda88c6c441ce1aadbe223d6c0784061a2Chris Lattner#include "llvm/Transforms/Utils/BasicBlockUtils.h" 67eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman#include "llvm/Transforms/Utils/Local.h" 68572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman#include "llvm/ADT/SmallBitVector.h" 69572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman#include "llvm/ADT/SetVector.h" 70572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman#include "llvm/ADT/DenseSet.h" 71169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman#include "llvm/Support/Debug.h" 72afc36a9520971832dfbebc0333593bf5d3098296Dan Gohman#include "llvm/Support/ValueHandle.h" 73460f656475738d1a95a6be95346908ce1597df25Daniel Dunbar#include "llvm/Support/raw_ostream.h" 74d277f2c66914aecb619c12855f6afae4c7ef883bEvan Cheng#include "llvm/Target/TargetLowering.h" 75cfb1d4235fe3291028341e6acf4139723b4749e3Jeff Cohen#include <algorithm> 76eaa13851a7fe604363577350c5cf65c257c4d41aNate Begemanusing namespace llvm; 77eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman 78572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmannamespace { 79572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 80572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// RegSortData - This class holds data which is used to order reuse candidates. 81572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanclass RegSortData { 82572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanpublic: 83572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman /// UsedByIndices - This represents the set of LSRUse indices which reference 84572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman /// a particular register. 85572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallBitVector UsedByIndices; 86572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 87572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman RegSortData() {} 88572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 89572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman void print(raw_ostream &OS) const; 90572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman void dump() const; 91572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman}; 92572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 93572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 94572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 95572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanvoid RegSortData::print(raw_ostream &OS) const { 96572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman OS << "[NumUses=" << UsedByIndices.count() << ']'; 97572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 98dc42f48ea90132509e678028e7dbab5544ef0794Dale Johannesen 99572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanvoid RegSortData::dump() const { 100572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman print(errs()); errs() << '\n'; 101572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 102dc42f48ea90132509e678028e7dbab5544ef0794Dale Johannesen 1037979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohmannamespace { 1047979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman 105572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// RegUseTracker - Map register candidates to information about how they are 106572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// used. 107572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanclass RegUseTracker { 108572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman typedef DenseMap<const SCEV *, RegSortData> RegUsesTy; 109d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng 110572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman RegUsesTy RegUses; 111572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallVector<const SCEV *, 16> RegSequence; 112d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng 113572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanpublic: 114572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman void CountRegister(const SCEV *Reg, size_t LUIdx); 115572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 116572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman bool isRegUsedByUsesOtherThan(const SCEV *Reg, size_t LUIdx) const; 117572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 118572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const SmallBitVector &getUsedByIndices(const SCEV *Reg) const; 119572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 120572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman void clear(); 121572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 122572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman typedef SmallVectorImpl<const SCEV *>::iterator iterator; 123572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman typedef SmallVectorImpl<const SCEV *>::const_iterator const_iterator; 124572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman iterator begin() { return RegSequence.begin(); } 125572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman iterator end() { return RegSequence.end(); } 126572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const_iterator begin() const { return RegSequence.begin(); } 127572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const_iterator end() const { return RegSequence.end(); } 128572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman}; 129572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 130572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 131572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 132572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanvoid 133572645cf84060c0fc25cb91d38cb9079918b3a88Dan GohmanRegUseTracker::CountRegister(const SCEV *Reg, size_t LUIdx) { 134572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman std::pair<RegUsesTy::iterator, bool> Pair = 135572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman RegUses.insert(std::make_pair(Reg, RegSortData())); 136572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman RegSortData &RSD = Pair.first->second; 137572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (Pair.second) 138572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman RegSequence.push_back(Reg); 139572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman RSD.UsedByIndices.resize(std::max(RSD.UsedByIndices.size(), LUIdx + 1)); 140572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman RSD.UsedByIndices.set(LUIdx); 141572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 142572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 143572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanbool 144572645cf84060c0fc25cb91d38cb9079918b3a88Dan GohmanRegUseTracker::isRegUsedByUsesOtherThan(const SCEV *Reg, size_t LUIdx) const { 145572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!RegUses.count(Reg)) return false; 146572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const SmallBitVector &UsedByIndices = 147572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman RegUses.find(Reg)->second.UsedByIndices; 148572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman int i = UsedByIndices.find_first(); 149572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (i == -1) return false; 150572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if ((size_t)i != LUIdx) return true; 151572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return UsedByIndices.find_next(i) != -1; 152572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 153572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 154572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanconst SmallBitVector &RegUseTracker::getUsedByIndices(const SCEV *Reg) const { 155572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman RegUsesTy::const_iterator I = RegUses.find(Reg); 156572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman assert(I != RegUses.end() && "Unknown register!"); 157572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return I->second.UsedByIndices; 158572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 159572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 160572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanvoid RegUseTracker::clear() { 161572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman RegUses.clear(); 162572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman RegSequence.clear(); 163572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 164572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 165572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmannamespace { 166572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 167572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// Formula - This class holds information that describes a formula for 168572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// computing satisfying a use. It may include broken-out immediates and scaled 169572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// registers. 170572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanstruct Formula { 171572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman /// AM - This is used to represent complex addressing, as well as other kinds 172572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman /// of interesting uses. 173572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman TargetLowering::AddrMode AM; 174572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 175572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman /// BaseRegs - The list of "base" registers for this use. When this is 176572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman /// non-empty, AM.HasBaseReg should be set to true. 177572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallVector<const SCEV *, 2> BaseRegs; 178572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 179572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman /// ScaledReg - The 'scaled' register for this use. This should be non-null 180572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman /// when AM.Scale is not zero. 181572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const SCEV *ScaledReg; 182572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 183572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Formula() : ScaledReg(0) {} 184572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 185572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman void InitialMatch(const SCEV *S, Loop *L, 186572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman ScalarEvolution &SE, DominatorTree &DT); 187572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 188572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman unsigned getNumRegs() const; 189572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const Type *getType() const; 190572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 191572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman bool referencesReg(const SCEV *S) const; 192572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman bool hasRegsUsedByUsesOtherThan(size_t LUIdx, 193572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const RegUseTracker &RegUses) const; 194572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 195572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman void print(raw_ostream &OS) const; 196572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman void dump() const; 197572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman}; 198572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 199572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 200572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 201572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// DoInitialMatch - Recurrsion helper for InitialMatch. 202572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanstatic void DoInitialMatch(const SCEV *S, Loop *L, 203572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallVectorImpl<const SCEV *> &Good, 204572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallVectorImpl<const SCEV *> &Bad, 205572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman ScalarEvolution &SE, DominatorTree &DT) { 206572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Collect expressions which properly dominate the loop header. 207572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (S->properlyDominates(L->getHeader(), &DT)) { 208572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Good.push_back(S); 209572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return; 210572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 211d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng 212572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Look at add operands. 213572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) { 214572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (SCEVAddExpr::op_iterator I = Add->op_begin(), E = Add->op_end(); 215572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman I != E; ++I) 216572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman DoInitialMatch(*I, L, Good, Bad, SE, DT); 217572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return; 218572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 219169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman 220572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Look at addrec operands. 221572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) 222572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!AR->getStart()->isZero()) { 223572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman DoInitialMatch(AR->getStart(), L, Good, Bad, SE, DT); 224572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman DoInitialMatch(SE.getAddRecExpr(SE.getIntegerSCEV(0, AR->getType()), 225572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman AR->getStepRecurrence(SE), 226572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman AR->getLoop()), 227572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman L, Good, Bad, SE, DT); 228572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return; 2297979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman } 2302d1be87ee40a4a0241d94448173879d9df2bc5b3Dan Gohman 231572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Handle a multiplication by -1 (negation) if it didn't fold. 232572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S)) 233572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (Mul->getOperand(0)->isAllOnesValue()) { 234572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallVector<const SCEV *, 4> Ops(Mul->op_begin()+1, Mul->op_end()); 235572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const SCEV *NewMul = SE.getMulExpr(Ops); 236572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 237572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallVector<const SCEV *, 4> MyGood; 238572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallVector<const SCEV *, 4> MyBad; 239572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman DoInitialMatch(NewMul, L, MyGood, MyBad, SE, DT); 240572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const SCEV *NegOne = SE.getSCEV(ConstantInt::getAllOnesValue( 241572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SE.getEffectiveSCEVType(NewMul->getType()))); 242572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (SmallVectorImpl<const SCEV *>::const_iterator I = MyGood.begin(), 243572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman E = MyGood.end(); I != E; ++I) 244572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Good.push_back(SE.getMulExpr(NegOne, *I)); 245572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (SmallVectorImpl<const SCEV *>::const_iterator I = MyBad.begin(), 246572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman E = MyBad.end(); I != E; ++I) 247572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Bad.push_back(SE.getMulExpr(NegOne, *I)); 248572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return; 2497979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman } 250eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman 251572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Ok, we can't do anything interesting. Just stuff the whole thing into a 252572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // register and hope for the best. 253572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Bad.push_back(S); 254a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman} 255844731a7f1909f55935e3514c9e713a62d67662eDan Gohman 256572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// InitialMatch - Incorporate loop-variant parts of S into this Formula, 257572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// attempting to keep all loop-invariant and loop-computable values in a 258572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// single base register. 259572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanvoid Formula::InitialMatch(const SCEV *S, Loop *L, 260572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman ScalarEvolution &SE, DominatorTree &DT) { 261572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallVector<const SCEV *, 4> Good; 262572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallVector<const SCEV *, 4> Bad; 263572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman DoInitialMatch(S, L, Good, Bad, SE, DT); 264572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!Good.empty()) { 265572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman BaseRegs.push_back(SE.getAddExpr(Good)); 266572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman AM.HasBaseReg = true; 267572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 268572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!Bad.empty()) { 269572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman BaseRegs.push_back(SE.getAddExpr(Bad)); 270572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman AM.HasBaseReg = true; 271572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 272572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 273eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman 274572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// getNumRegs - Return the total number of register operands used by this 275572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// formula. This does not include register uses implied by non-constant 276572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// addrec strides. 277572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanunsigned Formula::getNumRegs() const { 278572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return !!ScaledReg + BaseRegs.size(); 279a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman} 28056a1f806aff00a8be6db4eb5c9cd5c751ff0f4c1Jim Grosbach 281572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// getType - Return the type of this formula, if it has one, or null 282572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// otherwise. This type is meaningless except for the bit size. 283572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanconst Type *Formula::getType() const { 284572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return !BaseRegs.empty() ? BaseRegs.front()->getType() : 285572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman ScaledReg ? ScaledReg->getType() : 286572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman AM.BaseGV ? AM.BaseGV->getType() : 287572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 0; 288572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 28956a1f806aff00a8be6db4eb5c9cd5c751ff0f4c1Jim Grosbach 290572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// referencesReg - Test if this formula references the given register. 291572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanbool Formula::referencesReg(const SCEV *S) const { 292572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return S == ScaledReg || 293572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman std::find(BaseRegs.begin(), BaseRegs.end(), S) != BaseRegs.end(); 294572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 295eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman 296572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// hasRegsUsedByUsesOtherThan - Test whether this formula uses registers 297572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// which are used by uses other than the use with the given index. 298572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanbool Formula::hasRegsUsedByUsesOtherThan(size_t LUIdx, 299572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const RegUseTracker &RegUses) const { 300572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (ScaledReg) 301572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (RegUses.isRegUsedByUsesOtherThan(ScaledReg, LUIdx)) 302572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return true; 303572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (SmallVectorImpl<const SCEV *>::const_iterator I = BaseRegs.begin(), 304572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman E = BaseRegs.end(); I != E; ++I) 305572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (RegUses.isRegUsedByUsesOtherThan(*I, LUIdx)) 306572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return true; 307572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return false; 308572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 309572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 310572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanvoid Formula::print(raw_ostream &OS) const { 311572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman bool First = true; 312572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (AM.BaseGV) { 313572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!First) OS << " + "; else First = false; 314572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman WriteAsOperand(OS, AM.BaseGV, /*PrintType=*/false); 315572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 316572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (AM.BaseOffs != 0) { 317572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!First) OS << " + "; else First = false; 318572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman OS << AM.BaseOffs; 319572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 320572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (SmallVectorImpl<const SCEV *>::const_iterator I = BaseRegs.begin(), 321572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman E = BaseRegs.end(); I != E; ++I) { 322572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!First) OS << " + "; else First = false; 323572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman OS << "reg(" << **I << ')'; 324572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 325572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (AM.Scale != 0) { 326572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!First) OS << " + "; else First = false; 327572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman OS << AM.Scale << "*reg("; 328572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (ScaledReg) 329572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman OS << *ScaledReg; 330572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman else 331572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman OS << "<unknown>"; 332572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman OS << ')'; 333572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 334572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 335572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 336572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanvoid Formula::dump() const { 337572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman print(errs()); errs() << '\n'; 338572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 339572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 340572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// getSDiv - Return an expression for LHS /s RHS, if it can be determined, 341572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// or null otherwise. If IgnoreSignificantBits is true, expressions like 342572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// (X * Y) /s Y are simplified to Y, ignoring that the multiplication may 343572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// overflow, which is useful when the result will be used in a context where 344572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// the most significant bits are ignored. 345572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanstatic const SCEV *getSDiv(const SCEV *LHS, const SCEV *RHS, 346572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman ScalarEvolution &SE, 347572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman bool IgnoreSignificantBits = false) { 348572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Handle the trivial case, which works for any SCEV type. 349572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (LHS == RHS) 350572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return SE.getIntegerSCEV(1, LHS->getType()); 351572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 352572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Handle x /s -1 as x * -1, to give ScalarEvolution a chance to do some 353572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // folding. 354572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (RHS->isAllOnesValue()) 355572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return SE.getMulExpr(LHS, RHS); 356572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 357572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Check for a division of a constant by a constant. 358572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (const SCEVConstant *C = dyn_cast<SCEVConstant>(LHS)) { 359572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const SCEVConstant *RC = dyn_cast<SCEVConstant>(RHS); 360572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!RC) 361572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return 0; 362572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (C->getValue()->getValue().srem(RC->getValue()->getValue()) != 0) 363572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return 0; 364572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return SE.getConstant(C->getValue()->getValue() 365572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman .sdiv(RC->getValue()->getValue())); 366572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 367572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 368572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Distribute the sdiv over addrec operands. 369572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LHS)) { 370572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const SCEV *Start = getSDiv(AR->getStart(), RHS, SE, 371572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman IgnoreSignificantBits); 372572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!Start) return 0; 373572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const SCEV *Step = getSDiv(AR->getStepRecurrence(SE), RHS, SE, 374572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman IgnoreSignificantBits); 375572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!Step) return 0; 376572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return SE.getAddRecExpr(Start, Step, AR->getLoop()); 377572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 378572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 379572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Distribute the sdiv over add operands. 380572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(LHS)) { 381572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallVector<const SCEV *, 8> Ops; 382572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (SCEVAddExpr::op_iterator I = Add->op_begin(), E = Add->op_end(); 383572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman I != E; ++I) { 384572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const SCEV *Op = getSDiv(*I, RHS, SE, 385572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman IgnoreSignificantBits); 386572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!Op) return 0; 387572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Ops.push_back(Op); 388572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 389572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return SE.getAddExpr(Ops); 390572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 391572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 392572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Check for a multiply operand that we can pull RHS out of. 393572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(LHS)) 394572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (IgnoreSignificantBits || Mul->hasNoSignedWrap()) { 395572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallVector<const SCEV *, 4> Ops; 396572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman bool Found = false; 397572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (SCEVMulExpr::op_iterator I = Mul->op_begin(), E = Mul->op_end(); 398572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman I != E; ++I) { 399572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!Found) 400572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (const SCEV *Q = getSDiv(*I, RHS, SE, IgnoreSignificantBits)) { 401572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Ops.push_back(Q); 402572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Found = true; 403572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman continue; 404572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 405572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Ops.push_back(*I); 406a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman } 407572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return Found ? SE.getMulExpr(Ops) : 0; 408572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 409a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman 410572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Otherwise we don't know. 411572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return 0; 412572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 413572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 414572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// ExtractImmediate - If S involves the addition of a constant integer value, 415572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// return that integer value, and mutate S to point to a new SCEV with that 416572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// value excluded. 417572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanstatic int64_t ExtractImmediate(const SCEV *&S, ScalarEvolution &SE) { 418572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) { 419572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (C->getValue()->getValue().getMinSignedBits() <= 64) { 420572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman S = SE.getIntegerSCEV(0, C->getType()); 421572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return C->getValue()->getSExtValue(); 422572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 423572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } else if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) { 424572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallVector<const SCEV *, 8> NewOps(Add->op_begin(), Add->op_end()); 425572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman int64_t Result = ExtractImmediate(NewOps.front(), SE); 426572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman S = SE.getAddExpr(NewOps); 427572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return Result; 428572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } else if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) { 429572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallVector<const SCEV *, 8> NewOps(AR->op_begin(), AR->op_end()); 430572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman int64_t Result = ExtractImmediate(NewOps.front(), SE); 431572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman S = SE.getAddRecExpr(NewOps, AR->getLoop()); 432572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return Result; 43321e7722868378f67974e648ab21d0e3c69a0e379Dan Gohman } 434572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return 0; 435572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 436572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 437572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// ExtractSymbol - If S involves the addition of a GlobalValue address, 438572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// return that symbol, and mutate S to point to a new SCEV with that 439572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// value excluded. 440572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanstatic GlobalValue *ExtractSymbol(const SCEV *&S, ScalarEvolution &SE) { 441572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) { 442572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (GlobalValue *GV = dyn_cast<GlobalValue>(U->getValue())) { 443572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman S = SE.getIntegerSCEV(0, GV->getType()); 444572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return GV; 445572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 446572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } else if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) { 447572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallVector<const SCEV *, 8> NewOps(Add->op_begin(), Add->op_end()); 448572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman GlobalValue *Result = ExtractSymbol(NewOps.back(), SE); 449572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman S = SE.getAddExpr(NewOps); 450572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return Result; 451572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } else if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) { 452572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallVector<const SCEV *, 8> NewOps(AR->op_begin(), AR->op_end()); 453572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman GlobalValue *Result = ExtractSymbol(NewOps.front(), SE); 454572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman S = SE.getAddRecExpr(NewOps, AR->getLoop()); 455572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return Result; 456572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 457572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return 0; 458169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman} 459169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman 4607979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman/// isAddressUse - Returns true if the specified instruction is using the 4617979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman/// specified value as an address. 4627979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohmanstatic bool isAddressUse(Instruction *Inst, Value *OperandVal) { 4637979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman bool isAddress = isa<LoadInst>(Inst); 4647979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) { 4657979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman if (SI->getOperand(1) == OperandVal) 4667979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman isAddress = true; 4677979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) { 4687979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman // Addressing modes can also be folded into prefetches and a variety 4697979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman // of intrinsics. 4707979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman switch (II->getIntrinsicID()) { 4717979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman default: break; 4727979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman case Intrinsic::prefetch: 4737979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman case Intrinsic::x86_sse2_loadu_dq: 4747979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman case Intrinsic::x86_sse2_loadu_pd: 4757979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman case Intrinsic::x86_sse_loadu_ps: 4767979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman case Intrinsic::x86_sse_storeu_ps: 4777979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman case Intrinsic::x86_sse2_storeu_pd: 4787979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman case Intrinsic::x86_sse2_storeu_dq: 4797979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman case Intrinsic::x86_sse2_storel_dq: 4807979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman if (II->getOperand(1) == OperandVal) 4817979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman isAddress = true; 4827979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman break; 4837979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman } 4847979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman } 4857979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman return isAddress; 486169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman} 487169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman 4887979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman/// getAccessType - Return the type of the memory being accessed. 4897979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohmanstatic const Type *getAccessType(const Instruction *Inst) { 4907979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman const Type *AccessTy = Inst->getType(); 4917979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman if (const StoreInst *SI = dyn_cast<StoreInst>(Inst)) 4927979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman AccessTy = SI->getOperand(0)->getType(); 4937979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman else if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) { 4947979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman // Addressing modes can also be folded into prefetches and a variety 4957979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman // of intrinsics. 4967979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman switch (II->getIntrinsicID()) { 4977979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman default: break; 4987979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman case Intrinsic::x86_sse_storeu_ps: 4997979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman case Intrinsic::x86_sse2_storeu_pd: 5007979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman case Intrinsic::x86_sse2_storeu_dq: 5017979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman case Intrinsic::x86_sse2_storel_dq: 5027979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman AccessTy = II->getOperand(1)->getType(); 5037979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman break; 5047979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman } 5057979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman } 506572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 507572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // All pointers have the same requirements, so canonicalize them to an 508572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // arbitrary pointer type to minimize variation. 509572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (const PointerType *PTy = dyn_cast<PointerType>(AccessTy)) 510572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman AccessTy = PointerType::get(IntegerType::get(PTy->getContext(), 1), 511572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman PTy->getAddressSpace()); 512572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 5137979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman return AccessTy; 514a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman} 51581db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman 516572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// DeleteTriviallyDeadInstructions - If any of the instructions is the 517572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// specified set are trivially dead, delete them and see if this makes any of 518572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// their operands subsequently dead. 519572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanstatic bool 520572645cf84060c0fc25cb91d38cb9079918b3a88Dan GohmanDeleteTriviallyDeadInstructions(SmallVectorImpl<WeakVH> &DeadInsts) { 521572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman bool Changed = false; 5222f09f519542202b8af227c2a524f8fe82378a934Dan Gohman 523572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman while (!DeadInsts.empty()) { 524572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Instruction *I = dyn_cast_or_null<Instruction>(DeadInsts.pop_back_val()); 52581db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman 526572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (I == 0 || !isInstructionTriviallyDead(I)) 527572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman continue; 528221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner 529572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (User::op_iterator OI = I->op_begin(), E = I->op_end(); OI != E; ++OI) 530572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (Instruction *U = dyn_cast<Instruction>(*OI)) { 531572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman *OI = 0; 532572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (U->use_empty()) 533572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman DeadInsts.push_back(U); 534572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 535221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner 536572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman I->eraseFromParent(); 537572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Changed = true; 538572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 5392f09f519542202b8af227c2a524f8fe82378a934Dan Gohman 540572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return Changed; 5417979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman} 5422f46bb8178e30e3b845859a44b57c048db06ef84Dale Johannesen 543572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmannamespace { 5442f09f519542202b8af227c2a524f8fe82378a934Dan Gohman 545572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// Cost - This class is used to measure and compare candidate formulae. 546572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanclass Cost { 547572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman /// TODO: Some of these could be merged. Also, a lexical ordering 548572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman /// isn't always optimal. 549572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman unsigned NumRegs; 550572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman unsigned AddRecCost; 551572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman unsigned NumIVMuls; 552572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman unsigned NumBaseAdds; 553572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman unsigned ImmCost; 554572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman unsigned SetupCost; 555572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 556572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanpublic: 557572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Cost() 558572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman : NumRegs(0), AddRecCost(0), NumIVMuls(0), NumBaseAdds(0), ImmCost(0), 559572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SetupCost(0) {} 560572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 561572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman unsigned getNumRegs() const { return NumRegs; } 562572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 563572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman bool operator<(const Cost &Other) const; 564572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 565572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman void Loose(); 566572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 567572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman void RateFormula(const Formula &F, 568572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallPtrSet<const SCEV *, 16> &Regs, 569572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const DenseSet<const SCEV *> &VisitedRegs, 570572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const Loop *L, 571572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const SmallVectorImpl<int64_t> &Offsets, 572572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman ScalarEvolution &SE, DominatorTree &DT); 573572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 574572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman void print(raw_ostream &OS) const; 575572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman void dump() const; 576572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 577572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanprivate: 578572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman void RateRegister(const SCEV *Reg, 579572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallPtrSet<const SCEV *, 16> &Regs, 580572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const Loop *L, 581572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman ScalarEvolution &SE, DominatorTree &DT); 5829214b82c5446791b280821bbd892dca633130f80Dan Gohman void RatePrimaryRegister(const SCEV *Reg, 5839214b82c5446791b280821bbd892dca633130f80Dan Gohman SmallPtrSet<const SCEV *, 16> &Regs, 5849214b82c5446791b280821bbd892dca633130f80Dan Gohman const Loop *L, 5859214b82c5446791b280821bbd892dca633130f80Dan Gohman ScalarEvolution &SE, DominatorTree &DT); 586572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman}; 5870e0014d0499d6ec6402e07b71cf24af992a9d297Evan Cheng 588572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 5897979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman 590572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// RateRegister - Tally up interesting quantities from the given register. 591572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanvoid Cost::RateRegister(const SCEV *Reg, 592572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallPtrSet<const SCEV *, 16> &Regs, 593572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const Loop *L, 594572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman ScalarEvolution &SE, DominatorTree &DT) { 5959214b82c5446791b280821bbd892dca633130f80Dan Gohman if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Reg)) { 5969214b82c5446791b280821bbd892dca633130f80Dan Gohman if (AR->getLoop() == L) 5979214b82c5446791b280821bbd892dca633130f80Dan Gohman AddRecCost += 1; /// TODO: This should be a function of the stride. 5989214b82c5446791b280821bbd892dca633130f80Dan Gohman 5999214b82c5446791b280821bbd892dca633130f80Dan Gohman // If this is an addrec for a loop that's already been visited by LSR, 6009214b82c5446791b280821bbd892dca633130f80Dan Gohman // don't second-guess its addrec phi nodes. LSR isn't currently smart 6019214b82c5446791b280821bbd892dca633130f80Dan Gohman // enough to reason about more than one loop at a time. Consider these 6029214b82c5446791b280821bbd892dca633130f80Dan Gohman // registers free and leave them alone. 6039214b82c5446791b280821bbd892dca633130f80Dan Gohman else if (L->contains(AR->getLoop()) || 6049214b82c5446791b280821bbd892dca633130f80Dan Gohman (!AR->getLoop()->contains(L) && 6059214b82c5446791b280821bbd892dca633130f80Dan Gohman DT.dominates(L->getHeader(), AR->getLoop()->getHeader()))) { 6069214b82c5446791b280821bbd892dca633130f80Dan Gohman for (BasicBlock::iterator I = AR->getLoop()->getHeader()->begin(); 6079214b82c5446791b280821bbd892dca633130f80Dan Gohman PHINode *PN = dyn_cast<PHINode>(I); ++I) 6089214b82c5446791b280821bbd892dca633130f80Dan Gohman if (SE.isSCEVable(PN->getType()) && 6099214b82c5446791b280821bbd892dca633130f80Dan Gohman (SE.getEffectiveSCEVType(PN->getType()) == 6109214b82c5446791b280821bbd892dca633130f80Dan Gohman SE.getEffectiveSCEVType(AR->getType())) && 6119214b82c5446791b280821bbd892dca633130f80Dan Gohman SE.getSCEV(PN) == AR) 6129214b82c5446791b280821bbd892dca633130f80Dan Gohman return; 6139214b82c5446791b280821bbd892dca633130f80Dan Gohman 6149214b82c5446791b280821bbd892dca633130f80Dan Gohman // If this isn't one of the addrecs that the loop already has, it 6159214b82c5446791b280821bbd892dca633130f80Dan Gohman // would require a costly new phi and add. TODO: This isn't 6169214b82c5446791b280821bbd892dca633130f80Dan Gohman // precisely modeled right now. 6179214b82c5446791b280821bbd892dca633130f80Dan Gohman ++NumBaseAdds; 6189214b82c5446791b280821bbd892dca633130f80Dan Gohman if (!Regs.count(AR->getStart())) 619572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman RateRegister(AR->getStart(), Regs, L, SE, DT); 6209214b82c5446791b280821bbd892dca633130f80Dan Gohman } 6217979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman 6229214b82c5446791b280821bbd892dca633130f80Dan Gohman // Add the step value register, if it needs one. 6239214b82c5446791b280821bbd892dca633130f80Dan Gohman // TODO: The non-affine case isn't precisely modeled here. 6249214b82c5446791b280821bbd892dca633130f80Dan Gohman if (!AR->isAffine() || !isa<SCEVConstant>(AR->getOperand(1))) 6259214b82c5446791b280821bbd892dca633130f80Dan Gohman if (!Regs.count(AR->getStart())) 626572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman RateRegister(AR->getOperand(1), Regs, L, SE, DT); 627a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman } 6289214b82c5446791b280821bbd892dca633130f80Dan Gohman ++NumRegs; 6299214b82c5446791b280821bbd892dca633130f80Dan Gohman 6309214b82c5446791b280821bbd892dca633130f80Dan Gohman // Rough heuristic; favor registers which don't require extra setup 6319214b82c5446791b280821bbd892dca633130f80Dan Gohman // instructions in the preheader. 6329214b82c5446791b280821bbd892dca633130f80Dan Gohman if (!isa<SCEVUnknown>(Reg) && 6339214b82c5446791b280821bbd892dca633130f80Dan Gohman !isa<SCEVConstant>(Reg) && 6349214b82c5446791b280821bbd892dca633130f80Dan Gohman !(isa<SCEVAddRecExpr>(Reg) && 6359214b82c5446791b280821bbd892dca633130f80Dan Gohman (isa<SCEVUnknown>(cast<SCEVAddRecExpr>(Reg)->getStart()) || 6369214b82c5446791b280821bbd892dca633130f80Dan Gohman isa<SCEVConstant>(cast<SCEVAddRecExpr>(Reg)->getStart())))) 6379214b82c5446791b280821bbd892dca633130f80Dan Gohman ++SetupCost; 6389214b82c5446791b280821bbd892dca633130f80Dan Gohman} 6399214b82c5446791b280821bbd892dca633130f80Dan Gohman 6409214b82c5446791b280821bbd892dca633130f80Dan Gohman/// RatePrimaryRegister - Record this register in the set. If we haven't seen it 6419214b82c5446791b280821bbd892dca633130f80Dan Gohman/// before, rate it. 6429214b82c5446791b280821bbd892dca633130f80Dan Gohmanvoid Cost::RatePrimaryRegister(const SCEV *Reg, 6439214b82c5446791b280821bbd892dca633130f80Dan Gohman SmallPtrSet<const SCEV *, 16> &Regs, 6449214b82c5446791b280821bbd892dca633130f80Dan Gohman const Loop *L, 6459214b82c5446791b280821bbd892dca633130f80Dan Gohman ScalarEvolution &SE, DominatorTree &DT) { 6469214b82c5446791b280821bbd892dca633130f80Dan Gohman if (Regs.insert(Reg)) 6479214b82c5446791b280821bbd892dca633130f80Dan Gohman RateRegister(Reg, Regs, L, SE, DT); 6487979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman} 6497979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman 650572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanvoid Cost::RateFormula(const Formula &F, 651572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallPtrSet<const SCEV *, 16> &Regs, 652572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const DenseSet<const SCEV *> &VisitedRegs, 653572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const Loop *L, 654572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const SmallVectorImpl<int64_t> &Offsets, 655572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman ScalarEvolution &SE, DominatorTree &DT) { 656572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Tally up the registers. 657572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (const SCEV *ScaledReg = F.ScaledReg) { 658572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (VisitedRegs.count(ScaledReg)) { 659572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Loose(); 660572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return; 6617979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman } 6629214b82c5446791b280821bbd892dca633130f80Dan Gohman RatePrimaryRegister(ScaledReg, Regs, L, SE, DT); 6633821e478a574b80d7f8bc96fa42731291cfccfe8Chris Lattner } 664572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (SmallVectorImpl<const SCEV *>::const_iterator I = F.BaseRegs.begin(), 665572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman E = F.BaseRegs.end(); I != E; ++I) { 666572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const SCEV *BaseReg = *I; 667572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (VisitedRegs.count(BaseReg)) { 668572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Loose(); 669572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return; 6707979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman } 6719214b82c5446791b280821bbd892dca633130f80Dan Gohman RatePrimaryRegister(BaseReg, Regs, L, SE, DT); 6722d1be87ee40a4a0241d94448173879d9df2bc5b3Dan Gohman 673572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman NumIVMuls += isa<SCEVMulExpr>(BaseReg) && 674572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman BaseReg->hasComputableLoopEvolution(L); 675572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 676572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 677572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (F.BaseRegs.size() > 1) 678572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman NumBaseAdds += F.BaseRegs.size() - 1; 679572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 680572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Tally up the non-zero immediates. 681572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (SmallVectorImpl<int64_t>::const_iterator I = Offsets.begin(), 682572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman E = Offsets.end(); I != E; ++I) { 683572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman int64_t Offset = (uint64_t)*I + F.AM.BaseOffs; 684572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (F.AM.BaseGV) 685572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman ImmCost += 64; // Handle symbolic values conservatively. 686572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // TODO: This should probably be the pointer size. 687572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman else if (Offset != 0) 688572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman ImmCost += APInt(64, Offset, true).getMinSignedBits(); 689572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 690169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman} 691169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman 692572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// Loose - Set this cost to a loosing value. 693572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanvoid Cost::Loose() { 694572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman NumRegs = ~0u; 695572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman AddRecCost = ~0u; 696572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman NumIVMuls = ~0u; 697572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman NumBaseAdds = ~0u; 698572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman ImmCost = ~0u; 699572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SetupCost = ~0u; 700572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 70156a1f806aff00a8be6db4eb5c9cd5c751ff0f4c1Jim Grosbach 702572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// operator< - Choose the lower cost. 703572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanbool Cost::operator<(const Cost &Other) const { 704572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (NumRegs != Other.NumRegs) 705572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return NumRegs < Other.NumRegs; 706572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (AddRecCost != Other.AddRecCost) 707572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return AddRecCost < Other.AddRecCost; 708572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (NumIVMuls != Other.NumIVMuls) 709572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return NumIVMuls < Other.NumIVMuls; 710572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (NumBaseAdds != Other.NumBaseAdds) 711572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return NumBaseAdds < Other.NumBaseAdds; 712572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (ImmCost != Other.ImmCost) 713572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return ImmCost < Other.ImmCost; 714572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (SetupCost != Other.SetupCost) 715572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return SetupCost < Other.SetupCost; 716572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return false; 717572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 7187979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman 719572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanvoid Cost::print(raw_ostream &OS) const { 720572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman OS << NumRegs << " reg" << (NumRegs == 1 ? "" : "s"); 721572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (AddRecCost != 0) 722572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman OS << ", with addrec cost " << AddRecCost; 723572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (NumIVMuls != 0) 724572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman OS << ", plus " << NumIVMuls << " IV mul" << (NumIVMuls == 1 ? "" : "s"); 725572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (NumBaseAdds != 0) 726572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman OS << ", plus " << NumBaseAdds << " base add" 727572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman << (NumBaseAdds == 1 ? "" : "s"); 728572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (ImmCost != 0) 729572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman OS << ", plus " << ImmCost << " imm cost"; 730572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (SetupCost != 0) 731572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman OS << ", plus " << SetupCost << " setup cost"; 732572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 73344b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner 734572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanvoid Cost::dump() const { 735572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman print(errs()); errs() << '\n'; 73644b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner} 73744b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner 738572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmannamespace { 73944b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner 740572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// LSRFixup - An operand value in an instruction which is to be replaced 741572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// with some equivalent, possibly strength-reduced, replacement. 742572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanstruct LSRFixup { 743572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman /// UserInst - The instruction which will be updated. 744572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Instruction *UserInst; 74556a1f806aff00a8be6db4eb5c9cd5c751ff0f4c1Jim Grosbach 746572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman /// OperandValToReplace - The operand of the instruction which will 747572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman /// be replaced. The operand may be used more than once; every instance 748572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman /// will be replaced. 749572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Value *OperandValToReplace; 75056a1f806aff00a8be6db4eb5c9cd5c751ff0f4c1Jim Grosbach 751572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman /// PostIncLoop - If this user is to use the post-incremented value of an 752572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman /// induction variable, this variable is non-null and holds the loop 753572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman /// associated with the induction variable. 754572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const Loop *PostIncLoop; 75526d91f16464db56283087176a73981048331dd2dChris Lattner 756572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman /// LUIdx - The index of the LSRUse describing the expression which 757572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman /// this fixup needs, minus an offset (below). 758572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman size_t LUIdx; 75926d91f16464db56283087176a73981048331dd2dChris Lattner 760572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman /// Offset - A constant offset to be added to the LSRUse expression. 761572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman /// This allows multiple fixups to share the same LSRUse with different 762572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman /// offsets, for example in an unrolled loop. 763572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman int64_t Offset; 764572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 765572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LSRFixup(); 766572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 767572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman void print(raw_ostream &OS) const; 768572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman void dump() const; 769572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman}; 770572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 771572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 772572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 773572645cf84060c0fc25cb91d38cb9079918b3a88Dan GohmanLSRFixup::LSRFixup() 774572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman : UserInst(0), OperandValToReplace(0), PostIncLoop(0), 775572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LUIdx(~size_t(0)), Offset(0) {} 776572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 777572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanvoid LSRFixup::print(raw_ostream &OS) const { 778572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman OS << "UserInst="; 779572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Store is common and interesting enough to be worth special-casing. 780572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (StoreInst *Store = dyn_cast<StoreInst>(UserInst)) { 781572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman OS << "store "; 782572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman WriteAsOperand(OS, Store->getOperand(0), /*PrintType=*/false); 783572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } else if (UserInst->getType()->isVoidTy()) 784572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman OS << UserInst->getOpcodeName(); 785572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman else 786572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman WriteAsOperand(OS, UserInst, /*PrintType=*/false); 787572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 788572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman OS << ", OperandValToReplace="; 789572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman WriteAsOperand(OS, OperandValToReplace, /*PrintType=*/false); 790572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 791572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (PostIncLoop) { 792572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman OS << ", PostIncLoop="; 793572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman WriteAsOperand(OS, PostIncLoop->getHeader(), /*PrintType=*/false); 794934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner } 795a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman 796572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (LUIdx != ~size_t(0)) 797572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman OS << ", LUIdx=" << LUIdx; 798572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 799572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (Offset != 0) 800572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman OS << ", Offset=" << Offset; 8017979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman} 8027979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman 803572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanvoid LSRFixup::dump() const { 804572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman print(errs()); errs() << '\n'; 8057979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman} 8067979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman 807572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmannamespace { 808a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman 809572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// UniquifierDenseMapInfo - A DenseMapInfo implementation for holding 810572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// DenseMaps and DenseSets of sorted SmallVectors of const SCEV*. 811572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanstruct UniquifierDenseMapInfo { 812572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman static SmallVector<const SCEV *, 2> getEmptyKey() { 813572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallVector<const SCEV *, 2> V; 814572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman V.push_back(reinterpret_cast<const SCEV *>(-1)); 815572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return V; 816572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 817934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner 818572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman static SmallVector<const SCEV *, 2> getTombstoneKey() { 819572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallVector<const SCEV *, 2> V; 820572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman V.push_back(reinterpret_cast<const SCEV *>(-2)); 821572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return V; 822572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 823572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 824572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman static unsigned getHashValue(const SmallVector<const SCEV *, 2> &V) { 825572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman unsigned Result = 0; 826572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (SmallVectorImpl<const SCEV *>::const_iterator I = V.begin(), 827572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman E = V.end(); I != E; ++I) 828572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Result ^= DenseMapInfo<const SCEV *>::getHashValue(*I); 8297979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman return Result; 830a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman } 831a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman 832572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman static bool isEqual(const SmallVector<const SCEV *, 2> &LHS, 833572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const SmallVector<const SCEV *, 2> &RHS) { 834572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return LHS == RHS; 835572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 836572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman}; 837572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 838572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// LSRUse - This class holds the state that LSR keeps for each use in 839572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// IVUsers, as well as uses invented by LSR itself. It includes information 840572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// about what kinds of things can be folded into the user, information about 841572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// the user itself, and information about how the use may be satisfied. 842572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// TODO: Represent multiple users of the same expression in common? 843572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanclass LSRUse { 844572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman DenseSet<SmallVector<const SCEV *, 2>, UniquifierDenseMapInfo> Uniquifier; 845572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 846572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanpublic: 847572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman /// KindType - An enum for a kind of use, indicating what types of 848572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman /// scaled and immediate operands it might support. 849572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman enum KindType { 850572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Basic, ///< A normal use, with no folding. 851572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Special, ///< A special case of basic, allowing -1 scales. 852572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Address, ///< An address use; folding according to TargetLowering 853572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman ICmpZero ///< An equality icmp with both operands folded into one. 854572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // TODO: Add a generic icmp too? 855572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman }; 8567979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman 857572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman KindType Kind; 858572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const Type *AccessTy; 8597979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman 860572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallVector<int64_t, 8> Offsets; 861572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman int64_t MinOffset; 862572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman int64_t MaxOffset; 863203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen 864572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman /// AllFixupsOutsideLoop - This records whether all of the fixups using this 865572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman /// LSRUse are outside of the loop, in which case some special-case heuristics 866572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman /// may be used. 867572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman bool AllFixupsOutsideLoop; 8687979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman 869572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman /// Formulae - A list of ways to build a value that can satisfy this user. 870572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman /// After the list is populated, one of these is selected heuristically and 871572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman /// used to formulate a replacement for OperandValToReplace in UserInst. 872572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallVector<Formula, 12> Formulae; 873589bf0865ccd10d36f406d622c0160be249343e1Dale Johannesen 874572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman /// Regs - The set of register candidates used by all formulae in this LSRUse. 875572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallPtrSet<const SCEV *, 4> Regs; 876934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner 877572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LSRUse(KindType K, const Type *T) : Kind(K), AccessTy(T), 878572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman MinOffset(INT64_MAX), 879572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman MaxOffset(INT64_MIN), 880572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman AllFixupsOutsideLoop(true) {} 88156a1f806aff00a8be6db4eb5c9cd5c751ff0f4c1Jim Grosbach 882572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman bool InsertFormula(size_t LUIdx, const Formula &F); 883572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 884572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman void check() const; 885572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 886572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman void print(raw_ostream &OS) const; 887572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman void dump() const; 888572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman}; 889572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 890572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// InsertFormula - If the given formula has not yet been inserted, add it to 891572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// the list, and return true. Return false otherwise. 892572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanbool LSRUse::InsertFormula(size_t LUIdx, const Formula &F) { 893572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallVector<const SCEV *, 2> Key = F.BaseRegs; 894572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (F.ScaledReg) Key.push_back(F.ScaledReg); 895572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Unstable sort by host order ok, because this is only used for uniquifying. 896572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman std::sort(Key.begin(), Key.end()); 897572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 898572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!Uniquifier.insert(Key).second) 899572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return false; 900572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 901572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Using a register to hold the value of 0 is not profitable. 902572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman assert((!F.ScaledReg || !F.ScaledReg->isZero()) && 903572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman "Zero allocated in a scaled register!"); 904572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman#ifndef NDEBUG 905572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (SmallVectorImpl<const SCEV *>::const_iterator I = 906572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman F.BaseRegs.begin(), E = F.BaseRegs.end(); I != E; ++I) 907572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman assert(!(*I)->isZero() && "Zero allocated in a base register!"); 908572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman#endif 909572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 910572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Add the formula to the list. 911572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Formulae.push_back(F); 91256a1f806aff00a8be6db4eb5c9cd5c751ff0f4c1Jim Grosbach 913572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Record registers now being used by this use. 914572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (F.ScaledReg) Regs.insert(F.ScaledReg); 915572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Regs.insert(F.BaseRegs.begin(), F.BaseRegs.end()); 916572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 917572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return true; 918572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 919572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 920572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanvoid LSRUse::print(raw_ostream &OS) const { 921572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman OS << "LSR Use: Kind="; 922572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman switch (Kind) { 923572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman case Basic: OS << "Basic"; break; 924572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman case Special: OS << "Special"; break; 925572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman case ICmpZero: OS << "ICmpZero"; break; 926572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman case Address: 927572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman OS << "Address of "; 928572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (isa<PointerType>(AccessTy)) 929572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman OS << "pointer"; // the full pointer type could be really verbose 9307979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman else 931572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman OS << *AccessTy; 9327979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman } 9331bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner 934572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman OS << ", Offsets={"; 935572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (SmallVectorImpl<int64_t>::const_iterator I = Offsets.begin(), 936572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman E = Offsets.end(); I != E; ++I) { 937572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman OS << *I; 938572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (next(I) != E) 939572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman OS << ','; 940572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 941572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman OS << '}'; 942572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 943572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (AllFixupsOutsideLoop) 944572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman OS << ", all-fixups-outside-loop"; 9457979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman} 946d6b62a572210aff965a55626cf36a68821838844Evan Cheng 947572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanvoid LSRUse::dump() const { 948572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman print(errs()); errs() << '\n'; 949572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 95056a1f806aff00a8be6db4eb5c9cd5c751ff0f4c1Jim Grosbach 951572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// isLegalUse - Test whether the use described by AM is "legal", meaning it can 952572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// be completely folded into the user instruction at isel time. This includes 953572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// address-mode folding and special icmp tricks. 954572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanstatic bool isLegalUse(const TargetLowering::AddrMode &AM, 955572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LSRUse::KindType Kind, const Type *AccessTy, 956572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const TargetLowering *TLI) { 957572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman switch (Kind) { 958572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman case LSRUse::Address: 959572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // If we have low-level target information, ask the target if it can 960572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // completely fold this address. 961572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (TLI) return TLI->isLegalAddressingMode(AM, AccessTy); 962572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 963572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Otherwise, just guess that reg+reg addressing is legal. 964572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return !AM.BaseGV && AM.BaseOffs == 0 && AM.Scale <= 1; 965572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 966572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman case LSRUse::ICmpZero: 967572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // There's not even a target hook for querying whether it would be legal to 968572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // fold a GV into an ICmp. 969572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (AM.BaseGV) 970572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return false; 971579633cd1006f6add1b022e9c2bc96f7f0e65777Chris Lattner 972572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // ICmp only has two operands; don't allow more than two non-trivial parts. 973572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (AM.Scale != 0 && AM.HasBaseReg && AM.BaseOffs != 0) 974572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return false; 9751bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner 976572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // ICmp only supports no scale or a -1 scale, as we can "fold" a -1 scale by 977572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // putting the scaled register in the other operand of the icmp. 978572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (AM.Scale != 0 && AM.Scale != -1) 9797979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman return false; 98081db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman 981572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // If we have low-level target information, ask the target if it can fold an 982572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // integer immediate on an icmp. 983572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (AM.BaseOffs != 0) { 984572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (TLI) return TLI->isLegalICmpImmediate(-AM.BaseOffs); 985572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return false; 986572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 987572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 9887979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman return true; 98981db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman 990572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman case LSRUse::Basic: 991572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Only handle single-register values. 992572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return !AM.BaseGV && AM.Scale == 0 && AM.BaseOffs == 0; 99381db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman 994572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman case LSRUse::Special: 995572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Only handle -1 scales, or no scale. 996572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return AM.Scale == 0 || AM.Scale == -1; 997572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 998572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 999572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return false; 1000572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 100181db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman 1002572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanstatic bool isLegalUse(TargetLowering::AddrMode AM, 1003572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman int64_t MinOffset, int64_t MaxOffset, 1004572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LSRUse::KindType Kind, const Type *AccessTy, 1005572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const TargetLowering *TLI) { 1006572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Check for overflow. 1007572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (((int64_t)((uint64_t)AM.BaseOffs + MinOffset) > AM.BaseOffs) != 1008572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman (MinOffset > 0)) 1009572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return false; 1010572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman AM.BaseOffs = (uint64_t)AM.BaseOffs + MinOffset; 1011572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (isLegalUse(AM, Kind, AccessTy, TLI)) { 1012572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman AM.BaseOffs = (uint64_t)AM.BaseOffs - MinOffset; 1013572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Check for overflow. 1014572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (((int64_t)((uint64_t)AM.BaseOffs + MaxOffset) > AM.BaseOffs) != 1015572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman (MaxOffset > 0)) 10167979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman return false; 1017572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman AM.BaseOffs = (uint64_t)AM.BaseOffs + MaxOffset; 1018572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return isLegalUse(AM, Kind, AccessTy, TLI); 10197979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman } 1020572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return false; 10215f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng} 10225f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng 1023572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanstatic bool isAlwaysFoldable(int64_t BaseOffs, 1024572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman GlobalValue *BaseGV, 1025572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman bool HasBaseReg, 1026572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LSRUse::KindType Kind, const Type *AccessTy, 1027572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const TargetLowering *TLI, 1028572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman ScalarEvolution &SE) { 1029572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Fast-path: zero is always foldable. 1030572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (BaseOffs == 0 && !BaseGV) return true; 1031572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1032572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Conservatively, create an address with an immediate and a 1033572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // base and a scale. 1034572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman TargetLowering::AddrMode AM; 1035572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman AM.BaseOffs = BaseOffs; 1036572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman AM.BaseGV = BaseGV; 1037572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman AM.HasBaseReg = HasBaseReg; 1038572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman AM.Scale = Kind == LSRUse::ICmpZero ? -1 : 1; 1039572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1040572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return isLegalUse(AM, Kind, AccessTy, TLI); 1041a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman} 1042a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman 1043572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanstatic bool isAlwaysFoldable(const SCEV *S, 1044572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman int64_t MinOffset, int64_t MaxOffset, 1045572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman bool HasBaseReg, 1046572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LSRUse::KindType Kind, const Type *AccessTy, 1047572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const TargetLowering *TLI, 1048572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman ScalarEvolution &SE) { 1049572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Fast-path: zero is always foldable. 1050572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (S->isZero()) return true; 1051572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1052572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Conservatively, create an address with an immediate and a 1053572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // base and a scale. 1054572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman int64_t BaseOffs = ExtractImmediate(S, SE); 1055572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman GlobalValue *BaseGV = ExtractSymbol(S, SE); 1056572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1057572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // If there's anything else involved, it's not foldable. 1058572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!S->isZero()) return false; 1059572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1060572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Fast-path: zero is always foldable. 1061572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (BaseOffs == 0 && !BaseGV) return true; 1062572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1063572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Conservatively, create an address with an immediate and a 1064572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // base and a scale. 1065572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman TargetLowering::AddrMode AM; 1066572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman AM.BaseOffs = BaseOffs; 1067572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman AM.BaseGV = BaseGV; 1068572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman AM.HasBaseReg = HasBaseReg; 1069572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman AM.Scale = Kind == LSRUse::ICmpZero ? -1 : 1; 1070572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1071572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return isLegalUse(AM, MinOffset, MaxOffset, Kind, AccessTy, TLI); 1072572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 107356a1f806aff00a8be6db4eb5c9cd5c751ff0f4c1Jim Grosbach 1074572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// FormulaSorter - This class implements an ordering for formulae which sorts 1075572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// the by their standalone cost. 1076572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanclass FormulaSorter { 1077572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman /// These two sets are kept empty, so that we compute standalone costs. 1078572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman DenseSet<const SCEV *> VisitedRegs; 1079572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallPtrSet<const SCEV *, 16> Regs; 1080572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Loop *L; 1081572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LSRUse *LU; 1082572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman ScalarEvolution &SE; 1083572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman DominatorTree &DT; 1084572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1085572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanpublic: 1086572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman FormulaSorter(Loop *l, LSRUse &lu, ScalarEvolution &se, DominatorTree &dt) 1087572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman : L(l), LU(&lu), SE(se), DT(dt) {} 1088572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1089572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman bool operator()(const Formula &A, const Formula &B) { 1090572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Cost CostA; 1091572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman CostA.RateFormula(A, Regs, VisitedRegs, L, LU->Offsets, SE, DT); 1092572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Regs.clear(); 1093572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Cost CostB; 1094572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman CostB.RateFormula(B, Regs, VisitedRegs, L, LU->Offsets, SE, DT); 1095572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Regs.clear(); 1096572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return CostA < CostB; 1097572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 1098572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman}; 1099572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1100572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// LSRInstance - This class holds state for the main loop strength reduction 1101572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// logic. 1102572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanclass LSRInstance { 1103572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman IVUsers &IU; 1104572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman ScalarEvolution &SE; 1105572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman DominatorTree &DT; 1106572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const TargetLowering *const TLI; 1107572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Loop *const L; 1108572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman bool Changed; 1109572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1110572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman /// IVIncInsertPos - This is the insert position that the current loop's 1111572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman /// induction variable increment should be placed. In simple loops, this is 1112572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman /// the latch block's terminator. But in more complicated cases, this is a 1113572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman /// position which will dominate all the in-loop post-increment users. 1114572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Instruction *IVIncInsertPos; 1115572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1116572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman /// Factors - Interesting factors between use strides. 1117572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallSetVector<int64_t, 8> Factors; 1118572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1119572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman /// Types - Interesting use types, to facilitate truncation reuse. 1120572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallSetVector<const Type *, 4> Types; 1121572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1122572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman /// Fixups - The list of operands which are to be replaced. 1123572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallVector<LSRFixup, 16> Fixups; 1124572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1125572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman /// Uses - The list of interesting uses. 1126572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallVector<LSRUse, 16> Uses; 1127572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1128572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman /// RegUses - Track which uses use which register candidates. 1129572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman RegUseTracker RegUses; 1130572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1131572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman void OptimizeShadowIV(); 1132572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman bool FindIVUserForCond(ICmpInst *Cond, IVStrideUse *&CondUse); 1133572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman ICmpInst *OptimizeMax(ICmpInst *Cond, IVStrideUse* &CondUse); 1134572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman bool OptimizeLoopTermCond(); 1135572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1136572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman void CollectInterestingTypesAndFactors(); 1137572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman void CollectFixupsAndInitialFormulae(); 1138572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1139572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LSRFixup &getNewFixup() { 1140572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Fixups.push_back(LSRFixup()); 1141572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return Fixups.back(); 1142a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman } 114332e4c7c486084cdbed07925be4a0e9f3ab6caedeEvan Cheng 1144572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Support for sharing of LSRUses between LSRFixups. 1145572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman typedef DenseMap<const SCEV *, size_t> UseMapTy; 1146572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman UseMapTy UseMap; 1147572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1148572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman bool reconcileNewOffset(LSRUse &LU, int64_t NewOffset, 1149572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LSRUse::KindType Kind, const Type *AccessTy); 1150572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1151572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman std::pair<size_t, int64_t> getUse(const SCEV *&Expr, 1152572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LSRUse::KindType Kind, 1153572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const Type *AccessTy); 1154572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1155572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanpublic: 1156572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman void InsertInitialFormula(const SCEV *S, Loop *L, LSRUse &LU, size_t LUIdx); 1157572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman void InsertSupplementalFormula(const SCEV *S, LSRUse &LU, size_t LUIdx); 1158572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman void CountRegisters(const Formula &F, size_t LUIdx); 1159572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman bool InsertFormula(LSRUse &LU, unsigned LUIdx, const Formula &F); 1160572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1161572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman void CollectLoopInvariantFixupsAndFormulae(); 1162572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1163572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman void GenerateReassociations(LSRUse &LU, unsigned LUIdx, Formula Base, 1164572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman unsigned Depth = 0); 1165572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman void GenerateCombinations(LSRUse &LU, unsigned LUIdx, Formula Base); 1166572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman void GenerateSymbolicOffsets(LSRUse &LU, unsigned LUIdx, Formula Base); 1167572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman void GenerateConstantOffsets(LSRUse &LU, unsigned LUIdx, Formula Base); 1168572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman void GenerateICmpZeroScales(LSRUse &LU, unsigned LUIdx, Formula Base); 1169572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman void GenerateScales(LSRUse &LU, unsigned LUIdx, Formula Base); 1170572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman void GenerateTruncates(LSRUse &LU, unsigned LUIdx, Formula Base); 1171572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman void GenerateCrossUseConstantOffsets(); 1172572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman void GenerateAllReuseFormulae(); 1173572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1174572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman void FilterOutUndesirableDedicatedRegisters(); 1175572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman void NarrowSearchSpaceUsingHeuristics(); 1176572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1177572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman void SolveRecurse(SmallVectorImpl<const Formula *> &Solution, 1178572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Cost &SolutionCost, 1179572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallVectorImpl<const Formula *> &Workspace, 1180572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const Cost &CurCost, 1181572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const SmallPtrSet<const SCEV *, 16> &CurRegs, 1182572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman DenseSet<const SCEV *> &VisitedRegs) const; 1183572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman void Solve(SmallVectorImpl<const Formula *> &Solution) const; 1184572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1185572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Value *Expand(const LSRFixup &LF, 1186572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const Formula &F, 1187572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman BasicBlock::iterator IP, Loop *L, Instruction *IVIncInsertPos, 1188572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SCEVExpander &Rewriter, 1189572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallVectorImpl<WeakVH> &DeadInsts, 1190572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman ScalarEvolution &SE, DominatorTree &DT) const; 1191572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman void Rewrite(const LSRFixup &LF, 1192572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const Formula &F, 1193572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Loop *L, Instruction *IVIncInsertPos, 1194572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SCEVExpander &Rewriter, 1195572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallVectorImpl<WeakVH> &DeadInsts, 1196572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman ScalarEvolution &SE, DominatorTree &DT, 1197572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Pass *P) const; 1198572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman void ImplementSolution(const SmallVectorImpl<const Formula *> &Solution, 1199572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Pass *P); 1200572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1201572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LSRInstance(const TargetLowering *tli, Loop *l, Pass *P); 1202572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1203572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman bool getChanged() const { return Changed; } 1204572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1205572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman void print_factors_and_types(raw_ostream &OS) const; 1206572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman void print_fixups(raw_ostream &OS) const; 1207572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman void print_uses(raw_ostream &OS) const; 1208572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman void print(raw_ostream &OS) const; 1209572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman void dump() const; 1210572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman}; 12117979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman 1212a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman} 1213c17e0cf6c03a36f424fafe88497b5fdf351cd50aDan Gohman 1214572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// OptimizeShadowIV - If IV is used in a int-to-float cast 1215572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// inside the loop then try to eliminate the cast opeation. 1216572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanvoid LSRInstance::OptimizeShadowIV() { 1217572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const SCEV *BackedgeTakenCount = SE.getBackedgeTakenCount(L); 1218572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (isa<SCEVCouldNotCompute>(BackedgeTakenCount)) 1219572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return; 1220c17e0cf6c03a36f424fafe88497b5fdf351cd50aDan Gohman 1221572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (IVUsers::const_iterator UI = IU.begin(), E = IU.end(); 1222572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman UI != E; /* empty */) { 1223572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman IVUsers::const_iterator CandidateUI = UI; 1224572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman ++UI; 1225572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Instruction *ShadowUse = CandidateUI->getUser(); 1226572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const Type *DestTy = NULL; 1227c17e0cf6c03a36f424fafe88497b5fdf351cd50aDan Gohman 1228572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman /* If shadow use is a int->float cast then insert a second IV 1229572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman to eliminate this cast. 1230c17e0cf6c03a36f424fafe88497b5fdf351cd50aDan Gohman 1231572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (unsigned i = 0; i < n; ++i) 1232572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman foo((double)i); 1233c17e0cf6c03a36f424fafe88497b5fdf351cd50aDan Gohman 1234572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman is transformed into 1235c17e0cf6c03a36f424fafe88497b5fdf351cd50aDan Gohman 1236572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman double d = 0.0; 1237572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (unsigned i = 0; i < n; ++i, ++d) 1238572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman foo(d); 1239572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman */ 1240572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (UIToFPInst *UCast = dyn_cast<UIToFPInst>(CandidateUI->getUser())) 1241572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman DestTy = UCast->getDestTy(); 1242572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman else if (SIToFPInst *SCast = dyn_cast<SIToFPInst>(CandidateUI->getUser())) 1243572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman DestTy = SCast->getDestTy(); 1244572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!DestTy) continue; 12455792f51e12d9c8685399e9857799365854ab5bf6Evan Cheng 1246572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (TLI) { 1247572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // If target does not support DestTy natively then do not apply 1248572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // this transformation. 1249572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman EVT DVT = TLI->getValueType(DestTy); 1250572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!TLI->isTypeLegal(DVT)) continue; 12517e79b3898ddd919170d367a516f51296017146c2Chris Lattner } 12527e79b3898ddd919170d367a516f51296017146c2Chris Lattner 1253572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman PHINode *PH = dyn_cast<PHINode>(ShadowUse->getOperand(0)); 1254572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!PH) continue; 1255572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (PH->getNumIncomingValues() != 2) continue; 1256a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman 1257572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const Type *SrcTy = PH->getType(); 1258572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman int Mantissa = DestTy->getFPMantissaWidth(); 1259572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (Mantissa == -1) continue; 1260572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if ((int)SE.getTypeSizeInBits(SrcTy) > Mantissa) 1261572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman continue; 12622f46bb8178e30e3b845859a44b57c048db06ef84Dale Johannesen 1263572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman unsigned Entry, Latch; 1264572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (PH->getIncomingBlock(0) == L->getLoopPreheader()) { 1265572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Entry = 0; 1266572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Latch = 1; 1267572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } else { 1268572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Entry = 1; 1269572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Latch = 0; 1270a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman } 1271eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman 1272572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman ConstantInt *Init = dyn_cast<ConstantInt>(PH->getIncomingValue(Entry)); 1273572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!Init) continue; 1274572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Constant *NewInit = ConstantFP::get(DestTy, Init->getZExtValue()); 1275a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman 1276572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman BinaryOperator *Incr = 1277572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman dyn_cast<BinaryOperator>(PH->getIncomingValue(Latch)); 1278572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!Incr) continue; 1279572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (Incr->getOpcode() != Instruction::Add 1280572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman && Incr->getOpcode() != Instruction::Sub) 1281572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman continue; 1282aed01d19315132daf68414ace410ec725b4b6d30Chris Lattner 1283572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman /* Initialize new IV, double d = 0.0 in above example. */ 1284572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman ConstantInt *C = NULL; 1285572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (Incr->getOperand(0) == PH) 1286572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman C = dyn_cast<ConstantInt>(Incr->getOperand(1)); 1287572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman else if (Incr->getOperand(1) == PH) 1288572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman C = dyn_cast<ConstantInt>(Incr->getOperand(0)); 1289572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman else 1290572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman continue; 1291bc511725f08c45984be6ff47d069c3773a2f2eb0Dan Gohman 1292572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!C) continue; 1293a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman 1294572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Ignore negative constants, as the code below doesn't handle them 1295572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // correctly. TODO: Remove this restriction. 1296572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!C->getValue().isStrictlyPositive()) continue; 1297a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman 1298572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman /* Add new PHINode. */ 1299572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman PHINode *NewPH = PHINode::Create(DestTy, "IV.S.", PH); 1300a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman 1301572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman /* create new increment. '++d' in above example. */ 1302572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Constant *CFP = ConstantFP::get(DestTy, C->getZExtValue()); 1303572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman BinaryOperator *NewIncr = 1304572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman BinaryOperator::Create(Incr->getOpcode() == Instruction::Add ? 1305572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Instruction::FAdd : Instruction::FSub, 1306572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman NewPH, CFP, "IV.S.next.", Incr); 1307a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman 1308572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman NewPH->addIncoming(NewInit, PH->getIncomingBlock(Entry)); 1309572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman NewPH->addIncoming(NewIncr, PH->getIncomingBlock(Latch)); 13108b0ade3eb8281f9b332d5764cfe5c4ed3b2b32d8Dan Gohman 1311572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman /* Remove cast operation */ 1312572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman ShadowUse->replaceAllUsesWith(NewPH); 1313572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman ShadowUse->eraseFromParent(); 1314572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman break; 13157979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman } 1316a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman} 1317cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng 13187979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman/// FindIVUserForCond - If Cond has an operand that is an expression of an IV, 13197979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman/// set the IV user and stride information and return true, otherwise return 13207979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman/// false. 1321572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanbool LSRInstance::FindIVUserForCond(ICmpInst *Cond, 1322572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman IVStrideUse *&CondUse) { 1323572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (IVUsers::iterator UI = IU.begin(), E = IU.end(); UI != E; ++UI) 1324572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (UI->getUser() == Cond) { 1325572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // NOTE: we could handle setcc instructions with multiple uses here, but 1326572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // InstCombine does it as well for simple uses, it's not clear that it 1327572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // occurs enough in real life to handle. 1328572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman CondUse = UI; 1329572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return true; 1330a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman } 1331572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return false; 1332cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng} 1333cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng 13347979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman/// OptimizeMax - Rewrite the loop's terminating condition if it uses 13357979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman/// a max computation. 13367979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman/// 13377979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman/// This is a narrow solution to a specific, but acute, problem. For loops 13387979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman/// like this: 13397979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman/// 13407979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman/// i = 0; 13417979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman/// do { 13427979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman/// p[i] = 0.0; 13437979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman/// } while (++i < n); 13447979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman/// 13457979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman/// the trip count isn't just 'n', because 'n' might not be positive. And 13467979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman/// unfortunately this can come up even for loops where the user didn't use 13477979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman/// a C do-while loop. For example, seemingly well-behaved top-test loops 13487979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman/// will commonly be lowered like this: 13497979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman// 13507979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman/// if (n > 0) { 13517979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman/// i = 0; 13527979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman/// do { 13537979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman/// p[i] = 0.0; 13547979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman/// } while (++i < n); 13557979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman/// } 13567979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman/// 13577979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman/// and then it's possible for subsequent optimization to obscure the if 13587979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman/// test in such a way that indvars can't find it. 13597979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman/// 13607979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman/// When indvars can't find the if test in loops like this, it creates a 13617979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman/// max expression, which allows it to give the loop a canonical 13627979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman/// induction variable: 13637979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman/// 13647979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman/// i = 0; 13657979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman/// max = n < 1 ? 1 : n; 13667979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman/// do { 13677979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman/// p[i] = 0.0; 13687979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman/// } while (++i != max); 13697979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman/// 13707979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman/// Canonical induction variables are necessary because the loop passes 13717979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman/// are designed around them. The most obvious example of this is the 13727979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman/// LoopInfo analysis, which doesn't remember trip count values. It 13737979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman/// expects to be able to rediscover the trip count each time it is 1374572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// needed, and it does this using a simple analysis that only succeeds if 13757979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman/// the loop has a canonical induction variable. 13767979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman/// 13777979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman/// However, when it comes time to generate code, the maximum operation 13787979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman/// can be quite costly, especially if it's inside of an outer loop. 13797979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman/// 13807979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman/// This function solves this problem by detecting this type of loop and 13817979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman/// rewriting their conditions from ICMP_NE back to ICMP_SLT, and deleting 13827979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman/// the instructions for the maximum computation. 13837979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman/// 1384572645cf84060c0fc25cb91d38cb9079918b3a88Dan GohmanICmpInst *LSRInstance::OptimizeMax(ICmpInst *Cond, IVStrideUse* &CondUse) { 13857979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman // Check that the loop matches the pattern we're looking for. 13867979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman if (Cond->getPredicate() != CmpInst::ICMP_EQ && 13877979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman Cond->getPredicate() != CmpInst::ICMP_NE) 13887979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman return Cond; 1389ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman 13907979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman SelectInst *Sel = dyn_cast<SelectInst>(Cond->getOperand(1)); 13917979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman if (!Sel || !Sel->hasOneUse()) return Cond; 1392ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman 1393572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const SCEV *BackedgeTakenCount = SE.getBackedgeTakenCount(L); 13947979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman if (isa<SCEVCouldNotCompute>(BackedgeTakenCount)) 13957979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman return Cond; 1396572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const SCEV *One = SE.getIntegerSCEV(1, BackedgeTakenCount->getType()); 1397ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman 13987979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman // Add one to the backedge-taken count to get the trip count. 1399572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const SCEV *IterationCount = SE.getAddExpr(BackedgeTakenCount, One); 14008b0ade3eb8281f9b332d5764cfe5c4ed3b2b32d8Dan Gohman 14017979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman // Check for a max calculation that matches the pattern. 14027979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman if (!isa<SCEVSMaxExpr>(IterationCount) && !isa<SCEVUMaxExpr>(IterationCount)) 14037979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman return Cond; 14047979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman const SCEVNAryExpr *Max = cast<SCEVNAryExpr>(IterationCount); 1405572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (Max != SE.getSCEV(Sel)) return Cond; 1406ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman 14077979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman // To handle a max with more than two operands, this optimization would 14087979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman // require additional checking and setup. 14097979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman if (Max->getNumOperands() != 2) 14107979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman return Cond; 14117979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman 14127979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman const SCEV *MaxLHS = Max->getOperand(0); 14137979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman const SCEV *MaxRHS = Max->getOperand(1); 14147979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman if (!MaxLHS || MaxLHS != One) return Cond; 14157979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman // Check the relevant induction variable for conformance to 14167979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman // the pattern. 1417572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const SCEV *IV = SE.getSCEV(Cond->getOperand(0)); 14187979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(IV); 14197979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman if (!AR || !AR->isAffine() || 14207979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman AR->getStart() != One || 1421572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman AR->getStepRecurrence(SE) != One) 14227979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman return Cond; 1423ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman 14247979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman assert(AR->getLoop() == L && 14257979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman "Loop condition operand is an addrec in a different loop!"); 1426bc10b8c6c7e85e44d8231dfb2fb41a60300857e3Dan Gohman 14277979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman // Check the right operand of the select, and remember it, as it will 14287979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman // be used in the new comparison instruction. 14297979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman Value *NewRHS = 0; 1430572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (SE.getSCEV(Sel->getOperand(1)) == MaxRHS) 14317979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman NewRHS = Sel->getOperand(1); 1432572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman else if (SE.getSCEV(Sel->getOperand(2)) == MaxRHS) 14337979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman NewRHS = Sel->getOperand(2); 14347979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman if (!NewRHS) return Cond; 1435ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman 14367979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman // Determine the new comparison opcode. It may be signed or unsigned, 14377979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman // and the original comparison may be either equality or inequality. 14387979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman CmpInst::Predicate Pred = 14397979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman isa<SCEVSMaxExpr>(Max) ? CmpInst::ICMP_SLT : CmpInst::ICMP_ULT; 14407979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman if (Cond->getPredicate() == CmpInst::ICMP_EQ) 14417979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman Pred = CmpInst::getInversePredicate(Pred); 14422781f30eac8647552638246c28ab07dd0fc2c560Dan Gohman 14437979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman // Ok, everything looks ok to change the condition into an SLT or SGE and 14447979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman // delete the max calculation. 14457979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman ICmpInst *NewCond = 14467979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman new ICmpInst(Cond, Pred, Cond->getOperand(0), NewRHS, "scmp"); 1447ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman 14487979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman // Delete the max calculation instructions. 14497979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman Cond->replaceAllUsesWith(NewCond); 14507979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman CondUse->setUser(NewCond); 14517979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman Instruction *Cmp = cast<Instruction>(Sel->getOperand(0)); 14527979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman Cond->eraseFromParent(); 14537979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman Sel->eraseFromParent(); 14547979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman if (Cmp->use_empty()) 14557979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman Cmp->eraseFromParent(); 14567979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman return NewCond; 1457ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman} 1458ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman 145956a1f806aff00a8be6db4eb5c9cd5c751ff0f4c1Jim Grosbach/// OptimizeLoopTermCond - Change loop terminating condition to use the 14602d85052f2bae5a5a31c03017c48ca8a9eba1453cEvan Cheng/// postinc iv when possible. 1461572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanbool 1462572645cf84060c0fc25cb91d38cb9079918b3a88Dan GohmanLSRInstance::OptimizeLoopTermCond() { 1463572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallPtrSet<Instruction *, 4> PostIncs; 1464572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 14655792f51e12d9c8685399e9857799365854ab5bf6Evan Cheng BasicBlock *LatchBlock = L->getLoopLatch(); 1466076e085698c484354c9e131f1bd8fd001581397bEvan Cheng SmallVector<BasicBlock*, 8> ExitingBlocks; 1467076e085698c484354c9e131f1bd8fd001581397bEvan Cheng L->getExitingBlocks(ExitingBlocks); 146856a1f806aff00a8be6db4eb5c9cd5c751ff0f4c1Jim Grosbach 1469076e085698c484354c9e131f1bd8fd001581397bEvan Cheng for (unsigned i = 0, e = ExitingBlocks.size(); i != e; ++i) { 1470076e085698c484354c9e131f1bd8fd001581397bEvan Cheng BasicBlock *ExitingBlock = ExitingBlocks[i]; 1471010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner 1472572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Get the terminating condition for the loop if possible. If we 1473076e085698c484354c9e131f1bd8fd001581397bEvan Cheng // can, we want to change it to use a post-incremented version of its 1474076e085698c484354c9e131f1bd8fd001581397bEvan Cheng // induction variable, to allow coalescing the live ranges for the IV into 1475076e085698c484354c9e131f1bd8fd001581397bEvan Cheng // one register value. 1476cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng 1477076e085698c484354c9e131f1bd8fd001581397bEvan Cheng BranchInst *TermBr = dyn_cast<BranchInst>(ExitingBlock->getTerminator()); 1478076e085698c484354c9e131f1bd8fd001581397bEvan Cheng if (!TermBr) 1479076e085698c484354c9e131f1bd8fd001581397bEvan Cheng continue; 1480076e085698c484354c9e131f1bd8fd001581397bEvan Cheng // FIXME: Overly conservative, termination condition could be an 'or' etc.. 1481076e085698c484354c9e131f1bd8fd001581397bEvan Cheng if (TermBr->isUnconditional() || !isa<ICmpInst>(TermBr->getCondition())) 1482076e085698c484354c9e131f1bd8fd001581397bEvan Cheng continue; 1483076e085698c484354c9e131f1bd8fd001581397bEvan Cheng 1484076e085698c484354c9e131f1bd8fd001581397bEvan Cheng // Search IVUsesByStride to find Cond's IVUse if there is one. 1485076e085698c484354c9e131f1bd8fd001581397bEvan Cheng IVStrideUse *CondUse = 0; 1486076e085698c484354c9e131f1bd8fd001581397bEvan Cheng ICmpInst *Cond = cast<ICmpInst>(TermBr->getCondition()); 1487572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!FindIVUserForCond(Cond, CondUse)) 1488076e085698c484354c9e131f1bd8fd001581397bEvan Cheng continue; 1489076e085698c484354c9e131f1bd8fd001581397bEvan Cheng 14907979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman // If the trip count is computed in terms of a max (due to ScalarEvolution 14917979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman // being unable to find a sufficient guard, for example), change the loop 14927979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman // comparison to use SLT or ULT instead of NE. 1493572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // One consequence of doing this now is that it disrupts the count-down 1494572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // optimization. That's not always a bad thing though, because in such 1495572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // cases it may still be worthwhile to avoid a max. 1496572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Cond = OptimizeMax(Cond, CondUse); 1497572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1498572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // If this exiting block dominates the latch block, it may also use 1499572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // the post-inc value if it won't be shared with other uses. 1500572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Check for dominance. 1501572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!DT.dominates(ExitingBlock, LatchBlock)) 15027979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman continue; 15037979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman 1504572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Conservatively avoid trying to use the post-inc value in non-latch 1505572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // exits if there may be pre-inc users in intervening blocks. 1506590bfe8641631c136160fed7a9df9fe805938cbeDan Gohman if (LatchBlock != ExitingBlock) 1507572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (IVUsers::const_iterator UI = IU.begin(), E = IU.end(); UI != E; ++UI) 1508572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Test if the use is reachable from the exiting block. This dominator 1509572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // query is a conservative approximation of reachability. 1510572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (&*UI != CondUse && 1511572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman !DT.properlyDominates(UI->getUser()->getParent(), ExitingBlock)) { 1512572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Conservatively assume there may be reuse if the quotient of their 1513572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // strides could be a legal scale. 1514572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const SCEV *A = CondUse->getStride(); 1515572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const SCEV *B = UI->getStride(); 1516572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (SE.getTypeSizeInBits(A->getType()) != 1517572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SE.getTypeSizeInBits(B->getType())) { 1518572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (SE.getTypeSizeInBits(A->getType()) > 1519572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SE.getTypeSizeInBits(B->getType())) 1520572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman B = SE.getSignExtendExpr(B, A->getType()); 1521572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman else 1522572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman A = SE.getSignExtendExpr(A, B->getType()); 1523572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 1524572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (const SCEVConstant *D = 1525572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman dyn_cast_or_null<SCEVConstant>(getSDiv(B, A, SE))) { 1526572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Stride of one or negative one can have reuse with non-addresses. 1527572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (D->getValue()->isOne() || 1528572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman D->getValue()->isAllOnesValue()) 1529572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman goto decline_post_inc; 1530572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Avoid weird situations. 1531572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (D->getValue()->getValue().getMinSignedBits() >= 64 || 1532572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman D->getValue()->getValue().isMinSignedValue()) 1533572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman goto decline_post_inc; 1534590bfe8641631c136160fed7a9df9fe805938cbeDan Gohman // Without TLI, assume that any stride might be valid, and so any 1535590bfe8641631c136160fed7a9df9fe805938cbeDan Gohman // use might be shared. 1536590bfe8641631c136160fed7a9df9fe805938cbeDan Gohman if (!TLI) 1537590bfe8641631c136160fed7a9df9fe805938cbeDan Gohman goto decline_post_inc; 1538572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Check for possible scaled-address reuse. 1539572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const Type *AccessTy = getAccessType(UI->getUser()); 1540572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman TargetLowering::AddrMode AM; 1541572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman AM.Scale = D->getValue()->getSExtValue(); 15422763dfdc701964a01c4a0386fadd1c4e92be9021Dan Gohman if (TLI->isLegalAddressingMode(AM, AccessTy)) 1543572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman goto decline_post_inc; 1544572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman AM.Scale = -AM.Scale; 15452763dfdc701964a01c4a0386fadd1c4e92be9021Dan Gohman if (TLI->isLegalAddressingMode(AM, AccessTy)) 1546572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman goto decline_post_inc; 1547572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 1548572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 1549572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 155063c9463c62fce8cbe02176dfa2d73f375a06f1f2David Greene DEBUG(dbgs() << " Change loop exiting icmp to use postinc iv: " 1551572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman << *Cond << '\n'); 155256a1f806aff00a8be6db4eb5c9cd5c751ff0f4c1Jim Grosbach 1553076e085698c484354c9e131f1bd8fd001581397bEvan Cheng // It's possible for the setcc instruction to be anywhere in the loop, and 1554076e085698c484354c9e131f1bd8fd001581397bEvan Cheng // possible for it to have multiple users. If it is not immediately before 1555076e085698c484354c9e131f1bd8fd001581397bEvan Cheng // the exiting block branch, move it. 1556572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (&*++BasicBlock::iterator(Cond) != TermBr) { 1557572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (Cond->hasOneUse()) { 1558076e085698c484354c9e131f1bd8fd001581397bEvan Cheng Cond->moveBefore(TermBr); 1559076e085698c484354c9e131f1bd8fd001581397bEvan Cheng } else { 1560572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Clone the terminating condition and insert into the loopend. 1561572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman ICmpInst *OldCond = Cond; 1562076e085698c484354c9e131f1bd8fd001581397bEvan Cheng Cond = cast<ICmpInst>(Cond->clone()); 1563076e085698c484354c9e131f1bd8fd001581397bEvan Cheng Cond->setName(L->getHeader()->getName() + ".termcond"); 1564076e085698c484354c9e131f1bd8fd001581397bEvan Cheng ExitingBlock->getInstList().insert(TermBr, Cond); 1565076e085698c484354c9e131f1bd8fd001581397bEvan Cheng 1566076e085698c484354c9e131f1bd8fd001581397bEvan Cheng // Clone the IVUse, as the old use still exists! 1567572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman CondUse = &IU.AddUser(CondUse->getStride(), CondUse->getOffset(), 1568572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Cond, CondUse->getOperandValToReplace()); 1569572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman TermBr->replaceUsesOfWith(OldCond, Cond); 1570076e085698c484354c9e131f1bd8fd001581397bEvan Cheng } 1571010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner } 1572010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner 1573076e085698c484354c9e131f1bd8fd001581397bEvan Cheng // If we get to here, we know that we can transform the setcc instruction to 1574076e085698c484354c9e131f1bd8fd001581397bEvan Cheng // use the post-incremented version of the IV, allowing us to coalesce the 1575076e085698c484354c9e131f1bd8fd001581397bEvan Cheng // live ranges for the IV correctly. 1576572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman CondUse->setOffset(SE.getMinusSCEV(CondUse->getOffset(), 1577572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman CondUse->getStride())); 1578076e085698c484354c9e131f1bd8fd001581397bEvan Cheng CondUse->setIsUseOfPostIncrementedValue(true); 1579076e085698c484354c9e131f1bd8fd001581397bEvan Cheng Changed = true; 15805792f51e12d9c8685399e9857799365854ab5bf6Evan Cheng 1581572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman PostIncs.insert(Cond); 1582572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman decline_post_inc:; 1583c1acc3f764804d25f70d88f937ef9c460143e0f1Dale Johannesen } 1584c1acc3f764804d25f70d88f937ef9c460143e0f1Dale Johannesen 1585572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Determine an insertion point for the loop induction variable increment. It 1586572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // must dominate all the post-inc comparisons we just set up, and it must 1587572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // dominate the loop latch edge. 1588572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman IVIncInsertPos = L->getLoopLatch()->getTerminator(); 1589572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (SmallPtrSet<Instruction *, 4>::const_iterator I = PostIncs.begin(), 1590572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman E = PostIncs.end(); I != E; ++I) { 1591572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman BasicBlock *BB = 1592572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman DT.findNearestCommonDominator(IVIncInsertPos->getParent(), 1593572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman (*I)->getParent()); 1594572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (BB == (*I)->getParent()) 1595572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman IVIncInsertPos = *I; 1596572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman else if (BB != IVIncInsertPos->getParent()) 1597572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman IVIncInsertPos = BB->getTerminator(); 1598572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 15998b0ade3eb8281f9b332d5764cfe5c4ed3b2b32d8Dan Gohman 1600572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return Changed; 1601572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 1602a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman 1603572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanbool 1604572645cf84060c0fc25cb91d38cb9079918b3a88Dan GohmanLSRInstance::reconcileNewOffset(LSRUse &LU, int64_t NewOffset, 1605572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LSRUse::KindType Kind, const Type *AccessTy) { 1606572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman int64_t NewMinOffset = LU.MinOffset; 1607572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman int64_t NewMaxOffset = LU.MaxOffset; 1608572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const Type *NewAccessTy = AccessTy; 1609572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1610572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Check for a mismatched kind. It's tempting to collapse mismatched kinds to 1611572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // something conservative, however this can pessimize in the case that one of 1612572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // the uses will have all its uses outside the loop, for example. 1613572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (LU.Kind != Kind) 1614572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return false; 1615572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Conservatively assume HasBaseReg is true for now. 1616572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (NewOffset < LU.MinOffset) { 1617572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!isAlwaysFoldable(LU.MaxOffset - NewOffset, 0, /*HasBaseReg=*/true, 1618572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Kind, AccessTy, TLI, SE)) 16197979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman return false; 1620572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman NewMinOffset = NewOffset; 1621572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } else if (NewOffset > LU.MaxOffset) { 1622572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!isAlwaysFoldable(NewOffset - LU.MinOffset, 0, /*HasBaseReg=*/true, 1623572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Kind, AccessTy, TLI, SE)) 16247979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman return false; 1625572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman NewMaxOffset = NewOffset; 1626a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman } 1627572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Check for a mismatched access type, and fall back conservatively as needed. 1628572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (Kind == LSRUse::Address && AccessTy != LU.AccessTy) 1629572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman NewAccessTy = Type::getVoidTy(AccessTy->getContext()); 1630572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1631572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Update the use. 1632572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LU.MinOffset = NewMinOffset; 1633572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LU.MaxOffset = NewMaxOffset; 1634572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LU.AccessTy = NewAccessTy; 1635572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (NewOffset != LU.Offsets.back()) 1636572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LU.Offsets.push_back(NewOffset); 1637572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return true; 1638572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 1639a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman 1640572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// getUse - Return an LSRUse index and an offset value for a fixup which 1641572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// needs the given expression, with the given kind and optional access type. 1642572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// Either reuse an exisitng use or create a new one, as needed. 1643572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanstd::pair<size_t, int64_t> 1644572645cf84060c0fc25cb91d38cb9079918b3a88Dan GohmanLSRInstance::getUse(const SCEV *&Expr, 1645572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LSRUse::KindType Kind, const Type *AccessTy) { 1646572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const SCEV *Copy = Expr; 1647572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman int64_t Offset = ExtractImmediate(Expr, SE); 1648572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1649572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Basic uses can't accept any offset, for example. 1650572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!isAlwaysFoldable(Offset, 0, /*HasBaseReg=*/true, 1651572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Kind, AccessTy, TLI, SE)) { 1652572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Expr = Copy; 1653572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Offset = 0; 1654a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman } 1655a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman 1656572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman std::pair<UseMapTy::iterator, bool> P = 1657572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman UseMap.insert(std::make_pair(Expr, 0)); 1658572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!P.second) { 1659572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // A use already existed with this base. 1660572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman size_t LUIdx = P.first->second; 1661572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LSRUse &LU = Uses[LUIdx]; 1662572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (reconcileNewOffset(LU, Offset, Kind, AccessTy)) 1663572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Reuse this use. 1664572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return std::make_pair(LUIdx, Offset); 1665572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 1666a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman 1667572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Create a new use. 1668572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman size_t LUIdx = Uses.size(); 1669572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman P.first->second = LUIdx; 1670572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Uses.push_back(LSRUse(Kind, AccessTy)); 1671572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LSRUse &LU = Uses[LUIdx]; 1672a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman 1673572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // We don't need to track redundant offsets, but we don't need to go out 1674572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // of our way here to avoid them. 1675572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (LU.Offsets.empty() || Offset != LU.Offsets.back()) 1676572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LU.Offsets.push_back(Offset); 1677a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman 1678572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LU.MinOffset = Offset; 1679572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LU.MaxOffset = Offset; 1680572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return std::make_pair(LUIdx, Offset); 1681a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman} 1682a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman 1683572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanvoid LSRInstance::CollectInterestingTypesAndFactors() { 1684572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallSetVector<const SCEV *, 4> Strides; 1685572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1686572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Collect interesting types and factors. 1687572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (IVUsers::const_iterator UI = IU.begin(), E = IU.end(); UI != E; ++UI) { 1688572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const SCEV *Stride = UI->getStride(); 1689572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1690572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Collect interesting types. 1691572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Types.insert(SE.getEffectiveSCEVType(Stride->getType())); 1692572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1693572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Collect interesting factors. 1694572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (SmallSetVector<const SCEV *, 4>::const_iterator NewStrideIter = 1695572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Strides.begin(), SEnd = Strides.end(); NewStrideIter != SEnd; 1696572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman ++NewStrideIter) { 1697572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const SCEV *OldStride = Stride; 1698572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const SCEV *NewStride = *NewStrideIter; 1699572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (OldStride == NewStride) 17007979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman continue; 1701a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman 1702572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (SE.getTypeSizeInBits(OldStride->getType()) != 1703572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SE.getTypeSizeInBits(NewStride->getType())) { 1704572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (SE.getTypeSizeInBits(OldStride->getType()) > 1705572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SE.getTypeSizeInBits(NewStride->getType())) 1706572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman NewStride = SE.getSignExtendExpr(NewStride, OldStride->getType()); 1707572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman else 1708572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman OldStride = SE.getSignExtendExpr(OldStride, NewStride->getType()); 1709572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 1710572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (const SCEVConstant *Factor = 1711572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman dyn_cast_or_null<SCEVConstant>(getSDiv(NewStride, OldStride, 1712572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SE, true))) { 1713572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (Factor->getValue()->getValue().getMinSignedBits() <= 64) 1714572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Factors.insert(Factor->getValue()->getValue().getSExtValue()); 1715572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } else if (const SCEVConstant *Factor = 1716572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman dyn_cast_or_null<SCEVConstant>(getSDiv(OldStride, NewStride, 1717572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SE, true))) { 1718572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (Factor->getValue()->getValue().getMinSignedBits() <= 64) 1719572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Factors.insert(Factor->getValue()->getValue().getSExtValue()); 1720a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman } 1721a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman } 1722572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Strides.insert(Stride); 17238b0ade3eb8281f9b332d5764cfe5c4ed3b2b32d8Dan Gohman } 1724586f69a11881d828c056ce017b3fb432341d9657Evan Cheng 1725572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // If all uses use the same type, don't bother looking for truncation-based 1726572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // reuse. 1727572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (Types.size() == 1) 1728572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Types.clear(); 1729572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1730572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman DEBUG(print_factors_and_types(dbgs())); 1731c1acc3f764804d25f70d88f937ef9c460143e0f1Dale Johannesen} 1732c1acc3f764804d25f70d88f937ef9c460143e0f1Dale Johannesen 1733572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanvoid LSRInstance::CollectFixupsAndInitialFormulae() { 1734572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (IVUsers::const_iterator UI = IU.begin(), E = IU.end(); UI != E; ++UI) { 1735572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Record the uses. 1736572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LSRFixup &LF = getNewFixup(); 1737572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LF.UserInst = UI->getUser(); 1738572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LF.OperandValToReplace = UI->getOperandValToReplace(); 1739572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (UI->isUseOfPostIncrementedValue()) 1740572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LF.PostIncLoop = L; 17410f54dcbf07c69e41ecaa6b4fbf0d94956d8e9ff5Devang Patel 1742572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LSRUse::KindType Kind = LSRUse::Basic; 1743572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const Type *AccessTy = 0; 1744572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (isAddressUse(LF.UserInst, LF.OperandValToReplace)) { 1745572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Kind = LSRUse::Address; 1746572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman AccessTy = getAccessType(LF.UserInst); 1747572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 1748572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1749572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const SCEV *S = IU.getCanonicalExpr(*UI); 1750572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1751572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Equality (== and !=) ICmps are special. We can rewrite (i == N) as 1752572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // (N - i == 0), and this allows (N - i) to be the expression that we work 1753572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // with rather than just N or i, so we can consider the register 1754572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // requirements for both N and i at the same time. Limiting this code to 1755572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // equality icmps is not a problem because all interesting loops use 1756572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // equality icmps, thanks to IndVarSimplify. 1757572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (ICmpInst *CI = dyn_cast<ICmpInst>(LF.UserInst)) 1758572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (CI->isEquality()) { 1759572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Swap the operands if needed to put the OperandValToReplace on the 1760572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // left, for consistency. 1761572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Value *NV = CI->getOperand(1); 1762572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (NV == LF.OperandValToReplace) { 1763572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman CI->setOperand(1, CI->getOperand(0)); 1764572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman CI->setOperand(0, NV); 1765572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 1766572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1767572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // x == y --> x - y == 0 1768572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const SCEV *N = SE.getSCEV(NV); 1769572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (N->isLoopInvariant(L)) { 1770572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Kind = LSRUse::ICmpZero; 1771572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman S = SE.getMinusSCEV(N, S); 1772572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 1773572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1774572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // -1 and the negations of all interesting strides (except the negation 1775572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // of -1) are now also interesting. 1776572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (size_t i = 0, e = Factors.size(); i != e; ++i) 1777572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (Factors[i] != -1) 1778572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Factors.insert(-(uint64_t)Factors[i]); 1779572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Factors.insert(-1); 1780572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 1781572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1782572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Set up the initial formula for this use. 1783572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman std::pair<size_t, int64_t> P = getUse(S, Kind, AccessTy); 1784572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LF.LUIdx = P.first; 1785572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LF.Offset = P.second; 1786572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LSRUse &LU = Uses[LF.LUIdx]; 1787572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LU.AllFixupsOutsideLoop &= !L->contains(LF.UserInst); 1788572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1789572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // If this is the first use of this LSRUse, give it a formula. 1790572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (LU.Formulae.empty()) { 1791572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman InsertInitialFormula(S, L, LU, LF.LUIdx); 1792572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman CountRegisters(LU.Formulae.back(), LF.LUIdx); 1793572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 1794572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 1795572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1796572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman DEBUG(print_fixups(dbgs())); 1797572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 1798572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1799572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanvoid 1800572645cf84060c0fc25cb91d38cb9079918b3a88Dan GohmanLSRInstance::InsertInitialFormula(const SCEV *S, Loop *L, 1801572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LSRUse &LU, size_t LUIdx) { 1802572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Formula F; 1803572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman F.InitialMatch(S, L, SE, DT); 1804572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman bool Inserted = InsertFormula(LU, LUIdx, F); 1805572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman assert(Inserted && "Initial formula already exists!"); (void)Inserted; 1806572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 1807572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1808572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanvoid 1809572645cf84060c0fc25cb91d38cb9079918b3a88Dan GohmanLSRInstance::InsertSupplementalFormula(const SCEV *S, 1810572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LSRUse &LU, size_t LUIdx) { 1811572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Formula F; 1812572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman F.BaseRegs.push_back(S); 1813572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman F.AM.HasBaseReg = true; 1814572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman bool Inserted = InsertFormula(LU, LUIdx, F); 1815572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman assert(Inserted && "Supplemental formula already exists!"); (void)Inserted; 1816572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 1817572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1818572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// CountRegisters - Note which registers are used by the given formula, 1819572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// updating RegUses. 1820572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanvoid LSRInstance::CountRegisters(const Formula &F, size_t LUIdx) { 1821572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (F.ScaledReg) 1822572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman RegUses.CountRegister(F.ScaledReg, LUIdx); 1823572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (SmallVectorImpl<const SCEV *>::const_iterator I = F.BaseRegs.begin(), 1824572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman E = F.BaseRegs.end(); I != E; ++I) 1825572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman RegUses.CountRegister(*I, LUIdx); 1826572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 1827572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1828572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// InsertFormula - If the given formula has not yet been inserted, add it to 1829572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// the list, and return true. Return false otherwise. 1830572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanbool LSRInstance::InsertFormula(LSRUse &LU, unsigned LUIdx, const Formula &F) { 1831572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!LU.InsertFormula(LUIdx, F)) 18327979b72febb73f7bb1d1ed095a68f210822b2e7cDan Gohman return false; 183380b0f8c0628d848d36f04440687b61e3b2d3dff1Dan Gohman 1834572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman CountRegisters(F, LUIdx); 1835572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return true; 1836572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 1837572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1838572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// CollectLoopInvariantFixupsAndFormulae - Check for other uses of 1839572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// loop-invariant values which we're tracking. These other uses will pin these 1840572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// values in registers, making them less profitable for elimination. 1841572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// TODO: This currently misses non-constant addrec step registers. 1842572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// TODO: Should this give more weight to users inside the loop? 1843572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanvoid 1844572645cf84060c0fc25cb91d38cb9079918b3a88Dan GohmanLSRInstance::CollectLoopInvariantFixupsAndFormulae() { 1845572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallVector<const SCEV *, 8> Worklist(RegUses.begin(), RegUses.end()); 1846572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallPtrSet<const SCEV *, 8> Inserted; 1847572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1848572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman while (!Worklist.empty()) { 1849572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const SCEV *S = Worklist.pop_back_val(); 1850572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1851572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (const SCEVNAryExpr *N = dyn_cast<SCEVNAryExpr>(S)) 1852572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Worklist.insert(Worklist.end(), N->op_begin(), N->op_end()); 1853572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman else if (const SCEVCastExpr *C = dyn_cast<SCEVCastExpr>(S)) 1854572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Worklist.push_back(C->getOperand()); 1855572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman else if (const SCEVUDivExpr *D = dyn_cast<SCEVUDivExpr>(S)) { 1856572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Worklist.push_back(D->getLHS()); 1857572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Worklist.push_back(D->getRHS()); 1858572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } else if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) { 1859572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!Inserted.insert(U)) continue; 1860572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const Value *V = U->getValue(); 1861572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (const Instruction *Inst = dyn_cast<Instruction>(V)) 1862572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (L->contains(Inst)) continue; 1863572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (Value::use_const_iterator UI = V->use_begin(), UE = V->use_end(); 1864572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman UI != UE; ++UI) { 1865572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const Instruction *UserInst = dyn_cast<Instruction>(*UI); 1866572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Ignore non-instructions. 1867572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!UserInst) 1868572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman continue; 1869572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Ignore instructions in other functions (as can happen with 1870572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Constants). 1871572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (UserInst->getParent()->getParent() != L->getHeader()->getParent()) 1872572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman continue; 1873572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Ignore instructions not dominated by the loop. 1874572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const BasicBlock *UseBB = !isa<PHINode>(UserInst) ? 1875572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman UserInst->getParent() : 1876572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman cast<PHINode>(UserInst)->getIncomingBlock( 1877572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman PHINode::getIncomingValueNumForOperand(UI.getOperandNo())); 1878572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!DT.dominates(L->getHeader(), UseBB)) 1879572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman continue; 1880572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Ignore uses which are part of other SCEV expressions, to avoid 1881572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // analyzing them multiple times. 1882572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (SE.isSCEVable(UserInst->getType()) && 1883572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman !isa<SCEVUnknown>(SE.getSCEV(const_cast<Instruction *>(UserInst)))) 1884572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman continue; 1885572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Ignore icmp instructions which are already being analyzed. 1886572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (const ICmpInst *ICI = dyn_cast<ICmpInst>(UserInst)) { 1887572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman unsigned OtherIdx = !UI.getOperandNo(); 1888572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Value *OtherOp = const_cast<Value *>(ICI->getOperand(OtherIdx)); 1889572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (SE.getSCEV(OtherOp)->hasComputableLoopEvolution(L)) 1890572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman continue; 1891572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 1892572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1893572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LSRFixup &LF = getNewFixup(); 1894572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LF.UserInst = const_cast<Instruction *>(UserInst); 1895572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LF.OperandValToReplace = UI.getUse(); 1896572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman std::pair<size_t, int64_t> P = getUse(S, LSRUse::Basic, 0); 1897572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LF.LUIdx = P.first; 1898572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LF.Offset = P.second; 1899572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LSRUse &LU = Uses[LF.LUIdx]; 1900572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LU.AllFixupsOutsideLoop &= L->contains(LF.UserInst); 1901572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman InsertSupplementalFormula(U, LU, LF.LUIdx); 1902572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman CountRegisters(LU.Formulae.back(), Uses.size() - 1); 1903572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman break; 1904572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 1905572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 1906572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 1907572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 1908572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1909572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// CollectSubexprs - Split S into subexpressions which can be pulled out into 1910572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// separate registers. If C is non-null, multiply each subexpression by C. 1911572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanstatic void CollectSubexprs(const SCEV *S, const SCEVConstant *C, 1912572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallVectorImpl<const SCEV *> &Ops, 1913572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman ScalarEvolution &SE) { 1914572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) { 1915572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Break out add operands. 1916572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (SCEVAddExpr::op_iterator I = Add->op_begin(), E = Add->op_end(); 1917572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman I != E; ++I) 1918572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman CollectSubexprs(*I, C, Ops, SE); 1919572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return; 1920572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } else if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) { 1921572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Split a non-zero base out of an addrec. 1922572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!AR->getStart()->isZero()) { 1923572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman CollectSubexprs(SE.getAddRecExpr(SE.getIntegerSCEV(0, AR->getType()), 1924572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman AR->getStepRecurrence(SE), 1925572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman AR->getLoop()), C, Ops, SE); 192668d6da1f33be3f0a55f7fa87204069cfa79247c6Dan Gohman CollectSubexprs(AR->getStart(), C, Ops, SE); 1927572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return; 1928572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 1929572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } else if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S)) { 1930572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Break (C * (a + b + c)) into C*a + C*b + C*c. 1931572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (Mul->getNumOperands() == 2) 1932572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (const SCEVConstant *Op0 = 1933572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman dyn_cast<SCEVConstant>(Mul->getOperand(0))) { 1934572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman CollectSubexprs(Mul->getOperand(1), 1935572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman C ? cast<SCEVConstant>(SE.getMulExpr(C, Op0)) : Op0, 1936572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Ops, SE); 1937572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return; 1938572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 1939572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 1940572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1941572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Otherwise use the value itself. 1942572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Ops.push_back(C ? SE.getMulExpr(C, S) : S); 1943572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 1944572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1945572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// GenerateReassociations - Split out subexpressions from adds and the bases of 1946572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// addrecs. 1947572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanvoid LSRInstance::GenerateReassociations(LSRUse &LU, unsigned LUIdx, 1948572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Formula Base, 1949572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman unsigned Depth) { 1950572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Arbitrarily cap recursion to protect compile time. 1951572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (Depth >= 3) return; 1952572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1953572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (size_t i = 0, e = Base.BaseRegs.size(); i != e; ++i) { 1954572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const SCEV *BaseReg = Base.BaseRegs[i]; 1955572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1956572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallVector<const SCEV *, 8> AddOps; 1957572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman CollectSubexprs(BaseReg, 0, AddOps, SE); 1958572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (AddOps.size() == 1) continue; 1959572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1960572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (SmallVectorImpl<const SCEV *>::const_iterator J = AddOps.begin(), 1961572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman JE = AddOps.end(); J != JE; ++J) { 1962572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Don't pull a constant into a register if the constant could be folded 1963572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // into an immediate field. 1964572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (isAlwaysFoldable(*J, LU.MinOffset, LU.MaxOffset, 1965572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Base.getNumRegs() > 1, 1966572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LU.Kind, LU.AccessTy, TLI, SE)) 1967572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman continue; 1968572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1969572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Collect all operands except *J. 1970572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallVector<const SCEV *, 8> InnerAddOps; 1971572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (SmallVectorImpl<const SCEV *>::const_iterator K = AddOps.begin(), 1972572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman KE = AddOps.end(); K != KE; ++K) 1973572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (K != J) 1974572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman InnerAddOps.push_back(*K); 1975572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1976572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Don't leave just a constant behind in a register if the constant could 1977572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // be folded into an immediate field. 1978572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (InnerAddOps.size() == 1 && 1979572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman isAlwaysFoldable(InnerAddOps[0], LU.MinOffset, LU.MaxOffset, 1980572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Base.getNumRegs() > 1, 1981572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LU.Kind, LU.AccessTy, TLI, SE)) 1982572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman continue; 1983572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1984572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Formula F = Base; 1985572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman F.BaseRegs[i] = SE.getAddExpr(InnerAddOps); 1986572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman F.BaseRegs.push_back(*J); 1987572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (InsertFormula(LU, LUIdx, F)) 1988572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // If that formula hadn't been seen before, recurse to find more like 1989572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // it. 1990572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman GenerateReassociations(LU, LUIdx, LU.Formulae.back(), Depth+1); 1991572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 1992572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 1993572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 1994572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 1995572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// GenerateCombinations - Generate a formula consisting of all of the 1996572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// loop-dominating registers added into a single register. 1997572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanvoid LSRInstance::GenerateCombinations(LSRUse &LU, unsigned LUIdx, 1998441a38993e89104c424e00c95cce63c7351f4fc3Dan Gohman Formula Base) { 1999572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // This method is only intersting on a plurality of registers. 2000572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (Base.BaseRegs.size() <= 1) return; 2001572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2002572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Formula F = Base; 2003572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman F.BaseRegs.clear(); 2004572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallVector<const SCEV *, 4> Ops; 2005572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (SmallVectorImpl<const SCEV *>::const_iterator 2006572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman I = Base.BaseRegs.begin(), E = Base.BaseRegs.end(); I != E; ++I) { 2007572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const SCEV *BaseReg = *I; 2008572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (BaseReg->properlyDominates(L->getHeader(), &DT) && 2009572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman !BaseReg->hasComputableLoopEvolution(L)) 2010572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Ops.push_back(BaseReg); 2011572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman else 2012572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman F.BaseRegs.push_back(BaseReg); 2013572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2014572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (Ops.size() > 1) { 2015ce947366ec07ed3e9b017f0f4a07fa668a799119Dan Gohman const SCEV *Sum = SE.getAddExpr(Ops); 2016ce947366ec07ed3e9b017f0f4a07fa668a799119Dan Gohman // TODO: If Sum is zero, it probably means ScalarEvolution missed an 2017ce947366ec07ed3e9b017f0f4a07fa668a799119Dan Gohman // opportunity to fold something. For now, just ignore such cases 2018ce947366ec07ed3e9b017f0f4a07fa668a799119Dan Gohman // rather than procede with zero in a register. 2019ce947366ec07ed3e9b017f0f4a07fa668a799119Dan Gohman if (!Sum->isZero()) { 2020ce947366ec07ed3e9b017f0f4a07fa668a799119Dan Gohman F.BaseRegs.push_back(Sum); 2021ce947366ec07ed3e9b017f0f4a07fa668a799119Dan Gohman (void)InsertFormula(LU, LUIdx, F); 2022ce947366ec07ed3e9b017f0f4a07fa668a799119Dan Gohman } 2023572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2024572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 2025572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2026572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// GenerateSymbolicOffsets - Generate reuse formulae using symbolic offsets. 2027572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanvoid LSRInstance::GenerateSymbolicOffsets(LSRUse &LU, unsigned LUIdx, 2028572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Formula Base) { 2029572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // We can't add a symbolic offset if the address already contains one. 2030572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (Base.AM.BaseGV) return; 2031572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2032572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (size_t i = 0, e = Base.BaseRegs.size(); i != e; ++i) { 2033572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const SCEV *G = Base.BaseRegs[i]; 2034572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman GlobalValue *GV = ExtractSymbol(G, SE); 2035572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (G->isZero() || !GV) 2036572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman continue; 2037572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Formula F = Base; 2038572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman F.AM.BaseGV = GV; 2039572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!isLegalUse(F.AM, LU.MinOffset, LU.MaxOffset, 2040572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LU.Kind, LU.AccessTy, TLI)) 2041572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman continue; 2042572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman F.BaseRegs[i] = G; 2043572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman (void)InsertFormula(LU, LUIdx, F); 2044572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2045572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 2046572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2047572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// GenerateConstantOffsets - Generate reuse formulae using symbolic offsets. 2048572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanvoid LSRInstance::GenerateConstantOffsets(LSRUse &LU, unsigned LUIdx, 2049572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Formula Base) { 2050572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // TODO: For now, just add the min and max offset, because it usually isn't 2051572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // worthwhile looking at everything inbetween. 2052572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallVector<int64_t, 4> Worklist; 2053572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Worklist.push_back(LU.MinOffset); 2054572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (LU.MaxOffset != LU.MinOffset) 2055572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Worklist.push_back(LU.MaxOffset); 2056572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2057572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (size_t i = 0, e = Base.BaseRegs.size(); i != e; ++i) { 2058572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const SCEV *G = Base.BaseRegs[i]; 2059572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2060572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (SmallVectorImpl<int64_t>::const_iterator I = Worklist.begin(), 2061572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman E = Worklist.end(); I != E; ++I) { 2062572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Formula F = Base; 2063572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman F.AM.BaseOffs = (uint64_t)Base.AM.BaseOffs - *I; 2064572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (isLegalUse(F.AM, LU.MinOffset - *I, LU.MaxOffset - *I, 2065572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LU.Kind, LU.AccessTy, TLI)) { 2066572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman F.BaseRegs[i] = SE.getAddExpr(G, SE.getIntegerSCEV(*I, G->getType())); 2067572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2068572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman (void)InsertFormula(LU, LUIdx, F); 2069572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2070572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2071572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2072572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman int64_t Imm = ExtractImmediate(G, SE); 2073572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (G->isZero() || Imm == 0) 2074572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman continue; 2075572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Formula F = Base; 2076572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman F.AM.BaseOffs = (uint64_t)F.AM.BaseOffs + Imm; 2077572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!isLegalUse(F.AM, LU.MinOffset, LU.MaxOffset, 2078572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LU.Kind, LU.AccessTy, TLI)) 2079572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman continue; 2080572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman F.BaseRegs[i] = G; 2081572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman (void)InsertFormula(LU, LUIdx, F); 2082572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2083572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 2084572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2085572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// GenerateICmpZeroScales - For ICmpZero, check to see if we can scale up 2086572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// the comparison. For example, x == y -> x*c == y*c. 2087572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanvoid LSRInstance::GenerateICmpZeroScales(LSRUse &LU, unsigned LUIdx, 2088572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Formula Base) { 2089572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (LU.Kind != LSRUse::ICmpZero) return; 2090572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2091572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Determine the integer type for the base formula. 2092572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const Type *IntTy = Base.getType(); 2093572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!IntTy) return; 2094572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (SE.getTypeSizeInBits(IntTy) > 64) return; 2095572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2096572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Don't do this if there is more than one offset. 2097572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (LU.MinOffset != LU.MaxOffset) return; 2098572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2099572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman assert(!Base.AM.BaseGV && "ICmpZero use is not legal!"); 2100572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2101572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Check each interesting stride. 2102572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (SmallSetVector<int64_t, 8>::const_iterator 2103572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman I = Factors.begin(), E = Factors.end(); I != E; ++I) { 2104572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman int64_t Factor = *I; 2105572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Formula F = Base; 2106572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2107572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Check that the multiplication doesn't overflow. 2108572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman F.AM.BaseOffs = (uint64_t)Base.AM.BaseOffs * Factor; 2109572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if ((int64_t)F.AM.BaseOffs / Factor != Base.AM.BaseOffs) 2110572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman continue; 2111572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2112572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Check that multiplying with the use offset doesn't overflow. 2113572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman int64_t Offset = LU.MinOffset; 2114572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Offset = (uint64_t)Offset * Factor; 2115572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if ((int64_t)Offset / Factor != LU.MinOffset) 2116572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman continue; 2117572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2118572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Check that this scale is legal. 2119572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!isLegalUse(F.AM, Offset, Offset, LU.Kind, LU.AccessTy, TLI)) 2120572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman continue; 2121572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2122572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Compensate for the use having MinOffset built into it. 2123572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman F.AM.BaseOffs = (uint64_t)F.AM.BaseOffs + Offset - LU.MinOffset; 2124572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2125572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const SCEV *FactorS = SE.getIntegerSCEV(Factor, IntTy); 2126572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2127572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Check that multiplying with each base register doesn't overflow. 2128572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (size_t i = 0, e = F.BaseRegs.size(); i != e; ++i) { 2129572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman F.BaseRegs[i] = SE.getMulExpr(F.BaseRegs[i], FactorS); 2130572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (getSDiv(F.BaseRegs[i], FactorS, SE) != Base.BaseRegs[i]) 2131572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman goto next; 2132572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2133572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2134572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Check that multiplying with the scaled register doesn't overflow. 2135572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (F.ScaledReg) { 2136572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman F.ScaledReg = SE.getMulExpr(F.ScaledReg, FactorS); 2137572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (getSDiv(F.ScaledReg, FactorS, SE) != Base.ScaledReg) 2138572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman continue; 2139572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2140572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2141572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // If we make it here and it's legal, add it. 2142572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman (void)InsertFormula(LU, LUIdx, F); 2143572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman next:; 2144572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2145572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 2146572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2147572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// GenerateScales - Generate stride factor reuse formulae by making use of 2148572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// scaled-offset address modes, for example. 2149572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanvoid LSRInstance::GenerateScales(LSRUse &LU, unsigned LUIdx, 2150572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Formula Base) { 2151572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Determine the integer type for the base formula. 2152572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const Type *IntTy = Base.getType(); 2153572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!IntTy) return; 2154572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2155572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // If this Formula already has a scaled register, we can't add another one. 2156572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (Base.AM.Scale != 0) return; 2157572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2158572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Check each interesting stride. 2159572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (SmallSetVector<int64_t, 8>::const_iterator 2160572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman I = Factors.begin(), E = Factors.end(); I != E; ++I) { 2161572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman int64_t Factor = *I; 2162572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2163572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Base.AM.Scale = Factor; 2164572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Base.AM.HasBaseReg = Base.BaseRegs.size() > 1; 2165572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Check whether this scale is going to be legal. 2166572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!isLegalUse(Base.AM, LU.MinOffset, LU.MaxOffset, 2167572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LU.Kind, LU.AccessTy, TLI)) { 2168572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // As a special-case, handle special out-of-loop Basic users specially. 2169572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // TODO: Reconsider this special case. 2170572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (LU.Kind == LSRUse::Basic && 2171572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman isLegalUse(Base.AM, LU.MinOffset, LU.MaxOffset, 2172572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LSRUse::Special, LU.AccessTy, TLI) && 2173572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LU.AllFixupsOutsideLoop) 2174572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LU.Kind = LSRUse::Special; 2175572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman else 2176572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman continue; 2177572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2178572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // For an ICmpZero, negating a solitary base register won't lead to 2179572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // new solutions. 2180572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (LU.Kind == LSRUse::ICmpZero && 2181572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman !Base.AM.HasBaseReg && Base.AM.BaseOffs == 0 && !Base.AM.BaseGV) 2182572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman continue; 2183572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // For each addrec base reg, apply the scale, if possible. 2184572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (size_t i = 0, e = Base.BaseRegs.size(); i != e; ++i) 2185572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (const SCEVAddRecExpr *AR = 2186572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman dyn_cast<SCEVAddRecExpr>(Base.BaseRegs[i])) { 2187572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const SCEV *FactorS = SE.getIntegerSCEV(Factor, IntTy); 2188572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (FactorS->isZero()) 2189572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman continue; 2190572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Divide out the factor, ignoring high bits, since we'll be 2191572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // scaling the value back up in the end. 2192572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (const SCEV *Quotient = getSDiv(AR, FactorS, SE, true)) { 2193572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // TODO: This could be optimized to avoid all the copying. 2194572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Formula F = Base; 2195572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman F.ScaledReg = Quotient; 2196572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman std::swap(F.BaseRegs[i], F.BaseRegs.back()); 2197572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman F.BaseRegs.pop_back(); 2198572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman (void)InsertFormula(LU, LUIdx, F); 2199572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2200572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2201572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2202572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 2203572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2204572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// GenerateTruncates - Generate reuse formulae from different IV types. 2205572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanvoid LSRInstance::GenerateTruncates(LSRUse &LU, unsigned LUIdx, 2206572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Formula Base) { 2207572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // This requires TargetLowering to tell us which truncates are free. 2208572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!TLI) return; 2209572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2210572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Don't bother truncating symbolic values. 2211572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (Base.AM.BaseGV) return; 2212572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2213572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Determine the integer type for the base formula. 2214572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const Type *DstTy = Base.getType(); 2215572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!DstTy) return; 2216572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman DstTy = SE.getEffectiveSCEVType(DstTy); 2217572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2218572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (SmallSetVector<const Type *, 4>::const_iterator 2219572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman I = Types.begin(), E = Types.end(); I != E; ++I) { 2220572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const Type *SrcTy = *I; 2221572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (SrcTy != DstTy && TLI->isTruncateFree(SrcTy, DstTy)) { 2222572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Formula F = Base; 2223572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2224572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (F.ScaledReg) F.ScaledReg = SE.getAnyExtendExpr(F.ScaledReg, *I); 2225572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (SmallVectorImpl<const SCEV *>::iterator J = F.BaseRegs.begin(), 2226572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman JE = F.BaseRegs.end(); J != JE; ++J) 2227572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman *J = SE.getAnyExtendExpr(*J, SrcTy); 2228572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2229572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // TODO: This assumes we've done basic processing on all uses and 2230572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // have an idea what the register usage is. 2231572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!F.hasRegsUsedByUsesOtherThan(LUIdx, RegUses)) 2232572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman continue; 2233572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2234572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman (void)InsertFormula(LU, LUIdx, F); 2235572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2236572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2237572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 2238572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2239572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmannamespace { 2240572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 22416020d85c41987b0b7890d91bf66187aac6e8a3a1Dan Gohman/// WorkItem - Helper class for GenerateCrossUseConstantOffsets. It's used to 2242572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// defer modifications so that the search phase doesn't have to worry about 2243572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// the data structures moving underneath it. 2244572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanstruct WorkItem { 2245572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman size_t LUIdx; 2246572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman int64_t Imm; 2247572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const SCEV *OrigReg; 2248572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2249572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman WorkItem(size_t LI, int64_t I, const SCEV *R) 2250572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman : LUIdx(LI), Imm(I), OrigReg(R) {} 2251572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2252572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman void print(raw_ostream &OS) const; 2253572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman void dump() const; 2254572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman}; 2255572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2256572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 2257572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2258572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanvoid WorkItem::print(raw_ostream &OS) const { 2259572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman OS << "in formulae referencing " << *OrigReg << " in use " << LUIdx 2260572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman << " , add offset " << Imm; 2261572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 2262572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2263572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanvoid WorkItem::dump() const { 2264572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman print(errs()); errs() << '\n'; 2265572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 2266572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2267572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// GenerateCrossUseConstantOffsets - Look for registers which are a constant 2268572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// distance apart and try to form reuse opportunities between them. 2269572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanvoid LSRInstance::GenerateCrossUseConstantOffsets() { 2270572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Group the registers by their value without any added constant offset. 2271572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman typedef std::map<int64_t, const SCEV *> ImmMapTy; 2272572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman typedef DenseMap<const SCEV *, ImmMapTy> RegMapTy; 2273572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman RegMapTy Map; 2274572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman DenseMap<const SCEV *, SmallBitVector> UsedByIndicesMap; 2275572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallVector<const SCEV *, 8> Sequence; 2276572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (RegUseTracker::const_iterator I = RegUses.begin(), E = RegUses.end(); 2277572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman I != E; ++I) { 2278572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const SCEV *Reg = *I; 2279572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman int64_t Imm = ExtractImmediate(Reg, SE); 2280572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman std::pair<RegMapTy::iterator, bool> Pair = 2281572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Map.insert(std::make_pair(Reg, ImmMapTy())); 2282572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (Pair.second) 2283572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Sequence.push_back(Reg); 2284572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Pair.first->second.insert(std::make_pair(Imm, *I)); 2285572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman UsedByIndicesMap[Reg] |= RegUses.getUsedByIndices(*I); 2286572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2287572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2288572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Now examine each set of registers with the same base value. Build up 2289572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // a list of work to do and do the work in a separate step so that we're 2290572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // not adding formulae and register counts while we're searching. 2291572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallVector<WorkItem, 32> WorkItems; 2292572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallSet<std::pair<size_t, int64_t>, 32> UniqueItems; 2293572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (SmallVectorImpl<const SCEV *>::const_iterator I = Sequence.begin(), 2294572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman E = Sequence.end(); I != E; ++I) { 2295572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const SCEV *Reg = *I; 2296572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const ImmMapTy &Imms = Map.find(Reg)->second; 2297572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2298cd045c08cad9bc3e1e3e234453f5f4464b705e02Dan Gohman // It's not worthwhile looking for reuse if there's only one offset. 2299cd045c08cad9bc3e1e3e234453f5f4464b705e02Dan Gohman if (Imms.size() == 1) 2300cd045c08cad9bc3e1e3e234453f5f4464b705e02Dan Gohman continue; 2301cd045c08cad9bc3e1e3e234453f5f4464b705e02Dan Gohman 2302572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman DEBUG(dbgs() << "Generating cross-use offsets for " << *Reg << ':'; 2303572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (ImmMapTy::const_iterator J = Imms.begin(), JE = Imms.end(); 2304572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman J != JE; ++J) 2305572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman dbgs() << ' ' << J->first; 2306572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman dbgs() << '\n'); 2307572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2308572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Examine each offset. 2309572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (ImmMapTy::const_iterator J = Imms.begin(), JE = Imms.end(); 2310572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman J != JE; ++J) { 2311572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const SCEV *OrigReg = J->second; 2312572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2313572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman int64_t JImm = J->first; 2314572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const SmallBitVector &UsedByIndices = RegUses.getUsedByIndices(OrigReg); 2315572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2316572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!isa<SCEVConstant>(OrigReg) && 2317572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman UsedByIndicesMap[Reg].count() == 1) { 2318572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman DEBUG(dbgs() << "Skipping cross-use reuse for " << *OrigReg << '\n'); 2319572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman continue; 2320572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2321572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2322572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Conservatively examine offsets between this orig reg a few selected 2323572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // other orig regs. 2324572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman ImmMapTy::const_iterator OtherImms[] = { 2325572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Imms.begin(), prior(Imms.end()), 2326572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Imms.upper_bound((Imms.begin()->first + prior(Imms.end())->first) / 2) 2327572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman }; 2328572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (size_t i = 0, e = array_lengthof(OtherImms); i != e; ++i) { 2329572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman ImmMapTy::const_iterator M = OtherImms[i]; 2330cd045c08cad9bc3e1e3e234453f5f4464b705e02Dan Gohman if (M == J || M == JE) continue; 2331572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2332572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Compute the difference between the two. 2333572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman int64_t Imm = (uint64_t)JImm - M->first; 2334572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (int LUIdx = UsedByIndices.find_first(); LUIdx != -1; 2335572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LUIdx = UsedByIndices.find_next(LUIdx)) 2336572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Make a memo of this use, offset, and register tuple. 2337572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (UniqueItems.insert(std::make_pair(LUIdx, Imm))) 2338572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman WorkItems.push_back(WorkItem(LUIdx, Imm, OrigReg)); 2339572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2340572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2341572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2342572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2343572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Map.clear(); 2344572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Sequence.clear(); 2345572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman UsedByIndicesMap.clear(); 2346572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman UniqueItems.clear(); 2347572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2348572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Now iterate through the worklist and add new formulae. 2349572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (SmallVectorImpl<WorkItem>::const_iterator I = WorkItems.begin(), 2350572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman E = WorkItems.end(); I != E; ++I) { 2351572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const WorkItem &WI = *I; 2352572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman size_t LUIdx = WI.LUIdx; 2353572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LSRUse &LU = Uses[LUIdx]; 2354572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman int64_t Imm = WI.Imm; 2355572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const SCEV *OrigReg = WI.OrigReg; 2356572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2357572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const Type *IntTy = SE.getEffectiveSCEVType(OrigReg->getType()); 2358572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const SCEV *NegImmS = SE.getSCEV(ConstantInt::get(IntTy, -(uint64_t)Imm)); 2359572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman unsigned BitWidth = SE.getTypeSizeInBits(IntTy); 2360572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2361572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // TODO: Use a more targetted data structure. 2362572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (size_t L = 0, LE = LU.Formulae.size(); L != LE; ++L) { 2363572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Formula F = LU.Formulae[L]; 2364572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Use the immediate in the scaled register. 2365572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (F.ScaledReg == OrigReg) { 2366572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman int64_t Offs = (uint64_t)F.AM.BaseOffs + 2367572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Imm * (uint64_t)F.AM.Scale; 2368572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Don't create 50 + reg(-50). 2369572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (F.referencesReg(SE.getSCEV( 2370572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman ConstantInt::get(IntTy, -(uint64_t)Offs)))) 2371572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman continue; 2372572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Formula NewF = F; 2373572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman NewF.AM.BaseOffs = Offs; 2374572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!isLegalUse(NewF.AM, LU.MinOffset, LU.MaxOffset, 2375572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LU.Kind, LU.AccessTy, TLI)) 2376572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman continue; 2377572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman NewF.ScaledReg = SE.getAddExpr(NegImmS, NewF.ScaledReg); 2378572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2379572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // If the new scale is a constant in a register, and adding the constant 2380572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // value to the immediate would produce a value closer to zero than the 2381572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // immediate itself, then the formula isn't worthwhile. 2382572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (const SCEVConstant *C = dyn_cast<SCEVConstant>(NewF.ScaledReg)) 2383572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (C->getValue()->getValue().isNegative() != 2384572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman (NewF.AM.BaseOffs < 0) && 2385572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman (C->getValue()->getValue().abs() * APInt(BitWidth, F.AM.Scale)) 2386572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman .ule(APInt(BitWidth, NewF.AM.BaseOffs).abs())) 2387572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman continue; 2388572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2389572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // OK, looks good. 2390572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman (void)InsertFormula(LU, LUIdx, NewF); 2391572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } else { 2392572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Use the immediate in a base register. 2393572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (size_t N = 0, NE = F.BaseRegs.size(); N != NE; ++N) { 2394572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const SCEV *BaseReg = F.BaseRegs[N]; 2395572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (BaseReg != OrigReg) 2396572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman continue; 2397572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Formula NewF = F; 2398572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman NewF.AM.BaseOffs = (uint64_t)NewF.AM.BaseOffs + Imm; 2399572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!isLegalUse(NewF.AM, LU.MinOffset, LU.MaxOffset, 2400572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LU.Kind, LU.AccessTy, TLI)) 2401572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman continue; 2402572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman NewF.BaseRegs[N] = SE.getAddExpr(NegImmS, BaseReg); 2403572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2404572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // If the new formula has a constant in a register, and adding the 2405572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // constant value to the immediate would produce a value closer to 2406572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // zero than the immediate itself, then the formula isn't worthwhile. 2407572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (SmallVectorImpl<const SCEV *>::const_iterator 2408572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman J = NewF.BaseRegs.begin(), JE = NewF.BaseRegs.end(); 2409572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman J != JE; ++J) 2410572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (const SCEVConstant *C = dyn_cast<SCEVConstant>(*J)) 2411572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (C->getValue()->getValue().isNegative() != 2412572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman (NewF.AM.BaseOffs < 0) && 2413572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman C->getValue()->getValue().abs() 2414572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman .ule(APInt(BitWidth, NewF.AM.BaseOffs).abs())) 2415572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman goto skip_formula; 2416572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2417572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Ok, looks good. 2418572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman (void)InsertFormula(LU, LUIdx, NewF); 2419572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman break; 2420572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman skip_formula:; 2421572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2422572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2423572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2424572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2425572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 2426572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2427572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// GenerateAllReuseFormulae - Generate formulae for each use. 2428572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanvoid 2429572645cf84060c0fc25cb91d38cb9079918b3a88Dan GohmanLSRInstance::GenerateAllReuseFormulae() { 2430572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // This is split into two loops so that hasRegsUsedByUsesOtherThan 2431572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // queries are more precise. 2432572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) { 2433572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LSRUse &LU = Uses[LUIdx]; 2434572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (size_t i = 0, f = LU.Formulae.size(); i != f; ++i) 2435572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman GenerateReassociations(LU, LUIdx, LU.Formulae[i]); 2436572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (size_t i = 0, f = LU.Formulae.size(); i != f; ++i) 2437572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman GenerateCombinations(LU, LUIdx, LU.Formulae[i]); 2438572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2439572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) { 2440572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LSRUse &LU = Uses[LUIdx]; 2441572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (size_t i = 0, f = LU.Formulae.size(); i != f; ++i) 2442572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman GenerateSymbolicOffsets(LU, LUIdx, LU.Formulae[i]); 2443572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (size_t i = 0, f = LU.Formulae.size(); i != f; ++i) 2444572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman GenerateConstantOffsets(LU, LUIdx, LU.Formulae[i]); 2445572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (size_t i = 0, f = LU.Formulae.size(); i != f; ++i) 2446572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman GenerateICmpZeroScales(LU, LUIdx, LU.Formulae[i]); 2447572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (size_t i = 0, f = LU.Formulae.size(); i != f; ++i) 2448572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman GenerateScales(LU, LUIdx, LU.Formulae[i]); 2449572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (size_t i = 0, f = LU.Formulae.size(); i != f; ++i) 2450572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman GenerateTruncates(LU, LUIdx, LU.Formulae[i]); 2451572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2452572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2453572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman GenerateCrossUseConstantOffsets(); 2454572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 2455572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2456572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// If their are multiple formulae with the same set of registers used 2457572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// by other uses, pick the best one and delete the others. 2458572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanvoid LSRInstance::FilterOutUndesirableDedicatedRegisters() { 2459572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman#ifndef NDEBUG 2460572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman bool Changed = false; 2461572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman#endif 2462572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2463572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Collect the best formula for each unique set of shared registers. This 2464572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // is reset for each use. 2465572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman typedef DenseMap<SmallVector<const SCEV *, 2>, size_t, UniquifierDenseMapInfo> 2466572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman BestFormulaeTy; 2467572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman BestFormulaeTy BestFormulae; 2468572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2469572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) { 2470572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LSRUse &LU = Uses[LUIdx]; 2471572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman FormulaSorter Sorter(L, LU, SE, DT); 2472572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2473572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Clear out the set of used regs; it will be recomputed. 2474572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LU.Regs.clear(); 2475572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2476572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (size_t FIdx = 0, NumForms = LU.Formulae.size(); 2477572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman FIdx != NumForms; ++FIdx) { 2478572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Formula &F = LU.Formulae[FIdx]; 2479572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2480572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallVector<const SCEV *, 2> Key; 2481572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (SmallVectorImpl<const SCEV *>::const_iterator J = F.BaseRegs.begin(), 2482572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman JE = F.BaseRegs.end(); J != JE; ++J) { 2483572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const SCEV *Reg = *J; 2484572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (RegUses.isRegUsedByUsesOtherThan(Reg, LUIdx)) 2485572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Key.push_back(Reg); 2486572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2487572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (F.ScaledReg && 2488572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman RegUses.isRegUsedByUsesOtherThan(F.ScaledReg, LUIdx)) 2489572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Key.push_back(F.ScaledReg); 2490572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Unstable sort by host order ok, because this is only used for 2491572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // uniquifying. 2492572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman std::sort(Key.begin(), Key.end()); 2493572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2494572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman std::pair<BestFormulaeTy::const_iterator, bool> P = 2495572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman BestFormulae.insert(std::make_pair(Key, FIdx)); 2496572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!P.second) { 2497572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Formula &Best = LU.Formulae[P.first->second]; 2498572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (Sorter.operator()(F, Best)) 2499572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman std::swap(F, Best); 2500572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman DEBUG(dbgs() << "Filtering out "; F.print(dbgs()); 2501572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman dbgs() << "\n" 2502572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman " in favor of "; Best.print(dbgs()); 2503572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman dbgs() << '\n'); 2504572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman#ifndef NDEBUG 2505572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Changed = true; 2506572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman#endif 2507572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman std::swap(F, LU.Formulae.back()); 2508572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LU.Formulae.pop_back(); 2509572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman --FIdx; 2510572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman --NumForms; 2511572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman continue; 2512572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2513572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (F.ScaledReg) LU.Regs.insert(F.ScaledReg); 2514572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LU.Regs.insert(F.BaseRegs.begin(), F.BaseRegs.end()); 2515572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2516572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman BestFormulae.clear(); 2517572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2518572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2519572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman DEBUG(if (Changed) { 25209214b82c5446791b280821bbd892dca633130f80Dan Gohman dbgs() << "\n" 25219214b82c5446791b280821bbd892dca633130f80Dan Gohman "After filtering out undesirable candidates:\n"; 2522572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman print_uses(dbgs()); 2523572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman }); 2524572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 2525572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2526572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// NarrowSearchSpaceUsingHeuristics - If there are an extrordinary number of 2527572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// formulae to choose from, use some rough heuristics to prune down the number 2528572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// of formulae. This keeps the main solver from taking an extrordinary amount 2529572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// of time in some worst-case scenarios. 2530572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanvoid LSRInstance::NarrowSearchSpaceUsingHeuristics() { 2531572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // This is a rough guess that seems to work fairly well. 2532572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const size_t Limit = UINT16_MAX; 2533572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2534572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallPtrSet<const SCEV *, 4> Taken; 2535572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (;;) { 2536572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Estimate the worst-case number of solutions we might consider. We almost 2537572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // never consider this many solutions because we prune the search space, 2538572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // but the pruning isn't always sufficient. 2539572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman uint32_t Power = 1; 2540572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (SmallVectorImpl<LSRUse>::const_iterator I = Uses.begin(), 2541572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman E = Uses.end(); I != E; ++I) { 2542572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman size_t FSize = I->Formulae.size(); 2543572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (FSize >= Limit) { 2544572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Power = Limit; 2545572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman break; 2546572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2547572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Power *= FSize; 2548572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (Power >= Limit) 2549572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman break; 2550572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2551572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (Power < Limit) 2552572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman break; 2553572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2554572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Ok, we have too many of formulae on our hands to conveniently handle. 2555572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Use a rough heuristic to thin out the list. 2556572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2557572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Pick the register which is used by the most LSRUses, which is likely 2558572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // to be a good reuse register candidate. 2559572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const SCEV *Best = 0; 2560572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman unsigned BestNum = 0; 2561572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (RegUseTracker::const_iterator I = RegUses.begin(), E = RegUses.end(); 2562572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman I != E; ++I) { 2563572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const SCEV *Reg = *I; 2564572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (Taken.count(Reg)) 2565572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman continue; 2566572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!Best) 2567572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Best = Reg; 2568572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman else { 2569572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman unsigned Count = RegUses.getUsedByIndices(Reg).count(); 2570572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (Count > BestNum) { 2571572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Best = Reg; 2572572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman BestNum = Count; 2573572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2574572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2575572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2576572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2577572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman DEBUG(dbgs() << "Narrowing the search space by assuming " << *Best 2578572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman << " will yeild profitable reuse.\n"); 2579572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Taken.insert(Best); 2580572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2581572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // In any use with formulae which references this register, delete formulae 2582572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // which don't reference it. 2583572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (SmallVectorImpl<LSRUse>::iterator I = Uses.begin(), 2584572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman E = Uses.end(); I != E; ++I) { 2585572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LSRUse &LU = *I; 2586572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!LU.Regs.count(Best)) continue; 2587572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2588572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Clear out the set of used regs; it will be recomputed. 2589572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LU.Regs.clear(); 2590572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2591572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (size_t i = 0, e = LU.Formulae.size(); i != e; ++i) { 2592572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Formula &F = LU.Formulae[i]; 2593572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!F.referencesReg(Best)) { 2594572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman DEBUG(dbgs() << " Deleting "; F.print(dbgs()); dbgs() << '\n'); 2595572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman std::swap(LU.Formulae.back(), F); 2596572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LU.Formulae.pop_back(); 2597572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman --e; 2598572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman --i; 2599572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman continue; 2600572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2601572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2602572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (F.ScaledReg) LU.Regs.insert(F.ScaledReg); 2603572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LU.Regs.insert(F.BaseRegs.begin(), F.BaseRegs.end()); 2604572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2605572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2606572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2607572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman DEBUG(dbgs() << "After pre-selection:\n"; 2608572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman print_uses(dbgs())); 2609572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2610572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 2611572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2612572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// SolveRecurse - This is the recursive solver. 2613572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanvoid LSRInstance::SolveRecurse(SmallVectorImpl<const Formula *> &Solution, 2614572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Cost &SolutionCost, 2615572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallVectorImpl<const Formula *> &Workspace, 2616572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const Cost &CurCost, 2617572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const SmallPtrSet<const SCEV *, 16> &CurRegs, 2618572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman DenseSet<const SCEV *> &VisitedRegs) const { 2619572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Some ideas: 2620572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // - prune more: 2621572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // - use more aggressive filtering 2622572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // - sort the formula so that the most profitable solutions are found first 2623572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // - sort the uses too 2624572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // - search faster: 2625572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // - dont compute a cost, and then compare. compare while computing a cost 2626572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // and bail early. 2627572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // - track register sets with SmallBitVector 2628572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2629572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const LSRUse &LU = Uses[Workspace.size()]; 2630572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2631572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // If this use references any register that's already a part of the 2632572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // in-progress solution, consider it a requirement that a formula must 2633572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // reference that register in order to be considered. This prunes out 2634572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // unprofitable searching. 2635572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallSetVector<const SCEV *, 4> ReqRegs; 2636572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (SmallPtrSet<const SCEV *, 16>::const_iterator I = CurRegs.begin(), 2637572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman E = CurRegs.end(); I != E; ++I) 26389214b82c5446791b280821bbd892dca633130f80Dan Gohman if (LU.Regs.count(*I)) 2639572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman ReqRegs.insert(*I); 2640572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 26419214b82c5446791b280821bbd892dca633130f80Dan Gohman bool AnySatisfiedReqRegs = false; 2642572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallPtrSet<const SCEV *, 16> NewRegs; 2643572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Cost NewCost; 26449214b82c5446791b280821bbd892dca633130f80Dan Gohmanretry: 2645572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (SmallVectorImpl<Formula>::const_iterator I = LU.Formulae.begin(), 2646572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman E = LU.Formulae.end(); I != E; ++I) { 2647572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const Formula &F = *I; 2648572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2649572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Ignore formulae which do not use any of the required registers. 2650572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (SmallSetVector<const SCEV *, 4>::const_iterator J = ReqRegs.begin(), 2651572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman JE = ReqRegs.end(); J != JE; ++J) { 2652572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const SCEV *Reg = *J; 2653572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if ((!F.ScaledReg || F.ScaledReg != Reg) && 2654572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman std::find(F.BaseRegs.begin(), F.BaseRegs.end(), Reg) == 2655572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman F.BaseRegs.end()) 2656572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman goto skip; 2657572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 26589214b82c5446791b280821bbd892dca633130f80Dan Gohman AnySatisfiedReqRegs = true; 2659572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2660572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Evaluate the cost of the current formula. If it's already worse than 2661572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // the current best, prune the search at that point. 2662572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman NewCost = CurCost; 2663572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman NewRegs = CurRegs; 2664572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman NewCost.RateFormula(F, NewRegs, VisitedRegs, L, LU.Offsets, SE, DT); 2665572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (NewCost < SolutionCost) { 2666572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Workspace.push_back(&F); 2667572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (Workspace.size() != Uses.size()) { 2668572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SolveRecurse(Solution, SolutionCost, Workspace, NewCost, 2669572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman NewRegs, VisitedRegs); 2670572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (F.getNumRegs() == 1 && Workspace.size() == 1) 2671572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman VisitedRegs.insert(F.ScaledReg ? F.ScaledReg : F.BaseRegs[0]); 2672572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } else { 2673572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman DEBUG(dbgs() << "New best at "; NewCost.print(dbgs()); 2674572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman dbgs() << ". Regs:"; 2675572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (SmallPtrSet<const SCEV *, 16>::const_iterator 2676572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman I = NewRegs.begin(), E = NewRegs.end(); I != E; ++I) 2677572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman dbgs() << ' ' << **I; 2678572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman dbgs() << '\n'); 2679572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2680572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SolutionCost = NewCost; 2681572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Solution = Workspace; 2682572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2683572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Workspace.pop_back(); 2684572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2685572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman skip:; 2686572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 26879214b82c5446791b280821bbd892dca633130f80Dan Gohman 26889214b82c5446791b280821bbd892dca633130f80Dan Gohman // If none of the formulae had all of the required registers, relax the 26899214b82c5446791b280821bbd892dca633130f80Dan Gohman // constraint so that we don't exclude all formulae. 26909214b82c5446791b280821bbd892dca633130f80Dan Gohman if (!AnySatisfiedReqRegs) { 26919214b82c5446791b280821bbd892dca633130f80Dan Gohman ReqRegs.clear(); 26929214b82c5446791b280821bbd892dca633130f80Dan Gohman goto retry; 26939214b82c5446791b280821bbd892dca633130f80Dan Gohman } 2694572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 2695572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2696572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanvoid LSRInstance::Solve(SmallVectorImpl<const Formula *> &Solution) const { 2697572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallVector<const Formula *, 8> Workspace; 2698572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Cost SolutionCost; 2699572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SolutionCost.Loose(); 2700572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Cost CurCost; 2701572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallPtrSet<const SCEV *, 16> CurRegs; 2702572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman DenseSet<const SCEV *> VisitedRegs; 2703572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Workspace.reserve(Uses.size()); 2704572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2705572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SolveRecurse(Solution, SolutionCost, Workspace, CurCost, 2706572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman CurRegs, VisitedRegs); 2707572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2708572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Ok, we've now made all our decisions. 2709572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman DEBUG(dbgs() << "\n" 2710572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman "The chosen solution requires "; SolutionCost.print(dbgs()); 2711572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman dbgs() << ":\n"; 2712572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (size_t i = 0, e = Uses.size(); i != e; ++i) { 2713572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman dbgs() << " "; 2714572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Uses[i].print(dbgs()); 2715572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman dbgs() << "\n" 2716572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman " "; 2717572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Solution[i]->print(dbgs()); 2718572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman dbgs() << '\n'; 2719572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman }); 2720572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 2721572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2722572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// getImmediateDominator - A handy utility for the specific DominatorTree 2723572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// query that we need here. 2724572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// 2725572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanstatic BasicBlock *getImmediateDominator(BasicBlock *BB, DominatorTree &DT) { 2726572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman DomTreeNode *Node = DT.getNode(BB); 2727572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!Node) return 0; 2728572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Node = Node->getIDom(); 2729572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!Node) return 0; 2730572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return Node->getBlock(); 2731572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 2732572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2733572645cf84060c0fc25cb91d38cb9079918b3a88Dan GohmanValue *LSRInstance::Expand(const LSRFixup &LF, 2734572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const Formula &F, 2735572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman BasicBlock::iterator IP, 2736572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Loop *L, Instruction *IVIncInsertPos, 2737572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SCEVExpander &Rewriter, 2738572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallVectorImpl<WeakVH> &DeadInsts, 2739572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman ScalarEvolution &SE, DominatorTree &DT) const { 2740572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const LSRUse &LU = Uses[LF.LUIdx]; 2741572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2742572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Then, collect some instructions which we will remain dominated by when 2743572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // expanding the replacement. These must be dominated by any operands that 2744572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // will be required in the expansion. 2745572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallVector<Instruction *, 4> Inputs; 2746572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (Instruction *I = dyn_cast<Instruction>(LF.OperandValToReplace)) 2747572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Inputs.push_back(I); 2748572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (LU.Kind == LSRUse::ICmpZero) 2749572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (Instruction *I = 2750572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman dyn_cast<Instruction>(cast<ICmpInst>(LF.UserInst)->getOperand(1))) 2751572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Inputs.push_back(I); 2752572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (LF.PostIncLoop && !L->contains(LF.UserInst)) 2753572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Inputs.push_back(L->getLoopLatch()->getTerminator()); 2754572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2755572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Then, climb up the immediate dominator tree as far as we can go while 2756572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // still being dominated by the input positions. 2757572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (;;) { 2758572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman bool AllDominate = true; 2759572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Instruction *BetterPos = 0; 2760572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman BasicBlock *IDom = getImmediateDominator(IP->getParent(), DT); 2761572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!IDom) break; 2762572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Instruction *Tentative = IDom->getTerminator(); 2763572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (SmallVectorImpl<Instruction *>::const_iterator I = Inputs.begin(), 2764572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman E = Inputs.end(); I != E; ++I) { 2765572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Instruction *Inst = *I; 2766572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (Inst == Tentative || !DT.dominates(Inst, Tentative)) { 2767572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman AllDominate = false; 2768572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman break; 2769572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2770572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (IDom == Inst->getParent() && 2771572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman (!BetterPos || DT.dominates(BetterPos, Inst))) 2772572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman BetterPos = next(BasicBlock::iterator(Inst)); 2773572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2774572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!AllDominate) 2775572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman break; 2776572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (BetterPos) 2777572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman IP = BetterPos; 2778572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman else 2779572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman IP = Tentative; 2780572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2781572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman while (isa<PHINode>(IP)) ++IP; 2782572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2783572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Inform the Rewriter if we have a post-increment use, so that it can 2784572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // perform an advantageous expansion. 2785572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Rewriter.setPostInc(LF.PostIncLoop); 2786572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2787572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // This is the type that the user actually needs. 2788572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const Type *OpTy = LF.OperandValToReplace->getType(); 2789572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // This will be the type that we'll initially expand to. 2790572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const Type *Ty = F.getType(); 2791572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!Ty) 2792572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // No type known; just expand directly to the ultimate type. 2793572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Ty = OpTy; 2794572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman else if (SE.getEffectiveSCEVType(Ty) == SE.getEffectiveSCEVType(OpTy)) 2795572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Expand directly to the ultimate type if it's the right size. 2796572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Ty = OpTy; 2797572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // This is the type to do integer arithmetic in. 2798572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const Type *IntTy = SE.getEffectiveSCEVType(Ty); 2799572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2800572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Build up a list of operands to add together to form the full base. 2801572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallVector<const SCEV *, 8> Ops; 2802572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2803572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Expand the BaseRegs portion. 2804572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (SmallVectorImpl<const SCEV *>::const_iterator I = F.BaseRegs.begin(), 2805572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman E = F.BaseRegs.end(); I != E; ++I) { 2806572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const SCEV *Reg = *I; 2807572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman assert(!Reg->isZero() && "Zero allocated in a base register!"); 2808572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2809572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // If we're expanding for a post-inc user for the add-rec's loop, make the 2810572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // post-inc adjustment. 2811572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const SCEV *Start = Reg; 2812572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman while (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Start)) { 2813572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (AR->getLoop() == LF.PostIncLoop) { 2814572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Reg = SE.getAddExpr(Reg, AR->getStepRecurrence(SE)); 2815572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // If the user is inside the loop, insert the code after the increment 2816572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // so that it is dominated by its operand. 2817572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (L->contains(LF.UserInst)) 2818572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman IP = IVIncInsertPos; 2819572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman break; 2820572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2821572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Start = AR->getStart(); 2822572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2823572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2824572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Ops.push_back(SE.getUnknown(Rewriter.expandCodeFor(Reg, 0, IP))); 2825572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2826572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2827572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Expand the ScaledReg portion. 2828572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Value *ICmpScaledV = 0; 2829572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (F.AM.Scale != 0) { 2830572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const SCEV *ScaledS = F.ScaledReg; 2831572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2832572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // If we're expanding for a post-inc user for the add-rec's loop, make the 2833572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // post-inc adjustment. 2834572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(ScaledS)) 2835572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (AR->getLoop() == LF.PostIncLoop) 2836572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman ScaledS = SE.getAddExpr(ScaledS, AR->getStepRecurrence(SE)); 2837572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2838572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (LU.Kind == LSRUse::ICmpZero) { 2839572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // An interesting way of "folding" with an icmp is to use a negated 2840572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // scale, which we'll implement by inserting it into the other operand 2841572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // of the icmp. 2842572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman assert(F.AM.Scale == -1 && 2843572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman "The only scale supported by ICmpZero uses is -1!"); 2844572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman ICmpScaledV = Rewriter.expandCodeFor(ScaledS, 0, IP); 2845572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } else { 2846572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Otherwise just expand the scaled register and an explicit scale, 2847572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // which is expected to be matched as part of the address. 2848572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman ScaledS = SE.getUnknown(Rewriter.expandCodeFor(ScaledS, 0, IP)); 2849572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman ScaledS = SE.getMulExpr(ScaledS, 2850572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SE.getIntegerSCEV(F.AM.Scale, 2851572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman ScaledS->getType())); 2852572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Ops.push_back(ScaledS); 2853572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2854572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2855572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2856572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Expand the immediate portions. 2857572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (F.AM.BaseGV) 2858572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Ops.push_back(SE.getSCEV(F.AM.BaseGV)); 2859572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman int64_t Offset = (uint64_t)F.AM.BaseOffs + LF.Offset; 2860572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (Offset != 0) { 2861572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (LU.Kind == LSRUse::ICmpZero) { 2862572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // The other interesting way of "folding" with an ICmpZero is to use a 2863572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // negated immediate. 2864572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!ICmpScaledV) 2865572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman ICmpScaledV = ConstantInt::get(IntTy, -Offset); 2866572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman else { 2867572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Ops.push_back(SE.getUnknown(ICmpScaledV)); 2868572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman ICmpScaledV = ConstantInt::get(IntTy, Offset); 2869572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2870572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } else { 2871572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Just add the immediate values. These again are expected to be matched 2872572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // as part of the address. 2873572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Ops.push_back(SE.getIntegerSCEV(Offset, IntTy)); 2874572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2875572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2876572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2877572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Emit instructions summing all the operands. 2878572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const SCEV *FullS = Ops.empty() ? 2879572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SE.getIntegerSCEV(0, IntTy) : 2880572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SE.getAddExpr(Ops); 2881572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Value *FullV = Rewriter.expandCodeFor(FullS, Ty, IP); 2882572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2883572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // We're done expanding now, so reset the rewriter. 2884572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Rewriter.setPostInc(0); 2885572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2886572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // An ICmpZero Formula represents an ICmp which we're handling as a 2887572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // comparison against zero. Now that we've expanded an expression for that 2888572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // form, update the ICmp's other operand. 2889572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (LU.Kind == LSRUse::ICmpZero) { 2890572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman ICmpInst *CI = cast<ICmpInst>(LF.UserInst); 2891572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman DeadInsts.push_back(CI->getOperand(1)); 2892572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman assert(!F.AM.BaseGV && "ICmp does not support folding a global value and " 2893572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman "a scale at the same time!"); 2894572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (F.AM.Scale == -1) { 2895572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (ICmpScaledV->getType() != OpTy) { 2896572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Instruction *Cast = 2897572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman CastInst::Create(CastInst::getCastOpcode(ICmpScaledV, false, 2898572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman OpTy, false), 2899572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman ICmpScaledV, OpTy, "tmp", CI); 2900572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman ICmpScaledV = Cast; 2901572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2902572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman CI->setOperand(1, ICmpScaledV); 2903572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } else { 2904572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman assert(F.AM.Scale == 0 && 2905572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman "ICmp does not support folding a global value and " 2906572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman "a scale at the same time!"); 2907572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Constant *C = ConstantInt::getSigned(SE.getEffectiveSCEVType(OpTy), 2908572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman -(uint64_t)Offset); 2909572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (C->getType() != OpTy) 2910572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false, 2911572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman OpTy, false), 2912572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman C, OpTy); 2913572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2914572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman CI->setOperand(1, C); 2915572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2916572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2917572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2918572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return FullV; 2919572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 2920572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2921572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// Rewrite - Emit instructions for the leading candidate expression for this 2922572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// LSRUse (this is called "expanding"), and update the UserInst to reference 2923572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman/// the newly expanded value. 2924572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanvoid LSRInstance::Rewrite(const LSRFixup &LF, 2925572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const Formula &F, 2926572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Loop *L, Instruction *IVIncInsertPos, 2927572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SCEVExpander &Rewriter, 2928572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallVectorImpl<WeakVH> &DeadInsts, 2929572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman ScalarEvolution &SE, DominatorTree &DT, 2930572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Pass *P) const { 2931572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const Type *OpTy = LF.OperandValToReplace->getType(); 2932572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2933572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // First, find an insertion point that dominates UserInst. For PHI nodes, 2934572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // find the nearest block which dominates all the relevant uses. 2935572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (PHINode *PN = dyn_cast<PHINode>(LF.UserInst)) { 2936572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman DenseMap<BasicBlock *, Value *> Inserted; 2937572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) 2938572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (PN->getIncomingValue(i) == LF.OperandValToReplace) { 2939572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman BasicBlock *BB = PN->getIncomingBlock(i); 2940572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2941572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // If this is a critical edge, split the edge so that we do not insert 2942572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // the code on all predecessor/successor paths. We do this unless this 2943572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // is the canonical backedge for this loop, which complicates post-inc 2944572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // users. 2945572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (e != 1 && BB->getTerminator()->getNumSuccessors() > 1 && 2946572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman !isa<IndirectBrInst>(BB->getTerminator()) && 2947572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman (PN->getParent() != L->getHeader() || !L->contains(BB))) { 2948572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Split the critical edge. 2949572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman BasicBlock *NewBB = SplitCriticalEdge(BB, PN->getParent(), P); 2950572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2951572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // If PN is outside of the loop and BB is in the loop, we want to 2952572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // move the block to be immediately before the PHI block, not 2953572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // immediately after BB. 2954572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (L->contains(BB) && !L->contains(PN)) 2955572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman NewBB->moveBefore(PN->getParent()); 2956572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2957572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Splitting the edge can reduce the number of PHI entries we have. 2958572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman e = PN->getNumIncomingValues(); 2959572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman BB = NewBB; 2960572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman i = PN->getBasicBlockIndex(BB); 2961572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2962572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2963572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman std::pair<DenseMap<BasicBlock *, Value *>::iterator, bool> Pair = 2964572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Inserted.insert(std::make_pair(BB, static_cast<Value *>(0))); 2965572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!Pair.second) 2966572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman PN->setIncomingValue(i, Pair.first->second); 2967572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman else { 2968572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Value *FullV = Expand(LF, F, BB->getTerminator(), L, IVIncInsertPos, 2969572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Rewriter, DeadInsts, SE, DT); 2970572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2971572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // If this is reuse-by-noop-cast, insert the noop cast. 2972572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (FullV->getType() != OpTy) 2973572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman FullV = 2974572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman CastInst::Create(CastInst::getCastOpcode(FullV, false, 2975572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman OpTy, false), 2976572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman FullV, LF.OperandValToReplace->getType(), 2977572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman "tmp", BB->getTerminator()); 2978572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2979572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman PN->setIncomingValue(i, FullV); 2980572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Pair.first->second = FullV; 2981572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2982572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2983572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } else { 2984572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Value *FullV = Expand(LF, F, LF.UserInst, L, IVIncInsertPos, 2985572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Rewriter, DeadInsts, SE, DT); 2986572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2987572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // If this is reuse-by-noop-cast, insert the noop cast. 2988572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (FullV->getType() != OpTy) { 2989572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Instruction *Cast = 2990572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman CastInst::Create(CastInst::getCastOpcode(FullV, false, OpTy, false), 2991572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman FullV, OpTy, "tmp", LF.UserInst); 2992572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman FullV = Cast; 2993572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 2994572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 2995572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Update the user. ICmpZero is handled specially here (for now) because 2996572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Expand may have updated one of the operands of the icmp already, and 2997572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // its new value may happen to be equal to LF.OperandValToReplace, in 2998572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // which case doing replaceUsesOfWith leads to replacing both operands 2999572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // with the same value. TODO: Reorganize this. 3000572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (Uses[LF.LUIdx].Kind == LSRUse::ICmpZero) 3001572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LF.UserInst->setOperand(0, FullV); 3002572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman else 3003572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LF.UserInst->replaceUsesOfWith(LF.OperandValToReplace, FullV); 3004572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 3005572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 3006572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman DeadInsts.push_back(LF.OperandValToReplace); 3007572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 3008572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 3009572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanvoid 3010572645cf84060c0fc25cb91d38cb9079918b3a88Dan GohmanLSRInstance::ImplementSolution(const SmallVectorImpl<const Formula *> &Solution, 3011572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Pass *P) { 3012572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Keep track of instructions we may have made dead, so that 3013572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // we can remove them after we are done working. 3014572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallVector<WeakVH, 16> DeadInsts; 3015572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 3016572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SCEVExpander Rewriter(SE); 3017572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Rewriter.disableCanonicalMode(); 3018572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Rewriter.setIVIncInsertPos(L, IVIncInsertPos); 3019572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 3020572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Expand the new value definitions and update the users. 3021572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (size_t i = 0, e = Fixups.size(); i != e; ++i) { 3022572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman size_t LUIdx = Fixups[i].LUIdx; 3023572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 3024572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Rewrite(Fixups[i], *Solution[LUIdx], L, IVIncInsertPos, Rewriter, 3025572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman DeadInsts, SE, DT, P); 3026572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 3027572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Changed = true; 3028572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 3029572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 3030572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Clean up after ourselves. This must be done before deleting any 3031572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // instructions. 3032572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Rewriter.clear(); 3033f7912df4cbdb44aeac9ac9907c192dfc1e22646dDan Gohman 3034572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Changed |= DeleteTriviallyDeadInstructions(DeadInsts); 3035572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 3036010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner 3037572645cf84060c0fc25cb91d38cb9079918b3a88Dan GohmanLSRInstance::LSRInstance(const TargetLowering *tli, Loop *l, Pass *P) 3038572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman : IU(P->getAnalysis<IVUsers>()), 3039572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SE(P->getAnalysis<ScalarEvolution>()), 3040572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman DT(P->getAnalysis<DominatorTree>()), 3041572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman TLI(tli), L(l), Changed(false), IVIncInsertPos(0) { 30425792f51e12d9c8685399e9857799365854ab5bf6Evan Cheng 3043572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // If LoopSimplify form is not available, stay out of trouble. 3044572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!L->isLoopSimplifyForm()) return; 3045572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 3046572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // If there's no interesting work to be done, bail early. 3047572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (IU.empty()) return; 3048572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 3049572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman DEBUG(dbgs() << "\nLSR on loop "; 3050572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman WriteAsOperand(dbgs(), L->getHeader(), /*PrintType=*/false); 3051572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman dbgs() << ":\n"); 3052572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 3053572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman /// OptimizeShadowIV - If IV is used in a int-to-float cast 3054572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman /// inside the loop then try to eliminate the cast opeation. 3055572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman OptimizeShadowIV(); 3056572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 3057572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Change loop terminating condition to use the postinc iv when possible. 3058572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Changed |= OptimizeLoopTermCond(); 3059572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 3060572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman CollectInterestingTypesAndFactors(); 3061572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman CollectFixupsAndInitialFormulae(); 3062572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman CollectLoopInvariantFixupsAndFormulae(); 3063572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 3064572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman DEBUG(dbgs() << "LSR found " << Uses.size() << " uses:\n"; 3065572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman print_uses(dbgs())); 3066572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 3067572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Now use the reuse data to generate a bunch of interesting ways 3068572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // to formulate the values needed for the uses. 3069572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman GenerateAllReuseFormulae(); 3070572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 3071572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman DEBUG(dbgs() << "\n" 3072572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman "After generating reuse formulae:\n"; 3073572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman print_uses(dbgs())); 3074572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 3075572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman FilterOutUndesirableDedicatedRegisters(); 3076572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman NarrowSearchSpaceUsingHeuristics(); 3077572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 3078572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman SmallVector<const Formula *, 8> Solution; 3079572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Solve(Solution); 3080572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman assert(Solution.size() == Uses.size() && "Malformed solution!"); 3081572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 3082572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Release memory that is no longer needed. 3083572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Factors.clear(); 3084572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Types.clear(); 3085572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman RegUses.clear(); 3086572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 3087572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman#ifndef NDEBUG 3088572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Formulae should be legal. 3089572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (SmallVectorImpl<LSRUse>::const_iterator I = Uses.begin(), 3090572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman E = Uses.end(); I != E; ++I) { 3091572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const LSRUse &LU = *I; 3092572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (SmallVectorImpl<Formula>::const_iterator J = LU.Formulae.begin(), 3093572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman JE = LU.Formulae.end(); J != JE; ++J) 3094572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman assert(isLegalUse(J->AM, LU.MinOffset, LU.MaxOffset, 3095572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LU.Kind, LU.AccessTy, TLI) && 3096572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman "Illegal formula generated!"); 3097572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman }; 3098572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman#endif 3099010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner 3100572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Now that we've decided what we want, make it so. 3101572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman ImplementSolution(Solution, P); 3102572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 3103169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman 3104572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanvoid LSRInstance::print_factors_and_types(raw_ostream &OS) const { 3105572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (Factors.empty() && Types.empty()) return; 31061ce75dcbbcb6a67904a23b4ec701d1e994767c7eEvan Cheng 3107572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman OS << "LSR has identified the following interesting factors and types: "; 3108572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman bool First = true; 3109eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman 3110572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (SmallSetVector<int64_t, 8>::const_iterator 3111572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman I = Factors.begin(), E = Factors.end(); I != E; ++I) { 3112572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!First) OS << ", "; 3113572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman First = false; 3114572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman OS << '*' << *I; 3115572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 31168b0ade3eb8281f9b332d5764cfe5c4ed3b2b32d8Dan Gohman 3117572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (SmallSetVector<const Type *, 4>::const_iterator 3118572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman I = Types.begin(), E = Types.end(); I != E; ++I) { 3119572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman if (!First) OS << ", "; 3120572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman First = false; 3121572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman OS << '(' << **I << ')'; 3122572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 3123572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman OS << '\n'; 3124572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 3125c1acc3f764804d25f70d88f937ef9c460143e0f1Dale Johannesen 3126572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanvoid LSRInstance::print_fixups(raw_ostream &OS) const { 3127572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman OS << "LSR is examining the following fixup sites:\n"; 3128572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (SmallVectorImpl<LSRFixup>::const_iterator I = Fixups.begin(), 3129572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman E = Fixups.end(); I != E; ++I) { 3130572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const LSRFixup &LF = *I; 3131572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman dbgs() << " "; 3132572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LF.print(OS); 3133572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman OS << '\n'; 3134572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 3135572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 3136010ee2d95516fe13a574bce5d682a8f8997ab60bDan Gohman 3137572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanvoid LSRInstance::print_uses(raw_ostream &OS) const { 3138572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman OS << "LSR is examining the following uses:\n"; 3139572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (SmallVectorImpl<LSRUse>::const_iterator I = Uses.begin(), 3140572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman E = Uses.end(); I != E; ++I) { 3141572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const LSRUse &LU = *I; 3142572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman dbgs() << " "; 3143572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman LU.print(OS); 3144572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman OS << '\n'; 3145572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman for (SmallVectorImpl<Formula>::const_iterator J = LU.Formulae.begin(), 3146572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman JE = LU.Formulae.end(); J != JE; ++J) { 3147572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman OS << " "; 3148572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman J->print(OS); 3149572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman OS << '\n'; 3150572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman } 31516bec5bb344fc0374431aed1cb63418de607a1aecDan Gohman } 3152572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 3153572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 3154572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanvoid LSRInstance::print(raw_ostream &OS) const { 3155572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman print_factors_and_types(OS); 3156572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman print_fixups(OS); 3157572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman print_uses(OS); 3158572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 3159572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 3160572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanvoid LSRInstance::dump() const { 3161572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman print(errs()); errs() << '\n'; 3162572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 3163572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 3164572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmannamespace { 3165572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 3166572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanclass LoopStrengthReduce : public LoopPass { 3167572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman /// TLI - Keep a pointer of a TargetLowering to consult for determining 3168572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman /// transformation profitability. 3169572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman const TargetLowering *const TLI; 3170572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 3171572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanpublic: 3172572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman static char ID; // Pass ID, replacement for typeid 3173572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman explicit LoopStrengthReduce(const TargetLowering *tli = 0); 3174572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 3175572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanprivate: 3176572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman bool runOnLoop(Loop *L, LPPassManager &LPM); 3177572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman void getAnalysisUsage(AnalysisUsage &AU) const; 3178572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman}; 3179572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 3180572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 3181572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 3182572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanchar LoopStrengthReduce::ID = 0; 3183572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanstatic RegisterPass<LoopStrengthReduce> 3184572645cf84060c0fc25cb91d38cb9079918b3a88Dan GohmanX("loop-reduce", "Loop Strength Reduction"); 3185572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 3186572645cf84060c0fc25cb91d38cb9079918b3a88Dan GohmanPass *llvm::createLoopStrengthReducePass(const TargetLowering *TLI) { 3187572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman return new LoopStrengthReduce(TLI); 3188572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 3189572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 3190572645cf84060c0fc25cb91d38cb9079918b3a88Dan GohmanLoopStrengthReduce::LoopStrengthReduce(const TargetLowering *tli) 3191572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman : LoopPass(&ID), TLI(tli) {} 3192572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 3193572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanvoid LoopStrengthReduce::getAnalysisUsage(AnalysisUsage &AU) const { 3194572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // We split critical edges, so we change the CFG. However, we do update 3195572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // many analyses if they are around. 3196572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman AU.addPreservedID(LoopSimplifyID); 3197572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman AU.addPreserved<LoopInfo>(); 3198572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman AU.addPreserved("domfrontier"); 3199572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 3200572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman AU.addRequiredID(LoopSimplifyID); 3201572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman AU.addRequired<DominatorTree>(); 3202572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman AU.addPreserved<DominatorTree>(); 3203572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman AU.addRequired<ScalarEvolution>(); 3204572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman AU.addPreserved<ScalarEvolution>(); 3205572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman AU.addRequired<IVUsers>(); 3206572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman AU.addPreserved<IVUsers>(); 3207572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman} 3208572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 3209572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohmanbool LoopStrengthReduce::runOnLoop(Loop *L, LPPassManager & /*LPM*/) { 3210572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman bool Changed = false; 3211572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman 3212572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman // Run the main LSR transformation. 3213572645cf84060c0fc25cb91d38cb9079918b3a88Dan Gohman Changed |= LSRInstance(TLI, L, this).getChanged(); 3214eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman 3215afc36a9520971832dfbebc0333593bf5d3098296Dan Gohman // At this point, it is worth checking to see if any recurrence PHIs are also 321635738ac150afafe2359268d4b2169498c6c98c5fDan Gohman // dead, so that we can remove them as well. 32179fff2187a21f765ed87a25c48552a6942450f3e2Dan Gohman Changed |= DeleteDeadPHIs(L->getHeader()); 3218afc36a9520971832dfbebc0333593bf5d3098296Dan Gohman 32191ce75dcbbcb6a67904a23b4ec701d1e994767c7eEvan Cheng return Changed; 3220eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman} 3221