llvm.org GIT mirror llvm / 103e1a3
Implement review feedback, including additional transforms (icmp slt (sub A B) 1) -> (icmp sle A B) icmp sgt (sub A B) -1) -> (icmp sge A B) and add testcase. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@45256 91177308-0d34-0410-b5e6-96231b3b80d8 Christopher Lamb 11 years ago
2 changed file(s) with 40 addition(s) and 17 deletion(s). Raw diff Collapse all Expand all
47844784
47854785 if (isa(Op1)) // X icmp undef -> undef
47864786 return ReplaceInstUsesWith(I, UndefValue::get(Type::Int1Ty));
4787
4788 // (icmp cond (sub m A) 0) ->
4789 // (icmp cond m A)
4790 {
4791 ConstantInt *C1, *C2;
4792 Value *A;
4793 // Check both arguments of the compare for a matching subtract.
4794 if (match(Op0, m_ConstantInt(C1)) && C1->getValue() == 0 &&
4795 match(Op1, m_Sub(m_ConstantInt(C2), m_Value(A)))) {
4796 // We managed to fold the add into the RHS of the select condition.
4797 return new ICmpInst(I.getPredicate(), A, C2);
4798 } else if (match(Op1, m_ConstantInt(C1)) && C1->getValue() == 0 &&
4799 match(Op0, m_Sub(m_ConstantInt(C2), m_Value(A)))) {
4800 // We managed to fold the add into the LHS of the select condition.
4801 return new ICmpInst(I.getPredicate(), C2, A);
4802 }
4803 }
48044787
48054788 // icmp , - Global/Stack value
48064789 // addresses never equal each other! We already know that Op0 != Op1.
48494832 // See if we are doing a comparison between a constant and an instruction that
48504833 // can be folded into the comparison.
48514834 if (ConstantInt *CI = dyn_cast(Op1)) {
4835 Value *A, *B;
4836
4837 // (icmp cond (sub A B) 0) -> (icmp cond A B)
4838 if (CI->isNullValue() && match(Op0, m_Sub(m_Value(A), m_Value(B))))
4839 return new ICmpInst(I.getPredicate(), A, B);
4840
48524841 switch (I.getPredicate()) {
48534842 default: break;
48544843 case ICmpInst::ICMP_ULT: // A FALSE
48724861 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
48734862 if (isMinValuePlusOne(CI,true)) // A A == MIN
48744863 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, SubOne(CI));
4864
4865 // (icmp slt (sub A B) 1) -> (icmp sle A B)
4866 if (CI->isOne() && match(Op0, m_Sub(m_Value(A), m_Value(B))))
4867 return new ICmpInst(ICmpInst::ICMP_SLE, A, B);
48754868 break;
48764869
48774870 case ICmpInst::ICMP_UGT:
48954888 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
48964889 if (isMaxValueMinusOne(CI, true)) // A >s MAX-1 -> A == MAX
48974890 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, AddOne(CI));
4891
4892 // (icmp sgt (sub A B) -1) -> (icmp sge A B)
4893 if (CI->getValue().getSExtValue() == -1 &&
4894 match(Op0, m_Sub(m_Value(A), m_Value(B))))
4895 return new ICmpInst(ICmpInst::ICMP_SGE, A, B);
48984896 break;
48994897
49004898 case ICmpInst::ICMP_ULE:
0 ; RUN: llvm-as < %s | opt -instcombine | llvm-dis | not grep {sub}
1
2 define i32 @foo(i32 %a) {
3 entry:
4 %tmp2 = sub i32 99, %a ; [#uses=1]
5 %tmp3 = icmp sgt i32 %tmp2, -1 ; [#uses=1]
6 %retval = select i1 %tmp3, i32 %a, i32 0 ; [#uses=1]
7 ret i32 %retval
8 }
9
10 define i32 @bar(i32 %a) {
11 entry:
12 %tmp2 = sub i32 99, %a ; [#uses=1]
13 %tmp3 = icmp sge i32 %tmp2, 0; [#uses=1]
14 %retval = select i1 %tmp3, i32 %a, i32 0 ; [#uses=1]
15 ret i32 %retval
16 }
17
18 define i32 @baz(i32 %a) {
19 entry:
20 %tmp2 = sub i32 99, %a ; [#uses=1]
21 %tmp3 = icmp slt i32 %tmp2, 1 ; [#uses=1]
22 %retval = select i1 %tmp3, i32 %a, i32 0 ; [#uses=1]
23 ret i32 %retval
24 }