[llvm] r337714 - [ARM] Use unique_ptr to fix memory leak introduced in r337701

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 23 10:43:21 PDT 2018


Author: maskray
Date: Mon Jul 23 10:43:21 2018
New Revision: 337714

URL: http://llvm.org/viewvc/llvm-project?rev=337714&view=rev
Log:
[ARM] Use unique_ptr to fix memory leak introduced in r337701

Modified:
    llvm/trunk/lib/Target/ARM/ARMParallelDSP.cpp

Modified: llvm/trunk/lib/Target/ARM/ARMParallelDSP.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/ARM/ARMParallelDSP.cpp?rev=337714&r1=337713&r2=337714&view=diff
==============================================================================
--- llvm/trunk/lib/Target/ARM/ARMParallelDSP.cpp (original)
+++ llvm/trunk/lib/Target/ARM/ARMParallelDSP.cpp Mon Jul 23 10:43:21 2018
@@ -47,7 +47,7 @@ namespace {
   struct BinOpChain;
   struct Reduction;
 
-  using OpChainList     = SmallVector<OpChain*, 8>;
+  using OpChainList     = SmallVector<std::unique_ptr<OpChain>, 8>;
   using ReductionList   = SmallVector<Reduction, 8>;
   using ValueList       = SmallVector<Value*, 8>;
   using MemInstList     = SmallVector<Instruction*, 8>;
@@ -333,8 +333,8 @@ ARMParallelDSP::CreateParallelMACPairs(O
   // We can compare all elements, but then we need to compare and evaluate
   // different solutions.
   for(unsigned i=0; i<Elems-1; i+=2) {
-    BinOpChain *PMul0 = static_cast<BinOpChain*>(Candidates[i]);
-    BinOpChain *PMul1 = static_cast<BinOpChain*>(Candidates[i+1]);
+    BinOpChain *PMul0 = static_cast<BinOpChain*>(Candidates[i].get());
+    BinOpChain *PMul1 = static_cast<BinOpChain*>(Candidates[i+1].get());
     const Instruction *Mul0 = PMul0->Root;
     const Instruction *Mul1 = PMul1->Root;
 
@@ -456,7 +456,7 @@ static void AddMACCandidate(OpChainList
   if (IsNarrowSequence<16>(MulOp0, LHS) &&
       IsNarrowSequence<16>(MulOp1, RHS)) {
     LLVM_DEBUG(dbgs() << "OK, found narrow mul: "; Mul->dump());
-    Candidates.push_back(new BinOpChain(Mul, LHS, RHS));
+    Candidates.push_back(make_unique<BinOpChain>(Mul, LHS, RHS));
   }
 }
 
@@ -507,7 +507,7 @@ static void AliasCandidates(BasicBlock *
 static bool AreAliased(AliasAnalysis *AA, Instructions &Reads,
                        Instructions &Writes, OpChainList &MACCandidates) {
   LLVM_DEBUG(dbgs() << "Alias checks:\n");
-  for (auto *MAC : MACCandidates) {
+  for (auto &MAC : MACCandidates) {
     LLVM_DEBUG(dbgs() << "mul: "; MAC->Root->dump());
 
     // At the moment, we allow only simple chains that only consist of reads,
@@ -536,7 +536,7 @@ static bool AreAliased(AliasAnalysis *AA
 }
 
 static bool CheckMACMemory(OpChainList &Candidates) {
-  for (auto *C : Candidates) {
+  for (auto &C : Candidates) {
     // A mul has 2 operands, and a narrow op consist of sext and a load; thus
     // we expect at least 4 items in this operand value list.
     if (C->size() < 4) {
@@ -544,8 +544,8 @@ static bool CheckMACMemory(OpChainList &
       return false;
     }
     C->SetMemoryLocations();
-    ValueList &LHS = static_cast<BinOpChain*>(C)->LHS;
-    ValueList &RHS = static_cast<BinOpChain*>(C)->RHS;
+    ValueList &LHS = static_cast<BinOpChain*>(C.get())->LHS;
+    ValueList &RHS = static_cast<BinOpChain*>(C.get())->RHS;
 
     // Use +=2 to skip over the expected extend instructions.
     for (unsigned i = 0, e = LHS.size(); i < e; i += 2) {
@@ -604,7 +604,7 @@ bool ARMParallelDSP::MatchSMLAD(Function
     if (!CheckMACMemory(MACCandidates))
       continue;
 
-    R.MACCandidates = MACCandidates;
+    R.MACCandidates = std::move(MACCandidates);
 
     LLVM_DEBUG(dbgs() << "MAC candidates:\n";
       for (auto &M : R.MACCandidates)
@@ -623,8 +623,6 @@ bool ARMParallelDSP::MatchSMLAD(Function
       return false;
     PMACPairList PMACPairs = CreateParallelMACPairs(R.MACCandidates);
     Changed |= InsertParallelMACs(R, PMACPairs);
-    for (auto *C : R.MACCandidates)
-      delete C;
   }
 
   LLVM_DEBUG(if (Changed) dbgs() << "Header block:\n"; Header->dump(););




More information about the llvm-commits mailing list