llvm-6502/lib/MC
Bill Schmidt 34a9d4b3b9 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
..
MCDisassembler libLTO: Add a utility method to initialize the disassemblers. 2012-11-24 16:59:10 +00:00
MCParser Add support for .cfi_register now that it is easy to extent the representation 2012-11-25 15:14:49 +00:00
CMakeLists.txt Give MCRegisterInfo an implementation file. 2012-07-27 16:25:20 +00:00
ELFObjectWriter.cpp Initial TOC support for PowerPC64 object creation 2012-10-25 12:27:42 +00:00
LLVMBuild.txt LLVMBuild: Introduce a common section which currently has a list of the 2011-12-12 22:45:54 +00:00
MachObjectWriter.cpp Tidy up. 80 columns. 2012-09-18 23:05:12 +00:00
Makefile start straightening out libedis's dependencies and make it fit 2010-07-20 18:25:19 +00:00
MCAsmBackend.cpp MachO: direct-to-object attribute for data-in-code markers. 2012-10-01 22:20:54 +00:00
MCAsmInfo.cpp Fix alignment of .comm and .lcomm on mingw32. 2012-09-07 21:08:01 +00:00
MCAsmInfoCOFF.cpp Fix alignment of .comm and .lcomm on mingw32. 2012-09-07 21:08:01 +00:00
MCAsmInfoDarwin.cpp Fix alignment of .comm and .lcomm on mingw32. 2012-09-07 21:08:01 +00:00
MCAsmStreamer.cpp Add support for .cfi_register now that it is easy to extent the representation 2012-11-25 15:14:49 +00:00
MCAssembler.cpp Tidy up. Minor formatting. 2012-09-18 23:05:18 +00:00
MCAtom.cpp MCAtom extending methods need to extend the range of the atom as well. 2011-10-10 18:09:38 +00:00
MCCodeEmitter.cpp MC: Move target specific fixup info descriptors to TargetAsmBackend instead of 2010-12-16 03:20:06 +00:00
MCCodeGenInfo.cpp Sink codegen optimization level into MCCodeGenInfo along side relocation model 2011-11-16 08:38:26 +00:00
MCContext.cpp Avoid symbol name clash when filling TOC. 2012-09-18 17:10:37 +00:00
MCDisassembler.cpp
MCDwarf.cpp Add support for .cfi_register now that it is easy to extent the representation 2012-11-25 15:14:49 +00:00
MCELF.cpp Add support for gnu_indirect_function. 2011-12-12 17:34:04 +00:00
MCELF.h Make all static functions become static class methods. Move shared (duplicated) functions to new MCELF class. 2011-02-28 21:45:04 +00:00
MCELFObjectTargetWriter.cpp Initial TOC support for PowerPC64 object creation 2012-10-25 12:27:42 +00:00
MCELFStreamer.cpp PowerPC: add EmitTCEntry class for TOC creation 2012-10-15 15:43:14 +00:00
MCExpr.cpp This patch implements medium code model support for 64-bit PowerPC. 2012-11-27 17:35:46 +00:00
MCInst.cpp Release build: guard dump functions with 2012-09-12 05:06:18 +00:00
MCInstPrinter.cpp Make branch heavy code for generating marked up disassembly simpler 2012-10-23 22:52:52 +00:00
MCInstrAnalysis.cpp MCInstrAnalysis: Don't crash on instructions with no operands. 2011-09-19 17:56:00 +00:00
MCLabel.cpp Release build: guard dump functions with 2012-09-12 05:06:18 +00:00
MCMachObjectTargetWriter.cpp MC/Mach-O: On second thought, use a custom hook for enabling aggressive 2010-12-17 05:50:29 +00:00
MCMachOStreamer.cpp Hoist some grossly duplicated code from the COFF/ELF/MachO streamers into MCObjectStreamer. 2012-10-04 13:12:43 +00:00
MCModule.cpp Tidy up. 80 columns. 2011-11-15 16:46:22 +00:00
MCNullStreamer.cpp EmitZerofill should take a 64-bit size or else it's chopping off large zero-filled global. rdar://11729134 2012-06-22 20:14:46 +00:00
MCObjectFileInfo.cpp Add names for the accelerator table sections so that they can 2012-10-08 21:41:30 +00:00
MCObjectStreamer.cpp Hoist some grossly duplicated code from the COFF/ELF/MachO streamers into MCObjectStreamer. 2012-10-04 13:12:43 +00:00
MCObjectWriter.cpp Move [SU]LEB128 encoding to a utility header. 2012-08-08 23:56:06 +00:00
MCPureStreamer.cpp EmitZerofill should take a 64-bit size or else it's chopping off large zero-filled global. rdar://11729134 2012-06-22 20:14:46 +00:00
MCRegisterInfo.cpp Add MCRI::getNumSubRegIndices() and start checking SubRegIndex ranges. 2012-09-11 16:34:02 +00:00
MCSection.cpp
MCSectionCOFF.cpp Tidy up. Trailing whitespace. 2012-05-11 01:41:30 +00:00
MCSectionELF.cpp Tidy up. Trailing whitespace. 2012-05-11 01:41:30 +00:00
MCSectionMachO.cpp Reapply 127939 since Daniel fixed the breakage. <rdar://problem/9012638> 2011-03-19 02:42:31 +00:00
MCStreamer.cpp Add support for .cfi_register now that it is easy to extent the representation 2012-11-25 15:14:49 +00:00
MCSubtargetInfo.cpp Fix doxygen comment to match function name. 2012-10-03 06:47:18 +00:00
MCSymbol.cpp Fix Doxygen issues: 2012-09-14 14:57:36 +00:00
MCTargetAsmLexer.cpp Rename TargetAsmParser to MCTargetAsmParser and TargetAsmLexer to MCTargetAsmLexer; rename createAsmLexer to createMCAsmLexer and createAsmParser to createMCAsmParser. 2011-07-26 00:24:13 +00:00
MCValue.cpp Release build: guard dump functions with 2012-09-12 05:06:18 +00:00
MCWin64EH.cpp Fix for .pdata and .xdata section attributes on COFF. 2012-08-08 12:46:46 +00:00
SubtargetFeature.cpp Symbol hygiene: Make sure declarations and definitions match, make helper functions static. 2012-10-20 12:53:26 +00:00
WinCOFFObjectWriter.cpp Revert 'Fix a typo 'iff' => 'if''. iff is an abreviation of if and only if. See: http://en.wikipedia.org/wiki/If_and_only_if Commit 164767 2012-09-27 10:14:43 +00:00
WinCOFFStreamer.cpp Hoist some grossly duplicated code from the COFF/ELF/MachO streamers into MCObjectStreamer. 2012-10-04 13:12:43 +00:00