llvm.org GIT mirror llvm / b6bb7db
[PowerPC] Fix calls to non-function objects Looking at r225438 inspired me to see how the PowerPC backend handled the situation (calling a bitcasted TLS global), and it turns out we also produced an error (cannot select ...). What it means to "call" something that is not a function is implementation and platform specific, but in the name of doing something (besides crashing), this makes sure we do what GCC does (treat all such calls as calls through a function pointer -- meaning that the pointer is assumed, as is the convention on PPC, to point to a function descriptor structure holding the actual code address along with the function's TOC pointer and environment pointer). As GCC does, we now do the same for calling regular (non-TLS) non-function globals too. I'm not sure whether this is the most useful way to define the behavior, but at least we won't be alone. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@225617 91177308-0d34-0410-b5e6-96231b3b80d8 Hal Finkel 5 years ago
2 changed file(s) with 91 addition(s) and 5 deletion(s). Raw diff Collapse all Expand all
35723572 InFlag = Chain.getValue(1);
35733573 }
35743574
3575 // Is this global address that of a function that can be called by name? (as
3576 // opposed to something that must hold a descriptor for an indirect call).
3577 static bool isFunctionGlobalAddress(SDValue Callee) {
3578 if (GlobalAddressSDNode *G = dyn_cast(Callee)) {
3579 if (Callee.getOpcode() == ISD::GlobalTLSAddress ||
3580 Callee.getOpcode() == ISD::TargetGlobalTLSAddress)
3581 return false;
3582
3583 return G->getGlobal()->getType()->getElementType()->isFunctionTy();
3584 }
3585
3586 return false;
3587 }
3588
35753589 static
35763590 unsigned PrepareCall(SelectionDAG &DAG, SDValue &Callee, SDValue &InFlag,
35773591 SDValue &Chain, SDLoc dl, int SPDiff, bool isTailCall,
35973611 needIndirectCall = false;
35983612 }
35993613
3600 if (GlobalAddressSDNode *G = dyn_cast(Callee)) {
3614 if (isFunctionGlobalAddress(Callee)) {
3615 GlobalAddressSDNode *G = cast(Callee);
3616 // A call to a TLS address is actually an indirect call to a
3617 // thread-specific pointer.
36013618 unsigned OpFlags = 0;
36023619 if ((DAG.getTarget().getRelocationModel() != Reloc::Static &&
36033620 (Subtarget.getTargetTriple().isMacOSX() &&
46484665 // See PrepareCall() for more information about calls through function
46494666 // pointers in the 64-bit SVR4 ABI.
46504667 if (!isTailCall &&
4651 !dyn_cast(Callee) &&
4652 !dyn_cast(Callee)) {
4668 !isFunctionGlobalAddress(Callee) &&
4669 !isa(Callee)) {
46534670 // Load r2 into a virtual register and store it to the TOC save area.
46544671 SDValue Val = DAG.getCopyFromReg(Chain, dl, PPC::X2, MVT::i64);
46554672 // TOC save area offset.
50525069 // not mean the MTCTR instruction must use R12; it's easier to model this as
50535070 // an extra parameter, so do that.
50545071 if (!isTailCall &&
5055 !dyn_cast(Callee) &&
5056 !dyn_cast(Callee) &&
5072 !isFunctionGlobalAddress(Callee) &&
5073 !isa(Callee) &&
50575074 !isBLACompatibleAddress(Callee, DAG))
50585075 RegsToPass.push_back(std::make_pair((unsigned)(isPPC64 ? PPC::X12 :
50595076 PPC::R12), Callee));
0 ; RUN: llc -mcpu=ppc64 < %s | FileCheck %s
1 target datalayout = "E-m:e-i64:64-n32:64"
2 target triple = "powerpc64-unknown-linux-gnu"
3
4 %struct.cd = type { i64, i64, i64 }
5
6 @something = global [33 x i8] c"this is not really code, but...\0A\00", align 1
7 @tls_something = thread_local global %struct.cd zeroinitializer, align 8
8 @extern_something = external global %struct.cd
9
10 ; Function Attrs: nounwind
11 define void @foo() #0 {
12 entry:
13 tail call void bitcast ([33 x i8]* @something to void ()*)() #0
14 ret void
15
16 ; CHECK-LABEL: @foo
17 ; CHECK-DAG: addis [[REG1:[0-9]+]], 2, something@toc@ha
18 ; CHECK-DAG: std 2, 40(1)
19 ; CHECK-DAG: addi [[REG3:[0-9]+]], [[REG1]], something@toc@l
20 ; CHECK-DAG: ld [[REG2:[0-9]+]], 0([[REG3]])
21 ; CHECK-DAG: ld 11, 16([[REG3]])
22 ; CHECK-DAG: ld 2, 8([[REG3]])
23 ; CHECK-DAG: mtctr [[REG2]]
24 ; CHECK: bctrl
25 ; CHECK: ld 2, 40(1)
26 ; CHECK: blr
27 }
28
29 ; Function Attrs: nounwind
30 define void @bar() #0 {
31 entry:
32 tail call void bitcast (%struct.cd* @tls_something to void ()*)() #0
33 ret void
34
35 ; CHECK-LABEL: @bar
36 ; CHECK-DAG: addis [[REG1:[0-9]+]], 13, tls_something@tprel@ha
37 ; CHECK-DAG: std 2, 40(1)
38 ; CHECK-DAG: addi [[REG3:[0-9]+]], [[REG1]], tls_something@tprel@l
39 ; CHECK-DAG: ld [[REG2:[0-9]+]], 0([[REG3]])
40 ; CHECK-DAG: ld 11, 16([[REG3]])
41 ; CHECK-DAG: ld 2, 8([[REG3]])
42 ; CHECK-DAG: mtctr [[REG2]]
43 ; CHECK: bctrl
44 ; CHECK: ld 2, 40(1)
45 ; CHECK: blr
46 }
47
48 ; Function Attrs: nounwind
49 define void @ext() #0 {
50 entry:
51 tail call void bitcast (%struct.cd* @extern_something to void ()*)() #0
52 ret void
53
54 ; CHECK-LABEL: @ext
55 ; CHECK-DAG: addis [[REG1:[0-9]+]], 2, [[NAME:[._A-Za-z0-9]+]]@toc@ha
56 ; CHECK-DAG: std 2, 40(1)
57 ; CHECK-DAG: ld [[REG3:[0-9]+]], [[NAME]]@toc@l(3)
58 ; CHECK-DAG: ld [[REG2:[0-9]+]], 0([[REG3]])
59 ; CHECK-DAG: ld 11, 16([[REG3]])
60 ; CHECK-DAG: ld 2, 8([[REG3]])
61 ; CHECK-DAG: mtctr [[REG2]]
62 ; CHECK: bctrl
63 ; CHECK: ld 2, 40(1)
64 ; CHECK: blr
65 }
66
67 attributes #0 = { nounwind }
68