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