[llvm] [InstCombine] Preserve disjoint or after folding casted bitwise logic (PR #136815)
Jim Lin via llvm-commits
llvm-commits at lists.llvm.org
Thu Apr 24 22:19:35 PDT 2025
================
@@ -1846,27 +1846,29 @@ Instruction *InstCombinerImpl::foldCastedBitwiseLogic(BinaryOperator &I) {
if (CastOpcode != Cast1->getOpcode())
return nullptr;
- // If the source types do not match, but the casts are matching extends, we
- // can still narrow the logic op.
- if (SrcTy != Cast1->getSrcTy()) {
- Value *X, *Y;
- if (match(Cast0, m_OneUse(m_ZExtOrSExt(m_Value(X)))) &&
- match(Cast1, m_OneUse(m_ZExtOrSExt(m_Value(Y))))) {
- // Cast the narrower source to the wider source type.
- unsigned XNumBits = X->getType()->getScalarSizeInBits();
- unsigned YNumBits = Y->getType()->getScalarSizeInBits();
- if (XNumBits < YNumBits)
- X = Builder.CreateCast(CastOpcode, X, Y->getType());
- else
- Y = Builder.CreateCast(CastOpcode, Y, X->getType());
- // Do the logic op in the intermediate width, then widen more.
- Value *NarrowLogic = Builder.CreateBinOp(LogicOpc, X, Y);
- return CastInst::Create(CastOpcode, NarrowLogic, DestTy);
- }
+ Value *X, *Y;
+ if (match(Cast0, m_OneUse(m_ZExtOrSExt(m_Value(X)))) &&
+ match(Cast1, m_OneUse(m_ZExtOrSExt(m_Value(Y))))) {
+ // Cast the narrower source to the wider source type.
+ unsigned XNumBits = X->getType()->getScalarSizeInBits();
+ unsigned YNumBits = Y->getType()->getScalarSizeInBits();
+ // If the source types do not match, but the casts are matching extends, we
+ // can still narrow the logic op.
+ if (XNumBits < YNumBits)
+ X = Builder.CreateCast(CastOpcode, X, Y->getType());
+ else if (YNumBits < XNumBits)
+ Y = Builder.CreateCast(CastOpcode, Y, X->getType());
+
+ // Do the logic op in the intermediate width, then widen more.
+ Value *NarrowLogic = Builder.CreateBinOp(LogicOpc, X, Y);
+ if (auto *Disjoint = dyn_cast<PossiblyDisjointInst>(&I);
+ Disjoint && Disjoint->isDisjoint())
+ cast<PossiblyDisjointInst>(NarrowLogic)->setIsDisjoint(true);
----------------
tclin914 wrote:
> We should also preserve the disjoint flag here:
>
> https://github.com/llvm/llvm-project/blob/1a78ef9a9eddd73de7932f5c33a7a7ad7e8b1806/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp#L1879
We handle the case logicop + sext/zext wherther or not the src type are the same in one place.
https://github.com/llvm/llvm-project/pull/136815
More information about the llvm-commits
mailing list