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