LLVM 24.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 DomTreeUpdater;
23
24/// Given a set of branch descriptors [BB, Succ0, Succ1], create a "hub" such
25/// that the control flow from each BB to a successor is now split into two
26/// edges, one from BB to the hub and another from the hub to the successor. The
27/// hub consists of a series of guard blocks, one for each outgoing block. Each
28/// guard block conditionally branches to the corresponding outgoing block, or
29/// the next guard block in the chain. These guard blocks are returned in the
30/// argument vector.
31///
32/// This also updates any PHINodes in the successor. For each such PHINode, the
33/// operands for the edges that are rerouted through the hub are moved to a new
34/// PHINode in the hub, and the hub is made an operand of the original PHINode.
35/// Operands for edges that are not rerouted are kept.
36///
37/// Note that for some block BB with a conditional branch, it is not necessary
38/// that both successors are rerouted. The client specifies this by setting
39/// either Succ0 or Succ1 to nullptr, in which case, the corresponding successor
40/// is not rerouted.
41///
42/// Input CFG:
43/// ----------
44///
45/// Def
46/// |
47/// v
48/// In1 In2
49/// | |
50/// | |
51/// v v
52/// Foo ---> Out1 Out2
53/// |
54/// v
55/// Use
56///
57///
58/// Create hub: Incoming = {In1, In2}, Outgoing = {Out1, Out2}
59/// ----------------------------------------------------------
60///
61/// Def
62/// |
63/// v
64/// In1 In2 Foo
65/// | Hub | |
66/// | + - - | - - + |
67/// | ' v ' V
68/// +------> Guard1 -----> Out1
69/// ' | '
70/// ' v '
71/// ' Guard2 -----> Out2
72/// ' ' |
73/// + - - - - - + |
74/// v
75/// Use
76///
77/// Limitations:
78/// -----------
79/// 1. This assumes that all terminators in the CFG are direct branches (the
80/// "br" instruction). The presence of any other control flow such as
81/// indirectbr, switch or callbr will cause an assert.
82///
83/// 2. The updates to the PHINodes are not sufficient to restore SSA
84/// form. Consider a definition Def, its use Use, incoming block In2 and
85/// outgoing block Out2, such that:
86/// a. In2 is reachable from D or contains D.
87/// b. U is reachable from Out2 or is contained in Out2.
88/// c. U is not a PHINode if U is contained in Out2.
89///
90/// Clearly, Def dominates Out2 since the program is valid SSA. But when the
91/// hub is introduced, there is a new path through the hub along which Use is
92/// reachable from entry without passing through Def, and SSA is no longer
93/// valid. To fix this, we need to look at all the blocks post-dominated by
94/// the hub on the one hand, and dominated by Out2 on the other. This is left
95/// for the caller to accomplish, since each specific use of this function
96/// may have additional information which simplifies this fixup. For example,
97/// see restoreSSA() in the UnifyLoopExits pass.
107
109 BasicBlock *Succ1 = nullptr) {
110 assert(BB);
111 assert(Succ0 || Succ1);
112 Branches.emplace_back(BB, Succ0, Succ1);
113 }
114
115 /// Return the unified loop exit block and a flag indicating if the CFG was
116 /// changed at all.
117 LLVM_ABI std::pair<BasicBlock *, bool>
119 const StringRef Prefix,
120 std::optional<unsigned> MaxControlFlowBooleans = std::nullopt);
121
123};
124
125} // end namespace llvm
126
127#endif // LLVM_TRANSFORMS_UTILS_CONTROLFLOWUTILS_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
arc branch finalize
#define LLVM_ABI
Definition Compiler.h:211
This file defines the SmallVector class.
LLVM Basic Block Representation.
Definition BasicBlock.h:61
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.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
This is an optimization pass for GlobalISel generic memory operations.
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