mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2024-08-25 16:29:20 +00:00
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
194 lines
5.3 KiB
LLVM
194 lines
5.3 KiB
LLVM
; RUN: llc -O0 -mcpu=pwr7 -code-model=medium -filetype=obj %s -o - | \
|
|
; RUN: elf-dump --dump-section-data | FileCheck %s
|
|
|
|
; FIXME: When asm-parse is available, could make this an assembly test.
|
|
|
|
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"
|
|
|
|
@ei = external global i32
|
|
|
|
define signext i32 @test_external() nounwind {
|
|
entry:
|
|
%0 = load i32* @ei, align 4
|
|
%inc = add nsw i32 %0, 1
|
|
store i32 %inc, i32* @ei, align 4
|
|
ret i32 %0
|
|
}
|
|
|
|
; Verify generation of R_PPC64_TOC16_HA and R_PPC64_TOC16_LO_DS for
|
|
; accessing external variable ei.
|
|
;
|
|
; CHECK: '.rela.text'
|
|
; CHECK: Relocation 0
|
|
; CHECK-NEXT: 'r_offset'
|
|
; CHECK-NEXT: 'r_sym', 0x[[SYM1:[0-9]+]]
|
|
; CHECK-NEXT: 'r_type', 0x00000032
|
|
; CHECK: Relocation 1
|
|
; CHECK-NEXT: 'r_offset'
|
|
; CHECK-NEXT: 'r_sym', 0x[[SYM1]]
|
|
; CHECK-NEXT: 'r_type', 0x00000040
|
|
|
|
@test_fn_static.si = internal global i32 0, align 4
|
|
|
|
define signext i32 @test_fn_static() nounwind {
|
|
entry:
|
|
%0 = load i32* @test_fn_static.si, align 4
|
|
%inc = add nsw i32 %0, 1
|
|
store i32 %inc, i32* @test_fn_static.si, align 4
|
|
ret i32 %0
|
|
}
|
|
|
|
; Verify generation of R_PPC64_TOC16_HA and R_PPC64_TOC16_LO for
|
|
; accessing function-scoped variable si.
|
|
;
|
|
; CHECK: Relocation 2
|
|
; CHECK-NEXT: 'r_offset'
|
|
; CHECK-NEXT: 'r_sym', 0x[[SYM2:[0-9]+]]
|
|
; CHECK-NEXT: 'r_type', 0x00000032
|
|
; CHECK: Relocation 3
|
|
; CHECK-NEXT: 'r_offset'
|
|
; CHECK-NEXT: 'r_sym', 0x[[SYM2]]
|
|
; CHECK-NEXT: 'r_type', 0x00000030
|
|
|
|
@gi = global i32 5, align 4
|
|
|
|
define signext i32 @test_file_static() nounwind {
|
|
entry:
|
|
%0 = load i32* @gi, align 4
|
|
%inc = add nsw i32 %0, 1
|
|
store i32 %inc, i32* @gi, align 4
|
|
ret i32 %0
|
|
}
|
|
|
|
; Verify generation of R_PPC64_TOC16_HA and R_PPC64_TOC16_LO for
|
|
; accessing file-scope variable gi.
|
|
;
|
|
; CHECK: Relocation 4
|
|
; CHECK-NEXT: 'r_offset'
|
|
; CHECK-NEXT: 'r_sym', 0x[[SYM3:[0-9]+]]
|
|
; CHECK-NEXT: 'r_type', 0x00000032
|
|
; CHECK: Relocation 5
|
|
; CHECK-NEXT: 'r_offset'
|
|
; CHECK-NEXT: 'r_sym', 0x[[SYM3]]
|
|
; CHECK-NEXT: 'r_type', 0x00000030
|
|
|
|
define double @test_double_const() nounwind {
|
|
entry:
|
|
ret double 0x3F4FD4920B498CF0
|
|
}
|
|
|
|
; Verify generation of R_PPC64_TOC16_HA and R_PPC64_TOC16_LO for
|
|
; accessing a constant.
|
|
;
|
|
; CHECK: Relocation 6
|
|
; CHECK-NEXT: 'r_offset'
|
|
; CHECK-NEXT: 'r_sym', 0x[[SYM4:[0-9]+]]
|
|
; CHECK-NEXT: 'r_type', 0x00000032
|
|
; CHECK: Relocation 7
|
|
; CHECK-NEXT: 'r_offset'
|
|
; CHECK-NEXT: 'r_sym', 0x[[SYM4]]
|
|
; CHECK-NEXT: 'r_type', 0x00000030
|
|
|
|
define signext i32 @test_jump_table(i32 signext %i) nounwind {
|
|
entry:
|
|
%i.addr = alloca i32, align 4
|
|
store i32 %i, i32* %i.addr, align 4
|
|
%0 = load i32* %i.addr, align 4
|
|
switch i32 %0, label %sw.default [
|
|
i32 3, label %sw.bb
|
|
i32 4, label %sw.bb1
|
|
i32 5, label %sw.bb2
|
|
i32 6, label %sw.bb3
|
|
]
|
|
|
|
sw.default: ; preds = %entry
|
|
br label %sw.epilog
|
|
|
|
sw.bb: ; preds = %entry
|
|
%1 = load i32* %i.addr, align 4
|
|
%mul = mul nsw i32 %1, 7
|
|
store i32 %mul, i32* %i.addr, align 4
|
|
br label %sw.bb1
|
|
|
|
sw.bb1: ; preds = %entry, %sw.bb
|
|
%2 = load i32* %i.addr, align 4
|
|
%dec = add nsw i32 %2, -1
|
|
store i32 %dec, i32* %i.addr, align 4
|
|
br label %sw.bb2
|
|
|
|
sw.bb2: ; preds = %entry, %sw.bb1
|
|
%3 = load i32* %i.addr, align 4
|
|
%add = add nsw i32 %3, 3
|
|
store i32 %add, i32* %i.addr, align 4
|
|
br label %sw.bb3
|
|
|
|
sw.bb3: ; preds = %entry, %sw.bb2
|
|
%4 = load i32* %i.addr, align 4
|
|
%shl = shl i32 %4, 1
|
|
store i32 %shl, i32* %i.addr, align 4
|
|
br label %sw.epilog
|
|
|
|
sw.epilog: ; preds = %sw.bb3, %sw.default
|
|
%5 = load i32* %i.addr, align 4
|
|
ret i32 %5
|
|
}
|
|
|
|
; Verify generation of R_PPC64_TOC16_HA and R_PPC64_TOC16_LO_DS for
|
|
; accessing a jump table address.
|
|
;
|
|
; CHECK: Relocation 8
|
|
; CHECK-NEXT: 'r_offset'
|
|
; CHECK-NEXT: 'r_sym', 0x[[SYM5:[0-9]+]]
|
|
; CHECK-NEXT: 'r_type', 0x00000032
|
|
; CHECK: Relocation 9
|
|
; CHECK-NEXT: 'r_offset'
|
|
; CHECK-NEXT: 'r_sym', 0x[[SYM5]]
|
|
; CHECK-NEXT: 'r_type', 0x00000040
|
|
|
|
@ti = common global i32 0, align 4
|
|
|
|
define signext i32 @test_tentative() nounwind {
|
|
entry:
|
|
%0 = load i32* @ti, align 4
|
|
%inc = add nsw i32 %0, 1
|
|
store i32 %inc, i32* @ti, align 4
|
|
ret i32 %0
|
|
}
|
|
|
|
; Verify generation of R_PPC64_TOC16_HA and R_PPC64_TOC16_LO_DS for
|
|
; accessing tentatively declared variable ti.
|
|
;
|
|
; CHECK: Relocation 10
|
|
; CHECK-NEXT: 'r_offset'
|
|
; CHECK-NEXT: 'r_sym', 0x[[SYM6:[0-9]+]]
|
|
; CHECK-NEXT: 'r_type', 0x00000032
|
|
; CHECK: Relocation 11
|
|
; CHECK-NEXT: 'r_offset'
|
|
; CHECK-NEXT: 'r_sym', 0x[[SYM6]]
|
|
; CHECK-NEXT: 'r_type', 0x00000040
|
|
|
|
define i8* @test_fnaddr() nounwind {
|
|
entry:
|
|
%func = alloca i32 (i32)*, align 8
|
|
store i32 (i32)* @foo, i32 (i32)** %func, align 8
|
|
%0 = load i32 (i32)** %func, align 8
|
|
%1 = bitcast i32 (i32)* %0 to i8*
|
|
ret i8* %1
|
|
}
|
|
|
|
declare signext i32 @foo(i32 signext)
|
|
|
|
; Verify generation of R_PPC64_TOC16_HA and R_PPC64_TOC16_LO_DS for
|
|
; accessing function address foo.
|
|
;
|
|
; CHECK: Relocation 12
|
|
; CHECK-NEXT: 'r_offset'
|
|
; CHECK-NEXT: 'r_sym', 0x[[SYM7:[0-9]+]]
|
|
; CHECK-NEXT: 'r_type', 0x00000032
|
|
; CHECK: Relocation 13
|
|
; CHECK-NEXT: 'r_offset'
|
|
; CHECK-NEXT: 'r_sym', 0x[[SYM7]]
|
|
; CHECK-NEXT: 'r_type', 0x00000040
|
|
|