1 //===- BuildLibCalls.cpp - Utility builder for libcalls -------------------===//
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 implements some functions that will create standard C libcalls.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Transforms/Utils/BuildLibCalls.h"
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/Analysis/TargetLibraryInfo.h"
17 #include "llvm/IR/Constants.h"
18 #include "llvm/IR/DataLayout.h"
19 #include "llvm/IR/Function.h"
20 #include "llvm/IR/IRBuilder.h"
21 #include "llvm/IR/Intrinsics.h"
22 #include "llvm/IR/LLVMContext.h"
23 #include "llvm/IR/Module.h"
24 #include "llvm/IR/Type.h"
25
26 using namespace llvm;
27
28 /// CastToCStr - Return V if it is an i8*, otherwise cast it to i8*.
CastToCStr(Value * V,IRBuilder<> & B)29 Value *llvm::CastToCStr(Value *V, IRBuilder<> &B) {
30 unsigned AS = V->getType()->getPointerAddressSpace();
31 return B.CreateBitCast(V, B.getInt8PtrTy(AS), "cstr");
32 }
33
34 /// EmitStrLen - Emit a call to the strlen function to the builder, for the
35 /// specified pointer. This always returns an integer value of size intptr_t.
EmitStrLen(Value * Ptr,IRBuilder<> & B,const DataLayout & DL,const TargetLibraryInfo * TLI)36 Value *llvm::EmitStrLen(Value *Ptr, IRBuilder<> &B, const DataLayout &DL,
37 const TargetLibraryInfo *TLI) {
38 if (!TLI->has(LibFunc::strlen))
39 return nullptr;
40
41 Module *M = B.GetInsertBlock()->getParent()->getParent();
42 AttributeSet AS[2];
43 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
44 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
45 AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, AVs);
46
47 LLVMContext &Context = B.GetInsertBlock()->getContext();
48 Constant *StrLen = M->getOrInsertFunction(
49 "strlen", AttributeSet::get(M->getContext(), AS),
50 DL.getIntPtrType(Context), B.getInt8PtrTy(), nullptr);
51 CallInst *CI = B.CreateCall(StrLen, CastToCStr(Ptr, B), "strlen");
52 if (const Function *F = dyn_cast<Function>(StrLen->stripPointerCasts()))
53 CI->setCallingConv(F->getCallingConv());
54
55 return CI;
56 }
57
58 /// EmitStrChr - Emit a call to the strchr function to the builder, for the
59 /// specified pointer and character. Ptr is required to be some pointer type,
60 /// and the return value has 'i8*' type.
EmitStrChr(Value * Ptr,char C,IRBuilder<> & B,const TargetLibraryInfo * TLI)61 Value *llvm::EmitStrChr(Value *Ptr, char C, IRBuilder<> &B,
62 const TargetLibraryInfo *TLI) {
63 if (!TLI->has(LibFunc::strchr))
64 return nullptr;
65
66 Module *M = B.GetInsertBlock()->getParent()->getParent();
67 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
68 AttributeSet AS =
69 AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, AVs);
70
71 Type *I8Ptr = B.getInt8PtrTy();
72 Type *I32Ty = B.getInt32Ty();
73 Constant *StrChr = M->getOrInsertFunction("strchr",
74 AttributeSet::get(M->getContext(),
75 AS),
76 I8Ptr, I8Ptr, I32Ty, nullptr);
77 CallInst *CI = B.CreateCall(
78 StrChr, {CastToCStr(Ptr, B), ConstantInt::get(I32Ty, C)}, "strchr");
79 if (const Function *F = dyn_cast<Function>(StrChr->stripPointerCasts()))
80 CI->setCallingConv(F->getCallingConv());
81 return CI;
82 }
83
84 /// EmitStrNCmp - Emit a call to the strncmp function to the builder.
EmitStrNCmp(Value * Ptr1,Value * Ptr2,Value * Len,IRBuilder<> & B,const DataLayout & DL,const TargetLibraryInfo * TLI)85 Value *llvm::EmitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
86 const DataLayout &DL, const TargetLibraryInfo *TLI) {
87 if (!TLI->has(LibFunc::strncmp))
88 return nullptr;
89
90 Module *M = B.GetInsertBlock()->getParent()->getParent();
91 AttributeSet AS[3];
92 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
93 AS[1] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
94 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
95 AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, AVs);
96
97 LLVMContext &Context = B.GetInsertBlock()->getContext();
98 Value *StrNCmp = M->getOrInsertFunction(
99 "strncmp", AttributeSet::get(M->getContext(), AS), B.getInt32Ty(),
100 B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context), nullptr);
101 CallInst *CI = B.CreateCall(
102 StrNCmp, {CastToCStr(Ptr1, B), CastToCStr(Ptr2, B), Len}, "strncmp");
103
104 if (const Function *F = dyn_cast<Function>(StrNCmp->stripPointerCasts()))
105 CI->setCallingConv(F->getCallingConv());
106
107 return CI;
108 }
109
110 /// EmitStrCpy - Emit a call to the strcpy function to the builder, for the
111 /// specified pointer arguments.
EmitStrCpy(Value * Dst,Value * Src,IRBuilder<> & B,const TargetLibraryInfo * TLI,StringRef Name)112 Value *llvm::EmitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
113 const TargetLibraryInfo *TLI, StringRef Name) {
114 if (!TLI->has(LibFunc::strcpy))
115 return nullptr;
116
117 Module *M = B.GetInsertBlock()->getParent()->getParent();
118 AttributeSet AS[2];
119 AS[0] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
120 AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
121 Attribute::NoUnwind);
122 Type *I8Ptr = B.getInt8PtrTy();
123 Value *StrCpy = M->getOrInsertFunction(Name,
124 AttributeSet::get(M->getContext(), AS),
125 I8Ptr, I8Ptr, I8Ptr, nullptr);
126 CallInst *CI =
127 B.CreateCall(StrCpy, {CastToCStr(Dst, B), CastToCStr(Src, B)}, Name);
128 if (const Function *F = dyn_cast<Function>(StrCpy->stripPointerCasts()))
129 CI->setCallingConv(F->getCallingConv());
130 return CI;
131 }
132
133 /// EmitStrNCpy - Emit a call to the strncpy function to the builder, for the
134 /// specified pointer arguments.
EmitStrNCpy(Value * Dst,Value * Src,Value * Len,IRBuilder<> & B,const TargetLibraryInfo * TLI,StringRef Name)135 Value *llvm::EmitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B,
136 const TargetLibraryInfo *TLI, StringRef Name) {
137 if (!TLI->has(LibFunc::strncpy))
138 return nullptr;
139
140 Module *M = B.GetInsertBlock()->getParent()->getParent();
141 AttributeSet AS[2];
142 AS[0] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
143 AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
144 Attribute::NoUnwind);
145 Type *I8Ptr = B.getInt8PtrTy();
146 Value *StrNCpy = M->getOrInsertFunction(Name,
147 AttributeSet::get(M->getContext(),
148 AS),
149 I8Ptr, I8Ptr, I8Ptr,
150 Len->getType(), nullptr);
151 CallInst *CI = B.CreateCall(
152 StrNCpy, {CastToCStr(Dst, B), CastToCStr(Src, B), Len}, "strncpy");
153 if (const Function *F = dyn_cast<Function>(StrNCpy->stripPointerCasts()))
154 CI->setCallingConv(F->getCallingConv());
155 return CI;
156 }
157
158 /// EmitMemCpyChk - Emit a call to the __memcpy_chk function to the builder.
159 /// This expects that the Len and ObjSize have type 'intptr_t' and Dst/Src
160 /// are pointers.
EmitMemCpyChk(Value * Dst,Value * Src,Value * Len,Value * ObjSize,IRBuilder<> & B,const DataLayout & DL,const TargetLibraryInfo * TLI)161 Value *llvm::EmitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
162 IRBuilder<> &B, const DataLayout &DL,
163 const TargetLibraryInfo *TLI) {
164 if (!TLI->has(LibFunc::memcpy_chk))
165 return nullptr;
166
167 Module *M = B.GetInsertBlock()->getParent()->getParent();
168 AttributeSet AS;
169 AS = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
170 Attribute::NoUnwind);
171 LLVMContext &Context = B.GetInsertBlock()->getContext();
172 Value *MemCpy = M->getOrInsertFunction(
173 "__memcpy_chk", AttributeSet::get(M->getContext(), AS), B.getInt8PtrTy(),
174 B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context),
175 DL.getIntPtrType(Context), nullptr);
176 Dst = CastToCStr(Dst, B);
177 Src = CastToCStr(Src, B);
178 CallInst *CI = B.CreateCall(MemCpy, {Dst, Src, Len, ObjSize});
179 if (const Function *F = dyn_cast<Function>(MemCpy->stripPointerCasts()))
180 CI->setCallingConv(F->getCallingConv());
181 return CI;
182 }
183
184 /// EmitMemChr - Emit a call to the memchr function. This assumes that Ptr is
185 /// a pointer, Val is an i32 value, and Len is an 'intptr_t' value.
EmitMemChr(Value * Ptr,Value * Val,Value * Len,IRBuilder<> & B,const DataLayout & DL,const TargetLibraryInfo * TLI)186 Value *llvm::EmitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder<> &B,
187 const DataLayout &DL, const TargetLibraryInfo *TLI) {
188 if (!TLI->has(LibFunc::memchr))
189 return nullptr;
190
191 Module *M = B.GetInsertBlock()->getParent()->getParent();
192 AttributeSet AS;
193 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
194 AS = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, AVs);
195 LLVMContext &Context = B.GetInsertBlock()->getContext();
196 Value *MemChr = M->getOrInsertFunction(
197 "memchr", AttributeSet::get(M->getContext(), AS), B.getInt8PtrTy(),
198 B.getInt8PtrTy(), B.getInt32Ty(), DL.getIntPtrType(Context), nullptr);
199 CallInst *CI = B.CreateCall(MemChr, {CastToCStr(Ptr, B), Val, Len}, "memchr");
200
201 if (const Function *F = dyn_cast<Function>(MemChr->stripPointerCasts()))
202 CI->setCallingConv(F->getCallingConv());
203
204 return CI;
205 }
206
207 /// EmitMemCmp - Emit a call to the memcmp function.
EmitMemCmp(Value * Ptr1,Value * Ptr2,Value * Len,IRBuilder<> & B,const DataLayout & DL,const TargetLibraryInfo * TLI)208 Value *llvm::EmitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
209 const DataLayout &DL, const TargetLibraryInfo *TLI) {
210 if (!TLI->has(LibFunc::memcmp))
211 return nullptr;
212
213 Module *M = B.GetInsertBlock()->getParent()->getParent();
214 AttributeSet AS[3];
215 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
216 AS[1] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
217 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
218 AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, AVs);
219
220 LLVMContext &Context = B.GetInsertBlock()->getContext();
221 Value *MemCmp = M->getOrInsertFunction(
222 "memcmp", AttributeSet::get(M->getContext(), AS), B.getInt32Ty(),
223 B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context), nullptr);
224 CallInst *CI = B.CreateCall(
225 MemCmp, {CastToCStr(Ptr1, B), CastToCStr(Ptr2, B), Len}, "memcmp");
226
227 if (const Function *F = dyn_cast<Function>(MemCmp->stripPointerCasts()))
228 CI->setCallingConv(F->getCallingConv());
229
230 return CI;
231 }
232
233 /// Append a suffix to the function name according to the type of 'Op'.
AppendTypeSuffix(Value * Op,StringRef & Name,SmallString<20> & NameBuffer)234 static void AppendTypeSuffix(Value *Op, StringRef &Name, SmallString<20> &NameBuffer) {
235 if (!Op->getType()->isDoubleTy()) {
236 NameBuffer += Name;
237
238 if (Op->getType()->isFloatTy())
239 NameBuffer += 'f';
240 else
241 NameBuffer += 'l';
242
243 Name = NameBuffer;
244 }
245 return;
246 }
247
248 /// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name' (e.g.
249 /// 'floor'). This function is known to take a single of type matching 'Op' and
250 /// returns one value with the same type. If 'Op' is a long double, 'l' is
251 /// added as the suffix of name, if 'Op' is a float, we add a 'f' suffix.
EmitUnaryFloatFnCall(Value * Op,StringRef Name,IRBuilder<> & B,const AttributeSet & Attrs)252 Value *llvm::EmitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B,
253 const AttributeSet &Attrs) {
254 SmallString<20> NameBuffer;
255 AppendTypeSuffix(Op, Name, NameBuffer);
256
257 Module *M = B.GetInsertBlock()->getParent()->getParent();
258 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
259 Op->getType(), nullptr);
260 CallInst *CI = B.CreateCall(Callee, Op, Name);
261 CI->setAttributes(Attrs);
262 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
263 CI->setCallingConv(F->getCallingConv());
264
265 return CI;
266 }
267
268 /// EmitBinaryFloatFnCall - Emit a call to the binary function named 'Name'
269 /// (e.g. 'fmin'). This function is known to take type matching 'Op1' and 'Op2'
270 /// and return one value with the same type. If 'Op1/Op2' are long double, 'l'
271 /// is added as the suffix of name, if 'Op1/Op2' is a float, we add a 'f'
272 /// suffix.
EmitBinaryFloatFnCall(Value * Op1,Value * Op2,StringRef Name,IRBuilder<> & B,const AttributeSet & Attrs)273 Value *llvm::EmitBinaryFloatFnCall(Value *Op1, Value *Op2, StringRef Name,
274 IRBuilder<> &B, const AttributeSet &Attrs) {
275 SmallString<20> NameBuffer;
276 AppendTypeSuffix(Op1, Name, NameBuffer);
277
278 Module *M = B.GetInsertBlock()->getParent()->getParent();
279 Value *Callee = M->getOrInsertFunction(Name, Op1->getType(),
280 Op1->getType(), Op2->getType(), nullptr);
281 CallInst *CI = B.CreateCall(Callee, {Op1, Op2}, Name);
282 CI->setAttributes(Attrs);
283 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
284 CI->setCallingConv(F->getCallingConv());
285
286 return CI;
287 }
288
289 /// EmitPutChar - Emit a call to the putchar function. This assumes that Char
290 /// is an integer.
EmitPutChar(Value * Char,IRBuilder<> & B,const TargetLibraryInfo * TLI)291 Value *llvm::EmitPutChar(Value *Char, IRBuilder<> &B,
292 const TargetLibraryInfo *TLI) {
293 if (!TLI->has(LibFunc::putchar))
294 return nullptr;
295
296 Module *M = B.GetInsertBlock()->getParent()->getParent();
297 Value *PutChar = M->getOrInsertFunction("putchar", B.getInt32Ty(),
298 B.getInt32Ty(), nullptr);
299 CallInst *CI = B.CreateCall(PutChar,
300 B.CreateIntCast(Char,
301 B.getInt32Ty(),
302 /*isSigned*/true,
303 "chari"),
304 "putchar");
305
306 if (const Function *F = dyn_cast<Function>(PutChar->stripPointerCasts()))
307 CI->setCallingConv(F->getCallingConv());
308 return CI;
309 }
310
311 /// EmitPutS - Emit a call to the puts function. This assumes that Str is
312 /// some pointer.
EmitPutS(Value * Str,IRBuilder<> & B,const TargetLibraryInfo * TLI)313 Value *llvm::EmitPutS(Value *Str, IRBuilder<> &B,
314 const TargetLibraryInfo *TLI) {
315 if (!TLI->has(LibFunc::puts))
316 return nullptr;
317
318 Module *M = B.GetInsertBlock()->getParent()->getParent();
319 AttributeSet AS[2];
320 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
321 AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
322 Attribute::NoUnwind);
323
324 Value *PutS = M->getOrInsertFunction("puts",
325 AttributeSet::get(M->getContext(), AS),
326 B.getInt32Ty(),
327 B.getInt8PtrTy(),
328 nullptr);
329 CallInst *CI = B.CreateCall(PutS, CastToCStr(Str, B), "puts");
330 if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts()))
331 CI->setCallingConv(F->getCallingConv());
332 return CI;
333 }
334
335 /// EmitFPutC - Emit a call to the fputc function. This assumes that Char is
336 /// an integer and File is a pointer to FILE.
EmitFPutC(Value * Char,Value * File,IRBuilder<> & B,const TargetLibraryInfo * TLI)337 Value *llvm::EmitFPutC(Value *Char, Value *File, IRBuilder<> &B,
338 const TargetLibraryInfo *TLI) {
339 if (!TLI->has(LibFunc::fputc))
340 return nullptr;
341
342 Module *M = B.GetInsertBlock()->getParent()->getParent();
343 AttributeSet AS[2];
344 AS[0] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
345 AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
346 Attribute::NoUnwind);
347 Constant *F;
348 if (File->getType()->isPointerTy())
349 F = M->getOrInsertFunction("fputc",
350 AttributeSet::get(M->getContext(), AS),
351 B.getInt32Ty(),
352 B.getInt32Ty(), File->getType(),
353 nullptr);
354 else
355 F = M->getOrInsertFunction("fputc",
356 B.getInt32Ty(),
357 B.getInt32Ty(),
358 File->getType(), nullptr);
359 Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true,
360 "chari");
361 CallInst *CI = B.CreateCall(F, {Char, File}, "fputc");
362
363 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
364 CI->setCallingConv(Fn->getCallingConv());
365 return CI;
366 }
367
368 /// EmitFPutS - Emit a call to the puts function. Str is required to be a
369 /// pointer and File is a pointer to FILE.
EmitFPutS(Value * Str,Value * File,IRBuilder<> & B,const TargetLibraryInfo * TLI)370 Value *llvm::EmitFPutS(Value *Str, Value *File, IRBuilder<> &B,
371 const TargetLibraryInfo *TLI) {
372 if (!TLI->has(LibFunc::fputs))
373 return nullptr;
374
375 Module *M = B.GetInsertBlock()->getParent()->getParent();
376 AttributeSet AS[3];
377 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
378 AS[1] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
379 AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
380 Attribute::NoUnwind);
381 StringRef FPutsName = TLI->getName(LibFunc::fputs);
382 Constant *F;
383 if (File->getType()->isPointerTy())
384 F = M->getOrInsertFunction(FPutsName,
385 AttributeSet::get(M->getContext(), AS),
386 B.getInt32Ty(),
387 B.getInt8PtrTy(),
388 File->getType(), nullptr);
389 else
390 F = M->getOrInsertFunction(FPutsName, B.getInt32Ty(),
391 B.getInt8PtrTy(),
392 File->getType(), nullptr);
393 CallInst *CI = B.CreateCall(F, {CastToCStr(Str, B), File}, "fputs");
394
395 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
396 CI->setCallingConv(Fn->getCallingConv());
397 return CI;
398 }
399
400 /// EmitFWrite - Emit a call to the fwrite function. This assumes that Ptr is
401 /// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
EmitFWrite(Value * Ptr,Value * Size,Value * File,IRBuilder<> & B,const DataLayout & DL,const TargetLibraryInfo * TLI)402 Value *llvm::EmitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B,
403 const DataLayout &DL, const TargetLibraryInfo *TLI) {
404 if (!TLI->has(LibFunc::fwrite))
405 return nullptr;
406
407 Module *M = B.GetInsertBlock()->getParent()->getParent();
408 AttributeSet AS[3];
409 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
410 AS[1] = AttributeSet::get(M->getContext(), 4, Attribute::NoCapture);
411 AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
412 Attribute::NoUnwind);
413 LLVMContext &Context = B.GetInsertBlock()->getContext();
414 StringRef FWriteName = TLI->getName(LibFunc::fwrite);
415 Constant *F;
416 if (File->getType()->isPointerTy())
417 F = M->getOrInsertFunction(
418 FWriteName, AttributeSet::get(M->getContext(), AS),
419 DL.getIntPtrType(Context), B.getInt8PtrTy(), DL.getIntPtrType(Context),
420 DL.getIntPtrType(Context), File->getType(), nullptr);
421 else
422 F = M->getOrInsertFunction(FWriteName, DL.getIntPtrType(Context),
423 B.getInt8PtrTy(), DL.getIntPtrType(Context),
424 DL.getIntPtrType(Context), File->getType(),
425 nullptr);
426 CallInst *CI =
427 B.CreateCall(F, {CastToCStr(Ptr, B), Size,
428 ConstantInt::get(DL.getIntPtrType(Context), 1), File});
429
430 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
431 CI->setCallingConv(Fn->getCallingConv());
432 return CI;
433 }
434