Index: lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp =================================================================== --- lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp +++ lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp @@ -8824,10 +8824,24 @@ None); // This is not an ABI copy. SDValue Chain = DAG.getEntryNode(); - ISD::NodeType ExtendType = (FuncInfo.PreferredExtendType.find(V) == - FuncInfo.PreferredExtendType.end()) - ? ISD::ANY_EXTEND - : FuncInfo.PreferredExtendType[V]; + ISD::NodeType ExtendType = ISD::ANY_EXTEND; + if (V->getType()->isIntegerTy(1)) { + // Respect getBooleanContents while extending i1 values + switch (TLI.getBooleanContents(/*isVec=*/false, /*isFloat=*/false)) { + case TargetLowering::ZeroOrOneBooleanContent: + ExtendType = ISD::ZERO_EXTEND; + break; + case TargetLowering::ZeroOrNegativeOneBooleanContent: + ExtendType = ISD::SIGN_EXTEND; + break; + case TargetLowering::UndefinedBooleanContent: + break; + } + } else if (FuncInfo.PreferredExtendType.find(V) != + FuncInfo.PreferredExtendType.end()) { + ExtendType = FuncInfo.PreferredExtendType[V]; + } + RFV.getCopyToRegs(Op, DAG, getCurSDLoc(), Chain, nullptr, V, ExtendType); PendingExports.push_back(Chain); } Index: test/CodeGen/WebAssembly/bug-40172.ll =================================================================== --- /dev/null +++ test/CodeGen/WebAssembly/bug-40172.ll @@ -0,0 +1,47 @@ +; RUN: llc < %s -O0 -debug 2>&1 | FileCheck %s + +; Regression test for bug 40172. The problem was that when FastISel +; fell back to DAG ISel for the code below, the DAG was combined down +; to a fragment that looked like +; +; t2: i32,ch = CopyFromReg t0, Register:i32 %0 +; t24: i1 = truncate t2 +; t8: i32 = any_extend t24 +; t10: ch = CopyToReg t0, Register:i32 %6, t8 +; +; Where the icmp yielding %x5 has been combined away into the +; truncate-extend pair. However, in the next step the truncate-extend +; pair is combined out, yielding +; +; t2: i32,ch = CopyFromReg t0, Register:i32 %0 +; t10: ch = CopyToReg t0, Register:i32 %6, t2 +; +; Which no longer produces a bool that respects +; getBooleanContents. The extend should have been a zero_extend to +; start out with so that getBooleanContents would be respected. + +target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128" +target triple = "wasm32-unknown-unknown" + +; CHECK: t0: ch = EntryToken +; CHECK-NEXT: t2: i32,ch = CopyFromReg t0, Register:i32 %0 +; CHECK-NEXT: t3: i8 = truncate t2 +; CHECK-NEXT: t5: i8 = and t3, Constant:i8<1> +; CHECK-NEXT: t7: i1 = setcc t5, Constant:i8<1>, seteq:ch +; CHECK-NEXT: t8: i32 = zero_extend t7 +; CHECK-NEXT: t10: ch = CopyToReg t0, Register:i32 %5, t8 + +define void @test(i8 %byte) { + %t = alloca { i8, i8 }, align 1 + %x4 = and i8 %byte, 1 + %x5 = icmp eq i8 %x4, 1 + %x6 = and i8 %byte, 2 + %x7 = icmp eq i8 %x6, 2 + %x8 = bitcast { i8, i8 }* %t to i8* + %x9 = zext i1 %x5 to i8 + store i8 %x9, i8* %x8, align 1 + %x10 = getelementptr inbounds { i8, i8 }, { i8, i8 }* %t, i32 0, i32 1 + %x11 = zext i1 %x7 to i8 + store i8 %x11, i8* %x10, align 1 + ret void +} Index: test/CodeGen/X86/and-sink.ll =================================================================== --- test/CodeGen/X86/and-sink.ll +++ test/CodeGen/X86/and-sink.ll @@ -52,12 +52,13 @@ ; CHECK-NEXT: je .LBB1_5 ; CHECK-NEXT: # %bb.1: # %bb0.preheader ; CHECK-NEXT: movb {{[0-9]+}}(%esp), %al +; CHECK-NEXT: andb $1, %al ; CHECK-NEXT: movl {{[0-9]+}}(%esp), %ecx ; CHECK-NEXT: .p2align 4, 0x90 ; CHECK-NEXT: .LBB1_2: # %bb0 ; CHECK-NEXT: # =>This Inner Loop Header: Depth=1 ; CHECK-NEXT: movl $0, B -; CHECK-NEXT: testb $1, %al +; CHECK-NEXT: testb %al, %al ; CHECK-NEXT: je .LBB1_5 ; CHECK-NEXT: # %bb.3: # %bb1 ; CHECK-NEXT: # in Loop: Header=BB1_2 Depth=1 Index: test/CodeGen/X86/fast-isel-select.ll =================================================================== --- test/CodeGen/X86/fast-isel-select.ll +++ test/CodeGen/X86/fast-isel-select.ll @@ -10,9 +10,12 @@ ; CHECK-LABEL: fastisel_select: ; CHECK: ## %bb.0: ; CHECK-NEXT: movb %sil, %al +; CHECK-NEXT: andb $1, %al ; CHECK-NEXT: movb %dil, %cl +; CHECK-NEXT: andb $1, %cl ; CHECK-NEXT: xorl %esi, %esi ; CHECK-NEXT: subb %al, %cl +; CHECK-NEXT: andb $1, %cl ; CHECK-NEXT: testb $1, %cl ; CHECK-NEXT: movl $1204476887, %edi ## imm = 0x47CADBD7 ; CHECK-NEXT: cmovnel %edi, %esi Index: test/CodeGen/X86/gpr-to-mask.ll =================================================================== --- test/CodeGen/X86/gpr-to-mask.ll +++ test/CodeGen/X86/gpr-to-mask.ll @@ -107,6 +107,7 @@ ; X86-64-NEXT: # %bb.1: # %if ; X86-64-NEXT: movb (%rdx), %al ; X86-64-NEXT: addb (%rcx), %al +; X86-64-NEXT: andb $1, %al ; X86-64-NEXT: jmp .LBB2_3 ; X86-64-NEXT: .LBB2_2: # %else ; X86-64-NEXT: movb (%rcx), %al @@ -120,19 +121,20 @@ ; X86-32: # %bb.0: # %entry ; X86-32-NEXT: vmovss {{.*#+}} xmm0 = mem[0],zero,zero,zero ; X86-32-NEXT: vmovss {{.*#+}} xmm1 = mem[0],zero,zero,zero -; X86-32-NEXT: movl {{[0-9]+}}(%esp), %ecx +; X86-32-NEXT: movl {{[0-9]+}}(%esp), %edx ; X86-32-NEXT: movl {{[0-9]+}}(%esp), %eax ; X86-32-NEXT: testb $1, {{[0-9]+}}(%esp) ; X86-32-NEXT: je .LBB2_2 ; X86-32-NEXT: # %bb.1: # %if -; X86-32-NEXT: movl {{[0-9]+}}(%esp), %edx -; X86-32-NEXT: movb (%edx), %dl -; X86-32-NEXT: addb (%ecx), %dl +; X86-32-NEXT: movl {{[0-9]+}}(%esp), %ecx +; X86-32-NEXT: movb (%ecx), %cl +; X86-32-NEXT: addb (%edx), %cl +; X86-32-NEXT: andb $1, %cl ; X86-32-NEXT: jmp .LBB2_3 ; X86-32-NEXT: .LBB2_2: # %else -; X86-32-NEXT: movb (%ecx), %dl +; X86-32-NEXT: movb (%edx), %cl ; X86-32-NEXT: .LBB2_3: # %exit -; X86-32-NEXT: kmovd %edx, %k1 +; X86-32-NEXT: kmovd %ecx, %k1 ; X86-32-NEXT: vmovss %xmm1, %xmm0, %xmm0 {%k1} ; X86-32-NEXT: vmovss %xmm0, (%eax) ; X86-32-NEXT: retl @@ -213,11 +215,10 @@ ; X86-64-NEXT: je .LBB4_2 ; X86-64-NEXT: # %bb.1: # %if ; X86-64-NEXT: movb (%rsi), %al -; X86-64-NEXT: jmp .LBB4_3 +; X86-64-NEXT: movb %al, (%rcx) +; X86-64-NEXT: retq ; X86-64-NEXT: .LBB4_2: # %else ; X86-64-NEXT: movb (%rdx), %al -; X86-64-NEXT: .LBB4_3: # %exit -; X86-64-NEXT: andb $1, %al ; X86-64-NEXT: movb %al, (%rcx) ; X86-64-NEXT: retq ; @@ -233,7 +234,6 @@ ; X86-32-NEXT: movl {{[0-9]+}}(%esp), %ecx ; X86-32-NEXT: .LBB4_3: # %exit ; X86-32-NEXT: movb (%ecx), %cl -; X86-32-NEXT: andb $1, %cl ; X86-32-NEXT: movb %cl, (%eax) ; X86-32-NEXT: retl entry: Index: test/CodeGen/X86/test-shrink-bug.ll =================================================================== --- test/CodeGen/X86/test-shrink-bug.ll +++ test/CodeGen/X86/test-shrink-bug.ll @@ -76,7 +76,7 @@ ; CHECK-X64-NEXT: pshufd {{.*#+}} xmm1 = xmm0[1,0,3,2] ; CHECK-X64-NEXT: pand %xmm0, %xmm1 ; CHECK-X64-NEXT: pextrw $4, %xmm1, %eax -; CHECK-X64-NEXT: testb $1, %al +; CHECK-X64-NEXT: andb $1, %al ; CHECK-X64-NEXT: jne .LBB1_2 ; CHECK-X64-NEXT: # %bb.3: # %no ; CHECK-X64-NEXT: callq bar Index: test/CodeGen/X86/xmulo.ll =================================================================== --- test/CodeGen/X86/xmulo.ll +++ test/CodeGen/X86/xmulo.ll @@ -711,6 +711,7 @@ ; FAST-LABEL: bug27873: ; FAST: ## %bb.0: ; FAST-NEXT: movq %rdi, %rax +; FAST-NEXT: andb $1, %sil ; FAST-NEXT: movl $160, %ecx ; FAST-NEXT: mulq %rcx ; FAST-NEXT: seto %al