xref: /freebsd/contrib/llvm-project/llvm/lib/Target/Hexagon/HexagonVectorCombine.cpp (revision fcaf7f8644a9988098ac6be2165bce3ea4786e91)
1 //===-- HexagonVectorCombine.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 // HexagonVectorCombine is a utility class implementing a variety of functions
9 // that assist in vector-based optimizations.
10 //
11 // AlignVectors: replace unaligned vector loads and stores with aligned ones.
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/ADT/APInt.h"
15 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/ADT/Optional.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/Analysis/AliasAnalysis.h"
21 #include "llvm/Analysis/AssumptionCache.h"
22 #include "llvm/Analysis/InstructionSimplify.h"
23 #include "llvm/Analysis/TargetLibraryInfo.h"
24 #include "llvm/Analysis/ValueTracking.h"
25 #include "llvm/Analysis/VectorUtils.h"
26 #include "llvm/CodeGen/TargetPassConfig.h"
27 #include "llvm/IR/Dominators.h"
28 #include "llvm/IR/IRBuilder.h"
29 #include "llvm/IR/IntrinsicInst.h"
30 #include "llvm/IR/Intrinsics.h"
31 #include "llvm/IR/IntrinsicsHexagon.h"
32 #include "llvm/IR/Metadata.h"
33 #include "llvm/InitializePasses.h"
34 #include "llvm/Pass.h"
35 #include "llvm/Support/KnownBits.h"
36 #include "llvm/Support/MathExtras.h"
37 #include "llvm/Support/raw_ostream.h"
38 #include "llvm/Target/TargetMachine.h"
39 
40 #include "HexagonSubtarget.h"
41 #include "HexagonTargetMachine.h"
42 
43 #include <algorithm>
44 #include <deque>
45 #include <map>
46 #include <set>
47 #include <utility>
48 #include <vector>
49 
50 #define DEBUG_TYPE "hexagon-vc"
51 
52 using namespace llvm;
53 
54 namespace {
55 class HexagonVectorCombine {
56 public:
57   HexagonVectorCombine(Function &F_, AliasAnalysis &AA_, AssumptionCache &AC_,
58                        DominatorTree &DT_, TargetLibraryInfo &TLI_,
59                        const TargetMachine &TM_)
60       : F(F_), DL(F.getParent()->getDataLayout()), AA(AA_), AC(AC_), DT(DT_),
61         TLI(TLI_),
62         HST(static_cast<const HexagonSubtarget &>(*TM_.getSubtargetImpl(F))) {}
63 
64   bool run();
65 
66   // Common integer type.
67   IntegerType *getIntTy() const;
68   // Byte type: either scalar (when Length = 0), or vector with given
69   // element count.
70   Type *getByteTy(int ElemCount = 0) const;
71   // Boolean type: either scalar (when Length = 0), or vector with given
72   // element count.
73   Type *getBoolTy(int ElemCount = 0) const;
74   // Create a ConstantInt of type returned by getIntTy with the value Val.
75   ConstantInt *getConstInt(int Val) const;
76   // Get the integer value of V, if it exists.
77   Optional<APInt> getIntValue(const Value *Val) const;
78   // Is V a constant 0, or a vector of 0s?
79   bool isZero(const Value *Val) const;
80   // Is V an undef value?
81   bool isUndef(const Value *Val) const;
82 
83   int getSizeOf(const Value *Val) const;
84   int getSizeOf(const Type *Ty) const;
85   int getAllocSizeOf(const Type *Ty) const;
86   int getTypeAlignment(Type *Ty) const;
87 
88   Constant *getNullValue(Type *Ty) const;
89   Constant *getFullValue(Type *Ty) const;
90 
91   Value *insertb(IRBuilder<> &Builder, Value *Dest, Value *Src, int Start,
92                  int Length, int Where) const;
93   Value *vlalignb(IRBuilder<> &Builder, Value *Lo, Value *Hi, Value *Amt) const;
94   Value *vralignb(IRBuilder<> &Builder, Value *Lo, Value *Hi, Value *Amt) const;
95   Value *concat(IRBuilder<> &Builder, ArrayRef<Value *> Vecs) const;
96   Value *vresize(IRBuilder<> &Builder, Value *Val, int NewSize,
97                  Value *Pad) const;
98   Value *rescale(IRBuilder<> &Builder, Value *Mask, Type *FromTy,
99                  Type *ToTy) const;
100   Value *vlsb(IRBuilder<> &Builder, Value *Val) const;
101   Value *vbytes(IRBuilder<> &Builder, Value *Val) const;
102 
103   Value *createHvxIntrinsic(IRBuilder<> &Builder, Intrinsic::ID IntID,
104                             Type *RetTy, ArrayRef<Value *> Args) const;
105 
106   Optional<int> calculatePointerDifference(Value *Ptr0, Value *Ptr1) const;
107 
108   template <typename T = std::vector<Instruction *>>
109   bool isSafeToMoveBeforeInBB(const Instruction &In,
110                               BasicBlock::const_iterator To,
111                               const T &Ignore = {}) const;
112 
113   Function &F;
114   const DataLayout &DL;
115   AliasAnalysis &AA;
116   AssumptionCache &AC;
117   DominatorTree &DT;
118   TargetLibraryInfo &TLI;
119   const HexagonSubtarget &HST;
120 
121 private:
122 #ifndef NDEBUG
123   // These two functions are only used for assertions at the moment.
124   bool isByteVecTy(Type *Ty) const;
125   bool isSectorTy(Type *Ty) const;
126 #endif
127   Value *getElementRange(IRBuilder<> &Builder, Value *Lo, Value *Hi, int Start,
128                          int Length) const;
129 };
130 
131 class AlignVectors {
132 public:
133   AlignVectors(HexagonVectorCombine &HVC_) : HVC(HVC_) {}
134 
135   bool run();
136 
137 private:
138   using InstList = std::vector<Instruction *>;
139 
140   struct Segment {
141     void *Data;
142     int Start;
143     int Size;
144   };
145 
146   struct AddrInfo {
147     AddrInfo(const AddrInfo &) = default;
148     AddrInfo(const HexagonVectorCombine &HVC, Instruction *I, Value *A, Type *T,
149              Align H)
150         : Inst(I), Addr(A), ValTy(T), HaveAlign(H),
151           NeedAlign(HVC.getTypeAlignment(ValTy)) {}
152 
153     // XXX: add Size member?
154     Instruction *Inst;
155     Value *Addr;
156     Type *ValTy;
157     Align HaveAlign;
158     Align NeedAlign;
159     int Offset = 0; // Offset (in bytes) from the first member of the
160                     // containing AddrList.
161   };
162   using AddrList = std::vector<AddrInfo>;
163 
164   struct InstrLess {
165     bool operator()(const Instruction *A, const Instruction *B) const {
166       return A->comesBefore(B);
167     }
168   };
169   using DepList = std::set<Instruction *, InstrLess>;
170 
171   struct MoveGroup {
172     MoveGroup(const AddrInfo &AI, Instruction *B, bool Hvx, bool Load)
173         : Base(B), Main{AI.Inst}, IsHvx(Hvx), IsLoad(Load) {}
174     Instruction *Base; // Base instruction of the parent address group.
175     InstList Main;     // Main group of instructions.
176     InstList Deps;     // List of dependencies.
177     bool IsHvx;        // Is this group of HVX instructions?
178     bool IsLoad;       // Is this a load group?
179   };
180   using MoveList = std::vector<MoveGroup>;
181 
182   struct ByteSpan {
183     struct Segment {
184       // Segment of a Value: 'Len' bytes starting at byte 'Begin'.
185       Segment(Value *Val, int Begin, int Len)
186           : Val(Val), Start(Begin), Size(Len) {}
187       Segment(const Segment &Seg) = default;
188       Value *Val; // Value representable as a sequence of bytes.
189       int Start;  // First byte of the value that belongs to the segment.
190       int Size;   // Number of bytes in the segment.
191     };
192 
193     struct Block {
194       Block(Value *Val, int Len, int Pos) : Seg(Val, 0, Len), Pos(Pos) {}
195       Block(Value *Val, int Off, int Len, int Pos)
196           : Seg(Val, Off, Len), Pos(Pos) {}
197       Block(const Block &Blk) = default;
198       Segment Seg; // Value segment.
199       int Pos;     // Position (offset) of the segment in the Block.
200     };
201 
202     int extent() const;
203     ByteSpan section(int Start, int Length) const;
204     ByteSpan &shift(int Offset);
205     SmallVector<Value *, 8> values() const;
206 
207     int size() const { return Blocks.size(); }
208     Block &operator[](int i) { return Blocks[i]; }
209 
210     std::vector<Block> Blocks;
211 
212     using iterator = decltype(Blocks)::iterator;
213     iterator begin() { return Blocks.begin(); }
214     iterator end() { return Blocks.end(); }
215     using const_iterator = decltype(Blocks)::const_iterator;
216     const_iterator begin() const { return Blocks.begin(); }
217     const_iterator end() const { return Blocks.end(); }
218   };
219 
220   Align getAlignFromValue(const Value *V) const;
221   Optional<MemoryLocation> getLocation(const Instruction &In) const;
222   Optional<AddrInfo> getAddrInfo(Instruction &In) const;
223   bool isHvx(const AddrInfo &AI) const;
224 
225   Value *getPayload(Value *Val) const;
226   Value *getMask(Value *Val) const;
227   Value *getPassThrough(Value *Val) const;
228 
229   Value *createAdjustedPointer(IRBuilder<> &Builder, Value *Ptr, Type *ValTy,
230                                int Adjust) const;
231   Value *createAlignedPointer(IRBuilder<> &Builder, Value *Ptr, Type *ValTy,
232                               int Alignment) const;
233   Value *createAlignedLoad(IRBuilder<> &Builder, Type *ValTy, Value *Ptr,
234                            int Alignment, Value *Mask, Value *PassThru) const;
235   Value *createAlignedStore(IRBuilder<> &Builder, Value *Val, Value *Ptr,
236                             int Alignment, Value *Mask) const;
237 
238   bool createAddressGroups();
239   MoveList createLoadGroups(const AddrList &Group) const;
240   MoveList createStoreGroups(const AddrList &Group) const;
241   bool move(const MoveGroup &Move) const;
242   bool realignGroup(const MoveGroup &Move) const;
243 
244   friend raw_ostream &operator<<(raw_ostream &OS, const AddrInfo &AI);
245   friend raw_ostream &operator<<(raw_ostream &OS, const MoveGroup &MG);
246   friend raw_ostream &operator<<(raw_ostream &OS, const ByteSpan &BS);
247 
248   std::map<Instruction *, AddrList> AddrGroups;
249   HexagonVectorCombine &HVC;
250 };
251 
252 LLVM_ATTRIBUTE_UNUSED
253 raw_ostream &operator<<(raw_ostream &OS, const AlignVectors::AddrInfo &AI) {
254   OS << "Inst: " << AI.Inst << "  " << *AI.Inst << '\n';
255   OS << "Addr: " << *AI.Addr << '\n';
256   OS << "Type: " << *AI.ValTy << '\n';
257   OS << "HaveAlign: " << AI.HaveAlign.value() << '\n';
258   OS << "NeedAlign: " << AI.NeedAlign.value() << '\n';
259   OS << "Offset: " << AI.Offset;
260   return OS;
261 }
262 
263 LLVM_ATTRIBUTE_UNUSED
264 raw_ostream &operator<<(raw_ostream &OS, const AlignVectors::MoveGroup &MG) {
265   OS << "Main\n";
266   for (Instruction *I : MG.Main)
267     OS << "  " << *I << '\n';
268   OS << "Deps\n";
269   for (Instruction *I : MG.Deps)
270     OS << "  " << *I << '\n';
271   return OS;
272 }
273 
274 LLVM_ATTRIBUTE_UNUSED
275 raw_ostream &operator<<(raw_ostream &OS, const AlignVectors::ByteSpan &BS) {
276   OS << "ByteSpan[size=" << BS.size() << ", extent=" << BS.extent() << '\n';
277   for (const AlignVectors::ByteSpan::Block &B : BS) {
278     OS << "  @" << B.Pos << " [" << B.Seg.Start << ',' << B.Seg.Size << "] "
279        << *B.Seg.Val << '\n';
280   }
281   OS << ']';
282   return OS;
283 }
284 
285 } // namespace
286 
287 namespace {
288 
289 template <typename T> T *getIfUnordered(T *MaybeT) {
290   return MaybeT && MaybeT->isUnordered() ? MaybeT : nullptr;
291 }
292 template <typename T> T *isCandidate(Instruction *In) {
293   return dyn_cast<T>(In);
294 }
295 template <> LoadInst *isCandidate<LoadInst>(Instruction *In) {
296   return getIfUnordered(dyn_cast<LoadInst>(In));
297 }
298 template <> StoreInst *isCandidate<StoreInst>(Instruction *In) {
299   return getIfUnordered(dyn_cast<StoreInst>(In));
300 }
301 
302 #if !defined(_MSC_VER) || _MSC_VER >= 1926
303 // VS2017 and some versions of VS2019 have trouble compiling this:
304 // error C2976: 'std::map': too few template arguments
305 // VS 2019 16.x is known to work, except for 16.4/16.5 (MSC_VER 1924/1925)
306 template <typename Pred, typename... Ts>
307 void erase_if(std::map<Ts...> &map, Pred p)
308 #else
309 template <typename Pred, typename T, typename U>
310 void erase_if(std::map<T, U> &map, Pred p)
311 #endif
312 {
313   for (auto i = map.begin(), e = map.end(); i != e;) {
314     if (p(*i))
315       i = map.erase(i);
316     else
317       i = std::next(i);
318   }
319 }
320 
321 // Forward other erase_ifs to the LLVM implementations.
322 template <typename Pred, typename T> void erase_if(T &&container, Pred p) {
323   llvm::erase_if(std::forward<T>(container), p);
324 }
325 
326 } // namespace
327 
328 // --- Begin AlignVectors
329 
330 auto AlignVectors::ByteSpan::extent() const -> int {
331   if (size() == 0)
332     return 0;
333   int Min = Blocks[0].Pos;
334   int Max = Blocks[0].Pos + Blocks[0].Seg.Size;
335   for (int i = 1, e = size(); i != e; ++i) {
336     Min = std::min(Min, Blocks[i].Pos);
337     Max = std::max(Max, Blocks[i].Pos + Blocks[i].Seg.Size);
338   }
339   return Max - Min;
340 }
341 
342 auto AlignVectors::ByteSpan::section(int Start, int Length) const -> ByteSpan {
343   ByteSpan Section;
344   for (const ByteSpan::Block &B : Blocks) {
345     int L = std::max(B.Pos, Start);                       // Left end.
346     int R = std::min(B.Pos + B.Seg.Size, Start + Length); // Right end+1.
347     if (L < R) {
348       // How much to chop off the beginning of the segment:
349       int Off = L > B.Pos ? L - B.Pos : 0;
350       Section.Blocks.emplace_back(B.Seg.Val, B.Seg.Start + Off, R - L, L);
351     }
352   }
353   return Section;
354 }
355 
356 auto AlignVectors::ByteSpan::shift(int Offset) -> ByteSpan & {
357   for (Block &B : Blocks)
358     B.Pos += Offset;
359   return *this;
360 }
361 
362 auto AlignVectors::ByteSpan::values() const -> SmallVector<Value *, 8> {
363   SmallVector<Value *, 8> Values(Blocks.size());
364   for (int i = 0, e = Blocks.size(); i != e; ++i)
365     Values[i] = Blocks[i].Seg.Val;
366   return Values;
367 }
368 
369 auto AlignVectors::getAlignFromValue(const Value *V) const -> Align {
370   const auto *C = dyn_cast<ConstantInt>(V);
371   assert(C && "Alignment must be a compile-time constant integer");
372   return C->getAlignValue();
373 }
374 
375 auto AlignVectors::getAddrInfo(Instruction &In) const -> Optional<AddrInfo> {
376   if (auto *L = isCandidate<LoadInst>(&In))
377     return AddrInfo(HVC, L, L->getPointerOperand(), L->getType(),
378                     L->getAlign());
379   if (auto *S = isCandidate<StoreInst>(&In))
380     return AddrInfo(HVC, S, S->getPointerOperand(),
381                     S->getValueOperand()->getType(), S->getAlign());
382   if (auto *II = isCandidate<IntrinsicInst>(&In)) {
383     Intrinsic::ID ID = II->getIntrinsicID();
384     switch (ID) {
385     case Intrinsic::masked_load:
386       return AddrInfo(HVC, II, II->getArgOperand(0), II->getType(),
387                       getAlignFromValue(II->getArgOperand(1)));
388     case Intrinsic::masked_store:
389       return AddrInfo(HVC, II, II->getArgOperand(1),
390                       II->getArgOperand(0)->getType(),
391                       getAlignFromValue(II->getArgOperand(2)));
392     }
393   }
394   return Optional<AddrInfo>();
395 }
396 
397 auto AlignVectors::isHvx(const AddrInfo &AI) const -> bool {
398   return HVC.HST.isTypeForHVX(AI.ValTy);
399 }
400 
401 auto AlignVectors::getPayload(Value *Val) const -> Value * {
402   if (auto *In = dyn_cast<Instruction>(Val)) {
403     Intrinsic::ID ID = 0;
404     if (auto *II = dyn_cast<IntrinsicInst>(In))
405       ID = II->getIntrinsicID();
406     if (isa<StoreInst>(In) || ID == Intrinsic::masked_store)
407       return In->getOperand(0);
408   }
409   return Val;
410 }
411 
412 auto AlignVectors::getMask(Value *Val) const -> Value * {
413   if (auto *II = dyn_cast<IntrinsicInst>(Val)) {
414     switch (II->getIntrinsicID()) {
415     case Intrinsic::masked_load:
416       return II->getArgOperand(2);
417     case Intrinsic::masked_store:
418       return II->getArgOperand(3);
419     }
420   }
421 
422   Type *ValTy = getPayload(Val)->getType();
423   if (auto *VecTy = dyn_cast<VectorType>(ValTy)) {
424     int ElemCount = VecTy->getElementCount().getFixedValue();
425     return HVC.getFullValue(HVC.getBoolTy(ElemCount));
426   }
427   return HVC.getFullValue(HVC.getBoolTy());
428 }
429 
430 auto AlignVectors::getPassThrough(Value *Val) const -> Value * {
431   if (auto *II = dyn_cast<IntrinsicInst>(Val)) {
432     if (II->getIntrinsicID() == Intrinsic::masked_load)
433       return II->getArgOperand(3);
434   }
435   return UndefValue::get(getPayload(Val)->getType());
436 }
437 
438 auto AlignVectors::createAdjustedPointer(IRBuilder<> &Builder, Value *Ptr,
439                                          Type *ValTy, int Adjust) const
440     -> Value * {
441   // The adjustment is in bytes, but if it's a multiple of the type size,
442   // we don't need to do pointer casts.
443   auto *PtrTy = cast<PointerType>(Ptr->getType());
444   if (!PtrTy->isOpaque()) {
445     Type *ElemTy = PtrTy->getNonOpaquePointerElementType();
446     int ElemSize = HVC.getAllocSizeOf(ElemTy);
447     if (Adjust % ElemSize == 0 && Adjust != 0) {
448       Value *Tmp0 =
449           Builder.CreateGEP(ElemTy, Ptr, HVC.getConstInt(Adjust / ElemSize));
450       return Builder.CreatePointerCast(Tmp0, ValTy->getPointerTo());
451     }
452   }
453 
454   PointerType *CharPtrTy = Type::getInt8PtrTy(HVC.F.getContext());
455   Value *Tmp0 = Builder.CreatePointerCast(Ptr, CharPtrTy);
456   Value *Tmp1 = Builder.CreateGEP(Type::getInt8Ty(HVC.F.getContext()), Tmp0,
457                                   HVC.getConstInt(Adjust));
458   return Builder.CreatePointerCast(Tmp1, ValTy->getPointerTo());
459 }
460 
461 auto AlignVectors::createAlignedPointer(IRBuilder<> &Builder, Value *Ptr,
462                                         Type *ValTy, int Alignment) const
463     -> Value * {
464   Value *AsInt = Builder.CreatePtrToInt(Ptr, HVC.getIntTy());
465   Value *Mask = HVC.getConstInt(-Alignment);
466   Value *And = Builder.CreateAnd(AsInt, Mask);
467   return Builder.CreateIntToPtr(And, ValTy->getPointerTo());
468 }
469 
470 auto AlignVectors::createAlignedLoad(IRBuilder<> &Builder, Type *ValTy,
471                                      Value *Ptr, int Alignment, Value *Mask,
472                                      Value *PassThru) const -> Value * {
473   assert(!HVC.isUndef(Mask)); // Should this be allowed?
474   if (HVC.isZero(Mask))
475     return PassThru;
476   if (Mask == ConstantInt::getTrue(Mask->getType()))
477     return Builder.CreateAlignedLoad(ValTy, Ptr, Align(Alignment));
478   return Builder.CreateMaskedLoad(ValTy, Ptr, Align(Alignment), Mask, PassThru);
479 }
480 
481 auto AlignVectors::createAlignedStore(IRBuilder<> &Builder, Value *Val,
482                                       Value *Ptr, int Alignment,
483                                       Value *Mask) const -> Value * {
484   if (HVC.isZero(Mask) || HVC.isUndef(Val) || HVC.isUndef(Mask))
485     return UndefValue::get(Val->getType());
486   if (Mask == ConstantInt::getTrue(Mask->getType()))
487     return Builder.CreateAlignedStore(Val, Ptr, Align(Alignment));
488   return Builder.CreateMaskedStore(Val, Ptr, Align(Alignment), Mask);
489 }
490 
491 auto AlignVectors::createAddressGroups() -> bool {
492   // An address group created here may contain instructions spanning
493   // multiple basic blocks.
494   AddrList WorkStack;
495 
496   auto findBaseAndOffset = [&](AddrInfo &AI) -> std::pair<Instruction *, int> {
497     for (AddrInfo &W : WorkStack) {
498       if (auto D = HVC.calculatePointerDifference(AI.Addr, W.Addr))
499         return std::make_pair(W.Inst, *D);
500     }
501     return std::make_pair(nullptr, 0);
502   };
503 
504   auto traverseBlock = [&](DomTreeNode *DomN, auto Visit) -> void {
505     BasicBlock &Block = *DomN->getBlock();
506     for (Instruction &I : Block) {
507       auto AI = this->getAddrInfo(I); // Use this-> for gcc6.
508       if (!AI)
509         continue;
510       auto F = findBaseAndOffset(*AI);
511       Instruction *GroupInst;
512       if (Instruction *BI = F.first) {
513         AI->Offset = F.second;
514         GroupInst = BI;
515       } else {
516         WorkStack.push_back(*AI);
517         GroupInst = AI->Inst;
518       }
519       AddrGroups[GroupInst].push_back(*AI);
520     }
521 
522     for (DomTreeNode *C : DomN->children())
523       Visit(C, Visit);
524 
525     while (!WorkStack.empty() && WorkStack.back().Inst->getParent() == &Block)
526       WorkStack.pop_back();
527   };
528 
529   traverseBlock(HVC.DT.getRootNode(), traverseBlock);
530   assert(WorkStack.empty());
531 
532   // AddrGroups are formed.
533 
534   // Remove groups of size 1.
535   erase_if(AddrGroups, [](auto &G) { return G.second.size() == 1; });
536   // Remove groups that don't use HVX types.
537   erase_if(AddrGroups, [&](auto &G) {
538     return llvm::none_of(
539         G.second, [&](auto &I) { return HVC.HST.isTypeForHVX(I.ValTy); });
540   });
541 
542   return !AddrGroups.empty();
543 }
544 
545 auto AlignVectors::createLoadGroups(const AddrList &Group) const -> MoveList {
546   // Form load groups.
547   // To avoid complications with moving code across basic blocks, only form
548   // groups that are contained within a single basic block.
549 
550   auto getUpwardDeps = [](Instruction *In, Instruction *Base) {
551     BasicBlock *Parent = Base->getParent();
552     assert(In->getParent() == Parent &&
553            "Base and In should be in the same block");
554     assert(Base->comesBefore(In) && "Base should come before In");
555 
556     DepList Deps;
557     std::deque<Instruction *> WorkQ = {In};
558     while (!WorkQ.empty()) {
559       Instruction *D = WorkQ.front();
560       WorkQ.pop_front();
561       Deps.insert(D);
562       for (Value *Op : D->operands()) {
563         if (auto *I = dyn_cast<Instruction>(Op)) {
564           if (I->getParent() == Parent && Base->comesBefore(I))
565             WorkQ.push_back(I);
566         }
567       }
568     }
569     return Deps;
570   };
571 
572   auto tryAddTo = [&](const AddrInfo &Info, MoveGroup &Move) {
573     assert(!Move.Main.empty() && "Move group should have non-empty Main");
574     // Don't mix HVX and non-HVX instructions.
575     if (Move.IsHvx != isHvx(Info))
576       return false;
577     // Leading instruction in the load group.
578     Instruction *Base = Move.Main.front();
579     if (Base->getParent() != Info.Inst->getParent())
580       return false;
581 
582     auto isSafeToMoveToBase = [&](const Instruction *I) {
583       return HVC.isSafeToMoveBeforeInBB(*I, Base->getIterator());
584     };
585     DepList Deps = getUpwardDeps(Info.Inst, Base);
586     if (!llvm::all_of(Deps, isSafeToMoveToBase))
587       return false;
588 
589     // The dependencies will be moved together with the load, so make sure
590     // that none of them could be moved independently in another group.
591     Deps.erase(Info.Inst);
592     auto inAddrMap = [&](Instruction *I) { return AddrGroups.count(I) > 0; };
593     if (llvm::any_of(Deps, inAddrMap))
594       return false;
595     Move.Main.push_back(Info.Inst);
596     llvm::append_range(Move.Deps, Deps);
597     return true;
598   };
599 
600   MoveList LoadGroups;
601 
602   for (const AddrInfo &Info : Group) {
603     if (!Info.Inst->mayReadFromMemory())
604       continue;
605     if (LoadGroups.empty() || !tryAddTo(Info, LoadGroups.back()))
606       LoadGroups.emplace_back(Info, Group.front().Inst, isHvx(Info), true);
607   }
608 
609   // Erase singleton groups.
610   erase_if(LoadGroups, [](const MoveGroup &G) { return G.Main.size() <= 1; });
611   return LoadGroups;
612 }
613 
614 auto AlignVectors::createStoreGroups(const AddrList &Group) const -> MoveList {
615   // Form store groups.
616   // To avoid complications with moving code across basic blocks, only form
617   // groups that are contained within a single basic block.
618 
619   auto tryAddTo = [&](const AddrInfo &Info, MoveGroup &Move) {
620     assert(!Move.Main.empty() && "Move group should have non-empty Main");
621     // For stores with return values we'd have to collect downward depenencies.
622     // There are no such stores that we handle at the moment, so omit that.
623     assert(Info.Inst->getType()->isVoidTy() &&
624            "Not handling stores with return values");
625     // Don't mix HVX and non-HVX instructions.
626     if (Move.IsHvx != isHvx(Info))
627       return false;
628     // For stores we need to be careful whether it's safe to move them.
629     // Stores that are otherwise safe to move together may not appear safe
630     // to move over one another (i.e. isSafeToMoveBefore may return false).
631     Instruction *Base = Move.Main.front();
632     if (Base->getParent() != Info.Inst->getParent())
633       return false;
634     if (!HVC.isSafeToMoveBeforeInBB(*Info.Inst, Base->getIterator(), Move.Main))
635       return false;
636     Move.Main.push_back(Info.Inst);
637     return true;
638   };
639 
640   MoveList StoreGroups;
641 
642   for (auto I = Group.rbegin(), E = Group.rend(); I != E; ++I) {
643     const AddrInfo &Info = *I;
644     if (!Info.Inst->mayWriteToMemory())
645       continue;
646     if (StoreGroups.empty() || !tryAddTo(Info, StoreGroups.back()))
647       StoreGroups.emplace_back(Info, Group.front().Inst, isHvx(Info), false);
648   }
649 
650   // Erase singleton groups.
651   erase_if(StoreGroups, [](const MoveGroup &G) { return G.Main.size() <= 1; });
652   return StoreGroups;
653 }
654 
655 auto AlignVectors::move(const MoveGroup &Move) const -> bool {
656   assert(!Move.Main.empty() && "Move group should have non-empty Main");
657   Instruction *Where = Move.Main.front();
658 
659   if (Move.IsLoad) {
660     // Move all deps to before Where, keeping order.
661     for (Instruction *D : Move.Deps)
662       D->moveBefore(Where);
663     // Move all main instructions to after Where, keeping order.
664     ArrayRef<Instruction *> Main(Move.Main);
665     for (Instruction *M : Main.drop_front(1)) {
666       M->moveAfter(Where);
667       Where = M;
668     }
669   } else {
670     // NOTE: Deps are empty for "store" groups. If they need to be
671     // non-empty, decide on the order.
672     assert(Move.Deps.empty());
673     // Move all main instructions to before Where, inverting order.
674     ArrayRef<Instruction *> Main(Move.Main);
675     for (Instruction *M : Main.drop_front(1)) {
676       M->moveBefore(Where);
677       Where = M;
678     }
679   }
680 
681   return Move.Main.size() + Move.Deps.size() > 1;
682 }
683 
684 auto AlignVectors::realignGroup(const MoveGroup &Move) const -> bool {
685   // TODO: Needs support for masked loads/stores of "scalar" vectors.
686   if (!Move.IsHvx)
687     return false;
688 
689   // Return the element with the maximum alignment from Range,
690   // where GetValue obtains the value to compare from an element.
691   auto getMaxOf = [](auto Range, auto GetValue) {
692     return *std::max_element(
693         Range.begin(), Range.end(),
694         [&GetValue](auto &A, auto &B) { return GetValue(A) < GetValue(B); });
695   };
696 
697   const AddrList &BaseInfos = AddrGroups.at(Move.Base);
698 
699   // Conceptually, there is a vector of N bytes covering the addresses
700   // starting from the minimum offset (i.e. Base.Addr+Start). This vector
701   // represents a contiguous memory region that spans all accessed memory
702   // locations.
703   // The correspondence between loaded or stored values will be expressed
704   // in terms of this vector. For example, the 0th element of the vector
705   // from the Base address info will start at byte Start from the beginning
706   // of this conceptual vector.
707   //
708   // This vector will be loaded/stored starting at the nearest down-aligned
709   // address and the amount od the down-alignment will be AlignVal:
710   //   valign(load_vector(align_down(Base+Start)), AlignVal)
711 
712   std::set<Instruction *> TestSet(Move.Main.begin(), Move.Main.end());
713   AddrList MoveInfos;
714   llvm::copy_if(
715       BaseInfos, std::back_inserter(MoveInfos),
716       [&TestSet](const AddrInfo &AI) { return TestSet.count(AI.Inst); });
717 
718   // Maximum alignment present in the whole address group.
719   const AddrInfo &WithMaxAlign =
720       getMaxOf(MoveInfos, [](const AddrInfo &AI) { return AI.HaveAlign; });
721   Align MaxGiven = WithMaxAlign.HaveAlign;
722 
723   // Minimum alignment present in the move address group.
724   const AddrInfo &WithMinOffset =
725       getMaxOf(MoveInfos, [](const AddrInfo &AI) { return -AI.Offset; });
726 
727   const AddrInfo &WithMaxNeeded =
728       getMaxOf(MoveInfos, [](const AddrInfo &AI) { return AI.NeedAlign; });
729   Align MinNeeded = WithMaxNeeded.NeedAlign;
730 
731   // Set the builder at the top instruction in the move group.
732   Instruction *TopIn = Move.IsLoad ? Move.Main.front() : Move.Main.back();
733   IRBuilder<> Builder(TopIn);
734   Value *AlignAddr = nullptr; // Actual aligned address.
735   Value *AlignVal = nullptr;  // Right-shift amount (for valign).
736 
737   if (MinNeeded <= MaxGiven) {
738     int Start = WithMinOffset.Offset;
739     int OffAtMax = WithMaxAlign.Offset;
740     // Shift the offset of the maximally aligned instruction (OffAtMax)
741     // back by just enough multiples of the required alignment to cover the
742     // distance from Start to OffAtMax.
743     // Calculate the address adjustment amount based on the address with the
744     // maximum alignment. This is to allow a simple gep instruction instead
745     // of potential bitcasts to i8*.
746     int Adjust = -alignTo(OffAtMax - Start, MinNeeded.value());
747     AlignAddr = createAdjustedPointer(Builder, WithMaxAlign.Addr,
748                                       WithMaxAlign.ValTy, Adjust);
749     int Diff = Start - (OffAtMax + Adjust);
750     AlignVal = HVC.getConstInt(Diff);
751     assert(Diff >= 0);
752     assert(static_cast<decltype(MinNeeded.value())>(Diff) < MinNeeded.value());
753   } else {
754     // WithMinOffset is the lowest address in the group,
755     //   WithMinOffset.Addr = Base+Start.
756     // Align instructions for both HVX (V6_valign) and scalar (S2_valignrb)
757     // mask off unnecessary bits, so it's ok to just the original pointer as
758     // the alignment amount.
759     // Do an explicit down-alignment of the address to avoid creating an
760     // aligned instruction with an address that is not really aligned.
761     AlignAddr = createAlignedPointer(Builder, WithMinOffset.Addr,
762                                      WithMinOffset.ValTy, MinNeeded.value());
763     AlignVal = Builder.CreatePtrToInt(WithMinOffset.Addr, HVC.getIntTy());
764   }
765 
766   ByteSpan VSpan;
767   for (const AddrInfo &AI : MoveInfos) {
768     VSpan.Blocks.emplace_back(AI.Inst, HVC.getSizeOf(AI.ValTy),
769                               AI.Offset - WithMinOffset.Offset);
770   }
771 
772   // The aligned loads/stores will use blocks that are either scalars,
773   // or HVX vectors. Let "sector" be the unified term for such a block.
774   // blend(scalar, vector) -> sector...
775   int ScLen = Move.IsHvx ? HVC.HST.getVectorLength()
776                          : std::max<int>(MinNeeded.value(), 4);
777   assert(!Move.IsHvx || ScLen == 64 || ScLen == 128);
778   assert(Move.IsHvx || ScLen == 4 || ScLen == 8);
779 
780   Type *SecTy = HVC.getByteTy(ScLen);
781   int NumSectors = (VSpan.extent() + ScLen - 1) / ScLen;
782   bool DoAlign = !HVC.isZero(AlignVal);
783 
784   if (Move.IsLoad) {
785     ByteSpan ASpan;
786     auto *True = HVC.getFullValue(HVC.getBoolTy(ScLen));
787     auto *Undef = UndefValue::get(SecTy);
788 
789     for (int i = 0; i != NumSectors + DoAlign; ++i) {
790       Value *Ptr = createAdjustedPointer(Builder, AlignAddr, SecTy, i * ScLen);
791       // FIXME: generate a predicated load?
792       Value *Load = createAlignedLoad(Builder, SecTy, Ptr, ScLen, True, Undef);
793       // If vector shifting is potentially needed, accumulate metadata
794       // from source sections of twice the load width.
795       int Start = (i - DoAlign) * ScLen;
796       int Width = (1 + DoAlign) * ScLen;
797       propagateMetadata(cast<Instruction>(Load),
798                         VSpan.section(Start, Width).values());
799       ASpan.Blocks.emplace_back(Load, ScLen, i * ScLen);
800     }
801 
802     if (DoAlign) {
803       for (int j = 0; j != NumSectors; ++j) {
804         ASpan[j].Seg.Val = HVC.vralignb(Builder, ASpan[j].Seg.Val,
805                                         ASpan[j + 1].Seg.Val, AlignVal);
806       }
807     }
808 
809     for (ByteSpan::Block &B : VSpan) {
810       ByteSpan ASection = ASpan.section(B.Pos, B.Seg.Size).shift(-B.Pos);
811       Value *Accum = UndefValue::get(HVC.getByteTy(B.Seg.Size));
812       for (ByteSpan::Block &S : ASection) {
813         Value *Pay = HVC.vbytes(Builder, getPayload(S.Seg.Val));
814         Accum =
815             HVC.insertb(Builder, Accum, Pay, S.Seg.Start, S.Seg.Size, S.Pos);
816       }
817       // Instead of casting everything to bytes for the vselect, cast to the
818       // original value type. This will avoid complications with casting masks.
819       // For example, in cases when the original mask applied to i32, it could
820       // be converted to a mask applicable to i8 via pred_typecast intrinsic,
821       // but if the mask is not exactly of HVX length, extra handling would be
822       // needed to make it work.
823       Type *ValTy = getPayload(B.Seg.Val)->getType();
824       Value *Cast = Builder.CreateBitCast(Accum, ValTy);
825       Value *Sel = Builder.CreateSelect(getMask(B.Seg.Val), Cast,
826                                         getPassThrough(B.Seg.Val));
827       B.Seg.Val->replaceAllUsesWith(Sel);
828     }
829   } else {
830     // Stores.
831     ByteSpan ASpanV, ASpanM;
832 
833     // Return a vector value corresponding to the input value Val:
834     // either <1 x Val> for scalar Val, or Val itself for vector Val.
835     auto MakeVec = [](IRBuilder<> &Builder, Value *Val) -> Value * {
836       Type *Ty = Val->getType();
837       if (Ty->isVectorTy())
838         return Val;
839       auto *VecTy = VectorType::get(Ty, 1, /*Scalable*/ false);
840       return Builder.CreateBitCast(Val, VecTy);
841     };
842 
843     // Create an extra "undef" sector at the beginning and at the end.
844     // They will be used as the left/right filler in the vlalign step.
845     for (int i = (DoAlign ? -1 : 0); i != NumSectors + DoAlign; ++i) {
846       // For stores, the size of each section is an aligned vector length.
847       // Adjust the store offsets relative to the section start offset.
848       ByteSpan VSection = VSpan.section(i * ScLen, ScLen).shift(-i * ScLen);
849       Value *AccumV = UndefValue::get(SecTy);
850       Value *AccumM = HVC.getNullValue(SecTy);
851       for (ByteSpan::Block &S : VSection) {
852         Value *Pay = getPayload(S.Seg.Val);
853         Value *Mask = HVC.rescale(Builder, MakeVec(Builder, getMask(S.Seg.Val)),
854                                   Pay->getType(), HVC.getByteTy());
855         AccumM = HVC.insertb(Builder, AccumM, HVC.vbytes(Builder, Mask),
856                              S.Seg.Start, S.Seg.Size, S.Pos);
857         AccumV = HVC.insertb(Builder, AccumV, HVC.vbytes(Builder, Pay),
858                              S.Seg.Start, S.Seg.Size, S.Pos);
859       }
860       ASpanV.Blocks.emplace_back(AccumV, ScLen, i * ScLen);
861       ASpanM.Blocks.emplace_back(AccumM, ScLen, i * ScLen);
862     }
863 
864     // vlalign
865     if (DoAlign) {
866       for (int j = 1; j != NumSectors + 2; ++j) {
867         ASpanV[j - 1].Seg.Val = HVC.vlalignb(Builder, ASpanV[j - 1].Seg.Val,
868                                              ASpanV[j].Seg.Val, AlignVal);
869         ASpanM[j - 1].Seg.Val = HVC.vlalignb(Builder, ASpanM[j - 1].Seg.Val,
870                                              ASpanM[j].Seg.Val, AlignVal);
871       }
872     }
873 
874     for (int i = 0; i != NumSectors + DoAlign; ++i) {
875       Value *Ptr = createAdjustedPointer(Builder, AlignAddr, SecTy, i * ScLen);
876       Value *Val = ASpanV[i].Seg.Val;
877       Value *Mask = ASpanM[i].Seg.Val; // bytes
878       if (!HVC.isUndef(Val) && !HVC.isZero(Mask)) {
879         Value *Store = createAlignedStore(Builder, Val, Ptr, ScLen,
880                                           HVC.vlsb(Builder, Mask));
881         // If vector shifting is potentially needed, accumulate metadata
882         // from source sections of twice the store width.
883         int Start = (i - DoAlign) * ScLen;
884         int Width = (1 + DoAlign) * ScLen;
885         propagateMetadata(cast<Instruction>(Store),
886                           VSpan.section(Start, Width).values());
887       }
888     }
889   }
890 
891   for (auto *Inst : Move.Main)
892     Inst->eraseFromParent();
893 
894   return true;
895 }
896 
897 auto AlignVectors::run() -> bool {
898   if (!createAddressGroups())
899     return false;
900 
901   bool Changed = false;
902   MoveList LoadGroups, StoreGroups;
903 
904   for (auto &G : AddrGroups) {
905     llvm::append_range(LoadGroups, createLoadGroups(G.second));
906     llvm::append_range(StoreGroups, createStoreGroups(G.second));
907   }
908 
909   for (auto &M : LoadGroups)
910     Changed |= move(M);
911   for (auto &M : StoreGroups)
912     Changed |= move(M);
913 
914   for (auto &M : LoadGroups)
915     Changed |= realignGroup(M);
916   for (auto &M : StoreGroups)
917     Changed |= realignGroup(M);
918 
919   return Changed;
920 }
921 
922 // --- End AlignVectors
923 
924 auto HexagonVectorCombine::run() -> bool {
925   if (!HST.useHVXOps())
926     return false;
927 
928   bool Changed = AlignVectors(*this).run();
929   return Changed;
930 }
931 
932 auto HexagonVectorCombine::getIntTy() const -> IntegerType * {
933   return Type::getInt32Ty(F.getContext());
934 }
935 
936 auto HexagonVectorCombine::getByteTy(int ElemCount) const -> Type * {
937   assert(ElemCount >= 0);
938   IntegerType *ByteTy = Type::getInt8Ty(F.getContext());
939   if (ElemCount == 0)
940     return ByteTy;
941   return VectorType::get(ByteTy, ElemCount, /*Scalable*/ false);
942 }
943 
944 auto HexagonVectorCombine::getBoolTy(int ElemCount) const -> Type * {
945   assert(ElemCount >= 0);
946   IntegerType *BoolTy = Type::getInt1Ty(F.getContext());
947   if (ElemCount == 0)
948     return BoolTy;
949   return VectorType::get(BoolTy, ElemCount, /*Scalable*/ false);
950 }
951 
952 auto HexagonVectorCombine::getConstInt(int Val) const -> ConstantInt * {
953   return ConstantInt::getSigned(getIntTy(), Val);
954 }
955 
956 auto HexagonVectorCombine::isZero(const Value *Val) const -> bool {
957   if (auto *C = dyn_cast<Constant>(Val))
958     return C->isZeroValue();
959   return false;
960 }
961 
962 auto HexagonVectorCombine::getIntValue(const Value *Val) const
963     -> Optional<APInt> {
964   if (auto *CI = dyn_cast<ConstantInt>(Val))
965     return CI->getValue();
966   return None;
967 }
968 
969 auto HexagonVectorCombine::isUndef(const Value *Val) const -> bool {
970   return isa<UndefValue>(Val);
971 }
972 
973 auto HexagonVectorCombine::getSizeOf(const Value *Val) const -> int {
974   return getSizeOf(Val->getType());
975 }
976 
977 auto HexagonVectorCombine::getSizeOf(const Type *Ty) const -> int {
978   return DL.getTypeStoreSize(const_cast<Type *>(Ty)).getFixedValue();
979 }
980 
981 auto HexagonVectorCombine::getAllocSizeOf(const Type *Ty) const -> int {
982   return DL.getTypeAllocSize(const_cast<Type *>(Ty)).getFixedValue();
983 }
984 
985 auto HexagonVectorCombine::getTypeAlignment(Type *Ty) const -> int {
986   // The actual type may be shorter than the HVX vector, so determine
987   // the alignment based on subtarget info.
988   if (HST.isTypeForHVX(Ty))
989     return HST.getVectorLength();
990   return DL.getABITypeAlign(Ty).value();
991 }
992 
993 auto HexagonVectorCombine::getNullValue(Type *Ty) const -> Constant * {
994   assert(Ty->isIntOrIntVectorTy());
995   auto Zero = ConstantInt::get(Ty->getScalarType(), 0);
996   if (auto *VecTy = dyn_cast<VectorType>(Ty))
997     return ConstantVector::getSplat(VecTy->getElementCount(), Zero);
998   return Zero;
999 }
1000 
1001 auto HexagonVectorCombine::getFullValue(Type *Ty) const -> Constant * {
1002   assert(Ty->isIntOrIntVectorTy());
1003   auto Minus1 = ConstantInt::get(Ty->getScalarType(), -1);
1004   if (auto *VecTy = dyn_cast<VectorType>(Ty))
1005     return ConstantVector::getSplat(VecTy->getElementCount(), Minus1);
1006   return Minus1;
1007 }
1008 
1009 // Insert bytes [Start..Start+Length) of Src into Dst at byte Where.
1010 auto HexagonVectorCombine::insertb(IRBuilder<> &Builder, Value *Dst, Value *Src,
1011                                    int Start, int Length, int Where) const
1012     -> Value * {
1013   assert(isByteVecTy(Dst->getType()) && isByteVecTy(Src->getType()));
1014   int SrcLen = getSizeOf(Src);
1015   int DstLen = getSizeOf(Dst);
1016   assert(0 <= Start && Start + Length <= SrcLen);
1017   assert(0 <= Where && Where + Length <= DstLen);
1018 
1019   int P2Len = PowerOf2Ceil(SrcLen | DstLen);
1020   auto *Undef = UndefValue::get(getByteTy());
1021   Value *P2Src = vresize(Builder, Src, P2Len, Undef);
1022   Value *P2Dst = vresize(Builder, Dst, P2Len, Undef);
1023 
1024   SmallVector<int, 256> SMask(P2Len);
1025   for (int i = 0; i != P2Len; ++i) {
1026     // If i is in [Where, Where+Length), pick Src[Start+(i-Where)].
1027     // Otherwise, pick Dst[i];
1028     SMask[i] =
1029         (Where <= i && i < Where + Length) ? P2Len + Start + (i - Where) : i;
1030   }
1031 
1032   Value *P2Insert = Builder.CreateShuffleVector(P2Dst, P2Src, SMask);
1033   return vresize(Builder, P2Insert, DstLen, Undef);
1034 }
1035 
1036 auto HexagonVectorCombine::vlalignb(IRBuilder<> &Builder, Value *Lo, Value *Hi,
1037                                     Value *Amt) const -> Value * {
1038   assert(Lo->getType() == Hi->getType() && "Argument type mismatch");
1039   assert(isSectorTy(Hi->getType()));
1040   if (isZero(Amt))
1041     return Hi;
1042   int VecLen = getSizeOf(Hi);
1043   if (auto IntAmt = getIntValue(Amt))
1044     return getElementRange(Builder, Lo, Hi, VecLen - IntAmt->getSExtValue(),
1045                            VecLen);
1046 
1047   if (HST.isTypeForHVX(Hi->getType())) {
1048     int HwLen = HST.getVectorLength();
1049     assert(VecLen == HwLen && "Expecting an exact HVX type");
1050     Intrinsic::ID V6_vlalignb = HwLen == 64
1051                                     ? Intrinsic::hexagon_V6_vlalignb
1052                                     : Intrinsic::hexagon_V6_vlalignb_128B;
1053     return createHvxIntrinsic(Builder, V6_vlalignb, Hi->getType(),
1054                               {Hi, Lo, Amt});
1055   }
1056 
1057   if (VecLen == 4) {
1058     Value *Pair = concat(Builder, {Lo, Hi});
1059     Value *Shift = Builder.CreateLShr(Builder.CreateShl(Pair, Amt), 32);
1060     Value *Trunc = Builder.CreateTrunc(Shift, Type::getInt32Ty(F.getContext()));
1061     return Builder.CreateBitCast(Trunc, Hi->getType());
1062   }
1063   if (VecLen == 8) {
1064     Value *Sub = Builder.CreateSub(getConstInt(VecLen), Amt);
1065     return vralignb(Builder, Lo, Hi, Sub);
1066   }
1067   llvm_unreachable("Unexpected vector length");
1068 }
1069 
1070 auto HexagonVectorCombine::vralignb(IRBuilder<> &Builder, Value *Lo, Value *Hi,
1071                                     Value *Amt) const -> Value * {
1072   assert(Lo->getType() == Hi->getType() && "Argument type mismatch");
1073   assert(isSectorTy(Lo->getType()));
1074   if (isZero(Amt))
1075     return Lo;
1076   int VecLen = getSizeOf(Lo);
1077   if (auto IntAmt = getIntValue(Amt))
1078     return getElementRange(Builder, Lo, Hi, IntAmt->getSExtValue(), VecLen);
1079 
1080   if (HST.isTypeForHVX(Lo->getType())) {
1081     int HwLen = HST.getVectorLength();
1082     assert(VecLen == HwLen && "Expecting an exact HVX type");
1083     Intrinsic::ID V6_valignb = HwLen == 64 ? Intrinsic::hexagon_V6_valignb
1084                                            : Intrinsic::hexagon_V6_valignb_128B;
1085     return createHvxIntrinsic(Builder, V6_valignb, Lo->getType(),
1086                               {Hi, Lo, Amt});
1087   }
1088 
1089   if (VecLen == 4) {
1090     Value *Pair = concat(Builder, {Lo, Hi});
1091     Value *Shift = Builder.CreateLShr(Pair, Amt);
1092     Value *Trunc = Builder.CreateTrunc(Shift, Type::getInt32Ty(F.getContext()));
1093     return Builder.CreateBitCast(Trunc, Lo->getType());
1094   }
1095   if (VecLen == 8) {
1096     Type *Int64Ty = Type::getInt64Ty(F.getContext());
1097     Value *Lo64 = Builder.CreateBitCast(Lo, Int64Ty);
1098     Value *Hi64 = Builder.CreateBitCast(Hi, Int64Ty);
1099     Function *FI = Intrinsic::getDeclaration(F.getParent(),
1100                                              Intrinsic::hexagon_S2_valignrb);
1101     Value *Call = Builder.CreateCall(FI, {Hi64, Lo64, Amt});
1102     return Builder.CreateBitCast(Call, Lo->getType());
1103   }
1104   llvm_unreachable("Unexpected vector length");
1105 }
1106 
1107 // Concatenates a sequence of vectors of the same type.
1108 auto HexagonVectorCombine::concat(IRBuilder<> &Builder,
1109                                   ArrayRef<Value *> Vecs) const -> Value * {
1110   assert(!Vecs.empty());
1111   SmallVector<int, 256> SMask;
1112   std::vector<Value *> Work[2];
1113   int ThisW = 0, OtherW = 1;
1114 
1115   Work[ThisW].assign(Vecs.begin(), Vecs.end());
1116   while (Work[ThisW].size() > 1) {
1117     auto *Ty = cast<VectorType>(Work[ThisW].front()->getType());
1118     int ElemCount = Ty->getElementCount().getFixedValue();
1119     SMask.resize(ElemCount * 2);
1120     std::iota(SMask.begin(), SMask.end(), 0);
1121 
1122     Work[OtherW].clear();
1123     if (Work[ThisW].size() % 2 != 0)
1124       Work[ThisW].push_back(UndefValue::get(Ty));
1125     for (int i = 0, e = Work[ThisW].size(); i < e; i += 2) {
1126       Value *Joined = Builder.CreateShuffleVector(Work[ThisW][i],
1127                                                   Work[ThisW][i + 1], SMask);
1128       Work[OtherW].push_back(Joined);
1129     }
1130     std::swap(ThisW, OtherW);
1131   }
1132 
1133   // Since there may have been some undefs appended to make shuffle operands
1134   // have the same type, perform the last shuffle to only pick the original
1135   // elements.
1136   SMask.resize(Vecs.size() * getSizeOf(Vecs.front()->getType()));
1137   std::iota(SMask.begin(), SMask.end(), 0);
1138   Value *Total = Work[OtherW].front();
1139   return Builder.CreateShuffleVector(Total, SMask);
1140 }
1141 
1142 auto HexagonVectorCombine::vresize(IRBuilder<> &Builder, Value *Val,
1143                                    int NewSize, Value *Pad) const -> Value * {
1144   assert(isa<VectorType>(Val->getType()));
1145   auto *ValTy = cast<VectorType>(Val->getType());
1146   assert(ValTy->getElementType() == Pad->getType());
1147 
1148   int CurSize = ValTy->getElementCount().getFixedValue();
1149   if (CurSize == NewSize)
1150     return Val;
1151   // Truncate?
1152   if (CurSize > NewSize)
1153     return getElementRange(Builder, Val, /*Unused*/ Val, 0, NewSize);
1154   // Extend.
1155   SmallVector<int, 128> SMask(NewSize);
1156   std::iota(SMask.begin(), SMask.begin() + CurSize, 0);
1157   std::fill(SMask.begin() + CurSize, SMask.end(), CurSize);
1158   Value *PadVec = Builder.CreateVectorSplat(CurSize, Pad);
1159   return Builder.CreateShuffleVector(Val, PadVec, SMask);
1160 }
1161 
1162 auto HexagonVectorCombine::rescale(IRBuilder<> &Builder, Value *Mask,
1163                                    Type *FromTy, Type *ToTy) const -> Value * {
1164   // Mask is a vector <N x i1>, where each element corresponds to an
1165   // element of FromTy. Remap it so that each element will correspond
1166   // to an element of ToTy.
1167   assert(isa<VectorType>(Mask->getType()));
1168 
1169   Type *FromSTy = FromTy->getScalarType();
1170   Type *ToSTy = ToTy->getScalarType();
1171   if (FromSTy == ToSTy)
1172     return Mask;
1173 
1174   int FromSize = getSizeOf(FromSTy);
1175   int ToSize = getSizeOf(ToSTy);
1176   assert(FromSize % ToSize == 0 || ToSize % FromSize == 0);
1177 
1178   auto *MaskTy = cast<VectorType>(Mask->getType());
1179   int FromCount = MaskTy->getElementCount().getFixedValue();
1180   int ToCount = (FromCount * FromSize) / ToSize;
1181   assert((FromCount * FromSize) % ToSize == 0);
1182 
1183   auto *FromITy = IntegerType::get(F.getContext(), FromSize * 8);
1184   auto *ToITy = IntegerType::get(F.getContext(), ToSize * 8);
1185 
1186   // Mask <N x i1> -> sext to <N x FromTy> -> bitcast to <M x ToTy> ->
1187   // -> trunc to <M x i1>.
1188   Value *Ext = Builder.CreateSExt(
1189       Mask, VectorType::get(FromITy, FromCount, /*Scalable*/ false));
1190   Value *Cast = Builder.CreateBitCast(
1191       Ext, VectorType::get(ToITy, ToCount, /*Scalable*/ false));
1192   return Builder.CreateTrunc(
1193       Cast, VectorType::get(getBoolTy(), ToCount, /*Scalable*/ false));
1194 }
1195 
1196 // Bitcast to bytes, and return least significant bits.
1197 auto HexagonVectorCombine::vlsb(IRBuilder<> &Builder, Value *Val) const
1198     -> Value * {
1199   Type *ScalarTy = Val->getType()->getScalarType();
1200   if (ScalarTy == getBoolTy())
1201     return Val;
1202 
1203   Value *Bytes = vbytes(Builder, Val);
1204   if (auto *VecTy = dyn_cast<VectorType>(Bytes->getType()))
1205     return Builder.CreateTrunc(Bytes, getBoolTy(getSizeOf(VecTy)));
1206   // If Bytes is a scalar (i.e. Val was a scalar byte), return i1, not
1207   // <1 x i1>.
1208   return Builder.CreateTrunc(Bytes, getBoolTy());
1209 }
1210 
1211 // Bitcast to bytes for non-bool. For bool, convert i1 -> i8.
1212 auto HexagonVectorCombine::vbytes(IRBuilder<> &Builder, Value *Val) const
1213     -> Value * {
1214   Type *ScalarTy = Val->getType()->getScalarType();
1215   if (ScalarTy == getByteTy())
1216     return Val;
1217 
1218   if (ScalarTy != getBoolTy())
1219     return Builder.CreateBitCast(Val, getByteTy(getSizeOf(Val)));
1220   // For bool, return a sext from i1 to i8.
1221   if (auto *VecTy = dyn_cast<VectorType>(Val->getType()))
1222     return Builder.CreateSExt(Val, VectorType::get(getByteTy(), VecTy));
1223   return Builder.CreateSExt(Val, getByteTy());
1224 }
1225 
1226 auto HexagonVectorCombine::createHvxIntrinsic(IRBuilder<> &Builder,
1227                                               Intrinsic::ID IntID, Type *RetTy,
1228                                               ArrayRef<Value *> Args) const
1229     -> Value * {
1230   int HwLen = HST.getVectorLength();
1231   Type *BoolTy = Type::getInt1Ty(F.getContext());
1232   Type *Int32Ty = Type::getInt32Ty(F.getContext());
1233   // HVX vector -> v16i32/v32i32
1234   // HVX vector predicate -> v512i1/v1024i1
1235   auto getTypeForIntrin = [&](Type *Ty) -> Type * {
1236     if (HST.isTypeForHVX(Ty, /*IncludeBool*/ true)) {
1237       Type *ElemTy = cast<VectorType>(Ty)->getElementType();
1238       if (ElemTy == Int32Ty)
1239         return Ty;
1240       if (ElemTy == BoolTy)
1241         return VectorType::get(BoolTy, 8 * HwLen, /*Scalable*/ false);
1242       return VectorType::get(Int32Ty, HwLen / 4, /*Scalable*/ false);
1243     }
1244     // Non-HVX type. It should be a scalar.
1245     assert(Ty == Int32Ty || Ty->isIntegerTy(64));
1246     return Ty;
1247   };
1248 
1249   auto getCast = [&](IRBuilder<> &Builder, Value *Val,
1250                      Type *DestTy) -> Value * {
1251     Type *SrcTy = Val->getType();
1252     if (SrcTy == DestTy)
1253       return Val;
1254     if (HST.isTypeForHVX(SrcTy, /*IncludeBool*/ true)) {
1255       if (cast<VectorType>(SrcTy)->getElementType() == BoolTy) {
1256         // This should take care of casts the other way too, for example
1257         // v1024i1 -> v32i1.
1258         Intrinsic::ID TC = HwLen == 64
1259                                ? Intrinsic::hexagon_V6_pred_typecast
1260                                : Intrinsic::hexagon_V6_pred_typecast_128B;
1261         Function *FI = Intrinsic::getDeclaration(F.getParent(), TC,
1262                                                  {DestTy, Val->getType()});
1263         return Builder.CreateCall(FI, {Val});
1264       }
1265       // Non-predicate HVX vector.
1266       return Builder.CreateBitCast(Val, DestTy);
1267     }
1268     // Non-HVX type. It should be a scalar, and it should already have
1269     // a valid type.
1270     llvm_unreachable("Unexpected type");
1271   };
1272 
1273   SmallVector<Value *, 4> IntOps;
1274   for (Value *A : Args)
1275     IntOps.push_back(getCast(Builder, A, getTypeForIntrin(A->getType())));
1276   Function *FI = Intrinsic::getDeclaration(F.getParent(), IntID);
1277   Value *Call = Builder.CreateCall(FI, IntOps);
1278 
1279   Type *CallTy = Call->getType();
1280   if (CallTy == RetTy)
1281     return Call;
1282   // Scalar types should have RetTy matching the call return type.
1283   assert(HST.isTypeForHVX(CallTy, /*IncludeBool*/ true));
1284   if (cast<VectorType>(CallTy)->getElementType() == BoolTy)
1285     return getCast(Builder, Call, RetTy);
1286   return Builder.CreateBitCast(Call, RetTy);
1287 }
1288 
1289 auto HexagonVectorCombine::calculatePointerDifference(Value *Ptr0,
1290                                                       Value *Ptr1) const
1291     -> Optional<int> {
1292   struct Builder : IRBuilder<> {
1293     Builder(BasicBlock *B) : IRBuilder<>(B) {}
1294     ~Builder() {
1295       for (Instruction *I : llvm::reverse(ToErase))
1296         I->eraseFromParent();
1297     }
1298     SmallVector<Instruction *, 8> ToErase;
1299   };
1300 
1301 #define CallBuilder(B, F)                                                      \
1302   [&](auto &B_) {                                                              \
1303     Value *V = B_.F;                                                           \
1304     if (auto *I = dyn_cast<Instruction>(V))                                    \
1305       B_.ToErase.push_back(I);                                                 \
1306     return V;                                                                  \
1307   }(B)
1308 
1309   auto Simplify = [&](Value *V) {
1310     if (auto *I = dyn_cast<Instruction>(V)) {
1311       SimplifyQuery Q(DL, &TLI, &DT, &AC, I);
1312       if (Value *S = simplifyInstruction(I, Q))
1313         return S;
1314     }
1315     return V;
1316   };
1317 
1318   auto StripBitCast = [](Value *V) {
1319     while (auto *C = dyn_cast<BitCastInst>(V))
1320       V = C->getOperand(0);
1321     return V;
1322   };
1323 
1324   Ptr0 = StripBitCast(Ptr0);
1325   Ptr1 = StripBitCast(Ptr1);
1326   if (!isa<GetElementPtrInst>(Ptr0) || !isa<GetElementPtrInst>(Ptr1))
1327     return None;
1328 
1329   auto *Gep0 = cast<GetElementPtrInst>(Ptr0);
1330   auto *Gep1 = cast<GetElementPtrInst>(Ptr1);
1331   if (Gep0->getPointerOperand() != Gep1->getPointerOperand())
1332     return None;
1333 
1334   Builder B(Gep0->getParent());
1335   int Scale = getAllocSizeOf(Gep0->getSourceElementType());
1336 
1337   // FIXME: for now only check GEPs with a single index.
1338   if (Gep0->getNumOperands() != 2 || Gep1->getNumOperands() != 2)
1339     return None;
1340 
1341   Value *Idx0 = Gep0->getOperand(1);
1342   Value *Idx1 = Gep1->getOperand(1);
1343 
1344   // First, try to simplify the subtraction directly.
1345   if (auto *Diff = dyn_cast<ConstantInt>(
1346           Simplify(CallBuilder(B, CreateSub(Idx0, Idx1)))))
1347     return Diff->getSExtValue() * Scale;
1348 
1349   KnownBits Known0 = computeKnownBits(Idx0, DL, 0, &AC, Gep0, &DT);
1350   KnownBits Known1 = computeKnownBits(Idx1, DL, 0, &AC, Gep1, &DT);
1351   APInt Unknown = ~(Known0.Zero | Known0.One) | ~(Known1.Zero | Known1.One);
1352   if (Unknown.isAllOnes())
1353     return None;
1354 
1355   Value *MaskU = ConstantInt::get(Idx0->getType(), Unknown);
1356   Value *AndU0 = Simplify(CallBuilder(B, CreateAnd(Idx0, MaskU)));
1357   Value *AndU1 = Simplify(CallBuilder(B, CreateAnd(Idx1, MaskU)));
1358   Value *SubU = Simplify(CallBuilder(B, CreateSub(AndU0, AndU1)));
1359   int Diff0 = 0;
1360   if (auto *C = dyn_cast<ConstantInt>(SubU)) {
1361     Diff0 = C->getSExtValue();
1362   } else {
1363     return None;
1364   }
1365 
1366   Value *MaskK = ConstantInt::get(MaskU->getType(), ~Unknown);
1367   Value *AndK0 = Simplify(CallBuilder(B, CreateAnd(Idx0, MaskK)));
1368   Value *AndK1 = Simplify(CallBuilder(B, CreateAnd(Idx1, MaskK)));
1369   Value *SubK = Simplify(CallBuilder(B, CreateSub(AndK0, AndK1)));
1370   int Diff1 = 0;
1371   if (auto *C = dyn_cast<ConstantInt>(SubK)) {
1372     Diff1 = C->getSExtValue();
1373   } else {
1374     return None;
1375   }
1376 
1377   return (Diff0 + Diff1) * Scale;
1378 
1379 #undef CallBuilder
1380 }
1381 
1382 template <typename T>
1383 auto HexagonVectorCombine::isSafeToMoveBeforeInBB(const Instruction &In,
1384                                                   BasicBlock::const_iterator To,
1385                                                   const T &Ignore) const
1386     -> bool {
1387   auto getLocOrNone = [this](const Instruction &I) -> Optional<MemoryLocation> {
1388     if (const auto *II = dyn_cast<IntrinsicInst>(&I)) {
1389       switch (II->getIntrinsicID()) {
1390       case Intrinsic::masked_load:
1391         return MemoryLocation::getForArgument(II, 0, TLI);
1392       case Intrinsic::masked_store:
1393         return MemoryLocation::getForArgument(II, 1, TLI);
1394       }
1395     }
1396     return MemoryLocation::getOrNone(&I);
1397   };
1398 
1399   // The source and the destination must be in the same basic block.
1400   const BasicBlock &Block = *In.getParent();
1401   assert(Block.begin() == To || Block.end() == To || To->getParent() == &Block);
1402   // No PHIs.
1403   if (isa<PHINode>(In) || (To != Block.end() && isa<PHINode>(*To)))
1404     return false;
1405 
1406   if (!mayHaveNonDefUseDependency(In))
1407     return true;
1408   bool MayWrite = In.mayWriteToMemory();
1409   auto MaybeLoc = getLocOrNone(In);
1410 
1411   auto From = In.getIterator();
1412   if (From == To)
1413     return true;
1414   bool MoveUp = (To != Block.end() && To->comesBefore(&In));
1415   auto Range =
1416       MoveUp ? std::make_pair(To, From) : std::make_pair(std::next(From), To);
1417   for (auto It = Range.first; It != Range.second; ++It) {
1418     const Instruction &I = *It;
1419     if (llvm::is_contained(Ignore, &I))
1420       continue;
1421     // assume intrinsic can be ignored
1422     if (auto *II = dyn_cast<IntrinsicInst>(&I)) {
1423       if (II->getIntrinsicID() == Intrinsic::assume)
1424         continue;
1425     }
1426     // Parts based on isSafeToMoveBefore from CoveMoverUtils.cpp.
1427     if (I.mayThrow())
1428       return false;
1429     if (auto *CB = dyn_cast<CallBase>(&I)) {
1430       if (!CB->hasFnAttr(Attribute::WillReturn))
1431         return false;
1432       if (!CB->hasFnAttr(Attribute::NoSync))
1433         return false;
1434     }
1435     if (I.mayReadOrWriteMemory()) {
1436       auto MaybeLocI = getLocOrNone(I);
1437       if (MayWrite || I.mayWriteToMemory()) {
1438         if (!MaybeLoc || !MaybeLocI)
1439           return false;
1440         if (!AA.isNoAlias(*MaybeLoc, *MaybeLocI))
1441           return false;
1442       }
1443     }
1444   }
1445   return true;
1446 }
1447 
1448 #ifndef NDEBUG
1449 auto HexagonVectorCombine::isByteVecTy(Type *Ty) const -> bool {
1450   if (auto *VecTy = dyn_cast<VectorType>(Ty))
1451     return VecTy->getElementType() == getByteTy();
1452   return false;
1453 }
1454 
1455 auto HexagonVectorCombine::isSectorTy(Type *Ty) const -> bool {
1456   if (!isByteVecTy(Ty))
1457     return false;
1458   int Size = getSizeOf(Ty);
1459   if (HST.isTypeForHVX(Ty))
1460     return Size == static_cast<int>(HST.getVectorLength());
1461   return Size == 4 || Size == 8;
1462 }
1463 #endif
1464 
1465 auto HexagonVectorCombine::getElementRange(IRBuilder<> &Builder, Value *Lo,
1466                                            Value *Hi, int Start,
1467                                            int Length) const -> Value * {
1468   assert(0 <= Start && Start < Length);
1469   SmallVector<int, 128> SMask(Length);
1470   std::iota(SMask.begin(), SMask.end(), Start);
1471   return Builder.CreateShuffleVector(Lo, Hi, SMask);
1472 }
1473 
1474 // Pass management.
1475 
1476 namespace llvm {
1477 void initializeHexagonVectorCombineLegacyPass(PassRegistry &);
1478 FunctionPass *createHexagonVectorCombineLegacyPass();
1479 } // namespace llvm
1480 
1481 namespace {
1482 class HexagonVectorCombineLegacy : public FunctionPass {
1483 public:
1484   static char ID;
1485 
1486   HexagonVectorCombineLegacy() : FunctionPass(ID) {}
1487 
1488   StringRef getPassName() const override { return "Hexagon Vector Combine"; }
1489 
1490   void getAnalysisUsage(AnalysisUsage &AU) const override {
1491     AU.setPreservesCFG();
1492     AU.addRequired<AAResultsWrapperPass>();
1493     AU.addRequired<AssumptionCacheTracker>();
1494     AU.addRequired<DominatorTreeWrapperPass>();
1495     AU.addRequired<TargetLibraryInfoWrapperPass>();
1496     AU.addRequired<TargetPassConfig>();
1497     FunctionPass::getAnalysisUsage(AU);
1498   }
1499 
1500   bool runOnFunction(Function &F) override {
1501     if (skipFunction(F))
1502       return false;
1503     AliasAnalysis &AA = getAnalysis<AAResultsWrapperPass>().getAAResults();
1504     AssumptionCache &AC =
1505         getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
1506     DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
1507     TargetLibraryInfo &TLI =
1508         getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
1509     auto &TM = getAnalysis<TargetPassConfig>().getTM<HexagonTargetMachine>();
1510     HexagonVectorCombine HVC(F, AA, AC, DT, TLI, TM);
1511     return HVC.run();
1512   }
1513 };
1514 } // namespace
1515 
1516 char HexagonVectorCombineLegacy::ID = 0;
1517 
1518 INITIALIZE_PASS_BEGIN(HexagonVectorCombineLegacy, DEBUG_TYPE,
1519                       "Hexagon Vector Combine", false, false)
1520 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
1521 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
1522 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
1523 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
1524 INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
1525 INITIALIZE_PASS_END(HexagonVectorCombineLegacy, DEBUG_TYPE,
1526                     "Hexagon Vector Combine", false, false)
1527 
1528 FunctionPass *llvm::createHexagonVectorCombineLegacyPass() {
1529   return new HexagonVectorCombineLegacy();
1530 }
1531