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