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