[PATCH] D40721: [InstSimplify] Run constant folding if no other simplifications were possible

Igor Laevsky via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 1 06:35:16 PST 2017


igor-laevsky created this revision.

This came in as an unexpected requirement for the https://reviews.llvm.org/D40650.

Before this change we were doing constant folding only for the unrecognised instructions. For the recognised ones we were calling specific constant fold functions from their respective simplification functions. However this specific constant folding functions are slightly weaker that the general `ConstantFoldInstruction` because they don't fold operands. Turns out that for the insertelement we need full power of the `ConstantFoldInstruction` or we are going to fail some of the lit tests.

It felt like running constant folding after all InstSimplify transformations is more direct solution rather then trying to include constant folding into each of the simplification functions. Although this mostly affects insertelement case but we can improve later simply by removing constant folding from the simplification functions. This will give us better folding with less effort.


https://reviews.llvm.org/D40721

Files:
  lib/Analysis/InstructionSimplify.cpp


Index: lib/Analysis/InstructionSimplify.cpp
===================================================================
--- lib/Analysis/InstructionSimplify.cpp
+++ lib/Analysis/InstructionSimplify.cpp
@@ -4574,12 +4574,9 @@
 Value *llvm::SimplifyInstruction(Instruction *I, const SimplifyQuery &SQ,
                                  OptimizationRemarkEmitter *ORE) {
   const SimplifyQuery Q = SQ.CxtI ? SQ : SQ.getWithInstruction(I);
-  Value *Result;
+  Value *Result = nullptr;
 
   switch (I->getOpcode()) {
-  default:
-    Result = ConstantFoldInstruction(I, Q.DL, Q.TLI);
-    break;
   case Instruction::FAdd:
     Result = SimplifyFAddInst(I->getOperand(0), I->getOperand(1),
                               I->getFastMathFlags(), Q);
@@ -4712,6 +4709,10 @@
     break;
   }
 
+  // Try to do constant folding if above simplifications had failed
+  if (!Result)
+    Result = ConstantFoldInstruction(I, Q.DL, Q.TLI);
+
   // In general, it is possible for computeKnownBits to determine all bits in a
   // value even when the operands are not all constants.
   if (!Result && I->getType()->isIntOrIntVectorTy()) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D40721.125126.patch
Type: text/x-patch
Size: 1115 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20171201/12d4928a/attachment.bin>


More information about the llvm-commits mailing list