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