blob: aebc8fa510aa399b68367c44028524675c0431bc [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
James Y Knightfadf2502019-01-31 21:51:58 +000026template <class ArgIt>
27static void EnsureFunctionExists(Module &M, const char *Name,
28 ArgIt ArgBegin, ArgIt ArgEnd,
29 Type *RetTy) {
30 // Insert a correctly-typed definition now.
31 std::vector<Type *> ParamTys;
32 for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
33 ParamTys.push_back(I->getType());
34 M.getOrInsertFunction(Name, FunctionType::get(RetTy, ParamTys, false));
35}
36
37static void EnsureFPIntrinsicsExist(Module &M, Function &Fn,
38 const char *FName,
39 const char *DName, const char *LDName) {
40 // Insert definitions for all the floating point types.
41 switch((int)Fn.arg_begin()->getType()->getTypeID()) {
42 case Type::FloatTyID:
43 EnsureFunctionExists(M, FName, Fn.arg_begin(), Fn.arg_end(),
44 Type::getFloatTy(M.getContext()));
45 break;
46 case Type::DoubleTyID:
47 EnsureFunctionExists(M, DName, Fn.arg_begin(), Fn.arg_end(),
48 Type::getDoubleTy(M.getContext()));
49 break;
50 case Type::X86_FP80TyID:
51 case Type::FP128TyID:
52 case Type::PPC_FP128TyID:
53 EnsureFunctionExists(M, LDName, Fn.arg_begin(), Fn.arg_end(),
54 Fn.arg_begin()->getType());
55 break;
56 }
57}
58
Sanjay Patel76657f82017-12-15 18:34:45 +000059/// This function is used when we want to lower an intrinsic call to a call of
60/// an external function. This handles hard cases such as when there was already
61/// a prototype for the external function, but that prototype doesn't match the
62/// arguments we expect to pass in.
Chris Lattnerbcdadf32004-06-20 07:49:54 +000063template <class ArgIt>
64static CallInst *ReplaceCallWith(const char *NewFn, CallInst *CI,
Chris Lattner34acba42007-01-07 08:12:01 +000065 ArgIt ArgBegin, ArgIt ArgEnd,
Chris Lattner229907c2011-07-18 04:54:35 +000066 Type *RetTy) {
Owen Anderson4c472e12009-06-26 20:33:47 +000067 // If we haven't already looked up this function, check to see if the
68 // program already contains a function with this name.
Sanjay Patelaf674fb2015-12-14 17:24:23 +000069 Module *M = CI->getModule();
Owen Anderson4c472e12009-06-26 20:33:47 +000070 // Get or insert the definition now.
Jay Foadb804a2b2011-07-12 14:06:48 +000071 std::vector<Type *> ParamTys;
Owen Anderson4c472e12009-06-26 20:33:47 +000072 for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
73 ParamTys.push_back((*I)->getType());
James Y Knightfadf2502019-01-31 21:51:58 +000074 Constant* FCache = M->getOrInsertFunction(NewFn,
75 FunctionType::get(RetTy, ParamTys, false));
Chris Lattnerbcdadf32004-06-20 07:49:54 +000076
Duncan P. N. Exon Smith5ae59392015-10-09 19:13:58 +000077 IRBuilder<> Builder(CI->getParent(), CI->getIterator());
David Greene17a5dfe2007-08-01 03:43:44 +000078 SmallVector<Value *, 8> Args(ArgBegin, ArgEnd);
Jay Foad5bd375a2011-07-15 08:37:34 +000079 CallInst *NewCI = Builder.CreateCall(FCache, Args);
Jay Foad9a968a82009-05-12 20:27:44 +000080 NewCI->setName(CI->getName());
Chris Lattner34acba42007-01-07 08:12:01 +000081 if (!CI->use_empty())
82 CI->replaceAllUsesWith(NewCI);
Chris Lattnerbcdadf32004-06-20 07:49:54 +000083 return NewCI;
84}
85
Douglas Gregor6739a892010-05-11 06:17:44 +000086// VisualStudio defines setjmp as _setjmp
Michael J. Spencerded5f662010-09-24 19:48:47 +000087#if defined(_MSC_VER) && defined(setjmp) && \
88 !defined(setjmp_undefined_for_msvc)
89# pragma push_macro("setjmp")
90# undef setjmp
91# define setjmp_undefined_for_msvc
Douglas Gregor6739a892010-05-11 06:17:44 +000092#endif
93
James Y Knightfadf2502019-01-31 21:51:58 +000094void IntrinsicLowering::AddPrototypes(Module &M) {
95 LLVMContext &Context = M.getContext();
96 for (auto &F : M)
97 if (F.isDeclaration() && !F.use_empty())
98 switch (F.getIntrinsicID()) {
99 default: break;
100 case Intrinsic::setjmp:
101 EnsureFunctionExists(M, "setjmp", F.arg_begin(), F.arg_end(),
102 Type::getInt32Ty(M.getContext()));
103 break;
104 case Intrinsic::longjmp:
105 EnsureFunctionExists(M, "longjmp", F.arg_begin(), F.arg_end(),
106 Type::getVoidTy(M.getContext()));
107 break;
108 case Intrinsic::siglongjmp:
109 EnsureFunctionExists(M, "abort", F.arg_end(), F.arg_end(),
110 Type::getVoidTy(M.getContext()));
111 break;
112 case Intrinsic::memcpy:
113 M.getOrInsertFunction("memcpy",
114 Type::getInt8PtrTy(Context),
115 Type::getInt8PtrTy(Context),
116 Type::getInt8PtrTy(Context),
117 DL.getIntPtrType(Context));
118 break;
119 case Intrinsic::memmove:
120 M.getOrInsertFunction("memmove",
121 Type::getInt8PtrTy(Context),
122 Type::getInt8PtrTy(Context),
123 Type::getInt8PtrTy(Context),
124 DL.getIntPtrType(Context));
125 break;
126 case Intrinsic::memset:
127 M.getOrInsertFunction("memset",
128 Type::getInt8PtrTy(Context),
129 Type::getInt8PtrTy(Context),
130 Type::getInt32Ty(M.getContext()),
131 DL.getIntPtrType(Context));
132 break;
133 case Intrinsic::sqrt:
134 EnsureFPIntrinsicsExist(M, F, "sqrtf", "sqrt", "sqrtl");
135 break;
136 case Intrinsic::sin:
137 EnsureFPIntrinsicsExist(M, F, "sinf", "sin", "sinl");
138 break;
139 case Intrinsic::cos:
140 EnsureFPIntrinsicsExist(M, F, "cosf", "cos", "cosl");
141 break;
142 case Intrinsic::pow:
143 EnsureFPIntrinsicsExist(M, F, "powf", "pow", "powl");
144 break;
145 case Intrinsic::log:
146 EnsureFPIntrinsicsExist(M, F, "logf", "log", "logl");
147 break;
148 case Intrinsic::log2:
149 EnsureFPIntrinsicsExist(M, F, "log2f", "log2", "log2l");
150 break;
151 case Intrinsic::log10:
152 EnsureFPIntrinsicsExist(M, F, "log10f", "log10", "log10l");
153 break;
154 case Intrinsic::exp:
155 EnsureFPIntrinsicsExist(M, F, "expf", "exp", "expl");
156 break;
157 case Intrinsic::exp2:
158 EnsureFPIntrinsicsExist(M, F, "exp2f", "exp2", "exp2l");
159 break;
160 }
161}
162
Sanjay Patel76657f82017-12-15 18:34:45 +0000163/// Emit the code to lower bswap of V before the specified instruction IP.
Owen Andersonb6b25302009-07-14 23:09:55 +0000164static Value *LowerBSWAP(LLVMContext &Context, Value *V, Instruction *IP) {
Serge Guelton24386862017-11-30 11:06:22 +0000165 assert(V->getType()->isIntOrIntVectorTy() && "Can't bswap a non-integer type!");
Nate Begeman7d831fa2006-01-16 07:57:00 +0000166
Serge Guelton24386862017-11-30 11:06:22 +0000167 unsigned BitSize = V->getType()->getScalarSizeInBits();
Duncan P. N. Exon Smith5ae59392015-10-09 19:13:58 +0000168
169 IRBuilder<> Builder(IP);
Jay Foad9a968a82009-05-12 20:27:44 +0000170
Nate Begeman7d831fa2006-01-16 07:57:00 +0000171 switch(BitSize) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000172 default: llvm_unreachable("Unhandled type size of value to byteswap!");
Nate Begeman7d831fa2006-01-16 07:57:00 +0000173 case 16: {
Owen Andersonedb4a702009-07-24 23:12:02 +0000174 Value *Tmp1 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8),
Jay Foad9a968a82009-05-12 20:27:44 +0000175 "bswap.2");
Owen Andersonedb4a702009-07-24 23:12:02 +0000176 Value *Tmp2 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8),
Jay Foad9a968a82009-05-12 20:27:44 +0000177 "bswap.1");
178 V = Builder.CreateOr(Tmp1, Tmp2, "bswap.i16");
Nate Begeman7d831fa2006-01-16 07:57:00 +0000179 break;
180 }
181 case 32: {
Owen Andersonedb4a702009-07-24 23:12:02 +0000182 Value *Tmp4 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 24),
Jay Foad9a968a82009-05-12 20:27:44 +0000183 "bswap.4");
Owen Andersonedb4a702009-07-24 23:12:02 +0000184 Value *Tmp3 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8),
Jay Foad9a968a82009-05-12 20:27:44 +0000185 "bswap.3");
Owen Andersonedb4a702009-07-24 23:12:02 +0000186 Value *Tmp2 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8),
Jay Foad9a968a82009-05-12 20:27:44 +0000187 "bswap.2");
Owen Andersonedb4a702009-07-24 23:12:02 +0000188 Value *Tmp1 = Builder.CreateLShr(V,ConstantInt::get(V->getType(), 24),
Jay Foad9a968a82009-05-12 20:27:44 +0000189 "bswap.1");
Owen Andersonb6b25302009-07-14 23:09:55 +0000190 Tmp3 = Builder.CreateAnd(Tmp3,
Serge Guelton24386862017-11-30 11:06:22 +0000191 ConstantInt::get(V->getType(), 0xFF0000),
Jay Foad9a968a82009-05-12 20:27:44 +0000192 "bswap.and3");
Owen Andersonb6b25302009-07-14 23:09:55 +0000193 Tmp2 = Builder.CreateAnd(Tmp2,
Serge Guelton24386862017-11-30 11:06:22 +0000194 ConstantInt::get(V->getType(), 0xFF00),
Jay Foad9a968a82009-05-12 20:27:44 +0000195 "bswap.and2");
196 Tmp4 = Builder.CreateOr(Tmp4, Tmp3, "bswap.or1");
197 Tmp2 = Builder.CreateOr(Tmp2, Tmp1, "bswap.or2");
198 V = Builder.CreateOr(Tmp4, Tmp2, "bswap.i32");
Nate Begeman7d831fa2006-01-16 07:57:00 +0000199 break;
200 }
201 case 64: {
Owen Andersonedb4a702009-07-24 23:12:02 +0000202 Value *Tmp8 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 56),
Jay Foad9a968a82009-05-12 20:27:44 +0000203 "bswap.8");
Owen Andersonedb4a702009-07-24 23:12:02 +0000204 Value *Tmp7 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 40),
Jay Foad9a968a82009-05-12 20:27:44 +0000205 "bswap.7");
Owen Andersonedb4a702009-07-24 23:12:02 +0000206 Value *Tmp6 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 24),
Jay Foad9a968a82009-05-12 20:27:44 +0000207 "bswap.6");
Owen Andersonedb4a702009-07-24 23:12:02 +0000208 Value *Tmp5 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8),
Jay Foad9a968a82009-05-12 20:27:44 +0000209 "bswap.5");
Owen Andersonedb4a702009-07-24 23:12:02 +0000210 Value* Tmp4 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8),
Jay Foad9a968a82009-05-12 20:27:44 +0000211 "bswap.4");
Fangrui Songf78650a2018-07-30 19:41:25 +0000212 Value* Tmp3 = Builder.CreateLShr(V,
Owen Andersonedb4a702009-07-24 23:12:02 +0000213 ConstantInt::get(V->getType(), 24),
Jay Foad9a968a82009-05-12 20:27:44 +0000214 "bswap.3");
Fangrui Songf78650a2018-07-30 19:41:25 +0000215 Value* Tmp2 = Builder.CreateLShr(V,
Owen Andersonedb4a702009-07-24 23:12:02 +0000216 ConstantInt::get(V->getType(), 40),
Jay Foad9a968a82009-05-12 20:27:44 +0000217 "bswap.2");
Fangrui Songf78650a2018-07-30 19:41:25 +0000218 Value* Tmp1 = Builder.CreateLShr(V,
Owen Andersonedb4a702009-07-24 23:12:02 +0000219 ConstantInt::get(V->getType(), 56),
Jay Foad9a968a82009-05-12 20:27:44 +0000220 "bswap.1");
221 Tmp7 = Builder.CreateAnd(Tmp7,
Serge Guelton24386862017-11-30 11:06:22 +0000222 ConstantInt::get(V->getType(),
Jay Foad9a968a82009-05-12 20:27:44 +0000223 0xFF000000000000ULL),
224 "bswap.and7");
225 Tmp6 = Builder.CreateAnd(Tmp6,
Serge Guelton24386862017-11-30 11:06:22 +0000226 ConstantInt::get(V->getType(),
Jay Foad9a968a82009-05-12 20:27:44 +0000227 0xFF0000000000ULL),
228 "bswap.and6");
229 Tmp5 = Builder.CreateAnd(Tmp5,
Serge Guelton24386862017-11-30 11:06:22 +0000230 ConstantInt::get(V->getType(),
Owen Anderson55f1c092009-08-13 21:58:54 +0000231 0xFF00000000ULL),
Jay Foad9a968a82009-05-12 20:27:44 +0000232 "bswap.and5");
233 Tmp4 = Builder.CreateAnd(Tmp4,
Serge Guelton24386862017-11-30 11:06:22 +0000234 ConstantInt::get(V->getType(),
Owen Anderson55f1c092009-08-13 21:58:54 +0000235 0xFF000000ULL),
Jay Foad9a968a82009-05-12 20:27:44 +0000236 "bswap.and4");
237 Tmp3 = Builder.CreateAnd(Tmp3,
Serge Guelton24386862017-11-30 11:06:22 +0000238 ConstantInt::get(V->getType(),
Owen Anderson55f1c092009-08-13 21:58:54 +0000239 0xFF0000ULL),
Jay Foad9a968a82009-05-12 20:27:44 +0000240 "bswap.and3");
241 Tmp2 = Builder.CreateAnd(Tmp2,
Serge Guelton24386862017-11-30 11:06:22 +0000242 ConstantInt::get(V->getType(),
Owen Anderson55f1c092009-08-13 21:58:54 +0000243 0xFF00ULL),
Jay Foad9a968a82009-05-12 20:27:44 +0000244 "bswap.and2");
245 Tmp8 = Builder.CreateOr(Tmp8, Tmp7, "bswap.or1");
246 Tmp6 = Builder.CreateOr(Tmp6, Tmp5, "bswap.or2");
247 Tmp4 = Builder.CreateOr(Tmp4, Tmp3, "bswap.or3");
248 Tmp2 = Builder.CreateOr(Tmp2, Tmp1, "bswap.or4");
249 Tmp8 = Builder.CreateOr(Tmp8, Tmp6, "bswap.or5");
250 Tmp4 = Builder.CreateOr(Tmp4, Tmp2, "bswap.or6");
251 V = Builder.CreateOr(Tmp8, Tmp4, "bswap.i64");
Nate Begeman7d831fa2006-01-16 07:57:00 +0000252 break;
253 }
254 }
Nate Begeman7d831fa2006-01-16 07:57:00 +0000255 return V;
256}
257
Sanjay Patel76657f82017-12-15 18:34:45 +0000258/// Emit the code to lower ctpop of V before the specified instruction IP.
Owen Andersonb6b25302009-07-14 23:09:55 +0000259static Value *LowerCTPOP(LLVMContext &Context, Value *V, Instruction *IP) {
Duncan Sands9dff9be2010-02-15 16:12:20 +0000260 assert(V->getType()->isIntegerTy() && "Can't ctpop a non-integer type!");
Chris Lattner9ec975a2005-05-11 19:42:05 +0000261
262 static const uint64_t MaskValues[6] = {
263 0x5555555555555555ULL, 0x3333333333333333ULL,
264 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
265 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
266 };
267
Duncan P. N. Exon Smith5ae59392015-10-09 19:13:58 +0000268 IRBuilder<> Builder(IP);
Jay Foad9a968a82009-05-12 20:27:44 +0000269
Chris Lattner991ce362005-05-11 20:24:12 +0000270 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
Zhou Sheng0a0ae932007-06-02 04:10:33 +0000271 unsigned WordSize = (BitSize + 63) / 64;
Owen Andersonedb4a702009-07-24 23:12:02 +0000272 Value *Count = ConstantInt::get(V->getType(), 0);
Reid Spencerfdff9382006-11-08 06:47:33 +0000273
Zhou Sheng0a0ae932007-06-02 04:10:33 +0000274 for (unsigned n = 0; n < WordSize; ++n) {
275 Value *PartValue = V;
Fangrui Songf78650a2018-07-30 19:41:25 +0000276 for (unsigned i = 1, ct = 0; i < (BitSize>64 ? 64 : BitSize);
Zhou Sheng0a0ae932007-06-02 04:10:33 +0000277 i <<= 1, ++ct) {
Owen Andersonedb4a702009-07-24 23:12:02 +0000278 Value *MaskCst = ConstantInt::get(V->getType(), MaskValues[ct]);
Jay Foad9a968a82009-05-12 20:27:44 +0000279 Value *LHS = Builder.CreateAnd(PartValue, MaskCst, "cppop.and1");
280 Value *VShift = Builder.CreateLShr(PartValue,
Owen Andersonedb4a702009-07-24 23:12:02 +0000281 ConstantInt::get(V->getType(), i),
Jay Foad9a968a82009-05-12 20:27:44 +0000282 "ctpop.sh");
283 Value *RHS = Builder.CreateAnd(VShift, MaskCst, "cppop.and2");
284 PartValue = Builder.CreateAdd(LHS, RHS, "ctpop.step");
Zhou Sheng0a0ae932007-06-02 04:10:33 +0000285 }
Jay Foad9a968a82009-05-12 20:27:44 +0000286 Count = Builder.CreateAdd(PartValue, Count, "ctpop.part");
Zhou Sheng0a0ae932007-06-02 04:10:33 +0000287 if (BitSize > 64) {
Owen Andersonedb4a702009-07-24 23:12:02 +0000288 V = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 64),
Jay Foad9a968a82009-05-12 20:27:44 +0000289 "ctpop.part.sh");
Zhou Sheng0a0ae932007-06-02 04:10:33 +0000290 BitSize -= 64;
291 }
Chris Lattner9ec975a2005-05-11 19:42:05 +0000292 }
293
Chris Lattner079ebcf2007-08-06 16:36:18 +0000294 return Count;
Chris Lattner9ec975a2005-05-11 19:42:05 +0000295}
296
Sanjay Patel76657f82017-12-15 18:34:45 +0000297/// Emit the code to lower ctlz of V before the specified instruction IP.
Owen Andersonb6b25302009-07-14 23:09:55 +0000298static Value *LowerCTLZ(LLVMContext &Context, Value *V, Instruction *IP) {
Chris Lattner991ce362005-05-11 20:24:12 +0000299
Duncan P. N. Exon Smith5ae59392015-10-09 19:13:58 +0000300 IRBuilder<> Builder(IP);
Jay Foad9a968a82009-05-12 20:27:44 +0000301
Chris Lattner991ce362005-05-11 20:24:12 +0000302 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
Zhou Sheng0a0ae932007-06-02 04:10:33 +0000303 for (unsigned i = 1; i < BitSize; i <<= 1) {
Owen Andersonedb4a702009-07-24 23:12:02 +0000304 Value *ShVal = ConstantInt::get(V->getType(), i);
Jay Foad9a968a82009-05-12 20:27:44 +0000305 ShVal = Builder.CreateLShr(V, ShVal, "ctlz.sh");
306 V = Builder.CreateOr(V, ShVal, "ctlz.step");
Chris Lattner991ce362005-05-11 20:24:12 +0000307 }
308
Jay Foad9a968a82009-05-12 20:27:44 +0000309 V = Builder.CreateNot(V);
Owen Andersonb6b25302009-07-14 23:09:55 +0000310 return LowerCTPOP(Context, V, IP);
Chris Lattner991ce362005-05-11 20:24:12 +0000311}
312
Owen Anderson4c472e12009-06-26 20:33:47 +0000313static void ReplaceFPIntrinsicWithCall(CallInst *CI, const char *Fname,
314 const char *Dname,
Dale Johannesenf1acc4d2008-09-22 20:51:30 +0000315 const char *LDname) {
Gabor Greifed9ae7b2010-06-25 09:03:52 +0000316 CallSite CS(CI);
317 switch (CI->getArgOperand(0)->getType()->getTypeID()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000318 default: llvm_unreachable("Invalid type in intrinsic");
Dale Johannesenf1acc4d2008-09-22 20:51:30 +0000319 case Type::FloatTyID:
Gabor Greifed9ae7b2010-06-25 09:03:52 +0000320 ReplaceCallWith(Fname, CI, CS.arg_begin(), CS.arg_end(),
Owen Anderson55f1c092009-08-13 21:58:54 +0000321 Type::getFloatTy(CI->getContext()));
Dale Johannesenf1acc4d2008-09-22 20:51:30 +0000322 break;
323 case Type::DoubleTyID:
Gabor Greifed9ae7b2010-06-25 09:03:52 +0000324 ReplaceCallWith(Dname, CI, CS.arg_begin(), CS.arg_end(),
Owen Anderson55f1c092009-08-13 21:58:54 +0000325 Type::getDoubleTy(CI->getContext()));
Dale Johannesenf1acc4d2008-09-22 20:51:30 +0000326 break;
327 case Type::X86_FP80TyID:
328 case Type::FP128TyID:
329 case Type::PPC_FP128TyID:
Gabor Greifed9ae7b2010-06-25 09:03:52 +0000330 ReplaceCallWith(LDname, CI, CS.arg_begin(), CS.arg_end(),
331 CI->getArgOperand(0)->getType());
Dale Johannesenf1acc4d2008-09-22 20:51:30 +0000332 break;
333 }
334}
Reid Spencercce90f52007-04-04 23:48:25 +0000335
Chris Lattner2775aba2006-11-15 18:00:10 +0000336void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
Duncan P. N. Exon Smith5ae59392015-10-09 19:13:58 +0000337 IRBuilder<> Builder(CI);
Owen Anderson47db9412009-07-22 00:24:57 +0000338 LLVMContext &Context = CI->getContext();
Jay Foad9a968a82009-05-12 20:27:44 +0000339
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000340 const Function *Callee = CI->getCalledFunction();
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000341 assert(Callee && "Cannot lower an indirect call!");
Misha Brukman835702a2005-04-21 22:36:52 +0000342
Gabor Greifed9ae7b2010-06-25 09:03:52 +0000343 CallSite CS(CI);
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000344 switch (Callee->getIntrinsicID()) {
345 case Intrinsic::not_intrinsic:
Chris Lattner2104b8d2010-04-07 22:58:41 +0000346 report_fatal_error("Cannot lower a call to a non-intrinsic function '"+
Torok Edwinccb29cd2009-07-11 13:10:19 +0000347 Callee->getName() + "'!");
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000348 default:
Chris Lattner2104b8d2010-04-07 22:58:41 +0000349 report_fatal_error("Code generator does not support intrinsic function '"+
Torok Edwinccb29cd2009-07-11 13:10:19 +0000350 Callee->getName()+"'!");
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000351
Jakub Staszak3f158fd2011-07-06 18:22:43 +0000352 case Intrinsic::expect: {
353 // Just replace __builtin_expect(exp, c) with EXP.
354 Value *V = CI->getArgOperand(0);
355 CI->replaceAllUsesWith(V);
356 break;
357 }
358
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000359 // The setjmp/longjmp intrinsics should only exist in the code if it was
360 // never optimized (ie, right out of the CFE), or if it has been hacked on
361 // by the lowerinvoke pass. In both cases, the right thing to do is to
362 // convert the call to an explicit setjmp or longjmp call.
363 case Intrinsic::setjmp: {
Gabor Greifed9ae7b2010-06-25 09:03:52 +0000364 Value *V = ReplaceCallWith("setjmp", CI, CS.arg_begin(), CS.arg_end(),
Owen Anderson55f1c092009-08-13 21:58:54 +0000365 Type::getInt32Ty(Context));
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000366 if (!CI->getType()->isVoidTy())
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000367 CI->replaceAllUsesWith(V);
368 break;
369 }
Misha Brukman835702a2005-04-21 22:36:52 +0000370 case Intrinsic::sigsetjmp:
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000371 if (!CI->getType()->isVoidTy())
Owen Anderson5a1acd92009-07-31 20:28:14 +0000372 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000373 break;
374
375 case Intrinsic::longjmp: {
Gabor Greifed9ae7b2010-06-25 09:03:52 +0000376 ReplaceCallWith("longjmp", CI, CS.arg_begin(), CS.arg_end(),
Owen Anderson55f1c092009-08-13 21:58:54 +0000377 Type::getVoidTy(Context));
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000378 break;
379 }
380
381 case Intrinsic::siglongjmp: {
382 // Insert the call to abort
Fangrui Songf78650a2018-07-30 19:41:25 +0000383 ReplaceCallWith("abort", CI, CS.arg_end(), CS.arg_end(),
Owen Anderson55f1c092009-08-13 21:58:54 +0000384 Type::getVoidTy(Context));
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000385 break;
386 }
Reid Spencer6bba6c82007-04-01 07:35:23 +0000387 case Intrinsic::ctpop:
Gabor Greifed9ae7b2010-06-25 09:03:52 +0000388 CI->replaceAllUsesWith(LowerCTPOP(Context, CI->getArgOperand(0), CI));
Reid Spencerb4f9a6f2006-01-16 21:12:35 +0000389 break;
390
Reid Spencer6bba6c82007-04-01 07:35:23 +0000391 case Intrinsic::bswap:
Gabor Greifed9ae7b2010-06-25 09:03:52 +0000392 CI->replaceAllUsesWith(LowerBSWAP(Context, CI->getArgOperand(0), CI));
Nate Begeman7d831fa2006-01-16 07:57:00 +0000393 break;
Fangrui Songf78650a2018-07-30 19:41:25 +0000394
Reid Spencer6bba6c82007-04-01 07:35:23 +0000395 case Intrinsic::ctlz:
Gabor Greifed9ae7b2010-06-25 09:03:52 +0000396 CI->replaceAllUsesWith(LowerCTLZ(Context, CI->getArgOperand(0), CI));
Andrew Lenharth5e177822005-05-03 17:19:30 +0000397 break;
Nate Begeman7d831fa2006-01-16 07:57:00 +0000398
Reid Spencer6bba6c82007-04-01 07:35:23 +0000399 case Intrinsic::cttz: {
Chris Lattnerfe5759b2005-05-11 20:02:14 +0000400 // cttz(x) -> ctpop(~X & (X-1))
Gabor Greifed9ae7b2010-06-25 09:03:52 +0000401 Value *Src = CI->getArgOperand(0);
Jay Foad9a968a82009-05-12 20:27:44 +0000402 Value *NotSrc = Builder.CreateNot(Src);
403 NotSrc->setName(Src->getName() + ".not");
Owen Andersonedb4a702009-07-24 23:12:02 +0000404 Value *SrcM1 = ConstantInt::get(Src->getType(), 1);
Jay Foad9a968a82009-05-12 20:27:44 +0000405 SrcM1 = Builder.CreateSub(Src, SrcM1);
Owen Anderson47db9412009-07-22 00:24:57 +0000406 Src = LowerCTPOP(Context, Builder.CreateAnd(NotSrc, SrcM1), CI);
Andrew Lenharth5e177822005-05-03 17:19:30 +0000407 CI->replaceAllUsesWith(Src);
408 break;
409 }
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000410
Chris Lattner3b2b0af2006-01-13 02:22:08 +0000411 case Intrinsic::stacksave:
Bob Wilsonbfb44ef2013-02-08 21:48:29 +0000412 case Intrinsic::stackrestore: {
Chris Lattner3b2b0af2006-01-13 02:22:08 +0000413 if (!Warned)
Bob Wilsonbfb44ef2013-02-08 21:48:29 +0000414 errs() << "WARNING: this target does not support the llvm.stack"
415 << (Callee->getIntrinsicID() == Intrinsic::stacksave ?
416 "save" : "restore") << " intrinsic.\n";
Chris Lattner3b2b0af2006-01-13 02:22:08 +0000417 Warned = true;
Bob Wilsonbfb44ef2013-02-08 21:48:29 +0000418 if (Callee->getIntrinsicID() == Intrinsic::stacksave)
419 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
Chris Lattner3b2b0af2006-01-13 02:22:08 +0000420 break;
Bob Wilsonbfb44ef2013-02-08 21:48:29 +0000421 }
Fangrui Songf78650a2018-07-30 19:41:25 +0000422
Yury Gribovd7dbb662015-12-01 11:40:55 +0000423 case Intrinsic::get_dynamic_area_offset:
424 errs() << "WARNING: this target does not support the custom llvm.get."
425 "dynamic.area.offset. It is being lowered to a constant 0\n";
426 // Just lower it to a constant 0 because for most targets
427 // @llvm.get.dynamic.area.offset is lowered to zero.
428 CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 0));
429 break;
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000430 case Intrinsic::returnaddress:
431 case Intrinsic::frameaddress:
Bob Wilsonbfb44ef2013-02-08 21:48:29 +0000432 errs() << "WARNING: this target does not support the llvm."
433 << (Callee->getIntrinsicID() == Intrinsic::returnaddress ?
434 "return" : "frame") << "address intrinsic.\n";
Albert Gutowski795d7d62016-10-12 22:13:19 +0000435 CI->replaceAllUsesWith(
436 ConstantPointerNull::get(cast<PointerType>(CI->getType())));
437 break;
438 case Intrinsic::addressofreturnaddress:
439 errs() << "WARNING: this target does not support the "
440 "llvm.addressofreturnaddress intrinsic.\n";
441 CI->replaceAllUsesWith(
442 ConstantPointerNull::get(cast<PointerType>(CI->getType())));
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000443 break;
444
Chris Lattnerc87e03a2005-02-28 19:27:23 +0000445 case Intrinsic::prefetch:
446 break; // Simply strip out prefetches on unsupported architectures
447
Andrew Lenharthb4427912005-03-28 20:05:49 +0000448 case Intrinsic::pcmarker:
449 break; // Simply strip out pcmarker on unsupported architectures
Bob Wilsonbfb44ef2013-02-08 21:48:29 +0000450 case Intrinsic::readcyclecounter: {
451 errs() << "WARNING: this target does not support the llvm.readcyclecoun"
452 << "ter intrinsic. It is being lowered to a constant 0\n";
Owen Anderson55f1c092009-08-13 21:58:54 +0000453 CI->replaceAllUsesWith(ConstantInt::get(Type::getInt64Ty(Context), 0));
Andrew Lenharth01aa5632005-11-11 16:47:30 +0000454 break;
Bob Wilsonbfb44ef2013-02-08 21:48:29 +0000455 }
Andrew Lenharthb4427912005-03-28 20:05:49 +0000456
Jim Laskeya8bdac82006-03-23 18:06:46 +0000457 case Intrinsic::dbg_declare:
Shiva Chen2c864552018-05-09 02:40:45 +0000458 case Intrinsic::dbg_label:
Duncan Sands9d974202007-07-06 14:46:23 +0000459 break; // Simply strip out debugging intrinsics
460
Duncan Sands8e6ccb62009-10-14 16:11:37 +0000461 case Intrinsic::eh_typeid_for:
Duncan Sands9d974202007-07-06 14:46:23 +0000462 // Return something different to eh_selector.
Owen Andersonedb4a702009-07-24 23:12:02 +0000463 CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 1));
Duncan Sands9d974202007-07-06 14:46:23 +0000464 break;
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000465
Justin Holewinskifff1f5f2013-05-21 14:37:16 +0000466 case Intrinsic::annotation:
467 case Intrinsic::ptr_annotation:
468 // Just drop the annotation, but forward the value
469 CI->replaceAllUsesWith(CI->getOperand(0));
470 break;
471
Hal Finkel93046912014-07-25 21:13:35 +0000472 case Intrinsic::assume:
Tanya Lattnere199f972007-06-15 22:26:58 +0000473 case Intrinsic::var_annotation:
Hal Finkel93046912014-07-25 21:13:35 +0000474 break; // Strip out these intrinsics
Fangrui Songf78650a2018-07-30 19:41:25 +0000475
Chris Lattnerdd708342008-11-21 16:42:48 +0000476 case Intrinsic::memcpy: {
Rafael Espindola7c68beb2014-02-18 15:33:12 +0000477 Type *IntPtr = DL.getIntPtrType(Context);
Gabor Greifed9ae7b2010-06-25 09:03:52 +0000478 Value *Size = Builder.CreateIntCast(CI->getArgOperand(2), IntPtr,
Jay Foad9a968a82009-05-12 20:27:44 +0000479 /* isSigned */ false);
Chris Lattnerdc0a9a72007-02-06 19:06:38 +0000480 Value *Ops[3];
Gabor Greifed9ae7b2010-06-25 09:03:52 +0000481 Ops[0] = CI->getArgOperand(0);
482 Ops[1] = CI->getArgOperand(1);
Chris Lattnerdc0a9a72007-02-06 19:06:38 +0000483 Ops[2] = Size;
Gabor Greifed9ae7b2010-06-25 09:03:52 +0000484 ReplaceCallWith("memcpy", CI, Ops, Ops+3, CI->getArgOperand(0)->getType());
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000485 break;
486 }
Chris Lattnerdd708342008-11-21 16:42:48 +0000487 case Intrinsic::memmove: {
Rafael Espindola7c68beb2014-02-18 15:33:12 +0000488 Type *IntPtr = DL.getIntPtrType(Context);
Gabor Greifed9ae7b2010-06-25 09:03:52 +0000489 Value *Size = Builder.CreateIntCast(CI->getArgOperand(2), IntPtr,
Jay Foad9a968a82009-05-12 20:27:44 +0000490 /* isSigned */ false);
Chris Lattnerdc0a9a72007-02-06 19:06:38 +0000491 Value *Ops[3];
Gabor Greifed9ae7b2010-06-25 09:03:52 +0000492 Ops[0] = CI->getArgOperand(0);
493 Ops[1] = CI->getArgOperand(1);
Chris Lattnerdc0a9a72007-02-06 19:06:38 +0000494 Ops[2] = Size;
Gabor Greifed9ae7b2010-06-25 09:03:52 +0000495 ReplaceCallWith("memmove", CI, Ops, Ops+3, CI->getArgOperand(0)->getType());
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000496 break;
497 }
Chris Lattnerdd708342008-11-21 16:42:48 +0000498 case Intrinsic::memset: {
Matt Arsenaultc9003032013-11-10 04:46:57 +0000499 Value *Op0 = CI->getArgOperand(0);
Rafael Espindola7c68beb2014-02-18 15:33:12 +0000500 Type *IntPtr = DL.getIntPtrType(Op0->getType());
Gabor Greifed9ae7b2010-06-25 09:03:52 +0000501 Value *Size = Builder.CreateIntCast(CI->getArgOperand(2), IntPtr,
Jay Foad9a968a82009-05-12 20:27:44 +0000502 /* isSigned */ false);
Chris Lattnerdc0a9a72007-02-06 19:06:38 +0000503 Value *Ops[3];
Matt Arsenaultc9003032013-11-10 04:46:57 +0000504 Ops[0] = Op0;
Chris Lattnerdc0a9a72007-02-06 19:06:38 +0000505 // Extend the amount to i32.
Gabor Greif3e44ea12010-07-22 10:37:47 +0000506 Ops[1] = Builder.CreateIntCast(CI->getArgOperand(1),
507 Type::getInt32Ty(Context),
Jay Foad9a968a82009-05-12 20:27:44 +0000508 /* isSigned */ false);
Chris Lattnerdc0a9a72007-02-06 19:06:38 +0000509 Ops[2] = Size;
Gabor Greifed9ae7b2010-06-25 09:03:52 +0000510 ReplaceCallWith("memset", CI, Ops, Ops+3, CI->getArgOperand(0)->getType());
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000511 break;
512 }
Dale Johannesen4d4e77a2007-10-02 17:43:59 +0000513 case Intrinsic::sqrt: {
Owen Anderson4c472e12009-06-26 20:33:47 +0000514 ReplaceFPIntrinsicWithCall(CI, "sqrtf", "sqrt", "sqrtl");
Dale Johannesen6bf69ed2007-09-28 18:06:58 +0000515 break;
516 }
Dale Johannesenda2d8062008-09-04 00:47:13 +0000517 case Intrinsic::log: {
Owen Anderson4c472e12009-06-26 20:33:47 +0000518 ReplaceFPIntrinsicWithCall(CI, "logf", "log", "logl");
Dale Johannesenda2d8062008-09-04 00:47:13 +0000519 break;
520 }
521 case Intrinsic::log2: {
Owen Anderson4c472e12009-06-26 20:33:47 +0000522 ReplaceFPIntrinsicWithCall(CI, "log2f", "log2", "log2l");
Dale Johannesenda2d8062008-09-04 00:47:13 +0000523 break;
524 }
525 case Intrinsic::log10: {
Owen Anderson4c472e12009-06-26 20:33:47 +0000526 ReplaceFPIntrinsicWithCall(CI, "log10f", "log10", "log10l");
Dale Johannesenda2d8062008-09-04 00:47:13 +0000527 break;
528 }
529 case Intrinsic::exp: {
Owen Anderson4c472e12009-06-26 20:33:47 +0000530 ReplaceFPIntrinsicWithCall(CI, "expf", "exp", "expl");
Dale Johannesenda2d8062008-09-04 00:47:13 +0000531 break;
532 }
533 case Intrinsic::exp2: {
Owen Anderson4c472e12009-06-26 20:33:47 +0000534 ReplaceFPIntrinsicWithCall(CI, "exp2f", "exp2", "exp2l");
Dale Johannesenda2d8062008-09-04 00:47:13 +0000535 break;
536 }
537 case Intrinsic::pow: {
Owen Anderson4c472e12009-06-26 20:33:47 +0000538 ReplaceFPIntrinsicWithCall(CI, "powf", "pow", "powl");
Dale Johannesenda2d8062008-09-04 00:47:13 +0000539 break;
540 }
Josh Klontzac0d28d2014-08-08 15:00:12 +0000541 case Intrinsic::sin: {
542 ReplaceFPIntrinsicWithCall(CI, "sinf", "sin", "sinl");
543 break;
544 }
545 case Intrinsic::cos: {
546 ReplaceFPIntrinsicWithCall(CI, "cosf", "cos", "cosl");
547 break;
548 }
Josh Klontze1900dc2014-08-30 18:33:35 +0000549 case Intrinsic::floor: {
550 ReplaceFPIntrinsicWithCall(CI, "floorf", "floor", "floorl");
551 break;
552 }
Josh Klontzac0d28d2014-08-08 15:00:12 +0000553 case Intrinsic::ceil: {
554 ReplaceFPIntrinsicWithCall(CI, "ceilf", "ceil", "ceill");
555 break;
556 }
Josh Klontze1900dc2014-08-30 18:33:35 +0000557 case Intrinsic::trunc: {
558 ReplaceFPIntrinsicWithCall(CI, "truncf", "trunc", "truncl");
559 break;
560 }
561 case Intrinsic::round: {
562 ReplaceFPIntrinsicWithCall(CI, "roundf", "round", "roundl");
563 break;
564 }
565 case Intrinsic::copysign: {
566 ReplaceFPIntrinsicWithCall(CI, "copysignf", "copysign", "copysignl");
567 break;
568 }
Anton Korobeynikov66b91e66e2007-11-15 23:25:33 +0000569 case Intrinsic::flt_rounds:
570 // Lower to "round to the nearest"
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000571 if (!CI->getType()->isVoidTy())
Owen Andersonedb4a702009-07-24 23:12:02 +0000572 CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 1));
Anton Korobeynikov66b91e66e2007-11-15 23:25:33 +0000573 break;
Duncan Sandsdca0c282009-11-10 09:08:09 +0000574 case Intrinsic::invariant_start:
575 case Intrinsic::lifetime_start:
576 // Discard region information.
577 CI->replaceAllUsesWith(UndefValue::get(CI->getType()));
578 break;
579 case Intrinsic::invariant_end:
580 case Intrinsic::lifetime_end:
581 // Discard region information.
582 break;
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000583 }
Misha Brukman835702a2005-04-21 22:36:52 +0000584
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000585 assert(CI->use_empty() &&
586 "Lowering should have eliminated any uses of the intrinsic call!");
Chris Lattner9ec975a2005-05-11 19:42:05 +0000587 CI->eraseFromParent();
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000588}
Evan Cheng078b0b02011-01-08 01:24:27 +0000589
590bool IntrinsicLowering::LowerToByteSwap(CallInst *CI) {
591 // Verify this is a simple bswap.
592 if (CI->getNumArgOperands() != 1 ||
593 CI->getType() != CI->getArgOperand(0)->getType() ||
594 !CI->getType()->isIntegerTy())
595 return false;
596
Jay Foadb804a2b2011-07-12 14:06:48 +0000597 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng078b0b02011-01-08 01:24:27 +0000598 if (!Ty)
599 return false;
600
601 // Okay, we can do this xform, do so now.
Sanjay Patelaf674fb2015-12-14 17:24:23 +0000602 Module *M = CI->getModule();
Benjamin Kramere6e19332011-07-14 17:45:39 +0000603 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Ty);
Evan Cheng078b0b02011-01-08 01:24:27 +0000604
605 Value *Op = CI->getArgOperand(0);
606 Op = CallInst::Create(Int, Op, CI->getName(), CI);
607
608 CI->replaceAllUsesWith(Op);
609 CI->eraseFromParent();
610 return true;
611}