Sirish Pande | 4bd20c5 | 2012-05-12 05:10:30 +0000 | [diff] [blame] | 1 | //===----- HexagonNewValueJump.cpp - Hexagon Backend New Value Jump -------===// |
| 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 implements NewValueJump pass in Hexagon. |
| 11 | // Ideally, we should merge this as a Peephole pass prior to register |
Benjamin Kramer | bde9176 | 2012-06-02 10:20:22 +0000 | [diff] [blame] | 12 | // allocation, but because we have a spill in between the feeder and new value |
Sirish Pande | 4bd20c5 | 2012-05-12 05:10:30 +0000 | [diff] [blame] | 13 | // jump instructions, we are forced to write after register allocation. |
Benjamin Kramer | bde9176 | 2012-06-02 10:20:22 +0000 | [diff] [blame] | 14 | // Having said that, we should re-attempt to pull this earlier at some point |
Sirish Pande | 4bd20c5 | 2012-05-12 05:10:30 +0000 | [diff] [blame] | 15 | // in future. |
| 16 | |
| 17 | // The basic approach looks for sequence of predicated jump, compare instruciton |
| 18 | // that genereates the predicate and, the feeder to the predicate. Once it finds |
| 19 | // all, it collapses compare and jump instruction into a new valu jump |
| 20 | // intstructions. |
| 21 | // |
| 22 | // |
| 23 | //===----------------------------------------------------------------------===// |
Jyotsna Verma | a841af7 | 2013-05-02 22:10:59 +0000 | [diff] [blame] | 24 | #include "llvm/PassSupport.h" |
Jyotsna Verma | 84c4710 | 2013-05-06 18:49:23 +0000 | [diff] [blame] | 25 | #include "Hexagon.h" |
Jyotsna Verma | 84c4710 | 2013-05-06 18:49:23 +0000 | [diff] [blame] | 26 | #include "HexagonInstrInfo.h" |
| 27 | #include "HexagonMachineFunctionInfo.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 28 | #include "HexagonRegisterInfo.h" |
| 29 | #include "HexagonSubtarget.h" |
| 30 | #include "HexagonTargetMachine.h" |
| 31 | #include "llvm/ADT/DenseMap.h" |
| 32 | #include "llvm/ADT/Statistic.h" |
| 33 | #include "llvm/CodeGen/LiveVariables.h" |
| 34 | #include "llvm/CodeGen/MachineFunctionAnalysis.h" |
| 35 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 36 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 37 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 38 | #include "llvm/CodeGen/Passes.h" |
| 39 | #include "llvm/CodeGen/ScheduleDAGInstrs.h" |
Jyotsna Verma | 84c4710 | 2013-05-06 18:49:23 +0000 | [diff] [blame] | 40 | #include "llvm/Support/CommandLine.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 41 | #include "llvm/Support/Compiler.h" |
| 42 | #include "llvm/Support/Debug.h" |
Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 43 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 44 | #include "llvm/Target/TargetInstrInfo.h" |
| 45 | #include "llvm/Target/TargetMachine.h" |
| 46 | #include "llvm/Target/TargetRegisterInfo.h" |
| 47 | #include <map> |
Sirish Pande | 4bd20c5 | 2012-05-12 05:10:30 +0000 | [diff] [blame] | 48 | using namespace llvm; |
| 49 | |
Chandler Carruth | 84e68b2 | 2014-04-22 02:41:26 +0000 | [diff] [blame] | 50 | #define DEBUG_TYPE "hexagon-nvj" |
| 51 | |
Sirish Pande | 4bd20c5 | 2012-05-12 05:10:30 +0000 | [diff] [blame] | 52 | STATISTIC(NumNVJGenerated, "Number of New Value Jump Instructions created"); |
| 53 | |
Sirish Pande | 4bd20c5 | 2012-05-12 05:10:30 +0000 | [diff] [blame] | 54 | static cl::opt<int> |
| 55 | DbgNVJCount("nvj-count", cl::init(-1), cl::Hidden, cl::desc( |
| 56 | "Maximum number of predicated jumps to be converted to New Value Jump")); |
| 57 | |
| 58 | static cl::opt<bool> DisableNewValueJumps("disable-nvjump", cl::Hidden, |
| 59 | cl::ZeroOrMore, cl::init(false), |
| 60 | cl::desc("Disable New Value Jumps")); |
| 61 | |
Jyotsna Verma | 84c4710 | 2013-05-06 18:49:23 +0000 | [diff] [blame] | 62 | namespace llvm { |
Colin LeMahieu | 56efafc | 2015-06-15 19:05:35 +0000 | [diff] [blame] | 63 | FunctionPass *createHexagonNewValueJump(); |
Jyotsna Verma | 84c4710 | 2013-05-06 18:49:23 +0000 | [diff] [blame] | 64 | void initializeHexagonNewValueJumpPass(PassRegistry&); |
| 65 | } |
| 66 | |
| 67 | |
Sirish Pande | 4bd20c5 | 2012-05-12 05:10:30 +0000 | [diff] [blame] | 68 | namespace { |
| 69 | struct HexagonNewValueJump : public MachineFunctionPass { |
| 70 | const HexagonInstrInfo *QII; |
| 71 | const HexagonRegisterInfo *QRI; |
| 72 | |
| 73 | public: |
| 74 | static char ID; |
| 75 | |
Jyotsna Verma | 84c4710 | 2013-05-06 18:49:23 +0000 | [diff] [blame] | 76 | HexagonNewValueJump() : MachineFunctionPass(ID) { |
| 77 | initializeHexagonNewValueJumpPass(*PassRegistry::getPassRegistry()); |
| 78 | } |
Sirish Pande | 4bd20c5 | 2012-05-12 05:10:30 +0000 | [diff] [blame] | 79 | |
Craig Topper | 906c2cd | 2014-04-29 07:58:16 +0000 | [diff] [blame] | 80 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Jyotsna Verma | 1d29750 | 2013-05-02 15:39:30 +0000 | [diff] [blame] | 81 | AU.addRequired<MachineBranchProbabilityInfo>(); |
Sirish Pande | 4bd20c5 | 2012-05-12 05:10:30 +0000 | [diff] [blame] | 82 | MachineFunctionPass::getAnalysisUsage(AU); |
| 83 | } |
| 84 | |
Craig Topper | 906c2cd | 2014-04-29 07:58:16 +0000 | [diff] [blame] | 85 | const char *getPassName() const override { |
Sirish Pande | 4bd20c5 | 2012-05-12 05:10:30 +0000 | [diff] [blame] | 86 | return "Hexagon NewValueJump"; |
| 87 | } |
| 88 | |
Craig Topper | 906c2cd | 2014-04-29 07:58:16 +0000 | [diff] [blame] | 89 | bool runOnMachineFunction(MachineFunction &Fn) override; |
Sirish Pande | 4bd20c5 | 2012-05-12 05:10:30 +0000 | [diff] [blame] | 90 | |
| 91 | private: |
Jyotsna Verma | 1d29750 | 2013-05-02 15:39:30 +0000 | [diff] [blame] | 92 | /// \brief A handle to the branch probability pass. |
| 93 | const MachineBranchProbabilityInfo *MBPI; |
Sirish Pande | 4bd20c5 | 2012-05-12 05:10:30 +0000 | [diff] [blame] | 94 | |
| 95 | }; |
| 96 | |
| 97 | } // end of anonymous namespace |
| 98 | |
| 99 | char HexagonNewValueJump::ID = 0; |
| 100 | |
Jyotsna Verma | 84c4710 | 2013-05-06 18:49:23 +0000 | [diff] [blame] | 101 | INITIALIZE_PASS_BEGIN(HexagonNewValueJump, "hexagon-nvj", |
| 102 | "Hexagon NewValueJump", false, false) |
| 103 | INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo) |
| 104 | INITIALIZE_PASS_END(HexagonNewValueJump, "hexagon-nvj", |
| 105 | "Hexagon NewValueJump", false, false) |
| 106 | |
| 107 | |
Sirish Pande | 4bd20c5 | 2012-05-12 05:10:30 +0000 | [diff] [blame] | 108 | // We have identified this II could be feeder to NVJ, |
| 109 | // verify that it can be. |
| 110 | static bool canBeFeederToNewValueJump(const HexagonInstrInfo *QII, |
| 111 | const TargetRegisterInfo *TRI, |
| 112 | MachineBasicBlock::iterator II, |
| 113 | MachineBasicBlock::iterator end, |
| 114 | MachineBasicBlock::iterator skip, |
| 115 | MachineFunction &MF) { |
| 116 | |
| 117 | // Predicated instruction can not be feeder to NVJ. |
| 118 | if (QII->isPredicated(II)) |
| 119 | return false; |
| 120 | |
| 121 | // Bail out if feederReg is a paired register (double regs in |
| 122 | // our case). One would think that we can check to see if a given |
| 123 | // register cmpReg1 or cmpReg2 is a sub register of feederReg |
| 124 | // using -- if (QRI->isSubRegister(feederReg, cmpReg1) logic |
| 125 | // before the callsite of this function |
| 126 | // But we can not as it comes in the following fashion. |
| 127 | // %D0<def> = Hexagon_S2_lsr_r_p %D0<kill>, %R2<kill> |
| 128 | // %R0<def> = KILL %R0, %D0<imp-use,kill> |
| 129 | // %P0<def> = CMPEQri %R0<kill>, 0 |
| 130 | // Hence, we need to check if it's a KILL instruction. |
| 131 | if (II->getOpcode() == TargetOpcode::KILL) |
| 132 | return false; |
| 133 | |
| 134 | |
| 135 | // Make sure there there is no 'def' or 'use' of any of the uses of |
| 136 | // feeder insn between it's definition, this MI and jump, jmpInst |
| 137 | // skipping compare, cmpInst. |
| 138 | // Here's the example. |
| 139 | // r21=memub(r22+r24<<#0) |
| 140 | // p0 = cmp.eq(r21, #0) |
| 141 | // r4=memub(r3+r21<<#0) |
| 142 | // if (p0.new) jump:t .LBB29_45 |
| 143 | // Without this check, it will be converted into |
| 144 | // r4=memub(r3+r21<<#0) |
| 145 | // r21=memub(r22+r24<<#0) |
| 146 | // p0 = cmp.eq(r21, #0) |
| 147 | // if (p0.new) jump:t .LBB29_45 |
| 148 | // and result WAR hazards if converted to New Value Jump. |
| 149 | |
| 150 | for (unsigned i = 0; i < II->getNumOperands(); ++i) { |
| 151 | if (II->getOperand(i).isReg() && |
| 152 | (II->getOperand(i).isUse() || II->getOperand(i).isDef())) { |
| 153 | MachineBasicBlock::iterator localII = II; |
| 154 | ++localII; |
| 155 | unsigned Reg = II->getOperand(i).getReg(); |
| 156 | for (MachineBasicBlock::iterator localBegin = localII; |
| 157 | localBegin != end; ++localBegin) { |
| 158 | if (localBegin == skip ) continue; |
| 159 | // Check for Subregisters too. |
| 160 | if (localBegin->modifiesRegister(Reg, TRI) || |
| 161 | localBegin->readsRegister(Reg, TRI)) |
| 162 | return false; |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | return true; |
| 167 | } |
| 168 | |
| 169 | // These are the common checks that need to performed |
| 170 | // to determine if |
| 171 | // 1. compare instruction can be moved before jump. |
| 172 | // 2. feeder to the compare instruction can be moved before jump. |
| 173 | static bool commonChecksToProhibitNewValueJump(bool afterRA, |
| 174 | MachineBasicBlock::iterator MII) { |
| 175 | |
| 176 | // If store in path, bail out. |
| 177 | if (MII->getDesc().mayStore()) |
| 178 | return false; |
| 179 | |
| 180 | // if call in path, bail out. |
Colin LeMahieu | 984ef17 | 2014-12-12 21:12:27 +0000 | [diff] [blame] | 181 | if (MII->getOpcode() == Hexagon::J2_call) |
Sirish Pande | 4bd20c5 | 2012-05-12 05:10:30 +0000 | [diff] [blame] | 182 | return false; |
| 183 | |
| 184 | // if NVJ is running prior to RA, do the following checks. |
| 185 | if (!afterRA) { |
| 186 | // The following Target Opcode instructions are spurious |
| 187 | // to new value jump. If they are in the path, bail out. |
| 188 | // KILL sets kill flag on the opcode. It also sets up a |
| 189 | // single register, out of pair. |
| 190 | // %D0<def> = Hexagon_S2_lsr_r_p %D0<kill>, %R2<kill> |
| 191 | // %R0<def> = KILL %R0, %D0<imp-use,kill> |
| 192 | // %P0<def> = CMPEQri %R0<kill>, 0 |
| 193 | // PHI can be anything after RA. |
| 194 | // COPY can remateriaze things in between feeder, compare and nvj. |
| 195 | if (MII->getOpcode() == TargetOpcode::KILL || |
| 196 | MII->getOpcode() == TargetOpcode::PHI || |
| 197 | MII->getOpcode() == TargetOpcode::COPY) |
| 198 | return false; |
| 199 | |
| 200 | // The following pseudo Hexagon instructions sets "use" and "def" |
| 201 | // of registers by individual passes in the backend. At this time, |
| 202 | // we don't know the scope of usage and definitions of these |
| 203 | // instructions. |
Colin LeMahieu | 96bfaa9 | 2015-03-09 19:57:18 +0000 | [diff] [blame] | 204 | if (MII->getOpcode() == Hexagon::LDriw_pred || |
Sirish Pande | 4bd20c5 | 2012-05-12 05:10:30 +0000 | [diff] [blame] | 205 | MII->getOpcode() == Hexagon::STriw_pred) |
| 206 | return false; |
| 207 | } |
| 208 | |
| 209 | return true; |
| 210 | } |
| 211 | |
| 212 | static bool canCompareBeNewValueJump(const HexagonInstrInfo *QII, |
| 213 | const TargetRegisterInfo *TRI, |
| 214 | MachineBasicBlock::iterator II, |
| 215 | unsigned pReg, |
| 216 | bool secondReg, |
| 217 | bool optLocation, |
| 218 | MachineBasicBlock::iterator end, |
| 219 | MachineFunction &MF) { |
| 220 | |
| 221 | MachineInstr *MI = II; |
| 222 | |
| 223 | // If the second operand of the compare is an imm, make sure it's in the |
| 224 | // range specified by the arch. |
| 225 | if (!secondReg) { |
| 226 | int64_t v = MI->getOperand(2).getImm(); |
Sirish Pande | 4bd20c5 | 2012-05-12 05:10:30 +0000 | [diff] [blame] | 227 | |
| 228 | if (!(isUInt<5>(v) || |
Colin LeMahieu | 6e0f9f8 | 2014-11-26 19:43:12 +0000 | [diff] [blame] | 229 | ((MI->getOpcode() == Hexagon::C2_cmpeqi || |
| 230 | MI->getOpcode() == Hexagon::C2_cmpgti) && |
Sirish Pande | 4bd20c5 | 2012-05-12 05:10:30 +0000 | [diff] [blame] | 231 | (v == -1)))) |
| 232 | return false; |
| 233 | } |
| 234 | |
Jyotsna Verma | 84c4710 | 2013-05-06 18:49:23 +0000 | [diff] [blame] | 235 | unsigned cmpReg1, cmpOp2 = 0; // cmpOp2 assignment silences compiler warning. |
Sirish Pande | 4bd20c5 | 2012-05-12 05:10:30 +0000 | [diff] [blame] | 236 | cmpReg1 = MI->getOperand(1).getReg(); |
| 237 | |
| 238 | if (secondReg) { |
| 239 | cmpOp2 = MI->getOperand(2).getReg(); |
| 240 | |
| 241 | // Make sure that that second register is not from COPY |
| 242 | // At machine code level, we don't need this, but if we decide |
| 243 | // to move new value jump prior to RA, we would be needing this. |
| 244 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 245 | if (secondReg && !TargetRegisterInfo::isPhysicalRegister(cmpOp2)) { |
| 246 | MachineInstr *def = MRI.getVRegDef(cmpOp2); |
| 247 | if (def->getOpcode() == TargetOpcode::COPY) |
| 248 | return false; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | // Walk the instructions after the compare (predicate def) to the jump, |
| 253 | // and satisfy the following conditions. |
| 254 | ++II ; |
| 255 | for (MachineBasicBlock::iterator localII = II; localII != end; |
| 256 | ++localII) { |
| 257 | |
| 258 | // Check 1. |
| 259 | // If "common" checks fail, bail out. |
| 260 | if (!commonChecksToProhibitNewValueJump(optLocation, localII)) |
| 261 | return false; |
| 262 | |
| 263 | // Check 2. |
| 264 | // If there is a def or use of predicate (result of compare), bail out. |
| 265 | if (localII->modifiesRegister(pReg, TRI) || |
| 266 | localII->readsRegister(pReg, TRI)) |
| 267 | return false; |
| 268 | |
| 269 | // Check 3. |
| 270 | // If there is a def of any of the use of the compare (operands of compare), |
| 271 | // bail out. |
| 272 | // Eg. |
| 273 | // p0 = cmp.eq(r2, r0) |
| 274 | // r2 = r4 |
| 275 | // if (p0.new) jump:t .LBB28_3 |
| 276 | if (localII->modifiesRegister(cmpReg1, TRI) || |
| 277 | (secondReg && localII->modifiesRegister(cmpOp2, TRI))) |
| 278 | return false; |
| 279 | } |
| 280 | return true; |
| 281 | } |
| 282 | |
| 283 | // Given a compare operator, return a matching New Value Jump |
| 284 | // compare operator. Make sure that MI here is included in |
| 285 | // HexagonInstrInfo.cpp::isNewValueJumpCandidate |
Jyotsna Verma | 1d29750 | 2013-05-02 15:39:30 +0000 | [diff] [blame] | 286 | static unsigned getNewValueJumpOpcode(MachineInstr *MI, int reg, |
| 287 | bool secondRegNewified, |
| 288 | MachineBasicBlock *jmpTarget, |
| 289 | const MachineBranchProbabilityInfo |
| 290 | *MBPI) { |
| 291 | bool taken = false; |
| 292 | MachineBasicBlock *Src = MI->getParent(); |
| 293 | const BranchProbability Prediction = |
| 294 | MBPI->getEdgeProbability(Src, jmpTarget); |
| 295 | |
| 296 | if (Prediction >= BranchProbability(1,2)) |
| 297 | taken = true; |
| 298 | |
Sirish Pande | 4bd20c5 | 2012-05-12 05:10:30 +0000 | [diff] [blame] | 299 | switch (MI->getOpcode()) { |
Colin LeMahieu | 902157c | 2014-11-25 18:20:52 +0000 | [diff] [blame] | 300 | case Hexagon::C2_cmpeq: |
Colin LeMahieu | 6e3e62f | 2015-02-05 22:03:32 +0000 | [diff] [blame] | 301 | return taken ? Hexagon::J4_cmpeq_t_jumpnv_t |
| 302 | : Hexagon::J4_cmpeq_t_jumpnv_nt; |
Sirish Pande | 4bd20c5 | 2012-05-12 05:10:30 +0000 | [diff] [blame] | 303 | |
Colin LeMahieu | 6e0f9f8 | 2014-11-26 19:43:12 +0000 | [diff] [blame] | 304 | case Hexagon::C2_cmpeqi: { |
Sirish Pande | 4bd20c5 | 2012-05-12 05:10:30 +0000 | [diff] [blame] | 305 | if (reg >= 0) |
Colin LeMahieu | 6e3e62f | 2015-02-05 22:03:32 +0000 | [diff] [blame] | 306 | return taken ? Hexagon::J4_cmpeqi_t_jumpnv_t |
| 307 | : Hexagon::J4_cmpeqi_t_jumpnv_nt; |
Sirish Pande | 4bd20c5 | 2012-05-12 05:10:30 +0000 | [diff] [blame] | 308 | else |
Colin LeMahieu | 6e3e62f | 2015-02-05 22:03:32 +0000 | [diff] [blame] | 309 | return taken ? Hexagon::J4_cmpeqn1_t_jumpnv_t |
| 310 | : Hexagon::J4_cmpeqn1_t_jumpnv_nt; |
Sirish Pande | 4bd20c5 | 2012-05-12 05:10:30 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Colin LeMahieu | 902157c | 2014-11-25 18:20:52 +0000 | [diff] [blame] | 313 | case Hexagon::C2_cmpgt: { |
Sirish Pande | 4bd20c5 | 2012-05-12 05:10:30 +0000 | [diff] [blame] | 314 | if (secondRegNewified) |
Colin LeMahieu | 6e3e62f | 2015-02-05 22:03:32 +0000 | [diff] [blame] | 315 | return taken ? Hexagon::J4_cmplt_t_jumpnv_t |
| 316 | : Hexagon::J4_cmplt_t_jumpnv_nt; |
Sirish Pande | 4bd20c5 | 2012-05-12 05:10:30 +0000 | [diff] [blame] | 317 | else |
Colin LeMahieu | 6e3e62f | 2015-02-05 22:03:32 +0000 | [diff] [blame] | 318 | return taken ? Hexagon::J4_cmpgt_t_jumpnv_t |
| 319 | : Hexagon::J4_cmpgt_t_jumpnv_nt; |
Sirish Pande | 4bd20c5 | 2012-05-12 05:10:30 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Colin LeMahieu | 6e0f9f8 | 2014-11-26 19:43:12 +0000 | [diff] [blame] | 322 | case Hexagon::C2_cmpgti: { |
Sirish Pande | 4bd20c5 | 2012-05-12 05:10:30 +0000 | [diff] [blame] | 323 | if (reg >= 0) |
Colin LeMahieu | 6e3e62f | 2015-02-05 22:03:32 +0000 | [diff] [blame] | 324 | return taken ? Hexagon::J4_cmpgti_t_jumpnv_t |
| 325 | : Hexagon::J4_cmpgti_t_jumpnv_nt; |
Sirish Pande | 4bd20c5 | 2012-05-12 05:10:30 +0000 | [diff] [blame] | 326 | else |
Colin LeMahieu | 6e3e62f | 2015-02-05 22:03:32 +0000 | [diff] [blame] | 327 | return taken ? Hexagon::J4_cmpgtn1_t_jumpnv_t |
| 328 | : Hexagon::J4_cmpgtn1_t_jumpnv_nt; |
Sirish Pande | 4bd20c5 | 2012-05-12 05:10:30 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Colin LeMahieu | 902157c | 2014-11-25 18:20:52 +0000 | [diff] [blame] | 331 | case Hexagon::C2_cmpgtu: { |
Sirish Pande | 4bd20c5 | 2012-05-12 05:10:30 +0000 | [diff] [blame] | 332 | if (secondRegNewified) |
Colin LeMahieu | 6e3e62f | 2015-02-05 22:03:32 +0000 | [diff] [blame] | 333 | return taken ? Hexagon::J4_cmpltu_t_jumpnv_t |
| 334 | : Hexagon::J4_cmpltu_t_jumpnv_nt; |
Sirish Pande | 4bd20c5 | 2012-05-12 05:10:30 +0000 | [diff] [blame] | 335 | else |
Colin LeMahieu | 6e3e62f | 2015-02-05 22:03:32 +0000 | [diff] [blame] | 336 | return taken ? Hexagon::J4_cmpgtu_t_jumpnv_t |
| 337 | : Hexagon::J4_cmpgtu_t_jumpnv_nt; |
Sirish Pande | 4bd20c5 | 2012-05-12 05:10:30 +0000 | [diff] [blame] | 338 | } |
| 339 | |
Colin LeMahieu | 6e0f9f8 | 2014-11-26 19:43:12 +0000 | [diff] [blame] | 340 | case Hexagon::C2_cmpgtui: |
Colin LeMahieu | 6e3e62f | 2015-02-05 22:03:32 +0000 | [diff] [blame] | 341 | return taken ? Hexagon::J4_cmpgtui_t_jumpnv_t |
| 342 | : Hexagon::J4_cmpgtui_t_jumpnv_nt; |
Sirish Pande | 4bd20c5 | 2012-05-12 05:10:30 +0000 | [diff] [blame] | 343 | |
Sirish Pande | 4bd20c5 | 2012-05-12 05:10:30 +0000 | [diff] [blame] | 344 | default: |
| 345 | llvm_unreachable("Could not find matching New Value Jump instruction."); |
| 346 | } |
| 347 | // return *some value* to avoid compiler warning |
| 348 | return 0; |
| 349 | } |
| 350 | |
| 351 | bool HexagonNewValueJump::runOnMachineFunction(MachineFunction &MF) { |
| 352 | |
| 353 | DEBUG(dbgs() << "********** Hexagon New Value Jump **********\n" |
| 354 | << "********** Function: " |
Craig Topper | a538d83 | 2012-08-22 06:07:19 +0000 | [diff] [blame] | 355 | << MF.getName() << "\n"); |
Sirish Pande | 4bd20c5 | 2012-05-12 05:10:30 +0000 | [diff] [blame] | 356 | |
Eric Christopher | 0fef34e | 2015-02-02 22:11:42 +0000 | [diff] [blame] | 357 | // If we move NewValueJump before register allocation we'll need live variable |
| 358 | // analysis here too. |
Sirish Pande | 4bd20c5 | 2012-05-12 05:10:30 +0000 | [diff] [blame] | 359 | |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 360 | QII = static_cast<const HexagonInstrInfo *>(MF.getSubtarget().getInstrInfo()); |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 361 | QRI = static_cast<const HexagonRegisterInfo *>( |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 362 | MF.getSubtarget().getRegisterInfo()); |
Jyotsna Verma | 1d29750 | 2013-05-02 15:39:30 +0000 | [diff] [blame] | 363 | MBPI = &getAnalysis<MachineBranchProbabilityInfo>(); |
Sirish Pande | 4bd20c5 | 2012-05-12 05:10:30 +0000 | [diff] [blame] | 364 | |
Colin LeMahieu | 4fd203d | 2015-02-09 21:56:37 +0000 | [diff] [blame] | 365 | if (DisableNewValueJumps) { |
Sirish Pande | 4bd20c5 | 2012-05-12 05:10:30 +0000 | [diff] [blame] | 366 | return false; |
| 367 | } |
| 368 | |
| 369 | int nvjCount = DbgNVJCount; |
| 370 | int nvjGenerated = 0; |
| 371 | |
| 372 | // Loop through all the bb's of the function |
| 373 | for (MachineFunction::iterator MBBb = MF.begin(), MBBe = MF.end(); |
| 374 | MBBb != MBBe; ++MBBb) { |
Duncan P. N. Exon Smith | a72c6e2 | 2015-10-20 00:46:39 +0000 | [diff] [blame^] | 375 | MachineBasicBlock *MBB = &*MBBb; |
Sirish Pande | 4bd20c5 | 2012-05-12 05:10:30 +0000 | [diff] [blame] | 376 | |
| 377 | DEBUG(dbgs() << "** dumping bb ** " |
| 378 | << MBB->getNumber() << "\n"); |
| 379 | DEBUG(MBB->dump()); |
| 380 | DEBUG(dbgs() << "\n" << "********** dumping instr bottom up **********\n"); |
| 381 | bool foundJump = false; |
| 382 | bool foundCompare = false; |
| 383 | bool invertPredicate = false; |
| 384 | unsigned predReg = 0; // predicate reg of the jump. |
| 385 | unsigned cmpReg1 = 0; |
| 386 | int cmpOp2 = 0; |
| 387 | bool MO1IsKill = false; |
| 388 | bool MO2IsKill = false; |
| 389 | MachineBasicBlock::iterator jmpPos; |
| 390 | MachineBasicBlock::iterator cmpPos; |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 391 | MachineInstr *cmpInstr = nullptr, *jmpInstr = nullptr; |
| 392 | MachineBasicBlock *jmpTarget = nullptr; |
Sirish Pande | 4bd20c5 | 2012-05-12 05:10:30 +0000 | [diff] [blame] | 393 | bool afterRA = false; |
| 394 | bool isSecondOpReg = false; |
| 395 | bool isSecondOpNewified = false; |
| 396 | // Traverse the basic block - bottom up |
| 397 | for (MachineBasicBlock::iterator MII = MBB->end(), E = MBB->begin(); |
| 398 | MII != E;) { |
| 399 | MachineInstr *MI = --MII; |
| 400 | if (MI->isDebugValue()) { |
| 401 | continue; |
| 402 | } |
| 403 | |
| 404 | if ((nvjCount == 0) || (nvjCount > -1 && nvjCount <= nvjGenerated)) |
| 405 | break; |
| 406 | |
| 407 | DEBUG(dbgs() << "Instr: "; MI->dump(); dbgs() << "\n"); |
| 408 | |
| 409 | if (!foundJump && |
Colin LeMahieu | db0b13c | 2014-12-10 21:24:10 +0000 | [diff] [blame] | 410 | (MI->getOpcode() == Hexagon::J2_jumpt || |
| 411 | MI->getOpcode() == Hexagon::J2_jumpf || |
| 412 | MI->getOpcode() == Hexagon::J2_jumptnewpt || |
| 413 | MI->getOpcode() == Hexagon::J2_jumptnew || |
| 414 | MI->getOpcode() == Hexagon::J2_jumpfnewpt || |
| 415 | MI->getOpcode() == Hexagon::J2_jumpfnew)) { |
Sirish Pande | 4bd20c5 | 2012-05-12 05:10:30 +0000 | [diff] [blame] | 416 | // This is where you would insert your compare and |
| 417 | // instr that feeds compare |
| 418 | jmpPos = MII; |
| 419 | jmpInstr = MI; |
| 420 | predReg = MI->getOperand(0).getReg(); |
| 421 | afterRA = TargetRegisterInfo::isPhysicalRegister(predReg); |
| 422 | |
| 423 | // If ifconverter had not messed up with the kill flags of the |
| 424 | // operands, the following check on the kill flag would suffice. |
| 425 | // if(!jmpInstr->getOperand(0).isKill()) break; |
| 426 | |
| 427 | // This predicate register is live out out of BB |
| 428 | // this would only work if we can actually use Live |
| 429 | // variable analysis on phy regs - but LLVM does not |
| 430 | // provide LV analysis on phys regs. |
| 431 | //if(LVs.isLiveOut(predReg, *MBB)) break; |
| 432 | |
| 433 | // Get all the successors of this block - which will always |
| 434 | // be 2. Check if the predicate register is live in in those |
| 435 | // successor. If yes, we can not delete the predicate - |
| 436 | // I am doing this only because LLVM does not provide LiveOut |
| 437 | // at the BB level. |
| 438 | bool predLive = false; |
| 439 | for (MachineBasicBlock::const_succ_iterator SI = MBB->succ_begin(), |
| 440 | SIE = MBB->succ_end(); SI != SIE; ++SI) { |
| 441 | MachineBasicBlock* succMBB = *SI; |
| 442 | if (succMBB->isLiveIn(predReg)) { |
| 443 | predLive = true; |
| 444 | } |
| 445 | } |
| 446 | if (predLive) |
| 447 | break; |
| 448 | |
| 449 | jmpTarget = MI->getOperand(1).getMBB(); |
| 450 | foundJump = true; |
Colin LeMahieu | db0b13c | 2014-12-10 21:24:10 +0000 | [diff] [blame] | 451 | if (MI->getOpcode() == Hexagon::J2_jumpf || |
| 452 | MI->getOpcode() == Hexagon::J2_jumpfnewpt || |
| 453 | MI->getOpcode() == Hexagon::J2_jumpfnew) { |
Sirish Pande | 4bd20c5 | 2012-05-12 05:10:30 +0000 | [diff] [blame] | 454 | invertPredicate = true; |
| 455 | } |
| 456 | continue; |
| 457 | } |
| 458 | |
| 459 | // No new value jump if there is a barrier. A barrier has to be in its |
| 460 | // own packet. A barrier has zero operands. We conservatively bail out |
| 461 | // here if we see any instruction with zero operands. |
| 462 | if (foundJump && MI->getNumOperands() == 0) |
| 463 | break; |
| 464 | |
| 465 | if (foundJump && |
| 466 | !foundCompare && |
| 467 | MI->getOperand(0).isReg() && |
| 468 | MI->getOperand(0).getReg() == predReg) { |
| 469 | |
| 470 | // Not all compares can be new value compare. Arch Spec: 7.6.1.1 |
| 471 | if (QII->isNewValueJumpCandidate(MI)) { |
| 472 | |
| 473 | assert((MI->getDesc().isCompare()) && |
| 474 | "Only compare instruction can be collapsed into New Value Jump"); |
| 475 | isSecondOpReg = MI->getOperand(2).isReg(); |
| 476 | |
| 477 | if (!canCompareBeNewValueJump(QII, QRI, MII, predReg, isSecondOpReg, |
| 478 | afterRA, jmpPos, MF)) |
| 479 | break; |
| 480 | |
| 481 | cmpInstr = MI; |
| 482 | cmpPos = MII; |
| 483 | foundCompare = true; |
| 484 | |
| 485 | // We need cmpReg1 and cmpOp2(imm or reg) while building |
| 486 | // new value jump instruction. |
| 487 | cmpReg1 = MI->getOperand(1).getReg(); |
| 488 | if (MI->getOperand(1).isKill()) |
| 489 | MO1IsKill = true; |
| 490 | |
| 491 | if (isSecondOpReg) { |
| 492 | cmpOp2 = MI->getOperand(2).getReg(); |
| 493 | if (MI->getOperand(2).isKill()) |
| 494 | MO2IsKill = true; |
| 495 | } else |
| 496 | cmpOp2 = MI->getOperand(2).getImm(); |
| 497 | continue; |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | if (foundCompare && foundJump) { |
| 502 | |
| 503 | // If "common" checks fail, bail out on this BB. |
| 504 | if (!commonChecksToProhibitNewValueJump(afterRA, MII)) |
| 505 | break; |
| 506 | |
| 507 | bool foundFeeder = false; |
| 508 | MachineBasicBlock::iterator feederPos = MII; |
| 509 | if (MI->getOperand(0).isReg() && |
| 510 | MI->getOperand(0).isDef() && |
| 511 | (MI->getOperand(0).getReg() == cmpReg1 || |
| 512 | (isSecondOpReg && |
| 513 | MI->getOperand(0).getReg() == (unsigned) cmpOp2))) { |
| 514 | |
| 515 | unsigned feederReg = MI->getOperand(0).getReg(); |
| 516 | |
| 517 | // First try to see if we can get the feeder from the first operand |
| 518 | // of the compare. If we can not, and if secondOpReg is true |
| 519 | // (second operand of the compare is also register), try that one. |
| 520 | // TODO: Try to come up with some heuristic to figure out which |
| 521 | // feeder would benefit. |
| 522 | |
| 523 | if (feederReg == cmpReg1) { |
| 524 | if (!canBeFeederToNewValueJump(QII, QRI, MII, jmpPos, cmpPos, MF)) { |
| 525 | if (!isSecondOpReg) |
| 526 | break; |
| 527 | else |
| 528 | continue; |
| 529 | } else |
| 530 | foundFeeder = true; |
| 531 | } |
| 532 | |
| 533 | if (!foundFeeder && |
| 534 | isSecondOpReg && |
| 535 | feederReg == (unsigned) cmpOp2) |
| 536 | if (!canBeFeederToNewValueJump(QII, QRI, MII, jmpPos, cmpPos, MF)) |
| 537 | break; |
| 538 | |
| 539 | if (isSecondOpReg) { |
| 540 | // In case of CMPLT, or CMPLTU, or EQ with the second register |
| 541 | // to newify, swap the operands. |
Colin LeMahieu | 902157c | 2014-11-25 18:20:52 +0000 | [diff] [blame] | 542 | if (cmpInstr->getOpcode() == Hexagon::C2_cmpeq && |
Jyotsna Verma | 89c8482 | 2013-04-23 19:15:55 +0000 | [diff] [blame] | 543 | feederReg == (unsigned) cmpOp2) { |
Sirish Pande | 4bd20c5 | 2012-05-12 05:10:30 +0000 | [diff] [blame] | 544 | unsigned tmp = cmpReg1; |
| 545 | bool tmpIsKill = MO1IsKill; |
| 546 | cmpReg1 = cmpOp2; |
| 547 | MO1IsKill = MO2IsKill; |
| 548 | cmpOp2 = tmp; |
| 549 | MO2IsKill = tmpIsKill; |
| 550 | } |
| 551 | |
| 552 | // Now we have swapped the operands, all we need to check is, |
| 553 | // if the second operand (after swap) is the feeder. |
| 554 | // And if it is, make a note. |
| 555 | if (feederReg == (unsigned)cmpOp2) |
| 556 | isSecondOpNewified = true; |
| 557 | } |
| 558 | |
| 559 | // Now that we are moving feeder close the jump, |
| 560 | // make sure we are respecting the kill values of |
| 561 | // the operands of the feeder. |
| 562 | |
| 563 | bool updatedIsKill = false; |
| 564 | for (unsigned i = 0; i < MI->getNumOperands(); i++) { |
| 565 | MachineOperand &MO = MI->getOperand(i); |
| 566 | if (MO.isReg() && MO.isUse()) { |
| 567 | unsigned feederReg = MO.getReg(); |
| 568 | for (MachineBasicBlock::iterator localII = feederPos, |
| 569 | end = jmpPos; localII != end; localII++) { |
| 570 | MachineInstr *localMI = localII; |
| 571 | for (unsigned j = 0; j < localMI->getNumOperands(); j++) { |
| 572 | MachineOperand &localMO = localMI->getOperand(j); |
| 573 | if (localMO.isReg() && localMO.isUse() && |
| 574 | localMO.isKill() && feederReg == localMO.getReg()) { |
| 575 | // We found that there is kill of a use register |
| 576 | // Set up a kill flag on the register |
| 577 | localMO.setIsKill(false); |
| 578 | MO.setIsKill(); |
| 579 | updatedIsKill = true; |
| 580 | break; |
| 581 | } |
| 582 | } |
| 583 | if (updatedIsKill) break; |
| 584 | } |
| 585 | } |
| 586 | if (updatedIsKill) break; |
| 587 | } |
| 588 | |
| 589 | MBB->splice(jmpPos, MI->getParent(), MI); |
| 590 | MBB->splice(jmpPos, MI->getParent(), cmpInstr); |
| 591 | DebugLoc dl = MI->getDebugLoc(); |
| 592 | MachineInstr *NewMI; |
| 593 | |
| 594 | assert((QII->isNewValueJumpCandidate(cmpInstr)) && |
| 595 | "This compare is not a New Value Jump candidate."); |
| 596 | unsigned opc = getNewValueJumpOpcode(cmpInstr, cmpOp2, |
Jyotsna Verma | 1d29750 | 2013-05-02 15:39:30 +0000 | [diff] [blame] | 597 | isSecondOpNewified, |
| 598 | jmpTarget, MBPI); |
Sirish Pande | 4bd20c5 | 2012-05-12 05:10:30 +0000 | [diff] [blame] | 599 | if (invertPredicate) |
| 600 | opc = QII->getInvertedPredicatedOpcode(opc); |
| 601 | |
Jyotsna Verma | 89c8482 | 2013-04-23 19:15:55 +0000 | [diff] [blame] | 602 | if (isSecondOpReg) |
Sirish Pande | 4bd20c5 | 2012-05-12 05:10:30 +0000 | [diff] [blame] | 603 | NewMI = BuildMI(*MBB, jmpPos, dl, |
| 604 | QII->get(opc)) |
| 605 | .addReg(cmpReg1, getKillRegState(MO1IsKill)) |
| 606 | .addReg(cmpOp2, getKillRegState(MO2IsKill)) |
| 607 | .addMBB(jmpTarget); |
Jyotsna Verma | 89c8482 | 2013-04-23 19:15:55 +0000 | [diff] [blame] | 608 | |
Colin LeMahieu | 6e0f9f8 | 2014-11-26 19:43:12 +0000 | [diff] [blame] | 609 | else if ((cmpInstr->getOpcode() == Hexagon::C2_cmpeqi || |
| 610 | cmpInstr->getOpcode() == Hexagon::C2_cmpgti) && |
Jyotsna Verma | 89c8482 | 2013-04-23 19:15:55 +0000 | [diff] [blame] | 611 | cmpOp2 == -1 ) |
| 612 | // Corresponding new-value compare jump instructions don't have the |
| 613 | // operand for -1 immediate value. |
| 614 | NewMI = BuildMI(*MBB, jmpPos, dl, |
| 615 | QII->get(opc)) |
| 616 | .addReg(cmpReg1, getKillRegState(MO1IsKill)) |
| 617 | .addMBB(jmpTarget); |
| 618 | |
| 619 | else |
Sirish Pande | 4bd20c5 | 2012-05-12 05:10:30 +0000 | [diff] [blame] | 620 | NewMI = BuildMI(*MBB, jmpPos, dl, |
| 621 | QII->get(opc)) |
| 622 | .addReg(cmpReg1, getKillRegState(MO1IsKill)) |
| 623 | .addImm(cmpOp2) |
| 624 | .addMBB(jmpTarget); |
Sirish Pande | 4bd20c5 | 2012-05-12 05:10:30 +0000 | [diff] [blame] | 625 | |
| 626 | assert(NewMI && "New Value Jump Instruction Not created!"); |
Duncan Sands | 0480b9b | 2013-05-13 07:50:47 +0000 | [diff] [blame] | 627 | (void)NewMI; |
Sirish Pande | 4bd20c5 | 2012-05-12 05:10:30 +0000 | [diff] [blame] | 628 | if (cmpInstr->getOperand(0).isReg() && |
| 629 | cmpInstr->getOperand(0).isKill()) |
| 630 | cmpInstr->getOperand(0).setIsKill(false); |
| 631 | if (cmpInstr->getOperand(1).isReg() && |
| 632 | cmpInstr->getOperand(1).isKill()) |
| 633 | cmpInstr->getOperand(1).setIsKill(false); |
| 634 | cmpInstr->eraseFromParent(); |
| 635 | jmpInstr->eraseFromParent(); |
| 636 | ++nvjGenerated; |
| 637 | ++NumNVJGenerated; |
| 638 | break; |
| 639 | } |
| 640 | } |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | return true; |
| 645 | |
| 646 | } |
| 647 | |
| 648 | FunctionPass *llvm::createHexagonNewValueJump() { |
| 649 | return new HexagonNewValueJump(); |
| 650 | } |