Sam Parker | a761ba0 | 2019-08-28 08:51:13 +0000 | [diff] [blame] | 1 | //===- ARMParallelDSP.cpp - Parallel DSP Pass -----------------------------===// |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +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 |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | /// \file |
| 10 | /// Armv6 introduced instructions to perform 32-bit SIMD operations. The |
| 11 | /// purpose of this pass is do some IR pattern matching to create ACLE |
| 12 | /// DSP intrinsics, which map on these 32-bit SIMD operations. |
Sjoerd Meijer | 53449da | 2018-07-11 12:36:25 +0000 | [diff] [blame] | 13 | /// This pass runs only when unaligned accesses is supported/enabled. |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Sjoerd Meijer | b3e06fa | 2018-07-06 14:47:09 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/Statistic.h" |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallPtrSet.h" |
| 19 | #include "llvm/Analysis/AliasAnalysis.h" |
| 20 | #include "llvm/Analysis/LoopAccessAnalysis.h" |
Sam Parker | 1c3ca61 | 2019-10-16 09:37:03 +0000 | [diff] [blame^] | 21 | #include "llvm/Analysis/OrderedBasicBlock.h" |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Instructions.h" |
| 23 | #include "llvm/IR/NoFolder.h" |
| 24 | #include "llvm/Transforms/Scalar.h" |
| 25 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 26 | #include "llvm/Pass.h" |
| 27 | #include "llvm/PassRegistry.h" |
| 28 | #include "llvm/PassSupport.h" |
| 29 | #include "llvm/Support/Debug.h" |
| 30 | #include "llvm/IR/PatternMatch.h" |
| 31 | #include "llvm/CodeGen/TargetPassConfig.h" |
| 32 | #include "ARM.h" |
| 33 | #include "ARMSubtarget.h" |
| 34 | |
| 35 | using namespace llvm; |
| 36 | using namespace PatternMatch; |
| 37 | |
Sjoerd Meijer | b3e06fa | 2018-07-06 14:47:09 +0000 | [diff] [blame] | 38 | #define DEBUG_TYPE "arm-parallel-dsp" |
| 39 | |
| 40 | STATISTIC(NumSMLAD , "Number of smlad instructions generated"); |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 41 | |
Sjoerd Meijer | 3c859b3 | 2018-08-14 07:43:49 +0000 | [diff] [blame] | 42 | static cl::opt<bool> |
| 43 | DisableParallelDSP("disable-arm-parallel-dsp", cl::Hidden, cl::init(false), |
| 44 | cl::desc("Disable the ARM Parallel DSP pass")); |
| 45 | |
Sam Parker | 1c3ca61 | 2019-10-16 09:37:03 +0000 | [diff] [blame^] | 46 | static cl::opt<unsigned> |
| 47 | NumLoadLimit("arm-parallel-dsp-load-limit", cl::Hidden, cl::init(16), |
| 48 | cl::desc("Limit the number of loads analysed")); |
| 49 | |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 50 | namespace { |
Sam Parker | 414dd1c | 2019-07-29 08:41:51 +0000 | [diff] [blame] | 51 | struct MulCandidate; |
Sam Parker | 85ad78b | 2019-07-11 07:47:50 +0000 | [diff] [blame] | 52 | class Reduction; |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 53 | |
Sam Parker | cd38599 | 2019-08-02 08:21:17 +0000 | [diff] [blame] | 54 | using MulCandList = SmallVector<std::unique_ptr<MulCandidate>, 8>; |
| 55 | using MemInstList = SmallVectorImpl<LoadInst*>; |
| 56 | using MulPairList = SmallVector<std::pair<MulCandidate*, MulCandidate*>, 8>; |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 57 | |
Sam Parker | 414dd1c | 2019-07-29 08:41:51 +0000 | [diff] [blame] | 58 | // 'MulCandidate' holds the multiplication instructions that are candidates |
Sam Parker | 3da59e5 | 2019-07-26 14:11:40 +0000 | [diff] [blame] | 59 | // for parallel execution. |
Sam Parker | 414dd1c | 2019-07-29 08:41:51 +0000 | [diff] [blame] | 60 | struct MulCandidate { |
Sam Parker | 89a3799 | 2018-07-23 15:25:59 +0000 | [diff] [blame] | 61 | Instruction *Root; |
Sam Parker | 414dd1c | 2019-07-29 08:41:51 +0000 | [diff] [blame] | 62 | Value* LHS; |
| 63 | Value* RHS; |
Sam Parker | 3da59e5 | 2019-07-26 14:11:40 +0000 | [diff] [blame] | 64 | bool Exchange = false; |
Sam Parker | 89a3799 | 2018-07-23 15:25:59 +0000 | [diff] [blame] | 65 | bool ReadOnly = true; |
Sam Parker | a761ba0 | 2019-08-28 08:51:13 +0000 | [diff] [blame] | 66 | bool Paired = false; |
Sam Parker | cd38599 | 2019-08-02 08:21:17 +0000 | [diff] [blame] | 67 | SmallVector<LoadInst*, 2> VecLd; // Container for loads to widen. |
Sam Parker | 89a3799 | 2018-07-23 15:25:59 +0000 | [diff] [blame] | 68 | |
Sam Parker | 14c6dfd | 2019-08-02 07:32:28 +0000 | [diff] [blame] | 69 | MulCandidate(Instruction *I, Value *lhs, Value *rhs) : |
| 70 | Root(I), LHS(lhs), RHS(rhs) { } |
Sam Parker | 89a3799 | 2018-07-23 15:25:59 +0000 | [diff] [blame] | 71 | |
Sam Parker | 414dd1c | 2019-07-29 08:41:51 +0000 | [diff] [blame] | 72 | bool HasTwoLoadInputs() const { |
| 73 | return isa<LoadInst>(LHS) && isa<LoadInst>(RHS); |
| 74 | } |
Sam Parker | 7ca8c6f | 2019-08-01 08:17:51 +0000 | [diff] [blame] | 75 | |
| 76 | LoadInst *getBaseLoad() const { |
Sam Parker | a761ba0 | 2019-08-28 08:51:13 +0000 | [diff] [blame] | 77 | return VecLd.front(); |
Sam Parker | 7ca8c6f | 2019-08-01 08:17:51 +0000 | [diff] [blame] | 78 | } |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 79 | }; |
| 80 | |
Sam Parker | 85ad78b | 2019-07-11 07:47:50 +0000 | [diff] [blame] | 81 | /// Represent a sequence of multiply-accumulate operations with the aim to |
| 82 | /// perform the multiplications in parallel. |
| 83 | class Reduction { |
| 84 | Instruction *Root = nullptr; |
| 85 | Value *Acc = nullptr; |
Sam Parker | 414dd1c | 2019-07-29 08:41:51 +0000 | [diff] [blame] | 86 | MulCandList Muls; |
Sam Parker | cd38599 | 2019-08-02 08:21:17 +0000 | [diff] [blame] | 87 | MulPairList MulPairs; |
Sam Parker | a761ba0 | 2019-08-28 08:51:13 +0000 | [diff] [blame] | 88 | SetVector<Instruction*> Adds; |
Sam Parker | 85ad78b | 2019-07-11 07:47:50 +0000 | [diff] [blame] | 89 | |
| 90 | public: |
| 91 | Reduction() = delete; |
| 92 | |
| 93 | Reduction (Instruction *Add) : Root(Add) { } |
| 94 | |
| 95 | /// Record an Add instruction that is a part of the this reduction. |
| 96 | void InsertAdd(Instruction *I) { Adds.insert(I); } |
| 97 | |
Sam Parker | a761ba0 | 2019-08-28 08:51:13 +0000 | [diff] [blame] | 98 | /// Create MulCandidates, each rooted at a Mul instruction, that is a part |
| 99 | /// of this reduction. |
| 100 | void InsertMuls() { |
| 101 | auto GetMulOperand = [](Value *V) -> Instruction* { |
| 102 | if (auto *SExt = dyn_cast<SExtInst>(V)) { |
| 103 | if (auto *I = dyn_cast<Instruction>(SExt->getOperand(0))) |
| 104 | if (I->getOpcode() == Instruction::Mul) |
| 105 | return I; |
| 106 | } else if (auto *I = dyn_cast<Instruction>(V)) { |
| 107 | if (I->getOpcode() == Instruction::Mul) |
| 108 | return I; |
| 109 | } |
| 110 | return nullptr; |
| 111 | }; |
| 112 | |
| 113 | auto InsertMul = [this](Instruction *I) { |
| 114 | Value *LHS = cast<Instruction>(I->getOperand(0))->getOperand(0); |
| 115 | Value *RHS = cast<Instruction>(I->getOperand(1))->getOperand(0); |
| 116 | Muls.push_back(std::make_unique<MulCandidate>(I, LHS, RHS)); |
| 117 | }; |
| 118 | |
| 119 | for (auto *Add : Adds) { |
| 120 | if (Add == Acc) |
| 121 | continue; |
| 122 | if (auto *Mul = GetMulOperand(Add->getOperand(0))) |
| 123 | InsertMul(Mul); |
| 124 | if (auto *Mul = GetMulOperand(Add->getOperand(1))) |
| 125 | InsertMul(Mul); |
| 126 | } |
Sam Parker | 85ad78b | 2019-07-11 07:47:50 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | /// Add the incoming accumulator value, returns true if a value had not |
| 130 | /// already been added. Returning false signals to the user that this |
| 131 | /// reduction already has a value to initialise the accumulator. |
| 132 | bool InsertAcc(Value *V) { |
| 133 | if (Acc) |
| 134 | return false; |
| 135 | Acc = V; |
| 136 | return true; |
| 137 | } |
| 138 | |
Sam Parker | 414dd1c | 2019-07-29 08:41:51 +0000 | [diff] [blame] | 139 | /// Set two MulCandidates, rooted at muls, that can be executed as a single |
Sam Parker | 85ad78b | 2019-07-11 07:47:50 +0000 | [diff] [blame] | 140 | /// parallel operation. |
Sam Parker | a761ba0 | 2019-08-28 08:51:13 +0000 | [diff] [blame] | 141 | void AddMulPair(MulCandidate *Mul0, MulCandidate *Mul1, |
| 142 | bool Exchange = false) { |
| 143 | LLVM_DEBUG(dbgs() << "Pairing:\n" |
| 144 | << *Mul0->Root << "\n" |
| 145 | << *Mul1->Root << "\n"); |
| 146 | Mul0->Paired = true; |
| 147 | Mul1->Paired = true; |
| 148 | if (Exchange) |
| 149 | Mul1->Exchange = true; |
Sam Parker | 85ad78b | 2019-07-11 07:47:50 +0000 | [diff] [blame] | 150 | MulPairs.push_back(std::make_pair(Mul0, Mul1)); |
| 151 | } |
| 152 | |
| 153 | /// Return true if enough mul operations are found that can be executed in |
| 154 | /// parallel. |
| 155 | bool CreateParallelPairs(); |
| 156 | |
| 157 | /// Return the add instruction which is the root of the reduction. |
| 158 | Instruction *getRoot() { return Root; } |
| 159 | |
Sam Parker | 7ca8c6f | 2019-08-01 08:17:51 +0000 | [diff] [blame] | 160 | bool is64Bit() const { return Root->getType()->isIntegerTy(64); } |
| 161 | |
Sam Parker | c363deb | 2019-09-09 08:39:14 +0000 | [diff] [blame] | 162 | Type *getType() const { return Root->getType(); } |
| 163 | |
Sam Parker | 85ad78b | 2019-07-11 07:47:50 +0000 | [diff] [blame] | 164 | /// Return the incoming value to be accumulated. This maybe null. |
| 165 | Value *getAccumulator() { return Acc; } |
| 166 | |
| 167 | /// Return the set of adds that comprise the reduction. |
Sam Parker | a761ba0 | 2019-08-28 08:51:13 +0000 | [diff] [blame] | 168 | SetVector<Instruction*> &getAdds() { return Adds; } |
Sam Parker | 85ad78b | 2019-07-11 07:47:50 +0000 | [diff] [blame] | 169 | |
Sam Parker | 414dd1c | 2019-07-29 08:41:51 +0000 | [diff] [blame] | 170 | /// Return the MulCandidate, rooted at mul instruction, that comprise the |
Sam Parker | 85ad78b | 2019-07-11 07:47:50 +0000 | [diff] [blame] | 171 | /// the reduction. |
Sam Parker | 414dd1c | 2019-07-29 08:41:51 +0000 | [diff] [blame] | 172 | MulCandList &getMuls() { return Muls; } |
Sam Parker | 85ad78b | 2019-07-11 07:47:50 +0000 | [diff] [blame] | 173 | |
Sam Parker | 414dd1c | 2019-07-29 08:41:51 +0000 | [diff] [blame] | 174 | /// Return the MulCandidate, rooted at mul instructions, that have been |
Sam Parker | 85ad78b | 2019-07-11 07:47:50 +0000 | [diff] [blame] | 175 | /// paired for parallel execution. |
Sam Parker | cd38599 | 2019-08-02 08:21:17 +0000 | [diff] [blame] | 176 | MulPairList &getMulPairs() { return MulPairs; } |
Sam Parker | 85ad78b | 2019-07-11 07:47:50 +0000 | [diff] [blame] | 177 | |
| 178 | /// To finalise, replace the uses of the root with the intrinsic call. |
| 179 | void UpdateRoot(Instruction *SMLAD) { |
| 180 | Root->replaceAllUsesWith(SMLAD); |
| 181 | } |
Sam Parker | a761ba0 | 2019-08-28 08:51:13 +0000 | [diff] [blame] | 182 | |
| 183 | void dump() { |
| 184 | LLVM_DEBUG(dbgs() << "Reduction:\n"; |
| 185 | for (auto *Add : Adds) |
| 186 | LLVM_DEBUG(dbgs() << *Add << "\n"); |
| 187 | for (auto &Mul : Muls) |
| 188 | LLVM_DEBUG(dbgs() << *Mul->Root << "\n" |
| 189 | << " " << *Mul->LHS << "\n" |
| 190 | << " " << *Mul->RHS << "\n"); |
| 191 | LLVM_DEBUG(if (Acc) dbgs() << "Acc in: " << *Acc << "\n") |
| 192 | ); |
| 193 | } |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 194 | }; |
| 195 | |
Sam Parker | 4c4ff13 | 2019-03-14 11:14:13 +0000 | [diff] [blame] | 196 | class WidenedLoad { |
| 197 | LoadInst *NewLd = nullptr; |
| 198 | SmallVector<LoadInst*, 4> Loads; |
| 199 | |
| 200 | public: |
| 201 | WidenedLoad(SmallVectorImpl<LoadInst*> &Lds, LoadInst *Wide) |
| 202 | : NewLd(Wide) { |
| 203 | for (auto *I : Lds) |
| 204 | Loads.push_back(I); |
| 205 | } |
| 206 | LoadInst *getLoad() { |
| 207 | return NewLd; |
| 208 | } |
| 209 | }; |
| 210 | |
Sam Parker | a761ba0 | 2019-08-28 08:51:13 +0000 | [diff] [blame] | 211 | class ARMParallelDSP : public FunctionPass { |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 212 | ScalarEvolution *SE; |
| 213 | AliasAnalysis *AA; |
| 214 | TargetLibraryInfo *TLI; |
| 215 | DominatorTree *DT; |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 216 | const DataLayout *DL; |
| 217 | Module *M; |
Sam Parker | 453ba91 | 2018-11-09 09:18:00 +0000 | [diff] [blame] | 218 | std::map<LoadInst*, LoadInst*> LoadPairs; |
Sam Parker | 85ad78b | 2019-07-11 07:47:50 +0000 | [diff] [blame] | 219 | SmallPtrSet<LoadInst*, 4> OffsetLoads; |
Sam Parker | 4c4ff13 | 2019-03-14 11:14:13 +0000 | [diff] [blame] | 220 | std::map<LoadInst*, std::unique_ptr<WidenedLoad>> WideLoads; |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 221 | |
Sam Parker | 85ad78b | 2019-07-11 07:47:50 +0000 | [diff] [blame] | 222 | template<unsigned> |
Sam Parker | a761ba0 | 2019-08-28 08:51:13 +0000 | [diff] [blame] | 223 | bool IsNarrowSequence(Value *V); |
| 224 | bool Search(Value *V, BasicBlock *BB, Reduction &R); |
Sam Parker | a33e311 | 2019-05-13 09:23:32 +0000 | [diff] [blame] | 225 | bool RecordMemoryOps(BasicBlock *BB); |
Sam Parker | 85ad78b | 2019-07-11 07:47:50 +0000 | [diff] [blame] | 226 | void InsertParallelMACs(Reduction &Reduction); |
Fangrui Song | 6816934 | 2018-07-03 19:12:27 +0000 | [diff] [blame] | 227 | bool AreSequentialLoads(LoadInst *Ld0, LoadInst *Ld1, MemInstList &VecMem); |
Sam Parker | cd38599 | 2019-08-02 08:21:17 +0000 | [diff] [blame] | 228 | LoadInst* CreateWideLoad(MemInstList &Loads, IntegerType *LoadTy); |
Sam Parker | 85ad78b | 2019-07-11 07:47:50 +0000 | [diff] [blame] | 229 | bool CreateParallelPairs(Reduction &R); |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 230 | |
| 231 | /// Try to match and generate: SMLAD, SMLADX - Signed Multiply Accumulate |
| 232 | /// Dual performs two signed 16x16-bit multiplications. It adds the |
| 233 | /// products to a 32-bit accumulate operand. Optionally, the instruction can |
| 234 | /// exchange the halfwords of the second operand before performing the |
| 235 | /// arithmetic. |
Sam Parker | a761ba0 | 2019-08-28 08:51:13 +0000 | [diff] [blame] | 236 | bool MatchSMLAD(Function &F); |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 237 | |
| 238 | public: |
| 239 | static char ID; |
| 240 | |
Sam Parker | a761ba0 | 2019-08-28 08:51:13 +0000 | [diff] [blame] | 241 | ARMParallelDSP() : FunctionPass(ID) { } |
Sam Parker | a33e311 | 2019-05-13 09:23:32 +0000 | [diff] [blame] | 242 | |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 243 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Sam Parker | a761ba0 | 2019-08-28 08:51:13 +0000 | [diff] [blame] | 244 | FunctionPass::getAnalysisUsage(AU); |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 245 | AU.addRequired<AssumptionCacheTracker>(); |
| 246 | AU.addRequired<ScalarEvolutionWrapperPass>(); |
| 247 | AU.addRequired<AAResultsWrapperPass>(); |
| 248 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 249 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 250 | AU.addRequired<TargetPassConfig>(); |
Sam Parker | a761ba0 | 2019-08-28 08:51:13 +0000 | [diff] [blame] | 251 | AU.addPreserved<ScalarEvolutionWrapperPass>(); |
| 252 | AU.addPreserved<GlobalsAAWrapperPass>(); |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 253 | AU.setPreservesCFG(); |
| 254 | } |
| 255 | |
Sam Parker | a761ba0 | 2019-08-28 08:51:13 +0000 | [diff] [blame] | 256 | bool runOnFunction(Function &F) override { |
Sjoerd Meijer | 3c859b3 | 2018-08-14 07:43:49 +0000 | [diff] [blame] | 257 | if (DisableParallelDSP) |
| 258 | return false; |
Sam Parker | a761ba0 | 2019-08-28 08:51:13 +0000 | [diff] [blame] | 259 | if (skipFunction(F)) |
Eli Friedman | b27fc95 | 2019-07-23 20:48:46 +0000 | [diff] [blame] | 260 | return false; |
| 261 | |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 262 | SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE(); |
| 263 | AA = &getAnalysis<AAResultsWrapperPass>().getAAResults(); |
Teresa Johnson | 9c27b59 | 2019-09-07 03:09:36 +0000 | [diff] [blame] | 264 | TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F); |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 265 | DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 266 | auto &TPC = getAnalysis<TargetPassConfig>(); |
| 267 | |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 268 | M = F.getParent(); |
| 269 | DL = &M->getDataLayout(); |
| 270 | |
| 271 | auto &TM = TPC.getTM<TargetMachine>(); |
| 272 | auto *ST = &TM.getSubtarget<ARMSubtarget>(F); |
| 273 | |
| 274 | if (!ST->allowsUnalignedMem()) { |
| 275 | LLVM_DEBUG(dbgs() << "Unaligned memory access not supported: not " |
| 276 | "running pass ARMParallelDSP\n"); |
| 277 | return false; |
| 278 | } |
| 279 | |
| 280 | if (!ST->hasDSP()) { |
| 281 | LLVM_DEBUG(dbgs() << "DSP extension not enabled: not running pass " |
| 282 | "ARMParallelDSP\n"); |
| 283 | return false; |
| 284 | } |
| 285 | |
Sam Parker | 9e73020 | 2019-03-15 10:19:32 +0000 | [diff] [blame] | 286 | if (!ST->isLittle()) { |
| 287 | LLVM_DEBUG(dbgs() << "Only supporting little endian: not running pass " |
Sam Parker | a33e311 | 2019-05-13 09:23:32 +0000 | [diff] [blame] | 288 | << "ARMParallelDSP\n"); |
Sam Parker | 9e73020 | 2019-03-15 10:19:32 +0000 | [diff] [blame] | 289 | return false; |
| 290 | } |
| 291 | |
Sam Parker | a023c7a | 2018-09-12 09:17:44 +0000 | [diff] [blame] | 292 | LLVM_DEBUG(dbgs() << "\n== Parallel DSP pass ==\n"); |
| 293 | LLVM_DEBUG(dbgs() << " - " << F.getName() << "\n\n"); |
Sam Parker | 453ba91 | 2018-11-09 09:18:00 +0000 | [diff] [blame] | 294 | |
Sam Parker | a761ba0 | 2019-08-28 08:51:13 +0000 | [diff] [blame] | 295 | bool Changes = MatchSMLAD(F); |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 296 | return Changes; |
| 297 | } |
| 298 | }; |
| 299 | } |
| 300 | |
Sam Parker | ffc1681 | 2018-07-03 12:44:16 +0000 | [diff] [blame] | 301 | template<typename MemInst> |
| 302 | static bool AreSequentialAccesses(MemInst *MemOp0, MemInst *MemOp1, |
Sam Parker | 453ba91 | 2018-11-09 09:18:00 +0000 | [diff] [blame] | 303 | const DataLayout &DL, ScalarEvolution &SE) { |
Sam Parker | 4c4ff13 | 2019-03-14 11:14:13 +0000 | [diff] [blame] | 304 | if (isConsecutiveAccess(MemOp0, MemOp1, DL, SE)) |
Sam Parker | ffc1681 | 2018-07-03 12:44:16 +0000 | [diff] [blame] | 305 | return true; |
Sam Parker | ffc1681 | 2018-07-03 12:44:16 +0000 | [diff] [blame] | 306 | return false; |
| 307 | } |
| 308 | |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 309 | bool ARMParallelDSP::AreSequentialLoads(LoadInst *Ld0, LoadInst *Ld1, |
Sam Parker | ffc1681 | 2018-07-03 12:44:16 +0000 | [diff] [blame] | 310 | MemInstList &VecMem) { |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 311 | if (!Ld0 || !Ld1) |
| 312 | return false; |
| 313 | |
Sam Parker | 4c4ff13 | 2019-03-14 11:14:13 +0000 | [diff] [blame] | 314 | if (!LoadPairs.count(Ld0) || LoadPairs[Ld0] != Ld1) |
| 315 | return false; |
| 316 | |
| 317 | LLVM_DEBUG(dbgs() << "Loads are sequential and valid:\n"; |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 318 | dbgs() << "Ld0:"; Ld0->dump(); |
| 319 | dbgs() << "Ld1:"; Ld1->dump(); |
| 320 | ); |
| 321 | |
Sam Parker | 453ba91 | 2018-11-09 09:18:00 +0000 | [diff] [blame] | 322 | VecMem.clear(); |
| 323 | VecMem.push_back(Ld0); |
| 324 | VecMem.push_back(Ld1); |
| 325 | return true; |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Sam Parker | 85ad78b | 2019-07-11 07:47:50 +0000 | [diff] [blame] | 328 | // MaxBitwidth: the maximum supported bitwidth of the elements in the DSP |
| 329 | // instructions, which is set to 16. So here we should collect all i8 and i16 |
| 330 | // narrow operations. |
| 331 | // TODO: we currently only collect i16, and will support i8 later, so that's |
| 332 | // why we check that types are equal to MaxBitWidth, and not <= MaxBitWidth. |
| 333 | template<unsigned MaxBitWidth> |
Sam Parker | a761ba0 | 2019-08-28 08:51:13 +0000 | [diff] [blame] | 334 | bool ARMParallelDSP::IsNarrowSequence(Value *V) { |
Sam Parker | 7440065 | 2019-07-26 10:57:42 +0000 | [diff] [blame] | 335 | if (auto *SExt = dyn_cast<SExtInst>(V)) { |
| 336 | if (SExt->getSrcTy()->getIntegerBitWidth() != MaxBitWidth) |
Sam Parker | 85ad78b | 2019-07-11 07:47:50 +0000 | [diff] [blame] | 337 | return false; |
| 338 | |
Sam Parker | 7440065 | 2019-07-26 10:57:42 +0000 | [diff] [blame] | 339 | if (auto *Ld = dyn_cast<LoadInst>(SExt->getOperand(0))) { |
Sam Parker | a761ba0 | 2019-08-28 08:51:13 +0000 | [diff] [blame] | 340 | // Check that this load could be paired. |
| 341 | return LoadPairs.count(Ld) || OffsetLoads.count(Ld); |
Sam Parker | 85ad78b | 2019-07-11 07:47:50 +0000 | [diff] [blame] | 342 | } |
| 343 | } |
| 344 | return false; |
| 345 | } |
| 346 | |
Sam Parker | a33e311 | 2019-05-13 09:23:32 +0000 | [diff] [blame] | 347 | /// Iterate through the block and record base, offset pairs of loads which can |
| 348 | /// be widened into a single load. |
| 349 | bool ARMParallelDSP::RecordMemoryOps(BasicBlock *BB) { |
Sam Parker | 453ba91 | 2018-11-09 09:18:00 +0000 | [diff] [blame] | 350 | SmallVector<LoadInst*, 8> Loads; |
Sam Parker | a33e311 | 2019-05-13 09:23:32 +0000 | [diff] [blame] | 351 | SmallVector<Instruction*, 8> Writes; |
Sam Parker | a761ba0 | 2019-08-28 08:51:13 +0000 | [diff] [blame] | 352 | LoadPairs.clear(); |
| 353 | WideLoads.clear(); |
Sam Parker | 1c3ca61 | 2019-10-16 09:37:03 +0000 | [diff] [blame^] | 354 | OrderedBasicBlock OrderedBB(BB); |
Sam Parker | a33e311 | 2019-05-13 09:23:32 +0000 | [diff] [blame] | 355 | |
| 356 | // Collect loads and instruction that may write to memory. For now we only |
| 357 | // record loads which are simple, sign-extended and have a single user. |
| 358 | // TODO: Allow zero-extended loads. |
Sam Parker | 4c4ff13 | 2019-03-14 11:14:13 +0000 | [diff] [blame] | 359 | for (auto &I : *BB) { |
Sam Parker | a33e311 | 2019-05-13 09:23:32 +0000 | [diff] [blame] | 360 | if (I.mayWriteToMemory()) |
| 361 | Writes.push_back(&I); |
Sam Parker | 453ba91 | 2018-11-09 09:18:00 +0000 | [diff] [blame] | 362 | auto *Ld = dyn_cast<LoadInst>(&I); |
Sam Parker | 4c4ff13 | 2019-03-14 11:14:13 +0000 | [diff] [blame] | 363 | if (!Ld || !Ld->isSimple() || |
| 364 | !Ld->hasOneUse() || !isa<SExtInst>(Ld->user_back())) |
Sam Parker | 453ba91 | 2018-11-09 09:18:00 +0000 | [diff] [blame] | 365 | continue; |
| 366 | Loads.push_back(Ld); |
| 367 | } |
| 368 | |
Sam Parker | 1c3ca61 | 2019-10-16 09:37:03 +0000 | [diff] [blame^] | 369 | if (Loads.empty() || Loads.size() > NumLoadLimit) |
| 370 | return false; |
| 371 | |
Sam Parker | a33e311 | 2019-05-13 09:23:32 +0000 | [diff] [blame] | 372 | using InstSet = std::set<Instruction*>; |
| 373 | using DepMap = std::map<Instruction*, InstSet>; |
| 374 | DepMap RAWDeps; |
| 375 | |
| 376 | // Record any writes that may alias a load. |
| 377 | const auto Size = LocationSize::unknown(); |
Sam Parker | 1c3ca61 | 2019-10-16 09:37:03 +0000 | [diff] [blame^] | 378 | for (auto Write : Writes) { |
| 379 | for (auto Read : Loads) { |
Sam Parker | a33e311 | 2019-05-13 09:23:32 +0000 | [diff] [blame] | 380 | MemoryLocation ReadLoc = |
| 381 | MemoryLocation(Read->getPointerOperand(), Size); |
| 382 | |
| 383 | if (!isModOrRefSet(intersectModRef(AA->getModRefInfo(Write, ReadLoc), |
| 384 | ModRefInfo::ModRef))) |
| 385 | continue; |
Sam Parker | 1c3ca61 | 2019-10-16 09:37:03 +0000 | [diff] [blame^] | 386 | if (OrderedBB.dominates(Write, Read)) |
Sam Parker | a33e311 | 2019-05-13 09:23:32 +0000 | [diff] [blame] | 387 | RAWDeps[Read].insert(Write); |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | // Check whether there's not a write between the two loads which would |
| 392 | // prevent them from being safely merged. |
| 393 | auto SafeToPair = [&](LoadInst *Base, LoadInst *Offset) { |
Sam Parker | 1c3ca61 | 2019-10-16 09:37:03 +0000 | [diff] [blame^] | 394 | LoadInst *Dominator = OrderedBB.dominates(Base, Offset) ? Base : Offset; |
| 395 | LoadInst *Dominated = OrderedBB.dominates(Base, Offset) ? Offset : Base; |
Sam Parker | a33e311 | 2019-05-13 09:23:32 +0000 | [diff] [blame] | 396 | |
| 397 | if (RAWDeps.count(Dominated)) { |
| 398 | InstSet &WritesBefore = RAWDeps[Dominated]; |
| 399 | |
| 400 | for (auto Before : WritesBefore) { |
Sam Parker | a33e311 | 2019-05-13 09:23:32 +0000 | [diff] [blame] | 401 | // We can't move the second load backward, past a write, to merge |
| 402 | // with the first load. |
Sam Parker | 1c3ca61 | 2019-10-16 09:37:03 +0000 | [diff] [blame^] | 403 | if (OrderedBB.dominates(Dominator, Before)) |
Sam Parker | a33e311 | 2019-05-13 09:23:32 +0000 | [diff] [blame] | 404 | return false; |
| 405 | } |
| 406 | } |
| 407 | return true; |
| 408 | }; |
| 409 | |
| 410 | // Record base, offset load pairs. |
| 411 | for (auto *Base : Loads) { |
| 412 | for (auto *Offset : Loads) { |
Sam Parker | 1c3ca61 | 2019-10-16 09:37:03 +0000 | [diff] [blame^] | 413 | if (Base == Offset || OffsetLoads.count(Offset)) |
Sam Parker | 453ba91 | 2018-11-09 09:18:00 +0000 | [diff] [blame] | 414 | continue; |
| 415 | |
Sam Parker | a33e311 | 2019-05-13 09:23:32 +0000 | [diff] [blame] | 416 | if (AreSequentialAccesses<LoadInst>(Base, Offset, *DL, *SE) && |
| 417 | SafeToPair(Base, Offset)) { |
| 418 | LoadPairs[Base] = Offset; |
Sam Parker | 85ad78b | 2019-07-11 07:47:50 +0000 | [diff] [blame] | 419 | OffsetLoads.insert(Offset); |
Sam Parker | 4c4ff13 | 2019-03-14 11:14:13 +0000 | [diff] [blame] | 420 | break; |
Sam Parker | 453ba91 | 2018-11-09 09:18:00 +0000 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | } |
Sam Parker | 4c4ff13 | 2019-03-14 11:14:13 +0000 | [diff] [blame] | 424 | |
| 425 | LLVM_DEBUG(if (!LoadPairs.empty()) { |
| 426 | dbgs() << "Consecutive load pairs:\n"; |
| 427 | for (auto &MapIt : LoadPairs) { |
| 428 | LLVM_DEBUG(dbgs() << *MapIt.first << ", " |
| 429 | << *MapIt.second << "\n"); |
| 430 | } |
| 431 | }); |
Sam Parker | 453ba91 | 2018-11-09 09:18:00 +0000 | [diff] [blame] | 432 | return LoadPairs.size() > 1; |
| 433 | } |
| 434 | |
Sam Parker | a761ba0 | 2019-08-28 08:51:13 +0000 | [diff] [blame] | 435 | // Search recursively back through the operands to find a tree of values that |
| 436 | // form a multiply-accumulate chain. The search records the Add and Mul |
| 437 | // instructions that form the reduction and allows us to find a single value |
| 438 | // to be used as the initial input to the accumlator. |
| 439 | bool ARMParallelDSP::Search(Value *V, BasicBlock *BB, Reduction &R) { |
| 440 | // If we find a non-instruction, try to use it as the initial accumulator |
| 441 | // value. This may have already been found during the search in which case |
| 442 | // this function will return false, signaling a search fail. |
| 443 | auto *I = dyn_cast<Instruction>(V); |
| 444 | if (!I) |
| 445 | return R.InsertAcc(V); |
| 446 | |
| 447 | if (I->getParent() != BB) |
| 448 | return false; |
| 449 | |
| 450 | switch (I->getOpcode()) { |
| 451 | default: |
| 452 | break; |
| 453 | case Instruction::PHI: |
| 454 | // Could be the accumulator value. |
| 455 | return R.InsertAcc(V); |
| 456 | case Instruction::Add: { |
| 457 | // Adds should be adding together two muls, or another add and a mul to |
| 458 | // be within the mac chain. One of the operands may also be the |
| 459 | // accumulator value at which point we should stop searching. |
| 460 | R.InsertAdd(I); |
| 461 | Value *LHS = I->getOperand(0); |
| 462 | Value *RHS = I->getOperand(1); |
| 463 | bool ValidLHS = Search(LHS, BB, R); |
| 464 | bool ValidRHS = Search(RHS, BB, R); |
| 465 | |
| 466 | if (ValidLHS && ValidRHS) |
| 467 | return true; |
| 468 | |
| 469 | return R.InsertAcc(I); |
| 470 | } |
| 471 | case Instruction::Mul: { |
| 472 | Value *MulOp0 = I->getOperand(0); |
| 473 | Value *MulOp1 = I->getOperand(1); |
| 474 | return IsNarrowSequence<16>(MulOp0) && IsNarrowSequence<16>(MulOp1); |
| 475 | } |
| 476 | case Instruction::SExt: |
| 477 | return Search(I->getOperand(0), BB, R); |
| 478 | } |
| 479 | return false; |
| 480 | } |
| 481 | |
| 482 | // The pass needs to identify integer add/sub reductions of 16-bit vector |
Sam Parker | 85ad78b | 2019-07-11 07:47:50 +0000 | [diff] [blame] | 483 | // multiplications. |
| 484 | // To use SMLAD: |
| 485 | // 1) we first need to find integer add then look for this pattern: |
| 486 | // |
| 487 | // acc0 = ... |
| 488 | // ld0 = load i16 |
| 489 | // sext0 = sext i16 %ld0 to i32 |
| 490 | // ld1 = load i16 |
| 491 | // sext1 = sext i16 %ld1 to i32 |
| 492 | // mul0 = mul %sext0, %sext1 |
| 493 | // ld2 = load i16 |
| 494 | // sext2 = sext i16 %ld2 to i32 |
| 495 | // ld3 = load i16 |
| 496 | // sext3 = sext i16 %ld3 to i32 |
| 497 | // mul1 = mul i32 %sext2, %sext3 |
| 498 | // add0 = add i32 %mul0, %acc0 |
| 499 | // acc1 = add i32 %add0, %mul1 |
| 500 | // |
| 501 | // Which can be selected to: |
| 502 | // |
| 503 | // ldr r0 |
| 504 | // ldr r1 |
| 505 | // smlad r2, r0, r1, r2 |
| 506 | // |
| 507 | // If constants are used instead of loads, these will need to be hoisted |
| 508 | // out and into a register. |
| 509 | // |
| 510 | // If loop invariants are used instead of loads, these need to be packed |
| 511 | // before the loop begins. |
| 512 | // |
Sam Parker | a761ba0 | 2019-08-28 08:51:13 +0000 | [diff] [blame] | 513 | bool ARMParallelDSP::MatchSMLAD(Function &F) { |
Sam Parker | 85ad78b | 2019-07-11 07:47:50 +0000 | [diff] [blame] | 514 | bool Changed = false; |
Sam Parker | 85ad78b | 2019-07-11 07:47:50 +0000 | [diff] [blame] | 515 | |
Sam Parker | a761ba0 | 2019-08-28 08:51:13 +0000 | [diff] [blame] | 516 | for (auto &BB : F) { |
| 517 | SmallPtrSet<Instruction*, 4> AllAdds; |
| 518 | if (!RecordMemoryOps(&BB)) |
Sam Parker | 85ad78b | 2019-07-11 07:47:50 +0000 | [diff] [blame] | 519 | continue; |
| 520 | |
Sam Parker | a761ba0 | 2019-08-28 08:51:13 +0000 | [diff] [blame] | 521 | for (Instruction &I : reverse(BB)) { |
| 522 | if (I.getOpcode() != Instruction::Add) |
| 523 | continue; |
Sam Parker | 85ad78b | 2019-07-11 07:47:50 +0000 | [diff] [blame] | 524 | |
Sam Parker | a761ba0 | 2019-08-28 08:51:13 +0000 | [diff] [blame] | 525 | if (AllAdds.count(&I)) |
| 526 | continue; |
Sam Parker | 85ad78b | 2019-07-11 07:47:50 +0000 | [diff] [blame] | 527 | |
Sam Parker | a761ba0 | 2019-08-28 08:51:13 +0000 | [diff] [blame] | 528 | const auto *Ty = I.getType(); |
| 529 | if (!Ty->isIntegerTy(32) && !Ty->isIntegerTy(64)) |
| 530 | continue; |
Sam Parker | 85ad78b | 2019-07-11 07:47:50 +0000 | [diff] [blame] | 531 | |
Sam Parker | a761ba0 | 2019-08-28 08:51:13 +0000 | [diff] [blame] | 532 | Reduction R(&I); |
| 533 | if (!Search(&I, &BB, R)) |
| 534 | continue; |
Sam Parker | 85ad78b | 2019-07-11 07:47:50 +0000 | [diff] [blame] | 535 | |
Sam Parker | a761ba0 | 2019-08-28 08:51:13 +0000 | [diff] [blame] | 536 | R.InsertMuls(); |
| 537 | LLVM_DEBUG(dbgs() << "After search, Reduction:\n"; R.dump()); |
| 538 | |
| 539 | if (!CreateParallelPairs(R)) |
| 540 | continue; |
| 541 | |
| 542 | InsertParallelMACs(R); |
| 543 | Changed = true; |
| 544 | AllAdds.insert(R.getAdds().begin(), R.getAdds().end()); |
| 545 | } |
Sam Parker | 85ad78b | 2019-07-11 07:47:50 +0000 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | return Changed; |
| 549 | } |
| 550 | |
| 551 | bool ARMParallelDSP::CreateParallelPairs(Reduction &R) { |
| 552 | |
| 553 | // Not enough mul operations to make a pair. |
| 554 | if (R.getMuls().size() < 2) |
| 555 | return false; |
| 556 | |
| 557 | // Check that the muls operate directly upon sign extended loads. |
Sam Parker | 414dd1c | 2019-07-29 08:41:51 +0000 | [diff] [blame] | 558 | for (auto &MulCand : R.getMuls()) { |
| 559 | if (!MulCand->HasTwoLoadInputs()) |
Sam Parker | 85ad78b | 2019-07-11 07:47:50 +0000 | [diff] [blame] | 560 | return false; |
Sam Parker | 85ad78b | 2019-07-11 07:47:50 +0000 | [diff] [blame] | 561 | } |
| 562 | |
Sam Parker | 414dd1c | 2019-07-29 08:41:51 +0000 | [diff] [blame] | 563 | auto CanPair = [&](Reduction &R, MulCandidate *PMul0, MulCandidate *PMul1) { |
Sam Parker | 453ba91 | 2018-11-09 09:18:00 +0000 | [diff] [blame] | 564 | // The first elements of each vector should be loads with sexts. If we |
| 565 | // find that its two pairs of consecutive loads, then these can be |
| 566 | // transformed into two wider loads and the users can be replaced with |
| 567 | // DSP intrinsics. |
Sam Parker | 414dd1c | 2019-07-29 08:41:51 +0000 | [diff] [blame] | 568 | auto Ld0 = static_cast<LoadInst*>(PMul0->LHS); |
| 569 | auto Ld1 = static_cast<LoadInst*>(PMul1->LHS); |
| 570 | auto Ld2 = static_cast<LoadInst*>(PMul0->RHS); |
| 571 | auto Ld3 = static_cast<LoadInst*>(PMul1->RHS); |
Sam Parker | 453ba91 | 2018-11-09 09:18:00 +0000 | [diff] [blame] | 572 | |
Sam Parker | 414dd1c | 2019-07-29 08:41:51 +0000 | [diff] [blame] | 573 | if (AreSequentialLoads(Ld0, Ld1, PMul0->VecLd)) { |
| 574 | if (AreSequentialLoads(Ld2, Ld3, PMul1->VecLd)) { |
Sam Parker | 453ba91 | 2018-11-09 09:18:00 +0000 | [diff] [blame] | 575 | LLVM_DEBUG(dbgs() << "OK: found two pairs of parallel loads!\n"); |
Sam Parker | 414dd1c | 2019-07-29 08:41:51 +0000 | [diff] [blame] | 576 | R.AddMulPair(PMul0, PMul1); |
| 577 | return true; |
| 578 | } else if (AreSequentialLoads(Ld3, Ld2, PMul1->VecLd)) { |
| 579 | LLVM_DEBUG(dbgs() << "OK: found two pairs of parallel loads!\n"); |
| 580 | LLVM_DEBUG(dbgs() << " exchanging Ld2 and Ld3\n"); |
Sam Parker | a761ba0 | 2019-08-28 08:51:13 +0000 | [diff] [blame] | 581 | R.AddMulPair(PMul0, PMul1, true); |
Sam Parker | 453ba91 | 2018-11-09 09:18:00 +0000 | [diff] [blame] | 582 | return true; |
| 583 | } |
Sam Parker | 414dd1c | 2019-07-29 08:41:51 +0000 | [diff] [blame] | 584 | } else if (AreSequentialLoads(Ld1, Ld0, PMul0->VecLd) && |
| 585 | AreSequentialLoads(Ld2, Ld3, PMul1->VecLd)) { |
| 586 | LLVM_DEBUG(dbgs() << "OK: found two pairs of parallel loads!\n"); |
| 587 | LLVM_DEBUG(dbgs() << " exchanging Ld0 and Ld1\n"); |
| 588 | LLVM_DEBUG(dbgs() << " and swapping muls\n"); |
Sam Parker | 414dd1c | 2019-07-29 08:41:51 +0000 | [diff] [blame] | 589 | // Only the second operand can be exchanged, so swap the muls. |
Sam Parker | a761ba0 | 2019-08-28 08:51:13 +0000 | [diff] [blame] | 590 | R.AddMulPair(PMul1, PMul0, true); |
Sam Parker | 414dd1c | 2019-07-29 08:41:51 +0000 | [diff] [blame] | 591 | return true; |
Sam Parker | 453ba91 | 2018-11-09 09:18:00 +0000 | [diff] [blame] | 592 | } |
| 593 | return false; |
| 594 | }; |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 595 | |
Sam Parker | 414dd1c | 2019-07-29 08:41:51 +0000 | [diff] [blame] | 596 | MulCandList &Muls = R.getMuls(); |
Sam Parker | 85ad78b | 2019-07-11 07:47:50 +0000 | [diff] [blame] | 597 | const unsigned Elems = Muls.size(); |
Sam Parker | a023c7a | 2018-09-12 09:17:44 +0000 | [diff] [blame] | 598 | for (unsigned i = 0; i < Elems; ++i) { |
Sam Parker | 414dd1c | 2019-07-29 08:41:51 +0000 | [diff] [blame] | 599 | MulCandidate *PMul0 = static_cast<MulCandidate*>(Muls[i].get()); |
Sam Parker | a761ba0 | 2019-08-28 08:51:13 +0000 | [diff] [blame] | 600 | if (PMul0->Paired) |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 601 | continue; |
| 602 | |
Sam Parker | a023c7a | 2018-09-12 09:17:44 +0000 | [diff] [blame] | 603 | for (unsigned j = 0; j < Elems; ++j) { |
| 604 | if (i == j) |
| 605 | continue; |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 606 | |
Sam Parker | 414dd1c | 2019-07-29 08:41:51 +0000 | [diff] [blame] | 607 | MulCandidate *PMul1 = static_cast<MulCandidate*>(Muls[j].get()); |
Sam Parker | a761ba0 | 2019-08-28 08:51:13 +0000 | [diff] [blame] | 608 | if (PMul1->Paired) |
Sam Parker | a023c7a | 2018-09-12 09:17:44 +0000 | [diff] [blame] | 609 | continue; |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 610 | |
Sam Parker | a023c7a | 2018-09-12 09:17:44 +0000 | [diff] [blame] | 611 | const Instruction *Mul0 = PMul0->Root; |
| 612 | const Instruction *Mul1 = PMul1->Root; |
| 613 | if (Mul0 == Mul1) |
| 614 | continue; |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 615 | |
Sam Parker | a023c7a | 2018-09-12 09:17:44 +0000 | [diff] [blame] | 616 | assert(PMul0 != PMul1 && "expected different chains"); |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 617 | |
Sam Parker | a761ba0 | 2019-08-28 08:51:13 +0000 | [diff] [blame] | 618 | if (CanPair(R, PMul0, PMul1)) |
Sam Parker | a023c7a | 2018-09-12 09:17:44 +0000 | [diff] [blame] | 619 | break; |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 620 | } |
| 621 | } |
Sam Parker | 85ad78b | 2019-07-11 07:47:50 +0000 | [diff] [blame] | 622 | return !R.getMulPairs().empty(); |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 623 | } |
| 624 | |
Sam Parker | 85ad78b | 2019-07-11 07:47:50 +0000 | [diff] [blame] | 625 | void ARMParallelDSP::InsertParallelMACs(Reduction &R) { |
| 626 | |
Sam Parker | 7ca8c6f | 2019-08-01 08:17:51 +0000 | [diff] [blame] | 627 | auto CreateSMLAD = [&](LoadInst* WideLd0, LoadInst *WideLd1, |
| 628 | Value *Acc, bool Exchange, |
| 629 | Instruction *InsertAfter) { |
Sam Parker | 85ad78b | 2019-07-11 07:47:50 +0000 | [diff] [blame] | 630 | // Replace the reduction chain with an intrinsic call |
Sam Parker | 85ad78b | 2019-07-11 07:47:50 +0000 | [diff] [blame] | 631 | |
| 632 | Value* Args[] = { WideLd0, WideLd1, Acc }; |
| 633 | Function *SMLAD = nullptr; |
| 634 | if (Exchange) |
| 635 | SMLAD = Acc->getType()->isIntegerTy(32) ? |
| 636 | Intrinsic::getDeclaration(M, Intrinsic::arm_smladx) : |
| 637 | Intrinsic::getDeclaration(M, Intrinsic::arm_smlaldx); |
| 638 | else |
| 639 | SMLAD = Acc->getType()->isIntegerTy(32) ? |
| 640 | Intrinsic::getDeclaration(M, Intrinsic::arm_smlad) : |
| 641 | Intrinsic::getDeclaration(M, Intrinsic::arm_smlald); |
| 642 | |
| 643 | IRBuilder<NoFolder> Builder(InsertAfter->getParent(), |
Sam Parker | 1c3ca61 | 2019-10-16 09:37:03 +0000 | [diff] [blame^] | 644 | BasicBlock::iterator(InsertAfter)); |
Sam Parker | 85ad78b | 2019-07-11 07:47:50 +0000 | [diff] [blame] | 645 | Instruction *Call = Builder.CreateCall(SMLAD, Args); |
| 646 | NumSMLAD++; |
| 647 | return Call; |
| 648 | }; |
| 649 | |
Sam Parker | 1c3ca61 | 2019-10-16 09:37:03 +0000 | [diff] [blame^] | 650 | // Return the instruction after the dominated instruction. |
| 651 | auto GetInsertPoint = [this](Value *A, Value *B) { |
| 652 | assert((isa<Instruction>(A) || isa<Instruction>(B)) && |
| 653 | "expected at least one instruction"); |
| 654 | |
| 655 | Value *V = nullptr; |
| 656 | if (!isa<Instruction>(A)) |
| 657 | V = B; |
| 658 | else if (!isa<Instruction>(B)) |
| 659 | V = A; |
| 660 | else |
| 661 | V = DT->dominates(cast<Instruction>(A), cast<Instruction>(B)) ? B : A; |
| 662 | |
| 663 | return &*++BasicBlock::iterator(cast<Instruction>(V)); |
| 664 | }; |
| 665 | |
Sam Parker | 85ad78b | 2019-07-11 07:47:50 +0000 | [diff] [blame] | 666 | Value *Acc = R.getAccumulator(); |
Sam Parker | a761ba0 | 2019-08-28 08:51:13 +0000 | [diff] [blame] | 667 | |
| 668 | // For any muls that were discovered but not paired, accumulate their values |
| 669 | // as before. |
Sam Parker | 1c3ca61 | 2019-10-16 09:37:03 +0000 | [diff] [blame^] | 670 | IRBuilder<NoFolder> Builder(R.getRoot()->getParent()); |
Sam Parker | a761ba0 | 2019-08-28 08:51:13 +0000 | [diff] [blame] | 671 | MulCandList &MulCands = R.getMuls(); |
| 672 | for (auto &MulCand : MulCands) { |
| 673 | if (MulCand->Paired) |
| 674 | continue; |
| 675 | |
Sam Parker | 1c3ca61 | 2019-10-16 09:37:03 +0000 | [diff] [blame^] | 676 | Instruction *Mul = cast<Instruction>(MulCand->Root); |
Sam Parker | fea5322 | 2019-09-04 08:41:34 +0000 | [diff] [blame] | 677 | LLVM_DEBUG(dbgs() << "Accumulating unpaired mul: " << *Mul << "\n"); |
| 678 | |
Sam Parker | c363deb | 2019-09-09 08:39:14 +0000 | [diff] [blame] | 679 | if (R.getType() != Mul->getType()) { |
Sam Parker | fea5322 | 2019-09-04 08:41:34 +0000 | [diff] [blame] | 680 | assert(R.is64Bit() && "expected 64-bit result"); |
Sam Parker | 1c3ca61 | 2019-10-16 09:37:03 +0000 | [diff] [blame^] | 681 | Builder.SetInsertPoint(&*++BasicBlock::iterator(Mul)); |
| 682 | Mul = cast<Instruction>(Builder.CreateSExt(Mul, R.getRoot()->getType())); |
Sam Parker | fea5322 | 2019-09-04 08:41:34 +0000 | [diff] [blame] | 683 | } |
| 684 | |
Sam Parker | a761ba0 | 2019-08-28 08:51:13 +0000 | [diff] [blame] | 685 | if (!Acc) { |
Sam Parker | fea5322 | 2019-09-04 08:41:34 +0000 | [diff] [blame] | 686 | Acc = Mul; |
Sam Parker | a761ba0 | 2019-08-28 08:51:13 +0000 | [diff] [blame] | 687 | continue; |
| 688 | } |
Sam Parker | fea5322 | 2019-09-04 08:41:34 +0000 | [diff] [blame] | 689 | |
Sam Parker | 1c3ca61 | 2019-10-16 09:37:03 +0000 | [diff] [blame^] | 690 | // If Acc is the original incoming value to the reduction, it could be a |
| 691 | // phi. But the phi will dominate Mul, meaning that Mul will be the |
| 692 | // insertion point. |
| 693 | Builder.SetInsertPoint(GetInsertPoint(Mul, Acc)); |
Sam Parker | fea5322 | 2019-09-04 08:41:34 +0000 | [diff] [blame] | 694 | Acc = Builder.CreateAdd(Mul, Acc); |
Sam Parker | a761ba0 | 2019-08-28 08:51:13 +0000 | [diff] [blame] | 695 | } |
| 696 | |
Sam Parker | c363deb | 2019-09-09 08:39:14 +0000 | [diff] [blame] | 697 | if (!Acc) { |
Sam Parker | fea5322 | 2019-09-04 08:41:34 +0000 | [diff] [blame] | 698 | Acc = R.is64Bit() ? |
| 699 | ConstantInt::get(IntegerType::get(M->getContext(), 64), 0) : |
| 700 | ConstantInt::get(IntegerType::get(M->getContext(), 32), 0); |
Sam Parker | c363deb | 2019-09-09 08:39:14 +0000 | [diff] [blame] | 701 | } else if (Acc->getType() != R.getType()) { |
| 702 | Builder.SetInsertPoint(R.getRoot()); |
| 703 | Acc = Builder.CreateSExt(Acc, R.getType()); |
| 704 | } |
Sam Parker | 85ad78b | 2019-07-11 07:47:50 +0000 | [diff] [blame] | 705 | |
Sam Parker | 1c3ca61 | 2019-10-16 09:37:03 +0000 | [diff] [blame^] | 706 | // Roughly sort the mul pairs in their program order. |
| 707 | OrderedBasicBlock OrderedBB(R.getRoot()->getParent()); |
| 708 | llvm::sort(R.getMulPairs(), [&OrderedBB](auto &PairA, auto &PairB) { |
| 709 | const Instruction *A = PairA.first->Root; |
| 710 | const Instruction *B = PairB.first->Root; |
| 711 | return OrderedBB.dominates(A, B); |
| 712 | }); |
| 713 | |
Sam Parker | 7ca8c6f | 2019-08-01 08:17:51 +0000 | [diff] [blame] | 714 | IntegerType *Ty = IntegerType::get(M->getContext(), 32); |
Sam Parker | 85ad78b | 2019-07-11 07:47:50 +0000 | [diff] [blame] | 715 | for (auto &Pair : R.getMulPairs()) { |
Sam Parker | 7ca8c6f | 2019-08-01 08:17:51 +0000 | [diff] [blame] | 716 | MulCandidate *LHSMul = Pair.first; |
| 717 | MulCandidate *RHSMul = Pair.second; |
Sam Parker | 7ca8c6f | 2019-08-01 08:17:51 +0000 | [diff] [blame] | 718 | LoadInst *BaseLHS = LHSMul->getBaseLoad(); |
| 719 | LoadInst *BaseRHS = RHSMul->getBaseLoad(); |
| 720 | LoadInst *WideLHS = WideLoads.count(BaseLHS) ? |
| 721 | WideLoads[BaseLHS]->getLoad() : CreateWideLoad(LHSMul->VecLd, Ty); |
| 722 | LoadInst *WideRHS = WideLoads.count(BaseRHS) ? |
| 723 | WideLoads[BaseRHS]->getLoad() : CreateWideLoad(RHSMul->VecLd, Ty); |
Sam Parker | a023c7a | 2018-09-12 09:17:44 +0000 | [diff] [blame] | 724 | |
Sam Parker | 1c3ca61 | 2019-10-16 09:37:03 +0000 | [diff] [blame^] | 725 | Instruction *InsertAfter = GetInsertPoint(WideLHS, WideRHS); |
| 726 | InsertAfter = GetInsertPoint(InsertAfter, Acc); |
Sam Parker | 7ca8c6f | 2019-08-01 08:17:51 +0000 | [diff] [blame] | 727 | Acc = CreateSMLAD(WideLHS, WideRHS, Acc, RHSMul->Exchange, InsertAfter); |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 728 | } |
Sam Parker | 85ad78b | 2019-07-11 07:47:50 +0000 | [diff] [blame] | 729 | R.UpdateRoot(cast<Instruction>(Acc)); |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 730 | } |
| 731 | |
Sam Parker | cd38599 | 2019-08-02 08:21:17 +0000 | [diff] [blame] | 732 | LoadInst* ARMParallelDSP::CreateWideLoad(MemInstList &Loads, |
Sam Parker | a33e311 | 2019-05-13 09:23:32 +0000 | [diff] [blame] | 733 | IntegerType *LoadTy) { |
Sam Parker | 4c4ff13 | 2019-03-14 11:14:13 +0000 | [diff] [blame] | 734 | assert(Loads.size() == 2 && "currently only support widening two loads"); |
Sam Parker | a33e311 | 2019-05-13 09:23:32 +0000 | [diff] [blame] | 735 | |
| 736 | LoadInst *Base = Loads[0]; |
| 737 | LoadInst *Offset = Loads[1]; |
| 738 | |
| 739 | Instruction *BaseSExt = dyn_cast<SExtInst>(Base->user_back()); |
| 740 | Instruction *OffsetSExt = dyn_cast<SExtInst>(Offset->user_back()); |
| 741 | |
| 742 | assert((BaseSExt && OffsetSExt) |
| 743 | && "Loads should have a single, extending, user"); |
| 744 | |
| 745 | std::function<void(Value*, Value*)> MoveBefore = |
| 746 | [&](Value *A, Value *B) -> void { |
| 747 | if (!isa<Instruction>(A) || !isa<Instruction>(B)) |
| 748 | return; |
| 749 | |
| 750 | auto *Source = cast<Instruction>(A); |
| 751 | auto *Sink = cast<Instruction>(B); |
| 752 | |
| 753 | if (DT->dominates(Source, Sink) || |
| 754 | Source->getParent() != Sink->getParent() || |
| 755 | isa<PHINode>(Source) || isa<PHINode>(Sink)) |
| 756 | return; |
| 757 | |
| 758 | Source->moveBefore(Sink); |
Sam Parker | aeb21b9 | 2019-07-24 09:38:39 +0000 | [diff] [blame] | 759 | for (auto &Op : Source->operands()) |
| 760 | MoveBefore(Op, Source); |
Sam Parker | a33e311 | 2019-05-13 09:23:32 +0000 | [diff] [blame] | 761 | }; |
| 762 | |
| 763 | // Insert the load at the point of the original dominating load. |
| 764 | LoadInst *DomLoad = DT->dominates(Base, Offset) ? Base : Offset; |
| 765 | IRBuilder<NoFolder> IRB(DomLoad->getParent(), |
| 766 | ++BasicBlock::iterator(DomLoad)); |
| 767 | |
| 768 | // Bitcast the pointer to a wider type and create the wide load, while making |
| 769 | // sure to maintain the original alignment as this prevents ldrd from being |
| 770 | // generated when it could be illegal due to memory alignment. |
| 771 | const unsigned AddrSpace = DomLoad->getPointerAddressSpace(); |
| 772 | Value *VecPtr = IRB.CreateBitCast(Base->getPointerOperand(), |
Eli Friedman | b09c778 | 2018-10-18 19:34:30 +0000 | [diff] [blame] | 773 | LoadTy->getPointerTo(AddrSpace)); |
Sam Parker | 4c4ff13 | 2019-03-14 11:14:13 +0000 | [diff] [blame] | 774 | LoadInst *WideLoad = IRB.CreateAlignedLoad(LoadTy, VecPtr, |
Sam Parker | a33e311 | 2019-05-13 09:23:32 +0000 | [diff] [blame] | 775 | Base->getAlignment()); |
Sam Parker | 4c4ff13 | 2019-03-14 11:14:13 +0000 | [diff] [blame] | 776 | |
Sam Parker | a33e311 | 2019-05-13 09:23:32 +0000 | [diff] [blame] | 777 | // Make sure everything is in the correct order in the basic block. |
| 778 | MoveBefore(Base->getPointerOperand(), VecPtr); |
| 779 | MoveBefore(VecPtr, WideLoad); |
Sam Parker | 4c4ff13 | 2019-03-14 11:14:13 +0000 | [diff] [blame] | 780 | |
| 781 | // From the wide load, create two values that equal the original two loads. |
Sam Parker | a33e311 | 2019-05-13 09:23:32 +0000 | [diff] [blame] | 782 | // Loads[0] needs trunc while Loads[1] needs a lshr and trunc. |
| 783 | // TODO: Support big-endian as well. |
| 784 | Value *Bottom = IRB.CreateTrunc(WideLoad, Base->getType()); |
Sam Parker | a761ba0 | 2019-08-28 08:51:13 +0000 | [diff] [blame] | 785 | Value *NewBaseSExt = IRB.CreateSExt(Bottom, BaseSExt->getType()); |
| 786 | BaseSExt->replaceAllUsesWith(NewBaseSExt); |
Sam Parker | 4c4ff13 | 2019-03-14 11:14:13 +0000 | [diff] [blame] | 787 | |
Sam Parker | a33e311 | 2019-05-13 09:23:32 +0000 | [diff] [blame] | 788 | IntegerType *OffsetTy = cast<IntegerType>(Offset->getType()); |
| 789 | Value *ShiftVal = ConstantInt::get(LoadTy, OffsetTy->getBitWidth()); |
Sam Parker | 4c4ff13 | 2019-03-14 11:14:13 +0000 | [diff] [blame] | 790 | Value *Top = IRB.CreateLShr(WideLoad, ShiftVal); |
Sam Parker | a33e311 | 2019-05-13 09:23:32 +0000 | [diff] [blame] | 791 | Value *Trunc = IRB.CreateTrunc(Top, OffsetTy); |
Sam Parker | a761ba0 | 2019-08-28 08:51:13 +0000 | [diff] [blame] | 792 | Value *NewOffsetSExt = IRB.CreateSExt(Trunc, OffsetSExt->getType()); |
| 793 | OffsetSExt->replaceAllUsesWith(NewOffsetSExt); |
Sam Parker | 4c4ff13 | 2019-03-14 11:14:13 +0000 | [diff] [blame] | 794 | |
Sam Parker | a761ba0 | 2019-08-28 08:51:13 +0000 | [diff] [blame] | 795 | LLVM_DEBUG(dbgs() << "From Base and Offset:\n" |
| 796 | << *Base << "\n" << *Offset << "\n" |
| 797 | << "Created Wide Load:\n" |
| 798 | << *WideLoad << "\n" |
| 799 | << *Bottom << "\n" |
| 800 | << *NewBaseSExt << "\n" |
| 801 | << *Top << "\n" |
| 802 | << *Trunc << "\n" |
| 803 | << *NewOffsetSExt << "\n"); |
Sam Parker | a33e311 | 2019-05-13 09:23:32 +0000 | [diff] [blame] | 804 | WideLoads.emplace(std::make_pair(Base, |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 805 | std::make_unique<WidenedLoad>(Loads, WideLoad))); |
Sam Parker | 4c4ff13 | 2019-03-14 11:14:13 +0000 | [diff] [blame] | 806 | return WideLoad; |
Eli Friedman | b09c778 | 2018-10-18 19:34:30 +0000 | [diff] [blame] | 807 | } |
| 808 | |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 809 | Pass *llvm::createARMParallelDSPPass() { |
| 810 | return new ARMParallelDSP(); |
| 811 | } |
| 812 | |
| 813 | char ARMParallelDSP::ID = 0; |
| 814 | |
Sjoerd Meijer | b3e06fa | 2018-07-06 14:47:09 +0000 | [diff] [blame] | 815 | INITIALIZE_PASS_BEGIN(ARMParallelDSP, "arm-parallel-dsp", |
Sam Parker | a761ba0 | 2019-08-28 08:51:13 +0000 | [diff] [blame] | 816 | "Transform functions to use DSP intrinsics", false, false) |
Sjoerd Meijer | b3e06fa | 2018-07-06 14:47:09 +0000 | [diff] [blame] | 817 | INITIALIZE_PASS_END(ARMParallelDSP, "arm-parallel-dsp", |
Sam Parker | a761ba0 | 2019-08-28 08:51:13 +0000 | [diff] [blame] | 818 | "Transform functions to use DSP intrinsics", false, false) |