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