Chad Rosier | a306eeb | 2016-05-02 14:32:17 +0000 | [diff] [blame] | 1 | //===--------------------- InterleavedAccessPass.cpp ----------------------===// |
Hao Liu | 1c1e0c9 | 2015-06-26 02:10:27 +0000 | [diff] [blame] | 2 | // |
Chad Rosier | a306eeb | 2016-05-02 14:32:17 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Hao Liu | 1c1e0c9 | 2015-06-26 02:10:27 +0000 | [diff] [blame] | 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Interleaved Access pass, which identifies |
Chad Rosier | a306eeb | 2016-05-02 14:32:17 +0000 | [diff] [blame] | 11 | // interleaved memory accesses and transforms them into target specific |
| 12 | // intrinsics. |
Hao Liu | 1c1e0c9 | 2015-06-26 02:10:27 +0000 | [diff] [blame] | 13 | // |
| 14 | // An interleaved load reads data from memory into several vectors, with |
| 15 | // DE-interleaving the data on a factor. An interleaved store writes several |
| 16 | // vectors to memory with RE-interleaving the data on a factor. |
| 17 | // |
Chad Rosier | a306eeb | 2016-05-02 14:32:17 +0000 | [diff] [blame] | 18 | // As interleaved accesses are difficult to identified in CodeGen (mainly |
| 19 | // because the VECTOR_SHUFFLE DAG node is quite different from the shufflevector |
| 20 | // IR), we identify and transform them to intrinsics in this pass so the |
| 21 | // intrinsics can be easily matched into target specific instructions later in |
| 22 | // CodeGen. |
Hao Liu | 1c1e0c9 | 2015-06-26 02:10:27 +0000 | [diff] [blame] | 23 | // |
| 24 | // E.g. An interleaved load (Factor = 2): |
| 25 | // %wide.vec = load <8 x i32>, <8 x i32>* %ptr |
| 26 | // %v0 = shuffle <8 x i32> %wide.vec, <8 x i32> undef, <0, 2, 4, 6> |
| 27 | // %v1 = shuffle <8 x i32> %wide.vec, <8 x i32> undef, <1, 3, 5, 7> |
| 28 | // |
| 29 | // It could be transformed into a ld2 intrinsic in AArch64 backend or a vld2 |
| 30 | // intrinsic in ARM backend. |
| 31 | // |
| 32 | // E.g. An interleaved store (Factor = 3): |
| 33 | // %i.vec = shuffle <8 x i32> %v0, <8 x i32> %v1, |
| 34 | // <0, 4, 8, 1, 5, 9, 2, 6, 10, 3, 7, 11> |
| 35 | // store <12 x i32> %i.vec, <12 x i32>* %ptr |
| 36 | // |
| 37 | // It could be transformed into a st3 intrinsic in AArch64 backend or a vst3 |
| 38 | // intrinsic in ARM backend. |
| 39 | // |
| 40 | //===----------------------------------------------------------------------===// |
| 41 | |
| 42 | #include "llvm/CodeGen/Passes.h" |
| 43 | #include "llvm/IR/InstIterator.h" |
| 44 | #include "llvm/Support/Debug.h" |
| 45 | #include "llvm/Support/MathExtras.h" |
Hao Liu | b41c0b4 | 2015-06-26 04:38:21 +0000 | [diff] [blame] | 46 | #include "llvm/Support/raw_ostream.h" |
Hao Liu | 1c1e0c9 | 2015-06-26 02:10:27 +0000 | [diff] [blame] | 47 | #include "llvm/Target/TargetLowering.h" |
| 48 | #include "llvm/Target/TargetSubtargetInfo.h" |
| 49 | |
| 50 | using namespace llvm; |
| 51 | |
| 52 | #define DEBUG_TYPE "interleaved-access" |
| 53 | |
| 54 | static cl::opt<bool> LowerInterleavedAccesses( |
| 55 | "lower-interleaved-accesses", |
| 56 | cl::desc("Enable lowering interleaved accesses to intrinsics"), |
Silviu Baranga | 6d3f05c | 2015-09-01 11:12:35 +0000 | [diff] [blame] | 57 | cl::init(true), cl::Hidden); |
Hao Liu | 1c1e0c9 | 2015-06-26 02:10:27 +0000 | [diff] [blame] | 58 | |
| 59 | static unsigned MaxFactor; // The maximum supported interleave factor. |
| 60 | |
Hao Liu | 1c1e0c9 | 2015-06-26 02:10:27 +0000 | [diff] [blame] | 61 | namespace { |
| 62 | |
| 63 | class InterleavedAccess : public FunctionPass { |
| 64 | |
| 65 | public: |
| 66 | static char ID; |
| 67 | InterleavedAccess(const TargetMachine *TM = nullptr) |
| 68 | : FunctionPass(ID), TM(TM), TLI(nullptr) { |
| 69 | initializeInterleavedAccessPass(*PassRegistry::getPassRegistry()); |
| 70 | } |
| 71 | |
| 72 | const char *getPassName() const override { return "Interleaved Access Pass"; } |
| 73 | |
| 74 | bool runOnFunction(Function &F) override; |
| 75 | |
| 76 | private: |
| 77 | const TargetMachine *TM; |
| 78 | const TargetLowering *TLI; |
| 79 | |
| 80 | /// \brief Transform an interleaved load into target specific intrinsics. |
| 81 | bool lowerInterleavedLoad(LoadInst *LI, |
| 82 | SmallVector<Instruction *, 32> &DeadInsts); |
| 83 | |
| 84 | /// \brief Transform an interleaved store into target specific intrinsics. |
| 85 | bool lowerInterleavedStore(StoreInst *SI, |
| 86 | SmallVector<Instruction *, 32> &DeadInsts); |
| 87 | }; |
| 88 | } // end anonymous namespace. |
| 89 | |
| 90 | char InterleavedAccess::ID = 0; |
| 91 | INITIALIZE_TM_PASS(InterleavedAccess, "interleaved-access", |
| 92 | "Lower interleaved memory accesses to target specific intrinsics", |
| 93 | false, false) |
| 94 | |
| 95 | FunctionPass *llvm::createInterleavedAccessPass(const TargetMachine *TM) { |
| 96 | return new InterleavedAccess(TM); |
| 97 | } |
| 98 | |
| 99 | /// \brief Check if the mask is a DE-interleave mask of the given factor |
| 100 | /// \p Factor like: |
| 101 | /// <Index, Index+Factor, ..., Index+(NumElts-1)*Factor> |
| 102 | static bool isDeInterleaveMaskOfFactor(ArrayRef<int> Mask, unsigned Factor, |
| 103 | unsigned &Index) { |
| 104 | // Check all potential start indices from 0 to (Factor - 1). |
| 105 | for (Index = 0; Index < Factor; Index++) { |
| 106 | unsigned i = 0; |
| 107 | |
| 108 | // Check that elements are in ascending order by Factor. Ignore undef |
| 109 | // elements. |
| 110 | for (; i < Mask.size(); i++) |
| 111 | if (Mask[i] >= 0 && static_cast<unsigned>(Mask[i]) != Index + i * Factor) |
| 112 | break; |
| 113 | |
| 114 | if (i == Mask.size()) |
| 115 | return true; |
| 116 | } |
| 117 | |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | /// \brief Check if the mask is a DE-interleave mask for an interleaved load. |
| 122 | /// |
| 123 | /// E.g. DE-interleave masks (Factor = 2) could be: |
| 124 | /// <0, 2, 4, 6> (mask of index 0 to extract even elements) |
| 125 | /// <1, 3, 5, 7> (mask of index 1 to extract odd elements) |
| 126 | static bool isDeInterleaveMask(ArrayRef<int> Mask, unsigned &Factor, |
| 127 | unsigned &Index) { |
| 128 | if (Mask.size() < 2) |
| 129 | return false; |
| 130 | |
| 131 | // Check potential Factors. |
| 132 | for (Factor = 2; Factor <= MaxFactor; Factor++) |
| 133 | if (isDeInterleaveMaskOfFactor(Mask, Factor, Index)) |
| 134 | return true; |
| 135 | |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | /// \brief Check if the mask is RE-interleave mask for an interleaved store. |
| 140 | /// |
| 141 | /// I.e. <0, NumSubElts, ... , NumSubElts*(Factor - 1), 1, NumSubElts + 1, ...> |
| 142 | /// |
| 143 | /// E.g. The RE-interleave mask (Factor = 2) could be: |
| 144 | /// <0, 4, 1, 5, 2, 6, 3, 7> |
| 145 | static bool isReInterleaveMask(ArrayRef<int> Mask, unsigned &Factor) { |
| 146 | unsigned NumElts = Mask.size(); |
| 147 | if (NumElts < 4) |
| 148 | return false; |
| 149 | |
| 150 | // Check potential Factors. |
| 151 | for (Factor = 2; Factor <= MaxFactor; Factor++) { |
| 152 | if (NumElts % Factor) |
| 153 | continue; |
| 154 | |
| 155 | unsigned NumSubElts = NumElts / Factor; |
| 156 | if (!isPowerOf2_32(NumSubElts)) |
| 157 | continue; |
| 158 | |
| 159 | // Check whether each element matchs the RE-interleaved rule. Ignore undef |
| 160 | // elements. |
| 161 | unsigned i = 0; |
| 162 | for (; i < NumElts; i++) |
| 163 | if (Mask[i] >= 0 && |
| 164 | static_cast<unsigned>(Mask[i]) != |
| 165 | (i % Factor) * NumSubElts + i / Factor) |
| 166 | break; |
| 167 | |
| 168 | // Find a RE-interleaved mask of current factor. |
| 169 | if (i == NumElts) |
| 170 | return true; |
| 171 | } |
| 172 | |
| 173 | return false; |
| 174 | } |
| 175 | |
| 176 | bool InterleavedAccess::lowerInterleavedLoad( |
| 177 | LoadInst *LI, SmallVector<Instruction *, 32> &DeadInsts) { |
| 178 | if (!LI->isSimple()) |
| 179 | return false; |
| 180 | |
| 181 | SmallVector<ShuffleVectorInst *, 4> Shuffles; |
| 182 | |
| 183 | // Check if all users of this load are shufflevectors. |
| 184 | for (auto UI = LI->user_begin(), E = LI->user_end(); UI != E; UI++) { |
| 185 | ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(*UI); |
| 186 | if (!SVI || !isa<UndefValue>(SVI->getOperand(1))) |
| 187 | return false; |
| 188 | |
| 189 | Shuffles.push_back(SVI); |
| 190 | } |
| 191 | |
| 192 | if (Shuffles.empty()) |
| 193 | return false; |
| 194 | |
| 195 | unsigned Factor, Index; |
| 196 | |
| 197 | // Check if the first shufflevector is DE-interleave shuffle. |
| 198 | if (!isDeInterleaveMask(Shuffles[0]->getShuffleMask(), Factor, Index)) |
| 199 | return false; |
| 200 | |
| 201 | // Holds the corresponding index for each DE-interleave shuffle. |
| 202 | SmallVector<unsigned, 4> Indices; |
| 203 | Indices.push_back(Index); |
| 204 | |
| 205 | Type *VecTy = Shuffles[0]->getType(); |
| 206 | |
| 207 | // Check if other shufflevectors are also DE-interleaved of the same type |
| 208 | // and factor as the first shufflevector. |
| 209 | for (unsigned i = 1; i < Shuffles.size(); i++) { |
| 210 | if (Shuffles[i]->getType() != VecTy) |
| 211 | return false; |
| 212 | |
| 213 | if (!isDeInterleaveMaskOfFactor(Shuffles[i]->getShuffleMask(), Factor, |
| 214 | Index)) |
| 215 | return false; |
| 216 | |
| 217 | Indices.push_back(Index); |
| 218 | } |
| 219 | |
| 220 | DEBUG(dbgs() << "IA: Found an interleaved load: " << *LI << "\n"); |
| 221 | |
| 222 | // Try to create target specific intrinsics to replace the load and shuffles. |
| 223 | if (!TLI->lowerInterleavedLoad(LI, Shuffles, Indices, Factor)) |
| 224 | return false; |
| 225 | |
| 226 | for (auto SVI : Shuffles) |
| 227 | DeadInsts.push_back(SVI); |
| 228 | |
| 229 | DeadInsts.push_back(LI); |
| 230 | return true; |
| 231 | } |
| 232 | |
| 233 | bool InterleavedAccess::lowerInterleavedStore( |
| 234 | StoreInst *SI, SmallVector<Instruction *, 32> &DeadInsts) { |
| 235 | if (!SI->isSimple()) |
| 236 | return false; |
| 237 | |
| 238 | ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(SI->getValueOperand()); |
| 239 | if (!SVI || !SVI->hasOneUse()) |
| 240 | return false; |
| 241 | |
| 242 | // Check if the shufflevector is RE-interleave shuffle. |
| 243 | unsigned Factor; |
| 244 | if (!isReInterleaveMask(SVI->getShuffleMask(), Factor)) |
| 245 | return false; |
| 246 | |
| 247 | DEBUG(dbgs() << "IA: Found an interleaved store: " << *SI << "\n"); |
| 248 | |
| 249 | // Try to create target specific intrinsics to replace the store and shuffle. |
| 250 | if (!TLI->lowerInterleavedStore(SI, SVI, Factor)) |
| 251 | return false; |
| 252 | |
| 253 | // Already have a new target specific interleaved store. Erase the old store. |
| 254 | DeadInsts.push_back(SI); |
| 255 | DeadInsts.push_back(SVI); |
| 256 | return true; |
| 257 | } |
| 258 | |
| 259 | bool InterleavedAccess::runOnFunction(Function &F) { |
| 260 | if (!TM || !LowerInterleavedAccesses) |
| 261 | return false; |
| 262 | |
| 263 | DEBUG(dbgs() << "*** " << getPassName() << ": " << F.getName() << "\n"); |
| 264 | |
| 265 | TLI = TM->getSubtargetImpl(F)->getTargetLowering(); |
| 266 | MaxFactor = TLI->getMaxSupportedInterleaveFactor(); |
| 267 | |
| 268 | // Holds dead instructions that will be erased later. |
| 269 | SmallVector<Instruction *, 32> DeadInsts; |
| 270 | bool Changed = false; |
| 271 | |
Nico Rieck | 7819951 | 2015-08-06 19:10:45 +0000 | [diff] [blame] | 272 | for (auto &I : instructions(F)) { |
Hao Liu | 1c1e0c9 | 2015-06-26 02:10:27 +0000 | [diff] [blame] | 273 | if (LoadInst *LI = dyn_cast<LoadInst>(&I)) |
| 274 | Changed |= lowerInterleavedLoad(LI, DeadInsts); |
| 275 | |
| 276 | if (StoreInst *SI = dyn_cast<StoreInst>(&I)) |
| 277 | Changed |= lowerInterleavedStore(SI, DeadInsts); |
| 278 | } |
| 279 | |
| 280 | for (auto I : DeadInsts) |
| 281 | I->eraseFromParent(); |
| 282 | |
| 283 | return Changed; |
| 284 | } |