1 //===- IfConversion.cpp - Machine code if conversion pass -----------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the machine instruction level if-conversion pass, which
10 // tries to convert conditional branches into predicated instructions.
12 //===----------------------------------------------------------------------===//
14 #include "BranchFolding.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/ScopeExit.h"
17 #include "llvm/ADT/SmallSet.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/SparseSet.h"
20 #include "llvm/ADT/Statistic.h"
21 #include "llvm/ADT/iterator_range.h"
22 #include "llvm/Analysis/ProfileSummaryInfo.h"
23 #include "llvm/CodeGen/LivePhysRegs.h"
24 #include "llvm/CodeGen/MBFIWrapper.h"
25 #include "llvm/CodeGen/MachineBasicBlock.h"
26 #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
27 #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
28 #include "llvm/CodeGen/MachineFunction.h"
29 #include "llvm/CodeGen/MachineFunctionPass.h"
30 #include "llvm/CodeGen/MachineInstr.h"
31 #include "llvm/CodeGen/MachineInstrBuilder.h"
32 #include "llvm/CodeGen/MachineOperand.h"
33 #include "llvm/CodeGen/MachineRegisterInfo.h"
34 #include "llvm/CodeGen/TargetInstrInfo.h"
35 #include "llvm/CodeGen/TargetLowering.h"
36 #include "llvm/CodeGen/TargetRegisterInfo.h"
37 #include "llvm/CodeGen/TargetSchedule.h"
38 #include "llvm/CodeGen/TargetSubtargetInfo.h"
39 #include "llvm/IR/DebugLoc.h"
40 #include "llvm/InitializePasses.h"
41 #include "llvm/MC/MCRegisterInfo.h"
42 #include "llvm/Pass.h"
43 #include "llvm/Support/BranchProbability.h"
44 #include "llvm/Support/CommandLine.h"
45 #include "llvm/Support/Debug.h"
46 #include "llvm/Support/ErrorHandling.h"
47 #include "llvm/Support/raw_ostream.h"
58 #define DEBUG_TYPE "if-converter"
60 // Hidden options for help debugging.
61 static cl::opt
<int> IfCvtFnStart("ifcvt-fn-start", cl::init(-1), cl::Hidden
);
62 static cl::opt
<int> IfCvtFnStop("ifcvt-fn-stop", cl::init(-1), cl::Hidden
);
63 static cl::opt
<int> IfCvtLimit("ifcvt-limit", cl::init(-1), cl::Hidden
);
64 static cl::opt
<bool> DisableSimple("disable-ifcvt-simple",
65 cl::init(false), cl::Hidden
);
66 static cl::opt
<bool> DisableSimpleF("disable-ifcvt-simple-false",
67 cl::init(false), cl::Hidden
);
68 static cl::opt
<bool> DisableTriangle("disable-ifcvt-triangle",
69 cl::init(false), cl::Hidden
);
70 static cl::opt
<bool> DisableTriangleR("disable-ifcvt-triangle-rev",
71 cl::init(false), cl::Hidden
);
72 static cl::opt
<bool> DisableTriangleF("disable-ifcvt-triangle-false",
73 cl::init(false), cl::Hidden
);
74 static cl::opt
<bool> DisableDiamond("disable-ifcvt-diamond",
75 cl::init(false), cl::Hidden
);
76 static cl::opt
<bool> DisableForkedDiamond("disable-ifcvt-forked-diamond",
77 cl::init(false), cl::Hidden
);
78 static cl::opt
<bool> IfCvtBranchFold("ifcvt-branch-fold",
79 cl::init(true), cl::Hidden
);
81 STATISTIC(NumSimple
, "Number of simple if-conversions performed");
82 STATISTIC(NumSimpleFalse
, "Number of simple (F) if-conversions performed");
83 STATISTIC(NumTriangle
, "Number of triangle if-conversions performed");
84 STATISTIC(NumTriangleRev
, "Number of triangle (R) if-conversions performed");
85 STATISTIC(NumTriangleFalse
,"Number of triangle (F) if-conversions performed");
86 STATISTIC(NumTriangleFRev
, "Number of triangle (F/R) if-conversions performed");
87 STATISTIC(NumDiamonds
, "Number of diamond if-conversions performed");
88 STATISTIC(NumForkedDiamonds
, "Number of forked-diamond if-conversions performed");
89 STATISTIC(NumIfConvBBs
, "Number of if-converted blocks");
90 STATISTIC(NumDupBBs
, "Number of duplicated blocks");
91 STATISTIC(NumUnpred
, "Number of true blocks of diamonds unpredicated");
95 class IfConverter
: public MachineFunctionPass
{
97 ICNotClassfied
, // BB data valid, but not classified.
98 ICSimpleFalse
, // Same as ICSimple, but on the false path.
99 ICSimple
, // BB is entry of an one split, no rejoin sub-CFG.
100 ICTriangleFRev
, // Same as ICTriangleFalse, but false path rev condition.
101 ICTriangleRev
, // Same as ICTriangle, but true path rev condition.
102 ICTriangleFalse
, // Same as ICTriangle, but on the false path.
103 ICTriangle
, // BB is entry of a triangle sub-CFG.
104 ICDiamond
, // BB is entry of a diamond sub-CFG.
105 ICForkedDiamond
// BB is entry of an almost diamond sub-CFG, with a
106 // common tail that can be shared.
109 /// One per MachineBasicBlock, this is used to cache the result
110 /// if-conversion feasibility analysis. This includes results from
111 /// TargetInstrInfo::analyzeBranch() (i.e. TBB, FBB, and Cond), and its
112 /// classification, and common tail block of its successors (if it's a
113 /// diamond shape), its size, whether it's predicable, and whether any
114 /// instruction can clobber the 'would-be' predicate.
116 /// IsDone - True if BB is not to be considered for ifcvt.
117 /// IsBeingAnalyzed - True if BB is currently being analyzed.
118 /// IsAnalyzed - True if BB has been analyzed (info is still valid).
119 /// IsEnqueued - True if BB has been enqueued to be ifcvt'ed.
120 /// IsBrAnalyzable - True if analyzeBranch() returns false.
121 /// HasFallThrough - True if BB may fallthrough to the following BB.
122 /// IsUnpredicable - True if BB is known to be unpredicable.
123 /// ClobbersPred - True if BB could modify predicates (e.g. has
125 /// NonPredSize - Number of non-predicated instructions.
126 /// ExtraCost - Extra cost for multi-cycle instructions.
127 /// ExtraCost2 - Some instructions are slower when predicated
128 /// BB - Corresponding MachineBasicBlock.
129 /// TrueBB / FalseBB- See analyzeBranch().
130 /// BrCond - Conditions for end of block conditional branches.
131 /// Predicate - Predicate used in the BB.
134 bool IsBeingAnalyzed
: 1;
137 bool IsBrAnalyzable
: 1;
138 bool IsBrReversible
: 1;
139 bool HasFallThrough
: 1;
140 bool IsUnpredicable
: 1;
141 bool CannotBeCopied
: 1;
142 bool ClobbersPred
: 1;
143 unsigned NonPredSize
= 0;
144 unsigned ExtraCost
= 0;
145 unsigned ExtraCost2
= 0;
146 MachineBasicBlock
*BB
= nullptr;
147 MachineBasicBlock
*TrueBB
= nullptr;
148 MachineBasicBlock
*FalseBB
= nullptr;
149 SmallVector
<MachineOperand
, 4> BrCond
;
150 SmallVector
<MachineOperand
, 4> Predicate
;
152 BBInfo() : IsDone(false), IsBeingAnalyzed(false),
153 IsAnalyzed(false), IsEnqueued(false), IsBrAnalyzable(false),
154 IsBrReversible(false), HasFallThrough(false),
155 IsUnpredicable(false), CannotBeCopied(false),
156 ClobbersPred(false) {}
159 /// Record information about pending if-conversions to attempt:
160 /// BBI - Corresponding BBInfo.
161 /// Kind - Type of block. See IfcvtKind.
162 /// NeedSubsumption - True if the to-be-predicated BB has already been
164 /// NumDups - Number of instructions that would be duplicated due
165 /// to this if-conversion. (For diamonds, the number of
166 /// identical instructions at the beginnings of both
168 /// NumDups2 - For diamonds, the number of identical instructions
169 /// at the ends of both paths.
175 bool NeedSubsumption
: 1;
176 bool TClobbersPred
: 1;
177 bool FClobbersPred
: 1;
179 IfcvtToken(BBInfo
&b
, IfcvtKind k
, bool s
, unsigned d
, unsigned d2
= 0,
180 bool tc
= false, bool fc
= false)
181 : BBI(b
), Kind(k
), NumDups(d
), NumDups2(d2
), NeedSubsumption(s
),
182 TClobbersPred(tc
), FClobbersPred(fc
) {}
185 /// Results of if-conversion feasibility analysis indexed by basic block
187 std::vector
<BBInfo
> BBAnalysis
;
188 TargetSchedModel SchedModel
;
190 const TargetLoweringBase
*TLI
= nullptr;
191 const TargetInstrInfo
*TII
= nullptr;
192 const TargetRegisterInfo
*TRI
= nullptr;
193 const MachineBranchProbabilityInfo
*MBPI
= nullptr;
194 MachineRegisterInfo
*MRI
= nullptr;
198 bool PreRegAlloc
= true;
199 bool MadeChange
= false;
201 std::function
<bool(const MachineFunction
&)> PredicateFtor
;
206 IfConverter(std::function
<bool(const MachineFunction
&)> Ftor
= nullptr)
207 : MachineFunctionPass(ID
), PredicateFtor(std::move(Ftor
)) {
208 initializeIfConverterPass(*PassRegistry::getPassRegistry());
211 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
212 AU
.addRequired
<MachineBlockFrequencyInfoWrapperPass
>();
213 AU
.addRequired
<MachineBranchProbabilityInfoWrapperPass
>();
214 AU
.addRequired
<ProfileSummaryInfoWrapperPass
>();
215 MachineFunctionPass::getAnalysisUsage(AU
);
218 bool runOnMachineFunction(MachineFunction
&MF
) override
;
220 MachineFunctionProperties
getRequiredProperties() const override
{
221 return MachineFunctionProperties().set(
222 MachineFunctionProperties::Property::NoVRegs
);
226 bool reverseBranchCondition(BBInfo
&BBI
) const;
227 bool ValidSimple(BBInfo
&TrueBBI
, unsigned &Dups
,
228 BranchProbability Prediction
) const;
229 bool ValidTriangle(BBInfo
&TrueBBI
, BBInfo
&FalseBBI
,
230 bool FalseBranch
, unsigned &Dups
,
231 BranchProbability Prediction
) const;
232 bool CountDuplicatedInstructions(
233 MachineBasicBlock::iterator
&TIB
, MachineBasicBlock::iterator
&FIB
,
234 MachineBasicBlock::iterator
&TIE
, MachineBasicBlock::iterator
&FIE
,
235 unsigned &Dups1
, unsigned &Dups2
,
236 MachineBasicBlock
&TBB
, MachineBasicBlock
&FBB
,
237 bool SkipUnconditionalBranches
) const;
238 bool ValidDiamond(BBInfo
&TrueBBI
, BBInfo
&FalseBBI
,
239 unsigned &Dups1
, unsigned &Dups2
,
240 BBInfo
&TrueBBICalc
, BBInfo
&FalseBBICalc
) const;
241 bool ValidForkedDiamond(BBInfo
&TrueBBI
, BBInfo
&FalseBBI
,
242 unsigned &Dups1
, unsigned &Dups2
,
243 BBInfo
&TrueBBICalc
, BBInfo
&FalseBBICalc
) const;
244 void AnalyzeBranches(BBInfo
&BBI
);
245 void ScanInstructions(BBInfo
&BBI
,
246 MachineBasicBlock::iterator
&Begin
,
247 MachineBasicBlock::iterator
&End
,
248 bool BranchUnpredicable
= false) const;
249 bool RescanInstructions(
250 MachineBasicBlock::iterator
&TIB
, MachineBasicBlock::iterator
&FIB
,
251 MachineBasicBlock::iterator
&TIE
, MachineBasicBlock::iterator
&FIE
,
252 BBInfo
&TrueBBI
, BBInfo
&FalseBBI
) const;
253 void AnalyzeBlock(MachineBasicBlock
&MBB
,
254 std::vector
<std::unique_ptr
<IfcvtToken
>> &Tokens
);
255 bool FeasibilityAnalysis(BBInfo
&BBI
, SmallVectorImpl
<MachineOperand
> &Pred
,
256 bool isTriangle
= false, bool RevBranch
= false,
257 bool hasCommonTail
= false);
258 void AnalyzeBlocks(MachineFunction
&MF
,
259 std::vector
<std::unique_ptr
<IfcvtToken
>> &Tokens
);
260 void InvalidatePreds(MachineBasicBlock
&MBB
);
261 bool IfConvertSimple(BBInfo
&BBI
, IfcvtKind Kind
);
262 bool IfConvertTriangle(BBInfo
&BBI
, IfcvtKind Kind
);
263 bool IfConvertDiamondCommon(BBInfo
&BBI
, BBInfo
&TrueBBI
, BBInfo
&FalseBBI
,
264 unsigned NumDups1
, unsigned NumDups2
,
265 bool TClobbersPred
, bool FClobbersPred
,
266 bool RemoveBranch
, bool MergeAddEdges
);
267 bool IfConvertDiamond(BBInfo
&BBI
, IfcvtKind Kind
,
268 unsigned NumDups1
, unsigned NumDups2
,
269 bool TClobbers
, bool FClobbers
);
270 bool IfConvertForkedDiamond(BBInfo
&BBI
, IfcvtKind Kind
,
271 unsigned NumDups1
, unsigned NumDups2
,
272 bool TClobbers
, bool FClobbers
);
273 void PredicateBlock(BBInfo
&BBI
,
274 MachineBasicBlock::iterator E
,
275 SmallVectorImpl
<MachineOperand
> &Cond
,
276 SmallSet
<MCPhysReg
, 4> *LaterRedefs
= nullptr);
277 void CopyAndPredicateBlock(BBInfo
&ToBBI
, BBInfo
&FromBBI
,
278 SmallVectorImpl
<MachineOperand
> &Cond
,
279 bool IgnoreBr
= false);
280 void MergeBlocks(BBInfo
&ToBBI
, BBInfo
&FromBBI
, bool AddEdges
= true);
282 bool MeetIfcvtSizeLimit(MachineBasicBlock
&BB
,
283 unsigned Cycle
, unsigned Extra
,
284 BranchProbability Prediction
) const {
285 return Cycle
> 0 && TII
->isProfitableToIfCvt(BB
, Cycle
, Extra
,
289 bool MeetIfcvtSizeLimit(BBInfo
&TBBInfo
, BBInfo
&FBBInfo
,
290 MachineBasicBlock
&CommBB
, unsigned Dups
,
291 BranchProbability Prediction
, bool Forked
) const {
292 const MachineFunction
&MF
= *TBBInfo
.BB
->getParent();
293 if (MF
.getFunction().hasMinSize()) {
294 MachineBasicBlock::iterator TIB
= TBBInfo
.BB
->begin();
295 MachineBasicBlock::iterator FIB
= FBBInfo
.BB
->begin();
296 MachineBasicBlock::iterator TIE
= TBBInfo
.BB
->end();
297 MachineBasicBlock::iterator FIE
= FBBInfo
.BB
->end();
299 unsigned Dups1
= 0, Dups2
= 0;
300 if (!CountDuplicatedInstructions(TIB
, FIB
, TIE
, FIE
, Dups1
, Dups2
,
301 *TBBInfo
.BB
, *FBBInfo
.BB
,
302 /*SkipUnconditionalBranches*/ true))
303 llvm_unreachable("should already have been checked by ValidDiamond");
305 unsigned BranchBytes
= 0;
306 unsigned CommonBytes
= 0;
308 // Count common instructions at the start of the true and false blocks.
309 for (auto &I
: make_range(TBBInfo
.BB
->begin(), TIB
)) {
310 LLVM_DEBUG(dbgs() << "Common inst: " << I
);
311 CommonBytes
+= TII
->getInstSizeInBytes(I
);
313 for (auto &I
: make_range(FBBInfo
.BB
->begin(), FIB
)) {
314 LLVM_DEBUG(dbgs() << "Common inst: " << I
);
315 CommonBytes
+= TII
->getInstSizeInBytes(I
);
318 // Count instructions at the end of the true and false blocks, after
319 // the ones we plan to predicate. Analyzable branches will be removed
320 // (unless this is a forked diamond), and all other instructions are
321 // common between the two blocks.
322 for (auto &I
: make_range(TIE
, TBBInfo
.BB
->end())) {
323 if (I
.isBranch() && TBBInfo
.IsBrAnalyzable
&& !Forked
) {
324 LLVM_DEBUG(dbgs() << "Saving branch: " << I
);
325 BranchBytes
+= TII
->predictBranchSizeForIfCvt(I
);
327 LLVM_DEBUG(dbgs() << "Common inst: " << I
);
328 CommonBytes
+= TII
->getInstSizeInBytes(I
);
331 for (auto &I
: make_range(FIE
, FBBInfo
.BB
->end())) {
332 if (I
.isBranch() && FBBInfo
.IsBrAnalyzable
&& !Forked
) {
333 LLVM_DEBUG(dbgs() << "Saving branch: " << I
);
334 BranchBytes
+= TII
->predictBranchSizeForIfCvt(I
);
336 LLVM_DEBUG(dbgs() << "Common inst: " << I
);
337 CommonBytes
+= TII
->getInstSizeInBytes(I
);
340 for (auto &I
: CommBB
.terminators()) {
342 LLVM_DEBUG(dbgs() << "Saving branch: " << I
);
343 BranchBytes
+= TII
->predictBranchSizeForIfCvt(I
);
347 // The common instructions in one branch will be eliminated, halving
351 // Count the instructions which we need to predicate.
352 unsigned NumPredicatedInstructions
= 0;
353 for (auto &I
: make_range(TIB
, TIE
)) {
354 if (!I
.isDebugInstr()) {
355 LLVM_DEBUG(dbgs() << "Predicating: " << I
);
356 NumPredicatedInstructions
++;
359 for (auto &I
: make_range(FIB
, FIE
)) {
360 if (!I
.isDebugInstr()) {
361 LLVM_DEBUG(dbgs() << "Predicating: " << I
);
362 NumPredicatedInstructions
++;
366 // Even though we're optimising for size at the expense of performance,
367 // avoid creating really long predicated blocks.
368 if (NumPredicatedInstructions
> 15)
371 // Some targets (e.g. Thumb2) need to insert extra instructions to
372 // start predicated blocks.
373 unsigned ExtraPredicateBytes
= TII
->extraSizeToPredicateInstructions(
374 MF
, NumPredicatedInstructions
);
376 LLVM_DEBUG(dbgs() << "MeetIfcvtSizeLimit(BranchBytes=" << BranchBytes
377 << ", CommonBytes=" << CommonBytes
378 << ", NumPredicatedInstructions="
379 << NumPredicatedInstructions
380 << ", ExtraPredicateBytes=" << ExtraPredicateBytes
382 return (BranchBytes
+ CommonBytes
) > ExtraPredicateBytes
;
384 unsigned TCycle
= TBBInfo
.NonPredSize
+ TBBInfo
.ExtraCost
- Dups
;
385 unsigned FCycle
= FBBInfo
.NonPredSize
+ FBBInfo
.ExtraCost
- Dups
;
386 bool Res
= TCycle
> 0 && FCycle
> 0 &&
387 TII
->isProfitableToIfCvt(
388 *TBBInfo
.BB
, TCycle
, TBBInfo
.ExtraCost2
, *FBBInfo
.BB
,
389 FCycle
, FBBInfo
.ExtraCost2
, Prediction
);
390 LLVM_DEBUG(dbgs() << "MeetIfcvtSizeLimit(TCycle=" << TCycle
391 << ", FCycle=" << FCycle
392 << ", TExtra=" << TBBInfo
.ExtraCost2
<< ", FExtra="
393 << FBBInfo
.ExtraCost2
<< ") = " << Res
<< "\n");
398 /// Returns true if Block ends without a terminator.
399 bool blockAlwaysFallThrough(BBInfo
&BBI
) const {
400 return BBI
.IsBrAnalyzable
&& BBI
.TrueBB
== nullptr;
403 /// Used to sort if-conversion candidates.
404 static bool IfcvtTokenCmp(const std::unique_ptr
<IfcvtToken
> &C1
,
405 const std::unique_ptr
<IfcvtToken
> &C2
) {
406 int Incr1
= (C1
->Kind
== ICDiamond
)
407 ? -(int)(C1
->NumDups
+ C1
->NumDups2
) : (int)C1
->NumDups
;
408 int Incr2
= (C2
->Kind
== ICDiamond
)
409 ? -(int)(C2
->NumDups
+ C2
->NumDups2
) : (int)C2
->NumDups
;
412 else if (Incr1
== Incr2
) {
413 // Favors subsumption.
414 if (!C1
->NeedSubsumption
&& C2
->NeedSubsumption
)
416 else if (C1
->NeedSubsumption
== C2
->NeedSubsumption
) {
417 // Favors diamond over triangle, etc.
418 if ((unsigned)C1
->Kind
< (unsigned)C2
->Kind
)
420 else if (C1
->Kind
== C2
->Kind
)
421 return C1
->BBI
.BB
->getNumber() < C2
->BBI
.BB
->getNumber();
428 } // end anonymous namespace
430 char IfConverter::ID
= 0;
432 char &llvm::IfConverterID
= IfConverter::ID
;
434 INITIALIZE_PASS_BEGIN(IfConverter
, DEBUG_TYPE
, "If Converter", false, false)
435 INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfoWrapperPass
)
436 INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass
)
437 INITIALIZE_PASS_END(IfConverter
, DEBUG_TYPE
, "If Converter", false, false)
439 bool IfConverter::runOnMachineFunction(MachineFunction
&MF
) {
440 if (skipFunction(MF
.getFunction()) || (PredicateFtor
&& !PredicateFtor(MF
)))
443 const TargetSubtargetInfo
&ST
= MF
.getSubtarget();
444 TLI
= ST
.getTargetLowering();
445 TII
= ST
.getInstrInfo();
446 TRI
= ST
.getRegisterInfo();
448 getAnalysis
<MachineBlockFrequencyInfoWrapperPass
>().getMBFI());
449 MBPI
= &getAnalysis
<MachineBranchProbabilityInfoWrapperPass
>().getMBPI();
450 ProfileSummaryInfo
*PSI
=
451 &getAnalysis
<ProfileSummaryInfoWrapperPass
>().getPSI();
452 MRI
= &MF
.getRegInfo();
453 SchedModel
.init(&ST
);
455 if (!TII
) return false;
457 PreRegAlloc
= MRI
->isSSA();
459 bool BFChange
= false;
461 // Tail merge tend to expose more if-conversion opportunities.
462 BranchFolder
BF(true, false, MBFI
, *MBPI
, PSI
);
463 BFChange
= BF
.OptimizeFunction(MF
, TII
, ST
.getRegisterInfo());
466 LLVM_DEBUG(dbgs() << "\nIfcvt: function (" << ++FnNum
<< ") \'"
467 << MF
.getName() << "\'");
469 if (FnNum
< IfCvtFnStart
|| (IfCvtFnStop
!= -1 && FnNum
> IfCvtFnStop
)) {
470 LLVM_DEBUG(dbgs() << " skipped\n");
473 LLVM_DEBUG(dbgs() << "\n");
476 BBAnalysis
.resize(MF
.getNumBlockIDs());
478 std::vector
<std::unique_ptr
<IfcvtToken
>> Tokens
;
480 unsigned NumIfCvts
= NumSimple
+ NumSimpleFalse
+ NumTriangle
+
481 NumTriangleRev
+ NumTriangleFalse
+ NumTriangleFRev
+ NumDiamonds
;
482 while (IfCvtLimit
== -1 || (int)NumIfCvts
< IfCvtLimit
) {
483 // Do an initial analysis for each basic block and find all the potential
484 // candidates to perform if-conversion.
486 AnalyzeBlocks(MF
, Tokens
);
487 while (!Tokens
.empty()) {
488 std::unique_ptr
<IfcvtToken
> Token
= std::move(Tokens
.back());
490 BBInfo
&BBI
= Token
->BBI
;
491 IfcvtKind Kind
= Token
->Kind
;
492 unsigned NumDups
= Token
->NumDups
;
493 unsigned NumDups2
= Token
->NumDups2
;
495 // If the block has been evicted out of the queue or it has already been
496 // marked dead (due to it being predicated), then skip it.
498 BBI
.IsEnqueued
= false;
502 BBI
.IsEnqueued
= false;
506 default: llvm_unreachable("Unexpected!");
508 case ICSimpleFalse
: {
509 bool isFalse
= Kind
== ICSimpleFalse
;
510 if ((isFalse
&& DisableSimpleF
) || (!isFalse
&& DisableSimple
)) break;
511 LLVM_DEBUG(dbgs() << "Ifcvt (Simple"
512 << (Kind
== ICSimpleFalse
? " false" : "")
513 << "): " << printMBBReference(*BBI
.BB
) << " ("
514 << ((Kind
== ICSimpleFalse
) ? BBI
.FalseBB
->getNumber()
515 : BBI
.TrueBB
->getNumber())
517 RetVal
= IfConvertSimple(BBI
, Kind
);
518 LLVM_DEBUG(dbgs() << (RetVal
? "succeeded!" : "failed!") << "\n");
520 if (isFalse
) ++NumSimpleFalse
;
527 case ICTriangleFalse
:
528 case ICTriangleFRev
: {
529 bool isFalse
= Kind
== ICTriangleFalse
;
530 bool isRev
= (Kind
== ICTriangleRev
|| Kind
== ICTriangleFRev
);
531 if (DisableTriangle
&& !isFalse
&& !isRev
) break;
532 if (DisableTriangleR
&& !isFalse
&& isRev
) break;
533 if (DisableTriangleF
&& isFalse
&& !isRev
) break;
534 LLVM_DEBUG(dbgs() << "Ifcvt (Triangle");
536 LLVM_DEBUG(dbgs() << " false");
538 LLVM_DEBUG(dbgs() << " rev");
539 LLVM_DEBUG(dbgs() << "): " << printMBBReference(*BBI
.BB
)
540 << " (T:" << BBI
.TrueBB
->getNumber()
541 << ",F:" << BBI
.FalseBB
->getNumber() << ") ");
542 RetVal
= IfConvertTriangle(BBI
, Kind
);
543 LLVM_DEBUG(dbgs() << (RetVal
? "succeeded!" : "failed!") << "\n");
555 if (DisableDiamond
) break;
556 LLVM_DEBUG(dbgs() << "Ifcvt (Diamond): " << printMBBReference(*BBI
.BB
)
557 << " (T:" << BBI
.TrueBB
->getNumber()
558 << ",F:" << BBI
.FalseBB
->getNumber() << ") ");
559 RetVal
= IfConvertDiamond(BBI
, Kind
, NumDups
, NumDups2
,
560 Token
->TClobbersPred
,
561 Token
->FClobbersPred
);
562 LLVM_DEBUG(dbgs() << (RetVal
? "succeeded!" : "failed!") << "\n");
563 if (RetVal
) ++NumDiamonds
;
565 case ICForkedDiamond
:
566 if (DisableForkedDiamond
) break;
567 LLVM_DEBUG(dbgs() << "Ifcvt (Forked Diamond): "
568 << printMBBReference(*BBI
.BB
)
569 << " (T:" << BBI
.TrueBB
->getNumber()
570 << ",F:" << BBI
.FalseBB
->getNumber() << ") ");
571 RetVal
= IfConvertForkedDiamond(BBI
, Kind
, NumDups
, NumDups2
,
572 Token
->TClobbersPred
,
573 Token
->FClobbersPred
);
574 LLVM_DEBUG(dbgs() << (RetVal
? "succeeded!" : "failed!") << "\n");
575 if (RetVal
) ++NumForkedDiamonds
;
579 if (RetVal
&& MRI
->tracksLiveness())
580 recomputeLivenessFlags(*BBI
.BB
);
584 NumIfCvts
= NumSimple
+ NumSimpleFalse
+ NumTriangle
+ NumTriangleRev
+
585 NumTriangleFalse
+ NumTriangleFRev
+ NumDiamonds
;
586 if (IfCvtLimit
!= -1 && (int)NumIfCvts
>= IfCvtLimit
)
592 MadeChange
|= Change
;
598 if (MadeChange
&& IfCvtBranchFold
) {
599 BranchFolder
BF(false, false, MBFI
, *MBPI
, PSI
);
600 BF
.OptimizeFunction(MF
, TII
, MF
.getSubtarget().getRegisterInfo());
603 MadeChange
|= BFChange
;
607 /// BB has a fallthrough. Find its 'false' successor given its 'true' successor.
608 static MachineBasicBlock
*findFalseBlock(MachineBasicBlock
*BB
,
609 MachineBasicBlock
*TrueBB
) {
610 for (MachineBasicBlock
*SuccBB
: BB
->successors()) {
611 if (SuccBB
!= TrueBB
)
617 /// Reverse the condition of the end of the block branch. Swap block's 'true'
618 /// and 'false' successors.
619 bool IfConverter::reverseBranchCondition(BBInfo
&BBI
) const {
620 DebugLoc dl
; // FIXME: this is nowhere
621 if (!TII
->reverseBranchCondition(BBI
.BrCond
)) {
622 TII
->removeBranch(*BBI
.BB
);
623 TII
->insertBranch(*BBI
.BB
, BBI
.FalseBB
, BBI
.TrueBB
, BBI
.BrCond
, dl
);
624 std::swap(BBI
.TrueBB
, BBI
.FalseBB
);
630 /// Returns the next block in the function blocks ordering. If it is the end,
632 static inline MachineBasicBlock
*getNextBlock(MachineBasicBlock
&MBB
) {
633 MachineFunction::iterator I
= MBB
.getIterator();
634 MachineFunction::iterator E
= MBB
.getParent()->end();
640 /// Returns true if the 'true' block (along with its predecessor) forms a valid
641 /// simple shape for ifcvt. It also returns the number of instructions that the
642 /// ifcvt would need to duplicate if performed in Dups.
643 bool IfConverter::ValidSimple(BBInfo
&TrueBBI
, unsigned &Dups
,
644 BranchProbability Prediction
) const {
646 if (TrueBBI
.IsBeingAnalyzed
|| TrueBBI
.IsDone
)
649 if (TrueBBI
.IsBrAnalyzable
)
652 if (TrueBBI
.BB
->pred_size() > 1) {
653 if (TrueBBI
.CannotBeCopied
||
654 !TII
->isProfitableToDupForIfCvt(*TrueBBI
.BB
, TrueBBI
.NonPredSize
,
657 Dups
= TrueBBI
.NonPredSize
;
663 /// Returns true if the 'true' and 'false' blocks (along with their common
664 /// predecessor) forms a valid triangle shape for ifcvt. If 'FalseBranch' is
665 /// true, it checks if 'true' block's false branch branches to the 'false' block
666 /// rather than the other way around. It also returns the number of instructions
667 /// that the ifcvt would need to duplicate if performed in 'Dups'.
668 bool IfConverter::ValidTriangle(BBInfo
&TrueBBI
, BBInfo
&FalseBBI
,
669 bool FalseBranch
, unsigned &Dups
,
670 BranchProbability Prediction
) const {
672 if (TrueBBI
.BB
== FalseBBI
.BB
)
675 if (TrueBBI
.IsBeingAnalyzed
|| TrueBBI
.IsDone
)
678 if (TrueBBI
.BB
->pred_size() > 1) {
679 if (TrueBBI
.CannotBeCopied
)
682 unsigned Size
= TrueBBI
.NonPredSize
;
683 if (TrueBBI
.IsBrAnalyzable
) {
684 if (TrueBBI
.TrueBB
&& TrueBBI
.BrCond
.empty())
685 // Ends with an unconditional branch. It will be removed.
688 MachineBasicBlock
*FExit
= FalseBranch
689 ? TrueBBI
.TrueBB
: TrueBBI
.FalseBB
;
691 // Require a conditional branch
695 if (!TII
->isProfitableToDupForIfCvt(*TrueBBI
.BB
, Size
, Prediction
))
700 MachineBasicBlock
*TExit
= FalseBranch
? TrueBBI
.FalseBB
: TrueBBI
.TrueBB
;
701 if (!TExit
&& blockAlwaysFallThrough(TrueBBI
)) {
702 MachineFunction::iterator I
= TrueBBI
.BB
->getIterator();
703 if (++I
== TrueBBI
.BB
->getParent()->end())
707 return TExit
&& TExit
== FalseBBI
.BB
;
710 /// Count duplicated instructions and move the iterators to show where they
712 /// @param TIB True Iterator Begin
713 /// @param FIB False Iterator Begin
714 /// These two iterators initially point to the first instruction of the two
715 /// blocks, and finally point to the first non-shared instruction.
716 /// @param TIE True Iterator End
717 /// @param FIE False Iterator End
718 /// These two iterators initially point to End() for the two blocks() and
719 /// finally point to the first shared instruction in the tail.
720 /// Upon return [TIB, TIE), and [FIB, FIE) mark the un-duplicated portions of
722 /// @param Dups1 count of duplicated instructions at the beginning of the 2
724 /// @param Dups2 count of duplicated instructions at the end of the 2 blocks.
725 /// @param SkipUnconditionalBranches if true, Don't make sure that
726 /// unconditional branches at the end of the blocks are the same. True is
727 /// passed when the blocks are analyzable to allow for fallthrough to be
729 /// @return false if the shared portion prevents if conversion.
730 bool IfConverter::CountDuplicatedInstructions(
731 MachineBasicBlock::iterator
&TIB
,
732 MachineBasicBlock::iterator
&FIB
,
733 MachineBasicBlock::iterator
&TIE
,
734 MachineBasicBlock::iterator
&FIE
,
735 unsigned &Dups1
, unsigned &Dups2
,
736 MachineBasicBlock
&TBB
, MachineBasicBlock
&FBB
,
737 bool SkipUnconditionalBranches
) const {
738 while (TIB
!= TIE
&& FIB
!= FIE
) {
739 // Skip dbg_value instructions. These do not count.
740 TIB
= skipDebugInstructionsForward(TIB
, TIE
, false);
741 FIB
= skipDebugInstructionsForward(FIB
, FIE
, false);
742 if (TIB
== TIE
|| FIB
== FIE
)
744 if (!TIB
->isIdenticalTo(*FIB
))
746 // A pred-clobbering instruction in the shared portion prevents
748 std::vector
<MachineOperand
> PredDefs
;
749 if (TII
->ClobbersPredicate(*TIB
, PredDefs
, false))
751 // If we get all the way to the branch instructions, don't count them.
752 if (!TIB
->isBranch())
758 // Check for already containing all of the block.
759 if (TIB
== TIE
|| FIB
== FIE
)
761 // Now, in preparation for counting duplicate instructions at the ends of the
762 // blocks, switch to reverse_iterators. Note that getReverse() returns an
763 // iterator that points to the same instruction, unlike std::reverse_iterator.
764 // We have to do our own shifting so that we get the same range.
765 MachineBasicBlock::reverse_iterator RTIE
= std::next(TIE
.getReverse());
766 MachineBasicBlock::reverse_iterator RFIE
= std::next(FIE
.getReverse());
767 const MachineBasicBlock::reverse_iterator RTIB
= std::next(TIB
.getReverse());
768 const MachineBasicBlock::reverse_iterator RFIB
= std::next(FIB
.getReverse());
770 if (!TBB
.succ_empty() || !FBB
.succ_empty()) {
771 if (SkipUnconditionalBranches
) {
772 while (RTIE
!= RTIB
&& RTIE
->isUnconditionalBranch())
774 while (RFIE
!= RFIB
&& RFIE
->isUnconditionalBranch())
779 // Count duplicate instructions at the ends of the blocks.
780 while (RTIE
!= RTIB
&& RFIE
!= RFIB
) {
781 // Skip dbg_value instructions. These do not count.
782 // Note that these are reverse iterators going forward.
783 RTIE
= skipDebugInstructionsForward(RTIE
, RTIB
, false);
784 RFIE
= skipDebugInstructionsForward(RFIE
, RFIB
, false);
785 if (RTIE
== RTIB
|| RFIE
== RFIB
)
787 if (!RTIE
->isIdenticalTo(*RFIE
))
789 // We have to verify that any branch instructions are the same, and then we
790 // don't count them toward the # of duplicate instructions.
791 if (!RTIE
->isBranch())
796 TIE
= std::next(RTIE
.getReverse());
797 FIE
= std::next(RFIE
.getReverse());
801 /// RescanInstructions - Run ScanInstructions on a pair of blocks.
802 /// @param TIB - True Iterator Begin, points to first non-shared instruction
803 /// @param FIB - False Iterator Begin, points to first non-shared instruction
804 /// @param TIE - True Iterator End, points past last non-shared instruction
805 /// @param FIE - False Iterator End, points past last non-shared instruction
806 /// @param TrueBBI - BBInfo to update for the true block.
807 /// @param FalseBBI - BBInfo to update for the false block.
808 /// @returns - false if either block cannot be predicated or if both blocks end
809 /// with a predicate-clobbering instruction.
810 bool IfConverter::RescanInstructions(
811 MachineBasicBlock::iterator
&TIB
, MachineBasicBlock::iterator
&FIB
,
812 MachineBasicBlock::iterator
&TIE
, MachineBasicBlock::iterator
&FIE
,
813 BBInfo
&TrueBBI
, BBInfo
&FalseBBI
) const {
814 bool BranchUnpredicable
= true;
815 TrueBBI
.IsUnpredicable
= FalseBBI
.IsUnpredicable
= false;
816 ScanInstructions(TrueBBI
, TIB
, TIE
, BranchUnpredicable
);
817 if (TrueBBI
.IsUnpredicable
)
819 ScanInstructions(FalseBBI
, FIB
, FIE
, BranchUnpredicable
);
820 if (FalseBBI
.IsUnpredicable
)
822 if (TrueBBI
.ClobbersPred
&& FalseBBI
.ClobbersPred
)
828 static void verifySameBranchInstructions(
829 MachineBasicBlock
*MBB1
,
830 MachineBasicBlock
*MBB2
) {
831 const MachineBasicBlock::reverse_iterator B1
= MBB1
->rend();
832 const MachineBasicBlock::reverse_iterator B2
= MBB2
->rend();
833 MachineBasicBlock::reverse_iterator E1
= MBB1
->rbegin();
834 MachineBasicBlock::reverse_iterator E2
= MBB2
->rbegin();
835 while (E1
!= B1
&& E2
!= B2
) {
836 skipDebugInstructionsForward(E1
, B1
, false);
837 skipDebugInstructionsForward(E2
, B2
, false);
838 if (E1
== B1
&& E2
== B2
)
842 assert(!E2
->isBranch() && "Branch mis-match, one block is empty.");
846 assert(!E1
->isBranch() && "Branch mis-match, one block is empty.");
850 if (E1
->isBranch() || E2
->isBranch())
851 assert(E1
->isIdenticalTo(*E2
) &&
852 "Branch mis-match, branch instructions don't match.");
861 /// ValidForkedDiamond - Returns true if the 'true' and 'false' blocks (along
862 /// with their common predecessor) form a diamond if a common tail block is
864 /// While not strictly a diamond, this pattern would form a diamond if
865 /// tail-merging had merged the shared tails.
871 /// FalseBB TrueBB FalseBB
872 /// Currently only handles analyzable branches.
873 /// Specifically excludes actual diamonds to avoid overlap.
874 bool IfConverter::ValidForkedDiamond(
875 BBInfo
&TrueBBI
, BBInfo
&FalseBBI
,
876 unsigned &Dups1
, unsigned &Dups2
,
877 BBInfo
&TrueBBICalc
, BBInfo
&FalseBBICalc
) const {
879 if (TrueBBI
.IsBeingAnalyzed
|| TrueBBI
.IsDone
||
880 FalseBBI
.IsBeingAnalyzed
|| FalseBBI
.IsDone
)
883 if (!TrueBBI
.IsBrAnalyzable
|| !FalseBBI
.IsBrAnalyzable
)
885 // Don't IfConvert blocks that can't be folded into their predecessor.
886 if (TrueBBI
.BB
->pred_size() > 1 || FalseBBI
.BB
->pred_size() > 1)
889 // This function is specifically looking for conditional tails, as
890 // unconditional tails are already handled by the standard diamond case.
891 if (TrueBBI
.BrCond
.size() == 0 ||
892 FalseBBI
.BrCond
.size() == 0)
895 MachineBasicBlock
*TT
= TrueBBI
.TrueBB
;
896 MachineBasicBlock
*TF
= TrueBBI
.FalseBB
;
897 MachineBasicBlock
*FT
= FalseBBI
.TrueBB
;
898 MachineBasicBlock
*FF
= FalseBBI
.FalseBB
;
901 TT
= getNextBlock(*TrueBBI
.BB
);
903 TF
= getNextBlock(*TrueBBI
.BB
);
905 FT
= getNextBlock(*FalseBBI
.BB
);
907 FF
= getNextBlock(*FalseBBI
.BB
);
912 // Check successors. If they don't match, bail.
913 if (!((TT
== FT
&& TF
== FF
) || (TF
== FT
&& TT
== FF
)))
916 bool FalseReversed
= false;
917 if (TF
== FT
&& TT
== FF
) {
918 // If the branches are opposing, but we can't reverse, don't do it.
919 if (!FalseBBI
.IsBrReversible
)
921 FalseReversed
= true;
922 reverseBranchCondition(FalseBBI
);
924 auto UnReverseOnExit
= make_scope_exit([&]() {
926 reverseBranchCondition(FalseBBI
);
929 // Count duplicate instructions at the beginning of the true and false blocks.
930 MachineBasicBlock::iterator TIB
= TrueBBI
.BB
->begin();
931 MachineBasicBlock::iterator FIB
= FalseBBI
.BB
->begin();
932 MachineBasicBlock::iterator TIE
= TrueBBI
.BB
->end();
933 MachineBasicBlock::iterator FIE
= FalseBBI
.BB
->end();
934 if(!CountDuplicatedInstructions(TIB
, FIB
, TIE
, FIE
, Dups1
, Dups2
,
935 *TrueBBI
.BB
, *FalseBBI
.BB
,
936 /* SkipUnconditionalBranches */ true))
939 TrueBBICalc
.BB
= TrueBBI
.BB
;
940 FalseBBICalc
.BB
= FalseBBI
.BB
;
941 TrueBBICalc
.IsBrAnalyzable
= TrueBBI
.IsBrAnalyzable
;
942 FalseBBICalc
.IsBrAnalyzable
= FalseBBI
.IsBrAnalyzable
;
943 if (!RescanInstructions(TIB
, FIB
, TIE
, FIE
, TrueBBICalc
, FalseBBICalc
))
946 // The size is used to decide whether to if-convert, and the shared portions
947 // are subtracted off. Because of the subtraction, we just use the size that
948 // was calculated by the original ScanInstructions, as it is correct.
949 TrueBBICalc
.NonPredSize
= TrueBBI
.NonPredSize
;
950 FalseBBICalc
.NonPredSize
= FalseBBI
.NonPredSize
;
954 /// ValidDiamond - Returns true if the 'true' and 'false' blocks (along
955 /// with their common predecessor) forms a valid diamond shape for ifcvt.
956 bool IfConverter::ValidDiamond(
957 BBInfo
&TrueBBI
, BBInfo
&FalseBBI
,
958 unsigned &Dups1
, unsigned &Dups2
,
959 BBInfo
&TrueBBICalc
, BBInfo
&FalseBBICalc
) const {
961 if (TrueBBI
.IsBeingAnalyzed
|| TrueBBI
.IsDone
||
962 FalseBBI
.IsBeingAnalyzed
|| FalseBBI
.IsDone
)
965 // If the True and False BBs are equal we're dealing with a degenerate case
966 // that we don't treat as a diamond.
967 if (TrueBBI
.BB
== FalseBBI
.BB
)
970 MachineBasicBlock
*TT
= TrueBBI
.TrueBB
;
971 MachineBasicBlock
*FT
= FalseBBI
.TrueBB
;
973 if (!TT
&& blockAlwaysFallThrough(TrueBBI
))
974 TT
= getNextBlock(*TrueBBI
.BB
);
975 if (!FT
&& blockAlwaysFallThrough(FalseBBI
))
976 FT
= getNextBlock(*FalseBBI
.BB
);
979 if (!TT
&& (TrueBBI
.IsBrAnalyzable
|| FalseBBI
.IsBrAnalyzable
))
981 if (TrueBBI
.BB
->pred_size() > 1 || FalseBBI
.BB
->pred_size() > 1)
984 // FIXME: Allow true block to have an early exit?
985 if (TrueBBI
.FalseBB
|| FalseBBI
.FalseBB
)
988 // Count duplicate instructions at the beginning and end of the true and
990 // Skip unconditional branches only if we are considering an analyzable
991 // diamond. Otherwise the branches must be the same.
992 bool SkipUnconditionalBranches
=
993 TrueBBI
.IsBrAnalyzable
&& FalseBBI
.IsBrAnalyzable
;
994 MachineBasicBlock::iterator TIB
= TrueBBI
.BB
->begin();
995 MachineBasicBlock::iterator FIB
= FalseBBI
.BB
->begin();
996 MachineBasicBlock::iterator TIE
= TrueBBI
.BB
->end();
997 MachineBasicBlock::iterator FIE
= FalseBBI
.BB
->end();
998 if(!CountDuplicatedInstructions(TIB
, FIB
, TIE
, FIE
, Dups1
, Dups2
,
999 *TrueBBI
.BB
, *FalseBBI
.BB
,
1000 SkipUnconditionalBranches
))
1003 TrueBBICalc
.BB
= TrueBBI
.BB
;
1004 FalseBBICalc
.BB
= FalseBBI
.BB
;
1005 TrueBBICalc
.IsBrAnalyzable
= TrueBBI
.IsBrAnalyzable
;
1006 FalseBBICalc
.IsBrAnalyzable
= FalseBBI
.IsBrAnalyzable
;
1007 if (!RescanInstructions(TIB
, FIB
, TIE
, FIE
, TrueBBICalc
, FalseBBICalc
))
1009 // The size is used to decide whether to if-convert, and the shared portions
1010 // are subtracted off. Because of the subtraction, we just use the size that
1011 // was calculated by the original ScanInstructions, as it is correct.
1012 TrueBBICalc
.NonPredSize
= TrueBBI
.NonPredSize
;
1013 FalseBBICalc
.NonPredSize
= FalseBBI
.NonPredSize
;
1017 /// AnalyzeBranches - Look at the branches at the end of a block to determine if
1018 /// the block is predicable.
1019 void IfConverter::AnalyzeBranches(BBInfo
&BBI
) {
1023 BBI
.TrueBB
= BBI
.FalseBB
= nullptr;
1025 BBI
.IsBrAnalyzable
=
1026 !TII
->analyzeBranch(*BBI
.BB
, BBI
.TrueBB
, BBI
.FalseBB
, BBI
.BrCond
);
1027 if (!BBI
.IsBrAnalyzable
) {
1028 BBI
.TrueBB
= nullptr;
1029 BBI
.FalseBB
= nullptr;
1033 SmallVector
<MachineOperand
, 4> RevCond(BBI
.BrCond
.begin(), BBI
.BrCond
.end());
1034 BBI
.IsBrReversible
= (RevCond
.size() == 0) ||
1035 !TII
->reverseBranchCondition(RevCond
);
1036 BBI
.HasFallThrough
= BBI
.IsBrAnalyzable
&& BBI
.FalseBB
== nullptr;
1038 if (BBI
.BrCond
.size()) {
1039 // No false branch. This BB must end with a conditional branch and a
1042 BBI
.FalseBB
= findFalseBlock(BBI
.BB
, BBI
.TrueBB
);
1044 // Malformed bcc? True and false blocks are the same?
1045 BBI
.IsUnpredicable
= true;
1050 /// ScanInstructions - Scan all the instructions in the block to determine if
1051 /// the block is predicable. In most cases, that means all the instructions
1052 /// in the block are isPredicable(). Also checks if the block contains any
1053 /// instruction which can clobber a predicate (e.g. condition code register).
1054 /// If so, the block is not predicable unless it's the last instruction.
1055 void IfConverter::ScanInstructions(BBInfo
&BBI
,
1056 MachineBasicBlock::iterator
&Begin
,
1057 MachineBasicBlock::iterator
&End
,
1058 bool BranchUnpredicable
) const {
1059 if (BBI
.IsDone
|| BBI
.IsUnpredicable
)
1062 bool AlreadyPredicated
= !BBI
.Predicate
.empty();
1064 BBI
.NonPredSize
= 0;
1067 BBI
.ClobbersPred
= false;
1068 for (MachineInstr
&MI
: make_range(Begin
, End
)) {
1069 if (MI
.isDebugInstr())
1072 // It's unsafe to duplicate convergent instructions in this context, so set
1073 // BBI.CannotBeCopied to true if MI is convergent. To see why, consider the
1074 // following CFG, which is subject to our "simple" transformation.
1076 // BB0 // if (c1) goto BB1; else goto BB2;
1079 // | BB2 // if (c2) goto TBB; else goto FBB;
1088 // Suppose we want to move TBB's contents up into BB1 and BB2 (in BB1 they'd
1089 // be unconditional, and in BB2, they'd be predicated upon c2), and suppose
1090 // TBB contains a convergent instruction. This is safe iff doing so does
1091 // not add a control-flow dependency to the convergent instruction -- i.e.,
1092 // it's safe iff the set of control flows that leads us to the convergent
1093 // instruction does not get smaller after the transformation.
1095 // Originally we executed TBB if c1 || c2. After the transformation, there
1096 // are two copies of TBB's instructions. We get to the first if c1, and we
1097 // get to the second if !c1 && c2.
1099 // There are clearly fewer ways to satisfy the condition "c1" than
1100 // "c1 || c2". Since we've shrunk the set of control flows which lead to
1101 // our convergent instruction, the transformation is unsafe.
1102 if (MI
.isNotDuplicable() || MI
.isConvergent())
1103 BBI
.CannotBeCopied
= true;
1105 bool isPredicated
= TII
->isPredicated(MI
);
1106 bool isCondBr
= BBI
.IsBrAnalyzable
&& MI
.isConditionalBranch();
1108 if (BranchUnpredicable
&& MI
.isBranch()) {
1109 BBI
.IsUnpredicable
= true;
1113 // A conditional branch is not predicable, but it may be eliminated.
1117 if (!isPredicated
) {
1119 unsigned ExtraPredCost
= TII
->getPredicationCost(MI
);
1120 unsigned NumCycles
= SchedModel
.computeInstrLatency(&MI
, false);
1122 BBI
.ExtraCost
+= NumCycles
-1;
1123 BBI
.ExtraCost2
+= ExtraPredCost
;
1124 } else if (!AlreadyPredicated
) {
1125 // FIXME: This instruction is already predicated before the
1126 // if-conversion pass. It's probably something like a conditional move.
1127 // Mark this block unpredicable for now.
1128 BBI
.IsUnpredicable
= true;
1132 if (BBI
.ClobbersPred
&& !isPredicated
) {
1133 // Predicate modification instruction should end the block (except for
1134 // already predicated instructions and end of block branches).
1135 // Predicate may have been modified, the subsequent (currently)
1136 // unpredicated instructions cannot be correctly predicated.
1137 BBI
.IsUnpredicable
= true;
1141 // FIXME: Make use of PredDefs? e.g. ADDC, SUBC sets predicates but are
1142 // still potentially predicable.
1143 std::vector
<MachineOperand
> PredDefs
;
1144 if (TII
->ClobbersPredicate(MI
, PredDefs
, true))
1145 BBI
.ClobbersPred
= true;
1147 if (!TII
->isPredicable(MI
)) {
1148 BBI
.IsUnpredicable
= true;
1154 /// Determine if the block is a suitable candidate to be predicated by the
1155 /// specified predicate.
1156 /// @param BBI BBInfo for the block to check
1157 /// @param Pred Predicate array for the branch that leads to BBI
1158 /// @param isTriangle true if the Analysis is for a triangle
1159 /// @param RevBranch true if Reverse(Pred) leads to BBI (e.g. BBI is the false
1161 /// @param hasCommonTail true if BBI shares a tail with a sibling block that
1162 /// contains any instruction that would make the block unpredicable.
1163 bool IfConverter::FeasibilityAnalysis(BBInfo
&BBI
,
1164 SmallVectorImpl
<MachineOperand
> &Pred
,
1165 bool isTriangle
, bool RevBranch
,
1166 bool hasCommonTail
) {
1167 // If the block is dead or unpredicable, then it cannot be predicated.
1168 // Two blocks may share a common unpredicable tail, but this doesn't prevent
1169 // them from being if-converted. The non-shared portion is assumed to have
1171 if (BBI
.IsDone
|| (BBI
.IsUnpredicable
&& !hasCommonTail
))
1174 // If it is already predicated but we couldn't analyze its terminator, the
1175 // latter might fallthrough, but we can't determine where to.
1176 // Conservatively avoid if-converting again.
1177 if (BBI
.Predicate
.size() && !BBI
.IsBrAnalyzable
)
1180 // If it is already predicated, check if the new predicate subsumes
1182 if (BBI
.Predicate
.size() && !TII
->SubsumesPredicate(Pred
, BBI
.Predicate
))
1185 if (!hasCommonTail
&& BBI
.BrCond
.size()) {
1189 // Test predicate subsumption.
1190 SmallVector
<MachineOperand
, 4> RevPred(Pred
.begin(), Pred
.end());
1191 SmallVector
<MachineOperand
, 4> Cond(BBI
.BrCond
.begin(), BBI
.BrCond
.end());
1193 if (TII
->reverseBranchCondition(Cond
))
1196 if (TII
->reverseBranchCondition(RevPred
) ||
1197 !TII
->SubsumesPredicate(Cond
, RevPred
))
1204 /// Analyze the structure of the sub-CFG starting from the specified block.
1205 /// Record its successors and whether it looks like an if-conversion candidate.
1206 void IfConverter::AnalyzeBlock(
1207 MachineBasicBlock
&MBB
, std::vector
<std::unique_ptr
<IfcvtToken
>> &Tokens
) {
1209 BBState(MachineBasicBlock
&MBB
) : MBB(&MBB
) {}
1210 MachineBasicBlock
*MBB
;
1212 /// This flag is true if MBB's successors have been analyzed.
1213 bool SuccsAnalyzed
= false;
1216 // Push MBB to the stack.
1217 SmallVector
<BBState
, 16> BBStack(1, MBB
);
1219 while (!BBStack
.empty()) {
1220 BBState
&State
= BBStack
.back();
1221 MachineBasicBlock
*BB
= State
.MBB
;
1222 BBInfo
&BBI
= BBAnalysis
[BB
->getNumber()];
1224 if (!State
.SuccsAnalyzed
) {
1225 if (BBI
.IsAnalyzed
|| BBI
.IsBeingAnalyzed
) {
1231 BBI
.IsBeingAnalyzed
= true;
1233 AnalyzeBranches(BBI
);
1234 MachineBasicBlock::iterator Begin
= BBI
.BB
->begin();
1235 MachineBasicBlock::iterator End
= BBI
.BB
->end();
1236 ScanInstructions(BBI
, Begin
, End
);
1238 // Unanalyzable or ends with fallthrough or unconditional branch, or if is
1239 // not considered for ifcvt anymore.
1240 if (!BBI
.IsBrAnalyzable
|| BBI
.BrCond
.empty() || BBI
.IsDone
) {
1241 BBI
.IsBeingAnalyzed
= false;
1242 BBI
.IsAnalyzed
= true;
1247 // Do not ifcvt if either path is a back edge to the entry block.
1248 if (BBI
.TrueBB
== BB
|| BBI
.FalseBB
== BB
) {
1249 BBI
.IsBeingAnalyzed
= false;
1250 BBI
.IsAnalyzed
= true;
1255 // Do not ifcvt if true and false fallthrough blocks are the same.
1257 BBI
.IsBeingAnalyzed
= false;
1258 BBI
.IsAnalyzed
= true;
1263 // Push the False and True blocks to the stack.
1264 State
.SuccsAnalyzed
= true;
1265 BBStack
.push_back(*BBI
.FalseBB
);
1266 BBStack
.push_back(*BBI
.TrueBB
);
1270 BBInfo
&TrueBBI
= BBAnalysis
[BBI
.TrueBB
->getNumber()];
1271 BBInfo
&FalseBBI
= BBAnalysis
[BBI
.FalseBB
->getNumber()];
1273 if (TrueBBI
.IsDone
&& FalseBBI
.IsDone
) {
1274 BBI
.IsBeingAnalyzed
= false;
1275 BBI
.IsAnalyzed
= true;
1280 SmallVector
<MachineOperand
, 4>
1281 RevCond(BBI
.BrCond
.begin(), BBI
.BrCond
.end());
1282 bool CanRevCond
= !TII
->reverseBranchCondition(RevCond
);
1286 bool TNeedSub
= !TrueBBI
.Predicate
.empty();
1287 bool FNeedSub
= !FalseBBI
.Predicate
.empty();
1288 bool Enqueued
= false;
1290 BranchProbability Prediction
= MBPI
->getEdgeProbability(BB
, TrueBBI
.BB
);
1293 BBInfo TrueBBICalc
, FalseBBICalc
;
1294 auto feasibleDiamond
= [&](bool Forked
) {
1295 bool MeetsSize
= MeetIfcvtSizeLimit(TrueBBICalc
, FalseBBICalc
, *BB
,
1296 Dups
+ Dups2
, Prediction
, Forked
);
1297 bool TrueFeasible
= FeasibilityAnalysis(TrueBBI
, BBI
.BrCond
,
1298 /* IsTriangle */ false, /* RevCond */ false,
1299 /* hasCommonTail */ true);
1300 bool FalseFeasible
= FeasibilityAnalysis(FalseBBI
, RevCond
,
1301 /* IsTriangle */ false, /* RevCond */ false,
1302 /* hasCommonTail */ true);
1303 return MeetsSize
&& TrueFeasible
&& FalseFeasible
;
1306 if (ValidDiamond(TrueBBI
, FalseBBI
, Dups
, Dups2
,
1307 TrueBBICalc
, FalseBBICalc
)) {
1308 if (feasibleDiamond(false)) {
1316 // Note TailBB can be empty.
1317 Tokens
.push_back(std::make_unique
<IfcvtToken
>(
1318 BBI
, ICDiamond
, TNeedSub
| FNeedSub
, Dups
, Dups2
,
1319 (bool) TrueBBICalc
.ClobbersPred
, (bool) FalseBBICalc
.ClobbersPred
));
1322 } else if (ValidForkedDiamond(TrueBBI
, FalseBBI
, Dups
, Dups2
,
1323 TrueBBICalc
, FalseBBICalc
)) {
1324 if (feasibleDiamond(true)) {
1326 // if TBB and FBB have a common tail that includes their conditional
1327 // branch instructions, then we can If Convert this pattern.
1333 // FalseBB TrueBB FalseBB
1335 Tokens
.push_back(std::make_unique
<IfcvtToken
>(
1336 BBI
, ICForkedDiamond
, TNeedSub
| FNeedSub
, Dups
, Dups2
,
1337 (bool) TrueBBICalc
.ClobbersPred
, (bool) FalseBBICalc
.ClobbersPred
));
1343 if (ValidTriangle(TrueBBI
, FalseBBI
, false, Dups
, Prediction
) &&
1344 MeetIfcvtSizeLimit(*TrueBBI
.BB
, TrueBBI
.NonPredSize
+ TrueBBI
.ExtraCost
,
1345 TrueBBI
.ExtraCost2
, Prediction
) &&
1346 FeasibilityAnalysis(TrueBBI
, BBI
.BrCond
, true)) {
1355 std::make_unique
<IfcvtToken
>(BBI
, ICTriangle
, TNeedSub
, Dups
));
1359 if (ValidTriangle(TrueBBI
, FalseBBI
, true, Dups
, Prediction
) &&
1360 MeetIfcvtSizeLimit(*TrueBBI
.BB
, TrueBBI
.NonPredSize
+ TrueBBI
.ExtraCost
,
1361 TrueBBI
.ExtraCost2
, Prediction
) &&
1362 FeasibilityAnalysis(TrueBBI
, BBI
.BrCond
, true, true)) {
1364 std::make_unique
<IfcvtToken
>(BBI
, ICTriangleRev
, TNeedSub
, Dups
));
1368 if (ValidSimple(TrueBBI
, Dups
, Prediction
) &&
1369 MeetIfcvtSizeLimit(*TrueBBI
.BB
, TrueBBI
.NonPredSize
+ TrueBBI
.ExtraCost
,
1370 TrueBBI
.ExtraCost2
, Prediction
) &&
1371 FeasibilityAnalysis(TrueBBI
, BBI
.BrCond
)) {
1372 // Simple (split, no rejoin):
1380 std::make_unique
<IfcvtToken
>(BBI
, ICSimple
, TNeedSub
, Dups
));
1385 // Try the other path...
1386 if (ValidTriangle(FalseBBI
, TrueBBI
, false, Dups
,
1387 Prediction
.getCompl()) &&
1388 MeetIfcvtSizeLimit(*FalseBBI
.BB
,
1389 FalseBBI
.NonPredSize
+ FalseBBI
.ExtraCost
,
1390 FalseBBI
.ExtraCost2
, Prediction
.getCompl()) &&
1391 FeasibilityAnalysis(FalseBBI
, RevCond
, true)) {
1392 Tokens
.push_back(std::make_unique
<IfcvtToken
>(BBI
, ICTriangleFalse
,
1397 if (ValidTriangle(FalseBBI
, TrueBBI
, true, Dups
,
1398 Prediction
.getCompl()) &&
1399 MeetIfcvtSizeLimit(*FalseBBI
.BB
,
1400 FalseBBI
.NonPredSize
+ FalseBBI
.ExtraCost
,
1401 FalseBBI
.ExtraCost2
, Prediction
.getCompl()) &&
1402 FeasibilityAnalysis(FalseBBI
, RevCond
, true, true)) {
1404 std::make_unique
<IfcvtToken
>(BBI
, ICTriangleFRev
, FNeedSub
, Dups
));
1408 if (ValidSimple(FalseBBI
, Dups
, Prediction
.getCompl()) &&
1409 MeetIfcvtSizeLimit(*FalseBBI
.BB
,
1410 FalseBBI
.NonPredSize
+ FalseBBI
.ExtraCost
,
1411 FalseBBI
.ExtraCost2
, Prediction
.getCompl()) &&
1412 FeasibilityAnalysis(FalseBBI
, RevCond
)) {
1414 std::make_unique
<IfcvtToken
>(BBI
, ICSimpleFalse
, FNeedSub
, Dups
));
1419 BBI
.IsEnqueued
= Enqueued
;
1420 BBI
.IsBeingAnalyzed
= false;
1421 BBI
.IsAnalyzed
= true;
1426 /// Analyze all blocks and find entries for all if-conversion candidates.
1427 void IfConverter::AnalyzeBlocks(
1428 MachineFunction
&MF
, std::vector
<std::unique_ptr
<IfcvtToken
>> &Tokens
) {
1429 for (MachineBasicBlock
&MBB
: MF
)
1430 AnalyzeBlock(MBB
, Tokens
);
1432 // Sort to favor more complex ifcvt scheme.
1433 llvm::stable_sort(Tokens
, IfcvtTokenCmp
);
1436 /// Returns true either if ToMBB is the next block after MBB or that all the
1437 /// intervening blocks are empty (given MBB can fall through to its next block).
1438 static bool canFallThroughTo(MachineBasicBlock
&MBB
, MachineBasicBlock
&ToMBB
) {
1439 MachineFunction::iterator PI
= MBB
.getIterator();
1440 MachineFunction::iterator I
= std::next(PI
);
1441 MachineFunction::iterator TI
= ToMBB
.getIterator();
1442 MachineFunction::iterator E
= MBB
.getParent()->end();
1444 // Check isSuccessor to avoid case where the next block is empty, but
1445 // it's not a successor.
1446 if (I
== E
|| !I
->empty() || !PI
->isSuccessor(&*I
))
1450 // Finally see if the last I is indeed a successor to PI.
1451 return PI
->isSuccessor(&*I
);
1454 /// Invalidate predecessor BB info so it would be re-analyzed to determine if it
1455 /// can be if-converted. If predecessor is already enqueued, dequeue it!
1456 void IfConverter::InvalidatePreds(MachineBasicBlock
&MBB
) {
1457 for (const MachineBasicBlock
*Predecessor
: MBB
.predecessors()) {
1458 BBInfo
&PBBI
= BBAnalysis
[Predecessor
->getNumber()];
1459 if (PBBI
.IsDone
|| PBBI
.BB
== &MBB
)
1461 PBBI
.IsAnalyzed
= false;
1462 PBBI
.IsEnqueued
= false;
1466 /// Inserts an unconditional branch from \p MBB to \p ToMBB.
1467 static void InsertUncondBranch(MachineBasicBlock
&MBB
, MachineBasicBlock
&ToMBB
,
1468 const TargetInstrInfo
*TII
) {
1469 DebugLoc dl
; // FIXME: this is nowhere
1470 SmallVector
<MachineOperand
, 0> NoCond
;
1471 TII
->insertBranch(MBB
, &ToMBB
, nullptr, NoCond
, dl
);
1474 /// Behaves like LiveRegUnits::StepForward() but also adds implicit uses to all
1475 /// values defined in MI which are also live/used by MI.
1476 static void UpdatePredRedefs(MachineInstr
&MI
, LivePhysRegs
&Redefs
) {
1477 const TargetRegisterInfo
*TRI
= MI
.getMF()->getSubtarget().getRegisterInfo();
1479 // Before stepping forward past MI, remember which regs were live
1480 // before MI. This is needed to set the Undef flag only when reg is
1482 SparseSet
<MCPhysReg
, identity
<MCPhysReg
>> LiveBeforeMI
;
1483 LiveBeforeMI
.setUniverse(TRI
->getNumRegs());
1484 for (unsigned Reg
: Redefs
)
1485 LiveBeforeMI
.insert(Reg
);
1487 SmallVector
<std::pair
<MCPhysReg
, const MachineOperand
*>, 4> Clobbers
;
1488 Redefs
.stepForward(MI
, Clobbers
);
1490 // Now add the implicit uses for each of the clobbered values.
1491 for (auto Clobber
: Clobbers
) {
1492 // FIXME: Const cast here is nasty, but better than making StepForward
1493 // take a mutable instruction instead of const.
1494 unsigned Reg
= Clobber
.first
;
1495 MachineOperand
&Op
= const_cast<MachineOperand
&>(*Clobber
.second
);
1496 MachineInstr
*OpMI
= Op
.getParent();
1497 MachineInstrBuilder
MIB(*OpMI
->getMF(), OpMI
);
1498 if (Op
.isRegMask()) {
1499 // First handle regmasks. They clobber any entries in the mask which
1500 // means that we need a def for those registers.
1501 if (LiveBeforeMI
.count(Reg
))
1502 MIB
.addReg(Reg
, RegState::Implicit
);
1504 // We also need to add an implicit def of this register for the later
1505 // use to read from.
1506 // For the register allocator to have allocated a register clobbered
1507 // by the call which is used later, it must be the case that
1508 // the call doesn't return.
1509 MIB
.addReg(Reg
, RegState::Implicit
| RegState::Define
);
1512 if (any_of(TRI
->subregs_inclusive(Reg
),
1513 [&](MCPhysReg S
) { return LiveBeforeMI
.count(S
); }))
1514 MIB
.addReg(Reg
, RegState::Implicit
);
1518 /// If convert a simple (split, no rejoin) sub-CFG.
1519 bool IfConverter::IfConvertSimple(BBInfo
&BBI
, IfcvtKind Kind
) {
1520 BBInfo
&TrueBBI
= BBAnalysis
[BBI
.TrueBB
->getNumber()];
1521 BBInfo
&FalseBBI
= BBAnalysis
[BBI
.FalseBB
->getNumber()];
1522 BBInfo
*CvtBBI
= &TrueBBI
;
1523 BBInfo
*NextBBI
= &FalseBBI
;
1525 SmallVector
<MachineOperand
, 4> Cond(BBI
.BrCond
.begin(), BBI
.BrCond
.end());
1526 if (Kind
== ICSimpleFalse
)
1527 std::swap(CvtBBI
, NextBBI
);
1529 MachineBasicBlock
&CvtMBB
= *CvtBBI
->BB
;
1530 MachineBasicBlock
&NextMBB
= *NextBBI
->BB
;
1531 if (CvtBBI
->IsDone
||
1532 (CvtBBI
->CannotBeCopied
&& CvtMBB
.pred_size() > 1)) {
1533 // Something has changed. It's no longer safe to predicate this block.
1534 BBI
.IsAnalyzed
= false;
1535 CvtBBI
->IsAnalyzed
= false;
1539 if (CvtMBB
.hasAddressTaken())
1540 // Conservatively abort if-conversion if BB's address is taken.
1543 if (Kind
== ICSimpleFalse
)
1544 if (TII
->reverseBranchCondition(Cond
))
1545 llvm_unreachable("Unable to reverse branch condition!");
1549 if (MRI
->tracksLiveness()) {
1550 // Initialize liveins to the first BB. These are potentially redefined by
1551 // predicated instructions.
1552 Redefs
.addLiveInsNoPristines(CvtMBB
);
1553 Redefs
.addLiveInsNoPristines(NextMBB
);
1556 // Remove the branches from the entry so we can add the contents of the true
1558 BBI
.NonPredSize
-= TII
->removeBranch(*BBI
.BB
);
1560 if (CvtMBB
.pred_size() > 1) {
1561 // Copy instructions in the true block, predicate them, and add them to
1563 CopyAndPredicateBlock(BBI
, *CvtBBI
, Cond
);
1565 // Keep the CFG updated.
1566 BBI
.BB
->removeSuccessor(&CvtMBB
, true);
1568 // Predicate the instructions in the true block.
1569 PredicateBlock(*CvtBBI
, CvtMBB
.end(), Cond
);
1571 // Merge converted block into entry block. The BB to Cvt edge is removed
1573 MergeBlocks(BBI
, *CvtBBI
);
1576 bool IterIfcvt
= true;
1577 if (!canFallThroughTo(*BBI
.BB
, NextMBB
)) {
1578 InsertUncondBranch(*BBI
.BB
, NextMBB
, TII
);
1579 BBI
.HasFallThrough
= false;
1580 // Now ifcvt'd block will look like this:
1587 // We cannot further ifcvt this block because the unconditional branch
1588 // will have to be predicated on the new condition, that will not be
1589 // available if cmp executes.
1593 // Update block info. BB can be iteratively if-converted.
1596 InvalidatePreds(*BBI
.BB
);
1597 CvtBBI
->IsDone
= true;
1599 // FIXME: Must maintain LiveIns.
1603 /// If convert a triangle sub-CFG.
1604 bool IfConverter::IfConvertTriangle(BBInfo
&BBI
, IfcvtKind Kind
) {
1605 BBInfo
&TrueBBI
= BBAnalysis
[BBI
.TrueBB
->getNumber()];
1606 BBInfo
&FalseBBI
= BBAnalysis
[BBI
.FalseBB
->getNumber()];
1607 BBInfo
*CvtBBI
= &TrueBBI
;
1608 BBInfo
*NextBBI
= &FalseBBI
;
1609 DebugLoc dl
; // FIXME: this is nowhere
1611 SmallVector
<MachineOperand
, 4> Cond(BBI
.BrCond
.begin(), BBI
.BrCond
.end());
1612 if (Kind
== ICTriangleFalse
|| Kind
== ICTriangleFRev
)
1613 std::swap(CvtBBI
, NextBBI
);
1615 MachineBasicBlock
&CvtMBB
= *CvtBBI
->BB
;
1616 MachineBasicBlock
&NextMBB
= *NextBBI
->BB
;
1617 if (CvtBBI
->IsDone
||
1618 (CvtBBI
->CannotBeCopied
&& CvtMBB
.pred_size() > 1)) {
1619 // Something has changed. It's no longer safe to predicate this block.
1620 BBI
.IsAnalyzed
= false;
1621 CvtBBI
->IsAnalyzed
= false;
1625 if (CvtMBB
.hasAddressTaken())
1626 // Conservatively abort if-conversion if BB's address is taken.
1629 if (Kind
== ICTriangleFalse
|| Kind
== ICTriangleFRev
)
1630 if (TII
->reverseBranchCondition(Cond
))
1631 llvm_unreachable("Unable to reverse branch condition!");
1633 if (Kind
== ICTriangleRev
|| Kind
== ICTriangleFRev
) {
1634 if (reverseBranchCondition(*CvtBBI
)) {
1635 // BB has been changed, modify its predecessors (except for this
1636 // one) so they don't get ifcvt'ed based on bad intel.
1637 for (MachineBasicBlock
*PBB
: CvtMBB
.predecessors()) {
1640 BBInfo
&PBBI
= BBAnalysis
[PBB
->getNumber()];
1641 if (PBBI
.IsEnqueued
) {
1642 PBBI
.IsAnalyzed
= false;
1643 PBBI
.IsEnqueued
= false;
1649 // Initialize liveins to the first BB. These are potentially redefined by
1650 // predicated instructions.
1652 if (MRI
->tracksLiveness()) {
1653 Redefs
.addLiveInsNoPristines(CvtMBB
);
1654 Redefs
.addLiveInsNoPristines(NextMBB
);
1657 bool HasEarlyExit
= CvtBBI
->FalseBB
!= nullptr;
1658 BranchProbability CvtNext
, CvtFalse
, BBNext
, BBCvt
;
1661 // Get probabilities before modifying CvtMBB and BBI.BB.
1662 CvtNext
= MBPI
->getEdgeProbability(&CvtMBB
, &NextMBB
);
1663 CvtFalse
= MBPI
->getEdgeProbability(&CvtMBB
, CvtBBI
->FalseBB
);
1664 BBNext
= MBPI
->getEdgeProbability(BBI
.BB
, &NextMBB
);
1665 BBCvt
= MBPI
->getEdgeProbability(BBI
.BB
, &CvtMBB
);
1668 // Remove the branches from the entry so we can add the contents of the true
1670 BBI
.NonPredSize
-= TII
->removeBranch(*BBI
.BB
);
1672 if (CvtMBB
.pred_size() > 1) {
1673 // Copy instructions in the true block, predicate them, and add them to
1675 CopyAndPredicateBlock(BBI
, *CvtBBI
, Cond
, true);
1677 // Predicate the 'true' block after removing its branch.
1678 CvtBBI
->NonPredSize
-= TII
->removeBranch(CvtMBB
);
1679 PredicateBlock(*CvtBBI
, CvtMBB
.end(), Cond
);
1681 // Now merge the entry of the triangle with the true block.
1682 MergeBlocks(BBI
, *CvtBBI
, false);
1685 // Keep the CFG updated.
1686 BBI
.BB
->removeSuccessor(&CvtMBB
, true);
1688 // If 'true' block has a 'false' successor, add an exit branch to it.
1690 SmallVector
<MachineOperand
, 4> RevCond(CvtBBI
->BrCond
.begin(),
1691 CvtBBI
->BrCond
.end());
1692 if (TII
->reverseBranchCondition(RevCond
))
1693 llvm_unreachable("Unable to reverse branch condition!");
1695 // Update the edge probability for both CvtBBI->FalseBB and NextBBI.
1696 // NewNext = New_Prob(BBI.BB, NextMBB) =
1697 // Prob(BBI.BB, NextMBB) +
1698 // Prob(BBI.BB, CvtMBB) * Prob(CvtMBB, NextMBB)
1699 // NewFalse = New_Prob(BBI.BB, CvtBBI->FalseBB) =
1700 // Prob(BBI.BB, CvtMBB) * Prob(CvtMBB, CvtBBI->FalseBB)
1701 auto NewTrueBB
= getNextBlock(*BBI
.BB
);
1702 auto NewNext
= BBNext
+ BBCvt
* CvtNext
;
1703 auto NewTrueBBIter
= find(BBI
.BB
->successors(), NewTrueBB
);
1704 if (NewTrueBBIter
!= BBI
.BB
->succ_end())
1705 BBI
.BB
->setSuccProbability(NewTrueBBIter
, NewNext
);
1707 auto NewFalse
= BBCvt
* CvtFalse
;
1708 TII
->insertBranch(*BBI
.BB
, CvtBBI
->FalseBB
, nullptr, RevCond
, dl
);
1709 BBI
.BB
->addSuccessor(CvtBBI
->FalseBB
, NewFalse
);
1712 // Merge in the 'false' block if the 'false' block has no other
1713 // predecessors. Otherwise, add an unconditional branch to 'false'.
1714 bool FalseBBDead
= false;
1715 bool IterIfcvt
= true;
1716 bool isFallThrough
= canFallThroughTo(*BBI
.BB
, NextMBB
);
1717 if (!isFallThrough
) {
1718 // Only merge them if the true block does not fallthrough to the false
1719 // block. By not merging them, we make it possible to iteratively
1720 // ifcvt the blocks.
1721 if (!HasEarlyExit
&&
1722 NextMBB
.pred_size() == 1 && !NextBBI
->HasFallThrough
&&
1723 !NextMBB
.hasAddressTaken()) {
1724 MergeBlocks(BBI
, *NextBBI
);
1727 InsertUncondBranch(*BBI
.BB
, NextMBB
, TII
);
1728 BBI
.HasFallThrough
= false;
1730 // Mixed predicated and unpredicated code. This cannot be iteratively
1735 // Update block info. BB can be iteratively if-converted.
1738 InvalidatePreds(*BBI
.BB
);
1739 CvtBBI
->IsDone
= true;
1741 NextBBI
->IsDone
= true;
1743 // FIXME: Must maintain LiveIns.
1747 /// Common code shared between diamond conversions.
1748 /// \p BBI, \p TrueBBI, and \p FalseBBI form the diamond shape.
1749 /// \p NumDups1 - number of shared instructions at the beginning of \p TrueBBI
1751 /// \p NumDups2 - number of shared instructions at the end of \p TrueBBI
1753 /// \p RemoveBranch - Remove the common branch of the two blocks before
1754 /// predicating. Only false for unanalyzable fallthrough
1755 /// cases. The caller will replace the branch if necessary.
1756 /// \p MergeAddEdges - Add successor edges when merging blocks. Only false for
1757 /// unanalyzable fallthrough
1758 bool IfConverter::IfConvertDiamondCommon(
1759 BBInfo
&BBI
, BBInfo
&TrueBBI
, BBInfo
&FalseBBI
,
1760 unsigned NumDups1
, unsigned NumDups2
,
1761 bool TClobbersPred
, bool FClobbersPred
,
1762 bool RemoveBranch
, bool MergeAddEdges
) {
1764 if (TrueBBI
.IsDone
|| FalseBBI
.IsDone
||
1765 TrueBBI
.BB
->pred_size() > 1 || FalseBBI
.BB
->pred_size() > 1) {
1766 // Something has changed. It's no longer safe to predicate these blocks.
1767 BBI
.IsAnalyzed
= false;
1768 TrueBBI
.IsAnalyzed
= false;
1769 FalseBBI
.IsAnalyzed
= false;
1773 if (TrueBBI
.BB
->hasAddressTaken() || FalseBBI
.BB
->hasAddressTaken())
1774 // Conservatively abort if-conversion if either BB has its address taken.
1777 // Put the predicated instructions from the 'true' block before the
1778 // instructions from the 'false' block, unless the true block would clobber
1779 // the predicate, in which case, do the opposite.
1780 BBInfo
*BBI1
= &TrueBBI
;
1781 BBInfo
*BBI2
= &FalseBBI
;
1782 SmallVector
<MachineOperand
, 4> RevCond(BBI
.BrCond
.begin(), BBI
.BrCond
.end());
1783 if (TII
->reverseBranchCondition(RevCond
))
1784 llvm_unreachable("Unable to reverse branch condition!");
1785 SmallVector
<MachineOperand
, 4> *Cond1
= &BBI
.BrCond
;
1786 SmallVector
<MachineOperand
, 4> *Cond2
= &RevCond
;
1788 // Figure out the more profitable ordering.
1789 bool DoSwap
= false;
1790 if (TClobbersPred
&& !FClobbersPred
)
1792 else if (!TClobbersPred
&& !FClobbersPred
) {
1793 if (TrueBBI
.NonPredSize
> FalseBBI
.NonPredSize
)
1795 } else if (TClobbersPred
&& FClobbersPred
)
1796 llvm_unreachable("Predicate info cannot be clobbered by both sides.");
1798 std::swap(BBI1
, BBI2
);
1799 std::swap(Cond1
, Cond2
);
1802 // Remove the conditional branch from entry to the blocks.
1803 BBI
.NonPredSize
-= TII
->removeBranch(*BBI
.BB
);
1805 MachineBasicBlock
&MBB1
= *BBI1
->BB
;
1806 MachineBasicBlock
&MBB2
= *BBI2
->BB
;
1808 // Initialize the Redefs:
1809 // - BB2 live-in regs need implicit uses before being redefined by BB1
1811 // - BB1 live-out regs need implicit uses before being redefined by BB2
1812 // instructions. We start with BB1 live-ins so we have the live-out regs
1813 // after tracking the BB1 instructions.
1815 if (MRI
->tracksLiveness()) {
1816 Redefs
.addLiveInsNoPristines(MBB1
);
1817 Redefs
.addLiveInsNoPristines(MBB2
);
1820 // Remove the duplicated instructions at the beginnings of both paths.
1821 // Skip dbg_value instructions.
1822 MachineBasicBlock::iterator DI1
= MBB1
.getFirstNonDebugInstr(false);
1823 MachineBasicBlock::iterator DI2
= MBB2
.getFirstNonDebugInstr(false);
1824 BBI1
->NonPredSize
-= NumDups1
;
1825 BBI2
->NonPredSize
-= NumDups1
;
1827 // Skip past the dups on each side separately since there may be
1828 // differing dbg_value entries. NumDups1 can include a "return"
1829 // instruction, if it's not marked as "branch".
1830 for (unsigned i
= 0; i
< NumDups1
; ++DI1
) {
1831 if (DI1
== MBB1
.end())
1833 if (!DI1
->isDebugInstr())
1836 while (NumDups1
!= 0) {
1837 // Since this instruction is going to be deleted, update call
1838 // site info state if the instruction is call instruction.
1839 if (DI2
->shouldUpdateCallSiteInfo())
1840 MBB2
.getParent()->eraseCallSiteInfo(&*DI2
);
1843 if (DI2
== MBB2
.end())
1845 if (!DI2
->isDebugInstr())
1849 if (MRI
->tracksLiveness()) {
1850 for (const MachineInstr
&MI
: make_range(MBB1
.begin(), DI1
)) {
1851 SmallVector
<std::pair
<MCPhysReg
, const MachineOperand
*>, 4> Dummy
;
1852 Redefs
.stepForward(MI
, Dummy
);
1856 BBI
.BB
->splice(BBI
.BB
->end(), &MBB1
, MBB1
.begin(), DI1
);
1857 MBB2
.erase(MBB2
.begin(), DI2
);
1859 // The branches have been checked to match, so it is safe to remove the
1860 // branch in BB1 and rely on the copy in BB2. The complication is that
1861 // the blocks may end with a return instruction, which may or may not
1862 // be marked as "branch". If it's not, then it could be included in
1863 // "dups1", leaving the blocks potentially empty after moving the common
1866 // Unanalyzable branches must match exactly. Check that now.
1867 if (!BBI1
->IsBrAnalyzable
)
1868 verifySameBranchInstructions(&MBB1
, &MBB2
);
1870 // Remove duplicated instructions from the tail of MBB1: any branch
1871 // instructions, and the common instructions counted by NumDups2.
1873 while (DI1
!= MBB1
.begin()) {
1874 MachineBasicBlock::iterator Prev
= std::prev(DI1
);
1875 if (!Prev
->isBranch() && !Prev
->isDebugInstr())
1879 for (unsigned i
= 0; i
!= NumDups2
; ) {
1880 // NumDups2 only counted non-dbg_value instructions, so this won't
1881 // run off the head of the list.
1882 assert(DI1
!= MBB1
.begin());
1886 // Since this instruction is going to be deleted, update call
1887 // site info state if the instruction is call instruction.
1888 if (DI1
->shouldUpdateCallSiteInfo())
1889 MBB1
.getParent()->eraseCallSiteInfo(&*DI1
);
1891 // skip dbg_value instructions
1892 if (!DI1
->isDebugInstr())
1895 MBB1
.erase(DI1
, MBB1
.end());
1897 DI2
= BBI2
->BB
->end();
1898 // The branches have been checked to match. Skip over the branch in the false
1899 // block so that we don't try to predicate it.
1901 BBI2
->NonPredSize
-= TII
->removeBranch(*BBI2
->BB
);
1903 // Make DI2 point to the end of the range where the common "tail"
1904 // instructions could be found.
1905 while (DI2
!= MBB2
.begin()) {
1906 MachineBasicBlock::iterator Prev
= std::prev(DI2
);
1907 if (!Prev
->isBranch() && !Prev
->isDebugInstr())
1912 while (NumDups2
!= 0) {
1913 // NumDups2 only counted non-dbg_value instructions, so this won't
1914 // run off the head of the list.
1915 assert(DI2
!= MBB2
.begin());
1917 // skip dbg_value instructions
1918 if (!DI2
->isDebugInstr())
1922 // Remember which registers would later be defined by the false block.
1923 // This allows us not to predicate instructions in the true block that would
1924 // later be re-defined. That is, rather than
1930 SmallSet
<MCPhysReg
, 4> RedefsByFalse
;
1931 SmallSet
<MCPhysReg
, 4> ExtUses
;
1932 if (TII
->isProfitableToUnpredicate(MBB1
, MBB2
)) {
1933 for (const MachineInstr
&FI
: make_range(MBB2
.begin(), DI2
)) {
1934 if (FI
.isDebugInstr())
1936 SmallVector
<MCPhysReg
, 4> Defs
;
1937 for (const MachineOperand
&MO
: FI
.operands()) {
1940 Register Reg
= MO
.getReg();
1944 Defs
.push_back(Reg
);
1945 } else if (!RedefsByFalse
.count(Reg
)) {
1946 // These are defined before ctrl flow reach the 'false' instructions.
1947 // They cannot be modified by the 'true' instructions.
1948 for (MCPhysReg SubReg
: TRI
->subregs_inclusive(Reg
))
1949 ExtUses
.insert(SubReg
);
1953 for (MCPhysReg Reg
: Defs
) {
1954 if (!ExtUses
.count(Reg
)) {
1955 for (MCPhysReg SubReg
: TRI
->subregs_inclusive(Reg
))
1956 RedefsByFalse
.insert(SubReg
);
1962 // Predicate the 'true' block.
1963 PredicateBlock(*BBI1
, MBB1
.end(), *Cond1
, &RedefsByFalse
);
1965 // After predicating BBI1, if there is a predicated terminator in BBI1 and
1966 // a non-predicated in BBI2, then we don't want to predicate the one from
1967 // BBI2. The reason is that if we merged these blocks, we would end up with
1968 // two predicated terminators in the same block.
1969 // Also, if the branches in MBB1 and MBB2 were non-analyzable, then don't
1970 // predicate them either. They were checked to be identical, and so the
1971 // same branch would happen regardless of which path was taken.
1972 if (!MBB2
.empty() && (DI2
== MBB2
.end())) {
1973 MachineBasicBlock::iterator BBI1T
= MBB1
.getFirstTerminator();
1974 MachineBasicBlock::iterator BBI2T
= MBB2
.getFirstTerminator();
1975 bool BB1Predicated
= BBI1T
!= MBB1
.end() && TII
->isPredicated(*BBI1T
);
1976 bool BB2NonPredicated
= BBI2T
!= MBB2
.end() && !TII
->isPredicated(*BBI2T
);
1977 if (BB2NonPredicated
&& (BB1Predicated
|| !BBI2
->IsBrAnalyzable
))
1981 // Predicate the 'false' block.
1982 PredicateBlock(*BBI2
, DI2
, *Cond2
);
1984 // Merge the true block into the entry of the diamond.
1985 MergeBlocks(BBI
, *BBI1
, MergeAddEdges
);
1986 MergeBlocks(BBI
, *BBI2
, MergeAddEdges
);
1990 /// If convert an almost-diamond sub-CFG where the true
1991 /// and false blocks share a common tail.
1992 bool IfConverter::IfConvertForkedDiamond(
1993 BBInfo
&BBI
, IfcvtKind Kind
,
1994 unsigned NumDups1
, unsigned NumDups2
,
1995 bool TClobbersPred
, bool FClobbersPred
) {
1996 BBInfo
&TrueBBI
= BBAnalysis
[BBI
.TrueBB
->getNumber()];
1997 BBInfo
&FalseBBI
= BBAnalysis
[BBI
.FalseBB
->getNumber()];
1999 // Save the debug location for later.
2001 MachineBasicBlock::iterator TIE
= TrueBBI
.BB
->getFirstTerminator();
2002 if (TIE
!= TrueBBI
.BB
->end())
2003 dl
= TIE
->getDebugLoc();
2004 // Removing branches from both blocks is safe, because we have already
2005 // determined that both blocks have the same branch instructions. The branch
2006 // will be added back at the end, unpredicated.
2007 if (!IfConvertDiamondCommon(
2008 BBI
, TrueBBI
, FalseBBI
,
2010 TClobbersPred
, FClobbersPred
,
2011 /* RemoveBranch */ true, /* MergeAddEdges */ true))
2014 // Add back the branch.
2015 // Debug location saved above when removing the branch from BBI2
2016 TII
->insertBranch(*BBI
.BB
, TrueBBI
.TrueBB
, TrueBBI
.FalseBB
,
2017 TrueBBI
.BrCond
, dl
);
2019 // Update block info.
2020 BBI
.IsDone
= TrueBBI
.IsDone
= FalseBBI
.IsDone
= true;
2021 InvalidatePreds(*BBI
.BB
);
2023 // FIXME: Must maintain LiveIns.
2027 /// If convert a diamond sub-CFG.
2028 bool IfConverter::IfConvertDiamond(BBInfo
&BBI
, IfcvtKind Kind
,
2029 unsigned NumDups1
, unsigned NumDups2
,
2030 bool TClobbersPred
, bool FClobbersPred
) {
2031 BBInfo
&TrueBBI
= BBAnalysis
[BBI
.TrueBB
->getNumber()];
2032 BBInfo
&FalseBBI
= BBAnalysis
[BBI
.FalseBB
->getNumber()];
2033 MachineBasicBlock
*TailBB
= TrueBBI
.TrueBB
;
2035 // True block must fall through or end with an unanalyzable terminator.
2037 if (blockAlwaysFallThrough(TrueBBI
))
2038 TailBB
= FalseBBI
.TrueBB
;
2039 assert((TailBB
|| !TrueBBI
.IsBrAnalyzable
) && "Unexpected!");
2042 if (!IfConvertDiamondCommon(
2043 BBI
, TrueBBI
, FalseBBI
,
2045 TClobbersPred
, FClobbersPred
,
2046 /* RemoveBranch */ TrueBBI
.IsBrAnalyzable
,
2047 /* MergeAddEdges */ TailBB
== nullptr))
2050 // If the if-converted block falls through or unconditionally branches into
2051 // the tail block, and the tail block does not have other predecessors, then
2052 // fold the tail block in as well. Otherwise, unless it falls through to the
2053 // tail, add a unconditional branch to it.
2055 // We need to remove the edges to the true and false blocks manually since
2056 // we didn't let IfConvertDiamondCommon update the CFG.
2057 BBI
.BB
->removeSuccessor(TrueBBI
.BB
);
2058 BBI
.BB
->removeSuccessor(FalseBBI
.BB
, true);
2060 BBInfo
&TailBBI
= BBAnalysis
[TailBB
->getNumber()];
2061 bool CanMergeTail
= !TailBBI
.HasFallThrough
&&
2062 !TailBBI
.BB
->hasAddressTaken();
2063 // The if-converted block can still have a predicated terminator
2064 // (e.g. a predicated return). If that is the case, we cannot merge
2065 // it with the tail block.
2066 MachineBasicBlock::const_iterator TI
= BBI
.BB
->getFirstTerminator();
2067 if (TI
!= BBI
.BB
->end() && TII
->isPredicated(*TI
))
2068 CanMergeTail
= false;
2069 // There may still be a fall-through edge from BBI1 or BBI2 to TailBB;
2070 // check if there are any other predecessors besides those.
2071 unsigned NumPreds
= TailBB
->pred_size();
2073 CanMergeTail
= false;
2074 else if (NumPreds
== 1 && CanMergeTail
) {
2075 MachineBasicBlock::pred_iterator PI
= TailBB
->pred_begin();
2076 if (*PI
!= TrueBBI
.BB
&& *PI
!= FalseBBI
.BB
)
2077 CanMergeTail
= false;
2080 MergeBlocks(BBI
, TailBBI
);
2081 TailBBI
.IsDone
= true;
2083 BBI
.BB
->addSuccessor(TailBB
, BranchProbability::getOne());
2084 InsertUncondBranch(*BBI
.BB
, *TailBB
, TII
);
2085 BBI
.HasFallThrough
= false;
2089 // Update block info.
2090 BBI
.IsDone
= TrueBBI
.IsDone
= FalseBBI
.IsDone
= true;
2091 InvalidatePreds(*BBI
.BB
);
2093 // FIXME: Must maintain LiveIns.
2097 static bool MaySpeculate(const MachineInstr
&MI
,
2098 SmallSet
<MCPhysReg
, 4> &LaterRedefs
) {
2099 bool SawStore
= true;
2100 if (!MI
.isSafeToMove(nullptr, SawStore
))
2103 for (const MachineOperand
&MO
: MI
.operands()) {
2106 Register Reg
= MO
.getReg();
2109 if (MO
.isDef() && !LaterRedefs
.count(Reg
))
2116 /// Predicate instructions from the start of the block to the specified end with
2117 /// the specified condition.
2118 void IfConverter::PredicateBlock(BBInfo
&BBI
,
2119 MachineBasicBlock::iterator E
,
2120 SmallVectorImpl
<MachineOperand
> &Cond
,
2121 SmallSet
<MCPhysReg
, 4> *LaterRedefs
) {
2122 bool AnyUnpred
= false;
2123 bool MaySpec
= LaterRedefs
!= nullptr;
2124 for (MachineInstr
&I
: make_range(BBI
.BB
->begin(), E
)) {
2125 if (I
.isDebugInstr() || TII
->isPredicated(I
))
2127 // It may be possible not to predicate an instruction if it's the 'true'
2128 // side of a diamond and the 'false' side may re-define the instruction's
2130 if (MaySpec
&& MaySpeculate(I
, *LaterRedefs
)) {
2134 // If any instruction is predicated, then every instruction after it must
2137 if (!TII
->PredicateInstruction(I
, Cond
)) {
2139 dbgs() << "Unable to predicate " << I
<< "!\n";
2141 llvm_unreachable(nullptr);
2144 // If the predicated instruction now redefines a register as the result of
2145 // if-conversion, add an implicit kill.
2146 UpdatePredRedefs(I
, Redefs
);
2149 BBI
.Predicate
.append(Cond
.begin(), Cond
.end());
2151 BBI
.IsAnalyzed
= false;
2152 BBI
.NonPredSize
= 0;
2159 /// Copy and predicate instructions from source BB to the destination block.
2160 /// Skip end of block branches if IgnoreBr is true.
2161 void IfConverter::CopyAndPredicateBlock(BBInfo
&ToBBI
, BBInfo
&FromBBI
,
2162 SmallVectorImpl
<MachineOperand
> &Cond
,
2164 MachineFunction
&MF
= *ToBBI
.BB
->getParent();
2166 MachineBasicBlock
&FromMBB
= *FromBBI
.BB
;
2167 for (MachineInstr
&I
: FromMBB
) {
2168 // Do not copy the end of the block branches.
2169 if (IgnoreBr
&& I
.isBranch())
2172 MachineInstr
*MI
= MF
.CloneMachineInstr(&I
);
2173 // Make a copy of the call site info.
2174 if (I
.isCandidateForCallSiteEntry())
2175 MF
.copyCallSiteInfo(&I
, MI
);
2177 ToBBI
.BB
->insert(ToBBI
.BB
->end(), MI
);
2178 ToBBI
.NonPredSize
++;
2179 unsigned ExtraPredCost
= TII
->getPredicationCost(I
);
2180 unsigned NumCycles
= SchedModel
.computeInstrLatency(&I
, false);
2182 ToBBI
.ExtraCost
+= NumCycles
-1;
2183 ToBBI
.ExtraCost2
+= ExtraPredCost
;
2185 if (!TII
->isPredicated(I
) && !MI
->isDebugInstr()) {
2186 if (!TII
->PredicateInstruction(*MI
, Cond
)) {
2188 dbgs() << "Unable to predicate " << I
<< "!\n";
2190 llvm_unreachable(nullptr);
2194 // If the predicated instruction now redefines a register as the result of
2195 // if-conversion, add an implicit kill.
2196 UpdatePredRedefs(*MI
, Redefs
);
2200 std::vector
<MachineBasicBlock
*> Succs(FromMBB
.succ_begin(),
2201 FromMBB
.succ_end());
2202 MachineBasicBlock
*NBB
= getNextBlock(FromMBB
);
2203 MachineBasicBlock
*FallThrough
= FromBBI
.HasFallThrough
? NBB
: nullptr;
2205 for (MachineBasicBlock
*Succ
: Succs
) {
2206 // Fallthrough edge can't be transferred.
2207 if (Succ
== FallThrough
)
2209 ToBBI
.BB
->addSuccessor(Succ
);
2213 ToBBI
.Predicate
.append(FromBBI
.Predicate
.begin(), FromBBI
.Predicate
.end());
2214 ToBBI
.Predicate
.append(Cond
.begin(), Cond
.end());
2216 ToBBI
.ClobbersPred
|= FromBBI
.ClobbersPred
;
2217 ToBBI
.IsAnalyzed
= false;
2222 /// Move all instructions from FromBB to the end of ToBB. This will leave
2223 /// FromBB as an empty block, so remove all of its successor edges and move it
2224 /// to the end of the function. If AddEdges is true, i.e., when FromBBI's
2225 /// branch is being moved, add those successor edges to ToBBI and remove the old
2226 /// edge from ToBBI to FromBBI.
2227 void IfConverter::MergeBlocks(BBInfo
&ToBBI
, BBInfo
&FromBBI
, bool AddEdges
) {
2228 MachineBasicBlock
&FromMBB
= *FromBBI
.BB
;
2229 assert(!FromMBB
.hasAddressTaken() &&
2230 "Removing a BB whose address is taken!");
2232 // If we're about to splice an INLINEASM_BR from FromBBI, we need to update
2233 // ToBBI's successor list accordingly.
2234 if (FromMBB
.mayHaveInlineAsmBr())
2235 for (MachineInstr
&MI
: FromMBB
)
2236 if (MI
.getOpcode() == TargetOpcode::INLINEASM_BR
)
2237 for (MachineOperand
&MO
: MI
.operands())
2238 if (MO
.isMBB() && !ToBBI
.BB
->isSuccessor(MO
.getMBB()))
2239 ToBBI
.BB
->addSuccessor(MO
.getMBB(), BranchProbability::getZero());
2241 // In case FromMBB contains terminators (e.g. return instruction),
2242 // first move the non-terminator instructions, then the terminators.
2243 MachineBasicBlock::iterator FromTI
= FromMBB
.getFirstTerminator();
2244 MachineBasicBlock::iterator ToTI
= ToBBI
.BB
->getFirstTerminator();
2245 ToBBI
.BB
->splice(ToTI
, &FromMBB
, FromMBB
.begin(), FromTI
);
2247 // If FromBB has non-predicated terminator we should copy it at the end.
2248 if (FromTI
!= FromMBB
.end() && !TII
->isPredicated(*FromTI
))
2249 ToTI
= ToBBI
.BB
->end();
2250 ToBBI
.BB
->splice(ToTI
, &FromMBB
, FromTI
, FromMBB
.end());
2252 // Force normalizing the successors' probabilities of ToBBI.BB to convert all
2253 // unknown probabilities into known ones.
2254 // FIXME: This usage is too tricky and in the future we would like to
2255 // eliminate all unknown probabilities in MBB.
2256 if (ToBBI
.IsBrAnalyzable
)
2257 ToBBI
.BB
->normalizeSuccProbs();
2259 SmallVector
<MachineBasicBlock
*, 4> FromSuccs(FromMBB
.successors());
2260 MachineBasicBlock
*NBB
= getNextBlock(FromMBB
);
2261 MachineBasicBlock
*FallThrough
= FromBBI
.HasFallThrough
? NBB
: nullptr;
2262 // The edge probability from ToBBI.BB to FromMBB, which is only needed when
2263 // AddEdges is true and FromMBB is a successor of ToBBI.BB.
2264 auto To2FromProb
= BranchProbability::getZero();
2265 if (AddEdges
&& ToBBI
.BB
->isSuccessor(&FromMBB
)) {
2266 // Remove the old edge but remember the edge probability so we can calculate
2267 // the correct weights on the new edges being added further down.
2268 To2FromProb
= MBPI
->getEdgeProbability(ToBBI
.BB
, &FromMBB
);
2269 ToBBI
.BB
->removeSuccessor(&FromMBB
);
2272 for (MachineBasicBlock
*Succ
: FromSuccs
) {
2273 // Fallthrough edge can't be transferred.
2274 if (Succ
== FallThrough
) {
2275 FromMBB
.removeSuccessor(Succ
);
2279 auto NewProb
= BranchProbability::getZero();
2281 // Calculate the edge probability for the edge from ToBBI.BB to Succ,
2282 // which is a portion of the edge probability from FromMBB to Succ. The
2283 // portion ratio is the edge probability from ToBBI.BB to FromMBB (if
2284 // FromBBI is a successor of ToBBI.BB. See comment below for exception).
2285 NewProb
= MBPI
->getEdgeProbability(&FromMBB
, Succ
);
2287 // To2FromProb is 0 when FromMBB is not a successor of ToBBI.BB. This
2288 // only happens when if-converting a diamond CFG and FromMBB is the
2289 // tail BB. In this case FromMBB post-dominates ToBBI.BB and hence we
2290 // could just use the probabilities on FromMBB's out-edges when adding
2292 if (!To2FromProb
.isZero())
2293 NewProb
*= To2FromProb
;
2296 FromMBB
.removeSuccessor(Succ
);
2299 // If the edge from ToBBI.BB to Succ already exists, update the
2300 // probability of this edge by adding NewProb to it. An example is shown
2301 // below, in which A is ToBBI.BB and B is FromMBB. In this case we
2302 // don't have to set C as A's successor as it already is. We only need to
2303 // update the edge probability on A->C. Note that B will not be
2304 // immediately removed from A's successors. It is possible that B->D is
2305 // not removed either if D is a fallthrough of B. Later the edge A->D
2306 // (generated here) and B->D will be combined into one edge. To maintain
2307 // correct edge probability of this combined edge, we need to set the edge
2308 // probability of A->B to zero, which is already done above. The edge
2309 // probability on A->D is calculated by scaling the original probability
2310 // on A->B by the probability of B->D.
2312 // Before ifcvt: After ifcvt (assume B->D is kept):
2321 if (ToBBI
.BB
->isSuccessor(Succ
))
2322 ToBBI
.BB
->setSuccProbability(
2323 find(ToBBI
.BB
->successors(), Succ
),
2324 MBPI
->getEdgeProbability(ToBBI
.BB
, Succ
) + NewProb
);
2326 ToBBI
.BB
->addSuccessor(Succ
, NewProb
);
2330 // Move the now empty FromMBB out of the way to the end of the function so
2331 // it doesn't interfere with fallthrough checks done by canFallThroughTo().
2332 MachineBasicBlock
*Last
= &*FromMBB
.getParent()->rbegin();
2333 if (Last
!= &FromMBB
)
2334 FromMBB
.moveAfter(Last
);
2336 // Normalize the probabilities of ToBBI.BB's successors with all adjustment
2337 // we've done above.
2338 if (ToBBI
.IsBrAnalyzable
&& FromBBI
.IsBrAnalyzable
)
2339 ToBBI
.BB
->normalizeSuccProbs();
2341 ToBBI
.Predicate
.append(FromBBI
.Predicate
.begin(), FromBBI
.Predicate
.end());
2342 FromBBI
.Predicate
.clear();
2344 ToBBI
.NonPredSize
+= FromBBI
.NonPredSize
;
2345 ToBBI
.ExtraCost
+= FromBBI
.ExtraCost
;
2346 ToBBI
.ExtraCost2
+= FromBBI
.ExtraCost2
;
2347 FromBBI
.NonPredSize
= 0;
2348 FromBBI
.ExtraCost
= 0;
2349 FromBBI
.ExtraCost2
= 0;
2351 ToBBI
.ClobbersPred
|= FromBBI
.ClobbersPred
;
2352 ToBBI
.HasFallThrough
= FromBBI
.HasFallThrough
;
2353 ToBBI
.IsAnalyzed
= false;
2354 FromBBI
.IsAnalyzed
= false;
2358 llvm::createIfConverter(std::function
<bool(const MachineFunction
&)> Ftor
) {
2359 return new IfConverter(std::move(Ftor
));