[PATCH] D30108: Refactor code computing switch instruction cost. NFC.

Easwaran Raman via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 17 14:31:54 PST 2017


eraman created this revision.

https://reviews.llvm.org/D30108

Files:
  lib/Analysis/InlineCost.cpp


Index: lib/Analysis/InlineCost.cpp
===================================================================
--- lib/Analysis/InlineCost.cpp
+++ lib/Analysis/InlineCost.cpp
@@ -1013,26 +1013,33 @@
   // branches.
   if (isa<ConstantInt>(SI.getCondition()))
     return true;
+
+  // Lambda to compute the cost of switch instruction.
+  auto SwitchInstCost = [](SwitchInst &SI) {
+    // We need to compute a cost proportional to the number of
+    // distinct successor blocks. This fan-out in the CFG cannot be represented
+    // for free even if we can represent the core switch as a jumptable that
+    // takes a single instruction.
+    //
+    // NB: We convert large switches which are just used to initialize large phi
+    // nodes to lookup tables instead in simplify-cfg, so this shouldn't prevent
+    // inlining those. It will prevent inlining in cases where the optimization
+    // does not (yet) fire.
+    SmallPtrSet<BasicBlock *, 8> SuccessorBlocks;
+    SuccessorBlocks.insert(SI.getDefaultDest());
+    for (auto I = SI.case_begin(), E = SI.case_end(); I != E; ++I)
+      SuccessorBlocks.insert(I.getCaseSuccessor());
+
+    // Add cost corresponding to the number of distinct destinations. The first
+    // we model as free because of fallthrough.
+    return (SuccessorBlocks.size() - 1) * InlineConstants::InstrCost;
+  };
+
   if (Value *V = SimplifiedValues.lookup(SI.getCondition()))
     if (isa<ConstantInt>(V))
       return true;
 
-  // Otherwise, we need to accumulate a cost proportional to the number of
-  // distinct successor blocks. This fan-out in the CFG cannot be represented
-  // for free even if we can represent the core switch as a jumptable that
-  // takes a single instruction.
-  //
-  // NB: We convert large switches which are just used to initialize large phi
-  // nodes to lookup tables instead in simplify-cfg, so this shouldn't prevent
-  // inlining those. It will prevent inlining in cases where the optimization
-  // does not (yet) fire.
-  SmallPtrSet<BasicBlock *, 8> SuccessorBlocks;
-  SuccessorBlocks.insert(SI.getDefaultDest());
-  for (auto I = SI.case_begin(), E = SI.case_end(); I != E; ++I)
-    SuccessorBlocks.insert(I.getCaseSuccessor());
-  // Add cost corresponding to the number of distinct destinations. The first
-  // we model as free because of fallthrough.
-  Cost += (SuccessorBlocks.size() - 1) * InlineConstants::InstrCost;
+  Cost += SwitchInstCost(SI);
   return false;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D30108.88934.patch
Type: text/x-patch
Size: 2461 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170217/baec5f8b/attachment.bin>


More information about the llvm-commits mailing list