blob: e3579dfa9ba971a10f1db71ab3648eb4538252ee [file] [log] [blame]
Eugene Zelenko3b873362017-09-28 22:27:31 +00001//===- HexagonNewValueJump.cpp - Hexagon Backend New Value Jump -----------===//
Sirish Pande4bd20c52012-05-12 05:10:30 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Sirish Pande4bd20c52012-05-12 05:10:30 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This implements NewValueJump pass in Hexagon.
10// Ideally, we should merge this as a Peephole pass prior to register
Benjamin Kramerbde91762012-06-02 10:20:22 +000011// allocation, but because we have a spill in between the feeder and new value
Sirish Pande4bd20c52012-05-12 05:10:30 +000012// jump instructions, we are forced to write after register allocation.
Benjamin Kramerbde91762012-06-02 10:20:22 +000013// Having said that, we should re-attempt to pull this earlier at some point
Sirish Pande4bd20c52012-05-12 05:10:30 +000014// in future.
15
16// The basic approach looks for sequence of predicated jump, compare instruciton
17// that genereates the predicate and, the feeder to the predicate. Once it finds
Fangrui Song956ee792018-03-30 22:22:31 +000018// all, it collapses compare and jump instruction into a new value jump
Sirish Pande4bd20c52012-05-12 05:10:30 +000019// intstructions.
20//
Sirish Pande4bd20c52012-05-12 05:10:30 +000021//===----------------------------------------------------------------------===//
Eugene Zelenko3b873362017-09-28 22:27:31 +000022
Reid Kleckner05da2fe2019-11-13 13:15:01 -080023#include "llvm/InitializePasses.h"
Jyotsna Verma84c47102013-05-06 18:49:23 +000024#include "Hexagon.h"
Jyotsna Verma84c47102013-05-06 18:49:23 +000025#include "HexagonInstrInfo.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000026#include "HexagonRegisterInfo.h"
Krzysztof Parzyszek5d41cc12018-03-12 17:47:46 +000027#include "HexagonSubtarget.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000028#include "llvm/ADT/Statistic.h"
Eugene Zelenko3b873362017-09-28 22:27:31 +000029#include "llvm/CodeGen/MachineBasicBlock.h"
30#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
31#include "llvm/CodeGen/MachineFunction.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000032#include "llvm/CodeGen/MachineFunctionPass.h"
Eugene Zelenko3b873362017-09-28 22:27:31 +000033#include "llvm/CodeGen/MachineInstr.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000034#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenko3b873362017-09-28 22:27:31 +000035#include "llvm/CodeGen/MachineOperand.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000036#include "llvm/CodeGen/MachineRegisterInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000037#include "llvm/CodeGen/TargetOpcodes.h"
38#include "llvm/CodeGen/TargetRegisterInfo.h"
39#include "llvm/CodeGen/TargetSubtargetInfo.h"
Eugene Zelenko3b873362017-09-28 22:27:31 +000040#include "llvm/IR/DebugLoc.h"
41#include "llvm/MC/MCInstrDesc.h"
42#include "llvm/Pass.h"
43#include "llvm/Support/BranchProbability.h"
Jyotsna Verma84c47102013-05-06 18:49:23 +000044#include "llvm/Support/CommandLine.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000045#include "llvm/Support/Debug.h"
Eugene Zelenko3b873362017-09-28 22:27:31 +000046#include "llvm/Support/ErrorHandling.h"
47#include "llvm/Support/MathExtras.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000048#include "llvm/Support/raw_ostream.h"
Eugene Zelenko3b873362017-09-28 22:27:31 +000049#include <cassert>
50#include <cstdint>
51#include <iterator>
52
Sirish Pande4bd20c52012-05-12 05:10:30 +000053using namespace llvm;
54
Chandler Carruth84e68b22014-04-22 02:41:26 +000055#define DEBUG_TYPE "hexagon-nvj"
56
Sirish Pande4bd20c52012-05-12 05:10:30 +000057STATISTIC(NumNVJGenerated, "Number of New Value Jump Instructions created");
58
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +000059static cl::opt<int> DbgNVJCount("nvj-count", cl::init(-1), cl::Hidden,
60 cl::desc("Maximum number of predicated jumps to be converted to "
61 "New Value Jump"));
Sirish Pande4bd20c52012-05-12 05:10:30 +000062
63static cl::opt<bool> DisableNewValueJumps("disable-nvjump", cl::Hidden,
64 cl::ZeroOrMore, cl::init(false),
65 cl::desc("Disable New Value Jumps"));
66
Jyotsna Verma84c47102013-05-06 18:49:23 +000067namespace llvm {
Jyotsna Verma84c47102013-05-06 18:49:23 +000068
Eugene Zelenko3b873362017-09-28 22:27:31 +000069FunctionPass *createHexagonNewValueJump();
70void initializeHexagonNewValueJumpPass(PassRegistry&);
71
72} // end namespace llvm
Jyotsna Verma84c47102013-05-06 18:49:23 +000073
Sirish Pande4bd20c52012-05-12 05:10:30 +000074namespace {
Eugene Zelenko3b873362017-09-28 22:27:31 +000075
Sirish Pande4bd20c52012-05-12 05:10:30 +000076 struct HexagonNewValueJump : public MachineFunctionPass {
Sirish Pande4bd20c52012-05-12 05:10:30 +000077 static char ID;
78
Krzysztof Parzyszek5ddd2e52017-06-27 18:37:16 +000079 HexagonNewValueJump() : MachineFunctionPass(ID) {}
Sirish Pande4bd20c52012-05-12 05:10:30 +000080
Craig Topper906c2cd2014-04-29 07:58:16 +000081 void getAnalysisUsage(AnalysisUsage &AU) const override {
Jyotsna Verma1d297502013-05-02 15:39:30 +000082 AU.addRequired<MachineBranchProbabilityInfo>();
Sirish Pande4bd20c52012-05-12 05:10:30 +000083 MachineFunctionPass::getAnalysisUsage(AU);
84 }
85
Mehdi Amini117296c2016-10-01 02:56:57 +000086 StringRef getPassName() const override { return "Hexagon NewValueJump"; }
Sirish Pande4bd20c52012-05-12 05:10:30 +000087
Craig Topper906c2cd2014-04-29 07:58:16 +000088 bool runOnMachineFunction(MachineFunction &Fn) override;
Eugene Zelenko3b873362017-09-28 22:27:31 +000089
Derek Schuff1dbf7a52016-04-04 17:09:25 +000090 MachineFunctionProperties getRequiredProperties() const override {
91 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +000092 MachineFunctionProperties::Property::NoVRegs);
Derek Schuff1dbf7a52016-04-04 17:09:25 +000093 }
Sirish Pande4bd20c52012-05-12 05:10:30 +000094
95 private:
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +000096 const HexagonInstrInfo *QII;
97 const HexagonRegisterInfo *QRI;
98
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000099 /// A handle to the branch probability pass.
Jyotsna Verma1d297502013-05-02 15:39:30 +0000100 const MachineBranchProbabilityInfo *MBPI;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000101
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000102 bool isNewValueJumpCandidate(const MachineInstr &MI) const;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000103 };
104
Eugene Zelenko3b873362017-09-28 22:27:31 +0000105} // end anonymous namespace
Sirish Pande4bd20c52012-05-12 05:10:30 +0000106
107char HexagonNewValueJump::ID = 0;
108
Jyotsna Verma84c47102013-05-06 18:49:23 +0000109INITIALIZE_PASS_BEGIN(HexagonNewValueJump, "hexagon-nvj",
110 "Hexagon NewValueJump", false, false)
111INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
112INITIALIZE_PASS_END(HexagonNewValueJump, "hexagon-nvj",
113 "Hexagon NewValueJump", false, false)
114
Sirish Pande4bd20c52012-05-12 05:10:30 +0000115// We have identified this II could be feeder to NVJ,
116// verify that it can be.
117static bool canBeFeederToNewValueJump(const HexagonInstrInfo *QII,
118 const TargetRegisterInfo *TRI,
119 MachineBasicBlock::iterator II,
120 MachineBasicBlock::iterator end,
121 MachineBasicBlock::iterator skip,
122 MachineFunction &MF) {
Sirish Pande4bd20c52012-05-12 05:10:30 +0000123 // Predicated instruction can not be feeder to NVJ.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000124 if (QII->isPredicated(*II))
Sirish Pande4bd20c52012-05-12 05:10:30 +0000125 return false;
126
127 // Bail out if feederReg is a paired register (double regs in
128 // our case). One would think that we can check to see if a given
129 // register cmpReg1 or cmpReg2 is a sub register of feederReg
130 // using -- if (QRI->isSubRegister(feederReg, cmpReg1) logic
131 // before the callsite of this function
132 // But we can not as it comes in the following fashion.
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000133 // %d0 = Hexagon_S2_lsr_r_p killed %d0, killed %r2
134 // %r0 = KILL %r0, implicit killed %d0
135 // %p0 = CMPEQri killed %r0, 0
Sirish Pande4bd20c52012-05-12 05:10:30 +0000136 // Hence, we need to check if it's a KILL instruction.
137 if (II->getOpcode() == TargetOpcode::KILL)
138 return false;
139
Krzysztof Parzyszek2cfc7a42017-02-23 17:47:34 +0000140 if (II->isImplicitDef())
141 return false;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000142
Krzysztof Parzyszek44555222017-11-30 20:32:54 +0000143 if (QII->isSolo(*II))
144 return false;
145
Krzysztof Parzyszekbe253e72018-02-06 19:08:41 +0000146 if (QII->isFloat(*II))
147 return false;
148
149 // Make sure that the (unique) def operand is a register from IntRegs.
150 bool HadDef = false;
151 for (const MachineOperand &Op : II->operands()) {
152 if (!Op.isReg() || !Op.isDef())
153 continue;
154 if (HadDef)
155 return false;
156 HadDef = true;
157 if (!Hexagon::IntRegsRegClass.contains(Op.getReg()))
158 return false;
159 }
160 assert(HadDef);
161
Fangrui Song956ee792018-03-30 22:22:31 +0000162 // Make sure there is no 'def' or 'use' of any of the uses of
Eric Christopher563d0b92018-05-21 10:27:36 +0000163 // feeder insn between its definition, this MI and jump, jmpInst
Sirish Pande4bd20c52012-05-12 05:10:30 +0000164 // skipping compare, cmpInst.
165 // Here's the example.
166 // r21=memub(r22+r24<<#0)
167 // p0 = cmp.eq(r21, #0)
168 // r4=memub(r3+r21<<#0)
169 // if (p0.new) jump:t .LBB29_45
170 // Without this check, it will be converted into
171 // r4=memub(r3+r21<<#0)
172 // r21=memub(r22+r24<<#0)
173 // p0 = cmp.eq(r21, #0)
174 // if (p0.new) jump:t .LBB29_45
175 // and result WAR hazards if converted to New Value Jump.
Sirish Pande4bd20c52012-05-12 05:10:30 +0000176 for (unsigned i = 0; i < II->getNumOperands(); ++i) {
177 if (II->getOperand(i).isReg() &&
178 (II->getOperand(i).isUse() || II->getOperand(i).isDef())) {
179 MachineBasicBlock::iterator localII = II;
180 ++localII;
Daniel Sanders0c476112019-08-15 19:22:08 +0000181 Register Reg = II->getOperand(i).getReg();
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000182 for (MachineBasicBlock::iterator localBegin = localII; localBegin != end;
183 ++localBegin) {
184 if (localBegin == skip)
185 continue;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000186 // Check for Subregisters too.
187 if (localBegin->modifiesRegister(Reg, TRI) ||
188 localBegin->readsRegister(Reg, TRI))
189 return false;
190 }
191 }
192 }
193 return true;
194}
195
196// These are the common checks that need to performed
197// to determine if
198// 1. compare instruction can be moved before jump.
199// 2. feeder to the compare instruction can be moved before jump.
200static bool commonChecksToProhibitNewValueJump(bool afterRA,
201 MachineBasicBlock::iterator MII) {
Sirish Pande4bd20c52012-05-12 05:10:30 +0000202 // If store in path, bail out.
Krzysztof Parzyszek1fd0c7e2017-07-24 19:35:48 +0000203 if (MII->mayStore())
Sirish Pande4bd20c52012-05-12 05:10:30 +0000204 return false;
205
206 // if call in path, bail out.
Krzysztof Parzyszek3d9946e2016-08-19 17:54:49 +0000207 if (MII->isCall())
Sirish Pande4bd20c52012-05-12 05:10:30 +0000208 return false;
209
210 // if NVJ is running prior to RA, do the following checks.
211 if (!afterRA) {
212 // The following Target Opcode instructions are spurious
213 // to new value jump. If they are in the path, bail out.
214 // KILL sets kill flag on the opcode. It also sets up a
215 // single register, out of pair.
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000216 // %d0 = S2_lsr_r_p killed %d0, killed %r2
217 // %r0 = KILL %r0, implicit killed %d0
218 // %p0 = C2_cmpeqi killed %r0, 0
Sirish Pande4bd20c52012-05-12 05:10:30 +0000219 // PHI can be anything after RA.
220 // COPY can remateriaze things in between feeder, compare and nvj.
221 if (MII->getOpcode() == TargetOpcode::KILL ||
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000222 MII->getOpcode() == TargetOpcode::PHI ||
Sirish Pande4bd20c52012-05-12 05:10:30 +0000223 MII->getOpcode() == TargetOpcode::COPY)
224 return false;
225
226 // The following pseudo Hexagon instructions sets "use" and "def"
227 // of registers by individual passes in the backend. At this time,
228 // we don't know the scope of usage and definitions of these
229 // instructions.
Krzysztof Parzyszek3d9946e2016-08-19 17:54:49 +0000230 if (MII->getOpcode() == Hexagon::LDriw_pred ||
Sirish Pande4bd20c52012-05-12 05:10:30 +0000231 MII->getOpcode() == Hexagon::STriw_pred)
232 return false;
233 }
234
235 return true;
236}
237
238static bool canCompareBeNewValueJump(const HexagonInstrInfo *QII,
239 const TargetRegisterInfo *TRI,
240 MachineBasicBlock::iterator II,
241 unsigned pReg,
242 bool secondReg,
243 bool optLocation,
244 MachineBasicBlock::iterator end,
245 MachineFunction &MF) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000246 MachineInstr &MI = *II;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000247
248 // If the second operand of the compare is an imm, make sure it's in the
249 // range specified by the arch.
250 if (!secondReg) {
Krzysztof Parzyszek64e5d7d2017-10-20 19:33:12 +0000251 const MachineOperand &Op2 = MI.getOperand(2);
252 if (!Op2.isImm())
253 return false;
254
255 int64_t v = Op2.getImm();
Krzysztof Parzyszek3d9946e2016-08-19 17:54:49 +0000256 bool Valid = false;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000257
Krzysztof Parzyszek3d9946e2016-08-19 17:54:49 +0000258 switch (MI.getOpcode()) {
259 case Hexagon::C2_cmpeqi:
Krzysztof Parzyszek1fd0c7e2017-07-24 19:35:48 +0000260 case Hexagon::C4_cmpneqi:
Krzysztof Parzyszek3d9946e2016-08-19 17:54:49 +0000261 case Hexagon::C2_cmpgti:
Krzysztof Parzyszek1fd0c7e2017-07-24 19:35:48 +0000262 case Hexagon::C4_cmpltei:
Krzysztof Parzyszek3d9946e2016-08-19 17:54:49 +0000263 Valid = (isUInt<5>(v) || v == -1);
264 break;
265 case Hexagon::C2_cmpgtui:
Krzysztof Parzyszek1fd0c7e2017-07-24 19:35:48 +0000266 case Hexagon::C4_cmplteui:
Krzysztof Parzyszek3d9946e2016-08-19 17:54:49 +0000267 Valid = isUInt<5>(v);
268 break;
269 case Hexagon::S2_tstbit_i:
270 case Hexagon::S4_ntstbit_i:
271 Valid = (v == 0);
272 break;
273 }
274
275 if (!Valid)
Sirish Pande4bd20c52012-05-12 05:10:30 +0000276 return false;
277 }
278
Jyotsna Verma84c47102013-05-06 18:49:23 +0000279 unsigned cmpReg1, cmpOp2 = 0; // cmpOp2 assignment silences compiler warning.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000280 cmpReg1 = MI.getOperand(1).getReg();
Sirish Pande4bd20c52012-05-12 05:10:30 +0000281
282 if (secondReg) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000283 cmpOp2 = MI.getOperand(2).getReg();
Sirish Pande4bd20c52012-05-12 05:10:30 +0000284
Krzysztof Parzyszek3d9946e2016-08-19 17:54:49 +0000285 // If the same register appears as both operands, we cannot generate a new
286 // value compare. Only one operand may use the .new suffix.
287 if (cmpReg1 == cmpOp2)
288 return false;
289
Fangrui Song956ee792018-03-30 22:22:31 +0000290 // Make sure that the second register is not from COPY
291 // at machine code level, we don't need this, but if we decide
Sirish Pande4bd20c52012-05-12 05:10:30 +0000292 // to move new value jump prior to RA, we would be needing this.
293 MachineRegisterInfo &MRI = MF.getRegInfo();
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000294 if (secondReg && !Register::isPhysicalRegister(cmpOp2)) {
Sirish Pande4bd20c52012-05-12 05:10:30 +0000295 MachineInstr *def = MRI.getVRegDef(cmpOp2);
296 if (def->getOpcode() == TargetOpcode::COPY)
297 return false;
298 }
299 }
300
301 // Walk the instructions after the compare (predicate def) to the jump,
302 // and satisfy the following conditions.
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000303 ++II;
304 for (MachineBasicBlock::iterator localII = II; localII != end; ++localII) {
Shiva Chen801bf7e2018-05-09 02:42:00 +0000305 if (localII->isDebugInstr())
Krzysztof Parzyszek3d9946e2016-08-19 17:54:49 +0000306 continue;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000307
308 // Check 1.
309 // If "common" checks fail, bail out.
310 if (!commonChecksToProhibitNewValueJump(optLocation, localII))
311 return false;
312
313 // Check 2.
314 // If there is a def or use of predicate (result of compare), bail out.
315 if (localII->modifiesRegister(pReg, TRI) ||
316 localII->readsRegister(pReg, TRI))
317 return false;
318
319 // Check 3.
320 // If there is a def of any of the use of the compare (operands of compare),
321 // bail out.
322 // Eg.
323 // p0 = cmp.eq(r2, r0)
324 // r2 = r4
325 // if (p0.new) jump:t .LBB28_3
326 if (localII->modifiesRegister(cmpReg1, TRI) ||
327 (secondReg && localII->modifiesRegister(cmpOp2, TRI)))
328 return false;
329 }
330 return true;
331}
332
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000333// Given a compare operator, return a matching New Value Jump compare operator.
334// Make sure that MI here is included in isNewValueJumpCandidate.
Jyotsna Verma1d297502013-05-02 15:39:30 +0000335static unsigned getNewValueJumpOpcode(MachineInstr *MI, int reg,
336 bool secondRegNewified,
337 MachineBasicBlock *jmpTarget,
338 const MachineBranchProbabilityInfo
339 *MBPI) {
340 bool taken = false;
341 MachineBasicBlock *Src = MI->getParent();
342 const BranchProbability Prediction =
343 MBPI->getEdgeProbability(Src, jmpTarget);
344
345 if (Prediction >= BranchProbability(1,2))
346 taken = true;
347
Sirish Pande4bd20c52012-05-12 05:10:30 +0000348 switch (MI->getOpcode()) {
Colin LeMahieu902157c2014-11-25 18:20:52 +0000349 case Hexagon::C2_cmpeq:
Colin LeMahieu6e3e62f2015-02-05 22:03:32 +0000350 return taken ? Hexagon::J4_cmpeq_t_jumpnv_t
351 : Hexagon::J4_cmpeq_t_jumpnv_nt;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000352
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000353 case Hexagon::C2_cmpeqi:
Sirish Pande4bd20c52012-05-12 05:10:30 +0000354 if (reg >= 0)
Colin LeMahieu6e3e62f2015-02-05 22:03:32 +0000355 return taken ? Hexagon::J4_cmpeqi_t_jumpnv_t
356 : Hexagon::J4_cmpeqi_t_jumpnv_nt;
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000357 return taken ? Hexagon::J4_cmpeqn1_t_jumpnv_t
358 : Hexagon::J4_cmpeqn1_t_jumpnv_nt;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000359
Krzysztof Parzyszek1fd0c7e2017-07-24 19:35:48 +0000360 case Hexagon::C4_cmpneqi:
361 if (reg >= 0)
362 return taken ? Hexagon::J4_cmpeqi_f_jumpnv_t
363 : Hexagon::J4_cmpeqi_f_jumpnv_nt;
364 return taken ? Hexagon::J4_cmpeqn1_f_jumpnv_t :
365 Hexagon::J4_cmpeqn1_f_jumpnv_nt;
366
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000367 case Hexagon::C2_cmpgt:
Sirish Pande4bd20c52012-05-12 05:10:30 +0000368 if (secondRegNewified)
Colin LeMahieu6e3e62f2015-02-05 22:03:32 +0000369 return taken ? Hexagon::J4_cmplt_t_jumpnv_t
370 : Hexagon::J4_cmplt_t_jumpnv_nt;
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000371 return taken ? Hexagon::J4_cmpgt_t_jumpnv_t
372 : Hexagon::J4_cmpgt_t_jumpnv_nt;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000373
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000374 case Hexagon::C2_cmpgti:
Sirish Pande4bd20c52012-05-12 05:10:30 +0000375 if (reg >= 0)
Colin LeMahieu6e3e62f2015-02-05 22:03:32 +0000376 return taken ? Hexagon::J4_cmpgti_t_jumpnv_t
377 : Hexagon::J4_cmpgti_t_jumpnv_nt;
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000378 return taken ? Hexagon::J4_cmpgtn1_t_jumpnv_t
379 : Hexagon::J4_cmpgtn1_t_jumpnv_nt;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000380
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000381 case Hexagon::C2_cmpgtu:
Sirish Pande4bd20c52012-05-12 05:10:30 +0000382 if (secondRegNewified)
Colin LeMahieu6e3e62f2015-02-05 22:03:32 +0000383 return taken ? Hexagon::J4_cmpltu_t_jumpnv_t
384 : Hexagon::J4_cmpltu_t_jumpnv_nt;
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000385 return taken ? Hexagon::J4_cmpgtu_t_jumpnv_t
386 : Hexagon::J4_cmpgtu_t_jumpnv_nt;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000387
Colin LeMahieu6e0f9f82014-11-26 19:43:12 +0000388 case Hexagon::C2_cmpgtui:
Colin LeMahieu6e3e62f2015-02-05 22:03:32 +0000389 return taken ? Hexagon::J4_cmpgtui_t_jumpnv_t
390 : Hexagon::J4_cmpgtui_t_jumpnv_nt;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000391
Ron Liebermane6540e22015-12-08 16:28:32 +0000392 case Hexagon::C4_cmpneq:
393 return taken ? Hexagon::J4_cmpeq_f_jumpnv_t
394 : Hexagon::J4_cmpeq_f_jumpnv_nt;
395
396 case Hexagon::C4_cmplte:
397 if (secondRegNewified)
398 return taken ? Hexagon::J4_cmplt_f_jumpnv_t
399 : Hexagon::J4_cmplt_f_jumpnv_nt;
400 return taken ? Hexagon::J4_cmpgt_f_jumpnv_t
401 : Hexagon::J4_cmpgt_f_jumpnv_nt;
402
403 case Hexagon::C4_cmplteu:
404 if (secondRegNewified)
405 return taken ? Hexagon::J4_cmpltu_f_jumpnv_t
406 : Hexagon::J4_cmpltu_f_jumpnv_nt;
407 return taken ? Hexagon::J4_cmpgtu_f_jumpnv_t
408 : Hexagon::J4_cmpgtu_f_jumpnv_nt;
409
Krzysztof Parzyszek1fd0c7e2017-07-24 19:35:48 +0000410 case Hexagon::C4_cmpltei:
411 if (reg >= 0)
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000412 return taken ? Hexagon::J4_cmpgti_f_jumpnv_t
413 : Hexagon::J4_cmpgti_f_jumpnv_nt;
414 return taken ? Hexagon::J4_cmpgtn1_f_jumpnv_t
415 : Hexagon::J4_cmpgtn1_f_jumpnv_nt;
Krzysztof Parzyszek1fd0c7e2017-07-24 19:35:48 +0000416
417 case Hexagon::C4_cmplteui:
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000418 return taken ? Hexagon::J4_cmpgtui_f_jumpnv_t
419 : Hexagon::J4_cmpgtui_f_jumpnv_nt;
Krzysztof Parzyszek1fd0c7e2017-07-24 19:35:48 +0000420
Sirish Pande4bd20c52012-05-12 05:10:30 +0000421 default:
422 llvm_unreachable("Could not find matching New Value Jump instruction.");
423 }
424 // return *some value* to avoid compiler warning
425 return 0;
426}
427
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000428bool HexagonNewValueJump::isNewValueJumpCandidate(
429 const MachineInstr &MI) const {
430 switch (MI.getOpcode()) {
431 case Hexagon::C2_cmpeq:
432 case Hexagon::C2_cmpeqi:
433 case Hexagon::C2_cmpgt:
434 case Hexagon::C2_cmpgti:
435 case Hexagon::C2_cmpgtu:
436 case Hexagon::C2_cmpgtui:
437 case Hexagon::C4_cmpneq:
Krzysztof Parzyszek1fd0c7e2017-07-24 19:35:48 +0000438 case Hexagon::C4_cmpneqi:
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000439 case Hexagon::C4_cmplte:
440 case Hexagon::C4_cmplteu:
Krzysztof Parzyszek1fd0c7e2017-07-24 19:35:48 +0000441 case Hexagon::C4_cmpltei:
442 case Hexagon::C4_cmplteui:
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000443 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000444
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000445 default:
446 return false;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000447 }
448}
449
Sirish Pande4bd20c52012-05-12 05:10:30 +0000450bool HexagonNewValueJump::runOnMachineFunction(MachineFunction &MF) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000451 LLVM_DEBUG(dbgs() << "********** Hexagon New Value Jump **********\n"
452 << "********** Function: " << MF.getName() << "\n");
Sirish Pande4bd20c52012-05-12 05:10:30 +0000453
Matthias Braunf1caa282017-12-15 22:22:58 +0000454 if (skipFunction(MF.getFunction()))
Andrew Kaylor5b444a22016-04-26 19:46:28 +0000455 return false;
456
Eric Christopher0fef34e2015-02-02 22:11:42 +0000457 // If we move NewValueJump before register allocation we'll need live variable
458 // analysis here too.
Sirish Pande4bd20c52012-05-12 05:10:30 +0000459
Eric Christopherfc6de422014-08-05 02:39:49 +0000460 QII = static_cast<const HexagonInstrInfo *>(MF.getSubtarget().getInstrInfo());
Eric Christopherd9134482014-08-04 21:25:23 +0000461 QRI = static_cast<const HexagonRegisterInfo *>(
Eric Christopherfc6de422014-08-05 02:39:49 +0000462 MF.getSubtarget().getRegisterInfo());
Jyotsna Verma1d297502013-05-02 15:39:30 +0000463 MBPI = &getAnalysis<MachineBranchProbabilityInfo>();
Sirish Pande4bd20c52012-05-12 05:10:30 +0000464
Krzysztof Parzyszek5d41cc12018-03-12 17:47:46 +0000465 if (DisableNewValueJumps ||
466 !MF.getSubtarget<HexagonSubtarget>().useNewValueJumps())
Sirish Pande4bd20c52012-05-12 05:10:30 +0000467 return false;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000468
469 int nvjCount = DbgNVJCount;
470 int nvjGenerated = 0;
471
472 // Loop through all the bb's of the function
473 for (MachineFunction::iterator MBBb = MF.begin(), MBBe = MF.end();
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000474 MBBb != MBBe; ++MBBb) {
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000475 MachineBasicBlock *MBB = &*MBBb;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000476
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000477 LLVM_DEBUG(dbgs() << "** dumping bb ** " << MBB->getNumber() << "\n");
478 LLVM_DEBUG(MBB->dump());
479 LLVM_DEBUG(dbgs() << "\n"
480 << "********** dumping instr bottom up **********\n");
Sirish Pande4bd20c52012-05-12 05:10:30 +0000481 bool foundJump = false;
482 bool foundCompare = false;
483 bool invertPredicate = false;
484 unsigned predReg = 0; // predicate reg of the jump.
485 unsigned cmpReg1 = 0;
486 int cmpOp2 = 0;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000487 MachineBasicBlock::iterator jmpPos;
488 MachineBasicBlock::iterator cmpPos;
Craig Topper062a2ba2014-04-25 05:30:21 +0000489 MachineInstr *cmpInstr = nullptr, *jmpInstr = nullptr;
490 MachineBasicBlock *jmpTarget = nullptr;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000491 bool afterRA = false;
492 bool isSecondOpReg = false;
493 bool isSecondOpNewified = false;
494 // Traverse the basic block - bottom up
495 for (MachineBasicBlock::iterator MII = MBB->end(), E = MBB->begin();
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000496 MII != E;) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000497 MachineInstr &MI = *--MII;
Shiva Chen801bf7e2018-05-09 02:42:00 +0000498 if (MI.isDebugInstr()) {
Sirish Pande4bd20c52012-05-12 05:10:30 +0000499 continue;
500 }
501
502 if ((nvjCount == 0) || (nvjCount > -1 && nvjCount <= nvjGenerated))
503 break;
504
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000505 LLVM_DEBUG(dbgs() << "Instr: "; MI.dump(); dbgs() << "\n");
Sirish Pande4bd20c52012-05-12 05:10:30 +0000506
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000507 if (!foundJump && (MI.getOpcode() == Hexagon::J2_jumpt ||
Krzysztof Parzyszeka243adf2016-08-19 14:14:09 +0000508 MI.getOpcode() == Hexagon::J2_jumptpt ||
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000509 MI.getOpcode() == Hexagon::J2_jumpf ||
Krzysztof Parzyszeka243adf2016-08-19 14:14:09 +0000510 MI.getOpcode() == Hexagon::J2_jumpfpt ||
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000511 MI.getOpcode() == Hexagon::J2_jumptnewpt ||
512 MI.getOpcode() == Hexagon::J2_jumptnew ||
513 MI.getOpcode() == Hexagon::J2_jumpfnewpt ||
514 MI.getOpcode() == Hexagon::J2_jumpfnew)) {
Sirish Pande4bd20c52012-05-12 05:10:30 +0000515 // This is where you would insert your compare and
516 // instr that feeds compare
517 jmpPos = MII;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000518 jmpInstr = &MI;
519 predReg = MI.getOperand(0).getReg();
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000520 afterRA = Register::isPhysicalRegister(predReg);
Sirish Pande4bd20c52012-05-12 05:10:30 +0000521
522 // If ifconverter had not messed up with the kill flags of the
523 // operands, the following check on the kill flag would suffice.
524 // if(!jmpInstr->getOperand(0).isKill()) break;
525
Hiroshi Inoue372ffa12018-04-13 11:37:06 +0000526 // This predicate register is live out of BB
Sirish Pande4bd20c52012-05-12 05:10:30 +0000527 // this would only work if we can actually use Live
528 // variable analysis on phy regs - but LLVM does not
529 // provide LV analysis on phys regs.
530 //if(LVs.isLiveOut(predReg, *MBB)) break;
531
532 // Get all the successors of this block - which will always
Krzysztof Parzyszek3d9946e2016-08-19 17:54:49 +0000533 // be 2. Check if the predicate register is live-in in those
Sirish Pande4bd20c52012-05-12 05:10:30 +0000534 // successor. If yes, we can not delete the predicate -
535 // I am doing this only because LLVM does not provide LiveOut
536 // at the BB level.
537 bool predLive = false;
538 for (MachineBasicBlock::const_succ_iterator SI = MBB->succ_begin(),
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000539 SIE = MBB->succ_end();
540 SI != SIE; ++SI) {
541 MachineBasicBlock *succMBB = *SI;
542 if (succMBB->isLiveIn(predReg))
Sirish Pande4bd20c52012-05-12 05:10:30 +0000543 predLive = true;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000544 }
545 if (predLive)
546 break;
547
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000548 if (!MI.getOperand(1).isMBB())
Krzysztof Parzyszekb28ae102016-01-14 15:05:27 +0000549 continue;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000550 jmpTarget = MI.getOperand(1).getMBB();
Sirish Pande4bd20c52012-05-12 05:10:30 +0000551 foundJump = true;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000552 if (MI.getOpcode() == Hexagon::J2_jumpf ||
553 MI.getOpcode() == Hexagon::J2_jumpfnewpt ||
554 MI.getOpcode() == Hexagon::J2_jumpfnew) {
Sirish Pande4bd20c52012-05-12 05:10:30 +0000555 invertPredicate = true;
556 }
557 continue;
558 }
559
560 // No new value jump if there is a barrier. A barrier has to be in its
561 // own packet. A barrier has zero operands. We conservatively bail out
562 // here if we see any instruction with zero operands.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000563 if (foundJump && MI.getNumOperands() == 0)
Sirish Pande4bd20c52012-05-12 05:10:30 +0000564 break;
565
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000566 if (foundJump && !foundCompare && MI.getOperand(0).isReg() &&
567 MI.getOperand(0).getReg() == predReg) {
Sirish Pande4bd20c52012-05-12 05:10:30 +0000568 // Not all compares can be new value compare. Arch Spec: 7.6.1.1
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000569 if (isNewValueJumpCandidate(MI)) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000570 assert(
571 (MI.getDesc().isCompare()) &&
Sirish Pande4bd20c52012-05-12 05:10:30 +0000572 "Only compare instruction can be collapsed into New Value Jump");
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000573 isSecondOpReg = MI.getOperand(2).isReg();
Sirish Pande4bd20c52012-05-12 05:10:30 +0000574
575 if (!canCompareBeNewValueJump(QII, QRI, MII, predReg, isSecondOpReg,
576 afterRA, jmpPos, MF))
577 break;
578
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000579 cmpInstr = &MI;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000580 cmpPos = MII;
581 foundCompare = true;
582
583 // We need cmpReg1 and cmpOp2(imm or reg) while building
584 // new value jump instruction.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000585 cmpReg1 = MI.getOperand(1).getReg();
Sirish Pande4bd20c52012-05-12 05:10:30 +0000586
Krzysztof Parzyszek5ddd2e52017-06-27 18:37:16 +0000587 if (isSecondOpReg)
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000588 cmpOp2 = MI.getOperand(2).getReg();
Krzysztof Parzyszek5ddd2e52017-06-27 18:37:16 +0000589 else
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000590 cmpOp2 = MI.getOperand(2).getImm();
Sirish Pande4bd20c52012-05-12 05:10:30 +0000591 continue;
592 }
593 }
594
595 if (foundCompare && foundJump) {
Sirish Pande4bd20c52012-05-12 05:10:30 +0000596 // If "common" checks fail, bail out on this BB.
597 if (!commonChecksToProhibitNewValueJump(afterRA, MII))
598 break;
599
600 bool foundFeeder = false;
601 MachineBasicBlock::iterator feederPos = MII;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000602 if (MI.getOperand(0).isReg() && MI.getOperand(0).isDef() &&
603 (MI.getOperand(0).getReg() == cmpReg1 ||
604 (isSecondOpReg &&
605 MI.getOperand(0).getReg() == (unsigned)cmpOp2))) {
Sirish Pande4bd20c52012-05-12 05:10:30 +0000606
Daniel Sanders0c476112019-08-15 19:22:08 +0000607 Register feederReg = MI.getOperand(0).getReg();
Sirish Pande4bd20c52012-05-12 05:10:30 +0000608
609 // First try to see if we can get the feeder from the first operand
610 // of the compare. If we can not, and if secondOpReg is true
611 // (second operand of the compare is also register), try that one.
612 // TODO: Try to come up with some heuristic to figure out which
613 // feeder would benefit.
614
615 if (feederReg == cmpReg1) {
616 if (!canBeFeederToNewValueJump(QII, QRI, MII, jmpPos, cmpPos, MF)) {
617 if (!isSecondOpReg)
618 break;
619 else
620 continue;
621 } else
622 foundFeeder = true;
623 }
624
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000625 if (!foundFeeder && isSecondOpReg && feederReg == (unsigned)cmpOp2)
Sirish Pande4bd20c52012-05-12 05:10:30 +0000626 if (!canBeFeederToNewValueJump(QII, QRI, MII, jmpPos, cmpPos, MF))
627 break;
628
629 if (isSecondOpReg) {
630 // In case of CMPLT, or CMPLTU, or EQ with the second register
631 // to newify, swap the operands.
Krzysztof Parzyszek3d9946e2016-08-19 17:54:49 +0000632 unsigned COp = cmpInstr->getOpcode();
633 if ((COp == Hexagon::C2_cmpeq || COp == Hexagon::C4_cmpneq) &&
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000634 (feederReg == (unsigned)cmpOp2)) {
Sirish Pande4bd20c52012-05-12 05:10:30 +0000635 unsigned tmp = cmpReg1;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000636 cmpReg1 = cmpOp2;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000637 cmpOp2 = tmp;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000638 }
639
640 // Now we have swapped the operands, all we need to check is,
641 // if the second operand (after swap) is the feeder.
642 // And if it is, make a note.
643 if (feederReg == (unsigned)cmpOp2)
644 isSecondOpNewified = true;
645 }
646
647 // Now that we are moving feeder close the jump,
648 // make sure we are respecting the kill values of
649 // the operands of the feeder.
650
Krzysztof Parzyszek5ddd2e52017-06-27 18:37:16 +0000651 auto TransferKills = [jmpPos,cmpPos] (MachineInstr &MI) {
652 for (MachineOperand &MO : MI.operands()) {
653 if (!MO.isReg() || !MO.isUse())
654 continue;
Daniel Sanders0c476112019-08-15 19:22:08 +0000655 Register UseR = MO.getReg();
Krzysztof Parzyszek5ddd2e52017-06-27 18:37:16 +0000656 for (auto I = std::next(MI.getIterator()); I != jmpPos; ++I) {
657 if (I == cmpPos)
658 continue;
659 for (MachineOperand &Op : I->operands()) {
660 if (!Op.isReg() || !Op.isUse() || !Op.isKill())
661 continue;
662 if (Op.getReg() != UseR)
663 continue;
664 // We found that there is kill of a use register
665 // Set up a kill flag on the register
666 Op.setIsKill(false);
667 MO.setIsKill(true);
668 return;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000669 }
Sirish Pande4bd20c52012-05-12 05:10:30 +0000670 }
671 }
Krzysztof Parzyszek5ddd2e52017-06-27 18:37:16 +0000672 };
673
674 TransferKills(*feederPos);
675 TransferKills(*cmpPos);
676 bool MO1IsKill = cmpPos->killsRegister(cmpReg1, QRI);
677 bool MO2IsKill = isSecondOpReg && cmpPos->killsRegister(cmpOp2, QRI);
Sirish Pande4bd20c52012-05-12 05:10:30 +0000678
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000679 MBB->splice(jmpPos, MI.getParent(), MI);
680 MBB->splice(jmpPos, MI.getParent(), cmpInstr);
681 DebugLoc dl = MI.getDebugLoc();
Sirish Pande4bd20c52012-05-12 05:10:30 +0000682 MachineInstr *NewMI;
683
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000684 assert((isNewValueJumpCandidate(*cmpInstr)) &&
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000685 "This compare is not a New Value Jump candidate.");
Sirish Pande4bd20c52012-05-12 05:10:30 +0000686 unsigned opc = getNewValueJumpOpcode(cmpInstr, cmpOp2,
Jyotsna Verma1d297502013-05-02 15:39:30 +0000687 isSecondOpNewified,
688 jmpTarget, MBPI);
Sirish Pande4bd20c52012-05-12 05:10:30 +0000689 if (invertPredicate)
690 opc = QII->getInvertedPredicatedOpcode(opc);
691
Jyotsna Verma89c84822013-04-23 19:15:55 +0000692 if (isSecondOpReg)
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000693 NewMI = BuildMI(*MBB, jmpPos, dl, QII->get(opc))
694 .addReg(cmpReg1, getKillRegState(MO1IsKill))
695 .addReg(cmpOp2, getKillRegState(MO2IsKill))
696 .addMBB(jmpTarget);
Jyotsna Verma89c84822013-04-23 19:15:55 +0000697
Jyotsna Verma89c84822013-04-23 19:15:55 +0000698 else
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000699 NewMI = BuildMI(*MBB, jmpPos, dl, QII->get(opc))
700 .addReg(cmpReg1, getKillRegState(MO1IsKill))
701 .addImm(cmpOp2)
702 .addMBB(jmpTarget);
Sirish Pande4bd20c52012-05-12 05:10:30 +0000703
704 assert(NewMI && "New Value Jump Instruction Not created!");
Duncan Sands0480b9b2013-05-13 07:50:47 +0000705 (void)NewMI;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000706 if (cmpInstr->getOperand(0).isReg() &&
707 cmpInstr->getOperand(0).isKill())
708 cmpInstr->getOperand(0).setIsKill(false);
709 if (cmpInstr->getOperand(1).isReg() &&
710 cmpInstr->getOperand(1).isKill())
711 cmpInstr->getOperand(1).setIsKill(false);
712 cmpInstr->eraseFromParent();
713 jmpInstr->eraseFromParent();
714 ++nvjGenerated;
715 ++NumNVJGenerated;
716 break;
717 }
718 }
719 }
720 }
721
722 return true;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000723}
724
725FunctionPass *llvm::createHexagonNewValueJump() {
726 return new HexagonNewValueJump();
727}