[llvm] r341531 - [NFC] Simplify inner structure of InstructionPrecedenceTracking

Max Kazantsev via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 6 02:29:42 PDT 2018


Author: mkazantsev
Date: Thu Sep  6 02:29:42 2018
New Revision: 341531

URL: http://llvm.org/viewvc/llvm-project?rev=341531&view=rev
Log:
[NFC] Simplify inner structure of InstructionPrecedenceTracking

Currently it has a set KnownBlocks that marks blocks as having cached
answers and a map FirstSpecialInsts that maps these blocks to first
special instructions in them. The value in the map is always non-null,
and for blocks that are known to have no special instructions the map
does not have an instance.

This patch removes KnownBlocks as obsolete. Instead, for blocks that
are known to have no special instructions, we just put a nullptr value.
This makes the code much easier to read.

Modified:
    llvm/trunk/include/llvm/Analysis/InstructionPrecedenceTracking.h
    llvm/trunk/lib/Analysis/InstructionPrecedenceTracking.cpp

Modified: llvm/trunk/include/llvm/Analysis/InstructionPrecedenceTracking.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Analysis/InstructionPrecedenceTracking.h?rev=341531&r1=341530&r2=341531&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Analysis/InstructionPrecedenceTracking.h (original)
+++ llvm/trunk/include/llvm/Analysis/InstructionPrecedenceTracking.h Thu Sep  6 02:29:42 2018
@@ -27,12 +27,12 @@
 namespace llvm {
 
 class InstructionPrecedenceTracking {
-  // Maps a block to the topmost special instruction in it.
+  // Maps a block to the topmost special instruction in it. If the value is
+  // nullptr, it means that it is known that this block does not contain any
+  // special instructions.
   DenseMap<const BasicBlock *, const Instruction *> FirstSpecialInsts;
   // Allows to answer queries about precedence of instructions within one block.
   OrderedInstructions OI;
-  // Blocks for which we have the up-to-date cached information.
-  SmallPtrSet<const BasicBlock *, 8> KnownBlocks;
 
   // Fills information about the given block's special instructions.
   void fill(const BasicBlock *BB);

Modified: llvm/trunk/lib/Analysis/InstructionPrecedenceTracking.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/InstructionPrecedenceTracking.cpp?rev=341531&r1=341530&r2=341531&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/InstructionPrecedenceTracking.cpp (original)
+++ llvm/trunk/lib/Analysis/InstructionPrecedenceTracking.cpp Thu Sep  6 02:29:42 2018
@@ -42,11 +42,11 @@ const Instruction *InstructionPrecedence
     validate(BB);
 #endif
 
-  if (!KnownBlocks.count(BB))
+  if (FirstSpecialInsts.find(BB) == FirstSpecialInsts.end()) {
     fill(BB);
-  auto *FirstICF = FirstSpecialInsts.lookup(BB);
-  assert((!FirstICF || FirstICF->getParent() == BB) && "Inconsistent cache!");
-  return FirstICF;
+    assert(FirstSpecialInsts.find(BB) != FirstSpecialInsts.end() && "Must be!");
+  }
+  return FirstSpecialInsts[BB];
 }
 
 bool InstructionPrecedenceTracking::hasSpecialInstructions(
@@ -66,63 +66,48 @@ void InstructionPrecedenceTracking::fill
   for (auto &I : *BB)
     if (isSpecialInstruction(&I)) {
       FirstSpecialInsts[BB] = &I;
-      break;
+      return;
     }
 
-  // Mark this block as having a known result.
-  KnownBlocks.insert(BB);
+  // Mark this block as having no special instructions.
+  FirstSpecialInsts[BB] = nullptr;
 }
 
 #ifndef NDEBUG
 void InstructionPrecedenceTracking::validate(const BasicBlock *BB) const {
-  // If we don't know anything about this block, make sure we don't store
-  // a bucket for it in FirstSpecialInsts map.
-  if (!KnownBlocks.count(BB)) {
-    assert(FirstSpecialInsts.find(BB) == FirstSpecialInsts.end() && "Must be!");
+  auto It = FirstSpecialInsts.find(BB);
+  // Bail if we don't have anything cached for this block.
+  if (It == FirstSpecialInsts.end())
     return;
-  }
 
-  auto It = FirstSpecialInsts.find(BB);
-  bool BlockHasSpecialInsns = false;
-  for (const Instruction &Insn : *BB) {
+  for (const Instruction &Insn : *BB)
     if (isSpecialInstruction(&Insn)) {
-      assert(It != FirstSpecialInsts.end() &&
-             "Blocked marked as known but we have no cached value for it!");
       assert(It->second == &Insn &&
              "Cached first special instruction is wrong!");
-      BlockHasSpecialInsns = true;
-      break;
+      return;
     }
-  }
-  if (!BlockHasSpecialInsns)
-    assert(It == FirstSpecialInsts.end() &&
-           "Block is marked as having special instructions but in fact it "
-           "has none!");
+
+  assert(It->second == nullptr &&
+         "Block is marked as having special instructions but in fact it  has "
+         "none!");
 }
 
 void InstructionPrecedenceTracking::validateAll() const {
   // Check that for every known block the cached value is correct.
-  for (auto *BB : KnownBlocks)
-    validate(BB);
-
-  // Check that all blocks with cached values are marked as known.
   for (auto &It : FirstSpecialInsts)
-    assert(KnownBlocks.count(It.first) &&
-           "We have a cached value but the block is not marked as known?");
+    validate(It.first);
 }
 #endif
 
 void InstructionPrecedenceTracking::invalidateBlock(const BasicBlock *BB) {
   OI.invalidateBlock(BB);
   FirstSpecialInsts.erase(BB);
-  KnownBlocks.erase(BB);
 }
 
 void InstructionPrecedenceTracking::clear() {
   for (auto It : FirstSpecialInsts)
     OI.invalidateBlock(It.first);
   FirstSpecialInsts.clear();
-  KnownBlocks.clear();
 #ifndef NDEBUG
   // The map should be valid after clearing (at least empty).
   validateAll();




More information about the llvm-commits mailing list