[PATCH] patch for missed constantFold optimization in InstCombine

Budukh, Tejas via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 11 16:27:20 PST 2015


llvm::ConstantFoldCompareInstOperands misses an optimization when it tries to constantFold a compare instruction containing addrspacecast instead of combination of ptrtoint and inttoptr. Adding the patched code snippet in the code would enable the pass to handle addrspacecast and constantFold the missing cases.
Attached is the .ll file for the reproducer. Let me know if there are any suggestions/comments or I can commit the patch.

Patched code :
Index: ConstantFolding.cpp
===================================================================
diff --git a/llvm/trunk/lib/Analysis/ConstantFolding.cpp b/llvm/trunk/lib/Analysis/ConstantFolding.cpp
--- a/llvm/trunk/lib/Analysis/ConstantFolding.cpp            (revision 252222)
+++ b/llvm/trunk/lib/Analysis/ConstantFolding.cpp        (working copy)
@@ -1137,6 +1137,12 @@
           return ConstantFoldCompareInstOperands(Predicate, C, Null, DL, TLI);
         }
       }
+
+      if (CE0->getOpcode() == Instruction::AddrSpaceCast) {
+          Constant *C = CE0->getOperand(0);
+          Constant *Null = Constant::getNullValue(C->getType());
+          return ConstantFoldCompareInstOperands(Predicate, C, Null, TD, TLI);
+      }
     }

     if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(Ops1)) {


Thanks,
Tejas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20151112/1772c32f/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ConstantFolding.cpp.patch
Type: application/octet-stream
Size: 799 bytes
Desc: ConstantFolding.cpp.patch
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20151112/1772c32f/attachment.obj>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: new_Output.txt
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20151112/1772c32f/attachment.txt>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: old_Output.txt
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20151112/1772c32f/attachment-0001.txt>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: instCombineAddrspaceCast.ll
Type: application/octet-stream
Size: 1174 bytes
Desc: instCombineAddrspaceCast.ll
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20151112/1772c32f/attachment-0001.obj>


More information about the llvm-commits mailing list