blob: 8cbd8bcaeabbb6e3cdd27b50fd613dbce3cb4867 [file] [log] [blame]
Chris Lattnerbcdadf32004-06-20 07:49:54 +00001//===-- IntrinsicLowering.cpp - Intrinsic Lowering default implementation -===//
Misha Brukman835702a2005-04-21 22:36:52 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Misha Brukman835702a2005-04-21 22:36:52 +00006//
Chris Lattnerbcdadf32004-06-20 07:49:54 +00007//===----------------------------------------------------------------------===//
8//
Chris Lattner2775aba2006-11-15 18:00:10 +00009// This file implements the IntrinsicLowering class.
Chris Lattnerbcdadf32004-06-20 07:49:54 +000010//
11//===----------------------------------------------------------------------===//
12
Chandler Carruthaafe0912012-06-29 12:38:19 +000013#include "llvm/CodeGen/IntrinsicLowering.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000014#include "llvm/ADT/SmallVector.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000015#include "llvm/IR/CallSite.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000016#include "llvm/IR/Constants.h"
17#include "llvm/IR/DataLayout.h"
18#include "llvm/IR/DerivedTypes.h"
19#include "llvm/IR/IRBuilder.h"
20#include "llvm/IR/Module.h"
21#include "llvm/IR/Type.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000022#include "llvm/Support/ErrorHandling.h"
Chris Lattner13626022009-08-23 06:03:38 +000023#include "llvm/Support/raw_ostream.h"
Chris Lattnerbcdadf32004-06-20 07:49:54 +000024using namespace llvm;
25
Sanjay Patel76657f82017-12-15 18:34:45 +000026/// This function is used when we want to lower an intrinsic call to a call of
27/// an external function. This handles hard cases such as when there was already
28/// a prototype for the external function, but that prototype doesn't match the
29/// arguments we expect to pass in.
Chris Lattnerbcdadf32004-06-20 07:49:54 +000030template <class ArgIt>
31static CallInst *ReplaceCallWith(const char *NewFn, CallInst *CI,
Chris Lattner34acba42007-01-07 08:12:01 +000032 ArgIt ArgBegin, ArgIt ArgEnd,
Chris Lattner229907c2011-07-18 04:54:35 +000033 Type *RetTy) {
Owen Anderson4c472e12009-06-26 20:33:47 +000034 // If we haven't already looked up this function, check to see if the
35 // program already contains a function with this name.
Sanjay Patelaf674fb2015-12-14 17:24:23 +000036 Module *M = CI->getModule();
Owen Anderson4c472e12009-06-26 20:33:47 +000037 // Get or insert the definition now.
Jay Foadb804a2b2011-07-12 14:06:48 +000038 std::vector<Type *> ParamTys;
Owen Anderson4c472e12009-06-26 20:33:47 +000039 for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
40 ParamTys.push_back((*I)->getType());
James Y Knight13680222019-02-01 02:28:03 +000041 FunctionCallee FCache =
42 M->getOrInsertFunction(NewFn, FunctionType::get(RetTy, ParamTys, false));
Chris Lattnerbcdadf32004-06-20 07:49:54 +000043
Duncan P. N. Exon Smith5ae59392015-10-09 19:13:58 +000044 IRBuilder<> Builder(CI->getParent(), CI->getIterator());
David Greene17a5dfe2007-08-01 03:43:44 +000045 SmallVector<Value *, 8> Args(ArgBegin, ArgEnd);
Jay Foad5bd375a2011-07-15 08:37:34 +000046 CallInst *NewCI = Builder.CreateCall(FCache, Args);
Jay Foad9a968a82009-05-12 20:27:44 +000047 NewCI->setName(CI->getName());
Chris Lattner34acba42007-01-07 08:12:01 +000048 if (!CI->use_empty())
49 CI->replaceAllUsesWith(NewCI);
Chris Lattnerbcdadf32004-06-20 07:49:54 +000050 return NewCI;
51}
52
Douglas Gregor6739a892010-05-11 06:17:44 +000053// VisualStudio defines setjmp as _setjmp
Michael J. Spencerded5f662010-09-24 19:48:47 +000054#if defined(_MSC_VER) && defined(setjmp) && \
55 !defined(setjmp_undefined_for_msvc)
56# pragma push_macro("setjmp")
57# undef setjmp
58# define setjmp_undefined_for_msvc
Douglas Gregor6739a892010-05-11 06:17:44 +000059#endif
60
Sanjay Patel76657f82017-12-15 18:34:45 +000061/// Emit the code to lower bswap of V before the specified instruction IP.
Owen Andersonb6b25302009-07-14 23:09:55 +000062static Value *LowerBSWAP(LLVMContext &Context, Value *V, Instruction *IP) {
Serge Guelton24386862017-11-30 11:06:22 +000063 assert(V->getType()->isIntOrIntVectorTy() && "Can't bswap a non-integer type!");
Nate Begeman7d831fa2006-01-16 07:57:00 +000064
Serge Guelton24386862017-11-30 11:06:22 +000065 unsigned BitSize = V->getType()->getScalarSizeInBits();
Duncan P. N. Exon Smith5ae59392015-10-09 19:13:58 +000066
67 IRBuilder<> Builder(IP);
Jay Foad9a968a82009-05-12 20:27:44 +000068
Nate Begeman7d831fa2006-01-16 07:57:00 +000069 switch(BitSize) {
Torok Edwinfbcc6632009-07-14 16:55:14 +000070 default: llvm_unreachable("Unhandled type size of value to byteswap!");
Nate Begeman7d831fa2006-01-16 07:57:00 +000071 case 16: {
Owen Andersonedb4a702009-07-24 23:12:02 +000072 Value *Tmp1 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8),
Jay Foad9a968a82009-05-12 20:27:44 +000073 "bswap.2");
Owen Andersonedb4a702009-07-24 23:12:02 +000074 Value *Tmp2 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8),
Jay Foad9a968a82009-05-12 20:27:44 +000075 "bswap.1");
76 V = Builder.CreateOr(Tmp1, Tmp2, "bswap.i16");
Nate Begeman7d831fa2006-01-16 07:57:00 +000077 break;
78 }
79 case 32: {
Owen Andersonedb4a702009-07-24 23:12:02 +000080 Value *Tmp4 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 24),
Jay Foad9a968a82009-05-12 20:27:44 +000081 "bswap.4");
Owen Andersonedb4a702009-07-24 23:12:02 +000082 Value *Tmp3 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8),
Jay Foad9a968a82009-05-12 20:27:44 +000083 "bswap.3");
Owen Andersonedb4a702009-07-24 23:12:02 +000084 Value *Tmp2 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8),
Jay Foad9a968a82009-05-12 20:27:44 +000085 "bswap.2");
Owen Andersonedb4a702009-07-24 23:12:02 +000086 Value *Tmp1 = Builder.CreateLShr(V,ConstantInt::get(V->getType(), 24),
Jay Foad9a968a82009-05-12 20:27:44 +000087 "bswap.1");
Owen Andersonb6b25302009-07-14 23:09:55 +000088 Tmp3 = Builder.CreateAnd(Tmp3,
Serge Guelton24386862017-11-30 11:06:22 +000089 ConstantInt::get(V->getType(), 0xFF0000),
Jay Foad9a968a82009-05-12 20:27:44 +000090 "bswap.and3");
Owen Andersonb6b25302009-07-14 23:09:55 +000091 Tmp2 = Builder.CreateAnd(Tmp2,
Serge Guelton24386862017-11-30 11:06:22 +000092 ConstantInt::get(V->getType(), 0xFF00),
Jay Foad9a968a82009-05-12 20:27:44 +000093 "bswap.and2");
94 Tmp4 = Builder.CreateOr(Tmp4, Tmp3, "bswap.or1");
95 Tmp2 = Builder.CreateOr(Tmp2, Tmp1, "bswap.or2");
96 V = Builder.CreateOr(Tmp4, Tmp2, "bswap.i32");
Nate Begeman7d831fa2006-01-16 07:57:00 +000097 break;
98 }
99 case 64: {
Owen Andersonedb4a702009-07-24 23:12:02 +0000100 Value *Tmp8 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 56),
Jay Foad9a968a82009-05-12 20:27:44 +0000101 "bswap.8");
Owen Andersonedb4a702009-07-24 23:12:02 +0000102 Value *Tmp7 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 40),
Jay Foad9a968a82009-05-12 20:27:44 +0000103 "bswap.7");
Owen Andersonedb4a702009-07-24 23:12:02 +0000104 Value *Tmp6 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 24),
Jay Foad9a968a82009-05-12 20:27:44 +0000105 "bswap.6");
Owen Andersonedb4a702009-07-24 23:12:02 +0000106 Value *Tmp5 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8),
Jay Foad9a968a82009-05-12 20:27:44 +0000107 "bswap.5");
Owen Andersonedb4a702009-07-24 23:12:02 +0000108 Value* Tmp4 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8),
Jay Foad9a968a82009-05-12 20:27:44 +0000109 "bswap.4");
Fangrui Songf78650a2018-07-30 19:41:25 +0000110 Value* Tmp3 = Builder.CreateLShr(V,
Owen Andersonedb4a702009-07-24 23:12:02 +0000111 ConstantInt::get(V->getType(), 24),
Jay Foad9a968a82009-05-12 20:27:44 +0000112 "bswap.3");
Fangrui Songf78650a2018-07-30 19:41:25 +0000113 Value* Tmp2 = Builder.CreateLShr(V,
Owen Andersonedb4a702009-07-24 23:12:02 +0000114 ConstantInt::get(V->getType(), 40),
Jay Foad9a968a82009-05-12 20:27:44 +0000115 "bswap.2");
Fangrui Songf78650a2018-07-30 19:41:25 +0000116 Value* Tmp1 = Builder.CreateLShr(V,
Owen Andersonedb4a702009-07-24 23:12:02 +0000117 ConstantInt::get(V->getType(), 56),
Jay Foad9a968a82009-05-12 20:27:44 +0000118 "bswap.1");
119 Tmp7 = Builder.CreateAnd(Tmp7,
Serge Guelton24386862017-11-30 11:06:22 +0000120 ConstantInt::get(V->getType(),
Jay Foad9a968a82009-05-12 20:27:44 +0000121 0xFF000000000000ULL),
122 "bswap.and7");
123 Tmp6 = Builder.CreateAnd(Tmp6,
Serge Guelton24386862017-11-30 11:06:22 +0000124 ConstantInt::get(V->getType(),
Jay Foad9a968a82009-05-12 20:27:44 +0000125 0xFF0000000000ULL),
126 "bswap.and6");
127 Tmp5 = Builder.CreateAnd(Tmp5,
Serge Guelton24386862017-11-30 11:06:22 +0000128 ConstantInt::get(V->getType(),
Owen Anderson55f1c092009-08-13 21:58:54 +0000129 0xFF00000000ULL),
Jay Foad9a968a82009-05-12 20:27:44 +0000130 "bswap.and5");
131 Tmp4 = Builder.CreateAnd(Tmp4,
Serge Guelton24386862017-11-30 11:06:22 +0000132 ConstantInt::get(V->getType(),
Owen Anderson55f1c092009-08-13 21:58:54 +0000133 0xFF000000ULL),
Jay Foad9a968a82009-05-12 20:27:44 +0000134 "bswap.and4");
135 Tmp3 = Builder.CreateAnd(Tmp3,
Serge Guelton24386862017-11-30 11:06:22 +0000136 ConstantInt::get(V->getType(),
Owen Anderson55f1c092009-08-13 21:58:54 +0000137 0xFF0000ULL),
Jay Foad9a968a82009-05-12 20:27:44 +0000138 "bswap.and3");
139 Tmp2 = Builder.CreateAnd(Tmp2,
Serge Guelton24386862017-11-30 11:06:22 +0000140 ConstantInt::get(V->getType(),
Owen Anderson55f1c092009-08-13 21:58:54 +0000141 0xFF00ULL),
Jay Foad9a968a82009-05-12 20:27:44 +0000142 "bswap.and2");
143 Tmp8 = Builder.CreateOr(Tmp8, Tmp7, "bswap.or1");
144 Tmp6 = Builder.CreateOr(Tmp6, Tmp5, "bswap.or2");
145 Tmp4 = Builder.CreateOr(Tmp4, Tmp3, "bswap.or3");
146 Tmp2 = Builder.CreateOr(Tmp2, Tmp1, "bswap.or4");
147 Tmp8 = Builder.CreateOr(Tmp8, Tmp6, "bswap.or5");
148 Tmp4 = Builder.CreateOr(Tmp4, Tmp2, "bswap.or6");
149 V = Builder.CreateOr(Tmp8, Tmp4, "bswap.i64");
Nate Begeman7d831fa2006-01-16 07:57:00 +0000150 break;
151 }
152 }
Nate Begeman7d831fa2006-01-16 07:57:00 +0000153 return V;
154}
155
Sanjay Patel76657f82017-12-15 18:34:45 +0000156/// Emit the code to lower ctpop of V before the specified instruction IP.
Owen Andersonb6b25302009-07-14 23:09:55 +0000157static Value *LowerCTPOP(LLVMContext &Context, Value *V, Instruction *IP) {
Duncan Sands9dff9be2010-02-15 16:12:20 +0000158 assert(V->getType()->isIntegerTy() && "Can't ctpop a non-integer type!");
Chris Lattner9ec975a2005-05-11 19:42:05 +0000159
160 static const uint64_t MaskValues[6] = {
161 0x5555555555555555ULL, 0x3333333333333333ULL,
162 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
163 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
164 };
165
Duncan P. N. Exon Smith5ae59392015-10-09 19:13:58 +0000166 IRBuilder<> Builder(IP);
Jay Foad9a968a82009-05-12 20:27:44 +0000167
Chris Lattner991ce362005-05-11 20:24:12 +0000168 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
Zhou Sheng0a0ae932007-06-02 04:10:33 +0000169 unsigned WordSize = (BitSize + 63) / 64;
Owen Andersonedb4a702009-07-24 23:12:02 +0000170 Value *Count = ConstantInt::get(V->getType(), 0);
Reid Spencerfdff9382006-11-08 06:47:33 +0000171
Zhou Sheng0a0ae932007-06-02 04:10:33 +0000172 for (unsigned n = 0; n < WordSize; ++n) {
173 Value *PartValue = V;
Fangrui Songf78650a2018-07-30 19:41:25 +0000174 for (unsigned i = 1, ct = 0; i < (BitSize>64 ? 64 : BitSize);
Zhou Sheng0a0ae932007-06-02 04:10:33 +0000175 i <<= 1, ++ct) {
Owen Andersonedb4a702009-07-24 23:12:02 +0000176 Value *MaskCst = ConstantInt::get(V->getType(), MaskValues[ct]);
Jay Foad9a968a82009-05-12 20:27:44 +0000177 Value *LHS = Builder.CreateAnd(PartValue, MaskCst, "cppop.and1");
178 Value *VShift = Builder.CreateLShr(PartValue,
Owen Andersonedb4a702009-07-24 23:12:02 +0000179 ConstantInt::get(V->getType(), i),
Jay Foad9a968a82009-05-12 20:27:44 +0000180 "ctpop.sh");
181 Value *RHS = Builder.CreateAnd(VShift, MaskCst, "cppop.and2");
182 PartValue = Builder.CreateAdd(LHS, RHS, "ctpop.step");
Zhou Sheng0a0ae932007-06-02 04:10:33 +0000183 }
Jay Foad9a968a82009-05-12 20:27:44 +0000184 Count = Builder.CreateAdd(PartValue, Count, "ctpop.part");
Zhou Sheng0a0ae932007-06-02 04:10:33 +0000185 if (BitSize > 64) {
Owen Andersonedb4a702009-07-24 23:12:02 +0000186 V = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 64),
Jay Foad9a968a82009-05-12 20:27:44 +0000187 "ctpop.part.sh");
Zhou Sheng0a0ae932007-06-02 04:10:33 +0000188 BitSize -= 64;
189 }
Chris Lattner9ec975a2005-05-11 19:42:05 +0000190 }
191
Chris Lattner079ebcf2007-08-06 16:36:18 +0000192 return Count;
Chris Lattner9ec975a2005-05-11 19:42:05 +0000193}
194
Sanjay Patel76657f82017-12-15 18:34:45 +0000195/// Emit the code to lower ctlz of V before the specified instruction IP.
Owen Andersonb6b25302009-07-14 23:09:55 +0000196static Value *LowerCTLZ(LLVMContext &Context, Value *V, Instruction *IP) {
Chris Lattner991ce362005-05-11 20:24:12 +0000197
Duncan P. N. Exon Smith5ae59392015-10-09 19:13:58 +0000198 IRBuilder<> Builder(IP);
Jay Foad9a968a82009-05-12 20:27:44 +0000199
Chris Lattner991ce362005-05-11 20:24:12 +0000200 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
Zhou Sheng0a0ae932007-06-02 04:10:33 +0000201 for (unsigned i = 1; i < BitSize; i <<= 1) {
Owen Andersonedb4a702009-07-24 23:12:02 +0000202 Value *ShVal = ConstantInt::get(V->getType(), i);
Jay Foad9a968a82009-05-12 20:27:44 +0000203 ShVal = Builder.CreateLShr(V, ShVal, "ctlz.sh");
204 V = Builder.CreateOr(V, ShVal, "ctlz.step");
Chris Lattner991ce362005-05-11 20:24:12 +0000205 }
206
Jay Foad9a968a82009-05-12 20:27:44 +0000207 V = Builder.CreateNot(V);
Owen Andersonb6b25302009-07-14 23:09:55 +0000208 return LowerCTPOP(Context, V, IP);
Chris Lattner991ce362005-05-11 20:24:12 +0000209}
210
Owen Anderson4c472e12009-06-26 20:33:47 +0000211static void ReplaceFPIntrinsicWithCall(CallInst *CI, const char *Fname,
212 const char *Dname,
Dale Johannesenf1acc4d2008-09-22 20:51:30 +0000213 const char *LDname) {
Gabor Greifed9ae7b2010-06-25 09:03:52 +0000214 CallSite CS(CI);
215 switch (CI->getArgOperand(0)->getType()->getTypeID()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000216 default: llvm_unreachable("Invalid type in intrinsic");
Dale Johannesenf1acc4d2008-09-22 20:51:30 +0000217 case Type::FloatTyID:
Gabor Greifed9ae7b2010-06-25 09:03:52 +0000218 ReplaceCallWith(Fname, CI, CS.arg_begin(), CS.arg_end(),
Owen Anderson55f1c092009-08-13 21:58:54 +0000219 Type::getFloatTy(CI->getContext()));
Dale Johannesenf1acc4d2008-09-22 20:51:30 +0000220 break;
221 case Type::DoubleTyID:
Gabor Greifed9ae7b2010-06-25 09:03:52 +0000222 ReplaceCallWith(Dname, CI, CS.arg_begin(), CS.arg_end(),
Owen Anderson55f1c092009-08-13 21:58:54 +0000223 Type::getDoubleTy(CI->getContext()));
Dale Johannesenf1acc4d2008-09-22 20:51:30 +0000224 break;
225 case Type::X86_FP80TyID:
226 case Type::FP128TyID:
227 case Type::PPC_FP128TyID:
Gabor Greifed9ae7b2010-06-25 09:03:52 +0000228 ReplaceCallWith(LDname, CI, CS.arg_begin(), CS.arg_end(),
229 CI->getArgOperand(0)->getType());
Dale Johannesenf1acc4d2008-09-22 20:51:30 +0000230 break;
231 }
232}
Reid Spencercce90f52007-04-04 23:48:25 +0000233
Chris Lattner2775aba2006-11-15 18:00:10 +0000234void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
Duncan P. N. Exon Smith5ae59392015-10-09 19:13:58 +0000235 IRBuilder<> Builder(CI);
Owen Anderson47db9412009-07-22 00:24:57 +0000236 LLVMContext &Context = CI->getContext();
Jay Foad9a968a82009-05-12 20:27:44 +0000237
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000238 const Function *Callee = CI->getCalledFunction();
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000239 assert(Callee && "Cannot lower an indirect call!");
Misha Brukman835702a2005-04-21 22:36:52 +0000240
Gabor Greifed9ae7b2010-06-25 09:03:52 +0000241 CallSite CS(CI);
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000242 switch (Callee->getIntrinsicID()) {
243 case Intrinsic::not_intrinsic:
Chris Lattner2104b8d2010-04-07 22:58:41 +0000244 report_fatal_error("Cannot lower a call to a non-intrinsic function '"+
Torok Edwinccb29cd2009-07-11 13:10:19 +0000245 Callee->getName() + "'!");
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000246 default:
Chris Lattner2104b8d2010-04-07 22:58:41 +0000247 report_fatal_error("Code generator does not support intrinsic function '"+
Torok Edwinccb29cd2009-07-11 13:10:19 +0000248 Callee->getName()+"'!");
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000249
Jakub Staszak3f158fd2011-07-06 18:22:43 +0000250 case Intrinsic::expect: {
251 // Just replace __builtin_expect(exp, c) with EXP.
252 Value *V = CI->getArgOperand(0);
253 CI->replaceAllUsesWith(V);
254 break;
255 }
256
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000257 // The setjmp/longjmp intrinsics should only exist in the code if it was
258 // never optimized (ie, right out of the CFE), or if it has been hacked on
259 // by the lowerinvoke pass. In both cases, the right thing to do is to
260 // convert the call to an explicit setjmp or longjmp call.
261 case Intrinsic::setjmp: {
Gabor Greifed9ae7b2010-06-25 09:03:52 +0000262 Value *V = ReplaceCallWith("setjmp", CI, CS.arg_begin(), CS.arg_end(),
Owen Anderson55f1c092009-08-13 21:58:54 +0000263 Type::getInt32Ty(Context));
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000264 if (!CI->getType()->isVoidTy())
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000265 CI->replaceAllUsesWith(V);
266 break;
267 }
Misha Brukman835702a2005-04-21 22:36:52 +0000268 case Intrinsic::sigsetjmp:
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000269 if (!CI->getType()->isVoidTy())
Owen Anderson5a1acd92009-07-31 20:28:14 +0000270 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000271 break;
272
273 case Intrinsic::longjmp: {
Gabor Greifed9ae7b2010-06-25 09:03:52 +0000274 ReplaceCallWith("longjmp", CI, CS.arg_begin(), CS.arg_end(),
Owen Anderson55f1c092009-08-13 21:58:54 +0000275 Type::getVoidTy(Context));
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000276 break;
277 }
278
279 case Intrinsic::siglongjmp: {
280 // Insert the call to abort
Fangrui Songf78650a2018-07-30 19:41:25 +0000281 ReplaceCallWith("abort", CI, CS.arg_end(), CS.arg_end(),
Owen Anderson55f1c092009-08-13 21:58:54 +0000282 Type::getVoidTy(Context));
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000283 break;
284 }
Reid Spencer6bba6c82007-04-01 07:35:23 +0000285 case Intrinsic::ctpop:
Gabor Greifed9ae7b2010-06-25 09:03:52 +0000286 CI->replaceAllUsesWith(LowerCTPOP(Context, CI->getArgOperand(0), CI));
Reid Spencerb4f9a6f2006-01-16 21:12:35 +0000287 break;
288
Reid Spencer6bba6c82007-04-01 07:35:23 +0000289 case Intrinsic::bswap:
Gabor Greifed9ae7b2010-06-25 09:03:52 +0000290 CI->replaceAllUsesWith(LowerBSWAP(Context, CI->getArgOperand(0), CI));
Nate Begeman7d831fa2006-01-16 07:57:00 +0000291 break;
Fangrui Songf78650a2018-07-30 19:41:25 +0000292
Reid Spencer6bba6c82007-04-01 07:35:23 +0000293 case Intrinsic::ctlz:
Gabor Greifed9ae7b2010-06-25 09:03:52 +0000294 CI->replaceAllUsesWith(LowerCTLZ(Context, CI->getArgOperand(0), CI));
Andrew Lenharth5e177822005-05-03 17:19:30 +0000295 break;
Nate Begeman7d831fa2006-01-16 07:57:00 +0000296
Reid Spencer6bba6c82007-04-01 07:35:23 +0000297 case Intrinsic::cttz: {
Chris Lattnerfe5759b2005-05-11 20:02:14 +0000298 // cttz(x) -> ctpop(~X & (X-1))
Gabor Greifed9ae7b2010-06-25 09:03:52 +0000299 Value *Src = CI->getArgOperand(0);
Jay Foad9a968a82009-05-12 20:27:44 +0000300 Value *NotSrc = Builder.CreateNot(Src);
301 NotSrc->setName(Src->getName() + ".not");
Owen Andersonedb4a702009-07-24 23:12:02 +0000302 Value *SrcM1 = ConstantInt::get(Src->getType(), 1);
Jay Foad9a968a82009-05-12 20:27:44 +0000303 SrcM1 = Builder.CreateSub(Src, SrcM1);
Owen Anderson47db9412009-07-22 00:24:57 +0000304 Src = LowerCTPOP(Context, Builder.CreateAnd(NotSrc, SrcM1), CI);
Andrew Lenharth5e177822005-05-03 17:19:30 +0000305 CI->replaceAllUsesWith(Src);
306 break;
307 }
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000308
Chris Lattner3b2b0af2006-01-13 02:22:08 +0000309 case Intrinsic::stacksave:
Bob Wilsonbfb44ef2013-02-08 21:48:29 +0000310 case Intrinsic::stackrestore: {
Chris Lattner3b2b0af2006-01-13 02:22:08 +0000311 if (!Warned)
Bob Wilsonbfb44ef2013-02-08 21:48:29 +0000312 errs() << "WARNING: this target does not support the llvm.stack"
313 << (Callee->getIntrinsicID() == Intrinsic::stacksave ?
314 "save" : "restore") << " intrinsic.\n";
Chris Lattner3b2b0af2006-01-13 02:22:08 +0000315 Warned = true;
Bob Wilsonbfb44ef2013-02-08 21:48:29 +0000316 if (Callee->getIntrinsicID() == Intrinsic::stacksave)
317 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
Chris Lattner3b2b0af2006-01-13 02:22:08 +0000318 break;
Bob Wilsonbfb44ef2013-02-08 21:48:29 +0000319 }
Fangrui Songf78650a2018-07-30 19:41:25 +0000320
Yury Gribovd7dbb662015-12-01 11:40:55 +0000321 case Intrinsic::get_dynamic_area_offset:
322 errs() << "WARNING: this target does not support the custom llvm.get."
323 "dynamic.area.offset. It is being lowered to a constant 0\n";
324 // Just lower it to a constant 0 because for most targets
325 // @llvm.get.dynamic.area.offset is lowered to zero.
326 CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 0));
327 break;
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000328 case Intrinsic::returnaddress:
329 case Intrinsic::frameaddress:
Bob Wilsonbfb44ef2013-02-08 21:48:29 +0000330 errs() << "WARNING: this target does not support the llvm."
331 << (Callee->getIntrinsicID() == Intrinsic::returnaddress ?
332 "return" : "frame") << "address intrinsic.\n";
Albert Gutowski795d7d62016-10-12 22:13:19 +0000333 CI->replaceAllUsesWith(
334 ConstantPointerNull::get(cast<PointerType>(CI->getType())));
335 break;
336 case Intrinsic::addressofreturnaddress:
337 errs() << "WARNING: this target does not support the "
338 "llvm.addressofreturnaddress intrinsic.\n";
339 CI->replaceAllUsesWith(
340 ConstantPointerNull::get(cast<PointerType>(CI->getType())));
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000341 break;
342
Chris Lattnerc87e03a2005-02-28 19:27:23 +0000343 case Intrinsic::prefetch:
344 break; // Simply strip out prefetches on unsupported architectures
345
Andrew Lenharthb4427912005-03-28 20:05:49 +0000346 case Intrinsic::pcmarker:
347 break; // Simply strip out pcmarker on unsupported architectures
Bob Wilsonbfb44ef2013-02-08 21:48:29 +0000348 case Intrinsic::readcyclecounter: {
349 errs() << "WARNING: this target does not support the llvm.readcyclecoun"
350 << "ter intrinsic. It is being lowered to a constant 0\n";
Owen Anderson55f1c092009-08-13 21:58:54 +0000351 CI->replaceAllUsesWith(ConstantInt::get(Type::getInt64Ty(Context), 0));
Andrew Lenharth01aa5632005-11-11 16:47:30 +0000352 break;
Bob Wilsonbfb44ef2013-02-08 21:48:29 +0000353 }
Andrew Lenharthb4427912005-03-28 20:05:49 +0000354
Jim Laskeya8bdac82006-03-23 18:06:46 +0000355 case Intrinsic::dbg_declare:
Shiva Chen2c864552018-05-09 02:40:45 +0000356 case Intrinsic::dbg_label:
Duncan Sands9d974202007-07-06 14:46:23 +0000357 break; // Simply strip out debugging intrinsics
358
Duncan Sands8e6ccb62009-10-14 16:11:37 +0000359 case Intrinsic::eh_typeid_for:
Duncan Sands9d974202007-07-06 14:46:23 +0000360 // Return something different to eh_selector.
Owen Andersonedb4a702009-07-24 23:12:02 +0000361 CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 1));
Duncan Sands9d974202007-07-06 14:46:23 +0000362 break;
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000363
Justin Holewinskifff1f5f2013-05-21 14:37:16 +0000364 case Intrinsic::annotation:
365 case Intrinsic::ptr_annotation:
366 // Just drop the annotation, but forward the value
367 CI->replaceAllUsesWith(CI->getOperand(0));
368 break;
369
Hal Finkel93046912014-07-25 21:13:35 +0000370 case Intrinsic::assume:
Tanya Lattnere199f972007-06-15 22:26:58 +0000371 case Intrinsic::var_annotation:
Hal Finkel93046912014-07-25 21:13:35 +0000372 break; // Strip out these intrinsics
Fangrui Songf78650a2018-07-30 19:41:25 +0000373
Chris Lattnerdd708342008-11-21 16:42:48 +0000374 case Intrinsic::memcpy: {
Rafael Espindola7c68beb2014-02-18 15:33:12 +0000375 Type *IntPtr = DL.getIntPtrType(Context);
Gabor Greifed9ae7b2010-06-25 09:03:52 +0000376 Value *Size = Builder.CreateIntCast(CI->getArgOperand(2), IntPtr,
Jay Foad9a968a82009-05-12 20:27:44 +0000377 /* isSigned */ false);
Chris Lattnerdc0a9a72007-02-06 19:06:38 +0000378 Value *Ops[3];
Gabor Greifed9ae7b2010-06-25 09:03:52 +0000379 Ops[0] = CI->getArgOperand(0);
380 Ops[1] = CI->getArgOperand(1);
Chris Lattnerdc0a9a72007-02-06 19:06:38 +0000381 Ops[2] = Size;
Gabor Greifed9ae7b2010-06-25 09:03:52 +0000382 ReplaceCallWith("memcpy", CI, Ops, Ops+3, CI->getArgOperand(0)->getType());
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000383 break;
384 }
Chris Lattnerdd708342008-11-21 16:42:48 +0000385 case Intrinsic::memmove: {
Rafael Espindola7c68beb2014-02-18 15:33:12 +0000386 Type *IntPtr = DL.getIntPtrType(Context);
Gabor Greifed9ae7b2010-06-25 09:03:52 +0000387 Value *Size = Builder.CreateIntCast(CI->getArgOperand(2), IntPtr,
Jay Foad9a968a82009-05-12 20:27:44 +0000388 /* isSigned */ false);
Chris Lattnerdc0a9a72007-02-06 19:06:38 +0000389 Value *Ops[3];
Gabor Greifed9ae7b2010-06-25 09:03:52 +0000390 Ops[0] = CI->getArgOperand(0);
391 Ops[1] = CI->getArgOperand(1);
Chris Lattnerdc0a9a72007-02-06 19:06:38 +0000392 Ops[2] = Size;
Gabor Greifed9ae7b2010-06-25 09:03:52 +0000393 ReplaceCallWith("memmove", CI, Ops, Ops+3, CI->getArgOperand(0)->getType());
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000394 break;
395 }
Chris Lattnerdd708342008-11-21 16:42:48 +0000396 case Intrinsic::memset: {
Matt Arsenaultc9003032013-11-10 04:46:57 +0000397 Value *Op0 = CI->getArgOperand(0);
Rafael Espindola7c68beb2014-02-18 15:33:12 +0000398 Type *IntPtr = DL.getIntPtrType(Op0->getType());
Gabor Greifed9ae7b2010-06-25 09:03:52 +0000399 Value *Size = Builder.CreateIntCast(CI->getArgOperand(2), IntPtr,
Jay Foad9a968a82009-05-12 20:27:44 +0000400 /* isSigned */ false);
Chris Lattnerdc0a9a72007-02-06 19:06:38 +0000401 Value *Ops[3];
Matt Arsenaultc9003032013-11-10 04:46:57 +0000402 Ops[0] = Op0;
Chris Lattnerdc0a9a72007-02-06 19:06:38 +0000403 // Extend the amount to i32.
Gabor Greif3e44ea12010-07-22 10:37:47 +0000404 Ops[1] = Builder.CreateIntCast(CI->getArgOperand(1),
405 Type::getInt32Ty(Context),
Jay Foad9a968a82009-05-12 20:27:44 +0000406 /* isSigned */ false);
Chris Lattnerdc0a9a72007-02-06 19:06:38 +0000407 Ops[2] = Size;
Gabor Greifed9ae7b2010-06-25 09:03:52 +0000408 ReplaceCallWith("memset", CI, Ops, Ops+3, CI->getArgOperand(0)->getType());
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000409 break;
410 }
Dale Johannesen4d4e77a2007-10-02 17:43:59 +0000411 case Intrinsic::sqrt: {
Owen Anderson4c472e12009-06-26 20:33:47 +0000412 ReplaceFPIntrinsicWithCall(CI, "sqrtf", "sqrt", "sqrtl");
Dale Johannesen6bf69ed2007-09-28 18:06:58 +0000413 break;
414 }
Dale Johannesenda2d8062008-09-04 00:47:13 +0000415 case Intrinsic::log: {
Owen Anderson4c472e12009-06-26 20:33:47 +0000416 ReplaceFPIntrinsicWithCall(CI, "logf", "log", "logl");
Dale Johannesenda2d8062008-09-04 00:47:13 +0000417 break;
418 }
419 case Intrinsic::log2: {
Owen Anderson4c472e12009-06-26 20:33:47 +0000420 ReplaceFPIntrinsicWithCall(CI, "log2f", "log2", "log2l");
Dale Johannesenda2d8062008-09-04 00:47:13 +0000421 break;
422 }
423 case Intrinsic::log10: {
Owen Anderson4c472e12009-06-26 20:33:47 +0000424 ReplaceFPIntrinsicWithCall(CI, "log10f", "log10", "log10l");
Dale Johannesenda2d8062008-09-04 00:47:13 +0000425 break;
426 }
427 case Intrinsic::exp: {
Owen Anderson4c472e12009-06-26 20:33:47 +0000428 ReplaceFPIntrinsicWithCall(CI, "expf", "exp", "expl");
Dale Johannesenda2d8062008-09-04 00:47:13 +0000429 break;
430 }
431 case Intrinsic::exp2: {
Owen Anderson4c472e12009-06-26 20:33:47 +0000432 ReplaceFPIntrinsicWithCall(CI, "exp2f", "exp2", "exp2l");
Dale Johannesenda2d8062008-09-04 00:47:13 +0000433 break;
434 }
435 case Intrinsic::pow: {
Owen Anderson4c472e12009-06-26 20:33:47 +0000436 ReplaceFPIntrinsicWithCall(CI, "powf", "pow", "powl");
Dale Johannesenda2d8062008-09-04 00:47:13 +0000437 break;
438 }
Josh Klontzac0d28d2014-08-08 15:00:12 +0000439 case Intrinsic::sin: {
440 ReplaceFPIntrinsicWithCall(CI, "sinf", "sin", "sinl");
441 break;
442 }
443 case Intrinsic::cos: {
444 ReplaceFPIntrinsicWithCall(CI, "cosf", "cos", "cosl");
445 break;
446 }
Josh Klontze1900dc2014-08-30 18:33:35 +0000447 case Intrinsic::floor: {
448 ReplaceFPIntrinsicWithCall(CI, "floorf", "floor", "floorl");
449 break;
450 }
Josh Klontzac0d28d2014-08-08 15:00:12 +0000451 case Intrinsic::ceil: {
452 ReplaceFPIntrinsicWithCall(CI, "ceilf", "ceil", "ceill");
453 break;
454 }
Josh Klontze1900dc2014-08-30 18:33:35 +0000455 case Intrinsic::trunc: {
456 ReplaceFPIntrinsicWithCall(CI, "truncf", "trunc", "truncl");
457 break;
458 }
459 case Intrinsic::round: {
460 ReplaceFPIntrinsicWithCall(CI, "roundf", "round", "roundl");
461 break;
462 }
463 case Intrinsic::copysign: {
464 ReplaceFPIntrinsicWithCall(CI, "copysignf", "copysign", "copysignl");
465 break;
466 }
Anton Korobeynikov66b91e66e2007-11-15 23:25:33 +0000467 case Intrinsic::flt_rounds:
468 // Lower to "round to the nearest"
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000469 if (!CI->getType()->isVoidTy())
Owen Andersonedb4a702009-07-24 23:12:02 +0000470 CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 1));
Anton Korobeynikov66b91e66e2007-11-15 23:25:33 +0000471 break;
Duncan Sandsdca0c282009-11-10 09:08:09 +0000472 case Intrinsic::invariant_start:
473 case Intrinsic::lifetime_start:
474 // Discard region information.
475 CI->replaceAllUsesWith(UndefValue::get(CI->getType()));
476 break;
477 case Intrinsic::invariant_end:
478 case Intrinsic::lifetime_end:
479 // Discard region information.
480 break;
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000481 }
Misha Brukman835702a2005-04-21 22:36:52 +0000482
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000483 assert(CI->use_empty() &&
484 "Lowering should have eliminated any uses of the intrinsic call!");
Chris Lattner9ec975a2005-05-11 19:42:05 +0000485 CI->eraseFromParent();
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000486}
Evan Cheng078b0b02011-01-08 01:24:27 +0000487
488bool IntrinsicLowering::LowerToByteSwap(CallInst *CI) {
489 // Verify this is a simple bswap.
490 if (CI->getNumArgOperands() != 1 ||
491 CI->getType() != CI->getArgOperand(0)->getType() ||
492 !CI->getType()->isIntegerTy())
493 return false;
494
Jay Foadb804a2b2011-07-12 14:06:48 +0000495 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng078b0b02011-01-08 01:24:27 +0000496 if (!Ty)
497 return false;
498
499 // Okay, we can do this xform, do so now.
Sanjay Patelaf674fb2015-12-14 17:24:23 +0000500 Module *M = CI->getModule();
James Y Knight7976eb52019-02-01 20:43:25 +0000501 Function *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Ty);
Evan Cheng078b0b02011-01-08 01:24:27 +0000502
503 Value *Op = CI->getArgOperand(0);
504 Op = CallInst::Create(Int, Op, CI->getName(), CI);
505
506 CI->replaceAllUsesWith(Op);
507 CI->eraseFromParent();
508 return true;
509}