1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-10-19 19:12:56 +02:00

Weak non-function symbols were being accessed directly, which is

incorrect, as the chosen representative of the weak symbol may not live
with the code in question. Always indirect the access through the TOC
instead.

Patch by Kyle Butt!

llvm-svn: 253708
This commit is contained in:
Eric Christopher 2015-11-20 20:51:31 +00:00
parent 7c2597098d
commit 6ad23a3203
6 changed files with 91 additions and 47 deletions

View File

@ -669,28 +669,22 @@ void PPCAsmPrinter::EmitInstruction(const MachineInstr *MI) {
MO.isBlockAddress()) &&
"Invalid operand for ADDIStocHA!");
MCSymbol *MOSymbol = nullptr;
bool IsExternal = false;
bool IsNonLocalFunction = false;
bool IsCommon = false;
bool IsAvailExt = false;
bool GlobalToc = false;
if (MO.isGlobal()) {
const GlobalValue *GV = MO.getGlobal();
MOSymbol = getSymbol(GV);
IsExternal = GV->isDeclaration();
IsCommon = GV->hasCommonLinkage();
IsNonLocalFunction = GV->getType()->getElementType()->isFunctionTy() &&
!GV->isStrongDefinitionForLinker();
IsAvailExt = GV->hasAvailableExternallyLinkage();
} else if (MO.isCPI())
unsigned char GVFlags = Subtarget->classifyGlobalReference(GV);
GlobalToc = (GVFlags & PPCII::MO_NLP_FLAG);
} else if (MO.isCPI()) {
MOSymbol = GetCPISymbol(MO.getIndex());
else if (MO.isJTI())
} else if (MO.isJTI()) {
MOSymbol = GetJTISymbol(MO.getIndex());
else if (MO.isBlockAddress())
} else if (MO.isBlockAddress()) {
MOSymbol = GetBlockAddressSymbol(MO.getBlockAddress());
}
if (IsExternal || IsNonLocalFunction || IsCommon || IsAvailExt ||
MO.isJTI() || MO.isBlockAddress() ||
if (GlobalToc || MO.isJTI() || MO.isBlockAddress() ||
TM.getCodeModel() == CodeModel::Large)
MOSymbol = lookUpOrCreateTOCEntry(MOSymbol);
@ -727,13 +721,14 @@ void PPCAsmPrinter::EmitInstruction(const MachineInstr *MI) {
MOSymbol = lookUpOrCreateTOCEntry(MOSymbol);
}
else if (MO.isGlobal()) {
const GlobalValue *GValue = MO.getGlobal();
MOSymbol = getSymbol(GValue);
if (GValue->getType()->getElementType()->isFunctionTy() ||
GValue->isDeclaration() || GValue->hasCommonLinkage() ||
GValue->hasAvailableExternallyLinkage() ||
TM.getCodeModel() == CodeModel::Large)
MOSymbol = lookUpOrCreateTOCEntry(MOSymbol);
const GlobalValue *GV = MO.getGlobal();
MOSymbol = getSymbol(GV);
DEBUG(
unsigned char GVFlags = Subtarget->classifyGlobalReference(GV);
assert((GVFlags & PPCII::MO_NLP_FLAG) &&
"LDtocL used on symbol that could be accessed directly is "
"invalid. Must match ADDIStocHA."));
MOSymbol = lookUpOrCreateTOCEntry(MOSymbol);
}
const MCExpr *Exp =
@ -754,21 +749,18 @@ void PPCAsmPrinter::EmitInstruction(const MachineInstr *MI) {
const MachineOperand &MO = MI->getOperand(2);
assert((MO.isGlobal() || MO.isCPI()) && "Invalid operand for ADDItocL");
MCSymbol *MOSymbol = nullptr;
bool IsExternal = false;
bool IsNonLocalFunction = false;
if (MO.isGlobal()) {
const GlobalValue *GV = MO.getGlobal();
DEBUG(
unsigned char GVFlags = Subtarget->classifyGlobalReference(GV);
assert (
!(GVFlags & PPCII::MO_NLP_FLAG) &&
"Interposable definitions must use indirect access."));
MOSymbol = getSymbol(GV);
IsExternal = GV->isDeclaration();
IsNonLocalFunction = GV->getType()->getElementType()->isFunctionTy() &&
!GV->isStrongDefinitionForLinker();
} else if (MO.isCPI())
} else if (MO.isCPI()) {
MOSymbol = GetCPISymbol(MO.getIndex());
if (IsNonLocalFunction || IsExternal ||
TM.getCodeModel() == CodeModel::Large)
MOSymbol = lookUpOrCreateTOCEntry(MOSymbol);
}
const MCExpr *Exp =
MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_PPC_TOC_LO,

View File

@ -1972,19 +1972,15 @@ unsigned PPCFastISel::PPCMaterializeGV(const GlobalValue *GV, MVT VT) {
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDIStocHA),
HighPartReg).addReg(PPC::X2).addGlobalAddress(GV);
// If/when switches are implemented, jump tables should be handled
// on the "if" path here.
if (CModel == CodeModel::Large ||
(GV->getType()->getElementType()->isFunctionTy() &&
!GV->isStrongDefinitionForLinker()) ||
GV->isDeclaration() || GV->hasCommonLinkage() ||
GV->hasAvailableExternallyLinkage())
unsigned char GVFlags = PPCSubTarget->classifyGlobalReference(GV);
if (GVFlags & PPCII::MO_NLP_FLAG) {
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocL),
DestReg).addGlobalAddress(GV).addReg(HighPartReg);
else
} else {
// Otherwise generate the ADDItocL.
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDItocL),
DestReg).addReg(HighPartReg).addGlobalAddress(GV);
}
}
return DestReg;

View File

@ -2902,9 +2902,7 @@ SDNode *PPCDAGToDAGISel::Select(SDNode *N) {
break;
// The first source operand is a TargetGlobalAddress or a TargetJumpTable.
// If it is an externally defined symbol, a symbol with common linkage,
// a non-local function address, or a jump table address, or if we are
// generating code for large code model, we generate:
// If it must be toc-referenced according to PPCSubTarget, we generate:
// LDtocL(<ga:@sym>, ADDIStocHA(%X2, <ga:@sym>))
// Otherwise we generate:
// ADDItocL(ADDIStocHA(%X2, <ga:@sym>), <ga:@sym>)
@ -2919,13 +2917,12 @@ SDNode *PPCDAGToDAGISel::Select(SDNode *N) {
MVT::i64, GA, SDValue(Tmp, 0)));
if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(GA)) {
const GlobalValue *GValue = G->getGlobal();
if ((GValue->getType()->getElementType()->isFunctionTy() &&
!GValue->isStrongDefinitionForLinker()) ||
GValue->isDeclaration() || GValue->hasCommonLinkage() ||
GValue->hasAvailableExternallyLinkage())
const GlobalValue *GV = G->getGlobal();
unsigned char GVFlags = PPCSubTarget->classifyGlobalReference(GV);
if (GVFlags & PPCII::MO_NLP_FLAG) {
return transferMemOperands(N, CurDAG->getMachineNode(PPC::LDtocL, dl,
MVT::i64, GA, SDValue(Tmp, 0)));
}
}
return CurDAG->getMachineNode(PPC::ADDItocL, dl, MVT::i64,

View File

@ -210,5 +210,33 @@ bool PPCSubtarget::enableSubRegLiveness() const {
return UseSubRegLiveness;
}
unsigned char PPCSubtarget::classifyGlobalReference(
const GlobalValue *GV) const {
// Note that currently we don't generate non-pic references.
// If a caller wants that, this will have to be updated.
// Large code model always uses the TOC even for local symbols.
if (TM.getCodeModel() == CodeModel::Large)
return PPCII::MO_PIC_FLAG | PPCII::MO_NLP_FLAG;
unsigned char flags = PPCII::MO_PIC_FLAG;
// Only if the relocation mode is PIC do we have to worry about
// interposition. In all other cases we can use a slightly looser standard to
// decide how to access the symbol.
if (TM.getRelocationModel() == Reloc::PIC_) {
// If it's local, or it's non-default, it can't be interposed.
if (!GV->hasLocalLinkage() &&
GV->hasDefaultVisibility()) {
flags |= PPCII::MO_NLP_FLAG;
}
return flags;
}
if (GV->isStrongDefinitionForLinker())
return flags;
return flags | PPCII::MO_NLP_FLAG;
}
bool PPCSubtarget::isELFv2ABI() const { return TM.isELFv2ABI(); }
bool PPCSubtarget::isPPC64() const { return TM.isPPC64(); }

View File

@ -285,6 +285,10 @@ public:
bool useAA() const override;
bool enableSubRegLiveness() const override;
/// classifyGlobalReference - Classify a global variable reference for the
/// current subtarget accourding to how we should reference it.
unsigned char classifyGlobalReference(const GlobalValue *GV) const;
};
} // End llvm namespace

View File

@ -0,0 +1,27 @@
; RUN: llc -mcpu=pwr7 -O0 -code-model=medium <%s | FileCheck %s
; RUN: llc -mcpu=pwr7 -O0 -code-model=large <%s | FileCheck %s
; Test correct code generation for medium and large code model
; for loading and storing a weak variable
target datalayout = "E-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-f128:128:128-v128:128:128-n32:64"
target triple = "powerpc64-unknown-linux-gnu"
@wi = weak global i32 0, align 4
define signext i32 @test_weak() nounwind {
entry:
%0 = load i32, i32* @wi, align 4
%inc = add nsw i32 %0, 1
store i32 %inc, i32* @wi, align 4
ret i32 %0
}
; CHECK-LABEL: test_weak:
; CHECK: addis [[REG1:[0-9]+]], 2, .LC[[TOCNUM:[0-9]+]]@toc@ha
; CHECK: ld [[REG2:[0-9]+]], .LC[[TOCNUM]]@toc@l([[REG1]])
; CHECK: lwz {{[0-9]+}}, 0([[REG2]])
; CHECK: stw {{[0-9]+}}, 0([[REG2]])
; CHECK: .section .toc
; CHECK: .LC[[TOCNUM]]:
; CHECK: .tc {{[a-z0-9A-Z_.]+}}[TC],{{[a-z0-9A-Z_.]+}}