Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1825,6 +1825,14 @@ void SelectionDAGISel::SelectAllBasicBlocks(const Function &Fn) {

reportFastISelFailure(*MF, *ORE, R, EnableFastISelAbort > 2);

// If the call has operand bundles, then it's best if they are handled
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand the flow here. These seems pretty far removed from where the problem is?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The root cause of the problem is that SelectionDAG is designed to lower CallInst and its bundled ops together: for example, both the indirect call and the address-of GlobalValue that are generated in CF Guard (which can then be lowered as a call to the address of a GlobalValue, instead of reading the value and then calling to a register).

I'm not sure why SelectionDAGISel has this special case here (I assume to try to lower calls via SelectionDAG but then allow the rest of the block to be handled by FastISel), but it breaks that assumption and causes suboptimal codegen.

// together with the call instead of selecting the call as its own
// block.
if (cast<CallInst>(Inst)->hasOperandBundles()) {
NumFastIselFailures += NumFastIselRemaining;
break;
}

if (!Inst->getType()->isVoidTy() && !Inst->getType()->isTokenTy() &&
!Inst->use_empty()) {
Register &R = FuncInfo->ValueMap[Inst];
Expand Down
9 changes: 6 additions & 3 deletions llvm/test/CodeGen/X86/cfguard-checks.ll
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
; RUN: llc < %s -mtriple=i686-pc-windows-msvc | FileCheck %s -check-prefix=X86
; RUN: llc < %s -mtriple=x86_64-pc-windows-msvc | FileCheck %s -check-prefixes=X64,X64_MSVC
; RUN: llc < %s -mtriple=x86_64-pc-windows-msvc | FileCheck %s -check-prefixes=X64,X64_MSVC,X64_SELDAG
; RUN: llc < %s --fast-isel -mtriple=x86_64-pc-windows-msvc | FileCheck %s -check-prefixes=X64,X64_MSVC,X64_FISEL
; RUN: llc < %s -mtriple=i686-w64-windows-gnu | FileCheck %s -check-prefixes=X86,X86_MINGW
; RUN: llc < %s -mtriple=x86_64-w64-windows-gnu | FileCheck %s -check-prefixes=X64,X64_MINGW
; RUN: llc < %s -mtriple=x86_64-w64-windows-gnu | FileCheck %s -check-prefixes=X64,X64_MINGW,X64_SELDAG
; RUN: llc < %s --fast-isel -mtriple=x86_64-w64-windows-gnu | FileCheck %s -check-prefixes=X64,X64_MINGW,X64_FISEL
; Control Flow Guard is currently only available on Windows

; Test that Control Flow Guard checks are correctly added when required.
Expand All @@ -27,7 +29,8 @@ entry:
; X64-LABEL: func_guard_nocf
; X64: leaq target_func(%rip), %rax
; X64-NOT: __guard_dispatch_icall_fptr
; X64: callq *%rax
; X64_SELDAG: callq *%rax
; X64_FISEL: callq *32(%rsp)
}
attributes #0 = { "guard_nocf" }

Expand Down