Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 1 | //===-- IfConversion.cpp - Machine code if conversion pass. ---------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Justin Lebar | acc4710 | 2016-04-01 01:09:03 +0000 | [diff] [blame] | 10 | // This file implements the machine instruction level if-conversion pass, which |
| 11 | // tries to convert conditional branches into predicated instructions. |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/Passes.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "BranchFolding.h" |
| 17 | #include "llvm/ADT/STLExtras.h" |
Kyle Butt | d76755e | 2016-08-18 22:09:23 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/ScopeExit.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallSet.h" |
| 20 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/LivePhysRegs.h" |
Akira Hatanaka | bbd33f6 | 2014-08-07 19:30:13 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineBlockFrequencyInfo.h" |
Jakub Staszak | 3ef20e3 | 2011-08-03 22:53:41 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineBranchProbabilityInfo.h" |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Jakob Stoklund Olesen | f623e98 | 2012-12-20 18:08:06 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Evan Cheng | c5adcca | 2012-06-08 21:53:50 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Arnold Schwaighofer | d2f96b9 | 2013-09-30 15:28:56 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/TargetSchedule.h" |
Evan Cheng | e93ccc0 | 2007-06-08 19:10:51 +0000 | [diff] [blame] | 29 | #include "llvm/Support/CommandLine.h" |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Debug.h" |
Torok Edwin | ccb29cd | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 31 | #include "llvm/Support/ErrorHandling.h" |
| 32 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 33 | #include "llvm/Target/TargetInstrInfo.h" |
| 34 | #include "llvm/Target/TargetLowering.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 35 | #include "llvm/Target/TargetRegisterInfo.h" |
Arnold Schwaighofer | d2f96b9 | 2013-09-30 15:28:56 +0000 | [diff] [blame] | 36 | #include "llvm/Target/TargetSubtargetInfo.h" |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 37 | #include <algorithm> |
Benjamin Kramer | 82de7d3 | 2016-05-27 14:27:24 +0000 | [diff] [blame] | 38 | #include <utility> |
Arnold Schwaighofer | d2f96b9 | 2013-09-30 15:28:56 +0000 | [diff] [blame] | 39 | |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 40 | using namespace llvm; |
| 41 | |
Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 42 | #define DEBUG_TYPE "ifcvt" |
| 43 | |
Chris Lattner | 769c86b | 2008-01-07 05:40:58 +0000 | [diff] [blame] | 44 | // Hidden options for help debugging. |
| 45 | static cl::opt<int> IfCvtFnStart("ifcvt-fn-start", cl::init(-1), cl::Hidden); |
| 46 | static cl::opt<int> IfCvtFnStop("ifcvt-fn-stop", cl::init(-1), cl::Hidden); |
| 47 | static cl::opt<int> IfCvtLimit("ifcvt-limit", cl::init(-1), cl::Hidden); |
Bob Wilson | 8105144 | 2010-06-15 22:18:54 +0000 | [diff] [blame] | 48 | static cl::opt<bool> DisableSimple("disable-ifcvt-simple", |
Chris Lattner | 769c86b | 2008-01-07 05:40:58 +0000 | [diff] [blame] | 49 | cl::init(false), cl::Hidden); |
Bob Wilson | 8105144 | 2010-06-15 22:18:54 +0000 | [diff] [blame] | 50 | static cl::opt<bool> DisableSimpleF("disable-ifcvt-simple-false", |
Chris Lattner | 769c86b | 2008-01-07 05:40:58 +0000 | [diff] [blame] | 51 | cl::init(false), cl::Hidden); |
Bob Wilson | 8105144 | 2010-06-15 22:18:54 +0000 | [diff] [blame] | 52 | static cl::opt<bool> DisableTriangle("disable-ifcvt-triangle", |
Chris Lattner | 769c86b | 2008-01-07 05:40:58 +0000 | [diff] [blame] | 53 | cl::init(false), cl::Hidden); |
Bob Wilson | 8105144 | 2010-06-15 22:18:54 +0000 | [diff] [blame] | 54 | static cl::opt<bool> DisableTriangleR("disable-ifcvt-triangle-rev", |
Chris Lattner | 769c86b | 2008-01-07 05:40:58 +0000 | [diff] [blame] | 55 | cl::init(false), cl::Hidden); |
Bob Wilson | 8105144 | 2010-06-15 22:18:54 +0000 | [diff] [blame] | 56 | static cl::opt<bool> DisableTriangleF("disable-ifcvt-triangle-false", |
Chris Lattner | 769c86b | 2008-01-07 05:40:58 +0000 | [diff] [blame] | 57 | cl::init(false), cl::Hidden); |
Bob Wilson | 8105144 | 2010-06-15 22:18:54 +0000 | [diff] [blame] | 58 | static cl::opt<bool> DisableTriangleFR("disable-ifcvt-triangle-false-rev", |
Chris Lattner | 769c86b | 2008-01-07 05:40:58 +0000 | [diff] [blame] | 59 | cl::init(false), cl::Hidden); |
Bob Wilson | 8105144 | 2010-06-15 22:18:54 +0000 | [diff] [blame] | 60 | static cl::opt<bool> DisableDiamond("disable-ifcvt-diamond", |
Chris Lattner | 769c86b | 2008-01-07 05:40:58 +0000 | [diff] [blame] | 61 | cl::init(false), cl::Hidden); |
Evan Cheng | f128bdc | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 62 | static cl::opt<bool> IfCvtBranchFold("ifcvt-branch-fold", |
| 63 | cl::init(true), cl::Hidden); |
Evan Cheng | e93ccc0 | 2007-06-08 19:10:51 +0000 | [diff] [blame] | 64 | |
Evan Cheng | 2117d1f | 2007-06-09 01:03:43 +0000 | [diff] [blame] | 65 | STATISTIC(NumSimple, "Number of simple if-conversions performed"); |
| 66 | STATISTIC(NumSimpleFalse, "Number of simple (F) if-conversions performed"); |
| 67 | STATISTIC(NumTriangle, "Number of triangle if-conversions performed"); |
Evan Cheng | 9acfa7b | 2007-06-12 23:54:05 +0000 | [diff] [blame] | 68 | STATISTIC(NumTriangleRev, "Number of triangle (R) if-conversions performed"); |
Evan Cheng | 2117d1f | 2007-06-09 01:03:43 +0000 | [diff] [blame] | 69 | STATISTIC(NumTriangleFalse,"Number of triangle (F) if-conversions performed"); |
| 70 | STATISTIC(NumTriangleFRev, "Number of triangle (F/R) if-conversions performed"); |
| 71 | STATISTIC(NumDiamonds, "Number of diamond if-conversions performed"); |
| 72 | STATISTIC(NumIfConvBBs, "Number of if-converted blocks"); |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 73 | STATISTIC(NumDupBBs, "Number of duplicated blocks"); |
Evan Cheng | 4266a79 | 2011-12-19 22:01:30 +0000 | [diff] [blame] | 74 | STATISTIC(NumUnpred, "Number of true blocks of diamonds unpredicated"); |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 75 | |
| 76 | namespace { |
Nick Lewycky | 02d5f77 | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 77 | class IfConverter : public MachineFunctionPass { |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 78 | enum IfcvtKind { |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 79 | ICNotClassfied, // BB data valid, but not classified. |
Evan Cheng | 312b723 | 2007-06-04 06:47:22 +0000 | [diff] [blame] | 80 | ICSimpleFalse, // Same as ICSimple, but on the false path. |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 81 | ICSimple, // BB is entry of an one split, no rejoin sub-CFG. |
| 82 | ICTriangleFRev, // Same as ICTriangleFalse, but false path rev condition. |
Evan Cheng | 9acfa7b | 2007-06-12 23:54:05 +0000 | [diff] [blame] | 83 | ICTriangleRev, // Same as ICTriangle, but true path rev condition. |
Evan Cheng | 2117d1f | 2007-06-09 01:03:43 +0000 | [diff] [blame] | 84 | ICTriangleFalse, // Same as ICTriangle, but on the false path. |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 85 | ICTriangle, // BB is entry of a triangle sub-CFG. |
Kyle Butt | ce0196d | 2016-08-19 18:17:04 +0000 | [diff] [blame] | 86 | ICDiamond // BB is entry of a diamond sub-CFG. |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 87 | }; |
| 88 | |
Matthias Braun | 2c93179 | 2016-08-17 02:51:57 +0000 | [diff] [blame] | 89 | /// One per MachineBasicBlock, this is used to cache the result |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 90 | /// if-conversion feasibility analysis. This includes results from |
Jacques Pienaar | 71c30a1 | 2016-07-15 14:41:04 +0000 | [diff] [blame] | 91 | /// TargetInstrInfo::analyzeBranch() (i.e. TBB, FBB, and Cond), and its |
Evan Cheng | 0f745da | 2007-05-18 00:20:58 +0000 | [diff] [blame] | 92 | /// classification, and common tail block of its successors (if it's a |
Evan Cheng | 2e82cef | 2007-05-18 18:14:37 +0000 | [diff] [blame] | 93 | /// diamond shape), its size, whether it's predicable, and whether any |
| 94 | /// instruction can clobber the 'would-be' predicate. |
Evan Cheng | d0e6691 | 2007-05-23 07:23:16 +0000 | [diff] [blame] | 95 | /// |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 96 | /// IsDone - True if BB is not to be considered for ifcvt. |
| 97 | /// IsBeingAnalyzed - True if BB is currently being analyzed. |
| 98 | /// IsAnalyzed - True if BB has been analyzed (info is still valid). |
| 99 | /// IsEnqueued - True if BB has been enqueued to be ifcvt'ed. |
Jacques Pienaar | 71c30a1 | 2016-07-15 14:41:04 +0000 | [diff] [blame] | 100 | /// IsBrAnalyzable - True if analyzeBranch() returns false. |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 101 | /// HasFallThrough - True if BB may fallthrough to the following BB. |
| 102 | /// IsUnpredicable - True if BB is known to be unpredicable. |
Evan Cheng | fbc7309 | 2007-07-10 17:50:43 +0000 | [diff] [blame] | 103 | /// ClobbersPred - True if BB could modify predicates (e.g. has |
Evan Cheng | 9030b98 | 2007-06-06 10:16:17 +0000 | [diff] [blame] | 104 | /// cmp, call, etc.) |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 105 | /// NonPredSize - Number of non-predicated instructions. |
Evan Cheng | debf9c5 | 2010-11-03 00:45:17 +0000 | [diff] [blame] | 106 | /// ExtraCost - Extra cost for multi-cycle instructions. |
| 107 | /// ExtraCost2 - Some instructions are slower when predicated |
Evan Cheng | d0e6691 | 2007-05-23 07:23:16 +0000 | [diff] [blame] | 108 | /// BB - Corresponding MachineBasicBlock. |
Jacques Pienaar | 71c30a1 | 2016-07-15 14:41:04 +0000 | [diff] [blame] | 109 | /// TrueBB / FalseBB- See analyzeBranch(). |
Evan Cheng | d0e6691 | 2007-05-23 07:23:16 +0000 | [diff] [blame] | 110 | /// BrCond - Conditions for end of block conditional branches. |
| 111 | /// Predicate - Predicate used in the BB. |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 112 | struct BBInfo { |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 113 | bool IsDone : 1; |
| 114 | bool IsBeingAnalyzed : 1; |
| 115 | bool IsAnalyzed : 1; |
| 116 | bool IsEnqueued : 1; |
| 117 | bool IsBrAnalyzable : 1; |
| 118 | bool HasFallThrough : 1; |
| 119 | bool IsUnpredicable : 1; |
Evan Cheng | 23402fc | 2007-06-15 21:18:05 +0000 | [diff] [blame] | 120 | bool CannotBeCopied : 1; |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 121 | bool ClobbersPred : 1; |
Evan Cheng | d0e6691 | 2007-05-23 07:23:16 +0000 | [diff] [blame] | 122 | unsigned NonPredSize; |
Bob Wilson | efd360c | 2010-10-26 00:02:21 +0000 | [diff] [blame] | 123 | unsigned ExtraCost; |
Evan Cheng | debf9c5 | 2010-11-03 00:45:17 +0000 | [diff] [blame] | 124 | unsigned ExtraCost2; |
Evan Cheng | 0f745da | 2007-05-18 00:20:58 +0000 | [diff] [blame] | 125 | MachineBasicBlock *BB; |
| 126 | MachineBasicBlock *TrueBB; |
| 127 | MachineBasicBlock *FalseBB; |
Owen Anderson | 4f6bf04 | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 128 | SmallVector<MachineOperand, 4> BrCond; |
| 129 | SmallVector<MachineOperand, 4> Predicate; |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 130 | BBInfo() : IsDone(false), IsBeingAnalyzed(false), |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 131 | IsAnalyzed(false), IsEnqueued(false), IsBrAnalyzable(false), |
Kyle Butt | ce0196d | 2016-08-19 18:17:04 +0000 | [diff] [blame] | 132 | HasFallThrough(false), IsUnpredicable(false), |
| 133 | CannotBeCopied(false), ClobbersPred(false), NonPredSize(0), |
| 134 | ExtraCost(0), ExtraCost2(0), BB(nullptr), TrueBB(nullptr), |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 135 | FalseBB(nullptr) {} |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 136 | }; |
| 137 | |
Matthias Braun | 2c93179 | 2016-08-17 02:51:57 +0000 | [diff] [blame] | 138 | /// Record information about pending if-conversions to attempt: |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 139 | /// BBI - Corresponding BBInfo. |
| 140 | /// Kind - Type of block. See IfcvtKind. |
Bob Wilson | 2371f4f | 2009-05-13 23:25:24 +0000 | [diff] [blame] | 141 | /// NeedSubsumption - True if the to-be-predicated BB has already been |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 142 | /// predicated. |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 143 | /// NumDups - Number of instructions that would be duplicated due |
| 144 | /// to this if-conversion. (For diamonds, the number of |
| 145 | /// identical instructions at the beginnings of both |
| 146 | /// paths). |
| 147 | /// NumDups2 - For diamonds, the number of identical instructions |
| 148 | /// at the ends of both paths. |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 149 | struct IfcvtToken { |
| 150 | BBInfo &BBI; |
| 151 | IfcvtKind Kind; |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 152 | unsigned NumDups; |
| 153 | unsigned NumDups2; |
Kyle Butt | 6262ca3 | 2016-08-24 21:34:24 +0000 | [diff] [blame^] | 154 | bool NeedSubsumption : 1; |
| 155 | bool TClobbersPred : 1; |
| 156 | bool FClobbersPred : 1; |
| 157 | IfcvtToken(BBInfo &b, IfcvtKind k, bool s, unsigned d, unsigned d2 = 0, |
| 158 | bool tc = false, bool fc = false) |
| 159 | : BBI(b), Kind(k), NumDups(d), NumDups2(d2), NeedSubsumption(s), |
| 160 | TClobbersPred(tc), FClobbersPred(fc) {} |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 161 | }; |
| 162 | |
Matthias Braun | 2c93179 | 2016-08-17 02:51:57 +0000 | [diff] [blame] | 163 | /// Results of if-conversion feasibility analysis indexed by basic block |
| 164 | /// number. |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 165 | std::vector<BBInfo> BBAnalysis; |
Arnold Schwaighofer | d2f96b9 | 2013-09-30 15:28:56 +0000 | [diff] [blame] | 166 | TargetSchedModel SchedModel; |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 167 | |
Benjamin Kramer | 56b31bd | 2013-01-11 20:05:37 +0000 | [diff] [blame] | 168 | const TargetLoweringBase *TLI; |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 169 | const TargetInstrInfo *TII; |
Evan Cheng | f128bdc | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 170 | const TargetRegisterInfo *TRI; |
Jakub Staszak | 15e5b74 | 2011-08-03 22:34:43 +0000 | [diff] [blame] | 171 | const MachineBranchProbabilityInfo *MBPI; |
Evan Cheng | c5adcca | 2012-06-08 21:53:50 +0000 | [diff] [blame] | 172 | MachineRegisterInfo *MRI; |
Jakub Staszak | 15e5b74 | 2011-08-03 22:34:43 +0000 | [diff] [blame] | 173 | |
Juergen Ributzka | 310034e | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 174 | LivePhysRegs Redefs; |
| 175 | LivePhysRegs DontKill; |
Andrew Trick | 276dd45 | 2013-10-14 20:45:17 +0000 | [diff] [blame] | 176 | |
Evan Cheng | c5adcca | 2012-06-08 21:53:50 +0000 | [diff] [blame] | 177 | bool PreRegAlloc; |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 178 | bool MadeChange; |
Owen Anderson | 816e283 | 2009-06-24 23:41:44 +0000 | [diff] [blame] | 179 | int FnNum; |
Akira Hatanaka | 4a61619 | 2015-06-08 18:50:43 +0000 | [diff] [blame] | 180 | std::function<bool(const Function &)> PredicateFtor; |
| 181 | |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 182 | public: |
| 183 | static char ID; |
Akira Hatanaka | 4a61619 | 2015-06-08 18:50:43 +0000 | [diff] [blame] | 184 | IfConverter(std::function<bool(const Function &)> Ftor = nullptr) |
Benjamin Kramer | 82de7d3 | 2016-05-27 14:27:24 +0000 | [diff] [blame] | 185 | : MachineFunctionPass(ID), FnNum(-1), PredicateFtor(std::move(Ftor)) { |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 186 | initializeIfConverterPass(*PassRegistry::getPassRegistry()); |
| 187 | } |
Jakub Staszak | 15e5b74 | 2011-08-03 22:34:43 +0000 | [diff] [blame] | 188 | |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 189 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Akira Hatanaka | bbd33f6 | 2014-08-07 19:30:13 +0000 | [diff] [blame] | 190 | AU.addRequired<MachineBlockFrequencyInfo>(); |
Jakub Staszak | 15e5b74 | 2011-08-03 22:34:43 +0000 | [diff] [blame] | 191 | AU.addRequired<MachineBranchProbabilityInfo>(); |
Owen Anderson | 1b35f4c | 2010-09-28 20:42:15 +0000 | [diff] [blame] | 192 | MachineFunctionPass::getAnalysisUsage(AU); |
| 193 | } |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 194 | |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 195 | bool runOnMachineFunction(MachineFunction &MF) override; |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 196 | |
Derek Schuff | 1dbf7a5 | 2016-04-04 17:09:25 +0000 | [diff] [blame] | 197 | MachineFunctionProperties getRequiredProperties() const override { |
| 198 | return MachineFunctionProperties().set( |
| 199 | MachineFunctionProperties::Property::AllVRegsAllocated); |
| 200 | } |
| 201 | |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 202 | private: |
Kyle Butt | 59f2a2a | 2016-07-27 20:19:31 +0000 | [diff] [blame] | 203 | bool ReverseBranchCondition(BBInfo &BBI) const; |
Owen Anderson | f31f33e | 2010-10-01 22:45:50 +0000 | [diff] [blame] | 204 | bool ValidSimple(BBInfo &TrueBBI, unsigned &Dups, |
Cong Hou | c536bd9 | 2015-09-10 23:10:42 +0000 | [diff] [blame] | 205 | BranchProbability Prediction) const; |
Evan Cheng | 7783f82 | 2007-06-08 09:36:04 +0000 | [diff] [blame] | 206 | bool ValidTriangle(BBInfo &TrueBBI, BBInfo &FalseBBI, |
Owen Anderson | f31f33e | 2010-10-01 22:45:50 +0000 | [diff] [blame] | 207 | bool FalseBranch, unsigned &Dups, |
Cong Hou | c536bd9 | 2015-09-10 23:10:42 +0000 | [diff] [blame] | 208 | BranchProbability Prediction) const; |
Kyle Butt | 6262ca3 | 2016-08-24 21:34:24 +0000 | [diff] [blame^] | 209 | bool CountDuplicatedInstructions( |
| 210 | MachineBasicBlock::iterator &TIB, MachineBasicBlock::iterator &FIB, |
| 211 | MachineBasicBlock::iterator &TIE, MachineBasicBlock::iterator &FIE, |
| 212 | unsigned &Dups1, unsigned &Dups2, |
| 213 | MachineBasicBlock &TBB, MachineBasicBlock &FBB, |
| 214 | bool SkipUnconditionalBranches) const; |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 215 | bool ValidDiamond(BBInfo &TrueBBI, BBInfo &FalseBBI, |
Kyle Butt | 6262ca3 | 2016-08-24 21:34:24 +0000 | [diff] [blame^] | 216 | unsigned &Dups1, unsigned &Dups2, |
| 217 | BBInfo &TrueBBICalc, BBInfo &FalseBBICalc) const; |
Kyle Butt | 54bf3ce | 2016-08-06 01:52:34 +0000 | [diff] [blame] | 218 | void AnalyzeBranches(BBInfo &BBI); |
| 219 | void ScanInstructions(BBInfo &BBI, |
| 220 | MachineBasicBlock::iterator &Begin, |
Kyle Butt | 6262ca3 | 2016-08-24 21:34:24 +0000 | [diff] [blame^] | 221 | MachineBasicBlock::iterator &End, |
| 222 | bool BranchUnpredicable = false) const; |
| 223 | bool RescanInstructions( |
| 224 | MachineBasicBlock::iterator &TIB, MachineBasicBlock::iterator &FIB, |
| 225 | MachineBasicBlock::iterator &TIE, MachineBasicBlock::iterator &FIE, |
| 226 | BBInfo &TrueBBI, BBInfo &FalseBBI) const; |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 227 | void AnalyzeBlock(MachineBasicBlock &MBB, |
Justin Lebar | 3a7bc57 | 2016-02-22 17:51:28 +0000 | [diff] [blame] | 228 | std::vector<std::unique_ptr<IfcvtToken>> &Tokens); |
Owen Anderson | 4f6bf04 | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 229 | bool FeasibilityAnalysis(BBInfo &BBI, SmallVectorImpl<MachineOperand> &Cond, |
Kyle Butt | 6262ca3 | 2016-08-24 21:34:24 +0000 | [diff] [blame^] | 230 | bool isTriangle = false, bool RevBranch = false, |
| 231 | bool hasCommonTail = false); |
Justin Lebar | 3a7bc57 | 2016-02-22 17:51:28 +0000 | [diff] [blame] | 232 | void AnalyzeBlocks(MachineFunction &MF, |
| 233 | std::vector<std::unique_ptr<IfcvtToken>> &Tokens); |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 234 | void InvalidatePreds(MachineBasicBlock &MBB); |
Evan Cheng | 288f154 | 2007-06-08 22:01:07 +0000 | [diff] [blame] | 235 | void RemoveExtraEdges(BBInfo &BBI); |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 236 | bool IfConvertSimple(BBInfo &BBI, IfcvtKind Kind); |
| 237 | bool IfConvertTriangle(BBInfo &BBI, IfcvtKind Kind); |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 238 | bool IfConvertDiamond(BBInfo &BBI, IfcvtKind Kind, |
Kyle Butt | 6262ca3 | 2016-08-24 21:34:24 +0000 | [diff] [blame^] | 239 | unsigned NumDups1, unsigned NumDups2, |
| 240 | bool TClobbers, bool FClobbers); |
Evan Cheng | d0e6691 | 2007-05-23 07:23:16 +0000 | [diff] [blame] | 241 | void PredicateBlock(BBInfo &BBI, |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 242 | MachineBasicBlock::iterator E, |
Evan Cheng | f128bdc | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 243 | SmallVectorImpl<MachineOperand> &Cond, |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 244 | SmallSet<unsigned, 4> *LaterRedefs = nullptr); |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 245 | void CopyAndPredicateBlock(BBInfo &ToBBI, BBInfo &FromBBI, |
Owen Anderson | 4f6bf04 | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 246 | SmallVectorImpl<MachineOperand> &Cond, |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 247 | bool IgnoreBr = false); |
Bob Wilson | 1e5da55 | 2010-06-29 00:55:23 +0000 | [diff] [blame] | 248 | void MergeBlocks(BBInfo &ToBBI, BBInfo &FromBBI, bool AddEdges = true); |
Evan Cheng | 20e0599 | 2007-06-01 00:12:12 +0000 | [diff] [blame] | 249 | |
Evan Cheng | debf9c5 | 2010-11-03 00:45:17 +0000 | [diff] [blame] | 250 | bool MeetIfcvtSizeLimit(MachineBasicBlock &BB, |
| 251 | unsigned Cycle, unsigned Extra, |
Cong Hou | c536bd9 | 2015-09-10 23:10:42 +0000 | [diff] [blame] | 252 | BranchProbability Prediction) const { |
Evan Cheng | debf9c5 | 2010-11-03 00:45:17 +0000 | [diff] [blame] | 253 | return Cycle > 0 && TII->isProfitableToIfCvt(BB, Cycle, Extra, |
Jakub Staszak | 9b07c0a | 2011-07-10 02:58:07 +0000 | [diff] [blame] | 254 | Prediction); |
Evan Cheng | 02b184d | 2010-06-25 22:42:03 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Evan Cheng | debf9c5 | 2010-11-03 00:45:17 +0000 | [diff] [blame] | 257 | bool MeetIfcvtSizeLimit(MachineBasicBlock &TBB, |
| 258 | unsigned TCycle, unsigned TExtra, |
| 259 | MachineBasicBlock &FBB, |
| 260 | unsigned FCycle, unsigned FExtra, |
Cong Hou | c536bd9 | 2015-09-10 23:10:42 +0000 | [diff] [blame] | 261 | BranchProbability Prediction) const { |
Evan Cheng | debf9c5 | 2010-11-03 00:45:17 +0000 | [diff] [blame] | 262 | return TCycle > 0 && FCycle > 0 && |
| 263 | TII->isProfitableToIfCvt(TBB, TCycle, TExtra, FBB, FCycle, FExtra, |
Jakub Staszak | 9b07c0a | 2011-07-10 02:58:07 +0000 | [diff] [blame] | 264 | Prediction); |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Matthias Braun | 2c93179 | 2016-08-17 02:51:57 +0000 | [diff] [blame] | 267 | /// Returns true if Block ends without a terminator. |
Evan Cheng | be9859e | 2007-06-07 02:12:15 +0000 | [diff] [blame] | 268 | bool blockAlwaysFallThrough(BBInfo &BBI) const { |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 269 | return BBI.IsBrAnalyzable && BBI.TrueBB == nullptr; |
Evan Cheng | 9030b98 | 2007-06-06 10:16:17 +0000 | [diff] [blame] | 270 | } |
| 271 | |
Matthias Braun | 2c93179 | 2016-08-17 02:51:57 +0000 | [diff] [blame] | 272 | /// Used to sort if-conversion candidates. |
Justin Lebar | 3a7bc57 | 2016-02-22 17:51:28 +0000 | [diff] [blame] | 273 | static bool IfcvtTokenCmp(const std::unique_ptr<IfcvtToken> &C1, |
| 274 | const std::unique_ptr<IfcvtToken> &C2) { |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 275 | int Incr1 = (C1->Kind == ICDiamond) |
| 276 | ? -(int)(C1->NumDups + C1->NumDups2) : (int)C1->NumDups; |
| 277 | int Incr2 = (C2->Kind == ICDiamond) |
| 278 | ? -(int)(C2->NumDups + C2->NumDups2) : (int)C2->NumDups; |
| 279 | if (Incr1 > Incr2) |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 280 | return true; |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 281 | else if (Incr1 == Incr2) { |
Bob Wilson | 2371f4f | 2009-05-13 23:25:24 +0000 | [diff] [blame] | 282 | // Favors subsumption. |
David Blaikie | dc3f01e | 2015-03-09 01:57:13 +0000 | [diff] [blame] | 283 | if (!C1->NeedSubsumption && C2->NeedSubsumption) |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 284 | return true; |
Bob Wilson | 2371f4f | 2009-05-13 23:25:24 +0000 | [diff] [blame] | 285 | else if (C1->NeedSubsumption == C2->NeedSubsumption) { |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 286 | // Favors diamond over triangle, etc. |
| 287 | if ((unsigned)C1->Kind < (unsigned)C2->Kind) |
| 288 | return true; |
| 289 | else if (C1->Kind == C2->Kind) |
| 290 | return C1->BBI.BB->getNumber() < C2->BBI.BB->getNumber(); |
| 291 | } |
| 292 | } |
| 293 | return false; |
Evan Cheng | 20e0599 | 2007-06-01 00:12:12 +0000 | [diff] [blame] | 294 | } |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 295 | }; |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 296 | |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 297 | char IfConverter::ID = 0; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 298 | } |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 299 | |
Andrew Trick | 1fa5bcb | 2012-02-08 21:23:13 +0000 | [diff] [blame] | 300 | char &llvm::IfConverterID = IfConverter::ID; |
| 301 | |
Owen Anderson | 8ac477f | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 302 | INITIALIZE_PASS_BEGIN(IfConverter, "if-converter", "If Converter", false, false) |
Jakub Staszak | 15e5b74 | 2011-08-03 22:34:43 +0000 | [diff] [blame] | 303 | INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo) |
Owen Anderson | 8ac477f | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 304 | INITIALIZE_PASS_END(IfConverter, "if-converter", "If Converter", false, false) |
Bob Wilson | 97b9312 | 2009-10-28 20:46:46 +0000 | [diff] [blame] | 305 | |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 306 | bool IfConverter::runOnMachineFunction(MachineFunction &MF) { |
Andrew Kaylor | 50271f7 | 2016-05-03 22:32:30 +0000 | [diff] [blame] | 307 | if (skipFunction(*MF.getFunction()) || |
| 308 | (PredicateFtor && !PredicateFtor(*MF.getFunction()))) |
Akira Hatanaka | 4a61619 | 2015-06-08 18:50:43 +0000 | [diff] [blame] | 309 | return false; |
| 310 | |
Eric Christopher | 3d4276f | 2015-01-27 07:31:29 +0000 | [diff] [blame] | 311 | const TargetSubtargetInfo &ST = MF.getSubtarget(); |
| 312 | TLI = ST.getTargetLowering(); |
| 313 | TII = ST.getInstrInfo(); |
| 314 | TRI = ST.getRegisterInfo(); |
Haicheng Wu | 5b458cc | 2016-06-09 15:24:29 +0000 | [diff] [blame] | 315 | BranchFolder::MBFIWrapper MBFI(getAnalysis<MachineBlockFrequencyInfo>()); |
Jakub Staszak | 15e5b74 | 2011-08-03 22:34:43 +0000 | [diff] [blame] | 316 | MBPI = &getAnalysis<MachineBranchProbabilityInfo>(); |
Evan Cheng | c5adcca | 2012-06-08 21:53:50 +0000 | [diff] [blame] | 317 | MRI = &MF.getRegInfo(); |
Pete Cooper | 1175945 | 2014-09-02 17:43:54 +0000 | [diff] [blame] | 318 | SchedModel.init(ST.getSchedModel(), &ST, TII); |
Arnold Schwaighofer | d2f96b9 | 2013-09-30 15:28:56 +0000 | [diff] [blame] | 319 | |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 320 | if (!TII) return false; |
| 321 | |
Evan Cheng | c5adcca | 2012-06-08 21:53:50 +0000 | [diff] [blame] | 322 | PreRegAlloc = MRI->isSSA(); |
| 323 | |
| 324 | bool BFChange = false; |
| 325 | if (!PreRegAlloc) { |
| 326 | // Tail merge tend to expose more if-conversion opportunities. |
Haicheng Wu | 5b458cc | 2016-06-09 15:24:29 +0000 | [diff] [blame] | 327 | BranchFolder BF(true, false, MBFI, *MBPI); |
Eric Christopher | 3d4276f | 2015-01-27 07:31:29 +0000 | [diff] [blame] | 328 | BFChange = BF.OptimizeFunction(MF, TII, ST.getRegisterInfo(), |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 329 | getAnalysisIfAvailable<MachineModuleInfo>()); |
Evan Cheng | c5adcca | 2012-06-08 21:53:50 +0000 | [diff] [blame] | 330 | } |
Evan Cheng | 2d51c7c | 2010-06-18 23:09:54 +0000 | [diff] [blame] | 331 | |
David Greene | 72e47cd | 2010-01-04 22:02:01 +0000 | [diff] [blame] | 332 | DEBUG(dbgs() << "\nIfcvt: function (" << ++FnNum << ") \'" |
Craig Topper | a538d83 | 2012-08-22 06:07:19 +0000 | [diff] [blame] | 333 | << MF.getName() << "\'"); |
Evan Cheng | e93ccc0 | 2007-06-08 19:10:51 +0000 | [diff] [blame] | 334 | |
| 335 | if (FnNum < IfCvtFnStart || (IfCvtFnStop != -1 && FnNum > IfCvtFnStop)) { |
David Greene | 72e47cd | 2010-01-04 22:02:01 +0000 | [diff] [blame] | 336 | DEBUG(dbgs() << " skipped\n"); |
Evan Cheng | e93ccc0 | 2007-06-08 19:10:51 +0000 | [diff] [blame] | 337 | return false; |
| 338 | } |
David Greene | 72e47cd | 2010-01-04 22:02:01 +0000 | [diff] [blame] | 339 | DEBUG(dbgs() << "\n"); |
Evan Cheng | 20e0599 | 2007-06-01 00:12:12 +0000 | [diff] [blame] | 340 | |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 341 | MF.RenumberBlocks(); |
Evan Cheng | 20e0599 | 2007-06-01 00:12:12 +0000 | [diff] [blame] | 342 | BBAnalysis.resize(MF.getNumBlockIDs()); |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 343 | |
Justin Lebar | 3a7bc57 | 2016-02-22 17:51:28 +0000 | [diff] [blame] | 344 | std::vector<std::unique_ptr<IfcvtToken>> Tokens; |
Evan Cheng | 478b805 | 2007-05-18 01:55:58 +0000 | [diff] [blame] | 345 | MadeChange = false; |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 346 | unsigned NumIfCvts = NumSimple + NumSimpleFalse + NumTriangle + |
| 347 | NumTriangleRev + NumTriangleFalse + NumTriangleFRev + NumDiamonds; |
| 348 | while (IfCvtLimit == -1 || (int)NumIfCvts < IfCvtLimit) { |
Bob Wilson | 2371f4f | 2009-05-13 23:25:24 +0000 | [diff] [blame] | 349 | // Do an initial analysis for each basic block and find all the potential |
| 350 | // candidates to perform if-conversion. |
Bob Wilson | fc7d739 | 2010-06-15 18:57:15 +0000 | [diff] [blame] | 351 | bool Change = false; |
| 352 | AnalyzeBlocks(MF, Tokens); |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 353 | while (!Tokens.empty()) { |
Justin Lebar | 3a7bc57 | 2016-02-22 17:51:28 +0000 | [diff] [blame] | 354 | std::unique_ptr<IfcvtToken> Token = std::move(Tokens.back()); |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 355 | Tokens.pop_back(); |
| 356 | BBInfo &BBI = Token->BBI; |
| 357 | IfcvtKind Kind = Token->Kind; |
Nuno Lopes | 57c6594 | 2008-11-04 13:02:59 +0000 | [diff] [blame] | 358 | unsigned NumDups = Token->NumDups; |
Duncan Sands | dd571d3 | 2008-11-04 18:05:30 +0000 | [diff] [blame] | 359 | unsigned NumDups2 = Token->NumDups2; |
Nuno Lopes | 57c6594 | 2008-11-04 13:02:59 +0000 | [diff] [blame] | 360 | |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 361 | // If the block has been evicted out of the queue or it has already been |
| 362 | // marked dead (due to it being predicated), then skip it. |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 363 | if (BBI.IsDone) |
| 364 | BBI.IsEnqueued = false; |
| 365 | if (!BBI.IsEnqueued) |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 366 | continue; |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 367 | |
Evan Cheng | 1e6f08b | 2007-06-14 20:28:52 +0000 | [diff] [blame] | 368 | BBI.IsEnqueued = false; |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 369 | |
Evan Cheng | 312b723 | 2007-06-04 06:47:22 +0000 | [diff] [blame] | 370 | bool RetVal = false; |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 371 | switch (Kind) { |
Craig Topper | ee4dab5 | 2012-02-05 08:31:47 +0000 | [diff] [blame] | 372 | default: llvm_unreachable("Unexpected!"); |
Evan Cheng | 312b723 | 2007-06-04 06:47:22 +0000 | [diff] [blame] | 373 | case ICSimple: |
Evan Cheng | b30a894 | 2007-06-06 01:12:44 +0000 | [diff] [blame] | 374 | case ICSimpleFalse: { |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 375 | bool isFalse = Kind == ICSimpleFalse; |
Evan Cheng | 2117d1f | 2007-06-09 01:03:43 +0000 | [diff] [blame] | 376 | if ((isFalse && DisableSimpleF) || (!isFalse && DisableSimple)) break; |
Bob Wilson | 8105144 | 2010-06-15 22:18:54 +0000 | [diff] [blame] | 377 | DEBUG(dbgs() << "Ifcvt (Simple" << (Kind == ICSimpleFalse ? |
| 378 | " false" : "") |
Bill Wendling | fb36316 | 2009-08-22 20:11:17 +0000 | [diff] [blame] | 379 | << "): BB#" << BBI.BB->getNumber() << " (" |
| 380 | << ((Kind == ICSimpleFalse) |
| 381 | ? BBI.FalseBB->getNumber() |
| 382 | : BBI.TrueBB->getNumber()) << ") "); |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 383 | RetVal = IfConvertSimple(BBI, Kind); |
David Greene | 72e47cd | 2010-01-04 22:02:01 +0000 | [diff] [blame] | 384 | DEBUG(dbgs() << (RetVal ? "succeeded!" : "failed!") << "\n"); |
Anton Korobeynikov | 035eaac | 2008-02-20 11:10:28 +0000 | [diff] [blame] | 385 | if (RetVal) { |
Dan Gohman | d2d1ae1 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 386 | if (isFalse) ++NumSimpleFalse; |
| 387 | else ++NumSimple; |
Anton Korobeynikov | 035eaac | 2008-02-20 11:10:28 +0000 | [diff] [blame] | 388 | } |
Evan Cheng | 312b723 | 2007-06-04 06:47:22 +0000 | [diff] [blame] | 389 | break; |
Evan Cheng | b30a894 | 2007-06-06 01:12:44 +0000 | [diff] [blame] | 390 | } |
Evan Cheng | d0e6691 | 2007-05-23 07:23:16 +0000 | [diff] [blame] | 391 | case ICTriangle: |
Evan Cheng | 9acfa7b | 2007-06-12 23:54:05 +0000 | [diff] [blame] | 392 | case ICTriangleRev: |
Evan Cheng | 2117d1f | 2007-06-09 01:03:43 +0000 | [diff] [blame] | 393 | case ICTriangleFalse: |
Reid Spencer | 14b62a5 | 2007-06-10 00:19:17 +0000 | [diff] [blame] | 394 | case ICTriangleFRev: { |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 395 | bool isFalse = Kind == ICTriangleFalse; |
| 396 | bool isRev = (Kind == ICTriangleRev || Kind == ICTriangleFRev); |
Evan Cheng | 9acfa7b | 2007-06-12 23:54:05 +0000 | [diff] [blame] | 397 | if (DisableTriangle && !isFalse && !isRev) break; |
| 398 | if (DisableTriangleR && !isFalse && isRev) break; |
| 399 | if (DisableTriangleF && isFalse && !isRev) break; |
| 400 | if (DisableTriangleFR && isFalse && isRev) break; |
David Greene | 72e47cd | 2010-01-04 22:02:01 +0000 | [diff] [blame] | 401 | DEBUG(dbgs() << "Ifcvt (Triangle"); |
Evan Cheng | 2117d1f | 2007-06-09 01:03:43 +0000 | [diff] [blame] | 402 | if (isFalse) |
David Greene | 72e47cd | 2010-01-04 22:02:01 +0000 | [diff] [blame] | 403 | DEBUG(dbgs() << " false"); |
Evan Cheng | 9acfa7b | 2007-06-12 23:54:05 +0000 | [diff] [blame] | 404 | if (isRev) |
David Greene | 72e47cd | 2010-01-04 22:02:01 +0000 | [diff] [blame] | 405 | DEBUG(dbgs() << " rev"); |
| 406 | DEBUG(dbgs() << "): BB#" << BBI.BB->getNumber() << " (T:" |
Bill Wendling | fb36316 | 2009-08-22 20:11:17 +0000 | [diff] [blame] | 407 | << BBI.TrueBB->getNumber() << ",F:" |
| 408 | << BBI.FalseBB->getNumber() << ") "); |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 409 | RetVal = IfConvertTriangle(BBI, Kind); |
David Greene | 72e47cd | 2010-01-04 22:02:01 +0000 | [diff] [blame] | 410 | DEBUG(dbgs() << (RetVal ? "succeeded!" : "failed!") << "\n"); |
Evan Cheng | 2117d1f | 2007-06-09 01:03:43 +0000 | [diff] [blame] | 411 | if (RetVal) { |
Evan Cheng | 9acfa7b | 2007-06-12 23:54:05 +0000 | [diff] [blame] | 412 | if (isFalse) { |
Dan Gohman | d2d1ae1 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 413 | if (isRev) ++NumTriangleFRev; |
| 414 | else ++NumTriangleFalse; |
Evan Cheng | 9acfa7b | 2007-06-12 23:54:05 +0000 | [diff] [blame] | 415 | } else { |
Dan Gohman | d2d1ae1 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 416 | if (isRev) ++NumTriangleRev; |
| 417 | else ++NumTriangle; |
Evan Cheng | 9acfa7b | 2007-06-12 23:54:05 +0000 | [diff] [blame] | 418 | } |
Evan Cheng | 2117d1f | 2007-06-09 01:03:43 +0000 | [diff] [blame] | 419 | } |
Evan Cheng | d0e6691 | 2007-05-23 07:23:16 +0000 | [diff] [blame] | 420 | break; |
Reid Spencer | 14b62a5 | 2007-06-10 00:19:17 +0000 | [diff] [blame] | 421 | } |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 422 | case ICDiamond: { |
Evan Cheng | e93ccc0 | 2007-06-08 19:10:51 +0000 | [diff] [blame] | 423 | if (DisableDiamond) break; |
David Greene | 72e47cd | 2010-01-04 22:02:01 +0000 | [diff] [blame] | 424 | DEBUG(dbgs() << "Ifcvt (Diamond): BB#" << BBI.BB->getNumber() << " (T:" |
Bill Wendling | fb36316 | 2009-08-22 20:11:17 +0000 | [diff] [blame] | 425 | << BBI.TrueBB->getNumber() << ",F:" |
| 426 | << BBI.FalseBB->getNumber() << ") "); |
Kyle Butt | 6262ca3 | 2016-08-24 21:34:24 +0000 | [diff] [blame^] | 427 | RetVal = IfConvertDiamond(BBI, Kind, NumDups, NumDups2, |
| 428 | Token->TClobbersPred, |
| 429 | Token->FClobbersPred); |
David Greene | 72e47cd | 2010-01-04 22:02:01 +0000 | [diff] [blame] | 430 | DEBUG(dbgs() << (RetVal ? "succeeded!" : "failed!") << "\n"); |
Dan Gohman | d2d1ae1 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 431 | if (RetVal) ++NumDiamonds; |
Evan Cheng | d0e6691 | 2007-05-23 07:23:16 +0000 | [diff] [blame] | 432 | break; |
| 433 | } |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Evan Cheng | 312b723 | 2007-06-04 06:47:22 +0000 | [diff] [blame] | 436 | Change |= RetVal; |
Evan Cheng | e93ccc0 | 2007-06-08 19:10:51 +0000 | [diff] [blame] | 437 | |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 438 | NumIfCvts = NumSimple + NumSimpleFalse + NumTriangle + NumTriangleRev + |
| 439 | NumTriangleFalse + NumTriangleFRev + NumDiamonds; |
| 440 | if (IfCvtLimit != -1 && (int)NumIfCvts >= IfCvtLimit) |
Evan Cheng | e93ccc0 | 2007-06-08 19:10:51 +0000 | [diff] [blame] | 441 | break; |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 442 | } |
Evan Cheng | d0e6691 | 2007-05-23 07:23:16 +0000 | [diff] [blame] | 443 | |
Evan Cheng | d0e6691 | 2007-05-23 07:23:16 +0000 | [diff] [blame] | 444 | if (!Change) |
| 445 | break; |
Evan Cheng | 312b723 | 2007-06-04 06:47:22 +0000 | [diff] [blame] | 446 | MadeChange |= Change; |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 447 | } |
Evan Cheng | 478b805 | 2007-05-18 01:55:58 +0000 | [diff] [blame] | 448 | |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 449 | Tokens.clear(); |
Evan Cheng | 478b805 | 2007-05-18 01:55:58 +0000 | [diff] [blame] | 450 | BBAnalysis.clear(); |
| 451 | |
Evan Cheng | cf9e8a9 | 2010-06-18 22:17:13 +0000 | [diff] [blame] | 452 | if (MadeChange && IfCvtBranchFold) { |
Haicheng Wu | 5b458cc | 2016-06-09 15:24:29 +0000 | [diff] [blame] | 453 | BranchFolder BF(false, false, MBFI, *MBPI); |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 454 | BF.OptimizeFunction(MF, TII, MF.getSubtarget().getRegisterInfo(), |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 455 | getAnalysisIfAvailable<MachineModuleInfo>()); |
| 456 | } |
| 457 | |
Evan Cheng | 2d51c7c | 2010-06-18 23:09:54 +0000 | [diff] [blame] | 458 | MadeChange |= BFChange; |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 459 | return MadeChange; |
| 460 | } |
| 461 | |
Matthias Braun | 2c93179 | 2016-08-17 02:51:57 +0000 | [diff] [blame] | 462 | /// BB has a fallthrough. Find its 'false' successor given its 'true' successor. |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 463 | static MachineBasicBlock *findFalseBlock(MachineBasicBlock *BB, |
Evan Cheng | 0f745da | 2007-05-18 00:20:58 +0000 | [diff] [blame] | 464 | MachineBasicBlock *TrueBB) { |
Matthias Braun | b1e0558 | 2016-08-17 02:51:59 +0000 | [diff] [blame] | 465 | for (MachineBasicBlock *SuccBB : BB->successors()) { |
Evan Cheng | 0f745da | 2007-05-18 00:20:58 +0000 | [diff] [blame] | 466 | if (SuccBB != TrueBB) |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 467 | return SuccBB; |
| 468 | } |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 469 | return nullptr; |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 470 | } |
| 471 | |
Matthias Braun | 2c93179 | 2016-08-17 02:51:57 +0000 | [diff] [blame] | 472 | /// Reverse the condition of the end of the block branch. Swap block's 'true' |
| 473 | /// and 'false' successors. |
Kyle Butt | 59f2a2a | 2016-07-27 20:19:31 +0000 | [diff] [blame] | 474 | bool IfConverter::ReverseBranchCondition(BBInfo &BBI) const { |
Stuart Hastings | 0125b64 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 475 | DebugLoc dl; // FIXME: this is nowhere |
Evan Cheng | 312b723 | 2007-06-04 06:47:22 +0000 | [diff] [blame] | 476 | if (!TII->ReverseBranchCondition(BBI.BrCond)) { |
| 477 | TII->RemoveBranch(*BBI.BB); |
Stuart Hastings | 0125b64 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 478 | TII->InsertBranch(*BBI.BB, BBI.FalseBB, BBI.TrueBB, BBI.BrCond, dl); |
Evan Cheng | 312b723 | 2007-06-04 06:47:22 +0000 | [diff] [blame] | 479 | std::swap(BBI.TrueBB, BBI.FalseBB); |
| 480 | return true; |
| 481 | } |
| 482 | return false; |
| 483 | } |
| 484 | |
Matthias Braun | 2c93179 | 2016-08-17 02:51:57 +0000 | [diff] [blame] | 485 | /// Returns the next block in the function blocks ordering. If it is the end, |
| 486 | /// returns NULL. |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 487 | static inline MachineBasicBlock *getNextBlock(MachineBasicBlock &MBB) { |
| 488 | MachineFunction::iterator I = MBB.getIterator(); |
| 489 | MachineFunction::iterator E = MBB.getParent()->end(); |
Evan Cheng | 2117d1f | 2007-06-09 01:03:43 +0000 | [diff] [blame] | 490 | if (++I == E) |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 491 | return nullptr; |
Duncan P. N. Exon Smith | 5ae5939 | 2015-10-09 19:13:58 +0000 | [diff] [blame] | 492 | return &*I; |
Evan Cheng | 2117d1f | 2007-06-09 01:03:43 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Matthias Braun | 2c93179 | 2016-08-17 02:51:57 +0000 | [diff] [blame] | 495 | /// Returns true if the 'true' block (along with its predecessor) forms a valid |
| 496 | /// simple shape for ifcvt. It also returns the number of instructions that the |
| 497 | /// ifcvt would need to duplicate if performed in Dups. |
Owen Anderson | 1b35f4c | 2010-09-28 20:42:15 +0000 | [diff] [blame] | 498 | bool IfConverter::ValidSimple(BBInfo &TrueBBI, unsigned &Dups, |
Cong Hou | c536bd9 | 2015-09-10 23:10:42 +0000 | [diff] [blame] | 499 | BranchProbability Prediction) const { |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 500 | Dups = 0; |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 501 | if (TrueBBI.IsBeingAnalyzed || TrueBBI.IsDone) |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 502 | return false; |
| 503 | |
Evan Cheng | a955c02 | 2007-06-19 21:45:13 +0000 | [diff] [blame] | 504 | if (TrueBBI.IsBrAnalyzable) |
| 505 | return false; |
| 506 | |
Evan Cheng | 23402fc | 2007-06-15 21:18:05 +0000 | [diff] [blame] | 507 | if (TrueBBI.BB->pred_size() > 1) { |
| 508 | if (TrueBBI.CannotBeCopied || |
Owen Anderson | 1b35f4c | 2010-09-28 20:42:15 +0000 | [diff] [blame] | 509 | !TII->isProfitableToDupForIfCvt(*TrueBBI.BB, TrueBBI.NonPredSize, |
Jakub Staszak | 9b07c0a | 2011-07-10 02:58:07 +0000 | [diff] [blame] | 510 | Prediction)) |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 511 | return false; |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 512 | Dups = TrueBBI.NonPredSize; |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 513 | } |
| 514 | |
Evan Cheng | a955c02 | 2007-06-19 21:45:13 +0000 | [diff] [blame] | 515 | return true; |
Evan Cheng | 9030b98 | 2007-06-06 10:16:17 +0000 | [diff] [blame] | 516 | } |
| 517 | |
Matthias Braun | 2c93179 | 2016-08-17 02:51:57 +0000 | [diff] [blame] | 518 | /// Returns true if the 'true' and 'false' blocks (along with their common |
| 519 | /// predecessor) forms a valid triangle shape for ifcvt. If 'FalseBranch' is |
| 520 | /// true, it checks if 'true' block's false branch branches to the 'false' block |
| 521 | /// rather than the other way around. It also returns the number of instructions |
| 522 | /// that the ifcvt would need to duplicate if performed in 'Dups'. |
Evan Cheng | 7783f82 | 2007-06-08 09:36:04 +0000 | [diff] [blame] | 523 | bool IfConverter::ValidTriangle(BBInfo &TrueBBI, BBInfo &FalseBBI, |
Owen Anderson | f31f33e | 2010-10-01 22:45:50 +0000 | [diff] [blame] | 524 | bool FalseBranch, unsigned &Dups, |
Cong Hou | c536bd9 | 2015-09-10 23:10:42 +0000 | [diff] [blame] | 525 | BranchProbability Prediction) const { |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 526 | Dups = 0; |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 527 | if (TrueBBI.IsBeingAnalyzed || TrueBBI.IsDone) |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 528 | return false; |
| 529 | |
Evan Cheng | 23402fc | 2007-06-15 21:18:05 +0000 | [diff] [blame] | 530 | if (TrueBBI.BB->pred_size() > 1) { |
| 531 | if (TrueBBI.CannotBeCopied) |
| 532 | return false; |
| 533 | |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 534 | unsigned Size = TrueBBI.NonPredSize; |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 535 | if (TrueBBI.IsBrAnalyzable) { |
Dan Gohman | 70de4cb | 2008-01-29 13:02:09 +0000 | [diff] [blame] | 536 | if (TrueBBI.TrueBB && TrueBBI.BrCond.empty()) |
Bob Wilson | 2371f4f | 2009-05-13 23:25:24 +0000 | [diff] [blame] | 537 | // Ends with an unconditional branch. It will be removed. |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 538 | --Size; |
| 539 | else { |
| 540 | MachineBasicBlock *FExit = FalseBranch |
| 541 | ? TrueBBI.TrueBB : TrueBBI.FalseBB; |
| 542 | if (FExit) |
| 543 | // Require a conditional branch |
| 544 | ++Size; |
| 545 | } |
| 546 | } |
Jakub Staszak | 9b07c0a | 2011-07-10 02:58:07 +0000 | [diff] [blame] | 547 | if (!TII->isProfitableToDupForIfCvt(*TrueBBI.BB, Size, Prediction)) |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 548 | return false; |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 549 | Dups = Size; |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 550 | } |
Evan Cheng | be9859e | 2007-06-07 02:12:15 +0000 | [diff] [blame] | 551 | |
Evan Cheng | 7783f82 | 2007-06-08 09:36:04 +0000 | [diff] [blame] | 552 | MachineBasicBlock *TExit = FalseBranch ? TrueBBI.FalseBB : TrueBBI.TrueBB; |
| 553 | if (!TExit && blockAlwaysFallThrough(TrueBBI)) { |
Duncan P. N. Exon Smith | 5ae5939 | 2015-10-09 19:13:58 +0000 | [diff] [blame] | 554 | MachineFunction::iterator I = TrueBBI.BB->getIterator(); |
Evan Cheng | be9859e | 2007-06-07 02:12:15 +0000 | [diff] [blame] | 555 | if (++I == TrueBBI.BB->getParent()->end()) |
| 556 | return false; |
Duncan P. N. Exon Smith | 5ae5939 | 2015-10-09 19:13:58 +0000 | [diff] [blame] | 557 | TExit = &*I; |
Evan Cheng | be9859e | 2007-06-07 02:12:15 +0000 | [diff] [blame] | 558 | } |
Evan Cheng | 7783f82 | 2007-06-08 09:36:04 +0000 | [diff] [blame] | 559 | return TExit && TExit == FalseBBI.BB; |
Evan Cheng | be9859e | 2007-06-07 02:12:15 +0000 | [diff] [blame] | 560 | } |
| 561 | |
Matthias Braun | 2c93179 | 2016-08-17 02:51:57 +0000 | [diff] [blame] | 562 | /// Increment \p It until it points to a non-debug instruction or to \p End. |
Kyle Butt | fe91682 | 2016-08-06 01:52:31 +0000 | [diff] [blame] | 563 | /// @param It Iterator to increment |
| 564 | /// @param End Iterator that points to end. Will be compared to It |
| 565 | /// @returns true if It == End, false otherwise. |
| 566 | static inline bool skipDebugInstructionsForward( |
| 567 | MachineBasicBlock::iterator &It, |
| 568 | MachineBasicBlock::iterator &End) { |
| 569 | while (It != End && It->isDebugValue()) |
| 570 | It++; |
| 571 | return It == End; |
| 572 | } |
| 573 | |
Kyle Butt | d76755e | 2016-08-18 22:09:23 +0000 | [diff] [blame] | 574 | /// Shrink the provided inclusive range by one instruction. |
| 575 | /// If the range was one instruction (\p It == \p Begin), It is not modified, |
| 576 | /// but \p Empty is set to true. |
| 577 | static inline void shrinkInclusiveRange( |
| 578 | MachineBasicBlock::iterator &Begin, |
| 579 | MachineBasicBlock::iterator &It, |
| 580 | bool &Empty) { |
| 581 | if (It == Begin) |
| 582 | Empty = true; |
| 583 | else |
| 584 | It--; |
| 585 | } |
| 586 | |
| 587 | /// Decrement \p It until it points to a non-debug instruction or the range is |
| 588 | /// empty. |
Kyle Butt | fe91682 | 2016-08-06 01:52:31 +0000 | [diff] [blame] | 589 | /// @param It Iterator to decrement. |
David Majnemer | 70c93fa | 2016-08-06 08:37:12 +0000 | [diff] [blame] | 590 | /// @param Begin Iterator that points to beginning. Will be compared to It |
Kyle Butt | d76755e | 2016-08-18 22:09:23 +0000 | [diff] [blame] | 591 | /// @param Empty Set to true if the resulting range is Empty |
| 592 | /// @returns the value of Empty as a convenience. |
Kyle Butt | fe91682 | 2016-08-06 01:52:31 +0000 | [diff] [blame] | 593 | static inline bool skipDebugInstructionsBackward( |
Kyle Butt | d76755e | 2016-08-18 22:09:23 +0000 | [diff] [blame] | 594 | MachineBasicBlock::iterator &Begin, |
Kyle Butt | fe91682 | 2016-08-06 01:52:31 +0000 | [diff] [blame] | 595 | MachineBasicBlock::iterator &It, |
Kyle Butt | d76755e | 2016-08-18 22:09:23 +0000 | [diff] [blame] | 596 | bool &Empty) { |
| 597 | while (!Empty && It->isDebugValue()) |
| 598 | shrinkInclusiveRange(Begin, It, Empty); |
| 599 | return Empty; |
Kyle Butt | fe91682 | 2016-08-06 01:52:31 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Kyle Butt | 4f0e287 | 2016-08-06 01:52:33 +0000 | [diff] [blame] | 602 | /// Count duplicated instructions and move the iterators to show where they |
| 603 | /// are. |
| 604 | /// @param TIB True Iterator Begin |
| 605 | /// @param FIB False Iterator Begin |
| 606 | /// These two iterators initially point to the first instruction of the two |
| 607 | /// blocks, and finally point to the first non-shared instruction. |
| 608 | /// @param TIE True Iterator End |
| 609 | /// @param FIE False Iterator End |
| 610 | /// These two iterators initially point to End() for the two blocks() and |
| 611 | /// finally point to the first shared instruction in the tail. |
| 612 | /// Upon return [TIB, TIE), and [FIB, FIE) mark the un-duplicated portions of |
| 613 | /// two blocks. |
Kyle Butt | 6262ca3 | 2016-08-24 21:34:24 +0000 | [diff] [blame^] | 614 | /// @param Dups1 count of duplicated instructions at the beginning of the 2 |
| 615 | /// blocks. |
| 616 | /// @param Dups2 count of duplicated instructions at the end of the 2 blocks. |
| 617 | /// @param SkipUnconditionalBranches if true, Don't make sure that |
| 618 | /// unconditional branches at the end of the blocks are the same. True is |
| 619 | /// passed when the blocks are analyzable to allow for fallthrough to be |
| 620 | /// handled. |
| 621 | /// @return false if the shared portion prevents if conversion. |
| 622 | bool IfConverter::CountDuplicatedInstructions( |
Kyle Butt | 9b6d99b | 2016-07-27 20:19:33 +0000 | [diff] [blame] | 623 | MachineBasicBlock::iterator &TIB, |
| 624 | MachineBasicBlock::iterator &FIB, |
| 625 | MachineBasicBlock::iterator &TIE, |
| 626 | MachineBasicBlock::iterator &FIE, |
| 627 | unsigned &Dups1, unsigned &Dups2, |
| 628 | MachineBasicBlock &TBB, MachineBasicBlock &FBB, |
Kyle Butt | 6262ca3 | 2016-08-24 21:34:24 +0000 | [diff] [blame^] | 629 | bool SkipUnconditionalBranches) const { |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 630 | |
Bob Wilson | e1961fe | 2010-10-26 00:02:24 +0000 | [diff] [blame] | 631 | while (TIB != TIE && FIB != FIE) { |
Evan Cheng | c0e0d85 | 2010-06-18 21:52:57 +0000 | [diff] [blame] | 632 | // Skip dbg_value instructions. These do not count. |
Kyle Butt | fe91682 | 2016-08-06 01:52:31 +0000 | [diff] [blame] | 633 | if(skipDebugInstructionsForward(TIB, TIE)) |
| 634 | break; |
| 635 | if(skipDebugInstructionsForward(FIB, FIE)) |
| 636 | break; |
Duncan P. N. Exon Smith | fd8cc23 | 2016-02-27 20:01:33 +0000 | [diff] [blame] | 637 | if (!TIB->isIdenticalTo(*FIB)) |
Bob Wilson | e1961fe | 2010-10-26 00:02:24 +0000 | [diff] [blame] | 638 | break; |
Kyle Butt | 6262ca3 | 2016-08-24 21:34:24 +0000 | [diff] [blame^] | 639 | // A pred-clobbering instruction in the shared portion prevents |
| 640 | // if-conversion. |
| 641 | std::vector<MachineOperand> PredDefs; |
| 642 | if (TII->DefinesPredicate(*TIB, PredDefs)) |
| 643 | return false; |
Bob Wilson | e1961fe | 2010-10-26 00:02:24 +0000 | [diff] [blame] | 644 | ++Dups1; |
| 645 | ++TIB; |
| 646 | ++FIB; |
| 647 | } |
| 648 | |
Kyle Butt | ce0196d | 2016-08-19 18:17:04 +0000 | [diff] [blame] | 649 | // If both blocks are returning don't skip the branches, since they will |
| 650 | // likely be both identical return instructions. In such cases the return |
| 651 | // can be left unpredicated. |
Krzysztof Parzyszek | 2451c48 | 2016-01-20 13:14:52 +0000 | [diff] [blame] | 652 | // Check for already containing all of the block. |
| 653 | if (TIB == TIE || FIB == FIE) |
Kyle Butt | 6262ca3 | 2016-08-24 21:34:24 +0000 | [diff] [blame^] | 654 | return true; |
Kyle Butt | d76755e | 2016-08-18 22:09:23 +0000 | [diff] [blame] | 655 | // Now, in preparation for counting duplicate instructions at the ends of the |
| 656 | // blocks, move the end iterators up past any branch instructions. |
Krzysztof Parzyszek | 2451c48 | 2016-01-20 13:14:52 +0000 | [diff] [blame] | 657 | --TIE; |
| 658 | --FIE; |
Kyle Butt | d76755e | 2016-08-18 22:09:23 +0000 | [diff] [blame] | 659 | |
| 660 | // After this point TIB and TIE define an inclusive range, which means that |
| 661 | // TIB == TIE is true when there is one more instruction to consider, not at |
| 662 | // the end. Because we may not be able to go before TIB, we need a flag to |
| 663 | // indicate a completely empty range. |
| 664 | bool TEmpty = false, FEmpty = false; |
| 665 | |
| 666 | // Upon exit TIE and FIE will both point at the last non-shared instruction. |
| 667 | // They need to be moved forward to point past the last non-shared |
| 668 | // instruction if the range they delimit is non-empty. |
| 669 | auto IncrementEndIteratorsOnExit = make_scope_exit([&]() { |
| 670 | if (!TEmpty) |
| 671 | ++TIE; |
| 672 | if (!FEmpty) |
| 673 | ++FIE; |
| 674 | }); |
| 675 | |
Kyle Butt | 9b6d99b | 2016-07-27 20:19:33 +0000 | [diff] [blame] | 676 | if (!TBB.succ_empty() || !FBB.succ_empty()) { |
Kyle Butt | 6262ca3 | 2016-08-24 21:34:24 +0000 | [diff] [blame^] | 677 | if (SkipUnconditionalBranches) { |
Kyle Butt | d76755e | 2016-08-18 22:09:23 +0000 | [diff] [blame] | 678 | while (!TEmpty && TIE->isUnconditionalBranch()) |
| 679 | shrinkInclusiveRange(TIB, TIE, TEmpty); |
| 680 | while (!FEmpty && FIE->isUnconditionalBranch()) |
| 681 | shrinkInclusiveRange(FIB, FIE, FEmpty); |
Kyle Butt | 9b6d99b | 2016-07-27 20:19:33 +0000 | [diff] [blame] | 682 | } |
Bob Wilson | e1961fe | 2010-10-26 00:02:24 +0000 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | // If Dups1 includes all of a block, then don't count duplicate |
| 686 | // instructions at the end of the blocks. |
Kyle Butt | d76755e | 2016-08-18 22:09:23 +0000 | [diff] [blame] | 687 | if (TEmpty || FEmpty) |
Kyle Butt | 6262ca3 | 2016-08-24 21:34:24 +0000 | [diff] [blame^] | 688 | return true; |
Bob Wilson | e1961fe | 2010-10-26 00:02:24 +0000 | [diff] [blame] | 689 | |
| 690 | // Count duplicate instructions at the ends of the blocks. |
Kyle Butt | d76755e | 2016-08-18 22:09:23 +0000 | [diff] [blame] | 691 | while (!TEmpty && !FEmpty) { |
Bob Wilson | e1961fe | 2010-10-26 00:02:24 +0000 | [diff] [blame] | 692 | // Skip dbg_value instructions. These do not count. |
Kyle Butt | d76755e | 2016-08-18 22:09:23 +0000 | [diff] [blame] | 693 | if (skipDebugInstructionsBackward(TIB, TIE, TEmpty)) |
Kyle Butt | fe91682 | 2016-08-06 01:52:31 +0000 | [diff] [blame] | 694 | break; |
Kyle Butt | d76755e | 2016-08-18 22:09:23 +0000 | [diff] [blame] | 695 | if (skipDebugInstructionsBackward(FIB, FIE, FEmpty)) |
Kyle Butt | fe91682 | 2016-08-06 01:52:31 +0000 | [diff] [blame] | 696 | break; |
Duncan P. N. Exon Smith | fd8cc23 | 2016-02-27 20:01:33 +0000 | [diff] [blame] | 697 | if (!TIE->isIdenticalTo(*FIE)) |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 698 | break; |
Kyle Butt | 6262ca3 | 2016-08-24 21:34:24 +0000 | [diff] [blame^] | 699 | // We have to verify that any branch instructions are the same, and then we |
| 700 | // don't count them toward the # of duplicate instructions. |
| 701 | if (!TIE->isBranch()) |
Kyle Butt | 9b6d99b | 2016-07-27 20:19:33 +0000 | [diff] [blame] | 702 | ++Dups2; |
Kyle Butt | d76755e | 2016-08-18 22:09:23 +0000 | [diff] [blame] | 703 | shrinkInclusiveRange(TIB, TIE, TEmpty); |
| 704 | shrinkInclusiveRange(FIB, FIE, FEmpty); |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 705 | } |
Kyle Butt | 6262ca3 | 2016-08-24 21:34:24 +0000 | [diff] [blame^] | 706 | return true; |
| 707 | } |
| 708 | |
| 709 | /// RescanInstructions - Run ScanInstructions on a pair of blocks. |
| 710 | /// @param TIB - True Iterator Begin, points to first non-shared instruction |
| 711 | /// @param FIB - False Iterator Begin, points to first non-shared instruction |
| 712 | /// @param TIE - True Iterator End, points past last non-shared instruction |
| 713 | /// @param FIE - False Iterator End, points past last non-shared instruction |
| 714 | /// @param TrueBBI - BBInfo to update for the true block. |
| 715 | /// @param FalseBBI - BBInfo to update for the false block. |
| 716 | /// @returns - false if either block cannot be predicated or if both blocks end |
| 717 | /// with a predicate-clobbering instruction. |
| 718 | bool IfConverter::RescanInstructions( |
| 719 | MachineBasicBlock::iterator &TIB, MachineBasicBlock::iterator &FIB, |
| 720 | MachineBasicBlock::iterator &TIE, MachineBasicBlock::iterator &FIE, |
| 721 | BBInfo &TrueBBI, BBInfo &FalseBBI) const { |
| 722 | bool BranchUnpredicable = true; |
| 723 | TrueBBI.IsUnpredicable = FalseBBI.IsUnpredicable = false; |
| 724 | ScanInstructions(TrueBBI, TIB, TIE, BranchUnpredicable); |
| 725 | if (TrueBBI.IsUnpredicable) |
| 726 | return false; |
| 727 | ScanInstructions(FalseBBI, FIB, FIE, BranchUnpredicable); |
| 728 | if (FalseBBI.IsUnpredicable) |
| 729 | return false; |
| 730 | if (TrueBBI.ClobbersPred && FalseBBI.ClobbersPred) |
| 731 | return false; |
| 732 | return true; |
Kyle Butt | 9b6d99b | 2016-07-27 20:19:33 +0000 | [diff] [blame] | 733 | } |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 734 | |
Kyle Butt | 9b6d99b | 2016-07-27 20:19:33 +0000 | [diff] [blame] | 735 | /// ValidDiamond - Returns true if the 'true' and 'false' blocks (along |
| 736 | /// with their common predecessor) forms a valid diamond shape for ifcvt. |
Kyle Butt | 6262ca3 | 2016-08-24 21:34:24 +0000 | [diff] [blame^] | 737 | bool IfConverter::ValidDiamond( |
| 738 | BBInfo &TrueBBI, BBInfo &FalseBBI, |
| 739 | unsigned &Dups1, unsigned &Dups2, |
| 740 | BBInfo &TrueBBICalc, BBInfo &FalseBBICalc) const { |
Kyle Butt | 9b6d99b | 2016-07-27 20:19:33 +0000 | [diff] [blame] | 741 | Dups1 = Dups2 = 0; |
| 742 | if (TrueBBI.IsBeingAnalyzed || TrueBBI.IsDone || |
| 743 | FalseBBI.IsBeingAnalyzed || FalseBBI.IsDone) |
| 744 | return false; |
| 745 | |
| 746 | MachineBasicBlock *TT = TrueBBI.TrueBB; |
| 747 | MachineBasicBlock *FT = FalseBBI.TrueBB; |
| 748 | |
| 749 | if (!TT && blockAlwaysFallThrough(TrueBBI)) |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 750 | TT = getNextBlock(*TrueBBI.BB); |
Kyle Butt | 9b6d99b | 2016-07-27 20:19:33 +0000 | [diff] [blame] | 751 | if (!FT && blockAlwaysFallThrough(FalseBBI)) |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 752 | FT = getNextBlock(*FalseBBI.BB); |
Kyle Butt | 9b6d99b | 2016-07-27 20:19:33 +0000 | [diff] [blame] | 753 | if (TT != FT) |
| 754 | return false; |
| 755 | if (!TT && (TrueBBI.IsBrAnalyzable || FalseBBI.IsBrAnalyzable)) |
| 756 | return false; |
| 757 | if (TrueBBI.BB->pred_size() > 1 || FalseBBI.BB->pred_size() > 1) |
| 758 | return false; |
| 759 | |
| 760 | // FIXME: Allow true block to have an early exit? |
Kyle Butt | 6262ca3 | 2016-08-24 21:34:24 +0000 | [diff] [blame^] | 761 | if (TrueBBI.FalseBB || FalseBBI.FalseBB) |
Kyle Butt | 9b6d99b | 2016-07-27 20:19:33 +0000 | [diff] [blame] | 762 | return false; |
| 763 | |
| 764 | // Count duplicate instructions at the beginning and end of the true and |
| 765 | // false blocks. |
Kyle Butt | 6262ca3 | 2016-08-24 21:34:24 +0000 | [diff] [blame^] | 766 | // Skip unconditional branches only if we are considering an analyzable |
| 767 | // diamond. Otherwise the branches must be the same. |
| 768 | bool SkipUnconditionalBranches = |
| 769 | TrueBBI.IsBrAnalyzable && FalseBBI.IsBrAnalyzable; |
Kyle Butt | 9b6d99b | 2016-07-27 20:19:33 +0000 | [diff] [blame] | 770 | MachineBasicBlock::iterator TIB = TrueBBI.BB->begin(); |
| 771 | MachineBasicBlock::iterator FIB = FalseBBI.BB->begin(); |
| 772 | MachineBasicBlock::iterator TIE = TrueBBI.BB->end(); |
| 773 | MachineBasicBlock::iterator FIE = FalseBBI.BB->end(); |
Kyle Butt | 6262ca3 | 2016-08-24 21:34:24 +0000 | [diff] [blame^] | 774 | if(!CountDuplicatedInstructions(TIB, FIB, TIE, FIE, Dups1, Dups2, |
| 775 | *TrueBBI.BB, *FalseBBI.BB, |
| 776 | SkipUnconditionalBranches)) |
| 777 | return false; |
| 778 | |
| 779 | TrueBBICalc.BB = TrueBBI.BB; |
| 780 | FalseBBICalc.BB = FalseBBI.BB; |
| 781 | if (!RescanInstructions(TIB, FIB, TIE, FIE, TrueBBICalc, FalseBBICalc)) |
| 782 | return false; |
| 783 | // The size is used to decide whether to if-convert, and the shared portions |
| 784 | // are subtracted off. Because of the subtraction, we just use the size that |
| 785 | // was calculated by the original ScanInstructions, as it is correct. |
| 786 | TrueBBICalc.NonPredSize = TrueBBI.NonPredSize; |
| 787 | FalseBBICalc.NonPredSize = FalseBBI.NonPredSize; |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 788 | return true; |
Evan Cheng | be9859e | 2007-06-07 02:12:15 +0000 | [diff] [blame] | 789 | } |
| 790 | |
Kyle Butt | 54bf3ce | 2016-08-06 01:52:34 +0000 | [diff] [blame] | 791 | /// AnalyzeBranches - Look at the branches at the end of a block to determine if |
| 792 | /// the block is predicable. |
| 793 | void IfConverter::AnalyzeBranches(BBInfo &BBI) { |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 794 | if (BBI.IsDone) |
| 795 | return; |
| 796 | |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 797 | BBI.TrueBB = BBI.FalseBB = nullptr; |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 798 | BBI.BrCond.clear(); |
| 799 | BBI.IsBrAnalyzable = |
Jacques Pienaar | 71c30a1 | 2016-07-15 14:41:04 +0000 | [diff] [blame] | 800 | !TII->analyzeBranch(*BBI.BB, BBI.TrueBB, BBI.FalseBB, BBI.BrCond); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 801 | BBI.HasFallThrough = BBI.IsBrAnalyzable && BBI.FalseBB == nullptr; |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 802 | |
| 803 | if (BBI.BrCond.size()) { |
| 804 | // No false branch. This BB must end with a conditional branch and a |
| 805 | // fallthrough. |
| 806 | if (!BBI.FalseBB) |
Bob Wilson | 8105144 | 2010-06-15 22:18:54 +0000 | [diff] [blame] | 807 | BBI.FalseBB = findFalseBlock(BBI.BB, BBI.TrueBB); |
Evan Cheng | b9bff58 | 2009-06-15 21:24:34 +0000 | [diff] [blame] | 808 | if (!BBI.FalseBB) { |
| 809 | // Malformed bcc? True and false blocks are the same? |
| 810 | BBI.IsUnpredicable = true; |
Evan Cheng | b9bff58 | 2009-06-15 21:24:34 +0000 | [diff] [blame] | 811 | } |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 812 | } |
Kyle Butt | 54bf3ce | 2016-08-06 01:52:34 +0000 | [diff] [blame] | 813 | } |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 814 | |
Kyle Butt | 54bf3ce | 2016-08-06 01:52:34 +0000 | [diff] [blame] | 815 | /// ScanInstructions - Scan all the instructions in the block to determine if |
| 816 | /// the block is predicable. In most cases, that means all the instructions |
| 817 | /// in the block are isPredicable(). Also checks if the block contains any |
| 818 | /// instruction which can clobber a predicate (e.g. condition code register). |
| 819 | /// If so, the block is not predicable unless it's the last instruction. |
| 820 | void IfConverter::ScanInstructions(BBInfo &BBI, |
| 821 | MachineBasicBlock::iterator &Begin, |
Kyle Butt | 6262ca3 | 2016-08-24 21:34:24 +0000 | [diff] [blame^] | 822 | MachineBasicBlock::iterator &End, |
| 823 | bool BranchUnpredicable) const { |
Kyle Butt | 54bf3ce | 2016-08-06 01:52:34 +0000 | [diff] [blame] | 824 | if (BBI.IsDone || BBI.IsUnpredicable) |
| 825 | return; |
| 826 | |
| 827 | bool AlreadyPredicated = !BBI.Predicate.empty(); |
| 828 | |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 829 | BBI.NonPredSize = 0; |
Bob Wilson | efd360c | 2010-10-26 00:02:21 +0000 | [diff] [blame] | 830 | BBI.ExtraCost = 0; |
Evan Cheng | debf9c5 | 2010-11-03 00:45:17 +0000 | [diff] [blame] | 831 | BBI.ExtraCost2 = 0; |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 832 | BBI.ClobbersPred = false; |
Matthias Braun | b1e0558 | 2016-08-17 02:51:59 +0000 | [diff] [blame] | 833 | for (MachineInstr &MI : make_range(Begin, End)) { |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 834 | if (MI.isDebugValue()) |
Jim Grosbach | a1e08fb | 2010-06-04 23:01:26 +0000 | [diff] [blame] | 835 | continue; |
| 836 | |
Justin Lebar | 7dba2e0 | 2016-04-15 01:38:41 +0000 | [diff] [blame] | 837 | // It's unsafe to duplicate convergent instructions in this context, so set |
| 838 | // BBI.CannotBeCopied to true if MI is convergent. To see why, consider the |
| 839 | // following CFG, which is subject to our "simple" transformation. |
| 840 | // |
| 841 | // BB0 // if (c1) goto BB1; else goto BB2; |
| 842 | // / \ |
| 843 | // BB1 | |
| 844 | // | BB2 // if (c2) goto TBB; else goto FBB; |
| 845 | // | / | |
| 846 | // | / | |
| 847 | // TBB | |
| 848 | // | | |
| 849 | // | FBB |
| 850 | // | |
| 851 | // exit |
| 852 | // |
| 853 | // Suppose we want to move TBB's contents up into BB1 and BB2 (in BB1 they'd |
| 854 | // be unconditional, and in BB2, they'd be predicated upon c2), and suppose |
| 855 | // TBB contains a convergent instruction. This is safe iff doing so does |
| 856 | // not add a control-flow dependency to the convergent instruction -- i.e., |
| 857 | // it's safe iff the set of control flows that leads us to the convergent |
| 858 | // instruction does not get smaller after the transformation. |
| 859 | // |
| 860 | // Originally we executed TBB if c1 || c2. After the transformation, there |
| 861 | // are two copies of TBB's instructions. We get to the first if c1, and we |
| 862 | // get to the second if !c1 && c2. |
| 863 | // |
| 864 | // There are clearly fewer ways to satisfy the condition "c1" than |
| 865 | // "c1 || c2". Since we've shrunk the set of control flows which lead to |
| 866 | // our convergent instruction, the transformation is unsafe. |
| 867 | if (MI.isNotDuplicable() || MI.isConvergent()) |
Evan Cheng | 23402fc | 2007-06-15 21:18:05 +0000 | [diff] [blame] | 868 | BBI.CannotBeCopied = true; |
| 869 | |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 870 | bool isPredicated = TII->isPredicated(MI); |
| 871 | bool isCondBr = BBI.IsBrAnalyzable && MI.isConditionalBranch(); |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 872 | |
Kyle Butt | 6262ca3 | 2016-08-24 21:34:24 +0000 | [diff] [blame^] | 873 | if (BranchUnpredicable && MI.isBranch()) { |
| 874 | BBI.IsUnpredicable = true; |
| 875 | return; |
| 876 | } |
| 877 | |
Joey Gouly | a5153cb | 2013-09-09 14:21:49 +0000 | [diff] [blame] | 878 | // A conditional branch is not predicable, but it may be eliminated. |
| 879 | if (isCondBr) |
| 880 | continue; |
| 881 | |
| 882 | if (!isPredicated) { |
| 883 | BBI.NonPredSize++; |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 884 | unsigned ExtraPredCost = TII->getPredicationCost(MI); |
| 885 | unsigned NumCycles = SchedModel.computeInstrLatency(&MI, false); |
Joey Gouly | a5153cb | 2013-09-09 14:21:49 +0000 | [diff] [blame] | 886 | if (NumCycles > 1) |
| 887 | BBI.ExtraCost += NumCycles-1; |
| 888 | BBI.ExtraCost2 += ExtraPredCost; |
| 889 | } else if (!AlreadyPredicated) { |
| 890 | // FIXME: This instruction is already predicated before the |
| 891 | // if-conversion pass. It's probably something like a conditional move. |
| 892 | // Mark this block unpredicable for now. |
| 893 | BBI.IsUnpredicable = true; |
| 894 | return; |
Evan Cheng | 96c1457 | 2007-07-06 23:24:39 +0000 | [diff] [blame] | 895 | } |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 896 | |
| 897 | if (BBI.ClobbersPred && !isPredicated) { |
| 898 | // Predicate modification instruction should end the block (except for |
| 899 | // already predicated instructions and end of block branches). |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 900 | // Predicate may have been modified, the subsequent (currently) |
Evan Cheng | 96c1457 | 2007-07-06 23:24:39 +0000 | [diff] [blame] | 901 | // unpredicated instructions cannot be correctly predicated. |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 902 | BBI.IsUnpredicable = true; |
| 903 | return; |
| 904 | } |
| 905 | |
Evan Cheng | fbc7309 | 2007-07-10 17:50:43 +0000 | [diff] [blame] | 906 | // FIXME: Make use of PredDefs? e.g. ADDC, SUBC sets predicates but are |
| 907 | // still potentially predicable. |
| 908 | std::vector<MachineOperand> PredDefs; |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 909 | if (TII->DefinesPredicate(MI, PredDefs)) |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 910 | BBI.ClobbersPred = true; |
| 911 | |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 912 | if (!TII->isPredicable(MI)) { |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 913 | BBI.IsUnpredicable = true; |
| 914 | return; |
| 915 | } |
| 916 | } |
| 917 | } |
| 918 | |
Matthias Braun | 2c93179 | 2016-08-17 02:51:57 +0000 | [diff] [blame] | 919 | /// Determine if the block is a suitable candidate to be predicated by the |
| 920 | /// specified predicate. |
Kyle Butt | 6262ca3 | 2016-08-24 21:34:24 +0000 | [diff] [blame^] | 921 | /// @param BBI BBInfo for the block to check |
| 922 | /// @param Pred Predicate array for the branch that leads to BBI |
| 923 | /// @param isTriangle true if the Analysis is for a triangle |
| 924 | /// @param RevBranch true if Reverse(Pred) leads to BBI (e.g. BBI is the false |
| 925 | /// case |
| 926 | /// @param hasCommonTail true if BBI shares a tail with a sibling block that |
| 927 | /// contains any instruction that would make the block unpredicable. |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 928 | bool IfConverter::FeasibilityAnalysis(BBInfo &BBI, |
Owen Anderson | 4f6bf04 | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 929 | SmallVectorImpl<MachineOperand> &Pred, |
Kyle Butt | 6262ca3 | 2016-08-24 21:34:24 +0000 | [diff] [blame^] | 930 | bool isTriangle, bool RevBranch, |
| 931 | bool hasCommonTail) { |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 932 | // If the block is dead or unpredicable, then it cannot be predicated. |
Kyle Butt | 6262ca3 | 2016-08-24 21:34:24 +0000 | [diff] [blame^] | 933 | // Two blocks may share a common unpredicable tail, but this doesn't prevent |
| 934 | // them from being if-converted. The non-shared portion is assumed to have |
| 935 | // been checked |
| 936 | if (BBI.IsDone || (BBI.IsUnpredicable && !hasCommonTail)) |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 937 | return false; |
| 938 | |
Ahmed Bougacha | 7173b66 | 2015-03-21 01:23:15 +0000 | [diff] [blame] | 939 | // If it is already predicated but we couldn't analyze its terminator, the |
| 940 | // latter might fallthrough, but we can't determine where to. |
| 941 | // Conservatively avoid if-converting again. |
| 942 | if (BBI.Predicate.size() && !BBI.IsBrAnalyzable) |
| 943 | return false; |
| 944 | |
Quentin Colombet | bdab227 | 2013-07-24 20:20:37 +0000 | [diff] [blame] | 945 | // If it is already predicated, check if the new predicate subsumes |
| 946 | // its predicate. |
| 947 | if (BBI.Predicate.size() && !TII->SubsumesPredicate(Pred, BBI.Predicate)) |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 948 | return false; |
| 949 | |
Kyle Butt | 6262ca3 | 2016-08-24 21:34:24 +0000 | [diff] [blame^] | 950 | if (!hasCommonTail && BBI.BrCond.size()) { |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 951 | if (!isTriangle) |
| 952 | return false; |
| 953 | |
Bob Wilson | 2371f4f | 2009-05-13 23:25:24 +0000 | [diff] [blame] | 954 | // Test predicate subsumption. |
Owen Anderson | 4f6bf04 | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 955 | SmallVector<MachineOperand, 4> RevPred(Pred.begin(), Pred.end()); |
| 956 | SmallVector<MachineOperand, 4> Cond(BBI.BrCond.begin(), BBI.BrCond.end()); |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 957 | if (RevBranch) { |
| 958 | if (TII->ReverseBranchCondition(Cond)) |
| 959 | return false; |
| 960 | } |
| 961 | if (TII->ReverseBranchCondition(RevPred) || |
| 962 | !TII->SubsumesPredicate(Cond, RevPred)) |
| 963 | return false; |
| 964 | } |
| 965 | |
| 966 | return true; |
| 967 | } |
| 968 | |
Matthias Braun | 2c93179 | 2016-08-17 02:51:57 +0000 | [diff] [blame] | 969 | /// Analyze the structure of the sub-CFG starting from the specified block. |
| 970 | /// Record its successors and whether it looks like an if-conversion candidate. |
Justin Lebar | 3a7bc57 | 2016-02-22 17:51:28 +0000 | [diff] [blame] | 971 | void IfConverter::AnalyzeBlock( |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 972 | MachineBasicBlock &MBB, std::vector<std::unique_ptr<IfcvtToken>> &Tokens) { |
Akira Hatanaka | 14348aa | 2015-06-24 20:34:35 +0000 | [diff] [blame] | 973 | struct BBState { |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 974 | BBState(MachineBasicBlock &MBB) : MBB(&MBB), SuccsAnalyzed(false) {} |
Akira Hatanaka | 14348aa | 2015-06-24 20:34:35 +0000 | [diff] [blame] | 975 | MachineBasicBlock *MBB; |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 976 | |
Akira Hatanaka | 14348aa | 2015-06-24 20:34:35 +0000 | [diff] [blame] | 977 | /// This flag is true if MBB's successors have been analyzed. |
| 978 | bool SuccsAnalyzed; |
| 979 | }; |
Evan Cheng | 0f745da | 2007-05-18 00:20:58 +0000 | [diff] [blame] | 980 | |
Akira Hatanaka | 14348aa | 2015-06-24 20:34:35 +0000 | [diff] [blame] | 981 | // Push MBB to the stack. |
| 982 | SmallVector<BBState, 16> BBStack(1, MBB); |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 983 | |
Akira Hatanaka | 14348aa | 2015-06-24 20:34:35 +0000 | [diff] [blame] | 984 | while (!BBStack.empty()) { |
| 985 | BBState &State = BBStack.back(); |
| 986 | MachineBasicBlock *BB = State.MBB; |
| 987 | BBInfo &BBI = BBAnalysis[BB->getNumber()]; |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 988 | |
Akira Hatanaka | 14348aa | 2015-06-24 20:34:35 +0000 | [diff] [blame] | 989 | if (!State.SuccsAnalyzed) { |
| 990 | if (BBI.IsAnalyzed || BBI.IsBeingAnalyzed) { |
| 991 | BBStack.pop_back(); |
| 992 | continue; |
| 993 | } |
Evan Cheng | 9030b98 | 2007-06-06 10:16:17 +0000 | [diff] [blame] | 994 | |
Akira Hatanaka | 14348aa | 2015-06-24 20:34:35 +0000 | [diff] [blame] | 995 | BBI.BB = BB; |
| 996 | BBI.IsBeingAnalyzed = true; |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 997 | |
Kyle Butt | 54bf3ce | 2016-08-06 01:52:34 +0000 | [diff] [blame] | 998 | AnalyzeBranches(BBI); |
| 999 | MachineBasicBlock::iterator Begin = BBI.BB->begin(); |
| 1000 | MachineBasicBlock::iterator End = BBI.BB->end(); |
| 1001 | ScanInstructions(BBI, Begin, End); |
Evan Cheng | b9bff58 | 2009-06-15 21:24:34 +0000 | [diff] [blame] | 1002 | |
Akira Hatanaka | 14348aa | 2015-06-24 20:34:35 +0000 | [diff] [blame] | 1003 | // Unanalyzable or ends with fallthrough or unconditional branch, or if is |
| 1004 | // not considered for ifcvt anymore. |
| 1005 | if (!BBI.IsBrAnalyzable || BBI.BrCond.empty() || BBI.IsDone) { |
| 1006 | BBI.IsBeingAnalyzed = false; |
| 1007 | BBI.IsAnalyzed = true; |
| 1008 | BBStack.pop_back(); |
| 1009 | continue; |
| 1010 | } |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 1011 | |
Akira Hatanaka | 14348aa | 2015-06-24 20:34:35 +0000 | [diff] [blame] | 1012 | // Do not ifcvt if either path is a back edge to the entry block. |
| 1013 | if (BBI.TrueBB == BB || BBI.FalseBB == BB) { |
| 1014 | BBI.IsBeingAnalyzed = false; |
| 1015 | BBI.IsAnalyzed = true; |
| 1016 | BBStack.pop_back(); |
| 1017 | continue; |
| 1018 | } |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 1019 | |
Akira Hatanaka | 14348aa | 2015-06-24 20:34:35 +0000 | [diff] [blame] | 1020 | // Do not ifcvt if true and false fallthrough blocks are the same. |
| 1021 | if (!BBI.FalseBB) { |
| 1022 | BBI.IsBeingAnalyzed = false; |
| 1023 | BBI.IsAnalyzed = true; |
| 1024 | BBStack.pop_back(); |
| 1025 | continue; |
| 1026 | } |
Evan Cheng | 7783f82 | 2007-06-08 09:36:04 +0000 | [diff] [blame] | 1027 | |
Akira Hatanaka | 14348aa | 2015-06-24 20:34:35 +0000 | [diff] [blame] | 1028 | // Push the False and True blocks to the stack. |
| 1029 | State.SuccsAnalyzed = true; |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1030 | BBStack.push_back(*BBI.FalseBB); |
| 1031 | BBStack.push_back(*BBI.TrueBB); |
Akira Hatanaka | 14348aa | 2015-06-24 20:34:35 +0000 | [diff] [blame] | 1032 | continue; |
| 1033 | } |
Benjamin Kramer | 2016f0e | 2010-09-29 22:38:50 +0000 | [diff] [blame] | 1034 | |
Akira Hatanaka | 14348aa | 2015-06-24 20:34:35 +0000 | [diff] [blame] | 1035 | BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()]; |
| 1036 | BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()]; |
Jakub Staszak | 15e5b74 | 2011-08-03 22:34:43 +0000 | [diff] [blame] | 1037 | |
Akira Hatanaka | 14348aa | 2015-06-24 20:34:35 +0000 | [diff] [blame] | 1038 | if (TrueBBI.IsDone && FalseBBI.IsDone) { |
| 1039 | BBI.IsBeingAnalyzed = false; |
| 1040 | BBI.IsAnalyzed = true; |
| 1041 | BBStack.pop_back(); |
| 1042 | continue; |
| 1043 | } |
| 1044 | |
| 1045 | SmallVector<MachineOperand, 4> |
| 1046 | RevCond(BBI.BrCond.begin(), BBI.BrCond.end()); |
| 1047 | bool CanRevCond = !TII->ReverseBranchCondition(RevCond); |
| 1048 | |
| 1049 | unsigned Dups = 0; |
| 1050 | unsigned Dups2 = 0; |
| 1051 | bool TNeedSub = !TrueBBI.Predicate.empty(); |
| 1052 | bool FNeedSub = !FalseBBI.Predicate.empty(); |
| 1053 | bool Enqueued = false; |
| 1054 | |
| 1055 | BranchProbability Prediction = MBPI->getEdgeProbability(BB, TrueBBI.BB); |
| 1056 | |
Kyle Butt | 6262ca3 | 2016-08-24 21:34:24 +0000 | [diff] [blame^] | 1057 | if (CanRevCond) { |
| 1058 | BBInfo TrueBBICalc, FalseBBICalc; |
| 1059 | if (ValidDiamond(TrueBBI, FalseBBI, Dups, Dups2, |
| 1060 | TrueBBICalc, FalseBBICalc) && |
Kyle Butt | ce0196d | 2016-08-19 18:17:04 +0000 | [diff] [blame] | 1061 | MeetIfcvtSizeLimit(*TrueBBI.BB, (TrueBBI.NonPredSize - (Dups + Dups2) + |
Kyle Butt | 6262ca3 | 2016-08-24 21:34:24 +0000 | [diff] [blame^] | 1062 | TrueBBICalc.ExtraCost), |
| 1063 | TrueBBICalc.ExtraCost2, |
Kyle Butt | ce0196d | 2016-08-19 18:17:04 +0000 | [diff] [blame] | 1064 | *FalseBBI.BB, (FalseBBI.NonPredSize - (Dups + Dups2) + |
Kyle Butt | 6262ca3 | 2016-08-24 21:34:24 +0000 | [diff] [blame^] | 1065 | FalseBBICalc.ExtraCost), |
| 1066 | FalseBBICalc.ExtraCost2, |
| 1067 | Prediction) && |
| 1068 | FeasibilityAnalysis(TrueBBI, BBI.BrCond, |
| 1069 | /* IsTriangle */ false, /* RevCond */ false, |
| 1070 | /* hasCommonTail */ true) && |
| 1071 | FeasibilityAnalysis(FalseBBI, RevCond, |
| 1072 | /* IsTriangle */ false, /* RevCond */ false, |
| 1073 | /* hasCommonTail */ true)) { |
| 1074 | // Diamond: |
| 1075 | // EBB |
| 1076 | // / \_ |
| 1077 | // | | |
| 1078 | // TBB FBB |
| 1079 | // \ / |
| 1080 | // TailBB |
| 1081 | // Note TailBB can be empty. |
| 1082 | Tokens.push_back(llvm::make_unique<IfcvtToken>( |
| 1083 | BBI, ICDiamond, TNeedSub | FNeedSub, Dups, Dups2, |
| 1084 | (bool) TrueBBICalc.ClobbersPred, |
| 1085 | (bool) FalseBBICalc.ClobbersPred)); |
| 1086 | Enqueued = true; |
| 1087 | } |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 1088 | } |
| 1089 | |
Akira Hatanaka | 14348aa | 2015-06-24 20:34:35 +0000 | [diff] [blame] | 1090 | if (ValidTriangle(TrueBBI, FalseBBI, false, Dups, Prediction) && |
| 1091 | MeetIfcvtSizeLimit(*TrueBBI.BB, TrueBBI.NonPredSize + TrueBBI.ExtraCost, |
| 1092 | TrueBBI.ExtraCost2, Prediction) && |
| 1093 | FeasibilityAnalysis(TrueBBI, BBI.BrCond, true)) { |
| 1094 | // Triangle: |
| 1095 | // EBB |
| 1096 | // | \_ |
| 1097 | // | | |
| 1098 | // | TBB |
| 1099 | // | / |
| 1100 | // FBB |
Justin Lebar | 3a7bc57 | 2016-02-22 17:51:28 +0000 | [diff] [blame] | 1101 | Tokens.push_back( |
| 1102 | llvm::make_unique<IfcvtToken>(BBI, ICTriangle, TNeedSub, Dups)); |
Akira Hatanaka | 14348aa | 2015-06-24 20:34:35 +0000 | [diff] [blame] | 1103 | Enqueued = true; |
| 1104 | } |
| 1105 | |
| 1106 | if (ValidTriangle(TrueBBI, FalseBBI, true, Dups, Prediction) && |
| 1107 | MeetIfcvtSizeLimit(*TrueBBI.BB, TrueBBI.NonPredSize + TrueBBI.ExtraCost, |
| 1108 | TrueBBI.ExtraCost2, Prediction) && |
| 1109 | FeasibilityAnalysis(TrueBBI, BBI.BrCond, true, true)) { |
Justin Lebar | 3a7bc57 | 2016-02-22 17:51:28 +0000 | [diff] [blame] | 1110 | Tokens.push_back( |
| 1111 | llvm::make_unique<IfcvtToken>(BBI, ICTriangleRev, TNeedSub, Dups)); |
Akira Hatanaka | 14348aa | 2015-06-24 20:34:35 +0000 | [diff] [blame] | 1112 | Enqueued = true; |
| 1113 | } |
| 1114 | |
| 1115 | if (ValidSimple(TrueBBI, Dups, Prediction) && |
| 1116 | MeetIfcvtSizeLimit(*TrueBBI.BB, TrueBBI.NonPredSize + TrueBBI.ExtraCost, |
| 1117 | TrueBBI.ExtraCost2, Prediction) && |
| 1118 | FeasibilityAnalysis(TrueBBI, BBI.BrCond)) { |
| 1119 | // Simple (split, no rejoin): |
| 1120 | // EBB |
| 1121 | // | \_ |
| 1122 | // | | |
| 1123 | // | TBB---> exit |
| 1124 | // | |
| 1125 | // FBB |
Justin Lebar | 3a7bc57 | 2016-02-22 17:51:28 +0000 | [diff] [blame] | 1126 | Tokens.push_back( |
| 1127 | llvm::make_unique<IfcvtToken>(BBI, ICSimple, TNeedSub, Dups)); |
Akira Hatanaka | 14348aa | 2015-06-24 20:34:35 +0000 | [diff] [blame] | 1128 | Enqueued = true; |
| 1129 | } |
| 1130 | |
| 1131 | if (CanRevCond) { |
| 1132 | // Try the other path... |
| 1133 | if (ValidTriangle(FalseBBI, TrueBBI, false, Dups, |
| 1134 | Prediction.getCompl()) && |
| 1135 | MeetIfcvtSizeLimit(*FalseBBI.BB, |
| 1136 | FalseBBI.NonPredSize + FalseBBI.ExtraCost, |
| 1137 | FalseBBI.ExtraCost2, Prediction.getCompl()) && |
| 1138 | FeasibilityAnalysis(FalseBBI, RevCond, true)) { |
Justin Lebar | 3a7bc57 | 2016-02-22 17:51:28 +0000 | [diff] [blame] | 1139 | Tokens.push_back(llvm::make_unique<IfcvtToken>(BBI, ICTriangleFalse, |
| 1140 | FNeedSub, Dups)); |
Akira Hatanaka | 14348aa | 2015-06-24 20:34:35 +0000 | [diff] [blame] | 1141 | Enqueued = true; |
| 1142 | } |
| 1143 | |
| 1144 | if (ValidTriangle(FalseBBI, TrueBBI, true, Dups, |
| 1145 | Prediction.getCompl()) && |
| 1146 | MeetIfcvtSizeLimit(*FalseBBI.BB, |
| 1147 | FalseBBI.NonPredSize + FalseBBI.ExtraCost, |
Jakub Staszak | 9b07c0a | 2011-07-10 02:58:07 +0000 | [diff] [blame] | 1148 | FalseBBI.ExtraCost2, Prediction.getCompl()) && |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 1149 | FeasibilityAnalysis(FalseBBI, RevCond, true, true)) { |
Justin Lebar | 3a7bc57 | 2016-02-22 17:51:28 +0000 | [diff] [blame] | 1150 | Tokens.push_back( |
| 1151 | llvm::make_unique<IfcvtToken>(BBI, ICTriangleFRev, FNeedSub, Dups)); |
Akira Hatanaka | 14348aa | 2015-06-24 20:34:35 +0000 | [diff] [blame] | 1152 | Enqueued = true; |
| 1153 | } |
| 1154 | |
| 1155 | if (ValidSimple(FalseBBI, Dups, Prediction.getCompl()) && |
| 1156 | MeetIfcvtSizeLimit(*FalseBBI.BB, |
| 1157 | FalseBBI.NonPredSize + FalseBBI.ExtraCost, |
| 1158 | FalseBBI.ExtraCost2, Prediction.getCompl()) && |
| 1159 | FeasibilityAnalysis(FalseBBI, RevCond)) { |
Justin Lebar | 3a7bc57 | 2016-02-22 17:51:28 +0000 | [diff] [blame] | 1160 | Tokens.push_back( |
| 1161 | llvm::make_unique<IfcvtToken>(BBI, ICSimpleFalse, FNeedSub, Dups)); |
Akira Hatanaka | 14348aa | 2015-06-24 20:34:35 +0000 | [diff] [blame] | 1162 | Enqueued = true; |
| 1163 | } |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
Akira Hatanaka | 14348aa | 2015-06-24 20:34:35 +0000 | [diff] [blame] | 1166 | BBI.IsEnqueued = Enqueued; |
| 1167 | BBI.IsBeingAnalyzed = false; |
| 1168 | BBI.IsAnalyzed = true; |
| 1169 | BBStack.pop_back(); |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 1170 | } |
Evan Cheng | 2e82cef | 2007-05-18 18:14:37 +0000 | [diff] [blame] | 1171 | } |
| 1172 | |
Matthias Braun | 2c93179 | 2016-08-17 02:51:57 +0000 | [diff] [blame] | 1173 | /// Analyze all blocks and find entries for all if-conversion candidates. |
Justin Lebar | 3a7bc57 | 2016-02-22 17:51:28 +0000 | [diff] [blame] | 1174 | void IfConverter::AnalyzeBlocks( |
| 1175 | MachineFunction &MF, std::vector<std::unique_ptr<IfcvtToken>> &Tokens) { |
Matthias Braun | b1e0558 | 2016-08-17 02:51:59 +0000 | [diff] [blame] | 1176 | for (MachineBasicBlock &MBB : MF) |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1177 | AnalyzeBlock(MBB, Tokens); |
Evan Cheng | 905a8f4 | 2007-05-30 19:49:19 +0000 | [diff] [blame] | 1178 | |
Evan Cheng | 20e0599 | 2007-06-01 00:12:12 +0000 | [diff] [blame] | 1179 | // Sort to favor more complex ifcvt scheme. |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 1180 | std::stable_sort(Tokens.begin(), Tokens.end(), IfcvtTokenCmp); |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 1181 | } |
| 1182 | |
Matthias Braun | 2c93179 | 2016-08-17 02:51:57 +0000 | [diff] [blame] | 1183 | /// Returns true either if ToMBB is the next block after MBB or that all the |
| 1184 | /// intervening blocks are empty (given MBB can fall through to its next block). |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1185 | static bool canFallThroughTo(MachineBasicBlock &MBB, MachineBasicBlock &ToMBB) { |
| 1186 | MachineFunction::iterator PI = MBB.getIterator(); |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 1187 | MachineFunction::iterator I = std::next(PI); |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1188 | MachineFunction::iterator TI = ToMBB.getIterator(); |
| 1189 | MachineFunction::iterator E = MBB.getParent()->end(); |
Evan Cheng | f128bdc | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 1190 | while (I != TI) { |
| 1191 | // Check isSuccessor to avoid case where the next block is empty, but |
| 1192 | // it's not a successor. |
Duncan P. N. Exon Smith | 5ae5939 | 2015-10-09 19:13:58 +0000 | [diff] [blame] | 1193 | if (I == E || !I->empty() || !PI->isSuccessor(&*I)) |
Evan Cheng | 312b723 | 2007-06-04 06:47:22 +0000 | [diff] [blame] | 1194 | return false; |
Evan Cheng | f128bdc | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 1195 | PI = I++; |
| 1196 | } |
Evan Cheng | 312b723 | 2007-06-04 06:47:22 +0000 | [diff] [blame] | 1197 | return true; |
Evan Cheng | e26c091 | 2007-05-21 22:22:58 +0000 | [diff] [blame] | 1198 | } |
| 1199 | |
Matthias Braun | 2c93179 | 2016-08-17 02:51:57 +0000 | [diff] [blame] | 1200 | /// Invalidate predecessor BB info so it would be re-analyzed to determine if it |
| 1201 | /// can be if-converted. If predecessor is already enqueued, dequeue it! |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1202 | void IfConverter::InvalidatePreds(MachineBasicBlock &MBB) { |
| 1203 | for (const MachineBasicBlock *Predecessor : MBB.predecessors()) { |
Saleem Abdulrasool | f158ca3 | 2014-08-09 17:21:29 +0000 | [diff] [blame] | 1204 | BBInfo &PBBI = BBAnalysis[Predecessor->getNumber()]; |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1205 | if (PBBI.IsDone || PBBI.BB == &MBB) |
Evan Cheng | 9fc56c0 | 2007-06-14 23:13:19 +0000 | [diff] [blame] | 1206 | continue; |
Evan Cheng | add9776 | 2007-06-14 23:34:09 +0000 | [diff] [blame] | 1207 | PBBI.IsAnalyzed = false; |
| 1208 | PBBI.IsEnqueued = false; |
Evan Cheng | d0e6691 | 2007-05-23 07:23:16 +0000 | [diff] [blame] | 1209 | } |
| 1210 | } |
| 1211 | |
Matthias Braun | 2c93179 | 2016-08-17 02:51:57 +0000 | [diff] [blame] | 1212 | /// Inserts an unconditional branch from \p MBB to \p ToMBB. |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1213 | static void InsertUncondBranch(MachineBasicBlock &MBB, MachineBasicBlock &ToMBB, |
Evan Cheng | c2237ce | 2007-05-29 22:31:16 +0000 | [diff] [blame] | 1214 | const TargetInstrInfo *TII) { |
Stuart Hastings | 0125b64 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 1215 | DebugLoc dl; // FIXME: this is nowhere |
Dan Gohman | 14714cb | 2008-08-22 16:07:55 +0000 | [diff] [blame] | 1216 | SmallVector<MachineOperand, 0> NoCond; |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1217 | TII->InsertBranch(MBB, &ToMBB, nullptr, NoCond, dl); |
Evan Cheng | c2237ce | 2007-05-29 22:31:16 +0000 | [diff] [blame] | 1218 | } |
| 1219 | |
Matthias Braun | 2c93179 | 2016-08-17 02:51:57 +0000 | [diff] [blame] | 1220 | /// Remove true / false edges if either / both are no longer successors. |
Evan Cheng | 288f154 | 2007-06-08 22:01:07 +0000 | [diff] [blame] | 1221 | void IfConverter::RemoveExtraEdges(BBInfo &BBI) { |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1222 | MachineBasicBlock *TBB = nullptr, *FBB = nullptr; |
Owen Anderson | 4f6bf04 | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 1223 | SmallVector<MachineOperand, 4> Cond; |
Jacques Pienaar | 71c30a1 | 2016-07-15 14:41:04 +0000 | [diff] [blame] | 1224 | if (!TII->analyzeBranch(*BBI.BB, TBB, FBB, Cond)) |
Evan Cheng | 0598b2d | 2007-06-18 22:44:57 +0000 | [diff] [blame] | 1225 | BBI.BB->CorrectExtraCFGEdges(TBB, FBB, !Cond.empty()); |
Evan Cheng | 288f154 | 2007-06-08 22:01:07 +0000 | [diff] [blame] | 1226 | } |
| 1227 | |
Matthias Braun | d616ccc | 2013-10-11 19:04:37 +0000 | [diff] [blame] | 1228 | /// Behaves like LiveRegUnits::StepForward() but also adds implicit uses to all |
Jonas Paulsson | 196986c | 2016-08-03 05:46:35 +0000 | [diff] [blame] | 1229 | /// values defined in MI which are also live/used by MI. |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1230 | static void UpdatePredRedefs(MachineInstr &MI, LivePhysRegs &Redefs) { |
Jonas Paulsson | 196986c | 2016-08-03 05:46:35 +0000 | [diff] [blame] | 1231 | const TargetRegisterInfo *TRI = MI.getParent()->getParent() |
| 1232 | ->getSubtarget().getRegisterInfo(); |
| 1233 | |
| 1234 | // Before stepping forward past MI, remember which regs were live |
| 1235 | // before MI. This is needed to set the Undef flag only when reg is |
| 1236 | // dead. |
| 1237 | SparseSet<unsigned> LiveBeforeMI; |
| 1238 | LiveBeforeMI.setUniverse(TRI->getNumRegs()); |
Matthias Braun | b1e0558 | 2016-08-17 02:51:59 +0000 | [diff] [blame] | 1239 | for (unsigned Reg : Redefs) |
Jonas Paulsson | 196986c | 2016-08-03 05:46:35 +0000 | [diff] [blame] | 1240 | LiveBeforeMI.insert(Reg); |
| 1241 | |
Pete Cooper | 7605e37 | 2015-05-05 20:14:22 +0000 | [diff] [blame] | 1242 | SmallVector<std::pair<unsigned, const MachineOperand*>, 4> Clobbers; |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1243 | Redefs.stepForward(MI, Clobbers); |
Matthias Braun | d616ccc | 2013-10-11 19:04:37 +0000 | [diff] [blame] | 1244 | |
Pete Cooper | 7605e37 | 2015-05-05 20:14:22 +0000 | [diff] [blame] | 1245 | // Now add the implicit uses for each of the clobbered values. |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1246 | for (auto Clobber : Clobbers) { |
Pete Cooper | 7605e37 | 2015-05-05 20:14:22 +0000 | [diff] [blame] | 1247 | // FIXME: Const cast here is nasty, but better than making StepForward |
| 1248 | // take a mutable instruction instead of const. |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1249 | unsigned Reg = Clobber.first; |
| 1250 | MachineOperand &Op = const_cast<MachineOperand&>(*Clobber.second); |
Pete Cooper | 2748391 | 2015-05-06 22:51:04 +0000 | [diff] [blame] | 1251 | MachineInstr *OpMI = Op.getParent(); |
Pete Cooper | 7605e37 | 2015-05-05 20:14:22 +0000 | [diff] [blame] | 1252 | MachineInstrBuilder MIB(*OpMI->getParent()->getParent(), OpMI); |
Pete Cooper | ce9ad75 | 2015-05-05 22:09:41 +0000 | [diff] [blame] | 1253 | if (Op.isRegMask()) { |
| 1254 | // First handle regmasks. They clobber any entries in the mask which |
| 1255 | // means that we need a def for those registers. |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1256 | if (LiveBeforeMI.count(Reg)) |
| 1257 | MIB.addReg(Reg, RegState::Implicit); |
Pete Cooper | ce9ad75 | 2015-05-05 22:09:41 +0000 | [diff] [blame] | 1258 | |
| 1259 | // We also need to add an implicit def of this register for the later |
| 1260 | // use to read from. |
| 1261 | // For the register allocator to have allocated a register clobbered |
| 1262 | // by the call which is used later, it must be the case that |
| 1263 | // the call doesn't return. |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1264 | MIB.addReg(Reg, RegState::Implicit | RegState::Define); |
Pete Cooper | ce9ad75 | 2015-05-05 22:09:41 +0000 | [diff] [blame] | 1265 | continue; |
| 1266 | } |
| 1267 | assert(Op.isReg() && "Register operand required"); |
Pete Cooper | 2748391 | 2015-05-06 22:51:04 +0000 | [diff] [blame] | 1268 | if (Op.isDead()) { |
| 1269 | // If we found a dead def, but it needs to be live, then remove the dead |
| 1270 | // flag. |
| 1271 | if (Redefs.contains(Op.getReg())) |
| 1272 | Op.setIsDead(false); |
| 1273 | } |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1274 | if (LiveBeforeMI.count(Reg)) |
| 1275 | MIB.addReg(Reg, RegState::Implicit); |
Matthias Braun | d616ccc | 2013-10-11 19:04:37 +0000 | [diff] [blame] | 1276 | } |
| 1277 | } |
| 1278 | |
Matthias Braun | 2c93179 | 2016-08-17 02:51:57 +0000 | [diff] [blame] | 1279 | /// Remove kill flags from operands with a registers in the \p DontKill set. |
Juergen Ributzka | 310034e | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 1280 | static void RemoveKills(MachineInstr &MI, const LivePhysRegs &DontKill) { |
Duncan P. N. Exon Smith | f9ab416 | 2016-02-27 17:05:33 +0000 | [diff] [blame] | 1281 | for (MIBundleOperands O(MI); O.isValid(); ++O) { |
Matthias Braun | d616ccc | 2013-10-11 19:04:37 +0000 | [diff] [blame] | 1282 | if (!O->isReg() || !O->isKill()) |
| 1283 | continue; |
Juergen Ributzka | 310034e | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 1284 | if (DontKill.contains(O->getReg())) |
Matthias Braun | d616ccc | 2013-10-11 19:04:37 +0000 | [diff] [blame] | 1285 | O->setIsKill(false); |
| 1286 | } |
| 1287 | } |
| 1288 | |
Matthias Braun | 2c93179 | 2016-08-17 02:51:57 +0000 | [diff] [blame] | 1289 | /// Walks a range of machine instructions and removes kill flags for registers |
| 1290 | /// in the \p DontKill set. |
Matthias Braun | d616ccc | 2013-10-11 19:04:37 +0000 | [diff] [blame] | 1291 | static void RemoveKills(MachineBasicBlock::iterator I, |
| 1292 | MachineBasicBlock::iterator E, |
Juergen Ributzka | 310034e | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 1293 | const LivePhysRegs &DontKill, |
Matthias Braun | d616ccc | 2013-10-11 19:04:37 +0000 | [diff] [blame] | 1294 | const MCRegisterInfo &MCRI) { |
Matthias Braun | b1e0558 | 2016-08-17 02:51:59 +0000 | [diff] [blame] | 1295 | for (MachineInstr &MI : make_range(I, E)) |
| 1296 | RemoveKills(MI, DontKill); |
Evan Cheng | f128bdc | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 1297 | } |
| 1298 | |
Matthias Braun | 2c93179 | 2016-08-17 02:51:57 +0000 | [diff] [blame] | 1299 | /// If convert a simple (split, no rejoin) sub-CFG. |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 1300 | bool IfConverter::IfConvertSimple(BBInfo &BBI, IfcvtKind Kind) { |
Evan Cheng | e26c091 | 2007-05-21 22:22:58 +0000 | [diff] [blame] | 1301 | BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()]; |
| 1302 | BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()]; |
| 1303 | BBInfo *CvtBBI = &TrueBBI; |
| 1304 | BBInfo *NextBBI = &FalseBBI; |
Evan Cheng | d0e6691 | 2007-05-23 07:23:16 +0000 | [diff] [blame] | 1305 | |
Owen Anderson | 4f6bf04 | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 1306 | SmallVector<MachineOperand, 4> Cond(BBI.BrCond.begin(), BBI.BrCond.end()); |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 1307 | if (Kind == ICSimpleFalse) |
Evan Cheng | 4dcf1e8 | 2007-06-01 20:29:21 +0000 | [diff] [blame] | 1308 | std::swap(CvtBBI, NextBBI); |
Evan Cheng | 23402fc | 2007-06-15 21:18:05 +0000 | [diff] [blame] | 1309 | |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1310 | MachineBasicBlock &CvtMBB = *CvtBBI->BB; |
| 1311 | MachineBasicBlock &NextMBB = *NextBBI->BB; |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 1312 | if (CvtBBI->IsDone || |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1313 | (CvtBBI->CannotBeCopied && CvtMBB.pred_size() > 1)) { |
Evan Cheng | 23402fc | 2007-06-15 21:18:05 +0000 | [diff] [blame] | 1314 | // Something has changed. It's no longer safe to predicate this block. |
Evan Cheng | 23402fc | 2007-06-15 21:18:05 +0000 | [diff] [blame] | 1315 | BBI.IsAnalyzed = false; |
| 1316 | CvtBBI->IsAnalyzed = false; |
| 1317 | return false; |
Evan Cheng | 4dcf1e8 | 2007-06-01 20:29:21 +0000 | [diff] [blame] | 1318 | } |
Evan Cheng | d0e6691 | 2007-05-23 07:23:16 +0000 | [diff] [blame] | 1319 | |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1320 | if (CvtMBB.hasAddressTaken()) |
Evan Cheng | 8b8e8d8 | 2013-05-05 18:03:49 +0000 | [diff] [blame] | 1321 | // Conservatively abort if-conversion if BB's address is taken. |
| 1322 | return false; |
| 1323 | |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 1324 | if (Kind == ICSimpleFalse) |
Dan Gohman | 97d95d6 | 2008-10-21 03:29:32 +0000 | [diff] [blame] | 1325 | if (TII->ReverseBranchCondition(Cond)) |
Craig Topper | ee4dab5 | 2012-02-05 08:31:47 +0000 | [diff] [blame] | 1326 | llvm_unreachable("Unable to reverse branch condition!"); |
Evan Cheng | 23402fc | 2007-06-15 21:18:05 +0000 | [diff] [blame] | 1327 | |
Bob Wilson | 4581434 | 2010-06-19 05:33:57 +0000 | [diff] [blame] | 1328 | // Initialize liveins to the first BB. These are potentiall redefined by |
Evan Cheng | f128bdc | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 1329 | // predicated instructions. |
Andrew Trick | 276dd45 | 2013-10-14 20:45:17 +0000 | [diff] [blame] | 1330 | Redefs.init(TRI); |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1331 | Redefs.addLiveIns(CvtMBB); |
| 1332 | Redefs.addLiveIns(NextMBB); |
Matthias Braun | d616ccc | 2013-10-11 19:04:37 +0000 | [diff] [blame] | 1333 | |
| 1334 | // Compute a set of registers which must not be killed by instructions in |
| 1335 | // BB1: This is everything live-in to BB2. |
Andrew Trick | 276dd45 | 2013-10-14 20:45:17 +0000 | [diff] [blame] | 1336 | DontKill.init(TRI); |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1337 | DontKill.addLiveIns(NextMBB); |
Evan Cheng | f128bdc | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 1338 | |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1339 | if (CvtMBB.pred_size() > 1) { |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 1340 | BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB); |
Bob Wilson | 2371f4f | 2009-05-13 23:25:24 +0000 | [diff] [blame] | 1341 | // Copy instructions in the true block, predicate them, and add them to |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 1342 | // the entry block. |
Andrew Trick | 276dd45 | 2013-10-14 20:45:17 +0000 | [diff] [blame] | 1343 | CopyAndPredicateBlock(BBI, *CvtBBI, Cond); |
Hal Finkel | 95081bf | 2013-04-10 22:05:25 +0000 | [diff] [blame] | 1344 | |
| 1345 | // RemoveExtraEdges won't work if the block has an unanalyzable branch, so |
| 1346 | // explicitly remove CvtBBI as a successor. |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1347 | BBI.BB->removeSuccessor(&CvtMBB, true); |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 1348 | } else { |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1349 | RemoveKills(CvtMBB.begin(), CvtMBB.end(), DontKill, *TRI); |
| 1350 | PredicateBlock(*CvtBBI, CvtMBB.end(), Cond); |
Evan Cheng | e26c091 | 2007-05-21 22:22:58 +0000 | [diff] [blame] | 1351 | |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 1352 | // Merge converted block into entry block. |
| 1353 | BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB); |
| 1354 | MergeBlocks(BBI, *CvtBBI); |
| 1355 | } |
Evan Cheng | be9859e | 2007-06-07 02:12:15 +0000 | [diff] [blame] | 1356 | |
Evan Cheng | e4ec918 | 2007-06-06 02:08:52 +0000 | [diff] [blame] | 1357 | bool IterIfcvt = true; |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1358 | if (!canFallThroughTo(*BBI.BB, NextMBB)) { |
| 1359 | InsertUncondBranch(*BBI.BB, NextMBB, TII); |
Evan Cheng | 2117d1f | 2007-06-09 01:03:43 +0000 | [diff] [blame] | 1360 | BBI.HasFallThrough = false; |
Evan Cheng | 7783f82 | 2007-06-08 09:36:04 +0000 | [diff] [blame] | 1361 | // Now ifcvt'd block will look like this: |
| 1362 | // BB: |
| 1363 | // ... |
| 1364 | // t, f = cmp |
| 1365 | // if t op |
| 1366 | // b BBf |
| 1367 | // |
| 1368 | // We cannot further ifcvt this block because the unconditional branch |
| 1369 | // will have to be predicated on the new condition, that will not be |
| 1370 | // available if cmp executes. |
| 1371 | IterIfcvt = false; |
Evan Cheng | e4ec918 | 2007-06-06 02:08:52 +0000 | [diff] [blame] | 1372 | } |
Evan Cheng | e26c091 | 2007-05-21 22:22:58 +0000 | [diff] [blame] | 1373 | |
Evan Cheng | 288f154 | 2007-06-08 22:01:07 +0000 | [diff] [blame] | 1374 | RemoveExtraEdges(BBI); |
| 1375 | |
Evan Cheng | d0e6691 | 2007-05-23 07:23:16 +0000 | [diff] [blame] | 1376 | // Update block info. BB can be iteratively if-converted. |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 1377 | if (!IterIfcvt) |
| 1378 | BBI.IsDone = true; |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1379 | InvalidatePreds(*BBI.BB); |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 1380 | CvtBBI->IsDone = true; |
Evan Cheng | e26c091 | 2007-05-21 22:22:58 +0000 | [diff] [blame] | 1381 | |
| 1382 | // FIXME: Must maintain LiveIns. |
Evan Cheng | e26c091 | 2007-05-21 22:22:58 +0000 | [diff] [blame] | 1383 | return true; |
| 1384 | } |
| 1385 | |
Matthias Braun | 2c93179 | 2016-08-17 02:51:57 +0000 | [diff] [blame] | 1386 | /// If convert a triangle sub-CFG. |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 1387 | bool IfConverter::IfConvertTriangle(BBInfo &BBI, IfcvtKind Kind) { |
Evan Cheng | d0e6691 | 2007-05-23 07:23:16 +0000 | [diff] [blame] | 1388 | BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()]; |
Evan Cheng | 2117d1f | 2007-06-09 01:03:43 +0000 | [diff] [blame] | 1389 | BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()]; |
| 1390 | BBInfo *CvtBBI = &TrueBBI; |
| 1391 | BBInfo *NextBBI = &FalseBBI; |
Stuart Hastings | 0125b64 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 1392 | DebugLoc dl; // FIXME: this is nowhere |
Evan Cheng | 2117d1f | 2007-06-09 01:03:43 +0000 | [diff] [blame] | 1393 | |
Owen Anderson | 4f6bf04 | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 1394 | SmallVector<MachineOperand, 4> Cond(BBI.BrCond.begin(), BBI.BrCond.end()); |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 1395 | if (Kind == ICTriangleFalse || Kind == ICTriangleFRev) |
Evan Cheng | 2117d1f | 2007-06-09 01:03:43 +0000 | [diff] [blame] | 1396 | std::swap(CvtBBI, NextBBI); |
Evan Cheng | 23402fc | 2007-06-15 21:18:05 +0000 | [diff] [blame] | 1397 | |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1398 | MachineBasicBlock &CvtMBB = *CvtBBI->BB; |
| 1399 | MachineBasicBlock &NextMBB = *NextBBI->BB; |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 1400 | if (CvtBBI->IsDone || |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1401 | (CvtBBI->CannotBeCopied && CvtMBB.pred_size() > 1)) { |
Evan Cheng | 23402fc | 2007-06-15 21:18:05 +0000 | [diff] [blame] | 1402 | // Something has changed. It's no longer safe to predicate this block. |
Evan Cheng | 23402fc | 2007-06-15 21:18:05 +0000 | [diff] [blame] | 1403 | BBI.IsAnalyzed = false; |
| 1404 | CvtBBI->IsAnalyzed = false; |
| 1405 | return false; |
Evan Cheng | 2117d1f | 2007-06-09 01:03:43 +0000 | [diff] [blame] | 1406 | } |
Evan Cheng | 23402fc | 2007-06-15 21:18:05 +0000 | [diff] [blame] | 1407 | |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1408 | if (CvtMBB.hasAddressTaken()) |
Evan Cheng | 8b8e8d8 | 2013-05-05 18:03:49 +0000 | [diff] [blame] | 1409 | // Conservatively abort if-conversion if BB's address is taken. |
| 1410 | return false; |
| 1411 | |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 1412 | if (Kind == ICTriangleFalse || Kind == ICTriangleFRev) |
Dan Gohman | 97d95d6 | 2008-10-21 03:29:32 +0000 | [diff] [blame] | 1413 | if (TII->ReverseBranchCondition(Cond)) |
Craig Topper | ee4dab5 | 2012-02-05 08:31:47 +0000 | [diff] [blame] | 1414 | llvm_unreachable("Unable to reverse branch condition!"); |
Evan Cheng | 23402fc | 2007-06-15 21:18:05 +0000 | [diff] [blame] | 1415 | |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 1416 | if (Kind == ICTriangleRev || Kind == ICTriangleFRev) { |
Dan Gohman | 97d95d6 | 2008-10-21 03:29:32 +0000 | [diff] [blame] | 1417 | if (ReverseBranchCondition(*CvtBBI)) { |
| 1418 | // BB has been changed, modify its predecessors (except for this |
| 1419 | // one) so they don't get ifcvt'ed based on bad intel. |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1420 | for (MachineBasicBlock *PBB : CvtMBB.predecessors()) { |
Dan Gohman | 97d95d6 | 2008-10-21 03:29:32 +0000 | [diff] [blame] | 1421 | if (PBB == BBI.BB) |
| 1422 | continue; |
| 1423 | BBInfo &PBBI = BBAnalysis[PBB->getNumber()]; |
| 1424 | if (PBBI.IsEnqueued) { |
| 1425 | PBBI.IsAnalyzed = false; |
| 1426 | PBBI.IsEnqueued = false; |
| 1427 | } |
Evan Cheng | add9776 | 2007-06-14 23:34:09 +0000 | [diff] [blame] | 1428 | } |
Evan Cheng | 9acfa7b | 2007-06-12 23:54:05 +0000 | [diff] [blame] | 1429 | } |
| 1430 | } |
Evan Cheng | 6a2cf07 | 2007-06-01 07:41:07 +0000 | [diff] [blame] | 1431 | |
Bob Wilson | 4581434 | 2010-06-19 05:33:57 +0000 | [diff] [blame] | 1432 | // Initialize liveins to the first BB. These are potentially redefined by |
Evan Cheng | f128bdc | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 1433 | // predicated instructions. |
Andrew Trick | 276dd45 | 2013-10-14 20:45:17 +0000 | [diff] [blame] | 1434 | Redefs.init(TRI); |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1435 | Redefs.addLiveIns(CvtMBB); |
| 1436 | Redefs.addLiveIns(NextMBB); |
Evan Cheng | f128bdc | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 1437 | |
Andrew Trick | 276dd45 | 2013-10-14 20:45:17 +0000 | [diff] [blame] | 1438 | DontKill.clear(); |
| 1439 | |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1440 | bool HasEarlyExit = CvtBBI->FalseBB != nullptr; |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 1441 | BranchProbability CvtNext, CvtFalse, BBNext, BBCvt; |
Saleem Abdulrasool | f158ca3 | 2014-08-09 17:21:29 +0000 | [diff] [blame] | 1442 | |
Manman Ren | b681918 | 2014-01-29 23:18:47 +0000 | [diff] [blame] | 1443 | if (HasEarlyExit) { |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1444 | // Get probabilities before modifying CvtMBB and BBI.BB. |
| 1445 | CvtNext = MBPI->getEdgeProbability(&CvtMBB, &NextMBB); |
| 1446 | CvtFalse = MBPI->getEdgeProbability(&CvtMBB, CvtBBI->FalseBB); |
| 1447 | BBNext = MBPI->getEdgeProbability(BBI.BB, &NextMBB); |
| 1448 | BBCvt = MBPI->getEdgeProbability(BBI.BB, &CvtMBB); |
Manman Ren | b681918 | 2014-01-29 23:18:47 +0000 | [diff] [blame] | 1449 | } |
Saleem Abdulrasool | f158ca3 | 2014-08-09 17:21:29 +0000 | [diff] [blame] | 1450 | |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1451 | if (CvtMBB.pred_size() > 1) { |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 1452 | BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB); |
Bob Wilson | 2371f4f | 2009-05-13 23:25:24 +0000 | [diff] [blame] | 1453 | // Copy instructions in the true block, predicate them, and add them to |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 1454 | // the entry block. |
Andrew Trick | 276dd45 | 2013-10-14 20:45:17 +0000 | [diff] [blame] | 1455 | CopyAndPredicateBlock(BBI, *CvtBBI, Cond, true); |
Hal Finkel | 95081bf | 2013-04-10 22:05:25 +0000 | [diff] [blame] | 1456 | |
| 1457 | // RemoveExtraEdges won't work if the block has an unanalyzable branch, so |
| 1458 | // explicitly remove CvtBBI as a successor. |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1459 | BBI.BB->removeSuccessor(&CvtMBB, true); |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 1460 | } else { |
| 1461 | // Predicate the 'true' block after removing its branch. |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1462 | CvtBBI->NonPredSize -= TII->RemoveBranch(CvtMBB); |
| 1463 | PredicateBlock(*CvtBBI, CvtMBB.end(), Cond); |
Evan Cheng | e26c091 | 2007-05-21 22:22:58 +0000 | [diff] [blame] | 1464 | |
Evan Cheng | 0598b2d | 2007-06-18 22:44:57 +0000 | [diff] [blame] | 1465 | // Now merge the entry of the triangle with the true block. |
| 1466 | BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB); |
Bob Wilson | 1e5da55 | 2010-06-29 00:55:23 +0000 | [diff] [blame] | 1467 | MergeBlocks(BBI, *CvtBBI, false); |
Evan Cheng | 0598b2d | 2007-06-18 22:44:57 +0000 | [diff] [blame] | 1468 | } |
| 1469 | |
Evan Cheng | 312b723 | 2007-06-04 06:47:22 +0000 | [diff] [blame] | 1470 | // If 'true' block has a 'false' successor, add an exit branch to it. |
Evan Cheng | 6e4babe | 2007-06-05 01:31:40 +0000 | [diff] [blame] | 1471 | if (HasEarlyExit) { |
Owen Anderson | 4f6bf04 | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 1472 | SmallVector<MachineOperand, 4> RevCond(CvtBBI->BrCond.begin(), |
| 1473 | CvtBBI->BrCond.end()); |
Evan Cheng | 6a2cf07 | 2007-06-01 07:41:07 +0000 | [diff] [blame] | 1474 | if (TII->ReverseBranchCondition(RevCond)) |
Craig Topper | ee4dab5 | 2012-02-05 08:31:47 +0000 | [diff] [blame] | 1475 | llvm_unreachable("Unable to reverse branch condition!"); |
Hans Wennborg | 1dbaf67 | 2015-12-01 03:49:42 +0000 | [diff] [blame] | 1476 | |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 1477 | // Update the edge probability for both CvtBBI->FalseBB and NextBBI. |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1478 | // NewNext = New_Prob(BBI.BB, NextMBB) = |
| 1479 | // Prob(BBI.BB, NextMBB) + |
| 1480 | // Prob(BBI.BB, CvtMBB) * Prob(CvtMBB, NextMBB) |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 1481 | // NewFalse = New_Prob(BBI.BB, CvtBBI->FalseBB) = |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1482 | // Prob(BBI.BB, CvtMBB) * Prob(CvtMBB, CvtBBI->FalseBB) |
| 1483 | auto NewTrueBB = getNextBlock(*BBI.BB); |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 1484 | auto NewNext = BBNext + BBCvt * CvtNext; |
David Majnemer | 4253126 | 2016-08-12 03:55:06 +0000 | [diff] [blame] | 1485 | auto NewTrueBBIter = find(BBI.BB->successors(), NewTrueBB); |
Cong Hou | cb07d70 | 2015-12-01 21:50:20 +0000 | [diff] [blame] | 1486 | if (NewTrueBBIter != BBI.BB->succ_end()) |
| 1487 | BBI.BB->setSuccProbability(NewTrueBBIter, NewNext); |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 1488 | |
| 1489 | auto NewFalse = BBCvt * CvtFalse; |
| 1490 | TII->InsertBranch(*BBI.BB, CvtBBI->FalseBB, nullptr, RevCond, dl); |
| 1491 | BBI.BB->addSuccessor(CvtBBI->FalseBB, NewFalse); |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 1492 | } |
Evan Cheng | 7783f82 | 2007-06-08 09:36:04 +0000 | [diff] [blame] | 1493 | |
| 1494 | // Merge in the 'false' block if the 'false' block has no other |
Bob Wilson | 2371f4f | 2009-05-13 23:25:24 +0000 | [diff] [blame] | 1495 | // predecessors. Otherwise, add an unconditional branch to 'false'. |
Evan Cheng | 17aad816 | 2007-06-05 00:07:37 +0000 | [diff] [blame] | 1496 | bool FalseBBDead = false; |
Evan Cheng | e4ec918 | 2007-06-06 02:08:52 +0000 | [diff] [blame] | 1497 | bool IterIfcvt = true; |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1498 | bool isFallThrough = canFallThroughTo(*BBI.BB, NextMBB); |
Evan Cheng | d3f3f0a | 2007-06-07 08:13:00 +0000 | [diff] [blame] | 1499 | if (!isFallThrough) { |
| 1500 | // Only merge them if the true block does not fallthrough to the false |
| 1501 | // block. By not merging them, we make it possible to iteratively |
| 1502 | // ifcvt the blocks. |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 1503 | if (!HasEarlyExit && |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1504 | NextMBB.pred_size() == 1 && !NextBBI->HasFallThrough && |
| 1505 | !NextMBB.hasAddressTaken()) { |
Evan Cheng | 2117d1f | 2007-06-09 01:03:43 +0000 | [diff] [blame] | 1506 | MergeBlocks(BBI, *NextBBI); |
Evan Cheng | d3f3f0a | 2007-06-07 08:13:00 +0000 | [diff] [blame] | 1507 | FalseBBDead = true; |
Evan Cheng | d3f3f0a | 2007-06-07 08:13:00 +0000 | [diff] [blame] | 1508 | } else { |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1509 | InsertUncondBranch(*BBI.BB, NextMBB, TII); |
Evan Cheng | 2117d1f | 2007-06-09 01:03:43 +0000 | [diff] [blame] | 1510 | BBI.HasFallThrough = false; |
Evan Cheng | d3f3f0a | 2007-06-07 08:13:00 +0000 | [diff] [blame] | 1511 | } |
Evan Cheng | 7783f82 | 2007-06-08 09:36:04 +0000 | [diff] [blame] | 1512 | // Mixed predicated and unpredicated code. This cannot be iteratively |
| 1513 | // predicated. |
| 1514 | IterIfcvt = false; |
Evan Cheng | e4ec918 | 2007-06-06 02:08:52 +0000 | [diff] [blame] | 1515 | } |
Evan Cheng | e26c091 | 2007-05-21 22:22:58 +0000 | [diff] [blame] | 1516 | |
Evan Cheng | 288f154 | 2007-06-08 22:01:07 +0000 | [diff] [blame] | 1517 | RemoveExtraEdges(BBI); |
Evan Cheng | e26c091 | 2007-05-21 22:22:58 +0000 | [diff] [blame] | 1518 | |
Evan Cheng | d0e6691 | 2007-05-23 07:23:16 +0000 | [diff] [blame] | 1519 | // Update block info. BB can be iteratively if-converted. |
Bob Wilson | 8105144 | 2010-06-15 22:18:54 +0000 | [diff] [blame] | 1520 | if (!IterIfcvt) |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 1521 | BBI.IsDone = true; |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1522 | InvalidatePreds(*BBI.BB); |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 1523 | CvtBBI->IsDone = true; |
Evan Cheng | 17aad816 | 2007-06-05 00:07:37 +0000 | [diff] [blame] | 1524 | if (FalseBBDead) |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 1525 | NextBBI->IsDone = true; |
Evan Cheng | e26c091 | 2007-05-21 22:22:58 +0000 | [diff] [blame] | 1526 | |
| 1527 | // FIXME: Must maintain LiveIns. |
Evan Cheng | e26c091 | 2007-05-21 22:22:58 +0000 | [diff] [blame] | 1528 | return true; |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 1529 | } |
| 1530 | |
Kyle Butt | ce0196d | 2016-08-19 18:17:04 +0000 | [diff] [blame] | 1531 | /// If convert a diamond sub-CFG. |
Kyle Butt | 6262ca3 | 2016-08-24 21:34:24 +0000 | [diff] [blame^] | 1532 | /// \p BBI is the head of the diamond |
| 1533 | /// \p NumDups1 - number of shared instructions at the beginning of TrueBBI and |
| 1534 | /// FalseBBI |
| 1535 | /// \p NumDups2 - number of shared instructions at the end of TrueBBI and |
| 1536 | /// FalseBBI |
| 1537 | /// \p TClobbersPred - True if the true block clobbers the predicate in the |
| 1538 | /// non-shared portion. |
| 1539 | /// \p TClobbersPred - True if the false block clobbers the predicate in the |
| 1540 | /// non-shared portion. |
Kyle Butt | ce0196d | 2016-08-19 18:17:04 +0000 | [diff] [blame] | 1541 | bool IfConverter::IfConvertDiamond(BBInfo &BBI, IfcvtKind Kind, |
Kyle Butt | 6262ca3 | 2016-08-24 21:34:24 +0000 | [diff] [blame^] | 1542 | unsigned NumDups1, unsigned NumDups2, |
| 1543 | bool TClobbersPred, bool FClobbersPred) { |
Kyle Butt | ce0196d | 2016-08-19 18:17:04 +0000 | [diff] [blame] | 1544 | BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()]; |
| 1545 | BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()]; |
| 1546 | MachineBasicBlock *TailBB = TrueBBI.TrueBB; |
| 1547 | // True block must fall through or end with an unanalyzable terminator. |
| 1548 | if (!TailBB) { |
| 1549 | if (blockAlwaysFallThrough(TrueBBI)) |
| 1550 | TailBB = FalseBBI.TrueBB; |
| 1551 | assert((TailBB || !TrueBBI.IsBrAnalyzable) && "Unexpected!"); |
| 1552 | } |
Evan Cheng | e26c091 | 2007-05-21 22:22:58 +0000 | [diff] [blame] | 1553 | |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 1554 | if (TrueBBI.IsDone || FalseBBI.IsDone || |
Kyle Butt | ce0196d | 2016-08-19 18:17:04 +0000 | [diff] [blame] | 1555 | TrueBBI.BB->pred_size() > 1 || |
| 1556 | FalseBBI.BB->pred_size() > 1) { |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 1557 | // Something has changed. It's no longer safe to predicate these blocks. |
| 1558 | BBI.IsAnalyzed = false; |
| 1559 | TrueBBI.IsAnalyzed = false; |
| 1560 | FalseBBI.IsAnalyzed = false; |
| 1561 | return false; |
Evan Cheng | e26c091 | 2007-05-21 22:22:58 +0000 | [diff] [blame] | 1562 | } |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 1563 | |
Evan Cheng | 8b8e8d8 | 2013-05-05 18:03:49 +0000 | [diff] [blame] | 1564 | if (TrueBBI.BB->hasAddressTaken() || FalseBBI.BB->hasAddressTaken()) |
| 1565 | // Conservatively abort if-conversion if either BB has its address taken. |
| 1566 | return false; |
| 1567 | |
Bob Wilson | 1e5da55 | 2010-06-29 00:55:23 +0000 | [diff] [blame] | 1568 | // Put the predicated instructions from the 'true' block before the |
| 1569 | // instructions from the 'false' block, unless the true block would clobber |
| 1570 | // the predicate, in which case, do the opposite. |
Evan Cheng | 7948422 | 2007-06-05 23:46:14 +0000 | [diff] [blame] | 1571 | BBInfo *BBI1 = &TrueBBI; |
| 1572 | BBInfo *BBI2 = &FalseBBI; |
Owen Anderson | 4f6bf04 | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 1573 | SmallVector<MachineOperand, 4> RevCond(BBI.BrCond.begin(), BBI.BrCond.end()); |
Dan Gohman | 97d95d6 | 2008-10-21 03:29:32 +0000 | [diff] [blame] | 1574 | if (TII->ReverseBranchCondition(RevCond)) |
Craig Topper | ee4dab5 | 2012-02-05 08:31:47 +0000 | [diff] [blame] | 1575 | llvm_unreachable("Unable to reverse branch condition!"); |
Owen Anderson | 4f6bf04 | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 1576 | SmallVector<MachineOperand, 4> *Cond1 = &BBI.BrCond; |
| 1577 | SmallVector<MachineOperand, 4> *Cond2 = &RevCond; |
Evan Cheng | 3056599 | 2007-06-06 00:57:55 +0000 | [diff] [blame] | 1578 | |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 1579 | // Figure out the more profitable ordering. |
| 1580 | bool DoSwap = false; |
Kyle Butt | 6262ca3 | 2016-08-24 21:34:24 +0000 | [diff] [blame^] | 1581 | if (TClobbersPred && !FClobbersPred) |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 1582 | DoSwap = true; |
Kyle Butt | 6262ca3 | 2016-08-24 21:34:24 +0000 | [diff] [blame^] | 1583 | else if (TClobbersPred == FClobbersPred) { |
Evan Cheng | 0598b2d | 2007-06-18 22:44:57 +0000 | [diff] [blame] | 1584 | if (TrueBBI.NonPredSize > FalseBBI.NonPredSize) |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 1585 | DoSwap = true; |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 1586 | } |
| 1587 | if (DoSwap) { |
Evan Cheng | 3056599 | 2007-06-06 00:57:55 +0000 | [diff] [blame] | 1588 | std::swap(BBI1, BBI2); |
| 1589 | std::swap(Cond1, Cond2); |
Evan Cheng | 3056599 | 2007-06-06 00:57:55 +0000 | [diff] [blame] | 1590 | } |
Evan Cheng | 7948422 | 2007-06-05 23:46:14 +0000 | [diff] [blame] | 1591 | |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 1592 | // Remove the conditional branch from entry to the blocks. |
| 1593 | BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB); |
| 1594 | |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1595 | MachineBasicBlock &MBB1 = *BBI1->BB; |
| 1596 | MachineBasicBlock &MBB2 = *BBI2->BB; |
| 1597 | |
Krzysztof Parzyszek | a003b76 | 2016-08-11 18:42:06 +0000 | [diff] [blame] | 1598 | // Initialize the Redefs: |
| 1599 | // - BB2 live-in regs need implicit uses before being redefined by BB1 |
| 1600 | // instructions. |
| 1601 | // - BB1 live-out regs need implicit uses before being redefined by BB2 |
| 1602 | // instructions. We start with BB1 live-ins so we have the live-out regs |
| 1603 | // after tracking the BB1 instructions. |
Andrew Trick | 276dd45 | 2013-10-14 20:45:17 +0000 | [diff] [blame] | 1604 | Redefs.init(TRI); |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1605 | Redefs.addLiveIns(MBB1); |
| 1606 | Redefs.addLiveIns(MBB2); |
Evan Cheng | f128bdc | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 1607 | |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 1608 | // Remove the duplicated instructions at the beginnings of both paths. |
Jim Grosbach | 412800d | 2010-06-14 21:30:32 +0000 | [diff] [blame] | 1609 | // Skip dbg_value instructions |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1610 | MachineBasicBlock::iterator DI1 = MBB1.getFirstNonDebugInstr(); |
| 1611 | MachineBasicBlock::iterator DI2 = MBB2.getFirstNonDebugInstr(); |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 1612 | BBI1->NonPredSize -= NumDups1; |
| 1613 | BBI2->NonPredSize -= NumDups1; |
Jim Grosbach | ee6e29a | 2010-06-28 20:26:00 +0000 | [diff] [blame] | 1614 | |
| 1615 | // Skip past the dups on each side separately since there may be |
| 1616 | // differing dbg_value entries. |
| 1617 | for (unsigned i = 0; i < NumDups1; ++DI1) { |
| 1618 | if (!DI1->isDebugValue()) |
| 1619 | ++i; |
| 1620 | } |
Jim Grosbach | c34befc | 2010-06-25 23:05:46 +0000 | [diff] [blame] | 1621 | while (NumDups1 != 0) { |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 1622 | ++DI2; |
Jim Grosbach | ee6e29a | 2010-06-28 20:26:00 +0000 | [diff] [blame] | 1623 | if (!DI2->isDebugValue()) |
| 1624 | --NumDups1; |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 1625 | } |
Evan Cheng | f128bdc | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 1626 | |
Matthias Braun | d616ccc | 2013-10-11 19:04:37 +0000 | [diff] [blame] | 1627 | // Compute a set of registers which must not be killed by instructions in BB1: |
| 1628 | // This is everything used+live in BB2 after the duplicated instructions. We |
| 1629 | // can compute this set by simulating liveness backwards from the end of BB2. |
Andrew Trick | 276dd45 | 2013-10-14 20:45:17 +0000 | [diff] [blame] | 1630 | DontKill.init(TRI); |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1631 | for (const MachineInstr &MI : |
| 1632 | make_range(MBB2.rbegin(), MachineBasicBlock::reverse_iterator(DI2))) |
Matthias Braun | b1e0558 | 2016-08-17 02:51:59 +0000 | [diff] [blame] | 1633 | DontKill.stepBackward(MI); |
Matthias Braun | d616ccc | 2013-10-11 19:04:37 +0000 | [diff] [blame] | 1634 | |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1635 | for (const MachineInstr &MI : make_range(MBB1.begin(), DI1)) { |
Pete Cooper | 7605e37 | 2015-05-05 20:14:22 +0000 | [diff] [blame] | 1636 | SmallVector<std::pair<unsigned, const MachineOperand*>, 4> IgnoredClobbers; |
Matthias Braun | b1e0558 | 2016-08-17 02:51:59 +0000 | [diff] [blame] | 1637 | Redefs.stepForward(MI, IgnoredClobbers); |
Matthias Braun | d616ccc | 2013-10-11 19:04:37 +0000 | [diff] [blame] | 1638 | } |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1639 | BBI.BB->splice(BBI.BB->end(), &MBB1, MBB1.begin(), DI1); |
| 1640 | MBB2.erase(MBB2.begin(), DI2); |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 1641 | |
Kyle Butt | ce0196d | 2016-08-19 18:17:04 +0000 | [diff] [blame] | 1642 | // Remove branch from the 'true' block, unless it was not analyzable. |
| 1643 | // Non-analyzable branches need to be preserved, since in such cases, |
| 1644 | // the CFG structure is not an actual diamond (the join block may not |
| 1645 | // be present). |
| 1646 | if (BBI1->IsBrAnalyzable) |
| 1647 | BBI1->NonPredSize -= TII->RemoveBranch(MBB1); |
Krzysztof Parzyszek | 2451c48 | 2016-01-20 13:14:52 +0000 | [diff] [blame] | 1648 | // Remove duplicated instructions. |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1649 | DI1 = MBB1.end(); |
Jim Grosbach | 412800d | 2010-06-14 21:30:32 +0000 | [diff] [blame] | 1650 | for (unsigned i = 0; i != NumDups2; ) { |
| 1651 | // NumDups2 only counted non-dbg_value instructions, so this won't |
| 1652 | // run off the head of the list. |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1653 | assert(DI1 != MBB1.begin()); |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 1654 | --DI1; |
Jim Grosbach | 412800d | 2010-06-14 21:30:32 +0000 | [diff] [blame] | 1655 | // skip dbg_value instructions |
| 1656 | if (!DI1->isDebugValue()) |
| 1657 | ++i; |
| 1658 | } |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1659 | MBB1.erase(DI1, MBB1.end()); |
Evan Cheng | 7948422 | 2007-06-05 23:46:14 +0000 | [diff] [blame] | 1660 | |
Matthias Braun | d616ccc | 2013-10-11 19:04:37 +0000 | [diff] [blame] | 1661 | // Kill flags in the true block for registers living into the false block |
| 1662 | // must be removed. |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1663 | RemoveKills(MBB1.begin(), MBB1.end(), DontKill, *TRI); |
Matthias Braun | d616ccc | 2013-10-11 19:04:37 +0000 | [diff] [blame] | 1664 | |
Kyle Butt | ce0196d | 2016-08-19 18:17:04 +0000 | [diff] [blame] | 1665 | // Remove 'false' block branch (unless it was not analyzable), and find |
| 1666 | // the last instruction to predicate. |
| 1667 | if (BBI2->IsBrAnalyzable) |
| 1668 | BBI2->NonPredSize -= TII->RemoveBranch(MBB2); |
| 1669 | DI2 = MBB2.end(); |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 1670 | while (NumDups2 != 0) { |
Jim Grosbach | 412800d | 2010-06-14 21:30:32 +0000 | [diff] [blame] | 1671 | // NumDups2 only counted non-dbg_value instructions, so this won't |
| 1672 | // run off the head of the list. |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1673 | assert(DI2 != MBB2.begin()); |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 1674 | --DI2; |
Jim Grosbach | 412800d | 2010-06-14 21:30:32 +0000 | [diff] [blame] | 1675 | // skip dbg_value instructions |
| 1676 | if (!DI2->isDebugValue()) |
| 1677 | --NumDups2; |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 1678 | } |
Evan Cheng | 4266a79 | 2011-12-19 22:01:30 +0000 | [diff] [blame] | 1679 | |
| 1680 | // Remember which registers would later be defined by the false block. |
| 1681 | // This allows us not to predicate instructions in the true block that would |
| 1682 | // later be re-defined. That is, rather than |
| 1683 | // subeq r0, r1, #1 |
| 1684 | // addne r0, r1, #1 |
| 1685 | // generate: |
| 1686 | // sub r0, r1, #1 |
| 1687 | // addne r0, r1, #1 |
| 1688 | SmallSet<unsigned, 4> RedefsByFalse; |
| 1689 | SmallSet<unsigned, 4> ExtUses; |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1690 | if (TII->isProfitableToUnpredicate(MBB1, MBB2)) { |
| 1691 | for (const MachineInstr &FI : make_range(MBB2.begin(), DI2)) { |
Matthias Braun | b1e0558 | 2016-08-17 02:51:59 +0000 | [diff] [blame] | 1692 | if (FI.isDebugValue()) |
Evan Cheng | 4266a79 | 2011-12-19 22:01:30 +0000 | [diff] [blame] | 1693 | continue; |
| 1694 | SmallVector<unsigned, 4> Defs; |
Matthias Braun | b1e0558 | 2016-08-17 02:51:59 +0000 | [diff] [blame] | 1695 | for (const MachineOperand &MO : FI.operands()) { |
Evan Cheng | 4266a79 | 2011-12-19 22:01:30 +0000 | [diff] [blame] | 1696 | if (!MO.isReg()) |
| 1697 | continue; |
| 1698 | unsigned Reg = MO.getReg(); |
| 1699 | if (!Reg) |
| 1700 | continue; |
| 1701 | if (MO.isDef()) { |
| 1702 | Defs.push_back(Reg); |
| 1703 | } else if (!RedefsByFalse.count(Reg)) { |
| 1704 | // These are defined before ctrl flow reach the 'false' instructions. |
| 1705 | // They cannot be modified by the 'true' instructions. |
Chad Rosier | abdb1d6 | 2013-05-22 23:17:36 +0000 | [diff] [blame] | 1706 | for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true); |
| 1707 | SubRegs.isValid(); ++SubRegs) |
Jakob Stoklund Olesen | 54038d7 | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 1708 | ExtUses.insert(*SubRegs); |
Evan Cheng | 4266a79 | 2011-12-19 22:01:30 +0000 | [diff] [blame] | 1709 | } |
| 1710 | } |
| 1711 | |
Matthias Braun | b1e0558 | 2016-08-17 02:51:59 +0000 | [diff] [blame] | 1712 | for (unsigned Reg : Defs) { |
Evan Cheng | 4266a79 | 2011-12-19 22:01:30 +0000 | [diff] [blame] | 1713 | if (!ExtUses.count(Reg)) { |
Chad Rosier | abdb1d6 | 2013-05-22 23:17:36 +0000 | [diff] [blame] | 1714 | for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true); |
| 1715 | SubRegs.isValid(); ++SubRegs) |
Jakob Stoklund Olesen | 54038d7 | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 1716 | RedefsByFalse.insert(*SubRegs); |
Evan Cheng | 4266a79 | 2011-12-19 22:01:30 +0000 | [diff] [blame] | 1717 | } |
| 1718 | } |
| 1719 | } |
| 1720 | } |
| 1721 | |
| 1722 | // Predicate the 'true' block. |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1723 | PredicateBlock(*BBI1, MBB1.end(), *Cond1, &RedefsByFalse); |
Evan Cheng | 4266a79 | 2011-12-19 22:01:30 +0000 | [diff] [blame] | 1724 | |
Krzysztof Parzyszek | 2451c48 | 2016-01-20 13:14:52 +0000 | [diff] [blame] | 1725 | // After predicating BBI1, if there is a predicated terminator in BBI1 and |
| 1726 | // a non-predicated in BBI2, then we don't want to predicate the one from |
| 1727 | // BBI2. The reason is that if we merged these blocks, we would end up with |
| 1728 | // two predicated terminators in the same block. |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1729 | if (!MBB2.empty() && (DI2 == MBB2.end())) { |
| 1730 | MachineBasicBlock::iterator BBI1T = MBB1.getFirstTerminator(); |
| 1731 | MachineBasicBlock::iterator BBI2T = MBB2.getFirstTerminator(); |
| 1732 | if (BBI1T != MBB1.end() && TII->isPredicated(*BBI1T) && |
| 1733 | BBI2T != MBB2.end() && !TII->isPredicated(*BBI2T)) |
Krzysztof Parzyszek | 2451c48 | 2016-01-20 13:14:52 +0000 | [diff] [blame] | 1734 | --DI2; |
| 1735 | } |
| 1736 | |
Evan Cheng | 4266a79 | 2011-12-19 22:01:30 +0000 | [diff] [blame] | 1737 | // Predicate the 'false' block. |
Andrew Trick | 276dd45 | 2013-10-14 20:45:17 +0000 | [diff] [blame] | 1738 | PredicateBlock(*BBI2, DI2, *Cond2); |
Evan Cheng | 7948422 | 2007-06-05 23:46:14 +0000 | [diff] [blame] | 1739 | |
Evan Cheng | 0598b2d | 2007-06-18 22:44:57 +0000 | [diff] [blame] | 1740 | // Merge the true block into the entry of the diamond. |
Kyle Butt | ce0196d | 2016-08-19 18:17:04 +0000 | [diff] [blame] | 1741 | MergeBlocks(BBI, *BBI1, TailBB == nullptr); |
| 1742 | MergeBlocks(BBI, *BBI2, TailBB == nullptr); |
Evan Cheng | 3056599 | 2007-06-06 00:57:55 +0000 | [diff] [blame] | 1743 | |
Bob Wilson | 2371f4f | 2009-05-13 23:25:24 +0000 | [diff] [blame] | 1744 | // If the if-converted block falls through or unconditionally branches into |
| 1745 | // the tail block, and the tail block does not have other predecessors, then |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 1746 | // fold the tail block in as well. Otherwise, unless it falls through to the |
| 1747 | // tail, add a unconditional branch to it. |
| 1748 | if (TailBB) { |
Pete Cooper | 77c703f | 2011-11-04 23:49:14 +0000 | [diff] [blame] | 1749 | BBInfo &TailBBI = BBAnalysis[TailBB->getNumber()]; |
Evan Cheng | 8b8e8d8 | 2013-05-05 18:03:49 +0000 | [diff] [blame] | 1750 | bool CanMergeTail = !TailBBI.HasFallThrough && |
| 1751 | !TailBBI.BB->hasAddressTaken(); |
Krzysztof Parzyszek | 2451c48 | 2016-01-20 13:14:52 +0000 | [diff] [blame] | 1752 | // The if-converted block can still have a predicated terminator |
| 1753 | // (e.g. a predicated return). If that is the case, we cannot merge |
| 1754 | // it with the tail block. |
| 1755 | MachineBasicBlock::const_iterator TI = BBI.BB->getFirstTerminator(); |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1756 | if (TI != BBI.BB->end() && TII->isPredicated(*TI)) |
Krzysztof Parzyszek | 2451c48 | 2016-01-20 13:14:52 +0000 | [diff] [blame] | 1757 | CanMergeTail = false; |
Bob Wilson | 1e5da55 | 2010-06-29 00:55:23 +0000 | [diff] [blame] | 1758 | // There may still be a fall-through edge from BBI1 or BBI2 to TailBB; |
| 1759 | // check if there are any other predecessors besides those. |
| 1760 | unsigned NumPreds = TailBB->pred_size(); |
| 1761 | if (NumPreds > 1) |
| 1762 | CanMergeTail = false; |
| 1763 | else if (NumPreds == 1 && CanMergeTail) { |
| 1764 | MachineBasicBlock::pred_iterator PI = TailBB->pred_begin(); |
Kyle Butt | ce0196d | 2016-08-19 18:17:04 +0000 | [diff] [blame] | 1765 | if (*PI != &MBB1 && *PI != &MBB2) |
Bob Wilson | 1e5da55 | 2010-06-29 00:55:23 +0000 | [diff] [blame] | 1766 | CanMergeTail = false; |
| 1767 | } |
| 1768 | if (CanMergeTail) { |
Evan Cheng | 0598b2d | 2007-06-18 22:44:57 +0000 | [diff] [blame] | 1769 | MergeBlocks(BBI, TailBBI); |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 1770 | TailBBI.IsDone = true; |
| 1771 | } else { |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 1772 | BBI.BB->addSuccessor(TailBB, BranchProbability::getOne()); |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1773 | InsertUncondBranch(*BBI.BB, *TailBB, TII); |
Evan Cheng | 0598b2d | 2007-06-18 22:44:57 +0000 | [diff] [blame] | 1774 | BBI.HasFallThrough = false; |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 1775 | } |
Evan Cheng | e26c091 | 2007-05-21 22:22:58 +0000 | [diff] [blame] | 1776 | } |
| 1777 | |
Bob Wilson | 1e5da55 | 2010-06-29 00:55:23 +0000 | [diff] [blame] | 1778 | // RemoveExtraEdges won't work if the block has an unanalyzable branch, |
| 1779 | // which can happen here if TailBB is unanalyzable and is merged, so |
| 1780 | // explicitly remove BBI1 and BBI2 as successors. |
Kyle Butt | ce0196d | 2016-08-19 18:17:04 +0000 | [diff] [blame] | 1781 | BBI.BB->removeSuccessor(&MBB1); |
| 1782 | BBI.BB->removeSuccessor(&MBB2, true); |
Evan Cheng | 288f154 | 2007-06-08 22:01:07 +0000 | [diff] [blame] | 1783 | RemoveExtraEdges(BBI); |
| 1784 | |
Evan Cheng | 7948422 | 2007-06-05 23:46:14 +0000 | [diff] [blame] | 1785 | // Update block info. |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 1786 | BBI.IsDone = TrueBBI.IsDone = FalseBBI.IsDone = true; |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1787 | InvalidatePreds(*BBI.BB); |
Evan Cheng | e26c091 | 2007-05-21 22:22:58 +0000 | [diff] [blame] | 1788 | |
| 1789 | // FIXME: Must maintain LiveIns. |
Evan Cheng | e26c091 | 2007-05-21 22:22:58 +0000 | [diff] [blame] | 1790 | return true; |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 1791 | } |
| 1792 | |
Duncan P. N. Exon Smith | 0490cde | 2016-06-30 23:04:51 +0000 | [diff] [blame] | 1793 | static bool MaySpeculate(const MachineInstr &MI, |
Matthias Braun | 07066cc | 2015-05-19 21:22:20 +0000 | [diff] [blame] | 1794 | SmallSet<unsigned, 4> &LaterRedefs) { |
Evan Cheng | 4266a79 | 2011-12-19 22:01:30 +0000 | [diff] [blame] | 1795 | bool SawStore = true; |
Duncan P. N. Exon Smith | 0490cde | 2016-06-30 23:04:51 +0000 | [diff] [blame] | 1796 | if (!MI.isSafeToMove(nullptr, SawStore)) |
Evan Cheng | 4266a79 | 2011-12-19 22:01:30 +0000 | [diff] [blame] | 1797 | return false; |
| 1798 | |
Matthias Braun | b1e0558 | 2016-08-17 02:51:59 +0000 | [diff] [blame] | 1799 | for (const MachineOperand &MO : MI.operands()) { |
Evan Cheng | 4266a79 | 2011-12-19 22:01:30 +0000 | [diff] [blame] | 1800 | if (!MO.isReg()) |
| 1801 | continue; |
| 1802 | unsigned Reg = MO.getReg(); |
| 1803 | if (!Reg) |
| 1804 | continue; |
| 1805 | if (MO.isDef() && !LaterRedefs.count(Reg)) |
| 1806 | return false; |
| 1807 | } |
| 1808 | |
| 1809 | return true; |
| 1810 | } |
| 1811 | |
Matthias Braun | 2c93179 | 2016-08-17 02:51:57 +0000 | [diff] [blame] | 1812 | /// Predicate instructions from the start of the block to the specified end with |
| 1813 | /// the specified condition. |
Evan Cheng | d0e6691 | 2007-05-23 07:23:16 +0000 | [diff] [blame] | 1814 | void IfConverter::PredicateBlock(BBInfo &BBI, |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 1815 | MachineBasicBlock::iterator E, |
Evan Cheng | f128bdc | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 1816 | SmallVectorImpl<MachineOperand> &Cond, |
Evan Cheng | 4266a79 | 2011-12-19 22:01:30 +0000 | [diff] [blame] | 1817 | SmallSet<unsigned, 4> *LaterRedefs) { |
| 1818 | bool AnyUnpred = false; |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1819 | bool MaySpec = LaterRedefs != nullptr; |
Matthias Braun | b1e0558 | 2016-08-17 02:51:59 +0000 | [diff] [blame] | 1820 | for (MachineInstr &I : make_range(BBI.BB->begin(), E)) { |
Duncan P. N. Exon Smith | 0490cde | 2016-06-30 23:04:51 +0000 | [diff] [blame] | 1821 | if (I.isDebugValue() || TII->isPredicated(I)) |
Evan Cheng | d0e6691 | 2007-05-23 07:23:16 +0000 | [diff] [blame] | 1822 | continue; |
Evan Cheng | 4266a79 | 2011-12-19 22:01:30 +0000 | [diff] [blame] | 1823 | // It may be possible not to predicate an instruction if it's the 'true' |
| 1824 | // side of a diamond and the 'false' side may re-define the instruction's |
| 1825 | // defs. |
Matthias Braun | 07066cc | 2015-05-19 21:22:20 +0000 | [diff] [blame] | 1826 | if (MaySpec && MaySpeculate(I, *LaterRedefs)) { |
Evan Cheng | 4266a79 | 2011-12-19 22:01:30 +0000 | [diff] [blame] | 1827 | AnyUnpred = true; |
| 1828 | continue; |
| 1829 | } |
| 1830 | // If any instruction is predicated, then every instruction after it must |
| 1831 | // be predicated. |
| 1832 | MaySpec = false; |
Duncan P. N. Exon Smith | 0490cde | 2016-06-30 23:04:51 +0000 | [diff] [blame] | 1833 | if (!TII->PredicateInstruction(I, Cond)) { |
Torok Edwin | 08954aa | 2009-07-12 20:07:01 +0000 | [diff] [blame] | 1834 | #ifndef NDEBUG |
Duncan P. N. Exon Smith | 0490cde | 2016-06-30 23:04:51 +0000 | [diff] [blame] | 1835 | dbgs() << "Unable to predicate " << I << "!\n"; |
Torok Edwin | 08954aa | 2009-07-12 20:07:01 +0000 | [diff] [blame] | 1836 | #endif |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1837 | llvm_unreachable(nullptr); |
Evan Cheng | af71610 | 2007-05-16 21:54:37 +0000 | [diff] [blame] | 1838 | } |
Evan Cheng | f128bdc | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 1839 | |
Bob Wilson | 4581434 | 2010-06-19 05:33:57 +0000 | [diff] [blame] | 1840 | // If the predicated instruction now redefines a register as the result of |
Evan Cheng | f128bdc | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 1841 | // if-conversion, add an implicit kill. |
Duncan P. N. Exon Smith | 0490cde | 2016-06-30 23:04:51 +0000 | [diff] [blame] | 1842 | UpdatePredRedefs(I, Redefs); |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 1843 | } |
Evan Cheng | d0e6691 | 2007-05-23 07:23:16 +0000 | [diff] [blame] | 1844 | |
Benjamin Kramer | 4f6ac16 | 2015-02-28 10:11:12 +0000 | [diff] [blame] | 1845 | BBI.Predicate.append(Cond.begin(), Cond.end()); |
Evan Cheng | df1a429 | 2007-06-08 19:17:12 +0000 | [diff] [blame] | 1846 | |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 1847 | BBI.IsAnalyzed = false; |
| 1848 | BBI.NonPredSize = 0; |
| 1849 | |
Dan Gohman | d2d1ae1 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 1850 | ++NumIfConvBBs; |
Evan Cheng | 4266a79 | 2011-12-19 22:01:30 +0000 | [diff] [blame] | 1851 | if (AnyUnpred) |
| 1852 | ++NumUnpred; |
Evan Cheng | 312b723 | 2007-06-04 06:47:22 +0000 | [diff] [blame] | 1853 | } |
| 1854 | |
Matthias Braun | 2c93179 | 2016-08-17 02:51:57 +0000 | [diff] [blame] | 1855 | /// Copy and predicate instructions from source BB to the destination block. |
| 1856 | /// Skip end of block branches if IgnoreBr is true. |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 1857 | void IfConverter::CopyAndPredicateBlock(BBInfo &ToBBI, BBInfo &FromBBI, |
Owen Anderson | 4f6bf04 | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 1858 | SmallVectorImpl<MachineOperand> &Cond, |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 1859 | bool IgnoreBr) { |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 1860 | MachineFunction &MF = *ToBBI.BB->getParent(); |
| 1861 | |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1862 | MachineBasicBlock &FromMBB = *FromBBI.BB; |
| 1863 | for (MachineInstr &I : FromMBB) { |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 1864 | // Do not copy the end of the block branches. |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1865 | if (IgnoreBr && I.isBranch()) |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 1866 | break; |
| 1867 | |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1868 | MachineInstr *MI = MF.CloneMachineInstr(&I); |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 1869 | ToBBI.BB->insert(ToBBI.BB->end(), MI); |
Bob Wilson | efd360c | 2010-10-26 00:02:21 +0000 | [diff] [blame] | 1870 | ToBBI.NonPredSize++; |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1871 | unsigned ExtraPredCost = TII->getPredicationCost(I); |
| 1872 | unsigned NumCycles = SchedModel.computeInstrLatency(&I, false); |
Evan Cheng | debf9c5 | 2010-11-03 00:45:17 +0000 | [diff] [blame] | 1873 | if (NumCycles > 1) |
| 1874 | ToBBI.ExtraCost += NumCycles-1; |
| 1875 | ToBBI.ExtraCost2 += ExtraPredCost; |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 1876 | |
Bob Wilson | f82c8fc | 2010-06-18 17:07:23 +0000 | [diff] [blame] | 1877 | if (!TII->isPredicated(I) && !MI->isDebugValue()) { |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1878 | if (!TII->PredicateInstruction(*MI, Cond)) { |
Torok Edwin | 08954aa | 2009-07-12 20:07:01 +0000 | [diff] [blame] | 1879 | #ifndef NDEBUG |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1880 | dbgs() << "Unable to predicate " << I << "!\n"; |
Torok Edwin | 08954aa | 2009-07-12 20:07:01 +0000 | [diff] [blame] | 1881 | #endif |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1882 | llvm_unreachable(nullptr); |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 1883 | } |
Evan Cheng | f128bdc | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 1884 | } |
| 1885 | |
Bob Wilson | 4581434 | 2010-06-19 05:33:57 +0000 | [diff] [blame] | 1886 | // If the predicated instruction now redefines a register as the result of |
Evan Cheng | f128bdc | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 1887 | // if-conversion, add an implicit kill. |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1888 | UpdatePredRedefs(*MI, Redefs); |
Matthias Braun | d616ccc | 2013-10-11 19:04:37 +0000 | [diff] [blame] | 1889 | |
| 1890 | // Some kill flags may not be correct anymore. |
Andrew Trick | 276dd45 | 2013-10-14 20:45:17 +0000 | [diff] [blame] | 1891 | if (!DontKill.empty()) |
Juergen Ributzka | 310034e | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 1892 | RemoveKills(*MI, DontKill); |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 1893 | } |
| 1894 | |
Bob Wilson | 1e5da55 | 2010-06-29 00:55:23 +0000 | [diff] [blame] | 1895 | if (!IgnoreBr) { |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1896 | std::vector<MachineBasicBlock *> Succs(FromMBB.succ_begin(), |
| 1897 | FromMBB.succ_end()); |
| 1898 | MachineBasicBlock *NBB = getNextBlock(FromMBB); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1899 | MachineBasicBlock *FallThrough = FromBBI.HasFallThrough ? NBB : nullptr; |
Evan Cheng | 0598b2d | 2007-06-18 22:44:57 +0000 | [diff] [blame] | 1900 | |
Matthias Braun | b1e0558 | 2016-08-17 02:51:59 +0000 | [diff] [blame] | 1901 | for (MachineBasicBlock *Succ : Succs) { |
Bob Wilson | 1e5da55 | 2010-06-29 00:55:23 +0000 | [diff] [blame] | 1902 | // Fallthrough edge can't be transferred. |
| 1903 | if (Succ == FallThrough) |
| 1904 | continue; |
| 1905 | ToBBI.BB->addSuccessor(Succ); |
| 1906 | } |
Evan Cheng | 0598b2d | 2007-06-18 22:44:57 +0000 | [diff] [blame] | 1907 | } |
| 1908 | |
Benjamin Kramer | 4f6ac16 | 2015-02-28 10:11:12 +0000 | [diff] [blame] | 1909 | ToBBI.Predicate.append(FromBBI.Predicate.begin(), FromBBI.Predicate.end()); |
| 1910 | ToBBI.Predicate.append(Cond.begin(), Cond.end()); |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 1911 | |
| 1912 | ToBBI.ClobbersPred |= FromBBI.ClobbersPred; |
| 1913 | ToBBI.IsAnalyzed = false; |
| 1914 | |
Dan Gohman | d2d1ae1 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 1915 | ++NumDupBBs; |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 1916 | } |
| 1917 | |
Matthias Braun | 2c93179 | 2016-08-17 02:51:57 +0000 | [diff] [blame] | 1918 | /// Move all instructions from FromBB to the end of ToBB. This will leave |
| 1919 | /// FromBB as an empty block, so remove all of its successor edges except for |
| 1920 | /// the fall-through edge. If AddEdges is true, i.e., when FromBBI's branch is |
| 1921 | /// being moved, add those successor edges to ToBBI. |
Bob Wilson | 1e5da55 | 2010-06-29 00:55:23 +0000 | [diff] [blame] | 1922 | void IfConverter::MergeBlocks(BBInfo &ToBBI, BBInfo &FromBBI, bool AddEdges) { |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1923 | MachineBasicBlock &FromMBB = *FromBBI.BB; |
| 1924 | assert(!FromMBB.hasAddressTaken() && |
Evan Cheng | 8b8e8d8 | 2013-05-05 18:03:49 +0000 | [diff] [blame] | 1925 | "Removing a BB whose address is taken!"); |
| 1926 | |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1927 | // In case FromMBB contains terminators (e.g. return instruction), |
Krzysztof Parzyszek | 2451c48 | 2016-01-20 13:14:52 +0000 | [diff] [blame] | 1928 | // first move the non-terminator instructions, then the terminators. |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1929 | MachineBasicBlock::iterator FromTI = FromMBB.getFirstTerminator(); |
Krzysztof Parzyszek | 2451c48 | 2016-01-20 13:14:52 +0000 | [diff] [blame] | 1930 | MachineBasicBlock::iterator ToTI = ToBBI.BB->getFirstTerminator(); |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1931 | ToBBI.BB->splice(ToTI, &FromMBB, FromMBB.begin(), FromTI); |
Krzysztof Parzyszek | 2451c48 | 2016-01-20 13:14:52 +0000 | [diff] [blame] | 1932 | |
| 1933 | // If FromBB has non-predicated terminator we should copy it at the end. |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1934 | if (FromTI != FromMBB.end() && !TII->isPredicated(*FromTI)) |
Krzysztof Parzyszek | 2451c48 | 2016-01-20 13:14:52 +0000 | [diff] [blame] | 1935 | ToTI = ToBBI.BB->end(); |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1936 | ToBBI.BB->splice(ToTI, &FromMBB, FromTI, FromMBB.end()); |
Evan Cheng | d0e6691 | 2007-05-23 07:23:16 +0000 | [diff] [blame] | 1937 | |
Cong Hou | b9e8d48 | 2015-12-17 01:29:08 +0000 | [diff] [blame] | 1938 | // Force normalizing the successors' probabilities of ToBBI.BB to convert all |
| 1939 | // unknown probabilities into known ones. |
| 1940 | // FIXME: This usage is too tricky and in the future we would like to |
| 1941 | // eliminate all unknown probabilities in MBB. |
| 1942 | ToBBI.BB->normalizeSuccProbs(); |
| 1943 | |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1944 | SmallVector<MachineBasicBlock *, 4> FromSuccs(FromMBB.succ_begin(), |
| 1945 | FromMBB.succ_end()); |
| 1946 | MachineBasicBlock *NBB = getNextBlock(FromMBB); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1947 | MachineBasicBlock *FallThrough = FromBBI.HasFallThrough ? NBB : nullptr; |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1948 | // The edge probability from ToBBI.BB to FromMBB, which is only needed when |
| 1949 | // AddEdges is true and FromMBB is a successor of ToBBI.BB. |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 1950 | auto To2FromProb = BranchProbability::getZero(); |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1951 | if (AddEdges && ToBBI.BB->isSuccessor(&FromMBB)) { |
| 1952 | To2FromProb = MBPI->getEdgeProbability(ToBBI.BB, &FromMBB); |
| 1953 | // Set the edge probability from ToBBI.BB to FromMBB to zero to avoid the |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 1954 | // edge probability being merged to other edges when this edge is removed |
| 1955 | // later. |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1956 | ToBBI.BB->setSuccProbability(find(ToBBI.BB->successors(), &FromMBB), |
David Majnemer | 4253126 | 2016-08-12 03:55:06 +0000 | [diff] [blame] | 1957 | BranchProbability::getZero()); |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 1958 | } |
| 1959 | |
Matthias Braun | b1e0558 | 2016-08-17 02:51:59 +0000 | [diff] [blame] | 1960 | for (MachineBasicBlock *Succ : FromSuccs) { |
Evan Cheng | 288f154 | 2007-06-08 22:01:07 +0000 | [diff] [blame] | 1961 | // Fallthrough edge can't be transferred. |
Evan Cheng | be9859e | 2007-06-07 02:12:15 +0000 | [diff] [blame] | 1962 | if (Succ == FallThrough) |
| 1963 | continue; |
Cong Hou | d40105d | 2015-09-18 20:22:41 +0000 | [diff] [blame] | 1964 | |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 1965 | auto NewProb = BranchProbability::getZero(); |
Cong Hou | d40105d | 2015-09-18 20:22:41 +0000 | [diff] [blame] | 1966 | if (AddEdges) { |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 1967 | // Calculate the edge probability for the edge from ToBBI.BB to Succ, |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1968 | // which is a portion of the edge probability from FromMBB to Succ. The |
| 1969 | // portion ratio is the edge probability from ToBBI.BB to FromMBB (if |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 1970 | // FromBBI is a successor of ToBBI.BB. See comment below for excepion). |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1971 | NewProb = MBPI->getEdgeProbability(&FromMBB, Succ); |
Cong Hou | d40105d | 2015-09-18 20:22:41 +0000 | [diff] [blame] | 1972 | |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1973 | // To2FromProb is 0 when FromMBB is not a successor of ToBBI.BB. This |
| 1974 | // only happens when if-converting a diamond CFG and FromMBB is the |
| 1975 | // tail BB. In this case FromMBB post-dominates ToBBI.BB and hence we |
| 1976 | // could just use the probabilities on FromMBB's out-edges when adding |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 1977 | // new successors. |
| 1978 | if (!To2FromProb.isZero()) |
| 1979 | NewProb *= To2FromProb; |
Cong Hou | d40105d | 2015-09-18 20:22:41 +0000 | [diff] [blame] | 1980 | } |
| 1981 | |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1982 | FromMBB.removeSuccessor(Succ); |
Cong Hou | d40105d | 2015-09-18 20:22:41 +0000 | [diff] [blame] | 1983 | |
| 1984 | if (AddEdges) { |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 1985 | // If the edge from ToBBI.BB to Succ already exists, update the |
Cong Hou | c106989 | 2015-12-13 09:26:17 +0000 | [diff] [blame] | 1986 | // probability of this edge by adding NewProb to it. An example is shown |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 1987 | // below, in which A is ToBBI.BB and B is FromMBB. In this case we |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 1988 | // don't have to set C as A's successor as it already is. We only need to |
| 1989 | // update the edge probability on A->C. Note that B will not be |
| 1990 | // immediately removed from A's successors. It is possible that B->D is |
| 1991 | // not removed either if D is a fallthrough of B. Later the edge A->D |
| 1992 | // (generated here) and B->D will be combined into one edge. To maintain |
| 1993 | // correct edge probability of this combined edge, we need to set the edge |
| 1994 | // probability of A->B to zero, which is already done above. The edge |
| 1995 | // probability on A->D is calculated by scaling the original probability |
| 1996 | // on A->B by the probability of B->D. |
Cong Hou | d40105d | 2015-09-18 20:22:41 +0000 | [diff] [blame] | 1997 | // |
| 1998 | // Before ifcvt: After ifcvt (assume B->D is kept): |
| 1999 | // |
| 2000 | // A A |
| 2001 | // /| /|\ |
| 2002 | // / B / B| |
| 2003 | // | /| | || |
| 2004 | // |/ | | |/ |
| 2005 | // C D C D |
| 2006 | // |
| 2007 | if (ToBBI.BB->isSuccessor(Succ)) |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 2008 | ToBBI.BB->setSuccProbability( |
David Majnemer | 4253126 | 2016-08-12 03:55:06 +0000 | [diff] [blame] | 2009 | find(ToBBI.BB->successors(), Succ), |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 2010 | MBPI->getEdgeProbability(ToBBI.BB, Succ) + NewProb); |
Cong Hou | d40105d | 2015-09-18 20:22:41 +0000 | [diff] [blame] | 2011 | else |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 2012 | ToBBI.BB->addSuccessor(Succ, NewProb); |
Cong Hou | d40105d | 2015-09-18 20:22:41 +0000 | [diff] [blame] | 2013 | } |
Evan Cheng | be9859e | 2007-06-07 02:12:15 +0000 | [diff] [blame] | 2014 | } |
| 2015 | |
Bob Wilson | 2371f4f | 2009-05-13 23:25:24 +0000 | [diff] [blame] | 2016 | // Now FromBBI always falls through to the next block! |
Matthias Braun | 08f4704 | 2016-08-17 02:52:01 +0000 | [diff] [blame] | 2017 | if (NBB && !FromMBB.isSuccessor(NBB)) |
| 2018 | FromMBB.addSuccessor(NBB); |
Evan Cheng | 288f154 | 2007-06-08 22:01:07 +0000 | [diff] [blame] | 2019 | |
Cong Hou | c106989 | 2015-12-13 09:26:17 +0000 | [diff] [blame] | 2020 | // Normalize the probabilities of ToBBI.BB's successors with all adjustment |
| 2021 | // we've done above. |
| 2022 | ToBBI.BB->normalizeSuccProbs(); |
| 2023 | |
Benjamin Kramer | 4f6ac16 | 2015-02-28 10:11:12 +0000 | [diff] [blame] | 2024 | ToBBI.Predicate.append(FromBBI.Predicate.begin(), FromBBI.Predicate.end()); |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 2025 | FromBBI.Predicate.clear(); |
| 2026 | |
Evan Cheng | d0e6691 | 2007-05-23 07:23:16 +0000 | [diff] [blame] | 2027 | ToBBI.NonPredSize += FromBBI.NonPredSize; |
Bob Wilson | efd360c | 2010-10-26 00:02:21 +0000 | [diff] [blame] | 2028 | ToBBI.ExtraCost += FromBBI.ExtraCost; |
Evan Cheng | debf9c5 | 2010-11-03 00:45:17 +0000 | [diff] [blame] | 2029 | ToBBI.ExtraCost2 += FromBBI.ExtraCost2; |
Evan Cheng | d0e6691 | 2007-05-23 07:23:16 +0000 | [diff] [blame] | 2030 | FromBBI.NonPredSize = 0; |
Bob Wilson | efd360c | 2010-10-26 00:02:21 +0000 | [diff] [blame] | 2031 | FromBBI.ExtraCost = 0; |
Evan Cheng | debf9c5 | 2010-11-03 00:45:17 +0000 | [diff] [blame] | 2032 | FromBBI.ExtraCost2 = 0; |
Evan Cheng | 9030b98 | 2007-06-06 10:16:17 +0000 | [diff] [blame] | 2033 | |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 2034 | ToBBI.ClobbersPred |= FromBBI.ClobbersPred; |
Evan Cheng | 2117d1f | 2007-06-09 01:03:43 +0000 | [diff] [blame] | 2035 | ToBBI.HasFallThrough = FromBBI.HasFallThrough; |
Evan Cheng | 1e6f08b | 2007-06-14 20:28:52 +0000 | [diff] [blame] | 2036 | ToBBI.IsAnalyzed = false; |
| 2037 | FromBBI.IsAnalyzed = false; |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 2038 | } |
Akira Hatanaka | 4a61619 | 2015-06-08 18:50:43 +0000 | [diff] [blame] | 2039 | |
| 2040 | FunctionPass * |
| 2041 | llvm::createIfConverter(std::function<bool(const Function &)> Ftor) { |
Benjamin Kramer | d3f4c05 | 2016-06-12 16:13:55 +0000 | [diff] [blame] | 2042 | return new IfConverter(std::move(Ftor)); |
Akira Hatanaka | 4a61619 | 2015-06-08 18:50:43 +0000 | [diff] [blame] | 2043 | } |