| Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 1 | //===-- SystemZElimCompare.cpp - Eliminate comparison instructions --------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass: |
| 11 | // (1) tries to remove compares if CC already contains the required information |
| 12 | // (2) fuses compares and branches into COMPARE AND BRANCH instructions |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| Eugene Zelenko | 3943d2b | 2017-01-24 22:10:43 +0000 | [diff] [blame] | 16 | #include "SystemZ.h" |
| 17 | #include "SystemZInstrInfo.h" |
| Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 18 | #include "SystemZTargetMachine.h" |
| Eugene Zelenko | 3943d2b | 2017-01-24 22:10:43 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallVector.h" |
| Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/Statistic.h" |
| Eugene Zelenko | 3943d2b | 2017-01-24 22:10:43 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/StringRef.h" |
| 22 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 23 | #include "llvm/CodeGen/MachineFunction.h" |
| Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| Eugene Zelenko | 3943d2b | 2017-01-24 22:10:43 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineInstr.h" |
| Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| Eugene Zelenko | 3943d2b | 2017-01-24 22:10:43 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachineOperand.h" |
| David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
| 29 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
| Eugene Zelenko | 3943d2b | 2017-01-24 22:10:43 +0000 | [diff] [blame] | 30 | #include "llvm/MC/MCInstrDesc.h" |
| Eugene Zelenko | 3943d2b | 2017-01-24 22:10:43 +0000 | [diff] [blame] | 31 | #include <cassert> |
| 32 | #include <cstdint> |
| Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 33 | |
| 34 | using namespace llvm; |
| 35 | |
| Chandler Carruth | 84e68b2 | 2014-04-22 02:41:26 +0000 | [diff] [blame] | 36 | #define DEBUG_TYPE "systemz-elim-compare" |
| 37 | |
| Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 38 | STATISTIC(BranchOnCounts, "Number of branch-on-count instructions"); |
| Ulrich Weigand | 2d9e3d9 | 2016-11-28 13:59:22 +0000 | [diff] [blame] | 39 | STATISTIC(LoadAndTraps, "Number of load-and-trap instructions"); |
| Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 40 | STATISTIC(EliminatedComparisons, "Number of eliminated comparisons"); |
| 41 | STATISTIC(FusedComparisons, "Number of fused compare-and-branch instructions"); |
| 42 | |
| 43 | namespace { |
| Eugene Zelenko | 3943d2b | 2017-01-24 22:10:43 +0000 | [diff] [blame] | 44 | |
| Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 45 | // Represents the references to a particular register in one or more |
| 46 | // instructions. |
| 47 | struct Reference { |
| Eugene Zelenko | 3943d2b | 2017-01-24 22:10:43 +0000 | [diff] [blame] | 48 | Reference() = default; |
| Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 49 | |
| Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 50 | Reference &operator|=(const Reference &Other) { |
| 51 | Def |= Other.Def; |
| Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 52 | Use |= Other.Use; |
| Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 53 | return *this; |
| 54 | } |
| Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 55 | |
| Aaron Ballman | b46962f | 2015-02-15 22:00:20 +0000 | [diff] [blame] | 56 | explicit operator bool() const { return Def || Use; } |
| Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 57 | |
| Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 58 | // True if the register is defined or used in some form, either directly or |
| 59 | // via a sub- or super-register. |
| Eugene Zelenko | 3943d2b | 2017-01-24 22:10:43 +0000 | [diff] [blame] | 60 | bool Def = false; |
| 61 | bool Use = false; |
| Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 62 | }; |
| Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 63 | |
| Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 64 | class SystemZElimCompare : public MachineFunctionPass { |
| 65 | public: |
| 66 | static char ID; |
| Eugene Zelenko | 3943d2b | 2017-01-24 22:10:43 +0000 | [diff] [blame] | 67 | |
| Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 68 | SystemZElimCompare(const SystemZTargetMachine &tm) |
| Eugene Zelenko | 3943d2b | 2017-01-24 22:10:43 +0000 | [diff] [blame] | 69 | : MachineFunctionPass(ID) {} |
| Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 70 | |
| Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 71 | StringRef getPassName() const override { |
| Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 72 | return "SystemZ Comparison Elimination"; |
| 73 | } |
| Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 74 | |
| Richard Sandiford | 28c111e | 2014-03-06 11:00:15 +0000 | [diff] [blame] | 75 | bool processBlock(MachineBasicBlock &MBB); |
| Craig Topper | 9d74a5a | 2014-04-29 07:58:41 +0000 | [diff] [blame] | 76 | bool runOnMachineFunction(MachineFunction &F) override; |
| Eugene Zelenko | 3943d2b | 2017-01-24 22:10:43 +0000 | [diff] [blame] | 77 | |
| Derek Schuff | 1dbf7a5 | 2016-04-04 17:09:25 +0000 | [diff] [blame] | 78 | MachineFunctionProperties getRequiredProperties() const override { |
| 79 | return MachineFunctionProperties().set( |
| Matthias Braun | 1eb4736 | 2016-08-25 01:27:13 +0000 | [diff] [blame] | 80 | MachineFunctionProperties::Property::NoVRegs); |
| Derek Schuff | 1dbf7a5 | 2016-04-04 17:09:25 +0000 | [diff] [blame] | 81 | } |
| Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 82 | |
| Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 83 | private: |
| Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 84 | Reference getRegReferences(MachineInstr &MI, unsigned Reg); |
| 85 | bool convertToBRCT(MachineInstr &MI, MachineInstr &Compare, |
| Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 86 | SmallVectorImpl<MachineInstr *> &CCUsers); |
| Ulrich Weigand | 2d9e3d9 | 2016-11-28 13:59:22 +0000 | [diff] [blame] | 87 | bool convertToLoadAndTrap(MachineInstr &MI, MachineInstr &Compare, |
| 88 | SmallVectorImpl<MachineInstr *> &CCUsers); |
| Jonas Paulsson | 776a81a | 2018-01-15 15:41:26 +0000 | [diff] [blame] | 89 | bool convertToLoadAndTest(MachineInstr &MI, MachineInstr &Compare, |
| 90 | SmallVectorImpl<MachineInstr *> &CCUsers); |
| Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 91 | bool adjustCCMasksForInstr(MachineInstr &MI, MachineInstr &Compare, |
| Jonas Paulsson | 776a81a | 2018-01-15 15:41:26 +0000 | [diff] [blame] | 92 | SmallVectorImpl<MachineInstr *> &CCUsers, |
| 93 | unsigned ConvOpc = 0); |
| Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 94 | bool optimizeCompareZero(MachineInstr &Compare, |
| Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 95 | SmallVectorImpl<MachineInstr *> &CCUsers); |
| Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 96 | bool fuseCompareOperations(MachineInstr &Compare, |
| Zhan Jun Liau | ab42cbc | 2016-06-10 19:58:10 +0000 | [diff] [blame] | 97 | SmallVectorImpl<MachineInstr *> &CCUsers); |
| Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 98 | |
| Eugene Zelenko | 3943d2b | 2017-01-24 22:10:43 +0000 | [diff] [blame] | 99 | const SystemZInstrInfo *TII = nullptr; |
| 100 | const TargetRegisterInfo *TRI = nullptr; |
| Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 101 | }; |
| Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 102 | |
| Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 103 | char SystemZElimCompare::ID = 0; |
| Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 104 | |
| Eugene Zelenko | 3943d2b | 2017-01-24 22:10:43 +0000 | [diff] [blame] | 105 | } // end anonymous namespace |
| Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 106 | |
| 107 | // Return true if CC is live out of MBB. |
| Richard Sandiford | 28c111e | 2014-03-06 11:00:15 +0000 | [diff] [blame] | 108 | static bool isCCLiveOut(MachineBasicBlock &MBB) { |
| 109 | for (auto SI = MBB.succ_begin(), SE = MBB.succ_end(); SI != SE; ++SI) |
| Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 110 | if ((*SI)->isLiveIn(SystemZ::CC)) |
| 111 | return true; |
| 112 | return false; |
| 113 | } |
| 114 | |
| Jonas Paulsson | b0e8a2e | 2017-09-21 13:52:24 +0000 | [diff] [blame] | 115 | // Returns true if MI is an instruction whose output equals the value in Reg. |
| 116 | static bool preservesValueOf(MachineInstr &MI, unsigned Reg) { |
| Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 117 | switch (MI.getOpcode()) { |
| Richard Sandiford | b49a3ab | 2013-08-05 11:03:20 +0000 | [diff] [blame] | 118 | case SystemZ::LR: |
| 119 | case SystemZ::LGR: |
| 120 | case SystemZ::LGFR: |
| 121 | case SystemZ::LTR: |
| 122 | case SystemZ::LTGR: |
| 123 | case SystemZ::LTGFR: |
| Richard Sandiford | 0897fce | 2013-08-07 11:10:06 +0000 | [diff] [blame] | 124 | case SystemZ::LER: |
| 125 | case SystemZ::LDR: |
| 126 | case SystemZ::LXR: |
| 127 | case SystemZ::LTEBR: |
| 128 | case SystemZ::LTDBR: |
| 129 | case SystemZ::LTXBR: |
| Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 130 | if (MI.getOperand(1).getReg() == Reg) |
| Richard Sandiford | b49a3ab | 2013-08-05 11:03:20 +0000 | [diff] [blame] | 131 | return true; |
| 132 | } |
| 133 | |
| Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 134 | return false; |
| 135 | } |
| 136 | |
| Jonas Paulsson | b0e8a2e | 2017-09-21 13:52:24 +0000 | [diff] [blame] | 137 | // Return true if any CC result of MI would (perhaps after conversion) |
| 138 | // reflect the value of Reg. |
| 139 | static bool resultTests(MachineInstr &MI, unsigned Reg) { |
| 140 | if (MI.getNumOperands() > 0 && MI.getOperand(0).isReg() && |
| 141 | MI.getOperand(0).isDef() && MI.getOperand(0).getReg() == Reg) |
| 142 | return true; |
| 143 | |
| 144 | return (preservesValueOf(MI, Reg)); |
| 145 | } |
| 146 | |
| Jonas Paulsson | ee3685f | 2015-10-09 11:27:44 +0000 | [diff] [blame] | 147 | // Describe the references to Reg or any of its aliases in MI. |
| Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 148 | Reference SystemZElimCompare::getRegReferences(MachineInstr &MI, unsigned Reg) { |
| Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 149 | Reference Ref; |
| Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 150 | for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) { |
| 151 | const MachineOperand &MO = MI.getOperand(I); |
| Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 152 | if (MO.isReg()) { |
| 153 | if (unsigned MOReg = MO.getReg()) { |
| Jonas Paulsson | ee3685f | 2015-10-09 11:27:44 +0000 | [diff] [blame] | 154 | if (TRI->regsOverlap(MOReg, Reg)) { |
| 155 | if (MO.isUse()) |
| Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 156 | Ref.Use = true; |
| Jonas Paulsson | ee3685f | 2015-10-09 11:27:44 +0000 | [diff] [blame] | 157 | else if (MO.isDef()) |
| Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 158 | Ref.Def = true; |
| Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | return Ref; |
| 164 | } |
| 165 | |
| Jonas Paulsson | 5d3fbd3 | 2015-10-08 07:40:23 +0000 | [diff] [blame] | 166 | // Return true if this is a load and test which can be optimized the |
| 167 | // same way as compare instruction. |
| Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 168 | static bool isLoadAndTestAsCmp(MachineInstr &MI) { |
| Jonas Paulsson | 5d3fbd3 | 2015-10-08 07:40:23 +0000 | [diff] [blame] | 169 | // If we during isel used a load-and-test as a compare with 0, the |
| 170 | // def operand is dead. |
| Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 171 | return (MI.getOpcode() == SystemZ::LTEBR || |
| 172 | MI.getOpcode() == SystemZ::LTDBR || |
| 173 | MI.getOpcode() == SystemZ::LTXBR) && |
| 174 | MI.getOperand(0).isDead(); |
| Jonas Paulsson | 5d3fbd3 | 2015-10-08 07:40:23 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | // Return the source register of Compare, which is the unknown value |
| 178 | // being tested. |
| Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 179 | static unsigned getCompareSourceReg(MachineInstr &Compare) { |
| Jonas Paulsson | 5d3fbd3 | 2015-10-08 07:40:23 +0000 | [diff] [blame] | 180 | unsigned reg = 0; |
| Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 181 | if (Compare.isCompare()) |
| 182 | reg = Compare.getOperand(0).getReg(); |
| Jonas Paulsson | 5d3fbd3 | 2015-10-08 07:40:23 +0000 | [diff] [blame] | 183 | else if (isLoadAndTestAsCmp(Compare)) |
| Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 184 | reg = Compare.getOperand(1).getReg(); |
| Eugene Zelenko | 3943d2b | 2017-01-24 22:10:43 +0000 | [diff] [blame] | 185 | assert(reg); |
| Jonas Paulsson | 5d3fbd3 | 2015-10-08 07:40:23 +0000 | [diff] [blame] | 186 | |
| 187 | return reg; |
| 188 | } |
| 189 | |
| Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 190 | // Compare compares the result of MI against zero. If MI is an addition |
| 191 | // of -1 and if CCUsers is a single branch on nonzero, eliminate the addition |
| Ulrich Weigand | 7583991 | 2016-11-28 13:40:08 +0000 | [diff] [blame] | 192 | // and convert the branch to a BRCT(G) or BRCTH. Return true on success. |
| Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 193 | bool SystemZElimCompare::convertToBRCT( |
| 194 | MachineInstr &MI, MachineInstr &Compare, |
| 195 | SmallVectorImpl<MachineInstr *> &CCUsers) { |
| Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 196 | // Check whether we have an addition of -1. |
| Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 197 | unsigned Opcode = MI.getOpcode(); |
| Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 198 | unsigned BRCT; |
| 199 | if (Opcode == SystemZ::AHI) |
| 200 | BRCT = SystemZ::BRCT; |
| 201 | else if (Opcode == SystemZ::AGHI) |
| 202 | BRCT = SystemZ::BRCTG; |
| Ulrich Weigand | 7583991 | 2016-11-28 13:40:08 +0000 | [diff] [blame] | 203 | else if (Opcode == SystemZ::AIH) |
| 204 | BRCT = SystemZ::BRCTH; |
| Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 205 | else |
| 206 | return false; |
| Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 207 | if (MI.getOperand(2).getImm() != -1) |
| Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 208 | return false; |
| 209 | |
| 210 | // Check whether we have a single JLH. |
| 211 | if (CCUsers.size() != 1) |
| 212 | return false; |
| 213 | MachineInstr *Branch = CCUsers[0]; |
| 214 | if (Branch->getOpcode() != SystemZ::BRC || |
| 215 | Branch->getOperand(0).getImm() != SystemZ::CCMASK_ICMP || |
| 216 | Branch->getOperand(1).getImm() != SystemZ::CCMASK_CMP_NE) |
| 217 | return false; |
| 218 | |
| 219 | // We already know that there are no references to the register between |
| 220 | // MI and Compare. Make sure that there are also no references between |
| 221 | // Compare and Branch. |
| Jonas Paulsson | 5d3fbd3 | 2015-10-08 07:40:23 +0000 | [diff] [blame] | 222 | unsigned SrcReg = getCompareSourceReg(Compare); |
| Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 223 | MachineBasicBlock::iterator MBBI = Compare, MBBE = Branch; |
| 224 | for (++MBBI; MBBI != MBBE; ++MBBI) |
| Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 225 | if (getRegReferences(*MBBI, SrcReg)) |
| Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 226 | return false; |
| 227 | |
| Ulrich Weigand | 7583991 | 2016-11-28 13:40:08 +0000 | [diff] [blame] | 228 | // The transformation is OK. Rebuild Branch as a BRCT(G) or BRCTH. |
| Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 229 | MachineOperand Target(Branch->getOperand(2)); |
| Jonas Paulsson | 63a2b68 | 2015-10-10 07:14:24 +0000 | [diff] [blame] | 230 | while (Branch->getNumOperands()) |
| 231 | Branch->RemoveOperand(0); |
| Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 232 | Branch->setDesc(TII->get(BRCT)); |
| Ulrich Weigand | 7583991 | 2016-11-28 13:40:08 +0000 | [diff] [blame] | 233 | MachineInstrBuilder MIB(*Branch->getParent()->getParent(), Branch); |
| Diana Picus | 116bbab | 2017-01-13 09:58:52 +0000 | [diff] [blame] | 234 | MIB.add(MI.getOperand(0)).add(MI.getOperand(1)).add(Target); |
| Ulrich Weigand | 7583991 | 2016-11-28 13:40:08 +0000 | [diff] [blame] | 235 | // Add a CC def to BRCT(G), since we may have to split them again if the |
| 236 | // branch displacement overflows. BRCTH has a 32-bit displacement, so |
| 237 | // this is not necessary there. |
| 238 | if (BRCT != SystemZ::BRCTH) |
| 239 | MIB.addReg(SystemZ::CC, RegState::ImplicitDefine | RegState::Dead); |
| Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 240 | MI.eraseFromParent(); |
| Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 241 | return true; |
| 242 | } |
| 243 | |
| Ulrich Weigand | 2d9e3d9 | 2016-11-28 13:59:22 +0000 | [diff] [blame] | 244 | // Compare compares the result of MI against zero. If MI is a suitable load |
| 245 | // instruction and if CCUsers is a single conditional trap on zero, eliminate |
| 246 | // the load and convert the branch to a load-and-trap. Return true on success. |
| 247 | bool SystemZElimCompare::convertToLoadAndTrap( |
| 248 | MachineInstr &MI, MachineInstr &Compare, |
| 249 | SmallVectorImpl<MachineInstr *> &CCUsers) { |
| 250 | unsigned LATOpcode = TII->getLoadAndTrap(MI.getOpcode()); |
| 251 | if (!LATOpcode) |
| 252 | return false; |
| 253 | |
| 254 | // Check whether we have a single CondTrap that traps on zero. |
| 255 | if (CCUsers.size() != 1) |
| 256 | return false; |
| 257 | MachineInstr *Branch = CCUsers[0]; |
| 258 | if (Branch->getOpcode() != SystemZ::CondTrap || |
| 259 | Branch->getOperand(0).getImm() != SystemZ::CCMASK_ICMP || |
| 260 | Branch->getOperand(1).getImm() != SystemZ::CCMASK_CMP_EQ) |
| 261 | return false; |
| 262 | |
| 263 | // We already know that there are no references to the register between |
| 264 | // MI and Compare. Make sure that there are also no references between |
| 265 | // Compare and Branch. |
| 266 | unsigned SrcReg = getCompareSourceReg(Compare); |
| 267 | MachineBasicBlock::iterator MBBI = Compare, MBBE = Branch; |
| 268 | for (++MBBI; MBBI != MBBE; ++MBBI) |
| 269 | if (getRegReferences(*MBBI, SrcReg)) |
| 270 | return false; |
| 271 | |
| 272 | // The transformation is OK. Rebuild Branch as a load-and-trap. |
| Ulrich Weigand | 2d9e3d9 | 2016-11-28 13:59:22 +0000 | [diff] [blame] | 273 | while (Branch->getNumOperands()) |
| 274 | Branch->RemoveOperand(0); |
| 275 | Branch->setDesc(TII->get(LATOpcode)); |
| 276 | MachineInstrBuilder(*Branch->getParent()->getParent(), Branch) |
| Diana Picus | 116bbab | 2017-01-13 09:58:52 +0000 | [diff] [blame] | 277 | .add(MI.getOperand(0)) |
| 278 | .add(MI.getOperand(1)) |
| 279 | .add(MI.getOperand(2)) |
| 280 | .add(MI.getOperand(3)); |
| Ulrich Weigand | 2d9e3d9 | 2016-11-28 13:59:22 +0000 | [diff] [blame] | 281 | MI.eraseFromParent(); |
| 282 | return true; |
| 283 | } |
| 284 | |
| Richard Sandiford | b49a3ab | 2013-08-05 11:03:20 +0000 | [diff] [blame] | 285 | // If MI is a load instruction, try to convert it into a LOAD AND TEST. |
| 286 | // Return true on success. |
| Jonas Paulsson | 776a81a | 2018-01-15 15:41:26 +0000 | [diff] [blame] | 287 | bool SystemZElimCompare::convertToLoadAndTest( |
| 288 | MachineInstr &MI, MachineInstr &Compare, |
| 289 | SmallVectorImpl<MachineInstr *> &CCUsers) { |
| 290 | |
| 291 | // Try to adjust CC masks for the LOAD AND TEST opcode that could replace MI. |
| Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 292 | unsigned Opcode = TII->getLoadAndTest(MI.getOpcode()); |
| Jonas Paulsson | 776a81a | 2018-01-15 15:41:26 +0000 | [diff] [blame] | 293 | if (!Opcode || !adjustCCMasksForInstr(MI, Compare, CCUsers, Opcode)) |
| Richard Sandiford | b49a3ab | 2013-08-05 11:03:20 +0000 | [diff] [blame] | 294 | return false; |
| 295 | |
| Jonas Paulsson | e80d405 | 2018-06-07 05:59:07 +0000 | [diff] [blame^] | 296 | // Rebuild to get the CC operand in the right place. |
| 297 | MachineInstr *BuiltMI = |
| 298 | BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), TII->get(Opcode)); |
| 299 | for (const auto &MO : MI.operands()) |
| 300 | BuiltMI->addOperand(MO); |
| 301 | BuiltMI->setMemRefs(MI.memoperands_begin(), MI.memoperands_end()); |
| 302 | MI.eraseFromParent(); |
| 303 | |
| Richard Sandiford | b49a3ab | 2013-08-05 11:03:20 +0000 | [diff] [blame] | 304 | return true; |
| 305 | } |
| 306 | |
| Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 307 | // The CC users in CCUsers are testing the result of a comparison of some |
| Jonas Paulsson | 776a81a | 2018-01-15 15:41:26 +0000 | [diff] [blame] | 308 | // value X against zero and we know that any CC value produced by MI would |
| 309 | // also reflect the value of X. ConvOpc may be used to pass the transfomed |
| 310 | // opcode MI will have if this succeeds. Try to adjust CCUsers so that they |
| 311 | // test the result of MI directly, returning true on success. Leave |
| 312 | // everything unchanged on failure. |
| Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 313 | bool SystemZElimCompare::adjustCCMasksForInstr( |
| 314 | MachineInstr &MI, MachineInstr &Compare, |
| Jonas Paulsson | 776a81a | 2018-01-15 15:41:26 +0000 | [diff] [blame] | 315 | SmallVectorImpl<MachineInstr *> &CCUsers, |
| 316 | unsigned ConvOpc) { |
| 317 | int Opcode = (ConvOpc ? ConvOpc : MI.getOpcode()); |
| Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 318 | const MCInstrDesc &Desc = TII->get(Opcode); |
| 319 | unsigned MIFlags = Desc.TSFlags; |
| 320 | |
| 321 | // See which compare-style condition codes are available. |
| Richard Sandiford | 0897fce | 2013-08-07 11:10:06 +0000 | [diff] [blame] | 322 | unsigned ReusableCCMask = SystemZII::getCompareZeroCCMask(MIFlags); |
| Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 323 | |
| 324 | // For unsigned comparisons with zero, only equality makes sense. |
| Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 325 | unsigned CompareFlags = Compare.getDesc().TSFlags; |
| Richard Sandiford | 0897fce | 2013-08-07 11:10:06 +0000 | [diff] [blame] | 326 | if (CompareFlags & SystemZII::IsLogical) |
| 327 | ReusableCCMask &= SystemZ::CCMASK_CMP_EQ; |
| Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 328 | |
| 329 | if (ReusableCCMask == 0) |
| 330 | return false; |
| 331 | |
| 332 | unsigned CCValues = SystemZII::getCCValues(MIFlags); |
| 333 | assert((ReusableCCMask & ~CCValues) == 0 && "Invalid CCValues"); |
| 334 | |
| Jonas Paulsson | 776a81a | 2018-01-15 15:41:26 +0000 | [diff] [blame] | 335 | bool MIEquivalentToCmp = |
| 336 | (ReusableCCMask == CCValues && |
| 337 | CCValues == SystemZII::getCCValues(CompareFlags)); |
| Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 338 | |
| Jonas Paulsson | 776a81a | 2018-01-15 15:41:26 +0000 | [diff] [blame] | 339 | if (!MIEquivalentToCmp) { |
| 340 | // Now check whether these flags are enough for all users. |
| 341 | SmallVector<MachineOperand *, 4> AlterMasks; |
| 342 | for (unsigned int I = 0, E = CCUsers.size(); I != E; ++I) { |
| 343 | MachineInstr *MI = CCUsers[I]; |
| Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 344 | |
| Jonas Paulsson | 776a81a | 2018-01-15 15:41:26 +0000 | [diff] [blame] | 345 | // Fail if this isn't a use of CC that we understand. |
| 346 | unsigned Flags = MI->getDesc().TSFlags; |
| 347 | unsigned FirstOpNum; |
| 348 | if (Flags & SystemZII::CCMaskFirst) |
| 349 | FirstOpNum = 0; |
| 350 | else if (Flags & SystemZII::CCMaskLast) |
| 351 | FirstOpNum = MI->getNumExplicitOperands() - 2; |
| 352 | else |
| 353 | return false; |
| Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 354 | |
| Jonas Paulsson | 776a81a | 2018-01-15 15:41:26 +0000 | [diff] [blame] | 355 | // Check whether the instruction predicate treats all CC values |
| 356 | // outside of ReusableCCMask in the same way. In that case it |
| 357 | // doesn't matter what those CC values mean. |
| 358 | unsigned CCValid = MI->getOperand(FirstOpNum).getImm(); |
| 359 | unsigned CCMask = MI->getOperand(FirstOpNum + 1).getImm(); |
| 360 | unsigned OutValid = ~ReusableCCMask & CCValid; |
| 361 | unsigned OutMask = ~ReusableCCMask & CCMask; |
| 362 | if (OutMask != 0 && OutMask != OutValid) |
| 363 | return false; |
| Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 364 | |
| Jonas Paulsson | 776a81a | 2018-01-15 15:41:26 +0000 | [diff] [blame] | 365 | AlterMasks.push_back(&MI->getOperand(FirstOpNum)); |
| 366 | AlterMasks.push_back(&MI->getOperand(FirstOpNum + 1)); |
| 367 | } |
| 368 | |
| 369 | // All users are OK. Adjust the masks for MI. |
| 370 | for (unsigned I = 0, E = AlterMasks.size(); I != E; I += 2) { |
| 371 | AlterMasks[I]->setImm(CCValues); |
| 372 | unsigned CCMask = AlterMasks[I + 1]->getImm(); |
| 373 | if (CCMask & ~ReusableCCMask) |
| 374 | AlterMasks[I + 1]->setImm((CCMask & ReusableCCMask) | |
| 375 | (CCValues & ~ReusableCCMask)); |
| 376 | } |
| Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | // CC is now live after MI. |
| Jonas Paulsson | 776a81a | 2018-01-15 15:41:26 +0000 | [diff] [blame] | 380 | if (!ConvOpc) { |
| 381 | int CCDef = MI.findRegisterDefOperandIdx(SystemZ::CC, false, true, TRI); |
| 382 | assert(CCDef >= 0 && "Couldn't find CC set"); |
| 383 | MI.getOperand(CCDef).setIsDead(false); |
| 384 | } |
| 385 | |
| 386 | // Check if MI lies before Compare. |
| 387 | bool BeforeCmp = false; |
| 388 | MachineBasicBlock::iterator MBBI = MI, MBBE = MI.getParent()->end(); |
| 389 | for (++MBBI; MBBI != MBBE; ++MBBI) |
| 390 | if (MBBI == Compare) { |
| 391 | BeforeCmp = true; |
| 392 | break; |
| 393 | } |
| Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 394 | |
| 395 | // Clear any intervening kills of CC. |
| Jonas Paulsson | 776a81a | 2018-01-15 15:41:26 +0000 | [diff] [blame] | 396 | if (BeforeCmp) { |
| 397 | MachineBasicBlock::iterator MBBI = MI, MBBE = Compare; |
| 398 | for (++MBBI; MBBI != MBBE; ++MBBI) |
| 399 | MBBI->clearRegisterKills(SystemZ::CC, TRI); |
| 400 | } |
| Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 401 | |
| 402 | return true; |
| 403 | } |
| 404 | |
| Richard Sandiford | 0897fce | 2013-08-07 11:10:06 +0000 | [diff] [blame] | 405 | // Return true if Compare is a comparison against zero. |
| Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 406 | static bool isCompareZero(MachineInstr &Compare) { |
| 407 | switch (Compare.getOpcode()) { |
| Richard Sandiford | 0897fce | 2013-08-07 11:10:06 +0000 | [diff] [blame] | 408 | case SystemZ::LTEBRCompare: |
| 409 | case SystemZ::LTDBRCompare: |
| 410 | case SystemZ::LTXBRCompare: |
| 411 | return true; |
| 412 | |
| 413 | default: |
| Jonas Paulsson | 5d3fbd3 | 2015-10-08 07:40:23 +0000 | [diff] [blame] | 414 | if (isLoadAndTestAsCmp(Compare)) |
| 415 | return true; |
| Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 416 | return Compare.getNumExplicitOperands() == 2 && |
| 417 | Compare.getOperand(1).isImm() && Compare.getOperand(1).getImm() == 0; |
| Richard Sandiford | 0897fce | 2013-08-07 11:10:06 +0000 | [diff] [blame] | 418 | } |
| 419 | } |
| 420 | |
| Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 421 | // Try to optimize cases where comparison instruction Compare is testing |
| 422 | // a value against zero. Return true on success and if Compare should be |
| 423 | // deleted as dead. CCUsers is the list of instructions that use the CC |
| 424 | // value produced by Compare. |
| Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 425 | bool SystemZElimCompare::optimizeCompareZero( |
| 426 | MachineInstr &Compare, SmallVectorImpl<MachineInstr *> &CCUsers) { |
| Richard Sandiford | 0897fce | 2013-08-07 11:10:06 +0000 | [diff] [blame] | 427 | if (!isCompareZero(Compare)) |
| Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 428 | return false; |
| 429 | |
| 430 | // Search back for CC results that are based on the first operand. |
| Jonas Paulsson | 5d3fbd3 | 2015-10-08 07:40:23 +0000 | [diff] [blame] | 431 | unsigned SrcReg = getCompareSourceReg(Compare); |
| Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 432 | MachineBasicBlock &MBB = *Compare.getParent(); |
| Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 433 | Reference CCRefs; |
| 434 | Reference SrcRefs; |
| Jonas Paulsson | e80d405 | 2018-06-07 05:59:07 +0000 | [diff] [blame^] | 435 | for (MachineBasicBlock::reverse_iterator MBBI = |
| 436 | std::next(MachineBasicBlock::reverse_iterator(&Compare)), |
| 437 | MBBE = MBB.rend(); MBBI != MBBE;) { |
| 438 | MachineInstr &MI = *MBBI++; |
| Jonas Paulsson | 2c96dd6 | 2015-10-08 07:40:11 +0000 | [diff] [blame] | 439 | if (resultTests(MI, SrcReg)) { |
| Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 440 | // Try to remove both MI and Compare by converting a branch to BRCT(G). |
| Ulrich Weigand | 2d9e3d9 | 2016-11-28 13:59:22 +0000 | [diff] [blame] | 441 | // or a load-and-trap instruction. We don't care in this case whether |
| 442 | // CC is modified between MI and Compare. |
| 443 | if (!CCRefs.Use && !SrcRefs) { |
| 444 | if (convertToBRCT(MI, Compare, CCUsers)) { |
| 445 | BranchOnCounts += 1; |
| 446 | return true; |
| 447 | } |
| 448 | if (convertToLoadAndTrap(MI, Compare, CCUsers)) { |
| 449 | LoadAndTraps += 1; |
| 450 | return true; |
| 451 | } |
| Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 452 | } |
| 453 | // Try to eliminate Compare by reusing a CC result from MI. |
| Jonas Paulsson | 776a81a | 2018-01-15 15:41:26 +0000 | [diff] [blame] | 454 | if ((!CCRefs && convertToLoadAndTest(MI, Compare, CCUsers)) || |
| Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 455 | (!CCRefs.Def && adjustCCMasksForInstr(MI, Compare, CCUsers))) { |
| 456 | EliminatedComparisons += 1; |
| 457 | return true; |
| 458 | } |
| Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 459 | } |
| Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 460 | SrcRefs |= getRegReferences(MI, SrcReg); |
| 461 | if (SrcRefs.Def) |
| Jonas Paulsson | b0e8a2e | 2017-09-21 13:52:24 +0000 | [diff] [blame] | 462 | break; |
| Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 463 | CCRefs |= getRegReferences(MI, SystemZ::CC); |
| 464 | if (CCRefs.Use && CCRefs.Def) |
| Jonas Paulsson | b0e8a2e | 2017-09-21 13:52:24 +0000 | [diff] [blame] | 465 | break; |
| 466 | } |
| 467 | |
| 468 | // Also do a forward search to handle cases where an instruction after the |
| Jonas Paulsson | 22f208f | 2018-01-08 12:52:40 +0000 | [diff] [blame] | 469 | // compare can be converted, like |
| 470 | // LTEBRCompare %f0s, %f0s; %f2s = LER %f0s => LTEBRCompare %f2s, %f0s |
| Jonas Paulsson | e80d405 | 2018-06-07 05:59:07 +0000 | [diff] [blame^] | 471 | for (MachineBasicBlock::iterator MBBI = |
| 472 | std::next(MachineBasicBlock::iterator(&Compare)), MBBE = MBB.end(); |
| 473 | MBBI != MBBE;) { |
| 474 | MachineInstr &MI = *MBBI++; |
| Jonas Paulsson | b0e8a2e | 2017-09-21 13:52:24 +0000 | [diff] [blame] | 475 | if (preservesValueOf(MI, SrcReg)) { |
| 476 | // Try to eliminate Compare by reusing a CC result from MI. |
| Jonas Paulsson | 776a81a | 2018-01-15 15:41:26 +0000 | [diff] [blame] | 477 | if (convertToLoadAndTest(MI, Compare, CCUsers)) { |
| Jonas Paulsson | b0e8a2e | 2017-09-21 13:52:24 +0000 | [diff] [blame] | 478 | EliminatedComparisons += 1; |
| 479 | return true; |
| 480 | } |
| 481 | } |
| 482 | if (getRegReferences(MI, SrcReg).Def) |
| 483 | return false; |
| 484 | if (getRegReferences(MI, SystemZ::CC)) |
| Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 485 | return false; |
| Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 486 | } |
| Jonas Paulsson | b0e8a2e | 2017-09-21 13:52:24 +0000 | [diff] [blame] | 487 | |
| Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 488 | return false; |
| 489 | } |
| 490 | |
| 491 | // Try to fuse comparison instruction Compare into a later branch. |
| 492 | // Return true on success and if Compare is therefore redundant. |
| Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 493 | bool SystemZElimCompare::fuseCompareOperations( |
| 494 | MachineInstr &Compare, SmallVectorImpl<MachineInstr *> &CCUsers) { |
| Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 495 | // See whether we have a single branch with which to fuse. |
| 496 | if (CCUsers.size() != 1) |
| 497 | return false; |
| 498 | MachineInstr *Branch = CCUsers[0]; |
| Zhan Jun Liau | ab42cbc | 2016-06-10 19:58:10 +0000 | [diff] [blame] | 499 | SystemZII::FusedCompareType Type; |
| Ulrich Weigand | 2eb027d | 2016-04-07 16:11:44 +0000 | [diff] [blame] | 500 | switch (Branch->getOpcode()) { |
| 501 | case SystemZ::BRC: |
| 502 | Type = SystemZII::CompareAndBranch; |
| 503 | break; |
| 504 | case SystemZ::CondReturn: |
| 505 | Type = SystemZII::CompareAndReturn; |
| 506 | break; |
| Ulrich Weigand | 848a513 | 2016-04-11 12:12:32 +0000 | [diff] [blame] | 507 | case SystemZ::CallBCR: |
| 508 | Type = SystemZII::CompareAndSibcall; |
| 509 | break; |
| Zhan Jun Liau | ab42cbc | 2016-06-10 19:58:10 +0000 | [diff] [blame] | 510 | case SystemZ::CondTrap: |
| 511 | Type = SystemZII::CompareAndTrap; |
| 512 | break; |
| Ulrich Weigand | 2eb027d | 2016-04-07 16:11:44 +0000 | [diff] [blame] | 513 | default: |
| 514 | return false; |
| 515 | } |
| 516 | |
| 517 | // See whether we have a comparison that can be fused. |
| Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 518 | unsigned FusedOpcode = |
| 519 | TII->getFusedCompare(Compare.getOpcode(), Type, &Compare); |
| Ulrich Weigand | 2eb027d | 2016-04-07 16:11:44 +0000 | [diff] [blame] | 520 | if (!FusedOpcode) |
| Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 521 | return false; |
| 522 | |
| 523 | // Make sure that the operands are available at the branch. |
| Ulrich Weigand | a0e7325 | 2016-11-11 12:48:26 +0000 | [diff] [blame] | 524 | // SrcReg2 is the register if the source operand is a register, |
| 525 | // 0 if the source operand is immediate, and the base register |
| 526 | // if the source operand is memory (index is not supported). |
| Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 527 | unsigned SrcReg = Compare.getOperand(0).getReg(); |
| 528 | unsigned SrcReg2 = |
| 529 | Compare.getOperand(1).isReg() ? Compare.getOperand(1).getReg() : 0; |
| Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 530 | MachineBasicBlock::iterator MBBI = Compare, MBBE = Branch; |
| 531 | for (++MBBI; MBBI != MBBE; ++MBBI) |
| 532 | if (MBBI->modifiesRegister(SrcReg, TRI) || |
| 533 | (SrcReg2 && MBBI->modifiesRegister(SrcReg2, TRI))) |
| 534 | return false; |
| 535 | |
| Ulrich Weigand | 848a513 | 2016-04-11 12:12:32 +0000 | [diff] [blame] | 536 | // Read the branch mask, target (if applicable), regmask (if applicable). |
| Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 537 | MachineOperand CCMask(MBBI->getOperand(1)); |
| Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 538 | assert((CCMask.getImm() & ~SystemZ::CCMASK_ICMP) == 0 && |
| 539 | "Invalid condition-code mask for integer comparison"); |
| Ulrich Weigand | 2eb027d | 2016-04-07 16:11:44 +0000 | [diff] [blame] | 540 | // This is only valid for CompareAndBranch. |
| 541 | MachineOperand Target(MBBI->getOperand( |
| 542 | Type == SystemZII::CompareAndBranch ? 2 : 0)); |
| Ulrich Weigand | 848a513 | 2016-04-11 12:12:32 +0000 | [diff] [blame] | 543 | const uint32_t *RegMask; |
| 544 | if (Type == SystemZII::CompareAndSibcall) |
| 545 | RegMask = MBBI->getOperand(2).getRegMask(); |
| Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 546 | |
| 547 | // Clear out all current operands. |
| 548 | int CCUse = MBBI->findRegisterUseOperandIdx(SystemZ::CC, false, TRI); |
| Ulrich Weigand | 2eb027d | 2016-04-07 16:11:44 +0000 | [diff] [blame] | 549 | assert(CCUse >= 0 && "BRC/BCR must use CC"); |
| Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 550 | Branch->RemoveOperand(CCUse); |
| Ulrich Weigand | 848a513 | 2016-04-11 12:12:32 +0000 | [diff] [blame] | 551 | // Remove target (branch) or regmask (sibcall). |
| 552 | if (Type == SystemZII::CompareAndBranch || |
| 553 | Type == SystemZII::CompareAndSibcall) |
| Ulrich Weigand | 2eb027d | 2016-04-07 16:11:44 +0000 | [diff] [blame] | 554 | Branch->RemoveOperand(2); |
| Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 555 | Branch->RemoveOperand(1); |
| 556 | Branch->RemoveOperand(0); |
| 557 | |
| 558 | // Rebuild Branch as a fused compare and branch. |
| Ulrich Weigand | a0e7325 | 2016-11-11 12:48:26 +0000 | [diff] [blame] | 559 | // SrcNOps is the number of MI operands of the compare instruction |
| 560 | // that we need to copy over. |
| 561 | unsigned SrcNOps = 2; |
| 562 | if (FusedOpcode == SystemZ::CLT || FusedOpcode == SystemZ::CLGT) |
| 563 | SrcNOps = 3; |
| Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 564 | Branch->setDesc(TII->get(FusedOpcode)); |
| Ulrich Weigand | 2eb027d | 2016-04-07 16:11:44 +0000 | [diff] [blame] | 565 | MachineInstrBuilder MIB(*Branch->getParent()->getParent(), Branch); |
| Ulrich Weigand | a0e7325 | 2016-11-11 12:48:26 +0000 | [diff] [blame] | 566 | for (unsigned I = 0; I < SrcNOps; I++) |
| Diana Picus | 116bbab | 2017-01-13 09:58:52 +0000 | [diff] [blame] | 567 | MIB.add(Compare.getOperand(I)); |
| 568 | MIB.add(CCMask); |
| Ulrich Weigand | 2eb027d | 2016-04-07 16:11:44 +0000 | [diff] [blame] | 569 | |
| 570 | if (Type == SystemZII::CompareAndBranch) { |
| 571 | // Only conditional branches define CC, as they may be converted back |
| 572 | // to a non-fused branch because of a long displacement. Conditional |
| 573 | // returns don't have that problem. |
| Diana Picus | 116bbab | 2017-01-13 09:58:52 +0000 | [diff] [blame] | 574 | MIB.add(Target).addReg(SystemZ::CC, |
| 575 | RegState::ImplicitDefine | RegState::Dead); |
| Ulrich Weigand | 2eb027d | 2016-04-07 16:11:44 +0000 | [diff] [blame] | 576 | } |
| Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 577 | |
| Ulrich Weigand | 848a513 | 2016-04-11 12:12:32 +0000 | [diff] [blame] | 578 | if (Type == SystemZII::CompareAndSibcall) |
| 579 | MIB.addRegMask(RegMask); |
| 580 | |
| Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 581 | // Clear any intervening kills of SrcReg and SrcReg2. |
| 582 | MBBI = Compare; |
| 583 | for (++MBBI; MBBI != MBBE; ++MBBI) { |
| 584 | MBBI->clearRegisterKills(SrcReg, TRI); |
| 585 | if (SrcReg2) |
| 586 | MBBI->clearRegisterKills(SrcReg2, TRI); |
| 587 | } |
| 588 | FusedComparisons += 1; |
| 589 | return true; |
| 590 | } |
| 591 | |
| 592 | // Process all comparison instructions in MBB. Return true if something |
| 593 | // changed. |
| Richard Sandiford | 28c111e | 2014-03-06 11:00:15 +0000 | [diff] [blame] | 594 | bool SystemZElimCompare::processBlock(MachineBasicBlock &MBB) { |
| Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 595 | bool Changed = false; |
| 596 | |
| 597 | // Walk backwards through the block looking for comparisons, recording |
| 598 | // all CC users as we go. The subroutines can delete Compare and |
| 599 | // instructions before it. |
| 600 | bool CompleteCCUsers = !isCCLiveOut(MBB); |
| 601 | SmallVector<MachineInstr *, 4> CCUsers; |
| Richard Sandiford | 28c111e | 2014-03-06 11:00:15 +0000 | [diff] [blame] | 602 | MachineBasicBlock::iterator MBBI = MBB.end(); |
| 603 | while (MBBI != MBB.begin()) { |
| Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 604 | MachineInstr &MI = *--MBBI; |
| 605 | if (CompleteCCUsers && (MI.isCompare() || isLoadAndTestAsCmp(MI)) && |
| Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 606 | (optimizeCompareZero(MI, CCUsers) || |
| Zhan Jun Liau | ab42cbc | 2016-06-10 19:58:10 +0000 | [diff] [blame] | 607 | fuseCompareOperations(MI, CCUsers))) { |
| Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 608 | ++MBBI; |
| Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 609 | MI.eraseFromParent(); |
| Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 610 | Changed = true; |
| 611 | CCUsers.clear(); |
| Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 612 | continue; |
| 613 | } |
| 614 | |
| Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 615 | if (MI.definesRegister(SystemZ::CC)) { |
| Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 616 | CCUsers.clear(); |
| Jonas Paulsson | 9e1f3bd | 2015-10-08 07:39:55 +0000 | [diff] [blame] | 617 | CompleteCCUsers = true; |
| Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 618 | } |
| Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 619 | if (MI.readsRegister(SystemZ::CC) && CompleteCCUsers) |
| 620 | CCUsers.push_back(&MI); |
| Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 621 | } |
| 622 | return Changed; |
| 623 | } |
| 624 | |
| 625 | bool SystemZElimCompare::runOnMachineFunction(MachineFunction &F) { |
| Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 626 | if (skipFunction(F.getFunction())) |
| Andrew Kaylor | d9974cc | 2016-04-26 23:49:41 +0000 | [diff] [blame] | 627 | return false; |
| 628 | |
| Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 629 | TII = static_cast<const SystemZInstrInfo *>(F.getSubtarget().getInstrInfo()); |
| Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 630 | TRI = &TII->getRegisterInfo(); |
| 631 | |
| 632 | bool Changed = false; |
| Richard Sandiford | 28c111e | 2014-03-06 11:00:15 +0000 | [diff] [blame] | 633 | for (auto &MBB : F) |
| 634 | Changed |= processBlock(MBB); |
| Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 635 | |
| 636 | return Changed; |
| 637 | } |
| Eugene Zelenko | 3943d2b | 2017-01-24 22:10:43 +0000 | [diff] [blame] | 638 | |
| 639 | FunctionPass *llvm::createSystemZElimComparePass(SystemZTargetMachine &TM) { |
| 640 | return new SystemZElimCompare(TM); |
| 641 | } |