[PATCH] Added instruction combine to transform few more negative values addition to subtraction

Nick Lewycky nlewycky at gmail.com
Wed Jun 18 22:58:52 PDT 2014


Sadness. Did you know that gcc gets this even at -O0? Not this pattern mind you, but the original testcase. It's that simple, at least before we transform to xor(or) form. GCC doesn't get it in xor(or) form either.

I looked for a better way to address this optimizer deficiency and didn't find one. The one thing I didn't check is whether we could get it by reordering our optimizations inside instcombine and do something else before it gets into xor(or) form. Even if that did fix the testcase in PR14365 it would still leave the case where someone actually does write "((a|~c) ^ c) + (a+1)".

================
Comment at: lib/Transforms/InstCombine/InstCombineAddSub.cpp:928
@@ +927,3 @@
+
+  // This function creates 2 instructions to replace ADD, we need atleast one of
+  // LHS or RHS to have one use to ensure benefit in transform.
----------------
"atleast" --> "at least"

================
Comment at: lib/Transforms/InstCombine/InstCombineAddSub.cpp:951
@@ +950,3 @@
+    if (match(X, m_Xor(m_Value(Y), m_APInt(C1)))) {
+      if (match(Y, m_Or(m_Value(Z), m_APInt(C2))) && (*C2 == ~(*C1))) {
+        Value *NewAnd = Builder->CreateAnd(Z, *C1);
----------------
Optional: *C2 == ~(*C1) might be more efficient as !C2->intersects(*C1). It isn't, but in theory we could improve intersects to not compute an intermediate value (ie., it doesn't need to look at all the bits once it finds a pair are set in both).

================
Comment at: test/Transforms/InstCombine/add2.ll:142
@@ +141,1 @@
+}
\ No newline at end of file

----------------
Please make sure there's a newline at the end of the file.

http://reviews.llvm.org/D3733






More information about the llvm-commits mailing list