Skip to content
This repository was archived by the owner on Dec 20, 2019. It is now read-only.

Commit 657c311

Browse files
committed
Merging r312285:
------------------------------------------------------------------------ r312285 | ctopper | 2017-08-31 14:39:23 -0700 (Thu, 31 Aug 2017) | 11 lines [X86] Don't pull carry through X86ISD::ADD carryin, -1 if we can't guranteed we're really using the carry flag from the add. Prior to this patch we had a DAG combine that tried to bypass an X86ISD::ADD with -1 being added to the carry flag of some previous operation. We would then pass the carry flag directly to user. But this is only safe if the user is looking for the carry flag and not the zero flag. So we need to only do this combine in a context where we know what flag the consumer is using. Fixes PR34381. Differential Revision: https://reviews.llvm.org/D37317 ------------------------------------------------------------------------ git-svn-id: https://llvm.org/svn/llvm-project/llvm/branches/release_50@312333 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent a3b2103 commit 657c311

File tree

1 file changed

+45
-22
lines changed

1 file changed

+45
-22
lines changed

lib/Target/X86/X86ISelLowering.cpp

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30942,11 +30942,40 @@ static bool checkBoolTestAndOrSetCCCombine(SDValue Cond, X86::CondCode &CC0,
3094230942
return true;
3094330943
}
3094430944

30945+
// When legalizing carry, we create carries via add X, -1
30946+
// If that comes from an actual carry, via setcc, we use the
30947+
// carry directly.
30948+
static SDValue combineCarryThroughADD(SDValue EFLAGS) {
30949+
if (EFLAGS.getOpcode() == X86ISD::ADD) {
30950+
if (isAllOnesConstant(EFLAGS.getOperand(1))) {
30951+
SDValue Carry = EFLAGS.getOperand(0);
30952+
while (Carry.getOpcode() == ISD::TRUNCATE ||
30953+
Carry.getOpcode() == ISD::ZERO_EXTEND ||
30954+
Carry.getOpcode() == ISD::SIGN_EXTEND ||
30955+
Carry.getOpcode() == ISD::ANY_EXTEND ||
30956+
(Carry.getOpcode() == ISD::AND &&
30957+
isOneConstant(Carry.getOperand(1))))
30958+
Carry = Carry.getOperand(0);
30959+
if (Carry.getOpcode() == X86ISD::SETCC ||
30960+
Carry.getOpcode() == X86ISD::SETCC_CARRY) {
30961+
if (Carry.getConstantOperandVal(0) == X86::COND_B)
30962+
return Carry.getOperand(1);
30963+
}
30964+
}
30965+
}
30966+
30967+
return SDValue();
30968+
}
30969+
3094530970
/// Optimize an EFLAGS definition used according to the condition code \p CC
3094630971
/// into a simpler EFLAGS value, potentially returning a new \p CC and replacing
3094730972
/// uses of chain values.
3094830973
static SDValue combineSetCCEFLAGS(SDValue EFLAGS, X86::CondCode &CC,
3094930974
SelectionDAG &DAG) {
30975+
if (CC == X86::COND_B)
30976+
if (SDValue Flags = combineCarryThroughADD(EFLAGS))
30977+
return Flags;
30978+
3095030979
if (SDValue R = checkBoolTestSetCCCombine(EFLAGS, CC))
3095130980
return R;
3095230981
return combineSetCCAtomicArith(EFLAGS, CC, DAG);
@@ -34989,27 +35018,13 @@ static SDValue combineSIntToFP(SDNode *N, SelectionDAG &DAG,
3498935018
return SDValue();
3499035019
}
3499135020

34992-
// Optimize RES, EFLAGS = X86ISD::ADD LHS, RHS
34993-
static SDValue combineX86ADD(SDNode *N, SelectionDAG &DAG,
34994-
X86TargetLowering::DAGCombinerInfo &DCI) {
34995-
// When legalizing carry, we create carries via add X, -1
34996-
// If that comes from an actual carry, via setcc, we use the
34997-
// carry directly.
34998-
if (isAllOnesConstant(N->getOperand(1)) && N->hasAnyUseOfValue(1)) {
34999-
SDValue Carry = N->getOperand(0);
35000-
while (Carry.getOpcode() == ISD::TRUNCATE ||
35001-
Carry.getOpcode() == ISD::ZERO_EXTEND ||
35002-
Carry.getOpcode() == ISD::SIGN_EXTEND ||
35003-
Carry.getOpcode() == ISD::ANY_EXTEND ||
35004-
(Carry.getOpcode() == ISD::AND &&
35005-
isOneConstant(Carry.getOperand(1))))
35006-
Carry = Carry.getOperand(0);
35007-
35008-
if (Carry.getOpcode() == X86ISD::SETCC ||
35009-
Carry.getOpcode() == X86ISD::SETCC_CARRY) {
35010-
if (Carry.getConstantOperandVal(0) == X86::COND_B)
35011-
return DCI.CombineTo(N, SDValue(N, 0), Carry.getOperand(1));
35012-
}
35021+
static SDValue combineSBB(SDNode *N, SelectionDAG &DAG) {
35022+
if (SDValue Flags = combineCarryThroughADD(N->getOperand(2))) {
35023+
MVT VT = N->getSimpleValueType(0);
35024+
SDVTList VTs = DAG.getVTList(VT, MVT::i32);
35025+
return DAG.getNode(X86ISD::SBB, SDLoc(N), VTs,
35026+
N->getOperand(0), N->getOperand(1),
35027+
Flags);
3501335028
}
3501435029

3501535030
return SDValue();
@@ -35038,6 +35053,14 @@ static SDValue combineADC(SDNode *N, SelectionDAG &DAG,
3503835053
return DCI.CombineTo(N, Res1, CarryOut);
3503935054
}
3504035055

35056+
if (SDValue Flags = combineCarryThroughADD(N->getOperand(2))) {
35057+
MVT VT = N->getSimpleValueType(0);
35058+
SDVTList VTs = DAG.getVTList(VT, MVT::i32);
35059+
return DAG.getNode(X86ISD::ADC, SDLoc(N), VTs,
35060+
N->getOperand(0), N->getOperand(1),
35061+
Flags);
35062+
}
35063+
3504135064
return SDValue();
3504235065
}
3504335066

@@ -35677,7 +35700,7 @@ SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
3567735700
case X86ISD::CMOV: return combineCMov(N, DAG, DCI, Subtarget);
3567835701
case ISD::ADD: return combineAdd(N, DAG, Subtarget);
3567935702
case ISD::SUB: return combineSub(N, DAG, Subtarget);
35680-
case X86ISD::ADD: return combineX86ADD(N, DAG, DCI);
35703+
case X86ISD::SBB: return combineSBB(N, DAG);
3568135704
case X86ISD::ADC: return combineADC(N, DAG, DCI);
3568235705
case ISD::MUL: return combineMul(N, DAG, DCI, Subtarget);
3568335706
case ISD::SHL:

0 commit comments

Comments
 (0)