Use %ull here.
[llvm/stm8.git] / lib / Transforms / Utils / BuildLibCalls.cpp
blob4a90751936b5d0f24460a78c2c4e83f4976952f6
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.
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"
24 using namespace llvm;
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 |
38 Attribute::NoUnwind);
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());
49 return CI;
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());
69 return CI;
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 |
80 Attribute::NoUnwind);
82 LLVMContext &Context = B.GetInsertBlock()->getContext();
83 Value *StrNCmp = M->getOrInsertFunction("strncmp", AttrListPtr::get(AWI, 3),
84 B.getInt32Ty(),
85 B.getInt8PtrTy(),
86 B.getInt8PtrTy(),
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());
94 return CI;
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),
109 Name);
110 if (const Function *F = dyn_cast<Function>(StrCpy->stripPointerCasts()))
111 CI->setCallingConv(F->getCallingConv());
112 return CI;
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),
125 I8Ptr, I8Ptr, I8Ptr,
126 Len->getType(), NULL);
127 CallInst *CI = B.CreateCall3(StrNCpy, CastToCStr(Dst, B), CastToCStr(Src, B),
128 Len, "strncpy");
129 if (const Function *F = dyn_cast<Function>(StrNCpy->stripPointerCasts()))
130 CI->setCallingConv(F->getCallingConv());
131 return CI;
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
136 /// are pointers.
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),
145 B.getInt8PtrTy(),
146 B.getInt8PtrTy(),
147 B.getInt8PtrTy(),
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());
155 return CI;
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),
167 B.getInt8PtrTy(),
168 B.getInt8PtrTy(),
169 B.getInt32Ty(),
170 TD->getIntPtrType(Context),
171 NULL);
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());
177 return CI;
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),
192 B.getInt32Ty(),
193 B.getInt8PtrTy(),
194 B.getInt8PtrTy(),
195 TD->getIntPtrType(Context), NULL);
196 CallInst *CI = B.CreateCall3(MemCmp, CastToCStr(Ptr1, B), CastToCStr(Ptr2, B),
197 Len, "memcmp");
199 if (const Function *F = dyn_cast<Function>(MemCmp->stripPointerCasts()))
200 CI->setCallingConv(F->getCallingConv());
202 return CI;
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) {
211 char NameBuffer[20];
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
219 else
220 NameBuffer[NameLen] = 'l'; // floorl
221 NameBuffer[NameLen+1] = 0;
222 Name = NameBuffer;
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());
233 return CI;
236 /// EmitPutChar - Emit a call to the putchar function. This assumes that Char
237 /// is an integer.
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,
244 B.getInt32Ty(),
245 /*isSigned*/true,
246 "chari"),
247 "putchar");
249 if (const Function *F = dyn_cast<Function>(PutChar->stripPointerCasts()))
250 CI->setCallingConv(F->getCallingConv());
251 return CI;
254 /// EmitPutS - Emit a call to the puts function. This assumes that Str is
255 /// some pointer.
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),
263 B.getInt32Ty(),
264 B.getInt8PtrTy(),
265 NULL);
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);
280 Constant *F;
281 if (File->getType()->isPointerTy())
282 F = M->getOrInsertFunction("fputc", AttrListPtr::get(AWI, 2),
283 B.getInt32Ty(),
284 B.getInt32Ty(), File->getType(),
285 NULL);
286 else
287 F = M->getOrInsertFunction("fputc",
288 B.getInt32Ty(),
289 B.getInt32Ty(),
290 File->getType(), NULL);
291 Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true,
292 "chari");
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);
308 Constant *F;
309 if (File->getType()->isPointerTy())
310 F = M->getOrInsertFunction("fputs", AttrListPtr::get(AWI, 3),
311 B.getInt32Ty(),
312 B.getInt8PtrTy(),
313 File->getType(), NULL);
314 else
315 F = M->getOrInsertFunction("fputs", B.getInt32Ty(),
316 B.getInt8PtrTy(),
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();
334 Constant *F;
335 if (File->getType()->isPointerTy())
336 F = M->getOrInsertFunction("fwrite", AttrListPtr::get(AWI, 3),
337 TD->getIntPtrType(Context),
338 B.getInt8PtrTy(),
339 TD->getIntPtrType(Context),
340 TD->getIntPtrType(Context),
341 File->getType(), NULL);
342 else
343 F = M->getOrInsertFunction("fwrite", TD->getIntPtrType(Context),
344 B.getInt8PtrTy(),
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;
361 this->CI = CI;
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))
379 return false;
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));
385 return true;
387 return false;
390 // Should be similar to memcpy.
391 if (Name == "__mempcpy_chk") {
392 return false;
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))
402 return false;
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));
408 return true;
410 return false;
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))
420 return false;
422 if (isFoldable(3, 2, false)) {
423 Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(),
424 false);
425 B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1);
426 replaceCall(CI->getArgOperand(0));
427 return true;
429 return false;
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))
439 return 0;
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,
449 Name.substr(2, 6));
450 replaceCall(Ret);
451 return true;
453 return false;
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))
463 return false;
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));
468 replaceCall(Ret);
469 return true;
471 return false;
474 if (Name == "__strcat_chk") {
475 return false;
478 if (Name == "__strncat_chk") {
479 return false;
482 return false;