LLVM 20.0.0git
HexagonLoopIdiomRecognition.cpp
Go to the documentation of this file.
1//===- HexagonLoopIdiomRecognition.cpp ------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10#include "llvm/ADT/APInt.h"
11#include "llvm/ADT/DenseMap.h"
12#include "llvm/ADT/SetVector.h"
14#include "llvm/ADT/SmallSet.h"
16#include "llvm/ADT/StringRef.h"
27#include "llvm/IR/Attributes.h"
28#include "llvm/IR/BasicBlock.h"
29#include "llvm/IR/Constant.h"
30#include "llvm/IR/Constants.h"
31#include "llvm/IR/DataLayout.h"
32#include "llvm/IR/DebugLoc.h"
34#include "llvm/IR/Dominators.h"
35#include "llvm/IR/Function.h"
36#include "llvm/IR/IRBuilder.h"
37#include "llvm/IR/InstrTypes.h"
38#include "llvm/IR/Instruction.h"
41#include "llvm/IR/Intrinsics.h"
42#include "llvm/IR/IntrinsicsHexagon.h"
43#include "llvm/IR/Module.h"
44#include "llvm/IR/PassManager.h"
46#include "llvm/IR/Type.h"
47#include "llvm/IR/User.h"
48#include "llvm/IR/Value.h"
50#include "llvm/Pass.h"
54#include "llvm/Support/Debug.h"
63#include <algorithm>
64#include <array>
65#include <cassert>
66#include <cstdint>
67#include <cstdlib>
68#include <deque>
69#include <functional>
70#include <iterator>
71#include <map>
72#include <set>
73#include <utility>
74#include <vector>
75
76#define DEBUG_TYPE "hexagon-lir"
77
78using namespace llvm;
79
80static cl::opt<bool> DisableMemcpyIdiom("disable-memcpy-idiom",
81 cl::Hidden, cl::init(false),
82 cl::desc("Disable generation of memcpy in loop idiom recognition"));
83
84static cl::opt<bool> DisableMemmoveIdiom("disable-memmove-idiom",
85 cl::Hidden, cl::init(false),
86 cl::desc("Disable generation of memmove in loop idiom recognition"));
87
88static cl::opt<unsigned> RuntimeMemSizeThreshold("runtime-mem-idiom-threshold",
89 cl::Hidden, cl::init(0), cl::desc("Threshold (in bytes) for the runtime "
90 "check guarding the memmove."));
91
93 "compile-time-mem-idiom-threshold", cl::Hidden, cl::init(64),
94 cl::desc("Threshold (in bytes) to perform the transformation, if the "
95 "runtime loop count (mem transfer size) is known at compile-time."));
96
97static cl::opt<bool> OnlyNonNestedMemmove("only-nonnested-memmove-idiom",
98 cl::Hidden, cl::init(true),
99 cl::desc("Only enable generating memmove in non-nested loops"));
100
102 "disable-hexagon-volatile-memcpy", cl::Hidden, cl::init(false),
103 cl::desc("Enable Hexagon-specific memcpy for volatile destination."));
104
105static cl::opt<unsigned> SimplifyLimit("hlir-simplify-limit", cl::init(10000),
106 cl::Hidden, cl::desc("Maximum number of simplification steps in HLIR"));
107
109 = "hexagon_memcpy_forward_vp4cp4n2";
110
111
112namespace llvm {
113
116
117} // end namespace llvm
118
119namespace {
120
121class HexagonLoopIdiomRecognize {
122public:
123 explicit HexagonLoopIdiomRecognize(AliasAnalysis *AA, DominatorTree *DT,
124 LoopInfo *LF, const TargetLibraryInfo *TLI,
125 ScalarEvolution *SE)
126 : AA(AA), DT(DT), LF(LF), TLI(TLI), SE(SE) {}
127
128 bool run(Loop *L);
129
130private:
131 int getSCEVStride(const SCEVAddRecExpr *StoreEv);
132 bool isLegalStore(Loop *CurLoop, StoreInst *SI);
133 void collectStores(Loop *CurLoop, BasicBlock *BB,
135 bool processCopyingStore(Loop *CurLoop, StoreInst *SI, const SCEV *BECount);
136 bool coverLoop(Loop *L, SmallVectorImpl<Instruction *> &Insts) const;
137 bool runOnLoopBlock(Loop *CurLoop, BasicBlock *BB, const SCEV *BECount,
139 bool runOnCountableLoop(Loop *L);
140
141 AliasAnalysis *AA;
142 const DataLayout *DL;
143 DominatorTree *DT;
144 LoopInfo *LF;
145 const TargetLibraryInfo *TLI;
146 ScalarEvolution *SE;
147 bool HasMemcpy, HasMemmove;
148};
149
150class HexagonLoopIdiomRecognizeLegacyPass : public LoopPass {
151public:
152 static char ID;
153
154 explicit HexagonLoopIdiomRecognizeLegacyPass() : LoopPass(ID) {
157 }
158
159 StringRef getPassName() const override {
160 return "Recognize Hexagon-specific loop idioms";
161 }
162
163 void getAnalysisUsage(AnalysisUsage &AU) const override {
172 }
173
174 bool runOnLoop(Loop *L, LPPassManager &LPM) override;
175};
176
177struct Simplifier {
178 struct Rule {
179 using FuncType = std::function<Value *(Instruction *, LLVMContext &)>;
180 Rule(StringRef N, FuncType F) : Name(N), Fn(F) {}
181 StringRef Name; // For debugging.
182 FuncType Fn;
183 };
184
185 void addRule(StringRef N, const Rule::FuncType &F) {
186 Rules.push_back(Rule(N, F));
187 }
188
189private:
190 struct WorkListType {
191 WorkListType() = default;
192
193 void push_back(Value *V) {
194 // Do not push back duplicates.
195 if (S.insert(V).second)
196 Q.push_back(V);
197 }
198
199 Value *pop_front_val() {
200 Value *V = Q.front();
201 Q.pop_front();
202 S.erase(V);
203 return V;
204 }
205
206 bool empty() const { return Q.empty(); }
207
208 private:
209 std::deque<Value *> Q;
210 std::set<Value *> S;
211 };
212
213 using ValueSetType = std::set<Value *>;
214
215 std::vector<Rule> Rules;
216
217public:
218 struct Context {
219 using ValueMapType = DenseMap<Value *, Value *>;
220
221 Value *Root;
222 ValueSetType Used; // The set of all cloned values used by Root.
223 ValueSetType Clones; // The set of all cloned values.
224 LLVMContext &Ctx;
225
226 Context(Instruction *Exp)
227 : Ctx(Exp->getParent()->getParent()->getContext()) {
228 initialize(Exp);
229 }
230
231 ~Context() { cleanup(); }
232
233 void print(raw_ostream &OS, const Value *V) const;
234 Value *materialize(BasicBlock *B, BasicBlock::iterator At);
235
236 private:
237 friend struct Simplifier;
238
239 void initialize(Instruction *Exp);
240 void cleanup();
241
242 template <typename FuncT> void traverse(Value *V, FuncT F);
243 void record(Value *V);
244 void use(Value *V);
245 void unuse(Value *V);
246
247 bool equal(const Instruction *I, const Instruction *J) const;
248 Value *find(Value *Tree, Value *Sub) const;
249 Value *subst(Value *Tree, Value *OldV, Value *NewV);
250 void replace(Value *OldV, Value *NewV);
252 };
253
254 Value *simplify(Context &C);
255};
256
257 struct PE {
258 PE(const Simplifier::Context &c, Value *v = nullptr) : C(c), V(v) {}
259
260 const Simplifier::Context &C;
261 const Value *V;
262 };
263
265 raw_ostream &operator<<(raw_ostream &OS, const PE &P) {
266 P.C.print(OS, P.V ? P.V : P.C.Root);
267 return OS;
268 }
269
270} // end anonymous namespace
271
272char HexagonLoopIdiomRecognizeLegacyPass::ID = 0;
273
274INITIALIZE_PASS_BEGIN(HexagonLoopIdiomRecognizeLegacyPass, "hexagon-loop-idiom",
275 "Recognize Hexagon-specific loop idioms", false, false)
277INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
278INITIALIZE_PASS_DEPENDENCY(LCSSAWrapperPass)
283INITIALIZE_PASS_END(HexagonLoopIdiomRecognizeLegacyPass, "hexagon-loop-idiom",
284 "Recognize Hexagon-specific loop idioms", false, false)
285
286template <typename FuncT>
287void Simplifier::Context::traverse(Value *V, FuncT F) {
288 WorkListType Q;
289 Q.push_back(V);
290
291 while (!Q.empty()) {
292 Instruction *U = dyn_cast<Instruction>(Q.pop_front_val());
293 if (!U || U->getParent())
294 continue;
295 if (!F(U))
296 continue;
297 for (Value *Op : U->operands())
298 Q.push_back(Op);
299 }
300}
301
302void Simplifier::Context::print(raw_ostream &OS, const Value *V) const {
303 const auto *U = dyn_cast<const Instruction>(V);
304 if (!U) {
305 OS << V << '(' << *V << ')';
306 return;
307 }
308
309 if (U->getParent()) {
310 OS << U << '(';
311 U->printAsOperand(OS, true);
312 OS << ')';
313 return;
314 }
315
316 unsigned N = U->getNumOperands();
317 if (N != 0)
318 OS << U << '(';
319 OS << U->getOpcodeName();
320 for (const Value *Op : U->operands()) {
321 OS << ' ';
322 print(OS, Op);
323 }
324 if (N != 0)
325 OS << ')';
326}
327
328void Simplifier::Context::initialize(Instruction *Exp) {
329 // Perform a deep clone of the expression, set Root to the root
330 // of the clone, and build a map from the cloned values to the
331 // original ones.
332 ValueMapType M;
333 BasicBlock *Block = Exp->getParent();
334 WorkListType Q;
335 Q.push_back(Exp);
336
337 while (!Q.empty()) {
338 Value *V = Q.pop_front_val();
339 if (M.contains(V))
340 continue;
341 if (Instruction *U = dyn_cast<Instruction>(V)) {
342 if (isa<PHINode>(U) || U->getParent() != Block)
343 continue;
344 for (Value *Op : U->operands())
345 Q.push_back(Op);
346 M.insert({U, U->clone()});
347 }
348 }
349
350 for (std::pair<Value*,Value*> P : M) {
351 Instruction *U = cast<Instruction>(P.second);
352 for (unsigned i = 0, n = U->getNumOperands(); i != n; ++i) {
353 auto F = M.find(U->getOperand(i));
354 if (F != M.end())
355 U->setOperand(i, F->second);
356 }
357 }
358
359 auto R = M.find(Exp);
360 assert(R != M.end());
361 Root = R->second;
362
363 record(Root);
364 use(Root);
365}
366
367void Simplifier::Context::record(Value *V) {
368 auto Record = [this](Instruction *U) -> bool {
369 Clones.insert(U);
370 return true;
371 };
372 traverse(V, Record);
373}
374
375void Simplifier::Context::use(Value *V) {
376 auto Use = [this](Instruction *U) -> bool {
377 Used.insert(U);
378 return true;
379 };
380 traverse(V, Use);
381}
382
383void Simplifier::Context::unuse(Value *V) {
384 if (!isa<Instruction>(V) || cast<Instruction>(V)->getParent() != nullptr)
385 return;
386
387 auto Unuse = [this](Instruction *U) -> bool {
388 if (!U->use_empty())
389 return false;
390 Used.erase(U);
391 return true;
392 };
393 traverse(V, Unuse);
394}
395
396Value *Simplifier::Context::subst(Value *Tree, Value *OldV, Value *NewV) {
397 if (Tree == OldV)
398 return NewV;
399 if (OldV == NewV)
400 return Tree;
401
402 WorkListType Q;
403 Q.push_back(Tree);
404 while (!Q.empty()) {
405 Instruction *U = dyn_cast<Instruction>(Q.pop_front_val());
406 // If U is not an instruction, or it's not a clone, skip it.
407 if (!U || U->getParent())
408 continue;
409 for (unsigned i = 0, n = U->getNumOperands(); i != n; ++i) {
410 Value *Op = U->getOperand(i);
411 if (Op == OldV) {
412 U->setOperand(i, NewV);
413 unuse(OldV);
414 } else {
415 Q.push_back(Op);
416 }
417 }
418 }
419 return Tree;
420}
421
422void Simplifier::Context::replace(Value *OldV, Value *NewV) {
423 if (Root == OldV) {
424 Root = NewV;
425 use(Root);
426 return;
427 }
428
429 // NewV may be a complex tree that has just been created by one of the
430 // transformation rules. We need to make sure that it is commoned with
431 // the existing Root to the maximum extent possible.
432 // Identify all subtrees of NewV (including NewV itself) that have
433 // equivalent counterparts in Root, and replace those subtrees with
434 // these counterparts.
435 WorkListType Q;
436 Q.push_back(NewV);
437 while (!Q.empty()) {
438 Value *V = Q.pop_front_val();
439 Instruction *U = dyn_cast<Instruction>(V);
440 if (!U || U->getParent())
441 continue;
442 if (Value *DupV = find(Root, V)) {
443 if (DupV != V)
444 NewV = subst(NewV, V, DupV);
445 } else {
446 for (Value *Op : U->operands())
447 Q.push_back(Op);
448 }
449 }
450
451 // Now, simply replace OldV with NewV in Root.
452 Root = subst(Root, OldV, NewV);
453 use(Root);
454}
455
456void Simplifier::Context::cleanup() {
457 for (Value *V : Clones) {
458 Instruction *U = cast<Instruction>(V);
459 if (!U->getParent())
460 U->dropAllReferences();
461 }
462
463 for (Value *V : Clones) {
464 Instruction *U = cast<Instruction>(V);
465 if (!U->getParent())
466 U->deleteValue();
467 }
468}
469
470bool Simplifier::Context::equal(const Instruction *I,
471 const Instruction *J) const {
472 if (I == J)
473 return true;
474 if (!I->isSameOperationAs(J))
475 return false;
476 if (isa<PHINode>(I))
477 return I->isIdenticalTo(J);
478
479 for (unsigned i = 0, n = I->getNumOperands(); i != n; ++i) {
480 Value *OpI = I->getOperand(i), *OpJ = J->getOperand(i);
481 if (OpI == OpJ)
482 continue;
483 auto *InI = dyn_cast<const Instruction>(OpI);
484 auto *InJ = dyn_cast<const Instruction>(OpJ);
485 if (InI && InJ) {
486 if (!equal(InI, InJ))
487 return false;
488 } else if (InI != InJ || !InI)
489 return false;
490 }
491 return true;
492}
493
494Value *Simplifier::Context::find(Value *Tree, Value *Sub) const {
495 Instruction *SubI = dyn_cast<Instruction>(Sub);
496 WorkListType Q;
497 Q.push_back(Tree);
498
499 while (!Q.empty()) {
500 Value *V = Q.pop_front_val();
501 if (V == Sub)
502 return V;
503 Instruction *U = dyn_cast<Instruction>(V);
504 if (!U || U->getParent())
505 continue;
506 if (SubI && equal(SubI, U))
507 return U;
508 assert(!isa<PHINode>(U));
509 for (Value *Op : U->operands())
510 Q.push_back(Op);
511 }
512 return nullptr;
513}
514
515void Simplifier::Context::link(Instruction *I, BasicBlock *B,
517 if (I->getParent())
518 return;
519
520 for (Value *Op : I->operands()) {
521 if (Instruction *OpI = dyn_cast<Instruction>(Op))
522 link(OpI, B, At);
523 }
524
525 I->insertInto(B, At);
526}
527
528Value *Simplifier::Context::materialize(BasicBlock *B,
530 if (Instruction *RootI = dyn_cast<Instruction>(Root))
531 link(RootI, B, At);
532 return Root;
533}
534
535Value *Simplifier::simplify(Context &C) {
536 WorkListType Q;
537 Q.push_back(C.Root);
538 unsigned Count = 0;
539 const unsigned Limit = SimplifyLimit;
540
541 while (!Q.empty()) {
542 if (Count++ >= Limit)
543 break;
544 Instruction *U = dyn_cast<Instruction>(Q.pop_front_val());
545 if (!U || U->getParent() || !C.Used.count(U))
546 continue;
547 bool Changed = false;
548 for (Rule &R : Rules) {
549 Value *W = R.Fn(U, C.Ctx);
550 if (!W)
551 continue;
552 Changed = true;
553 C.record(W);
554 C.replace(U, W);
555 Q.push_back(C.Root);
556 break;
557 }
558 if (!Changed) {
559 for (Value *Op : U->operands())
560 Q.push_back(Op);
561 }
562 }
563 return Count < Limit ? C.Root : nullptr;
564}
565
566//===----------------------------------------------------------------------===//
567//
568// Implementation of PolynomialMultiplyRecognize
569//
570//===----------------------------------------------------------------------===//
571
572namespace {
573
574 class PolynomialMultiplyRecognize {
575 public:
576 explicit PolynomialMultiplyRecognize(Loop *loop, const DataLayout &dl,
577 const DominatorTree &dt, const TargetLibraryInfo &tli,
578 ScalarEvolution &se)
579 : CurLoop(loop), DL(dl), DT(dt), TLI(tli), SE(se) {}
580
581 bool recognize();
582
583 private:
584 using ValueSeq = SetVector<Value *>;
585
586 IntegerType *getPmpyType() const {
587 LLVMContext &Ctx = CurLoop->getHeader()->getParent()->getContext();
588 return IntegerType::get(Ctx, 32);
589 }
590
591 bool isPromotableTo(Value *V, IntegerType *Ty);
592 void promoteTo(Instruction *In, IntegerType *DestTy, BasicBlock *LoopB);
593 bool promoteTypes(BasicBlock *LoopB, BasicBlock *ExitB);
594
595 Value *getCountIV(BasicBlock *BB);
596 bool findCycle(Value *Out, Value *In, ValueSeq &Cycle);
597 void classifyCycle(Instruction *DivI, ValueSeq &Cycle, ValueSeq &Early,
598 ValueSeq &Late);
599 bool classifyInst(Instruction *UseI, ValueSeq &Early, ValueSeq &Late);
600 bool commutesWithShift(Instruction *I);
601 bool highBitsAreZero(Value *V, unsigned IterCount);
602 bool keepsHighBitsZero(Value *V, unsigned IterCount);
603 bool isOperandShifted(Instruction *I, Value *Op);
604 bool convertShiftsToLeft(BasicBlock *LoopB, BasicBlock *ExitB,
605 unsigned IterCount);
606 void cleanupLoopBody(BasicBlock *LoopB);
607
608 struct ParsedValues {
609 ParsedValues() = default;
610
611 Value *M = nullptr;
612 Value *P = nullptr;
613 Value *Q = nullptr;
614 Value *R = nullptr;
615 Value *X = nullptr;
616 Instruction *Res = nullptr;
617 unsigned IterCount = 0;
618 bool Left = false;
619 bool Inv = false;
620 };
621
622 bool matchLeftShift(SelectInst *SelI, Value *CIV, ParsedValues &PV);
623 bool matchRightShift(SelectInst *SelI, ParsedValues &PV);
624 bool scanSelect(SelectInst *SI, BasicBlock *LoopB, BasicBlock *PrehB,
625 Value *CIV, ParsedValues &PV, bool PreScan);
626 unsigned getInverseMxN(unsigned QP);
627 Value *generate(BasicBlock::iterator At, ParsedValues &PV);
628
629 void setupPreSimplifier(Simplifier &S);
630 void setupPostSimplifier(Simplifier &S);
631
632 Loop *CurLoop;
633 const DataLayout &DL;
634 const DominatorTree &DT;
635 const TargetLibraryInfo &TLI;
636 ScalarEvolution &SE;
637 };
638
639} // end anonymous namespace
640
641Value *PolynomialMultiplyRecognize::getCountIV(BasicBlock *BB) {
642 pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
643 if (std::distance(PI, PE) != 2)
644 return nullptr;
645 BasicBlock *PB = (*PI == BB) ? *std::next(PI) : *PI;
646
647 for (auto I = BB->begin(), E = BB->end(); I != E && isa<PHINode>(I); ++I) {
648 auto *PN = cast<PHINode>(I);
649 Value *InitV = PN->getIncomingValueForBlock(PB);
650 if (!isa<ConstantInt>(InitV) || !cast<ConstantInt>(InitV)->isZero())
651 continue;
652 Value *IterV = PN->getIncomingValueForBlock(BB);
653 auto *BO = dyn_cast<BinaryOperator>(IterV);
654 if (!BO)
655 continue;
656 if (BO->getOpcode() != Instruction::Add)
657 continue;
658 Value *IncV = nullptr;
659 if (BO->getOperand(0) == PN)
660 IncV = BO->getOperand(1);
661 else if (BO->getOperand(1) == PN)
662 IncV = BO->getOperand(0);
663 if (IncV == nullptr)
664 continue;
665
666 if (auto *T = dyn_cast<ConstantInt>(IncV))
667 if (T->isOne())
668 return PN;
669 }
670 return nullptr;
671}
672
674 for (auto UI = I->user_begin(), UE = I->user_end(); UI != UE;) {
675 Use &TheUse = UI.getUse();
676 ++UI;
677 if (auto *II = dyn_cast<Instruction>(TheUse.getUser()))
678 if (BB == II->getParent())
679 II->replaceUsesOfWith(I, J);
680 }
681}
682
683bool PolynomialMultiplyRecognize::matchLeftShift(SelectInst *SelI,
684 Value *CIV, ParsedValues &PV) {
685 // Match the following:
686 // select (X & (1 << i)) != 0 ? R ^ (Q << i) : R
687 // select (X & (1 << i)) == 0 ? R : R ^ (Q << i)
688 // The condition may also check for equality with the masked value, i.e
689 // select (X & (1 << i)) == (1 << i) ? R ^ (Q << i) : R
690 // select (X & (1 << i)) != (1 << i) ? R : R ^ (Q << i);
691
692 Value *CondV = SelI->getCondition();
693 Value *TrueV = SelI->getTrueValue();
694 Value *FalseV = SelI->getFalseValue();
695
696 using namespace PatternMatch;
697
699 Value *A = nullptr, *B = nullptr, *C = nullptr;
700
701 if (!match(CondV, m_ICmp(P, m_And(m_Value(A), m_Value(B)), m_Value(C))) &&
702 !match(CondV, m_ICmp(P, m_Value(C), m_And(m_Value(A), m_Value(B)))))
703 return false;
705 return false;
706 // Matched: select (A & B) == C ? ... : ...
707 // select (A & B) != C ? ... : ...
708
709 Value *X = nullptr, *Sh1 = nullptr;
710 // Check (A & B) for (X & (1 << i)):
711 if (match(A, m_Shl(m_One(), m_Specific(CIV)))) {
712 Sh1 = A;
713 X = B;
714 } else if (match(B, m_Shl(m_One(), m_Specific(CIV)))) {
715 Sh1 = B;
716 X = A;
717 } else {
718 // TODO: Could also check for an induction variable containing single
719 // bit shifted left by 1 in each iteration.
720 return false;
721 }
722
723 bool TrueIfZero;
724
725 // Check C against the possible values for comparison: 0 and (1 << i):
726 if (match(C, m_Zero()))
727 TrueIfZero = (P == CmpInst::ICMP_EQ);
728 else if (C == Sh1)
729 TrueIfZero = (P == CmpInst::ICMP_NE);
730 else
731 return false;
732
733 // So far, matched:
734 // select (X & (1 << i)) ? ... : ...
735 // including variations of the check against zero/non-zero value.
736
737 Value *ShouldSameV = nullptr, *ShouldXoredV = nullptr;
738 if (TrueIfZero) {
739 ShouldSameV = TrueV;
740 ShouldXoredV = FalseV;
741 } else {
742 ShouldSameV = FalseV;
743 ShouldXoredV = TrueV;
744 }
745
746 Value *Q = nullptr, *R = nullptr, *Y = nullptr, *Z = nullptr;
747 Value *T = nullptr;
748 if (match(ShouldXoredV, m_Xor(m_Value(Y), m_Value(Z)))) {
749 // Matched: select +++ ? ... : Y ^ Z
750 // select +++ ? Y ^ Z : ...
751 // where +++ denotes previously checked matches.
752 if (ShouldSameV == Y)
753 T = Z;
754 else if (ShouldSameV == Z)
755 T = Y;
756 else
757 return false;
758 R = ShouldSameV;
759 // Matched: select +++ ? R : R ^ T
760 // select +++ ? R ^ T : R
761 // depending on TrueIfZero.
762
763 } else if (match(ShouldSameV, m_Zero())) {
764 // Matched: select +++ ? 0 : ...
765 // select +++ ? ... : 0
766 if (!SelI->hasOneUse())
767 return false;
768 T = ShouldXoredV;
769 // Matched: select +++ ? 0 : T
770 // select +++ ? T : 0
771
772 Value *U = *SelI->user_begin();
773 if (!match(U, m_c_Xor(m_Specific(SelI), m_Value(R))))
774 return false;
775 // Matched: xor (select +++ ? 0 : T), R
776 // xor (select +++ ? T : 0), R
777 } else
778 return false;
779
780 // The xor input value T is isolated into its own match so that it could
781 // be checked against an induction variable containing a shifted bit
782 // (todo).
783 // For now, check against (Q << i).
784 if (!match(T, m_Shl(m_Value(Q), m_Specific(CIV))) &&
785 !match(T, m_Shl(m_ZExt(m_Value(Q)), m_ZExt(m_Specific(CIV)))))
786 return false;
787 // Matched: select +++ ? R : R ^ (Q << i)
788 // select +++ ? R ^ (Q << i) : R
789
790 PV.X = X;
791 PV.Q = Q;
792 PV.R = R;
793 PV.Left = true;
794 return true;
795}
796
797bool PolynomialMultiplyRecognize::matchRightShift(SelectInst *SelI,
798 ParsedValues &PV) {
799 // Match the following:
800 // select (X & 1) != 0 ? (R >> 1) ^ Q : (R >> 1)
801 // select (X & 1) == 0 ? (R >> 1) : (R >> 1) ^ Q
802 // The condition may also check for equality with the masked value, i.e
803 // select (X & 1) == 1 ? (R >> 1) ^ Q : (R >> 1)
804 // select (X & 1) != 1 ? (R >> 1) : (R >> 1) ^ Q
805
806 Value *CondV = SelI->getCondition();
807 Value *TrueV = SelI->getTrueValue();
808 Value *FalseV = SelI->getFalseValue();
809
810 using namespace PatternMatch;
811
812 Value *C = nullptr;
814 bool TrueIfZero;
815
816 if (match(CondV, m_c_ICmp(P, m_Value(C), m_Zero()))) {
818 return false;
819 // Matched: select C == 0 ? ... : ...
820 // select C != 0 ? ... : ...
821 TrueIfZero = (P == CmpInst::ICMP_EQ);
822 } else if (match(CondV, m_c_ICmp(P, m_Value(C), m_One()))) {
824 return false;
825 // Matched: select C == 1 ? ... : ...
826 // select C != 1 ? ... : ...
827 TrueIfZero = (P == CmpInst::ICMP_NE);
828 } else
829 return false;
830
831 Value *X = nullptr;
832 if (!match(C, m_And(m_Value(X), m_One())))
833 return false;
834 // Matched: select (X & 1) == +++ ? ... : ...
835 // select (X & 1) != +++ ? ... : ...
836
837 Value *R = nullptr, *Q = nullptr;
838 if (TrueIfZero) {
839 // The select's condition is true if the tested bit is 0.
840 // TrueV must be the shift, FalseV must be the xor.
841 if (!match(TrueV, m_LShr(m_Value(R), m_One())))
842 return false;
843 // Matched: select +++ ? (R >> 1) : ...
844 if (!match(FalseV, m_c_Xor(m_Specific(TrueV), m_Value(Q))))
845 return false;
846 // Matched: select +++ ? (R >> 1) : (R >> 1) ^ Q
847 // with commuting ^.
848 } else {
849 // The select's condition is true if the tested bit is 1.
850 // TrueV must be the xor, FalseV must be the shift.
851 if (!match(FalseV, m_LShr(m_Value(R), m_One())))
852 return false;
853 // Matched: select +++ ? ... : (R >> 1)
854 if (!match(TrueV, m_c_Xor(m_Specific(FalseV), m_Value(Q))))
855 return false;
856 // Matched: select +++ ? (R >> 1) ^ Q : (R >> 1)
857 // with commuting ^.
858 }
859
860 PV.X = X;
861 PV.Q = Q;
862 PV.R = R;
863 PV.Left = false;
864 return true;
865}
866
867bool PolynomialMultiplyRecognize::scanSelect(SelectInst *SelI,
868 BasicBlock *LoopB, BasicBlock *PrehB, Value *CIV, ParsedValues &PV,
869 bool PreScan) {
870 using namespace PatternMatch;
871
872 // The basic pattern for R = P.Q is:
873 // for i = 0..31
874 // R = phi (0, R')
875 // if (P & (1 << i)) ; test-bit(P, i)
876 // R' = R ^ (Q << i)
877 //
878 // Similarly, the basic pattern for R = (P/Q).Q - P
879 // for i = 0..31
880 // R = phi(P, R')
881 // if (R & (1 << i))
882 // R' = R ^ (Q << i)
883
884 // There exist idioms, where instead of Q being shifted left, P is shifted
885 // right. This produces a result that is shifted right by 32 bits (the
886 // non-shifted result is 64-bit).
887 //
888 // For R = P.Q, this would be:
889 // for i = 0..31
890 // R = phi (0, R')
891 // if ((P >> i) & 1)
892 // R' = (R >> 1) ^ Q ; R is cycled through the loop, so it must
893 // else ; be shifted by 1, not i.
894 // R' = R >> 1
895 //
896 // And for the inverse:
897 // for i = 0..31
898 // R = phi (P, R')
899 // if (R & 1)
900 // R' = (R >> 1) ^ Q
901 // else
902 // R' = R >> 1
903
904 // The left-shifting idioms share the same pattern:
905 // select (X & (1 << i)) ? R ^ (Q << i) : R
906 // Similarly for right-shifting idioms:
907 // select (X & 1) ? (R >> 1) ^ Q
908
909 if (matchLeftShift(SelI, CIV, PV)) {
910 // If this is a pre-scan, getting this far is sufficient.
911 if (PreScan)
912 return true;
913
914 // Need to make sure that the SelI goes back into R.
915 auto *RPhi = dyn_cast<PHINode>(PV.R);
916 if (!RPhi)
917 return false;
918 if (SelI != RPhi->getIncomingValueForBlock(LoopB))
919 return false;
920 PV.Res = SelI;
921
922 // If X is loop invariant, it must be the input polynomial, and the
923 // idiom is the basic polynomial multiply.
924 if (CurLoop->isLoopInvariant(PV.X)) {
925 PV.P = PV.X;
926 PV.Inv = false;
927 } else {
928 // X is not loop invariant. If X == R, this is the inverse pmpy.
929 // Otherwise, check for an xor with an invariant value. If the
930 // variable argument to the xor is R, then this is still a valid
931 // inverse pmpy.
932 PV.Inv = true;
933 if (PV.X != PV.R) {
934 Value *Var = nullptr, *Inv = nullptr, *X1 = nullptr, *X2 = nullptr;
935 if (!match(PV.X, m_Xor(m_Value(X1), m_Value(X2))))
936 return false;
937 auto *I1 = dyn_cast<Instruction>(X1);
938 auto *I2 = dyn_cast<Instruction>(X2);
939 if (!I1 || I1->getParent() != LoopB) {
940 Var = X2;
941 Inv = X1;
942 } else if (!I2 || I2->getParent() != LoopB) {
943 Var = X1;
944 Inv = X2;
945 } else
946 return false;
947 if (Var != PV.R)
948 return false;
949 PV.M = Inv;
950 }
951 // The input polynomial P still needs to be determined. It will be
952 // the entry value of R.
953 Value *EntryP = RPhi->getIncomingValueForBlock(PrehB);
954 PV.P = EntryP;
955 }
956
957 return true;
958 }
959
960 if (matchRightShift(SelI, PV)) {
961 // If this is an inverse pattern, the Q polynomial must be known at
962 // compile time.
963 if (PV.Inv && !isa<ConstantInt>(PV.Q))
964 return false;
965 if (PreScan)
966 return true;
967 // There is no exact matching of right-shift pmpy.
968 return false;
969 }
970
971 return false;
972}
973
974bool PolynomialMultiplyRecognize::isPromotableTo(Value *Val,
975 IntegerType *DestTy) {
976 IntegerType *T = dyn_cast<IntegerType>(Val->getType());
977 if (!T || T->getBitWidth() > DestTy->getBitWidth())
978 return false;
979 if (T->getBitWidth() == DestTy->getBitWidth())
980 return true;
981 // Non-instructions are promotable. The reason why an instruction may not
982 // be promotable is that it may produce a different result if its operands
983 // and the result are promoted, for example, it may produce more non-zero
984 // bits. While it would still be possible to represent the proper result
985 // in a wider type, it may require adding additional instructions (which
986 // we don't want to do).
987 Instruction *In = dyn_cast<Instruction>(Val);
988 if (!In)
989 return true;
990 // The bitwidth of the source type is smaller than the destination.
991 // Check if the individual operation can be promoted.
992 switch (In->getOpcode()) {
993 case Instruction::PHI:
994 case Instruction::ZExt:
995 case Instruction::And:
996 case Instruction::Or:
997 case Instruction::Xor:
998 case Instruction::LShr: // Shift right is ok.
999 case Instruction::Select:
1000 case Instruction::Trunc:
1001 return true;
1002 case Instruction::ICmp:
1003 if (CmpInst *CI = cast<CmpInst>(In))
1004 return CI->isEquality() || CI->isUnsigned();
1005 llvm_unreachable("Cast failed unexpectedly");
1006 case Instruction::Add:
1007 return In->hasNoSignedWrap() && In->hasNoUnsignedWrap();
1008 }
1009 return false;
1010}
1011
1012void PolynomialMultiplyRecognize::promoteTo(Instruction *In,
1013 IntegerType *DestTy, BasicBlock *LoopB) {
1014 Type *OrigTy = In->getType();
1015 assert(!OrigTy->isVoidTy() && "Invalid instruction to promote");
1016
1017 // Leave boolean values alone.
1018 if (!In->getType()->isIntegerTy(1))
1019 In->mutateType(DestTy);
1020 unsigned DestBW = DestTy->getBitWidth();
1021
1022 // Handle PHIs.
1023 if (PHINode *P = dyn_cast<PHINode>(In)) {
1024 unsigned N = P->getNumIncomingValues();
1025 for (unsigned i = 0; i != N; ++i) {
1026 BasicBlock *InB = P->getIncomingBlock(i);
1027 if (InB == LoopB)
1028 continue;
1029 Value *InV = P->getIncomingValue(i);
1030 IntegerType *Ty = cast<IntegerType>(InV->getType());
1031 // Do not promote values in PHI nodes of type i1.
1032 if (Ty != P->getType()) {
1033 // If the value type does not match the PHI type, the PHI type
1034 // must have been promoted.
1035 assert(Ty->getBitWidth() < DestBW);
1036 InV = IRBuilder<>(InB->getTerminator()).CreateZExt(InV, DestTy);
1037 P->setIncomingValue(i, InV);
1038 }
1039 }
1040 } else if (ZExtInst *Z = dyn_cast<ZExtInst>(In)) {
1041 Value *Op = Z->getOperand(0);
1042 if (Op->getType() == Z->getType())
1043 Z->replaceAllUsesWith(Op);
1044 Z->eraseFromParent();
1045 return;
1046 }
1047 if (TruncInst *T = dyn_cast<TruncInst>(In)) {
1048 IntegerType *TruncTy = cast<IntegerType>(OrigTy);
1049 Value *Mask = ConstantInt::get(DestTy, (1u << TruncTy->getBitWidth()) - 1);
1050 Value *And = IRBuilder<>(In).CreateAnd(T->getOperand(0), Mask);
1051 T->replaceAllUsesWith(And);
1052 T->eraseFromParent();
1053 return;
1054 }
1055
1056 // Promote immediates.
1057 for (unsigned i = 0, n = In->getNumOperands(); i != n; ++i) {
1058 if (ConstantInt *CI = dyn_cast<ConstantInt>(In->getOperand(i)))
1059 if (CI->getBitWidth() < DestBW)
1060 In->setOperand(i, ConstantInt::get(DestTy, CI->getZExtValue()));
1061 }
1062}
1063
1064bool PolynomialMultiplyRecognize::promoteTypes(BasicBlock *LoopB,
1065 BasicBlock *ExitB) {
1066 assert(LoopB);
1067 // Skip loops where the exit block has more than one predecessor. The values
1068 // coming from the loop block will be promoted to another type, and so the
1069 // values coming into the exit block from other predecessors would also have
1070 // to be promoted.
1071 if (!ExitB || (ExitB->getSinglePredecessor() != LoopB))
1072 return false;
1073 IntegerType *DestTy = getPmpyType();
1074 // Check if the exit values have types that are no wider than the type
1075 // that we want to promote to.
1076 unsigned DestBW = DestTy->getBitWidth();
1077 for (PHINode &P : ExitB->phis()) {
1078 if (P.getNumIncomingValues() != 1)
1079 return false;
1080 assert(P.getIncomingBlock(0) == LoopB);
1081 IntegerType *T = dyn_cast<IntegerType>(P.getType());
1082 if (!T || T->getBitWidth() > DestBW)
1083 return false;
1084 }
1085
1086 // Check all instructions in the loop.
1087 for (Instruction &In : *LoopB)
1088 if (!In.isTerminator() && !isPromotableTo(&In, DestTy))
1089 return false;
1090
1091 // Perform the promotion.
1092 std::vector<Instruction*> LoopIns;
1093 std::transform(LoopB->begin(), LoopB->end(), std::back_inserter(LoopIns),
1094 [](Instruction &In) { return &In; });
1095 for (Instruction *In : LoopIns)
1096 if (!In->isTerminator())
1097 promoteTo(In, DestTy, LoopB);
1098
1099 // Fix up the PHI nodes in the exit block.
1100 Instruction *EndI = ExitB->getFirstNonPHI();
1101 BasicBlock::iterator End = EndI ? EndI->getIterator() : ExitB->end();
1102 for (auto I = ExitB->begin(); I != End; ++I) {
1103 PHINode *P = dyn_cast<PHINode>(I);
1104 if (!P)
1105 break;
1106 Type *Ty0 = P->getIncomingValue(0)->getType();
1107 Type *PTy = P->getType();
1108 if (PTy != Ty0) {
1109 assert(Ty0 == DestTy);
1110 // In order to create the trunc, P must have the promoted type.
1111 P->mutateType(Ty0);
1112 Value *T = IRBuilder<>(ExitB, End).CreateTrunc(P, PTy);
1113 // In order for the RAUW to work, the types of P and T must match.
1114 P->mutateType(PTy);
1115 P->replaceAllUsesWith(T);
1116 // Final update of the P's type.
1117 P->mutateType(Ty0);
1118 cast<Instruction>(T)->setOperand(0, P);
1119 }
1120 }
1121
1122 return true;
1123}
1124
1125bool PolynomialMultiplyRecognize::findCycle(Value *Out, Value *In,
1126 ValueSeq &Cycle) {
1127 // Out = ..., In, ...
1128 if (Out == In)
1129 return true;
1130
1131 auto *BB = cast<Instruction>(Out)->getParent();
1132 bool HadPhi = false;
1133
1134 for (auto *U : Out->users()) {
1135 auto *I = dyn_cast<Instruction>(&*U);
1136 if (I == nullptr || I->getParent() != BB)
1137 continue;
1138 // Make sure that there are no multi-iteration cycles, e.g.
1139 // p1 = phi(p2)
1140 // p2 = phi(p1)
1141 // The cycle p1->p2->p1 would span two loop iterations.
1142 // Check that there is only one phi in the cycle.
1143 bool IsPhi = isa<PHINode>(I);
1144 if (IsPhi && HadPhi)
1145 return false;
1146 HadPhi |= IsPhi;
1147 if (!Cycle.insert(I))
1148 return false;
1149 if (findCycle(I, In, Cycle))
1150 break;
1151 Cycle.remove(I);
1152 }
1153 return !Cycle.empty();
1154}
1155
1156void PolynomialMultiplyRecognize::classifyCycle(Instruction *DivI,
1157 ValueSeq &Cycle, ValueSeq &Early, ValueSeq &Late) {
1158 // All the values in the cycle that are between the phi node and the
1159 // divider instruction will be classified as "early", all other values
1160 // will be "late".
1161
1162 bool IsE = true;
1163 unsigned I, N = Cycle.size();
1164 for (I = 0; I < N; ++I) {
1165 Value *V = Cycle[I];
1166 if (DivI == V)
1167 IsE = false;
1168 else if (!isa<PHINode>(V))
1169 continue;
1170 // Stop if found either.
1171 break;
1172 }
1173 // "I" is the index of either DivI or the phi node, whichever was first.
1174 // "E" is "false" or "true" respectively.
1175 ValueSeq &First = !IsE ? Early : Late;
1176 for (unsigned J = 0; J < I; ++J)
1177 First.insert(Cycle[J]);
1178
1179 ValueSeq &Second = IsE ? Early : Late;
1180 Second.insert(Cycle[I]);
1181 for (++I; I < N; ++I) {
1182 Value *V = Cycle[I];
1183 if (DivI == V || isa<PHINode>(V))
1184 break;
1185 Second.insert(V);
1186 }
1187
1188 for (; I < N; ++I)
1189 First.insert(Cycle[I]);
1190}
1191
1192bool PolynomialMultiplyRecognize::classifyInst(Instruction *UseI,
1193 ValueSeq &Early, ValueSeq &Late) {
1194 // Select is an exception, since the condition value does not have to be
1195 // classified in the same way as the true/false values. The true/false
1196 // values do have to be both early or both late.
1197 if (UseI->getOpcode() == Instruction::Select) {
1198 Value *TV = UseI->getOperand(1), *FV = UseI->getOperand(2);
1199 if (Early.count(TV) || Early.count(FV)) {
1200 if (Late.count(TV) || Late.count(FV))
1201 return false;
1202 Early.insert(UseI);
1203 } else if (Late.count(TV) || Late.count(FV)) {
1204 if (Early.count(TV) || Early.count(FV))
1205 return false;
1206 Late.insert(UseI);
1207 }
1208 return true;
1209 }
1210
1211 // Not sure what would be the example of this, but the code below relies
1212 // on having at least one operand.
1213 if (UseI->getNumOperands() == 0)
1214 return true;
1215
1216 bool AE = true, AL = true;
1217 for (auto &I : UseI->operands()) {
1218 if (Early.count(&*I))
1219 AL = false;
1220 else if (Late.count(&*I))
1221 AE = false;
1222 }
1223 // If the operands appear "all early" and "all late" at the same time,
1224 // then it means that none of them are actually classified as either.
1225 // This is harmless.
1226 if (AE && AL)
1227 return true;
1228 // Conversely, if they are neither "all early" nor "all late", then
1229 // we have a mixture of early and late operands that is not a known
1230 // exception.
1231 if (!AE && !AL)
1232 return false;
1233
1234 // Check that we have covered the two special cases.
1235 assert(AE != AL);
1236
1237 if (AE)
1238 Early.insert(UseI);
1239 else
1240 Late.insert(UseI);
1241 return true;
1242}
1243
1244bool PolynomialMultiplyRecognize::commutesWithShift(Instruction *I) {
1245 switch (I->getOpcode()) {
1246 case Instruction::And:
1247 case Instruction::Or:
1248 case Instruction::Xor:
1249 case Instruction::LShr:
1250 case Instruction::Shl:
1251 case Instruction::Select:
1252 case Instruction::ICmp:
1253 case Instruction::PHI:
1254 break;
1255 default:
1256 return false;
1257 }
1258 return true;
1259}
1260
1261bool PolynomialMultiplyRecognize::highBitsAreZero(Value *V,
1262 unsigned IterCount) {
1263 auto *T = dyn_cast<IntegerType>(V->getType());
1264 if (!T)
1265 return false;
1266
1267 KnownBits Known(T->getBitWidth());
1268 computeKnownBits(V, Known, DL);
1269 return Known.countMinLeadingZeros() >= IterCount;
1270}
1271
1272bool PolynomialMultiplyRecognize::keepsHighBitsZero(Value *V,
1273 unsigned IterCount) {
1274 // Assume that all inputs to the value have the high bits zero.
1275 // Check if the value itself preserves the zeros in the high bits.
1276 if (auto *C = dyn_cast<ConstantInt>(V))
1277 return C->getValue().countl_zero() >= IterCount;
1278
1279 if (auto *I = dyn_cast<Instruction>(V)) {
1280 switch (I->getOpcode()) {
1281 case Instruction::And:
1282 case Instruction::Or:
1283 case Instruction::Xor:
1284 case Instruction::LShr:
1285 case Instruction::Select:
1286 case Instruction::ICmp:
1287 case Instruction::PHI:
1288 case Instruction::ZExt:
1289 return true;
1290 }
1291 }
1292
1293 return false;
1294}
1295
1296bool PolynomialMultiplyRecognize::isOperandShifted(Instruction *I, Value *Op) {
1297 unsigned Opc = I->getOpcode();
1298 if (Opc == Instruction::Shl || Opc == Instruction::LShr)
1299 return Op != I->getOperand(1);
1300 return true;
1301}
1302
1303bool PolynomialMultiplyRecognize::convertShiftsToLeft(BasicBlock *LoopB,
1304 BasicBlock *ExitB, unsigned IterCount) {
1305 Value *CIV = getCountIV(LoopB);
1306 if (CIV == nullptr)
1307 return false;
1308 auto *CIVTy = dyn_cast<IntegerType>(CIV->getType());
1309 if (CIVTy == nullptr)
1310 return false;
1311
1312 ValueSeq RShifts;
1313 ValueSeq Early, Late, Cycled;
1314
1315 // Find all value cycles that contain logical right shifts by 1.
1316 for (Instruction &I : *LoopB) {
1317 using namespace PatternMatch;
1318
1319 Value *V = nullptr;
1320 if (!match(&I, m_LShr(m_Value(V), m_One())))
1321 continue;
1322 ValueSeq C;
1323 if (!findCycle(&I, V, C))
1324 continue;
1325
1326 // Found a cycle.
1327 C.insert(&I);
1328 classifyCycle(&I, C, Early, Late);
1329 Cycled.insert(C.begin(), C.end());
1330 RShifts.insert(&I);
1331 }
1332
1333 // Find the set of all values affected by the shift cycles, i.e. all
1334 // cycled values, and (recursively) all their users.
1335 ValueSeq Users(Cycled.begin(), Cycled.end());
1336 for (unsigned i = 0; i < Users.size(); ++i) {
1337 Value *V = Users[i];
1338 if (!isa<IntegerType>(V->getType()))
1339 return false;
1340 auto *R = cast<Instruction>(V);
1341 // If the instruction does not commute with shifts, the loop cannot
1342 // be unshifted.
1343 if (!commutesWithShift(R))
1344 return false;
1345 for (User *U : R->users()) {
1346 auto *T = cast<Instruction>(U);
1347 // Skip users from outside of the loop. They will be handled later.
1348 // Also, skip the right-shifts and phi nodes, since they mix early
1349 // and late values.
1350 if (T->getParent() != LoopB || RShifts.count(T) || isa<PHINode>(T))
1351 continue;
1352
1353 Users.insert(T);
1354 if (!classifyInst(T, Early, Late))
1355 return false;
1356 }
1357 }
1358
1359 if (Users.empty())
1360 return false;
1361
1362 // Verify that high bits remain zero.
1363 ValueSeq Internal(Users.begin(), Users.end());
1364 ValueSeq Inputs;
1365 for (unsigned i = 0; i < Internal.size(); ++i) {
1366 auto *R = dyn_cast<Instruction>(Internal[i]);
1367 if (!R)
1368 continue;
1369 for (Value *Op : R->operands()) {
1370 auto *T = dyn_cast<Instruction>(Op);
1371 if (T && T->getParent() != LoopB)
1372 Inputs.insert(Op);
1373 else
1374 Internal.insert(Op);
1375 }
1376 }
1377 for (Value *V : Inputs)
1378 if (!highBitsAreZero(V, IterCount))
1379 return false;
1380 for (Value *V : Internal)
1381 if (!keepsHighBitsZero(V, IterCount))
1382 return false;
1383
1384 // Finally, the work can be done. Unshift each user.
1385 IRBuilder<> IRB(LoopB);
1386 std::map<Value*,Value*> ShiftMap;
1387
1388 using CastMapType = std::map<std::pair<Value *, Type *>, Value *>;
1389
1390 CastMapType CastMap;
1391
1392 auto upcast = [] (CastMapType &CM, IRBuilder<> &IRB, Value *V,
1393 IntegerType *Ty) -> Value* {
1394 auto H = CM.find(std::make_pair(V, Ty));
1395 if (H != CM.end())
1396 return H->second;
1397 Value *CV = IRB.CreateIntCast(V, Ty, false);
1398 CM.insert(std::make_pair(std::make_pair(V, Ty), CV));
1399 return CV;
1400 };
1401
1402 for (auto I = LoopB->begin(), E = LoopB->end(); I != E; ++I) {
1403 using namespace PatternMatch;
1404
1405 if (isa<PHINode>(I) || !Users.count(&*I))
1406 continue;
1407
1408 // Match lshr x, 1.
1409 Value *V = nullptr;
1410 if (match(&*I, m_LShr(m_Value(V), m_One()))) {
1411 replaceAllUsesOfWithIn(&*I, V, LoopB);
1412 continue;
1413 }
1414 // For each non-cycled operand, replace it with the corresponding
1415 // value shifted left.
1416 for (auto &J : I->operands()) {
1417 Value *Op = J.get();
1418 if (!isOperandShifted(&*I, Op))
1419 continue;
1420 if (Users.count(Op))
1421 continue;
1422 // Skip shifting zeros.
1423 if (isa<ConstantInt>(Op) && cast<ConstantInt>(Op)->isZero())
1424 continue;
1425 // Check if we have already generated a shift for this value.
1426 auto F = ShiftMap.find(Op);
1427 Value *W = (F != ShiftMap.end()) ? F->second : nullptr;
1428 if (W == nullptr) {
1429 IRB.SetInsertPoint(&*I);
1430 // First, the shift amount will be CIV or CIV+1, depending on
1431 // whether the value is early or late. Instead of creating CIV+1,
1432 // do a single shift of the value.
1433 Value *ShAmt = CIV, *ShVal = Op;
1434 auto *VTy = cast<IntegerType>(ShVal->getType());
1435 auto *ATy = cast<IntegerType>(ShAmt->getType());
1436 if (Late.count(&*I))
1437 ShVal = IRB.CreateShl(Op, ConstantInt::get(VTy, 1));
1438 // Second, the types of the shifted value and the shift amount
1439 // must match.
1440 if (VTy != ATy) {
1441 if (VTy->getBitWidth() < ATy->getBitWidth())
1442 ShVal = upcast(CastMap, IRB, ShVal, ATy);
1443 else
1444 ShAmt = upcast(CastMap, IRB, ShAmt, VTy);
1445 }
1446 // Ready to generate the shift and memoize it.
1447 W = IRB.CreateShl(ShVal, ShAmt);
1448 ShiftMap.insert(std::make_pair(Op, W));
1449 }
1450 I->replaceUsesOfWith(Op, W);
1451 }
1452 }
1453
1454 // Update the users outside of the loop to account for having left
1455 // shifts. They would normally be shifted right in the loop, so shift
1456 // them right after the loop exit.
1457 // Take advantage of the loop-closed SSA form, which has all the post-
1458 // loop values in phi nodes.
1459 IRB.SetInsertPoint(ExitB, ExitB->getFirstInsertionPt());
1460 for (auto P = ExitB->begin(), Q = ExitB->end(); P != Q; ++P) {
1461 if (!isa<PHINode>(P))
1462 break;
1463 auto *PN = cast<PHINode>(P);
1464 Value *U = PN->getIncomingValueForBlock(LoopB);
1465 if (!Users.count(U))
1466 continue;
1467 Value *S = IRB.CreateLShr(PN, ConstantInt::get(PN->getType(), IterCount));
1468 PN->replaceAllUsesWith(S);
1469 // The above RAUW will create
1470 // S = lshr S, IterCount
1471 // so we need to fix it back into
1472 // S = lshr PN, IterCount
1473 cast<User>(S)->replaceUsesOfWith(S, PN);
1474 }
1475
1476 return true;
1477}
1478
1479void PolynomialMultiplyRecognize::cleanupLoopBody(BasicBlock *LoopB) {
1480 for (auto &I : *LoopB)
1481 if (Value *SV = simplifyInstruction(&I, {DL, &TLI, &DT}))
1482 I.replaceAllUsesWith(SV);
1483
1486}
1487
1488unsigned PolynomialMultiplyRecognize::getInverseMxN(unsigned QP) {
1489 // Arrays of coefficients of Q and the inverse, C.
1490 // Q[i] = coefficient at x^i.
1491 std::array<char,32> Q, C;
1492
1493 for (unsigned i = 0; i < 32; ++i) {
1494 Q[i] = QP & 1;
1495 QP >>= 1;
1496 }
1497 assert(Q[0] == 1);
1498
1499 // Find C, such that
1500 // (Q[n]*x^n + ... + Q[1]*x + Q[0]) * (C[n]*x^n + ... + C[1]*x + C[0]) = 1
1501 //
1502 // For it to have a solution, Q[0] must be 1. Since this is Z2[x], the
1503 // operations * and + are & and ^ respectively.
1504 //
1505 // Find C[i] recursively, by comparing i-th coefficient in the product
1506 // with 0 (or 1 for i=0).
1507 //
1508 // C[0] = 1, since C[0] = Q[0], and Q[0] = 1.
1509 C[0] = 1;
1510 for (unsigned i = 1; i < 32; ++i) {
1511 // Solve for C[i] in:
1512 // C[0]Q[i] ^ C[1]Q[i-1] ^ ... ^ C[i-1]Q[1] ^ C[i]Q[0] = 0
1513 // This is equivalent to
1514 // C[0]Q[i] ^ C[1]Q[i-1] ^ ... ^ C[i-1]Q[1] ^ C[i] = 0
1515 // which is
1516 // C[0]Q[i] ^ C[1]Q[i-1] ^ ... ^ C[i-1]Q[1] = C[i]
1517 unsigned T = 0;
1518 for (unsigned j = 0; j < i; ++j)
1519 T = T ^ (C[j] & Q[i-j]);
1520 C[i] = T;
1521 }
1522
1523 unsigned QV = 0;
1524 for (unsigned i = 0; i < 32; ++i)
1525 if (C[i])
1526 QV |= (1 << i);
1527
1528 return QV;
1529}
1530
1531Value *PolynomialMultiplyRecognize::generate(BasicBlock::iterator At,
1532 ParsedValues &PV) {
1533 IRBuilder<> B(&*At);
1534 Module *M = At->getParent()->getParent()->getParent();
1535 Function *PMF =
1536 Intrinsic::getOrInsertDeclaration(M, Intrinsic::hexagon_M4_pmpyw);
1537
1538 Value *P = PV.P, *Q = PV.Q, *P0 = P;
1539 unsigned IC = PV.IterCount;
1540
1541 if (PV.M != nullptr)
1542 P0 = P = B.CreateXor(P, PV.M);
1543
1544 // Create a bit mask to clear the high bits beyond IterCount.
1545 auto *BMI = ConstantInt::get(P->getType(), APInt::getLowBitsSet(32, IC));
1546
1547 if (PV.IterCount != 32)
1548 P = B.CreateAnd(P, BMI);
1549
1550 if (PV.Inv) {
1551 auto *QI = dyn_cast<ConstantInt>(PV.Q);
1552 assert(QI && QI->getBitWidth() <= 32);
1553
1554 // Again, clearing bits beyond IterCount.
1555 unsigned M = (1 << PV.IterCount) - 1;
1556 unsigned Tmp = (QI->getZExtValue() | 1) & M;
1557 unsigned QV = getInverseMxN(Tmp) & M;
1558 auto *QVI = ConstantInt::get(QI->getType(), QV);
1559 P = B.CreateCall(PMF, {P, QVI});
1560 P = B.CreateTrunc(P, QI->getType());
1561 if (IC != 32)
1562 P = B.CreateAnd(P, BMI);
1563 }
1564
1565 Value *R = B.CreateCall(PMF, {P, Q});
1566
1567 if (PV.M != nullptr)
1568 R = B.CreateXor(R, B.CreateIntCast(P0, R->getType(), false));
1569
1570 return R;
1571}
1572
1573static bool hasZeroSignBit(const Value *V) {
1574 if (const auto *CI = dyn_cast<const ConstantInt>(V))
1575 return CI->getValue().isNonNegative();
1576 const Instruction *I = dyn_cast<const Instruction>(V);
1577 if (!I)
1578 return false;
1579 switch (I->getOpcode()) {
1580 case Instruction::LShr:
1581 if (const auto SI = dyn_cast<const ConstantInt>(I->getOperand(1)))
1582 return SI->getZExtValue() > 0;
1583 return false;
1584 case Instruction::Or:
1585 case Instruction::Xor:
1586 return hasZeroSignBit(I->getOperand(0)) &&
1587 hasZeroSignBit(I->getOperand(1));
1588 case Instruction::And:
1589 return hasZeroSignBit(I->getOperand(0)) ||
1590 hasZeroSignBit(I->getOperand(1));
1591 }
1592 return false;
1593}
1594
1595void PolynomialMultiplyRecognize::setupPreSimplifier(Simplifier &S) {
1596 S.addRule("sink-zext",
1597 // Sink zext past bitwise operations.
1598 [](Instruction *I, LLVMContext &Ctx) -> Value* {
1599 if (I->getOpcode() != Instruction::ZExt)
1600 return nullptr;
1601 Instruction *T = dyn_cast<Instruction>(I->getOperand(0));
1602 if (!T)
1603 return nullptr;
1604 switch (T->getOpcode()) {
1605 case Instruction::And:
1606 case Instruction::Or:
1607 case Instruction::Xor:
1608 break;
1609 default:
1610 return nullptr;
1611 }
1612 IRBuilder<> B(Ctx);
1613 return B.CreateBinOp(cast<BinaryOperator>(T)->getOpcode(),
1614 B.CreateZExt(T->getOperand(0), I->getType()),
1615 B.CreateZExt(T->getOperand(1), I->getType()));
1616 });
1617 S.addRule("xor/and -> and/xor",
1618 // (xor (and x a) (and y a)) -> (and (xor x y) a)
1619 [](Instruction *I, LLVMContext &Ctx) -> Value* {
1620 if (I->getOpcode() != Instruction::Xor)
1621 return nullptr;
1622 Instruction *And0 = dyn_cast<Instruction>(I->getOperand(0));
1623 Instruction *And1 = dyn_cast<Instruction>(I->getOperand(1));
1624 if (!And0 || !And1)
1625 return nullptr;
1626 if (And0->getOpcode() != Instruction::And ||
1627 And1->getOpcode() != Instruction::And)
1628 return nullptr;
1629 if (And0->getOperand(1) != And1->getOperand(1))
1630 return nullptr;
1631 IRBuilder<> B(Ctx);
1632 return B.CreateAnd(B.CreateXor(And0->getOperand(0), And1->getOperand(0)),
1633 And0->getOperand(1));
1634 });
1635 S.addRule("sink binop into select",
1636 // (Op (select c x y) z) -> (select c (Op x z) (Op y z))
1637 // (Op x (select c y z)) -> (select c (Op x y) (Op x z))
1638 [](Instruction *I, LLVMContext &Ctx) -> Value* {
1639 BinaryOperator *BO = dyn_cast<BinaryOperator>(I);
1640 if (!BO)
1641 return nullptr;
1643 if (SelectInst *Sel = dyn_cast<SelectInst>(BO->getOperand(0))) {
1644 IRBuilder<> B(Ctx);
1645 Value *X = Sel->getTrueValue(), *Y = Sel->getFalseValue();
1646 Value *Z = BO->getOperand(1);
1647 return B.CreateSelect(Sel->getCondition(),
1648 B.CreateBinOp(Op, X, Z),
1649 B.CreateBinOp(Op, Y, Z));
1650 }
1651 if (SelectInst *Sel = dyn_cast<SelectInst>(BO->getOperand(1))) {
1652 IRBuilder<> B(Ctx);
1653 Value *X = BO->getOperand(0);
1654 Value *Y = Sel->getTrueValue(), *Z = Sel->getFalseValue();
1655 return B.CreateSelect(Sel->getCondition(),
1656 B.CreateBinOp(Op, X, Y),
1657 B.CreateBinOp(Op, X, Z));
1658 }
1659 return nullptr;
1660 });
1661 S.addRule("fold select-select",
1662 // (select c (select c x y) z) -> (select c x z)
1663 // (select c x (select c y z)) -> (select c x z)
1664 [](Instruction *I, LLVMContext &Ctx) -> Value* {
1665 SelectInst *Sel = dyn_cast<SelectInst>(I);
1666 if (!Sel)
1667 return nullptr;
1668 IRBuilder<> B(Ctx);
1669 Value *C = Sel->getCondition();
1670 if (SelectInst *Sel0 = dyn_cast<SelectInst>(Sel->getTrueValue())) {
1671 if (Sel0->getCondition() == C)
1672 return B.CreateSelect(C, Sel0->getTrueValue(), Sel->getFalseValue());
1673 }
1674 if (SelectInst *Sel1 = dyn_cast<SelectInst>(Sel->getFalseValue())) {
1675 if (Sel1->getCondition() == C)
1676 return B.CreateSelect(C, Sel->getTrueValue(), Sel1->getFalseValue());
1677 }
1678 return nullptr;
1679 });
1680 S.addRule("or-signbit -> xor-signbit",
1681 // (or (lshr x 1) 0x800.0) -> (xor (lshr x 1) 0x800.0)
1682 [](Instruction *I, LLVMContext &Ctx) -> Value* {
1683 if (I->getOpcode() != Instruction::Or)
1684 return nullptr;
1685 ConstantInt *Msb = dyn_cast<ConstantInt>(I->getOperand(1));
1686 if (!Msb || !Msb->getValue().isSignMask())
1687 return nullptr;
1688 if (!hasZeroSignBit(I->getOperand(0)))
1689 return nullptr;
1690 return IRBuilder<>(Ctx).CreateXor(I->getOperand(0), Msb);
1691 });
1692 S.addRule("sink lshr into binop",
1693 // (lshr (BitOp x y) c) -> (BitOp (lshr x c) (lshr y c))
1694 [](Instruction *I, LLVMContext &Ctx) -> Value* {
1695 if (I->getOpcode() != Instruction::LShr)
1696 return nullptr;
1697 BinaryOperator *BitOp = dyn_cast<BinaryOperator>(I->getOperand(0));
1698 if (!BitOp)
1699 return nullptr;
1700 switch (BitOp->getOpcode()) {
1701 case Instruction::And:
1702 case Instruction::Or:
1703 case Instruction::Xor:
1704 break;
1705 default:
1706 return nullptr;
1707 }
1708 IRBuilder<> B(Ctx);
1709 Value *S = I->getOperand(1);
1710 return B.CreateBinOp(BitOp->getOpcode(),
1711 B.CreateLShr(BitOp->getOperand(0), S),
1712 B.CreateLShr(BitOp->getOperand(1), S));
1713 });
1714 S.addRule("expose bitop-const",
1715 // (BitOp1 (BitOp2 x a) b) -> (BitOp2 x (BitOp1 a b))
1716 [](Instruction *I, LLVMContext &Ctx) -> Value* {
1717 auto IsBitOp = [](unsigned Op) -> bool {
1718 switch (Op) {
1719 case Instruction::And:
1720 case Instruction::Or:
1721 case Instruction::Xor:
1722 return true;
1723 }
1724 return false;
1725 };
1726 BinaryOperator *BitOp1 = dyn_cast<BinaryOperator>(I);
1727 if (!BitOp1 || !IsBitOp(BitOp1->getOpcode()))
1728 return nullptr;
1729 BinaryOperator *BitOp2 = dyn_cast<BinaryOperator>(BitOp1->getOperand(0));
1730 if (!BitOp2 || !IsBitOp(BitOp2->getOpcode()))
1731 return nullptr;
1732 ConstantInt *CA = dyn_cast<ConstantInt>(BitOp2->getOperand(1));
1733 ConstantInt *CB = dyn_cast<ConstantInt>(BitOp1->getOperand(1));
1734 if (!CA || !CB)
1735 return nullptr;
1736 IRBuilder<> B(Ctx);
1737 Value *X = BitOp2->getOperand(0);
1738 return B.CreateBinOp(BitOp2->getOpcode(), X,
1739 B.CreateBinOp(BitOp1->getOpcode(), CA, CB));
1740 });
1741}
1742
1743void PolynomialMultiplyRecognize::setupPostSimplifier(Simplifier &S) {
1744 S.addRule("(and (xor (and x a) y) b) -> (and (xor x y) b), if b == b&a",
1745 [](Instruction *I, LLVMContext &Ctx) -> Value* {
1746 if (I->getOpcode() != Instruction::And)
1747 return nullptr;
1748 Instruction *Xor = dyn_cast<Instruction>(I->getOperand(0));
1749 ConstantInt *C0 = dyn_cast<ConstantInt>(I->getOperand(1));
1750 if (!Xor || !C0)
1751 return nullptr;
1752 if (Xor->getOpcode() != Instruction::Xor)
1753 return nullptr;
1754 Instruction *And0 = dyn_cast<Instruction>(Xor->getOperand(0));
1755 Instruction *And1 = dyn_cast<Instruction>(Xor->getOperand(1));
1756 // Pick the first non-null and.
1757 if (!And0 || And0->getOpcode() != Instruction::And)
1758 std::swap(And0, And1);
1759 ConstantInt *C1 = dyn_cast<ConstantInt>(And0->getOperand(1));
1760 if (!C1)
1761 return nullptr;
1762 uint32_t V0 = C0->getZExtValue();
1763 uint32_t V1 = C1->getZExtValue();
1764 if (V0 != (V0 & V1))
1765 return nullptr;
1766 IRBuilder<> B(Ctx);
1767 return B.CreateAnd(B.CreateXor(And0->getOperand(0), And1), C0);
1768 });
1769}
1770
1771bool PolynomialMultiplyRecognize::recognize() {
1772 LLVM_DEBUG(dbgs() << "Starting PolynomialMultiplyRecognize on loop\n"
1773 << *CurLoop << '\n');
1774 // Restrictions:
1775 // - The loop must consist of a single block.
1776 // - The iteration count must be known at compile-time.
1777 // - The loop must have an induction variable starting from 0, and
1778 // incremented in each iteration of the loop.
1779 BasicBlock *LoopB = CurLoop->getHeader();
1780 LLVM_DEBUG(dbgs() << "Loop header:\n" << *LoopB);
1781
1782 if (LoopB != CurLoop->getLoopLatch())
1783 return false;
1784 BasicBlock *ExitB = CurLoop->getExitBlock();
1785 if (ExitB == nullptr)
1786 return false;
1787 BasicBlock *EntryB = CurLoop->getLoopPreheader();
1788 if (EntryB == nullptr)
1789 return false;
1790
1791 unsigned IterCount = 0;
1792 const SCEV *CT = SE.getBackedgeTakenCount(CurLoop);
1793 if (isa<SCEVCouldNotCompute>(CT))
1794 return false;
1795 if (auto *CV = dyn_cast<SCEVConstant>(CT))
1796 IterCount = CV->getValue()->getZExtValue() + 1;
1797
1798 Value *CIV = getCountIV(LoopB);
1799 ParsedValues PV;
1800 Simplifier PreSimp;
1801 PV.IterCount = IterCount;
1802 LLVM_DEBUG(dbgs() << "Loop IV: " << *CIV << "\nIterCount: " << IterCount
1803 << '\n');
1804
1805 setupPreSimplifier(PreSimp);
1806
1807 // Perform a preliminary scan of select instructions to see if any of them
1808 // looks like a generator of the polynomial multiply steps. Assume that a
1809 // loop can only contain a single transformable operation, so stop the
1810 // traversal after the first reasonable candidate was found.
1811 // XXX: Currently this approach can modify the loop before being 100% sure
1812 // that the transformation can be carried out.
1813 bool FoundPreScan = false;
1814 auto FeedsPHI = [LoopB](const Value *V) -> bool {
1815 for (const Value *U : V->users()) {
1816 if (const auto *P = dyn_cast<const PHINode>(U))
1817 if (P->getParent() == LoopB)
1818 return true;
1819 }
1820 return false;
1821 };
1822 for (Instruction &In : *LoopB) {
1823 SelectInst *SI = dyn_cast<SelectInst>(&In);
1824 if (!SI || !FeedsPHI(SI))
1825 continue;
1826
1827 Simplifier::Context C(SI);
1828 Value *T = PreSimp.simplify(C);
1829 SelectInst *SelI = (T && isa<SelectInst>(T)) ? cast<SelectInst>(T) : SI;
1830 LLVM_DEBUG(dbgs() << "scanSelect(pre-scan): " << PE(C, SelI) << '\n');
1831 if (scanSelect(SelI, LoopB, EntryB, CIV, PV, true)) {
1832 FoundPreScan = true;
1833 if (SelI != SI) {
1834 Value *NewSel = C.materialize(LoopB, SI->getIterator());
1835 SI->replaceAllUsesWith(NewSel);
1837 }
1838 break;
1839 }
1840 }
1841
1842 if (!FoundPreScan) {
1843 LLVM_DEBUG(dbgs() << "Have not found candidates for pmpy\n");
1844 return false;
1845 }
1846
1847 if (!PV.Left) {
1848 // The right shift version actually only returns the higher bits of
1849 // the result (each iteration discards the LSB). If we want to convert it
1850 // to a left-shifting loop, the working data type must be at least as
1851 // wide as the target's pmpy instruction.
1852 if (!promoteTypes(LoopB, ExitB))
1853 return false;
1854 // Run post-promotion simplifications.
1855 Simplifier PostSimp;
1856 setupPostSimplifier(PostSimp);
1857 for (Instruction &In : *LoopB) {
1858 SelectInst *SI = dyn_cast<SelectInst>(&In);
1859 if (!SI || !FeedsPHI(SI))
1860 continue;
1861 Simplifier::Context C(SI);
1862 Value *T = PostSimp.simplify(C);
1863 SelectInst *SelI = dyn_cast_or_null<SelectInst>(T);
1864 if (SelI != SI) {
1865 Value *NewSel = C.materialize(LoopB, SI->getIterator());
1866 SI->replaceAllUsesWith(NewSel);
1868 }
1869 break;
1870 }
1871
1872 if (!convertShiftsToLeft(LoopB, ExitB, IterCount))
1873 return false;
1874 cleanupLoopBody(LoopB);
1875 }
1876
1877 // Scan the loop again, find the generating select instruction.
1878 bool FoundScan = false;
1879 for (Instruction &In : *LoopB) {
1880 SelectInst *SelI = dyn_cast<SelectInst>(&In);
1881 if (!SelI)
1882 continue;
1883 LLVM_DEBUG(dbgs() << "scanSelect: " << *SelI << '\n');
1884 FoundScan = scanSelect(SelI, LoopB, EntryB, CIV, PV, false);
1885 if (FoundScan)
1886 break;
1887 }
1888 assert(FoundScan);
1889
1890 LLVM_DEBUG({
1891 StringRef PP = (PV.M ? "(P+M)" : "P");
1892 if (!PV.Inv)
1893 dbgs() << "Found pmpy idiom: R = " << PP << ".Q\n";
1894 else
1895 dbgs() << "Found inverse pmpy idiom: R = (" << PP << "/Q).Q) + "
1896 << PP << "\n";
1897 dbgs() << " Res:" << *PV.Res << "\n P:" << *PV.P << "\n";
1898 if (PV.M)
1899 dbgs() << " M:" << *PV.M << "\n";
1900 dbgs() << " Q:" << *PV.Q << "\n";
1901 dbgs() << " Iteration count:" << PV.IterCount << "\n";
1902 });
1903
1904 BasicBlock::iterator At(EntryB->getTerminator());
1905 Value *PM = generate(At, PV);
1906 if (PM == nullptr)
1907 return false;
1908
1909 if (PM->getType() != PV.Res->getType())
1910 PM = IRBuilder<>(&*At).CreateIntCast(PM, PV.Res->getType(), false);
1911
1912 PV.Res->replaceAllUsesWith(PM);
1913 PV.Res->eraseFromParent();
1914 return true;
1915}
1916
1917int HexagonLoopIdiomRecognize::getSCEVStride(const SCEVAddRecExpr *S) {
1918 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(S->getOperand(1)))
1919 return SC->getAPInt().getSExtValue();
1920 return 0;
1921}
1922
1923bool HexagonLoopIdiomRecognize::isLegalStore(Loop *CurLoop, StoreInst *SI) {
1924 // Allow volatile stores if HexagonVolatileMemcpy is enabled.
1925 if (!(SI->isVolatile() && HexagonVolatileMemcpy) && !SI->isSimple())
1926 return false;
1927
1928 Value *StoredVal = SI->getValueOperand();
1929 Value *StorePtr = SI->getPointerOperand();
1930
1931 // Reject stores that are so large that they overflow an unsigned.
1932 uint64_t SizeInBits = DL->getTypeSizeInBits(StoredVal->getType());
1933 if ((SizeInBits & 7) || (SizeInBits >> 32) != 0)
1934 return false;
1935
1936 // See if the pointer expression is an AddRec like {base,+,1} on the current
1937 // loop, which indicates a strided store. If we have something else, it's a
1938 // random store we can't handle.
1939 auto *StoreEv = dyn_cast<SCEVAddRecExpr>(SE->getSCEV(StorePtr));
1940 if (!StoreEv || StoreEv->getLoop() != CurLoop || !StoreEv->isAffine())
1941 return false;
1942
1943 // Check to see if the stride matches the size of the store. If so, then we
1944 // know that every byte is touched in the loop.
1945 int Stride = getSCEVStride(StoreEv);
1946 if (Stride == 0)
1947 return false;
1948 unsigned StoreSize = DL->getTypeStoreSize(SI->getValueOperand()->getType());
1949 if (StoreSize != unsigned(std::abs(Stride)))
1950 return false;
1951
1952 // The store must be feeding a non-volatile load.
1953 LoadInst *LI = dyn_cast<LoadInst>(SI->getValueOperand());
1954 if (!LI || !LI->isSimple())
1955 return false;
1956
1957 // See if the pointer expression is an AddRec like {base,+,1} on the current
1958 // loop, which indicates a strided load. If we have something else, it's a
1959 // random load we can't handle.
1960 Value *LoadPtr = LI->getPointerOperand();
1961 auto *LoadEv = dyn_cast<SCEVAddRecExpr>(SE->getSCEV(LoadPtr));
1962 if (!LoadEv || LoadEv->getLoop() != CurLoop || !LoadEv->isAffine())
1963 return false;
1964
1965 // The store and load must share the same stride.
1966 if (StoreEv->getOperand(1) != LoadEv->getOperand(1))
1967 return false;
1968
1969 // Success. This store can be converted into a memcpy.
1970 return true;
1971}
1972
1973/// mayLoopAccessLocation - Return true if the specified loop might access the
1974/// specified pointer location, which is a loop-strided access. The 'Access'
1975/// argument specifies what the verboten forms of access are (read or write).
1976static bool
1978 const SCEV *BECount, unsigned StoreSize,
1979 AliasAnalysis &AA,
1981 // Get the location that may be stored across the loop. Since the access
1982 // is strided positively through memory, we say that the modified location
1983 // starts at the pointer and has infinite size.
1985
1986 // If the loop iterates a fixed number of times, we can refine the access
1987 // size to be exactly the size of the memset, which is (BECount+1)*StoreSize
1988 if (const SCEVConstant *BECst = dyn_cast<SCEVConstant>(BECount))
1989 AccessSize = LocationSize::precise((BECst->getValue()->getZExtValue() + 1) *
1990 StoreSize);
1991
1992 // TODO: For this to be really effective, we have to dive into the pointer
1993 // operand in the store. Store to &A[i] of 100 will always return may alias
1994 // with store of &A[100], we need to StoreLoc to be "A" with size of 100,
1995 // which will then no-alias a store to &A[100].
1996 MemoryLocation StoreLoc(Ptr, AccessSize);
1997
1998 for (auto *B : L->blocks())
1999 for (auto &I : *B)
2000 if (Ignored.count(&I) == 0 &&
2001 isModOrRefSet(AA.getModRefInfo(&I, StoreLoc) & Access))
2002 return true;
2003
2004 return false;
2005}
2006
2007void HexagonLoopIdiomRecognize::collectStores(Loop *CurLoop, BasicBlock *BB,
2009 Stores.clear();
2010 for (Instruction &I : *BB)
2011 if (StoreInst *SI = dyn_cast<StoreInst>(&I))
2012 if (isLegalStore(CurLoop, SI))
2013 Stores.push_back(SI);
2014}
2015
2016bool HexagonLoopIdiomRecognize::processCopyingStore(Loop *CurLoop,
2017 StoreInst *SI, const SCEV *BECount) {
2018 assert((SI->isSimple() || (SI->isVolatile() && HexagonVolatileMemcpy)) &&
2019 "Expected only non-volatile stores, or Hexagon-specific memcpy"
2020 "to volatile destination.");
2021
2022 Value *StorePtr = SI->getPointerOperand();
2023 auto *StoreEv = cast<SCEVAddRecExpr>(SE->getSCEV(StorePtr));
2024 unsigned Stride = getSCEVStride(StoreEv);
2025 unsigned StoreSize = DL->getTypeStoreSize(SI->getValueOperand()->getType());
2026 if (Stride != StoreSize)
2027 return false;
2028
2029 // See if the pointer expression is an AddRec like {base,+,1} on the current
2030 // loop, which indicates a strided load. If we have something else, it's a
2031 // random load we can't handle.
2032 auto *LI = cast<LoadInst>(SI->getValueOperand());
2033 auto *LoadEv = cast<SCEVAddRecExpr>(SE->getSCEV(LI->getPointerOperand()));
2034
2035 // The trip count of the loop and the base pointer of the addrec SCEV is
2036 // guaranteed to be loop invariant, which means that it should dominate the
2037 // header. This allows us to insert code for it in the preheader.
2038 BasicBlock *Preheader = CurLoop->getLoopPreheader();
2039 Instruction *ExpPt = Preheader->getTerminator();
2040 IRBuilder<> Builder(ExpPt);
2041 SCEVExpander Expander(*SE, *DL, "hexagon-loop-idiom");
2042
2043 Type *IntPtrTy = Builder.getIntPtrTy(*DL, SI->getPointerAddressSpace());
2044
2045 // Okay, we have a strided store "p[i]" of a loaded value. We can turn
2046 // this into a memcpy/memmove in the loop preheader now if we want. However,
2047 // this would be unsafe to do if there is anything else in the loop that may
2048 // read or write the memory region we're storing to. For memcpy, this
2049 // includes the load that feeds the stores. Check for an alias by generating
2050 // the base address and checking everything.
2051 Value *StoreBasePtr = Expander.expandCodeFor(StoreEv->getStart(),
2052 Builder.getPtrTy(SI->getPointerAddressSpace()), ExpPt);
2053 Value *LoadBasePtr = nullptr;
2054
2055 bool Overlap = false;
2056 bool DestVolatile = SI->isVolatile();
2057 Type *BECountTy = BECount->getType();
2058
2059 if (DestVolatile) {
2060 // The trip count must fit in i32, since it is the type of the "num_words"
2061 // argument to hexagon_memcpy_forward_vp4cp4n2.
2062 if (StoreSize != 4 || DL->getTypeSizeInBits(BECountTy) > 32) {
2063CleanupAndExit:
2064 // If we generated new code for the base pointer, clean up.
2065 Expander.clear();
2066 if (StoreBasePtr && (LoadBasePtr != StoreBasePtr)) {
2068 StoreBasePtr = nullptr;
2069 }
2070 if (LoadBasePtr) {
2072 LoadBasePtr = nullptr;
2073 }
2074 return false;
2075 }
2076 }
2077
2079 Ignore1.insert(SI);
2080 if (mayLoopAccessLocation(StoreBasePtr, ModRefInfo::ModRef, CurLoop, BECount,
2081 StoreSize, *AA, Ignore1)) {
2082 // Check if the load is the offending instruction.
2083 Ignore1.insert(LI);
2084 if (mayLoopAccessLocation(StoreBasePtr, ModRefInfo::ModRef, CurLoop,
2085 BECount, StoreSize, *AA, Ignore1)) {
2086 // Still bad. Nothing we can do.
2087 goto CleanupAndExit;
2088 }
2089 // It worked with the load ignored.
2090 Overlap = true;
2091 }
2092
2093 if (!Overlap) {
2094 if (DisableMemcpyIdiom || !HasMemcpy)
2095 goto CleanupAndExit;
2096 } else {
2097 // Don't generate memmove if this function will be inlined. This is
2098 // because the caller will undergo this transformation after inlining.
2099 Function *Func = CurLoop->getHeader()->getParent();
2100 if (Func->hasFnAttribute(Attribute::AlwaysInline))
2101 goto CleanupAndExit;
2102
2103 // In case of a memmove, the call to memmove will be executed instead
2104 // of the loop, so we need to make sure that there is nothing else in
2105 // the loop than the load, store and instructions that these two depend
2106 // on.
2108 Insts.push_back(SI);
2109 Insts.push_back(LI);
2110 if (!coverLoop(CurLoop, Insts))
2111 goto CleanupAndExit;
2112
2113 if (DisableMemmoveIdiom || !HasMemmove)
2114 goto CleanupAndExit;
2115 bool IsNested = CurLoop->getParentLoop() != nullptr;
2116 if (IsNested && OnlyNonNestedMemmove)
2117 goto CleanupAndExit;
2118 }
2119
2120 // For a memcpy, we have to make sure that the input array is not being
2121 // mutated by the loop.
2122 LoadBasePtr = Expander.expandCodeFor(LoadEv->getStart(),
2123 Builder.getPtrTy(LI->getPointerAddressSpace()), ExpPt);
2124
2126 Ignore2.insert(SI);
2127 if (mayLoopAccessLocation(LoadBasePtr, ModRefInfo::Mod, CurLoop, BECount,
2128 StoreSize, *AA, Ignore2))
2129 goto CleanupAndExit;
2130
2131 // Check the stride.
2132 bool StridePos = getSCEVStride(LoadEv) >= 0;
2133
2134 // Currently, the volatile memcpy only emulates traversing memory forward.
2135 if (!StridePos && DestVolatile)
2136 goto CleanupAndExit;
2137
2138 bool RuntimeCheck = (Overlap || DestVolatile);
2139
2140 BasicBlock *ExitB;
2141 if (RuntimeCheck) {
2142 // The runtime check needs a single exit block.
2143 SmallVector<BasicBlock*, 8> ExitBlocks;
2144 CurLoop->getUniqueExitBlocks(ExitBlocks);
2145 if (ExitBlocks.size() != 1)
2146 goto CleanupAndExit;
2147 ExitB = ExitBlocks[0];
2148 }
2149
2150 // The # stored bytes is (BECount+1)*Size. Expand the trip count out to
2151 // pointer size if it isn't already.
2152 LLVMContext &Ctx = SI->getContext();
2153 BECount = SE->getTruncateOrZeroExtend(BECount, IntPtrTy);
2154 DebugLoc DLoc = SI->getDebugLoc();
2155
2156 const SCEV *NumBytesS =
2157 SE->getAddExpr(BECount, SE->getOne(IntPtrTy), SCEV::FlagNUW);
2158 if (StoreSize != 1)
2159 NumBytesS = SE->getMulExpr(NumBytesS, SE->getConstant(IntPtrTy, StoreSize),
2161 Value *NumBytes = Expander.expandCodeFor(NumBytesS, IntPtrTy, ExpPt);
2162 if (Instruction *In = dyn_cast<Instruction>(NumBytes))
2163 if (Value *Simp = simplifyInstruction(In, {*DL, TLI, DT}))
2164 NumBytes = Simp;
2165
2166 CallInst *NewCall;
2167
2168 if (RuntimeCheck) {
2169 unsigned Threshold = RuntimeMemSizeThreshold;
2170 if (ConstantInt *CI = dyn_cast<ConstantInt>(NumBytes)) {
2171 uint64_t C = CI->getZExtValue();
2172 if (Threshold != 0 && C < Threshold)
2173 goto CleanupAndExit;
2175 goto CleanupAndExit;
2176 }
2177
2178 BasicBlock *Header = CurLoop->getHeader();
2179 Function *Func = Header->getParent();
2180 Loop *ParentL = LF->getLoopFor(Preheader);
2181 StringRef HeaderName = Header->getName();
2182
2183 // Create a new (empty) preheader, and update the PHI nodes in the
2184 // header to use the new preheader.
2185 BasicBlock *NewPreheader = BasicBlock::Create(Ctx, HeaderName+".rtli.ph",
2186 Func, Header);
2187 if (ParentL)
2188 ParentL->addBasicBlockToLoop(NewPreheader, *LF);
2189 IRBuilder<>(NewPreheader).CreateBr(Header);
2190 for (auto &In : *Header) {
2191 PHINode *PN = dyn_cast<PHINode>(&In);
2192 if (!PN)
2193 break;
2194 int bx = PN->getBasicBlockIndex(Preheader);
2195 if (bx >= 0)
2196 PN->setIncomingBlock(bx, NewPreheader);
2197 }
2198 DT->addNewBlock(NewPreheader, Preheader);
2199 DT->changeImmediateDominator(Header, NewPreheader);
2200
2201 // Check for safe conditions to execute memmove.
2202 // If stride is positive, copying things from higher to lower addresses
2203 // is equivalent to memmove. For negative stride, it's the other way
2204 // around. Copying forward in memory with positive stride may not be
2205 // same as memmove since we may be copying values that we just stored
2206 // in some previous iteration.
2207 Value *LA = Builder.CreatePtrToInt(LoadBasePtr, IntPtrTy);
2208 Value *SA = Builder.CreatePtrToInt(StoreBasePtr, IntPtrTy);
2209 Value *LowA = StridePos ? SA : LA;
2210 Value *HighA = StridePos ? LA : SA;
2211 Value *CmpA = Builder.CreateICmpULT(LowA, HighA);
2212 Value *Cond = CmpA;
2213
2214 // Check for distance between pointers. Since the case LowA < HighA
2215 // is checked for above, assume LowA >= HighA.
2216 Value *Dist = Builder.CreateSub(LowA, HighA);
2217 Value *CmpD = Builder.CreateICmpSLE(NumBytes, Dist);
2218 Value *CmpEither = Builder.CreateOr(Cond, CmpD);
2219 Cond = CmpEither;
2220
2221 if (Threshold != 0) {
2222 Type *Ty = NumBytes->getType();
2223 Value *Thr = ConstantInt::get(Ty, Threshold);
2224 Value *CmpB = Builder.CreateICmpULT(Thr, NumBytes);
2225 Value *CmpBoth = Builder.CreateAnd(Cond, CmpB);
2226 Cond = CmpBoth;
2227 }
2228 BasicBlock *MemmoveB = BasicBlock::Create(Ctx, Header->getName()+".rtli",
2229 Func, NewPreheader);
2230 if (ParentL)
2231 ParentL->addBasicBlockToLoop(MemmoveB, *LF);
2232 Instruction *OldT = Preheader->getTerminator();
2233 Builder.CreateCondBr(Cond, MemmoveB, NewPreheader);
2234 OldT->eraseFromParent();
2235 Preheader->setName(Preheader->getName()+".old");
2236 DT->addNewBlock(MemmoveB, Preheader);
2237 // Find the new immediate dominator of the exit block.
2238 BasicBlock *ExitD = Preheader;
2239 for (BasicBlock *PB : predecessors(ExitB)) {
2240 ExitD = DT->findNearestCommonDominator(ExitD, PB);
2241 if (!ExitD)
2242 break;
2243 }
2244 // If the prior immediate dominator of ExitB was dominated by the
2245 // old preheader, then the old preheader becomes the new immediate
2246 // dominator. Otherwise don't change anything (because the newly
2247 // added blocks are dominated by the old preheader).
2248 if (ExitD && DT->dominates(Preheader, ExitD)) {
2249 DomTreeNode *BN = DT->getNode(ExitB);
2250 DomTreeNode *DN = DT->getNode(ExitD);
2251 BN->setIDom(DN);
2252 }
2253
2254 // Add a call to memmove to the conditional block.
2255 IRBuilder<> CondBuilder(MemmoveB);
2256 CondBuilder.CreateBr(ExitB);
2257 CondBuilder.SetInsertPoint(MemmoveB->getTerminator());
2258
2259 if (DestVolatile) {
2260 Type *Int32Ty = Type::getInt32Ty(Ctx);
2261 Type *PtrTy = PointerType::get(Ctx, 0);
2262 Type *VoidTy = Type::getVoidTy(Ctx);
2263 Module *M = Func->getParent();
2264 FunctionCallee Fn = M->getOrInsertFunction(
2265 HexagonVolatileMemcpyName, VoidTy, PtrTy, PtrTy, Int32Ty);
2266
2267 const SCEV *OneS = SE->getConstant(Int32Ty, 1);
2268 const SCEV *BECount32 = SE->getTruncateOrZeroExtend(BECount, Int32Ty);
2269 const SCEV *NumWordsS = SE->getAddExpr(BECount32, OneS, SCEV::FlagNUW);
2270 Value *NumWords = Expander.expandCodeFor(NumWordsS, Int32Ty,
2271 MemmoveB->getTerminator());
2272 if (Instruction *In = dyn_cast<Instruction>(NumWords))
2273 if (Value *Simp = simplifyInstruction(In, {*DL, TLI, DT}))
2274 NumWords = Simp;
2275
2276 NewCall = CondBuilder.CreateCall(Fn,
2277 {StoreBasePtr, LoadBasePtr, NumWords});
2278 } else {
2279 NewCall = CondBuilder.CreateMemMove(
2280 StoreBasePtr, SI->getAlign(), LoadBasePtr, LI->getAlign(), NumBytes);
2281 }
2282 } else {
2283 NewCall = Builder.CreateMemCpy(StoreBasePtr, SI->getAlign(), LoadBasePtr,
2284 LI->getAlign(), NumBytes);
2285 // Okay, the memcpy has been formed. Zap the original store and
2286 // anything that feeds into it.
2288 }
2289
2290 NewCall->setDebugLoc(DLoc);
2291
2292 LLVM_DEBUG(dbgs() << " Formed " << (Overlap ? "memmove: " : "memcpy: ")
2293 << *NewCall << "\n"
2294 << " from load ptr=" << *LoadEv << " at: " << *LI << "\n"
2295 << " from store ptr=" << *StoreEv << " at: " << *SI
2296 << "\n");
2297
2298 return true;
2299}
2300
2301// Check if the instructions in Insts, together with their dependencies
2302// cover the loop in the sense that the loop could be safely eliminated once
2303// the instructions in Insts are removed.
2304bool HexagonLoopIdiomRecognize::coverLoop(Loop *L,
2305 SmallVectorImpl<Instruction*> &Insts) const {
2306 SmallSet<BasicBlock*,8> LoopBlocks;
2307 for (auto *B : L->blocks())
2308 LoopBlocks.insert(B);
2309
2310 SetVector<Instruction*> Worklist(Insts.begin(), Insts.end());
2311
2312 // Collect all instructions from the loop that the instructions in Insts
2313 // depend on (plus their dependencies, etc.). These instructions will
2314 // constitute the expression trees that feed those in Insts, but the trees
2315 // will be limited only to instructions contained in the loop.
2316 for (unsigned i = 0; i < Worklist.size(); ++i) {
2317 Instruction *In = Worklist[i];
2318 for (auto I = In->op_begin(), E = In->op_end(); I != E; ++I) {
2319 Instruction *OpI = dyn_cast<Instruction>(I);
2320 if (!OpI)
2321 continue;
2322 BasicBlock *PB = OpI->getParent();
2323 if (!LoopBlocks.count(PB))
2324 continue;
2325 Worklist.insert(OpI);
2326 }
2327 }
2328
2329 // Scan all instructions in the loop, if any of them have a user outside
2330 // of the loop, or outside of the expressions collected above, then either
2331 // the loop has a side-effect visible outside of it, or there are
2332 // instructions in it that are not involved in the original set Insts.
2333 for (auto *B : L->blocks()) {
2334 for (auto &In : *B) {
2335 if (isa<BranchInst>(In) || isa<DbgInfoIntrinsic>(In))
2336 continue;
2337 if (!Worklist.count(&In) && In.mayHaveSideEffects())
2338 return false;
2339 for (auto *K : In.users()) {
2340 Instruction *UseI = dyn_cast<Instruction>(K);
2341 if (!UseI)
2342 continue;
2343 BasicBlock *UseB = UseI->getParent();
2344 if (LF->getLoopFor(UseB) != L)
2345 return false;
2346 }
2347 }
2348 }
2349
2350 return true;
2351}
2352
2353/// runOnLoopBlock - Process the specified block, which lives in a counted loop
2354/// with the specified backedge count. This block is known to be in the current
2355/// loop and not in any subloops.
2356bool HexagonLoopIdiomRecognize::runOnLoopBlock(Loop *CurLoop, BasicBlock *BB,
2357 const SCEV *BECount, SmallVectorImpl<BasicBlock*> &ExitBlocks) {
2358 // We can only promote stores in this block if they are unconditionally
2359 // executed in the loop. For a block to be unconditionally executed, it has
2360 // to dominate all the exit blocks of the loop. Verify this now.
2361 auto DominatedByBB = [this,BB] (BasicBlock *EB) -> bool {
2362 return DT->dominates(BB, EB);
2363 };
2364 if (!all_of(ExitBlocks, DominatedByBB))
2365 return false;
2366
2367 bool MadeChange = false;
2368 // Look for store instructions, which may be optimized to memset/memcpy.
2370 collectStores(CurLoop, BB, Stores);
2371
2372 // Optimize the store into a memcpy, if it feeds an similarly strided load.
2373 for (auto &SI : Stores)
2374 MadeChange |= processCopyingStore(CurLoop, SI, BECount);
2375
2376 return MadeChange;
2377}
2378
2379bool HexagonLoopIdiomRecognize::runOnCountableLoop(Loop *L) {
2380 PolynomialMultiplyRecognize PMR(L, *DL, *DT, *TLI, *SE);
2381 if (PMR.recognize())
2382 return true;
2383
2384 if (!HasMemcpy && !HasMemmove)
2385 return false;
2386
2387 const SCEV *BECount = SE->getBackedgeTakenCount(L);
2388 assert(!isa<SCEVCouldNotCompute>(BECount) &&
2389 "runOnCountableLoop() called on a loop without a predictable"
2390 "backedge-taken count");
2391
2393 L->getUniqueExitBlocks(ExitBlocks);
2394
2395 bool Changed = false;
2396
2397 // Scan all the blocks in the loop that are not in subloops.
2398 for (auto *BB : L->getBlocks()) {
2399 // Ignore blocks in subloops.
2400 if (LF->getLoopFor(BB) != L)
2401 continue;
2402 Changed |= runOnLoopBlock(L, BB, BECount, ExitBlocks);
2403 }
2404
2405 return Changed;
2406}
2407
2408bool HexagonLoopIdiomRecognize::run(Loop *L) {
2409 const Module &M = *L->getHeader()->getParent()->getParent();
2410 if (Triple(M.getTargetTriple()).getArch() != Triple::hexagon)
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 DL = &L->getHeader()->getDataLayout();
2424
2425 HasMemcpy = TLI->has(LibFunc_memcpy);
2426 HasMemmove = TLI->has(LibFunc_memmove);
2427
2428 if (SE->hasLoopInvariantBackedgeTakenCount(L))
2429 return runOnCountableLoop(L);
2430 return false;
2431}
2432
2433bool HexagonLoopIdiomRecognizeLegacyPass::runOnLoop(Loop *L,
2434 LPPassManager &LPM) {
2435 if (skipLoop(L))
2436 return false;
2437
2438 auto *AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
2439 auto *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
2440 auto *LF = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
2441 auto *TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(
2442 *L->getHeader()->getParent());
2443 auto *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
2444 return HexagonLoopIdiomRecognize(AA, DT, LF, TLI, SE).run(L);
2445}
2446
2448 return new HexagonLoopIdiomRecognizeLegacyPass();
2449}
2450
2454 LPMUpdater &U) {
2455 return HexagonLoopIdiomRecognize(&AR.AA, &AR.DT, &AR.LI, &AR.TLI, &AR.SE)
2456 .run(&L)
2459}
This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
This file contains the simple types necessary to represent the attributes associated with functions a...
static const Function * getParent(const Value *V)
static void cleanup(BlockFrequencyInfoImplBase &BFI)
Clear all memory not needed downstream.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define LLVM_ATTRIBUTE_USED
Definition: Compiler.h:230
This file contains the declarations for the subclasses of Constant, which represent the different fla...
DXIL Resource Access
#define LLVM_DEBUG(...)
Definition: Debug.h:106
This file defines the DenseMap class.
std::string Name
bool End
Definition: ELF_riscv.cpp:480
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
hexagon bit simplify
static cl::opt< unsigned > SimplifyLimit("hlir-simplify-limit", cl::init(10000), cl::Hidden, cl::desc("Maximum number of simplification steps in HLIR"))
hexagon loop Recognize Hexagon specific loop idioms
static cl::opt< bool > DisableMemcpyIdiom("disable-memcpy-idiom", cl::Hidden, cl::init(false), cl::desc("Disable generation of memcpy in loop idiom recognition"))
static void replaceAllUsesOfWithIn(Value *I, Value *J, BasicBlock *BB)
static cl::opt< unsigned > RuntimeMemSizeThreshold("runtime-mem-idiom-threshold", cl::Hidden, cl::init(0), cl::desc("Threshold (in bytes) for the runtime " "check guarding the memmove."))
static cl::opt< bool > HexagonVolatileMemcpy("disable-hexagon-volatile-memcpy", cl::Hidden, cl::init(false), cl::desc("Enable Hexagon-specific memcpy for volatile destination."))
hexagon loop idiom
static cl::opt< bool > DisableMemmoveIdiom("disable-memmove-idiom", cl::Hidden, cl::init(false), cl::desc("Disable generation of memmove in loop idiom recognition"))
static cl::opt< unsigned > CompileTimeMemSizeThreshold("compile-time-mem-idiom-threshold", cl::Hidden, cl::init(64), cl::desc("Threshold (in bytes) to perform the transformation, if the " "runtime loop count (mem transfer size) is known at compile-time."))
static bool mayLoopAccessLocation(Value *Ptr, ModRefInfo Access, Loop *L, const SCEV *BECount, unsigned StoreSize, AliasAnalysis &AA, SmallPtrSetImpl< Instruction * > &Ignored)
mayLoopAccessLocation - Return true if the specified loop might access the specified pointer location...
static const char * HexagonVolatileMemcpyName
static bool hasZeroSignBit(const Value *V)
static cl::opt< bool > OnlyNonNestedMemmove("only-nonnested-memmove-idiom", cl::Hidden, cl::init(true), cl::desc("Only enable generating memmove in non-nested loops"))
Module.h This file contains the declarations for the Module class.
This header defines various interfaces for pass management in LLVM.
iv Induction Variable Users
Definition: IVUsers.cpp:48
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
Definition: Lint.cpp:533
Move duplicate certain instructions close to their use
Definition: Localizer.cpp:33
This header provides classes for managing per-loop analyses.
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
#define H(x, y, z)
Definition: MD5.cpp:57
This file provides utility analysis objects describing memory locations.
uint64_t IntrinsicInst * II
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
#define P(N)
if(PassOpts->AAPipeline)
PassBuilder PB(Machine, PassOpts->PTO, std::nullopt, &PIC)
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:55
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:57
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:52
const SmallVectorImpl< MachineOperand > & Cond
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
This file implements a set that has insertion order iteration characteristics.
This file defines the SmallPtrSet class.
This file defines the SmallSet class.
This file defines the SmallVector class.
static void initialize(TargetLibraryInfoImpl &TLI, const Triple &T, ArrayRef< StringLiteral > StandardNames)
Initialize the set of available library functions based on the specified target triple.
static std::optional< unsigned > getOpcode(ArrayRef< VPValue * > Values)
Returns the opcode of Values or ~0 if they do not all agree.
Definition: VPlanSLP.cpp:191
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object.
ModRefInfo getModRefInfo(const Instruction *I, const std::optional< MemoryLocation > &OptLoc)
Check whether or not an instruction may read or write the optionally specified memory location.
bool isSignMask() const
Check if the APInt's value is returned by getSignMask.
Definition: APInt.h:466
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition: APInt.h:306
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
Represent the analysis usage information of a pass.
AnalysisUsage & addRequiredID(const void *ID)
Definition: Pass.cpp:270
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
LLVM Basic Block Representation.
Definition: BasicBlock.h:61
iterator end()
Definition: BasicBlock.h:461
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:448
iterator_range< const_phi_iterator > phis() const
Returns a range that iterates over the phis in the basic block.
Definition: BasicBlock.h:517
const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
Definition: BasicBlock.cpp:416
const Instruction * getFirstNonPHI() const
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
Definition: BasicBlock.cpp:367
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition: BasicBlock.h:212
const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
Definition: BasicBlock.cpp:459
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:219
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:177
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.h:239
BinaryOps getOpcode() const
Definition: InstrTypes.h:370
This class represents a function call, abstracting a target machine's calling convention.
This class is the base class for the comparison instructions.
Definition: InstrTypes.h:661
@ ICMP_EQ
equal
Definition: InstrTypes.h:694
@ ICMP_NE
not equal
Definition: InstrTypes.h:695
An abstraction over a floating-point predicate, and a pack of an integer predicate with samesign info...
Definition: CmpPredicate.h:22
This is the shared class of boolean and integer constants.
Definition: Constants.h:83
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition: Constants.h:148
This class represents an Operation in the Expression.
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:63
A debug info location.
Definition: DebugLoc.h:33
void setIDom(DomTreeNodeBase *NewIDom)
void changeImmediateDominator(DomTreeNodeBase< NodeT > *N, DomTreeNodeBase< NodeT > *NewIDom)
changeImmediateDominator - This method is used to update the dominator tree information when a node's...
DomTreeNodeBase< NodeT > * addNewBlock(NodeT *BB, NodeT *DomBB)
Add a new node to the dominator tree information.
DomTreeNodeBase< NodeT > * getNode(const NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:317
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:162
Instruction * findNearestCommonDominator(Instruction *I1, Instruction *I2) const
Find the nearest instruction I that dominates both I1 and I2, in the sense that a result produced bef...
Definition: Dominators.cpp:344
bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
Definition: Dominators.cpp:122
A handy container for a FunctionType+Callee-pointer pair, which can be passed around as a single enti...
Definition: DerivedTypes.h:170
A possibly irreducible generalization of a Loop.
Value * CreateZExt(Value *V, Type *DestTy, const Twine &Name="", bool IsNonNeg=false)
Definition: IRBuilder.h:2048
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1498
Value * CreateTrunc(Value *V, Type *DestTy, const Twine &Name="", bool IsNUW=false, bool IsNSW=false)
Definition: IRBuilder.h:2034
BranchInst * CreateBr(BasicBlock *Dest)
Create an unconditional 'br label X' instruction.
Definition: IRBuilder.h:1138
Value * CreateIntCast(Value *V, Type *DestTy, bool isSigned, const Twine &Name="")
Definition: IRBuilder.h:2227
Value * CreateXor(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1542
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2697
InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
Definition: Instruction.cpp:92
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:274
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
Definition: Instruction.h:468
Class to represent integer types.
Definition: DerivedTypes.h:42
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition: Type.cpp:311
unsigned getBitWidth() const
Get the number of bits in this IntegerType.
Definition: DerivedTypes.h:74
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
This class provides an interface for updating the loop pass manager based on mutations to the loop ne...
An instruction for reading from memory.
Definition: Instructions.h:176
unsigned getPointerAddressSpace() const
Returns the address space of the pointer operand.
Definition: Instructions.h:261
Value * getPointerOperand()
Definition: Instructions.h:255
bool isSimple() const
Definition: Instructions.h:247
Align getAlign() const
Return the alignment of the access that is being performed.
Definition: Instructions.h:211
static LocationSize precise(uint64_t Value)
static constexpr LocationSize afterPointer()
Any location after the base pointer (but still within the underlying object).
BlockT * getHeader() const
void addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase< BlockT, LoopT > &LI)
This method is used by other analyses to update loop information.
BlockT * getLoopPreheader() const
If there is a preheader for this loop, return it.
void getUniqueExitBlocks(SmallVectorImpl< BlockT * > &ExitBlocks) const
Return all unique successor blocks of this loop.
LoopT * getParentLoop() const
Return the parent loop if it exists or nullptr for top level loops.
The legacy pass manager's analysis pass to compute loop information.
Definition: LoopInfo.h:593
virtual bool runOnLoop(Loop *L, LPPassManager &LPM)=0
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:39
Representation for a specific memory location.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
void setIncomingBlock(unsigned i, BasicBlock *BB)
int getBasicBlockIndex(const BasicBlock *BB) const
Return the first index of the specified basic block in the value list for this PHI.
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:37
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:94
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Pass.cpp:98
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:111
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:117
This node represents a polynomial recurrence on the trip count of the specified loop.
This class represents a constant integer value.
This class uses information about analyze scalars to rewrite expressions in canonical form.
const SCEV * getOperand(unsigned i) const
This class represents an analyzed expression in the program.
Type * getType() const
Return the LLVM type of this SCEV expression.
The main scalar evolution driver.
This class represents the LLVM 'select' instruction.
const Value * getFalseValue() const
const Value * getCondition() const
const Value * getTrueValue() const
A vector that has set insertion semantics.
Definition: SetVector.h:57
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:363
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:452
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:384
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:519
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition: SmallSet.h:132
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.
Definition: SmallSet.h:175
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition: SmallSet.h:181
size_t size() const
Definition: SmallVector.h:78
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:573
void push_back(const T &Elt)
Definition: SmallVector.h:413
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
An instruction for storing to memory.
Definition: Instructions.h:292
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
Provides information about what library functions are available for the current target.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
ArchType getArch() const
Get the parsed architecture type of this triple.
Definition: Triple.h:383
This class represents a truncation of integer types.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
static Type * getVoidTy(LLVMContext &C)
static IntegerType * getInt32Ty(LLVMContext &C)
bool isVoidTy() const
Return true if this is 'void'.
Definition: Type.h:139
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
User * getUser() const
Returns the User that contains this Use.
Definition: Use.h:72
op_range operands()
Definition: User.h:288
Value * getOperand(unsigned i) const
Definition: User.h:228
unsigned getNumOperands() const
Definition: User.h:250
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
user_iterator user_begin()
Definition: Value.h:397
void setName(const Twine &Name)
Change the name of the value.
Definition: Value.cpp:377
bool hasOneUse() const
Return true if there is exactly one use of this value.
Definition: Value.h:434
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:534
iterator_range< user_iterator > users()
Definition: Value.h:421
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:309
This class represents zero extension of integer types.
const ParentTy * getParent() const
Definition: ilist_node.h:32
self_iterator getIterator()
Definition: ilist_node.h:132
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
Definition: BitmaskEnum.h:125
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
Function * getOrInsertDeclaration(Module *M, ID id, ArrayRef< Type * > Tys={})
Look up the Function declaration of the intrinsic id in the Module M.
Definition: Intrinsics.cpp:731
@ SC
CHAIN = SC CHAIN, Imm128 - System call.
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::Xor > m_Xor(const LHS &L, const RHS &R)
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:49
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
Definition: PatternMatch.h:885
CmpClass_match< LHS, RHS, ICmpInst, true > m_c_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
Matches an ICmp with a predicate over LHS and RHS in either order.
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
Definition: PatternMatch.h:592
BinaryOp_match< LHS, RHS, Instruction::Xor, true > m_c_Xor(const LHS &L, const RHS &R)
Matches an Xor with LHS and RHS in either order.
CastInst_match< OpTy, ZExtInst > m_ZExt(const OpTy &Op)
Matches ZExt.
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
Definition: PatternMatch.h:92
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
CmpClass_match< LHS, RHS, ICmpInst > m_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
Definition: PatternMatch.h:612
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:443
PointerTypeMap run(const Module &M)
Compute the PointerTypeMap for the module M.
NodeAddr< FuncNode * > Func
Definition: RDFGraph.h:393
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1759
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1739
bool RecursivelyDeleteTriviallyDeadInstructions(Value *V, const TargetLibraryInfo *TLI=nullptr, MemorySSAUpdater *MSSAU=nullptr, std::function< void(Value *)> AboutToDeleteCallback=std::function< void(Value *)>())
If the specified value is a trivially dead instruction, delete it.
Definition: Local.cpp:546
char & LoopSimplifyID
auto pred_end(const MachineBasicBlock *BB)
Pass * createHexagonLoopIdiomPass()
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition: STLExtras.h:657
char & LCSSAID
Definition: LCSSA.cpp:542
Value * simplifyInstruction(Instruction *I, const SimplifyQuery &Q)
See if we can compute a simplified version of this instruction.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
bool isModOrRefSet(const ModRefInfo MRI)
Definition: ModRef.h:42
ModRefInfo
Flags indicating whether a memory access modifies or references memory.
Definition: ModRef.h:27
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
void replace(R &&Range, const T &OldValue, const T &NewValue)
Provide wrappers to std::replace which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1866
@ Xor
Bitwise or logical XOR of integers.
@ And
Bitwise or logical AND of integers.
void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
DWARFExpression::Operation Op
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
Definition: APFixedPoint.h:303
auto pred_begin(const MachineBasicBlock *BB)
void initializeHexagonLoopIdiomRecognizeLegacyPassPass(PassRegistry &)
PreservedAnalyses getLoopPassPreservedAnalyses()
Returns the minimum set of Analyses that all loop passes must preserve.
auto predecessors(const MachineBasicBlock *BB)
bool equal(L &&LRange, R &&RRange)
Wrapper function around std::equal to detect if pair-wise elements between two ranges are the same.
Definition: STLExtras.h:2067
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:860
#define N
PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM, LoopStandardAnalysisResults &AR, LPMUpdater &U)
The adaptor from a function pass to a loop pass computes these analyses and makes them available to t...