LLVM 23.0.0git
ControlFlowUtils.h
Go to the documentation of this file.
1//===- Transforms/Utils/ControlFlowUtils.h --------------------*- C++ -*---===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Utilities to manipulate the CFG and restore SSA for the new control flow.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_TRANSFORMS_UTILS_CONTROLFLOWUTILS_H
14#define LLVM_TRANSFORMS_UTILS_CONTROLFLOWUTILS_H
15
17#include "llvm/ADT/StringRef.h"
18
19namespace llvm {
20
21class BasicBlock;
22class CallBrInst;
23class LoopInfo;
24class DomTreeUpdater;
25
26/// Given a set of branch descriptors [BB, Succ0, Succ1], create a "hub" such
27/// that the control flow from each BB to a successor is now split into two
28/// edges, one from BB to the hub and another from the hub to the successor. The
29/// hub consists of a series of guard blocks, one for each outgoing block. Each
30/// guard block conditionally branches to the corresponding outgoing block, or
31/// the next guard block in the chain. These guard blocks are returned in the
32/// argument vector.
33///
34/// This also updates any PHINodes in the successor. For each such PHINode, the
35/// operands corresponding to incoming blocks are moved to a new PHINode in the
36/// hub, and the hub is made an operand of the original PHINode.
37///
38/// Note that for some block BB with a conditional branch, it is not necessary
39/// that both successors are rerouted. The client specifies this by setting
40/// either Succ0 or Succ1 to nullptr, in which case, the corresponding successor
41/// is not rerouted.
42///
43/// Input CFG:
44/// ----------
45///
46/// Def
47/// |
48/// v
49/// In1 In2
50/// | |
51/// | |
52/// v v
53/// Foo ---> Out1 Out2
54/// |
55/// v
56/// Use
57///
58///
59/// Create hub: Incoming = {In1, In2}, Outgoing = {Out1, Out2}
60/// ----------------------------------------------------------
61///
62/// Def
63/// |
64/// v
65/// In1 In2 Foo
66/// | Hub | |
67/// | + - - | - - + |
68/// | ' v ' V
69/// +------> Guard1 -----> Out1
70/// ' | '
71/// ' v '
72/// ' Guard2 -----> Out2
73/// ' ' |
74/// + - - - - - + |
75/// v
76/// Use
77///
78/// Limitations:
79/// -----------
80/// 1. This assumes that all terminators in the CFG are direct branches (the
81/// "br" instruction). The presence of any other control flow such as
82/// indirectbr, switch or callbr will cause an assert.
83///
84/// 2. The updates to the PHINodes are not sufficient to restore SSA
85/// form. Consider a definition Def, its use Use, incoming block In2 and
86/// outgoing block Out2, such that:
87/// a. In2 is reachable from D or contains D.
88/// b. U is reachable from Out2 or is contained in Out2.
89/// c. U is not a PHINode if U is contained in Out2.
90///
91/// Clearly, Def dominates Out2 since the program is valid SSA. But when the
92/// hub is introduced, there is a new path through the hub along which Use is
93/// reachable from entry without passing through Def, and SSA is no longer
94/// valid. To fix this, we need to look at all the blocks post-dominated by
95/// the hub on the one hand, and dominated by Out2 on the other. This is left
96/// for the caller to accomplish, since each specific use of this function
97/// may have additional information which simplifies this fixup. For example,
98/// see restoreSSA() in the UnifyLoopExits pass.
108
110 BasicBlock *Succ1 = nullptr) {
111 assert(BB);
112 assert(Succ0 || Succ1);
113 Branches.emplace_back(BB, Succ0, Succ1);
114 }
115
116 /// Return the unified loop exit block and a flag indicating if the CFG was
117 /// changed at all.
118 std::pair<BasicBlock *, bool>
120 const StringRef Prefix,
121 std::optional<unsigned> MaxControlFlowBooleans = std::nullopt);
122
124};
125
126} // end namespace llvm
127
128#endif // LLVM_TRANSFORMS_UTILS_CONTROLFLOWUTILS_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
arc branch finalize
This file defines the SmallVector class.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
CallBr instruction, tracking function calls that may not return control but instead transfer it to a ...
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
BranchDescriptor(BasicBlock *BB, BasicBlock *Succ0, BasicBlock *Succ1)
Given a set of branch descriptors [BB, Succ0, Succ1], create a "hub" such that the control flow from ...
void addBranch(BasicBlock *BB, BasicBlock *Succ0, BasicBlock *Succ1=nullptr)
SmallVector< BranchDescriptor > Branches