This is an archive of the discontinued LLVM Phabricator instance.

A instruction bitcast a constant, and get used across the basic block will generate additional COPY SDNode.The example as follow:
AbandonedPublic

Authored by linzj on Sep 2 2019, 3:21 AM.

Details

Reviewers
sunfish
Summary
; ModuleID = '1.c'
source_filename = "1.c"
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

; Function Attrs: norecurse nounwind uwtable writeonly
define dso_local i32 @foo(i8* %a, i8* %b, i8* %c) {
entry:
  %const1 = bitcast i32 -524288 to i32
  %0 = ptrtoint i8* %a to i32
  %1 = and i32 %0, %const1
  %2 = inttoptr i32 %1 to i8*
  %3 = getelementptr i8, i8* %2, i32 4
  %4 = bitcast i8* %3 to i32*
  %5 = load i32, i32* %4, align 4
  %6= icmp eq i32 %5, 0
  br i1 %6, label %B0, label %B1
B0:
  %7 = ptrtoint i8* %a to i32
  %8 = and i32 %7, %const1
  %9 = inttoptr i32 %8 to i8*
  %10 = getelementptr i8, i8* %9, i32 4
  %11 = bitcast i8* %10 to i32*
  %12 = load i32, i32* %11, align 4
  ret i32 %12
B1:
  ret i32 -1
}

will generates:

foo:                                    # @foo
	.cfi_startproc
# %bb.0:                                # %entry
	movl	%edi, %eax
	andl	$-524288, %eax          # imm = 0xFFF80000
	cmpl	$0, 4(%rax)
	je	.LBB0_1
# %bb.2:                                # %B1
	movl	$-1, %eax
	retq
.LBB0_1:                                # %B0
=>	movl	$-524288, %eax          # imm = 0xFFF80000
	andl	%eax, %edi
	movl	4(%rdi), %eax
	retq

The movl instruction pointed by an arrow is generated by the additional COPY
SDNode.

The patch is to revisit the bitcast instruction to get a valid SDValue for the
constant.

Event Timeline

linzj created this revision.Sep 2 2019, 3:21 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 2 2019, 3:21 AM
lebedev.ri edited the summary of this revision. (Show Details)Sep 2 2019, 3:36 AM

tests missing.
From the first look this fix seems rather ad-hoc.

linzj added a comment.Sep 2 2019, 5:34 AM

tests missing.
From the first look this fix seems rather ad-hoc.

Essentially it's a constant leaking problem. The bitcast instructions are generated by the "constant hoist" algorithm from LLVM. I does not write this snippet.

linzj abandoned this revision.Sep 2 2019, 6:22 PM

I think the ConstantHoist have its job well done, maybe reload a constant from a stack slot is a good idea.