blob: e2c9335db41906f8b71d01be2f1a23f3f76417f6 [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
Reid Kleckner5d986952019-12-11 07:55:26 -080017#include "ARM.h"
18#include "ARMSubtarget.h"
Sjoerd Meijerc89ca552018-06-28 12:55:29 +000019#include "llvm/ADT/SmallPtrSet.h"
Reid Kleckner5d986952019-12-11 07:55:26 -080020#include "llvm/ADT/Statistic.h"
Sjoerd Meijerc89ca552018-06-28 12:55:29 +000021#include "llvm/Analysis/AliasAnalysis.h"
22#include "llvm/Analysis/LoopAccessAnalysis.h"
Sam Parker1c3ca612019-10-16 09:37:03 +000023#include "llvm/Analysis/OrderedBasicBlock.h"
Reid Kleckner5d986952019-12-11 07:55:26 -080024#include "llvm/CodeGen/TargetPassConfig.h"
Sjoerd Meijerc89ca552018-06-28 12:55:29 +000025#include "llvm/IR/Instructions.h"
Reid Kleckner5d986952019-12-11 07:55:26 -080026#include "llvm/IR/IntrinsicsARM.h"
Sjoerd Meijerc89ca552018-06-28 12:55:29 +000027#include "llvm/IR/NoFolder.h"
Reid Kleckner5d986952019-12-11 07:55:26 -080028#include "llvm/IR/PatternMatch.h"
Sjoerd Meijerc89ca552018-06-28 12:55:29 +000029#include "llvm/Pass.h"
30#include "llvm/PassRegistry.h"
31#include "llvm/PassSupport.h"
32#include "llvm/Support/Debug.h"
Reid Kleckner5d986952019-12-11 07:55:26 -080033#include "llvm/Transforms/Scalar.h"
34#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Sjoerd Meijerc89ca552018-06-28 12:55:29 +000035
36using namespace llvm;
37using namespace PatternMatch;
38
Sjoerd Meijerb3e06fa2018-07-06 14:47:09 +000039#define DEBUG_TYPE "arm-parallel-dsp"
40
41STATISTIC(NumSMLAD , "Number of smlad instructions generated");
Sjoerd Meijerc89ca552018-06-28 12:55:29 +000042
Sjoerd Meijer3c859b32018-08-14 07:43:49 +000043static cl::opt<bool>
44DisableParallelDSP("disable-arm-parallel-dsp", cl::Hidden, cl::init(false),
45 cl::desc("Disable the ARM Parallel DSP pass"));
46
Sam Parker1c3ca612019-10-16 09:37:03 +000047static cl::opt<unsigned>
48NumLoadLimit("arm-parallel-dsp-load-limit", cl::Hidden, cl::init(16),
49 cl::desc("Limit the number of loads analysed"));
50
Sjoerd Meijerc89ca552018-06-28 12:55:29 +000051namespace {
Sam Parker414dd1c2019-07-29 08:41:51 +000052 struct MulCandidate;
Sam Parker85ad78b2019-07-11 07:47:50 +000053 class Reduction;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +000054
Sam Parkercd385992019-08-02 08:21:17 +000055 using MulCandList = SmallVector<std::unique_ptr<MulCandidate>, 8>;
56 using MemInstList = SmallVectorImpl<LoadInst*>;
57 using MulPairList = SmallVector<std::pair<MulCandidate*, MulCandidate*>, 8>;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +000058
Sam Parker414dd1c2019-07-29 08:41:51 +000059 // 'MulCandidate' holds the multiplication instructions that are candidates
Sam Parker3da59e52019-07-26 14:11:40 +000060 // for parallel execution.
Sam Parker414dd1c2019-07-29 08:41:51 +000061 struct MulCandidate {
Sam Parker89a37992018-07-23 15:25:59 +000062 Instruction *Root;
Sam Parker414dd1c2019-07-29 08:41:51 +000063 Value* LHS;
64 Value* RHS;
Sam Parker3da59e52019-07-26 14:11:40 +000065 bool Exchange = false;
Sam Parker89a37992018-07-23 15:25:59 +000066 bool ReadOnly = true;
Sam Parkera761ba02019-08-28 08:51:13 +000067 bool Paired = false;
Sam Parkercd385992019-08-02 08:21:17 +000068 SmallVector<LoadInst*, 2> VecLd; // Container for loads to widen.
Sam Parker89a37992018-07-23 15:25:59 +000069
Sam Parker14c6dfd2019-08-02 07:32:28 +000070 MulCandidate(Instruction *I, Value *lhs, Value *rhs) :
71 Root(I), LHS(lhs), RHS(rhs) { }
Sam Parker89a37992018-07-23 15:25:59 +000072
Sam Parker414dd1c2019-07-29 08:41:51 +000073 bool HasTwoLoadInputs() const {
74 return isa<LoadInst>(LHS) && isa<LoadInst>(RHS);
75 }
Sam Parker7ca8c6f2019-08-01 08:17:51 +000076
77 LoadInst *getBaseLoad() const {
Sam Parkera761ba02019-08-28 08:51:13 +000078 return VecLd.front();
Sam Parker7ca8c6f2019-08-01 08:17:51 +000079 }
Sjoerd Meijerc89ca552018-06-28 12:55:29 +000080 };
81
Sam Parker85ad78b2019-07-11 07:47:50 +000082 /// Represent a sequence of multiply-accumulate operations with the aim to
83 /// perform the multiplications in parallel.
84 class Reduction {
85 Instruction *Root = nullptr;
86 Value *Acc = nullptr;
Sam Parker414dd1c2019-07-29 08:41:51 +000087 MulCandList Muls;
Sam Parkercd385992019-08-02 08:21:17 +000088 MulPairList MulPairs;
Sam Parkera761ba02019-08-28 08:51:13 +000089 SetVector<Instruction*> Adds;
Sam Parker85ad78b2019-07-11 07:47:50 +000090
91 public:
92 Reduction() = delete;
93
94 Reduction (Instruction *Add) : Root(Add) { }
95
96 /// Record an Add instruction that is a part of the this reduction.
97 void InsertAdd(Instruction *I) { Adds.insert(I); }
98
Sam Parkera761ba02019-08-28 08:51:13 +000099 /// Create MulCandidates, each rooted at a Mul instruction, that is a part
100 /// of this reduction.
101 void InsertMuls() {
102 auto GetMulOperand = [](Value *V) -> Instruction* {
103 if (auto *SExt = dyn_cast<SExtInst>(V)) {
104 if (auto *I = dyn_cast<Instruction>(SExt->getOperand(0)))
105 if (I->getOpcode() == Instruction::Mul)
106 return I;
107 } else if (auto *I = dyn_cast<Instruction>(V)) {
108 if (I->getOpcode() == Instruction::Mul)
109 return I;
110 }
111 return nullptr;
112 };
113
114 auto InsertMul = [this](Instruction *I) {
115 Value *LHS = cast<Instruction>(I->getOperand(0))->getOperand(0);
116 Value *RHS = cast<Instruction>(I->getOperand(1))->getOperand(0);
117 Muls.push_back(std::make_unique<MulCandidate>(I, LHS, RHS));
118 };
119
120 for (auto *Add : Adds) {
121 if (Add == Acc)
122 continue;
123 if (auto *Mul = GetMulOperand(Add->getOperand(0)))
124 InsertMul(Mul);
125 if (auto *Mul = GetMulOperand(Add->getOperand(1)))
126 InsertMul(Mul);
127 }
Sam Parker85ad78b2019-07-11 07:47:50 +0000128 }
129
130 /// Add the incoming accumulator value, returns true if a value had not
131 /// already been added. Returning false signals to the user that this
132 /// reduction already has a value to initialise the accumulator.
133 bool InsertAcc(Value *V) {
134 if (Acc)
135 return false;
136 Acc = V;
137 return true;
138 }
139
Sam Parker414dd1c2019-07-29 08:41:51 +0000140 /// Set two MulCandidates, rooted at muls, that can be executed as a single
Sam Parker85ad78b2019-07-11 07:47:50 +0000141 /// parallel operation.
Sam Parkera761ba02019-08-28 08:51:13 +0000142 void AddMulPair(MulCandidate *Mul0, MulCandidate *Mul1,
143 bool Exchange = false) {
144 LLVM_DEBUG(dbgs() << "Pairing:\n"
145 << *Mul0->Root << "\n"
146 << *Mul1->Root << "\n");
147 Mul0->Paired = true;
148 Mul1->Paired = true;
149 if (Exchange)
150 Mul1->Exchange = true;
Sam Parker85ad78b2019-07-11 07:47:50 +0000151 MulPairs.push_back(std::make_pair(Mul0, Mul1));
152 }
153
154 /// Return true if enough mul operations are found that can be executed in
155 /// parallel.
156 bool CreateParallelPairs();
157
158 /// Return the add instruction which is the root of the reduction.
159 Instruction *getRoot() { return Root; }
160
Sam Parker7ca8c6f2019-08-01 08:17:51 +0000161 bool is64Bit() const { return Root->getType()->isIntegerTy(64); }
162
Sam Parkerc363deb2019-09-09 08:39:14 +0000163 Type *getType() const { return Root->getType(); }
164
Sam Parker85ad78b2019-07-11 07:47:50 +0000165 /// Return the incoming value to be accumulated. This maybe null.
166 Value *getAccumulator() { return Acc; }
167
168 /// Return the set of adds that comprise the reduction.
Sam Parkera761ba02019-08-28 08:51:13 +0000169 SetVector<Instruction*> &getAdds() { return Adds; }
Sam Parker85ad78b2019-07-11 07:47:50 +0000170
Sam Parker414dd1c2019-07-29 08:41:51 +0000171 /// Return the MulCandidate, rooted at mul instruction, that comprise the
Sam Parker85ad78b2019-07-11 07:47:50 +0000172 /// the reduction.
Sam Parker414dd1c2019-07-29 08:41:51 +0000173 MulCandList &getMuls() { return Muls; }
Sam Parker85ad78b2019-07-11 07:47:50 +0000174
Sam Parker414dd1c2019-07-29 08:41:51 +0000175 /// Return the MulCandidate, rooted at mul instructions, that have been
Sam Parker85ad78b2019-07-11 07:47:50 +0000176 /// paired for parallel execution.
Sam Parkercd385992019-08-02 08:21:17 +0000177 MulPairList &getMulPairs() { return MulPairs; }
Sam Parker85ad78b2019-07-11 07:47:50 +0000178
179 /// To finalise, replace the uses of the root with the intrinsic call.
180 void UpdateRoot(Instruction *SMLAD) {
181 Root->replaceAllUsesWith(SMLAD);
182 }
Sam Parkera761ba02019-08-28 08:51:13 +0000183
184 void dump() {
185 LLVM_DEBUG(dbgs() << "Reduction:\n";
186 for (auto *Add : Adds)
187 LLVM_DEBUG(dbgs() << *Add << "\n");
188 for (auto &Mul : Muls)
189 LLVM_DEBUG(dbgs() << *Mul->Root << "\n"
190 << " " << *Mul->LHS << "\n"
191 << " " << *Mul->RHS << "\n");
192 LLVM_DEBUG(if (Acc) dbgs() << "Acc in: " << *Acc << "\n")
193 );
194 }
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000195 };
196
Sam Parker4c4ff132019-03-14 11:14:13 +0000197 class WidenedLoad {
198 LoadInst *NewLd = nullptr;
199 SmallVector<LoadInst*, 4> Loads;
200
201 public:
202 WidenedLoad(SmallVectorImpl<LoadInst*> &Lds, LoadInst *Wide)
203 : NewLd(Wide) {
204 for (auto *I : Lds)
205 Loads.push_back(I);
206 }
207 LoadInst *getLoad() {
208 return NewLd;
209 }
210 };
211
Sam Parkera761ba02019-08-28 08:51:13 +0000212 class ARMParallelDSP : public FunctionPass {
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000213 ScalarEvolution *SE;
214 AliasAnalysis *AA;
215 TargetLibraryInfo *TLI;
216 DominatorTree *DT;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000217 const DataLayout *DL;
218 Module *M;
Sam Parker453ba912018-11-09 09:18:00 +0000219 std::map<LoadInst*, LoadInst*> LoadPairs;
Sam Parker85ad78b2019-07-11 07:47:50 +0000220 SmallPtrSet<LoadInst*, 4> OffsetLoads;
Sam Parker4c4ff132019-03-14 11:14:13 +0000221 std::map<LoadInst*, std::unique_ptr<WidenedLoad>> WideLoads;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000222
Sam Parker85ad78b2019-07-11 07:47:50 +0000223 template<unsigned>
Sam Parkera761ba02019-08-28 08:51:13 +0000224 bool IsNarrowSequence(Value *V);
225 bool Search(Value *V, BasicBlock *BB, Reduction &R);
Sam Parkera33e3112019-05-13 09:23:32 +0000226 bool RecordMemoryOps(BasicBlock *BB);
Sam Parker85ad78b2019-07-11 07:47:50 +0000227 void InsertParallelMACs(Reduction &Reduction);
Fangrui Song68169342018-07-03 19:12:27 +0000228 bool AreSequentialLoads(LoadInst *Ld0, LoadInst *Ld1, MemInstList &VecMem);
Sam Parkercd385992019-08-02 08:21:17 +0000229 LoadInst* CreateWideLoad(MemInstList &Loads, IntegerType *LoadTy);
Sam Parker85ad78b2019-07-11 07:47:50 +0000230 bool CreateParallelPairs(Reduction &R);
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000231
232 /// Try to match and generate: SMLAD, SMLADX - Signed Multiply Accumulate
233 /// Dual performs two signed 16x16-bit multiplications. It adds the
234 /// products to a 32-bit accumulate operand. Optionally, the instruction can
235 /// exchange the halfwords of the second operand before performing the
236 /// arithmetic.
Sam Parkera761ba02019-08-28 08:51:13 +0000237 bool MatchSMLAD(Function &F);
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000238
239 public:
240 static char ID;
241
Sam Parkera761ba02019-08-28 08:51:13 +0000242 ARMParallelDSP() : FunctionPass(ID) { }
Sam Parkera33e3112019-05-13 09:23:32 +0000243
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000244 void getAnalysisUsage(AnalysisUsage &AU) const override {
Sam Parkera761ba02019-08-28 08:51:13 +0000245 FunctionPass::getAnalysisUsage(AU);
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000246 AU.addRequired<AssumptionCacheTracker>();
247 AU.addRequired<ScalarEvolutionWrapperPass>();
248 AU.addRequired<AAResultsWrapperPass>();
249 AU.addRequired<TargetLibraryInfoWrapperPass>();
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000250 AU.addRequired<DominatorTreeWrapperPass>();
251 AU.addRequired<TargetPassConfig>();
Sam Parkera761ba02019-08-28 08:51:13 +0000252 AU.addPreserved<ScalarEvolutionWrapperPass>();
253 AU.addPreserved<GlobalsAAWrapperPass>();
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000254 AU.setPreservesCFG();
255 }
256
Sam Parkera761ba02019-08-28 08:51:13 +0000257 bool runOnFunction(Function &F) override {
Sjoerd Meijer3c859b32018-08-14 07:43:49 +0000258 if (DisableParallelDSP)
259 return false;
Sam Parkera761ba02019-08-28 08:51:13 +0000260 if (skipFunction(F))
Eli Friedmanb27fc952019-07-23 20:48:46 +0000261 return false;
262
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000263 SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
264 AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
Teresa Johnson9c27b592019-09-07 03:09:36 +0000265 TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000266 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000267 auto &TPC = getAnalysis<TargetPassConfig>();
268
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000269 M = F.getParent();
270 DL = &M->getDataLayout();
271
272 auto &TM = TPC.getTM<TargetMachine>();
273 auto *ST = &TM.getSubtarget<ARMSubtarget>(F);
274
275 if (!ST->allowsUnalignedMem()) {
276 LLVM_DEBUG(dbgs() << "Unaligned memory access not supported: not "
277 "running pass ARMParallelDSP\n");
278 return false;
279 }
280
281 if (!ST->hasDSP()) {
282 LLVM_DEBUG(dbgs() << "DSP extension not enabled: not running pass "
283 "ARMParallelDSP\n");
284 return false;
285 }
286
Sam Parker9e730202019-03-15 10:19:32 +0000287 if (!ST->isLittle()) {
288 LLVM_DEBUG(dbgs() << "Only supporting little endian: not running pass "
Sam Parkera33e3112019-05-13 09:23:32 +0000289 << "ARMParallelDSP\n");
Sam Parker9e730202019-03-15 10:19:32 +0000290 return false;
291 }
292
Sam Parkera023c7a2018-09-12 09:17:44 +0000293 LLVM_DEBUG(dbgs() << "\n== Parallel DSP pass ==\n");
294 LLVM_DEBUG(dbgs() << " - " << F.getName() << "\n\n");
Sam Parker453ba912018-11-09 09:18:00 +0000295
Sam Parkera761ba02019-08-28 08:51:13 +0000296 bool Changes = MatchSMLAD(F);
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000297 return Changes;
298 }
299 };
300}
301
Sam Parkerffc16812018-07-03 12:44:16 +0000302template<typename MemInst>
303static bool AreSequentialAccesses(MemInst *MemOp0, MemInst *MemOp1,
Sam Parker453ba912018-11-09 09:18:00 +0000304 const DataLayout &DL, ScalarEvolution &SE) {
Sam Parker4c4ff132019-03-14 11:14:13 +0000305 if (isConsecutiveAccess(MemOp0, MemOp1, DL, SE))
Sam Parkerffc16812018-07-03 12:44:16 +0000306 return true;
Sam Parkerffc16812018-07-03 12:44:16 +0000307 return false;
308}
309
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000310bool ARMParallelDSP::AreSequentialLoads(LoadInst *Ld0, LoadInst *Ld1,
Sam Parkerffc16812018-07-03 12:44:16 +0000311 MemInstList &VecMem) {
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000312 if (!Ld0 || !Ld1)
313 return false;
314
Sam Parker4c4ff132019-03-14 11:14:13 +0000315 if (!LoadPairs.count(Ld0) || LoadPairs[Ld0] != Ld1)
316 return false;
317
318 LLVM_DEBUG(dbgs() << "Loads are sequential and valid:\n";
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000319 dbgs() << "Ld0:"; Ld0->dump();
320 dbgs() << "Ld1:"; Ld1->dump();
321 );
322
Sam Parker453ba912018-11-09 09:18:00 +0000323 VecMem.clear();
324 VecMem.push_back(Ld0);
325 VecMem.push_back(Ld1);
326 return true;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000327}
328
Sam Parker85ad78b2019-07-11 07:47:50 +0000329// MaxBitwidth: the maximum supported bitwidth of the elements in the DSP
330// instructions, which is set to 16. So here we should collect all i8 and i16
331// narrow operations.
332// TODO: we currently only collect i16, and will support i8 later, so that's
333// why we check that types are equal to MaxBitWidth, and not <= MaxBitWidth.
334template<unsigned MaxBitWidth>
Sam Parkera761ba02019-08-28 08:51:13 +0000335bool ARMParallelDSP::IsNarrowSequence(Value *V) {
Sam Parker74400652019-07-26 10:57:42 +0000336 if (auto *SExt = dyn_cast<SExtInst>(V)) {
337 if (SExt->getSrcTy()->getIntegerBitWidth() != MaxBitWidth)
Sam Parker85ad78b2019-07-11 07:47:50 +0000338 return false;
339
Sam Parker74400652019-07-26 10:57:42 +0000340 if (auto *Ld = dyn_cast<LoadInst>(SExt->getOperand(0))) {
Sam Parkera761ba02019-08-28 08:51:13 +0000341 // Check that this load could be paired.
342 return LoadPairs.count(Ld) || OffsetLoads.count(Ld);
Sam Parker85ad78b2019-07-11 07:47:50 +0000343 }
344 }
345 return false;
346}
347
Sam Parkera33e3112019-05-13 09:23:32 +0000348/// Iterate through the block and record base, offset pairs of loads which can
349/// be widened into a single load.
350bool ARMParallelDSP::RecordMemoryOps(BasicBlock *BB) {
Sam Parker453ba912018-11-09 09:18:00 +0000351 SmallVector<LoadInst*, 8> Loads;
Sam Parkera33e3112019-05-13 09:23:32 +0000352 SmallVector<Instruction*, 8> Writes;
Sam Parkera761ba02019-08-28 08:51:13 +0000353 LoadPairs.clear();
354 WideLoads.clear();
Sam Parker1c3ca612019-10-16 09:37:03 +0000355 OrderedBasicBlock OrderedBB(BB);
Sam Parkera33e3112019-05-13 09:23:32 +0000356
357 // Collect loads and instruction that may write to memory. For now we only
358 // record loads which are simple, sign-extended and have a single user.
359 // TODO: Allow zero-extended loads.
Sam Parker4c4ff132019-03-14 11:14:13 +0000360 for (auto &I : *BB) {
Sam Parkera33e3112019-05-13 09:23:32 +0000361 if (I.mayWriteToMemory())
362 Writes.push_back(&I);
Sam Parker453ba912018-11-09 09:18:00 +0000363 auto *Ld = dyn_cast<LoadInst>(&I);
Sam Parker4c4ff132019-03-14 11:14:13 +0000364 if (!Ld || !Ld->isSimple() ||
365 !Ld->hasOneUse() || !isa<SExtInst>(Ld->user_back()))
Sam Parker453ba912018-11-09 09:18:00 +0000366 continue;
367 Loads.push_back(Ld);
368 }
369
Sam Parker1c3ca612019-10-16 09:37:03 +0000370 if (Loads.empty() || Loads.size() > NumLoadLimit)
371 return false;
372
Sam Parkera33e3112019-05-13 09:23:32 +0000373 using InstSet = std::set<Instruction*>;
374 using DepMap = std::map<Instruction*, InstSet>;
375 DepMap RAWDeps;
376
377 // Record any writes that may alias a load.
378 const auto Size = LocationSize::unknown();
Sam Parker1c3ca612019-10-16 09:37:03 +0000379 for (auto Write : Writes) {
380 for (auto Read : Loads) {
Sam Parkera33e3112019-05-13 09:23:32 +0000381 MemoryLocation ReadLoc =
382 MemoryLocation(Read->getPointerOperand(), Size);
383
384 if (!isModOrRefSet(intersectModRef(AA->getModRefInfo(Write, ReadLoc),
385 ModRefInfo::ModRef)))
386 continue;
Sam Parker1c3ca612019-10-16 09:37:03 +0000387 if (OrderedBB.dominates(Write, Read))
Sam Parkera33e3112019-05-13 09:23:32 +0000388 RAWDeps[Read].insert(Write);
389 }
390 }
391
392 // Check whether there's not a write between the two loads which would
393 // prevent them from being safely merged.
394 auto SafeToPair = [&](LoadInst *Base, LoadInst *Offset) {
Sam Parker1c3ca612019-10-16 09:37:03 +0000395 LoadInst *Dominator = OrderedBB.dominates(Base, Offset) ? Base : Offset;
396 LoadInst *Dominated = OrderedBB.dominates(Base, Offset) ? Offset : Base;
Sam Parkera33e3112019-05-13 09:23:32 +0000397
398 if (RAWDeps.count(Dominated)) {
399 InstSet &WritesBefore = RAWDeps[Dominated];
400
401 for (auto Before : WritesBefore) {
Sam Parkera33e3112019-05-13 09:23:32 +0000402 // We can't move the second load backward, past a write, to merge
403 // with the first load.
Sam Parker1c3ca612019-10-16 09:37:03 +0000404 if (OrderedBB.dominates(Dominator, Before))
Sam Parkera33e3112019-05-13 09:23:32 +0000405 return false;
406 }
407 }
408 return true;
409 };
410
411 // Record base, offset load pairs.
412 for (auto *Base : Loads) {
413 for (auto *Offset : Loads) {
Sam Parker1c3ca612019-10-16 09:37:03 +0000414 if (Base == Offset || OffsetLoads.count(Offset))
Sam Parker453ba912018-11-09 09:18:00 +0000415 continue;
416
Sam Parkera33e3112019-05-13 09:23:32 +0000417 if (AreSequentialAccesses<LoadInst>(Base, Offset, *DL, *SE) &&
418 SafeToPair(Base, Offset)) {
419 LoadPairs[Base] = Offset;
Sam Parker85ad78b2019-07-11 07:47:50 +0000420 OffsetLoads.insert(Offset);
Sam Parker4c4ff132019-03-14 11:14:13 +0000421 break;
Sam Parker453ba912018-11-09 09:18:00 +0000422 }
423 }
424 }
Sam Parker4c4ff132019-03-14 11:14:13 +0000425
426 LLVM_DEBUG(if (!LoadPairs.empty()) {
427 dbgs() << "Consecutive load pairs:\n";
428 for (auto &MapIt : LoadPairs) {
429 LLVM_DEBUG(dbgs() << *MapIt.first << ", "
430 << *MapIt.second << "\n");
431 }
432 });
Sam Parker453ba912018-11-09 09:18:00 +0000433 return LoadPairs.size() > 1;
434}
435
Sam Parkera761ba02019-08-28 08:51:13 +0000436// Search recursively back through the operands to find a tree of values that
437// form a multiply-accumulate chain. The search records the Add and Mul
438// instructions that form the reduction and allows us to find a single value
439// to be used as the initial input to the accumlator.
440bool ARMParallelDSP::Search(Value *V, BasicBlock *BB, Reduction &R) {
441 // If we find a non-instruction, try to use it as the initial accumulator
442 // value. This may have already been found during the search in which case
443 // this function will return false, signaling a search fail.
444 auto *I = dyn_cast<Instruction>(V);
445 if (!I)
446 return R.InsertAcc(V);
447
448 if (I->getParent() != BB)
449 return false;
450
451 switch (I->getOpcode()) {
452 default:
453 break;
454 case Instruction::PHI:
455 // Could be the accumulator value.
456 return R.InsertAcc(V);
457 case Instruction::Add: {
458 // Adds should be adding together two muls, or another add and a mul to
459 // be within the mac chain. One of the operands may also be the
460 // accumulator value at which point we should stop searching.
461 R.InsertAdd(I);
462 Value *LHS = I->getOperand(0);
463 Value *RHS = I->getOperand(1);
464 bool ValidLHS = Search(LHS, BB, R);
465 bool ValidRHS = Search(RHS, BB, R);
466
467 if (ValidLHS && ValidRHS)
468 return true;
469
470 return R.InsertAcc(I);
471 }
472 case Instruction::Mul: {
473 Value *MulOp0 = I->getOperand(0);
474 Value *MulOp1 = I->getOperand(1);
475 return IsNarrowSequence<16>(MulOp0) && IsNarrowSequence<16>(MulOp1);
476 }
477 case Instruction::SExt:
478 return Search(I->getOperand(0), BB, R);
479 }
480 return false;
481}
482
483// The pass needs to identify integer add/sub reductions of 16-bit vector
Sam Parker85ad78b2019-07-11 07:47:50 +0000484// multiplications.
485// To use SMLAD:
486// 1) we first need to find integer add then look for this pattern:
487//
488// acc0 = ...
489// ld0 = load i16
490// sext0 = sext i16 %ld0 to i32
491// ld1 = load i16
492// sext1 = sext i16 %ld1 to i32
493// mul0 = mul %sext0, %sext1
494// ld2 = load i16
495// sext2 = sext i16 %ld2 to i32
496// ld3 = load i16
497// sext3 = sext i16 %ld3 to i32
498// mul1 = mul i32 %sext2, %sext3
499// add0 = add i32 %mul0, %acc0
500// acc1 = add i32 %add0, %mul1
501//
502// Which can be selected to:
503//
504// ldr r0
505// ldr r1
506// smlad r2, r0, r1, r2
507//
508// If constants are used instead of loads, these will need to be hoisted
509// out and into a register.
510//
511// If loop invariants are used instead of loads, these need to be packed
512// before the loop begins.
513//
Sam Parkera761ba02019-08-28 08:51:13 +0000514bool ARMParallelDSP::MatchSMLAD(Function &F) {
Sam Parker85ad78b2019-07-11 07:47:50 +0000515 bool Changed = false;
Sam Parker85ad78b2019-07-11 07:47:50 +0000516
Sam Parkera761ba02019-08-28 08:51:13 +0000517 for (auto &BB : F) {
518 SmallPtrSet<Instruction*, 4> AllAdds;
519 if (!RecordMemoryOps(&BB))
Sam Parker85ad78b2019-07-11 07:47:50 +0000520 continue;
521
Sam Parkera761ba02019-08-28 08:51:13 +0000522 for (Instruction &I : reverse(BB)) {
523 if (I.getOpcode() != Instruction::Add)
524 continue;
Sam Parker85ad78b2019-07-11 07:47:50 +0000525
Sam Parkera761ba02019-08-28 08:51:13 +0000526 if (AllAdds.count(&I))
527 continue;
Sam Parker85ad78b2019-07-11 07:47:50 +0000528
Sam Parkera761ba02019-08-28 08:51:13 +0000529 const auto *Ty = I.getType();
530 if (!Ty->isIntegerTy(32) && !Ty->isIntegerTy(64))
531 continue;
Sam Parker85ad78b2019-07-11 07:47:50 +0000532
Sam Parkera761ba02019-08-28 08:51:13 +0000533 Reduction R(&I);
534 if (!Search(&I, &BB, R))
535 continue;
Sam Parker85ad78b2019-07-11 07:47:50 +0000536
Sam Parkera761ba02019-08-28 08:51:13 +0000537 R.InsertMuls();
538 LLVM_DEBUG(dbgs() << "After search, Reduction:\n"; R.dump());
539
540 if (!CreateParallelPairs(R))
541 continue;
542
543 InsertParallelMACs(R);
544 Changed = true;
545 AllAdds.insert(R.getAdds().begin(), R.getAdds().end());
546 }
Sam Parker85ad78b2019-07-11 07:47:50 +0000547 }
548
549 return Changed;
550}
551
552bool ARMParallelDSP::CreateParallelPairs(Reduction &R) {
553
554 // Not enough mul operations to make a pair.
555 if (R.getMuls().size() < 2)
556 return false;
557
558 // Check that the muls operate directly upon sign extended loads.
Sam Parker414dd1c2019-07-29 08:41:51 +0000559 for (auto &MulCand : R.getMuls()) {
560 if (!MulCand->HasTwoLoadInputs())
Sam Parker85ad78b2019-07-11 07:47:50 +0000561 return false;
Sam Parker85ad78b2019-07-11 07:47:50 +0000562 }
563
Sam Parker414dd1c2019-07-29 08:41:51 +0000564 auto CanPair = [&](Reduction &R, MulCandidate *PMul0, MulCandidate *PMul1) {
Sam Parker453ba912018-11-09 09:18:00 +0000565 // The first elements of each vector should be loads with sexts. If we
566 // find that its two pairs of consecutive loads, then these can be
567 // transformed into two wider loads and the users can be replaced with
568 // DSP intrinsics.
Sam Parker414dd1c2019-07-29 08:41:51 +0000569 auto Ld0 = static_cast<LoadInst*>(PMul0->LHS);
570 auto Ld1 = static_cast<LoadInst*>(PMul1->LHS);
571 auto Ld2 = static_cast<LoadInst*>(PMul0->RHS);
572 auto Ld3 = static_cast<LoadInst*>(PMul1->RHS);
Sam Parker453ba912018-11-09 09:18:00 +0000573
Sam Parker414dd1c2019-07-29 08:41:51 +0000574 if (AreSequentialLoads(Ld0, Ld1, PMul0->VecLd)) {
575 if (AreSequentialLoads(Ld2, Ld3, PMul1->VecLd)) {
Sam Parker453ba912018-11-09 09:18:00 +0000576 LLVM_DEBUG(dbgs() << "OK: found two pairs of parallel loads!\n");
Sam Parker414dd1c2019-07-29 08:41:51 +0000577 R.AddMulPair(PMul0, PMul1);
578 return true;
579 } else if (AreSequentialLoads(Ld3, Ld2, PMul1->VecLd)) {
580 LLVM_DEBUG(dbgs() << "OK: found two pairs of parallel loads!\n");
581 LLVM_DEBUG(dbgs() << " exchanging Ld2 and Ld3\n");
Sam Parkera761ba02019-08-28 08:51:13 +0000582 R.AddMulPair(PMul0, PMul1, true);
Sam Parker453ba912018-11-09 09:18:00 +0000583 return true;
584 }
Sam Parker414dd1c2019-07-29 08:41:51 +0000585 } else if (AreSequentialLoads(Ld1, Ld0, PMul0->VecLd) &&
586 AreSequentialLoads(Ld2, Ld3, PMul1->VecLd)) {
587 LLVM_DEBUG(dbgs() << "OK: found two pairs of parallel loads!\n");
588 LLVM_DEBUG(dbgs() << " exchanging Ld0 and Ld1\n");
589 LLVM_DEBUG(dbgs() << " and swapping muls\n");
Sam Parker414dd1c2019-07-29 08:41:51 +0000590 // Only the second operand can be exchanged, so swap the muls.
Sam Parkera761ba02019-08-28 08:51:13 +0000591 R.AddMulPair(PMul1, PMul0, true);
Sam Parker414dd1c2019-07-29 08:41:51 +0000592 return true;
Sam Parker453ba912018-11-09 09:18:00 +0000593 }
594 return false;
595 };
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000596
Sam Parker414dd1c2019-07-29 08:41:51 +0000597 MulCandList &Muls = R.getMuls();
Sam Parker85ad78b2019-07-11 07:47:50 +0000598 const unsigned Elems = Muls.size();
Sam Parkera023c7a2018-09-12 09:17:44 +0000599 for (unsigned i = 0; i < Elems; ++i) {
Sam Parker414dd1c2019-07-29 08:41:51 +0000600 MulCandidate *PMul0 = static_cast<MulCandidate*>(Muls[i].get());
Sam Parkera761ba02019-08-28 08:51:13 +0000601 if (PMul0->Paired)
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000602 continue;
603
Sam Parkera023c7a2018-09-12 09:17:44 +0000604 for (unsigned j = 0; j < Elems; ++j) {
605 if (i == j)
606 continue;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000607
Sam Parker414dd1c2019-07-29 08:41:51 +0000608 MulCandidate *PMul1 = static_cast<MulCandidate*>(Muls[j].get());
Sam Parkera761ba02019-08-28 08:51:13 +0000609 if (PMul1->Paired)
Sam Parkera023c7a2018-09-12 09:17:44 +0000610 continue;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000611
Sam Parkera023c7a2018-09-12 09:17:44 +0000612 const Instruction *Mul0 = PMul0->Root;
613 const Instruction *Mul1 = PMul1->Root;
614 if (Mul0 == Mul1)
615 continue;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000616
Sam Parkera023c7a2018-09-12 09:17:44 +0000617 assert(PMul0 != PMul1 && "expected different chains");
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000618
Sam Parkera761ba02019-08-28 08:51:13 +0000619 if (CanPair(R, PMul0, PMul1))
Sam Parkera023c7a2018-09-12 09:17:44 +0000620 break;
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000621 }
622 }
Sam Parker85ad78b2019-07-11 07:47:50 +0000623 return !R.getMulPairs().empty();
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000624}
625
Sam Parker85ad78b2019-07-11 07:47:50 +0000626void ARMParallelDSP::InsertParallelMACs(Reduction &R) {
627
Sam Parker7ca8c6f2019-08-01 08:17:51 +0000628 auto CreateSMLAD = [&](LoadInst* WideLd0, LoadInst *WideLd1,
629 Value *Acc, bool Exchange,
630 Instruction *InsertAfter) {
Sam Parker85ad78b2019-07-11 07:47:50 +0000631 // Replace the reduction chain with an intrinsic call
Sam Parker85ad78b2019-07-11 07:47:50 +0000632
633 Value* Args[] = { WideLd0, WideLd1, Acc };
634 Function *SMLAD = nullptr;
635 if (Exchange)
636 SMLAD = Acc->getType()->isIntegerTy(32) ?
637 Intrinsic::getDeclaration(M, Intrinsic::arm_smladx) :
638 Intrinsic::getDeclaration(M, Intrinsic::arm_smlaldx);
639 else
640 SMLAD = Acc->getType()->isIntegerTy(32) ?
641 Intrinsic::getDeclaration(M, Intrinsic::arm_smlad) :
642 Intrinsic::getDeclaration(M, Intrinsic::arm_smlald);
643
644 IRBuilder<NoFolder> Builder(InsertAfter->getParent(),
Sam Parker1c3ca612019-10-16 09:37:03 +0000645 BasicBlock::iterator(InsertAfter));
Sam Parker85ad78b2019-07-11 07:47:50 +0000646 Instruction *Call = Builder.CreateCall(SMLAD, Args);
647 NumSMLAD++;
648 return Call;
649 };
650
Sam Parker1c3ca612019-10-16 09:37:03 +0000651 // Return the instruction after the dominated instruction.
652 auto GetInsertPoint = [this](Value *A, Value *B) {
653 assert((isa<Instruction>(A) || isa<Instruction>(B)) &&
654 "expected at least one instruction");
655
656 Value *V = nullptr;
657 if (!isa<Instruction>(A))
658 V = B;
659 else if (!isa<Instruction>(B))
660 V = A;
661 else
662 V = DT->dominates(cast<Instruction>(A), cast<Instruction>(B)) ? B : A;
663
664 return &*++BasicBlock::iterator(cast<Instruction>(V));
665 };
666
Sam Parker85ad78b2019-07-11 07:47:50 +0000667 Value *Acc = R.getAccumulator();
Sam Parkera761ba02019-08-28 08:51:13 +0000668
669 // For any muls that were discovered but not paired, accumulate their values
670 // as before.
Sam Parker1c3ca612019-10-16 09:37:03 +0000671 IRBuilder<NoFolder> Builder(R.getRoot()->getParent());
Sam Parkera761ba02019-08-28 08:51:13 +0000672 MulCandList &MulCands = R.getMuls();
673 for (auto &MulCand : MulCands) {
674 if (MulCand->Paired)
675 continue;
676
Sam Parker1c3ca612019-10-16 09:37:03 +0000677 Instruction *Mul = cast<Instruction>(MulCand->Root);
Sam Parkerfea53222019-09-04 08:41:34 +0000678 LLVM_DEBUG(dbgs() << "Accumulating unpaired mul: " << *Mul << "\n");
679
Sam Parkerc363deb2019-09-09 08:39:14 +0000680 if (R.getType() != Mul->getType()) {
Sam Parkerfea53222019-09-04 08:41:34 +0000681 assert(R.is64Bit() && "expected 64-bit result");
Sam Parker1c3ca612019-10-16 09:37:03 +0000682 Builder.SetInsertPoint(&*++BasicBlock::iterator(Mul));
683 Mul = cast<Instruction>(Builder.CreateSExt(Mul, R.getRoot()->getType()));
Sam Parkerfea53222019-09-04 08:41:34 +0000684 }
685
Sam Parkera761ba02019-08-28 08:51:13 +0000686 if (!Acc) {
Sam Parkerfea53222019-09-04 08:41:34 +0000687 Acc = Mul;
Sam Parkera761ba02019-08-28 08:51:13 +0000688 continue;
689 }
Sam Parkerfea53222019-09-04 08:41:34 +0000690
Sam Parker1c3ca612019-10-16 09:37:03 +0000691 // If Acc is the original incoming value to the reduction, it could be a
692 // phi. But the phi will dominate Mul, meaning that Mul will be the
693 // insertion point.
694 Builder.SetInsertPoint(GetInsertPoint(Mul, Acc));
Sam Parkerfea53222019-09-04 08:41:34 +0000695 Acc = Builder.CreateAdd(Mul, Acc);
Sam Parkera761ba02019-08-28 08:51:13 +0000696 }
697
Sam Parkerc363deb2019-09-09 08:39:14 +0000698 if (!Acc) {
Sam Parkerfea53222019-09-04 08:41:34 +0000699 Acc = R.is64Bit() ?
700 ConstantInt::get(IntegerType::get(M->getContext(), 64), 0) :
701 ConstantInt::get(IntegerType::get(M->getContext(), 32), 0);
Sam Parkerc363deb2019-09-09 08:39:14 +0000702 } else if (Acc->getType() != R.getType()) {
703 Builder.SetInsertPoint(R.getRoot());
704 Acc = Builder.CreateSExt(Acc, R.getType());
705 }
Sam Parker85ad78b2019-07-11 07:47:50 +0000706
Sam Parker1c3ca612019-10-16 09:37:03 +0000707 // Roughly sort the mul pairs in their program order.
708 OrderedBasicBlock OrderedBB(R.getRoot()->getParent());
709 llvm::sort(R.getMulPairs(), [&OrderedBB](auto &PairA, auto &PairB) {
710 const Instruction *A = PairA.first->Root;
711 const Instruction *B = PairB.first->Root;
712 return OrderedBB.dominates(A, B);
713 });
714
Sam Parker7ca8c6f2019-08-01 08:17:51 +0000715 IntegerType *Ty = IntegerType::get(M->getContext(), 32);
Sam Parker85ad78b2019-07-11 07:47:50 +0000716 for (auto &Pair : R.getMulPairs()) {
Sam Parker7ca8c6f2019-08-01 08:17:51 +0000717 MulCandidate *LHSMul = Pair.first;
718 MulCandidate *RHSMul = Pair.second;
Sam Parker7ca8c6f2019-08-01 08:17:51 +0000719 LoadInst *BaseLHS = LHSMul->getBaseLoad();
720 LoadInst *BaseRHS = RHSMul->getBaseLoad();
721 LoadInst *WideLHS = WideLoads.count(BaseLHS) ?
722 WideLoads[BaseLHS]->getLoad() : CreateWideLoad(LHSMul->VecLd, Ty);
723 LoadInst *WideRHS = WideLoads.count(BaseRHS) ?
724 WideLoads[BaseRHS]->getLoad() : CreateWideLoad(RHSMul->VecLd, Ty);
Sam Parkera023c7a2018-09-12 09:17:44 +0000725
Sam Parker1c3ca612019-10-16 09:37:03 +0000726 Instruction *InsertAfter = GetInsertPoint(WideLHS, WideRHS);
727 InsertAfter = GetInsertPoint(InsertAfter, Acc);
Sam Parker7ca8c6f2019-08-01 08:17:51 +0000728 Acc = CreateSMLAD(WideLHS, WideRHS, Acc, RHSMul->Exchange, InsertAfter);
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000729 }
Sam Parker85ad78b2019-07-11 07:47:50 +0000730 R.UpdateRoot(cast<Instruction>(Acc));
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000731}
732
Sam Parkercd385992019-08-02 08:21:17 +0000733LoadInst* ARMParallelDSP::CreateWideLoad(MemInstList &Loads,
Sam Parkera33e3112019-05-13 09:23:32 +0000734 IntegerType *LoadTy) {
Sam Parker4c4ff132019-03-14 11:14:13 +0000735 assert(Loads.size() == 2 && "currently only support widening two loads");
Sam Parkera33e3112019-05-13 09:23:32 +0000736
737 LoadInst *Base = Loads[0];
738 LoadInst *Offset = Loads[1];
739
740 Instruction *BaseSExt = dyn_cast<SExtInst>(Base->user_back());
741 Instruction *OffsetSExt = dyn_cast<SExtInst>(Offset->user_back());
742
743 assert((BaseSExt && OffsetSExt)
744 && "Loads should have a single, extending, user");
745
746 std::function<void(Value*, Value*)> MoveBefore =
747 [&](Value *A, Value *B) -> void {
748 if (!isa<Instruction>(A) || !isa<Instruction>(B))
749 return;
750
751 auto *Source = cast<Instruction>(A);
752 auto *Sink = cast<Instruction>(B);
753
754 if (DT->dominates(Source, Sink) ||
755 Source->getParent() != Sink->getParent() ||
756 isa<PHINode>(Source) || isa<PHINode>(Sink))
757 return;
758
759 Source->moveBefore(Sink);
Sam Parkeraeb21b92019-07-24 09:38:39 +0000760 for (auto &Op : Source->operands())
761 MoveBefore(Op, Source);
Sam Parkera33e3112019-05-13 09:23:32 +0000762 };
763
764 // Insert the load at the point of the original dominating load.
765 LoadInst *DomLoad = DT->dominates(Base, Offset) ? Base : Offset;
766 IRBuilder<NoFolder> IRB(DomLoad->getParent(),
767 ++BasicBlock::iterator(DomLoad));
768
769 // Bitcast the pointer to a wider type and create the wide load, while making
770 // sure to maintain the original alignment as this prevents ldrd from being
771 // generated when it could be illegal due to memory alignment.
772 const unsigned AddrSpace = DomLoad->getPointerAddressSpace();
773 Value *VecPtr = IRB.CreateBitCast(Base->getPointerOperand(),
Eli Friedmanb09c7782018-10-18 19:34:30 +0000774 LoadTy->getPointerTo(AddrSpace));
Sam Parker4c4ff132019-03-14 11:14:13 +0000775 LoadInst *WideLoad = IRB.CreateAlignedLoad(LoadTy, VecPtr,
Sam Parkera33e3112019-05-13 09:23:32 +0000776 Base->getAlignment());
Sam Parker4c4ff132019-03-14 11:14:13 +0000777
Sam Parkera33e3112019-05-13 09:23:32 +0000778 // Make sure everything is in the correct order in the basic block.
779 MoveBefore(Base->getPointerOperand(), VecPtr);
780 MoveBefore(VecPtr, WideLoad);
Sam Parker4c4ff132019-03-14 11:14:13 +0000781
782 // From the wide load, create two values that equal the original two loads.
Sam Parkera33e3112019-05-13 09:23:32 +0000783 // Loads[0] needs trunc while Loads[1] needs a lshr and trunc.
784 // TODO: Support big-endian as well.
785 Value *Bottom = IRB.CreateTrunc(WideLoad, Base->getType());
Sam Parkera761ba02019-08-28 08:51:13 +0000786 Value *NewBaseSExt = IRB.CreateSExt(Bottom, BaseSExt->getType());
787 BaseSExt->replaceAllUsesWith(NewBaseSExt);
Sam Parker4c4ff132019-03-14 11:14:13 +0000788
Sam Parkera33e3112019-05-13 09:23:32 +0000789 IntegerType *OffsetTy = cast<IntegerType>(Offset->getType());
790 Value *ShiftVal = ConstantInt::get(LoadTy, OffsetTy->getBitWidth());
Sam Parker4c4ff132019-03-14 11:14:13 +0000791 Value *Top = IRB.CreateLShr(WideLoad, ShiftVal);
Sam Parkera33e3112019-05-13 09:23:32 +0000792 Value *Trunc = IRB.CreateTrunc(Top, OffsetTy);
Sam Parkera761ba02019-08-28 08:51:13 +0000793 Value *NewOffsetSExt = IRB.CreateSExt(Trunc, OffsetSExt->getType());
794 OffsetSExt->replaceAllUsesWith(NewOffsetSExt);
Sam Parker4c4ff132019-03-14 11:14:13 +0000795
Sam Parkera761ba02019-08-28 08:51:13 +0000796 LLVM_DEBUG(dbgs() << "From Base and Offset:\n"
797 << *Base << "\n" << *Offset << "\n"
798 << "Created Wide Load:\n"
799 << *WideLoad << "\n"
800 << *Bottom << "\n"
801 << *NewBaseSExt << "\n"
802 << *Top << "\n"
803 << *Trunc << "\n"
804 << *NewOffsetSExt << "\n");
Sam Parkera33e3112019-05-13 09:23:32 +0000805 WideLoads.emplace(std::make_pair(Base,
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000806 std::make_unique<WidenedLoad>(Loads, WideLoad)));
Sam Parker4c4ff132019-03-14 11:14:13 +0000807 return WideLoad;
Eli Friedmanb09c7782018-10-18 19:34:30 +0000808}
809
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000810Pass *llvm::createARMParallelDSPPass() {
811 return new ARMParallelDSP();
812}
813
814char ARMParallelDSP::ID = 0;
815
Sjoerd Meijerb3e06fa2018-07-06 14:47:09 +0000816INITIALIZE_PASS_BEGIN(ARMParallelDSP, "arm-parallel-dsp",
Sam Parkera761ba02019-08-28 08:51:13 +0000817 "Transform functions to use DSP intrinsics", false, false)
Sjoerd Meijerb3e06fa2018-07-06 14:47:09 +0000818INITIALIZE_PASS_END(ARMParallelDSP, "arm-parallel-dsp",
Sam Parkera761ba02019-08-28 08:51:13 +0000819 "Transform functions to use DSP intrinsics", false, false)