[llvm] [CGP]: Optimize mul.overflow. (PR #148343)
Hassnaa Hamdi via llvm-commits
llvm-commits at lists.llvm.org
Sun Aug 10 20:17:03 PDT 2025
================
@@ -6403,6 +6409,229 @@ bool CodeGenPrepare::optimizeGatherScatterInst(Instruction *MemoryInst,
return true;
}
+// Rewrite the umul_with_overflow intrinsic by checking if both of the
+// operands' value range is within the legal type. If so, we can optimize the
+// multiplication algorithm. This code is supposed to be written during the step
+// of type legalization, but given that we need to reconstruct the IR which is
+// not doable there, we do it here.
+bool CodeGenPrepare::optimizeUMulWithOverflow(Instruction *I) {
+ // Enable this optimization only for aarch64.
+ if (!TLI->getTargetMachine().getTargetTriple().isAArch64())
+ return false;
+ if (TLI->getTypeAction(
+ I->getContext(),
+ TLI->getValueType(*DL, I->getType()->getContainedType(0))) !=
+ TargetLowering::TypeExpandInteger)
+ return false;
+
+ Value *LHS = I->getOperand(0);
+ Value *RHS = I->getOperand(1);
+ auto *Ty = LHS->getType();
+ unsigned VTBitWidth = Ty->getScalarSizeInBits();
+ unsigned VTHalfBitWidth = VTBitWidth / 2;
+ auto *LegalTy = IntegerType::getIntNTy(I->getContext(), VTHalfBitWidth);
+
+ // Skip the optimization if the type with HalfBitWidth is not legal for the
+ // target.
+ if (TLI->getTypeAction(I->getContext(), TLI->getValueType(*DL, LegalTy)) !=
+ TargetLowering::TypeLegal)
+ return false;
+
+ I->getParent()->setName("overflow.res");
+ auto *OverflowResBB = I->getParent();
+ auto *OverflowoEntryBB =
+ I->getParent()->splitBasicBlock(I, "overflow.entry", /*Before*/ true);
+ BasicBlock *NoOverflowBB = BasicBlock::Create(
+ I->getContext(), "overflow.no", I->getFunction(), OverflowResBB);
+ BasicBlock *OverflowBB = BasicBlock::Create(I->getContext(), "overflow",
+ I->getFunction(), OverflowResBB);
+ // new blocks should be:
+ // entry:
+ // (lhs_lo ne lhs_hi) || (rhs_lo ne rhs_hi) ? overflow, overflow_no
+
+ // overflow_no:
+ // overflow:
+ // overflow.res:
+ //------------------------------------------------------------------------------
----------------
hassnaaHamdi wrote:
done
https://github.com/llvm/llvm-project/pull/148343
More information about the llvm-commits
mailing list