blob: 51cf6bbd8a241615bd34b942d0d4e79da6a7c639 [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//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This implements NewValueJump pass in Hexagon.
11// Ideally, we should merge this as a Peephole pass prior to register
Benjamin Kramerbde91762012-06-02 10:20:22 +000012// allocation, but because we have a spill in between the feeder and new value
Sirish Pande4bd20c52012-05-12 05:10:30 +000013// jump instructions, we are forced to write after register allocation.
Benjamin Kramerbde91762012-06-02 10:20:22 +000014// Having said that, we should re-attempt to pull this earlier at some point
Sirish Pande4bd20c52012-05-12 05:10:30 +000015// in future.
16
17// The basic approach looks for sequence of predicated jump, compare instruciton
18// that genereates the predicate and, the feeder to the predicate. Once it finds
19// all, it collapses compare and jump instruction into a new valu jump
20// intstructions.
21//
Sirish Pande4bd20c52012-05-12 05:10:30 +000022//===----------------------------------------------------------------------===//
Eugene Zelenko3b873362017-09-28 22:27:31 +000023
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"
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
Jyotsna Verma1d297502013-05-02 15:39:30 +000098 /// \brief A handle to the branch probability pass.
99 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 Mistrih9d7bb0c2017-11-28 17:15:09 +0000132 // %d0<def> = Hexagon_S2_lsr_r_p %d0<kill>, %r2<kill>
133 // %r0<def> = KILL %r0, %d0<imp-use,kill>
134 // %p0<def> = CMPEQri %r0<kill>, 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
142 // Make sure there there is no 'def' or 'use' of any of the uses of
143 // feeder insn between it's definition, this MI and jump, jmpInst
144 // skipping compare, cmpInst.
145 // Here's the example.
146 // r21=memub(r22+r24<<#0)
147 // p0 = cmp.eq(r21, #0)
148 // r4=memub(r3+r21<<#0)
149 // if (p0.new) jump:t .LBB29_45
150 // Without this check, it will be converted into
151 // r4=memub(r3+r21<<#0)
152 // r21=memub(r22+r24<<#0)
153 // p0 = cmp.eq(r21, #0)
154 // if (p0.new) jump:t .LBB29_45
155 // and result WAR hazards if converted to New Value Jump.
Sirish Pande4bd20c52012-05-12 05:10:30 +0000156 for (unsigned i = 0; i < II->getNumOperands(); ++i) {
157 if (II->getOperand(i).isReg() &&
158 (II->getOperand(i).isUse() || II->getOperand(i).isDef())) {
159 MachineBasicBlock::iterator localII = II;
160 ++localII;
161 unsigned Reg = II->getOperand(i).getReg();
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000162 for (MachineBasicBlock::iterator localBegin = localII; localBegin != end;
163 ++localBegin) {
164 if (localBegin == skip)
165 continue;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000166 // Check for Subregisters too.
167 if (localBegin->modifiesRegister(Reg, TRI) ||
168 localBegin->readsRegister(Reg, TRI))
169 return false;
170 }
171 }
172 }
173 return true;
174}
175
176// These are the common checks that need to performed
177// to determine if
178// 1. compare instruction can be moved before jump.
179// 2. feeder to the compare instruction can be moved before jump.
180static bool commonChecksToProhibitNewValueJump(bool afterRA,
181 MachineBasicBlock::iterator MII) {
Sirish Pande4bd20c52012-05-12 05:10:30 +0000182 // If store in path, bail out.
Krzysztof Parzyszek1fd0c7e2017-07-24 19:35:48 +0000183 if (MII->mayStore())
Sirish Pande4bd20c52012-05-12 05:10:30 +0000184 return false;
185
186 // if call in path, bail out.
Krzysztof Parzyszek3d9946e2016-08-19 17:54:49 +0000187 if (MII->isCall())
Sirish Pande4bd20c52012-05-12 05:10:30 +0000188 return false;
189
190 // if NVJ is running prior to RA, do the following checks.
191 if (!afterRA) {
192 // The following Target Opcode instructions are spurious
193 // to new value jump. If they are in the path, bail out.
194 // KILL sets kill flag on the opcode. It also sets up a
195 // single register, out of pair.
Francis Visoiu Mistrih9d7bb0c2017-11-28 17:15:09 +0000196 // %d0<def> = S2_lsr_r_p %d0<kill>, %r2<kill>
197 // %r0<def> = KILL %r0, %d0<imp-use,kill>
198 // %p0<def> = C2_cmpeqi %r0<kill>, 0
Sirish Pande4bd20c52012-05-12 05:10:30 +0000199 // PHI can be anything after RA.
200 // COPY can remateriaze things in between feeder, compare and nvj.
201 if (MII->getOpcode() == TargetOpcode::KILL ||
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000202 MII->getOpcode() == TargetOpcode::PHI ||
Sirish Pande4bd20c52012-05-12 05:10:30 +0000203 MII->getOpcode() == TargetOpcode::COPY)
204 return false;
205
206 // The following pseudo Hexagon instructions sets "use" and "def"
207 // of registers by individual passes in the backend. At this time,
208 // we don't know the scope of usage and definitions of these
209 // instructions.
Krzysztof Parzyszek3d9946e2016-08-19 17:54:49 +0000210 if (MII->getOpcode() == Hexagon::LDriw_pred ||
Sirish Pande4bd20c52012-05-12 05:10:30 +0000211 MII->getOpcode() == Hexagon::STriw_pred)
212 return false;
213 }
214
215 return true;
216}
217
218static bool canCompareBeNewValueJump(const HexagonInstrInfo *QII,
219 const TargetRegisterInfo *TRI,
220 MachineBasicBlock::iterator II,
221 unsigned pReg,
222 bool secondReg,
223 bool optLocation,
224 MachineBasicBlock::iterator end,
225 MachineFunction &MF) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000226 MachineInstr &MI = *II;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000227
228 // If the second operand of the compare is an imm, make sure it's in the
229 // range specified by the arch.
230 if (!secondReg) {
Krzysztof Parzyszek64e5d7d2017-10-20 19:33:12 +0000231 const MachineOperand &Op2 = MI.getOperand(2);
232 if (!Op2.isImm())
233 return false;
234
235 int64_t v = Op2.getImm();
Krzysztof Parzyszek3d9946e2016-08-19 17:54:49 +0000236 bool Valid = false;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000237
Krzysztof Parzyszek3d9946e2016-08-19 17:54:49 +0000238 switch (MI.getOpcode()) {
239 case Hexagon::C2_cmpeqi:
Krzysztof Parzyszek1fd0c7e2017-07-24 19:35:48 +0000240 case Hexagon::C4_cmpneqi:
Krzysztof Parzyszek3d9946e2016-08-19 17:54:49 +0000241 case Hexagon::C2_cmpgti:
Krzysztof Parzyszek1fd0c7e2017-07-24 19:35:48 +0000242 case Hexagon::C4_cmpltei:
Krzysztof Parzyszek3d9946e2016-08-19 17:54:49 +0000243 Valid = (isUInt<5>(v) || v == -1);
244 break;
245 case Hexagon::C2_cmpgtui:
Krzysztof Parzyszek1fd0c7e2017-07-24 19:35:48 +0000246 case Hexagon::C4_cmplteui:
Krzysztof Parzyszek3d9946e2016-08-19 17:54:49 +0000247 Valid = isUInt<5>(v);
248 break;
249 case Hexagon::S2_tstbit_i:
250 case Hexagon::S4_ntstbit_i:
251 Valid = (v == 0);
252 break;
253 }
254
255 if (!Valid)
Sirish Pande4bd20c52012-05-12 05:10:30 +0000256 return false;
257 }
258
Jyotsna Verma84c47102013-05-06 18:49:23 +0000259 unsigned cmpReg1, cmpOp2 = 0; // cmpOp2 assignment silences compiler warning.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000260 cmpReg1 = MI.getOperand(1).getReg();
Sirish Pande4bd20c52012-05-12 05:10:30 +0000261
262 if (secondReg) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000263 cmpOp2 = MI.getOperand(2).getReg();
Sirish Pande4bd20c52012-05-12 05:10:30 +0000264
Krzysztof Parzyszek3d9946e2016-08-19 17:54:49 +0000265 // If the same register appears as both operands, we cannot generate a new
266 // value compare. Only one operand may use the .new suffix.
267 if (cmpReg1 == cmpOp2)
268 return false;
269
Sirish Pande4bd20c52012-05-12 05:10:30 +0000270 // Make sure that that second register is not from COPY
271 // At machine code level, we don't need this, but if we decide
272 // to move new value jump prior to RA, we would be needing this.
273 MachineRegisterInfo &MRI = MF.getRegInfo();
274 if (secondReg && !TargetRegisterInfo::isPhysicalRegister(cmpOp2)) {
275 MachineInstr *def = MRI.getVRegDef(cmpOp2);
276 if (def->getOpcode() == TargetOpcode::COPY)
277 return false;
278 }
279 }
280
281 // Walk the instructions after the compare (predicate def) to the jump,
282 // and satisfy the following conditions.
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000283 ++II;
284 for (MachineBasicBlock::iterator localII = II; localII != end; ++localII) {
Krzysztof Parzyszek3d9946e2016-08-19 17:54:49 +0000285 if (localII->isDebugValue())
286 continue;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000287
288 // Check 1.
289 // If "common" checks fail, bail out.
290 if (!commonChecksToProhibitNewValueJump(optLocation, localII))
291 return false;
292
293 // Check 2.
294 // If there is a def or use of predicate (result of compare), bail out.
295 if (localII->modifiesRegister(pReg, TRI) ||
296 localII->readsRegister(pReg, TRI))
297 return false;
298
299 // Check 3.
300 // If there is a def of any of the use of the compare (operands of compare),
301 // bail out.
302 // Eg.
303 // p0 = cmp.eq(r2, r0)
304 // r2 = r4
305 // if (p0.new) jump:t .LBB28_3
306 if (localII->modifiesRegister(cmpReg1, TRI) ||
307 (secondReg && localII->modifiesRegister(cmpOp2, TRI)))
308 return false;
309 }
310 return true;
311}
312
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000313// Given a compare operator, return a matching New Value Jump compare operator.
314// Make sure that MI here is included in isNewValueJumpCandidate.
Jyotsna Verma1d297502013-05-02 15:39:30 +0000315static unsigned getNewValueJumpOpcode(MachineInstr *MI, int reg,
316 bool secondRegNewified,
317 MachineBasicBlock *jmpTarget,
318 const MachineBranchProbabilityInfo
319 *MBPI) {
320 bool taken = false;
321 MachineBasicBlock *Src = MI->getParent();
322 const BranchProbability Prediction =
323 MBPI->getEdgeProbability(Src, jmpTarget);
324
325 if (Prediction >= BranchProbability(1,2))
326 taken = true;
327
Sirish Pande4bd20c52012-05-12 05:10:30 +0000328 switch (MI->getOpcode()) {
Colin LeMahieu902157c2014-11-25 18:20:52 +0000329 case Hexagon::C2_cmpeq:
Colin LeMahieu6e3e62f2015-02-05 22:03:32 +0000330 return taken ? Hexagon::J4_cmpeq_t_jumpnv_t
331 : Hexagon::J4_cmpeq_t_jumpnv_nt;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000332
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000333 case Hexagon::C2_cmpeqi:
Sirish Pande4bd20c52012-05-12 05:10:30 +0000334 if (reg >= 0)
Colin LeMahieu6e3e62f2015-02-05 22:03:32 +0000335 return taken ? Hexagon::J4_cmpeqi_t_jumpnv_t
336 : Hexagon::J4_cmpeqi_t_jumpnv_nt;
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000337 return taken ? Hexagon::J4_cmpeqn1_t_jumpnv_t
338 : Hexagon::J4_cmpeqn1_t_jumpnv_nt;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000339
Krzysztof Parzyszek1fd0c7e2017-07-24 19:35:48 +0000340 case Hexagon::C4_cmpneqi:
341 if (reg >= 0)
342 return taken ? Hexagon::J4_cmpeqi_f_jumpnv_t
343 : Hexagon::J4_cmpeqi_f_jumpnv_nt;
344 return taken ? Hexagon::J4_cmpeqn1_f_jumpnv_t :
345 Hexagon::J4_cmpeqn1_f_jumpnv_nt;
346
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000347 case Hexagon::C2_cmpgt:
Sirish Pande4bd20c52012-05-12 05:10:30 +0000348 if (secondRegNewified)
Colin LeMahieu6e3e62f2015-02-05 22:03:32 +0000349 return taken ? Hexagon::J4_cmplt_t_jumpnv_t
350 : Hexagon::J4_cmplt_t_jumpnv_nt;
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000351 return taken ? Hexagon::J4_cmpgt_t_jumpnv_t
352 : Hexagon::J4_cmpgt_t_jumpnv_nt;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000353
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000354 case Hexagon::C2_cmpgti:
Sirish Pande4bd20c52012-05-12 05:10:30 +0000355 if (reg >= 0)
Colin LeMahieu6e3e62f2015-02-05 22:03:32 +0000356 return taken ? Hexagon::J4_cmpgti_t_jumpnv_t
357 : Hexagon::J4_cmpgti_t_jumpnv_nt;
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000358 return taken ? Hexagon::J4_cmpgtn1_t_jumpnv_t
359 : Hexagon::J4_cmpgtn1_t_jumpnv_nt;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000360
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000361 case Hexagon::C2_cmpgtu:
Sirish Pande4bd20c52012-05-12 05:10:30 +0000362 if (secondRegNewified)
Colin LeMahieu6e3e62f2015-02-05 22:03:32 +0000363 return taken ? Hexagon::J4_cmpltu_t_jumpnv_t
364 : Hexagon::J4_cmpltu_t_jumpnv_nt;
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000365 return taken ? Hexagon::J4_cmpgtu_t_jumpnv_t
366 : Hexagon::J4_cmpgtu_t_jumpnv_nt;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000367
Colin LeMahieu6e0f9f82014-11-26 19:43:12 +0000368 case Hexagon::C2_cmpgtui:
Colin LeMahieu6e3e62f2015-02-05 22:03:32 +0000369 return taken ? Hexagon::J4_cmpgtui_t_jumpnv_t
370 : Hexagon::J4_cmpgtui_t_jumpnv_nt;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000371
Ron Liebermane6540e22015-12-08 16:28:32 +0000372 case Hexagon::C4_cmpneq:
373 return taken ? Hexagon::J4_cmpeq_f_jumpnv_t
374 : Hexagon::J4_cmpeq_f_jumpnv_nt;
375
376 case Hexagon::C4_cmplte:
377 if (secondRegNewified)
378 return taken ? Hexagon::J4_cmplt_f_jumpnv_t
379 : Hexagon::J4_cmplt_f_jumpnv_nt;
380 return taken ? Hexagon::J4_cmpgt_f_jumpnv_t
381 : Hexagon::J4_cmpgt_f_jumpnv_nt;
382
383 case Hexagon::C4_cmplteu:
384 if (secondRegNewified)
385 return taken ? Hexagon::J4_cmpltu_f_jumpnv_t
386 : Hexagon::J4_cmpltu_f_jumpnv_nt;
387 return taken ? Hexagon::J4_cmpgtu_f_jumpnv_t
388 : Hexagon::J4_cmpgtu_f_jumpnv_nt;
389
Krzysztof Parzyszek1fd0c7e2017-07-24 19:35:48 +0000390 case Hexagon::C4_cmpltei:
391 if (reg >= 0)
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000392 return taken ? Hexagon::J4_cmpgti_f_jumpnv_t
393 : Hexagon::J4_cmpgti_f_jumpnv_nt;
394 return taken ? Hexagon::J4_cmpgtn1_f_jumpnv_t
395 : Hexagon::J4_cmpgtn1_f_jumpnv_nt;
Krzysztof Parzyszek1fd0c7e2017-07-24 19:35:48 +0000396
397 case Hexagon::C4_cmplteui:
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000398 return taken ? Hexagon::J4_cmpgtui_f_jumpnv_t
399 : Hexagon::J4_cmpgtui_f_jumpnv_nt;
Krzysztof Parzyszek1fd0c7e2017-07-24 19:35:48 +0000400
Sirish Pande4bd20c52012-05-12 05:10:30 +0000401 default:
402 llvm_unreachable("Could not find matching New Value Jump instruction.");
403 }
404 // return *some value* to avoid compiler warning
405 return 0;
406}
407
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000408bool HexagonNewValueJump::isNewValueJumpCandidate(
409 const MachineInstr &MI) const {
410 switch (MI.getOpcode()) {
411 case Hexagon::C2_cmpeq:
412 case Hexagon::C2_cmpeqi:
413 case Hexagon::C2_cmpgt:
414 case Hexagon::C2_cmpgti:
415 case Hexagon::C2_cmpgtu:
416 case Hexagon::C2_cmpgtui:
417 case Hexagon::C4_cmpneq:
Krzysztof Parzyszek1fd0c7e2017-07-24 19:35:48 +0000418 case Hexagon::C4_cmpneqi:
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000419 case Hexagon::C4_cmplte:
420 case Hexagon::C4_cmplteu:
Krzysztof Parzyszek1fd0c7e2017-07-24 19:35:48 +0000421 case Hexagon::C4_cmpltei:
422 case Hexagon::C4_cmplteui:
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000423 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000424
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000425 default:
426 return false;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000427 }
428}
429
Sirish Pande4bd20c52012-05-12 05:10:30 +0000430bool HexagonNewValueJump::runOnMachineFunction(MachineFunction &MF) {
Sirish Pande4bd20c52012-05-12 05:10:30 +0000431 DEBUG(dbgs() << "********** Hexagon New Value Jump **********\n"
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000432 << "********** Function: " << MF.getName() << "\n");
Sirish Pande4bd20c52012-05-12 05:10:30 +0000433
Andrew Kaylor5b444a22016-04-26 19:46:28 +0000434 if (skipFunction(*MF.getFunction()))
435 return false;
436
Eric Christopher0fef34e2015-02-02 22:11:42 +0000437 // If we move NewValueJump before register allocation we'll need live variable
438 // analysis here too.
Sirish Pande4bd20c52012-05-12 05:10:30 +0000439
Eric Christopherfc6de422014-08-05 02:39:49 +0000440 QII = static_cast<const HexagonInstrInfo *>(MF.getSubtarget().getInstrInfo());
Eric Christopherd9134482014-08-04 21:25:23 +0000441 QRI = static_cast<const HexagonRegisterInfo *>(
Eric Christopherfc6de422014-08-05 02:39:49 +0000442 MF.getSubtarget().getRegisterInfo());
Jyotsna Verma1d297502013-05-02 15:39:30 +0000443 MBPI = &getAnalysis<MachineBranchProbabilityInfo>();
Sirish Pande4bd20c52012-05-12 05:10:30 +0000444
Colin LeMahieu4fd203d2015-02-09 21:56:37 +0000445 if (DisableNewValueJumps) {
Sirish Pande4bd20c52012-05-12 05:10:30 +0000446 return false;
447 }
448
449 int nvjCount = DbgNVJCount;
450 int nvjGenerated = 0;
451
452 // Loop through all the bb's of the function
453 for (MachineFunction::iterator MBBb = MF.begin(), MBBe = MF.end();
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000454 MBBb != MBBe; ++MBBb) {
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000455 MachineBasicBlock *MBB = &*MBBb;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000456
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000457 DEBUG(dbgs() << "** dumping bb ** " << MBB->getNumber() << "\n");
Sirish Pande4bd20c52012-05-12 05:10:30 +0000458 DEBUG(MBB->dump());
459 DEBUG(dbgs() << "\n" << "********** dumping instr bottom up **********\n");
460 bool foundJump = false;
461 bool foundCompare = false;
462 bool invertPredicate = false;
463 unsigned predReg = 0; // predicate reg of the jump.
464 unsigned cmpReg1 = 0;
465 int cmpOp2 = 0;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000466 MachineBasicBlock::iterator jmpPos;
467 MachineBasicBlock::iterator cmpPos;
Craig Topper062a2ba2014-04-25 05:30:21 +0000468 MachineInstr *cmpInstr = nullptr, *jmpInstr = nullptr;
469 MachineBasicBlock *jmpTarget = nullptr;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000470 bool afterRA = false;
471 bool isSecondOpReg = false;
472 bool isSecondOpNewified = false;
473 // Traverse the basic block - bottom up
474 for (MachineBasicBlock::iterator MII = MBB->end(), E = MBB->begin();
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000475 MII != E;) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000476 MachineInstr &MI = *--MII;
477 if (MI.isDebugValue()) {
Sirish Pande4bd20c52012-05-12 05:10:30 +0000478 continue;
479 }
480
481 if ((nvjCount == 0) || (nvjCount > -1 && nvjCount <= nvjGenerated))
482 break;
483
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000484 DEBUG(dbgs() << "Instr: "; MI.dump(); dbgs() << "\n");
Sirish Pande4bd20c52012-05-12 05:10:30 +0000485
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000486 if (!foundJump && (MI.getOpcode() == Hexagon::J2_jumpt ||
Krzysztof Parzyszeka243adf2016-08-19 14:14:09 +0000487 MI.getOpcode() == Hexagon::J2_jumptpt ||
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000488 MI.getOpcode() == Hexagon::J2_jumpf ||
Krzysztof Parzyszeka243adf2016-08-19 14:14:09 +0000489 MI.getOpcode() == Hexagon::J2_jumpfpt ||
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000490 MI.getOpcode() == Hexagon::J2_jumptnewpt ||
491 MI.getOpcode() == Hexagon::J2_jumptnew ||
492 MI.getOpcode() == Hexagon::J2_jumpfnewpt ||
493 MI.getOpcode() == Hexagon::J2_jumpfnew)) {
Sirish Pande4bd20c52012-05-12 05:10:30 +0000494 // This is where you would insert your compare and
495 // instr that feeds compare
496 jmpPos = MII;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000497 jmpInstr = &MI;
498 predReg = MI.getOperand(0).getReg();
Sirish Pande4bd20c52012-05-12 05:10:30 +0000499 afterRA = TargetRegisterInfo::isPhysicalRegister(predReg);
500
501 // If ifconverter had not messed up with the kill flags of the
502 // operands, the following check on the kill flag would suffice.
503 // if(!jmpInstr->getOperand(0).isKill()) break;
504
505 // This predicate register is live out out of BB
506 // this would only work if we can actually use Live
507 // variable analysis on phy regs - but LLVM does not
508 // provide LV analysis on phys regs.
509 //if(LVs.isLiveOut(predReg, *MBB)) break;
510
511 // Get all the successors of this block - which will always
Krzysztof Parzyszek3d9946e2016-08-19 17:54:49 +0000512 // be 2. Check if the predicate register is live-in in those
Sirish Pande4bd20c52012-05-12 05:10:30 +0000513 // successor. If yes, we can not delete the predicate -
514 // I am doing this only because LLVM does not provide LiveOut
515 // at the BB level.
516 bool predLive = false;
517 for (MachineBasicBlock::const_succ_iterator SI = MBB->succ_begin(),
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000518 SIE = MBB->succ_end();
519 SI != SIE; ++SI) {
520 MachineBasicBlock *succMBB = *SI;
521 if (succMBB->isLiveIn(predReg))
Sirish Pande4bd20c52012-05-12 05:10:30 +0000522 predLive = true;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000523 }
524 if (predLive)
525 break;
526
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000527 if (!MI.getOperand(1).isMBB())
Krzysztof Parzyszekb28ae102016-01-14 15:05:27 +0000528 continue;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000529 jmpTarget = MI.getOperand(1).getMBB();
Sirish Pande4bd20c52012-05-12 05:10:30 +0000530 foundJump = true;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000531 if (MI.getOpcode() == Hexagon::J2_jumpf ||
532 MI.getOpcode() == Hexagon::J2_jumpfnewpt ||
533 MI.getOpcode() == Hexagon::J2_jumpfnew) {
Sirish Pande4bd20c52012-05-12 05:10:30 +0000534 invertPredicate = true;
535 }
536 continue;
537 }
538
539 // No new value jump if there is a barrier. A barrier has to be in its
540 // own packet. A barrier has zero operands. We conservatively bail out
541 // here if we see any instruction with zero operands.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000542 if (foundJump && MI.getNumOperands() == 0)
Sirish Pande4bd20c52012-05-12 05:10:30 +0000543 break;
544
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000545 if (foundJump && !foundCompare && MI.getOperand(0).isReg() &&
546 MI.getOperand(0).getReg() == predReg) {
Sirish Pande4bd20c52012-05-12 05:10:30 +0000547 // Not all compares can be new value compare. Arch Spec: 7.6.1.1
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000548 if (isNewValueJumpCandidate(MI)) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000549 assert(
550 (MI.getDesc().isCompare()) &&
Sirish Pande4bd20c52012-05-12 05:10:30 +0000551 "Only compare instruction can be collapsed into New Value Jump");
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000552 isSecondOpReg = MI.getOperand(2).isReg();
Sirish Pande4bd20c52012-05-12 05:10:30 +0000553
554 if (!canCompareBeNewValueJump(QII, QRI, MII, predReg, isSecondOpReg,
555 afterRA, jmpPos, MF))
556 break;
557
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000558 cmpInstr = &MI;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000559 cmpPos = MII;
560 foundCompare = true;
561
562 // We need cmpReg1 and cmpOp2(imm or reg) while building
563 // new value jump instruction.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000564 cmpReg1 = MI.getOperand(1).getReg();
Sirish Pande4bd20c52012-05-12 05:10:30 +0000565
Krzysztof Parzyszek5ddd2e52017-06-27 18:37:16 +0000566 if (isSecondOpReg)
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000567 cmpOp2 = MI.getOperand(2).getReg();
Krzysztof Parzyszek5ddd2e52017-06-27 18:37:16 +0000568 else
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000569 cmpOp2 = MI.getOperand(2).getImm();
Sirish Pande4bd20c52012-05-12 05:10:30 +0000570 continue;
571 }
572 }
573
574 if (foundCompare && foundJump) {
Sirish Pande4bd20c52012-05-12 05:10:30 +0000575 // If "common" checks fail, bail out on this BB.
576 if (!commonChecksToProhibitNewValueJump(afterRA, MII))
577 break;
578
579 bool foundFeeder = false;
580 MachineBasicBlock::iterator feederPos = MII;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000581 if (MI.getOperand(0).isReg() && MI.getOperand(0).isDef() &&
582 (MI.getOperand(0).getReg() == cmpReg1 ||
583 (isSecondOpReg &&
584 MI.getOperand(0).getReg() == (unsigned)cmpOp2))) {
Sirish Pande4bd20c52012-05-12 05:10:30 +0000585
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000586 unsigned feederReg = MI.getOperand(0).getReg();
Sirish Pande4bd20c52012-05-12 05:10:30 +0000587
588 // First try to see if we can get the feeder from the first operand
589 // of the compare. If we can not, and if secondOpReg is true
590 // (second operand of the compare is also register), try that one.
591 // TODO: Try to come up with some heuristic to figure out which
592 // feeder would benefit.
593
594 if (feederReg == cmpReg1) {
595 if (!canBeFeederToNewValueJump(QII, QRI, MII, jmpPos, cmpPos, MF)) {
596 if (!isSecondOpReg)
597 break;
598 else
599 continue;
600 } else
601 foundFeeder = true;
602 }
603
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000604 if (!foundFeeder && isSecondOpReg && feederReg == (unsigned)cmpOp2)
Sirish Pande4bd20c52012-05-12 05:10:30 +0000605 if (!canBeFeederToNewValueJump(QII, QRI, MII, jmpPos, cmpPos, MF))
606 break;
607
608 if (isSecondOpReg) {
609 // In case of CMPLT, or CMPLTU, or EQ with the second register
610 // to newify, swap the operands.
Krzysztof Parzyszek3d9946e2016-08-19 17:54:49 +0000611 unsigned COp = cmpInstr->getOpcode();
612 if ((COp == Hexagon::C2_cmpeq || COp == Hexagon::C4_cmpneq) &&
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000613 (feederReg == (unsigned)cmpOp2)) {
Sirish Pande4bd20c52012-05-12 05:10:30 +0000614 unsigned tmp = cmpReg1;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000615 cmpReg1 = cmpOp2;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000616 cmpOp2 = tmp;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000617 }
618
619 // Now we have swapped the operands, all we need to check is,
620 // if the second operand (after swap) is the feeder.
621 // And if it is, make a note.
622 if (feederReg == (unsigned)cmpOp2)
623 isSecondOpNewified = true;
624 }
625
626 // Now that we are moving feeder close the jump,
627 // make sure we are respecting the kill values of
628 // the operands of the feeder.
629
Krzysztof Parzyszek5ddd2e52017-06-27 18:37:16 +0000630 auto TransferKills = [jmpPos,cmpPos] (MachineInstr &MI) {
631 for (MachineOperand &MO : MI.operands()) {
632 if (!MO.isReg() || !MO.isUse())
633 continue;
634 unsigned UseR = MO.getReg();
635 for (auto I = std::next(MI.getIterator()); I != jmpPos; ++I) {
636 if (I == cmpPos)
637 continue;
638 for (MachineOperand &Op : I->operands()) {
639 if (!Op.isReg() || !Op.isUse() || !Op.isKill())
640 continue;
641 if (Op.getReg() != UseR)
642 continue;
643 // We found that there is kill of a use register
644 // Set up a kill flag on the register
645 Op.setIsKill(false);
646 MO.setIsKill(true);
647 return;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000648 }
Sirish Pande4bd20c52012-05-12 05:10:30 +0000649 }
650 }
Krzysztof Parzyszek5ddd2e52017-06-27 18:37:16 +0000651 };
652
653 TransferKills(*feederPos);
654 TransferKills(*cmpPos);
655 bool MO1IsKill = cmpPos->killsRegister(cmpReg1, QRI);
656 bool MO2IsKill = isSecondOpReg && cmpPos->killsRegister(cmpOp2, QRI);
Sirish Pande4bd20c52012-05-12 05:10:30 +0000657
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000658 MBB->splice(jmpPos, MI.getParent(), MI);
659 MBB->splice(jmpPos, MI.getParent(), cmpInstr);
660 DebugLoc dl = MI.getDebugLoc();
Sirish Pande4bd20c52012-05-12 05:10:30 +0000661 MachineInstr *NewMI;
662
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000663 assert((isNewValueJumpCandidate(*cmpInstr)) &&
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000664 "This compare is not a New Value Jump candidate.");
Sirish Pande4bd20c52012-05-12 05:10:30 +0000665 unsigned opc = getNewValueJumpOpcode(cmpInstr, cmpOp2,
Jyotsna Verma1d297502013-05-02 15:39:30 +0000666 isSecondOpNewified,
667 jmpTarget, MBPI);
Sirish Pande4bd20c52012-05-12 05:10:30 +0000668 if (invertPredicate)
669 opc = QII->getInvertedPredicatedOpcode(opc);
670
Jyotsna Verma89c84822013-04-23 19:15:55 +0000671 if (isSecondOpReg)
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000672 NewMI = BuildMI(*MBB, jmpPos, dl, QII->get(opc))
673 .addReg(cmpReg1, getKillRegState(MO1IsKill))
674 .addReg(cmpOp2, getKillRegState(MO2IsKill))
675 .addMBB(jmpTarget);
Jyotsna Verma89c84822013-04-23 19:15:55 +0000676
Jyotsna Verma89c84822013-04-23 19:15:55 +0000677 else
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000678 NewMI = BuildMI(*MBB, jmpPos, dl, QII->get(opc))
679 .addReg(cmpReg1, getKillRegState(MO1IsKill))
680 .addImm(cmpOp2)
681 .addMBB(jmpTarget);
Sirish Pande4bd20c52012-05-12 05:10:30 +0000682
683 assert(NewMI && "New Value Jump Instruction Not created!");
Duncan Sands0480b9b2013-05-13 07:50:47 +0000684 (void)NewMI;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000685 if (cmpInstr->getOperand(0).isReg() &&
686 cmpInstr->getOperand(0).isKill())
687 cmpInstr->getOperand(0).setIsKill(false);
688 if (cmpInstr->getOperand(1).isReg() &&
689 cmpInstr->getOperand(1).isKill())
690 cmpInstr->getOperand(1).setIsKill(false);
691 cmpInstr->eraseFromParent();
692 jmpInstr->eraseFromParent();
693 ++nvjGenerated;
694 ++NumNVJGenerated;
695 break;
696 }
697 }
698 }
699 }
700
701 return true;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000702}
703
704FunctionPass *llvm::createHexagonNewValueJump() {
705 return new HexagonNewValueJump();
706}