1//===- ControlFlowUtils.cpp - Control Flow Utilities -----------------------==//
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#include "llvm/Transforms/Utils/ControlFlowUtils.h"
14#include "llvm/ADT/SetVector.h"
15#include "llvm/Analysis/DomTreeUpdater.h"
16#include "llvm/Analysis/LoopInfo.h"
17#include "llvm/IR/Constants.h"
18#include "llvm/IR/Instructions.h"
19#include "llvm/IR/ValueHandle.h"
20#include "llvm/Transforms/Utils/Local.h"
21
22#define DEBUG_TYPE "control-flow-hub"
23
24using namespace llvm;
25
26using BBPredicates = DenseMap<BasicBlock *, Instruction *>;
27using EdgeDescriptor = ControlFlowHub::BranchDescriptor;
28
29// Redirects the terminator of the incoming block to the first guard block in
30// the hub. Returns the branch condition from `BB` if it exits.
31// - If only one of Succ0 or Succ1 is not null, the corresponding branch
32// successor is redirected to the FirstGuardBlock.
33// - Else both are not null, and branch is replaced with an unconditional
34// branch to the FirstGuardBlock.
35static Value *redirectToHub(BasicBlock *BB, BasicBlock *Succ0,
36 BasicBlock *Succ1, BasicBlock *FirstGuardBlock) {
37 if (auto *Branch = dyn_cast<UncondBrInst>(Val: BB->getTerminator())) {
38 assert(Succ0 == Branch->getSuccessor(0));
39 assert(!Succ1);
40 Branch->setSuccessor(FirstGuardBlock);
41 return nullptr;
42 }
43
44 auto *Branch = cast<CondBrInst>(Val: BB->getTerminator());
45 auto *Condition = Branch->getCondition();
46
47 assert(Succ0 || Succ1);
48 assert(!Succ1 || Succ1 == Branch->getSuccessor(1));
49 if (Succ0 && !Succ1) {
50 Branch->setSuccessor(idx: 0, NewSucc: FirstGuardBlock);
51 } else if (Succ1 && !Succ0) {
52 Branch->setSuccessor(idx: 1, NewSucc: FirstGuardBlock);
53 } else {
54 Branch->eraseFromParent();
55 UncondBrInst::Create(IfTrue: FirstGuardBlock, InsertBefore: BB);
56 }
57
58 return Condition;
59}
60
61// Setup the branch instructions for guard blocks.
62//
63// Each guard block terminates in a conditional branch that transfers
64// control to the corresponding outgoing block or the next guard
65// block. The last guard block has two outgoing blocks as successors.
66static void setupBranchForGuard(ArrayRef<BasicBlock *> GuardBlocks,
67 ArrayRef<BasicBlock *> Outgoing,
68 BBPredicates &GuardPredicates) {
69 assert(Outgoing.size() > 1);
70 assert(GuardBlocks.size() == Outgoing.size() - 1);
71 int I = 0;
72 for (int E = GuardBlocks.size() - 1; I != E; ++I) {
73 BasicBlock *Out = Outgoing[I];
74 CondBrInst::Create(Cond: GuardPredicates[Out], IfTrue: Out, IfFalse: GuardBlocks[I + 1],
75 InsertBefore: GuardBlocks[I]);
76 }
77 BasicBlock *Out = Outgoing[I];
78 CondBrInst::Create(Cond: GuardPredicates[Out], IfTrue: Out, IfFalse: Outgoing[I + 1],
79 InsertBefore: GuardBlocks[I]);
80}
81
82// Assign an index to each outgoing block. At the corresponding guard
83// block, compute the branch condition by comparing this index.
84static void calcPredicateUsingInteger(ArrayRef<EdgeDescriptor> Branches,
85 ArrayRef<BasicBlock *> Outgoing,
86 ArrayRef<BasicBlock *> GuardBlocks,
87 BBPredicates &GuardPredicates) {
88 LLVMContext &Context = GuardBlocks.front()->getContext();
89 BasicBlock *FirstGuardBlock = GuardBlocks.front();
90 Type *Int32Ty = Type::getInt32Ty(C&: Context);
91
92 auto *Phi = PHINode::Create(Ty: Int32Ty, NumReservedValues: Branches.size(), NameStr: "merged.bb.idx",
93 InsertBefore: FirstGuardBlock);
94
95 for (auto [BB, Succ0, Succ1] : Branches) {
96 Value *Condition = redirectToHub(BB, Succ0, Succ1, FirstGuardBlock);
97 Value *IncomingId = nullptr;
98 if (Succ0 && Succ1 && Succ0 != Succ1) {
99 auto Succ0Iter = find(Range&: Outgoing, Val: Succ0);
100 auto Succ1Iter = find(Range&: Outgoing, Val: Succ1);
101 Value *Id0 =
102 ConstantInt::get(Ty: Int32Ty, V: std::distance(first: Outgoing.begin(), last: Succ0Iter));
103 Value *Id1 =
104 ConstantInt::get(Ty: Int32Ty, V: std::distance(first: Outgoing.begin(), last: Succ1Iter));
105 IncomingId = SelectInst::Create(C: Condition, S1: Id0, S2: Id1, NameStr: "target.bb.idx",
106 InsertBefore: BB->getTerminator()->getIterator());
107 } else {
108 // Get the index of the non-null successor, or when both successors
109 // are the same block, use that block's index directly.
110 auto SuccIter = Succ0 ? find(Range&: Outgoing, Val: Succ0) : find(Range&: Outgoing, Val: Succ1);
111 IncomingId =
112 ConstantInt::get(Ty: Int32Ty, V: std::distance(first: Outgoing.begin(), last: SuccIter));
113 }
114 Phi->addIncoming(V: IncomingId, BB);
115 }
116
117 for (int I = 0, E = Outgoing.size() - 1; I != E; ++I) {
118 BasicBlock *Out = Outgoing[I];
119 LLVM_DEBUG(dbgs() << "Creating integer guard for " << Out->getName()
120 << "\n");
121 auto *Cmp = ICmpInst::Create(Op: Instruction::ICmp, Pred: ICmpInst::ICMP_EQ, S1: Phi,
122 S2: ConstantInt::get(Ty: Int32Ty, V: I),
123 Name: Out->getName() + ".predicate", InsertBefore: GuardBlocks[I]);
124 GuardPredicates[Out] = Cmp;
125 }
126}
127
128// Determine the branch condition to be used at each guard block from the
129// original boolean values.
130static void calcPredicateUsingBooleans(
131 ArrayRef<EdgeDescriptor> Branches, ArrayRef<BasicBlock *> Outgoing,
132 SmallVectorImpl<BasicBlock *> &GuardBlocks, BBPredicates &GuardPredicates,
133 SmallVectorImpl<WeakVH> &DeletionCandidates) {
134 LLVMContext &Context = GuardBlocks.front()->getContext();
135 auto *BoolTrue = ConstantInt::getTrue(Context);
136 auto *BoolFalse = ConstantInt::getFalse(Context);
137 BasicBlock *FirstGuardBlock = GuardBlocks.front();
138
139 // The predicate for the last outgoing is trivially true, and so we
140 // process only the first N-1 successors.
141 for (int I = 0, E = Outgoing.size() - 1; I != E; ++I) {
142 BasicBlock *Out = Outgoing[I];
143 LLVM_DEBUG(dbgs() << "Creating boolean guard for " << Out->getName()
144 << "\n");
145
146 auto *Phi =
147 PHINode::Create(Ty: Type::getInt1Ty(C&: Context), NumReservedValues: Branches.size(),
148 NameStr: StringRef("Guard.") + Out->getName(), InsertBefore: FirstGuardBlock);
149 GuardPredicates[Out] = Phi;
150 }
151
152 for (auto [BB, Succ0, Succ1] : Branches) {
153 Value *Condition = redirectToHub(BB, Succ0, Succ1, FirstGuardBlock);
154
155 // Optimization: Consider an incoming block A with both successors
156 // Succ0 and Succ1 in the set of outgoing blocks. The predicates
157 // for Succ0 and Succ1 complement each other. If Succ0 is visited
158 // first in the loop below, control will branch to Succ0 using the
159 // corresponding predicate. But if that branch is not taken, then
160 // control must reach Succ1, which means that the incoming value of
161 // the predicate from `BB` is true for Succ1.
162 bool OneSuccessorDone = false;
163 for (int I = 0, E = Outgoing.size() - 1; I != E; ++I) {
164 BasicBlock *Out = Outgoing[I];
165 PHINode *Phi = cast<PHINode>(Val: GuardPredicates[Out]);
166 if (Out != Succ0 && Out != Succ1) {
167 Phi->addIncoming(V: BoolFalse, BB);
168 } else if (!Succ0 || !Succ1 || Succ0 == Succ1 || OneSuccessorDone) {
169 // Optimization: When only one successor is an outgoing block,
170 // or both successors are the same block, the incoming predicate
171 // from `BB` is always true.
172 Phi->addIncoming(V: BoolTrue, BB);
173 } else {
174 assert(Succ0 && Succ1);
175 if (Out == Succ0) {
176 Phi->addIncoming(V: Condition, BB);
177 } else {
178 Value *Inverted = invertCondition(Condition);
179 DeletionCandidates.push_back(Elt: Condition);
180 Phi->addIncoming(V: Inverted, BB);
181 }
182 OneSuccessorDone = true;
183 }
184 }
185 }
186}
187
188// Capture the existing control flow as guard predicates, and redirect
189// control flow from \p Incoming block through the \p GuardBlocks to the
190// \p Outgoing blocks.
191//
192// There is one guard predicate for each outgoing block OutBB. The
193// predicate represents whether the hub should transfer control flow
194// to OutBB. These predicates are NOT ORTHOGONAL. The Hub evaluates
195// them in the same order as the Outgoing set-vector, and control
196// branches to the first outgoing block whose predicate evaluates to true.
197//
198// The last guard block has two outgoing blocks as successors since the
199// condition for the final outgoing block is trivially true. So we create one
200// less block (including the first guard block) than the number of outgoing
201// blocks.
202static void convertToGuardPredicates(
203 ArrayRef<EdgeDescriptor> Branches, ArrayRef<BasicBlock *> Outgoing,
204 SmallVectorImpl<BasicBlock *> &GuardBlocks,
205 SmallVectorImpl<WeakVH> &DeletionCandidates, const StringRef Prefix,
206 std::optional<unsigned> MaxControlFlowBooleans) {
207 BBPredicates GuardPredicates;
208 Function *F = Outgoing.front()->getParent();
209
210 for (int I = 0, E = Outgoing.size() - 1; I != E; ++I)
211 GuardBlocks.push_back(
212 Elt: BasicBlock::Create(Context&: F->getContext(), Name: Prefix + ".guard", Parent: F));
213
214 // When we are using an integer to record which target block to jump to, we
215 // are creating less live values, actually we are using one single integer to
216 // store the index of the target block. When we are using booleans to store
217 // the branching information, we need (N-1) boolean values, where N is the
218 // number of outgoing block.
219 if (!MaxControlFlowBooleans || Outgoing.size() <= *MaxControlFlowBooleans)
220 calcPredicateUsingBooleans(Branches, Outgoing, GuardBlocks, GuardPredicates,
221 DeletionCandidates);
222 else
223 calcPredicateUsingInteger(Branches, Outgoing, GuardBlocks, GuardPredicates);
224
225 setupBranchForGuard(GuardBlocks, Outgoing, GuardPredicates);
226}
227
228// After creating a control flow hub, the operands of PHINodes in an outgoing
229// block Out no longer match the predecessors of that block. Predecessors of Out
230// that are incoming blocks to the hub are now replaced by just one edge from
231// the hub. To match this new control flow, the corresponding values from each
232// PHINode must now be moved a new PHINode in the first guard block of the hub.
233//
234// This operation cannot be performed with SSAUpdater, because it involves one
235// new use: If the block Out is in the list of Incoming blocks, then the newly
236// created PHI in the Hub will use itself along that edge from Out to Hub.
237static void reconnectPhis(BasicBlock *Out, BasicBlock *GuardBlock,
238 ArrayRef<EdgeDescriptor> Incoming,
239 BasicBlock *FirstGuardBlock) {
240 auto I = Out->begin();
241 while (I != Out->end() && isa<PHINode>(Val: I)) {
242 auto *Phi = cast<PHINode>(Val&: I);
243 auto *NewPhi =
244 PHINode::Create(Ty: Phi->getType(), NumReservedValues: Incoming.size(),
245 NameStr: Phi->getName() + ".moved", InsertBefore: FirstGuardBlock->begin());
246 bool AllUndef = true;
247 for (auto [BB, Succ0, Succ1] : Incoming) {
248 Value *V = PoisonValue::get(T: Phi->getType());
249 if (Phi->getBasicBlockIndex(BB) != -1) {
250 V = Phi->removeIncomingValue(BB, DeletePHIIfEmpty: false);
251 // When both successors are the same (Succ0 == Succ1), there are two
252 // edges from BB to Out, so we need to remove the second PHI entry too.
253 if (Succ0 && Succ1 && Succ0 == Succ1 &&
254 Phi->getBasicBlockIndex(BB) != -1)
255 Phi->removeIncomingValue(BB, DeletePHIIfEmpty: false);
256 if (BB == Out) {
257 V = NewPhi;
258 }
259 AllUndef &= isa<UndefValue>(Val: V);
260 }
261
262 NewPhi->addIncoming(V, BB);
263 }
264 assert(NewPhi->getNumIncomingValues() == Incoming.size());
265 Value *NewV = NewPhi;
266 if (AllUndef) {
267 NewPhi->eraseFromParent();
268 NewV = PoisonValue::get(T: Phi->getType());
269 }
270 if (Phi->getNumOperands() == 0) {
271 Phi->replaceAllUsesWith(V: NewV);
272 I = Phi->eraseFromParent();
273 continue;
274 }
275 Phi->addIncoming(V: NewV, BB: GuardBlock);
276 ++I;
277 }
278}
279
280std::pair<BasicBlock *, bool> ControlFlowHub::finalize(
281 DomTreeUpdater *DTU, SmallVectorImpl<BasicBlock *> &GuardBlocks,
282 const StringRef Prefix, std::optional<unsigned> MaxControlFlowBooleans) {
283#ifndef NDEBUG
284 SmallPtrSet<BasicBlock *, 8> Incoming;
285#endif
286 SetVector<BasicBlock *> Outgoing;
287
288 for (auto [BB, Succ0, Succ1] : Branches) {
289#ifndef NDEBUG
290 assert(
291 (Incoming.insert(BB).second || isa<CallBrInst>(BB->getTerminator())) &&
292 "Duplicate entry for incoming block.");
293#endif
294 if (Succ0)
295 Outgoing.insert(X: Succ0);
296 if (Succ1)
297 Outgoing.insert(X: Succ1);
298 }
299
300 assert(Outgoing.size() && "No outgoing edges");
301
302 if (Outgoing.size() < 2)
303 return {Outgoing.front(), false};
304
305 SmallVector<DominatorTree::UpdateType, 16> Updates;
306 if (DTU) {
307 for (auto [BB, Succ0, Succ1] : Branches) {
308 if (Succ0)
309 Updates.push_back(Elt: {DominatorTree::Delete, BB, Succ0});
310 // Only add Succ1 if it's different from Succ0 to avoid duplicate updates
311 if (Succ1 && Succ1 != Succ0)
312 Updates.push_back(Elt: {DominatorTree::Delete, BB, Succ1});
313 }
314 }
315
316 SmallVector<WeakVH, 8> DeletionCandidates;
317 convertToGuardPredicates(Branches, Outgoing: Outgoing.getArrayRef(), GuardBlocks,
318 DeletionCandidates, Prefix, MaxControlFlowBooleans);
319 BasicBlock *FirstGuardBlock = GuardBlocks.front();
320
321 // Update the PHINodes in each outgoing block to match the new control flow.
322 for (int I = 0, E = GuardBlocks.size(); I != E; ++I)
323 reconnectPhis(Out: Outgoing[I], GuardBlock: GuardBlocks[I], Incoming: Branches, FirstGuardBlock);
324 // Process the Nth (last) outgoing block with the (N-1)th (last) guard block.
325 reconnectPhis(Out: Outgoing.back(), GuardBlock: GuardBlocks.back(), Incoming: Branches, FirstGuardBlock);
326
327 if (DTU) {
328 int NumGuards = GuardBlocks.size();
329
330 for (auto [BB, Succ0, Succ1] : Branches)
331 Updates.push_back(Elt: {DominatorTree::Insert, BB, FirstGuardBlock});
332
333 for (int I = 0; I != NumGuards - 1; ++I) {
334 Updates.push_back(Elt: {DominatorTree::Insert, GuardBlocks[I], Outgoing[I]});
335 Updates.push_back(
336 Elt: {DominatorTree::Insert, GuardBlocks[I], GuardBlocks[I + 1]});
337 }
338 // The second successor of the last guard block is an outgoing block instead
339 // of having a "next" guard block.
340 Updates.push_back(Elt: {DominatorTree::Insert, GuardBlocks[NumGuards - 1],
341 Outgoing[NumGuards - 1]});
342 Updates.push_back(Elt: {DominatorTree::Insert, GuardBlocks[NumGuards - 1],
343 Outgoing[NumGuards]});
344 DTU->applyUpdates(Updates);
345 }
346
347 for (auto I : DeletionCandidates) {
348 if (I->use_empty())
349 if (auto *Inst = dyn_cast_or_null<Instruction>(Val&: I))
350 Inst->eraseFromParent();
351 }
352
353 return {FirstGuardBlock, true};
354}
355