[PATCH] D40892: Add early out to O(n^2) switch analysis in switch-to-select conversion

Andrew Scheidecker via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 6 05:05:26 PST 2017


AndrewScheidecker created this revision.
Herald added subscribers: sunfish, aheejin, dschuff, jfb.

This adds an early out to the switch->select conversion analysis as soon as we've seen more than two unique results or cases. The conversion ultimately aborts in this case, but currently only does so after fully analyzing the switch, which takes O(NumCases^2) time if the switch is in a form that doesn't early out for other reasons.

In my WebAssembly VM that uses LLVM to generate machine code, I was running into this on a test case with a large, but simple, switch from the WebAssembly reference interpreter test suite: https://github.com/WebAssembly/spec/blob/master/test/core/br_table.wast#L110

To find the bottleneck, I increased the number of cases in the switch statement further, which caused it to spend 10 seconds in LLVM optimization passes. Adding this early out reduced that to tens of milliseconds.


Repository:
  rL LLVM

https://reviews.llvm.org/D40892

Files:
  lib/Transforms/Utils/SimplifyCFG.cpp


Index: lib/Transforms/Utils/SimplifyCFG.cpp
===================================================================
--- lib/Transforms/Utils/SimplifyCFG.cpp
+++ lib/Transforms/Utils/SimplifyCFG.cpp
@@ -4720,6 +4720,15 @@
       return false;
     MapCaseToResult(CaseVal, UniqueResults, Results.begin()->second);
 
+    // No more than two unique results can be selected between.
+    if (UniqueResults.size() > 2)
+      return false;
+
+    // No result may have more than one case that selects it.
+    for (const auto &RI : UniqueResults)
+      if (RI.second.size() > 1)
+        return false;
+
     // Check the PHI consistency.
     if (!PHI)
       PHI = Results[0].first;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D40892.125709.patch
Type: text/x-patch
Size: 678 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20171206/b8286429/attachment.bin>


More information about the llvm-commits mailing list