[llvm] 98db333 - [SLC] Fix pointer diff type in sprintf() optimization
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Tue Jan 25 06:23:03 PST 2022
Author: Nikita Popov
Date: 2022-01-25T15:22:56+01:00
New Revision: 98db33349bcc4d12a56313c406c1f40038258f10
URL: https://github.com/llvm/llvm-project/commit/98db33349bcc4d12a56313c406c1f40038258f10
DIFF: https://github.com/llvm/llvm-project/commit/98db33349bcc4d12a56313c406c1f40038258f10.diff
LOG: [SLC] Fix pointer diff type in sprintf() optimization
We should always be calculating a byte-wise difference here.
Previously this calculated the pointer difference while taking
the pointer element type into account, which is incorrect.
Added:
Modified:
llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
llvm/test/Transforms/InstCombine/stpcpy-1.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
index 123fb0cfd1cb..e02d02a05752 100644
--- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
@@ -2515,9 +2515,9 @@ Value *LibCallSimplifier::optimizeSPrintFString(CallInst *CI,
} else if (Value *V = emitStpCpy(Dest, CI->getArgOperand(2), B, TLI)) {
// sprintf(dest, "%s", str) -> stpcpy(dest, str) - dest
// Handle mismatched pointer types (goes away with typeless pointers?).
- V = B.CreatePointerCast(V, Dest->getType());
- Value *PtrDiff = B.CreatePtrDiff(
- Dest->getType()->getPointerElementType(), V, Dest);
+ V = B.CreatePointerCast(V, B.getInt8PtrTy());
+ Dest = B.CreatePointerCast(Dest, B.getInt8PtrTy());
+ Value *PtrDiff = B.CreatePtrDiff(B.getInt8Ty(), V, Dest);
return B.CreateIntCast(PtrDiff, CI->getType(), false);
}
diff --git a/llvm/test/Transforms/InstCombine/stpcpy-1.ll b/llvm/test/Transforms/InstCombine/stpcpy-1.ll
index 5c59edc639c9..79587102af8a 100644
--- a/llvm/test/Transforms/InstCombine/stpcpy-1.ll
+++ b/llvm/test/Transforms/InstCombine/stpcpy-1.ll
@@ -59,8 +59,8 @@ define i8* @test_no_simplify1() {
define i8* @test_no_simplify2(i8* %dst, i8* %src) {
; CHECK-LABEL: @test_no_simplify2(
-; CHECK-NEXT: %ret = musttail call i8* @stpcpy(i8* %dst, i8* %src)
-; CHECK-NEXT: ret i8* %ret
+; CHECK-NEXT: [[RET:%.*]] = musttail call i8* @stpcpy(i8* [[DST:%.*]], i8* [[SRC:%.*]])
+; CHECK-NEXT: ret i8* [[RET]]
;
%ret = musttail call i8* @stpcpy(i8* %dst, i8* %src)
ret i8* %ret
@@ -87,13 +87,9 @@ define i32 @PR51200(i8** %p, i32* %p2) {
; CHECK-NEXT: [[CSTR1:%.*]] = bitcast i32* [[P2:%.*]] to i8*
; CHECK-NEXT: [[STPCPY:%.*]] = call i8* @stpcpy(i8* [[CSTR]], i8* [[CSTR1]])
; CHECK-NEXT: [[TMP1:%.*]] = ptrtoint i8* [[STPCPY]] to i32
-; CHECK-NEXT: [[TMP2:%.*]] = zext i32 [[TMP1]] to i64
-; CHECK-NEXT: [[TMP3:%.*]] = ptrtoint i8** [[P]] to i32
-; CHECK-NEXT: [[TMP4:%.*]] = zext i32 [[TMP3]] to i64
-; CHECK-NEXT: [[TMP5:%.*]] = sub nsw i64 [[TMP2]], [[TMP4]]
-; CHECK-NEXT: [[TMP6:%.*]] = lshr exact i64 [[TMP5]], 2
-; CHECK-NEXT: [[TMP7:%.*]] = trunc i64 [[TMP6]] to i32
-; CHECK-NEXT: ret i32 [[TMP7]]
+; CHECK-NEXT: [[TMP2:%.*]] = ptrtoint i8** [[P]] to i32
+; CHECK-NEXT: [[TMP3:%.*]] = sub i32 [[TMP1]], [[TMP2]]
+; CHECK-NEXT: ret i32 [[TMP3]]
;
%call = call i32 (i8**, i32*, ...) @sprintf(i8** %p, i32* bitcast ([3 x i8]* @percent_s to i32*), i32* %p2)
ret i32 %call
More information about the llvm-commits
mailing list