blob: beb44fb12e9571024c2af5459d6d94ba164c006d [file] [log] [blame]
Sjoerd Meijerc89ca552018-06-28 12:55:29 +00001//===- ParallelDSP.cpp - Parallel DSP Pass --------------------------------===//
2//
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"
21#include "llvm/Analysis/LoopPass.h"
22#include "llvm/Analysis/LoopInfo.h"
23#include "llvm/IR/Instructions.h"
24#include "llvm/IR/NoFolder.h"
25#include "llvm/Transforms/Scalar.h"
26#include "llvm/Transforms/Utils/BasicBlockUtils.h"
27#include "llvm/Transforms/Utils/LoopUtils.h"
28#include "llvm/Pass.h"
29#include "llvm/PassRegistry.h"
30#include "llvm/PassSupport.h"
31#include "llvm/Support/Debug.h"
32#include "llvm/IR/PatternMatch.h"
33#include "llvm/CodeGen/TargetPassConfig.h"
34#include "ARM.h"
35#include "ARMSubtarget.h"
36
37using namespace llvm;
38using namespace PatternMatch;
39
Sjoerd Meijerb3e06fa2018-07-06 14:47:09 +000040#define DEBUG_TYPE "arm-parallel-dsp"
41
42STATISTIC(NumSMLAD , "Number of smlad instructions generated");
Sjoerd Meijerc89ca552018-06-28 12:55:29 +000043
Sjoerd Meijer3c859b32018-08-14 07:43:49 +000044static cl::opt<bool>
45DisableParallelDSP("disable-arm-parallel-dsp", cl::Hidden, cl::init(false),
46 cl::desc("Disable the ARM Parallel DSP pass"));
47
Sjoerd Meijerc89ca552018-06-28 12:55:29 +000048namespace {
Sam Parker89a37992018-07-23 15:25:59 +000049 struct OpChain;
50 struct BinOpChain;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +000051 struct Reduction;
52
Fangrui Song58407ca2018-07-23 17:43:21 +000053 using OpChainList = SmallVector<std::unique_ptr<OpChain>, 8>;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +000054 using ReductionList = SmallVector<Reduction, 8>;
55 using ValueList = SmallVector<Value*, 8>;
Sam Parker4c4ff132019-03-14 11:14:13 +000056 using MemInstList = SmallVector<LoadInst*, 8>;
Sam Parker89a37992018-07-23 15:25:59 +000057 using PMACPair = std::pair<BinOpChain*,BinOpChain*>;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +000058 using PMACPairList = SmallVector<PMACPair, 8>;
59 using Instructions = SmallVector<Instruction*,16>;
60 using MemLocList = SmallVector<MemoryLocation, 4>;
61
Sam Parker89a37992018-07-23 15:25:59 +000062 struct OpChain {
63 Instruction *Root;
64 ValueList AllValues;
Eli Friedmanb09c7782018-10-18 19:34:30 +000065 MemInstList VecLd; // List of all load instructions.
Sam Parkera33e3112019-05-13 09:23:32 +000066 MemInstList Loads;
Sam Parker89a37992018-07-23 15:25:59 +000067 bool ReadOnly = true;
68
69 OpChain(Instruction *I, ValueList &vl) : Root(I), AllValues(vl) { }
Jordan Rupprechte5daf612018-07-23 17:38:05 +000070 virtual ~OpChain() = default;
Sam Parker89a37992018-07-23 15:25:59 +000071
Sam Parkera33e3112019-05-13 09:23:32 +000072 void PopulateLoads() {
Sam Parker89a37992018-07-23 15:25:59 +000073 for (auto *V : AllValues) {
Sam Parkera33e3112019-05-13 09:23:32 +000074 if (auto *Ld = dyn_cast<LoadInst>(V))
75 Loads.push_back(Ld);
Sam Parker89a37992018-07-23 15:25:59 +000076 }
77 }
78
79 unsigned size() const { return AllValues.size(); }
80 };
81
82 // 'BinOpChain' and 'Reduction' are just some bookkeeping data structures.
Sjoerd Meijerc89ca552018-06-28 12:55:29 +000083 // 'Reduction' contains the phi-node and accumulator statement from where we
Sam Parker89a37992018-07-23 15:25:59 +000084 // start pattern matching, and 'BinOpChain' the multiplication
Sjoerd Meijerc89ca552018-06-28 12:55:29 +000085 // instructions that are candidates for parallel execution.
Sam Parker89a37992018-07-23 15:25:59 +000086 struct BinOpChain : public OpChain {
87 ValueList LHS; // List of all (narrow) left hand operands.
88 ValueList RHS; // List of all (narrow) right hand operands.
Sam Parkera023c7a2018-09-12 09:17:44 +000089 bool Exchange = false;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +000090
Sam Parker89a37992018-07-23 15:25:59 +000091 BinOpChain(Instruction *I, ValueList &lhs, ValueList &rhs) :
92 OpChain(I, lhs), LHS(lhs), RHS(rhs) {
93 for (auto *V : RHS)
94 AllValues.push_back(V);
95 }
Sam Parker453ba912018-11-09 09:18:00 +000096
97 bool AreSymmetrical(BinOpChain *Other);
Sjoerd Meijerc89ca552018-06-28 12:55:29 +000098 };
99
100 struct Reduction {
101 PHINode *Phi; // The Phi-node from where we start
102 // pattern matching.
103 Instruction *AccIntAdd; // The accumulating integer add statement,
104 // i.e, the reduction statement.
Sam Parker89a37992018-07-23 15:25:59 +0000105 OpChainList MACCandidates; // The MAC candidates associated with
Sjoerd Meijer53449da2018-07-11 12:36:25 +0000106 // this reduction statement.
Sam Parker453ba912018-11-09 09:18:00 +0000107 PMACPairList PMACPairs;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000108 Reduction (PHINode *P, Instruction *Acc) : Phi(P), AccIntAdd(Acc) { };
109 };
110
Sam Parker4c4ff132019-03-14 11:14:13 +0000111 class WidenedLoad {
112 LoadInst *NewLd = nullptr;
113 SmallVector<LoadInst*, 4> Loads;
114
115 public:
116 WidenedLoad(SmallVectorImpl<LoadInst*> &Lds, LoadInst *Wide)
117 : NewLd(Wide) {
118 for (auto *I : Lds)
119 Loads.push_back(I);
120 }
121 LoadInst *getLoad() {
122 return NewLd;
123 }
124 };
125
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000126 class ARMParallelDSP : public LoopPass {
127 ScalarEvolution *SE;
128 AliasAnalysis *AA;
129 TargetLibraryInfo *TLI;
130 DominatorTree *DT;
131 LoopInfo *LI;
132 Loop *L;
133 const DataLayout *DL;
134 Module *M;
Sam Parker453ba912018-11-09 09:18:00 +0000135 std::map<LoadInst*, LoadInst*> LoadPairs;
Sam Parker4c4ff132019-03-14 11:14:13 +0000136 std::map<LoadInst*, std::unique_ptr<WidenedLoad>> WideLoads;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000137
Sam Parkera33e3112019-05-13 09:23:32 +0000138 bool RecordMemoryOps(BasicBlock *BB);
Sam Parker453ba912018-11-09 09:18:00 +0000139 bool InsertParallelMACs(Reduction &Reduction);
Fangrui Song68169342018-07-03 19:12:27 +0000140 bool AreSequentialLoads(LoadInst *Ld0, LoadInst *Ld1, MemInstList &VecMem);
Sam Parkera33e3112019-05-13 09:23:32 +0000141 LoadInst* CreateWideLoad(SmallVectorImpl<LoadInst*> &Loads,
142 IntegerType *LoadTy);
Sam Parker453ba912018-11-09 09:18:00 +0000143 void CreateParallelMACPairs(Reduction &R);
Sam Parker4c4ff132019-03-14 11:14:13 +0000144 Instruction *CreateSMLADCall(SmallVectorImpl<LoadInst*> &VecLd0,
145 SmallVectorImpl<LoadInst*> &VecLd1,
Sam Parkera023c7a2018-09-12 09:17:44 +0000146 Instruction *Acc, bool Exchange,
147 Instruction *InsertAfter);
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000148
149 /// Try to match and generate: SMLAD, SMLADX - Signed Multiply Accumulate
150 /// Dual performs two signed 16x16-bit multiplications. It adds the
151 /// products to a 32-bit accumulate operand. Optionally, the instruction can
152 /// exchange the halfwords of the second operand before performing the
153 /// arithmetic.
154 bool MatchSMLAD(Function &F);
155
156 public:
157 static char ID;
158
159 ARMParallelDSP() : LoopPass(ID) { }
160
Sam Parkera33e3112019-05-13 09:23:32 +0000161 bool doInitialization(Loop *L, LPPassManager &LPM) override {
162 LoadPairs.clear();
163 WideLoads.clear();
164 return true;
165 }
166
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000167 void getAnalysisUsage(AnalysisUsage &AU) const override {
168 LoopPass::getAnalysisUsage(AU);
169 AU.addRequired<AssumptionCacheTracker>();
170 AU.addRequired<ScalarEvolutionWrapperPass>();
171 AU.addRequired<AAResultsWrapperPass>();
172 AU.addRequired<TargetLibraryInfoWrapperPass>();
173 AU.addRequired<LoopInfoWrapperPass>();
174 AU.addRequired<DominatorTreeWrapperPass>();
175 AU.addRequired<TargetPassConfig>();
176 AU.addPreserved<LoopInfoWrapperPass>();
177 AU.setPreservesCFG();
178 }
179
180 bool runOnLoop(Loop *TheLoop, LPPassManager &) override {
Sjoerd Meijer3c859b32018-08-14 07:43:49 +0000181 if (DisableParallelDSP)
182 return false;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000183 L = TheLoop;
184 SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
185 AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
186 TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
187 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
188 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
189 auto &TPC = getAnalysis<TargetPassConfig>();
190
191 BasicBlock *Header = TheLoop->getHeader();
192 if (!Header)
193 return false;
194
195 // TODO: We assume the loop header and latch to be the same block.
196 // This is not a fundamental restriction, but lifting this would just
197 // require more work to do the transformation and then patch up the CFG.
198 if (Header != TheLoop->getLoopLatch()) {
199 LLVM_DEBUG(dbgs() << "The loop header is not the loop latch: not "
200 "running pass ARMParallelDSP\n");
201 return false;
202 }
203
Sam Parker9e730202019-03-15 10:19:32 +0000204 // We need a preheader as getIncomingValueForBlock assumes there is one.
205 if (!TheLoop->getLoopPreheader()) {
206 LLVM_DEBUG(dbgs() << "No preheader found, bailing out\n");
207 return false;
208 }
209
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000210 Function &F = *Header->getParent();
211 M = F.getParent();
212 DL = &M->getDataLayout();
213
214 auto &TM = TPC.getTM<TargetMachine>();
215 auto *ST = &TM.getSubtarget<ARMSubtarget>(F);
216
217 if (!ST->allowsUnalignedMem()) {
218 LLVM_DEBUG(dbgs() << "Unaligned memory access not supported: not "
219 "running pass ARMParallelDSP\n");
220 return false;
221 }
222
223 if (!ST->hasDSP()) {
224 LLVM_DEBUG(dbgs() << "DSP extension not enabled: not running pass "
225 "ARMParallelDSP\n");
226 return false;
227 }
228
Sam Parker9e730202019-03-15 10:19:32 +0000229 if (!ST->isLittle()) {
230 LLVM_DEBUG(dbgs() << "Only supporting little endian: not running pass "
Sam Parkera33e3112019-05-13 09:23:32 +0000231 << "ARMParallelDSP\n");
Sam Parker9e730202019-03-15 10:19:32 +0000232 return false;
233 }
234
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000235 LoopAccessInfo LAI(L, SE, TLI, AA, DT, LI);
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000236
Sam Parkera023c7a2018-09-12 09:17:44 +0000237 LLVM_DEBUG(dbgs() << "\n== Parallel DSP pass ==\n");
238 LLVM_DEBUG(dbgs() << " - " << F.getName() << "\n\n");
Sam Parker453ba912018-11-09 09:18:00 +0000239
Sam Parkera33e3112019-05-13 09:23:32 +0000240 if (!RecordMemoryOps(Header)) {
Sam Parker453ba912018-11-09 09:18:00 +0000241 LLVM_DEBUG(dbgs() << " - No sequential loads found.\n");
242 return false;
243 }
244
Sam Parker4c4ff132019-03-14 11:14:13 +0000245 bool Changes = MatchSMLAD(F);
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000246 return Changes;
247 }
248 };
249}
250
Sjoerd Meijer27be58b2018-07-05 08:21:40 +0000251// MaxBitwidth: the maximum supported bitwidth of the elements in the DSP
252// instructions, which is set to 16. So here we should collect all i8 and i16
253// narrow operations.
254// TODO: we currently only collect i16, and will support i8 later, so that's
255// why we check that types are equal to MaxBitWidth, and not <= MaxBitWidth.
256template<unsigned MaxBitWidth>
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000257static bool IsNarrowSequence(Value *V, ValueList &VL) {
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000258 ConstantInt *CInt;
259
260 if (match(V, m_ConstantInt(CInt))) {
261 // TODO: if a constant is used, it needs to fit within the bit width.
262 return false;
263 }
264
265 auto *I = dyn_cast<Instruction>(V);
266 if (!I)
267 return false;
268
269 Value *Val, *LHS, *RHS;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000270 if (match(V, m_Trunc(m_Value(Val)))) {
Sjoerd Meijer27be58b2018-07-05 08:21:40 +0000271 if (cast<TruncInst>(I)->getDestTy()->getIntegerBitWidth() == MaxBitWidth)
272 return IsNarrowSequence<MaxBitWidth>(Val, VL);
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000273 } else if (match(V, m_Add(m_Value(LHS), m_Value(RHS)))) {
274 // TODO: we need to implement sadd16/sadd8 for this, which enables to
275 // also do the rewrite for smlad8.ll, but it is unsupported for now.
Sjoerd Meijer27be58b2018-07-05 08:21:40 +0000276 return false;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000277 } else if (match(V, m_ZExtOrSExt(m_Value(Val)))) {
Sam Parker4c4ff132019-03-14 11:14:13 +0000278 if (cast<CastInst>(I)->getSrcTy()->getIntegerBitWidth() != MaxBitWidth)
Sjoerd Meijer27be58b2018-07-05 08:21:40 +0000279 return false;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000280
Sjoerd Meijer27be58b2018-07-05 08:21:40 +0000281 if (match(Val, m_Load(m_Value()))) {
Sjoerd Meijer27be58b2018-07-05 08:21:40 +0000282 VL.push_back(Val);
283 VL.push_back(I);
284 return true;
285 }
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000286 }
Sjoerd Meijer27be58b2018-07-05 08:21:40 +0000287 return false;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000288}
289
Sam Parkerffc16812018-07-03 12:44:16 +0000290template<typename MemInst>
291static bool AreSequentialAccesses(MemInst *MemOp0, MemInst *MemOp1,
Sam Parker453ba912018-11-09 09:18:00 +0000292 const DataLayout &DL, ScalarEvolution &SE) {
Sam Parker4c4ff132019-03-14 11:14:13 +0000293 if (isConsecutiveAccess(MemOp0, MemOp1, DL, SE))
Sam Parkerffc16812018-07-03 12:44:16 +0000294 return true;
Sam Parkerffc16812018-07-03 12:44:16 +0000295 return false;
296}
297
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000298bool ARMParallelDSP::AreSequentialLoads(LoadInst *Ld0, LoadInst *Ld1,
Sam Parkerffc16812018-07-03 12:44:16 +0000299 MemInstList &VecMem) {
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000300 if (!Ld0 || !Ld1)
301 return false;
302
Sam Parker4c4ff132019-03-14 11:14:13 +0000303 if (!LoadPairs.count(Ld0) || LoadPairs[Ld0] != Ld1)
304 return false;
305
306 LLVM_DEBUG(dbgs() << "Loads are sequential and valid:\n";
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000307 dbgs() << "Ld0:"; Ld0->dump();
308 dbgs() << "Ld1:"; Ld1->dump();
309 );
310
Sam Parker453ba912018-11-09 09:18:00 +0000311 VecMem.clear();
312 VecMem.push_back(Ld0);
313 VecMem.push_back(Ld1);
314 return true;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000315}
316
Sam Parkera33e3112019-05-13 09:23:32 +0000317/// Iterate through the block and record base, offset pairs of loads which can
318/// be widened into a single load.
319bool ARMParallelDSP::RecordMemoryOps(BasicBlock *BB) {
Sam Parker453ba912018-11-09 09:18:00 +0000320 SmallVector<LoadInst*, 8> Loads;
Sam Parkera33e3112019-05-13 09:23:32 +0000321 SmallVector<Instruction*, 8> Writes;
322
323 // Collect loads and instruction that may write to memory. For now we only
324 // record loads which are simple, sign-extended and have a single user.
325 // TODO: Allow zero-extended loads.
Sam Parker4c4ff132019-03-14 11:14:13 +0000326 for (auto &I : *BB) {
Sam Parkera33e3112019-05-13 09:23:32 +0000327 if (I.mayWriteToMemory())
328 Writes.push_back(&I);
Sam Parker453ba912018-11-09 09:18:00 +0000329 auto *Ld = dyn_cast<LoadInst>(&I);
Sam Parker4c4ff132019-03-14 11:14:13 +0000330 if (!Ld || !Ld->isSimple() ||
331 !Ld->hasOneUse() || !isa<SExtInst>(Ld->user_back()))
Sam Parker453ba912018-11-09 09:18:00 +0000332 continue;
333 Loads.push_back(Ld);
334 }
335
Sam Parkera33e3112019-05-13 09:23:32 +0000336 using InstSet = std::set<Instruction*>;
337 using DepMap = std::map<Instruction*, InstSet>;
338 DepMap RAWDeps;
339
340 // Record any writes that may alias a load.
341 const auto Size = LocationSize::unknown();
342 for (auto Read : Loads) {
343 for (auto Write : Writes) {
344 MemoryLocation ReadLoc =
345 MemoryLocation(Read->getPointerOperand(), Size);
346
347 if (!isModOrRefSet(intersectModRef(AA->getModRefInfo(Write, ReadLoc),
348 ModRefInfo::ModRef)))
349 continue;
350 if (DT->dominates(Write, Read))
351 RAWDeps[Read].insert(Write);
352 }
353 }
354
355 // Check whether there's not a write between the two loads which would
356 // prevent them from being safely merged.
357 auto SafeToPair = [&](LoadInst *Base, LoadInst *Offset) {
358 LoadInst *Dominator = DT->dominates(Base, Offset) ? Base : Offset;
359 LoadInst *Dominated = DT->dominates(Base, Offset) ? Offset : Base;
360
361 if (RAWDeps.count(Dominated)) {
362 InstSet &WritesBefore = RAWDeps[Dominated];
363
364 for (auto Before : WritesBefore) {
365
366 // We can't move the second load backward, past a write, to merge
367 // with the first load.
368 if (DT->dominates(Dominator, Before))
369 return false;
370 }
371 }
372 return true;
373 };
374
375 // Record base, offset load pairs.
376 for (auto *Base : Loads) {
377 for (auto *Offset : Loads) {
378 if (Base == Offset)
Sam Parker453ba912018-11-09 09:18:00 +0000379 continue;
380
Sam Parkera33e3112019-05-13 09:23:32 +0000381 if (AreSequentialAccesses<LoadInst>(Base, Offset, *DL, *SE) &&
382 SafeToPair(Base, Offset)) {
383 LoadPairs[Base] = Offset;
Sam Parker4c4ff132019-03-14 11:14:13 +0000384 break;
Sam Parker453ba912018-11-09 09:18:00 +0000385 }
386 }
387 }
Sam Parker4c4ff132019-03-14 11:14:13 +0000388
389 LLVM_DEBUG(if (!LoadPairs.empty()) {
390 dbgs() << "Consecutive load pairs:\n";
391 for (auto &MapIt : LoadPairs) {
392 LLVM_DEBUG(dbgs() << *MapIt.first << ", "
393 << *MapIt.second << "\n");
394 }
395 });
Sam Parker453ba912018-11-09 09:18:00 +0000396 return LoadPairs.size() > 1;
397}
398
399void ARMParallelDSP::CreateParallelMACPairs(Reduction &R) {
400 OpChainList &Candidates = R.MACCandidates;
401 PMACPairList &PMACPairs = R.PMACPairs;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000402 const unsigned Elems = Candidates.size();
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000403
404 if (Elems < 2)
Sam Parker453ba912018-11-09 09:18:00 +0000405 return;
406
407 auto CanPair = [&](BinOpChain *PMul0, BinOpChain *PMul1) {
408 if (!PMul0->AreSymmetrical(PMul1))
409 return false;
410
411 // The first elements of each vector should be loads with sexts. If we
412 // find that its two pairs of consecutive loads, then these can be
413 // transformed into two wider loads and the users can be replaced with
414 // DSP intrinsics.
415 for (unsigned x = 0; x < PMul0->LHS.size(); x += 2) {
416 auto *Ld0 = dyn_cast<LoadInst>(PMul0->LHS[x]);
417 auto *Ld1 = dyn_cast<LoadInst>(PMul1->LHS[x]);
418 auto *Ld2 = dyn_cast<LoadInst>(PMul0->RHS[x]);
419 auto *Ld3 = dyn_cast<LoadInst>(PMul1->RHS[x]);
420
421 if (!Ld0 || !Ld1 || !Ld2 || !Ld3)
422 return false;
423
Sam Parker4c4ff132019-03-14 11:14:13 +0000424 LLVM_DEBUG(dbgs() << "Loads:\n"
425 << " - " << *Ld0 << "\n"
426 << " - " << *Ld1 << "\n"
427 << " - " << *Ld2 << "\n"
428 << " - " << *Ld3 << "\n");
Sam Parker453ba912018-11-09 09:18:00 +0000429
430 if (AreSequentialLoads(Ld0, Ld1, PMul0->VecLd)) {
431 if (AreSequentialLoads(Ld2, Ld3, PMul1->VecLd)) {
432 LLVM_DEBUG(dbgs() << "OK: found two pairs of parallel loads!\n");
433 PMACPairs.push_back(std::make_pair(PMul0, PMul1));
434 return true;
435 } else if (AreSequentialLoads(Ld3, Ld2, PMul1->VecLd)) {
436 LLVM_DEBUG(dbgs() << "OK: found two pairs of parallel loads!\n");
437 LLVM_DEBUG(dbgs() << " exchanging Ld2 and Ld3\n");
438 PMul1->Exchange = true;
439 PMACPairs.push_back(std::make_pair(PMul0, PMul1));
440 return true;
441 }
442 } else if (AreSequentialLoads(Ld1, Ld0, PMul0->VecLd) &&
443 AreSequentialLoads(Ld2, Ld3, PMul1->VecLd)) {
444 LLVM_DEBUG(dbgs() << "OK: found two pairs of parallel loads!\n");
445 LLVM_DEBUG(dbgs() << " exchanging Ld0 and Ld1\n");
446 LLVM_DEBUG(dbgs() << " and swapping muls\n");
447 PMul0->Exchange = true;
448 // Only the second operand can be exchanged, so swap the muls.
449 PMACPairs.push_back(std::make_pair(PMul1, PMul0));
450 return true;
451 }
452 }
453 return false;
454 };
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000455
Sam Parkera023c7a2018-09-12 09:17:44 +0000456 SmallPtrSet<const Instruction*, 4> Paired;
457 for (unsigned i = 0; i < Elems; ++i) {
Fangrui Song58407ca2018-07-23 17:43:21 +0000458 BinOpChain *PMul0 = static_cast<BinOpChain*>(Candidates[i].get());
Sam Parkera023c7a2018-09-12 09:17:44 +0000459 if (Paired.count(PMul0->Root))
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000460 continue;
461
Sam Parkera023c7a2018-09-12 09:17:44 +0000462 for (unsigned j = 0; j < Elems; ++j) {
463 if (i == j)
464 continue;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000465
Sam Parkera023c7a2018-09-12 09:17:44 +0000466 BinOpChain *PMul1 = static_cast<BinOpChain*>(Candidates[j].get());
467 if (Paired.count(PMul1->Root))
468 continue;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000469
Sam Parkera023c7a2018-09-12 09:17:44 +0000470 const Instruction *Mul0 = PMul0->Root;
471 const Instruction *Mul1 = PMul1->Root;
472 if (Mul0 == Mul1)
473 continue;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000474
Sam Parkera023c7a2018-09-12 09:17:44 +0000475 assert(PMul0 != PMul1 && "expected different chains");
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000476
Sam Parker453ba912018-11-09 09:18:00 +0000477 if (CanPair(PMul0, PMul1)) {
Sam Parkera023c7a2018-09-12 09:17:44 +0000478 Paired.insert(Mul0);
479 Paired.insert(Mul1);
480 break;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000481 }
482 }
483 }
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000484}
485
Sam Parker453ba912018-11-09 09:18:00 +0000486bool ARMParallelDSP::InsertParallelMACs(Reduction &Reduction) {
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000487 Instruction *Acc = Reduction.Phi;
488 Instruction *InsertAfter = Reduction.AccIntAdd;
489
Sam Parker453ba912018-11-09 09:18:00 +0000490 for (auto &Pair : Reduction.PMACPairs) {
Sam Parkera023c7a2018-09-12 09:17:44 +0000491 BinOpChain *PMul0 = Pair.first;
492 BinOpChain *PMul1 = Pair.second;
Sam Parkera33e3112019-05-13 09:23:32 +0000493 LLVM_DEBUG(dbgs() << "Found parallel MACs:\n"
494 << "- " << *PMul0->Root << "\n"
495 << "- " << *PMul1->Root << "\n");
Sam Parkera023c7a2018-09-12 09:17:44 +0000496
Sam Parker4c4ff132019-03-14 11:14:13 +0000497 Acc = CreateSMLADCall(PMul0->VecLd, PMul1->VecLd, Acc, PMul1->Exchange,
498 InsertAfter);
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000499 InsertAfter = Acc;
500 }
501
502 if (Acc != Reduction.Phi) {
503 LLVM_DEBUG(dbgs() << "Replace Accumulate: "; Acc->dump());
504 Reduction.AccIntAdd->replaceAllUsesWith(Acc);
505 return true;
506 }
507 return false;
508}
509
Sam Parker89a37992018-07-23 15:25:59 +0000510static void MatchParallelMACSequences(Reduction &R,
511 OpChainList &Candidates) {
Sam Parkera023c7a2018-09-12 09:17:44 +0000512 Instruction *Acc = R.AccIntAdd;
Sam Parker4c4ff132019-03-14 11:14:13 +0000513 LLVM_DEBUG(dbgs() << "\n- Analysing:\t" << *Acc << "\n");
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000514
Sam Parkera023c7a2018-09-12 09:17:44 +0000515 // Returns false to signal the search should be stopped.
516 std::function<bool(Value*)> Match =
517 [&Candidates, &Match](Value *V) -> bool {
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000518
Sam Parkera023c7a2018-09-12 09:17:44 +0000519 auto *I = dyn_cast<Instruction>(V);
Sam Parker11879112018-09-12 09:58:56 +0000520 if (!I)
Sam Parkera023c7a2018-09-12 09:17:44 +0000521 return false;
Sam Parker01db2982018-09-11 14:01:22 +0000522
Sam Parkera023c7a2018-09-12 09:17:44 +0000523 switch (I->getOpcode()) {
524 case Instruction::Add:
525 if (Match(I->getOperand(0)) || (Match(I->getOperand(1))))
526 return true;
527 break;
Sam Parker5338f7a2018-11-26 10:22:55 +0000528 case Instruction::Mul: {
529 Value *MulOp0 = I->getOperand(0);
530 Value *MulOp1 = I->getOperand(1);
Sam Parkera33e3112019-05-13 09:23:32 +0000531 if (isa<SExtInst>(MulOp0) && isa<SExtInst>(MulOp1)) {
532 ValueList LHS;
533 ValueList RHS;
534 if (IsNarrowSequence<16>(MulOp0, LHS) &&
535 IsNarrowSequence<16>(MulOp1, RHS)) {
536 Candidates.push_back(make_unique<BinOpChain>(I, LHS, RHS));
537 }
538 }
Sam Parker5338f7a2018-11-26 10:22:55 +0000539 return false;
540 }
Sam Parkera023c7a2018-09-12 09:17:44 +0000541 case Instruction::SExt:
Sam Parker5338f7a2018-11-26 10:22:55 +0000542 return Match(I->getOperand(0));
Sam Parkera023c7a2018-09-12 09:17:44 +0000543 }
544 return false;
545 };
546
547 while (Match (Acc));
548 LLVM_DEBUG(dbgs() << "Finished matching MAC sequences, found "
549 << Candidates.size() << " candidates.\n");
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000550}
551
Eli Friedmanb09c7782018-10-18 19:34:30 +0000552static bool CheckMACMemory(OpChainList &Candidates) {
Fangrui Song58407ca2018-07-23 17:43:21 +0000553 for (auto &C : Candidates) {
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000554 // A mul has 2 operands, and a narrow op consist of sext and a load; thus
555 // we expect at least 4 items in this operand value list.
Sam Parker89a37992018-07-23 15:25:59 +0000556 if (C->size() < 4) {
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000557 LLVM_DEBUG(dbgs() << "Operand list too short.\n");
558 return false;
559 }
Sam Parkera33e3112019-05-13 09:23:32 +0000560 C->PopulateLoads();
Fangrui Song58407ca2018-07-23 17:43:21 +0000561 ValueList &LHS = static_cast<BinOpChain*>(C.get())->LHS;
562 ValueList &RHS = static_cast<BinOpChain*>(C.get())->RHS;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000563
Sam Parker89a37992018-07-23 15:25:59 +0000564 // Use +=2 to skip over the expected extend instructions.
565 for (unsigned i = 0, e = LHS.size(); i < e; i += 2) {
566 if (!isa<LoadInst>(LHS[i]) || !isa<LoadInst>(RHS[i]))
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000567 return false;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000568 }
569 }
570 return true;
571}
572
573// Loop Pass that needs to identify integer add/sub reductions of 16-bit vector
574// multiplications.
575// To use SMLAD:
576// 1) we first need to find integer add reduction PHIs,
577// 2) then from the PHI, look for this pattern:
578//
579// acc0 = phi i32 [0, %entry], [%acc1, %loop.body]
580// ld0 = load i16
581// sext0 = sext i16 %ld0 to i32
582// ld1 = load i16
583// sext1 = sext i16 %ld1 to i32
584// mul0 = mul %sext0, %sext1
585// ld2 = load i16
586// sext2 = sext i16 %ld2 to i32
587// ld3 = load i16
588// sext3 = sext i16 %ld3 to i32
589// mul1 = mul i32 %sext2, %sext3
590// add0 = add i32 %mul0, %acc0
591// acc1 = add i32 %add0, %mul1
592//
593// Which can be selected to:
594//
595// ldr.h r0
596// ldr.h r1
597// smlad r2, r0, r1, r2
598//
599// If constants are used instead of loads, these will need to be hoisted
600// out and into a register.
601//
602// If loop invariants are used instead of loads, these need to be packed
603// before the loop begins.
604//
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000605bool ARMParallelDSP::MatchSMLAD(Function &F) {
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000606
Sam Parkera33e3112019-05-13 09:23:32 +0000607 auto FindReductions = [&](ReductionList &Reductions) {
608 RecurrenceDescriptor RecDesc;
609 const bool HasFnNoNaNAttr =
610 F.getFnAttribute("no-nans-fp-math").getValueAsString() == "true";
611 BasicBlock *Latch = L->getLoopLatch();
612
613 for (PHINode &Phi : Latch->phis()) {
614 const auto *Ty = Phi.getType();
615 if (!Ty->isIntegerTy(32) && !Ty->isIntegerTy(64))
616 continue;
617
618 const bool IsReduction = RecurrenceDescriptor::AddReductionVar(
619 &Phi, RecurrenceDescriptor::RK_IntegerAdd, L, HasFnNoNaNAttr, RecDesc);
620
621 if (!IsReduction)
622 continue;
623
624 Instruction *Acc = dyn_cast<Instruction>(Phi.getIncomingValueForBlock(Latch));
625 if (!Acc)
626 continue;
627
628 Reductions.push_back(Reduction(&Phi, Acc));
629 }
630 return !Reductions.empty();
631 };
632
Sam Parker89a37992018-07-23 15:25:59 +0000633 ReductionList Reductions;
Sam Parkera33e3112019-05-13 09:23:32 +0000634 if (!FindReductions(Reductions))
635 return false;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000636
637 for (auto &R : Reductions) {
Sam Parker89a37992018-07-23 15:25:59 +0000638 OpChainList MACCandidates;
639 MatchParallelMACSequences(R, MACCandidates);
Eli Friedmanb09c7782018-10-18 19:34:30 +0000640 if (!CheckMACMemory(MACCandidates))
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000641 continue;
Sam Parker89a37992018-07-23 15:25:59 +0000642
Fangrui Song58407ca2018-07-23 17:43:21 +0000643 R.MACCandidates = std::move(MACCandidates);
Sjoerd Meijer53449da2018-07-11 12:36:25 +0000644
645 LLVM_DEBUG(dbgs() << "MAC candidates:\n";
646 for (auto &M : R.MACCandidates)
Sam Parker89a37992018-07-23 15:25:59 +0000647 M->Root->dump();
Sjoerd Meijer53449da2018-07-11 12:36:25 +0000648 dbgs() << "\n";);
649 }
650
Sam Parkera33e3112019-05-13 09:23:32 +0000651 bool Changed = false;
652 // Check whether statements in the basic block that write to memory alias
653 // with the memory locations accessed by the MAC-chains.
Sjoerd Meijer53449da2018-07-11 12:36:25 +0000654 for (auto &R : Reductions) {
Sam Parker453ba912018-11-09 09:18:00 +0000655 CreateParallelMACPairs(R);
656 Changed |= InsertParallelMACs(R);
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000657 }
658
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000659 return Changed;
660}
661
Sam Parkera33e3112019-05-13 09:23:32 +0000662LoadInst* ARMParallelDSP::CreateWideLoad(SmallVectorImpl<LoadInst*> &Loads,
663 IntegerType *LoadTy) {
Sam Parker4c4ff132019-03-14 11:14:13 +0000664 assert(Loads.size() == 2 && "currently only support widening two loads");
Sam Parkera33e3112019-05-13 09:23:32 +0000665
666 LoadInst *Base = Loads[0];
667 LoadInst *Offset = Loads[1];
668
669 Instruction *BaseSExt = dyn_cast<SExtInst>(Base->user_back());
670 Instruction *OffsetSExt = dyn_cast<SExtInst>(Offset->user_back());
671
672 assert((BaseSExt && OffsetSExt)
673 && "Loads should have a single, extending, user");
674
675 std::function<void(Value*, Value*)> MoveBefore =
676 [&](Value *A, Value *B) -> void {
677 if (!isa<Instruction>(A) || !isa<Instruction>(B))
678 return;
679
680 auto *Source = cast<Instruction>(A);
681 auto *Sink = cast<Instruction>(B);
682
683 if (DT->dominates(Source, Sink) ||
684 Source->getParent() != Sink->getParent() ||
685 isa<PHINode>(Source) || isa<PHINode>(Sink))
686 return;
687
688 Source->moveBefore(Sink);
689 for (auto &U : Source->uses())
690 MoveBefore(Source, U.getUser());
691 };
692
693 // Insert the load at the point of the original dominating load.
694 LoadInst *DomLoad = DT->dominates(Base, Offset) ? Base : Offset;
695 IRBuilder<NoFolder> IRB(DomLoad->getParent(),
696 ++BasicBlock::iterator(DomLoad));
697
698 // Bitcast the pointer to a wider type and create the wide load, while making
699 // sure to maintain the original alignment as this prevents ldrd from being
700 // generated when it could be illegal due to memory alignment.
701 const unsigned AddrSpace = DomLoad->getPointerAddressSpace();
702 Value *VecPtr = IRB.CreateBitCast(Base->getPointerOperand(),
Eli Friedmanb09c7782018-10-18 19:34:30 +0000703 LoadTy->getPointerTo(AddrSpace));
Sam Parker4c4ff132019-03-14 11:14:13 +0000704 LoadInst *WideLoad = IRB.CreateAlignedLoad(LoadTy, VecPtr,
Sam Parkera33e3112019-05-13 09:23:32 +0000705 Base->getAlignment());
Sam Parker4c4ff132019-03-14 11:14:13 +0000706
Sam Parkera33e3112019-05-13 09:23:32 +0000707 // Make sure everything is in the correct order in the basic block.
708 MoveBefore(Base->getPointerOperand(), VecPtr);
709 MoveBefore(VecPtr, WideLoad);
Sam Parker4c4ff132019-03-14 11:14:13 +0000710
711 // From the wide load, create two values that equal the original two loads.
Sam Parkera33e3112019-05-13 09:23:32 +0000712 // Loads[0] needs trunc while Loads[1] needs a lshr and trunc.
713 // TODO: Support big-endian as well.
714 Value *Bottom = IRB.CreateTrunc(WideLoad, Base->getType());
715 BaseSExt->setOperand(0, Bottom);
Sam Parker4c4ff132019-03-14 11:14:13 +0000716
Sam Parkera33e3112019-05-13 09:23:32 +0000717 IntegerType *OffsetTy = cast<IntegerType>(Offset->getType());
718 Value *ShiftVal = ConstantInt::get(LoadTy, OffsetTy->getBitWidth());
Sam Parker4c4ff132019-03-14 11:14:13 +0000719 Value *Top = IRB.CreateLShr(WideLoad, ShiftVal);
Sam Parkera33e3112019-05-13 09:23:32 +0000720 Value *Trunc = IRB.CreateTrunc(Top, OffsetTy);
721 OffsetSExt->setOperand(0, Trunc);
Sam Parker4c4ff132019-03-14 11:14:13 +0000722
Sam Parkera33e3112019-05-13 09:23:32 +0000723 WideLoads.emplace(std::make_pair(Base,
Sam Parker4c4ff132019-03-14 11:14:13 +0000724 make_unique<WidenedLoad>(Loads, WideLoad)));
725 return WideLoad;
Eli Friedmanb09c7782018-10-18 19:34:30 +0000726}
727
Sam Parker4c4ff132019-03-14 11:14:13 +0000728Instruction *ARMParallelDSP::CreateSMLADCall(SmallVectorImpl<LoadInst*> &VecLd0,
729 SmallVectorImpl<LoadInst*> &VecLd1,
Sam Parkera023c7a2018-09-12 09:17:44 +0000730 Instruction *Acc, bool Exchange,
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000731 Instruction *InsertAfter) {
Sam Parkera023c7a2018-09-12 09:17:44 +0000732 LLVM_DEBUG(dbgs() << "Create SMLAD intrinsic using:\n"
Sam Parker4c4ff132019-03-14 11:14:13 +0000733 << "- " << *VecLd0[0] << "\n"
734 << "- " << *VecLd0[1] << "\n"
735 << "- " << *VecLd1[0] << "\n"
736 << "- " << *VecLd1[1] << "\n"
Sam Parkera023c7a2018-09-12 09:17:44 +0000737 << "- " << *Acc << "\n"
Sam Parker4c4ff132019-03-14 11:14:13 +0000738 << "- Exchange: " << Exchange << "\n");
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000739
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000740 // Replace the reduction chain with an intrinsic call
Sam Parker4c4ff132019-03-14 11:14:13 +0000741 IntegerType *Ty = IntegerType::get(M->getContext(), 32);
742 LoadInst *WideLd0 = WideLoads.count(VecLd0[0]) ?
Sam Parkera33e3112019-05-13 09:23:32 +0000743 WideLoads[VecLd0[0]]->getLoad() : CreateWideLoad(VecLd0, Ty);
Sam Parker4c4ff132019-03-14 11:14:13 +0000744 LoadInst *WideLd1 = WideLoads.count(VecLd1[0]) ?
Sam Parkera33e3112019-05-13 09:23:32 +0000745 WideLoads[VecLd1[0]]->getLoad() : CreateWideLoad(VecLd1, Ty);
746
Sam Parker4c4ff132019-03-14 11:14:13 +0000747 Value* Args[] = { WideLd0, WideLd1, Acc };
Sam Parkera023c7a2018-09-12 09:17:44 +0000748 Function *SMLAD = nullptr;
749 if (Exchange)
750 SMLAD = Acc->getType()->isIntegerTy(32) ?
751 Intrinsic::getDeclaration(M, Intrinsic::arm_smladx) :
752 Intrinsic::getDeclaration(M, Intrinsic::arm_smlaldx);
753 else
754 SMLAD = Acc->getType()->isIntegerTy(32) ?
755 Intrinsic::getDeclaration(M, Intrinsic::arm_smlad) :
756 Intrinsic::getDeclaration(M, Intrinsic::arm_smlald);
Sam Parkera33e3112019-05-13 09:23:32 +0000757
758 IRBuilder<NoFolder> Builder(InsertAfter->getParent(),
759 ++BasicBlock::iterator(InsertAfter));
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000760 CallInst *Call = Builder.CreateCall(SMLAD, Args);
Sjoerd Meijerb3e06fa2018-07-06 14:47:09 +0000761 NumSMLAD++;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000762 return Call;
763}
764
Sam Parker453ba912018-11-09 09:18:00 +0000765// Compare the value lists in Other to this chain.
766bool BinOpChain::AreSymmetrical(BinOpChain *Other) {
767 // Element-by-element comparison of Value lists returning true if they are
768 // instructions with the same opcode or constants with the same value.
769 auto CompareValueList = [](const ValueList &VL0,
770 const ValueList &VL1) {
771 if (VL0.size() != VL1.size()) {
772 LLVM_DEBUG(dbgs() << "Muls are mismatching operand list lengths: "
773 << VL0.size() << " != " << VL1.size() << "\n");
774 return false;
775 }
776
777 const unsigned Pairs = VL0.size();
Sam Parker453ba912018-11-09 09:18:00 +0000778
779 for (unsigned i = 0; i < Pairs; ++i) {
780 const Value *V0 = VL0[i];
781 const Value *V1 = VL1[i];
782 const auto *Inst0 = dyn_cast<Instruction>(V0);
783 const auto *Inst1 = dyn_cast<Instruction>(V1);
784
Sam Parker453ba912018-11-09 09:18:00 +0000785 if (!Inst0 || !Inst1)
786 return false;
787
Sam Parker4c4ff132019-03-14 11:14:13 +0000788 if (Inst0->isSameOperationAs(Inst1))
Sam Parker453ba912018-11-09 09:18:00 +0000789 continue;
Sam Parker453ba912018-11-09 09:18:00 +0000790
791 const APInt *C0, *C1;
792 if (!(match(V0, m_APInt(C0)) && match(V1, m_APInt(C1)) && C0 == C1))
793 return false;
794 }
795
Sam Parker453ba912018-11-09 09:18:00 +0000796 return true;
797 };
798
799 return CompareValueList(LHS, Other->LHS) &&
800 CompareValueList(RHS, Other->RHS);
801}
802
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000803Pass *llvm::createARMParallelDSPPass() {
804 return new ARMParallelDSP();
805}
806
807char ARMParallelDSP::ID = 0;
808
Sjoerd Meijerb3e06fa2018-07-06 14:47:09 +0000809INITIALIZE_PASS_BEGIN(ARMParallelDSP, "arm-parallel-dsp",
Simon Pilgrimc09b5e32018-06-28 18:37:16 +0000810 "Transform loops to use DSP intrinsics", false, false)
Sjoerd Meijerb3e06fa2018-07-06 14:47:09 +0000811INITIALIZE_PASS_END(ARMParallelDSP, "arm-parallel-dsp",
Simon Pilgrimc09b5e32018-06-28 18:37:16 +0000812 "Transform loops to use DSP intrinsics", false, false)