blob: 680d01e12af02d8192020e8de042b366bbabc8b6 [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
Jyotsna Verma84c47102013-05-06 18:49:23 +000023#include "Hexagon.h"
Jyotsna Verma84c47102013-05-06 18:49:23 +000024#include "HexagonInstrInfo.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000025#include "HexagonRegisterInfo.h"
Krzysztof Parzyszek5d41cc12018-03-12 17:47:46 +000026#include "HexagonSubtarget.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000027#include "llvm/ADT/Statistic.h"
Eugene Zelenko3b873362017-09-28 22:27:31 +000028#include "llvm/CodeGen/MachineBasicBlock.h"
29#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
30#include "llvm/CodeGen/MachineFunction.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000031#include "llvm/CodeGen/MachineFunctionPass.h"
Eugene Zelenko3b873362017-09-28 22:27:31 +000032#include "llvm/CodeGen/MachineInstr.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000033#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenko3b873362017-09-28 22:27:31 +000034#include "llvm/CodeGen/MachineOperand.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000035#include "llvm/CodeGen/MachineRegisterInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000036#include "llvm/CodeGen/TargetOpcodes.h"
37#include "llvm/CodeGen/TargetRegisterInfo.h"
38#include "llvm/CodeGen/TargetSubtargetInfo.h"
Eugene Zelenko3b873362017-09-28 22:27:31 +000039#include "llvm/IR/DebugLoc.h"
40#include "llvm/MC/MCInstrDesc.h"
41#include "llvm/Pass.h"
42#include "llvm/Support/BranchProbability.h"
Jyotsna Verma84c47102013-05-06 18:49:23 +000043#include "llvm/Support/CommandLine.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000044#include "llvm/Support/Debug.h"
Eugene Zelenko3b873362017-09-28 22:27:31 +000045#include "llvm/Support/ErrorHandling.h"
46#include "llvm/Support/MathExtras.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000047#include "llvm/Support/raw_ostream.h"
Eugene Zelenko3b873362017-09-28 22:27:31 +000048#include <cassert>
49#include <cstdint>
50#include <iterator>
51
Sirish Pande4bd20c52012-05-12 05:10:30 +000052using namespace llvm;
53
Chandler Carruth84e68b22014-04-22 02:41:26 +000054#define DEBUG_TYPE "hexagon-nvj"
55
Sirish Pande4bd20c52012-05-12 05:10:30 +000056STATISTIC(NumNVJGenerated, "Number of New Value Jump Instructions created");
57
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +000058static cl::opt<int> DbgNVJCount("nvj-count", cl::init(-1), cl::Hidden,
59 cl::desc("Maximum number of predicated jumps to be converted to "
60 "New Value Jump"));
Sirish Pande4bd20c52012-05-12 05:10:30 +000061
62static cl::opt<bool> DisableNewValueJumps("disable-nvjump", cl::Hidden,
63 cl::ZeroOrMore, cl::init(false),
64 cl::desc("Disable New Value Jumps"));
65
Jyotsna Verma84c47102013-05-06 18:49:23 +000066namespace llvm {
Jyotsna Verma84c47102013-05-06 18:49:23 +000067
Eugene Zelenko3b873362017-09-28 22:27:31 +000068FunctionPass *createHexagonNewValueJump();
69void initializeHexagonNewValueJumpPass(PassRegistry&);
70
71} // end namespace llvm
Jyotsna Verma84c47102013-05-06 18:49:23 +000072
Sirish Pande4bd20c52012-05-12 05:10:30 +000073namespace {
Eugene Zelenko3b873362017-09-28 22:27:31 +000074
Sirish Pande4bd20c52012-05-12 05:10:30 +000075 struct HexagonNewValueJump : public MachineFunctionPass {
Sirish Pande4bd20c52012-05-12 05:10:30 +000076 static char ID;
77
Krzysztof Parzyszek5ddd2e52017-06-27 18:37:16 +000078 HexagonNewValueJump() : MachineFunctionPass(ID) {}
Sirish Pande4bd20c52012-05-12 05:10:30 +000079
Craig Topper906c2cd2014-04-29 07:58:16 +000080 void getAnalysisUsage(AnalysisUsage &AU) const override {
Jyotsna Verma1d297502013-05-02 15:39:30 +000081 AU.addRequired<MachineBranchProbabilityInfo>();
Sirish Pande4bd20c52012-05-12 05:10:30 +000082 MachineFunctionPass::getAnalysisUsage(AU);
83 }
84
Mehdi Amini117296c2016-10-01 02:56:57 +000085 StringRef getPassName() const override { return "Hexagon NewValueJump"; }
Sirish Pande4bd20c52012-05-12 05:10:30 +000086
Craig Topper906c2cd2014-04-29 07:58:16 +000087 bool runOnMachineFunction(MachineFunction &Fn) override;
Eugene Zelenko3b873362017-09-28 22:27:31 +000088
Derek Schuff1dbf7a52016-04-04 17:09:25 +000089 MachineFunctionProperties getRequiredProperties() const override {
90 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +000091 MachineFunctionProperties::Property::NoVRegs);
Derek Schuff1dbf7a52016-04-04 17:09:25 +000092 }
Sirish Pande4bd20c52012-05-12 05:10:30 +000093
94 private:
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +000095 const HexagonInstrInfo *QII;
96 const HexagonRegisterInfo *QRI;
97
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000098 /// A handle to the branch probability pass.
Jyotsna Verma1d297502013-05-02 15:39:30 +000099 const MachineBranchProbabilityInfo *MBPI;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000100
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000101 bool isNewValueJumpCandidate(const MachineInstr &MI) const;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000102 };
103
Eugene Zelenko3b873362017-09-28 22:27:31 +0000104} // end anonymous namespace
Sirish Pande4bd20c52012-05-12 05:10:30 +0000105
106char HexagonNewValueJump::ID = 0;
107
Jyotsna Verma84c47102013-05-06 18:49:23 +0000108INITIALIZE_PASS_BEGIN(HexagonNewValueJump, "hexagon-nvj",
109 "Hexagon NewValueJump", false, false)
110INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
111INITIALIZE_PASS_END(HexagonNewValueJump, "hexagon-nvj",
112 "Hexagon NewValueJump", false, false)
113
Sirish Pande4bd20c52012-05-12 05:10:30 +0000114// We have identified this II could be feeder to NVJ,
115// verify that it can be.
116static bool canBeFeederToNewValueJump(const HexagonInstrInfo *QII,
117 const TargetRegisterInfo *TRI,
118 MachineBasicBlock::iterator II,
119 MachineBasicBlock::iterator end,
120 MachineBasicBlock::iterator skip,
121 MachineFunction &MF) {
Sirish Pande4bd20c52012-05-12 05:10:30 +0000122 // Predicated instruction can not be feeder to NVJ.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000123 if (QII->isPredicated(*II))
Sirish Pande4bd20c52012-05-12 05:10:30 +0000124 return false;
125
126 // Bail out if feederReg is a paired register (double regs in
127 // our case). One would think that we can check to see if a given
128 // register cmpReg1 or cmpReg2 is a sub register of feederReg
129 // using -- if (QRI->isSubRegister(feederReg, cmpReg1) logic
130 // before the callsite of this function
131 // But we can not as it comes in the following fashion.
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000132 // %d0 = Hexagon_S2_lsr_r_p killed %d0, killed %r2
133 // %r0 = KILL %r0, implicit killed %d0
134 // %p0 = CMPEQri killed %r0, 0
Sirish Pande4bd20c52012-05-12 05:10:30 +0000135 // Hence, we need to check if it's a KILL instruction.
136 if (II->getOpcode() == TargetOpcode::KILL)
137 return false;
138
Krzysztof Parzyszek2cfc7a42017-02-23 17:47:34 +0000139 if (II->isImplicitDef())
140 return false;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000141
Krzysztof Parzyszek44555222017-11-30 20:32:54 +0000142 if (QII->isSolo(*II))
143 return false;
144
Krzysztof Parzyszekbe253e72018-02-06 19:08:41 +0000145 if (QII->isFloat(*II))
146 return false;
147
148 // Make sure that the (unique) def operand is a register from IntRegs.
149 bool HadDef = false;
150 for (const MachineOperand &Op : II->operands()) {
151 if (!Op.isReg() || !Op.isDef())
152 continue;
153 if (HadDef)
154 return false;
155 HadDef = true;
156 if (!Hexagon::IntRegsRegClass.contains(Op.getReg()))
157 return false;
158 }
159 assert(HadDef);
160
Fangrui Song956ee792018-03-30 22:22:31 +0000161 // Make sure there is no 'def' or 'use' of any of the uses of
Eric Christopher563d0b92018-05-21 10:27:36 +0000162 // feeder insn between its definition, this MI and jump, jmpInst
Sirish Pande4bd20c52012-05-12 05:10:30 +0000163 // skipping compare, cmpInst.
164 // Here's the example.
165 // r21=memub(r22+r24<<#0)
166 // p0 = cmp.eq(r21, #0)
167 // r4=memub(r3+r21<<#0)
168 // if (p0.new) jump:t .LBB29_45
169 // Without this check, it will be converted into
170 // r4=memub(r3+r21<<#0)
171 // r21=memub(r22+r24<<#0)
172 // p0 = cmp.eq(r21, #0)
173 // if (p0.new) jump:t .LBB29_45
174 // and result WAR hazards if converted to New Value Jump.
Sirish Pande4bd20c52012-05-12 05:10:30 +0000175 for (unsigned i = 0; i < II->getNumOperands(); ++i) {
176 if (II->getOperand(i).isReg() &&
177 (II->getOperand(i).isUse() || II->getOperand(i).isDef())) {
178 MachineBasicBlock::iterator localII = II;
179 ++localII;
Daniel Sanders0c476112019-08-15 19:22:08 +0000180 Register Reg = II->getOperand(i).getReg();
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000181 for (MachineBasicBlock::iterator localBegin = localII; localBegin != end;
182 ++localBegin) {
183 if (localBegin == skip)
184 continue;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000185 // Check for Subregisters too.
186 if (localBegin->modifiesRegister(Reg, TRI) ||
187 localBegin->readsRegister(Reg, TRI))
188 return false;
189 }
190 }
191 }
192 return true;
193}
194
195// These are the common checks that need to performed
196// to determine if
197// 1. compare instruction can be moved before jump.
198// 2. feeder to the compare instruction can be moved before jump.
199static bool commonChecksToProhibitNewValueJump(bool afterRA,
200 MachineBasicBlock::iterator MII) {
Sirish Pande4bd20c52012-05-12 05:10:30 +0000201 // If store in path, bail out.
Krzysztof Parzyszek1fd0c7e2017-07-24 19:35:48 +0000202 if (MII->mayStore())
Sirish Pande4bd20c52012-05-12 05:10:30 +0000203 return false;
204
205 // if call in path, bail out.
Krzysztof Parzyszek3d9946e2016-08-19 17:54:49 +0000206 if (MII->isCall())
Sirish Pande4bd20c52012-05-12 05:10:30 +0000207 return false;
208
209 // if NVJ is running prior to RA, do the following checks.
210 if (!afterRA) {
211 // The following Target Opcode instructions are spurious
212 // to new value jump. If they are in the path, bail out.
213 // KILL sets kill flag on the opcode. It also sets up a
214 // single register, out of pair.
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000215 // %d0 = S2_lsr_r_p killed %d0, killed %r2
216 // %r0 = KILL %r0, implicit killed %d0
217 // %p0 = C2_cmpeqi killed %r0, 0
Sirish Pande4bd20c52012-05-12 05:10:30 +0000218 // PHI can be anything after RA.
219 // COPY can remateriaze things in between feeder, compare and nvj.
220 if (MII->getOpcode() == TargetOpcode::KILL ||
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000221 MII->getOpcode() == TargetOpcode::PHI ||
Sirish Pande4bd20c52012-05-12 05:10:30 +0000222 MII->getOpcode() == TargetOpcode::COPY)
223 return false;
224
225 // The following pseudo Hexagon instructions sets "use" and "def"
226 // of registers by individual passes in the backend. At this time,
227 // we don't know the scope of usage and definitions of these
228 // instructions.
Krzysztof Parzyszek3d9946e2016-08-19 17:54:49 +0000229 if (MII->getOpcode() == Hexagon::LDriw_pred ||
Sirish Pande4bd20c52012-05-12 05:10:30 +0000230 MII->getOpcode() == Hexagon::STriw_pred)
231 return false;
232 }
233
234 return true;
235}
236
237static bool canCompareBeNewValueJump(const HexagonInstrInfo *QII,
238 const TargetRegisterInfo *TRI,
239 MachineBasicBlock::iterator II,
240 unsigned pReg,
241 bool secondReg,
242 bool optLocation,
243 MachineBasicBlock::iterator end,
244 MachineFunction &MF) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000245 MachineInstr &MI = *II;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000246
247 // If the second operand of the compare is an imm, make sure it's in the
248 // range specified by the arch.
249 if (!secondReg) {
Krzysztof Parzyszek64e5d7d2017-10-20 19:33:12 +0000250 const MachineOperand &Op2 = MI.getOperand(2);
251 if (!Op2.isImm())
252 return false;
253
254 int64_t v = Op2.getImm();
Krzysztof Parzyszek3d9946e2016-08-19 17:54:49 +0000255 bool Valid = false;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000256
Krzysztof Parzyszek3d9946e2016-08-19 17:54:49 +0000257 switch (MI.getOpcode()) {
258 case Hexagon::C2_cmpeqi:
Krzysztof Parzyszek1fd0c7e2017-07-24 19:35:48 +0000259 case Hexagon::C4_cmpneqi:
Krzysztof Parzyszek3d9946e2016-08-19 17:54:49 +0000260 case Hexagon::C2_cmpgti:
Krzysztof Parzyszek1fd0c7e2017-07-24 19:35:48 +0000261 case Hexagon::C4_cmpltei:
Krzysztof Parzyszek3d9946e2016-08-19 17:54:49 +0000262 Valid = (isUInt<5>(v) || v == -1);
263 break;
264 case Hexagon::C2_cmpgtui:
Krzysztof Parzyszek1fd0c7e2017-07-24 19:35:48 +0000265 case Hexagon::C4_cmplteui:
Krzysztof Parzyszek3d9946e2016-08-19 17:54:49 +0000266 Valid = isUInt<5>(v);
267 break;
268 case Hexagon::S2_tstbit_i:
269 case Hexagon::S4_ntstbit_i:
270 Valid = (v == 0);
271 break;
272 }
273
274 if (!Valid)
Sirish Pande4bd20c52012-05-12 05:10:30 +0000275 return false;
276 }
277
Jyotsna Verma84c47102013-05-06 18:49:23 +0000278 unsigned cmpReg1, cmpOp2 = 0; // cmpOp2 assignment silences compiler warning.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000279 cmpReg1 = MI.getOperand(1).getReg();
Sirish Pande4bd20c52012-05-12 05:10:30 +0000280
281 if (secondReg) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000282 cmpOp2 = MI.getOperand(2).getReg();
Sirish Pande4bd20c52012-05-12 05:10:30 +0000283
Krzysztof Parzyszek3d9946e2016-08-19 17:54:49 +0000284 // If the same register appears as both operands, we cannot generate a new
285 // value compare. Only one operand may use the .new suffix.
286 if (cmpReg1 == cmpOp2)
287 return false;
288
Fangrui Song956ee792018-03-30 22:22:31 +0000289 // Make sure that the second register is not from COPY
290 // at machine code level, we don't need this, but if we decide
Sirish Pande4bd20c52012-05-12 05:10:30 +0000291 // to move new value jump prior to RA, we would be needing this.
292 MachineRegisterInfo &MRI = MF.getRegInfo();
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000293 if (secondReg && !Register::isPhysicalRegister(cmpOp2)) {
Sirish Pande4bd20c52012-05-12 05:10:30 +0000294 MachineInstr *def = MRI.getVRegDef(cmpOp2);
295 if (def->getOpcode() == TargetOpcode::COPY)
296 return false;
297 }
298 }
299
300 // Walk the instructions after the compare (predicate def) to the jump,
301 // and satisfy the following conditions.
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000302 ++II;
303 for (MachineBasicBlock::iterator localII = II; localII != end; ++localII) {
Shiva Chen801bf7e2018-05-09 02:42:00 +0000304 if (localII->isDebugInstr())
Krzysztof Parzyszek3d9946e2016-08-19 17:54:49 +0000305 continue;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000306
307 // Check 1.
308 // If "common" checks fail, bail out.
309 if (!commonChecksToProhibitNewValueJump(optLocation, localII))
310 return false;
311
312 // Check 2.
313 // If there is a def or use of predicate (result of compare), bail out.
314 if (localII->modifiesRegister(pReg, TRI) ||
315 localII->readsRegister(pReg, TRI))
316 return false;
317
318 // Check 3.
319 // If there is a def of any of the use of the compare (operands of compare),
320 // bail out.
321 // Eg.
322 // p0 = cmp.eq(r2, r0)
323 // r2 = r4
324 // if (p0.new) jump:t .LBB28_3
325 if (localII->modifiesRegister(cmpReg1, TRI) ||
326 (secondReg && localII->modifiesRegister(cmpOp2, TRI)))
327 return false;
328 }
329 return true;
330}
331
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000332// Given a compare operator, return a matching New Value Jump compare operator.
333// Make sure that MI here is included in isNewValueJumpCandidate.
Jyotsna Verma1d297502013-05-02 15:39:30 +0000334static unsigned getNewValueJumpOpcode(MachineInstr *MI, int reg,
335 bool secondRegNewified,
336 MachineBasicBlock *jmpTarget,
337 const MachineBranchProbabilityInfo
338 *MBPI) {
339 bool taken = false;
340 MachineBasicBlock *Src = MI->getParent();
341 const BranchProbability Prediction =
342 MBPI->getEdgeProbability(Src, jmpTarget);
343
344 if (Prediction >= BranchProbability(1,2))
345 taken = true;
346
Sirish Pande4bd20c52012-05-12 05:10:30 +0000347 switch (MI->getOpcode()) {
Colin LeMahieu902157c2014-11-25 18:20:52 +0000348 case Hexagon::C2_cmpeq:
Colin LeMahieu6e3e62f2015-02-05 22:03:32 +0000349 return taken ? Hexagon::J4_cmpeq_t_jumpnv_t
350 : Hexagon::J4_cmpeq_t_jumpnv_nt;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000351
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000352 case Hexagon::C2_cmpeqi:
Sirish Pande4bd20c52012-05-12 05:10:30 +0000353 if (reg >= 0)
Colin LeMahieu6e3e62f2015-02-05 22:03:32 +0000354 return taken ? Hexagon::J4_cmpeqi_t_jumpnv_t
355 : Hexagon::J4_cmpeqi_t_jumpnv_nt;
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000356 return taken ? Hexagon::J4_cmpeqn1_t_jumpnv_t
357 : Hexagon::J4_cmpeqn1_t_jumpnv_nt;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000358
Krzysztof Parzyszek1fd0c7e2017-07-24 19:35:48 +0000359 case Hexagon::C4_cmpneqi:
360 if (reg >= 0)
361 return taken ? Hexagon::J4_cmpeqi_f_jumpnv_t
362 : Hexagon::J4_cmpeqi_f_jumpnv_nt;
363 return taken ? Hexagon::J4_cmpeqn1_f_jumpnv_t :
364 Hexagon::J4_cmpeqn1_f_jumpnv_nt;
365
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000366 case Hexagon::C2_cmpgt:
Sirish Pande4bd20c52012-05-12 05:10:30 +0000367 if (secondRegNewified)
Colin LeMahieu6e3e62f2015-02-05 22:03:32 +0000368 return taken ? Hexagon::J4_cmplt_t_jumpnv_t
369 : Hexagon::J4_cmplt_t_jumpnv_nt;
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000370 return taken ? Hexagon::J4_cmpgt_t_jumpnv_t
371 : Hexagon::J4_cmpgt_t_jumpnv_nt;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000372
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000373 case Hexagon::C2_cmpgti:
Sirish Pande4bd20c52012-05-12 05:10:30 +0000374 if (reg >= 0)
Colin LeMahieu6e3e62f2015-02-05 22:03:32 +0000375 return taken ? Hexagon::J4_cmpgti_t_jumpnv_t
376 : Hexagon::J4_cmpgti_t_jumpnv_nt;
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000377 return taken ? Hexagon::J4_cmpgtn1_t_jumpnv_t
378 : Hexagon::J4_cmpgtn1_t_jumpnv_nt;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000379
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000380 case Hexagon::C2_cmpgtu:
Sirish Pande4bd20c52012-05-12 05:10:30 +0000381 if (secondRegNewified)
Colin LeMahieu6e3e62f2015-02-05 22:03:32 +0000382 return taken ? Hexagon::J4_cmpltu_t_jumpnv_t
383 : Hexagon::J4_cmpltu_t_jumpnv_nt;
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000384 return taken ? Hexagon::J4_cmpgtu_t_jumpnv_t
385 : Hexagon::J4_cmpgtu_t_jumpnv_nt;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000386
Colin LeMahieu6e0f9f82014-11-26 19:43:12 +0000387 case Hexagon::C2_cmpgtui:
Colin LeMahieu6e3e62f2015-02-05 22:03:32 +0000388 return taken ? Hexagon::J4_cmpgtui_t_jumpnv_t
389 : Hexagon::J4_cmpgtui_t_jumpnv_nt;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000390
Ron Liebermane6540e22015-12-08 16:28:32 +0000391 case Hexagon::C4_cmpneq:
392 return taken ? Hexagon::J4_cmpeq_f_jumpnv_t
393 : Hexagon::J4_cmpeq_f_jumpnv_nt;
394
395 case Hexagon::C4_cmplte:
396 if (secondRegNewified)
397 return taken ? Hexagon::J4_cmplt_f_jumpnv_t
398 : Hexagon::J4_cmplt_f_jumpnv_nt;
399 return taken ? Hexagon::J4_cmpgt_f_jumpnv_t
400 : Hexagon::J4_cmpgt_f_jumpnv_nt;
401
402 case Hexagon::C4_cmplteu:
403 if (secondRegNewified)
404 return taken ? Hexagon::J4_cmpltu_f_jumpnv_t
405 : Hexagon::J4_cmpltu_f_jumpnv_nt;
406 return taken ? Hexagon::J4_cmpgtu_f_jumpnv_t
407 : Hexagon::J4_cmpgtu_f_jumpnv_nt;
408
Krzysztof Parzyszek1fd0c7e2017-07-24 19:35:48 +0000409 case Hexagon::C4_cmpltei:
410 if (reg >= 0)
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000411 return taken ? Hexagon::J4_cmpgti_f_jumpnv_t
412 : Hexagon::J4_cmpgti_f_jumpnv_nt;
413 return taken ? Hexagon::J4_cmpgtn1_f_jumpnv_t
414 : Hexagon::J4_cmpgtn1_f_jumpnv_nt;
Krzysztof Parzyszek1fd0c7e2017-07-24 19:35:48 +0000415
416 case Hexagon::C4_cmplteui:
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000417 return taken ? Hexagon::J4_cmpgtui_f_jumpnv_t
418 : Hexagon::J4_cmpgtui_f_jumpnv_nt;
Krzysztof Parzyszek1fd0c7e2017-07-24 19:35:48 +0000419
Sirish Pande4bd20c52012-05-12 05:10:30 +0000420 default:
421 llvm_unreachable("Could not find matching New Value Jump instruction.");
422 }
423 // return *some value* to avoid compiler warning
424 return 0;
425}
426
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000427bool HexagonNewValueJump::isNewValueJumpCandidate(
428 const MachineInstr &MI) const {
429 switch (MI.getOpcode()) {
430 case Hexagon::C2_cmpeq:
431 case Hexagon::C2_cmpeqi:
432 case Hexagon::C2_cmpgt:
433 case Hexagon::C2_cmpgti:
434 case Hexagon::C2_cmpgtu:
435 case Hexagon::C2_cmpgtui:
436 case Hexagon::C4_cmpneq:
Krzysztof Parzyszek1fd0c7e2017-07-24 19:35:48 +0000437 case Hexagon::C4_cmpneqi:
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000438 case Hexagon::C4_cmplte:
439 case Hexagon::C4_cmplteu:
Krzysztof Parzyszek1fd0c7e2017-07-24 19:35:48 +0000440 case Hexagon::C4_cmpltei:
441 case Hexagon::C4_cmplteui:
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000442 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000443
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000444 default:
445 return false;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000446 }
447}
448
Sirish Pande4bd20c52012-05-12 05:10:30 +0000449bool HexagonNewValueJump::runOnMachineFunction(MachineFunction &MF) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000450 LLVM_DEBUG(dbgs() << "********** Hexagon New Value Jump **********\n"
451 << "********** Function: " << MF.getName() << "\n");
Sirish Pande4bd20c52012-05-12 05:10:30 +0000452
Matthias Braunf1caa282017-12-15 22:22:58 +0000453 if (skipFunction(MF.getFunction()))
Andrew Kaylor5b444a22016-04-26 19:46:28 +0000454 return false;
455
Eric Christopher0fef34e2015-02-02 22:11:42 +0000456 // If we move NewValueJump before register allocation we'll need live variable
457 // analysis here too.
Sirish Pande4bd20c52012-05-12 05:10:30 +0000458
Eric Christopherfc6de422014-08-05 02:39:49 +0000459 QII = static_cast<const HexagonInstrInfo *>(MF.getSubtarget().getInstrInfo());
Eric Christopherd9134482014-08-04 21:25:23 +0000460 QRI = static_cast<const HexagonRegisterInfo *>(
Eric Christopherfc6de422014-08-05 02:39:49 +0000461 MF.getSubtarget().getRegisterInfo());
Jyotsna Verma1d297502013-05-02 15:39:30 +0000462 MBPI = &getAnalysis<MachineBranchProbabilityInfo>();
Sirish Pande4bd20c52012-05-12 05:10:30 +0000463
Krzysztof Parzyszek5d41cc12018-03-12 17:47:46 +0000464 if (DisableNewValueJumps ||
465 !MF.getSubtarget<HexagonSubtarget>().useNewValueJumps())
Sirish Pande4bd20c52012-05-12 05:10:30 +0000466 return false;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000467
468 int nvjCount = DbgNVJCount;
469 int nvjGenerated = 0;
470
471 // Loop through all the bb's of the function
472 for (MachineFunction::iterator MBBb = MF.begin(), MBBe = MF.end();
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000473 MBBb != MBBe; ++MBBb) {
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000474 MachineBasicBlock *MBB = &*MBBb;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000475
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000476 LLVM_DEBUG(dbgs() << "** dumping bb ** " << MBB->getNumber() << "\n");
477 LLVM_DEBUG(MBB->dump());
478 LLVM_DEBUG(dbgs() << "\n"
479 << "********** dumping instr bottom up **********\n");
Sirish Pande4bd20c52012-05-12 05:10:30 +0000480 bool foundJump = false;
481 bool foundCompare = false;
482 bool invertPredicate = false;
483 unsigned predReg = 0; // predicate reg of the jump.
484 unsigned cmpReg1 = 0;
485 int cmpOp2 = 0;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000486 MachineBasicBlock::iterator jmpPos;
487 MachineBasicBlock::iterator cmpPos;
Craig Topper062a2ba2014-04-25 05:30:21 +0000488 MachineInstr *cmpInstr = nullptr, *jmpInstr = nullptr;
489 MachineBasicBlock *jmpTarget = nullptr;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000490 bool afterRA = false;
491 bool isSecondOpReg = false;
492 bool isSecondOpNewified = false;
493 // Traverse the basic block - bottom up
494 for (MachineBasicBlock::iterator MII = MBB->end(), E = MBB->begin();
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000495 MII != E;) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000496 MachineInstr &MI = *--MII;
Shiva Chen801bf7e2018-05-09 02:42:00 +0000497 if (MI.isDebugInstr()) {
Sirish Pande4bd20c52012-05-12 05:10:30 +0000498 continue;
499 }
500
501 if ((nvjCount == 0) || (nvjCount > -1 && nvjCount <= nvjGenerated))
502 break;
503
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000504 LLVM_DEBUG(dbgs() << "Instr: "; MI.dump(); dbgs() << "\n");
Sirish Pande4bd20c52012-05-12 05:10:30 +0000505
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000506 if (!foundJump && (MI.getOpcode() == Hexagon::J2_jumpt ||
Krzysztof Parzyszeka243adf2016-08-19 14:14:09 +0000507 MI.getOpcode() == Hexagon::J2_jumptpt ||
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000508 MI.getOpcode() == Hexagon::J2_jumpf ||
Krzysztof Parzyszeka243adf2016-08-19 14:14:09 +0000509 MI.getOpcode() == Hexagon::J2_jumpfpt ||
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000510 MI.getOpcode() == Hexagon::J2_jumptnewpt ||
511 MI.getOpcode() == Hexagon::J2_jumptnew ||
512 MI.getOpcode() == Hexagon::J2_jumpfnewpt ||
513 MI.getOpcode() == Hexagon::J2_jumpfnew)) {
Sirish Pande4bd20c52012-05-12 05:10:30 +0000514 // This is where you would insert your compare and
515 // instr that feeds compare
516 jmpPos = MII;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000517 jmpInstr = &MI;
518 predReg = MI.getOperand(0).getReg();
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000519 afterRA = Register::isPhysicalRegister(predReg);
Sirish Pande4bd20c52012-05-12 05:10:30 +0000520
521 // If ifconverter had not messed up with the kill flags of the
522 // operands, the following check on the kill flag would suffice.
523 // if(!jmpInstr->getOperand(0).isKill()) break;
524
Hiroshi Inoue372ffa12018-04-13 11:37:06 +0000525 // This predicate register is live out of BB
Sirish Pande4bd20c52012-05-12 05:10:30 +0000526 // this would only work if we can actually use Live
527 // variable analysis on phy regs - but LLVM does not
528 // provide LV analysis on phys regs.
529 //if(LVs.isLiveOut(predReg, *MBB)) break;
530
531 // Get all the successors of this block - which will always
Krzysztof Parzyszek3d9946e2016-08-19 17:54:49 +0000532 // be 2. Check if the predicate register is live-in in those
Sirish Pande4bd20c52012-05-12 05:10:30 +0000533 // successor. If yes, we can not delete the predicate -
534 // I am doing this only because LLVM does not provide LiveOut
535 // at the BB level.
536 bool predLive = false;
537 for (MachineBasicBlock::const_succ_iterator SI = MBB->succ_begin(),
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000538 SIE = MBB->succ_end();
539 SI != SIE; ++SI) {
540 MachineBasicBlock *succMBB = *SI;
541 if (succMBB->isLiveIn(predReg))
Sirish Pande4bd20c52012-05-12 05:10:30 +0000542 predLive = true;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000543 }
544 if (predLive)
545 break;
546
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000547 if (!MI.getOperand(1).isMBB())
Krzysztof Parzyszekb28ae102016-01-14 15:05:27 +0000548 continue;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000549 jmpTarget = MI.getOperand(1).getMBB();
Sirish Pande4bd20c52012-05-12 05:10:30 +0000550 foundJump = true;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000551 if (MI.getOpcode() == Hexagon::J2_jumpf ||
552 MI.getOpcode() == Hexagon::J2_jumpfnewpt ||
553 MI.getOpcode() == Hexagon::J2_jumpfnew) {
Sirish Pande4bd20c52012-05-12 05:10:30 +0000554 invertPredicate = true;
555 }
556 continue;
557 }
558
559 // No new value jump if there is a barrier. A barrier has to be in its
560 // own packet. A barrier has zero operands. We conservatively bail out
561 // here if we see any instruction with zero operands.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000562 if (foundJump && MI.getNumOperands() == 0)
Sirish Pande4bd20c52012-05-12 05:10:30 +0000563 break;
564
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000565 if (foundJump && !foundCompare && MI.getOperand(0).isReg() &&
566 MI.getOperand(0).getReg() == predReg) {
Sirish Pande4bd20c52012-05-12 05:10:30 +0000567 // Not all compares can be new value compare. Arch Spec: 7.6.1.1
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000568 if (isNewValueJumpCandidate(MI)) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000569 assert(
570 (MI.getDesc().isCompare()) &&
Sirish Pande4bd20c52012-05-12 05:10:30 +0000571 "Only compare instruction can be collapsed into New Value Jump");
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000572 isSecondOpReg = MI.getOperand(2).isReg();
Sirish Pande4bd20c52012-05-12 05:10:30 +0000573
574 if (!canCompareBeNewValueJump(QII, QRI, MII, predReg, isSecondOpReg,
575 afterRA, jmpPos, MF))
576 break;
577
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000578 cmpInstr = &MI;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000579 cmpPos = MII;
580 foundCompare = true;
581
582 // We need cmpReg1 and cmpOp2(imm or reg) while building
583 // new value jump instruction.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000584 cmpReg1 = MI.getOperand(1).getReg();
Sirish Pande4bd20c52012-05-12 05:10:30 +0000585
Krzysztof Parzyszek5ddd2e52017-06-27 18:37:16 +0000586 if (isSecondOpReg)
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000587 cmpOp2 = MI.getOperand(2).getReg();
Krzysztof Parzyszek5ddd2e52017-06-27 18:37:16 +0000588 else
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000589 cmpOp2 = MI.getOperand(2).getImm();
Sirish Pande4bd20c52012-05-12 05:10:30 +0000590 continue;
591 }
592 }
593
594 if (foundCompare && foundJump) {
Sirish Pande4bd20c52012-05-12 05:10:30 +0000595 // If "common" checks fail, bail out on this BB.
596 if (!commonChecksToProhibitNewValueJump(afterRA, MII))
597 break;
598
599 bool foundFeeder = false;
600 MachineBasicBlock::iterator feederPos = MII;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000601 if (MI.getOperand(0).isReg() && MI.getOperand(0).isDef() &&
602 (MI.getOperand(0).getReg() == cmpReg1 ||
603 (isSecondOpReg &&
604 MI.getOperand(0).getReg() == (unsigned)cmpOp2))) {
Sirish Pande4bd20c52012-05-12 05:10:30 +0000605
Daniel Sanders0c476112019-08-15 19:22:08 +0000606 Register feederReg = MI.getOperand(0).getReg();
Sirish Pande4bd20c52012-05-12 05:10:30 +0000607
608 // First try to see if we can get the feeder from the first operand
609 // of the compare. If we can not, and if secondOpReg is true
610 // (second operand of the compare is also register), try that one.
611 // TODO: Try to come up with some heuristic to figure out which
612 // feeder would benefit.
613
614 if (feederReg == cmpReg1) {
615 if (!canBeFeederToNewValueJump(QII, QRI, MII, jmpPos, cmpPos, MF)) {
616 if (!isSecondOpReg)
617 break;
618 else
619 continue;
620 } else
621 foundFeeder = true;
622 }
623
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000624 if (!foundFeeder && isSecondOpReg && feederReg == (unsigned)cmpOp2)
Sirish Pande4bd20c52012-05-12 05:10:30 +0000625 if (!canBeFeederToNewValueJump(QII, QRI, MII, jmpPos, cmpPos, MF))
626 break;
627
628 if (isSecondOpReg) {
629 // In case of CMPLT, or CMPLTU, or EQ with the second register
630 // to newify, swap the operands.
Krzysztof Parzyszek3d9946e2016-08-19 17:54:49 +0000631 unsigned COp = cmpInstr->getOpcode();
632 if ((COp == Hexagon::C2_cmpeq || COp == Hexagon::C4_cmpneq) &&
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000633 (feederReg == (unsigned)cmpOp2)) {
Sirish Pande4bd20c52012-05-12 05:10:30 +0000634 unsigned tmp = cmpReg1;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000635 cmpReg1 = cmpOp2;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000636 cmpOp2 = tmp;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000637 }
638
639 // Now we have swapped the operands, all we need to check is,
640 // if the second operand (after swap) is the feeder.
641 // And if it is, make a note.
642 if (feederReg == (unsigned)cmpOp2)
643 isSecondOpNewified = true;
644 }
645
646 // Now that we are moving feeder close the jump,
647 // make sure we are respecting the kill values of
648 // the operands of the feeder.
649
Krzysztof Parzyszek5ddd2e52017-06-27 18:37:16 +0000650 auto TransferKills = [jmpPos,cmpPos] (MachineInstr &MI) {
651 for (MachineOperand &MO : MI.operands()) {
652 if (!MO.isReg() || !MO.isUse())
653 continue;
Daniel Sanders0c476112019-08-15 19:22:08 +0000654 Register UseR = MO.getReg();
Krzysztof Parzyszek5ddd2e52017-06-27 18:37:16 +0000655 for (auto I = std::next(MI.getIterator()); I != jmpPos; ++I) {
656 if (I == cmpPos)
657 continue;
658 for (MachineOperand &Op : I->operands()) {
659 if (!Op.isReg() || !Op.isUse() || !Op.isKill())
660 continue;
661 if (Op.getReg() != UseR)
662 continue;
663 // We found that there is kill of a use register
664 // Set up a kill flag on the register
665 Op.setIsKill(false);
666 MO.setIsKill(true);
667 return;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000668 }
Sirish Pande4bd20c52012-05-12 05:10:30 +0000669 }
670 }
Krzysztof Parzyszek5ddd2e52017-06-27 18:37:16 +0000671 };
672
673 TransferKills(*feederPos);
674 TransferKills(*cmpPos);
675 bool MO1IsKill = cmpPos->killsRegister(cmpReg1, QRI);
676 bool MO2IsKill = isSecondOpReg && cmpPos->killsRegister(cmpOp2, QRI);
Sirish Pande4bd20c52012-05-12 05:10:30 +0000677
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000678 MBB->splice(jmpPos, MI.getParent(), MI);
679 MBB->splice(jmpPos, MI.getParent(), cmpInstr);
680 DebugLoc dl = MI.getDebugLoc();
Sirish Pande4bd20c52012-05-12 05:10:30 +0000681 MachineInstr *NewMI;
682
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000683 assert((isNewValueJumpCandidate(*cmpInstr)) &&
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000684 "This compare is not a New Value Jump candidate.");
Sirish Pande4bd20c52012-05-12 05:10:30 +0000685 unsigned opc = getNewValueJumpOpcode(cmpInstr, cmpOp2,
Jyotsna Verma1d297502013-05-02 15:39:30 +0000686 isSecondOpNewified,
687 jmpTarget, MBPI);
Sirish Pande4bd20c52012-05-12 05:10:30 +0000688 if (invertPredicate)
689 opc = QII->getInvertedPredicatedOpcode(opc);
690
Jyotsna Verma89c84822013-04-23 19:15:55 +0000691 if (isSecondOpReg)
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000692 NewMI = BuildMI(*MBB, jmpPos, dl, QII->get(opc))
693 .addReg(cmpReg1, getKillRegState(MO1IsKill))
694 .addReg(cmpOp2, getKillRegState(MO2IsKill))
695 .addMBB(jmpTarget);
Jyotsna Verma89c84822013-04-23 19:15:55 +0000696
Jyotsna Verma89c84822013-04-23 19:15:55 +0000697 else
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000698 NewMI = BuildMI(*MBB, jmpPos, dl, QII->get(opc))
699 .addReg(cmpReg1, getKillRegState(MO1IsKill))
700 .addImm(cmpOp2)
701 .addMBB(jmpTarget);
Sirish Pande4bd20c52012-05-12 05:10:30 +0000702
703 assert(NewMI && "New Value Jump Instruction Not created!");
Duncan Sands0480b9b2013-05-13 07:50:47 +0000704 (void)NewMI;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000705 if (cmpInstr->getOperand(0).isReg() &&
706 cmpInstr->getOperand(0).isKill())
707 cmpInstr->getOperand(0).setIsKill(false);
708 if (cmpInstr->getOperand(1).isReg() &&
709 cmpInstr->getOperand(1).isKill())
710 cmpInstr->getOperand(1).setIsKill(false);
711 cmpInstr->eraseFromParent();
712 jmpInstr->eraseFromParent();
713 ++nvjGenerated;
714 ++NumNVJGenerated;
715 break;
716 }
717 }
718 }
719 }
720
721 return true;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000722}
723
724FunctionPass *llvm::createHexagonNewValueJump() {
725 return new HexagonNewValueJump();
726}