[llvm] [GlobalIsel][AArch64] Replace N bit G_ADD with N/2 bit G_ADD if the lower bits are known to be zeros (PR #101327)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 1 05:18:58 PDT 2024


================
@@ -5099,6 +5099,67 @@ bool CombinerHelper::matchAddEToAddO(MachineInstr &MI, BuildFnTy &MatchInfo) {
   return true;
 }
 
+bool CombinerHelper::matchAddWithKnownZeroLowerHalfBits(MachineInstr &MI,
+                                                        BuildFnTy &MatchInfo) {
+  GAdd *Add = cast<GAdd>(&MI);
+
+  const Register DstReg = Add->getReg(0);
+  const LLT FullTy = MRI.getType(DstReg);
+
+  if (!FullTy.isScalar())
+    return false;
+
+  const std::uint64_t FullSize = FullTy.getSizeInBits();
+  const std::uint64_t HalfSize = (FullSize + 1) / 2;
+  const LLT HalfTy = LLT::scalar(HalfSize);
+
+  if (isLegal({TargetOpcode::G_ADD, {FullTy}}) ||
+      !isLegal({TargetOpcode::G_ADD, {HalfTy}}) ||
+      !isLegalOrBeforeLegalizer(
+          {TargetOpcode::G_UNMERGE_VALUES, {HalfTy, FullTy}}) ||
+      !isLegalOrBeforeLegalizer(
+          {TargetOpcode::G_MERGE_VALUES, {FullTy, HalfTy}}))
+    return false;
+
+  const Register LhsReg = Add->getLHSReg();
+  const Register RhsReg = Add->getRHSReg();
+
+  const KnownBits LhsKnownBits = KB->getKnownBits(LhsReg);
+  const KnownBits LhsLoBits = LhsKnownBits.extractBits(HalfSize, 0);
+
+  const KnownBits RhsKnownBits = KB->getKnownBits(RhsReg);
+  const KnownBits RhsLoBits = RhsKnownBits.extractBits(HalfSize, 0);
----------------
arsenm wrote:

Check the RHS first, and early exit before checking LHS 

https://github.com/llvm/llvm-project/pull/101327


More information about the llvm-commits mailing list