blob: f240a59dfe94ae1ad3894ed0d91cb294546d4ddd [file] [log] [blame]
Eugene Zelenko4d060b72017-07-29 00:56:56 +00001//===- HexagonLoopIdiomRecognition.cpp ------------------------------------===//
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +00002//
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#define DEBUG_TYPE "hexagon-lir"
11
Eugene Zelenko4d060b72017-07-29 00:56:56 +000012#include "llvm/ADT/APInt.h"
13#include "llvm/ADT/DenseMap.h"
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +000014#include "llvm/ADT/SetVector.h"
Eugene Zelenko4d060b72017-07-29 00:56:56 +000015#include "llvm/ADT/SmallPtrSet.h"
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +000016#include "llvm/ADT/SmallSet.h"
Eugene Zelenko4d060b72017-07-29 00:56:56 +000017#include "llvm/ADT/SmallVector.h"
18#include "llvm/ADT/StringRef.h"
19#include "llvm/ADT/Triple.h"
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +000020#include "llvm/Analysis/AliasAnalysis.h"
21#include "llvm/Analysis/InstructionSimplify.h"
Eugene Zelenko4d060b72017-07-29 00:56:56 +000022#include "llvm/Analysis/LoopInfo.h"
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +000023#include "llvm/Analysis/LoopPass.h"
Eugene Zelenko4d060b72017-07-29 00:56:56 +000024#include "llvm/Analysis/MemoryLocation.h"
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +000025#include "llvm/Analysis/ScalarEvolution.h"
26#include "llvm/Analysis/ScalarEvolutionExpander.h"
27#include "llvm/Analysis/ScalarEvolutionExpressions.h"
28#include "llvm/Analysis/TargetLibraryInfo.h"
David Blaikie2be39222018-03-21 22:34:23 +000029#include "llvm/Analysis/Utils/Local.h"
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +000030#include "llvm/Analysis/ValueTracking.h"
Eugene Zelenko4d060b72017-07-29 00:56:56 +000031#include "llvm/IR/Attributes.h"
32#include "llvm/IR/BasicBlock.h"
33#include "llvm/IR/Constant.h"
34#include "llvm/IR/Constants.h"
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +000035#include "llvm/IR/DataLayout.h"
Eugene Zelenko4d060b72017-07-29 00:56:56 +000036#include "llvm/IR/DebugLoc.h"
37#include "llvm/IR/DerivedTypes.h"
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +000038#include "llvm/IR/Dominators.h"
Eugene Zelenko4d060b72017-07-29 00:56:56 +000039#include "llvm/IR/Function.h"
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +000040#include "llvm/IR/IRBuilder.h"
Eugene Zelenko4d060b72017-07-29 00:56:56 +000041#include "llvm/IR/InstrTypes.h"
42#include "llvm/IR/Instruction.h"
43#include "llvm/IR/Instructions.h"
44#include "llvm/IR/IntrinsicInst.h"
45#include "llvm/IR/Intrinsics.h"
46#include "llvm/IR/Module.h"
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +000047#include "llvm/IR/PatternMatch.h"
Eugene Zelenko4d060b72017-07-29 00:56:56 +000048#include "llvm/IR/Type.h"
49#include "llvm/IR/User.h"
50#include "llvm/IR/Value.h"
51#include "llvm/Pass.h"
52#include "llvm/Support/Casting.h"
53#include "llvm/Support/CommandLine.h"
54#include "llvm/Support/Compiler.h"
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +000055#include "llvm/Support/Debug.h"
Eugene Zelenko4d060b72017-07-29 00:56:56 +000056#include "llvm/Support/ErrorHandling.h"
Craig Topperb45eabc2017-04-26 16:39:58 +000057#include "llvm/Support/KnownBits.h"
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +000058#include "llvm/Support/raw_ostream.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000059#include "llvm/Transforms/Scalar.h"
David Blaikiea373d182018-03-28 17:44:36 +000060#include "llvm/Transforms/Utils.h"
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +000061#include <algorithm>
62#include <array>
Eugene Zelenko4d060b72017-07-29 00:56:56 +000063#include <cassert>
64#include <cstdint>
65#include <cstdlib>
66#include <deque>
67#include <functional>
68#include <iterator>
69#include <map>
70#include <set>
71#include <utility>
72#include <vector>
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +000073
74using namespace llvm;
75
76static cl::opt<bool> DisableMemcpyIdiom("disable-memcpy-idiom",
77 cl::Hidden, cl::init(false),
78 cl::desc("Disable generation of memcpy in loop idiom recognition"));
79
80static cl::opt<bool> DisableMemmoveIdiom("disable-memmove-idiom",
81 cl::Hidden, cl::init(false),
82 cl::desc("Disable generation of memmove in loop idiom recognition"));
83
84static cl::opt<unsigned> RuntimeMemSizeThreshold("runtime-mem-idiom-threshold",
85 cl::Hidden, cl::init(0), cl::desc("Threshold (in bytes) for the runtime "
86 "check guarding the memmove."));
87
88static cl::opt<unsigned> CompileTimeMemSizeThreshold(
89 "compile-time-mem-idiom-threshold", cl::Hidden, cl::init(64),
90 cl::desc("Threshold (in bytes) to perform the transformation, if the "
91 "runtime loop count (mem transfer size) is known at compile-time."));
92
93static cl::opt<bool> OnlyNonNestedMemmove("only-nonnested-memmove-idiom",
94 cl::Hidden, cl::init(true),
95 cl::desc("Only enable generating memmove in non-nested loops"));
96
97cl::opt<bool> HexagonVolatileMemcpy("disable-hexagon-volatile-memcpy",
98 cl::Hidden, cl::init(false),
99 cl::desc("Enable Hexagon-specific memcpy for volatile destination."));
100
Krzysztof Parzyszek51fd5402017-06-01 18:00:47 +0000101static cl::opt<unsigned> SimplifyLimit("hlir-simplify-limit", cl::init(10000),
102 cl::Hidden, cl::desc("Maximum number of simplification steps in HLIR"));
103
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +0000104static const char *HexagonVolatileMemcpyName
105 = "hexagon_memcpy_forward_vp4cp4n2";
106
107
108namespace llvm {
Eugene Zelenko4d060b72017-07-29 00:56:56 +0000109
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +0000110 void initializeHexagonLoopIdiomRecognizePass(PassRegistry&);
111 Pass *createHexagonLoopIdiomPass();
Eugene Zelenko4d060b72017-07-29 00:56:56 +0000112
113} // end namespace llvm
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +0000114
115namespace {
Eugene Zelenko4d060b72017-07-29 00:56:56 +0000116
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +0000117 class HexagonLoopIdiomRecognize : public LoopPass {
118 public:
119 static char ID;
Eugene Zelenko4d060b72017-07-29 00:56:56 +0000120
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +0000121 explicit HexagonLoopIdiomRecognize() : LoopPass(ID) {
122 initializeHexagonLoopIdiomRecognizePass(*PassRegistry::getPassRegistry());
123 }
Eugene Zelenko4d060b72017-07-29 00:56:56 +0000124
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +0000125 StringRef getPassName() const override {
126 return "Recognize Hexagon-specific loop idioms";
127 }
128
129 void getAnalysisUsage(AnalysisUsage &AU) const override {
130 AU.addRequired<LoopInfoWrapperPass>();
131 AU.addRequiredID(LoopSimplifyID);
132 AU.addRequiredID(LCSSAID);
133 AU.addRequired<AAResultsWrapperPass>();
134 AU.addPreserved<AAResultsWrapperPass>();
135 AU.addRequired<ScalarEvolutionWrapperPass>();
136 AU.addRequired<DominatorTreeWrapperPass>();
137 AU.addRequired<TargetLibraryInfoWrapperPass>();
138 AU.addPreserved<TargetLibraryInfoWrapperPass>();
139 }
140
141 bool runOnLoop(Loop *L, LPPassManager &LPM) override;
142
143 private:
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +0000144 int getSCEVStride(const SCEVAddRecExpr *StoreEv);
145 bool isLegalStore(Loop *CurLoop, StoreInst *SI);
146 void collectStores(Loop *CurLoop, BasicBlock *BB,
147 SmallVectorImpl<StoreInst*> &Stores);
148 bool processCopyingStore(Loop *CurLoop, StoreInst *SI, const SCEV *BECount);
149 bool coverLoop(Loop *L, SmallVectorImpl<Instruction*> &Insts) const;
150 bool runOnLoopBlock(Loop *CurLoop, BasicBlock *BB, const SCEV *BECount,
151 SmallVectorImpl<BasicBlock*> &ExitBlocks);
152 bool runOnCountableLoop(Loop *L);
153
154 AliasAnalysis *AA;
155 const DataLayout *DL;
156 DominatorTree *DT;
157 LoopInfo *LF;
158 const TargetLibraryInfo *TLI;
159 ScalarEvolution *SE;
160 bool HasMemcpy, HasMemmove;
161 };
Eugene Zelenko4d060b72017-07-29 00:56:56 +0000162
163 struct Simplifier {
Krzysztof Parzyszek385a4e042017-11-07 17:05:54 +0000164 struct Rule {
165 using FuncType = std::function<Value* (Instruction*, LLVMContext&)>;
166 Rule(StringRef N, FuncType F) : Name(N), Fn(F) {}
167 StringRef Name; // For debugging.
168 FuncType Fn;
169 };
Eugene Zelenko4d060b72017-07-29 00:56:56 +0000170
Krzysztof Parzyszek385a4e042017-11-07 17:05:54 +0000171 void addRule(StringRef N, const Rule::FuncType &F) {
172 Rules.push_back(Rule(N, F));
173 }
Eugene Zelenko4d060b72017-07-29 00:56:56 +0000174
175 private:
176 struct WorkListType {
177 WorkListType() = default;
178
179 void push_back(Value* V) {
180 // Do not push back duplicates.
181 if (!S.count(V)) { Q.push_back(V); S.insert(V); }
182 }
183
184 Value *pop_front_val() {
185 Value *V = Q.front(); Q.pop_front(); S.erase(V);
186 return V;
187 }
188
189 bool empty() const { return Q.empty(); }
190
191 private:
192 std::deque<Value*> Q;
193 std::set<Value*> S;
194 };
195
196 using ValueSetType = std::set<Value *>;
197
198 std::vector<Rule> Rules;
199
200 public:
201 struct Context {
202 using ValueMapType = DenseMap<Value *, Value *>;
203
204 Value *Root;
205 ValueSetType Used; // The set of all cloned values used by Root.
206 ValueSetType Clones; // The set of all cloned values.
207 LLVMContext &Ctx;
208
209 Context(Instruction *Exp)
210 : Ctx(Exp->getParent()->getParent()->getContext()) {
211 initialize(Exp);
212 }
213
214 ~Context() { cleanup(); }
215
216 void print(raw_ostream &OS, const Value *V) const;
217 Value *materialize(BasicBlock *B, BasicBlock::iterator At);
218
219 private:
220 friend struct Simplifier;
221
222 void initialize(Instruction *Exp);
223 void cleanup();
224
225 template <typename FuncT> void traverse(Value *V, FuncT F);
226 void record(Value *V);
227 void use(Value *V);
228 void unuse(Value *V);
229
230 bool equal(const Instruction *I, const Instruction *J) const;
231 Value *find(Value *Tree, Value *Sub) const;
232 Value *subst(Value *Tree, Value *OldV, Value *NewV);
233 void replace(Value *OldV, Value *NewV);
234 void link(Instruction *I, BasicBlock *B, BasicBlock::iterator At);
235 };
236
237 Value *simplify(Context &C);
238 };
239
240 struct PE {
241 PE(const Simplifier::Context &c, Value *v = nullptr) : C(c), V(v) {}
242
243 const Simplifier::Context &C;
244 const Value *V;
245 };
246
247 raw_ostream &operator<< (raw_ostream &OS, const PE &P) LLVM_ATTRIBUTE_USED;
248 raw_ostream &operator<< (raw_ostream &OS, const PE &P) {
249 P.C.print(OS, P.V ? P.V : P.C.Root);
250 return OS;
251 }
252
253} // end anonymous namespace
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +0000254
255char HexagonLoopIdiomRecognize::ID = 0;
256
257INITIALIZE_PASS_BEGIN(HexagonLoopIdiomRecognize, "hexagon-loop-idiom",
258 "Recognize Hexagon-specific loop idioms", false, false)
259INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
260INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
261INITIALIZE_PASS_DEPENDENCY(LCSSAWrapperPass)
262INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
263INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
264INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
265INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
266INITIALIZE_PASS_END(HexagonLoopIdiomRecognize, "hexagon-loop-idiom",
267 "Recognize Hexagon-specific loop idioms", false, false)
268
Krzysztof Parzyszekd033d1f2017-03-21 17:09:27 +0000269template <typename FuncT>
270void Simplifier::Context::traverse(Value *V, FuncT F) {
271 WorkListType Q;
272 Q.push_back(V);
273
274 while (!Q.empty()) {
Krzysztof Parzyszek10fbac02017-03-23 23:01:22 +0000275 Instruction *U = dyn_cast<Instruction>(Q.pop_front_val());
Krzysztof Parzyszekd033d1f2017-03-21 17:09:27 +0000276 if (!U || U->getParent())
277 continue;
278 if (!F(U))
279 continue;
280 for (Value *Op : U->operands())
281 Q.push_back(Op);
282 }
283}
284
Krzysztof Parzyszekd033d1f2017-03-21 17:09:27 +0000285void Simplifier::Context::print(raw_ostream &OS, const Value *V) const {
286 const auto *U = dyn_cast<const Instruction>(V);
287 if (!U) {
288 OS << V << '(' << *V << ')';
289 return;
290 }
291
292 if (U->getParent()) {
293 OS << U << '(';
294 U->printAsOperand(OS, true);
295 OS << ')';
296 return;
297 }
298
299 unsigned N = U->getNumOperands();
300 if (N != 0)
301 OS << U << '(';
302 OS << U->getOpcodeName();
303 for (const Value *Op : U->operands()) {
304 OS << ' ';
305 print(OS, Op);
306 }
307 if (N != 0)
308 OS << ')';
309}
310
Krzysztof Parzyszekd033d1f2017-03-21 17:09:27 +0000311void Simplifier::Context::initialize(Instruction *Exp) {
312 // Perform a deep clone of the expression, set Root to the root
313 // of the clone, and build a map from the cloned values to the
314 // original ones.
315 ValueMapType M;
316 BasicBlock *Block = Exp->getParent();
317 WorkListType Q;
318 Q.push_back(Exp);
319
320 while (!Q.empty()) {
Krzysztof Parzyszek10fbac02017-03-23 23:01:22 +0000321 Value *V = Q.pop_front_val();
Krzysztof Parzyszekd033d1f2017-03-21 17:09:27 +0000322 if (M.find(V) != M.end())
323 continue;
324 if (Instruction *U = dyn_cast<Instruction>(V)) {
325 if (isa<PHINode>(U) || U->getParent() != Block)
326 continue;
327 for (Value *Op : U->operands())
328 Q.push_back(Op);
329 M.insert({U, U->clone()});
330 }
331 }
332
333 for (std::pair<Value*,Value*> P : M) {
334 Instruction *U = cast<Instruction>(P.second);
335 for (unsigned i = 0, n = U->getNumOperands(); i != n; ++i) {
336 auto F = M.find(U->getOperand(i));
337 if (F != M.end())
338 U->setOperand(i, F->second);
339 }
340 }
341
342 auto R = M.find(Exp);
343 assert(R != M.end());
344 Root = R->second;
345
346 record(Root);
347 use(Root);
348}
349
Krzysztof Parzyszekd033d1f2017-03-21 17:09:27 +0000350void Simplifier::Context::record(Value *V) {
351 auto Record = [this](Instruction *U) -> bool {
352 Clones.insert(U);
353 return true;
354 };
355 traverse(V, Record);
356}
357
Krzysztof Parzyszekd033d1f2017-03-21 17:09:27 +0000358void Simplifier::Context::use(Value *V) {
359 auto Use = [this](Instruction *U) -> bool {
360 Used.insert(U);
361 return true;
362 };
363 traverse(V, Use);
364}
365
Krzysztof Parzyszekd033d1f2017-03-21 17:09:27 +0000366void Simplifier::Context::unuse(Value *V) {
367 if (!isa<Instruction>(V) || cast<Instruction>(V)->getParent() != nullptr)
368 return;
369
370 auto Unuse = [this](Instruction *U) -> bool {
371 if (!U->use_empty())
372 return false;
373 Used.erase(U);
374 return true;
375 };
376 traverse(V, Unuse);
377}
378
Krzysztof Parzyszekd033d1f2017-03-21 17:09:27 +0000379Value *Simplifier::Context::subst(Value *Tree, Value *OldV, Value *NewV) {
380 if (Tree == OldV)
381 return NewV;
382 if (OldV == NewV)
383 return Tree;
384
385 WorkListType Q;
386 Q.push_back(Tree);
387 while (!Q.empty()) {
Krzysztof Parzyszek10fbac02017-03-23 23:01:22 +0000388 Instruction *U = dyn_cast<Instruction>(Q.pop_front_val());
Krzysztof Parzyszekd033d1f2017-03-21 17:09:27 +0000389 // If U is not an instruction, or it's not a clone, skip it.
390 if (!U || U->getParent())
391 continue;
392 for (unsigned i = 0, n = U->getNumOperands(); i != n; ++i) {
393 Value *Op = U->getOperand(i);
394 if (Op == OldV) {
395 U->setOperand(i, NewV);
396 unuse(OldV);
397 } else {
398 Q.push_back(Op);
399 }
400 }
401 }
402 return Tree;
403}
404
Krzysztof Parzyszekd033d1f2017-03-21 17:09:27 +0000405void Simplifier::Context::replace(Value *OldV, Value *NewV) {
406 if (Root == OldV) {
407 Root = NewV;
408 use(Root);
409 return;
410 }
411
412 // NewV may be a complex tree that has just been created by one of the
413 // transformation rules. We need to make sure that it is commoned with
414 // the existing Root to the maximum extent possible.
415 // Identify all subtrees of NewV (including NewV itself) that have
416 // equivalent counterparts in Root, and replace those subtrees with
417 // these counterparts.
418 WorkListType Q;
419 Q.push_back(NewV);
420 while (!Q.empty()) {
Krzysztof Parzyszek10fbac02017-03-23 23:01:22 +0000421 Value *V = Q.pop_front_val();
Krzysztof Parzyszekd033d1f2017-03-21 17:09:27 +0000422 Instruction *U = dyn_cast<Instruction>(V);
423 if (!U || U->getParent())
424 continue;
425 if (Value *DupV = find(Root, V)) {
426 if (DupV != V)
427 NewV = subst(NewV, V, DupV);
428 } else {
429 for (Value *Op : U->operands())
430 Q.push_back(Op);
431 }
432 }
433
434 // Now, simply replace OldV with NewV in Root.
435 Root = subst(Root, OldV, NewV);
436 use(Root);
437}
438
Krzysztof Parzyszekd033d1f2017-03-21 17:09:27 +0000439void Simplifier::Context::cleanup() {
440 for (Value *V : Clones) {
441 Instruction *U = cast<Instruction>(V);
442 if (!U->getParent())
443 U->dropAllReferences();
444 }
445
446 for (Value *V : Clones) {
447 Instruction *U = cast<Instruction>(V);
448 if (!U->getParent())
Reid Kleckner96ab8722017-05-18 17:24:10 +0000449 U->deleteValue();
Krzysztof Parzyszekd033d1f2017-03-21 17:09:27 +0000450 }
451}
452
Krzysztof Parzyszekd033d1f2017-03-21 17:09:27 +0000453bool Simplifier::Context::equal(const Instruction *I,
454 const Instruction *J) const {
455 if (I == J)
456 return true;
457 if (!I->isSameOperationAs(J))
458 return false;
459 if (isa<PHINode>(I))
460 return I->isIdenticalTo(J);
461
462 for (unsigned i = 0, n = I->getNumOperands(); i != n; ++i) {
463 Value *OpI = I->getOperand(i), *OpJ = J->getOperand(i);
464 if (OpI == OpJ)
465 continue;
466 auto *InI = dyn_cast<const Instruction>(OpI);
467 auto *InJ = dyn_cast<const Instruction>(OpJ);
468 if (InI && InJ) {
469 if (!equal(InI, InJ))
470 return false;
471 } else if (InI != InJ || !InI)
472 return false;
473 }
474 return true;
475}
476
Krzysztof Parzyszekd033d1f2017-03-21 17:09:27 +0000477Value *Simplifier::Context::find(Value *Tree, Value *Sub) const {
478 Instruction *SubI = dyn_cast<Instruction>(Sub);
479 WorkListType Q;
480 Q.push_back(Tree);
481
482 while (!Q.empty()) {
Krzysztof Parzyszek10fbac02017-03-23 23:01:22 +0000483 Value *V = Q.pop_front_val();
Krzysztof Parzyszekd033d1f2017-03-21 17:09:27 +0000484 if (V == Sub)
485 return V;
486 Instruction *U = dyn_cast<Instruction>(V);
487 if (!U || U->getParent())
488 continue;
489 if (SubI && equal(SubI, U))
490 return U;
491 assert(!isa<PHINode>(U));
492 for (Value *Op : U->operands())
493 Q.push_back(Op);
494 }
495 return nullptr;
496}
497
Krzysztof Parzyszekd033d1f2017-03-21 17:09:27 +0000498void Simplifier::Context::link(Instruction *I, BasicBlock *B,
499 BasicBlock::iterator At) {
500 if (I->getParent())
501 return;
502
503 for (Value *Op : I->operands()) {
504 if (Instruction *OpI = dyn_cast<Instruction>(Op))
505 link(OpI, B, At);
506 }
507
508 B->getInstList().insert(At, I);
509}
510
Krzysztof Parzyszekd033d1f2017-03-21 17:09:27 +0000511Value *Simplifier::Context::materialize(BasicBlock *B,
512 BasicBlock::iterator At) {
513 if (Instruction *RootI = dyn_cast<Instruction>(Root))
514 link(RootI, B, At);
515 return Root;
516}
517
Krzysztof Parzyszekd033d1f2017-03-21 17:09:27 +0000518Value *Simplifier::simplify(Context &C) {
519 WorkListType Q;
520 Q.push_back(C.Root);
Krzysztof Parzyszek10fbac02017-03-23 23:01:22 +0000521 unsigned Count = 0;
Krzysztof Parzyszek51fd5402017-06-01 18:00:47 +0000522 const unsigned Limit = SimplifyLimit;
Krzysztof Parzyszekd033d1f2017-03-21 17:09:27 +0000523
524 while (!Q.empty()) {
Krzysztof Parzyszek10fbac02017-03-23 23:01:22 +0000525 if (Count++ >= Limit)
526 break;
527 Instruction *U = dyn_cast<Instruction>(Q.pop_front_val());
Krzysztof Parzyszekd033d1f2017-03-21 17:09:27 +0000528 if (!U || U->getParent() || !C.Used.count(U))
529 continue;
530 bool Changed = false;
531 for (Rule &R : Rules) {
Krzysztof Parzyszek385a4e042017-11-07 17:05:54 +0000532 Value *W = R.Fn(U, C.Ctx);
Krzysztof Parzyszekd033d1f2017-03-21 17:09:27 +0000533 if (!W)
534 continue;
535 Changed = true;
536 C.record(W);
537 C.replace(U, W);
538 Q.push_back(C.Root);
539 break;
540 }
541 if (!Changed) {
542 for (Value *Op : U->operands())
543 Q.push_back(Op);
544 }
545 }
Krzysztof Parzyszek51fd5402017-06-01 18:00:47 +0000546 return Count < Limit ? C.Root : nullptr;
Krzysztof Parzyszekd033d1f2017-03-21 17:09:27 +0000547}
548
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +0000549//===----------------------------------------------------------------------===//
550//
551// Implementation of PolynomialMultiplyRecognize
552//
553//===----------------------------------------------------------------------===//
554
555namespace {
Eugene Zelenko4d060b72017-07-29 00:56:56 +0000556
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +0000557 class PolynomialMultiplyRecognize {
558 public:
559 explicit PolynomialMultiplyRecognize(Loop *loop, const DataLayout &dl,
560 const DominatorTree &dt, const TargetLibraryInfo &tli,
561 ScalarEvolution &se)
562 : CurLoop(loop), DL(dl), DT(dt), TLI(tli), SE(se) {}
563
564 bool recognize();
Eugene Zelenko4d060b72017-07-29 00:56:56 +0000565
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +0000566 private:
Eugene Zelenko4d060b72017-07-29 00:56:56 +0000567 using ValueSeq = SetVector<Value *>;
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +0000568
Krzysztof Parzyszekd033d1f2017-03-21 17:09:27 +0000569 IntegerType *getPmpyType() const {
570 LLVMContext &Ctx = CurLoop->getHeader()->getParent()->getContext();
571 return IntegerType::get(Ctx, 32);
572 }
Eugene Zelenko4d060b72017-07-29 00:56:56 +0000573
Krzysztof Parzyszekd033d1f2017-03-21 17:09:27 +0000574 bool isPromotableTo(Value *V, IntegerType *Ty);
575 void promoteTo(Instruction *In, IntegerType *DestTy, BasicBlock *LoopB);
576 bool promoteTypes(BasicBlock *LoopB, BasicBlock *ExitB);
577
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +0000578 Value *getCountIV(BasicBlock *BB);
579 bool findCycle(Value *Out, Value *In, ValueSeq &Cycle);
580 void classifyCycle(Instruction *DivI, ValueSeq &Cycle, ValueSeq &Early,
581 ValueSeq &Late);
582 bool classifyInst(Instruction *UseI, ValueSeq &Early, ValueSeq &Late);
583 bool commutesWithShift(Instruction *I);
584 bool highBitsAreZero(Value *V, unsigned IterCount);
585 bool keepsHighBitsZero(Value *V, unsigned IterCount);
586 bool isOperandShifted(Instruction *I, Value *Op);
587 bool convertShiftsToLeft(BasicBlock *LoopB, BasicBlock *ExitB,
588 unsigned IterCount);
589 void cleanupLoopBody(BasicBlock *LoopB);
590
591 struct ParsedValues {
Eugene Zelenko4d060b72017-07-29 00:56:56 +0000592 ParsedValues() = default;
593
594 Value *M = nullptr;
595 Value *P = nullptr;
596 Value *Q = nullptr;
597 Value *R = nullptr;
598 Value *X = nullptr;
599 Instruction *Res = nullptr;
600 unsigned IterCount = 0;
601 bool Left = false;
602 bool Inv = false;
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +0000603 };
604
605 bool matchLeftShift(SelectInst *SelI, Value *CIV, ParsedValues &PV);
606 bool matchRightShift(SelectInst *SelI, ParsedValues &PV);
607 bool scanSelect(SelectInst *SI, BasicBlock *LoopB, BasicBlock *PrehB,
608 Value *CIV, ParsedValues &PV, bool PreScan);
609 unsigned getInverseMxN(unsigned QP);
610 Value *generate(BasicBlock::iterator At, ParsedValues &PV);
611
Krzysztof Parzyszek5d9844f2018-01-30 22:03:59 +0000612 void setupPreSimplifier(Simplifier &S);
613 void setupPostSimplifier(Simplifier &S);
Krzysztof Parzyszekd033d1f2017-03-21 17:09:27 +0000614
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +0000615 Loop *CurLoop;
616 const DataLayout &DL;
617 const DominatorTree &DT;
618 const TargetLibraryInfo &TLI;
619 ScalarEvolution &SE;
620 };
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +0000621
Eugene Zelenko4d060b72017-07-29 00:56:56 +0000622} // end anonymous namespace
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +0000623
624Value *PolynomialMultiplyRecognize::getCountIV(BasicBlock *BB) {
625 pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
626 if (std::distance(PI, PE) != 2)
627 return nullptr;
628 BasicBlock *PB = (*PI == BB) ? *std::next(PI) : *PI;
629
630 for (auto I = BB->begin(), E = BB->end(); I != E && isa<PHINode>(I); ++I) {
631 auto *PN = cast<PHINode>(I);
632 Value *InitV = PN->getIncomingValueForBlock(PB);
633 if (!isa<ConstantInt>(InitV) || !cast<ConstantInt>(InitV)->isZero())
634 continue;
635 Value *IterV = PN->getIncomingValueForBlock(BB);
636 if (!isa<BinaryOperator>(IterV))
637 continue;
638 auto *BO = dyn_cast<BinaryOperator>(IterV);
639 if (BO->getOpcode() != Instruction::Add)
640 continue;
641 Value *IncV = nullptr;
642 if (BO->getOperand(0) == PN)
643 IncV = BO->getOperand(1);
644 else if (BO->getOperand(1) == PN)
645 IncV = BO->getOperand(0);
646 if (IncV == nullptr)
647 continue;
648
649 if (auto *T = dyn_cast<ConstantInt>(IncV))
650 if (T->getZExtValue() == 1)
651 return PN;
652 }
653 return nullptr;
654}
655
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +0000656static void replaceAllUsesOfWithIn(Value *I, Value *J, BasicBlock *BB) {
657 for (auto UI = I->user_begin(), UE = I->user_end(); UI != UE;) {
658 Use &TheUse = UI.getUse();
659 ++UI;
660 if (auto *II = dyn_cast<Instruction>(TheUse.getUser()))
661 if (BB == II->getParent())
662 II->replaceUsesOfWith(I, J);
663 }
664}
665
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +0000666bool PolynomialMultiplyRecognize::matchLeftShift(SelectInst *SelI,
667 Value *CIV, ParsedValues &PV) {
668 // Match the following:
669 // select (X & (1 << i)) != 0 ? R ^ (Q << i) : R
670 // select (X & (1 << i)) == 0 ? R : R ^ (Q << i)
671 // The condition may also check for equality with the masked value, i.e
672 // select (X & (1 << i)) == (1 << i) ? R ^ (Q << i) : R
673 // select (X & (1 << i)) != (1 << i) ? R : R ^ (Q << i);
674
675 Value *CondV = SelI->getCondition();
676 Value *TrueV = SelI->getTrueValue();
677 Value *FalseV = SelI->getFalseValue();
678
679 using namespace PatternMatch;
680
681 CmpInst::Predicate P;
682 Value *A = nullptr, *B = nullptr, *C = nullptr;
683
684 if (!match(CondV, m_ICmp(P, m_And(m_Value(A), m_Value(B)), m_Value(C))) &&
685 !match(CondV, m_ICmp(P, m_Value(C), m_And(m_Value(A), m_Value(B)))))
686 return false;
687 if (P != CmpInst::ICMP_EQ && P != CmpInst::ICMP_NE)
688 return false;
689 // Matched: select (A & B) == C ? ... : ...
690 // select (A & B) != C ? ... : ...
691
692 Value *X = nullptr, *Sh1 = nullptr;
693 // Check (A & B) for (X & (1 << i)):
694 if (match(A, m_Shl(m_One(), m_Specific(CIV)))) {
695 Sh1 = A;
696 X = B;
697 } else if (match(B, m_Shl(m_One(), m_Specific(CIV)))) {
698 Sh1 = B;
699 X = A;
700 } else {
701 // TODO: Could also check for an induction variable containing single
702 // bit shifted left by 1 in each iteration.
703 return false;
704 }
705
706 bool TrueIfZero;
707
708 // Check C against the possible values for comparison: 0 and (1 << i):
709 if (match(C, m_Zero()))
710 TrueIfZero = (P == CmpInst::ICMP_EQ);
711 else if (C == Sh1)
712 TrueIfZero = (P == CmpInst::ICMP_NE);
713 else
714 return false;
715
716 // So far, matched:
717 // select (X & (1 << i)) ? ... : ...
718 // including variations of the check against zero/non-zero value.
719
720 Value *ShouldSameV = nullptr, *ShouldXoredV = nullptr;
721 if (TrueIfZero) {
722 ShouldSameV = TrueV;
723 ShouldXoredV = FalseV;
724 } else {
725 ShouldSameV = FalseV;
726 ShouldXoredV = TrueV;
727 }
728
729 Value *Q = nullptr, *R = nullptr, *Y = nullptr, *Z = nullptr;
730 Value *T = nullptr;
731 if (match(ShouldXoredV, m_Xor(m_Value(Y), m_Value(Z)))) {
732 // Matched: select +++ ? ... : Y ^ Z
733 // select +++ ? Y ^ Z : ...
734 // where +++ denotes previously checked matches.
735 if (ShouldSameV == Y)
736 T = Z;
737 else if (ShouldSameV == Z)
738 T = Y;
739 else
740 return false;
741 R = ShouldSameV;
742 // Matched: select +++ ? R : R ^ T
743 // select +++ ? R ^ T : R
744 // depending on TrueIfZero.
745
746 } else if (match(ShouldSameV, m_Zero())) {
747 // Matched: select +++ ? 0 : ...
748 // select +++ ? ... : 0
749 if (!SelI->hasOneUse())
750 return false;
751 T = ShouldXoredV;
752 // Matched: select +++ ? 0 : T
753 // select +++ ? T : 0
754
755 Value *U = *SelI->user_begin();
756 if (!match(U, m_Xor(m_Specific(SelI), m_Value(R))) &&
757 !match(U, m_Xor(m_Value(R), m_Specific(SelI))))
758 return false;
759 // Matched: xor (select +++ ? 0 : T), R
760 // xor (select +++ ? T : 0), R
761 } else
762 return false;
763
764 // The xor input value T is isolated into its own match so that it could
765 // be checked against an induction variable containing a shifted bit
766 // (todo).
767 // For now, check against (Q << i).
768 if (!match(T, m_Shl(m_Value(Q), m_Specific(CIV))) &&
769 !match(T, m_Shl(m_ZExt(m_Value(Q)), m_ZExt(m_Specific(CIV)))))
770 return false;
771 // Matched: select +++ ? R : R ^ (Q << i)
772 // select +++ ? R ^ (Q << i) : R
773
774 PV.X = X;
775 PV.Q = Q;
776 PV.R = R;
777 PV.Left = true;
778 return true;
779}
780
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +0000781bool PolynomialMultiplyRecognize::matchRightShift(SelectInst *SelI,
782 ParsedValues &PV) {
783 // Match the following:
784 // select (X & 1) != 0 ? (R >> 1) ^ Q : (R >> 1)
785 // select (X & 1) == 0 ? (R >> 1) : (R >> 1) ^ Q
786 // The condition may also check for equality with the masked value, i.e
787 // select (X & 1) == 1 ? (R >> 1) ^ Q : (R >> 1)
788 // select (X & 1) != 1 ? (R >> 1) : (R >> 1) ^ Q
789
790 Value *CondV = SelI->getCondition();
791 Value *TrueV = SelI->getTrueValue();
792 Value *FalseV = SelI->getFalseValue();
793
794 using namespace PatternMatch;
795
796 Value *C = nullptr;
797 CmpInst::Predicate P;
798 bool TrueIfZero;
799
800 if (match(CondV, m_ICmp(P, m_Value(C), m_Zero())) ||
801 match(CondV, m_ICmp(P, m_Zero(), m_Value(C)))) {
802 if (P != CmpInst::ICMP_EQ && P != CmpInst::ICMP_NE)
803 return false;
804 // Matched: select C == 0 ? ... : ...
805 // select C != 0 ? ... : ...
806 TrueIfZero = (P == CmpInst::ICMP_EQ);
807 } else if (match(CondV, m_ICmp(P, m_Value(C), m_One())) ||
808 match(CondV, m_ICmp(P, m_One(), m_Value(C)))) {
809 if (P != CmpInst::ICMP_EQ && P != CmpInst::ICMP_NE)
810 return false;
811 // Matched: select C == 1 ? ... : ...
812 // select C != 1 ? ... : ...
813 TrueIfZero = (P == CmpInst::ICMP_NE);
814 } else
815 return false;
816
817 Value *X = nullptr;
818 if (!match(C, m_And(m_Value(X), m_One())) &&
819 !match(C, m_And(m_One(), m_Value(X))))
820 return false;
821 // Matched: select (X & 1) == +++ ? ... : ...
822 // select (X & 1) != +++ ? ... : ...
823
824 Value *R = nullptr, *Q = nullptr;
825 if (TrueIfZero) {
826 // The select's condition is true if the tested bit is 0.
827 // TrueV must be the shift, FalseV must be the xor.
828 if (!match(TrueV, m_LShr(m_Value(R), m_One())))
829 return false;
830 // Matched: select +++ ? (R >> 1) : ...
831 if (!match(FalseV, m_Xor(m_Specific(TrueV), m_Value(Q))) &&
832 !match(FalseV, m_Xor(m_Value(Q), m_Specific(TrueV))))
833 return false;
834 // Matched: select +++ ? (R >> 1) : (R >> 1) ^ Q
835 // with commuting ^.
836 } else {
837 // The select's condition is true if the tested bit is 1.
838 // TrueV must be the xor, FalseV must be the shift.
839 if (!match(FalseV, m_LShr(m_Value(R), m_One())))
840 return false;
841 // Matched: select +++ ? ... : (R >> 1)
842 if (!match(TrueV, m_Xor(m_Specific(FalseV), m_Value(Q))) &&
843 !match(TrueV, m_Xor(m_Value(Q), m_Specific(FalseV))))
844 return false;
845 // Matched: select +++ ? (R >> 1) ^ Q : (R >> 1)
846 // with commuting ^.
847 }
848
849 PV.X = X;
850 PV.Q = Q;
851 PV.R = R;
852 PV.Left = false;
853 return true;
854}
855
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +0000856bool PolynomialMultiplyRecognize::scanSelect(SelectInst *SelI,
857 BasicBlock *LoopB, BasicBlock *PrehB, Value *CIV, ParsedValues &PV,
858 bool PreScan) {
859 using namespace PatternMatch;
Eugene Zelenko4d060b72017-07-29 00:56:56 +0000860
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +0000861 // The basic pattern for R = P.Q is:
862 // for i = 0..31
863 // R = phi (0, R')
864 // if (P & (1 << i)) ; test-bit(P, i)
865 // R' = R ^ (Q << i)
866 //
867 // Similarly, the basic pattern for R = (P/Q).Q - P
868 // for i = 0..31
869 // R = phi(P, R')
870 // if (R & (1 << i))
871 // R' = R ^ (Q << i)
872
873 // There exist idioms, where instead of Q being shifted left, P is shifted
874 // right. This produces a result that is shifted right by 32 bits (the
875 // non-shifted result is 64-bit).
876 //
877 // For R = P.Q, this would be:
878 // for i = 0..31
879 // R = phi (0, R')
880 // if ((P >> i) & 1)
881 // R' = (R >> 1) ^ Q ; R is cycled through the loop, so it must
882 // else ; be shifted by 1, not i.
883 // R' = R >> 1
884 //
885 // And for the inverse:
886 // for i = 0..31
887 // R = phi (P, R')
888 // if (R & 1)
889 // R' = (R >> 1) ^ Q
890 // else
891 // R' = R >> 1
892
893 // The left-shifting idioms share the same pattern:
894 // select (X & (1 << i)) ? R ^ (Q << i) : R
895 // Similarly for right-shifting idioms:
896 // select (X & 1) ? (R >> 1) ^ Q
897
898 if (matchLeftShift(SelI, CIV, PV)) {
899 // If this is a pre-scan, getting this far is sufficient.
900 if (PreScan)
901 return true;
902
903 // Need to make sure that the SelI goes back into R.
904 auto *RPhi = dyn_cast<PHINode>(PV.R);
905 if (!RPhi)
906 return false;
907 if (SelI != RPhi->getIncomingValueForBlock(LoopB))
908 return false;
909 PV.Res = SelI;
910
911 // If X is loop invariant, it must be the input polynomial, and the
912 // idiom is the basic polynomial multiply.
913 if (CurLoop->isLoopInvariant(PV.X)) {
914 PV.P = PV.X;
915 PV.Inv = false;
916 } else {
917 // X is not loop invariant. If X == R, this is the inverse pmpy.
918 // Otherwise, check for an xor with an invariant value. If the
919 // variable argument to the xor is R, then this is still a valid
920 // inverse pmpy.
921 PV.Inv = true;
922 if (PV.X != PV.R) {
923 Value *Var = nullptr, *Inv = nullptr, *X1 = nullptr, *X2 = nullptr;
924 if (!match(PV.X, m_Xor(m_Value(X1), m_Value(X2))))
925 return false;
926 auto *I1 = dyn_cast<Instruction>(X1);
927 auto *I2 = dyn_cast<Instruction>(X2);
928 if (!I1 || I1->getParent() != LoopB) {
929 Var = X2;
930 Inv = X1;
931 } else if (!I2 || I2->getParent() != LoopB) {
932 Var = X1;
933 Inv = X2;
934 } else
935 return false;
936 if (Var != PV.R)
937 return false;
938 PV.M = Inv;
939 }
940 // The input polynomial P still needs to be determined. It will be
941 // the entry value of R.
942 Value *EntryP = RPhi->getIncomingValueForBlock(PrehB);
943 PV.P = EntryP;
944 }
945
946 return true;
947 }
948
949 if (matchRightShift(SelI, PV)) {
950 // If this is an inverse pattern, the Q polynomial must be known at
951 // compile time.
952 if (PV.Inv && !isa<ConstantInt>(PV.Q))
953 return false;
954 if (PreScan)
955 return true;
956 // There is no exact matching of right-shift pmpy.
957 return false;
958 }
959
960 return false;
961}
962
Krzysztof Parzyszekd033d1f2017-03-21 17:09:27 +0000963bool PolynomialMultiplyRecognize::isPromotableTo(Value *Val,
964 IntegerType *DestTy) {
965 IntegerType *T = dyn_cast<IntegerType>(Val->getType());
966 if (!T || T->getBitWidth() > DestTy->getBitWidth())
967 return false;
968 if (T->getBitWidth() == DestTy->getBitWidth())
969 return true;
970 // Non-instructions are promotable. The reason why an instruction may not
971 // be promotable is that it may produce a different result if its operands
972 // and the result are promoted, for example, it may produce more non-zero
973 // bits. While it would still be possible to represent the proper result
974 // in a wider type, it may require adding additional instructions (which
975 // we don't want to do).
976 Instruction *In = dyn_cast<Instruction>(Val);
977 if (!In)
978 return true;
979 // The bitwidth of the source type is smaller than the destination.
980 // Check if the individual operation can be promoted.
981 switch (In->getOpcode()) {
982 case Instruction::PHI:
983 case Instruction::ZExt:
984 case Instruction::And:
985 case Instruction::Or:
986 case Instruction::Xor:
987 case Instruction::LShr: // Shift right is ok.
988 case Instruction::Select:
Krzysztof Parzyszek5d9844f2018-01-30 22:03:59 +0000989 case Instruction::Trunc:
Krzysztof Parzyszekd033d1f2017-03-21 17:09:27 +0000990 return true;
991 case Instruction::ICmp:
992 if (CmpInst *CI = cast<CmpInst>(In))
993 return CI->isEquality() || CI->isUnsigned();
994 llvm_unreachable("Cast failed unexpectedly");
995 case Instruction::Add:
996 return In->hasNoSignedWrap() && In->hasNoUnsignedWrap();
997 }
998 return false;
999}
1000
Krzysztof Parzyszekd033d1f2017-03-21 17:09:27 +00001001void PolynomialMultiplyRecognize::promoteTo(Instruction *In,
1002 IntegerType *DestTy, BasicBlock *LoopB) {
Krzysztof Parzyszek5d9844f2018-01-30 22:03:59 +00001003 Type *OrigTy = In->getType();
1004
Krzysztof Parzyszekd033d1f2017-03-21 17:09:27 +00001005 // Leave boolean values alone.
1006 if (!In->getType()->isIntegerTy(1))
1007 In->mutateType(DestTy);
1008 unsigned DestBW = DestTy->getBitWidth();
1009
1010 // Handle PHIs.
1011 if (PHINode *P = dyn_cast<PHINode>(In)) {
1012 unsigned N = P->getNumIncomingValues();
1013 for (unsigned i = 0; i != N; ++i) {
1014 BasicBlock *InB = P->getIncomingBlock(i);
1015 if (InB == LoopB)
1016 continue;
1017 Value *InV = P->getIncomingValue(i);
1018 IntegerType *Ty = cast<IntegerType>(InV->getType());
1019 // Do not promote values in PHI nodes of type i1.
1020 if (Ty != P->getType()) {
1021 // If the value type does not match the PHI type, the PHI type
1022 // must have been promoted.
1023 assert(Ty->getBitWidth() < DestBW);
1024 InV = IRBuilder<>(InB->getTerminator()).CreateZExt(InV, DestTy);
1025 P->setIncomingValue(i, InV);
1026 }
1027 }
1028 } else if (ZExtInst *Z = dyn_cast<ZExtInst>(In)) {
1029 Value *Op = Z->getOperand(0);
1030 if (Op->getType() == Z->getType())
1031 Z->replaceAllUsesWith(Op);
1032 Z->eraseFromParent();
1033 return;
1034 }
Krzysztof Parzyszek5d9844f2018-01-30 22:03:59 +00001035 if (TruncInst *T = dyn_cast<TruncInst>(In)) {
1036 IntegerType *TruncTy = cast<IntegerType>(OrigTy);
1037 Value *Mask = ConstantInt::get(DestTy, (1u << TruncTy->getBitWidth()) - 1);
1038 Value *And = IRBuilder<>(In).CreateAnd(T->getOperand(0), Mask);
1039 T->replaceAllUsesWith(And);
1040 T->eraseFromParent();
1041 return;
1042 }
Krzysztof Parzyszekd033d1f2017-03-21 17:09:27 +00001043
1044 // Promote immediates.
1045 for (unsigned i = 0, n = In->getNumOperands(); i != n; ++i) {
1046 if (ConstantInt *CI = dyn_cast<ConstantInt>(In->getOperand(i)))
1047 if (CI->getType()->getBitWidth() < DestBW)
1048 In->setOperand(i, ConstantInt::get(DestTy, CI->getZExtValue()));
1049 }
1050}
1051
Krzysztof Parzyszekd033d1f2017-03-21 17:09:27 +00001052bool PolynomialMultiplyRecognize::promoteTypes(BasicBlock *LoopB,
1053 BasicBlock *ExitB) {
1054 assert(LoopB);
1055 // Skip loops where the exit block has more than one predecessor. The values
1056 // coming from the loop block will be promoted to another type, and so the
1057 // values coming into the exit block from other predecessors would also have
1058 // to be promoted.
1059 if (!ExitB || (ExitB->getSinglePredecessor() != LoopB))
1060 return false;
1061 IntegerType *DestTy = getPmpyType();
1062 // Check if the exit values have types that are no wider than the type
1063 // that we want to promote to.
1064 unsigned DestBW = DestTy->getBitWidth();
Benjamin Kramerc7fc81e2017-12-30 15:27:33 +00001065 for (PHINode &P : ExitB->phis()) {
1066 if (P.getNumIncomingValues() != 1)
Krzysztof Parzyszekd033d1f2017-03-21 17:09:27 +00001067 return false;
Benjamin Kramerc7fc81e2017-12-30 15:27:33 +00001068 assert(P.getIncomingBlock(0) == LoopB);
1069 IntegerType *T = dyn_cast<IntegerType>(P.getType());
Krzysztof Parzyszekd033d1f2017-03-21 17:09:27 +00001070 if (!T || T->getBitWidth() > DestBW)
1071 return false;
1072 }
1073
1074 // Check all instructions in the loop.
1075 for (Instruction &In : *LoopB)
1076 if (!In.isTerminator() && !isPromotableTo(&In, DestTy))
1077 return false;
1078
1079 // Perform the promotion.
1080 std::vector<Instruction*> LoopIns;
1081 std::transform(LoopB->begin(), LoopB->end(), std::back_inserter(LoopIns),
1082 [](Instruction &In) { return &In; });
1083 for (Instruction *In : LoopIns)
1084 promoteTo(In, DestTy, LoopB);
1085
1086 // Fix up the PHI nodes in the exit block.
1087 Instruction *EndI = ExitB->getFirstNonPHI();
1088 BasicBlock::iterator End = EndI ? EndI->getIterator() : ExitB->end();
1089 for (auto I = ExitB->begin(); I != End; ++I) {
1090 PHINode *P = dyn_cast<PHINode>(I);
1091 if (!P)
1092 break;
1093 Type *Ty0 = P->getIncomingValue(0)->getType();
1094 Type *PTy = P->getType();
1095 if (PTy != Ty0) {
1096 assert(Ty0 == DestTy);
1097 // In order to create the trunc, P must have the promoted type.
1098 P->mutateType(Ty0);
1099 Value *T = IRBuilder<>(ExitB, End).CreateTrunc(P, PTy);
1100 // In order for the RAUW to work, the types of P and T must match.
1101 P->mutateType(PTy);
1102 P->replaceAllUsesWith(T);
1103 // Final update of the P's type.
1104 P->mutateType(Ty0);
1105 cast<Instruction>(T)->setOperand(0, P);
1106 }
1107 }
1108
1109 return true;
1110}
1111
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +00001112bool PolynomialMultiplyRecognize::findCycle(Value *Out, Value *In,
1113 ValueSeq &Cycle) {
1114 // Out = ..., In, ...
1115 if (Out == In)
1116 return true;
1117
1118 auto *BB = cast<Instruction>(Out)->getParent();
1119 bool HadPhi = false;
1120
1121 for (auto U : Out->users()) {
1122 auto *I = dyn_cast<Instruction>(&*U);
1123 if (I == nullptr || I->getParent() != BB)
1124 continue;
1125 // Make sure that there are no multi-iteration cycles, e.g.
1126 // p1 = phi(p2)
1127 // p2 = phi(p1)
1128 // The cycle p1->p2->p1 would span two loop iterations.
1129 // Check that there is only one phi in the cycle.
1130 bool IsPhi = isa<PHINode>(I);
1131 if (IsPhi && HadPhi)
1132 return false;
1133 HadPhi |= IsPhi;
1134 if (Cycle.count(I))
1135 return false;
1136 Cycle.insert(I);
1137 if (findCycle(I, In, Cycle))
1138 break;
1139 Cycle.remove(I);
1140 }
1141 return !Cycle.empty();
1142}
1143
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +00001144void PolynomialMultiplyRecognize::classifyCycle(Instruction *DivI,
1145 ValueSeq &Cycle, ValueSeq &Early, ValueSeq &Late) {
1146 // All the values in the cycle that are between the phi node and the
1147 // divider instruction will be classified as "early", all other values
1148 // will be "late".
1149
1150 bool IsE = true;
1151 unsigned I, N = Cycle.size();
1152 for (I = 0; I < N; ++I) {
1153 Value *V = Cycle[I];
1154 if (DivI == V)
1155 IsE = false;
1156 else if (!isa<PHINode>(V))
1157 continue;
1158 // Stop if found either.
1159 break;
1160 }
1161 // "I" is the index of either DivI or the phi node, whichever was first.
1162 // "E" is "false" or "true" respectively.
1163 ValueSeq &First = !IsE ? Early : Late;
1164 for (unsigned J = 0; J < I; ++J)
1165 First.insert(Cycle[J]);
1166
1167 ValueSeq &Second = IsE ? Early : Late;
1168 Second.insert(Cycle[I]);
1169 for (++I; I < N; ++I) {
1170 Value *V = Cycle[I];
1171 if (DivI == V || isa<PHINode>(V))
1172 break;
1173 Second.insert(V);
1174 }
1175
1176 for (; I < N; ++I)
1177 First.insert(Cycle[I]);
1178}
1179
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +00001180bool PolynomialMultiplyRecognize::classifyInst(Instruction *UseI,
1181 ValueSeq &Early, ValueSeq &Late) {
1182 // Select is an exception, since the condition value does not have to be
1183 // classified in the same way as the true/false values. The true/false
1184 // values do have to be both early or both late.
1185 if (UseI->getOpcode() == Instruction::Select) {
1186 Value *TV = UseI->getOperand(1), *FV = UseI->getOperand(2);
1187 if (Early.count(TV) || Early.count(FV)) {
1188 if (Late.count(TV) || Late.count(FV))
1189 return false;
1190 Early.insert(UseI);
1191 } else if (Late.count(TV) || Late.count(FV)) {
1192 if (Early.count(TV) || Early.count(FV))
1193 return false;
1194 Late.insert(UseI);
1195 }
1196 return true;
1197 }
1198
1199 // Not sure what would be the example of this, but the code below relies
1200 // on having at least one operand.
1201 if (UseI->getNumOperands() == 0)
1202 return true;
1203
1204 bool AE = true, AL = true;
1205 for (auto &I : UseI->operands()) {
1206 if (Early.count(&*I))
1207 AL = false;
1208 else if (Late.count(&*I))
1209 AE = false;
1210 }
1211 // If the operands appear "all early" and "all late" at the same time,
1212 // then it means that none of them are actually classified as either.
1213 // This is harmless.
1214 if (AE && AL)
1215 return true;
1216 // Conversely, if they are neither "all early" nor "all late", then
1217 // we have a mixture of early and late operands that is not a known
1218 // exception.
1219 if (!AE && !AL)
1220 return false;
1221
1222 // Check that we have covered the two special cases.
1223 assert(AE != AL);
1224
1225 if (AE)
1226 Early.insert(UseI);
1227 else
1228 Late.insert(UseI);
1229 return true;
1230}
1231
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +00001232bool PolynomialMultiplyRecognize::commutesWithShift(Instruction *I) {
1233 switch (I->getOpcode()) {
1234 case Instruction::And:
1235 case Instruction::Or:
1236 case Instruction::Xor:
1237 case Instruction::LShr:
1238 case Instruction::Shl:
1239 case Instruction::Select:
1240 case Instruction::ICmp:
1241 case Instruction::PHI:
1242 break;
1243 default:
1244 return false;
1245 }
1246 return true;
1247}
1248
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +00001249bool PolynomialMultiplyRecognize::highBitsAreZero(Value *V,
1250 unsigned IterCount) {
1251 auto *T = dyn_cast<IntegerType>(V->getType());
1252 if (!T)
1253 return false;
1254
Craig Topperb45eabc2017-04-26 16:39:58 +00001255 KnownBits Known(T->getBitWidth());
1256 computeKnownBits(V, Known, DL);
Craig Topper8df66c62017-05-12 17:20:30 +00001257 return Known.countMinLeadingZeros() >= IterCount;
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +00001258}
1259
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +00001260bool PolynomialMultiplyRecognize::keepsHighBitsZero(Value *V,
1261 unsigned IterCount) {
1262 // Assume that all inputs to the value have the high bits zero.
1263 // Check if the value itself preserves the zeros in the high bits.
1264 if (auto *C = dyn_cast<ConstantInt>(V))
1265 return C->getValue().countLeadingZeros() >= IterCount;
1266
1267 if (auto *I = dyn_cast<Instruction>(V)) {
1268 switch (I->getOpcode()) {
1269 case Instruction::And:
1270 case Instruction::Or:
1271 case Instruction::Xor:
1272 case Instruction::LShr:
1273 case Instruction::Select:
1274 case Instruction::ICmp:
1275 case Instruction::PHI:
Krzysztof Parzyszekd033d1f2017-03-21 17:09:27 +00001276 case Instruction::ZExt:
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +00001277 return true;
1278 }
1279 }
1280
1281 return false;
1282}
1283
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +00001284bool PolynomialMultiplyRecognize::isOperandShifted(Instruction *I, Value *Op) {
1285 unsigned Opc = I->getOpcode();
1286 if (Opc == Instruction::Shl || Opc == Instruction::LShr)
1287 return Op != I->getOperand(1);
1288 return true;
1289}
1290
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +00001291bool PolynomialMultiplyRecognize::convertShiftsToLeft(BasicBlock *LoopB,
1292 BasicBlock *ExitB, unsigned IterCount) {
1293 Value *CIV = getCountIV(LoopB);
1294 if (CIV == nullptr)
1295 return false;
1296 auto *CIVTy = dyn_cast<IntegerType>(CIV->getType());
1297 if (CIVTy == nullptr)
1298 return false;
1299
1300 ValueSeq RShifts;
1301 ValueSeq Early, Late, Cycled;
1302
1303 // Find all value cycles that contain logical right shifts by 1.
1304 for (Instruction &I : *LoopB) {
1305 using namespace PatternMatch;
Eugene Zelenko4d060b72017-07-29 00:56:56 +00001306
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +00001307 Value *V = nullptr;
1308 if (!match(&I, m_LShr(m_Value(V), m_One())))
1309 continue;
1310 ValueSeq C;
1311 if (!findCycle(&I, V, C))
1312 continue;
1313
1314 // Found a cycle.
1315 C.insert(&I);
1316 classifyCycle(&I, C, Early, Late);
1317 Cycled.insert(C.begin(), C.end());
1318 RShifts.insert(&I);
1319 }
1320
1321 // Find the set of all values affected by the shift cycles, i.e. all
1322 // cycled values, and (recursively) all their users.
1323 ValueSeq Users(Cycled.begin(), Cycled.end());
1324 for (unsigned i = 0; i < Users.size(); ++i) {
1325 Value *V = Users[i];
1326 if (!isa<IntegerType>(V->getType()))
1327 return false;
1328 auto *R = cast<Instruction>(V);
1329 // If the instruction does not commute with shifts, the loop cannot
1330 // be unshifted.
1331 if (!commutesWithShift(R))
1332 return false;
1333 for (auto I = R->user_begin(), E = R->user_end(); I != E; ++I) {
1334 auto *T = cast<Instruction>(*I);
1335 // Skip users from outside of the loop. They will be handled later.
1336 // Also, skip the right-shifts and phi nodes, since they mix early
1337 // and late values.
1338 if (T->getParent() != LoopB || RShifts.count(T) || isa<PHINode>(T))
1339 continue;
1340
1341 Users.insert(T);
1342 if (!classifyInst(T, Early, Late))
1343 return false;
1344 }
1345 }
1346
Eugene Zelenko4d060b72017-07-29 00:56:56 +00001347 if (Users.empty())
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +00001348 return false;
1349
1350 // Verify that high bits remain zero.
1351 ValueSeq Internal(Users.begin(), Users.end());
1352 ValueSeq Inputs;
1353 for (unsigned i = 0; i < Internal.size(); ++i) {
1354 auto *R = dyn_cast<Instruction>(Internal[i]);
1355 if (!R)
1356 continue;
1357 for (Value *Op : R->operands()) {
1358 auto *T = dyn_cast<Instruction>(Op);
1359 if (T && T->getParent() != LoopB)
1360 Inputs.insert(Op);
1361 else
1362 Internal.insert(Op);
1363 }
1364 }
1365 for (Value *V : Inputs)
1366 if (!highBitsAreZero(V, IterCount))
1367 return false;
1368 for (Value *V : Internal)
1369 if (!keepsHighBitsZero(V, IterCount))
1370 return false;
1371
1372 // Finally, the work can be done. Unshift each user.
1373 IRBuilder<> IRB(LoopB);
1374 std::map<Value*,Value*> ShiftMap;
Eugene Zelenko4d060b72017-07-29 00:56:56 +00001375
1376 using CastMapType = std::map<std::pair<Value *, Type *>, Value *>;
1377
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +00001378 CastMapType CastMap;
1379
1380 auto upcast = [] (CastMapType &CM, IRBuilder<> &IRB, Value *V,
1381 IntegerType *Ty) -> Value* {
1382 auto H = CM.find(std::make_pair(V, Ty));
1383 if (H != CM.end())
1384 return H->second;
1385 Value *CV = IRB.CreateIntCast(V, Ty, false);
1386 CM.insert(std::make_pair(std::make_pair(V, Ty), CV));
1387 return CV;
1388 };
1389
1390 for (auto I = LoopB->begin(), E = LoopB->end(); I != E; ++I) {
Eugene Zelenko4d060b72017-07-29 00:56:56 +00001391 using namespace PatternMatch;
1392
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +00001393 if (isa<PHINode>(I) || !Users.count(&*I))
1394 continue;
Eugene Zelenko4d060b72017-07-29 00:56:56 +00001395
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +00001396 // Match lshr x, 1.
1397 Value *V = nullptr;
1398 if (match(&*I, m_LShr(m_Value(V), m_One()))) {
1399 replaceAllUsesOfWithIn(&*I, V, LoopB);
1400 continue;
1401 }
1402 // For each non-cycled operand, replace it with the corresponding
1403 // value shifted left.
1404 for (auto &J : I->operands()) {
1405 Value *Op = J.get();
1406 if (!isOperandShifted(&*I, Op))
1407 continue;
1408 if (Users.count(Op))
1409 continue;
1410 // Skip shifting zeros.
1411 if (isa<ConstantInt>(Op) && cast<ConstantInt>(Op)->isZero())
1412 continue;
1413 // Check if we have already generated a shift for this value.
1414 auto F = ShiftMap.find(Op);
1415 Value *W = (F != ShiftMap.end()) ? F->second : nullptr;
1416 if (W == nullptr) {
1417 IRB.SetInsertPoint(&*I);
1418 // First, the shift amount will be CIV or CIV+1, depending on
1419 // whether the value is early or late. Instead of creating CIV+1,
1420 // do a single shift of the value.
1421 Value *ShAmt = CIV, *ShVal = Op;
1422 auto *VTy = cast<IntegerType>(ShVal->getType());
1423 auto *ATy = cast<IntegerType>(ShAmt->getType());
1424 if (Late.count(&*I))
1425 ShVal = IRB.CreateShl(Op, ConstantInt::get(VTy, 1));
1426 // Second, the types of the shifted value and the shift amount
1427 // must match.
1428 if (VTy != ATy) {
1429 if (VTy->getBitWidth() < ATy->getBitWidth())
1430 ShVal = upcast(CastMap, IRB, ShVal, ATy);
1431 else
1432 ShAmt = upcast(CastMap, IRB, ShAmt, VTy);
1433 }
1434 // Ready to generate the shift and memoize it.
1435 W = IRB.CreateShl(ShVal, ShAmt);
1436 ShiftMap.insert(std::make_pair(Op, W));
1437 }
1438 I->replaceUsesOfWith(Op, W);
1439 }
1440 }
1441
1442 // Update the users outside of the loop to account for having left
1443 // shifts. They would normally be shifted right in the loop, so shift
1444 // them right after the loop exit.
1445 // Take advantage of the loop-closed SSA form, which has all the post-
1446 // loop values in phi nodes.
1447 IRB.SetInsertPoint(ExitB, ExitB->getFirstInsertionPt());
1448 for (auto P = ExitB->begin(), Q = ExitB->end(); P != Q; ++P) {
1449 if (!isa<PHINode>(P))
1450 break;
1451 auto *PN = cast<PHINode>(P);
1452 Value *U = PN->getIncomingValueForBlock(LoopB);
1453 if (!Users.count(U))
1454 continue;
1455 Value *S = IRB.CreateLShr(PN, ConstantInt::get(PN->getType(), IterCount));
1456 PN->replaceAllUsesWith(S);
1457 // The above RAUW will create
1458 // S = lshr S, IterCount
1459 // so we need to fix it back into
1460 // S = lshr PN, IterCount
1461 cast<User>(S)->replaceUsesOfWith(S, PN);
1462 }
1463
1464 return true;
1465}
1466
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +00001467void PolynomialMultiplyRecognize::cleanupLoopBody(BasicBlock *LoopB) {
1468 for (auto &I : *LoopB)
Daniel Berlin4d0fe642017-04-28 19:55:38 +00001469 if (Value *SV = SimplifyInstruction(&I, {DL, &TLI, &DT}))
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +00001470 I.replaceAllUsesWith(SV);
1471
1472 for (auto I = LoopB->begin(), N = I; I != LoopB->end(); I = N) {
1473 N = std::next(I);
1474 RecursivelyDeleteTriviallyDeadInstructions(&*I, &TLI);
1475 }
1476}
1477
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +00001478unsigned PolynomialMultiplyRecognize::getInverseMxN(unsigned QP) {
1479 // Arrays of coefficients of Q and the inverse, C.
1480 // Q[i] = coefficient at x^i.
1481 std::array<char,32> Q, C;
1482
1483 for (unsigned i = 0; i < 32; ++i) {
1484 Q[i] = QP & 1;
1485 QP >>= 1;
1486 }
1487 assert(Q[0] == 1);
1488
1489 // Find C, such that
1490 // (Q[n]*x^n + ... + Q[1]*x + Q[0]) * (C[n]*x^n + ... + C[1]*x + C[0]) = 1
1491 //
1492 // For it to have a solution, Q[0] must be 1. Since this is Z2[x], the
1493 // operations * and + are & and ^ respectively.
1494 //
1495 // Find C[i] recursively, by comparing i-th coefficient in the product
1496 // with 0 (or 1 for i=0).
1497 //
1498 // C[0] = 1, since C[0] = Q[0], and Q[0] = 1.
1499 C[0] = 1;
1500 for (unsigned i = 1; i < 32; ++i) {
1501 // Solve for C[i] in:
1502 // C[0]Q[i] ^ C[1]Q[i-1] ^ ... ^ C[i-1]Q[1] ^ C[i]Q[0] = 0
1503 // This is equivalent to
1504 // C[0]Q[i] ^ C[1]Q[i-1] ^ ... ^ C[i-1]Q[1] ^ C[i] = 0
1505 // which is
1506 // C[0]Q[i] ^ C[1]Q[i-1] ^ ... ^ C[i-1]Q[1] = C[i]
1507 unsigned T = 0;
1508 for (unsigned j = 0; j < i; ++j)
1509 T = T ^ (C[j] & Q[i-j]);
1510 C[i] = T;
1511 }
1512
1513 unsigned QV = 0;
1514 for (unsigned i = 0; i < 32; ++i)
1515 if (C[i])
1516 QV |= (1 << i);
1517
1518 return QV;
1519}
1520
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +00001521Value *PolynomialMultiplyRecognize::generate(BasicBlock::iterator At,
1522 ParsedValues &PV) {
1523 IRBuilder<> B(&*At);
1524 Module *M = At->getParent()->getParent()->getParent();
1525 Value *PMF = Intrinsic::getDeclaration(M, Intrinsic::hexagon_M4_pmpyw);
1526
1527 Value *P = PV.P, *Q = PV.Q, *P0 = P;
1528 unsigned IC = PV.IterCount;
1529
1530 if (PV.M != nullptr)
1531 P0 = P = B.CreateXor(P, PV.M);
1532
1533 // Create a bit mask to clear the high bits beyond IterCount.
1534 auto *BMI = ConstantInt::get(P->getType(), APInt::getLowBitsSet(32, IC));
1535
1536 if (PV.IterCount != 32)
1537 P = B.CreateAnd(P, BMI);
1538
1539 if (PV.Inv) {
1540 auto *QI = dyn_cast<ConstantInt>(PV.Q);
1541 assert(QI && QI->getBitWidth() <= 32);
1542
1543 // Again, clearing bits beyond IterCount.
1544 unsigned M = (1 << PV.IterCount) - 1;
1545 unsigned Tmp = (QI->getZExtValue() | 1) & M;
1546 unsigned QV = getInverseMxN(Tmp) & M;
1547 auto *QVI = ConstantInt::get(QI->getType(), QV);
1548 P = B.CreateCall(PMF, {P, QVI});
1549 P = B.CreateTrunc(P, QI->getType());
1550 if (IC != 32)
1551 P = B.CreateAnd(P, BMI);
1552 }
1553
1554 Value *R = B.CreateCall(PMF, {P, Q});
1555
1556 if (PV.M != nullptr)
1557 R = B.CreateXor(R, B.CreateIntCast(P0, R->getType(), false));
1558
1559 return R;
1560}
1561
Krzysztof Parzyszek385a4e042017-11-07 17:05:54 +00001562static bool hasZeroSignBit(const Value *V) {
1563 if (const auto *CI = dyn_cast<const ConstantInt>(V))
1564 return (CI->getType()->getSignBit() & CI->getSExtValue()) == 0;
1565 const Instruction *I = dyn_cast<const Instruction>(V);
1566 if (!I)
1567 return false;
1568 switch (I->getOpcode()) {
1569 case Instruction::LShr:
1570 if (const auto SI = dyn_cast<const ConstantInt>(I->getOperand(1)))
1571 return SI->getZExtValue() > 0;
1572 return false;
1573 case Instruction::Or:
1574 case Instruction::Xor:
1575 return hasZeroSignBit(I->getOperand(0)) &&
1576 hasZeroSignBit(I->getOperand(1));
1577 case Instruction::And:
1578 return hasZeroSignBit(I->getOperand(0)) ||
1579 hasZeroSignBit(I->getOperand(1));
1580 }
1581 return false;
1582}
1583
Krzysztof Parzyszek5d9844f2018-01-30 22:03:59 +00001584void PolynomialMultiplyRecognize::setupPreSimplifier(Simplifier &S) {
1585 S.addRule("sink-zext",
Krzysztof Parzyszekd033d1f2017-03-21 17:09:27 +00001586 // Sink zext past bitwise operations.
1587 [](Instruction *I, LLVMContext &Ctx) -> Value* {
1588 if (I->getOpcode() != Instruction::ZExt)
1589 return nullptr;
1590 Instruction *T = dyn_cast<Instruction>(I->getOperand(0));
1591 if (!T)
1592 return nullptr;
1593 switch (T->getOpcode()) {
1594 case Instruction::And:
1595 case Instruction::Or:
1596 case Instruction::Xor:
1597 break;
1598 default:
1599 return nullptr;
1600 }
1601 IRBuilder<> B(Ctx);
1602 return B.CreateBinOp(cast<BinaryOperator>(T)->getOpcode(),
1603 B.CreateZExt(T->getOperand(0), I->getType()),
1604 B.CreateZExt(T->getOperand(1), I->getType()));
1605 });
Krzysztof Parzyszek5d9844f2018-01-30 22:03:59 +00001606 S.addRule("xor/and -> and/xor",
Krzysztof Parzyszekd033d1f2017-03-21 17:09:27 +00001607 // (xor (and x a) (and y a)) -> (and (xor x y) a)
1608 [](Instruction *I, LLVMContext &Ctx) -> Value* {
1609 if (I->getOpcode() != Instruction::Xor)
1610 return nullptr;
1611 Instruction *And0 = dyn_cast<Instruction>(I->getOperand(0));
1612 Instruction *And1 = dyn_cast<Instruction>(I->getOperand(1));
1613 if (!And0 || !And1)
1614 return nullptr;
1615 if (And0->getOpcode() != Instruction::And ||
1616 And1->getOpcode() != Instruction::And)
1617 return nullptr;
1618 if (And0->getOperand(1) != And1->getOperand(1))
1619 return nullptr;
1620 IRBuilder<> B(Ctx);
1621 return B.CreateAnd(B.CreateXor(And0->getOperand(0), And1->getOperand(0)),
1622 And0->getOperand(1));
1623 });
Krzysztof Parzyszek5d9844f2018-01-30 22:03:59 +00001624 S.addRule("sink binop into select",
Krzysztof Parzyszekd033d1f2017-03-21 17:09:27 +00001625 // (Op (select c x y) z) -> (select c (Op x z) (Op y z))
1626 // (Op x (select c y z)) -> (select c (Op x y) (Op x z))
1627 [](Instruction *I, LLVMContext &Ctx) -> Value* {
1628 BinaryOperator *BO = dyn_cast<BinaryOperator>(I);
1629 if (!BO)
1630 return nullptr;
1631 Instruction::BinaryOps Op = BO->getOpcode();
1632 if (SelectInst *Sel = dyn_cast<SelectInst>(BO->getOperand(0))) {
1633 IRBuilder<> B(Ctx);
1634 Value *X = Sel->getTrueValue(), *Y = Sel->getFalseValue();
1635 Value *Z = BO->getOperand(1);
1636 return B.CreateSelect(Sel->getCondition(),
1637 B.CreateBinOp(Op, X, Z),
1638 B.CreateBinOp(Op, Y, Z));
1639 }
1640 if (SelectInst *Sel = dyn_cast<SelectInst>(BO->getOperand(1))) {
1641 IRBuilder<> B(Ctx);
1642 Value *X = BO->getOperand(0);
1643 Value *Y = Sel->getTrueValue(), *Z = Sel->getFalseValue();
1644 return B.CreateSelect(Sel->getCondition(),
1645 B.CreateBinOp(Op, X, Y),
1646 B.CreateBinOp(Op, X, Z));
1647 }
1648 return nullptr;
1649 });
Krzysztof Parzyszek5d9844f2018-01-30 22:03:59 +00001650 S.addRule("fold select-select",
Krzysztof Parzyszekd033d1f2017-03-21 17:09:27 +00001651 // (select c (select c x y) z) -> (select c x z)
1652 // (select c x (select c y z)) -> (select c x z)
1653 [](Instruction *I, LLVMContext &Ctx) -> Value* {
1654 SelectInst *Sel = dyn_cast<SelectInst>(I);
1655 if (!Sel)
1656 return nullptr;
1657 IRBuilder<> B(Ctx);
1658 Value *C = Sel->getCondition();
1659 if (SelectInst *Sel0 = dyn_cast<SelectInst>(Sel->getTrueValue())) {
1660 if (Sel0->getCondition() == C)
1661 return B.CreateSelect(C, Sel0->getTrueValue(), Sel->getFalseValue());
1662 }
1663 if (SelectInst *Sel1 = dyn_cast<SelectInst>(Sel->getFalseValue())) {
1664 if (Sel1->getCondition() == C)
1665 return B.CreateSelect(C, Sel->getTrueValue(), Sel1->getFalseValue());
1666 }
1667 return nullptr;
1668 });
Krzysztof Parzyszek5d9844f2018-01-30 22:03:59 +00001669 S.addRule("or-signbit -> xor-signbit",
Krzysztof Parzyszekd033d1f2017-03-21 17:09:27 +00001670 // (or (lshr x 1) 0x800.0) -> (xor (lshr x 1) 0x800.0)
1671 [](Instruction *I, LLVMContext &Ctx) -> Value* {
1672 if (I->getOpcode() != Instruction::Or)
1673 return nullptr;
Krzysztof Parzyszekd033d1f2017-03-21 17:09:27 +00001674 ConstantInt *Msb = dyn_cast<ConstantInt>(I->getOperand(1));
1675 if (!Msb || Msb->getZExtValue() != Msb->getType()->getSignBit())
1676 return nullptr;
Krzysztof Parzyszek385a4e042017-11-07 17:05:54 +00001677 if (!hasZeroSignBit(I->getOperand(0)))
1678 return nullptr;
1679 return IRBuilder<>(Ctx).CreateXor(I->getOperand(0), Msb);
Krzysztof Parzyszekd033d1f2017-03-21 17:09:27 +00001680 });
Krzysztof Parzyszek5d9844f2018-01-30 22:03:59 +00001681 S.addRule("sink lshr into binop",
Krzysztof Parzyszekd033d1f2017-03-21 17:09:27 +00001682 // (lshr (BitOp x y) c) -> (BitOp (lshr x c) (lshr y c))
1683 [](Instruction *I, LLVMContext &Ctx) -> Value* {
1684 if (I->getOpcode() != Instruction::LShr)
1685 return nullptr;
1686 BinaryOperator *BitOp = dyn_cast<BinaryOperator>(I->getOperand(0));
1687 if (!BitOp)
1688 return nullptr;
1689 switch (BitOp->getOpcode()) {
1690 case Instruction::And:
1691 case Instruction::Or:
1692 case Instruction::Xor:
1693 break;
1694 default:
1695 return nullptr;
1696 }
1697 IRBuilder<> B(Ctx);
1698 Value *S = I->getOperand(1);
1699 return B.CreateBinOp(BitOp->getOpcode(),
1700 B.CreateLShr(BitOp->getOperand(0), S),
1701 B.CreateLShr(BitOp->getOperand(1), S));
1702 });
Krzysztof Parzyszek5d9844f2018-01-30 22:03:59 +00001703 S.addRule("expose bitop-const",
Krzysztof Parzyszekd033d1f2017-03-21 17:09:27 +00001704 // (BitOp1 (BitOp2 x a) b) -> (BitOp2 x (BitOp1 a b))
1705 [](Instruction *I, LLVMContext &Ctx) -> Value* {
1706 auto IsBitOp = [](unsigned Op) -> bool {
1707 switch (Op) {
1708 case Instruction::And:
1709 case Instruction::Or:
1710 case Instruction::Xor:
1711 return true;
1712 }
1713 return false;
1714 };
1715 BinaryOperator *BitOp1 = dyn_cast<BinaryOperator>(I);
1716 if (!BitOp1 || !IsBitOp(BitOp1->getOpcode()))
1717 return nullptr;
1718 BinaryOperator *BitOp2 = dyn_cast<BinaryOperator>(BitOp1->getOperand(0));
1719 if (!BitOp2 || !IsBitOp(BitOp2->getOpcode()))
1720 return nullptr;
1721 ConstantInt *CA = dyn_cast<ConstantInt>(BitOp2->getOperand(1));
1722 ConstantInt *CB = dyn_cast<ConstantInt>(BitOp1->getOperand(1));
1723 if (!CA || !CB)
1724 return nullptr;
1725 IRBuilder<> B(Ctx);
1726 Value *X = BitOp2->getOperand(0);
1727 return B.CreateBinOp(BitOp2->getOpcode(), X,
1728 B.CreateBinOp(BitOp1->getOpcode(), CA, CB));
1729 });
1730}
1731
Krzysztof Parzyszek5d9844f2018-01-30 22:03:59 +00001732void PolynomialMultiplyRecognize::setupPostSimplifier(Simplifier &S) {
1733 S.addRule("(and (xor (and x a) y) b) -> (and (xor x y) b), if b == b&a",
1734 [](Instruction *I, LLVMContext &Ctx) -> Value* {
1735 if (I->getOpcode() != Instruction::And)
1736 return nullptr;
1737 Instruction *Xor = dyn_cast<Instruction>(I->getOperand(0));
1738 ConstantInt *C0 = dyn_cast<ConstantInt>(I->getOperand(1));
1739 if (!Xor || !C0)
1740 return nullptr;
1741 if (Xor->getOpcode() != Instruction::Xor)
1742 return nullptr;
1743 Instruction *And0 = dyn_cast<Instruction>(Xor->getOperand(0));
1744 Instruction *And1 = dyn_cast<Instruction>(Xor->getOperand(1));
1745 // Pick the first non-null and.
1746 if (!And0 || And0->getOpcode() != Instruction::And)
1747 std::swap(And0, And1);
1748 ConstantInt *C1 = dyn_cast<ConstantInt>(And0->getOperand(1));
1749 if (!C1)
1750 return nullptr;
1751 uint32_t V0 = C0->getZExtValue();
1752 uint32_t V1 = C1->getZExtValue();
1753 if (V0 != (V0 & V1))
1754 return nullptr;
1755 IRBuilder<> B(Ctx);
1756 return B.CreateAnd(B.CreateXor(And0->getOperand(0), And1), C0);
1757 });
1758}
1759
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +00001760bool PolynomialMultiplyRecognize::recognize() {
Krzysztof Parzyszekd033d1f2017-03-21 17:09:27 +00001761 DEBUG(dbgs() << "Starting PolynomialMultiplyRecognize on loop\n"
1762 << *CurLoop << '\n');
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +00001763 // Restrictions:
1764 // - The loop must consist of a single block.
1765 // - The iteration count must be known at compile-time.
1766 // - The loop must have an induction variable starting from 0, and
1767 // incremented in each iteration of the loop.
1768 BasicBlock *LoopB = CurLoop->getHeader();
Krzysztof Parzyszekd033d1f2017-03-21 17:09:27 +00001769 DEBUG(dbgs() << "Loop header:\n" << *LoopB);
1770
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +00001771 if (LoopB != CurLoop->getLoopLatch())
1772 return false;
1773 BasicBlock *ExitB = CurLoop->getExitBlock();
1774 if (ExitB == nullptr)
1775 return false;
1776 BasicBlock *EntryB = CurLoop->getLoopPreheader();
1777 if (EntryB == nullptr)
1778 return false;
1779
1780 unsigned IterCount = 0;
1781 const SCEV *CT = SE.getBackedgeTakenCount(CurLoop);
1782 if (isa<SCEVCouldNotCompute>(CT))
1783 return false;
1784 if (auto *CV = dyn_cast<SCEVConstant>(CT))
1785 IterCount = CV->getValue()->getZExtValue() + 1;
1786
1787 Value *CIV = getCountIV(LoopB);
1788 ParsedValues PV;
Krzysztof Parzyszek5d9844f2018-01-30 22:03:59 +00001789 Simplifier PreSimp;
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +00001790 PV.IterCount = IterCount;
Krzysztof Parzyszekd033d1f2017-03-21 17:09:27 +00001791 DEBUG(dbgs() << "Loop IV: " << *CIV << "\nIterCount: " << IterCount << '\n');
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +00001792
Krzysztof Parzyszek5d9844f2018-01-30 22:03:59 +00001793 setupPreSimplifier(PreSimp);
Krzysztof Parzyszekd033d1f2017-03-21 17:09:27 +00001794
1795 // Perform a preliminary scan of select instructions to see if any of them
1796 // looks like a generator of the polynomial multiply steps. Assume that a
1797 // loop can only contain a single transformable operation, so stop the
1798 // traversal after the first reasonable candidate was found.
1799 // XXX: Currently this approach can modify the loop before being 100% sure
1800 // that the transformation can be carried out.
1801 bool FoundPreScan = false;
Krzysztof Parzyszek385a4e042017-11-07 17:05:54 +00001802 auto FeedsPHI = [LoopB](const Value *V) -> bool {
1803 for (const Value *U : V->users()) {
1804 if (const auto *P = dyn_cast<const PHINode>(U))
1805 if (P->getParent() == LoopB)
1806 return true;
1807 }
1808 return false;
1809 };
Krzysztof Parzyszekd033d1f2017-03-21 17:09:27 +00001810 for (Instruction &In : *LoopB) {
1811 SelectInst *SI = dyn_cast<SelectInst>(&In);
Krzysztof Parzyszek385a4e042017-11-07 17:05:54 +00001812 if (!SI || !FeedsPHI(SI))
Krzysztof Parzyszekd033d1f2017-03-21 17:09:27 +00001813 continue;
1814
1815 Simplifier::Context C(SI);
Krzysztof Parzyszek5d9844f2018-01-30 22:03:59 +00001816 Value *T = PreSimp.simplify(C);
Krzysztof Parzyszekd033d1f2017-03-21 17:09:27 +00001817 SelectInst *SelI = (T && isa<SelectInst>(T)) ? cast<SelectInst>(T) : SI;
1818 DEBUG(dbgs() << "scanSelect(pre-scan): " << PE(C, SelI) << '\n');
1819 if (scanSelect(SelI, LoopB, EntryB, CIV, PV, true)) {
1820 FoundPreScan = true;
1821 if (SelI != SI) {
1822 Value *NewSel = C.materialize(LoopB, SI->getIterator());
1823 SI->replaceAllUsesWith(NewSel);
1824 RecursivelyDeleteTriviallyDeadInstructions(SI, &TLI);
1825 }
1826 break;
1827 }
1828 }
1829
1830 if (!FoundPreScan) {
1831 DEBUG(dbgs() << "Have not found candidates for pmpy\n");
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +00001832 return false;
Krzysztof Parzyszekd033d1f2017-03-21 17:09:27 +00001833 }
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +00001834
1835 if (!PV.Left) {
Krzysztof Parzyszekd033d1f2017-03-21 17:09:27 +00001836 // The right shift version actually only returns the higher bits of
1837 // the result (each iteration discards the LSB). If we want to convert it
1838 // to a left-shifting loop, the working data type must be at least as
1839 // wide as the target's pmpy instruction.
1840 if (!promoteTypes(LoopB, ExitB))
1841 return false;
Krzysztof Parzyszek5d9844f2018-01-30 22:03:59 +00001842 // Run post-promotion simplifications.
1843 Simplifier PostSimp;
1844 setupPostSimplifier(PostSimp);
1845 for (Instruction &In : *LoopB) {
1846 SelectInst *SI = dyn_cast<SelectInst>(&In);
1847 if (!SI || !FeedsPHI(SI))
1848 continue;
1849 Simplifier::Context C(SI);
1850 Value *T = PostSimp.simplify(C);
1851 SelectInst *SelI = dyn_cast_or_null<SelectInst>(T);
1852 if (SelI != SI) {
1853 Value *NewSel = C.materialize(LoopB, SI->getIterator());
1854 SI->replaceAllUsesWith(NewSel);
1855 RecursivelyDeleteTriviallyDeadInstructions(SI, &TLI);
1856 }
1857 break;
1858 }
1859
Krzysztof Parzyszek9bd4d912017-06-13 13:51:49 +00001860 if (!convertShiftsToLeft(LoopB, ExitB, IterCount))
1861 return false;
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +00001862 cleanupLoopBody(LoopB);
1863 }
1864
Krzysztof Parzyszekd033d1f2017-03-21 17:09:27 +00001865 // Scan the loop again, find the generating select instruction.
1866 bool FoundScan = false;
1867 for (Instruction &In : *LoopB) {
1868 SelectInst *SelI = dyn_cast<SelectInst>(&In);
1869 if (!SelI)
1870 continue;
1871 DEBUG(dbgs() << "scanSelect: " << *SelI << '\n');
1872 FoundScan = scanSelect(SelI, LoopB, EntryB, CIV, PV, false);
1873 if (FoundScan)
1874 break;
1875 }
1876 assert(FoundScan);
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +00001877
1878 DEBUG({
1879 StringRef PP = (PV.M ? "(P+M)" : "P");
1880 if (!PV.Inv)
1881 dbgs() << "Found pmpy idiom: R = " << PP << ".Q\n";
1882 else
1883 dbgs() << "Found inverse pmpy idiom: R = (" << PP << "/Q).Q) + "
1884 << PP << "\n";
1885 dbgs() << " Res:" << *PV.Res << "\n P:" << *PV.P << "\n";
1886 if (PV.M)
1887 dbgs() << " M:" << *PV.M << "\n";
1888 dbgs() << " Q:" << *PV.Q << "\n";
1889 dbgs() << " Iteration count:" << PV.IterCount << "\n";
1890 });
1891
1892 BasicBlock::iterator At(EntryB->getTerminator());
1893 Value *PM = generate(At, PV);
1894 if (PM == nullptr)
1895 return false;
1896
1897 if (PM->getType() != PV.Res->getType())
1898 PM = IRBuilder<>(&*At).CreateIntCast(PM, PV.Res->getType(), false);
1899
1900 PV.Res->replaceAllUsesWith(PM);
1901 PV.Res->eraseFromParent();
1902 return true;
1903}
1904
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +00001905int HexagonLoopIdiomRecognize::getSCEVStride(const SCEVAddRecExpr *S) {
1906 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(S->getOperand(1)))
1907 return SC->getAPInt().getSExtValue();
1908 return 0;
1909}
1910
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +00001911bool HexagonLoopIdiomRecognize::isLegalStore(Loop *CurLoop, StoreInst *SI) {
Krzysztof Parzyszek35ce5da2017-01-27 20:40:14 +00001912 // Allow volatile stores if HexagonVolatileMemcpy is enabled.
1913 if (!(SI->isVolatile() && HexagonVolatileMemcpy) && !SI->isSimple())
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +00001914 return false;
1915
1916 Value *StoredVal = SI->getValueOperand();
1917 Value *StorePtr = SI->getPointerOperand();
1918
1919 // Reject stores that are so large that they overflow an unsigned.
1920 uint64_t SizeInBits = DL->getTypeSizeInBits(StoredVal->getType());
1921 if ((SizeInBits & 7) || (SizeInBits >> 32) != 0)
1922 return false;
1923
1924 // See if the pointer expression is an AddRec like {base,+,1} on the current
1925 // loop, which indicates a strided store. If we have something else, it's a
1926 // random store we can't handle.
1927 auto *StoreEv = dyn_cast<SCEVAddRecExpr>(SE->getSCEV(StorePtr));
1928 if (!StoreEv || StoreEv->getLoop() != CurLoop || !StoreEv->isAffine())
1929 return false;
1930
1931 // Check to see if the stride matches the size of the store. If so, then we
1932 // know that every byte is touched in the loop.
1933 int Stride = getSCEVStride(StoreEv);
1934 if (Stride == 0)
1935 return false;
Jonas Paulssonf0ff20f2017-11-28 14:44:32 +00001936 unsigned StoreSize = DL->getTypeStoreSize(SI->getValueOperand()->getType());
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +00001937 if (StoreSize != unsigned(std::abs(Stride)))
1938 return false;
1939
1940 // The store must be feeding a non-volatile load.
1941 LoadInst *LI = dyn_cast<LoadInst>(SI->getValueOperand());
1942 if (!LI || !LI->isSimple())
1943 return false;
1944
1945 // See if the pointer expression is an AddRec like {base,+,1} on the current
1946 // loop, which indicates a strided load. If we have something else, it's a
1947 // random load we can't handle.
1948 Value *LoadPtr = LI->getPointerOperand();
1949 auto *LoadEv = dyn_cast<SCEVAddRecExpr>(SE->getSCEV(LoadPtr));
1950 if (!LoadEv || LoadEv->getLoop() != CurLoop || !LoadEv->isAffine())
1951 return false;
1952
1953 // The store and load must share the same stride.
1954 if (StoreEv->getOperand(1) != LoadEv->getOperand(1))
1955 return false;
1956
1957 // Success. This store can be converted into a memcpy.
1958 return true;
1959}
1960
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +00001961/// mayLoopAccessLocation - Return true if the specified loop might access the
1962/// specified pointer location, which is a loop-strided access. The 'Access'
1963/// argument specifies what the verboten forms of access are (read or write).
1964static bool
1965mayLoopAccessLocation(Value *Ptr, ModRefInfo Access, Loop *L,
1966 const SCEV *BECount, unsigned StoreSize,
1967 AliasAnalysis &AA,
1968 SmallPtrSetImpl<Instruction *> &Ignored) {
1969 // Get the location that may be stored across the loop. Since the access
1970 // is strided positively through memory, we say that the modified location
1971 // starts at the pointer and has infinite size.
1972 uint64_t AccessSize = MemoryLocation::UnknownSize;
1973
1974 // If the loop iterates a fixed number of times, we can refine the access
1975 // size to be exactly the size of the memset, which is (BECount+1)*StoreSize
1976 if (const SCEVConstant *BECst = dyn_cast<SCEVConstant>(BECount))
1977 AccessSize = (BECst->getValue()->getZExtValue() + 1) * StoreSize;
1978
1979 // TODO: For this to be really effective, we have to dive into the pointer
1980 // operand in the store. Store to &A[i] of 100 will always return may alias
1981 // with store of &A[100], we need to StoreLoc to be "A" with size of 100,
1982 // which will then no-alias a store to &A[100].
1983 MemoryLocation StoreLoc(Ptr, AccessSize);
1984
1985 for (auto *B : L->blocks())
1986 for (auto &I : *B)
Alina Sbirlea63d22502017-12-05 20:12:23 +00001987 if (Ignored.count(&I) == 0 &&
Alina Sbirlea18fea012017-12-06 19:56:37 +00001988 isModOrRefSet(
1989 intersectModRef(AA.getModRefInfo(&I, StoreLoc), Access)))
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +00001990 return true;
1991
1992 return false;
1993}
1994
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +00001995void HexagonLoopIdiomRecognize::collectStores(Loop *CurLoop, BasicBlock *BB,
1996 SmallVectorImpl<StoreInst*> &Stores) {
1997 Stores.clear();
1998 for (Instruction &I : *BB)
1999 if (StoreInst *SI = dyn_cast<StoreInst>(&I))
2000 if (isLegalStore(CurLoop, SI))
2001 Stores.push_back(SI);
2002}
2003
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +00002004bool HexagonLoopIdiomRecognize::processCopyingStore(Loop *CurLoop,
2005 StoreInst *SI, const SCEV *BECount) {
Michael Kupersteine18aad32017-01-31 22:48:45 +00002006 assert((SI->isSimple() || (SI->isVolatile() && HexagonVolatileMemcpy)) &&
2007 "Expected only non-volatile stores, or Hexagon-specific memcpy"
2008 "to volatile destination.");
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +00002009
2010 Value *StorePtr = SI->getPointerOperand();
2011 auto *StoreEv = cast<SCEVAddRecExpr>(SE->getSCEV(StorePtr));
2012 unsigned Stride = getSCEVStride(StoreEv);
Jonas Paulssonf0ff20f2017-11-28 14:44:32 +00002013 unsigned StoreSize = DL->getTypeStoreSize(SI->getValueOperand()->getType());
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +00002014 if (Stride != StoreSize)
2015 return false;
2016
2017 // See if the pointer expression is an AddRec like {base,+,1} on the current
2018 // loop, which indicates a strided load. If we have something else, it's a
2019 // random load we can't handle.
2020 LoadInst *LI = dyn_cast<LoadInst>(SI->getValueOperand());
2021 auto *LoadEv = cast<SCEVAddRecExpr>(SE->getSCEV(LI->getPointerOperand()));
2022
2023 // The trip count of the loop and the base pointer of the addrec SCEV is
2024 // guaranteed to be loop invariant, which means that it should dominate the
2025 // header. This allows us to insert code for it in the preheader.
2026 BasicBlock *Preheader = CurLoop->getLoopPreheader();
2027 Instruction *ExpPt = Preheader->getTerminator();
2028 IRBuilder<> Builder(ExpPt);
2029 SCEVExpander Expander(*SE, *DL, "hexagon-loop-idiom");
2030
2031 Type *IntPtrTy = Builder.getIntPtrTy(*DL, SI->getPointerAddressSpace());
2032
2033 // Okay, we have a strided store "p[i]" of a loaded value. We can turn
2034 // this into a memcpy/memmove in the loop preheader now if we want. However,
2035 // this would be unsafe to do if there is anything else in the loop that may
2036 // read or write the memory region we're storing to. For memcpy, this
2037 // includes the load that feeds the stores. Check for an alias by generating
2038 // the base address and checking everything.
2039 Value *StoreBasePtr = Expander.expandCodeFor(StoreEv->getStart(),
2040 Builder.getInt8PtrTy(SI->getPointerAddressSpace()), ExpPt);
2041 Value *LoadBasePtr = nullptr;
2042
2043 bool Overlap = false;
2044 bool DestVolatile = SI->isVolatile();
2045 Type *BECountTy = BECount->getType();
2046
2047 if (DestVolatile) {
2048 // The trip count must fit in i32, since it is the type of the "num_words"
2049 // argument to hexagon_memcpy_forward_vp4cp4n2.
2050 if (StoreSize != 4 || DL->getTypeSizeInBits(BECountTy) > 32) {
2051CleanupAndExit:
2052 // If we generated new code for the base pointer, clean up.
2053 Expander.clear();
2054 if (StoreBasePtr && (LoadBasePtr != StoreBasePtr)) {
2055 RecursivelyDeleteTriviallyDeadInstructions(StoreBasePtr, TLI);
2056 StoreBasePtr = nullptr;
2057 }
2058 if (LoadBasePtr) {
2059 RecursivelyDeleteTriviallyDeadInstructions(LoadBasePtr, TLI);
2060 LoadBasePtr = nullptr;
2061 }
2062 return false;
2063 }
2064 }
2065
2066 SmallPtrSet<Instruction*, 2> Ignore1;
2067 Ignore1.insert(SI);
Alina Sbirlea193429f2017-12-07 22:41:34 +00002068 if (mayLoopAccessLocation(StoreBasePtr, ModRefInfo::ModRef, CurLoop, BECount,
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +00002069 StoreSize, *AA, Ignore1)) {
2070 // Check if the load is the offending instruction.
2071 Ignore1.insert(LI);
Alina Sbirlea193429f2017-12-07 22:41:34 +00002072 if (mayLoopAccessLocation(StoreBasePtr, ModRefInfo::ModRef, CurLoop,
2073 BECount, StoreSize, *AA, Ignore1)) {
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +00002074 // Still bad. Nothing we can do.
2075 goto CleanupAndExit;
2076 }
2077 // It worked with the load ignored.
2078 Overlap = true;
2079 }
2080
2081 if (!Overlap) {
2082 if (DisableMemcpyIdiom || !HasMemcpy)
2083 goto CleanupAndExit;
2084 } else {
2085 // Don't generate memmove if this function will be inlined. This is
2086 // because the caller will undergo this transformation after inlining.
2087 Function *Func = CurLoop->getHeader()->getParent();
2088 if (Func->hasFnAttribute(Attribute::AlwaysInline))
2089 goto CleanupAndExit;
2090
2091 // In case of a memmove, the call to memmove will be executed instead
2092 // of the loop, so we need to make sure that there is nothing else in
2093 // the loop than the load, store and instructions that these two depend
2094 // on.
2095 SmallVector<Instruction*,2> Insts;
2096 Insts.push_back(SI);
2097 Insts.push_back(LI);
2098 if (!coverLoop(CurLoop, Insts))
2099 goto CleanupAndExit;
2100
2101 if (DisableMemmoveIdiom || !HasMemmove)
2102 goto CleanupAndExit;
Eugene Zelenko4d060b72017-07-29 00:56:56 +00002103 bool IsNested = CurLoop->getParentLoop() != nullptr;
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +00002104 if (IsNested && OnlyNonNestedMemmove)
2105 goto CleanupAndExit;
2106 }
2107
2108 // For a memcpy, we have to make sure that the input array is not being
2109 // mutated by the loop.
2110 LoadBasePtr = Expander.expandCodeFor(LoadEv->getStart(),
2111 Builder.getInt8PtrTy(LI->getPointerAddressSpace()), ExpPt);
2112
2113 SmallPtrSet<Instruction*, 2> Ignore2;
2114 Ignore2.insert(SI);
Alina Sbirlea193429f2017-12-07 22:41:34 +00002115 if (mayLoopAccessLocation(LoadBasePtr, ModRefInfo::Mod, CurLoop, BECount,
2116 StoreSize, *AA, Ignore2))
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +00002117 goto CleanupAndExit;
2118
2119 // Check the stride.
2120 bool StridePos = getSCEVStride(LoadEv) >= 0;
2121
2122 // Currently, the volatile memcpy only emulates traversing memory forward.
2123 if (!StridePos && DestVolatile)
2124 goto CleanupAndExit;
2125
2126 bool RuntimeCheck = (Overlap || DestVolatile);
2127
2128 BasicBlock *ExitB;
2129 if (RuntimeCheck) {
2130 // The runtime check needs a single exit block.
2131 SmallVector<BasicBlock*, 8> ExitBlocks;
2132 CurLoop->getUniqueExitBlocks(ExitBlocks);
2133 if (ExitBlocks.size() != 1)
2134 goto CleanupAndExit;
2135 ExitB = ExitBlocks[0];
2136 }
2137
2138 // The # stored bytes is (BECount+1)*Size. Expand the trip count out to
2139 // pointer size if it isn't already.
2140 LLVMContext &Ctx = SI->getContext();
2141 BECount = SE->getTruncateOrZeroExtend(BECount, IntPtrTy);
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +00002142 DebugLoc DLoc = SI->getDebugLoc();
2143
2144 const SCEV *NumBytesS =
2145 SE->getAddExpr(BECount, SE->getOne(IntPtrTy), SCEV::FlagNUW);
2146 if (StoreSize != 1)
2147 NumBytesS = SE->getMulExpr(NumBytesS, SE->getConstant(IntPtrTy, StoreSize),
2148 SCEV::FlagNUW);
2149 Value *NumBytes = Expander.expandCodeFor(NumBytesS, IntPtrTy, ExpPt);
2150 if (Instruction *In = dyn_cast<Instruction>(NumBytes))
Daniel Berlin4d0fe642017-04-28 19:55:38 +00002151 if (Value *Simp = SimplifyInstruction(In, {*DL, TLI, DT}))
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +00002152 NumBytes = Simp;
2153
2154 CallInst *NewCall;
2155
2156 if (RuntimeCheck) {
2157 unsigned Threshold = RuntimeMemSizeThreshold;
2158 if (ConstantInt *CI = dyn_cast<ConstantInt>(NumBytes)) {
2159 uint64_t C = CI->getZExtValue();
2160 if (Threshold != 0 && C < Threshold)
2161 goto CleanupAndExit;
2162 if (C < CompileTimeMemSizeThreshold)
2163 goto CleanupAndExit;
2164 }
2165
2166 BasicBlock *Header = CurLoop->getHeader();
2167 Function *Func = Header->getParent();
2168 Loop *ParentL = LF->getLoopFor(Preheader);
2169 StringRef HeaderName = Header->getName();
2170
2171 // Create a new (empty) preheader, and update the PHI nodes in the
2172 // header to use the new preheader.
2173 BasicBlock *NewPreheader = BasicBlock::Create(Ctx, HeaderName+".rtli.ph",
2174 Func, Header);
2175 if (ParentL)
2176 ParentL->addBasicBlockToLoop(NewPreheader, *LF);
2177 IRBuilder<>(NewPreheader).CreateBr(Header);
2178 for (auto &In : *Header) {
2179 PHINode *PN = dyn_cast<PHINode>(&In);
2180 if (!PN)
2181 break;
2182 int bx = PN->getBasicBlockIndex(Preheader);
2183 if (bx >= 0)
2184 PN->setIncomingBlock(bx, NewPreheader);
2185 }
2186 DT->addNewBlock(NewPreheader, Preheader);
2187 DT->changeImmediateDominator(Header, NewPreheader);
2188
2189 // Check for safe conditions to execute memmove.
2190 // If stride is positive, copying things from higher to lower addresses
2191 // is equivalent to memmove. For negative stride, it's the other way
2192 // around. Copying forward in memory with positive stride may not be
2193 // same as memmove since we may be copying values that we just stored
2194 // in some previous iteration.
2195 Value *LA = Builder.CreatePtrToInt(LoadBasePtr, IntPtrTy);
2196 Value *SA = Builder.CreatePtrToInt(StoreBasePtr, IntPtrTy);
2197 Value *LowA = StridePos ? SA : LA;
2198 Value *HighA = StridePos ? LA : SA;
2199 Value *CmpA = Builder.CreateICmpULT(LowA, HighA);
2200 Value *Cond = CmpA;
2201
Krzysztof Parzyszekc09a14e2017-08-24 11:59:53 +00002202 // Check for distance between pointers. Since the case LowA < HighA
2203 // is checked for above, assume LowA >= HighA.
2204 Value *Dist = Builder.CreateSub(LowA, HighA);
2205 Value *CmpD = Builder.CreateICmpSLE(NumBytes, Dist);
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +00002206 Value *CmpEither = Builder.CreateOr(Cond, CmpD);
2207 Cond = CmpEither;
2208
2209 if (Threshold != 0) {
2210 Type *Ty = NumBytes->getType();
2211 Value *Thr = ConstantInt::get(Ty, Threshold);
2212 Value *CmpB = Builder.CreateICmpULT(Thr, NumBytes);
2213 Value *CmpBoth = Builder.CreateAnd(Cond, CmpB);
2214 Cond = CmpBoth;
2215 }
2216 BasicBlock *MemmoveB = BasicBlock::Create(Ctx, Header->getName()+".rtli",
2217 Func, NewPreheader);
2218 if (ParentL)
2219 ParentL->addBasicBlockToLoop(MemmoveB, *LF);
2220 Instruction *OldT = Preheader->getTerminator();
2221 Builder.CreateCondBr(Cond, MemmoveB, NewPreheader);
2222 OldT->eraseFromParent();
2223 Preheader->setName(Preheader->getName()+".old");
2224 DT->addNewBlock(MemmoveB, Preheader);
2225 // Find the new immediate dominator of the exit block.
2226 BasicBlock *ExitD = Preheader;
2227 for (auto PI = pred_begin(ExitB), PE = pred_end(ExitB); PI != PE; ++PI) {
2228 BasicBlock *PB = *PI;
2229 ExitD = DT->findNearestCommonDominator(ExitD, PB);
2230 if (!ExitD)
2231 break;
2232 }
2233 // If the prior immediate dominator of ExitB was dominated by the
2234 // old preheader, then the old preheader becomes the new immediate
2235 // dominator. Otherwise don't change anything (because the newly
2236 // added blocks are dominated by the old preheader).
2237 if (ExitD && DT->dominates(Preheader, ExitD)) {
2238 DomTreeNode *BN = DT->getNode(ExitB);
2239 DomTreeNode *DN = DT->getNode(ExitD);
2240 BN->setIDom(DN);
2241 }
2242
2243 // Add a call to memmove to the conditional block.
2244 IRBuilder<> CondBuilder(MemmoveB);
2245 CondBuilder.CreateBr(ExitB);
2246 CondBuilder.SetInsertPoint(MemmoveB->getTerminator());
2247
2248 if (DestVolatile) {
2249 Type *Int32Ty = Type::getInt32Ty(Ctx);
2250 Type *Int32PtrTy = Type::getInt32PtrTy(Ctx);
2251 Type *VoidTy = Type::getVoidTy(Ctx);
2252 Module *M = Func->getParent();
2253 Constant *CF = M->getOrInsertFunction(HexagonVolatileMemcpyName, VoidTy,
Serge Guelton59a2d7b2017-04-11 15:01:18 +00002254 Int32PtrTy, Int32PtrTy, Int32Ty);
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +00002255 Function *Fn = cast<Function>(CF);
2256 Fn->setLinkage(Function::ExternalLinkage);
2257
2258 const SCEV *OneS = SE->getConstant(Int32Ty, 1);
2259 const SCEV *BECount32 = SE->getTruncateOrZeroExtend(BECount, Int32Ty);
2260 const SCEV *NumWordsS = SE->getAddExpr(BECount32, OneS, SCEV::FlagNUW);
2261 Value *NumWords = Expander.expandCodeFor(NumWordsS, Int32Ty,
2262 MemmoveB->getTerminator());
2263 if (Instruction *In = dyn_cast<Instruction>(NumWords))
Daniel Berlin4d0fe642017-04-28 19:55:38 +00002264 if (Value *Simp = SimplifyInstruction(In, {*DL, TLI, DT}))
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +00002265 NumWords = Simp;
2266
2267 Value *Op0 = (StoreBasePtr->getType() == Int32PtrTy)
2268 ? StoreBasePtr
2269 : CondBuilder.CreateBitCast(StoreBasePtr, Int32PtrTy);
2270 Value *Op1 = (LoadBasePtr->getType() == Int32PtrTy)
2271 ? LoadBasePtr
2272 : CondBuilder.CreateBitCast(LoadBasePtr, Int32PtrTy);
2273 NewCall = CondBuilder.CreateCall(Fn, {Op0, Op1, NumWords});
2274 } else {
Daniel Neilsonf4fa26f2018-02-09 23:33:35 +00002275 NewCall = CondBuilder.CreateMemMove(StoreBasePtr, SI->getAlignment(),
2276 LoadBasePtr, LI->getAlignment(),
2277 NumBytes);
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +00002278 }
2279 } else {
Daniel Neilsonf4fa26f2018-02-09 23:33:35 +00002280 NewCall = Builder.CreateMemCpy(StoreBasePtr, SI->getAlignment(),
2281 LoadBasePtr, LI->getAlignment(),
2282 NumBytes);
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +00002283 // Okay, the memcpy has been formed. Zap the original store and
2284 // anything that feeds into it.
2285 RecursivelyDeleteTriviallyDeadInstructions(SI, TLI);
2286 }
2287
2288 NewCall->setDebugLoc(DLoc);
2289
2290 DEBUG(dbgs() << " Formed " << (Overlap ? "memmove: " : "memcpy: ")
2291 << *NewCall << "\n"
2292 << " from load ptr=" << *LoadEv << " at: " << *LI << "\n"
2293 << " from store ptr=" << *StoreEv << " at: " << *SI << "\n");
2294
2295 return true;
2296}
2297
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +00002298// \brief Check if the instructions in Insts, together with their dependencies
2299// cover the loop in the sense that the loop could be safely eliminated once
2300// the instructions in Insts are removed.
2301bool HexagonLoopIdiomRecognize::coverLoop(Loop *L,
2302 SmallVectorImpl<Instruction*> &Insts) const {
2303 SmallSet<BasicBlock*,8> LoopBlocks;
2304 for (auto *B : L->blocks())
2305 LoopBlocks.insert(B);
2306
2307 SetVector<Instruction*> Worklist(Insts.begin(), Insts.end());
2308
2309 // Collect all instructions from the loop that the instructions in Insts
2310 // depend on (plus their dependencies, etc.). These instructions will
2311 // constitute the expression trees that feed those in Insts, but the trees
2312 // will be limited only to instructions contained in the loop.
2313 for (unsigned i = 0; i < Worklist.size(); ++i) {
2314 Instruction *In = Worklist[i];
2315 for (auto I = In->op_begin(), E = In->op_end(); I != E; ++I) {
2316 Instruction *OpI = dyn_cast<Instruction>(I);
2317 if (!OpI)
2318 continue;
2319 BasicBlock *PB = OpI->getParent();
2320 if (!LoopBlocks.count(PB))
2321 continue;
2322 Worklist.insert(OpI);
2323 }
2324 }
2325
2326 // Scan all instructions in the loop, if any of them have a user outside
2327 // of the loop, or outside of the expressions collected above, then either
2328 // the loop has a side-effect visible outside of it, or there are
2329 // instructions in it that are not involved in the original set Insts.
2330 for (auto *B : L->blocks()) {
2331 for (auto &In : *B) {
2332 if (isa<BranchInst>(In) || isa<DbgInfoIntrinsic>(In))
2333 continue;
2334 if (!Worklist.count(&In) && In.mayHaveSideEffects())
2335 return false;
2336 for (const auto &K : In.users()) {
2337 Instruction *UseI = dyn_cast<Instruction>(K);
2338 if (!UseI)
2339 continue;
2340 BasicBlock *UseB = UseI->getParent();
2341 if (LF->getLoopFor(UseB) != L)
2342 return false;
2343 }
2344 }
2345 }
2346
2347 return true;
2348}
2349
2350/// runOnLoopBlock - Process the specified block, which lives in a counted loop
2351/// with the specified backedge count. This block is known to be in the current
2352/// loop and not in any subloops.
2353bool HexagonLoopIdiomRecognize::runOnLoopBlock(Loop *CurLoop, BasicBlock *BB,
2354 const SCEV *BECount, SmallVectorImpl<BasicBlock*> &ExitBlocks) {
2355 // We can only promote stores in this block if they are unconditionally
2356 // executed in the loop. For a block to be unconditionally executed, it has
2357 // to dominate all the exit blocks of the loop. Verify this now.
2358 auto DominatedByBB = [this,BB] (BasicBlock *EB) -> bool {
2359 return DT->dominates(BB, EB);
2360 };
2361 if (!std::all_of(ExitBlocks.begin(), ExitBlocks.end(), DominatedByBB))
2362 return false;
2363
2364 bool MadeChange = false;
2365 // Look for store instructions, which may be optimized to memset/memcpy.
2366 SmallVector<StoreInst*,8> Stores;
2367 collectStores(CurLoop, BB, Stores);
2368
2369 // Optimize the store into a memcpy, if it feeds an similarly strided load.
2370 for (auto &SI : Stores)
2371 MadeChange |= processCopyingStore(CurLoop, SI, BECount);
2372
2373 return MadeChange;
2374}
2375
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +00002376bool HexagonLoopIdiomRecognize::runOnCountableLoop(Loop *L) {
2377 PolynomialMultiplyRecognize PMR(L, *DL, *DT, *TLI, *SE);
2378 if (PMR.recognize())
2379 return true;
2380
2381 if (!HasMemcpy && !HasMemmove)
2382 return false;
2383
2384 const SCEV *BECount = SE->getBackedgeTakenCount(L);
2385 assert(!isa<SCEVCouldNotCompute>(BECount) &&
2386 "runOnCountableLoop() called on a loop without a predictable"
2387 "backedge-taken count");
2388
2389 SmallVector<BasicBlock *, 8> ExitBlocks;
2390 L->getUniqueExitBlocks(ExitBlocks);
2391
2392 bool Changed = false;
2393
2394 // Scan all the blocks in the loop that are not in subloops.
2395 for (auto *BB : L->getBlocks()) {
2396 // Ignore blocks in subloops.
2397 if (LF->getLoopFor(BB) != L)
2398 continue;
2399 Changed |= runOnLoopBlock(L, BB, BECount, ExitBlocks);
2400 }
2401
2402 return Changed;
2403}
2404
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +00002405bool HexagonLoopIdiomRecognize::runOnLoop(Loop *L, LPPassManager &LPM) {
2406 const Module &M = *L->getHeader()->getParent()->getParent();
2407 if (Triple(M.getTargetTriple()).getArch() != Triple::hexagon)
2408 return false;
2409
2410 if (skipLoop(L))
2411 return false;
2412
2413 // If the loop could not be converted to canonical form, it must have an
2414 // indirectbr in it, just give up.
2415 if (!L->getLoopPreheader())
2416 return false;
2417
2418 // Disable loop idiom recognition if the function's name is a common idiom.
2419 StringRef Name = L->getHeader()->getParent()->getName();
2420 if (Name == "memset" || Name == "memcpy" || Name == "memmove")
2421 return false;
2422
2423 AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
2424 DL = &L->getHeader()->getModule()->getDataLayout();
2425 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
2426 LF = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
2427 TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
2428 SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
2429
2430 HasMemcpy = TLI->has(LibFunc_memcpy);
2431 HasMemmove = TLI->has(LibFunc_memmove);
2432
2433 if (SE->hasLoopInvariantBackedgeTakenCount(L))
2434 return runOnCountableLoop(L);
2435 return false;
2436}
2437
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +00002438Pass *llvm::createHexagonLoopIdiomPass() {
2439 return new HexagonLoopIdiomRecognize();
2440}