Index: include/llvm/CodeGen/ISDOpcodes.h =================================================================== --- include/llvm/CodeGen/ISDOpcodes.h +++ include/llvm/CodeGen/ISDOpcodes.h @@ -372,6 +372,13 @@ /// then the result type must also be a vector type. SETCC, + /// Like SetCC, ops #0 and #1 are the LHS and RHS operands to compare, but + /// op #2 is a *carry value* which is 1 iff a lower part of LHS is less than + /// a lower part of RHS. Op #3 is the condition code. The only valid codes + /// are the ones corresponding to integer < and >=. This can be used to + /// compare two wide integers: (setcce lhshi rhshi (subc lhslo rhslo) cc). + SETCCE, + /// SHL_PARTS/SRA_PARTS/SRL_PARTS - These operators are used for expanded /// integer shift operations. The operation ordering is: /// [Lo,Hi] = op [LoLHS,HiLHS], Amt Index: lib/CodeGen/SelectionDAG/LegalizeDAG.cpp =================================================================== --- lib/CodeGen/SelectionDAG/LegalizeDAG.cpp +++ lib/CodeGen/SelectionDAG/LegalizeDAG.cpp @@ -1242,7 +1242,8 @@ case ISD::SETCC: case ISD::BR_CC: { unsigned CCOperand = Node->getOpcode() == ISD::SELECT_CC ? 4 : - Node->getOpcode() == ISD::SETCC ? 2 : 1; + Node->getOpcode() == ISD::SETCC ? 2 : + Node->getOpcode() == ISD::SETCCE ? 3 : 1; unsigned CompareOperand = Node->getOpcode() == ISD::BR_CC ? 2 : 0; MVT OpVT = Node->getOperand(CompareOperand).getSimpleValueType(); ISD::CondCode CCCode = Index: lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp =================================================================== --- lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp +++ lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp @@ -2634,6 +2634,7 @@ case ISD::SCALAR_TO_VECTOR: Res = ExpandOp_SCALAR_TO_VECTOR(N); break; case ISD::SELECT_CC: Res = ExpandIntOp_SELECT_CC(N); break; case ISD::SETCC: Res = ExpandIntOp_SETCC(N); break; + case ISD::SETCCE: Res = ExpandIntOp_SETCCE(N); break; case ISD::SINT_TO_FP: Res = ExpandIntOp_SINT_TO_FP(N); break; case ISD::STORE: Res = ExpandIntOp_STORE(cast(N), OpNo); break; case ISD::TRUNCATE: Res = ExpandIntOp_TRUNCATE(N); break; @@ -2761,6 +2762,44 @@ return; } + if (LHSHi == RHSHi) { + // Comparing the low bits is enough. + NewLHS = Tmp1; + NewRHS = SDValue(); + return; + } + + // Lower with SETCCE if the target supports it. + if (TLI.getOperationAction( + ISD::SETCCE, + TLI.getTypeToExpandTo(*DAG.getContext(), LHSLo.getValueType())) == + TargetLowering::Custom) { + // SETCCE only works with < and >=. + bool FlipOperands = false; + switch (CCCode) { + // For > and <=, flip operands and condition code: + case ISD::SETGT: CCCode = ISD::SETLT; FlipOperands = true; break; + case ISD::SETUGT: CCCode = ISD::SETULT; FlipOperands = true; break; + case ISD::SETLE: CCCode = ISD::SETGE; FlipOperands = true; break; + case ISD::SETULE: CCCode = ISD::SETUGE; FlipOperands = true; break; + default: break; + } + if (FlipOperands) { + std::swap(LHSLo, RHSLo); + std::swap(LHSHi, RHSHi); + } + // Perform a wide subtraction, feeding the carry from the low part into + // SETCCE. + SDVTList VTList = DAG.getVTList(LHSLo.getValueType(), MVT::Glue); + SDValue LowCmp = DAG.getNode(ISD::SUBC, dl, VTList, LHSLo, RHSLo); + SDValue Res = + DAG.getNode(ISD::SETCCE, dl, getSetCCResultType(LHSLo.getValueType()), + LHSHi, RHSHi, LowCmp.getValue(1), DAG.getCondCode(CCCode)); + NewLHS = Res; + NewRHS = SDValue(); + return; + } + NewLHS = TLI.SimplifySetCC(getSetCCResultType(LHSHi.getValueType()), LHSHi, RHSHi, ISD::SETEQ, false, DagCombineInfo, dl); @@ -2825,6 +2864,24 @@ DAG.getCondCode(CCCode)), 0); } +SDValue DAGTypeLegalizer::ExpandIntOp_SETCCE(SDNode *N) { + SDValue LHS = N->getOperand(0); + SDValue RHS = N->getOperand(1); + SDValue Carry = N->getOperand(2); + SDValue Cond = N->getOperand(3); + SDLoc dl = SDLoc(N); + + SDValue LHSLo, LHSHi, RHSLo, RHSHi; + GetExpandedInteger(LHS, LHSLo, LHSHi); + GetExpandedInteger(RHS, RHSLo, RHSHi); + + // Expand to a SUBE for the low part and a smaller SETCCE for the high. + SDVTList VTList = DAG.getVTList(LHSLo.getValueType(), MVT::Glue); + SDValue LowCmp = DAG.getNode(ISD::SUBE, dl, VTList, LHSLo, RHSLo, Carry); + return DAG.getNode(ISD::SETCCE, dl, N->getValueType(0), LHSHi, RHSHi, + LowCmp.getValue(1), Cond); +} + SDValue DAGTypeLegalizer::ExpandIntOp_Shift(SDNode *N) { // The value being shifted is legal, but the shift amount is too big. // It follows that either the result of the shift is undefined, or the Index: lib/CodeGen/SelectionDAG/LegalizeTypes.h =================================================================== --- lib/CodeGen/SelectionDAG/LegalizeTypes.h +++ lib/CodeGen/SelectionDAG/LegalizeTypes.h @@ -356,6 +356,7 @@ SDValue ExpandIntOp_BR_CC(SDNode *N); SDValue ExpandIntOp_SELECT_CC(SDNode *N); SDValue ExpandIntOp_SETCC(SDNode *N); + SDValue ExpandIntOp_SETCCE(SDNode *N); SDValue ExpandIntOp_Shift(SDNode *N); SDValue ExpandIntOp_SINT_TO_FP(SDNode *N); SDValue ExpandIntOp_STORE(StoreSDNode *N, unsigned OpNo); Index: lib/CodeGen/SelectionDAG/SelectionDAGDumper.cpp =================================================================== --- lib/CodeGen/SelectionDAG/SelectionDAGDumper.cpp +++ lib/CodeGen/SelectionDAG/SelectionDAGDumper.cpp @@ -209,6 +209,7 @@ case ISD::FPOWI: return "fpowi"; case ISD::SETCC: return "setcc"; + case ISD::SETCCE: return "setcce"; case ISD::SELECT: return "select"; case ISD::VSELECT: return "vselect"; case ISD::SELECT_CC: return "select_cc"; Index: lib/Target/X86/X86ISelLowering.h =================================================================== --- lib/Target/X86/X86ISelLowering.h +++ lib/Target/X86/X86ISelLowering.h @@ -1021,6 +1021,7 @@ SDValue LowerToBT(SDValue And, ISD::CondCode CC, SDLoc dl, SelectionDAG &DAG) const; SDValue LowerSETCC(SDValue Op, SelectionDAG &DAG) const; + SDValue LowerSETCCE(SDValue Op, SelectionDAG &DAG) const; SDValue LowerSELECT(SDValue Op, SelectionDAG &DAG) const; SDValue LowerBRCOND(SDValue Op, SelectionDAG &DAG) const; SDValue LowerJumpTable(SDValue Op, SelectionDAG &DAG) const; Index: lib/Target/X86/X86ISelLowering.cpp =================================================================== --- lib/Target/X86/X86ISelLowering.cpp +++ lib/Target/X86/X86ISelLowering.cpp @@ -421,10 +421,15 @@ setOperationAction(ISD::SETCC , MVT::f32 , Custom); setOperationAction(ISD::SETCC , MVT::f64 , Custom); setOperationAction(ISD::SETCC , MVT::f80 , Custom); + setOperationAction(ISD::SETCCE , MVT::i8 , Custom); + setOperationAction(ISD::SETCCE , MVT::i16 , Custom); + setOperationAction(ISD::SETCCE , MVT::i32 , Custom); if (Subtarget->is64Bit()) { setOperationAction(ISD::SELECT , MVT::i64 , Custom); setOperationAction(ISD::SETCC , MVT::i64 , Custom); + setOperationAction(ISD::SETCCE , MVT::i64 , Custom); } + setOperationAction(ISD::EH_RETURN , MVT::Other, Custom); // NOTE: EH_SJLJ_SETJMP/_LONGJMP supported here is NOT intended to support // SjLj exception handling but a light-weight setjmp/longjmp replacement to @@ -14578,6 +14583,30 @@ return SetCC; } +SDValue X86TargetLowering::LowerSETCCE(SDValue Op, SelectionDAG &DAG) const { + SDValue LHS = Op.getOperand(0); + SDValue RHS = Op.getOperand(1); + SDValue Carry = Op.getOperand(2); + SDValue Cond = Op.getOperand(3); + SDLoc DL(Op); + + X86::CondCode CC; + switch (cast(Cond)->get()) { + // This operation can only detect < and >=. + case ISD::SETLT: CC = X86::COND_L; break; + case ISD::SETULT: CC = X86::COND_B; break; + case ISD::SETGE: CC = X86::COND_GE; break; + case ISD::SETUGE: CC = X86::COND_AE; break; + default: + llvm_unreachable("Unexpected condition code!"); + } + + SDVTList VTs = DAG.getVTList(LHS.getValueType(), MVT::i32); + SDValue Cmp = DAG.getNode(X86ISD::SBB, DL, VTs, LHS, RHS, Carry); + return DAG.getNode(X86ISD::SETCC, DL, Op.getValueType(), + DAG.getConstant(CC, DL, MVT::i8), Cmp.getValue(1)); +} + // isX86LogicalCmp - Return true if opcode is a X86 logical comparison. static bool isX86LogicalCmp(SDValue Op) { unsigned Opc = Op.getNode()->getOpcode(); @@ -19661,6 +19690,7 @@ case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG); case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG); case ISD::SETCC: return LowerSETCC(Op, DAG); + case ISD::SETCCE: return LowerSETCCE(Op, DAG); case ISD::SELECT: return LowerSELECT(Op, DAG); case ISD::BRCOND: return LowerBRCOND(Op, DAG); case ISD::JumpTable: return LowerJumpTable(Op, DAG); Index: test/CodeGen/X86/2012-08-17-legalizer-crash.ll =================================================================== --- test/CodeGen/X86/2012-08-17-legalizer-crash.ll +++ test/CodeGen/X86/2012-08-17-legalizer-crash.ll @@ -26,6 +26,5 @@ ret void ; CHECK-LABEL: fn1: -; CHECK: shrq $32, [[REG:%.*]] -; CHECK: sete +; CHECK: jb } Index: test/CodeGen/X86/atomic-minmax-i6432.ll =================================================================== --- test/CodeGen/X86/atomic-minmax-i6432.ll +++ test/CodeGen/X86/atomic-minmax-i6432.ll @@ -8,7 +8,7 @@ %1 = atomicrmw max i64* @sc64, i64 5 acquire ; LINUX: [[LABEL:.LBB[0-9]+_[0-9]+]] ; LINUX: cmpl -; LINUX: seta +; LINUX: sbbl ; LINUX: cmovne ; LINUX: cmovne ; LINUX: lock cmpxchg8b @@ -16,7 +16,7 @@ %2 = atomicrmw min i64* @sc64, i64 6 acquire ; LINUX: [[LABEL:.LBB[0-9]+_[0-9]+]] ; LINUX: cmpl -; LINUX: setb +; LINUX: sbbl ; LINUX: cmovne ; LINUX: cmovne ; LINUX: lock cmpxchg8b @@ -24,7 +24,7 @@ %3 = atomicrmw umax i64* @sc64, i64 7 acquire ; LINUX: [[LABEL:.LBB[0-9]+_[0-9]+]] ; LINUX: cmpl -; LINUX: seta +; LINUX: sbbl ; LINUX: cmovne ; LINUX: cmovne ; LINUX: lock cmpxchg8b @@ -32,7 +32,7 @@ %4 = atomicrmw umin i64* @sc64, i64 8 acquire ; LINUX: [[LABEL:.LBB[0-9]+_[0-9]+]] ; LINUX: cmpl -; LINUX: setb +; LINUX: sbbl ; LINUX: cmovne ; LINUX: cmovne ; LINUX: lock cmpxchg8b Index: test/CodeGen/X86/atomic128.ll =================================================================== --- test/CodeGen/X86/atomic128.ll +++ test/CodeGen/X86/atomic128.ll @@ -119,16 +119,9 @@ ; CHECK-DAG: movq 8(%rdi), %rdx ; CHECK: [[LOOP:.?LBB[0-9]+_[0-9]+]]: -; CHECK: cmpq %rsi, %rax -; CHECK: setbe [[CMP:%[a-z0-9]+]] -; CHECK: cmpq [[INCHI]], %rdx -; CHECK: setle [[HICMP:%[a-z0-9]+]] -; CHECK: je [[USE_LO:.?LBB[0-9]+_[0-9]+]] - -; CHECK: movb [[HICMP]], [[CMP]] -; CHECK: [[USE_LO]]: -; CHECK: testb [[CMP]], [[CMP]] -; CHECK: movq %rsi, %rbx +; CHECK: cmpq +; CHECK: sbbq +; CHECK: setg ; CHECK: cmovneq %rax, %rbx ; CHECK: movq [[INCHI]], %rcx ; CHECK: cmovneq %rdx, %rcx @@ -151,16 +144,9 @@ ; CHECK-DAG: movq 8(%rdi), %rdx ; CHECK: [[LOOP:.?LBB[0-9]+_[0-9]+]]: -; CHECK: cmpq %rsi, %rax -; CHECK: setae [[CMP:%[a-z0-9]+]] -; CHECK: cmpq [[INCHI]], %rdx -; CHECK: setge [[HICMP:%[a-z0-9]+]] -; CHECK: je [[USE_LO:.?LBB[0-9]+_[0-9]+]] - -; CHECK: movb [[HICMP]], [[CMP]] -; CHECK: [[USE_LO]]: -; CHECK: testb [[CMP]], [[CMP]] -; CHECK: movq %rsi, %rbx +; CHECK: cmpq +; CHECK: sbbq +; CHECK: setge ; CHECK: cmovneq %rax, %rbx ; CHECK: movq [[INCHI]], %rcx ; CHECK: cmovneq %rdx, %rcx @@ -183,16 +169,9 @@ ; CHECK-DAG: movq 8(%rdi), %rdx ; CHECK: [[LOOP:.?LBB[0-9]+_[0-9]+]]: -; CHECK: cmpq %rsi, %rax -; CHECK: setbe [[CMP:%[a-z0-9]+]] -; CHECK: cmpq [[INCHI]], %rdx -; CHECK: setbe [[HICMP:%[a-z0-9]+]] -; CHECK: je [[USE_LO:.?LBB[0-9]+_[0-9]+]] - -; CHECK: movb [[HICMP]], [[CMP]] -; CHECK: [[USE_LO]]: -; CHECK: testb [[CMP]], [[CMP]] -; CHECK: movq %rsi, %rbx +; CHECK: cmpq +; CHECK: sbbq +; CHECK: seta ; CHECK: cmovneq %rax, %rbx ; CHECK: movq [[INCHI]], %rcx ; CHECK: cmovneq %rdx, %rcx @@ -215,16 +194,9 @@ ; CHECK-DAG: movq 8(%rdi), %rdx ; CHECK: [[LOOP:.?LBB[0-9]+_[0-9]+]]: -; CHECK: cmpq %rax, %rsi -; CHECK: setb [[CMP:%[a-z0-9]+]] -; CHECK: cmpq [[INCHI]], %rdx -; CHECK: seta [[HICMP:%[a-z0-9]+]] -; CHECK: je [[USE_LO:.?LBB[0-9]+_[0-9]+]] - -; CHECK: movb [[HICMP]], [[CMP]] -; CHECK: [[USE_LO]]: -; CHECK: testb [[CMP]], [[CMP]] -; CHECK: movq %rsi, %rbx +; CHECK: cmpq +; CHECK: sbbq +; CHECK: setb ; CHECK: cmovneq %rax, %rbx ; CHECK: movq [[INCHI]], %rcx ; CHECK: cmovneq %rdx, %rcx Index: test/CodeGen/X86/avx512-cmp.ll =================================================================== --- test/CodeGen/X86/avx512-cmp.ll +++ test/CodeGen/X86/avx512-cmp.ll @@ -1,5 +1,4 @@ ; RUN: llc < %s -mtriple=x86_64-apple-darwin -mcpu=knl --show-mc-encoding | FileCheck %s -; RUN: llc < %s -mtriple=i386-apple-darwin -mcpu=knl | FileCheck %s --check-prefix AVX512-32 ; CHECK-LABEL: test1 ; CHECK: vucomisd {{.*}}encoding: [0x62 @@ -100,27 +99,3 @@ B: ret i32 7 } - -; AVX512-32-LABEL: test10 -; AVX512-32: movl 4(%esp), %ecx -; AVX512-32: cmpl $9, (%ecx) -; AVX512-32: seta %al -; AVX512-32: cmpl $0, 4(%ecx) -; AVX512-32: setg %cl -; AVX512-32: je -; AVX512-32: movb %cl, %al -; AVX512-32: testb $1, %al - -define void @test10(i64* %i.addr) { - - %x = load i64, i64* %i.addr, align 8 - %cmp = icmp slt i64 %x, 10 - br i1 %cmp, label %true, label %false - -true: - ret void - -false: - ret void -} - Index: test/CodeGen/X86/wide-integer-cmp.ll =================================================================== --- /dev/null +++ test/CodeGen/X86/wide-integer-cmp.ll @@ -0,0 +1,106 @@ +; RUN: llc -mtriple=i686-linux-gnu %s -o - | FileCheck %s + + +define i32 @branch_eq(i64 %a, i64 %b) { +entry: + %cmp = icmp eq i64 %a, %b + br i1 %cmp, label %bb1, label %bb2 +bb1: + ret i32 1 +bb2: + ret i32 2 + +; CHECK-LABEL: branch_eq: +; CHECK: movl 4(%esp), [[LHSLo:%[a-z]+]] +; CHECK: movl 8(%esp), [[LHSHi:%[a-z]+]] +; CHECK: xorl 16(%esp), [[LHSHi]] +; CHECK: xorl 12(%esp), [[LHSLo]] +; CHECK: orl [[LHSHi]], [[LHSLo]] +; CHECK: jne [[FALSE:.LBB[0-9_]+]] +; CHECK: movl $1, %eax +; CHECK: retl +; CHECK: [[FALSE]]: +; CHECK: movl $2, %eax +; CHECK: retl +} + +define i32 @branch_slt(i64 %a, i64 %b) { +entry: + %cmp = icmp slt i64 %a, %b + br i1 %cmp, label %bb1, label %bb2 +bb1: + ret i32 1 +bb2: + ret i32 2 + +; CHECK-LABEL: branch_slt: +; CHECK: movl 4(%esp), [[LHSLo:%[a-z]+]] +; CHECK: movl 8(%esp), [[LHSHi:%[a-z]+]] +; CHECK: cmpl 12(%esp), [[LHSLo]] +; CHECK: sbbl 16(%esp), [[LHSHi]] +; CHECK: jge [[FALSE:.LBB[0-9_]+]] +; CHECK: movl $1, %eax +; CHECK: retl +; CHECK: [[FALSE]]: +; CHECK: movl $2, %eax +; CHECK: retl +} + +define i32 @branch_ule(i64 %a, i64 %b) { +entry: + %cmp = icmp ule i64 %a, %b + br i1 %cmp, label %bb1, label %bb2 +bb1: + ret i32 1 +bb2: + ret i32 2 + +; CHECK-LABEL: branch_ule: +; CHECK: movl 12(%esp), [[RHSLo:%[a-z]+]] +; CHECK: movl 16(%esp), [[RHSHi:%[a-z]+]] +; CHECK: cmpl 4(%esp), [[RHSLo]] +; CHECK: sbbl 8(%esp), [[RHSHi]] +; CHECK: jb [[FALSE:.LBB[0-9_]+]] +; CHECK: movl $1, %eax +; CHECK: retl +; CHECK: [[FALSE]]: +; CHECK: movl $2, %eax +; CHECK: retl +} + +define i32 @set_gt(i64 %a, i64 %b) { +entry: + %cmp = icmp sgt i64 %a, %b + %res = select i1 %cmp, i32 1, i32 0 + ret i32 %res + +; CHECK-LABEL: set_gt: +; CHECK: movl 12(%esp), [[RHSLo:%[a-z]+]] +; CHECK: movl 16(%esp), [[RHSHi:%[a-z]+]] +; CHECK: cmpl 4(%esp), [[RHSLo]] +; CHECK: sbbl 8(%esp), [[RHSHi]] +; CHECK: setl %al +; CHECK: retl +} + +define i32 @test_wide(i128 %a, i128 %b) { +entry: + %cmp = icmp slt i128 %a, %b + br i1 %cmp, label %bb1, label %bb2 +bb1: + ret i32 1 +bb2: + ret i32 2 + +; CHECK-LABEL: test_wide: +; CHECK: cmpl 24(%esp) +; CHECK: sbbl 28(%esp) +; CHECK: sbbl 32(%esp) +; CHECK: sbbl 36(%esp) +; CHECK: jge [[FALSE:.LBB[0-9_]+]] +; CHECK: movl $1, %eax +; CHECK: retl +; CHECK: [[FALSE]]: +; CHECK: movl $2, %eax +; CHECK: retl +} Index: test/CodeGen/X86/win32-pic-jumptable.ll =================================================================== --- test/CodeGen/X86/win32-pic-jumptable.ll +++ test/CodeGen/X86/win32-pic-jumptable.ll @@ -7,10 +7,10 @@ ; CHECK-NEXT: jmpl *%eax ; CHECK: LJTI0_0: +; CHECK-NEXT: .long LBB0_2-L0$pb +; CHECK-NEXT: .long LBB0_3-L0$pb ; CHECK-NEXT: .long LBB0_4-L0$pb ; CHECK-NEXT: .long LBB0_5-L0$pb -; CHECK-NEXT: .long LBB0_6-L0$pb -; CHECK-NEXT: .long LBB0_7-L0$pb target triple = "i686--windows-itanium"