diff --git a/llvm/lib/CodeGen/MachineCSE.cpp b/llvm/lib/CodeGen/MachineCSE.cpp --- a/llvm/lib/CodeGen/MachineCSE.cpp +++ b/llvm/lib/CodeGen/MachineCSE.cpp @@ -51,6 +51,10 @@ #define DEBUG_TYPE "machine-cse" +static cl::opt + 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,9 @@ // 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)) { + // Enable global CSE when optimizing for size. + if (!EnableGlobalCSE && !MI->getMF()->getFunction().hasOptSize() && + TII->isAsCheapAsAMove(*MI)) { MachineBasicBlock *BB = MI->getParent(); if (CSBB != BB && !CSBB->isSuccessor(BB)) return false; diff --git a/llvm/test/CodeGen/RISCV/enable-global-cse.ll b/llvm/test/CodeGen/RISCV/enable-global-cse.ll new file mode 100644 --- /dev/null +++ b/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 +}