VectorUtils.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. //===----------- VectorUtils.cpp - Vectorizer utility functions -----------===//
  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 vectorizer utilities.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Analysis/LoopInfo.h"
  14. #include "llvm/Analysis/ScalarEvolutionExpressions.h"
  15. #include "llvm/Analysis/ScalarEvolution.h"
  16. #include "llvm/Analysis/VectorUtils.h"
  17. #include "llvm/IR/GetElementPtrTypeIterator.h"
  18. #include "llvm/IR/PatternMatch.h"
  19. #include "llvm/IR/Value.h"
  20. /// \brief Identify if the intrinsic is trivially vectorizable.
  21. /// This method returns true if the intrinsic's argument types are all
  22. /// scalars for the scalar form of the intrinsic and all vectors for
  23. /// the vector form of the intrinsic.
  24. bool llvm::isTriviallyVectorizable(Intrinsic::ID ID) {
  25. switch (ID) {
  26. case Intrinsic::sqrt:
  27. case Intrinsic::sin:
  28. case Intrinsic::cos:
  29. case Intrinsic::exp:
  30. case Intrinsic::exp2:
  31. case Intrinsic::log:
  32. case Intrinsic::log10:
  33. case Intrinsic::log2:
  34. case Intrinsic::fabs:
  35. case Intrinsic::minnum:
  36. case Intrinsic::maxnum:
  37. case Intrinsic::copysign:
  38. case Intrinsic::floor:
  39. case Intrinsic::ceil:
  40. case Intrinsic::trunc:
  41. case Intrinsic::rint:
  42. case Intrinsic::nearbyint:
  43. case Intrinsic::round:
  44. case Intrinsic::bswap:
  45. case Intrinsic::ctpop:
  46. case Intrinsic::pow:
  47. case Intrinsic::fma:
  48. case Intrinsic::fmuladd:
  49. case Intrinsic::ctlz:
  50. case Intrinsic::cttz:
  51. case Intrinsic::powi:
  52. return true;
  53. default:
  54. return false;
  55. }
  56. }
  57. /// \brief Identifies if the intrinsic has a scalar operand. It check for
  58. /// ctlz,cttz and powi special intrinsics whose argument is scalar.
  59. bool llvm::hasVectorInstrinsicScalarOpd(Intrinsic::ID ID,
  60. unsigned ScalarOpdIdx) {
  61. switch (ID) {
  62. case Intrinsic::ctlz:
  63. case Intrinsic::cttz:
  64. case Intrinsic::powi:
  65. return (ScalarOpdIdx == 1);
  66. default:
  67. return false;
  68. }
  69. }
  70. /// \brief Check call has a unary float signature
  71. /// It checks following:
  72. /// a) call should have a single argument
  73. /// b) argument type should be floating point type
  74. /// c) call instruction type and argument type should be same
  75. /// d) call should only reads memory.
  76. /// If all these condition is met then return ValidIntrinsicID
  77. /// else return not_intrinsic.
  78. llvm::Intrinsic::ID
  79. llvm::checkUnaryFloatSignature(const CallInst &I,
  80. Intrinsic::ID ValidIntrinsicID) {
  81. if (I.getNumArgOperands() != 1 ||
  82. !I.getArgOperand(0)->getType()->isFloatingPointTy() ||
  83. I.getType() != I.getArgOperand(0)->getType() || !I.onlyReadsMemory())
  84. return Intrinsic::not_intrinsic;
  85. return ValidIntrinsicID;
  86. }
  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.
  95. llvm::Intrinsic::ID
  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;
  104. return ValidIntrinsicID;
  105. }
  106. /// \brief Returns intrinsic ID for call.
  107. /// For the input call instruction it finds mapping intrinsic and returns
  108. /// its ID, in case it does not found it return not_intrinsic.
  109. llvm::Intrinsic::ID llvm::getIntrinsicIDForCall(CallInst *CI,
  110. const TargetLibraryInfo *TLI) {
  111. // If we have an intrinsic call, check if it is trivially vectorizable.
  112. if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI)) {
  113. Intrinsic::ID ID = II->getIntrinsicID();
  114. if (isTriviallyVectorizable(ID) || ID == Intrinsic::lifetime_start ||
  115. ID == Intrinsic::lifetime_end || ID == Intrinsic::assume)
  116. return ID;
  117. return Intrinsic::not_intrinsic;
  118. }
  119. if (!TLI)
  120. return Intrinsic::not_intrinsic;
  121. LibFunc::Func Func;
  122. Function *F = CI->getCalledFunction();
  123. // We're going to make assumptions on the semantics of the functions, check
  124. // that the target knows that it's available in this environment and it does
  125. // not have local linkage.
  126. if (!F || F->hasLocalLinkage() || !TLI->getLibFunc(F->getName(), Func))
  127. return Intrinsic::not_intrinsic;
  128. // Otherwise check if we have a call to a function that can be turned into a
  129. // vector intrinsic.
  130. switch (Func) {
  131. default:
  132. break;
  133. case LibFunc::sin:
  134. case LibFunc::sinf:
  135. case LibFunc::sinl:
  136. return checkUnaryFloatSignature(*CI, Intrinsic::sin);
  137. case LibFunc::cos:
  138. case LibFunc::cosf:
  139. case LibFunc::cosl:
  140. return checkUnaryFloatSignature(*CI, Intrinsic::cos);
  141. case LibFunc::exp:
  142. case LibFunc::expf:
  143. case LibFunc::expl:
  144. return checkUnaryFloatSignature(*CI, Intrinsic::exp);
  145. case LibFunc::exp2:
  146. case LibFunc::exp2f:
  147. case LibFunc::exp2l:
  148. return checkUnaryFloatSignature(*CI, Intrinsic::exp2);
  149. case LibFunc::log:
  150. case LibFunc::logf:
  151. case LibFunc::logl:
  152. return checkUnaryFloatSignature(*CI, Intrinsic::log);
  153. case LibFunc::log10:
  154. case LibFunc::log10f:
  155. case LibFunc::log10l:
  156. return checkUnaryFloatSignature(*CI, Intrinsic::log10);
  157. case LibFunc::log2:
  158. case LibFunc::log2f:
  159. case LibFunc::log2l:
  160. return checkUnaryFloatSignature(*CI, Intrinsic::log2);
  161. case LibFunc::fabs:
  162. case LibFunc::fabsf:
  163. case LibFunc::fabsl:
  164. return checkUnaryFloatSignature(*CI, Intrinsic::fabs);
  165. case LibFunc::fmin:
  166. case LibFunc::fminf:
  167. case LibFunc::fminl:
  168. return checkBinaryFloatSignature(*CI, Intrinsic::minnum);
  169. case LibFunc::fmax:
  170. case LibFunc::fmaxf:
  171. case LibFunc::fmaxl:
  172. return checkBinaryFloatSignature(*CI, Intrinsic::maxnum);
  173. case LibFunc::copysign:
  174. case LibFunc::copysignf:
  175. case LibFunc::copysignl:
  176. return checkBinaryFloatSignature(*CI, Intrinsic::copysign);
  177. case LibFunc::floor:
  178. case LibFunc::floorf:
  179. case LibFunc::floorl:
  180. return checkUnaryFloatSignature(*CI, Intrinsic::floor);
  181. case LibFunc::ceil:
  182. case LibFunc::ceilf:
  183. case LibFunc::ceill:
  184. return checkUnaryFloatSignature(*CI, Intrinsic::ceil);
  185. case LibFunc::trunc:
  186. case LibFunc::truncf:
  187. case LibFunc::truncl:
  188. return checkUnaryFloatSignature(*CI, Intrinsic::trunc);
  189. case LibFunc::rint:
  190. case LibFunc::rintf:
  191. case LibFunc::rintl:
  192. return checkUnaryFloatSignature(*CI, Intrinsic::rint);
  193. case LibFunc::nearbyint:
  194. case LibFunc::nearbyintf:
  195. case LibFunc::nearbyintl:
  196. return checkUnaryFloatSignature(*CI, Intrinsic::nearbyint);
  197. case LibFunc::round:
  198. case LibFunc::roundf:
  199. case LibFunc::roundl:
  200. return checkUnaryFloatSignature(*CI, Intrinsic::round);
  201. case LibFunc::pow:
  202. case LibFunc::powf:
  203. case LibFunc::powl:
  204. return checkBinaryFloatSignature(*CI, Intrinsic::pow);
  205. }
  206. return Intrinsic::not_intrinsic;
  207. }
  208. /// \brief Find the operand of the GEP that should be checked for consecutive
  209. /// stores. This ignores trailing indices that have no effect on the final
  210. /// pointer.
  211. unsigned llvm::getGEPInductionOperand(const GetElementPtrInst *Gep) {
  212. const DataLayout &DL = Gep->getModule()->getDataLayout();
  213. unsigned LastOperand = Gep->getNumOperands() - 1;
  214. unsigned GEPAllocSize = DL.getTypeAllocSize(
  215. cast<PointerType>(Gep->getType()->getScalarType())->getElementType());
  216. // Walk backwards and try to peel off zeros.
  217. while (LastOperand > 1 &&
  218. match(Gep->getOperand(LastOperand), llvm::PatternMatch::m_Zero())) {
  219. // Find the type we're currently indexing into.
  220. gep_type_iterator GEPTI = gep_type_begin(Gep);
  221. std::advance(GEPTI, LastOperand - 1);
  222. // If it's a type with the same allocation size as the result of the GEP we
  223. // can peel off the zero index.
  224. if (DL.getTypeAllocSize(*GEPTI) != GEPAllocSize)
  225. break;
  226. --LastOperand;
  227. }
  228. return LastOperand;
  229. }
  230. /// \brief If the argument is a GEP, then returns the operand identified by
  231. /// getGEPInductionOperand. However, if there is some other non-loop-invariant
  232. /// operand, it returns that instead.
  233. llvm::Value *llvm::stripGetElementPtr(llvm::Value *Ptr, ScalarEvolution *SE,
  234. Loop *Lp) {
  235. GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr);
  236. if (!GEP)
  237. return Ptr;
  238. unsigned InductionOperand = getGEPInductionOperand(GEP);
  239. // Check that all of the gep indices are uniform except for our induction
  240. // operand.
  241. for (unsigned i = 0, e = GEP->getNumOperands(); i != e; ++i)
  242. if (i != InductionOperand &&
  243. !SE->isLoopInvariant(SE->getSCEV(GEP->getOperand(i)), Lp))
  244. return Ptr;
  245. return GEP->getOperand(InductionOperand);
  246. }
  247. /// \brief If a value has only one user that is a CastInst, return it.
  248. llvm::Value *llvm::getUniqueCastUse(llvm::Value *Ptr, Loop *Lp, Type *Ty) {
  249. llvm::Value *UniqueCast = nullptr;
  250. for (User *U : Ptr->users()) {
  251. CastInst *CI = dyn_cast<CastInst>(U);
  252. if (CI && CI->getType() == Ty) {
  253. if (!UniqueCast)
  254. UniqueCast = CI;
  255. else
  256. return nullptr;
  257. }
  258. }
  259. return UniqueCast;
  260. }
  261. /// \brief Get the stride of a pointer access in a loop. Looks for symbolic
  262. /// strides "a[i*stride]". Returns the symbolic stride, or null otherwise.
  263. llvm::Value *llvm::getStrideFromPointer(llvm::Value *Ptr, ScalarEvolution *SE,
  264. Loop *Lp) {
  265. const PointerType *PtrTy = dyn_cast<PointerType>(Ptr->getType());
  266. if (!PtrTy || PtrTy->isAggregateType())
  267. return nullptr;
  268. // Try to remove a gep instruction to make the pointer (actually index at this
  269. // point) easier analyzable. If OrigPtr is equal to Ptr we are analzying the
  270. // pointer, otherwise, we are analyzing the index.
  271. llvm::Value *OrigPtr = Ptr;
  272. // The size of the pointer access.
  273. int64_t PtrAccessSize = 1;
  274. Ptr = stripGetElementPtr(Ptr, SE, Lp);
  275. const SCEV *V = SE->getSCEV(Ptr);
  276. if (Ptr != OrigPtr)
  277. // Strip off casts.
  278. while (const SCEVCastExpr *C = dyn_cast<SCEVCastExpr>(V))
  279. V = C->getOperand();
  280. const SCEVAddRecExpr *S = dyn_cast<SCEVAddRecExpr>(V);
  281. if (!S)
  282. return nullptr;
  283. V = S->getStepRecurrence(*SE);
  284. if (!V)
  285. return nullptr;
  286. // Strip off the size of access multiplication if we are still analyzing the
  287. // pointer.
  288. if (OrigPtr == Ptr) {
  289. const DataLayout &DL = Lp->getHeader()->getModule()->getDataLayout();
  290. DL.getTypeAllocSize(PtrTy->getElementType());
  291. if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(V)) {
  292. if (M->getOperand(0)->getSCEVType() != scConstant)
  293. return nullptr;
  294. const APInt &APStepVal =
  295. cast<SCEVConstant>(M->getOperand(0))->getValue()->getValue();
  296. // Huge step value - give up.
  297. if (APStepVal.getBitWidth() > 64)
  298. return nullptr;
  299. int64_t StepVal = APStepVal.getSExtValue();
  300. if (PtrAccessSize != StepVal)
  301. return nullptr;
  302. V = M->getOperand(1);
  303. }
  304. }
  305. // Strip off casts.
  306. Type *StripedOffRecurrenceCast = nullptr;
  307. if (const SCEVCastExpr *C = dyn_cast<SCEVCastExpr>(V)) {
  308. StripedOffRecurrenceCast = C->getType();
  309. V = C->getOperand();
  310. }
  311. // Look for the loop invariant symbolic value.
  312. const SCEVUnknown *U = dyn_cast<SCEVUnknown>(V);
  313. if (!U)
  314. return nullptr;
  315. llvm::Value *Stride = U->getValue();
  316. if (!Lp->isLoopInvariant(Stride))
  317. return nullptr;
  318. // If we have stripped off the recurrence cast we have to make sure that we
  319. // return the value that is used in this loop so that we can replace it later.
  320. if (StripedOffRecurrenceCast)
  321. Stride = getUniqueCastUse(Stride, Lp, StripedOffRecurrenceCast);
  322. return Stride;
  323. }