1 //==------ llvm/CodeGen/LoopTraversal.h - Loop Traversal -*- C++ -*---------==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 /// \file Loop Traversal logic.
12 /// This class provides the basic blocks traversal order used by passes like
13 /// ReachingDefAnalysis and ExecutionDomainFix.
14 /// It identifies basic blocks that are part of loops and should to be visited
15 /// twice and returns efficient traversal order for all the blocks.
17 //===----------------------------------------------------------------------===//
19 #ifndef LLVM_CODEGEN_LOOPTRAVERSAL_H
20 #define LLVM_CODEGEN_LOOPTRAVERSAL_H
22 #include "llvm/ADT/SmallVector.h"
26 class MachineBasicBlock;
27 class MachineFunction;
29 /// This class provides the basic blocks traversal order used by passes like
30 /// ReachingDefAnalysis and ExecutionDomainFix.
31 /// It identifies basic blocks that are part of loops and should to be visited
32 /// twice and returns efficient traversal order for all the blocks.
34 /// We want to visit every instruction in every basic block in order to update
35 /// it's execution domain or collect clearance information. However, for the
36 /// clearance calculation, we need to know clearances from all predecessors
37 /// (including any backedges), therfore we need to visit some blocks twice.
38 /// As an example, consider the following loop.
41 /// PH -> A -> B (xmm<Undef> -> xmm<Def>) -> C -> D -> EXIT
43 /// +----------------------------------+
45 /// The iteration order this pass will return is as follows:
46 /// Optimized: PH A B C A' B' C' D
48 /// The basic block order is constructed as follows:
49 /// Once we finish processing some block, we update the counters in MBBInfos
50 /// and re-process any successors that are now 'done'.
51 /// We call a block that is ready for its final round of processing `done`
52 /// (isBlockDone), e.g. when all predecessor information is known.
54 /// Note that a naive traversal order would be to do two complete passes over
55 /// all basic blocks/instructions, the first for recording clearances, the
56 /// second for updating clearance based on backedges.
57 /// However, for functions without backedges, or functions with a lot of
58 /// straight-line code, and a small loop, that would be a lot of unnecessary
59 /// work (since only the BBs that are part of the loop require two passes).
61 /// E.g., the naive iteration order for the above exmple is as follows:
62 /// Naive: PH A B C D A' B' C' D'
64 /// In the optimized approach we avoid processing D twice, because we
65 /// can entirely process the predecessors before getting to D.
69 /// Whether we have gotten to this block in primary processing yet.
70 bool PrimaryCompleted = false;
72 /// The number of predecessors for which primary processing has completed
73 unsigned IncomingProcessed = 0;
75 /// The value of `IncomingProcessed` at the start of primary processing
76 unsigned PrimaryIncoming = 0;
78 /// The number of predecessors for which all processing steps are done.
79 unsigned IncomingCompleted = 0;
83 using MBBInfoMap = SmallVector<MBBInfo, 4>;
84 /// Helps keep track if we proccessed this block and all its predecessors.
88 struct TraversedMBBInfo {
90 MachineBasicBlock *MBB = nullptr;
92 /// True if this is the first time we process the basic block.
93 bool PrimaryPass = true;
95 /// True if the block that is ready for its final round of processing.
98 TraversedMBBInfo(MachineBasicBlock *BB = nullptr, bool Primary = true,
100 : MBB(BB), PrimaryPass(Primary), IsDone(Done) {}
104 /// Identifies basic blocks that are part of loops and should to be
105 /// visited twice and returns efficient traversal order for all the blocks.
106 typedef SmallVector<TraversedMBBInfo, 4> TraversalOrder;
107 TraversalOrder traverse(MachineFunction &MF);
110 /// Returens true if the block is ready for its final round of processing.
111 bool isBlockDone(MachineBasicBlock *MBB);
116 #endif // LLVM_CODEGEN_LOOPTRAVERSAL_H