blob: ae5657a0a2c16a97b490a3b58b1d9c66cb628c3e [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"
Sam Parker1c3ca612019-10-16 09:37:03 +000021#include "llvm/Analysis/OrderedBasicBlock.h"
Sjoerd Meijerc89ca552018-06-28 12:55:29 +000022#include "llvm/IR/Instructions.h"
23#include "llvm/IR/NoFolder.h"
24#include "llvm/Transforms/Scalar.h"
25#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Sjoerd Meijerc89ca552018-06-28 12:55:29 +000026#include "llvm/Pass.h"
27#include "llvm/PassRegistry.h"
28#include "llvm/PassSupport.h"
29#include "llvm/Support/Debug.h"
30#include "llvm/IR/PatternMatch.h"
31#include "llvm/CodeGen/TargetPassConfig.h"
32#include "ARM.h"
33#include "ARMSubtarget.h"
34
35using namespace llvm;
36using namespace PatternMatch;
37
Sjoerd Meijerb3e06fa2018-07-06 14:47:09 +000038#define DEBUG_TYPE "arm-parallel-dsp"
39
40STATISTIC(NumSMLAD , "Number of smlad instructions generated");
Sjoerd Meijerc89ca552018-06-28 12:55:29 +000041
Sjoerd Meijer3c859b32018-08-14 07:43:49 +000042static cl::opt<bool>
43DisableParallelDSP("disable-arm-parallel-dsp", cl::Hidden, cl::init(false),
44 cl::desc("Disable the ARM Parallel DSP pass"));
45
Sam Parker1c3ca612019-10-16 09:37:03 +000046static cl::opt<unsigned>
47NumLoadLimit("arm-parallel-dsp-load-limit", cl::Hidden, cl::init(16),
48 cl::desc("Limit the number of loads analysed"));
49
Sjoerd Meijerc89ca552018-06-28 12:55:29 +000050namespace {
Sam Parker414dd1c2019-07-29 08:41:51 +000051 struct MulCandidate;
Sam Parker85ad78b2019-07-11 07:47:50 +000052 class Reduction;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +000053
Sam Parkercd385992019-08-02 08:21:17 +000054 using MulCandList = SmallVector<std::unique_ptr<MulCandidate>, 8>;
55 using MemInstList = SmallVectorImpl<LoadInst*>;
56 using MulPairList = SmallVector<std::pair<MulCandidate*, MulCandidate*>, 8>;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +000057
Sam Parker414dd1c2019-07-29 08:41:51 +000058 // 'MulCandidate' holds the multiplication instructions that are candidates
Sam Parker3da59e52019-07-26 14:11:40 +000059 // for parallel execution.
Sam Parker414dd1c2019-07-29 08:41:51 +000060 struct MulCandidate {
Sam Parker89a37992018-07-23 15:25:59 +000061 Instruction *Root;
Sam Parker414dd1c2019-07-29 08:41:51 +000062 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;
Sam Parkera761ba02019-08-28 08:51:13 +000066 bool Paired = false;
Sam Parkercd385992019-08-02 08:21:17 +000067 SmallVector<LoadInst*, 2> VecLd; // Container for loads to widen.
Sam Parker89a37992018-07-23 15:25:59 +000068
Sam Parker14c6dfd2019-08-02 07:32:28 +000069 MulCandidate(Instruction *I, Value *lhs, Value *rhs) :
70 Root(I), LHS(lhs), RHS(rhs) { }
Sam Parker89a37992018-07-23 15:25:59 +000071
Sam Parker414dd1c2019-07-29 08:41:51 +000072 bool HasTwoLoadInputs() const {
73 return isa<LoadInst>(LHS) && isa<LoadInst>(RHS);
74 }
Sam Parker7ca8c6f2019-08-01 08:17:51 +000075
76 LoadInst *getBaseLoad() const {
Sam Parkera761ba02019-08-28 08:51:13 +000077 return VecLd.front();
Sam Parker7ca8c6f2019-08-01 08:17:51 +000078 }
Sjoerd Meijerc89ca552018-06-28 12:55:29 +000079 };
80
Sam Parker85ad78b2019-07-11 07:47:50 +000081 /// Represent a sequence of multiply-accumulate operations with the aim to
82 /// perform the multiplications in parallel.
83 class Reduction {
84 Instruction *Root = nullptr;
85 Value *Acc = nullptr;
Sam Parker414dd1c2019-07-29 08:41:51 +000086 MulCandList Muls;
Sam Parkercd385992019-08-02 08:21:17 +000087 MulPairList MulPairs;
Sam Parkera761ba02019-08-28 08:51:13 +000088 SetVector<Instruction*> Adds;
Sam Parker85ad78b2019-07-11 07:47:50 +000089
90 public:
91 Reduction() = delete;
92
93 Reduction (Instruction *Add) : Root(Add) { }
94
95 /// Record an Add instruction that is a part of the this reduction.
96 void InsertAdd(Instruction *I) { Adds.insert(I); }
97
Sam Parkera761ba02019-08-28 08:51:13 +000098 /// Create MulCandidates, each rooted at a Mul instruction, that is a part
99 /// of this reduction.
100 void InsertMuls() {
101 auto GetMulOperand = [](Value *V) -> Instruction* {
102 if (auto *SExt = dyn_cast<SExtInst>(V)) {
103 if (auto *I = dyn_cast<Instruction>(SExt->getOperand(0)))
104 if (I->getOpcode() == Instruction::Mul)
105 return I;
106 } else if (auto *I = dyn_cast<Instruction>(V)) {
107 if (I->getOpcode() == Instruction::Mul)
108 return I;
109 }
110 return nullptr;
111 };
112
113 auto InsertMul = [this](Instruction *I) {
114 Value *LHS = cast<Instruction>(I->getOperand(0))->getOperand(0);
115 Value *RHS = cast<Instruction>(I->getOperand(1))->getOperand(0);
116 Muls.push_back(std::make_unique<MulCandidate>(I, LHS, RHS));
117 };
118
119 for (auto *Add : Adds) {
120 if (Add == Acc)
121 continue;
122 if (auto *Mul = GetMulOperand(Add->getOperand(0)))
123 InsertMul(Mul);
124 if (auto *Mul = GetMulOperand(Add->getOperand(1)))
125 InsertMul(Mul);
126 }
Sam Parker85ad78b2019-07-11 07:47:50 +0000127 }
128
129 /// Add the incoming accumulator value, returns true if a value had not
130 /// already been added. Returning false signals to the user that this
131 /// reduction already has a value to initialise the accumulator.
132 bool InsertAcc(Value *V) {
133 if (Acc)
134 return false;
135 Acc = V;
136 return true;
137 }
138
Sam Parker414dd1c2019-07-29 08:41:51 +0000139 /// Set two MulCandidates, rooted at muls, that can be executed as a single
Sam Parker85ad78b2019-07-11 07:47:50 +0000140 /// parallel operation.
Sam Parkera761ba02019-08-28 08:51:13 +0000141 void AddMulPair(MulCandidate *Mul0, MulCandidate *Mul1,
142 bool Exchange = false) {
143 LLVM_DEBUG(dbgs() << "Pairing:\n"
144 << *Mul0->Root << "\n"
145 << *Mul1->Root << "\n");
146 Mul0->Paired = true;
147 Mul1->Paired = true;
148 if (Exchange)
149 Mul1->Exchange = true;
Sam Parker85ad78b2019-07-11 07:47:50 +0000150 MulPairs.push_back(std::make_pair(Mul0, Mul1));
151 }
152
153 /// Return true if enough mul operations are found that can be executed in
154 /// parallel.
155 bool CreateParallelPairs();
156
157 /// Return the add instruction which is the root of the reduction.
158 Instruction *getRoot() { return Root; }
159
Sam Parker7ca8c6f2019-08-01 08:17:51 +0000160 bool is64Bit() const { return Root->getType()->isIntegerTy(64); }
161
Sam Parkerc363deb2019-09-09 08:39:14 +0000162 Type *getType() const { return Root->getType(); }
163
Sam Parker85ad78b2019-07-11 07:47:50 +0000164 /// Return the incoming value to be accumulated. This maybe null.
165 Value *getAccumulator() { return Acc; }
166
167 /// Return the set of adds that comprise the reduction.
Sam Parkera761ba02019-08-28 08:51:13 +0000168 SetVector<Instruction*> &getAdds() { return Adds; }
Sam Parker85ad78b2019-07-11 07:47:50 +0000169
Sam Parker414dd1c2019-07-29 08:41:51 +0000170 /// Return the MulCandidate, rooted at mul instruction, that comprise the
Sam Parker85ad78b2019-07-11 07:47:50 +0000171 /// the reduction.
Sam Parker414dd1c2019-07-29 08:41:51 +0000172 MulCandList &getMuls() { return Muls; }
Sam Parker85ad78b2019-07-11 07:47:50 +0000173
Sam Parker414dd1c2019-07-29 08:41:51 +0000174 /// Return the MulCandidate, rooted at mul instructions, that have been
Sam Parker85ad78b2019-07-11 07:47:50 +0000175 /// paired for parallel execution.
Sam Parkercd385992019-08-02 08:21:17 +0000176 MulPairList &getMulPairs() { return MulPairs; }
Sam Parker85ad78b2019-07-11 07:47:50 +0000177
178 /// To finalise, replace the uses of the root with the intrinsic call.
179 void UpdateRoot(Instruction *SMLAD) {
180 Root->replaceAllUsesWith(SMLAD);
181 }
Sam Parkera761ba02019-08-28 08:51:13 +0000182
183 void dump() {
184 LLVM_DEBUG(dbgs() << "Reduction:\n";
185 for (auto *Add : Adds)
186 LLVM_DEBUG(dbgs() << *Add << "\n");
187 for (auto &Mul : Muls)
188 LLVM_DEBUG(dbgs() << *Mul->Root << "\n"
189 << " " << *Mul->LHS << "\n"
190 << " " << *Mul->RHS << "\n");
191 LLVM_DEBUG(if (Acc) dbgs() << "Acc in: " << *Acc << "\n")
192 );
193 }
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000194 };
195
Sam Parker4c4ff132019-03-14 11:14:13 +0000196 class WidenedLoad {
197 LoadInst *NewLd = nullptr;
198 SmallVector<LoadInst*, 4> Loads;
199
200 public:
201 WidenedLoad(SmallVectorImpl<LoadInst*> &Lds, LoadInst *Wide)
202 : NewLd(Wide) {
203 for (auto *I : Lds)
204 Loads.push_back(I);
205 }
206 LoadInst *getLoad() {
207 return NewLd;
208 }
209 };
210
Sam Parkera761ba02019-08-28 08:51:13 +0000211 class ARMParallelDSP : public FunctionPass {
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000212 ScalarEvolution *SE;
213 AliasAnalysis *AA;
214 TargetLibraryInfo *TLI;
215 DominatorTree *DT;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000216 const DataLayout *DL;
217 Module *M;
Sam Parker453ba912018-11-09 09:18:00 +0000218 std::map<LoadInst*, LoadInst*> LoadPairs;
Sam Parker85ad78b2019-07-11 07:47:50 +0000219 SmallPtrSet<LoadInst*, 4> OffsetLoads;
Sam Parker4c4ff132019-03-14 11:14:13 +0000220 std::map<LoadInst*, std::unique_ptr<WidenedLoad>> WideLoads;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000221
Sam Parker85ad78b2019-07-11 07:47:50 +0000222 template<unsigned>
Sam Parkera761ba02019-08-28 08:51:13 +0000223 bool IsNarrowSequence(Value *V);
224 bool Search(Value *V, BasicBlock *BB, Reduction &R);
Sam Parkera33e3112019-05-13 09:23:32 +0000225 bool RecordMemoryOps(BasicBlock *BB);
Sam Parker85ad78b2019-07-11 07:47:50 +0000226 void InsertParallelMACs(Reduction &Reduction);
Fangrui Song68169342018-07-03 19:12:27 +0000227 bool AreSequentialLoads(LoadInst *Ld0, LoadInst *Ld1, MemInstList &VecMem);
Sam Parkercd385992019-08-02 08:21:17 +0000228 LoadInst* CreateWideLoad(MemInstList &Loads, IntegerType *LoadTy);
Sam Parker85ad78b2019-07-11 07:47:50 +0000229 bool CreateParallelPairs(Reduction &R);
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000230
231 /// Try to match and generate: SMLAD, SMLADX - Signed Multiply Accumulate
232 /// Dual performs two signed 16x16-bit multiplications. It adds the
233 /// products to a 32-bit accumulate operand. Optionally, the instruction can
234 /// exchange the halfwords of the second operand before performing the
235 /// arithmetic.
Sam Parkera761ba02019-08-28 08:51:13 +0000236 bool MatchSMLAD(Function &F);
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000237
238 public:
239 static char ID;
240
Sam Parkera761ba02019-08-28 08:51:13 +0000241 ARMParallelDSP() : FunctionPass(ID) { }
Sam Parkera33e3112019-05-13 09:23:32 +0000242
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000243 void getAnalysisUsage(AnalysisUsage &AU) const override {
Sam Parkera761ba02019-08-28 08:51:13 +0000244 FunctionPass::getAnalysisUsage(AU);
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000245 AU.addRequired<AssumptionCacheTracker>();
246 AU.addRequired<ScalarEvolutionWrapperPass>();
247 AU.addRequired<AAResultsWrapperPass>();
248 AU.addRequired<TargetLibraryInfoWrapperPass>();
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000249 AU.addRequired<DominatorTreeWrapperPass>();
250 AU.addRequired<TargetPassConfig>();
Sam Parkera761ba02019-08-28 08:51:13 +0000251 AU.addPreserved<ScalarEvolutionWrapperPass>();
252 AU.addPreserved<GlobalsAAWrapperPass>();
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000253 AU.setPreservesCFG();
254 }
255
Sam Parkera761ba02019-08-28 08:51:13 +0000256 bool runOnFunction(Function &F) override {
Sjoerd Meijer3c859b32018-08-14 07:43:49 +0000257 if (DisableParallelDSP)
258 return false;
Sam Parkera761ba02019-08-28 08:51:13 +0000259 if (skipFunction(F))
Eli Friedmanb27fc952019-07-23 20:48:46 +0000260 return false;
261
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000262 SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
263 AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
Teresa Johnson9c27b592019-09-07 03:09:36 +0000264 TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000265 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000266 auto &TPC = getAnalysis<TargetPassConfig>();
267
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000268 M = F.getParent();
269 DL = &M->getDataLayout();
270
271 auto &TM = TPC.getTM<TargetMachine>();
272 auto *ST = &TM.getSubtarget<ARMSubtarget>(F);
273
274 if (!ST->allowsUnalignedMem()) {
275 LLVM_DEBUG(dbgs() << "Unaligned memory access not supported: not "
276 "running pass ARMParallelDSP\n");
277 return false;
278 }
279
280 if (!ST->hasDSP()) {
281 LLVM_DEBUG(dbgs() << "DSP extension not enabled: not running pass "
282 "ARMParallelDSP\n");
283 return false;
284 }
285
Sam Parker9e730202019-03-15 10:19:32 +0000286 if (!ST->isLittle()) {
287 LLVM_DEBUG(dbgs() << "Only supporting little endian: not running pass "
Sam Parkera33e3112019-05-13 09:23:32 +0000288 << "ARMParallelDSP\n");
Sam Parker9e730202019-03-15 10:19:32 +0000289 return false;
290 }
291
Sam Parkera023c7a2018-09-12 09:17:44 +0000292 LLVM_DEBUG(dbgs() << "\n== Parallel DSP pass ==\n");
293 LLVM_DEBUG(dbgs() << " - " << F.getName() << "\n\n");
Sam Parker453ba912018-11-09 09:18:00 +0000294
Sam Parkera761ba02019-08-28 08:51:13 +0000295 bool Changes = MatchSMLAD(F);
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000296 return Changes;
297 }
298 };
299}
300
Sam Parkerffc16812018-07-03 12:44:16 +0000301template<typename MemInst>
302static bool AreSequentialAccesses(MemInst *MemOp0, MemInst *MemOp1,
Sam Parker453ba912018-11-09 09:18:00 +0000303 const DataLayout &DL, ScalarEvolution &SE) {
Sam Parker4c4ff132019-03-14 11:14:13 +0000304 if (isConsecutiveAccess(MemOp0, MemOp1, DL, SE))
Sam Parkerffc16812018-07-03 12:44:16 +0000305 return true;
Sam Parkerffc16812018-07-03 12:44:16 +0000306 return false;
307}
308
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000309bool ARMParallelDSP::AreSequentialLoads(LoadInst *Ld0, LoadInst *Ld1,
Sam Parkerffc16812018-07-03 12:44:16 +0000310 MemInstList &VecMem) {
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000311 if (!Ld0 || !Ld1)
312 return false;
313
Sam Parker4c4ff132019-03-14 11:14:13 +0000314 if (!LoadPairs.count(Ld0) || LoadPairs[Ld0] != Ld1)
315 return false;
316
317 LLVM_DEBUG(dbgs() << "Loads are sequential and valid:\n";
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000318 dbgs() << "Ld0:"; Ld0->dump();
319 dbgs() << "Ld1:"; Ld1->dump();
320 );
321
Sam Parker453ba912018-11-09 09:18:00 +0000322 VecMem.clear();
323 VecMem.push_back(Ld0);
324 VecMem.push_back(Ld1);
325 return true;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000326}
327
Sam Parker85ad78b2019-07-11 07:47:50 +0000328// MaxBitwidth: the maximum supported bitwidth of the elements in the DSP
329// instructions, which is set to 16. So here we should collect all i8 and i16
330// narrow operations.
331// TODO: we currently only collect i16, and will support i8 later, so that's
332// why we check that types are equal to MaxBitWidth, and not <= MaxBitWidth.
333template<unsigned MaxBitWidth>
Sam Parkera761ba02019-08-28 08:51:13 +0000334bool ARMParallelDSP::IsNarrowSequence(Value *V) {
Sam Parker74400652019-07-26 10:57:42 +0000335 if (auto *SExt = dyn_cast<SExtInst>(V)) {
336 if (SExt->getSrcTy()->getIntegerBitWidth() != MaxBitWidth)
Sam Parker85ad78b2019-07-11 07:47:50 +0000337 return false;
338
Sam Parker74400652019-07-26 10:57:42 +0000339 if (auto *Ld = dyn_cast<LoadInst>(SExt->getOperand(0))) {
Sam Parkera761ba02019-08-28 08:51:13 +0000340 // Check that this load could be paired.
341 return LoadPairs.count(Ld) || OffsetLoads.count(Ld);
Sam Parker85ad78b2019-07-11 07:47:50 +0000342 }
343 }
344 return false;
345}
346
Sam Parkera33e3112019-05-13 09:23:32 +0000347/// Iterate through the block and record base, offset pairs of loads which can
348/// be widened into a single load.
349bool ARMParallelDSP::RecordMemoryOps(BasicBlock *BB) {
Sam Parker453ba912018-11-09 09:18:00 +0000350 SmallVector<LoadInst*, 8> Loads;
Sam Parkera33e3112019-05-13 09:23:32 +0000351 SmallVector<Instruction*, 8> Writes;
Sam Parkera761ba02019-08-28 08:51:13 +0000352 LoadPairs.clear();
353 WideLoads.clear();
Sam Parker1c3ca612019-10-16 09:37:03 +0000354 OrderedBasicBlock OrderedBB(BB);
Sam Parkera33e3112019-05-13 09:23:32 +0000355
356 // Collect loads and instruction that may write to memory. For now we only
357 // record loads which are simple, sign-extended and have a single user.
358 // TODO: Allow zero-extended loads.
Sam Parker4c4ff132019-03-14 11:14:13 +0000359 for (auto &I : *BB) {
Sam Parkera33e3112019-05-13 09:23:32 +0000360 if (I.mayWriteToMemory())
361 Writes.push_back(&I);
Sam Parker453ba912018-11-09 09:18:00 +0000362 auto *Ld = dyn_cast<LoadInst>(&I);
Sam Parker4c4ff132019-03-14 11:14:13 +0000363 if (!Ld || !Ld->isSimple() ||
364 !Ld->hasOneUse() || !isa<SExtInst>(Ld->user_back()))
Sam Parker453ba912018-11-09 09:18:00 +0000365 continue;
366 Loads.push_back(Ld);
367 }
368
Sam Parker1c3ca612019-10-16 09:37:03 +0000369 if (Loads.empty() || Loads.size() > NumLoadLimit)
370 return false;
371
Sam Parkera33e3112019-05-13 09:23:32 +0000372 using InstSet = std::set<Instruction*>;
373 using DepMap = std::map<Instruction*, InstSet>;
374 DepMap RAWDeps;
375
376 // Record any writes that may alias a load.
377 const auto Size = LocationSize::unknown();
Sam Parker1c3ca612019-10-16 09:37:03 +0000378 for (auto Write : Writes) {
379 for (auto Read : Loads) {
Sam Parkera33e3112019-05-13 09:23:32 +0000380 MemoryLocation ReadLoc =
381 MemoryLocation(Read->getPointerOperand(), Size);
382
383 if (!isModOrRefSet(intersectModRef(AA->getModRefInfo(Write, ReadLoc),
384 ModRefInfo::ModRef)))
385 continue;
Sam Parker1c3ca612019-10-16 09:37:03 +0000386 if (OrderedBB.dominates(Write, Read))
Sam Parkera33e3112019-05-13 09:23:32 +0000387 RAWDeps[Read].insert(Write);
388 }
389 }
390
391 // Check whether there's not a write between the two loads which would
392 // prevent them from being safely merged.
393 auto SafeToPair = [&](LoadInst *Base, LoadInst *Offset) {
Sam Parker1c3ca612019-10-16 09:37:03 +0000394 LoadInst *Dominator = OrderedBB.dominates(Base, Offset) ? Base : Offset;
395 LoadInst *Dominated = OrderedBB.dominates(Base, Offset) ? Offset : Base;
Sam Parkera33e3112019-05-13 09:23:32 +0000396
397 if (RAWDeps.count(Dominated)) {
398 InstSet &WritesBefore = RAWDeps[Dominated];
399
400 for (auto Before : WritesBefore) {
Sam Parkera33e3112019-05-13 09:23:32 +0000401 // We can't move the second load backward, past a write, to merge
402 // with the first load.
Sam Parker1c3ca612019-10-16 09:37:03 +0000403 if (OrderedBB.dominates(Dominator, Before))
Sam Parkera33e3112019-05-13 09:23:32 +0000404 return false;
405 }
406 }
407 return true;
408 };
409
410 // Record base, offset load pairs.
411 for (auto *Base : Loads) {
412 for (auto *Offset : Loads) {
Sam Parker1c3ca612019-10-16 09:37:03 +0000413 if (Base == Offset || OffsetLoads.count(Offset))
Sam Parker453ba912018-11-09 09:18:00 +0000414 continue;
415
Sam Parkera33e3112019-05-13 09:23:32 +0000416 if (AreSequentialAccesses<LoadInst>(Base, Offset, *DL, *SE) &&
417 SafeToPair(Base, Offset)) {
418 LoadPairs[Base] = Offset;
Sam Parker85ad78b2019-07-11 07:47:50 +0000419 OffsetLoads.insert(Offset);
Sam Parker4c4ff132019-03-14 11:14:13 +0000420 break;
Sam Parker453ba912018-11-09 09:18:00 +0000421 }
422 }
423 }
Sam Parker4c4ff132019-03-14 11:14:13 +0000424
425 LLVM_DEBUG(if (!LoadPairs.empty()) {
426 dbgs() << "Consecutive load pairs:\n";
427 for (auto &MapIt : LoadPairs) {
428 LLVM_DEBUG(dbgs() << *MapIt.first << ", "
429 << *MapIt.second << "\n");
430 }
431 });
Sam Parker453ba912018-11-09 09:18:00 +0000432 return LoadPairs.size() > 1;
433}
434
Sam Parkera761ba02019-08-28 08:51:13 +0000435// Search recursively back through the operands to find a tree of values that
436// form a multiply-accumulate chain. The search records the Add and Mul
437// instructions that form the reduction and allows us to find a single value
438// to be used as the initial input to the accumlator.
439bool ARMParallelDSP::Search(Value *V, BasicBlock *BB, Reduction &R) {
440 // If we find a non-instruction, try to use it as the initial accumulator
441 // value. This may have already been found during the search in which case
442 // this function will return false, signaling a search fail.
443 auto *I = dyn_cast<Instruction>(V);
444 if (!I)
445 return R.InsertAcc(V);
446
447 if (I->getParent() != BB)
448 return false;
449
450 switch (I->getOpcode()) {
451 default:
452 break;
453 case Instruction::PHI:
454 // Could be the accumulator value.
455 return R.InsertAcc(V);
456 case Instruction::Add: {
457 // Adds should be adding together two muls, or another add and a mul to
458 // be within the mac chain. One of the operands may also be the
459 // accumulator value at which point we should stop searching.
460 R.InsertAdd(I);
461 Value *LHS = I->getOperand(0);
462 Value *RHS = I->getOperand(1);
463 bool ValidLHS = Search(LHS, BB, R);
464 bool ValidRHS = Search(RHS, BB, R);
465
466 if (ValidLHS && ValidRHS)
467 return true;
468
469 return R.InsertAcc(I);
470 }
471 case Instruction::Mul: {
472 Value *MulOp0 = I->getOperand(0);
473 Value *MulOp1 = I->getOperand(1);
474 return IsNarrowSequence<16>(MulOp0) && IsNarrowSequence<16>(MulOp1);
475 }
476 case Instruction::SExt:
477 return Search(I->getOperand(0), BB, R);
478 }
479 return false;
480}
481
482// The pass needs to identify integer add/sub reductions of 16-bit vector
Sam Parker85ad78b2019-07-11 07:47:50 +0000483// multiplications.
484// To use SMLAD:
485// 1) we first need to find integer add then look for this pattern:
486//
487// acc0 = ...
488// ld0 = load i16
489// sext0 = sext i16 %ld0 to i32
490// ld1 = load i16
491// sext1 = sext i16 %ld1 to i32
492// mul0 = mul %sext0, %sext1
493// ld2 = load i16
494// sext2 = sext i16 %ld2 to i32
495// ld3 = load i16
496// sext3 = sext i16 %ld3 to i32
497// mul1 = mul i32 %sext2, %sext3
498// add0 = add i32 %mul0, %acc0
499// acc1 = add i32 %add0, %mul1
500//
501// Which can be selected to:
502//
503// ldr r0
504// ldr r1
505// smlad r2, r0, r1, r2
506//
507// If constants are used instead of loads, these will need to be hoisted
508// out and into a register.
509//
510// If loop invariants are used instead of loads, these need to be packed
511// before the loop begins.
512//
Sam Parkera761ba02019-08-28 08:51:13 +0000513bool ARMParallelDSP::MatchSMLAD(Function &F) {
Sam Parker85ad78b2019-07-11 07:47:50 +0000514 bool Changed = false;
Sam Parker85ad78b2019-07-11 07:47:50 +0000515
Sam Parkera761ba02019-08-28 08:51:13 +0000516 for (auto &BB : F) {
517 SmallPtrSet<Instruction*, 4> AllAdds;
518 if (!RecordMemoryOps(&BB))
Sam Parker85ad78b2019-07-11 07:47:50 +0000519 continue;
520
Sam Parkera761ba02019-08-28 08:51:13 +0000521 for (Instruction &I : reverse(BB)) {
522 if (I.getOpcode() != Instruction::Add)
523 continue;
Sam Parker85ad78b2019-07-11 07:47:50 +0000524
Sam Parkera761ba02019-08-28 08:51:13 +0000525 if (AllAdds.count(&I))
526 continue;
Sam Parker85ad78b2019-07-11 07:47:50 +0000527
Sam Parkera761ba02019-08-28 08:51:13 +0000528 const auto *Ty = I.getType();
529 if (!Ty->isIntegerTy(32) && !Ty->isIntegerTy(64))
530 continue;
Sam Parker85ad78b2019-07-11 07:47:50 +0000531
Sam Parkera761ba02019-08-28 08:51:13 +0000532 Reduction R(&I);
533 if (!Search(&I, &BB, R))
534 continue;
Sam Parker85ad78b2019-07-11 07:47:50 +0000535
Sam Parkera761ba02019-08-28 08:51:13 +0000536 R.InsertMuls();
537 LLVM_DEBUG(dbgs() << "After search, Reduction:\n"; R.dump());
538
539 if (!CreateParallelPairs(R))
540 continue;
541
542 InsertParallelMACs(R);
543 Changed = true;
544 AllAdds.insert(R.getAdds().begin(), R.getAdds().end());
545 }
Sam Parker85ad78b2019-07-11 07:47:50 +0000546 }
547
548 return Changed;
549}
550
551bool ARMParallelDSP::CreateParallelPairs(Reduction &R) {
552
553 // Not enough mul operations to make a pair.
554 if (R.getMuls().size() < 2)
555 return false;
556
557 // Check that the muls operate directly upon sign extended loads.
Sam Parker414dd1c2019-07-29 08:41:51 +0000558 for (auto &MulCand : R.getMuls()) {
559 if (!MulCand->HasTwoLoadInputs())
Sam Parker85ad78b2019-07-11 07:47:50 +0000560 return false;
Sam Parker85ad78b2019-07-11 07:47:50 +0000561 }
562
Sam Parker414dd1c2019-07-29 08:41:51 +0000563 auto CanPair = [&](Reduction &R, MulCandidate *PMul0, MulCandidate *PMul1) {
Sam Parker453ba912018-11-09 09:18:00 +0000564 // The first elements of each vector should be loads with sexts. If we
565 // find that its two pairs of consecutive loads, then these can be
566 // transformed into two wider loads and the users can be replaced with
567 // DSP intrinsics.
Sam Parker414dd1c2019-07-29 08:41:51 +0000568 auto Ld0 = static_cast<LoadInst*>(PMul0->LHS);
569 auto Ld1 = static_cast<LoadInst*>(PMul1->LHS);
570 auto Ld2 = static_cast<LoadInst*>(PMul0->RHS);
571 auto Ld3 = static_cast<LoadInst*>(PMul1->RHS);
Sam Parker453ba912018-11-09 09:18:00 +0000572
Sam Parker414dd1c2019-07-29 08:41:51 +0000573 if (AreSequentialLoads(Ld0, Ld1, PMul0->VecLd)) {
574 if (AreSequentialLoads(Ld2, Ld3, PMul1->VecLd)) {
Sam Parker453ba912018-11-09 09:18:00 +0000575 LLVM_DEBUG(dbgs() << "OK: found two pairs of parallel loads!\n");
Sam Parker414dd1c2019-07-29 08:41:51 +0000576 R.AddMulPair(PMul0, PMul1);
577 return true;
578 } else if (AreSequentialLoads(Ld3, Ld2, PMul1->VecLd)) {
579 LLVM_DEBUG(dbgs() << "OK: found two pairs of parallel loads!\n");
580 LLVM_DEBUG(dbgs() << " exchanging Ld2 and Ld3\n");
Sam Parkera761ba02019-08-28 08:51:13 +0000581 R.AddMulPair(PMul0, PMul1, true);
Sam Parker453ba912018-11-09 09:18:00 +0000582 return true;
583 }
Sam Parker414dd1c2019-07-29 08:41:51 +0000584 } else if (AreSequentialLoads(Ld1, Ld0, PMul0->VecLd) &&
585 AreSequentialLoads(Ld2, Ld3, PMul1->VecLd)) {
586 LLVM_DEBUG(dbgs() << "OK: found two pairs of parallel loads!\n");
587 LLVM_DEBUG(dbgs() << " exchanging Ld0 and Ld1\n");
588 LLVM_DEBUG(dbgs() << " and swapping muls\n");
Sam Parker414dd1c2019-07-29 08:41:51 +0000589 // Only the second operand can be exchanged, so swap the muls.
Sam Parkera761ba02019-08-28 08:51:13 +0000590 R.AddMulPair(PMul1, PMul0, true);
Sam Parker414dd1c2019-07-29 08:41:51 +0000591 return true;
Sam Parker453ba912018-11-09 09:18:00 +0000592 }
593 return false;
594 };
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000595
Sam Parker414dd1c2019-07-29 08:41:51 +0000596 MulCandList &Muls = R.getMuls();
Sam Parker85ad78b2019-07-11 07:47:50 +0000597 const unsigned Elems = Muls.size();
Sam Parkera023c7a2018-09-12 09:17:44 +0000598 for (unsigned i = 0; i < Elems; ++i) {
Sam Parker414dd1c2019-07-29 08:41:51 +0000599 MulCandidate *PMul0 = static_cast<MulCandidate*>(Muls[i].get());
Sam Parkera761ba02019-08-28 08:51:13 +0000600 if (PMul0->Paired)
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000601 continue;
602
Sam Parkera023c7a2018-09-12 09:17:44 +0000603 for (unsigned j = 0; j < Elems; ++j) {
604 if (i == j)
605 continue;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000606
Sam Parker414dd1c2019-07-29 08:41:51 +0000607 MulCandidate *PMul1 = static_cast<MulCandidate*>(Muls[j].get());
Sam Parkera761ba02019-08-28 08:51:13 +0000608 if (PMul1->Paired)
Sam Parkera023c7a2018-09-12 09:17:44 +0000609 continue;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000610
Sam Parkera023c7a2018-09-12 09:17:44 +0000611 const Instruction *Mul0 = PMul0->Root;
612 const Instruction *Mul1 = PMul1->Root;
613 if (Mul0 == Mul1)
614 continue;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000615
Sam Parkera023c7a2018-09-12 09:17:44 +0000616 assert(PMul0 != PMul1 && "expected different chains");
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000617
Sam Parkera761ba02019-08-28 08:51:13 +0000618 if (CanPair(R, PMul0, PMul1))
Sam Parkera023c7a2018-09-12 09:17:44 +0000619 break;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000620 }
621 }
Sam Parker85ad78b2019-07-11 07:47:50 +0000622 return !R.getMulPairs().empty();
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000623}
624
Sam Parker85ad78b2019-07-11 07:47:50 +0000625void ARMParallelDSP::InsertParallelMACs(Reduction &R) {
626
Sam Parker7ca8c6f2019-08-01 08:17:51 +0000627 auto CreateSMLAD = [&](LoadInst* WideLd0, LoadInst *WideLd1,
628 Value *Acc, bool Exchange,
629 Instruction *InsertAfter) {
Sam Parker85ad78b2019-07-11 07:47:50 +0000630 // Replace the reduction chain with an intrinsic call
Sam Parker85ad78b2019-07-11 07:47:50 +0000631
632 Value* Args[] = { WideLd0, WideLd1, Acc };
633 Function *SMLAD = nullptr;
634 if (Exchange)
635 SMLAD = Acc->getType()->isIntegerTy(32) ?
636 Intrinsic::getDeclaration(M, Intrinsic::arm_smladx) :
637 Intrinsic::getDeclaration(M, Intrinsic::arm_smlaldx);
638 else
639 SMLAD = Acc->getType()->isIntegerTy(32) ?
640 Intrinsic::getDeclaration(M, Intrinsic::arm_smlad) :
641 Intrinsic::getDeclaration(M, Intrinsic::arm_smlald);
642
643 IRBuilder<NoFolder> Builder(InsertAfter->getParent(),
Sam Parker1c3ca612019-10-16 09:37:03 +0000644 BasicBlock::iterator(InsertAfter));
Sam Parker85ad78b2019-07-11 07:47:50 +0000645 Instruction *Call = Builder.CreateCall(SMLAD, Args);
646 NumSMLAD++;
647 return Call;
648 };
649
Sam Parker1c3ca612019-10-16 09:37:03 +0000650 // Return the instruction after the dominated instruction.
651 auto GetInsertPoint = [this](Value *A, Value *B) {
652 assert((isa<Instruction>(A) || isa<Instruction>(B)) &&
653 "expected at least one instruction");
654
655 Value *V = nullptr;
656 if (!isa<Instruction>(A))
657 V = B;
658 else if (!isa<Instruction>(B))
659 V = A;
660 else
661 V = DT->dominates(cast<Instruction>(A), cast<Instruction>(B)) ? B : A;
662
663 return &*++BasicBlock::iterator(cast<Instruction>(V));
664 };
665
Sam Parker85ad78b2019-07-11 07:47:50 +0000666 Value *Acc = R.getAccumulator();
Sam Parkera761ba02019-08-28 08:51:13 +0000667
668 // For any muls that were discovered but not paired, accumulate their values
669 // as before.
Sam Parker1c3ca612019-10-16 09:37:03 +0000670 IRBuilder<NoFolder> Builder(R.getRoot()->getParent());
Sam Parkera761ba02019-08-28 08:51:13 +0000671 MulCandList &MulCands = R.getMuls();
672 for (auto &MulCand : MulCands) {
673 if (MulCand->Paired)
674 continue;
675
Sam Parker1c3ca612019-10-16 09:37:03 +0000676 Instruction *Mul = cast<Instruction>(MulCand->Root);
Sam Parkerfea53222019-09-04 08:41:34 +0000677 LLVM_DEBUG(dbgs() << "Accumulating unpaired mul: " << *Mul << "\n");
678
Sam Parkerc363deb2019-09-09 08:39:14 +0000679 if (R.getType() != Mul->getType()) {
Sam Parkerfea53222019-09-04 08:41:34 +0000680 assert(R.is64Bit() && "expected 64-bit result");
Sam Parker1c3ca612019-10-16 09:37:03 +0000681 Builder.SetInsertPoint(&*++BasicBlock::iterator(Mul));
682 Mul = cast<Instruction>(Builder.CreateSExt(Mul, R.getRoot()->getType()));
Sam Parkerfea53222019-09-04 08:41:34 +0000683 }
684
Sam Parkera761ba02019-08-28 08:51:13 +0000685 if (!Acc) {
Sam Parkerfea53222019-09-04 08:41:34 +0000686 Acc = Mul;
Sam Parkera761ba02019-08-28 08:51:13 +0000687 continue;
688 }
Sam Parkerfea53222019-09-04 08:41:34 +0000689
Sam Parker1c3ca612019-10-16 09:37:03 +0000690 // If Acc is the original incoming value to the reduction, it could be a
691 // phi. But the phi will dominate Mul, meaning that Mul will be the
692 // insertion point.
693 Builder.SetInsertPoint(GetInsertPoint(Mul, Acc));
Sam Parkerfea53222019-09-04 08:41:34 +0000694 Acc = Builder.CreateAdd(Mul, Acc);
Sam Parkera761ba02019-08-28 08:51:13 +0000695 }
696
Sam Parkerc363deb2019-09-09 08:39:14 +0000697 if (!Acc) {
Sam Parkerfea53222019-09-04 08:41:34 +0000698 Acc = R.is64Bit() ?
699 ConstantInt::get(IntegerType::get(M->getContext(), 64), 0) :
700 ConstantInt::get(IntegerType::get(M->getContext(), 32), 0);
Sam Parkerc363deb2019-09-09 08:39:14 +0000701 } else if (Acc->getType() != R.getType()) {
702 Builder.SetInsertPoint(R.getRoot());
703 Acc = Builder.CreateSExt(Acc, R.getType());
704 }
Sam Parker85ad78b2019-07-11 07:47:50 +0000705
Sam Parker1c3ca612019-10-16 09:37:03 +0000706 // Roughly sort the mul pairs in their program order.
707 OrderedBasicBlock OrderedBB(R.getRoot()->getParent());
708 llvm::sort(R.getMulPairs(), [&OrderedBB](auto &PairA, auto &PairB) {
709 const Instruction *A = PairA.first->Root;
710 const Instruction *B = PairB.first->Root;
711 return OrderedBB.dominates(A, B);
712 });
713
Sam Parker7ca8c6f2019-08-01 08:17:51 +0000714 IntegerType *Ty = IntegerType::get(M->getContext(), 32);
Sam Parker85ad78b2019-07-11 07:47:50 +0000715 for (auto &Pair : R.getMulPairs()) {
Sam Parker7ca8c6f2019-08-01 08:17:51 +0000716 MulCandidate *LHSMul = Pair.first;
717 MulCandidate *RHSMul = Pair.second;
Sam Parker7ca8c6f2019-08-01 08:17:51 +0000718 LoadInst *BaseLHS = LHSMul->getBaseLoad();
719 LoadInst *BaseRHS = RHSMul->getBaseLoad();
720 LoadInst *WideLHS = WideLoads.count(BaseLHS) ?
721 WideLoads[BaseLHS]->getLoad() : CreateWideLoad(LHSMul->VecLd, Ty);
722 LoadInst *WideRHS = WideLoads.count(BaseRHS) ?
723 WideLoads[BaseRHS]->getLoad() : CreateWideLoad(RHSMul->VecLd, Ty);
Sam Parkera023c7a2018-09-12 09:17:44 +0000724
Sam Parker1c3ca612019-10-16 09:37:03 +0000725 Instruction *InsertAfter = GetInsertPoint(WideLHS, WideRHS);
726 InsertAfter = GetInsertPoint(InsertAfter, Acc);
Sam Parker7ca8c6f2019-08-01 08:17:51 +0000727 Acc = CreateSMLAD(WideLHS, WideRHS, Acc, RHSMul->Exchange, InsertAfter);
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000728 }
Sam Parker85ad78b2019-07-11 07:47:50 +0000729 R.UpdateRoot(cast<Instruction>(Acc));
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000730}
731
Sam Parkercd385992019-08-02 08:21:17 +0000732LoadInst* ARMParallelDSP::CreateWideLoad(MemInstList &Loads,
Sam Parkera33e3112019-05-13 09:23:32 +0000733 IntegerType *LoadTy) {
Sam Parker4c4ff132019-03-14 11:14:13 +0000734 assert(Loads.size() == 2 && "currently only support widening two loads");
Sam Parkera33e3112019-05-13 09:23:32 +0000735
736 LoadInst *Base = Loads[0];
737 LoadInst *Offset = Loads[1];
738
739 Instruction *BaseSExt = dyn_cast<SExtInst>(Base->user_back());
740 Instruction *OffsetSExt = dyn_cast<SExtInst>(Offset->user_back());
741
742 assert((BaseSExt && OffsetSExt)
743 && "Loads should have a single, extending, user");
744
745 std::function<void(Value*, Value*)> MoveBefore =
746 [&](Value *A, Value *B) -> void {
747 if (!isa<Instruction>(A) || !isa<Instruction>(B))
748 return;
749
750 auto *Source = cast<Instruction>(A);
751 auto *Sink = cast<Instruction>(B);
752
753 if (DT->dominates(Source, Sink) ||
754 Source->getParent() != Sink->getParent() ||
755 isa<PHINode>(Source) || isa<PHINode>(Sink))
756 return;
757
758 Source->moveBefore(Sink);
Sam Parkeraeb21b92019-07-24 09:38:39 +0000759 for (auto &Op : Source->operands())
760 MoveBefore(Op, Source);
Sam Parkera33e3112019-05-13 09:23:32 +0000761 };
762
763 // Insert the load at the point of the original dominating load.
764 LoadInst *DomLoad = DT->dominates(Base, Offset) ? Base : Offset;
765 IRBuilder<NoFolder> IRB(DomLoad->getParent(),
766 ++BasicBlock::iterator(DomLoad));
767
768 // Bitcast the pointer to a wider type and create the wide load, while making
769 // sure to maintain the original alignment as this prevents ldrd from being
770 // generated when it could be illegal due to memory alignment.
771 const unsigned AddrSpace = DomLoad->getPointerAddressSpace();
772 Value *VecPtr = IRB.CreateBitCast(Base->getPointerOperand(),
Eli Friedmanb09c7782018-10-18 19:34:30 +0000773 LoadTy->getPointerTo(AddrSpace));
Sam Parker4c4ff132019-03-14 11:14:13 +0000774 LoadInst *WideLoad = IRB.CreateAlignedLoad(LoadTy, VecPtr,
Sam Parkera33e3112019-05-13 09:23:32 +0000775 Base->getAlignment());
Sam Parker4c4ff132019-03-14 11:14:13 +0000776
Sam Parkera33e3112019-05-13 09:23:32 +0000777 // Make sure everything is in the correct order in the basic block.
778 MoveBefore(Base->getPointerOperand(), VecPtr);
779 MoveBefore(VecPtr, WideLoad);
Sam Parker4c4ff132019-03-14 11:14:13 +0000780
781 // From the wide load, create two values that equal the original two loads.
Sam Parkera33e3112019-05-13 09:23:32 +0000782 // Loads[0] needs trunc while Loads[1] needs a lshr and trunc.
783 // TODO: Support big-endian as well.
784 Value *Bottom = IRB.CreateTrunc(WideLoad, Base->getType());
Sam Parkera761ba02019-08-28 08:51:13 +0000785 Value *NewBaseSExt = IRB.CreateSExt(Bottom, BaseSExt->getType());
786 BaseSExt->replaceAllUsesWith(NewBaseSExt);
Sam Parker4c4ff132019-03-14 11:14:13 +0000787
Sam Parkera33e3112019-05-13 09:23:32 +0000788 IntegerType *OffsetTy = cast<IntegerType>(Offset->getType());
789 Value *ShiftVal = ConstantInt::get(LoadTy, OffsetTy->getBitWidth());
Sam Parker4c4ff132019-03-14 11:14:13 +0000790 Value *Top = IRB.CreateLShr(WideLoad, ShiftVal);
Sam Parkera33e3112019-05-13 09:23:32 +0000791 Value *Trunc = IRB.CreateTrunc(Top, OffsetTy);
Sam Parkera761ba02019-08-28 08:51:13 +0000792 Value *NewOffsetSExt = IRB.CreateSExt(Trunc, OffsetSExt->getType());
793 OffsetSExt->replaceAllUsesWith(NewOffsetSExt);
Sam Parker4c4ff132019-03-14 11:14:13 +0000794
Sam Parkera761ba02019-08-28 08:51:13 +0000795 LLVM_DEBUG(dbgs() << "From Base and Offset:\n"
796 << *Base << "\n" << *Offset << "\n"
797 << "Created Wide Load:\n"
798 << *WideLoad << "\n"
799 << *Bottom << "\n"
800 << *NewBaseSExt << "\n"
801 << *Top << "\n"
802 << *Trunc << "\n"
803 << *NewOffsetSExt << "\n");
Sam Parkera33e3112019-05-13 09:23:32 +0000804 WideLoads.emplace(std::make_pair(Base,
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000805 std::make_unique<WidenedLoad>(Loads, WideLoad)));
Sam Parker4c4ff132019-03-14 11:14:13 +0000806 return WideLoad;
Eli Friedmanb09c7782018-10-18 19:34:30 +0000807}
808
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000809Pass *llvm::createARMParallelDSPPass() {
810 return new ARMParallelDSP();
811}
812
813char ARMParallelDSP::ID = 0;
814
Sjoerd Meijerb3e06fa2018-07-06 14:47:09 +0000815INITIALIZE_PASS_BEGIN(ARMParallelDSP, "arm-parallel-dsp",
Sam Parkera761ba02019-08-28 08:51:13 +0000816 "Transform functions to use DSP intrinsics", false, false)
Sjoerd Meijerb3e06fa2018-07-06 14:47:09 +0000817INITIALIZE_PASS_END(ARMParallelDSP, "arm-parallel-dsp",
Sam Parkera761ba02019-08-28 08:51:13 +0000818 "Transform functions to use DSP intrinsics", false, false)