[llvm] [Hexagon] Order objects on the stack by their alignments (PR #81280)

via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 9 10:11:27 PST 2024


================
@@ -2688,3 +2688,66 @@ bool HexagonFrameLowering::mayOverflowFrameOffset(MachineFunction &MF) const {
 
   return false;
 }
+
+namespace {
+// Struct used by orderFrameObjects to help sort the stack objects.
+struct HexagonFrameSortingObject {
+  bool IsValid = false;
+  unsigned Index = 0; // Index of Object into MFI list.
+  unsigned Size = 0;
+  Align ObjectAlignment = Align(1); // Alignment of Object in bytes.
+};
+
+struct HexagonFrameSortingComparator {
+  inline bool operator()(const HexagonFrameSortingObject &A,
+                         const HexagonFrameSortingObject &B) const {
+    return std::make_tuple(!A.IsValid, A.ObjectAlignment, A.Size) <
+           std::make_tuple(!B.IsValid, B.ObjectAlignment, B.Size);
+  }
+};
+} // namespace
+
+// Sort objects on the stack by alignment value and then by size to minimize
+// padding.
+void HexagonFrameLowering::orderFrameObjects(
+    const MachineFunction &MF, SmallVectorImpl<int> &ObjectsToAllocate) const {
+  const MachineFrameInfo &MFI = MF.getFrameInfo();
+  int NObjects = ObjectsToAllocate.size();
+
+  if (ObjectsToAllocate.empty())
+    return;
+
+  // Create an array of all MFI objects.
+  std::vector<HexagonFrameSortingObject> SortingObjects(
+      MFI.getObjectIndexEnd());
+
+  for (int i = 0, j = 0, e = MFI.getObjectIndexEnd(); i != e && j != NObjects;
+       ++i) {
+    if (i != ObjectsToAllocate[j])
----------------
androm3da wrote:

This `ObjectsToAllocate` list is a list of indices?  If so they should probably be declared as `unsigned`?  If we decide to change this to `unsigned` then the type for `i` should probably change to correspond.

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


More information about the llvm-commits mailing list