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