[llvm] r223583 - InstSimplify: Optimize away useless unsigned comparisons

Kostya Serebryany kcc at google.com
Mon Dec 8 08:25:26 PST 2014


msan is unhappy:
http://lab.llvm.org:8011/builders/sanitizer-x86_64-linux-bootstrap/builds/5268/steps/build%20stage3%2Fmsan%20clang/logs/stdio
Can this be related?

==6641== WARNING: MemorySanitizer: use-of-uninitialized-value
    #0 0x7f04d6c29d69 in simplifyUnsignedRangeCheck(llvm::ICmpInst*,
llvm::ICmpInst*, bool)
/mnt/b/sanitizer-buildbot2/sanitizer-x86_64-linux-bootstrap/build/llvm/lib/Analysis/InstructionSimplify.cpp:1468:7
    #1 0x7f04d6c2b026 in SimplifyAndOfICmps(llvm::ICmpInst*,
llvm::ICmpInst*)
/mnt/b/sanitizer-buildbot2/sanitizer-x86_64-linux-bootstrap/build/llvm/lib/Analysis/InstructionSimplify.cpp:1489:18
    #2 0x7f04d6bf2855 in SimplifyAndInst(llvm::Value*, llvm::Value*,
(anonymous namespace)::Query const&, unsigned int)
/mnt/b/sanitizer-buildbot2/sanitizer-x86_64-linux-bootstrap/build/llvm/lib/Analysis/InstructionSimplify.cpp:1594:22
    #3 0x7f04d6c13520 in SimplifyAndInst
/mnt/b/sanitizer-buildbot2/sanitizer-x86_64-linux-bootstrap/build/llvm/lib/Analysis/InstructionSimplify.cpp:1635:10
    #4 0x7f04d6c13520 in llvm::SimplifyInstruction(llvm::Instruction*,
llvm::DataLayout const*, llvm::TargetLibraryInfo const*,
llvm::DominatorTree const*, llvm::AssumptionTracker*)
/mnt/b/sanitizer-buildbot2/sanitizer-x86_64-linux-bootstrap/build/llvm/lib/Analysis/InstructionSimplify.cpp:3633
    #5 0x7f04d7e2c861 in processNode
/mnt/b/sanitizer-buildbot2/sanitizer-x86_64-linux-bootstrap/build/llvm/lib/Transforms/Scalar/EarlyCSE.cpp:452:20
    #6 0x7f04d7e2c861 in (anonymous
namespace)::EarlyCSE::runOnFunction(llvm::Function&)
/mnt/b/sanitizer-buildbot2/sanitizer-x86_64-linux-bootstrap/build/llvm/lib/Transforms/Scalar/EarlyCSE.cpp:620
    #7 0x7f04d77f0ccc in
llvm::FPPassManager::runOnFunction(llvm::Function&)
/mnt/b/sanitizer-buildbot2/sanitizer-x86_64-linux-bootstrap/build/llvm/lib/IR/LegacyPassManager.cpp:1541:23
    #8 0x7f04d77ef0df in
llvm::legacy::FunctionPassManagerImpl::run(llvm::Function&)
/mnt/b/sanitizer-buildbot2/sanitizer-x86_64-linux-bootstrap/build/llvm/lib/IR/LegacyPassManager.cpp:1490:16
    #9 0x7f04d90ca6e5 in EmitAssembly
/mnt/b/sanitizer-buildbot2/sanitizer-x86_64-linux-bootstrap/build/llvm/tools/clang/lib/CodeGen/BackendUtil.cpp:601:9
    #10 0x7f04d90ca6e5 in
clang::EmitBackendOutput(clang::DiagnosticsEngine&,
clang::CodeGenOptions const&, clang::TargetOptions const&,
clang::LangOptions const&, llvm::StringRef, llvm::Module*,
clang::BackendAction, llvm::raw_ostream*)
/mnt/b/sanitizer-buildbot2/sanitizer-x86_64-linux-bootstrap/build/llvm/tools/clang/lib/CodeGen/BackendUtil.cpp:624
    #11 0x7f04d90a4a6e in
clang::BackendConsumer::HandleTranslationUnit(clang::ASTContext&)
/mnt/b/sanitizer-buildbot2/sanitizer-x86_64-linux-bootstrap/build/llvm/tools/clang/lib/CodeGen/CodeGenAction.cpp:175:7
    #12 0x7f04da511082 in clang::ParseAST(clang::Sema&, bool, bool)
/mnt/b/sanitizer-buildbot2/sanitizer-x86_64-linux-bootstrap/build/llvm/tools/clang/lib/Parse/ParseAST.cpp:154:3
    #13 0x7f04d8797035 in clang::FrontendAction::Execute()
/mnt/b/sanitizer-buildbot2/sanitizer-x86_64-linux-bootstrap/build/llvm/tools/clang/lib/Frontend/FrontendAction.cpp:428:8
    #14 0x7f04d8712d52 in
clang::CompilerInstance::ExecuteAction(clang::FrontendAction&)
/mnt/b/sanitizer-buildbot2/sanitizer-x86_64-linux-bootstrap/build/llvm/tools/clang/lib/Frontend/CompilerInstance.cpp:811:7
    #15 0x7f04d8963c6c in
clang::ExecuteCompilerInvocation(clang::CompilerInstance*)
/mnt/b/sanitizer-buildbot2/sanitizer-x86_64-linux-bootstrap/build/llvm/tools/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp:222:18
    #16 0x7f04d53a1bbb in cc1_main(llvm::ArrayRef<char const*>, char
const*, void*) /mnt/b/sanitizer-buildbot2/sanitizer-x86_64-linux-bootstrap/build/llvm/tools/clang/tools/driver/cc1_main.cpp:110:13
    #17 0x7f04d539c84b in ExecuteCC1Tool
/mnt/b/sanitizer-buildbot2/sanitizer-x86_64-linux-bootstrap/build/llvm/tools/clang/tools/driver/driver.cpp:369:12
   #18 0x7f04d539c84b in main
/mnt/b/sanitizer-buildbot2/sanitizer-x86_64-linux-bootstrap/build/llvm/tools/clang/tools/driver/driver.cpp:415
    #19 0x7f04d362eeac in __libc_start_main
(/lib/x86_64-linux-gnu/libc.so.6+0x1eeac)
    #20 0x7f04d53317b0 in _start
(/mnt/b/sanitizer-buildbot2/sanitizer-x86_64-linux-bootstrap/build/llvm_build_msan/bin/clang-3.6+0x6357b0)



On Sat, Dec 6, 2014 at 2:51 AM, David Majnemer <david.majnemer at gmail.com>
wrote:

> Author: majnemer
> Date: Sat Dec  6 04:51:40 2014
> New Revision: 223583
>
> URL: http://llvm.org/viewvc/llvm-project?rev=223583&view=rev
> Log:
> InstSimplify: Optimize away useless unsigned comparisons
>
> Code like X < Y && Y == 0 should always be folded away to false.
>
> Modified:
>     llvm/trunk/lib/Analysis/InstructionSimplify.cpp
>     llvm/trunk/lib/IR/ConstantFold.cpp
>     llvm/trunk/test/Transforms/InstSimplify/AndOrXor.ll
>
> Modified: llvm/trunk/lib/Analysis/InstructionSimplify.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/InstructionSimplify.cpp?rev=223583&r1=223582&r2=223583&view=diff
>
> ==============================================================================
> --- llvm/trunk/lib/Analysis/InstructionSimplify.cpp (original)
> +++ llvm/trunk/lib/Analysis/InstructionSimplify.cpp Sat Dec  6 04:51:40
> 2014
> @@ -1443,12 +1443,57 @@ Value *llvm::SimplifyAShrInst(Value *Op0
>                              RecursionLimit);
>  }
>
> +static Value *simplifyUnsignedRangeCheck(ICmpInst *ZeroICmp,
> +                                         ICmpInst *UnsignedICmp, bool
> IsAnd) {
> +  Value *X, *Y;
> +
> +  ICmpInst::Predicate EqPred;
> +  if (!match(ZeroICmp, m_ICmp(EqPred, m_Value(Y), m_Zero())) &&
> +      ICmpInst::isEquality(EqPred))
> +    return nullptr;
> +
> +  ICmpInst::Predicate UnsignedPred;
> +  if (match(UnsignedICmp, m_ICmp(UnsignedPred, m_Value(X),
> m_Specific(Y))) &&
> +      ICmpInst::isUnsigned(UnsignedPred))
> +    ;
> +  else if (match(UnsignedICmp,
> +                 m_ICmp(UnsignedPred, m_Value(Y), m_Specific(X))) &&
> +           ICmpInst::isUnsigned(UnsignedPred))
> +    UnsignedPred = ICmpInst::getSwappedPredicate(UnsignedPred);
> +  else
> +    return nullptr;
> +
> +  // X < Y && Y != 0  -->  X < Y
> +  // X < Y || Y != 0  -->  Y != 0
> +  if (UnsignedPred == ICmpInst::ICMP_ULT && EqPred == ICmpInst::ICMP_NE)
> +    return IsAnd ? UnsignedICmp : ZeroICmp;
> +
> +  // X >= Y || Y != 0  -->  true
> +  // X >= Y || Y == 0  -->  X >= Y
> +  if (UnsignedPred == ICmpInst::ICMP_UGE && !IsAnd) {
> +    if (EqPred == ICmpInst::ICMP_NE)
> +      return getTrue(UnsignedICmp->getType());
> +    return UnsignedICmp;
> +  }
> +
> +  // X < Y && Y == 0  -->  false
> +  if (UnsignedPred == ICmpInst::ICMP_ULT && EqPred == ICmpInst::ICMP_EQ &&
> +      IsAnd)
> +    return getFalse(UnsignedICmp->getType());
> +
> +  return nullptr;
> +}
> +
>  // Simplify (and (icmp ...) (icmp ...)) to true when we can tell that the
> range
>  // of possible values cannot be satisfied.
>  static Value *SimplifyAndOfICmps(ICmpInst *Op0, ICmpInst *Op1) {
>    ICmpInst::Predicate Pred0, Pred1;
>    ConstantInt *CI1, *CI2;
>    Value *V;
> +
> +  if (Value *X = simplifyUnsignedRangeCheck(Op0, Op1, /*IsAnd=*/true))
> +    return X;
> +
>    if (!match(Op0, m_ICmp(Pred0, m_Add(m_Value(V), m_ConstantInt(CI1)),
>                           m_ConstantInt(CI2))))
>     return nullptr;
> @@ -1602,6 +1647,10 @@ static Value *SimplifyOrOfICmps(ICmpInst
>    ICmpInst::Predicate Pred0, Pred1;
>    ConstantInt *CI1, *CI2;
>    Value *V;
> +
> +  if (Value *X = simplifyUnsignedRangeCheck(Op0, Op1, /*IsAnd=*/false))
> +    return X;
> +
>    if (!match(Op0, m_ICmp(Pred0, m_Add(m_Value(V), m_ConstantInt(CI1)),
>                           m_ConstantInt(CI2))))
>     return nullptr;
>
> Modified: llvm/trunk/lib/IR/ConstantFold.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/ConstantFold.cpp?rev=223583&r1=223582&r2=223583&view=diff
>
> ==============================================================================
> --- llvm/trunk/lib/IR/ConstantFold.cpp (original)
> +++ llvm/trunk/lib/IR/ConstantFold.cpp Sat Dec  6 04:51:40 2014
> @@ -1350,7 +1350,7 @@ static ICmpInst::Predicate areGlobalsPot
>                                                        const GlobalValue
> *GV2) {
>    // Don't try to decide equality of aliases.
>    if (!isa<GlobalAlias>(GV1) && !isa<GlobalAlias>(GV2))
> -    if (!GV1->hasExternalWeakLinkage() || !GV2->hasExternalWeakLinkage())
> +    if (!GV1->hasExternalWeakLinkage() && !GV2->hasExternalWeakLinkage())
>        return ICmpInst::ICMP_NE;
>    return ICmpInst::BAD_ICMP_PREDICATE;
>  }
>
> Modified: llvm/trunk/test/Transforms/InstSimplify/AndOrXor.ll
> URL:
> http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstSimplify/AndOrXor.ll?rev=223583&r1=223582&r2=223583&view=diff
>
> ==============================================================================
> --- llvm/trunk/test/Transforms/InstSimplify/AndOrXor.ll (original)
> +++ llvm/trunk/test/Transforms/InstSimplify/AndOrXor.ll Sat Dec  6
> 04:51:40 2014
> @@ -155,3 +155,51 @@ define i32 @neg_nuw(i32 %x) {
>    ret i32 %neg
>  ; CHECK: ret i32 0
>  }
> +
> +define i1 @and_icmp1(i32 %x, i32 %y) {
> +  %1 = icmp ult i32 %x, %y
> +  %2 = icmp ne i32 %y, 0
> +  %3 = and i1 %1, %2
> +  ret i1 %3
> +}
> +; CHECK-LABEL: @and_icmp1(
> +; CHECK: %[[cmp:.*]] = icmp ult i32 %x, %y
> +; CHECK: ret i1 %[[cmp]]
> +
> +define i1 @and_icmp2(i32 %x, i32 %y) {
> +  %1 = icmp ult i32 %x, %y
> +  %2 = icmp eq i32 %y, 0
> +  %3 = and i1 %1, %2
> +  ret i1 %3
> +}
> +; CHECK-LABEL: @and_icmp2(
> +; CHECK: ret i1 false
> +
> +define i1 @or_icmp1(i32 %x, i32 %y) {
> +  %1 = icmp ult i32 %x, %y
> +  %2 = icmp ne i32 %y, 0
> +  %3 = or i1 %1, %2
> +  ret i1 %3
> +}
> +; CHECK-LABEL: @or_icmp1(
> +; CHECK: %[[cmp:.*]] = icmp ne i32 %y, 0
> +; CHECK: ret i1 %[[cmp]]
> +
> +define i1 @or_icmp2(i32 %x, i32 %y) {
> +  %1 = icmp uge i32 %x, %y
> +  %2 = icmp ne i32 %y, 0
> +  %3 = or i1 %1, %2
> +  ret i1 %3
> +}
> +; CHECK-LABEL: @or_icmp2(
> +; CHECK: ret i1 true
> +
> +define i1 @or_icmp3(i32 %x, i32 %y) {
> +  %1 = icmp uge i32 %x, %y
> +  %2 = icmp eq i32 %y, 0
> +  %3 = or i1 %1, %2
> +  ret i1 %3
> +}
> +; CHECK-LABEL: @or_icmp3(
> +; CHECK: %[[cmp:.*]] = icmp uge i32 %x, %y
> +; CHECK: ret i1 %[[cmp]]
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20141208/b075a288/attachment.html>


More information about the llvm-commits mailing list