blob: aa61ccad0a94385827d2538c2784cfe14a9250ac [file] [log] [blame]
Sam Parker2200a9b2019-07-31 07:32:03 +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 Parker89a37992018-07-23 15:25:59 +000046 struct OpChain;
Sam Parker414dd1c2019-07-29 08:41:51 +000047 struct MulCandidate;
Sam Parker85ad78b2019-07-11 07:47:50 +000048 class Reduction;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +000049
Sam Parker414dd1c2019-07-29 08:41:51 +000050 using MulCandList = SmallVector<std::unique_ptr<MulCandidate>, 8>;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +000051 using ReductionList = SmallVector<Reduction, 8>;
52 using ValueList = SmallVector<Value*, 8>;
Sam Parker4c4ff132019-03-14 11:14:13 +000053 using MemInstList = SmallVector<LoadInst*, 8>;
Sam Parker414dd1c2019-07-29 08:41:51 +000054 using PMACPair = std::pair<MulCandidate*,MulCandidate*>;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +000055 using PMACPairList = SmallVector<PMACPair, 8>;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +000056
Sam Parker414dd1c2019-07-29 08:41:51 +000057 // 'MulCandidate' holds the multiplication instructions that are candidates
Sam Parker3da59e52019-07-26 14:11:40 +000058 // for parallel execution.
Sam Parker414dd1c2019-07-29 08:41:51 +000059 struct MulCandidate {
Sam Parker89a37992018-07-23 15:25:59 +000060 Instruction *Root;
Sam Parker414dd1c2019-07-29 08:41:51 +000061 MemInstList VecLd; // Container for loads to widen.
62 Value* LHS;
63 Value* RHS;
Sam Parker3da59e52019-07-26 14:11:40 +000064 bool Exchange = false;
Sam Parker89a37992018-07-23 15:25:59 +000065 bool ReadOnly = true;
66
Sam Parker414dd1c2019-07-29 08:41:51 +000067 MulCandidate(Instruction *I, ValueList &lhs, ValueList &rhs) :
68 Root(I), LHS(lhs.front()), RHS(rhs.front()) { }
Sam Parker89a37992018-07-23 15:25:59 +000069
Sam Parker414dd1c2019-07-29 08:41:51 +000070 bool HasTwoLoadInputs() const {
71 return isa<LoadInst>(LHS) && isa<LoadInst>(RHS);
72 }
Sjoerd Meijerc89ca552018-06-28 12:55:29 +000073 };
74
Sam Parker85ad78b2019-07-11 07:47:50 +000075 /// Represent a sequence of multiply-accumulate operations with the aim to
76 /// perform the multiplications in parallel.
77 class Reduction {
78 Instruction *Root = nullptr;
79 Value *Acc = nullptr;
Sam Parker414dd1c2019-07-29 08:41:51 +000080 MulCandList Muls;
Sam Parker85ad78b2019-07-11 07:47:50 +000081 PMACPairList MulPairs;
82 SmallPtrSet<Instruction*, 4> Adds;
83
84 public:
85 Reduction() = delete;
86
87 Reduction (Instruction *Add) : Root(Add) { }
88
89 /// Record an Add instruction that is a part of the this reduction.
90 void InsertAdd(Instruction *I) { Adds.insert(I); }
91
Sam Parker414dd1c2019-07-29 08:41:51 +000092 /// Record a MulCandidate, rooted at a Mul instruction, that is a part of
Sam Parker85ad78b2019-07-11 07:47:50 +000093 /// this reduction.
94 void InsertMul(Instruction *I, ValueList &LHS, ValueList &RHS) {
Sam Parker414dd1c2019-07-29 08:41:51 +000095 Muls.push_back(make_unique<MulCandidate>(I, LHS, RHS));
Sam Parker85ad78b2019-07-11 07:47:50 +000096 }
97
98 /// Add the incoming accumulator value, returns true if a value had not
99 /// already been added. Returning false signals to the user that this
100 /// reduction already has a value to initialise the accumulator.
101 bool InsertAcc(Value *V) {
102 if (Acc)
103 return false;
104 Acc = V;
105 return true;
106 }
107
Sam Parker414dd1c2019-07-29 08:41:51 +0000108 /// Set two MulCandidates, rooted at muls, that can be executed as a single
Sam Parker85ad78b2019-07-11 07:47:50 +0000109 /// parallel operation.
Sam Parker414dd1c2019-07-29 08:41:51 +0000110 void AddMulPair(MulCandidate *Mul0, MulCandidate *Mul1) {
Sam Parker85ad78b2019-07-11 07:47:50 +0000111 MulPairs.push_back(std::make_pair(Mul0, Mul1));
112 }
113
114 /// Return true if enough mul operations are found that can be executed in
115 /// parallel.
116 bool CreateParallelPairs();
117
118 /// Return the add instruction which is the root of the reduction.
119 Instruction *getRoot() { return Root; }
120
121 /// Return the incoming value to be accumulated. This maybe null.
122 Value *getAccumulator() { return Acc; }
123
124 /// Return the set of adds that comprise the reduction.
125 SmallPtrSetImpl<Instruction*> &getAdds() { return Adds; }
126
Sam Parker414dd1c2019-07-29 08:41:51 +0000127 /// Return the MulCandidate, rooted at mul instruction, that comprise the
Sam Parker85ad78b2019-07-11 07:47:50 +0000128 /// the reduction.
Sam Parker414dd1c2019-07-29 08:41:51 +0000129 MulCandList &getMuls() { return Muls; }
Sam Parker85ad78b2019-07-11 07:47:50 +0000130
Sam Parker414dd1c2019-07-29 08:41:51 +0000131 /// Return the MulCandidate, rooted at mul instructions, that have been
Sam Parker85ad78b2019-07-11 07:47:50 +0000132 /// paired for parallel execution.
133 PMACPairList &getMulPairs() { return MulPairs; }
134
135 /// To finalise, replace the uses of the root with the intrinsic call.
136 void UpdateRoot(Instruction *SMLAD) {
137 Root->replaceAllUsesWith(SMLAD);
138 }
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000139 };
140
Sam Parker4c4ff132019-03-14 11:14:13 +0000141 class WidenedLoad {
142 LoadInst *NewLd = nullptr;
143 SmallVector<LoadInst*, 4> Loads;
144
145 public:
146 WidenedLoad(SmallVectorImpl<LoadInst*> &Lds, LoadInst *Wide)
147 : NewLd(Wide) {
148 for (auto *I : Lds)
149 Loads.push_back(I);
150 }
151 LoadInst *getLoad() {
152 return NewLd;
153 }
154 };
155
Sam Parker2200a9b2019-07-31 07:32:03 +0000156 class ARMParallelDSP : public FunctionPass {
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000157 ScalarEvolution *SE;
158 AliasAnalysis *AA;
159 TargetLibraryInfo *TLI;
160 DominatorTree *DT;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000161 const DataLayout *DL;
162 Module *M;
Sam Parker453ba912018-11-09 09:18:00 +0000163 std::map<LoadInst*, LoadInst*> LoadPairs;
Sam Parker85ad78b2019-07-11 07:47:50 +0000164 SmallPtrSet<LoadInst*, 4> OffsetLoads;
Sam Parker4c4ff132019-03-14 11:14:13 +0000165 std::map<LoadInst*, std::unique_ptr<WidenedLoad>> WideLoads;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000166
Sam Parker85ad78b2019-07-11 07:47:50 +0000167 template<unsigned>
168 bool IsNarrowSequence(Value *V, ValueList &VL);
169
Sam Parkera33e3112019-05-13 09:23:32 +0000170 bool RecordMemoryOps(BasicBlock *BB);
Sam Parker85ad78b2019-07-11 07:47:50 +0000171 void InsertParallelMACs(Reduction &Reduction);
Fangrui Song68169342018-07-03 19:12:27 +0000172 bool AreSequentialLoads(LoadInst *Ld0, LoadInst *Ld1, MemInstList &VecMem);
Sam Parkera33e3112019-05-13 09:23:32 +0000173 LoadInst* CreateWideLoad(SmallVectorImpl<LoadInst*> &Loads,
174 IntegerType *LoadTy);
Sam Parker85ad78b2019-07-11 07:47:50 +0000175 bool CreateParallelPairs(Reduction &R);
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000176
177 /// Try to match and generate: SMLAD, SMLADX - Signed Multiply Accumulate
178 /// Dual performs two signed 16x16-bit multiplications. It adds the
179 /// products to a 32-bit accumulate operand. Optionally, the instruction can
180 /// exchange the halfwords of the second operand before performing the
181 /// arithmetic.
Sam Parker2200a9b2019-07-31 07:32:03 +0000182 bool MatchSMLAD(Function &F);
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000183
184 public:
185 static char ID;
186
Sam Parker2200a9b2019-07-31 07:32:03 +0000187 ARMParallelDSP() : FunctionPass(ID) { }
Sam Parkera33e3112019-05-13 09:23:32 +0000188
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000189 void getAnalysisUsage(AnalysisUsage &AU) const override {
Sam Parker2200a9b2019-07-31 07:32:03 +0000190 FunctionPass::getAnalysisUsage(AU);
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000191 AU.addRequired<AssumptionCacheTracker>();
192 AU.addRequired<ScalarEvolutionWrapperPass>();
193 AU.addRequired<AAResultsWrapperPass>();
194 AU.addRequired<TargetLibraryInfoWrapperPass>();
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000195 AU.addRequired<DominatorTreeWrapperPass>();
196 AU.addRequired<TargetPassConfig>();
Sam Parker2200a9b2019-07-31 07:32:03 +0000197 AU.addPreserved<ScalarEvolutionWrapperPass>();
198 AU.addPreserved<GlobalsAAWrapperPass>();
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000199 AU.setPreservesCFG();
200 }
201
Sam Parker2200a9b2019-07-31 07:32:03 +0000202 bool runOnFunction(Function &F) override {
Sjoerd Meijer3c859b32018-08-14 07:43:49 +0000203 if (DisableParallelDSP)
204 return false;
Sam Parker2200a9b2019-07-31 07:32:03 +0000205 if (skipFunction(F))
Eli Friedmanb27fc952019-07-23 20:48:46 +0000206 return false;
207
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000208 SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
209 AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
210 TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
211 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000212 auto &TPC = getAnalysis<TargetPassConfig>();
213
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000214 M = F.getParent();
215 DL = &M->getDataLayout();
216
217 auto &TM = TPC.getTM<TargetMachine>();
218 auto *ST = &TM.getSubtarget<ARMSubtarget>(F);
219
220 if (!ST->allowsUnalignedMem()) {
221 LLVM_DEBUG(dbgs() << "Unaligned memory access not supported: not "
222 "running pass ARMParallelDSP\n");
223 return false;
224 }
225
226 if (!ST->hasDSP()) {
227 LLVM_DEBUG(dbgs() << "DSP extension not enabled: not running pass "
228 "ARMParallelDSP\n");
229 return false;
230 }
231
Sam Parker9e730202019-03-15 10:19:32 +0000232 if (!ST->isLittle()) {
233 LLVM_DEBUG(dbgs() << "Only supporting little endian: not running pass "
Sam Parkera33e3112019-05-13 09:23:32 +0000234 << "ARMParallelDSP\n");
Sam Parker9e730202019-03-15 10:19:32 +0000235 return false;
236 }
237
Sam Parkera023c7a2018-09-12 09:17:44 +0000238 LLVM_DEBUG(dbgs() << "\n== Parallel DSP pass ==\n");
239 LLVM_DEBUG(dbgs() << " - " << F.getName() << "\n\n");
Sam Parker453ba912018-11-09 09:18:00 +0000240
Sam Parker2200a9b2019-07-31 07:32:03 +0000241 bool Changes = MatchSMLAD(F);
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000242 return Changes;
243 }
244 };
245}
246
Sam Parkerffc16812018-07-03 12:44:16 +0000247template<typename MemInst>
248static bool AreSequentialAccesses(MemInst *MemOp0, MemInst *MemOp1,
Sam Parker453ba912018-11-09 09:18:00 +0000249 const DataLayout &DL, ScalarEvolution &SE) {
Sam Parker4c4ff132019-03-14 11:14:13 +0000250 if (isConsecutiveAccess(MemOp0, MemOp1, DL, SE))
Sam Parkerffc16812018-07-03 12:44:16 +0000251 return true;
Sam Parkerffc16812018-07-03 12:44:16 +0000252 return false;
253}
254
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000255bool ARMParallelDSP::AreSequentialLoads(LoadInst *Ld0, LoadInst *Ld1,
Sam Parkerffc16812018-07-03 12:44:16 +0000256 MemInstList &VecMem) {
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000257 if (!Ld0 || !Ld1)
258 return false;
259
Sam Parker4c4ff132019-03-14 11:14:13 +0000260 if (!LoadPairs.count(Ld0) || LoadPairs[Ld0] != Ld1)
261 return false;
262
263 LLVM_DEBUG(dbgs() << "Loads are sequential and valid:\n";
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000264 dbgs() << "Ld0:"; Ld0->dump();
265 dbgs() << "Ld1:"; Ld1->dump();
266 );
267
Sam Parker453ba912018-11-09 09:18:00 +0000268 VecMem.clear();
269 VecMem.push_back(Ld0);
270 VecMem.push_back(Ld1);
271 return true;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000272}
273
Sam Parker85ad78b2019-07-11 07:47:50 +0000274// MaxBitwidth: the maximum supported bitwidth of the elements in the DSP
275// instructions, which is set to 16. So here we should collect all i8 and i16
276// narrow operations.
277// TODO: we currently only collect i16, and will support i8 later, so that's
278// why we check that types are equal to MaxBitWidth, and not <= MaxBitWidth.
279template<unsigned MaxBitWidth>
280bool ARMParallelDSP::IsNarrowSequence(Value *V, ValueList &VL) {
Sam Parker74400652019-07-26 10:57:42 +0000281 if (auto *SExt = dyn_cast<SExtInst>(V)) {
282 if (SExt->getSrcTy()->getIntegerBitWidth() != MaxBitWidth)
Sam Parker85ad78b2019-07-11 07:47:50 +0000283 return false;
284
Sam Parker74400652019-07-26 10:57:42 +0000285 if (auto *Ld = dyn_cast<LoadInst>(SExt->getOperand(0))) {
Sam Parker85ad78b2019-07-11 07:47:50 +0000286 // Check that these load could be paired.
287 if (!LoadPairs.count(Ld) && !OffsetLoads.count(Ld))
288 return false;
289
Sam Parker74400652019-07-26 10:57:42 +0000290 VL.push_back(Ld);
291 VL.push_back(SExt);
Sam Parker85ad78b2019-07-11 07:47:50 +0000292 return true;
293 }
294 }
295 return false;
296}
297
Sam Parkera33e3112019-05-13 09:23:32 +0000298/// Iterate through the block and record base, offset pairs of loads which can
299/// be widened into a single load.
300bool ARMParallelDSP::RecordMemoryOps(BasicBlock *BB) {
Sam Parker453ba912018-11-09 09:18:00 +0000301 SmallVector<LoadInst*, 8> Loads;
Sam Parkera33e3112019-05-13 09:23:32 +0000302 SmallVector<Instruction*, 8> Writes;
Sam Parker2200a9b2019-07-31 07:32:03 +0000303 LoadPairs.clear();
304 WideLoads.clear();
Sam Parkera33e3112019-05-13 09:23:32 +0000305
306 // Collect loads and instruction that may write to memory. For now we only
307 // record loads which are simple, sign-extended and have a single user.
308 // TODO: Allow zero-extended loads.
Sam Parker4c4ff132019-03-14 11:14:13 +0000309 for (auto &I : *BB) {
Sam Parkera33e3112019-05-13 09:23:32 +0000310 if (I.mayWriteToMemory())
311 Writes.push_back(&I);
Sam Parker453ba912018-11-09 09:18:00 +0000312 auto *Ld = dyn_cast<LoadInst>(&I);
Sam Parker4c4ff132019-03-14 11:14:13 +0000313 if (!Ld || !Ld->isSimple() ||
314 !Ld->hasOneUse() || !isa<SExtInst>(Ld->user_back()))
Sam Parker453ba912018-11-09 09:18:00 +0000315 continue;
316 Loads.push_back(Ld);
317 }
318
Sam Parkera33e3112019-05-13 09:23:32 +0000319 using InstSet = std::set<Instruction*>;
320 using DepMap = std::map<Instruction*, InstSet>;
321 DepMap RAWDeps;
322
323 // Record any writes that may alias a load.
324 const auto Size = LocationSize::unknown();
325 for (auto Read : Loads) {
326 for (auto Write : Writes) {
327 MemoryLocation ReadLoc =
328 MemoryLocation(Read->getPointerOperand(), Size);
329
330 if (!isModOrRefSet(intersectModRef(AA->getModRefInfo(Write, ReadLoc),
331 ModRefInfo::ModRef)))
332 continue;
333 if (DT->dominates(Write, Read))
334 RAWDeps[Read].insert(Write);
335 }
336 }
337
338 // Check whether there's not a write between the two loads which would
339 // prevent them from being safely merged.
340 auto SafeToPair = [&](LoadInst *Base, LoadInst *Offset) {
341 LoadInst *Dominator = DT->dominates(Base, Offset) ? Base : Offset;
342 LoadInst *Dominated = DT->dominates(Base, Offset) ? Offset : Base;
343
344 if (RAWDeps.count(Dominated)) {
345 InstSet &WritesBefore = RAWDeps[Dominated];
346
347 for (auto Before : WritesBefore) {
348
349 // We can't move the second load backward, past a write, to merge
350 // with the first load.
351 if (DT->dominates(Dominator, Before))
352 return false;
353 }
354 }
355 return true;
356 };
357
358 // Record base, offset load pairs.
359 for (auto *Base : Loads) {
360 for (auto *Offset : Loads) {
361 if (Base == Offset)
Sam Parker453ba912018-11-09 09:18:00 +0000362 continue;
363
Sam Parkera33e3112019-05-13 09:23:32 +0000364 if (AreSequentialAccesses<LoadInst>(Base, Offset, *DL, *SE) &&
365 SafeToPair(Base, Offset)) {
366 LoadPairs[Base] = Offset;
Sam Parker85ad78b2019-07-11 07:47:50 +0000367 OffsetLoads.insert(Offset);
Sam Parker4c4ff132019-03-14 11:14:13 +0000368 break;
Sam Parker453ba912018-11-09 09:18:00 +0000369 }
370 }
371 }
Sam Parker4c4ff132019-03-14 11:14:13 +0000372
373 LLVM_DEBUG(if (!LoadPairs.empty()) {
374 dbgs() << "Consecutive load pairs:\n";
375 for (auto &MapIt : LoadPairs) {
376 LLVM_DEBUG(dbgs() << *MapIt.first << ", "
377 << *MapIt.second << "\n");
378 }
379 });
Sam Parker453ba912018-11-09 09:18:00 +0000380 return LoadPairs.size() > 1;
381}
382
Sam Parker2200a9b2019-07-31 07:32:03 +0000383// The pass needs to identify integer add/sub reductions of 16-bit vector
Sam Parker85ad78b2019-07-11 07:47:50 +0000384// multiplications.
385// To use SMLAD:
386// 1) we first need to find integer add then look for this pattern:
387//
388// acc0 = ...
389// ld0 = load i16
390// sext0 = sext i16 %ld0 to i32
391// ld1 = load i16
392// sext1 = sext i16 %ld1 to i32
393// mul0 = mul %sext0, %sext1
394// ld2 = load i16
395// sext2 = sext i16 %ld2 to i32
396// ld3 = load i16
397// sext3 = sext i16 %ld3 to i32
398// mul1 = mul i32 %sext2, %sext3
399// add0 = add i32 %mul0, %acc0
400// acc1 = add i32 %add0, %mul1
401//
402// Which can be selected to:
403//
404// ldr r0
405// ldr r1
406// smlad r2, r0, r1, r2
407//
408// If constants are used instead of loads, these will need to be hoisted
409// out and into a register.
410//
411// If loop invariants are used instead of loads, these need to be packed
412// before the loop begins.
413//
Sam Parker2200a9b2019-07-31 07:32:03 +0000414bool ARMParallelDSP::MatchSMLAD(Function &F) {
Sam Parker85ad78b2019-07-11 07:47:50 +0000415 // Search recursively back through the operands to find a tree of values that
416 // form a multiply-accumulate chain. The search records the Add and Mul
417 // instructions that form the reduction and allows us to find a single value
418 // to be used as the initial input to the accumlator.
Sam Parker2200a9b2019-07-31 07:32:03 +0000419 std::function<bool(Value*, BasicBlock*, Reduction&)> Search = [&]
420 (Value *V, BasicBlock *BB, Reduction &R) -> bool {
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000421
Sam Parker85ad78b2019-07-11 07:47:50 +0000422 // If we find a non-instruction, try to use it as the initial accumulator
423 // value. This may have already been found during the search in which case
424 // this function will return false, signaling a search fail.
425 auto *I = dyn_cast<Instruction>(V);
426 if (!I)
427 return R.InsertAcc(V);
Sam Parker453ba912018-11-09 09:18:00 +0000428
Sam Parker2200a9b2019-07-31 07:32:03 +0000429 if (I->getParent() != BB)
430 return false;
431
Sam Parker85ad78b2019-07-11 07:47:50 +0000432 switch (I->getOpcode()) {
433 default:
434 break;
435 case Instruction::PHI:
436 // Could be the accumulator value.
437 return R.InsertAcc(V);
438 case Instruction::Add: {
439 // Adds should be adding together two muls, or another add and a mul to
440 // be within the mac chain. One of the operands may also be the
441 // accumulator value at which point we should stop searching.
Sam Parker2200a9b2019-07-31 07:32:03 +0000442 bool ValidLHS = Search(I->getOperand(0), BB, R);
443 bool ValidRHS = Search(I->getOperand(1), BB, R);
Sam Parker85ad78b2019-07-11 07:47:50 +0000444 if (!ValidLHS && !ValidLHS)
445 return false;
446 else if (ValidLHS && ValidRHS) {
447 R.InsertAdd(I);
448 return true;
449 } else {
450 R.InsertAdd(I);
451 return R.InsertAcc(I);
452 }
453 }
454 case Instruction::Mul: {
455 Value *MulOp0 = I->getOperand(0);
456 Value *MulOp1 = I->getOperand(1);
457 if (isa<SExtInst>(MulOp0) && isa<SExtInst>(MulOp1)) {
458 ValueList LHS;
459 ValueList RHS;
460 if (IsNarrowSequence<16>(MulOp0, LHS) &&
461 IsNarrowSequence<16>(MulOp1, RHS)) {
462 R.InsertMul(I, LHS, RHS);
463 return true;
464 }
465 }
466 return false;
467 }
468 case Instruction::SExt:
Sam Parker2200a9b2019-07-31 07:32:03 +0000469 return Search(I->getOperand(0), BB, R);
Sam Parker85ad78b2019-07-11 07:47:50 +0000470 }
471 return false;
472 };
473
474 bool Changed = false;
Sam Parker85ad78b2019-07-11 07:47:50 +0000475
Sam Parker2200a9b2019-07-31 07:32:03 +0000476 for (auto &BB : F) {
477 SmallPtrSet<Instruction*, 4> AllAdds;
478 if (!RecordMemoryOps(&BB))
Sam Parker85ad78b2019-07-11 07:47:50 +0000479 continue;
480
Sam Parker2200a9b2019-07-31 07:32:03 +0000481 for (Instruction &I : reverse(BB)) {
482 if (I.getOpcode() != Instruction::Add)
483 continue;
Sam Parker85ad78b2019-07-11 07:47:50 +0000484
Sam Parker2200a9b2019-07-31 07:32:03 +0000485 if (AllAdds.count(&I))
486 continue;
Sam Parker85ad78b2019-07-11 07:47:50 +0000487
Sam Parker2200a9b2019-07-31 07:32:03 +0000488 const auto *Ty = I.getType();
489 if (!Ty->isIntegerTy(32) && !Ty->isIntegerTy(64))
490 continue;
Sam Parker85ad78b2019-07-11 07:47:50 +0000491
Sam Parker2200a9b2019-07-31 07:32:03 +0000492 Reduction R(&I);
493 if (!Search(&I, &BB, R))
494 continue;
Sam Parker85ad78b2019-07-11 07:47:50 +0000495
Sam Parker2200a9b2019-07-31 07:32:03 +0000496 if (!CreateParallelPairs(R))
497 continue;
498
499 InsertParallelMACs(R);
500 Changed = true;
501 AllAdds.insert(R.getAdds().begin(), R.getAdds().end());
502 }
Sam Parker85ad78b2019-07-11 07:47:50 +0000503 }
504
505 return Changed;
506}
507
508bool ARMParallelDSP::CreateParallelPairs(Reduction &R) {
509
510 // Not enough mul operations to make a pair.
511 if (R.getMuls().size() < 2)
512 return false;
513
514 // Check that the muls operate directly upon sign extended loads.
Sam Parker414dd1c2019-07-29 08:41:51 +0000515 for (auto &MulCand : R.getMuls()) {
516 if (!MulCand->HasTwoLoadInputs())
Sam Parker85ad78b2019-07-11 07:47:50 +0000517 return false;
Sam Parker85ad78b2019-07-11 07:47:50 +0000518 }
519
Sam Parker414dd1c2019-07-29 08:41:51 +0000520 auto CanPair = [&](Reduction &R, MulCandidate *PMul0, MulCandidate *PMul1) {
Sam Parker453ba912018-11-09 09:18:00 +0000521 // The first elements of each vector should be loads with sexts. If we
522 // find that its two pairs of consecutive loads, then these can be
523 // transformed into two wider loads and the users can be replaced with
524 // DSP intrinsics.
Sam Parker414dd1c2019-07-29 08:41:51 +0000525 auto Ld0 = static_cast<LoadInst*>(PMul0->LHS);
526 auto Ld1 = static_cast<LoadInst*>(PMul1->LHS);
527 auto Ld2 = static_cast<LoadInst*>(PMul0->RHS);
528 auto Ld3 = static_cast<LoadInst*>(PMul1->RHS);
Sam Parker453ba912018-11-09 09:18:00 +0000529
Sam Parker414dd1c2019-07-29 08:41:51 +0000530 LLVM_DEBUG(dbgs() << "Loads:\n"
531 << " - " << *Ld0 << "\n"
532 << " - " << *Ld1 << "\n"
533 << " - " << *Ld2 << "\n"
534 << " - " << *Ld3 << "\n");
Sam Parker453ba912018-11-09 09:18:00 +0000535
Sam Parker414dd1c2019-07-29 08:41:51 +0000536 if (AreSequentialLoads(Ld0, Ld1, PMul0->VecLd)) {
537 if (AreSequentialLoads(Ld2, Ld3, PMul1->VecLd)) {
Sam Parker453ba912018-11-09 09:18:00 +0000538 LLVM_DEBUG(dbgs() << "OK: found two pairs of parallel loads!\n");
Sam Parker414dd1c2019-07-29 08:41:51 +0000539 R.AddMulPair(PMul0, PMul1);
540 return true;
541 } else if (AreSequentialLoads(Ld3, Ld2, PMul1->VecLd)) {
542 LLVM_DEBUG(dbgs() << "OK: found two pairs of parallel loads!\n");
543 LLVM_DEBUG(dbgs() << " exchanging Ld2 and Ld3\n");
544 PMul1->Exchange = true;
545 R.AddMulPair(PMul0, PMul1);
Sam Parker453ba912018-11-09 09:18:00 +0000546 return true;
547 }
Sam Parker414dd1c2019-07-29 08:41:51 +0000548 } else if (AreSequentialLoads(Ld1, Ld0, PMul0->VecLd) &&
549 AreSequentialLoads(Ld2, Ld3, PMul1->VecLd)) {
550 LLVM_DEBUG(dbgs() << "OK: found two pairs of parallel loads!\n");
551 LLVM_DEBUG(dbgs() << " exchanging Ld0 and Ld1\n");
552 LLVM_DEBUG(dbgs() << " and swapping muls\n");
553 PMul0->Exchange = true;
554 // Only the second operand can be exchanged, so swap the muls.
555 R.AddMulPair(PMul1, PMul0);
556 return true;
Sam Parker453ba912018-11-09 09:18:00 +0000557 }
558 return false;
559 };
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000560
Sam Parker414dd1c2019-07-29 08:41:51 +0000561 MulCandList &Muls = R.getMuls();
Sam Parker85ad78b2019-07-11 07:47:50 +0000562 const unsigned Elems = Muls.size();
Sam Parkera023c7a2018-09-12 09:17:44 +0000563 SmallPtrSet<const Instruction*, 4> Paired;
564 for (unsigned i = 0; i < Elems; ++i) {
Sam Parker414dd1c2019-07-29 08:41:51 +0000565 MulCandidate *PMul0 = static_cast<MulCandidate*>(Muls[i].get());
Sam Parkera023c7a2018-09-12 09:17:44 +0000566 if (Paired.count(PMul0->Root))
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000567 continue;
568
Sam Parkera023c7a2018-09-12 09:17:44 +0000569 for (unsigned j = 0; j < Elems; ++j) {
570 if (i == j)
571 continue;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000572
Sam Parker414dd1c2019-07-29 08:41:51 +0000573 MulCandidate *PMul1 = static_cast<MulCandidate*>(Muls[j].get());
Sam Parkera023c7a2018-09-12 09:17:44 +0000574 if (Paired.count(PMul1->Root))
575 continue;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000576
Sam Parkera023c7a2018-09-12 09:17:44 +0000577 const Instruction *Mul0 = PMul0->Root;
578 const Instruction *Mul1 = PMul1->Root;
579 if (Mul0 == Mul1)
580 continue;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000581
Sam Parkera023c7a2018-09-12 09:17:44 +0000582 assert(PMul0 != PMul1 && "expected different chains");
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000583
Sam Parker85ad78b2019-07-11 07:47:50 +0000584 if (CanPair(R, PMul0, PMul1)) {
Sam Parkera023c7a2018-09-12 09:17:44 +0000585 Paired.insert(Mul0);
586 Paired.insert(Mul1);
587 break;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000588 }
589 }
590 }
Sam Parker85ad78b2019-07-11 07:47:50 +0000591 return !R.getMulPairs().empty();
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000592}
593
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000594
Sam Parker85ad78b2019-07-11 07:47:50 +0000595void ARMParallelDSP::InsertParallelMACs(Reduction &R) {
596
597 auto CreateSMLADCall = [&](SmallVectorImpl<LoadInst*> &VecLd0,
598 SmallVectorImpl<LoadInst*> &VecLd1,
599 Value *Acc, bool Exchange,
600 Instruction *InsertAfter) {
601 // Replace the reduction chain with an intrinsic call
602 IntegerType *Ty = IntegerType::get(M->getContext(), 32);
603 LoadInst *WideLd0 = WideLoads.count(VecLd0[0]) ?
604 WideLoads[VecLd0[0]]->getLoad() : CreateWideLoad(VecLd0, Ty);
605 LoadInst *WideLd1 = WideLoads.count(VecLd1[0]) ?
606 WideLoads[VecLd1[0]]->getLoad() : CreateWideLoad(VecLd1, Ty);
607
608 Value* Args[] = { WideLd0, WideLd1, Acc };
609 Function *SMLAD = nullptr;
610 if (Exchange)
611 SMLAD = Acc->getType()->isIntegerTy(32) ?
612 Intrinsic::getDeclaration(M, Intrinsic::arm_smladx) :
613 Intrinsic::getDeclaration(M, Intrinsic::arm_smlaldx);
614 else
615 SMLAD = Acc->getType()->isIntegerTy(32) ?
616 Intrinsic::getDeclaration(M, Intrinsic::arm_smlad) :
617 Intrinsic::getDeclaration(M, Intrinsic::arm_smlald);
618
619 IRBuilder<NoFolder> Builder(InsertAfter->getParent(),
620 ++BasicBlock::iterator(InsertAfter));
621 Instruction *Call = Builder.CreateCall(SMLAD, Args);
622 NumSMLAD++;
623 return Call;
624 };
625
626 Instruction *InsertAfter = R.getRoot();
627 Value *Acc = R.getAccumulator();
628 if (!Acc)
629 Acc = ConstantInt::get(IntegerType::get(M->getContext(), 32), 0);
630
631 LLVM_DEBUG(dbgs() << "Root: " << *InsertAfter << "\n"
632 << "Acc: " << *Acc << "\n");
633 for (auto &Pair : R.getMulPairs()) {
Sam Parker414dd1c2019-07-29 08:41:51 +0000634 MulCandidate *PMul0 = Pair.first;
635 MulCandidate *PMul1 = Pair.second;
Sam Parker85ad78b2019-07-11 07:47:50 +0000636 LLVM_DEBUG(dbgs() << "Muls:\n"
Sam Parkera33e3112019-05-13 09:23:32 +0000637 << "- " << *PMul0->Root << "\n"
638 << "- " << *PMul1->Root << "\n");
Sam Parkera023c7a2018-09-12 09:17:44 +0000639
Sam Parker4c4ff132019-03-14 11:14:13 +0000640 Acc = CreateSMLADCall(PMul0->VecLd, PMul1->VecLd, Acc, PMul1->Exchange,
641 InsertAfter);
Sam Parker85ad78b2019-07-11 07:47:50 +0000642 InsertAfter = cast<Instruction>(Acc);
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000643 }
Sam Parker85ad78b2019-07-11 07:47:50 +0000644 R.UpdateRoot(cast<Instruction>(Acc));
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000645}
646
Sam Parkera33e3112019-05-13 09:23:32 +0000647LoadInst* ARMParallelDSP::CreateWideLoad(SmallVectorImpl<LoadInst*> &Loads,
648 IntegerType *LoadTy) {
Sam Parker4c4ff132019-03-14 11:14:13 +0000649 assert(Loads.size() == 2 && "currently only support widening two loads");
Sam Parkera33e3112019-05-13 09:23:32 +0000650
651 LoadInst *Base = Loads[0];
652 LoadInst *Offset = Loads[1];
653
654 Instruction *BaseSExt = dyn_cast<SExtInst>(Base->user_back());
655 Instruction *OffsetSExt = dyn_cast<SExtInst>(Offset->user_back());
656
657 assert((BaseSExt && OffsetSExt)
658 && "Loads should have a single, extending, user");
659
660 std::function<void(Value*, Value*)> MoveBefore =
661 [&](Value *A, Value *B) -> void {
662 if (!isa<Instruction>(A) || !isa<Instruction>(B))
663 return;
664
665 auto *Source = cast<Instruction>(A);
666 auto *Sink = cast<Instruction>(B);
667
668 if (DT->dominates(Source, Sink) ||
669 Source->getParent() != Sink->getParent() ||
670 isa<PHINode>(Source) || isa<PHINode>(Sink))
671 return;
672
673 Source->moveBefore(Sink);
Sam Parkeraeb21b92019-07-24 09:38:39 +0000674 for (auto &Op : Source->operands())
675 MoveBefore(Op, Source);
Sam Parkera33e3112019-05-13 09:23:32 +0000676 };
677
678 // Insert the load at the point of the original dominating load.
679 LoadInst *DomLoad = DT->dominates(Base, Offset) ? Base : Offset;
680 IRBuilder<NoFolder> IRB(DomLoad->getParent(),
681 ++BasicBlock::iterator(DomLoad));
682
683 // Bitcast the pointer to a wider type and create the wide load, while making
684 // sure to maintain the original alignment as this prevents ldrd from being
685 // generated when it could be illegal due to memory alignment.
686 const unsigned AddrSpace = DomLoad->getPointerAddressSpace();
687 Value *VecPtr = IRB.CreateBitCast(Base->getPointerOperand(),
Eli Friedmanb09c7782018-10-18 19:34:30 +0000688 LoadTy->getPointerTo(AddrSpace));
Sam Parker4c4ff132019-03-14 11:14:13 +0000689 LoadInst *WideLoad = IRB.CreateAlignedLoad(LoadTy, VecPtr,
Sam Parkera33e3112019-05-13 09:23:32 +0000690 Base->getAlignment());
Sam Parker4c4ff132019-03-14 11:14:13 +0000691
Sam Parkera33e3112019-05-13 09:23:32 +0000692 // Make sure everything is in the correct order in the basic block.
693 MoveBefore(Base->getPointerOperand(), VecPtr);
694 MoveBefore(VecPtr, WideLoad);
Sam Parker4c4ff132019-03-14 11:14:13 +0000695
696 // From the wide load, create two values that equal the original two loads.
Sam Parkera33e3112019-05-13 09:23:32 +0000697 // Loads[0] needs trunc while Loads[1] needs a lshr and trunc.
698 // TODO: Support big-endian as well.
699 Value *Bottom = IRB.CreateTrunc(WideLoad, Base->getType());
700 BaseSExt->setOperand(0, Bottom);
Sam Parker4c4ff132019-03-14 11:14:13 +0000701
Sam Parkera33e3112019-05-13 09:23:32 +0000702 IntegerType *OffsetTy = cast<IntegerType>(Offset->getType());
703 Value *ShiftVal = ConstantInt::get(LoadTy, OffsetTy->getBitWidth());
Sam Parker4c4ff132019-03-14 11:14:13 +0000704 Value *Top = IRB.CreateLShr(WideLoad, ShiftVal);
Sam Parkera33e3112019-05-13 09:23:32 +0000705 Value *Trunc = IRB.CreateTrunc(Top, OffsetTy);
706 OffsetSExt->setOperand(0, Trunc);
Sam Parker4c4ff132019-03-14 11:14:13 +0000707
Sam Parkera33e3112019-05-13 09:23:32 +0000708 WideLoads.emplace(std::make_pair(Base,
Sam Parker4c4ff132019-03-14 11:14:13 +0000709 make_unique<WidenedLoad>(Loads, WideLoad)));
710 return WideLoad;
Eli Friedmanb09c7782018-10-18 19:34:30 +0000711}
712
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000713Pass *llvm::createARMParallelDSPPass() {
714 return new ARMParallelDSP();
715}
716
717char ARMParallelDSP::ID = 0;
718
Sjoerd Meijerb3e06fa2018-07-06 14:47:09 +0000719INITIALIZE_PASS_BEGIN(ARMParallelDSP, "arm-parallel-dsp",
Sam Parker2200a9b2019-07-31 07:32:03 +0000720 "Transform functions to use DSP intrinsics", false, false)
Sjoerd Meijerb3e06fa2018-07-06 14:47:09 +0000721INITIALIZE_PASS_END(ARMParallelDSP, "arm-parallel-dsp",
Sam Parker2200a9b2019-07-31 07:32:03 +0000722 "Transform functions to use DSP intrinsics", false, false)