1 //===- BuildLibCalls.cpp - Utility builder for libcalls -------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements some functions that will create standard C libcalls.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Transforms/Utils/BuildLibCalls.h"
15 #include "llvm/Type.h"
16 #include "llvm/Constants.h"
17 #include "llvm/Function.h"
18 #include "llvm/Module.h"
19 #include "llvm/Support/IRBuilder.h"
20 #include "llvm/Target/TargetData.h"
21 #include "llvm/LLVMContext.h"
22 #include "llvm/Intrinsics.h"
26 /// CastToCStr - Return V if it is an i8*, otherwise cast it to i8*.
27 Value
*llvm::CastToCStr(Value
*V
, IRBuilder
<> &B
) {
28 return B
.CreateBitCast(V
, B
.getInt8PtrTy(), "cstr");
31 /// EmitStrLen - Emit a call to the strlen function to the builder, for the
32 /// specified pointer. This always returns an integer value of size intptr_t.
33 Value
*llvm::EmitStrLen(Value
*Ptr
, IRBuilder
<> &B
, const TargetData
*TD
) {
34 Module
*M
= B
.GetInsertBlock()->getParent()->getParent();
35 AttributeWithIndex AWI
[2];
36 AWI
[0] = AttributeWithIndex::get(1, Attribute::NoCapture
);
37 AWI
[1] = AttributeWithIndex::get(~0u, Attribute::ReadOnly
|
40 LLVMContext
&Context
= B
.GetInsertBlock()->getContext();
41 Constant
*StrLen
= M
->getOrInsertFunction("strlen", AttrListPtr::get(AWI
, 2),
42 TD
->getIntPtrType(Context
),
45 CallInst
*CI
= B
.CreateCall(StrLen
, CastToCStr(Ptr
, B
), "strlen");
46 if (const Function
*F
= dyn_cast
<Function
>(StrLen
->stripPointerCasts()))
47 CI
->setCallingConv(F
->getCallingConv());
52 /// EmitStrChr - Emit a call to the strchr function to the builder, for the
53 /// specified pointer and character. Ptr is required to be some pointer type,
54 /// and the return value has 'i8*' type.
55 Value
*llvm::EmitStrChr(Value
*Ptr
, char C
, IRBuilder
<> &B
,
56 const TargetData
*TD
) {
57 Module
*M
= B
.GetInsertBlock()->getParent()->getParent();
58 AttributeWithIndex AWI
=
59 AttributeWithIndex::get(~0u, Attribute::ReadOnly
| Attribute::NoUnwind
);
61 const Type
*I8Ptr
= B
.getInt8PtrTy();
62 const Type
*I32Ty
= B
.getInt32Ty();
63 Constant
*StrChr
= M
->getOrInsertFunction("strchr", AttrListPtr::get(&AWI
, 1),
64 I8Ptr
, I8Ptr
, I32Ty
, NULL
);
65 CallInst
*CI
= B
.CreateCall2(StrChr
, CastToCStr(Ptr
, B
),
66 ConstantInt::get(I32Ty
, C
), "strchr");
67 if (const Function
*F
= dyn_cast
<Function
>(StrChr
->stripPointerCasts()))
68 CI
->setCallingConv(F
->getCallingConv());
72 /// EmitStrNCmp - Emit a call to the strncmp function to the builder.
73 Value
*llvm::EmitStrNCmp(Value
*Ptr1
, Value
*Ptr2
, Value
*Len
,
74 IRBuilder
<> &B
, const TargetData
*TD
) {
75 Module
*M
= B
.GetInsertBlock()->getParent()->getParent();
76 AttributeWithIndex AWI
[3];
77 AWI
[0] = AttributeWithIndex::get(1, Attribute::NoCapture
);
78 AWI
[1] = AttributeWithIndex::get(2, Attribute::NoCapture
);
79 AWI
[2] = AttributeWithIndex::get(~0u, Attribute::ReadOnly
|
82 LLVMContext
&Context
= B
.GetInsertBlock()->getContext();
83 Value
*StrNCmp
= M
->getOrInsertFunction("strncmp", AttrListPtr::get(AWI
, 3),
87 TD
->getIntPtrType(Context
), NULL
);
88 CallInst
*CI
= B
.CreateCall3(StrNCmp
, CastToCStr(Ptr1
, B
),
89 CastToCStr(Ptr2
, B
), Len
, "strncmp");
91 if (const Function
*F
= dyn_cast
<Function
>(StrNCmp
->stripPointerCasts()))
92 CI
->setCallingConv(F
->getCallingConv());
97 /// EmitStrCpy - Emit a call to the strcpy function to the builder, for the
98 /// specified pointer arguments.
99 Value
*llvm::EmitStrCpy(Value
*Dst
, Value
*Src
, IRBuilder
<> &B
,
100 const TargetData
*TD
, StringRef Name
) {
101 Module
*M
= B
.GetInsertBlock()->getParent()->getParent();
102 AttributeWithIndex AWI
[2];
103 AWI
[0] = AttributeWithIndex::get(2, Attribute::NoCapture
);
104 AWI
[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind
);
105 const Type
*I8Ptr
= B
.getInt8PtrTy();
106 Value
*StrCpy
= M
->getOrInsertFunction(Name
, AttrListPtr::get(AWI
, 2),
107 I8Ptr
, I8Ptr
, I8Ptr
, NULL
);
108 CallInst
*CI
= B
.CreateCall2(StrCpy
, CastToCStr(Dst
, B
), CastToCStr(Src
, B
),
110 if (const Function
*F
= dyn_cast
<Function
>(StrCpy
->stripPointerCasts()))
111 CI
->setCallingConv(F
->getCallingConv());
115 /// EmitStrNCpy - Emit a call to the strncpy function to the builder, for the
116 /// specified pointer arguments.
117 Value
*llvm::EmitStrNCpy(Value
*Dst
, Value
*Src
, Value
*Len
,
118 IRBuilder
<> &B
, const TargetData
*TD
, StringRef Name
) {
119 Module
*M
= B
.GetInsertBlock()->getParent()->getParent();
120 AttributeWithIndex AWI
[2];
121 AWI
[0] = AttributeWithIndex::get(2, Attribute::NoCapture
);
122 AWI
[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind
);
123 const Type
*I8Ptr
= B
.getInt8PtrTy();
124 Value
*StrNCpy
= M
->getOrInsertFunction(Name
, AttrListPtr::get(AWI
, 2),
126 Len
->getType(), NULL
);
127 CallInst
*CI
= B
.CreateCall3(StrNCpy
, CastToCStr(Dst
, B
), CastToCStr(Src
, B
),
129 if (const Function
*F
= dyn_cast
<Function
>(StrNCpy
->stripPointerCasts()))
130 CI
->setCallingConv(F
->getCallingConv());
134 /// EmitMemCpyChk - Emit a call to the __memcpy_chk function to the builder.
135 /// This expects that the Len and ObjSize have type 'intptr_t' and Dst/Src
137 Value
*llvm::EmitMemCpyChk(Value
*Dst
, Value
*Src
, Value
*Len
, Value
*ObjSize
,
138 IRBuilder
<> &B
, const TargetData
*TD
) {
139 Module
*M
= B
.GetInsertBlock()->getParent()->getParent();
140 AttributeWithIndex AWI
;
141 AWI
= AttributeWithIndex::get(~0u, Attribute::NoUnwind
);
142 LLVMContext
&Context
= B
.GetInsertBlock()->getContext();
143 Value
*MemCpy
= M
->getOrInsertFunction("__memcpy_chk",
144 AttrListPtr::get(&AWI
, 1),
148 TD
->getIntPtrType(Context
),
149 TD
->getIntPtrType(Context
), NULL
);
150 Dst
= CastToCStr(Dst
, B
);
151 Src
= CastToCStr(Src
, B
);
152 CallInst
*CI
= B
.CreateCall4(MemCpy
, Dst
, Src
, Len
, ObjSize
);
153 if (const Function
*F
= dyn_cast
<Function
>(MemCpy
->stripPointerCasts()))
154 CI
->setCallingConv(F
->getCallingConv());
158 /// EmitMemChr - Emit a call to the memchr function. This assumes that Ptr is
159 /// a pointer, Val is an i32 value, and Len is an 'intptr_t' value.
160 Value
*llvm::EmitMemChr(Value
*Ptr
, Value
*Val
,
161 Value
*Len
, IRBuilder
<> &B
, const TargetData
*TD
) {
162 Module
*M
= B
.GetInsertBlock()->getParent()->getParent();
163 AttributeWithIndex AWI
;
164 AWI
= AttributeWithIndex::get(~0u, Attribute::ReadOnly
| Attribute::NoUnwind
);
165 LLVMContext
&Context
= B
.GetInsertBlock()->getContext();
166 Value
*MemChr
= M
->getOrInsertFunction("memchr", AttrListPtr::get(&AWI
, 1),
170 TD
->getIntPtrType(Context
),
172 CallInst
*CI
= B
.CreateCall3(MemChr
, CastToCStr(Ptr
, B
), Val
, Len
, "memchr");
174 if (const Function
*F
= dyn_cast
<Function
>(MemChr
->stripPointerCasts()))
175 CI
->setCallingConv(F
->getCallingConv());
180 /// EmitMemCmp - Emit a call to the memcmp function.
181 Value
*llvm::EmitMemCmp(Value
*Ptr1
, Value
*Ptr2
,
182 Value
*Len
, IRBuilder
<> &B
, const TargetData
*TD
) {
183 Module
*M
= B
.GetInsertBlock()->getParent()->getParent();
184 AttributeWithIndex AWI
[3];
185 AWI
[0] = AttributeWithIndex::get(1, Attribute::NoCapture
);
186 AWI
[1] = AttributeWithIndex::get(2, Attribute::NoCapture
);
187 AWI
[2] = AttributeWithIndex::get(~0u, Attribute::ReadOnly
|
188 Attribute::NoUnwind
);
190 LLVMContext
&Context
= B
.GetInsertBlock()->getContext();
191 Value
*MemCmp
= M
->getOrInsertFunction("memcmp", AttrListPtr::get(AWI
, 3),
195 TD
->getIntPtrType(Context
), NULL
);
196 CallInst
*CI
= B
.CreateCall3(MemCmp
, CastToCStr(Ptr1
, B
), CastToCStr(Ptr2
, B
),
199 if (const Function
*F
= dyn_cast
<Function
>(MemCmp
->stripPointerCasts()))
200 CI
->setCallingConv(F
->getCallingConv());
205 /// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name' (e.g.
206 /// 'floor'). This function is known to take a single of type matching 'Op' and
207 /// returns one value with the same type. If 'Op' is a long double, 'l' is
208 /// added as the suffix of name, if 'Op' is a float, we add a 'f' suffix.
209 Value
*llvm::EmitUnaryFloatFnCall(Value
*Op
, const char *Name
,
210 IRBuilder
<> &B
, const AttrListPtr
&Attrs
) {
212 if (!Op
->getType()->isDoubleTy()) {
213 // If we need to add a suffix, copy into NameBuffer.
214 unsigned NameLen
= strlen(Name
);
215 assert(NameLen
< sizeof(NameBuffer
)-2);
216 memcpy(NameBuffer
, Name
, NameLen
);
217 if (Op
->getType()->isFloatTy())
218 NameBuffer
[NameLen
] = 'f'; // floorf
220 NameBuffer
[NameLen
] = 'l'; // floorl
221 NameBuffer
[NameLen
+1] = 0;
225 Module
*M
= B
.GetInsertBlock()->getParent()->getParent();
226 Value
*Callee
= M
->getOrInsertFunction(Name
, Op
->getType(),
227 Op
->getType(), NULL
);
228 CallInst
*CI
= B
.CreateCall(Callee
, Op
, Name
);
229 CI
->setAttributes(Attrs
);
230 if (const Function
*F
= dyn_cast
<Function
>(Callee
->stripPointerCasts()))
231 CI
->setCallingConv(F
->getCallingConv());
236 /// EmitPutChar - Emit a call to the putchar function. This assumes that Char
238 Value
*llvm::EmitPutChar(Value
*Char
, IRBuilder
<> &B
, const TargetData
*TD
) {
239 Module
*M
= B
.GetInsertBlock()->getParent()->getParent();
240 Value
*PutChar
= M
->getOrInsertFunction("putchar", B
.getInt32Ty(),
241 B
.getInt32Ty(), NULL
);
242 CallInst
*CI
= B
.CreateCall(PutChar
,
243 B
.CreateIntCast(Char
,
249 if (const Function
*F
= dyn_cast
<Function
>(PutChar
->stripPointerCasts()))
250 CI
->setCallingConv(F
->getCallingConv());
254 /// EmitPutS - Emit a call to the puts function. This assumes that Str is
256 void llvm::EmitPutS(Value
*Str
, IRBuilder
<> &B
, const TargetData
*TD
) {
257 Module
*M
= B
.GetInsertBlock()->getParent()->getParent();
258 AttributeWithIndex AWI
[2];
259 AWI
[0] = AttributeWithIndex::get(1, Attribute::NoCapture
);
260 AWI
[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind
);
262 Value
*PutS
= M
->getOrInsertFunction("puts", AttrListPtr::get(AWI
, 2),
266 CallInst
*CI
= B
.CreateCall(PutS
, CastToCStr(Str
, B
), "puts");
267 if (const Function
*F
= dyn_cast
<Function
>(PutS
->stripPointerCasts()))
268 CI
->setCallingConv(F
->getCallingConv());
272 /// EmitFPutC - Emit a call to the fputc function. This assumes that Char is
273 /// an integer and File is a pointer to FILE.
274 void llvm::EmitFPutC(Value
*Char
, Value
*File
, IRBuilder
<> &B
,
275 const TargetData
*TD
) {
276 Module
*M
= B
.GetInsertBlock()->getParent()->getParent();
277 AttributeWithIndex AWI
[2];
278 AWI
[0] = AttributeWithIndex::get(2, Attribute::NoCapture
);
279 AWI
[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind
);
281 if (File
->getType()->isPointerTy())
282 F
= M
->getOrInsertFunction("fputc", AttrListPtr::get(AWI
, 2),
284 B
.getInt32Ty(), File
->getType(),
287 F
= M
->getOrInsertFunction("fputc",
290 File
->getType(), NULL
);
291 Char
= B
.CreateIntCast(Char
, B
.getInt32Ty(), /*isSigned*/true,
293 CallInst
*CI
= B
.CreateCall2(F
, Char
, File
, "fputc");
295 if (const Function
*Fn
= dyn_cast
<Function
>(F
->stripPointerCasts()))
296 CI
->setCallingConv(Fn
->getCallingConv());
299 /// EmitFPutS - Emit a call to the puts function. Str is required to be a
300 /// pointer and File is a pointer to FILE.
301 void llvm::EmitFPutS(Value
*Str
, Value
*File
, IRBuilder
<> &B
,
302 const TargetData
*TD
) {
303 Module
*M
= B
.GetInsertBlock()->getParent()->getParent();
304 AttributeWithIndex AWI
[3];
305 AWI
[0] = AttributeWithIndex::get(1, Attribute::NoCapture
);
306 AWI
[1] = AttributeWithIndex::get(2, Attribute::NoCapture
);
307 AWI
[2] = AttributeWithIndex::get(~0u, Attribute::NoUnwind
);
309 if (File
->getType()->isPointerTy())
310 F
= M
->getOrInsertFunction("fputs", AttrListPtr::get(AWI
, 3),
313 File
->getType(), NULL
);
315 F
= M
->getOrInsertFunction("fputs", B
.getInt32Ty(),
317 File
->getType(), NULL
);
318 CallInst
*CI
= B
.CreateCall2(F
, CastToCStr(Str
, B
), File
, "fputs");
320 if (const Function
*Fn
= dyn_cast
<Function
>(F
->stripPointerCasts()))
321 CI
->setCallingConv(Fn
->getCallingConv());
324 /// EmitFWrite - Emit a call to the fwrite function. This assumes that Ptr is
325 /// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
326 void llvm::EmitFWrite(Value
*Ptr
, Value
*Size
, Value
*File
,
327 IRBuilder
<> &B
, const TargetData
*TD
) {
328 Module
*M
= B
.GetInsertBlock()->getParent()->getParent();
329 AttributeWithIndex AWI
[3];
330 AWI
[0] = AttributeWithIndex::get(1, Attribute::NoCapture
);
331 AWI
[1] = AttributeWithIndex::get(4, Attribute::NoCapture
);
332 AWI
[2] = AttributeWithIndex::get(~0u, Attribute::NoUnwind
);
333 LLVMContext
&Context
= B
.GetInsertBlock()->getContext();
335 if (File
->getType()->isPointerTy())
336 F
= M
->getOrInsertFunction("fwrite", AttrListPtr::get(AWI
, 3),
337 TD
->getIntPtrType(Context
),
339 TD
->getIntPtrType(Context
),
340 TD
->getIntPtrType(Context
),
341 File
->getType(), NULL
);
343 F
= M
->getOrInsertFunction("fwrite", TD
->getIntPtrType(Context
),
345 TD
->getIntPtrType(Context
),
346 TD
->getIntPtrType(Context
),
347 File
->getType(), NULL
);
348 CallInst
*CI
= B
.CreateCall4(F
, CastToCStr(Ptr
, B
), Size
,
349 ConstantInt::get(TD
->getIntPtrType(Context
), 1), File
);
351 if (const Function
*Fn
= dyn_cast
<Function
>(F
->stripPointerCasts()))
352 CI
->setCallingConv(Fn
->getCallingConv());
355 SimplifyFortifiedLibCalls::~SimplifyFortifiedLibCalls() { }
357 bool SimplifyFortifiedLibCalls::fold(CallInst
*CI
, const TargetData
*TD
) {
358 // We really need TargetData for later.
359 if (!TD
) return false;
362 Function
*Callee
= CI
->getCalledFunction();
363 StringRef Name
= Callee
->getName();
364 const FunctionType
*FT
= Callee
->getFunctionType();
365 BasicBlock
*BB
= CI
->getParent();
366 LLVMContext
&Context
= CI
->getParent()->getContext();
367 IRBuilder
<> B(Context
);
369 // Set the builder to the instruction after the call.
370 B
.SetInsertPoint(BB
, CI
);
372 if (Name
== "__memcpy_chk") {
373 // Check if this has the right signature.
374 if (FT
->getNumParams() != 4 || FT
->getReturnType() != FT
->getParamType(0) ||
375 !FT
->getParamType(0)->isPointerTy() ||
376 !FT
->getParamType(1)->isPointerTy() ||
377 FT
->getParamType(2) != TD
->getIntPtrType(Context
) ||
378 FT
->getParamType(3) != TD
->getIntPtrType(Context
))
381 if (isFoldable(3, 2, false)) {
382 B
.CreateMemCpy(CI
->getArgOperand(0), CI
->getArgOperand(1),
383 CI
->getArgOperand(2), 1);
384 replaceCall(CI
->getArgOperand(0));
390 // Should be similar to memcpy.
391 if (Name
== "__mempcpy_chk") {
395 if (Name
== "__memmove_chk") {
396 // Check if this has the right signature.
397 if (FT
->getNumParams() != 4 || FT
->getReturnType() != FT
->getParamType(0) ||
398 !FT
->getParamType(0)->isPointerTy() ||
399 !FT
->getParamType(1)->isPointerTy() ||
400 FT
->getParamType(2) != TD
->getIntPtrType(Context
) ||
401 FT
->getParamType(3) != TD
->getIntPtrType(Context
))
404 if (isFoldable(3, 2, false)) {
405 B
.CreateMemMove(CI
->getArgOperand(0), CI
->getArgOperand(1),
406 CI
->getArgOperand(2), 1);
407 replaceCall(CI
->getArgOperand(0));
413 if (Name
== "__memset_chk") {
414 // Check if this has the right signature.
415 if (FT
->getNumParams() != 4 || FT
->getReturnType() != FT
->getParamType(0) ||
416 !FT
->getParamType(0)->isPointerTy() ||
417 !FT
->getParamType(1)->isIntegerTy() ||
418 FT
->getParamType(2) != TD
->getIntPtrType(Context
) ||
419 FT
->getParamType(3) != TD
->getIntPtrType(Context
))
422 if (isFoldable(3, 2, false)) {
423 Value
*Val
= B
.CreateIntCast(CI
->getArgOperand(1), B
.getInt8Ty(),
425 B
.CreateMemSet(CI
->getArgOperand(0), Val
, CI
->getArgOperand(2), 1);
426 replaceCall(CI
->getArgOperand(0));
432 if (Name
== "__strcpy_chk" || Name
== "__stpcpy_chk") {
433 // Check if this has the right signature.
434 if (FT
->getNumParams() != 3 ||
435 FT
->getReturnType() != FT
->getParamType(0) ||
436 FT
->getParamType(0) != FT
->getParamType(1) ||
437 FT
->getParamType(0) != Type::getInt8PtrTy(Context
) ||
438 FT
->getParamType(2) != TD
->getIntPtrType(Context
))
442 // If a) we don't have any length information, or b) we know this will
443 // fit then just lower to a plain st[rp]cpy. Otherwise we'll keep our
444 // st[rp]cpy_chk call which may fail at runtime if the size is too long.
445 // TODO: It might be nice to get a maximum length out of the possible
446 // string lengths for varying.
447 if (isFoldable(2, 1, true)) {
448 Value
*Ret
= EmitStrCpy(CI
->getArgOperand(0), CI
->getArgOperand(1), B
, TD
,
456 if (Name
== "__strncpy_chk" || Name
== "__stpncpy_chk") {
457 // Check if this has the right signature.
458 if (FT
->getNumParams() != 4 || FT
->getReturnType() != FT
->getParamType(0) ||
459 FT
->getParamType(0) != FT
->getParamType(1) ||
460 FT
->getParamType(0) != Type::getInt8PtrTy(Context
) ||
461 !FT
->getParamType(2)->isIntegerTy() ||
462 FT
->getParamType(3) != TD
->getIntPtrType(Context
))
465 if (isFoldable(3, 2, false)) {
466 Value
*Ret
= EmitStrNCpy(CI
->getArgOperand(0), CI
->getArgOperand(1),
467 CI
->getArgOperand(2), B
, TD
, Name
.substr(2, 7));
474 if (Name
== "__strcat_chk") {
478 if (Name
== "__strncat_chk") {