ConstantFoldInstruction: avoid wasted calls to ConstantFoldConstantExpression
authorFiona Glaser <escha@apple.com>
Sun, 13 Mar 2016 05:36:15 +0000 (05:36 +0000)
committerFiona Glaser <escha@apple.com>
Sun, 13 Mar 2016 05:36:15 +0000 (05:36 +0000)
Check to see if all operands are constant before calling simplify on them
so that we don't perform wasted simplifications.

llvm-svn: 263374

llvm/lib/Analysis/ConstantFolding.cpp

index 45988a0..9e21a7e 100644 (file)
@@ -983,12 +983,12 @@ Constant *llvm::ConstantFoldInstruction(Instruction *I, const DataLayout &DL,
 
   // Scan the operand list, checking to see if they are all constants, if so,
   // hand off to ConstantFoldInstOperandsImpl.
-  SmallVector<Constant*, 8> Ops;
-  for (User::op_iterator i = I->op_begin(), e = I->op_end(); i != e; ++i) {
-    Constant *Op = dyn_cast<Constant>(*i);
-    if (!Op)
-      return nullptr;  // All operands not constant!
+  if (!all_of(I->operands(), [](Use &U) { return isa<Constant>(U); }))
+    return nullptr;
 
+  SmallVector<Constant *, 8> Ops;
+  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))
       Op = ConstantFoldConstantExpression(NewCE, DL, TLI);