2011-12-22 01:57:09 +00:00
|
|
|
//===-- PPCELFObjectWriter.cpp - PPC ELF Writer ---------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "MCTargetDesc/PPCMCTargetDesc.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "MCTargetDesc/PPCFixupKinds.h"
|
2012-12-14 20:28:38 +00:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2011-12-22 01:57:09 +00:00
|
|
|
#include "llvm/MC/MCELFObjectWriter.h"
|
2012-10-25 12:27:42 +00:00
|
|
|
#include "llvm/MC/MCExpr.h"
|
|
|
|
#include "llvm/MC/MCValue.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2011-12-22 01:57:09 +00:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class PPCELFObjectWriter : public MCELFObjectTargetWriter {
|
|
|
|
public:
|
|
|
|
PPCELFObjectWriter(bool Is64Bit, uint8_t OSABI);
|
|
|
|
|
|
|
|
virtual ~PPCELFObjectWriter();
|
|
|
|
protected:
|
2012-10-25 12:27:42 +00:00
|
|
|
virtual unsigned getRelocTypeInner(const MCValue &Target,
|
|
|
|
const MCFixup &Fixup,
|
|
|
|
bool IsPCRel) const;
|
2011-12-22 01:57:09 +00:00
|
|
|
virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
|
|
|
|
bool IsPCRel, bool IsRelocWithSymbol,
|
|
|
|
int64_t Addend) const;
|
2013-07-01 18:19:56 +00:00
|
|
|
virtual const MCSymbol *ExplicitRelSym(const MCAssembler &Asm,
|
|
|
|
const MCValue &Target,
|
|
|
|
const MCFragment &F,
|
|
|
|
const MCFixup &Fixup,
|
|
|
|
bool IsPCRel) const;
|
2012-10-25 12:27:42 +00:00
|
|
|
virtual const MCSymbol *undefinedExplicitRelSym(const MCValue &Target,
|
|
|
|
const MCFixup &Fixup,
|
|
|
|
bool IsPCRel) const;
|
2011-12-22 01:57:09 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
PPCELFObjectWriter::PPCELFObjectWriter(bool Is64Bit, uint8_t OSABI)
|
|
|
|
: MCELFObjectTargetWriter(Is64Bit, OSABI,
|
|
|
|
Is64Bit ? ELF::EM_PPC64 : ELF::EM_PPC,
|
2011-12-22 18:38:06 +00:00
|
|
|
/*HasRelocationAddend*/ true) {}
|
2011-12-22 01:57:09 +00:00
|
|
|
|
|
|
|
PPCELFObjectWriter::~PPCELFObjectWriter() {
|
|
|
|
}
|
|
|
|
|
2012-10-25 12:27:42 +00:00
|
|
|
unsigned PPCELFObjectWriter::getRelocTypeInner(const MCValue &Target,
|
|
|
|
const MCFixup &Fixup,
|
|
|
|
bool IsPCRel) const
|
|
|
|
{
|
|
|
|
MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
|
|
|
|
MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
|
|
|
|
|
2011-12-22 01:57:09 +00:00
|
|
|
// determine the type of the relocation
|
|
|
|
unsigned Type;
|
|
|
|
if (IsPCRel) {
|
|
|
|
switch ((unsigned)Fixup.getKind()) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Unimplemented");
|
|
|
|
case PPC::fixup_ppc_br24:
|
2013-06-24 11:03:33 +00:00
|
|
|
case PPC::fixup_ppc_br24abs:
|
2011-12-22 01:57:09 +00:00
|
|
|
Type = ELF::R_PPC_REL24;
|
|
|
|
break;
|
2013-04-26 15:38:30 +00:00
|
|
|
case PPC::fixup_ppc_brcond14:
|
2013-06-24 11:03:33 +00:00
|
|
|
case PPC::fixup_ppc_brcond14abs:
|
2013-04-26 15:38:30 +00:00
|
|
|
Type = ELF::R_PPC_REL14;
|
|
|
|
break;
|
2013-06-21 14:44:37 +00:00
|
|
|
case PPC::fixup_ppc_half16:
|
|
|
|
switch (Modifier) {
|
|
|
|
default: llvm_unreachable("Unsupported Modifier");
|
|
|
|
case MCSymbolRefExpr::VK_None:
|
|
|
|
Type = ELF::R_PPC_REL16;
|
|
|
|
break;
|
|
|
|
case MCSymbolRefExpr::VK_PPC_LO:
|
|
|
|
Type = ELF::R_PPC_REL16_LO;
|
|
|
|
break;
|
|
|
|
case MCSymbolRefExpr::VK_PPC_HI:
|
|
|
|
Type = ELF::R_PPC_REL16_HI;
|
|
|
|
break;
|
|
|
|
case MCSymbolRefExpr::VK_PPC_HA:
|
|
|
|
Type = ELF::R_PPC_REL16_HA;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2013-01-04 19:08:13 +00:00
|
|
|
case FK_Data_4:
|
2011-12-22 01:57:09 +00:00
|
|
|
case FK_PCRel_4:
|
|
|
|
Type = ELF::R_PPC_REL32;
|
|
|
|
break;
|
2013-01-04 19:08:13 +00:00
|
|
|
case FK_Data_8:
|
|
|
|
case FK_PCRel_8:
|
|
|
|
Type = ELF::R_PPC64_REL64;
|
|
|
|
break;
|
2011-12-22 01:57:09 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch ((unsigned)Fixup.getKind()) {
|
|
|
|
default: llvm_unreachable("invalid fixup kind!");
|
2013-06-24 11:03:33 +00:00
|
|
|
case PPC::fixup_ppc_br24abs:
|
2011-12-22 01:57:09 +00:00
|
|
|
Type = ELF::R_PPC_ADDR24;
|
|
|
|
break;
|
2013-06-24 11:03:33 +00:00
|
|
|
case PPC::fixup_ppc_brcond14abs:
|
2012-10-25 12:27:42 +00:00
|
|
|
Type = ELF::R_PPC_ADDR14; // XXX: or BRNTAKEN?_
|
2011-12-22 01:57:09 +00:00
|
|
|
break;
|
2013-05-17 12:37:21 +00:00
|
|
|
case PPC::fixup_ppc_half16:
|
2012-11-13 19:24:36 +00:00
|
|
|
switch (Modifier) {
|
|
|
|
default: llvm_unreachable("Unsupported Modifier");
|
2013-06-20 22:04:40 +00:00
|
|
|
case MCSymbolRefExpr::VK_None:
|
|
|
|
Type = ELF::R_PPC_ADDR16;
|
2012-11-13 19:24:36 +00:00
|
|
|
break;
|
2013-06-21 14:42:20 +00:00
|
|
|
case MCSymbolRefExpr::VK_PPC_LO:
|
2013-06-20 22:04:40 +00:00
|
|
|
Type = ELF::R_PPC_ADDR16_LO;
|
2012-12-12 19:29:35 +00:00
|
|
|
break;
|
2013-06-21 14:42:49 +00:00
|
|
|
case MCSymbolRefExpr::VK_PPC_HI:
|
|
|
|
Type = ELF::R_PPC_ADDR16_HI;
|
|
|
|
break;
|
2013-06-21 14:42:20 +00:00
|
|
|
case MCSymbolRefExpr::VK_PPC_HA:
|
2012-11-13 19:24:36 +00:00
|
|
|
Type = ELF::R_PPC_ADDR16_HA;
|
This patch implements medium code model support for 64-bit PowerPC.
The default for 64-bit PowerPC is small code model, in which TOC entries
must be addressable using a 16-bit offset from the TOC pointer. Additionally,
only TOC entries are addressed via the TOC pointer.
With medium code model, TOC entries and data sections can all be addressed
via the TOC pointer using a 32-bit offset. Cooperation with the linker
allows 16-bit offsets to be used when these are sufficient, reducing the
number of extra instructions that need to be executed. Medium code model
also does not generate explicit TOC entries in ".section toc" for variables
that are wholly internal to the compilation unit.
Consider a load of an external 4-byte integer. With small code model, the
compiler generates:
ld 3, .LC1@toc(2)
lwz 4, 0(3)
.section .toc,"aw",@progbits
.LC1:
.tc ei[TC],ei
With medium model, it instead generates:
addis 3, 2, .LC1@toc@ha
ld 3, .LC1@toc@l(3)
lwz 4, 0(3)
.section .toc,"aw",@progbits
.LC1:
.tc ei[TC],ei
Here .LC1@toc@ha is a relocation requesting the upper 16 bits of the
32-bit offset of ei's TOC entry from the TOC base pointer. Similarly,
.LC1@toc@l is a relocation requesting the lower 16 bits. Note that if
the linker determines that ei's TOC entry is within a 16-bit offset of
the TOC base pointer, it will replace the "addis" with a "nop", and
replace the "ld" with the identical "ld" instruction from the small
code model example.
Consider next a load of a function-scope static integer. For small code
model, the compiler generates:
ld 3, .LC1@toc(2)
lwz 4, 0(3)
.section .toc,"aw",@progbits
.LC1:
.tc test_fn_static.si[TC],test_fn_static.si
.type test_fn_static.si,@object
.local test_fn_static.si
.comm test_fn_static.si,4,4
For medium code model, the compiler generates:
addis 3, 2, test_fn_static.si@toc@ha
addi 3, 3, test_fn_static.si@toc@l
lwz 4, 0(3)
.type test_fn_static.si,@object
.local test_fn_static.si
.comm test_fn_static.si,4,4
Again, the linker may replace the "addis" with a "nop", calculating only
a 16-bit offset when this is sufficient.
Note that it would be more efficient for the compiler to generate:
addis 3, 2, test_fn_static.si@toc@ha
lwz 4, test_fn_static.si@toc@l(3)
The current patch does not perform this optimization yet. This will be
addressed as a peephole optimization in a later patch.
For the moment, the default code model for 64-bit PowerPC will remain the
small code model. We plan to eventually change the default to medium code
model, which matches current upstream GCC behavior. Note that the different
code models are ABI-compatible, so code compiled with different models will
be linked and execute correctly.
I've tested the regression suite and the application/benchmark test suite in
two ways: Once with the patch as submitted here, and once with additional
logic to force medium code model as the default. The tests all compile
cleanly, with one exception. The mandel-2 application test fails due to an
unrelated ABI compatibility with passing complex numbers. It just so happens
that small code model was incredibly lucky, in that temporary values in
floating-point registers held the expected values needed by the external
library routine that was called incorrectly. My current thought is to correct
the ABI problems with _Complex before making medium code model the default,
to avoid introducing this "regression."
Here are a few comments on how the patch works, since the selection code
can be difficult to follow:
The existing logic for small code model defines three pseudo-instructions:
LDtoc for most uses, LDtocJTI for jump table addresses, and LDtocCPT for
constant pool addresses. These are expanded by SelectCodeCommon(). The
pseudo-instruction approach doesn't work for medium code model, because
we need to generate two instructions when we match the same pattern.
Instead, new logic in PPCDAGToDAGISel::Select() intercepts the TOC_ENTRY
node for medium code model, and generates an ADDIStocHA followed by either
a LDtocL or an ADDItocL. These new node types correspond naturally to
the sequences described above.
The addis/ld sequence is generated for the following cases:
* Jump table addresses
* Function addresses
* External global variables
* Tentative definitions of global variables (common linkage)
The addis/addi sequence is generated for the following cases:
* Constant pool entries
* File-scope static global variables
* Function-scope static variables
Expanding to the two-instruction sequences at select time exposes the
instructions to subsequent optimization, particularly scheduling.
The rest of the processing occurs at assembly time, in
PPCAsmPrinter::EmitInstruction. Each of the instructions is converted to
a "real" PowerPC instruction. When a TOC entry needs to be created, this
is done here in the same manner as for the existing LDtoc, LDtocJTI, and
LDtocCPT pseudo-instructions (I factored out a new routine to handle this).
I had originally thought that if a TOC entry was needed for LDtocL or
ADDItocL, it would already have been generated for the previous ADDIStocHA.
However, at higher optimization levels, the ADDIStocHA may appear in a
different block, which may be assembled textually following the block
containing the LDtocL or ADDItocL. So it is necessary to include the
possibility of creating a new TOC entry for those two instructions.
Note that for LDtocL, we generate a new form of LD called LDrs. This
allows specifying the @toc@l relocation for the offset field of the LD
instruction (i.e., the offset is replaced by a SymbolLo relocation).
When the peephole optimization described above is added, we will need
to do similar things for all immediate-form load and store operations.
The seven "mcm-n.ll" test cases are kept separate because otherwise the
intermingling of various TOC entries and so forth makes the tests fragile
and hard to understand.
The above assumes use of an external assembler. For use of the
integrated assembler, new relocations are added and used by
PPCELFObjectWriter. Testing is done with "mcm-obj.ll", which tests for
proper generation of the various relocations for the same sequences
tested with the external assembler.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@168708 91177308-0d34-0410-b5e6-96231b3b80d8
2012-11-27 17:35:46 +00:00
|
|
|
break;
|
2013-06-21 14:43:42 +00:00
|
|
|
case MCSymbolRefExpr::VK_PPC_HIGHER:
|
|
|
|
Type = ELF::R_PPC64_ADDR16_HIGHER;
|
|
|
|
break;
|
|
|
|
case MCSymbolRefExpr::VK_PPC_HIGHERA:
|
|
|
|
Type = ELF::R_PPC64_ADDR16_HIGHERA;
|
|
|
|
break;
|
|
|
|
case MCSymbolRefExpr::VK_PPC_HIGHEST:
|
|
|
|
Type = ELF::R_PPC64_ADDR16_HIGHEST;
|
|
|
|
break;
|
|
|
|
case MCSymbolRefExpr::VK_PPC_HIGHESTA:
|
|
|
|
Type = ELF::R_PPC64_ADDR16_HIGHESTA;
|
|
|
|
break;
|
2013-06-25 16:49:50 +00:00
|
|
|
case MCSymbolRefExpr::VK_GOT:
|
|
|
|
Type = ELF::R_PPC_GOT16;
|
|
|
|
break;
|
|
|
|
case MCSymbolRefExpr::VK_PPC_GOT_LO:
|
|
|
|
Type = ELF::R_PPC_GOT16_LO;
|
|
|
|
break;
|
|
|
|
case MCSymbolRefExpr::VK_PPC_GOT_HI:
|
|
|
|
Type = ELF::R_PPC_GOT16_HI;
|
|
|
|
break;
|
|
|
|
case MCSymbolRefExpr::VK_PPC_GOT_HA:
|
|
|
|
Type = ELF::R_PPC_GOT16_HA;
|
|
|
|
break;
|
2013-06-21 14:42:20 +00:00
|
|
|
case MCSymbolRefExpr::VK_PPC_TOC:
|
2013-06-20 22:04:40 +00:00
|
|
|
Type = ELF::R_PPC64_TOC16;
|
This patch improves the 64-bit PowerPC InitialExec TLS support by providing
for a wider range of GOT entries that can hold thread-relative offsets.
This matches the behavior of GCC, which was not documented in the PPC64 TLS
ABI. The ABI will be updated with the new code sequence.
Former sequence:
ld 9,x@got@tprel(2)
add 9,9,x@tls
New sequence:
addis 9,2,x@got@tprel@ha
ld 9,x@got@tprel@l(9)
add 9,9,x@tls
Note that a linker optimization exists to transform the new sequence into
the shorter sequence when appropriate, by replacing the addis with a nop
and modifying the base register and relocation type of the ld.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@170209 91177308-0d34-0410-b5e6-96231b3b80d8
2012-12-14 17:02:38 +00:00
|
|
|
break;
|
2013-06-21 14:42:20 +00:00
|
|
|
case MCSymbolRefExpr::VK_PPC_TOC_LO:
|
2013-06-20 22:04:40 +00:00
|
|
|
Type = ELF::R_PPC64_TOC16_LO;
|
This patch implements the general dynamic TLS model for 64-bit PowerPC.
Given a thread-local symbol x with global-dynamic access, the generated
code to obtain x's address is:
Instruction Relocation Symbol
addis ra,r2,x@got@tlsgd@ha R_PPC64_GOT_TLSGD16_HA x
addi r3,ra,x@got@tlsgd@l R_PPC64_GOT_TLSGD16_L x
bl __tls_get_addr(x@tlsgd) R_PPC64_TLSGD x
R_PPC64_REL24 __tls_get_addr
nop
<use address in r3>
The implementation borrows from the medium code model work for introducing
special forms of ADDIS and ADDI into the DAG representation. This is made
slightly more complicated by having to introduce a call to the external
function __tls_get_addr. Using the full call machinery is overkill and,
more importantly, makes it difficult to add a special relocation. So I've
introduced another opcode GET_TLS_ADDR to represent the function call, and
surrounded it with register copies to set up the parameter and return value.
Most of the code is pretty straightforward. I ran into one peculiarity
when I introduced a new PPC opcode BL8_NOP_ELF_TLSGD, which is just like
BL8_NOP_ELF except that it takes another parameter to represent the symbol
("x" above) that requires a relocation on the call. Something in the
TblGen machinery causes BL8_NOP_ELF and BL8_NOP_ELF_TLSGD to be treated
identically during the emit phase, so this second operand was never
visited to generate relocations. This is the reason for the slightly
messy workaround in PPCMCCodeEmitter.cpp:getDirectBrEncoding().
Two new tests are included to demonstrate correct external assembly and
correct generation of relocations using the integrated assembler.
Comments welcome!
Thanks,
Bill
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@169910 91177308-0d34-0410-b5e6-96231b3b80d8
2012-12-11 20:30:11 +00:00
|
|
|
break;
|
2013-06-21 14:43:10 +00:00
|
|
|
case MCSymbolRefExpr::VK_PPC_TOC_HI:
|
|
|
|
Type = ELF::R_PPC64_TOC16_HI;
|
|
|
|
break;
|
2013-06-21 14:42:20 +00:00
|
|
|
case MCSymbolRefExpr::VK_PPC_TOC_HA:
|
2013-06-20 22:04:40 +00:00
|
|
|
Type = ELF::R_PPC64_TOC16_HA;
|
2012-12-12 19:29:35 +00:00
|
|
|
break;
|
2013-06-21 14:44:15 +00:00
|
|
|
case MCSymbolRefExpr::VK_PPC_TPREL:
|
|
|
|
Type = ELF::R_PPC_TPREL16;
|
|
|
|
break;
|
2013-06-21 14:42:20 +00:00
|
|
|
case MCSymbolRefExpr::VK_PPC_TPREL_LO:
|
2012-11-13 19:24:36 +00:00
|
|
|
Type = ELF::R_PPC_TPREL16_LO;
|
|
|
|
break;
|
2013-06-21 14:44:15 +00:00
|
|
|
case MCSymbolRefExpr::VK_PPC_TPREL_HI:
|
|
|
|
Type = ELF::R_PPC_TPREL16_HI;
|
|
|
|
break;
|
2013-06-21 14:42:20 +00:00
|
|
|
case MCSymbolRefExpr::VK_PPC_TPREL_HA:
|
2013-06-20 22:04:40 +00:00
|
|
|
Type = ELF::R_PPC_TPREL16_HA;
|
|
|
|
break;
|
2013-06-21 14:44:15 +00:00
|
|
|
case MCSymbolRefExpr::VK_PPC_TPREL_HIGHER:
|
|
|
|
Type = ELF::R_PPC64_TPREL16_HIGHER;
|
|
|
|
break;
|
|
|
|
case MCSymbolRefExpr::VK_PPC_TPREL_HIGHERA:
|
|
|
|
Type = ELF::R_PPC64_TPREL16_HIGHERA;
|
|
|
|
break;
|
|
|
|
case MCSymbolRefExpr::VK_PPC_TPREL_HIGHEST:
|
|
|
|
Type = ELF::R_PPC64_TPREL16_HIGHEST;
|
|
|
|
break;
|
|
|
|
case MCSymbolRefExpr::VK_PPC_TPREL_HIGHESTA:
|
|
|
|
Type = ELF::R_PPC64_TPREL16_HIGHESTA;
|
|
|
|
break;
|
|
|
|
case MCSymbolRefExpr::VK_PPC_DTPREL:
|
|
|
|
Type = ELF::R_PPC64_DTPREL16;
|
|
|
|
break;
|
2013-06-21 14:42:20 +00:00
|
|
|
case MCSymbolRefExpr::VK_PPC_DTPREL_LO:
|
2012-12-12 19:29:35 +00:00
|
|
|
Type = ELF::R_PPC64_DTPREL16_LO;
|
|
|
|
break;
|
2013-06-21 14:44:15 +00:00
|
|
|
case MCSymbolRefExpr::VK_PPC_DTPREL_HI:
|
|
|
|
Type = ELF::R_PPC64_DTPREL16_HI;
|
|
|
|
break;
|
2013-06-21 14:42:20 +00:00
|
|
|
case MCSymbolRefExpr::VK_PPC_DTPREL_HA:
|
2013-06-20 22:04:40 +00:00
|
|
|
Type = ELF::R_PPC64_DTPREL16_HA;
|
This patch implements medium code model support for 64-bit PowerPC.
The default for 64-bit PowerPC is small code model, in which TOC entries
must be addressable using a 16-bit offset from the TOC pointer. Additionally,
only TOC entries are addressed via the TOC pointer.
With medium code model, TOC entries and data sections can all be addressed
via the TOC pointer using a 32-bit offset. Cooperation with the linker
allows 16-bit offsets to be used when these are sufficient, reducing the
number of extra instructions that need to be executed. Medium code model
also does not generate explicit TOC entries in ".section toc" for variables
that are wholly internal to the compilation unit.
Consider a load of an external 4-byte integer. With small code model, the
compiler generates:
ld 3, .LC1@toc(2)
lwz 4, 0(3)
.section .toc,"aw",@progbits
.LC1:
.tc ei[TC],ei
With medium model, it instead generates:
addis 3, 2, .LC1@toc@ha
ld 3, .LC1@toc@l(3)
lwz 4, 0(3)
.section .toc,"aw",@progbits
.LC1:
.tc ei[TC],ei
Here .LC1@toc@ha is a relocation requesting the upper 16 bits of the
32-bit offset of ei's TOC entry from the TOC base pointer. Similarly,
.LC1@toc@l is a relocation requesting the lower 16 bits. Note that if
the linker determines that ei's TOC entry is within a 16-bit offset of
the TOC base pointer, it will replace the "addis" with a "nop", and
replace the "ld" with the identical "ld" instruction from the small
code model example.
Consider next a load of a function-scope static integer. For small code
model, the compiler generates:
ld 3, .LC1@toc(2)
lwz 4, 0(3)
.section .toc,"aw",@progbits
.LC1:
.tc test_fn_static.si[TC],test_fn_static.si
.type test_fn_static.si,@object
.local test_fn_static.si
.comm test_fn_static.si,4,4
For medium code model, the compiler generates:
addis 3, 2, test_fn_static.si@toc@ha
addi 3, 3, test_fn_static.si@toc@l
lwz 4, 0(3)
.type test_fn_static.si,@object
.local test_fn_static.si
.comm test_fn_static.si,4,4
Again, the linker may replace the "addis" with a "nop", calculating only
a 16-bit offset when this is sufficient.
Note that it would be more efficient for the compiler to generate:
addis 3, 2, test_fn_static.si@toc@ha
lwz 4, test_fn_static.si@toc@l(3)
The current patch does not perform this optimization yet. This will be
addressed as a peephole optimization in a later patch.
For the moment, the default code model for 64-bit PowerPC will remain the
small code model. We plan to eventually change the default to medium code
model, which matches current upstream GCC behavior. Note that the different
code models are ABI-compatible, so code compiled with different models will
be linked and execute correctly.
I've tested the regression suite and the application/benchmark test suite in
two ways: Once with the patch as submitted here, and once with additional
logic to force medium code model as the default. The tests all compile
cleanly, with one exception. The mandel-2 application test fails due to an
unrelated ABI compatibility with passing complex numbers. It just so happens
that small code model was incredibly lucky, in that temporary values in
floating-point registers held the expected values needed by the external
library routine that was called incorrectly. My current thought is to correct
the ABI problems with _Complex before making medium code model the default,
to avoid introducing this "regression."
Here are a few comments on how the patch works, since the selection code
can be difficult to follow:
The existing logic for small code model defines three pseudo-instructions:
LDtoc for most uses, LDtocJTI for jump table addresses, and LDtocCPT for
constant pool addresses. These are expanded by SelectCodeCommon(). The
pseudo-instruction approach doesn't work for medium code model, because
we need to generate two instructions when we match the same pattern.
Instead, new logic in PPCDAGToDAGISel::Select() intercepts the TOC_ENTRY
node for medium code model, and generates an ADDIStocHA followed by either
a LDtocL or an ADDItocL. These new node types correspond naturally to
the sequences described above.
The addis/ld sequence is generated for the following cases:
* Jump table addresses
* Function addresses
* External global variables
* Tentative definitions of global variables (common linkage)
The addis/addi sequence is generated for the following cases:
* Constant pool entries
* File-scope static global variables
* Function-scope static variables
Expanding to the two-instruction sequences at select time exposes the
instructions to subsequent optimization, particularly scheduling.
The rest of the processing occurs at assembly time, in
PPCAsmPrinter::EmitInstruction. Each of the instructions is converted to
a "real" PowerPC instruction. When a TOC entry needs to be created, this
is done here in the same manner as for the existing LDtoc, LDtocJTI, and
LDtocCPT pseudo-instructions (I factored out a new routine to handle this).
I had originally thought that if a TOC entry was needed for LDtocL or
ADDItocL, it would already have been generated for the previous ADDIStocHA.
However, at higher optimization levels, the ADDIStocHA may appear in a
different block, which may be assembled textually following the block
containing the LDtocL or ADDItocL. So it is necessary to include the
possibility of creating a new TOC entry for those two instructions.
Note that for LDtocL, we generate a new form of LD called LDrs. This
allows specifying the @toc@l relocation for the offset field of the LD
instruction (i.e., the offset is replaced by a SymbolLo relocation).
When the peephole optimization described above is added, we will need
to do similar things for all immediate-form load and store operations.
The seven "mcm-n.ll" test cases are kept separate because otherwise the
intermingling of various TOC entries and so forth makes the tests fragile
and hard to understand.
The above assumes use of an external assembler. For use of the
integrated assembler, new relocations are added and used by
PPCELFObjectWriter. Testing is done with "mcm-obj.ll", which tests for
proper generation of the various relocations for the same sequences
tested with the external assembler.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@168708 91177308-0d34-0410-b5e6-96231b3b80d8
2012-11-27 17:35:46 +00:00
|
|
|
break;
|
2013-06-21 14:44:15 +00:00
|
|
|
case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHER:
|
|
|
|
Type = ELF::R_PPC64_DTPREL16_HIGHER;
|
|
|
|
break;
|
|
|
|
case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHERA:
|
|
|
|
Type = ELF::R_PPC64_DTPREL16_HIGHERA;
|
|
|
|
break;
|
|
|
|
case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHEST:
|
|
|
|
Type = ELF::R_PPC64_DTPREL16_HIGHEST;
|
|
|
|
break;
|
|
|
|
case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHESTA:
|
|
|
|
Type = ELF::R_PPC64_DTPREL16_HIGHESTA;
|
|
|
|
break;
|
|
|
|
case MCSymbolRefExpr::VK_PPC_GOT_TLSGD:
|
|
|
|
Type = ELF::R_PPC64_GOT_TLSGD16;
|
|
|
|
break;
|
2013-06-21 14:42:20 +00:00
|
|
|
case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_LO:
|
This patch implements the general dynamic TLS model for 64-bit PowerPC.
Given a thread-local symbol x with global-dynamic access, the generated
code to obtain x's address is:
Instruction Relocation Symbol
addis ra,r2,x@got@tlsgd@ha R_PPC64_GOT_TLSGD16_HA x
addi r3,ra,x@got@tlsgd@l R_PPC64_GOT_TLSGD16_L x
bl __tls_get_addr(x@tlsgd) R_PPC64_TLSGD x
R_PPC64_REL24 __tls_get_addr
nop
<use address in r3>
The implementation borrows from the medium code model work for introducing
special forms of ADDIS and ADDI into the DAG representation. This is made
slightly more complicated by having to introduce a call to the external
function __tls_get_addr. Using the full call machinery is overkill and,
more importantly, makes it difficult to add a special relocation. So I've
introduced another opcode GET_TLS_ADDR to represent the function call, and
surrounded it with register copies to set up the parameter and return value.
Most of the code is pretty straightforward. I ran into one peculiarity
when I introduced a new PPC opcode BL8_NOP_ELF_TLSGD, which is just like
BL8_NOP_ELF except that it takes another parameter to represent the symbol
("x" above) that requires a relocation on the call. Something in the
TblGen machinery causes BL8_NOP_ELF and BL8_NOP_ELF_TLSGD to be treated
identically during the emit phase, so this second operand was never
visited to generate relocations. This is the reason for the slightly
messy workaround in PPCMCCodeEmitter.cpp:getDirectBrEncoding().
Two new tests are included to demonstrate correct external assembly and
correct generation of relocations using the integrated assembler.
Comments welcome!
Thanks,
Bill
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@169910 91177308-0d34-0410-b5e6-96231b3b80d8
2012-12-11 20:30:11 +00:00
|
|
|
Type = ELF::R_PPC64_GOT_TLSGD16_LO;
|
|
|
|
break;
|
2013-06-21 14:44:15 +00:00
|
|
|
case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_HI:
|
|
|
|
Type = ELF::R_PPC64_GOT_TLSGD16_HI;
|
|
|
|
break;
|
2013-06-21 14:42:20 +00:00
|
|
|
case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_HA:
|
2013-06-20 22:04:40 +00:00
|
|
|
Type = ELF::R_PPC64_GOT_TLSGD16_HA;
|
|
|
|
break;
|
2013-06-21 14:44:15 +00:00
|
|
|
case MCSymbolRefExpr::VK_PPC_GOT_TLSLD:
|
|
|
|
Type = ELF::R_PPC64_GOT_TLSLD16;
|
|
|
|
break;
|
2013-06-21 14:42:20 +00:00
|
|
|
case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_LO:
|
2012-12-12 19:29:35 +00:00
|
|
|
Type = ELF::R_PPC64_GOT_TLSLD16_LO;
|
|
|
|
break;
|
2013-06-21 14:44:15 +00:00
|
|
|
case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HI:
|
|
|
|
Type = ELF::R_PPC64_GOT_TLSLD16_HI;
|
|
|
|
break;
|
2013-06-21 14:42:20 +00:00
|
|
|
case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HA:
|
2013-06-20 22:04:40 +00:00
|
|
|
Type = ELF::R_PPC64_GOT_TLSLD16_HA;
|
|
|
|
break;
|
2013-07-05 13:49:46 +00:00
|
|
|
case MCSymbolRefExpr::VK_PPC_GOT_TPREL:
|
|
|
|
/* We don't have R_PPC64_GOT_TPREL16, but since GOT offsets
|
|
|
|
are always 4-aligned, we can use R_PPC64_GOT_TPREL16_DS. */
|
|
|
|
Type = ELF::R_PPC64_GOT_TPREL16_DS;
|
|
|
|
break;
|
|
|
|
case MCSymbolRefExpr::VK_PPC_GOT_TPREL_LO:
|
|
|
|
/* We don't have R_PPC64_GOT_TPREL16_LO, but since GOT offsets
|
|
|
|
are always 4-aligned, we can use R_PPC64_GOT_TPREL16_LO_DS. */
|
|
|
|
Type = ELF::R_PPC64_GOT_TPREL16_LO_DS;
|
|
|
|
break;
|
2013-06-21 14:44:15 +00:00
|
|
|
case MCSymbolRefExpr::VK_PPC_GOT_TPREL_HI:
|
|
|
|
Type = ELF::R_PPC64_GOT_TPREL16_HI;
|
|
|
|
break;
|
2013-07-05 13:49:46 +00:00
|
|
|
case MCSymbolRefExpr::VK_PPC_GOT_DTPREL:
|
|
|
|
/* We don't have R_PPC64_GOT_DTPREL16, but since GOT offsets
|
|
|
|
are always 4-aligned, we can use R_PPC64_GOT_DTPREL16_DS. */
|
|
|
|
Type = ELF::R_PPC64_GOT_DTPREL16_DS;
|
|
|
|
break;
|
|
|
|
case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_LO:
|
|
|
|
/* We don't have R_PPC64_GOT_DTPREL16_LO, but since GOT offsets
|
|
|
|
are always 4-aligned, we can use R_PPC64_GOT_DTPREL16_LO_DS. */
|
|
|
|
Type = ELF::R_PPC64_GOT_DTPREL16_LO_DS;
|
|
|
|
break;
|
2013-06-21 14:42:20 +00:00
|
|
|
case MCSymbolRefExpr::VK_PPC_GOT_TPREL_HA:
|
2013-06-20 22:04:40 +00:00
|
|
|
Type = ELF::R_PPC64_GOT_TPREL16_HA;
|
|
|
|
break;
|
2013-06-21 14:44:15 +00:00
|
|
|
case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_HI:
|
|
|
|
Type = ELF::R_PPC64_GOT_DTPREL16_HI;
|
|
|
|
break;
|
|
|
|
case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_HA:
|
|
|
|
Type = ELF::R_PPC64_GOT_DTPREL16_HA;
|
|
|
|
break;
|
2012-11-13 19:24:36 +00:00
|
|
|
}
|
2011-12-22 01:57:09 +00:00
|
|
|
break;
|
2013-05-17 12:37:21 +00:00
|
|
|
case PPC::fixup_ppc_half16ds:
|
2013-02-21 00:05:29 +00:00
|
|
|
switch (Modifier) {
|
|
|
|
default: llvm_unreachable("Unsupported Modifier");
|
|
|
|
case MCSymbolRefExpr::VK_None:
|
PowerPC: Simplify handling of fixups.
MCTargetDesc/PPCMCCodeEmitter.cpp current has code like:
if (isSVR4ABI() && is64BitMode())
Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
(MCFixupKind)PPC::fixup_ppc_toc16));
else
Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
(MCFixupKind)PPC::fixup_ppc_lo16));
This is a problem for the asm parser, since it requires knowledge of
the ABI / 64-bit mode to be set up. However, more fundamentally,
at this point we shouldn't make such distinctions anyway; in an assembler
file, it always ought to be possible to e.g. generate TOC relocations even
when the main ABI is one that doesn't use TOC.
Fortunately, this is actually completely unnecessary; that code was added
to decide whether to generate TOC relocations, but that information is in
fact already encoded in the VariantKind of the underlying symbol.
This commit therefore merges those fixup types into one, and then decides
which relocation to use based on the VariantKind.
No changes in generated code.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@178007 91177308-0d34-0410-b5e6-96231b3b80d8
2013-03-26 10:56:47 +00:00
|
|
|
Type = ELF::R_PPC64_ADDR16_DS;
|
2013-02-21 00:05:29 +00:00
|
|
|
break;
|
2013-06-21 14:42:20 +00:00
|
|
|
case MCSymbolRefExpr::VK_PPC_LO:
|
2013-05-08 17:50:07 +00:00
|
|
|
Type = ELF::R_PPC64_ADDR16_LO_DS;
|
|
|
|
break;
|
2013-06-25 16:49:50 +00:00
|
|
|
case MCSymbolRefExpr::VK_GOT:
|
|
|
|
Type = ELF::R_PPC64_GOT16_DS;
|
|
|
|
break;
|
|
|
|
case MCSymbolRefExpr::VK_PPC_GOT_LO:
|
|
|
|
Type = ELF::R_PPC64_GOT16_LO_DS;
|
|
|
|
break;
|
2013-06-21 14:42:20 +00:00
|
|
|
case MCSymbolRefExpr::VK_PPC_TOC:
|
This patch implements medium code model support for 64-bit PowerPC.
The default for 64-bit PowerPC is small code model, in which TOC entries
must be addressable using a 16-bit offset from the TOC pointer. Additionally,
only TOC entries are addressed via the TOC pointer.
With medium code model, TOC entries and data sections can all be addressed
via the TOC pointer using a 32-bit offset. Cooperation with the linker
allows 16-bit offsets to be used when these are sufficient, reducing the
number of extra instructions that need to be executed. Medium code model
also does not generate explicit TOC entries in ".section toc" for variables
that are wholly internal to the compilation unit.
Consider a load of an external 4-byte integer. With small code model, the
compiler generates:
ld 3, .LC1@toc(2)
lwz 4, 0(3)
.section .toc,"aw",@progbits
.LC1:
.tc ei[TC],ei
With medium model, it instead generates:
addis 3, 2, .LC1@toc@ha
ld 3, .LC1@toc@l(3)
lwz 4, 0(3)
.section .toc,"aw",@progbits
.LC1:
.tc ei[TC],ei
Here .LC1@toc@ha is a relocation requesting the upper 16 bits of the
32-bit offset of ei's TOC entry from the TOC base pointer. Similarly,
.LC1@toc@l is a relocation requesting the lower 16 bits. Note that if
the linker determines that ei's TOC entry is within a 16-bit offset of
the TOC base pointer, it will replace the "addis" with a "nop", and
replace the "ld" with the identical "ld" instruction from the small
code model example.
Consider next a load of a function-scope static integer. For small code
model, the compiler generates:
ld 3, .LC1@toc(2)
lwz 4, 0(3)
.section .toc,"aw",@progbits
.LC1:
.tc test_fn_static.si[TC],test_fn_static.si
.type test_fn_static.si,@object
.local test_fn_static.si
.comm test_fn_static.si,4,4
For medium code model, the compiler generates:
addis 3, 2, test_fn_static.si@toc@ha
addi 3, 3, test_fn_static.si@toc@l
lwz 4, 0(3)
.type test_fn_static.si,@object
.local test_fn_static.si
.comm test_fn_static.si,4,4
Again, the linker may replace the "addis" with a "nop", calculating only
a 16-bit offset when this is sufficient.
Note that it would be more efficient for the compiler to generate:
addis 3, 2, test_fn_static.si@toc@ha
lwz 4, test_fn_static.si@toc@l(3)
The current patch does not perform this optimization yet. This will be
addressed as a peephole optimization in a later patch.
For the moment, the default code model for 64-bit PowerPC will remain the
small code model. We plan to eventually change the default to medium code
model, which matches current upstream GCC behavior. Note that the different
code models are ABI-compatible, so code compiled with different models will
be linked and execute correctly.
I've tested the regression suite and the application/benchmark test suite in
two ways: Once with the patch as submitted here, and once with additional
logic to force medium code model as the default. The tests all compile
cleanly, with one exception. The mandel-2 application test fails due to an
unrelated ABI compatibility with passing complex numbers. It just so happens
that small code model was incredibly lucky, in that temporary values in
floating-point registers held the expected values needed by the external
library routine that was called incorrectly. My current thought is to correct
the ABI problems with _Complex before making medium code model the default,
to avoid introducing this "regression."
Here are a few comments on how the patch works, since the selection code
can be difficult to follow:
The existing logic for small code model defines three pseudo-instructions:
LDtoc for most uses, LDtocJTI for jump table addresses, and LDtocCPT for
constant pool addresses. These are expanded by SelectCodeCommon(). The
pseudo-instruction approach doesn't work for medium code model, because
we need to generate two instructions when we match the same pattern.
Instead, new logic in PPCDAGToDAGISel::Select() intercepts the TOC_ENTRY
node for medium code model, and generates an ADDIStocHA followed by either
a LDtocL or an ADDItocL. These new node types correspond naturally to
the sequences described above.
The addis/ld sequence is generated for the following cases:
* Jump table addresses
* Function addresses
* External global variables
* Tentative definitions of global variables (common linkage)
The addis/addi sequence is generated for the following cases:
* Constant pool entries
* File-scope static global variables
* Function-scope static variables
Expanding to the two-instruction sequences at select time exposes the
instructions to subsequent optimization, particularly scheduling.
The rest of the processing occurs at assembly time, in
PPCAsmPrinter::EmitInstruction. Each of the instructions is converted to
a "real" PowerPC instruction. When a TOC entry needs to be created, this
is done here in the same manner as for the existing LDtoc, LDtocJTI, and
LDtocCPT pseudo-instructions (I factored out a new routine to handle this).
I had originally thought that if a TOC entry was needed for LDtocL or
ADDItocL, it would already have been generated for the previous ADDIStocHA.
However, at higher optimization levels, the ADDIStocHA may appear in a
different block, which may be assembled textually following the block
containing the LDtocL or ADDItocL. So it is necessary to include the
possibility of creating a new TOC entry for those two instructions.
Note that for LDtocL, we generate a new form of LD called LDrs. This
allows specifying the @toc@l relocation for the offset field of the LD
instruction (i.e., the offset is replaced by a SymbolLo relocation).
When the peephole optimization described above is added, we will need
to do similar things for all immediate-form load and store operations.
The seven "mcm-n.ll" test cases are kept separate because otherwise the
intermingling of various TOC entries and so forth makes the tests fragile
and hard to understand.
The above assumes use of an external assembler. For use of the
integrated assembler, new relocations are added and used by
PPCELFObjectWriter. Testing is done with "mcm-obj.ll", which tests for
proper generation of the various relocations for the same sequences
tested with the external assembler.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@168708 91177308-0d34-0410-b5e6-96231b3b80d8
2012-11-27 17:35:46 +00:00
|
|
|
Type = ELF::R_PPC64_TOC16_DS;
|
|
|
|
break;
|
2013-06-21 14:42:20 +00:00
|
|
|
case MCSymbolRefExpr::VK_PPC_TOC_LO:
|
This patch implements medium code model support for 64-bit PowerPC.
The default for 64-bit PowerPC is small code model, in which TOC entries
must be addressable using a 16-bit offset from the TOC pointer. Additionally,
only TOC entries are addressed via the TOC pointer.
With medium code model, TOC entries and data sections can all be addressed
via the TOC pointer using a 32-bit offset. Cooperation with the linker
allows 16-bit offsets to be used when these are sufficient, reducing the
number of extra instructions that need to be executed. Medium code model
also does not generate explicit TOC entries in ".section toc" for variables
that are wholly internal to the compilation unit.
Consider a load of an external 4-byte integer. With small code model, the
compiler generates:
ld 3, .LC1@toc(2)
lwz 4, 0(3)
.section .toc,"aw",@progbits
.LC1:
.tc ei[TC],ei
With medium model, it instead generates:
addis 3, 2, .LC1@toc@ha
ld 3, .LC1@toc@l(3)
lwz 4, 0(3)
.section .toc,"aw",@progbits
.LC1:
.tc ei[TC],ei
Here .LC1@toc@ha is a relocation requesting the upper 16 bits of the
32-bit offset of ei's TOC entry from the TOC base pointer. Similarly,
.LC1@toc@l is a relocation requesting the lower 16 bits. Note that if
the linker determines that ei's TOC entry is within a 16-bit offset of
the TOC base pointer, it will replace the "addis" with a "nop", and
replace the "ld" with the identical "ld" instruction from the small
code model example.
Consider next a load of a function-scope static integer. For small code
model, the compiler generates:
ld 3, .LC1@toc(2)
lwz 4, 0(3)
.section .toc,"aw",@progbits
.LC1:
.tc test_fn_static.si[TC],test_fn_static.si
.type test_fn_static.si,@object
.local test_fn_static.si
.comm test_fn_static.si,4,4
For medium code model, the compiler generates:
addis 3, 2, test_fn_static.si@toc@ha
addi 3, 3, test_fn_static.si@toc@l
lwz 4, 0(3)
.type test_fn_static.si,@object
.local test_fn_static.si
.comm test_fn_static.si,4,4
Again, the linker may replace the "addis" with a "nop", calculating only
a 16-bit offset when this is sufficient.
Note that it would be more efficient for the compiler to generate:
addis 3, 2, test_fn_static.si@toc@ha
lwz 4, test_fn_static.si@toc@l(3)
The current patch does not perform this optimization yet. This will be
addressed as a peephole optimization in a later patch.
For the moment, the default code model for 64-bit PowerPC will remain the
small code model. We plan to eventually change the default to medium code
model, which matches current upstream GCC behavior. Note that the different
code models are ABI-compatible, so code compiled with different models will
be linked and execute correctly.
I've tested the regression suite and the application/benchmark test suite in
two ways: Once with the patch as submitted here, and once with additional
logic to force medium code model as the default. The tests all compile
cleanly, with one exception. The mandel-2 application test fails due to an
unrelated ABI compatibility with passing complex numbers. It just so happens
that small code model was incredibly lucky, in that temporary values in
floating-point registers held the expected values needed by the external
library routine that was called incorrectly. My current thought is to correct
the ABI problems with _Complex before making medium code model the default,
to avoid introducing this "regression."
Here are a few comments on how the patch works, since the selection code
can be difficult to follow:
The existing logic for small code model defines three pseudo-instructions:
LDtoc for most uses, LDtocJTI for jump table addresses, and LDtocCPT for
constant pool addresses. These are expanded by SelectCodeCommon(). The
pseudo-instruction approach doesn't work for medium code model, because
we need to generate two instructions when we match the same pattern.
Instead, new logic in PPCDAGToDAGISel::Select() intercepts the TOC_ENTRY
node for medium code model, and generates an ADDIStocHA followed by either
a LDtocL or an ADDItocL. These new node types correspond naturally to
the sequences described above.
The addis/ld sequence is generated for the following cases:
* Jump table addresses
* Function addresses
* External global variables
* Tentative definitions of global variables (common linkage)
The addis/addi sequence is generated for the following cases:
* Constant pool entries
* File-scope static global variables
* Function-scope static variables
Expanding to the two-instruction sequences at select time exposes the
instructions to subsequent optimization, particularly scheduling.
The rest of the processing occurs at assembly time, in
PPCAsmPrinter::EmitInstruction. Each of the instructions is converted to
a "real" PowerPC instruction. When a TOC entry needs to be created, this
is done here in the same manner as for the existing LDtoc, LDtocJTI, and
LDtocCPT pseudo-instructions (I factored out a new routine to handle this).
I had originally thought that if a TOC entry was needed for LDtocL or
ADDItocL, it would already have been generated for the previous ADDIStocHA.
However, at higher optimization levels, the ADDIStocHA may appear in a
different block, which may be assembled textually following the block
containing the LDtocL or ADDItocL. So it is necessary to include the
possibility of creating a new TOC entry for those two instructions.
Note that for LDtocL, we generate a new form of LD called LDrs. This
allows specifying the @toc@l relocation for the offset field of the LD
instruction (i.e., the offset is replaced by a SymbolLo relocation).
When the peephole optimization described above is added, we will need
to do similar things for all immediate-form load and store operations.
The seven "mcm-n.ll" test cases are kept separate because otherwise the
intermingling of various TOC entries and so forth makes the tests fragile
and hard to understand.
The above assumes use of an external assembler. For use of the
integrated assembler, new relocations are added and used by
PPCELFObjectWriter. Testing is done with "mcm-obj.ll", which tests for
proper generation of the various relocations for the same sequences
tested with the external assembler.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@168708 91177308-0d34-0410-b5e6-96231b3b80d8
2012-11-27 17:35:46 +00:00
|
|
|
Type = ELF::R_PPC64_TOC16_LO_DS;
|
|
|
|
break;
|
2013-06-21 14:44:15 +00:00
|
|
|
case MCSymbolRefExpr::VK_PPC_TPREL:
|
|
|
|
Type = ELF::R_PPC64_TPREL16_DS;
|
|
|
|
break;
|
|
|
|
case MCSymbolRefExpr::VK_PPC_TPREL_LO:
|
|
|
|
Type = ELF::R_PPC64_TPREL16_LO_DS;
|
|
|
|
break;
|
|
|
|
case MCSymbolRefExpr::VK_PPC_DTPREL:
|
|
|
|
Type = ELF::R_PPC64_DTPREL16_DS;
|
|
|
|
break;
|
|
|
|
case MCSymbolRefExpr::VK_PPC_DTPREL_LO:
|
|
|
|
Type = ELF::R_PPC64_DTPREL16_LO_DS;
|
|
|
|
break;
|
|
|
|
case MCSymbolRefExpr::VK_PPC_GOT_TPREL:
|
|
|
|
Type = ELF::R_PPC64_GOT_TPREL16_DS;
|
|
|
|
break;
|
2013-06-21 14:42:20 +00:00
|
|
|
case MCSymbolRefExpr::VK_PPC_GOT_TPREL_LO:
|
This patch improves the 64-bit PowerPC InitialExec TLS support by providing
for a wider range of GOT entries that can hold thread-relative offsets.
This matches the behavior of GCC, which was not documented in the PPC64 TLS
ABI. The ABI will be updated with the new code sequence.
Former sequence:
ld 9,x@got@tprel(2)
add 9,9,x@tls
New sequence:
addis 9,2,x@got@tprel@ha
ld 9,x@got@tprel@l(9)
add 9,9,x@tls
Note that a linker optimization exists to transform the new sequence into
the shorter sequence when appropriate, by replacing the addis with a nop
and modifying the base register and relocation type of the ld.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@170209 91177308-0d34-0410-b5e6-96231b3b80d8
2012-12-14 17:02:38 +00:00
|
|
|
Type = ELF::R_PPC64_GOT_TPREL16_LO_DS;
|
2012-12-04 16:18:08 +00:00
|
|
|
break;
|
2013-06-21 14:44:15 +00:00
|
|
|
case MCSymbolRefExpr::VK_PPC_GOT_DTPREL:
|
|
|
|
Type = ELF::R_PPC64_GOT_DTPREL16_DS;
|
|
|
|
break;
|
|
|
|
case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_LO:
|
|
|
|
Type = ELF::R_PPC64_GOT_DTPREL16_LO_DS;
|
|
|
|
break;
|
This patch implements medium code model support for 64-bit PowerPC.
The default for 64-bit PowerPC is small code model, in which TOC entries
must be addressable using a 16-bit offset from the TOC pointer. Additionally,
only TOC entries are addressed via the TOC pointer.
With medium code model, TOC entries and data sections can all be addressed
via the TOC pointer using a 32-bit offset. Cooperation with the linker
allows 16-bit offsets to be used when these are sufficient, reducing the
number of extra instructions that need to be executed. Medium code model
also does not generate explicit TOC entries in ".section toc" for variables
that are wholly internal to the compilation unit.
Consider a load of an external 4-byte integer. With small code model, the
compiler generates:
ld 3, .LC1@toc(2)
lwz 4, 0(3)
.section .toc,"aw",@progbits
.LC1:
.tc ei[TC],ei
With medium model, it instead generates:
addis 3, 2, .LC1@toc@ha
ld 3, .LC1@toc@l(3)
lwz 4, 0(3)
.section .toc,"aw",@progbits
.LC1:
.tc ei[TC],ei
Here .LC1@toc@ha is a relocation requesting the upper 16 bits of the
32-bit offset of ei's TOC entry from the TOC base pointer. Similarly,
.LC1@toc@l is a relocation requesting the lower 16 bits. Note that if
the linker determines that ei's TOC entry is within a 16-bit offset of
the TOC base pointer, it will replace the "addis" with a "nop", and
replace the "ld" with the identical "ld" instruction from the small
code model example.
Consider next a load of a function-scope static integer. For small code
model, the compiler generates:
ld 3, .LC1@toc(2)
lwz 4, 0(3)
.section .toc,"aw",@progbits
.LC1:
.tc test_fn_static.si[TC],test_fn_static.si
.type test_fn_static.si,@object
.local test_fn_static.si
.comm test_fn_static.si,4,4
For medium code model, the compiler generates:
addis 3, 2, test_fn_static.si@toc@ha
addi 3, 3, test_fn_static.si@toc@l
lwz 4, 0(3)
.type test_fn_static.si,@object
.local test_fn_static.si
.comm test_fn_static.si,4,4
Again, the linker may replace the "addis" with a "nop", calculating only
a 16-bit offset when this is sufficient.
Note that it would be more efficient for the compiler to generate:
addis 3, 2, test_fn_static.si@toc@ha
lwz 4, test_fn_static.si@toc@l(3)
The current patch does not perform this optimization yet. This will be
addressed as a peephole optimization in a later patch.
For the moment, the default code model for 64-bit PowerPC will remain the
small code model. We plan to eventually change the default to medium code
model, which matches current upstream GCC behavior. Note that the different
code models are ABI-compatible, so code compiled with different models will
be linked and execute correctly.
I've tested the regression suite and the application/benchmark test suite in
two ways: Once with the patch as submitted here, and once with additional
logic to force medium code model as the default. The tests all compile
cleanly, with one exception. The mandel-2 application test fails due to an
unrelated ABI compatibility with passing complex numbers. It just so happens
that small code model was incredibly lucky, in that temporary values in
floating-point registers held the expected values needed by the external
library routine that was called incorrectly. My current thought is to correct
the ABI problems with _Complex before making medium code model the default,
to avoid introducing this "regression."
Here are a few comments on how the patch works, since the selection code
can be difficult to follow:
The existing logic for small code model defines three pseudo-instructions:
LDtoc for most uses, LDtocJTI for jump table addresses, and LDtocCPT for
constant pool addresses. These are expanded by SelectCodeCommon(). The
pseudo-instruction approach doesn't work for medium code model, because
we need to generate two instructions when we match the same pattern.
Instead, new logic in PPCDAGToDAGISel::Select() intercepts the TOC_ENTRY
node for medium code model, and generates an ADDIStocHA followed by either
a LDtocL or an ADDItocL. These new node types correspond naturally to
the sequences described above.
The addis/ld sequence is generated for the following cases:
* Jump table addresses
* Function addresses
* External global variables
* Tentative definitions of global variables (common linkage)
The addis/addi sequence is generated for the following cases:
* Constant pool entries
* File-scope static global variables
* Function-scope static variables
Expanding to the two-instruction sequences at select time exposes the
instructions to subsequent optimization, particularly scheduling.
The rest of the processing occurs at assembly time, in
PPCAsmPrinter::EmitInstruction. Each of the instructions is converted to
a "real" PowerPC instruction. When a TOC entry needs to be created, this
is done here in the same manner as for the existing LDtoc, LDtocJTI, and
LDtocCPT pseudo-instructions (I factored out a new routine to handle this).
I had originally thought that if a TOC entry was needed for LDtocL or
ADDItocL, it would already have been generated for the previous ADDIStocHA.
However, at higher optimization levels, the ADDIStocHA may appear in a
different block, which may be assembled textually following the block
containing the LDtocL or ADDItocL. So it is necessary to include the
possibility of creating a new TOC entry for those two instructions.
Note that for LDtocL, we generate a new form of LD called LDrs. This
allows specifying the @toc@l relocation for the offset field of the LD
instruction (i.e., the offset is replaced by a SymbolLo relocation).
When the peephole optimization described above is added, we will need
to do similar things for all immediate-form load and store operations.
The seven "mcm-n.ll" test cases are kept separate because otherwise the
intermingling of various TOC entries and so forth makes the tests fragile
and hard to understand.
The above assumes use of an external assembler. For use of the
integrated assembler, new relocations are added and used by
PPCELFObjectWriter. Testing is done with "mcm-obj.ll", which tests for
proper generation of the various relocations for the same sequences
tested with the external assembler.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@168708 91177308-0d34-0410-b5e6-96231b3b80d8
2012-11-27 17:35:46 +00:00
|
|
|
}
|
2012-10-25 12:27:42 +00:00
|
|
|
break;
|
2012-12-12 19:29:35 +00:00
|
|
|
case PPC::fixup_ppc_nofixup:
|
|
|
|
switch (Modifier) {
|
|
|
|
default: llvm_unreachable("Unsupported Modifier");
|
[PowerPC] Revert r185476 and fix up TLS variant kinds
In the commit message to r185476 I wrote:
>The PowerPC-specific modifiers VK_PPC_TLSGD and VK_PPC_TLSLD
>correspond exactly to the generic modifiers VK_TLSGD and VK_TLSLD.
>This causes some confusion with the asm parser, since VK_PPC_TLSGD
>is output as @tlsgd, which is then read back in as VK_TLSGD.
>
>To avoid this confusion, this patch removes the PowerPC-specific
>modifiers and uses the generic modifiers throughout. (The only
>drawback is that the generic modifiers are printed in upper case
>while the usual convention on PowerPC is to use lower-case modifiers.
>But this is just a cosmetic issue.)
This was unfortunately incorrect, there is is fact another,
serious drawback to using the default VK_TLSLD/VK_TLSGD
variant kinds: using these causes ELFObjectWriter::RelocNeedsGOT
to return true, which in turn causes the ELFObjectWriter to emit
an undefined reference to _GLOBAL_OFFSET_TABLE_.
This is a problem on powerpc64, because it uses the TOC instead
of the GOT, and the linker does not provide _GLOBAL_OFFSET_TABLE_,
so the symbol remains undefined. This means shared libraries
using TLS built with the integrated assembler are currently
broken.
While the whole RelocNeedsGOT / _GLOBAL_OFFSET_TABLE_ situation
probably ought to be properly fixed at some point, for now I'm
simply reverting the r185476 commit. Now this in turn exposes
the breakage of handling @tlsgd/@tlsld in the asm parser that
this check-in was originally intended to fix.
To avoid this regression, I'm also adding a different fix for
this problem: while common code now parses @tlsgd as VK_TLSGD,
a special hack in the asm parser translates this code to the
platform-specific VK_PPC_TLSGD that the back-end now expects.
While this is not really pretty, it's self-contained and
shouldn't hurt anything else for now. One the underlying
problem is fixed, this hack can be reverted again.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@185945 91177308-0d34-0410-b5e6-96231b3b80d8
2013-07-09 16:41:09 +00:00
|
|
|
case MCSymbolRefExpr::VK_PPC_TLSGD:
|
2012-12-12 19:29:35 +00:00
|
|
|
Type = ELF::R_PPC64_TLSGD;
|
|
|
|
break;
|
[PowerPC] Revert r185476 and fix up TLS variant kinds
In the commit message to r185476 I wrote:
>The PowerPC-specific modifiers VK_PPC_TLSGD and VK_PPC_TLSLD
>correspond exactly to the generic modifiers VK_TLSGD and VK_TLSLD.
>This causes some confusion with the asm parser, since VK_PPC_TLSGD
>is output as @tlsgd, which is then read back in as VK_TLSGD.
>
>To avoid this confusion, this patch removes the PowerPC-specific
>modifiers and uses the generic modifiers throughout. (The only
>drawback is that the generic modifiers are printed in upper case
>while the usual convention on PowerPC is to use lower-case modifiers.
>But this is just a cosmetic issue.)
This was unfortunately incorrect, there is is fact another,
serious drawback to using the default VK_TLSLD/VK_TLSGD
variant kinds: using these causes ELFObjectWriter::RelocNeedsGOT
to return true, which in turn causes the ELFObjectWriter to emit
an undefined reference to _GLOBAL_OFFSET_TABLE_.
This is a problem on powerpc64, because it uses the TOC instead
of the GOT, and the linker does not provide _GLOBAL_OFFSET_TABLE_,
so the symbol remains undefined. This means shared libraries
using TLS built with the integrated assembler are currently
broken.
While the whole RelocNeedsGOT / _GLOBAL_OFFSET_TABLE_ situation
probably ought to be properly fixed at some point, for now I'm
simply reverting the r185476 commit. Now this in turn exposes
the breakage of handling @tlsgd/@tlsld in the asm parser that
this check-in was originally intended to fix.
To avoid this regression, I'm also adding a different fix for
this problem: while common code now parses @tlsgd as VK_TLSGD,
a special hack in the asm parser translates this code to the
platform-specific VK_PPC_TLSGD that the back-end now expects.
While this is not really pretty, it's self-contained and
shouldn't hurt anything else for now. One the underlying
problem is fixed, this hack can be reverted again.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@185945 91177308-0d34-0410-b5e6-96231b3b80d8
2013-07-09 16:41:09 +00:00
|
|
|
case MCSymbolRefExpr::VK_PPC_TLSLD:
|
2012-12-12 19:29:35 +00:00
|
|
|
Type = ELF::R_PPC64_TLSLD;
|
|
|
|
break;
|
2013-07-05 12:22:36 +00:00
|
|
|
case MCSymbolRefExpr::VK_PPC_TLS:
|
|
|
|
Type = ELF::R_PPC64_TLS;
|
|
|
|
break;
|
2012-12-12 19:29:35 +00:00
|
|
|
}
|
This patch implements the general dynamic TLS model for 64-bit PowerPC.
Given a thread-local symbol x with global-dynamic access, the generated
code to obtain x's address is:
Instruction Relocation Symbol
addis ra,r2,x@got@tlsgd@ha R_PPC64_GOT_TLSGD16_HA x
addi r3,ra,x@got@tlsgd@l R_PPC64_GOT_TLSGD16_L x
bl __tls_get_addr(x@tlsgd) R_PPC64_TLSGD x
R_PPC64_REL24 __tls_get_addr
nop
<use address in r3>
The implementation borrows from the medium code model work for introducing
special forms of ADDIS and ADDI into the DAG representation. This is made
slightly more complicated by having to introduce a call to the external
function __tls_get_addr. Using the full call machinery is overkill and,
more importantly, makes it difficult to add a special relocation. So I've
introduced another opcode GET_TLS_ADDR to represent the function call, and
surrounded it with register copies to set up the parameter and return value.
Most of the code is pretty straightforward. I ran into one peculiarity
when I introduced a new PPC opcode BL8_NOP_ELF_TLSGD, which is just like
BL8_NOP_ELF except that it takes another parameter to represent the symbol
("x" above) that requires a relocation on the call. Something in the
TblGen machinery causes BL8_NOP_ELF and BL8_NOP_ELF_TLSGD to be treated
identically during the emit phase, so this second operand was never
visited to generate relocations. This is the reason for the slightly
messy workaround in PPCMCCodeEmitter.cpp:getDirectBrEncoding().
Two new tests are included to demonstrate correct external assembly and
correct generation of relocations using the integrated assembler.
Comments welcome!
Thanks,
Bill
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@169910 91177308-0d34-0410-b5e6-96231b3b80d8
2012-12-11 20:30:11 +00:00
|
|
|
break;
|
2012-10-25 12:27:42 +00:00
|
|
|
case FK_Data_8:
|
|
|
|
switch (Modifier) {
|
|
|
|
default: llvm_unreachable("Unsupported Modifier");
|
2013-06-20 22:39:42 +00:00
|
|
|
case MCSymbolRefExpr::VK_PPC_TOCBASE:
|
2012-10-25 12:27:42 +00:00
|
|
|
Type = ELF::R_PPC64_TOC;
|
|
|
|
break;
|
|
|
|
case MCSymbolRefExpr::VK_None:
|
|
|
|
Type = ELF::R_PPC64_ADDR64;
|
|
|
|
break;
|
2013-07-01 23:33:29 +00:00
|
|
|
case MCSymbolRefExpr::VK_PPC_DTPMOD:
|
|
|
|
Type = ELF::R_PPC64_DTPMOD64;
|
|
|
|
break;
|
|
|
|
case MCSymbolRefExpr::VK_PPC_TPREL:
|
|
|
|
Type = ELF::R_PPC64_TPREL64;
|
|
|
|
break;
|
|
|
|
case MCSymbolRefExpr::VK_PPC_DTPREL:
|
|
|
|
Type = ELF::R_PPC64_DTPREL64;
|
|
|
|
break;
|
2012-10-25 12:27:42 +00:00
|
|
|
}
|
|
|
|
break;
|
2011-12-22 01:57:09 +00:00
|
|
|
case FK_Data_4:
|
|
|
|
Type = ELF::R_PPC_ADDR32;
|
|
|
|
break;
|
|
|
|
case FK_Data_2:
|
|
|
|
Type = ELF::R_PPC_ADDR16;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Type;
|
|
|
|
}
|
|
|
|
|
2012-10-25 12:27:42 +00:00
|
|
|
unsigned PPCELFObjectWriter::GetRelocType(const MCValue &Target,
|
|
|
|
const MCFixup &Fixup,
|
|
|
|
bool IsPCRel,
|
|
|
|
bool IsRelocWithSymbol,
|
|
|
|
int64_t Addend) const {
|
|
|
|
return getRelocTypeInner(Target, Fixup, IsPCRel);
|
|
|
|
}
|
|
|
|
|
2013-07-01 18:19:56 +00:00
|
|
|
const MCSymbol *PPCELFObjectWriter::ExplicitRelSym(const MCAssembler &Asm,
|
|
|
|
const MCValue &Target,
|
|
|
|
const MCFragment &F,
|
|
|
|
const MCFixup &Fixup,
|
|
|
|
bool IsPCRel) const {
|
|
|
|
assert(Target.getSymA() && "SymA cannot be 0");
|
|
|
|
MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
|
|
|
|
MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
|
|
|
|
|
|
|
|
bool EmitThisSym;
|
|
|
|
switch (Modifier) {
|
|
|
|
// GOT references always need a relocation, even if the
|
|
|
|
// target symbol is local.
|
|
|
|
case MCSymbolRefExpr::VK_GOT:
|
|
|
|
case MCSymbolRefExpr::VK_PPC_GOT_LO:
|
|
|
|
case MCSymbolRefExpr::VK_PPC_GOT_HI:
|
|
|
|
case MCSymbolRefExpr::VK_PPC_GOT_HA:
|
|
|
|
EmitThisSym = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
EmitThisSym = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (EmitThisSym)
|
|
|
|
return &Target.getSymA()->getSymbol().AliasedSymbol();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-10-25 12:27:42 +00:00
|
|
|
const MCSymbol *PPCELFObjectWriter::undefinedExplicitRelSym(const MCValue &Target,
|
|
|
|
const MCFixup &Fixup,
|
|
|
|
bool IsPCRel) const {
|
|
|
|
assert(Target.getSymA() && "SymA cannot be 0");
|
|
|
|
const MCSymbol &Symbol = Target.getSymA()->getSymbol().AliasedSymbol();
|
|
|
|
|
|
|
|
unsigned RelocType = getRelocTypeInner(Target, Fixup, IsPCRel);
|
|
|
|
|
|
|
|
// The .odp creation emits a relocation against the symbol ".TOC." which
|
|
|
|
// create a R_PPC64_TOC relocation. However the relocation symbol name
|
|
|
|
// in final object creation should be NULL, since the symbol does not
|
|
|
|
// really exist, it is just the reference to TOC base for the current
|
|
|
|
// object file.
|
|
|
|
bool EmitThisSym = RelocType != ELF::R_PPC64_TOC;
|
|
|
|
|
|
|
|
if (EmitThisSym && !Symbol.isTemporary())
|
|
|
|
return &Symbol;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-12-22 01:57:09 +00:00
|
|
|
MCObjectWriter *llvm::createPPCELFObjectWriter(raw_ostream &OS,
|
|
|
|
bool Is64Bit,
|
|
|
|
uint8_t OSABI) {
|
|
|
|
MCELFObjectTargetWriter *MOTW = new PPCELFObjectWriter(Is64Bit, OSABI);
|
2011-12-22 18:38:06 +00:00
|
|
|
return createELFObjectWriter(MOTW, OS, /*IsLittleEndian=*/false);
|
2011-12-22 01:57:09 +00:00
|
|
|
}
|