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