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 | |
Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 16 | #include "SystemZTargetMachine.h" |
| 17 | #include "llvm/ADT/Statistic.h" |
| 18 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 19 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 20 | #include "llvm/IR/Function.h" |
Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 21 | #include "llvm/Support/MathExtras.h" |
| 22 | #include "llvm/Target/TargetInstrInfo.h" |
| 23 | #include "llvm/Target/TargetMachine.h" |
| 24 | #include "llvm/Target/TargetRegisterInfo.h" |
| 25 | |
| 26 | using namespace llvm; |
| 27 | |
Chandler Carruth | 84e68b2 | 2014-04-22 02:41:26 +0000 | [diff] [blame] | 28 | #define DEBUG_TYPE "systemz-elim-compare" |
| 29 | |
Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 30 | STATISTIC(BranchOnCounts, "Number of branch-on-count instructions"); |
Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 31 | STATISTIC(EliminatedComparisons, "Number of eliminated comparisons"); |
| 32 | STATISTIC(FusedComparisons, "Number of fused compare-and-branch instructions"); |
| 33 | |
| 34 | namespace { |
Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 35 | // Represents the references to a particular register in one or more |
| 36 | // instructions. |
| 37 | struct Reference { |
| 38 | Reference() |
Jonas Paulsson | ee3685f | 2015-10-09 11:27:44 +0000 | [diff] [blame] | 39 | : Def(false), Use(false) {} |
Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 40 | |
Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 41 | Reference &operator|=(const Reference &Other) { |
| 42 | Def |= Other.Def; |
Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 43 | Use |= Other.Use; |
Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 44 | return *this; |
| 45 | } |
Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 46 | |
Aaron Ballman | b46962f | 2015-02-15 22:00:20 +0000 | [diff] [blame] | 47 | explicit operator bool() const { return Def || Use; } |
Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 48 | |
Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 49 | // True if the register is defined or used in some form, either directly or |
| 50 | // via a sub- or super-register. |
| 51 | bool Def; |
| 52 | bool Use; |
Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 53 | }; |
Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 54 | |
Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 55 | class SystemZElimCompare : public MachineFunctionPass { |
| 56 | public: |
| 57 | static char ID; |
| 58 | SystemZElimCompare(const SystemZTargetMachine &tm) |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 59 | : MachineFunctionPass(ID), TII(nullptr), TRI(nullptr) {} |
Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 60 | |
Richard Sandiford | b4d67b5 | 2014-03-06 12:03:36 +0000 | [diff] [blame] | 61 | const char *getPassName() const override { |
Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 62 | return "SystemZ Comparison Elimination"; |
| 63 | } |
Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 64 | |
Richard Sandiford | 28c111e | 2014-03-06 11:00:15 +0000 | [diff] [blame] | 65 | bool processBlock(MachineBasicBlock &MBB); |
Craig Topper | 9d74a5a | 2014-04-29 07:58:41 +0000 | [diff] [blame] | 66 | bool runOnMachineFunction(MachineFunction &F) override; |
Derek Schuff | 1dbf7a5 | 2016-04-04 17:09:25 +0000 | [diff] [blame] | 67 | MachineFunctionProperties getRequiredProperties() const override { |
| 68 | return MachineFunctionProperties().set( |
Matthias Braun | 1eb4736 | 2016-08-25 01:27:13 +0000 | [diff] [blame^] | 69 | MachineFunctionProperties::Property::NoVRegs); |
Derek Schuff | 1dbf7a5 | 2016-04-04 17:09:25 +0000 | [diff] [blame] | 70 | } |
Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 71 | |
Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 72 | private: |
Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 73 | Reference getRegReferences(MachineInstr &MI, unsigned Reg); |
| 74 | bool convertToBRCT(MachineInstr &MI, MachineInstr &Compare, |
Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 75 | SmallVectorImpl<MachineInstr *> &CCUsers); |
Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 76 | bool convertToLoadAndTest(MachineInstr &MI); |
| 77 | bool adjustCCMasksForInstr(MachineInstr &MI, MachineInstr &Compare, |
Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 78 | SmallVectorImpl<MachineInstr *> &CCUsers); |
Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 79 | bool optimizeCompareZero(MachineInstr &Compare, |
Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 80 | SmallVectorImpl<MachineInstr *> &CCUsers); |
Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 81 | bool fuseCompareOperations(MachineInstr &Compare, |
Zhan Jun Liau | ab42cbc | 2016-06-10 19:58:10 +0000 | [diff] [blame] | 82 | SmallVectorImpl<MachineInstr *> &CCUsers); |
Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 83 | |
Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 84 | const SystemZInstrInfo *TII; |
| 85 | const TargetRegisterInfo *TRI; |
| 86 | }; |
Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 87 | |
Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 88 | char SystemZElimCompare::ID = 0; |
| 89 | } // end anonymous namespace |
Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 90 | |
| 91 | FunctionPass *llvm::createSystemZElimComparePass(SystemZTargetMachine &TM) { |
| 92 | return new SystemZElimCompare(TM); |
| 93 | } |
| 94 | |
| 95 | // Return true if CC is live out of MBB. |
Richard Sandiford | 28c111e | 2014-03-06 11:00:15 +0000 | [diff] [blame] | 96 | static bool isCCLiveOut(MachineBasicBlock &MBB) { |
| 97 | 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] | 98 | if ((*SI)->isLiveIn(SystemZ::CC)) |
| 99 | return true; |
| 100 | return false; |
| 101 | } |
| 102 | |
Jonas Paulsson | 2c96dd6 | 2015-10-08 07:40:11 +0000 | [diff] [blame] | 103 | // Return true if any CC result of MI would reflect the value of Reg. |
Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 104 | static bool resultTests(MachineInstr &MI, unsigned Reg) { |
| 105 | if (MI.getNumOperands() > 0 && MI.getOperand(0).isReg() && |
| 106 | MI.getOperand(0).isDef() && MI.getOperand(0).getReg() == Reg) |
Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 107 | return true; |
| 108 | |
Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 109 | switch (MI.getOpcode()) { |
Richard Sandiford | b49a3ab | 2013-08-05 11:03:20 +0000 | [diff] [blame] | 110 | case SystemZ::LR: |
| 111 | case SystemZ::LGR: |
| 112 | case SystemZ::LGFR: |
| 113 | case SystemZ::LTR: |
| 114 | case SystemZ::LTGR: |
| 115 | case SystemZ::LTGFR: |
Richard Sandiford | 0897fce | 2013-08-07 11:10:06 +0000 | [diff] [blame] | 116 | case SystemZ::LER: |
| 117 | case SystemZ::LDR: |
| 118 | case SystemZ::LXR: |
| 119 | case SystemZ::LTEBR: |
| 120 | case SystemZ::LTDBR: |
| 121 | case SystemZ::LTXBR: |
Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 122 | if (MI.getOperand(1).getReg() == Reg) |
Richard Sandiford | b49a3ab | 2013-08-05 11:03:20 +0000 | [diff] [blame] | 123 | return true; |
| 124 | } |
| 125 | |
Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 126 | return false; |
| 127 | } |
| 128 | |
Jonas Paulsson | ee3685f | 2015-10-09 11:27:44 +0000 | [diff] [blame] | 129 | // 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] | 130 | Reference SystemZElimCompare::getRegReferences(MachineInstr &MI, unsigned Reg) { |
Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 131 | Reference Ref; |
Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 132 | for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) { |
| 133 | const MachineOperand &MO = MI.getOperand(I); |
Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 134 | if (MO.isReg()) { |
| 135 | if (unsigned MOReg = MO.getReg()) { |
Jonas Paulsson | ee3685f | 2015-10-09 11:27:44 +0000 | [diff] [blame] | 136 | if (TRI->regsOverlap(MOReg, Reg)) { |
| 137 | if (MO.isUse()) |
Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 138 | Ref.Use = true; |
Jonas Paulsson | ee3685f | 2015-10-09 11:27:44 +0000 | [diff] [blame] | 139 | else if (MO.isDef()) |
Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 140 | Ref.Def = true; |
Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | return Ref; |
| 146 | } |
| 147 | |
Jonas Paulsson | 5d3fbd3 | 2015-10-08 07:40:23 +0000 | [diff] [blame] | 148 | // Return true if this is a load and test which can be optimized the |
| 149 | // same way as compare instruction. |
Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 150 | static bool isLoadAndTestAsCmp(MachineInstr &MI) { |
Jonas Paulsson | 5d3fbd3 | 2015-10-08 07:40:23 +0000 | [diff] [blame] | 151 | // If we during isel used a load-and-test as a compare with 0, the |
| 152 | // def operand is dead. |
Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 153 | return (MI.getOpcode() == SystemZ::LTEBR || |
| 154 | MI.getOpcode() == SystemZ::LTDBR || |
| 155 | MI.getOpcode() == SystemZ::LTXBR) && |
| 156 | MI.getOperand(0).isDead(); |
Jonas Paulsson | 5d3fbd3 | 2015-10-08 07:40:23 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | // Return the source register of Compare, which is the unknown value |
| 160 | // being tested. |
Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 161 | static unsigned getCompareSourceReg(MachineInstr &Compare) { |
Jonas Paulsson | 5d3fbd3 | 2015-10-08 07:40:23 +0000 | [diff] [blame] | 162 | unsigned reg = 0; |
Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 163 | if (Compare.isCompare()) |
| 164 | reg = Compare.getOperand(0).getReg(); |
Jonas Paulsson | 5d3fbd3 | 2015-10-08 07:40:23 +0000 | [diff] [blame] | 165 | else if (isLoadAndTestAsCmp(Compare)) |
Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 166 | reg = Compare.getOperand(1).getReg(); |
Jonas Paulsson | 5d3fbd3 | 2015-10-08 07:40:23 +0000 | [diff] [blame] | 167 | assert (reg); |
| 168 | |
| 169 | return reg; |
| 170 | } |
| 171 | |
Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 172 | // Compare compares the result of MI against zero. If MI is an addition |
| 173 | // of -1 and if CCUsers is a single branch on nonzero, eliminate the addition |
| 174 | // and convert the branch to a BRCT(G). Return true on success. |
Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 175 | bool SystemZElimCompare::convertToBRCT( |
| 176 | MachineInstr &MI, MachineInstr &Compare, |
| 177 | SmallVectorImpl<MachineInstr *> &CCUsers) { |
Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 178 | // Check whether we have an addition of -1. |
Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 179 | unsigned Opcode = MI.getOpcode(); |
Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 180 | unsigned BRCT; |
| 181 | if (Opcode == SystemZ::AHI) |
| 182 | BRCT = SystemZ::BRCT; |
| 183 | else if (Opcode == SystemZ::AGHI) |
| 184 | BRCT = SystemZ::BRCTG; |
| 185 | else |
| 186 | return false; |
Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 187 | if (MI.getOperand(2).getImm() != -1) |
Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 188 | return false; |
| 189 | |
| 190 | // Check whether we have a single JLH. |
| 191 | if (CCUsers.size() != 1) |
| 192 | return false; |
| 193 | MachineInstr *Branch = CCUsers[0]; |
| 194 | if (Branch->getOpcode() != SystemZ::BRC || |
| 195 | Branch->getOperand(0).getImm() != SystemZ::CCMASK_ICMP || |
| 196 | Branch->getOperand(1).getImm() != SystemZ::CCMASK_CMP_NE) |
| 197 | return false; |
| 198 | |
| 199 | // We already know that there are no references to the register between |
| 200 | // MI and Compare. Make sure that there are also no references between |
| 201 | // Compare and Branch. |
Jonas Paulsson | 5d3fbd3 | 2015-10-08 07:40:23 +0000 | [diff] [blame] | 202 | unsigned SrcReg = getCompareSourceReg(Compare); |
Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 203 | MachineBasicBlock::iterator MBBI = Compare, MBBE = Branch; |
| 204 | for (++MBBI; MBBI != MBBE; ++MBBI) |
Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 205 | if (getRegReferences(*MBBI, SrcReg)) |
Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 206 | return false; |
| 207 | |
| 208 | // The transformation is OK. Rebuild Branch as a BRCT(G). |
| 209 | MachineOperand Target(Branch->getOperand(2)); |
Jonas Paulsson | 63a2b68 | 2015-10-10 07:14:24 +0000 | [diff] [blame] | 210 | while (Branch->getNumOperands()) |
| 211 | Branch->RemoveOperand(0); |
Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 212 | Branch->setDesc(TII->get(BRCT)); |
| 213 | MachineInstrBuilder(*Branch->getParent()->getParent(), Branch) |
Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 214 | .addOperand(MI.getOperand(0)) |
| 215 | .addOperand(MI.getOperand(1)) |
| 216 | .addOperand(Target) |
| 217 | .addReg(SystemZ::CC, RegState::ImplicitDefine | RegState::Dead); |
| 218 | MI.eraseFromParent(); |
Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 219 | return true; |
| 220 | } |
| 221 | |
Richard Sandiford | b49a3ab | 2013-08-05 11:03:20 +0000 | [diff] [blame] | 222 | // If MI is a load instruction, try to convert it into a LOAD AND TEST. |
| 223 | // Return true on success. |
Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 224 | bool SystemZElimCompare::convertToLoadAndTest(MachineInstr &MI) { |
| 225 | unsigned Opcode = TII->getLoadAndTest(MI.getOpcode()); |
Richard Sandiford | b49a3ab | 2013-08-05 11:03:20 +0000 | [diff] [blame] | 226 | if (!Opcode) |
| 227 | return false; |
| 228 | |
Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 229 | MI.setDesc(TII->get(Opcode)); |
| 230 | MachineInstrBuilder(*MI.getParent()->getParent(), MI) |
| 231 | .addReg(SystemZ::CC, RegState::ImplicitDefine); |
Richard Sandiford | b49a3ab | 2013-08-05 11:03:20 +0000 | [diff] [blame] | 232 | return true; |
| 233 | } |
| 234 | |
Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 235 | // The CC users in CCUsers are testing the result of a comparison of some |
| 236 | // value X against zero and we know that any CC value produced by MI |
| 237 | // would also reflect the value of X. Try to adjust CCUsers so that |
| 238 | // they test the result of MI directly, returning true on success. |
| 239 | // Leave everything unchanged on failure. |
Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 240 | bool SystemZElimCompare::adjustCCMasksForInstr( |
| 241 | MachineInstr &MI, MachineInstr &Compare, |
| 242 | SmallVectorImpl<MachineInstr *> &CCUsers) { |
| 243 | int Opcode = MI.getOpcode(); |
Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 244 | const MCInstrDesc &Desc = TII->get(Opcode); |
| 245 | unsigned MIFlags = Desc.TSFlags; |
| 246 | |
| 247 | // See which compare-style condition codes are available. |
Richard Sandiford | 0897fce | 2013-08-07 11:10:06 +0000 | [diff] [blame] | 248 | unsigned ReusableCCMask = SystemZII::getCompareZeroCCMask(MIFlags); |
Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 249 | |
| 250 | // For unsigned comparisons with zero, only equality makes sense. |
Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 251 | unsigned CompareFlags = Compare.getDesc().TSFlags; |
Richard Sandiford | 0897fce | 2013-08-07 11:10:06 +0000 | [diff] [blame] | 252 | if (CompareFlags & SystemZII::IsLogical) |
| 253 | ReusableCCMask &= SystemZ::CCMASK_CMP_EQ; |
Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 254 | |
| 255 | if (ReusableCCMask == 0) |
| 256 | return false; |
| 257 | |
| 258 | unsigned CCValues = SystemZII::getCCValues(MIFlags); |
| 259 | assert((ReusableCCMask & ~CCValues) == 0 && "Invalid CCValues"); |
| 260 | |
| 261 | // Now check whether these flags are enough for all users. |
| 262 | SmallVector<MachineOperand *, 4> AlterMasks; |
| 263 | for (unsigned int I = 0, E = CCUsers.size(); I != E; ++I) { |
| 264 | MachineInstr *MI = CCUsers[I]; |
| 265 | |
| 266 | // Fail if this isn't a use of CC that we understand. |
| 267 | unsigned Flags = MI->getDesc().TSFlags; |
| 268 | unsigned FirstOpNum; |
| 269 | if (Flags & SystemZII::CCMaskFirst) |
| 270 | FirstOpNum = 0; |
| 271 | else if (Flags & SystemZII::CCMaskLast) |
| 272 | FirstOpNum = MI->getNumExplicitOperands() - 2; |
| 273 | else |
| 274 | return false; |
| 275 | |
| 276 | // Check whether the instruction predicate treats all CC values |
| 277 | // outside of ReusableCCMask in the same way. In that case it |
| 278 | // doesn't matter what those CC values mean. |
| 279 | unsigned CCValid = MI->getOperand(FirstOpNum).getImm(); |
| 280 | unsigned CCMask = MI->getOperand(FirstOpNum + 1).getImm(); |
| 281 | unsigned OutValid = ~ReusableCCMask & CCValid; |
| 282 | unsigned OutMask = ~ReusableCCMask & CCMask; |
| 283 | if (OutMask != 0 && OutMask != OutValid) |
| 284 | return false; |
| 285 | |
| 286 | AlterMasks.push_back(&MI->getOperand(FirstOpNum)); |
| 287 | AlterMasks.push_back(&MI->getOperand(FirstOpNum + 1)); |
| 288 | } |
| 289 | |
| 290 | // All users are OK. Adjust the masks for MI. |
| 291 | for (unsigned I = 0, E = AlterMasks.size(); I != E; I += 2) { |
| 292 | AlterMasks[I]->setImm(CCValues); |
| 293 | unsigned CCMask = AlterMasks[I + 1]->getImm(); |
| 294 | if (CCMask & ~ReusableCCMask) |
| 295 | AlterMasks[I + 1]->setImm((CCMask & ReusableCCMask) | |
| 296 | (CCValues & ~ReusableCCMask)); |
| 297 | } |
| 298 | |
| 299 | // CC is now live after MI. |
Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 300 | int CCDef = MI.findRegisterDefOperandIdx(SystemZ::CC, false, true, TRI); |
Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 301 | assert(CCDef >= 0 && "Couldn't find CC set"); |
Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 302 | MI.getOperand(CCDef).setIsDead(false); |
Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 303 | |
| 304 | // Clear any intervening kills of CC. |
| 305 | MachineBasicBlock::iterator MBBI = MI, MBBE = Compare; |
| 306 | for (++MBBI; MBBI != MBBE; ++MBBI) |
| 307 | MBBI->clearRegisterKills(SystemZ::CC, TRI); |
| 308 | |
| 309 | return true; |
| 310 | } |
| 311 | |
Richard Sandiford | 0897fce | 2013-08-07 11:10:06 +0000 | [diff] [blame] | 312 | // Return true if Compare is a comparison against zero. |
Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 313 | static bool isCompareZero(MachineInstr &Compare) { |
| 314 | switch (Compare.getOpcode()) { |
Richard Sandiford | 0897fce | 2013-08-07 11:10:06 +0000 | [diff] [blame] | 315 | case SystemZ::LTEBRCompare: |
| 316 | case SystemZ::LTDBRCompare: |
| 317 | case SystemZ::LTXBRCompare: |
| 318 | return true; |
| 319 | |
| 320 | default: |
Jonas Paulsson | 5d3fbd3 | 2015-10-08 07:40:23 +0000 | [diff] [blame] | 321 | |
| 322 | if (isLoadAndTestAsCmp(Compare)) |
| 323 | return true; |
| 324 | |
Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 325 | return Compare.getNumExplicitOperands() == 2 && |
| 326 | Compare.getOperand(1).isImm() && Compare.getOperand(1).getImm() == 0; |
Richard Sandiford | 0897fce | 2013-08-07 11:10:06 +0000 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | |
Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 330 | // Try to optimize cases where comparison instruction Compare is testing |
| 331 | // a value against zero. Return true on success and if Compare should be |
| 332 | // deleted as dead. CCUsers is the list of instructions that use the CC |
| 333 | // value produced by Compare. |
Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 334 | bool SystemZElimCompare::optimizeCompareZero( |
| 335 | MachineInstr &Compare, SmallVectorImpl<MachineInstr *> &CCUsers) { |
Richard Sandiford | 0897fce | 2013-08-07 11:10:06 +0000 | [diff] [blame] | 336 | if (!isCompareZero(Compare)) |
Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 337 | return false; |
| 338 | |
| 339 | // Search back for CC results that are based on the first operand. |
Jonas Paulsson | 5d3fbd3 | 2015-10-08 07:40:23 +0000 | [diff] [blame] | 340 | unsigned SrcReg = getCompareSourceReg(Compare); |
Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 341 | MachineBasicBlock &MBB = *Compare.getParent(); |
Richard Sandiford | 28c111e | 2014-03-06 11:00:15 +0000 | [diff] [blame] | 342 | MachineBasicBlock::iterator MBBI = Compare, MBBE = MBB.begin(); |
Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 343 | Reference CCRefs; |
| 344 | Reference SrcRefs; |
Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 345 | while (MBBI != MBBE) { |
| 346 | --MBBI; |
Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 347 | MachineInstr &MI = *MBBI; |
Jonas Paulsson | 2c96dd6 | 2015-10-08 07:40:11 +0000 | [diff] [blame] | 348 | if (resultTests(MI, SrcReg)) { |
Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 349 | // Try to remove both MI and Compare by converting a branch to BRCT(G). |
| 350 | // We don't care in this case whether CC is modified between MI and |
| 351 | // Compare. |
| 352 | if (!CCRefs.Use && !SrcRefs && convertToBRCT(MI, Compare, CCUsers)) { |
| 353 | BranchOnCounts += 1; |
| 354 | return true; |
| 355 | } |
| 356 | // Try to eliminate Compare by reusing a CC result from MI. |
| 357 | if ((!CCRefs && convertToLoadAndTest(MI)) || |
| 358 | (!CCRefs.Def && adjustCCMasksForInstr(MI, Compare, CCUsers))) { |
| 359 | EliminatedComparisons += 1; |
| 360 | return true; |
| 361 | } |
Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 362 | } |
Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 363 | SrcRefs |= getRegReferences(MI, SrcReg); |
| 364 | if (SrcRefs.Def) |
Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 365 | return false; |
Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 366 | CCRefs |= getRegReferences(MI, SystemZ::CC); |
| 367 | if (CCRefs.Use && CCRefs.Def) |
| 368 | return false; |
Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 369 | } |
| 370 | return false; |
| 371 | } |
| 372 | |
| 373 | // Try to fuse comparison instruction Compare into a later branch. |
| 374 | // 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] | 375 | bool SystemZElimCompare::fuseCompareOperations( |
| 376 | MachineInstr &Compare, SmallVectorImpl<MachineInstr *> &CCUsers) { |
Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 377 | // See whether we have a single branch with which to fuse. |
| 378 | if (CCUsers.size() != 1) |
| 379 | return false; |
| 380 | MachineInstr *Branch = CCUsers[0]; |
Zhan Jun Liau | ab42cbc | 2016-06-10 19:58:10 +0000 | [diff] [blame] | 381 | SystemZII::FusedCompareType Type; |
Ulrich Weigand | 2eb027d | 2016-04-07 16:11:44 +0000 | [diff] [blame] | 382 | switch (Branch->getOpcode()) { |
| 383 | case SystemZ::BRC: |
| 384 | Type = SystemZII::CompareAndBranch; |
| 385 | break; |
| 386 | case SystemZ::CondReturn: |
| 387 | Type = SystemZII::CompareAndReturn; |
| 388 | break; |
Ulrich Weigand | 848a513 | 2016-04-11 12:12:32 +0000 | [diff] [blame] | 389 | case SystemZ::CallBCR: |
| 390 | Type = SystemZII::CompareAndSibcall; |
| 391 | break; |
Zhan Jun Liau | ab42cbc | 2016-06-10 19:58:10 +0000 | [diff] [blame] | 392 | case SystemZ::CondTrap: |
| 393 | Type = SystemZII::CompareAndTrap; |
| 394 | break; |
Ulrich Weigand | 2eb027d | 2016-04-07 16:11:44 +0000 | [diff] [blame] | 395 | default: |
| 396 | return false; |
| 397 | } |
| 398 | |
| 399 | // 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] | 400 | unsigned FusedOpcode = |
| 401 | TII->getFusedCompare(Compare.getOpcode(), Type, &Compare); |
Ulrich Weigand | 2eb027d | 2016-04-07 16:11:44 +0000 | [diff] [blame] | 402 | if (!FusedOpcode) |
Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 403 | return false; |
| 404 | |
| 405 | // Make sure that the operands are available at the branch. |
Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 406 | unsigned SrcReg = Compare.getOperand(0).getReg(); |
| 407 | unsigned SrcReg2 = |
| 408 | Compare.getOperand(1).isReg() ? Compare.getOperand(1).getReg() : 0; |
Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 409 | MachineBasicBlock::iterator MBBI = Compare, MBBE = Branch; |
| 410 | for (++MBBI; MBBI != MBBE; ++MBBI) |
| 411 | if (MBBI->modifiesRegister(SrcReg, TRI) || |
| 412 | (SrcReg2 && MBBI->modifiesRegister(SrcReg2, TRI))) |
| 413 | return false; |
| 414 | |
Ulrich Weigand | 848a513 | 2016-04-11 12:12:32 +0000 | [diff] [blame] | 415 | // Read the branch mask, target (if applicable), regmask (if applicable). |
Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 416 | MachineOperand CCMask(MBBI->getOperand(1)); |
Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 417 | assert((CCMask.getImm() & ~SystemZ::CCMASK_ICMP) == 0 && |
| 418 | "Invalid condition-code mask for integer comparison"); |
Ulrich Weigand | 2eb027d | 2016-04-07 16:11:44 +0000 | [diff] [blame] | 419 | // This is only valid for CompareAndBranch. |
| 420 | MachineOperand Target(MBBI->getOperand( |
| 421 | Type == SystemZII::CompareAndBranch ? 2 : 0)); |
Ulrich Weigand | 848a513 | 2016-04-11 12:12:32 +0000 | [diff] [blame] | 422 | const uint32_t *RegMask; |
| 423 | if (Type == SystemZII::CompareAndSibcall) |
| 424 | RegMask = MBBI->getOperand(2).getRegMask(); |
Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 425 | |
| 426 | // Clear out all current operands. |
| 427 | int CCUse = MBBI->findRegisterUseOperandIdx(SystemZ::CC, false, TRI); |
Ulrich Weigand | 2eb027d | 2016-04-07 16:11:44 +0000 | [diff] [blame] | 428 | assert(CCUse >= 0 && "BRC/BCR must use CC"); |
Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 429 | Branch->RemoveOperand(CCUse); |
Ulrich Weigand | 848a513 | 2016-04-11 12:12:32 +0000 | [diff] [blame] | 430 | // Remove target (branch) or regmask (sibcall). |
| 431 | if (Type == SystemZII::CompareAndBranch || |
| 432 | Type == SystemZII::CompareAndSibcall) |
Ulrich Weigand | 2eb027d | 2016-04-07 16:11:44 +0000 | [diff] [blame] | 433 | Branch->RemoveOperand(2); |
Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 434 | Branch->RemoveOperand(1); |
| 435 | Branch->RemoveOperand(0); |
| 436 | |
| 437 | // Rebuild Branch as a fused compare and branch. |
| 438 | Branch->setDesc(TII->get(FusedOpcode)); |
Ulrich Weigand | 2eb027d | 2016-04-07 16:11:44 +0000 | [diff] [blame] | 439 | MachineInstrBuilder MIB(*Branch->getParent()->getParent(), Branch); |
Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 440 | MIB.addOperand(Compare.getOperand(0)) |
| 441 | .addOperand(Compare.getOperand(1)) |
| 442 | .addOperand(CCMask); |
Ulrich Weigand | 2eb027d | 2016-04-07 16:11:44 +0000 | [diff] [blame] | 443 | |
| 444 | if (Type == SystemZII::CompareAndBranch) { |
| 445 | // Only conditional branches define CC, as they may be converted back |
| 446 | // to a non-fused branch because of a long displacement. Conditional |
| 447 | // returns don't have that problem. |
| 448 | MIB.addOperand(Target) |
Jonas Paulsson | 9028acf | 2016-05-02 09:37:40 +0000 | [diff] [blame] | 449 | .addReg(SystemZ::CC, RegState::ImplicitDefine | RegState::Dead); |
Ulrich Weigand | 2eb027d | 2016-04-07 16:11:44 +0000 | [diff] [blame] | 450 | } |
Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 451 | |
Ulrich Weigand | 848a513 | 2016-04-11 12:12:32 +0000 | [diff] [blame] | 452 | if (Type == SystemZII::CompareAndSibcall) |
| 453 | MIB.addRegMask(RegMask); |
| 454 | |
Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 455 | // Clear any intervening kills of SrcReg and SrcReg2. |
| 456 | MBBI = Compare; |
| 457 | for (++MBBI; MBBI != MBBE; ++MBBI) { |
| 458 | MBBI->clearRegisterKills(SrcReg, TRI); |
| 459 | if (SrcReg2) |
| 460 | MBBI->clearRegisterKills(SrcReg2, TRI); |
| 461 | } |
| 462 | FusedComparisons += 1; |
| 463 | return true; |
| 464 | } |
| 465 | |
| 466 | // Process all comparison instructions in MBB. Return true if something |
| 467 | // changed. |
Richard Sandiford | 28c111e | 2014-03-06 11:00:15 +0000 | [diff] [blame] | 468 | bool SystemZElimCompare::processBlock(MachineBasicBlock &MBB) { |
Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 469 | bool Changed = false; |
| 470 | |
| 471 | // Walk backwards through the block looking for comparisons, recording |
| 472 | // all CC users as we go. The subroutines can delete Compare and |
| 473 | // instructions before it. |
| 474 | bool CompleteCCUsers = !isCCLiveOut(MBB); |
| 475 | SmallVector<MachineInstr *, 4> CCUsers; |
Richard Sandiford | 28c111e | 2014-03-06 11:00:15 +0000 | [diff] [blame] | 476 | MachineBasicBlock::iterator MBBI = MBB.end(); |
| 477 | while (MBBI != MBB.begin()) { |
Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 478 | MachineInstr &MI = *--MBBI; |
| 479 | if (CompleteCCUsers && (MI.isCompare() || isLoadAndTestAsCmp(MI)) && |
Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 480 | (optimizeCompareZero(MI, CCUsers) || |
Zhan Jun Liau | ab42cbc | 2016-06-10 19:58:10 +0000 | [diff] [blame] | 481 | fuseCompareOperations(MI, CCUsers))) { |
Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 482 | ++MBBI; |
Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 483 | MI.eraseFromParent(); |
Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 484 | Changed = true; |
| 485 | CCUsers.clear(); |
Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 486 | continue; |
| 487 | } |
| 488 | |
Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 489 | if (MI.definesRegister(SystemZ::CC)) { |
Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 490 | CCUsers.clear(); |
Jonas Paulsson | 9e1f3bd | 2015-10-08 07:39:55 +0000 | [diff] [blame] | 491 | CompleteCCUsers = true; |
Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 492 | } |
Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 493 | if (MI.readsRegister(SystemZ::CC) && CompleteCCUsers) |
| 494 | CCUsers.push_back(&MI); |
Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 495 | } |
| 496 | return Changed; |
| 497 | } |
| 498 | |
| 499 | bool SystemZElimCompare::runOnMachineFunction(MachineFunction &F) { |
Andrew Kaylor | d9974cc | 2016-04-26 23:49:41 +0000 | [diff] [blame] | 500 | if (skipFunction(*F.getFunction())) |
| 501 | return false; |
| 502 | |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 503 | TII = static_cast<const SystemZInstrInfo *>(F.getSubtarget().getInstrInfo()); |
Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 504 | TRI = &TII->getRegisterInfo(); |
| 505 | |
| 506 | bool Changed = false; |
Richard Sandiford | 28c111e | 2014-03-06 11:00:15 +0000 | [diff] [blame] | 507 | for (auto &MBB : F) |
| 508 | Changed |= processBlock(MBB); |
Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 509 | |
| 510 | return Changed; |
| 511 | } |