OLD | NEW |
1 //===- InstCombineCompares.cpp --------------------------------------------===// | 1 //===- InstCombineCompares.cpp --------------------------------------------===// |
2 // | 2 // |
3 // The LLVM Compiler Infrastructure | 3 // The LLVM Compiler Infrastructure |
4 // | 4 // |
5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
7 // | 7 // |
8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
9 // | 9 // |
10 // This file implements the visitICmp and visitFCmp functions. | 10 // This file implements the visitICmp and visitFCmp functions. |
(...skipping 1924 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1935 // addition in wider type, and explicitly checks for overflow using | 1935 // addition in wider type, and explicitly checks for overflow using |
1936 // comparisons against INT_MIN and INT_MAX. Simplify this by using the | 1936 // comparisons against INT_MIN and INT_MAX. Simplify this by using the |
1937 // sadd_with_overflow intrinsic. | 1937 // sadd_with_overflow intrinsic. |
1938 // | 1938 // |
1939 // TODO: This could probably be generalized to handle other overflow-safe | 1939 // TODO: This could probably be generalized to handle other overflow-safe |
1940 // operations if we worked out the formulas to compute the appropriate | 1940 // operations if we worked out the formulas to compute the appropriate |
1941 // magic constants. | 1941 // magic constants. |
1942 // | 1942 // |
1943 // sum = a + b | 1943 // sum = a + b |
1944 // if (sum+128 >u 255) ... -> llvm.sadd.with.overflow.i8 | 1944 // if (sum+128 >u 255) ... -> llvm.sadd.with.overflow.i8 |
1945 { | 1945 // @LOCALMOD-BEGIN |
1946 ConstantInt *CI2; // I = icmp ugt (add (add A, B), CI2), CI | 1946 // This is disabled for PNaCl, because we don't support the |
1947 if (I.getPredicate() == ICmpInst::ICMP_UGT && | 1947 // with.overflow intrinsics in PNaCl's stable ABI. |
1948 match(Op0, m_Add(m_Add(m_Value(A), m_Value(B)), m_ConstantInt(CI2)))) | 1948 if (0) { |
1949 if (Instruction *Res = ProcessUGT_ADDCST_ADD(I, A, B, CI2, CI, *this)) | 1949 ConstantInt *CI2; // I = icmp ugt (add (add A, B), CI2), CI |
1950 return Res; | 1950 if (I.getPredicate() == ICmpInst::ICMP_UGT && |
| 1951 match(Op0, m_Add(m_Add(m_Value(A), m_Value(B)), m_ConstantInt(CI2)))) |
| 1952 if (Instruction *Res = ProcessUGT_ADDCST_ADD(I, A, B, CI2, CI, *this)) |
| 1953 return Res; |
1951 } | 1954 } |
| 1955 // @LOCALMOD-END |
1952 | 1956 |
1953 // (icmp ne/eq (sub A B) 0) -> (icmp ne/eq A, B) | 1957 // (icmp ne/eq (sub A B) 0) -> (icmp ne/eq A, B) |
1954 if (I.isEquality() && CI->isZero() && | 1958 if (I.isEquality() && CI->isZero() && |
1955 match(Op0, m_Sub(m_Value(A), m_Value(B)))) { | 1959 match(Op0, m_Sub(m_Value(A), m_Value(B)))) { |
1956 // (icmp cond A B) if cond is equality | 1960 // (icmp cond A B) if cond is equality |
1957 return new ICmpInst(I.getPredicate(), A, B); | 1961 return new ICmpInst(I.getPredicate(), A, B); |
1958 } | 1962 } |
1959 | 1963 |
1960 // If we have an icmp le or icmp ge instruction, turn it into the | 1964 // If we have an icmp le or icmp ge instruction, turn it into the |
1961 // appropriate icmp lt or icmp gt instruction. This allows us to rely on | 1965 // appropriate icmp lt or icmp gt instruction. This allows us to rely on |
(...skipping 585 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2547 { Value *A, *B; | 2551 { Value *A, *B; |
2548 // ~x < ~y --> y < x | 2552 // ~x < ~y --> y < x |
2549 // ~x < cst --> ~cst < x | 2553 // ~x < cst --> ~cst < x |
2550 if (match(Op0, m_Not(m_Value(A)))) { | 2554 if (match(Op0, m_Not(m_Value(A)))) { |
2551 if (match(Op1, m_Not(m_Value(B)))) | 2555 if (match(Op1, m_Not(m_Value(B)))) |
2552 return new ICmpInst(I.getPredicate(), B, A); | 2556 return new ICmpInst(I.getPredicate(), B, A); |
2553 if (ConstantInt *RHSC = dyn_cast<ConstantInt>(Op1)) | 2557 if (ConstantInt *RHSC = dyn_cast<ConstantInt>(Op1)) |
2554 return new ICmpInst(I.getPredicate(), ConstantExpr::getNot(RHSC), A); | 2558 return new ICmpInst(I.getPredicate(), ConstantExpr::getNot(RHSC), A); |
2555 } | 2559 } |
2556 | 2560 |
2557 // (a+b) <u a --> llvm.uadd.with.overflow. | 2561 // @LOCALMOD-BEGIN |
2558 // (a+b) <u b --> llvm.uadd.with.overflow. | 2562 // This is disabled for PNaCl, because we don't support the |
2559 if (I.getPredicate() == ICmpInst::ICMP_ULT && | 2563 // with.overflow intrinsics in PNaCl's stable ABI. |
2560 match(Op0, m_Add(m_Value(A), m_Value(B))) && | 2564 if (0) { |
2561 (Op1 == A || Op1 == B)) | 2565 // (a+b) <u a --> llvm.uadd.with.overflow. |
2562 if (Instruction *R = ProcessUAddIdiom(I, Op0, *this)) | 2566 // (a+b) <u b --> llvm.uadd.with.overflow. |
2563 return R; | 2567 if (I.getPredicate() == ICmpInst::ICMP_ULT && |
| 2568 match(Op0, m_Add(m_Value(A), m_Value(B))) && |
| 2569 (Op1 == A || Op1 == B)) |
| 2570 if (Instruction *R = ProcessUAddIdiom(I, Op0, *this)) |
| 2571 return R; |
2564 | 2572 |
2565 // a >u (a+b) --> llvm.uadd.with.overflow. | 2573 // a >u (a+b) --> llvm.uadd.with.overflow. |
2566 // b >u (a+b) --> llvm.uadd.with.overflow. | 2574 // b >u (a+b) --> llvm.uadd.with.overflow. |
2567 if (I.getPredicate() == ICmpInst::ICMP_UGT && | 2575 if (I.getPredicate() == ICmpInst::ICMP_UGT && |
2568 match(Op1, m_Add(m_Value(A), m_Value(B))) && | 2576 match(Op1, m_Add(m_Value(A), m_Value(B))) && |
2569 (Op0 == A || Op0 == B)) | 2577 (Op0 == A || Op0 == B)) |
2570 if (Instruction *R = ProcessUAddIdiom(I, Op1, *this)) | 2578 if (Instruction *R = ProcessUAddIdiom(I, Op1, *this)) |
2571 return R; | 2579 return R; |
| 2580 } |
| 2581 // @LOCALMOD-END |
2572 } | 2582 } |
2573 | 2583 |
2574 if (I.isEquality()) { | 2584 if (I.isEquality()) { |
2575 Value *A, *B, *C, *D; | 2585 Value *A, *B, *C, *D; |
2576 | 2586 |
2577 if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) { | 2587 if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) { |
2578 if (A == Op1 || B == Op1) { // (A^B) == A -> B == 0 | 2588 if (A == Op1 || B == Op1) { // (A^B) == A -> B == 0 |
2579 Value *OtherVal = A == Op1 ? B : A; | 2589 Value *OtherVal = A == Op1 ? B : A; |
2580 return new ICmpInst(I.getPredicate(), OtherVal, | 2590 return new ICmpInst(I.getPredicate(), OtherVal, |
2581 Constant::getNullValue(A->getType())); | 2591 Constant::getNullValue(A->getType())); |
(...skipping 501 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3083 | 3093 |
3084 // fcmp (fpext x), (fpext y) -> fcmp x, y | 3094 // fcmp (fpext x), (fpext y) -> fcmp x, y |
3085 if (FPExtInst *LHSExt = dyn_cast<FPExtInst>(Op0)) | 3095 if (FPExtInst *LHSExt = dyn_cast<FPExtInst>(Op0)) |
3086 if (FPExtInst *RHSExt = dyn_cast<FPExtInst>(Op1)) | 3096 if (FPExtInst *RHSExt = dyn_cast<FPExtInst>(Op1)) |
3087 if (LHSExt->getSrcTy() == RHSExt->getSrcTy()) | 3097 if (LHSExt->getSrcTy() == RHSExt->getSrcTy()) |
3088 return new FCmpInst(I.getPredicate(), LHSExt->getOperand(0), | 3098 return new FCmpInst(I.getPredicate(), LHSExt->getOperand(0), |
3089 RHSExt->getOperand(0)); | 3099 RHSExt->getOperand(0)); |
3090 | 3100 |
3091 return Changed ? &I : 0; | 3101 return Changed ? &I : 0; |
3092 } | 3102 } |
OLD | NEW |