diff --git a/llvm/lib/CodeGen/Spill2Reg.cpp b/llvm/lib/CodeGen/Spill2Reg.cpp --- a/llvm/lib/CodeGen/Spill2Reg.cpp +++ b/llvm/lib/CodeGen/Spill2Reg.cpp @@ -16,6 +16,7 @@ //===----------------------------------------------------------------------===// #include "AllocationOrder.h" +#include "llvm/ADT/Statistic.h" #include "llvm/CodeGen/LiveRegUnits.h" #include "llvm/CodeGen/MachineFrameInfo.h" #include "llvm/CodeGen/MachineFunctionPass.h" @@ -29,6 +30,9 @@ using namespace llvm; +#define DEBUG_TYPE "Spill2Reg" +STATISTIC(NumSpill2RegInstrs, "Number of spills/reloads replaced by spill2reg"); + static cl::opt EnableSpill2Reg("enable-spill2reg", cl::Hidden, cl::init(true), cl::desc("Enable Spill2Reg pass")); @@ -211,8 +215,7 @@ } bool Spill2Reg::isProfitable(const MachineInstr *MI) const { - // TODO: Unimplemented. - return true; + return TII->isSpill2RegProfitable(MI, TRI, MRI); } bool Spill2Reg::allAccessesProfitable(const StackSlotDataEntry &Entry) const { @@ -236,7 +239,33 @@ // Replace stack-based spills/reloads with register-based ones. void Spill2Reg::replaceStackWithReg(StackSlotDataEntry &Entry, Register VectorReg) { - // TODO: Unimplemented + for (StackSlotDataEntry::MIData &SpillData : Entry.Spills) { + MachineInstr *StackSpill = SpillData.MI; + assert(SpillData.MO->isReg() && "Expected register MO"); + Register OldReg = SpillData.MO->getReg(); + + MachineInstr *SpillToVector = TII->spill2RegInsertToVectorReg( + VectorReg, OldReg, SpillData.MemBits, StackSpill->getParent(), + /*InsertBeforeIt=*/StackSpill->getIterator(), TRI); + + // Spill to stack is no longer needed. + StackSpill->eraseFromParent(); + assert(OldReg.isPhysical() && "Otherwise we need to removeInterval()"); + } + + for (StackSlotDataEntry::MIData &ReloadData : Entry.Reloads) { + MachineInstr *StackReload = ReloadData.MI; + assert(ReloadData.MO->isReg() && "Expected Reg MO"); + Register OldReg = ReloadData.MO->getReg(); + + MachineInstr *ReloadFromReg = TII->spill2RegExtractFromVectorReg( + OldReg, VectorReg, ReloadData.MemBits, StackReload->getParent(), + /*InsertBeforeIt=*/StackReload->getIterator(), TRI); + + // Reload from stack is no longer needed. + StackReload->eraseFromParent(); + assert(OldReg.isPhysical() && "Otherwise we need to removeInterval()"); + } } void Spill2Reg::calculateLiveRegs(StackSlotDataEntry &Entry, @@ -267,6 +296,8 @@ // Replace stack accesses with register accesses. replaceStackWithReg(Entry, *PhysVectorRegOpt); + + NumSpill2RegInstrs += Entry.Spills.size() + Entry.Reloads.size(); } } diff --git a/llvm/test/CodeGen/X86/spill2reg_mask_spills.mir b/llvm/test/CodeGen/X86/spill2reg_mask_spills.mir new file mode 100644 --- /dev/null +++ b/llvm/test/CodeGen/X86/spill2reg_mask_spills.mir @@ -0,0 +1,32 @@ +# NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py +# RUN: llc %s -o - -mtriple=x86_64-unknown-linux -enable-spill2reg -mattr=+avx512f --run-pass=spill2reg -simplify-mir -spill2reg-mem-instrs=0 -spill2reg-vec-instrs=99999 | FileCheck %s + +# Checks that spills reading from $k mask registers are skipped by Spill2Reg. + +--- | + @D0 = dso_local local_unnamed_addr global i32 0, align 4 + @U0 = dso_local local_unnamed_addr global i32 0, align 4 + define void @func() { ret void } +... +--- +name: func +alignment: 16 +tracksRegLiveness: true +tracksDebugUserValues: true +frameInfo: + maxAlignment: 4 +stack: + - { id: 0, type: spill-slot, size: 4, alignment: 4 } +machineFunctionInfo: {} +body: | + ; CHECK-LABEL: bb.0: + ; CHECK-NEXT: liveins: $k1 + ; CHECK: KMOVWmk %stack.0, 1, $noreg, 0, $noreg, renamable $k1 :: (store (s16) into %stack.0) + ; CHECK-NEXT: renamable $k1 = KMOVWkm %stack.0, 1, $noreg, 0, $noreg :: (load (s16) from %stack.0) + ; CHECK-NEXT: RET 0 + bb.0: + liveins: $k1 + KMOVWmk %stack.0, 1, $noreg, 0, $noreg, killed renamable $k1 :: (store (s16) into %stack.0) + renamable $k1 = KMOVWkm %stack.0, 1, $noreg, 0, $noreg :: (load (s16) from %stack.0) + RET 0 +... diff --git a/llvm/test/CodeGen/X86/spill2reg_simple_1.mir b/llvm/test/CodeGen/X86/spill2reg_simple_1.mir new file mode 100644 --- /dev/null +++ b/llvm/test/CodeGen/X86/spill2reg_simple_1.mir @@ -0,0 +1,39 @@ +# NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py +# RUN: llc %s -o - -mtriple=x86_64-unknown-linux -enable-spill2reg -mattr=+sse4.1 --run-pass=spill2reg -simplify-mir -spill2reg-mem-instrs=0 -spill2reg-vec-instrs=99999 | FileCheck %s + +# Simple test with a single spill-reload pair: +# spill stack.0 +# reload stack.0 + +--- | + @D0 = dso_local local_unnamed_addr global i32 0, align 4 + @U0 = dso_local local_unnamed_addr global i32 0, align 4 + define void @func() { ret void } +... +--- +name: func +alignment: 16 +tracksRegLiveness: true +tracksDebugUserValues: true +frameInfo: + maxAlignment: 4 +stack: + - { id: 0, type: spill-slot, size: 4, alignment: 4 } +machineFunctionInfo: {} +body: | + ; CHECK-LABEL: bb.0: + ; CHECK-NEXT: $eax = MOV32rm $rip, 1, $noreg, @D0, $noreg :: (dereferenceable load (s32) from @D0) + ; CHECK-NEXT: $xmm0 = MOVDI2PDIrr $eax + ; CHECK-NEXT: $eax = MOVPDI2DIrr $xmm0 + ; CHECK-NEXT: MOV32mr $rip, 1, $noreg, @U0, $noreg, killed renamable $eax :: (store (s32) into @U0) + ; CHECK-NEXT: RET 0 + + bb.0: + ; spill + $eax = MOV32rm $rip, 1, $noreg, @D0, $noreg :: (dereferenceable load (s32) from @D0) + MOV32mr %stack.0, 1, $noreg, 0, $noreg, killed renamable $eax :: (store (s32) into %stack.0) + ; reload + $eax = MOV32rm %stack.0, 1, $noreg, 0, $noreg :: (load (s32) from %stack.0) + MOV32mr $rip, 1, $noreg, @U0, $noreg, killed renamable $eax :: (store (s32) into @U0) + RET 0 +... diff --git a/llvm/test/CodeGen/X86/spill2reg_simple_2.mir b/llvm/test/CodeGen/X86/spill2reg_simple_2.mir new file mode 100644 --- /dev/null +++ b/llvm/test/CodeGen/X86/spill2reg_simple_2.mir @@ -0,0 +1,52 @@ +# NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py +# RUN: llc %s -o - -mtriple=x86_64-unknown-linux -enable-spill2reg -mattr=+sse4.1 --run-pass=spill2reg -simplify-mir -spill2reg-mem-instrs=0 -spill2reg-vec-instrs=99999 | FileCheck %s + +# Simple test with two overlapping spill-reload pairs. +# spill stack.0 +# spill stack.1 +# reload stack.0 +# reload stack.1 + +--- | + @D0 = dso_local local_unnamed_addr global i32 0, align 4 + @D1 = dso_local local_unnamed_addr global i32 0, align 4 + @U0 = dso_local local_unnamed_addr global i32 0, align 4 + @U1 = dso_local local_unnamed_addr global i32 0, align 4 + define void @func() { ret void } +... +--- +name: func +alignment: 16 +tracksRegLiveness: true +tracksDebugUserValues: true +frameInfo: + maxAlignment: 4 +stack: + - { id: 0, type: spill-slot, size: 4, alignment: 4 } + - { id: 1, type: spill-slot, size: 4, alignment: 4 } +machineFunctionInfo: {} +body: | + ; CHECK-LABEL: bb.0: + ; CHECK-NEXT: $eax = MOV32rm $rip, 1, $noreg, @D0, $noreg :: (dereferenceable load (s32) from @D0) + ; CHECK-NEXT: $xmm0 = MOVDI2PDIrr $eax + ; CHECK-NEXT: $eax = MOV32rm $rip, 1, $noreg, @D1, $noreg :: (dereferenceable load (s32) from @D1) + ; CHECK-NEXT: $xmm0 = MOVDI2PDIrr $eax + ; CHECK-NEXT: $eax = MOVPDI2DIrr $xmm0 + ; CHECK-NEXT: MOV32mr $rip, 1, $noreg, @U0, $noreg, killed renamable $eax :: (store (s32) into @U0) + ; CHECK-NEXT: $eax = MOVPDI2DIrr $xmm0 + ; CHECK-NEXT: MOV32mr $rip, 1, $noreg, @U1, $noreg, killed renamable $eax :: (store (s32) into @U1) + ; CHECK-NEXT: RET 0 + + bb.0: + $eax = MOV32rm $rip, 1, $noreg, @D0, $noreg :: (dereferenceable load (s32) from @D0) + MOV32mr %stack.0, 1, $noreg, 0, $noreg, killed renamable $eax :: (store (s32) into %stack.0) + $eax = MOV32rm $rip, 1, $noreg, @D1, $noreg :: (dereferenceable load (s32) from @D1) + MOV32mr %stack.1, 1, $noreg, 0, $noreg, killed renamable $eax :: (store (s32) into %stack.1) + + $eax = MOV32rm %stack.0, 1, $noreg, 0, $noreg :: (load (s32) from %stack.0) + MOV32mr $rip, 1, $noreg, @U0, $noreg, killed renamable $eax :: (store (s32) into @U0) + $eax = MOV32rm %stack.1, 1, $noreg, 0, $noreg :: (load (s32) from %stack.1) + MOV32mr $rip, 1, $noreg, @U1, $noreg, killed renamable $eax :: (store (s32) into @U1) + RET 0 + +...