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