| Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 1 | //===- LoopIdiomRecognize.cpp - Loop idiom recognition --------------------===// | 
| Chris Lattner | 81ae3f2 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 2 | // | 
| Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | 
|  | 4 | // See https://llvm.org/LICENSE.txt for license information. | 
|  | 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | 
| Chris Lattner | 81ae3f2 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 6 | // | 
|  | 7 | //===----------------------------------------------------------------------===// | 
|  | 8 | // | 
|  | 9 | // This pass implements an idiom recognizer that transforms simple loops into a | 
|  | 10 | // non-loop form.  In cases that this kicks in, it can be a significant | 
|  | 11 | // performance win. | 
|  | 12 | // | 
| Andrew Kaylor | 7cdf01e | 2016-08-11 18:28:33 +0000 | [diff] [blame] | 13 | // If compiling for code size we avoid idiom recognition if the resulting | 
|  | 14 | // code could be larger than the code for the original loop. One way this could | 
|  | 15 | // happen is if the loop is not removable after idiom recognition due to the | 
|  | 16 | // presence of non-idiom instructions. The initial implementation of the | 
|  | 17 | // heuristics applies to idioms in multi-block loops. | 
|  | 18 | // | 
| Chris Lattner | 81ae3f2 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 19 | //===----------------------------------------------------------------------===// | 
| Chris Lattner | 0469e01 | 2011-01-02 18:32:09 +0000 | [diff] [blame] | 20 | // | 
|  | 21 | // TODO List: | 
|  | 22 | // | 
|  | 23 | // Future loop memory idioms to recognize: | 
| Chandler Carruth | 099f5cb0 | 2012-11-02 08:33:25 +0000 | [diff] [blame] | 24 | //   memcmp, memmove, strlen, etc. | 
| Chris Lattner | 0469e01 | 2011-01-02 18:32:09 +0000 | [diff] [blame] | 25 | // Future floating point idioms to recognize in -ffast-math mode: | 
|  | 26 | //   fpowi | 
|  | 27 | // Future integer operation idioms to recognize: | 
| Craig Topper | c9a6000 | 2018-12-26 21:59:48 +0000 | [diff] [blame] | 28 | //   ctpop | 
| Chris Lattner | 0469e01 | 2011-01-02 18:32:09 +0000 | [diff] [blame] | 29 | // | 
|  | 30 | // Beware that isel's default lowering for ctpop is highly inefficient for | 
|  | 31 | // i64 and larger types when i64 is legal and the value has few bits set.  It | 
|  | 32 | // would be good to enhance isel to emit a loop for ctpop in this case. | 
|  | 33 | // | 
| Chris Lattner | 02a9776 | 2011-01-03 01:10:08 +0000 | [diff] [blame] | 34 | // This could recognize common matrix multiplies and dot product idioms and | 
| Chris Lattner | 8fac5db | 2011-01-02 23:19:45 +0000 | [diff] [blame] | 35 | // replace them with calls to BLAS (if linked in??). | 
|  | 36 | // | 
| Chris Lattner | 0469e01 | 2011-01-02 18:32:09 +0000 | [diff] [blame] | 37 | //===----------------------------------------------------------------------===// | 
| Chris Lattner | 81ae3f2 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 38 |  | 
| Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 39 | #include "llvm/ADT/APInt.h" | 
|  | 40 | #include "llvm/ADT/ArrayRef.h" | 
|  | 41 | #include "llvm/ADT/DenseMap.h" | 
| Haicheng Wu | f1c00a2 | 2016-01-26 02:27:47 +0000 | [diff] [blame] | 42 | #include "llvm/ADT/MapVector.h" | 
|  | 43 | #include "llvm/ADT/SetVector.h" | 
| Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 44 | #include "llvm/ADT/SmallPtrSet.h" | 
|  | 45 | #include "llvm/ADT/SmallVector.h" | 
| Chandler Carruth | aafe091 | 2012-06-29 12:38:19 +0000 | [diff] [blame] | 46 | #include "llvm/ADT/Statistic.h" | 
| Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 47 | #include "llvm/ADT/StringRef.h" | 
| Chris Lattner | cb18bfa | 2010-12-27 18:39:08 +0000 | [diff] [blame] | 48 | #include "llvm/Analysis/AliasAnalysis.h" | 
| Haicheng Wu | f1c00a2 | 2016-01-26 02:27:47 +0000 | [diff] [blame] | 49 | #include "llvm/Analysis/LoopAccessAnalysis.h" | 
| Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 50 | #include "llvm/Analysis/LoopInfo.h" | 
| Dehao Chen | b9f8e29 | 2016-07-12 18:45:51 +0000 | [diff] [blame] | 51 | #include "llvm/Analysis/LoopPass.h" | 
| Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 52 | #include "llvm/Analysis/MemoryLocation.h" | 
|  | 53 | #include "llvm/Analysis/ScalarEvolution.h" | 
| Chad Rosier | a15b4b6 | 2015-11-23 21:09:13 +0000 | [diff] [blame] | 54 | #include "llvm/Analysis/ScalarEvolutionExpander.h" | 
| Chandler Carruth | aafe091 | 2012-06-29 12:38:19 +0000 | [diff] [blame] | 55 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" | 
| Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 56 | #include "llvm/Analysis/TargetLibraryInfo.h" | 
| Chandler Carruth | d3e7355 | 2013-01-07 03:08:10 +0000 | [diff] [blame] | 57 | #include "llvm/Analysis/TargetTransformInfo.h" | 
| David Blaikie | 31b98d2 | 2018-06-04 21:23:21 +0000 | [diff] [blame] | 58 | #include "llvm/Transforms/Utils/Local.h" | 
| Chris Lattner | 7c5f9c3 | 2010-12-26 20:45:45 +0000 | [diff] [blame] | 59 | #include "llvm/Analysis/ValueTracking.h" | 
| Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 60 | #include "llvm/IR/Attributes.h" | 
|  | 61 | #include "llvm/IR/BasicBlock.h" | 
|  | 62 | #include "llvm/IR/Constant.h" | 
|  | 63 | #include "llvm/IR/Constants.h" | 
| Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 64 | #include "llvm/IR/DataLayout.h" | 
| Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 65 | #include "llvm/IR/DebugLoc.h" | 
|  | 66 | #include "llvm/IR/DerivedTypes.h" | 
| Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 67 | #include "llvm/IR/Dominators.h" | 
| Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 68 | #include "llvm/IR/GlobalValue.h" | 
|  | 69 | #include "llvm/IR/GlobalVariable.h" | 
| Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 70 | #include "llvm/IR/IRBuilder.h" | 
| Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 71 | #include "llvm/IR/InstrTypes.h" | 
|  | 72 | #include "llvm/IR/Instruction.h" | 
|  | 73 | #include "llvm/IR/Instructions.h" | 
| Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 74 | #include "llvm/IR/IntrinsicInst.h" | 
| Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 75 | #include "llvm/IR/Intrinsics.h" | 
|  | 76 | #include "llvm/IR/LLVMContext.h" | 
| Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 77 | #include "llvm/IR/Module.h" | 
| Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 78 | #include "llvm/IR/PassManager.h" | 
|  | 79 | #include "llvm/IR/Type.h" | 
|  | 80 | #include "llvm/IR/User.h" | 
|  | 81 | #include "llvm/IR/Value.h" | 
|  | 82 | #include "llvm/IR/ValueHandle.h" | 
|  | 83 | #include "llvm/Pass.h" | 
|  | 84 | #include "llvm/Support/Casting.h" | 
|  | 85 | #include "llvm/Support/CommandLine.h" | 
| Chandler Carruth | aafe091 | 2012-06-29 12:38:19 +0000 | [diff] [blame] | 86 | #include "llvm/Support/Debug.h" | 
|  | 87 | #include "llvm/Support/raw_ostream.h" | 
| Dehao Chen | b9f8e29 | 2016-07-12 18:45:51 +0000 | [diff] [blame] | 88 | #include "llvm/Transforms/Scalar.h" | 
| Xin Tong | 99c4e2f | 2018-04-08 13:19:53 +0000 | [diff] [blame] | 89 | #include "llvm/Transforms/Scalar/LoopIdiomRecognize.h" | 
| Ahmed Bougacha | ace97c1 | 2016-04-27 19:04:50 +0000 | [diff] [blame] | 90 | #include "llvm/Transforms/Utils/BuildLibCalls.h" | 
| Chandler Carruth | 31088a9 | 2016-02-19 10:45:18 +0000 | [diff] [blame] | 91 | #include "llvm/Transforms/Utils/LoopUtils.h" | 
| Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 92 | #include <algorithm> | 
|  | 93 | #include <cassert> | 
|  | 94 | #include <cstdint> | 
|  | 95 | #include <utility> | 
|  | 96 | #include <vector> | 
|  | 97 |  | 
| Chris Lattner | 81ae3f2 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 98 | using namespace llvm; | 
|  | 99 |  | 
| Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 100 | #define DEBUG_TYPE "loop-idiom" | 
|  | 101 |  | 
| Chandler Carruth | 099f5cb0 | 2012-11-02 08:33:25 +0000 | [diff] [blame] | 102 | STATISTIC(NumMemSet, "Number of memset's formed from loop stores"); | 
|  | 103 | STATISTIC(NumMemCpy, "Number of memcpy's formed from loop load+stores"); | 
| Chris Lattner | 81ae3f2 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 104 |  | 
| Andrew Kaylor | 7cdf01e | 2016-08-11 18:28:33 +0000 | [diff] [blame] | 105 | static cl::opt<bool> UseLIRCodeSizeHeurs( | 
|  | 106 | "use-lir-code-size-heurs", | 
|  | 107 | cl::desc("Use loop idiom recognition code size heuristics when compiling" | 
|  | 108 | "with -Os/-Oz"), | 
|  | 109 | cl::init(true), cl::Hidden); | 
|  | 110 |  | 
| Chris Lattner | 81ae3f2 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 111 | namespace { | 
| Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 112 |  | 
| Dehao Chen | b9f8e29 | 2016-07-12 18:45:51 +0000 | [diff] [blame] | 113 | class LoopIdiomRecognize { | 
| Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 114 | Loop *CurLoop = nullptr; | 
| Chandler Carruth | bf143e2 | 2015-08-14 00:21:10 +0000 | [diff] [blame] | 115 | AliasAnalysis *AA; | 
| Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 116 | DominatorTree *DT; | 
| Chandler Carruth | 18c2669 | 2015-08-13 09:27:01 +0000 | [diff] [blame] | 117 | LoopInfo *LI; | 
| Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 118 | ScalarEvolution *SE; | 
|  | 119 | TargetLibraryInfo *TLI; | 
|  | 120 | const TargetTransformInfo *TTI; | 
| Chad Rosier | 43f9b48 | 2015-11-06 16:33:57 +0000 | [diff] [blame] | 121 | const DataLayout *DL; | 
| Andrew Kaylor | 7cdf01e | 2016-08-11 18:28:33 +0000 | [diff] [blame] | 122 | bool ApplyCodeSizeHeuristics; | 
| Chris Lattner | 81ae3f2 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 123 |  | 
| Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 124 | public: | 
| Dehao Chen | b9f8e29 | 2016-07-12 18:45:51 +0000 | [diff] [blame] | 125 | explicit LoopIdiomRecognize(AliasAnalysis *AA, DominatorTree *DT, | 
|  | 126 | LoopInfo *LI, ScalarEvolution *SE, | 
|  | 127 | TargetLibraryInfo *TLI, | 
|  | 128 | const TargetTransformInfo *TTI, | 
|  | 129 | const DataLayout *DL) | 
| Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 130 | : AA(AA), DT(DT), LI(LI), SE(SE), TLI(TLI), TTI(TTI), DL(DL) {} | 
| Chris Lattner | 81ae3f2 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 131 |  | 
| Dehao Chen | b9f8e29 | 2016-07-12 18:45:51 +0000 | [diff] [blame] | 132 | bool runOnLoop(Loop *L); | 
| Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 133 |  | 
| Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 134 | private: | 
| Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 135 | using StoreList = SmallVector<StoreInst *, 8>; | 
|  | 136 | using StoreListMap = MapVector<Value *, StoreList>; | 
|  | 137 |  | 
| Haicheng Wu | f1c00a2 | 2016-01-26 02:27:47 +0000 | [diff] [blame] | 138 | StoreListMap StoreRefsForMemset; | 
|  | 139 | StoreListMap StoreRefsForMemsetPattern; | 
| Haicheng Wu | 9d6c940 | 2016-01-04 21:43:14 +0000 | [diff] [blame] | 140 | StoreList StoreRefsForMemcpy; | 
|  | 141 | bool HasMemset; | 
|  | 142 | bool HasMemsetPattern; | 
|  | 143 | bool HasMemcpy; | 
| Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 144 |  | 
| Anna Thomas | 5ecb8f7 | 2017-05-19 17:05:36 +0000 | [diff] [blame] | 145 | /// Return code for isLegalStore() | 
|  | 146 | enum LegalStoreKind { | 
| Anna Thomas | ae3f752f | 2017-05-19 18:00:30 +0000 | [diff] [blame] | 147 | None = 0, | 
| Anna Thomas | 5ecb8f7 | 2017-05-19 17:05:36 +0000 | [diff] [blame] | 148 | Memset, | 
|  | 149 | MemsetPattern, | 
|  | 150 | Memcpy, | 
| Anna Thomas | b2a212c | 2017-06-06 16:45:25 +0000 | [diff] [blame] | 151 | UnorderedAtomicMemcpy, | 
| Anna Thomas | ae3f752f | 2017-05-19 18:00:30 +0000 | [diff] [blame] | 152 | DontUse // Dummy retval never to be used. Allows catching errors in retval | 
|  | 153 | // handling. | 
| Anna Thomas | 5ecb8f7 | 2017-05-19 17:05:36 +0000 | [diff] [blame] | 154 | }; | 
| Chad Rosier | cc9030b | 2015-11-11 23:00:59 +0000 | [diff] [blame] | 155 |  | 
| Chandler Carruth | d9c6070 | 2015-08-13 00:10:03 +0000 | [diff] [blame] | 156 | /// \name Countable Loop Idiom Handling | 
|  | 157 | /// @{ | 
|  | 158 |  | 
| Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 159 | bool runOnCountableLoop(); | 
| Chandler Carruth | d9c6070 | 2015-08-13 00:10:03 +0000 | [diff] [blame] | 160 | bool runOnLoopBlock(BasicBlock *BB, const SCEV *BECount, | 
|  | 161 | SmallVectorImpl<BasicBlock *> &ExitBlocks); | 
|  | 162 |  | 
| Chad Rosier | cc9030b | 2015-11-11 23:00:59 +0000 | [diff] [blame] | 163 | void collectStores(BasicBlock *BB); | 
| Anna Thomas | 5ecb8f7 | 2017-05-19 17:05:36 +0000 | [diff] [blame] | 164 | LegalStoreKind isLegalStore(StoreInst *SI); | 
| JF Bastien | 7e2dd2d | 2018-09-07 18:17:59 +0000 | [diff] [blame] | 165 | enum class ForMemset { No, Yes }; | 
| Haicheng Wu | f1c00a2 | 2016-01-26 02:27:47 +0000 | [diff] [blame] | 166 | bool processLoopStores(SmallVectorImpl<StoreInst *> &SL, const SCEV *BECount, | 
| JF Bastien | 7e2dd2d | 2018-09-07 18:17:59 +0000 | [diff] [blame] | 167 | ForMemset For); | 
| Chandler Carruth | d9c6070 | 2015-08-13 00:10:03 +0000 | [diff] [blame] | 168 | bool processLoopMemSet(MemSetInst *MSI, const SCEV *BECount); | 
|  | 169 |  | 
|  | 170 | bool processLoopStridedStore(Value *DestPtr, unsigned StoreSize, | 
| Haicheng Wu | 9d6c940 | 2016-01-04 21:43:14 +0000 | [diff] [blame] | 171 | unsigned StoreAlignment, Value *StoredVal, | 
| Haicheng Wu | f1c00a2 | 2016-01-26 02:27:47 +0000 | [diff] [blame] | 172 | Instruction *TheStore, | 
|  | 173 | SmallPtrSetImpl<Instruction *> &Stores, | 
|  | 174 | const SCEVAddRecExpr *Ev, const SCEV *BECount, | 
| Andrew Kaylor | 7cdf01e | 2016-08-11 18:28:33 +0000 | [diff] [blame] | 175 | bool NegStride, bool IsLoopMemset = false); | 
| Haicheng Wu | 9d6c940 | 2016-01-04 21:43:14 +0000 | [diff] [blame] | 176 | bool processLoopStoreOfLoopLoad(StoreInst *SI, const SCEV *BECount); | 
| Andrew Kaylor | 7cdf01e | 2016-08-11 18:28:33 +0000 | [diff] [blame] | 177 | bool avoidLIRForMultiBlockLoop(bool IsMemset = false, | 
|  | 178 | bool IsLoopMemset = false); | 
| Chandler Carruth | d9c6070 | 2015-08-13 00:10:03 +0000 | [diff] [blame] | 179 |  | 
|  | 180 | /// @} | 
|  | 181 | /// \name Noncountable Loop Idiom Handling | 
|  | 182 | /// @{ | 
|  | 183 |  | 
|  | 184 | bool runOnNoncountableLoop(); | 
|  | 185 |  | 
| Chandler Carruth | 8219a50 | 2015-08-13 00:44:29 +0000 | [diff] [blame] | 186 | bool recognizePopcount(); | 
|  | 187 | void transformLoopToPopcount(BasicBlock *PreCondBB, Instruction *CntInst, | 
|  | 188 | PHINode *CntPhi, Value *Var); | 
| Craig Topper | c9a6000 | 2018-12-26 21:59:48 +0000 | [diff] [blame] | 189 | bool recognizeAndInsertFFS();  /// Find First Set: ctlz or cttz | 
|  | 190 | void transformLoopToCountable(Intrinsic::ID IntrinID, BasicBlock *PreCondBB, | 
|  | 191 | Instruction *CntInst, PHINode *CntPhi, | 
|  | 192 | Value *Var, Instruction *DefX, | 
| Craig Topper | 2835278 | 2018-07-08 01:45:47 +0000 | [diff] [blame] | 193 | const DebugLoc &DL, bool ZeroCheck, | 
|  | 194 | bool IsCntPhiUsedOutsideLoop); | 
| Chandler Carruth | 8219a50 | 2015-08-13 00:44:29 +0000 | [diff] [blame] | 195 |  | 
| Chandler Carruth | d9c6070 | 2015-08-13 00:10:03 +0000 | [diff] [blame] | 196 | /// @} | 
| Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 197 | }; | 
|  | 198 |  | 
| Dehao Chen | b9f8e29 | 2016-07-12 18:45:51 +0000 | [diff] [blame] | 199 | class LoopIdiomRecognizeLegacyPass : public LoopPass { | 
|  | 200 | public: | 
|  | 201 | static char ID; | 
| Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 202 |  | 
| Dehao Chen | b9f8e29 | 2016-07-12 18:45:51 +0000 | [diff] [blame] | 203 | explicit LoopIdiomRecognizeLegacyPass() : LoopPass(ID) { | 
|  | 204 | initializeLoopIdiomRecognizeLegacyPassPass( | 
|  | 205 | *PassRegistry::getPassRegistry()); | 
|  | 206 | } | 
|  | 207 |  | 
|  | 208 | bool runOnLoop(Loop *L, LPPassManager &LPM) override { | 
|  | 209 | if (skipLoop(L)) | 
|  | 210 | return false; | 
|  | 211 |  | 
|  | 212 | AliasAnalysis *AA = &getAnalysis<AAResultsWrapperPass>().getAAResults(); | 
|  | 213 | DominatorTree *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); | 
|  | 214 | LoopInfo *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); | 
|  | 215 | ScalarEvolution *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE(); | 
|  | 216 | TargetLibraryInfo *TLI = | 
|  | 217 | &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); | 
|  | 218 | const TargetTransformInfo *TTI = | 
|  | 219 | &getAnalysis<TargetTransformInfoWrapperPass>().getTTI( | 
|  | 220 | *L->getHeader()->getParent()); | 
|  | 221 | const DataLayout *DL = &L->getHeader()->getModule()->getDataLayout(); | 
|  | 222 |  | 
|  | 223 | LoopIdiomRecognize LIR(AA, DT, LI, SE, TLI, TTI, DL); | 
|  | 224 | return LIR.runOnLoop(L); | 
|  | 225 | } | 
|  | 226 |  | 
|  | 227 | /// This transformation requires natural loop information & requires that | 
|  | 228 | /// loop preheaders be inserted into the CFG. | 
| Dehao Chen | b9f8e29 | 2016-07-12 18:45:51 +0000 | [diff] [blame] | 229 | void getAnalysisUsage(AnalysisUsage &AU) const override { | 
|  | 230 | AU.addRequired<TargetLibraryInfoWrapperPass>(); | 
|  | 231 | AU.addRequired<TargetTransformInfoWrapperPass>(); | 
|  | 232 | getLoopAnalysisUsage(AU); | 
|  | 233 | } | 
|  | 234 | }; | 
| Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 235 |  | 
|  | 236 | } // end anonymous namespace | 
|  | 237 |  | 
|  | 238 | char LoopIdiomRecognizeLegacyPass::ID = 0; | 
| Chris Lattner | 81ae3f2 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 239 |  | 
| Chandler Carruth | 410eaeb | 2017-01-11 06:23:21 +0000 | [diff] [blame] | 240 | PreservedAnalyses LoopIdiomRecognizePass::run(Loop &L, LoopAnalysisManager &AM, | 
|  | 241 | LoopStandardAnalysisResults &AR, | 
|  | 242 | LPMUpdater &) { | 
| Dehao Chen | b9f8e29 | 2016-07-12 18:45:51 +0000 | [diff] [blame] | 243 | const auto *DL = &L.getHeader()->getModule()->getDataLayout(); | 
| Dehao Chen | b9f8e29 | 2016-07-12 18:45:51 +0000 | [diff] [blame] | 244 |  | 
| Chandler Carruth | 410eaeb | 2017-01-11 06:23:21 +0000 | [diff] [blame] | 245 | LoopIdiomRecognize LIR(&AR.AA, &AR.DT, &AR.LI, &AR.SE, &AR.TLI, &AR.TTI, DL); | 
| Dehao Chen | b9f8e29 | 2016-07-12 18:45:51 +0000 | [diff] [blame] | 246 | if (!LIR.runOnLoop(&L)) | 
|  | 247 | return PreservedAnalyses::all(); | 
|  | 248 |  | 
|  | 249 | return getLoopPassPreservedAnalyses(); | 
|  | 250 | } | 
|  | 251 |  | 
| Dehao Chen | b9f8e29 | 2016-07-12 18:45:51 +0000 | [diff] [blame] | 252 | INITIALIZE_PASS_BEGIN(LoopIdiomRecognizeLegacyPass, "loop-idiom", | 
|  | 253 | "Recognize loop idioms", false, false) | 
| Chandler Carruth | 31088a9 | 2016-02-19 10:45:18 +0000 | [diff] [blame] | 254 | INITIALIZE_PASS_DEPENDENCY(LoopPass) | 
| Chandler Carruth | b98f63d | 2015-01-15 10:41:28 +0000 | [diff] [blame] | 255 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) | 
| Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 256 | INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass) | 
| Dehao Chen | b9f8e29 | 2016-07-12 18:45:51 +0000 | [diff] [blame] | 257 | INITIALIZE_PASS_END(LoopIdiomRecognizeLegacyPass, "loop-idiom", | 
|  | 258 | "Recognize loop idioms", false, false) | 
| Chris Lattner | 81ae3f2 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 259 |  | 
| Dehao Chen | b9f8e29 | 2016-07-12 18:45:51 +0000 | [diff] [blame] | 260 | Pass *llvm::createLoopIdiomPass() { return new LoopIdiomRecognizeLegacyPass(); } | 
| Chris Lattner | 81ae3f2 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 261 |  | 
| David Majnemer | c5601df | 2016-06-20 16:03:25 +0000 | [diff] [blame] | 262 | static void deleteDeadInstruction(Instruction *I) { | 
| Benjamin Kramer | f094d77 | 2015-02-07 21:37:08 +0000 | [diff] [blame] | 263 | I->replaceAllUsesWith(UndefValue::get(I->getType())); | 
|  | 264 | I->eraseFromParent(); | 
| Chandler Carruth | 099f5cb0 | 2012-11-02 08:33:25 +0000 | [diff] [blame] | 265 | } | 
|  | 266 |  | 
| Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 267 | //===----------------------------------------------------------------------===// | 
|  | 268 | // | 
| Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 269 | //          Implementation of LoopIdiomRecognize | 
|  | 270 | // | 
|  | 271 | //===----------------------------------------------------------------------===// | 
|  | 272 |  | 
| Dehao Chen | b9f8e29 | 2016-07-12 18:45:51 +0000 | [diff] [blame] | 273 | bool LoopIdiomRecognize::runOnLoop(Loop *L) { | 
| Chandler Carruth | d9c6070 | 2015-08-13 00:10:03 +0000 | [diff] [blame] | 274 | CurLoop = L; | 
| Chandler Carruth | d9c6070 | 2015-08-13 00:10:03 +0000 | [diff] [blame] | 275 | // If the loop could not be converted to canonical form, it must have an | 
|  | 276 | // indirectbr in it, just give up. | 
|  | 277 | if (!L->getLoopPreheader()) | 
|  | 278 | return false; | 
|  | 279 |  | 
|  | 280 | // Disable loop idiom recognition if the function's name is a common idiom. | 
|  | 281 | StringRef Name = L->getHeader()->getParent()->getName(); | 
|  | 282 | if (Name == "memset" || Name == "memcpy") | 
|  | 283 | return false; | 
|  | 284 |  | 
| Andrew Kaylor | 7cdf01e | 2016-08-11 18:28:33 +0000 | [diff] [blame] | 285 | // Determine if code size heuristics need to be applied. | 
|  | 286 | ApplyCodeSizeHeuristics = | 
| Evandro Menezes | 85bd397 | 2019-04-04 22:40:06 +0000 | [diff] [blame] | 287 | L->getHeader()->getParent()->hasOptSize() && UseLIRCodeSizeHeurs; | 
| Andrew Kaylor | 7cdf01e | 2016-08-11 18:28:33 +0000 | [diff] [blame] | 288 |  | 
| David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 289 | HasMemset = TLI->has(LibFunc_memset); | 
|  | 290 | HasMemsetPattern = TLI->has(LibFunc_memset_pattern16); | 
|  | 291 | HasMemcpy = TLI->has(LibFunc_memcpy); | 
| Haicheng Wu | 9d6c940 | 2016-01-04 21:43:14 +0000 | [diff] [blame] | 292 |  | 
|  | 293 | if (HasMemset || HasMemsetPattern || HasMemcpy) | 
|  | 294 | if (SE->hasLoopInvariantBackedgeTakenCount(L)) | 
|  | 295 | return runOnCountableLoop(); | 
| Chandler Carruth | dc29832 | 2015-08-13 01:03:26 +0000 | [diff] [blame] | 296 |  | 
| Chandler Carruth | d9c6070 | 2015-08-13 00:10:03 +0000 | [diff] [blame] | 297 | return runOnNoncountableLoop(); | 
|  | 298 | } | 
|  | 299 |  | 
| Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 300 | bool LoopIdiomRecognize::runOnCountableLoop() { | 
|  | 301 | const SCEV *BECount = SE->getBackedgeTakenCount(CurLoop); | 
| Davide Italiano | 8ed0446 | 2015-05-11 21:02:34 +0000 | [diff] [blame] | 302 | assert(!isa<SCEVCouldNotCompute>(BECount) && | 
| Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 303 | "runOnCountableLoop() called on a loop without a predictable" | 
|  | 304 | "backedge-taken count"); | 
| Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 305 |  | 
|  | 306 | // If this loop executes exactly one time, then it should be peeled, not | 
|  | 307 | // optimized by this pass. | 
|  | 308 | if (const SCEVConstant *BECst = dyn_cast<SCEVConstant>(BECount)) | 
| Sanjoy Das | 0de2fec | 2015-12-17 20:28:46 +0000 | [diff] [blame] | 309 | if (BECst->getAPInt() == 0) | 
| Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 310 | return false; | 
|  | 311 |  | 
| Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 312 | SmallVector<BasicBlock *, 8> ExitBlocks; | 
| Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 313 | CurLoop->getUniqueExitBlocks(ExitBlocks); | 
|  | 314 |  | 
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 315 | LLVM_DEBUG(dbgs() << "loop-idiom Scanning: F[" | 
|  | 316 | << CurLoop->getHeader()->getParent()->getName() | 
|  | 317 | << "] Loop %" << CurLoop->getHeader()->getName() << "\n"); | 
| Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 318 |  | 
|  | 319 | bool MadeChange = false; | 
| Haicheng Wu | a95cd126 | 2016-07-06 21:05:40 +0000 | [diff] [blame] | 320 |  | 
|  | 321 | // The following transforms hoist stores/memsets into the loop pre-header. | 
|  | 322 | // Give up if the loop has instructions may throw. | 
| Max Kazantsev | 9c90ec2 | 2018-10-16 08:31:05 +0000 | [diff] [blame] | 323 | SimpleLoopSafetyInfo SafetyInfo; | 
| Max Kazantsev | 530b8d1 | 2018-08-15 05:55:43 +0000 | [diff] [blame] | 324 | SafetyInfo.computeLoopSafetyInfo(CurLoop); | 
|  | 325 | if (SafetyInfo.anyBlockMayThrow()) | 
| Haicheng Wu | a95cd126 | 2016-07-06 21:05:40 +0000 | [diff] [blame] | 326 | return MadeChange; | 
|  | 327 |  | 
| Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 328 | // Scan all the blocks in the loop that are not in subloops. | 
| Davide Italiano | 95a77e8 | 2015-05-14 21:52:12 +0000 | [diff] [blame] | 329 | for (auto *BB : CurLoop->getBlocks()) { | 
| Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 330 | // Ignore blocks in subloops. | 
| Chandler Carruth | 18c2669 | 2015-08-13 09:27:01 +0000 | [diff] [blame] | 331 | if (LI->getLoopFor(BB) != CurLoop) | 
| Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 332 | continue; | 
|  | 333 |  | 
| Davide Italiano | 80625af | 2015-05-13 19:51:21 +0000 | [diff] [blame] | 334 | MadeChange |= runOnLoopBlock(BB, BECount, ExitBlocks); | 
| Shuxin Yang | 95de7c3 | 2012-12-09 03:12:46 +0000 | [diff] [blame] | 335 | } | 
|  | 336 | return MadeChange; | 
|  | 337 | } | 
|  | 338 |  | 
| Chad Rosier | 4acff96 | 2016-02-12 19:05:27 +0000 | [diff] [blame] | 339 | static APInt getStoreStride(const SCEVAddRecExpr *StoreEv) { | 
| Chad Rosier | a548fe5 | 2015-11-12 19:09:16 +0000 | [diff] [blame] | 340 | const SCEVConstant *ConstStride = cast<SCEVConstant>(StoreEv->getOperand(1)); | 
| Chad Rosier | 4acff96 | 2016-02-12 19:05:27 +0000 | [diff] [blame] | 341 | return ConstStride->getAPInt(); | 
| Chad Rosier | a548fe5 | 2015-11-12 19:09:16 +0000 | [diff] [blame] | 342 | } | 
|  | 343 |  | 
| Chad Rosier | 94274fb | 2015-12-21 14:49:32 +0000 | [diff] [blame] | 344 | /// getMemSetPatternValue - If a strided store of the specified value is safe to | 
|  | 345 | /// turn into a memset_pattern16, return a ConstantArray of 16 bytes that should | 
|  | 346 | /// be passed in.  Otherwise, return null. | 
|  | 347 | /// | 
|  | 348 | /// Note that we don't ever attempt to use memset_pattern8 or 4, because these | 
|  | 349 | /// just replicate their input array and then pass on to memset_pattern16. | 
|  | 350 | static Constant *getMemSetPatternValue(Value *V, const DataLayout *DL) { | 
| JF Bastien | 73d8e4e | 2018-09-21 05:17:42 +0000 | [diff] [blame] | 351 | // FIXME: This could check for UndefValue because it can be merged into any | 
|  | 352 | // other valid pattern. | 
|  | 353 |  | 
| Chad Rosier | 94274fb | 2015-12-21 14:49:32 +0000 | [diff] [blame] | 354 | // If the value isn't a constant, we can't promote it to being in a constant | 
|  | 355 | // array.  We could theoretically do a store to an alloca or something, but | 
|  | 356 | // that doesn't seem worthwhile. | 
|  | 357 | Constant *C = dyn_cast<Constant>(V); | 
|  | 358 | if (!C) | 
|  | 359 | return nullptr; | 
|  | 360 |  | 
|  | 361 | // Only handle simple values that are a power of two bytes in size. | 
|  | 362 | uint64_t Size = DL->getTypeSizeInBits(V->getType()); | 
|  | 363 | if (Size == 0 || (Size & 7) || (Size & (Size - 1))) | 
|  | 364 | return nullptr; | 
|  | 365 |  | 
|  | 366 | // Don't care enough about darwin/ppc to implement this. | 
|  | 367 | if (DL->isBigEndian()) | 
|  | 368 | return nullptr; | 
|  | 369 |  | 
|  | 370 | // Convert to size in bytes. | 
|  | 371 | Size /= 8; | 
|  | 372 |  | 
|  | 373 | // TODO: If CI is larger than 16-bytes, we can try slicing it in half to see | 
|  | 374 | // if the top and bottom are the same (e.g. for vectors and large integers). | 
|  | 375 | if (Size > 16) | 
|  | 376 | return nullptr; | 
|  | 377 |  | 
|  | 378 | // If the constant is exactly 16 bytes, just use it. | 
|  | 379 | if (Size == 16) | 
|  | 380 | return C; | 
|  | 381 |  | 
|  | 382 | // Otherwise, we'll use an array of the constants. | 
|  | 383 | unsigned ArraySize = 16 / Size; | 
|  | 384 | ArrayType *AT = ArrayType::get(V->getType(), ArraySize); | 
|  | 385 | return ConstantArray::get(AT, std::vector<Constant *>(ArraySize, C)); | 
|  | 386 | } | 
|  | 387 |  | 
| Anna Thomas | ae3f752f | 2017-05-19 18:00:30 +0000 | [diff] [blame] | 388 | LoopIdiomRecognize::LegalStoreKind | 
|  | 389 | LoopIdiomRecognize::isLegalStore(StoreInst *SI) { | 
| Chad Rosier | 869962f | 2015-12-01 14:26:35 +0000 | [diff] [blame] | 390 | // Don't touch volatile stores. | 
| Anna Thomas | b2a212c | 2017-06-06 16:45:25 +0000 | [diff] [blame] | 391 | if (SI->isVolatile()) | 
|  | 392 | return LegalStoreKind::None; | 
|  | 393 | // We only want simple or unordered-atomic stores. | 
|  | 394 | if (!SI->isUnordered()) | 
| Anna Thomas | 5ecb8f7 | 2017-05-19 17:05:36 +0000 | [diff] [blame] | 395 | return LegalStoreKind::None; | 
| Chad Rosier | 869962f | 2015-12-01 14:26:35 +0000 | [diff] [blame] | 396 |  | 
| Sanjoy Das | 206f65c | 2017-04-24 20:12:10 +0000 | [diff] [blame] | 397 | // Don't convert stores of non-integral pointer types to memsets (which stores | 
|  | 398 | // integers). | 
|  | 399 | if (DL->isNonIntegralPointerType(SI->getValueOperand()->getType())) | 
| Anna Thomas | 5ecb8f7 | 2017-05-19 17:05:36 +0000 | [diff] [blame] | 400 | return LegalStoreKind::None; | 
| Sanjoy Das | 206f65c | 2017-04-24 20:12:10 +0000 | [diff] [blame] | 401 |  | 
| Haicheng Wu | 57e1a3e | 2016-02-17 21:00:06 +0000 | [diff] [blame] | 402 | // Avoid merging nontemporal stores. | 
|  | 403 | if (SI->getMetadata(LLVMContext::MD_nontemporal)) | 
| Anna Thomas | 5ecb8f7 | 2017-05-19 17:05:36 +0000 | [diff] [blame] | 404 | return LegalStoreKind::None; | 
| Haicheng Wu | 57e1a3e | 2016-02-17 21:00:06 +0000 | [diff] [blame] | 405 |  | 
| Chad Rosier | a548fe5 | 2015-11-12 19:09:16 +0000 | [diff] [blame] | 406 | Value *StoredVal = SI->getValueOperand(); | 
|  | 407 | Value *StorePtr = SI->getPointerOperand(); | 
|  | 408 |  | 
|  | 409 | // Reject stores that are so large that they overflow an unsigned. | 
|  | 410 | uint64_t SizeInBits = DL->getTypeSizeInBits(StoredVal->getType()); | 
|  | 411 | if ((SizeInBits & 7) || (SizeInBits >> 32) != 0) | 
| Anna Thomas | 5ecb8f7 | 2017-05-19 17:05:36 +0000 | [diff] [blame] | 412 | return LegalStoreKind::None; | 
| Chad Rosier | a548fe5 | 2015-11-12 19:09:16 +0000 | [diff] [blame] | 413 |  | 
|  | 414 | // See if the pointer expression is an AddRec like {base,+,1} on the current | 
|  | 415 | // loop, which indicates a strided store.  If we have something else, it's a | 
|  | 416 | // random store we can't handle. | 
|  | 417 | const SCEVAddRecExpr *StoreEv = | 
|  | 418 | dyn_cast<SCEVAddRecExpr>(SE->getSCEV(StorePtr)); | 
|  | 419 | if (!StoreEv || StoreEv->getLoop() != CurLoop || !StoreEv->isAffine()) | 
| Anna Thomas | 5ecb8f7 | 2017-05-19 17:05:36 +0000 | [diff] [blame] | 420 | return LegalStoreKind::None; | 
| Chad Rosier | a548fe5 | 2015-11-12 19:09:16 +0000 | [diff] [blame] | 421 |  | 
|  | 422 | // Check to see if we have a constant stride. | 
|  | 423 | if (!isa<SCEVConstant>(StoreEv->getOperand(1))) | 
| Anna Thomas | 5ecb8f7 | 2017-05-19 17:05:36 +0000 | [diff] [blame] | 424 | return LegalStoreKind::None; | 
| Chad Rosier | a548fe5 | 2015-11-12 19:09:16 +0000 | [diff] [blame] | 425 |  | 
| Haicheng Wu | 9d6c940 | 2016-01-04 21:43:14 +0000 | [diff] [blame] | 426 | // See if the store can be turned into a memset. | 
|  | 427 |  | 
|  | 428 | // If the stored value is a byte-wise value (like i32 -1), then it may be | 
|  | 429 | // turned into a memset of i8 -1, assuming that all the consecutive bytes | 
|  | 430 | // are stored.  A store of i32 0x01020304 can never be turned into a memset, | 
|  | 431 | // but it can be turned into memset_pattern if the target supports it. | 
|  | 432 | Value *SplatValue = isBytewiseValue(StoredVal); | 
|  | 433 | Constant *PatternValue = nullptr; | 
|  | 434 |  | 
| Anna Thomas | b2a212c | 2017-06-06 16:45:25 +0000 | [diff] [blame] | 435 | // Note: memset and memset_pattern on unordered-atomic is yet not supported | 
|  | 436 | bool UnorderedAtomic = SI->isUnordered() && !SI->isSimple(); | 
|  | 437 |  | 
| Haicheng Wu | 9d6c940 | 2016-01-04 21:43:14 +0000 | [diff] [blame] | 438 | // If we're allowed to form a memset, and the stored value would be | 
|  | 439 | // acceptable for memset, use it. | 
| Anna Thomas | b2a212c | 2017-06-06 16:45:25 +0000 | [diff] [blame] | 440 | if (!UnorderedAtomic && HasMemset && SplatValue && | 
| Haicheng Wu | 9d6c940 | 2016-01-04 21:43:14 +0000 | [diff] [blame] | 441 | // Verify that the stored value is loop invariant.  If not, we can't | 
|  | 442 | // promote the memset. | 
|  | 443 | CurLoop->isLoopInvariant(SplatValue)) { | 
|  | 444 | // It looks like we can use SplatValue. | 
| Anna Thomas | 5ecb8f7 | 2017-05-19 17:05:36 +0000 | [diff] [blame] | 445 | return LegalStoreKind::Memset; | 
| Anna Thomas | b2a212c | 2017-06-06 16:45:25 +0000 | [diff] [blame] | 446 | } else if (!UnorderedAtomic && HasMemsetPattern && | 
| Haicheng Wu | 9d6c940 | 2016-01-04 21:43:14 +0000 | [diff] [blame] | 447 | // Don't create memset_pattern16s with address spaces. | 
|  | 448 | StorePtr->getType()->getPointerAddressSpace() == 0 && | 
|  | 449 | (PatternValue = getMemSetPatternValue(StoredVal, DL))) { | 
|  | 450 | // It looks like we can use PatternValue! | 
| Anna Thomas | 5ecb8f7 | 2017-05-19 17:05:36 +0000 | [diff] [blame] | 451 | return LegalStoreKind::MemsetPattern; | 
| Haicheng Wu | 9d6c940 | 2016-01-04 21:43:14 +0000 | [diff] [blame] | 452 | } | 
|  | 453 |  | 
|  | 454 | // Otherwise, see if the store can be turned into a memcpy. | 
|  | 455 | if (HasMemcpy) { | 
|  | 456 | // Check to see if the stride matches the size of the store.  If so, then we | 
|  | 457 | // know that every byte is touched in the loop. | 
| Chad Rosier | 4acff96 | 2016-02-12 19:05:27 +0000 | [diff] [blame] | 458 | APInt Stride = getStoreStride(StoreEv); | 
| Jonas Paulsson | f0ff20f | 2017-11-28 14:44:32 +0000 | [diff] [blame] | 459 | unsigned StoreSize = DL->getTypeStoreSize(SI->getValueOperand()->getType()); | 
| Haicheng Wu | 9d6c940 | 2016-01-04 21:43:14 +0000 | [diff] [blame] | 460 | if (StoreSize != Stride && StoreSize != -Stride) | 
| Anna Thomas | 5ecb8f7 | 2017-05-19 17:05:36 +0000 | [diff] [blame] | 461 | return LegalStoreKind::None; | 
| Haicheng Wu | 9d6c940 | 2016-01-04 21:43:14 +0000 | [diff] [blame] | 462 |  | 
|  | 463 | // The store must be feeding a non-volatile load. | 
|  | 464 | LoadInst *LI = dyn_cast<LoadInst>(SI->getValueOperand()); | 
| Anna Thomas | b2a212c | 2017-06-06 16:45:25 +0000 | [diff] [blame] | 465 |  | 
|  | 466 | // Only allow non-volatile loads | 
|  | 467 | if (!LI || LI->isVolatile()) | 
|  | 468 | return LegalStoreKind::None; | 
|  | 469 | // Only allow simple or unordered-atomic loads | 
|  | 470 | if (!LI->isUnordered()) | 
| Anna Thomas | 5ecb8f7 | 2017-05-19 17:05:36 +0000 | [diff] [blame] | 471 | return LegalStoreKind::None; | 
| Haicheng Wu | 9d6c940 | 2016-01-04 21:43:14 +0000 | [diff] [blame] | 472 |  | 
|  | 473 | // See if the pointer expression is an AddRec like {base,+,1} on the current | 
|  | 474 | // loop, which indicates a strided load.  If we have something else, it's a | 
|  | 475 | // random load we can't handle. | 
|  | 476 | const SCEVAddRecExpr *LoadEv = | 
|  | 477 | dyn_cast<SCEVAddRecExpr>(SE->getSCEV(LI->getPointerOperand())); | 
|  | 478 | if (!LoadEv || LoadEv->getLoop() != CurLoop || !LoadEv->isAffine()) | 
| Anna Thomas | 5ecb8f7 | 2017-05-19 17:05:36 +0000 | [diff] [blame] | 479 | return LegalStoreKind::None; | 
| Haicheng Wu | 9d6c940 | 2016-01-04 21:43:14 +0000 | [diff] [blame] | 480 |  | 
|  | 481 | // The store and load must share the same stride. | 
|  | 482 | if (StoreEv->getOperand(1) != LoadEv->getOperand(1)) | 
| Anna Thomas | 5ecb8f7 | 2017-05-19 17:05:36 +0000 | [diff] [blame] | 483 | return LegalStoreKind::None; | 
| Haicheng Wu | 9d6c940 | 2016-01-04 21:43:14 +0000 | [diff] [blame] | 484 |  | 
|  | 485 | // Success.  This store can be converted into a memcpy. | 
| Anna Thomas | b2a212c | 2017-06-06 16:45:25 +0000 | [diff] [blame] | 486 | UnorderedAtomic = UnorderedAtomic || LI->isAtomic(); | 
|  | 487 | return UnorderedAtomic ? LegalStoreKind::UnorderedAtomicMemcpy | 
|  | 488 | : LegalStoreKind::Memcpy; | 
| Haicheng Wu | 9d6c940 | 2016-01-04 21:43:14 +0000 | [diff] [blame] | 489 | } | 
|  | 490 | // This store can't be transformed into a memset/memcpy. | 
| Anna Thomas | 5ecb8f7 | 2017-05-19 17:05:36 +0000 | [diff] [blame] | 491 | return LegalStoreKind::None; | 
| Chad Rosier | a548fe5 | 2015-11-12 19:09:16 +0000 | [diff] [blame] | 492 | } | 
|  | 493 |  | 
| Chad Rosier | cc9030b | 2015-11-11 23:00:59 +0000 | [diff] [blame] | 494 | void LoopIdiomRecognize::collectStores(BasicBlock *BB) { | 
| Haicheng Wu | 9d6c940 | 2016-01-04 21:43:14 +0000 | [diff] [blame] | 495 | StoreRefsForMemset.clear(); | 
| Haicheng Wu | f1c00a2 | 2016-01-26 02:27:47 +0000 | [diff] [blame] | 496 | StoreRefsForMemsetPattern.clear(); | 
| Haicheng Wu | 9d6c940 | 2016-01-04 21:43:14 +0000 | [diff] [blame] | 497 | StoreRefsForMemcpy.clear(); | 
| Chad Rosier | cc9030b | 2015-11-11 23:00:59 +0000 | [diff] [blame] | 498 | for (Instruction &I : *BB) { | 
|  | 499 | StoreInst *SI = dyn_cast<StoreInst>(&I); | 
|  | 500 | if (!SI) | 
|  | 501 | continue; | 
|  | 502 |  | 
| Chad Rosier | a548fe5 | 2015-11-12 19:09:16 +0000 | [diff] [blame] | 503 | // Make sure this is a strided store with a constant stride. | 
| Anna Thomas | 5ecb8f7 | 2017-05-19 17:05:36 +0000 | [diff] [blame] | 504 | switch (isLegalStore(SI)) { | 
|  | 505 | case LegalStoreKind::None: | 
|  | 506 | // Nothing to do | 
|  | 507 | break; | 
|  | 508 | case LegalStoreKind::Memset: { | 
| Haicheng Wu | f1c00a2 | 2016-01-26 02:27:47 +0000 | [diff] [blame] | 509 | // Find the base pointer. | 
|  | 510 | Value *Ptr = GetUnderlyingObject(SI->getPointerOperand(), *DL); | 
|  | 511 | StoreRefsForMemset[Ptr].push_back(SI); | 
| Anna Thomas | 5ecb8f7 | 2017-05-19 17:05:36 +0000 | [diff] [blame] | 512 | } break; | 
|  | 513 | case LegalStoreKind::MemsetPattern: { | 
| Haicheng Wu | f1c00a2 | 2016-01-26 02:27:47 +0000 | [diff] [blame] | 514 | // Find the base pointer. | 
|  | 515 | Value *Ptr = GetUnderlyingObject(SI->getPointerOperand(), *DL); | 
|  | 516 | StoreRefsForMemsetPattern[Ptr].push_back(SI); | 
| Anna Thomas | 5ecb8f7 | 2017-05-19 17:05:36 +0000 | [diff] [blame] | 517 | } break; | 
|  | 518 | case LegalStoreKind::Memcpy: | 
| Anna Thomas | b2a212c | 2017-06-06 16:45:25 +0000 | [diff] [blame] | 519 | case LegalStoreKind::UnorderedAtomicMemcpy: | 
| Haicheng Wu | 9d6c940 | 2016-01-04 21:43:14 +0000 | [diff] [blame] | 520 | StoreRefsForMemcpy.push_back(SI); | 
| Anna Thomas | 5ecb8f7 | 2017-05-19 17:05:36 +0000 | [diff] [blame] | 521 | break; | 
|  | 522 | default: | 
|  | 523 | assert(false && "unhandled return value"); | 
|  | 524 | break; | 
|  | 525 | } | 
| Chad Rosier | cc9030b | 2015-11-11 23:00:59 +0000 | [diff] [blame] | 526 | } | 
|  | 527 | } | 
|  | 528 |  | 
| Chris Lattner | 8455b6e | 2011-01-02 19:01:03 +0000 | [diff] [blame] | 529 | /// runOnLoopBlock - Process the specified block, which lives in a counted loop | 
|  | 530 | /// with the specified backedge count.  This block is known to be in the current | 
|  | 531 | /// loop and not in any subloops. | 
| Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 532 | bool LoopIdiomRecognize::runOnLoopBlock( | 
|  | 533 | BasicBlock *BB, const SCEV *BECount, | 
|  | 534 | SmallVectorImpl<BasicBlock *> &ExitBlocks) { | 
| Chris Lattner | 8455b6e | 2011-01-02 19:01:03 +0000 | [diff] [blame] | 535 | // We can only promote stores in this block if they are unconditionally | 
|  | 536 | // executed in the loop.  For a block to be unconditionally executed, it has | 
|  | 537 | // to dominate all the exit blocks of the loop.  Verify this now. | 
|  | 538 | for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) | 
|  | 539 | if (!DT->dominates(BB, ExitBlocks[i])) | 
|  | 540 | return false; | 
| Andrew Trick | 328b223 | 2011-03-14 16:48:10 +0000 | [diff] [blame] | 541 |  | 
| Chris Lattner | 7c5f9c3 | 2010-12-26 20:45:45 +0000 | [diff] [blame] | 542 | bool MadeChange = false; | 
| Chad Rosier | cc9030b | 2015-11-11 23:00:59 +0000 | [diff] [blame] | 543 | // Look for store instructions, which may be optimized to memset/memcpy. | 
|  | 544 | collectStores(BB); | 
| Haicheng Wu | 9d6c940 | 2016-01-04 21:43:14 +0000 | [diff] [blame] | 545 |  | 
| Haicheng Wu | f1c00a2 | 2016-01-26 02:27:47 +0000 | [diff] [blame] | 546 | // Look for a single store or sets of stores with a common base, which can be | 
|  | 547 | // optimized into a memset (memset_pattern).  The latter most commonly happens | 
|  | 548 | // with structs and handunrolled loops. | 
|  | 549 | for (auto &SL : StoreRefsForMemset) | 
| JF Bastien | 7e2dd2d | 2018-09-07 18:17:59 +0000 | [diff] [blame] | 550 | MadeChange |= processLoopStores(SL.second, BECount, ForMemset::Yes); | 
| Haicheng Wu | f1c00a2 | 2016-01-26 02:27:47 +0000 | [diff] [blame] | 551 |  | 
|  | 552 | for (auto &SL : StoreRefsForMemsetPattern) | 
| JF Bastien | 7e2dd2d | 2018-09-07 18:17:59 +0000 | [diff] [blame] | 553 | MadeChange |= processLoopStores(SL.second, BECount, ForMemset::No); | 
| Chad Rosier | cc9030b | 2015-11-11 23:00:59 +0000 | [diff] [blame] | 554 |  | 
| Haicheng Wu | 9d6c940 | 2016-01-04 21:43:14 +0000 | [diff] [blame] | 555 | // Optimize the store into a memcpy, if it feeds an similarly strided load. | 
|  | 556 | for (auto &SI : StoreRefsForMemcpy) | 
|  | 557 | MadeChange |= processLoopStoreOfLoopLoad(SI, BECount); | 
|  | 558 |  | 
| Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 559 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E;) { | 
| Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 560 | Instruction *Inst = &*I++; | 
| Chris Lattner | 8643810 | 2011-01-04 07:46:33 +0000 | [diff] [blame] | 561 | // Look for memset instructions, which may be optimized to a larger memset. | 
| Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 562 | if (MemSetInst *MSI = dyn_cast<MemSetInst>(Inst)) { | 
| Sanjoy Das | e6bca0e | 2017-05-01 17:07:49 +0000 | [diff] [blame] | 563 | WeakTrackingVH InstPtr(&*I); | 
| Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 564 | if (!processLoopMemSet(MSI, BECount)) | 
|  | 565 | continue; | 
| Chris Lattner | 8643810 | 2011-01-04 07:46:33 +0000 | [diff] [blame] | 566 | MadeChange = true; | 
| Andrew Trick | 328b223 | 2011-03-14 16:48:10 +0000 | [diff] [blame] | 567 |  | 
| Chris Lattner | 8643810 | 2011-01-04 07:46:33 +0000 | [diff] [blame] | 568 | // If processing the memset invalidated our iterator, start over from the | 
|  | 569 | // top of the block. | 
| Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 570 | if (!InstPtr) | 
| Chris Lattner | 8643810 | 2011-01-04 07:46:33 +0000 | [diff] [blame] | 571 | I = BB->begin(); | 
|  | 572 | continue; | 
|  | 573 | } | 
| Chris Lattner | 7c5f9c3 | 2010-12-26 20:45:45 +0000 | [diff] [blame] | 574 | } | 
| Andrew Trick | 328b223 | 2011-03-14 16:48:10 +0000 | [diff] [blame] | 575 |  | 
| Chris Lattner | 7c5f9c3 | 2010-12-26 20:45:45 +0000 | [diff] [blame] | 576 | return MadeChange; | 
| Chris Lattner | 81ae3f2 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 577 | } | 
|  | 578 |  | 
| JF Bastien | 7e2dd2d | 2018-09-07 18:17:59 +0000 | [diff] [blame] | 579 | /// See if this store(s) can be promoted to a memset. | 
| Haicheng Wu | f1c00a2 | 2016-01-26 02:27:47 +0000 | [diff] [blame] | 580 | bool LoopIdiomRecognize::processLoopStores(SmallVectorImpl<StoreInst *> &SL, | 
| JF Bastien | 7e2dd2d | 2018-09-07 18:17:59 +0000 | [diff] [blame] | 581 | const SCEV *BECount, ForMemset For) { | 
| Haicheng Wu | f1c00a2 | 2016-01-26 02:27:47 +0000 | [diff] [blame] | 582 | // Try to find consecutive stores that can be transformed into memsets. | 
|  | 583 | SetVector<StoreInst *> Heads, Tails; | 
|  | 584 | SmallDenseMap<StoreInst *, StoreInst *> ConsecutiveChain; | 
| Chris Lattner | 8643810 | 2011-01-04 07:46:33 +0000 | [diff] [blame] | 585 |  | 
| Haicheng Wu | f1c00a2 | 2016-01-26 02:27:47 +0000 | [diff] [blame] | 586 | // Do a quadratic search on all of the given stores and find | 
|  | 587 | // all of the pairs of stores that follow each other. | 
|  | 588 | SmallVector<unsigned, 16> IndexQueue; | 
|  | 589 | for (unsigned i = 0, e = SL.size(); i < e; ++i) { | 
|  | 590 | assert(SL[i]->isSimple() && "Expected only non-volatile stores."); | 
| Andrew Trick | 328b223 | 2011-03-14 16:48:10 +0000 | [diff] [blame] | 591 |  | 
| Haicheng Wu | f1c00a2 | 2016-01-26 02:27:47 +0000 | [diff] [blame] | 592 | Value *FirstStoredVal = SL[i]->getValueOperand(); | 
|  | 593 | Value *FirstStorePtr = SL[i]->getPointerOperand(); | 
|  | 594 | const SCEVAddRecExpr *FirstStoreEv = | 
|  | 595 | cast<SCEVAddRecExpr>(SE->getSCEV(FirstStorePtr)); | 
| Chad Rosier | 4acff96 | 2016-02-12 19:05:27 +0000 | [diff] [blame] | 596 | APInt FirstStride = getStoreStride(FirstStoreEv); | 
| Jonas Paulsson | f0ff20f | 2017-11-28 14:44:32 +0000 | [diff] [blame] | 597 | unsigned FirstStoreSize = DL->getTypeStoreSize(SL[i]->getValueOperand()->getType()); | 
| Chad Rosier | 7967614 | 2015-10-28 14:38:49 +0000 | [diff] [blame] | 598 |  | 
| Haicheng Wu | f1c00a2 | 2016-01-26 02:27:47 +0000 | [diff] [blame] | 599 | // See if we can optimize just this store in isolation. | 
| Chad Rosier | 4acff96 | 2016-02-12 19:05:27 +0000 | [diff] [blame] | 600 | if (FirstStride == FirstStoreSize || -FirstStride == FirstStoreSize) { | 
| Haicheng Wu | f1c00a2 | 2016-01-26 02:27:47 +0000 | [diff] [blame] | 601 | Heads.insert(SL[i]); | 
|  | 602 | continue; | 
|  | 603 | } | 
| Chris Lattner | 0f4a640 | 2011-02-19 19:31:39 +0000 | [diff] [blame] | 604 |  | 
| Haicheng Wu | f1c00a2 | 2016-01-26 02:27:47 +0000 | [diff] [blame] | 605 | Value *FirstSplatValue = nullptr; | 
|  | 606 | Constant *FirstPatternValue = nullptr; | 
|  | 607 |  | 
| JF Bastien | 7e2dd2d | 2018-09-07 18:17:59 +0000 | [diff] [blame] | 608 | if (For == ForMemset::Yes) | 
| Haicheng Wu | f1c00a2 | 2016-01-26 02:27:47 +0000 | [diff] [blame] | 609 | FirstSplatValue = isBytewiseValue(FirstStoredVal); | 
|  | 610 | else | 
|  | 611 | FirstPatternValue = getMemSetPatternValue(FirstStoredVal, DL); | 
|  | 612 |  | 
|  | 613 | assert((FirstSplatValue || FirstPatternValue) && | 
|  | 614 | "Expected either splat value or pattern value."); | 
|  | 615 |  | 
|  | 616 | IndexQueue.clear(); | 
|  | 617 | // If a store has multiple consecutive store candidates, search Stores | 
|  | 618 | // array according to the sequence: from i+1 to e, then from i-1 to 0. | 
|  | 619 | // This is because usually pairing with immediate succeeding or preceding | 
|  | 620 | // candidate create the best chance to find memset opportunity. | 
|  | 621 | unsigned j = 0; | 
|  | 622 | for (j = i + 1; j < e; ++j) | 
|  | 623 | IndexQueue.push_back(j); | 
|  | 624 | for (j = i; j > 0; --j) | 
|  | 625 | IndexQueue.push_back(j - 1); | 
|  | 626 |  | 
|  | 627 | for (auto &k : IndexQueue) { | 
|  | 628 | assert(SL[k]->isSimple() && "Expected only non-volatile stores."); | 
|  | 629 | Value *SecondStorePtr = SL[k]->getPointerOperand(); | 
|  | 630 | const SCEVAddRecExpr *SecondStoreEv = | 
|  | 631 | cast<SCEVAddRecExpr>(SE->getSCEV(SecondStorePtr)); | 
| Chad Rosier | 4acff96 | 2016-02-12 19:05:27 +0000 | [diff] [blame] | 632 | APInt SecondStride = getStoreStride(SecondStoreEv); | 
| Haicheng Wu | f1c00a2 | 2016-01-26 02:27:47 +0000 | [diff] [blame] | 633 |  | 
|  | 634 | if (FirstStride != SecondStride) | 
|  | 635 | continue; | 
|  | 636 |  | 
|  | 637 | Value *SecondStoredVal = SL[k]->getValueOperand(); | 
|  | 638 | Value *SecondSplatValue = nullptr; | 
|  | 639 | Constant *SecondPatternValue = nullptr; | 
|  | 640 |  | 
| JF Bastien | 7e2dd2d | 2018-09-07 18:17:59 +0000 | [diff] [blame] | 641 | if (For == ForMemset::Yes) | 
| Haicheng Wu | f1c00a2 | 2016-01-26 02:27:47 +0000 | [diff] [blame] | 642 | SecondSplatValue = isBytewiseValue(SecondStoredVal); | 
|  | 643 | else | 
|  | 644 | SecondPatternValue = getMemSetPatternValue(SecondStoredVal, DL); | 
|  | 645 |  | 
|  | 646 | assert((SecondSplatValue || SecondPatternValue) && | 
|  | 647 | "Expected either splat value or pattern value."); | 
|  | 648 |  | 
|  | 649 | if (isConsecutiveAccess(SL[i], SL[k], *DL, *SE, false)) { | 
| JF Bastien | 7e2dd2d | 2018-09-07 18:17:59 +0000 | [diff] [blame] | 650 | if (For == ForMemset::Yes) { | 
| JF Bastien | 73d8e4e | 2018-09-21 05:17:42 +0000 | [diff] [blame] | 651 | if (isa<UndefValue>(FirstSplatValue)) | 
|  | 652 | FirstSplatValue = SecondSplatValue; | 
| Haicheng Wu | f1c00a2 | 2016-01-26 02:27:47 +0000 | [diff] [blame] | 653 | if (FirstSplatValue != SecondSplatValue) | 
|  | 654 | continue; | 
|  | 655 | } else { | 
| JF Bastien | 73d8e4e | 2018-09-21 05:17:42 +0000 | [diff] [blame] | 656 | if (isa<UndefValue>(FirstPatternValue)) | 
|  | 657 | FirstPatternValue = SecondPatternValue; | 
| Haicheng Wu | f1c00a2 | 2016-01-26 02:27:47 +0000 | [diff] [blame] | 658 | if (FirstPatternValue != SecondPatternValue) | 
|  | 659 | continue; | 
|  | 660 | } | 
|  | 661 | Tails.insert(SL[k]); | 
|  | 662 | Heads.insert(SL[i]); | 
|  | 663 | ConsecutiveChain[SL[i]] = SL[k]; | 
|  | 664 | break; | 
|  | 665 | } | 
|  | 666 | } | 
|  | 667 | } | 
|  | 668 |  | 
|  | 669 | // We may run into multiple chains that merge into a single chain. We mark the | 
|  | 670 | // stores that we transformed so that we don't visit the same store twice. | 
|  | 671 | SmallPtrSet<Value *, 16> TransformedStores; | 
|  | 672 | bool Changed = false; | 
|  | 673 |  | 
|  | 674 | // For stores that start but don't end a link in the chain: | 
|  | 675 | for (SetVector<StoreInst *>::iterator it = Heads.begin(), e = Heads.end(); | 
|  | 676 | it != e; ++it) { | 
|  | 677 | if (Tails.count(*it)) | 
|  | 678 | continue; | 
|  | 679 |  | 
|  | 680 | // We found a store instr that starts a chain. Now follow the chain and try | 
|  | 681 | // to transform it. | 
|  | 682 | SmallPtrSet<Instruction *, 8> AdjacentStores; | 
|  | 683 | StoreInst *I = *it; | 
|  | 684 |  | 
|  | 685 | StoreInst *HeadStore = I; | 
|  | 686 | unsigned StoreSize = 0; | 
|  | 687 |  | 
|  | 688 | // Collect the chain into a list. | 
|  | 689 | while (Tails.count(I) || Heads.count(I)) { | 
|  | 690 | if (TransformedStores.count(I)) | 
|  | 691 | break; | 
|  | 692 | AdjacentStores.insert(I); | 
|  | 693 |  | 
| Jonas Paulsson | f0ff20f | 2017-11-28 14:44:32 +0000 | [diff] [blame] | 694 | StoreSize += DL->getTypeStoreSize(I->getValueOperand()->getType()); | 
| Haicheng Wu | f1c00a2 | 2016-01-26 02:27:47 +0000 | [diff] [blame] | 695 | // Move to the next value in the chain. | 
|  | 696 | I = ConsecutiveChain[I]; | 
|  | 697 | } | 
|  | 698 |  | 
|  | 699 | Value *StoredVal = HeadStore->getValueOperand(); | 
|  | 700 | Value *StorePtr = HeadStore->getPointerOperand(); | 
|  | 701 | const SCEVAddRecExpr *StoreEv = cast<SCEVAddRecExpr>(SE->getSCEV(StorePtr)); | 
| Chad Rosier | 4acff96 | 2016-02-12 19:05:27 +0000 | [diff] [blame] | 702 | APInt Stride = getStoreStride(StoreEv); | 
| Haicheng Wu | f1c00a2 | 2016-01-26 02:27:47 +0000 | [diff] [blame] | 703 |  | 
|  | 704 | // Check to see if the stride matches the size of the stores.  If so, then | 
|  | 705 | // we know that every byte is touched in the loop. | 
|  | 706 | if (StoreSize != Stride && StoreSize != -Stride) | 
|  | 707 | continue; | 
|  | 708 |  | 
|  | 709 | bool NegStride = StoreSize == -Stride; | 
|  | 710 |  | 
|  | 711 | if (processLoopStridedStore(StorePtr, StoreSize, HeadStore->getAlignment(), | 
|  | 712 | StoredVal, HeadStore, AdjacentStores, StoreEv, | 
|  | 713 | BECount, NegStride)) { | 
|  | 714 | TransformedStores.insert(AdjacentStores.begin(), AdjacentStores.end()); | 
|  | 715 | Changed = true; | 
|  | 716 | } | 
|  | 717 | } | 
|  | 718 |  | 
|  | 719 | return Changed; | 
| Chris Lattner | 81ae3f2 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 720 | } | 
|  | 721 |  | 
| Chris Lattner | 8643810 | 2011-01-04 07:46:33 +0000 | [diff] [blame] | 722 | /// processLoopMemSet - See if this memset can be promoted to a large memset. | 
| Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 723 | bool LoopIdiomRecognize::processLoopMemSet(MemSetInst *MSI, | 
|  | 724 | const SCEV *BECount) { | 
| Chris Lattner | 8643810 | 2011-01-04 07:46:33 +0000 | [diff] [blame] | 725 | // We can only handle non-volatile memsets with a constant size. | 
| Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 726 | if (MSI->isVolatile() || !isa<ConstantInt>(MSI->getLength())) | 
|  | 727 | return false; | 
| Chris Lattner | 8643810 | 2011-01-04 07:46:33 +0000 | [diff] [blame] | 728 |  | 
| Chris Lattner | e6b261f | 2011-02-18 22:22:15 +0000 | [diff] [blame] | 729 | // If we're not allowed to hack on memset, we fail. | 
| Ahmed Bougacha | 7f97193 | 2016-04-27 19:04:46 +0000 | [diff] [blame] | 730 | if (!HasMemset) | 
| Chris Lattner | e6b261f | 2011-02-18 22:22:15 +0000 | [diff] [blame] | 731 | return false; | 
| Andrew Trick | 328b223 | 2011-03-14 16:48:10 +0000 | [diff] [blame] | 732 |  | 
| Chris Lattner | 8643810 | 2011-01-04 07:46:33 +0000 | [diff] [blame] | 733 | Value *Pointer = MSI->getDest(); | 
| Andrew Trick | 328b223 | 2011-03-14 16:48:10 +0000 | [diff] [blame] | 734 |  | 
| Chris Lattner | 8643810 | 2011-01-04 07:46:33 +0000 | [diff] [blame] | 735 | // See if the pointer expression is an AddRec like {base,+,1} on the current | 
|  | 736 | // loop, which indicates a strided store.  If we have something else, it's a | 
|  | 737 | // random store we can't handle. | 
|  | 738 | const SCEVAddRecExpr *Ev = dyn_cast<SCEVAddRecExpr>(SE->getSCEV(Pointer)); | 
| Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 739 | if (!Ev || Ev->getLoop() != CurLoop || !Ev->isAffine()) | 
| Chris Lattner | 8643810 | 2011-01-04 07:46:33 +0000 | [diff] [blame] | 740 | return false; | 
|  | 741 |  | 
|  | 742 | // Reject memsets that are so large that they overflow an unsigned. | 
|  | 743 | uint64_t SizeInBytes = cast<ConstantInt>(MSI->getLength())->getZExtValue(); | 
|  | 744 | if ((SizeInBytes >> 32) != 0) | 
|  | 745 | return false; | 
| Andrew Trick | 328b223 | 2011-03-14 16:48:10 +0000 | [diff] [blame] | 746 |  | 
| Chris Lattner | 8643810 | 2011-01-04 07:46:33 +0000 | [diff] [blame] | 747 | // Check to see if the stride matches the size of the memset.  If so, then we | 
|  | 748 | // know that every byte is touched in the loop. | 
| Chad Rosier | 81362a8 | 2016-02-12 21:03:23 +0000 | [diff] [blame] | 749 | const SCEVConstant *ConstStride = dyn_cast<SCEVConstant>(Ev->getOperand(1)); | 
|  | 750 | if (!ConstStride) | 
|  | 751 | return false; | 
| Andrew Trick | 328b223 | 2011-03-14 16:48:10 +0000 | [diff] [blame] | 752 |  | 
| Chad Rosier | 81362a8 | 2016-02-12 21:03:23 +0000 | [diff] [blame] | 753 | APInt Stride = ConstStride->getAPInt(); | 
|  | 754 | if (SizeInBytes != Stride && SizeInBytes != -Stride) | 
| Chris Lattner | 8643810 | 2011-01-04 07:46:33 +0000 | [diff] [blame] | 755 | return false; | 
| Andrew Trick | 328b223 | 2011-03-14 16:48:10 +0000 | [diff] [blame] | 756 |  | 
| Haicheng Wu | 9d6c940 | 2016-01-04 21:43:14 +0000 | [diff] [blame] | 757 | // Verify that the memset value is loop invariant.  If not, we can't promote | 
|  | 758 | // the memset. | 
|  | 759 | Value *SplatValue = MSI->getValue(); | 
|  | 760 | if (!SplatValue || !CurLoop->isLoopInvariant(SplatValue)) | 
|  | 761 | return false; | 
|  | 762 |  | 
| Haicheng Wu | f1c00a2 | 2016-01-26 02:27:47 +0000 | [diff] [blame] | 763 | SmallPtrSet<Instruction *, 1> MSIs; | 
|  | 764 | MSIs.insert(MSI); | 
| Chad Rosier | 81362a8 | 2016-02-12 21:03:23 +0000 | [diff] [blame] | 765 | bool NegStride = SizeInBytes == -Stride; | 
| Chris Lattner | 0f4a640 | 2011-02-19 19:31:39 +0000 | [diff] [blame] | 766 | return processLoopStridedStore(Pointer, (unsigned)SizeInBytes, | 
| Daniel Neilson | fb99a49 | 2018-02-08 17:33:08 +0000 | [diff] [blame] | 767 | MSI->getDestAlignment(), SplatValue, MSI, MSIs, | 
|  | 768 | Ev, BECount, NegStride, /*IsLoopMemset=*/true); | 
| Chris Lattner | 8643810 | 2011-01-04 07:46:33 +0000 | [diff] [blame] | 769 | } | 
|  | 770 |  | 
| Chandler Carruth | 099f5cb0 | 2012-11-02 08:33:25 +0000 | [diff] [blame] | 771 | /// mayLoopAccessLocation - Return true if the specified loop might access the | 
|  | 772 | /// specified pointer location, which is a loop-strided access.  The 'Access' | 
|  | 773 | /// argument specifies what the verboten forms of access are (read or write). | 
| Haicheng Wu | f1c00a2 | 2016-01-26 02:27:47 +0000 | [diff] [blame] | 774 | static bool | 
|  | 775 | mayLoopAccessLocation(Value *Ptr, ModRefInfo Access, Loop *L, | 
|  | 776 | const SCEV *BECount, unsigned StoreSize, | 
|  | 777 | AliasAnalysis &AA, | 
|  | 778 | SmallPtrSetImpl<Instruction *> &IgnoredStores) { | 
| Chandler Carruth | 099f5cb0 | 2012-11-02 08:33:25 +0000 | [diff] [blame] | 779 | // Get the location that may be stored across the loop.  Since the access is | 
|  | 780 | // strided positively through memory, we say that the modified location starts | 
|  | 781 | // at the pointer and has infinite size. | 
| George Burgess IV | 7e12875 | 2018-12-24 05:55:50 +0000 | [diff] [blame] | 782 | LocationSize AccessSize = LocationSize::unknown(); | 
| Chandler Carruth | 099f5cb0 | 2012-11-02 08:33:25 +0000 | [diff] [blame] | 783 |  | 
|  | 784 | // If the loop iterates a fixed number of times, we can refine the access size | 
|  | 785 | // to be exactly the size of the memset, which is (BECount+1)*StoreSize | 
|  | 786 | if (const SCEVConstant *BECst = dyn_cast<SCEVConstant>(BECount)) | 
| George Burgess IV | 7e12875 | 2018-12-24 05:55:50 +0000 | [diff] [blame] | 787 | AccessSize = LocationSize::precise((BECst->getValue()->getZExtValue() + 1) * | 
|  | 788 | StoreSize); | 
| Chandler Carruth | 099f5cb0 | 2012-11-02 08:33:25 +0000 | [diff] [blame] | 789 |  | 
|  | 790 | // TODO: For this to be really effective, we have to dive into the pointer | 
|  | 791 | // operand in the store.  Store to &A[i] of 100 will always return may alias | 
|  | 792 | // with store of &A[100], we need to StoreLoc to be "A" with size of 100, | 
|  | 793 | // which will then no-alias a store to &A[100]. | 
| Chandler Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 794 | MemoryLocation StoreLoc(Ptr, AccessSize); | 
| Chandler Carruth | 099f5cb0 | 2012-11-02 08:33:25 +0000 | [diff] [blame] | 795 |  | 
|  | 796 | for (Loop::block_iterator BI = L->block_begin(), E = L->block_end(); BI != E; | 
|  | 797 | ++BI) | 
| Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 798 | for (Instruction &I : **BI) | 
|  | 799 | if (IgnoredStores.count(&I) == 0 && | 
| Alina Sbirlea | 18fea01 | 2017-12-06 19:56:37 +0000 | [diff] [blame] | 800 | isModOrRefSet( | 
|  | 801 | intersectModRef(AA.getModRefInfo(&I, StoreLoc), Access))) | 
| Chandler Carruth | 099f5cb0 | 2012-11-02 08:33:25 +0000 | [diff] [blame] | 802 | return true; | 
|  | 803 |  | 
|  | 804 | return false; | 
|  | 805 | } | 
|  | 806 |  | 
| Chad Rosier | ed0c7d1 | 2015-11-13 19:11:07 +0000 | [diff] [blame] | 807 | // If we have a negative stride, Start refers to the end of the memory location | 
|  | 808 | // we're trying to memset.  Therefore, we need to recompute the base pointer, | 
|  | 809 | // which is just Start - BECount*Size. | 
|  | 810 | static const SCEV *getStartForNegStride(const SCEV *Start, const SCEV *BECount, | 
|  | 811 | Type *IntPtr, unsigned StoreSize, | 
|  | 812 | ScalarEvolution *SE) { | 
|  | 813 | const SCEV *Index = SE->getTruncateOrZeroExtend(BECount, IntPtr); | 
|  | 814 | if (StoreSize != 1) | 
|  | 815 | Index = SE->getMulExpr(Index, SE->getConstant(IntPtr, StoreSize), | 
|  | 816 | SCEV::FlagNUW); | 
|  | 817 | return SE->getMinusSCEV(Start, Index); | 
|  | 818 | } | 
|  | 819 |  | 
| Chandler Carruth | 1dc34c6 | 2017-07-25 10:48:32 +0000 | [diff] [blame] | 820 | /// Compute the number of bytes as a SCEV from the backedge taken count. | 
|  | 821 | /// | 
|  | 822 | /// This also maps the SCEV into the provided type and tries to handle the | 
|  | 823 | /// computation in a way that will fold cleanly. | 
|  | 824 | static const SCEV *getNumBytes(const SCEV *BECount, Type *IntPtr, | 
|  | 825 | unsigned StoreSize, Loop *CurLoop, | 
|  | 826 | const DataLayout *DL, ScalarEvolution *SE) { | 
|  | 827 | const SCEV *NumBytesS; | 
|  | 828 | // The # stored bytes is (BECount+1)*Size.  Expand the trip count out to | 
|  | 829 | // pointer size if it isn't already. | 
|  | 830 | // | 
|  | 831 | // If we're going to need to zero extend the BE count, check if we can add | 
|  | 832 | // one to it prior to zero extending without overflow. Provided this is safe, | 
|  | 833 | // it allows better simplification of the +1. | 
|  | 834 | if (DL->getTypeSizeInBits(BECount->getType()) < | 
|  | 835 | DL->getTypeSizeInBits(IntPtr) && | 
|  | 836 | SE->isLoopEntryGuardedByCond( | 
|  | 837 | CurLoop, ICmpInst::ICMP_NE, BECount, | 
|  | 838 | SE->getNegativeSCEV(SE->getOne(BECount->getType())))) { | 
|  | 839 | NumBytesS = SE->getZeroExtendExpr( | 
|  | 840 | SE->getAddExpr(BECount, SE->getOne(BECount->getType()), SCEV::FlagNUW), | 
|  | 841 | IntPtr); | 
|  | 842 | } else { | 
|  | 843 | NumBytesS = SE->getAddExpr(SE->getTruncateOrZeroExtend(BECount, IntPtr), | 
|  | 844 | SE->getOne(IntPtr), SCEV::FlagNUW); | 
|  | 845 | } | 
|  | 846 |  | 
|  | 847 | // And scale it based on the store size. | 
|  | 848 | if (StoreSize != 1) { | 
|  | 849 | NumBytesS = SE->getMulExpr(NumBytesS, SE->getConstant(IntPtr, StoreSize), | 
|  | 850 | SCEV::FlagNUW); | 
|  | 851 | } | 
|  | 852 | return NumBytesS; | 
|  | 853 | } | 
|  | 854 |  | 
| Chris Lattner | 0f4a640 | 2011-02-19 19:31:39 +0000 | [diff] [blame] | 855 | /// processLoopStridedStore - We see a strided store of some value.  If we can | 
|  | 856 | /// 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] | 857 | bool LoopIdiomRecognize::processLoopStridedStore( | 
|  | 858 | Value *DestPtr, unsigned StoreSize, unsigned StoreAlignment, | 
| Haicheng Wu | f1c00a2 | 2016-01-26 02:27:47 +0000 | [diff] [blame] | 859 | Value *StoredVal, Instruction *TheStore, | 
|  | 860 | SmallPtrSetImpl<Instruction *> &Stores, const SCEVAddRecExpr *Ev, | 
| Andrew Kaylor | 7cdf01e | 2016-08-11 18:28:33 +0000 | [diff] [blame] | 861 | const SCEV *BECount, bool NegStride, bool IsLoopMemset) { | 
| Chris Lattner | 0f4a640 | 2011-02-19 19:31:39 +0000 | [diff] [blame] | 862 | Value *SplatValue = isBytewiseValue(StoredVal); | 
| Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 863 | Constant *PatternValue = nullptr; | 
| Matt Arsenault | 009faed | 2013-09-11 05:09:42 +0000 | [diff] [blame] | 864 |  | 
| Haicheng Wu | 9d6c940 | 2016-01-04 21:43:14 +0000 | [diff] [blame] | 865 | if (!SplatValue) | 
|  | 866 | PatternValue = getMemSetPatternValue(StoredVal, DL); | 
|  | 867 |  | 
|  | 868 | assert((SplatValue || PatternValue) && | 
|  | 869 | "Expected either splat value or pattern value."); | 
| Andrew Trick | 328b223 | 2011-03-14 16:48:10 +0000 | [diff] [blame] | 870 |  | 
| Chris Lattner | c4ca7ab | 2011-05-22 17:39:56 +0000 | [diff] [blame] | 871 | // The trip count of the loop and the base pointer of the addrec SCEV is | 
|  | 872 | // guaranteed to be loop invariant, which means that it should dominate the | 
|  | 873 | // header.  This allows us to insert code for it in the preheader. | 
| Haicheng Wu | 9d6c940 | 2016-01-04 21:43:14 +0000 | [diff] [blame] | 874 | unsigned DestAS = DestPtr->getType()->getPointerAddressSpace(); | 
| Chris Lattner | c4ca7ab | 2011-05-22 17:39:56 +0000 | [diff] [blame] | 875 | BasicBlock *Preheader = CurLoop->getLoopPreheader(); | 
|  | 876 | IRBuilder<> Builder(Preheader->getTerminator()); | 
| Chad Rosier | 43f9b48 | 2015-11-06 16:33:57 +0000 | [diff] [blame] | 877 | SCEVExpander Expander(*SE, *DL, "loop-idiom"); | 
| Andrew Trick | 60ab3ef | 2011-06-28 05:04:16 +0000 | [diff] [blame] | 878 |  | 
| Matt Arsenault | 009faed | 2013-09-11 05:09:42 +0000 | [diff] [blame] | 879 | Type *DestInt8PtrTy = Builder.getInt8PtrTy(DestAS); | 
| Chad Rosier | 43f9b48 | 2015-11-06 16:33:57 +0000 | [diff] [blame] | 880 | Type *IntPtr = Builder.getIntPtrTy(*DL, DestAS); | 
| Chad Rosier | 7967614 | 2015-10-28 14:38:49 +0000 | [diff] [blame] | 881 |  | 
|  | 882 | const SCEV *Start = Ev->getStart(); | 
| Chad Rosier | 2fa50a7 | 2015-11-13 19:13:40 +0000 | [diff] [blame] | 883 | // Handle negative strided loops. | 
| Chad Rosier | ed0c7d1 | 2015-11-13 19:11:07 +0000 | [diff] [blame] | 884 | if (NegStride) | 
|  | 885 | Start = getStartForNegStride(Start, BECount, IntPtr, StoreSize, SE); | 
| Matt Arsenault | 009faed | 2013-09-11 05:09:42 +0000 | [diff] [blame] | 886 |  | 
| Aditya Kumar | 1c42d13 | 2017-05-05 14:49:45 +0000 | [diff] [blame] | 887 | // TODO: ideally we should still be able to generate memset if SCEV expander | 
|  | 888 | // is taught to generate the dependencies at the latest point. | 
|  | 889 | if (!isSafeToExpand(Start, *SE)) | 
|  | 890 | return false; | 
|  | 891 |  | 
| Chris Lattner | 29e14ed | 2010-12-26 23:42:51 +0000 | [diff] [blame] | 892 | // 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] | 893 | // this into a memset in the loop preheader now if we want.  However, this | 
|  | 894 | // 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] | 895 | // or write to the aliased location.  Check for any overlap by generating the | 
|  | 896 | // base pointer and checking the region. | 
| Chad Rosier | 7967614 | 2015-10-28 14:38:49 +0000 | [diff] [blame] | 897 | Value *BasePtr = | 
|  | 898 | Expander.expandCodeFor(Start, DestInt8PtrTy, Preheader->getTerminator()); | 
| Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 899 | if (mayLoopAccessLocation(BasePtr, ModRefInfo::ModRef, CurLoop, BECount, | 
|  | 900 | StoreSize, *AA, Stores)) { | 
| Chandler Carruth | 099f5cb0 | 2012-11-02 08:33:25 +0000 | [diff] [blame] | 901 | Expander.clear(); | 
|  | 902 | // If we generated new code for the base pointer, clean up. | 
| Benjamin Kramer | f094d77 | 2015-02-07 21:37:08 +0000 | [diff] [blame] | 903 | RecursivelyDeleteTriviallyDeadInstructions(BasePtr, TLI); | 
| Chandler Carruth | 099f5cb0 | 2012-11-02 08:33:25 +0000 | [diff] [blame] | 904 | return false; | 
|  | 905 | } | 
|  | 906 |  | 
| Andrew Kaylor | 7cdf01e | 2016-08-11 18:28:33 +0000 | [diff] [blame] | 907 | if (avoidLIRForMultiBlockLoop(/*IsMemset=*/true, IsLoopMemset)) | 
|  | 908 | return false; | 
|  | 909 |  | 
| Chris Lattner | c4ca7ab | 2011-05-22 17:39:56 +0000 | [diff] [blame] | 910 | // Okay, everything looks good, insert the memset. | 
|  | 911 |  | 
| Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 912 | const SCEV *NumBytesS = | 
| Chandler Carruth | 1dc34c6 | 2017-07-25 10:48:32 +0000 | [diff] [blame] | 913 | getNumBytes(BECount, IntPtr, StoreSize, CurLoop, DL, SE); | 
| Andrew Trick | 328b223 | 2011-03-14 16:48:10 +0000 | [diff] [blame] | 914 |  | 
| Aditya Kumar | 1c42d13 | 2017-05-05 14:49:45 +0000 | [diff] [blame] | 915 | // TODO: ideally we should still be able to generate memset if SCEV expander | 
|  | 916 | // is taught to generate the dependencies at the latest point. | 
|  | 917 | if (!isSafeToExpand(NumBytesS, *SE)) | 
|  | 918 | return false; | 
|  | 919 |  | 
| Andrew Trick | 328b223 | 2011-03-14 16:48:10 +0000 | [diff] [blame] | 920 | Value *NumBytes = | 
| Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 921 | Expander.expandCodeFor(NumBytesS, IntPtr, Preheader->getTerminator()); | 
| Andrew Trick | 328b223 | 2011-03-14 16:48:10 +0000 | [diff] [blame] | 922 |  | 
| Devang Patel | d00c628 | 2011-03-07 22:43:45 +0000 | [diff] [blame] | 923 | CallInst *NewCall; | 
| Matt Arsenault | 5df49bd | 2013-09-11 05:09:35 +0000 | [diff] [blame] | 924 | if (SplatValue) { | 
| Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 925 | NewCall = | 
|  | 926 | Builder.CreateMemSet(BasePtr, SplatValue, NumBytes, StoreAlignment); | 
| Matt Arsenault | 5df49bd | 2013-09-11 05:09:35 +0000 | [diff] [blame] | 927 | } else { | 
| Matt Arsenault | 009faed | 2013-09-11 05:09:42 +0000 | [diff] [blame] | 928 | // Everything is emitted in default address space | 
|  | 929 | Type *Int8PtrTy = DestInt8PtrTy; | 
|  | 930 |  | 
| Sanjay Patel | af674fb | 2015-12-14 17:24:23 +0000 | [diff] [blame] | 931 | Module *M = TheStore->getModule(); | 
| David Bolvansky | 7c7760d | 2018-10-16 21:18:31 +0000 | [diff] [blame] | 932 | StringRef FuncName = "memset_pattern16"; | 
| James Y Knight | 1368022 | 2019-02-01 02:28:03 +0000 | [diff] [blame] | 933 | FunctionCallee MSP = M->getOrInsertFunction(FuncName, Builder.getVoidTy(), | 
|  | 934 | Int8PtrTy, Int8PtrTy, IntPtr); | 
| David Bolvansky | 7c7760d | 2018-10-16 21:18:31 +0000 | [diff] [blame] | 935 | inferLibFuncAttributes(M, FuncName, *TLI); | 
| Andrew Trick | 328b223 | 2011-03-14 16:48:10 +0000 | [diff] [blame] | 936 |  | 
| Chris Lattner | 0f4a640 | 2011-02-19 19:31:39 +0000 | [diff] [blame] | 937 | // Otherwise we should form a memset_pattern16.  PatternValue is known to be | 
|  | 938 | // an constant array of 16-bytes.  Plop the value into a mergable global. | 
|  | 939 | GlobalVariable *GV = new GlobalVariable(*M, PatternValue->getType(), true, | 
| Benjamin Kramer | 838752d | 2015-03-03 00:17:09 +0000 | [diff] [blame] | 940 | GlobalValue::PrivateLinkage, | 
| Chris Lattner | 0f4a640 | 2011-02-19 19:31:39 +0000 | [diff] [blame] | 941 | PatternValue, ".memset_pattern"); | 
| Peter Collingbourne | 96efdd6 | 2016-06-14 21:01:22 +0000 | [diff] [blame] | 942 | GV->setUnnamedAddr(GlobalValue::UnnamedAddr::Global); // Ok to merge these. | 
| Chris Lattner | 0f4a640 | 2011-02-19 19:31:39 +0000 | [diff] [blame] | 943 | GV->setAlignment(16); | 
| Matt Arsenault | 009faed | 2013-09-11 05:09:42 +0000 | [diff] [blame] | 944 | Value *PatternPtr = ConstantExpr::getBitCast(GV, Int8PtrTy); | 
| David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 945 | NewCall = Builder.CreateCall(MSP, {BasePtr, PatternPtr, NumBytes}); | 
| Chris Lattner | 0f4a640 | 2011-02-19 19:31:39 +0000 | [diff] [blame] | 946 | } | 
| Andrew Trick | 328b223 | 2011-03-14 16:48:10 +0000 | [diff] [blame] | 947 |  | 
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 948 | LLVM_DEBUG(dbgs() << "  Formed memset: " << *NewCall << "\n" | 
|  | 949 | << "    from store to: " << *Ev << " at: " << *TheStore | 
|  | 950 | << "\n"); | 
| Devang Patel | d00c628 | 2011-03-07 22:43:45 +0000 | [diff] [blame] | 951 | NewCall->setDebugLoc(TheStore->getDebugLoc()); | 
| Andrew Trick | 328b223 | 2011-03-14 16:48:10 +0000 | [diff] [blame] | 952 |  | 
| Chris Lattner | b9fe685 | 2010-12-27 00:03:23 +0000 | [diff] [blame] | 953 | // Okay, the memset has been formed.  Zap the original store and anything that | 
|  | 954 | // feeds into it. | 
| Haicheng Wu | f1c00a2 | 2016-01-26 02:27:47 +0000 | [diff] [blame] | 955 | for (auto *I : Stores) | 
| David Majnemer | 41ff4fd | 2016-06-20 16:07:38 +0000 | [diff] [blame] | 956 | deleteDeadInstruction(I); | 
| Chris Lattner | 12f91be | 2011-01-02 07:36:44 +0000 | [diff] [blame] | 957 | ++NumMemSet; | 
| Chris Lattner | 29e14ed | 2010-12-26 23:42:51 +0000 | [diff] [blame] | 958 | return true; | 
|  | 959 | } | 
|  | 960 |  | 
| Chad Rosier | 1cd3da1 | 2015-11-19 21:33:07 +0000 | [diff] [blame] | 961 | /// If the stored value is a strided load in the same loop with the same stride | 
|  | 962 | /// this may be transformable into a memcpy.  This kicks in for stuff like | 
| Xin Tong | a41bf70 | 2017-05-01 23:08:19 +0000 | [diff] [blame] | 963 | /// for (i) A[i] = B[i]; | 
| Haicheng Wu | 9d6c940 | 2016-01-04 21:43:14 +0000 | [diff] [blame] | 964 | bool LoopIdiomRecognize::processLoopStoreOfLoopLoad(StoreInst *SI, | 
|  | 965 | const SCEV *BECount) { | 
| Anna Thomas | b2a212c | 2017-06-06 16:45:25 +0000 | [diff] [blame] | 966 | assert(SI->isUnordered() && "Expected only non-volatile non-ordered stores."); | 
| Haicheng Wu | 9d6c940 | 2016-01-04 21:43:14 +0000 | [diff] [blame] | 967 |  | 
|  | 968 | Value *StorePtr = SI->getPointerOperand(); | 
|  | 969 | const SCEVAddRecExpr *StoreEv = cast<SCEVAddRecExpr>(SE->getSCEV(StorePtr)); | 
| Chad Rosier | 4acff96 | 2016-02-12 19:05:27 +0000 | [diff] [blame] | 970 | APInt Stride = getStoreStride(StoreEv); | 
| Jonas Paulsson | f0ff20f | 2017-11-28 14:44:32 +0000 | [diff] [blame] | 971 | unsigned StoreSize = DL->getTypeStoreSize(SI->getValueOperand()->getType()); | 
| Haicheng Wu | 9d6c940 | 2016-01-04 21:43:14 +0000 | [diff] [blame] | 972 | bool NegStride = StoreSize == -Stride; | 
| Andrew Trick | 328b223 | 2011-03-14 16:48:10 +0000 | [diff] [blame] | 973 |  | 
| Chad Rosier | fddc01f | 2015-11-19 18:22:21 +0000 | [diff] [blame] | 974 | // The store must be feeding a non-volatile load. | 
| Haicheng Wu | 9d6c940 | 2016-01-04 21:43:14 +0000 | [diff] [blame] | 975 | LoadInst *LI = cast<LoadInst>(SI->getValueOperand()); | 
| Anna Thomas | b2a212c | 2017-06-06 16:45:25 +0000 | [diff] [blame] | 976 | assert(LI->isUnordered() && "Expected only non-volatile non-ordered loads."); | 
| Chad Rosier | fddc01f | 2015-11-19 18:22:21 +0000 | [diff] [blame] | 977 |  | 
|  | 978 | // See if the pointer expression is an AddRec like {base,+,1} on the current | 
|  | 979 | // loop, which indicates a strided load.  If we have something else, it's a | 
|  | 980 | // random load we can't handle. | 
| Chad Rosier | 3ecc8d8 | 2015-11-19 18:25:11 +0000 | [diff] [blame] | 981 | const SCEVAddRecExpr *LoadEv = | 
| Haicheng Wu | 9d6c940 | 2016-01-04 21:43:14 +0000 | [diff] [blame] | 982 | cast<SCEVAddRecExpr>(SE->getSCEV(LI->getPointerOperand())); | 
| Andrew Trick | 328b223 | 2011-03-14 16:48:10 +0000 | [diff] [blame] | 983 |  | 
| Chris Lattner | c4ca7ab | 2011-05-22 17:39:56 +0000 | [diff] [blame] | 984 | // The trip count of the loop and the base pointer of the addrec SCEV is | 
|  | 985 | // guaranteed to be loop invariant, which means that it should dominate the | 
|  | 986 | // header.  This allows us to insert code for it in the preheader. | 
|  | 987 | BasicBlock *Preheader = CurLoop->getLoopPreheader(); | 
|  | 988 | IRBuilder<> Builder(Preheader->getTerminator()); | 
| Chad Rosier | 43f9b48 | 2015-11-06 16:33:57 +0000 | [diff] [blame] | 989 | SCEVExpander Expander(*SE, *DL, "loop-idiom"); | 
| Andrew Trick | 60ab3ef | 2011-06-28 05:04:16 +0000 | [diff] [blame] | 990 |  | 
| Chad Rosier | cc299b6 | 2015-11-13 21:51:02 +0000 | [diff] [blame] | 991 | const SCEV *StrStart = StoreEv->getStart(); | 
|  | 992 | unsigned StrAS = SI->getPointerAddressSpace(); | 
|  | 993 | Type *IntPtrTy = Builder.getIntPtrTy(*DL, StrAS); | 
|  | 994 |  | 
|  | 995 | // Handle negative strided loops. | 
|  | 996 | if (NegStride) | 
|  | 997 | StrStart = getStartForNegStride(StrStart, BECount, IntPtrTy, StoreSize, SE); | 
|  | 998 |  | 
| Chris Lattner | 85b6d81 | 2011-01-02 03:37:56 +0000 | [diff] [blame] | 999 | // 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] | 1000 | // this into a memcpy in the loop preheader now if we want.  However, this | 
|  | 1001 | // would be unsafe to do if there is anything else in the loop that may read | 
|  | 1002 | // or write the memory region we're storing to.  This includes the load that | 
|  | 1003 | // feeds the stores.  Check for an alias by generating the base address and | 
|  | 1004 | // checking everything. | 
| Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 1005 | Value *StoreBasePtr = Expander.expandCodeFor( | 
| Chad Rosier | cc299b6 | 2015-11-13 21:51:02 +0000 | [diff] [blame] | 1006 | StrStart, Builder.getInt8PtrTy(StrAS), Preheader->getTerminator()); | 
| Chandler Carruth | 099f5cb0 | 2012-11-02 08:33:25 +0000 | [diff] [blame] | 1007 |  | 
| Haicheng Wu | f1c00a2 | 2016-01-26 02:27:47 +0000 | [diff] [blame] | 1008 | SmallPtrSet<Instruction *, 1> Stores; | 
|  | 1009 | Stores.insert(SI); | 
| Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 1010 | if (mayLoopAccessLocation(StoreBasePtr, ModRefInfo::ModRef, CurLoop, BECount, | 
| Haicheng Wu | f1c00a2 | 2016-01-26 02:27:47 +0000 | [diff] [blame] | 1011 | StoreSize, *AA, Stores)) { | 
| Chandler Carruth | 099f5cb0 | 2012-11-02 08:33:25 +0000 | [diff] [blame] | 1012 | Expander.clear(); | 
|  | 1013 | // If we generated new code for the base pointer, clean up. | 
| Benjamin Kramer | f094d77 | 2015-02-07 21:37:08 +0000 | [diff] [blame] | 1014 | RecursivelyDeleteTriviallyDeadInstructions(StoreBasePtr, TLI); | 
| Chandler Carruth | 099f5cb0 | 2012-11-02 08:33:25 +0000 | [diff] [blame] | 1015 | return false; | 
|  | 1016 | } | 
|  | 1017 |  | 
| Chad Rosier | cc299b6 | 2015-11-13 21:51:02 +0000 | [diff] [blame] | 1018 | const SCEV *LdStart = LoadEv->getStart(); | 
|  | 1019 | unsigned LdAS = LI->getPointerAddressSpace(); | 
|  | 1020 |  | 
|  | 1021 | // Handle negative strided loops. | 
|  | 1022 | if (NegStride) | 
|  | 1023 | LdStart = getStartForNegStride(LdStart, BECount, IntPtrTy, StoreSize, SE); | 
|  | 1024 |  | 
| Chandler Carruth | 099f5cb0 | 2012-11-02 08:33:25 +0000 | [diff] [blame] | 1025 | // For a memcpy, we have to make sure that the input array is not being | 
|  | 1026 | // mutated by the loop. | 
| Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 1027 | Value *LoadBasePtr = Expander.expandCodeFor( | 
| Chad Rosier | cc299b6 | 2015-11-13 21:51:02 +0000 | [diff] [blame] | 1028 | LdStart, Builder.getInt8PtrTy(LdAS), Preheader->getTerminator()); | 
| Chris Lattner | c4ca7ab | 2011-05-22 17:39:56 +0000 | [diff] [blame] | 1029 |  | 
| Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 1030 | if (mayLoopAccessLocation(LoadBasePtr, ModRefInfo::Mod, CurLoop, BECount, | 
|  | 1031 | StoreSize, *AA, Stores)) { | 
| Chandler Carruth | 099f5cb0 | 2012-11-02 08:33:25 +0000 | [diff] [blame] | 1032 | Expander.clear(); | 
|  | 1033 | // If we generated new code for the base pointer, clean up. | 
| Benjamin Kramer | f094d77 | 2015-02-07 21:37:08 +0000 | [diff] [blame] | 1034 | RecursivelyDeleteTriviallyDeadInstructions(LoadBasePtr, TLI); | 
|  | 1035 | RecursivelyDeleteTriviallyDeadInstructions(StoreBasePtr, TLI); | 
| Chandler Carruth | 099f5cb0 | 2012-11-02 08:33:25 +0000 | [diff] [blame] | 1036 | return false; | 
|  | 1037 | } | 
|  | 1038 |  | 
| Andrew Kaylor | 7cdf01e | 2016-08-11 18:28:33 +0000 | [diff] [blame] | 1039 | if (avoidLIRForMultiBlockLoop()) | 
|  | 1040 | return false; | 
|  | 1041 |  | 
| Chris Lattner | c4ca7ab | 2011-05-22 17:39:56 +0000 | [diff] [blame] | 1042 | // Okay, everything is safe, we can transform this! | 
| Andrew Trick | 60ab3ef | 2011-06-28 05:04:16 +0000 | [diff] [blame] | 1043 |  | 
| Chandler Carruth | bad690e | 2015-08-12 23:06:37 +0000 | [diff] [blame] | 1044 | const SCEV *NumBytesS = | 
| Chandler Carruth | 1dc34c6 | 2017-07-25 10:48:32 +0000 | [diff] [blame] | 1045 | getNumBytes(BECount, IntPtrTy, StoreSize, CurLoop, DL, SE); | 
| Daniel Neilson | 3faabbb | 2017-06-16 14:43:59 +0000 | [diff] [blame] | 1046 |  | 
|  | 1047 | Value *NumBytes = | 
|  | 1048 | Expander.expandCodeFor(NumBytesS, IntPtrTy, Preheader->getTerminator()); | 
|  | 1049 |  | 
| Anna Thomas | b2a212c | 2017-06-06 16:45:25 +0000 | [diff] [blame] | 1050 | CallInst *NewCall = nullptr; | 
|  | 1051 | // Check whether to generate an unordered atomic memcpy: | 
| Hiroshi Inoue | f209649 | 2018-06-14 05:41:49 +0000 | [diff] [blame] | 1052 | //  If the load or store are atomic, then they must necessarily be unordered | 
| Anna Thomas | b2a212c | 2017-06-06 16:45:25 +0000 | [diff] [blame] | 1053 | //  by previous checks. | 
| Daniel Neilson | 3faabbb | 2017-06-16 14:43:59 +0000 | [diff] [blame] | 1054 | if (!SI->isAtomic() && !LI->isAtomic()) | 
| Daniel Neilson | fb99a49 | 2018-02-08 17:33:08 +0000 | [diff] [blame] | 1055 | NewCall = Builder.CreateMemCpy(StoreBasePtr, SI->getAlignment(), | 
|  | 1056 | LoadBasePtr, LI->getAlignment(), NumBytes); | 
| Daniel Neilson | 3faabbb | 2017-06-16 14:43:59 +0000 | [diff] [blame] | 1057 | else { | 
| Anna Thomas | b2a212c | 2017-06-06 16:45:25 +0000 | [diff] [blame] | 1058 | // We cannot allow unaligned ops for unordered load/store, so reject | 
|  | 1059 | // anything where the alignment isn't at least the element size. | 
| Daniel Neilson | fb99a49 | 2018-02-08 17:33:08 +0000 | [diff] [blame] | 1060 | unsigned Align = std::min(SI->getAlignment(), LI->getAlignment()); | 
| Anna Thomas | b2a212c | 2017-06-06 16:45:25 +0000 | [diff] [blame] | 1061 | if (Align < StoreSize) | 
|  | 1062 | return false; | 
|  | 1063 |  | 
|  | 1064 | // If the element.atomic memcpy is not lowered into explicit | 
|  | 1065 | // loads/stores later, then it will be lowered into an element-size | 
|  | 1066 | // specific lib call. If the lib call doesn't exist for our store size, then | 
|  | 1067 | // we shouldn't generate the memcpy. | 
|  | 1068 | if (StoreSize > TTI->getAtomicMemIntrinsicMaxElementSize()) | 
|  | 1069 | return false; | 
|  | 1070 |  | 
| Daniel Neilson | 6e4aa1e | 2017-11-10 19:38:12 +0000 | [diff] [blame] | 1071 | // Create the call. | 
|  | 1072 | // Note that unordered atomic loads/stores are *required* by the spec to | 
|  | 1073 | // have an alignment but non-atomic loads/stores may not. | 
| Daniel Neilson | 3faabbb | 2017-06-16 14:43:59 +0000 | [diff] [blame] | 1074 | NewCall = Builder.CreateElementUnorderedAtomicMemCpy( | 
| Daniel Neilson | 6e4aa1e | 2017-11-10 19:38:12 +0000 | [diff] [blame] | 1075 | StoreBasePtr, SI->getAlignment(), LoadBasePtr, LI->getAlignment(), | 
|  | 1076 | NumBytes, StoreSize); | 
| Anna Thomas | b2a212c | 2017-06-06 16:45:25 +0000 | [diff] [blame] | 1077 | } | 
| Devang Patel | 0daa07e | 2011-05-04 21:37:05 +0000 | [diff] [blame] | 1078 | NewCall->setDebugLoc(SI->getDebugLoc()); | 
| Andrew Trick | 328b223 | 2011-03-14 16:48:10 +0000 | [diff] [blame] | 1079 |  | 
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1080 | LLVM_DEBUG(dbgs() << "  Formed memcpy: " << *NewCall << "\n" | 
|  | 1081 | << "    from load ptr=" << *LoadEv << " at: " << *LI << "\n" | 
|  | 1082 | << "    from store ptr=" << *StoreEv << " at: " << *SI | 
|  | 1083 | << "\n"); | 
| Andrew Trick | 60ab3ef | 2011-06-28 05:04:16 +0000 | [diff] [blame] | 1084 |  | 
| Chad Rosier | 7f08d80 | 2015-10-13 20:59:16 +0000 | [diff] [blame] | 1085 | // Okay, the memcpy has been formed.  Zap the original store and anything that | 
| Chris Lattner | 85b6d81 | 2011-01-02 03:37:56 +0000 | [diff] [blame] | 1086 | // feeds into it. | 
| David Majnemer | 41ff4fd | 2016-06-20 16:07:38 +0000 | [diff] [blame] | 1087 | deleteDeadInstruction(SI); | 
| Chandler Carruth | 099f5cb0 | 2012-11-02 08:33:25 +0000 | [diff] [blame] | 1088 | ++NumMemCpy; | 
| Chris Lattner | 85b6d81 | 2011-01-02 03:37:56 +0000 | [diff] [blame] | 1089 | return true; | 
|  | 1090 | } | 
| Chandler Carruth | d9c6070 | 2015-08-13 00:10:03 +0000 | [diff] [blame] | 1091 |  | 
| Andrew Kaylor | 7cdf01e | 2016-08-11 18:28:33 +0000 | [diff] [blame] | 1092 | // When compiling for codesize we avoid idiom recognition for a multi-block loop | 
|  | 1093 | // unless it is a loop_memset idiom or a memset/memcpy idiom in a nested loop. | 
|  | 1094 | // | 
|  | 1095 | bool LoopIdiomRecognize::avoidLIRForMultiBlockLoop(bool IsMemset, | 
|  | 1096 | bool IsLoopMemset) { | 
|  | 1097 | if (ApplyCodeSizeHeuristics && CurLoop->getNumBlocks() > 1) { | 
|  | 1098 | if (!CurLoop->getParentLoop() && (!IsMemset || !IsLoopMemset)) { | 
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1099 | LLVM_DEBUG(dbgs() << "  " << CurLoop->getHeader()->getParent()->getName() | 
|  | 1100 | << " : LIR " << (IsMemset ? "Memset" : "Memcpy") | 
|  | 1101 | << " avoided: multi-block top-level loop\n"); | 
| Andrew Kaylor | 7cdf01e | 2016-08-11 18:28:33 +0000 | [diff] [blame] | 1102 | return true; | 
|  | 1103 | } | 
|  | 1104 | } | 
|  | 1105 |  | 
|  | 1106 | return false; | 
|  | 1107 | } | 
|  | 1108 |  | 
| Chandler Carruth | d9c6070 | 2015-08-13 00:10:03 +0000 | [diff] [blame] | 1109 | bool LoopIdiomRecognize::runOnNoncountableLoop() { | 
| Craig Topper | c9a6000 | 2018-12-26 21:59:48 +0000 | [diff] [blame] | 1110 | return recognizePopcount() || recognizeAndInsertFFS(); | 
| Chandler Carruth | d9c6070 | 2015-08-13 00:10:03 +0000 | [diff] [blame] | 1111 | } | 
| Chandler Carruth | 8219a50 | 2015-08-13 00:44:29 +0000 | [diff] [blame] | 1112 |  | 
|  | 1113 | /// Check if the given conditional branch is based on the comparison between | 
| Craig Topper | c9a6000 | 2018-12-26 21:59:48 +0000 | [diff] [blame] | 1114 | /// a variable and zero, and if the variable is non-zero or zero (JmpOnZero is | 
|  | 1115 | /// true), the control yields to the loop entry. If the branch matches the | 
|  | 1116 | /// behavior, the variable involved in the comparison is returned. This function | 
|  | 1117 | /// will be called to see if the precondition and postcondition of the loop are | 
|  | 1118 | /// in desirable form. | 
|  | 1119 | static Value *matchCondition(BranchInst *BI, BasicBlock *LoopEntry, | 
|  | 1120 | bool JmpOnZero = false) { | 
| Chandler Carruth | 8219a50 | 2015-08-13 00:44:29 +0000 | [diff] [blame] | 1121 | if (!BI || !BI->isConditional()) | 
|  | 1122 | return nullptr; | 
|  | 1123 |  | 
|  | 1124 | ICmpInst *Cond = dyn_cast<ICmpInst>(BI->getCondition()); | 
|  | 1125 | if (!Cond) | 
|  | 1126 | return nullptr; | 
|  | 1127 |  | 
|  | 1128 | ConstantInt *CmpZero = dyn_cast<ConstantInt>(Cond->getOperand(1)); | 
|  | 1129 | if (!CmpZero || !CmpZero->isZero()) | 
|  | 1130 | return nullptr; | 
|  | 1131 |  | 
| Craig Topper | c9a6000 | 2018-12-26 21:59:48 +0000 | [diff] [blame] | 1132 | BasicBlock *TrueSucc = BI->getSuccessor(0); | 
|  | 1133 | BasicBlock *FalseSucc = BI->getSuccessor(1); | 
|  | 1134 | if (JmpOnZero) | 
|  | 1135 | std::swap(TrueSucc, FalseSucc); | 
|  | 1136 |  | 
| Chandler Carruth | 8219a50 | 2015-08-13 00:44:29 +0000 | [diff] [blame] | 1137 | ICmpInst::Predicate Pred = Cond->getPredicate(); | 
| Craig Topper | c9a6000 | 2018-12-26 21:59:48 +0000 | [diff] [blame] | 1138 | if ((Pred == ICmpInst::ICMP_NE && TrueSucc == LoopEntry) || | 
|  | 1139 | (Pred == ICmpInst::ICMP_EQ && FalseSucc == LoopEntry)) | 
| Chandler Carruth | 8219a50 | 2015-08-13 00:44:29 +0000 | [diff] [blame] | 1140 | return Cond->getOperand(0); | 
|  | 1141 |  | 
|  | 1142 | return nullptr; | 
|  | 1143 | } | 
|  | 1144 |  | 
| Davide Italiano | 4bc9119 | 2017-05-23 22:32:56 +0000 | [diff] [blame] | 1145 | // Check if the recurrence variable `VarX` is in the right form to create | 
|  | 1146 | // the idiom. Returns the value coerced to a PHINode if so. | 
|  | 1147 | static PHINode *getRecurrenceVar(Value *VarX, Instruction *DefX, | 
|  | 1148 | BasicBlock *LoopEntry) { | 
|  | 1149 | auto *PhiX = dyn_cast<PHINode>(VarX); | 
|  | 1150 | if (PhiX && PhiX->getParent() == LoopEntry && | 
|  | 1151 | (PhiX->getOperand(0) == DefX || PhiX->getOperand(1) == DefX)) | 
|  | 1152 | return PhiX; | 
|  | 1153 | return nullptr; | 
|  | 1154 | } | 
|  | 1155 |  | 
| Chandler Carruth | 8219a50 | 2015-08-13 00:44:29 +0000 | [diff] [blame] | 1156 | /// Return true iff the idiom is detected in the loop. | 
|  | 1157 | /// | 
|  | 1158 | /// Additionally: | 
|  | 1159 | /// 1) \p CntInst is set to the instruction counting the population bit. | 
|  | 1160 | /// 2) \p CntPhi is set to the corresponding phi node. | 
|  | 1161 | /// 3) \p Var is set to the value whose population bits are being counted. | 
|  | 1162 | /// | 
|  | 1163 | /// The core idiom we are trying to detect is: | 
|  | 1164 | /// \code | 
|  | 1165 | ///    if (x0 != 0) | 
|  | 1166 | ///      goto loop-exit // the precondition of the loop | 
|  | 1167 | ///    cnt0 = init-val; | 
|  | 1168 | ///    do { | 
|  | 1169 | ///       x1 = phi (x0, x2); | 
|  | 1170 | ///       cnt1 = phi(cnt0, cnt2); | 
|  | 1171 | /// | 
|  | 1172 | ///       cnt2 = cnt1 + 1; | 
|  | 1173 | ///        ... | 
|  | 1174 | ///       x2 = x1 & (x1 - 1); | 
|  | 1175 | ///        ... | 
|  | 1176 | ///    } while(x != 0); | 
|  | 1177 | /// | 
|  | 1178 | /// loop-exit: | 
|  | 1179 | /// \endcode | 
|  | 1180 | static bool detectPopcountIdiom(Loop *CurLoop, BasicBlock *PreCondBB, | 
|  | 1181 | Instruction *&CntInst, PHINode *&CntPhi, | 
|  | 1182 | Value *&Var) { | 
|  | 1183 | // step 1: Check to see if the look-back branch match this pattern: | 
|  | 1184 | //    "if (a!=0) goto loop-entry". | 
|  | 1185 | BasicBlock *LoopEntry; | 
|  | 1186 | Instruction *DefX2, *CountInst; | 
|  | 1187 | Value *VarX1, *VarX0; | 
|  | 1188 | PHINode *PhiX, *CountPhi; | 
|  | 1189 |  | 
|  | 1190 | DefX2 = CountInst = nullptr; | 
|  | 1191 | VarX1 = VarX0 = nullptr; | 
|  | 1192 | PhiX = CountPhi = nullptr; | 
|  | 1193 | LoopEntry = *(CurLoop->block_begin()); | 
|  | 1194 |  | 
|  | 1195 | // step 1: Check if the loop-back branch is in desirable form. | 
|  | 1196 | { | 
|  | 1197 | if (Value *T = matchCondition( | 
|  | 1198 | dyn_cast<BranchInst>(LoopEntry->getTerminator()), LoopEntry)) | 
|  | 1199 | DefX2 = dyn_cast<Instruction>(T); | 
|  | 1200 | else | 
|  | 1201 | return false; | 
|  | 1202 | } | 
|  | 1203 |  | 
|  | 1204 | // step 2: detect instructions corresponding to "x2 = x1 & (x1 - 1)" | 
|  | 1205 | { | 
|  | 1206 | if (!DefX2 || DefX2->getOpcode() != Instruction::And) | 
|  | 1207 | return false; | 
|  | 1208 |  | 
|  | 1209 | BinaryOperator *SubOneOp; | 
|  | 1210 |  | 
|  | 1211 | if ((SubOneOp = dyn_cast<BinaryOperator>(DefX2->getOperand(0)))) | 
|  | 1212 | VarX1 = DefX2->getOperand(1); | 
|  | 1213 | else { | 
|  | 1214 | VarX1 = DefX2->getOperand(0); | 
|  | 1215 | SubOneOp = dyn_cast<BinaryOperator>(DefX2->getOperand(1)); | 
|  | 1216 | } | 
| Craig Topper | 856fd68 | 2018-05-03 05:48:49 +0000 | [diff] [blame] | 1217 | if (!SubOneOp || SubOneOp->getOperand(0) != VarX1) | 
| Chandler Carruth | 8219a50 | 2015-08-13 00:44:29 +0000 | [diff] [blame] | 1218 | return false; | 
|  | 1219 |  | 
| Craig Topper | 8ef2abd | 2018-05-03 05:00:18 +0000 | [diff] [blame] | 1220 | ConstantInt *Dec = dyn_cast<ConstantInt>(SubOneOp->getOperand(1)); | 
| Chandler Carruth | 8219a50 | 2015-08-13 00:44:29 +0000 | [diff] [blame] | 1221 | if (!Dec || | 
| Craig Topper | 8ef2abd | 2018-05-03 05:00:18 +0000 | [diff] [blame] | 1222 | !((SubOneOp->getOpcode() == Instruction::Sub && Dec->isOne()) || | 
|  | 1223 | (SubOneOp->getOpcode() == Instruction::Add && | 
| Craig Topper | 79ab643 | 2017-07-06 18:39:47 +0000 | [diff] [blame] | 1224 | Dec->isMinusOne()))) { | 
| Chandler Carruth | 8219a50 | 2015-08-13 00:44:29 +0000 | [diff] [blame] | 1225 | return false; | 
|  | 1226 | } | 
|  | 1227 | } | 
|  | 1228 |  | 
|  | 1229 | // step 3: Check the recurrence of variable X | 
| Davide Italiano | 4bc9119 | 2017-05-23 22:32:56 +0000 | [diff] [blame] | 1230 | PhiX = getRecurrenceVar(VarX1, DefX2, LoopEntry); | 
|  | 1231 | if (!PhiX) | 
|  | 1232 | return false; | 
| Chandler Carruth | 8219a50 | 2015-08-13 00:44:29 +0000 | [diff] [blame] | 1233 |  | 
|  | 1234 | // step 4: Find the instruction which count the population: cnt2 = cnt1 + 1 | 
|  | 1235 | { | 
|  | 1236 | CountInst = nullptr; | 
| Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 1237 | for (BasicBlock::iterator Iter = LoopEntry->getFirstNonPHI()->getIterator(), | 
| Chandler Carruth | 8219a50 | 2015-08-13 00:44:29 +0000 | [diff] [blame] | 1238 | IterE = LoopEntry->end(); | 
|  | 1239 | Iter != IterE; Iter++) { | 
| Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 1240 | Instruction *Inst = &*Iter; | 
| Chandler Carruth | 8219a50 | 2015-08-13 00:44:29 +0000 | [diff] [blame] | 1241 | if (Inst->getOpcode() != Instruction::Add) | 
|  | 1242 | continue; | 
|  | 1243 |  | 
|  | 1244 | ConstantInt *Inc = dyn_cast<ConstantInt>(Inst->getOperand(1)); | 
|  | 1245 | if (!Inc || !Inc->isOne()) | 
|  | 1246 | continue; | 
|  | 1247 |  | 
| Davide Italiano | 7bf95b9 | 2017-05-23 23:51:54 +0000 | [diff] [blame] | 1248 | PHINode *Phi = getRecurrenceVar(Inst->getOperand(0), Inst, LoopEntry); | 
|  | 1249 | if (!Phi) | 
| Chandler Carruth | 8219a50 | 2015-08-13 00:44:29 +0000 | [diff] [blame] | 1250 | continue; | 
|  | 1251 |  | 
|  | 1252 | // Check if the result of the instruction is live of the loop. | 
|  | 1253 | bool LiveOutLoop = false; | 
|  | 1254 | for (User *U : Inst->users()) { | 
|  | 1255 | if ((cast<Instruction>(U))->getParent() != LoopEntry) { | 
|  | 1256 | LiveOutLoop = true; | 
|  | 1257 | break; | 
|  | 1258 | } | 
|  | 1259 | } | 
|  | 1260 |  | 
|  | 1261 | if (LiveOutLoop) { | 
|  | 1262 | CountInst = Inst; | 
|  | 1263 | CountPhi = Phi; | 
|  | 1264 | break; | 
|  | 1265 | } | 
|  | 1266 | } | 
|  | 1267 |  | 
|  | 1268 | if (!CountInst) | 
|  | 1269 | return false; | 
|  | 1270 | } | 
|  | 1271 |  | 
|  | 1272 | // step 5: check if the precondition is in this form: | 
|  | 1273 | //   "if (x != 0) goto loop-head ; else goto somewhere-we-don't-care;" | 
|  | 1274 | { | 
|  | 1275 | auto *PreCondBr = dyn_cast<BranchInst>(PreCondBB->getTerminator()); | 
|  | 1276 | Value *T = matchCondition(PreCondBr, CurLoop->getLoopPreheader()); | 
|  | 1277 | if (T != PhiX->getOperand(0) && T != PhiX->getOperand(1)) | 
|  | 1278 | return false; | 
|  | 1279 |  | 
|  | 1280 | CntInst = CountInst; | 
|  | 1281 | CntPhi = CountPhi; | 
|  | 1282 | Var = T; | 
|  | 1283 | } | 
|  | 1284 |  | 
|  | 1285 | return true; | 
|  | 1286 | } | 
|  | 1287 |  | 
| Evgeny Stupachenko | 2fecd38 | 2017-05-15 19:08:56 +0000 | [diff] [blame] | 1288 | /// Return true if the idiom is detected in the loop. | 
|  | 1289 | /// | 
|  | 1290 | /// Additionally: | 
|  | 1291 | /// 1) \p CntInst is set to the instruction Counting Leading Zeros (CTLZ) | 
|  | 1292 | ///       or nullptr if there is no such. | 
|  | 1293 | /// 2) \p CntPhi is set to the corresponding phi node | 
|  | 1294 | ///       or nullptr if there is no such. | 
|  | 1295 | /// 3) \p Var is set to the value whose CTLZ could be used. | 
|  | 1296 | /// 4) \p DefX is set to the instruction calculating Loop exit condition. | 
|  | 1297 | /// | 
|  | 1298 | /// The core idiom we are trying to detect is: | 
|  | 1299 | /// \code | 
|  | 1300 | ///    if (x0 == 0) | 
|  | 1301 | ///      goto loop-exit // the precondition of the loop | 
|  | 1302 | ///    cnt0 = init-val; | 
|  | 1303 | ///    do { | 
|  | 1304 | ///       x = phi (x0, x.next);   //PhiX | 
|  | 1305 | ///       cnt = phi(cnt0, cnt.next); | 
|  | 1306 | /// | 
|  | 1307 | ///       cnt.next = cnt + 1; | 
|  | 1308 | ///        ... | 
|  | 1309 | ///       x.next = x >> 1;   // DefX | 
|  | 1310 | ///        ... | 
|  | 1311 | ///    } while(x.next != 0); | 
|  | 1312 | /// | 
|  | 1313 | /// loop-exit: | 
|  | 1314 | /// \endcode | 
| Craig Topper | c9a6000 | 2018-12-26 21:59:48 +0000 | [diff] [blame] | 1315 | static bool detectShiftUntilZeroIdiom(Loop *CurLoop, const DataLayout &DL, | 
|  | 1316 | Intrinsic::ID &IntrinID, Value *&InitX, | 
|  | 1317 | Instruction *&CntInst, PHINode *&CntPhi, | 
|  | 1318 | Instruction *&DefX) { | 
| Evgeny Stupachenko | 2fecd38 | 2017-05-15 19:08:56 +0000 | [diff] [blame] | 1319 | BasicBlock *LoopEntry; | 
|  | 1320 | Value *VarX = nullptr; | 
|  | 1321 |  | 
|  | 1322 | DefX = nullptr; | 
| Evgeny Stupachenko | 2fecd38 | 2017-05-15 19:08:56 +0000 | [diff] [blame] | 1323 | CntInst = nullptr; | 
|  | 1324 | CntPhi = nullptr; | 
|  | 1325 | LoopEntry = *(CurLoop->block_begin()); | 
|  | 1326 |  | 
|  | 1327 | // step 1: Check if the loop-back branch is in desirable form. | 
|  | 1328 | if (Value *T = matchCondition( | 
|  | 1329 | dyn_cast<BranchInst>(LoopEntry->getTerminator()), LoopEntry)) | 
|  | 1330 | DefX = dyn_cast<Instruction>(T); | 
|  | 1331 | else | 
|  | 1332 | return false; | 
|  | 1333 |  | 
| Craig Topper | c9a6000 | 2018-12-26 21:59:48 +0000 | [diff] [blame] | 1334 | // step 2: detect instructions corresponding to "x.next = x >> 1 or x << 1" | 
|  | 1335 | if (!DefX || !DefX->isShift()) | 
| Evgeny Stupachenko | 2fecd38 | 2017-05-15 19:08:56 +0000 | [diff] [blame] | 1336 | return false; | 
| Craig Topper | c9a6000 | 2018-12-26 21:59:48 +0000 | [diff] [blame] | 1337 | IntrinID = DefX->getOpcode() == Instruction::Shl ? Intrinsic::cttz : | 
|  | 1338 | Intrinsic::ctlz; | 
| Evgeny Stupachenko | d699de2 | 2017-11-03 18:50:03 +0000 | [diff] [blame] | 1339 | ConstantInt *Shft = dyn_cast<ConstantInt>(DefX->getOperand(1)); | 
|  | 1340 | if (!Shft || !Shft->isOne()) | 
|  | 1341 | return false; | 
| Evgeny Stupachenko | 2fecd38 | 2017-05-15 19:08:56 +0000 | [diff] [blame] | 1342 | VarX = DefX->getOperand(0); | 
|  | 1343 |  | 
|  | 1344 | // step 3: Check the recurrence of variable X | 
| Craig Topper | c9a6000 | 2018-12-26 21:59:48 +0000 | [diff] [blame] | 1345 | PHINode *PhiX = getRecurrenceVar(VarX, DefX, LoopEntry); | 
| Davide Italiano | 4bc9119 | 2017-05-23 22:32:56 +0000 | [diff] [blame] | 1346 | if (!PhiX) | 
| Evgeny Stupachenko | 2fecd38 | 2017-05-15 19:08:56 +0000 | [diff] [blame] | 1347 | return false; | 
|  | 1348 |  | 
| Craig Topper | c9a6000 | 2018-12-26 21:59:48 +0000 | [diff] [blame] | 1349 | InitX = PhiX->getIncomingValueForBlock(CurLoop->getLoopPreheader()); | 
|  | 1350 |  | 
|  | 1351 | // Make sure the initial value can't be negative otherwise the ashr in the | 
|  | 1352 | // loop might never reach zero which would make the loop infinite. | 
|  | 1353 | if (DefX->getOpcode() == Instruction::AShr && !isKnownNonNegative(InitX, DL)) | 
|  | 1354 | return false; | 
|  | 1355 |  | 
| Evgeny Stupachenko | 2fecd38 | 2017-05-15 19:08:56 +0000 | [diff] [blame] | 1356 | // step 4: Find the instruction which count the CTLZ: cnt.next = cnt + 1 | 
|  | 1357 | // TODO: We can skip the step. If loop trip count is known (CTLZ), | 
|  | 1358 | //       then all uses of "cnt.next" could be optimized to the trip count | 
|  | 1359 | //       plus "cnt0". Currently it is not optimized. | 
|  | 1360 | //       This step could be used to detect POPCNT instruction: | 
|  | 1361 | //       cnt.next = cnt + (x.next & 1) | 
|  | 1362 | for (BasicBlock::iterator Iter = LoopEntry->getFirstNonPHI()->getIterator(), | 
|  | 1363 | IterE = LoopEntry->end(); | 
|  | 1364 | Iter != IterE; Iter++) { | 
|  | 1365 | Instruction *Inst = &*Iter; | 
|  | 1366 | if (Inst->getOpcode() != Instruction::Add) | 
|  | 1367 | continue; | 
|  | 1368 |  | 
|  | 1369 | ConstantInt *Inc = dyn_cast<ConstantInt>(Inst->getOperand(1)); | 
|  | 1370 | if (!Inc || !Inc->isOne()) | 
|  | 1371 | continue; | 
|  | 1372 |  | 
| Davide Italiano | 7bf95b9 | 2017-05-23 23:51:54 +0000 | [diff] [blame] | 1373 | PHINode *Phi = getRecurrenceVar(Inst->getOperand(0), Inst, LoopEntry); | 
|  | 1374 | if (!Phi) | 
| Evgeny Stupachenko | 2fecd38 | 2017-05-15 19:08:56 +0000 | [diff] [blame] | 1375 | continue; | 
|  | 1376 |  | 
|  | 1377 | CntInst = Inst; | 
|  | 1378 | CntPhi = Phi; | 
|  | 1379 | break; | 
|  | 1380 | } | 
|  | 1381 | if (!CntInst) | 
|  | 1382 | return false; | 
|  | 1383 |  | 
|  | 1384 | return true; | 
|  | 1385 | } | 
|  | 1386 |  | 
| Craig Topper | c9a6000 | 2018-12-26 21:59:48 +0000 | [diff] [blame] | 1387 | /// Recognize CTLZ or CTTZ idiom in a non-countable loop and convert the loop | 
|  | 1388 | /// to countable (with CTLZ / CTTZ trip count). If CTLZ / CTTZ inserted as a new | 
|  | 1389 | /// trip count returns true; otherwise, returns false. | 
|  | 1390 | bool LoopIdiomRecognize::recognizeAndInsertFFS() { | 
| Evgeny Stupachenko | 2fecd38 | 2017-05-15 19:08:56 +0000 | [diff] [blame] | 1391 | // Give up if the loop has multiple blocks or multiple backedges. | 
|  | 1392 | if (CurLoop->getNumBackEdges() != 1 || CurLoop->getNumBlocks() != 1) | 
|  | 1393 | return false; | 
|  | 1394 |  | 
| Craig Topper | c9a6000 | 2018-12-26 21:59:48 +0000 | [diff] [blame] | 1395 | Intrinsic::ID IntrinID; | 
|  | 1396 | Value *InitX; | 
|  | 1397 | Instruction *DefX = nullptr; | 
|  | 1398 | PHINode *CntPhi = nullptr; | 
|  | 1399 | Instruction *CntInst = nullptr; | 
|  | 1400 | // Help decide if transformation is profitable. For ShiftUntilZero idiom, | 
|  | 1401 | // this is always 6. | 
|  | 1402 | size_t IdiomCanonicalSize = 6; | 
|  | 1403 |  | 
|  | 1404 | if (!detectShiftUntilZeroIdiom(CurLoop, *DL, IntrinID, InitX, | 
|  | 1405 | CntInst, CntPhi, DefX)) | 
| Evgeny Stupachenko | 2fecd38 | 2017-05-15 19:08:56 +0000 | [diff] [blame] | 1406 | return false; | 
|  | 1407 |  | 
|  | 1408 | bool IsCntPhiUsedOutsideLoop = false; | 
|  | 1409 | for (User *U : CntPhi->users()) | 
| Craig Topper | 8304231 | 2018-05-04 01:04:24 +0000 | [diff] [blame] | 1410 | if (!CurLoop->contains(cast<Instruction>(U))) { | 
| Evgeny Stupachenko | 2fecd38 | 2017-05-15 19:08:56 +0000 | [diff] [blame] | 1411 | IsCntPhiUsedOutsideLoop = true; | 
|  | 1412 | break; | 
|  | 1413 | } | 
|  | 1414 | bool IsCntInstUsedOutsideLoop = false; | 
|  | 1415 | for (User *U : CntInst->users()) | 
| Craig Topper | 8304231 | 2018-05-04 01:04:24 +0000 | [diff] [blame] | 1416 | if (!CurLoop->contains(cast<Instruction>(U))) { | 
| Evgeny Stupachenko | 2fecd38 | 2017-05-15 19:08:56 +0000 | [diff] [blame] | 1417 | IsCntInstUsedOutsideLoop = true; | 
|  | 1418 | break; | 
|  | 1419 | } | 
|  | 1420 | // If both CntInst and CntPhi are used outside the loop the profitability | 
|  | 1421 | // is questionable. | 
|  | 1422 | if (IsCntInstUsedOutsideLoop && IsCntPhiUsedOutsideLoop) | 
|  | 1423 | return false; | 
|  | 1424 |  | 
|  | 1425 | // For some CPUs result of CTLZ(X) intrinsic is undefined | 
|  | 1426 | // when X is 0. If we can not guarantee X != 0, we need to check this | 
|  | 1427 | // when expand. | 
|  | 1428 | bool ZeroCheck = false; | 
|  | 1429 | // It is safe to assume Preheader exist as it was checked in | 
|  | 1430 | // parent function RunOnLoop. | 
|  | 1431 | BasicBlock *PH = CurLoop->getLoopPreheader(); | 
| Craig Topper | 9a6c0bd | 2018-05-31 22:16:55 +0000 | [diff] [blame] | 1432 |  | 
| Craig Topper | ed6acde | 2018-07-11 22:35:28 +0000 | [diff] [blame] | 1433 | // If we are using the count instruction outside the loop, make sure we | 
|  | 1434 | // have a zero check as a precondition. Without the check the loop would run | 
|  | 1435 | // one iteration for before any check of the input value. This means 0 and 1 | 
|  | 1436 | // would have identical behavior in the original loop and thus | 
|  | 1437 | if (!IsCntPhiUsedOutsideLoop) { | 
|  | 1438 | auto *PreCondBB = PH->getSinglePredecessor(); | 
|  | 1439 | if (!PreCondBB) | 
|  | 1440 | return false; | 
|  | 1441 | auto *PreCondBI = dyn_cast<BranchInst>(PreCondBB->getTerminator()); | 
|  | 1442 | if (!PreCondBI) | 
|  | 1443 | return false; | 
|  | 1444 | if (matchCondition(PreCondBI, PH) != InitX) | 
|  | 1445 | return false; | 
|  | 1446 | ZeroCheck = true; | 
|  | 1447 | } | 
| Evgeny Stupachenko | 2fecd38 | 2017-05-15 19:08:56 +0000 | [diff] [blame] | 1448 |  | 
| Craig Topper | c9a6000 | 2018-12-26 21:59:48 +0000 | [diff] [blame] | 1449 | // Check if CTLZ / CTTZ intrinsic is profitable. Assume it is always | 
|  | 1450 | // profitable if we delete the loop. | 
|  | 1451 |  | 
|  | 1452 | // the loop has only 6 instructions: | 
| Evgeny Stupachenko | 2fecd38 | 2017-05-15 19:08:56 +0000 | [diff] [blame] | 1453 | //  %n.addr.0 = phi [ %n, %entry ], [ %shr, %while.cond ] | 
|  | 1454 | //  %i.0 = phi [ %i0, %entry ], [ %inc, %while.cond ] | 
|  | 1455 | //  %shr = ashr %n.addr.0, 1 | 
|  | 1456 | //  %tobool = icmp eq %shr, 0 | 
|  | 1457 | //  %inc = add nsw %i.0, 1 | 
|  | 1458 | //  br i1 %tobool | 
|  | 1459 |  | 
| Craig Topper | cafae62 | 2018-05-04 01:04:26 +0000 | [diff] [blame] | 1460 | const Value *Args[] = | 
| Craig Topper | ded8ee0 | 2018-05-04 17:39:08 +0000 | [diff] [blame] | 1461 | {InitX, ZeroCheck ? ConstantInt::getTrue(InitX->getContext()) | 
|  | 1462 | : ConstantInt::getFalse(InitX->getContext())}; | 
| Davide Italiano | 73929c4 | 2019-02-03 20:33:20 +0000 | [diff] [blame] | 1463 |  | 
|  | 1464 | // @llvm.dbg doesn't count as they have no semantic effect. | 
|  | 1465 | auto InstWithoutDebugIt = CurLoop->getHeader()->instructionsWithoutDebug(); | 
|  | 1466 | uint32_t HeaderSize = | 
|  | 1467 | std::distance(InstWithoutDebugIt.begin(), InstWithoutDebugIt.end()); | 
|  | 1468 |  | 
|  | 1469 | if (HeaderSize != IdiomCanonicalSize && | 
| Craig Topper | c9a6000 | 2018-12-26 21:59:48 +0000 | [diff] [blame] | 1470 | TTI->getIntrinsicCost(IntrinID, InitX->getType(), Args) > | 
| Davide Italiano | 73929c4 | 2019-02-03 20:33:20 +0000 | [diff] [blame] | 1471 | TargetTransformInfo::TCC_Basic) | 
| Evgeny Stupachenko | 2fecd38 | 2017-05-15 19:08:56 +0000 | [diff] [blame] | 1472 | return false; | 
|  | 1473 |  | 
| Craig Topper | c9a6000 | 2018-12-26 21:59:48 +0000 | [diff] [blame] | 1474 | transformLoopToCountable(IntrinID, PH, CntInst, CntPhi, InitX, DefX, | 
| Craig Topper | 2835278 | 2018-07-08 01:45:47 +0000 | [diff] [blame] | 1475 | DefX->getDebugLoc(), ZeroCheck, | 
|  | 1476 | IsCntPhiUsedOutsideLoop); | 
| Evgeny Stupachenko | 2fecd38 | 2017-05-15 19:08:56 +0000 | [diff] [blame] | 1477 | return true; | 
|  | 1478 | } | 
|  | 1479 |  | 
| Chandler Carruth | 8219a50 | 2015-08-13 00:44:29 +0000 | [diff] [blame] | 1480 | /// Recognizes a population count idiom in a non-countable loop. | 
|  | 1481 | /// | 
|  | 1482 | /// If detected, transforms the relevant code to issue the popcount intrinsic | 
|  | 1483 | /// function call, and returns true; otherwise, returns false. | 
|  | 1484 | bool LoopIdiomRecognize::recognizePopcount() { | 
| Chandler Carruth | 8219a50 | 2015-08-13 00:44:29 +0000 | [diff] [blame] | 1485 | if (TTI->getPopcntSupport(32) != TargetTransformInfo::PSK_FastHardware) | 
|  | 1486 | return false; | 
|  | 1487 |  | 
|  | 1488 | // Counting population are usually conducted by few arithmetic instructions. | 
| Nick Lewycky | 06b0ea2 | 2015-08-18 22:41:58 +0000 | [diff] [blame] | 1489 | // Such instructions can be easily "absorbed" by vacant slots in a | 
| Chandler Carruth | 8219a50 | 2015-08-13 00:44:29 +0000 | [diff] [blame] | 1490 | // non-compact loop. Therefore, recognizing popcount idiom only makes sense | 
|  | 1491 | // in a compact loop. | 
|  | 1492 |  | 
| Renato Golin | 655348f | 2015-08-13 11:25:38 +0000 | [diff] [blame] | 1493 | // Give up if the loop has multiple blocks or multiple backedges. | 
|  | 1494 | if (CurLoop->getNumBackEdges() != 1 || CurLoop->getNumBlocks() != 1) | 
| Chandler Carruth | 8219a50 | 2015-08-13 00:44:29 +0000 | [diff] [blame] | 1495 | return false; | 
|  | 1496 |  | 
| Renato Golin | 655348f | 2015-08-13 11:25:38 +0000 | [diff] [blame] | 1497 | BasicBlock *LoopBody = *(CurLoop->block_begin()); | 
|  | 1498 | if (LoopBody->size() >= 20) { | 
|  | 1499 | // The loop is too big, bail out. | 
| Chandler Carruth | 8219a50 | 2015-08-13 00:44:29 +0000 | [diff] [blame] | 1500 | return false; | 
| Renato Golin | 655348f | 2015-08-13 11:25:38 +0000 | [diff] [blame] | 1501 | } | 
| Chandler Carruth | 8219a50 | 2015-08-13 00:44:29 +0000 | [diff] [blame] | 1502 |  | 
|  | 1503 | // It should have a preheader containing nothing but an unconditional branch. | 
| Renato Golin | 655348f | 2015-08-13 11:25:38 +0000 | [diff] [blame] | 1504 | BasicBlock *PH = CurLoop->getLoopPreheader(); | 
| Davide Italiano | c0169fa | 2016-10-07 18:39:43 +0000 | [diff] [blame] | 1505 | if (!PH || &PH->front() != PH->getTerminator()) | 
| Renato Golin | 655348f | 2015-08-13 11:25:38 +0000 | [diff] [blame] | 1506 | return false; | 
|  | 1507 | auto *EntryBI = dyn_cast<BranchInst>(PH->getTerminator()); | 
| Chandler Carruth | 8219a50 | 2015-08-13 00:44:29 +0000 | [diff] [blame] | 1508 | if (!EntryBI || EntryBI->isConditional()) | 
|  | 1509 | return false; | 
|  | 1510 |  | 
| Hiroshi Inoue | f209649 | 2018-06-14 05:41:49 +0000 | [diff] [blame] | 1511 | // It should have a precondition block where the generated popcount intrinsic | 
| Chandler Carruth | 8219a50 | 2015-08-13 00:44:29 +0000 | [diff] [blame] | 1512 | // function can be inserted. | 
| Renato Golin | 655348f | 2015-08-13 11:25:38 +0000 | [diff] [blame] | 1513 | auto *PreCondBB = PH->getSinglePredecessor(); | 
| Chandler Carruth | 8219a50 | 2015-08-13 00:44:29 +0000 | [diff] [blame] | 1514 | if (!PreCondBB) | 
|  | 1515 | return false; | 
|  | 1516 | auto *PreCondBI = dyn_cast<BranchInst>(PreCondBB->getTerminator()); | 
|  | 1517 | if (!PreCondBI || PreCondBI->isUnconditional()) | 
|  | 1518 | return false; | 
|  | 1519 |  | 
|  | 1520 | Instruction *CntInst; | 
|  | 1521 | PHINode *CntPhi; | 
|  | 1522 | Value *Val; | 
|  | 1523 | if (!detectPopcountIdiom(CurLoop, PreCondBB, CntInst, CntPhi, Val)) | 
|  | 1524 | return false; | 
|  | 1525 |  | 
|  | 1526 | transformLoopToPopcount(PreCondBB, CntInst, CntPhi, Val); | 
|  | 1527 | return true; | 
|  | 1528 | } | 
|  | 1529 |  | 
|  | 1530 | static CallInst *createPopcntIntrinsic(IRBuilder<> &IRBuilder, Value *Val, | 
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 1531 | const DebugLoc &DL) { | 
| Chandler Carruth | 8219a50 | 2015-08-13 00:44:29 +0000 | [diff] [blame] | 1532 | Value *Ops[] = {Val}; | 
|  | 1533 | Type *Tys[] = {Val->getType()}; | 
|  | 1534 |  | 
|  | 1535 | Module *M = IRBuilder.GetInsertBlock()->getParent()->getParent(); | 
| James Y Knight | 7976eb5 | 2019-02-01 20:43:25 +0000 | [diff] [blame] | 1536 | Function *Func = Intrinsic::getDeclaration(M, Intrinsic::ctpop, Tys); | 
| Chandler Carruth | 8219a50 | 2015-08-13 00:44:29 +0000 | [diff] [blame] | 1537 | CallInst *CI = IRBuilder.CreateCall(Func, Ops); | 
|  | 1538 | CI->setDebugLoc(DL); | 
|  | 1539 |  | 
|  | 1540 | return CI; | 
|  | 1541 | } | 
|  | 1542 |  | 
| Craig Topper | c9a6000 | 2018-12-26 21:59:48 +0000 | [diff] [blame] | 1543 | static CallInst *createFFSIntrinsic(IRBuilder<> &IRBuilder, Value *Val, | 
|  | 1544 | const DebugLoc &DL, bool ZeroCheck, | 
|  | 1545 | Intrinsic::ID IID) { | 
| Evgeny Stupachenko | 2fecd38 | 2017-05-15 19:08:56 +0000 | [diff] [blame] | 1546 | Value *Ops[] = {Val, ZeroCheck ? IRBuilder.getTrue() : IRBuilder.getFalse()}; | 
|  | 1547 | Type *Tys[] = {Val->getType()}; | 
|  | 1548 |  | 
|  | 1549 | Module *M = IRBuilder.GetInsertBlock()->getParent()->getParent(); | 
| James Y Knight | 7976eb5 | 2019-02-01 20:43:25 +0000 | [diff] [blame] | 1550 | Function *Func = Intrinsic::getDeclaration(M, IID, Tys); | 
| Evgeny Stupachenko | 2fecd38 | 2017-05-15 19:08:56 +0000 | [diff] [blame] | 1551 | CallInst *CI = IRBuilder.CreateCall(Func, Ops); | 
|  | 1552 | CI->setDebugLoc(DL); | 
|  | 1553 |  | 
|  | 1554 | return CI; | 
|  | 1555 | } | 
|  | 1556 |  | 
| Craig Topper | c9a6000 | 2018-12-26 21:59:48 +0000 | [diff] [blame] | 1557 | /// Transform the following loop (Using CTLZ, CTTZ is similar): | 
| Evgeny Stupachenko | 2fecd38 | 2017-05-15 19:08:56 +0000 | [diff] [blame] | 1558 | /// loop: | 
|  | 1559 | ///   CntPhi = PHI [Cnt0, CntInst] | 
|  | 1560 | ///   PhiX = PHI [InitX, DefX] | 
|  | 1561 | ///   CntInst = CntPhi + 1 | 
|  | 1562 | ///   DefX = PhiX >> 1 | 
| Eugene Zelenko | dd40f5e | 2017-10-16 21:34:24 +0000 | [diff] [blame] | 1563 | ///   LOOP_BODY | 
| Evgeny Stupachenko | 2fecd38 | 2017-05-15 19:08:56 +0000 | [diff] [blame] | 1564 | ///   Br: loop if (DefX != 0) | 
|  | 1565 | /// Use(CntPhi) or Use(CntInst) | 
|  | 1566 | /// | 
|  | 1567 | /// Into: | 
|  | 1568 | /// If CntPhi used outside the loop: | 
|  | 1569 | ///   CountPrev = BitWidth(InitX) - CTLZ(InitX >> 1) | 
|  | 1570 | ///   Count = CountPrev + 1 | 
|  | 1571 | /// else | 
|  | 1572 | ///   Count = BitWidth(InitX) - CTLZ(InitX) | 
|  | 1573 | /// loop: | 
|  | 1574 | ///   CntPhi = PHI [Cnt0, CntInst] | 
|  | 1575 | ///   PhiX = PHI [InitX, DefX] | 
|  | 1576 | ///   PhiCount = PHI [Count, Dec] | 
|  | 1577 | ///   CntInst = CntPhi + 1 | 
|  | 1578 | ///   DefX = PhiX >> 1 | 
|  | 1579 | ///   Dec = PhiCount - 1 | 
|  | 1580 | ///   LOOP_BODY | 
|  | 1581 | ///   Br: loop if (Dec != 0) | 
|  | 1582 | /// Use(CountPrev + Cnt0) // Use(CntPhi) | 
|  | 1583 | /// or | 
|  | 1584 | /// Use(Count + Cnt0) // Use(CntInst) | 
|  | 1585 | /// | 
|  | 1586 | /// If LOOP_BODY is empty the loop will be deleted. | 
|  | 1587 | /// If CntInst and DefX are not used in LOOP_BODY they will be removed. | 
|  | 1588 | void LoopIdiomRecognize::transformLoopToCountable( | 
| Craig Topper | c9a6000 | 2018-12-26 21:59:48 +0000 | [diff] [blame] | 1589 | Intrinsic::ID IntrinID, BasicBlock *Preheader, Instruction *CntInst, | 
|  | 1590 | PHINode *CntPhi, Value *InitX, Instruction *DefX, const DebugLoc &DL, | 
|  | 1591 | bool ZeroCheck, bool IsCntPhiUsedOutsideLoop) { | 
| Craig Topper | 9510f70 | 2018-05-04 01:04:28 +0000 | [diff] [blame] | 1592 | BranchInst *PreheaderBr = cast<BranchInst>(Preheader->getTerminator()); | 
| Evgeny Stupachenko | 2fecd38 | 2017-05-15 19:08:56 +0000 | [diff] [blame] | 1593 |  | 
| Craig Topper | c9a6000 | 2018-12-26 21:59:48 +0000 | [diff] [blame] | 1594 | // Step 1: Insert the CTLZ/CTTZ instruction at the end of the preheader block | 
|  | 1595 | IRBuilder<> Builder(PreheaderBr); | 
|  | 1596 | Builder.SetCurrentDebugLocation(DL); | 
|  | 1597 | Value *FFS, *Count, *CountPrev, *NewCount, *InitXNext; | 
|  | 1598 |  | 
| Evgeny Stupachenko | 2fecd38 | 2017-05-15 19:08:56 +0000 | [diff] [blame] | 1599 | //   Count = BitWidth - CTLZ(InitX); | 
|  | 1600 | // If there are uses of CntPhi create: | 
|  | 1601 | //   CountPrev = BitWidth - CTLZ(InitX >> 1); | 
| Craig Topper | 2835278 | 2018-07-08 01:45:47 +0000 | [diff] [blame] | 1602 | if (IsCntPhiUsedOutsideLoop) { | 
|  | 1603 | if (DefX->getOpcode() == Instruction::AShr) | 
|  | 1604 | InitXNext = | 
|  | 1605 | Builder.CreateAShr(InitX, ConstantInt::get(InitX->getType(), 1)); | 
|  | 1606 | else if (DefX->getOpcode() == Instruction::LShr) | 
|  | 1607 | InitXNext = | 
|  | 1608 | Builder.CreateLShr(InitX, ConstantInt::get(InitX->getType(), 1)); | 
| Craig Topper | c9a6000 | 2018-12-26 21:59:48 +0000 | [diff] [blame] | 1609 | else if (DefX->getOpcode() == Instruction::Shl) // cttz | 
|  | 1610 | InitXNext = | 
|  | 1611 | Builder.CreateShl(InitX, ConstantInt::get(InitX->getType(), 1)); | 
| Craig Topper | 2835278 | 2018-07-08 01:45:47 +0000 | [diff] [blame] | 1612 | else | 
| Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 1613 | llvm_unreachable("Unexpected opcode!"); | 
| Craig Topper | 2835278 | 2018-07-08 01:45:47 +0000 | [diff] [blame] | 1614 | } else | 
| Evgeny Stupachenko | 2fecd38 | 2017-05-15 19:08:56 +0000 | [diff] [blame] | 1615 | InitXNext = InitX; | 
| Craig Topper | c9a6000 | 2018-12-26 21:59:48 +0000 | [diff] [blame] | 1616 | FFS = createFFSIntrinsic(Builder, InitXNext, DL, ZeroCheck, IntrinID); | 
| Evgeny Stupachenko | 2fecd38 | 2017-05-15 19:08:56 +0000 | [diff] [blame] | 1617 | Count = Builder.CreateSub( | 
| Craig Topper | c9a6000 | 2018-12-26 21:59:48 +0000 | [diff] [blame] | 1618 | ConstantInt::get(FFS->getType(), | 
|  | 1619 | FFS->getType()->getIntegerBitWidth()), | 
|  | 1620 | FFS); | 
| Evgeny Stupachenko | 2fecd38 | 2017-05-15 19:08:56 +0000 | [diff] [blame] | 1621 | if (IsCntPhiUsedOutsideLoop) { | 
|  | 1622 | CountPrev = Count; | 
|  | 1623 | Count = Builder.CreateAdd( | 
|  | 1624 | CountPrev, | 
|  | 1625 | ConstantInt::get(CountPrev->getType(), 1)); | 
|  | 1626 | } | 
| Evgeny Stupachenko | 2fecd38 | 2017-05-15 19:08:56 +0000 | [diff] [blame] | 1627 |  | 
| Craig Topper | c9a6000 | 2018-12-26 21:59:48 +0000 | [diff] [blame] | 1628 | NewCount = Builder.CreateZExtOrTrunc( | 
|  | 1629 | IsCntPhiUsedOutsideLoop ? CountPrev : Count, | 
|  | 1630 | cast<IntegerType>(CntInst->getType())); | 
|  | 1631 |  | 
|  | 1632 | // If the counter's initial value is not zero, insert Add Inst. | 
| Evgeny Stupachenko | 2fecd38 | 2017-05-15 19:08:56 +0000 | [diff] [blame] | 1633 | Value *CntInitVal = CntPhi->getIncomingValueForBlock(Preheader); | 
|  | 1634 | ConstantInt *InitConst = dyn_cast<ConstantInt>(CntInitVal); | 
|  | 1635 | if (!InitConst || !InitConst->isZero()) | 
|  | 1636 | NewCount = Builder.CreateAdd(NewCount, CntInitVal); | 
|  | 1637 |  | 
|  | 1638 | // Step 2: Insert new IV and loop condition: | 
|  | 1639 | // loop: | 
|  | 1640 | //   ... | 
|  | 1641 | //   PhiCount = PHI [Count, Dec] | 
|  | 1642 | //   ... | 
|  | 1643 | //   Dec = PhiCount - 1 | 
|  | 1644 | //   ... | 
|  | 1645 | //   Br: loop if (Dec != 0) | 
|  | 1646 | BasicBlock *Body = *(CurLoop->block_begin()); | 
| Craig Topper | 9510f70 | 2018-05-04 01:04:28 +0000 | [diff] [blame] | 1647 | auto *LbBr = cast<BranchInst>(Body->getTerminator()); | 
| Evgeny Stupachenko | 2fecd38 | 2017-05-15 19:08:56 +0000 | [diff] [blame] | 1648 | ICmpInst *LbCond = cast<ICmpInst>(LbBr->getCondition()); | 
|  | 1649 | Type *Ty = Count->getType(); | 
|  | 1650 |  | 
|  | 1651 | PHINode *TcPhi = PHINode::Create(Ty, 2, "tcphi", &Body->front()); | 
|  | 1652 |  | 
|  | 1653 | Builder.SetInsertPoint(LbCond); | 
|  | 1654 | Instruction *TcDec = cast<Instruction>( | 
|  | 1655 | Builder.CreateSub(TcPhi, ConstantInt::get(Ty, 1), | 
|  | 1656 | "tcdec", false, true)); | 
|  | 1657 |  | 
|  | 1658 | TcPhi->addIncoming(Count, Preheader); | 
|  | 1659 | TcPhi->addIncoming(TcDec, Body); | 
|  | 1660 |  | 
|  | 1661 | CmpInst::Predicate Pred = | 
|  | 1662 | (LbBr->getSuccessor(0) == Body) ? CmpInst::ICMP_NE : CmpInst::ICMP_EQ; | 
|  | 1663 | LbCond->setPredicate(Pred); | 
|  | 1664 | LbCond->setOperand(0, TcDec); | 
|  | 1665 | LbCond->setOperand(1, ConstantInt::get(Ty, 0)); | 
|  | 1666 |  | 
|  | 1667 | // Step 3: All the references to the original counter outside | 
| Craig Topper | c9a6000 | 2018-12-26 21:59:48 +0000 | [diff] [blame] | 1668 | //  the loop are replaced with the NewCount | 
| Evgeny Stupachenko | 2fecd38 | 2017-05-15 19:08:56 +0000 | [diff] [blame] | 1669 | if (IsCntPhiUsedOutsideLoop) | 
|  | 1670 | CntPhi->replaceUsesOutsideBlock(NewCount, Body); | 
|  | 1671 | else | 
|  | 1672 | CntInst->replaceUsesOutsideBlock(NewCount, Body); | 
|  | 1673 |  | 
|  | 1674 | // step 4: Forget the "non-computable" trip-count SCEV associated with the | 
|  | 1675 | //   loop. The loop would otherwise not be deleted even if it becomes empty. | 
|  | 1676 | SE->forgetLoop(CurLoop); | 
|  | 1677 | } | 
|  | 1678 |  | 
| Chandler Carruth | 8219a50 | 2015-08-13 00:44:29 +0000 | [diff] [blame] | 1679 | void LoopIdiomRecognize::transformLoopToPopcount(BasicBlock *PreCondBB, | 
|  | 1680 | Instruction *CntInst, | 
|  | 1681 | PHINode *CntPhi, Value *Var) { | 
|  | 1682 | BasicBlock *PreHead = CurLoop->getLoopPreheader(); | 
| Craig Topper | 9510f70 | 2018-05-04 01:04:28 +0000 | [diff] [blame] | 1683 | auto *PreCondBr = cast<BranchInst>(PreCondBB->getTerminator()); | 
| Craig Topper | 2784786 | 2018-06-25 20:45:45 +0000 | [diff] [blame] | 1684 | const DebugLoc &DL = CntInst->getDebugLoc(); | 
| Chandler Carruth | 8219a50 | 2015-08-13 00:44:29 +0000 | [diff] [blame] | 1685 |  | 
|  | 1686 | // Assuming before transformation, the loop is following: | 
|  | 1687 | //  if (x) // the precondition | 
|  | 1688 | //     do { cnt++; x &= x - 1; } while(x); | 
|  | 1689 |  | 
|  | 1690 | // Step 1: Insert the ctpop instruction at the end of the precondition block | 
|  | 1691 | IRBuilder<> Builder(PreCondBr); | 
|  | 1692 | Value *PopCnt, *PopCntZext, *NewCount, *TripCnt; | 
|  | 1693 | { | 
|  | 1694 | PopCnt = createPopcntIntrinsic(Builder, Var, DL); | 
|  | 1695 | NewCount = PopCntZext = | 
|  | 1696 | Builder.CreateZExtOrTrunc(PopCnt, cast<IntegerType>(CntPhi->getType())); | 
|  | 1697 |  | 
|  | 1698 | if (NewCount != PopCnt) | 
|  | 1699 | (cast<Instruction>(NewCount))->setDebugLoc(DL); | 
|  | 1700 |  | 
|  | 1701 | // TripCnt is exactly the number of iterations the loop has | 
|  | 1702 | TripCnt = NewCount; | 
|  | 1703 |  | 
|  | 1704 | // If the population counter's initial value is not zero, insert Add Inst. | 
|  | 1705 | Value *CntInitVal = CntPhi->getIncomingValueForBlock(PreHead); | 
|  | 1706 | ConstantInt *InitConst = dyn_cast<ConstantInt>(CntInitVal); | 
|  | 1707 | if (!InitConst || !InitConst->isZero()) { | 
|  | 1708 | NewCount = Builder.CreateAdd(NewCount, CntInitVal); | 
|  | 1709 | (cast<Instruction>(NewCount))->setDebugLoc(DL); | 
|  | 1710 | } | 
|  | 1711 | } | 
|  | 1712 |  | 
| Nick Lewycky | 2c85254 | 2015-08-19 06:22:33 +0000 | [diff] [blame] | 1713 | // Step 2: Replace the precondition from "if (x == 0) goto loop-exit" to | 
| Nick Lewycky | 1098e49 | 2015-08-19 06:25:30 +0000 | [diff] [blame] | 1714 | //   "if (NewCount == 0) loop-exit". Without this change, the intrinsic | 
| Chandler Carruth | 8219a50 | 2015-08-13 00:44:29 +0000 | [diff] [blame] | 1715 | //   function would be partial dead code, and downstream passes will drag | 
|  | 1716 | //   it back from the precondition block to the preheader. | 
|  | 1717 | { | 
|  | 1718 | ICmpInst *PreCond = cast<ICmpInst>(PreCondBr->getCondition()); | 
|  | 1719 |  | 
|  | 1720 | Value *Opnd0 = PopCntZext; | 
|  | 1721 | Value *Opnd1 = ConstantInt::get(PopCntZext->getType(), 0); | 
|  | 1722 | if (PreCond->getOperand(0) != Var) | 
|  | 1723 | std::swap(Opnd0, Opnd1); | 
|  | 1724 |  | 
|  | 1725 | ICmpInst *NewPreCond = cast<ICmpInst>( | 
|  | 1726 | Builder.CreateICmp(PreCond->getPredicate(), Opnd0, Opnd1)); | 
|  | 1727 | PreCondBr->setCondition(NewPreCond); | 
|  | 1728 |  | 
|  | 1729 | RecursivelyDeleteTriviallyDeadInstructions(PreCond, TLI); | 
|  | 1730 | } | 
|  | 1731 |  | 
|  | 1732 | // Step 3: Note that the population count is exactly the trip count of the | 
| Hiroshi Inoue | c8e9245 | 2018-01-29 05:17:03 +0000 | [diff] [blame] | 1733 | // loop in question, which enable us to convert the loop from noncountable | 
| Chandler Carruth | 8219a50 | 2015-08-13 00:44:29 +0000 | [diff] [blame] | 1734 | // loop into a countable one. The benefit is twofold: | 
|  | 1735 | // | 
| Nick Lewycky | 2c85254 | 2015-08-19 06:22:33 +0000 | [diff] [blame] | 1736 | //  - If the loop only counts population, the entire loop becomes dead after | 
|  | 1737 | //    the transformation. It is a lot easier to prove a countable loop dead | 
|  | 1738 | //    than to prove a noncountable one. (In some C dialects, an infinite loop | 
| Chandler Carruth | 8219a50 | 2015-08-13 00:44:29 +0000 | [diff] [blame] | 1739 | //    isn't dead even if it computes nothing useful. In general, DCE needs | 
|  | 1740 | //    to prove a noncountable loop finite before safely delete it.) | 
|  | 1741 | // | 
|  | 1742 | //  - If the loop also performs something else, it remains alive. | 
|  | 1743 | //    Since it is transformed to countable form, it can be aggressively | 
|  | 1744 | //    optimized by some optimizations which are in general not applicable | 
|  | 1745 | //    to a noncountable loop. | 
|  | 1746 | // | 
|  | 1747 | // After this step, this loop (conceptually) would look like following: | 
|  | 1748 | //   newcnt = __builtin_ctpop(x); | 
|  | 1749 | //   t = newcnt; | 
|  | 1750 | //   if (x) | 
|  | 1751 | //     do { cnt++; x &= x-1; t--) } while (t > 0); | 
|  | 1752 | BasicBlock *Body = *(CurLoop->block_begin()); | 
|  | 1753 | { | 
| Craig Topper | 9510f70 | 2018-05-04 01:04:28 +0000 | [diff] [blame] | 1754 | auto *LbBr = cast<BranchInst>(Body->getTerminator()); | 
| Chandler Carruth | 8219a50 | 2015-08-13 00:44:29 +0000 | [diff] [blame] | 1755 | ICmpInst *LbCond = cast<ICmpInst>(LbBr->getCondition()); | 
|  | 1756 | Type *Ty = TripCnt->getType(); | 
|  | 1757 |  | 
| Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 1758 | PHINode *TcPhi = PHINode::Create(Ty, 2, "tcphi", &Body->front()); | 
| Chandler Carruth | 8219a50 | 2015-08-13 00:44:29 +0000 | [diff] [blame] | 1759 |  | 
|  | 1760 | Builder.SetInsertPoint(LbCond); | 
| Chandler Carruth | 8219a50 | 2015-08-13 00:44:29 +0000 | [diff] [blame] | 1761 | Instruction *TcDec = cast<Instruction>( | 
| Nick Lewycky | 1098e49 | 2015-08-19 06:25:30 +0000 | [diff] [blame] | 1762 | Builder.CreateSub(TcPhi, ConstantInt::get(Ty, 1), | 
|  | 1763 | "tcdec", false, true)); | 
| Chandler Carruth | 8219a50 | 2015-08-13 00:44:29 +0000 | [diff] [blame] | 1764 |  | 
|  | 1765 | TcPhi->addIncoming(TripCnt, PreHead); | 
|  | 1766 | TcPhi->addIncoming(TcDec, Body); | 
|  | 1767 |  | 
|  | 1768 | CmpInst::Predicate Pred = | 
|  | 1769 | (LbBr->getSuccessor(0) == Body) ? CmpInst::ICMP_UGT : CmpInst::ICMP_SLE; | 
|  | 1770 | LbCond->setPredicate(Pred); | 
|  | 1771 | LbCond->setOperand(0, TcDec); | 
| Nick Lewycky | 2c85254 | 2015-08-19 06:22:33 +0000 | [diff] [blame] | 1772 | LbCond->setOperand(1, ConstantInt::get(Ty, 0)); | 
| Chandler Carruth | 8219a50 | 2015-08-13 00:44:29 +0000 | [diff] [blame] | 1773 | } | 
|  | 1774 |  | 
|  | 1775 | // Step 4: All the references to the original population counter outside | 
|  | 1776 | //  the loop are replaced with the NewCount -- the value returned from | 
|  | 1777 | //  __builtin_ctpop(). | 
|  | 1778 | CntInst->replaceUsesOutsideBlock(NewCount, Body); | 
|  | 1779 |  | 
|  | 1780 | // step 5: Forget the "non-computable" trip-count SCEV associated with the | 
|  | 1781 | //   loop. The loop would otherwise not be deleted even if it becomes empty. | 
|  | 1782 | SE->forgetLoop(CurLoop); | 
|  | 1783 | } |