Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 1 | //===- ConstantHoisting.cpp - Prepare code for expensive constants --------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass identifies expensive constants to hoist and coalesces them to |
| 11 | // better prepare it for SelectionDAG-based code generation. This works around |
| 12 | // the limitations of the basic-block-at-a-time approach. |
| 13 | // |
| 14 | // First it scans all instructions for integer constants and calculates its |
| 15 | // cost. If the constant can be folded into the instruction (the cost is |
| 16 | // TCC_Free) or the cost is just a simple operation (TCC_BASIC), then we don't |
| 17 | // consider it expensive and leave it alone. This is the default behavior and |
| 18 | // the default implementation of getIntImmCost will always return TCC_Free. |
| 19 | // |
| 20 | // If the cost is more than TCC_BASIC, then the integer constant can't be folded |
| 21 | // into the instruction and it might be beneficial to hoist the constant. |
| 22 | // Similar constants are coalesced to reduce register pressure and |
| 23 | // materialization code. |
| 24 | // |
| 25 | // When a constant is hoisted, it is also hidden behind a bitcast to force it to |
| 26 | // be live-out of the basic block. Otherwise the constant would be just |
| 27 | // duplicated and each basic block would have its own copy in the SelectionDAG. |
| 28 | // The SelectionDAG recognizes such constants as opaque and doesn't perform |
| 29 | // certain transformations on them, which would create a new expensive constant. |
| 30 | // |
| 31 | // This optimization is only applied to integer constants in instructions and |
Juergen Ributzka | f0dff49 | 2014-03-21 06:04:45 +0000 | [diff] [blame] | 32 | // simple (this means not nested) constant cast expressions. For example: |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 33 | // %0 = load i64* inttoptr (i64 big_constant to i64*) |
| 34 | //===----------------------------------------------------------------------===// |
| 35 | |
| 36 | #define DEBUG_TYPE "consthoist" |
| 37 | #include "llvm/Transforms/Scalar.h" |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 38 | #include "llvm/ADT/SmallSet.h" |
Juergen Ributzka | a29a5b8 | 2014-03-21 06:04:30 +0000 | [diff] [blame] | 39 | #include "llvm/ADT/SmallVector.h" |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 40 | #include "llvm/ADT/Statistic.h" |
| 41 | #include "llvm/Analysis/TargetTransformInfo.h" |
| 42 | #include "llvm/IR/Constants.h" |
| 43 | #include "llvm/IR/Dominators.h" |
| 44 | #include "llvm/IR/IntrinsicInst.h" |
| 45 | #include "llvm/Pass.h" |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 46 | #include "llvm/Support/Debug.h" |
| 47 | |
| 48 | using namespace llvm; |
| 49 | |
| 50 | STATISTIC(NumConstantsHoisted, "Number of constants hoisted"); |
| 51 | STATISTIC(NumConstantsRebased, "Number of constants rebased"); |
| 52 | |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 53 | namespace { |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 54 | struct ConstantUser; |
| 55 | struct RebasedConstantInfo; |
| 56 | |
| 57 | typedef SmallVector<ConstantUser, 8> ConstantUseListType; |
| 58 | typedef SmallVector<RebasedConstantInfo, 4> RebasedConstantListType; |
| 59 | |
| 60 | /// \brief Keeps track of the user of a constant and the operand index where the |
| 61 | /// constant is used. |
| 62 | struct ConstantUser { |
| 63 | Instruction *Inst; |
| 64 | unsigned OpndIdx; |
| 65 | |
| 66 | ConstantUser(Instruction *Inst, unsigned Idx) : Inst(Inst), OpndIdx(Idx) { } |
| 67 | }; |
| 68 | |
Juergen Ributzka | f0dff49 | 2014-03-21 06:04:45 +0000 | [diff] [blame] | 69 | /// \brief Keeps track of a constant candidate and its uses. |
Juergen Ributzka | 6dab520 | 2014-03-20 19:55:52 +0000 | [diff] [blame] | 70 | struct ConstantCandidate { |
Juergen Ributzka | 6dab520 | 2014-03-20 19:55:52 +0000 | [diff] [blame] | 71 | ConstantUseListType Uses; |
Juergen Ributzka | a29a5b8 | 2014-03-21 06:04:30 +0000 | [diff] [blame] | 72 | ConstantInt *ConstInt; |
| 73 | unsigned CumulativeCost; |
| 74 | |
| 75 | ConstantCandidate(ConstantInt *ConstInt) |
| 76 | : ConstInt(ConstInt), CumulativeCost(0) { } |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 77 | |
| 78 | /// \brief Add the user to the use list and update the cost. |
| 79 | void addUser(Instruction *Inst, unsigned Idx, unsigned Cost) { |
| 80 | CumulativeCost += Cost; |
| 81 | Uses.push_back(ConstantUser(Inst, Idx)); |
| 82 | } |
Juergen Ributzka | 6dab520 | 2014-03-20 19:55:52 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 85 | /// \brief This represents a constant that has been rebased with respect to a |
| 86 | /// base constant. The difference to the base constant is recorded in Offset. |
| 87 | struct RebasedConstantInfo { |
| 88 | ConstantUseListType Uses; |
| 89 | Constant *Offset; |
Juergen Ributzka | 500abd4 | 2014-03-21 06:04:39 +0000 | [diff] [blame] | 90 | mutable BasicBlock *IDom; |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 91 | |
| 92 | RebasedConstantInfo(ConstantUseListType &&Uses, Constant *Offset) |
Juergen Ributzka | 500abd4 | 2014-03-21 06:04:39 +0000 | [diff] [blame] | 93 | : Uses(Uses), Offset(Offset), IDom(nullptr) { } |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | /// \brief A base constant and all its rebased constants. |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 97 | struct ConstantInfo { |
| 98 | ConstantInt *BaseConstant; |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 99 | RebasedConstantListType RebasedConstants; |
| 100 | }; |
| 101 | |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 102 | /// \brief The constant hoisting pass. |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 103 | class ConstantHoisting : public FunctionPass { |
Juergen Ributzka | a29a5b8 | 2014-03-21 06:04:30 +0000 | [diff] [blame] | 104 | typedef DenseMap<ConstantInt *, unsigned> ConstCandMapType; |
| 105 | typedef std::vector<ConstantCandidate> ConstCandVecType; |
| 106 | |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 107 | const TargetTransformInfo *TTI; |
| 108 | DominatorTree *DT; |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 109 | BasicBlock *Entry; |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 110 | |
Juergen Ributzka | a29a5b8 | 2014-03-21 06:04:30 +0000 | [diff] [blame] | 111 | /// Keeps track of constant candidates found in the function. |
Juergen Ributzka | a29a5b8 | 2014-03-21 06:04:30 +0000 | [diff] [blame] | 112 | ConstCandVecType ConstCandVec; |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 113 | |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 114 | /// Keep track of cast instructions we already cloned. |
| 115 | SmallDenseMap<Instruction *, Instruction *> ClonedCastMap; |
| 116 | |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 117 | /// These are the final constants we decided to hoist. |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 118 | SmallVector<ConstantInfo, 8> ConstantVec; |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 119 | public: |
| 120 | static char ID; // Pass identification, replacement for typeid |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 121 | ConstantHoisting() : FunctionPass(ID), TTI(0), DT(0), Entry(0) { |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 122 | initializeConstantHoistingPass(*PassRegistry::getPassRegistry()); |
| 123 | } |
| 124 | |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 125 | bool runOnFunction(Function &Fn) override; |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 126 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 127 | const char *getPassName() const override { return "Constant Hoisting"; } |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 128 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 129 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 130 | AU.setPreservesCFG(); |
| 131 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 132 | AU.addRequired<TargetTransformInfo>(); |
| 133 | } |
| 134 | |
| 135 | private: |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 136 | /// \brief Initialize the pass. |
| 137 | void setup(Function &Fn) { |
| 138 | DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
| 139 | TTI = &getAnalysis<TargetTransformInfo>(); |
| 140 | Entry = &Fn.getEntryBlock(); |
| 141 | } |
| 142 | |
| 143 | /// \brief Cleanup. |
| 144 | void cleanup() { |
| 145 | ConstantVec.clear(); |
| 146 | ClonedCastMap.clear(); |
| 147 | ConstCandVec.clear(); |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 148 | |
| 149 | TTI = nullptr; |
| 150 | DT = nullptr; |
| 151 | Entry = nullptr; |
| 152 | } |
| 153 | |
Juergen Ributzka | 500abd4 | 2014-03-21 06:04:39 +0000 | [diff] [blame] | 154 | /// \brief Find the common dominator of all uses and cache the result for |
| 155 | /// future lookup. |
| 156 | BasicBlock *getIDom(const RebasedConstantInfo &RCI) const { |
| 157 | if (RCI.IDom) |
| 158 | return RCI.IDom; |
| 159 | RCI.IDom = findIDomOfAllUses(RCI.Uses); |
| 160 | assert(RCI.IDom && "Invalid IDom."); |
| 161 | return RCI.IDom; |
| 162 | } |
| 163 | |
| 164 | BasicBlock *findIDomOfAllUses(const ConstantUseListType &Uses) const; |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 165 | Instruction *findMatInsertPt(Instruction *Inst, unsigned Idx = ~0U) const; |
| 166 | Instruction *findConstantInsertionPoint(const ConstantInfo &ConstInfo) const; |
Juergen Ributzka | 7be410f | 2014-03-25 21:21:10 +0000 | [diff] [blame] | 167 | void collectConstantCandidates(ConstCandMapType &ConstCandMap, |
| 168 | Instruction *Inst, unsigned Idx, |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 169 | ConstantInt *ConstInt); |
Juergen Ributzka | 7be410f | 2014-03-25 21:21:10 +0000 | [diff] [blame] | 170 | void collectConstantCandidates(ConstCandMapType &ConstCandMap, |
| 171 | Instruction *Inst); |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 172 | void collectConstantCandidates(Function &Fn); |
Juergen Ributzka | b8489b3 | 2014-03-21 06:04:33 +0000 | [diff] [blame] | 173 | void findAndMakeBaseConstant(ConstCandVecType::iterator S, |
Juergen Ributzka | a29a5b8 | 2014-03-21 06:04:30 +0000 | [diff] [blame] | 174 | ConstCandVecType::iterator E); |
Juergen Ributzka | b8489b3 | 2014-03-21 06:04:33 +0000 | [diff] [blame] | 175 | void findBaseConstants(); |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 176 | void emitBaseConstants(Instruction *Base, Constant *Offset, |
| 177 | const ConstantUser &ConstUser); |
| 178 | bool emitBaseConstants(); |
| 179 | void deleteDeadCastInst() const; |
| 180 | bool optimizeConstants(Function &Fn); |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 181 | }; |
| 182 | } |
| 183 | |
| 184 | char ConstantHoisting::ID = 0; |
| 185 | INITIALIZE_PASS_BEGIN(ConstantHoisting, "consthoist", "Constant Hoisting", |
| 186 | false, false) |
| 187 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
| 188 | INITIALIZE_AG_DEPENDENCY(TargetTransformInfo) |
| 189 | INITIALIZE_PASS_END(ConstantHoisting, "consthoist", "Constant Hoisting", |
| 190 | false, false) |
| 191 | |
| 192 | FunctionPass *llvm::createConstantHoistingPass() { |
| 193 | return new ConstantHoisting(); |
| 194 | } |
| 195 | |
| 196 | /// \brief Perform the constant hoisting optimization for the given function. |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 197 | bool ConstantHoisting::runOnFunction(Function &Fn) { |
| 198 | DEBUG(dbgs() << "********** Begin Constant Hoisting **********\n"); |
| 199 | DEBUG(dbgs() << "********** Function: " << Fn.getName() << '\n'); |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 200 | |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 201 | setup(Fn); |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 202 | |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 203 | bool MadeChange = optimizeConstants(Fn); |
| 204 | |
| 205 | if (MadeChange) { |
| 206 | DEBUG(dbgs() << "********** Function after Constant Hoisting: " |
| 207 | << Fn.getName() << '\n'); |
| 208 | DEBUG(dbgs() << Fn); |
| 209 | } |
| 210 | DEBUG(dbgs() << "********** End Constant Hoisting **********\n"); |
| 211 | |
| 212 | cleanup(); |
| 213 | |
| 214 | return MadeChange; |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Juergen Ributzka | 500abd4 | 2014-03-21 06:04:39 +0000 | [diff] [blame] | 217 | /// \brief Find nearest common dominator of all uses. |
| 218 | /// FIXME: Replace this with NearestCommonDominator once it is in common code. |
| 219 | BasicBlock * |
| 220 | ConstantHoisting::findIDomOfAllUses(const ConstantUseListType &Uses) const { |
| 221 | // Collect all basic blocks. |
| 222 | SmallPtrSet<BasicBlock *, 8> BBs; |
| 223 | for (auto const &U : Uses) |
| 224 | BBs.insert(findMatInsertPt(U.Inst, U.OpndIdx)->getParent()); |
| 225 | |
| 226 | if (BBs.count(Entry)) |
| 227 | return Entry; |
| 228 | |
| 229 | while (BBs.size() >= 2) { |
| 230 | BasicBlock *BB, *BB1, *BB2; |
| 231 | BB1 = *BBs.begin(); |
| 232 | BB2 = *std::next(BBs.begin()); |
| 233 | BB = DT->findNearestCommonDominator(BB1, BB2); |
| 234 | if (BB == Entry) |
| 235 | return Entry; |
| 236 | BBs.erase(BB1); |
| 237 | BBs.erase(BB2); |
| 238 | BBs.insert(BB); |
| 239 | } |
| 240 | assert((BBs.size() == 1) && "Expected only one element."); |
| 241 | return *BBs.begin(); |
| 242 | } |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 243 | |
| 244 | /// \brief Find the constant materialization insertion point. |
| 245 | Instruction *ConstantHoisting::findMatInsertPt(Instruction *Inst, |
| 246 | unsigned Idx) const { |
| 247 | // The simple and common case. |
| 248 | if (!isa<PHINode>(Inst) && !isa<LandingPadInst>(Inst)) |
| 249 | return Inst; |
| 250 | |
| 251 | // We can't insert directly before a phi node or landing pad. Insert before |
| 252 | // the terminator of the incoming or dominating block. |
| 253 | assert(Entry != Inst->getParent() && "PHI or landing pad in entry block!"); |
| 254 | if (Idx != ~0U && isa<PHINode>(Inst)) |
| 255 | return cast<PHINode>(Inst)->getIncomingBlock(Idx)->getTerminator(); |
| 256 | |
| 257 | BasicBlock *IDom = DT->getNode(Inst->getParent())->getIDom()->getBlock(); |
| 258 | return IDom->getTerminator(); |
| 259 | } |
| 260 | |
| 261 | /// \brief Find an insertion point that dominates all uses. |
| 262 | Instruction *ConstantHoisting:: |
| 263 | findConstantInsertionPoint(const ConstantInfo &ConstInfo) const { |
| 264 | assert(!ConstInfo.RebasedConstants.empty() && "Invalid constant info entry."); |
Juergen Ributzka | 500abd4 | 2014-03-21 06:04:39 +0000 | [diff] [blame] | 265 | // Collect all IDoms. |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 266 | SmallPtrSet<BasicBlock *, 8> BBs; |
| 267 | for (auto const &RCI : ConstInfo.RebasedConstants) |
Juergen Ributzka | 500abd4 | 2014-03-21 06:04:39 +0000 | [diff] [blame] | 268 | BBs.insert(getIDom(RCI)); |
| 269 | |
| 270 | assert(!BBs.empty() && "No dominators!?"); |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 271 | |
| 272 | if (BBs.count(Entry)) |
| 273 | return &Entry->front(); |
| 274 | |
| 275 | while (BBs.size() >= 2) { |
| 276 | BasicBlock *BB, *BB1, *BB2; |
| 277 | BB1 = *BBs.begin(); |
| 278 | BB2 = *std::next(BBs.begin()); |
| 279 | BB = DT->findNearestCommonDominator(BB1, BB2); |
| 280 | if (BB == Entry) |
| 281 | return &Entry->front(); |
| 282 | BBs.erase(BB1); |
| 283 | BBs.erase(BB2); |
| 284 | BBs.insert(BB); |
| 285 | } |
| 286 | assert((BBs.size() == 1) && "Expected only one element."); |
| 287 | Instruction &FirstInst = (*BBs.begin())->front(); |
| 288 | return findMatInsertPt(&FirstInst); |
| 289 | } |
| 290 | |
| 291 | |
| 292 | /// \brief Record constant integer ConstInt for instruction Inst at operand |
| 293 | /// index Idx. |
| 294 | /// |
Juergen Ributzka | f0dff49 | 2014-03-21 06:04:45 +0000 | [diff] [blame] | 295 | /// The operand at index Idx is not necessarily the constant integer itself. It |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 296 | /// could also be a cast instruction or a constant expression that uses the |
| 297 | // constant integer. |
Juergen Ributzka | 7be410f | 2014-03-25 21:21:10 +0000 | [diff] [blame] | 298 | void ConstantHoisting::collectConstantCandidates(ConstCandMapType &ConstCandMap, |
| 299 | Instruction *Inst, |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 300 | unsigned Idx, |
| 301 | ConstantInt *ConstInt) { |
Juergen Ributzka | 6dab520 | 2014-03-20 19:55:52 +0000 | [diff] [blame] | 302 | unsigned Cost; |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 303 | // Ask the target about the cost of materializing the constant for the given |
Juergen Ributzka | f0dff49 | 2014-03-21 06:04:45 +0000 | [diff] [blame] | 304 | // instruction and operand index. |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 305 | if (auto IntrInst = dyn_cast<IntrinsicInst>(Inst)) |
Juergen Ributzka | f0dff49 | 2014-03-21 06:04:45 +0000 | [diff] [blame] | 306 | Cost = TTI->getIntImmCost(IntrInst->getIntrinsicID(), Idx, |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 307 | ConstInt->getValue(), ConstInt->getType()); |
Juergen Ributzka | 6dab520 | 2014-03-20 19:55:52 +0000 | [diff] [blame] | 308 | else |
Juergen Ributzka | f0dff49 | 2014-03-21 06:04:45 +0000 | [diff] [blame] | 309 | Cost = TTI->getIntImmCost(Inst->getOpcode(), Idx, ConstInt->getValue(), |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 310 | ConstInt->getType()); |
Juergen Ributzka | 6dab520 | 2014-03-20 19:55:52 +0000 | [diff] [blame] | 311 | |
Juergen Ributzka | a29a5b8 | 2014-03-21 06:04:30 +0000 | [diff] [blame] | 312 | // Ignore cheap integer constants. |
Juergen Ributzka | 6dab520 | 2014-03-20 19:55:52 +0000 | [diff] [blame] | 313 | if (Cost > TargetTransformInfo::TCC_Basic) { |
Juergen Ributzka | a29a5b8 | 2014-03-21 06:04:30 +0000 | [diff] [blame] | 314 | ConstCandMapType::iterator Itr; |
| 315 | bool Inserted; |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 316 | std::tie(Itr, Inserted) = ConstCandMap.insert(std::make_pair(ConstInt, 0)); |
Juergen Ributzka | a29a5b8 | 2014-03-21 06:04:30 +0000 | [diff] [blame] | 317 | if (Inserted) { |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 318 | ConstCandVec.push_back(ConstantCandidate(ConstInt)); |
Juergen Ributzka | a29a5b8 | 2014-03-21 06:04:30 +0000 | [diff] [blame] | 319 | Itr->second = ConstCandVec.size() - 1; |
| 320 | } |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 321 | ConstCandVec[Itr->second].addUser(Inst, Idx, Cost); |
| 322 | DEBUG(if (isa<ConstantInt>(Inst->getOperand(Idx))) |
| 323 | dbgs() << "Collect constant " << *ConstInt << " from " << *Inst |
| 324 | << " with cost " << Cost << '\n'; |
| 325 | else |
| 326 | dbgs() << "Collect constant " << *ConstInt << " indirectly from " |
| 327 | << *Inst << " via " << *Inst->getOperand(Idx) << " with cost " |
| 328 | << Cost << '\n'; |
| 329 | ); |
Juergen Ributzka | 6dab520 | 2014-03-20 19:55:52 +0000 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 333 | /// \brief Scan the instruction for expensive integer constants and record them |
| 334 | /// in the constant candidate vector. |
Juergen Ributzka | 7be410f | 2014-03-25 21:21:10 +0000 | [diff] [blame] | 335 | void ConstantHoisting::collectConstantCandidates(ConstCandMapType &ConstCandMap, |
| 336 | Instruction *Inst) { |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 337 | // Skip all cast instructions. They are visited indirectly later on. |
| 338 | if (Inst->isCast()) |
| 339 | return; |
| 340 | |
| 341 | // Can't handle inline asm. Skip it. |
| 342 | if (auto Call = dyn_cast<CallInst>(Inst)) |
| 343 | if (isa<InlineAsm>(Call->getCalledValue())) |
| 344 | return; |
Juergen Ributzka | 6dab520 | 2014-03-20 19:55:52 +0000 | [diff] [blame] | 345 | |
| 346 | // Scan all operands. |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 347 | for (unsigned Idx = 0, E = Inst->getNumOperands(); Idx != E; ++Idx) { |
| 348 | Value *Opnd = Inst->getOperand(Idx); |
| 349 | |
Juergen Ributzka | 7be410f | 2014-03-25 21:21:10 +0000 | [diff] [blame] | 350 | // Visit constant integers. |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 351 | if (auto ConstInt = dyn_cast<ConstantInt>(Opnd)) { |
Juergen Ributzka | 7be410f | 2014-03-25 21:21:10 +0000 | [diff] [blame] | 352 | collectConstantCandidates(ConstCandMap, Inst, Idx, ConstInt); |
Juergen Ributzka | 6dab520 | 2014-03-20 19:55:52 +0000 | [diff] [blame] | 353 | continue; |
| 354 | } |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 355 | |
| 356 | // Visit cast instructions that have constant integers. |
| 357 | if (auto CastInst = dyn_cast<Instruction>(Opnd)) { |
| 358 | // Only visit cast instructions, which have been skipped. All other |
| 359 | // instructions should have already been visited. |
| 360 | if (!CastInst->isCast()) |
Juergen Ributzka | 6dab520 | 2014-03-20 19:55:52 +0000 | [diff] [blame] | 361 | continue; |
| 362 | |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 363 | if (auto *ConstInt = dyn_cast<ConstantInt>(CastInst->getOperand(0))) { |
| 364 | // Pretend the constant is directly used by the instruction and ignore |
| 365 | // the cast instruction. |
Juergen Ributzka | 7be410f | 2014-03-25 21:21:10 +0000 | [diff] [blame] | 366 | collectConstantCandidates(ConstCandMap, Inst, Idx, ConstInt); |
Juergen Ributzka | 6dab520 | 2014-03-20 19:55:52 +0000 | [diff] [blame] | 367 | continue; |
| 368 | } |
| 369 | } |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 370 | |
| 371 | // Visit constant expressions that have constant integers. |
| 372 | if (auto ConstExpr = dyn_cast<ConstantExpr>(Opnd)) { |
| 373 | // Only visit constant cast expressions. |
| 374 | if (!ConstExpr->isCast()) |
| 375 | continue; |
| 376 | |
| 377 | if (auto ConstInt = dyn_cast<ConstantInt>(ConstExpr->getOperand(0))) { |
| 378 | // Pretend the constant is directly used by the instruction and ignore |
| 379 | // the constant expression. |
Juergen Ributzka | 7be410f | 2014-03-25 21:21:10 +0000 | [diff] [blame] | 380 | collectConstantCandidates(ConstCandMap, Inst, Idx, ConstInt); |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 381 | continue; |
| 382 | } |
| 383 | } |
| 384 | } // end of for all operands |
Juergen Ributzka | 6dab520 | 2014-03-20 19:55:52 +0000 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | /// \brief Collect all integer constants in the function that cannot be folded |
| 388 | /// into an instruction itself. |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 389 | void ConstantHoisting::collectConstantCandidates(Function &Fn) { |
Juergen Ributzka | 7be410f | 2014-03-25 21:21:10 +0000 | [diff] [blame] | 390 | ConstCandMapType ConstCandMap; |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 391 | for (Function::iterator BB : Fn) |
| 392 | for (BasicBlock::iterator Inst : *BB) |
Juergen Ributzka | 7be410f | 2014-03-25 21:21:10 +0000 | [diff] [blame] | 393 | collectConstantCandidates(ConstCandMap, Inst); |
Juergen Ributzka | 6dab520 | 2014-03-20 19:55:52 +0000 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | /// \brief Find the base constant within the given range and rebase all other |
| 397 | /// constants with respect to the base constant. |
Juergen Ributzka | b8489b3 | 2014-03-21 06:04:33 +0000 | [diff] [blame] | 398 | void ConstantHoisting::findAndMakeBaseConstant(ConstCandVecType::iterator S, |
Juergen Ributzka | a29a5b8 | 2014-03-21 06:04:30 +0000 | [diff] [blame] | 399 | ConstCandVecType::iterator E) { |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 400 | auto MaxCostItr = S; |
Juergen Ributzka | 6dab520 | 2014-03-20 19:55:52 +0000 | [diff] [blame] | 401 | unsigned NumUses = 0; |
| 402 | // Use the constant that has the maximum cost as base constant. |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 403 | for (auto ConstCand = S; ConstCand != E; ++ConstCand) { |
| 404 | NumUses += ConstCand->Uses.size(); |
| 405 | if (ConstCand->CumulativeCost > MaxCostItr->CumulativeCost) |
| 406 | MaxCostItr = ConstCand; |
Juergen Ributzka | 6dab520 | 2014-03-20 19:55:52 +0000 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | // Don't hoist constants that have only one use. |
| 410 | if (NumUses <= 1) |
| 411 | return; |
| 412 | |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 413 | ConstantInfo ConstInfo; |
| 414 | ConstInfo.BaseConstant = MaxCostItr->ConstInt; |
| 415 | Type *Ty = ConstInfo.BaseConstant->getType(); |
| 416 | |
Juergen Ributzka | 6dab520 | 2014-03-20 19:55:52 +0000 | [diff] [blame] | 417 | // Rebase the constants with respect to the base constant. |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 418 | for (auto ConstCand = S; ConstCand != E; ++ConstCand) { |
| 419 | APInt Diff = ConstCand->ConstInt->getValue() - |
| 420 | ConstInfo.BaseConstant->getValue(); |
| 421 | Constant *Offset = Diff == 0 ? nullptr : ConstantInt::get(Ty, Diff); |
| 422 | ConstInfo.RebasedConstants.push_back( |
| 423 | RebasedConstantInfo(std::move(ConstCand->Uses), Offset)); |
Juergen Ributzka | 6dab520 | 2014-03-20 19:55:52 +0000 | [diff] [blame] | 424 | } |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 425 | ConstantVec.push_back(ConstInfo); |
Juergen Ributzka | 6dab520 | 2014-03-20 19:55:52 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 428 | /// \brief Finds and combines constant candidates that can be easily |
| 429 | /// rematerialized with an add from a common base constant. |
Juergen Ributzka | b8489b3 | 2014-03-21 06:04:33 +0000 | [diff] [blame] | 430 | void ConstantHoisting::findBaseConstants() { |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 431 | // Sort the constants by value and type. This invalidates the mapping! |
Juergen Ributzka | a29a5b8 | 2014-03-21 06:04:30 +0000 | [diff] [blame] | 432 | std::sort(ConstCandVec.begin(), ConstCandVec.end(), |
| 433 | [](const ConstantCandidate &LHS, const ConstantCandidate &RHS) { |
| 434 | if (LHS.ConstInt->getType() != RHS.ConstInt->getType()) |
| 435 | return LHS.ConstInt->getType()->getBitWidth() < |
| 436 | RHS.ConstInt->getType()->getBitWidth(); |
| 437 | return LHS.ConstInt->getValue().ult(RHS.ConstInt->getValue()); |
Juergen Ributzka | 4635793 | 2014-03-20 20:17:13 +0000 | [diff] [blame] | 438 | }); |
Juergen Ributzka | 6dab520 | 2014-03-20 19:55:52 +0000 | [diff] [blame] | 439 | |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 440 | // Simple linear scan through the sorted constant candidate vector for viable |
| 441 | // merge candidates. |
| 442 | auto MinValItr = ConstCandVec.begin(); |
| 443 | for (auto CC = std::next(ConstCandVec.begin()), E = ConstCandVec.end(); |
| 444 | CC != E; ++CC) { |
| 445 | if (MinValItr->ConstInt->getType() == CC->ConstInt->getType()) { |
Juergen Ributzka | 6dab520 | 2014-03-20 19:55:52 +0000 | [diff] [blame] | 446 | // Check if the constant is in range of an add with immediate. |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 447 | APInt Diff = CC->ConstInt->getValue() - MinValItr->ConstInt->getValue(); |
Juergen Ributzka | 6dab520 | 2014-03-20 19:55:52 +0000 | [diff] [blame] | 448 | if ((Diff.getBitWidth() <= 64) && |
| 449 | TTI->isLegalAddImmediate(Diff.getSExtValue())) |
| 450 | continue; |
| 451 | } |
| 452 | // We either have now a different constant type or the constant is not in |
| 453 | // range of an add with immediate anymore. |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 454 | findAndMakeBaseConstant(MinValItr, CC); |
Juergen Ributzka | 6dab520 | 2014-03-20 19:55:52 +0000 | [diff] [blame] | 455 | // Start a new base constant search. |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 456 | MinValItr = CC; |
Juergen Ributzka | 6dab520 | 2014-03-20 19:55:52 +0000 | [diff] [blame] | 457 | } |
| 458 | // Finalize the last base constant search. |
Juergen Ributzka | b8489b3 | 2014-03-21 06:04:33 +0000 | [diff] [blame] | 459 | findAndMakeBaseConstant(MinValItr, ConstCandVec.end()); |
Juergen Ributzka | 4635793 | 2014-03-20 20:17:13 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Juergen Ributzka | e802d50 | 2014-03-22 01:49:27 +0000 | [diff] [blame] | 462 | /// \brief Updates the operand at Idx in instruction Inst with the result of |
| 463 | /// instruction Mat. If the instruction is a PHI node then special |
| 464 | /// handling for duplicate values form the same incomming basic block is |
| 465 | /// required. |
| 466 | /// \return The update will always succeed, but the return value indicated if |
| 467 | /// Mat was used for the update or not. |
| 468 | static bool updateOperand(Instruction *Inst, unsigned Idx, Instruction *Mat) { |
| 469 | if (auto PHI = dyn_cast<PHINode>(Inst)) { |
| 470 | // Check if any previous operand of the PHI node has the same incoming basic |
| 471 | // block. This is a very odd case that happens when the incoming basic block |
| 472 | // has a switch statement. In this case use the same value as the previous |
| 473 | // operand(s), otherwise we will fail verification due to different values. |
| 474 | // The values are actually the same, but the variable names are different |
| 475 | // and the verifier doesn't like that. |
| 476 | BasicBlock *IncomingBB = PHI->getIncomingBlock(Idx); |
| 477 | for (unsigned i = 0; i < Idx; ++i) { |
| 478 | if (PHI->getIncomingBlock(i) == IncomingBB) { |
| 479 | Value *IncomingVal = PHI->getIncomingValue(i); |
| 480 | Inst->setOperand(Idx, IncomingVal); |
| 481 | return false; |
| 482 | } |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | Inst->setOperand(Idx, Mat); |
| 487 | return true; |
| 488 | } |
| 489 | |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 490 | /// \brief Emit materialization code for all rebased constants and update their |
| 491 | /// users. |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 492 | void ConstantHoisting::emitBaseConstants(Instruction *Base, Constant *Offset, |
| 493 | const ConstantUser &ConstUser) { |
| 494 | Instruction *Mat = Base; |
| 495 | if (Offset) { |
| 496 | Instruction *InsertionPt = findMatInsertPt(ConstUser.Inst, |
| 497 | ConstUser.OpndIdx); |
| 498 | Mat = BinaryOperator::Create(Instruction::Add, Base, Offset, |
| 499 | "const_mat", InsertionPt); |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 500 | |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 501 | DEBUG(dbgs() << "Materialize constant (" << *Base->getOperand(0) |
| 502 | << " + " << *Offset << ") in BB " |
| 503 | << Mat->getParent()->getName() << '\n' << *Mat << '\n'); |
| 504 | Mat->setDebugLoc(ConstUser.Inst->getDebugLoc()); |
| 505 | } |
| 506 | Value *Opnd = ConstUser.Inst->getOperand(ConstUser.OpndIdx); |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 507 | |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 508 | // Visit constant integer. |
| 509 | if (isa<ConstantInt>(Opnd)) { |
| 510 | DEBUG(dbgs() << "Update: " << *ConstUser.Inst << '\n'); |
Juergen Ributzka | e802d50 | 2014-03-22 01:49:27 +0000 | [diff] [blame] | 511 | if (!updateOperand(ConstUser.Inst, ConstUser.OpndIdx, Mat) && Offset) |
| 512 | Mat->eraseFromParent(); |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 513 | DEBUG(dbgs() << "To : " << *ConstUser.Inst << '\n'); |
Juergen Ributzka | 6dab520 | 2014-03-20 19:55:52 +0000 | [diff] [blame] | 514 | return; |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 515 | } |
Juergen Ributzka | 6dab520 | 2014-03-20 19:55:52 +0000 | [diff] [blame] | 516 | |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 517 | // Visit cast instruction. |
| 518 | if (auto CastInst = dyn_cast<Instruction>(Opnd)) { |
| 519 | assert(CastInst->isCast() && "Expected an cast instruction!"); |
| 520 | // Check if we already have visited this cast instruction before to avoid |
| 521 | // unnecessary cloning. |
| 522 | Instruction *&ClonedCastInst = ClonedCastMap[CastInst]; |
| 523 | if (!ClonedCastInst) { |
| 524 | ClonedCastInst = CastInst->clone(); |
| 525 | ClonedCastInst->setOperand(0, Mat); |
| 526 | ClonedCastInst->insertAfter(CastInst); |
| 527 | // Use the same debug location as the original cast instruction. |
| 528 | ClonedCastInst->setDebugLoc(CastInst->getDebugLoc()); |
| 529 | DEBUG(dbgs() << "Clone instruction: " << *ClonedCastInst << '\n' |
| 530 | << "To : " << *CastInst << '\n'); |
Juergen Ributzka | 4635793 | 2014-03-20 20:17:13 +0000 | [diff] [blame] | 531 | } |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 532 | |
| 533 | DEBUG(dbgs() << "Update: " << *ConstUser.Inst << '\n'); |
Juergen Ributzka | e802d50 | 2014-03-22 01:49:27 +0000 | [diff] [blame] | 534 | updateOperand(ConstUser.Inst, ConstUser.OpndIdx, ClonedCastInst); |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 535 | DEBUG(dbgs() << "To : " << *ConstUser.Inst << '\n'); |
| 536 | return; |
Juergen Ributzka | 4635793 | 2014-03-20 20:17:13 +0000 | [diff] [blame] | 537 | } |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 538 | |
| 539 | // Visit constant expression. |
| 540 | if (auto ConstExpr = dyn_cast<ConstantExpr>(Opnd)) { |
| 541 | Instruction *ConstExprInst = ConstExpr->getAsInstruction(); |
| 542 | ConstExprInst->setOperand(0, Mat); |
| 543 | ConstExprInst->insertBefore(findMatInsertPt(ConstUser.Inst, |
| 544 | ConstUser.OpndIdx)); |
| 545 | |
| 546 | // Use the same debug location as the instruction we are about to update. |
| 547 | ConstExprInst->setDebugLoc(ConstUser.Inst->getDebugLoc()); |
| 548 | |
| 549 | DEBUG(dbgs() << "Create instruction: " << *ConstExprInst << '\n' |
| 550 | << "From : " << *ConstExpr << '\n'); |
| 551 | DEBUG(dbgs() << "Update: " << *ConstUser.Inst << '\n'); |
Juergen Ributzka | e802d50 | 2014-03-22 01:49:27 +0000 | [diff] [blame] | 552 | if (!updateOperand(ConstUser.Inst, ConstUser.OpndIdx, ConstExprInst)) { |
| 553 | ConstExprInst->eraseFromParent(); |
| 554 | if (Offset) |
| 555 | Mat->eraseFromParent(); |
| 556 | } |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 557 | DEBUG(dbgs() << "To : " << *ConstUser.Inst << '\n'); |
| 558 | return; |
Juergen Ributzka | 4c8a025 | 2014-02-08 00:20:45 +0000 | [diff] [blame] | 559 | } |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | /// \brief Hoist and hide the base constant behind a bitcast and emit |
| 563 | /// materialization code for derived constants. |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 564 | bool ConstantHoisting::emitBaseConstants() { |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 565 | bool MadeChange = false; |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 566 | for (auto const &ConstInfo : ConstantVec) { |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 567 | // Hoist and hide the base constant behind a bitcast. |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 568 | Instruction *IP = findConstantInsertionPoint(ConstInfo); |
| 569 | IntegerType *Ty = ConstInfo.BaseConstant->getType(); |
| 570 | Instruction *Base = |
| 571 | new BitCastInst(ConstInfo.BaseConstant, Ty, "const", IP); |
| 572 | DEBUG(dbgs() << "Hoist constant (" << *ConstInfo.BaseConstant << ") to BB " |
| 573 | << IP->getParent()->getName() << '\n' << *Base << '\n'); |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 574 | NumConstantsHoisted++; |
| 575 | |
| 576 | // Emit materialization code for all rebased constants. |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 577 | for (auto const &RCI : ConstInfo.RebasedConstants) { |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 578 | NumConstantsRebased++; |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 579 | for (auto const &U : RCI.Uses) |
| 580 | emitBaseConstants(Base, RCI.Offset, U); |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | // Use the same debug location as the last user of the constant. |
| 584 | assert(!Base->use_empty() && "The use list is empty!?"); |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 585 | assert(isa<Instruction>(Base->user_back()) && |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 586 | "All uses should be instructions."); |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 587 | Base->setDebugLoc(cast<Instruction>(Base->user_back())->getDebugLoc()); |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 588 | |
| 589 | // Correct for base constant, which we counted above too. |
| 590 | NumConstantsRebased--; |
| 591 | MadeChange = true; |
| 592 | } |
| 593 | return MadeChange; |
| 594 | } |
| 595 | |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 596 | /// \brief Check all cast instructions we made a copy of and remove them if they |
| 597 | /// have no more users. |
| 598 | void ConstantHoisting::deleteDeadCastInst() const { |
| 599 | for (auto const &I : ClonedCastMap) |
| 600 | if (I.first->use_empty()) |
Juergen Ributzka | e474752 | 2014-03-22 01:49:30 +0000 | [diff] [blame] | 601 | I.first->eraseFromParent(); |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 602 | } |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 603 | |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 604 | /// \brief Optimize expensive integer constants in the given function. |
| 605 | bool ConstantHoisting::optimizeConstants(Function &Fn) { |
Juergen Ributzka | 4635793 | 2014-03-20 20:17:13 +0000 | [diff] [blame] | 606 | // Collect all constant candidates. |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 607 | collectConstantCandidates(Fn); |
Juergen Ributzka | 4635793 | 2014-03-20 20:17:13 +0000 | [diff] [blame] | 608 | |
Juergen Ributzka | a29a5b8 | 2014-03-21 06:04:30 +0000 | [diff] [blame] | 609 | // There are no constant candidates to worry about. |
| 610 | if (ConstCandVec.empty()) |
| 611 | return false; |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 612 | |
| 613 | // Combine constants that can be easily materialized with an add from a common |
| 614 | // base constant. |
Juergen Ributzka | b8489b3 | 2014-03-21 06:04:33 +0000 | [diff] [blame] | 615 | findBaseConstants(); |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 616 | |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 617 | // There are no constants to emit. |
| 618 | if (ConstantVec.empty()) |
| 619 | return false; |
| 620 | |
Juergen Ributzka | f0dff49 | 2014-03-21 06:04:45 +0000 | [diff] [blame] | 621 | // Finally hoist the base constant and emit materialization code for dependent |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 622 | // constants. |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 623 | bool MadeChange = emitBaseConstants(); |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 624 | |
Juergen Ributzka | 5429c06 | 2014-03-21 06:04:36 +0000 | [diff] [blame] | 625 | // Cleanup dead instructions. |
| 626 | deleteDeadCastInst(); |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 627 | |
| 628 | return MadeChange; |
| 629 | } |