From 7e5812cd174c9f29135dc1567aecf7fe3b6450ef Mon Sep 17 00:00:00 2001 From: Misha Brukman Date: Mon, 28 Jun 2004 18:20:59 +0000 Subject: [PATCH] Fix loading and storing PC-relative static variables, courtesy of Nate Begeman. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@14468 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Target/PowerPC/PPC32ISelSimple.cpp | 13 +++++++++++-- lib/Target/PowerPC/PowerPCISelSimple.cpp | 13 +++++++++++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/lib/Target/PowerPC/PPC32ISelSimple.cpp b/lib/Target/PowerPC/PPC32ISelSimple.cpp index 4302beb79b3..6d7b585106d 100644 --- a/lib/Target/PowerPC/PPC32ISelSimple.cpp +++ b/lib/Target/PowerPC/PPC32ISelSimple.cpp @@ -365,10 +365,19 @@ unsigned ISel::getReg(Value *V, MachineBasicBlock *MBB, copyConstantToRegister(MBB, IPt, C, Reg); return Reg; } else if (GlobalValue *GV = dyn_cast(V)) { + // GV is located at PC + distance + unsigned LRsave = makeAnotherReg(Type::IntTy); + unsigned CurPC = makeAnotherReg(Type::IntTy); unsigned Reg1 = makeAnotherReg(V->getType()); unsigned Reg2 = makeAnotherReg(V->getType()); - // Move the address of the global into the register - BuildMI(*MBB, IPt, PPC32::LOADHiAddr, 2, Reg1).addReg(PPC32::R0) + // Save the old LR + BuildMI(*MBB, IPt, PPC32::MFLR, 0, LRsave); + // Move PC to destination reg + BuildMI(*MBB, IPt, PPC32::MovePCtoLR, 0, CurPC); + // Restore the old LR + BuildMI(*MBB, IPt, PPC32::MTLR, 1).addReg(LRsave); + // Move value at PC + distance into return reg + BuildMI(*MBB, IPt, PPC32::LOADHiAddr, 2, Reg1).addReg(CurPC) .addGlobalAddress(GV); BuildMI(*MBB, IPt, PPC32::LOADLoAddr, 2, Reg2).addReg(Reg1) .addGlobalAddress(GV); diff --git a/lib/Target/PowerPC/PowerPCISelSimple.cpp b/lib/Target/PowerPC/PowerPCISelSimple.cpp index 4302beb79b3..6d7b585106d 100644 --- a/lib/Target/PowerPC/PowerPCISelSimple.cpp +++ b/lib/Target/PowerPC/PowerPCISelSimple.cpp @@ -365,10 +365,19 @@ unsigned ISel::getReg(Value *V, MachineBasicBlock *MBB, copyConstantToRegister(MBB, IPt, C, Reg); return Reg; } else if (GlobalValue *GV = dyn_cast(V)) { + // GV is located at PC + distance + unsigned LRsave = makeAnotherReg(Type::IntTy); + unsigned CurPC = makeAnotherReg(Type::IntTy); unsigned Reg1 = makeAnotherReg(V->getType()); unsigned Reg2 = makeAnotherReg(V->getType()); - // Move the address of the global into the register - BuildMI(*MBB, IPt, PPC32::LOADHiAddr, 2, Reg1).addReg(PPC32::R0) + // Save the old LR + BuildMI(*MBB, IPt, PPC32::MFLR, 0, LRsave); + // Move PC to destination reg + BuildMI(*MBB, IPt, PPC32::MovePCtoLR, 0, CurPC); + // Restore the old LR + BuildMI(*MBB, IPt, PPC32::MTLR, 1).addReg(LRsave); + // Move value at PC + distance into return reg + BuildMI(*MBB, IPt, PPC32::LOADHiAddr, 2, Reg1).addReg(CurPC) .addGlobalAddress(GV); BuildMI(*MBB, IPt, PPC32::LOADLoAddr, 2, Reg2).addReg(Reg1) .addGlobalAddress(GV);