blob: 82ccaea60c82715ea1883a732ffe2695b4f703a0 [file] [log] [blame]
Eric Christopherb6174e32010-03-05 22:25:30 +00001//===- 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/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"
23
24using namespace llvm;
25
26/// CastToCStr - Return V if it is an i8*, otherwise cast it to i8*.
27Value *llvm::CastToCStr(Value *V, IRBuilder<> &B) {
28 return B.CreateBitCast(V, B.getInt8PtrTy(), "cstr");
29}
30
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.
33Value *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 |
38 Attribute::NoUnwind);
39
40 LLVMContext &Context = B.GetInsertBlock()->getContext();
41 Constant *StrLen = M->getOrInsertFunction("strlen", AttrListPtr::get(AWI, 2),
42 TD->getIntPtrType(Context),
43 B.getInt8PtrTy(),
44 NULL);
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());
48
49 return CI;
50}
51
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.
55Value *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);
60
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());
69 return CI;
70}
71
72/// EmitStrCpy - Emit a call to the strcpy function to the builder, for the
73/// specified pointer arguments.
74Value *llvm::EmitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
Benjamin Kramer7fa30b82010-03-11 20:45:13 +000075 const TargetData *TD, StringRef Name) {
Eric Christopherb6174e32010-03-05 22:25:30 +000076 Module *M = B.GetInsertBlock()->getParent()->getParent();
77 AttributeWithIndex AWI[2];
78 AWI[0] = AttributeWithIndex::get(2, Attribute::NoCapture);
79 AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
80 const Type *I8Ptr = B.getInt8PtrTy();
Benjamin Kramer7fa30b82010-03-11 20:45:13 +000081 Value *StrCpy = M->getOrInsertFunction(Name, AttrListPtr::get(AWI, 2),
Eric Christopherb6174e32010-03-05 22:25:30 +000082 I8Ptr, I8Ptr, I8Ptr, NULL);
83 CallInst *CI = B.CreateCall2(StrCpy, CastToCStr(Dst, B), CastToCStr(Src, B),
Benjamin Kramer7fa30b82010-03-11 20:45:13 +000084 Name);
Eric Christopherb6174e32010-03-05 22:25:30 +000085 if (const Function *F = dyn_cast<Function>(StrCpy->stripPointerCasts()))
86 CI->setCallingConv(F->getCallingConv());
87 return CI;
88}
89
Eric Christopherb0722af2010-03-11 17:45:38 +000090/// EmitStrNCpy - Emit a call to the strncpy function to the builder, for the
Eric Christopherbd973762010-03-11 01:25:07 +000091/// specified pointer arguments.
92Value *llvm::EmitStrNCpy(Value *Dst, Value *Src, Value *Len,
93 IRBuilder<> &B, const TargetData *TD) {
94 Module *M = B.GetInsertBlock()->getParent()->getParent();
95 AttributeWithIndex AWI[2];
96 AWI[0] = AttributeWithIndex::get(2, Attribute::NoCapture);
97 AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
98 const Type *I8Ptr = B.getInt8PtrTy();
99 Value *StrNCpy = M->getOrInsertFunction("strncpy", AttrListPtr::get(AWI, 2),
100 I8Ptr, I8Ptr, I8Ptr,
101 Len->getType(), NULL);
102 CallInst *CI = B.CreateCall3(StrNCpy, CastToCStr(Dst, B), CastToCStr(Src, B),
103 Len, "strncpy");
104 if (const Function *F = dyn_cast<Function>(StrNCpy->stripPointerCasts()))
105 CI->setCallingConv(F->getCallingConv());
106 return CI;
107}
108
109
Eric Christopherb6174e32010-03-05 22:25:30 +0000110/// EmitMemCpy - Emit a call to the memcpy function to the builder. This always
111/// expects that the size has type 'intptr_t' and Dst/Src are pointers.
112Value *llvm::EmitMemCpy(Value *Dst, Value *Src, Value *Len,
113 unsigned Align, IRBuilder<> &B, const TargetData *TD) {
114 Module *M = B.GetInsertBlock()->getParent()->getParent();
115 const Type *Ty = Len->getType();
116 Value *MemCpy = Intrinsic::getDeclaration(M, Intrinsic::memcpy, &Ty, 1);
117 Dst = CastToCStr(Dst, B);
118 Src = CastToCStr(Src, B);
119 return B.CreateCall4(MemCpy, Dst, Src, Len,
120 ConstantInt::get(B.getInt32Ty(), Align));
121}
122
123/// EmitMemMove - Emit a call to the memmove function to the builder. This
124/// always expects that the size has type 'intptr_t' and Dst/Src are pointers.
125Value *llvm::EmitMemMove(Value *Dst, Value *Src, Value *Len,
126 unsigned Align, IRBuilder<> &B, const TargetData *TD) {
127 Module *M = B.GetInsertBlock()->getParent()->getParent();
128 LLVMContext &Context = B.GetInsertBlock()->getContext();
129 const Type *Ty = TD->getIntPtrType(Context);
130 Value *MemMove = Intrinsic::getDeclaration(M, Intrinsic::memmove, &Ty, 1);
131 Dst = CastToCStr(Dst, B);
132 Src = CastToCStr(Src, B);
133 Value *A = ConstantInt::get(B.getInt32Ty(), Align);
134 return B.CreateCall4(MemMove, Dst, Src, Len, A);
135}
136
137/// EmitMemChr - Emit a call to the memchr function. This assumes that Ptr is
138/// a pointer, Val is an i32 value, and Len is an 'intptr_t' value.
139Value *llvm::EmitMemChr(Value *Ptr, Value *Val,
140 Value *Len, IRBuilder<> &B, const TargetData *TD) {
141 Module *M = B.GetInsertBlock()->getParent()->getParent();
142 AttributeWithIndex AWI;
143 AWI = AttributeWithIndex::get(~0u, Attribute::ReadOnly | Attribute::NoUnwind);
144 LLVMContext &Context = B.GetInsertBlock()->getContext();
145 Value *MemChr = M->getOrInsertFunction("memchr", AttrListPtr::get(&AWI, 1),
146 B.getInt8PtrTy(),
147 B.getInt8PtrTy(),
148 B.getInt32Ty(),
149 TD->getIntPtrType(Context),
150 NULL);
151 CallInst *CI = B.CreateCall3(MemChr, CastToCStr(Ptr, B), Val, Len, "memchr");
152
153 if (const Function *F = dyn_cast<Function>(MemChr->stripPointerCasts()))
154 CI->setCallingConv(F->getCallingConv());
155
156 return CI;
157}
158
159/// EmitMemCmp - Emit a call to the memcmp function.
160Value *llvm::EmitMemCmp(Value *Ptr1, Value *Ptr2,
161 Value *Len, IRBuilder<> &B, const TargetData *TD) {
162 Module *M = B.GetInsertBlock()->getParent()->getParent();
163 AttributeWithIndex AWI[3];
164 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
165 AWI[1] = AttributeWithIndex::get(2, Attribute::NoCapture);
166 AWI[2] = AttributeWithIndex::get(~0u, Attribute::ReadOnly |
167 Attribute::NoUnwind);
168
169 LLVMContext &Context = B.GetInsertBlock()->getContext();
170 Value *MemCmp = M->getOrInsertFunction("memcmp", AttrListPtr::get(AWI, 3),
171 B.getInt32Ty(),
172 B.getInt8PtrTy(),
173 B.getInt8PtrTy(),
174 TD->getIntPtrType(Context), NULL);
175 CallInst *CI = B.CreateCall3(MemCmp, CastToCStr(Ptr1, B), CastToCStr(Ptr2, B),
176 Len, "memcmp");
177
178 if (const Function *F = dyn_cast<Function>(MemCmp->stripPointerCasts()))
179 CI->setCallingConv(F->getCallingConv());
180
181 return CI;
182}
183
184/// EmitMemSet - Emit a call to the memset function
185Value *llvm::EmitMemSet(Value *Dst, Value *Val,
186 Value *Len, IRBuilder<> &B, const TargetData *TD) {
187 Module *M = B.GetInsertBlock()->getParent()->getParent();
188 Intrinsic::ID IID = Intrinsic::memset;
189 const Type *Tys[1];
190 Tys[0] = Len->getType();
191 Value *MemSet = Intrinsic::getDeclaration(M, IID, Tys, 1);
192 Value *Align = ConstantInt::get(B.getInt32Ty(), 1);
193 return B.CreateCall4(MemSet, CastToCStr(Dst, B), Val, Len, Align);
194}
195
196/// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name' (e.g.
197/// 'floor'). This function is known to take a single of type matching 'Op' and
198/// returns one value with the same type. If 'Op' is a long double, 'l' is
199/// added as the suffix of name, if 'Op' is a float, we add a 'f' suffix.
200Value *llvm::EmitUnaryFloatFnCall(Value *Op, const char *Name,
201 IRBuilder<> &B, const AttrListPtr &Attrs) {
202 char NameBuffer[20];
203 if (!Op->getType()->isDoubleTy()) {
204 // If we need to add a suffix, copy into NameBuffer.
205 unsigned NameLen = strlen(Name);
206 assert(NameLen < sizeof(NameBuffer)-2);
207 memcpy(NameBuffer, Name, NameLen);
208 if (Op->getType()->isFloatTy())
209 NameBuffer[NameLen] = 'f'; // floorf
210 else
211 NameBuffer[NameLen] = 'l'; // floorl
212 NameBuffer[NameLen+1] = 0;
213 Name = NameBuffer;
214 }
215
216 Module *M = B.GetInsertBlock()->getParent()->getParent();
217 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
218 Op->getType(), NULL);
219 CallInst *CI = B.CreateCall(Callee, Op, Name);
220 CI->setAttributes(Attrs);
221 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
222 CI->setCallingConv(F->getCallingConv());
223
224 return CI;
225}
226
227/// EmitPutChar - Emit a call to the putchar function. This assumes that Char
228/// is an integer.
229Value *llvm::EmitPutChar(Value *Char, IRBuilder<> &B, const TargetData *TD) {
230 Module *M = B.GetInsertBlock()->getParent()->getParent();
231 Value *PutChar = M->getOrInsertFunction("putchar", B.getInt32Ty(),
232 B.getInt32Ty(), NULL);
233 CallInst *CI = B.CreateCall(PutChar,
234 B.CreateIntCast(Char,
235 B.getInt32Ty(),
236 /*isSigned*/true,
237 "chari"),
238 "putchar");
239
240 if (const Function *F = dyn_cast<Function>(PutChar->stripPointerCasts()))
241 CI->setCallingConv(F->getCallingConv());
242 return CI;
243}
244
245/// EmitPutS - Emit a call to the puts function. This assumes that Str is
246/// some pointer.
247void llvm::EmitPutS(Value *Str, IRBuilder<> &B, const TargetData *TD) {
248 Module *M = B.GetInsertBlock()->getParent()->getParent();
249 AttributeWithIndex AWI[2];
250 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
251 AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
252
253 Value *PutS = M->getOrInsertFunction("puts", AttrListPtr::get(AWI, 2),
254 B.getInt32Ty(),
255 B.getInt8PtrTy(),
256 NULL);
257 CallInst *CI = B.CreateCall(PutS, CastToCStr(Str, B), "puts");
258 if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts()))
259 CI->setCallingConv(F->getCallingConv());
260
261}
262
263/// EmitFPutC - Emit a call to the fputc function. This assumes that Char is
264/// an integer and File is a pointer to FILE.
265void llvm::EmitFPutC(Value *Char, Value *File, IRBuilder<> &B,
266 const TargetData *TD) {
267 Module *M = B.GetInsertBlock()->getParent()->getParent();
268 AttributeWithIndex AWI[2];
269 AWI[0] = AttributeWithIndex::get(2, Attribute::NoCapture);
270 AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
271 Constant *F;
272 if (File->getType()->isPointerTy())
273 F = M->getOrInsertFunction("fputc", AttrListPtr::get(AWI, 2),
274 B.getInt32Ty(),
275 B.getInt32Ty(), File->getType(),
276 NULL);
277 else
278 F = M->getOrInsertFunction("fputc",
279 B.getInt32Ty(),
280 B.getInt32Ty(),
281 File->getType(), NULL);
282 Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true,
283 "chari");
284 CallInst *CI = B.CreateCall2(F, Char, File, "fputc");
285
286 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
287 CI->setCallingConv(Fn->getCallingConv());
288}
289
290/// EmitFPutS - Emit a call to the puts function. Str is required to be a
291/// pointer and File is a pointer to FILE.
292void llvm::EmitFPutS(Value *Str, Value *File, IRBuilder<> &B,
293 const TargetData *TD) {
294 Module *M = B.GetInsertBlock()->getParent()->getParent();
295 AttributeWithIndex AWI[3];
296 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
297 AWI[1] = AttributeWithIndex::get(2, Attribute::NoCapture);
298 AWI[2] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
299 Constant *F;
300 if (File->getType()->isPointerTy())
301 F = M->getOrInsertFunction("fputs", AttrListPtr::get(AWI, 3),
302 B.getInt32Ty(),
303 B.getInt8PtrTy(),
304 File->getType(), NULL);
305 else
306 F = M->getOrInsertFunction("fputs", B.getInt32Ty(),
307 B.getInt8PtrTy(),
308 File->getType(), NULL);
309 CallInst *CI = B.CreateCall2(F, CastToCStr(Str, B), File, "fputs");
310
311 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
312 CI->setCallingConv(Fn->getCallingConv());
313}
314
315/// EmitFWrite - Emit a call to the fwrite function. This assumes that Ptr is
316/// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
317void llvm::EmitFWrite(Value *Ptr, Value *Size, Value *File,
318 IRBuilder<> &B, const TargetData *TD) {
319 Module *M = B.GetInsertBlock()->getParent()->getParent();
320 AttributeWithIndex AWI[3];
321 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
322 AWI[1] = AttributeWithIndex::get(4, Attribute::NoCapture);
323 AWI[2] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
324 LLVMContext &Context = B.GetInsertBlock()->getContext();
325 Constant *F;
326 if (File->getType()->isPointerTy())
327 F = M->getOrInsertFunction("fwrite", AttrListPtr::get(AWI, 3),
328 TD->getIntPtrType(Context),
329 B.getInt8PtrTy(),
330 TD->getIntPtrType(Context),
331 TD->getIntPtrType(Context),
332 File->getType(), NULL);
333 else
334 F = M->getOrInsertFunction("fwrite", TD->getIntPtrType(Context),
335 B.getInt8PtrTy(),
336 TD->getIntPtrType(Context),
337 TD->getIntPtrType(Context),
338 File->getType(), NULL);
339 CallInst *CI = B.CreateCall4(F, CastToCStr(Ptr, B), Size,
340 ConstantInt::get(TD->getIntPtrType(Context), 1), File);
341
342 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
343 CI->setCallingConv(Fn->getCallingConv());
344}
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000345
346bool SimplifyFortifiedLibCalls::fold(CallInst *CI, const TargetData *TD) {
347 this->CI = CI;
348 StringRef Name = CI->getCalledFunction()->getName();
349 BasicBlock *BB = CI->getParent();
350 IRBuilder<> B(CI->getParent()->getContext());
351
352 // Set the builder to the instruction after the call.
353 B.SetInsertPoint(BB, CI);
354
355 if (Name == "__memcpy_chk") {
356 if (isFoldable(4, 3, false)) {
357 EmitMemCpy(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3),
358 1, B, TD);
359 replaceCall(CI->getOperand(1));
360 return true;
361 }
362 return false;
363 }
364
365 // Should be similar to memcpy.
366 if (Name == "__mempcpy_chk") {
367 return false;
368 }
369
370 if (Name == "__memmove_chk") {
371 if (isFoldable(4, 3, false)) {
372 EmitMemMove(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3),
373 1, B, TD);
374 replaceCall(CI->getOperand(1));
375 return true;
376 }
377 return false;
378 }
379
380 if (Name == "__memset_chk") {
381 if (isFoldable(4, 3, false)) {
382 Value *Val = B.CreateIntCast(CI->getOperand(2), B.getInt8Ty(),
383 false);
384 EmitMemSet(CI->getOperand(1), Val, CI->getOperand(3), B, TD);
385 replaceCall(CI->getOperand(1));
386 return true;
387 }
388 return false;
389 }
390
391 if (Name == "__strcpy_chk" || Name == "__stpcpy_chk") {
392 // If a) we don't have any length information, or b) we know this will
393 // fit then just lower to a plain st[rp]cpy. Otherwise we'll keep our
394 // st[rp]cpy_chk call which may fail at runtime if the size is too long.
395 // TODO: It might be nice to get a maximum length out of the possible
396 // string lengths for varying.
397 if (isFoldable(3, 2, true)) {
398 Value *Ret = EmitStrCpy(CI->getOperand(1), CI->getOperand(2), B, TD,
399 Name.substr(2, 6));
400 replaceCall(Ret);
401 return true;
402 }
403 return false;
404 }
405
406 if (Name == "__strncpy_chk") {
407 if (isFoldable(4, 3, false)) {
408 Value *Ret = EmitStrNCpy(CI->getOperand(1), CI->getOperand(2),
409 CI->getOperand(3), B, TD);
410 replaceCall(Ret);
411 return true;
412 }
413 return false;
414 }
415
416 if (Name == "__strcat_chk") {
417 return false;
418 }
419
420 if (Name == "__strncat_chk") {
421 return false;
422 }
423
424 return false;
425}