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