Index: include/llvm/Transforms/Utils/SimplifyLibCalls.h =================================================================== --- include/llvm/Transforms/Utils/SimplifyLibCalls.h +++ include/llvm/Transforms/Utils/SimplifyLibCalls.h @@ -150,6 +150,8 @@ Value *optimizeIsDigit(CallInst *CI, IRBuilder<> &B); Value *optimizeIsAscii(CallInst *CI, IRBuilder<> &B); Value *optimizeToAscii(CallInst *CI, IRBuilder<> &B); + Value *optimizeAtoi(CallInst *CI, IRBuilder<> &B); + Value *optimizeStrtol(CallInst *CI, IRBuilder<> &B); // Formatting and IO Library Call Optimizations Value *optimizeErrorReporting(CallInst *CI, IRBuilder<> &B, Index: lib/Transforms/Utils/SimplifyLibCalls.cpp =================================================================== --- lib/Transforms/Utils/SimplifyLibCalls.cpp +++ lib/Transforms/Utils/SimplifyLibCalls.cpp @@ -1677,6 +1677,29 @@ ConstantInt::get(CI->getType(), 0x7F)); } +Value *LibCallSimplifier::optimizeAtoi(CallInst *CI, IRBuilder<> &B) { + StringRef Str; + if (!getConstantStringInfo(CI->getArgOperand(0), Str)) + return nullptr; + + return ConstantInt::get(CI->getType(), atoi(Str.str().c_str())); +} + +Value *LibCallSimplifier::optimizeStrtol(CallInst *CI, IRBuilder<> &B) { + StringRef Str; + if (!getConstantStringInfo(CI->getArgOperand(0), Str)) + return nullptr; + + if (!isa(CI->getArgOperand(1))) + return nullptr; + + if (ConstantInt *CInt = dyn_cast(CI->getArgOperand(2))) + return ConstantInt::get(CI->getType(), strtol(Str.str().c_str(), nullptr, + CInt->getSExtValue())); + + return nullptr; +} + //===----------------------------------------------------------------------===// // Formatting and IO Library Call Optimizations //===----------------------------------------------------------------------===// @@ -2271,6 +2294,10 @@ return optimizeIsAscii(CI, Builder); case LibFunc_toascii: return optimizeToAscii(CI, Builder); + case LibFunc_atoi: + return optimizeAtoi(CI, Builder); + case LibFunc_strtol: + return optimizeStrtol(CI, Builder); case LibFunc_printf: return optimizePrintF(CI, Builder); case LibFunc_sprintf: Index: test/Transforms/InstCombine/str-int-2.ll =================================================================== --- /dev/null +++ test/Transforms/InstCombine/str-int-2.ll @@ -0,0 +1,14 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py +; RUN: opt < %s -instcombine -S | FileCheck %s + +@.str = private unnamed_addr constant [11 x i8] c"2147483648\00", align 1 + +declare i64 @strtol(i8*, i8**, i32) #1 + +define i64 @strtol_long_int() #0 { +; CHECK-LABEL: @strtol_long_int( +; CHECK-NEXT: ret i64 2147483648 +; + %call = call i64 @strtol(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i32 0, i32 0), i8** null, i32 10) #3 + ret i64 %call +} Index: test/Transforms/InstCombine/str-int.ll =================================================================== --- /dev/null +++ test/Transforms/InstCombine/str-int.ll @@ -0,0 +1,51 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py +; RUN: opt < %s -instcombine -S | FileCheck %s + +@.str = private unnamed_addr constant [3 x i8] c"12\00", align 1 +@.str.1 = private unnamed_addr constant [2 x i8] c"0\00", align 1 + +declare i32 @strtol(i8*, i8**, i32) #1 +declare i32 @atoi(i8*) #2 + +define i32 @strtol_dec() #0 { +; CHECK-LABEL: @strtol_dec( +; CHECK-NEXT: ret i32 12 +; + %call = call i32 @strtol(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i32 0, i32 0), i8** null, i32 10) #2 + ret i32 %call +} + +define i32 @strtol_base_zero() #0 { +; CHECK-LABEL: @strtol_base_zero( +; CHECK-NEXT: ret i32 12 +; + %call = call i32 @strtol(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i32 0, i32 0), i8** null, i32 0) #2 + ret i32 %call +} + +define i32 @strtol_hex() #0 { +; CHECK-LABEL: @strtol_hex( +; CHECK-NEXT: ret i32 18 +; + %call = call i32 @strtol(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i32 0, i32 0), i8** null, i32 16) #2 + ret i32 %call +} + +define i32 @strtol_endptr_not_null() #0 { +; CHECK-LABEL: @strtol_endptr_not_null( +; CHECK-NEXT: [[END:%.*]] = alloca i8*, align 4 +; CHECK-NEXT: [[CALL:%.*]] = call i32 @strtol(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** nonnull [[END]], i32 10) +; CHECK-NEXT: ret i32 [[CALL]] +; + %end = alloca i8*, align 4 + %call = call i32 @strtol(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i32 0, i32 0), i8** %end, i32 10) #2 + ret i32 %call +} + +define i32 @atoi_test() #0 { +; CHECK-LABEL: @atoi_test( +; CHECK-NEXT: ret i32 12 +; + %call = call i32 @atoi(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i32 0, i32 0)) #4 + ret i32 %call +}