IntrinsicLowering.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. //===-- IntrinsicLowering.cpp - Intrinsic Lowering default implementation -===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements the IntrinsicLowering class.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/CodeGen/IntrinsicLowering.h"
  13. #include "llvm/ADT/SmallVector.h"
  14. #include "llvm/IR/Constants.h"
  15. #include "llvm/IR/DataLayout.h"
  16. #include "llvm/IR/DerivedTypes.h"
  17. #include "llvm/IR/IRBuilder.h"
  18. #include "llvm/IR/Module.h"
  19. #include "llvm/IR/Type.h"
  20. #include "llvm/Support/ErrorHandling.h"
  21. #include "llvm/Support/raw_ostream.h"
  22. using namespace llvm;
  23. /// This function is used when we want to lower an intrinsic call to a call of
  24. /// an external function. This handles hard cases such as when there was already
  25. /// a prototype for the external function, but that prototype doesn't match the
  26. /// arguments we expect to pass in.
  27. template <class ArgIt>
  28. static CallInst *ReplaceCallWith(const char *NewFn, CallInst *CI,
  29. ArgIt ArgBegin, ArgIt ArgEnd,
  30. Type *RetTy) {
  31. // If we haven't already looked up this function, check to see if the
  32. // program already contains a function with this name.
  33. Module *M = CI->getModule();
  34. // Get or insert the definition now.
  35. std::vector<Type *> ParamTys;
  36. for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
  37. ParamTys.push_back((*I)->getType());
  38. FunctionCallee FCache =
  39. M->getOrInsertFunction(NewFn, FunctionType::get(RetTy, ParamTys, false));
  40. IRBuilder<> Builder(CI->getParent(), CI->getIterator());
  41. SmallVector<Value *, 8> Args(ArgBegin, ArgEnd);
  42. CallInst *NewCI = Builder.CreateCall(FCache, Args);
  43. NewCI->setName(CI->getName());
  44. if (!CI->use_empty())
  45. CI->replaceAllUsesWith(NewCI);
  46. return NewCI;
  47. }
  48. /// Emit the code to lower bswap of V before the specified instruction IP.
  49. static Value *LowerBSWAP(LLVMContext &Context, Value *V, Instruction *IP) {
  50. assert(V->getType()->isIntOrIntVectorTy() && "Can't bswap a non-integer type!");
  51. unsigned BitSize = V->getType()->getScalarSizeInBits();
  52. IRBuilder<> Builder(IP);
  53. switch(BitSize) {
  54. default: llvm_unreachable("Unhandled type size of value to byteswap!");
  55. case 16: {
  56. Value *Tmp1 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8),
  57. "bswap.2");
  58. Value *Tmp2 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8),
  59. "bswap.1");
  60. V = Builder.CreateOr(Tmp1, Tmp2, "bswap.i16");
  61. break;
  62. }
  63. case 32: {
  64. Value *Tmp4 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 24),
  65. "bswap.4");
  66. Value *Tmp3 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8),
  67. "bswap.3");
  68. Value *Tmp2 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8),
  69. "bswap.2");
  70. Value *Tmp1 = Builder.CreateLShr(V,ConstantInt::get(V->getType(), 24),
  71. "bswap.1");
  72. Tmp3 = Builder.CreateAnd(Tmp3,
  73. ConstantInt::get(V->getType(), 0xFF0000),
  74. "bswap.and3");
  75. Tmp2 = Builder.CreateAnd(Tmp2,
  76. ConstantInt::get(V->getType(), 0xFF00),
  77. "bswap.and2");
  78. Tmp4 = Builder.CreateOr(Tmp4, Tmp3, "bswap.or1");
  79. Tmp2 = Builder.CreateOr(Tmp2, Tmp1, "bswap.or2");
  80. V = Builder.CreateOr(Tmp4, Tmp2, "bswap.i32");
  81. break;
  82. }
  83. case 64: {
  84. Value *Tmp8 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 56),
  85. "bswap.8");
  86. Value *Tmp7 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 40),
  87. "bswap.7");
  88. Value *Tmp6 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 24),
  89. "bswap.6");
  90. Value *Tmp5 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8),
  91. "bswap.5");
  92. Value* Tmp4 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8),
  93. "bswap.4");
  94. Value* Tmp3 = Builder.CreateLShr(V,
  95. ConstantInt::get(V->getType(), 24),
  96. "bswap.3");
  97. Value* Tmp2 = Builder.CreateLShr(V,
  98. ConstantInt::get(V->getType(), 40),
  99. "bswap.2");
  100. Value* Tmp1 = Builder.CreateLShr(V,
  101. ConstantInt::get(V->getType(), 56),
  102. "bswap.1");
  103. Tmp7 = Builder.CreateAnd(Tmp7,
  104. ConstantInt::get(V->getType(),
  105. 0xFF000000000000ULL),
  106. "bswap.and7");
  107. Tmp6 = Builder.CreateAnd(Tmp6,
  108. ConstantInt::get(V->getType(),
  109. 0xFF0000000000ULL),
  110. "bswap.and6");
  111. Tmp5 = Builder.CreateAnd(Tmp5,
  112. ConstantInt::get(V->getType(),
  113. 0xFF00000000ULL),
  114. "bswap.and5");
  115. Tmp4 = Builder.CreateAnd(Tmp4,
  116. ConstantInt::get(V->getType(),
  117. 0xFF000000ULL),
  118. "bswap.and4");
  119. Tmp3 = Builder.CreateAnd(Tmp3,
  120. ConstantInt::get(V->getType(),
  121. 0xFF0000ULL),
  122. "bswap.and3");
  123. Tmp2 = Builder.CreateAnd(Tmp2,
  124. ConstantInt::get(V->getType(),
  125. 0xFF00ULL),
  126. "bswap.and2");
  127. Tmp8 = Builder.CreateOr(Tmp8, Tmp7, "bswap.or1");
  128. Tmp6 = Builder.CreateOr(Tmp6, Tmp5, "bswap.or2");
  129. Tmp4 = Builder.CreateOr(Tmp4, Tmp3, "bswap.or3");
  130. Tmp2 = Builder.CreateOr(Tmp2, Tmp1, "bswap.or4");
  131. Tmp8 = Builder.CreateOr(Tmp8, Tmp6, "bswap.or5");
  132. Tmp4 = Builder.CreateOr(Tmp4, Tmp2, "bswap.or6");
  133. V = Builder.CreateOr(Tmp8, Tmp4, "bswap.i64");
  134. break;
  135. }
  136. }
  137. return V;
  138. }
  139. /// Emit the code to lower ctpop of V before the specified instruction IP.
  140. static Value *LowerCTPOP(LLVMContext &Context, Value *V, Instruction *IP) {
  141. assert(V->getType()->isIntegerTy() && "Can't ctpop a non-integer type!");
  142. static const uint64_t MaskValues[6] = {
  143. 0x5555555555555555ULL, 0x3333333333333333ULL,
  144. 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
  145. 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
  146. };
  147. IRBuilder<> Builder(IP);
  148. unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
  149. unsigned WordSize = (BitSize + 63) / 64;
  150. Value *Count = ConstantInt::get(V->getType(), 0);
  151. for (unsigned n = 0; n < WordSize; ++n) {
  152. Value *PartValue = V;
  153. for (unsigned i = 1, ct = 0; i < (BitSize>64 ? 64 : BitSize);
  154. i <<= 1, ++ct) {
  155. Value *MaskCst = ConstantInt::get(V->getType(), MaskValues[ct]);
  156. Value *LHS = Builder.CreateAnd(PartValue, MaskCst, "cppop.and1");
  157. Value *VShift = Builder.CreateLShr(PartValue,
  158. ConstantInt::get(V->getType(), i),
  159. "ctpop.sh");
  160. Value *RHS = Builder.CreateAnd(VShift, MaskCst, "cppop.and2");
  161. PartValue = Builder.CreateAdd(LHS, RHS, "ctpop.step");
  162. }
  163. Count = Builder.CreateAdd(PartValue, Count, "ctpop.part");
  164. if (BitSize > 64) {
  165. V = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 64),
  166. "ctpop.part.sh");
  167. BitSize -= 64;
  168. }
  169. }
  170. return Count;
  171. }
  172. /// Emit the code to lower ctlz of V before the specified instruction IP.
  173. static Value *LowerCTLZ(LLVMContext &Context, Value *V, Instruction *IP) {
  174. IRBuilder<> Builder(IP);
  175. unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
  176. for (unsigned i = 1; i < BitSize; i <<= 1) {
  177. Value *ShVal = ConstantInt::get(V->getType(), i);
  178. ShVal = Builder.CreateLShr(V, ShVal, "ctlz.sh");
  179. V = Builder.CreateOr(V, ShVal, "ctlz.step");
  180. }
  181. V = Builder.CreateNot(V);
  182. return LowerCTPOP(Context, V, IP);
  183. }
  184. static void ReplaceFPIntrinsicWithCall(CallInst *CI, const char *Fname,
  185. const char *Dname,
  186. const char *LDname) {
  187. switch (CI->getArgOperand(0)->getType()->getTypeID()) {
  188. default: llvm_unreachable("Invalid type in intrinsic");
  189. case Type::FloatTyID:
  190. ReplaceCallWith(Fname, CI, CI->arg_begin(), CI->arg_end(),
  191. Type::getFloatTy(CI->getContext()));
  192. break;
  193. case Type::DoubleTyID:
  194. ReplaceCallWith(Dname, CI, CI->arg_begin(), CI->arg_end(),
  195. Type::getDoubleTy(CI->getContext()));
  196. break;
  197. case Type::X86_FP80TyID:
  198. case Type::FP128TyID:
  199. case Type::PPC_FP128TyID:
  200. ReplaceCallWith(LDname, CI, CI->arg_begin(), CI->arg_end(),
  201. CI->getArgOperand(0)->getType());
  202. break;
  203. }
  204. }
  205. void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
  206. IRBuilder<> Builder(CI);
  207. LLVMContext &Context = CI->getContext();
  208. const Function *Callee = CI->getCalledFunction();
  209. assert(Callee && "Cannot lower an indirect call!");
  210. switch (Callee->getIntrinsicID()) {
  211. case Intrinsic::not_intrinsic:
  212. report_fatal_error("Cannot lower a call to a non-intrinsic function '"+
  213. Callee->getName() + "'!");
  214. default:
  215. report_fatal_error("Code generator does not support intrinsic function '"+
  216. Callee->getName()+"'!");
  217. case Intrinsic::expect: {
  218. // Just replace __builtin_expect(exp, c) with EXP.
  219. Value *V = CI->getArgOperand(0);
  220. CI->replaceAllUsesWith(V);
  221. break;
  222. }
  223. case Intrinsic::ctpop:
  224. CI->replaceAllUsesWith(LowerCTPOP(Context, CI->getArgOperand(0), CI));
  225. break;
  226. case Intrinsic::bswap:
  227. CI->replaceAllUsesWith(LowerBSWAP(Context, CI->getArgOperand(0), CI));
  228. break;
  229. case Intrinsic::ctlz:
  230. CI->replaceAllUsesWith(LowerCTLZ(Context, CI->getArgOperand(0), CI));
  231. break;
  232. case Intrinsic::cttz: {
  233. // cttz(x) -> ctpop(~X & (X-1))
  234. Value *Src = CI->getArgOperand(0);
  235. Value *NotSrc = Builder.CreateNot(Src);
  236. NotSrc->setName(Src->getName() + ".not");
  237. Value *SrcM1 = ConstantInt::get(Src->getType(), 1);
  238. SrcM1 = Builder.CreateSub(Src, SrcM1);
  239. Src = LowerCTPOP(Context, Builder.CreateAnd(NotSrc, SrcM1), CI);
  240. CI->replaceAllUsesWith(Src);
  241. break;
  242. }
  243. case Intrinsic::stacksave:
  244. case Intrinsic::stackrestore: {
  245. if (!Warned)
  246. errs() << "WARNING: this target does not support the llvm.stack"
  247. << (Callee->getIntrinsicID() == Intrinsic::stacksave ?
  248. "save" : "restore") << " intrinsic.\n";
  249. Warned = true;
  250. if (Callee->getIntrinsicID() == Intrinsic::stacksave)
  251. CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
  252. break;
  253. }
  254. case Intrinsic::get_dynamic_area_offset:
  255. errs() << "WARNING: this target does not support the custom llvm.get."
  256. "dynamic.area.offset. It is being lowered to a constant 0\n";
  257. // Just lower it to a constant 0 because for most targets
  258. // @llvm.get.dynamic.area.offset is lowered to zero.
  259. CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 0));
  260. break;
  261. case Intrinsic::returnaddress:
  262. case Intrinsic::frameaddress:
  263. errs() << "WARNING: this target does not support the llvm."
  264. << (Callee->getIntrinsicID() == Intrinsic::returnaddress ?
  265. "return" : "frame") << "address intrinsic.\n";
  266. CI->replaceAllUsesWith(
  267. ConstantPointerNull::get(cast<PointerType>(CI->getType())));
  268. break;
  269. case Intrinsic::addressofreturnaddress:
  270. errs() << "WARNING: this target does not support the "
  271. "llvm.addressofreturnaddress intrinsic.\n";
  272. CI->replaceAllUsesWith(
  273. ConstantPointerNull::get(cast<PointerType>(CI->getType())));
  274. break;
  275. case Intrinsic::prefetch:
  276. break; // Simply strip out prefetches on unsupported architectures
  277. case Intrinsic::pcmarker:
  278. break; // Simply strip out pcmarker on unsupported architectures
  279. case Intrinsic::readcyclecounter: {
  280. errs() << "WARNING: this target does not support the llvm.readcyclecoun"
  281. << "ter intrinsic. It is being lowered to a constant 0\n";
  282. CI->replaceAllUsesWith(ConstantInt::get(Type::getInt64Ty(Context), 0));
  283. break;
  284. }
  285. case Intrinsic::dbg_declare:
  286. case Intrinsic::dbg_label:
  287. break; // Simply strip out debugging intrinsics
  288. case Intrinsic::eh_typeid_for:
  289. // Return something different to eh_selector.
  290. CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 1));
  291. break;
  292. case Intrinsic::annotation:
  293. case Intrinsic::ptr_annotation:
  294. // Just drop the annotation, but forward the value
  295. CI->replaceAllUsesWith(CI->getOperand(0));
  296. break;
  297. case Intrinsic::assume:
  298. case Intrinsic::experimental_noalias_scope_decl:
  299. case Intrinsic::var_annotation:
  300. break; // Strip out these intrinsics
  301. case Intrinsic::memcpy: {
  302. Type *IntPtr = DL.getIntPtrType(Context);
  303. Value *Size = Builder.CreateIntCast(CI->getArgOperand(2), IntPtr,
  304. /* isSigned */ false);
  305. Value *Ops[3];
  306. Ops[0] = CI->getArgOperand(0);
  307. Ops[1] = CI->getArgOperand(1);
  308. Ops[2] = Size;
  309. ReplaceCallWith("memcpy", CI, Ops, Ops+3, CI->getArgOperand(0)->getType());
  310. break;
  311. }
  312. case Intrinsic::memmove: {
  313. Type *IntPtr = DL.getIntPtrType(Context);
  314. Value *Size = Builder.CreateIntCast(CI->getArgOperand(2), IntPtr,
  315. /* isSigned */ false);
  316. Value *Ops[3];
  317. Ops[0] = CI->getArgOperand(0);
  318. Ops[1] = CI->getArgOperand(1);
  319. Ops[2] = Size;
  320. ReplaceCallWith("memmove", CI, Ops, Ops+3, CI->getArgOperand(0)->getType());
  321. break;
  322. }
  323. case Intrinsic::memset: {
  324. Value *Op0 = CI->getArgOperand(0);
  325. Type *IntPtr = DL.getIntPtrType(Op0->getType());
  326. Value *Size = Builder.CreateIntCast(CI->getArgOperand(2), IntPtr,
  327. /* isSigned */ false);
  328. Value *Ops[3];
  329. Ops[0] = Op0;
  330. // Extend the amount to i32.
  331. Ops[1] = Builder.CreateIntCast(CI->getArgOperand(1),
  332. Type::getInt32Ty(Context),
  333. /* isSigned */ false);
  334. Ops[2] = Size;
  335. ReplaceCallWith("memset", CI, Ops, Ops+3, CI->getArgOperand(0)->getType());
  336. break;
  337. }
  338. case Intrinsic::sqrt: {
  339. ReplaceFPIntrinsicWithCall(CI, "sqrtf", "sqrt", "sqrtl");
  340. break;
  341. }
  342. case Intrinsic::log: {
  343. ReplaceFPIntrinsicWithCall(CI, "logf", "log", "logl");
  344. break;
  345. }
  346. case Intrinsic::log2: {
  347. ReplaceFPIntrinsicWithCall(CI, "log2f", "log2", "log2l");
  348. break;
  349. }
  350. case Intrinsic::log10: {
  351. ReplaceFPIntrinsicWithCall(CI, "log10f", "log10", "log10l");
  352. break;
  353. }
  354. case Intrinsic::exp: {
  355. ReplaceFPIntrinsicWithCall(CI, "expf", "exp", "expl");
  356. break;
  357. }
  358. case Intrinsic::exp2: {
  359. ReplaceFPIntrinsicWithCall(CI, "exp2f", "exp2", "exp2l");
  360. break;
  361. }
  362. case Intrinsic::pow: {
  363. ReplaceFPIntrinsicWithCall(CI, "powf", "pow", "powl");
  364. break;
  365. }
  366. case Intrinsic::sin: {
  367. ReplaceFPIntrinsicWithCall(CI, "sinf", "sin", "sinl");
  368. break;
  369. }
  370. case Intrinsic::cos: {
  371. ReplaceFPIntrinsicWithCall(CI, "cosf", "cos", "cosl");
  372. break;
  373. }
  374. case Intrinsic::floor: {
  375. ReplaceFPIntrinsicWithCall(CI, "floorf", "floor", "floorl");
  376. break;
  377. }
  378. case Intrinsic::ceil: {
  379. ReplaceFPIntrinsicWithCall(CI, "ceilf", "ceil", "ceill");
  380. break;
  381. }
  382. case Intrinsic::trunc: {
  383. ReplaceFPIntrinsicWithCall(CI, "truncf", "trunc", "truncl");
  384. break;
  385. }
  386. case Intrinsic::round: {
  387. ReplaceFPIntrinsicWithCall(CI, "roundf", "round", "roundl");
  388. break;
  389. }
  390. case Intrinsic::roundeven: {
  391. ReplaceFPIntrinsicWithCall(CI, "roundevenf", "roundeven", "roundevenl");
  392. break;
  393. }
  394. case Intrinsic::copysign: {
  395. ReplaceFPIntrinsicWithCall(CI, "copysignf", "copysign", "copysignl");
  396. break;
  397. }
  398. case Intrinsic::get_rounding:
  399. // Lower to "round to the nearest"
  400. if (!CI->getType()->isVoidTy())
  401. CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 1));
  402. break;
  403. case Intrinsic::invariant_start:
  404. case Intrinsic::lifetime_start:
  405. // Discard region information.
  406. CI->replaceAllUsesWith(UndefValue::get(CI->getType()));
  407. break;
  408. case Intrinsic::invariant_end:
  409. case Intrinsic::lifetime_end:
  410. // Discard region information.
  411. break;
  412. }
  413. assert(CI->use_empty() &&
  414. "Lowering should have eliminated any uses of the intrinsic call!");
  415. CI->eraseFromParent();
  416. }
  417. bool IntrinsicLowering::LowerToByteSwap(CallInst *CI) {
  418. // Verify this is a simple bswap.
  419. if (CI->arg_size() != 1 || CI->getType() != CI->getArgOperand(0)->getType() ||
  420. !CI->getType()->isIntegerTy())
  421. return false;
  422. IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
  423. if (!Ty)
  424. return false;
  425. // Okay, we can do this xform, do so now.
  426. Module *M = CI->getModule();
  427. Function *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Ty);
  428. Value *Op = CI->getArgOperand(0);
  429. Op = CallInst::Create(Int, Op, CI->getName(), CI);
  430. CI->replaceAllUsesWith(Op);
  431. CI->eraseFromParent();
  432. return true;
  433. }