LLVM  8.0.1
VPlanVerifier.cpp
Go to the documentation of this file.
1 //===-- VPlanVerifier.cpp -------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// This file defines the class VPlanVerifier, which contains utility functions
12 /// to check the consistency and invariants of a VPlan.
13 ///
14 //===----------------------------------------------------------------------===//
15 
16 #include "VPlanVerifier.h"
18 
19 #define DEBUG_TYPE "loop-vectorize"
20 
21 using namespace llvm;
22 
23 static cl::opt<bool> EnableHCFGVerifier("vplan-verify-hcfg", cl::init(false),
24  cl::Hidden,
25  cl::desc("Verify VPlan H-CFG."));
26 
27 #ifndef NDEBUG
28 /// Utility function that checks whether \p VPBlockVec has duplicate
29 /// VPBlockBases.
30 static bool hasDuplicates(const SmallVectorImpl<VPBlockBase *> &VPBlockVec) {
32  for (const auto *Block : VPBlockVec) {
33  if (VPBlockSet.count(Block))
34  return true;
35  VPBlockSet.insert(Block);
36  }
37  return false;
38 }
39 #endif
40 
41 /// Helper function that verifies the CFG invariants of the VPBlockBases within
42 /// \p Region. Checks in this function are generic for VPBlockBases. They are
43 /// not specific for VPBasicBlocks or VPRegionBlocks.
45  for (const VPBlockBase *VPB :
48  // Check block's parent.
49  assert(VPB->getParent() == Region && "VPBlockBase has wrong parent");
50 
51  // Check block's condition bit.
52  if (VPB->getNumSuccessors() > 1)
53  assert(VPB->getCondBit() && "Missing condition bit!");
54  else
55  assert(!VPB->getCondBit() && "Unexpected condition bit!");
56 
57  // Check block's successors.
58  const auto &Successors = VPB->getSuccessors();
59  // There must be only one instance of a successor in block's successor list.
60  // TODO: This won't work for switch statements.
61  assert(!hasDuplicates(Successors) &&
62  "Multiple instances of the same successor.");
63 
64  for (const VPBlockBase *Succ : Successors) {
65  // There must be a bi-directional link between block and successor.
66  const auto &SuccPreds = Succ->getPredecessors();
67  assert(std::find(SuccPreds.begin(), SuccPreds.end(), VPB) !=
68  SuccPreds.end() &&
69  "Missing predecessor link.");
70  (void)SuccPreds;
71  }
72 
73  // Check block's predecessors.
74  const auto &Predecessors = VPB->getPredecessors();
75  // There must be only one instance of a predecessor in block's predecessor
76  // list.
77  // TODO: This won't work for switch statements.
78  assert(!hasDuplicates(Predecessors) &&
79  "Multiple instances of the same predecessor.");
80 
81  for (const VPBlockBase *Pred : Predecessors) {
82  // Block and predecessor must be inside the same region.
83  assert(Pred->getParent() == VPB->getParent() &&
84  "Predecessor is not in the same region.");
85 
86  // There must be a bi-directional link between block and predecessor.
87  const auto &PredSuccs = Pred->getSuccessors();
88  assert(std::find(PredSuccs.begin(), PredSuccs.end(), VPB) !=
89  PredSuccs.end() &&
90  "Missing successor link.");
91  (void)PredSuccs;
92  }
93  }
94 }
95 
96 /// Verify the CFG invariants of VPRegionBlock \p Region and its nested
97 /// VPBlockBases. Do not recurse inside nested VPRegionBlocks.
98 static void verifyRegion(const VPRegionBlock *Region) {
99  const VPBlockBase *Entry = Region->getEntry();
100  const VPBlockBase *Exit = Region->getExit();
101 
102  // Entry and Exit shouldn't have any predecessor/successor, respectively.
103  assert(!Entry->getNumPredecessors() && "Region entry has predecessors.");
104  assert(!Exit->getNumSuccessors() && "Region exit has successors.");
105  (void)Entry;
106  (void)Exit;
107 
108  verifyBlocksInRegion(Region);
109 }
110 
111 /// Verify the CFG invariants of VPRegionBlock \p Region and its nested
112 /// VPBlockBases. Recurse inside nested VPRegionBlocks.
113 static void verifyRegionRec(const VPRegionBlock *Region) {
114  verifyRegion(Region);
115 
116  // Recurse inside nested regions.
117  for (const VPBlockBase *VPB :
120  if (const auto *SubRegion = dyn_cast<VPRegionBlock>(VPB))
121  verifyRegionRec(SubRegion);
122  }
123 }
124 
126  const VPRegionBlock *TopRegion) const {
127  if (!EnableHCFGVerifier)
128  return;
129 
130  LLVM_DEBUG(dbgs() << "Verifying VPlan H-CFG.\n");
131  assert(!TopRegion->getParent() && "VPlan Top Region should have no parent.");
132  verifyRegionRec(TopRegion);
133 }
This class represents lattice values for constants.
Definition: AllocatorList.h:24
VPRegionBlock * getParent()
Definition: VPlan.h:406
VPRegionBlock represents a collection of VPBasicBlocks and VPRegionBlocks which form a Single-Entry-S...
Definition: VPlan.h:1050
static void verifyRegion(const VPRegionBlock *Region)
Verify the CFG invariants of VPRegionBlock Region and its nested VPBlockBases.
static bool hasDuplicates(const SmallVectorImpl< VPBlockBase *> &VPBlockVec)
Utility function that checks whether VPBlockVec has duplicate VPBlockBases.
static cl::opt< bool > EnableHCFGVerifier("vplan-verify-hcfg", cl::init(false), cl::Hidden, cl::desc("Verify VPlan H-CFG."))
const VPBlockBase * getExit() const
Definition: VPlan.h:1104
void verifyHierarchicalCFG(const VPRegionBlock *TopRegion) const
Verify the invariants of the H-CFG starting from TopRegion.
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:423
size_t getNumPredecessors() const
Definition: VPlan.h:442
size_t getNumSuccessors() const
Definition: VPlan.h:441
auto find(R &&Range, const T &Val) -> decltype(adl_begin(Range))
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1207
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
VPBlockBase is the building block of the Hierarchical Control-Flow Graph.
Definition: VPlan.h:334
static void verifyRegionRec(const VPRegionBlock *Region)
Verify the CFG invariants of VPRegionBlock Region and its nested VPBlockBases.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:133
Implements a dense probed hash-table based set with some number of buckets stored inline...
Definition: DenseSet.h:268
const VPBlockBase * getEntry() const
Definition: VPlan.h:1086
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
Definition: DenseSet.h:92
This file declares the class VPlanVerifier, which contains utility functions to check the consistency...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
#define LLVM_DEBUG(X)
Definition: Debug.h:123
static void verifyBlocksInRegion(const VPRegionBlock *Region)
Helper function that verifies the CFG invariants of the VPBlockBases within Region.