1//===- IfConversion.cpp - Machine code if conversion pass -----------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the machine instruction level if-conversion pass, which
10// tries to convert conditional branches into predicated instructions.
11//
12//===----------------------------------------------------------------------===//
13
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/Pass.h"
42#include "llvm/Support/BranchProbability.h"
43#include "llvm/Support/CommandLine.h"
44#include "llvm/Support/Debug.h"
45#include "llvm/Support/ErrorHandling.h"
46#include "llvm/Support/raw_ostream.h"
47#include <algorithm>
48#include <cassert>
49#include <functional>
50#include <iterator>
51#include <memory>
52#include <utility>
53#include <vector>
54
55using namespace llvm;
56
57#define DEBUG_TYPE "if-converter"
58
59// Hidden options for help debugging.
60static cl::opt<int> IfCvtFnStart("ifcvt-fn-start", cl::init(Val: -1), cl::Hidden);
61static cl::opt<int> IfCvtFnStop("ifcvt-fn-stop", cl::init(Val: -1), cl::Hidden);
62static cl::opt<int> IfCvtLimit("ifcvt-limit", cl::init(Val: -1), cl::Hidden);
63static cl::opt<bool> DisableSimple("disable-ifcvt-simple",
64 cl::init(Val: false), cl::Hidden);
65static cl::opt<bool> DisableSimpleF("disable-ifcvt-simple-false",
66 cl::init(Val: false), cl::Hidden);
67static cl::opt<bool> DisableTriangle("disable-ifcvt-triangle",
68 cl::init(Val: false), cl::Hidden);
69static cl::opt<bool> DisableTriangleR("disable-ifcvt-triangle-rev",
70 cl::init(Val: false), cl::Hidden);
71static cl::opt<bool> DisableTriangleF("disable-ifcvt-triangle-false",
72 cl::init(Val: false), cl::Hidden);
73static cl::opt<bool> DisableDiamond("disable-ifcvt-diamond",
74 cl::init(Val: false), cl::Hidden);
75static cl::opt<bool> DisableForkedDiamond("disable-ifcvt-forked-diamond",
76 cl::init(Val: false), cl::Hidden);
77static cl::opt<bool> IfCvtBranchFold("ifcvt-branch-fold",
78 cl::init(Val: true), cl::Hidden);
79
80STATISTIC(NumSimple, "Number of simple if-conversions performed");
81STATISTIC(NumSimpleFalse, "Number of simple (F) if-conversions performed");
82STATISTIC(NumTriangle, "Number of triangle if-conversions performed");
83STATISTIC(NumTriangleRev, "Number of triangle (R) if-conversions performed");
84STATISTIC(NumTriangleFalse,"Number of triangle (F) if-conversions performed");
85STATISTIC(NumTriangleFRev, "Number of triangle (F/R) if-conversions performed");
86STATISTIC(NumDiamonds, "Number of diamond if-conversions performed");
87STATISTIC(NumForkedDiamonds, "Number of forked-diamond if-conversions performed");
88STATISTIC(NumIfConvBBs, "Number of if-converted blocks");
89STATISTIC(NumDupBBs, "Number of duplicated blocks");
90STATISTIC(NumUnpred, "Number of true blocks of diamonds unpredicated");
91
92namespace {
93
94 class IfConverter : public MachineFunctionPass {
95 enum IfcvtKind {
96 ICNotClassfied, // BB data valid, but not classified.
97 ICSimpleFalse, // Same as ICSimple, but on the false path.
98 ICSimple, // BB is entry of an one split, no rejoin sub-CFG.
99 ICTriangleFRev, // Same as ICTriangleFalse, but false path rev condition.
100 ICTriangleRev, // Same as ICTriangle, but true path rev condition.
101 ICTriangleFalse, // Same as ICTriangle, but on the false path.
102 ICTriangle, // BB is entry of a triangle sub-CFG.
103 ICDiamond, // BB is entry of a diamond sub-CFG.
104 ICForkedDiamond // BB is entry of an almost diamond sub-CFG, with a
105 // common tail that can be shared.
106 };
107
108 /// One per MachineBasicBlock, this is used to cache the result
109 /// if-conversion feasibility analysis. This includes results from
110 /// TargetInstrInfo::analyzeBranch() (i.e. TBB, FBB, and Cond), and its
111 /// classification, and common tail block of its successors (if it's a
112 /// diamond shape), its size, whether it's predicable, and whether any
113 /// instruction can clobber the 'would-be' predicate.
114 ///
115 /// IsDone - True if BB is not to be considered for ifcvt.
116 /// IsBeingAnalyzed - True if BB is currently being analyzed.
117 /// IsAnalyzed - True if BB has been analyzed (info is still valid).
118 /// IsEnqueued - True if BB has been enqueued to be ifcvt'ed.
119 /// IsBrAnalyzable - True if analyzeBranch() returns false.
120 /// HasFallThrough - True if BB has fallthrough to the following BB.
121 /// Note that BB may have a fallthrough if both
122 /// !HasFallThrough and !IsBrAnalyzable is true. Also note
123 /// that blockNeverFallThrough() can be used to prove that
124 /// there is no fall through.
125 /// IsUnpredicable - True if BB is known to be unpredicable.
126 /// ClobbersPred - True if BB could modify predicates (e.g. has
127 /// cmp, call, etc.)
128 /// NonPredSize - Number of non-predicated instructions.
129 /// ExtraCost - Extra cost for multi-cycle instructions.
130 /// ExtraCost2 - Some instructions are slower when predicated
131 /// BB - Corresponding MachineBasicBlock.
132 /// TrueBB / FalseBB- See analyzeBranch(), but note that FalseBB can be set
133 /// by AnalyzeBranches even if there is a fallthrough. So
134 /// it doesn't correspond exactly to the result from
135 /// TTI::analyzeBranch.
136 /// BrCond - Conditions for end of block conditional branches.
137 /// Predicate - Predicate used in the BB.
138 struct BBInfo {
139 bool IsDone : 1;
140 bool IsBeingAnalyzed : 1;
141 bool IsAnalyzed : 1;
142 bool IsEnqueued : 1;
143 bool IsBrAnalyzable : 1;
144 bool IsBrReversible : 1;
145 bool HasFallThrough : 1;
146 bool IsUnpredicable : 1;
147 bool CannotBeCopied : 1;
148 bool ClobbersPred : 1;
149 unsigned NonPredSize = 0;
150 unsigned ExtraCost = 0;
151 unsigned ExtraCost2 = 0;
152 MachineBasicBlock *BB = nullptr;
153 MachineBasicBlock *TrueBB = nullptr;
154 MachineBasicBlock *FalseBB = nullptr;
155 SmallVector<MachineOperand, 4> BrCond;
156 SmallVector<MachineOperand, 4> Predicate;
157
158 BBInfo() : IsDone(false), IsBeingAnalyzed(false),
159 IsAnalyzed(false), IsEnqueued(false), IsBrAnalyzable(false),
160 IsBrReversible(false), HasFallThrough(false),
161 IsUnpredicable(false), CannotBeCopied(false),
162 ClobbersPred(false) {}
163 };
164
165 /// Record information about pending if-conversions to attempt:
166 /// BBI - Corresponding BBInfo.
167 /// Kind - Type of block. See IfcvtKind.
168 /// NeedSubsumption - True if the to-be-predicated BB has already been
169 /// predicated.
170 /// NumDups - Number of instructions that would be duplicated due
171 /// to this if-conversion. (For diamonds, the number of
172 /// identical instructions at the beginnings of both
173 /// paths).
174 /// NumDups2 - For diamonds, the number of identical instructions
175 /// at the ends of both paths.
176 struct IfcvtToken {
177 BBInfo &BBI;
178 IfcvtKind Kind;
179 unsigned NumDups;
180 unsigned NumDups2;
181 bool NeedSubsumption : 1;
182 bool TClobbersPred : 1;
183 bool FClobbersPred : 1;
184
185 IfcvtToken(BBInfo &b, IfcvtKind k, bool s, unsigned d, unsigned d2 = 0,
186 bool tc = false, bool fc = false)
187 : BBI(b), Kind(k), NumDups(d), NumDups2(d2), NeedSubsumption(s),
188 TClobbersPred(tc), FClobbersPred(fc) {}
189 };
190
191 /// Results of if-conversion feasibility analysis indexed by basic block
192 /// number.
193 std::vector<BBInfo> BBAnalysis;
194 TargetSchedModel SchedModel;
195
196 const TargetLoweringBase *TLI = nullptr;
197 const TargetInstrInfo *TII = nullptr;
198 const TargetRegisterInfo *TRI = nullptr;
199 const MachineBranchProbabilityInfo *MBPI = nullptr;
200 MachineRegisterInfo *MRI = nullptr;
201
202 LivePhysRegs Redefs;
203
204 bool PreRegAlloc = true;
205 bool MadeChange = false;
206 int FnNum = -1;
207 std::function<bool(const MachineFunction &)> PredicateFtor;
208
209 public:
210 static char ID;
211
212 IfConverter(std::function<bool(const MachineFunction &)> Ftor = nullptr)
213 : MachineFunctionPass(ID), PredicateFtor(std::move(Ftor)) {}
214
215 void getAnalysisUsage(AnalysisUsage &AU) const override {
216 AU.addRequired<MachineBlockFrequencyInfoWrapperPass>();
217 AU.addRequired<MachineBranchProbabilityInfoWrapperPass>();
218 AU.addRequired<ProfileSummaryInfoWrapperPass>();
219 MachineFunctionPass::getAnalysisUsage(AU);
220 }
221
222 bool runOnMachineFunction(MachineFunction &MF) override;
223
224 MachineFunctionProperties getRequiredProperties() const override {
225 return MachineFunctionProperties().setNoVRegs();
226 }
227
228 private:
229 bool reverseBranchCondition(BBInfo &BBI) const;
230 bool ValidSimple(BBInfo &TrueBBI, unsigned &Dups,
231 BranchProbability Prediction) const;
232 bool ValidTriangle(BBInfo &TrueBBI, BBInfo &FalseBBI,
233 bool FalseBranch, unsigned &Dups,
234 BranchProbability Prediction) const;
235 bool CountDuplicatedInstructions(
236 MachineBasicBlock::iterator &TIB, MachineBasicBlock::iterator &FIB,
237 MachineBasicBlock::iterator &TIE, MachineBasicBlock::iterator &FIE,
238 unsigned &Dups1, unsigned &Dups2,
239 MachineBasicBlock &TBB, MachineBasicBlock &FBB,
240 bool SkipUnconditionalBranches) const;
241 bool ValidDiamond(BBInfo &TrueBBI, BBInfo &FalseBBI,
242 unsigned &Dups1, unsigned &Dups2,
243 BBInfo &TrueBBICalc, BBInfo &FalseBBICalc) const;
244 bool ValidForkedDiamond(BBInfo &TrueBBI, BBInfo &FalseBBI,
245 unsigned &Dups1, unsigned &Dups2,
246 BBInfo &TrueBBICalc, BBInfo &FalseBBICalc) const;
247 void AnalyzeBranches(BBInfo &BBI);
248 void ScanInstructions(BBInfo &BBI,
249 MachineBasicBlock::iterator &Begin,
250 MachineBasicBlock::iterator &End,
251 bool BranchUnpredicable = false) const;
252 bool RescanInstructions(
253 MachineBasicBlock::iterator &TIB, MachineBasicBlock::iterator &FIB,
254 MachineBasicBlock::iterator &TIE, MachineBasicBlock::iterator &FIE,
255 BBInfo &TrueBBI, BBInfo &FalseBBI) const;
256 void AnalyzeBlock(MachineBasicBlock &MBB,
257 std::vector<std::unique_ptr<IfcvtToken>> &Tokens);
258 bool FeasibilityAnalysis(BBInfo &BBI, SmallVectorImpl<MachineOperand> &Pred,
259 bool isTriangle = false, bool RevBranch = false,
260 bool hasCommonTail = false);
261 void AnalyzeBlocks(MachineFunction &MF,
262 std::vector<std::unique_ptr<IfcvtToken>> &Tokens);
263 void InvalidatePreds(MachineBasicBlock &MBB);
264 bool IfConvertSimple(BBInfo &BBI, IfcvtKind Kind);
265 bool IfConvertTriangle(BBInfo &BBI, IfcvtKind Kind);
266 bool IfConvertDiamondCommon(BBInfo &BBI, BBInfo &TrueBBI, BBInfo &FalseBBI,
267 unsigned NumDups1, unsigned NumDups2,
268 bool TClobbersPred, bool FClobbersPred,
269 bool RemoveBranch, bool MergeAddEdges);
270 bool IfConvertDiamond(BBInfo &BBI, IfcvtKind Kind,
271 unsigned NumDups1, unsigned NumDups2,
272 bool TClobbers, bool FClobbers);
273 bool IfConvertForkedDiamond(BBInfo &BBI, IfcvtKind Kind,
274 unsigned NumDups1, unsigned NumDups2,
275 bool TClobbers, bool FClobbers);
276 void PredicateBlock(BBInfo &BBI, MachineBasicBlock::iterator E,
277 SmallVectorImpl<MachineOperand> &Cond,
278 SmallSet<MCRegister, 4> *LaterRedefs = nullptr);
279 void CopyAndPredicateBlock(BBInfo &ToBBI, BBInfo &FromBBI,
280 SmallVectorImpl<MachineOperand> &Cond,
281 bool IgnoreBr = false);
282 void MergeBlocks(BBInfo &ToBBI, BBInfo &FromBBI, bool AddEdges = true);
283
284 bool MeetIfcvtSizeLimit(MachineBasicBlock &BB,
285 unsigned Cycle, unsigned Extra,
286 BranchProbability Prediction) const {
287 return Cycle > 0 && TII->isProfitableToIfCvt(MBB&: BB, NumCycles: Cycle, ExtraPredCycles: Extra,
288 Probability: Prediction);
289 }
290
291 bool MeetIfcvtSizeLimit(BBInfo &TBBInfo, BBInfo &FBBInfo,
292 MachineBasicBlock &CommBB, unsigned Dups,
293 BranchProbability Prediction, bool Forked) const {
294 const MachineFunction &MF = *TBBInfo.BB->getParent();
295 if (MF.getFunction().hasMinSize()) {
296 MachineBasicBlock::iterator TIB = TBBInfo.BB->begin();
297 MachineBasicBlock::iterator FIB = FBBInfo.BB->begin();
298 MachineBasicBlock::iterator TIE = TBBInfo.BB->end();
299 MachineBasicBlock::iterator FIE = FBBInfo.BB->end();
300
301 unsigned Dups1 = 0, Dups2 = 0;
302 if (!CountDuplicatedInstructions(TIB, FIB, TIE, FIE, Dups1, Dups2,
303 TBB&: *TBBInfo.BB, FBB&: *FBBInfo.BB,
304 /*SkipUnconditionalBranches*/ true))
305 llvm_unreachable("should already have been checked by ValidDiamond");
306
307 unsigned BranchBytes = 0;
308 unsigned CommonBytes = 0;
309
310 // Count common instructions at the start of the true and false blocks.
311 for (auto &I : make_range(x: TBBInfo.BB->begin(), y: TIB)) {
312 LLVM_DEBUG(dbgs() << "Common inst: " << I);
313 CommonBytes += TII->getInstSizeInBytes(MI: I);
314 }
315 for (auto &I : make_range(x: FBBInfo.BB->begin(), y: FIB)) {
316 LLVM_DEBUG(dbgs() << "Common inst: " << I);
317 CommonBytes += TII->getInstSizeInBytes(MI: I);
318 }
319
320 // Count instructions at the end of the true and false blocks, after
321 // the ones we plan to predicate. Analyzable branches will be removed
322 // (unless this is a forked diamond), and all other instructions are
323 // common between the two blocks.
324 for (auto &I : make_range(x: TIE, y: TBBInfo.BB->end())) {
325 if (I.isBranch() && TBBInfo.IsBrAnalyzable && !Forked) {
326 LLVM_DEBUG(dbgs() << "Saving branch: " << I);
327 BranchBytes += TII->predictBranchSizeForIfCvt(MI&: I);
328 } else {
329 LLVM_DEBUG(dbgs() << "Common inst: " << I);
330 CommonBytes += TII->getInstSizeInBytes(MI: I);
331 }
332 }
333 for (auto &I : make_range(x: FIE, y: FBBInfo.BB->end())) {
334 if (I.isBranch() && FBBInfo.IsBrAnalyzable && !Forked) {
335 LLVM_DEBUG(dbgs() << "Saving branch: " << I);
336 BranchBytes += TII->predictBranchSizeForIfCvt(MI&: I);
337 } else {
338 LLVM_DEBUG(dbgs() << "Common inst: " << I);
339 CommonBytes += TII->getInstSizeInBytes(MI: I);
340 }
341 }
342 for (auto &I : CommBB.terminators()) {
343 if (I.isBranch()) {
344 LLVM_DEBUG(dbgs() << "Saving branch: " << I);
345 BranchBytes += TII->predictBranchSizeForIfCvt(MI&: I);
346 }
347 }
348
349 // The common instructions in one branch will be eliminated, halving
350 // their code size.
351 CommonBytes /= 2;
352
353 // Count the instructions which we need to predicate.
354 unsigned NumPredicatedInstructions = 0;
355 for (auto &I : make_range(x: TIB, y: TIE)) {
356 if (!I.isDebugInstr()) {
357 LLVM_DEBUG(dbgs() << "Predicating: " << I);
358 NumPredicatedInstructions++;
359 }
360 }
361 for (auto &I : make_range(x: FIB, y: FIE)) {
362 if (!I.isDebugInstr()) {
363 LLVM_DEBUG(dbgs() << "Predicating: " << I);
364 NumPredicatedInstructions++;
365 }
366 }
367
368 // Even though we're optimising for size at the expense of performance,
369 // avoid creating really long predicated blocks.
370 if (NumPredicatedInstructions > 15)
371 return false;
372
373 // Some targets (e.g. Thumb2) need to insert extra instructions to
374 // start predicated blocks.
375 unsigned ExtraPredicateBytes = TII->extraSizeToPredicateInstructions(
376 MF, NumInsts: NumPredicatedInstructions);
377
378 LLVM_DEBUG(dbgs() << "MeetIfcvtSizeLimit(BranchBytes=" << BranchBytes
379 << ", CommonBytes=" << CommonBytes
380 << ", NumPredicatedInstructions="
381 << NumPredicatedInstructions
382 << ", ExtraPredicateBytes=" << ExtraPredicateBytes
383 << ")\n");
384 return (BranchBytes + CommonBytes) > ExtraPredicateBytes;
385 } else {
386 unsigned TCycle = TBBInfo.NonPredSize + TBBInfo.ExtraCost - Dups;
387 unsigned FCycle = FBBInfo.NonPredSize + FBBInfo.ExtraCost - Dups;
388 bool Res = TCycle > 0 && FCycle > 0 &&
389 TII->isProfitableToIfCvt(
390 TMBB&: *TBBInfo.BB, NumTCycles: TCycle, ExtraTCycles: TBBInfo.ExtraCost2, FMBB&: *FBBInfo.BB,
391 NumFCycles: FCycle, ExtraFCycles: FBBInfo.ExtraCost2, Probability: Prediction);
392 LLVM_DEBUG(dbgs() << "MeetIfcvtSizeLimit(TCycle=" << TCycle
393 << ", FCycle=" << FCycle
394 << ", TExtra=" << TBBInfo.ExtraCost2 << ", FExtra="
395 << FBBInfo.ExtraCost2 << ") = " << Res << "\n");
396 return Res;
397 }
398 }
399
400 /// Returns true if Block ends without a terminator.
401 bool blockAlwaysFallThrough(BBInfo &BBI) const {
402 return BBI.IsBrAnalyzable && BBI.TrueBB == nullptr;
403 }
404
405 /// Returns true if Block is known not to fallthrough to the following BB.
406 bool blockNeverFallThrough(BBInfo &BBI) const {
407 // Trust "HasFallThrough" if we could analyze branches.
408 if (BBI.IsBrAnalyzable)
409 return !BBI.HasFallThrough;
410 // If this is the last MBB in the function, or if the textual successor
411 // isn't in the successor list, then there is no fallthrough.
412 MachineFunction::iterator PI = BBI.BB->getIterator();
413 MachineFunction::iterator I = std::next(x: PI);
414 if (I == BBI.BB->getParent()->end() || !PI->isSuccessor(MBB: &*I))
415 return true;
416 // Could not prove that there is no fallthrough.
417 return false;
418 }
419
420 /// Used to sort if-conversion candidates.
421 static bool IfcvtTokenCmp(const std::unique_ptr<IfcvtToken> &C1,
422 const std::unique_ptr<IfcvtToken> &C2) {
423 int Incr1 = (C1->Kind == ICDiamond)
424 ? -(int)(C1->NumDups + C1->NumDups2) : (int)C1->NumDups;
425 int Incr2 = (C2->Kind == ICDiamond)
426 ? -(int)(C2->NumDups + C2->NumDups2) : (int)C2->NumDups;
427 if (Incr1 > Incr2)
428 return true;
429 else if (Incr1 == Incr2) {
430 // Favors subsumption.
431 if (!C1->NeedSubsumption && C2->NeedSubsumption)
432 return true;
433 else if (C1->NeedSubsumption == C2->NeedSubsumption) {
434 // Favors diamond over triangle, etc.
435 if ((unsigned)C1->Kind < (unsigned)C2->Kind)
436 return true;
437 else if (C1->Kind == C2->Kind)
438 return C1->BBI.BB->getNumber() < C2->BBI.BB->getNumber();
439 }
440 }
441 return false;
442 }
443 };
444
445} // end anonymous namespace
446
447char IfConverter::ID = 0;
448
449char &llvm::IfConverterID = IfConverter::ID;
450
451INITIALIZE_PASS_BEGIN(IfConverter, DEBUG_TYPE, "If Converter", false, false)
452INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfoWrapperPass)
453INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass)
454INITIALIZE_PASS_END(IfConverter, DEBUG_TYPE, "If Converter", false, false)
455
456bool IfConverter::runOnMachineFunction(MachineFunction &MF) {
457 if (skipFunction(F: MF.getFunction()) || (PredicateFtor && !PredicateFtor(MF)))
458 return false;
459
460 const TargetSubtargetInfo &ST = MF.getSubtarget();
461 TLI = ST.getTargetLowering();
462 TII = ST.getInstrInfo();
463 TRI = ST.getRegisterInfo();
464 MBFIWrapper MBFI(
465 getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI());
466 MBPI = &getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI();
467 ProfileSummaryInfo *PSI =
468 &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
469 MRI = &MF.getRegInfo();
470 SchedModel.init(TSInfo: &ST);
471
472 if (!TII) return false;
473
474 PreRegAlloc = MRI->isSSA();
475
476 bool BFChange = false;
477 if (!PreRegAlloc) {
478 // Tail merge tend to expose more if-conversion opportunities.
479 BranchFolder BF(true, false, MBFI, *MBPI, PSI);
480 BFChange = BF.OptimizeFunction(MF, tii: TII, tri: ST.getRegisterInfo());
481 }
482
483 LLVM_DEBUG(dbgs() << "\nIfcvt: function (" << ++FnNum << ") \'"
484 << MF.getName() << "\'");
485
486 if (FnNum < IfCvtFnStart || (IfCvtFnStop != -1 && FnNum > IfCvtFnStop)) {
487 LLVM_DEBUG(dbgs() << " skipped\n");
488 return false;
489 }
490 LLVM_DEBUG(dbgs() << "\n");
491
492 MF.RenumberBlocks();
493 BBAnalysis.resize(new_size: MF.getNumBlockIDs());
494
495 std::vector<std::unique_ptr<IfcvtToken>> Tokens;
496 MadeChange = false;
497 unsigned NumIfCvts = NumSimple + NumSimpleFalse + NumTriangle +
498 NumTriangleRev + NumTriangleFalse + NumTriangleFRev + NumDiamonds;
499 while (IfCvtLimit == -1 || (int)NumIfCvts < IfCvtLimit) {
500 // Do an initial analysis for each basic block and find all the potential
501 // candidates to perform if-conversion.
502 bool Change = false;
503 AnalyzeBlocks(MF, Tokens);
504 while (!Tokens.empty()) {
505 std::unique_ptr<IfcvtToken> Token = std::move(Tokens.back());
506 Tokens.pop_back();
507 BBInfo &BBI = Token->BBI;
508 IfcvtKind Kind = Token->Kind;
509 unsigned NumDups = Token->NumDups;
510 unsigned NumDups2 = Token->NumDups2;
511
512 // If the block has been evicted out of the queue or it has already been
513 // marked dead (due to it being predicated), then skip it.
514 if (BBI.IsDone)
515 BBI.IsEnqueued = false;
516 if (!BBI.IsEnqueued)
517 continue;
518
519 BBI.IsEnqueued = false;
520
521 bool RetVal = false;
522 switch (Kind) {
523 default: llvm_unreachable("Unexpected!");
524 case ICSimple:
525 case ICSimpleFalse: {
526 bool isFalse = Kind == ICSimpleFalse;
527 if ((isFalse && DisableSimpleF) || (!isFalse && DisableSimple)) break;
528 LLVM_DEBUG(dbgs() << "Ifcvt (Simple"
529 << (Kind == ICSimpleFalse ? " false" : "")
530 << "): " << printMBBReference(*BBI.BB) << " ("
531 << ((Kind == ICSimpleFalse) ? BBI.FalseBB->getNumber()
532 : BBI.TrueBB->getNumber())
533 << ") ");
534 RetVal = IfConvertSimple(BBI, Kind);
535 LLVM_DEBUG(dbgs() << (RetVal ? "succeeded!" : "failed!") << "\n");
536 if (RetVal) {
537 if (isFalse) ++NumSimpleFalse;
538 else ++NumSimple;
539 }
540 break;
541 }
542 case ICTriangle:
543 case ICTriangleRev:
544 case ICTriangleFalse:
545 case ICTriangleFRev: {
546 bool isFalse = Kind == ICTriangleFalse;
547 bool isRev = (Kind == ICTriangleRev || Kind == ICTriangleFRev);
548 if (DisableTriangle && !isFalse && !isRev) break;
549 if (DisableTriangleR && !isFalse && isRev) break;
550 if (DisableTriangleF && isFalse && !isRev) break;
551 LLVM_DEBUG(dbgs() << "Ifcvt (Triangle");
552 if (isFalse)
553 LLVM_DEBUG(dbgs() << " false");
554 if (isRev)
555 LLVM_DEBUG(dbgs() << " rev");
556 LLVM_DEBUG(dbgs() << "): " << printMBBReference(*BBI.BB)
557 << " (T:" << BBI.TrueBB->getNumber()
558 << ",F:" << BBI.FalseBB->getNumber() << ") ");
559 RetVal = IfConvertTriangle(BBI, Kind);
560 LLVM_DEBUG(dbgs() << (RetVal ? "succeeded!" : "failed!") << "\n");
561 if (RetVal) {
562 if (isFalse)
563 ++NumTriangleFalse;
564 else if (isRev)
565 ++NumTriangleRev;
566 else
567 ++NumTriangle;
568 }
569 break;
570 }
571 case ICDiamond:
572 if (DisableDiamond) break;
573 LLVM_DEBUG(dbgs() << "Ifcvt (Diamond): " << printMBBReference(*BBI.BB)
574 << " (T:" << BBI.TrueBB->getNumber()
575 << ",F:" << BBI.FalseBB->getNumber() << ") ");
576 RetVal = IfConvertDiamond(BBI, Kind, NumDups1: NumDups, NumDups2,
577 TClobbers: Token->TClobbersPred,
578 FClobbers: Token->FClobbersPred);
579 LLVM_DEBUG(dbgs() << (RetVal ? "succeeded!" : "failed!") << "\n");
580 if (RetVal) ++NumDiamonds;
581 break;
582 case ICForkedDiamond:
583 if (DisableForkedDiamond) break;
584 LLVM_DEBUG(dbgs() << "Ifcvt (Forked Diamond): "
585 << printMBBReference(*BBI.BB)
586 << " (T:" << BBI.TrueBB->getNumber()
587 << ",F:" << BBI.FalseBB->getNumber() << ") ");
588 RetVal = IfConvertForkedDiamond(BBI, Kind, NumDups1: NumDups, NumDups2,
589 TClobbers: Token->TClobbersPred,
590 FClobbers: Token->FClobbersPred);
591 LLVM_DEBUG(dbgs() << (RetVal ? "succeeded!" : "failed!") << "\n");
592 if (RetVal) ++NumForkedDiamonds;
593 break;
594 }
595
596 if (RetVal && MRI->tracksLiveness())
597 recomputeLivenessFlags(MBB&: *BBI.BB);
598
599 Change |= RetVal;
600
601 NumIfCvts = NumSimple + NumSimpleFalse + NumTriangle + NumTriangleRev +
602 NumTriangleFalse + NumTriangleFRev + NumDiamonds;
603 if (IfCvtLimit != -1 && (int)NumIfCvts >= IfCvtLimit)
604 break;
605 }
606
607 if (!Change)
608 break;
609 MadeChange |= Change;
610 }
611
612 Tokens.clear();
613 BBAnalysis.clear();
614
615 if (MadeChange && IfCvtBranchFold) {
616 BranchFolder BF(false, false, MBFI, *MBPI, PSI);
617 BF.OptimizeFunction(MF, tii: TII, tri: MF.getSubtarget().getRegisterInfo());
618 }
619
620 MadeChange |= BFChange;
621 return MadeChange;
622}
623
624/// BB has a fallthrough. Find its 'false' successor given its 'true' successor.
625static MachineBasicBlock *findFalseBlock(MachineBasicBlock *BB,
626 MachineBasicBlock *TrueBB) {
627 for (MachineBasicBlock *SuccBB : BB->successors()) {
628 if (SuccBB != TrueBB)
629 return SuccBB;
630 }
631 return nullptr;
632}
633
634/// Reverse the condition of the end of the block branch. Swap block's 'true'
635/// and 'false' successors.
636bool IfConverter::reverseBranchCondition(BBInfo &BBI) const {
637 DebugLoc dl; // FIXME: this is nowhere
638 if (!TII->reverseBranchCondition(Cond&: BBI.BrCond)) {
639 TII->removeBranch(MBB&: *BBI.BB);
640 TII->insertBranch(MBB&: *BBI.BB, TBB: BBI.FalseBB, FBB: BBI.TrueBB, Cond: BBI.BrCond, DL: dl);
641 std::swap(a&: BBI.TrueBB, b&: BBI.FalseBB);
642 return true;
643 }
644 return false;
645}
646
647/// Returns the next block in the function blocks ordering. If it is the end,
648/// returns NULL.
649static inline MachineBasicBlock *getNextBlock(MachineBasicBlock &MBB) {
650 MachineFunction::iterator I = MBB.getIterator();
651 MachineFunction::iterator E = MBB.getParent()->end();
652 if (++I == E)
653 return nullptr;
654 return &*I;
655}
656
657/// Returns true if the 'true' block (along with its predecessor) forms a valid
658/// simple shape for ifcvt. It also returns the number of instructions that the
659/// ifcvt would need to duplicate if performed in Dups.
660bool IfConverter::ValidSimple(BBInfo &TrueBBI, unsigned &Dups,
661 BranchProbability Prediction) const {
662 Dups = 0;
663 if (TrueBBI.IsBeingAnalyzed || TrueBBI.IsDone)
664 return false;
665
666 if (TrueBBI.IsBrAnalyzable)
667 return false;
668
669 if (TrueBBI.BB->pred_size() > 1) {
670 if (TrueBBI.CannotBeCopied ||
671 !TII->isProfitableToDupForIfCvt(MBB&: *TrueBBI.BB, NumCycles: TrueBBI.NonPredSize,
672 Probability: Prediction))
673 return false;
674 Dups = TrueBBI.NonPredSize;
675 }
676
677 return true;
678}
679
680/// Returns true if the 'true' and 'false' blocks (along with their common
681/// predecessor) forms a valid triangle shape for ifcvt. If 'FalseBranch' is
682/// true, it checks if 'true' block's false branch branches to the 'false' block
683/// rather than the other way around. It also returns the number of instructions
684/// that the ifcvt would need to duplicate if performed in 'Dups'.
685bool IfConverter::ValidTriangle(BBInfo &TrueBBI, BBInfo &FalseBBI,
686 bool FalseBranch, unsigned &Dups,
687 BranchProbability Prediction) const {
688 Dups = 0;
689 if (TrueBBI.BB == FalseBBI.BB)
690 return false;
691
692 if (TrueBBI.IsBeingAnalyzed || TrueBBI.IsDone)
693 return false;
694
695 if (TrueBBI.BB->pred_size() > 1) {
696 if (TrueBBI.CannotBeCopied)
697 return false;
698
699 unsigned Size = TrueBBI.NonPredSize;
700 if (TrueBBI.IsBrAnalyzable) {
701 if (TrueBBI.TrueBB && TrueBBI.BrCond.empty())
702 // Ends with an unconditional branch. It will be removed.
703 --Size;
704 else {
705 MachineBasicBlock *FExit = FalseBranch
706 ? TrueBBI.TrueBB : TrueBBI.FalseBB;
707 if (FExit)
708 // Require a conditional branch
709 ++Size;
710 }
711 }
712 if (!TII->isProfitableToDupForIfCvt(MBB&: *TrueBBI.BB, NumCycles: Size, Probability: Prediction))
713 return false;
714 Dups = Size;
715 }
716
717 MachineBasicBlock *TExit = FalseBranch ? TrueBBI.FalseBB : TrueBBI.TrueBB;
718 if (!TExit && blockAlwaysFallThrough(BBI&: TrueBBI)) {
719 MachineFunction::iterator I = TrueBBI.BB->getIterator();
720 if (++I == TrueBBI.BB->getParent()->end())
721 return false;
722 TExit = &*I;
723 }
724 return TExit && TExit == FalseBBI.BB;
725}
726
727/// Count duplicated instructions and move the iterators to show where they
728/// are.
729/// @param TIB True Iterator Begin
730/// @param FIB False Iterator Begin
731/// These two iterators initially point to the first instruction of the two
732/// blocks, and finally point to the first non-shared instruction.
733/// @param TIE True Iterator End
734/// @param FIE False Iterator End
735/// These two iterators initially point to End() for the two blocks() and
736/// finally point to the first shared instruction in the tail.
737/// Upon return [TIB, TIE), and [FIB, FIE) mark the un-duplicated portions of
738/// two blocks.
739/// @param Dups1 count of duplicated instructions at the beginning of the 2
740/// blocks.
741/// @param Dups2 count of duplicated instructions at the end of the 2 blocks.
742/// @param SkipUnconditionalBranches if true, Don't make sure that
743/// unconditional branches at the end of the blocks are the same. True is
744/// passed when the blocks are analyzable to allow for fallthrough to be
745/// handled.
746/// @return false if the shared portion prevents if conversion.
747bool IfConverter::CountDuplicatedInstructions(
748 MachineBasicBlock::iterator &TIB,
749 MachineBasicBlock::iterator &FIB,
750 MachineBasicBlock::iterator &TIE,
751 MachineBasicBlock::iterator &FIE,
752 unsigned &Dups1, unsigned &Dups2,
753 MachineBasicBlock &TBB, MachineBasicBlock &FBB,
754 bool SkipUnconditionalBranches) const {
755 while (TIB != TIE && FIB != FIE) {
756 // Skip dbg_value instructions. These do not count.
757 TIB = skipDebugInstructionsForward(It: TIB, End: TIE, SkipPseudoOp: false);
758 FIB = skipDebugInstructionsForward(It: FIB, End: FIE, SkipPseudoOp: false);
759 if (TIB == TIE || FIB == FIE)
760 break;
761 if (!TIB->isIdenticalTo(Other: *FIB))
762 break;
763 // A pred-clobbering instruction in the shared portion prevents
764 // if-conversion.
765 std::vector<MachineOperand> PredDefs;
766 if (TII->ClobbersPredicate(MI&: *TIB, Pred&: PredDefs, SkipDead: false))
767 return false;
768 // If we get all the way to the branch instructions, don't count them.
769 if (!TIB->isBranch())
770 ++Dups1;
771 ++TIB;
772 ++FIB;
773 }
774
775 // Check for already containing all of the block.
776 if (TIB == TIE || FIB == FIE)
777 return true;
778 // Now, in preparation for counting duplicate instructions at the ends of the
779 // blocks, switch to reverse_iterators. Note that getReverse() returns an
780 // iterator that points to the same instruction, unlike std::reverse_iterator.
781 // We have to do our own shifting so that we get the same range.
782 MachineBasicBlock::reverse_iterator RTIE = std::next(x: TIE.getReverse());
783 MachineBasicBlock::reverse_iterator RFIE = std::next(x: FIE.getReverse());
784 const MachineBasicBlock::reverse_iterator RTIB = std::next(x: TIB.getReverse());
785 const MachineBasicBlock::reverse_iterator RFIB = std::next(x: FIB.getReverse());
786
787 if (!TBB.succ_empty() || !FBB.succ_empty()) {
788 if (SkipUnconditionalBranches) {
789 while (RTIE != RTIB && RTIE->isUnconditionalBranch())
790 ++RTIE;
791 while (RFIE != RFIB && RFIE->isUnconditionalBranch())
792 ++RFIE;
793 }
794 }
795
796 // Count duplicate instructions at the ends of the blocks.
797 while (RTIE != RTIB && RFIE != RFIB) {
798 // Skip dbg_value instructions. These do not count.
799 // Note that these are reverse iterators going forward.
800 RTIE = skipDebugInstructionsForward(It: RTIE, End: RTIB, SkipPseudoOp: false);
801 RFIE = skipDebugInstructionsForward(It: RFIE, End: RFIB, SkipPseudoOp: false);
802 if (RTIE == RTIB || RFIE == RFIB)
803 break;
804 if (!RTIE->isIdenticalTo(Other: *RFIE))
805 break;
806 // We have to verify that any branch instructions are the same, and then we
807 // don't count them toward the # of duplicate instructions.
808 if (!RTIE->isBranch())
809 ++Dups2;
810 ++RTIE;
811 ++RFIE;
812 }
813 TIE = std::next(x: RTIE.getReverse());
814 FIE = std::next(x: RFIE.getReverse());
815 return true;
816}
817
818/// RescanInstructions - Run ScanInstructions on a pair of blocks.
819/// @param TIB - True Iterator Begin, points to first non-shared instruction
820/// @param FIB - False Iterator Begin, points to first non-shared instruction
821/// @param TIE - True Iterator End, points past last non-shared instruction
822/// @param FIE - False Iterator End, points past last non-shared instruction
823/// @param TrueBBI - BBInfo to update for the true block.
824/// @param FalseBBI - BBInfo to update for the false block.
825/// @returns - false if either block cannot be predicated or if both blocks end
826/// with a predicate-clobbering instruction.
827bool IfConverter::RescanInstructions(
828 MachineBasicBlock::iterator &TIB, MachineBasicBlock::iterator &FIB,
829 MachineBasicBlock::iterator &TIE, MachineBasicBlock::iterator &FIE,
830 BBInfo &TrueBBI, BBInfo &FalseBBI) const {
831 bool BranchUnpredicable = true;
832 TrueBBI.IsUnpredicable = FalseBBI.IsUnpredicable = false;
833 ScanInstructions(BBI&: TrueBBI, Begin&: TIB, End&: TIE, BranchUnpredicable);
834 if (TrueBBI.IsUnpredicable)
835 return false;
836 ScanInstructions(BBI&: FalseBBI, Begin&: FIB, End&: FIE, BranchUnpredicable);
837 if (FalseBBI.IsUnpredicable)
838 return false;
839 if (TrueBBI.ClobbersPred && FalseBBI.ClobbersPred)
840 return false;
841 return true;
842}
843
844#ifndef NDEBUG
845static void verifySameBranchInstructions(
846 MachineBasicBlock *MBB1,
847 MachineBasicBlock *MBB2) {
848 const MachineBasicBlock::reverse_iterator B1 = MBB1->rend();
849 const MachineBasicBlock::reverse_iterator B2 = MBB2->rend();
850 MachineBasicBlock::reverse_iterator E1 = MBB1->rbegin();
851 MachineBasicBlock::reverse_iterator E2 = MBB2->rbegin();
852 while (E1 != B1 && E2 != B2) {
853 skipDebugInstructionsForward(E1, B1, false);
854 skipDebugInstructionsForward(E2, B2, false);
855 if (E1 == B1 && E2 == B2)
856 break;
857
858 if (E1 == B1) {
859 assert(!E2->isBranch() && "Branch mis-match, one block is empty.");
860 break;
861 }
862 if (E2 == B2) {
863 assert(!E1->isBranch() && "Branch mis-match, one block is empty.");
864 break;
865 }
866
867 if (E1->isBranch() || E2->isBranch())
868 assert(E1->isIdenticalTo(*E2) &&
869 "Branch mis-match, branch instructions don't match.");
870 else
871 break;
872 ++E1;
873 ++E2;
874 }
875}
876#endif
877
878/// ValidForkedDiamond - Returns true if the 'true' and 'false' blocks (along
879/// with their common predecessor) form a diamond if a common tail block is
880/// extracted.
881/// While not strictly a diamond, this pattern would form a diamond if
882/// tail-merging had merged the shared tails.
883/// EBB
884/// _/ \_
885/// | |
886/// TBB FBB
887/// / \ / \
888/// FalseBB TrueBB FalseBB
889/// Currently only handles analyzable branches.
890/// Specifically excludes actual diamonds to avoid overlap.
891bool IfConverter::ValidForkedDiamond(
892 BBInfo &TrueBBI, BBInfo &FalseBBI,
893 unsigned &Dups1, unsigned &Dups2,
894 BBInfo &TrueBBICalc, BBInfo &FalseBBICalc) const {
895 Dups1 = Dups2 = 0;
896 if (TrueBBI.IsBeingAnalyzed || TrueBBI.IsDone ||
897 FalseBBI.IsBeingAnalyzed || FalseBBI.IsDone)
898 return false;
899
900 if (!TrueBBI.IsBrAnalyzable || !FalseBBI.IsBrAnalyzable)
901 return false;
902 // Don't IfConvert blocks that can't be folded into their predecessor.
903 if (TrueBBI.BB->pred_size() > 1 || FalseBBI.BB->pred_size() > 1)
904 return false;
905
906 // This function is specifically looking for conditional tails, as
907 // unconditional tails are already handled by the standard diamond case.
908 if (TrueBBI.BrCond.size() == 0 ||
909 FalseBBI.BrCond.size() == 0)
910 return false;
911
912 MachineBasicBlock *TT = TrueBBI.TrueBB;
913 MachineBasicBlock *TF = TrueBBI.FalseBB;
914 MachineBasicBlock *FT = FalseBBI.TrueBB;
915 MachineBasicBlock *FF = FalseBBI.FalseBB;
916
917 if (!TT)
918 TT = getNextBlock(MBB&: *TrueBBI.BB);
919 if (!TF)
920 TF = getNextBlock(MBB&: *TrueBBI.BB);
921 if (!FT)
922 FT = getNextBlock(MBB&: *FalseBBI.BB);
923 if (!FF)
924 FF = getNextBlock(MBB&: *FalseBBI.BB);
925
926 if (!TT || !TF)
927 return false;
928
929 // Check successors. If they don't match, bail.
930 if (!((TT == FT && TF == FF) || (TF == FT && TT == FF)))
931 return false;
932
933 bool FalseReversed = false;
934 if (TF == FT && TT == FF) {
935 // If the branches are opposing, but we can't reverse, don't do it.
936 if (!FalseBBI.IsBrReversible)
937 return false;
938 FalseReversed = true;
939 reverseBranchCondition(BBI&: FalseBBI);
940 }
941 llvm::scope_exit UnReverseOnExit([&]() {
942 if (FalseReversed)
943 reverseBranchCondition(BBI&: FalseBBI);
944 });
945
946 // Count duplicate instructions at the beginning of the true and false blocks.
947 MachineBasicBlock::iterator TIB = TrueBBI.BB->begin();
948 MachineBasicBlock::iterator FIB = FalseBBI.BB->begin();
949 MachineBasicBlock::iterator TIE = TrueBBI.BB->end();
950 MachineBasicBlock::iterator FIE = FalseBBI.BB->end();
951 if(!CountDuplicatedInstructions(TIB, FIB, TIE, FIE, Dups1, Dups2,
952 TBB&: *TrueBBI.BB, FBB&: *FalseBBI.BB,
953 /* SkipUnconditionalBranches */ true))
954 return false;
955
956 TrueBBICalc.BB = TrueBBI.BB;
957 FalseBBICalc.BB = FalseBBI.BB;
958 TrueBBICalc.IsBrAnalyzable = TrueBBI.IsBrAnalyzable;
959 FalseBBICalc.IsBrAnalyzable = FalseBBI.IsBrAnalyzable;
960 if (!RescanInstructions(TIB, FIB, TIE, FIE, TrueBBI&: TrueBBICalc, FalseBBI&: FalseBBICalc))
961 return false;
962
963 // The size is used to decide whether to if-convert, and the shared portions
964 // are subtracted off. Because of the subtraction, we just use the size that
965 // was calculated by the original ScanInstructions, as it is correct.
966 TrueBBICalc.NonPredSize = TrueBBI.NonPredSize;
967 FalseBBICalc.NonPredSize = FalseBBI.NonPredSize;
968 return true;
969}
970
971/// ValidDiamond - Returns true if the 'true' and 'false' blocks (along
972/// with their common predecessor) forms a valid diamond shape for ifcvt.
973bool IfConverter::ValidDiamond(
974 BBInfo &TrueBBI, BBInfo &FalseBBI,
975 unsigned &Dups1, unsigned &Dups2,
976 BBInfo &TrueBBICalc, BBInfo &FalseBBICalc) const {
977 Dups1 = Dups2 = 0;
978 if (TrueBBI.IsBeingAnalyzed || TrueBBI.IsDone ||
979 FalseBBI.IsBeingAnalyzed || FalseBBI.IsDone)
980 return false;
981
982 // If the True and False BBs are equal we're dealing with a degenerate case
983 // that we don't treat as a diamond.
984 if (TrueBBI.BB == FalseBBI.BB)
985 return false;
986
987 MachineBasicBlock *TT = TrueBBI.TrueBB;
988 MachineBasicBlock *FT = FalseBBI.TrueBB;
989
990 if (!TT && blockAlwaysFallThrough(BBI&: TrueBBI))
991 TT = getNextBlock(MBB&: *TrueBBI.BB);
992 if (!FT && blockAlwaysFallThrough(BBI&: FalseBBI))
993 FT = getNextBlock(MBB&: *FalseBBI.BB);
994 if (TT != FT)
995 return false;
996 if (!TT && (TrueBBI.IsBrAnalyzable || FalseBBI.IsBrAnalyzable))
997 return false;
998 if (TrueBBI.BB->pred_size() > 1 || FalseBBI.BB->pred_size() > 1)
999 return false;
1000
1001 // FIXME: Allow true block to have an early exit?
1002 if (TrueBBI.FalseBB || FalseBBI.FalseBB)
1003 return false;
1004
1005 // Count duplicate instructions at the beginning and end of the true and
1006 // false blocks.
1007 // Skip unconditional branches only if we are considering an analyzable
1008 // diamond. Otherwise the branches must be the same.
1009 bool SkipUnconditionalBranches =
1010 TrueBBI.IsBrAnalyzable && FalseBBI.IsBrAnalyzable;
1011 MachineBasicBlock::iterator TIB = TrueBBI.BB->begin();
1012 MachineBasicBlock::iterator FIB = FalseBBI.BB->begin();
1013 MachineBasicBlock::iterator TIE = TrueBBI.BB->end();
1014 MachineBasicBlock::iterator FIE = FalseBBI.BB->end();
1015 if(!CountDuplicatedInstructions(TIB, FIB, TIE, FIE, Dups1, Dups2,
1016 TBB&: *TrueBBI.BB, FBB&: *FalseBBI.BB,
1017 SkipUnconditionalBranches))
1018 return false;
1019
1020 TrueBBICalc.BB = TrueBBI.BB;
1021 FalseBBICalc.BB = FalseBBI.BB;
1022 TrueBBICalc.IsBrAnalyzable = TrueBBI.IsBrAnalyzable;
1023 FalseBBICalc.IsBrAnalyzable = FalseBBI.IsBrAnalyzable;
1024 if (!RescanInstructions(TIB, FIB, TIE, FIE, TrueBBI&: TrueBBICalc, FalseBBI&: FalseBBICalc))
1025 return false;
1026 // The size is used to decide whether to if-convert, and the shared portions
1027 // are subtracted off. Because of the subtraction, we just use the size that
1028 // was calculated by the original ScanInstructions, as it is correct.
1029 TrueBBICalc.NonPredSize = TrueBBI.NonPredSize;
1030 FalseBBICalc.NonPredSize = FalseBBI.NonPredSize;
1031 return true;
1032}
1033
1034/// AnalyzeBranches - Look at the branches at the end of a block to determine if
1035/// the block is predicable.
1036void IfConverter::AnalyzeBranches(BBInfo &BBI) {
1037 if (BBI.IsDone)
1038 return;
1039
1040 BBI.TrueBB = BBI.FalseBB = nullptr;
1041 BBI.BrCond.clear();
1042 BBI.IsBrAnalyzable =
1043 !TII->analyzeBranch(MBB&: *BBI.BB, TBB&: BBI.TrueBB, FBB&: BBI.FalseBB, Cond&: BBI.BrCond);
1044 if (!BBI.IsBrAnalyzable) {
1045 BBI.TrueBB = nullptr;
1046 BBI.FalseBB = nullptr;
1047 BBI.BrCond.clear();
1048 }
1049
1050 SmallVector<MachineOperand, 4> RevCond(BBI.BrCond.begin(), BBI.BrCond.end());
1051 BBI.IsBrReversible = (RevCond.size() == 0) ||
1052 !TII->reverseBranchCondition(Cond&: RevCond);
1053 BBI.HasFallThrough = BBI.IsBrAnalyzable && BBI.FalseBB == nullptr;
1054
1055 if (BBI.BrCond.size()) {
1056 // No false branch. This BB must end with a conditional branch and a
1057 // fallthrough.
1058 if (!BBI.FalseBB)
1059 BBI.FalseBB = findFalseBlock(BB: BBI.BB, TrueBB: BBI.TrueBB);
1060 if (!BBI.FalseBB) {
1061 // Malformed bcc? True and false blocks are the same?
1062 BBI.IsUnpredicable = true;
1063 }
1064 }
1065}
1066
1067/// ScanInstructions - Scan all the instructions in the block to determine if
1068/// the block is predicable. In most cases, that means all the instructions
1069/// in the block are isPredicable(). Also checks if the block contains any
1070/// instruction which can clobber a predicate (e.g. condition code register).
1071/// If so, the block is not predicable unless it's the last instruction.
1072void IfConverter::ScanInstructions(BBInfo &BBI,
1073 MachineBasicBlock::iterator &Begin,
1074 MachineBasicBlock::iterator &End,
1075 bool BranchUnpredicable) const {
1076 if (BBI.IsDone || BBI.IsUnpredicable)
1077 return;
1078
1079 bool AlreadyPredicated = !BBI.Predicate.empty();
1080
1081 BBI.NonPredSize = 0;
1082 BBI.ExtraCost = 0;
1083 BBI.ExtraCost2 = 0;
1084 BBI.ClobbersPred = false;
1085 for (MachineInstr &MI : make_range(x: Begin, y: End)) {
1086 if (MI.isDebugInstr())
1087 continue;
1088
1089 // It's unsafe to duplicate convergent instructions in this context, so set
1090 // BBI.CannotBeCopied to true if MI is convergent. To see why, consider the
1091 // following CFG, which is subject to our "simple" transformation.
1092 //
1093 // BB0 // if (c1) goto BB1; else goto BB2;
1094 // / \
1095 // BB1 |
1096 // | BB2 // if (c2) goto TBB; else goto FBB;
1097 // | / |
1098 // | / |
1099 // TBB |
1100 // | |
1101 // | FBB
1102 // |
1103 // exit
1104 //
1105 // Suppose we want to move TBB's contents up into BB1 and BB2 (in BB1 they'd
1106 // be unconditional, and in BB2, they'd be predicated upon c2), and suppose
1107 // TBB contains a convergent instruction. This is safe iff doing so does
1108 // not add a control-flow dependency to the convergent instruction -- i.e.,
1109 // it's safe iff the set of control flows that leads us to the convergent
1110 // instruction does not get smaller after the transformation.
1111 //
1112 // Originally we executed TBB if c1 || c2. After the transformation, there
1113 // are two copies of TBB's instructions. We get to the first if c1, and we
1114 // get to the second if !c1 && c2.
1115 //
1116 // There are clearly fewer ways to satisfy the condition "c1" than
1117 // "c1 || c2". Since we've shrunk the set of control flows which lead to
1118 // our convergent instruction, the transformation is unsafe.
1119 if (MI.isNotDuplicable() || MI.isConvergent())
1120 BBI.CannotBeCopied = true;
1121
1122 bool isPredicated = TII->isPredicated(MI);
1123 bool isCondBr = BBI.IsBrAnalyzable && MI.isConditionalBranch();
1124
1125 if (BranchUnpredicable && MI.isBranch()) {
1126 BBI.IsUnpredicable = true;
1127 return;
1128 }
1129
1130 // A conditional branch is not predicable, but it may be eliminated.
1131 if (isCondBr)
1132 continue;
1133
1134 if (!isPredicated) {
1135 BBI.NonPredSize++;
1136 unsigned ExtraPredCost = TII->getPredicationCost(MI);
1137 unsigned NumCycles = SchedModel.computeInstrLatency(MI: &MI, UseDefaultDefLatency: false);
1138 if (NumCycles > 1)
1139 BBI.ExtraCost += NumCycles-1;
1140 BBI.ExtraCost2 += ExtraPredCost;
1141 } else if (!AlreadyPredicated) {
1142 // FIXME: This instruction is already predicated before the
1143 // if-conversion pass. It's probably something like a conditional move.
1144 // Mark this block unpredicable for now.
1145 BBI.IsUnpredicable = true;
1146 return;
1147 }
1148
1149 if (BBI.ClobbersPred && !isPredicated) {
1150 // Predicate modification instruction should end the block (except for
1151 // already predicated instructions and end of block branches).
1152 // Predicate may have been modified, the subsequent (currently)
1153 // unpredicated instructions cannot be correctly predicated.
1154 BBI.IsUnpredicable = true;
1155 return;
1156 }
1157
1158 // FIXME: Make use of PredDefs? e.g. ADDC, SUBC sets predicates but are
1159 // still potentially predicable.
1160 std::vector<MachineOperand> PredDefs;
1161 if (TII->ClobbersPredicate(MI, Pred&: PredDefs, SkipDead: true))
1162 BBI.ClobbersPred = true;
1163
1164 if (!TII->isPredicable(MI)) {
1165 BBI.IsUnpredicable = true;
1166 return;
1167 }
1168 }
1169}
1170
1171/// Determine if the block is a suitable candidate to be predicated by the
1172/// specified predicate.
1173/// @param BBI BBInfo for the block to check
1174/// @param Pred Predicate array for the branch that leads to BBI
1175/// @param isTriangle true if the Analysis is for a triangle
1176/// @param RevBranch true if Reverse(Pred) leads to BBI (e.g. BBI is the false
1177/// case
1178/// @param hasCommonTail true if BBI shares a tail with a sibling block that
1179/// contains any instruction that would make the block unpredicable.
1180bool IfConverter::FeasibilityAnalysis(BBInfo &BBI,
1181 SmallVectorImpl<MachineOperand> &Pred,
1182 bool isTriangle, bool RevBranch,
1183 bool hasCommonTail) {
1184 // If the block is dead or unpredicable, then it cannot be predicated.
1185 // Two blocks may share a common unpredicable tail, but this doesn't prevent
1186 // them from being if-converted. The non-shared portion is assumed to have
1187 // been checked
1188 if (BBI.IsDone || (BBI.IsUnpredicable && !hasCommonTail))
1189 return false;
1190
1191 // If it is already predicated but we couldn't analyze its terminator, the
1192 // latter might fallthrough, but we can't determine where to.
1193 // Conservatively avoid if-converting again.
1194 if (BBI.Predicate.size() && !BBI.IsBrAnalyzable)
1195 return false;
1196
1197 // If it is already predicated, check if the new predicate subsumes
1198 // its predicate.
1199 if (BBI.Predicate.size() && !TII->SubsumesPredicate(Pred1: Pred, Pred2: BBI.Predicate))
1200 return false;
1201
1202 if (!hasCommonTail && BBI.BrCond.size()) {
1203 if (!isTriangle)
1204 return false;
1205
1206 // Test predicate subsumption.
1207 SmallVector<MachineOperand, 4> RevPred(Pred.begin(), Pred.end());
1208 SmallVector<MachineOperand, 4> Cond(BBI.BrCond.begin(), BBI.BrCond.end());
1209 if (RevBranch) {
1210 if (TII->reverseBranchCondition(Cond))
1211 return false;
1212 }
1213 if (TII->reverseBranchCondition(Cond&: RevPred) ||
1214 !TII->SubsumesPredicate(Pred1: Cond, Pred2: RevPred))
1215 return false;
1216 }
1217
1218 return true;
1219}
1220
1221/// Analyze the structure of the sub-CFG starting from the specified block.
1222/// Record its successors and whether it looks like an if-conversion candidate.
1223void IfConverter::AnalyzeBlock(
1224 MachineBasicBlock &MBB, std::vector<std::unique_ptr<IfcvtToken>> &Tokens) {
1225 struct BBState {
1226 BBState(MachineBasicBlock &MBB) : MBB(&MBB) {}
1227 MachineBasicBlock *MBB;
1228
1229 /// This flag is true if MBB's successors have been analyzed.
1230 bool SuccsAnalyzed = false;
1231 };
1232
1233 // Push MBB to the stack.
1234 SmallVector<BBState, 16> BBStack(1, MBB);
1235
1236 while (!BBStack.empty()) {
1237 BBState &State = BBStack.back();
1238 MachineBasicBlock *BB = State.MBB;
1239 BBInfo &BBI = BBAnalysis[BB->getNumber()];
1240
1241 if (!State.SuccsAnalyzed) {
1242 if (BBI.IsAnalyzed || BBI.IsBeingAnalyzed) {
1243 BBStack.pop_back();
1244 continue;
1245 }
1246
1247 BBI.BB = BB;
1248 BBI.IsBeingAnalyzed = true;
1249
1250 AnalyzeBranches(BBI);
1251 MachineBasicBlock::iterator Begin = BBI.BB->begin();
1252 MachineBasicBlock::iterator End = BBI.BB->end();
1253 ScanInstructions(BBI, Begin, End);
1254
1255 // Unanalyzable or ends with fallthrough or unconditional branch, or if is
1256 // not considered for ifcvt anymore.
1257 if (!BBI.IsBrAnalyzable || BBI.BrCond.empty() || BBI.IsDone) {
1258 BBI.IsBeingAnalyzed = false;
1259 BBI.IsAnalyzed = true;
1260 BBStack.pop_back();
1261 continue;
1262 }
1263
1264 // Do not ifcvt if either path is a back edge to the entry block.
1265 if (BBI.TrueBB == BB || BBI.FalseBB == BB) {
1266 BBI.IsBeingAnalyzed = false;
1267 BBI.IsAnalyzed = true;
1268 BBStack.pop_back();
1269 continue;
1270 }
1271
1272 // Do not ifcvt if true and false fallthrough blocks are the same.
1273 if (!BBI.FalseBB) {
1274 BBI.IsBeingAnalyzed = false;
1275 BBI.IsAnalyzed = true;
1276 BBStack.pop_back();
1277 continue;
1278 }
1279
1280 // Push the False and True blocks to the stack.
1281 State.SuccsAnalyzed = true;
1282 BBStack.push_back(Elt: *BBI.FalseBB);
1283 BBStack.push_back(Elt: *BBI.TrueBB);
1284 continue;
1285 }
1286
1287 BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
1288 BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
1289
1290 if (TrueBBI.IsDone && FalseBBI.IsDone) {
1291 BBI.IsBeingAnalyzed = false;
1292 BBI.IsAnalyzed = true;
1293 BBStack.pop_back();
1294 continue;
1295 }
1296
1297 SmallVector<MachineOperand, 4>
1298 RevCond(BBI.BrCond.begin(), BBI.BrCond.end());
1299 bool CanRevCond = !TII->reverseBranchCondition(Cond&: RevCond);
1300
1301 unsigned Dups = 0;
1302 unsigned Dups2 = 0;
1303 bool TNeedSub = !TrueBBI.Predicate.empty();
1304 bool FNeedSub = !FalseBBI.Predicate.empty();
1305 bool Enqueued = false;
1306
1307 BranchProbability Prediction = MBPI->getEdgeProbability(Src: BB, Dst: TrueBBI.BB);
1308
1309 if (CanRevCond) {
1310 BBInfo TrueBBICalc, FalseBBICalc;
1311 auto feasibleDiamond = [&](bool Forked) {
1312 bool MeetsSize = MeetIfcvtSizeLimit(TBBInfo&: TrueBBICalc, FBBInfo&: FalseBBICalc, CommBB&: *BB,
1313 Dups: Dups + Dups2, Prediction, Forked);
1314 bool TrueFeasible = FeasibilityAnalysis(BBI&: TrueBBI, Pred&: BBI.BrCond,
1315 /* IsTriangle */ isTriangle: false, /* RevCond */ RevBranch: false,
1316 /* hasCommonTail */ true);
1317 bool FalseFeasible = FeasibilityAnalysis(BBI&: FalseBBI, Pred&: RevCond,
1318 /* IsTriangle */ isTriangle: false, /* RevCond */ RevBranch: false,
1319 /* hasCommonTail */ true);
1320 return MeetsSize && TrueFeasible && FalseFeasible;
1321 };
1322
1323 if (ValidDiamond(TrueBBI, FalseBBI, Dups1&: Dups, Dups2,
1324 TrueBBICalc, FalseBBICalc)) {
1325 if (feasibleDiamond(false)) {
1326 // Diamond:
1327 // EBB
1328 // / \_
1329 // | |
1330 // TBB FBB
1331 // \ /
1332 // TailBB
1333 // Note TailBB can be empty.
1334 Tokens.push_back(x: std::make_unique<IfcvtToken>(
1335 args&: BBI, args: ICDiamond, args: TNeedSub | FNeedSub, args&: Dups, args&: Dups2,
1336 args: (bool) TrueBBICalc.ClobbersPred, args: (bool) FalseBBICalc.ClobbersPred));
1337 Enqueued = true;
1338 }
1339 } else if (ValidForkedDiamond(TrueBBI, FalseBBI, Dups1&: Dups, Dups2,
1340 TrueBBICalc, FalseBBICalc)) {
1341 if (feasibleDiamond(true)) {
1342 // ForkedDiamond:
1343 // if TBB and FBB have a common tail that includes their conditional
1344 // branch instructions, then we can If Convert this pattern.
1345 // EBB
1346 // _/ \_
1347 // | |
1348 // TBB FBB
1349 // / \ / \
1350 // FalseBB TrueBB FalseBB
1351 //
1352 Tokens.push_back(x: std::make_unique<IfcvtToken>(
1353 args&: BBI, args: ICForkedDiamond, args: TNeedSub | FNeedSub, args&: Dups, args&: Dups2,
1354 args: (bool) TrueBBICalc.ClobbersPred, args: (bool) FalseBBICalc.ClobbersPred));
1355 Enqueued = true;
1356 }
1357 }
1358 }
1359
1360 if (ValidTriangle(TrueBBI, FalseBBI, FalseBranch: false, Dups, Prediction) &&
1361 MeetIfcvtSizeLimit(BB&: *TrueBBI.BB, Cycle: TrueBBI.NonPredSize + TrueBBI.ExtraCost,
1362 Extra: TrueBBI.ExtraCost2, Prediction) &&
1363 FeasibilityAnalysis(BBI&: TrueBBI, Pred&: BBI.BrCond, isTriangle: true)) {
1364 // Triangle:
1365 // EBB
1366 // | \_
1367 // | |
1368 // | TBB
1369 // | /
1370 // FBB
1371 Tokens.push_back(
1372 x: std::make_unique<IfcvtToken>(args&: BBI, args: ICTriangle, args&: TNeedSub, args&: Dups));
1373 Enqueued = true;
1374 }
1375
1376 if (ValidTriangle(TrueBBI, FalseBBI, FalseBranch: true, Dups, Prediction) &&
1377 MeetIfcvtSizeLimit(BB&: *TrueBBI.BB, Cycle: TrueBBI.NonPredSize + TrueBBI.ExtraCost,
1378 Extra: TrueBBI.ExtraCost2, Prediction) &&
1379 FeasibilityAnalysis(BBI&: TrueBBI, Pred&: BBI.BrCond, isTriangle: true, RevBranch: true)) {
1380 Tokens.push_back(
1381 x: std::make_unique<IfcvtToken>(args&: BBI, args: ICTriangleRev, args&: TNeedSub, args&: Dups));
1382 Enqueued = true;
1383 }
1384
1385 if (ValidSimple(TrueBBI, Dups, Prediction) &&
1386 MeetIfcvtSizeLimit(BB&: *TrueBBI.BB, Cycle: TrueBBI.NonPredSize + TrueBBI.ExtraCost,
1387 Extra: TrueBBI.ExtraCost2, Prediction) &&
1388 FeasibilityAnalysis(BBI&: TrueBBI, Pred&: BBI.BrCond)) {
1389 // Simple (split, no rejoin):
1390 // EBB
1391 // | \_
1392 // | |
1393 // | TBB---> exit
1394 // |
1395 // FBB
1396 Tokens.push_back(
1397 x: std::make_unique<IfcvtToken>(args&: BBI, args: ICSimple, args&: TNeedSub, args&: Dups));
1398 Enqueued = true;
1399 }
1400
1401 if (CanRevCond) {
1402 // Try the other path...
1403 if (ValidTriangle(TrueBBI&: FalseBBI, FalseBBI&: TrueBBI, FalseBranch: false, Dups,
1404 Prediction: Prediction.getCompl()) &&
1405 MeetIfcvtSizeLimit(BB&: *FalseBBI.BB,
1406 Cycle: FalseBBI.NonPredSize + FalseBBI.ExtraCost,
1407 Extra: FalseBBI.ExtraCost2, Prediction: Prediction.getCompl()) &&
1408 FeasibilityAnalysis(BBI&: FalseBBI, Pred&: RevCond, isTriangle: true)) {
1409 Tokens.push_back(x: std::make_unique<IfcvtToken>(args&: BBI, args: ICTriangleFalse,
1410 args&: FNeedSub, args&: Dups));
1411 Enqueued = true;
1412 }
1413
1414 if (ValidTriangle(TrueBBI&: FalseBBI, FalseBBI&: TrueBBI, FalseBranch: true, Dups,
1415 Prediction: Prediction.getCompl()) &&
1416 MeetIfcvtSizeLimit(BB&: *FalseBBI.BB,
1417 Cycle: FalseBBI.NonPredSize + FalseBBI.ExtraCost,
1418 Extra: FalseBBI.ExtraCost2, Prediction: Prediction.getCompl()) &&
1419 FeasibilityAnalysis(BBI&: FalseBBI, Pred&: RevCond, isTriangle: true, RevBranch: true)) {
1420 Tokens.push_back(
1421 x: std::make_unique<IfcvtToken>(args&: BBI, args: ICTriangleFRev, args&: FNeedSub, args&: Dups));
1422 Enqueued = true;
1423 }
1424
1425 if (ValidSimple(TrueBBI&: FalseBBI, Dups, Prediction: Prediction.getCompl()) &&
1426 MeetIfcvtSizeLimit(BB&: *FalseBBI.BB,
1427 Cycle: FalseBBI.NonPredSize + FalseBBI.ExtraCost,
1428 Extra: FalseBBI.ExtraCost2, Prediction: Prediction.getCompl()) &&
1429 FeasibilityAnalysis(BBI&: FalseBBI, Pred&: RevCond)) {
1430 Tokens.push_back(
1431 x: std::make_unique<IfcvtToken>(args&: BBI, args: ICSimpleFalse, args&: FNeedSub, args&: Dups));
1432 Enqueued = true;
1433 }
1434 }
1435
1436 BBI.IsEnqueued = Enqueued;
1437 BBI.IsBeingAnalyzed = false;
1438 BBI.IsAnalyzed = true;
1439 BBStack.pop_back();
1440 }
1441}
1442
1443/// Analyze all blocks and find entries for all if-conversion candidates.
1444void IfConverter::AnalyzeBlocks(
1445 MachineFunction &MF, std::vector<std::unique_ptr<IfcvtToken>> &Tokens) {
1446 for (MachineBasicBlock &MBB : MF)
1447 AnalyzeBlock(MBB, Tokens);
1448
1449 // Sort to favor more complex ifcvt scheme.
1450 llvm::stable_sort(Range&: Tokens, C: IfcvtTokenCmp);
1451}
1452
1453/// Returns true either if ToMBB is the next block after MBB or that all the
1454/// intervening blocks are empty (given MBB can fall through to its next block).
1455static bool canFallThroughTo(MachineBasicBlock &MBB, MachineBasicBlock &ToMBB) {
1456 MachineFunction::iterator PI = MBB.getIterator();
1457 MachineFunction::iterator I = std::next(x: PI);
1458 MachineFunction::iterator TI = ToMBB.getIterator();
1459 MachineFunction::iterator E = MBB.getParent()->end();
1460 while (I != TI) {
1461 // Check isSuccessor to avoid case where the next block is empty, but
1462 // it's not a successor.
1463 if (I == E || !I->empty() || !PI->isSuccessor(MBB: &*I))
1464 return false;
1465 PI = I++;
1466 }
1467 // Finally see if the last I is indeed a successor to PI.
1468 return PI->isSuccessor(MBB: &*I);
1469}
1470
1471/// Invalidate predecessor BB info so it would be re-analyzed to determine if it
1472/// can be if-converted. If predecessor is already enqueued, dequeue it!
1473void IfConverter::InvalidatePreds(MachineBasicBlock &MBB) {
1474 for (const MachineBasicBlock *Predecessor : MBB.predecessors()) {
1475 BBInfo &PBBI = BBAnalysis[Predecessor->getNumber()];
1476 if (PBBI.IsDone || PBBI.BB == &MBB)
1477 continue;
1478 PBBI.IsAnalyzed = false;
1479 PBBI.IsEnqueued = false;
1480 }
1481}
1482
1483/// Inserts an unconditional branch from \p MBB to \p ToMBB.
1484static void InsertUncondBranch(MachineBasicBlock &MBB, MachineBasicBlock &ToMBB,
1485 const TargetInstrInfo *TII) {
1486 DebugLoc dl; // FIXME: this is nowhere
1487 SmallVector<MachineOperand, 0> NoCond;
1488 TII->insertBranch(MBB, TBB: &ToMBB, FBB: nullptr, Cond: NoCond, DL: dl);
1489}
1490
1491/// Behaves like LiveRegUnits::StepForward() but also adds implicit uses to all
1492/// values defined in MI which are also live/used by MI.
1493static void UpdatePredRedefs(MachineInstr &MI, LivePhysRegs &Redefs) {
1494 const TargetRegisterInfo *TRI = MI.getMF()->getSubtarget().getRegisterInfo();
1495
1496 // Before stepping forward past MI, remember which regs were live
1497 // before MI. This is needed to set the Undef flag only when reg is
1498 // dead.
1499 SparseSet<MCPhysReg, MCPhysReg> LiveBeforeMI;
1500 LiveBeforeMI.setUniverse(TRI->getNumRegs());
1501 for (unsigned Reg : Redefs)
1502 LiveBeforeMI.insert(Val: Reg);
1503
1504 SmallVector<std::pair<MCPhysReg, const MachineOperand*>, 4> Clobbers;
1505 Redefs.stepForward(MI, Clobbers);
1506
1507 // Now add the implicit uses for each of the clobbered values.
1508 for (auto Clobber : Clobbers) {
1509 // FIXME: Const cast here is nasty, but better than making StepForward
1510 // take a mutable instruction instead of const.
1511 unsigned Reg = Clobber.first;
1512 MachineOperand &Op = const_cast<MachineOperand&>(*Clobber.second);
1513 MachineInstr *OpMI = Op.getParent();
1514 MachineInstrBuilder MIB(*OpMI->getMF(), OpMI);
1515 if (Op.isRegMask()) {
1516 // First handle regmasks. They clobber any entries in the mask which
1517 // means that we need a def for those registers.
1518 if (LiveBeforeMI.count(Key: Reg))
1519 MIB.addReg(RegNo: Reg, Flags: RegState::Implicit);
1520
1521 // We also need to add an implicit def of this register for the later
1522 // use to read from.
1523 // For the register allocator to have allocated a register clobbered
1524 // by the call which is used later, it must be the case that
1525 // the call doesn't return.
1526 MIB.addReg(RegNo: Reg, Flags: RegState::Implicit | RegState::Define);
1527 continue;
1528 }
1529 if (any_of(Range: TRI->subregs_inclusive(Reg),
1530 P: [&](MCPhysReg S) { return LiveBeforeMI.count(Key: S); }))
1531 MIB.addReg(RegNo: Reg, Flags: RegState::Implicit);
1532 }
1533}
1534
1535/// If convert a simple (split, no rejoin) sub-CFG.
1536bool IfConverter::IfConvertSimple(BBInfo &BBI, IfcvtKind Kind) {
1537 BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
1538 BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
1539 BBInfo *CvtBBI = &TrueBBI;
1540 BBInfo *NextBBI = &FalseBBI;
1541
1542 SmallVector<MachineOperand, 4> Cond(BBI.BrCond.begin(), BBI.BrCond.end());
1543 if (Kind == ICSimpleFalse)
1544 std::swap(a&: CvtBBI, b&: NextBBI);
1545
1546 MachineBasicBlock &CvtMBB = *CvtBBI->BB;
1547 MachineBasicBlock &NextMBB = *NextBBI->BB;
1548 if (CvtBBI->IsDone ||
1549 (CvtBBI->CannotBeCopied && CvtMBB.pred_size() > 1)) {
1550 // Something has changed. It's no longer safe to predicate this block.
1551 BBI.IsAnalyzed = false;
1552 CvtBBI->IsAnalyzed = false;
1553 return false;
1554 }
1555
1556 if (CvtMBB.hasAddressTaken())
1557 // Conservatively abort if-conversion if BB's address is taken.
1558 return false;
1559
1560 if (Kind == ICSimpleFalse)
1561 if (TII->reverseBranchCondition(Cond))
1562 llvm_unreachable("Unable to reverse branch condition!");
1563
1564 Redefs.init(TRI: *TRI);
1565
1566 if (MRI->tracksLiveness()) {
1567 // Initialize liveins to the first BB. These are potentially redefined by
1568 // predicated instructions.
1569 Redefs.addLiveInsNoPristines(MBB: CvtMBB);
1570 Redefs.addLiveInsNoPristines(MBB: NextMBB);
1571 }
1572
1573 // Remove the branches from the entry so we can add the contents of the true
1574 // block to it.
1575 BBI.NonPredSize -= TII->removeBranch(MBB&: *BBI.BB);
1576
1577 if (CvtMBB.pred_size() > 1) {
1578 // Copy instructions in the true block, predicate them, and add them to
1579 // the entry block.
1580 CopyAndPredicateBlock(ToBBI&: BBI, FromBBI&: *CvtBBI, Cond);
1581
1582 // Keep the CFG updated.
1583 BBI.BB->removeSuccessor(Succ: &CvtMBB, NormalizeSuccProbs: true);
1584 } else {
1585 // Predicate the instructions in the true block.
1586 PredicateBlock(BBI&: *CvtBBI, E: CvtMBB.end(), Cond);
1587
1588 // Merge converted block into entry block. The BB to Cvt edge is removed
1589 // by MergeBlocks.
1590 MergeBlocks(ToBBI&: BBI, FromBBI&: *CvtBBI);
1591 }
1592
1593 bool IterIfcvt = true;
1594 if (!canFallThroughTo(MBB&: *BBI.BB, ToMBB&: NextMBB)) {
1595 InsertUncondBranch(MBB&: *BBI.BB, ToMBB&: NextMBB, TII);
1596 BBI.HasFallThrough = false;
1597 // Now ifcvt'd block will look like this:
1598 // BB:
1599 // ...
1600 // t, f = cmp
1601 // if t op
1602 // b BBf
1603 //
1604 // We cannot further ifcvt this block because the unconditional branch
1605 // will have to be predicated on the new condition, that will not be
1606 // available if cmp executes.
1607 IterIfcvt = false;
1608 }
1609
1610 // Update block info. BB can be iteratively if-converted.
1611 if (!IterIfcvt)
1612 BBI.IsDone = true;
1613 InvalidatePreds(MBB&: *BBI.BB);
1614 CvtBBI->IsDone = true;
1615
1616 // FIXME: Must maintain LiveIns.
1617 return true;
1618}
1619
1620/// If convert a triangle sub-CFG.
1621bool IfConverter::IfConvertTriangle(BBInfo &BBI, IfcvtKind Kind) {
1622 BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
1623 BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
1624 BBInfo *CvtBBI = &TrueBBI;
1625 BBInfo *NextBBI = &FalseBBI;
1626 DebugLoc dl; // FIXME: this is nowhere
1627
1628 SmallVector<MachineOperand, 4> Cond(BBI.BrCond.begin(), BBI.BrCond.end());
1629 if (Kind == ICTriangleFalse || Kind == ICTriangleFRev)
1630 std::swap(a&: CvtBBI, b&: NextBBI);
1631
1632 MachineBasicBlock &CvtMBB = *CvtBBI->BB;
1633 MachineBasicBlock &NextMBB = *NextBBI->BB;
1634 if (CvtBBI->IsDone ||
1635 (CvtBBI->CannotBeCopied && CvtMBB.pred_size() > 1)) {
1636 // Something has changed. It's no longer safe to predicate this block.
1637 BBI.IsAnalyzed = false;
1638 CvtBBI->IsAnalyzed = false;
1639 return false;
1640 }
1641
1642 if (CvtMBB.hasAddressTaken())
1643 // Conservatively abort if-conversion if BB's address is taken.
1644 return false;
1645
1646 if (Kind == ICTriangleFalse || Kind == ICTriangleFRev)
1647 if (TII->reverseBranchCondition(Cond))
1648 llvm_unreachable("Unable to reverse branch condition!");
1649
1650 if (Kind == ICTriangleRev || Kind == ICTriangleFRev) {
1651 if (reverseBranchCondition(BBI&: *CvtBBI)) {
1652 // BB has been changed, modify its predecessors (except for this
1653 // one) so they don't get ifcvt'ed based on bad intel.
1654 for (MachineBasicBlock *PBB : CvtMBB.predecessors()) {
1655 if (PBB == BBI.BB)
1656 continue;
1657 BBInfo &PBBI = BBAnalysis[PBB->getNumber()];
1658 if (PBBI.IsEnqueued) {
1659 PBBI.IsAnalyzed = false;
1660 PBBI.IsEnqueued = false;
1661 }
1662 }
1663 }
1664 }
1665
1666 // Initialize liveins to the first BB. These are potentially redefined by
1667 // predicated instructions.
1668 Redefs.init(TRI: *TRI);
1669 if (MRI->tracksLiveness()) {
1670 Redefs.addLiveInsNoPristines(MBB: CvtMBB);
1671 Redefs.addLiveInsNoPristines(MBB: NextMBB);
1672 }
1673
1674 bool HasEarlyExit = CvtBBI->FalseBB != nullptr;
1675 BranchProbability CvtNext, CvtFalse, BBNext, BBCvt;
1676
1677 if (HasEarlyExit) {
1678 // Get probabilities before modifying CvtMBB and BBI.BB.
1679 CvtNext = MBPI->getEdgeProbability(Src: &CvtMBB, Dst: &NextMBB);
1680 CvtFalse = MBPI->getEdgeProbability(Src: &CvtMBB, Dst: CvtBBI->FalseBB);
1681 BBNext = MBPI->getEdgeProbability(Src: BBI.BB, Dst: &NextMBB);
1682 BBCvt = MBPI->getEdgeProbability(Src: BBI.BB, Dst: &CvtMBB);
1683 }
1684
1685 // Remove the branches from the entry so we can add the contents of the true
1686 // block to it.
1687 BBI.NonPredSize -= TII->removeBranch(MBB&: *BBI.BB);
1688
1689 if (CvtMBB.pred_size() > 1) {
1690 // Copy instructions in the true block, predicate them, and add them to
1691 // the entry block.
1692 CopyAndPredicateBlock(ToBBI&: BBI, FromBBI&: *CvtBBI, Cond, IgnoreBr: true);
1693 } else {
1694 // Predicate the 'true' block after removing its branch.
1695 CvtBBI->NonPredSize -= TII->removeBranch(MBB&: CvtMBB);
1696 PredicateBlock(BBI&: *CvtBBI, E: CvtMBB.end(), Cond);
1697
1698 // Now merge the entry of the triangle with the true block.
1699 MergeBlocks(ToBBI&: BBI, FromBBI&: *CvtBBI, AddEdges: false);
1700 }
1701
1702 // Keep the CFG updated.
1703 BBI.BB->removeSuccessor(Succ: &CvtMBB, NormalizeSuccProbs: true);
1704
1705 // If 'true' block has a 'false' successor, add an exit branch to it.
1706 if (HasEarlyExit) {
1707 SmallVector<MachineOperand, 4> RevCond(CvtBBI->BrCond.begin(),
1708 CvtBBI->BrCond.end());
1709 if (TII->reverseBranchCondition(Cond&: RevCond))
1710 llvm_unreachable("Unable to reverse branch condition!");
1711
1712 // Update the edge probability for both CvtBBI->FalseBB and NextBBI.
1713 // NewNext = New_Prob(BBI.BB, NextMBB) =
1714 // Prob(BBI.BB, NextMBB) +
1715 // Prob(BBI.BB, CvtMBB) * Prob(CvtMBB, NextMBB)
1716 // NewFalse = New_Prob(BBI.BB, CvtBBI->FalseBB) =
1717 // Prob(BBI.BB, CvtMBB) * Prob(CvtMBB, CvtBBI->FalseBB)
1718 auto NewTrueBB = getNextBlock(MBB&: *BBI.BB);
1719 auto NewNext = BBNext + BBCvt * CvtNext;
1720 auto NewTrueBBIter = find(Range: BBI.BB->successors(), Val: NewTrueBB);
1721 if (NewTrueBBIter != BBI.BB->succ_end())
1722 BBI.BB->setSuccProbability(I: NewTrueBBIter, Prob: NewNext);
1723
1724 auto NewFalse = BBCvt * CvtFalse;
1725 TII->insertBranch(MBB&: *BBI.BB, TBB: CvtBBI->FalseBB, FBB: nullptr, Cond: RevCond, DL: dl);
1726 BBI.BB->addSuccessor(Succ: CvtBBI->FalseBB, Prob: NewFalse);
1727 }
1728
1729 // Merge in the 'false' block if the 'false' block has no other
1730 // predecessors. Otherwise, add an unconditional branch to 'false'.
1731 bool FalseBBDead = false;
1732 bool IterIfcvt = true;
1733 bool isFallThrough = canFallThroughTo(MBB&: *BBI.BB, ToMBB&: NextMBB);
1734 if (!isFallThrough) {
1735 // Only merge them if the true block does not fallthrough to the false
1736 // block. By not merging them, we make it possible to iteratively
1737 // ifcvt the blocks.
1738 if (!HasEarlyExit && NextMBB.pred_size() == 1 &&
1739 blockNeverFallThrough(BBI&: *NextBBI) && !NextMBB.hasAddressTaken()) {
1740 MergeBlocks(ToBBI&: BBI, FromBBI&: *NextBBI);
1741 FalseBBDead = true;
1742 } else {
1743 InsertUncondBranch(MBB&: *BBI.BB, ToMBB&: NextMBB, TII);
1744 BBI.HasFallThrough = false;
1745 }
1746 // Mixed predicated and unpredicated code. This cannot be iteratively
1747 // predicated.
1748 IterIfcvt = false;
1749 }
1750
1751 // Update block info. BB can be iteratively if-converted.
1752 if (!IterIfcvt)
1753 BBI.IsDone = true;
1754 InvalidatePreds(MBB&: *BBI.BB);
1755 CvtBBI->IsDone = true;
1756 if (FalseBBDead)
1757 NextBBI->IsDone = true;
1758
1759 // FIXME: Must maintain LiveIns.
1760 return true;
1761}
1762
1763/// Common code shared between diamond conversions.
1764/// \p BBI, \p TrueBBI, and \p FalseBBI form the diamond shape.
1765/// \p NumDups1 - number of shared instructions at the beginning of \p TrueBBI
1766/// and FalseBBI
1767/// \p NumDups2 - number of shared instructions at the end of \p TrueBBI
1768/// and \p FalseBBI
1769/// \p RemoveBranch - Remove the common branch of the two blocks before
1770/// predicating. Only false for unanalyzable fallthrough
1771/// cases. The caller will replace the branch if necessary.
1772/// \p MergeAddEdges - Add successor edges when merging blocks. Only false for
1773/// unanalyzable fallthrough
1774bool IfConverter::IfConvertDiamondCommon(
1775 BBInfo &BBI, BBInfo &TrueBBI, BBInfo &FalseBBI,
1776 unsigned NumDups1, unsigned NumDups2,
1777 bool TClobbersPred, bool FClobbersPred,
1778 bool RemoveBranch, bool MergeAddEdges) {
1779
1780 if (TrueBBI.IsDone || FalseBBI.IsDone ||
1781 TrueBBI.BB->pred_size() > 1 || FalseBBI.BB->pred_size() > 1) {
1782 // Something has changed. It's no longer safe to predicate these blocks.
1783 BBI.IsAnalyzed = false;
1784 TrueBBI.IsAnalyzed = false;
1785 FalseBBI.IsAnalyzed = false;
1786 return false;
1787 }
1788
1789 if (TrueBBI.BB->hasAddressTaken() || FalseBBI.BB->hasAddressTaken())
1790 // Conservatively abort if-conversion if either BB has its address taken.
1791 return false;
1792
1793 // Put the predicated instructions from the 'true' block before the
1794 // instructions from the 'false' block, unless the true block would clobber
1795 // the predicate, in which case, do the opposite.
1796 BBInfo *BBI1 = &TrueBBI;
1797 BBInfo *BBI2 = &FalseBBI;
1798 SmallVector<MachineOperand, 4> RevCond(BBI.BrCond.begin(), BBI.BrCond.end());
1799 if (TII->reverseBranchCondition(Cond&: RevCond))
1800 llvm_unreachable("Unable to reverse branch condition!");
1801 SmallVector<MachineOperand, 4> *Cond1 = &BBI.BrCond;
1802 SmallVector<MachineOperand, 4> *Cond2 = &RevCond;
1803
1804 // Figure out the more profitable ordering.
1805 bool DoSwap = false;
1806 if (TClobbersPred && !FClobbersPred)
1807 DoSwap = true;
1808 else if (!TClobbersPred && !FClobbersPred) {
1809 if (TrueBBI.NonPredSize > FalseBBI.NonPredSize)
1810 DoSwap = true;
1811 } else if (TClobbersPred && FClobbersPred)
1812 llvm_unreachable("Predicate info cannot be clobbered by both sides.");
1813 if (DoSwap) {
1814 std::swap(a&: BBI1, b&: BBI2);
1815 std::swap(a&: Cond1, b&: Cond2);
1816 }
1817
1818 // Remove the conditional branch from entry to the blocks.
1819 BBI.NonPredSize -= TII->removeBranch(MBB&: *BBI.BB);
1820
1821 MachineBasicBlock &MBB1 = *BBI1->BB;
1822 MachineBasicBlock &MBB2 = *BBI2->BB;
1823
1824 // Initialize the Redefs:
1825 // - BB2 live-in regs need implicit uses before being redefined by BB1
1826 // instructions.
1827 // - BB1 live-out regs need implicit uses before being redefined by BB2
1828 // instructions. We start with BB1 live-ins so we have the live-out regs
1829 // after tracking the BB1 instructions.
1830 Redefs.init(TRI: *TRI);
1831 if (MRI->tracksLiveness()) {
1832 Redefs.addLiveInsNoPristines(MBB: MBB1);
1833 Redefs.addLiveInsNoPristines(MBB: MBB2);
1834 }
1835
1836 // Remove the duplicated instructions at the beginnings of both paths.
1837 // Skip dbg_value instructions.
1838 MachineBasicBlock::iterator DI1 = MBB1.getFirstNonDebugInstr(SkipPseudoOp: false);
1839 MachineBasicBlock::iterator DI2 = MBB2.getFirstNonDebugInstr(SkipPseudoOp: false);
1840 BBI1->NonPredSize -= NumDups1;
1841 BBI2->NonPredSize -= NumDups1;
1842
1843 // Skip past the dups on each side separately since there may be
1844 // differing dbg_value entries. NumDups1 can include a "return"
1845 // instruction, if it's not marked as "branch".
1846 for (unsigned i = 0; i < NumDups1; ++DI1) {
1847 if (DI1 == MBB1.end())
1848 break;
1849 if (!DI1->isDebugInstr())
1850 ++i;
1851 }
1852 while (NumDups1 != 0) {
1853 // Since this instruction is going to be deleted, update call
1854 // info state if the instruction is call instruction.
1855 if (DI2->shouldUpdateAdditionalCallInfo())
1856 MBB2.getParent()->eraseAdditionalCallInfo(MI: &*DI2);
1857
1858 ++DI2;
1859 if (DI2 == MBB2.end())
1860 break;
1861 if (!DI2->isDebugInstr())
1862 --NumDups1;
1863 }
1864
1865 if (MRI->tracksLiveness()) {
1866 for (const MachineInstr &MI : make_range(x: MBB1.begin(), y: DI1)) {
1867 SmallVector<std::pair<MCPhysReg, const MachineOperand*>, 4> Dummy;
1868 Redefs.stepForward(MI, Clobbers&: Dummy);
1869 }
1870 }
1871
1872 BBI.BB->splice(Where: BBI.BB->end(), Other: &MBB1, From: MBB1.begin(), To: DI1);
1873 MBB2.erase(I: MBB2.begin(), E: DI2);
1874
1875 // The branches have been checked to match, so it is safe to remove the
1876 // branch in BB1 and rely on the copy in BB2. The complication is that
1877 // the blocks may end with a return instruction, which may or may not
1878 // be marked as "branch". If it's not, then it could be included in
1879 // "dups1", leaving the blocks potentially empty after moving the common
1880 // duplicates.
1881#ifndef NDEBUG
1882 // Unanalyzable branches must match exactly. Check that now.
1883 if (!BBI1->IsBrAnalyzable)
1884 verifySameBranchInstructions(&MBB1, &MBB2);
1885#endif
1886 // Remove duplicated instructions from the tail of MBB1: any branch
1887 // instructions, and the common instructions counted by NumDups2.
1888 DI1 = MBB1.end();
1889 while (DI1 != MBB1.begin()) {
1890 MachineBasicBlock::iterator Prev = std::prev(x: DI1);
1891 if (!Prev->isBranch() && !Prev->isDebugInstr())
1892 break;
1893 DI1 = Prev;
1894 }
1895 for (unsigned i = 0; i != NumDups2; ) {
1896 // NumDups2 only counted non-dbg_value instructions, so this won't
1897 // run off the head of the list.
1898 assert(DI1 != MBB1.begin());
1899
1900 --DI1;
1901
1902 // Since this instruction is going to be deleted, update call
1903 // info state if the instruction is call instruction.
1904 if (DI1->shouldUpdateAdditionalCallInfo())
1905 MBB1.getParent()->eraseAdditionalCallInfo(MI: &*DI1);
1906
1907 // skip dbg_value instructions
1908 if (!DI1->isDebugInstr())
1909 ++i;
1910 }
1911 MBB1.erase(I: DI1, E: MBB1.end());
1912
1913 DI2 = BBI2->BB->end();
1914 // The branches have been checked to match. Skip over the branch in the false
1915 // block so that we don't try to predicate it.
1916 if (RemoveBranch)
1917 BBI2->NonPredSize -= TII->removeBranch(MBB&: *BBI2->BB);
1918 else {
1919 // Make DI2 point to the end of the range where the common "tail"
1920 // instructions could be found.
1921 while (DI2 != MBB2.begin()) {
1922 MachineBasicBlock::iterator Prev = std::prev(x: DI2);
1923 if (!Prev->isBranch() && !Prev->isDebugInstr())
1924 break;
1925 DI2 = Prev;
1926 }
1927 }
1928 while (NumDups2 != 0) {
1929 // NumDups2 only counted non-dbg_value instructions, so this won't
1930 // run off the head of the list.
1931 assert(DI2 != MBB2.begin());
1932 --DI2;
1933 // skip dbg_value instructions
1934 if (!DI2->isDebugInstr())
1935 --NumDups2;
1936 }
1937
1938 // Remember which registers would later be defined by the false block.
1939 // This allows us not to predicate instructions in the true block that would
1940 // later be re-defined. That is, rather than
1941 // subeq r0, r1, #1
1942 // addne r0, r1, #1
1943 // generate:
1944 // sub r0, r1, #1
1945 // addne r0, r1, #1
1946 SmallSet<MCRegister, 4> RedefsByFalse;
1947 SmallSet<MCRegister, 4> ExtUses;
1948 if (TII->isProfitableToUnpredicate(TMBB&: MBB1, FMBB&: MBB2)) {
1949 for (const MachineInstr &FI : make_range(x: MBB2.begin(), y: DI2)) {
1950 if (FI.isDebugInstr())
1951 continue;
1952 SmallVector<MCRegister, 4> Defs;
1953 for (const MachineOperand &MO : FI.operands()) {
1954 if (!MO.isReg())
1955 continue;
1956 Register Reg = MO.getReg();
1957 if (!Reg)
1958 continue;
1959 if (MO.isDef()) {
1960 Defs.push_back(Elt: Reg);
1961 } else if (!RedefsByFalse.count(V: Reg)) {
1962 // These are defined before ctrl flow reach the 'false' instructions.
1963 // They cannot be modified by the 'true' instructions.
1964 ExtUses.insert_range(R: TRI->subregs_inclusive(Reg));
1965 }
1966 }
1967
1968 for (MCRegister Reg : Defs) {
1969 if (!ExtUses.contains(V: Reg))
1970 RedefsByFalse.insert_range(R: TRI->subregs_inclusive(Reg));
1971 }
1972 }
1973 }
1974
1975 // Predicate the 'true' block.
1976 PredicateBlock(BBI&: *BBI1, E: MBB1.end(), Cond&: *Cond1, LaterRedefs: &RedefsByFalse);
1977
1978 // After predicating BBI1, if there is a predicated terminator in BBI1 and
1979 // a non-predicated in BBI2, then we don't want to predicate the one from
1980 // BBI2. The reason is that if we merged these blocks, we would end up with
1981 // two predicated terminators in the same block.
1982 // Also, if the branches in MBB1 and MBB2 were non-analyzable, then don't
1983 // predicate them either. They were checked to be identical, and so the
1984 // same branch would happen regardless of which path was taken.
1985 if (!MBB2.empty() && (DI2 == MBB2.end())) {
1986 MachineBasicBlock::iterator BBI1T = MBB1.getFirstTerminator();
1987 MachineBasicBlock::iterator BBI2T = MBB2.getFirstTerminator();
1988 bool BB1Predicated = BBI1T != MBB1.end() && TII->isPredicated(MI: *BBI1T);
1989 bool BB2NonPredicated = BBI2T != MBB2.end() && !TII->isPredicated(MI: *BBI2T);
1990 if (BB2NonPredicated && (BB1Predicated || !BBI2->IsBrAnalyzable))
1991 --DI2;
1992 }
1993
1994 // Predicate the 'false' block.
1995 PredicateBlock(BBI&: *BBI2, E: DI2, Cond&: *Cond2);
1996
1997 // Merge the true block into the entry of the diamond.
1998 MergeBlocks(ToBBI&: BBI, FromBBI&: *BBI1, AddEdges: MergeAddEdges);
1999 MergeBlocks(ToBBI&: BBI, FromBBI&: *BBI2, AddEdges: MergeAddEdges);
2000 return true;
2001}
2002
2003/// If convert an almost-diamond sub-CFG where the true
2004/// and false blocks share a common tail.
2005bool IfConverter::IfConvertForkedDiamond(
2006 BBInfo &BBI, IfcvtKind Kind,
2007 unsigned NumDups1, unsigned NumDups2,
2008 bool TClobbersPred, bool FClobbersPred) {
2009 BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
2010 BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
2011
2012 // Save the debug location for later.
2013 DebugLoc dl;
2014 MachineBasicBlock::iterator TIE = TrueBBI.BB->getFirstTerminator();
2015 if (TIE != TrueBBI.BB->end())
2016 dl = TIE->getDebugLoc();
2017 // Removing branches from both blocks is safe, because we have already
2018 // determined that both blocks have the same branch instructions. The branch
2019 // will be added back at the end, unpredicated.
2020 if (!IfConvertDiamondCommon(
2021 BBI, TrueBBI, FalseBBI,
2022 NumDups1, NumDups2,
2023 TClobbersPred, FClobbersPred,
2024 /* RemoveBranch */ true, /* MergeAddEdges */ true))
2025 return false;
2026
2027 // Add back the branch.
2028 // Debug location saved above when removing the branch from BBI2
2029 TII->insertBranch(MBB&: *BBI.BB, TBB: TrueBBI.TrueBB, FBB: TrueBBI.FalseBB,
2030 Cond: TrueBBI.BrCond, DL: dl);
2031
2032 // Update block info.
2033 BBI.IsDone = TrueBBI.IsDone = FalseBBI.IsDone = true;
2034 InvalidatePreds(MBB&: *BBI.BB);
2035
2036 // FIXME: Must maintain LiveIns.
2037 return true;
2038}
2039
2040/// If convert a diamond sub-CFG.
2041bool IfConverter::IfConvertDiamond(BBInfo &BBI, IfcvtKind Kind,
2042 unsigned NumDups1, unsigned NumDups2,
2043 bool TClobbersPred, bool FClobbersPred) {
2044 BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
2045 BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
2046 MachineBasicBlock *TailBB = TrueBBI.TrueBB;
2047
2048 // True block must fall through or end with an unanalyzable terminator.
2049 if (!TailBB) {
2050 if (blockAlwaysFallThrough(BBI&: TrueBBI))
2051 TailBB = FalseBBI.TrueBB;
2052 assert((TailBB || !TrueBBI.IsBrAnalyzable) && "Unexpected!");
2053 }
2054
2055 if (!IfConvertDiamondCommon(
2056 BBI, TrueBBI, FalseBBI,
2057 NumDups1, NumDups2,
2058 TClobbersPred, FClobbersPred,
2059 /* RemoveBranch */ TrueBBI.IsBrAnalyzable,
2060 /* MergeAddEdges */ TailBB == nullptr))
2061 return false;
2062
2063 // If the if-converted block falls through or unconditionally branches into
2064 // the tail block, and the tail block does not have other predecessors, then
2065 // fold the tail block in as well. Otherwise, unless it falls through to the
2066 // tail, add a unconditional branch to it.
2067 if (TailBB) {
2068 // We need to remove the edges to the true and false blocks manually since
2069 // we didn't let IfConvertDiamondCommon update the CFG.
2070 BBI.BB->removeSuccessor(Succ: TrueBBI.BB);
2071 BBI.BB->removeSuccessor(Succ: FalseBBI.BB, NormalizeSuccProbs: true);
2072
2073 BBInfo &TailBBI = BBAnalysis[TailBB->getNumber()];
2074 bool CanMergeTail =
2075 blockNeverFallThrough(BBI&: TailBBI) && !TailBBI.BB->hasAddressTaken();
2076 // The if-converted block can still have a predicated terminator
2077 // (e.g. a predicated return). If that is the case, we cannot merge
2078 // it with the tail block.
2079 MachineBasicBlock::const_iterator TI = BBI.BB->getFirstTerminator();
2080 if (TI != BBI.BB->end() && TII->isPredicated(MI: *TI))
2081 CanMergeTail = false;
2082 // There may still be a fall-through edge from BBI1 or BBI2 to TailBB;
2083 // check if there are any other predecessors besides those.
2084 unsigned NumPreds = TailBB->pred_size();
2085 if (NumPreds > 1)
2086 CanMergeTail = false;
2087 else if (NumPreds == 1 && CanMergeTail) {
2088 MachineBasicBlock::pred_iterator PI = TailBB->pred_begin();
2089 if (*PI != TrueBBI.BB && *PI != FalseBBI.BB)
2090 CanMergeTail = false;
2091 }
2092 if (CanMergeTail) {
2093 MergeBlocks(ToBBI&: BBI, FromBBI&: TailBBI);
2094 TailBBI.IsDone = true;
2095 } else {
2096 BBI.BB->addSuccessor(Succ: TailBB, Prob: BranchProbability::getOne());
2097 InsertUncondBranch(MBB&: *BBI.BB, ToMBB&: *TailBB, TII);
2098 BBI.HasFallThrough = false;
2099 }
2100 }
2101
2102 // Update block info.
2103 BBI.IsDone = TrueBBI.IsDone = FalseBBI.IsDone = true;
2104 InvalidatePreds(MBB&: *BBI.BB);
2105
2106 // FIXME: Must maintain LiveIns.
2107 return true;
2108}
2109
2110static bool MaySpeculate(const MachineInstr &MI,
2111 SmallSet<MCRegister, 4> &LaterRedefs) {
2112 bool SawStore = true;
2113 if (!MI.isSafeToMove(SawStore))
2114 return false;
2115
2116 for (const MachineOperand &MO : MI.operands()) {
2117 if (!MO.isReg())
2118 continue;
2119 Register Reg = MO.getReg();
2120 if (!Reg)
2121 continue;
2122 if (MO.isDef() && !LaterRedefs.count(V: Reg))
2123 return false;
2124 }
2125
2126 return true;
2127}
2128
2129/// Predicate instructions from the start of the block to the specified end with
2130/// the specified condition.
2131void IfConverter::PredicateBlock(BBInfo &BBI, MachineBasicBlock::iterator E,
2132 SmallVectorImpl<MachineOperand> &Cond,
2133 SmallSet<MCRegister, 4> *LaterRedefs) {
2134 bool AnyUnpred = false;
2135 bool MaySpec = LaterRedefs != nullptr;
2136 for (MachineInstr &I : make_range(x: BBI.BB->begin(), y: E)) {
2137 if (I.isDebugInstr() || TII->isPredicated(MI: I))
2138 continue;
2139 // It may be possible not to predicate an instruction if it's the 'true'
2140 // side of a diamond and the 'false' side may re-define the instruction's
2141 // defs.
2142 if (MaySpec && MaySpeculate(MI: I, LaterRedefs&: *LaterRedefs)) {
2143 AnyUnpred = true;
2144 continue;
2145 }
2146 // If any instruction is predicated, then every instruction after it must
2147 // be predicated.
2148 MaySpec = false;
2149 if (!TII->PredicateInstruction(MI&: I, Pred: Cond)) {
2150#ifndef NDEBUG
2151 dbgs() << "Unable to predicate " << I << "!\n";
2152#endif
2153 llvm_unreachable(nullptr);
2154 }
2155
2156 // If the predicated instruction now redefines a register as the result of
2157 // if-conversion, add an implicit kill.
2158 UpdatePredRedefs(MI&: I, Redefs);
2159 }
2160
2161 BBI.Predicate.append(in_start: Cond.begin(), in_end: Cond.end());
2162
2163 BBI.IsAnalyzed = false;
2164 BBI.NonPredSize = 0;
2165
2166 ++NumIfConvBBs;
2167 if (AnyUnpred)
2168 ++NumUnpred;
2169}
2170
2171/// Copy and predicate instructions from source BB to the destination block.
2172/// Skip end of block branches if IgnoreBr is true.
2173void IfConverter::CopyAndPredicateBlock(BBInfo &ToBBI, BBInfo &FromBBI,
2174 SmallVectorImpl<MachineOperand> &Cond,
2175 bool IgnoreBr) {
2176 MachineFunction &MF = *ToBBI.BB->getParent();
2177
2178 MachineBasicBlock &FromMBB = *FromBBI.BB;
2179 for (MachineInstr &I : FromMBB) {
2180 // Do not copy the end of the block branches.
2181 if (IgnoreBr && I.isBranch())
2182 break;
2183
2184 MachineInstr *MI = MF.CloneMachineInstr(Orig: &I);
2185 // Make a copy of the call info.
2186 if (I.isCandidateForAdditionalCallInfo())
2187 MF.copyAdditionalCallInfo(Old: &I, New: MI);
2188
2189 ToBBI.BB->insert(I: ToBBI.BB->end(), MI);
2190 ToBBI.NonPredSize++;
2191 unsigned ExtraPredCost = TII->getPredicationCost(MI: I);
2192 unsigned NumCycles = SchedModel.computeInstrLatency(MI: &I, UseDefaultDefLatency: false);
2193 if (NumCycles > 1)
2194 ToBBI.ExtraCost += NumCycles-1;
2195 ToBBI.ExtraCost2 += ExtraPredCost;
2196
2197 if (!TII->isPredicated(MI: I) && !MI->isDebugInstr()) {
2198 if (!TII->PredicateInstruction(MI&: *MI, Pred: Cond)) {
2199#ifndef NDEBUG
2200 dbgs() << "Unable to predicate " << I << "!\n";
2201#endif
2202 llvm_unreachable(nullptr);
2203 }
2204 }
2205
2206 // If the predicated instruction now redefines a register as the result of
2207 // if-conversion, add an implicit kill.
2208 UpdatePredRedefs(MI&: *MI, Redefs);
2209 }
2210
2211 if (!IgnoreBr) {
2212 std::vector<MachineBasicBlock *> Succs(FromMBB.succ_begin(),
2213 FromMBB.succ_end());
2214 MachineBasicBlock *NBB = getNextBlock(MBB&: FromMBB);
2215 MachineBasicBlock *FallThrough = FromBBI.HasFallThrough ? NBB : nullptr;
2216
2217 for (MachineBasicBlock *Succ : Succs) {
2218 // Fallthrough edge can't be transferred.
2219 if (Succ == FallThrough)
2220 continue;
2221 ToBBI.BB->addSuccessor(Succ);
2222 }
2223 }
2224
2225 ToBBI.Predicate.append(in_start: FromBBI.Predicate.begin(), in_end: FromBBI.Predicate.end());
2226 ToBBI.Predicate.append(in_start: Cond.begin(), in_end: Cond.end());
2227
2228 ToBBI.ClobbersPred |= FromBBI.ClobbersPred;
2229 ToBBI.IsAnalyzed = false;
2230
2231 ++NumDupBBs;
2232}
2233
2234/// Move all instructions from FromBB to the end of ToBB. This will leave
2235/// FromBB as an empty block, so remove all of its successor edges and move it
2236/// to the end of the function. If AddEdges is true, i.e., when FromBBI's
2237/// branch is being moved, add those successor edges to ToBBI and remove the old
2238/// edge from ToBBI to FromBBI.
2239void IfConverter::MergeBlocks(BBInfo &ToBBI, BBInfo &FromBBI, bool AddEdges) {
2240 MachineBasicBlock &FromMBB = *FromBBI.BB;
2241 assert(!FromMBB.hasAddressTaken() &&
2242 "Removing a BB whose address is taken!");
2243
2244 // If we're about to splice an INLINEASM_BR from FromBBI, we need to update
2245 // ToBBI's successor list accordingly.
2246 if (FromMBB.mayHaveInlineAsmBr())
2247 for (MachineInstr &MI : FromMBB)
2248 if (MI.getOpcode() == TargetOpcode::INLINEASM_BR)
2249 for (MachineOperand &MO : MI.operands())
2250 if (MO.isMBB() && !ToBBI.BB->isSuccessor(MBB: MO.getMBB()))
2251 ToBBI.BB->addSuccessor(Succ: MO.getMBB(), Prob: BranchProbability::getZero());
2252
2253 // In case FromMBB contains terminators (e.g. return instruction),
2254 // first move the non-terminator instructions, then the terminators.
2255 MachineBasicBlock::iterator FromTI = FromMBB.getFirstTerminator();
2256 MachineBasicBlock::iterator ToTI = ToBBI.BB->getFirstTerminator();
2257 ToBBI.BB->splice(Where: ToTI, Other: &FromMBB, From: FromMBB.begin(), To: FromTI);
2258
2259 // If FromBB has non-predicated terminator we should copy it at the end.
2260 if (FromTI != FromMBB.end() && !TII->isPredicated(MI: *FromTI))
2261 ToTI = ToBBI.BB->end();
2262 ToBBI.BB->splice(Where: ToTI, Other: &FromMBB, From: FromTI, To: FromMBB.end());
2263
2264 // Force normalizing the successors' probabilities of ToBBI.BB to convert all
2265 // unknown probabilities into known ones.
2266 // FIXME: This usage is too tricky and in the future we would like to
2267 // eliminate all unknown probabilities in MBB.
2268 if (ToBBI.IsBrAnalyzable)
2269 ToBBI.BB->normalizeSuccProbs();
2270
2271 SmallVector<MachineBasicBlock *, 4> FromSuccs(FromMBB.successors());
2272 MachineBasicBlock *NBB = getNextBlock(MBB&: FromMBB);
2273 MachineBasicBlock *FallThrough = FromBBI.HasFallThrough ? NBB : nullptr;
2274 // The edge probability from ToBBI.BB to FromMBB, which is only needed when
2275 // AddEdges is true and FromMBB is a successor of ToBBI.BB.
2276 auto To2FromProb = BranchProbability::getZero();
2277 if (AddEdges && ToBBI.BB->isSuccessor(MBB: &FromMBB)) {
2278 // Remove the old edge but remember the edge probability so we can calculate
2279 // the correct weights on the new edges being added further down.
2280 To2FromProb = MBPI->getEdgeProbability(Src: ToBBI.BB, Dst: &FromMBB);
2281 ToBBI.BB->removeSuccessor(Succ: &FromMBB);
2282 }
2283
2284 for (MachineBasicBlock *Succ : FromSuccs) {
2285 // Fallthrough edge can't be transferred.
2286 if (Succ == FallThrough) {
2287 FromMBB.removeSuccessor(Succ);
2288 continue;
2289 }
2290
2291 auto NewProb = BranchProbability::getZero();
2292 if (AddEdges) {
2293 // Calculate the edge probability for the edge from ToBBI.BB to Succ,
2294 // which is a portion of the edge probability from FromMBB to Succ. The
2295 // portion ratio is the edge probability from ToBBI.BB to FromMBB (if
2296 // FromBBI is a successor of ToBBI.BB. See comment below for exception).
2297 NewProb = MBPI->getEdgeProbability(Src: &FromMBB, Dst: Succ);
2298
2299 // To2FromProb is 0 when FromMBB is not a successor of ToBBI.BB. This
2300 // only happens when if-converting a diamond CFG and FromMBB is the
2301 // tail BB. In this case FromMBB post-dominates ToBBI.BB and hence we
2302 // could just use the probabilities on FromMBB's out-edges when adding
2303 // new successors.
2304 if (!To2FromProb.isZero())
2305 NewProb *= To2FromProb;
2306 }
2307
2308 FromMBB.removeSuccessor(Succ);
2309
2310 if (AddEdges) {
2311 // If the edge from ToBBI.BB to Succ already exists, update the
2312 // probability of this edge by adding NewProb to it. An example is shown
2313 // below, in which A is ToBBI.BB and B is FromMBB. In this case we
2314 // don't have to set C as A's successor as it already is. We only need to
2315 // update the edge probability on A->C. Note that B will not be
2316 // immediately removed from A's successors. It is possible that B->D is
2317 // not removed either if D is a fallthrough of B. Later the edge A->D
2318 // (generated here) and B->D will be combined into one edge. To maintain
2319 // correct edge probability of this combined edge, we need to set the edge
2320 // probability of A->B to zero, which is already done above. The edge
2321 // probability on A->D is calculated by scaling the original probability
2322 // on A->B by the probability of B->D.
2323 //
2324 // Before ifcvt: After ifcvt (assume B->D is kept):
2325 //
2326 // A A
2327 // /| /|\
2328 // / B / B|
2329 // | /| | ||
2330 // |/ | | |/
2331 // C D C D
2332 //
2333 if (ToBBI.BB->isSuccessor(MBB: Succ))
2334 ToBBI.BB->setSuccProbability(
2335 I: find(Range: ToBBI.BB->successors(), Val: Succ),
2336 Prob: MBPI->getEdgeProbability(Src: ToBBI.BB, Dst: Succ) + NewProb);
2337 else
2338 ToBBI.BB->addSuccessor(Succ, Prob: NewProb);
2339 }
2340 }
2341
2342 // Move the now empty FromMBB out of the way to the end of the function so
2343 // it doesn't interfere with fallthrough checks done by canFallThroughTo().
2344 MachineBasicBlock *Last = &*FromMBB.getParent()->rbegin();
2345 if (Last != &FromMBB)
2346 FromMBB.moveAfter(NewBefore: Last);
2347
2348 // Normalize the probabilities of ToBBI.BB's successors with all adjustment
2349 // we've done above.
2350 if (ToBBI.IsBrAnalyzable && FromBBI.IsBrAnalyzable)
2351 ToBBI.BB->normalizeSuccProbs();
2352
2353 ToBBI.Predicate.append(in_start: FromBBI.Predicate.begin(), in_end: FromBBI.Predicate.end());
2354 FromBBI.Predicate.clear();
2355
2356 ToBBI.NonPredSize += FromBBI.NonPredSize;
2357 ToBBI.ExtraCost += FromBBI.ExtraCost;
2358 ToBBI.ExtraCost2 += FromBBI.ExtraCost2;
2359 FromBBI.NonPredSize = 0;
2360 FromBBI.ExtraCost = 0;
2361 FromBBI.ExtraCost2 = 0;
2362
2363 ToBBI.ClobbersPred |= FromBBI.ClobbersPred;
2364 ToBBI.HasFallThrough = FromBBI.HasFallThrough;
2365 ToBBI.IsAnalyzed = false;
2366 FromBBI.IsAnalyzed = false;
2367}
2368
2369FunctionPass *
2370llvm::createIfConverter(std::function<bool(const MachineFunction &)> Ftor) {
2371 return new IfConverter(std::move(Ftor));
2372}
2373