llvm.org GIT mirror llvm / fd8b1fd
Remove an orthogonal transformation of the selection condition from my most recent submission. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@45169 91177308-0d34-0410-b5e6-96231b3b80d8 Christopher Lamb 11 years ago
2 changed file(s) with 3 addition(s) and 40 deletion(s). Raw diff Collapse all Expand all
21302130 }
21312131 }
21322132
2133 // add (select (icmp 0 (sub m A)) X Y) A ->
2134 // add (select (icmp A m) X Y) A
2135 //
21362133 // add (select X 0 (sub n A)) A ->
21372134 // select X A n
21382135 {
21462143 Value *TV = SI->getTrueValue();
21472144 Value *FV = SI->getFalseValue();
21482145 Value *A;
2149
2150 // Can we fold the add into the argument of the compare?
2151 Value *Cond = SI->getCondition();
2152 if (ICmpInst *IC = dyn_cast(Cond)) {
2153 Value *ICOp0 = IC->getOperand(0);
2154 Value *ICOp1 = IC->getOperand(1);
2155 ConstantInt *C3, *C4;
2156
2157 // Check both arguments of the compare for a matching subtract.
2158 if (match(ICOp0, m_ConstantInt(C3)) && C3->getValue() == 0 &&
2159 match(ICOp1, m_Sub(m_ConstantInt(C4), m_Value(A))) &&
2160 A == Other) {
2161 // We managed to fold the add into the RHS of the select condition.
2162 Cond = new ICmpInst(IC->getPredicate(), A, C4, "asis", SI);
2163 } else if (match(ICOp1, m_ConstantInt(C3)) && C3->getValue() == 0 &&
2164 match(ICOp0, m_Sub(m_ConstantInt(C4), m_Value(A))) &&
2165 A == Other) {
2166 // We managed to fold the add into the LHS of the select condition.
2167 Cond = new ICmpInst(IC->getPredicate(), C4, A, "asis", SI);
2168 }
2169 }
21702146
21712147 // Can we fold the add into the argument of the select?
21722148 // We check both true and false select arguments for a matching subtract.
21762152 A == Other) {
21772153 // We managed to fold the add into the true select value,
21782154 // picking up a simplified condition, if available.
2179 return new SelectInst(Cond, C2, A, "adselsub");
2155 return new SelectInst(SI->getCondition(), C2, A);
21802156 } else if (match(TV, m_ConstantInt(C1)) && C1->getValue() == 0 &&
21812157 match(FV, m_Sub(m_ConstantInt(C2), m_Value(A))) &&
21822158 A == Other) {
21832159 // We managed to fold the add into the false select value,
21842160 // picking up a simplified condition, if available.
2185 return new SelectInst(Cond, A, C2, "adselsub");
2186 } else if (Cond != SI->getCondition()) {
2187 // We only managed to fold the add into the select condition.
2188 SI->setOperand(0, Cond);
2189 Changed = true;
2161 return new SelectInst(SI->getCondition(), A, C2);
21902162 }
21912163 }
21922164 }
None ; RUN: llvm-as < %s | opt -instcombine | llvm-dis | not grep {sub}
0 ; RUN: llvm-as < %s | opt -instcombine | llvm-dis | grep {add} | count 1
11
22 define i32 @foo(i32 %a) {
33 entry:
1818 ret i32 %tmp12
1919 }
2020
21 define i32 @baz(i32 %a) {
22 entry:
23 %tmp15 = sub i32 99, %a ; [#uses=1]
24 %tmp16 = icmp slt i32 %tmp15, 0 ; [#uses=1]
25 %smax = select i1 %tmp16, i32 0, i32 42 ; [#uses=1]
26 %tmp12 = add i32 %smax, %a ; [#uses=1]
27 ret i32 %tmp12
28 }
29
3021 define i32 @fun(i32 %a) {
3122 entry:
3223 %tmp15 = sub i32 99, %a ; [#uses=1]