@@ -266,7 +266,7 @@ class TransferTracker {
266266 // / object fields to track variable locations as we step through the block.
267267 // / FIXME: could just examine mloctracker instead of passing in \p mlocs?
268268 void
269- loadInlocs (MachineBasicBlock &MBB, ValueIDNum * MLocs,
269+ loadInlocs (MachineBasicBlock &MBB, ValueTable & MLocs,
270270 const SmallVectorImpl<std::pair<DebugVariable, DbgValue>> &VLocs,
271271 unsigned NumLocs) {
272272 ActiveMLocs.clear ();
@@ -1022,8 +1022,8 @@ bool InstrRefBasedLDV::transferDebugValue(const MachineInstr &MI) {
10221022}
10231023
10241024bool InstrRefBasedLDV::transferDebugInstrRef (MachineInstr &MI,
1025- ValueIDNum * *MLiveOuts,
1026- ValueIDNum * *MLiveIns) {
1025+ const ValueTable *MLiveOuts,
1026+ const ValueTable *MLiveIns) {
10271027 if (!MI.isDebugRef ())
10281028 return false ;
10291029
@@ -1782,8 +1782,8 @@ void InstrRefBasedLDV::accumulateFragmentMap(MachineInstr &MI) {
17821782 AllSeenFragments.insert (ThisFragment);
17831783}
17841784
1785- void InstrRefBasedLDV::process (MachineInstr &MI, ValueIDNum * *MLiveOuts,
1786- ValueIDNum * *MLiveIns) {
1785+ void InstrRefBasedLDV::process (MachineInstr &MI, const ValueTable *MLiveOuts,
1786+ const ValueTable *MLiveIns) {
17871787 // Try to interpret an MI as a debug or transfer instruction. Only if it's
17881788 // none of these should we interpret it's register defs as new value
17891789 // definitions.
@@ -1833,7 +1833,10 @@ void InstrRefBasedLDV::produceMLocTransferFunction(
18331833
18341834 // Step through each instruction in this block.
18351835 for (auto &MI : MBB) {
1836- process (MI);
1836+ // Pass in an empty unique_ptr for the value tables when accumulating the
1837+ // machine transfer function.
1838+ process (MI, nullptr , nullptr );
1839+
18371840 // Also accumulate fragment map.
18381841 if (MI.isDebugValue () || MI.isDebugRef ())
18391842 accumulateFragmentMap (MI);
@@ -1922,7 +1925,7 @@ void InstrRefBasedLDV::produceMLocTransferFunction(
19221925
19231926bool InstrRefBasedLDV::mlocJoin (
19241927 MachineBasicBlock &MBB, SmallPtrSet<const MachineBasicBlock *, 16 > &Visited,
1925- ValueIDNum ** OutLocs, ValueIDNum * InLocs) {
1928+ FuncValueTable & OutLocs, ValueTable & InLocs) {
19261929 LLVM_DEBUG (dbgs () << " join MBB: " << MBB.getNumber () << " \n " );
19271930 bool Changed = false ;
19281931
@@ -2023,7 +2026,7 @@ void InstrRefBasedLDV::findStackIndexInterference(
20232026
20242027void InstrRefBasedLDV::placeMLocPHIs (
20252028 MachineFunction &MF, SmallPtrSetImpl<MachineBasicBlock *> &AllBlocks,
2026- ValueIDNum ** MInLocs, SmallVectorImpl<MLocTransferMap> &MLocTransfer) {
2029+ FuncValueTable & MInLocs, SmallVectorImpl<MLocTransferMap> &MLocTransfer) {
20272030 SmallVector<unsigned , 4 > StackUnits;
20282031 findStackIndexInterference (StackUnits);
20292032
@@ -2152,7 +2155,7 @@ void InstrRefBasedLDV::placeMLocPHIs(
21522155}
21532156
21542157void InstrRefBasedLDV::buildMLocValueMap (
2155- MachineFunction &MF, ValueIDNum ** MInLocs, ValueIDNum ** MOutLocs,
2158+ MachineFunction &MF, FuncValueTable & MInLocs, FuncValueTable & MOutLocs,
21562159 SmallVectorImpl<MLocTransferMap> &MLocTransfer) {
21572160 std::priority_queue<unsigned int , std::vector<unsigned int >,
21582161 std::greater<unsigned int >>
@@ -2294,7 +2297,7 @@ void InstrRefBasedLDV::BlockPHIPlacement(
22942297
22952298Optional<ValueIDNum> InstrRefBasedLDV::pickVPHILoc (
22962299 const MachineBasicBlock &MBB, const DebugVariable &Var,
2297- const LiveIdxT &LiveOuts, ValueIDNum ** MOutLocs,
2300+ const LiveIdxT &LiveOuts, FuncValueTable & MOutLocs,
22982301 const SmallVectorImpl<const MachineBasicBlock *> &BlockOrders) {
22992302 // Collect a set of locations from predecessor where its live-out value can
23002303 // be found.
@@ -2562,7 +2565,7 @@ void InstrRefBasedLDV::getBlocksForScope(
25622565void InstrRefBasedLDV::buildVLocValueMap (
25632566 const DILocation *DILoc, const SmallSet<DebugVariable, 4 > &VarsWeCareAbout,
25642567 SmallPtrSetImpl<MachineBasicBlock *> &AssignBlocks, LiveInsT &Output,
2565- ValueIDNum ** MOutLocs, ValueIDNum ** MInLocs,
2568+ FuncValueTable & MOutLocs, FuncValueTable & MInLocs,
25662569 SmallVectorImpl<VLocTracker> &AllTheVLocs) {
25672570 // This method is much like buildMLocValueMap: but focuses on a single
25682571 // LexicalScope at a time. Pick out a set of blocks and variables that are
@@ -2947,7 +2950,7 @@ void InstrRefBasedLDV::makeDepthFirstEjectionMap(
29472950bool InstrRefBasedLDV::depthFirstVLocAndEmit (
29482951 unsigned MaxNumBlocks, const ScopeToDILocT &ScopeToDILocation,
29492952 const ScopeToVarsT &ScopeToVars, ScopeToAssignBlocksT &ScopeToAssignBlocks,
2950- LiveInsT &Output, ValueIDNum ** MOutLocs, ValueIDNum ** MInLocs,
2953+ LiveInsT &Output, FuncValueTable & MOutLocs, FuncValueTable & MInLocs,
29512954 SmallVectorImpl<VLocTracker> &AllTheVLocs, MachineFunction &MF,
29522955 DenseMap<DebugVariable, unsigned > &AllVarsNumbering,
29532956 const TargetPassConfig &TPC) {
@@ -2956,15 +2959,8 @@ bool InstrRefBasedLDV::depthFirstVLocAndEmit(
29562959 VTracker = nullptr ;
29572960
29582961 // No scopes? No variable locations.
2959- if (!LS.getCurrentFunctionScope ()) {
2960- // FIXME: this is a sticking plaster to prevent a memory leak, these
2961- // pointers will be automagically freed by being unique pointers, shortly.
2962- for (unsigned int I = 0 ; I < MaxNumBlocks; ++I) {
2963- delete[] MInLocs[I];
2964- delete[] MOutLocs[I];
2965- }
2962+ if (!LS.getCurrentFunctionScope ())
29662963 return false ;
2967- }
29682964
29692965 // Build map from block number to the last scope that uses the block.
29702966 SmallVector<unsigned , 16 > EjectionMap;
@@ -2988,17 +2984,14 @@ bool InstrRefBasedLDV::depthFirstVLocAndEmit(
29882984 CurBB = BBNum;
29892985 CurInst = 1 ;
29902986 for (auto &MI : MBB) {
2991- process (MI, MOutLocs, MInLocs);
2987+ process (MI, MOutLocs. get () , MInLocs. get () );
29922988 TTracker->checkInstForNewValues (CurInst, MI.getIterator ());
29932989 ++CurInst;
29942990 }
29952991
29962992 // Free machine-location tables for this block.
2997- delete[] MInLocs[BBNum];
2998- delete[] MOutLocs[BBNum];
2999- // Make ourselves brittle to use-after-free errors.
3000- MInLocs[BBNum] = nullptr ;
3001- MOutLocs[BBNum] = nullptr ;
2993+ MInLocs[BBNum].reset ();
2994+ MOutLocs[BBNum].reset ();
30022995 // We don't need live-in variable values for this block either.
30032996 Output[BBNum].clear ();
30042997 AllTheVLocs[BBNum].clear ();
@@ -3066,16 +3059,6 @@ bool InstrRefBasedLDV::depthFirstVLocAndEmit(
30663059 if (MOutLocs[MBB->getNumber ()])
30673060 EjectBlock (*MBB);
30683061
3069- // Finally, there might have been gaps in the block numbering, from dead
3070- // blocks being deleted or folded. In those scenarios, we might allocate a
3071- // block-table that's never ejected, meaning we have to free it at the end.
3072- for (unsigned int I = 0 ; I < MaxNumBlocks; ++I) {
3073- if (MInLocs[I]) {
3074- delete[] MInLocs[I];
3075- delete[] MOutLocs[I];
3076- }
3077- }
3078-
30793062 return emitTransfers (AllVarsNumbering);
30803063}
30813064
@@ -3173,13 +3156,13 @@ bool InstrRefBasedLDV::ExtendRanges(MachineFunction &MF,
31733156 // Allocate and initialize two array-of-arrays for the live-in and live-out
31743157 // machine values. The outer dimension is the block number; while the inner
31753158 // dimension is a LocIdx from MLocTracker.
3176- ValueIDNum ** MOutLocs = new ValueIDNum *[MaxNumBlocks] ;
3177- ValueIDNum ** MInLocs = new ValueIDNum *[MaxNumBlocks] ;
3159+ FuncValueTable MOutLocs = std::make_unique<ValueTable[]>(MaxNumBlocks) ;
3160+ FuncValueTable MInLocs = std::make_unique<ValueTable[]>(MaxNumBlocks) ;
31783161 unsigned NumLocs = MTracker->getNumLocs ();
31793162 for (int i = 0 ; i < MaxNumBlocks; ++i) {
31803163 // These all auto-initialize to ValueIDNum::EmptyValue
3181- MOutLocs[i] = new ValueIDNum[NumLocs] ;
3182- MInLocs[i] = new ValueIDNum[NumLocs] ;
3164+ MOutLocs[i] = std::make_unique< ValueIDNum[]>(NumLocs) ;
3165+ MInLocs[i] = std::make_unique< ValueIDNum[]>(NumLocs) ;
31833166 }
31843167
31853168 // Solve the machine value dataflow problem using the MLocTransfer function,
@@ -3216,7 +3199,7 @@ bool InstrRefBasedLDV::ExtendRanges(MachineFunction &MF,
32163199 MTracker->loadFromArray (MInLocs[CurBB], CurBB);
32173200 CurInst = 1 ;
32183201 for (auto &MI : MBB) {
3219- process (MI, MOutLocs, MInLocs);
3202+ process (MI, MOutLocs. get () , MInLocs. get () );
32203203 ++CurInst;
32213204 }
32223205 MTracker->reset ();
@@ -3271,12 +3254,6 @@ bool InstrRefBasedLDV::ExtendRanges(MachineFunction &MF,
32713254 << " has " << MaxNumBlocks << " basic blocks and "
32723255 << VarAssignCount
32733256 << " variable assignments, exceeding limits.\n " );
3274-
3275- // Perform memory cleanup that emitLocations would do otherwise.
3276- for (int Idx = 0 ; Idx < MaxNumBlocks; ++Idx) {
3277- delete[] MOutLocs[Idx];
3278- delete[] MInLocs[Idx];
3279- }
32803257 } else {
32813258 // Optionally, solve the variable value problem and emit to blocks by using
32823259 // a lexical-scope-depth search. It should be functionally identical to
@@ -3286,10 +3263,6 @@ bool InstrRefBasedLDV::ExtendRanges(MachineFunction &MF,
32863263 SavedLiveIns, MOutLocs, MInLocs, vlocs, MF, AllVarsNumbering, *TPC);
32873264 }
32883265
3289- // Elements of these arrays will be deleted by emitLocations.
3290- delete[] MOutLocs;
3291- delete[] MInLocs;
3292-
32933266 delete MTracker;
32943267 delete TTracker;
32953268 MTracker = nullptr ;
@@ -3406,9 +3379,10 @@ class LDVSSAUpdater {
34063379 // / Machine location where any PHI must occur.
34073380 LocIdx Loc;
34083381 // / Table of live-in machine value numbers for blocks / locations.
3409- ValueIDNum * *MLiveIns;
3382+ const ValueTable *MLiveIns;
34103383
3411- LDVSSAUpdater (LocIdx L, ValueIDNum **MLiveIns) : Loc(L), MLiveIns(MLiveIns) {}
3384+ LDVSSAUpdater (LocIdx L, const ValueTable *MLiveIns)
3385+ : Loc(L), MLiveIns(MLiveIns) {}
34123386
34133387 void reset () {
34143388 for (auto &Block : BlockMap)
@@ -3565,11 +3539,13 @@ template <> class SSAUpdaterTraits<LDVSSAUpdater> {
35653539
35663540} // end namespace llvm
35673541
3568- Optional<ValueIDNum> InstrRefBasedLDV::resolveDbgPHIs (MachineFunction &MF,
3569- ValueIDNum **MLiveOuts,
3570- ValueIDNum **MLiveIns,
3571- MachineInstr &Here,
3572- uint64_t InstrNum) {
3542+ Optional<ValueIDNum> InstrRefBasedLDV::resolveDbgPHIs (
3543+ MachineFunction &MF, const ValueTable *MLiveOuts,
3544+ const ValueTable *MLiveIns, MachineInstr &Here, uint64_t InstrNum) {
3545+ assert (MLiveOuts && MLiveIns &&
3546+ " Tried to resolve DBG_PHI before location "
3547+ " tables allocated?" );
3548+
35733549 // This function will be called twice per DBG_INSTR_REF, and might end up
35743550 // computing lots of SSA information: memoize it.
35753551 auto SeenDbgPHIIt = SeenDbgPHIs.find (&Here);
@@ -3583,8 +3559,8 @@ Optional<ValueIDNum> InstrRefBasedLDV::resolveDbgPHIs(MachineFunction &MF,
35833559}
35843560
35853561Optional<ValueIDNum> InstrRefBasedLDV::resolveDbgPHIsImpl (
3586- MachineFunction &MF, ValueIDNum **MLiveOuts, ValueIDNum **MLiveIns ,
3587- MachineInstr &Here, uint64_t InstrNum) {
3562+ MachineFunction &MF, const ValueTable *MLiveOuts ,
3563+ const ValueTable *MLiveIns, MachineInstr &Here, uint64_t InstrNum) {
35883564 // Pick out records of DBG_PHI instructions that have been observed. If there
35893565 // are none, then we cannot compute a value number.
35903566 auto RangePair = std::equal_range (DebugPHINumToValue.begin (),
@@ -3691,7 +3667,7 @@ Optional<ValueIDNum> InstrRefBasedLDV::resolveDbgPHIsImpl(
36913667 return None;
36923668
36933669 ValueIDNum ValueToCheck;
3694- ValueIDNum * BlockLiveOuts = MLiveOuts[PHIIt.first ->BB .getNumber ()];
3670+ const ValueTable & BlockLiveOuts = MLiveOuts[PHIIt.first ->BB .getNumber ()];
36953671
36963672 auto VVal = ValidatedValues.find (PHIIt.first );
36973673 if (VVal == ValidatedValues.end ()) {
0 commit comments