Skip to content

Commit 54c20b5

Browse files
author
Evan Cheng
committed
When a live virtual register is being clobbered by an implicit def, it is spilled
and the spill is its kill. However, if the local allocator has determined the register has not been modified (possible when its value was reloaded), it would not issue a restore. In that case, mark the last use of the virtual register as kill. llvm-svn: 46111
1 parent 3866995 commit 54c20b5

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

llvm/lib/CodeGen/RegAllocLocal.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,16 @@ namespace {
8686
//
8787
std::vector<unsigned> PhysRegsUseOrder;
8888

89+
// Virt2LastUseMap - This maps each virtual register to its last use
90+
// (MachineInstr*, operand index pair).
91+
IndexedMap<std::pair<MachineInstr*, unsigned>, VirtReg2IndexFunctor>
92+
Virt2LastUseMap;
93+
94+
std::pair<MachineInstr*,unsigned>& getVirtRegLastUse(unsigned Reg) {
95+
assert(MRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!");
96+
return Virt2LastUseMap[Reg];
97+
}
98+
8999
// VirtRegModified - This bitset contains information about which virtual
90100
// registers need to be spilled back to memory when their registers are
91101
// scavenged. If a virtual register has simply been rematerialized, there
@@ -282,8 +292,12 @@ void RALocal::spillVirtReg(MachineBasicBlock &MBB,
282292

283293
const TargetInstrInfo* TII = MBB.getParent()->getTarget().getInstrInfo();
284294

285-
if (!isVirtRegModified(VirtReg))
295+
if (!isVirtRegModified(VirtReg)) {
286296
DOUT << " which has not been modified, so no store necessary!";
297+
std::pair<MachineInstr*, unsigned> &LastUse = getVirtRegLastUse(VirtReg);
298+
if (LastUse.first)
299+
LastUse.first->getOperand(LastUse.second).setIsKill();
300+
}
287301

288302
// Otherwise, there is a virtual register corresponding to this physical
289303
// register. We only need to spill it into its stack slot if it has been
@@ -507,6 +521,7 @@ MachineInstr *RALocal::reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
507521

508522
MF->getRegInfo().setPhysRegUsed(PhysReg);
509523
MI->getOperand(OpNum).setReg(PhysReg); // Assign the input register
524+
getVirtRegLastUse(VirtReg) = std::make_pair(MI, OpNum);
510525
return MI;
511526
}
512527

@@ -722,6 +737,7 @@ void RALocal::AllocateBasicBlock(MachineBasicBlock &MBB) {
722737
DestPhysReg = getReg(MBB, MI, DestVirtReg);
723738
MF->getRegInfo().setPhysRegUsed(DestPhysReg);
724739
markVirtRegModified(DestVirtReg);
740+
getVirtRegLastUse(DestVirtReg) = std::make_pair((MachineInstr*)0, 0);
725741
MI->getOperand(i).setReg(DestPhysReg); // Assign the output register
726742
}
727743
}
@@ -823,7 +839,8 @@ bool RALocal::runOnMachineFunction(MachineFunction &Fn) {
823839
// mapping for all virtual registers
824840
unsigned LastVirtReg = MF->getRegInfo().getLastVirtReg();
825841
Virt2PhysRegMap.grow(LastVirtReg);
826-
VirtRegModified.resize(LastVirtReg-MRegisterInfo::FirstVirtualRegister);
842+
Virt2LastUseMap.grow(LastVirtReg);
843+
VirtRegModified.resize(LastVirtReg+1-MRegisterInfo::FirstVirtualRegister);
827844

828845
// Loop over all of the basic blocks, eliminating virtual register references
829846
for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
@@ -834,6 +851,7 @@ bool RALocal::runOnMachineFunction(MachineFunction &Fn) {
834851
PhysRegsUsed.clear();
835852
VirtRegModified.clear();
836853
Virt2PhysRegMap.clear();
854+
Virt2LastUseMap.clear();
837855
return true;
838856
}
839857

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
; RUN: llvm-as < %s | llc -march=x86 -mattr=+sse2 -regalloc=local
2+
3+
define void @SolveCubic(double %a, double %b, double %c, double %d, i32* %solutions, double* %x) {
4+
entry:
5+
%tmp71 = load x86_fp80* null, align 16 ; <x86_fp80> [#uses=1]
6+
%tmp72 = fdiv x86_fp80 %tmp71, 0xKC000C000000000000000 ; <x86_fp80> [#uses=1]
7+
%tmp73 = add x86_fp80 0xK00000000000000000000, %tmp72 ; <x86_fp80> [#uses=1]
8+
%tmp7374 = fptrunc x86_fp80 %tmp73 to double ; <double> [#uses=1]
9+
store double %tmp7374, double* null, align 8
10+
%tmp81 = load double* null, align 8 ; <double> [#uses=1]
11+
%tmp82 = add double %tmp81, 0x401921FB54442D18 ; <double> [#uses=1]
12+
%tmp83 = fdiv double %tmp82, 3.000000e+00 ; <double> [#uses=1]
13+
%tmp84 = call double @cos( double %tmp83 ) ; <double> [#uses=1]
14+
%tmp85 = mul double 0.000000e+00, %tmp84 ; <double> [#uses=1]
15+
%tmp8586 = fpext double %tmp85 to x86_fp80 ; <x86_fp80> [#uses=1]
16+
%tmp87 = load x86_fp80* null, align 16 ; <x86_fp80> [#uses=1]
17+
%tmp88 = fdiv x86_fp80 %tmp87, 0xKC000C000000000000000 ; <x86_fp80> [#uses=1]
18+
%tmp89 = add x86_fp80 %tmp8586, %tmp88 ; <x86_fp80> [#uses=1]
19+
%tmp8990 = fptrunc x86_fp80 %tmp89 to double ; <double> [#uses=1]
20+
store double %tmp8990, double* null, align 8
21+
%tmp97 = load double* null, align 8 ; <double> [#uses=1]
22+
%tmp98 = add double %tmp97, 0x402921FB54442D18 ; <double> [#uses=1]
23+
%tmp99 = fdiv double %tmp98, 3.000000e+00 ; <double> [#uses=1]
24+
%tmp100 = call double @cos( double %tmp99 ) ; <double> [#uses=1]
25+
%tmp101 = mul double 0.000000e+00, %tmp100 ; <double> [#uses=1]
26+
%tmp101102 = fpext double %tmp101 to x86_fp80 ; <x86_fp80> [#uses=1]
27+
%tmp103 = load x86_fp80* null, align 16 ; <x86_fp80> [#uses=1]
28+
%tmp104 = fdiv x86_fp80 %tmp103, 0xKC000C000000000000000 ; <x86_fp80> [#uses=1]
29+
%tmp105 = add x86_fp80 %tmp101102, %tmp104 ; <x86_fp80> [#uses=1]
30+
%tmp105106 = fptrunc x86_fp80 %tmp105 to double ; <double> [#uses=1]
31+
store double %tmp105106, double* null, align 8
32+
ret void
33+
}
34+
35+
declare double @cos(double)

0 commit comments

Comments
 (0)