1 //===-- IfConversion.cpp - Machine code if conversion pass. ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the machine instruction level if-conversion pass.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "ifcvt"
15 #include "llvm/Function.h"
16 #include "llvm/CodeGen/Passes.h"
17 #include "llvm/CodeGen/MachineModuleInfo.h"
18 #include "llvm/CodeGen/MachineFunctionPass.h"
19 #include "llvm/Target/TargetInstrInfo.h"
20 #include "llvm/Target/TargetLowering.h"
21 #include "llvm/Target/TargetMachine.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/ADT/DepthFirstIterator.h"
25 #include "llvm/ADT/Statistic.h"
26 #include "llvm/ADT/STLExtras.h"
29 // Hidden options for help debugging.
30 static cl::opt
<int> IfCvtFnStart("ifcvt-fn-start", cl::init(-1), cl::Hidden
);
31 static cl::opt
<int> IfCvtFnStop("ifcvt-fn-stop", cl::init(-1), cl::Hidden
);
32 static cl::opt
<int> IfCvtLimit("ifcvt-limit", cl::init(-1), cl::Hidden
);
33 static cl::opt
<bool> DisableSimple("disable-ifcvt-simple",
34 cl::init(false), cl::Hidden
);
35 static cl::opt
<bool> DisableSimpleF("disable-ifcvt-simple-false",
36 cl::init(false), cl::Hidden
);
37 static cl::opt
<bool> DisableTriangle("disable-ifcvt-triangle",
38 cl::init(false), cl::Hidden
);
39 static cl::opt
<bool> DisableTriangleR("disable-ifcvt-triangle-rev",
40 cl::init(false), cl::Hidden
);
41 static cl::opt
<bool> DisableTriangleF("disable-ifcvt-triangle-false",
42 cl::init(false), cl::Hidden
);
43 static cl::opt
<bool> DisableTriangleFR("disable-ifcvt-triangle-false-rev",
44 cl::init(false), cl::Hidden
);
45 static cl::opt
<bool> DisableDiamond("disable-ifcvt-diamond",
46 cl::init(false), cl::Hidden
);
48 STATISTIC(NumSimple
, "Number of simple if-conversions performed");
49 STATISTIC(NumSimpleFalse
, "Number of simple (F) if-conversions performed");
50 STATISTIC(NumTriangle
, "Number of triangle if-conversions performed");
51 STATISTIC(NumTriangleRev
, "Number of triangle (R) if-conversions performed");
52 STATISTIC(NumTriangleFalse
,"Number of triangle (F) if-conversions performed");
53 STATISTIC(NumTriangleFRev
, "Number of triangle (F/R) if-conversions performed");
54 STATISTIC(NumDiamonds
, "Number of diamond if-conversions performed");
55 STATISTIC(NumIfConvBBs
, "Number of if-converted blocks");
56 STATISTIC(NumDupBBs
, "Number of duplicated blocks");
59 class VISIBILITY_HIDDEN IfConverter
: public MachineFunctionPass
{
61 ICNotClassfied
, // BB data valid, but not classified.
62 ICSimpleFalse
, // Same as ICSimple, but on the false path.
63 ICSimple
, // BB is entry of an one split, no rejoin sub-CFG.
64 ICTriangleFRev
, // Same as ICTriangleFalse, but false path rev condition.
65 ICTriangleRev
, // Same as ICTriangle, but true path rev condition.
66 ICTriangleFalse
, // Same as ICTriangle, but on the false path.
67 ICTriangle
, // BB is entry of a triangle sub-CFG.
68 ICDiamond
// BB is entry of a diamond sub-CFG.
71 /// BBInfo - One per MachineBasicBlock, this is used to cache the result
72 /// if-conversion feasibility analysis. This includes results from
73 /// TargetInstrInfo::AnalyzeBranch() (i.e. TBB, FBB, and Cond), and its
74 /// classification, and common tail block of its successors (if it's a
75 /// diamond shape), its size, whether it's predicable, and whether any
76 /// instruction can clobber the 'would-be' predicate.
78 /// IsDone - True if BB is not to be considered for ifcvt.
79 /// IsBeingAnalyzed - True if BB is currently being analyzed.
80 /// IsAnalyzed - True if BB has been analyzed (info is still valid).
81 /// IsEnqueued - True if BB has been enqueued to be ifcvt'ed.
82 /// IsBrAnalyzable - True if AnalyzeBranch() returns false.
83 /// HasFallThrough - True if BB may fallthrough to the following BB.
84 /// IsUnpredicable - True if BB is known to be unpredicable.
85 /// ClobbersPred - True if BB could modify predicates (e.g. has
87 /// NonPredSize - Number of non-predicated instructions.
88 /// BB - Corresponding MachineBasicBlock.
89 /// TrueBB / FalseBB- See AnalyzeBranch().
90 /// BrCond - Conditions for end of block conditional branches.
91 /// Predicate - Predicate used in the BB.
94 bool IsBeingAnalyzed
: 1;
97 bool IsBrAnalyzable
: 1;
98 bool HasFallThrough
: 1;
99 bool IsUnpredicable
: 1;
100 bool CannotBeCopied
: 1;
101 bool ClobbersPred
: 1;
102 unsigned NonPredSize
;
103 MachineBasicBlock
*BB
;
104 MachineBasicBlock
*TrueBB
;
105 MachineBasicBlock
*FalseBB
;
106 SmallVector
<MachineOperand
, 4> BrCond
;
107 SmallVector
<MachineOperand
, 4> Predicate
;
108 BBInfo() : IsDone(false), IsBeingAnalyzed(false),
109 IsAnalyzed(false), IsEnqueued(false), IsBrAnalyzable(false),
110 HasFallThrough(false), IsUnpredicable(false),
111 CannotBeCopied(false), ClobbersPred(false), NonPredSize(0),
112 BB(0), TrueBB(0), FalseBB(0) {}
115 /// IfcvtToken - Record information about pending if-conversions to attemp:
116 /// BBI - Corresponding BBInfo.
117 /// Kind - Type of block. See IfcvtKind.
118 /// NeedSubsumsion - True if the to be predicated BB has already been
120 /// NumDups - Number of instructions that would be duplicated due
121 /// to this if-conversion. (For diamonds, the number of
122 /// identical instructions at the beginnings of both
124 /// NumDups2 - For diamonds, the number of identical instructions
125 /// at the ends of both paths.
132 IfcvtToken(BBInfo
&b
, IfcvtKind k
, bool s
, unsigned d
, unsigned d2
= 0)
133 : BBI(b
), Kind(k
), NeedSubsumsion(s
), NumDups(d
), NumDups2(d2
) {}
136 /// Roots - Basic blocks that do not have successors. These are the starting
137 /// points of Graph traversal.
138 std::vector
<MachineBasicBlock
*> Roots
;
140 /// BBAnalysis - Results of if-conversion feasibility analysis indexed by
141 /// basic block number.
142 std::vector
<BBInfo
> BBAnalysis
;
144 const TargetLowering
*TLI
;
145 const TargetInstrInfo
*TII
;
149 IfConverter() : MachineFunctionPass(&ID
) {}
151 virtual bool runOnMachineFunction(MachineFunction
&MF
);
152 virtual const char *getPassName() const { return "If Converter"; }
155 bool ReverseBranchCondition(BBInfo
&BBI
);
156 bool ValidSimple(BBInfo
&TrueBBI
, unsigned &Dups
) const;
157 bool ValidTriangle(BBInfo
&TrueBBI
, BBInfo
&FalseBBI
,
158 bool FalseBranch
, unsigned &Dups
) const;
159 bool ValidDiamond(BBInfo
&TrueBBI
, BBInfo
&FalseBBI
,
160 unsigned &Dups1
, unsigned &Dups2
) const;
161 void ScanInstructions(BBInfo
&BBI
);
162 BBInfo
&AnalyzeBlock(MachineBasicBlock
*BB
,
163 std::vector
<IfcvtToken
*> &Tokens
);
164 bool FeasibilityAnalysis(BBInfo
&BBI
, SmallVectorImpl
<MachineOperand
> &Cond
,
165 bool isTriangle
= false, bool RevBranch
= false);
166 bool AnalyzeBlocks(MachineFunction
&MF
,
167 std::vector
<IfcvtToken
*> &Tokens
);
168 void InvalidatePreds(MachineBasicBlock
*BB
);
169 void RemoveExtraEdges(BBInfo
&BBI
);
170 bool IfConvertSimple(BBInfo
&BBI
, IfcvtKind Kind
);
171 bool IfConvertTriangle(BBInfo
&BBI
, IfcvtKind Kind
);
172 bool IfConvertDiamond(BBInfo
&BBI
, IfcvtKind Kind
,
173 unsigned NumDups1
, unsigned NumDups2
);
174 void PredicateBlock(BBInfo
&BBI
,
175 MachineBasicBlock::iterator E
,
176 SmallVectorImpl
<MachineOperand
> &Cond
);
177 void CopyAndPredicateBlock(BBInfo
&ToBBI
, BBInfo
&FromBBI
,
178 SmallVectorImpl
<MachineOperand
> &Cond
,
179 bool IgnoreBr
= false);
180 void MergeBlocks(BBInfo
&ToBBI
, BBInfo
&FromBBI
);
182 bool MeetIfcvtSizeLimit(unsigned Size
) const {
183 return Size
> 0 && Size
<= TLI
->getIfCvtBlockSizeLimit();
186 // blockAlwaysFallThrough - Block ends without a terminator.
187 bool blockAlwaysFallThrough(BBInfo
&BBI
) const {
188 return BBI
.IsBrAnalyzable
&& BBI
.TrueBB
== NULL
;
191 // IfcvtTokenCmp - Used to sort if-conversion candidates.
192 static bool IfcvtTokenCmp(IfcvtToken
*C1
, IfcvtToken
*C2
) {
193 int Incr1
= (C1
->Kind
== ICDiamond
)
194 ? -(int)(C1
->NumDups
+ C1
->NumDups2
) : (int)C1
->NumDups
;
195 int Incr2
= (C2
->Kind
== ICDiamond
)
196 ? -(int)(C2
->NumDups
+ C2
->NumDups2
) : (int)C2
->NumDups
;
199 else if (Incr1
== Incr2
) {
200 // Favors subsumsion.
201 if (C1
->NeedSubsumsion
== false && C2
->NeedSubsumsion
== true)
203 else if (C1
->NeedSubsumsion
== C2
->NeedSubsumsion
) {
204 // Favors diamond over triangle, etc.
205 if ((unsigned)C1
->Kind
< (unsigned)C2
->Kind
)
207 else if (C1
->Kind
== C2
->Kind
)
208 return C1
->BBI
.BB
->getNumber() < C2
->BBI
.BB
->getNumber();
215 char IfConverter::ID
= 0;
218 static RegisterPass
<IfConverter
>
219 X("if-converter", "If Converter");
221 FunctionPass
*llvm::createIfConverterPass() { return new IfConverter(); }
223 bool IfConverter::runOnMachineFunction(MachineFunction
&MF
) {
224 TLI
= MF
.getTarget().getTargetLowering();
225 TII
= MF
.getTarget().getInstrInfo();
226 if (!TII
) return false;
228 static int FnNum
= -1;
229 DOUT
<< "\nIfcvt: function (" << ++FnNum
<< ") \'"
230 << MF
.getFunction()->getName() << "\'";
232 if (FnNum
< IfCvtFnStart
|| (IfCvtFnStop
!= -1 && FnNum
> IfCvtFnStop
)) {
233 DOUT
<< " skipped\n";
239 BBAnalysis
.resize(MF
.getNumBlockIDs());
241 // Look for root nodes, i.e. blocks without successors.
242 for (MachineFunction::iterator I
= MF
.begin(), E
= MF
.end(); I
!= E
; ++I
)
246 std::vector
<IfcvtToken
*> Tokens
;
248 unsigned NumIfCvts
= NumSimple
+ NumSimpleFalse
+ NumTriangle
+
249 NumTriangleRev
+ NumTriangleFalse
+ NumTriangleFRev
+ NumDiamonds
;
250 while (IfCvtLimit
== -1 || (int)NumIfCvts
< IfCvtLimit
) {
251 // Do an intial analysis for each basic block and finding all the potential
252 // candidates to perform if-convesion.
253 bool Change
= AnalyzeBlocks(MF
, Tokens
);
254 while (!Tokens
.empty()) {
255 IfcvtToken
*Token
= Tokens
.back();
257 BBInfo
&BBI
= Token
->BBI
;
258 IfcvtKind Kind
= Token
->Kind
;
259 unsigned NumDups
= Token
->NumDups
;
260 unsigned NumDups2
= Token
->NumDups2
;
264 // If the block has been evicted out of the queue or it has already been
265 // marked dead (due to it being predicated), then skip it.
267 BBI
.IsEnqueued
= false;
271 BBI
.IsEnqueued
= false;
275 default: assert(false && "Unexpected!");
278 case ICSimpleFalse
: {
279 bool isFalse
= Kind
== ICSimpleFalse
;
280 if ((isFalse
&& DisableSimpleF
) || (!isFalse
&& DisableSimple
)) break;
281 DOUT
<< "Ifcvt (Simple" << (Kind
== ICSimpleFalse
? " false" :"")
282 << "): BB#" << BBI
.BB
->getNumber() << " ("
283 << ((Kind
== ICSimpleFalse
)
284 ? BBI
.FalseBB
->getNumber()
285 : BBI
.TrueBB
->getNumber()) << ") ";
286 RetVal
= IfConvertSimple(BBI
, Kind
);
287 DOUT
<< (RetVal
? "succeeded!" : "failed!") << "\n";
289 if (isFalse
) NumSimpleFalse
++;
296 case ICTriangleFalse
:
297 case ICTriangleFRev
: {
298 bool isFalse
= Kind
== ICTriangleFalse
;
299 bool isRev
= (Kind
== ICTriangleRev
|| Kind
== ICTriangleFRev
);
300 if (DisableTriangle
&& !isFalse
&& !isRev
) break;
301 if (DisableTriangleR
&& !isFalse
&& isRev
) break;
302 if (DisableTriangleF
&& isFalse
&& !isRev
) break;
303 if (DisableTriangleFR
&& isFalse
&& isRev
) break;
304 DOUT
<< "Ifcvt (Triangle";
309 DOUT
<< "): BB#" << BBI
.BB
->getNumber() << " (T:"
310 << BBI
.TrueBB
->getNumber() << ",F:"
311 << BBI
.FalseBB
->getNumber() << ") ";
312 RetVal
= IfConvertTriangle(BBI
, Kind
);
313 DOUT
<< (RetVal
? "succeeded!" : "failed!") << "\n";
316 if (isRev
) NumTriangleFRev
++;
317 else NumTriangleFalse
++;
319 if (isRev
) NumTriangleRev
++;
326 if (DisableDiamond
) break;
327 DOUT
<< "Ifcvt (Diamond): BB#" << BBI
.BB
->getNumber() << " (T:"
328 << BBI
.TrueBB
->getNumber() << ",F:"
329 << BBI
.FalseBB
->getNumber() << ") ";
330 RetVal
= IfConvertDiamond(BBI
, Kind
, NumDups
, NumDups2
);
331 DOUT
<< (RetVal
? "succeeded!" : "failed!") << "\n";
332 if (RetVal
) NumDiamonds
++;
339 NumIfCvts
= NumSimple
+ NumSimpleFalse
+ NumTriangle
+ NumTriangleRev
+
340 NumTriangleFalse
+ NumTriangleFRev
+ NumDiamonds
;
341 if (IfCvtLimit
!= -1 && (int)NumIfCvts
>= IfCvtLimit
)
347 MadeChange
|= Change
;
350 // Delete tokens in case of early exit.
351 while (!Tokens
.empty()) {
352 IfcvtToken
*Token
= Tokens
.back();
364 /// findFalseBlock - BB has a fallthrough. Find its 'false' successor given
365 /// its 'true' successor.
366 static MachineBasicBlock
*findFalseBlock(MachineBasicBlock
*BB
,
367 MachineBasicBlock
*TrueBB
) {
368 for (MachineBasicBlock::succ_iterator SI
= BB
->succ_begin(),
369 E
= BB
->succ_end(); SI
!= E
; ++SI
) {
370 MachineBasicBlock
*SuccBB
= *SI
;
371 if (SuccBB
!= TrueBB
)
377 /// ReverseBranchCondition - Reverse the condition of the end of the block
378 /// branchs. Swap block's 'true' and 'false' successors.
379 bool IfConverter::ReverseBranchCondition(BBInfo
&BBI
) {
380 if (!TII
->ReverseBranchCondition(BBI
.BrCond
)) {
381 TII
->RemoveBranch(*BBI
.BB
);
382 TII
->InsertBranch(*BBI
.BB
, BBI
.FalseBB
, BBI
.TrueBB
, BBI
.BrCond
);
383 std::swap(BBI
.TrueBB
, BBI
.FalseBB
);
389 /// getNextBlock - Returns the next block in the function blocks ordering. If
390 /// it is the end, returns NULL.
391 static inline MachineBasicBlock
*getNextBlock(MachineBasicBlock
*BB
) {
392 MachineFunction::iterator I
= BB
;
393 MachineFunction::iterator E
= BB
->getParent()->end();
399 /// ValidSimple - Returns true if the 'true' block (along with its
400 /// predecessor) forms a valid simple shape for ifcvt. It also returns the
401 /// number of instructions that the ifcvt would need to duplicate if performed
403 bool IfConverter::ValidSimple(BBInfo
&TrueBBI
, unsigned &Dups
) const {
405 if (TrueBBI
.IsBeingAnalyzed
|| TrueBBI
.IsDone
)
408 if (TrueBBI
.IsBrAnalyzable
)
411 if (TrueBBI
.BB
->pred_size() > 1) {
412 if (TrueBBI
.CannotBeCopied
||
413 TrueBBI
.NonPredSize
> TLI
->getIfCvtDupBlockSizeLimit())
415 Dups
= TrueBBI
.NonPredSize
;
421 /// ValidTriangle - Returns true if the 'true' and 'false' blocks (along
422 /// with their common predecessor) forms a valid triangle shape for ifcvt.
423 /// If 'FalseBranch' is true, it checks if 'true' block's false branch
424 /// branches to the false branch rather than the other way around. It also
425 /// returns the number of instructions that the ifcvt would need to duplicate
426 /// if performed in 'Dups'.
427 bool IfConverter::ValidTriangle(BBInfo
&TrueBBI
, BBInfo
&FalseBBI
,
428 bool FalseBranch
, unsigned &Dups
) const {
430 if (TrueBBI
.IsBeingAnalyzed
|| TrueBBI
.IsDone
)
433 if (TrueBBI
.BB
->pred_size() > 1) {
434 if (TrueBBI
.CannotBeCopied
)
437 unsigned Size
= TrueBBI
.NonPredSize
;
438 if (TrueBBI
.IsBrAnalyzable
) {
439 if (TrueBBI
.TrueBB
&& TrueBBI
.BrCond
.empty())
440 // End with an unconditional branch. It will be removed.
443 MachineBasicBlock
*FExit
= FalseBranch
444 ? TrueBBI
.TrueBB
: TrueBBI
.FalseBB
;
446 // Require a conditional branch
450 if (Size
> TLI
->getIfCvtDupBlockSizeLimit())
455 MachineBasicBlock
*TExit
= FalseBranch
? TrueBBI
.FalseBB
: TrueBBI
.TrueBB
;
456 if (!TExit
&& blockAlwaysFallThrough(TrueBBI
)) {
457 MachineFunction::iterator I
= TrueBBI
.BB
;
458 if (++I
== TrueBBI
.BB
->getParent()->end())
462 return TExit
&& TExit
== FalseBBI
.BB
;
466 MachineBasicBlock::iterator
firstNonBranchInst(MachineBasicBlock
*BB
,
467 const TargetInstrInfo
*TII
) {
468 MachineBasicBlock::iterator I
= BB
->end();
469 while (I
!= BB
->begin()) {
471 if (!I
->getDesc().isBranch())
477 /// ValidDiamond - Returns true if the 'true' and 'false' blocks (along
478 /// with their common predecessor) forms a valid diamond shape for ifcvt.
479 bool IfConverter::ValidDiamond(BBInfo
&TrueBBI
, BBInfo
&FalseBBI
,
480 unsigned &Dups1
, unsigned &Dups2
) const {
482 if (TrueBBI
.IsBeingAnalyzed
|| TrueBBI
.IsDone
||
483 FalseBBI
.IsBeingAnalyzed
|| FalseBBI
.IsDone
)
486 MachineBasicBlock
*TT
= TrueBBI
.TrueBB
;
487 MachineBasicBlock
*FT
= FalseBBI
.TrueBB
;
489 if (!TT
&& blockAlwaysFallThrough(TrueBBI
))
490 TT
= getNextBlock(TrueBBI
.BB
);
491 if (!FT
&& blockAlwaysFallThrough(FalseBBI
))
492 FT
= getNextBlock(FalseBBI
.BB
);
495 if (TT
== NULL
&& (TrueBBI
.IsBrAnalyzable
|| FalseBBI
.IsBrAnalyzable
))
497 if (TrueBBI
.BB
->pred_size() > 1 || FalseBBI
.BB
->pred_size() > 1)
500 // FIXME: Allow true block to have an early exit?
501 if (TrueBBI
.FalseBB
|| FalseBBI
.FalseBB
||
502 (TrueBBI
.ClobbersPred
&& FalseBBI
.ClobbersPred
))
505 MachineBasicBlock::iterator TI
= TrueBBI
.BB
->begin();
506 MachineBasicBlock::iterator FI
= FalseBBI
.BB
->begin();
507 while (TI
!= TrueBBI
.BB
->end() && FI
!= FalseBBI
.BB
->end()) {
508 if (!TI
->isIdenticalTo(FI
))
515 TI
= firstNonBranchInst(TrueBBI
.BB
, TII
);
516 FI
= firstNonBranchInst(FalseBBI
.BB
, TII
);
517 while (TI
!= TrueBBI
.BB
->begin() && FI
!= FalseBBI
.BB
->begin()) {
518 if (!TI
->isIdenticalTo(FI
))
528 /// ScanInstructions - Scan all the instructions in the block to determine if
529 /// the block is predicable. In most cases, that means all the instructions
530 /// in the block are isPredicable(). Also checks if the block contains any
531 /// instruction which can clobber a predicate (e.g. condition code register).
532 /// If so, the block is not predicable unless it's the last instruction.
533 void IfConverter::ScanInstructions(BBInfo
&BBI
) {
537 bool AlreadyPredicated
= BBI
.Predicate
.size() > 0;
538 // First analyze the end of BB branches.
539 BBI
.TrueBB
= BBI
.FalseBB
= NULL
;
542 !TII
->AnalyzeBranch(*BBI
.BB
, BBI
.TrueBB
, BBI
.FalseBB
, BBI
.BrCond
);
543 BBI
.HasFallThrough
= BBI
.IsBrAnalyzable
&& BBI
.FalseBB
== NULL
;
545 if (BBI
.BrCond
.size()) {
546 // No false branch. This BB must end with a conditional branch and a
549 BBI
.FalseBB
= findFalseBlock(BBI
.BB
, BBI
.TrueBB
);
550 assert(BBI
.FalseBB
&& "Expected to find the fallthrough block!");
553 // Then scan all the instructions.
555 BBI
.ClobbersPred
= false;
556 bool SeenCondBr
= false;
557 for (MachineBasicBlock::iterator I
= BBI
.BB
->begin(), E
= BBI
.BB
->end();
559 const TargetInstrDesc
&TID
= I
->getDesc();
560 if (TID
.isNotDuplicable())
561 BBI
.CannotBeCopied
= true;
563 bool isPredicated
= TII
->isPredicated(I
);
564 bool isCondBr
= BBI
.IsBrAnalyzable
&& TID
.isConditionalBranch();
569 else if (!AlreadyPredicated
) {
570 // FIXME: This instruction is already predicated before the
571 // if-conversion pass. It's probably something like a conditional move.
572 // Mark this block unpredicable for now.
573 BBI
.IsUnpredicable
= true;
579 if (BBI
.ClobbersPred
&& !isPredicated
) {
580 // Predicate modification instruction should end the block (except for
581 // already predicated instructions and end of block branches).
585 // Conditional branches is not predicable. But it may be eliminated.
589 // Predicate may have been modified, the subsequent (currently)
590 // unpredicated instructions cannot be correctly predicated.
591 BBI
.IsUnpredicable
= true;
595 // FIXME: Make use of PredDefs? e.g. ADDC, SUBC sets predicates but are
596 // still potentially predicable.
597 std::vector
<MachineOperand
> PredDefs
;
598 if (TII
->DefinesPredicate(I
, PredDefs
))
599 BBI
.ClobbersPred
= true;
601 if (!TID
.isPredicable()) {
602 BBI
.IsUnpredicable
= true;
608 /// FeasibilityAnalysis - Determine if the block is a suitable candidate to be
609 /// predicated by the specified predicate.
610 bool IfConverter::FeasibilityAnalysis(BBInfo
&BBI
,
611 SmallVectorImpl
<MachineOperand
> &Pred
,
612 bool isTriangle
, bool RevBranch
) {
613 // If the block is dead or unpredicable, then it cannot be predicated.
614 if (BBI
.IsDone
|| BBI
.IsUnpredicable
)
617 // If it is already predicated, check if its predicate subsumes the new
619 if (BBI
.Predicate
.size() && !TII
->SubsumesPredicate(BBI
.Predicate
, Pred
))
622 if (BBI
.BrCond
.size()) {
626 // Test predicate subsumsion.
627 SmallVector
<MachineOperand
, 4> RevPred(Pred
.begin(), Pred
.end());
628 SmallVector
<MachineOperand
, 4> Cond(BBI
.BrCond
.begin(), BBI
.BrCond
.end());
630 if (TII
->ReverseBranchCondition(Cond
))
633 if (TII
->ReverseBranchCondition(RevPred
) ||
634 !TII
->SubsumesPredicate(Cond
, RevPred
))
641 /// AnalyzeBlock - Analyze the structure of the sub-CFG starting from
642 /// the specified block. Record its successors and whether it looks like an
643 /// if-conversion candidate.
644 IfConverter::BBInfo
&IfConverter::AnalyzeBlock(MachineBasicBlock
*BB
,
645 std::vector
<IfcvtToken
*> &Tokens
) {
646 BBInfo
&BBI
= BBAnalysis
[BB
->getNumber()];
648 if (BBI
.IsAnalyzed
|| BBI
.IsBeingAnalyzed
)
652 BBI
.IsBeingAnalyzed
= true;
654 ScanInstructions(BBI
);
656 // Unanalyable or ends with fallthrough or unconditional branch.
657 if (!BBI
.IsBrAnalyzable
|| BBI
.BrCond
.empty()) {
658 BBI
.IsBeingAnalyzed
= false;
659 BBI
.IsAnalyzed
= true;
663 // Do not ifcvt if either path is a back edge to the entry block.
664 if (BBI
.TrueBB
== BB
|| BBI
.FalseBB
== BB
) {
665 BBI
.IsBeingAnalyzed
= false;
666 BBI
.IsAnalyzed
= true;
670 BBInfo
&TrueBBI
= AnalyzeBlock(BBI
.TrueBB
, Tokens
);
671 BBInfo
&FalseBBI
= AnalyzeBlock(BBI
.FalseBB
, Tokens
);
673 if (TrueBBI
.IsDone
&& FalseBBI
.IsDone
) {
674 BBI
.IsBeingAnalyzed
= false;
675 BBI
.IsAnalyzed
= true;
679 SmallVector
<MachineOperand
, 4> RevCond(BBI
.BrCond
.begin(), BBI
.BrCond
.end());
680 bool CanRevCond
= !TII
->ReverseBranchCondition(RevCond
);
684 bool TNeedSub
= TrueBBI
.Predicate
.size() > 0;
685 bool FNeedSub
= FalseBBI
.Predicate
.size() > 0;
686 bool Enqueued
= false;
687 if (CanRevCond
&& ValidDiamond(TrueBBI
, FalseBBI
, Dups
, Dups2
) &&
688 MeetIfcvtSizeLimit(TrueBBI
.NonPredSize
- (Dups
+ Dups2
)) &&
689 MeetIfcvtSizeLimit(FalseBBI
.NonPredSize
- (Dups
+ Dups2
)) &&
690 FeasibilityAnalysis(TrueBBI
, BBI
.BrCond
) &&
691 FeasibilityAnalysis(FalseBBI
, RevCond
)) {
699 // Note TailBB can be empty.
700 Tokens
.push_back(new IfcvtToken(BBI
, ICDiamond
, TNeedSub
|FNeedSub
, Dups
,
705 if (ValidTriangle(TrueBBI
, FalseBBI
, false, Dups
) &&
706 MeetIfcvtSizeLimit(TrueBBI
.NonPredSize
) &&
707 FeasibilityAnalysis(TrueBBI
, BBI
.BrCond
, true)) {
715 Tokens
.push_back(new IfcvtToken(BBI
, ICTriangle
, TNeedSub
, Dups
));
719 if (ValidTriangle(TrueBBI
, FalseBBI
, true, Dups
) &&
720 MeetIfcvtSizeLimit(TrueBBI
.NonPredSize
) &&
721 FeasibilityAnalysis(TrueBBI
, BBI
.BrCond
, true, true)) {
722 Tokens
.push_back(new IfcvtToken(BBI
, ICTriangleRev
, TNeedSub
, Dups
));
726 if (ValidSimple(TrueBBI
, Dups
) &&
727 MeetIfcvtSizeLimit(TrueBBI
.NonPredSize
) &&
728 FeasibilityAnalysis(TrueBBI
, BBI
.BrCond
)) {
729 // Simple (split, no rejoin):
736 Tokens
.push_back(new IfcvtToken(BBI
, ICSimple
, TNeedSub
, Dups
));
741 // Try the other path...
742 if (ValidTriangle(FalseBBI
, TrueBBI
, false, Dups
) &&
743 MeetIfcvtSizeLimit(FalseBBI
.NonPredSize
) &&
744 FeasibilityAnalysis(FalseBBI
, RevCond
, true)) {
745 Tokens
.push_back(new IfcvtToken(BBI
, ICTriangleFalse
, FNeedSub
, Dups
));
749 if (ValidTriangle(FalseBBI
, TrueBBI
, true, Dups
) &&
750 MeetIfcvtSizeLimit(FalseBBI
.NonPredSize
) &&
751 FeasibilityAnalysis(FalseBBI
, RevCond
, true, true)) {
752 Tokens
.push_back(new IfcvtToken(BBI
, ICTriangleFRev
, FNeedSub
, Dups
));
756 if (ValidSimple(FalseBBI
, Dups
) &&
757 MeetIfcvtSizeLimit(FalseBBI
.NonPredSize
) &&
758 FeasibilityAnalysis(FalseBBI
, RevCond
)) {
759 Tokens
.push_back(new IfcvtToken(BBI
, ICSimpleFalse
, FNeedSub
, Dups
));
764 BBI
.IsEnqueued
= Enqueued
;
765 BBI
.IsBeingAnalyzed
= false;
766 BBI
.IsAnalyzed
= true;
770 /// AnalyzeBlocks - Analyze all blocks and find entries for all if-conversion
771 /// candidates. It returns true if any CFG restructuring is done to expose more
772 /// if-conversion opportunities.
773 bool IfConverter::AnalyzeBlocks(MachineFunction
&MF
,
774 std::vector
<IfcvtToken
*> &Tokens
) {
776 std::set
<MachineBasicBlock
*> Visited
;
777 for (unsigned i
= 0, e
= Roots
.size(); i
!= e
; ++i
) {
778 for (idf_ext_iterator
<MachineBasicBlock
*> I
=idf_ext_begin(Roots
[i
],Visited
),
779 E
= idf_ext_end(Roots
[i
], Visited
); I
!= E
; ++I
) {
780 MachineBasicBlock
*BB
= *I
;
781 AnalyzeBlock(BB
, Tokens
);
785 // Sort to favor more complex ifcvt scheme.
786 std::stable_sort(Tokens
.begin(), Tokens
.end(), IfcvtTokenCmp
);
791 /// canFallThroughTo - Returns true either if ToBB is the next block after BB or
792 /// that all the intervening blocks are empty (given BB can fall through to its
794 static bool canFallThroughTo(MachineBasicBlock
*BB
, MachineBasicBlock
*ToBB
) {
795 MachineFunction::iterator I
= BB
;
796 MachineFunction::iterator TI
= ToBB
;
797 MachineFunction::iterator E
= BB
->getParent()->end();
799 if (I
== E
|| !I
->empty())
804 /// InvalidatePreds - Invalidate predecessor BB info so it would be re-analyzed
805 /// to determine if it can be if-converted. If predecessor is already enqueued,
807 void IfConverter::InvalidatePreds(MachineBasicBlock
*BB
) {
808 for (MachineBasicBlock::pred_iterator PI
= BB
->pred_begin(),
809 E
= BB
->pred_end(); PI
!= E
; ++PI
) {
810 BBInfo
&PBBI
= BBAnalysis
[(*PI
)->getNumber()];
811 if (PBBI
.IsDone
|| PBBI
.BB
== BB
)
813 PBBI
.IsAnalyzed
= false;
814 PBBI
.IsEnqueued
= false;
818 /// InsertUncondBranch - Inserts an unconditional branch from BB to ToBB.
820 static void InsertUncondBranch(MachineBasicBlock
*BB
, MachineBasicBlock
*ToBB
,
821 const TargetInstrInfo
*TII
) {
822 SmallVector
<MachineOperand
, 0> NoCond
;
823 TII
->InsertBranch(*BB
, ToBB
, NULL
, NoCond
);
826 /// RemoveExtraEdges - Remove true / false edges if either / both are no longer
828 void IfConverter::RemoveExtraEdges(BBInfo
&BBI
) {
829 MachineBasicBlock
*TBB
= NULL
, *FBB
= NULL
;
830 SmallVector
<MachineOperand
, 4> Cond
;
831 if (!TII
->AnalyzeBranch(*BBI
.BB
, TBB
, FBB
, Cond
))
832 BBI
.BB
->CorrectExtraCFGEdges(TBB
, FBB
, !Cond
.empty());
835 /// IfConvertSimple - If convert a simple (split, no rejoin) sub-CFG.
837 bool IfConverter::IfConvertSimple(BBInfo
&BBI
, IfcvtKind Kind
) {
838 BBInfo
&TrueBBI
= BBAnalysis
[BBI
.TrueBB
->getNumber()];
839 BBInfo
&FalseBBI
= BBAnalysis
[BBI
.FalseBB
->getNumber()];
840 BBInfo
*CvtBBI
= &TrueBBI
;
841 BBInfo
*NextBBI
= &FalseBBI
;
843 SmallVector
<MachineOperand
, 4> Cond(BBI
.BrCond
.begin(), BBI
.BrCond
.end());
844 if (Kind
== ICSimpleFalse
)
845 std::swap(CvtBBI
, NextBBI
);
847 if (CvtBBI
->IsDone
||
848 (CvtBBI
->CannotBeCopied
&& CvtBBI
->BB
->pred_size() > 1)) {
849 // Something has changed. It's no longer safe to predicate this block.
850 BBI
.IsAnalyzed
= false;
851 CvtBBI
->IsAnalyzed
= false;
855 if (Kind
== ICSimpleFalse
)
856 if (TII
->ReverseBranchCondition(Cond
))
857 assert(false && "Unable to reverse branch condition!");
859 if (CvtBBI
->BB
->pred_size() > 1) {
860 BBI
.NonPredSize
-= TII
->RemoveBranch(*BBI
.BB
);
861 // Copy instructions in the true block, predicate them add them to
863 CopyAndPredicateBlock(BBI
, *CvtBBI
, Cond
);
865 PredicateBlock(*CvtBBI
, CvtBBI
->BB
->end(), Cond
);
867 // Merge converted block into entry block.
868 BBI
.NonPredSize
-= TII
->RemoveBranch(*BBI
.BB
);
869 MergeBlocks(BBI
, *CvtBBI
);
872 bool IterIfcvt
= true;
873 if (!canFallThroughTo(BBI
.BB
, NextBBI
->BB
)) {
874 InsertUncondBranch(BBI
.BB
, NextBBI
->BB
, TII
);
875 BBI
.HasFallThrough
= false;
876 // Now ifcvt'd block will look like this:
883 // We cannot further ifcvt this block because the unconditional branch
884 // will have to be predicated on the new condition, that will not be
885 // available if cmp executes.
889 RemoveExtraEdges(BBI
);
891 // Update block info. BB can be iteratively if-converted.
894 InvalidatePreds(BBI
.BB
);
895 CvtBBI
->IsDone
= true;
897 // FIXME: Must maintain LiveIns.
901 /// IfConvertTriangle - If convert a triangle sub-CFG.
903 bool IfConverter::IfConvertTriangle(BBInfo
&BBI
, IfcvtKind Kind
) {
904 BBInfo
&TrueBBI
= BBAnalysis
[BBI
.TrueBB
->getNumber()];
905 BBInfo
&FalseBBI
= BBAnalysis
[BBI
.FalseBB
->getNumber()];
906 BBInfo
*CvtBBI
= &TrueBBI
;
907 BBInfo
*NextBBI
= &FalseBBI
;
909 SmallVector
<MachineOperand
, 4> Cond(BBI
.BrCond
.begin(), BBI
.BrCond
.end());
910 if (Kind
== ICTriangleFalse
|| Kind
== ICTriangleFRev
)
911 std::swap(CvtBBI
, NextBBI
);
913 if (CvtBBI
->IsDone
||
914 (CvtBBI
->CannotBeCopied
&& CvtBBI
->BB
->pred_size() > 1)) {
915 // Something has changed. It's no longer safe to predicate this block.
916 BBI
.IsAnalyzed
= false;
917 CvtBBI
->IsAnalyzed
= false;
921 if (Kind
== ICTriangleFalse
|| Kind
== ICTriangleFRev
)
922 if (TII
->ReverseBranchCondition(Cond
))
923 assert(false && "Unable to reverse branch condition!");
925 if (Kind
== ICTriangleRev
|| Kind
== ICTriangleFRev
) {
926 if (ReverseBranchCondition(*CvtBBI
)) {
927 // BB has been changed, modify its predecessors (except for this
928 // one) so they don't get ifcvt'ed based on bad intel.
929 for (MachineBasicBlock::pred_iterator PI
= CvtBBI
->BB
->pred_begin(),
930 E
= CvtBBI
->BB
->pred_end(); PI
!= E
; ++PI
) {
931 MachineBasicBlock
*PBB
= *PI
;
934 BBInfo
&PBBI
= BBAnalysis
[PBB
->getNumber()];
935 if (PBBI
.IsEnqueued
) {
936 PBBI
.IsAnalyzed
= false;
937 PBBI
.IsEnqueued
= false;
943 bool HasEarlyExit
= CvtBBI
->FalseBB
!= NULL
;
944 bool DupBB
= CvtBBI
->BB
->pred_size() > 1;
946 BBI
.NonPredSize
-= TII
->RemoveBranch(*BBI
.BB
);
947 // Copy instructions in the true block, predicate them add them to
949 CopyAndPredicateBlock(BBI
, *CvtBBI
, Cond
, true);
951 // Predicate the 'true' block after removing its branch.
952 CvtBBI
->NonPredSize
-= TII
->RemoveBranch(*CvtBBI
->BB
);
953 PredicateBlock(*CvtBBI
, CvtBBI
->BB
->end(), Cond
);
957 // Now merge the entry of the triangle with the true block.
958 BBI
.NonPredSize
-= TII
->RemoveBranch(*BBI
.BB
);
959 MergeBlocks(BBI
, *CvtBBI
);
962 // If 'true' block has a 'false' successor, add an exit branch to it.
964 SmallVector
<MachineOperand
, 4> RevCond(CvtBBI
->BrCond
.begin(),
965 CvtBBI
->BrCond
.end());
966 if (TII
->ReverseBranchCondition(RevCond
))
967 assert(false && "Unable to reverse branch condition!");
968 TII
->InsertBranch(*BBI
.BB
, CvtBBI
->FalseBB
, NULL
, RevCond
);
969 BBI
.BB
->addSuccessor(CvtBBI
->FalseBB
);
972 // Merge in the 'false' block if the 'false' block has no other
973 // predecessors. Otherwise, add a unconditional branch from to 'false'.
974 bool FalseBBDead
= false;
975 bool IterIfcvt
= true;
976 bool isFallThrough
= canFallThroughTo(BBI
.BB
, NextBBI
->BB
);
977 if (!isFallThrough
) {
978 // Only merge them if the true block does not fallthrough to the false
979 // block. By not merging them, we make it possible to iteratively
982 NextBBI
->BB
->pred_size() == 1 && !NextBBI
->HasFallThrough
) {
983 MergeBlocks(BBI
, *NextBBI
);
986 InsertUncondBranch(BBI
.BB
, NextBBI
->BB
, TII
);
987 BBI
.HasFallThrough
= false;
989 // Mixed predicated and unpredicated code. This cannot be iteratively
994 RemoveExtraEdges(BBI
);
996 // Update block info. BB can be iteratively if-converted.
999 InvalidatePreds(BBI
.BB
);
1000 CvtBBI
->IsDone
= true;
1002 NextBBI
->IsDone
= true;
1004 // FIXME: Must maintain LiveIns.
1008 /// IfConvertDiamond - If convert a diamond sub-CFG.
1010 bool IfConverter::IfConvertDiamond(BBInfo
&BBI
, IfcvtKind Kind
,
1011 unsigned NumDups1
, unsigned NumDups2
) {
1012 BBInfo
&TrueBBI
= BBAnalysis
[BBI
.TrueBB
->getNumber()];
1013 BBInfo
&FalseBBI
= BBAnalysis
[BBI
.FalseBB
->getNumber()];
1014 MachineBasicBlock
*TailBB
= TrueBBI
.TrueBB
;
1015 // True block must fall through or ended with unanalyzable terminator.
1017 if (blockAlwaysFallThrough(TrueBBI
))
1018 TailBB
= FalseBBI
.TrueBB
;
1019 assert((TailBB
|| !TrueBBI
.IsBrAnalyzable
) && "Unexpected!");
1022 if (TrueBBI
.IsDone
|| FalseBBI
.IsDone
||
1023 TrueBBI
.BB
->pred_size() > 1 ||
1024 FalseBBI
.BB
->pred_size() > 1) {
1025 // Something has changed. It's no longer safe to predicate these blocks.
1026 BBI
.IsAnalyzed
= false;
1027 TrueBBI
.IsAnalyzed
= false;
1028 FalseBBI
.IsAnalyzed
= false;
1032 // Merge the 'true' and 'false' blocks by copying the instructions
1033 // from the 'false' block to the 'true' block. That is, unless the true
1034 // block would clobber the predicate, in that case, do the opposite.
1035 BBInfo
*BBI1
= &TrueBBI
;
1036 BBInfo
*BBI2
= &FalseBBI
;
1037 SmallVector
<MachineOperand
, 4> RevCond(BBI
.BrCond
.begin(), BBI
.BrCond
.end());
1038 if (TII
->ReverseBranchCondition(RevCond
))
1039 assert(false && "Unable to reverse branch condition!");
1040 SmallVector
<MachineOperand
, 4> *Cond1
= &BBI
.BrCond
;
1041 SmallVector
<MachineOperand
, 4> *Cond2
= &RevCond
;
1043 // Figure out the more profitable ordering.
1044 bool DoSwap
= false;
1045 if (TrueBBI
.ClobbersPred
&& !FalseBBI
.ClobbersPred
)
1047 else if (TrueBBI
.ClobbersPred
== FalseBBI
.ClobbersPred
) {
1048 if (TrueBBI
.NonPredSize
> FalseBBI
.NonPredSize
)
1052 std::swap(BBI1
, BBI2
);
1053 std::swap(Cond1
, Cond2
);
1056 // Remove the conditional branch from entry to the blocks.
1057 BBI
.NonPredSize
-= TII
->RemoveBranch(*BBI
.BB
);
1059 // Remove the duplicated instructions at the beginnings of both paths.
1060 MachineBasicBlock::iterator DI1
= BBI1
->BB
->begin();
1061 MachineBasicBlock::iterator DI2
= BBI2
->BB
->begin();
1062 BBI1
->NonPredSize
-= NumDups1
;
1063 BBI2
->NonPredSize
-= NumDups1
;
1064 while (NumDups1
!= 0) {
1069 BBI
.BB
->splice(BBI
.BB
->end(), BBI1
->BB
, BBI1
->BB
->begin(), DI1
);
1070 BBI2
->BB
->erase(BBI2
->BB
->begin(), DI2
);
1072 // Predicate the 'true' block after removing its branch.
1073 BBI1
->NonPredSize
-= TII
->RemoveBranch(*BBI1
->BB
);
1074 DI1
= BBI1
->BB
->end();
1075 for (unsigned i
= 0; i
!= NumDups2
; ++i
)
1077 BBI1
->BB
->erase(DI1
, BBI1
->BB
->end());
1078 PredicateBlock(*BBI1
, BBI1
->BB
->end(), *Cond1
);
1080 // Predicate the 'false' block.
1081 BBI2
->NonPredSize
-= TII
->RemoveBranch(*BBI2
->BB
);
1082 DI2
= BBI2
->BB
->end();
1083 while (NumDups2
!= 0) {
1087 PredicateBlock(*BBI2
, DI2
, *Cond2
);
1089 // Merge the true block into the entry of the diamond.
1090 MergeBlocks(BBI
, *BBI1
);
1091 MergeBlocks(BBI
, *BBI2
);
1093 // If the if-converted block fallthrough or unconditionally branch into the
1094 // tail block, and the tail block does not have other predecessors, then
1095 // fold the tail block in as well. Otherwise, unless it falls through to the
1096 // tail, add a unconditional branch to it.
1098 BBInfo TailBBI
= BBAnalysis
[TailBB
->getNumber()];
1099 if (TailBB
->pred_size() == 1 && !TailBBI
.HasFallThrough
) {
1100 BBI
.NonPredSize
-= TII
->RemoveBranch(*BBI
.BB
);
1101 MergeBlocks(BBI
, TailBBI
);
1102 TailBBI
.IsDone
= true;
1104 InsertUncondBranch(BBI
.BB
, TailBB
, TII
);
1105 BBI
.HasFallThrough
= false;
1109 RemoveExtraEdges(BBI
);
1111 // Update block info.
1112 BBI
.IsDone
= TrueBBI
.IsDone
= FalseBBI
.IsDone
= true;
1113 InvalidatePreds(BBI
.BB
);
1115 // FIXME: Must maintain LiveIns.
1119 /// PredicateBlock - Predicate instructions from the start of the block to the
1120 /// specified end with the specified condition.
1121 void IfConverter::PredicateBlock(BBInfo
&BBI
,
1122 MachineBasicBlock::iterator E
,
1123 SmallVectorImpl
<MachineOperand
> &Cond
) {
1124 for (MachineBasicBlock::iterator I
= BBI
.BB
->begin(); I
!= E
; ++I
) {
1125 if (TII
->isPredicated(I
))
1127 if (!TII
->PredicateInstruction(I
, Cond
)) {
1128 cerr
<< "Unable to predicate " << *I
<< "!\n";
1133 std::copy(Cond
.begin(), Cond
.end(), std::back_inserter(BBI
.Predicate
));
1135 BBI
.IsAnalyzed
= false;
1136 BBI
.NonPredSize
= 0;
1141 /// CopyAndPredicateBlock - Copy and predicate instructions from source BB to
1142 /// the destination block. Skip end of block branches if IgnoreBr is true.
1143 void IfConverter::CopyAndPredicateBlock(BBInfo
&ToBBI
, BBInfo
&FromBBI
,
1144 SmallVectorImpl
<MachineOperand
> &Cond
,
1146 MachineFunction
&MF
= *ToBBI
.BB
->getParent();
1148 for (MachineBasicBlock::iterator I
= FromBBI
.BB
->begin(),
1149 E
= FromBBI
.BB
->end(); I
!= E
; ++I
) {
1150 const TargetInstrDesc
&TID
= I
->getDesc();
1151 bool isPredicated
= TII
->isPredicated(I
);
1152 // Do not copy the end of the block branches.
1153 if (IgnoreBr
&& !isPredicated
&& TID
.isBranch())
1156 MachineInstr
*MI
= MF
.CloneMachineInstr(I
);
1157 ToBBI
.BB
->insert(ToBBI
.BB
->end(), MI
);
1158 ToBBI
.NonPredSize
++;
1161 if (!TII
->PredicateInstruction(MI
, Cond
)) {
1162 cerr
<< "Unable to predicate " << *MI
<< "!\n";
1167 std::vector
<MachineBasicBlock
*> Succs(FromBBI
.BB
->succ_begin(),
1168 FromBBI
.BB
->succ_end());
1169 MachineBasicBlock
*NBB
= getNextBlock(FromBBI
.BB
);
1170 MachineBasicBlock
*FallThrough
= FromBBI
.HasFallThrough
? NBB
: NULL
;
1172 for (unsigned i
= 0, e
= Succs
.size(); i
!= e
; ++i
) {
1173 MachineBasicBlock
*Succ
= Succs
[i
];
1174 // Fallthrough edge can't be transferred.
1175 if (Succ
== FallThrough
)
1177 if (!ToBBI
.BB
->isSuccessor(Succ
))
1178 ToBBI
.BB
->addSuccessor(Succ
);
1181 std::copy(FromBBI
.Predicate
.begin(), FromBBI
.Predicate
.end(),
1182 std::back_inserter(ToBBI
.Predicate
));
1183 std::copy(Cond
.begin(), Cond
.end(), std::back_inserter(ToBBI
.Predicate
));
1185 ToBBI
.ClobbersPred
|= FromBBI
.ClobbersPred
;
1186 ToBBI
.IsAnalyzed
= false;
1191 /// MergeBlocks - Move all instructions from FromBB to the end of ToBB.
1193 void IfConverter::MergeBlocks(BBInfo
&ToBBI
, BBInfo
&FromBBI
) {
1194 ToBBI
.BB
->splice(ToBBI
.BB
->end(),
1195 FromBBI
.BB
, FromBBI
.BB
->begin(), FromBBI
.BB
->end());
1197 // Redirect all branches to FromBB to ToBB.
1198 std::vector
<MachineBasicBlock
*> Preds(FromBBI
.BB
->pred_begin(),
1199 FromBBI
.BB
->pred_end());
1200 for (unsigned i
= 0, e
= Preds
.size(); i
!= e
; ++i
) {
1201 MachineBasicBlock
*Pred
= Preds
[i
];
1202 if (Pred
== ToBBI
.BB
)
1204 Pred
->ReplaceUsesOfBlockWith(FromBBI
.BB
, ToBBI
.BB
);
1207 std::vector
<MachineBasicBlock
*> Succs(FromBBI
.BB
->succ_begin(),
1208 FromBBI
.BB
->succ_end());
1209 MachineBasicBlock
*NBB
= getNextBlock(FromBBI
.BB
);
1210 MachineBasicBlock
*FallThrough
= FromBBI
.HasFallThrough
? NBB
: NULL
;
1212 for (unsigned i
= 0, e
= Succs
.size(); i
!= e
; ++i
) {
1213 MachineBasicBlock
*Succ
= Succs
[i
];
1214 // Fallthrough edge can't be transferred.
1215 if (Succ
== FallThrough
)
1217 FromBBI
.BB
->removeSuccessor(Succ
);
1218 if (!ToBBI
.BB
->isSuccessor(Succ
))
1219 ToBBI
.BB
->addSuccessor(Succ
);
1222 // Now FromBBI always fall through to the next block!
1223 if (NBB
&& !FromBBI
.BB
->isSuccessor(NBB
))
1224 FromBBI
.BB
->addSuccessor(NBB
);
1226 std::copy(FromBBI
.Predicate
.begin(), FromBBI
.Predicate
.end(),
1227 std::back_inserter(ToBBI
.Predicate
));
1228 FromBBI
.Predicate
.clear();
1230 ToBBI
.NonPredSize
+= FromBBI
.NonPredSize
;
1231 FromBBI
.NonPredSize
= 0;
1233 ToBBI
.ClobbersPred
|= FromBBI
.ClobbersPred
;
1234 ToBBI
.HasFallThrough
= FromBBI
.HasFallThrough
;
1235 ToBBI
.IsAnalyzed
= false;
1236 FromBBI
.IsAnalyzed
= false;