Tried implementing SPF_ABS and instcombine for ABS(ABS(X))
I left ABS(-X) -> ABS(X) from TODO comment because it was not
reducing instructions count. For calculating ABS(X), it generate
instruction to negate X for select instruction.
Differential D3658
Added instcombine for 'ABS(ABS(X)) -> ABS(X)' dinesh.d on May 7 2014, 12:53 PM. Authored by
Details Tried implementing SPF_ABS and instcombine for ABS(ABS(X)) I left ABS(-X) -> ABS(X) from TODO comment because it was not
Diff Detail Event Timeline
Comment Actions updated code to handle following patterns [Thanks for pointing out] // (X > 0) ? X : -X // (X < 0) ? -X : X // (X > 1) ? -X : X Comment Actions corrected one typo in comments. (x > 1)? -x : x whereas I meant (x < 1)? -x : x Comment Actions One potential bug, and a couple of small comments, but otherwise looks fine. (Note: I don't have commit rights so this is not an approval to commit.)
Comment Actions Thanks Philip. I appreciate your effort. I do have one request though. As you are also new to llvm, it will be great if you can avoid
Comment Actions Updated patch to handle both ABS(X) and NEG(ABS(X)) and tests for all possible combinations or ABS(ABS) and NABS(NABS) Comment Actions With the last round of changes, this looks fine to me. I'm unclear whether this is covering all useful patterns (i.e. why don't we need GTE, or LTE pattens?), but what's here seems correct and useful. Reminder: You will need to find another reviewer. I do not have commit rights and can not approve your change. Comment Actions Thanks for review, Philip. InstCombine code changes >= and <= to corrosponding > and < before this code get hit. > cat test.cpp int test(int x) { int y = (x >= 0) ? x : -x; int z = (y >= 0) ? y : -y; return z; } > clang -O0 -S -emit-llvm test.cpp > opt -mem2reg -instcombine -debug test.ll Let me know if you have any doubts. Comment Actions The updated patch LGTM. Thanks! We don't have to match patterns for sle and uge as instcombine canonicalizes them into slt and ugt. That makes code like this simpler. |
Personally, I find the code with these extracted much harder to read. I would prefer you not extract these, but don't have a strong preference. i.e. feel free to ignore