Skip to content
55 changes: 34 additions & 21 deletions llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,15 @@ struct PromoteMem2Reg {
/// number.
SmallVector<unsigned> BBNumPreds;

/// The state of incoming values for the current DFS step.
RenamePassData::ValVector IncomingVals;

/// The state of incoming locations for the current DFS step.
RenamePassData::LocationVector IncomingLocs;

// DFS work stack.
SmallVector<RenamePassData, 8> Worklist;

/// Whether the function has the no-signed-zeros-fp-math attribute set.
bool NoSignedZeros = false;

Expand Down Expand Up @@ -423,10 +432,7 @@ struct PromoteMem2Reg {
void ComputeLiveInBlocks(AllocaInst *AI, AllocaInfo &Info,
const SmallPtrSetImpl<BasicBlock *> &DefBlocks,
SmallPtrSetImpl<BasicBlock *> &LiveInBlocks);
void RenamePass(BasicBlock *BB, BasicBlock *Pred,
RenamePassData::ValVector IncVals,
RenamePassData::LocationVector IncLocs,
std::vector<RenamePassData> &Worklist);
void RenamePass(BasicBlock *BB, BasicBlock *Pred);
bool QueuePhiNode(BasicBlock *BB, unsigned AllocaIdx, unsigned &Version);

/// Delete dbg.assigns that have been demoted to dbg.values.
Expand All @@ -438,6 +444,20 @@ struct PromoteMem2Reg {
DVR->eraseFromParent();
DVRAssignsToDelete.clear();
}

void pushToWorklist(BasicBlock *BB, BasicBlock *Pred,
RenamePassData::ValVector IncVals,
RenamePassData::LocationVector IncLocs) {
Worklist.emplace_back(BB, Pred, std::move(IncVals), std::move(IncLocs));
}

RenamePassData popFromWorklist() {
RenamePassData R = std::move(Worklist.back());
Worklist.pop_back();
IncomingVals = std::move(R.Values);
IncomingLocs = std::move(R.Locations);
return R;
}
};

} // end anonymous namespace
Expand Down Expand Up @@ -849,29 +869,26 @@ void PromoteMem2Reg::run() {
// Set the incoming values for the basic block to be null values for all of
// the alloca's. We do this in case there is a load of a value that has not
// been stored yet. In this case, it will get this null value.
RenamePassData::ValVector Values(Allocas.size());
IncomingVals.resize(Allocas.size());
for (unsigned i = 0, e = Allocas.size(); i != e; ++i)
Values[i] = UndefValue::get(Allocas[i]->getAllocatedType());
IncomingVals[i] = UndefValue::get(Allocas[i]->getAllocatedType());

// When handling debug info, treat all incoming values as if they have unknown
// locations until proven otherwise.
RenamePassData::LocationVector Locations(Allocas.size());
IncomingLocs.resize(Allocas.size());

// The renamer uses the Visited set to avoid infinite loops.
Visited.resize(F.getMaxBlockNumber());

// Walks all basic blocks in the function performing the SSA rename algorithm
// and inserting the phi nodes we marked as necessary
std::vector<RenamePassData> RenamePassWorkList;
RenamePassWorkList.emplace_back(&F.front(), nullptr, std::move(Values),
std::move(Locations));
pushToWorklist(&F.front(), nullptr, std::move(IncomingVals),
std::move(IncomingLocs));
do {
RenamePassData RPD = std::move(RenamePassWorkList.back());
RenamePassWorkList.pop_back();
RenamePassData RPD = popFromWorklist();
// RenamePass may add new worklist entries.
RenamePass(RPD.BB, RPD.Pred, std::move(RPD.Values),
std::move(RPD.Locations), RenamePassWorkList);
} while (!RenamePassWorkList.empty());
RenamePass(RPD.BB, RPD.Pred);
} while (!Worklist.empty());

// Remove the allocas themselves from the function.
for (Instruction *A : Allocas) {
Expand Down Expand Up @@ -1096,10 +1113,7 @@ static void updateForIncomingValueLocation(PHINode *PN, DebugLoc DL,
///
/// IncomingVals indicates what value each Alloca contains on exit from the
/// predecessor block Pred.
void PromoteMem2Reg::RenamePass(BasicBlock *BB, BasicBlock *Pred,
RenamePassData::ValVector IncomingVals,
RenamePassData::LocationVector IncomingLocs,
std::vector<RenamePassData> &Worklist) {
void PromoteMem2Reg::RenamePass(BasicBlock *BB, BasicBlock *Pred) {
// If we are inserting any phi nodes into this BB, they will already be in the
// block.
if (PHINode *APN = dyn_cast<PHINode>(BB->begin())) {
Expand Down Expand Up @@ -1226,8 +1240,7 @@ void PromoteMem2Reg::RenamePass(BasicBlock *BB, BasicBlock *Pred,
IncomingVals = Worklist.back().Values;
IncomingLocs = Worklist.back().Locations;
}
Worklist.emplace_back(S, BB, std::move(IncomingVals),
std::move(IncomingLocs));
pushToWorklist(S, BB, std::move(IncomingVals), std::move(IncomingLocs));
}
}

Expand Down