2019-08-28 16:51:13 +08:00
|
|
|
//===- ARMParallelDSP.cpp - Parallel DSP Pass -----------------------------===//
|
2018-06-28 20:55:29 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-06-28 20:55:29 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
/// \file
|
|
|
|
/// Armv6 introduced instructions to perform 32-bit SIMD operations. The
|
|
|
|
/// purpose of this pass is do some IR pattern matching to create ACLE
|
|
|
|
/// DSP intrinsics, which map on these 32-bit SIMD operations.
|
2018-07-11 20:36:25 +08:00
|
|
|
/// This pass runs only when unaligned accesses is supported/enabled.
|
2018-06-28 20:55:29 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-12-11 23:55:26 +08:00
|
|
|
#include "ARM.h"
|
|
|
|
#include "ARMSubtarget.h"
|
2018-06-28 20:55:29 +08:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2019-12-11 23:55:26 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2018-06-28 20:55:29 +08:00
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
2020-06-06 21:06:25 +08:00
|
|
|
#include "llvm/Analysis/AssumptionCache.h"
|
|
|
|
#include "llvm/Analysis/GlobalsModRef.h"
|
2018-06-28 20:55:29 +08:00
|
|
|
#include "llvm/Analysis/LoopAccessAnalysis.h"
|
2020-12-03 00:53:17 +08:00
|
|
|
#include "llvm/Analysis/TargetLibraryInfo.h"
|
2019-12-11 23:55:26 +08:00
|
|
|
#include "llvm/CodeGen/TargetPassConfig.h"
|
2018-06-28 20:55:29 +08:00
|
|
|
#include "llvm/IR/Instructions.h"
|
2019-12-11 23:55:26 +08:00
|
|
|
#include "llvm/IR/IntrinsicsARM.h"
|
2018-06-28 20:55:29 +08:00
|
|
|
#include "llvm/IR/NoFolder.h"
|
2019-12-11 23:55:26 +08:00
|
|
|
#include "llvm/IR/PatternMatch.h"
|
2018-06-28 20:55:29 +08:00
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/PassRegistry.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
2019-12-11 23:55:26 +08:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
|
|
|
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
|
2018-06-28 20:55:29 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace PatternMatch;
|
|
|
|
|
2018-07-06 22:47:09 +08:00
|
|
|
#define DEBUG_TYPE "arm-parallel-dsp"
|
|
|
|
|
|
|
|
STATISTIC(NumSMLAD , "Number of smlad instructions generated");
|
2018-06-28 20:55:29 +08:00
|
|
|
|
2018-08-14 15:43:49 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
DisableParallelDSP("disable-arm-parallel-dsp", cl::Hidden, cl::init(false),
|
|
|
|
cl::desc("Disable the ARM Parallel DSP pass"));
|
|
|
|
|
2019-10-16 17:37:03 +08:00
|
|
|
static cl::opt<unsigned>
|
|
|
|
NumLoadLimit("arm-parallel-dsp-load-limit", cl::Hidden, cl::init(16),
|
|
|
|
cl::desc("Limit the number of loads analysed"));
|
|
|
|
|
2018-06-28 20:55:29 +08:00
|
|
|
namespace {
|
2019-07-29 16:41:51 +08:00
|
|
|
struct MulCandidate;
|
2019-07-11 15:47:50 +08:00
|
|
|
class Reduction;
|
2018-06-28 20:55:29 +08:00
|
|
|
|
2019-08-02 16:21:17 +08:00
|
|
|
using MulCandList = SmallVector<std::unique_ptr<MulCandidate>, 8>;
|
|
|
|
using MemInstList = SmallVectorImpl<LoadInst*>;
|
|
|
|
using MulPairList = SmallVector<std::pair<MulCandidate*, MulCandidate*>, 8>;
|
2018-06-28 20:55:29 +08:00
|
|
|
|
2019-07-29 16:41:51 +08:00
|
|
|
// 'MulCandidate' holds the multiplication instructions that are candidates
|
2019-07-26 22:11:40 +08:00
|
|
|
// for parallel execution.
|
2019-07-29 16:41:51 +08:00
|
|
|
struct MulCandidate {
|
2018-07-23 23:25:59 +08:00
|
|
|
Instruction *Root;
|
2019-07-29 16:41:51 +08:00
|
|
|
Value* LHS;
|
|
|
|
Value* RHS;
|
2019-07-26 22:11:40 +08:00
|
|
|
bool Exchange = false;
|
2018-07-23 23:25:59 +08:00
|
|
|
bool ReadOnly = true;
|
2019-08-28 16:51:13 +08:00
|
|
|
bool Paired = false;
|
2019-08-02 16:21:17 +08:00
|
|
|
SmallVector<LoadInst*, 2> VecLd; // Container for loads to widen.
|
2018-07-23 23:25:59 +08:00
|
|
|
|
2019-08-02 15:32:28 +08:00
|
|
|
MulCandidate(Instruction *I, Value *lhs, Value *rhs) :
|
|
|
|
Root(I), LHS(lhs), RHS(rhs) { }
|
2018-07-23 23:25:59 +08:00
|
|
|
|
2019-07-29 16:41:51 +08:00
|
|
|
bool HasTwoLoadInputs() const {
|
|
|
|
return isa<LoadInst>(LHS) && isa<LoadInst>(RHS);
|
|
|
|
}
|
2019-08-01 16:17:51 +08:00
|
|
|
|
|
|
|
LoadInst *getBaseLoad() const {
|
2019-08-28 16:51:13 +08:00
|
|
|
return VecLd.front();
|
2019-08-01 16:17:51 +08:00
|
|
|
}
|
2018-06-28 20:55:29 +08:00
|
|
|
};
|
|
|
|
|
2019-07-11 15:47:50 +08:00
|
|
|
/// Represent a sequence of multiply-accumulate operations with the aim to
|
|
|
|
/// perform the multiplications in parallel.
|
|
|
|
class Reduction {
|
|
|
|
Instruction *Root = nullptr;
|
|
|
|
Value *Acc = nullptr;
|
2019-07-29 16:41:51 +08:00
|
|
|
MulCandList Muls;
|
2019-08-02 16:21:17 +08:00
|
|
|
MulPairList MulPairs;
|
2019-08-28 16:51:13 +08:00
|
|
|
SetVector<Instruction*> Adds;
|
2019-07-11 15:47:50 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
Reduction() = delete;
|
|
|
|
|
|
|
|
Reduction (Instruction *Add) : Root(Add) { }
|
|
|
|
|
|
|
|
/// Record an Add instruction that is a part of the this reduction.
|
|
|
|
void InsertAdd(Instruction *I) { Adds.insert(I); }
|
|
|
|
|
2019-08-28 16:51:13 +08:00
|
|
|
/// Create MulCandidates, each rooted at a Mul instruction, that is a part
|
|
|
|
/// of this reduction.
|
|
|
|
void InsertMuls() {
|
|
|
|
auto GetMulOperand = [](Value *V) -> Instruction* {
|
|
|
|
if (auto *SExt = dyn_cast<SExtInst>(V)) {
|
|
|
|
if (auto *I = dyn_cast<Instruction>(SExt->getOperand(0)))
|
|
|
|
if (I->getOpcode() == Instruction::Mul)
|
|
|
|
return I;
|
|
|
|
} else if (auto *I = dyn_cast<Instruction>(V)) {
|
|
|
|
if (I->getOpcode() == Instruction::Mul)
|
|
|
|
return I;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto InsertMul = [this](Instruction *I) {
|
|
|
|
Value *LHS = cast<Instruction>(I->getOperand(0))->getOperand(0);
|
|
|
|
Value *RHS = cast<Instruction>(I->getOperand(1))->getOperand(0);
|
|
|
|
Muls.push_back(std::make_unique<MulCandidate>(I, LHS, RHS));
|
|
|
|
};
|
|
|
|
|
|
|
|
for (auto *Add : Adds) {
|
|
|
|
if (Add == Acc)
|
|
|
|
continue;
|
|
|
|
if (auto *Mul = GetMulOperand(Add->getOperand(0)))
|
|
|
|
InsertMul(Mul);
|
|
|
|
if (auto *Mul = GetMulOperand(Add->getOperand(1)))
|
|
|
|
InsertMul(Mul);
|
|
|
|
}
|
2019-07-11 15:47:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Add the incoming accumulator value, returns true if a value had not
|
|
|
|
/// already been added. Returning false signals to the user that this
|
|
|
|
/// reduction already has a value to initialise the accumulator.
|
|
|
|
bool InsertAcc(Value *V) {
|
|
|
|
if (Acc)
|
|
|
|
return false;
|
|
|
|
Acc = V;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-29 16:41:51 +08:00
|
|
|
/// Set two MulCandidates, rooted at muls, that can be executed as a single
|
2019-07-11 15:47:50 +08:00
|
|
|
/// parallel operation.
|
2019-08-28 16:51:13 +08:00
|
|
|
void AddMulPair(MulCandidate *Mul0, MulCandidate *Mul1,
|
|
|
|
bool Exchange = false) {
|
|
|
|
LLVM_DEBUG(dbgs() << "Pairing:\n"
|
|
|
|
<< *Mul0->Root << "\n"
|
|
|
|
<< *Mul1->Root << "\n");
|
|
|
|
Mul0->Paired = true;
|
|
|
|
Mul1->Paired = true;
|
|
|
|
if (Exchange)
|
|
|
|
Mul1->Exchange = true;
|
2019-07-11 15:47:50 +08:00
|
|
|
MulPairs.push_back(std::make_pair(Mul0, Mul1));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return true if enough mul operations are found that can be executed in
|
|
|
|
/// parallel.
|
|
|
|
bool CreateParallelPairs();
|
|
|
|
|
|
|
|
/// Return the add instruction which is the root of the reduction.
|
|
|
|
Instruction *getRoot() { return Root; }
|
|
|
|
|
2019-08-01 16:17:51 +08:00
|
|
|
bool is64Bit() const { return Root->getType()->isIntegerTy(64); }
|
|
|
|
|
2019-09-09 16:39:14 +08:00
|
|
|
Type *getType() const { return Root->getType(); }
|
|
|
|
|
2019-07-11 15:47:50 +08:00
|
|
|
/// Return the incoming value to be accumulated. This maybe null.
|
|
|
|
Value *getAccumulator() { return Acc; }
|
|
|
|
|
|
|
|
/// Return the set of adds that comprise the reduction.
|
2019-08-28 16:51:13 +08:00
|
|
|
SetVector<Instruction*> &getAdds() { return Adds; }
|
2019-07-11 15:47:50 +08:00
|
|
|
|
2019-07-29 16:41:51 +08:00
|
|
|
/// Return the MulCandidate, rooted at mul instruction, that comprise the
|
2019-07-11 15:47:50 +08:00
|
|
|
/// the reduction.
|
2019-07-29 16:41:51 +08:00
|
|
|
MulCandList &getMuls() { return Muls; }
|
2019-07-11 15:47:50 +08:00
|
|
|
|
2019-07-29 16:41:51 +08:00
|
|
|
/// Return the MulCandidate, rooted at mul instructions, that have been
|
2019-07-11 15:47:50 +08:00
|
|
|
/// paired for parallel execution.
|
2019-08-02 16:21:17 +08:00
|
|
|
MulPairList &getMulPairs() { return MulPairs; }
|
2019-07-11 15:47:50 +08:00
|
|
|
|
|
|
|
/// To finalise, replace the uses of the root with the intrinsic call.
|
|
|
|
void UpdateRoot(Instruction *SMLAD) {
|
|
|
|
Root->replaceAllUsesWith(SMLAD);
|
|
|
|
}
|
2019-08-28 16:51:13 +08:00
|
|
|
|
|
|
|
void dump() {
|
|
|
|
LLVM_DEBUG(dbgs() << "Reduction:\n";
|
|
|
|
for (auto *Add : Adds)
|
|
|
|
LLVM_DEBUG(dbgs() << *Add << "\n");
|
|
|
|
for (auto &Mul : Muls)
|
|
|
|
LLVM_DEBUG(dbgs() << *Mul->Root << "\n"
|
|
|
|
<< " " << *Mul->LHS << "\n"
|
|
|
|
<< " " << *Mul->RHS << "\n");
|
|
|
|
LLVM_DEBUG(if (Acc) dbgs() << "Acc in: " << *Acc << "\n")
|
|
|
|
);
|
|
|
|
}
|
2018-06-28 20:55:29 +08:00
|
|
|
};
|
|
|
|
|
2019-03-14 19:14:13 +08:00
|
|
|
class WidenedLoad {
|
|
|
|
LoadInst *NewLd = nullptr;
|
|
|
|
SmallVector<LoadInst*, 4> Loads;
|
|
|
|
|
|
|
|
public:
|
|
|
|
WidenedLoad(SmallVectorImpl<LoadInst*> &Lds, LoadInst *Wide)
|
|
|
|
: NewLd(Wide) {
|
2021-01-25 04:18:55 +08:00
|
|
|
append_range(Loads, Lds);
|
2019-03-14 19:14:13 +08:00
|
|
|
}
|
|
|
|
LoadInst *getLoad() {
|
|
|
|
return NewLd;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-08-28 16:51:13 +08:00
|
|
|
class ARMParallelDSP : public FunctionPass {
|
2018-06-28 20:55:29 +08:00
|
|
|
ScalarEvolution *SE;
|
|
|
|
AliasAnalysis *AA;
|
|
|
|
TargetLibraryInfo *TLI;
|
|
|
|
DominatorTree *DT;
|
|
|
|
const DataLayout *DL;
|
|
|
|
Module *M;
|
2018-11-09 17:18:00 +08:00
|
|
|
std::map<LoadInst*, LoadInst*> LoadPairs;
|
2019-07-11 15:47:50 +08:00
|
|
|
SmallPtrSet<LoadInst*, 4> OffsetLoads;
|
2019-03-14 19:14:13 +08:00
|
|
|
std::map<LoadInst*, std::unique_ptr<WidenedLoad>> WideLoads;
|
2018-06-28 20:55:29 +08:00
|
|
|
|
2019-07-11 15:47:50 +08:00
|
|
|
template<unsigned>
|
2019-08-28 16:51:13 +08:00
|
|
|
bool IsNarrowSequence(Value *V);
|
|
|
|
bool Search(Value *V, BasicBlock *BB, Reduction &R);
|
2019-05-13 17:23:32 +08:00
|
|
|
bool RecordMemoryOps(BasicBlock *BB);
|
2019-07-11 15:47:50 +08:00
|
|
|
void InsertParallelMACs(Reduction &Reduction);
|
2018-07-04 03:12:27 +08:00
|
|
|
bool AreSequentialLoads(LoadInst *Ld0, LoadInst *Ld1, MemInstList &VecMem);
|
2019-08-02 16:21:17 +08:00
|
|
|
LoadInst* CreateWideLoad(MemInstList &Loads, IntegerType *LoadTy);
|
2019-07-11 15:47:50 +08:00
|
|
|
bool CreateParallelPairs(Reduction &R);
|
2018-06-28 20:55:29 +08:00
|
|
|
|
|
|
|
/// Try to match and generate: SMLAD, SMLADX - Signed Multiply Accumulate
|
|
|
|
/// Dual performs two signed 16x16-bit multiplications. It adds the
|
|
|
|
/// products to a 32-bit accumulate operand. Optionally, the instruction can
|
|
|
|
/// exchange the halfwords of the second operand before performing the
|
|
|
|
/// arithmetic.
|
2019-08-28 16:51:13 +08:00
|
|
|
bool MatchSMLAD(Function &F);
|
2018-06-28 20:55:29 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
|
2019-08-28 16:51:13 +08:00
|
|
|
ARMParallelDSP() : FunctionPass(ID) { }
|
2019-05-13 17:23:32 +08:00
|
|
|
|
2018-06-28 20:55:29 +08:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2019-08-28 16:51:13 +08:00
|
|
|
FunctionPass::getAnalysisUsage(AU);
|
2018-06-28 20:55:29 +08:00
|
|
|
AU.addRequired<AssumptionCacheTracker>();
|
|
|
|
AU.addRequired<ScalarEvolutionWrapperPass>();
|
|
|
|
AU.addRequired<AAResultsWrapperPass>();
|
|
|
|
AU.addRequired<TargetLibraryInfoWrapperPass>();
|
|
|
|
AU.addRequired<DominatorTreeWrapperPass>();
|
|
|
|
AU.addRequired<TargetPassConfig>();
|
2019-08-28 16:51:13 +08:00
|
|
|
AU.addPreserved<ScalarEvolutionWrapperPass>();
|
|
|
|
AU.addPreserved<GlobalsAAWrapperPass>();
|
2018-06-28 20:55:29 +08:00
|
|
|
AU.setPreservesCFG();
|
|
|
|
}
|
|
|
|
|
2019-08-28 16:51:13 +08:00
|
|
|
bool runOnFunction(Function &F) override {
|
2018-08-14 15:43:49 +08:00
|
|
|
if (DisableParallelDSP)
|
|
|
|
return false;
|
2019-08-28 16:51:13 +08:00
|
|
|
if (skipFunction(F))
|
2019-07-24 04:48:46 +08:00
|
|
|
return false;
|
|
|
|
|
2018-06-28 20:55:29 +08:00
|
|
|
SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
|
|
|
|
AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
|
Change TargetLibraryInfo analysis passes to always require Function
Summary:
This is the first change to enable the TLI to be built per-function so
that -fno-builtin* handling can be migrated to use function attributes.
See discussion on D61634 for background. This is an enabler for fixing
handling of these options for LTO, for example.
This change should not affect behavior, as the provided function is not
yet used to build a specifically per-function TLI, but rather enables
that migration.
Most of the changes were very mechanical, e.g. passing a Function to the
legacy analysis pass's getTLI interface, or in Module level cases,
adding a callback. This is similar to the way the per-function TTI
analysis works.
There was one place where we were looking for builtins but not in the
context of a specific function. See FindCXAAtExit in
lib/Transforms/IPO/GlobalOpt.cpp. I'm somewhat concerned my workaround
could provide the wrong behavior in some corner cases. Suggestions
welcome.
Reviewers: chandlerc, hfinkel
Subscribers: arsenm, dschuff, jvesely, nhaehnle, mehdi_amini, javed.absar, sbc100, jgravelle-google, eraman, aheejin, steven_wu, george.burgess.iv, dexonsmith, jfb, asbirlea, gchatelet, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D66428
llvm-svn: 371284
2019-09-07 11:09:36 +08:00
|
|
|
TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
|
2018-06-28 20:55:29 +08:00
|
|
|
DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
|
|
|
|
auto &TPC = getAnalysis<TargetPassConfig>();
|
|
|
|
|
|
|
|
M = F.getParent();
|
|
|
|
DL = &M->getDataLayout();
|
|
|
|
|
|
|
|
auto &TM = TPC.getTM<TargetMachine>();
|
|
|
|
auto *ST = &TM.getSubtarget<ARMSubtarget>(F);
|
|
|
|
|
|
|
|
if (!ST->allowsUnalignedMem()) {
|
|
|
|
LLVM_DEBUG(dbgs() << "Unaligned memory access not supported: not "
|
|
|
|
"running pass ARMParallelDSP\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ST->hasDSP()) {
|
|
|
|
LLVM_DEBUG(dbgs() << "DSP extension not enabled: not running pass "
|
|
|
|
"ARMParallelDSP\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-15 18:19:32 +08:00
|
|
|
if (!ST->isLittle()) {
|
|
|
|
LLVM_DEBUG(dbgs() << "Only supporting little endian: not running pass "
|
2019-05-13 17:23:32 +08:00
|
|
|
<< "ARMParallelDSP\n");
|
2019-03-15 18:19:32 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-09-12 17:17:44 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "\n== Parallel DSP pass ==\n");
|
|
|
|
LLVM_DEBUG(dbgs() << " - " << F.getName() << "\n\n");
|
2018-11-09 17:18:00 +08:00
|
|
|
|
2019-08-28 16:51:13 +08:00
|
|
|
bool Changes = MatchSMLAD(F);
|
2018-06-28 20:55:29 +08:00
|
|
|
return Changes;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-07-03 20:44:16 +08:00
|
|
|
template<typename MemInst>
|
|
|
|
static bool AreSequentialAccesses(MemInst *MemOp0, MemInst *MemOp1,
|
2018-11-09 17:18:00 +08:00
|
|
|
const DataLayout &DL, ScalarEvolution &SE) {
|
2019-03-14 19:14:13 +08:00
|
|
|
if (isConsecutiveAccess(MemOp0, MemOp1, DL, SE))
|
2018-07-03 20:44:16 +08:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-06-28 20:55:29 +08:00
|
|
|
bool ARMParallelDSP::AreSequentialLoads(LoadInst *Ld0, LoadInst *Ld1,
|
2018-07-03 20:44:16 +08:00
|
|
|
MemInstList &VecMem) {
|
2018-06-28 20:55:29 +08:00
|
|
|
if (!Ld0 || !Ld1)
|
|
|
|
return false;
|
|
|
|
|
2019-03-14 19:14:13 +08:00
|
|
|
if (!LoadPairs.count(Ld0) || LoadPairs[Ld0] != Ld1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
LLVM_DEBUG(dbgs() << "Loads are sequential and valid:\n";
|
2018-06-28 20:55:29 +08:00
|
|
|
dbgs() << "Ld0:"; Ld0->dump();
|
|
|
|
dbgs() << "Ld1:"; Ld1->dump();
|
|
|
|
);
|
|
|
|
|
2018-11-09 17:18:00 +08:00
|
|
|
VecMem.clear();
|
|
|
|
VecMem.push_back(Ld0);
|
|
|
|
VecMem.push_back(Ld1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-11 15:47:50 +08:00
|
|
|
// MaxBitwidth: the maximum supported bitwidth of the elements in the DSP
|
|
|
|
// instructions, which is set to 16. So here we should collect all i8 and i16
|
|
|
|
// narrow operations.
|
|
|
|
// TODO: we currently only collect i16, and will support i8 later, so that's
|
|
|
|
// why we check that types are equal to MaxBitWidth, and not <= MaxBitWidth.
|
|
|
|
template<unsigned MaxBitWidth>
|
2019-08-28 16:51:13 +08:00
|
|
|
bool ARMParallelDSP::IsNarrowSequence(Value *V) {
|
2019-07-26 18:57:42 +08:00
|
|
|
if (auto *SExt = dyn_cast<SExtInst>(V)) {
|
|
|
|
if (SExt->getSrcTy()->getIntegerBitWidth() != MaxBitWidth)
|
2019-07-11 15:47:50 +08:00
|
|
|
return false;
|
|
|
|
|
2019-07-26 18:57:42 +08:00
|
|
|
if (auto *Ld = dyn_cast<LoadInst>(SExt->getOperand(0))) {
|
2019-08-28 16:51:13 +08:00
|
|
|
// Check that this load could be paired.
|
|
|
|
return LoadPairs.count(Ld) || OffsetLoads.count(Ld);
|
2019-07-11 15:47:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-13 17:23:32 +08:00
|
|
|
/// Iterate through the block and record base, offset pairs of loads which can
|
|
|
|
/// be widened into a single load.
|
|
|
|
bool ARMParallelDSP::RecordMemoryOps(BasicBlock *BB) {
|
2018-11-09 17:18:00 +08:00
|
|
|
SmallVector<LoadInst*, 8> Loads;
|
2019-05-13 17:23:32 +08:00
|
|
|
SmallVector<Instruction*, 8> Writes;
|
2019-08-28 16:51:13 +08:00
|
|
|
LoadPairs.clear();
|
|
|
|
WideLoads.clear();
|
2019-05-13 17:23:32 +08:00
|
|
|
|
|
|
|
// Collect loads and instruction that may write to memory. For now we only
|
|
|
|
// record loads which are simple, sign-extended and have a single user.
|
|
|
|
// TODO: Allow zero-extended loads.
|
2019-03-14 19:14:13 +08:00
|
|
|
for (auto &I : *BB) {
|
2019-05-13 17:23:32 +08:00
|
|
|
if (I.mayWriteToMemory())
|
|
|
|
Writes.push_back(&I);
|
2018-11-09 17:18:00 +08:00
|
|
|
auto *Ld = dyn_cast<LoadInst>(&I);
|
2019-03-14 19:14:13 +08:00
|
|
|
if (!Ld || !Ld->isSimple() ||
|
|
|
|
!Ld->hasOneUse() || !isa<SExtInst>(Ld->user_back()))
|
2018-11-09 17:18:00 +08:00
|
|
|
continue;
|
|
|
|
Loads.push_back(Ld);
|
|
|
|
}
|
|
|
|
|
2019-10-16 17:37:03 +08:00
|
|
|
if (Loads.empty() || Loads.size() > NumLoadLimit)
|
|
|
|
return false;
|
|
|
|
|
2019-05-13 17:23:32 +08:00
|
|
|
using InstSet = std::set<Instruction*>;
|
|
|
|
using DepMap = std::map<Instruction*, InstSet>;
|
|
|
|
DepMap RAWDeps;
|
|
|
|
|
|
|
|
// Record any writes that may alias a load.
|
2020-11-18 03:11:09 +08:00
|
|
|
const auto Size = LocationSize::beforeOrAfterPointer();
|
2019-10-16 17:37:03 +08:00
|
|
|
for (auto Write : Writes) {
|
|
|
|
for (auto Read : Loads) {
|
2019-05-13 17:23:32 +08:00
|
|
|
MemoryLocation ReadLoc =
|
|
|
|
MemoryLocation(Read->getPointerOperand(), Size);
|
|
|
|
|
|
|
|
if (!isModOrRefSet(intersectModRef(AA->getModRefInfo(Write, ReadLoc),
|
|
|
|
ModRefInfo::ModRef)))
|
2018-11-09 17:18:00 +08:00
|
|
|
continue;
|
[IR] Lazily number instructions for local dominance queries
Essentially, fold OrderedBasicBlock into BasicBlock, and make it
auto-invalidate the instruction ordering when new instructions are
added. Notably, we don't need to invalidate it when removing
instructions, which is helpful when a pass mostly delete dead
instructions rather than transforming them.
The downside is that Instruction grows from 56 bytes to 64 bytes. The
resulting LLVM code is substantially simpler and automatically handles
invalidation, which makes me think that this is the right speed and size
tradeoff.
The important change is in SymbolTableTraitsImpl.h, where the numbering
is invalidated. Everything else should be straightforward.
We probably want to implement a fancier re-numbering scheme so that
local updates don't invalidate the ordering, but I plan for that to be
future work, maybe for someone else.
Reviewed By: lattner, vsk, fhahn, dexonsmith
Differential Revision: https://reviews.llvm.org/D51664
2020-02-19 06:33:54 +08:00
|
|
|
if (Write->comesBefore(Read))
|
2019-05-13 17:23:32 +08:00
|
|
|
RAWDeps[Read].insert(Write);
|
|
|
|
}
|
|
|
|
}
|
2018-11-09 17:18:00 +08:00
|
|
|
|
2019-05-13 17:23:32 +08:00
|
|
|
// Check whether there's not a write between the two loads which would
|
|
|
|
// prevent them from being safely merged.
|
|
|
|
auto SafeToPair = [&](LoadInst *Base, LoadInst *Offset) {
|
[IR] Lazily number instructions for local dominance queries
Essentially, fold OrderedBasicBlock into BasicBlock, and make it
auto-invalidate the instruction ordering when new instructions are
added. Notably, we don't need to invalidate it when removing
instructions, which is helpful when a pass mostly delete dead
instructions rather than transforming them.
The downside is that Instruction grows from 56 bytes to 64 bytes. The
resulting LLVM code is substantially simpler and automatically handles
invalidation, which makes me think that this is the right speed and size
tradeoff.
The important change is in SymbolTableTraitsImpl.h, where the numbering
is invalidated. Everything else should be straightforward.
We probably want to implement a fancier re-numbering scheme so that
local updates don't invalidate the ordering, but I plan for that to be
future work, maybe for someone else.
Reviewed By: lattner, vsk, fhahn, dexonsmith
Differential Revision: https://reviews.llvm.org/D51664
2020-02-19 06:33:54 +08:00
|
|
|
bool BaseFirst = Base->comesBefore(Offset);
|
|
|
|
LoadInst *Dominator = BaseFirst ? Base : Offset;
|
|
|
|
LoadInst *Dominated = BaseFirst ? Offset : Base;
|
2019-05-13 17:23:32 +08:00
|
|
|
|
|
|
|
if (RAWDeps.count(Dominated)) {
|
|
|
|
InstSet &WritesBefore = RAWDeps[Dominated];
|
|
|
|
|
|
|
|
for (auto Before : WritesBefore) {
|
|
|
|
// We can't move the second load backward, past a write, to merge
|
|
|
|
// with the first load.
|
[IR] Lazily number instructions for local dominance queries
Essentially, fold OrderedBasicBlock into BasicBlock, and make it
auto-invalidate the instruction ordering when new instructions are
added. Notably, we don't need to invalidate it when removing
instructions, which is helpful when a pass mostly delete dead
instructions rather than transforming them.
The downside is that Instruction grows from 56 bytes to 64 bytes. The
resulting LLVM code is substantially simpler and automatically handles
invalidation, which makes me think that this is the right speed and size
tradeoff.
The important change is in SymbolTableTraitsImpl.h, where the numbering
is invalidated. Everything else should be straightforward.
We probably want to implement a fancier re-numbering scheme so that
local updates don't invalidate the ordering, but I plan for that to be
future work, maybe for someone else.
Reviewed By: lattner, vsk, fhahn, dexonsmith
Differential Revision: https://reviews.llvm.org/D51664
2020-02-19 06:33:54 +08:00
|
|
|
if (Dominator->comesBefore(Before))
|
2019-05-13 17:23:32 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Record base, offset load pairs.
|
|
|
|
for (auto *Base : Loads) {
|
|
|
|
for (auto *Offset : Loads) {
|
2019-10-16 17:37:03 +08:00
|
|
|
if (Base == Offset || OffsetLoads.count(Offset))
|
2019-05-13 17:23:32 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (AreSequentialAccesses<LoadInst>(Base, Offset, *DL, *SE) &&
|
|
|
|
SafeToPair(Base, Offset)) {
|
|
|
|
LoadPairs[Base] = Offset;
|
2019-07-11 15:47:50 +08:00
|
|
|
OffsetLoads.insert(Offset);
|
2019-03-14 19:14:13 +08:00
|
|
|
break;
|
2018-11-09 17:18:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-14 19:14:13 +08:00
|
|
|
|
|
|
|
LLVM_DEBUG(if (!LoadPairs.empty()) {
|
|
|
|
dbgs() << "Consecutive load pairs:\n";
|
|
|
|
for (auto &MapIt : LoadPairs) {
|
|
|
|
LLVM_DEBUG(dbgs() << *MapIt.first << ", "
|
|
|
|
<< *MapIt.second << "\n");
|
|
|
|
}
|
|
|
|
});
|
2018-11-09 17:18:00 +08:00
|
|
|
return LoadPairs.size() > 1;
|
2018-06-28 20:55:29 +08:00
|
|
|
}
|
|
|
|
|
2019-08-28 16:51:13 +08:00
|
|
|
// Search recursively back through the operands to find a tree of values that
|
|
|
|
// form a multiply-accumulate chain. The search records the Add and Mul
|
|
|
|
// instructions that form the reduction and allows us to find a single value
|
|
|
|
// to be used as the initial input to the accumlator.
|
|
|
|
bool ARMParallelDSP::Search(Value *V, BasicBlock *BB, Reduction &R) {
|
|
|
|
// If we find a non-instruction, try to use it as the initial accumulator
|
|
|
|
// value. This may have already been found during the search in which case
|
|
|
|
// this function will return false, signaling a search fail.
|
|
|
|
auto *I = dyn_cast<Instruction>(V);
|
|
|
|
if (!I)
|
|
|
|
return R.InsertAcc(V);
|
|
|
|
|
|
|
|
if (I->getParent() != BB)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
switch (I->getOpcode()) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
case Instruction::PHI:
|
|
|
|
// Could be the accumulator value.
|
|
|
|
return R.InsertAcc(V);
|
|
|
|
case Instruction::Add: {
|
|
|
|
// Adds should be adding together two muls, or another add and a mul to
|
|
|
|
// be within the mac chain. One of the operands may also be the
|
|
|
|
// accumulator value at which point we should stop searching.
|
|
|
|
R.InsertAdd(I);
|
|
|
|
Value *LHS = I->getOperand(0);
|
|
|
|
Value *RHS = I->getOperand(1);
|
|
|
|
bool ValidLHS = Search(LHS, BB, R);
|
|
|
|
bool ValidRHS = Search(RHS, BB, R);
|
|
|
|
|
|
|
|
if (ValidLHS && ValidRHS)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return R.InsertAcc(I);
|
|
|
|
}
|
|
|
|
case Instruction::Mul: {
|
|
|
|
Value *MulOp0 = I->getOperand(0);
|
|
|
|
Value *MulOp1 = I->getOperand(1);
|
|
|
|
return IsNarrowSequence<16>(MulOp0) && IsNarrowSequence<16>(MulOp1);
|
|
|
|
}
|
|
|
|
case Instruction::SExt:
|
|
|
|
return Search(I->getOperand(0), BB, R);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The pass needs to identify integer add/sub reductions of 16-bit vector
|
2019-07-11 15:47:50 +08:00
|
|
|
// multiplications.
|
|
|
|
// To use SMLAD:
|
|
|
|
// 1) we first need to find integer add then look for this pattern:
|
|
|
|
//
|
|
|
|
// acc0 = ...
|
|
|
|
// ld0 = load i16
|
|
|
|
// sext0 = sext i16 %ld0 to i32
|
|
|
|
// ld1 = load i16
|
|
|
|
// sext1 = sext i16 %ld1 to i32
|
|
|
|
// mul0 = mul %sext0, %sext1
|
|
|
|
// ld2 = load i16
|
|
|
|
// sext2 = sext i16 %ld2 to i32
|
|
|
|
// ld3 = load i16
|
|
|
|
// sext3 = sext i16 %ld3 to i32
|
|
|
|
// mul1 = mul i32 %sext2, %sext3
|
|
|
|
// add0 = add i32 %mul0, %acc0
|
|
|
|
// acc1 = add i32 %add0, %mul1
|
|
|
|
//
|
|
|
|
// Which can be selected to:
|
|
|
|
//
|
|
|
|
// ldr r0
|
|
|
|
// ldr r1
|
|
|
|
// smlad r2, r0, r1, r2
|
|
|
|
//
|
|
|
|
// If constants are used instead of loads, these will need to be hoisted
|
|
|
|
// out and into a register.
|
|
|
|
//
|
|
|
|
// If loop invariants are used instead of loads, these need to be packed
|
|
|
|
// before the loop begins.
|
|
|
|
//
|
2019-08-28 16:51:13 +08:00
|
|
|
bool ARMParallelDSP::MatchSMLAD(Function &F) {
|
2019-07-11 15:47:50 +08:00
|
|
|
bool Changed = false;
|
|
|
|
|
2019-08-28 16:51:13 +08:00
|
|
|
for (auto &BB : F) {
|
|
|
|
SmallPtrSet<Instruction*, 4> AllAdds;
|
|
|
|
if (!RecordMemoryOps(&BB))
|
2019-07-11 15:47:50 +08:00
|
|
|
continue;
|
|
|
|
|
2019-08-28 16:51:13 +08:00
|
|
|
for (Instruction &I : reverse(BB)) {
|
|
|
|
if (I.getOpcode() != Instruction::Add)
|
|
|
|
continue;
|
2019-07-11 15:47:50 +08:00
|
|
|
|
2019-08-28 16:51:13 +08:00
|
|
|
if (AllAdds.count(&I))
|
|
|
|
continue;
|
2019-07-11 15:47:50 +08:00
|
|
|
|
2019-08-28 16:51:13 +08:00
|
|
|
const auto *Ty = I.getType();
|
|
|
|
if (!Ty->isIntegerTy(32) && !Ty->isIntegerTy(64))
|
|
|
|
continue;
|
2019-07-11 15:47:50 +08:00
|
|
|
|
2019-08-28 16:51:13 +08:00
|
|
|
Reduction R(&I);
|
|
|
|
if (!Search(&I, &BB, R))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
R.InsertMuls();
|
|
|
|
LLVM_DEBUG(dbgs() << "After search, Reduction:\n"; R.dump());
|
2019-07-31 15:32:03 +08:00
|
|
|
|
2019-08-28 16:51:13 +08:00
|
|
|
if (!CreateParallelPairs(R))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
InsertParallelMACs(R);
|
|
|
|
Changed = true;
|
|
|
|
AllAdds.insert(R.getAdds().begin(), R.getAdds().end());
|
|
|
|
}
|
2019-07-11 15:47:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ARMParallelDSP::CreateParallelPairs(Reduction &R) {
|
|
|
|
|
|
|
|
// Not enough mul operations to make a pair.
|
|
|
|
if (R.getMuls().size() < 2)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Check that the muls operate directly upon sign extended loads.
|
2019-07-29 16:41:51 +08:00
|
|
|
for (auto &MulCand : R.getMuls()) {
|
|
|
|
if (!MulCand->HasTwoLoadInputs())
|
2019-07-11 15:47:50 +08:00
|
|
|
return false;
|
|
|
|
}
|
2018-11-09 17:18:00 +08:00
|
|
|
|
2019-07-29 16:41:51 +08:00
|
|
|
auto CanPair = [&](Reduction &R, MulCandidate *PMul0, MulCandidate *PMul1) {
|
2018-11-09 17:18:00 +08:00
|
|
|
// The first elements of each vector should be loads with sexts. If we
|
|
|
|
// find that its two pairs of consecutive loads, then these can be
|
|
|
|
// transformed into two wider loads and the users can be replaced with
|
|
|
|
// DSP intrinsics.
|
2019-07-29 16:41:51 +08:00
|
|
|
auto Ld0 = static_cast<LoadInst*>(PMul0->LHS);
|
|
|
|
auto Ld1 = static_cast<LoadInst*>(PMul1->LHS);
|
|
|
|
auto Ld2 = static_cast<LoadInst*>(PMul0->RHS);
|
|
|
|
auto Ld3 = static_cast<LoadInst*>(PMul1->RHS);
|
|
|
|
|
2020-04-20 22:04:03 +08:00
|
|
|
// Check that each mul is operating on two different loads.
|
|
|
|
if (Ld0 == Ld2 || Ld1 == Ld3)
|
|
|
|
return false;
|
|
|
|
|
2019-07-29 16:41:51 +08:00
|
|
|
if (AreSequentialLoads(Ld0, Ld1, PMul0->VecLd)) {
|
|
|
|
if (AreSequentialLoads(Ld2, Ld3, PMul1->VecLd)) {
|
|
|
|
LLVM_DEBUG(dbgs() << "OK: found two pairs of parallel loads!\n");
|
|
|
|
R.AddMulPair(PMul0, PMul1);
|
|
|
|
return true;
|
|
|
|
} else if (AreSequentialLoads(Ld3, Ld2, PMul1->VecLd)) {
|
2018-11-09 17:18:00 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "OK: found two pairs of parallel loads!\n");
|
2019-07-29 16:41:51 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " exchanging Ld2 and Ld3\n");
|
2019-08-28 16:51:13 +08:00
|
|
|
R.AddMulPair(PMul0, PMul1, true);
|
2018-11-09 17:18:00 +08:00
|
|
|
return true;
|
|
|
|
}
|
2019-07-29 16:41:51 +08:00
|
|
|
} else if (AreSequentialLoads(Ld1, Ld0, PMul0->VecLd) &&
|
|
|
|
AreSequentialLoads(Ld2, Ld3, PMul1->VecLd)) {
|
|
|
|
LLVM_DEBUG(dbgs() << "OK: found two pairs of parallel loads!\n");
|
|
|
|
LLVM_DEBUG(dbgs() << " exchanging Ld0 and Ld1\n");
|
|
|
|
LLVM_DEBUG(dbgs() << " and swapping muls\n");
|
|
|
|
// Only the second operand can be exchanged, so swap the muls.
|
2019-08-28 16:51:13 +08:00
|
|
|
R.AddMulPair(PMul1, PMul0, true);
|
2019-07-29 16:41:51 +08:00
|
|
|
return true;
|
2018-11-09 17:18:00 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
2018-06-28 20:55:29 +08:00
|
|
|
|
2019-07-29 16:41:51 +08:00
|
|
|
MulCandList &Muls = R.getMuls();
|
2019-07-11 15:47:50 +08:00
|
|
|
const unsigned Elems = Muls.size();
|
2018-09-12 17:17:44 +08:00
|
|
|
for (unsigned i = 0; i < Elems; ++i) {
|
2019-07-29 16:41:51 +08:00
|
|
|
MulCandidate *PMul0 = static_cast<MulCandidate*>(Muls[i].get());
|
2019-08-28 16:51:13 +08:00
|
|
|
if (PMul0->Paired)
|
2018-06-28 20:55:29 +08:00
|
|
|
continue;
|
|
|
|
|
2018-09-12 17:17:44 +08:00
|
|
|
for (unsigned j = 0; j < Elems; ++j) {
|
|
|
|
if (i == j)
|
|
|
|
continue;
|
|
|
|
|
2019-07-29 16:41:51 +08:00
|
|
|
MulCandidate *PMul1 = static_cast<MulCandidate*>(Muls[j].get());
|
2019-08-28 16:51:13 +08:00
|
|
|
if (PMul1->Paired)
|
2018-09-12 17:17:44 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
const Instruction *Mul0 = PMul0->Root;
|
|
|
|
const Instruction *Mul1 = PMul1->Root;
|
|
|
|
if (Mul0 == Mul1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
assert(PMul0 != PMul1 && "expected different chains");
|
|
|
|
|
2019-08-28 16:51:13 +08:00
|
|
|
if (CanPair(R, PMul0, PMul1))
|
2018-09-12 17:17:44 +08:00
|
|
|
break;
|
2018-06-28 20:55:29 +08:00
|
|
|
}
|
|
|
|
}
|
2019-07-11 15:47:50 +08:00
|
|
|
return !R.getMulPairs().empty();
|
2018-06-28 20:55:29 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 15:47:50 +08:00
|
|
|
void ARMParallelDSP::InsertParallelMACs(Reduction &R) {
|
|
|
|
|
2019-08-01 16:17:51 +08:00
|
|
|
auto CreateSMLAD = [&](LoadInst* WideLd0, LoadInst *WideLd1,
|
|
|
|
Value *Acc, bool Exchange,
|
|
|
|
Instruction *InsertAfter) {
|
2019-07-11 15:47:50 +08:00
|
|
|
// Replace the reduction chain with an intrinsic call
|
|
|
|
|
|
|
|
Value* Args[] = { WideLd0, WideLd1, Acc };
|
|
|
|
Function *SMLAD = nullptr;
|
|
|
|
if (Exchange)
|
|
|
|
SMLAD = Acc->getType()->isIntegerTy(32) ?
|
|
|
|
Intrinsic::getDeclaration(M, Intrinsic::arm_smladx) :
|
|
|
|
Intrinsic::getDeclaration(M, Intrinsic::arm_smlaldx);
|
|
|
|
else
|
|
|
|
SMLAD = Acc->getType()->isIntegerTy(32) ?
|
|
|
|
Intrinsic::getDeclaration(M, Intrinsic::arm_smlad) :
|
|
|
|
Intrinsic::getDeclaration(M, Intrinsic::arm_smlald);
|
|
|
|
|
|
|
|
IRBuilder<NoFolder> Builder(InsertAfter->getParent(),
|
2019-10-16 17:37:03 +08:00
|
|
|
BasicBlock::iterator(InsertAfter));
|
2019-07-11 15:47:50 +08:00
|
|
|
Instruction *Call = Builder.CreateCall(SMLAD, Args);
|
|
|
|
NumSMLAD++;
|
|
|
|
return Call;
|
|
|
|
};
|
|
|
|
|
2019-10-16 17:37:03 +08:00
|
|
|
// Return the instruction after the dominated instruction.
|
|
|
|
auto GetInsertPoint = [this](Value *A, Value *B) {
|
|
|
|
assert((isa<Instruction>(A) || isa<Instruction>(B)) &&
|
|
|
|
"expected at least one instruction");
|
|
|
|
|
|
|
|
Value *V = nullptr;
|
|
|
|
if (!isa<Instruction>(A))
|
|
|
|
V = B;
|
|
|
|
else if (!isa<Instruction>(B))
|
|
|
|
V = A;
|
|
|
|
else
|
|
|
|
V = DT->dominates(cast<Instruction>(A), cast<Instruction>(B)) ? B : A;
|
|
|
|
|
|
|
|
return &*++BasicBlock::iterator(cast<Instruction>(V));
|
|
|
|
};
|
|
|
|
|
2019-07-11 15:47:50 +08:00
|
|
|
Value *Acc = R.getAccumulator();
|
2019-08-28 16:51:13 +08:00
|
|
|
|
|
|
|
// For any muls that were discovered but not paired, accumulate their values
|
|
|
|
// as before.
|
2019-10-16 17:37:03 +08:00
|
|
|
IRBuilder<NoFolder> Builder(R.getRoot()->getParent());
|
2019-08-28 16:51:13 +08:00
|
|
|
MulCandList &MulCands = R.getMuls();
|
|
|
|
for (auto &MulCand : MulCands) {
|
|
|
|
if (MulCand->Paired)
|
|
|
|
continue;
|
|
|
|
|
2019-10-16 17:37:03 +08:00
|
|
|
Instruction *Mul = cast<Instruction>(MulCand->Root);
|
2019-09-04 16:41:34 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Accumulating unpaired mul: " << *Mul << "\n");
|
|
|
|
|
2019-09-09 16:39:14 +08:00
|
|
|
if (R.getType() != Mul->getType()) {
|
2019-09-04 16:41:34 +08:00
|
|
|
assert(R.is64Bit() && "expected 64-bit result");
|
2019-10-16 17:37:03 +08:00
|
|
|
Builder.SetInsertPoint(&*++BasicBlock::iterator(Mul));
|
|
|
|
Mul = cast<Instruction>(Builder.CreateSExt(Mul, R.getRoot()->getType()));
|
2019-09-04 16:41:34 +08:00
|
|
|
}
|
|
|
|
|
2019-08-28 16:51:13 +08:00
|
|
|
if (!Acc) {
|
2019-09-04 16:41:34 +08:00
|
|
|
Acc = Mul;
|
2019-08-28 16:51:13 +08:00
|
|
|
continue;
|
|
|
|
}
|
2019-09-04 16:41:34 +08:00
|
|
|
|
2019-10-16 17:37:03 +08:00
|
|
|
// If Acc is the original incoming value to the reduction, it could be a
|
|
|
|
// phi. But the phi will dominate Mul, meaning that Mul will be the
|
|
|
|
// insertion point.
|
|
|
|
Builder.SetInsertPoint(GetInsertPoint(Mul, Acc));
|
2019-09-04 16:41:34 +08:00
|
|
|
Acc = Builder.CreateAdd(Mul, Acc);
|
2019-08-28 16:51:13 +08:00
|
|
|
}
|
|
|
|
|
2019-09-09 16:39:14 +08:00
|
|
|
if (!Acc) {
|
2019-09-04 16:41:34 +08:00
|
|
|
Acc = R.is64Bit() ?
|
|
|
|
ConstantInt::get(IntegerType::get(M->getContext(), 64), 0) :
|
|
|
|
ConstantInt::get(IntegerType::get(M->getContext(), 32), 0);
|
2019-09-09 16:39:14 +08:00
|
|
|
} else if (Acc->getType() != R.getType()) {
|
|
|
|
Builder.SetInsertPoint(R.getRoot());
|
|
|
|
Acc = Builder.CreateSExt(Acc, R.getType());
|
|
|
|
}
|
2019-07-11 15:47:50 +08:00
|
|
|
|
2019-10-16 17:37:03 +08:00
|
|
|
// Roughly sort the mul pairs in their program order.
|
[IR] Lazily number instructions for local dominance queries
Essentially, fold OrderedBasicBlock into BasicBlock, and make it
auto-invalidate the instruction ordering when new instructions are
added. Notably, we don't need to invalidate it when removing
instructions, which is helpful when a pass mostly delete dead
instructions rather than transforming them.
The downside is that Instruction grows from 56 bytes to 64 bytes. The
resulting LLVM code is substantially simpler and automatically handles
invalidation, which makes me think that this is the right speed and size
tradeoff.
The important change is in SymbolTableTraitsImpl.h, where the numbering
is invalidated. Everything else should be straightforward.
We probably want to implement a fancier re-numbering scheme so that
local updates don't invalidate the ordering, but I plan for that to be
future work, maybe for someone else.
Reviewed By: lattner, vsk, fhahn, dexonsmith
Differential Revision: https://reviews.llvm.org/D51664
2020-02-19 06:33:54 +08:00
|
|
|
llvm::sort(R.getMulPairs(), [](auto &PairA, auto &PairB) {
|
|
|
|
const Instruction *A = PairA.first->Root;
|
|
|
|
const Instruction *B = PairB.first->Root;
|
|
|
|
return A->comesBefore(B);
|
|
|
|
});
|
2019-10-16 17:37:03 +08:00
|
|
|
|
2019-08-01 16:17:51 +08:00
|
|
|
IntegerType *Ty = IntegerType::get(M->getContext(), 32);
|
2019-07-11 15:47:50 +08:00
|
|
|
for (auto &Pair : R.getMulPairs()) {
|
2019-08-01 16:17:51 +08:00
|
|
|
MulCandidate *LHSMul = Pair.first;
|
|
|
|
MulCandidate *RHSMul = Pair.second;
|
|
|
|
LoadInst *BaseLHS = LHSMul->getBaseLoad();
|
|
|
|
LoadInst *BaseRHS = RHSMul->getBaseLoad();
|
|
|
|
LoadInst *WideLHS = WideLoads.count(BaseLHS) ?
|
|
|
|
WideLoads[BaseLHS]->getLoad() : CreateWideLoad(LHSMul->VecLd, Ty);
|
|
|
|
LoadInst *WideRHS = WideLoads.count(BaseRHS) ?
|
|
|
|
WideLoads[BaseRHS]->getLoad() : CreateWideLoad(RHSMul->VecLd, Ty);
|
|
|
|
|
2019-10-16 17:37:03 +08:00
|
|
|
Instruction *InsertAfter = GetInsertPoint(WideLHS, WideRHS);
|
|
|
|
InsertAfter = GetInsertPoint(InsertAfter, Acc);
|
2019-08-01 16:17:51 +08:00
|
|
|
Acc = CreateSMLAD(WideLHS, WideRHS, Acc, RHSMul->Exchange, InsertAfter);
|
2018-06-28 20:55:29 +08:00
|
|
|
}
|
2019-07-11 15:47:50 +08:00
|
|
|
R.UpdateRoot(cast<Instruction>(Acc));
|
2018-06-28 20:55:29 +08:00
|
|
|
}
|
|
|
|
|
2019-08-02 16:21:17 +08:00
|
|
|
LoadInst* ARMParallelDSP::CreateWideLoad(MemInstList &Loads,
|
2019-05-13 17:23:32 +08:00
|
|
|
IntegerType *LoadTy) {
|
2019-03-14 19:14:13 +08:00
|
|
|
assert(Loads.size() == 2 && "currently only support widening two loads");
|
2019-05-13 17:23:32 +08:00
|
|
|
|
|
|
|
LoadInst *Base = Loads[0];
|
|
|
|
LoadInst *Offset = Loads[1];
|
|
|
|
|
|
|
|
Instruction *BaseSExt = dyn_cast<SExtInst>(Base->user_back());
|
|
|
|
Instruction *OffsetSExt = dyn_cast<SExtInst>(Offset->user_back());
|
|
|
|
|
|
|
|
assert((BaseSExt && OffsetSExt)
|
|
|
|
&& "Loads should have a single, extending, user");
|
|
|
|
|
|
|
|
std::function<void(Value*, Value*)> MoveBefore =
|
|
|
|
[&](Value *A, Value *B) -> void {
|
|
|
|
if (!isa<Instruction>(A) || !isa<Instruction>(B))
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto *Source = cast<Instruction>(A);
|
|
|
|
auto *Sink = cast<Instruction>(B);
|
|
|
|
|
|
|
|
if (DT->dominates(Source, Sink) ||
|
|
|
|
Source->getParent() != Sink->getParent() ||
|
|
|
|
isa<PHINode>(Source) || isa<PHINode>(Sink))
|
|
|
|
return;
|
|
|
|
|
|
|
|
Source->moveBefore(Sink);
|
2019-07-24 17:38:39 +08:00
|
|
|
for (auto &Op : Source->operands())
|
|
|
|
MoveBefore(Op, Source);
|
2019-05-13 17:23:32 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Insert the load at the point of the original dominating load.
|
|
|
|
LoadInst *DomLoad = DT->dominates(Base, Offset) ? Base : Offset;
|
|
|
|
IRBuilder<NoFolder> IRB(DomLoad->getParent(),
|
|
|
|
++BasicBlock::iterator(DomLoad));
|
|
|
|
|
|
|
|
// Bitcast the pointer to a wider type and create the wide load, while making
|
|
|
|
// sure to maintain the original alignment as this prevents ldrd from being
|
|
|
|
// generated when it could be illegal due to memory alignment.
|
|
|
|
const unsigned AddrSpace = DomLoad->getPointerAddressSpace();
|
|
|
|
Value *VecPtr = IRB.CreateBitCast(Base->getPointerOperand(),
|
2018-10-19 03:34:30 +08:00
|
|
|
LoadTy->getPointerTo(AddrSpace));
|
2020-01-23 18:33:12 +08:00
|
|
|
LoadInst *WideLoad = IRB.CreateAlignedLoad(LoadTy, VecPtr, Base->getAlign());
|
2019-05-13 17:23:32 +08:00
|
|
|
|
|
|
|
// Make sure everything is in the correct order in the basic block.
|
|
|
|
MoveBefore(Base->getPointerOperand(), VecPtr);
|
|
|
|
MoveBefore(VecPtr, WideLoad);
|
2019-03-14 19:14:13 +08:00
|
|
|
|
|
|
|
// From the wide load, create two values that equal the original two loads.
|
2019-05-13 17:23:32 +08:00
|
|
|
// Loads[0] needs trunc while Loads[1] needs a lshr and trunc.
|
|
|
|
// TODO: Support big-endian as well.
|
|
|
|
Value *Bottom = IRB.CreateTrunc(WideLoad, Base->getType());
|
2019-08-28 16:51:13 +08:00
|
|
|
Value *NewBaseSExt = IRB.CreateSExt(Bottom, BaseSExt->getType());
|
|
|
|
BaseSExt->replaceAllUsesWith(NewBaseSExt);
|
2019-03-14 19:14:13 +08:00
|
|
|
|
2019-05-13 17:23:32 +08:00
|
|
|
IntegerType *OffsetTy = cast<IntegerType>(Offset->getType());
|
|
|
|
Value *ShiftVal = ConstantInt::get(LoadTy, OffsetTy->getBitWidth());
|
2019-03-14 19:14:13 +08:00
|
|
|
Value *Top = IRB.CreateLShr(WideLoad, ShiftVal);
|
2019-05-13 17:23:32 +08:00
|
|
|
Value *Trunc = IRB.CreateTrunc(Top, OffsetTy);
|
2019-08-28 16:51:13 +08:00
|
|
|
Value *NewOffsetSExt = IRB.CreateSExt(Trunc, OffsetSExt->getType());
|
|
|
|
OffsetSExt->replaceAllUsesWith(NewOffsetSExt);
|
|
|
|
|
|
|
|
LLVM_DEBUG(dbgs() << "From Base and Offset:\n"
|
|
|
|
<< *Base << "\n" << *Offset << "\n"
|
|
|
|
<< "Created Wide Load:\n"
|
|
|
|
<< *WideLoad << "\n"
|
|
|
|
<< *Bottom << "\n"
|
|
|
|
<< *NewBaseSExt << "\n"
|
|
|
|
<< *Top << "\n"
|
|
|
|
<< *Trunc << "\n"
|
|
|
|
<< *NewOffsetSExt << "\n");
|
2019-05-13 17:23:32 +08:00
|
|
|
WideLoads.emplace(std::make_pair(Base,
|
2019-08-15 23:54:37 +08:00
|
|
|
std::make_unique<WidenedLoad>(Loads, WideLoad)));
|
2019-03-14 19:14:13 +08:00
|
|
|
return WideLoad;
|
2018-10-19 03:34:30 +08:00
|
|
|
}
|
|
|
|
|
2018-06-28 20:55:29 +08:00
|
|
|
Pass *llvm::createARMParallelDSPPass() {
|
|
|
|
return new ARMParallelDSP();
|
|
|
|
}
|
|
|
|
|
|
|
|
char ARMParallelDSP::ID = 0;
|
|
|
|
|
2018-07-06 22:47:09 +08:00
|
|
|
INITIALIZE_PASS_BEGIN(ARMParallelDSP, "arm-parallel-dsp",
|
2019-08-28 16:51:13 +08:00
|
|
|
"Transform functions to use DSP intrinsics", false, false)
|
2018-07-06 22:47:09 +08:00
|
|
|
INITIALIZE_PASS_END(ARMParallelDSP, "arm-parallel-dsp",
|
2019-08-28 16:51:13 +08:00
|
|
|
"Transform functions to use DSP intrinsics", false, false)
|