diff --git a/lib/Transforms/Utils/SimplifyLibCalls.cpp b/lib/Transforms/Utils/SimplifyLibCalls.cpp index c3ea63852fed..20281181b2a7 100644 --- a/lib/Transforms/Utils/SimplifyLibCalls.cpp +++ b/lib/Transforms/Utils/SimplifyLibCalls.cpp @@ -951,7 +951,14 @@ struct MemCmpOpt : public LibCallOptimization { // Make sure we're not reading out-of-bounds memory. if (Len > LHSStr.size() || Len > RHSStr.size()) return 0; - uint64_t Ret = memcmp(LHSStr.data(), RHSStr.data(), Len); + // Fold the memcmp and normalize the result. This way we get consistent + // results across multiple platforms. + uint64_t Ret = 0; + int Cmp = memcmp(LHSStr.data(), RHSStr.data(), Len); + if (Cmp < 0) + Ret = -1; + else if (Cmp > 0) + Ret = 1; return ConstantInt::get(CI->getType(), Ret); } diff --git a/test/Transforms/InstCombine/memcmp-1.ll b/test/Transforms/InstCombine/memcmp-1.ll index 4238c5f8fb15..c97b201fc0e9 100644 --- a/test/Transforms/InstCombine/memcmp-1.ll +++ b/test/Transforms/InstCombine/memcmp-1.ll @@ -59,7 +59,7 @@ define i32 @test_simplify5() { %mem2 = getelementptr [4 x i8]* @foo, i32 0, i32 0 %ret = call i32 @memcmp(i8* %mem1, i8* %mem2, i32 3) ret i32 %ret -; CHECK: ret i32 {{[0-9]+}} +; CHECK: ret i32 1 } define i32 @test_simplify6() { @@ -68,5 +68,5 @@ define i32 @test_simplify6() { %mem2 = getelementptr [4 x i8]* @hel, i32 0, i32 0 %ret = call i32 @memcmp(i8* %mem1, i8* %mem2, i32 3) ret i32 %ret -; CHECK: ret i32 {{-[0-9]+}} +; CHECK: ret i32 -1 }