blob: a0ce254158326ddbe6b7e3b7adf1bd6cc1e8451b [file] [log] [blame]
Evan Chengf5e53a52007-05-16 02:00:57 +00001//===-- IfConversion.cpp - Machine code if conversion pass. ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Evan Chengf5e53a52007-05-16 02:00:57 +00007//
8//===----------------------------------------------------------------------===//
9//
Justin Lebaracc47102016-04-01 01:09:03 +000010// This file implements the machine instruction level if-conversion pass, which
11// tries to convert conditional branches into predicated instructions.
Evan Chengf5e53a52007-05-16 02:00:57 +000012//
13//===----------------------------------------------------------------------===//
14
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "BranchFolding.h"
16#include "llvm/ADT/STLExtras.h"
Kyle Buttd76755e2016-08-18 22:09:23 +000017#include "llvm/ADT/ScopeExit.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/ADT/SmallSet.h"
19#include "llvm/ADT/Statistic.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000020#include "llvm/CodeGen/LivePhysRegs.h"
Akira Hatanakabbd33f62014-08-07 19:30:13 +000021#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
Jakub Staszak3ef20e32011-08-03 22:53:41 +000022#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
Evan Chengf5e53a52007-05-16 02:00:57 +000023#include "llvm/CodeGen/MachineFunctionPass.h"
Jakob Stoklund Olesenf623e982012-12-20 18:08:06 +000024#include "llvm/CodeGen/MachineInstrBuilder.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/CodeGen/MachineModuleInfo.h"
Evan Chengc5adcca2012-06-08 21:53:50 +000026#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000027#include "llvm/CodeGen/Passes.h"
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +000028#include "llvm/CodeGen/TargetSchedule.h"
Evan Chenge93ccc02007-06-08 19:10:51 +000029#include "llvm/Support/CommandLine.h"
Evan Chengf5e53a52007-05-16 02:00:57 +000030#include "llvm/Support/Debug.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000031#include "llvm/Support/ErrorHandling.h"
32#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000033#include "llvm/Target/TargetInstrInfo.h"
34#include "llvm/Target/TargetLowering.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000035#include "llvm/Target/TargetRegisterInfo.h"
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +000036#include "llvm/Target/TargetSubtargetInfo.h"
Cong Houd97c1002015-12-01 05:29:22 +000037#include <algorithm>
Benjamin Kramer82de7d32016-05-27 14:27:24 +000038#include <utility>
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +000039
Evan Chengf5e53a52007-05-16 02:00:57 +000040using namespace llvm;
41
Matthias Braun1527baa2017-05-25 21:26:32 +000042#define DEBUG_TYPE "if-converter"
Chandler Carruth1b9dde02014-04-22 02:02:50 +000043
Chris Lattner769c86b2008-01-07 05:40:58 +000044// Hidden options for help debugging.
45static cl::opt<int> IfCvtFnStart("ifcvt-fn-start", cl::init(-1), cl::Hidden);
46static cl::opt<int> IfCvtFnStop("ifcvt-fn-stop", cl::init(-1), cl::Hidden);
47static cl::opt<int> IfCvtLimit("ifcvt-limit", cl::init(-1), cl::Hidden);
Bob Wilson81051442010-06-15 22:18:54 +000048static cl::opt<bool> DisableSimple("disable-ifcvt-simple",
Chris Lattner769c86b2008-01-07 05:40:58 +000049 cl::init(false), cl::Hidden);
Bob Wilson81051442010-06-15 22:18:54 +000050static cl::opt<bool> DisableSimpleF("disable-ifcvt-simple-false",
Chris Lattner769c86b2008-01-07 05:40:58 +000051 cl::init(false), cl::Hidden);
Bob Wilson81051442010-06-15 22:18:54 +000052static cl::opt<bool> DisableTriangle("disable-ifcvt-triangle",
Chris Lattner769c86b2008-01-07 05:40:58 +000053 cl::init(false), cl::Hidden);
Bob Wilson81051442010-06-15 22:18:54 +000054static cl::opt<bool> DisableTriangleR("disable-ifcvt-triangle-rev",
Chris Lattner769c86b2008-01-07 05:40:58 +000055 cl::init(false), cl::Hidden);
Bob Wilson81051442010-06-15 22:18:54 +000056static cl::opt<bool> DisableTriangleF("disable-ifcvt-triangle-false",
Chris Lattner769c86b2008-01-07 05:40:58 +000057 cl::init(false), cl::Hidden);
Bob Wilson81051442010-06-15 22:18:54 +000058static cl::opt<bool> DisableTriangleFR("disable-ifcvt-triangle-false-rev",
Chris Lattner769c86b2008-01-07 05:40:58 +000059 cl::init(false), cl::Hidden);
Bob Wilson81051442010-06-15 22:18:54 +000060static cl::opt<bool> DisableDiamond("disable-ifcvt-diamond",
Chris Lattner769c86b2008-01-07 05:40:58 +000061 cl::init(false), cl::Hidden);
Kyle Butta8c73712016-08-24 21:34:27 +000062static cl::opt<bool> DisableForkedDiamond("disable-ifcvt-forked-diamond",
63 cl::init(false), cl::Hidden);
Evan Chengf128bdc2010-06-16 07:35:02 +000064static cl::opt<bool> IfCvtBranchFold("ifcvt-branch-fold",
65 cl::init(true), cl::Hidden);
Evan Chenge93ccc02007-06-08 19:10:51 +000066
Evan Cheng2117d1f2007-06-09 01:03:43 +000067STATISTIC(NumSimple, "Number of simple if-conversions performed");
68STATISTIC(NumSimpleFalse, "Number of simple (F) if-conversions performed");
69STATISTIC(NumTriangle, "Number of triangle if-conversions performed");
Evan Cheng9acfa7b2007-06-12 23:54:05 +000070STATISTIC(NumTriangleRev, "Number of triangle (R) if-conversions performed");
Evan Cheng2117d1f2007-06-09 01:03:43 +000071STATISTIC(NumTriangleFalse,"Number of triangle (F) if-conversions performed");
72STATISTIC(NumTriangleFRev, "Number of triangle (F/R) if-conversions performed");
73STATISTIC(NumDiamonds, "Number of diamond if-conversions performed");
Kyle Butta8c73712016-08-24 21:34:27 +000074STATISTIC(NumForkedDiamonds, "Number of forked-diamond if-conversions performed");
Evan Cheng2117d1f2007-06-09 01:03:43 +000075STATISTIC(NumIfConvBBs, "Number of if-converted blocks");
Evan Cheng92fb5452007-06-15 07:36:12 +000076STATISTIC(NumDupBBs, "Number of duplicated blocks");
Evan Cheng4266a792011-12-19 22:01:30 +000077STATISTIC(NumUnpred, "Number of true blocks of diamonds unpredicated");
Evan Chengf5e53a52007-05-16 02:00:57 +000078
79namespace {
Nick Lewycky02d5f772009-10-25 06:33:48 +000080 class IfConverter : public MachineFunctionPass {
Evan Cheng3a51c852007-06-16 09:34:52 +000081 enum IfcvtKind {
Evan Chengf5e53a52007-05-16 02:00:57 +000082 ICNotClassfied, // BB data valid, but not classified.
Evan Cheng312b7232007-06-04 06:47:22 +000083 ICSimpleFalse, // Same as ICSimple, but on the false path.
Evan Cheng3a51c852007-06-16 09:34:52 +000084 ICSimple, // BB is entry of an one split, no rejoin sub-CFG.
85 ICTriangleFRev, // Same as ICTriangleFalse, but false path rev condition.
Evan Cheng9acfa7b2007-06-12 23:54:05 +000086 ICTriangleRev, // Same as ICTriangle, but true path rev condition.
Evan Cheng2117d1f2007-06-09 01:03:43 +000087 ICTriangleFalse, // Same as ICTriangle, but on the false path.
Evan Cheng3a51c852007-06-16 09:34:52 +000088 ICTriangle, // BB is entry of a triangle sub-CFG.
Kyle Butta8c73712016-08-24 21:34:27 +000089 ICDiamond, // BB is entry of a diamond sub-CFG.
90 ICForkedDiamond // BB is entry of an almost diamond sub-CFG, with a
91 // common tail that can be shared.
Evan Chengf5e53a52007-05-16 02:00:57 +000092 };
93
Matthias Braun2c931792016-08-17 02:51:57 +000094 /// One per MachineBasicBlock, this is used to cache the result
Evan Chengf5e53a52007-05-16 02:00:57 +000095 /// if-conversion feasibility analysis. This includes results from
Jacques Pienaar71c30a12016-07-15 14:41:04 +000096 /// TargetInstrInfo::analyzeBranch() (i.e. TBB, FBB, and Cond), and its
Evan Cheng0f745da2007-05-18 00:20:58 +000097 /// classification, and common tail block of its successors (if it's a
Evan Cheng2e82cef2007-05-18 18:14:37 +000098 /// diamond shape), its size, whether it's predicable, and whether any
99 /// instruction can clobber the 'would-be' predicate.
Evan Chengd0e66912007-05-23 07:23:16 +0000100 ///
Evan Cheng4dd31a72007-06-11 22:26:22 +0000101 /// IsDone - True if BB is not to be considered for ifcvt.
102 /// IsBeingAnalyzed - True if BB is currently being analyzed.
103 /// IsAnalyzed - True if BB has been analyzed (info is still valid).
104 /// IsEnqueued - True if BB has been enqueued to be ifcvt'ed.
Jacques Pienaar71c30a12016-07-15 14:41:04 +0000105 /// IsBrAnalyzable - True if analyzeBranch() returns false.
Evan Cheng4dd31a72007-06-11 22:26:22 +0000106 /// HasFallThrough - True if BB may fallthrough to the following BB.
107 /// IsUnpredicable - True if BB is known to be unpredicable.
Evan Chengfbc73092007-07-10 17:50:43 +0000108 /// ClobbersPred - True if BB could modify predicates (e.g. has
Evan Cheng9030b982007-06-06 10:16:17 +0000109 /// cmp, call, etc.)
Evan Cheng4dd31a72007-06-11 22:26:22 +0000110 /// NonPredSize - Number of non-predicated instructions.
Evan Chengdebf9c52010-11-03 00:45:17 +0000111 /// ExtraCost - Extra cost for multi-cycle instructions.
112 /// ExtraCost2 - Some instructions are slower when predicated
Evan Chengd0e66912007-05-23 07:23:16 +0000113 /// BB - Corresponding MachineBasicBlock.
Jacques Pienaar71c30a12016-07-15 14:41:04 +0000114 /// TrueBB / FalseBB- See analyzeBranch().
Evan Chengd0e66912007-05-23 07:23:16 +0000115 /// BrCond - Conditions for end of block conditional branches.
116 /// Predicate - Predicate used in the BB.
Evan Chengf5e53a52007-05-16 02:00:57 +0000117 struct BBInfo {
Evan Cheng4dd31a72007-06-11 22:26:22 +0000118 bool IsDone : 1;
119 bool IsBeingAnalyzed : 1;
120 bool IsAnalyzed : 1;
121 bool IsEnqueued : 1;
122 bool IsBrAnalyzable : 1;
Kyle Butta8c73712016-08-24 21:34:27 +0000123 bool IsBrReversible : 1;
Evan Cheng4dd31a72007-06-11 22:26:22 +0000124 bool HasFallThrough : 1;
125 bool IsUnpredicable : 1;
Evan Cheng23402fc2007-06-15 21:18:05 +0000126 bool CannotBeCopied : 1;
Evan Cheng4dd31a72007-06-11 22:26:22 +0000127 bool ClobbersPred : 1;
Evan Chengd0e66912007-05-23 07:23:16 +0000128 unsigned NonPredSize;
Bob Wilsonefd360c2010-10-26 00:02:21 +0000129 unsigned ExtraCost;
Evan Chengdebf9c52010-11-03 00:45:17 +0000130 unsigned ExtraCost2;
Evan Cheng0f745da2007-05-18 00:20:58 +0000131 MachineBasicBlock *BB;
132 MachineBasicBlock *TrueBB;
133 MachineBasicBlock *FalseBB;
Owen Anderson4f6bf042008-08-14 22:49:33 +0000134 SmallVector<MachineOperand, 4> BrCond;
135 SmallVector<MachineOperand, 4> Predicate;
Evan Cheng3a51c852007-06-16 09:34:52 +0000136 BBInfo() : IsDone(false), IsBeingAnalyzed(false),
Evan Cheng4dd31a72007-06-11 22:26:22 +0000137 IsAnalyzed(false), IsEnqueued(false), IsBrAnalyzable(false),
Kyle Butta8c73712016-08-24 21:34:27 +0000138 IsBrReversible(false), HasFallThrough(false),
139 IsUnpredicable(false), CannotBeCopied(false),
140 ClobbersPred(false), NonPredSize(0), ExtraCost(0),
141 ExtraCost2(0), BB(nullptr), TrueBB(nullptr),
Craig Topperc0196b12014-04-14 00:51:57 +0000142 FalseBB(nullptr) {}
Evan Cheng3a51c852007-06-16 09:34:52 +0000143 };
144
Matthias Braun2c931792016-08-17 02:51:57 +0000145 /// Record information about pending if-conversions to attempt:
Evan Cheng3a51c852007-06-16 09:34:52 +0000146 /// BBI - Corresponding BBInfo.
147 /// Kind - Type of block. See IfcvtKind.
Bob Wilson2371f4f2009-05-13 23:25:24 +0000148 /// NeedSubsumption - True if the to-be-predicated BB has already been
Evan Cheng3a51c852007-06-16 09:34:52 +0000149 /// predicated.
Evan Cheng51eb2c32007-06-18 08:37:25 +0000150 /// NumDups - Number of instructions that would be duplicated due
151 /// to this if-conversion. (For diamonds, the number of
152 /// identical instructions at the beginnings of both
153 /// paths).
154 /// NumDups2 - For diamonds, the number of identical instructions
155 /// at the ends of both paths.
Evan Cheng3a51c852007-06-16 09:34:52 +0000156 struct IfcvtToken {
157 BBInfo &BBI;
158 IfcvtKind Kind;
Evan Cheng51eb2c32007-06-18 08:37:25 +0000159 unsigned NumDups;
160 unsigned NumDups2;
Kyle Butt6262ca32016-08-24 21:34:24 +0000161 bool NeedSubsumption : 1;
162 bool TClobbersPred : 1;
163 bool FClobbersPred : 1;
164 IfcvtToken(BBInfo &b, IfcvtKind k, bool s, unsigned d, unsigned d2 = 0,
165 bool tc = false, bool fc = false)
166 : BBI(b), Kind(k), NumDups(d), NumDups2(d2), NeedSubsumption(s),
167 TClobbersPred(tc), FClobbersPred(fc) {}
Evan Chengf5e53a52007-05-16 02:00:57 +0000168 };
169
Matthias Braun2c931792016-08-17 02:51:57 +0000170 /// Results of if-conversion feasibility analysis indexed by basic block
171 /// number.
Evan Chengf5e53a52007-05-16 02:00:57 +0000172 std::vector<BBInfo> BBAnalysis;
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +0000173 TargetSchedModel SchedModel;
Evan Chengf5e53a52007-05-16 02:00:57 +0000174
Benjamin Kramer56b31bd2013-01-11 20:05:37 +0000175 const TargetLoweringBase *TLI;
Evan Chengf5e53a52007-05-16 02:00:57 +0000176 const TargetInstrInfo *TII;
Evan Chengf128bdc2010-06-16 07:35:02 +0000177 const TargetRegisterInfo *TRI;
Jakub Staszak15e5b742011-08-03 22:34:43 +0000178 const MachineBranchProbabilityInfo *MBPI;
Evan Chengc5adcca2012-06-08 21:53:50 +0000179 MachineRegisterInfo *MRI;
Jakub Staszak15e5b742011-08-03 22:34:43 +0000180
Juergen Ributzka310034e2013-12-14 06:52:56 +0000181 LivePhysRegs Redefs;
182 LivePhysRegs DontKill;
Andrew Trick276dd452013-10-14 20:45:17 +0000183
Evan Chengc5adcca2012-06-08 21:53:50 +0000184 bool PreRegAlloc;
Evan Chengf5e53a52007-05-16 02:00:57 +0000185 bool MadeChange;
Owen Anderson816e2832009-06-24 23:41:44 +0000186 int FnNum;
Matthias Braun8b38ffa2016-10-24 23:23:02 +0000187 std::function<bool(const MachineFunction &)> PredicateFtor;
Akira Hatanaka4a616192015-06-08 18:50:43 +0000188
Evan Chengf5e53a52007-05-16 02:00:57 +0000189 public:
190 static char ID;
Matthias Braun8b38ffa2016-10-24 23:23:02 +0000191 IfConverter(std::function<bool(const MachineFunction &)> Ftor = nullptr)
Benjamin Kramer82de7d32016-05-27 14:27:24 +0000192 : MachineFunctionPass(ID), FnNum(-1), PredicateFtor(std::move(Ftor)) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000193 initializeIfConverterPass(*PassRegistry::getPassRegistry());
194 }
Jakub Staszak15e5b742011-08-03 22:34:43 +0000195
Craig Topper4584cd52014-03-07 09:26:03 +0000196 void getAnalysisUsage(AnalysisUsage &AU) const override {
Akira Hatanakabbd33f62014-08-07 19:30:13 +0000197 AU.addRequired<MachineBlockFrequencyInfo>();
Jakub Staszak15e5b742011-08-03 22:34:43 +0000198 AU.addRequired<MachineBranchProbabilityInfo>();
Owen Anderson1b35f4c2010-09-28 20:42:15 +0000199 MachineFunctionPass::getAnalysisUsage(AU);
200 }
Evan Chengf5e53a52007-05-16 02:00:57 +0000201
Craig Topper4584cd52014-03-07 09:26:03 +0000202 bool runOnMachineFunction(MachineFunction &MF) override;
Evan Chengf5e53a52007-05-16 02:00:57 +0000203
Derek Schuff1dbf7a52016-04-04 17:09:25 +0000204 MachineFunctionProperties getRequiredProperties() const override {
205 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +0000206 MachineFunctionProperties::Property::NoVRegs);
Derek Schuff1dbf7a52016-04-04 17:09:25 +0000207 }
208
Evan Chengf5e53a52007-05-16 02:00:57 +0000209 private:
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000210 bool reverseBranchCondition(BBInfo &BBI) const;
Owen Andersonf31f33e2010-10-01 22:45:50 +0000211 bool ValidSimple(BBInfo &TrueBBI, unsigned &Dups,
Cong Houc536bd92015-09-10 23:10:42 +0000212 BranchProbability Prediction) const;
Evan Cheng7783f822007-06-08 09:36:04 +0000213 bool ValidTriangle(BBInfo &TrueBBI, BBInfo &FalseBBI,
Owen Andersonf31f33e2010-10-01 22:45:50 +0000214 bool FalseBranch, unsigned &Dups,
Cong Houc536bd92015-09-10 23:10:42 +0000215 BranchProbability Prediction) const;
Kyle Butt6262ca32016-08-24 21:34:24 +0000216 bool CountDuplicatedInstructions(
217 MachineBasicBlock::iterator &TIB, MachineBasicBlock::iterator &FIB,
218 MachineBasicBlock::iterator &TIE, MachineBasicBlock::iterator &FIE,
219 unsigned &Dups1, unsigned &Dups2,
220 MachineBasicBlock &TBB, MachineBasicBlock &FBB,
221 bool SkipUnconditionalBranches) const;
Evan Cheng51eb2c32007-06-18 08:37:25 +0000222 bool ValidDiamond(BBInfo &TrueBBI, BBInfo &FalseBBI,
Kyle Butt6262ca32016-08-24 21:34:24 +0000223 unsigned &Dups1, unsigned &Dups2,
224 BBInfo &TrueBBICalc, BBInfo &FalseBBICalc) const;
Kyle Butta8c73712016-08-24 21:34:27 +0000225 bool ValidForkedDiamond(BBInfo &TrueBBI, BBInfo &FalseBBI,
226 unsigned &Dups1, unsigned &Dups2,
227 BBInfo &TrueBBICalc, BBInfo &FalseBBICalc) const;
Kyle Butt54bf3ce2016-08-06 01:52:34 +0000228 void AnalyzeBranches(BBInfo &BBI);
229 void ScanInstructions(BBInfo &BBI,
230 MachineBasicBlock::iterator &Begin,
Kyle Butt6262ca32016-08-24 21:34:24 +0000231 MachineBasicBlock::iterator &End,
232 bool BranchUnpredicable = false) const;
233 bool RescanInstructions(
234 MachineBasicBlock::iterator &TIB, MachineBasicBlock::iterator &FIB,
235 MachineBasicBlock::iterator &TIE, MachineBasicBlock::iterator &FIE,
236 BBInfo &TrueBBI, BBInfo &FalseBBI) const;
Matthias Braun08f47042016-08-17 02:52:01 +0000237 void AnalyzeBlock(MachineBasicBlock &MBB,
Justin Lebar3a7bc572016-02-22 17:51:28 +0000238 std::vector<std::unique_ptr<IfcvtToken>> &Tokens);
Owen Anderson4f6bf042008-08-14 22:49:33 +0000239 bool FeasibilityAnalysis(BBInfo &BBI, SmallVectorImpl<MachineOperand> &Cond,
Kyle Butt6262ca32016-08-24 21:34:24 +0000240 bool isTriangle = false, bool RevBranch = false,
241 bool hasCommonTail = false);
Justin Lebar3a7bc572016-02-22 17:51:28 +0000242 void AnalyzeBlocks(MachineFunction &MF,
243 std::vector<std::unique_ptr<IfcvtToken>> &Tokens);
Matthias Braun08f47042016-08-17 02:52:01 +0000244 void InvalidatePreds(MachineBasicBlock &MBB);
Evan Cheng3a51c852007-06-16 09:34:52 +0000245 bool IfConvertSimple(BBInfo &BBI, IfcvtKind Kind);
246 bool IfConvertTriangle(BBInfo &BBI, IfcvtKind Kind);
Kyle Butta8c73712016-08-24 21:34:27 +0000247 bool IfConvertDiamondCommon(BBInfo &BBI, BBInfo &TrueBBI, BBInfo &FalseBBI,
248 unsigned NumDups1, unsigned NumDups2,
249 bool TClobbersPred, bool FClobbersPred,
Kyle Butt092c4dd2016-08-29 18:27:12 +0000250 bool RemoveBranch, bool MergeAddEdges);
Evan Cheng51eb2c32007-06-18 08:37:25 +0000251 bool IfConvertDiamond(BBInfo &BBI, IfcvtKind Kind,
Kyle Butt6262ca32016-08-24 21:34:24 +0000252 unsigned NumDups1, unsigned NumDups2,
253 bool TClobbers, bool FClobbers);
Kyle Butta8c73712016-08-24 21:34:27 +0000254 bool IfConvertForkedDiamond(BBInfo &BBI, IfcvtKind Kind,
255 unsigned NumDups1, unsigned NumDups2,
256 bool TClobbers, bool FClobbers);
Evan Chengd0e66912007-05-23 07:23:16 +0000257 void PredicateBlock(BBInfo &BBI,
Evan Cheng51eb2c32007-06-18 08:37:25 +0000258 MachineBasicBlock::iterator E,
Evan Chengf128bdc2010-06-16 07:35:02 +0000259 SmallVectorImpl<MachineOperand> &Cond,
Craig Topperc0196b12014-04-14 00:51:57 +0000260 SmallSet<unsigned, 4> *LaterRedefs = nullptr);
Evan Cheng92fb5452007-06-15 07:36:12 +0000261 void CopyAndPredicateBlock(BBInfo &ToBBI, BBInfo &FromBBI,
Owen Anderson4f6bf042008-08-14 22:49:33 +0000262 SmallVectorImpl<MachineOperand> &Cond,
Evan Cheng92fb5452007-06-15 07:36:12 +0000263 bool IgnoreBr = false);
Bob Wilson1e5da552010-06-29 00:55:23 +0000264 void MergeBlocks(BBInfo &ToBBI, BBInfo &FromBBI, bool AddEdges = true);
Evan Cheng20e05992007-06-01 00:12:12 +0000265
Evan Chengdebf9c52010-11-03 00:45:17 +0000266 bool MeetIfcvtSizeLimit(MachineBasicBlock &BB,
267 unsigned Cycle, unsigned Extra,
Cong Houc536bd92015-09-10 23:10:42 +0000268 BranchProbability Prediction) const {
Evan Chengdebf9c52010-11-03 00:45:17 +0000269 return Cycle > 0 && TII->isProfitableToIfCvt(BB, Cycle, Extra,
Jakub Staszak9b07c0a2011-07-10 02:58:07 +0000270 Prediction);
Evan Cheng02b184d2010-06-25 22:42:03 +0000271 }
272
Evan Chengdebf9c52010-11-03 00:45:17 +0000273 bool MeetIfcvtSizeLimit(MachineBasicBlock &TBB,
274 unsigned TCycle, unsigned TExtra,
275 MachineBasicBlock &FBB,
276 unsigned FCycle, unsigned FExtra,
Cong Houc536bd92015-09-10 23:10:42 +0000277 BranchProbability Prediction) const {
Evan Chengdebf9c52010-11-03 00:45:17 +0000278 return TCycle > 0 && FCycle > 0 &&
279 TII->isProfitableToIfCvt(TBB, TCycle, TExtra, FBB, FCycle, FExtra,
Jakub Staszak9b07c0a2011-07-10 02:58:07 +0000280 Prediction);
Evan Cheng51eb2c32007-06-18 08:37:25 +0000281 }
282
Matthias Braun2c931792016-08-17 02:51:57 +0000283 /// Returns true if Block ends without a terminator.
Evan Chengbe9859e2007-06-07 02:12:15 +0000284 bool blockAlwaysFallThrough(BBInfo &BBI) const {
Craig Topperc0196b12014-04-14 00:51:57 +0000285 return BBI.IsBrAnalyzable && BBI.TrueBB == nullptr;
Evan Cheng9030b982007-06-06 10:16:17 +0000286 }
287
Matthias Braun2c931792016-08-17 02:51:57 +0000288 /// Used to sort if-conversion candidates.
Justin Lebar3a7bc572016-02-22 17:51:28 +0000289 static bool IfcvtTokenCmp(const std::unique_ptr<IfcvtToken> &C1,
290 const std::unique_ptr<IfcvtToken> &C2) {
Evan Cheng51eb2c32007-06-18 08:37:25 +0000291 int Incr1 = (C1->Kind == ICDiamond)
292 ? -(int)(C1->NumDups + C1->NumDups2) : (int)C1->NumDups;
293 int Incr2 = (C2->Kind == ICDiamond)
294 ? -(int)(C2->NumDups + C2->NumDups2) : (int)C2->NumDups;
295 if (Incr1 > Incr2)
Evan Cheng3a51c852007-06-16 09:34:52 +0000296 return true;
Evan Cheng51eb2c32007-06-18 08:37:25 +0000297 else if (Incr1 == Incr2) {
Bob Wilson2371f4f2009-05-13 23:25:24 +0000298 // Favors subsumption.
David Blaikiedc3f01e2015-03-09 01:57:13 +0000299 if (!C1->NeedSubsumption && C2->NeedSubsumption)
Evan Cheng3a51c852007-06-16 09:34:52 +0000300 return true;
Bob Wilson2371f4f2009-05-13 23:25:24 +0000301 else if (C1->NeedSubsumption == C2->NeedSubsumption) {
Evan Cheng3a51c852007-06-16 09:34:52 +0000302 // Favors diamond over triangle, etc.
303 if ((unsigned)C1->Kind < (unsigned)C2->Kind)
304 return true;
305 else if (C1->Kind == C2->Kind)
306 return C1->BBI.BB->getNumber() < C2->BBI.BB->getNumber();
307 }
308 }
309 return false;
Evan Cheng20e05992007-06-01 00:12:12 +0000310 }
Evan Chengf5e53a52007-05-16 02:00:57 +0000311 };
Evan Cheng3a51c852007-06-16 09:34:52 +0000312
Evan Chengf5e53a52007-05-16 02:00:57 +0000313 char IfConverter::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000314}
Evan Chengf5e53a52007-05-16 02:00:57 +0000315
Andrew Trick1fa5bcb2012-02-08 21:23:13 +0000316char &llvm::IfConverterID = IfConverter::ID;
317
Matthias Braun1527baa2017-05-25 21:26:32 +0000318INITIALIZE_PASS_BEGIN(IfConverter, DEBUG_TYPE, "If Converter", false, false)
Jakub Staszak15e5b742011-08-03 22:34:43 +0000319INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
Matthias Braun1527baa2017-05-25 21:26:32 +0000320INITIALIZE_PASS_END(IfConverter, DEBUG_TYPE, "If Converter", false, false)
Bob Wilson97b93122009-10-28 20:46:46 +0000321
Evan Chengf5e53a52007-05-16 02:00:57 +0000322bool IfConverter::runOnMachineFunction(MachineFunction &MF) {
Matthias Braun8b38ffa2016-10-24 23:23:02 +0000323 if (skipFunction(*MF.getFunction()) || (PredicateFtor && !PredicateFtor(MF)))
Akira Hatanaka4a616192015-06-08 18:50:43 +0000324 return false;
325
Eric Christopher3d4276f2015-01-27 07:31:29 +0000326 const TargetSubtargetInfo &ST = MF.getSubtarget();
327 TLI = ST.getTargetLowering();
328 TII = ST.getInstrInfo();
329 TRI = ST.getRegisterInfo();
Haicheng Wu5b458cc2016-06-09 15:24:29 +0000330 BranchFolder::MBFIWrapper MBFI(getAnalysis<MachineBlockFrequencyInfo>());
Jakub Staszak15e5b742011-08-03 22:34:43 +0000331 MBPI = &getAnalysis<MachineBranchProbabilityInfo>();
Evan Chengc5adcca2012-06-08 21:53:50 +0000332 MRI = &MF.getRegInfo();
Pete Cooper11759452014-09-02 17:43:54 +0000333 SchedModel.init(ST.getSchedModel(), &ST, TII);
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +0000334
Evan Chengf5e53a52007-05-16 02:00:57 +0000335 if (!TII) return false;
336
Evan Chengc5adcca2012-06-08 21:53:50 +0000337 PreRegAlloc = MRI->isSSA();
338
339 bool BFChange = false;
340 if (!PreRegAlloc) {
341 // Tail merge tend to expose more if-conversion opportunities.
Haicheng Wu5b458cc2016-06-09 15:24:29 +0000342 BranchFolder BF(true, false, MBFI, *MBPI);
Eric Christopher3d4276f2015-01-27 07:31:29 +0000343 BFChange = BF.OptimizeFunction(MF, TII, ST.getRegisterInfo(),
Eric Christopherfc6de422014-08-05 02:39:49 +0000344 getAnalysisIfAvailable<MachineModuleInfo>());
Evan Chengc5adcca2012-06-08 21:53:50 +0000345 }
Evan Cheng2d51c7c2010-06-18 23:09:54 +0000346
David Greene72e47cd2010-01-04 22:02:01 +0000347 DEBUG(dbgs() << "\nIfcvt: function (" << ++FnNum << ") \'"
Craig Toppera538d832012-08-22 06:07:19 +0000348 << MF.getName() << "\'");
Evan Chenge93ccc02007-06-08 19:10:51 +0000349
350 if (FnNum < IfCvtFnStart || (IfCvtFnStop != -1 && FnNum > IfCvtFnStop)) {
David Greene72e47cd2010-01-04 22:02:01 +0000351 DEBUG(dbgs() << " skipped\n");
Evan Chenge93ccc02007-06-08 19:10:51 +0000352 return false;
353 }
David Greene72e47cd2010-01-04 22:02:01 +0000354 DEBUG(dbgs() << "\n");
Evan Cheng20e05992007-06-01 00:12:12 +0000355
Evan Chengf5e53a52007-05-16 02:00:57 +0000356 MF.RenumberBlocks();
Evan Cheng20e05992007-06-01 00:12:12 +0000357 BBAnalysis.resize(MF.getNumBlockIDs());
Evan Chengf5e53a52007-05-16 02:00:57 +0000358
Justin Lebar3a7bc572016-02-22 17:51:28 +0000359 std::vector<std::unique_ptr<IfcvtToken>> Tokens;
Evan Cheng478b8052007-05-18 01:55:58 +0000360 MadeChange = false;
Evan Cheng92fb5452007-06-15 07:36:12 +0000361 unsigned NumIfCvts = NumSimple + NumSimpleFalse + NumTriangle +
362 NumTriangleRev + NumTriangleFalse + NumTriangleFRev + NumDiamonds;
363 while (IfCvtLimit == -1 || (int)NumIfCvts < IfCvtLimit) {
Bob Wilson2371f4f2009-05-13 23:25:24 +0000364 // Do an initial analysis for each basic block and find all the potential
365 // candidates to perform if-conversion.
Bob Wilsonfc7d7392010-06-15 18:57:15 +0000366 bool Change = false;
367 AnalyzeBlocks(MF, Tokens);
Evan Cheng3a51c852007-06-16 09:34:52 +0000368 while (!Tokens.empty()) {
Justin Lebar3a7bc572016-02-22 17:51:28 +0000369 std::unique_ptr<IfcvtToken> Token = std::move(Tokens.back());
Evan Cheng3a51c852007-06-16 09:34:52 +0000370 Tokens.pop_back();
371 BBInfo &BBI = Token->BBI;
372 IfcvtKind Kind = Token->Kind;
Nuno Lopes57c65942008-11-04 13:02:59 +0000373 unsigned NumDups = Token->NumDups;
Duncan Sandsdd571d32008-11-04 18:05:30 +0000374 unsigned NumDups2 = Token->NumDups2;
Nuno Lopes57c65942008-11-04 13:02:59 +0000375
Evan Cheng4dd31a72007-06-11 22:26:22 +0000376 // If the block has been evicted out of the queue or it has already been
377 // marked dead (due to it being predicated), then skip it.
Evan Cheng3a51c852007-06-16 09:34:52 +0000378 if (BBI.IsDone)
379 BBI.IsEnqueued = false;
380 if (!BBI.IsEnqueued)
Evan Cheng4dd31a72007-06-11 22:26:22 +0000381 continue;
Evan Cheng3a51c852007-06-16 09:34:52 +0000382
Evan Cheng1e6f08b2007-06-14 20:28:52 +0000383 BBI.IsEnqueued = false;
Evan Cheng4dd31a72007-06-11 22:26:22 +0000384
Evan Cheng312b7232007-06-04 06:47:22 +0000385 bool RetVal = false;
Evan Cheng3a51c852007-06-16 09:34:52 +0000386 switch (Kind) {
Craig Topperee4dab52012-02-05 08:31:47 +0000387 default: llvm_unreachable("Unexpected!");
Evan Cheng312b7232007-06-04 06:47:22 +0000388 case ICSimple:
Evan Chengb30a8942007-06-06 01:12:44 +0000389 case ICSimpleFalse: {
Evan Cheng3a51c852007-06-16 09:34:52 +0000390 bool isFalse = Kind == ICSimpleFalse;
Evan Cheng2117d1f2007-06-09 01:03:43 +0000391 if ((isFalse && DisableSimpleF) || (!isFalse && DisableSimple)) break;
Bob Wilson81051442010-06-15 22:18:54 +0000392 DEBUG(dbgs() << "Ifcvt (Simple" << (Kind == ICSimpleFalse ?
393 " false" : "")
Bill Wendlingfb363162009-08-22 20:11:17 +0000394 << "): BB#" << BBI.BB->getNumber() << " ("
395 << ((Kind == ICSimpleFalse)
396 ? BBI.FalseBB->getNumber()
397 : BBI.TrueBB->getNumber()) << ") ");
Evan Cheng3a51c852007-06-16 09:34:52 +0000398 RetVal = IfConvertSimple(BBI, Kind);
David Greene72e47cd2010-01-04 22:02:01 +0000399 DEBUG(dbgs() << (RetVal ? "succeeded!" : "failed!") << "\n");
Anton Korobeynikov035eaac2008-02-20 11:10:28 +0000400 if (RetVal) {
Dan Gohmand2d1ae12010-06-22 15:08:57 +0000401 if (isFalse) ++NumSimpleFalse;
402 else ++NumSimple;
Anton Korobeynikov035eaac2008-02-20 11:10:28 +0000403 }
Evan Cheng312b7232007-06-04 06:47:22 +0000404 break;
Evan Chengb30a8942007-06-06 01:12:44 +0000405 }
Evan Chengd0e66912007-05-23 07:23:16 +0000406 case ICTriangle:
Evan Cheng9acfa7b2007-06-12 23:54:05 +0000407 case ICTriangleRev:
Evan Cheng2117d1f2007-06-09 01:03:43 +0000408 case ICTriangleFalse:
Reid Spencer14b62a52007-06-10 00:19:17 +0000409 case ICTriangleFRev: {
Evan Cheng3a51c852007-06-16 09:34:52 +0000410 bool isFalse = Kind == ICTriangleFalse;
411 bool isRev = (Kind == ICTriangleRev || Kind == ICTriangleFRev);
Evan Cheng9acfa7b2007-06-12 23:54:05 +0000412 if (DisableTriangle && !isFalse && !isRev) break;
413 if (DisableTriangleR && !isFalse && isRev) break;
414 if (DisableTriangleF && isFalse && !isRev) break;
415 if (DisableTriangleFR && isFalse && isRev) break;
David Greene72e47cd2010-01-04 22:02:01 +0000416 DEBUG(dbgs() << "Ifcvt (Triangle");
Evan Cheng2117d1f2007-06-09 01:03:43 +0000417 if (isFalse)
David Greene72e47cd2010-01-04 22:02:01 +0000418 DEBUG(dbgs() << " false");
Evan Cheng9acfa7b2007-06-12 23:54:05 +0000419 if (isRev)
David Greene72e47cd2010-01-04 22:02:01 +0000420 DEBUG(dbgs() << " rev");
421 DEBUG(dbgs() << "): BB#" << BBI.BB->getNumber() << " (T:"
Bill Wendlingfb363162009-08-22 20:11:17 +0000422 << BBI.TrueBB->getNumber() << ",F:"
423 << BBI.FalseBB->getNumber() << ") ");
Evan Cheng3a51c852007-06-16 09:34:52 +0000424 RetVal = IfConvertTriangle(BBI, Kind);
David Greene72e47cd2010-01-04 22:02:01 +0000425 DEBUG(dbgs() << (RetVal ? "succeeded!" : "failed!") << "\n");
Evan Cheng2117d1f2007-06-09 01:03:43 +0000426 if (RetVal) {
Evan Cheng9acfa7b2007-06-12 23:54:05 +0000427 if (isFalse) {
Dan Gohmand2d1ae12010-06-22 15:08:57 +0000428 if (isRev) ++NumTriangleFRev;
429 else ++NumTriangleFalse;
Evan Cheng9acfa7b2007-06-12 23:54:05 +0000430 } else {
Dan Gohmand2d1ae12010-06-22 15:08:57 +0000431 if (isRev) ++NumTriangleRev;
432 else ++NumTriangle;
Evan Cheng9acfa7b2007-06-12 23:54:05 +0000433 }
Evan Cheng2117d1f2007-06-09 01:03:43 +0000434 }
Evan Chengd0e66912007-05-23 07:23:16 +0000435 break;
Reid Spencer14b62a52007-06-10 00:19:17 +0000436 }
Evan Cheng3a51c852007-06-16 09:34:52 +0000437 case ICDiamond: {
Evan Chenge93ccc02007-06-08 19:10:51 +0000438 if (DisableDiamond) break;
David Greene72e47cd2010-01-04 22:02:01 +0000439 DEBUG(dbgs() << "Ifcvt (Diamond): BB#" << BBI.BB->getNumber() << " (T:"
Bill Wendlingfb363162009-08-22 20:11:17 +0000440 << BBI.TrueBB->getNumber() << ",F:"
441 << BBI.FalseBB->getNumber() << ") ");
Kyle Butt6262ca32016-08-24 21:34:24 +0000442 RetVal = IfConvertDiamond(BBI, Kind, NumDups, NumDups2,
443 Token->TClobbersPred,
444 Token->FClobbersPred);
David Greene72e47cd2010-01-04 22:02:01 +0000445 DEBUG(dbgs() << (RetVal ? "succeeded!" : "failed!") << "\n");
Dan Gohmand2d1ae12010-06-22 15:08:57 +0000446 if (RetVal) ++NumDiamonds;
Evan Chengd0e66912007-05-23 07:23:16 +0000447 break;
448 }
Kyle Butta8c73712016-08-24 21:34:27 +0000449 case ICForkedDiamond: {
450 if (DisableForkedDiamond) break;
451 DEBUG(dbgs() << "Ifcvt (Forked Diamond): BB#"
452 << BBI.BB->getNumber() << " (T:"
453 << BBI.TrueBB->getNumber() << ",F:"
454 << BBI.FalseBB->getNumber() << ") ");
455 RetVal = IfConvertForkedDiamond(BBI, Kind, NumDups, NumDups2,
456 Token->TClobbersPred,
457 Token->FClobbersPred);
458 DEBUG(dbgs() << (RetVal ? "succeeded!" : "failed!") << "\n");
459 if (RetVal) ++NumForkedDiamonds;
460 break;
461 }
Evan Cheng3a51c852007-06-16 09:34:52 +0000462 }
463
Evan Cheng312b7232007-06-04 06:47:22 +0000464 Change |= RetVal;
Evan Chenge93ccc02007-06-08 19:10:51 +0000465
Evan Cheng92fb5452007-06-15 07:36:12 +0000466 NumIfCvts = NumSimple + NumSimpleFalse + NumTriangle + NumTriangleRev +
467 NumTriangleFalse + NumTriangleFRev + NumDiamonds;
468 if (IfCvtLimit != -1 && (int)NumIfCvts >= IfCvtLimit)
Evan Chenge93ccc02007-06-08 19:10:51 +0000469 break;
Evan Chengf5e53a52007-05-16 02:00:57 +0000470 }
Evan Chengd0e66912007-05-23 07:23:16 +0000471
Evan Chengd0e66912007-05-23 07:23:16 +0000472 if (!Change)
473 break;
Evan Cheng312b7232007-06-04 06:47:22 +0000474 MadeChange |= Change;
Evan Chengf5e53a52007-05-16 02:00:57 +0000475 }
Evan Cheng478b8052007-05-18 01:55:58 +0000476
Evan Cheng3a51c852007-06-16 09:34:52 +0000477 Tokens.clear();
Evan Cheng478b8052007-05-18 01:55:58 +0000478 BBAnalysis.clear();
479
Evan Chengcf9e8a92010-06-18 22:17:13 +0000480 if (MadeChange && IfCvtBranchFold) {
Haicheng Wu5b458cc2016-06-09 15:24:29 +0000481 BranchFolder BF(false, false, MBFI, *MBPI);
Eric Christopherfc6de422014-08-05 02:39:49 +0000482 BF.OptimizeFunction(MF, TII, MF.getSubtarget().getRegisterInfo(),
Evan Cheng3d2fce02009-09-04 07:47:40 +0000483 getAnalysisIfAvailable<MachineModuleInfo>());
484 }
485
Evan Cheng2d51c7c2010-06-18 23:09:54 +0000486 MadeChange |= BFChange;
Evan Chengf5e53a52007-05-16 02:00:57 +0000487 return MadeChange;
488}
489
Matthias Braun2c931792016-08-17 02:51:57 +0000490/// BB has a fallthrough. Find its 'false' successor given its 'true' successor.
Evan Chengf5e53a52007-05-16 02:00:57 +0000491static MachineBasicBlock *findFalseBlock(MachineBasicBlock *BB,
Evan Cheng0f745da2007-05-18 00:20:58 +0000492 MachineBasicBlock *TrueBB) {
Matthias Braunb1e05582016-08-17 02:51:59 +0000493 for (MachineBasicBlock *SuccBB : BB->successors()) {
Evan Cheng0f745da2007-05-18 00:20:58 +0000494 if (SuccBB != TrueBB)
Evan Chengf5e53a52007-05-16 02:00:57 +0000495 return SuccBB;
496 }
Craig Topperc0196b12014-04-14 00:51:57 +0000497 return nullptr;
Evan Chengf5e53a52007-05-16 02:00:57 +0000498}
499
Matthias Braun2c931792016-08-17 02:51:57 +0000500/// Reverse the condition of the end of the block branch. Swap block's 'true'
501/// and 'false' successors.
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000502bool IfConverter::reverseBranchCondition(BBInfo &BBI) const {
Stuart Hastings0125b642010-06-17 22:43:56 +0000503 DebugLoc dl; // FIXME: this is nowhere
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000504 if (!TII->reverseBranchCondition(BBI.BrCond)) {
505 TII->removeBranch(*BBI.BB);
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000506 TII->insertBranch(*BBI.BB, BBI.FalseBB, BBI.TrueBB, BBI.BrCond, dl);
Evan Cheng312b7232007-06-04 06:47:22 +0000507 std::swap(BBI.TrueBB, BBI.FalseBB);
508 return true;
509 }
510 return false;
511}
512
Matthias Braun2c931792016-08-17 02:51:57 +0000513/// Returns the next block in the function blocks ordering. If it is the end,
514/// returns NULL.
Matthias Braun08f47042016-08-17 02:52:01 +0000515static inline MachineBasicBlock *getNextBlock(MachineBasicBlock &MBB) {
516 MachineFunction::iterator I = MBB.getIterator();
517 MachineFunction::iterator E = MBB.getParent()->end();
Evan Cheng2117d1f2007-06-09 01:03:43 +0000518 if (++I == E)
Craig Topperc0196b12014-04-14 00:51:57 +0000519 return nullptr;
Duncan P. N. Exon Smith5ae59392015-10-09 19:13:58 +0000520 return &*I;
Evan Cheng2117d1f2007-06-09 01:03:43 +0000521}
522
Matthias Braun2c931792016-08-17 02:51:57 +0000523/// Returns true if the 'true' block (along with its predecessor) forms a valid
524/// simple shape for ifcvt. It also returns the number of instructions that the
525/// ifcvt would need to duplicate if performed in Dups.
Owen Anderson1b35f4c2010-09-28 20:42:15 +0000526bool IfConverter::ValidSimple(BBInfo &TrueBBI, unsigned &Dups,
Cong Houc536bd92015-09-10 23:10:42 +0000527 BranchProbability Prediction) const {
Evan Cheng3a51c852007-06-16 09:34:52 +0000528 Dups = 0;
Evan Cheng51eb2c32007-06-18 08:37:25 +0000529 if (TrueBBI.IsBeingAnalyzed || TrueBBI.IsDone)
Evan Cheng4dd31a72007-06-11 22:26:22 +0000530 return false;
531
Evan Chenga955c022007-06-19 21:45:13 +0000532 if (TrueBBI.IsBrAnalyzable)
533 return false;
534
Evan Cheng23402fc2007-06-15 21:18:05 +0000535 if (TrueBBI.BB->pred_size() > 1) {
536 if (TrueBBI.CannotBeCopied ||
Owen Anderson1b35f4c2010-09-28 20:42:15 +0000537 !TII->isProfitableToDupForIfCvt(*TrueBBI.BB, TrueBBI.NonPredSize,
Jakub Staszak9b07c0a2011-07-10 02:58:07 +0000538 Prediction))
Evan Cheng92fb5452007-06-15 07:36:12 +0000539 return false;
Evan Cheng3a51c852007-06-16 09:34:52 +0000540 Dups = TrueBBI.NonPredSize;
Evan Cheng92fb5452007-06-15 07:36:12 +0000541 }
542
Evan Chenga955c022007-06-19 21:45:13 +0000543 return true;
Evan Cheng9030b982007-06-06 10:16:17 +0000544}
545
Matthias Braun2c931792016-08-17 02:51:57 +0000546/// Returns true if the 'true' and 'false' blocks (along with their common
547/// predecessor) forms a valid triangle shape for ifcvt. If 'FalseBranch' is
548/// true, it checks if 'true' block's false branch branches to the 'false' block
549/// rather than the other way around. It also returns the number of instructions
550/// that the ifcvt would need to duplicate if performed in 'Dups'.
Evan Cheng7783f822007-06-08 09:36:04 +0000551bool IfConverter::ValidTriangle(BBInfo &TrueBBI, BBInfo &FalseBBI,
Owen Andersonf31f33e2010-10-01 22:45:50 +0000552 bool FalseBranch, unsigned &Dups,
Cong Houc536bd92015-09-10 23:10:42 +0000553 BranchProbability Prediction) const {
Evan Cheng3a51c852007-06-16 09:34:52 +0000554 Dups = 0;
Evan Cheng51eb2c32007-06-18 08:37:25 +0000555 if (TrueBBI.IsBeingAnalyzed || TrueBBI.IsDone)
Evan Cheng4dd31a72007-06-11 22:26:22 +0000556 return false;
557
Evan Cheng23402fc2007-06-15 21:18:05 +0000558 if (TrueBBI.BB->pred_size() > 1) {
559 if (TrueBBI.CannotBeCopied)
560 return false;
561
Evan Cheng92fb5452007-06-15 07:36:12 +0000562 unsigned Size = TrueBBI.NonPredSize;
Evan Cheng3a51c852007-06-16 09:34:52 +0000563 if (TrueBBI.IsBrAnalyzable) {
Dan Gohman70de4cb2008-01-29 13:02:09 +0000564 if (TrueBBI.TrueBB && TrueBBI.BrCond.empty())
Bob Wilson2371f4f2009-05-13 23:25:24 +0000565 // Ends with an unconditional branch. It will be removed.
Evan Cheng3a51c852007-06-16 09:34:52 +0000566 --Size;
567 else {
568 MachineBasicBlock *FExit = FalseBranch
569 ? TrueBBI.TrueBB : TrueBBI.FalseBB;
570 if (FExit)
571 // Require a conditional branch
572 ++Size;
573 }
574 }
Jakub Staszak9b07c0a2011-07-10 02:58:07 +0000575 if (!TII->isProfitableToDupForIfCvt(*TrueBBI.BB, Size, Prediction))
Evan Cheng92fb5452007-06-15 07:36:12 +0000576 return false;
Evan Cheng3a51c852007-06-16 09:34:52 +0000577 Dups = Size;
Evan Cheng92fb5452007-06-15 07:36:12 +0000578 }
Evan Chengbe9859e2007-06-07 02:12:15 +0000579
Evan Cheng7783f822007-06-08 09:36:04 +0000580 MachineBasicBlock *TExit = FalseBranch ? TrueBBI.FalseBB : TrueBBI.TrueBB;
581 if (!TExit && blockAlwaysFallThrough(TrueBBI)) {
Duncan P. N. Exon Smith5ae59392015-10-09 19:13:58 +0000582 MachineFunction::iterator I = TrueBBI.BB->getIterator();
Evan Chengbe9859e2007-06-07 02:12:15 +0000583 if (++I == TrueBBI.BB->getParent()->end())
584 return false;
Duncan P. N. Exon Smith5ae59392015-10-09 19:13:58 +0000585 TExit = &*I;
Evan Chengbe9859e2007-06-07 02:12:15 +0000586 }
Evan Cheng7783f822007-06-08 09:36:04 +0000587 return TExit && TExit == FalseBBI.BB;
Evan Chengbe9859e2007-06-07 02:12:15 +0000588}
589
Kyle Butt4f0e2872016-08-06 01:52:33 +0000590/// Count duplicated instructions and move the iterators to show where they
591/// are.
592/// @param TIB True Iterator Begin
593/// @param FIB False Iterator Begin
594/// These two iterators initially point to the first instruction of the two
595/// blocks, and finally point to the first non-shared instruction.
596/// @param TIE True Iterator End
597/// @param FIE False Iterator End
598/// These two iterators initially point to End() for the two blocks() and
599/// finally point to the first shared instruction in the tail.
600/// Upon return [TIB, TIE), and [FIB, FIE) mark the un-duplicated portions of
601/// two blocks.
Kyle Butt6262ca32016-08-24 21:34:24 +0000602/// @param Dups1 count of duplicated instructions at the beginning of the 2
603/// blocks.
604/// @param Dups2 count of duplicated instructions at the end of the 2 blocks.
605/// @param SkipUnconditionalBranches if true, Don't make sure that
606/// unconditional branches at the end of the blocks are the same. True is
607/// passed when the blocks are analyzable to allow for fallthrough to be
608/// handled.
609/// @return false if the shared portion prevents if conversion.
610bool IfConverter::CountDuplicatedInstructions(
Kyle Butt9b6d99b2016-07-27 20:19:33 +0000611 MachineBasicBlock::iterator &TIB,
612 MachineBasicBlock::iterator &FIB,
613 MachineBasicBlock::iterator &TIE,
614 MachineBasicBlock::iterator &FIE,
615 unsigned &Dups1, unsigned &Dups2,
616 MachineBasicBlock &TBB, MachineBasicBlock &FBB,
Kyle Butt6262ca32016-08-24 21:34:24 +0000617 bool SkipUnconditionalBranches) const {
Evan Cheng4dd31a72007-06-11 22:26:22 +0000618
Bob Wilsone1961fe2010-10-26 00:02:24 +0000619 while (TIB != TIE && FIB != FIE) {
Evan Chengc0e0d852010-06-18 21:52:57 +0000620 // Skip dbg_value instructions. These do not count.
Florian Hahn3c8b8c92016-12-16 11:10:26 +0000621 TIB = skipDebugInstructionsForward(TIB, TIE);
Florian Hahn3c8b8c92016-12-16 11:10:26 +0000622 FIB = skipDebugInstructionsForward(FIB, FIE);
Kyle Buttc4614b32017-01-26 20:02:47 +0000623 if (TIB == TIE || FIB == FIE)
Kyle Buttfe916822016-08-06 01:52:31 +0000624 break;
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +0000625 if (!TIB->isIdenticalTo(*FIB))
Bob Wilsone1961fe2010-10-26 00:02:24 +0000626 break;
Kyle Butt6262ca32016-08-24 21:34:24 +0000627 // A pred-clobbering instruction in the shared portion prevents
628 // if-conversion.
629 std::vector<MachineOperand> PredDefs;
630 if (TII->DefinesPredicate(*TIB, PredDefs))
631 return false;
Kyle Butt93e94e82016-09-02 01:20:06 +0000632 // If we get all the way to the branch instructions, don't count them.
633 if (!TIB->isBranch())
634 ++Dups1;
Bob Wilsone1961fe2010-10-26 00:02:24 +0000635 ++TIB;
636 ++FIB;
637 }
638
Krzysztof Parzyszek2451c482016-01-20 13:14:52 +0000639 // Check for already containing all of the block.
640 if (TIB == TIE || FIB == FIE)
Kyle Butt6262ca32016-08-24 21:34:24 +0000641 return true;
Kyle Buttd76755e2016-08-18 22:09:23 +0000642 // Now, in preparation for counting duplicate instructions at the ends of the
Kyle Buttc4614b32017-01-26 20:02:47 +0000643 // blocks, switch to reverse_iterators. Note that getReverse() returns an
644 // iterator that points to the same instruction, unlike std::reverse_iterator.
645 // We have to do our own shifting so that we get the same range.
646 MachineBasicBlock::reverse_iterator RTIE = std::next(TIE.getReverse());
647 MachineBasicBlock::reverse_iterator RFIE = std::next(FIE.getReverse());
648 const MachineBasicBlock::reverse_iterator RTIB = std::next(TIB.getReverse());
649 const MachineBasicBlock::reverse_iterator RFIB = std::next(FIB.getReverse());
Kyle Buttd76755e2016-08-18 22:09:23 +0000650
Kyle Butt9b6d99b2016-07-27 20:19:33 +0000651 if (!TBB.succ_empty() || !FBB.succ_empty()) {
Kyle Butt6262ca32016-08-24 21:34:24 +0000652 if (SkipUnconditionalBranches) {
Kyle Buttc4614b32017-01-26 20:02:47 +0000653 while (RTIE != RTIB && RTIE->isUnconditionalBranch())
654 ++RTIE;
655 while (RFIE != RFIB && RFIE->isUnconditionalBranch())
656 ++RFIE;
Kyle Butt9b6d99b2016-07-27 20:19:33 +0000657 }
Bob Wilsone1961fe2010-10-26 00:02:24 +0000658 }
659
Bob Wilsone1961fe2010-10-26 00:02:24 +0000660 // Count duplicate instructions at the ends of the blocks.
Kyle Buttc4614b32017-01-26 20:02:47 +0000661 while (RTIE != RTIB && RFIE != RFIB) {
Bob Wilsone1961fe2010-10-26 00:02:24 +0000662 // Skip dbg_value instructions. These do not count.
Kyle Buttc4614b32017-01-26 20:02:47 +0000663 // Note that these are reverse iterators going forward.
664 RTIE = skipDebugInstructionsForward(RTIE, RTIB);
665 RFIE = skipDebugInstructionsForward(RFIE, RFIB);
666 if (RTIE == RTIB || RFIE == RFIB)
Kyle Buttfe916822016-08-06 01:52:31 +0000667 break;
Kyle Buttc4614b32017-01-26 20:02:47 +0000668 if (!RTIE->isIdenticalTo(*RFIE))
Evan Cheng51eb2c32007-06-18 08:37:25 +0000669 break;
Kyle Butt6262ca32016-08-24 21:34:24 +0000670 // We have to verify that any branch instructions are the same, and then we
671 // don't count them toward the # of duplicate instructions.
Kyle Buttc4614b32017-01-26 20:02:47 +0000672 if (!RTIE->isBranch())
Kyle Butt9b6d99b2016-07-27 20:19:33 +0000673 ++Dups2;
Kyle Buttc4614b32017-01-26 20:02:47 +0000674 ++RTIE;
675 ++RFIE;
Evan Cheng51eb2c32007-06-18 08:37:25 +0000676 }
Kyle Buttc4614b32017-01-26 20:02:47 +0000677 TIE = std::next(RTIE.getReverse());
678 FIE = std::next(RFIE.getReverse());
Kyle Butt6262ca32016-08-24 21:34:24 +0000679 return true;
680}
681
682/// RescanInstructions - Run ScanInstructions on a pair of blocks.
683/// @param TIB - True Iterator Begin, points to first non-shared instruction
684/// @param FIB - False Iterator Begin, points to first non-shared instruction
685/// @param TIE - True Iterator End, points past last non-shared instruction
686/// @param FIE - False Iterator End, points past last non-shared instruction
687/// @param TrueBBI - BBInfo to update for the true block.
688/// @param FalseBBI - BBInfo to update for the false block.
689/// @returns - false if either block cannot be predicated or if both blocks end
690/// with a predicate-clobbering instruction.
691bool IfConverter::RescanInstructions(
692 MachineBasicBlock::iterator &TIB, MachineBasicBlock::iterator &FIB,
693 MachineBasicBlock::iterator &TIE, MachineBasicBlock::iterator &FIE,
694 BBInfo &TrueBBI, BBInfo &FalseBBI) const {
695 bool BranchUnpredicable = true;
696 TrueBBI.IsUnpredicable = FalseBBI.IsUnpredicable = false;
697 ScanInstructions(TrueBBI, TIB, TIE, BranchUnpredicable);
698 if (TrueBBI.IsUnpredicable)
699 return false;
700 ScanInstructions(FalseBBI, FIB, FIE, BranchUnpredicable);
701 if (FalseBBI.IsUnpredicable)
702 return false;
703 if (TrueBBI.ClobbersPred && FalseBBI.ClobbersPred)
704 return false;
705 return true;
Kyle Butt9b6d99b2016-07-27 20:19:33 +0000706}
Evan Cheng51eb2c32007-06-18 08:37:25 +0000707
Kyle Butt092c4dd2016-08-29 18:27:12 +0000708#ifndef NDEBUG
709static void verifySameBranchInstructions(
710 MachineBasicBlock *MBB1,
711 MachineBasicBlock *MBB2) {
Kyle Buttc4614b32017-01-26 20:02:47 +0000712 const MachineBasicBlock::reverse_iterator B1 = MBB1->rend();
713 const MachineBasicBlock::reverse_iterator B2 = MBB2->rend();
714 MachineBasicBlock::reverse_iterator E1 = MBB1->rbegin();
715 MachineBasicBlock::reverse_iterator E2 = MBB2->rbegin();
716 while (E1 != B1 && E2 != B2) {
717 skipDebugInstructionsForward(E1, B1);
718 skipDebugInstructionsForward(E2, B2);
719 if (E1 == B1 && E2 == B2)
Kyle Butt092c4dd2016-08-29 18:27:12 +0000720 break;
721
Kyle Buttc4614b32017-01-26 20:02:47 +0000722 if (E1 == B1) {
Kyle Butt092c4dd2016-08-29 18:27:12 +0000723 assert(!E2->isBranch() && "Branch mis-match, one block is empty.");
724 break;
725 }
Kyle Buttc4614b32017-01-26 20:02:47 +0000726 if (E2 == B2) {
Kyle Butt092c4dd2016-08-29 18:27:12 +0000727 assert(!E1->isBranch() && "Branch mis-match, one block is empty.");
728 break;
729 }
730
731 if (E1->isBranch() || E2->isBranch())
732 assert(E1->isIdenticalTo(*E2) &&
733 "Branch mis-match, branch instructions don't match.");
734 else
735 break;
Kyle Buttc4614b32017-01-26 20:02:47 +0000736 ++E1;
737 ++E2;
Kyle Butt092c4dd2016-08-29 18:27:12 +0000738 }
739}
740#endif
741
Kyle Butta8c73712016-08-24 21:34:27 +0000742/// ValidForkedDiamond - Returns true if the 'true' and 'false' blocks (along
743/// with their common predecessor) form a diamond if a common tail block is
744/// extracted.
745/// While not strictly a diamond, this pattern would form a diamond if
746/// tail-merging had merged the shared tails.
747/// EBB
748/// _/ \_
749/// | |
750/// TBB FBB
751/// / \ / \
752/// FalseBB TrueBB FalseBB
753/// Currently only handles analyzable branches.
754/// Specifically excludes actual diamonds to avoid overlap.
755bool IfConverter::ValidForkedDiamond(
756 BBInfo &TrueBBI, BBInfo &FalseBBI,
757 unsigned &Dups1, unsigned &Dups2,
758 BBInfo &TrueBBICalc, BBInfo &FalseBBICalc) const {
759 Dups1 = Dups2 = 0;
760 if (TrueBBI.IsBeingAnalyzed || TrueBBI.IsDone ||
761 FalseBBI.IsBeingAnalyzed || FalseBBI.IsDone)
762 return false;
763
764 if (!TrueBBI.IsBrAnalyzable || !FalseBBI.IsBrAnalyzable)
765 return false;
766 // Don't IfConvert blocks that can't be folded into their predecessor.
767 if (TrueBBI.BB->pred_size() > 1 || FalseBBI.BB->pred_size() > 1)
768 return false;
769
770 // This function is specifically looking for conditional tails, as
771 // unconditional tails are already handled by the standard diamond case.
772 if (TrueBBI.BrCond.size() == 0 ||
773 FalseBBI.BrCond.size() == 0)
774 return false;
775
776 MachineBasicBlock *TT = TrueBBI.TrueBB;
777 MachineBasicBlock *TF = TrueBBI.FalseBB;
778 MachineBasicBlock *FT = FalseBBI.TrueBB;
779 MachineBasicBlock *FF = FalseBBI.FalseBB;
780
781 if (!TT)
782 TT = getNextBlock(*TrueBBI.BB);
783 if (!TF)
784 TF = getNextBlock(*TrueBBI.BB);
785 if (!FT)
786 FT = getNextBlock(*FalseBBI.BB);
787 if (!FF)
788 FF = getNextBlock(*FalseBBI.BB);
789
790 if (!TT || !TF)
791 return false;
792
793 // Check successors. If they don't match, bail.
794 if (!((TT == FT && TF == FF) || (TF == FT && TT == FF)))
795 return false;
796
797 bool FalseReversed = false;
798 if (TF == FT && TT == FF) {
799 // If the branches are opposing, but we can't reverse, don't do it.
800 if (!FalseBBI.IsBrReversible)
801 return false;
802 FalseReversed = true;
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000803 reverseBranchCondition(FalseBBI);
Kyle Butta8c73712016-08-24 21:34:27 +0000804 }
805 auto UnReverseOnExit = make_scope_exit([&]() {
806 if (FalseReversed)
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000807 reverseBranchCondition(FalseBBI);
Kyle Butta8c73712016-08-24 21:34:27 +0000808 });
809
810 // Count duplicate instructions at the beginning of the true and false blocks.
811 MachineBasicBlock::iterator TIB = TrueBBI.BB->begin();
812 MachineBasicBlock::iterator FIB = FalseBBI.BB->begin();
813 MachineBasicBlock::iterator TIE = TrueBBI.BB->end();
814 MachineBasicBlock::iterator FIE = FalseBBI.BB->end();
815 if(!CountDuplicatedInstructions(TIB, FIB, TIE, FIE, Dups1, Dups2,
816 *TrueBBI.BB, *FalseBBI.BB,
817 /* SkipUnconditionalBranches */ true))
818 return false;
819
820 TrueBBICalc.BB = TrueBBI.BB;
821 FalseBBICalc.BB = FalseBBI.BB;
822 if (!RescanInstructions(TIB, FIB, TIE, FIE, TrueBBICalc, FalseBBICalc))
823 return false;
824
825 // The size is used to decide whether to if-convert, and the shared portions
826 // are subtracted off. Because of the subtraction, we just use the size that
827 // was calculated by the original ScanInstructions, as it is correct.
828 TrueBBICalc.NonPredSize = TrueBBI.NonPredSize;
829 FalseBBICalc.NonPredSize = FalseBBI.NonPredSize;
830 return true;
831}
832
Kyle Butt9b6d99b2016-07-27 20:19:33 +0000833/// ValidDiamond - Returns true if the 'true' and 'false' blocks (along
834/// with their common predecessor) forms a valid diamond shape for ifcvt.
Kyle Butt6262ca32016-08-24 21:34:24 +0000835bool IfConverter::ValidDiamond(
836 BBInfo &TrueBBI, BBInfo &FalseBBI,
837 unsigned &Dups1, unsigned &Dups2,
838 BBInfo &TrueBBICalc, BBInfo &FalseBBICalc) const {
Kyle Butt9b6d99b2016-07-27 20:19:33 +0000839 Dups1 = Dups2 = 0;
840 if (TrueBBI.IsBeingAnalyzed || TrueBBI.IsDone ||
841 FalseBBI.IsBeingAnalyzed || FalseBBI.IsDone)
842 return false;
843
844 MachineBasicBlock *TT = TrueBBI.TrueBB;
845 MachineBasicBlock *FT = FalseBBI.TrueBB;
846
847 if (!TT && blockAlwaysFallThrough(TrueBBI))
Matthias Braun08f47042016-08-17 02:52:01 +0000848 TT = getNextBlock(*TrueBBI.BB);
Kyle Butt9b6d99b2016-07-27 20:19:33 +0000849 if (!FT && blockAlwaysFallThrough(FalseBBI))
Matthias Braun08f47042016-08-17 02:52:01 +0000850 FT = getNextBlock(*FalseBBI.BB);
Kyle Butt9b6d99b2016-07-27 20:19:33 +0000851 if (TT != FT)
852 return false;
853 if (!TT && (TrueBBI.IsBrAnalyzable || FalseBBI.IsBrAnalyzable))
854 return false;
855 if (TrueBBI.BB->pred_size() > 1 || FalseBBI.BB->pred_size() > 1)
856 return false;
857
858 // FIXME: Allow true block to have an early exit?
Kyle Butt6262ca32016-08-24 21:34:24 +0000859 if (TrueBBI.FalseBB || FalseBBI.FalseBB)
Kyle Butt9b6d99b2016-07-27 20:19:33 +0000860 return false;
861
862 // Count duplicate instructions at the beginning and end of the true and
863 // false blocks.
Kyle Butt6262ca32016-08-24 21:34:24 +0000864 // Skip unconditional branches only if we are considering an analyzable
865 // diamond. Otherwise the branches must be the same.
866 bool SkipUnconditionalBranches =
867 TrueBBI.IsBrAnalyzable && FalseBBI.IsBrAnalyzable;
Kyle Butt9b6d99b2016-07-27 20:19:33 +0000868 MachineBasicBlock::iterator TIB = TrueBBI.BB->begin();
869 MachineBasicBlock::iterator FIB = FalseBBI.BB->begin();
870 MachineBasicBlock::iterator TIE = TrueBBI.BB->end();
871 MachineBasicBlock::iterator FIE = FalseBBI.BB->end();
Kyle Butt6262ca32016-08-24 21:34:24 +0000872 if(!CountDuplicatedInstructions(TIB, FIB, TIE, FIE, Dups1, Dups2,
873 *TrueBBI.BB, *FalseBBI.BB,
874 SkipUnconditionalBranches))
875 return false;
876
877 TrueBBICalc.BB = TrueBBI.BB;
878 FalseBBICalc.BB = FalseBBI.BB;
879 if (!RescanInstructions(TIB, FIB, TIE, FIE, TrueBBICalc, FalseBBICalc))
880 return false;
881 // The size is used to decide whether to if-convert, and the shared portions
882 // are subtracted off. Because of the subtraction, we just use the size that
883 // was calculated by the original ScanInstructions, as it is correct.
884 TrueBBICalc.NonPredSize = TrueBBI.NonPredSize;
885 FalseBBICalc.NonPredSize = FalseBBI.NonPredSize;
Evan Cheng51eb2c32007-06-18 08:37:25 +0000886 return true;
Evan Chengbe9859e2007-06-07 02:12:15 +0000887}
888
Kyle Butt54bf3ce2016-08-06 01:52:34 +0000889/// AnalyzeBranches - Look at the branches at the end of a block to determine if
890/// the block is predicable.
891void IfConverter::AnalyzeBranches(BBInfo &BBI) {
Evan Cheng4dd31a72007-06-11 22:26:22 +0000892 if (BBI.IsDone)
893 return;
894
Craig Topperc0196b12014-04-14 00:51:57 +0000895 BBI.TrueBB = BBI.FalseBB = nullptr;
Evan Cheng4dd31a72007-06-11 22:26:22 +0000896 BBI.BrCond.clear();
897 BBI.IsBrAnalyzable =
Jacques Pienaar71c30a12016-07-15 14:41:04 +0000898 !TII->analyzeBranch(*BBI.BB, BBI.TrueBB, BBI.FalseBB, BBI.BrCond);
Kyle Butta8c73712016-08-24 21:34:27 +0000899 SmallVector<MachineOperand, 4> RevCond(BBI.BrCond.begin(), BBI.BrCond.end());
900 BBI.IsBrReversible = (RevCond.size() == 0) ||
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000901 !TII->reverseBranchCondition(RevCond);
Craig Topperc0196b12014-04-14 00:51:57 +0000902 BBI.HasFallThrough = BBI.IsBrAnalyzable && BBI.FalseBB == nullptr;
Evan Cheng4dd31a72007-06-11 22:26:22 +0000903
904 if (BBI.BrCond.size()) {
905 // No false branch. This BB must end with a conditional branch and a
906 // fallthrough.
907 if (!BBI.FalseBB)
Bob Wilson81051442010-06-15 22:18:54 +0000908 BBI.FalseBB = findFalseBlock(BBI.BB, BBI.TrueBB);
Evan Chengb9bff582009-06-15 21:24:34 +0000909 if (!BBI.FalseBB) {
910 // Malformed bcc? True and false blocks are the same?
911 BBI.IsUnpredicable = true;
Evan Chengb9bff582009-06-15 21:24:34 +0000912 }
Evan Cheng4dd31a72007-06-11 22:26:22 +0000913 }
Kyle Butt54bf3ce2016-08-06 01:52:34 +0000914}
Evan Cheng4dd31a72007-06-11 22:26:22 +0000915
Kyle Butt54bf3ce2016-08-06 01:52:34 +0000916/// ScanInstructions - Scan all the instructions in the block to determine if
917/// the block is predicable. In most cases, that means all the instructions
918/// in the block are isPredicable(). Also checks if the block contains any
919/// instruction which can clobber a predicate (e.g. condition code register).
920/// If so, the block is not predicable unless it's the last instruction.
921void IfConverter::ScanInstructions(BBInfo &BBI,
922 MachineBasicBlock::iterator &Begin,
Kyle Butt6262ca32016-08-24 21:34:24 +0000923 MachineBasicBlock::iterator &End,
924 bool BranchUnpredicable) const {
Kyle Butt54bf3ce2016-08-06 01:52:34 +0000925 if (BBI.IsDone || BBI.IsUnpredicable)
926 return;
927
928 bool AlreadyPredicated = !BBI.Predicate.empty();
929
Evan Cheng4dd31a72007-06-11 22:26:22 +0000930 BBI.NonPredSize = 0;
Bob Wilsonefd360c2010-10-26 00:02:21 +0000931 BBI.ExtraCost = 0;
Evan Chengdebf9c52010-11-03 00:45:17 +0000932 BBI.ExtraCost2 = 0;
Evan Cheng4dd31a72007-06-11 22:26:22 +0000933 BBI.ClobbersPred = false;
Matthias Braunb1e05582016-08-17 02:51:59 +0000934 for (MachineInstr &MI : make_range(Begin, End)) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000935 if (MI.isDebugValue())
Jim Grosbacha1e08fb2010-06-04 23:01:26 +0000936 continue;
937
Justin Lebar7dba2e02016-04-15 01:38:41 +0000938 // It's unsafe to duplicate convergent instructions in this context, so set
939 // BBI.CannotBeCopied to true if MI is convergent. To see why, consider the
940 // following CFG, which is subject to our "simple" transformation.
941 //
942 // BB0 // if (c1) goto BB1; else goto BB2;
943 // / \
944 // BB1 |
945 // | BB2 // if (c2) goto TBB; else goto FBB;
946 // | / |
947 // | / |
948 // TBB |
949 // | |
950 // | FBB
951 // |
952 // exit
953 //
954 // Suppose we want to move TBB's contents up into BB1 and BB2 (in BB1 they'd
955 // be unconditional, and in BB2, they'd be predicated upon c2), and suppose
956 // TBB contains a convergent instruction. This is safe iff doing so does
957 // not add a control-flow dependency to the convergent instruction -- i.e.,
958 // it's safe iff the set of control flows that leads us to the convergent
959 // instruction does not get smaller after the transformation.
960 //
961 // Originally we executed TBB if c1 || c2. After the transformation, there
962 // are two copies of TBB's instructions. We get to the first if c1, and we
963 // get to the second if !c1 && c2.
964 //
965 // There are clearly fewer ways to satisfy the condition "c1" than
966 // "c1 || c2". Since we've shrunk the set of control flows which lead to
967 // our convergent instruction, the transformation is unsafe.
968 if (MI.isNotDuplicable() || MI.isConvergent())
Evan Cheng23402fc2007-06-15 21:18:05 +0000969 BBI.CannotBeCopied = true;
970
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000971 bool isPredicated = TII->isPredicated(MI);
972 bool isCondBr = BBI.IsBrAnalyzable && MI.isConditionalBranch();
Evan Cheng4dd31a72007-06-11 22:26:22 +0000973
Kyle Butt6262ca32016-08-24 21:34:24 +0000974 if (BranchUnpredicable && MI.isBranch()) {
975 BBI.IsUnpredicable = true;
976 return;
977 }
978
Joey Goulya5153cb2013-09-09 14:21:49 +0000979 // A conditional branch is not predicable, but it may be eliminated.
980 if (isCondBr)
981 continue;
982
983 if (!isPredicated) {
984 BBI.NonPredSize++;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000985 unsigned ExtraPredCost = TII->getPredicationCost(MI);
986 unsigned NumCycles = SchedModel.computeInstrLatency(&MI, false);
Joey Goulya5153cb2013-09-09 14:21:49 +0000987 if (NumCycles > 1)
988 BBI.ExtraCost += NumCycles-1;
989 BBI.ExtraCost2 += ExtraPredCost;
990 } else if (!AlreadyPredicated) {
991 // FIXME: This instruction is already predicated before the
992 // if-conversion pass. It's probably something like a conditional move.
993 // Mark this block unpredicable for now.
994 BBI.IsUnpredicable = true;
995 return;
Evan Cheng96c14572007-07-06 23:24:39 +0000996 }
Evan Cheng4dd31a72007-06-11 22:26:22 +0000997
998 if (BBI.ClobbersPred && !isPredicated) {
999 // Predicate modification instruction should end the block (except for
1000 // already predicated instructions and end of block branches).
Evan Cheng4dd31a72007-06-11 22:26:22 +00001001 // Predicate may have been modified, the subsequent (currently)
Evan Cheng96c14572007-07-06 23:24:39 +00001002 // unpredicated instructions cannot be correctly predicated.
Evan Cheng4dd31a72007-06-11 22:26:22 +00001003 BBI.IsUnpredicable = true;
1004 return;
1005 }
1006
Evan Chengfbc73092007-07-10 17:50:43 +00001007 // FIXME: Make use of PredDefs? e.g. ADDC, SUBC sets predicates but are
1008 // still potentially predicable.
1009 std::vector<MachineOperand> PredDefs;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001010 if (TII->DefinesPredicate(MI, PredDefs))
Evan Cheng4dd31a72007-06-11 22:26:22 +00001011 BBI.ClobbersPred = true;
1012
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001013 if (!TII->isPredicable(MI)) {
Evan Cheng4dd31a72007-06-11 22:26:22 +00001014 BBI.IsUnpredicable = true;
1015 return;
1016 }
1017 }
1018}
1019
Matthias Braun2c931792016-08-17 02:51:57 +00001020/// Determine if the block is a suitable candidate to be predicated by the
1021/// specified predicate.
Kyle Butt6262ca32016-08-24 21:34:24 +00001022/// @param BBI BBInfo for the block to check
1023/// @param Pred Predicate array for the branch that leads to BBI
1024/// @param isTriangle true if the Analysis is for a triangle
1025/// @param RevBranch true if Reverse(Pred) leads to BBI (e.g. BBI is the false
1026/// case
1027/// @param hasCommonTail true if BBI shares a tail with a sibling block that
1028/// contains any instruction that would make the block unpredicable.
Evan Cheng4dd31a72007-06-11 22:26:22 +00001029bool IfConverter::FeasibilityAnalysis(BBInfo &BBI,
Owen Anderson4f6bf042008-08-14 22:49:33 +00001030 SmallVectorImpl<MachineOperand> &Pred,
Kyle Butt6262ca32016-08-24 21:34:24 +00001031 bool isTriangle, bool RevBranch,
1032 bool hasCommonTail) {
Evan Cheng3a51c852007-06-16 09:34:52 +00001033 // If the block is dead or unpredicable, then it cannot be predicated.
Kyle Butt6262ca32016-08-24 21:34:24 +00001034 // Two blocks may share a common unpredicable tail, but this doesn't prevent
1035 // them from being if-converted. The non-shared portion is assumed to have
1036 // been checked
1037 if (BBI.IsDone || (BBI.IsUnpredicable && !hasCommonTail))
Evan Cheng4dd31a72007-06-11 22:26:22 +00001038 return false;
1039
Ahmed Bougacha7173b662015-03-21 01:23:15 +00001040 // If it is already predicated but we couldn't analyze its terminator, the
1041 // latter might fallthrough, but we can't determine where to.
1042 // Conservatively avoid if-converting again.
1043 if (BBI.Predicate.size() && !BBI.IsBrAnalyzable)
1044 return false;
1045
Quentin Colombetbdab2272013-07-24 20:20:37 +00001046 // If it is already predicated, check if the new predicate subsumes
1047 // its predicate.
1048 if (BBI.Predicate.size() && !TII->SubsumesPredicate(Pred, BBI.Predicate))
Evan Cheng4dd31a72007-06-11 22:26:22 +00001049 return false;
1050
Kyle Butt6262ca32016-08-24 21:34:24 +00001051 if (!hasCommonTail && BBI.BrCond.size()) {
Evan Cheng4dd31a72007-06-11 22:26:22 +00001052 if (!isTriangle)
1053 return false;
1054
Bob Wilson2371f4f2009-05-13 23:25:24 +00001055 // Test predicate subsumption.
Owen Anderson4f6bf042008-08-14 22:49:33 +00001056 SmallVector<MachineOperand, 4> RevPred(Pred.begin(), Pred.end());
1057 SmallVector<MachineOperand, 4> Cond(BBI.BrCond.begin(), BBI.BrCond.end());
Evan Cheng4dd31a72007-06-11 22:26:22 +00001058 if (RevBranch) {
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +00001059 if (TII->reverseBranchCondition(Cond))
Evan Cheng4dd31a72007-06-11 22:26:22 +00001060 return false;
1061 }
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +00001062 if (TII->reverseBranchCondition(RevPred) ||
Evan Cheng4dd31a72007-06-11 22:26:22 +00001063 !TII->SubsumesPredicate(Cond, RevPred))
1064 return false;
1065 }
1066
1067 return true;
1068}
1069
Matthias Braun2c931792016-08-17 02:51:57 +00001070/// Analyze the structure of the sub-CFG starting from the specified block.
1071/// Record its successors and whether it looks like an if-conversion candidate.
Justin Lebar3a7bc572016-02-22 17:51:28 +00001072void IfConverter::AnalyzeBlock(
Matthias Braun08f47042016-08-17 02:52:01 +00001073 MachineBasicBlock &MBB, std::vector<std::unique_ptr<IfcvtToken>> &Tokens) {
Akira Hatanaka14348aa2015-06-24 20:34:35 +00001074 struct BBState {
Matthias Braun08f47042016-08-17 02:52:01 +00001075 BBState(MachineBasicBlock &MBB) : MBB(&MBB), SuccsAnalyzed(false) {}
Akira Hatanaka14348aa2015-06-24 20:34:35 +00001076 MachineBasicBlock *MBB;
Evan Chengf5e53a52007-05-16 02:00:57 +00001077
Akira Hatanaka14348aa2015-06-24 20:34:35 +00001078 /// This flag is true if MBB's successors have been analyzed.
1079 bool SuccsAnalyzed;
1080 };
Evan Cheng0f745da2007-05-18 00:20:58 +00001081
Akira Hatanaka14348aa2015-06-24 20:34:35 +00001082 // Push MBB to the stack.
1083 SmallVector<BBState, 16> BBStack(1, MBB);
Evan Cheng4dd31a72007-06-11 22:26:22 +00001084
Akira Hatanaka14348aa2015-06-24 20:34:35 +00001085 while (!BBStack.empty()) {
1086 BBState &State = BBStack.back();
1087 MachineBasicBlock *BB = State.MBB;
1088 BBInfo &BBI = BBAnalysis[BB->getNumber()];
Evan Cheng4dd31a72007-06-11 22:26:22 +00001089
Akira Hatanaka14348aa2015-06-24 20:34:35 +00001090 if (!State.SuccsAnalyzed) {
1091 if (BBI.IsAnalyzed || BBI.IsBeingAnalyzed) {
1092 BBStack.pop_back();
1093 continue;
1094 }
Evan Cheng9030b982007-06-06 10:16:17 +00001095
Akira Hatanaka14348aa2015-06-24 20:34:35 +00001096 BBI.BB = BB;
1097 BBI.IsBeingAnalyzed = true;
Evan Cheng4dd31a72007-06-11 22:26:22 +00001098
Kyle Butt54bf3ce2016-08-06 01:52:34 +00001099 AnalyzeBranches(BBI);
1100 MachineBasicBlock::iterator Begin = BBI.BB->begin();
1101 MachineBasicBlock::iterator End = BBI.BB->end();
1102 ScanInstructions(BBI, Begin, End);
Evan Chengb9bff582009-06-15 21:24:34 +00001103
Akira Hatanaka14348aa2015-06-24 20:34:35 +00001104 // Unanalyzable or ends with fallthrough or unconditional branch, or if is
1105 // not considered for ifcvt anymore.
1106 if (!BBI.IsBrAnalyzable || BBI.BrCond.empty() || BBI.IsDone) {
1107 BBI.IsBeingAnalyzed = false;
1108 BBI.IsAnalyzed = true;
1109 BBStack.pop_back();
1110 continue;
1111 }
Evan Cheng4dd31a72007-06-11 22:26:22 +00001112
Akira Hatanaka14348aa2015-06-24 20:34:35 +00001113 // Do not ifcvt if either path is a back edge to the entry block.
1114 if (BBI.TrueBB == BB || BBI.FalseBB == BB) {
1115 BBI.IsBeingAnalyzed = false;
1116 BBI.IsAnalyzed = true;
1117 BBStack.pop_back();
1118 continue;
1119 }
Evan Cheng4dd31a72007-06-11 22:26:22 +00001120
Akira Hatanaka14348aa2015-06-24 20:34:35 +00001121 // Do not ifcvt if true and false fallthrough blocks are the same.
1122 if (!BBI.FalseBB) {
1123 BBI.IsBeingAnalyzed = false;
1124 BBI.IsAnalyzed = true;
1125 BBStack.pop_back();
1126 continue;
1127 }
Evan Cheng7783f822007-06-08 09:36:04 +00001128
Akira Hatanaka14348aa2015-06-24 20:34:35 +00001129 // Push the False and True blocks to the stack.
1130 State.SuccsAnalyzed = true;
Matthias Braun08f47042016-08-17 02:52:01 +00001131 BBStack.push_back(*BBI.FalseBB);
1132 BBStack.push_back(*BBI.TrueBB);
Akira Hatanaka14348aa2015-06-24 20:34:35 +00001133 continue;
1134 }
Benjamin Kramer2016f0e2010-09-29 22:38:50 +00001135
Akira Hatanaka14348aa2015-06-24 20:34:35 +00001136 BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
1137 BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
Jakub Staszak15e5b742011-08-03 22:34:43 +00001138
Akira Hatanaka14348aa2015-06-24 20:34:35 +00001139 if (TrueBBI.IsDone && FalseBBI.IsDone) {
1140 BBI.IsBeingAnalyzed = false;
1141 BBI.IsAnalyzed = true;
1142 BBStack.pop_back();
1143 continue;
1144 }
1145
1146 SmallVector<MachineOperand, 4>
1147 RevCond(BBI.BrCond.begin(), BBI.BrCond.end());
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +00001148 bool CanRevCond = !TII->reverseBranchCondition(RevCond);
Akira Hatanaka14348aa2015-06-24 20:34:35 +00001149
1150 unsigned Dups = 0;
1151 unsigned Dups2 = 0;
1152 bool TNeedSub = !TrueBBI.Predicate.empty();
1153 bool FNeedSub = !FalseBBI.Predicate.empty();
1154 bool Enqueued = false;
1155
1156 BranchProbability Prediction = MBPI->getEdgeProbability(BB, TrueBBI.BB);
1157
Kyle Butt6262ca32016-08-24 21:34:24 +00001158 if (CanRevCond) {
1159 BBInfo TrueBBICalc, FalseBBICalc;
Kyle Butta8c73712016-08-24 21:34:27 +00001160 auto feasibleDiamond = [&]() {
1161 bool MeetsSize = MeetIfcvtSizeLimit(
1162 *TrueBBI.BB, (TrueBBICalc.NonPredSize - (Dups + Dups2) +
1163 TrueBBICalc.ExtraCost), TrueBBICalc.ExtraCost2,
1164 *FalseBBI.BB, (FalseBBICalc.NonPredSize - (Dups + Dups2) +
1165 FalseBBICalc.ExtraCost), FalseBBICalc.ExtraCost2,
1166 Prediction);
1167 bool TrueFeasible = FeasibilityAnalysis(TrueBBI, BBI.BrCond,
1168 /* IsTriangle */ false, /* RevCond */ false,
1169 /* hasCommonTail */ true);
1170 bool FalseFeasible = FeasibilityAnalysis(FalseBBI, RevCond,
1171 /* IsTriangle */ false, /* RevCond */ false,
1172 /* hasCommonTail */ true);
1173 return MeetsSize && TrueFeasible && FalseFeasible;
1174 };
1175
Kyle Butt6262ca32016-08-24 21:34:24 +00001176 if (ValidDiamond(TrueBBI, FalseBBI, Dups, Dups2,
Kyle Butta8c73712016-08-24 21:34:27 +00001177 TrueBBICalc, FalseBBICalc)) {
1178 if (feasibleDiamond()) {
1179 // Diamond:
1180 // EBB
1181 // / \_
1182 // | |
1183 // TBB FBB
1184 // \ /
1185 // TailBB
1186 // Note TailBB can be empty.
1187 Tokens.push_back(llvm::make_unique<IfcvtToken>(
1188 BBI, ICDiamond, TNeedSub | FNeedSub, Dups, Dups2,
1189 (bool) TrueBBICalc.ClobbersPred, (bool) FalseBBICalc.ClobbersPred));
1190 Enqueued = true;
1191 }
1192 } else if (ValidForkedDiamond(TrueBBI, FalseBBI, Dups, Dups2,
1193 TrueBBICalc, FalseBBICalc)) {
1194 if (feasibleDiamond()) {
1195 // ForkedDiamond:
1196 // if TBB and FBB have a common tail that includes their conditional
1197 // branch instructions, then we can If Convert this pattern.
1198 // EBB
1199 // _/ \_
1200 // | |
1201 // TBB FBB
1202 // / \ / \
1203 // FalseBB TrueBB FalseBB
1204 //
1205 Tokens.push_back(llvm::make_unique<IfcvtToken>(
1206 BBI, ICForkedDiamond, TNeedSub | FNeedSub, Dups, Dups2,
1207 (bool) TrueBBICalc.ClobbersPred, (bool) FalseBBICalc.ClobbersPred));
1208 Enqueued = true;
1209 }
Kyle Butt6262ca32016-08-24 21:34:24 +00001210 }
Evan Cheng3a51c852007-06-16 09:34:52 +00001211 }
1212
Akira Hatanaka14348aa2015-06-24 20:34:35 +00001213 if (ValidTriangle(TrueBBI, FalseBBI, false, Dups, Prediction) &&
1214 MeetIfcvtSizeLimit(*TrueBBI.BB, TrueBBI.NonPredSize + TrueBBI.ExtraCost,
1215 TrueBBI.ExtraCost2, Prediction) &&
1216 FeasibilityAnalysis(TrueBBI, BBI.BrCond, true)) {
1217 // Triangle:
1218 // EBB
1219 // | \_
1220 // | |
1221 // | TBB
1222 // | /
1223 // FBB
Justin Lebar3a7bc572016-02-22 17:51:28 +00001224 Tokens.push_back(
1225 llvm::make_unique<IfcvtToken>(BBI, ICTriangle, TNeedSub, Dups));
Akira Hatanaka14348aa2015-06-24 20:34:35 +00001226 Enqueued = true;
1227 }
1228
1229 if (ValidTriangle(TrueBBI, FalseBBI, true, Dups, Prediction) &&
1230 MeetIfcvtSizeLimit(*TrueBBI.BB, TrueBBI.NonPredSize + TrueBBI.ExtraCost,
1231 TrueBBI.ExtraCost2, Prediction) &&
1232 FeasibilityAnalysis(TrueBBI, BBI.BrCond, true, true)) {
Justin Lebar3a7bc572016-02-22 17:51:28 +00001233 Tokens.push_back(
1234 llvm::make_unique<IfcvtToken>(BBI, ICTriangleRev, TNeedSub, Dups));
Akira Hatanaka14348aa2015-06-24 20:34:35 +00001235 Enqueued = true;
1236 }
1237
1238 if (ValidSimple(TrueBBI, Dups, Prediction) &&
1239 MeetIfcvtSizeLimit(*TrueBBI.BB, TrueBBI.NonPredSize + TrueBBI.ExtraCost,
1240 TrueBBI.ExtraCost2, Prediction) &&
1241 FeasibilityAnalysis(TrueBBI, BBI.BrCond)) {
1242 // Simple (split, no rejoin):
1243 // EBB
1244 // | \_
1245 // | |
1246 // | TBB---> exit
1247 // |
1248 // FBB
Justin Lebar3a7bc572016-02-22 17:51:28 +00001249 Tokens.push_back(
1250 llvm::make_unique<IfcvtToken>(BBI, ICSimple, TNeedSub, Dups));
Akira Hatanaka14348aa2015-06-24 20:34:35 +00001251 Enqueued = true;
1252 }
1253
1254 if (CanRevCond) {
1255 // Try the other path...
1256 if (ValidTriangle(FalseBBI, TrueBBI, false, Dups,
1257 Prediction.getCompl()) &&
1258 MeetIfcvtSizeLimit(*FalseBBI.BB,
1259 FalseBBI.NonPredSize + FalseBBI.ExtraCost,
1260 FalseBBI.ExtraCost2, Prediction.getCompl()) &&
1261 FeasibilityAnalysis(FalseBBI, RevCond, true)) {
Justin Lebar3a7bc572016-02-22 17:51:28 +00001262 Tokens.push_back(llvm::make_unique<IfcvtToken>(BBI, ICTriangleFalse,
1263 FNeedSub, Dups));
Akira Hatanaka14348aa2015-06-24 20:34:35 +00001264 Enqueued = true;
1265 }
1266
1267 if (ValidTriangle(FalseBBI, TrueBBI, true, Dups,
1268 Prediction.getCompl()) &&
1269 MeetIfcvtSizeLimit(*FalseBBI.BB,
1270 FalseBBI.NonPredSize + FalseBBI.ExtraCost,
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001271 FalseBBI.ExtraCost2, Prediction.getCompl()) &&
Evan Cheng3a51c852007-06-16 09:34:52 +00001272 FeasibilityAnalysis(FalseBBI, RevCond, true, true)) {
Justin Lebar3a7bc572016-02-22 17:51:28 +00001273 Tokens.push_back(
1274 llvm::make_unique<IfcvtToken>(BBI, ICTriangleFRev, FNeedSub, Dups));
Akira Hatanaka14348aa2015-06-24 20:34:35 +00001275 Enqueued = true;
1276 }
1277
1278 if (ValidSimple(FalseBBI, Dups, Prediction.getCompl()) &&
1279 MeetIfcvtSizeLimit(*FalseBBI.BB,
1280 FalseBBI.NonPredSize + FalseBBI.ExtraCost,
1281 FalseBBI.ExtraCost2, Prediction.getCompl()) &&
1282 FeasibilityAnalysis(FalseBBI, RevCond)) {
Justin Lebar3a7bc572016-02-22 17:51:28 +00001283 Tokens.push_back(
1284 llvm::make_unique<IfcvtToken>(BBI, ICSimpleFalse, FNeedSub, Dups));
Akira Hatanaka14348aa2015-06-24 20:34:35 +00001285 Enqueued = true;
1286 }
Evan Cheng3a51c852007-06-16 09:34:52 +00001287 }
1288
Akira Hatanaka14348aa2015-06-24 20:34:35 +00001289 BBI.IsEnqueued = Enqueued;
1290 BBI.IsBeingAnalyzed = false;
1291 BBI.IsAnalyzed = true;
1292 BBStack.pop_back();
Evan Chengf5e53a52007-05-16 02:00:57 +00001293 }
Evan Cheng2e82cef2007-05-18 18:14:37 +00001294}
1295
Matthias Braun2c931792016-08-17 02:51:57 +00001296/// Analyze all blocks and find entries for all if-conversion candidates.
Justin Lebar3a7bc572016-02-22 17:51:28 +00001297void IfConverter::AnalyzeBlocks(
1298 MachineFunction &MF, std::vector<std::unique_ptr<IfcvtToken>> &Tokens) {
Matthias Braunb1e05582016-08-17 02:51:59 +00001299 for (MachineBasicBlock &MBB : MF)
Matthias Braun08f47042016-08-17 02:52:01 +00001300 AnalyzeBlock(MBB, Tokens);
Evan Cheng905a8f42007-05-30 19:49:19 +00001301
Evan Cheng20e05992007-06-01 00:12:12 +00001302 // Sort to favor more complex ifcvt scheme.
Evan Cheng3a51c852007-06-16 09:34:52 +00001303 std::stable_sort(Tokens.begin(), Tokens.end(), IfcvtTokenCmp);
Evan Chengf5e53a52007-05-16 02:00:57 +00001304}
1305
Matthias Braun2c931792016-08-17 02:51:57 +00001306/// Returns true either if ToMBB is the next block after MBB or that all the
1307/// intervening blocks are empty (given MBB can fall through to its next block).
Matthias Braun08f47042016-08-17 02:52:01 +00001308static bool canFallThroughTo(MachineBasicBlock &MBB, MachineBasicBlock &ToMBB) {
1309 MachineFunction::iterator PI = MBB.getIterator();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001310 MachineFunction::iterator I = std::next(PI);
Matthias Braun08f47042016-08-17 02:52:01 +00001311 MachineFunction::iterator TI = ToMBB.getIterator();
1312 MachineFunction::iterator E = MBB.getParent()->end();
Evan Chengf128bdc2010-06-16 07:35:02 +00001313 while (I != TI) {
1314 // Check isSuccessor to avoid case where the next block is empty, but
1315 // it's not a successor.
Duncan P. N. Exon Smith5ae59392015-10-09 19:13:58 +00001316 if (I == E || !I->empty() || !PI->isSuccessor(&*I))
Evan Cheng312b7232007-06-04 06:47:22 +00001317 return false;
Evan Chengf128bdc2010-06-16 07:35:02 +00001318 PI = I++;
1319 }
Mikael Holmen21c867c2017-05-10 13:06:13 +00001320 // Finally see if the last I is indeed a successor to PI.
1321 return PI->isSuccessor(&*I);
Evan Chenge26c0912007-05-21 22:22:58 +00001322}
1323
Matthias Braun2c931792016-08-17 02:51:57 +00001324/// Invalidate predecessor BB info so it would be re-analyzed to determine if it
1325/// can be if-converted. If predecessor is already enqueued, dequeue it!
Matthias Braun08f47042016-08-17 02:52:01 +00001326void IfConverter::InvalidatePreds(MachineBasicBlock &MBB) {
1327 for (const MachineBasicBlock *Predecessor : MBB.predecessors()) {
Saleem Abdulrasoolf158ca32014-08-09 17:21:29 +00001328 BBInfo &PBBI = BBAnalysis[Predecessor->getNumber()];
Matthias Braun08f47042016-08-17 02:52:01 +00001329 if (PBBI.IsDone || PBBI.BB == &MBB)
Evan Cheng9fc56c02007-06-14 23:13:19 +00001330 continue;
Evan Chengadd97762007-06-14 23:34:09 +00001331 PBBI.IsAnalyzed = false;
1332 PBBI.IsEnqueued = false;
Evan Chengd0e66912007-05-23 07:23:16 +00001333 }
1334}
1335
Matthias Braun2c931792016-08-17 02:51:57 +00001336/// Inserts an unconditional branch from \p MBB to \p ToMBB.
Matthias Braun08f47042016-08-17 02:52:01 +00001337static void InsertUncondBranch(MachineBasicBlock &MBB, MachineBasicBlock &ToMBB,
Evan Chengc2237ce2007-05-29 22:31:16 +00001338 const TargetInstrInfo *TII) {
Stuart Hastings0125b642010-06-17 22:43:56 +00001339 DebugLoc dl; // FIXME: this is nowhere
Dan Gohman14714cb2008-08-22 16:07:55 +00001340 SmallVector<MachineOperand, 0> NoCond;
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +00001341 TII->insertBranch(MBB, &ToMBB, nullptr, NoCond, dl);
Evan Chengc2237ce2007-05-29 22:31:16 +00001342}
1343
Matthias Braund616ccc2013-10-11 19:04:37 +00001344/// Behaves like LiveRegUnits::StepForward() but also adds implicit uses to all
Jonas Paulsson196986c2016-08-03 05:46:35 +00001345/// values defined in MI which are also live/used by MI.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001346static void UpdatePredRedefs(MachineInstr &MI, LivePhysRegs &Redefs) {
Jonas Paulsson196986c2016-08-03 05:46:35 +00001347 const TargetRegisterInfo *TRI = MI.getParent()->getParent()
1348 ->getSubtarget().getRegisterInfo();
1349
1350 // Before stepping forward past MI, remember which regs were live
1351 // before MI. This is needed to set the Undef flag only when reg is
1352 // dead.
1353 SparseSet<unsigned> LiveBeforeMI;
1354 LiveBeforeMI.setUniverse(TRI->getNumRegs());
Matthias Braunb1e05582016-08-17 02:51:59 +00001355 for (unsigned Reg : Redefs)
Jonas Paulsson196986c2016-08-03 05:46:35 +00001356 LiveBeforeMI.insert(Reg);
1357
Pete Cooper7605e372015-05-05 20:14:22 +00001358 SmallVector<std::pair<unsigned, const MachineOperand*>, 4> Clobbers;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001359 Redefs.stepForward(MI, Clobbers);
Matthias Braund616ccc2013-10-11 19:04:37 +00001360
Pete Cooper7605e372015-05-05 20:14:22 +00001361 // Now add the implicit uses for each of the clobbered values.
Matthias Braun08f47042016-08-17 02:52:01 +00001362 for (auto Clobber : Clobbers) {
Pete Cooper7605e372015-05-05 20:14:22 +00001363 // FIXME: Const cast here is nasty, but better than making StepForward
1364 // take a mutable instruction instead of const.
Matthias Braun08f47042016-08-17 02:52:01 +00001365 unsigned Reg = Clobber.first;
1366 MachineOperand &Op = const_cast<MachineOperand&>(*Clobber.second);
Pete Cooper27483912015-05-06 22:51:04 +00001367 MachineInstr *OpMI = Op.getParent();
Pete Cooper7605e372015-05-05 20:14:22 +00001368 MachineInstrBuilder MIB(*OpMI->getParent()->getParent(), OpMI);
Pete Cooperce9ad752015-05-05 22:09:41 +00001369 if (Op.isRegMask()) {
1370 // First handle regmasks. They clobber any entries in the mask which
1371 // means that we need a def for those registers.
Matthias Braun08f47042016-08-17 02:52:01 +00001372 if (LiveBeforeMI.count(Reg))
1373 MIB.addReg(Reg, RegState::Implicit);
Pete Cooperce9ad752015-05-05 22:09:41 +00001374
1375 // We also need to add an implicit def of this register for the later
1376 // use to read from.
1377 // For the register allocator to have allocated a register clobbered
1378 // by the call which is used later, it must be the case that
1379 // the call doesn't return.
Matthias Braun08f47042016-08-17 02:52:01 +00001380 MIB.addReg(Reg, RegState::Implicit | RegState::Define);
Pete Cooperce9ad752015-05-05 22:09:41 +00001381 continue;
1382 }
1383 assert(Op.isReg() && "Register operand required");
Pete Cooper27483912015-05-06 22:51:04 +00001384 if (Op.isDead()) {
1385 // If we found a dead def, but it needs to be live, then remove the dead
1386 // flag.
1387 if (Redefs.contains(Op.getReg()))
1388 Op.setIsDead(false);
1389 }
Matthias Braun08f47042016-08-17 02:52:01 +00001390 if (LiveBeforeMI.count(Reg))
1391 MIB.addReg(Reg, RegState::Implicit);
Krzysztof Parzyszekdcb1bca2016-09-28 20:07:41 +00001392 else {
1393 bool HasLiveSubReg = false;
1394 for (MCSubRegIterator S(Reg, TRI); S.isValid(); ++S) {
1395 if (!LiveBeforeMI.count(*S))
1396 continue;
1397 HasLiveSubReg = true;
1398 break;
1399 }
1400 if (HasLiveSubReg)
1401 MIB.addReg(Reg, RegState::Implicit);
1402 }
Matthias Braund616ccc2013-10-11 19:04:37 +00001403 }
1404}
1405
Matthias Braun2c931792016-08-17 02:51:57 +00001406/// Remove kill flags from operands with a registers in the \p DontKill set.
Juergen Ributzka310034e2013-12-14 06:52:56 +00001407static void RemoveKills(MachineInstr &MI, const LivePhysRegs &DontKill) {
Duncan P. N. Exon Smithf9ab4162016-02-27 17:05:33 +00001408 for (MIBundleOperands O(MI); O.isValid(); ++O) {
Matthias Braund616ccc2013-10-11 19:04:37 +00001409 if (!O->isReg() || !O->isKill())
1410 continue;
Juergen Ributzka310034e2013-12-14 06:52:56 +00001411 if (DontKill.contains(O->getReg()))
Matthias Braund616ccc2013-10-11 19:04:37 +00001412 O->setIsKill(false);
1413 }
1414}
1415
Matthias Braun2c931792016-08-17 02:51:57 +00001416/// Walks a range of machine instructions and removes kill flags for registers
1417/// in the \p DontKill set.
Matthias Braund616ccc2013-10-11 19:04:37 +00001418static void RemoveKills(MachineBasicBlock::iterator I,
1419 MachineBasicBlock::iterator E,
Juergen Ributzka310034e2013-12-14 06:52:56 +00001420 const LivePhysRegs &DontKill,
Matthias Braund616ccc2013-10-11 19:04:37 +00001421 const MCRegisterInfo &MCRI) {
Matthias Braunb1e05582016-08-17 02:51:59 +00001422 for (MachineInstr &MI : make_range(I, E))
1423 RemoveKills(MI, DontKill);
Evan Chengf128bdc2010-06-16 07:35:02 +00001424}
1425
Matthias Braun2c931792016-08-17 02:51:57 +00001426/// If convert a simple (split, no rejoin) sub-CFG.
Evan Cheng3a51c852007-06-16 09:34:52 +00001427bool IfConverter::IfConvertSimple(BBInfo &BBI, IfcvtKind Kind) {
Evan Chenge26c0912007-05-21 22:22:58 +00001428 BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
1429 BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
1430 BBInfo *CvtBBI = &TrueBBI;
1431 BBInfo *NextBBI = &FalseBBI;
Evan Chengd0e66912007-05-23 07:23:16 +00001432
Owen Anderson4f6bf042008-08-14 22:49:33 +00001433 SmallVector<MachineOperand, 4> Cond(BBI.BrCond.begin(), BBI.BrCond.end());
Evan Cheng3a51c852007-06-16 09:34:52 +00001434 if (Kind == ICSimpleFalse)
Evan Cheng4dcf1e82007-06-01 20:29:21 +00001435 std::swap(CvtBBI, NextBBI);
Evan Cheng23402fc2007-06-15 21:18:05 +00001436
Matthias Braun08f47042016-08-17 02:52:01 +00001437 MachineBasicBlock &CvtMBB = *CvtBBI->BB;
1438 MachineBasicBlock &NextMBB = *NextBBI->BB;
Evan Cheng51eb2c32007-06-18 08:37:25 +00001439 if (CvtBBI->IsDone ||
Matthias Braun08f47042016-08-17 02:52:01 +00001440 (CvtBBI->CannotBeCopied && CvtMBB.pred_size() > 1)) {
Evan Cheng23402fc2007-06-15 21:18:05 +00001441 // Something has changed. It's no longer safe to predicate this block.
Evan Cheng23402fc2007-06-15 21:18:05 +00001442 BBI.IsAnalyzed = false;
1443 CvtBBI->IsAnalyzed = false;
1444 return false;
Evan Cheng4dcf1e82007-06-01 20:29:21 +00001445 }
Evan Chengd0e66912007-05-23 07:23:16 +00001446
Matthias Braun08f47042016-08-17 02:52:01 +00001447 if (CvtMBB.hasAddressTaken())
Evan Cheng8b8e8d82013-05-05 18:03:49 +00001448 // Conservatively abort if-conversion if BB's address is taken.
1449 return false;
1450
Evan Cheng3a51c852007-06-16 09:34:52 +00001451 if (Kind == ICSimpleFalse)
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +00001452 if (TII->reverseBranchCondition(Cond))
Craig Topperee4dab52012-02-05 08:31:47 +00001453 llvm_unreachable("Unable to reverse branch condition!");
Evan Cheng23402fc2007-06-15 21:18:05 +00001454
Matthias Braun0c989a82016-12-08 00:15:51 +00001455 Redefs.init(*TRI);
Matthias Braun0c989a82016-12-08 00:15:51 +00001456 DontKill.init(*TRI);
Matthias Braun11723322017-01-05 20:01:19 +00001457
1458 if (MRI->tracksLiveness()) {
1459 // Initialize liveins to the first BB. These are potentiall redefined by
1460 // predicated instructions.
1461 Redefs.addLiveIns(CvtMBB);
1462 Redefs.addLiveIns(NextMBB);
1463 // Compute a set of registers which must not be killed by instructions in
1464 // BB1: This is everything live-in to BB2.
1465 DontKill.addLiveIns(NextMBB);
1466 }
Evan Chengf128bdc2010-06-16 07:35:02 +00001467
Mikael Holmen45bd32f2017-06-26 09:33:04 +00001468 // Remove the branches from the entry so we can add the contents of the true
1469 // block to it.
1470 BBI.NonPredSize -= TII->removeBranch(*BBI.BB);
1471
Matthias Braun08f47042016-08-17 02:52:01 +00001472 if (CvtMBB.pred_size() > 1) {
Bob Wilson2371f4f2009-05-13 23:25:24 +00001473 // Copy instructions in the true block, predicate them, and add them to
Evan Cheng92fb5452007-06-15 07:36:12 +00001474 // the entry block.
Andrew Trick276dd452013-10-14 20:45:17 +00001475 CopyAndPredicateBlock(BBI, *CvtBBI, Cond);
Hal Finkel95081bf2013-04-10 22:05:25 +00001476
Mikael Holmen8b106802017-08-11 06:57:08 +00001477 // Keep the CFG updated.
Matthias Braun08f47042016-08-17 02:52:01 +00001478 BBI.BB->removeSuccessor(&CvtMBB, true);
Evan Cheng92fb5452007-06-15 07:36:12 +00001479 } else {
Mikael Holmen45bd32f2017-06-26 09:33:04 +00001480 // Predicate the instructions in the true block.
Matthias Braun08f47042016-08-17 02:52:01 +00001481 RemoveKills(CvtMBB.begin(), CvtMBB.end(), DontKill, *TRI);
1482 PredicateBlock(*CvtBBI, CvtMBB.end(), Cond);
Evan Chenge26c0912007-05-21 22:22:58 +00001483
Mikael Holmen8b106802017-08-11 06:57:08 +00001484 // Merge converted block into entry block. The BB to Cvt edge is removed
1485 // by MergeBlocks.
Evan Cheng92fb5452007-06-15 07:36:12 +00001486 MergeBlocks(BBI, *CvtBBI);
1487 }
Evan Chengbe9859e2007-06-07 02:12:15 +00001488
Evan Chenge4ec9182007-06-06 02:08:52 +00001489 bool IterIfcvt = true;
Matthias Braun08f47042016-08-17 02:52:01 +00001490 if (!canFallThroughTo(*BBI.BB, NextMBB)) {
1491 InsertUncondBranch(*BBI.BB, NextMBB, TII);
Evan Cheng2117d1f2007-06-09 01:03:43 +00001492 BBI.HasFallThrough = false;
Evan Cheng7783f822007-06-08 09:36:04 +00001493 // Now ifcvt'd block will look like this:
1494 // BB:
1495 // ...
1496 // t, f = cmp
1497 // if t op
1498 // b BBf
1499 //
1500 // We cannot further ifcvt this block because the unconditional branch
1501 // will have to be predicated on the new condition, that will not be
1502 // available if cmp executes.
1503 IterIfcvt = false;
Evan Chenge4ec9182007-06-06 02:08:52 +00001504 }
Evan Chenge26c0912007-05-21 22:22:58 +00001505
Evan Chengd0e66912007-05-23 07:23:16 +00001506 // Update block info. BB can be iteratively if-converted.
Evan Cheng4dd31a72007-06-11 22:26:22 +00001507 if (!IterIfcvt)
1508 BBI.IsDone = true;
Matthias Braun08f47042016-08-17 02:52:01 +00001509 InvalidatePreds(*BBI.BB);
Evan Cheng4dd31a72007-06-11 22:26:22 +00001510 CvtBBI->IsDone = true;
Evan Chenge26c0912007-05-21 22:22:58 +00001511
1512 // FIXME: Must maintain LiveIns.
Evan Chenge26c0912007-05-21 22:22:58 +00001513 return true;
1514}
1515
Matthias Braun2c931792016-08-17 02:51:57 +00001516/// If convert a triangle sub-CFG.
Evan Cheng3a51c852007-06-16 09:34:52 +00001517bool IfConverter::IfConvertTriangle(BBInfo &BBI, IfcvtKind Kind) {
Evan Chengd0e66912007-05-23 07:23:16 +00001518 BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
Evan Cheng2117d1f2007-06-09 01:03:43 +00001519 BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
1520 BBInfo *CvtBBI = &TrueBBI;
1521 BBInfo *NextBBI = &FalseBBI;
Stuart Hastings0125b642010-06-17 22:43:56 +00001522 DebugLoc dl; // FIXME: this is nowhere
Evan Cheng2117d1f2007-06-09 01:03:43 +00001523
Owen Anderson4f6bf042008-08-14 22:49:33 +00001524 SmallVector<MachineOperand, 4> Cond(BBI.BrCond.begin(), BBI.BrCond.end());
Evan Cheng3a51c852007-06-16 09:34:52 +00001525 if (Kind == ICTriangleFalse || Kind == ICTriangleFRev)
Evan Cheng2117d1f2007-06-09 01:03:43 +00001526 std::swap(CvtBBI, NextBBI);
Evan Cheng23402fc2007-06-15 21:18:05 +00001527
Matthias Braun08f47042016-08-17 02:52:01 +00001528 MachineBasicBlock &CvtMBB = *CvtBBI->BB;
1529 MachineBasicBlock &NextMBB = *NextBBI->BB;
Evan Cheng51eb2c32007-06-18 08:37:25 +00001530 if (CvtBBI->IsDone ||
Matthias Braun08f47042016-08-17 02:52:01 +00001531 (CvtBBI->CannotBeCopied && CvtMBB.pred_size() > 1)) {
Evan Cheng23402fc2007-06-15 21:18:05 +00001532 // Something has changed. It's no longer safe to predicate this block.
Evan Cheng23402fc2007-06-15 21:18:05 +00001533 BBI.IsAnalyzed = false;
1534 CvtBBI->IsAnalyzed = false;
1535 return false;
Evan Cheng2117d1f2007-06-09 01:03:43 +00001536 }
Evan Cheng23402fc2007-06-15 21:18:05 +00001537
Matthias Braun08f47042016-08-17 02:52:01 +00001538 if (CvtMBB.hasAddressTaken())
Evan Cheng8b8e8d82013-05-05 18:03:49 +00001539 // Conservatively abort if-conversion if BB's address is taken.
1540 return false;
1541
Evan Cheng3a51c852007-06-16 09:34:52 +00001542 if (Kind == ICTriangleFalse || Kind == ICTriangleFRev)
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +00001543 if (TII->reverseBranchCondition(Cond))
Craig Topperee4dab52012-02-05 08:31:47 +00001544 llvm_unreachable("Unable to reverse branch condition!");
Evan Cheng23402fc2007-06-15 21:18:05 +00001545
Evan Cheng3a51c852007-06-16 09:34:52 +00001546 if (Kind == ICTriangleRev || Kind == ICTriangleFRev) {
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +00001547 if (reverseBranchCondition(*CvtBBI)) {
Dan Gohman97d95d62008-10-21 03:29:32 +00001548 // BB has been changed, modify its predecessors (except for this
1549 // one) so they don't get ifcvt'ed based on bad intel.
Matthias Braun08f47042016-08-17 02:52:01 +00001550 for (MachineBasicBlock *PBB : CvtMBB.predecessors()) {
Dan Gohman97d95d62008-10-21 03:29:32 +00001551 if (PBB == BBI.BB)
1552 continue;
1553 BBInfo &PBBI = BBAnalysis[PBB->getNumber()];
1554 if (PBBI.IsEnqueued) {
1555 PBBI.IsAnalyzed = false;
1556 PBBI.IsEnqueued = false;
1557 }
Evan Chengadd97762007-06-14 23:34:09 +00001558 }
Evan Cheng9acfa7b2007-06-12 23:54:05 +00001559 }
1560 }
Evan Cheng6a2cf072007-06-01 07:41:07 +00001561
Bob Wilson45814342010-06-19 05:33:57 +00001562 // Initialize liveins to the first BB. These are potentially redefined by
Evan Chengf128bdc2010-06-16 07:35:02 +00001563 // predicated instructions.
Matthias Braun0c989a82016-12-08 00:15:51 +00001564 Redefs.init(*TRI);
Matthias Braun11723322017-01-05 20:01:19 +00001565 if (MRI->tracksLiveness()) {
1566 Redefs.addLiveIns(CvtMBB);
1567 Redefs.addLiveIns(NextMBB);
1568 }
Evan Chengf128bdc2010-06-16 07:35:02 +00001569
Andrew Trick276dd452013-10-14 20:45:17 +00001570 DontKill.clear();
1571
Craig Topperc0196b12014-04-14 00:51:57 +00001572 bool HasEarlyExit = CvtBBI->FalseBB != nullptr;
Cong Houd97c1002015-12-01 05:29:22 +00001573 BranchProbability CvtNext, CvtFalse, BBNext, BBCvt;
Saleem Abdulrasoolf158ca32014-08-09 17:21:29 +00001574
Manman Renb6819182014-01-29 23:18:47 +00001575 if (HasEarlyExit) {
Matthias Braun08f47042016-08-17 02:52:01 +00001576 // Get probabilities before modifying CvtMBB and BBI.BB.
1577 CvtNext = MBPI->getEdgeProbability(&CvtMBB, &NextMBB);
1578 CvtFalse = MBPI->getEdgeProbability(&CvtMBB, CvtBBI->FalseBB);
1579 BBNext = MBPI->getEdgeProbability(BBI.BB, &NextMBB);
1580 BBCvt = MBPI->getEdgeProbability(BBI.BB, &CvtMBB);
Manman Renb6819182014-01-29 23:18:47 +00001581 }
Saleem Abdulrasoolf158ca32014-08-09 17:21:29 +00001582
Mikael Holmen45bd32f2017-06-26 09:33:04 +00001583 // Remove the branches from the entry so we can add the contents of the true
1584 // block to it.
1585 BBI.NonPredSize -= TII->removeBranch(*BBI.BB);
1586
Matthias Braun08f47042016-08-17 02:52:01 +00001587 if (CvtMBB.pred_size() > 1) {
Bob Wilson2371f4f2009-05-13 23:25:24 +00001588 // Copy instructions in the true block, predicate them, and add them to
Evan Cheng92fb5452007-06-15 07:36:12 +00001589 // the entry block.
Andrew Trick276dd452013-10-14 20:45:17 +00001590 CopyAndPredicateBlock(BBI, *CvtBBI, Cond, true);
Evan Cheng92fb5452007-06-15 07:36:12 +00001591 } else {
1592 // Predicate the 'true' block after removing its branch.
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +00001593 CvtBBI->NonPredSize -= TII->removeBranch(CvtMBB);
Matthias Braun08f47042016-08-17 02:52:01 +00001594 PredicateBlock(*CvtBBI, CvtMBB.end(), Cond);
Evan Chenge26c0912007-05-21 22:22:58 +00001595
Mikael Holmence3ec452017-05-12 06:28:58 +00001596 // Now merge the entry of the triangle with the true block.
Bob Wilson1e5da552010-06-29 00:55:23 +00001597 MergeBlocks(BBI, *CvtBBI, false);
Evan Cheng0598b2d2007-06-18 22:44:57 +00001598 }
1599
Mikael Holmen8b106802017-08-11 06:57:08 +00001600 // Keep the CFG updated.
1601 BBI.BB->removeSuccessor(&CvtMBB, true);
1602
Evan Cheng312b7232007-06-04 06:47:22 +00001603 // If 'true' block has a 'false' successor, add an exit branch to it.
Evan Cheng6e4babe2007-06-05 01:31:40 +00001604 if (HasEarlyExit) {
Owen Anderson4f6bf042008-08-14 22:49:33 +00001605 SmallVector<MachineOperand, 4> RevCond(CvtBBI->BrCond.begin(),
1606 CvtBBI->BrCond.end());
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +00001607 if (TII->reverseBranchCondition(RevCond))
Craig Topperee4dab52012-02-05 08:31:47 +00001608 llvm_unreachable("Unable to reverse branch condition!");
Hans Wennborg1dbaf672015-12-01 03:49:42 +00001609
Cong Houd97c1002015-12-01 05:29:22 +00001610 // Update the edge probability for both CvtBBI->FalseBB and NextBBI.
Matthias Braun08f47042016-08-17 02:52:01 +00001611 // NewNext = New_Prob(BBI.BB, NextMBB) =
1612 // Prob(BBI.BB, NextMBB) +
1613 // Prob(BBI.BB, CvtMBB) * Prob(CvtMBB, NextMBB)
Cong Houd97c1002015-12-01 05:29:22 +00001614 // NewFalse = New_Prob(BBI.BB, CvtBBI->FalseBB) =
Matthias Braun08f47042016-08-17 02:52:01 +00001615 // Prob(BBI.BB, CvtMBB) * Prob(CvtMBB, CvtBBI->FalseBB)
1616 auto NewTrueBB = getNextBlock(*BBI.BB);
Cong Houd97c1002015-12-01 05:29:22 +00001617 auto NewNext = BBNext + BBCvt * CvtNext;
David Majnemer42531262016-08-12 03:55:06 +00001618 auto NewTrueBBIter = find(BBI.BB->successors(), NewTrueBB);
Cong Houcb07d702015-12-01 21:50:20 +00001619 if (NewTrueBBIter != BBI.BB->succ_end())
1620 BBI.BB->setSuccProbability(NewTrueBBIter, NewNext);
Cong Houd97c1002015-12-01 05:29:22 +00001621
1622 auto NewFalse = BBCvt * CvtFalse;
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +00001623 TII->insertBranch(*BBI.BB, CvtBBI->FalseBB, nullptr, RevCond, dl);
Cong Houd97c1002015-12-01 05:29:22 +00001624 BBI.BB->addSuccessor(CvtBBI->FalseBB, NewFalse);
Evan Cheng92fb5452007-06-15 07:36:12 +00001625 }
Evan Cheng7783f822007-06-08 09:36:04 +00001626
1627 // Merge in the 'false' block if the 'false' block has no other
Bob Wilson2371f4f2009-05-13 23:25:24 +00001628 // predecessors. Otherwise, add an unconditional branch to 'false'.
Evan Cheng17aad8162007-06-05 00:07:37 +00001629 bool FalseBBDead = false;
Evan Chenge4ec9182007-06-06 02:08:52 +00001630 bool IterIfcvt = true;
Matthias Braun08f47042016-08-17 02:52:01 +00001631 bool isFallThrough = canFallThroughTo(*BBI.BB, NextMBB);
Evan Chengd3f3f0a2007-06-07 08:13:00 +00001632 if (!isFallThrough) {
1633 // Only merge them if the true block does not fallthrough to the false
1634 // block. By not merging them, we make it possible to iteratively
1635 // ifcvt the blocks.
Evan Cheng51eb2c32007-06-18 08:37:25 +00001636 if (!HasEarlyExit &&
Tobias Grosser8cf785f2017-05-29 06:12:18 +00001637 NextMBB.pred_size() == 1 && !NextBBI->HasFallThrough &&
Matthias Braun08f47042016-08-17 02:52:01 +00001638 !NextMBB.hasAddressTaken()) {
Evan Cheng2117d1f2007-06-09 01:03:43 +00001639 MergeBlocks(BBI, *NextBBI);
Evan Chengd3f3f0a2007-06-07 08:13:00 +00001640 FalseBBDead = true;
Evan Chengd3f3f0a2007-06-07 08:13:00 +00001641 } else {
Matthias Braun08f47042016-08-17 02:52:01 +00001642 InsertUncondBranch(*BBI.BB, NextMBB, TII);
Evan Cheng2117d1f2007-06-09 01:03:43 +00001643 BBI.HasFallThrough = false;
Evan Chengd3f3f0a2007-06-07 08:13:00 +00001644 }
Evan Cheng7783f822007-06-08 09:36:04 +00001645 // Mixed predicated and unpredicated code. This cannot be iteratively
1646 // predicated.
1647 IterIfcvt = false;
Evan Chenge4ec9182007-06-06 02:08:52 +00001648 }
Evan Chenge26c0912007-05-21 22:22:58 +00001649
Evan Chengd0e66912007-05-23 07:23:16 +00001650 // Update block info. BB can be iteratively if-converted.
Bob Wilson81051442010-06-15 22:18:54 +00001651 if (!IterIfcvt)
Evan Cheng4dd31a72007-06-11 22:26:22 +00001652 BBI.IsDone = true;
Matthias Braun08f47042016-08-17 02:52:01 +00001653 InvalidatePreds(*BBI.BB);
Evan Cheng4dd31a72007-06-11 22:26:22 +00001654 CvtBBI->IsDone = true;
Evan Cheng17aad8162007-06-05 00:07:37 +00001655 if (FalseBBDead)
Evan Cheng4dd31a72007-06-11 22:26:22 +00001656 NextBBI->IsDone = true;
Evan Chenge26c0912007-05-21 22:22:58 +00001657
1658 // FIXME: Must maintain LiveIns.
Evan Chenge26c0912007-05-21 22:22:58 +00001659 return true;
Evan Chengf5e53a52007-05-16 02:00:57 +00001660}
1661
Kyle Butta8c73712016-08-24 21:34:27 +00001662/// Common code shared between diamond conversions.
1663/// \p BBI, \p TrueBBI, and \p FalseBBI form the diamond shape.
1664/// \p NumDups1 - number of shared instructions at the beginning of \p TrueBBI
1665/// and FalseBBI
1666/// \p NumDups2 - number of shared instructions at the end of \p TrueBBI
1667/// and \p FalseBBI
Kyle Butt092c4dd2016-08-29 18:27:12 +00001668/// \p RemoveBranch - Remove the common branch of the two blocks before
1669/// predicating. Only false for unanalyzable fallthrough
1670/// cases. The caller will replace the branch if necessary.
Kyle Butta8c73712016-08-24 21:34:27 +00001671/// \p MergeAddEdges - Add successor edges when merging blocks. Only false for
1672/// unanalyzable fallthrough
1673bool IfConverter::IfConvertDiamondCommon(
1674 BBInfo &BBI, BBInfo &TrueBBI, BBInfo &FalseBBI,
1675 unsigned NumDups1, unsigned NumDups2,
1676 bool TClobbersPred, bool FClobbersPred,
Kyle Butt092c4dd2016-08-29 18:27:12 +00001677 bool RemoveBranch, bool MergeAddEdges) {
Evan Chenge26c0912007-05-21 22:22:58 +00001678
Evan Cheng51eb2c32007-06-18 08:37:25 +00001679 if (TrueBBI.IsDone || FalseBBI.IsDone ||
Kyle Butta8c73712016-08-24 21:34:27 +00001680 TrueBBI.BB->pred_size() > 1 || FalseBBI.BB->pred_size() > 1) {
Evan Cheng51eb2c32007-06-18 08:37:25 +00001681 // Something has changed. It's no longer safe to predicate these blocks.
1682 BBI.IsAnalyzed = false;
1683 TrueBBI.IsAnalyzed = false;
1684 FalseBBI.IsAnalyzed = false;
1685 return false;
Evan Chenge26c0912007-05-21 22:22:58 +00001686 }
Evan Cheng51eb2c32007-06-18 08:37:25 +00001687
Evan Cheng8b8e8d82013-05-05 18:03:49 +00001688 if (TrueBBI.BB->hasAddressTaken() || FalseBBI.BB->hasAddressTaken())
1689 // Conservatively abort if-conversion if either BB has its address taken.
1690 return false;
1691
Bob Wilson1e5da552010-06-29 00:55:23 +00001692 // Put the predicated instructions from the 'true' block before the
1693 // instructions from the 'false' block, unless the true block would clobber
1694 // the predicate, in which case, do the opposite.
Evan Cheng79484222007-06-05 23:46:14 +00001695 BBInfo *BBI1 = &TrueBBI;
1696 BBInfo *BBI2 = &FalseBBI;
Owen Anderson4f6bf042008-08-14 22:49:33 +00001697 SmallVector<MachineOperand, 4> RevCond(BBI.BrCond.begin(), BBI.BrCond.end());
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +00001698 if (TII->reverseBranchCondition(RevCond))
Craig Topperee4dab52012-02-05 08:31:47 +00001699 llvm_unreachable("Unable to reverse branch condition!");
Owen Anderson4f6bf042008-08-14 22:49:33 +00001700 SmallVector<MachineOperand, 4> *Cond1 = &BBI.BrCond;
1701 SmallVector<MachineOperand, 4> *Cond2 = &RevCond;
Evan Cheng30565992007-06-06 00:57:55 +00001702
Evan Cheng3a51c852007-06-16 09:34:52 +00001703 // Figure out the more profitable ordering.
1704 bool DoSwap = false;
Kyle Butt6262ca32016-08-24 21:34:24 +00001705 if (TClobbersPred && !FClobbersPred)
Evan Cheng3a51c852007-06-16 09:34:52 +00001706 DoSwap = true;
Kyle Butte31cc842016-09-02 18:29:28 +00001707 else if (!TClobbersPred && !FClobbersPred) {
Evan Cheng0598b2d2007-06-18 22:44:57 +00001708 if (TrueBBI.NonPredSize > FalseBBI.NonPredSize)
Evan Cheng3a51c852007-06-16 09:34:52 +00001709 DoSwap = true;
Kyle Butte31cc842016-09-02 18:29:28 +00001710 } else if (TClobbersPred && FClobbersPred)
1711 llvm_unreachable("Predicate info cannot be clobbered by both sides.");
Evan Cheng3a51c852007-06-16 09:34:52 +00001712 if (DoSwap) {
Evan Cheng30565992007-06-06 00:57:55 +00001713 std::swap(BBI1, BBI2);
1714 std::swap(Cond1, Cond2);
Evan Cheng30565992007-06-06 00:57:55 +00001715 }
Evan Cheng79484222007-06-05 23:46:14 +00001716
Evan Cheng51eb2c32007-06-18 08:37:25 +00001717 // Remove the conditional branch from entry to the blocks.
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +00001718 BBI.NonPredSize -= TII->removeBranch(*BBI.BB);
Evan Cheng51eb2c32007-06-18 08:37:25 +00001719
Matthias Braun08f47042016-08-17 02:52:01 +00001720 MachineBasicBlock &MBB1 = *BBI1->BB;
1721 MachineBasicBlock &MBB2 = *BBI2->BB;
1722
Krzysztof Parzyszeka003b762016-08-11 18:42:06 +00001723 // Initialize the Redefs:
1724 // - BB2 live-in regs need implicit uses before being redefined by BB1
1725 // instructions.
1726 // - BB1 live-out regs need implicit uses before being redefined by BB2
1727 // instructions. We start with BB1 live-ins so we have the live-out regs
1728 // after tracking the BB1 instructions.
Matthias Braun0c989a82016-12-08 00:15:51 +00001729 Redefs.init(*TRI);
Matthias Braun11723322017-01-05 20:01:19 +00001730 if (MRI->tracksLiveness()) {
1731 Redefs.addLiveIns(MBB1);
1732 Redefs.addLiveIns(MBB2);
1733 }
Evan Chengf128bdc2010-06-16 07:35:02 +00001734
Evan Cheng51eb2c32007-06-18 08:37:25 +00001735 // Remove the duplicated instructions at the beginnings of both paths.
Jim Grosbach412800d2010-06-14 21:30:32 +00001736 // Skip dbg_value instructions
Matthias Braun08f47042016-08-17 02:52:01 +00001737 MachineBasicBlock::iterator DI1 = MBB1.getFirstNonDebugInstr();
1738 MachineBasicBlock::iterator DI2 = MBB2.getFirstNonDebugInstr();
Evan Cheng51eb2c32007-06-18 08:37:25 +00001739 BBI1->NonPredSize -= NumDups1;
1740 BBI2->NonPredSize -= NumDups1;
Jim Grosbachee6e29a2010-06-28 20:26:00 +00001741
1742 // Skip past the dups on each side separately since there may be
1743 // differing dbg_value entries.
1744 for (unsigned i = 0; i < NumDups1; ++DI1) {
1745 if (!DI1->isDebugValue())
1746 ++i;
1747 }
Jim Grosbachc34befc2010-06-25 23:05:46 +00001748 while (NumDups1 != 0) {
Evan Cheng51eb2c32007-06-18 08:37:25 +00001749 ++DI2;
Jim Grosbachee6e29a2010-06-28 20:26:00 +00001750 if (!DI2->isDebugValue())
1751 --NumDups1;
Evan Cheng51eb2c32007-06-18 08:37:25 +00001752 }
Evan Chengf128bdc2010-06-16 07:35:02 +00001753
Matthias Braund616ccc2013-10-11 19:04:37 +00001754 // Compute a set of registers which must not be killed by instructions in BB1:
1755 // This is everything used+live in BB2 after the duplicated instructions. We
1756 // can compute this set by simulating liveness backwards from the end of BB2.
Matthias Braun0c989a82016-12-08 00:15:51 +00001757 DontKill.init(*TRI);
Matthias Braun11723322017-01-05 20:01:19 +00001758 if (MRI->tracksLiveness()) {
1759 for (const MachineInstr &MI : make_range(MBB2.rbegin(), ++DI2.getReverse()))
1760 DontKill.stepBackward(MI);
Matthias Braund616ccc2013-10-11 19:04:37 +00001761
Matthias Braun11723322017-01-05 20:01:19 +00001762 for (const MachineInstr &MI : make_range(MBB1.begin(), DI1)) {
1763 SmallVector<std::pair<unsigned, const MachineOperand*>, 4> Dummy;
1764 Redefs.stepForward(MI, Dummy);
1765 }
Matthias Braund616ccc2013-10-11 19:04:37 +00001766 }
Krzysztof Parzyszeka3017aa2017-09-06 17:57:13 +00001767 // Kill flags in the true block for registers living into the false block
1768 // must be removed. This should be done before extracting the common
1769 // instructions from the beginning of the MBB1, since these instructions
1770 // can actually differ between MBB1 and MBB2 in terms of <kill> flags.
1771 RemoveKills(MBB1.begin(), MBB1.end(), DontKill, *TRI);
1772
Matthias Braun08f47042016-08-17 02:52:01 +00001773 BBI.BB->splice(BBI.BB->end(), &MBB1, MBB1.begin(), DI1);
1774 MBB2.erase(MBB2.begin(), DI2);
Evan Cheng51eb2c32007-06-18 08:37:25 +00001775
Kyle Butt092c4dd2016-08-29 18:27:12 +00001776 // The branches have been checked to match, so it is safe to remove the branch
1777 // in BB1 and rely on the copy in BB2
1778#ifndef NDEBUG
1779 // Unanalyzable branches must match exactly. Check that now.
1780 if (!BBI1->IsBrAnalyzable)
1781 verifySameBranchInstructions(&MBB1, &MBB2);
1782#endif
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +00001783 BBI1->NonPredSize -= TII->removeBranch(*BBI1->BB);
Krzysztof Parzyszek2451c482016-01-20 13:14:52 +00001784 // Remove duplicated instructions.
Matthias Braun08f47042016-08-17 02:52:01 +00001785 DI1 = MBB1.end();
Jim Grosbach412800d2010-06-14 21:30:32 +00001786 for (unsigned i = 0; i != NumDups2; ) {
1787 // NumDups2 only counted non-dbg_value instructions, so this won't
1788 // run off the head of the list.
Matthias Braun08f47042016-08-17 02:52:01 +00001789 assert(DI1 != MBB1.begin());
Evan Cheng51eb2c32007-06-18 08:37:25 +00001790 --DI1;
Jim Grosbach412800d2010-06-14 21:30:32 +00001791 // skip dbg_value instructions
1792 if (!DI1->isDebugValue())
1793 ++i;
1794 }
Matthias Braun08f47042016-08-17 02:52:01 +00001795 MBB1.erase(DI1, MBB1.end());
Evan Cheng79484222007-06-05 23:46:14 +00001796
Kyle Butta8c73712016-08-24 21:34:27 +00001797 DI2 = BBI2->BB->end();
Kyle Butt092c4dd2016-08-29 18:27:12 +00001798 // The branches have been checked to match. Skip over the branch in the false
1799 // block so that we don't try to predicate it.
1800 if (RemoveBranch)
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +00001801 BBI2->NonPredSize -= TII->removeBranch(*BBI2->BB);
Kyle Butt092c4dd2016-08-29 18:27:12 +00001802 else {
1803 do {
1804 assert(DI2 != MBB2.begin());
1805 DI2--;
1806 } while (DI2->isBranch() || DI2->isDebugValue());
1807 DI2++;
1808 }
Evan Cheng51eb2c32007-06-18 08:37:25 +00001809 while (NumDups2 != 0) {
Jim Grosbach412800d2010-06-14 21:30:32 +00001810 // NumDups2 only counted non-dbg_value instructions, so this won't
1811 // run off the head of the list.
Matthias Braun08f47042016-08-17 02:52:01 +00001812 assert(DI2 != MBB2.begin());
Evan Cheng51eb2c32007-06-18 08:37:25 +00001813 --DI2;
Jim Grosbach412800d2010-06-14 21:30:32 +00001814 // skip dbg_value instructions
1815 if (!DI2->isDebugValue())
1816 --NumDups2;
Evan Cheng51eb2c32007-06-18 08:37:25 +00001817 }
Evan Cheng4266a792011-12-19 22:01:30 +00001818
1819 // Remember which registers would later be defined by the false block.
1820 // This allows us not to predicate instructions in the true block that would
1821 // later be re-defined. That is, rather than
1822 // subeq r0, r1, #1
1823 // addne r0, r1, #1
1824 // generate:
1825 // sub r0, r1, #1
1826 // addne r0, r1, #1
1827 SmallSet<unsigned, 4> RedefsByFalse;
1828 SmallSet<unsigned, 4> ExtUses;
Matthias Braun08f47042016-08-17 02:52:01 +00001829 if (TII->isProfitableToUnpredicate(MBB1, MBB2)) {
1830 for (const MachineInstr &FI : make_range(MBB2.begin(), DI2)) {
Matthias Braunb1e05582016-08-17 02:51:59 +00001831 if (FI.isDebugValue())
Evan Cheng4266a792011-12-19 22:01:30 +00001832 continue;
1833 SmallVector<unsigned, 4> Defs;
Matthias Braunb1e05582016-08-17 02:51:59 +00001834 for (const MachineOperand &MO : FI.operands()) {
Evan Cheng4266a792011-12-19 22:01:30 +00001835 if (!MO.isReg())
1836 continue;
1837 unsigned Reg = MO.getReg();
1838 if (!Reg)
1839 continue;
1840 if (MO.isDef()) {
1841 Defs.push_back(Reg);
1842 } else if (!RedefsByFalse.count(Reg)) {
1843 // These are defined before ctrl flow reach the 'false' instructions.
1844 // They cannot be modified by the 'true' instructions.
Chad Rosierabdb1d62013-05-22 23:17:36 +00001845 for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
1846 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +00001847 ExtUses.insert(*SubRegs);
Evan Cheng4266a792011-12-19 22:01:30 +00001848 }
1849 }
1850
Matthias Braunb1e05582016-08-17 02:51:59 +00001851 for (unsigned Reg : Defs) {
Evan Cheng4266a792011-12-19 22:01:30 +00001852 if (!ExtUses.count(Reg)) {
Chad Rosierabdb1d62013-05-22 23:17:36 +00001853 for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
1854 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +00001855 RedefsByFalse.insert(*SubRegs);
Evan Cheng4266a792011-12-19 22:01:30 +00001856 }
1857 }
1858 }
1859 }
1860
1861 // Predicate the 'true' block.
Matthias Braun08f47042016-08-17 02:52:01 +00001862 PredicateBlock(*BBI1, MBB1.end(), *Cond1, &RedefsByFalse);
Evan Cheng4266a792011-12-19 22:01:30 +00001863
Krzysztof Parzyszek2451c482016-01-20 13:14:52 +00001864 // After predicating BBI1, if there is a predicated terminator in BBI1 and
1865 // a non-predicated in BBI2, then we don't want to predicate the one from
1866 // BBI2. The reason is that if we merged these blocks, we would end up with
1867 // two predicated terminators in the same block.
Matthias Braun08f47042016-08-17 02:52:01 +00001868 if (!MBB2.empty() && (DI2 == MBB2.end())) {
1869 MachineBasicBlock::iterator BBI1T = MBB1.getFirstTerminator();
1870 MachineBasicBlock::iterator BBI2T = MBB2.getFirstTerminator();
1871 if (BBI1T != MBB1.end() && TII->isPredicated(*BBI1T) &&
1872 BBI2T != MBB2.end() && !TII->isPredicated(*BBI2T))
Krzysztof Parzyszek2451c482016-01-20 13:14:52 +00001873 --DI2;
1874 }
1875
Evan Cheng4266a792011-12-19 22:01:30 +00001876 // Predicate the 'false' block.
Andrew Trick276dd452013-10-14 20:45:17 +00001877 PredicateBlock(*BBI2, DI2, *Cond2);
Evan Cheng79484222007-06-05 23:46:14 +00001878
Evan Cheng0598b2d2007-06-18 22:44:57 +00001879 // Merge the true block into the entry of the diamond.
Kyle Butta8c73712016-08-24 21:34:27 +00001880 MergeBlocks(BBI, *BBI1, MergeAddEdges);
1881 MergeBlocks(BBI, *BBI2, MergeAddEdges);
1882 return true;
1883}
1884
1885/// If convert an almost-diamond sub-CFG where the true
1886/// and false blocks share a common tail.
1887bool IfConverter::IfConvertForkedDiamond(
1888 BBInfo &BBI, IfcvtKind Kind,
1889 unsigned NumDups1, unsigned NumDups2,
1890 bool TClobbersPred, bool FClobbersPred) {
1891 BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
1892 BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
1893
1894 // Save the debug location for later.
1895 DebugLoc dl;
1896 MachineBasicBlock::iterator TIE = TrueBBI.BB->getFirstTerminator();
1897 if (TIE != TrueBBI.BB->end())
1898 dl = TIE->getDebugLoc();
1899 // Removing branches from both blocks is safe, because we have already
1900 // determined that both blocks have the same branch instructions. The branch
1901 // will be added back at the end, unpredicated.
1902 if (!IfConvertDiamondCommon(
1903 BBI, TrueBBI, FalseBBI,
1904 NumDups1, NumDups2,
1905 TClobbersPred, FClobbersPred,
Kyle Butt092c4dd2016-08-29 18:27:12 +00001906 /* RemoveBranch */ true, /* MergeAddEdges */ true))
Kyle Butta8c73712016-08-24 21:34:27 +00001907 return false;
1908
1909 // Add back the branch.
1910 // Debug location saved above when removing the branch from BBI2
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +00001911 TII->insertBranch(*BBI.BB, TrueBBI.TrueBB, TrueBBI.FalseBB,
Kyle Butta8c73712016-08-24 21:34:27 +00001912 TrueBBI.BrCond, dl);
1913
Kyle Butta8c73712016-08-24 21:34:27 +00001914 // Update block info.
1915 BBI.IsDone = TrueBBI.IsDone = FalseBBI.IsDone = true;
1916 InvalidatePreds(*BBI.BB);
1917
1918 // FIXME: Must maintain LiveIns.
1919 return true;
1920}
1921
1922/// If convert a diamond sub-CFG.
1923bool IfConverter::IfConvertDiamond(BBInfo &BBI, IfcvtKind Kind,
1924 unsigned NumDups1, unsigned NumDups2,
1925 bool TClobbersPred, bool FClobbersPred) {
1926 BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
1927 BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
1928 MachineBasicBlock *TailBB = TrueBBI.TrueBB;
1929
1930 // True block must fall through or end with an unanalyzable terminator.
1931 if (!TailBB) {
1932 if (blockAlwaysFallThrough(TrueBBI))
1933 TailBB = FalseBBI.TrueBB;
1934 assert((TailBB || !TrueBBI.IsBrAnalyzable) && "Unexpected!");
1935 }
1936
1937 if (!IfConvertDiamondCommon(
1938 BBI, TrueBBI, FalseBBI,
1939 NumDups1, NumDups2,
Kyle Butt86999212016-09-02 18:29:26 +00001940 TClobbersPred, FClobbersPred,
Kyle Butt092c4dd2016-08-29 18:27:12 +00001941 /* RemoveBranch */ TrueBBI.IsBrAnalyzable,
Kyle Butta8c73712016-08-24 21:34:27 +00001942 /* MergeAddEdges */ TailBB == nullptr))
1943 return false;
Evan Cheng30565992007-06-06 00:57:55 +00001944
Bob Wilson2371f4f2009-05-13 23:25:24 +00001945 // If the if-converted block falls through or unconditionally branches into
1946 // the tail block, and the tail block does not have other predecessors, then
Evan Cheng51eb2c32007-06-18 08:37:25 +00001947 // fold the tail block in as well. Otherwise, unless it falls through to the
1948 // tail, add a unconditional branch to it.
1949 if (TailBB) {
Mikael Holmen8b106802017-08-11 06:57:08 +00001950 // We need to remove the edges to the true and false blocks manually since
1951 // we didn't let IfConvertDiamondCommon update the CFG.
1952 BBI.BB->removeSuccessor(TrueBBI.BB);
1953 BBI.BB->removeSuccessor(FalseBBI.BB, true);
1954
Pete Cooper77c703f2011-11-04 23:49:14 +00001955 BBInfo &TailBBI = BBAnalysis[TailBB->getNumber()];
Evan Cheng8b8e8d82013-05-05 18:03:49 +00001956 bool CanMergeTail = !TailBBI.HasFallThrough &&
1957 !TailBBI.BB->hasAddressTaken();
Krzysztof Parzyszek2451c482016-01-20 13:14:52 +00001958 // The if-converted block can still have a predicated terminator
1959 // (e.g. a predicated return). If that is the case, we cannot merge
1960 // it with the tail block.
1961 MachineBasicBlock::const_iterator TI = BBI.BB->getFirstTerminator();
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001962 if (TI != BBI.BB->end() && TII->isPredicated(*TI))
Krzysztof Parzyszek2451c482016-01-20 13:14:52 +00001963 CanMergeTail = false;
Bob Wilson1e5da552010-06-29 00:55:23 +00001964 // There may still be a fall-through edge from BBI1 or BBI2 to TailBB;
1965 // check if there are any other predecessors besides those.
1966 unsigned NumPreds = TailBB->pred_size();
1967 if (NumPreds > 1)
1968 CanMergeTail = false;
1969 else if (NumPreds == 1 && CanMergeTail) {
1970 MachineBasicBlock::pred_iterator PI = TailBB->pred_begin();
Kyle Butta8c73712016-08-24 21:34:27 +00001971 if (*PI != TrueBBI.BB && *PI != FalseBBI.BB)
Bob Wilson1e5da552010-06-29 00:55:23 +00001972 CanMergeTail = false;
1973 }
1974 if (CanMergeTail) {
Evan Cheng0598b2d2007-06-18 22:44:57 +00001975 MergeBlocks(BBI, TailBBI);
Evan Cheng51eb2c32007-06-18 08:37:25 +00001976 TailBBI.IsDone = true;
1977 } else {
Cong Houd97c1002015-12-01 05:29:22 +00001978 BBI.BB->addSuccessor(TailBB, BranchProbability::getOne());
Matthias Braun08f47042016-08-17 02:52:01 +00001979 InsertUncondBranch(*BBI.BB, *TailBB, TII);
Evan Cheng0598b2d2007-06-18 22:44:57 +00001980 BBI.HasFallThrough = false;
Evan Cheng51eb2c32007-06-18 08:37:25 +00001981 }
Evan Chenge26c0912007-05-21 22:22:58 +00001982 }
1983
Evan Cheng79484222007-06-05 23:46:14 +00001984 // Update block info.
Evan Cheng4dd31a72007-06-11 22:26:22 +00001985 BBI.IsDone = TrueBBI.IsDone = FalseBBI.IsDone = true;
Matthias Braun08f47042016-08-17 02:52:01 +00001986 InvalidatePreds(*BBI.BB);
Evan Chenge26c0912007-05-21 22:22:58 +00001987
1988 // FIXME: Must maintain LiveIns.
Evan Chenge26c0912007-05-21 22:22:58 +00001989 return true;
Evan Chengf5e53a52007-05-16 02:00:57 +00001990}
1991
Duncan P. N. Exon Smith0490cde2016-06-30 23:04:51 +00001992static bool MaySpeculate(const MachineInstr &MI,
Matthias Braun07066cc2015-05-19 21:22:20 +00001993 SmallSet<unsigned, 4> &LaterRedefs) {
Evan Cheng4266a792011-12-19 22:01:30 +00001994 bool SawStore = true;
Duncan P. N. Exon Smith0490cde2016-06-30 23:04:51 +00001995 if (!MI.isSafeToMove(nullptr, SawStore))
Evan Cheng4266a792011-12-19 22:01:30 +00001996 return false;
1997
Matthias Braunb1e05582016-08-17 02:51:59 +00001998 for (const MachineOperand &MO : MI.operands()) {
Evan Cheng4266a792011-12-19 22:01:30 +00001999 if (!MO.isReg())
2000 continue;
2001 unsigned Reg = MO.getReg();
2002 if (!Reg)
2003 continue;
2004 if (MO.isDef() && !LaterRedefs.count(Reg))
2005 return false;
2006 }
2007
2008 return true;
2009}
2010
Matthias Braun2c931792016-08-17 02:51:57 +00002011/// Predicate instructions from the start of the block to the specified end with
2012/// the specified condition.
Evan Chengd0e66912007-05-23 07:23:16 +00002013void IfConverter::PredicateBlock(BBInfo &BBI,
Evan Cheng51eb2c32007-06-18 08:37:25 +00002014 MachineBasicBlock::iterator E,
Evan Chengf128bdc2010-06-16 07:35:02 +00002015 SmallVectorImpl<MachineOperand> &Cond,
Evan Cheng4266a792011-12-19 22:01:30 +00002016 SmallSet<unsigned, 4> *LaterRedefs) {
2017 bool AnyUnpred = false;
Craig Topperc0196b12014-04-14 00:51:57 +00002018 bool MaySpec = LaterRedefs != nullptr;
Matthias Braunb1e05582016-08-17 02:51:59 +00002019 for (MachineInstr &I : make_range(BBI.BB->begin(), E)) {
Duncan P. N. Exon Smith0490cde2016-06-30 23:04:51 +00002020 if (I.isDebugValue() || TII->isPredicated(I))
Evan Chengd0e66912007-05-23 07:23:16 +00002021 continue;
Evan Cheng4266a792011-12-19 22:01:30 +00002022 // It may be possible not to predicate an instruction if it's the 'true'
2023 // side of a diamond and the 'false' side may re-define the instruction's
2024 // defs.
Matthias Braun07066cc2015-05-19 21:22:20 +00002025 if (MaySpec && MaySpeculate(I, *LaterRedefs)) {
Evan Cheng4266a792011-12-19 22:01:30 +00002026 AnyUnpred = true;
2027 continue;
2028 }
2029 // If any instruction is predicated, then every instruction after it must
2030 // be predicated.
2031 MaySpec = false;
Duncan P. N. Exon Smith0490cde2016-06-30 23:04:51 +00002032 if (!TII->PredicateInstruction(I, Cond)) {
Torok Edwin08954aa2009-07-12 20:07:01 +00002033#ifndef NDEBUG
Duncan P. N. Exon Smith0490cde2016-06-30 23:04:51 +00002034 dbgs() << "Unable to predicate " << I << "!\n";
Torok Edwin08954aa2009-07-12 20:07:01 +00002035#endif
Craig Topperc0196b12014-04-14 00:51:57 +00002036 llvm_unreachable(nullptr);
Evan Chengaf716102007-05-16 21:54:37 +00002037 }
Evan Chengf128bdc2010-06-16 07:35:02 +00002038
Bob Wilson45814342010-06-19 05:33:57 +00002039 // If the predicated instruction now redefines a register as the result of
Evan Chengf128bdc2010-06-16 07:35:02 +00002040 // if-conversion, add an implicit kill.
Duncan P. N. Exon Smith0490cde2016-06-30 23:04:51 +00002041 UpdatePredRedefs(I, Redefs);
Evan Chengf5e53a52007-05-16 02:00:57 +00002042 }
Evan Chengd0e66912007-05-23 07:23:16 +00002043
Benjamin Kramer4f6ac162015-02-28 10:11:12 +00002044 BBI.Predicate.append(Cond.begin(), Cond.end());
Evan Chengdf1a4292007-06-08 19:17:12 +00002045
Evan Cheng92fb5452007-06-15 07:36:12 +00002046 BBI.IsAnalyzed = false;
2047 BBI.NonPredSize = 0;
2048
Dan Gohmand2d1ae12010-06-22 15:08:57 +00002049 ++NumIfConvBBs;
Evan Cheng4266a792011-12-19 22:01:30 +00002050 if (AnyUnpred)
2051 ++NumUnpred;
Evan Cheng312b7232007-06-04 06:47:22 +00002052}
2053
Matthias Braun2c931792016-08-17 02:51:57 +00002054/// Copy and predicate instructions from source BB to the destination block.
2055/// Skip end of block branches if IgnoreBr is true.
Evan Cheng92fb5452007-06-15 07:36:12 +00002056void IfConverter::CopyAndPredicateBlock(BBInfo &ToBBI, BBInfo &FromBBI,
Owen Anderson4f6bf042008-08-14 22:49:33 +00002057 SmallVectorImpl<MachineOperand> &Cond,
Evan Cheng92fb5452007-06-15 07:36:12 +00002058 bool IgnoreBr) {
Dan Gohman3b460302008-07-07 23:14:23 +00002059 MachineFunction &MF = *ToBBI.BB->getParent();
2060
Matthias Braun08f47042016-08-17 02:52:01 +00002061 MachineBasicBlock &FromMBB = *FromBBI.BB;
2062 for (MachineInstr &I : FromMBB) {
Evan Cheng92fb5452007-06-15 07:36:12 +00002063 // Do not copy the end of the block branches.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002064 if (IgnoreBr && I.isBranch())
Evan Cheng92fb5452007-06-15 07:36:12 +00002065 break;
2066
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002067 MachineInstr *MI = MF.CloneMachineInstr(&I);
Evan Cheng92fb5452007-06-15 07:36:12 +00002068 ToBBI.BB->insert(ToBBI.BB->end(), MI);
Bob Wilsonefd360c2010-10-26 00:02:21 +00002069 ToBBI.NonPredSize++;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002070 unsigned ExtraPredCost = TII->getPredicationCost(I);
2071 unsigned NumCycles = SchedModel.computeInstrLatency(&I, false);
Evan Chengdebf9c52010-11-03 00:45:17 +00002072 if (NumCycles > 1)
2073 ToBBI.ExtraCost += NumCycles-1;
2074 ToBBI.ExtraCost2 += ExtraPredCost;
Evan Cheng92fb5452007-06-15 07:36:12 +00002075
Bob Wilsonf82c8fc2010-06-18 17:07:23 +00002076 if (!TII->isPredicated(I) && !MI->isDebugValue()) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002077 if (!TII->PredicateInstruction(*MI, Cond)) {
Torok Edwin08954aa2009-07-12 20:07:01 +00002078#ifndef NDEBUG
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002079 dbgs() << "Unable to predicate " << I << "!\n";
Torok Edwin08954aa2009-07-12 20:07:01 +00002080#endif
Craig Topperc0196b12014-04-14 00:51:57 +00002081 llvm_unreachable(nullptr);
Evan Cheng92fb5452007-06-15 07:36:12 +00002082 }
Evan Chengf128bdc2010-06-16 07:35:02 +00002083 }
2084
Bob Wilson45814342010-06-19 05:33:57 +00002085 // If the predicated instruction now redefines a register as the result of
Evan Chengf128bdc2010-06-16 07:35:02 +00002086 // if-conversion, add an implicit kill.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002087 UpdatePredRedefs(*MI, Redefs);
Matthias Braund616ccc2013-10-11 19:04:37 +00002088
2089 // Some kill flags may not be correct anymore.
Andrew Trick276dd452013-10-14 20:45:17 +00002090 if (!DontKill.empty())
Juergen Ributzka310034e2013-12-14 06:52:56 +00002091 RemoveKills(*MI, DontKill);
Evan Cheng92fb5452007-06-15 07:36:12 +00002092 }
2093
Bob Wilson1e5da552010-06-29 00:55:23 +00002094 if (!IgnoreBr) {
Matthias Braun08f47042016-08-17 02:52:01 +00002095 std::vector<MachineBasicBlock *> Succs(FromMBB.succ_begin(),
2096 FromMBB.succ_end());
2097 MachineBasicBlock *NBB = getNextBlock(FromMBB);
Craig Topperc0196b12014-04-14 00:51:57 +00002098 MachineBasicBlock *FallThrough = FromBBI.HasFallThrough ? NBB : nullptr;
Evan Cheng0598b2d2007-06-18 22:44:57 +00002099
Matthias Braunb1e05582016-08-17 02:51:59 +00002100 for (MachineBasicBlock *Succ : Succs) {
Bob Wilson1e5da552010-06-29 00:55:23 +00002101 // Fallthrough edge can't be transferred.
2102 if (Succ == FallThrough)
2103 continue;
2104 ToBBI.BB->addSuccessor(Succ);
2105 }
Evan Cheng0598b2d2007-06-18 22:44:57 +00002106 }
2107
Benjamin Kramer4f6ac162015-02-28 10:11:12 +00002108 ToBBI.Predicate.append(FromBBI.Predicate.begin(), FromBBI.Predicate.end());
2109 ToBBI.Predicate.append(Cond.begin(), Cond.end());
Evan Cheng92fb5452007-06-15 07:36:12 +00002110
2111 ToBBI.ClobbersPred |= FromBBI.ClobbersPred;
2112 ToBBI.IsAnalyzed = false;
2113
Dan Gohmand2d1ae12010-06-22 15:08:57 +00002114 ++NumDupBBs;
Evan Cheng92fb5452007-06-15 07:36:12 +00002115}
2116
Matthias Braun2c931792016-08-17 02:51:57 +00002117/// Move all instructions from FromBB to the end of ToBB. This will leave
2118/// FromBB as an empty block, so remove all of its successor edges except for
2119/// the fall-through edge. If AddEdges is true, i.e., when FromBBI's branch is
Mikael Holmen8b106802017-08-11 06:57:08 +00002120/// being moved, add those successor edges to ToBBI and remove the old edge
2121/// from ToBBI to FromBBI.
Bob Wilson1e5da552010-06-29 00:55:23 +00002122void IfConverter::MergeBlocks(BBInfo &ToBBI, BBInfo &FromBBI, bool AddEdges) {
Matthias Braun08f47042016-08-17 02:52:01 +00002123 MachineBasicBlock &FromMBB = *FromBBI.BB;
2124 assert(!FromMBB.hasAddressTaken() &&
Evan Cheng8b8e8d82013-05-05 18:03:49 +00002125 "Removing a BB whose address is taken!");
2126
Matthias Braun08f47042016-08-17 02:52:01 +00002127 // In case FromMBB contains terminators (e.g. return instruction),
Krzysztof Parzyszek2451c482016-01-20 13:14:52 +00002128 // first move the non-terminator instructions, then the terminators.
Matthias Braun08f47042016-08-17 02:52:01 +00002129 MachineBasicBlock::iterator FromTI = FromMBB.getFirstTerminator();
Krzysztof Parzyszek2451c482016-01-20 13:14:52 +00002130 MachineBasicBlock::iterator ToTI = ToBBI.BB->getFirstTerminator();
Matthias Braun08f47042016-08-17 02:52:01 +00002131 ToBBI.BB->splice(ToTI, &FromMBB, FromMBB.begin(), FromTI);
Krzysztof Parzyszek2451c482016-01-20 13:14:52 +00002132
2133 // If FromBB has non-predicated terminator we should copy it at the end.
Matthias Braun08f47042016-08-17 02:52:01 +00002134 if (FromTI != FromMBB.end() && !TII->isPredicated(*FromTI))
Krzysztof Parzyszek2451c482016-01-20 13:14:52 +00002135 ToTI = ToBBI.BB->end();
Matthias Braun08f47042016-08-17 02:52:01 +00002136 ToBBI.BB->splice(ToTI, &FromMBB, FromTI, FromMBB.end());
Evan Chengd0e66912007-05-23 07:23:16 +00002137
Cong Houb9e8d482015-12-17 01:29:08 +00002138 // Force normalizing the successors' probabilities of ToBBI.BB to convert all
2139 // unknown probabilities into known ones.
2140 // FIXME: This usage is too tricky and in the future we would like to
2141 // eliminate all unknown probabilities in MBB.
Krzysztof Parzyszek5b8fae52017-03-06 19:12:42 +00002142 if (ToBBI.IsBrAnalyzable)
2143 ToBBI.BB->normalizeSuccProbs();
Cong Houb9e8d482015-12-17 01:29:08 +00002144
Matthias Braun08f47042016-08-17 02:52:01 +00002145 SmallVector<MachineBasicBlock *, 4> FromSuccs(FromMBB.succ_begin(),
2146 FromMBB.succ_end());
2147 MachineBasicBlock *NBB = getNextBlock(FromMBB);
Craig Topperc0196b12014-04-14 00:51:57 +00002148 MachineBasicBlock *FallThrough = FromBBI.HasFallThrough ? NBB : nullptr;
Matthias Braun08f47042016-08-17 02:52:01 +00002149 // The edge probability from ToBBI.BB to FromMBB, which is only needed when
2150 // AddEdges is true and FromMBB is a successor of ToBBI.BB.
Cong Houd97c1002015-12-01 05:29:22 +00002151 auto To2FromProb = BranchProbability::getZero();
Matthias Braun08f47042016-08-17 02:52:01 +00002152 if (AddEdges && ToBBI.BB->isSuccessor(&FromMBB)) {
Mikael Holmen8b106802017-08-11 06:57:08 +00002153 // Remove the old edge but remember the edge probability so we can calculate
2154 // the correct weights on the new edges being added further down.
Matthias Braun08f47042016-08-17 02:52:01 +00002155 To2FromProb = MBPI->getEdgeProbability(ToBBI.BB, &FromMBB);
Mikael Holmen8b106802017-08-11 06:57:08 +00002156 ToBBI.BB->removeSuccessor(&FromMBB);
Cong Houd97c1002015-12-01 05:29:22 +00002157 }
2158
Matthias Braunb1e05582016-08-17 02:51:59 +00002159 for (MachineBasicBlock *Succ : FromSuccs) {
Evan Cheng288f1542007-06-08 22:01:07 +00002160 // Fallthrough edge can't be transferred.
Evan Chengbe9859e2007-06-07 02:12:15 +00002161 if (Succ == FallThrough)
2162 continue;
Cong Houd40105d2015-09-18 20:22:41 +00002163
Cong Houd97c1002015-12-01 05:29:22 +00002164 auto NewProb = BranchProbability::getZero();
Cong Houd40105d2015-09-18 20:22:41 +00002165 if (AddEdges) {
Cong Houd97c1002015-12-01 05:29:22 +00002166 // Calculate the edge probability for the edge from ToBBI.BB to Succ,
Matthias Braun08f47042016-08-17 02:52:01 +00002167 // which is a portion of the edge probability from FromMBB to Succ. The
2168 // portion ratio is the edge probability from ToBBI.BB to FromMBB (if
Cong Houd97c1002015-12-01 05:29:22 +00002169 // FromBBI is a successor of ToBBI.BB. See comment below for excepion).
Matthias Braun08f47042016-08-17 02:52:01 +00002170 NewProb = MBPI->getEdgeProbability(&FromMBB, Succ);
Cong Houd40105d2015-09-18 20:22:41 +00002171
Matthias Braun08f47042016-08-17 02:52:01 +00002172 // To2FromProb is 0 when FromMBB is not a successor of ToBBI.BB. This
2173 // only happens when if-converting a diamond CFG and FromMBB is the
2174 // tail BB. In this case FromMBB post-dominates ToBBI.BB and hence we
2175 // could just use the probabilities on FromMBB's out-edges when adding
Cong Houd97c1002015-12-01 05:29:22 +00002176 // new successors.
2177 if (!To2FromProb.isZero())
2178 NewProb *= To2FromProb;
Cong Houd40105d2015-09-18 20:22:41 +00002179 }
2180
Matthias Braun08f47042016-08-17 02:52:01 +00002181 FromMBB.removeSuccessor(Succ);
Cong Houd40105d2015-09-18 20:22:41 +00002182
2183 if (AddEdges) {
Cong Houd97c1002015-12-01 05:29:22 +00002184 // If the edge from ToBBI.BB to Succ already exists, update the
Cong Houc1069892015-12-13 09:26:17 +00002185 // probability of this edge by adding NewProb to it. An example is shown
Matthias Braun08f47042016-08-17 02:52:01 +00002186 // below, in which A is ToBBI.BB and B is FromMBB. In this case we
Cong Houd97c1002015-12-01 05:29:22 +00002187 // don't have to set C as A's successor as it already is. We only need to
2188 // update the edge probability on A->C. Note that B will not be
2189 // immediately removed from A's successors. It is possible that B->D is
2190 // not removed either if D is a fallthrough of B. Later the edge A->D
2191 // (generated here) and B->D will be combined into one edge. To maintain
2192 // correct edge probability of this combined edge, we need to set the edge
2193 // probability of A->B to zero, which is already done above. The edge
2194 // probability on A->D is calculated by scaling the original probability
2195 // on A->B by the probability of B->D.
Cong Houd40105d2015-09-18 20:22:41 +00002196 //
2197 // Before ifcvt: After ifcvt (assume B->D is kept):
2198 //
2199 // A A
2200 // /| /|\
2201 // / B / B|
2202 // | /| | ||
2203 // |/ | | |/
2204 // C D C D
2205 //
2206 if (ToBBI.BB->isSuccessor(Succ))
Cong Houd97c1002015-12-01 05:29:22 +00002207 ToBBI.BB->setSuccProbability(
David Majnemer42531262016-08-12 03:55:06 +00002208 find(ToBBI.BB->successors(), Succ),
Cong Houd97c1002015-12-01 05:29:22 +00002209 MBPI->getEdgeProbability(ToBBI.BB, Succ) + NewProb);
Cong Houd40105d2015-09-18 20:22:41 +00002210 else
Cong Houd97c1002015-12-01 05:29:22 +00002211 ToBBI.BB->addSuccessor(Succ, NewProb);
Cong Houd40105d2015-09-18 20:22:41 +00002212 }
Evan Chengbe9859e2007-06-07 02:12:15 +00002213 }
2214
Mikael Holmen8b106802017-08-11 06:57:08 +00002215 // Move the now empty FromMBB out of the way to the end of the function so
2216 // it doesn't interfere with fallthrough checks done by canFallThroughTo().
2217 MachineBasicBlock *Last = &*FromMBB.getParent()->rbegin();
2218 if (Last != &FromMBB)
2219 FromMBB.moveAfter(Last);
Evan Cheng288f1542007-06-08 22:01:07 +00002220
Cong Houc1069892015-12-13 09:26:17 +00002221 // Normalize the probabilities of ToBBI.BB's successors with all adjustment
2222 // we've done above.
Krzysztof Parzyszek5b8fae52017-03-06 19:12:42 +00002223 if (ToBBI.IsBrAnalyzable && FromBBI.IsBrAnalyzable)
2224 ToBBI.BB->normalizeSuccProbs();
Cong Houc1069892015-12-13 09:26:17 +00002225
Benjamin Kramer4f6ac162015-02-28 10:11:12 +00002226 ToBBI.Predicate.append(FromBBI.Predicate.begin(), FromBBI.Predicate.end());
Evan Cheng92fb5452007-06-15 07:36:12 +00002227 FromBBI.Predicate.clear();
2228
Evan Chengd0e66912007-05-23 07:23:16 +00002229 ToBBI.NonPredSize += FromBBI.NonPredSize;
Bob Wilsonefd360c2010-10-26 00:02:21 +00002230 ToBBI.ExtraCost += FromBBI.ExtraCost;
Evan Chengdebf9c52010-11-03 00:45:17 +00002231 ToBBI.ExtraCost2 += FromBBI.ExtraCost2;
Evan Chengd0e66912007-05-23 07:23:16 +00002232 FromBBI.NonPredSize = 0;
Bob Wilsonefd360c2010-10-26 00:02:21 +00002233 FromBBI.ExtraCost = 0;
Evan Chengdebf9c52010-11-03 00:45:17 +00002234 FromBBI.ExtraCost2 = 0;
Evan Cheng9030b982007-06-06 10:16:17 +00002235
Evan Cheng4dd31a72007-06-11 22:26:22 +00002236 ToBBI.ClobbersPred |= FromBBI.ClobbersPred;
Evan Cheng2117d1f2007-06-09 01:03:43 +00002237 ToBBI.HasFallThrough = FromBBI.HasFallThrough;
Evan Cheng1e6f08b2007-06-14 20:28:52 +00002238 ToBBI.IsAnalyzed = false;
2239 FromBBI.IsAnalyzed = false;
Evan Chengf5e53a52007-05-16 02:00:57 +00002240}
Akira Hatanaka4a616192015-06-08 18:50:43 +00002241
2242FunctionPass *
Matthias Braun8b38ffa2016-10-24 23:23:02 +00002243llvm::createIfConverter(std::function<bool(const MachineFunction &)> Ftor) {
Benjamin Kramerd3f4c052016-06-12 16:13:55 +00002244 return new IfConverter(std::move(Ftor));
Akira Hatanaka4a616192015-06-08 18:50:43 +00002245}