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

zuojian lin via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 2 03:27:01 PDT 2019


linzj created this revision.
linzj added a reviewer: sunfish.
Herald added subscribers: llvm-commits, hiraditya.
Herald added a project: LLVM.

; 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 <https://reviews.llvm.org/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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D67068

Files:
  llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp


Index: llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
===================================================================
--- llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
+++ llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
@@ -1394,6 +1394,17 @@
   SDValue &N = NodeMap[V];
   if (N.getNode()) return N;
 
+  // Special handling BitCastInst with a constant.
+  if (const BitCastInst *I = dyn_cast<BitCastInst>(V)) {
+    if (const Constant *C = dyn_cast<Constant>(I->getOperand(0))) {
+      visitBitCast(*I);
+      SDValue &N = NodeMap[V];
+      assert(N.getNode());
+      resolveDanglingDebugInfo(V, N);
+      return N;
+    }
+  }
+
   // If there's a virtual register allocated and initialized for this
   // value, use it.
   if (SDValue copyFromReg = getCopyFromRegs(V, V->getType()))


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D67068.218322.patch
Type: text/x-patch
Size: 817 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190902/e4d1ec7f/attachment.bin>


More information about the llvm-commits mailing list