Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 1 | //===- ParallelDSP.cpp - Parallel DSP Pass --------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | /// \file |
| 11 | /// Armv6 introduced instructions to perform 32-bit SIMD operations. The |
| 12 | /// purpose of this pass is do some IR pattern matching to create ACLE |
| 13 | /// DSP intrinsics, which map on these 32-bit SIMD operations. |
Sjoerd Meijer | 53449da | 2018-07-11 12:36:25 +0000 | [diff] [blame] | 14 | /// This pass runs only when unaligned accesses is supported/enabled. |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
Sjoerd Meijer | b3e06fa | 2018-07-06 14:47:09 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/Statistic.h" |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallPtrSet.h" |
| 20 | #include "llvm/Analysis/AliasAnalysis.h" |
| 21 | #include "llvm/Analysis/LoopAccessAnalysis.h" |
| 22 | #include "llvm/Analysis/LoopPass.h" |
| 23 | #include "llvm/Analysis/LoopInfo.h" |
| 24 | #include "llvm/IR/Instructions.h" |
| 25 | #include "llvm/IR/NoFolder.h" |
| 26 | #include "llvm/Transforms/Scalar.h" |
| 27 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 28 | #include "llvm/Transforms/Utils/LoopUtils.h" |
| 29 | #include "llvm/Pass.h" |
| 30 | #include "llvm/PassRegistry.h" |
| 31 | #include "llvm/PassSupport.h" |
| 32 | #include "llvm/Support/Debug.h" |
| 33 | #include "llvm/IR/PatternMatch.h" |
| 34 | #include "llvm/CodeGen/TargetPassConfig.h" |
| 35 | #include "ARM.h" |
| 36 | #include "ARMSubtarget.h" |
| 37 | |
| 38 | using namespace llvm; |
| 39 | using namespace PatternMatch; |
| 40 | |
Sjoerd Meijer | b3e06fa | 2018-07-06 14:47:09 +0000 | [diff] [blame] | 41 | #define DEBUG_TYPE "arm-parallel-dsp" |
| 42 | |
| 43 | STATISTIC(NumSMLAD , "Number of smlad instructions generated"); |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 44 | |
Sjoerd Meijer | 3c859b3 | 2018-08-14 07:43:49 +0000 | [diff] [blame] | 45 | static cl::opt<bool> |
| 46 | DisableParallelDSP("disable-arm-parallel-dsp", cl::Hidden, cl::init(false), |
| 47 | cl::desc("Disable the ARM Parallel DSP pass")); |
| 48 | |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 49 | namespace { |
Sam Parker | 89a3799 | 2018-07-23 15:25:59 +0000 | [diff] [blame] | 50 | struct OpChain; |
| 51 | struct BinOpChain; |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 52 | struct Reduction; |
| 53 | |
Fangrui Song | 58407ca | 2018-07-23 17:43:21 +0000 | [diff] [blame] | 54 | using OpChainList = SmallVector<std::unique_ptr<OpChain>, 8>; |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 55 | using ReductionList = SmallVector<Reduction, 8>; |
| 56 | using ValueList = SmallVector<Value*, 8>; |
Sam Parker | ffc1681 | 2018-07-03 12:44:16 +0000 | [diff] [blame] | 57 | using MemInstList = SmallVector<Instruction*, 8>; |
Sam Parker | 2ef3c0d | 2018-10-17 13:02:48 +0000 | [diff] [blame] | 58 | using LoadInstList = SmallVector<LoadInst*, 8>; |
Sam Parker | 89a3799 | 2018-07-23 15:25:59 +0000 | [diff] [blame] | 59 | using PMACPair = std::pair<BinOpChain*,BinOpChain*>; |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 60 | using PMACPairList = SmallVector<PMACPair, 8>; |
| 61 | using Instructions = SmallVector<Instruction*,16>; |
| 62 | using MemLocList = SmallVector<MemoryLocation, 4>; |
| 63 | |
Sam Parker | 89a3799 | 2018-07-23 15:25:59 +0000 | [diff] [blame] | 64 | struct OpChain { |
| 65 | Instruction *Root; |
| 66 | ValueList AllValues; |
Sam Parker | 2ef3c0d | 2018-10-17 13:02:48 +0000 | [diff] [blame] | 67 | MemInstList VecLd; // List of all sequential load instructions. |
| 68 | LoadInstList Loads; // List of all load instructions. |
Sam Parker | 89a3799 | 2018-07-23 15:25:59 +0000 | [diff] [blame] | 69 | MemLocList MemLocs; // All memory locations read by this tree. |
| 70 | bool ReadOnly = true; |
| 71 | |
| 72 | OpChain(Instruction *I, ValueList &vl) : Root(I), AllValues(vl) { } |
Jordan Rupprecht | e5daf61 | 2018-07-23 17:38:05 +0000 | [diff] [blame] | 73 | virtual ~OpChain() = default; |
Sam Parker | 89a3799 | 2018-07-23 15:25:59 +0000 | [diff] [blame] | 74 | |
| 75 | void SetMemoryLocations() { |
George Burgess IV | 6ef8002 | 2018-10-10 21:28:44 +0000 | [diff] [blame] | 76 | const auto Size = LocationSize::unknown(); |
Sam Parker | 89a3799 | 2018-07-23 15:25:59 +0000 | [diff] [blame] | 77 | for (auto *V : AllValues) { |
| 78 | if (auto *I = dyn_cast<Instruction>(V)) { |
| 79 | if (I->mayWriteToMemory()) |
| 80 | ReadOnly = false; |
Sam Parker | 2ef3c0d | 2018-10-17 13:02:48 +0000 | [diff] [blame] | 81 | if (auto *Ld = dyn_cast<LoadInst>(V)) { |
Sam Parker | 89a3799 | 2018-07-23 15:25:59 +0000 | [diff] [blame] | 82 | MemLocs.push_back(MemoryLocation(Ld->getPointerOperand(), Size)); |
Sam Parker | 2ef3c0d | 2018-10-17 13:02:48 +0000 | [diff] [blame] | 83 | Loads.push_back(Ld); |
| 84 | } |
Sam Parker | 89a3799 | 2018-07-23 15:25:59 +0000 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | unsigned size() const { return AllValues.size(); } |
| 90 | }; |
| 91 | |
| 92 | // 'BinOpChain' and 'Reduction' are just some bookkeeping data structures. |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 93 | // 'Reduction' contains the phi-node and accumulator statement from where we |
Sam Parker | 89a3799 | 2018-07-23 15:25:59 +0000 | [diff] [blame] | 94 | // start pattern matching, and 'BinOpChain' the multiplication |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 95 | // instructions that are candidates for parallel execution. |
Sam Parker | 89a3799 | 2018-07-23 15:25:59 +0000 | [diff] [blame] | 96 | struct BinOpChain : public OpChain { |
| 97 | ValueList LHS; // List of all (narrow) left hand operands. |
| 98 | ValueList RHS; // List of all (narrow) right hand operands. |
Sam Parker | a023c7a | 2018-09-12 09:17:44 +0000 | [diff] [blame] | 99 | bool Exchange = false; |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 100 | |
Sam Parker | 89a3799 | 2018-07-23 15:25:59 +0000 | [diff] [blame] | 101 | BinOpChain(Instruction *I, ValueList &lhs, ValueList &rhs) : |
| 102 | OpChain(I, lhs), LHS(lhs), RHS(rhs) { |
| 103 | for (auto *V : RHS) |
| 104 | AllValues.push_back(V); |
| 105 | } |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | struct Reduction { |
| 109 | PHINode *Phi; // The Phi-node from where we start |
| 110 | // pattern matching. |
| 111 | Instruction *AccIntAdd; // The accumulating integer add statement, |
| 112 | // i.e, the reduction statement. |
| 113 | |
Sam Parker | 89a3799 | 2018-07-23 15:25:59 +0000 | [diff] [blame] | 114 | OpChainList MACCandidates; // The MAC candidates associated with |
Sjoerd Meijer | 53449da | 2018-07-11 12:36:25 +0000 | [diff] [blame] | 115 | // this reduction statement. |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 116 | Reduction (PHINode *P, Instruction *Acc) : Phi(P), AccIntAdd(Acc) { }; |
| 117 | }; |
| 118 | |
| 119 | class ARMParallelDSP : public LoopPass { |
| 120 | ScalarEvolution *SE; |
| 121 | AliasAnalysis *AA; |
| 122 | TargetLibraryInfo *TLI; |
| 123 | DominatorTree *DT; |
| 124 | LoopInfo *LI; |
| 125 | Loop *L; |
| 126 | const DataLayout *DL; |
| 127 | Module *M; |
| 128 | |
| 129 | bool InsertParallelMACs(Reduction &Reduction, PMACPairList &PMACPairs); |
Fangrui Song | 6816934 | 2018-07-03 19:12:27 +0000 | [diff] [blame] | 130 | bool AreSequentialLoads(LoadInst *Ld0, LoadInst *Ld1, MemInstList &VecMem); |
Sam Parker | 89a3799 | 2018-07-23 15:25:59 +0000 | [diff] [blame] | 131 | PMACPairList CreateParallelMACPairs(OpChainList &Candidates); |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 132 | Instruction *CreateSMLADCall(LoadInst *VecLd0, LoadInst *VecLd1, |
Sam Parker | a023c7a | 2018-09-12 09:17:44 +0000 | [diff] [blame] | 133 | Instruction *Acc, bool Exchange, |
| 134 | Instruction *InsertAfter); |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 135 | |
| 136 | /// Try to match and generate: SMLAD, SMLADX - Signed Multiply Accumulate |
| 137 | /// Dual performs two signed 16x16-bit multiplications. It adds the |
| 138 | /// products to a 32-bit accumulate operand. Optionally, the instruction can |
| 139 | /// exchange the halfwords of the second operand before performing the |
| 140 | /// arithmetic. |
| 141 | bool MatchSMLAD(Function &F); |
Sam Parker | 2ef3c0d | 2018-10-17 13:02:48 +0000 | [diff] [blame] | 142 | bool MatchTopBottomMuls(BasicBlock *LoopBody); |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 143 | |
| 144 | public: |
| 145 | static char ID; |
| 146 | |
| 147 | ARMParallelDSP() : LoopPass(ID) { } |
| 148 | |
| 149 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 150 | LoopPass::getAnalysisUsage(AU); |
| 151 | AU.addRequired<AssumptionCacheTracker>(); |
| 152 | AU.addRequired<ScalarEvolutionWrapperPass>(); |
| 153 | AU.addRequired<AAResultsWrapperPass>(); |
| 154 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
| 155 | AU.addRequired<LoopInfoWrapperPass>(); |
| 156 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 157 | AU.addRequired<TargetPassConfig>(); |
| 158 | AU.addPreserved<LoopInfoWrapperPass>(); |
| 159 | AU.setPreservesCFG(); |
| 160 | } |
| 161 | |
| 162 | bool runOnLoop(Loop *TheLoop, LPPassManager &) override { |
Sjoerd Meijer | 3c859b3 | 2018-08-14 07:43:49 +0000 | [diff] [blame] | 163 | if (DisableParallelDSP) |
| 164 | return false; |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 165 | L = TheLoop; |
| 166 | SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE(); |
| 167 | AA = &getAnalysis<AAResultsWrapperPass>().getAAResults(); |
| 168 | TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); |
| 169 | DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
| 170 | LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
| 171 | auto &TPC = getAnalysis<TargetPassConfig>(); |
| 172 | |
| 173 | BasicBlock *Header = TheLoop->getHeader(); |
| 174 | if (!Header) |
| 175 | return false; |
| 176 | |
| 177 | // TODO: We assume the loop header and latch to be the same block. |
| 178 | // This is not a fundamental restriction, but lifting this would just |
| 179 | // require more work to do the transformation and then patch up the CFG. |
| 180 | if (Header != TheLoop->getLoopLatch()) { |
| 181 | LLVM_DEBUG(dbgs() << "The loop header is not the loop latch: not " |
| 182 | "running pass ARMParallelDSP\n"); |
| 183 | return false; |
| 184 | } |
| 185 | |
| 186 | Function &F = *Header->getParent(); |
| 187 | M = F.getParent(); |
| 188 | DL = &M->getDataLayout(); |
| 189 | |
| 190 | auto &TM = TPC.getTM<TargetMachine>(); |
| 191 | auto *ST = &TM.getSubtarget<ARMSubtarget>(F); |
| 192 | |
| 193 | if (!ST->allowsUnalignedMem()) { |
| 194 | LLVM_DEBUG(dbgs() << "Unaligned memory access not supported: not " |
| 195 | "running pass ARMParallelDSP\n"); |
| 196 | return false; |
| 197 | } |
| 198 | |
| 199 | if (!ST->hasDSP()) { |
| 200 | LLVM_DEBUG(dbgs() << "DSP extension not enabled: not running pass " |
| 201 | "ARMParallelDSP\n"); |
| 202 | return false; |
| 203 | } |
| 204 | |
| 205 | LoopAccessInfo LAI(L, SE, TLI, AA, DT, LI); |
| 206 | bool Changes = false; |
| 207 | |
Sam Parker | a023c7a | 2018-09-12 09:17:44 +0000 | [diff] [blame] | 208 | LLVM_DEBUG(dbgs() << "\n== Parallel DSP pass ==\n"); |
| 209 | LLVM_DEBUG(dbgs() << " - " << F.getName() << "\n\n"); |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 210 | Changes = MatchSMLAD(F); |
Sam Parker | 2ef3c0d | 2018-10-17 13:02:48 +0000 | [diff] [blame] | 211 | if (!Changes) |
| 212 | Changes = MatchTopBottomMuls(Header); |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 213 | return Changes; |
| 214 | } |
| 215 | }; |
| 216 | } |
| 217 | |
Sjoerd Meijer | 27be58b | 2018-07-05 08:21:40 +0000 | [diff] [blame] | 218 | // MaxBitwidth: the maximum supported bitwidth of the elements in the DSP |
| 219 | // instructions, which is set to 16. So here we should collect all i8 and i16 |
| 220 | // narrow operations. |
| 221 | // TODO: we currently only collect i16, and will support i8 later, so that's |
| 222 | // why we check that types are equal to MaxBitWidth, and not <= MaxBitWidth. |
| 223 | template<unsigned MaxBitWidth> |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 224 | static bool IsNarrowSequence(Value *V, ValueList &VL) { |
Sjoerd Meijer | 27be58b | 2018-07-05 08:21:40 +0000 | [diff] [blame] | 225 | LLVM_DEBUG(dbgs() << "Is narrow sequence? "; V->dump()); |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 226 | ConstantInt *CInt; |
| 227 | |
| 228 | if (match(V, m_ConstantInt(CInt))) { |
| 229 | // TODO: if a constant is used, it needs to fit within the bit width. |
| 230 | return false; |
| 231 | } |
| 232 | |
| 233 | auto *I = dyn_cast<Instruction>(V); |
| 234 | if (!I) |
| 235 | return false; |
| 236 | |
| 237 | Value *Val, *LHS, *RHS; |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 238 | if (match(V, m_Trunc(m_Value(Val)))) { |
Sjoerd Meijer | 27be58b | 2018-07-05 08:21:40 +0000 | [diff] [blame] | 239 | if (cast<TruncInst>(I)->getDestTy()->getIntegerBitWidth() == MaxBitWidth) |
| 240 | return IsNarrowSequence<MaxBitWidth>(Val, VL); |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 241 | } else if (match(V, m_Add(m_Value(LHS), m_Value(RHS)))) { |
| 242 | // TODO: we need to implement sadd16/sadd8 for this, which enables to |
| 243 | // also do the rewrite for smlad8.ll, but it is unsupported for now. |
Sjoerd Meijer | 27be58b | 2018-07-05 08:21:40 +0000 | [diff] [blame] | 244 | LLVM_DEBUG(dbgs() << "No, unsupported Op:\t"; I->dump()); |
| 245 | return false; |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 246 | } else if (match(V, m_ZExtOrSExt(m_Value(Val)))) { |
Sjoerd Meijer | 27be58b | 2018-07-05 08:21:40 +0000 | [diff] [blame] | 247 | if (cast<CastInst>(I)->getSrcTy()->getIntegerBitWidth() != MaxBitWidth) { |
| 248 | LLVM_DEBUG(dbgs() << "No, wrong SrcTy size: " << |
| 249 | cast<CastInst>(I)->getSrcTy()->getIntegerBitWidth() << "\n"); |
| 250 | return false; |
| 251 | } |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 252 | |
Sjoerd Meijer | 27be58b | 2018-07-05 08:21:40 +0000 | [diff] [blame] | 253 | if (match(Val, m_Load(m_Value()))) { |
| 254 | LLVM_DEBUG(dbgs() << "Yes, found narrow Load:\t"; Val->dump()); |
| 255 | VL.push_back(Val); |
| 256 | VL.push_back(I); |
| 257 | return true; |
| 258 | } |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 259 | } |
Sjoerd Meijer | 27be58b | 2018-07-05 08:21:40 +0000 | [diff] [blame] | 260 | LLVM_DEBUG(dbgs() << "No, unsupported Op:\t"; I->dump()); |
| 261 | return false; |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | // Element-by-element comparison of Value lists returning true if they are |
| 265 | // instructions with the same opcode or constants with the same value. |
| 266 | static bool AreSymmetrical(const ValueList &VL0, |
| 267 | const ValueList &VL1) { |
| 268 | if (VL0.size() != VL1.size()) { |
| 269 | LLVM_DEBUG(dbgs() << "Muls are mismatching operand list lengths: " |
| 270 | << VL0.size() << " != " << VL1.size() << "\n"); |
| 271 | return false; |
| 272 | } |
| 273 | |
| 274 | const unsigned Pairs = VL0.size(); |
| 275 | LLVM_DEBUG(dbgs() << "Number of operand pairs: " << Pairs << "\n"); |
| 276 | |
| 277 | for (unsigned i = 0; i < Pairs; ++i) { |
| 278 | const Value *V0 = VL0[i]; |
| 279 | const Value *V1 = VL1[i]; |
| 280 | const auto *Inst0 = dyn_cast<Instruction>(V0); |
| 281 | const auto *Inst1 = dyn_cast<Instruction>(V1); |
| 282 | |
| 283 | LLVM_DEBUG(dbgs() << "Pair " << i << ":\n"; |
| 284 | dbgs() << "mul1: "; V0->dump(); |
| 285 | dbgs() << "mul2: "; V1->dump()); |
| 286 | |
| 287 | if (!Inst0 || !Inst1) |
| 288 | return false; |
| 289 | |
| 290 | if (Inst0->isSameOperationAs(Inst1)) { |
| 291 | LLVM_DEBUG(dbgs() << "OK: same operation found!\n"); |
| 292 | continue; |
| 293 | } |
| 294 | |
| 295 | const APInt *C0, *C1; |
| 296 | if (!(match(V0, m_APInt(C0)) && match(V1, m_APInt(C1)) && C0 == C1)) |
| 297 | return false; |
| 298 | } |
| 299 | |
| 300 | LLVM_DEBUG(dbgs() << "OK: found symmetrical operand lists.\n"); |
| 301 | return true; |
| 302 | } |
| 303 | |
Sam Parker | ffc1681 | 2018-07-03 12:44:16 +0000 | [diff] [blame] | 304 | template<typename MemInst> |
| 305 | static bool AreSequentialAccesses(MemInst *MemOp0, MemInst *MemOp1, |
| 306 | MemInstList &VecMem, const DataLayout &DL, |
| 307 | ScalarEvolution &SE) { |
| 308 | if (!MemOp0->isSimple() || !MemOp1->isSimple()) { |
| 309 | LLVM_DEBUG(dbgs() << "No, not touching volatile access\n"); |
| 310 | return false; |
| 311 | } |
| 312 | if (isConsecutiveAccess(MemOp0, MemOp1, DL, SE)) { |
Sam Parker | a023c7a | 2018-09-12 09:17:44 +0000 | [diff] [blame] | 313 | VecMem.clear(); |
Sam Parker | ffc1681 | 2018-07-03 12:44:16 +0000 | [diff] [blame] | 314 | VecMem.push_back(MemOp0); |
| 315 | VecMem.push_back(MemOp1); |
| 316 | LLVM_DEBUG(dbgs() << "OK: accesses are consecutive.\n"); |
| 317 | return true; |
| 318 | } |
| 319 | LLVM_DEBUG(dbgs() << "No, accesses aren't consecutive.\n"); |
| 320 | return false; |
| 321 | } |
| 322 | |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 323 | bool ARMParallelDSP::AreSequentialLoads(LoadInst *Ld0, LoadInst *Ld1, |
Sam Parker | ffc1681 | 2018-07-03 12:44:16 +0000 | [diff] [blame] | 324 | MemInstList &VecMem) { |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 325 | if (!Ld0 || !Ld1) |
| 326 | return false; |
| 327 | |
| 328 | LLVM_DEBUG(dbgs() << "Are consecutive loads:\n"; |
| 329 | dbgs() << "Ld0:"; Ld0->dump(); |
| 330 | dbgs() << "Ld1:"; Ld1->dump(); |
| 331 | ); |
| 332 | |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 333 | if (!Ld0->hasOneUse() || !Ld1->hasOneUse()) { |
| 334 | LLVM_DEBUG(dbgs() << "No, load has more than one use.\n"); |
| 335 | return false; |
| 336 | } |
Sam Parker | ffc1681 | 2018-07-03 12:44:16 +0000 | [diff] [blame] | 337 | |
| 338 | return AreSequentialAccesses<LoadInst>(Ld0, Ld1, VecMem, *DL, *SE); |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | PMACPairList |
Sam Parker | 89a3799 | 2018-07-23 15:25:59 +0000 | [diff] [blame] | 342 | ARMParallelDSP::CreateParallelMACPairs(OpChainList &Candidates) { |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 343 | const unsigned Elems = Candidates.size(); |
| 344 | PMACPairList PMACPairs; |
| 345 | |
| 346 | if (Elems < 2) |
| 347 | return PMACPairs; |
| 348 | |
Sam Parker | a023c7a | 2018-09-12 09:17:44 +0000 | [diff] [blame] | 349 | SmallPtrSet<const Instruction*, 4> Paired; |
| 350 | for (unsigned i = 0; i < Elems; ++i) { |
Fangrui Song | 58407ca | 2018-07-23 17:43:21 +0000 | [diff] [blame] | 351 | BinOpChain *PMul0 = static_cast<BinOpChain*>(Candidates[i].get()); |
Sam Parker | a023c7a | 2018-09-12 09:17:44 +0000 | [diff] [blame] | 352 | if (Paired.count(PMul0->Root)) |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 353 | continue; |
| 354 | |
Sam Parker | a023c7a | 2018-09-12 09:17:44 +0000 | [diff] [blame] | 355 | for (unsigned j = 0; j < Elems; ++j) { |
| 356 | if (i == j) |
| 357 | continue; |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 358 | |
Sam Parker | a023c7a | 2018-09-12 09:17:44 +0000 | [diff] [blame] | 359 | BinOpChain *PMul1 = static_cast<BinOpChain*>(Candidates[j].get()); |
| 360 | if (Paired.count(PMul1->Root)) |
| 361 | continue; |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 362 | |
Sam Parker | a023c7a | 2018-09-12 09:17:44 +0000 | [diff] [blame] | 363 | const Instruction *Mul0 = PMul0->Root; |
| 364 | const Instruction *Mul1 = PMul1->Root; |
| 365 | if (Mul0 == Mul1) |
| 366 | continue; |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 367 | |
Sam Parker | a023c7a | 2018-09-12 09:17:44 +0000 | [diff] [blame] | 368 | assert(PMul0 != PMul1 && "expected different chains"); |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 369 | |
Sam Parker | a023c7a | 2018-09-12 09:17:44 +0000 | [diff] [blame] | 370 | LLVM_DEBUG(dbgs() << "\nCheck parallel muls:\n"; |
| 371 | dbgs() << "- "; Mul0->dump(); |
| 372 | dbgs() << "- "; Mul1->dump()); |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 373 | |
Sam Parker | a023c7a | 2018-09-12 09:17:44 +0000 | [diff] [blame] | 374 | const ValueList &Mul0_LHS = PMul0->LHS; |
| 375 | const ValueList &Mul0_RHS = PMul0->RHS; |
| 376 | const ValueList &Mul1_LHS = PMul1->LHS; |
| 377 | const ValueList &Mul1_RHS = PMul1->RHS; |
| 378 | |
| 379 | if (!AreSymmetrical(Mul0_LHS, Mul1_LHS) || |
| 380 | !AreSymmetrical(Mul0_RHS, Mul1_RHS)) |
| 381 | continue; |
| 382 | |
| 383 | LLVM_DEBUG(dbgs() << "OK: mul operands list match:\n"); |
| 384 | // The first elements of each vector should be loads with sexts. If we |
| 385 | // find that its two pairs of consecutive loads, then these can be |
| 386 | // transformed into two wider loads and the users can be replaced with |
| 387 | // DSP intrinsics. |
| 388 | bool Found = false; |
| 389 | for (unsigned x = 0; x < Mul0_LHS.size(); x += 2) { |
| 390 | auto *Ld0 = dyn_cast<LoadInst>(Mul0_LHS[x]); |
| 391 | auto *Ld1 = dyn_cast<LoadInst>(Mul1_LHS[x]); |
| 392 | auto *Ld2 = dyn_cast<LoadInst>(Mul0_RHS[x]); |
| 393 | auto *Ld3 = dyn_cast<LoadInst>(Mul1_RHS[x]); |
| 394 | |
| 395 | if (!Ld0 || !Ld1 || !Ld2 || !Ld3) |
| 396 | continue; |
| 397 | |
| 398 | LLVM_DEBUG(dbgs() << "Looking at operands " << x << ":\n" |
| 399 | << "\t Ld0: " << *Ld0 << "\n" |
| 400 | << "\t Ld1: " << *Ld1 << "\n" |
| 401 | << "and operands " << x + 2 << ":\n" |
| 402 | << "\t Ld2: " << *Ld2 << "\n" |
| 403 | << "\t Ld3: " << *Ld3 << "\n"); |
| 404 | |
| 405 | if (AreSequentialLoads(Ld0, Ld1, PMul0->VecLd)) { |
| 406 | if (AreSequentialLoads(Ld2, Ld3, PMul1->VecLd)) { |
| 407 | LLVM_DEBUG(dbgs() << "OK: found two pairs of parallel loads!\n"); |
| 408 | PMACPairs.push_back(std::make_pair(PMul0, PMul1)); |
| 409 | Found = true; |
| 410 | } else if (AreSequentialLoads(Ld3, Ld2, PMul1->VecLd)) { |
| 411 | LLVM_DEBUG(dbgs() << "OK: found two pairs of parallel loads!\n"); |
| 412 | LLVM_DEBUG(dbgs() << " exchanging Ld2 and Ld3\n"); |
| 413 | PMul1->Exchange = true; |
| 414 | PMACPairs.push_back(std::make_pair(PMul0, PMul1)); |
| 415 | Found = true; |
| 416 | } |
| 417 | } else if (AreSequentialLoads(Ld1, Ld0, PMul0->VecLd)) { |
| 418 | if (AreSequentialLoads(Ld2, Ld3, PMul1->VecLd)) { |
| 419 | LLVM_DEBUG(dbgs() << "OK: found two pairs of parallel loads!\n"); |
| 420 | LLVM_DEBUG(dbgs() << " exchanging Ld0 and Ld1\n"); |
| 421 | LLVM_DEBUG(dbgs() << " and swapping muls\n"); |
| 422 | PMul0->Exchange = true; |
| 423 | // Only the second operand can be exchanged, so swap the muls. |
| 424 | PMACPairs.push_back(std::make_pair(PMul1, PMul0)); |
| 425 | Found = true; |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | if (Found) { |
| 430 | Paired.insert(Mul0); |
| 431 | Paired.insert(Mul1); |
| 432 | break; |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | } |
| 436 | return PMACPairs; |
| 437 | } |
| 438 | |
| 439 | bool ARMParallelDSP::InsertParallelMACs(Reduction &Reduction, |
| 440 | PMACPairList &PMACPairs) { |
| 441 | Instruction *Acc = Reduction.Phi; |
| 442 | Instruction *InsertAfter = Reduction.AccIntAdd; |
| 443 | |
| 444 | for (auto &Pair : PMACPairs) { |
Sam Parker | a023c7a | 2018-09-12 09:17:44 +0000 | [diff] [blame] | 445 | BinOpChain *PMul0 = Pair.first; |
| 446 | BinOpChain *PMul1 = Pair.second; |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 447 | LLVM_DEBUG(dbgs() << "Found parallel MACs!!\n"; |
Sam Parker | a023c7a | 2018-09-12 09:17:44 +0000 | [diff] [blame] | 448 | dbgs() << "- "; PMul0->Root->dump(); |
| 449 | dbgs() << "- "; PMul1->Root->dump()); |
| 450 | |
| 451 | auto *VecLd0 = cast<LoadInst>(PMul0->VecLd[0]); |
| 452 | auto *VecLd1 = cast<LoadInst>(PMul1->VecLd[0]); |
| 453 | Acc = CreateSMLADCall(VecLd0, VecLd1, Acc, PMul1->Exchange, InsertAfter); |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 454 | InsertAfter = Acc; |
| 455 | } |
| 456 | |
| 457 | if (Acc != Reduction.Phi) { |
| 458 | LLVM_DEBUG(dbgs() << "Replace Accumulate: "; Acc->dump()); |
| 459 | Reduction.AccIntAdd->replaceAllUsesWith(Acc); |
| 460 | return true; |
| 461 | } |
| 462 | return false; |
| 463 | } |
| 464 | |
Sam Parker | 89a3799 | 2018-07-23 15:25:59 +0000 | [diff] [blame] | 465 | static void MatchReductions(Function &F, Loop *TheLoop, BasicBlock *Header, |
| 466 | ReductionList &Reductions) { |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 467 | RecurrenceDescriptor RecDesc; |
| 468 | const bool HasFnNoNaNAttr = |
| 469 | F.getFnAttribute("no-nans-fp-math").getValueAsString() == "true"; |
| 470 | const BasicBlock *Latch = TheLoop->getLoopLatch(); |
| 471 | |
| 472 | // We need a preheader as getIncomingValueForBlock assumes there is one. |
Sjoerd Meijer | 53449da | 2018-07-11 12:36:25 +0000 | [diff] [blame] | 473 | if (!TheLoop->getLoopPreheader()) { |
| 474 | LLVM_DEBUG(dbgs() << "No preheader found, bailing out\n"); |
Sam Parker | 89a3799 | 2018-07-23 15:25:59 +0000 | [diff] [blame] | 475 | return; |
Sjoerd Meijer | 53449da | 2018-07-11 12:36:25 +0000 | [diff] [blame] | 476 | } |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 477 | |
| 478 | for (PHINode &Phi : Header->phis()) { |
| 479 | const auto *Ty = Phi.getType(); |
Sam Parker | 01db298 | 2018-09-11 14:01:22 +0000 | [diff] [blame] | 480 | if (!Ty->isIntegerTy(32) && !Ty->isIntegerTy(64)) |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 481 | continue; |
| 482 | |
| 483 | const bool IsReduction = |
| 484 | RecurrenceDescriptor::AddReductionVar(&Phi, |
| 485 | RecurrenceDescriptor::RK_IntegerAdd, |
| 486 | TheLoop, HasFnNoNaNAttr, RecDesc); |
| 487 | if (!IsReduction) |
| 488 | continue; |
| 489 | |
| 490 | Instruction *Acc = dyn_cast<Instruction>(Phi.getIncomingValueForBlock(Latch)); |
| 491 | if (!Acc) |
| 492 | continue; |
| 493 | |
| 494 | Reductions.push_back(Reduction(&Phi, Acc)); |
| 495 | } |
| 496 | |
| 497 | LLVM_DEBUG( |
| 498 | dbgs() << "\nAccumulating integer additions (reductions) found:\n"; |
Sam Parker | 89a3799 | 2018-07-23 15:25:59 +0000 | [diff] [blame] | 499 | for (auto &R : Reductions) { |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 500 | dbgs() << "- "; R.Phi->dump(); |
| 501 | dbgs() << "-> "; R.AccIntAdd->dump(); |
| 502 | } |
| 503 | ); |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 504 | } |
| 505 | |
Sam Parker | 2ef3c0d | 2018-10-17 13:02:48 +0000 | [diff] [blame] | 506 | static void AddMulCandidate(OpChainList &Candidates, |
Sam Parker | 01db298 | 2018-09-11 14:01:22 +0000 | [diff] [blame] | 507 | Instruction *Mul, |
| 508 | Value *MulOp0, Value *MulOp1) { |
Sam Parker | 2ef3c0d | 2018-10-17 13:02:48 +0000 | [diff] [blame] | 509 | LLVM_DEBUG(dbgs() << "OK, found mul:\t"; Mul->dump()); |
Sam Parker | 01db298 | 2018-09-11 14:01:22 +0000 | [diff] [blame] | 510 | assert(Mul->getOpcode() == Instruction::Mul && |
| 511 | "expected mul instruction"); |
Sam Parker | 89a3799 | 2018-07-23 15:25:59 +0000 | [diff] [blame] | 512 | ValueList LHS; |
| 513 | ValueList RHS; |
| 514 | if (IsNarrowSequence<16>(MulOp0, LHS) && |
| 515 | IsNarrowSequence<16>(MulOp1, RHS)) { |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 516 | LLVM_DEBUG(dbgs() << "OK, found narrow mul: "; Mul->dump()); |
Fangrui Song | 58407ca | 2018-07-23 17:43:21 +0000 | [diff] [blame] | 517 | Candidates.push_back(make_unique<BinOpChain>(Mul, LHS, RHS)); |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 518 | } |
| 519 | } |
| 520 | |
Sam Parker | 89a3799 | 2018-07-23 15:25:59 +0000 | [diff] [blame] | 521 | static void MatchParallelMACSequences(Reduction &R, |
| 522 | OpChainList &Candidates) { |
Sam Parker | a023c7a | 2018-09-12 09:17:44 +0000 | [diff] [blame] | 523 | Instruction *Acc = R.AccIntAdd; |
| 524 | LLVM_DEBUG(dbgs() << "\n- Analysing:\t" << *Acc); |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 525 | |
Sam Parker | a023c7a | 2018-09-12 09:17:44 +0000 | [diff] [blame] | 526 | // Returns false to signal the search should be stopped. |
| 527 | std::function<bool(Value*)> Match = |
| 528 | [&Candidates, &Match](Value *V) -> bool { |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 529 | |
Sam Parker | a023c7a | 2018-09-12 09:17:44 +0000 | [diff] [blame] | 530 | auto *I = dyn_cast<Instruction>(V); |
Sam Parker | 1187911 | 2018-09-12 09:58:56 +0000 | [diff] [blame] | 531 | if (!I) |
Sam Parker | a023c7a | 2018-09-12 09:17:44 +0000 | [diff] [blame] | 532 | return false; |
Sam Parker | 01db298 | 2018-09-11 14:01:22 +0000 | [diff] [blame] | 533 | |
Sam Parker | a023c7a | 2018-09-12 09:17:44 +0000 | [diff] [blame] | 534 | Value *MulOp0, *MulOp1; |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 535 | |
Sam Parker | a023c7a | 2018-09-12 09:17:44 +0000 | [diff] [blame] | 536 | switch (I->getOpcode()) { |
| 537 | case Instruction::Add: |
| 538 | if (Match(I->getOperand(0)) || (Match(I->getOperand(1)))) |
| 539 | return true; |
| 540 | break; |
| 541 | case Instruction::Mul: |
| 542 | if (match (I, (m_Mul(m_Value(MulOp0), m_Value(MulOp1))))) { |
Sam Parker | 2ef3c0d | 2018-10-17 13:02:48 +0000 | [diff] [blame] | 543 | AddMulCandidate(Candidates, I, MulOp0, MulOp1); |
Sam Parker | a023c7a | 2018-09-12 09:17:44 +0000 | [diff] [blame] | 544 | return false; |
| 545 | } |
| 546 | break; |
| 547 | case Instruction::SExt: |
| 548 | if (match (I, (m_SExt(m_Mul(m_Value(MulOp0), m_Value(MulOp1)))))) { |
| 549 | Instruction *Mul = cast<Instruction>(I->getOperand(0)); |
Sam Parker | 2ef3c0d | 2018-10-17 13:02:48 +0000 | [diff] [blame] | 550 | AddMulCandidate(Candidates, Mul, MulOp0, MulOp1); |
Sam Parker | a023c7a | 2018-09-12 09:17:44 +0000 | [diff] [blame] | 551 | return false; |
| 552 | } |
| 553 | break; |
| 554 | } |
| 555 | return false; |
| 556 | }; |
| 557 | |
| 558 | while (Match (Acc)); |
| 559 | LLVM_DEBUG(dbgs() << "Finished matching MAC sequences, found " |
| 560 | << Candidates.size() << " candidates.\n"); |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | // Collects all instructions that are not part of the MAC chains, which is the |
| 564 | // set of instructions that can potentially alias with the MAC operands. |
Sjoerd Meijer | 53449da | 2018-07-11 12:36:25 +0000 | [diff] [blame] | 565 | static void AliasCandidates(BasicBlock *Header, Instructions &Reads, |
| 566 | Instructions &Writes) { |
| 567 | for (auto &I : *Header) { |
| 568 | if (I.mayReadFromMemory()) |
| 569 | Reads.push_back(&I); |
| 570 | if (I.mayWriteToMemory()) |
| 571 | Writes.push_back(&I); |
| 572 | } |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 573 | } |
| 574 | |
Sjoerd Meijer | 53449da | 2018-07-11 12:36:25 +0000 | [diff] [blame] | 575 | // Check whether statements in the basic block that write to memory alias with |
| 576 | // the memory locations accessed by the MAC-chains. |
| 577 | // TODO: we need the read statements when we accept more complicated chains. |
| 578 | static bool AreAliased(AliasAnalysis *AA, Instructions &Reads, |
Sam Parker | 2ef3c0d | 2018-10-17 13:02:48 +0000 | [diff] [blame] | 579 | Instructions &Writes, OpChainList &Candidates) { |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 580 | LLVM_DEBUG(dbgs() << "Alias checks:\n"); |
Sam Parker | 2ef3c0d | 2018-10-17 13:02:48 +0000 | [diff] [blame] | 581 | for (auto &Candidate : Candidates) { |
| 582 | LLVM_DEBUG(dbgs() << "mul: "; Candidate->Root->dump()); |
| 583 | Candidate->SetMemoryLocations(); |
Sjoerd Meijer | 53449da | 2018-07-11 12:36:25 +0000 | [diff] [blame] | 584 | |
| 585 | // At the moment, we allow only simple chains that only consist of reads, |
| 586 | // accumulate their result with an integer add, and thus that don't write |
| 587 | // memory, and simply bail if they do. |
Sam Parker | 2ef3c0d | 2018-10-17 13:02:48 +0000 | [diff] [blame] | 588 | if (!Candidate->ReadOnly) |
Sjoerd Meijer | 53449da | 2018-07-11 12:36:25 +0000 | [diff] [blame] | 589 | return true; |
| 590 | |
| 591 | // Now for all writes in the basic block, check that they don't alias with |
| 592 | // the memory locations accessed by our MAC-chain: |
| 593 | for (auto *I : Writes) { |
| 594 | LLVM_DEBUG(dbgs() << "- "; I->dump()); |
Sam Parker | 2ef3c0d | 2018-10-17 13:02:48 +0000 | [diff] [blame] | 595 | assert(Candidate->MemLocs.size() >= 2 && "expecting at least 2 memlocs"); |
| 596 | for (auto &MemLoc : Candidate->MemLocs) { |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 597 | if (isModOrRefSet(intersectModRef(AA->getModRefInfo(I, MemLoc), |
| 598 | ModRefInfo::ModRef))) { |
| 599 | LLVM_DEBUG(dbgs() << "Yes, aliases found\n"); |
| 600 | return true; |
| 601 | } |
| 602 | } |
| 603 | } |
| 604 | } |
Sjoerd Meijer | 53449da | 2018-07-11 12:36:25 +0000 | [diff] [blame] | 605 | |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 606 | LLVM_DEBUG(dbgs() << "OK: no aliases found!\n"); |
| 607 | return false; |
| 608 | } |
| 609 | |
Sam Parker | 2ef3c0d | 2018-10-17 13:02:48 +0000 | [diff] [blame] | 610 | static bool CheckMulMemory(OpChainList &Candidates) { |
Fangrui Song | 58407ca | 2018-07-23 17:43:21 +0000 | [diff] [blame] | 611 | for (auto &C : Candidates) { |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 612 | // A mul has 2 operands, and a narrow op consist of sext and a load; thus |
| 613 | // we expect at least 4 items in this operand value list. |
Sam Parker | 89a3799 | 2018-07-23 15:25:59 +0000 | [diff] [blame] | 614 | if (C->size() < 4) { |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 615 | LLVM_DEBUG(dbgs() << "Operand list too short.\n"); |
| 616 | return false; |
| 617 | } |
Fangrui Song | 58407ca | 2018-07-23 17:43:21 +0000 | [diff] [blame] | 618 | ValueList &LHS = static_cast<BinOpChain*>(C.get())->LHS; |
| 619 | ValueList &RHS = static_cast<BinOpChain*>(C.get())->RHS; |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 620 | |
Sam Parker | 89a3799 | 2018-07-23 15:25:59 +0000 | [diff] [blame] | 621 | // Use +=2 to skip over the expected extend instructions. |
| 622 | for (unsigned i = 0, e = LHS.size(); i < e; i += 2) { |
| 623 | if (!isa<LoadInst>(LHS[i]) || !isa<LoadInst>(RHS[i])) |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 624 | return false; |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 625 | } |
| 626 | } |
| 627 | return true; |
| 628 | } |
| 629 | |
Sam Parker | 2ef3c0d | 2018-10-17 13:02:48 +0000 | [diff] [blame] | 630 | static LoadInst *CreateLoadIns(IRBuilder<NoFolder> &IRB, LoadInst *BaseLoad, |
| 631 | const Type *LoadTy) { |
| 632 | const unsigned AddrSpace = BaseLoad->getPointerAddressSpace(); |
| 633 | |
| 634 | Value *VecPtr = IRB.CreateBitCast(BaseLoad->getPointerOperand(), |
| 635 | LoadTy->getPointerTo(AddrSpace)); |
| 636 | return IRB.CreateAlignedLoad(VecPtr, BaseLoad->getAlignment()); |
| 637 | } |
| 638 | |
| 639 | /// Given two instructions, return the one that comes first in the basic block. |
| 640 | /// A work around for not being able to do > or < on bb iterators. |
| 641 | static Instruction* GetFirst(Instruction *A, Instruction *B) { |
| 642 | BasicBlock::iterator First(A); |
| 643 | BasicBlock::iterator Second(B); |
| 644 | |
| 645 | BasicBlock *BB = A->getParent(); |
| 646 | assert(BB == B->getParent() && |
| 647 | "Can't compare instructions in different blocks"); |
| 648 | BasicBlock::iterator Last = BB->end(); |
| 649 | |
| 650 | // Iterate through the block, if the 'First' iterator is found, then return |
| 651 | // Second. |
| 652 | while (Second != Last) { |
| 653 | if (Second == First) |
| 654 | return B; |
| 655 | ++Second; |
| 656 | } |
| 657 | return A; |
| 658 | } |
| 659 | |
| 660 | /// Attempt to widen loads and use smulbb, smulbt, smultb and smultt muls. |
| 661 | // TODO: This, like smlad generation, expects the leave operands to be loads |
| 662 | // that are sign extended. We should be able to handle scalar values as well |
| 663 | // performing these muls on word x half types to generate smulwb and smulwt. |
| 664 | bool ARMParallelDSP::MatchTopBottomMuls(BasicBlock *LoopBody) { |
| 665 | LLVM_DEBUG(dbgs() << "Attempting to find BT|TB muls.\n"); |
| 666 | |
| 667 | OpChainList Candidates; |
| 668 | for (auto &I : *LoopBody) { |
| 669 | if (I.getOpcode() == Instruction::Mul) { |
| 670 | Type *Ty = I.getType(); |
| 671 | if (Ty->isIntegerTy() && |
| 672 | (Ty->getScalarSizeInBits() == 32 || |
| 673 | Ty->getScalarSizeInBits() == 64)) |
| 674 | AddMulCandidate(Candidates, &I, I.getOperand(0), I.getOperand(1)); |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | if (Candidates.empty()) |
| 679 | return false; |
| 680 | |
| 681 | Instructions Reads; |
| 682 | Instructions Writes; |
| 683 | AliasCandidates(LoopBody, Reads, Writes); |
| 684 | |
| 685 | if (AreAliased(AA, Reads, Writes, Candidates)) |
| 686 | return false; |
| 687 | |
| 688 | DenseMap<LoadInst*, LoadInst*> SeqLoads; |
| 689 | SmallPtrSet<LoadInst*, 8> OffsetLoads; |
| 690 | |
| 691 | for (unsigned i = 0; i < Candidates.size(); ++i) { |
| 692 | for (unsigned j = 0; j < Candidates.size(); ++j) { |
| 693 | if (i == j) |
| 694 | continue; |
| 695 | |
| 696 | OpChain *MulChain0 = Candidates[i].get(); |
| 697 | OpChain *MulChain1 = Candidates[j].get(); |
| 698 | |
| 699 | for (auto *Ld0 : MulChain0->Loads) { |
| 700 | if (SeqLoads.count(Ld0) || OffsetLoads.count(Ld0)) |
| 701 | continue; |
| 702 | |
| 703 | for (auto *Ld1 : MulChain1->Loads) { |
| 704 | if (SeqLoads.count(Ld1) || OffsetLoads.count(Ld1)) |
| 705 | continue; |
| 706 | |
| 707 | MemInstList VecMem; |
| 708 | if (AreSequentialLoads(Ld0, Ld1, VecMem)) { |
| 709 | SeqLoads[Ld0] = Ld1; |
| 710 | OffsetLoads.insert(Ld1); |
| 711 | } |
| 712 | } |
| 713 | } |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | if (SeqLoads.empty()) |
| 718 | return false; |
| 719 | |
| 720 | IRBuilder<NoFolder> IRB(LoopBody); |
| 721 | const Type *Ty = IntegerType::get(M->getContext(), 32); |
| 722 | |
| 723 | auto IsUserMul = [](Use &U) { |
| 724 | auto *Mul = cast<Instruction>(U.getUser()); |
| 725 | return Mul->getOpcode() == Instruction::Mul; |
| 726 | }; |
| 727 | |
| 728 | LLVM_DEBUG(dbgs() << "Found some sequential loads, now widening:\n"); |
| 729 | for (auto &Pair : SeqLoads) { |
| 730 | LoadInst *BaseLd = Pair.first; |
| 731 | LoadInst *OffsetLd = Pair.second; |
| 732 | |
| 733 | // Check that all the base users are muls. |
| 734 | auto *BaseSExt = cast<Instruction>(BaseLd->user_back()); |
| 735 | for (Use &U : BaseSExt->uses()) { |
| 736 | if (!IsUserMul(U)) |
| 737 | return false; |
| 738 | } |
| 739 | |
| 740 | // Check that all the offset users are muls. |
| 741 | // TODO We exit early on finding a sext user which isn't a mul, but many |
| 742 | // arm instructions would be able to perform the necessary shift too. |
| 743 | auto *OffsetSExt = cast<Instruction>(OffsetLd->user_back()); |
| 744 | for (Use &U : OffsetSExt->uses()) { |
| 745 | if (!IsUserMul(U)) |
| 746 | return false; |
| 747 | } |
| 748 | |
| 749 | LLVM_DEBUG(dbgs() << " - with base load: " << *BaseLd << "\n"); |
| 750 | LLVM_DEBUG(dbgs() << " - with offset load: " << *OffsetLd << "\n"); |
| 751 | Instruction *InsertPt = GetFirst(BaseLd, OffsetLd); |
| 752 | IRB.SetInsertPoint(InsertPt); |
| 753 | LoadInst *WideLd = CreateLoadIns(IRB, BaseLd, Ty); |
| 754 | LLVM_DEBUG(dbgs() << " - created wide load: " << *WideLd << "\n"); |
| 755 | |
| 756 | // Move the pointer operands before their users. |
| 757 | std::function<void(Instruction*, Instruction*)> MoveBefore = |
| 758 | [&MoveBefore](Instruction *Source, Instruction *Sink) -> void { |
| 759 | Source->moveBefore(Sink); |
| 760 | for (Use &U : Source->operands()) { |
| 761 | Value *Op = U.get(); |
| 762 | if (auto *I = dyn_cast<Instruction>(Op)) { |
| 763 | if (isa<PHINode>(I) || I->getParent() != Source->getParent()) |
| 764 | continue; |
| 765 | MoveBefore(I, Source); |
| 766 | } |
| 767 | } |
| 768 | }; |
| 769 | |
| 770 | // If we're inserting the load before BaseLd, we probably need to move the |
| 771 | // the pointer operand too. This operand is cast to an i32* in |
| 772 | // CreateLoadIns. |
| 773 | if (InsertPt != BaseLd) { |
| 774 | if (auto *GEP = dyn_cast<GetElementPtrInst>(BaseLd->getPointerOperand())) |
| 775 | MoveBefore(GEP, cast<Instruction>(WideLd->getPointerOperand())); |
| 776 | } |
| 777 | |
| 778 | // BaseUser needs to: (asr (shl WideLoad, 16), 16) |
| 779 | // OffsetUser needs to: (asr WideLoad, 16) |
| 780 | auto *Top = cast<Instruction>(IRB.CreateAShr(WideLd, 16)); |
| 781 | auto *Shl = cast<Instruction>(IRB.CreateShl(WideLd, 16)); |
| 782 | auto *Bottom = cast<Instruction>(IRB.CreateAShr(Shl, 16)); |
| 783 | |
| 784 | BaseSExt->replaceAllUsesWith(Bottom); |
| 785 | OffsetSExt->replaceAllUsesWith(Top); |
| 786 | |
| 787 | BaseSExt->eraseFromParent(); |
| 788 | OffsetSExt->eraseFromParent(); |
| 789 | BaseLd->eraseFromParent(); |
| 790 | OffsetLd->eraseFromParent(); |
| 791 | } |
| 792 | LLVM_DEBUG(dbgs() << "Block after top bottom mul replacements:\n" |
| 793 | << *LoopBody << "\n"); |
| 794 | return true; |
| 795 | } |
| 796 | |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 797 | // Loop Pass that needs to identify integer add/sub reductions of 16-bit vector |
| 798 | // multiplications. |
| 799 | // To use SMLAD: |
| 800 | // 1) we first need to find integer add reduction PHIs, |
| 801 | // 2) then from the PHI, look for this pattern: |
| 802 | // |
| 803 | // acc0 = phi i32 [0, %entry], [%acc1, %loop.body] |
| 804 | // ld0 = load i16 |
| 805 | // sext0 = sext i16 %ld0 to i32 |
| 806 | // ld1 = load i16 |
| 807 | // sext1 = sext i16 %ld1 to i32 |
| 808 | // mul0 = mul %sext0, %sext1 |
| 809 | // ld2 = load i16 |
| 810 | // sext2 = sext i16 %ld2 to i32 |
| 811 | // ld3 = load i16 |
| 812 | // sext3 = sext i16 %ld3 to i32 |
| 813 | // mul1 = mul i32 %sext2, %sext3 |
| 814 | // add0 = add i32 %mul0, %acc0 |
| 815 | // acc1 = add i32 %add0, %mul1 |
| 816 | // |
| 817 | // Which can be selected to: |
| 818 | // |
| 819 | // ldr.h r0 |
| 820 | // ldr.h r1 |
| 821 | // smlad r2, r0, r1, r2 |
| 822 | // |
| 823 | // If constants are used instead of loads, these will need to be hoisted |
| 824 | // out and into a register. |
| 825 | // |
| 826 | // If loop invariants are used instead of loads, these need to be packed |
| 827 | // before the loop begins. |
| 828 | // |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 829 | bool ARMParallelDSP::MatchSMLAD(Function &F) { |
| 830 | BasicBlock *Header = L->getHeader(); |
| 831 | LLVM_DEBUG(dbgs() << "= Matching SMLAD =\n"; |
| 832 | dbgs() << "Header block:\n"; Header->dump(); |
| 833 | dbgs() << "Loop info:\n\n"; L->dump()); |
| 834 | |
Sam Parker | 89a3799 | 2018-07-23 15:25:59 +0000 | [diff] [blame] | 835 | ReductionList Reductions; |
| 836 | MatchReductions(F, L, Header, Reductions); |
Sam Parker | 2ef3c0d | 2018-10-17 13:02:48 +0000 | [diff] [blame] | 837 | if (Reductions.empty()) |
| 838 | return false; |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 839 | |
| 840 | for (auto &R : Reductions) { |
Sam Parker | 89a3799 | 2018-07-23 15:25:59 +0000 | [diff] [blame] | 841 | OpChainList MACCandidates; |
| 842 | MatchParallelMACSequences(R, MACCandidates); |
Sam Parker | 2ef3c0d | 2018-10-17 13:02:48 +0000 | [diff] [blame] | 843 | if (!CheckMulMemory(MACCandidates)) |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 844 | continue; |
Sam Parker | 89a3799 | 2018-07-23 15:25:59 +0000 | [diff] [blame] | 845 | |
Fangrui Song | 58407ca | 2018-07-23 17:43:21 +0000 | [diff] [blame] | 846 | R.MACCandidates = std::move(MACCandidates); |
Sjoerd Meijer | 53449da | 2018-07-11 12:36:25 +0000 | [diff] [blame] | 847 | |
| 848 | LLVM_DEBUG(dbgs() << "MAC candidates:\n"; |
| 849 | for (auto &M : R.MACCandidates) |
Sam Parker | 89a3799 | 2018-07-23 15:25:59 +0000 | [diff] [blame] | 850 | M->Root->dump(); |
Sjoerd Meijer | 53449da | 2018-07-11 12:36:25 +0000 | [diff] [blame] | 851 | dbgs() << "\n";); |
| 852 | } |
| 853 | |
| 854 | // Collect all instructions that may read or write memory. Our alias |
| 855 | // analysis checks bail out if any of these instructions aliases with an |
| 856 | // instruction from the MAC-chain. |
| 857 | Instructions Reads, Writes; |
| 858 | AliasCandidates(Header, Reads, Writes); |
| 859 | |
Sam Parker | 2ef3c0d | 2018-10-17 13:02:48 +0000 | [diff] [blame] | 860 | bool Changed = false; |
Sjoerd Meijer | 53449da | 2018-07-11 12:36:25 +0000 | [diff] [blame] | 861 | for (auto &R : Reductions) { |
| 862 | if (AreAliased(AA, Reads, Writes, R.MACCandidates)) |
| 863 | return false; |
| 864 | PMACPairList PMACPairs = CreateParallelMACPairs(R.MACCandidates); |
| 865 | Changed |= InsertParallelMACs(R, PMACPairs); |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | LLVM_DEBUG(if (Changed) dbgs() << "Header block:\n"; Header->dump();); |
| 869 | return Changed; |
| 870 | } |
| 871 | |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 872 | Instruction *ARMParallelDSP::CreateSMLADCall(LoadInst *VecLd0, LoadInst *VecLd1, |
Sam Parker | a023c7a | 2018-09-12 09:17:44 +0000 | [diff] [blame] | 873 | Instruction *Acc, bool Exchange, |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 874 | Instruction *InsertAfter) { |
Sam Parker | a023c7a | 2018-09-12 09:17:44 +0000 | [diff] [blame] | 875 | LLVM_DEBUG(dbgs() << "Create SMLAD intrinsic using:\n" |
| 876 | << "- " << *VecLd0 << "\n" |
| 877 | << "- " << *VecLd1 << "\n" |
| 878 | << "- " << *Acc << "\n" |
| 879 | << "Exchange: " << Exchange << "\n"); |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 880 | |
| 881 | IRBuilder<NoFolder> Builder(InsertAfter->getParent(), |
| 882 | ++BasicBlock::iterator(InsertAfter)); |
| 883 | |
| 884 | // Replace the reduction chain with an intrinsic call |
Sam Parker | 01db298 | 2018-09-11 14:01:22 +0000 | [diff] [blame] | 885 | const Type *Ty = IntegerType::get(M->getContext(), 32); |
Sam Parker | 2ef3c0d | 2018-10-17 13:02:48 +0000 | [diff] [blame] | 886 | LoadInst *NewLd0 = CreateLoadIns(Builder, &VecLd0[0], Ty); |
| 887 | LoadInst *NewLd1 = CreateLoadIns(Builder, &VecLd1[0], Ty); |
Sam Parker | a023c7a | 2018-09-12 09:17:44 +0000 | [diff] [blame] | 888 | Value* Args[] = { NewLd0, NewLd1, Acc }; |
| 889 | Function *SMLAD = nullptr; |
| 890 | if (Exchange) |
| 891 | SMLAD = Acc->getType()->isIntegerTy(32) ? |
| 892 | Intrinsic::getDeclaration(M, Intrinsic::arm_smladx) : |
| 893 | Intrinsic::getDeclaration(M, Intrinsic::arm_smlaldx); |
| 894 | else |
| 895 | SMLAD = Acc->getType()->isIntegerTy(32) ? |
| 896 | Intrinsic::getDeclaration(M, Intrinsic::arm_smlad) : |
| 897 | Intrinsic::getDeclaration(M, Intrinsic::arm_smlald); |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 898 | CallInst *Call = Builder.CreateCall(SMLAD, Args); |
Sjoerd Meijer | b3e06fa | 2018-07-06 14:47:09 +0000 | [diff] [blame] | 899 | NumSMLAD++; |
Sjoerd Meijer | c89ca55 | 2018-06-28 12:55:29 +0000 | [diff] [blame] | 900 | return Call; |
| 901 | } |
| 902 | |
| 903 | Pass *llvm::createARMParallelDSPPass() { |
| 904 | return new ARMParallelDSP(); |
| 905 | } |
| 906 | |
| 907 | char ARMParallelDSP::ID = 0; |
| 908 | |
Sjoerd Meijer | b3e06fa | 2018-07-06 14:47:09 +0000 | [diff] [blame] | 909 | INITIALIZE_PASS_BEGIN(ARMParallelDSP, "arm-parallel-dsp", |
Simon Pilgrim | c09b5e3 | 2018-06-28 18:37:16 +0000 | [diff] [blame] | 910 | "Transform loops to use DSP intrinsics", false, false) |
Sjoerd Meijer | b3e06fa | 2018-07-06 14:47:09 +0000 | [diff] [blame] | 911 | INITIALIZE_PASS_END(ARMParallelDSP, "arm-parallel-dsp", |
Simon Pilgrim | c09b5e3 | 2018-06-28 18:37:16 +0000 | [diff] [blame] | 912 | "Transform loops to use DSP intrinsics", false, false) |