-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[Clang] Add __builtin_stack_address
#148281
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
029d9fc
0b5e0ed
2b8084a
020909e
88db438
0669180
aea6944
20d0259
da68490
6cdcf58
c56be28
1ed8a5d
1339b31
64eb4ad
7c38381
d47cb56
1ba60ce
4f1fad5
6ced7f8
fdb520a
1f77682
57e9fe5
ae3b2b6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| // RUN: %clang_cc1 -emit-llvm %s -o - | FileCheck %s | ||
|
|
||
| // CHECK-LABEL: define {{[^@]+}} @a() | ||
| // CHECK: call {{[^@]+}} @llvm.stackaddress.p0() | ||
| void *a() { | ||
| return __builtin_stack_address(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm %s -o - | llvm-cxxfilt | FileCheck %s --check-prefixes=COMMON,NO-OPT | ||
| // RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm %s -O3 -o - | llvm-cxxfilt | FileCheck %s --check-prefixes=COMMON,OPT | ||
|
|
||
| struct S { | ||
| void *a(); | ||
| }; | ||
|
|
||
| // COMMON-LABEL: @S::a() | ||
| // COMMON: call ptr @llvm.stackaddress.p0() | ||
| void *S::a() { | ||
| return __builtin_stack_address(); | ||
| } | ||
|
|
||
| // COMMON-LABEL: define {{[^@]+}} @two() | ||
| void *two() { | ||
|
|
||
| // The compiler is allowed to inline a function calling `__builtin_stack_address`. | ||
| // | ||
| // OPT-NOT: define {{[^@]+}} @"two()::$_0::operator()() const" | ||
| // OPT: call {{[^@]+}} @llvm.stackaddress.p0() | ||
| // | ||
| // NO-OPT-DAG: define {{[^@]+}} @"two()::$_0::operator()() const" | ||
| // NO-OPT-DAG: call {{[^@]+}} @"two()::$_0::operator()() const" | ||
| auto l = []() { return __builtin_stack_address(); }; | ||
| return l(); | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -14389,6 +14389,35 @@ Semantics: | |||||
|
|
||||||
| Note this intrinsic is only verified on AArch64 and ARM. | ||||||
|
|
||||||
| '``llvm.stackaddress``' Intrinsic | ||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||||||
|
|
||||||
| Syntax: | ||||||
| """"""" | ||||||
|
|
||||||
| :: | ||||||
|
|
||||||
| declare ptr @llvm.stackaddress.p0() | ||||||
|
|
||||||
| Overview: | ||||||
| """"""""" | ||||||
|
|
||||||
| The '``llvm.stackaddress``' instrinsic returns the starting address of the | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| stack region that may be used by called functions. | ||||||
moorabbit marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
|
|
||||||
| Semantics: | ||||||
| """""""""" | ||||||
|
|
||||||
| This intrinsic returns the *logical* value of the stack pointer register, that | ||||||
| is, the address separating the stack space of the current function from the | ||||||
| stack space that may be modified by called functions. | ||||||
|
|
||||||
| On certain targets (e.g. x86), the logical and actual (or physical) values of | ||||||
| the stack pointer register are the same. However, on other architectures (e.g. | ||||||
| SPARCv9), the logical value of the stack pointer register may differ from the | ||||||
| physical value. '``llvm.stackaddress``' handles this discrepancy and returns | ||||||
| the correct boundary address. | ||||||
|
|
||||||
moorabbit marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you describe how it differs from |
||||||
| '``llvm.frameaddress``' Intrinsic | ||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||||||
|
|
||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -1012,6 +1012,7 @@ void SelectionDAGLegalize::LegalizeOp(SDNode *Node) { | |||||
| case ISD::INTRINSIC_WO_CHAIN: | ||||||
| case ISD::INTRINSIC_VOID: | ||||||
| case ISD::STACKSAVE: | ||||||
| case ISD::STACKADDRESS: | ||||||
| Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other); | ||||||
| break; | ||||||
| case ISD::GET_DYNAMIC_AREA_OFFSET: | ||||||
|
|
@@ -3680,6 +3681,7 @@ bool SelectionDAGLegalize::ExpandNode(SDNode *Node) { | |||||
| Results.push_back(Tmp1); | ||||||
| break; | ||||||
| } | ||||||
| case ISD::STACKADDRESS: | ||||||
| case ISD::STACKSAVE: | ||||||
| // Expand to CopyFromReg if the target set | ||||||
| // StackPointerRegisterToSaveRestore. | ||||||
moorabbit marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
|
@@ -3690,6 +3692,13 @@ bool SelectionDAGLegalize::ExpandNode(SDNode *Node) { | |||||
| } else { | ||||||
| Results.push_back(DAG.getUNDEF(Node->getValueType(0))); | ||||||
| Results.push_back(Node->getOperand(0)); | ||||||
|
|
||||||
| const char *IntrinsicName = Node->getOpcode() == ISD::STACKADDRESS | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| ? "llvm.stackaddress" | ||||||
| : "llvm.stacksave"; | ||||||
| DAG.getContext()->diagnose(DiagnosticInfoLegalizationFailure( | ||||||
| Twine(IntrinsicName) + " is not supported on this target.", | ||||||
| DAG.getMachineFunction().getFunction(), dl.getDebugLoc())); | ||||||
| } | ||||||
| break; | ||||||
| case ISD::STACKRESTORE: | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -1861,6 +1861,7 @@ SparcTargetLowering::SparcTargetLowering(const TargetMachine &TM, | |||||
| setOperationAction(ISD::STACKSAVE , MVT::Other, Expand); | ||||||
| setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand); | ||||||
| setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom); | ||||||
| setOperationAction(ISD::STACKADDRESS, MVT::Other, Custom); | ||||||
|
|
||||||
| setStackPointerRegisterToSaveRestore(SP::O6); | ||||||
|
|
||||||
|
|
@@ -2720,6 +2721,28 @@ static SDValue LowerVAARG(SDValue Op, SelectionDAG &DAG) { | |||||
| Align(std::min(PtrVT.getFixedSizeInBits(), VT.getFixedSizeInBits()) / 8)); | ||||||
| } | ||||||
|
|
||||||
| static SDValue LowerSTACKADDRESS(SDValue Op, SelectionDAG &DAG, | ||||||
| const SparcSubtarget *Subtarget) { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| SDValue Chain = Op.getOperand(0); | ||||||
| EVT VT = Op->getValueType(0); | ||||||
| SDLoc DL(Op); | ||||||
|
|
||||||
| unsigned SPReg = SP::O6; | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| SDValue SP = DAG.getCopyFromReg(Chain, DL, SPReg, VT); | ||||||
|
|
||||||
| // Unbias the stack pointer register. | ||||||
| unsigned OffsetToStackStart = Subtarget->getStackPointerBias(); | ||||||
| // Move past the register save area: 8 in registers + 8 local registers. | ||||||
| OffsetToStackStart += 16 * (Subtarget->is64Bit() ? 8 : 4); | ||||||
| // Move past the struct return address slot (4 bytes) on SPARC 32-bit. | ||||||
| if (!Subtarget->is64Bit()) | ||||||
| OffsetToStackStart += 4; | ||||||
|
|
||||||
| SDValue StackAddr = DAG.getNode(ISD::ADD, DL, VT, SP, | ||||||
| DAG.getConstant(OffsetToStackStart, DL, VT)); | ||||||
| return DAG.getMergeValues({StackAddr, Chain}, DL); | ||||||
| } | ||||||
|
|
||||||
| static SDValue LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG, | ||||||
| const SparcSubtarget *Subtarget) { | ||||||
| SDValue Chain = Op.getOperand(0); | ||||||
|
|
@@ -3117,6 +3140,8 @@ LowerOperation(SDValue Op, SelectionDAG &DAG) const { | |||||
| case ISD::VAARG: return LowerVAARG(Op, DAG); | ||||||
| case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG, | ||||||
| Subtarget); | ||||||
| case ISD::STACKADDRESS: | ||||||
| return LowerSTACKADDRESS(Op, DAG, Subtarget); | ||||||
|
|
||||||
| case ISD::LOAD: return LowerLOAD(Op, DAG); | ||||||
| case ISD::STORE: return LowerSTORE(Op, DAG); | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| ; RUN: llc < %s -mtriple=aarch64 | FileCheck %s | ||
|
|
||
| declare ptr @llvm.stackaddress.p0() | ||
|
|
||
| define ptr @test() { | ||
| ; CHECK: mov x0, sp | ||
| ; CHECK: ret | ||
| %sp = call ptr @llvm.stackaddress.p0() | ||
| ret ptr %sp | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| ; RUN: llc < %s -mtriple=armv7 | FileCheck %s | ||
|
|
||
| declare ptr @llvm.stackaddress.p0() | ||
|
|
||
| define ptr @test() { | ||
| ; CHECK: mov r0, sp | ||
| ; CHECK: bx lr | ||
| %sp = call ptr @llvm.stackaddress.p0() | ||
| ret ptr %sp | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| ; RUN: not llc < %s -mtriple nvptx 2>&1 | FileCheck %s | ||
|
|
||
| declare ptr @llvm.stackaddress.p0() | ||
|
|
||
| define ptr @test() { | ||
| ; CHECK: error: <unknown>:0:0: llvm.stackaddress is not supported on this target. | ||
| %sp = call ptr @llvm.stackaddress.p0() | ||
| ret ptr %sp | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| ; RUN: llc < %s -mtriple=sparc | FileCheck --check-prefix=sparc32 %s | ||
| ; RUN: llc < %s -mtriple=sparcv9 | FileCheck --check-prefix=sparc64 %s | ||
|
|
||
| declare ptr @llvm.stackaddress.p0() | ||
|
|
||
| define ptr @test() { | ||
| ; sparc32: save %sp, -96, %sp | ||
| ; sparc32: ret | ||
| ; sparc32: restore %sp, 68, %o0 | ||
| ; | ||
| ; sparc64: save %sp, -128, %sp | ||
| ; sparc64: ret | ||
| ; sparc64: restore %sp, 2175, %o0 | ||
| %sp = call ptr @llvm.stackaddress.p0() | ||
| ret ptr %sp | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| ; RUN: llc < %s -mtriple=x86_64-linux-gnu -o - | FileCheck --check-prefix=x86_64 %s | ||
| ; RUN: llc < %s -mtriple=i386-linux-gnu -o - | FileCheck --check-prefix=i386 %s | ||
|
|
||
| declare ptr @llvm.stackaddress.p0() | ||
|
|
||
| define ptr @test() { | ||
| ; x86_64: movq %rsp, %rax | ||
| ; x86_64: retq | ||
|
|
||
| ; i386: movl %esp, %eax | ||
| ; i386: retl | ||
| %sp = call ptr @llvm.stackaddress.p0() | ||
| ret ptr %sp | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.