Skip to content

Commit

Permalink
unique_ptrify some containers in GlobalISel::RegisterBankInfo
Browse files Browse the repository at this point in the history
To simplify/clarify memory ownership, make leaks (as one was found/fixed
recently) harder to write, etc.

(also, while I was there - removed a duplicate lookup in a container)

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@293506 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
dwblaikie committed Jan 30, 2017
1 parent 6f6c6c9 commit ea0aa0f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 23 deletions.
8 changes: 4 additions & 4 deletions include/llvm/CodeGen/GlobalISel/RegisterBankInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -378,15 +378,15 @@ class RegisterBankInfo {

/// Keep dynamically allocated PartialMapping in a separate map.
/// This shouldn't be needed when everything gets TableGen'ed.
mutable DenseMap<unsigned, const PartialMapping *> MapOfPartialMappings;
mutable DenseMap<unsigned, std::unique_ptr<const PartialMapping>> MapOfPartialMappings;

/// Keep dynamically allocated ValueMapping in a separate map.
/// This shouldn't be needed when everything gets TableGen'ed.
mutable DenseMap<unsigned, const ValueMapping *> MapOfValueMappings;
mutable DenseMap<unsigned, std::unique_ptr<const ValueMapping> > MapOfValueMappings;

/// Keep dynamically allocated array of ValueMapping in a separate map.
/// This shouldn't be needed when everything gets TableGen'ed.
mutable DenseMap<unsigned, ValueMapping *> MapOfOperandsMappings;
mutable DenseMap<unsigned, std::unique_ptr<ValueMapping[]>> MapOfOperandsMappings;

/// Create a RegisterBankInfo that can accomodate up to \p NumRegBanks
/// RegisterBank instances.
Expand Down Expand Up @@ -512,7 +512,7 @@ class RegisterBankInfo {
}

public:
virtual ~RegisterBankInfo();
virtual ~RegisterBankInfo() = default;

/// Get the register bank identified by \p ID.
const RegisterBank &getRegBank(unsigned ID) const {
Expand Down
28 changes: 9 additions & 19 deletions lib/CodeGen/GlobalISel/RegisterBankInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,6 @@ RegisterBankInfo::RegisterBankInfo(RegisterBank **RegBanks,
#endif // NDEBUG
}

RegisterBankInfo::~RegisterBankInfo() {
for (auto It : MapOfPartialMappings)
delete It.second;
for (auto It : MapOfValueMappings)
delete It.second;
for (auto It : MapOfOperandsMappings)
delete[] It.second;
}

bool RegisterBankInfo::verify(const TargetRegisterInfo &TRI) const {
#ifndef NDEBUG
for (unsigned Idx = 0, End = getNumRegBanks(); Idx != End; ++Idx) {
Expand Down Expand Up @@ -236,8 +227,8 @@ RegisterBankInfo::getPartialMapping(unsigned StartIdx, unsigned Length,

++NumPartialMappingsCreated;

const PartialMapping *&PartMapping = MapOfPartialMappings[Hash];
PartMapping = new PartialMapping{StartIdx, Length, RegBank};
auto &PartMapping = MapOfPartialMappings[Hash];
PartMapping = llvm::make_unique<PartialMapping>(StartIdx, Length, RegBank);
return *PartMapping;
}

Expand Down Expand Up @@ -270,8 +261,8 @@ RegisterBankInfo::getValueMapping(const PartialMapping *BreakDown,

++NumValueMappingsCreated;

const ValueMapping *&ValMapping = MapOfValueMappings[Hash];
ValMapping = new ValueMapping{BreakDown, NumBreakDowns};
auto &ValMapping = MapOfValueMappings[Hash];
ValMapping = llvm::make_unique<ValueMapping>(BreakDown, NumBreakDowns);
return *ValMapping;
}

Expand All @@ -284,9 +275,9 @@ RegisterBankInfo::getOperandsMapping(Iterator Begin, Iterator End) const {
// The addresses of the value mapping are unique.
// Therefore, we can use them directly to hash the operand mapping.
hash_code Hash = hash_combine_range(Begin, End);
const auto &It = MapOfOperandsMappings.find(Hash);
if (It != MapOfOperandsMappings.end())
return It->second;
auto &Res = MapOfOperandsMappings[Hash];
if (Res)
return Res.get();

++NumOperandsMappingsCreated;

Expand All @@ -295,16 +286,15 @@ RegisterBankInfo::getOperandsMapping(Iterator Begin, Iterator End) const {
// mapping, because we use the pointer of the ValueMapping
// to hash and we expect them to uniquely identify an instance
// of value mapping.
ValueMapping *&Res = MapOfOperandsMappings[Hash];
Res = new ValueMapping[std::distance(Begin, End)];
Res = llvm::make_unique<ValueMapping[]>(std::distance(Begin, End));
unsigned Idx = 0;
for (Iterator It = Begin; It != End; ++It, ++Idx) {
const ValueMapping *ValMap = *It;
if (!ValMap)
continue;
Res[Idx] = *ValMap;
}
return Res;
return Res.get();
}

const RegisterBankInfo::ValueMapping *RegisterBankInfo::getOperandsMapping(
Expand Down

0 comments on commit ea0aa0f

Please sign in to comment.