Reformat blank lines.
[oota-llvm.git] / include / llvm / Transforms / Utils / VectorUtils.h
1 //===- llvm/Transforms/Utils/VectorUtils.h - Vector utilities -*- C++ -*-=====//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines some vectorizer utilities.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_TRANSFORMS_UTILS_VECTORUTILS_H
15 #define LLVM_TRANSFORMS_UTILS_VECTORUTILS_H
16
17 #include "llvm/IR/Intrinsics.h"
18 #include "llvm/IR/IntrinsicInst.h"
19 #include "llvm/Target/TargetLibraryInfo.h"
20
21 namespace llvm {
22
23 /// \brief Identify if the intrinsic is trivially vectorizable.
24 ///
25 /// This method returns true if the intrinsic's argument types are all
26 /// scalars for the scalar form of the intrinsic and all vectors for
27 /// the vector form of the intrinsic.
28 static inline bool isTriviallyVectorizable(Intrinsic::ID ID) {
29   switch (ID) {
30   case Intrinsic::sqrt:
31   case Intrinsic::sin:
32   case Intrinsic::cos:
33   case Intrinsic::exp:
34   case Intrinsic::exp2:
35   case Intrinsic::log:
36   case Intrinsic::log10:
37   case Intrinsic::log2:
38   case Intrinsic::fabs:
39   case Intrinsic::copysign:
40   case Intrinsic::floor:
41   case Intrinsic::ceil:
42   case Intrinsic::trunc:
43   case Intrinsic::rint:
44   case Intrinsic::nearbyint:
45   case Intrinsic::round:
46   case Intrinsic::ctpop:
47   case Intrinsic::pow:
48   case Intrinsic::fma:
49   case Intrinsic::fmuladd:
50     return true;
51   default:
52     return false;
53   }
54 }
55
56 static Intrinsic::ID checkUnaryFloatSignature(const CallInst &I,
57                                               Intrinsic::ID ValidIntrinsicID) {
58   if (I.getNumArgOperands() != 1 ||
59       !I.getArgOperand(0)->getType()->isFloatingPointTy() ||
60       I.getType() != I.getArgOperand(0)->getType() ||
61       !I.onlyReadsMemory())
62     return Intrinsic::not_intrinsic;
63
64   return ValidIntrinsicID;
65 }
66
67 static Intrinsic::ID checkBinaryFloatSignature(const CallInst &I,
68                                                Intrinsic::ID ValidIntrinsicID) {
69   if (I.getNumArgOperands() != 2 ||
70       !I.getArgOperand(0)->getType()->isFloatingPointTy() ||
71       !I.getArgOperand(1)->getType()->isFloatingPointTy() ||
72       I.getType() != I.getArgOperand(0)->getType() ||
73       I.getType() != I.getArgOperand(1)->getType() ||
74       !I.onlyReadsMemory())
75     return Intrinsic::not_intrinsic;
76
77   return ValidIntrinsicID;
78 }
79
80 static Intrinsic::ID
81 getIntrinsicIDForCall(CallInst *CI, const TargetLibraryInfo *TLI) {
82   // If we have an intrinsic call, check if it is trivially vectorizable.
83   if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI)) {
84     Intrinsic::ID ID = II->getIntrinsicID();
85     if (isTriviallyVectorizable(ID) || ID == Intrinsic::lifetime_start ||
86         ID == Intrinsic::lifetime_end)
87       return ID;
88     else
89       return Intrinsic::not_intrinsic;
90   }
91
92   if (!TLI)
93     return Intrinsic::not_intrinsic;
94
95   LibFunc::Func Func;
96   Function *F = CI->getCalledFunction();
97   // We're going to make assumptions on the semantics of the functions, check
98   // that the target knows that it's available in this environment and it does
99   // not have local linkage.
100   if (!F || F->hasLocalLinkage() || !TLI->getLibFunc(F->getName(), Func))
101     return Intrinsic::not_intrinsic;
102
103   // Otherwise check if we have a call to a function that can be turned into a
104   // vector intrinsic.
105   switch (Func) {
106   default:
107     break;
108   case LibFunc::sin:
109   case LibFunc::sinf:
110   case LibFunc::sinl:
111     return checkUnaryFloatSignature(*CI, Intrinsic::sin);
112   case LibFunc::cos:
113   case LibFunc::cosf:
114   case LibFunc::cosl:
115     return checkUnaryFloatSignature(*CI, Intrinsic::cos);
116   case LibFunc::exp:
117   case LibFunc::expf:
118   case LibFunc::expl:
119     return checkUnaryFloatSignature(*CI, Intrinsic::exp);
120   case LibFunc::exp2:
121   case LibFunc::exp2f:
122   case LibFunc::exp2l:
123     return checkUnaryFloatSignature(*CI, Intrinsic::exp2);
124   case LibFunc::log:
125   case LibFunc::logf:
126   case LibFunc::logl:
127     return checkUnaryFloatSignature(*CI, Intrinsic::log);
128   case LibFunc::log10:
129   case LibFunc::log10f:
130   case LibFunc::log10l:
131     return checkUnaryFloatSignature(*CI, Intrinsic::log10);
132   case LibFunc::log2:
133   case LibFunc::log2f:
134   case LibFunc::log2l:
135     return checkUnaryFloatSignature(*CI, Intrinsic::log2);
136   case LibFunc::fabs:
137   case LibFunc::fabsf:
138   case LibFunc::fabsl:
139     return checkUnaryFloatSignature(*CI, Intrinsic::fabs);
140   case LibFunc::copysign:
141   case LibFunc::copysignf:
142   case LibFunc::copysignl:
143     return checkBinaryFloatSignature(*CI, Intrinsic::copysign);
144   case LibFunc::floor:
145   case LibFunc::floorf:
146   case LibFunc::floorl:
147     return checkUnaryFloatSignature(*CI, Intrinsic::floor);
148   case LibFunc::ceil:
149   case LibFunc::ceilf:
150   case LibFunc::ceill:
151     return checkUnaryFloatSignature(*CI, Intrinsic::ceil);
152   case LibFunc::trunc:
153   case LibFunc::truncf:
154   case LibFunc::truncl:
155     return checkUnaryFloatSignature(*CI, Intrinsic::trunc);
156   case LibFunc::rint:
157   case LibFunc::rintf:
158   case LibFunc::rintl:
159     return checkUnaryFloatSignature(*CI, Intrinsic::rint);
160   case LibFunc::nearbyint:
161   case LibFunc::nearbyintf:
162   case LibFunc::nearbyintl:
163     return checkUnaryFloatSignature(*CI, Intrinsic::nearbyint);
164   case LibFunc::round:
165   case LibFunc::roundf:
166   case LibFunc::roundl:
167     return checkUnaryFloatSignature(*CI, Intrinsic::round);
168   case LibFunc::pow:
169   case LibFunc::powf:
170   case LibFunc::powl:
171     return checkBinaryFloatSignature(*CI, Intrinsic::pow);
172   }
173
174   return Intrinsic::not_intrinsic;
175 }
176
177 } // llvm namespace
178
179 #endif