[PATCH] D18155: Instcombine: try to avoid wasted work in ConstantFold

escha via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 15 17:49:01 PDT 2016


escha updated this revision to Diff 50787.
escha added a comment.

Updated diff with some context.


Repository:
  rL LLVM

http://reviews.llvm.org/D18155

Files:
  include/llvm/Analysis/ConstantFolding.h
  lib/Analysis/ConstantFolding.cpp
  lib/Transforms/InstCombine/InstructionCombining.cpp

Index: lib/Transforms/InstCombine/InstructionCombining.cpp
===================================================================
--- lib/Transforms/InstCombine/InstructionCombining.cpp
+++ lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -2937,18 +2937,6 @@
         continue;
       }
 
-      // ConstantProp instruction if trivially constant.
-      if (!Inst->use_empty() &&
-          (Inst->getNumOperands() == 0 || isa<Constant>(Inst->getOperand(0))))
-        if (Constant *C = ConstantFoldInstruction(Inst, DL, TLI)) {
-          DEBUG(dbgs() << "IC: ConstFold to: " << *C << " from: "
-                       << *Inst << '\n');
-          Inst->replaceAllUsesWith(C);
-          ++NumConstProp;
-          Inst->eraseFromParent();
-          continue;
-        }
-
       // See if we can constant fold its operands.
       for (User::op_iterator i = Inst->op_begin(), e = Inst->op_end(); i != e;
            ++i) {
@@ -2968,6 +2956,18 @@
         }
       }
 
+      // ConstantProp instruction if trivially constant.
+      if (!Inst->use_empty() &&
+          (Inst->getNumOperands() == 0 || isa<Constant>(Inst->getOperand(0))))
+        if (Constant *C = ConstantFoldInstruction(Inst, DL, TLI, false  )) {
+          DEBUG(dbgs() << "IC: ConstFold to: " << *C << " from: "
+                       << *Inst << '\n');
+          Inst->replaceAllUsesWith(C);
+          ++NumConstProp;
+          Inst->eraseFromParent();
+          continue;
+        }
+
       InstrsForInstCombineWorklist.push_back(Inst);
     }
 
Index: lib/Analysis/ConstantFolding.cpp
===================================================================
--- lib/Analysis/ConstantFolding.cpp
+++ lib/Analysis/ConstantFolding.cpp
@@ -949,7 +949,8 @@
 //===----------------------------------------------------------------------===//
 
 Constant *llvm::ConstantFoldInstruction(Instruction *I, const DataLayout &DL,
-                                        const TargetLibraryInfo *TLI) {
+                                        const TargetLibraryInfo *TLI,
+                                        bool FoldOperands) {
   // Handle PHI nodes quickly here...
   if (PHINode *PN = dyn_cast<PHINode>(I)) {
     Constant *CommonValue = nullptr;
@@ -966,7 +967,8 @@
       if (!C)
         return nullptr;
       // Fold the PHI's operands.
-      if (ConstantExpr *NewC = dyn_cast<ConstantExpr>(C))
+      ConstantExpr *NewC = dyn_cast<ConstantExpr>(C);
+      if (NewC && FoldOperands)
         C = ConstantFoldConstantExpression(NewC, DL, TLI);
       // If the incoming value is a different constant to
       // the one we saw previously, then give up.
@@ -989,7 +991,8 @@
   for (User::op_iterator i = I->op_begin(), e = I->op_end(); i != e; ++i) {
     Constant *Op = cast<Constant>(*i);
     // Fold the Instruction's operands.
-    if (ConstantExpr *NewCE = dyn_cast<ConstantExpr>(Op))
+    ConstantExpr *NewCE = dyn_cast<ConstantExpr>(Op);
+    if (NewCE && FoldOperands)
       Op = ConstantFoldConstantExpression(NewCE, DL, TLI);
 
     Ops.push_back(Op);
Index: include/llvm/Analysis/ConstantFolding.h
===================================================================
--- include/llvm/Analysis/ConstantFolding.h
+++ include/llvm/Analysis/ConstantFolding.h
@@ -35,8 +35,11 @@
 /// Note that this fails if not all of the operands are constant.  Otherwise,
 /// this function can only fail when attempting to fold instructions like loads
 /// and stores, which have no constant expression form.
+/// Only attempt to constant-fold ConstantExpr operands if FoldOperands
+/// is set to true; set this to false if they are already known to be folded.
 Constant *ConstantFoldInstruction(Instruction *I, const DataLayout &DL,
-                                  const TargetLibraryInfo *TLI = nullptr);
+                                  const TargetLibraryInfo *TLI = nullptr,
+                                  bool FoldOperands = true);
 
 /// ConstantFoldConstantExpression - Attempt to fold the constant expression
 /// using the specified DataLayout.  If successful, the constant result is


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D18155.50787.patch
Type: text/x-patch
Size: 4081 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160316/dd0eeee6/attachment.bin>


More information about the llvm-commits mailing list