forked from OSchip/llvm-project
[InstCombine] sprintf(dest, "%s", str) -> memccpy(dest, str, 0, MAX)
llvm-svn: 373333
This commit is contained in:
parent
f488cbdcd8
commit
8fc6a1bf56
|
@ -2415,9 +2415,11 @@ Value *LibCallSimplifier::optimizePrintF(CallInst *CI, IRBuilder<> &B) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Value *LibCallSimplifier::optimizeSPrintFString(CallInst *CI, IRBuilder<> &B) {
|
Value *LibCallSimplifier::optimizeSPrintFString(CallInst *CI, IRBuilder<> &B) {
|
||||||
|
Value *Dst = CI->getArgOperand(0);
|
||||||
|
Value *FmtStr = CI->getArgOperand(1);
|
||||||
// Check for a fixed format string.
|
// Check for a fixed format string.
|
||||||
StringRef FormatStr;
|
StringRef FormatStr;
|
||||||
if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr))
|
if (!getConstantStringInfo(FmtStr, FormatStr))
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
// If we just have a format string (nothing else crazy) transform it.
|
// If we just have a format string (nothing else crazy) transform it.
|
||||||
|
@ -2428,9 +2430,10 @@ Value *LibCallSimplifier::optimizeSPrintFString(CallInst *CI, IRBuilder<> &B) {
|
||||||
return nullptr; // we found a format specifier, bail out.
|
return nullptr; // we found a format specifier, bail out.
|
||||||
|
|
||||||
// sprintf(str, fmt) -> llvm.memcpy(align 1 str, align 1 fmt, strlen(fmt)+1)
|
// sprintf(str, fmt) -> llvm.memcpy(align 1 str, align 1 fmt, strlen(fmt)+1)
|
||||||
B.CreateMemCpy(CI->getArgOperand(0), 1, CI->getArgOperand(1), 1,
|
B.CreateMemCpy(
|
||||||
ConstantInt::get(DL.getIntPtrType(CI->getContext()),
|
Dst, 1, FmtStr, 1,
|
||||||
FormatStr.size() + 1)); // Copy the null byte.
|
ConstantInt::get(DL.getIntPtrType(CI->getContext()),
|
||||||
|
FormatStr.size() + 1)); // Copy the null byte.
|
||||||
return ConstantInt::get(CI->getType(), FormatStr.size());
|
return ConstantInt::get(CI->getType(), FormatStr.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2440,13 +2443,14 @@ Value *LibCallSimplifier::optimizeSPrintFString(CallInst *CI, IRBuilder<> &B) {
|
||||||
CI->getNumArgOperands() < 3)
|
CI->getNumArgOperands() < 3)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
|
Value *Str = CI->getArgOperand(2);
|
||||||
// Decode the second character of the format string.
|
// Decode the second character of the format string.
|
||||||
if (FormatStr[1] == 'c') {
|
if (FormatStr[1] == 'c') {
|
||||||
// sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
|
// sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
|
||||||
if (!CI->getArgOperand(2)->getType()->isIntegerTy())
|
if (!Str->getType()->isIntegerTy())
|
||||||
return nullptr;
|
return nullptr;
|
||||||
Value *V = B.CreateTrunc(CI->getArgOperand(2), B.getInt8Ty(), "char");
|
Value *V = B.CreateTrunc(Str, B.getInt8Ty(), "char");
|
||||||
Value *Ptr = castToCStr(CI->getArgOperand(0), B);
|
Value *Ptr = castToCStr(Dst, B);
|
||||||
B.CreateStore(V, Ptr);
|
B.CreateStore(V, Ptr);
|
||||||
Ptr = B.CreateGEP(B.getInt8Ty(), Ptr, B.getInt32(1), "nul");
|
Ptr = B.CreateGEP(B.getInt8Ty(), Ptr, B.getInt32(1), "nul");
|
||||||
B.CreateStore(B.getInt8(0), Ptr);
|
B.CreateStore(B.getInt8(0), Ptr);
|
||||||
|
@ -2455,17 +2459,30 @@ Value *LibCallSimplifier::optimizeSPrintFString(CallInst *CI, IRBuilder<> &B) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (FormatStr[1] == 's') {
|
if (FormatStr[1] == 's') {
|
||||||
|
if (!Str->getType()->isPointerTy())
|
||||||
|
return nullptr;
|
||||||
|
|
||||||
|
// sprintf(dest, "%s", str) -> memccpy(dest, str, 0, MAX)
|
||||||
|
if (CI->use_empty()) {
|
||||||
|
Value *S = (Dst->getType() == Str->getType())
|
||||||
|
? Str
|
||||||
|
: B.CreateBitCast(Str, Dst->getType());
|
||||||
|
unsigned SizeTyBitwidth =
|
||||||
|
DL.getIntPtrType(CI->getContext())->getPrimitiveSizeInBits();
|
||||||
|
Value *NewSize = ConstantInt::get(B.getIntNTy(SizeTyBitwidth),
|
||||||
|
APInt::getMaxValue(SizeTyBitwidth));
|
||||||
|
emitMemCCpy(Dst, S, B.getInt32('\0'), NewSize, B, TLI);
|
||||||
|
return Dst;
|
||||||
|
}
|
||||||
|
|
||||||
// sprintf(dest, "%s", str) -> llvm.memcpy(align 1 dest, align 1 str,
|
// sprintf(dest, "%s", str) -> llvm.memcpy(align 1 dest, align 1 str,
|
||||||
// strlen(str)+1)
|
// strlen(str)+1)
|
||||||
if (!CI->getArgOperand(2)->getType()->isPointerTy())
|
Value *Len = emitStrLen(Str, B, DL, TLI);
|
||||||
return nullptr;
|
|
||||||
|
|
||||||
Value *Len = emitStrLen(CI->getArgOperand(2), B, DL, TLI);
|
|
||||||
if (!Len)
|
if (!Len)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
Value *IncLen =
|
Value *IncLen =
|
||||||
B.CreateAdd(Len, ConstantInt::get(Len->getType(), 1), "leninc");
|
B.CreateAdd(Len, ConstantInt::get(Len->getType(), 1), "leninc");
|
||||||
B.CreateMemCpy(CI->getArgOperand(0), 1, CI->getArgOperand(2), 1, IncLen);
|
B.CreateMemCpy(Dst, 1, Str, 1, IncLen);
|
||||||
|
|
||||||
// The sprintf result is the unincremented number of bytes in the string.
|
// The sprintf result is the unincremented number of bytes in the string.
|
||||||
return B.CreateIntCast(Len, CI->getType(), false);
|
return B.CreateIntCast(Len, CI->getType(), false);
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
|
||||||
; RUN: opt -instcombine -S < %s | FileCheck %s
|
; RUN: opt -instcombine -S < %s | FileCheck %s
|
||||||
; PR7265
|
; PR7265
|
||||||
|
|
||||||
|
@ -9,10 +10,14 @@ target triple = "x86_64-unknown-linux-gnu"
|
||||||
@.str = private constant [3 x i8] c"%s\00"
|
@.str = private constant [3 x i8] c"%s\00"
|
||||||
|
|
||||||
define void @CopyEventArg(%union.anon* %ev) nounwind {
|
define void @CopyEventArg(%union.anon* %ev) nounwind {
|
||||||
|
; CHECK-LABEL: @CopyEventArg(
|
||||||
|
; CHECK-NEXT: entry:
|
||||||
|
; CHECK-NEXT: [[TMP0:%.*]] = bitcast %union.anon* [[EV:%.*]] to i8*
|
||||||
|
; CHECK-NEXT: [[MEMCCPY:%.*]] = call i8* @memccpy(i8* undef, i8* [[TMP0]], i32 0, i64 -1)
|
||||||
|
; CHECK-NEXT: ret void
|
||||||
|
;
|
||||||
entry:
|
entry:
|
||||||
%call = call i32 (i8*, i8*, ...) @sprintf(i8* undef, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), %union.anon* %ev) nounwind
|
%call = call i32 (i8*, i8*, ...) @sprintf(i8* undef, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), %union.anon* %ev) nounwind
|
||||||
; CHECK: bitcast %union.anon* %ev to i8*
|
|
||||||
; CHECK: call void @llvm.memcpy.p0i8.p0i8.i64
|
|
||||||
ret void
|
ret void
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -85,15 +85,11 @@ define void @test_simplify4(i8* %dst) {
|
||||||
|
|
||||||
define void @test_simplify5(i8* %dst, i8* %str) {
|
define void @test_simplify5(i8* %dst, i8* %str) {
|
||||||
; CHECK-LABEL: @test_simplify5(
|
; CHECK-LABEL: @test_simplify5(
|
||||||
; CHECK-NEXT: [[STRLEN:%.*]] = call i32 @strlen(i8* nonnull dereferenceable(1) [[STR:%.*]])
|
; CHECK-NEXT: [[MEMCCPY:%.*]] = call i8* @memccpy(i8* [[DST:%.*]], i8* [[STR:%.*]], i32 0, i32 -1)
|
||||||
; CHECK-NEXT: [[LENINC:%.*]] = add i32 [[STRLEN]], 1
|
|
||||||
; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* align 1 [[DST:%.*]], i8* align 1 [[STR]], i32 [[LENINC]], i1 false)
|
|
||||||
; CHECK-NEXT: ret void
|
; CHECK-NEXT: ret void
|
||||||
;
|
;
|
||||||
; CHECK-IPRINTF-LABEL: @test_simplify5(
|
; CHECK-IPRINTF-LABEL: @test_simplify5(
|
||||||
; CHECK-IPRINTF-NEXT: [[STRLEN:%.*]] = call i32 @strlen(i8* nonnull dereferenceable(1) [[STR:%.*]])
|
; CHECK-IPRINTF-NEXT: [[MEMCCPY:%.*]] = call i8* @memccpy(i8* [[DST:%.*]], i8* [[STR:%.*]], i32 0, i32 -1)
|
||||||
; CHECK-IPRINTF-NEXT: [[LENINC:%.*]] = add i32 [[STRLEN]], 1
|
|
||||||
; CHECK-IPRINTF-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* align 1 [[DST:%.*]], i8* align 1 [[STR]], i32 [[LENINC]], i1 false)
|
|
||||||
; CHECK-IPRINTF-NEXT: ret void
|
; CHECK-IPRINTF-NEXT: ret void
|
||||||
;
|
;
|
||||||
%fmt = getelementptr [3 x i8], [3 x i8]* @percent_s, i32 0, i32 0
|
%fmt = getelementptr [3 x i8], [3 x i8]* @percent_s, i32 0, i32 0
|
||||||
|
|
Loading…
Reference in New Issue