1*4ba319b5SDimitry Andric //===-- VPlanVerifier.cpp -------------------------------------------------===//
2*4ba319b5SDimitry Andric //
3*4ba319b5SDimitry Andric //                     The LLVM Compiler Infrastructure
4*4ba319b5SDimitry Andric //
5*4ba319b5SDimitry Andric // This file is distributed under the University of Illinois Open Source
6*4ba319b5SDimitry Andric // License. See LICENSE.TXT for details.
7*4ba319b5SDimitry Andric //
8*4ba319b5SDimitry Andric //===----------------------------------------------------------------------===//
9*4ba319b5SDimitry Andric ///
10*4ba319b5SDimitry Andric /// \file
11*4ba319b5SDimitry Andric /// This file defines the class VPlanVerifier, which contains utility functions
12*4ba319b5SDimitry Andric /// to check the consistency and invariants of a VPlan.
13*4ba319b5SDimitry Andric ///
14*4ba319b5SDimitry Andric //===----------------------------------------------------------------------===//
15*4ba319b5SDimitry Andric 
16*4ba319b5SDimitry Andric #include "VPlanVerifier.h"
17*4ba319b5SDimitry Andric #include "llvm/ADT/DepthFirstIterator.h"
18*4ba319b5SDimitry Andric 
19*4ba319b5SDimitry Andric #define DEBUG_TYPE "loop-vectorize"
20*4ba319b5SDimitry Andric 
21*4ba319b5SDimitry Andric using namespace llvm;
22*4ba319b5SDimitry Andric 
23*4ba319b5SDimitry Andric static cl::opt<bool> EnableHCFGVerifier("vplan-verify-hcfg", cl::init(false),
24*4ba319b5SDimitry Andric                                         cl::Hidden,
25*4ba319b5SDimitry Andric                                         cl::desc("Verify VPlan H-CFG."));
26*4ba319b5SDimitry Andric 
27*4ba319b5SDimitry Andric #ifndef NDEBUG
28*4ba319b5SDimitry Andric /// Utility function that checks whether \p VPBlockVec has duplicate
29*4ba319b5SDimitry Andric /// VPBlockBases.
hasDuplicates(const SmallVectorImpl<VPBlockBase * > & VPBlockVec)30*4ba319b5SDimitry Andric static bool hasDuplicates(const SmallVectorImpl<VPBlockBase *> &VPBlockVec) {
31*4ba319b5SDimitry Andric   SmallDenseSet<const VPBlockBase *, 8> VPBlockSet;
32*4ba319b5SDimitry Andric   for (const auto *Block : VPBlockVec) {
33*4ba319b5SDimitry Andric     if (VPBlockSet.count(Block))
34*4ba319b5SDimitry Andric       return true;
35*4ba319b5SDimitry Andric     VPBlockSet.insert(Block);
36*4ba319b5SDimitry Andric   }
37*4ba319b5SDimitry Andric   return false;
38*4ba319b5SDimitry Andric }
39*4ba319b5SDimitry Andric #endif
40*4ba319b5SDimitry Andric 
41*4ba319b5SDimitry Andric /// Helper function that verifies the CFG invariants of the VPBlockBases within
42*4ba319b5SDimitry Andric /// \p Region. Checks in this function are generic for VPBlockBases. They are
43*4ba319b5SDimitry Andric /// not specific for VPBasicBlocks or VPRegionBlocks.
verifyBlocksInRegion(const VPRegionBlock * Region)44*4ba319b5SDimitry Andric static void verifyBlocksInRegion(const VPRegionBlock *Region) {
45*4ba319b5SDimitry Andric   for (const VPBlockBase *VPB :
46*4ba319b5SDimitry Andric        make_range(df_iterator<const VPBlockBase *>::begin(Region->getEntry()),
47*4ba319b5SDimitry Andric                   df_iterator<const VPBlockBase *>::end(Region->getExit()))) {
48*4ba319b5SDimitry Andric     // Check block's parent.
49*4ba319b5SDimitry Andric     assert(VPB->getParent() == Region && "VPBlockBase has wrong parent");
50*4ba319b5SDimitry Andric 
51*4ba319b5SDimitry Andric     // Check block's condition bit.
52*4ba319b5SDimitry Andric     if (VPB->getNumSuccessors() > 1)
53*4ba319b5SDimitry Andric       assert(VPB->getCondBit() && "Missing condition bit!");
54*4ba319b5SDimitry Andric     else
55*4ba319b5SDimitry Andric       assert(!VPB->getCondBit() && "Unexpected condition bit!");
56*4ba319b5SDimitry Andric 
57*4ba319b5SDimitry Andric     // Check block's successors.
58*4ba319b5SDimitry Andric     const auto &Successors = VPB->getSuccessors();
59*4ba319b5SDimitry Andric     // There must be only one instance of a successor in block's successor list.
60*4ba319b5SDimitry Andric     // TODO: This won't work for switch statements.
61*4ba319b5SDimitry Andric     assert(!hasDuplicates(Successors) &&
62*4ba319b5SDimitry Andric            "Multiple instances of the same successor.");
63*4ba319b5SDimitry Andric 
64*4ba319b5SDimitry Andric     for (const VPBlockBase *Succ : Successors) {
65*4ba319b5SDimitry Andric       // There must be a bi-directional link between block and successor.
66*4ba319b5SDimitry Andric       const auto &SuccPreds = Succ->getPredecessors();
67*4ba319b5SDimitry Andric       assert(std::find(SuccPreds.begin(), SuccPreds.end(), VPB) !=
68*4ba319b5SDimitry Andric                  SuccPreds.end() &&
69*4ba319b5SDimitry Andric              "Missing predecessor link.");
70*4ba319b5SDimitry Andric       (void)SuccPreds;
71*4ba319b5SDimitry Andric     }
72*4ba319b5SDimitry Andric 
73*4ba319b5SDimitry Andric     // Check block's predecessors.
74*4ba319b5SDimitry Andric     const auto &Predecessors = VPB->getPredecessors();
75*4ba319b5SDimitry Andric     // There must be only one instance of a predecessor in block's predecessor
76*4ba319b5SDimitry Andric     // list.
77*4ba319b5SDimitry Andric     // TODO: This won't work for switch statements.
78*4ba319b5SDimitry Andric     assert(!hasDuplicates(Predecessors) &&
79*4ba319b5SDimitry Andric            "Multiple instances of the same predecessor.");
80*4ba319b5SDimitry Andric 
81*4ba319b5SDimitry Andric     for (const VPBlockBase *Pred : Predecessors) {
82*4ba319b5SDimitry Andric       // Block and predecessor must be inside the same region.
83*4ba319b5SDimitry Andric       assert(Pred->getParent() == VPB->getParent() &&
84*4ba319b5SDimitry Andric              "Predecessor is not in the same region.");
85*4ba319b5SDimitry Andric 
86*4ba319b5SDimitry Andric       // There must be a bi-directional link between block and predecessor.
87*4ba319b5SDimitry Andric       const auto &PredSuccs = Pred->getSuccessors();
88*4ba319b5SDimitry Andric       assert(std::find(PredSuccs.begin(), PredSuccs.end(), VPB) !=
89*4ba319b5SDimitry Andric                  PredSuccs.end() &&
90*4ba319b5SDimitry Andric              "Missing successor link.");
91*4ba319b5SDimitry Andric       (void)PredSuccs;
92*4ba319b5SDimitry Andric     }
93*4ba319b5SDimitry Andric   }
94*4ba319b5SDimitry Andric }
95*4ba319b5SDimitry Andric 
96*4ba319b5SDimitry Andric /// Verify the CFG invariants of VPRegionBlock \p Region and its nested
97*4ba319b5SDimitry Andric /// VPBlockBases. Do not recurse inside nested VPRegionBlocks.
verifyRegion(const VPRegionBlock * Region)98*4ba319b5SDimitry Andric static void verifyRegion(const VPRegionBlock *Region) {
99*4ba319b5SDimitry Andric   const VPBlockBase *Entry = Region->getEntry();
100*4ba319b5SDimitry Andric   const VPBlockBase *Exit = Region->getExit();
101*4ba319b5SDimitry Andric 
102*4ba319b5SDimitry Andric   // Entry and Exit shouldn't have any predecessor/successor, respectively.
103*4ba319b5SDimitry Andric   assert(!Entry->getNumPredecessors() && "Region entry has predecessors.");
104*4ba319b5SDimitry Andric   assert(!Exit->getNumSuccessors() && "Region exit has successors.");
105*4ba319b5SDimitry Andric   (void)Entry;
106*4ba319b5SDimitry Andric   (void)Exit;
107*4ba319b5SDimitry Andric 
108*4ba319b5SDimitry Andric   verifyBlocksInRegion(Region);
109*4ba319b5SDimitry Andric }
110*4ba319b5SDimitry Andric 
111*4ba319b5SDimitry Andric /// Verify the CFG invariants of VPRegionBlock \p Region and its nested
112*4ba319b5SDimitry Andric /// VPBlockBases. Recurse inside nested VPRegionBlocks.
verifyRegionRec(const VPRegionBlock * Region)113*4ba319b5SDimitry Andric static void verifyRegionRec(const VPRegionBlock *Region) {
114*4ba319b5SDimitry Andric   verifyRegion(Region);
115*4ba319b5SDimitry Andric 
116*4ba319b5SDimitry Andric   // Recurse inside nested regions.
117*4ba319b5SDimitry Andric   for (const VPBlockBase *VPB :
118*4ba319b5SDimitry Andric        make_range(df_iterator<const VPBlockBase *>::begin(Region->getEntry()),
119*4ba319b5SDimitry Andric                   df_iterator<const VPBlockBase *>::end(Region->getExit()))) {
120*4ba319b5SDimitry Andric     if (const auto *SubRegion = dyn_cast<VPRegionBlock>(VPB))
121*4ba319b5SDimitry Andric       verifyRegionRec(SubRegion);
122*4ba319b5SDimitry Andric   }
123*4ba319b5SDimitry Andric }
124*4ba319b5SDimitry Andric 
verifyHierarchicalCFG(const VPRegionBlock * TopRegion) const125*4ba319b5SDimitry Andric void VPlanVerifier::verifyHierarchicalCFG(
126*4ba319b5SDimitry Andric     const VPRegionBlock *TopRegion) const {
127*4ba319b5SDimitry Andric   if (!EnableHCFGVerifier)
128*4ba319b5SDimitry Andric     return;
129*4ba319b5SDimitry Andric 
130*4ba319b5SDimitry Andric   LLVM_DEBUG(dbgs() << "Verifying VPlan H-CFG.\n");
131*4ba319b5SDimitry Andric   assert(!TopRegion->getParent() && "VPlan Top Region should have no parent.");
132*4ba319b5SDimitry Andric   verifyRegionRec(TopRegion);
133*4ba319b5SDimitry Andric }
134