1 //===- llvm/Transforms/Utils/VectorUtils.h - Vector utilities -*- C++ -*-=====//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines some vectorizer utilities.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_TRANSFORMS_UTILS_VECTORUTILS_H
15 #define LLVM_TRANSFORMS_UTILS_VECTORUTILS_H
17 #include "llvm/ADT/MapVector.h"
18 #include "llvm/Analysis/TargetLibraryInfo.h"
22 template <typename T> class ArrayRef;
24 class GetElementPtrInst;
26 class ScalarEvolution;
27 class TargetTransformInfo;
35 /// \brief Identify if the intrinsic is trivially vectorizable.
36 /// This method returns true if the intrinsic's argument types are all
37 /// scalars for the scalar form of the intrinsic and all vectors for
38 /// the vector form of the intrinsic.
39 bool isTriviallyVectorizable(Intrinsic::ID ID);
41 /// \brief Identifies if the intrinsic has a scalar operand. It checks for
42 /// ctlz,cttz and powi special intrinsics whose argument is scalar.
43 bool hasVectorInstrinsicScalarOpd(Intrinsic::ID ID, unsigned ScalarOpdIdx);
45 /// \brief Returns intrinsic ID for call.
46 /// For the input call instruction it finds mapping intrinsic and returns
47 /// its intrinsic ID, in case it does not found it return not_intrinsic.
48 Intrinsic::ID getVectorIntrinsicIDForCall(const CallInst *CI,
49 const TargetLibraryInfo *TLI);
51 /// \brief Find the operand of the GEP that should be checked for consecutive
52 /// stores. This ignores trailing indices that have no effect on the final
54 unsigned getGEPInductionOperand(const GetElementPtrInst *Gep);
56 /// \brief If the argument is a GEP, then returns the operand identified by
57 /// getGEPInductionOperand. However, if there is some other non-loop-invariant
58 /// operand, it returns that instead.
59 Value *stripGetElementPtr(Value *Ptr, ScalarEvolution *SE, Loop *Lp);
61 /// \brief If a value has only one user that is a CastInst, return it.
62 Value *getUniqueCastUse(Value *Ptr, Loop *Lp, Type *Ty);
64 /// \brief Get the stride of a pointer access in a loop. Looks for symbolic
65 /// strides "a[i*stride]". Returns the symbolic stride, or null otherwise.
66 Value *getStrideFromPointer(Value *Ptr, ScalarEvolution *SE, Loop *Lp);
68 /// \brief Given a vector and an element number, see if the scalar value is
69 /// already around as a register, for example if it were inserted then extracted
71 Value *findScalarElement(Value *V, unsigned EltNo);
73 /// \brief Get splat value if the input is a splat vector or return nullptr.
74 /// The value may be extracted from a splat constants vector or from
75 /// a sequence of instructions that broadcast a single value into a vector.
76 const Value *getSplatValue(const Value *V);
78 /// \brief Compute a map of integer instructions to their minimum legal type
81 /// C semantics force sub-int-sized values (e.g. i8, i16) to be promoted to int
82 /// type (e.g. i32) whenever arithmetic is performed on them.
84 /// For targets with native i8 or i16 operations, usually InstCombine can shrink
85 /// the arithmetic type down again. However InstCombine refuses to create
86 /// illegal types, so for targets without i8 or i16 registers, the lengthening
87 /// and shrinking remains.
89 /// Most SIMD ISAs (e.g. NEON) however support vectors of i8 or i16 even when
90 /// their scalar equivalents do not, so during vectorization it is important to
91 /// remove these lengthens and truncates when deciding the profitability of
94 /// This function analyzes the given range of instructions and determines the
95 /// minimum type size each can be converted to. It attempts to remove or
96 /// minimize type size changes across each def-use chain, so for example in the
100 /// %2 = add i8 %1, 2
101 /// %3 = load i16, i16*
102 /// %4 = zext i8 %2 to i32
103 /// %5 = zext i16 %3 to i32
104 /// %6 = add i32 %4, %5
105 /// %7 = trunc i32 %6 to i16
107 /// Instruction %6 must be done at least in i16, so computeMinimumValueSizes
108 /// will return: {%1: 16, %2: 16, %3: 16, %4: 16, %5: 16, %6: 16, %7: 16}.
110 /// If the optional TargetTransformInfo is provided, this function tries harder
111 /// to do less work by only looking at illegal types.
112 MapVector<Instruction*, uint64_t>
113 computeMinimumValueSizes(ArrayRef<BasicBlock*> Blocks,
115 const TargetTransformInfo *TTI=nullptr);
117 /// Specifically, let Kinds = [MD_tbaa, MD_alias_scope, MD_noalias, MD_fpmath,
118 /// MD_nontemporal]. For K in Kinds, we get the MDNode for K from each of the
119 /// elements of VL, compute their "intersection" (i.e., the most generic
120 /// metadata value that covers all of the individual values), and set I's
121 /// metadata for M equal to the intersection value.
123 /// This function always sets a (possibly null) value for each K in Kinds.
124 Instruction *propagateMetadata(Instruction *I, ArrayRef<Value *> VL);