Skip to content
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

[CodeGen] Fix sinking local values in lpads with phis #43

Merged
merged 1 commit into from
Mar 29, 2020
Merged
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
17 changes: 16 additions & 1 deletion llvm/lib/CodeGen/SelectionDAG/FastISel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,21 @@ static bool isRegUsedByPhiNodes(unsigned DefReg,
return false;
}

static bool isTerminatingEHLabel(MachineBasicBlock *MBB, MachineInstr &MI) {
// Ignore non-EH labels.
if (!MI.isEHLabel())
return false;

// Any EH label outside a landing pad must be for an invoke. Consider it a
// terminator.
if (!MBB->isEHPad())
return true;

// If this is a landingpad, the first non-phi instruction will be an EH_LABEL.
// Don't consider that label to be a terminator.
return MI.getIterator() != MBB->getFirstNonPHI();
}

/// Build a map of instruction orders. Return the first terminator and its
/// order. Consider EH_LABEL instructions to be terminators as well, since local
/// values for phis after invokes must be materialized before the call.
Expand All @@ -233,7 +248,7 @@ void FastISel::InstOrderMap::initialize(
unsigned Order = 0;
for (MachineInstr &I : *MBB) {
if (!FirstTerminator &&
(I.isTerminator() || (I.isEHLabel() && &I != &MBB->front()))) {
(I.isTerminator() || isTerminatingEHLabel(MBB, I))) {
FirstTerminator = &I;
FirstTerminatorOrder = Order;
}
Expand Down
36 changes: 36 additions & 0 deletions llvm/test/CodeGen/X86/sink-local-value.ll
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,42 @@ try.cont: ; preds = %entry, %lpad
; CHECK: retl


define i32 @lpad_phi() personality i32 (...)* @__gxx_personality_v0 {
entry:
store i32 42, i32* @sink_across
invoke void @may_throw()
to label %try.cont unwind label %lpad

lpad: ; preds = %entry
%p = phi i32 [ 11, %entry ] ; Trivial, but -O0 keeps it
%0 = landingpad { i8*, i32 }
catch i8* null
store i32 %p, i32* @sink_across
br label %try.cont

try.cont: ; preds = %entry, %lpad
%r.0 = phi i32 [ 13, %entry ], [ 55, %lpad ]
ret i32 %r.0
}

; The constant materialization should be *after* the stores to sink_across, but
; before any EH_LABEL.

; CHECK-LABEL: lpad_phi:
; CHECK: movl $42, sink_across
; CHECK: movl $13, %{{[a-z]*}}
; CHECK: .Ltmp{{.*}}:
; CHECK: calll may_throw
; CHECK: .Ltmp{{.*}}:
; CHECK: jmp .LBB{{.*}}
; CHECK: .LBB{{.*}}: # %lpad
; CHECK-NEXT: .Ltmp{{.*}}:
; CHECK: movl {{.*}}, sink_across
; CHECK: movl $55, %{{[a-z]*}}
; CHECK: .LBB{{.*}}: # %try.cont
; CHECK: retl


; Function Attrs: nounwind readnone speculatable
declare void @llvm.dbg.value(metadata, metadata, metadata) #0

Expand Down