mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2024-08-23 02:29:18 +00:00
This patch is a third attempt to properly handle the local-dynamic and global-dynamic TLS models. In my original implementation, calls to __tls_get_addr were hidden from view until the asm-printer phase, at which point the underlying branch-and-link instruction was created with proper relocations. This mostly worked well, but I used some repellent techniques to ensure that the TLS_GET_ADDR nodes at the SD and MI levels correctly received input from GPR3 and produced output into GPR3. This proved to work badly in the presence of multiple TLS variable accesses, with the copies to and from GPR3 being scheduled incorrectly and generally creating havoc. In r221703, I addressed that problem by representing the calls to __tls_get_addr as true calls during instruction lowering. This had the advantage of removing all of the bad hacks and relying on the existing call machinery to properly glue the copies in place. It looked like this was going to be the right way to go. However, as a side effect of the recent discovery of problems with linker optimizations for TLS, we discovered cases of suboptimal code generation with this strategy. The problem comes when tls_get_addr is called for the same address, and there is a resulting CSE opportunity. It turns out that in such cases MachineCSE will common the addis/addi instructions that set up the input value to tls_get_addr, but will not common the calls themselves. MachineCSE does not have any machinery to common idempotent calls. This is perfectly sensible, since presumably this would be done at the IR level, and introducing calls in the back end isn't commonplace. In any case, we end up with two calls to __tls_get_addr when one would suffice, and that isn't good. I presumed that the original design would have allowed commoning of the machine-specific nodes that hid the __tls_get_addr calls, so as suggested by Ulrich Weigand, I went back to that design and cleaned it up so that the copies were properly held together by glue nodes. However, it turned out that this didn't work either...the presence of copies to physical registers kept the machine-specific nodes from being commoned also. All of which leads to the design presented here. This is a return to the original design, except that no attempt is made to introduce copies to and from GPR3 during instruction lowering. Virtual registers are used until prior to register allocation. At that point, a special pass is run that identifies the machine-specific nodes that hide the tls_get_addr calls and introduces the copies to and from GPR3 around them. The register allocator then coalesces these copies away. With this design, MachineCSE succeeds in commoning tls_get_addr calls where possible, and we get nice optimal code generation (better than GCC at the moment, which does not common these calls). One additional problem must be dealt with: After introducing the mentions of the physical register GPR3, the aggressive anti-dependence breaker sees opportunities to improve scheduling by selecting a different register instead. Flags must be used on the instruction descriptions to tell the anti-dependence breaker to keep its hands in its pockets. One thing missing from the original design was recording a definition of the link register on the GET_TLS_ADDR nodes. Doing this was found to be insufficient to force a stack frame to be created, which led to looping behavior because two different LR values were stored at the same address. This appears to have been an oversight in PPCFrameLowering::determineFrameLayout(), which is repaired here. Because MustSaveLR() returns true for calls to builtin_return_address, this changed the expected behavior of test/CodeGen/PowerPC/retaddr2.ll, which now stacks a frame but formerly did not. I've fixed the test case to reflect this. There are existing TLS tests to catch regressions; the checks in test/CodeGen/PowerPC/tls-store2.ll proved to be too restrictive in the face of instruction scheduling with these changes, so I fixed that up. I've added a new test case based on the PrettyStackTrace module that demonstrated the original problem. This checks that we get correct code generation and that CSE of the calls to __get_tls_addr has taken place. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@227976 91177308-0d34-0410-b5e6-96231b3b80d8
217 lines
6.9 KiB
C++
217 lines
6.9 KiB
C++
//===-- PPCMCInstLower.cpp - Convert PPC MachineInstr to an MCInst --------===//
|
|
//
|
|
// The LLVM Compiler Infrastructure
|
|
//
|
|
// This file is distributed under the University of Illinois Open Source
|
|
// License. See LICENSE.TXT for details.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This file contains code to lower PPC MachineInstrs to their corresponding
|
|
// MCInst records.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "PPC.h"
|
|
#include "MCTargetDesc/PPCMCExpr.h"
|
|
#include "PPCSubtarget.h"
|
|
#include "llvm/ADT/SmallString.h"
|
|
#include "llvm/ADT/Twine.h"
|
|
#include "llvm/CodeGen/AsmPrinter.h"
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
|
#include "llvm/CodeGen/MachineModuleInfoImpls.h"
|
|
#include "llvm/IR/DataLayout.h"
|
|
#include "llvm/IR/GlobalValue.h"
|
|
#include "llvm/IR/Mangler.h"
|
|
#include "llvm/MC/MCAsmInfo.h"
|
|
#include "llvm/MC/MCExpr.h"
|
|
#include "llvm/MC/MCInst.h"
|
|
#include "llvm/Target/TargetLowering.h"
|
|
#include "llvm/Target/TargetLoweringObjectFile.h"
|
|
using namespace llvm;
|
|
|
|
static MachineModuleInfoMachO &getMachOMMI(AsmPrinter &AP) {
|
|
return AP.MMI->getObjFileInfo<MachineModuleInfoMachO>();
|
|
}
|
|
|
|
|
|
static MCSymbol *GetSymbolFromOperand(const MachineOperand &MO, AsmPrinter &AP){
|
|
const TargetMachine &TM = AP.TM;
|
|
Mangler *Mang = AP.Mang;
|
|
const DataLayout *DL = TM.getDataLayout();
|
|
MCContext &Ctx = AP.OutContext;
|
|
bool isDarwin = Triple(TM.getTargetTriple()).isOSDarwin();
|
|
|
|
SmallString<128> Name;
|
|
StringRef Suffix;
|
|
if (MO.getTargetFlags() == PPCII::MO_PLT_OR_STUB) {
|
|
if (isDarwin)
|
|
Suffix = "$stub";
|
|
} else if (MO.getTargetFlags() & PPCII::MO_NLP_FLAG)
|
|
Suffix = "$non_lazy_ptr";
|
|
|
|
if (!Suffix.empty())
|
|
Name += DL->getPrivateGlobalPrefix();
|
|
|
|
unsigned PrefixLen = Name.size();
|
|
|
|
if (!MO.isGlobal()) {
|
|
assert(MO.isSymbol() && "Isn't a symbol reference");
|
|
Mang->getNameWithPrefix(Name, MO.getSymbolName());
|
|
} else {
|
|
const GlobalValue *GV = MO.getGlobal();
|
|
TM.getNameWithPrefix(Name, GV, *Mang);
|
|
}
|
|
|
|
unsigned OrigLen = Name.size() - PrefixLen;
|
|
|
|
Name += Suffix;
|
|
MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name.str());
|
|
StringRef OrigName = StringRef(Name).substr(PrefixLen, OrigLen);
|
|
|
|
// If the target flags on the operand changes the name of the symbol, do that
|
|
// before we return the symbol.
|
|
if (MO.getTargetFlags() == PPCII::MO_PLT_OR_STUB && isDarwin) {
|
|
MachineModuleInfoImpl::StubValueTy &StubSym =
|
|
getMachOMMI(AP).getFnStubEntry(Sym);
|
|
if (StubSym.getPointer())
|
|
return Sym;
|
|
|
|
if (MO.isGlobal()) {
|
|
StubSym =
|
|
MachineModuleInfoImpl::
|
|
StubValueTy(AP.getSymbol(MO.getGlobal()),
|
|
!MO.getGlobal()->hasInternalLinkage());
|
|
} else {
|
|
StubSym =
|
|
MachineModuleInfoImpl::
|
|
StubValueTy(Ctx.GetOrCreateSymbol(OrigName), false);
|
|
}
|
|
return Sym;
|
|
}
|
|
|
|
// If the symbol reference is actually to a non_lazy_ptr, not to the symbol,
|
|
// then add the suffix.
|
|
if (MO.getTargetFlags() & PPCII::MO_NLP_FLAG) {
|
|
MachineModuleInfoMachO &MachO = getMachOMMI(AP);
|
|
|
|
MachineModuleInfoImpl::StubValueTy &StubSym =
|
|
(MO.getTargetFlags() & PPCII::MO_NLP_HIDDEN_FLAG) ?
|
|
MachO.getHiddenGVStubEntry(Sym) : MachO.getGVStubEntry(Sym);
|
|
|
|
if (!StubSym.getPointer()) {
|
|
assert(MO.isGlobal() && "Extern symbol not handled yet");
|
|
StubSym = MachineModuleInfoImpl::
|
|
StubValueTy(AP.getSymbol(MO.getGlobal()),
|
|
!MO.getGlobal()->hasInternalLinkage());
|
|
}
|
|
return Sym;
|
|
}
|
|
|
|
return Sym;
|
|
}
|
|
|
|
static MCOperand GetSymbolRef(const MachineOperand &MO, const MCSymbol *Symbol,
|
|
AsmPrinter &Printer, bool isDarwin) {
|
|
MCContext &Ctx = Printer.OutContext;
|
|
MCSymbolRefExpr::VariantKind RefKind = MCSymbolRefExpr::VK_None;
|
|
|
|
unsigned access = MO.getTargetFlags() & PPCII::MO_ACCESS_MASK;
|
|
|
|
switch (access) {
|
|
case PPCII::MO_TPREL_LO:
|
|
RefKind = MCSymbolRefExpr::VK_PPC_TPREL_LO;
|
|
break;
|
|
case PPCII::MO_TPREL_HA:
|
|
RefKind = MCSymbolRefExpr::VK_PPC_TPREL_HA;
|
|
break;
|
|
case PPCII::MO_DTPREL_LO:
|
|
RefKind = MCSymbolRefExpr::VK_PPC_DTPREL_LO;
|
|
break;
|
|
case PPCII::MO_TLSLD_LO:
|
|
RefKind = MCSymbolRefExpr::VK_PPC_GOT_TLSLD_LO;
|
|
break;
|
|
case PPCII::MO_TOC_LO:
|
|
RefKind = MCSymbolRefExpr::VK_PPC_TOC_LO;
|
|
break;
|
|
case PPCII::MO_TLS:
|
|
RefKind = MCSymbolRefExpr::VK_PPC_TLS;
|
|
break;
|
|
}
|
|
|
|
if (MO.getTargetFlags() == PPCII::MO_PLT_OR_STUB && !isDarwin)
|
|
RefKind = MCSymbolRefExpr::VK_PLT;
|
|
|
|
const MCExpr *Expr = MCSymbolRefExpr::Create(Symbol, RefKind, Ctx);
|
|
|
|
if (!MO.isJTI() && MO.getOffset())
|
|
Expr = MCBinaryExpr::CreateAdd(Expr,
|
|
MCConstantExpr::Create(MO.getOffset(), Ctx),
|
|
Ctx);
|
|
|
|
// Subtract off the PIC base if required.
|
|
if (MO.getTargetFlags() & PPCII::MO_PIC_FLAG) {
|
|
const MachineFunction *MF = MO.getParent()->getParent()->getParent();
|
|
|
|
const MCExpr *PB = MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
|
|
Expr = MCBinaryExpr::CreateSub(Expr, PB, Ctx);
|
|
}
|
|
|
|
// Add ha16() / lo16() markers if required.
|
|
switch (access) {
|
|
case PPCII::MO_LO:
|
|
Expr = PPCMCExpr::CreateLo(Expr, isDarwin, Ctx);
|
|
break;
|
|
case PPCII::MO_HA:
|
|
Expr = PPCMCExpr::CreateHa(Expr, isDarwin, Ctx);
|
|
break;
|
|
}
|
|
|
|
return MCOperand::CreateExpr(Expr);
|
|
}
|
|
|
|
void llvm::LowerPPCMachineInstrToMCInst(const MachineInstr *MI, MCInst &OutMI,
|
|
AsmPrinter &AP, bool isDarwin) {
|
|
OutMI.setOpcode(MI->getOpcode());
|
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
const MachineOperand &MO = MI->getOperand(i);
|
|
|
|
MCOperand MCOp;
|
|
switch (MO.getType()) {
|
|
default:
|
|
MI->dump();
|
|
llvm_unreachable("unknown operand type");
|
|
case MachineOperand::MO_Register:
|
|
assert(!MO.getSubReg() && "Subregs should be eliminated!");
|
|
MCOp = MCOperand::CreateReg(MO.getReg());
|
|
break;
|
|
case MachineOperand::MO_Immediate:
|
|
MCOp = MCOperand::CreateImm(MO.getImm());
|
|
break;
|
|
case MachineOperand::MO_MachineBasicBlock:
|
|
MCOp = MCOperand::CreateExpr(MCSymbolRefExpr::Create(
|
|
MO.getMBB()->getSymbol(), AP.OutContext));
|
|
break;
|
|
case MachineOperand::MO_GlobalAddress:
|
|
case MachineOperand::MO_ExternalSymbol:
|
|
MCOp = GetSymbolRef(MO, GetSymbolFromOperand(MO, AP), AP, isDarwin);
|
|
break;
|
|
case MachineOperand::MO_JumpTableIndex:
|
|
MCOp = GetSymbolRef(MO, AP.GetJTISymbol(MO.getIndex()), AP, isDarwin);
|
|
break;
|
|
case MachineOperand::MO_ConstantPoolIndex:
|
|
MCOp = GetSymbolRef(MO, AP.GetCPISymbol(MO.getIndex()), AP, isDarwin);
|
|
break;
|
|
case MachineOperand::MO_BlockAddress:
|
|
MCOp = GetSymbolRef(MO,AP.GetBlockAddressSymbol(MO.getBlockAddress()),AP,
|
|
isDarwin);
|
|
break;
|
|
case MachineOperand::MO_RegisterMask:
|
|
continue;
|
|
}
|
|
|
|
OutMI.addOperand(MCOp);
|
|
}
|
|
}
|