Skip to content

Commit 142a016

Browse files
committed
implement feedback part 2
1 parent be509bc commit 142a016

File tree

7 files changed

+55
-45
lines changed

7 files changed

+55
-45
lines changed

llvm/include/llvm/Transforms/Utils/BasicBlockUtils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "llvm/IR/BasicBlock.h"
2222
#include "llvm/IR/Dominators.h"
2323
#include "llvm/Support/Compiler.h"
24+
#include "llvm/Support/Printable.h"
2425
#include <cassert>
2526

2627
namespace llvm {
@@ -618,6 +619,8 @@ LLVM_ABI bool hasOnlySimpleTerminator(const Function &F);
618619
// (br/brcond/unreachable/ret) or callbr.
619620
LLVM_ABI bool hasOnlySimpleTerminatorOrCallBr(const Function &F);
620621

622+
LLVM_ABI Printable printBBPtr(const BasicBlock *BB);
623+
621624
} // end namespace llvm
622625

623626
#endif // LLVM_TRANSFORMS_UTILS_BASICBLOCKUTILS_H

llvm/include/llvm/Transforms/Utils/ControlFlowUtils.h

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -123,30 +123,31 @@ struct ControlFlowHub {
123123

124124
SmallVector<BranchDescriptor> Branches;
125125

126-
/**
127-
* \brief Create a new intermediate target block for a callbr edge.
128-
*
129-
* This function creates a new basic block (the "target block") that sits
130-
* between a callbr instruction and one of its successors. The callbr's
131-
* successor is rewired to this new block, and the new block unconditionally
132-
* branches to the original successor. This is useful for normalizing control
133-
* flow, e.g., when transforming irreducible loops.
134-
*
135-
* \param CallBr The callbr instruction whose edge is to be split.
136-
* \param Succ The original successor basic block to be reached.
137-
* \param SuccIdx The index of the successor in the callbr instruction.
138-
* \param AttachToCallBr If true, the new block is associated with the
139-
* callbr's parent for loop/cycle info. If false, the new block is associated
140-
* with the callbr's successor for loop/cycle info. \param CI Optional
141-
* CycleInfo for updating cycle membership. \param DTU Optional
142-
* DomTreeUpdater for updating the dominator tree. \param LI Optional LoopInfo
143-
* for updating loop membership.
144-
*
145-
* \returns The newly created intermediate target block.
146-
*
147-
* \note This function updates PHI nodes, dominator tree, loop info, and cycle
148-
* info as needed.
149-
*/
126+
/// \brief Create a new intermediate target block for a callbr edge.
127+
///
128+
/// This function creates a new basic block (the "target block") that sits
129+
/// between a callbr instruction and one of its successors. The callbr's
130+
/// successor is rewired to this new block, and the new block unconditionally
131+
/// branches to the original successor. This is useful for normalizing control
132+
/// flow, e.g., when transforming irreducible loops.
133+
///
134+
/// \param CallBr The callbr instruction whose edge is to be split.
135+
/// \param Succ The original successor basic block to be reached.
136+
/// \param SuccIdx The index of the successor in the callbr
137+
/// instruction.
138+
/// \param AttachToCallBr If true, the new block is associated with the
139+
/// callbr's parent for loop/cycle info.
140+
/// If false, the new block is associated with the
141+
/// callbr's successor for loop/cycle info.
142+
/// \param CI Optional CycleInfo for updating cycle membership.
143+
/// \param DTU Optional DomTreeUpdater for updating the dominator
144+
/// tree.
145+
/// \param LI Optional LoopInfo for updating loop membership.
146+
///
147+
/// \returns The newly created intermediate target block.
148+
///
149+
/// \note This function updates PHI nodes, dominator tree, loop info, and
150+
/// cycle info as needed.
150151
static BasicBlock *
151152
createCallBrTarget(CallBrInst *CallBr, BasicBlock *Succ, unsigned SuccIdx,
152153
bool AttachToCallBr = true, CycleInfo *CI = nullptr,

llvm/lib/Transforms/Utils/BasicBlockUtils.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1783,4 +1783,11 @@ bool llvm::hasOnlySimpleTerminator(const Function &F) {
17831783
bool llvm::hasOnlySimpleTerminatorOrCallBr(const Function &F) {
17841784
return hasOnlyGivenTerminators<ReturnInst, UnreachableInst, BranchInst,
17851785
CallBrInst>(F);
1786-
}
1786+
}
1787+
1788+
Printable llvm::printBBPtr(const BasicBlock *BB) {
1789+
return Printable([BB](raw_ostream &OS) {
1790+
if (BB)
1791+
return BB->printAsOperand(OS);
1792+
});
1793+
}

llvm/lib/Transforms/Utils/ControlFlowUtils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,4 +396,4 @@ BasicBlock *ControlFlowHub::createCallBrTarget(
396396
{DominatorTree::Insert, CallBrTarget, Succ}});
397397
}
398398
return CallBrTarget;
399-
}
399+
}

llvm/lib/Transforms/Utils/FixIrreducible.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -299,9 +299,9 @@ static bool fixIrreducible(Cycle &C, CycleInfo &CI, DominatorTree &DT,
299299
assert(Succ0 || Succ1);
300300
CHub.addBranch(P, Succ0, Succ1);
301301

302-
LLVM_DEBUG(dbgs() << "Added internal branch: " << P->getName() << " -> "
303-
<< (Succ0 ? Succ0->getName() : "") << " "
304-
<< (Succ1 ? Succ1->getName() : "") << "\n");
302+
LLVM_DEBUG(dbgs() << "Added internal branch: " << printBBPtr(P) << " -> "
303+
<< printBBPtr(Succ0) << (Succ0 && Succ1 ? " " : "")
304+
<< printBBPtr(Succ1) << "\n");
305305
} else if (CallBrInst *CallBr = dyn_cast<CallBrInst>(P->getTerminator())) {
306306
for (unsigned I = 0; I < CallBr->getNumSuccessors(); ++I) {
307307
BasicBlock *Succ = CallBr->getSuccessor(I);
@@ -310,8 +310,8 @@ static bool fixIrreducible(Cycle &C, CycleInfo &CI, DominatorTree &DT,
310310
BasicBlock *NewSucc = ControlFlowHub::createCallBrTarget(
311311
CallBr, Succ, I, false, &CI, &DTU, LI);
312312
CHub.addBranch(NewSucc, Succ);
313-
LLVM_DEBUG(dbgs() << "Added internal branch: " << NewSucc->getName()
314-
<< " -> " << Succ->getName() << "\n");
313+
LLVM_DEBUG(dbgs() << "Added internal branch: " << printBBPtr(NewSucc)
314+
<< " -> " << printBBPtr(Succ) << "\n");
315315
}
316316
} else {
317317
llvm_unreachable("unsupported block terminator");
@@ -336,9 +336,9 @@ static bool fixIrreducible(Cycle &C, CycleInfo &CI, DominatorTree &DT,
336336
Succ1 = Succ1 && C.contains(Succ1) ? Succ1 : nullptr;
337337
CHub.addBranch(P, Succ0, Succ1);
338338

339-
LLVM_DEBUG(dbgs() << "Added external branch: " << P->getName() << " -> "
340-
<< (Succ0 ? Succ0->getName() : "") << " "
341-
<< (Succ1 ? Succ1->getName() : "") << "\n");
339+
LLVM_DEBUG(dbgs() << "Added external branch: " << printBBPtr(P) << " -> "
340+
<< printBBPtr(Succ0) << (Succ0 && Succ1 ? " " : "")
341+
<< printBBPtr(Succ1) << "\n");
342342
} else if (CallBrInst *CallBr = dyn_cast<CallBrInst>(P->getTerminator())) {
343343
for (unsigned I = 0; I < CallBr->getNumSuccessors(); ++I) {
344344
BasicBlock *Succ = CallBr->getSuccessor(I);
@@ -347,8 +347,8 @@ static bool fixIrreducible(Cycle &C, CycleInfo &CI, DominatorTree &DT,
347347
BasicBlock *NewSucc = ControlFlowHub::createCallBrTarget(
348348
CallBr, Succ, I, true, &CI, &DTU, LI);
349349
CHub.addBranch(NewSucc, Succ);
350-
LLVM_DEBUG(dbgs() << "Added external branch: " << NewSucc->getName()
351-
<< " -> " << Succ->getName() << "\n");
350+
LLVM_DEBUG(dbgs() << "Added external branch: " << printBBPtr(NewSucc)
351+
<< " -> " << printBBPtr(Succ) << "\n");
352352
}
353353
} else {
354354
llvm_unreachable("unsupported block terminator");

llvm/lib/Transforms/Utils/UnifyLoopExits.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,9 @@ static bool unifyLoopExits(DominatorTree &DT, LoopInfo &LI, Loop *L) {
170170
Succ1 = L->contains(Succ1) ? nullptr : Succ1;
171171
CHub.addBranch(BB, Succ0, Succ1);
172172

173-
LLVM_DEBUG(dbgs() << "Added exiting branch: " << BB->getName() << " -> {"
174-
<< (Succ0 ? Succ0->getName() : "<none>") << ", "
175-
<< (Succ1 ? Succ1->getName() : "<none>") << "}\n");
173+
LLVM_DEBUG(dbgs() << "Added extiting branch: " << printBBPtr(BB) << " -> "
174+
<< printBBPtr(Succ0) << (Succ0 && Succ1 ? " " : "")
175+
<< printBBPtr(Succ1) << "\n");
176176
} else if (CallBrInst *CallBr = dyn_cast<CallBrInst>(BB->getTerminator())) {
177177
for (unsigned J = 0; J < CallBr->getNumSuccessors(); ++J) {
178178
BasicBlock *Succ = CallBr->getSuccessor(J);
@@ -187,8 +187,8 @@ static bool unifyLoopExits(DominatorTree &DT, LoopInfo &LI, Loop *L) {
187187
// themselves.
188188
ExitingBlocks[I] = NewSucc;
189189
CHub.addBranch(NewSucc, Succ);
190-
LLVM_DEBUG(dbgs() << "Added exiting branch: " << NewSucc->getName()
191-
<< " -> " << Succ->getName() << "\n");
190+
LLVM_DEBUG(dbgs() << "Added exiting branch: " << printBBPtr(NewSucc)
191+
<< " -> " << printBBPtr(Succ) << "\n");
192192
// Also add the new target block to the list of exiting blocks that
193193
// should later be added to the parent loops.
194194
CallBrTargetBlocks.push_back(NewSucc);

llvm/test/Transforms/FixIrreducible/callbr.ll

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2-
; RUN: opt < %s -fix-irreducible --verify-loop-info -S | FileCheck %s
32
; RUN: opt < %s -passes='fix-irreducible,verify<loops>' -S | FileCheck %s
43
; RUN: opt < %s -passes='verify<loops>,fix-irreducible,verify<loops>' -S | FileCheck %s
54
; RUN: opt < %s -passes='print<loops>' -disable-output 2>&1 | FileCheck %s --check-prefix LOOPS-BEFORE
@@ -9,9 +8,9 @@
98
; LOOPS-AFTER: Loop info for function 'callbr_entry':{{$}}
109
; LOOPS-AFTER-NEXT: Loop at depth 1 containing: %irr.guard<header>,%indirect,%fallthrough<latch><exiting>{{$}}
1110

12-
; LOOPS-BEFORE-NEXT: Loop info for function 'callbr_entry_targets_with_phi_nodes':
13-
; LOOPS-AFTER-NEXT: Loop info for function 'callbr_entry_targets_with_phi_nodes':
14-
; LOOPS-AFTER-NEXT: Loop at depth 1 containing: %irr.guard<header>,%block1<exiting>,%block<latch>
11+
; LOOPS-BEFORE-NEXT: Loop info for function 'callbr_entry_targets_with_phi_nodes':{{$}}
12+
; LOOPS-AFTER-NEXT: Loop info for function 'callbr_entry_targets_with_phi_nodes':{{$}}
13+
; LOOPS-AFTER-NEXT: Loop at depth 1 containing: %irr.guard<header>,%block1<exiting>,%block<latch>{{$}}
1514

1615
; LOOPS-BEFORE-NEXT: Loop info for function 'callbr_entry_multiple_indirect_targets':{{$}}
1716
; LOOPS-AFTER-NEXT: Loop info for function 'callbr_entry_multiple_indirect_targets':{{$}}

0 commit comments

Comments
 (0)