blob: 8f177d9a4ee0a69e3c0ab3d3c2bf5fe79a4d59d3 [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
Krzysztof Parzyszek44555222017-11-30 20:32:54 +0000142 if (QII->isSolo(*II))
143 return false;
144
Sirish Pande4bd20c52012-05-12 05:10:30 +0000145 // Make sure there there is no 'def' or 'use' of any of the uses of
146 // feeder insn between it's definition, this MI and jump, jmpInst
147 // skipping compare, cmpInst.
148 // Here's the example.
149 // r21=memub(r22+r24<<#0)
150 // p0 = cmp.eq(r21, #0)
151 // r4=memub(r3+r21<<#0)
152 // if (p0.new) jump:t .LBB29_45
153 // Without this check, it will be converted into
154 // r4=memub(r3+r21<<#0)
155 // r21=memub(r22+r24<<#0)
156 // p0 = cmp.eq(r21, #0)
157 // if (p0.new) jump:t .LBB29_45
158 // and result WAR hazards if converted to New Value Jump.
Sirish Pande4bd20c52012-05-12 05:10:30 +0000159 for (unsigned i = 0; i < II->getNumOperands(); ++i) {
160 if (II->getOperand(i).isReg() &&
161 (II->getOperand(i).isUse() || II->getOperand(i).isDef())) {
162 MachineBasicBlock::iterator localII = II;
163 ++localII;
164 unsigned Reg = II->getOperand(i).getReg();
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000165 for (MachineBasicBlock::iterator localBegin = localII; localBegin != end;
166 ++localBegin) {
167 if (localBegin == skip)
168 continue;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000169 // Check for Subregisters too.
170 if (localBegin->modifiesRegister(Reg, TRI) ||
171 localBegin->readsRegister(Reg, TRI))
172 return false;
173 }
174 }
175 }
176 return true;
177}
178
179// These are the common checks that need to performed
180// to determine if
181// 1. compare instruction can be moved before jump.
182// 2. feeder to the compare instruction can be moved before jump.
183static bool commonChecksToProhibitNewValueJump(bool afterRA,
184 MachineBasicBlock::iterator MII) {
Sirish Pande4bd20c52012-05-12 05:10:30 +0000185 // If store in path, bail out.
Krzysztof Parzyszek1fd0c7e2017-07-24 19:35:48 +0000186 if (MII->mayStore())
Sirish Pande4bd20c52012-05-12 05:10:30 +0000187 return false;
188
189 // if call in path, bail out.
Krzysztof Parzyszek3d9946e2016-08-19 17:54:49 +0000190 if (MII->isCall())
Sirish Pande4bd20c52012-05-12 05:10:30 +0000191 return false;
192
193 // if NVJ is running prior to RA, do the following checks.
194 if (!afterRA) {
195 // The following Target Opcode instructions are spurious
196 // to new value jump. If they are in the path, bail out.
197 // KILL sets kill flag on the opcode. It also sets up a
198 // single register, out of pair.
Francis Visoiu Mistrih9d7bb0c2017-11-28 17:15:09 +0000199 // %d0<def> = S2_lsr_r_p %d0<kill>, %r2<kill>
200 // %r0<def> = KILL %r0, %d0<imp-use,kill>
201 // %p0<def> = C2_cmpeqi %r0<kill>, 0
Sirish Pande4bd20c52012-05-12 05:10:30 +0000202 // PHI can be anything after RA.
203 // COPY can remateriaze things in between feeder, compare and nvj.
204 if (MII->getOpcode() == TargetOpcode::KILL ||
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000205 MII->getOpcode() == TargetOpcode::PHI ||
Sirish Pande4bd20c52012-05-12 05:10:30 +0000206 MII->getOpcode() == TargetOpcode::COPY)
207 return false;
208
209 // The following pseudo Hexagon instructions sets "use" and "def"
210 // of registers by individual passes in the backend. At this time,
211 // we don't know the scope of usage and definitions of these
212 // instructions.
Krzysztof Parzyszek3d9946e2016-08-19 17:54:49 +0000213 if (MII->getOpcode() == Hexagon::LDriw_pred ||
Sirish Pande4bd20c52012-05-12 05:10:30 +0000214 MII->getOpcode() == Hexagon::STriw_pred)
215 return false;
216 }
217
218 return true;
219}
220
221static bool canCompareBeNewValueJump(const HexagonInstrInfo *QII,
222 const TargetRegisterInfo *TRI,
223 MachineBasicBlock::iterator II,
224 unsigned pReg,
225 bool secondReg,
226 bool optLocation,
227 MachineBasicBlock::iterator end,
228 MachineFunction &MF) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000229 MachineInstr &MI = *II;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000230
231 // If the second operand of the compare is an imm, make sure it's in the
232 // range specified by the arch.
233 if (!secondReg) {
Krzysztof Parzyszek64e5d7d2017-10-20 19:33:12 +0000234 const MachineOperand &Op2 = MI.getOperand(2);
235 if (!Op2.isImm())
236 return false;
237
238 int64_t v = Op2.getImm();
Krzysztof Parzyszek3d9946e2016-08-19 17:54:49 +0000239 bool Valid = false;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000240
Krzysztof Parzyszek3d9946e2016-08-19 17:54:49 +0000241 switch (MI.getOpcode()) {
242 case Hexagon::C2_cmpeqi:
Krzysztof Parzyszek1fd0c7e2017-07-24 19:35:48 +0000243 case Hexagon::C4_cmpneqi:
Krzysztof Parzyszek3d9946e2016-08-19 17:54:49 +0000244 case Hexagon::C2_cmpgti:
Krzysztof Parzyszek1fd0c7e2017-07-24 19:35:48 +0000245 case Hexagon::C4_cmpltei:
Krzysztof Parzyszek3d9946e2016-08-19 17:54:49 +0000246 Valid = (isUInt<5>(v) || v == -1);
247 break;
248 case Hexagon::C2_cmpgtui:
Krzysztof Parzyszek1fd0c7e2017-07-24 19:35:48 +0000249 case Hexagon::C4_cmplteui:
Krzysztof Parzyszek3d9946e2016-08-19 17:54:49 +0000250 Valid = isUInt<5>(v);
251 break;
252 case Hexagon::S2_tstbit_i:
253 case Hexagon::S4_ntstbit_i:
254 Valid = (v == 0);
255 break;
256 }
257
258 if (!Valid)
Sirish Pande4bd20c52012-05-12 05:10:30 +0000259 return false;
260 }
261
Jyotsna Verma84c47102013-05-06 18:49:23 +0000262 unsigned cmpReg1, cmpOp2 = 0; // cmpOp2 assignment silences compiler warning.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000263 cmpReg1 = MI.getOperand(1).getReg();
Sirish Pande4bd20c52012-05-12 05:10:30 +0000264
265 if (secondReg) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000266 cmpOp2 = MI.getOperand(2).getReg();
Sirish Pande4bd20c52012-05-12 05:10:30 +0000267
Krzysztof Parzyszek3d9946e2016-08-19 17:54:49 +0000268 // If the same register appears as both operands, we cannot generate a new
269 // value compare. Only one operand may use the .new suffix.
270 if (cmpReg1 == cmpOp2)
271 return false;
272
Sirish Pande4bd20c52012-05-12 05:10:30 +0000273 // Make sure that that second register is not from COPY
274 // At machine code level, we don't need this, but if we decide
275 // to move new value jump prior to RA, we would be needing this.
276 MachineRegisterInfo &MRI = MF.getRegInfo();
277 if (secondReg && !TargetRegisterInfo::isPhysicalRegister(cmpOp2)) {
278 MachineInstr *def = MRI.getVRegDef(cmpOp2);
279 if (def->getOpcode() == TargetOpcode::COPY)
280 return false;
281 }
282 }
283
284 // Walk the instructions after the compare (predicate def) to the jump,
285 // and satisfy the following conditions.
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000286 ++II;
287 for (MachineBasicBlock::iterator localII = II; localII != end; ++localII) {
Krzysztof Parzyszek3d9946e2016-08-19 17:54:49 +0000288 if (localII->isDebugValue())
289 continue;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000290
291 // Check 1.
292 // If "common" checks fail, bail out.
293 if (!commonChecksToProhibitNewValueJump(optLocation, localII))
294 return false;
295
296 // Check 2.
297 // If there is a def or use of predicate (result of compare), bail out.
298 if (localII->modifiesRegister(pReg, TRI) ||
299 localII->readsRegister(pReg, TRI))
300 return false;
301
302 // Check 3.
303 // If there is a def of any of the use of the compare (operands of compare),
304 // bail out.
305 // Eg.
306 // p0 = cmp.eq(r2, r0)
307 // r2 = r4
308 // if (p0.new) jump:t .LBB28_3
309 if (localII->modifiesRegister(cmpReg1, TRI) ||
310 (secondReg && localII->modifiesRegister(cmpOp2, TRI)))
311 return false;
312 }
313 return true;
314}
315
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000316// Given a compare operator, return a matching New Value Jump compare operator.
317// Make sure that MI here is included in isNewValueJumpCandidate.
Jyotsna Verma1d297502013-05-02 15:39:30 +0000318static unsigned getNewValueJumpOpcode(MachineInstr *MI, int reg,
319 bool secondRegNewified,
320 MachineBasicBlock *jmpTarget,
321 const MachineBranchProbabilityInfo
322 *MBPI) {
323 bool taken = false;
324 MachineBasicBlock *Src = MI->getParent();
325 const BranchProbability Prediction =
326 MBPI->getEdgeProbability(Src, jmpTarget);
327
328 if (Prediction >= BranchProbability(1,2))
329 taken = true;
330
Sirish Pande4bd20c52012-05-12 05:10:30 +0000331 switch (MI->getOpcode()) {
Colin LeMahieu902157c2014-11-25 18:20:52 +0000332 case Hexagon::C2_cmpeq:
Colin LeMahieu6e3e62f2015-02-05 22:03:32 +0000333 return taken ? Hexagon::J4_cmpeq_t_jumpnv_t
334 : Hexagon::J4_cmpeq_t_jumpnv_nt;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000335
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000336 case Hexagon::C2_cmpeqi:
Sirish Pande4bd20c52012-05-12 05:10:30 +0000337 if (reg >= 0)
Colin LeMahieu6e3e62f2015-02-05 22:03:32 +0000338 return taken ? Hexagon::J4_cmpeqi_t_jumpnv_t
339 : Hexagon::J4_cmpeqi_t_jumpnv_nt;
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000340 return taken ? Hexagon::J4_cmpeqn1_t_jumpnv_t
341 : Hexagon::J4_cmpeqn1_t_jumpnv_nt;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000342
Krzysztof Parzyszek1fd0c7e2017-07-24 19:35:48 +0000343 case Hexagon::C4_cmpneqi:
344 if (reg >= 0)
345 return taken ? Hexagon::J4_cmpeqi_f_jumpnv_t
346 : Hexagon::J4_cmpeqi_f_jumpnv_nt;
347 return taken ? Hexagon::J4_cmpeqn1_f_jumpnv_t :
348 Hexagon::J4_cmpeqn1_f_jumpnv_nt;
349
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000350 case Hexagon::C2_cmpgt:
Sirish Pande4bd20c52012-05-12 05:10:30 +0000351 if (secondRegNewified)
Colin LeMahieu6e3e62f2015-02-05 22:03:32 +0000352 return taken ? Hexagon::J4_cmplt_t_jumpnv_t
353 : Hexagon::J4_cmplt_t_jumpnv_nt;
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000354 return taken ? Hexagon::J4_cmpgt_t_jumpnv_t
355 : Hexagon::J4_cmpgt_t_jumpnv_nt;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000356
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000357 case Hexagon::C2_cmpgti:
Sirish Pande4bd20c52012-05-12 05:10:30 +0000358 if (reg >= 0)
Colin LeMahieu6e3e62f2015-02-05 22:03:32 +0000359 return taken ? Hexagon::J4_cmpgti_t_jumpnv_t
360 : Hexagon::J4_cmpgti_t_jumpnv_nt;
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000361 return taken ? Hexagon::J4_cmpgtn1_t_jumpnv_t
362 : Hexagon::J4_cmpgtn1_t_jumpnv_nt;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000363
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000364 case Hexagon::C2_cmpgtu:
Sirish Pande4bd20c52012-05-12 05:10:30 +0000365 if (secondRegNewified)
Colin LeMahieu6e3e62f2015-02-05 22:03:32 +0000366 return taken ? Hexagon::J4_cmpltu_t_jumpnv_t
367 : Hexagon::J4_cmpltu_t_jumpnv_nt;
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000368 return taken ? Hexagon::J4_cmpgtu_t_jumpnv_t
369 : Hexagon::J4_cmpgtu_t_jumpnv_nt;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000370
Colin LeMahieu6e0f9f82014-11-26 19:43:12 +0000371 case Hexagon::C2_cmpgtui:
Colin LeMahieu6e3e62f2015-02-05 22:03:32 +0000372 return taken ? Hexagon::J4_cmpgtui_t_jumpnv_t
373 : Hexagon::J4_cmpgtui_t_jumpnv_nt;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000374
Ron Liebermane6540e22015-12-08 16:28:32 +0000375 case Hexagon::C4_cmpneq:
376 return taken ? Hexagon::J4_cmpeq_f_jumpnv_t
377 : Hexagon::J4_cmpeq_f_jumpnv_nt;
378
379 case Hexagon::C4_cmplte:
380 if (secondRegNewified)
381 return taken ? Hexagon::J4_cmplt_f_jumpnv_t
382 : Hexagon::J4_cmplt_f_jumpnv_nt;
383 return taken ? Hexagon::J4_cmpgt_f_jumpnv_t
384 : Hexagon::J4_cmpgt_f_jumpnv_nt;
385
386 case Hexagon::C4_cmplteu:
387 if (secondRegNewified)
388 return taken ? Hexagon::J4_cmpltu_f_jumpnv_t
389 : Hexagon::J4_cmpltu_f_jumpnv_nt;
390 return taken ? Hexagon::J4_cmpgtu_f_jumpnv_t
391 : Hexagon::J4_cmpgtu_f_jumpnv_nt;
392
Krzysztof Parzyszek1fd0c7e2017-07-24 19:35:48 +0000393 case Hexagon::C4_cmpltei:
394 if (reg >= 0)
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000395 return taken ? Hexagon::J4_cmpgti_f_jumpnv_t
396 : Hexagon::J4_cmpgti_f_jumpnv_nt;
397 return taken ? Hexagon::J4_cmpgtn1_f_jumpnv_t
398 : Hexagon::J4_cmpgtn1_f_jumpnv_nt;
Krzysztof Parzyszek1fd0c7e2017-07-24 19:35:48 +0000399
400 case Hexagon::C4_cmplteui:
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000401 return taken ? Hexagon::J4_cmpgtui_f_jumpnv_t
402 : Hexagon::J4_cmpgtui_f_jumpnv_nt;
Krzysztof Parzyszek1fd0c7e2017-07-24 19:35:48 +0000403
Sirish Pande4bd20c52012-05-12 05:10:30 +0000404 default:
405 llvm_unreachable("Could not find matching New Value Jump instruction.");
406 }
407 // return *some value* to avoid compiler warning
408 return 0;
409}
410
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000411bool HexagonNewValueJump::isNewValueJumpCandidate(
412 const MachineInstr &MI) const {
413 switch (MI.getOpcode()) {
414 case Hexagon::C2_cmpeq:
415 case Hexagon::C2_cmpeqi:
416 case Hexagon::C2_cmpgt:
417 case Hexagon::C2_cmpgti:
418 case Hexagon::C2_cmpgtu:
419 case Hexagon::C2_cmpgtui:
420 case Hexagon::C4_cmpneq:
Krzysztof Parzyszek1fd0c7e2017-07-24 19:35:48 +0000421 case Hexagon::C4_cmpneqi:
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000422 case Hexagon::C4_cmplte:
423 case Hexagon::C4_cmplteu:
Krzysztof Parzyszek1fd0c7e2017-07-24 19:35:48 +0000424 case Hexagon::C4_cmpltei:
425 case Hexagon::C4_cmplteui:
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000426 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000427
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000428 default:
429 return false;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000430 }
431}
432
Sirish Pande4bd20c52012-05-12 05:10:30 +0000433bool HexagonNewValueJump::runOnMachineFunction(MachineFunction &MF) {
Sirish Pande4bd20c52012-05-12 05:10:30 +0000434 DEBUG(dbgs() << "********** Hexagon New Value Jump **********\n"
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000435 << "********** Function: " << MF.getName() << "\n");
Sirish Pande4bd20c52012-05-12 05:10:30 +0000436
Andrew Kaylor5b444a22016-04-26 19:46:28 +0000437 if (skipFunction(*MF.getFunction()))
438 return false;
439
Eric Christopher0fef34e2015-02-02 22:11:42 +0000440 // If we move NewValueJump before register allocation we'll need live variable
441 // analysis here too.
Sirish Pande4bd20c52012-05-12 05:10:30 +0000442
Eric Christopherfc6de422014-08-05 02:39:49 +0000443 QII = static_cast<const HexagonInstrInfo *>(MF.getSubtarget().getInstrInfo());
Eric Christopherd9134482014-08-04 21:25:23 +0000444 QRI = static_cast<const HexagonRegisterInfo *>(
Eric Christopherfc6de422014-08-05 02:39:49 +0000445 MF.getSubtarget().getRegisterInfo());
Jyotsna Verma1d297502013-05-02 15:39:30 +0000446 MBPI = &getAnalysis<MachineBranchProbabilityInfo>();
Sirish Pande4bd20c52012-05-12 05:10:30 +0000447
Colin LeMahieu4fd203d2015-02-09 21:56:37 +0000448 if (DisableNewValueJumps) {
Sirish Pande4bd20c52012-05-12 05:10:30 +0000449 return false;
450 }
451
452 int nvjCount = DbgNVJCount;
453 int nvjGenerated = 0;
454
455 // Loop through all the bb's of the function
456 for (MachineFunction::iterator MBBb = MF.begin(), MBBe = MF.end();
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000457 MBBb != MBBe; ++MBBb) {
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000458 MachineBasicBlock *MBB = &*MBBb;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000459
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000460 DEBUG(dbgs() << "** dumping bb ** " << MBB->getNumber() << "\n");
Sirish Pande4bd20c52012-05-12 05:10:30 +0000461 DEBUG(MBB->dump());
462 DEBUG(dbgs() << "\n" << "********** dumping instr bottom up **********\n");
463 bool foundJump = false;
464 bool foundCompare = false;
465 bool invertPredicate = false;
466 unsigned predReg = 0; // predicate reg of the jump.
467 unsigned cmpReg1 = 0;
468 int cmpOp2 = 0;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000469 MachineBasicBlock::iterator jmpPos;
470 MachineBasicBlock::iterator cmpPos;
Craig Topper062a2ba2014-04-25 05:30:21 +0000471 MachineInstr *cmpInstr = nullptr, *jmpInstr = nullptr;
472 MachineBasicBlock *jmpTarget = nullptr;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000473 bool afterRA = false;
474 bool isSecondOpReg = false;
475 bool isSecondOpNewified = false;
476 // Traverse the basic block - bottom up
477 for (MachineBasicBlock::iterator MII = MBB->end(), E = MBB->begin();
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000478 MII != E;) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000479 MachineInstr &MI = *--MII;
480 if (MI.isDebugValue()) {
Sirish Pande4bd20c52012-05-12 05:10:30 +0000481 continue;
482 }
483
484 if ((nvjCount == 0) || (nvjCount > -1 && nvjCount <= nvjGenerated))
485 break;
486
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000487 DEBUG(dbgs() << "Instr: "; MI.dump(); dbgs() << "\n");
Sirish Pande4bd20c52012-05-12 05:10:30 +0000488
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000489 if (!foundJump && (MI.getOpcode() == Hexagon::J2_jumpt ||
Krzysztof Parzyszeka243adf2016-08-19 14:14:09 +0000490 MI.getOpcode() == Hexagon::J2_jumptpt ||
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000491 MI.getOpcode() == Hexagon::J2_jumpf ||
Krzysztof Parzyszeka243adf2016-08-19 14:14:09 +0000492 MI.getOpcode() == Hexagon::J2_jumpfpt ||
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000493 MI.getOpcode() == Hexagon::J2_jumptnewpt ||
494 MI.getOpcode() == Hexagon::J2_jumptnew ||
495 MI.getOpcode() == Hexagon::J2_jumpfnewpt ||
496 MI.getOpcode() == Hexagon::J2_jumpfnew)) {
Sirish Pande4bd20c52012-05-12 05:10:30 +0000497 // This is where you would insert your compare and
498 // instr that feeds compare
499 jmpPos = MII;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000500 jmpInstr = &MI;
501 predReg = MI.getOperand(0).getReg();
Sirish Pande4bd20c52012-05-12 05:10:30 +0000502 afterRA = TargetRegisterInfo::isPhysicalRegister(predReg);
503
504 // If ifconverter had not messed up with the kill flags of the
505 // operands, the following check on the kill flag would suffice.
506 // if(!jmpInstr->getOperand(0).isKill()) break;
507
508 // This predicate register is live out out of BB
509 // this would only work if we can actually use Live
510 // variable analysis on phy regs - but LLVM does not
511 // provide LV analysis on phys regs.
512 //if(LVs.isLiveOut(predReg, *MBB)) break;
513
514 // Get all the successors of this block - which will always
Krzysztof Parzyszek3d9946e2016-08-19 17:54:49 +0000515 // be 2. Check if the predicate register is live-in in those
Sirish Pande4bd20c52012-05-12 05:10:30 +0000516 // successor. If yes, we can not delete the predicate -
517 // I am doing this only because LLVM does not provide LiveOut
518 // at the BB level.
519 bool predLive = false;
520 for (MachineBasicBlock::const_succ_iterator SI = MBB->succ_begin(),
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000521 SIE = MBB->succ_end();
522 SI != SIE; ++SI) {
523 MachineBasicBlock *succMBB = *SI;
524 if (succMBB->isLiveIn(predReg))
Sirish Pande4bd20c52012-05-12 05:10:30 +0000525 predLive = true;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000526 }
527 if (predLive)
528 break;
529
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000530 if (!MI.getOperand(1).isMBB())
Krzysztof Parzyszekb28ae102016-01-14 15:05:27 +0000531 continue;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000532 jmpTarget = MI.getOperand(1).getMBB();
Sirish Pande4bd20c52012-05-12 05:10:30 +0000533 foundJump = true;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000534 if (MI.getOpcode() == Hexagon::J2_jumpf ||
535 MI.getOpcode() == Hexagon::J2_jumpfnewpt ||
536 MI.getOpcode() == Hexagon::J2_jumpfnew) {
Sirish Pande4bd20c52012-05-12 05:10:30 +0000537 invertPredicate = true;
538 }
539 continue;
540 }
541
542 // No new value jump if there is a barrier. A barrier has to be in its
543 // own packet. A barrier has zero operands. We conservatively bail out
544 // here if we see any instruction with zero operands.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000545 if (foundJump && MI.getNumOperands() == 0)
Sirish Pande4bd20c52012-05-12 05:10:30 +0000546 break;
547
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000548 if (foundJump && !foundCompare && MI.getOperand(0).isReg() &&
549 MI.getOperand(0).getReg() == predReg) {
Sirish Pande4bd20c52012-05-12 05:10:30 +0000550 // Not all compares can be new value compare. Arch Spec: 7.6.1.1
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000551 if (isNewValueJumpCandidate(MI)) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000552 assert(
553 (MI.getDesc().isCompare()) &&
Sirish Pande4bd20c52012-05-12 05:10:30 +0000554 "Only compare instruction can be collapsed into New Value Jump");
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000555 isSecondOpReg = MI.getOperand(2).isReg();
Sirish Pande4bd20c52012-05-12 05:10:30 +0000556
557 if (!canCompareBeNewValueJump(QII, QRI, MII, predReg, isSecondOpReg,
558 afterRA, jmpPos, MF))
559 break;
560
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000561 cmpInstr = &MI;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000562 cmpPos = MII;
563 foundCompare = true;
564
565 // We need cmpReg1 and cmpOp2(imm or reg) while building
566 // new value jump instruction.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000567 cmpReg1 = MI.getOperand(1).getReg();
Sirish Pande4bd20c52012-05-12 05:10:30 +0000568
Krzysztof Parzyszek5ddd2e52017-06-27 18:37:16 +0000569 if (isSecondOpReg)
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000570 cmpOp2 = MI.getOperand(2).getReg();
Krzysztof Parzyszek5ddd2e52017-06-27 18:37:16 +0000571 else
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000572 cmpOp2 = MI.getOperand(2).getImm();
Sirish Pande4bd20c52012-05-12 05:10:30 +0000573 continue;
574 }
575 }
576
577 if (foundCompare && foundJump) {
Sirish Pande4bd20c52012-05-12 05:10:30 +0000578 // If "common" checks fail, bail out on this BB.
579 if (!commonChecksToProhibitNewValueJump(afterRA, MII))
580 break;
581
582 bool foundFeeder = false;
583 MachineBasicBlock::iterator feederPos = MII;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000584 if (MI.getOperand(0).isReg() && MI.getOperand(0).isDef() &&
585 (MI.getOperand(0).getReg() == cmpReg1 ||
586 (isSecondOpReg &&
587 MI.getOperand(0).getReg() == (unsigned)cmpOp2))) {
Sirish Pande4bd20c52012-05-12 05:10:30 +0000588
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000589 unsigned feederReg = MI.getOperand(0).getReg();
Sirish Pande4bd20c52012-05-12 05:10:30 +0000590
591 // First try to see if we can get the feeder from the first operand
592 // of the compare. If we can not, and if secondOpReg is true
593 // (second operand of the compare is also register), try that one.
594 // TODO: Try to come up with some heuristic to figure out which
595 // feeder would benefit.
596
597 if (feederReg == cmpReg1) {
598 if (!canBeFeederToNewValueJump(QII, QRI, MII, jmpPos, cmpPos, MF)) {
599 if (!isSecondOpReg)
600 break;
601 else
602 continue;
603 } else
604 foundFeeder = true;
605 }
606
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000607 if (!foundFeeder && isSecondOpReg && feederReg == (unsigned)cmpOp2)
Sirish Pande4bd20c52012-05-12 05:10:30 +0000608 if (!canBeFeederToNewValueJump(QII, QRI, MII, jmpPos, cmpPos, MF))
609 break;
610
611 if (isSecondOpReg) {
612 // In case of CMPLT, or CMPLTU, or EQ with the second register
613 // to newify, swap the operands.
Krzysztof Parzyszek3d9946e2016-08-19 17:54:49 +0000614 unsigned COp = cmpInstr->getOpcode();
615 if ((COp == Hexagon::C2_cmpeq || COp == Hexagon::C4_cmpneq) &&
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000616 (feederReg == (unsigned)cmpOp2)) {
Sirish Pande4bd20c52012-05-12 05:10:30 +0000617 unsigned tmp = cmpReg1;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000618 cmpReg1 = cmpOp2;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000619 cmpOp2 = tmp;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000620 }
621
622 // Now we have swapped the operands, all we need to check is,
623 // if the second operand (after swap) is the feeder.
624 // And if it is, make a note.
625 if (feederReg == (unsigned)cmpOp2)
626 isSecondOpNewified = true;
627 }
628
629 // Now that we are moving feeder close the jump,
630 // make sure we are respecting the kill values of
631 // the operands of the feeder.
632
Krzysztof Parzyszek5ddd2e52017-06-27 18:37:16 +0000633 auto TransferKills = [jmpPos,cmpPos] (MachineInstr &MI) {
634 for (MachineOperand &MO : MI.operands()) {
635 if (!MO.isReg() || !MO.isUse())
636 continue;
637 unsigned UseR = MO.getReg();
638 for (auto I = std::next(MI.getIterator()); I != jmpPos; ++I) {
639 if (I == cmpPos)
640 continue;
641 for (MachineOperand &Op : I->operands()) {
642 if (!Op.isReg() || !Op.isUse() || !Op.isKill())
643 continue;
644 if (Op.getReg() != UseR)
645 continue;
646 // We found that there is kill of a use register
647 // Set up a kill flag on the register
648 Op.setIsKill(false);
649 MO.setIsKill(true);
650 return;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000651 }
Sirish Pande4bd20c52012-05-12 05:10:30 +0000652 }
653 }
Krzysztof Parzyszek5ddd2e52017-06-27 18:37:16 +0000654 };
655
656 TransferKills(*feederPos);
657 TransferKills(*cmpPos);
658 bool MO1IsKill = cmpPos->killsRegister(cmpReg1, QRI);
659 bool MO2IsKill = isSecondOpReg && cmpPos->killsRegister(cmpOp2, QRI);
Sirish Pande4bd20c52012-05-12 05:10:30 +0000660
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000661 MBB->splice(jmpPos, MI.getParent(), MI);
662 MBB->splice(jmpPos, MI.getParent(), cmpInstr);
663 DebugLoc dl = MI.getDebugLoc();
Sirish Pande4bd20c52012-05-12 05:10:30 +0000664 MachineInstr *NewMI;
665
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000666 assert((isNewValueJumpCandidate(*cmpInstr)) &&
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000667 "This compare is not a New Value Jump candidate.");
Sirish Pande4bd20c52012-05-12 05:10:30 +0000668 unsigned opc = getNewValueJumpOpcode(cmpInstr, cmpOp2,
Jyotsna Verma1d297502013-05-02 15:39:30 +0000669 isSecondOpNewified,
670 jmpTarget, MBPI);
Sirish Pande4bd20c52012-05-12 05:10:30 +0000671 if (invertPredicate)
672 opc = QII->getInvertedPredicatedOpcode(opc);
673
Jyotsna Verma89c84822013-04-23 19:15:55 +0000674 if (isSecondOpReg)
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000675 NewMI = BuildMI(*MBB, jmpPos, dl, QII->get(opc))
676 .addReg(cmpReg1, getKillRegState(MO1IsKill))
677 .addReg(cmpOp2, getKillRegState(MO2IsKill))
678 .addMBB(jmpTarget);
Jyotsna Verma89c84822013-04-23 19:15:55 +0000679
Jyotsna Verma89c84822013-04-23 19:15:55 +0000680 else
Krzysztof Parzyszekcfd88062017-07-28 21:52:21 +0000681 NewMI = BuildMI(*MBB, jmpPos, dl, QII->get(opc))
682 .addReg(cmpReg1, getKillRegState(MO1IsKill))
683 .addImm(cmpOp2)
684 .addMBB(jmpTarget);
Sirish Pande4bd20c52012-05-12 05:10:30 +0000685
686 assert(NewMI && "New Value Jump Instruction Not created!");
Duncan Sands0480b9b2013-05-13 07:50:47 +0000687 (void)NewMI;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000688 if (cmpInstr->getOperand(0).isReg() &&
689 cmpInstr->getOperand(0).isKill())
690 cmpInstr->getOperand(0).setIsKill(false);
691 if (cmpInstr->getOperand(1).isReg() &&
692 cmpInstr->getOperand(1).isKill())
693 cmpInstr->getOperand(1).setIsKill(false);
694 cmpInstr->eraseFromParent();
695 jmpInstr->eraseFromParent();
696 ++nvjGenerated;
697 ++NumNVJGenerated;
698 break;
699 }
700 }
701 }
702 }
703
704 return true;
Sirish Pande4bd20c52012-05-12 05:10:30 +0000705}
706
707FunctionPass *llvm::createHexagonNewValueJump() {
708 return new HexagonNewValueJump();
709}