Skip to content

Commit 4d5edeb

Browse files
author
v01dxyz
committed
[CodeGenPrepare] Do not despeculate count leading ones if promotion
For count leading ones, ie CTLZ (XOR Op -1), legalisation should be able to optimise this case when a promotion is necessary. Despeculation should not be applied in this case as it will separate XOR and CTLZ in two different basic blocks. This is particularly problematic with SelectionDAG.
1 parent 626c7ce commit 4d5edeb

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

llvm/lib/CodeGen/CodeGenPrepare.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2298,8 +2298,8 @@ static bool despeculateCountZeros(IntrinsicInst *CountZeros,
22982298
if (match(CountZeros->getOperand(1), m_One()))
22992299
return false;
23002300

2301-
// If it's cheap to speculate, there's nothing to do.
23022301
Type *Ty = CountZeros->getType();
2302+
// If it's cheap to speculate, there's nothing to do.
23032303
auto IntrinsicID = CountZeros->getIntrinsicID();
23042304
if ((IntrinsicID == Intrinsic::cttz && TLI->isCheapToSpeculateCttz(Ty)) ||
23052305
(IntrinsicID == Intrinsic::ctlz && TLI->isCheapToSpeculateCtlz(Ty)))
@@ -2310,6 +2310,21 @@ static bool despeculateCountZeros(IntrinsicInst *CountZeros,
23102310
if (Ty->isVectorTy() || SizeInBits > DL->getLargestLegalIntTypeSizeInBits())
23112311
return false;
23122312

2313+
// do not despeculate if we have (ctlz (xor op -1)) if the operand is
2314+
// promoted as legalisation would later transform to:
2315+
//
2316+
// (ctlz (lshift (xor (extend op) -1)
2317+
// lshiftamount))
2318+
//
2319+
// Despeculation is not only useless but also not wanted with SelectionDAG
2320+
// as XOR and CTLZ would be in different basic blocks.
2321+
EVT VTy = TLI->getValueType(*DL, Ty);
2322+
if (match(CountZeros->getOperand(0), m_Not(m_Value())) &&
2323+
(TLI->getTypeAction(CountZeros->getContext(), VTy) ==
2324+
TargetLowering::TypePromoteInteger ||
2325+
TLI->getOperationAction(ISD::CTLZ, VTy) == TargetLowering::Promote))
2326+
return false;
2327+
23132328
// Bail if the value is never zero.
23142329
Use &Op = CountZeros->getOperandUse(0);
23152330
if (isKnownNonZero(Op, *DL))

0 commit comments

Comments
 (0)