[PATCH] D74155: [X86CmovConversion] Make heuristic for optimized cmov depth more conservative (PR44539)

Nikita Popov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 6 12:24:38 PST 2020


nikic created this revision.
nikic added reviewers: andreadb, RKSimon, spatel, craig.topper.
Herald added subscribers: llvm-commits, hiraditya.
Herald added a project: LLVM.

Fix/workaround for https://bugs.llvm.org/show_bug.cgi?id=44539. As discussed there, this patch makes some overly optimistic assumptions, as it does not have access to actual branch weights.

This patch makes the computation of the depth of the optimized cmov more conservative, by assuming a distribution of 75/25 rather than 50/50 and placing the weights to get the more conservative result (larger depth). The fully conservative choice would be `std::max(TrueOpDepth, FalseOpDepth)`, but that would break at least one existing test (which may or may not be an issue in practice).


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D74155

Files:
  llvm/lib/Target/X86/X86CmovConversion.cpp


Index: llvm/lib/Target/X86/X86CmovConversion.cpp
===================================================================
--- llvm/lib/Target/X86/X86CmovConversion.cpp
+++ llvm/lib/Target/X86/X86CmovConversion.cpp
@@ -364,12 +364,13 @@
 /// \param TrueOpDepth depth cost of CMOV true value operand.
 /// \param FalseOpDepth depth cost of CMOV false value operand.
 static unsigned getDepthOfOptCmov(unsigned TrueOpDepth, unsigned FalseOpDepth) {
-  //===--------------------------------------------------------------------===//
-  // With no info about branch weight, we assume 50% for each value operand.
-  // Thus, depth of optimized CMOV instruction is the rounded up average of
-  // its True-Operand-Value-Depth and False-Operand-Value-Depth.
-  //===--------------------------------------------------------------------===//
-  return (TrueOpDepth + FalseOpDepth + 1) / 2;
+  // The depth of the result after branch conversion is
+  // TrueOpDepth * TrueOpProbability + FalseOpDepth * FalseOpProbability.
+  // As we have no info about branch weight, we assume 75% for one and 25% for
+  // the other, and pick the result with the largest resulting depth.
+  return std::max(
+      divideCeil(TrueOpDepth * 3 + FalseOpDepth, 4),
+      divideCeil(FalseOpDepth * 3 + TrueOpDepth, 4));
 }
 
 bool X86CmovConverterPass::checkForProfitableCmovCandidates(


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D74155.242978.patch
Type: text/x-patch
Size: 1353 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200206/bb6242fb/attachment.bin>


More information about the llvm-commits mailing list