Chris Lattner | 81ae3f2 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 1 | //===-- LoopIdiomRecognize.cpp - Loop idiom recognition -------------------===// |
| 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 implements an idiom recognizer that transforms simple loops into a |
| 11 | // non-loop form. In cases that this kicks in, it can be a significant |
| 12 | // performance win. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0469e01 | 2011-01-02 18:32:09 +0000 | [diff] [blame] | 15 | // |
| 16 | // TODO List: |
| 17 | // |
| 18 | // Future loop memory idioms to recognize: |
Chandler Carruth | 099f5cb0 | 2012-11-02 08:33:25 +0000 | [diff] [blame] | 19 | // memcmp, memmove, strlen, etc. |
Chris Lattner | 0469e01 | 2011-01-02 18:32:09 +0000 | [diff] [blame] | 20 | // Future floating point idioms to recognize in -ffast-math mode: |
| 21 | // fpowi |
| 22 | // Future integer operation idioms to recognize: |
| 23 | // ctpop, ctlz, cttz |
| 24 | // |
| 25 | // Beware that isel's default lowering for ctpop is highly inefficient for |
| 26 | // i64 and larger types when i64 is legal and the value has few bits set. It |
| 27 | // would be good to enhance isel to emit a loop for ctpop in this case. |
| 28 | // |
| 29 | // We should enhance the memset/memcpy recognition to handle multiple stores in |
| 30 | // the loop. This would handle things like: |
| 31 | // void foo(_Complex float *P) |
| 32 | // for (i) { __real__(*P) = 0; __imag__(*P) = 0; } |
Chris Lattner | 8fac5db | 2011-01-02 23:19:45 +0000 | [diff] [blame] | 33 | // |
Chris Lattner | bc661d6 | 2011-02-21 02:08:54 +0000 | [diff] [blame] | 34 | // We should enhance this to handle negative strides through memory. |
| 35 | // Alternatively (and perhaps better) we could rely on an earlier pass to force |
| 36 | // forward iteration through memory, which is generally better for cache |
| 37 | // behavior. Negative strides *do* happen for memset/memcpy loops. |
| 38 | // |
Chris Lattner | 02a9776 | 2011-01-03 01:10:08 +0000 | [diff] [blame] | 39 | // This could recognize common matrix multiplies and dot product idioms and |
Chris Lattner | 8fac5db | 2011-01-02 23:19:45 +0000 | [diff] [blame] | 40 | // replace them with calls to BLAS (if linked in??). |
| 41 | // |
Chris Lattner | 0469e01 | 2011-01-02 18:32:09 +0000 | [diff] [blame] | 42 | //===----------------------------------------------------------------------===// |
Chris Lattner | 81ae3f2 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 43 | |
Chris Lattner | 81ae3f2 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 44 | #include "llvm/Transforms/Scalar.h" |
Chandler Carruth | aafe091 | 2012-06-29 12:38:19 +0000 | [diff] [blame] | 45 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | cb18bfa | 2010-12-27 18:39:08 +0000 | [diff] [blame] | 46 | #include "llvm/Analysis/AliasAnalysis.h" |
Chris Lattner | 81ae3f2 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 47 | #include "llvm/Analysis/LoopPass.h" |
Chris Lattner | 29e14ed | 2010-12-26 23:42:51 +0000 | [diff] [blame] | 48 | #include "llvm/Analysis/ScalarEvolutionExpander.h" |
Chandler Carruth | aafe091 | 2012-06-29 12:38:19 +0000 | [diff] [blame] | 49 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 50 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Chandler Carruth | d3e7355 | 2013-01-07 03:08:10 +0000 | [diff] [blame] | 51 | #include "llvm/Analysis/TargetTransformInfo.h" |
Chris Lattner | 7c5f9c3 | 2010-12-26 20:45:45 +0000 | [diff] [blame] | 52 | #include "llvm/Analysis/ValueTracking.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 53 | #include "llvm/IR/DataLayout.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 54 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 55 | #include "llvm/IR/IRBuilder.h" |
| 56 | #include "llvm/IR/IntrinsicInst.h" |
| 57 | #include "llvm/IR/Module.h" |
Chandler Carruth | aafe091 | 2012-06-29 12:38:19 +0000 | [diff] [blame] | 58 | #include "llvm/Support/Debug.h" |
| 59 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | b9fe685 | 2010-12-27 00:03:23 +0000 | [diff] [blame] | 60 | #include "llvm/Transforms/Utils/Local.h" |
Chris Lattner | 81ae3f2 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 61 | using namespace llvm; |
| 62 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 63 | #define DEBUG_TYPE "loop-idiom" |
| 64 | |
Chandler Carruth | 099f5cb0 | 2012-11-02 08:33:25 +0000 | [diff] [blame] | 65 | STATISTIC(NumMemSet, "Number of memset's formed from loop stores"); |
| 66 | STATISTIC(NumMemCpy, "Number of memcpy's formed from loop load+stores"); |
Chris Lattner | 81ae3f2 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 67 | |
| 68 | namespace { |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 69 | |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 70 | class LoopIdiomRecognize; |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 71 | |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 72 | /// This class is to recoginize idioms of population-count conducted in |
| 73 | /// a noncountable loop. Currently it only recognizes this pattern: |
| 74 | /// \code |
| 75 | /// while(x) {cnt++; ...; x &= x - 1; ...} |
| 76 | /// \endcode |
| 77 | class NclPopcountRecognize { |
| 78 | LoopIdiomRecognize &LIR; |
| 79 | Loop *CurLoop; |
| 80 | BasicBlock *PreCondBB; |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 81 | |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 82 | typedef IRBuilder<> IRBuilderTy; |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 83 | |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 84 | public: |
| 85 | explicit NclPopcountRecognize(LoopIdiomRecognize &TheLIR); |
| 86 | bool recognize(); |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 87 | |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 88 | private: |
| 89 | /// Take a glimpse of the loop to see if we need to go ahead recoginizing |
| 90 | /// the idiom. |
| 91 | bool preliminaryScreen(); |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 92 | |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 93 | /// Check if the given conditional branch is based on the comparison |
| 94 | /// between a variable and zero, and if the variable is non-zero, the |
| 95 | /// control yields to the loop entry. If the branch matches the behavior, |
| 96 | /// the variable involved in the comparion is returned. This function will |
| 97 | /// be called to see if the precondition and postcondition of the loop |
| 98 | /// are in desirable form. |
| 99 | Value *matchCondition(BranchInst *Br, BasicBlock *NonZeroTarget) const; |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 100 | |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 101 | /// Return true iff the idiom is detected in the loop. and 1) \p CntInst |
| 102 | /// is set to the instruction counting the population bit. 2) \p CntPhi |
| 103 | /// is set to the corresponding phi node. 3) \p Var is set to the value |
| 104 | /// whose population bits are being counted. |
| 105 | bool detectIdiom(Instruction *&CntInst, PHINode *&CntPhi, Value *&Var) const; |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 106 | |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 107 | /// Insert ctpop intrinsic function and some obviously dead instructions. |
| 108 | void transform(Instruction *CntInst, PHINode *CntPhi, Value *Var); |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 109 | |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 110 | /// Create llvm.ctpop.* intrinsic function. |
| 111 | CallInst *createPopcntIntrinsic(IRBuilderTy &IRB, Value *Val, DebugLoc DL); |
| 112 | }; |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 113 | |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 114 | class LoopIdiomRecognize : public LoopPass { |
| 115 | Loop *CurLoop; |
| 116 | DominatorTree *DT; |
| 117 | ScalarEvolution *SE; |
| 118 | TargetLibraryInfo *TLI; |
| 119 | const TargetTransformInfo *TTI; |
Chris Lattner | 81ae3f2 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 120 | |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 121 | public: |
| 122 | static char ID; |
| 123 | explicit LoopIdiomRecognize() : LoopPass(ID) { |
| 124 | initializeLoopIdiomRecognizePass(*PassRegistry::getPassRegistry()); |
| 125 | DT = nullptr; |
| 126 | SE = nullptr; |
| 127 | TLI = nullptr; |
| 128 | TTI = nullptr; |
| 129 | } |
Chris Lattner | 81ae3f2 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 130 | |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 131 | bool runOnLoop(Loop *L, LPPassManager &LPM) override; |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 132 | |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 133 | /// This transformation requires natural loop information & requires that |
| 134 | /// loop preheaders be inserted into the CFG. |
| 135 | /// |
| 136 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 137 | AU.addRequired<LoopInfoWrapperPass>(); |
| 138 | AU.addPreserved<LoopInfoWrapperPass>(); |
| 139 | AU.addRequiredID(LoopSimplifyID); |
| 140 | AU.addPreservedID(LoopSimplifyID); |
| 141 | AU.addRequiredID(LCSSAID); |
| 142 | AU.addPreservedID(LCSSAID); |
| 143 | AU.addRequired<AliasAnalysis>(); |
| 144 | AU.addPreserved<AliasAnalysis>(); |
| 145 | AU.addRequired<ScalarEvolution>(); |
| 146 | AU.addPreserved<ScalarEvolution>(); |
| 147 | AU.addPreserved<DominatorTreeWrapperPass>(); |
| 148 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 149 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
| 150 | AU.addRequired<TargetTransformInfoWrapperPass>(); |
| 151 | } |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 152 | |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 153 | DominatorTree *getDominatorTree() { |
| 154 | return DT ? DT |
| 155 | : (DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree()); |
| 156 | } |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 157 | |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 158 | ScalarEvolution *getScalarEvolution() { |
| 159 | return SE ? SE : (SE = &getAnalysis<ScalarEvolution>()); |
| 160 | } |
Chandler Carruth | b98f63d | 2015-01-15 10:41:28 +0000 | [diff] [blame] | 161 | |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 162 | TargetLibraryInfo *getTargetLibraryInfo() { |
| 163 | if (!TLI) |
| 164 | TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 165 | |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 166 | return TLI; |
| 167 | } |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 168 | |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 169 | const TargetTransformInfo *getTargetTransformInfo() { |
| 170 | return TTI ? TTI |
| 171 | : (TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI( |
| 172 | *CurLoop->getHeader()->getParent())); |
| 173 | } |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 174 | |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 175 | Loop *getLoop() const { return CurLoop; } |
| 176 | |
| 177 | private: |
Chandler Carruth | d9c6070 | 2015-08-13 00:10:03 +0000 | [diff] [blame] | 178 | /// \name Countable Loop Idiom Handling |
| 179 | /// @{ |
| 180 | |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 181 | bool runOnCountableLoop(); |
Chandler Carruth | d9c6070 | 2015-08-13 00:10:03 +0000 | [diff] [blame] | 182 | bool runOnLoopBlock(BasicBlock *BB, const SCEV *BECount, |
| 183 | SmallVectorImpl<BasicBlock *> &ExitBlocks); |
| 184 | |
| 185 | bool processLoopStore(StoreInst *SI, const SCEV *BECount); |
| 186 | bool processLoopMemSet(MemSetInst *MSI, const SCEV *BECount); |
| 187 | |
| 188 | bool processLoopStridedStore(Value *DestPtr, unsigned StoreSize, |
| 189 | unsigned StoreAlignment, Value *SplatValue, |
| 190 | Instruction *TheStore, const SCEVAddRecExpr *Ev, |
| 191 | const SCEV *BECount); |
| 192 | bool processLoopStoreOfLoopLoad(StoreInst *SI, unsigned StoreSize, |
| 193 | const SCEVAddRecExpr *StoreEv, |
| 194 | const SCEVAddRecExpr *LoadEv, |
| 195 | const SCEV *BECount); |
| 196 | |
| 197 | /// @} |
| 198 | /// \name Noncountable Loop Idiom Handling |
| 199 | /// @{ |
| 200 | |
| 201 | bool runOnNoncountableLoop(); |
| 202 | |
| 203 | /// @} |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 204 | }; |
| 205 | |
| 206 | } // End anonymous namespace. |
Chris Lattner | 81ae3f2 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 207 | |
| 208 | char LoopIdiomRecognize::ID = 0; |
| 209 | INITIALIZE_PASS_BEGIN(LoopIdiomRecognize, "loop-idiom", "Recognize loop idioms", |
| 210 | false, false) |
Chandler Carruth | 4f8f307 | 2015-01-17 14:16:18 +0000 | [diff] [blame] | 211 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass) |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 212 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
Chris Lattner | 81ae3f2 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 213 | INITIALIZE_PASS_DEPENDENCY(LoopSimplify) |
| 214 | INITIALIZE_PASS_DEPENDENCY(LCSSA) |
| 215 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolution) |
Chandler Carruth | b98f63d | 2015-01-15 10:41:28 +0000 | [diff] [blame] | 216 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) |
Chris Lattner | cb18bfa | 2010-12-27 18:39:08 +0000 | [diff] [blame] | 217 | INITIALIZE_AG_DEPENDENCY(AliasAnalysis) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 218 | INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass) |
Chris Lattner | 81ae3f2 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 219 | INITIALIZE_PASS_END(LoopIdiomRecognize, "loop-idiom", "Recognize loop idioms", |
| 220 | false, false) |
| 221 | |
| 222 | Pass *llvm::createLoopIdiomPass() { return new LoopIdiomRecognize(); } |
| 223 | |
Chris Lattner | c4ca7ab | 2011-05-22 17:39:56 +0000 | [diff] [blame] | 224 | /// deleteDeadInstruction - Delete this instruction. Before we do, go through |
Chris Lattner | b9fe685 | 2010-12-27 00:03:23 +0000 | [diff] [blame] | 225 | /// and zero out all the operands of this instruction. If any of them become |
| 226 | /// dead, delete them and the computation tree that feeds them. |
| 227 | /// |
Benjamin Kramer | f094d77 | 2015-02-07 21:37:08 +0000 | [diff] [blame] | 228 | static void deleteDeadInstruction(Instruction *I, |
Benjamin Kramer | 8bcc971 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 229 | const TargetLibraryInfo *TLI) { |
Benjamin Kramer | f094d77 | 2015-02-07 21:37:08 +0000 | [diff] [blame] | 230 | SmallVector<Value *, 16> Operands(I->value_op_begin(), I->value_op_end()); |
| 231 | I->replaceAllUsesWith(UndefValue::get(I->getType())); |
| 232 | I->eraseFromParent(); |
| 233 | for (Value *Op : Operands) |
| 234 | RecursivelyDeleteTriviallyDeadInstructions(Op, TLI); |
Chandler Carruth | 099f5cb0 | 2012-11-02 08:33:25 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 237 | //===----------------------------------------------------------------------===// |
| 238 | // |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 239 | // Implementation of NclPopcountRecognize |
| 240 | // |
| 241 | //===----------------------------------------------------------------------===// |
| 242 | |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 243 | NclPopcountRecognize::NclPopcountRecognize(LoopIdiomRecognize &TheLIR) |
| 244 | : LIR(TheLIR), CurLoop(TheLIR.getLoop()), PreCondBB(nullptr) {} |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 245 | |
| 246 | bool NclPopcountRecognize::preliminaryScreen() { |
Chandler Carruth | 6fe147f | 2013-01-05 10:00:09 +0000 | [diff] [blame] | 247 | const TargetTransformInfo *TTI = LIR.getTargetTransformInfo(); |
Chandler Carruth | 50a36cd | 2013-01-07 03:16:03 +0000 | [diff] [blame] | 248 | if (TTI->getPopcntSupport(32) != TargetTransformInfo::PSK_FastHardware) |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 249 | return false; |
| 250 | |
Robert Wilhelm | 2788d3e | 2013-09-28 13:42:22 +0000 | [diff] [blame] | 251 | // Counting population are usually conducted by few arithmetic instructions. |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 252 | // Such instructions can be easilly "absorbed" by vacant slots in a |
| 253 | // non-compact loop. Therefore, recognizing popcount idiom only makes sense |
| 254 | // in a compact loop. |
| 255 | |
| 256 | // Give up if the loop has multiple blocks or multiple backedges. |
| 257 | if (CurLoop->getNumBackEdges() != 1 || CurLoop->getNumBlocks() != 1) |
| 258 | return false; |
| 259 | |
| 260 | BasicBlock *LoopBody = *(CurLoop->block_begin()); |
| 261 | if (LoopBody->size() >= 20) { |
| 262 | // The loop is too big, bail out. |
| 263 | return false; |
| 264 | } |
| 265 | |
Chandler Carruth | be158b1 | 2015-08-12 23:55:56 +0000 | [diff] [blame] | 266 | // It should have a preheader containing nothing but an unconditional branch. |
| 267 | BasicBlock *PH = CurLoop->getLoopPreheader(); |
| 268 | if (!PH) |
| 269 | return false; |
| 270 | if (&PH->front() != PH->getTerminator()) |
| 271 | return false; |
| 272 | auto *EntryBI = dyn_cast<BranchInst>(PH->getTerminator()); |
| 273 | if (!EntryBI || EntryBI->isConditional()) |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 274 | return false; |
| 275 | |
| 276 | // It should have a precondition block where the generated popcount instrinsic |
Chandler Carruth | be158b1 | 2015-08-12 23:55:56 +0000 | [diff] [blame] | 277 | // function can be inserted. |
| 278 | PreCondBB = PH->getSinglePredecessor(); |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 279 | if (!PreCondBB) |
| 280 | return false; |
Chandler Carruth | be158b1 | 2015-08-12 23:55:56 +0000 | [diff] [blame] | 281 | auto *PreCondBI = dyn_cast<BranchInst>(PreCondBB->getTerminator()); |
| 282 | if (!PreCondBI || PreCondBI->isUnconditional()) |
| 283 | return false; |
Matt Arsenault | fb18323 | 2013-07-22 18:59:58 +0000 | [diff] [blame] | 284 | |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 285 | return true; |
| 286 | } |
| 287 | |
Jim Grosbach | 708f80f | 2014-04-29 22:41:58 +0000 | [diff] [blame] | 288 | Value *NclPopcountRecognize::matchCondition(BranchInst *Br, |
| 289 | BasicBlock *LoopEntry) const { |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 290 | if (!Br || !Br->isConditional()) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 291 | return nullptr; |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 292 | |
| 293 | ICmpInst *Cond = dyn_cast<ICmpInst>(Br->getCondition()); |
| 294 | if (!Cond) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 295 | return nullptr; |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 296 | |
| 297 | ConstantInt *CmpZero = dyn_cast<ConstantInt>(Cond->getOperand(1)); |
| 298 | if (!CmpZero || !CmpZero->isZero()) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 299 | return nullptr; |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 300 | |
| 301 | ICmpInst::Predicate Pred = Cond->getPredicate(); |
| 302 | if ((Pred == ICmpInst::ICMP_NE && Br->getSuccessor(0) == LoopEntry) || |
| 303 | (Pred == ICmpInst::ICMP_EQ && Br->getSuccessor(1) == LoopEntry)) |
| 304 | return Cond->getOperand(0); |
| 305 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 306 | return nullptr; |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 309 | bool NclPopcountRecognize::detectIdiom(Instruction *&CntInst, PHINode *&CntPhi, |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 310 | Value *&Var) const { |
| 311 | // Following code tries to detect this idiom: |
| 312 | // |
| 313 | // if (x0 != 0) |
| 314 | // goto loop-exit // the precondition of the loop |
| 315 | // cnt0 = init-val; |
| 316 | // do { |
| 317 | // x1 = phi (x0, x2); |
| 318 | // cnt1 = phi(cnt0, cnt2); |
| 319 | // |
| 320 | // cnt2 = cnt1 + 1; |
| 321 | // ... |
| 322 | // x2 = x1 & (x1 - 1); |
| 323 | // ... |
| 324 | // } while(x != 0); |
| 325 | // |
| 326 | // loop-exit: |
| 327 | // |
| 328 | |
| 329 | // step 1: Check to see if the look-back branch match this pattern: |
| 330 | // "if (a!=0) goto loop-entry". |
| 331 | BasicBlock *LoopEntry; |
| 332 | Instruction *DefX2, *CountInst; |
| 333 | Value *VarX1, *VarX0; |
| 334 | PHINode *PhiX, *CountPhi; |
| 335 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 336 | DefX2 = CountInst = nullptr; |
| 337 | VarX1 = VarX0 = nullptr; |
| 338 | PhiX = CountPhi = nullptr; |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 339 | LoopEntry = *(CurLoop->block_begin()); |
| 340 | |
| 341 | // step 1: Check if the loop-back branch is in desirable form. |
| 342 | { |
Chandler Carruth | be158b1 | 2015-08-12 23:55:56 +0000 | [diff] [blame] | 343 | if (Value *T = matchCondition( |
| 344 | dyn_cast<BranchInst>(LoopEntry->getTerminator()), LoopEntry)) |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 345 | DefX2 = dyn_cast<Instruction>(T); |
| 346 | else |
| 347 | return false; |
| 348 | } |
| 349 | |
| 350 | // step 2: detect instructions corresponding to "x2 = x1 & (x1 - 1)" |
| 351 | { |
Shuxin Yang | c5c730b | 2013-01-10 23:32:01 +0000 | [diff] [blame] | 352 | if (!DefX2 || DefX2->getOpcode() != Instruction::And) |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 353 | return false; |
| 354 | |
| 355 | BinaryOperator *SubOneOp; |
| 356 | |
| 357 | if ((SubOneOp = dyn_cast<BinaryOperator>(DefX2->getOperand(0)))) |
| 358 | VarX1 = DefX2->getOperand(1); |
| 359 | else { |
| 360 | VarX1 = DefX2->getOperand(0); |
| 361 | SubOneOp = dyn_cast<BinaryOperator>(DefX2->getOperand(1)); |
| 362 | } |
| 363 | if (!SubOneOp) |
| 364 | return false; |
| 365 | |
| 366 | Instruction *SubInst = cast<Instruction>(SubOneOp); |
| 367 | ConstantInt *Dec = dyn_cast<ConstantInt>(SubInst->getOperand(1)); |
| 368 | if (!Dec || |
| 369 | !((SubInst->getOpcode() == Instruction::Sub && Dec->isOne()) || |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 370 | (SubInst->getOpcode() == Instruction::Add && |
| 371 | Dec->isAllOnesValue()))) { |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 372 | return false; |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | // step 3: Check the recurrence of variable X |
| 377 | { |
| 378 | PhiX = dyn_cast<PHINode>(VarX1); |
| 379 | if (!PhiX || |
| 380 | (PhiX->getOperand(0) != DefX2 && PhiX->getOperand(1) != DefX2)) { |
| 381 | return false; |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | // step 4: Find the instruction which count the population: cnt2 = cnt1 + 1 |
| 386 | { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 387 | CountInst = nullptr; |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 388 | for (BasicBlock::iterator Iter = LoopEntry->getFirstNonPHI(), |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 389 | IterE = LoopEntry->end(); |
| 390 | Iter != IterE; Iter++) { |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 391 | Instruction *Inst = Iter; |
| 392 | if (Inst->getOpcode() != Instruction::Add) |
| 393 | continue; |
| 394 | |
| 395 | ConstantInt *Inc = dyn_cast<ConstantInt>(Inst->getOperand(1)); |
| 396 | if (!Inc || !Inc->isOne()) |
| 397 | continue; |
| 398 | |
| 399 | PHINode *Phi = dyn_cast<PHINode>(Inst->getOperand(0)); |
| 400 | if (!Phi || Phi->getParent() != LoopEntry) |
| 401 | continue; |
| 402 | |
| 403 | // Check if the result of the instruction is live of the loop. |
| 404 | bool LiveOutLoop = false; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 405 | for (User *U : Inst->users()) { |
| 406 | if ((cast<Instruction>(U))->getParent() != LoopEntry) { |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 407 | LiveOutLoop = true; |
| 408 | break; |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | |
| 412 | if (LiveOutLoop) { |
| 413 | CountInst = Inst; |
| 414 | CountPhi = Phi; |
| 415 | break; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | if (!CountInst) |
| 420 | return false; |
| 421 | } |
| 422 | |
| 423 | // step 5: check if the precondition is in this form: |
| 424 | // "if (x != 0) goto loop-head ; else goto somewhere-we-don't-care;" |
| 425 | { |
Chandler Carruth | be158b1 | 2015-08-12 23:55:56 +0000 | [diff] [blame] | 426 | auto *PreCondBr = dyn_cast<BranchInst>(PreCondBB->getTerminator()); |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 427 | Value *T = matchCondition(PreCondBr, CurLoop->getLoopPreheader()); |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 428 | if (T != PhiX->getOperand(0) && T != PhiX->getOperand(1)) |
| 429 | return false; |
| 430 | |
| 431 | CntInst = CountInst; |
| 432 | CntPhi = CountPhi; |
| 433 | Var = T; |
| 434 | } |
| 435 | |
| 436 | return true; |
| 437 | } |
| 438 | |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 439 | void NclPopcountRecognize::transform(Instruction *CntInst, PHINode *CntPhi, |
| 440 | Value *Var) { |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 441 | |
| 442 | ScalarEvolution *SE = LIR.getScalarEvolution(); |
| 443 | TargetLibraryInfo *TLI = LIR.getTargetLibraryInfo(); |
| 444 | BasicBlock *PreHead = CurLoop->getLoopPreheader(); |
Chandler Carruth | be158b1 | 2015-08-12 23:55:56 +0000 | [diff] [blame] | 445 | auto *PreCondBr = dyn_cast<BranchInst>(PreCondBB->getTerminator()); |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 446 | const DebugLoc DL = CntInst->getDebugLoc(); |
| 447 | |
| 448 | // Assuming before transformation, the loop is following: |
| 449 | // if (x) // the precondition |
| 450 | // do { cnt++; x &= x - 1; } while(x); |
Matt Arsenault | fb18323 | 2013-07-22 18:59:58 +0000 | [diff] [blame] | 451 | |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 452 | // Step 1: Insert the ctpop instruction at the end of the precondition block |
| 453 | IRBuilderTy Builder(PreCondBr); |
| 454 | Value *PopCnt, *PopCntZext, *NewCount, *TripCnt; |
| 455 | { |
| 456 | PopCnt = createPopcntIntrinsic(Builder, Var, DL); |
| 457 | NewCount = PopCntZext = |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 458 | Builder.CreateZExtOrTrunc(PopCnt, cast<IntegerType>(CntPhi->getType())); |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 459 | |
| 460 | if (NewCount != PopCnt) |
| 461 | (cast<Instruction>(NewCount))->setDebugLoc(DL); |
| 462 | |
| 463 | // TripCnt is exactly the number of iterations the loop has |
| 464 | TripCnt = NewCount; |
| 465 | |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 466 | // If the population counter's initial value is not zero, insert Add Inst. |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 467 | Value *CntInitVal = CntPhi->getIncomingValueForBlock(PreHead); |
| 468 | ConstantInt *InitConst = dyn_cast<ConstantInt>(CntInitVal); |
| 469 | if (!InitConst || !InitConst->isZero()) { |
| 470 | NewCount = Builder.CreateAdd(NewCount, CntInitVal); |
| 471 | (cast<Instruction>(NewCount))->setDebugLoc(DL); |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | // Step 2: Replace the precondition from "if(x == 0) goto loop-exit" to |
| 476 | // "if(NewCount == 0) loop-exit". Withtout this change, the intrinsic |
| 477 | // function would be partial dead code, and downstream passes will drag |
| 478 | // it back from the precondition block to the preheader. |
| 479 | { |
| 480 | ICmpInst *PreCond = cast<ICmpInst>(PreCondBr->getCondition()); |
| 481 | |
| 482 | Value *Opnd0 = PopCntZext; |
| 483 | Value *Opnd1 = ConstantInt::get(PopCntZext->getType(), 0); |
| 484 | if (PreCond->getOperand(0) != Var) |
| 485 | std::swap(Opnd0, Opnd1); |
| 486 | |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 487 | ICmpInst *NewPreCond = cast<ICmpInst>( |
| 488 | Builder.CreateICmp(PreCond->getPredicate(), Opnd0, Opnd1)); |
Pete Cooper | 90d95ed | 2015-07-13 21:25:33 +0000 | [diff] [blame] | 489 | PreCondBr->setCondition(NewPreCond); |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 490 | |
Benjamin Kramer | f094d77 | 2015-02-07 21:37:08 +0000 | [diff] [blame] | 491 | RecursivelyDeleteTriviallyDeadInstructions(PreCond, TLI); |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | // Step 3: Note that the population count is exactly the trip count of the |
| 495 | // loop in question, which enble us to to convert the loop from noncountable |
| 496 | // loop into a countable one. The benefit is twofold: |
| 497 | // |
| 498 | // - If the loop only counts population, the entire loop become dead after |
| 499 | // the transformation. It is lots easier to prove a countable loop dead |
| 500 | // than to prove a noncountable one. (In some C dialects, a infite loop |
| 501 | // isn't dead even if it computes nothing useful. In general, DCE needs |
| 502 | // to prove a noncountable loop finite before safely delete it.) |
| 503 | // |
| 504 | // - If the loop also performs something else, it remains alive. |
| 505 | // Since it is transformed to countable form, it can be aggressively |
| 506 | // optimized by some optimizations which are in general not applicable |
| 507 | // to a noncountable loop. |
| 508 | // |
| 509 | // After this step, this loop (conceptually) would look like following: |
| 510 | // newcnt = __builtin_ctpop(x); |
| 511 | // t = newcnt; |
| 512 | // if (x) |
| 513 | // do { cnt++; x &= x-1; t--) } while (t > 0); |
| 514 | BasicBlock *Body = *(CurLoop->block_begin()); |
| 515 | { |
Chandler Carruth | be158b1 | 2015-08-12 23:55:56 +0000 | [diff] [blame] | 516 | auto *LbBr = dyn_cast<BranchInst>(Body->getTerminator()); |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 517 | ICmpInst *LbCond = cast<ICmpInst>(LbBr->getCondition()); |
| 518 | Type *Ty = TripCnt->getType(); |
| 519 | |
| 520 | PHINode *TcPhi = PHINode::Create(Ty, 2, "tcphi", Body->begin()); |
| 521 | |
| 522 | Builder.SetInsertPoint(LbCond); |
| 523 | Value *Opnd1 = cast<Value>(TcPhi); |
| 524 | Value *Opnd2 = cast<Value>(ConstantInt::get(Ty, 1)); |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 525 | Instruction *TcDec = cast<Instruction>( |
| 526 | Builder.CreateSub(Opnd1, Opnd2, "tcdec", false, true)); |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 527 | |
| 528 | TcPhi->addIncoming(TripCnt, PreHead); |
| 529 | TcPhi->addIncoming(TcDec, Body); |
| 530 | |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 531 | CmpInst::Predicate Pred = |
| 532 | (LbBr->getSuccessor(0) == Body) ? CmpInst::ICMP_UGT : CmpInst::ICMP_SLE; |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 533 | LbCond->setPredicate(Pred); |
| 534 | LbCond->setOperand(0, TcDec); |
| 535 | LbCond->setOperand(1, cast<Value>(ConstantInt::get(Ty, 0))); |
| 536 | } |
| 537 | |
| 538 | // Step 4: All the references to the original population counter outside |
| 539 | // the loop are replaced with the NewCount -- the value returned from |
| 540 | // __builtin_ctpop(). |
Benjamin Kramer | f094d77 | 2015-02-07 21:37:08 +0000 | [diff] [blame] | 541 | CntInst->replaceUsesOutsideBlock(NewCount, Body); |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 542 | |
| 543 | // step 5: Forget the "non-computable" trip-count SCEV associated with the |
| 544 | // loop. The loop would otherwise not be deleted even if it becomes empty. |
| 545 | SE->forgetLoop(CurLoop); |
| 546 | } |
| 547 | |
Matt Arsenault | fb18323 | 2013-07-22 18:59:58 +0000 | [diff] [blame] | 548 | CallInst *NclPopcountRecognize::createPopcntIntrinsic(IRBuilderTy &IRBuilder, |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 549 | Value *Val, DebugLoc DL) { |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 550 | Value *Ops[] = {Val}; |
| 551 | Type *Tys[] = {Val->getType()}; |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 552 | |
| 553 | Module *M = (*(CurLoop->block_begin()))->getParent()->getParent(); |
| 554 | Value *Func = Intrinsic::getDeclaration(M, Intrinsic::ctpop, Tys); |
| 555 | CallInst *CI = IRBuilder.CreateCall(Func, Ops); |
| 556 | CI->setDebugLoc(DL); |
| 557 | |
| 558 | return CI; |
| 559 | } |
| 560 | |
| 561 | /// recognize - detect population count idiom in a non-countable loop. If |
| 562 | /// detected, transform the relevant code to popcount intrinsic function |
| 563 | /// call, and return true; otherwise, return false. |
| 564 | bool NclPopcountRecognize::recognize() { |
Chandler Carruth | 6fe147f | 2013-01-05 10:00:09 +0000 | [diff] [blame] | 565 | if (!LIR.getTargetTransformInfo()) |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 566 | return false; |
| 567 | |
| 568 | LIR.getScalarEvolution(); |
| 569 | |
| 570 | if (!preliminaryScreen()) |
| 571 | return false; |
| 572 | |
| 573 | Instruction *CntInst; |
| 574 | PHINode *CntPhi; |
| 575 | Value *Val; |
| 576 | if (!detectIdiom(CntInst, CntPhi, Val)) |
| 577 | return false; |
| 578 | |
| 579 | transform(CntInst, CntPhi, Val); |
| 580 | return true; |
| 581 | } |
| 582 | |
| 583 | //===----------------------------------------------------------------------===// |
| 584 | // |
| 585 | // Implementation of LoopIdiomRecognize |
| 586 | // |
| 587 | //===----------------------------------------------------------------------===// |
| 588 | |
Chandler Carruth | d9c6070 | 2015-08-13 00:10:03 +0000 | [diff] [blame] | 589 | bool LoopIdiomRecognize::runOnLoop(Loop *L, LPPassManager &LPM) { |
| 590 | if (skipOptnoneFunction(L)) |
| 591 | return false; |
| 592 | |
| 593 | CurLoop = L; |
| 594 | |
| 595 | // If the loop could not be converted to canonical form, it must have an |
| 596 | // indirectbr in it, just give up. |
| 597 | if (!L->getLoopPreheader()) |
| 598 | return false; |
| 599 | |
| 600 | // Disable loop idiom recognition if the function's name is a common idiom. |
| 601 | StringRef Name = L->getHeader()->getParent()->getName(); |
| 602 | if (Name == "memset" || Name == "memcpy") |
| 603 | return false; |
| 604 | |
| 605 | SE = &getAnalysis<ScalarEvolution>(); |
| 606 | if (SE->hasLoopInvariantBackedgeTakenCount(L)) |
| 607 | return runOnCountableLoop(); |
| 608 | return runOnNoncountableLoop(); |
| 609 | } |
| 610 | |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 611 | bool LoopIdiomRecognize::runOnCountableLoop() { |
| 612 | const SCEV *BECount = SE->getBackedgeTakenCount(CurLoop); |
Davide Italiano | 8ed0446 | 2015-05-11 21:02:34 +0000 | [diff] [blame] | 613 | assert(!isa<SCEVCouldNotCompute>(BECount) && |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 614 | "runOnCountableLoop() called on a loop without a predictable" |
| 615 | "backedge-taken count"); |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 616 | |
| 617 | // If this loop executes exactly one time, then it should be peeled, not |
| 618 | // optimized by this pass. |
| 619 | if (const SCEVConstant *BECst = dyn_cast<SCEVConstant>(BECount)) |
| 620 | if (BECst->getValue()->getValue() == 0) |
| 621 | return false; |
| 622 | |
Matt Arsenault | fb18323 | 2013-07-22 18:59:58 +0000 | [diff] [blame] | 623 | // set DT |
Shuxin Yang | 98c844f | 2013-01-02 18:26:31 +0000 | [diff] [blame] | 624 | (void)getDominatorTree(); |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 625 | |
Chandler Carruth | 4f8f307 | 2015-01-17 14:16:18 +0000 | [diff] [blame] | 626 | LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
Chandler Carruth | b98f63d | 2015-01-15 10:41:28 +0000 | [diff] [blame] | 627 | TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 628 | |
Matt Arsenault | fb18323 | 2013-07-22 18:59:58 +0000 | [diff] [blame] | 629 | // set TLI |
Shuxin Yang | 98c844f | 2013-01-02 18:26:31 +0000 | [diff] [blame] | 630 | (void)getTargetLibraryInfo(); |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 631 | |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 632 | SmallVector<BasicBlock *, 8> ExitBlocks; |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 633 | CurLoop->getUniqueExitBlocks(ExitBlocks); |
| 634 | |
| 635 | DEBUG(dbgs() << "loop-idiom Scanning: F[" |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 636 | << CurLoop->getHeader()->getParent()->getName() << "] Loop %" |
| 637 | << CurLoop->getHeader()->getName() << "\n"); |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 638 | |
| 639 | bool MadeChange = false; |
| 640 | // Scan all the blocks in the loop that are not in subloops. |
Davide Italiano | 95a77e8 | 2015-05-14 21:52:12 +0000 | [diff] [blame] | 641 | for (auto *BB : CurLoop->getBlocks()) { |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 642 | // Ignore blocks in subloops. |
Davide Italiano | 80625af | 2015-05-13 19:51:21 +0000 | [diff] [blame] | 643 | if (LI.getLoopFor(BB) != CurLoop) |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 644 | continue; |
| 645 | |
Davide Italiano | 80625af | 2015-05-13 19:51:21 +0000 | [diff] [blame] | 646 | MadeChange |= runOnLoopBlock(BB, BECount, ExitBlocks); |
Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 647 | } |
| 648 | return MadeChange; |
| 649 | } |
| 650 | |
Chris Lattner | 8455b6e | 2011-01-02 19:01:03 +0000 | [diff] [blame] | 651 | /// runOnLoopBlock - Process the specified block, which lives in a counted loop |
| 652 | /// with the specified backedge count. This block is known to be in the current |
| 653 | /// loop and not in any subloops. |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 654 | bool LoopIdiomRecognize::runOnLoopBlock( |
| 655 | BasicBlock *BB, const SCEV *BECount, |
| 656 | SmallVectorImpl<BasicBlock *> &ExitBlocks) { |
Chris Lattner | 8455b6e | 2011-01-02 19:01:03 +0000 | [diff] [blame] | 657 | // We can only promote stores in this block if they are unconditionally |
| 658 | // executed in the loop. For a block to be unconditionally executed, it has |
| 659 | // to dominate all the exit blocks of the loop. Verify this now. |
| 660 | for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) |
| 661 | if (!DT->dominates(BB, ExitBlocks[i])) |
| 662 | return false; |
Andrew Trick | 328b223 | 2011-03-14 16:48:10 +0000 | [diff] [blame] | 663 | |
Chris Lattner | 7c5f9c3 | 2010-12-26 20:45:45 +0000 | [diff] [blame] | 664 | bool MadeChange = false; |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 665 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E;) { |
Chris Lattner | a62b01d | 2011-01-04 07:27:30 +0000 | [diff] [blame] | 666 | Instruction *Inst = I++; |
| 667 | // Look for store instructions, which may be optimized to memset/memcpy. |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 668 | if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) { |
Chris Lattner | a62b01d | 2011-01-04 07:27:30 +0000 | [diff] [blame] | 669 | WeakVH InstPtr(I); |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 670 | if (!processLoopStore(SI, BECount)) |
| 671 | continue; |
Chris Lattner | a62b01d | 2011-01-04 07:27:30 +0000 | [diff] [blame] | 672 | MadeChange = true; |
Andrew Trick | 328b223 | 2011-03-14 16:48:10 +0000 | [diff] [blame] | 673 | |
Chris Lattner | a62b01d | 2011-01-04 07:27:30 +0000 | [diff] [blame] | 674 | // If processing the store invalidated our iterator, start over from the |
Chris Lattner | 8643810 | 2011-01-04 07:46:33 +0000 | [diff] [blame] | 675 | // top of the block. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 676 | if (!InstPtr) |
Chris Lattner | a62b01d | 2011-01-04 07:27:30 +0000 | [diff] [blame] | 677 | I = BB->begin(); |
| 678 | continue; |
| 679 | } |
Andrew Trick | 328b223 | 2011-03-14 16:48:10 +0000 | [diff] [blame] | 680 | |
Chris Lattner | 8643810 | 2011-01-04 07:46:33 +0000 | [diff] [blame] | 681 | // Look for memset instructions, which may be optimized to a larger memset. |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 682 | if (MemSetInst *MSI = dyn_cast<MemSetInst>(Inst)) { |
Chris Lattner | 8643810 | 2011-01-04 07:46:33 +0000 | [diff] [blame] | 683 | WeakVH InstPtr(I); |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 684 | if (!processLoopMemSet(MSI, BECount)) |
| 685 | continue; |
Chris Lattner | 8643810 | 2011-01-04 07:46:33 +0000 | [diff] [blame] | 686 | MadeChange = true; |
Andrew Trick | 328b223 | 2011-03-14 16:48:10 +0000 | [diff] [blame] | 687 | |
Chris Lattner | 8643810 | 2011-01-04 07:46:33 +0000 | [diff] [blame] | 688 | // If processing the memset invalidated our iterator, start over from the |
| 689 | // top of the block. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 690 | if (!InstPtr) |
Chris Lattner | 8643810 | 2011-01-04 07:46:33 +0000 | [diff] [blame] | 691 | I = BB->begin(); |
| 692 | continue; |
| 693 | } |
Chris Lattner | 7c5f9c3 | 2010-12-26 20:45:45 +0000 | [diff] [blame] | 694 | } |
Andrew Trick | 328b223 | 2011-03-14 16:48:10 +0000 | [diff] [blame] | 695 | |
Chris Lattner | 7c5f9c3 | 2010-12-26 20:45:45 +0000 | [diff] [blame] | 696 | return MadeChange; |
Chris Lattner | 81ae3f2 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 697 | } |
| 698 | |
Chris Lattner | 8643810 | 2011-01-04 07:46:33 +0000 | [diff] [blame] | 699 | /// processLoopStore - See if this store can be promoted to a memset or memcpy. |
Chris Lattner | 7c5f9c3 | 2010-12-26 20:45:45 +0000 | [diff] [blame] | 700 | bool LoopIdiomRecognize::processLoopStore(StoreInst *SI, const SCEV *BECount) { |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 701 | if (!SI->isSimple()) |
| 702 | return false; |
Chris Lattner | 8643810 | 2011-01-04 07:46:33 +0000 | [diff] [blame] | 703 | |
Chris Lattner | 7c5f9c3 | 2010-12-26 20:45:45 +0000 | [diff] [blame] | 704 | Value *StoredVal = SI->getValueOperand(); |
Chris Lattner | 29e14ed | 2010-12-26 23:42:51 +0000 | [diff] [blame] | 705 | Value *StorePtr = SI->getPointerOperand(); |
Andrew Trick | 328b223 | 2011-03-14 16:48:10 +0000 | [diff] [blame] | 706 | |
Chris Lattner | 65a699d | 2010-12-28 18:53:48 +0000 | [diff] [blame] | 707 | // Reject stores that are so large that they overflow an unsigned. |
Mehdi Amini | 46a4355 | 2015-03-04 18:43:29 +0000 | [diff] [blame] | 708 | auto &DL = CurLoop->getHeader()->getModule()->getDataLayout(); |
| 709 | uint64_t SizeInBits = DL.getTypeSizeInBits(StoredVal->getType()); |
Chris Lattner | 65a699d | 2010-12-28 18:53:48 +0000 | [diff] [blame] | 710 | if ((SizeInBits & 7) || (SizeInBits >> 32) != 0) |
Chris Lattner | 7c5f9c3 | 2010-12-26 20:45:45 +0000 | [diff] [blame] | 711 | return false; |
Andrew Trick | 328b223 | 2011-03-14 16:48:10 +0000 | [diff] [blame] | 712 | |
Chris Lattner | 7c5f9c3 | 2010-12-26 20:45:45 +0000 | [diff] [blame] | 713 | // See if the pointer expression is an AddRec like {base,+,1} on the current |
| 714 | // loop, which indicates a strided store. If we have something else, it's a |
| 715 | // random store we can't handle. |
Chris Lattner | 85b6d81 | 2011-01-02 03:37:56 +0000 | [diff] [blame] | 716 | const SCEVAddRecExpr *StoreEv = |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 717 | dyn_cast<SCEVAddRecExpr>(SE->getSCEV(StorePtr)); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 718 | if (!StoreEv || StoreEv->getLoop() != CurLoop || !StoreEv->isAffine()) |
Chris Lattner | 7c5f9c3 | 2010-12-26 20:45:45 +0000 | [diff] [blame] | 719 | return false; |
| 720 | |
| 721 | // Check to see if the stride matches the size of the store. If so, then we |
| 722 | // know that every byte is touched in the loop. |
Andrew Trick | 328b223 | 2011-03-14 16:48:10 +0000 | [diff] [blame] | 723 | unsigned StoreSize = (unsigned)SizeInBits >> 3; |
Chris Lattner | 85b6d81 | 2011-01-02 03:37:56 +0000 | [diff] [blame] | 724 | const SCEVConstant *Stride = dyn_cast<SCEVConstant>(StoreEv->getOperand(1)); |
Andrew Trick | 328b223 | 2011-03-14 16:48:10 +0000 | [diff] [blame] | 725 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 726 | if (!Stride || StoreSize != Stride->getValue()->getValue()) { |
Chris Lattner | bc661d6 | 2011-02-21 02:08:54 +0000 | [diff] [blame] | 727 | // TODO: Could also handle negative stride here someday, that will require |
| 728 | // the validity check in mayLoopAccessLocation to be updated though. |
| 729 | // Enable this to print exact negative strides. |
Chris Lattner | 2333ac2 | 2011-02-21 17:02:55 +0000 | [diff] [blame] | 730 | if (0 && Stride && StoreSize == -Stride->getValue()->getValue()) { |
Chris Lattner | bc661d6 | 2011-02-21 02:08:54 +0000 | [diff] [blame] | 731 | dbgs() << "NEGATIVE STRIDE: " << *SI << "\n"; |
| 732 | dbgs() << "BB: " << *SI->getParent(); |
| 733 | } |
Andrew Trick | 328b223 | 2011-03-14 16:48:10 +0000 | [diff] [blame] | 734 | |
Chris Lattner | 7c5f9c3 | 2010-12-26 20:45:45 +0000 | [diff] [blame] | 735 | return false; |
Chris Lattner | bc661d6 | 2011-02-21 02:08:54 +0000 | [diff] [blame] | 736 | } |
Chris Lattner | 0f4a640 | 2011-02-19 19:31:39 +0000 | [diff] [blame] | 737 | |
| 738 | // See if we can optimize just this store in isolation. |
| 739 | if (processLoopStridedStore(StorePtr, StoreSize, SI->getAlignment(), |
| 740 | StoredVal, SI, StoreEv, BECount)) |
| 741 | return true; |
Chris Lattner | 29e14ed | 2010-12-26 23:42:51 +0000 | [diff] [blame] | 742 | |
Chris Lattner | 85b6d81 | 2011-01-02 03:37:56 +0000 | [diff] [blame] | 743 | // If the stored value is a strided load in the same loop with the same stride |
| 744 | // this this may be transformable into a memcpy. This kicks in for stuff like |
| 745 | // for (i) A[i] = B[i]; |
| 746 | if (LoadInst *LI = dyn_cast<LoadInst>(StoredVal)) { |
| 747 | const SCEVAddRecExpr *LoadEv = |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 748 | dyn_cast<SCEVAddRecExpr>(SE->getSCEV(LI->getOperand(0))); |
Chris Lattner | 85b6d81 | 2011-01-02 03:37:56 +0000 | [diff] [blame] | 749 | if (LoadEv && LoadEv->getLoop() == CurLoop && LoadEv->isAffine() && |
Eli Friedman | 7c5dc12 | 2011-09-12 20:23:13 +0000 | [diff] [blame] | 750 | StoreEv->getOperand(1) == LoadEv->getOperand(1) && LI->isSimple()) |
Chris Lattner | 85b6d81 | 2011-01-02 03:37:56 +0000 | [diff] [blame] | 751 | if (processLoopStoreOfLoopLoad(SI, StoreSize, StoreEv, LoadEv, BECount)) |
| 752 | return true; |
| 753 | } |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 754 | // errs() << "UNHANDLED strided store: " << *StoreEv << " - " << *SI << "\n"; |
Chris Lattner | 7c5f9c3 | 2010-12-26 20:45:45 +0000 | [diff] [blame] | 755 | |
Chris Lattner | 81ae3f2 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 756 | return false; |
| 757 | } |
| 758 | |
Chris Lattner | 8643810 | 2011-01-04 07:46:33 +0000 | [diff] [blame] | 759 | /// processLoopMemSet - See if this memset can be promoted to a large memset. |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 760 | bool LoopIdiomRecognize::processLoopMemSet(MemSetInst *MSI, |
| 761 | const SCEV *BECount) { |
Chris Lattner | 8643810 | 2011-01-04 07:46:33 +0000 | [diff] [blame] | 762 | // We can only handle non-volatile memsets with a constant size. |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 763 | if (MSI->isVolatile() || !isa<ConstantInt>(MSI->getLength())) |
| 764 | return false; |
Chris Lattner | 8643810 | 2011-01-04 07:46:33 +0000 | [diff] [blame] | 765 | |
Chris Lattner | e6b261f | 2011-02-18 22:22:15 +0000 | [diff] [blame] | 766 | // If we're not allowed to hack on memset, we fail. |
| 767 | if (!TLI->has(LibFunc::memset)) |
| 768 | return false; |
Andrew Trick | 328b223 | 2011-03-14 16:48:10 +0000 | [diff] [blame] | 769 | |
Chris Lattner | 8643810 | 2011-01-04 07:46:33 +0000 | [diff] [blame] | 770 | Value *Pointer = MSI->getDest(); |
Andrew Trick | 328b223 | 2011-03-14 16:48:10 +0000 | [diff] [blame] | 771 | |
Chris Lattner | 8643810 | 2011-01-04 07:46:33 +0000 | [diff] [blame] | 772 | // See if the pointer expression is an AddRec like {base,+,1} on the current |
| 773 | // loop, which indicates a strided store. If we have something else, it's a |
| 774 | // random store we can't handle. |
| 775 | const SCEVAddRecExpr *Ev = dyn_cast<SCEVAddRecExpr>(SE->getSCEV(Pointer)); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 776 | if (!Ev || Ev->getLoop() != CurLoop || !Ev->isAffine()) |
Chris Lattner | 8643810 | 2011-01-04 07:46:33 +0000 | [diff] [blame] | 777 | return false; |
| 778 | |
| 779 | // Reject memsets that are so large that they overflow an unsigned. |
| 780 | uint64_t SizeInBytes = cast<ConstantInt>(MSI->getLength())->getZExtValue(); |
| 781 | if ((SizeInBytes >> 32) != 0) |
| 782 | return false; |
Andrew Trick | 328b223 | 2011-03-14 16:48:10 +0000 | [diff] [blame] | 783 | |
Chris Lattner | 8643810 | 2011-01-04 07:46:33 +0000 | [diff] [blame] | 784 | // Check to see if the stride matches the size of the memset. If so, then we |
| 785 | // know that every byte is touched in the loop. |
| 786 | const SCEVConstant *Stride = dyn_cast<SCEVConstant>(Ev->getOperand(1)); |
Andrew Trick | 328b223 | 2011-03-14 16:48:10 +0000 | [diff] [blame] | 787 | |
Chris Lattner | 8643810 | 2011-01-04 07:46:33 +0000 | [diff] [blame] | 788 | // TODO: Could also handle negative stride here someday, that will require the |
| 789 | // validity check in mayLoopAccessLocation to be updated though. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 790 | if (!Stride || MSI->getLength() != Stride->getValue()) |
Chris Lattner | 8643810 | 2011-01-04 07:46:33 +0000 | [diff] [blame] | 791 | return false; |
Andrew Trick | 328b223 | 2011-03-14 16:48:10 +0000 | [diff] [blame] | 792 | |
Chris Lattner | 0f4a640 | 2011-02-19 19:31:39 +0000 | [diff] [blame] | 793 | return processLoopStridedStore(Pointer, (unsigned)SizeInBytes, |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 794 | MSI->getAlignment(), MSI->getValue(), MSI, Ev, |
| 795 | BECount); |
Chris Lattner | 8643810 | 2011-01-04 07:46:33 +0000 | [diff] [blame] | 796 | } |
| 797 | |
Chandler Carruth | 099f5cb0 | 2012-11-02 08:33:25 +0000 | [diff] [blame] | 798 | /// mayLoopAccessLocation - Return true if the specified loop might access the |
| 799 | /// specified pointer location, which is a loop-strided access. The 'Access' |
| 800 | /// argument specifies what the verboten forms of access are (read or write). |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 801 | static bool mayLoopAccessLocation(Value *Ptr, ModRefInfo Access, Loop *L, |
| 802 | const SCEV *BECount, unsigned StoreSize, |
| 803 | AliasAnalysis &AA, |
Chandler Carruth | 099f5cb0 | 2012-11-02 08:33:25 +0000 | [diff] [blame] | 804 | Instruction *IgnoredStore) { |
| 805 | // Get the location that may be stored across the loop. Since the access is |
| 806 | // strided positively through memory, we say that the modified location starts |
| 807 | // at the pointer and has infinite size. |
Chandler Carruth | ecbd168 | 2015-06-17 07:21:38 +0000 | [diff] [blame] | 808 | uint64_t AccessSize = MemoryLocation::UnknownSize; |
Chandler Carruth | 099f5cb0 | 2012-11-02 08:33:25 +0000 | [diff] [blame] | 809 | |
| 810 | // If the loop iterates a fixed number of times, we can refine the access size |
| 811 | // to be exactly the size of the memset, which is (BECount+1)*StoreSize |
| 812 | if (const SCEVConstant *BECst = dyn_cast<SCEVConstant>(BECount)) |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 813 | AccessSize = (BECst->getValue()->getZExtValue() + 1) * StoreSize; |
Chandler Carruth | 099f5cb0 | 2012-11-02 08:33:25 +0000 | [diff] [blame] | 814 | |
| 815 | // TODO: For this to be really effective, we have to dive into the pointer |
| 816 | // operand in the store. Store to &A[i] of 100 will always return may alias |
| 817 | // with store of &A[100], we need to StoreLoc to be "A" with size of 100, |
| 818 | // which will then no-alias a store to &A[100]. |
Chandler Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 819 | MemoryLocation StoreLoc(Ptr, AccessSize); |
Chandler Carruth | 099f5cb0 | 2012-11-02 08:33:25 +0000 | [diff] [blame] | 820 | |
| 821 | for (Loop::block_iterator BI = L->block_begin(), E = L->block_end(); BI != E; |
| 822 | ++BI) |
| 823 | for (BasicBlock::iterator I = (*BI)->begin(), E = (*BI)->end(); I != E; ++I) |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 824 | if (&*I != IgnoredStore && (AA.getModRefInfo(I, StoreLoc) & Access)) |
Chandler Carruth | 099f5cb0 | 2012-11-02 08:33:25 +0000 | [diff] [blame] | 825 | return true; |
| 826 | |
| 827 | return false; |
| 828 | } |
| 829 | |
Chris Lattner | 0f4a640 | 2011-02-19 19:31:39 +0000 | [diff] [blame] | 830 | /// getMemSetPatternValue - If a strided store of the specified value is safe to |
| 831 | /// turn into a memset_pattern16, return a ConstantArray of 16 bytes that should |
| 832 | /// be passed in. Otherwise, return null. |
| 833 | /// |
| 834 | /// Note that we don't ever attempt to use memset_pattern8 or 4, because these |
| 835 | /// just replicate their input array and then pass on to memset_pattern16. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 836 | static Constant *getMemSetPatternValue(Value *V, const DataLayout &DL) { |
Chris Lattner | 0f4a640 | 2011-02-19 19:31:39 +0000 | [diff] [blame] | 837 | // If the value isn't a constant, we can't promote it to being in a constant |
| 838 | // array. We could theoretically do a store to an alloca or something, but |
| 839 | // that doesn't seem worthwhile. |
| 840 | Constant *C = dyn_cast<Constant>(V); |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 841 | if (!C) |
| 842 | return nullptr; |
Andrew Trick | 328b223 | 2011-03-14 16:48:10 +0000 | [diff] [blame] | 843 | |
Chris Lattner | 0f4a640 | 2011-02-19 19:31:39 +0000 | [diff] [blame] | 844 | // Only handle simple values that are a power of two bytes in size. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 845 | uint64_t Size = DL.getTypeSizeInBits(V->getType()); |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 846 | if (Size == 0 || (Size & 7) || (Size & (Size - 1))) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 847 | return nullptr; |
Andrew Trick | 328b223 | 2011-03-14 16:48:10 +0000 | [diff] [blame] | 848 | |
Chris Lattner | 72a35fb | 2011-02-19 19:56:44 +0000 | [diff] [blame] | 849 | // Don't care enough about darwin/ppc to implement this. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 850 | if (DL.isBigEndian()) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 851 | return nullptr; |
Chris Lattner | 0f4a640 | 2011-02-19 19:31:39 +0000 | [diff] [blame] | 852 | |
| 853 | // Convert to size in bytes. |
| 854 | Size /= 8; |
Chris Lattner | 0f4a640 | 2011-02-19 19:31:39 +0000 | [diff] [blame] | 855 | |
Chris Lattner | 0f4a640 | 2011-02-19 19:31:39 +0000 | [diff] [blame] | 856 | // TODO: If CI is larger than 16-bytes, we can try slicing it in half to see |
Chris Lattner | 72a35fb | 2011-02-19 19:56:44 +0000 | [diff] [blame] | 857 | // if the top and bottom are the same (e.g. for vectors and large integers). |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 858 | if (Size > 16) |
| 859 | return nullptr; |
Andrew Trick | 328b223 | 2011-03-14 16:48:10 +0000 | [diff] [blame] | 860 | |
Chris Lattner | 72a35fb | 2011-02-19 19:56:44 +0000 | [diff] [blame] | 861 | // If the constant is exactly 16 bytes, just use it. |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 862 | if (Size == 16) |
| 863 | return C; |
Chris Lattner | 0f4a640 | 2011-02-19 19:31:39 +0000 | [diff] [blame] | 864 | |
Chris Lattner | 72a35fb | 2011-02-19 19:56:44 +0000 | [diff] [blame] | 865 | // Otherwise, we'll use an array of the constants. |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 866 | unsigned ArraySize = 16 / Size; |
Chris Lattner | 72a35fb | 2011-02-19 19:56:44 +0000 | [diff] [blame] | 867 | ArrayType *AT = ArrayType::get(V->getType(), ArraySize); |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 868 | return ConstantArray::get(AT, std::vector<Constant *>(ArraySize, C)); |
Chris Lattner | 0f4a640 | 2011-02-19 19:31:39 +0000 | [diff] [blame] | 869 | } |
| 870 | |
Chris Lattner | 0f4a640 | 2011-02-19 19:31:39 +0000 | [diff] [blame] | 871 | /// processLoopStridedStore - We see a strided store of some value. If we can |
| 872 | /// transform this into a memset or memset_pattern in the loop preheader, do so. |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 873 | bool LoopIdiomRecognize::processLoopStridedStore( |
| 874 | Value *DestPtr, unsigned StoreSize, unsigned StoreAlignment, |
| 875 | Value *StoredVal, Instruction *TheStore, const SCEVAddRecExpr *Ev, |
| 876 | const SCEV *BECount) { |
Andrew Trick | 328b223 | 2011-03-14 16:48:10 +0000 | [diff] [blame] | 877 | |
Chris Lattner | 0f4a640 | 2011-02-19 19:31:39 +0000 | [diff] [blame] | 878 | // If the stored value is a byte-wise value (like i32 -1), then it may be |
| 879 | // turned into a memset of i8 -1, assuming that all the consecutive bytes |
| 880 | // are stored. A store of i32 0x01020304 can never be turned into a memset, |
| 881 | // but it can be turned into memset_pattern if the target supports it. |
| 882 | Value *SplatValue = isBytewiseValue(StoredVal); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 883 | Constant *PatternValue = nullptr; |
Mehdi Amini | 46a4355 | 2015-03-04 18:43:29 +0000 | [diff] [blame] | 884 | auto &DL = CurLoop->getHeader()->getModule()->getDataLayout(); |
Matt Arsenault | 009faed | 2013-09-11 05:09:42 +0000 | [diff] [blame] | 885 | unsigned DestAS = DestPtr->getType()->getPointerAddressSpace(); |
| 886 | |
Chris Lattner | 0f4a640 | 2011-02-19 19:31:39 +0000 | [diff] [blame] | 887 | // If we're allowed to form a memset, and the stored value would be acceptable |
| 888 | // for memset, use it. |
| 889 | if (SplatValue && TLI->has(LibFunc::memset) && |
| 890 | // Verify that the stored value is loop invariant. If not, we can't |
| 891 | // promote the memset. |
| 892 | CurLoop->isLoopInvariant(SplatValue)) { |
| 893 | // Keep and use SplatValue. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 894 | PatternValue = nullptr; |
Mehdi Amini | 46a4355 | 2015-03-04 18:43:29 +0000 | [diff] [blame] | 895 | } else if (DestAS == 0 && TLI->has(LibFunc::memset_pattern16) && |
| 896 | (PatternValue = getMemSetPatternValue(StoredVal, DL))) { |
Matt Arsenault | 009faed | 2013-09-11 05:09:42 +0000 | [diff] [blame] | 897 | // Don't create memset_pattern16s with address spaces. |
Chris Lattner | 0f4a640 | 2011-02-19 19:31:39 +0000 | [diff] [blame] | 898 | // It looks like we can use PatternValue! |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 899 | SplatValue = nullptr; |
Chris Lattner | 0f4a640 | 2011-02-19 19:31:39 +0000 | [diff] [blame] | 900 | } else { |
| 901 | // Otherwise, this isn't an idiom we can transform. For example, we can't |
Eli Friedman | a93ab13 | 2011-09-13 00:44:16 +0000 | [diff] [blame] | 902 | // do anything with a 3-byte store. |
Chris Lattner | a351444 | 2011-01-01 20:12:04 +0000 | [diff] [blame] | 903 | return false; |
Chris Lattner | 0f4a640 | 2011-02-19 19:31:39 +0000 | [diff] [blame] | 904 | } |
Andrew Trick | 328b223 | 2011-03-14 16:48:10 +0000 | [diff] [blame] | 905 | |
Chris Lattner | c4ca7ab | 2011-05-22 17:39:56 +0000 | [diff] [blame] | 906 | // The trip count of the loop and the base pointer of the addrec SCEV is |
| 907 | // guaranteed to be loop invariant, which means that it should dominate the |
| 908 | // header. This allows us to insert code for it in the preheader. |
| 909 | BasicBlock *Preheader = CurLoop->getLoopPreheader(); |
| 910 | IRBuilder<> Builder(Preheader->getTerminator()); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 911 | SCEVExpander Expander(*SE, DL, "loop-idiom"); |
Andrew Trick | 60ab3ef | 2011-06-28 05:04:16 +0000 | [diff] [blame] | 912 | |
Matt Arsenault | 009faed | 2013-09-11 05:09:42 +0000 | [diff] [blame] | 913 | Type *DestInt8PtrTy = Builder.getInt8PtrTy(DestAS); |
| 914 | |
Chris Lattner | 29e14ed | 2010-12-26 23:42:51 +0000 | [diff] [blame] | 915 | // Okay, we have a strided store "p[i]" of a splattable value. We can turn |
Benjamin Kramer | f77f224 | 2012-10-21 19:31:16 +0000 | [diff] [blame] | 916 | // this into a memset in the loop preheader now if we want. However, this |
| 917 | // would be unsafe to do if there is anything else in the loop that may read |
Chandler Carruth | 7ec5085 | 2012-11-01 08:07:29 +0000 | [diff] [blame] | 918 | // or write to the aliased location. Check for any overlap by generating the |
| 919 | // base pointer and checking the region. |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 920 | Value *BasePtr = Expander.expandCodeFor(Ev->getStart(), DestInt8PtrTy, |
| 921 | Preheader->getTerminator()); |
Andrew Trick | 328b223 | 2011-03-14 16:48:10 +0000 | [diff] [blame] | 922 | |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 923 | if (mayLoopAccessLocation(BasePtr, MRI_ModRef, CurLoop, BECount, StoreSize, |
| 924 | getAnalysis<AliasAnalysis>(), TheStore)) { |
Chandler Carruth | 099f5cb0 | 2012-11-02 08:33:25 +0000 | [diff] [blame] | 925 | Expander.clear(); |
| 926 | // If we generated new code for the base pointer, clean up. |
Benjamin Kramer | f094d77 | 2015-02-07 21:37:08 +0000 | [diff] [blame] | 927 | RecursivelyDeleteTriviallyDeadInstructions(BasePtr, TLI); |
Chandler Carruth | 099f5cb0 | 2012-11-02 08:33:25 +0000 | [diff] [blame] | 928 | return false; |
| 929 | } |
| 930 | |
Chris Lattner | c4ca7ab | 2011-05-22 17:39:56 +0000 | [diff] [blame] | 931 | // Okay, everything looks good, insert the memset. |
| 932 | |
Chris Lattner | 29e14ed | 2010-12-26 23:42:51 +0000 | [diff] [blame] | 933 | // The # stored bytes is (BECount+1)*Size. Expand the trip count out to |
| 934 | // pointer size if it isn't already. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 935 | Type *IntPtr = Builder.getIntPtrTy(DL, DestAS); |
Chris Lattner | 0ba473c | 2011-01-04 00:06:55 +0000 | [diff] [blame] | 936 | BECount = SE->getTruncateOrZeroExtend(BECount, IntPtr); |
Andrew Trick | 328b223 | 2011-03-14 16:48:10 +0000 | [diff] [blame] | 937 | |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 938 | const SCEV *NumBytesS = |
| 939 | SE->getAddExpr(BECount, SE->getConstant(IntPtr, 1), SCEV::FlagNUW); |
Matt Arsenault | 5df49bd | 2013-09-11 05:09:35 +0000 | [diff] [blame] | 940 | if (StoreSize != 1) { |
Chris Lattner | 29e14ed | 2010-12-26 23:42:51 +0000 | [diff] [blame] | 941 | NumBytesS = SE->getMulExpr(NumBytesS, SE->getConstant(IntPtr, StoreSize), |
Andrew Trick | 8b55b73 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 942 | SCEV::FlagNUW); |
Matt Arsenault | 5df49bd | 2013-09-11 05:09:35 +0000 | [diff] [blame] | 943 | } |
Andrew Trick | 328b223 | 2011-03-14 16:48:10 +0000 | [diff] [blame] | 944 | |
| 945 | Value *NumBytes = |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 946 | Expander.expandCodeFor(NumBytesS, IntPtr, Preheader->getTerminator()); |
Andrew Trick | 328b223 | 2011-03-14 16:48:10 +0000 | [diff] [blame] | 947 | |
Devang Patel | d00c628 | 2011-03-07 22:43:45 +0000 | [diff] [blame] | 948 | CallInst *NewCall; |
Matt Arsenault | 5df49bd | 2013-09-11 05:09:35 +0000 | [diff] [blame] | 949 | if (SplatValue) { |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 950 | NewCall = |
| 951 | Builder.CreateMemSet(BasePtr, SplatValue, NumBytes, StoreAlignment); |
Matt Arsenault | 5df49bd | 2013-09-11 05:09:35 +0000 | [diff] [blame] | 952 | } else { |
Matt Arsenault | 009faed | 2013-09-11 05:09:42 +0000 | [diff] [blame] | 953 | // Everything is emitted in default address space |
| 954 | Type *Int8PtrTy = DestInt8PtrTy; |
| 955 | |
Chris Lattner | 0f4a640 | 2011-02-19 19:31:39 +0000 | [diff] [blame] | 956 | Module *M = TheStore->getParent()->getParent()->getParent(); |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 957 | Value *MSP = |
| 958 | M->getOrInsertFunction("memset_pattern16", Builder.getVoidTy(), |
| 959 | Int8PtrTy, Int8PtrTy, IntPtr, (void *)nullptr); |
Andrew Trick | 328b223 | 2011-03-14 16:48:10 +0000 | [diff] [blame] | 960 | |
Chris Lattner | 0f4a640 | 2011-02-19 19:31:39 +0000 | [diff] [blame] | 961 | // Otherwise we should form a memset_pattern16. PatternValue is known to be |
| 962 | // an constant array of 16-bytes. Plop the value into a mergable global. |
| 963 | GlobalVariable *GV = new GlobalVariable(*M, PatternValue->getType(), true, |
Benjamin Kramer | 838752d | 2015-03-03 00:17:09 +0000 | [diff] [blame] | 964 | GlobalValue::PrivateLinkage, |
Chris Lattner | 0f4a640 | 2011-02-19 19:31:39 +0000 | [diff] [blame] | 965 | PatternValue, ".memset_pattern"); |
| 966 | GV->setUnnamedAddr(true); // Ok to merge these. |
| 967 | GV->setAlignment(16); |
Matt Arsenault | 009faed | 2013-09-11 05:09:42 +0000 | [diff] [blame] | 968 | Value *PatternPtr = ConstantExpr::getBitCast(GV, Int8PtrTy); |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 969 | NewCall = Builder.CreateCall(MSP, {BasePtr, PatternPtr, NumBytes}); |
Chris Lattner | 0f4a640 | 2011-02-19 19:31:39 +0000 | [diff] [blame] | 970 | } |
Andrew Trick | 328b223 | 2011-03-14 16:48:10 +0000 | [diff] [blame] | 971 | |
Chris Lattner | 29e14ed | 2010-12-26 23:42:51 +0000 | [diff] [blame] | 972 | DEBUG(dbgs() << " Formed memset: " << *NewCall << "\n" |
Chris Lattner | 8643810 | 2011-01-04 07:46:33 +0000 | [diff] [blame] | 973 | << " from store to: " << *Ev << " at: " << *TheStore << "\n"); |
Devang Patel | d00c628 | 2011-03-07 22:43:45 +0000 | [diff] [blame] | 974 | NewCall->setDebugLoc(TheStore->getDebugLoc()); |
Andrew Trick | 328b223 | 2011-03-14 16:48:10 +0000 | [diff] [blame] | 975 | |
Chris Lattner | b9fe685 | 2010-12-27 00:03:23 +0000 | [diff] [blame] | 976 | // Okay, the memset has been formed. Zap the original store and anything that |
| 977 | // feeds into it. |
Benjamin Kramer | f094d77 | 2015-02-07 21:37:08 +0000 | [diff] [blame] | 978 | deleteDeadInstruction(TheStore, TLI); |
Chris Lattner | 12f91be | 2011-01-02 07:36:44 +0000 | [diff] [blame] | 979 | ++NumMemSet; |
Chris Lattner | 29e14ed | 2010-12-26 23:42:51 +0000 | [diff] [blame] | 980 | return true; |
| 981 | } |
| 982 | |
Chris Lattner | 85b6d81 | 2011-01-02 03:37:56 +0000 | [diff] [blame] | 983 | /// processLoopStoreOfLoopLoad - We see a strided store whose value is a |
| 984 | /// same-strided load. |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 985 | bool LoopIdiomRecognize::processLoopStoreOfLoopLoad( |
| 986 | StoreInst *SI, unsigned StoreSize, const SCEVAddRecExpr *StoreEv, |
| 987 | const SCEVAddRecExpr *LoadEv, const SCEV *BECount) { |
Chris Lattner | e6b261f | 2011-02-18 22:22:15 +0000 | [diff] [blame] | 988 | // If we're not allowed to form memcpy, we fail. |
Chandler Carruth | 099f5cb0 | 2012-11-02 08:33:25 +0000 | [diff] [blame] | 989 | if (!TLI->has(LibFunc::memcpy)) |
Chris Lattner | e6b261f | 2011-02-18 22:22:15 +0000 | [diff] [blame] | 990 | return false; |
Andrew Trick | 328b223 | 2011-03-14 16:48:10 +0000 | [diff] [blame] | 991 | |
Chris Lattner | 85b6d81 | 2011-01-02 03:37:56 +0000 | [diff] [blame] | 992 | LoadInst *LI = cast<LoadInst>(SI->getValueOperand()); |
Andrew Trick | 328b223 | 2011-03-14 16:48:10 +0000 | [diff] [blame] | 993 | |
Chris Lattner | c4ca7ab | 2011-05-22 17:39:56 +0000 | [diff] [blame] | 994 | // The trip count of the loop and the base pointer of the addrec SCEV is |
| 995 | // guaranteed to be loop invariant, which means that it should dominate the |
| 996 | // header. This allows us to insert code for it in the preheader. |
| 997 | BasicBlock *Preheader = CurLoop->getLoopPreheader(); |
| 998 | IRBuilder<> Builder(Preheader->getTerminator()); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 999 | const DataLayout &DL = Preheader->getModule()->getDataLayout(); |
| 1000 | SCEVExpander Expander(*SE, DL, "loop-idiom"); |
Andrew Trick | 60ab3ef | 2011-06-28 05:04:16 +0000 | [diff] [blame] | 1001 | |
Chris Lattner | 85b6d81 | 2011-01-02 03:37:56 +0000 | [diff] [blame] | 1002 | // Okay, we have a strided store "p[i]" of a loaded value. We can turn |
Chandler Carruth | 099f5cb0 | 2012-11-02 08:33:25 +0000 | [diff] [blame] | 1003 | // this into a memcpy in the loop preheader now if we want. However, this |
| 1004 | // would be unsafe to do if there is anything else in the loop that may read |
| 1005 | // or write the memory region we're storing to. This includes the load that |
| 1006 | // feeds the stores. Check for an alias by generating the base address and |
| 1007 | // checking everything. |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 1008 | Value *StoreBasePtr = Expander.expandCodeFor( |
| 1009 | StoreEv->getStart(), Builder.getInt8PtrTy(SI->getPointerAddressSpace()), |
| 1010 | Preheader->getTerminator()); |
Chandler Carruth | 099f5cb0 | 2012-11-02 08:33:25 +0000 | [diff] [blame] | 1011 | |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 1012 | if (mayLoopAccessLocation(StoreBasePtr, MRI_ModRef, CurLoop, BECount, |
| 1013 | StoreSize, getAnalysis<AliasAnalysis>(), SI)) { |
Chandler Carruth | 099f5cb0 | 2012-11-02 08:33:25 +0000 | [diff] [blame] | 1014 | Expander.clear(); |
| 1015 | // If we generated new code for the base pointer, clean up. |
Benjamin Kramer | f094d77 | 2015-02-07 21:37:08 +0000 | [diff] [blame] | 1016 | RecursivelyDeleteTriviallyDeadInstructions(StoreBasePtr, TLI); |
Chandler Carruth | 099f5cb0 | 2012-11-02 08:33:25 +0000 | [diff] [blame] | 1017 | return false; |
| 1018 | } |
| 1019 | |
| 1020 | // For a memcpy, we have to make sure that the input array is not being |
| 1021 | // mutated by the loop. |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 1022 | Value *LoadBasePtr = Expander.expandCodeFor( |
| 1023 | LoadEv->getStart(), Builder.getInt8PtrTy(LI->getPointerAddressSpace()), |
| 1024 | Preheader->getTerminator()); |
Chris Lattner | c4ca7ab | 2011-05-22 17:39:56 +0000 | [diff] [blame] | 1025 | |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 1026 | if (mayLoopAccessLocation(LoadBasePtr, MRI_Mod, CurLoop, BECount, StoreSize, |
| 1027 | getAnalysis<AliasAnalysis>(), SI)) { |
Chandler Carruth | 099f5cb0 | 2012-11-02 08:33:25 +0000 | [diff] [blame] | 1028 | Expander.clear(); |
| 1029 | // If we generated new code for the base pointer, clean up. |
Benjamin Kramer | f094d77 | 2015-02-07 21:37:08 +0000 | [diff] [blame] | 1030 | RecursivelyDeleteTriviallyDeadInstructions(LoadBasePtr, TLI); |
| 1031 | RecursivelyDeleteTriviallyDeadInstructions(StoreBasePtr, TLI); |
Chandler Carruth | 099f5cb0 | 2012-11-02 08:33:25 +0000 | [diff] [blame] | 1032 | return false; |
| 1033 | } |
| 1034 | |
Chris Lattner | c4ca7ab | 2011-05-22 17:39:56 +0000 | [diff] [blame] | 1035 | // Okay, everything is safe, we can transform this! |
Andrew Trick | 60ab3ef | 2011-06-28 05:04:16 +0000 | [diff] [blame] | 1036 | |
Chris Lattner | 85b6d81 | 2011-01-02 03:37:56 +0000 | [diff] [blame] | 1037 | // The # stored bytes is (BECount+1)*Size. Expand the trip count out to |
| 1038 | // pointer size if it isn't already. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1039 | Type *IntPtrTy = Builder.getIntPtrTy(DL, SI->getPointerAddressSpace()); |
Matt Arsenault | 009faed | 2013-09-11 05:09:42 +0000 | [diff] [blame] | 1040 | BECount = SE->getTruncateOrZeroExtend(BECount, IntPtrTy); |
Andrew Trick | 328b223 | 2011-03-14 16:48:10 +0000 | [diff] [blame] | 1041 | |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 1042 | const SCEV *NumBytesS = |
| 1043 | SE->getAddExpr(BECount, SE->getConstant(IntPtrTy, 1), SCEV::FlagNUW); |
Chris Lattner | 85b6d81 | 2011-01-02 03:37:56 +0000 | [diff] [blame] | 1044 | if (StoreSize != 1) |
Matt Arsenault | 009faed | 2013-09-11 05:09:42 +0000 | [diff] [blame] | 1045 | NumBytesS = SE->getMulExpr(NumBytesS, SE->getConstant(IntPtrTy, StoreSize), |
Andrew Trick | 8b55b73 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 1046 | SCEV::FlagNUW); |
Andrew Trick | 328b223 | 2011-03-14 16:48:10 +0000 | [diff] [blame] | 1047 | |
Chris Lattner | 85b6d81 | 2011-01-02 03:37:56 +0000 | [diff] [blame] | 1048 | Value *NumBytes = |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 1049 | Expander.expandCodeFor(NumBytesS, IntPtrTy, Preheader->getTerminator()); |
Andrew Trick | 328b223 | 2011-03-14 16:48:10 +0000 | [diff] [blame] | 1050 | |
Chandler Carruth | 099f5cb0 | 2012-11-02 08:33:25 +0000 | [diff] [blame] | 1051 | CallInst *NewCall = |
Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 1052 | Builder.CreateMemCpy(StoreBasePtr, LoadBasePtr, NumBytes, |
| 1053 | std::min(SI->getAlignment(), LI->getAlignment())); |
Devang Patel | 0daa07e | 2011-05-04 21:37:05 +0000 | [diff] [blame] | 1054 | NewCall->setDebugLoc(SI->getDebugLoc()); |
Andrew Trick | 328b223 | 2011-03-14 16:48:10 +0000 | [diff] [blame] | 1055 | |
Chandler Carruth | 099f5cb0 | 2012-11-02 08:33:25 +0000 | [diff] [blame] | 1056 | DEBUG(dbgs() << " Formed memcpy: " << *NewCall << "\n" |
Chris Lattner | 85b6d81 | 2011-01-02 03:37:56 +0000 | [diff] [blame] | 1057 | << " from load ptr=" << *LoadEv << " at: " << *LI << "\n" |
| 1058 | << " from store ptr=" << *StoreEv << " at: " << *SI << "\n"); |
Andrew Trick | 60ab3ef | 2011-06-28 05:04:16 +0000 | [diff] [blame] | 1059 | |
Chris Lattner | 85b6d81 | 2011-01-02 03:37:56 +0000 | [diff] [blame] | 1060 | // Okay, the memset has been formed. Zap the original store and anything that |
| 1061 | // feeds into it. |
Benjamin Kramer | f094d77 | 2015-02-07 21:37:08 +0000 | [diff] [blame] | 1062 | deleteDeadInstruction(SI, TLI); |
Chandler Carruth | 099f5cb0 | 2012-11-02 08:33:25 +0000 | [diff] [blame] | 1063 | ++NumMemCpy; |
Chris Lattner | 85b6d81 | 2011-01-02 03:37:56 +0000 | [diff] [blame] | 1064 | return true; |
| 1065 | } |
Chandler Carruth | d9c6070 | 2015-08-13 00:10:03 +0000 | [diff] [blame] | 1066 | |
| 1067 | bool LoopIdiomRecognize::runOnNoncountableLoop() { |
| 1068 | NclPopcountRecognize Popcount(*this); |
| 1069 | if (Popcount.recognize()) |
| 1070 | return true; |
| 1071 | |
| 1072 | return false; |
| 1073 | } |