Skip to content

Commit 0d00d2b

Browse files
committed
(WORK IN PROGRESS) try to tail call address computation w/ more than two args
this folds more stuff, but also finds new breakages Fixes llvm#136848
1 parent 9062a38 commit 0d00d2b

File tree

3 files changed

+39
-15
lines changed

3 files changed

+39
-15
lines changed

llvm/lib/Target/X86/X86ISelDAGToDAG.cpp

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -897,20 +897,29 @@ static bool isCalleeLoad(SDValue Callee, SDValue &Chain, bool HasCallSeq) {
897897
Chain = Chain.getOperand(0);
898898
}
899899

900-
if (!Chain.getNumOperands())
901-
return false;
902-
// Since we are not checking for AA here, conservatively abort if the chain
903-
// writes to memory. It's not safe to move the callee (a load) across a store.
904-
if (isa<MemSDNode>(Chain.getNode()) &&
905-
cast<MemSDNode>(Chain.getNode())->writeMem())
900+
while (true) {
901+
if (!Chain.getNumOperands())
902+
return false;
903+
// Since we are not checking for AA here, conservatively abort if the chain
904+
// writes to memory. It's not safe to move the callee (a load) across a store.
905+
if (isa<MemSDNode>(Chain.getNode()) &&
906+
cast<MemSDNode>(Chain.getNode())->writeMem())
907+
return false;
908+
909+
if (Chain.getOperand(0).getNode() == Callee.getNode())
910+
return true;
911+
if (Chain.getOperand(0).getOpcode() == ISD::TokenFactor &&
912+
Callee.getValue(1).isOperandOf(Chain.getOperand(0).getNode()) &&
913+
Callee.getValue(1).hasOneUse())
914+
return true;
915+
916+
// Look past CopyToReg's.
917+
if (Chain.getOperand(0).getOpcode() == ISD::CopyToReg) {
918+
Chain = Chain.getOperand(0);
919+
continue;
920+
}
906921
return false;
907-
if (Chain.getOperand(0).getNode() == Callee.getNode())
908-
return true;
909-
if (Chain.getOperand(0).getOpcode() == ISD::TokenFactor &&
910-
Callee.getValue(1).isOperandOf(Chain.getOperand(0).getNode()) &&
911-
Callee.getValue(1).hasOneUse())
912-
return true;
913-
return false;
922+
}
914923
}
915924

916925
static bool isEndbrImm64(uint64_t Imm) {

llvm/test/CodeGen/X86/cfguard-checks.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,7 @@ entry:
210210
; X64-LABEL: vmptr_thunk:
211211
; X64: movq (%rcx), %rax
212212
; X64-NEXT: movq 8(%rax), %rax
213-
; X64-NEXT: movq __guard_dispatch_icall_fptr(%rip), %rdx
214-
; X64-NEXT: rex64 jmpq *%rdx # TAILCALL
213+
; X64-NEXT: rex64 jmpq *__guard_dispatch_icall_fptr(%rip) # TAILCALL
215214
; X64-NOT: callq
216215
}
217216

llvm/test/CodeGen/X86/fold-call-4.ll

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
; RUN: llc < %s | FileCheck %s
2+
3+
target triple = "x86_64-unknown-linux-gnu"
4+
5+
; The callee address computation should get folded into the call.
6+
; CHECK-LABEL: f:
7+
; CHECK-NOT: mov
8+
; CHECK: jmpq *(%rdi,%rsi,8)
9+
10+
define void @f(ptr %table, i64 %idx) {
11+
entry:
12+
%arrayidx = getelementptr inbounds ptr, ptr %table, i64 %idx
13+
%funcptr = load ptr, ptr %arrayidx, align 8
14+
tail call void %funcptr(ptr %table, i64 %idx)
15+
ret void
16+
}

0 commit comments

Comments
 (0)