[llvm] [IRTranslator] Simplify fixed vector ConstantAggregateZero handling. NFC (PR #108667)

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 13 17:22:41 PDT 2024


https://github.com/topperc created https://github.com/llvm/llvm-project/pull/108667

We don't need to loop through the elements, they're all the same zero. We can get the first element and create a splat build_vector.

>From ab356a5fadad1f2718b2976272f67ff2791f61c6 Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Fri, 13 Sep 2024 17:20:11 -0700
Subject: [PATCH] [IRTranslator] Simplify fixed vector ConstantAggregateZero
 handling. NFC

We don't need to loop through the elements, they're all the same
zero. We can get the first element and create a splat build_vector.
---
 llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)

diff --git a/llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp b/llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp
index b85087c23845d5..7fbefd05ae67c7 100644
--- a/llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp
@@ -3532,14 +3532,11 @@ bool IRTranslator::translate(const Constant &C, Register Reg) {
       return false;
     // Return the scalar if it is a <1 x Ty> vector.
     unsigned NumElts = CAZ->getElementCount().getFixedValue();
+    Constant &Elt = *CAZ->getElementValue(0u);
     if (NumElts == 1)
-      return translateCopy(C, *CAZ->getElementValue(0u), *EntryBuilder);
-    SmallVector<Register, 4> Ops;
-    for (unsigned I = 0; I < NumElts; ++I) {
-      Constant &Elt = *CAZ->getElementValue(I);
-      Ops.push_back(getOrCreateVReg(Elt));
-    }
-    EntryBuilder->buildBuildVector(Reg, Ops);
+      return translateCopy(C, Elt, *EntryBuilder);
+    // All elements are zero so we can just use the first one.
+    EntryBuilder->buildSplatBuildVector(Reg, getOrCreateVReg(Elt));
   } else if (auto CV = dyn_cast<ConstantDataVector>(&C)) {
     // Return the scalar if it is a <1 x Ty> vector.
     if (CV->getNumElements() == 1)



More information about the llvm-commits mailing list