[PATCH] D114361: [MachineCSE] Add an option to enable global CSE

wangpc via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 22 04:17:58 PST 2021


pcwang-thead updated this revision to Diff 388875.
pcwang-thead added a comment.

Amend commit message.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D114361/new/

https://reviews.llvm.org/D114361

Files:
  llvm/lib/CodeGen/MachineCSE.cpp
  llvm/test/CodeGen/RISCV/enable-global-cse.ll


Index: llvm/test/CodeGen/RISCV/enable-global-cse.ll
===================================================================
--- /dev/null
+++ llvm/test/CodeGen/RISCV/enable-global-cse.ll
@@ -0,0 +1,38 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
+; RUN: llc -mtriple=riscv64 --enable-global-cse -verify-machineinstrs < %s | FileCheck %s
+
+define i1 @foo(i64 %a, i64 %b) {
+; CHECK-LABEL: foo:
+; CHECK:       # %bb.0: # %entry
+; CHECK-NEXT:    mv a2, a0
+; CHECK-NEXT:    li a3, 2
+; CHECK-NEXT:    li a0, 1
+; CHECK-NEXT:    blt a2, a3, .LBB0_4
+; CHECK-NEXT:  # %bb.1: # %if.end
+; CHECK-NEXT:    li a2, 3
+; CHECK-NEXT:    beq a1, a2, .LBB0_3
+; CHECK-NEXT:  # %bb.2: # %if.end
+; CHECK-NEXT:    bne a1, a3, .LBB0_4
+; CHECK-NEXT:  .LBB0_3: # %bb3
+; CHECK-NEXT:    li a0, 0
+; CHECK-NEXT:  .LBB0_4: # %return
+; CHECK-NEXT:    ret
+entry:
+  %cmp = icmp slt i64 %a, 2
+  br i1 %cmp, label %return, label %if.end
+if.end:
+  switch i64 %b, label %return [
+    i64 1, label %bb1
+    i64 2, label %bb2
+    i64 3, label %bb3
+  ]
+bb1:
+  br label %return
+bb2:
+  br label %return
+bb3:
+  br label %return
+return:
+  %ret = phi i1 [ true, %bb1 ], [ false, %bb2 ], [ false, %bb3 ], [ true, %if.end ], [ true, %entry ]
+  ret i1 %ret
+}
Index: llvm/lib/CodeGen/MachineCSE.cpp
===================================================================
--- llvm/lib/CodeGen/MachineCSE.cpp
+++ llvm/lib/CodeGen/MachineCSE.cpp
@@ -51,6 +51,10 @@
 
 #define DEBUG_TYPE "machine-cse"
 
+static cl::opt<bool>
+    EnableGlobalCSE("enable-global-cse", cl::Hidden, cl::init(false),
+                    cl::desc("Enable CSE on the whole function."));
+
 STATISTIC(NumCoalesces, "Number of copies coalesced");
 STATISTIC(NumCSEs,      "Number of common subexpression eliminated");
 STATISTIC(NumPREs,      "Number of partial redundant expression"
@@ -454,7 +458,7 @@
   // Heuristics #1: Don't CSE "cheap" computation if the def is not local or in
   // an immediate predecessor. We don't want to increase register pressure and
   // end up causing other computation to be spilled.
-  if (TII->isAsCheapAsAMove(*MI)) {
+  if (!EnableGlobalCSE && TII->isAsCheapAsAMove(*MI)) {
     MachineBasicBlock *BB = MI->getParent();
     if (CSBB != BB && !CSBB->isSuccessor(BB))
       return false;
@@ -903,9 +907,14 @@
 }
 
 bool MachineCSE::runOnMachineFunction(MachineFunction &MF) {
-  if (skipFunction(MF.getFunction()))
+  Function &F = MF.getFunction();
+  if (skipFunction(F))
     return false;
 
+  // Enable global CSE when optimizing for size.
+  if (F.hasOptSize())
+    EnableGlobalCSE = true;
+
   TII = MF.getSubtarget().getInstrInfo();
   TRI = MF.getSubtarget().getRegisterInfo();
   MRI = &MF.getRegInfo();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D114361.388875.patch
Type: text/x-patch
Size: 2739 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211122/97f23023/attachment.bin>


More information about the llvm-commits mailing list