blob: 1cca1f83dff714105ae430f237a8e901646a34aa [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
Evan Chengf5e53a52007-05-16 02:00:57 +000015#include "llvm/CodeGen/Passes.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "BranchFolding.h"
17#include "llvm/ADT/STLExtras.h"
Kyle Buttd76755e2016-08-18 22:09:23 +000018#include "llvm/ADT/ScopeExit.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/ADT/SmallSet.h"
20#include "llvm/ADT/Statistic.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000021#include "llvm/CodeGen/LivePhysRegs.h"
Akira Hatanakabbd33f62014-08-07 19:30:13 +000022#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
Jakub Staszak3ef20e32011-08-03 22:53:41 +000023#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
Evan Chengf5e53a52007-05-16 02:00:57 +000024#include "llvm/CodeGen/MachineFunctionPass.h"
Jakob Stoklund Olesenf623e982012-12-20 18:08:06 +000025#include "llvm/CodeGen/MachineInstrBuilder.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000026#include "llvm/CodeGen/MachineModuleInfo.h"
Evan Chengc5adcca2012-06-08 21:53:50 +000027#include "llvm/CodeGen/MachineRegisterInfo.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
Chandler Carruth1b9dde02014-04-22 02:02:50 +000042#define DEBUG_TYPE "ifcvt"
43
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;
Akira Hatanaka4a616192015-06-08 18:50:43 +0000187 std::function<bool(const Function &)> PredicateFtor;
188
Evan Chengf5e53a52007-05-16 02:00:57 +0000189 public:
190 static char ID;
Akira Hatanaka4a616192015-06-08 18:50:43 +0000191 IfConverter(std::function<bool(const Function &)> 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:
Kyle Butt59f2a2a2016-07-27 20:19:31 +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 Cheng288f1542007-06-08 22:01:07 +0000245 void RemoveExtraEdges(BBInfo &BBI);
Evan Cheng3a51c852007-06-16 09:34:52 +0000246 bool IfConvertSimple(BBInfo &BBI, IfcvtKind Kind);
247 bool IfConvertTriangle(BBInfo &BBI, IfcvtKind Kind);
Kyle Butta8c73712016-08-24 21:34:27 +0000248 bool IfConvertDiamondCommon(BBInfo &BBI, BBInfo &TrueBBI, BBInfo &FalseBBI,
249 unsigned NumDups1, unsigned NumDups2,
250 bool TClobbersPred, bool FClobbersPred,
251 bool RemoveTrueBranch, bool RemoveFalseBranch,
252 bool MergeAddEdges);
Evan Cheng51eb2c32007-06-18 08:37:25 +0000253 bool IfConvertDiamond(BBInfo &BBI, IfcvtKind Kind,
Kyle Butt6262ca32016-08-24 21:34:24 +0000254 unsigned NumDups1, unsigned NumDups2,
255 bool TClobbers, bool FClobbers);
Kyle Butta8c73712016-08-24 21:34:27 +0000256 bool IfConvertForkedDiamond(BBInfo &BBI, IfcvtKind Kind,
257 unsigned NumDups1, unsigned NumDups2,
258 bool TClobbers, bool FClobbers);
Evan Chengd0e66912007-05-23 07:23:16 +0000259 void PredicateBlock(BBInfo &BBI,
Evan Cheng51eb2c32007-06-18 08:37:25 +0000260 MachineBasicBlock::iterator E,
Evan Chengf128bdc2010-06-16 07:35:02 +0000261 SmallVectorImpl<MachineOperand> &Cond,
Craig Topperc0196b12014-04-14 00:51:57 +0000262 SmallSet<unsigned, 4> *LaterRedefs = nullptr);
Evan Cheng92fb5452007-06-15 07:36:12 +0000263 void CopyAndPredicateBlock(BBInfo &ToBBI, BBInfo &FromBBI,
Owen Anderson4f6bf042008-08-14 22:49:33 +0000264 SmallVectorImpl<MachineOperand> &Cond,
Evan Cheng92fb5452007-06-15 07:36:12 +0000265 bool IgnoreBr = false);
Bob Wilson1e5da552010-06-29 00:55:23 +0000266 void MergeBlocks(BBInfo &ToBBI, BBInfo &FromBBI, bool AddEdges = true);
Evan Cheng20e05992007-06-01 00:12:12 +0000267
Evan Chengdebf9c52010-11-03 00:45:17 +0000268 bool MeetIfcvtSizeLimit(MachineBasicBlock &BB,
269 unsigned Cycle, unsigned Extra,
Cong Houc536bd92015-09-10 23:10:42 +0000270 BranchProbability Prediction) const {
Evan Chengdebf9c52010-11-03 00:45:17 +0000271 return Cycle > 0 && TII->isProfitableToIfCvt(BB, Cycle, Extra,
Jakub Staszak9b07c0a2011-07-10 02:58:07 +0000272 Prediction);
Evan Cheng02b184d2010-06-25 22:42:03 +0000273 }
274
Evan Chengdebf9c52010-11-03 00:45:17 +0000275 bool MeetIfcvtSizeLimit(MachineBasicBlock &TBB,
276 unsigned TCycle, unsigned TExtra,
277 MachineBasicBlock &FBB,
278 unsigned FCycle, unsigned FExtra,
Cong Houc536bd92015-09-10 23:10:42 +0000279 BranchProbability Prediction) const {
Evan Chengdebf9c52010-11-03 00:45:17 +0000280 return TCycle > 0 && FCycle > 0 &&
281 TII->isProfitableToIfCvt(TBB, TCycle, TExtra, FBB, FCycle, FExtra,
Jakub Staszak9b07c0a2011-07-10 02:58:07 +0000282 Prediction);
Evan Cheng51eb2c32007-06-18 08:37:25 +0000283 }
284
Matthias Braun2c931792016-08-17 02:51:57 +0000285 /// Returns true if Block ends without a terminator.
Evan Chengbe9859e2007-06-07 02:12:15 +0000286 bool blockAlwaysFallThrough(BBInfo &BBI) const {
Craig Topperc0196b12014-04-14 00:51:57 +0000287 return BBI.IsBrAnalyzable && BBI.TrueBB == nullptr;
Evan Cheng9030b982007-06-06 10:16:17 +0000288 }
289
Matthias Braun2c931792016-08-17 02:51:57 +0000290 /// Used to sort if-conversion candidates.
Justin Lebar3a7bc572016-02-22 17:51:28 +0000291 static bool IfcvtTokenCmp(const std::unique_ptr<IfcvtToken> &C1,
292 const std::unique_ptr<IfcvtToken> &C2) {
Evan Cheng51eb2c32007-06-18 08:37:25 +0000293 int Incr1 = (C1->Kind == ICDiamond)
294 ? -(int)(C1->NumDups + C1->NumDups2) : (int)C1->NumDups;
295 int Incr2 = (C2->Kind == ICDiamond)
296 ? -(int)(C2->NumDups + C2->NumDups2) : (int)C2->NumDups;
297 if (Incr1 > Incr2)
Evan Cheng3a51c852007-06-16 09:34:52 +0000298 return true;
Evan Cheng51eb2c32007-06-18 08:37:25 +0000299 else if (Incr1 == Incr2) {
Bob Wilson2371f4f2009-05-13 23:25:24 +0000300 // Favors subsumption.
David Blaikiedc3f01e2015-03-09 01:57:13 +0000301 if (!C1->NeedSubsumption && C2->NeedSubsumption)
Evan Cheng3a51c852007-06-16 09:34:52 +0000302 return true;
Bob Wilson2371f4f2009-05-13 23:25:24 +0000303 else if (C1->NeedSubsumption == C2->NeedSubsumption) {
Evan Cheng3a51c852007-06-16 09:34:52 +0000304 // Favors diamond over triangle, etc.
305 if ((unsigned)C1->Kind < (unsigned)C2->Kind)
306 return true;
307 else if (C1->Kind == C2->Kind)
308 return C1->BBI.BB->getNumber() < C2->BBI.BB->getNumber();
309 }
310 }
311 return false;
Evan Cheng20e05992007-06-01 00:12:12 +0000312 }
Evan Chengf5e53a52007-05-16 02:00:57 +0000313 };
Evan Cheng3a51c852007-06-16 09:34:52 +0000314
Evan Chengf5e53a52007-05-16 02:00:57 +0000315 char IfConverter::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000316}
Evan Chengf5e53a52007-05-16 02:00:57 +0000317
Andrew Trick1fa5bcb2012-02-08 21:23:13 +0000318char &llvm::IfConverterID = IfConverter::ID;
319
Owen Anderson8ac477f2010-10-12 19:48:12 +0000320INITIALIZE_PASS_BEGIN(IfConverter, "if-converter", "If Converter", false, false)
Jakub Staszak15e5b742011-08-03 22:34:43 +0000321INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
Owen Anderson8ac477f2010-10-12 19:48:12 +0000322INITIALIZE_PASS_END(IfConverter, "if-converter", "If Converter", false, false)
Bob Wilson97b93122009-10-28 20:46:46 +0000323
Evan Chengf5e53a52007-05-16 02:00:57 +0000324bool IfConverter::runOnMachineFunction(MachineFunction &MF) {
Andrew Kaylor50271f72016-05-03 22:32:30 +0000325 if (skipFunction(*MF.getFunction()) ||
326 (PredicateFtor && !PredicateFtor(*MF.getFunction())))
Akira Hatanaka4a616192015-06-08 18:50:43 +0000327 return false;
328
Eric Christopher3d4276f2015-01-27 07:31:29 +0000329 const TargetSubtargetInfo &ST = MF.getSubtarget();
330 TLI = ST.getTargetLowering();
331 TII = ST.getInstrInfo();
332 TRI = ST.getRegisterInfo();
Haicheng Wu5b458cc2016-06-09 15:24:29 +0000333 BranchFolder::MBFIWrapper MBFI(getAnalysis<MachineBlockFrequencyInfo>());
Jakub Staszak15e5b742011-08-03 22:34:43 +0000334 MBPI = &getAnalysis<MachineBranchProbabilityInfo>();
Evan Chengc5adcca2012-06-08 21:53:50 +0000335 MRI = &MF.getRegInfo();
Pete Cooper11759452014-09-02 17:43:54 +0000336 SchedModel.init(ST.getSchedModel(), &ST, TII);
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +0000337
Evan Chengf5e53a52007-05-16 02:00:57 +0000338 if (!TII) return false;
339
Evan Chengc5adcca2012-06-08 21:53:50 +0000340 PreRegAlloc = MRI->isSSA();
341
342 bool BFChange = false;
343 if (!PreRegAlloc) {
344 // Tail merge tend to expose more if-conversion opportunities.
Haicheng Wu5b458cc2016-06-09 15:24:29 +0000345 BranchFolder BF(true, false, MBFI, *MBPI);
Eric Christopher3d4276f2015-01-27 07:31:29 +0000346 BFChange = BF.OptimizeFunction(MF, TII, ST.getRegisterInfo(),
Eric Christopherfc6de422014-08-05 02:39:49 +0000347 getAnalysisIfAvailable<MachineModuleInfo>());
Evan Chengc5adcca2012-06-08 21:53:50 +0000348 }
Evan Cheng2d51c7c2010-06-18 23:09:54 +0000349
David Greene72e47cd2010-01-04 22:02:01 +0000350 DEBUG(dbgs() << "\nIfcvt: function (" << ++FnNum << ") \'"
Craig Toppera538d832012-08-22 06:07:19 +0000351 << MF.getName() << "\'");
Evan Chenge93ccc02007-06-08 19:10:51 +0000352
353 if (FnNum < IfCvtFnStart || (IfCvtFnStop != -1 && FnNum > IfCvtFnStop)) {
David Greene72e47cd2010-01-04 22:02:01 +0000354 DEBUG(dbgs() << " skipped\n");
Evan Chenge93ccc02007-06-08 19:10:51 +0000355 return false;
356 }
David Greene72e47cd2010-01-04 22:02:01 +0000357 DEBUG(dbgs() << "\n");
Evan Cheng20e05992007-06-01 00:12:12 +0000358
Evan Chengf5e53a52007-05-16 02:00:57 +0000359 MF.RenumberBlocks();
Evan Cheng20e05992007-06-01 00:12:12 +0000360 BBAnalysis.resize(MF.getNumBlockIDs());
Evan Chengf5e53a52007-05-16 02:00:57 +0000361
Justin Lebar3a7bc572016-02-22 17:51:28 +0000362 std::vector<std::unique_ptr<IfcvtToken>> Tokens;
Evan Cheng478b8052007-05-18 01:55:58 +0000363 MadeChange = false;
Evan Cheng92fb5452007-06-15 07:36:12 +0000364 unsigned NumIfCvts = NumSimple + NumSimpleFalse + NumTriangle +
365 NumTriangleRev + NumTriangleFalse + NumTriangleFRev + NumDiamonds;
366 while (IfCvtLimit == -1 || (int)NumIfCvts < IfCvtLimit) {
Bob Wilson2371f4f2009-05-13 23:25:24 +0000367 // Do an initial analysis for each basic block and find all the potential
368 // candidates to perform if-conversion.
Bob Wilsonfc7d7392010-06-15 18:57:15 +0000369 bool Change = false;
370 AnalyzeBlocks(MF, Tokens);
Evan Cheng3a51c852007-06-16 09:34:52 +0000371 while (!Tokens.empty()) {
Justin Lebar3a7bc572016-02-22 17:51:28 +0000372 std::unique_ptr<IfcvtToken> Token = std::move(Tokens.back());
Evan Cheng3a51c852007-06-16 09:34:52 +0000373 Tokens.pop_back();
374 BBInfo &BBI = Token->BBI;
375 IfcvtKind Kind = Token->Kind;
Nuno Lopes57c65942008-11-04 13:02:59 +0000376 unsigned NumDups = Token->NumDups;
Duncan Sandsdd571d32008-11-04 18:05:30 +0000377 unsigned NumDups2 = Token->NumDups2;
Nuno Lopes57c65942008-11-04 13:02:59 +0000378
Evan Cheng4dd31a72007-06-11 22:26:22 +0000379 // If the block has been evicted out of the queue or it has already been
380 // marked dead (due to it being predicated), then skip it.
Evan Cheng3a51c852007-06-16 09:34:52 +0000381 if (BBI.IsDone)
382 BBI.IsEnqueued = false;
383 if (!BBI.IsEnqueued)
Evan Cheng4dd31a72007-06-11 22:26:22 +0000384 continue;
Evan Cheng3a51c852007-06-16 09:34:52 +0000385
Evan Cheng1e6f08b2007-06-14 20:28:52 +0000386 BBI.IsEnqueued = false;
Evan Cheng4dd31a72007-06-11 22:26:22 +0000387
Evan Cheng312b7232007-06-04 06:47:22 +0000388 bool RetVal = false;
Evan Cheng3a51c852007-06-16 09:34:52 +0000389 switch (Kind) {
Craig Topperee4dab52012-02-05 08:31:47 +0000390 default: llvm_unreachable("Unexpected!");
Evan Cheng312b7232007-06-04 06:47:22 +0000391 case ICSimple:
Evan Chengb30a8942007-06-06 01:12:44 +0000392 case ICSimpleFalse: {
Evan Cheng3a51c852007-06-16 09:34:52 +0000393 bool isFalse = Kind == ICSimpleFalse;
Evan Cheng2117d1f2007-06-09 01:03:43 +0000394 if ((isFalse && DisableSimpleF) || (!isFalse && DisableSimple)) break;
Bob Wilson81051442010-06-15 22:18:54 +0000395 DEBUG(dbgs() << "Ifcvt (Simple" << (Kind == ICSimpleFalse ?
396 " false" : "")
Bill Wendlingfb363162009-08-22 20:11:17 +0000397 << "): BB#" << BBI.BB->getNumber() << " ("
398 << ((Kind == ICSimpleFalse)
399 ? BBI.FalseBB->getNumber()
400 : BBI.TrueBB->getNumber()) << ") ");
Evan Cheng3a51c852007-06-16 09:34:52 +0000401 RetVal = IfConvertSimple(BBI, Kind);
David Greene72e47cd2010-01-04 22:02:01 +0000402 DEBUG(dbgs() << (RetVal ? "succeeded!" : "failed!") << "\n");
Anton Korobeynikov035eaac2008-02-20 11:10:28 +0000403 if (RetVal) {
Dan Gohmand2d1ae12010-06-22 15:08:57 +0000404 if (isFalse) ++NumSimpleFalse;
405 else ++NumSimple;
Anton Korobeynikov035eaac2008-02-20 11:10:28 +0000406 }
Evan Cheng312b7232007-06-04 06:47:22 +0000407 break;
Evan Chengb30a8942007-06-06 01:12:44 +0000408 }
Evan Chengd0e66912007-05-23 07:23:16 +0000409 case ICTriangle:
Evan Cheng9acfa7b2007-06-12 23:54:05 +0000410 case ICTriangleRev:
Evan Cheng2117d1f2007-06-09 01:03:43 +0000411 case ICTriangleFalse:
Reid Spencer14b62a52007-06-10 00:19:17 +0000412 case ICTriangleFRev: {
Evan Cheng3a51c852007-06-16 09:34:52 +0000413 bool isFalse = Kind == ICTriangleFalse;
414 bool isRev = (Kind == ICTriangleRev || Kind == ICTriangleFRev);
Evan Cheng9acfa7b2007-06-12 23:54:05 +0000415 if (DisableTriangle && !isFalse && !isRev) break;
416 if (DisableTriangleR && !isFalse && isRev) break;
417 if (DisableTriangleF && isFalse && !isRev) break;
418 if (DisableTriangleFR && isFalse && isRev) break;
David Greene72e47cd2010-01-04 22:02:01 +0000419 DEBUG(dbgs() << "Ifcvt (Triangle");
Evan Cheng2117d1f2007-06-09 01:03:43 +0000420 if (isFalse)
David Greene72e47cd2010-01-04 22:02:01 +0000421 DEBUG(dbgs() << " false");
Evan Cheng9acfa7b2007-06-12 23:54:05 +0000422 if (isRev)
David Greene72e47cd2010-01-04 22:02:01 +0000423 DEBUG(dbgs() << " rev");
424 DEBUG(dbgs() << "): BB#" << BBI.BB->getNumber() << " (T:"
Bill Wendlingfb363162009-08-22 20:11:17 +0000425 << BBI.TrueBB->getNumber() << ",F:"
426 << BBI.FalseBB->getNumber() << ") ");
Evan Cheng3a51c852007-06-16 09:34:52 +0000427 RetVal = IfConvertTriangle(BBI, Kind);
David Greene72e47cd2010-01-04 22:02:01 +0000428 DEBUG(dbgs() << (RetVal ? "succeeded!" : "failed!") << "\n");
Evan Cheng2117d1f2007-06-09 01:03:43 +0000429 if (RetVal) {
Evan Cheng9acfa7b2007-06-12 23:54:05 +0000430 if (isFalse) {
Dan Gohmand2d1ae12010-06-22 15:08:57 +0000431 if (isRev) ++NumTriangleFRev;
432 else ++NumTriangleFalse;
Evan Cheng9acfa7b2007-06-12 23:54:05 +0000433 } else {
Dan Gohmand2d1ae12010-06-22 15:08:57 +0000434 if (isRev) ++NumTriangleRev;
435 else ++NumTriangle;
Evan Cheng9acfa7b2007-06-12 23:54:05 +0000436 }
Evan Cheng2117d1f2007-06-09 01:03:43 +0000437 }
Evan Chengd0e66912007-05-23 07:23:16 +0000438 break;
Reid Spencer14b62a52007-06-10 00:19:17 +0000439 }
Evan Cheng3a51c852007-06-16 09:34:52 +0000440 case ICDiamond: {
Evan Chenge93ccc02007-06-08 19:10:51 +0000441 if (DisableDiamond) break;
David Greene72e47cd2010-01-04 22:02:01 +0000442 DEBUG(dbgs() << "Ifcvt (Diamond): BB#" << BBI.BB->getNumber() << " (T:"
Bill Wendlingfb363162009-08-22 20:11:17 +0000443 << BBI.TrueBB->getNumber() << ",F:"
444 << BBI.FalseBB->getNumber() << ") ");
Kyle Butt6262ca32016-08-24 21:34:24 +0000445 RetVal = IfConvertDiamond(BBI, Kind, NumDups, NumDups2,
446 Token->TClobbersPred,
447 Token->FClobbersPred);
David Greene72e47cd2010-01-04 22:02:01 +0000448 DEBUG(dbgs() << (RetVal ? "succeeded!" : "failed!") << "\n");
Dan Gohmand2d1ae12010-06-22 15:08:57 +0000449 if (RetVal) ++NumDiamonds;
Evan Chengd0e66912007-05-23 07:23:16 +0000450 break;
451 }
Kyle Butta8c73712016-08-24 21:34:27 +0000452 case ICForkedDiamond: {
453 if (DisableForkedDiamond) break;
454 DEBUG(dbgs() << "Ifcvt (Forked Diamond): BB#"
455 << BBI.BB->getNumber() << " (T:"
456 << BBI.TrueBB->getNumber() << ",F:"
457 << BBI.FalseBB->getNumber() << ") ");
458 RetVal = IfConvertForkedDiamond(BBI, Kind, NumDups, NumDups2,
459 Token->TClobbersPred,
460 Token->FClobbersPred);
461 DEBUG(dbgs() << (RetVal ? "succeeded!" : "failed!") << "\n");
462 if (RetVal) ++NumForkedDiamonds;
463 break;
464 }
Evan Cheng3a51c852007-06-16 09:34:52 +0000465 }
466
Evan Cheng312b7232007-06-04 06:47:22 +0000467 Change |= RetVal;
Evan Chenge93ccc02007-06-08 19:10:51 +0000468
Evan Cheng92fb5452007-06-15 07:36:12 +0000469 NumIfCvts = NumSimple + NumSimpleFalse + NumTriangle + NumTriangleRev +
470 NumTriangleFalse + NumTriangleFRev + NumDiamonds;
471 if (IfCvtLimit != -1 && (int)NumIfCvts >= IfCvtLimit)
Evan Chenge93ccc02007-06-08 19:10:51 +0000472 break;
Evan Chengf5e53a52007-05-16 02:00:57 +0000473 }
Evan Chengd0e66912007-05-23 07:23:16 +0000474
Evan Chengd0e66912007-05-23 07:23:16 +0000475 if (!Change)
476 break;
Evan Cheng312b7232007-06-04 06:47:22 +0000477 MadeChange |= Change;
Evan Chengf5e53a52007-05-16 02:00:57 +0000478 }
Evan Cheng478b8052007-05-18 01:55:58 +0000479
Evan Cheng3a51c852007-06-16 09:34:52 +0000480 Tokens.clear();
Evan Cheng478b8052007-05-18 01:55:58 +0000481 BBAnalysis.clear();
482
Evan Chengcf9e8a92010-06-18 22:17:13 +0000483 if (MadeChange && IfCvtBranchFold) {
Haicheng Wu5b458cc2016-06-09 15:24:29 +0000484 BranchFolder BF(false, false, MBFI, *MBPI);
Eric Christopherfc6de422014-08-05 02:39:49 +0000485 BF.OptimizeFunction(MF, TII, MF.getSubtarget().getRegisterInfo(),
Evan Cheng3d2fce02009-09-04 07:47:40 +0000486 getAnalysisIfAvailable<MachineModuleInfo>());
487 }
488
Evan Cheng2d51c7c2010-06-18 23:09:54 +0000489 MadeChange |= BFChange;
Evan Chengf5e53a52007-05-16 02:00:57 +0000490 return MadeChange;
491}
492
Matthias Braun2c931792016-08-17 02:51:57 +0000493/// BB has a fallthrough. Find its 'false' successor given its 'true' successor.
Evan Chengf5e53a52007-05-16 02:00:57 +0000494static MachineBasicBlock *findFalseBlock(MachineBasicBlock *BB,
Evan Cheng0f745da2007-05-18 00:20:58 +0000495 MachineBasicBlock *TrueBB) {
Matthias Braunb1e05582016-08-17 02:51:59 +0000496 for (MachineBasicBlock *SuccBB : BB->successors()) {
Evan Cheng0f745da2007-05-18 00:20:58 +0000497 if (SuccBB != TrueBB)
Evan Chengf5e53a52007-05-16 02:00:57 +0000498 return SuccBB;
499 }
Craig Topperc0196b12014-04-14 00:51:57 +0000500 return nullptr;
Evan Chengf5e53a52007-05-16 02:00:57 +0000501}
502
Matthias Braun2c931792016-08-17 02:51:57 +0000503/// Reverse the condition of the end of the block branch. Swap block's 'true'
504/// and 'false' successors.
Kyle Butt59f2a2a2016-07-27 20:19:31 +0000505bool IfConverter::ReverseBranchCondition(BBInfo &BBI) const {
Stuart Hastings0125b642010-06-17 22:43:56 +0000506 DebugLoc dl; // FIXME: this is nowhere
Evan Cheng312b7232007-06-04 06:47:22 +0000507 if (!TII->ReverseBranchCondition(BBI.BrCond)) {
508 TII->RemoveBranch(*BBI.BB);
Stuart Hastings0125b642010-06-17 22:43:56 +0000509 TII->InsertBranch(*BBI.BB, BBI.FalseBB, BBI.TrueBB, BBI.BrCond, dl);
Evan Cheng312b7232007-06-04 06:47:22 +0000510 std::swap(BBI.TrueBB, BBI.FalseBB);
511 return true;
512 }
513 return false;
514}
515
Matthias Braun2c931792016-08-17 02:51:57 +0000516/// Returns the next block in the function blocks ordering. If it is the end,
517/// returns NULL.
Matthias Braun08f47042016-08-17 02:52:01 +0000518static inline MachineBasicBlock *getNextBlock(MachineBasicBlock &MBB) {
519 MachineFunction::iterator I = MBB.getIterator();
520 MachineFunction::iterator E = MBB.getParent()->end();
Evan Cheng2117d1f2007-06-09 01:03:43 +0000521 if (++I == E)
Craig Topperc0196b12014-04-14 00:51:57 +0000522 return nullptr;
Duncan P. N. Exon Smith5ae59392015-10-09 19:13:58 +0000523 return &*I;
Evan Cheng2117d1f2007-06-09 01:03:43 +0000524}
525
Matthias Braun2c931792016-08-17 02:51:57 +0000526/// Returns true if the 'true' block (along with its predecessor) forms a valid
527/// simple shape for ifcvt. It also returns the number of instructions that the
528/// ifcvt would need to duplicate if performed in Dups.
Owen Anderson1b35f4c2010-09-28 20:42:15 +0000529bool IfConverter::ValidSimple(BBInfo &TrueBBI, unsigned &Dups,
Cong Houc536bd92015-09-10 23:10:42 +0000530 BranchProbability Prediction) const {
Evan Cheng3a51c852007-06-16 09:34:52 +0000531 Dups = 0;
Evan Cheng51eb2c32007-06-18 08:37:25 +0000532 if (TrueBBI.IsBeingAnalyzed || TrueBBI.IsDone)
Evan Cheng4dd31a72007-06-11 22:26:22 +0000533 return false;
534
Evan Chenga955c022007-06-19 21:45:13 +0000535 if (TrueBBI.IsBrAnalyzable)
536 return false;
537
Evan Cheng23402fc2007-06-15 21:18:05 +0000538 if (TrueBBI.BB->pred_size() > 1) {
539 if (TrueBBI.CannotBeCopied ||
Owen Anderson1b35f4c2010-09-28 20:42:15 +0000540 !TII->isProfitableToDupForIfCvt(*TrueBBI.BB, TrueBBI.NonPredSize,
Jakub Staszak9b07c0a2011-07-10 02:58:07 +0000541 Prediction))
Evan Cheng92fb5452007-06-15 07:36:12 +0000542 return false;
Evan Cheng3a51c852007-06-16 09:34:52 +0000543 Dups = TrueBBI.NonPredSize;
Evan Cheng92fb5452007-06-15 07:36:12 +0000544 }
545
Evan Chenga955c022007-06-19 21:45:13 +0000546 return true;
Evan Cheng9030b982007-06-06 10:16:17 +0000547}
548
Matthias Braun2c931792016-08-17 02:51:57 +0000549/// Returns true if the 'true' and 'false' blocks (along with their common
550/// predecessor) forms a valid triangle shape for ifcvt. If 'FalseBranch' is
551/// true, it checks if 'true' block's false branch branches to the 'false' block
552/// rather than the other way around. It also returns the number of instructions
553/// that the ifcvt would need to duplicate if performed in 'Dups'.
Evan Cheng7783f822007-06-08 09:36:04 +0000554bool IfConverter::ValidTriangle(BBInfo &TrueBBI, BBInfo &FalseBBI,
Owen Andersonf31f33e2010-10-01 22:45:50 +0000555 bool FalseBranch, unsigned &Dups,
Cong Houc536bd92015-09-10 23:10:42 +0000556 BranchProbability Prediction) const {
Evan Cheng3a51c852007-06-16 09:34:52 +0000557 Dups = 0;
Evan Cheng51eb2c32007-06-18 08:37:25 +0000558 if (TrueBBI.IsBeingAnalyzed || TrueBBI.IsDone)
Evan Cheng4dd31a72007-06-11 22:26:22 +0000559 return false;
560
Evan Cheng23402fc2007-06-15 21:18:05 +0000561 if (TrueBBI.BB->pred_size() > 1) {
562 if (TrueBBI.CannotBeCopied)
563 return false;
564
Evan Cheng92fb5452007-06-15 07:36:12 +0000565 unsigned Size = TrueBBI.NonPredSize;
Evan Cheng3a51c852007-06-16 09:34:52 +0000566 if (TrueBBI.IsBrAnalyzable) {
Dan Gohman70de4cb2008-01-29 13:02:09 +0000567 if (TrueBBI.TrueBB && TrueBBI.BrCond.empty())
Bob Wilson2371f4f2009-05-13 23:25:24 +0000568 // Ends with an unconditional branch. It will be removed.
Evan Cheng3a51c852007-06-16 09:34:52 +0000569 --Size;
570 else {
571 MachineBasicBlock *FExit = FalseBranch
572 ? TrueBBI.TrueBB : TrueBBI.FalseBB;
573 if (FExit)
574 // Require a conditional branch
575 ++Size;
576 }
577 }
Jakub Staszak9b07c0a2011-07-10 02:58:07 +0000578 if (!TII->isProfitableToDupForIfCvt(*TrueBBI.BB, Size, Prediction))
Evan Cheng92fb5452007-06-15 07:36:12 +0000579 return false;
Evan Cheng3a51c852007-06-16 09:34:52 +0000580 Dups = Size;
Evan Cheng92fb5452007-06-15 07:36:12 +0000581 }
Evan Chengbe9859e2007-06-07 02:12:15 +0000582
Evan Cheng7783f822007-06-08 09:36:04 +0000583 MachineBasicBlock *TExit = FalseBranch ? TrueBBI.FalseBB : TrueBBI.TrueBB;
584 if (!TExit && blockAlwaysFallThrough(TrueBBI)) {
Duncan P. N. Exon Smith5ae59392015-10-09 19:13:58 +0000585 MachineFunction::iterator I = TrueBBI.BB->getIterator();
Evan Chengbe9859e2007-06-07 02:12:15 +0000586 if (++I == TrueBBI.BB->getParent()->end())
587 return false;
Duncan P. N. Exon Smith5ae59392015-10-09 19:13:58 +0000588 TExit = &*I;
Evan Chengbe9859e2007-06-07 02:12:15 +0000589 }
Evan Cheng7783f822007-06-08 09:36:04 +0000590 return TExit && TExit == FalseBBI.BB;
Evan Chengbe9859e2007-06-07 02:12:15 +0000591}
592
Matthias Braun2c931792016-08-17 02:51:57 +0000593/// Increment \p It until it points to a non-debug instruction or to \p End.
Kyle Buttfe916822016-08-06 01:52:31 +0000594/// @param It Iterator to increment
595/// @param End Iterator that points to end. Will be compared to It
596/// @returns true if It == End, false otherwise.
597static inline bool skipDebugInstructionsForward(
598 MachineBasicBlock::iterator &It,
599 MachineBasicBlock::iterator &End) {
600 while (It != End && It->isDebugValue())
601 It++;
602 return It == End;
603}
604
Kyle Buttd76755e2016-08-18 22:09:23 +0000605/// Shrink the provided inclusive range by one instruction.
606/// If the range was one instruction (\p It == \p Begin), It is not modified,
607/// but \p Empty is set to true.
608static inline void shrinkInclusiveRange(
609 MachineBasicBlock::iterator &Begin,
610 MachineBasicBlock::iterator &It,
611 bool &Empty) {
612 if (It == Begin)
613 Empty = true;
614 else
615 It--;
616}
617
618/// Decrement \p It until it points to a non-debug instruction or the range is
619/// empty.
Kyle Buttfe916822016-08-06 01:52:31 +0000620/// @param It Iterator to decrement.
David Majnemer70c93fa2016-08-06 08:37:12 +0000621/// @param Begin Iterator that points to beginning. Will be compared to It
Kyle Buttd76755e2016-08-18 22:09:23 +0000622/// @param Empty Set to true if the resulting range is Empty
623/// @returns the value of Empty as a convenience.
Kyle Buttfe916822016-08-06 01:52:31 +0000624static inline bool skipDebugInstructionsBackward(
Kyle Buttd76755e2016-08-18 22:09:23 +0000625 MachineBasicBlock::iterator &Begin,
Kyle Buttfe916822016-08-06 01:52:31 +0000626 MachineBasicBlock::iterator &It,
Kyle Buttd76755e2016-08-18 22:09:23 +0000627 bool &Empty) {
628 while (!Empty && It->isDebugValue())
629 shrinkInclusiveRange(Begin, It, Empty);
630 return Empty;
Kyle Buttfe916822016-08-06 01:52:31 +0000631}
632
Kyle Butt4f0e2872016-08-06 01:52:33 +0000633/// Count duplicated instructions and move the iterators to show where they
634/// are.
635/// @param TIB True Iterator Begin
636/// @param FIB False Iterator Begin
637/// These two iterators initially point to the first instruction of the two
638/// blocks, and finally point to the first non-shared instruction.
639/// @param TIE True Iterator End
640/// @param FIE False Iterator End
641/// These two iterators initially point to End() for the two blocks() and
642/// finally point to the first shared instruction in the tail.
643/// Upon return [TIB, TIE), and [FIB, FIE) mark the un-duplicated portions of
644/// two blocks.
Kyle Butt6262ca32016-08-24 21:34:24 +0000645/// @param Dups1 count of duplicated instructions at the beginning of the 2
646/// blocks.
647/// @param Dups2 count of duplicated instructions at the end of the 2 blocks.
648/// @param SkipUnconditionalBranches if true, Don't make sure that
649/// unconditional branches at the end of the blocks are the same. True is
650/// passed when the blocks are analyzable to allow for fallthrough to be
651/// handled.
652/// @return false if the shared portion prevents if conversion.
653bool IfConverter::CountDuplicatedInstructions(
Kyle Butt9b6d99b2016-07-27 20:19:33 +0000654 MachineBasicBlock::iterator &TIB,
655 MachineBasicBlock::iterator &FIB,
656 MachineBasicBlock::iterator &TIE,
657 MachineBasicBlock::iterator &FIE,
658 unsigned &Dups1, unsigned &Dups2,
659 MachineBasicBlock &TBB, MachineBasicBlock &FBB,
Kyle Butt6262ca32016-08-24 21:34:24 +0000660 bool SkipUnconditionalBranches) const {
Evan Cheng4dd31a72007-06-11 22:26:22 +0000661
Bob Wilsone1961fe2010-10-26 00:02:24 +0000662 while (TIB != TIE && FIB != FIE) {
Evan Chengc0e0d852010-06-18 21:52:57 +0000663 // Skip dbg_value instructions. These do not count.
Kyle Buttfe916822016-08-06 01:52:31 +0000664 if(skipDebugInstructionsForward(TIB, TIE))
665 break;
666 if(skipDebugInstructionsForward(FIB, FIE))
667 break;
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +0000668 if (!TIB->isIdenticalTo(*FIB))
Bob Wilsone1961fe2010-10-26 00:02:24 +0000669 break;
Kyle Butt6262ca32016-08-24 21:34:24 +0000670 // A pred-clobbering instruction in the shared portion prevents
671 // if-conversion.
672 std::vector<MachineOperand> PredDefs;
673 if (TII->DefinesPredicate(*TIB, PredDefs))
674 return false;
Bob Wilsone1961fe2010-10-26 00:02:24 +0000675 ++Dups1;
676 ++TIB;
677 ++FIB;
678 }
679
Krzysztof Parzyszek2451c482016-01-20 13:14:52 +0000680 // Check for already containing all of the block.
681 if (TIB == TIE || FIB == FIE)
Kyle Butt6262ca32016-08-24 21:34:24 +0000682 return true;
Kyle Buttd76755e2016-08-18 22:09:23 +0000683 // Now, in preparation for counting duplicate instructions at the ends of the
684 // blocks, move the end iterators up past any branch instructions.
Krzysztof Parzyszek2451c482016-01-20 13:14:52 +0000685 --TIE;
686 --FIE;
Kyle Buttd76755e2016-08-18 22:09:23 +0000687
688 // After this point TIB and TIE define an inclusive range, which means that
689 // TIB == TIE is true when there is one more instruction to consider, not at
690 // the end. Because we may not be able to go before TIB, we need a flag to
691 // indicate a completely empty range.
692 bool TEmpty = false, FEmpty = false;
693
694 // Upon exit TIE and FIE will both point at the last non-shared instruction.
695 // They need to be moved forward to point past the last non-shared
696 // instruction if the range they delimit is non-empty.
697 auto IncrementEndIteratorsOnExit = make_scope_exit([&]() {
698 if (!TEmpty)
699 ++TIE;
700 if (!FEmpty)
701 ++FIE;
702 });
703
Kyle Butt9b6d99b2016-07-27 20:19:33 +0000704 if (!TBB.succ_empty() || !FBB.succ_empty()) {
Kyle Butt6262ca32016-08-24 21:34:24 +0000705 if (SkipUnconditionalBranches) {
Kyle Buttd76755e2016-08-18 22:09:23 +0000706 while (!TEmpty && TIE->isUnconditionalBranch())
707 shrinkInclusiveRange(TIB, TIE, TEmpty);
708 while (!FEmpty && FIE->isUnconditionalBranch())
709 shrinkInclusiveRange(FIB, FIE, FEmpty);
Kyle Butt9b6d99b2016-07-27 20:19:33 +0000710 }
Bob Wilsone1961fe2010-10-26 00:02:24 +0000711 }
712
713 // If Dups1 includes all of a block, then don't count duplicate
714 // instructions at the end of the blocks.
Kyle Buttd76755e2016-08-18 22:09:23 +0000715 if (TEmpty || FEmpty)
Kyle Butt6262ca32016-08-24 21:34:24 +0000716 return true;
Bob Wilsone1961fe2010-10-26 00:02:24 +0000717
718 // Count duplicate instructions at the ends of the blocks.
Kyle Buttd76755e2016-08-18 22:09:23 +0000719 while (!TEmpty && !FEmpty) {
Bob Wilsone1961fe2010-10-26 00:02:24 +0000720 // Skip dbg_value instructions. These do not count.
Kyle Buttd76755e2016-08-18 22:09:23 +0000721 if (skipDebugInstructionsBackward(TIB, TIE, TEmpty))
Kyle Buttfe916822016-08-06 01:52:31 +0000722 break;
Kyle Buttd76755e2016-08-18 22:09:23 +0000723 if (skipDebugInstructionsBackward(FIB, FIE, FEmpty))
Kyle Buttfe916822016-08-06 01:52:31 +0000724 break;
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +0000725 if (!TIE->isIdenticalTo(*FIE))
Evan Cheng51eb2c32007-06-18 08:37:25 +0000726 break;
Kyle Butt6262ca32016-08-24 21:34:24 +0000727 // We have to verify that any branch instructions are the same, and then we
728 // don't count them toward the # of duplicate instructions.
729 if (!TIE->isBranch())
Kyle Butt9b6d99b2016-07-27 20:19:33 +0000730 ++Dups2;
Kyle Buttd76755e2016-08-18 22:09:23 +0000731 shrinkInclusiveRange(TIB, TIE, TEmpty);
732 shrinkInclusiveRange(FIB, FIE, FEmpty);
Evan Cheng51eb2c32007-06-18 08:37:25 +0000733 }
Kyle Butt6262ca32016-08-24 21:34:24 +0000734 return true;
735}
736
737/// RescanInstructions - Run ScanInstructions on a pair of blocks.
738/// @param TIB - True Iterator Begin, points to first non-shared instruction
739/// @param FIB - False Iterator Begin, points to first non-shared instruction
740/// @param TIE - True Iterator End, points past last non-shared instruction
741/// @param FIE - False Iterator End, points past last non-shared instruction
742/// @param TrueBBI - BBInfo to update for the true block.
743/// @param FalseBBI - BBInfo to update for the false block.
744/// @returns - false if either block cannot be predicated or if both blocks end
745/// with a predicate-clobbering instruction.
746bool IfConverter::RescanInstructions(
747 MachineBasicBlock::iterator &TIB, MachineBasicBlock::iterator &FIB,
748 MachineBasicBlock::iterator &TIE, MachineBasicBlock::iterator &FIE,
749 BBInfo &TrueBBI, BBInfo &FalseBBI) const {
750 bool BranchUnpredicable = true;
751 TrueBBI.IsUnpredicable = FalseBBI.IsUnpredicable = false;
752 ScanInstructions(TrueBBI, TIB, TIE, BranchUnpredicable);
753 if (TrueBBI.IsUnpredicable)
754 return false;
755 ScanInstructions(FalseBBI, FIB, FIE, BranchUnpredicable);
756 if (FalseBBI.IsUnpredicable)
757 return false;
758 if (TrueBBI.ClobbersPred && FalseBBI.ClobbersPred)
759 return false;
760 return true;
Kyle Butt9b6d99b2016-07-27 20:19:33 +0000761}
Evan Cheng51eb2c32007-06-18 08:37:25 +0000762
Kyle Butta8c73712016-08-24 21:34:27 +0000763/// ValidForkedDiamond - Returns true if the 'true' and 'false' blocks (along
764/// with their common predecessor) form a diamond if a common tail block is
765/// extracted.
766/// While not strictly a diamond, this pattern would form a diamond if
767/// tail-merging had merged the shared tails.
768/// EBB
769/// _/ \_
770/// | |
771/// TBB FBB
772/// / \ / \
773/// FalseBB TrueBB FalseBB
774/// Currently only handles analyzable branches.
775/// Specifically excludes actual diamonds to avoid overlap.
776bool IfConverter::ValidForkedDiamond(
777 BBInfo &TrueBBI, BBInfo &FalseBBI,
778 unsigned &Dups1, unsigned &Dups2,
779 BBInfo &TrueBBICalc, BBInfo &FalseBBICalc) const {
780 Dups1 = Dups2 = 0;
781 if (TrueBBI.IsBeingAnalyzed || TrueBBI.IsDone ||
782 FalseBBI.IsBeingAnalyzed || FalseBBI.IsDone)
783 return false;
784
785 if (!TrueBBI.IsBrAnalyzable || !FalseBBI.IsBrAnalyzable)
786 return false;
787 // Don't IfConvert blocks that can't be folded into their predecessor.
788 if (TrueBBI.BB->pred_size() > 1 || FalseBBI.BB->pred_size() > 1)
789 return false;
790
791 // This function is specifically looking for conditional tails, as
792 // unconditional tails are already handled by the standard diamond case.
793 if (TrueBBI.BrCond.size() == 0 ||
794 FalseBBI.BrCond.size() == 0)
795 return false;
796
797 MachineBasicBlock *TT = TrueBBI.TrueBB;
798 MachineBasicBlock *TF = TrueBBI.FalseBB;
799 MachineBasicBlock *FT = FalseBBI.TrueBB;
800 MachineBasicBlock *FF = FalseBBI.FalseBB;
801
802 if (!TT)
803 TT = getNextBlock(*TrueBBI.BB);
804 if (!TF)
805 TF = getNextBlock(*TrueBBI.BB);
806 if (!FT)
807 FT = getNextBlock(*FalseBBI.BB);
808 if (!FF)
809 FF = getNextBlock(*FalseBBI.BB);
810
811 if (!TT || !TF)
812 return false;
813
814 // Check successors. If they don't match, bail.
815 if (!((TT == FT && TF == FF) || (TF == FT && TT == FF)))
816 return false;
817
818 bool FalseReversed = false;
819 if (TF == FT && TT == FF) {
820 // If the branches are opposing, but we can't reverse, don't do it.
821 if (!FalseBBI.IsBrReversible)
822 return false;
823 FalseReversed = true;
824 ReverseBranchCondition(FalseBBI);
825 }
826 auto UnReverseOnExit = make_scope_exit([&]() {
827 if (FalseReversed)
828 ReverseBranchCondition(FalseBBI);
829 });
830
831 // Count duplicate instructions at the beginning of the true and false blocks.
832 MachineBasicBlock::iterator TIB = TrueBBI.BB->begin();
833 MachineBasicBlock::iterator FIB = FalseBBI.BB->begin();
834 MachineBasicBlock::iterator TIE = TrueBBI.BB->end();
835 MachineBasicBlock::iterator FIE = FalseBBI.BB->end();
836 if(!CountDuplicatedInstructions(TIB, FIB, TIE, FIE, Dups1, Dups2,
837 *TrueBBI.BB, *FalseBBI.BB,
838 /* SkipUnconditionalBranches */ true))
839 return false;
840
841 TrueBBICalc.BB = TrueBBI.BB;
842 FalseBBICalc.BB = FalseBBI.BB;
843 if (!RescanInstructions(TIB, FIB, TIE, FIE, TrueBBICalc, FalseBBICalc))
844 return false;
845
846 // The size is used to decide whether to if-convert, and the shared portions
847 // are subtracted off. Because of the subtraction, we just use the size that
848 // was calculated by the original ScanInstructions, as it is correct.
849 TrueBBICalc.NonPredSize = TrueBBI.NonPredSize;
850 FalseBBICalc.NonPredSize = FalseBBI.NonPredSize;
851 return true;
852}
853
Kyle Butt9b6d99b2016-07-27 20:19:33 +0000854/// ValidDiamond - Returns true if the 'true' and 'false' blocks (along
855/// with their common predecessor) forms a valid diamond shape for ifcvt.
Kyle Butt6262ca32016-08-24 21:34:24 +0000856bool IfConverter::ValidDiamond(
857 BBInfo &TrueBBI, BBInfo &FalseBBI,
858 unsigned &Dups1, unsigned &Dups2,
859 BBInfo &TrueBBICalc, BBInfo &FalseBBICalc) const {
Kyle Butt9b6d99b2016-07-27 20:19:33 +0000860 Dups1 = Dups2 = 0;
861 if (TrueBBI.IsBeingAnalyzed || TrueBBI.IsDone ||
862 FalseBBI.IsBeingAnalyzed || FalseBBI.IsDone)
863 return false;
864
865 MachineBasicBlock *TT = TrueBBI.TrueBB;
866 MachineBasicBlock *FT = FalseBBI.TrueBB;
867
868 if (!TT && blockAlwaysFallThrough(TrueBBI))
Matthias Braun08f47042016-08-17 02:52:01 +0000869 TT = getNextBlock(*TrueBBI.BB);
Kyle Butt9b6d99b2016-07-27 20:19:33 +0000870 if (!FT && blockAlwaysFallThrough(FalseBBI))
Matthias Braun08f47042016-08-17 02:52:01 +0000871 FT = getNextBlock(*FalseBBI.BB);
Kyle Butt9b6d99b2016-07-27 20:19:33 +0000872 if (TT != FT)
873 return false;
874 if (!TT && (TrueBBI.IsBrAnalyzable || FalseBBI.IsBrAnalyzable))
875 return false;
876 if (TrueBBI.BB->pred_size() > 1 || FalseBBI.BB->pred_size() > 1)
877 return false;
878
879 // FIXME: Allow true block to have an early exit?
Kyle Butt6262ca32016-08-24 21:34:24 +0000880 if (TrueBBI.FalseBB || FalseBBI.FalseBB)
Kyle Butt9b6d99b2016-07-27 20:19:33 +0000881 return false;
882
883 // Count duplicate instructions at the beginning and end of the true and
884 // false blocks.
Kyle Butt6262ca32016-08-24 21:34:24 +0000885 // Skip unconditional branches only if we are considering an analyzable
886 // diamond. Otherwise the branches must be the same.
887 bool SkipUnconditionalBranches =
888 TrueBBI.IsBrAnalyzable && FalseBBI.IsBrAnalyzable;
Kyle Butt9b6d99b2016-07-27 20:19:33 +0000889 MachineBasicBlock::iterator TIB = TrueBBI.BB->begin();
890 MachineBasicBlock::iterator FIB = FalseBBI.BB->begin();
891 MachineBasicBlock::iterator TIE = TrueBBI.BB->end();
892 MachineBasicBlock::iterator FIE = FalseBBI.BB->end();
Kyle Butt6262ca32016-08-24 21:34:24 +0000893 if(!CountDuplicatedInstructions(TIB, FIB, TIE, FIE, Dups1, Dups2,
894 *TrueBBI.BB, *FalseBBI.BB,
895 SkipUnconditionalBranches))
896 return false;
897
898 TrueBBICalc.BB = TrueBBI.BB;
899 FalseBBICalc.BB = FalseBBI.BB;
900 if (!RescanInstructions(TIB, FIB, TIE, FIE, TrueBBICalc, FalseBBICalc))
901 return false;
902 // The size is used to decide whether to if-convert, and the shared portions
903 // are subtracted off. Because of the subtraction, we just use the size that
904 // was calculated by the original ScanInstructions, as it is correct.
905 TrueBBICalc.NonPredSize = TrueBBI.NonPredSize;
906 FalseBBICalc.NonPredSize = FalseBBI.NonPredSize;
Evan Cheng51eb2c32007-06-18 08:37:25 +0000907 return true;
Evan Chengbe9859e2007-06-07 02:12:15 +0000908}
909
Kyle Butt54bf3ce2016-08-06 01:52:34 +0000910/// AnalyzeBranches - Look at the branches at the end of a block to determine if
911/// the block is predicable.
912void IfConverter::AnalyzeBranches(BBInfo &BBI) {
Evan Cheng4dd31a72007-06-11 22:26:22 +0000913 if (BBI.IsDone)
914 return;
915
Craig Topperc0196b12014-04-14 00:51:57 +0000916 BBI.TrueBB = BBI.FalseBB = nullptr;
Evan Cheng4dd31a72007-06-11 22:26:22 +0000917 BBI.BrCond.clear();
918 BBI.IsBrAnalyzable =
Jacques Pienaar71c30a12016-07-15 14:41:04 +0000919 !TII->analyzeBranch(*BBI.BB, BBI.TrueBB, BBI.FalseBB, BBI.BrCond);
Kyle Butta8c73712016-08-24 21:34:27 +0000920 SmallVector<MachineOperand, 4> RevCond(BBI.BrCond.begin(), BBI.BrCond.end());
921 BBI.IsBrReversible = (RevCond.size() == 0) ||
922 !TII->ReverseBranchCondition(RevCond);
Craig Topperc0196b12014-04-14 00:51:57 +0000923 BBI.HasFallThrough = BBI.IsBrAnalyzable && BBI.FalseBB == nullptr;
Evan Cheng4dd31a72007-06-11 22:26:22 +0000924
925 if (BBI.BrCond.size()) {
926 // No false branch. This BB must end with a conditional branch and a
927 // fallthrough.
928 if (!BBI.FalseBB)
Bob Wilson81051442010-06-15 22:18:54 +0000929 BBI.FalseBB = findFalseBlock(BBI.BB, BBI.TrueBB);
Evan Chengb9bff582009-06-15 21:24:34 +0000930 if (!BBI.FalseBB) {
931 // Malformed bcc? True and false blocks are the same?
932 BBI.IsUnpredicable = true;
Evan Chengb9bff582009-06-15 21:24:34 +0000933 }
Evan Cheng4dd31a72007-06-11 22:26:22 +0000934 }
Kyle Butt54bf3ce2016-08-06 01:52:34 +0000935}
Evan Cheng4dd31a72007-06-11 22:26:22 +0000936
Kyle Butt54bf3ce2016-08-06 01:52:34 +0000937/// ScanInstructions - Scan all the instructions in the block to determine if
938/// the block is predicable. In most cases, that means all the instructions
939/// in the block are isPredicable(). Also checks if the block contains any
940/// instruction which can clobber a predicate (e.g. condition code register).
941/// If so, the block is not predicable unless it's the last instruction.
942void IfConverter::ScanInstructions(BBInfo &BBI,
943 MachineBasicBlock::iterator &Begin,
Kyle Butt6262ca32016-08-24 21:34:24 +0000944 MachineBasicBlock::iterator &End,
945 bool BranchUnpredicable) const {
Kyle Butt54bf3ce2016-08-06 01:52:34 +0000946 if (BBI.IsDone || BBI.IsUnpredicable)
947 return;
948
949 bool AlreadyPredicated = !BBI.Predicate.empty();
950
Evan Cheng4dd31a72007-06-11 22:26:22 +0000951 BBI.NonPredSize = 0;
Bob Wilsonefd360c2010-10-26 00:02:21 +0000952 BBI.ExtraCost = 0;
Evan Chengdebf9c52010-11-03 00:45:17 +0000953 BBI.ExtraCost2 = 0;
Evan Cheng4dd31a72007-06-11 22:26:22 +0000954 BBI.ClobbersPred = false;
Matthias Braunb1e05582016-08-17 02:51:59 +0000955 for (MachineInstr &MI : make_range(Begin, End)) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000956 if (MI.isDebugValue())
Jim Grosbacha1e08fb2010-06-04 23:01:26 +0000957 continue;
958
Justin Lebar7dba2e02016-04-15 01:38:41 +0000959 // It's unsafe to duplicate convergent instructions in this context, so set
960 // BBI.CannotBeCopied to true if MI is convergent. To see why, consider the
961 // following CFG, which is subject to our "simple" transformation.
962 //
963 // BB0 // if (c1) goto BB1; else goto BB2;
964 // / \
965 // BB1 |
966 // | BB2 // if (c2) goto TBB; else goto FBB;
967 // | / |
968 // | / |
969 // TBB |
970 // | |
971 // | FBB
972 // |
973 // exit
974 //
975 // Suppose we want to move TBB's contents up into BB1 and BB2 (in BB1 they'd
976 // be unconditional, and in BB2, they'd be predicated upon c2), and suppose
977 // TBB contains a convergent instruction. This is safe iff doing so does
978 // not add a control-flow dependency to the convergent instruction -- i.e.,
979 // it's safe iff the set of control flows that leads us to the convergent
980 // instruction does not get smaller after the transformation.
981 //
982 // Originally we executed TBB if c1 || c2. After the transformation, there
983 // are two copies of TBB's instructions. We get to the first if c1, and we
984 // get to the second if !c1 && c2.
985 //
986 // There are clearly fewer ways to satisfy the condition "c1" than
987 // "c1 || c2". Since we've shrunk the set of control flows which lead to
988 // our convergent instruction, the transformation is unsafe.
989 if (MI.isNotDuplicable() || MI.isConvergent())
Evan Cheng23402fc2007-06-15 21:18:05 +0000990 BBI.CannotBeCopied = true;
991
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000992 bool isPredicated = TII->isPredicated(MI);
993 bool isCondBr = BBI.IsBrAnalyzable && MI.isConditionalBranch();
Evan Cheng4dd31a72007-06-11 22:26:22 +0000994
Kyle Butt6262ca32016-08-24 21:34:24 +0000995 if (BranchUnpredicable && MI.isBranch()) {
996 BBI.IsUnpredicable = true;
997 return;
998 }
999
Joey Goulya5153cb2013-09-09 14:21:49 +00001000 // A conditional branch is not predicable, but it may be eliminated.
1001 if (isCondBr)
1002 continue;
1003
1004 if (!isPredicated) {
1005 BBI.NonPredSize++;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001006 unsigned ExtraPredCost = TII->getPredicationCost(MI);
1007 unsigned NumCycles = SchedModel.computeInstrLatency(&MI, false);
Joey Goulya5153cb2013-09-09 14:21:49 +00001008 if (NumCycles > 1)
1009 BBI.ExtraCost += NumCycles-1;
1010 BBI.ExtraCost2 += ExtraPredCost;
1011 } else if (!AlreadyPredicated) {
1012 // FIXME: This instruction is already predicated before the
1013 // if-conversion pass. It's probably something like a conditional move.
1014 // Mark this block unpredicable for now.
1015 BBI.IsUnpredicable = true;
1016 return;
Evan Cheng96c14572007-07-06 23:24:39 +00001017 }
Evan Cheng4dd31a72007-06-11 22:26:22 +00001018
1019 if (BBI.ClobbersPred && !isPredicated) {
1020 // Predicate modification instruction should end the block (except for
1021 // already predicated instructions and end of block branches).
Evan Cheng4dd31a72007-06-11 22:26:22 +00001022 // Predicate may have been modified, the subsequent (currently)
Evan Cheng96c14572007-07-06 23:24:39 +00001023 // unpredicated instructions cannot be correctly predicated.
Evan Cheng4dd31a72007-06-11 22:26:22 +00001024 BBI.IsUnpredicable = true;
1025 return;
1026 }
1027
Evan Chengfbc73092007-07-10 17:50:43 +00001028 // FIXME: Make use of PredDefs? e.g. ADDC, SUBC sets predicates but are
1029 // still potentially predicable.
1030 std::vector<MachineOperand> PredDefs;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001031 if (TII->DefinesPredicate(MI, PredDefs))
Evan Cheng4dd31a72007-06-11 22:26:22 +00001032 BBI.ClobbersPred = true;
1033
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001034 if (!TII->isPredicable(MI)) {
Evan Cheng4dd31a72007-06-11 22:26:22 +00001035 BBI.IsUnpredicable = true;
1036 return;
1037 }
1038 }
1039}
1040
Matthias Braun2c931792016-08-17 02:51:57 +00001041/// Determine if the block is a suitable candidate to be predicated by the
1042/// specified predicate.
Kyle Butt6262ca32016-08-24 21:34:24 +00001043/// @param BBI BBInfo for the block to check
1044/// @param Pred Predicate array for the branch that leads to BBI
1045/// @param isTriangle true if the Analysis is for a triangle
1046/// @param RevBranch true if Reverse(Pred) leads to BBI (e.g. BBI is the false
1047/// case
1048/// @param hasCommonTail true if BBI shares a tail with a sibling block that
1049/// contains any instruction that would make the block unpredicable.
Evan Cheng4dd31a72007-06-11 22:26:22 +00001050bool IfConverter::FeasibilityAnalysis(BBInfo &BBI,
Owen Anderson4f6bf042008-08-14 22:49:33 +00001051 SmallVectorImpl<MachineOperand> &Pred,
Kyle Butt6262ca32016-08-24 21:34:24 +00001052 bool isTriangle, bool RevBranch,
1053 bool hasCommonTail) {
Evan Cheng3a51c852007-06-16 09:34:52 +00001054 // If the block is dead or unpredicable, then it cannot be predicated.
Kyle Butt6262ca32016-08-24 21:34:24 +00001055 // Two blocks may share a common unpredicable tail, but this doesn't prevent
1056 // them from being if-converted. The non-shared portion is assumed to have
1057 // been checked
1058 if (BBI.IsDone || (BBI.IsUnpredicable && !hasCommonTail))
Evan Cheng4dd31a72007-06-11 22:26:22 +00001059 return false;
1060
Ahmed Bougacha7173b662015-03-21 01:23:15 +00001061 // If it is already predicated but we couldn't analyze its terminator, the
1062 // latter might fallthrough, but we can't determine where to.
1063 // Conservatively avoid if-converting again.
1064 if (BBI.Predicate.size() && !BBI.IsBrAnalyzable)
1065 return false;
1066
Quentin Colombetbdab2272013-07-24 20:20:37 +00001067 // If it is already predicated, check if the new predicate subsumes
1068 // its predicate.
1069 if (BBI.Predicate.size() && !TII->SubsumesPredicate(Pred, BBI.Predicate))
Evan Cheng4dd31a72007-06-11 22:26:22 +00001070 return false;
1071
Kyle Butt6262ca32016-08-24 21:34:24 +00001072 if (!hasCommonTail && BBI.BrCond.size()) {
Evan Cheng4dd31a72007-06-11 22:26:22 +00001073 if (!isTriangle)
1074 return false;
1075
Bob Wilson2371f4f2009-05-13 23:25:24 +00001076 // Test predicate subsumption.
Owen Anderson4f6bf042008-08-14 22:49:33 +00001077 SmallVector<MachineOperand, 4> RevPred(Pred.begin(), Pred.end());
1078 SmallVector<MachineOperand, 4> Cond(BBI.BrCond.begin(), BBI.BrCond.end());
Evan Cheng4dd31a72007-06-11 22:26:22 +00001079 if (RevBranch) {
1080 if (TII->ReverseBranchCondition(Cond))
1081 return false;
1082 }
1083 if (TII->ReverseBranchCondition(RevPred) ||
1084 !TII->SubsumesPredicate(Cond, RevPred))
1085 return false;
1086 }
1087
1088 return true;
1089}
1090
Matthias Braun2c931792016-08-17 02:51:57 +00001091/// Analyze the structure of the sub-CFG starting from the specified block.
1092/// Record its successors and whether it looks like an if-conversion candidate.
Justin Lebar3a7bc572016-02-22 17:51:28 +00001093void IfConverter::AnalyzeBlock(
Matthias Braun08f47042016-08-17 02:52:01 +00001094 MachineBasicBlock &MBB, std::vector<std::unique_ptr<IfcvtToken>> &Tokens) {
Akira Hatanaka14348aa2015-06-24 20:34:35 +00001095 struct BBState {
Matthias Braun08f47042016-08-17 02:52:01 +00001096 BBState(MachineBasicBlock &MBB) : MBB(&MBB), SuccsAnalyzed(false) {}
Akira Hatanaka14348aa2015-06-24 20:34:35 +00001097 MachineBasicBlock *MBB;
Evan Chengf5e53a52007-05-16 02:00:57 +00001098
Akira Hatanaka14348aa2015-06-24 20:34:35 +00001099 /// This flag is true if MBB's successors have been analyzed.
1100 bool SuccsAnalyzed;
1101 };
Evan Cheng0f745da2007-05-18 00:20:58 +00001102
Akira Hatanaka14348aa2015-06-24 20:34:35 +00001103 // Push MBB to the stack.
1104 SmallVector<BBState, 16> BBStack(1, MBB);
Evan Cheng4dd31a72007-06-11 22:26:22 +00001105
Akira Hatanaka14348aa2015-06-24 20:34:35 +00001106 while (!BBStack.empty()) {
1107 BBState &State = BBStack.back();
1108 MachineBasicBlock *BB = State.MBB;
1109 BBInfo &BBI = BBAnalysis[BB->getNumber()];
Evan Cheng4dd31a72007-06-11 22:26:22 +00001110
Akira Hatanaka14348aa2015-06-24 20:34:35 +00001111 if (!State.SuccsAnalyzed) {
1112 if (BBI.IsAnalyzed || BBI.IsBeingAnalyzed) {
1113 BBStack.pop_back();
1114 continue;
1115 }
Evan Cheng9030b982007-06-06 10:16:17 +00001116
Akira Hatanaka14348aa2015-06-24 20:34:35 +00001117 BBI.BB = BB;
1118 BBI.IsBeingAnalyzed = true;
Evan Cheng4dd31a72007-06-11 22:26:22 +00001119
Kyle Butt54bf3ce2016-08-06 01:52:34 +00001120 AnalyzeBranches(BBI);
1121 MachineBasicBlock::iterator Begin = BBI.BB->begin();
1122 MachineBasicBlock::iterator End = BBI.BB->end();
1123 ScanInstructions(BBI, Begin, End);
Evan Chengb9bff582009-06-15 21:24:34 +00001124
Akira Hatanaka14348aa2015-06-24 20:34:35 +00001125 // Unanalyzable or ends with fallthrough or unconditional branch, or if is
1126 // not considered for ifcvt anymore.
1127 if (!BBI.IsBrAnalyzable || BBI.BrCond.empty() || BBI.IsDone) {
1128 BBI.IsBeingAnalyzed = false;
1129 BBI.IsAnalyzed = true;
1130 BBStack.pop_back();
1131 continue;
1132 }
Evan Cheng4dd31a72007-06-11 22:26:22 +00001133
Akira Hatanaka14348aa2015-06-24 20:34:35 +00001134 // Do not ifcvt if either path is a back edge to the entry block.
1135 if (BBI.TrueBB == BB || BBI.FalseBB == BB) {
1136 BBI.IsBeingAnalyzed = false;
1137 BBI.IsAnalyzed = true;
1138 BBStack.pop_back();
1139 continue;
1140 }
Evan Cheng4dd31a72007-06-11 22:26:22 +00001141
Akira Hatanaka14348aa2015-06-24 20:34:35 +00001142 // Do not ifcvt if true and false fallthrough blocks are the same.
1143 if (!BBI.FalseBB) {
1144 BBI.IsBeingAnalyzed = false;
1145 BBI.IsAnalyzed = true;
1146 BBStack.pop_back();
1147 continue;
1148 }
Evan Cheng7783f822007-06-08 09:36:04 +00001149
Akira Hatanaka14348aa2015-06-24 20:34:35 +00001150 // Push the False and True blocks to the stack.
1151 State.SuccsAnalyzed = true;
Matthias Braun08f47042016-08-17 02:52:01 +00001152 BBStack.push_back(*BBI.FalseBB);
1153 BBStack.push_back(*BBI.TrueBB);
Akira Hatanaka14348aa2015-06-24 20:34:35 +00001154 continue;
1155 }
Benjamin Kramer2016f0e2010-09-29 22:38:50 +00001156
Akira Hatanaka14348aa2015-06-24 20:34:35 +00001157 BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
1158 BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
Jakub Staszak15e5b742011-08-03 22:34:43 +00001159
Akira Hatanaka14348aa2015-06-24 20:34:35 +00001160 if (TrueBBI.IsDone && FalseBBI.IsDone) {
1161 BBI.IsBeingAnalyzed = false;
1162 BBI.IsAnalyzed = true;
1163 BBStack.pop_back();
1164 continue;
1165 }
1166
1167 SmallVector<MachineOperand, 4>
1168 RevCond(BBI.BrCond.begin(), BBI.BrCond.end());
1169 bool CanRevCond = !TII->ReverseBranchCondition(RevCond);
1170
1171 unsigned Dups = 0;
1172 unsigned Dups2 = 0;
1173 bool TNeedSub = !TrueBBI.Predicate.empty();
1174 bool FNeedSub = !FalseBBI.Predicate.empty();
1175 bool Enqueued = false;
1176
1177 BranchProbability Prediction = MBPI->getEdgeProbability(BB, TrueBBI.BB);
1178
Kyle Butt6262ca32016-08-24 21:34:24 +00001179 if (CanRevCond) {
1180 BBInfo TrueBBICalc, FalseBBICalc;
Kyle Butta8c73712016-08-24 21:34:27 +00001181 auto feasibleDiamond = [&]() {
1182 bool MeetsSize = MeetIfcvtSizeLimit(
1183 *TrueBBI.BB, (TrueBBICalc.NonPredSize - (Dups + Dups2) +
1184 TrueBBICalc.ExtraCost), TrueBBICalc.ExtraCost2,
1185 *FalseBBI.BB, (FalseBBICalc.NonPredSize - (Dups + Dups2) +
1186 FalseBBICalc.ExtraCost), FalseBBICalc.ExtraCost2,
1187 Prediction);
1188 bool TrueFeasible = FeasibilityAnalysis(TrueBBI, BBI.BrCond,
1189 /* IsTriangle */ false, /* RevCond */ false,
1190 /* hasCommonTail */ true);
1191 bool FalseFeasible = FeasibilityAnalysis(FalseBBI, RevCond,
1192 /* IsTriangle */ false, /* RevCond */ false,
1193 /* hasCommonTail */ true);
1194 return MeetsSize && TrueFeasible && FalseFeasible;
1195 };
1196
Kyle Butt6262ca32016-08-24 21:34:24 +00001197 if (ValidDiamond(TrueBBI, FalseBBI, Dups, Dups2,
Kyle Butta8c73712016-08-24 21:34:27 +00001198 TrueBBICalc, FalseBBICalc)) {
1199 if (feasibleDiamond()) {
1200 // Diamond:
1201 // EBB
1202 // / \_
1203 // | |
1204 // TBB FBB
1205 // \ /
1206 // TailBB
1207 // Note TailBB can be empty.
1208 Tokens.push_back(llvm::make_unique<IfcvtToken>(
1209 BBI, ICDiamond, TNeedSub | FNeedSub, Dups, Dups2,
1210 (bool) TrueBBICalc.ClobbersPred, (bool) FalseBBICalc.ClobbersPred));
1211 Enqueued = true;
1212 }
1213 } else if (ValidForkedDiamond(TrueBBI, FalseBBI, Dups, Dups2,
1214 TrueBBICalc, FalseBBICalc)) {
1215 if (feasibleDiamond()) {
1216 // ForkedDiamond:
1217 // if TBB and FBB have a common tail that includes their conditional
1218 // branch instructions, then we can If Convert this pattern.
1219 // EBB
1220 // _/ \_
1221 // | |
1222 // TBB FBB
1223 // / \ / \
1224 // FalseBB TrueBB FalseBB
1225 //
1226 Tokens.push_back(llvm::make_unique<IfcvtToken>(
1227 BBI, ICForkedDiamond, TNeedSub | FNeedSub, Dups, Dups2,
1228 (bool) TrueBBICalc.ClobbersPred, (bool) FalseBBICalc.ClobbersPred));
1229 Enqueued = true;
1230 }
Kyle Butt6262ca32016-08-24 21:34:24 +00001231 }
Evan Cheng3a51c852007-06-16 09:34:52 +00001232 }
1233
Akira Hatanaka14348aa2015-06-24 20:34:35 +00001234 if (ValidTriangle(TrueBBI, FalseBBI, false, Dups, Prediction) &&
1235 MeetIfcvtSizeLimit(*TrueBBI.BB, TrueBBI.NonPredSize + TrueBBI.ExtraCost,
1236 TrueBBI.ExtraCost2, Prediction) &&
1237 FeasibilityAnalysis(TrueBBI, BBI.BrCond, true)) {
1238 // Triangle:
1239 // EBB
1240 // | \_
1241 // | |
1242 // | TBB
1243 // | /
1244 // FBB
Justin Lebar3a7bc572016-02-22 17:51:28 +00001245 Tokens.push_back(
1246 llvm::make_unique<IfcvtToken>(BBI, ICTriangle, TNeedSub, Dups));
Akira Hatanaka14348aa2015-06-24 20:34:35 +00001247 Enqueued = true;
1248 }
1249
1250 if (ValidTriangle(TrueBBI, FalseBBI, true, Dups, Prediction) &&
1251 MeetIfcvtSizeLimit(*TrueBBI.BB, TrueBBI.NonPredSize + TrueBBI.ExtraCost,
1252 TrueBBI.ExtraCost2, Prediction) &&
1253 FeasibilityAnalysis(TrueBBI, BBI.BrCond, true, true)) {
Justin Lebar3a7bc572016-02-22 17:51:28 +00001254 Tokens.push_back(
1255 llvm::make_unique<IfcvtToken>(BBI, ICTriangleRev, TNeedSub, Dups));
Akira Hatanaka14348aa2015-06-24 20:34:35 +00001256 Enqueued = true;
1257 }
1258
1259 if (ValidSimple(TrueBBI, Dups, Prediction) &&
1260 MeetIfcvtSizeLimit(*TrueBBI.BB, TrueBBI.NonPredSize + TrueBBI.ExtraCost,
1261 TrueBBI.ExtraCost2, Prediction) &&
1262 FeasibilityAnalysis(TrueBBI, BBI.BrCond)) {
1263 // Simple (split, no rejoin):
1264 // EBB
1265 // | \_
1266 // | |
1267 // | TBB---> exit
1268 // |
1269 // FBB
Justin Lebar3a7bc572016-02-22 17:51:28 +00001270 Tokens.push_back(
1271 llvm::make_unique<IfcvtToken>(BBI, ICSimple, TNeedSub, Dups));
Akira Hatanaka14348aa2015-06-24 20:34:35 +00001272 Enqueued = true;
1273 }
1274
1275 if (CanRevCond) {
1276 // Try the other path...
1277 if (ValidTriangle(FalseBBI, TrueBBI, false, Dups,
1278 Prediction.getCompl()) &&
1279 MeetIfcvtSizeLimit(*FalseBBI.BB,
1280 FalseBBI.NonPredSize + FalseBBI.ExtraCost,
1281 FalseBBI.ExtraCost2, Prediction.getCompl()) &&
1282 FeasibilityAnalysis(FalseBBI, RevCond, true)) {
Justin Lebar3a7bc572016-02-22 17:51:28 +00001283 Tokens.push_back(llvm::make_unique<IfcvtToken>(BBI, ICTriangleFalse,
1284 FNeedSub, Dups));
Akira Hatanaka14348aa2015-06-24 20:34:35 +00001285 Enqueued = true;
1286 }
1287
1288 if (ValidTriangle(FalseBBI, TrueBBI, true, Dups,
1289 Prediction.getCompl()) &&
1290 MeetIfcvtSizeLimit(*FalseBBI.BB,
1291 FalseBBI.NonPredSize + FalseBBI.ExtraCost,
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001292 FalseBBI.ExtraCost2, Prediction.getCompl()) &&
Evan Cheng3a51c852007-06-16 09:34:52 +00001293 FeasibilityAnalysis(FalseBBI, RevCond, true, true)) {
Justin Lebar3a7bc572016-02-22 17:51:28 +00001294 Tokens.push_back(
1295 llvm::make_unique<IfcvtToken>(BBI, ICTriangleFRev, FNeedSub, Dups));
Akira Hatanaka14348aa2015-06-24 20:34:35 +00001296 Enqueued = true;
1297 }
1298
1299 if (ValidSimple(FalseBBI, Dups, Prediction.getCompl()) &&
1300 MeetIfcvtSizeLimit(*FalseBBI.BB,
1301 FalseBBI.NonPredSize + FalseBBI.ExtraCost,
1302 FalseBBI.ExtraCost2, Prediction.getCompl()) &&
1303 FeasibilityAnalysis(FalseBBI, RevCond)) {
Justin Lebar3a7bc572016-02-22 17:51:28 +00001304 Tokens.push_back(
1305 llvm::make_unique<IfcvtToken>(BBI, ICSimpleFalse, FNeedSub, Dups));
Akira Hatanaka14348aa2015-06-24 20:34:35 +00001306 Enqueued = true;
1307 }
Evan Cheng3a51c852007-06-16 09:34:52 +00001308 }
1309
Akira Hatanaka14348aa2015-06-24 20:34:35 +00001310 BBI.IsEnqueued = Enqueued;
1311 BBI.IsBeingAnalyzed = false;
1312 BBI.IsAnalyzed = true;
1313 BBStack.pop_back();
Evan Chengf5e53a52007-05-16 02:00:57 +00001314 }
Evan Cheng2e82cef2007-05-18 18:14:37 +00001315}
1316
Matthias Braun2c931792016-08-17 02:51:57 +00001317/// Analyze all blocks and find entries for all if-conversion candidates.
Justin Lebar3a7bc572016-02-22 17:51:28 +00001318void IfConverter::AnalyzeBlocks(
1319 MachineFunction &MF, std::vector<std::unique_ptr<IfcvtToken>> &Tokens) {
Matthias Braunb1e05582016-08-17 02:51:59 +00001320 for (MachineBasicBlock &MBB : MF)
Matthias Braun08f47042016-08-17 02:52:01 +00001321 AnalyzeBlock(MBB, Tokens);
Evan Cheng905a8f42007-05-30 19:49:19 +00001322
Evan Cheng20e05992007-06-01 00:12:12 +00001323 // Sort to favor more complex ifcvt scheme.
Evan Cheng3a51c852007-06-16 09:34:52 +00001324 std::stable_sort(Tokens.begin(), Tokens.end(), IfcvtTokenCmp);
Evan Chengf5e53a52007-05-16 02:00:57 +00001325}
1326
Matthias Braun2c931792016-08-17 02:51:57 +00001327/// Returns true either if ToMBB is the next block after MBB or that all the
1328/// intervening blocks are empty (given MBB can fall through to its next block).
Matthias Braun08f47042016-08-17 02:52:01 +00001329static bool canFallThroughTo(MachineBasicBlock &MBB, MachineBasicBlock &ToMBB) {
1330 MachineFunction::iterator PI = MBB.getIterator();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001331 MachineFunction::iterator I = std::next(PI);
Matthias Braun08f47042016-08-17 02:52:01 +00001332 MachineFunction::iterator TI = ToMBB.getIterator();
1333 MachineFunction::iterator E = MBB.getParent()->end();
Evan Chengf128bdc2010-06-16 07:35:02 +00001334 while (I != TI) {
1335 // Check isSuccessor to avoid case where the next block is empty, but
1336 // it's not a successor.
Duncan P. N. Exon Smith5ae59392015-10-09 19:13:58 +00001337 if (I == E || !I->empty() || !PI->isSuccessor(&*I))
Evan Cheng312b7232007-06-04 06:47:22 +00001338 return false;
Evan Chengf128bdc2010-06-16 07:35:02 +00001339 PI = I++;
1340 }
Evan Cheng312b7232007-06-04 06:47:22 +00001341 return true;
Evan Chenge26c0912007-05-21 22:22:58 +00001342}
1343
Matthias Braun2c931792016-08-17 02:51:57 +00001344/// Invalidate predecessor BB info so it would be re-analyzed to determine if it
1345/// can be if-converted. If predecessor is already enqueued, dequeue it!
Matthias Braun08f47042016-08-17 02:52:01 +00001346void IfConverter::InvalidatePreds(MachineBasicBlock &MBB) {
1347 for (const MachineBasicBlock *Predecessor : MBB.predecessors()) {
Saleem Abdulrasoolf158ca32014-08-09 17:21:29 +00001348 BBInfo &PBBI = BBAnalysis[Predecessor->getNumber()];
Matthias Braun08f47042016-08-17 02:52:01 +00001349 if (PBBI.IsDone || PBBI.BB == &MBB)
Evan Cheng9fc56c02007-06-14 23:13:19 +00001350 continue;
Evan Chengadd97762007-06-14 23:34:09 +00001351 PBBI.IsAnalyzed = false;
1352 PBBI.IsEnqueued = false;
Evan Chengd0e66912007-05-23 07:23:16 +00001353 }
1354}
1355
Matthias Braun2c931792016-08-17 02:51:57 +00001356/// Inserts an unconditional branch from \p MBB to \p ToMBB.
Matthias Braun08f47042016-08-17 02:52:01 +00001357static void InsertUncondBranch(MachineBasicBlock &MBB, MachineBasicBlock &ToMBB,
Evan Chengc2237ce2007-05-29 22:31:16 +00001358 const TargetInstrInfo *TII) {
Stuart Hastings0125b642010-06-17 22:43:56 +00001359 DebugLoc dl; // FIXME: this is nowhere
Dan Gohman14714cb2008-08-22 16:07:55 +00001360 SmallVector<MachineOperand, 0> NoCond;
Matthias Braun08f47042016-08-17 02:52:01 +00001361 TII->InsertBranch(MBB, &ToMBB, nullptr, NoCond, dl);
Evan Chengc2237ce2007-05-29 22:31:16 +00001362}
1363
Matthias Braun2c931792016-08-17 02:51:57 +00001364/// Remove true / false edges if either / both are no longer successors.
Evan Cheng288f1542007-06-08 22:01:07 +00001365void IfConverter::RemoveExtraEdges(BBInfo &BBI) {
Craig Topperc0196b12014-04-14 00:51:57 +00001366 MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
Owen Anderson4f6bf042008-08-14 22:49:33 +00001367 SmallVector<MachineOperand, 4> Cond;
Jacques Pienaar71c30a12016-07-15 14:41:04 +00001368 if (!TII->analyzeBranch(*BBI.BB, TBB, FBB, Cond))
Evan Cheng0598b2d2007-06-18 22:44:57 +00001369 BBI.BB->CorrectExtraCFGEdges(TBB, FBB, !Cond.empty());
Evan Cheng288f1542007-06-08 22:01:07 +00001370}
1371
Matthias Braund616ccc2013-10-11 19:04:37 +00001372/// Behaves like LiveRegUnits::StepForward() but also adds implicit uses to all
Jonas Paulsson196986c2016-08-03 05:46:35 +00001373/// values defined in MI which are also live/used by MI.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001374static void UpdatePredRedefs(MachineInstr &MI, LivePhysRegs &Redefs) {
Jonas Paulsson196986c2016-08-03 05:46:35 +00001375 const TargetRegisterInfo *TRI = MI.getParent()->getParent()
1376 ->getSubtarget().getRegisterInfo();
1377
1378 // Before stepping forward past MI, remember which regs were live
1379 // before MI. This is needed to set the Undef flag only when reg is
1380 // dead.
1381 SparseSet<unsigned> LiveBeforeMI;
1382 LiveBeforeMI.setUniverse(TRI->getNumRegs());
Matthias Braunb1e05582016-08-17 02:51:59 +00001383 for (unsigned Reg : Redefs)
Jonas Paulsson196986c2016-08-03 05:46:35 +00001384 LiveBeforeMI.insert(Reg);
1385
Pete Cooper7605e372015-05-05 20:14:22 +00001386 SmallVector<std::pair<unsigned, const MachineOperand*>, 4> Clobbers;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001387 Redefs.stepForward(MI, Clobbers);
Matthias Braund616ccc2013-10-11 19:04:37 +00001388
Pete Cooper7605e372015-05-05 20:14:22 +00001389 // Now add the implicit uses for each of the clobbered values.
Matthias Braun08f47042016-08-17 02:52:01 +00001390 for (auto Clobber : Clobbers) {
Pete Cooper7605e372015-05-05 20:14:22 +00001391 // FIXME: Const cast here is nasty, but better than making StepForward
1392 // take a mutable instruction instead of const.
Matthias Braun08f47042016-08-17 02:52:01 +00001393 unsigned Reg = Clobber.first;
1394 MachineOperand &Op = const_cast<MachineOperand&>(*Clobber.second);
Pete Cooper27483912015-05-06 22:51:04 +00001395 MachineInstr *OpMI = Op.getParent();
Pete Cooper7605e372015-05-05 20:14:22 +00001396 MachineInstrBuilder MIB(*OpMI->getParent()->getParent(), OpMI);
Pete Cooperce9ad752015-05-05 22:09:41 +00001397 if (Op.isRegMask()) {
1398 // First handle regmasks. They clobber any entries in the mask which
1399 // means that we need a def for those registers.
Matthias Braun08f47042016-08-17 02:52:01 +00001400 if (LiveBeforeMI.count(Reg))
1401 MIB.addReg(Reg, RegState::Implicit);
Pete Cooperce9ad752015-05-05 22:09:41 +00001402
1403 // We also need to add an implicit def of this register for the later
1404 // use to read from.
1405 // For the register allocator to have allocated a register clobbered
1406 // by the call which is used later, it must be the case that
1407 // the call doesn't return.
Matthias Braun08f47042016-08-17 02:52:01 +00001408 MIB.addReg(Reg, RegState::Implicit | RegState::Define);
Pete Cooperce9ad752015-05-05 22:09:41 +00001409 continue;
1410 }
1411 assert(Op.isReg() && "Register operand required");
Pete Cooper27483912015-05-06 22:51:04 +00001412 if (Op.isDead()) {
1413 // If we found a dead def, but it needs to be live, then remove the dead
1414 // flag.
1415 if (Redefs.contains(Op.getReg()))
1416 Op.setIsDead(false);
1417 }
Matthias Braun08f47042016-08-17 02:52:01 +00001418 if (LiveBeforeMI.count(Reg))
1419 MIB.addReg(Reg, RegState::Implicit);
Matthias Braund616ccc2013-10-11 19:04:37 +00001420 }
1421}
1422
Matthias Braun2c931792016-08-17 02:51:57 +00001423/// Remove kill flags from operands with a registers in the \p DontKill set.
Juergen Ributzka310034e2013-12-14 06:52:56 +00001424static void RemoveKills(MachineInstr &MI, const LivePhysRegs &DontKill) {
Duncan P. N. Exon Smithf9ab4162016-02-27 17:05:33 +00001425 for (MIBundleOperands O(MI); O.isValid(); ++O) {
Matthias Braund616ccc2013-10-11 19:04:37 +00001426 if (!O->isReg() || !O->isKill())
1427 continue;
Juergen Ributzka310034e2013-12-14 06:52:56 +00001428 if (DontKill.contains(O->getReg()))
Matthias Braund616ccc2013-10-11 19:04:37 +00001429 O->setIsKill(false);
1430 }
1431}
1432
Matthias Braun2c931792016-08-17 02:51:57 +00001433/// Walks a range of machine instructions and removes kill flags for registers
1434/// in the \p DontKill set.
Matthias Braund616ccc2013-10-11 19:04:37 +00001435static void RemoveKills(MachineBasicBlock::iterator I,
1436 MachineBasicBlock::iterator E,
Juergen Ributzka310034e2013-12-14 06:52:56 +00001437 const LivePhysRegs &DontKill,
Matthias Braund616ccc2013-10-11 19:04:37 +00001438 const MCRegisterInfo &MCRI) {
Matthias Braunb1e05582016-08-17 02:51:59 +00001439 for (MachineInstr &MI : make_range(I, E))
1440 RemoveKills(MI, DontKill);
Evan Chengf128bdc2010-06-16 07:35:02 +00001441}
1442
Matthias Braun2c931792016-08-17 02:51:57 +00001443/// If convert a simple (split, no rejoin) sub-CFG.
Evan Cheng3a51c852007-06-16 09:34:52 +00001444bool IfConverter::IfConvertSimple(BBInfo &BBI, IfcvtKind Kind) {
Evan Chenge26c0912007-05-21 22:22:58 +00001445 BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
1446 BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
1447 BBInfo *CvtBBI = &TrueBBI;
1448 BBInfo *NextBBI = &FalseBBI;
Evan Chengd0e66912007-05-23 07:23:16 +00001449
Owen Anderson4f6bf042008-08-14 22:49:33 +00001450 SmallVector<MachineOperand, 4> Cond(BBI.BrCond.begin(), BBI.BrCond.end());
Evan Cheng3a51c852007-06-16 09:34:52 +00001451 if (Kind == ICSimpleFalse)
Evan Cheng4dcf1e82007-06-01 20:29:21 +00001452 std::swap(CvtBBI, NextBBI);
Evan Cheng23402fc2007-06-15 21:18:05 +00001453
Matthias Braun08f47042016-08-17 02:52:01 +00001454 MachineBasicBlock &CvtMBB = *CvtBBI->BB;
1455 MachineBasicBlock &NextMBB = *NextBBI->BB;
Evan Cheng51eb2c32007-06-18 08:37:25 +00001456 if (CvtBBI->IsDone ||
Matthias Braun08f47042016-08-17 02:52:01 +00001457 (CvtBBI->CannotBeCopied && CvtMBB.pred_size() > 1)) {
Evan Cheng23402fc2007-06-15 21:18:05 +00001458 // Something has changed. It's no longer safe to predicate this block.
Evan Cheng23402fc2007-06-15 21:18:05 +00001459 BBI.IsAnalyzed = false;
1460 CvtBBI->IsAnalyzed = false;
1461 return false;
Evan Cheng4dcf1e82007-06-01 20:29:21 +00001462 }
Evan Chengd0e66912007-05-23 07:23:16 +00001463
Matthias Braun08f47042016-08-17 02:52:01 +00001464 if (CvtMBB.hasAddressTaken())
Evan Cheng8b8e8d82013-05-05 18:03:49 +00001465 // Conservatively abort if-conversion if BB's address is taken.
1466 return false;
1467
Evan Cheng3a51c852007-06-16 09:34:52 +00001468 if (Kind == ICSimpleFalse)
Dan Gohman97d95d62008-10-21 03:29:32 +00001469 if (TII->ReverseBranchCondition(Cond))
Craig Topperee4dab52012-02-05 08:31:47 +00001470 llvm_unreachable("Unable to reverse branch condition!");
Evan Cheng23402fc2007-06-15 21:18:05 +00001471
Bob Wilson45814342010-06-19 05:33:57 +00001472 // Initialize liveins to the first BB. These are potentiall redefined by
Evan Chengf128bdc2010-06-16 07:35:02 +00001473 // predicated instructions.
Andrew Trick276dd452013-10-14 20:45:17 +00001474 Redefs.init(TRI);
Matthias Braun08f47042016-08-17 02:52:01 +00001475 Redefs.addLiveIns(CvtMBB);
1476 Redefs.addLiveIns(NextMBB);
Matthias Braund616ccc2013-10-11 19:04:37 +00001477
1478 // Compute a set of registers which must not be killed by instructions in
1479 // BB1: This is everything live-in to BB2.
Andrew Trick276dd452013-10-14 20:45:17 +00001480 DontKill.init(TRI);
Matthias Braun08f47042016-08-17 02:52:01 +00001481 DontKill.addLiveIns(NextMBB);
Evan Chengf128bdc2010-06-16 07:35:02 +00001482
Matthias Braun08f47042016-08-17 02:52:01 +00001483 if (CvtMBB.pred_size() > 1) {
Evan Cheng92fb5452007-06-15 07:36:12 +00001484 BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
Bob Wilson2371f4f2009-05-13 23:25:24 +00001485 // Copy instructions in the true block, predicate them, and add them to
Evan Cheng92fb5452007-06-15 07:36:12 +00001486 // the entry block.
Andrew Trick276dd452013-10-14 20:45:17 +00001487 CopyAndPredicateBlock(BBI, *CvtBBI, Cond);
Hal Finkel95081bf2013-04-10 22:05:25 +00001488
1489 // RemoveExtraEdges won't work if the block has an unanalyzable branch, so
1490 // explicitly remove CvtBBI as a successor.
Matthias Braun08f47042016-08-17 02:52:01 +00001491 BBI.BB->removeSuccessor(&CvtMBB, true);
Evan Cheng92fb5452007-06-15 07:36:12 +00001492 } else {
Matthias Braun08f47042016-08-17 02:52:01 +00001493 RemoveKills(CvtMBB.begin(), CvtMBB.end(), DontKill, *TRI);
1494 PredicateBlock(*CvtBBI, CvtMBB.end(), Cond);
Evan Chenge26c0912007-05-21 22:22:58 +00001495
Evan Cheng92fb5452007-06-15 07:36:12 +00001496 // Merge converted block into entry block.
1497 BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
1498 MergeBlocks(BBI, *CvtBBI);
1499 }
Evan Chengbe9859e2007-06-07 02:12:15 +00001500
Evan Chenge4ec9182007-06-06 02:08:52 +00001501 bool IterIfcvt = true;
Matthias Braun08f47042016-08-17 02:52:01 +00001502 if (!canFallThroughTo(*BBI.BB, NextMBB)) {
1503 InsertUncondBranch(*BBI.BB, NextMBB, TII);
Evan Cheng2117d1f2007-06-09 01:03:43 +00001504 BBI.HasFallThrough = false;
Evan Cheng7783f822007-06-08 09:36:04 +00001505 // Now ifcvt'd block will look like this:
1506 // BB:
1507 // ...
1508 // t, f = cmp
1509 // if t op
1510 // b BBf
1511 //
1512 // We cannot further ifcvt this block because the unconditional branch
1513 // will have to be predicated on the new condition, that will not be
1514 // available if cmp executes.
1515 IterIfcvt = false;
Evan Chenge4ec9182007-06-06 02:08:52 +00001516 }
Evan Chenge26c0912007-05-21 22:22:58 +00001517
Evan Cheng288f1542007-06-08 22:01:07 +00001518 RemoveExtraEdges(BBI);
1519
Evan Chengd0e66912007-05-23 07:23:16 +00001520 // Update block info. BB can be iteratively if-converted.
Evan Cheng4dd31a72007-06-11 22:26:22 +00001521 if (!IterIfcvt)
1522 BBI.IsDone = true;
Matthias Braun08f47042016-08-17 02:52:01 +00001523 InvalidatePreds(*BBI.BB);
Evan Cheng4dd31a72007-06-11 22:26:22 +00001524 CvtBBI->IsDone = true;
Evan Chenge26c0912007-05-21 22:22:58 +00001525
1526 // FIXME: Must maintain LiveIns.
Evan Chenge26c0912007-05-21 22:22:58 +00001527 return true;
1528}
1529
Matthias Braun2c931792016-08-17 02:51:57 +00001530/// If convert a triangle sub-CFG.
Evan Cheng3a51c852007-06-16 09:34:52 +00001531bool IfConverter::IfConvertTriangle(BBInfo &BBI, IfcvtKind Kind) {
Evan Chengd0e66912007-05-23 07:23:16 +00001532 BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
Evan Cheng2117d1f2007-06-09 01:03:43 +00001533 BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
1534 BBInfo *CvtBBI = &TrueBBI;
1535 BBInfo *NextBBI = &FalseBBI;
Stuart Hastings0125b642010-06-17 22:43:56 +00001536 DebugLoc dl; // FIXME: this is nowhere
Evan Cheng2117d1f2007-06-09 01:03:43 +00001537
Owen Anderson4f6bf042008-08-14 22:49:33 +00001538 SmallVector<MachineOperand, 4> Cond(BBI.BrCond.begin(), BBI.BrCond.end());
Evan Cheng3a51c852007-06-16 09:34:52 +00001539 if (Kind == ICTriangleFalse || Kind == ICTriangleFRev)
Evan Cheng2117d1f2007-06-09 01:03:43 +00001540 std::swap(CvtBBI, NextBBI);
Evan Cheng23402fc2007-06-15 21:18:05 +00001541
Matthias Braun08f47042016-08-17 02:52:01 +00001542 MachineBasicBlock &CvtMBB = *CvtBBI->BB;
1543 MachineBasicBlock &NextMBB = *NextBBI->BB;
Evan Cheng51eb2c32007-06-18 08:37:25 +00001544 if (CvtBBI->IsDone ||
Matthias Braun08f47042016-08-17 02:52:01 +00001545 (CvtBBI->CannotBeCopied && CvtMBB.pred_size() > 1)) {
Evan Cheng23402fc2007-06-15 21:18:05 +00001546 // Something has changed. It's no longer safe to predicate this block.
Evan Cheng23402fc2007-06-15 21:18:05 +00001547 BBI.IsAnalyzed = false;
1548 CvtBBI->IsAnalyzed = false;
1549 return false;
Evan Cheng2117d1f2007-06-09 01:03:43 +00001550 }
Evan Cheng23402fc2007-06-15 21:18:05 +00001551
Matthias Braun08f47042016-08-17 02:52:01 +00001552 if (CvtMBB.hasAddressTaken())
Evan Cheng8b8e8d82013-05-05 18:03:49 +00001553 // Conservatively abort if-conversion if BB's address is taken.
1554 return false;
1555
Evan Cheng3a51c852007-06-16 09:34:52 +00001556 if (Kind == ICTriangleFalse || Kind == ICTriangleFRev)
Dan Gohman97d95d62008-10-21 03:29:32 +00001557 if (TII->ReverseBranchCondition(Cond))
Craig Topperee4dab52012-02-05 08:31:47 +00001558 llvm_unreachable("Unable to reverse branch condition!");
Evan Cheng23402fc2007-06-15 21:18:05 +00001559
Evan Cheng3a51c852007-06-16 09:34:52 +00001560 if (Kind == ICTriangleRev || Kind == ICTriangleFRev) {
Dan Gohman97d95d62008-10-21 03:29:32 +00001561 if (ReverseBranchCondition(*CvtBBI)) {
1562 // BB has been changed, modify its predecessors (except for this
1563 // one) so they don't get ifcvt'ed based on bad intel.
Matthias Braun08f47042016-08-17 02:52:01 +00001564 for (MachineBasicBlock *PBB : CvtMBB.predecessors()) {
Dan Gohman97d95d62008-10-21 03:29:32 +00001565 if (PBB == BBI.BB)
1566 continue;
1567 BBInfo &PBBI = BBAnalysis[PBB->getNumber()];
1568 if (PBBI.IsEnqueued) {
1569 PBBI.IsAnalyzed = false;
1570 PBBI.IsEnqueued = false;
1571 }
Evan Chengadd97762007-06-14 23:34:09 +00001572 }
Evan Cheng9acfa7b2007-06-12 23:54:05 +00001573 }
1574 }
Evan Cheng6a2cf072007-06-01 07:41:07 +00001575
Bob Wilson45814342010-06-19 05:33:57 +00001576 // Initialize liveins to the first BB. These are potentially redefined by
Evan Chengf128bdc2010-06-16 07:35:02 +00001577 // predicated instructions.
Andrew Trick276dd452013-10-14 20:45:17 +00001578 Redefs.init(TRI);
Matthias Braun08f47042016-08-17 02:52:01 +00001579 Redefs.addLiveIns(CvtMBB);
1580 Redefs.addLiveIns(NextMBB);
Evan Chengf128bdc2010-06-16 07:35:02 +00001581
Andrew Trick276dd452013-10-14 20:45:17 +00001582 DontKill.clear();
1583
Craig Topperc0196b12014-04-14 00:51:57 +00001584 bool HasEarlyExit = CvtBBI->FalseBB != nullptr;
Cong Houd97c1002015-12-01 05:29:22 +00001585 BranchProbability CvtNext, CvtFalse, BBNext, BBCvt;
Saleem Abdulrasoolf158ca32014-08-09 17:21:29 +00001586
Manman Renb6819182014-01-29 23:18:47 +00001587 if (HasEarlyExit) {
Matthias Braun08f47042016-08-17 02:52:01 +00001588 // Get probabilities before modifying CvtMBB and BBI.BB.
1589 CvtNext = MBPI->getEdgeProbability(&CvtMBB, &NextMBB);
1590 CvtFalse = MBPI->getEdgeProbability(&CvtMBB, CvtBBI->FalseBB);
1591 BBNext = MBPI->getEdgeProbability(BBI.BB, &NextMBB);
1592 BBCvt = MBPI->getEdgeProbability(BBI.BB, &CvtMBB);
Manman Renb6819182014-01-29 23:18:47 +00001593 }
Saleem Abdulrasoolf158ca32014-08-09 17:21:29 +00001594
Matthias Braun08f47042016-08-17 02:52:01 +00001595 if (CvtMBB.pred_size() > 1) {
Evan Cheng92fb5452007-06-15 07:36:12 +00001596 BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
Bob Wilson2371f4f2009-05-13 23:25:24 +00001597 // Copy instructions in the true block, predicate them, and add them to
Evan Cheng92fb5452007-06-15 07:36:12 +00001598 // the entry block.
Andrew Trick276dd452013-10-14 20:45:17 +00001599 CopyAndPredicateBlock(BBI, *CvtBBI, Cond, true);
Hal Finkel95081bf2013-04-10 22:05:25 +00001600
1601 // RemoveExtraEdges won't work if the block has an unanalyzable branch, so
1602 // explicitly remove CvtBBI as a successor.
Matthias Braun08f47042016-08-17 02:52:01 +00001603 BBI.BB->removeSuccessor(&CvtMBB, true);
Evan Cheng92fb5452007-06-15 07:36:12 +00001604 } else {
1605 // Predicate the 'true' block after removing its branch.
Matthias Braun08f47042016-08-17 02:52:01 +00001606 CvtBBI->NonPredSize -= TII->RemoveBranch(CvtMBB);
1607 PredicateBlock(*CvtBBI, CvtMBB.end(), Cond);
Evan Chenge26c0912007-05-21 22:22:58 +00001608
Evan Cheng0598b2d2007-06-18 22:44:57 +00001609 // Now merge the entry of the triangle with the true block.
1610 BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
Bob Wilson1e5da552010-06-29 00:55:23 +00001611 MergeBlocks(BBI, *CvtBBI, false);
Evan Cheng0598b2d2007-06-18 22:44:57 +00001612 }
1613
Evan Cheng312b7232007-06-04 06:47:22 +00001614 // If 'true' block has a 'false' successor, add an exit branch to it.
Evan Cheng6e4babe2007-06-05 01:31:40 +00001615 if (HasEarlyExit) {
Owen Anderson4f6bf042008-08-14 22:49:33 +00001616 SmallVector<MachineOperand, 4> RevCond(CvtBBI->BrCond.begin(),
1617 CvtBBI->BrCond.end());
Evan Cheng6a2cf072007-06-01 07:41:07 +00001618 if (TII->ReverseBranchCondition(RevCond))
Craig Topperee4dab52012-02-05 08:31:47 +00001619 llvm_unreachable("Unable to reverse branch condition!");
Hans Wennborg1dbaf672015-12-01 03:49:42 +00001620
Cong Houd97c1002015-12-01 05:29:22 +00001621 // Update the edge probability for both CvtBBI->FalseBB and NextBBI.
Matthias Braun08f47042016-08-17 02:52:01 +00001622 // NewNext = New_Prob(BBI.BB, NextMBB) =
1623 // Prob(BBI.BB, NextMBB) +
1624 // Prob(BBI.BB, CvtMBB) * Prob(CvtMBB, NextMBB)
Cong Houd97c1002015-12-01 05:29:22 +00001625 // NewFalse = New_Prob(BBI.BB, CvtBBI->FalseBB) =
Matthias Braun08f47042016-08-17 02:52:01 +00001626 // Prob(BBI.BB, CvtMBB) * Prob(CvtMBB, CvtBBI->FalseBB)
1627 auto NewTrueBB = getNextBlock(*BBI.BB);
Cong Houd97c1002015-12-01 05:29:22 +00001628 auto NewNext = BBNext + BBCvt * CvtNext;
David Majnemer42531262016-08-12 03:55:06 +00001629 auto NewTrueBBIter = find(BBI.BB->successors(), NewTrueBB);
Cong Houcb07d702015-12-01 21:50:20 +00001630 if (NewTrueBBIter != BBI.BB->succ_end())
1631 BBI.BB->setSuccProbability(NewTrueBBIter, NewNext);
Cong Houd97c1002015-12-01 05:29:22 +00001632
1633 auto NewFalse = BBCvt * CvtFalse;
1634 TII->InsertBranch(*BBI.BB, CvtBBI->FalseBB, nullptr, RevCond, dl);
1635 BBI.BB->addSuccessor(CvtBBI->FalseBB, NewFalse);
Evan Cheng92fb5452007-06-15 07:36:12 +00001636 }
Evan Cheng7783f822007-06-08 09:36:04 +00001637
1638 // Merge in the 'false' block if the 'false' block has no other
Bob Wilson2371f4f2009-05-13 23:25:24 +00001639 // predecessors. Otherwise, add an unconditional branch to 'false'.
Evan Cheng17aad8162007-06-05 00:07:37 +00001640 bool FalseBBDead = false;
Evan Chenge4ec9182007-06-06 02:08:52 +00001641 bool IterIfcvt = true;
Matthias Braun08f47042016-08-17 02:52:01 +00001642 bool isFallThrough = canFallThroughTo(*BBI.BB, NextMBB);
Evan Chengd3f3f0a2007-06-07 08:13:00 +00001643 if (!isFallThrough) {
1644 // Only merge them if the true block does not fallthrough to the false
1645 // block. By not merging them, we make it possible to iteratively
1646 // ifcvt the blocks.
Evan Cheng51eb2c32007-06-18 08:37:25 +00001647 if (!HasEarlyExit &&
Matthias Braun08f47042016-08-17 02:52:01 +00001648 NextMBB.pred_size() == 1 && !NextBBI->HasFallThrough &&
1649 !NextMBB.hasAddressTaken()) {
Evan Cheng2117d1f2007-06-09 01:03:43 +00001650 MergeBlocks(BBI, *NextBBI);
Evan Chengd3f3f0a2007-06-07 08:13:00 +00001651 FalseBBDead = true;
Evan Chengd3f3f0a2007-06-07 08:13:00 +00001652 } else {
Matthias Braun08f47042016-08-17 02:52:01 +00001653 InsertUncondBranch(*BBI.BB, NextMBB, TII);
Evan Cheng2117d1f2007-06-09 01:03:43 +00001654 BBI.HasFallThrough = false;
Evan Chengd3f3f0a2007-06-07 08:13:00 +00001655 }
Evan Cheng7783f822007-06-08 09:36:04 +00001656 // Mixed predicated and unpredicated code. This cannot be iteratively
1657 // predicated.
1658 IterIfcvt = false;
Evan Chenge4ec9182007-06-06 02:08:52 +00001659 }
Evan Chenge26c0912007-05-21 22:22:58 +00001660
Evan Cheng288f1542007-06-08 22:01:07 +00001661 RemoveExtraEdges(BBI);
Evan Chenge26c0912007-05-21 22:22:58 +00001662
Evan Chengd0e66912007-05-23 07:23:16 +00001663 // Update block info. BB can be iteratively if-converted.
Bob Wilson81051442010-06-15 22:18:54 +00001664 if (!IterIfcvt)
Evan Cheng4dd31a72007-06-11 22:26:22 +00001665 BBI.IsDone = true;
Matthias Braun08f47042016-08-17 02:52:01 +00001666 InvalidatePreds(*BBI.BB);
Evan Cheng4dd31a72007-06-11 22:26:22 +00001667 CvtBBI->IsDone = true;
Evan Cheng17aad8162007-06-05 00:07:37 +00001668 if (FalseBBDead)
Evan Cheng4dd31a72007-06-11 22:26:22 +00001669 NextBBI->IsDone = true;
Evan Chenge26c0912007-05-21 22:22:58 +00001670
1671 // FIXME: Must maintain LiveIns.
Evan Chenge26c0912007-05-21 22:22:58 +00001672 return true;
Evan Chengf5e53a52007-05-16 02:00:57 +00001673}
1674
Kyle Butta8c73712016-08-24 21:34:27 +00001675/// Common code shared between diamond conversions.
1676/// \p BBI, \p TrueBBI, and \p FalseBBI form the diamond shape.
1677/// \p NumDups1 - number of shared instructions at the beginning of \p TrueBBI
1678/// and FalseBBI
1679/// \p NumDups2 - number of shared instructions at the end of \p TrueBBI
1680/// and \p FalseBBI
1681/// \p RemoveTrueBranch - Remove the branch of the true block before predicating
1682/// Only false for unanalyzable fallthrough cases.
1683/// \p RemoveFalseBranch - Remove the branch of the false block before
1684/// predicating Only false for unanalyzable fallthrough
1685/// cases.
1686/// \p MergeAddEdges - Add successor edges when merging blocks. Only false for
1687/// unanalyzable fallthrough
1688bool IfConverter::IfConvertDiamondCommon(
1689 BBInfo &BBI, BBInfo &TrueBBI, BBInfo &FalseBBI,
1690 unsigned NumDups1, unsigned NumDups2,
1691 bool TClobbersPred, bool FClobbersPred,
1692 bool RemoveTrueBranch, bool RemoveFalseBranch,
1693 bool MergeAddEdges) {
Evan Chenge26c0912007-05-21 22:22:58 +00001694
Evan Cheng51eb2c32007-06-18 08:37:25 +00001695 if (TrueBBI.IsDone || FalseBBI.IsDone ||
Kyle Butta8c73712016-08-24 21:34:27 +00001696 TrueBBI.BB->pred_size() > 1 || FalseBBI.BB->pred_size() > 1) {
Evan Cheng51eb2c32007-06-18 08:37:25 +00001697 // Something has changed. It's no longer safe to predicate these blocks.
1698 BBI.IsAnalyzed = false;
1699 TrueBBI.IsAnalyzed = false;
1700 FalseBBI.IsAnalyzed = false;
1701 return false;
Evan Chenge26c0912007-05-21 22:22:58 +00001702 }
Evan Cheng51eb2c32007-06-18 08:37:25 +00001703
Evan Cheng8b8e8d82013-05-05 18:03:49 +00001704 if (TrueBBI.BB->hasAddressTaken() || FalseBBI.BB->hasAddressTaken())
1705 // Conservatively abort if-conversion if either BB has its address taken.
1706 return false;
1707
Bob Wilson1e5da552010-06-29 00:55:23 +00001708 // Put the predicated instructions from the 'true' block before the
1709 // instructions from the 'false' block, unless the true block would clobber
1710 // the predicate, in which case, do the opposite.
Evan Cheng79484222007-06-05 23:46:14 +00001711 BBInfo *BBI1 = &TrueBBI;
1712 BBInfo *BBI2 = &FalseBBI;
Owen Anderson4f6bf042008-08-14 22:49:33 +00001713 SmallVector<MachineOperand, 4> RevCond(BBI.BrCond.begin(), BBI.BrCond.end());
Dan Gohman97d95d62008-10-21 03:29:32 +00001714 if (TII->ReverseBranchCondition(RevCond))
Craig Topperee4dab52012-02-05 08:31:47 +00001715 llvm_unreachable("Unable to reverse branch condition!");
Owen Anderson4f6bf042008-08-14 22:49:33 +00001716 SmallVector<MachineOperand, 4> *Cond1 = &BBI.BrCond;
1717 SmallVector<MachineOperand, 4> *Cond2 = &RevCond;
Evan Cheng30565992007-06-06 00:57:55 +00001718
Evan Cheng3a51c852007-06-16 09:34:52 +00001719 // Figure out the more profitable ordering.
1720 bool DoSwap = false;
Kyle Butt6262ca32016-08-24 21:34:24 +00001721 if (TClobbersPred && !FClobbersPred)
Evan Cheng3a51c852007-06-16 09:34:52 +00001722 DoSwap = true;
Kyle Butt6262ca32016-08-24 21:34:24 +00001723 else if (TClobbersPred == FClobbersPred) {
Evan Cheng0598b2d2007-06-18 22:44:57 +00001724 if (TrueBBI.NonPredSize > FalseBBI.NonPredSize)
Evan Cheng3a51c852007-06-16 09:34:52 +00001725 DoSwap = true;
Evan Cheng3a51c852007-06-16 09:34:52 +00001726 }
1727 if (DoSwap) {
Evan Cheng30565992007-06-06 00:57:55 +00001728 std::swap(BBI1, BBI2);
1729 std::swap(Cond1, Cond2);
Kyle Butta8c73712016-08-24 21:34:27 +00001730 std::swap(RemoveTrueBranch, RemoveFalseBranch);
Evan Cheng30565992007-06-06 00:57:55 +00001731 }
Evan Cheng79484222007-06-05 23:46:14 +00001732
Evan Cheng51eb2c32007-06-18 08:37:25 +00001733 // Remove the conditional branch from entry to the blocks.
1734 BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
1735
Matthias Braun08f47042016-08-17 02:52:01 +00001736 MachineBasicBlock &MBB1 = *BBI1->BB;
1737 MachineBasicBlock &MBB2 = *BBI2->BB;
1738
Krzysztof Parzyszeka003b762016-08-11 18:42:06 +00001739 // Initialize the Redefs:
1740 // - BB2 live-in regs need implicit uses before being redefined by BB1
1741 // instructions.
1742 // - BB1 live-out regs need implicit uses before being redefined by BB2
1743 // instructions. We start with BB1 live-ins so we have the live-out regs
1744 // after tracking the BB1 instructions.
Andrew Trick276dd452013-10-14 20:45:17 +00001745 Redefs.init(TRI);
Matthias Braun08f47042016-08-17 02:52:01 +00001746 Redefs.addLiveIns(MBB1);
1747 Redefs.addLiveIns(MBB2);
Evan Chengf128bdc2010-06-16 07:35:02 +00001748
Evan Cheng51eb2c32007-06-18 08:37:25 +00001749 // Remove the duplicated instructions at the beginnings of both paths.
Jim Grosbach412800d2010-06-14 21:30:32 +00001750 // Skip dbg_value instructions
Matthias Braun08f47042016-08-17 02:52:01 +00001751 MachineBasicBlock::iterator DI1 = MBB1.getFirstNonDebugInstr();
1752 MachineBasicBlock::iterator DI2 = MBB2.getFirstNonDebugInstr();
Evan Cheng51eb2c32007-06-18 08:37:25 +00001753 BBI1->NonPredSize -= NumDups1;
1754 BBI2->NonPredSize -= NumDups1;
Jim Grosbachee6e29a2010-06-28 20:26:00 +00001755
1756 // Skip past the dups on each side separately since there may be
1757 // differing dbg_value entries.
1758 for (unsigned i = 0; i < NumDups1; ++DI1) {
1759 if (!DI1->isDebugValue())
1760 ++i;
1761 }
Jim Grosbachc34befc2010-06-25 23:05:46 +00001762 while (NumDups1 != 0) {
Evan Cheng51eb2c32007-06-18 08:37:25 +00001763 ++DI2;
Jim Grosbachee6e29a2010-06-28 20:26:00 +00001764 if (!DI2->isDebugValue())
1765 --NumDups1;
Evan Cheng51eb2c32007-06-18 08:37:25 +00001766 }
Evan Chengf128bdc2010-06-16 07:35:02 +00001767
Matthias Braund616ccc2013-10-11 19:04:37 +00001768 // Compute a set of registers which must not be killed by instructions in BB1:
1769 // This is everything used+live in BB2 after the duplicated instructions. We
1770 // can compute this set by simulating liveness backwards from the end of BB2.
Andrew Trick276dd452013-10-14 20:45:17 +00001771 DontKill.init(TRI);
Matthias Braun08f47042016-08-17 02:52:01 +00001772 for (const MachineInstr &MI :
1773 make_range(MBB2.rbegin(), MachineBasicBlock::reverse_iterator(DI2)))
Matthias Braunb1e05582016-08-17 02:51:59 +00001774 DontKill.stepBackward(MI);
Matthias Braund616ccc2013-10-11 19:04:37 +00001775
Matthias Braun08f47042016-08-17 02:52:01 +00001776 for (const MachineInstr &MI : make_range(MBB1.begin(), DI1)) {
Pete Cooper7605e372015-05-05 20:14:22 +00001777 SmallVector<std::pair<unsigned, const MachineOperand*>, 4> IgnoredClobbers;
Matthias Braunb1e05582016-08-17 02:51:59 +00001778 Redefs.stepForward(MI, IgnoredClobbers);
Matthias Braund616ccc2013-10-11 19:04:37 +00001779 }
Matthias Braun08f47042016-08-17 02:52:01 +00001780 BBI.BB->splice(BBI.BB->end(), &MBB1, MBB1.begin(), DI1);
1781 MBB2.erase(MBB2.begin(), DI2);
Evan Cheng51eb2c32007-06-18 08:37:25 +00001782
Kyle Butta8c73712016-08-24 21:34:27 +00001783 if (RemoveTrueBranch)
1784 BBI1->NonPredSize -= TII->RemoveBranch(*BBI1->BB);
Krzysztof Parzyszek2451c482016-01-20 13:14:52 +00001785 // Remove duplicated instructions.
Matthias Braun08f47042016-08-17 02:52:01 +00001786 DI1 = MBB1.end();
Jim Grosbach412800d2010-06-14 21:30:32 +00001787 for (unsigned i = 0; i != NumDups2; ) {
1788 // NumDups2 only counted non-dbg_value instructions, so this won't
1789 // run off the head of the list.
Matthias Braun08f47042016-08-17 02:52:01 +00001790 assert(DI1 != MBB1.begin());
Evan Cheng51eb2c32007-06-18 08:37:25 +00001791 --DI1;
Jim Grosbach412800d2010-06-14 21:30:32 +00001792 // skip dbg_value instructions
1793 if (!DI1->isDebugValue())
1794 ++i;
1795 }
Matthias Braun08f47042016-08-17 02:52:01 +00001796 MBB1.erase(DI1, MBB1.end());
Evan Cheng79484222007-06-05 23:46:14 +00001797
Matthias Braund616ccc2013-10-11 19:04:37 +00001798 // Kill flags in the true block for registers living into the false block
1799 // must be removed.
Matthias Braun08f47042016-08-17 02:52:01 +00001800 RemoveKills(MBB1.begin(), MBB1.end(), DontKill, *TRI);
Matthias Braund616ccc2013-10-11 19:04:37 +00001801
Kyle Butta8c73712016-08-24 21:34:27 +00001802 // Remove 'false' block branch, and find the last instruction to predicate.
1803 // Save the debug location.
1804 if (RemoveFalseBranch)
1805 BBI2->NonPredSize -= TII->RemoveBranch(*BBI2->BB);
1806 DI2 = BBI2->BB->end();
Evan Cheng51eb2c32007-06-18 08:37:25 +00001807 while (NumDups2 != 0) {
Jim Grosbach412800d2010-06-14 21:30:32 +00001808 // NumDups2 only counted non-dbg_value instructions, so this won't
1809 // run off the head of the list.
Matthias Braun08f47042016-08-17 02:52:01 +00001810 assert(DI2 != MBB2.begin());
Evan Cheng51eb2c32007-06-18 08:37:25 +00001811 --DI2;
Jim Grosbach412800d2010-06-14 21:30:32 +00001812 // skip dbg_value instructions
1813 if (!DI2->isDebugValue())
1814 --NumDups2;
Evan Cheng51eb2c32007-06-18 08:37:25 +00001815 }
Evan Cheng4266a792011-12-19 22:01:30 +00001816
1817 // Remember which registers would later be defined by the false block.
1818 // This allows us not to predicate instructions in the true block that would
1819 // later be re-defined. That is, rather than
1820 // subeq r0, r1, #1
1821 // addne r0, r1, #1
1822 // generate:
1823 // sub r0, r1, #1
1824 // addne r0, r1, #1
1825 SmallSet<unsigned, 4> RedefsByFalse;
1826 SmallSet<unsigned, 4> ExtUses;
Matthias Braun08f47042016-08-17 02:52:01 +00001827 if (TII->isProfitableToUnpredicate(MBB1, MBB2)) {
1828 for (const MachineInstr &FI : make_range(MBB2.begin(), DI2)) {
Matthias Braunb1e05582016-08-17 02:51:59 +00001829 if (FI.isDebugValue())
Evan Cheng4266a792011-12-19 22:01:30 +00001830 continue;
1831 SmallVector<unsigned, 4> Defs;
Matthias Braunb1e05582016-08-17 02:51:59 +00001832 for (const MachineOperand &MO : FI.operands()) {
Evan Cheng4266a792011-12-19 22:01:30 +00001833 if (!MO.isReg())
1834 continue;
1835 unsigned Reg = MO.getReg();
1836 if (!Reg)
1837 continue;
1838 if (MO.isDef()) {
1839 Defs.push_back(Reg);
1840 } else if (!RedefsByFalse.count(Reg)) {
1841 // These are defined before ctrl flow reach the 'false' instructions.
1842 // They cannot be modified by the 'true' instructions.
Chad Rosierabdb1d62013-05-22 23:17:36 +00001843 for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
1844 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +00001845 ExtUses.insert(*SubRegs);
Evan Cheng4266a792011-12-19 22:01:30 +00001846 }
1847 }
1848
Matthias Braunb1e05582016-08-17 02:51:59 +00001849 for (unsigned Reg : Defs) {
Evan Cheng4266a792011-12-19 22:01:30 +00001850 if (!ExtUses.count(Reg)) {
Chad Rosierabdb1d62013-05-22 23:17:36 +00001851 for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
1852 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +00001853 RedefsByFalse.insert(*SubRegs);
Evan Cheng4266a792011-12-19 22:01:30 +00001854 }
1855 }
1856 }
1857 }
1858
1859 // Predicate the 'true' block.
Matthias Braun08f47042016-08-17 02:52:01 +00001860 PredicateBlock(*BBI1, MBB1.end(), *Cond1, &RedefsByFalse);
Evan Cheng4266a792011-12-19 22:01:30 +00001861
Krzysztof Parzyszek2451c482016-01-20 13:14:52 +00001862 // After predicating BBI1, if there is a predicated terminator in BBI1 and
1863 // a non-predicated in BBI2, then we don't want to predicate the one from
1864 // BBI2. The reason is that if we merged these blocks, we would end up with
1865 // two predicated terminators in the same block.
Matthias Braun08f47042016-08-17 02:52:01 +00001866 if (!MBB2.empty() && (DI2 == MBB2.end())) {
1867 MachineBasicBlock::iterator BBI1T = MBB1.getFirstTerminator();
1868 MachineBasicBlock::iterator BBI2T = MBB2.getFirstTerminator();
1869 if (BBI1T != MBB1.end() && TII->isPredicated(*BBI1T) &&
1870 BBI2T != MBB2.end() && !TII->isPredicated(*BBI2T))
Krzysztof Parzyszek2451c482016-01-20 13:14:52 +00001871 --DI2;
1872 }
1873
Evan Cheng4266a792011-12-19 22:01:30 +00001874 // Predicate the 'false' block.
Andrew Trick276dd452013-10-14 20:45:17 +00001875 PredicateBlock(*BBI2, DI2, *Cond2);
Evan Cheng79484222007-06-05 23:46:14 +00001876
Evan Cheng0598b2d2007-06-18 22:44:57 +00001877 // Merge the true block into the entry of the diamond.
Kyle Butta8c73712016-08-24 21:34:27 +00001878 MergeBlocks(BBI, *BBI1, MergeAddEdges);
1879 MergeBlocks(BBI, *BBI2, MergeAddEdges);
1880 return true;
1881}
1882
1883/// If convert an almost-diamond sub-CFG where the true
1884/// and false blocks share a common tail.
1885bool IfConverter::IfConvertForkedDiamond(
1886 BBInfo &BBI, IfcvtKind Kind,
1887 unsigned NumDups1, unsigned NumDups2,
1888 bool TClobbersPred, bool FClobbersPred) {
1889 BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
1890 BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
1891
1892 // Save the debug location for later.
1893 DebugLoc dl;
1894 MachineBasicBlock::iterator TIE = TrueBBI.BB->getFirstTerminator();
1895 if (TIE != TrueBBI.BB->end())
1896 dl = TIE->getDebugLoc();
1897 // Removing branches from both blocks is safe, because we have already
1898 // determined that both blocks have the same branch instructions. The branch
1899 // will be added back at the end, unpredicated.
1900 if (!IfConvertDiamondCommon(
1901 BBI, TrueBBI, FalseBBI,
1902 NumDups1, NumDups2,
1903 TClobbersPred, FClobbersPred,
1904 /* RemoveTrueBranch */ true, /* RemoveFalseBranch */ true,
1905 /* MergeAddEdges */ true))
1906 return false;
1907
1908 // Add back the branch.
1909 // Debug location saved above when removing the branch from BBI2
1910 TII->InsertBranch(*BBI.BB, TrueBBI.TrueBB, TrueBBI.FalseBB,
1911 TrueBBI.BrCond, dl);
1912
1913 RemoveExtraEdges(BBI);
1914
1915 // Update block info.
1916 BBI.IsDone = TrueBBI.IsDone = FalseBBI.IsDone = true;
1917 InvalidatePreds(*BBI.BB);
1918
1919 // FIXME: Must maintain LiveIns.
1920 return true;
1921}
1922
1923/// If convert a diamond sub-CFG.
1924bool IfConverter::IfConvertDiamond(BBInfo &BBI, IfcvtKind Kind,
1925 unsigned NumDups1, unsigned NumDups2,
1926 bool TClobbersPred, bool FClobbersPred) {
1927 BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
1928 BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
1929 MachineBasicBlock *TailBB = TrueBBI.TrueBB;
1930
1931 // True block must fall through or end with an unanalyzable terminator.
1932 if (!TailBB) {
1933 if (blockAlwaysFallThrough(TrueBBI))
1934 TailBB = FalseBBI.TrueBB;
1935 assert((TailBB || !TrueBBI.IsBrAnalyzable) && "Unexpected!");
1936 }
1937
1938 if (!IfConvertDiamondCommon(
1939 BBI, TrueBBI, FalseBBI,
1940 NumDups1, NumDups2,
1941 TrueBBI.ClobbersPred, FalseBBI.ClobbersPred,
1942 /* RemoveTrueBranch */ TrueBBI.IsBrAnalyzable,
1943 /* RemoveFalseBranch */ FalseBBI.IsBrAnalyzable,
1944 /* MergeAddEdges */ TailBB == nullptr))
1945 return false;
Evan Cheng30565992007-06-06 00:57:55 +00001946
Bob Wilson2371f4f2009-05-13 23:25:24 +00001947 // If the if-converted block falls through or unconditionally branches into
1948 // the tail block, and the tail block does not have other predecessors, then
Evan Cheng51eb2c32007-06-18 08:37:25 +00001949 // fold the tail block in as well. Otherwise, unless it falls through to the
1950 // tail, add a unconditional branch to it.
1951 if (TailBB) {
Pete Cooper77c703f2011-11-04 23:49:14 +00001952 BBInfo &TailBBI = BBAnalysis[TailBB->getNumber()];
Evan Cheng8b8e8d82013-05-05 18:03:49 +00001953 bool CanMergeTail = !TailBBI.HasFallThrough &&
1954 !TailBBI.BB->hasAddressTaken();
Krzysztof Parzyszek2451c482016-01-20 13:14:52 +00001955 // The if-converted block can still have a predicated terminator
1956 // (e.g. a predicated return). If that is the case, we cannot merge
1957 // it with the tail block.
1958 MachineBasicBlock::const_iterator TI = BBI.BB->getFirstTerminator();
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001959 if (TI != BBI.BB->end() && TII->isPredicated(*TI))
Krzysztof Parzyszek2451c482016-01-20 13:14:52 +00001960 CanMergeTail = false;
Bob Wilson1e5da552010-06-29 00:55:23 +00001961 // There may still be a fall-through edge from BBI1 or BBI2 to TailBB;
1962 // check if there are any other predecessors besides those.
1963 unsigned NumPreds = TailBB->pred_size();
1964 if (NumPreds > 1)
1965 CanMergeTail = false;
1966 else if (NumPreds == 1 && CanMergeTail) {
1967 MachineBasicBlock::pred_iterator PI = TailBB->pred_begin();
Kyle Butta8c73712016-08-24 21:34:27 +00001968 if (*PI != TrueBBI.BB && *PI != FalseBBI.BB)
Bob Wilson1e5da552010-06-29 00:55:23 +00001969 CanMergeTail = false;
1970 }
1971 if (CanMergeTail) {
Evan Cheng0598b2d2007-06-18 22:44:57 +00001972 MergeBlocks(BBI, TailBBI);
Evan Cheng51eb2c32007-06-18 08:37:25 +00001973 TailBBI.IsDone = true;
1974 } else {
Cong Houd97c1002015-12-01 05:29:22 +00001975 BBI.BB->addSuccessor(TailBB, BranchProbability::getOne());
Matthias Braun08f47042016-08-17 02:52:01 +00001976 InsertUncondBranch(*BBI.BB, *TailBB, TII);
Evan Cheng0598b2d2007-06-18 22:44:57 +00001977 BBI.HasFallThrough = false;
Evan Cheng51eb2c32007-06-18 08:37:25 +00001978 }
Evan Chenge26c0912007-05-21 22:22:58 +00001979 }
1980
Bob Wilson1e5da552010-06-29 00:55:23 +00001981 // RemoveExtraEdges won't work if the block has an unanalyzable branch,
1982 // which can happen here if TailBB is unanalyzable and is merged, so
1983 // explicitly remove BBI1 and BBI2 as successors.
Kyle Butta8c73712016-08-24 21:34:27 +00001984 BBI.BB->removeSuccessor(TrueBBI.BB);
1985 BBI.BB->removeSuccessor(FalseBBI.BB, /* NormalizeSuccessProbs */ true);
Evan Cheng288f1542007-06-08 22:01:07 +00001986 RemoveExtraEdges(BBI);
1987
Evan Cheng79484222007-06-05 23:46:14 +00001988 // Update block info.
Evan Cheng4dd31a72007-06-11 22:26:22 +00001989 BBI.IsDone = TrueBBI.IsDone = FalseBBI.IsDone = true;
Matthias Braun08f47042016-08-17 02:52:01 +00001990 InvalidatePreds(*BBI.BB);
Evan Chenge26c0912007-05-21 22:22:58 +00001991
1992 // FIXME: Must maintain LiveIns.
Evan Chenge26c0912007-05-21 22:22:58 +00001993 return true;
Evan Chengf5e53a52007-05-16 02:00:57 +00001994}
1995
Duncan P. N. Exon Smith0490cde2016-06-30 23:04:51 +00001996static bool MaySpeculate(const MachineInstr &MI,
Matthias Braun07066cc2015-05-19 21:22:20 +00001997 SmallSet<unsigned, 4> &LaterRedefs) {
Evan Cheng4266a792011-12-19 22:01:30 +00001998 bool SawStore = true;
Duncan P. N. Exon Smith0490cde2016-06-30 23:04:51 +00001999 if (!MI.isSafeToMove(nullptr, SawStore))
Evan Cheng4266a792011-12-19 22:01:30 +00002000 return false;
2001
Matthias Braunb1e05582016-08-17 02:51:59 +00002002 for (const MachineOperand &MO : MI.operands()) {
Evan Cheng4266a792011-12-19 22:01:30 +00002003 if (!MO.isReg())
2004 continue;
2005 unsigned Reg = MO.getReg();
2006 if (!Reg)
2007 continue;
2008 if (MO.isDef() && !LaterRedefs.count(Reg))
2009 return false;
2010 }
2011
2012 return true;
2013}
2014
Matthias Braun2c931792016-08-17 02:51:57 +00002015/// Predicate instructions from the start of the block to the specified end with
2016/// the specified condition.
Evan Chengd0e66912007-05-23 07:23:16 +00002017void IfConverter::PredicateBlock(BBInfo &BBI,
Evan Cheng51eb2c32007-06-18 08:37:25 +00002018 MachineBasicBlock::iterator E,
Evan Chengf128bdc2010-06-16 07:35:02 +00002019 SmallVectorImpl<MachineOperand> &Cond,
Evan Cheng4266a792011-12-19 22:01:30 +00002020 SmallSet<unsigned, 4> *LaterRedefs) {
2021 bool AnyUnpred = false;
Craig Topperc0196b12014-04-14 00:51:57 +00002022 bool MaySpec = LaterRedefs != nullptr;
Matthias Braunb1e05582016-08-17 02:51:59 +00002023 for (MachineInstr &I : make_range(BBI.BB->begin(), E)) {
Duncan P. N. Exon Smith0490cde2016-06-30 23:04:51 +00002024 if (I.isDebugValue() || TII->isPredicated(I))
Evan Chengd0e66912007-05-23 07:23:16 +00002025 continue;
Evan Cheng4266a792011-12-19 22:01:30 +00002026 // It may be possible not to predicate an instruction if it's the 'true'
2027 // side of a diamond and the 'false' side may re-define the instruction's
2028 // defs.
Matthias Braun07066cc2015-05-19 21:22:20 +00002029 if (MaySpec && MaySpeculate(I, *LaterRedefs)) {
Evan Cheng4266a792011-12-19 22:01:30 +00002030 AnyUnpred = true;
2031 continue;
2032 }
2033 // If any instruction is predicated, then every instruction after it must
2034 // be predicated.
2035 MaySpec = false;
Duncan P. N. Exon Smith0490cde2016-06-30 23:04:51 +00002036 if (!TII->PredicateInstruction(I, Cond)) {
Torok Edwin08954aa2009-07-12 20:07:01 +00002037#ifndef NDEBUG
Duncan P. N. Exon Smith0490cde2016-06-30 23:04:51 +00002038 dbgs() << "Unable to predicate " << I << "!\n";
Torok Edwin08954aa2009-07-12 20:07:01 +00002039#endif
Craig Topperc0196b12014-04-14 00:51:57 +00002040 llvm_unreachable(nullptr);
Evan Chengaf716102007-05-16 21:54:37 +00002041 }
Evan Chengf128bdc2010-06-16 07:35:02 +00002042
Bob Wilson45814342010-06-19 05:33:57 +00002043 // If the predicated instruction now redefines a register as the result of
Evan Chengf128bdc2010-06-16 07:35:02 +00002044 // if-conversion, add an implicit kill.
Duncan P. N. Exon Smith0490cde2016-06-30 23:04:51 +00002045 UpdatePredRedefs(I, Redefs);
Evan Chengf5e53a52007-05-16 02:00:57 +00002046 }
Evan Chengd0e66912007-05-23 07:23:16 +00002047
Benjamin Kramer4f6ac162015-02-28 10:11:12 +00002048 BBI.Predicate.append(Cond.begin(), Cond.end());
Evan Chengdf1a4292007-06-08 19:17:12 +00002049
Evan Cheng92fb5452007-06-15 07:36:12 +00002050 BBI.IsAnalyzed = false;
2051 BBI.NonPredSize = 0;
2052
Dan Gohmand2d1ae12010-06-22 15:08:57 +00002053 ++NumIfConvBBs;
Evan Cheng4266a792011-12-19 22:01:30 +00002054 if (AnyUnpred)
2055 ++NumUnpred;
Evan Cheng312b7232007-06-04 06:47:22 +00002056}
2057
Matthias Braun2c931792016-08-17 02:51:57 +00002058/// Copy and predicate instructions from source BB to the destination block.
2059/// Skip end of block branches if IgnoreBr is true.
Evan Cheng92fb5452007-06-15 07:36:12 +00002060void IfConverter::CopyAndPredicateBlock(BBInfo &ToBBI, BBInfo &FromBBI,
Owen Anderson4f6bf042008-08-14 22:49:33 +00002061 SmallVectorImpl<MachineOperand> &Cond,
Evan Cheng92fb5452007-06-15 07:36:12 +00002062 bool IgnoreBr) {
Dan Gohman3b460302008-07-07 23:14:23 +00002063 MachineFunction &MF = *ToBBI.BB->getParent();
2064
Matthias Braun08f47042016-08-17 02:52:01 +00002065 MachineBasicBlock &FromMBB = *FromBBI.BB;
2066 for (MachineInstr &I : FromMBB) {
Evan Cheng92fb5452007-06-15 07:36:12 +00002067 // Do not copy the end of the block branches.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002068 if (IgnoreBr && I.isBranch())
Evan Cheng92fb5452007-06-15 07:36:12 +00002069 break;
2070
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002071 MachineInstr *MI = MF.CloneMachineInstr(&I);
Evan Cheng92fb5452007-06-15 07:36:12 +00002072 ToBBI.BB->insert(ToBBI.BB->end(), MI);
Bob Wilsonefd360c2010-10-26 00:02:21 +00002073 ToBBI.NonPredSize++;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002074 unsigned ExtraPredCost = TII->getPredicationCost(I);
2075 unsigned NumCycles = SchedModel.computeInstrLatency(&I, false);
Evan Chengdebf9c52010-11-03 00:45:17 +00002076 if (NumCycles > 1)
2077 ToBBI.ExtraCost += NumCycles-1;
2078 ToBBI.ExtraCost2 += ExtraPredCost;
Evan Cheng92fb5452007-06-15 07:36:12 +00002079
Bob Wilsonf82c8fc2010-06-18 17:07:23 +00002080 if (!TII->isPredicated(I) && !MI->isDebugValue()) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002081 if (!TII->PredicateInstruction(*MI, Cond)) {
Torok Edwin08954aa2009-07-12 20:07:01 +00002082#ifndef NDEBUG
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002083 dbgs() << "Unable to predicate " << I << "!\n";
Torok Edwin08954aa2009-07-12 20:07:01 +00002084#endif
Craig Topperc0196b12014-04-14 00:51:57 +00002085 llvm_unreachable(nullptr);
Evan Cheng92fb5452007-06-15 07:36:12 +00002086 }
Evan Chengf128bdc2010-06-16 07:35:02 +00002087 }
2088
Bob Wilson45814342010-06-19 05:33:57 +00002089 // If the predicated instruction now redefines a register as the result of
Evan Chengf128bdc2010-06-16 07:35:02 +00002090 // if-conversion, add an implicit kill.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002091 UpdatePredRedefs(*MI, Redefs);
Matthias Braund616ccc2013-10-11 19:04:37 +00002092
2093 // Some kill flags may not be correct anymore.
Andrew Trick276dd452013-10-14 20:45:17 +00002094 if (!DontKill.empty())
Juergen Ributzka310034e2013-12-14 06:52:56 +00002095 RemoveKills(*MI, DontKill);
Evan Cheng92fb5452007-06-15 07:36:12 +00002096 }
2097
Bob Wilson1e5da552010-06-29 00:55:23 +00002098 if (!IgnoreBr) {
Matthias Braun08f47042016-08-17 02:52:01 +00002099 std::vector<MachineBasicBlock *> Succs(FromMBB.succ_begin(),
2100 FromMBB.succ_end());
2101 MachineBasicBlock *NBB = getNextBlock(FromMBB);
Craig Topperc0196b12014-04-14 00:51:57 +00002102 MachineBasicBlock *FallThrough = FromBBI.HasFallThrough ? NBB : nullptr;
Evan Cheng0598b2d2007-06-18 22:44:57 +00002103
Matthias Braunb1e05582016-08-17 02:51:59 +00002104 for (MachineBasicBlock *Succ : Succs) {
Bob Wilson1e5da552010-06-29 00:55:23 +00002105 // Fallthrough edge can't be transferred.
2106 if (Succ == FallThrough)
2107 continue;
2108 ToBBI.BB->addSuccessor(Succ);
2109 }
Evan Cheng0598b2d2007-06-18 22:44:57 +00002110 }
2111
Benjamin Kramer4f6ac162015-02-28 10:11:12 +00002112 ToBBI.Predicate.append(FromBBI.Predicate.begin(), FromBBI.Predicate.end());
2113 ToBBI.Predicate.append(Cond.begin(), Cond.end());
Evan Cheng92fb5452007-06-15 07:36:12 +00002114
2115 ToBBI.ClobbersPred |= FromBBI.ClobbersPred;
2116 ToBBI.IsAnalyzed = false;
2117
Dan Gohmand2d1ae12010-06-22 15:08:57 +00002118 ++NumDupBBs;
Evan Cheng92fb5452007-06-15 07:36:12 +00002119}
2120
Matthias Braun2c931792016-08-17 02:51:57 +00002121/// Move all instructions from FromBB to the end of ToBB. This will leave
2122/// FromBB as an empty block, so remove all of its successor edges except for
2123/// the fall-through edge. If AddEdges is true, i.e., when FromBBI's branch is
2124/// being moved, add those successor edges to ToBBI.
Bob Wilson1e5da552010-06-29 00:55:23 +00002125void IfConverter::MergeBlocks(BBInfo &ToBBI, BBInfo &FromBBI, bool AddEdges) {
Matthias Braun08f47042016-08-17 02:52:01 +00002126 MachineBasicBlock &FromMBB = *FromBBI.BB;
2127 assert(!FromMBB.hasAddressTaken() &&
Evan Cheng8b8e8d82013-05-05 18:03:49 +00002128 "Removing a BB whose address is taken!");
2129
Matthias Braun08f47042016-08-17 02:52:01 +00002130 // In case FromMBB contains terminators (e.g. return instruction),
Krzysztof Parzyszek2451c482016-01-20 13:14:52 +00002131 // first move the non-terminator instructions, then the terminators.
Matthias Braun08f47042016-08-17 02:52:01 +00002132 MachineBasicBlock::iterator FromTI = FromMBB.getFirstTerminator();
Krzysztof Parzyszek2451c482016-01-20 13:14:52 +00002133 MachineBasicBlock::iterator ToTI = ToBBI.BB->getFirstTerminator();
Matthias Braun08f47042016-08-17 02:52:01 +00002134 ToBBI.BB->splice(ToTI, &FromMBB, FromMBB.begin(), FromTI);
Krzysztof Parzyszek2451c482016-01-20 13:14:52 +00002135
2136 // If FromBB has non-predicated terminator we should copy it at the end.
Matthias Braun08f47042016-08-17 02:52:01 +00002137 if (FromTI != FromMBB.end() && !TII->isPredicated(*FromTI))
Krzysztof Parzyszek2451c482016-01-20 13:14:52 +00002138 ToTI = ToBBI.BB->end();
Matthias Braun08f47042016-08-17 02:52:01 +00002139 ToBBI.BB->splice(ToTI, &FromMBB, FromTI, FromMBB.end());
Evan Chengd0e66912007-05-23 07:23:16 +00002140
Cong Houb9e8d482015-12-17 01:29:08 +00002141 // Force normalizing the successors' probabilities of ToBBI.BB to convert all
2142 // unknown probabilities into known ones.
2143 // FIXME: This usage is too tricky and in the future we would like to
2144 // eliminate all unknown probabilities in MBB.
2145 ToBBI.BB->normalizeSuccProbs();
2146
Matthias Braun08f47042016-08-17 02:52:01 +00002147 SmallVector<MachineBasicBlock *, 4> FromSuccs(FromMBB.succ_begin(),
2148 FromMBB.succ_end());
2149 MachineBasicBlock *NBB = getNextBlock(FromMBB);
Craig Topperc0196b12014-04-14 00:51:57 +00002150 MachineBasicBlock *FallThrough = FromBBI.HasFallThrough ? NBB : nullptr;
Matthias Braun08f47042016-08-17 02:52:01 +00002151 // The edge probability from ToBBI.BB to FromMBB, which is only needed when
2152 // AddEdges is true and FromMBB is a successor of ToBBI.BB.
Cong Houd97c1002015-12-01 05:29:22 +00002153 auto To2FromProb = BranchProbability::getZero();
Matthias Braun08f47042016-08-17 02:52:01 +00002154 if (AddEdges && ToBBI.BB->isSuccessor(&FromMBB)) {
2155 To2FromProb = MBPI->getEdgeProbability(ToBBI.BB, &FromMBB);
2156 // Set the edge probability from ToBBI.BB to FromMBB to zero to avoid the
Cong Houd97c1002015-12-01 05:29:22 +00002157 // edge probability being merged to other edges when this edge is removed
2158 // later.
Matthias Braun08f47042016-08-17 02:52:01 +00002159 ToBBI.BB->setSuccProbability(find(ToBBI.BB->successors(), &FromMBB),
David Majnemer42531262016-08-12 03:55:06 +00002160 BranchProbability::getZero());
Cong Houd97c1002015-12-01 05:29:22 +00002161 }
2162
Matthias Braunb1e05582016-08-17 02:51:59 +00002163 for (MachineBasicBlock *Succ : FromSuccs) {
Evan Cheng288f1542007-06-08 22:01:07 +00002164 // Fallthrough edge can't be transferred.
Evan Chengbe9859e2007-06-07 02:12:15 +00002165 if (Succ == FallThrough)
2166 continue;
Cong Houd40105d2015-09-18 20:22:41 +00002167
Cong Houd97c1002015-12-01 05:29:22 +00002168 auto NewProb = BranchProbability::getZero();
Cong Houd40105d2015-09-18 20:22:41 +00002169 if (AddEdges) {
Cong Houd97c1002015-12-01 05:29:22 +00002170 // Calculate the edge probability for the edge from ToBBI.BB to Succ,
Matthias Braun08f47042016-08-17 02:52:01 +00002171 // which is a portion of the edge probability from FromMBB to Succ. The
2172 // portion ratio is the edge probability from ToBBI.BB to FromMBB (if
Cong Houd97c1002015-12-01 05:29:22 +00002173 // FromBBI is a successor of ToBBI.BB. See comment below for excepion).
Matthias Braun08f47042016-08-17 02:52:01 +00002174 NewProb = MBPI->getEdgeProbability(&FromMBB, Succ);
Cong Houd40105d2015-09-18 20:22:41 +00002175
Matthias Braun08f47042016-08-17 02:52:01 +00002176 // To2FromProb is 0 when FromMBB is not a successor of ToBBI.BB. This
2177 // only happens when if-converting a diamond CFG and FromMBB is the
2178 // tail BB. In this case FromMBB post-dominates ToBBI.BB and hence we
2179 // could just use the probabilities on FromMBB's out-edges when adding
Cong Houd97c1002015-12-01 05:29:22 +00002180 // new successors.
2181 if (!To2FromProb.isZero())
2182 NewProb *= To2FromProb;
Cong Houd40105d2015-09-18 20:22:41 +00002183 }
2184
Matthias Braun08f47042016-08-17 02:52:01 +00002185 FromMBB.removeSuccessor(Succ);
Cong Houd40105d2015-09-18 20:22:41 +00002186
2187 if (AddEdges) {
Cong Houd97c1002015-12-01 05:29:22 +00002188 // If the edge from ToBBI.BB to Succ already exists, update the
Cong Houc1069892015-12-13 09:26:17 +00002189 // probability of this edge by adding NewProb to it. An example is shown
Matthias Braun08f47042016-08-17 02:52:01 +00002190 // below, in which A is ToBBI.BB and B is FromMBB. In this case we
Cong Houd97c1002015-12-01 05:29:22 +00002191 // don't have to set C as A's successor as it already is. We only need to
2192 // update the edge probability on A->C. Note that B will not be
2193 // immediately removed from A's successors. It is possible that B->D is
2194 // not removed either if D is a fallthrough of B. Later the edge A->D
2195 // (generated here) and B->D will be combined into one edge. To maintain
2196 // correct edge probability of this combined edge, we need to set the edge
2197 // probability of A->B to zero, which is already done above. The edge
2198 // probability on A->D is calculated by scaling the original probability
2199 // on A->B by the probability of B->D.
Cong Houd40105d2015-09-18 20:22:41 +00002200 //
2201 // Before ifcvt: After ifcvt (assume B->D is kept):
2202 //
2203 // A A
2204 // /| /|\
2205 // / B / B|
2206 // | /| | ||
2207 // |/ | | |/
2208 // C D C D
2209 //
2210 if (ToBBI.BB->isSuccessor(Succ))
Cong Houd97c1002015-12-01 05:29:22 +00002211 ToBBI.BB->setSuccProbability(
David Majnemer42531262016-08-12 03:55:06 +00002212 find(ToBBI.BB->successors(), Succ),
Cong Houd97c1002015-12-01 05:29:22 +00002213 MBPI->getEdgeProbability(ToBBI.BB, Succ) + NewProb);
Cong Houd40105d2015-09-18 20:22:41 +00002214 else
Cong Houd97c1002015-12-01 05:29:22 +00002215 ToBBI.BB->addSuccessor(Succ, NewProb);
Cong Houd40105d2015-09-18 20:22:41 +00002216 }
Evan Chengbe9859e2007-06-07 02:12:15 +00002217 }
2218
Bob Wilson2371f4f2009-05-13 23:25:24 +00002219 // Now FromBBI always falls through to the next block!
Matthias Braun08f47042016-08-17 02:52:01 +00002220 if (NBB && !FromMBB.isSuccessor(NBB))
2221 FromMBB.addSuccessor(NBB);
Evan Cheng288f1542007-06-08 22:01:07 +00002222
Cong Houc1069892015-12-13 09:26:17 +00002223 // Normalize the probabilities of ToBBI.BB's successors with all adjustment
2224 // we've done above.
2225 ToBBI.BB->normalizeSuccProbs();
2226
Benjamin Kramer4f6ac162015-02-28 10:11:12 +00002227 ToBBI.Predicate.append(FromBBI.Predicate.begin(), FromBBI.Predicate.end());
Evan Cheng92fb5452007-06-15 07:36:12 +00002228 FromBBI.Predicate.clear();
2229
Evan Chengd0e66912007-05-23 07:23:16 +00002230 ToBBI.NonPredSize += FromBBI.NonPredSize;
Bob Wilsonefd360c2010-10-26 00:02:21 +00002231 ToBBI.ExtraCost += FromBBI.ExtraCost;
Evan Chengdebf9c52010-11-03 00:45:17 +00002232 ToBBI.ExtraCost2 += FromBBI.ExtraCost2;
Evan Chengd0e66912007-05-23 07:23:16 +00002233 FromBBI.NonPredSize = 0;
Bob Wilsonefd360c2010-10-26 00:02:21 +00002234 FromBBI.ExtraCost = 0;
Evan Chengdebf9c52010-11-03 00:45:17 +00002235 FromBBI.ExtraCost2 = 0;
Evan Cheng9030b982007-06-06 10:16:17 +00002236
Evan Cheng4dd31a72007-06-11 22:26:22 +00002237 ToBBI.ClobbersPred |= FromBBI.ClobbersPred;
Evan Cheng2117d1f2007-06-09 01:03:43 +00002238 ToBBI.HasFallThrough = FromBBI.HasFallThrough;
Evan Cheng1e6f08b2007-06-14 20:28:52 +00002239 ToBBI.IsAnalyzed = false;
2240 FromBBI.IsAnalyzed = false;
Evan Chengf5e53a52007-05-16 02:00:57 +00002241}
Akira Hatanaka4a616192015-06-08 18:50:43 +00002242
2243FunctionPass *
2244llvm::createIfConverter(std::function<bool(const Function &)> Ftor) {
Benjamin Kramerd3f4c052016-06-12 16:13:55 +00002245 return new IfConverter(std::move(Ftor));
Akira Hatanaka4a616192015-06-08 18:50:43 +00002246}