[llvm] 20b1094 - [StackSafety,NFC] Replace map with vector

Vitaly Buka via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 19 02:32:31 PDT 2020


Author: Vitaly Buka
Date: 2020-06-19T02:32:17-07:00
New Revision: 20b1094a04a01c3f3b7c9edd071be8a30945af11

URL: https://github.com/llvm/llvm-project/commit/20b1094a04a01c3f3b7c9edd071be8a30945af11
DIFF: https://github.com/llvm/llvm-project/commit/20b1094a04a01c3f3b7c9edd071be8a30945af11.diff

LOG: [StackSafety,NFC] Replace map with vector

We don't need to lookup InstructionNumbering by number, so
we can use vector with index as assigned number.

Added: 
    

Modified: 
    llvm/include/llvm/Analysis/StackLifetime.h
    llvm/lib/Analysis/StackLifetime.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Analysis/StackLifetime.h b/llvm/include/llvm/Analysis/StackLifetime.h
index 40c826813696..5ebc6c81172f 100644
--- a/llvm/include/llvm/Analysis/StackLifetime.h
+++ b/llvm/include/llvm/Analysis/StackLifetime.h
@@ -94,11 +94,9 @@ class StackLifetime {
   using LivenessMap = DenseMap<const BasicBlock *, BlockLifetimeInfo>;
   LivenessMap BlockLiveness;
 
-  /// Number of interesting instructions.
-  int NumInst = -1;
-
-  /// Numeric ids for interesting instructions.
-  DenseMap<const IntrinsicInst *, unsigned> InstructionNumbering;
+  /// Interesting instructions. Instructions of the same block are adjustent
+  /// preserve in-block order.
+  SmallVector<const IntrinsicInst *, 64> Instructions;
 
   /// A range [Start, End) of instruction ids for each basic block.
   /// Instructions inside each BB have monotonic and consecutive ids.
@@ -137,7 +135,15 @@ class StackLifetime {
                 LivenessType Type);
 
   void run();
-  std::vector<const IntrinsicInst *> getMarkers() const;
+
+  iterator_range<
+      filter_iterator<ArrayRef<const IntrinsicInst *>::const_iterator,
+                      std::function<bool(const IntrinsicInst *)>>>
+  getMarkers() const {
+    std::function<bool(const IntrinsicInst *)> NotNull(
+        [](const IntrinsicInst *I) -> bool { return I; });
+    return make_filter_range(Instructions, NotNull);
+  }
 
   /// Returns a set of "interesting" instructions where the given alloca is
   /// live. Not all instructions in a function are interesting: we pick a set
@@ -147,8 +153,7 @@ class StackLifetime {
   /// Returns a live range that represents an alloca that is live throughout the
   /// entire function.
   LiveRange getFullLiveRange() const {
-    assert(NumInst >= 0);
-    return LiveRange(NumInst, true);
+    return LiveRange(Instructions.size(), true);
   }
 
   void print(raw_ostream &O);

diff  --git a/llvm/lib/Analysis/StackLifetime.cpp b/llvm/lib/Analysis/StackLifetime.cpp
index bd225157f0aa..0bc40c10597d 100644
--- a/llvm/lib/Analysis/StackLifetime.cpp
+++ b/llvm/lib/Analysis/StackLifetime.cpp
@@ -50,14 +50,6 @@ static bool readMarker(const Instruction *I, bool *IsStart) {
   return true;
 }
 
-std::vector<const IntrinsicInst *> StackLifetime::getMarkers() const {
-  std::vector<const IntrinsicInst *> Markers;
-  for (auto &M : InstructionNumbering)
-    if (M.getFirst()->isLifetimeStartOrEnd())
-      Markers.push_back(M.getFirst());
-  return Markers;
-}
-
 void StackLifetime::collectMarkers() {
   InterestingAllocas.resize(NumAllocas);
   DenseMap<const BasicBlock *, SmallDenseMap<const IntrinsicInst *, Marker>>
@@ -96,29 +88,28 @@ void StackLifetime::collectMarkers() {
   // * the list of markers in the instruction order
   // * the sets of allocas whose lifetime starts or ends in this BB
   LLVM_DEBUG(dbgs() << "Instructions:\n");
-  unsigned InstNo = 0;
   for (const BasicBlock *BB : depth_first(&F)) {
-    LLVM_DEBUG(dbgs() << "  " << InstNo << ": BB " << BB->getName() << "\n");
-    unsigned BBStart = InstNo++;
+    LLVM_DEBUG(dbgs() << "  " << Instructions.size() << ": BB " << BB->getName()
+                      << "\n");
+    auto BBStart = Instructions.size();
+    Instructions.push_back(nullptr);
 
     BlockLifetimeInfo &BlockInfo =
         BlockLiveness.try_emplace(BB, NumAllocas).first->getSecond();
 
     auto &BlockMarkerSet = BBMarkerSet[BB];
     if (BlockMarkerSet.empty()) {
-      unsigned BBEnd = InstNo;
-      BlockInstRange[BB] = std::make_pair(BBStart, BBEnd);
+      BlockInstRange[BB] = std::make_pair(BBStart, Instructions.size());
       continue;
     }
 
     auto ProcessMarker = [&](const IntrinsicInst *I, const Marker &M) {
-      LLVM_DEBUG(dbgs() << "  " << InstNo << ":  "
+      LLVM_DEBUG(dbgs() << "  " << Instructions.size() << ":  "
                         << (M.IsStart ? "start " : "end   ") << M.AllocaNo
                         << ", " << *I << "\n");
 
-      BBMarkers[BB].push_back({InstNo, M});
-
-      InstructionNumbering[I] = InstNo++;
+      BBMarkers[BB].push_back({Instructions.size(), M});
+      Instructions.push_back(I);
 
       if (M.IsStart) {
         BlockInfo.End.reset(M.AllocaNo);
@@ -145,10 +136,8 @@ void StackLifetime::collectMarkers() {
       }
     }
 
-    unsigned BBEnd = InstNo;
-    BlockInstRange[BB] = std::make_pair(BBStart, BBEnd);
+    BlockInstRange[BB] = std::make_pair(BBStart, Instructions.size());
   }
-  NumInst = InstNo;
 }
 
 void StackLifetime::calculateLocalLiveness() {
@@ -294,7 +283,7 @@ StackLifetime::StackLifetime(const Function &F,
 }
 
 void StackLifetime::run() {
-  LiveRanges.resize(NumAllocas, LiveRange(NumInst));
+  LiveRanges.resize(NumAllocas, LiveRange(Instructions.size()));
   for (unsigned I = 0; I < NumAllocas; ++I)
     if (!InterestingAllocas.test(I))
       LiveRanges[I] = getFullLiveRange();
@@ -308,10 +297,8 @@ void StackLifetime::run() {
 class StackLifetime::LifetimeAnnotationWriter
     : public AssemblyAnnotationWriter {
   const StackLifetime &SL;
-  SmallVector<StringRef, 16> Names;
-
   void printInstrAlive(unsigned InstrNo, formatted_raw_ostream &OS) {
-    Names.clear();
+    SmallVector<StringRef, 16> Names;
     for (const auto &KV : SL.AllocaNumbering) {
       if (SL.LiveRanges[KV.getSecond()].test(InstrNo))
         Names.push_back(KV.getFirst()->getName());
@@ -329,11 +316,11 @@ class StackLifetime::LifetimeAnnotationWriter
   }
 
   void printInfoComment(const Value &V, formatted_raw_ostream &OS) override {
-    auto It = SL.InstructionNumbering.find(dyn_cast<IntrinsicInst>(&V));
-    if (It == SL.InstructionNumbering.end())
+    auto It = llvm::find(SL.Instructions, &V);
+    if (It == SL.Instructions.end())
       return; // Unintresting.
     OS << "\n";
-    printInstrAlive(It->getSecond(), OS);
+    printInstrAlive(It - SL.Instructions.begin(), OS);
   }
 
 public:


        


More information about the llvm-commits mailing list