Eugene Zelenko | f193332 | 2017-09-22 23:46:57 +0000 | [diff] [blame] | 1 | //===- InterleavedAccessPass.cpp ------------------------------------------===// |
Hao Liu | 1c1e0c9 | 2015-06-26 02:10:27 +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 |
Hao Liu | 1c1e0c9 | 2015-06-26 02:10:27 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements the Interleaved Access pass, which identifies |
Chad Rosier | a306eeb | 2016-05-02 14:32:17 +0000 | [diff] [blame] | 10 | // interleaved memory accesses and transforms them into target specific |
| 11 | // intrinsics. |
Hao Liu | 1c1e0c9 | 2015-06-26 02:10:27 +0000 | [diff] [blame] | 12 | // |
| 13 | // An interleaved load reads data from memory into several vectors, with |
| 14 | // DE-interleaving the data on a factor. An interleaved store writes several |
| 15 | // vectors to memory with RE-interleaving the data on a factor. |
| 16 | // |
Chad Rosier | a306eeb | 2016-05-02 14:32:17 +0000 | [diff] [blame] | 17 | // As interleaved accesses are difficult to identified in CodeGen (mainly |
| 18 | // because the VECTOR_SHUFFLE DAG node is quite different from the shufflevector |
| 19 | // IR), we identify and transform them to intrinsics in this pass so the |
| 20 | // intrinsics can be easily matched into target specific instructions later in |
| 21 | // CodeGen. |
Hao Liu | 1c1e0c9 | 2015-06-26 02:10:27 +0000 | [diff] [blame] | 22 | // |
| 23 | // E.g. An interleaved load (Factor = 2): |
| 24 | // %wide.vec = load <8 x i32>, <8 x i32>* %ptr |
| 25 | // %v0 = shuffle <8 x i32> %wide.vec, <8 x i32> undef, <0, 2, 4, 6> |
| 26 | // %v1 = shuffle <8 x i32> %wide.vec, <8 x i32> undef, <1, 3, 5, 7> |
| 27 | // |
| 28 | // It could be transformed into a ld2 intrinsic in AArch64 backend or a vld2 |
| 29 | // intrinsic in ARM backend. |
| 30 | // |
David L Kreitzer | 01a057a | 2016-10-14 18:20:41 +0000 | [diff] [blame] | 31 | // In X86, this can be further optimized into a set of target |
| 32 | // specific loads followed by an optimized sequence of shuffles. |
| 33 | // |
Hao Liu | 1c1e0c9 | 2015-06-26 02:10:27 +0000 | [diff] [blame] | 34 | // E.g. An interleaved store (Factor = 3): |
| 35 | // %i.vec = shuffle <8 x i32> %v0, <8 x i32> %v1, |
| 36 | // <0, 4, 8, 1, 5, 9, 2, 6, 10, 3, 7, 11> |
| 37 | // store <12 x i32> %i.vec, <12 x i32>* %ptr |
| 38 | // |
| 39 | // It could be transformed into a st3 intrinsic in AArch64 backend or a vst3 |
| 40 | // intrinsic in ARM backend. |
| 41 | // |
David L Kreitzer | 01a057a | 2016-10-14 18:20:41 +0000 | [diff] [blame] | 42 | // Similarly, a set of interleaved stores can be transformed into an optimized |
| 43 | // sequence of shuffles followed by a set of target specific stores for X86. |
Eugene Zelenko | f193332 | 2017-09-22 23:46:57 +0000 | [diff] [blame] | 44 | // |
Hao Liu | 1c1e0c9 | 2015-06-26 02:10:27 +0000 | [diff] [blame] | 45 | //===----------------------------------------------------------------------===// |
| 46 | |
Eugene Zelenko | f193332 | 2017-09-22 23:46:57 +0000 | [diff] [blame] | 47 | #include "llvm/ADT/ArrayRef.h" |
| 48 | #include "llvm/ADT/DenseMap.h" |
| 49 | #include "llvm/ADT/SmallVector.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 50 | #include "llvm/CodeGen/TargetLowering.h" |
Francis Visoiu Mistrih | 8b61764 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 51 | #include "llvm/CodeGen/TargetPassConfig.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 52 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
Eugene Zelenko | f193332 | 2017-09-22 23:46:57 +0000 | [diff] [blame] | 53 | #include "llvm/IR/Constants.h" |
Matthew Simpson | 476c0af | 2016-05-19 21:39:00 +0000 | [diff] [blame] | 54 | #include "llvm/IR/Dominators.h" |
Eugene Zelenko | f193332 | 2017-09-22 23:46:57 +0000 | [diff] [blame] | 55 | #include "llvm/IR/Function.h" |
| 56 | #include "llvm/IR/IRBuilder.h" |
Hao Liu | 1c1e0c9 | 2015-06-26 02:10:27 +0000 | [diff] [blame] | 57 | #include "llvm/IR/InstIterator.h" |
Eugene Zelenko | f193332 | 2017-09-22 23:46:57 +0000 | [diff] [blame] | 58 | #include "llvm/IR/Instruction.h" |
| 59 | #include "llvm/IR/Instructions.h" |
| 60 | #include "llvm/IR/Type.h" |
| 61 | #include "llvm/Pass.h" |
| 62 | #include "llvm/Support/Casting.h" |
| 63 | #include "llvm/Support/CommandLine.h" |
Hao Liu | 1c1e0c9 | 2015-06-26 02:10:27 +0000 | [diff] [blame] | 64 | #include "llvm/Support/Debug.h" |
| 65 | #include "llvm/Support/MathExtras.h" |
Hao Liu | b41c0b4 | 2015-06-26 04:38:21 +0000 | [diff] [blame] | 66 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | f193332 | 2017-09-22 23:46:57 +0000 | [diff] [blame] | 67 | #include "llvm/Target/TargetMachine.h" |
Eugene Zelenko | f193332 | 2017-09-22 23:46:57 +0000 | [diff] [blame] | 68 | #include <cassert> |
| 69 | #include <utility> |
Hao Liu | 1c1e0c9 | 2015-06-26 02:10:27 +0000 | [diff] [blame] | 70 | |
| 71 | using namespace llvm; |
| 72 | |
| 73 | #define DEBUG_TYPE "interleaved-access" |
| 74 | |
| 75 | static cl::opt<bool> LowerInterleavedAccesses( |
| 76 | "lower-interleaved-accesses", |
| 77 | cl::desc("Enable lowering interleaved accesses to intrinsics"), |
Silviu Baranga | 6d3f05c | 2015-09-01 11:12:35 +0000 | [diff] [blame] | 78 | cl::init(true), cl::Hidden); |
Hao Liu | 1c1e0c9 | 2015-06-26 02:10:27 +0000 | [diff] [blame] | 79 | |
Hao Liu | 1c1e0c9 | 2015-06-26 02:10:27 +0000 | [diff] [blame] | 80 | namespace { |
| 81 | |
| 82 | class InterleavedAccess : public FunctionPass { |
Hao Liu | 1c1e0c9 | 2015-06-26 02:10:27 +0000 | [diff] [blame] | 83 | public: |
| 84 | static char ID; |
Eugene Zelenko | f193332 | 2017-09-22 23:46:57 +0000 | [diff] [blame] | 85 | |
| 86 | InterleavedAccess() : FunctionPass(ID) { |
Hao Liu | 1c1e0c9 | 2015-06-26 02:10:27 +0000 | [diff] [blame] | 87 | initializeInterleavedAccessPass(*PassRegistry::getPassRegistry()); |
| 88 | } |
| 89 | |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 90 | StringRef getPassName() const override { return "Interleaved Access Pass"; } |
Hao Liu | 1c1e0c9 | 2015-06-26 02:10:27 +0000 | [diff] [blame] | 91 | |
| 92 | bool runOnFunction(Function &F) override; |
| 93 | |
Matthew Simpson | 476c0af | 2016-05-19 21:39:00 +0000 | [diff] [blame] | 94 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 95 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 96 | AU.addPreserved<DominatorTreeWrapperPass>(); |
| 97 | } |
| 98 | |
Hao Liu | 1c1e0c9 | 2015-06-26 02:10:27 +0000 | [diff] [blame] | 99 | private: |
Eugene Zelenko | f193332 | 2017-09-22 23:46:57 +0000 | [diff] [blame] | 100 | DominatorTree *DT = nullptr; |
| 101 | const TargetLowering *TLI = nullptr; |
Hao Liu | 1c1e0c9 | 2015-06-26 02:10:27 +0000 | [diff] [blame] | 102 | |
Benjamin Kramer | 1e425c9 | 2016-10-18 18:59:58 +0000 | [diff] [blame] | 103 | /// The maximum supported interleave factor. |
| 104 | unsigned MaxFactor; |
| 105 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 106 | /// Transform an interleaved load into target specific intrinsics. |
Hao Liu | 1c1e0c9 | 2015-06-26 02:10:27 +0000 | [diff] [blame] | 107 | bool lowerInterleavedLoad(LoadInst *LI, |
| 108 | SmallVector<Instruction *, 32> &DeadInsts); |
| 109 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 110 | /// Transform an interleaved store into target specific intrinsics. |
Hao Liu | 1c1e0c9 | 2015-06-26 02:10:27 +0000 | [diff] [blame] | 111 | bool lowerInterleavedStore(StoreInst *SI, |
| 112 | SmallVector<Instruction *, 32> &DeadInsts); |
Matthew Simpson | 476c0af | 2016-05-19 21:39:00 +0000 | [diff] [blame] | 113 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 114 | /// Returns true if the uses of an interleaved load by the |
Matthew Simpson | 476c0af | 2016-05-19 21:39:00 +0000 | [diff] [blame] | 115 | /// extractelement instructions in \p Extracts can be replaced by uses of the |
| 116 | /// shufflevector instructions in \p Shuffles instead. If so, the necessary |
| 117 | /// replacements are also performed. |
| 118 | bool tryReplaceExtracts(ArrayRef<ExtractElementInst *> Extracts, |
| 119 | ArrayRef<ShuffleVectorInst *> Shuffles); |
Hao Liu | 1c1e0c9 | 2015-06-26 02:10:27 +0000 | [diff] [blame] | 120 | }; |
Eugene Zelenko | f193332 | 2017-09-22 23:46:57 +0000 | [diff] [blame] | 121 | |
Hao Liu | 1c1e0c9 | 2015-06-26 02:10:27 +0000 | [diff] [blame] | 122 | } // end anonymous namespace. |
| 123 | |
| 124 | char InterleavedAccess::ID = 0; |
Eugene Zelenko | f193332 | 2017-09-22 23:46:57 +0000 | [diff] [blame] | 125 | |
Matthias Braun | 1527baa | 2017-05-25 21:26:32 +0000 | [diff] [blame] | 126 | INITIALIZE_PASS_BEGIN(InterleavedAccess, DEBUG_TYPE, |
Matthew Simpson | 476c0af | 2016-05-19 21:39:00 +0000 | [diff] [blame] | 127 | "Lower interleaved memory accesses to target specific intrinsics", false, |
| 128 | false) |
| 129 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
Matthias Braun | 1527baa | 2017-05-25 21:26:32 +0000 | [diff] [blame] | 130 | INITIALIZE_PASS_END(InterleavedAccess, DEBUG_TYPE, |
Matthew Simpson | 476c0af | 2016-05-19 21:39:00 +0000 | [diff] [blame] | 131 | "Lower interleaved memory accesses to target specific intrinsics", false, |
| 132 | false) |
Hao Liu | 1c1e0c9 | 2015-06-26 02:10:27 +0000 | [diff] [blame] | 133 | |
Francis Visoiu Mistrih | 8b61764 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 134 | FunctionPass *llvm::createInterleavedAccessPass() { |
| 135 | return new InterleavedAccess(); |
Hao Liu | 1c1e0c9 | 2015-06-26 02:10:27 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 138 | /// Check if the mask is a DE-interleave mask of the given factor |
Hao Liu | 1c1e0c9 | 2015-06-26 02:10:27 +0000 | [diff] [blame] | 139 | /// \p Factor like: |
| 140 | /// <Index, Index+Factor, ..., Index+(NumElts-1)*Factor> |
| 141 | static bool isDeInterleaveMaskOfFactor(ArrayRef<int> Mask, unsigned Factor, |
| 142 | unsigned &Index) { |
| 143 | // Check all potential start indices from 0 to (Factor - 1). |
| 144 | for (Index = 0; Index < Factor; Index++) { |
| 145 | unsigned i = 0; |
| 146 | |
| 147 | // Check that elements are in ascending order by Factor. Ignore undef |
| 148 | // elements. |
| 149 | for (; i < Mask.size(); i++) |
| 150 | if (Mask[i] >= 0 && static_cast<unsigned>(Mask[i]) != Index + i * Factor) |
| 151 | break; |
| 152 | |
| 153 | if (i == Mask.size()) |
| 154 | return true; |
| 155 | } |
| 156 | |
| 157 | return false; |
| 158 | } |
| 159 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 160 | /// Check if the mask is a DE-interleave mask for an interleaved load. |
Hao Liu | 1c1e0c9 | 2015-06-26 02:10:27 +0000 | [diff] [blame] | 161 | /// |
| 162 | /// E.g. DE-interleave masks (Factor = 2) could be: |
| 163 | /// <0, 2, 4, 6> (mask of index 0 to extract even elements) |
| 164 | /// <1, 3, 5, 7> (mask of index 1 to extract odd elements) |
| 165 | static bool isDeInterleaveMask(ArrayRef<int> Mask, unsigned &Factor, |
Eli Friedman | 96f295e | 2019-03-28 20:44:50 +0000 | [diff] [blame] | 166 | unsigned &Index, unsigned MaxFactor, |
| 167 | unsigned NumLoadElements) { |
Hao Liu | 1c1e0c9 | 2015-06-26 02:10:27 +0000 | [diff] [blame] | 168 | if (Mask.size() < 2) |
| 169 | return false; |
| 170 | |
| 171 | // Check potential Factors. |
Eli Friedman | 96f295e | 2019-03-28 20:44:50 +0000 | [diff] [blame] | 172 | for (Factor = 2; Factor <= MaxFactor; Factor++) { |
| 173 | // Make sure we don't produce a load wider than the input load. |
| 174 | if (Mask.size() * Factor > NumLoadElements) |
| 175 | return false; |
Hao Liu | 1c1e0c9 | 2015-06-26 02:10:27 +0000 | [diff] [blame] | 176 | if (isDeInterleaveMaskOfFactor(Mask, Factor, Index)) |
| 177 | return true; |
Eli Friedman | 96f295e | 2019-03-28 20:44:50 +0000 | [diff] [blame] | 178 | } |
Hao Liu | 1c1e0c9 | 2015-06-26 02:10:27 +0000 | [diff] [blame] | 179 | |
| 180 | return false; |
| 181 | } |
| 182 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 183 | /// Check if the mask can be used in an interleaved store. |
Alina Sbirlea | 77c5eaa | 2016-12-13 19:32:36 +0000 | [diff] [blame] | 184 | // |
| 185 | /// It checks for a more general pattern than the RE-interleave mask. |
| 186 | /// I.e. <x, y, ... z, x+1, y+1, ...z+1, x+2, y+2, ...z+2, ...> |
| 187 | /// E.g. For a Factor of 2 (LaneLen=4): <4, 32, 5, 33, 6, 34, 7, 35> |
| 188 | /// E.g. For a Factor of 3 (LaneLen=4): <4, 32, 16, 5, 33, 17, 6, 34, 18, 7, 35, 19> |
| 189 | /// E.g. For a Factor of 4 (LaneLen=2): <8, 2, 12, 4, 9, 3, 13, 5> |
Hao Liu | 1c1e0c9 | 2015-06-26 02:10:27 +0000 | [diff] [blame] | 190 | /// |
Alina Sbirlea | 77c5eaa | 2016-12-13 19:32:36 +0000 | [diff] [blame] | 191 | /// The particular case of an RE-interleave mask is: |
| 192 | /// I.e. <0, LaneLen, ... , LaneLen*(Factor - 1), 1, LaneLen + 1, ...> |
| 193 | /// E.g. For a Factor of 2 (LaneLen=4): <0, 4, 1, 5, 2, 6, 3, 7> |
Benjamin Kramer | 1e425c9 | 2016-10-18 18:59:58 +0000 | [diff] [blame] | 194 | static bool isReInterleaveMask(ArrayRef<int> Mask, unsigned &Factor, |
Matthias Braun | 01fa962 | 2017-01-31 18:37:53 +0000 | [diff] [blame] | 195 | unsigned MaxFactor, unsigned OpNumElts) { |
Hao Liu | 1c1e0c9 | 2015-06-26 02:10:27 +0000 | [diff] [blame] | 196 | unsigned NumElts = Mask.size(); |
| 197 | if (NumElts < 4) |
| 198 | return false; |
| 199 | |
| 200 | // Check potential Factors. |
| 201 | for (Factor = 2; Factor <= MaxFactor; Factor++) { |
| 202 | if (NumElts % Factor) |
| 203 | continue; |
| 204 | |
Alina Sbirlea | 77c5eaa | 2016-12-13 19:32:36 +0000 | [diff] [blame] | 205 | unsigned LaneLen = NumElts / Factor; |
| 206 | if (!isPowerOf2_32(LaneLen)) |
Hao Liu | 1c1e0c9 | 2015-06-26 02:10:27 +0000 | [diff] [blame] | 207 | continue; |
| 208 | |
Alina Sbirlea | 77c5eaa | 2016-12-13 19:32:36 +0000 | [diff] [blame] | 209 | // Check whether each element matches the general interleaved rule. |
| 210 | // Ignore undef elements, as long as the defined elements match the rule. |
| 211 | // Outer loop processes all factors (x, y, z in the above example) |
| 212 | unsigned I = 0, J; |
| 213 | for (; I < Factor; I++) { |
| 214 | unsigned SavedLaneValue; |
| 215 | unsigned SavedNoUndefs = 0; |
| 216 | |
| 217 | // Inner loop processes consecutive accesses (x, x+1... in the example) |
| 218 | for (J = 0; J < LaneLen - 1; J++) { |
| 219 | // Lane computes x's position in the Mask |
| 220 | unsigned Lane = J * Factor + I; |
| 221 | unsigned NextLane = Lane + Factor; |
| 222 | int LaneValue = Mask[Lane]; |
| 223 | int NextLaneValue = Mask[NextLane]; |
| 224 | |
| 225 | // If both are defined, values must be sequential |
| 226 | if (LaneValue >= 0 && NextLaneValue >= 0 && |
| 227 | LaneValue + 1 != NextLaneValue) |
| 228 | break; |
| 229 | |
| 230 | // If the next value is undef, save the current one as reference |
| 231 | if (LaneValue >= 0 && NextLaneValue < 0) { |
| 232 | SavedLaneValue = LaneValue; |
| 233 | SavedNoUndefs = 1; |
| 234 | } |
| 235 | |
| 236 | // Undefs are allowed, but defined elements must still be consecutive: |
| 237 | // i.e.: x,..., undef,..., x + 2,..., undef,..., undef,..., x + 5, .... |
| 238 | // Verify this by storing the last non-undef followed by an undef |
| 239 | // Check that following non-undef masks are incremented with the |
| 240 | // corresponding distance. |
| 241 | if (SavedNoUndefs > 0 && LaneValue < 0) { |
| 242 | SavedNoUndefs++; |
| 243 | if (NextLaneValue >= 0 && |
| 244 | SavedLaneValue + SavedNoUndefs != (unsigned)NextLaneValue) |
| 245 | break; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | if (J < LaneLen - 1) |
Hao Liu | 1c1e0c9 | 2015-06-26 02:10:27 +0000 | [diff] [blame] | 250 | break; |
| 251 | |
Alina Sbirlea | 77c5eaa | 2016-12-13 19:32:36 +0000 | [diff] [blame] | 252 | int StartMask = 0; |
| 253 | if (Mask[I] >= 0) { |
| 254 | // Check that the start of the I range (J=0) is greater than 0 |
| 255 | StartMask = Mask[I]; |
| 256 | } else if (Mask[(LaneLen - 1) * Factor + I] >= 0) { |
| 257 | // StartMask defined by the last value in lane |
| 258 | StartMask = Mask[(LaneLen - 1) * Factor + I] - J; |
| 259 | } else if (SavedNoUndefs > 0) { |
| 260 | // StartMask defined by some non-zero value in the j loop |
| 261 | StartMask = SavedLaneValue - (LaneLen - 1 - SavedNoUndefs); |
| 262 | } |
| 263 | // else StartMask remains set to 0, i.e. all elements are undefs |
| 264 | |
| 265 | if (StartMask < 0) |
| 266 | break; |
Matthias Braun | 01fa962 | 2017-01-31 18:37:53 +0000 | [diff] [blame] | 267 | // We must stay within the vectors; This case can happen with undefs. |
| 268 | if (StartMask + LaneLen > OpNumElts*2) |
| 269 | break; |
Alina Sbirlea | 77c5eaa | 2016-12-13 19:32:36 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | // Found an interleaved mask of current factor. |
| 273 | if (I == Factor) |
Hao Liu | 1c1e0c9 | 2015-06-26 02:10:27 +0000 | [diff] [blame] | 274 | return true; |
| 275 | } |
| 276 | |
| 277 | return false; |
| 278 | } |
| 279 | |
| 280 | bool InterleavedAccess::lowerInterleavedLoad( |
| 281 | LoadInst *LI, SmallVector<Instruction *, 32> &DeadInsts) { |
| 282 | if (!LI->isSimple()) |
| 283 | return false; |
| 284 | |
| 285 | SmallVector<ShuffleVectorInst *, 4> Shuffles; |
Matthew Simpson | 476c0af | 2016-05-19 21:39:00 +0000 | [diff] [blame] | 286 | SmallVector<ExtractElementInst *, 4> Extracts; |
Hao Liu | 1c1e0c9 | 2015-06-26 02:10:27 +0000 | [diff] [blame] | 287 | |
Matthew Simpson | 476c0af | 2016-05-19 21:39:00 +0000 | [diff] [blame] | 288 | // Check if all users of this load are shufflevectors. If we encounter any |
| 289 | // users that are extractelement instructions, we save them to later check if |
| 290 | // they can be modifed to extract from one of the shufflevectors instead of |
| 291 | // the load. |
Hao Liu | 1c1e0c9 | 2015-06-26 02:10:27 +0000 | [diff] [blame] | 292 | for (auto UI = LI->user_begin(), E = LI->user_end(); UI != E; UI++) { |
Matthew Simpson | 476c0af | 2016-05-19 21:39:00 +0000 | [diff] [blame] | 293 | auto *Extract = dyn_cast<ExtractElementInst>(*UI); |
| 294 | if (Extract && isa<ConstantInt>(Extract->getIndexOperand())) { |
| 295 | Extracts.push_back(Extract); |
| 296 | continue; |
| 297 | } |
Hao Liu | 1c1e0c9 | 2015-06-26 02:10:27 +0000 | [diff] [blame] | 298 | ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(*UI); |
| 299 | if (!SVI || !isa<UndefValue>(SVI->getOperand(1))) |
| 300 | return false; |
| 301 | |
| 302 | Shuffles.push_back(SVI); |
| 303 | } |
| 304 | |
| 305 | if (Shuffles.empty()) |
| 306 | return false; |
| 307 | |
| 308 | unsigned Factor, Index; |
| 309 | |
Eli Friedman | 96f295e | 2019-03-28 20:44:50 +0000 | [diff] [blame] | 310 | unsigned NumLoadElements = LI->getType()->getVectorNumElements(); |
Hao Liu | 1c1e0c9 | 2015-06-26 02:10:27 +0000 | [diff] [blame] | 311 | // Check if the first shufflevector is DE-interleave shuffle. |
Benjamin Kramer | 1e425c9 | 2016-10-18 18:59:58 +0000 | [diff] [blame] | 312 | if (!isDeInterleaveMask(Shuffles[0]->getShuffleMask(), Factor, Index, |
Eli Friedman | 96f295e | 2019-03-28 20:44:50 +0000 | [diff] [blame] | 313 | MaxFactor, NumLoadElements)) |
Hao Liu | 1c1e0c9 | 2015-06-26 02:10:27 +0000 | [diff] [blame] | 314 | return false; |
| 315 | |
| 316 | // Holds the corresponding index for each DE-interleave shuffle. |
| 317 | SmallVector<unsigned, 4> Indices; |
| 318 | Indices.push_back(Index); |
| 319 | |
| 320 | Type *VecTy = Shuffles[0]->getType(); |
| 321 | |
| 322 | // Check if other shufflevectors are also DE-interleaved of the same type |
| 323 | // and factor as the first shufflevector. |
| 324 | for (unsigned i = 1; i < Shuffles.size(); i++) { |
| 325 | if (Shuffles[i]->getType() != VecTy) |
| 326 | return false; |
| 327 | |
| 328 | if (!isDeInterleaveMaskOfFactor(Shuffles[i]->getShuffleMask(), Factor, |
| 329 | Index)) |
| 330 | return false; |
| 331 | |
| 332 | Indices.push_back(Index); |
| 333 | } |
| 334 | |
Matthew Simpson | 476c0af | 2016-05-19 21:39:00 +0000 | [diff] [blame] | 335 | // Try and modify users of the load that are extractelement instructions to |
| 336 | // use the shufflevector instructions instead of the load. |
| 337 | if (!tryReplaceExtracts(Extracts, Shuffles)) |
| 338 | return false; |
| 339 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 340 | LLVM_DEBUG(dbgs() << "IA: Found an interleaved load: " << *LI << "\n"); |
Hao Liu | 1c1e0c9 | 2015-06-26 02:10:27 +0000 | [diff] [blame] | 341 | |
| 342 | // Try to create target specific intrinsics to replace the load and shuffles. |
| 343 | if (!TLI->lowerInterleavedLoad(LI, Shuffles, Indices, Factor)) |
| 344 | return false; |
| 345 | |
| 346 | for (auto SVI : Shuffles) |
| 347 | DeadInsts.push_back(SVI); |
| 348 | |
| 349 | DeadInsts.push_back(LI); |
| 350 | return true; |
| 351 | } |
| 352 | |
Matthew Simpson | 476c0af | 2016-05-19 21:39:00 +0000 | [diff] [blame] | 353 | bool InterleavedAccess::tryReplaceExtracts( |
| 354 | ArrayRef<ExtractElementInst *> Extracts, |
| 355 | ArrayRef<ShuffleVectorInst *> Shuffles) { |
Matthew Simpson | 476c0af | 2016-05-19 21:39:00 +0000 | [diff] [blame] | 356 | // If there aren't any extractelement instructions to modify, there's nothing |
| 357 | // to do. |
| 358 | if (Extracts.empty()) |
| 359 | return true; |
| 360 | |
| 361 | // Maps extractelement instructions to vector-index pairs. The extractlement |
| 362 | // instructions will be modified to use the new vector and index operands. |
| 363 | DenseMap<ExtractElementInst *, std::pair<Value *, int>> ReplacementMap; |
| 364 | |
| 365 | for (auto *Extract : Extracts) { |
Matthew Simpson | 476c0af | 2016-05-19 21:39:00 +0000 | [diff] [blame] | 366 | // The vector index that is extracted. |
| 367 | auto *IndexOperand = cast<ConstantInt>(Extract->getIndexOperand()); |
| 368 | auto Index = IndexOperand->getSExtValue(); |
| 369 | |
| 370 | // Look for a suitable shufflevector instruction. The goal is to modify the |
| 371 | // extractelement instruction (which uses an interleaved load) to use one |
| 372 | // of the shufflevector instructions instead of the load. |
| 373 | for (auto *Shuffle : Shuffles) { |
Matthew Simpson | 476c0af | 2016-05-19 21:39:00 +0000 | [diff] [blame] | 374 | // If the shufflevector instruction doesn't dominate the extract, we |
| 375 | // can't create a use of it. |
| 376 | if (!DT->dominates(Shuffle, Extract)) |
| 377 | continue; |
| 378 | |
| 379 | // Inspect the indices of the shufflevector instruction. If the shuffle |
| 380 | // selects the same index that is extracted, we can modify the |
| 381 | // extractelement instruction. |
| 382 | SmallVector<int, 4> Indices; |
| 383 | Shuffle->getShuffleMask(Indices); |
| 384 | for (unsigned I = 0; I < Indices.size(); ++I) |
| 385 | if (Indices[I] == Index) { |
| 386 | assert(Extract->getOperand(0) == Shuffle->getOperand(0) && |
| 387 | "Vector operations do not match"); |
| 388 | ReplacementMap[Extract] = std::make_pair(Shuffle, I); |
| 389 | break; |
| 390 | } |
| 391 | |
| 392 | // If we found a suitable shufflevector instruction, stop looking. |
| 393 | if (ReplacementMap.count(Extract)) |
| 394 | break; |
| 395 | } |
| 396 | |
| 397 | // If we did not find a suitable shufflevector instruction, the |
| 398 | // extractelement instruction cannot be modified, so we must give up. |
| 399 | if (!ReplacementMap.count(Extract)) |
| 400 | return false; |
| 401 | } |
| 402 | |
| 403 | // Finally, perform the replacements. |
| 404 | IRBuilder<> Builder(Extracts[0]->getContext()); |
| 405 | for (auto &Replacement : ReplacementMap) { |
| 406 | auto *Extract = Replacement.first; |
| 407 | auto *Vector = Replacement.second.first; |
| 408 | auto Index = Replacement.second.second; |
| 409 | Builder.SetInsertPoint(Extract); |
| 410 | Extract->replaceAllUsesWith(Builder.CreateExtractElement(Vector, Index)); |
| 411 | Extract->eraseFromParent(); |
| 412 | } |
| 413 | |
| 414 | return true; |
| 415 | } |
| 416 | |
Hao Liu | 1c1e0c9 | 2015-06-26 02:10:27 +0000 | [diff] [blame] | 417 | bool InterleavedAccess::lowerInterleavedStore( |
| 418 | StoreInst *SI, SmallVector<Instruction *, 32> &DeadInsts) { |
| 419 | if (!SI->isSimple()) |
| 420 | return false; |
| 421 | |
| 422 | ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(SI->getValueOperand()); |
| 423 | if (!SVI || !SVI->hasOneUse()) |
| 424 | return false; |
| 425 | |
| 426 | // Check if the shufflevector is RE-interleave shuffle. |
| 427 | unsigned Factor; |
Matthias Braun | 01fa962 | 2017-01-31 18:37:53 +0000 | [diff] [blame] | 428 | unsigned OpNumElts = SVI->getOperand(0)->getType()->getVectorNumElements(); |
| 429 | if (!isReInterleaveMask(SVI->getShuffleMask(), Factor, MaxFactor, OpNumElts)) |
Hao Liu | 1c1e0c9 | 2015-06-26 02:10:27 +0000 | [diff] [blame] | 430 | return false; |
| 431 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 432 | LLVM_DEBUG(dbgs() << "IA: Found an interleaved store: " << *SI << "\n"); |
Hao Liu | 1c1e0c9 | 2015-06-26 02:10:27 +0000 | [diff] [blame] | 433 | |
| 434 | // Try to create target specific intrinsics to replace the store and shuffle. |
| 435 | if (!TLI->lowerInterleavedStore(SI, SVI, Factor)) |
| 436 | return false; |
| 437 | |
| 438 | // Already have a new target specific interleaved store. Erase the old store. |
| 439 | DeadInsts.push_back(SI); |
| 440 | DeadInsts.push_back(SVI); |
| 441 | return true; |
| 442 | } |
| 443 | |
| 444 | bool InterleavedAccess::runOnFunction(Function &F) { |
Francis Visoiu Mistrih | 8b61764 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 445 | auto *TPC = getAnalysisIfAvailable<TargetPassConfig>(); |
| 446 | if (!TPC || !LowerInterleavedAccesses) |
Hao Liu | 1c1e0c9 | 2015-06-26 02:10:27 +0000 | [diff] [blame] | 447 | return false; |
| 448 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 449 | LLVM_DEBUG(dbgs() << "*** " << getPassName() << ": " << F.getName() << "\n"); |
Hao Liu | 1c1e0c9 | 2015-06-26 02:10:27 +0000 | [diff] [blame] | 450 | |
Matthew Simpson | 476c0af | 2016-05-19 21:39:00 +0000 | [diff] [blame] | 451 | DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Francis Visoiu Mistrih | 8b61764 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 452 | auto &TM = TPC->getTM<TargetMachine>(); |
| 453 | TLI = TM.getSubtargetImpl(F)->getTargetLowering(); |
Hao Liu | 1c1e0c9 | 2015-06-26 02:10:27 +0000 | [diff] [blame] | 454 | MaxFactor = TLI->getMaxSupportedInterleaveFactor(); |
| 455 | |
| 456 | // Holds dead instructions that will be erased later. |
| 457 | SmallVector<Instruction *, 32> DeadInsts; |
| 458 | bool Changed = false; |
| 459 | |
Nico Rieck | 7819951 | 2015-08-06 19:10:45 +0000 | [diff] [blame] | 460 | for (auto &I : instructions(F)) { |
Hao Liu | 1c1e0c9 | 2015-06-26 02:10:27 +0000 | [diff] [blame] | 461 | if (LoadInst *LI = dyn_cast<LoadInst>(&I)) |
| 462 | Changed |= lowerInterleavedLoad(LI, DeadInsts); |
| 463 | |
| 464 | if (StoreInst *SI = dyn_cast<StoreInst>(&I)) |
| 465 | Changed |= lowerInterleavedStore(SI, DeadInsts); |
| 466 | } |
| 467 | |
| 468 | for (auto I : DeadInsts) |
| 469 | I->eraseFromParent(); |
| 470 | |
| 471 | return Changed; |
| 472 | } |