blob: 8bda733c50c2f8f208b7d4ddf1c63d6b8d91ff1f [file] [log] [blame]
Sam Parkera761ba02019-08-28 08:51:13 +00001//===- ARMParallelDSP.cpp - Parallel DSP Pass -----------------------------===//
Sjoerd Meijerc89ca552018-06-28 12:55:29 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Meijerc89ca552018-06-28 12:55:29 +00006//
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 Meijer53449da2018-07-11 12:36:25 +000013/// This pass runs only when unaligned accesses is supported/enabled.
Sjoerd Meijerc89ca552018-06-28 12:55:29 +000014//
15//===----------------------------------------------------------------------===//
16
Sjoerd Meijerb3e06fa2018-07-06 14:47:09 +000017#include "llvm/ADT/Statistic.h"
Sjoerd Meijerc89ca552018-06-28 12:55:29 +000018#include "llvm/ADT/SmallPtrSet.h"
19#include "llvm/Analysis/AliasAnalysis.h"
20#include "llvm/Analysis/LoopAccessAnalysis.h"
Sjoerd Meijerc89ca552018-06-28 12:55:29 +000021#include "llvm/IR/Instructions.h"
22#include "llvm/IR/NoFolder.h"
23#include "llvm/Transforms/Scalar.h"
24#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Sjoerd Meijerc89ca552018-06-28 12:55:29 +000025#include "llvm/Pass.h"
26#include "llvm/PassRegistry.h"
27#include "llvm/PassSupport.h"
28#include "llvm/Support/Debug.h"
29#include "llvm/IR/PatternMatch.h"
30#include "llvm/CodeGen/TargetPassConfig.h"
31#include "ARM.h"
32#include "ARMSubtarget.h"
33
34using namespace llvm;
35using namespace PatternMatch;
36
Sjoerd Meijerb3e06fa2018-07-06 14:47:09 +000037#define DEBUG_TYPE "arm-parallel-dsp"
38
39STATISTIC(NumSMLAD , "Number of smlad instructions generated");
Sjoerd Meijerc89ca552018-06-28 12:55:29 +000040
Sjoerd Meijer3c859b32018-08-14 07:43:49 +000041static cl::opt<bool>
42DisableParallelDSP("disable-arm-parallel-dsp", cl::Hidden, cl::init(false),
43 cl::desc("Disable the ARM Parallel DSP pass"));
44
Sjoerd Meijerc89ca552018-06-28 12:55:29 +000045namespace {
Sam Parker414dd1c2019-07-29 08:41:51 +000046 struct MulCandidate;
Sam Parker85ad78b2019-07-11 07:47:50 +000047 class Reduction;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +000048
Sam Parkercd385992019-08-02 08:21:17 +000049 using MulCandList = SmallVector<std::unique_ptr<MulCandidate>, 8>;
50 using MemInstList = SmallVectorImpl<LoadInst*>;
51 using MulPairList = SmallVector<std::pair<MulCandidate*, MulCandidate*>, 8>;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +000052
Sam Parker414dd1c2019-07-29 08:41:51 +000053 // 'MulCandidate' holds the multiplication instructions that are candidates
Sam Parker3da59e52019-07-26 14:11:40 +000054 // for parallel execution.
Sam Parker414dd1c2019-07-29 08:41:51 +000055 struct MulCandidate {
Sam Parker89a37992018-07-23 15:25:59 +000056 Instruction *Root;
Sam Parker414dd1c2019-07-29 08:41:51 +000057 Value* LHS;
58 Value* RHS;
Sam Parker3da59e52019-07-26 14:11:40 +000059 bool Exchange = false;
Sam Parker89a37992018-07-23 15:25:59 +000060 bool ReadOnly = true;
Sam Parkera761ba02019-08-28 08:51:13 +000061 bool Paired = false;
Sam Parkercd385992019-08-02 08:21:17 +000062 SmallVector<LoadInst*, 2> VecLd; // Container for loads to widen.
Sam Parker89a37992018-07-23 15:25:59 +000063
Sam Parker14c6dfd2019-08-02 07:32:28 +000064 MulCandidate(Instruction *I, Value *lhs, Value *rhs) :
65 Root(I), LHS(lhs), RHS(rhs) { }
Sam Parker89a37992018-07-23 15:25:59 +000066
Sam Parker414dd1c2019-07-29 08:41:51 +000067 bool HasTwoLoadInputs() const {
68 return isa<LoadInst>(LHS) && isa<LoadInst>(RHS);
69 }
Sam Parker7ca8c6f2019-08-01 08:17:51 +000070
71 LoadInst *getBaseLoad() const {
Sam Parkera761ba02019-08-28 08:51:13 +000072 return VecLd.front();
Sam Parker7ca8c6f2019-08-01 08:17:51 +000073 }
Sjoerd Meijerc89ca552018-06-28 12:55:29 +000074 };
75
Sam Parker85ad78b2019-07-11 07:47:50 +000076 /// Represent a sequence of multiply-accumulate operations with the aim to
77 /// perform the multiplications in parallel.
78 class Reduction {
79 Instruction *Root = nullptr;
80 Value *Acc = nullptr;
Sam Parker414dd1c2019-07-29 08:41:51 +000081 MulCandList Muls;
Sam Parkercd385992019-08-02 08:21:17 +000082 MulPairList MulPairs;
Sam Parkera761ba02019-08-28 08:51:13 +000083 SetVector<Instruction*> Adds;
Sam Parker85ad78b2019-07-11 07:47:50 +000084
85 public:
86 Reduction() = delete;
87
88 Reduction (Instruction *Add) : Root(Add) { }
89
90 /// Record an Add instruction that is a part of the this reduction.
91 void InsertAdd(Instruction *I) { Adds.insert(I); }
92
Sam Parkera761ba02019-08-28 08:51:13 +000093 /// Create MulCandidates, each rooted at a Mul instruction, that is a part
94 /// of this reduction.
95 void InsertMuls() {
96 auto GetMulOperand = [](Value *V) -> Instruction* {
97 if (auto *SExt = dyn_cast<SExtInst>(V)) {
98 if (auto *I = dyn_cast<Instruction>(SExt->getOperand(0)))
99 if (I->getOpcode() == Instruction::Mul)
100 return I;
101 } else if (auto *I = dyn_cast<Instruction>(V)) {
102 if (I->getOpcode() == Instruction::Mul)
103 return I;
104 }
105 return nullptr;
106 };
107
108 auto InsertMul = [this](Instruction *I) {
109 Value *LHS = cast<Instruction>(I->getOperand(0))->getOperand(0);
110 Value *RHS = cast<Instruction>(I->getOperand(1))->getOperand(0);
111 Muls.push_back(std::make_unique<MulCandidate>(I, LHS, RHS));
112 };
113
114 for (auto *Add : Adds) {
115 if (Add == Acc)
116 continue;
117 if (auto *Mul = GetMulOperand(Add->getOperand(0)))
118 InsertMul(Mul);
119 if (auto *Mul = GetMulOperand(Add->getOperand(1)))
120 InsertMul(Mul);
121 }
Sam Parker85ad78b2019-07-11 07:47:50 +0000122 }
123
124 /// Add the incoming accumulator value, returns true if a value had not
125 /// already been added. Returning false signals to the user that this
126 /// reduction already has a value to initialise the accumulator.
127 bool InsertAcc(Value *V) {
128 if (Acc)
129 return false;
130 Acc = V;
131 return true;
132 }
133
Sam Parker414dd1c2019-07-29 08:41:51 +0000134 /// Set two MulCandidates, rooted at muls, that can be executed as a single
Sam Parker85ad78b2019-07-11 07:47:50 +0000135 /// parallel operation.
Sam Parkera761ba02019-08-28 08:51:13 +0000136 void AddMulPair(MulCandidate *Mul0, MulCandidate *Mul1,
137 bool Exchange = false) {
138 LLVM_DEBUG(dbgs() << "Pairing:\n"
139 << *Mul0->Root << "\n"
140 << *Mul1->Root << "\n");
141 Mul0->Paired = true;
142 Mul1->Paired = true;
143 if (Exchange)
144 Mul1->Exchange = true;
Sam Parker85ad78b2019-07-11 07:47:50 +0000145 MulPairs.push_back(std::make_pair(Mul0, Mul1));
146 }
147
148 /// Return true if enough mul operations are found that can be executed in
149 /// parallel.
150 bool CreateParallelPairs();
151
152 /// Return the add instruction which is the root of the reduction.
153 Instruction *getRoot() { return Root; }
154
Sam Parker7ca8c6f2019-08-01 08:17:51 +0000155 bool is64Bit() const { return Root->getType()->isIntegerTy(64); }
156
Sam Parkerc363deb2019-09-09 08:39:14 +0000157 Type *getType() const { return Root->getType(); }
158
Sam Parker85ad78b2019-07-11 07:47:50 +0000159 /// Return the incoming value to be accumulated. This maybe null.
160 Value *getAccumulator() { return Acc; }
161
162 /// Return the set of adds that comprise the reduction.
Sam Parkera761ba02019-08-28 08:51:13 +0000163 SetVector<Instruction*> &getAdds() { return Adds; }
Sam Parker85ad78b2019-07-11 07:47:50 +0000164
Sam Parker414dd1c2019-07-29 08:41:51 +0000165 /// Return the MulCandidate, rooted at mul instruction, that comprise the
Sam Parker85ad78b2019-07-11 07:47:50 +0000166 /// the reduction.
Sam Parker414dd1c2019-07-29 08:41:51 +0000167 MulCandList &getMuls() { return Muls; }
Sam Parker85ad78b2019-07-11 07:47:50 +0000168
Sam Parker414dd1c2019-07-29 08:41:51 +0000169 /// Return the MulCandidate, rooted at mul instructions, that have been
Sam Parker85ad78b2019-07-11 07:47:50 +0000170 /// paired for parallel execution.
Sam Parkercd385992019-08-02 08:21:17 +0000171 MulPairList &getMulPairs() { return MulPairs; }
Sam Parker85ad78b2019-07-11 07:47:50 +0000172
173 /// To finalise, replace the uses of the root with the intrinsic call.
174 void UpdateRoot(Instruction *SMLAD) {
175 Root->replaceAllUsesWith(SMLAD);
176 }
Sam Parkera761ba02019-08-28 08:51:13 +0000177
178 void dump() {
179 LLVM_DEBUG(dbgs() << "Reduction:\n";
180 for (auto *Add : Adds)
181 LLVM_DEBUG(dbgs() << *Add << "\n");
182 for (auto &Mul : Muls)
183 LLVM_DEBUG(dbgs() << *Mul->Root << "\n"
184 << " " << *Mul->LHS << "\n"
185 << " " << *Mul->RHS << "\n");
186 LLVM_DEBUG(if (Acc) dbgs() << "Acc in: " << *Acc << "\n")
187 );
188 }
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000189 };
190
Sam Parker4c4ff132019-03-14 11:14:13 +0000191 class WidenedLoad {
192 LoadInst *NewLd = nullptr;
193 SmallVector<LoadInst*, 4> Loads;
194
195 public:
196 WidenedLoad(SmallVectorImpl<LoadInst*> &Lds, LoadInst *Wide)
197 : NewLd(Wide) {
198 for (auto *I : Lds)
199 Loads.push_back(I);
200 }
201 LoadInst *getLoad() {
202 return NewLd;
203 }
204 };
205
Sam Parkera761ba02019-08-28 08:51:13 +0000206 class ARMParallelDSP : public FunctionPass {
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000207 ScalarEvolution *SE;
208 AliasAnalysis *AA;
209 TargetLibraryInfo *TLI;
210 DominatorTree *DT;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000211 const DataLayout *DL;
212 Module *M;
Sam Parker453ba912018-11-09 09:18:00 +0000213 std::map<LoadInst*, LoadInst*> LoadPairs;
Sam Parker85ad78b2019-07-11 07:47:50 +0000214 SmallPtrSet<LoadInst*, 4> OffsetLoads;
Sam Parker4c4ff132019-03-14 11:14:13 +0000215 std::map<LoadInst*, std::unique_ptr<WidenedLoad>> WideLoads;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000216
Sam Parker85ad78b2019-07-11 07:47:50 +0000217 template<unsigned>
Sam Parkera761ba02019-08-28 08:51:13 +0000218 bool IsNarrowSequence(Value *V);
219 bool Search(Value *V, BasicBlock *BB, Reduction &R);
Sam Parkera33e3112019-05-13 09:23:32 +0000220 bool RecordMemoryOps(BasicBlock *BB);
Sam Parker85ad78b2019-07-11 07:47:50 +0000221 void InsertParallelMACs(Reduction &Reduction);
Fangrui Song68169342018-07-03 19:12:27 +0000222 bool AreSequentialLoads(LoadInst *Ld0, LoadInst *Ld1, MemInstList &VecMem);
Sam Parkercd385992019-08-02 08:21:17 +0000223 LoadInst* CreateWideLoad(MemInstList &Loads, IntegerType *LoadTy);
Sam Parker85ad78b2019-07-11 07:47:50 +0000224 bool CreateParallelPairs(Reduction &R);
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000225
226 /// Try to match and generate: SMLAD, SMLADX - Signed Multiply Accumulate
227 /// Dual performs two signed 16x16-bit multiplications. It adds the
228 /// products to a 32-bit accumulate operand. Optionally, the instruction can
229 /// exchange the halfwords of the second operand before performing the
230 /// arithmetic.
Sam Parkera761ba02019-08-28 08:51:13 +0000231 bool MatchSMLAD(Function &F);
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000232
233 public:
234 static char ID;
235
Sam Parkera761ba02019-08-28 08:51:13 +0000236 ARMParallelDSP() : FunctionPass(ID) { }
Sam Parkera33e3112019-05-13 09:23:32 +0000237
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000238 void getAnalysisUsage(AnalysisUsage &AU) const override {
Sam Parkera761ba02019-08-28 08:51:13 +0000239 FunctionPass::getAnalysisUsage(AU);
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000240 AU.addRequired<AssumptionCacheTracker>();
241 AU.addRequired<ScalarEvolutionWrapperPass>();
242 AU.addRequired<AAResultsWrapperPass>();
243 AU.addRequired<TargetLibraryInfoWrapperPass>();
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000244 AU.addRequired<DominatorTreeWrapperPass>();
245 AU.addRequired<TargetPassConfig>();
Sam Parkera761ba02019-08-28 08:51:13 +0000246 AU.addPreserved<ScalarEvolutionWrapperPass>();
247 AU.addPreserved<GlobalsAAWrapperPass>();
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000248 AU.setPreservesCFG();
249 }
250
Sam Parkera761ba02019-08-28 08:51:13 +0000251 bool runOnFunction(Function &F) override {
Sjoerd Meijer3c859b32018-08-14 07:43:49 +0000252 if (DisableParallelDSP)
253 return false;
Sam Parkera761ba02019-08-28 08:51:13 +0000254 if (skipFunction(F))
Eli Friedmanb27fc952019-07-23 20:48:46 +0000255 return false;
256
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000257 SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
258 AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
Teresa Johnson9c27b592019-09-07 03:09:36 +0000259 TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000260 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000261 auto &TPC = getAnalysis<TargetPassConfig>();
262
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000263 M = F.getParent();
264 DL = &M->getDataLayout();
265
266 auto &TM = TPC.getTM<TargetMachine>();
267 auto *ST = &TM.getSubtarget<ARMSubtarget>(F);
268
269 if (!ST->allowsUnalignedMem()) {
270 LLVM_DEBUG(dbgs() << "Unaligned memory access not supported: not "
271 "running pass ARMParallelDSP\n");
272 return false;
273 }
274
275 if (!ST->hasDSP()) {
276 LLVM_DEBUG(dbgs() << "DSP extension not enabled: not running pass "
277 "ARMParallelDSP\n");
278 return false;
279 }
280
Sam Parker9e730202019-03-15 10:19:32 +0000281 if (!ST->isLittle()) {
282 LLVM_DEBUG(dbgs() << "Only supporting little endian: not running pass "
Sam Parkera33e3112019-05-13 09:23:32 +0000283 << "ARMParallelDSP\n");
Sam Parker9e730202019-03-15 10:19:32 +0000284 return false;
285 }
286
Sam Parkera023c7a2018-09-12 09:17:44 +0000287 LLVM_DEBUG(dbgs() << "\n== Parallel DSP pass ==\n");
288 LLVM_DEBUG(dbgs() << " - " << F.getName() << "\n\n");
Sam Parker453ba912018-11-09 09:18:00 +0000289
Sam Parkera761ba02019-08-28 08:51:13 +0000290 bool Changes = MatchSMLAD(F);
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000291 return Changes;
292 }
293 };
294}
295
Sam Parkerffc16812018-07-03 12:44:16 +0000296template<typename MemInst>
297static bool AreSequentialAccesses(MemInst *MemOp0, MemInst *MemOp1,
Sam Parker453ba912018-11-09 09:18:00 +0000298 const DataLayout &DL, ScalarEvolution &SE) {
Sam Parker4c4ff132019-03-14 11:14:13 +0000299 if (isConsecutiveAccess(MemOp0, MemOp1, DL, SE))
Sam Parkerffc16812018-07-03 12:44:16 +0000300 return true;
Sam Parkerffc16812018-07-03 12:44:16 +0000301 return false;
302}
303
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000304bool ARMParallelDSP::AreSequentialLoads(LoadInst *Ld0, LoadInst *Ld1,
Sam Parkerffc16812018-07-03 12:44:16 +0000305 MemInstList &VecMem) {
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000306 if (!Ld0 || !Ld1)
307 return false;
308
Sam Parker4c4ff132019-03-14 11:14:13 +0000309 if (!LoadPairs.count(Ld0) || LoadPairs[Ld0] != Ld1)
310 return false;
311
312 LLVM_DEBUG(dbgs() << "Loads are sequential and valid:\n";
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000313 dbgs() << "Ld0:"; Ld0->dump();
314 dbgs() << "Ld1:"; Ld1->dump();
315 );
316
Sam Parker453ba912018-11-09 09:18:00 +0000317 VecMem.clear();
318 VecMem.push_back(Ld0);
319 VecMem.push_back(Ld1);
320 return true;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000321}
322
Sam Parker85ad78b2019-07-11 07:47:50 +0000323// MaxBitwidth: the maximum supported bitwidth of the elements in the DSP
324// instructions, which is set to 16. So here we should collect all i8 and i16
325// narrow operations.
326// TODO: we currently only collect i16, and will support i8 later, so that's
327// why we check that types are equal to MaxBitWidth, and not <= MaxBitWidth.
328template<unsigned MaxBitWidth>
Sam Parkera761ba02019-08-28 08:51:13 +0000329bool ARMParallelDSP::IsNarrowSequence(Value *V) {
Sam Parker74400652019-07-26 10:57:42 +0000330 if (auto *SExt = dyn_cast<SExtInst>(V)) {
331 if (SExt->getSrcTy()->getIntegerBitWidth() != MaxBitWidth)
Sam Parker85ad78b2019-07-11 07:47:50 +0000332 return false;
333
Sam Parker74400652019-07-26 10:57:42 +0000334 if (auto *Ld = dyn_cast<LoadInst>(SExt->getOperand(0))) {
Sam Parkera761ba02019-08-28 08:51:13 +0000335 // Check that this load could be paired.
336 return LoadPairs.count(Ld) || OffsetLoads.count(Ld);
Sam Parker85ad78b2019-07-11 07:47:50 +0000337 }
338 }
339 return false;
340}
341
Sam Parkera33e3112019-05-13 09:23:32 +0000342/// Iterate through the block and record base, offset pairs of loads which can
343/// be widened into a single load.
344bool ARMParallelDSP::RecordMemoryOps(BasicBlock *BB) {
Sam Parker453ba912018-11-09 09:18:00 +0000345 SmallVector<LoadInst*, 8> Loads;
Sam Parkera33e3112019-05-13 09:23:32 +0000346 SmallVector<Instruction*, 8> Writes;
Sam Parkera761ba02019-08-28 08:51:13 +0000347 LoadPairs.clear();
348 WideLoads.clear();
Sam Parkera33e3112019-05-13 09:23:32 +0000349
350 // Collect loads and instruction that may write to memory. For now we only
351 // record loads which are simple, sign-extended and have a single user.
352 // TODO: Allow zero-extended loads.
Sam Parker4c4ff132019-03-14 11:14:13 +0000353 for (auto &I : *BB) {
Sam Parkera33e3112019-05-13 09:23:32 +0000354 if (I.mayWriteToMemory())
355 Writes.push_back(&I);
Sam Parker453ba912018-11-09 09:18:00 +0000356 auto *Ld = dyn_cast<LoadInst>(&I);
Sam Parker4c4ff132019-03-14 11:14:13 +0000357 if (!Ld || !Ld->isSimple() ||
358 !Ld->hasOneUse() || !isa<SExtInst>(Ld->user_back()))
Sam Parker453ba912018-11-09 09:18:00 +0000359 continue;
360 Loads.push_back(Ld);
361 }
362
Sam Parkera33e3112019-05-13 09:23:32 +0000363 using InstSet = std::set<Instruction*>;
364 using DepMap = std::map<Instruction*, InstSet>;
365 DepMap RAWDeps;
366
367 // Record any writes that may alias a load.
368 const auto Size = LocationSize::unknown();
369 for (auto Read : Loads) {
370 for (auto Write : Writes) {
371 MemoryLocation ReadLoc =
372 MemoryLocation(Read->getPointerOperand(), Size);
373
374 if (!isModOrRefSet(intersectModRef(AA->getModRefInfo(Write, ReadLoc),
375 ModRefInfo::ModRef)))
376 continue;
377 if (DT->dominates(Write, Read))
378 RAWDeps[Read].insert(Write);
379 }
380 }
381
382 // Check whether there's not a write between the two loads which would
383 // prevent them from being safely merged.
384 auto SafeToPair = [&](LoadInst *Base, LoadInst *Offset) {
385 LoadInst *Dominator = DT->dominates(Base, Offset) ? Base : Offset;
386 LoadInst *Dominated = DT->dominates(Base, Offset) ? Offset : Base;
387
388 if (RAWDeps.count(Dominated)) {
389 InstSet &WritesBefore = RAWDeps[Dominated];
390
391 for (auto Before : WritesBefore) {
Sam Parkera33e3112019-05-13 09:23:32 +0000392 // We can't move the second load backward, past a write, to merge
393 // with the first load.
394 if (DT->dominates(Dominator, Before))
395 return false;
396 }
397 }
398 return true;
399 };
400
401 // Record base, offset load pairs.
402 for (auto *Base : Loads) {
403 for (auto *Offset : Loads) {
404 if (Base == Offset)
Sam Parker453ba912018-11-09 09:18:00 +0000405 continue;
406
Sam Parkera33e3112019-05-13 09:23:32 +0000407 if (AreSequentialAccesses<LoadInst>(Base, Offset, *DL, *SE) &&
408 SafeToPair(Base, Offset)) {
409 LoadPairs[Base] = Offset;
Sam Parker85ad78b2019-07-11 07:47:50 +0000410 OffsetLoads.insert(Offset);
Sam Parker4c4ff132019-03-14 11:14:13 +0000411 break;
Sam Parker453ba912018-11-09 09:18:00 +0000412 }
413 }
414 }
Sam Parker4c4ff132019-03-14 11:14:13 +0000415
416 LLVM_DEBUG(if (!LoadPairs.empty()) {
417 dbgs() << "Consecutive load pairs:\n";
418 for (auto &MapIt : LoadPairs) {
419 LLVM_DEBUG(dbgs() << *MapIt.first << ", "
420 << *MapIt.second << "\n");
421 }
422 });
Sam Parker453ba912018-11-09 09:18:00 +0000423 return LoadPairs.size() > 1;
424}
425
Sam Parkera761ba02019-08-28 08:51:13 +0000426// Search recursively back through the operands to find a tree of values that
427// form a multiply-accumulate chain. The search records the Add and Mul
428// instructions that form the reduction and allows us to find a single value
429// to be used as the initial input to the accumlator.
430bool ARMParallelDSP::Search(Value *V, BasicBlock *BB, Reduction &R) {
431 // If we find a non-instruction, try to use it as the initial accumulator
432 // value. This may have already been found during the search in which case
433 // this function will return false, signaling a search fail.
434 auto *I = dyn_cast<Instruction>(V);
435 if (!I)
436 return R.InsertAcc(V);
437
438 if (I->getParent() != BB)
439 return false;
440
441 switch (I->getOpcode()) {
442 default:
443 break;
444 case Instruction::PHI:
445 // Could be the accumulator value.
446 return R.InsertAcc(V);
447 case Instruction::Add: {
448 // Adds should be adding together two muls, or another add and a mul to
449 // be within the mac chain. One of the operands may also be the
450 // accumulator value at which point we should stop searching.
451 R.InsertAdd(I);
452 Value *LHS = I->getOperand(0);
453 Value *RHS = I->getOperand(1);
454 bool ValidLHS = Search(LHS, BB, R);
455 bool ValidRHS = Search(RHS, BB, R);
456
457 if (ValidLHS && ValidRHS)
458 return true;
459
460 return R.InsertAcc(I);
461 }
462 case Instruction::Mul: {
463 Value *MulOp0 = I->getOperand(0);
464 Value *MulOp1 = I->getOperand(1);
465 return IsNarrowSequence<16>(MulOp0) && IsNarrowSequence<16>(MulOp1);
466 }
467 case Instruction::SExt:
468 return Search(I->getOperand(0), BB, R);
469 }
470 return false;
471}
472
473// The pass needs to identify integer add/sub reductions of 16-bit vector
Sam Parker85ad78b2019-07-11 07:47:50 +0000474// multiplications.
475// To use SMLAD:
476// 1) we first need to find integer add then look for this pattern:
477//
478// acc0 = ...
479// ld0 = load i16
480// sext0 = sext i16 %ld0 to i32
481// ld1 = load i16
482// sext1 = sext i16 %ld1 to i32
483// mul0 = mul %sext0, %sext1
484// ld2 = load i16
485// sext2 = sext i16 %ld2 to i32
486// ld3 = load i16
487// sext3 = sext i16 %ld3 to i32
488// mul1 = mul i32 %sext2, %sext3
489// add0 = add i32 %mul0, %acc0
490// acc1 = add i32 %add0, %mul1
491//
492// Which can be selected to:
493//
494// ldr r0
495// ldr r1
496// smlad r2, r0, r1, r2
497//
498// If constants are used instead of loads, these will need to be hoisted
499// out and into a register.
500//
501// If loop invariants are used instead of loads, these need to be packed
502// before the loop begins.
503//
Sam Parkera761ba02019-08-28 08:51:13 +0000504bool ARMParallelDSP::MatchSMLAD(Function &F) {
Sam Parker85ad78b2019-07-11 07:47:50 +0000505 bool Changed = false;
Sam Parker85ad78b2019-07-11 07:47:50 +0000506
Sam Parkera761ba02019-08-28 08:51:13 +0000507 for (auto &BB : F) {
508 SmallPtrSet<Instruction*, 4> AllAdds;
509 if (!RecordMemoryOps(&BB))
Sam Parker85ad78b2019-07-11 07:47:50 +0000510 continue;
511
Sam Parkera761ba02019-08-28 08:51:13 +0000512 for (Instruction &I : reverse(BB)) {
513 if (I.getOpcode() != Instruction::Add)
514 continue;
Sam Parker85ad78b2019-07-11 07:47:50 +0000515
Sam Parkera761ba02019-08-28 08:51:13 +0000516 if (AllAdds.count(&I))
517 continue;
Sam Parker85ad78b2019-07-11 07:47:50 +0000518
Sam Parkera761ba02019-08-28 08:51:13 +0000519 const auto *Ty = I.getType();
520 if (!Ty->isIntegerTy(32) && !Ty->isIntegerTy(64))
521 continue;
Sam Parker85ad78b2019-07-11 07:47:50 +0000522
Sam Parkera761ba02019-08-28 08:51:13 +0000523 Reduction R(&I);
524 if (!Search(&I, &BB, R))
525 continue;
Sam Parker85ad78b2019-07-11 07:47:50 +0000526
Sam Parkera761ba02019-08-28 08:51:13 +0000527 R.InsertMuls();
528 LLVM_DEBUG(dbgs() << "After search, Reduction:\n"; R.dump());
529
530 if (!CreateParallelPairs(R))
531 continue;
532
533 InsertParallelMACs(R);
534 Changed = true;
535 AllAdds.insert(R.getAdds().begin(), R.getAdds().end());
536 }
Sam Parker85ad78b2019-07-11 07:47:50 +0000537 }
538
539 return Changed;
540}
541
542bool ARMParallelDSP::CreateParallelPairs(Reduction &R) {
543
544 // Not enough mul operations to make a pair.
545 if (R.getMuls().size() < 2)
546 return false;
547
548 // Check that the muls operate directly upon sign extended loads.
Sam Parker414dd1c2019-07-29 08:41:51 +0000549 for (auto &MulCand : R.getMuls()) {
550 if (!MulCand->HasTwoLoadInputs())
Sam Parker85ad78b2019-07-11 07:47:50 +0000551 return false;
Sam Parker85ad78b2019-07-11 07:47:50 +0000552 }
553
Sam Parker414dd1c2019-07-29 08:41:51 +0000554 auto CanPair = [&](Reduction &R, MulCandidate *PMul0, MulCandidate *PMul1) {
Sam Parker453ba912018-11-09 09:18:00 +0000555 // The first elements of each vector should be loads with sexts. If we
556 // find that its two pairs of consecutive loads, then these can be
557 // transformed into two wider loads and the users can be replaced with
558 // DSP intrinsics.
Sam Parker414dd1c2019-07-29 08:41:51 +0000559 auto Ld0 = static_cast<LoadInst*>(PMul0->LHS);
560 auto Ld1 = static_cast<LoadInst*>(PMul1->LHS);
561 auto Ld2 = static_cast<LoadInst*>(PMul0->RHS);
562 auto Ld3 = static_cast<LoadInst*>(PMul1->RHS);
Sam Parker453ba912018-11-09 09:18:00 +0000563
Sam Parker414dd1c2019-07-29 08:41:51 +0000564 if (AreSequentialLoads(Ld0, Ld1, PMul0->VecLd)) {
565 if (AreSequentialLoads(Ld2, Ld3, PMul1->VecLd)) {
Sam Parker453ba912018-11-09 09:18:00 +0000566 LLVM_DEBUG(dbgs() << "OK: found two pairs of parallel loads!\n");
Sam Parker414dd1c2019-07-29 08:41:51 +0000567 R.AddMulPair(PMul0, PMul1);
568 return true;
569 } else if (AreSequentialLoads(Ld3, Ld2, PMul1->VecLd)) {
570 LLVM_DEBUG(dbgs() << "OK: found two pairs of parallel loads!\n");
571 LLVM_DEBUG(dbgs() << " exchanging Ld2 and Ld3\n");
Sam Parkera761ba02019-08-28 08:51:13 +0000572 R.AddMulPair(PMul0, PMul1, true);
Sam Parker453ba912018-11-09 09:18:00 +0000573 return true;
574 }
Sam Parker414dd1c2019-07-29 08:41:51 +0000575 } else if (AreSequentialLoads(Ld1, Ld0, PMul0->VecLd) &&
576 AreSequentialLoads(Ld2, Ld3, PMul1->VecLd)) {
577 LLVM_DEBUG(dbgs() << "OK: found two pairs of parallel loads!\n");
578 LLVM_DEBUG(dbgs() << " exchanging Ld0 and Ld1\n");
579 LLVM_DEBUG(dbgs() << " and swapping muls\n");
Sam Parker414dd1c2019-07-29 08:41:51 +0000580 // Only the second operand can be exchanged, so swap the muls.
Sam Parkera761ba02019-08-28 08:51:13 +0000581 R.AddMulPair(PMul1, PMul0, true);
Sam Parker414dd1c2019-07-29 08:41:51 +0000582 return true;
Sam Parker453ba912018-11-09 09:18:00 +0000583 }
584 return false;
585 };
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000586
Sam Parker414dd1c2019-07-29 08:41:51 +0000587 MulCandList &Muls = R.getMuls();
Sam Parker85ad78b2019-07-11 07:47:50 +0000588 const unsigned Elems = Muls.size();
Sam Parkera023c7a2018-09-12 09:17:44 +0000589 for (unsigned i = 0; i < Elems; ++i) {
Sam Parker414dd1c2019-07-29 08:41:51 +0000590 MulCandidate *PMul0 = static_cast<MulCandidate*>(Muls[i].get());
Sam Parkera761ba02019-08-28 08:51:13 +0000591 if (PMul0->Paired)
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000592 continue;
593
Sam Parkera023c7a2018-09-12 09:17:44 +0000594 for (unsigned j = 0; j < Elems; ++j) {
595 if (i == j)
596 continue;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000597
Sam Parker414dd1c2019-07-29 08:41:51 +0000598 MulCandidate *PMul1 = static_cast<MulCandidate*>(Muls[j].get());
Sam Parkera761ba02019-08-28 08:51:13 +0000599 if (PMul1->Paired)
Sam Parkera023c7a2018-09-12 09:17:44 +0000600 continue;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000601
Sam Parkera023c7a2018-09-12 09:17:44 +0000602 const Instruction *Mul0 = PMul0->Root;
603 const Instruction *Mul1 = PMul1->Root;
604 if (Mul0 == Mul1)
605 continue;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000606
Sam Parkera023c7a2018-09-12 09:17:44 +0000607 assert(PMul0 != PMul1 && "expected different chains");
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000608
Sam Parkera761ba02019-08-28 08:51:13 +0000609 if (CanPair(R, PMul0, PMul1))
Sam Parkera023c7a2018-09-12 09:17:44 +0000610 break;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000611 }
612 }
Sam Parker85ad78b2019-07-11 07:47:50 +0000613 return !R.getMulPairs().empty();
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000614}
615
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000616
Sam Parker85ad78b2019-07-11 07:47:50 +0000617void ARMParallelDSP::InsertParallelMACs(Reduction &R) {
618
Sam Parker7ca8c6f2019-08-01 08:17:51 +0000619 auto CreateSMLAD = [&](LoadInst* WideLd0, LoadInst *WideLd1,
620 Value *Acc, bool Exchange,
621 Instruction *InsertAfter) {
Sam Parker85ad78b2019-07-11 07:47:50 +0000622 // Replace the reduction chain with an intrinsic call
Sam Parker85ad78b2019-07-11 07:47:50 +0000623
624 Value* Args[] = { WideLd0, WideLd1, Acc };
625 Function *SMLAD = nullptr;
626 if (Exchange)
627 SMLAD = Acc->getType()->isIntegerTy(32) ?
628 Intrinsic::getDeclaration(M, Intrinsic::arm_smladx) :
629 Intrinsic::getDeclaration(M, Intrinsic::arm_smlaldx);
630 else
631 SMLAD = Acc->getType()->isIntegerTy(32) ?
632 Intrinsic::getDeclaration(M, Intrinsic::arm_smlad) :
633 Intrinsic::getDeclaration(M, Intrinsic::arm_smlald);
634
635 IRBuilder<NoFolder> Builder(InsertAfter->getParent(),
636 ++BasicBlock::iterator(InsertAfter));
637 Instruction *Call = Builder.CreateCall(SMLAD, Args);
638 NumSMLAD++;
639 return Call;
640 };
641
642 Instruction *InsertAfter = R.getRoot();
643 Value *Acc = R.getAccumulator();
Sam Parkera761ba02019-08-28 08:51:13 +0000644
645 // For any muls that were discovered but not paired, accumulate their values
646 // as before.
647 IRBuilder<NoFolder> Builder(InsertAfter->getParent(),
648 ++BasicBlock::iterator(InsertAfter));
649 MulCandList &MulCands = R.getMuls();
650 for (auto &MulCand : MulCands) {
651 if (MulCand->Paired)
652 continue;
653
Sam Parkerfea53222019-09-04 08:41:34 +0000654 Value *Mul = MulCand->Root;
655 LLVM_DEBUG(dbgs() << "Accumulating unpaired mul: " << *Mul << "\n");
656
Sam Parkerc363deb2019-09-09 08:39:14 +0000657 if (R.getType() != Mul->getType()) {
Sam Parkerfea53222019-09-04 08:41:34 +0000658 assert(R.is64Bit() && "expected 64-bit result");
Sam Parkerc363deb2019-09-09 08:39:14 +0000659 Mul = Builder.CreateSExt(Mul, R.getType());
Sam Parkerfea53222019-09-04 08:41:34 +0000660 }
661
Sam Parkera761ba02019-08-28 08:51:13 +0000662 if (!Acc) {
Sam Parkerfea53222019-09-04 08:41:34 +0000663 Acc = Mul;
Sam Parkera761ba02019-08-28 08:51:13 +0000664 continue;
665 }
Sam Parkerfea53222019-09-04 08:41:34 +0000666
667 Acc = Builder.CreateAdd(Mul, Acc);
Sam Parkera761ba02019-08-28 08:51:13 +0000668 InsertAfter = cast<Instruction>(Acc);
669 }
670
Sam Parkerc363deb2019-09-09 08:39:14 +0000671 if (!Acc) {
Sam Parkerfea53222019-09-04 08:41:34 +0000672 Acc = R.is64Bit() ?
673 ConstantInt::get(IntegerType::get(M->getContext(), 64), 0) :
674 ConstantInt::get(IntegerType::get(M->getContext(), 32), 0);
Sam Parkerc363deb2019-09-09 08:39:14 +0000675 } else if (Acc->getType() != R.getType()) {
676 Builder.SetInsertPoint(R.getRoot());
677 Acc = Builder.CreateSExt(Acc, R.getType());
678 }
Sam Parker85ad78b2019-07-11 07:47:50 +0000679
Sam Parker7ca8c6f2019-08-01 08:17:51 +0000680 IntegerType *Ty = IntegerType::get(M->getContext(), 32);
Sam Parker85ad78b2019-07-11 07:47:50 +0000681 for (auto &Pair : R.getMulPairs()) {
Sam Parker7ca8c6f2019-08-01 08:17:51 +0000682 MulCandidate *LHSMul = Pair.first;
683 MulCandidate *RHSMul = Pair.second;
Sam Parker7ca8c6f2019-08-01 08:17:51 +0000684 LoadInst *BaseLHS = LHSMul->getBaseLoad();
685 LoadInst *BaseRHS = RHSMul->getBaseLoad();
686 LoadInst *WideLHS = WideLoads.count(BaseLHS) ?
687 WideLoads[BaseLHS]->getLoad() : CreateWideLoad(LHSMul->VecLd, Ty);
688 LoadInst *WideRHS = WideLoads.count(BaseRHS) ?
689 WideLoads[BaseRHS]->getLoad() : CreateWideLoad(RHSMul->VecLd, Ty);
Sam Parkera023c7a2018-09-12 09:17:44 +0000690
Sam Parker7ca8c6f2019-08-01 08:17:51 +0000691 Acc = CreateSMLAD(WideLHS, WideRHS, Acc, RHSMul->Exchange, InsertAfter);
Sam Parker85ad78b2019-07-11 07:47:50 +0000692 InsertAfter = cast<Instruction>(Acc);
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000693 }
Sam Parker85ad78b2019-07-11 07:47:50 +0000694 R.UpdateRoot(cast<Instruction>(Acc));
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000695}
696
Sam Parkercd385992019-08-02 08:21:17 +0000697LoadInst* ARMParallelDSP::CreateWideLoad(MemInstList &Loads,
Sam Parkera33e3112019-05-13 09:23:32 +0000698 IntegerType *LoadTy) {
Sam Parker4c4ff132019-03-14 11:14:13 +0000699 assert(Loads.size() == 2 && "currently only support widening two loads");
Sam Parkera33e3112019-05-13 09:23:32 +0000700
701 LoadInst *Base = Loads[0];
702 LoadInst *Offset = Loads[1];
703
704 Instruction *BaseSExt = dyn_cast<SExtInst>(Base->user_back());
705 Instruction *OffsetSExt = dyn_cast<SExtInst>(Offset->user_back());
706
707 assert((BaseSExt && OffsetSExt)
708 && "Loads should have a single, extending, user");
709
710 std::function<void(Value*, Value*)> MoveBefore =
711 [&](Value *A, Value *B) -> void {
712 if (!isa<Instruction>(A) || !isa<Instruction>(B))
713 return;
714
715 auto *Source = cast<Instruction>(A);
716 auto *Sink = cast<Instruction>(B);
717
718 if (DT->dominates(Source, Sink) ||
719 Source->getParent() != Sink->getParent() ||
720 isa<PHINode>(Source) || isa<PHINode>(Sink))
721 return;
722
723 Source->moveBefore(Sink);
Sam Parkeraeb21b92019-07-24 09:38:39 +0000724 for (auto &Op : Source->operands())
725 MoveBefore(Op, Source);
Sam Parkera33e3112019-05-13 09:23:32 +0000726 };
727
728 // Insert the load at the point of the original dominating load.
729 LoadInst *DomLoad = DT->dominates(Base, Offset) ? Base : Offset;
730 IRBuilder<NoFolder> IRB(DomLoad->getParent(),
731 ++BasicBlock::iterator(DomLoad));
732
733 // Bitcast the pointer to a wider type and create the wide load, while making
734 // sure to maintain the original alignment as this prevents ldrd from being
735 // generated when it could be illegal due to memory alignment.
736 const unsigned AddrSpace = DomLoad->getPointerAddressSpace();
737 Value *VecPtr = IRB.CreateBitCast(Base->getPointerOperand(),
Eli Friedmanb09c7782018-10-18 19:34:30 +0000738 LoadTy->getPointerTo(AddrSpace));
Sam Parker4c4ff132019-03-14 11:14:13 +0000739 LoadInst *WideLoad = IRB.CreateAlignedLoad(LoadTy, VecPtr,
Sam Parkera33e3112019-05-13 09:23:32 +0000740 Base->getAlignment());
Sam Parker4c4ff132019-03-14 11:14:13 +0000741
Sam Parkera33e3112019-05-13 09:23:32 +0000742 // Make sure everything is in the correct order in the basic block.
743 MoveBefore(Base->getPointerOperand(), VecPtr);
744 MoveBefore(VecPtr, WideLoad);
Sam Parker4c4ff132019-03-14 11:14:13 +0000745
746 // From the wide load, create two values that equal the original two loads.
Sam Parkera33e3112019-05-13 09:23:32 +0000747 // Loads[0] needs trunc while Loads[1] needs a lshr and trunc.
748 // TODO: Support big-endian as well.
749 Value *Bottom = IRB.CreateTrunc(WideLoad, Base->getType());
Sam Parkera761ba02019-08-28 08:51:13 +0000750 Value *NewBaseSExt = IRB.CreateSExt(Bottom, BaseSExt->getType());
751 BaseSExt->replaceAllUsesWith(NewBaseSExt);
Sam Parker4c4ff132019-03-14 11:14:13 +0000752
Sam Parkera33e3112019-05-13 09:23:32 +0000753 IntegerType *OffsetTy = cast<IntegerType>(Offset->getType());
754 Value *ShiftVal = ConstantInt::get(LoadTy, OffsetTy->getBitWidth());
Sam Parker4c4ff132019-03-14 11:14:13 +0000755 Value *Top = IRB.CreateLShr(WideLoad, ShiftVal);
Sam Parkera33e3112019-05-13 09:23:32 +0000756 Value *Trunc = IRB.CreateTrunc(Top, OffsetTy);
Sam Parkera761ba02019-08-28 08:51:13 +0000757 Value *NewOffsetSExt = IRB.CreateSExt(Trunc, OffsetSExt->getType());
758 OffsetSExt->replaceAllUsesWith(NewOffsetSExt);
Sam Parker4c4ff132019-03-14 11:14:13 +0000759
Sam Parkera761ba02019-08-28 08:51:13 +0000760 LLVM_DEBUG(dbgs() << "From Base and Offset:\n"
761 << *Base << "\n" << *Offset << "\n"
762 << "Created Wide Load:\n"
763 << *WideLoad << "\n"
764 << *Bottom << "\n"
765 << *NewBaseSExt << "\n"
766 << *Top << "\n"
767 << *Trunc << "\n"
768 << *NewOffsetSExt << "\n");
Sam Parkera33e3112019-05-13 09:23:32 +0000769 WideLoads.emplace(std::make_pair(Base,
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000770 std::make_unique<WidenedLoad>(Loads, WideLoad)));
Sam Parker4c4ff132019-03-14 11:14:13 +0000771 return WideLoad;
Eli Friedmanb09c7782018-10-18 19:34:30 +0000772}
773
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000774Pass *llvm::createARMParallelDSPPass() {
775 return new ARMParallelDSP();
776}
777
778char ARMParallelDSP::ID = 0;
779
Sjoerd Meijerb3e06fa2018-07-06 14:47:09 +0000780INITIALIZE_PASS_BEGIN(ARMParallelDSP, "arm-parallel-dsp",
Sam Parkera761ba02019-08-28 08:51:13 +0000781 "Transform functions to use DSP intrinsics", false, false)
Sjoerd Meijerb3e06fa2018-07-06 14:47:09 +0000782INITIALIZE_PASS_END(ARMParallelDSP, "arm-parallel-dsp",
Sam Parkera761ba02019-08-28 08:51:13 +0000783 "Transform functions to use DSP intrinsics", false, false)