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