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