xref: /llvm-project/llvm/lib/Target/Hexagon/HexagonVectorCombine.cpp (revision e5026f0179f2871ba9fefc489972643324a4ba4f)
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 // HvxIdioms: recognize various opportunities to generate HVX intrinsic code.
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/ADT/APInt.h"
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/ADT/DenseMap.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/InstSimplifyFolder.h"
23 #include "llvm/Analysis/InstructionSimplify.h"
24 #include "llvm/Analysis/ScalarEvolution.h"
25 #include "llvm/Analysis/TargetLibraryInfo.h"
26 #include "llvm/Analysis/ValueTracking.h"
27 #include "llvm/Analysis/VectorUtils.h"
28 #include "llvm/CodeGen/TargetPassConfig.h"
29 #include "llvm/CodeGen/ValueTypes.h"
30 #include "llvm/IR/Dominators.h"
31 #include "llvm/IR/IRBuilder.h"
32 #include "llvm/IR/IntrinsicInst.h"
33 #include "llvm/IR/Intrinsics.h"
34 #include "llvm/IR/IntrinsicsHexagon.h"
35 #include "llvm/IR/Metadata.h"
36 #include "llvm/IR/PatternMatch.h"
37 #include "llvm/InitializePasses.h"
38 #include "llvm/Pass.h"
39 #include "llvm/Support/CommandLine.h"
40 #include "llvm/Support/KnownBits.h"
41 #include "llvm/Support/MathExtras.h"
42 #include "llvm/Support/raw_ostream.h"
43 #include "llvm/Target/TargetMachine.h"
44 #include "llvm/Transforms/Utils/Local.h"
45 
46 #include "HexagonSubtarget.h"
47 #include "HexagonTargetMachine.h"
48 
49 #include <algorithm>
50 #include <deque>
51 #include <map>
52 #include <optional>
53 #include <set>
54 #include <utility>
55 #include <vector>
56 
57 #define DEBUG_TYPE "hexagon-vc"
58 
59 using namespace llvm;
60 
61 namespace {
62 cl::opt<bool> DumpModule("hvc-dump-module", cl::Hidden);
63 cl::opt<bool> VAEnabled("hvc-va", cl::Hidden, cl::init(true)); // Align
64 cl::opt<bool> VIEnabled("hvc-vi", cl::Hidden, cl::init(true)); // Idioms
65 cl::opt<bool> VADoFullStores("hvc-va-full-stores", cl::Hidden);
66 
67 cl::opt<unsigned> VAGroupCountLimit("hvc-va-group-count-limit", cl::Hidden,
68                                     cl::init(~0));
69 cl::opt<unsigned> VAGroupSizeLimit("hvc-va-group-size-limit", cl::Hidden,
70                                    cl::init(~0));
71 
72 class HexagonVectorCombine {
73 public:
74   HexagonVectorCombine(Function &F_, AliasAnalysis &AA_, AssumptionCache &AC_,
75                        DominatorTree &DT_, ScalarEvolution &SE_,
76                        TargetLibraryInfo &TLI_, const TargetMachine &TM_)
77       : F(F_), DL(F.getParent()->getDataLayout()), AA(AA_), AC(AC_), DT(DT_),
78         SE(SE_), TLI(TLI_),
79         HST(static_cast<const HexagonSubtarget &>(*TM_.getSubtargetImpl(F))) {}
80 
81   bool run();
82 
83   // Common integer type.
84   IntegerType *getIntTy(unsigned Width = 32) const;
85   // Byte type: either scalar (when Length = 0), or vector with given
86   // element count.
87   Type *getByteTy(int ElemCount = 0) const;
88   // Boolean type: either scalar (when Length = 0), or vector with given
89   // element count.
90   Type *getBoolTy(int ElemCount = 0) const;
91   // Create a ConstantInt of type returned by getIntTy with the value Val.
92   ConstantInt *getConstInt(int Val, unsigned Width = 32) const;
93   // Get the integer value of V, if it exists.
94   std::optional<APInt> getIntValue(const Value *Val) const;
95   // Is Val a constant 0, or a vector of 0s?
96   bool isZero(const Value *Val) const;
97   // Is Val an undef value?
98   bool isUndef(const Value *Val) const;
99   // Is Val a scalar (i1 true) or a vector of (i1 true)?
100   bool isTrue(const Value *Val) const;
101   // Is Val a scalar (i1 false) or a vector of (i1 false)?
102   bool isFalse(const Value *Val) const;
103 
104   // Get HVX vector type with the given element type.
105   VectorType *getHvxTy(Type *ElemTy, bool Pair = false) const;
106 
107   enum SizeKind {
108     Store, // Store size
109     Alloc, // Alloc size
110   };
111   int getSizeOf(const Value *Val, SizeKind Kind = Store) const;
112   int getSizeOf(const Type *Ty, SizeKind Kind = Store) const;
113   int getTypeAlignment(Type *Ty) const;
114   size_t length(Value *Val) const;
115   size_t length(Type *Ty) const;
116 
117   Constant *getNullValue(Type *Ty) const;
118   Constant *getFullValue(Type *Ty) const;
119   Constant *getConstSplat(Type *Ty, int Val) const;
120 
121   Value *simplify(Value *Val) const;
122 
123   Value *insertb(IRBuilderBase &Builder, Value *Dest, Value *Src, int Start,
124                  int Length, int Where) const;
125   Value *vlalignb(IRBuilderBase &Builder, Value *Lo, Value *Hi,
126                   Value *Amt) const;
127   Value *vralignb(IRBuilderBase &Builder, Value *Lo, Value *Hi,
128                   Value *Amt) const;
129   Value *concat(IRBuilderBase &Builder, ArrayRef<Value *> Vecs) const;
130   Value *vresize(IRBuilderBase &Builder, Value *Val, int NewSize,
131                  Value *Pad) const;
132   Value *rescale(IRBuilderBase &Builder, Value *Mask, Type *FromTy,
133                  Type *ToTy) const;
134   Value *vlsb(IRBuilderBase &Builder, Value *Val) const;
135   Value *vbytes(IRBuilderBase &Builder, Value *Val) const;
136   Value *subvector(IRBuilderBase &Builder, Value *Val, unsigned Start,
137                    unsigned Length) const;
138   Value *sublo(IRBuilderBase &Builder, Value *Val) const;
139   Value *subhi(IRBuilderBase &Builder, Value *Val) const;
140   Value *vdeal(IRBuilderBase &Builder, Value *Val0, Value *Val1) const;
141   Value *vshuff(IRBuilderBase &Builder, Value *Val0, Value *Val1) const;
142 
143   Value *createHvxIntrinsic(IRBuilderBase &Builder, Intrinsic::ID IntID,
144                             Type *RetTy, ArrayRef<Value *> Args,
145                             ArrayRef<Type *> ArgTys = std::nullopt,
146                             ArrayRef<Value *> MDSources = std::nullopt) const;
147   SmallVector<Value *> splitVectorElements(IRBuilderBase &Builder, Value *Vec,
148                                            unsigned ToWidth) const;
149   Value *joinVectorElements(IRBuilderBase &Builder, ArrayRef<Value *> Values,
150                             VectorType *ToType) const;
151 
152   std::optional<int> calculatePointerDifference(Value *Ptr0, Value *Ptr1) const;
153 
154   unsigned getNumSignificantBits(const Value *V,
155                                  const Instruction *CtxI = nullptr) const;
156   KnownBits getKnownBits(const Value *V,
157                          const Instruction *CtxI = nullptr) const;
158 
159   bool isSafeToClone(const Instruction &In) const;
160 
161   template <typename T = std::vector<Instruction *>>
162   bool isSafeToMoveBeforeInBB(const Instruction &In,
163                               BasicBlock::const_iterator To,
164                               const T &IgnoreInsts = {}) const;
165 
166   // This function is only used for assertions at the moment.
167   [[maybe_unused]] bool isByteVecTy(Type *Ty) const;
168 
169   Function &F;
170   const DataLayout &DL;
171   AliasAnalysis &AA;
172   AssumptionCache &AC;
173   DominatorTree &DT;
174   ScalarEvolution &SE;
175   TargetLibraryInfo &TLI;
176   const HexagonSubtarget &HST;
177 
178 private:
179   Value *getElementRange(IRBuilderBase &Builder, Value *Lo, Value *Hi,
180                          int Start, int Length) const;
181 };
182 
183 class AlignVectors {
184   // This code tries to replace unaligned vector loads/stores with aligned
185   // ones.
186   // Consider unaligned load:
187   //   %v = original_load %some_addr, align <bad>
188   //   %user = %v
189   // It will generate
190   //      = load ..., align <good>
191   //      = load ..., align <good>
192   //      = valign
193   //      etc.
194   //   %synthesize = combine/shuffle the loaded data so that it looks
195   //                 exactly like what "original_load" has loaded.
196   //   %user = %synthesize
197   // Similarly for stores.
198 public:
199   AlignVectors(const HexagonVectorCombine &HVC_) : HVC(HVC_) {}
200 
201   bool run();
202 
203 private:
204   using InstList = std::vector<Instruction *>;
205   using InstMap = DenseMap<Instruction *, Instruction *>;
206 
207   struct AddrInfo {
208     AddrInfo(const AddrInfo &) = default;
209     AddrInfo(const HexagonVectorCombine &HVC, Instruction *I, Value *A, Type *T,
210              Align H)
211         : Inst(I), Addr(A), ValTy(T), HaveAlign(H),
212           NeedAlign(HVC.getTypeAlignment(ValTy)) {}
213     AddrInfo &operator=(const AddrInfo &) = default;
214 
215     // XXX: add Size member?
216     Instruction *Inst;
217     Value *Addr;
218     Type *ValTy;
219     Align HaveAlign;
220     Align NeedAlign;
221     int Offset = 0; // Offset (in bytes) from the first member of the
222                     // containing AddrList.
223   };
224   using AddrList = std::vector<AddrInfo>;
225 
226   struct InstrLess {
227     bool operator()(const Instruction *A, const Instruction *B) const {
228       return A->comesBefore(B);
229     }
230   };
231   using DepList = std::set<Instruction *, InstrLess>;
232 
233   struct MoveGroup {
234     MoveGroup(const AddrInfo &AI, Instruction *B, bool Hvx, bool Load)
235         : Base(B), Main{AI.Inst}, Clones{}, IsHvx(Hvx), IsLoad(Load) {}
236     MoveGroup() = default;
237     Instruction *Base; // Base instruction of the parent address group.
238     InstList Main;     // Main group of instructions.
239     InstList Deps;     // List of dependencies.
240     InstMap Clones;    // Map from original Deps to cloned ones.
241     bool IsHvx;        // Is this group of HVX instructions?
242     bool IsLoad;       // Is this a load group?
243   };
244   using MoveList = std::vector<MoveGroup>;
245 
246   struct ByteSpan {
247     // A representation of "interesting" bytes within a given span of memory.
248     // These bytes are those that are loaded or stored, and they don't have
249     // to cover the entire span of memory.
250     //
251     // The representation works by picking a contiguous sequence of bytes
252     // from somewhere within a llvm::Value, and placing it at a given offset
253     // within the span.
254     //
255     // The sequence of bytes from llvm:Value is represented by Segment.
256     // Block is Segment, plus where it goes in the span.
257     //
258     // An important feature of ByteSpan is being able to make a "section",
259     // i.e. creating another ByteSpan corresponding to a range of offsets
260     // relative to the source span.
261 
262     struct Segment {
263       // Segment of a Value: 'Len' bytes starting at byte 'Begin'.
264       Segment(Value *Val, int Begin, int Len)
265           : Val(Val), Start(Begin), Size(Len) {}
266       Segment(const Segment &Seg) = default;
267       Segment &operator=(const Segment &Seg) = default;
268       Value *Val; // Value representable as a sequence of bytes.
269       int Start;  // First byte of the value that belongs to the segment.
270       int Size;   // Number of bytes in the segment.
271     };
272 
273     struct Block {
274       Block(Value *Val, int Len, int Pos) : Seg(Val, 0, Len), Pos(Pos) {}
275       Block(Value *Val, int Off, int Len, int Pos)
276           : Seg(Val, Off, Len), Pos(Pos) {}
277       Block(const Block &Blk) = default;
278       Block &operator=(const Block &Blk) = default;
279       Segment Seg; // Value segment.
280       int Pos;     // Position (offset) of the block in the span.
281     };
282 
283     int extent() const;
284     ByteSpan section(int Start, int Length) const;
285     ByteSpan &shift(int Offset);
286     SmallVector<Value *, 8> values() const;
287 
288     int size() const { return Blocks.size(); }
289     Block &operator[](int i) { return Blocks[i]; }
290     const Block &operator[](int i) const { return Blocks[i]; }
291 
292     std::vector<Block> Blocks;
293 
294     using iterator = decltype(Blocks)::iterator;
295     iterator begin() { return Blocks.begin(); }
296     iterator end() { return Blocks.end(); }
297     using const_iterator = decltype(Blocks)::const_iterator;
298     const_iterator begin() const { return Blocks.begin(); }
299     const_iterator end() const { return Blocks.end(); }
300   };
301 
302   Align getAlignFromValue(const Value *V) const;
303   std::optional<AddrInfo> getAddrInfo(Instruction &In) const;
304   bool isHvx(const AddrInfo &AI) const;
305   // This function is only used for assertions at the moment.
306   [[maybe_unused]] bool isSectorTy(Type *Ty) const;
307 
308   Value *getPayload(Value *Val) const;
309   Value *getMask(Value *Val) const;
310   Value *getPassThrough(Value *Val) const;
311 
312   Value *createAdjustedPointer(IRBuilderBase &Builder, Value *Ptr, Type *ValTy,
313                                int Adjust,
314                                const InstMap &CloneMap = InstMap()) const;
315   Value *createAlignedPointer(IRBuilderBase &Builder, Value *Ptr, Type *ValTy,
316                               int Alignment,
317                               const InstMap &CloneMap = InstMap()) const;
318 
319   Value *createLoad(IRBuilderBase &Builder, Type *ValTy, Value *Ptr,
320                     Value *Predicate, int Alignment, Value *Mask,
321                     Value *PassThru,
322                     ArrayRef<Value *> MDSources = std::nullopt) const;
323   Value *createSimpleLoad(IRBuilderBase &Builder, Type *ValTy, Value *Ptr,
324                           int Alignment,
325                           ArrayRef<Value *> MDSources = std::nullopt) const;
326 
327   Value *createStore(IRBuilderBase &Builder, Value *Val, Value *Ptr,
328                      Value *Predicate, int Alignment, Value *Mask,
329                      ArrayRef<Value *> MDSources = std ::nullopt) const;
330   Value *createSimpleStore(IRBuilderBase &Builder, Value *Val, Value *Ptr,
331                            int Alignment,
332                            ArrayRef<Value *> MDSources = std ::nullopt) const;
333 
334   Value *createPredicatedLoad(IRBuilderBase &Builder, Type *ValTy, Value *Ptr,
335                               Value *Predicate, int Alignment,
336                               ArrayRef<Value *> MDSources = std::nullopt) const;
337   Value *
338   createPredicatedStore(IRBuilderBase &Builder, Value *Val, Value *Ptr,
339                         Value *Predicate, int Alignment,
340                         ArrayRef<Value *> MDSources = std::nullopt) const;
341 
342   DepList getUpwardDeps(Instruction *In, Instruction *Base) const;
343   bool createAddressGroups();
344   MoveList createLoadGroups(const AddrList &Group) const;
345   MoveList createStoreGroups(const AddrList &Group) const;
346   bool moveTogether(MoveGroup &Move) const;
347   template <typename T> InstMap cloneBefore(Instruction *To, T &&Insts) const;
348 
349   void realignLoadGroup(IRBuilderBase &Builder, const ByteSpan &VSpan,
350                         int ScLen, Value *AlignVal, Value *AlignAddr) const;
351   void realignStoreGroup(IRBuilderBase &Builder, const ByteSpan &VSpan,
352                          int ScLen, Value *AlignVal, Value *AlignAddr) const;
353   bool realignGroup(const MoveGroup &Move) const;
354 
355   Value *makeTestIfUnaligned(IRBuilderBase &Builder, Value *AlignVal,
356                              int Alignment) const;
357 
358   friend raw_ostream &operator<<(raw_ostream &OS, const AddrInfo &AI);
359   friend raw_ostream &operator<<(raw_ostream &OS, const MoveGroup &MG);
360   friend raw_ostream &operator<<(raw_ostream &OS, const ByteSpan::Block &B);
361   friend raw_ostream &operator<<(raw_ostream &OS, const ByteSpan &BS);
362 
363   std::map<Instruction *, AddrList> AddrGroups;
364   const HexagonVectorCombine &HVC;
365 };
366 
367 LLVM_ATTRIBUTE_UNUSED
368 raw_ostream &operator<<(raw_ostream &OS, const AlignVectors::AddrInfo &AI) {
369   OS << "Inst: " << AI.Inst << "  " << *AI.Inst << '\n';
370   OS << "Addr: " << *AI.Addr << '\n';
371   OS << "Type: " << *AI.ValTy << '\n';
372   OS << "HaveAlign: " << AI.HaveAlign.value() << '\n';
373   OS << "NeedAlign: " << AI.NeedAlign.value() << '\n';
374   OS << "Offset: " << AI.Offset;
375   return OS;
376 }
377 
378 LLVM_ATTRIBUTE_UNUSED
379 raw_ostream &operator<<(raw_ostream &OS, const AlignVectors::MoveGroup &MG) {
380   OS << "IsLoad:" << (MG.IsLoad ? "yes" : "no");
381   OS << ", IsHvx:" << (MG.IsHvx ? "yes" : "no") << '\n';
382   OS << "Main\n";
383   for (Instruction *I : MG.Main)
384     OS << "  " << *I << '\n';
385   OS << "Deps\n";
386   for (Instruction *I : MG.Deps)
387     OS << "  " << *I << '\n';
388   OS << "Clones\n";
389   for (auto [K, V] : MG.Clones) {
390     OS << "    ";
391     K->printAsOperand(OS, false);
392     OS << "\t-> " << *V << '\n';
393   }
394   return OS;
395 }
396 
397 LLVM_ATTRIBUTE_UNUSED
398 raw_ostream &operator<<(raw_ostream &OS,
399                         const AlignVectors::ByteSpan::Block &B) {
400   OS << "  @" << B.Pos << " [" << B.Seg.Start << ',' << B.Seg.Size << "] ";
401   if (B.Seg.Val == reinterpret_cast<const Value *>(&B)) {
402     OS << "(self:" << B.Seg.Val << ')';
403   } else if (B.Seg.Val != nullptr) {
404     OS << *B.Seg.Val;
405   } else {
406     OS << "(null)";
407   }
408   return OS;
409 }
410 
411 LLVM_ATTRIBUTE_UNUSED
412 raw_ostream &operator<<(raw_ostream &OS, const AlignVectors::ByteSpan &BS) {
413   OS << "ByteSpan[size=" << BS.size() << ", extent=" << BS.extent() << '\n';
414   for (const AlignVectors::ByteSpan::Block &B : BS)
415     OS << B << '\n';
416   OS << ']';
417   return OS;
418 }
419 
420 class HvxIdioms {
421 public:
422   HvxIdioms(const HexagonVectorCombine &HVC_) : HVC(HVC_) {
423     auto *Int32Ty = HVC.getIntTy(32);
424     HvxI32Ty = HVC.getHvxTy(Int32Ty, /*Pair=*/false);
425     HvxP32Ty = HVC.getHvxTy(Int32Ty, /*Pair=*/true);
426   }
427 
428   bool run();
429 
430 private:
431   enum Signedness { Positive, Signed, Unsigned };
432 
433   // Value + sign
434   // This is to keep track of whether the value should be treated as signed
435   // or unsigned, or is known to be positive.
436   struct SValue {
437     Value *Val;
438     Signedness Sgn;
439   };
440 
441   struct FxpOp {
442     unsigned Opcode;
443     unsigned Frac; // Number of fraction bits
444     SValue X, Y;
445     // If present, add 1 << RoundAt before shift:
446     std::optional<unsigned> RoundAt;
447     VectorType *ResTy;
448   };
449 
450   auto getNumSignificantBits(Value *V, Instruction *In) const
451       -> std::pair<unsigned, Signedness>;
452   auto canonSgn(SValue X, SValue Y) const -> std::pair<SValue, SValue>;
453 
454   auto matchFxpMul(Instruction &In) const -> std::optional<FxpOp>;
455   auto processFxpMul(Instruction &In, const FxpOp &Op) const -> Value *;
456 
457   auto processFxpMulChopped(IRBuilderBase &Builder, Instruction &In,
458                             const FxpOp &Op) const -> Value *;
459   auto createMulQ15(IRBuilderBase &Builder, SValue X, SValue Y,
460                     bool Rounding) const -> Value *;
461   auto createMulQ31(IRBuilderBase &Builder, SValue X, SValue Y,
462                     bool Rounding) const -> Value *;
463   // Return {Result, Carry}, where Carry is a vector predicate.
464   auto createAddCarry(IRBuilderBase &Builder, Value *X, Value *Y,
465                       Value *CarryIn = nullptr) const
466       -> std::pair<Value *, Value *>;
467   auto createMul16(IRBuilderBase &Builder, SValue X, SValue Y) const -> Value *;
468   auto createMulH16(IRBuilderBase &Builder, SValue X, SValue Y) const
469       -> Value *;
470   auto createMul32(IRBuilderBase &Builder, SValue X, SValue Y) const
471       -> std::pair<Value *, Value *>;
472   auto createAddLong(IRBuilderBase &Builder, ArrayRef<Value *> WordX,
473                      ArrayRef<Value *> WordY) const -> SmallVector<Value *>;
474   auto createMulLong(IRBuilderBase &Builder, ArrayRef<Value *> WordX,
475                      Signedness SgnX, ArrayRef<Value *> WordY,
476                      Signedness SgnY) const -> SmallVector<Value *>;
477 
478   VectorType *HvxI32Ty;
479   VectorType *HvxP32Ty;
480   const HexagonVectorCombine &HVC;
481 
482   friend raw_ostream &operator<<(raw_ostream &, const FxpOp &);
483 };
484 
485 [[maybe_unused]] raw_ostream &operator<<(raw_ostream &OS,
486                                          const HvxIdioms::FxpOp &Op) {
487   static const char *SgnNames[] = {"Positive", "Signed", "Unsigned"};
488   OS << Instruction::getOpcodeName(Op.Opcode) << '.' << Op.Frac;
489   if (Op.RoundAt.has_value()) {
490     if (Op.Frac != 0 && *Op.RoundAt == Op.Frac - 1) {
491       OS << ":rnd";
492     } else {
493       OS << " + 1<<" << *Op.RoundAt;
494     }
495   }
496   OS << "\n  X:(" << SgnNames[Op.X.Sgn] << ") " << *Op.X.Val << "\n"
497      << "  Y:(" << SgnNames[Op.Y.Sgn] << ") " << *Op.Y.Val;
498   return OS;
499 }
500 
501 } // namespace
502 
503 namespace {
504 
505 template <typename T> T *getIfUnordered(T *MaybeT) {
506   return MaybeT && MaybeT->isUnordered() ? MaybeT : nullptr;
507 }
508 template <typename T> T *isCandidate(Instruction *In) {
509   return dyn_cast<T>(In);
510 }
511 template <> LoadInst *isCandidate<LoadInst>(Instruction *In) {
512   return getIfUnordered(dyn_cast<LoadInst>(In));
513 }
514 template <> StoreInst *isCandidate<StoreInst>(Instruction *In) {
515   return getIfUnordered(dyn_cast<StoreInst>(In));
516 }
517 
518 #if !defined(_MSC_VER) || _MSC_VER >= 1926
519 // VS2017 and some versions of VS2019 have trouble compiling this:
520 // error C2976: 'std::map': too few template arguments
521 // VS 2019 16.x is known to work, except for 16.4/16.5 (MSC_VER 1924/1925)
522 template <typename Pred, typename... Ts>
523 void erase_if(std::map<Ts...> &map, Pred p)
524 #else
525 template <typename Pred, typename T, typename U>
526 void erase_if(std::map<T, U> &map, Pred p)
527 #endif
528 {
529   for (auto i = map.begin(), e = map.end(); i != e;) {
530     if (p(*i))
531       i = map.erase(i);
532     else
533       i = std::next(i);
534   }
535 }
536 
537 // Forward other erase_ifs to the LLVM implementations.
538 template <typename Pred, typename T> void erase_if(T &&container, Pred p) {
539   llvm::erase_if(std::forward<T>(container), p);
540 }
541 
542 } // namespace
543 
544 // --- Begin AlignVectors
545 
546 // For brevity, only consider loads. We identify a group of loads where we
547 // know the relative differences between their addresses, so we know how they
548 // are laid out in memory (relative to one another). These loads can overlap,
549 // can be shorter or longer than the desired vector length.
550 // Ultimately we want to generate a sequence of aligned loads that will load
551 // every byte that the original loads loaded, and have the program use these
552 // loaded values instead of the original loads.
553 // We consider the contiguous memory area spanned by all these loads.
554 //
555 // Let's say that a single aligned vector load can load 16 bytes at a time.
556 // If the program wanted to use a byte at offset 13 from the beginning of the
557 // original span, it will be a byte at offset 13+x in the aligned data for
558 // some x>=0. This may happen to be in the first aligned load, or in the load
559 // following it. Since we generally don't know what the that alignment value
560 // is at compile time, we proactively do valigns on the aligned loads, so that
561 // byte that was at offset 13 is still at offset 13 after the valigns.
562 //
563 // This will be the starting point for making the rest of the program use the
564 // data loaded by the new loads.
565 // For each original load, and its users:
566 //   %v = load ...
567 //   ... = %v
568 //   ... = %v
569 // we create
570 //   %new_v = extract/combine/shuffle data from loaded/valigned vectors so
571 //            it contains the same value as %v did before
572 // then replace all users of %v with %new_v.
573 //   ... = %new_v
574 //   ... = %new_v
575 
576 auto AlignVectors::ByteSpan::extent() const -> int {
577   if (size() == 0)
578     return 0;
579   int Min = Blocks[0].Pos;
580   int Max = Blocks[0].Pos + Blocks[0].Seg.Size;
581   for (int i = 1, e = size(); i != e; ++i) {
582     Min = std::min(Min, Blocks[i].Pos);
583     Max = std::max(Max, Blocks[i].Pos + Blocks[i].Seg.Size);
584   }
585   return Max - Min;
586 }
587 
588 auto AlignVectors::ByteSpan::section(int Start, int Length) const -> ByteSpan {
589   ByteSpan Section;
590   for (const ByteSpan::Block &B : Blocks) {
591     int L = std::max(B.Pos, Start);                       // Left end.
592     int R = std::min(B.Pos + B.Seg.Size, Start + Length); // Right end+1.
593     if (L < R) {
594       // How much to chop off the beginning of the segment:
595       int Off = L > B.Pos ? L - B.Pos : 0;
596       Section.Blocks.emplace_back(B.Seg.Val, B.Seg.Start + Off, R - L, L);
597     }
598   }
599   return Section;
600 }
601 
602 auto AlignVectors::ByteSpan::shift(int Offset) -> ByteSpan & {
603   for (Block &B : Blocks)
604     B.Pos += Offset;
605   return *this;
606 }
607 
608 auto AlignVectors::ByteSpan::values() const -> SmallVector<Value *, 8> {
609   SmallVector<Value *, 8> Values(Blocks.size());
610   for (int i = 0, e = Blocks.size(); i != e; ++i)
611     Values[i] = Blocks[i].Seg.Val;
612   return Values;
613 }
614 
615 auto AlignVectors::getAlignFromValue(const Value *V) const -> Align {
616   const auto *C = dyn_cast<ConstantInt>(V);
617   assert(C && "Alignment must be a compile-time constant integer");
618   return C->getAlignValue();
619 }
620 
621 auto AlignVectors::getAddrInfo(Instruction &In) const
622     -> std::optional<AddrInfo> {
623   if (auto *L = isCandidate<LoadInst>(&In))
624     return AddrInfo(HVC, L, L->getPointerOperand(), L->getType(),
625                     L->getAlign());
626   if (auto *S = isCandidate<StoreInst>(&In))
627     return AddrInfo(HVC, S, S->getPointerOperand(),
628                     S->getValueOperand()->getType(), S->getAlign());
629   if (auto *II = isCandidate<IntrinsicInst>(&In)) {
630     Intrinsic::ID ID = II->getIntrinsicID();
631     switch (ID) {
632     case Intrinsic::masked_load:
633       return AddrInfo(HVC, II, II->getArgOperand(0), II->getType(),
634                       getAlignFromValue(II->getArgOperand(1)));
635     case Intrinsic::masked_store:
636       return AddrInfo(HVC, II, II->getArgOperand(1),
637                       II->getArgOperand(0)->getType(),
638                       getAlignFromValue(II->getArgOperand(2)));
639     }
640   }
641   return std::nullopt;
642 }
643 
644 auto AlignVectors::isHvx(const AddrInfo &AI) const -> bool {
645   return HVC.HST.isTypeForHVX(AI.ValTy);
646 }
647 
648 auto AlignVectors::getPayload(Value *Val) const -> Value * {
649   if (auto *In = dyn_cast<Instruction>(Val)) {
650     Intrinsic::ID ID = 0;
651     if (auto *II = dyn_cast<IntrinsicInst>(In))
652       ID = II->getIntrinsicID();
653     if (isa<StoreInst>(In) || ID == Intrinsic::masked_store)
654       return In->getOperand(0);
655   }
656   return Val;
657 }
658 
659 auto AlignVectors::getMask(Value *Val) const -> Value * {
660   if (auto *II = dyn_cast<IntrinsicInst>(Val)) {
661     switch (II->getIntrinsicID()) {
662     case Intrinsic::masked_load:
663       return II->getArgOperand(2);
664     case Intrinsic::masked_store:
665       return II->getArgOperand(3);
666     }
667   }
668 
669   Type *ValTy = getPayload(Val)->getType();
670   if (auto *VecTy = dyn_cast<VectorType>(ValTy))
671     return HVC.getFullValue(HVC.getBoolTy(HVC.length(VecTy)));
672   return HVC.getFullValue(HVC.getBoolTy());
673 }
674 
675 auto AlignVectors::getPassThrough(Value *Val) const -> Value * {
676   if (auto *II = dyn_cast<IntrinsicInst>(Val)) {
677     if (II->getIntrinsicID() == Intrinsic::masked_load)
678       return II->getArgOperand(3);
679   }
680   return UndefValue::get(getPayload(Val)->getType());
681 }
682 
683 auto AlignVectors::createAdjustedPointer(IRBuilderBase &Builder, Value *Ptr,
684                                          Type *ValTy, int Adjust,
685                                          const InstMap &CloneMap) const
686     -> Value * {
687   if (auto *I = dyn_cast<Instruction>(Ptr))
688     if (Instruction *New = CloneMap.lookup(I))
689       Ptr = New;
690   return Builder.CreateGEP(Type::getInt8Ty(HVC.F.getContext()), Ptr,
691                            HVC.getConstInt(Adjust), "gep");
692 }
693 
694 auto AlignVectors::createAlignedPointer(IRBuilderBase &Builder, Value *Ptr,
695                                         Type *ValTy, int Alignment,
696                                         const InstMap &CloneMap) const
697     -> Value * {
698   auto remap = [&](Value *V) -> Value * {
699     if (auto *I = dyn_cast<Instruction>(V)) {
700       for (auto [Old, New] : CloneMap)
701         I->replaceUsesOfWith(Old, New);
702       return I;
703     }
704     return V;
705   };
706   Value *AsInt = Builder.CreatePtrToInt(Ptr, HVC.getIntTy(), "pti");
707   Value *Mask = HVC.getConstInt(-Alignment);
708   Value *And = Builder.CreateAnd(remap(AsInt), Mask, "and");
709   return Builder.CreateIntToPtr(
710       And, PointerType::getUnqual(ValTy->getContext()), "itp");
711 }
712 
713 auto AlignVectors::createLoad(IRBuilderBase &Builder, Type *ValTy, Value *Ptr,
714                               Value *Predicate, int Alignment, Value *Mask,
715                               Value *PassThru,
716                               ArrayRef<Value *> MDSources) const -> Value * {
717   bool HvxHasPredLoad = HVC.HST.useHVXV62Ops();
718   // Predicate is nullptr if not creating predicated load
719   if (Predicate) {
720     assert(!Predicate->getType()->isVectorTy() &&
721            "Expectning scalar predicate");
722     if (HVC.isFalse(Predicate))
723       return UndefValue::get(ValTy);
724     if (!HVC.isTrue(Predicate) && HvxHasPredLoad) {
725       Value *Load = createPredicatedLoad(Builder, ValTy, Ptr, Predicate,
726                                          Alignment, MDSources);
727       return Builder.CreateSelect(Mask, Load, PassThru);
728     }
729     // Predicate == true here.
730   }
731   assert(!HVC.isUndef(Mask)); // Should this be allowed?
732   if (HVC.isZero(Mask))
733     return PassThru;
734   if (HVC.isTrue(Mask))
735     return createSimpleLoad(Builder, ValTy, Ptr, Alignment, MDSources);
736 
737   Instruction *Load = Builder.CreateMaskedLoad(ValTy, Ptr, Align(Alignment),
738                                                Mask, PassThru, "mld");
739   propagateMetadata(Load, MDSources);
740   return Load;
741 }
742 
743 auto AlignVectors::createSimpleLoad(IRBuilderBase &Builder, Type *ValTy,
744                                     Value *Ptr, int Alignment,
745                                     ArrayRef<Value *> MDSources) const
746     -> Value * {
747   Instruction *Load =
748       Builder.CreateAlignedLoad(ValTy, Ptr, Align(Alignment), "ald");
749   propagateMetadata(Load, MDSources);
750   return Load;
751 }
752 
753 auto AlignVectors::createPredicatedLoad(IRBuilderBase &Builder, Type *ValTy,
754                                         Value *Ptr, Value *Predicate,
755                                         int Alignment,
756                                         ArrayRef<Value *> MDSources) const
757     -> Value * {
758   assert(HVC.HST.isTypeForHVX(ValTy) &&
759          "Predicates 'scalar' vector loads not yet supported");
760   assert(Predicate);
761   assert(!Predicate->getType()->isVectorTy() && "Expectning scalar predicate");
762   assert(HVC.getSizeOf(ValTy, HVC.Alloc) % Alignment == 0);
763   if (HVC.isFalse(Predicate))
764     return UndefValue::get(ValTy);
765   if (HVC.isTrue(Predicate))
766     return createSimpleLoad(Builder, ValTy, Ptr, Alignment, MDSources);
767 
768   auto V6_vL32b_pred_ai = HVC.HST.getIntrinsicId(Hexagon::V6_vL32b_pred_ai);
769   // FIXME: This may not put the offset from Ptr into the vmem offset.
770   return HVC.createHvxIntrinsic(Builder, V6_vL32b_pred_ai, ValTy,
771                                 {Predicate, Ptr, HVC.getConstInt(0)},
772                                 std::nullopt, MDSources);
773 }
774 
775 auto AlignVectors::createStore(IRBuilderBase &Builder, Value *Val, Value *Ptr,
776                                Value *Predicate, int Alignment, Value *Mask,
777                                ArrayRef<Value *> MDSources) const -> Value * {
778   if (HVC.isZero(Mask) || HVC.isUndef(Val) || HVC.isUndef(Mask))
779     return UndefValue::get(Val->getType());
780   assert(!Predicate || (!Predicate->getType()->isVectorTy() &&
781                         "Expectning scalar predicate"));
782   if (Predicate) {
783     if (HVC.isFalse(Predicate))
784       return UndefValue::get(Val->getType());
785     if (HVC.isTrue(Predicate))
786       Predicate = nullptr;
787   }
788   // Here both Predicate and Mask are true or unknown.
789 
790   if (HVC.isTrue(Mask)) {
791     if (Predicate) { // Predicate unknown
792       return createPredicatedStore(Builder, Val, Ptr, Predicate, Alignment,
793                                    MDSources);
794     }
795     // Predicate is true:
796     return createSimpleStore(Builder, Val, Ptr, Alignment, MDSources);
797   }
798 
799   // Mask is unknown
800   if (!Predicate) {
801     Instruction *Store =
802         Builder.CreateMaskedStore(Val, Ptr, Align(Alignment), Mask);
803     propagateMetadata(Store, MDSources);
804     return Store;
805   }
806 
807   // Both Predicate and Mask are unknown.
808   // Emulate masked store with predicated-load + mux + predicated-store.
809   Value *PredLoad = createPredicatedLoad(Builder, Val->getType(), Ptr,
810                                          Predicate, Alignment, MDSources);
811   Value *Mux = Builder.CreateSelect(Mask, Val, PredLoad);
812   return createPredicatedStore(Builder, Mux, Ptr, Predicate, Alignment,
813                                MDSources);
814 }
815 
816 auto AlignVectors::createSimpleStore(IRBuilderBase &Builder, Value *Val,
817                                      Value *Ptr, int Alignment,
818                                      ArrayRef<Value *> MDSources) const
819     -> Value * {
820   Instruction *Store = Builder.CreateAlignedStore(Val, Ptr, Align(Alignment));
821   propagateMetadata(Store, MDSources);
822   return Store;
823 }
824 
825 auto AlignVectors::createPredicatedStore(IRBuilderBase &Builder, Value *Val,
826                                          Value *Ptr, Value *Predicate,
827                                          int Alignment,
828                                          ArrayRef<Value *> MDSources) const
829     -> Value * {
830   assert(HVC.HST.isTypeForHVX(Val->getType()) &&
831          "Predicates 'scalar' vector stores not yet supported");
832   assert(Predicate);
833   if (HVC.isFalse(Predicate))
834     return UndefValue::get(Val->getType());
835   if (HVC.isTrue(Predicate))
836     return createSimpleStore(Builder, Val, Ptr, Alignment, MDSources);
837 
838   assert(HVC.getSizeOf(Val, HVC.Alloc) % Alignment == 0);
839   auto V6_vS32b_pred_ai = HVC.HST.getIntrinsicId(Hexagon::V6_vS32b_pred_ai);
840   // FIXME: This may not put the offset from Ptr into the vmem offset.
841   return HVC.createHvxIntrinsic(Builder, V6_vS32b_pred_ai, nullptr,
842                                 {Predicate, Ptr, HVC.getConstInt(0), Val},
843                                 std::nullopt, MDSources);
844 }
845 
846 auto AlignVectors::getUpwardDeps(Instruction *In, Instruction *Base) const
847     -> DepList {
848   BasicBlock *Parent = Base->getParent();
849   assert(In->getParent() == Parent &&
850          "Base and In should be in the same block");
851   assert(Base->comesBefore(In) && "Base should come before In");
852 
853   DepList Deps;
854   std::deque<Instruction *> WorkQ = {In};
855   while (!WorkQ.empty()) {
856     Instruction *D = WorkQ.front();
857     WorkQ.pop_front();
858     if (D != In)
859       Deps.insert(D);
860     for (Value *Op : D->operands()) {
861       if (auto *I = dyn_cast<Instruction>(Op)) {
862         if (I->getParent() == Parent && Base->comesBefore(I))
863           WorkQ.push_back(I);
864       }
865     }
866   }
867   return Deps;
868 }
869 
870 auto AlignVectors::createAddressGroups() -> bool {
871   // An address group created here may contain instructions spanning
872   // multiple basic blocks.
873   AddrList WorkStack;
874 
875   auto findBaseAndOffset = [&](AddrInfo &AI) -> std::pair<Instruction *, int> {
876     for (AddrInfo &W : WorkStack) {
877       if (auto D = HVC.calculatePointerDifference(AI.Addr, W.Addr))
878         return std::make_pair(W.Inst, *D);
879     }
880     return std::make_pair(nullptr, 0);
881   };
882 
883   auto traverseBlock = [&](DomTreeNode *DomN, auto Visit) -> void {
884     BasicBlock &Block = *DomN->getBlock();
885     for (Instruction &I : Block) {
886       auto AI = this->getAddrInfo(I); // Use this-> for gcc6.
887       if (!AI)
888         continue;
889       auto F = findBaseAndOffset(*AI);
890       Instruction *GroupInst;
891       if (Instruction *BI = F.first) {
892         AI->Offset = F.second;
893         GroupInst = BI;
894       } else {
895         WorkStack.push_back(*AI);
896         GroupInst = AI->Inst;
897       }
898       AddrGroups[GroupInst].push_back(*AI);
899     }
900 
901     for (DomTreeNode *C : DomN->children())
902       Visit(C, Visit);
903 
904     while (!WorkStack.empty() && WorkStack.back().Inst->getParent() == &Block)
905       WorkStack.pop_back();
906   };
907 
908   traverseBlock(HVC.DT.getRootNode(), traverseBlock);
909   assert(WorkStack.empty());
910 
911   // AddrGroups are formed.
912 
913   // Remove groups of size 1.
914   erase_if(AddrGroups, [](auto &G) { return G.second.size() == 1; });
915   // Remove groups that don't use HVX types.
916   erase_if(AddrGroups, [&](auto &G) {
917     return llvm::none_of(
918         G.second, [&](auto &I) { return HVC.HST.isTypeForHVX(I.ValTy); });
919   });
920 
921   return !AddrGroups.empty();
922 }
923 
924 auto AlignVectors::createLoadGroups(const AddrList &Group) const -> MoveList {
925   // Form load groups.
926   // To avoid complications with moving code across basic blocks, only form
927   // groups that are contained within a single basic block.
928   unsigned SizeLimit = VAGroupSizeLimit;
929   if (SizeLimit == 0)
930     return {};
931 
932   auto tryAddTo = [&](const AddrInfo &Info, MoveGroup &Move) {
933     assert(!Move.Main.empty() && "Move group should have non-empty Main");
934     if (Move.Main.size() >= SizeLimit)
935       return false;
936     // Don't mix HVX and non-HVX instructions.
937     if (Move.IsHvx != isHvx(Info))
938       return false;
939     // Leading instruction in the load group.
940     Instruction *Base = Move.Main.front();
941     if (Base->getParent() != Info.Inst->getParent())
942       return false;
943     // Check if it's safe to move the load.
944     if (!HVC.isSafeToMoveBeforeInBB(*Info.Inst, Base->getIterator()))
945       return false;
946     // And if it's safe to clone the dependencies.
947     auto isSafeToCopyAtBase = [&](const Instruction *I) {
948       return HVC.isSafeToMoveBeforeInBB(*I, Base->getIterator()) &&
949              HVC.isSafeToClone(*I);
950     };
951     DepList Deps = getUpwardDeps(Info.Inst, Base);
952     if (!llvm::all_of(Deps, isSafeToCopyAtBase))
953       return false;
954 
955     Move.Main.push_back(Info.Inst);
956     llvm::append_range(Move.Deps, Deps);
957     return true;
958   };
959 
960   MoveList LoadGroups;
961 
962   for (const AddrInfo &Info : Group) {
963     if (!Info.Inst->mayReadFromMemory())
964       continue;
965     if (LoadGroups.empty() || !tryAddTo(Info, LoadGroups.back()))
966       LoadGroups.emplace_back(Info, Group.front().Inst, isHvx(Info), true);
967   }
968 
969   // Erase singleton groups.
970   erase_if(LoadGroups, [](const MoveGroup &G) { return G.Main.size() <= 1; });
971 
972   // Erase HVX groups on targets < HvxV62 (due to lack of predicated loads).
973   if (!HVC.HST.useHVXV62Ops())
974     erase_if(LoadGroups, [](const MoveGroup &G) { return G.IsHvx; });
975 
976   return LoadGroups;
977 }
978 
979 auto AlignVectors::createStoreGroups(const AddrList &Group) const -> MoveList {
980   // Form store groups.
981   // To avoid complications with moving code across basic blocks, only form
982   // groups that are contained within a single basic block.
983   unsigned SizeLimit = VAGroupSizeLimit;
984   if (SizeLimit == 0)
985     return {};
986 
987   auto tryAddTo = [&](const AddrInfo &Info, MoveGroup &Move) {
988     assert(!Move.Main.empty() && "Move group should have non-empty Main");
989     if (Move.Main.size() >= SizeLimit)
990       return false;
991     // For stores with return values we'd have to collect downward depenencies.
992     // There are no such stores that we handle at the moment, so omit that.
993     assert(Info.Inst->getType()->isVoidTy() &&
994            "Not handling stores with return values");
995     // Don't mix HVX and non-HVX instructions.
996     if (Move.IsHvx != isHvx(Info))
997       return false;
998     // For stores we need to be careful whether it's safe to move them.
999     // Stores that are otherwise safe to move together may not appear safe
1000     // to move over one another (i.e. isSafeToMoveBefore may return false).
1001     Instruction *Base = Move.Main.front();
1002     if (Base->getParent() != Info.Inst->getParent())
1003       return false;
1004     if (!HVC.isSafeToMoveBeforeInBB(*Info.Inst, Base->getIterator(), Move.Main))
1005       return false;
1006     Move.Main.push_back(Info.Inst);
1007     return true;
1008   };
1009 
1010   MoveList StoreGroups;
1011 
1012   for (auto I = Group.rbegin(), E = Group.rend(); I != E; ++I) {
1013     const AddrInfo &Info = *I;
1014     if (!Info.Inst->mayWriteToMemory())
1015       continue;
1016     if (StoreGroups.empty() || !tryAddTo(Info, StoreGroups.back()))
1017       StoreGroups.emplace_back(Info, Group.front().Inst, isHvx(Info), false);
1018   }
1019 
1020   // Erase singleton groups.
1021   erase_if(StoreGroups, [](const MoveGroup &G) { return G.Main.size() <= 1; });
1022 
1023   // Erase HVX groups on targets < HvxV62 (due to lack of predicated loads).
1024   if (!HVC.HST.useHVXV62Ops())
1025     erase_if(StoreGroups, [](const MoveGroup &G) { return G.IsHvx; });
1026 
1027   // Erase groups where every store is a full HVX vector. The reason is that
1028   // aligning predicated stores generates complex code that may be less
1029   // efficient than a sequence of unaligned vector stores.
1030   if (!VADoFullStores) {
1031     erase_if(StoreGroups, [this](const MoveGroup &G) {
1032       return G.IsHvx && llvm::all_of(G.Main, [this](Instruction *S) {
1033                auto MaybeInfo = this->getAddrInfo(*S);
1034                assert(MaybeInfo.has_value());
1035                return HVC.HST.isHVXVectorType(
1036                    EVT::getEVT(MaybeInfo->ValTy, false));
1037              });
1038     });
1039   }
1040 
1041   return StoreGroups;
1042 }
1043 
1044 auto AlignVectors::moveTogether(MoveGroup &Move) const -> bool {
1045   // Move all instructions to be adjacent.
1046   assert(!Move.Main.empty() && "Move group should have non-empty Main");
1047   Instruction *Where = Move.Main.front();
1048 
1049   if (Move.IsLoad) {
1050     // Move all the loads (and dependencies) to where the first load is.
1051     // Clone all deps to before Where, keeping order.
1052     Move.Clones = cloneBefore(Where, Move.Deps);
1053     // Move all main instructions to after Where, keeping order.
1054     ArrayRef<Instruction *> Main(Move.Main);
1055     for (Instruction *M : Main) {
1056       if (M != Where)
1057         M->moveAfter(Where);
1058       for (auto [Old, New] : Move.Clones)
1059         M->replaceUsesOfWith(Old, New);
1060       Where = M;
1061     }
1062     // Replace Deps with the clones.
1063     for (int i = 0, e = Move.Deps.size(); i != e; ++i)
1064       Move.Deps[i] = Move.Clones[Move.Deps[i]];
1065   } else {
1066     // Move all the stores to where the last store is.
1067     // NOTE: Deps are empty for "store" groups. If they need to be
1068     // non-empty, decide on the order.
1069     assert(Move.Deps.empty());
1070     // Move all main instructions to before Where, inverting order.
1071     ArrayRef<Instruction *> Main(Move.Main);
1072     for (Instruction *M : Main.drop_front(1)) {
1073       M->moveBefore(Where);
1074       Where = M;
1075     }
1076   }
1077 
1078   return Move.Main.size() + Move.Deps.size() > 1;
1079 }
1080 
1081 template <typename T>
1082 auto AlignVectors::cloneBefore(Instruction *To, T &&Insts) const -> InstMap {
1083   InstMap Map;
1084 
1085   for (Instruction *I : Insts) {
1086     assert(HVC.isSafeToClone(*I));
1087     Instruction *C = I->clone();
1088     C->setName(Twine("c.") + I->getName() + ".");
1089     C->insertBefore(To);
1090 
1091     for (auto [Old, New] : Map)
1092       C->replaceUsesOfWith(Old, New);
1093     Map.insert(std::make_pair(I, C));
1094   }
1095   return Map;
1096 }
1097 
1098 auto AlignVectors::realignLoadGroup(IRBuilderBase &Builder,
1099                                     const ByteSpan &VSpan, int ScLen,
1100                                     Value *AlignVal, Value *AlignAddr) const
1101     -> void {
1102   LLVM_DEBUG(dbgs() << __func__ << "\n");
1103 
1104   Type *SecTy = HVC.getByteTy(ScLen);
1105   int NumSectors = (VSpan.extent() + ScLen - 1) / ScLen;
1106   bool DoAlign = !HVC.isZero(AlignVal);
1107   BasicBlock::iterator BasePos = Builder.GetInsertPoint();
1108   BasicBlock *BaseBlock = Builder.GetInsertBlock();
1109 
1110   ByteSpan ASpan;
1111   auto *True = HVC.getFullValue(HVC.getBoolTy(ScLen));
1112   auto *Undef = UndefValue::get(SecTy);
1113 
1114   // Created load does not have to be "Instruction" (e.g. "undef").
1115   SmallVector<Value *> Loads(NumSectors + DoAlign, nullptr);
1116 
1117   // We could create all of the aligned loads, and generate the valigns
1118   // at the location of the first load, but for large load groups, this
1119   // could create highly suboptimal code (there have been groups of 140+
1120   // loads in real code).
1121   // Instead, place the loads/valigns as close to the users as possible.
1122   // In any case we need to have a mapping from the blocks of VSpan (the
1123   // span covered by the pre-existing loads) to ASpan (the span covered
1124   // by the aligned loads). There is a small problem, though: ASpan needs
1125   // to have pointers to the loads/valigns, but we don't have these loads
1126   // because we don't know where to put them yet. We find out by creating
1127   // a section of ASpan that corresponds to values (blocks) from VSpan,
1128   // and checking where the new load should be placed. We need to attach
1129   // this location information to each block in ASpan somehow, so we put
1130   // distincts values for Seg.Val in each ASpan.Blocks[i], and use a map
1131   // to store the location for each Seg.Val.
1132   // The distinct values happen to be Blocks[i].Seg.Val = &Blocks[i],
1133   // which helps with printing ByteSpans without crashing when printing
1134   // Segments with these temporary identifiers in place of Val.
1135 
1136   // Populate the blocks first, to avoid reallocations of the vector
1137   // interfering with generating the placeholder addresses.
1138   for (int Index = 0; Index != NumSectors; ++Index)
1139     ASpan.Blocks.emplace_back(nullptr, ScLen, Index * ScLen);
1140   for (int Index = 0; Index != NumSectors; ++Index) {
1141     ASpan.Blocks[Index].Seg.Val =
1142         reinterpret_cast<Value *>(&ASpan.Blocks[Index]);
1143   }
1144 
1145   // Multiple values from VSpan can map to the same value in ASpan. Since we
1146   // try to create loads lazily, we need to find the earliest use for each
1147   // value from ASpan.
1148   DenseMap<void *, Instruction *> EarliestUser;
1149   auto isEarlier = [](Instruction *A, Instruction *B) {
1150     if (B == nullptr)
1151       return true;
1152     if (A == nullptr)
1153       return false;
1154     assert(A->getParent() == B->getParent());
1155     return A->comesBefore(B);
1156   };
1157   auto earliestUser = [&](const auto &Uses) {
1158     Instruction *User = nullptr;
1159     for (const Use &U : Uses) {
1160       auto *I = dyn_cast<Instruction>(U.getUser());
1161       assert(I != nullptr && "Load used in a non-instruction?");
1162       // Make sure we only consider users in this block, but we need
1163       // to remember if there were users outside the block too. This is
1164       // because if no users are found, aligned loads will not be created.
1165       if (I->getParent() == BaseBlock) {
1166         if (!isa<PHINode>(I))
1167           User = std::min(User, I, isEarlier);
1168       } else {
1169         User = std::min(User, BaseBlock->getTerminator(), isEarlier);
1170       }
1171     }
1172     return User;
1173   };
1174 
1175   for (const ByteSpan::Block &B : VSpan) {
1176     ByteSpan ASection = ASpan.section(B.Pos, B.Seg.Size);
1177     for (const ByteSpan::Block &S : ASection) {
1178       EarliestUser[S.Seg.Val] = std::min(
1179           EarliestUser[S.Seg.Val], earliestUser(B.Seg.Val->uses()), isEarlier);
1180     }
1181   }
1182 
1183   LLVM_DEBUG({
1184     dbgs() << "ASpan:\n" << ASpan << '\n';
1185     dbgs() << "Earliest users of ASpan:\n";
1186     for (auto &[Val, User] : EarliestUser) {
1187       dbgs() << Val << "\n ->" << *User << '\n';
1188     }
1189   });
1190 
1191   auto createLoad = [&](IRBuilderBase &Builder, const ByteSpan &VSpan,
1192                         int Index, bool MakePred) {
1193     Value *Ptr =
1194         createAdjustedPointer(Builder, AlignAddr, SecTy, Index * ScLen);
1195     Value *Predicate =
1196         MakePred ? makeTestIfUnaligned(Builder, AlignVal, ScLen) : nullptr;
1197 
1198     // If vector shifting is potentially needed, accumulate metadata
1199     // from source sections of twice the load width.
1200     int Start = (Index - DoAlign) * ScLen;
1201     int Width = (1 + DoAlign) * ScLen;
1202     return this->createLoad(Builder, SecTy, Ptr, Predicate, ScLen, True, Undef,
1203                             VSpan.section(Start, Width).values());
1204   };
1205 
1206   auto moveBefore = [this](Instruction *In, Instruction *To) {
1207     // Move In and its upward dependencies to before To.
1208     assert(In->getParent() == To->getParent());
1209     DepList Deps = getUpwardDeps(In, To);
1210     In->moveBefore(To);
1211     // DepList is sorted with respect to positions in the basic block.
1212     InstMap Map = cloneBefore(In, Deps);
1213     for (auto [Old, New] : Map)
1214       In->replaceUsesOfWith(Old, New);
1215   };
1216 
1217   // Generate necessary loads at appropriate locations.
1218   LLVM_DEBUG(dbgs() << "Creating loads for ASpan sectors\n");
1219   for (int Index = 0; Index != NumSectors + 1; ++Index) {
1220     // In ASpan, each block will be either a single aligned load, or a
1221     // valign of a pair of loads. In the latter case, an aligned load j
1222     // will belong to the current valign, and the one in the previous
1223     // block (for j > 0).
1224     // Place the load at a location which will dominate the valign, assuming
1225     // the valign will be placed right before the earliest user.
1226     Instruction *PrevAt =
1227         DoAlign && Index > 0 ? EarliestUser[&ASpan[Index - 1]] : nullptr;
1228     Instruction *ThisAt =
1229         Index < NumSectors ? EarliestUser[&ASpan[Index]] : nullptr;
1230     if (auto *Where = std::min(PrevAt, ThisAt, isEarlier)) {
1231       Builder.SetInsertPoint(Where);
1232       Loads[Index] =
1233           createLoad(Builder, VSpan, Index, DoAlign && Index == NumSectors);
1234       // We know it's safe to put the load at BasePos, but we'd prefer to put
1235       // it at "Where". To see if the load is safe to be placed at Where, put
1236       // it there first and then check if it's safe to move it to BasePos.
1237       // If not, then the load needs to be placed at BasePos.
1238       // We can't do this check proactively because we need the load to exist
1239       // in order to check legality.
1240       if (auto *Load = dyn_cast<Instruction>(Loads[Index])) {
1241         if (!HVC.isSafeToMoveBeforeInBB(*Load, BasePos))
1242           moveBefore(Load, &*BasePos);
1243       }
1244       LLVM_DEBUG(dbgs() << "Loads[" << Index << "]:" << *Loads[Index] << '\n');
1245     }
1246   }
1247 
1248   // Generate valigns if needed, and fill in proper values in ASpan
1249   LLVM_DEBUG(dbgs() << "Creating values for ASpan sectors\n");
1250   for (int Index = 0; Index != NumSectors; ++Index) {
1251     ASpan[Index].Seg.Val = nullptr;
1252     if (auto *Where = EarliestUser[&ASpan[Index]]) {
1253       Builder.SetInsertPoint(Where);
1254       Value *Val = Loads[Index];
1255       assert(Val != nullptr);
1256       if (DoAlign) {
1257         Value *NextLoad = Loads[Index + 1];
1258         assert(NextLoad != nullptr);
1259         Val = HVC.vralignb(Builder, Val, NextLoad, AlignVal);
1260       }
1261       ASpan[Index].Seg.Val = Val;
1262       LLVM_DEBUG(dbgs() << "ASpan[" << Index << "]:" << *Val << '\n');
1263     }
1264   }
1265 
1266   for (const ByteSpan::Block &B : VSpan) {
1267     ByteSpan ASection = ASpan.section(B.Pos, B.Seg.Size).shift(-B.Pos);
1268     Value *Accum = UndefValue::get(HVC.getByteTy(B.Seg.Size));
1269     Builder.SetInsertPoint(cast<Instruction>(B.Seg.Val));
1270 
1271     // We're generating a reduction, where each instruction depends on
1272     // the previous one, so we need to order them according to the position
1273     // of their inputs in the code.
1274     std::vector<ByteSpan::Block *> ABlocks;
1275     for (ByteSpan::Block &S : ASection) {
1276       if (S.Seg.Val != nullptr)
1277         ABlocks.push_back(&S);
1278     }
1279     llvm::sort(ABlocks,
1280                [&](const ByteSpan::Block *A, const ByteSpan::Block *B) {
1281                  return isEarlier(cast<Instruction>(A->Seg.Val),
1282                                   cast<Instruction>(B->Seg.Val));
1283                });
1284     for (ByteSpan::Block *S : ABlocks) {
1285       // The processing of the data loaded by the aligned loads
1286       // needs to be inserted after the data is available.
1287       Instruction *SegI = cast<Instruction>(S->Seg.Val);
1288       Builder.SetInsertPoint(&*std::next(SegI->getIterator()));
1289       Value *Pay = HVC.vbytes(Builder, getPayload(S->Seg.Val));
1290       Accum =
1291           HVC.insertb(Builder, Accum, Pay, S->Seg.Start, S->Seg.Size, S->Pos);
1292     }
1293     // Instead of casting everything to bytes for the vselect, cast to the
1294     // original value type. This will avoid complications with casting masks.
1295     // For example, in cases when the original mask applied to i32, it could
1296     // be converted to a mask applicable to i8 via pred_typecast intrinsic,
1297     // but if the mask is not exactly of HVX length, extra handling would be
1298     // needed to make it work.
1299     Type *ValTy = getPayload(B.Seg.Val)->getType();
1300     Value *Cast = Builder.CreateBitCast(Accum, ValTy, "cst");
1301     Value *Sel = Builder.CreateSelect(getMask(B.Seg.Val), Cast,
1302                                       getPassThrough(B.Seg.Val), "sel");
1303     B.Seg.Val->replaceAllUsesWith(Sel);
1304   }
1305 }
1306 
1307 auto AlignVectors::realignStoreGroup(IRBuilderBase &Builder,
1308                                      const ByteSpan &VSpan, int ScLen,
1309                                      Value *AlignVal, Value *AlignAddr) const
1310     -> void {
1311   LLVM_DEBUG(dbgs() << __func__ << "\n");
1312 
1313   Type *SecTy = HVC.getByteTy(ScLen);
1314   int NumSectors = (VSpan.extent() + ScLen - 1) / ScLen;
1315   bool DoAlign = !HVC.isZero(AlignVal);
1316 
1317   // Stores.
1318   ByteSpan ASpanV, ASpanM;
1319 
1320   // Return a vector value corresponding to the input value Val:
1321   // either <1 x Val> for scalar Val, or Val itself for vector Val.
1322   auto MakeVec = [](IRBuilderBase &Builder, Value *Val) -> Value * {
1323     Type *Ty = Val->getType();
1324     if (Ty->isVectorTy())
1325       return Val;
1326     auto *VecTy = VectorType::get(Ty, 1, /*Scalable=*/false);
1327     return Builder.CreateBitCast(Val, VecTy, "cst");
1328   };
1329 
1330   // Create an extra "undef" sector at the beginning and at the end.
1331   // They will be used as the left/right filler in the vlalign step.
1332   for (int Index = (DoAlign ? -1 : 0); Index != NumSectors + DoAlign; ++Index) {
1333     // For stores, the size of each section is an aligned vector length.
1334     // Adjust the store offsets relative to the section start offset.
1335     ByteSpan VSection =
1336         VSpan.section(Index * ScLen, ScLen).shift(-Index * ScLen);
1337     Value *Undef = UndefValue::get(SecTy);
1338     Value *Zero = HVC.getNullValue(SecTy);
1339     Value *AccumV = Undef;
1340     Value *AccumM = Zero;
1341     for (ByteSpan::Block &S : VSection) {
1342       Value *Pay = getPayload(S.Seg.Val);
1343       Value *Mask = HVC.rescale(Builder, MakeVec(Builder, getMask(S.Seg.Val)),
1344                                 Pay->getType(), HVC.getByteTy());
1345       Value *PartM = HVC.insertb(Builder, Zero, HVC.vbytes(Builder, Mask),
1346                                  S.Seg.Start, S.Seg.Size, S.Pos);
1347       AccumM = Builder.CreateOr(AccumM, PartM);
1348 
1349       Value *PartV = HVC.insertb(Builder, Undef, HVC.vbytes(Builder, Pay),
1350                                  S.Seg.Start, S.Seg.Size, S.Pos);
1351 
1352       AccumV = Builder.CreateSelect(
1353           Builder.CreateICmp(CmpInst::ICMP_NE, PartM, Zero), PartV, AccumV);
1354     }
1355     ASpanV.Blocks.emplace_back(AccumV, ScLen, Index * ScLen);
1356     ASpanM.Blocks.emplace_back(AccumM, ScLen, Index * ScLen);
1357   }
1358 
1359   LLVM_DEBUG({
1360     dbgs() << "ASpanV before vlalign:\n" << ASpanV << '\n';
1361     dbgs() << "ASpanM before vlalign:\n" << ASpanM << '\n';
1362   });
1363 
1364   // vlalign
1365   if (DoAlign) {
1366     for (int Index = 1; Index != NumSectors + 2; ++Index) {
1367       Value *PrevV = ASpanV[Index - 1].Seg.Val, *ThisV = ASpanV[Index].Seg.Val;
1368       Value *PrevM = ASpanM[Index - 1].Seg.Val, *ThisM = ASpanM[Index].Seg.Val;
1369       assert(isSectorTy(PrevV->getType()) && isSectorTy(PrevM->getType()));
1370       ASpanV[Index - 1].Seg.Val = HVC.vlalignb(Builder, PrevV, ThisV, AlignVal);
1371       ASpanM[Index - 1].Seg.Val = HVC.vlalignb(Builder, PrevM, ThisM, AlignVal);
1372     }
1373   }
1374 
1375   LLVM_DEBUG({
1376     dbgs() << "ASpanV after vlalign:\n" << ASpanV << '\n';
1377     dbgs() << "ASpanM after vlalign:\n" << ASpanM << '\n';
1378   });
1379 
1380   auto createStore = [&](IRBuilderBase &Builder, const ByteSpan &ASpanV,
1381                          const ByteSpan &ASpanM, int Index, bool MakePred) {
1382     Value *Val = ASpanV[Index].Seg.Val;
1383     Value *Mask = ASpanM[Index].Seg.Val; // bytes
1384     if (HVC.isUndef(Val) || HVC.isZero(Mask))
1385       return;
1386     Value *Ptr =
1387         createAdjustedPointer(Builder, AlignAddr, SecTy, Index * ScLen);
1388     Value *Predicate =
1389         MakePred ? makeTestIfUnaligned(Builder, AlignVal, ScLen) : nullptr;
1390 
1391     // If vector shifting is potentially needed, accumulate metadata
1392     // from source sections of twice the store width.
1393     int Start = (Index - DoAlign) * ScLen;
1394     int Width = (1 + DoAlign) * ScLen;
1395     this->createStore(Builder, Val, Ptr, Predicate, ScLen,
1396                       HVC.vlsb(Builder, Mask),
1397                       VSpan.section(Start, Width).values());
1398   };
1399 
1400   for (int Index = 0; Index != NumSectors + DoAlign; ++Index) {
1401     createStore(Builder, ASpanV, ASpanM, Index, DoAlign && Index == NumSectors);
1402   }
1403 }
1404 
1405 auto AlignVectors::realignGroup(const MoveGroup &Move) const -> bool {
1406   LLVM_DEBUG(dbgs() << "Realigning group:\n" << Move << '\n');
1407 
1408   // TODO: Needs support for masked loads/stores of "scalar" vectors.
1409   if (!Move.IsHvx)
1410     return false;
1411 
1412   // Return the element with the maximum alignment from Range,
1413   // where GetValue obtains the value to compare from an element.
1414   auto getMaxOf = [](auto Range, auto GetValue) {
1415     return *std::max_element(
1416         Range.begin(), Range.end(),
1417         [&GetValue](auto &A, auto &B) { return GetValue(A) < GetValue(B); });
1418   };
1419 
1420   const AddrList &BaseInfos = AddrGroups.at(Move.Base);
1421 
1422   // Conceptually, there is a vector of N bytes covering the addresses
1423   // starting from the minimum offset (i.e. Base.Addr+Start). This vector
1424   // represents a contiguous memory region that spans all accessed memory
1425   // locations.
1426   // The correspondence between loaded or stored values will be expressed
1427   // in terms of this vector. For example, the 0th element of the vector
1428   // from the Base address info will start at byte Start from the beginning
1429   // of this conceptual vector.
1430   //
1431   // This vector will be loaded/stored starting at the nearest down-aligned
1432   // address and the amount od the down-alignment will be AlignVal:
1433   //   valign(load_vector(align_down(Base+Start)), AlignVal)
1434 
1435   std::set<Instruction *> TestSet(Move.Main.begin(), Move.Main.end());
1436   AddrList MoveInfos;
1437   llvm::copy_if(
1438       BaseInfos, std::back_inserter(MoveInfos),
1439       [&TestSet](const AddrInfo &AI) { return TestSet.count(AI.Inst); });
1440 
1441   // Maximum alignment present in the whole address group.
1442   const AddrInfo &WithMaxAlign =
1443       getMaxOf(MoveInfos, [](const AddrInfo &AI) { return AI.HaveAlign; });
1444   Align MaxGiven = WithMaxAlign.HaveAlign;
1445 
1446   // Minimum alignment present in the move address group.
1447   const AddrInfo &WithMinOffset =
1448       getMaxOf(MoveInfos, [](const AddrInfo &AI) { return -AI.Offset; });
1449 
1450   const AddrInfo &WithMaxNeeded =
1451       getMaxOf(MoveInfos, [](const AddrInfo &AI) { return AI.NeedAlign; });
1452   Align MinNeeded = WithMaxNeeded.NeedAlign;
1453 
1454   // Set the builder's insertion point right before the load group, or
1455   // immediately after the store group. (Instructions in a store group are
1456   // listed in reverse order.)
1457   Instruction *InsertAt = Move.Main.front();
1458   if (!Move.IsLoad) {
1459     // There should be a terminator (which store isn't, but check anyways).
1460     assert(InsertAt->getIterator() != InsertAt->getParent()->end());
1461     InsertAt = &*std::next(InsertAt->getIterator());
1462   }
1463 
1464   IRBuilder Builder(InsertAt->getParent(), InsertAt->getIterator(),
1465                     InstSimplifyFolder(HVC.DL));
1466   Value *AlignAddr = nullptr; // Actual aligned address.
1467   Value *AlignVal = nullptr;  // Right-shift amount (for valign).
1468 
1469   if (MinNeeded <= MaxGiven) {
1470     int Start = WithMinOffset.Offset;
1471     int OffAtMax = WithMaxAlign.Offset;
1472     // Shift the offset of the maximally aligned instruction (OffAtMax)
1473     // back by just enough multiples of the required alignment to cover the
1474     // distance from Start to OffAtMax.
1475     // Calculate the address adjustment amount based on the address with the
1476     // maximum alignment. This is to allow a simple gep instruction instead
1477     // of potential bitcasts to i8*.
1478     int Adjust = -alignTo(OffAtMax - Start, MinNeeded.value());
1479     AlignAddr = createAdjustedPointer(Builder, WithMaxAlign.Addr,
1480                                       WithMaxAlign.ValTy, Adjust, Move.Clones);
1481     int Diff = Start - (OffAtMax + Adjust);
1482     AlignVal = HVC.getConstInt(Diff);
1483     assert(Diff >= 0);
1484     assert(static_cast<decltype(MinNeeded.value())>(Diff) < MinNeeded.value());
1485   } else {
1486     // WithMinOffset is the lowest address in the group,
1487     //   WithMinOffset.Addr = Base+Start.
1488     // Align instructions for both HVX (V6_valign) and scalar (S2_valignrb)
1489     // mask off unnecessary bits, so it's ok to just the original pointer as
1490     // the alignment amount.
1491     // Do an explicit down-alignment of the address to avoid creating an
1492     // aligned instruction with an address that is not really aligned.
1493     AlignAddr =
1494         createAlignedPointer(Builder, WithMinOffset.Addr, WithMinOffset.ValTy,
1495                              MinNeeded.value(), Move.Clones);
1496     AlignVal =
1497         Builder.CreatePtrToInt(WithMinOffset.Addr, HVC.getIntTy(), "pti");
1498     if (auto *I = dyn_cast<Instruction>(AlignVal)) {
1499       for (auto [Old, New] : Move.Clones)
1500         I->replaceUsesOfWith(Old, New);
1501     }
1502   }
1503 
1504   ByteSpan VSpan;
1505   for (const AddrInfo &AI : MoveInfos) {
1506     VSpan.Blocks.emplace_back(AI.Inst, HVC.getSizeOf(AI.ValTy),
1507                               AI.Offset - WithMinOffset.Offset);
1508   }
1509 
1510   // The aligned loads/stores will use blocks that are either scalars,
1511   // or HVX vectors. Let "sector" be the unified term for such a block.
1512   // blend(scalar, vector) -> sector...
1513   int ScLen = Move.IsHvx ? HVC.HST.getVectorLength()
1514                          : std::max<int>(MinNeeded.value(), 4);
1515   assert(!Move.IsHvx || ScLen == 64 || ScLen == 128);
1516   assert(Move.IsHvx || ScLen == 4 || ScLen == 8);
1517 
1518   LLVM_DEBUG({
1519     dbgs() << "ScLen:  " << ScLen << "\n";
1520     dbgs() << "AlignVal:" << *AlignVal << "\n";
1521     dbgs() << "AlignAddr:" << *AlignAddr << "\n";
1522     dbgs() << "VSpan:\n" << VSpan << '\n';
1523   });
1524 
1525   if (Move.IsLoad)
1526     realignLoadGroup(Builder, VSpan, ScLen, AlignVal, AlignAddr);
1527   else
1528     realignStoreGroup(Builder, VSpan, ScLen, AlignVal, AlignAddr);
1529 
1530   for (auto *Inst : Move.Main)
1531     Inst->eraseFromParent();
1532 
1533   return true;
1534 }
1535 
1536 auto AlignVectors::makeTestIfUnaligned(IRBuilderBase &Builder, Value *AlignVal,
1537                                        int Alignment) const -> Value * {
1538   auto *AlignTy = AlignVal->getType();
1539   Value *And = Builder.CreateAnd(
1540       AlignVal, ConstantInt::get(AlignTy, Alignment - 1), "and");
1541   Value *Zero = ConstantInt::get(AlignTy, 0);
1542   return Builder.CreateICmpNE(And, Zero, "isz");
1543 }
1544 
1545 auto AlignVectors::isSectorTy(Type *Ty) const -> bool {
1546   if (!HVC.isByteVecTy(Ty))
1547     return false;
1548   int Size = HVC.getSizeOf(Ty);
1549   if (HVC.HST.isTypeForHVX(Ty))
1550     return Size == static_cast<int>(HVC.HST.getVectorLength());
1551   return Size == 4 || Size == 8;
1552 }
1553 
1554 auto AlignVectors::run() -> bool {
1555   LLVM_DEBUG(dbgs() << "Running HVC::AlignVectors on " << HVC.F.getName()
1556                     << '\n');
1557   if (!createAddressGroups())
1558     return false;
1559 
1560   LLVM_DEBUG({
1561     dbgs() << "Address groups(" << AddrGroups.size() << "):\n";
1562     for (auto &[In, AL] : AddrGroups) {
1563       for (const AddrInfo &AI : AL)
1564         dbgs() << "---\n" << AI << '\n';
1565     }
1566   });
1567 
1568   bool Changed = false;
1569   MoveList LoadGroups, StoreGroups;
1570 
1571   for (auto &G : AddrGroups) {
1572     llvm::append_range(LoadGroups, createLoadGroups(G.second));
1573     llvm::append_range(StoreGroups, createStoreGroups(G.second));
1574   }
1575 
1576   LLVM_DEBUG({
1577     dbgs() << "\nLoad groups(" << LoadGroups.size() << "):\n";
1578     for (const MoveGroup &G : LoadGroups)
1579       dbgs() << G << "\n";
1580     dbgs() << "Store groups(" << StoreGroups.size() << "):\n";
1581     for (const MoveGroup &G : StoreGroups)
1582       dbgs() << G << "\n";
1583   });
1584 
1585   // Cumulative limit on the number of groups.
1586   unsigned CountLimit = VAGroupCountLimit;
1587   if (CountLimit == 0)
1588     return false;
1589 
1590   if (LoadGroups.size() > CountLimit) {
1591     LoadGroups.resize(CountLimit);
1592     StoreGroups.clear();
1593   } else {
1594     unsigned StoreLimit = CountLimit - LoadGroups.size();
1595     if (StoreGroups.size() > StoreLimit)
1596       StoreGroups.resize(StoreLimit);
1597   }
1598 
1599   for (auto &M : LoadGroups)
1600     Changed |= moveTogether(M);
1601   for (auto &M : StoreGroups)
1602     Changed |= moveTogether(M);
1603 
1604   LLVM_DEBUG(dbgs() << "After moveTogether:\n" << HVC.F);
1605 
1606   for (auto &M : LoadGroups)
1607     Changed |= realignGroup(M);
1608   for (auto &M : StoreGroups)
1609     Changed |= realignGroup(M);
1610 
1611   return Changed;
1612 }
1613 
1614 // --- End AlignVectors
1615 
1616 // --- Begin HvxIdioms
1617 
1618 auto HvxIdioms::getNumSignificantBits(Value *V, Instruction *In) const
1619     -> std::pair<unsigned, Signedness> {
1620   unsigned Bits = HVC.getNumSignificantBits(V, In);
1621   // The significant bits are calculated including the sign bit. This may
1622   // add an extra bit for zero-extended values, e.g. (zext i32 to i64) may
1623   // result in 33 significant bits. To avoid extra words, skip the extra
1624   // sign bit, but keep information that the value is to be treated as
1625   // unsigned.
1626   KnownBits Known = HVC.getKnownBits(V, In);
1627   Signedness Sign = Signed;
1628   unsigned NumToTest = 0; // Number of bits used in test for unsignedness.
1629   if (isPowerOf2_32(Bits))
1630     NumToTest = Bits;
1631   else if (Bits > 1 && isPowerOf2_32(Bits - 1))
1632     NumToTest = Bits - 1;
1633 
1634   if (NumToTest != 0 && Known.Zero.ashr(NumToTest).isAllOnes()) {
1635     Sign = Unsigned;
1636     Bits = NumToTest;
1637   }
1638 
1639   // If the top bit of the nearest power-of-2 is zero, this value is
1640   // positive. It could be treated as either signed or unsigned.
1641   if (unsigned Pow2 = PowerOf2Ceil(Bits); Pow2 != Bits) {
1642     if (Known.Zero.ashr(Pow2 - 1).isAllOnes())
1643       Sign = Positive;
1644   }
1645   return {Bits, Sign};
1646 }
1647 
1648 auto HvxIdioms::canonSgn(SValue X, SValue Y) const
1649     -> std::pair<SValue, SValue> {
1650   // Canonicalize the signedness of X and Y, so that the result is one of:
1651   //   S, S
1652   //   U/P, S
1653   //   U/P, U/P
1654   if (X.Sgn == Signed && Y.Sgn != Signed)
1655     std::swap(X, Y);
1656   return {X, Y};
1657 }
1658 
1659 // Match
1660 //   (X * Y) [>> N], or
1661 //   ((X * Y) + (1 << M)) >> N
1662 auto HvxIdioms::matchFxpMul(Instruction &In) const -> std::optional<FxpOp> {
1663   using namespace PatternMatch;
1664   auto *Ty = In.getType();
1665 
1666   if (!Ty->isVectorTy() || !Ty->getScalarType()->isIntegerTy())
1667     return std::nullopt;
1668 
1669   unsigned Width = cast<IntegerType>(Ty->getScalarType())->getBitWidth();
1670 
1671   FxpOp Op;
1672   Value *Exp = &In;
1673 
1674   // Fixed-point multiplication is always shifted right (except when the
1675   // fraction is 0 bits).
1676   auto m_Shr = [](auto &&V, auto &&S) {
1677     return m_CombineOr(m_LShr(V, S), m_AShr(V, S));
1678   };
1679 
1680   const APInt *Qn = nullptr;
1681   if (Value * T; match(Exp, m_Shr(m_Value(T), m_APInt(Qn)))) {
1682     Op.Frac = Qn->getZExtValue();
1683     Exp = T;
1684   } else {
1685     Op.Frac = 0;
1686   }
1687 
1688   if (Op.Frac > Width)
1689     return std::nullopt;
1690 
1691   // Check if there is rounding added.
1692   const APInt *C = nullptr;
1693   if (Value * T; Op.Frac > 0 && match(Exp, m_Add(m_Value(T), m_APInt(C)))) {
1694     uint64_t CV = C->getZExtValue();
1695     if (CV != 0 && !isPowerOf2_64(CV))
1696       return std::nullopt;
1697     if (CV != 0)
1698       Op.RoundAt = Log2_64(CV);
1699     Exp = T;
1700   }
1701 
1702   // Check if the rest is a multiplication.
1703   if (match(Exp, m_Mul(m_Value(Op.X.Val), m_Value(Op.Y.Val)))) {
1704     Op.Opcode = Instruction::Mul;
1705     // FIXME: The information below is recomputed.
1706     Op.X.Sgn = getNumSignificantBits(Op.X.Val, &In).second;
1707     Op.Y.Sgn = getNumSignificantBits(Op.Y.Val, &In).second;
1708     Op.ResTy = cast<VectorType>(Ty);
1709     return Op;
1710   }
1711 
1712   return std::nullopt;
1713 }
1714 
1715 auto HvxIdioms::processFxpMul(Instruction &In, const FxpOp &Op) const
1716     -> Value * {
1717   assert(Op.X.Val->getType() == Op.Y.Val->getType());
1718 
1719   auto *VecTy = dyn_cast<VectorType>(Op.X.Val->getType());
1720   if (VecTy == nullptr)
1721     return nullptr;
1722   auto *ElemTy = cast<IntegerType>(VecTy->getElementType());
1723   unsigned ElemWidth = ElemTy->getBitWidth();
1724 
1725   // TODO: This can be relaxed after legalization is done pre-isel.
1726   if ((HVC.length(VecTy) * ElemWidth) % (8 * HVC.HST.getVectorLength()) != 0)
1727     return nullptr;
1728 
1729   // There are no special intrinsics that should be used for multiplying
1730   // signed 8-bit values, so just skip them. Normal codegen should handle
1731   // this just fine.
1732   if (ElemWidth <= 8)
1733     return nullptr;
1734   // Similarly, if this is just a multiplication that can be handled without
1735   // intervention, then leave it alone.
1736   if (ElemWidth <= 32 && Op.Frac == 0)
1737     return nullptr;
1738 
1739   auto [BitsX, SignX] = getNumSignificantBits(Op.X.Val, &In);
1740   auto [BitsY, SignY] = getNumSignificantBits(Op.Y.Val, &In);
1741 
1742   // TODO: Add multiplication of vectors by scalar registers (up to 4 bytes).
1743 
1744   Value *X = Op.X.Val, *Y = Op.Y.Val;
1745   IRBuilder Builder(In.getParent(), In.getIterator(),
1746                     InstSimplifyFolder(HVC.DL));
1747 
1748   auto roundUpWidth = [](unsigned Width) -> unsigned {
1749     if (Width <= 32 && !isPowerOf2_32(Width)) {
1750       // If the element width is not a power of 2, round it up
1751       // to the next one. Do this for widths not exceeding 32.
1752       return PowerOf2Ceil(Width);
1753     }
1754     if (Width > 32 && Width % 32 != 0) {
1755       // For wider elements, round it up to the multiple of 32.
1756       return alignTo(Width, 32u);
1757     }
1758     return Width;
1759   };
1760 
1761   BitsX = roundUpWidth(BitsX);
1762   BitsY = roundUpWidth(BitsY);
1763 
1764   // For elementwise multiplication vectors must have the same lengths, so
1765   // resize the elements of both inputs to the same width, the max of the
1766   // calculated significant bits.
1767   unsigned Width = std::max(BitsX, BitsY);
1768 
1769   auto *ResizeTy = VectorType::get(HVC.getIntTy(Width), VecTy);
1770   if (Width < ElemWidth) {
1771     X = Builder.CreateTrunc(X, ResizeTy, "trn");
1772     Y = Builder.CreateTrunc(Y, ResizeTy, "trn");
1773   } else if (Width > ElemWidth) {
1774     X = SignX == Signed ? Builder.CreateSExt(X, ResizeTy, "sxt")
1775                         : Builder.CreateZExt(X, ResizeTy, "zxt");
1776     Y = SignY == Signed ? Builder.CreateSExt(Y, ResizeTy, "sxt")
1777                         : Builder.CreateZExt(Y, ResizeTy, "zxt");
1778   };
1779 
1780   assert(X->getType() == Y->getType() && X->getType() == ResizeTy);
1781 
1782   unsigned VecLen = HVC.length(ResizeTy);
1783   unsigned ChopLen = (8 * HVC.HST.getVectorLength()) / std::min(Width, 32u);
1784 
1785   SmallVector<Value *> Results;
1786   FxpOp ChopOp = Op;
1787   ChopOp.ResTy = VectorType::get(Op.ResTy->getElementType(), ChopLen, false);
1788 
1789   for (unsigned V = 0; V != VecLen / ChopLen; ++V) {
1790     ChopOp.X.Val = HVC.subvector(Builder, X, V * ChopLen, ChopLen);
1791     ChopOp.Y.Val = HVC.subvector(Builder, Y, V * ChopLen, ChopLen);
1792     Results.push_back(processFxpMulChopped(Builder, In, ChopOp));
1793     if (Results.back() == nullptr)
1794       break;
1795   }
1796 
1797   if (Results.empty() || Results.back() == nullptr)
1798     return nullptr;
1799 
1800   Value *Cat = HVC.concat(Builder, Results);
1801   Value *Ext = SignX == Signed || SignY == Signed
1802                    ? Builder.CreateSExt(Cat, VecTy, "sxt")
1803                    : Builder.CreateZExt(Cat, VecTy, "zxt");
1804   return Ext;
1805 }
1806 
1807 auto HvxIdioms::processFxpMulChopped(IRBuilderBase &Builder, Instruction &In,
1808                                      const FxpOp &Op) const -> Value * {
1809   assert(Op.X.Val->getType() == Op.Y.Val->getType());
1810   auto *InpTy = cast<VectorType>(Op.X.Val->getType());
1811   unsigned Width = InpTy->getScalarSizeInBits();
1812   bool Rounding = Op.RoundAt.has_value();
1813 
1814   if (!Op.RoundAt || *Op.RoundAt == Op.Frac - 1) {
1815     // The fixed-point intrinsics do signed multiplication.
1816     if (Width == Op.Frac + 1 && Op.X.Sgn != Unsigned && Op.Y.Sgn != Unsigned) {
1817       Value *QMul = nullptr;
1818       if (Width == 16) {
1819         QMul = createMulQ15(Builder, Op.X, Op.Y, Rounding);
1820       } else if (Width == 32) {
1821         QMul = createMulQ31(Builder, Op.X, Op.Y, Rounding);
1822       }
1823       if (QMul != nullptr)
1824         return QMul;
1825     }
1826   }
1827 
1828   assert(Width >= 32 || isPowerOf2_32(Width)); // Width <= 32 => Width is 2^n
1829   assert(Width < 32 || Width % 32 == 0);       // Width > 32 => Width is 32*k
1830 
1831   // If Width < 32, then it should really be 16.
1832   if (Width < 32) {
1833     if (Width < 16)
1834       return nullptr;
1835     // Getting here with Op.Frac == 0 isn't wrong, but suboptimal: here we
1836     // generate a full precision products, which is unnecessary if there is
1837     // no shift.
1838     assert(Width == 16);
1839     assert(Op.Frac != 0 && "Unshifted mul should have been skipped");
1840     if (Op.Frac == 16) {
1841       // Multiply high
1842       if (Value *MulH = createMulH16(Builder, Op.X, Op.Y))
1843         return MulH;
1844     }
1845     // Do full-precision multiply and shift.
1846     Value *Prod32 = createMul16(Builder, Op.X, Op.Y);
1847     if (Rounding) {
1848       Value *RoundVal = HVC.getConstSplat(Prod32->getType(), 1 << *Op.RoundAt);
1849       Prod32 = Builder.CreateAdd(Prod32, RoundVal, "add");
1850     }
1851 
1852     Value *ShiftAmt = HVC.getConstSplat(Prod32->getType(), Op.Frac);
1853     Value *Shifted = Op.X.Sgn == Signed || Op.Y.Sgn == Signed
1854                          ? Builder.CreateAShr(Prod32, ShiftAmt, "asr")
1855                          : Builder.CreateLShr(Prod32, ShiftAmt, "lsr");
1856     return Builder.CreateTrunc(Shifted, InpTy, "trn");
1857   }
1858 
1859   // Width >= 32
1860 
1861   // Break up the arguments Op.X and Op.Y into vectors of smaller widths
1862   // in preparation of doing the multiplication by 32-bit parts.
1863   auto WordX = HVC.splitVectorElements(Builder, Op.X.Val, /*ToWidth=*/32);
1864   auto WordY = HVC.splitVectorElements(Builder, Op.Y.Val, /*ToWidth=*/32);
1865   auto WordP = createMulLong(Builder, WordX, Op.X.Sgn, WordY, Op.Y.Sgn);
1866 
1867   auto *HvxWordTy = cast<VectorType>(WordP.front()->getType());
1868 
1869   // Add the optional rounding to the proper word.
1870   if (Op.RoundAt.has_value()) {
1871     Value *Zero = HVC.getNullValue(WordX[0]->getType());
1872     SmallVector<Value *> RoundV(WordP.size(), Zero);
1873     RoundV[*Op.RoundAt / 32] =
1874         HVC.getConstSplat(HvxWordTy, 1 << (*Op.RoundAt % 32));
1875     WordP = createAddLong(Builder, WordP, RoundV);
1876   }
1877 
1878   // createRightShiftLong?
1879 
1880   // Shift all products right by Op.Frac.
1881   unsigned SkipWords = Op.Frac / 32;
1882   Constant *ShiftAmt = HVC.getConstSplat(HvxWordTy, Op.Frac % 32);
1883 
1884   for (int Dst = 0, End = WordP.size() - SkipWords; Dst != End; ++Dst) {
1885     int Src = Dst + SkipWords;
1886     Value *Lo = WordP[Src];
1887     if (Src + 1 < End) {
1888       Value *Hi = WordP[Src + 1];
1889       WordP[Dst] = Builder.CreateIntrinsic(HvxWordTy, Intrinsic::fshr,
1890                                            {Hi, Lo, ShiftAmt},
1891                                            /*FMFSource*/ nullptr, "int");
1892     } else {
1893       // The shift of the most significant word.
1894       WordP[Dst] = Builder.CreateAShr(Lo, ShiftAmt, "asr");
1895     }
1896   }
1897   if (SkipWords != 0)
1898     WordP.resize(WordP.size() - SkipWords);
1899 
1900   return HVC.joinVectorElements(Builder, WordP, Op.ResTy);
1901 }
1902 
1903 auto HvxIdioms::createMulQ15(IRBuilderBase &Builder, SValue X, SValue Y,
1904                              bool Rounding) const -> Value * {
1905   assert(X.Val->getType() == Y.Val->getType());
1906   assert(X.Val->getType()->getScalarType() == HVC.getIntTy(16));
1907   assert(HVC.HST.isHVXVectorType(EVT::getEVT(X.Val->getType(), false)));
1908 
1909   // There is no non-rounding intrinsic for i16.
1910   if (!Rounding || X.Sgn == Unsigned || Y.Sgn == Unsigned)
1911     return nullptr;
1912 
1913   auto V6_vmpyhvsrs = HVC.HST.getIntrinsicId(Hexagon::V6_vmpyhvsrs);
1914   return HVC.createHvxIntrinsic(Builder, V6_vmpyhvsrs, X.Val->getType(),
1915                                 {X.Val, Y.Val});
1916 }
1917 
1918 auto HvxIdioms::createMulQ31(IRBuilderBase &Builder, SValue X, SValue Y,
1919                              bool Rounding) const -> Value * {
1920   Type *InpTy = X.Val->getType();
1921   assert(InpTy == Y.Val->getType());
1922   assert(InpTy->getScalarType() == HVC.getIntTy(32));
1923   assert(HVC.HST.isHVXVectorType(EVT::getEVT(InpTy, false)));
1924 
1925   if (X.Sgn == Unsigned || Y.Sgn == Unsigned)
1926     return nullptr;
1927 
1928   auto V6_vmpyewuh = HVC.HST.getIntrinsicId(Hexagon::V6_vmpyewuh);
1929   auto V6_vmpyo_acc = Rounding
1930                           ? HVC.HST.getIntrinsicId(Hexagon::V6_vmpyowh_rnd_sacc)
1931                           : HVC.HST.getIntrinsicId(Hexagon::V6_vmpyowh_sacc);
1932   Value *V1 =
1933       HVC.createHvxIntrinsic(Builder, V6_vmpyewuh, InpTy, {X.Val, Y.Val});
1934   return HVC.createHvxIntrinsic(Builder, V6_vmpyo_acc, InpTy,
1935                                 {V1, X.Val, Y.Val});
1936 }
1937 
1938 auto HvxIdioms::createAddCarry(IRBuilderBase &Builder, Value *X, Value *Y,
1939                                Value *CarryIn) const
1940     -> std::pair<Value *, Value *> {
1941   assert(X->getType() == Y->getType());
1942   auto VecTy = cast<VectorType>(X->getType());
1943   if (VecTy == HvxI32Ty && HVC.HST.useHVXV62Ops()) {
1944     SmallVector<Value *> Args = {X, Y};
1945     Intrinsic::ID AddCarry;
1946     if (CarryIn == nullptr && HVC.HST.useHVXV66Ops()) {
1947       AddCarry = HVC.HST.getIntrinsicId(Hexagon::V6_vaddcarryo);
1948     } else {
1949       AddCarry = HVC.HST.getIntrinsicId(Hexagon::V6_vaddcarry);
1950       if (CarryIn == nullptr)
1951         CarryIn = HVC.getNullValue(HVC.getBoolTy(HVC.length(VecTy)));
1952       Args.push_back(CarryIn);
1953     }
1954     Value *Ret = HVC.createHvxIntrinsic(Builder, AddCarry,
1955                                         /*RetTy=*/nullptr, Args);
1956     Value *Result = Builder.CreateExtractValue(Ret, {0}, "ext");
1957     Value *CarryOut = Builder.CreateExtractValue(Ret, {1}, "ext");
1958     return {Result, CarryOut};
1959   }
1960 
1961   // In other cases, do a regular add, and unsigned compare-less-than.
1962   // The carry-out can originate in two places: adding the carry-in or adding
1963   // the two input values.
1964   Value *Result1 = X; // Result1 = X + CarryIn
1965   if (CarryIn != nullptr) {
1966     unsigned Width = VecTy->getScalarSizeInBits();
1967     uint32_t Mask = 1;
1968     if (Width < 32) {
1969       for (unsigned i = 0, e = 32 / Width; i != e; ++i)
1970         Mask = (Mask << Width) | 1;
1971     }
1972     auto V6_vandqrt = HVC.HST.getIntrinsicId(Hexagon::V6_vandqrt);
1973     Value *ValueIn =
1974         HVC.createHvxIntrinsic(Builder, V6_vandqrt, /*RetTy=*/nullptr,
1975                                {CarryIn, HVC.getConstInt(Mask)});
1976     Result1 = Builder.CreateAdd(X, ValueIn, "add");
1977   }
1978 
1979   Value *CarryOut1 = Builder.CreateCmp(CmpInst::ICMP_ULT, Result1, X, "cmp");
1980   Value *Result2 = Builder.CreateAdd(Result1, Y, "add");
1981   Value *CarryOut2 = Builder.CreateCmp(CmpInst::ICMP_ULT, Result2, Y, "cmp");
1982   return {Result2, Builder.CreateOr(CarryOut1, CarryOut2, "orb")};
1983 }
1984 
1985 auto HvxIdioms::createMul16(IRBuilderBase &Builder, SValue X, SValue Y) const
1986     -> Value * {
1987   Intrinsic::ID V6_vmpyh = 0;
1988   std::tie(X, Y) = canonSgn(X, Y);
1989 
1990   if (X.Sgn == Signed) {
1991     V6_vmpyh = HVC.HST.getIntrinsicId(Hexagon::V6_vmpyhv);
1992   } else if (Y.Sgn == Signed) {
1993     // In vmpyhus the second operand is unsigned
1994     V6_vmpyh = HVC.HST.getIntrinsicId(Hexagon::V6_vmpyhus);
1995   } else {
1996     V6_vmpyh = HVC.HST.getIntrinsicId(Hexagon::V6_vmpyuhv);
1997   }
1998 
1999   // i16*i16 -> i32 / interleaved
2000   Value *P =
2001       HVC.createHvxIntrinsic(Builder, V6_vmpyh, HvxP32Ty, {Y.Val, X.Val});
2002   // Deinterleave
2003   return HVC.vshuff(Builder, HVC.sublo(Builder, P), HVC.subhi(Builder, P));
2004 }
2005 
2006 auto HvxIdioms::createMulH16(IRBuilderBase &Builder, SValue X, SValue Y) const
2007     -> Value * {
2008   Type *HvxI16Ty = HVC.getHvxTy(HVC.getIntTy(16), /*Pair=*/false);
2009 
2010   if (HVC.HST.useHVXV69Ops()) {
2011     if (X.Sgn != Signed && Y.Sgn != Signed) {
2012       auto V6_vmpyuhvs = HVC.HST.getIntrinsicId(Hexagon::V6_vmpyuhvs);
2013       return HVC.createHvxIntrinsic(Builder, V6_vmpyuhvs, HvxI16Ty,
2014                                     {X.Val, Y.Val});
2015     }
2016   }
2017 
2018   Type *HvxP16Ty = HVC.getHvxTy(HVC.getIntTy(16), /*Pair=*/true);
2019   Value *Pair16 =
2020       Builder.CreateBitCast(createMul16(Builder, X, Y), HvxP16Ty, "cst");
2021   unsigned Len = HVC.length(HvxP16Ty) / 2;
2022 
2023   SmallVector<int, 128> PickOdd(Len);
2024   for (int i = 0; i != static_cast<int>(Len); ++i)
2025     PickOdd[i] = 2 * i + 1;
2026 
2027   return Builder.CreateShuffleVector(
2028       HVC.sublo(Builder, Pair16), HVC.subhi(Builder, Pair16), PickOdd, "shf");
2029 }
2030 
2031 auto HvxIdioms::createMul32(IRBuilderBase &Builder, SValue X, SValue Y) const
2032     -> std::pair<Value *, Value *> {
2033   assert(X.Val->getType() == Y.Val->getType());
2034   assert(X.Val->getType() == HvxI32Ty);
2035 
2036   Intrinsic::ID V6_vmpy_parts;
2037   std::tie(X, Y) = canonSgn(X, Y);
2038 
2039   if (X.Sgn == Signed) {
2040     V6_vmpy_parts = Intrinsic::hexagon_V6_vmpyss_parts;
2041   } else if (Y.Sgn == Signed) {
2042     V6_vmpy_parts = Intrinsic::hexagon_V6_vmpyus_parts;
2043   } else {
2044     V6_vmpy_parts = Intrinsic::hexagon_V6_vmpyuu_parts;
2045   }
2046 
2047   Value *Parts = HVC.createHvxIntrinsic(Builder, V6_vmpy_parts, nullptr,
2048                                         {X.Val, Y.Val}, {HvxI32Ty});
2049   Value *Hi = Builder.CreateExtractValue(Parts, {0}, "ext");
2050   Value *Lo = Builder.CreateExtractValue(Parts, {1}, "ext");
2051   return {Lo, Hi};
2052 }
2053 
2054 auto HvxIdioms::createAddLong(IRBuilderBase &Builder, ArrayRef<Value *> WordX,
2055                               ArrayRef<Value *> WordY) const
2056     -> SmallVector<Value *> {
2057   assert(WordX.size() == WordY.size());
2058   unsigned Idx = 0, Length = WordX.size();
2059   SmallVector<Value *> Sum(Length);
2060 
2061   while (Idx != Length) {
2062     if (HVC.isZero(WordX[Idx]))
2063       Sum[Idx] = WordY[Idx];
2064     else if (HVC.isZero(WordY[Idx]))
2065       Sum[Idx] = WordX[Idx];
2066     else
2067       break;
2068     ++Idx;
2069   }
2070 
2071   Value *Carry = nullptr;
2072   for (; Idx != Length; ++Idx) {
2073     std::tie(Sum[Idx], Carry) =
2074         createAddCarry(Builder, WordX[Idx], WordY[Idx], Carry);
2075   }
2076 
2077   // This drops the final carry beyond the highest word.
2078   return Sum;
2079 }
2080 
2081 auto HvxIdioms::createMulLong(IRBuilderBase &Builder, ArrayRef<Value *> WordX,
2082                               Signedness SgnX, ArrayRef<Value *> WordY,
2083                               Signedness SgnY) const -> SmallVector<Value *> {
2084   SmallVector<SmallVector<Value *>> Products(WordX.size() + WordY.size());
2085 
2086   // WordX[i] * WordY[j] produces words i+j and i+j+1 of the results,
2087   // that is halves 2(i+j), 2(i+j)+1, 2(i+j)+2, 2(i+j)+3.
2088   for (int i = 0, e = WordX.size(); i != e; ++i) {
2089     for (int j = 0, f = WordY.size(); j != f; ++j) {
2090       // Check the 4 halves that this multiplication can generate.
2091       Signedness SX = (i + 1 == e) ? SgnX : Unsigned;
2092       Signedness SY = (j + 1 == f) ? SgnY : Unsigned;
2093       auto [Lo, Hi] = createMul32(Builder, {WordX[i], SX}, {WordY[j], SY});
2094       Products[i + j + 0].push_back(Lo);
2095       Products[i + j + 1].push_back(Hi);
2096     }
2097   }
2098 
2099   Value *Zero = HVC.getNullValue(WordX[0]->getType());
2100 
2101   auto pop_back_or_zero = [Zero](auto &Vector) -> Value * {
2102     if (Vector.empty())
2103       return Zero;
2104     auto Last = Vector.back();
2105     Vector.pop_back();
2106     return Last;
2107   };
2108 
2109   for (int i = 0, e = Products.size(); i != e; ++i) {
2110     while (Products[i].size() > 1) {
2111       Value *Carry = nullptr; // no carry-in
2112       for (int j = i; j != e; ++j) {
2113         auto &ProdJ = Products[j];
2114         auto [Sum, CarryOut] = createAddCarry(Builder, pop_back_or_zero(ProdJ),
2115                                               pop_back_or_zero(ProdJ), Carry);
2116         ProdJ.insert(ProdJ.begin(), Sum);
2117         Carry = CarryOut;
2118       }
2119     }
2120   }
2121 
2122   SmallVector<Value *> WordP;
2123   for (auto &P : Products) {
2124     assert(P.size() == 1 && "Should have been added together");
2125     WordP.push_back(P.front());
2126   }
2127 
2128   return WordP;
2129 }
2130 
2131 auto HvxIdioms::run() -> bool {
2132   bool Changed = false;
2133 
2134   for (BasicBlock &B : HVC.F) {
2135     for (auto It = B.rbegin(); It != B.rend(); ++It) {
2136       if (auto Fxm = matchFxpMul(*It)) {
2137         Value *New = processFxpMul(*It, *Fxm);
2138         // Always report "changed" for now.
2139         Changed = true;
2140         if (!New)
2141           continue;
2142         bool StartOver = !isa<Instruction>(New);
2143         It->replaceAllUsesWith(New);
2144         RecursivelyDeleteTriviallyDeadInstructions(&*It, &HVC.TLI);
2145         It = StartOver ? B.rbegin()
2146                        : cast<Instruction>(New)->getReverseIterator();
2147         Changed = true;
2148       }
2149     }
2150   }
2151 
2152   return Changed;
2153 }
2154 
2155 // --- End HvxIdioms
2156 
2157 auto HexagonVectorCombine::run() -> bool {
2158   if (DumpModule)
2159     dbgs() << "Module before HexagonVectorCombine\n" << *F.getParent();
2160 
2161   bool Changed = false;
2162   if (HST.useHVXOps()) {
2163     if (VAEnabled)
2164       Changed |= AlignVectors(*this).run();
2165     if (VIEnabled)
2166       Changed |= HvxIdioms(*this).run();
2167   }
2168 
2169   if (DumpModule) {
2170     dbgs() << "Module " << (Changed ? "(modified)" : "(unchanged)")
2171            << " after HexagonVectorCombine\n"
2172            << *F.getParent();
2173   }
2174   return Changed;
2175 }
2176 
2177 auto HexagonVectorCombine::getIntTy(unsigned Width) const -> IntegerType * {
2178   return IntegerType::get(F.getContext(), Width);
2179 }
2180 
2181 auto HexagonVectorCombine::getByteTy(int ElemCount) const -> Type * {
2182   assert(ElemCount >= 0);
2183   IntegerType *ByteTy = Type::getInt8Ty(F.getContext());
2184   if (ElemCount == 0)
2185     return ByteTy;
2186   return VectorType::get(ByteTy, ElemCount, /*Scalable=*/false);
2187 }
2188 
2189 auto HexagonVectorCombine::getBoolTy(int ElemCount) const -> Type * {
2190   assert(ElemCount >= 0);
2191   IntegerType *BoolTy = Type::getInt1Ty(F.getContext());
2192   if (ElemCount == 0)
2193     return BoolTy;
2194   return VectorType::get(BoolTy, ElemCount, /*Scalable=*/false);
2195 }
2196 
2197 auto HexagonVectorCombine::getConstInt(int Val, unsigned Width) const
2198     -> ConstantInt * {
2199   return ConstantInt::getSigned(getIntTy(Width), Val);
2200 }
2201 
2202 auto HexagonVectorCombine::isZero(const Value *Val) const -> bool {
2203   if (auto *C = dyn_cast<Constant>(Val))
2204     return C->isZeroValue();
2205   return false;
2206 }
2207 
2208 auto HexagonVectorCombine::getIntValue(const Value *Val) const
2209     -> std::optional<APInt> {
2210   if (auto *CI = dyn_cast<ConstantInt>(Val))
2211     return CI->getValue();
2212   return std::nullopt;
2213 }
2214 
2215 auto HexagonVectorCombine::isUndef(const Value *Val) const -> bool {
2216   return isa<UndefValue>(Val);
2217 }
2218 
2219 auto HexagonVectorCombine::isTrue(const Value *Val) const -> bool {
2220   return Val == ConstantInt::getTrue(Val->getType());
2221 }
2222 
2223 auto HexagonVectorCombine::isFalse(const Value *Val) const -> bool {
2224   return isZero(Val);
2225 }
2226 
2227 auto HexagonVectorCombine::getHvxTy(Type *ElemTy, bool Pair) const
2228     -> VectorType * {
2229   EVT ETy = EVT::getEVT(ElemTy, false);
2230   assert(ETy.isSimple() && "Invalid HVX element type");
2231   // Do not allow boolean types here: they don't have a fixed length.
2232   assert(HST.isHVXElementType(ETy.getSimpleVT(), /*IncludeBool=*/false) &&
2233          "Invalid HVX element type");
2234   unsigned HwLen = HST.getVectorLength();
2235   unsigned NumElems = (8 * HwLen) / ETy.getSizeInBits();
2236   return VectorType::get(ElemTy, Pair ? 2 * NumElems : NumElems,
2237                          /*Scalable=*/false);
2238 }
2239 
2240 auto HexagonVectorCombine::getSizeOf(const Value *Val, SizeKind Kind) const
2241     -> int {
2242   return getSizeOf(Val->getType(), Kind);
2243 }
2244 
2245 auto HexagonVectorCombine::getSizeOf(const Type *Ty, SizeKind Kind) const
2246     -> int {
2247   auto *NcTy = const_cast<Type *>(Ty);
2248   switch (Kind) {
2249   case Store:
2250     return DL.getTypeStoreSize(NcTy).getFixedValue();
2251   case Alloc:
2252     return DL.getTypeAllocSize(NcTy).getFixedValue();
2253   }
2254   llvm_unreachable("Unhandled SizeKind enum");
2255 }
2256 
2257 auto HexagonVectorCombine::getTypeAlignment(Type *Ty) const -> int {
2258   // The actual type may be shorter than the HVX vector, so determine
2259   // the alignment based on subtarget info.
2260   if (HST.isTypeForHVX(Ty))
2261     return HST.getVectorLength();
2262   return DL.getABITypeAlign(Ty).value();
2263 }
2264 
2265 auto HexagonVectorCombine::length(Value *Val) const -> size_t {
2266   return length(Val->getType());
2267 }
2268 
2269 auto HexagonVectorCombine::length(Type *Ty) const -> size_t {
2270   auto *VecTy = dyn_cast<VectorType>(Ty);
2271   assert(VecTy && "Must be a vector type");
2272   return VecTy->getElementCount().getFixedValue();
2273 }
2274 
2275 auto HexagonVectorCombine::getNullValue(Type *Ty) const -> Constant * {
2276   assert(Ty->isIntOrIntVectorTy());
2277   auto Zero = ConstantInt::get(Ty->getScalarType(), 0);
2278   if (auto *VecTy = dyn_cast<VectorType>(Ty))
2279     return ConstantVector::getSplat(VecTy->getElementCount(), Zero);
2280   return Zero;
2281 }
2282 
2283 auto HexagonVectorCombine::getFullValue(Type *Ty) const -> Constant * {
2284   assert(Ty->isIntOrIntVectorTy());
2285   auto Minus1 = ConstantInt::get(Ty->getScalarType(), -1);
2286   if (auto *VecTy = dyn_cast<VectorType>(Ty))
2287     return ConstantVector::getSplat(VecTy->getElementCount(), Minus1);
2288   return Minus1;
2289 }
2290 
2291 auto HexagonVectorCombine::getConstSplat(Type *Ty, int Val) const
2292     -> Constant * {
2293   assert(Ty->isVectorTy());
2294   auto VecTy = cast<VectorType>(Ty);
2295   Type *ElemTy = VecTy->getElementType();
2296   // Add support for floats if needed.
2297   auto *Splat = ConstantVector::getSplat(VecTy->getElementCount(),
2298                                          ConstantInt::get(ElemTy, Val));
2299   return Splat;
2300 }
2301 
2302 auto HexagonVectorCombine::simplify(Value *V) const -> Value * {
2303   if (auto *In = dyn_cast<Instruction>(V)) {
2304     SimplifyQuery Q(DL, &TLI, &DT, &AC, In);
2305     return simplifyInstruction(In, Q);
2306   }
2307   return nullptr;
2308 }
2309 
2310 // Insert bytes [Start..Start+Length) of Src into Dst at byte Where.
2311 auto HexagonVectorCombine::insertb(IRBuilderBase &Builder, Value *Dst,
2312                                    Value *Src, int Start, int Length,
2313                                    int Where) const -> Value * {
2314   assert(isByteVecTy(Dst->getType()) && isByteVecTy(Src->getType()));
2315   int SrcLen = getSizeOf(Src);
2316   int DstLen = getSizeOf(Dst);
2317   assert(0 <= Start && Start + Length <= SrcLen);
2318   assert(0 <= Where && Where + Length <= DstLen);
2319 
2320   int P2Len = PowerOf2Ceil(SrcLen | DstLen);
2321   auto *Undef = UndefValue::get(getByteTy());
2322   Value *P2Src = vresize(Builder, Src, P2Len, Undef);
2323   Value *P2Dst = vresize(Builder, Dst, P2Len, Undef);
2324 
2325   SmallVector<int, 256> SMask(P2Len);
2326   for (int i = 0; i != P2Len; ++i) {
2327     // If i is in [Where, Where+Length), pick Src[Start+(i-Where)].
2328     // Otherwise, pick Dst[i];
2329     SMask[i] =
2330         (Where <= i && i < Where + Length) ? P2Len + Start + (i - Where) : i;
2331   }
2332 
2333   Value *P2Insert = Builder.CreateShuffleVector(P2Dst, P2Src, SMask, "shf");
2334   return vresize(Builder, P2Insert, DstLen, Undef);
2335 }
2336 
2337 auto HexagonVectorCombine::vlalignb(IRBuilderBase &Builder, Value *Lo,
2338                                     Value *Hi, Value *Amt) const -> Value * {
2339   assert(Lo->getType() == Hi->getType() && "Argument type mismatch");
2340   if (isZero(Amt))
2341     return Hi;
2342   int VecLen = getSizeOf(Hi);
2343   if (auto IntAmt = getIntValue(Amt))
2344     return getElementRange(Builder, Lo, Hi, VecLen - IntAmt->getSExtValue(),
2345                            VecLen);
2346 
2347   if (HST.isTypeForHVX(Hi->getType())) {
2348     assert(static_cast<unsigned>(VecLen) == HST.getVectorLength() &&
2349            "Expecting an exact HVX type");
2350     return createHvxIntrinsic(Builder, HST.getIntrinsicId(Hexagon::V6_vlalignb),
2351                               Hi->getType(), {Hi, Lo, Amt});
2352   }
2353 
2354   if (VecLen == 4) {
2355     Value *Pair = concat(Builder, {Lo, Hi});
2356     Value *Shift =
2357         Builder.CreateLShr(Builder.CreateShl(Pair, Amt, "shl"), 32, "lsr");
2358     Value *Trunc =
2359         Builder.CreateTrunc(Shift, Type::getInt32Ty(F.getContext()), "trn");
2360     return Builder.CreateBitCast(Trunc, Hi->getType(), "cst");
2361   }
2362   if (VecLen == 8) {
2363     Value *Sub = Builder.CreateSub(getConstInt(VecLen), Amt, "sub");
2364     return vralignb(Builder, Lo, Hi, Sub);
2365   }
2366   llvm_unreachable("Unexpected vector length");
2367 }
2368 
2369 auto HexagonVectorCombine::vralignb(IRBuilderBase &Builder, Value *Lo,
2370                                     Value *Hi, Value *Amt) const -> Value * {
2371   assert(Lo->getType() == Hi->getType() && "Argument type mismatch");
2372   if (isZero(Amt))
2373     return Lo;
2374   int VecLen = getSizeOf(Lo);
2375   if (auto IntAmt = getIntValue(Amt))
2376     return getElementRange(Builder, Lo, Hi, IntAmt->getSExtValue(), VecLen);
2377 
2378   if (HST.isTypeForHVX(Lo->getType())) {
2379     assert(static_cast<unsigned>(VecLen) == HST.getVectorLength() &&
2380            "Expecting an exact HVX type");
2381     return createHvxIntrinsic(Builder, HST.getIntrinsicId(Hexagon::V6_valignb),
2382                               Lo->getType(), {Hi, Lo, Amt});
2383   }
2384 
2385   if (VecLen == 4) {
2386     Value *Pair = concat(Builder, {Lo, Hi});
2387     Value *Shift = Builder.CreateLShr(Pair, Amt, "lsr");
2388     Value *Trunc =
2389         Builder.CreateTrunc(Shift, Type::getInt32Ty(F.getContext()), "trn");
2390     return Builder.CreateBitCast(Trunc, Lo->getType(), "cst");
2391   }
2392   if (VecLen == 8) {
2393     Type *Int64Ty = Type::getInt64Ty(F.getContext());
2394     Value *Lo64 = Builder.CreateBitCast(Lo, Int64Ty, "cst");
2395     Value *Hi64 = Builder.CreateBitCast(Hi, Int64Ty, "cst");
2396     Function *FI = Intrinsic::getDeclaration(F.getParent(),
2397                                              Intrinsic::hexagon_S2_valignrb);
2398     Value *Call = Builder.CreateCall(FI, {Hi64, Lo64, Amt}, "cup");
2399     return Builder.CreateBitCast(Call, Lo->getType(), "cst");
2400   }
2401   llvm_unreachable("Unexpected vector length");
2402 }
2403 
2404 // Concatenates a sequence of vectors of the same type.
2405 auto HexagonVectorCombine::concat(IRBuilderBase &Builder,
2406                                   ArrayRef<Value *> Vecs) const -> Value * {
2407   assert(!Vecs.empty());
2408   SmallVector<int, 256> SMask;
2409   std::vector<Value *> Work[2];
2410   int ThisW = 0, OtherW = 1;
2411 
2412   Work[ThisW].assign(Vecs.begin(), Vecs.end());
2413   while (Work[ThisW].size() > 1) {
2414     auto *Ty = cast<VectorType>(Work[ThisW].front()->getType());
2415     SMask.resize(length(Ty) * 2);
2416     std::iota(SMask.begin(), SMask.end(), 0);
2417 
2418     Work[OtherW].clear();
2419     if (Work[ThisW].size() % 2 != 0)
2420       Work[ThisW].push_back(UndefValue::get(Ty));
2421     for (int i = 0, e = Work[ThisW].size(); i < e; i += 2) {
2422       Value *Joined = Builder.CreateShuffleVector(
2423           Work[ThisW][i], Work[ThisW][i + 1], SMask, "shf");
2424       Work[OtherW].push_back(Joined);
2425     }
2426     std::swap(ThisW, OtherW);
2427   }
2428 
2429   // Since there may have been some undefs appended to make shuffle operands
2430   // have the same type, perform the last shuffle to only pick the original
2431   // elements.
2432   SMask.resize(Vecs.size() * length(Vecs.front()->getType()));
2433   std::iota(SMask.begin(), SMask.end(), 0);
2434   Value *Total = Work[ThisW].front();
2435   return Builder.CreateShuffleVector(Total, SMask, "shf");
2436 }
2437 
2438 auto HexagonVectorCombine::vresize(IRBuilderBase &Builder, Value *Val,
2439                                    int NewSize, Value *Pad) const -> Value * {
2440   assert(isa<VectorType>(Val->getType()));
2441   auto *ValTy = cast<VectorType>(Val->getType());
2442   assert(ValTy->getElementType() == Pad->getType());
2443 
2444   int CurSize = length(ValTy);
2445   if (CurSize == NewSize)
2446     return Val;
2447   // Truncate?
2448   if (CurSize > NewSize)
2449     return getElementRange(Builder, Val, /*Ignored*/ Val, 0, NewSize);
2450   // Extend.
2451   SmallVector<int, 128> SMask(NewSize);
2452   std::iota(SMask.begin(), SMask.begin() + CurSize, 0);
2453   std::fill(SMask.begin() + CurSize, SMask.end(), CurSize);
2454   Value *PadVec = Builder.CreateVectorSplat(CurSize, Pad, "spt");
2455   return Builder.CreateShuffleVector(Val, PadVec, SMask, "shf");
2456 }
2457 
2458 auto HexagonVectorCombine::rescale(IRBuilderBase &Builder, Value *Mask,
2459                                    Type *FromTy, Type *ToTy) const -> Value * {
2460   // Mask is a vector <N x i1>, where each element corresponds to an
2461   // element of FromTy. Remap it so that each element will correspond
2462   // to an element of ToTy.
2463   assert(isa<VectorType>(Mask->getType()));
2464 
2465   Type *FromSTy = FromTy->getScalarType();
2466   Type *ToSTy = ToTy->getScalarType();
2467   if (FromSTy == ToSTy)
2468     return Mask;
2469 
2470   int FromSize = getSizeOf(FromSTy);
2471   int ToSize = getSizeOf(ToSTy);
2472   assert(FromSize % ToSize == 0 || ToSize % FromSize == 0);
2473 
2474   auto *MaskTy = cast<VectorType>(Mask->getType());
2475   int FromCount = length(MaskTy);
2476   int ToCount = (FromCount * FromSize) / ToSize;
2477   assert((FromCount * FromSize) % ToSize == 0);
2478 
2479   auto *FromITy = getIntTy(FromSize * 8);
2480   auto *ToITy = getIntTy(ToSize * 8);
2481 
2482   // Mask <N x i1> -> sext to <N x FromTy> -> bitcast to <M x ToTy> ->
2483   // -> trunc to <M x i1>.
2484   Value *Ext = Builder.CreateSExt(
2485       Mask, VectorType::get(FromITy, FromCount, /*Scalable=*/false), "sxt");
2486   Value *Cast = Builder.CreateBitCast(
2487       Ext, VectorType::get(ToITy, ToCount, /*Scalable=*/false), "cst");
2488   return Builder.CreateTrunc(
2489       Cast, VectorType::get(getBoolTy(), ToCount, /*Scalable=*/false), "trn");
2490 }
2491 
2492 // Bitcast to bytes, and return least significant bits.
2493 auto HexagonVectorCombine::vlsb(IRBuilderBase &Builder, Value *Val) const
2494     -> Value * {
2495   Type *ScalarTy = Val->getType()->getScalarType();
2496   if (ScalarTy == getBoolTy())
2497     return Val;
2498 
2499   Value *Bytes = vbytes(Builder, Val);
2500   if (auto *VecTy = dyn_cast<VectorType>(Bytes->getType()))
2501     return Builder.CreateTrunc(Bytes, getBoolTy(getSizeOf(VecTy)), "trn");
2502   // If Bytes is a scalar (i.e. Val was a scalar byte), return i1, not
2503   // <1 x i1>.
2504   return Builder.CreateTrunc(Bytes, getBoolTy(), "trn");
2505 }
2506 
2507 // Bitcast to bytes for non-bool. For bool, convert i1 -> i8.
2508 auto HexagonVectorCombine::vbytes(IRBuilderBase &Builder, Value *Val) const
2509     -> Value * {
2510   Type *ScalarTy = Val->getType()->getScalarType();
2511   if (ScalarTy == getByteTy())
2512     return Val;
2513 
2514   if (ScalarTy != getBoolTy())
2515     return Builder.CreateBitCast(Val, getByteTy(getSizeOf(Val)), "cst");
2516   // For bool, return a sext from i1 to i8.
2517   if (auto *VecTy = dyn_cast<VectorType>(Val->getType()))
2518     return Builder.CreateSExt(Val, VectorType::get(getByteTy(), VecTy), "sxt");
2519   return Builder.CreateSExt(Val, getByteTy(), "sxt");
2520 }
2521 
2522 auto HexagonVectorCombine::subvector(IRBuilderBase &Builder, Value *Val,
2523                                      unsigned Start, unsigned Length) const
2524     -> Value * {
2525   assert(Start + Length <= length(Val));
2526   return getElementRange(Builder, Val, /*Ignored*/ Val, Start, Length);
2527 }
2528 
2529 auto HexagonVectorCombine::sublo(IRBuilderBase &Builder, Value *Val) const
2530     -> Value * {
2531   size_t Len = length(Val);
2532   assert(Len % 2 == 0 && "Length should be even");
2533   return subvector(Builder, Val, 0, Len / 2);
2534 }
2535 
2536 auto HexagonVectorCombine::subhi(IRBuilderBase &Builder, Value *Val) const
2537     -> Value * {
2538   size_t Len = length(Val);
2539   assert(Len % 2 == 0 && "Length should be even");
2540   return subvector(Builder, Val, Len / 2, Len / 2);
2541 }
2542 
2543 auto HexagonVectorCombine::vdeal(IRBuilderBase &Builder, Value *Val0,
2544                                  Value *Val1) const -> Value * {
2545   assert(Val0->getType() == Val1->getType());
2546   int Len = length(Val0);
2547   SmallVector<int, 128> Mask(2 * Len);
2548 
2549   for (int i = 0; i != Len; ++i) {
2550     Mask[i] = 2 * i;           // Even
2551     Mask[i + Len] = 2 * i + 1; // Odd
2552   }
2553   return Builder.CreateShuffleVector(Val0, Val1, Mask, "shf");
2554 }
2555 
2556 auto HexagonVectorCombine::vshuff(IRBuilderBase &Builder, Value *Val0,
2557                                   Value *Val1) const -> Value * { //
2558   assert(Val0->getType() == Val1->getType());
2559   int Len = length(Val0);
2560   SmallVector<int, 128> Mask(2 * Len);
2561 
2562   for (int i = 0; i != Len; ++i) {
2563     Mask[2 * i + 0] = i;       // Val0
2564     Mask[2 * i + 1] = i + Len; // Val1
2565   }
2566   return Builder.CreateShuffleVector(Val0, Val1, Mask, "shf");
2567 }
2568 
2569 auto HexagonVectorCombine::createHvxIntrinsic(IRBuilderBase &Builder,
2570                                               Intrinsic::ID IntID, Type *RetTy,
2571                                               ArrayRef<Value *> Args,
2572                                               ArrayRef<Type *> ArgTys,
2573                                               ArrayRef<Value *> MDSources) const
2574     -> Value * {
2575   auto getCast = [&](IRBuilderBase &Builder, Value *Val,
2576                      Type *DestTy) -> Value * {
2577     Type *SrcTy = Val->getType();
2578     if (SrcTy == DestTy)
2579       return Val;
2580 
2581     // Non-HVX type. It should be a scalar, and it should already have
2582     // a valid type.
2583     assert(HST.isTypeForHVX(SrcTy, /*IncludeBool=*/true));
2584 
2585     Type *BoolTy = Type::getInt1Ty(F.getContext());
2586     if (cast<VectorType>(SrcTy)->getElementType() != BoolTy)
2587       return Builder.CreateBitCast(Val, DestTy, "cst");
2588 
2589     // Predicate HVX vector.
2590     unsigned HwLen = HST.getVectorLength();
2591     Intrinsic::ID TC = HwLen == 64 ? Intrinsic::hexagon_V6_pred_typecast
2592                                    : Intrinsic::hexagon_V6_pred_typecast_128B;
2593     Function *FI =
2594         Intrinsic::getDeclaration(F.getParent(), TC, {DestTy, Val->getType()});
2595     return Builder.CreateCall(FI, {Val}, "cup");
2596   };
2597 
2598   Function *IntrFn = Intrinsic::getDeclaration(F.getParent(), IntID, ArgTys);
2599   FunctionType *IntrTy = IntrFn->getFunctionType();
2600 
2601   SmallVector<Value *, 4> IntrArgs;
2602   for (int i = 0, e = Args.size(); i != e; ++i) {
2603     Value *A = Args[i];
2604     Type *T = IntrTy->getParamType(i);
2605     if (A->getType() != T) {
2606       IntrArgs.push_back(getCast(Builder, A, T));
2607     } else {
2608       IntrArgs.push_back(A);
2609     }
2610   }
2611   StringRef MaybeName = !IntrTy->getReturnType()->isVoidTy() ? "cup" : "";
2612   CallInst *Call = Builder.CreateCall(IntrFn, IntrArgs, MaybeName);
2613 
2614   MemoryEffects ME = Call->getAttributes().getMemoryEffects();
2615   if (!ME.doesNotAccessMemory() && !ME.onlyAccessesInaccessibleMem())
2616     propagateMetadata(Call, MDSources);
2617 
2618   Type *CallTy = Call->getType();
2619   if (RetTy == nullptr || CallTy == RetTy)
2620     return Call;
2621   // Scalar types should have RetTy matching the call return type.
2622   assert(HST.isTypeForHVX(CallTy, /*IncludeBool=*/true));
2623   return getCast(Builder, Call, RetTy);
2624 }
2625 
2626 auto HexagonVectorCombine::splitVectorElements(IRBuilderBase &Builder,
2627                                                Value *Vec,
2628                                                unsigned ToWidth) const
2629     -> SmallVector<Value *> {
2630   // Break a vector of wide elements into a series of vectors with narrow
2631   // elements:
2632   //   (...c0:b0:a0, ...c1:b1:a1, ...c2:b2:a2, ...)
2633   // -->
2634   //   (a0, a1, a2, ...)    // lowest "ToWidth" bits
2635   //   (b0, b1, b2, ...)    // the next lowest...
2636   //   (c0, c1, c2, ...)    // ...
2637   //   ...
2638   //
2639   // The number of elements in each resulting vector is the same as
2640   // in the original vector.
2641 
2642   auto *VecTy = cast<VectorType>(Vec->getType());
2643   assert(VecTy->getElementType()->isIntegerTy());
2644   unsigned FromWidth = VecTy->getScalarSizeInBits();
2645   assert(isPowerOf2_32(ToWidth) && isPowerOf2_32(FromWidth));
2646   assert(ToWidth <= FromWidth && "Breaking up into wider elements?");
2647   unsigned NumResults = FromWidth / ToWidth;
2648 
2649   SmallVector<Value *> Results(NumResults);
2650   Results[0] = Vec;
2651   unsigned Length = length(VecTy);
2652 
2653   // Do it by splitting in half, since those operations correspond to deal
2654   // instructions.
2655   auto splitInHalf = [&](unsigned Begin, unsigned End, auto splitFunc) -> void {
2656     // Take V = Results[Begin], split it in L, H.
2657     // Store Results[Begin] = L, Results[(Begin+End)/2] = H
2658     // Call itself recursively split(Begin, Half), split(Half+1, End)
2659     if (Begin + 1 == End)
2660       return;
2661 
2662     Value *Val = Results[Begin];
2663     unsigned Width = Val->getType()->getScalarSizeInBits();
2664 
2665     auto *VTy = VectorType::get(getIntTy(Width / 2), 2 * Length, false);
2666     Value *VVal = Builder.CreateBitCast(Val, VTy, "cst");
2667 
2668     Value *Res = vdeal(Builder, sublo(Builder, VVal), subhi(Builder, VVal));
2669 
2670     unsigned Half = (Begin + End) / 2;
2671     Results[Begin] = sublo(Builder, Res);
2672     Results[Half] = subhi(Builder, Res);
2673 
2674     splitFunc(Begin, Half, splitFunc);
2675     splitFunc(Half, End, splitFunc);
2676   };
2677 
2678   splitInHalf(0, NumResults, splitInHalf);
2679   return Results;
2680 }
2681 
2682 auto HexagonVectorCombine::joinVectorElements(IRBuilderBase &Builder,
2683                                               ArrayRef<Value *> Values,
2684                                               VectorType *ToType) const
2685     -> Value * {
2686   assert(ToType->getElementType()->isIntegerTy());
2687 
2688   // If the list of values does not have power-of-2 elements, append copies
2689   // of the sign bit to it, to make the size be 2^n.
2690   // The reason for this is that the values will be joined in pairs, because
2691   // otherwise the shuffles will result in convoluted code. With pairwise
2692   // joins, the shuffles will hopefully be folded into a perfect shuffle.
2693   // The output will need to be sign-extended to a type with element width
2694   // being a power-of-2 anyways.
2695   SmallVector<Value *> Inputs(Values.begin(), Values.end());
2696 
2697   unsigned ToWidth = ToType->getScalarSizeInBits();
2698   unsigned Width = Inputs.front()->getType()->getScalarSizeInBits();
2699   assert(Width <= ToWidth);
2700   assert(isPowerOf2_32(Width) && isPowerOf2_32(ToWidth));
2701   unsigned Length = length(Inputs.front()->getType());
2702 
2703   unsigned NeedInputs = ToWidth / Width;
2704   if (Inputs.size() != NeedInputs) {
2705     // Having too many inputs is ok: drop the high bits (usual wrap-around).
2706     // If there are too few, fill them with the sign bit.
2707     Value *Last = Inputs.back();
2708     Value *Sign = Builder.CreateAShr(
2709         Last, getConstSplat(Last->getType(), Width - 1), "asr");
2710     Inputs.resize(NeedInputs, Sign);
2711   }
2712 
2713   while (Inputs.size() > 1) {
2714     Width *= 2;
2715     auto *VTy = VectorType::get(getIntTy(Width), Length, false);
2716     for (int i = 0, e = Inputs.size(); i < e; i += 2) {
2717       Value *Res = vshuff(Builder, Inputs[i], Inputs[i + 1]);
2718       Inputs[i / 2] = Builder.CreateBitCast(Res, VTy, "cst");
2719     }
2720     Inputs.resize(Inputs.size() / 2);
2721   }
2722 
2723   assert(Inputs.front()->getType() == ToType);
2724   return Inputs.front();
2725 }
2726 
2727 auto HexagonVectorCombine::calculatePointerDifference(Value *Ptr0,
2728                                                       Value *Ptr1) const
2729     -> std::optional<int> {
2730   // Try SCEV first.
2731   const SCEV *Scev0 = SE.getSCEV(Ptr0);
2732   const SCEV *Scev1 = SE.getSCEV(Ptr1);
2733   const SCEV *ScevDiff = SE.getMinusSCEV(Scev0, Scev1);
2734   if (auto *Const = dyn_cast<SCEVConstant>(ScevDiff)) {
2735     APInt V = Const->getAPInt();
2736     if (V.isSignedIntN(8 * sizeof(int)))
2737       return static_cast<int>(V.getSExtValue());
2738   }
2739 
2740   struct Builder : IRBuilder<> {
2741     Builder(BasicBlock *B) : IRBuilder<>(B->getTerminator()) {}
2742     ~Builder() {
2743       for (Instruction *I : llvm::reverse(ToErase))
2744         I->eraseFromParent();
2745     }
2746     SmallVector<Instruction *, 8> ToErase;
2747   };
2748 
2749 #define CallBuilder(B, F)                                                      \
2750   [&](auto &B_) {                                                              \
2751     Value *V = B_.F;                                                           \
2752     if (auto *I = dyn_cast<Instruction>(V))                                    \
2753       B_.ToErase.push_back(I);                                                 \
2754     return V;                                                                  \
2755   }(B)
2756 
2757   auto Simplify = [this](Value *V) {
2758     if (Value *S = simplify(V))
2759       return S;
2760     return V;
2761   };
2762 
2763   auto StripBitCast = [](Value *V) {
2764     while (auto *C = dyn_cast<BitCastInst>(V))
2765       V = C->getOperand(0);
2766     return V;
2767   };
2768 
2769   Ptr0 = StripBitCast(Ptr0);
2770   Ptr1 = StripBitCast(Ptr1);
2771   if (!isa<GetElementPtrInst>(Ptr0) || !isa<GetElementPtrInst>(Ptr1))
2772     return std::nullopt;
2773 
2774   auto *Gep0 = cast<GetElementPtrInst>(Ptr0);
2775   auto *Gep1 = cast<GetElementPtrInst>(Ptr1);
2776   if (Gep0->getPointerOperand() != Gep1->getPointerOperand())
2777     return std::nullopt;
2778   if (Gep0->getSourceElementType() != Gep1->getSourceElementType())
2779     return std::nullopt;
2780 
2781   Builder B(Gep0->getParent());
2782   int Scale = getSizeOf(Gep0->getSourceElementType(), Alloc);
2783 
2784   // FIXME: for now only check GEPs with a single index.
2785   if (Gep0->getNumOperands() != 2 || Gep1->getNumOperands() != 2)
2786     return std::nullopt;
2787 
2788   Value *Idx0 = Gep0->getOperand(1);
2789   Value *Idx1 = Gep1->getOperand(1);
2790 
2791   // First, try to simplify the subtraction directly.
2792   if (auto *Diff = dyn_cast<ConstantInt>(
2793           Simplify(CallBuilder(B, CreateSub(Idx0, Idx1)))))
2794     return Diff->getSExtValue() * Scale;
2795 
2796   KnownBits Known0 = getKnownBits(Idx0, Gep0);
2797   KnownBits Known1 = getKnownBits(Idx1, Gep1);
2798   APInt Unknown = ~(Known0.Zero | Known0.One) | ~(Known1.Zero | Known1.One);
2799   if (Unknown.isAllOnes())
2800     return std::nullopt;
2801 
2802   Value *MaskU = ConstantInt::get(Idx0->getType(), Unknown);
2803   Value *AndU0 = Simplify(CallBuilder(B, CreateAnd(Idx0, MaskU)));
2804   Value *AndU1 = Simplify(CallBuilder(B, CreateAnd(Idx1, MaskU)));
2805   Value *SubU = Simplify(CallBuilder(B, CreateSub(AndU0, AndU1)));
2806   int Diff0 = 0;
2807   if (auto *C = dyn_cast<ConstantInt>(SubU)) {
2808     Diff0 = C->getSExtValue();
2809   } else {
2810     return std::nullopt;
2811   }
2812 
2813   Value *MaskK = ConstantInt::get(MaskU->getType(), ~Unknown);
2814   Value *AndK0 = Simplify(CallBuilder(B, CreateAnd(Idx0, MaskK)));
2815   Value *AndK1 = Simplify(CallBuilder(B, CreateAnd(Idx1, MaskK)));
2816   Value *SubK = Simplify(CallBuilder(B, CreateSub(AndK0, AndK1)));
2817   int Diff1 = 0;
2818   if (auto *C = dyn_cast<ConstantInt>(SubK)) {
2819     Diff1 = C->getSExtValue();
2820   } else {
2821     return std::nullopt;
2822   }
2823 
2824   return (Diff0 + Diff1) * Scale;
2825 
2826 #undef CallBuilder
2827 }
2828 
2829 auto HexagonVectorCombine::getNumSignificantBits(const Value *V,
2830                                                  const Instruction *CtxI) const
2831     -> unsigned {
2832   return ComputeMaxSignificantBits(V, DL, /*Depth=*/0, &AC, CtxI, &DT);
2833 }
2834 
2835 auto HexagonVectorCombine::getKnownBits(const Value *V,
2836                                         const Instruction *CtxI) const
2837     -> KnownBits {
2838   return computeKnownBits(V, DL, /*Depth=*/0, &AC, CtxI, &DT);
2839 }
2840 
2841 auto HexagonVectorCombine::isSafeToClone(const Instruction &In) const -> bool {
2842   if (In.mayHaveSideEffects() || In.isAtomic() || In.isVolatile() ||
2843       In.isFenceLike() || In.mayReadOrWriteMemory()) {
2844     return false;
2845   }
2846   if (isa<CallBase>(In) || isa<AllocaInst>(In))
2847     return false;
2848   return true;
2849 }
2850 
2851 template <typename T>
2852 auto HexagonVectorCombine::isSafeToMoveBeforeInBB(const Instruction &In,
2853                                                   BasicBlock::const_iterator To,
2854                                                   const T &IgnoreInsts) const
2855     -> bool {
2856   auto getLocOrNone =
2857       [this](const Instruction &I) -> std::optional<MemoryLocation> {
2858     if (const auto *II = dyn_cast<IntrinsicInst>(&I)) {
2859       switch (II->getIntrinsicID()) {
2860       case Intrinsic::masked_load:
2861         return MemoryLocation::getForArgument(II, 0, TLI);
2862       case Intrinsic::masked_store:
2863         return MemoryLocation::getForArgument(II, 1, TLI);
2864       }
2865     }
2866     return MemoryLocation::getOrNone(&I);
2867   };
2868 
2869   // The source and the destination must be in the same basic block.
2870   const BasicBlock &Block = *In.getParent();
2871   assert(Block.begin() == To || Block.end() == To || To->getParent() == &Block);
2872   // No PHIs.
2873   if (isa<PHINode>(In) || (To != Block.end() && isa<PHINode>(*To)))
2874     return false;
2875 
2876   if (!mayHaveNonDefUseDependency(In))
2877     return true;
2878   bool MayWrite = In.mayWriteToMemory();
2879   auto MaybeLoc = getLocOrNone(In);
2880 
2881   auto From = In.getIterator();
2882   if (From == To)
2883     return true;
2884   bool MoveUp = (To != Block.end() && To->comesBefore(&In));
2885   auto Range =
2886       MoveUp ? std::make_pair(To, From) : std::make_pair(std::next(From), To);
2887   for (auto It = Range.first; It != Range.second; ++It) {
2888     const Instruction &I = *It;
2889     if (llvm::is_contained(IgnoreInsts, &I))
2890       continue;
2891     // assume intrinsic can be ignored
2892     if (auto *II = dyn_cast<IntrinsicInst>(&I)) {
2893       if (II->getIntrinsicID() == Intrinsic::assume)
2894         continue;
2895     }
2896     // Parts based on isSafeToMoveBefore from CoveMoverUtils.cpp.
2897     if (I.mayThrow())
2898       return false;
2899     if (auto *CB = dyn_cast<CallBase>(&I)) {
2900       if (!CB->hasFnAttr(Attribute::WillReturn))
2901         return false;
2902       if (!CB->hasFnAttr(Attribute::NoSync))
2903         return false;
2904     }
2905     if (I.mayReadOrWriteMemory()) {
2906       auto MaybeLocI = getLocOrNone(I);
2907       if (MayWrite || I.mayWriteToMemory()) {
2908         if (!MaybeLoc || !MaybeLocI)
2909           return false;
2910         if (!AA.isNoAlias(*MaybeLoc, *MaybeLocI))
2911           return false;
2912       }
2913     }
2914   }
2915   return true;
2916 }
2917 
2918 auto HexagonVectorCombine::isByteVecTy(Type *Ty) const -> bool {
2919   if (auto *VecTy = dyn_cast<VectorType>(Ty))
2920     return VecTy->getElementType() == getByteTy();
2921   return false;
2922 }
2923 
2924 auto HexagonVectorCombine::getElementRange(IRBuilderBase &Builder, Value *Lo,
2925                                            Value *Hi, int Start,
2926                                            int Length) const -> Value * {
2927   assert(0 <= Start && size_t(Start + Length) < length(Lo) + length(Hi));
2928   SmallVector<int, 128> SMask(Length);
2929   std::iota(SMask.begin(), SMask.end(), Start);
2930   return Builder.CreateShuffleVector(Lo, Hi, SMask, "shf");
2931 }
2932 
2933 // Pass management.
2934 
2935 namespace llvm {
2936 void initializeHexagonVectorCombineLegacyPass(PassRegistry &);
2937 FunctionPass *createHexagonVectorCombineLegacyPass();
2938 } // namespace llvm
2939 
2940 namespace {
2941 class HexagonVectorCombineLegacy : public FunctionPass {
2942 public:
2943   static char ID;
2944 
2945   HexagonVectorCombineLegacy() : FunctionPass(ID) {}
2946 
2947   StringRef getPassName() const override { return "Hexagon Vector Combine"; }
2948 
2949   void getAnalysisUsage(AnalysisUsage &AU) const override {
2950     AU.setPreservesCFG();
2951     AU.addRequired<AAResultsWrapperPass>();
2952     AU.addRequired<AssumptionCacheTracker>();
2953     AU.addRequired<DominatorTreeWrapperPass>();
2954     AU.addRequired<ScalarEvolutionWrapperPass>();
2955     AU.addRequired<TargetLibraryInfoWrapperPass>();
2956     AU.addRequired<TargetPassConfig>();
2957     FunctionPass::getAnalysisUsage(AU);
2958   }
2959 
2960   bool runOnFunction(Function &F) override {
2961     if (skipFunction(F))
2962       return false;
2963     AliasAnalysis &AA = getAnalysis<AAResultsWrapperPass>().getAAResults();
2964     AssumptionCache &AC =
2965         getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
2966     DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
2967     ScalarEvolution &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE();
2968     TargetLibraryInfo &TLI =
2969         getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
2970     auto &TM = getAnalysis<TargetPassConfig>().getTM<HexagonTargetMachine>();
2971     HexagonVectorCombine HVC(F, AA, AC, DT, SE, TLI, TM);
2972     return HVC.run();
2973   }
2974 };
2975 } // namespace
2976 
2977 char HexagonVectorCombineLegacy::ID = 0;
2978 
2979 INITIALIZE_PASS_BEGIN(HexagonVectorCombineLegacy, DEBUG_TYPE,
2980                       "Hexagon Vector Combine", false, false)
2981 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
2982 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
2983 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
2984 INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
2985 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
2986 INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
2987 INITIALIZE_PASS_END(HexagonVectorCombineLegacy, DEBUG_TYPE,
2988                     "Hexagon Vector Combine", false, false)
2989 
2990 FunctionPass *llvm::createHexagonVectorCombineLegacyPass() {
2991   return new HexagonVectorCombineLegacy();
2992 }
2993