1 //===----------- VectorUtils.cpp - Vectorizer utility functions -----------===//
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 vectorizer utilities.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Analysis/VectorUtils.h"
16 /// \brief Identify if the intrinsic is trivially vectorizable.
17 /// This method returns true if the intrinsic's argument types are all
18 /// scalars for the scalar form of the intrinsic and all vectors for
19 /// the vector form of the intrinsic.
20 bool llvm::isTriviallyVectorizable(Intrinsic::ID ID) {
28 case Intrinsic::log10:
31 case Intrinsic::minnum:
32 case Intrinsic::maxnum:
33 case Intrinsic::copysign:
34 case Intrinsic::floor:
36 case Intrinsic::trunc:
38 case Intrinsic::nearbyint:
39 case Intrinsic::round:
40 case Intrinsic::bswap:
41 case Intrinsic::ctpop:
44 case Intrinsic::fmuladd:
54 /// \brief Identifies if the intrinsic has a scalar operand. It check for
55 /// ctlz,cttz and powi special intrinsics whose argument is scalar.
56 bool llvm::hasVectorInstrinsicScalarOpd(Intrinsic::ID ID,
57 unsigned ScalarOpdIdx) {
62 return (ScalarOpdIdx == 1);
68 /// \brief Check call has a unary float signature
69 /// It checks following:
70 /// a) call should have a single argument
71 /// b) argument type should be floating point type
72 /// c) call instruction type and argument type should be same
73 /// d) call should only reads memory.
74 /// If all these condition is met then return ValidIntrinsicID
75 /// else return not_intrinsic.
77 llvm::checkUnaryFloatSignature(const CallInst &I,
78 Intrinsic::ID ValidIntrinsicID) {
79 if (I.getNumArgOperands() != 1 ||
80 !I.getArgOperand(0)->getType()->isFloatingPointTy() ||
81 I.getType() != I.getArgOperand(0)->getType() || !I.onlyReadsMemory())
82 return Intrinsic::not_intrinsic;
84 return ValidIntrinsicID;
87 /// \brief Check call has a binary float signature
88 /// It checks following:
89 /// a) call should have 2 arguments.
90 /// b) arguments type should be floating point type
91 /// c) call instruction type and arguments type should be same
92 /// d) call should only reads memory.
93 /// If all these condition is met then return ValidIntrinsicID
94 /// else return not_intrinsic.
96 llvm::checkBinaryFloatSignature(const CallInst &I,
97 Intrinsic::ID ValidIntrinsicID) {
98 if (I.getNumArgOperands() != 2 ||
99 !I.getArgOperand(0)->getType()->isFloatingPointTy() ||
100 !I.getArgOperand(1)->getType()->isFloatingPointTy() ||
101 I.getType() != I.getArgOperand(0)->getType() ||
102 I.getType() != I.getArgOperand(1)->getType() || !I.onlyReadsMemory())
103 return Intrinsic::not_intrinsic;
105 return ValidIntrinsicID;
108 /// \brief Returns intrinsic ID for call.
109 /// For the input call instruction it finds mapping intrinsic and returns
110 /// its ID, in case it does not found it return not_intrinsic.
111 llvm::Intrinsic::ID llvm::getIntrinsicIDForCall(CallInst *CI,
112 const TargetLibraryInfo *TLI) {
113 // If we have an intrinsic call, check if it is trivially vectorizable.
114 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI)) {
115 Intrinsic::ID ID = II->getIntrinsicID();
116 if (isTriviallyVectorizable(ID) || ID == Intrinsic::lifetime_start ||
117 ID == Intrinsic::lifetime_end || ID == Intrinsic::assume)
119 return Intrinsic::not_intrinsic;
123 return Intrinsic::not_intrinsic;
126 Function *F = CI->getCalledFunction();
127 // We're going to make assumptions on the semantics of the functions, check
128 // that the target knows that it's available in this environment and it does
129 // not have local linkage.
130 if (!F || F->hasLocalLinkage() || !TLI->getLibFunc(F->getName(), Func))
131 return Intrinsic::not_intrinsic;
133 // Otherwise check if we have a call to a function that can be turned into a
141 return checkUnaryFloatSignature(*CI, Intrinsic::sin);
145 return checkUnaryFloatSignature(*CI, Intrinsic::cos);
149 return checkUnaryFloatSignature(*CI, Intrinsic::exp);
153 return checkUnaryFloatSignature(*CI, Intrinsic::exp2);
157 return checkUnaryFloatSignature(*CI, Intrinsic::log);
159 case LibFunc::log10f:
160 case LibFunc::log10l:
161 return checkUnaryFloatSignature(*CI, Intrinsic::log10);
165 return checkUnaryFloatSignature(*CI, Intrinsic::log2);
169 return checkUnaryFloatSignature(*CI, Intrinsic::fabs);
173 return checkBinaryFloatSignature(*CI, Intrinsic::minnum);
177 return checkBinaryFloatSignature(*CI, Intrinsic::maxnum);
178 case LibFunc::copysign:
179 case LibFunc::copysignf:
180 case LibFunc::copysignl:
181 return checkBinaryFloatSignature(*CI, Intrinsic::copysign);
183 case LibFunc::floorf:
184 case LibFunc::floorl:
185 return checkUnaryFloatSignature(*CI, Intrinsic::floor);
189 return checkUnaryFloatSignature(*CI, Intrinsic::ceil);
191 case LibFunc::truncf:
192 case LibFunc::truncl:
193 return checkUnaryFloatSignature(*CI, Intrinsic::trunc);
197 return checkUnaryFloatSignature(*CI, Intrinsic::rint);
198 case LibFunc::nearbyint:
199 case LibFunc::nearbyintf:
200 case LibFunc::nearbyintl:
201 return checkUnaryFloatSignature(*CI, Intrinsic::nearbyint);
203 case LibFunc::roundf:
204 case LibFunc::roundl:
205 return checkUnaryFloatSignature(*CI, Intrinsic::round);
209 return checkBinaryFloatSignature(*CI, Intrinsic::pow);
212 return Intrinsic::not_intrinsic;