Skip to content

Commit 43dc11c

Browse files
committed
Add strncpy libcall creator. Use it when it should be used.
llvm-svn: 98219
1 parent e8e7952 commit 43dc11c

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

llvm/include/llvm/Transforms/Utils/BuildLibCalls.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ namespace llvm {
3939
Value *EmitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
4040
const TargetData *TD);
4141

42+
/// EmitStrNCpy - Emit a call to the strncpy function to the builder, for the
43+
/// specified pointer arguments and length.
44+
Value *EmitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B,
45+
const TargetData *TD);
46+
4247
/// EmitMemCpy - Emit a call to the memcpy function to the builder. This
4348
/// always expects that the size has type 'intptr_t' and Dst/Src are pointers.
4449
Value *EmitMemCpy(Value *Dst, Value *Src, Value *Len,

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,8 @@ Instruction *InstCombiner::tryOptimizeCall(CallInst *CI, const TargetData *TD) {
850850
return 0;
851851
if (SizeCI->isAllOnesValue() ||
852852
SizeCI->getZExtValue() <= SizeArg->getZExtValue()) {
853-
Value *Ret = EmitStrCpy(CI->getOperand(1), CI->getOperand(2), B, TD);
853+
Value *Ret = EmitStrNCpy(CI->getOperand(1), CI->getOperand(2),
854+
CI->getOperand(3), B, TD);
854855
return ReplaceInstUsesWith(*CI, Ret);
855856
}
856857
return 0;

llvm/lib/Transforms/Utils/BuildLibCalls.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,26 @@ Value *llvm::EmitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
8787
return CI;
8888
}
8989

90+
/// EmitStrNCpy - Emit a call to the strcpy function to the builder, for the
91+
/// specified pointer arguments.
92+
Value *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+
90110
/// EmitMemCpy - Emit a call to the memcpy function to the builder. This always
91111
/// expects that the size has type 'intptr_t' and Dst/Src are pointers.
92112
Value *llvm::EmitMemCpy(Value *Dst, Value *Src, Value *Len,

0 commit comments

Comments
 (0)