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