Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Target/Hexagon/MCTargetDesc/HexagonMCCompound.cpp
Line
Count
Source (jump to first uncovered line)
1
//=== HexagonMCCompound.cpp - Hexagon Compound checker  -------------------===//
2
//
3
// 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
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This file is looks at a packet and tries to form compound insns
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "MCTargetDesc/HexagonBaseInfo.h"
14
#include "MCTargetDesc/HexagonMCInstrInfo.h"
15
#include "MCTargetDesc/HexagonMCShuffler.h"
16
#include "llvm/MC/MCContext.h"
17
#include "llvm/MC/MCInst.h"
18
#include "llvm/Support/Debug.h"
19
#include "llvm/Support/ErrorHandling.h"
20
#include "llvm/Support/raw_ostream.h"
21
#include <cassert>
22
#include <cstdint>
23
24
using namespace llvm;
25
using namespace Hexagon;
26
27
#define DEBUG_TYPE "hexagon-mccompound"
28
29
enum OpcodeIndex {
30
  fp0_jump_nt = 0,
31
  fp0_jump_t,
32
  fp1_jump_nt,
33
  fp1_jump_t,
34
  tp0_jump_nt,
35
  tp0_jump_t,
36
  tp1_jump_nt,
37
  tp1_jump_t
38
};
39
40
static const unsigned tstBitOpcode[8] = {
41
    J4_tstbit0_fp0_jump_nt, J4_tstbit0_fp0_jump_t,  J4_tstbit0_fp1_jump_nt,
42
    J4_tstbit0_fp1_jump_t,  J4_tstbit0_tp0_jump_nt, J4_tstbit0_tp0_jump_t,
43
    J4_tstbit0_tp1_jump_nt, J4_tstbit0_tp1_jump_t};
44
static const unsigned cmpeqBitOpcode[8] = {
45
    J4_cmpeq_fp0_jump_nt, J4_cmpeq_fp0_jump_t,  J4_cmpeq_fp1_jump_nt,
46
    J4_cmpeq_fp1_jump_t,  J4_cmpeq_tp0_jump_nt, J4_cmpeq_tp0_jump_t,
47
    J4_cmpeq_tp1_jump_nt, J4_cmpeq_tp1_jump_t};
48
static const unsigned cmpgtBitOpcode[8] = {
49
    J4_cmpgt_fp0_jump_nt, J4_cmpgt_fp0_jump_t,  J4_cmpgt_fp1_jump_nt,
50
    J4_cmpgt_fp1_jump_t,  J4_cmpgt_tp0_jump_nt, J4_cmpgt_tp0_jump_t,
51
    J4_cmpgt_tp1_jump_nt, J4_cmpgt_tp1_jump_t};
52
static const unsigned cmpgtuBitOpcode[8] = {
53
    J4_cmpgtu_fp0_jump_nt, J4_cmpgtu_fp0_jump_t,  J4_cmpgtu_fp1_jump_nt,
54
    J4_cmpgtu_fp1_jump_t,  J4_cmpgtu_tp0_jump_nt, J4_cmpgtu_tp0_jump_t,
55
    J4_cmpgtu_tp1_jump_nt, J4_cmpgtu_tp1_jump_t};
56
static const unsigned cmpeqiBitOpcode[8] = {
57
    J4_cmpeqi_fp0_jump_nt, J4_cmpeqi_fp0_jump_t,  J4_cmpeqi_fp1_jump_nt,
58
    J4_cmpeqi_fp1_jump_t,  J4_cmpeqi_tp0_jump_nt, J4_cmpeqi_tp0_jump_t,
59
    J4_cmpeqi_tp1_jump_nt, J4_cmpeqi_tp1_jump_t};
60
static const unsigned cmpgtiBitOpcode[8] = {
61
    J4_cmpgti_fp0_jump_nt, J4_cmpgti_fp0_jump_t,  J4_cmpgti_fp1_jump_nt,
62
    J4_cmpgti_fp1_jump_t,  J4_cmpgti_tp0_jump_nt, J4_cmpgti_tp0_jump_t,
63
    J4_cmpgti_tp1_jump_nt, J4_cmpgti_tp1_jump_t};
64
static const unsigned cmpgtuiBitOpcode[8] = {
65
    J4_cmpgtui_fp0_jump_nt, J4_cmpgtui_fp0_jump_t,  J4_cmpgtui_fp1_jump_nt,
66
    J4_cmpgtui_fp1_jump_t,  J4_cmpgtui_tp0_jump_nt, J4_cmpgtui_tp0_jump_t,
67
    J4_cmpgtui_tp1_jump_nt, J4_cmpgtui_tp1_jump_t};
68
static const unsigned cmpeqn1BitOpcode[8] = {
69
    J4_cmpeqn1_fp0_jump_nt, J4_cmpeqn1_fp0_jump_t,  J4_cmpeqn1_fp1_jump_nt,
70
    J4_cmpeqn1_fp1_jump_t,  J4_cmpeqn1_tp0_jump_nt, J4_cmpeqn1_tp0_jump_t,
71
    J4_cmpeqn1_tp1_jump_nt, J4_cmpeqn1_tp1_jump_t};
72
static const unsigned cmpgtn1BitOpcode[8] = {
73
    J4_cmpgtn1_fp0_jump_nt, J4_cmpgtn1_fp0_jump_t,  J4_cmpgtn1_fp1_jump_nt,
74
    J4_cmpgtn1_fp1_jump_t,  J4_cmpgtn1_tp0_jump_nt, J4_cmpgtn1_tp0_jump_t,
75
    J4_cmpgtn1_tp1_jump_nt, J4_cmpgtn1_tp1_jump_t,
76
};
77
78
// enum HexagonII::CompoundGroup
79
9.07k
static unsigned getCompoundCandidateGroup(MCInst const &MI, bool IsExtended) {
80
9.07k
  unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
81
9.07k
82
9.07k
  switch (MI.getOpcode()) {
83
9.07k
  default:
84
7.90k
    return HexagonII::HCG_None;
85
9.07k
  //
86
9.07k
  // Compound pairs.
87
9.07k
  // "p0=cmp.eq(Rs16,Rt16); if (p0.new) jump:nt #r9:2"
88
9.07k
  // "Rd16=#U6 ; jump #r9:2"
89
9.07k
  // "Rd16=Rs16 ; jump #r9:2"
90
9.07k
  //
91
9.07k
  case Hexagon::C2_cmpeq:
92
97
  case Hexagon::C2_cmpgt:
93
97
  case Hexagon::C2_cmpgtu:
94
97
    if (IsExtended)
95
0
      return false;
96
97
    DstReg = MI.getOperand(0).getReg();
97
97
    Src1Reg = MI.getOperand(1).getReg();
98
97
    Src2Reg = MI.getOperand(2).getReg();
99
97
    if ((Hexagon::P0 == DstReg || 
Hexagon::P1 == DstReg18
) &&
100
97
        
HexagonMCInstrInfo::isIntRegForSubInst(Src1Reg)93
&&
101
97
        
HexagonMCInstrInfo::isIntRegForSubInst(Src2Reg)90
)
102
89
      return HexagonII::HCG_A;
103
8
    break;
104
243
  case Hexagon::C2_cmpeqi:
105
243
  case Hexagon::C2_cmpgti:
106
243
  case Hexagon::C2_cmpgtui:
107
243
    if (IsExtended)
108
36
      return false;
109
207
    // P0 = cmp.eq(Rs,#u2)
110
207
    DstReg = MI.getOperand(0).getReg();
111
207
    SrcReg = MI.getOperand(1).getReg();
112
207
    if ((Hexagon::P0 == DstReg || 
Hexagon::P1 == DstReg24
) &&
113
207
        
HexagonMCInstrInfo::isIntRegForSubInst(SrcReg)206
&&
114
207
        
(203
HexagonMCInstrInfo::inRange<5>(MI, 2)203
||
115
203
         
HexagonMCInstrInfo::minConstant(MI, 2) == -133
))
116
192
      return HexagonII::HCG_A;
117
15
    break;
118
151
  case Hexagon::A2_tfr:
119
151
    if (IsExtended)
120
4
      return false;
121
147
    // Rd = Rs
122
147
    DstReg = MI.getOperand(0).getReg();
123
147
    SrcReg = MI.getOperand(1).getReg();
124
147
    if (HexagonMCInstrInfo::isIntRegForSubInst(DstReg) &&
125
147
        
HexagonMCInstrInfo::isIntRegForSubInst(SrcReg)139
)
126
138
      return HexagonII::HCG_A;
127
9
    break;
128
203
  case Hexagon::A2_tfrsi:
129
203
    if (IsExtended)
130
58
      return false;
131
145
    // Rd = #u6
132
145
    DstReg = MI.getOperand(0).getReg();
133
145
    if (HexagonMCInstrInfo::minConstant(MI, 1) <= 63 &&
134
145
        
HexagonMCInstrInfo::minConstant(MI, 1) >= 0134
&&
135
145
        
HexagonMCInstrInfo::isIntRegForSubInst(DstReg)129
)
136
127
      return HexagonII::HCG_A;
137
18
    break;
138
18
  case Hexagon::S2_tstbit_i:
139
15
    if (IsExtended)
140
0
      return false;
141
15
    DstReg = MI.getOperand(0).getReg();
142
15
    Src1Reg = MI.getOperand(1).getReg();
143
15
    if ((Hexagon::P0 == DstReg || 
Hexagon::P1 == DstReg4
) &&
144
15
        HexagonMCInstrInfo::isIntRegForSubInst(Src1Reg) &&
145
15
        HexagonMCInstrInfo::minConstant(MI, 2) == 0)
146
15
      return HexagonII::HCG_A;
147
0
    break;
148
0
  // The fact that .new form is used pretty much guarantees
149
0
  // that predicate register will match. Nevertheless,
150
0
  // there could be some false positives without additional
151
0
  // checking.
152
281
  case Hexagon::J2_jumptnew:
153
281
  case Hexagon::J2_jumpfnew:
154
281
  case Hexagon::J2_jumptnewpt:
155
281
  case Hexagon::J2_jumpfnewpt:
156
281
    Src1Reg = MI.getOperand(0).getReg();
157
281
    if (Hexagon::P0 == Src1Reg || 
Hexagon::P1 == Src1Reg49
)
158
276
      return HexagonII::HCG_B;
159
5
    break;
160
5
  // Transfer and jump:
161
5
  // Rd=#U6 ; jump #r9:2
162
5
  // Rd=Rs ; jump #r9:2
163
5
  // Do not test for jump range here.
164
181
  case Hexagon::J2_jump:
165
181
  case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
166
181
    return HexagonII::HCG_C;
167
181
    
break0
;
168
55
  }
169
55
170
55
  return HexagonII::HCG_None;
171
55
}
172
173
/// getCompoundOp - Return the index from 0-7 into the above opcode lists.
174
195
static unsigned getCompoundOp(MCInst const &HMCI) {
175
195
  const MCOperand &Predicate = HMCI.getOperand(0);
176
195
  unsigned PredReg = Predicate.getReg();
177
195
178
195
  assert((PredReg == Hexagon::P0) || (PredReg == Hexagon::P1) ||
179
195
         (PredReg == Hexagon::P2) || (PredReg == Hexagon::P3));
180
195
181
195
  switch (HMCI.getOpcode()) {
182
195
  default:
183
0
    llvm_unreachable("Expected match not found.\n");
184
195
    
break0
;
185
195
  case Hexagon::J2_jumpfnew:
186
40
    return (PredReg == Hexagon::P0) ? 
fp0_jump_nt31
:
fp1_jump_nt9
;
187
195
  case Hexagon::J2_jumpfnewpt:
188
65
    return (PredReg == Hexagon::P0) ? 
fp0_jump_t56
:
fp1_jump_t9
;
189
195
  case Hexagon::J2_jumptnew:
190
56
    return (PredReg == Hexagon::P0) ? 
tp0_jump_nt46
:
tp1_jump_nt10
;
191
195
  case Hexagon::J2_jumptnewpt:
192
34
    return (PredReg == Hexagon::P0) ? 
tp0_jump_t24
:
tp1_jump_t10
;
193
195
  }
194
195
}
195
196
static MCInst *getCompoundInsn(MCContext &Context, MCInst const &L,
197
230
                               MCInst const &R) {
198
230
  MCInst *CompoundInsn = nullptr;
199
230
  unsigned compoundOpcode;
200
230
  MCOperand Rs, Rt;
201
230
  int64_t Value;
202
230
  bool Success;
203
230
204
230
  switch (L.getOpcode()) {
205
230
  default:
206
0
    LLVM_DEBUG(dbgs() << "Possible compound ignored\n");
207
0
    return CompoundInsn;
208
230
209
230
  case Hexagon::A2_tfrsi:
210
22
    Rt = L.getOperand(0);
211
22
    compoundOpcode = J4_jumpseti;
212
22
    CompoundInsn = new (Context) MCInst;
213
22
    CompoundInsn->setOpcode(compoundOpcode);
214
22
215
22
    CompoundInsn->addOperand(Rt);
216
22
    CompoundInsn->addOperand(L.getOperand(1)); // Immediate
217
22
    CompoundInsn->addOperand(R.getOperand(0)); // Jump target
218
22
    break;
219
230
220
230
  case Hexagon::A2_tfr:
221
13
    Rt = L.getOperand(0);
222
13
    Rs = L.getOperand(1);
223
13
224
13
    compoundOpcode = J4_jumpsetr;
225
13
    CompoundInsn = new (Context) MCInst;
226
13
    CompoundInsn->setOpcode(compoundOpcode);
227
13
    CompoundInsn->addOperand(Rt);
228
13
    CompoundInsn->addOperand(Rs);
229
13
    CompoundInsn->addOperand(R.getOperand(0)); // Jump target.
230
13
231
13
    break;
232
230
233
230
  case Hexagon::C2_cmpeq:
234
19
    LLVM_DEBUG(dbgs() << "CX: C2_cmpeq\n");
235
19
    Rs = L.getOperand(1);
236
19
    Rt = L.getOperand(2);
237
19
238
19
    compoundOpcode = cmpeqBitOpcode[getCompoundOp(R)];
239
19
    CompoundInsn = new (Context) MCInst;
240
19
    CompoundInsn->setOpcode(compoundOpcode);
241
19
    CompoundInsn->addOperand(Rs);
242
19
    CompoundInsn->addOperand(Rt);
243
19
    CompoundInsn->addOperand(R.getOperand(1));
244
19
    break;
245
230
246
230
  case Hexagon::C2_cmpgt:
247
14
    LLVM_DEBUG(dbgs() << "CX: C2_cmpgt\n");
248
14
    Rs = L.getOperand(1);
249
14
    Rt = L.getOperand(2);
250
14
251
14
    compoundOpcode = cmpgtBitOpcode[getCompoundOp(R)];
252
14
    CompoundInsn = new (Context) MCInst;
253
14
    CompoundInsn->setOpcode(compoundOpcode);
254
14
    CompoundInsn->addOperand(Rs);
255
14
    CompoundInsn->addOperand(Rt);
256
14
    CompoundInsn->addOperand(R.getOperand(1));
257
14
    break;
258
230
259
230
  case Hexagon::C2_cmpgtu:
260
11
    LLVM_DEBUG(dbgs() << "CX: C2_cmpgtu\n");
261
11
    Rs = L.getOperand(1);
262
11
    Rt = L.getOperand(2);
263
11
264
11
    compoundOpcode = cmpgtuBitOpcode[getCompoundOp(R)];
265
11
    CompoundInsn = new (Context) MCInst;
266
11
    CompoundInsn->setOpcode(compoundOpcode);
267
11
    CompoundInsn->addOperand(Rs);
268
11
    CompoundInsn->addOperand(Rt);
269
11
    CompoundInsn->addOperand(R.getOperand(1));
270
11
    break;
271
230
272
230
  case Hexagon::C2_cmpeqi:
273
67
    LLVM_DEBUG(dbgs() << "CX: C2_cmpeqi\n");
274
67
    Success = L.getOperand(2).getExpr()->evaluateAsAbsolute(Value);
275
67
    (void)Success;
276
67
    assert(Success);
277
67
    if (Value == -1)
278
8
      compoundOpcode = cmpeqn1BitOpcode[getCompoundOp(R)];
279
59
    else
280
59
      compoundOpcode = cmpeqiBitOpcode[getCompoundOp(R)];
281
67
282
67
    Rs = L.getOperand(1);
283
67
    CompoundInsn = new (Context) MCInst;
284
67
    CompoundInsn->setOpcode(compoundOpcode);
285
67
    CompoundInsn->addOperand(Rs);
286
67
    CompoundInsn->addOperand(L.getOperand(2));
287
67
    CompoundInsn->addOperand(R.getOperand(1));
288
67
    break;
289
230
290
230
  case Hexagon::C2_cmpgti:
291
43
    LLVM_DEBUG(dbgs() << "CX: C2_cmpgti\n");
292
43
    Success = L.getOperand(2).getExpr()->evaluateAsAbsolute(Value);
293
43
    (void)Success;
294
43
    assert(Success);
295
43
    if (Value == -1)
296
12
      compoundOpcode = cmpgtn1BitOpcode[getCompoundOp(R)];
297
31
    else
298
31
      compoundOpcode = cmpgtiBitOpcode[getCompoundOp(R)];
299
43
300
43
    Rs = L.getOperand(1);
301
43
    CompoundInsn = new (Context) MCInst;
302
43
    CompoundInsn->setOpcode(compoundOpcode);
303
43
    CompoundInsn->addOperand(Rs);
304
43
    CompoundInsn->addOperand(L.getOperand(2));
305
43
    CompoundInsn->addOperand(R.getOperand(1));
306
43
    break;
307
230
308
230
  case Hexagon::C2_cmpgtui:
309
29
    LLVM_DEBUG(dbgs() << "CX: C2_cmpgtui\n");
310
29
    Rs = L.getOperand(1);
311
29
    compoundOpcode = cmpgtuiBitOpcode[getCompoundOp(R)];
312
29
    CompoundInsn = new (Context) MCInst;
313
29
    CompoundInsn->setOpcode(compoundOpcode);
314
29
    CompoundInsn->addOperand(Rs);
315
29
    CompoundInsn->addOperand(L.getOperand(2));
316
29
    CompoundInsn->addOperand(R.getOperand(1));
317
29
    break;
318
230
319
230
  case Hexagon::S2_tstbit_i:
320
12
    LLVM_DEBUG(dbgs() << "CX: S2_tstbit_i\n");
321
12
    Rs = L.getOperand(1);
322
12
    compoundOpcode = tstBitOpcode[getCompoundOp(R)];
323
12
    CompoundInsn = new (Context) MCInst;
324
12
    CompoundInsn->setOpcode(compoundOpcode);
325
12
    CompoundInsn->addOperand(Rs);
326
12
    CompoundInsn->addOperand(R.getOperand(1));
327
12
    break;
328
230
  }
329
230
330
230
  return CompoundInsn;
331
230
}
332
333
/// Non-Symmetrical. See if these two instructions are fit for compound pair.
334
static bool isOrderedCompoundPair(MCInst const &MIa, bool IsExtendedA,
335
4.53k
                                  MCInst const &MIb, bool IsExtendedB) {
336
4.53k
  unsigned MIaG = getCompoundCandidateGroup(MIa, IsExtendedA);
337
4.53k
  unsigned MIbG = getCompoundCandidateGroup(MIb, IsExtendedB);
338
4.53k
  // We have two candidates - check that this is the same register
339
4.53k
  // we are talking about.
340
4.53k
  unsigned Opca = MIa.getOpcode();
341
4.53k
  if (MIaG == HexagonII::HCG_A && 
MIbG == HexagonII::HCG_C561
&&
342
4.53k
      
(39
Opca == Hexagon::A2_tfr39
||
Opca == Hexagon::A2_tfrsi26
))
343
35
    return true;
344
4.50k
  return ((MIaG == HexagonII::HCG_A && 
MIbG == HexagonII::HCG_B526
) &&
345
4.50k
          
(MIa.getOperand(0).getReg() == MIb.getOperand(0).getReg())204
);
346
4.50k
}
347
348
static bool lookForCompound(MCInstrInfo const &MCII, MCContext &Context,
349
23.5k
                            MCInst &MCI) {
350
23.5k
  assert(HexagonMCInstrInfo::isBundle(MCI));
351
23.5k
  bool JExtended = false;
352
23.5k
  for (MCInst::iterator J =
353
23.5k
           MCI.begin() + HexagonMCInstrInfo::bundleInstructionsOffset;
354
61.7k
       J != MCI.end(); 
++J38.1k
) {
355
38.4k
    MCInst const *JumpInst = J->getInst();
356
38.4k
    if (HexagonMCInstrInfo::isImmext(*JumpInst)) {
357
2.00k
      JExtended = true;
358
2.00k
      continue;
359
2.00k
    }
360
36.4k
    if (HexagonMCInstrInfo::getType(MCII, *JumpInst) == HexagonII::TypeJ) {
361
4.80k
      // Try to pair with another insn (B)undled with jump.
362
4.80k
      bool BExtended = false;
363
4.80k
      for (MCInst::iterator B =
364
4.80k
               MCI.begin() + HexagonMCInstrInfo::bundleInstructionsOffset;
365
14.1k
           B != MCI.end(); 
++B9.32k
) {
366
9.55k
        MCInst const *Inst = B->getInst();
367
9.55k
        if (JumpInst == Inst)
368
4.79k
          continue;
369
4.76k
        if (HexagonMCInstrInfo::isImmext(*Inst)) {
370
228
          BExtended = true;
371
228
          continue;
372
228
        }
373
4.53k
        LLVM_DEBUG(dbgs() << "J,B: " << JumpInst->getOpcode() << ","
374
4.53k
                          << Inst->getOpcode() << "\n");
375
4.53k
        if (isOrderedCompoundPair(*Inst, BExtended, *JumpInst, JExtended)) {
376
230
          MCInst *CompoundInsn = getCompoundInsn(Context, *Inst, *JumpInst);
377
230
          if (CompoundInsn) {
378
230
            LLVM_DEBUG(dbgs() << "B: " << Inst->getOpcode() << ","
379
230
                              << JumpInst->getOpcode() << " Compounds to "
380
230
                              << CompoundInsn->getOpcode() << "\n");
381
230
            J->setInst(CompoundInsn);
382
230
            MCI.erase(B);
383
230
            return true;
384
230
          }
385
4.30k
        }
386
4.30k
        BExtended = false;
387
4.30k
      }
388
4.80k
    }
389
36.4k
    JExtended = false;
390
36.1k
  }
391
23.5k
  
return false23.3k
;
392
23.5k
}
393
394
/// tryCompound - Given a bundle check for compound insns when one
395
/// is found update the contents fo the bundle with the compound insn.
396
/// If a compound instruction is found then the bundle will have one
397
/// additional slot.
398
void HexagonMCInstrInfo::tryCompound(MCInstrInfo const &MCII, MCSubtargetInfo const &STI,
399
23.3k
                                     MCContext &Context, MCInst &MCI) {
400
23.3k
  assert(HexagonMCInstrInfo::isBundle(MCI) &&
401
23.3k
         "Non-Bundle where Bundle expected");
402
23.3k
403
23.3k
  // By definition a compound must have 2 insn.
404
23.3k
  if (MCI.size() < 2)
405
14
    return;
406
23.3k
407
23.3k
  bool StartedValid = llvm::HexagonMCShuffle(Context, false, MCII, STI, MCI);
408
23.3k
409
23.3k
  // Create a vector, needed to keep the order of jump instructions.
410
23.3k
  MCInst CheckList(MCI);
411
23.3k
412
23.3k
  // Look for compounds until none are found, only update the bundle when
413
23.3k
  // a compound is found.
414
23.5k
  while (lookForCompound(MCII, Context, CheckList)) {
415
230
    // Keep the original bundle around in case the shuffle fails.
416
230
    MCInst OriginalBundle(MCI);
417
230
418
230
    // Need to update the bundle.
419
230
    MCI = CheckList;
420
230
421
230
    if (StartedValid &&
422
230
        
!llvm::HexagonMCShuffle(Context, false, MCII, STI, MCI)228
) {
423
1
      LLVM_DEBUG(dbgs() << "Found ERROR\n");
424
1
      MCI = OriginalBundle;
425
1
    }
426
230
  }
427
23.3k
}