================
@@ -322,36 +322,26 @@ void RecordType::complete(ArrayRef<Type> members, bool 
packed, bool padded) {
     llvm_unreachable("failed to complete record");
 }
 
-/// Return the largest member of in the type.
-///
-/// Recurses into union members never returning a union as the largest member.
-Type RecordType::getLargestMember(const ::mlir::DataLayout &dataLayout) const {
-  assert(isUnion() && "Only call getLargestMember on unions");
+/// Return the 'storage' type of the union, that is, without padding,
+/// the type that is used to convert to the 'storage' for LLVM-IR.
+Type RecordType::getUnionStorageType(
+    const ::mlir::DataLayout &dataLayout) const {
+  assert(isUnion() && "Only call getUnionStorageType on unions");
   llvm::ArrayRef<Type> members = getMembers();
-  if (members.empty())
+  if (members.empty() || (getPadded() && members.size() == 1))
     return {};
 
   // If the union is padded, we need to ignore the last member,
   // which is the padding.
-  auto endIt = getPadded() ? std::prev(members.end()) : members.end();
-  if (endIt == members.begin())
-    return {};
-  return *std::max_element(members.begin(), endIt, [&](Type lhs, Type rhs) {
-    return dataLayout.getTypeABIAlignment(lhs) <
-               dataLayout.getTypeABIAlignment(rhs) ||
-           (dataLayout.getTypeABIAlignment(lhs) ==
-                dataLayout.getTypeABIAlignment(rhs) &&
-            dataLayout.getTypeSize(lhs) < dataLayout.getTypeSize(rhs));
-  });
-}
-
-mlir::Type RecordType::getPadding() const {
-  if (!getPadded())
-    return {};
-  llvm::ArrayRef<mlir::Type> members = getMembers();
-  if (members.empty())
-    return {};
-  return members.back();
+  return *std::max_element(
+      members.begin(), std::prev(members.end(), getPadded()),
----------------
adams381 wrote:

The `std::prev(members.end(), getPadded())` range expression (and the 
`max_element` loop) appears three times across `getUnionStorageType`, 
`getTypeSizeInBits`, and `getABIAlignment`. A small private helper would reduce 
the repetition:

```cpp
ArrayRef<Type> getActualMembers() const {
    return getMembers().drop_back(getPadded() ? 1 : 0);
}
```

https://github.com/llvm/llvm-project/pull/199292
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to