1 #include "ARMBaseInstrInfo.h"
2 #include "ARMSubtarget.h"
3 #include "ARMTargetMachine.h"
4 #include "llvm/Support/TargetRegistry.h"
5 #include "llvm/Support/TargetSelect.h"
6 #include "llvm/Target/TargetMachine.h"
7 #include "llvm/Target/TargetOptions.h"
9 #include "gtest/gtest.h"
13 // Test for instructions that aren't immediately obviously valid within a
14 // tail-predicated loop. This should be marked up in their tablegen
15 // descriptions. Currently we, conservatively, disallow:
16 // - cross beat carries.
17 // - narrowing of results.
18 // - top/bottom operations.
19 // - complex operations.
20 // - horizontal operations.
22 // - interleaved memory instructions.
23 // TODO: Add to this list once we can handle them safely.
24 TEST(MachineInstrValidTailPredication
, IsCorrect
) {
28 auto IsValidTPOpcode
= [](unsigned Opcode
) {
66 case MVE_VBICIZ0v4i32
:
67 case MVE_VBICIZ0v8i16
:
68 case MVE_VBICIZ16v4i32
:
69 case MVE_VBICIZ24v4i32
:
70 case MVE_VBICIZ8v4i32
:
71 case MVE_VBICIZ8v8i16
:
107 case MVE_VCVTf16s16_fix
:
108 case MVE_VCVTf16s16n
:
109 case MVE_VCVTf16u16_fix
:
110 case MVE_VCVTf16u16n
:
111 case MVE_VCVTf32s32_fix
:
112 case MVE_VCVTf32s32n
:
113 case MVE_VCVTf32u32_fix
:
114 case MVE_VCVTf32u32n
:
115 case MVE_VCVTs16f16_fix
:
116 case MVE_VCVTs16f16a
:
117 case MVE_VCVTs16f16m
:
118 case MVE_VCVTs16f16n
:
119 case MVE_VCVTs16f16p
:
120 case MVE_VCVTs16f16z
:
121 case MVE_VCVTs32f32_fix
:
122 case MVE_VCVTs32f32a
:
123 case MVE_VCVTs32f32m
:
124 case MVE_VCVTs32f32n
:
125 case MVE_VCVTs32f32p
:
126 case MVE_VCVTs32f32z
:
127 case MVE_VCVTu16f16_fix
:
128 case MVE_VCVTu16f16a
:
129 case MVE_VCVTu16f16m
:
130 case MVE_VCVTu16f16n
:
131 case MVE_VCVTu16f16p
:
132 case MVE_VCVTu16f16z
:
133 case MVE_VCVTu32f32_fix
:
134 case MVE_VCVTu32f32a
:
135 case MVE_VCVTu32f32m
:
136 case MVE_VCVTu32f32n
:
137 case MVE_VCVTu32f32p
:
138 case MVE_VCVTu32f32z
:
149 case MVE_VFMA_qr_Sf16
:
150 case MVE_VFMA_qr_Sf32
:
151 case MVE_VFMA_qr_f16
:
152 case MVE_VFMA_qr_f32
:
153 case MVE_VMLAS_qr_s16
:
154 case MVE_VMLAS_qr_s32
:
155 case MVE_VMLAS_qr_s8
:
156 case MVE_VMLAS_qr_u16
:
157 case MVE_VMLAS_qr_u32
:
158 case MVE_VMLAS_qr_u8
:
159 case MVE_VMLA_qr_s16
:
160 case MVE_VMLA_qr_s32
:
162 case MVE_VMLA_qr_u16
:
163 case MVE_VMLA_qr_u32
:
165 case MVE_VHADD_qr_s16
:
166 case MVE_VHADD_qr_s32
:
167 case MVE_VHADD_qr_s8
:
168 case MVE_VHADD_qr_u16
:
169 case MVE_VHADD_qr_u32
:
170 case MVE_VHADD_qr_u8
:
177 case MVE_VHSUB_qr_s16
:
178 case MVE_VHSUB_qr_s32
:
179 case MVE_VHSUB_qr_s8
:
180 case MVE_VHSUB_qr_u16
:
181 case MVE_VHSUB_qr_u32
:
182 case MVE_VHSUB_qr_u8
:
196 case MVE_VLDRBS16_post
:
197 case MVE_VLDRBS16_pre
:
198 case MVE_VLDRBS16_rq
:
200 case MVE_VLDRBS32_post
:
201 case MVE_VLDRBS32_pre
:
202 case MVE_VLDRBS32_rq
:
204 case MVE_VLDRBU16_post
:
205 case MVE_VLDRBU16_pre
:
206 case MVE_VLDRBU16_rq
:
208 case MVE_VLDRBU32_post
:
209 case MVE_VLDRBU32_pre
:
210 case MVE_VLDRBU32_rq
:
212 case MVE_VLDRBU8_post
:
213 case MVE_VLDRBU8_pre
:
215 case MVE_VLDRDU64_qi
:
216 case MVE_VLDRDU64_qi_pre
:
217 case MVE_VLDRDU64_rq
:
218 case MVE_VLDRDU64_rq_u
:
220 case MVE_VLDRHS32_post
:
221 case MVE_VLDRHS32_pre
:
222 case MVE_VLDRHS32_rq
:
223 case MVE_VLDRHS32_rq_u
:
225 case MVE_VLDRHU16_post
:
226 case MVE_VLDRHU16_pre
:
227 case MVE_VLDRHU16_rq
:
228 case MVE_VLDRHU16_rq_u
:
230 case MVE_VLDRHU32_post
:
231 case MVE_VLDRHU32_pre
:
232 case MVE_VLDRHU32_rq
:
233 case MVE_VLDRHU32_rq_u
:
235 case MVE_VLDRWU32_post
:
236 case MVE_VLDRWU32_pre
:
237 case MVE_VLDRWU32_qi
:
238 case MVE_VLDRWU32_qi_pre
:
239 case MVE_VLDRWU32_rq
:
240 case MVE_VLDRWU32_rq_u
:
246 case MVE_VMUL_qr_f16
:
247 case MVE_VMUL_qr_f32
:
248 case MVE_VMUL_qr_i16
:
249 case MVE_VMUL_qr_i32
:
263 case MVE_VORRIZ0v4i32
:
264 case MVE_VORRIZ0v8i16
:
265 case MVE_VORRIZ16v4i32
:
266 case MVE_VORRIZ24v4i32
:
267 case MVE_VORRIZ8v4i32
:
268 case MVE_VORRIZ8v8i16
:
297 case MVE_VQADD_qr_s16
:
298 case MVE_VQADD_qr_s32
:
299 case MVE_VQADD_qr_s8
:
300 case MVE_VQADD_qr_u16
:
301 case MVE_VQADD_qr_u32
:
302 case MVE_VQADD_qr_u8
:
312 case MVE_VQRSHL_by_vecs16
:
313 case MVE_VQRSHL_by_vecs32
:
314 case MVE_VQRSHL_by_vecs8
:
315 case MVE_VQRSHL_by_vecu16
:
316 case MVE_VQRSHL_by_vecu32
:
317 case MVE_VQRSHL_by_vecu8
:
318 case MVE_VQRSHL_qrs16
:
319 case MVE_VQRSHL_qrs32
:
320 case MVE_VQRSHL_qrs8
:
321 case MVE_VQRSHL_qru16
:
322 case MVE_VQRSHL_qru8
:
323 case MVE_VQRSHL_qru32
:
324 case MVE_VQSHLU_imms16
:
325 case MVE_VQSHLU_imms32
:
326 case MVE_VQSHLU_imms8
:
327 case MVE_VQSHL_by_vecs16
:
328 case MVE_VQSHL_by_vecs32
:
329 case MVE_VQSHL_by_vecs8
:
330 case MVE_VQSHL_by_vecu16
:
331 case MVE_VQSHL_by_vecu32
:
332 case MVE_VQSHL_by_vecu8
:
333 case MVE_VQSHL_qrs16
:
334 case MVE_VQSHL_qrs32
:
336 case MVE_VQSHL_qru16
:
337 case MVE_VQSHL_qru32
:
339 case MVE_VQSUB_qr_s16
:
340 case MVE_VQSUB_qr_s32
:
341 case MVE_VQSUB_qr_s8
:
342 case MVE_VQSUB_qr_u16
:
343 case MVE_VQSUB_qr_u32
:
344 case MVE_VQSUB_qr_u8
:
369 case MVE_VRSHL_by_vecs16
:
370 case MVE_VRSHL_by_vecs32
:
371 case MVE_VRSHL_by_vecs8
:
372 case MVE_VRSHL_by_vecu16
:
373 case MVE_VRSHL_by_vecu32
:
374 case MVE_VRSHL_by_vecu8
:
375 case MVE_VRSHL_qrs16
:
376 case MVE_VRSHL_qrs32
:
378 case MVE_VRSHL_qru16
:
379 case MVE_VRSHL_qru32
:
381 case MVE_VRSHR_imms16
:
382 case MVE_VRSHR_imms32
:
383 case MVE_VRSHR_imms8
:
384 case MVE_VRSHR_immu16
:
385 case MVE_VRSHR_immu32
:
386 case MVE_VRSHR_immu8
:
387 case MVE_VSHL_by_vecs16
:
388 case MVE_VSHL_by_vecs32
:
389 case MVE_VSHL_by_vecs8
:
390 case MVE_VSHL_by_vecu16
:
391 case MVE_VSHL_by_vecu32
:
392 case MVE_VSHL_by_vecu8
:
393 case MVE_VSHL_immi16
:
394 case MVE_VSHL_immi32
:
402 case MVE_VSHR_imms16
:
403 case MVE_VSHR_imms32
:
405 case MVE_VSHR_immu16
:
406 case MVE_VSHR_immu32
:
421 case MVE_VSTRB16_post
:
422 case MVE_VSTRB16_pre
:
425 case MVE_VSTRB32_post
:
426 case MVE_VSTRB32_pre
:
430 case MVE_VSTRBU8_post
:
431 case MVE_VSTRBU8_pre
:
433 case MVE_VSTRD64_qi_pre
:
435 case MVE_VSTRD64_rq_u
:
437 case MVE_VSTRH16_rq_u
:
439 case MVE_VSTRH32_post
:
440 case MVE_VSTRH32_pre
:
442 case MVE_VSTRH32_rq_u
:
444 case MVE_VSTRHU16_post
:
445 case MVE_VSTRHU16_pre
:
447 case MVE_VSTRW32_qi_pre
:
449 case MVE_VSTRW32_rq_u
:
451 case MVE_VSTRWU32_post
:
452 case MVE_VSTRWU32_pre
:
453 case MVE_VSUB_qr_f16
:
454 case MVE_VSUB_qr_f32
:
455 case MVE_VSUB_qr_i16
:
456 case MVE_VSUB_qr_i32
:
467 LLVMInitializeARMTargetInfo();
468 LLVMInitializeARMTarget();
469 LLVMInitializeARMTargetMC();
471 auto TT(Triple::normalize("thumbv8.1m.main-arm-none-eabi"));
473 const Target
*T
= TargetRegistry::lookupTarget(TT
, Error
);
479 TargetOptions Options
;
480 auto TM
= std::unique_ptr
<LLVMTargetMachine
>(
481 static_cast<LLVMTargetMachine
*>(
482 T
->createTargetMachine(TT
, "generic", "", Options
, None
, None
,
483 CodeGenOpt::Default
)));
484 ARMSubtarget
ST(TM
->getTargetTriple(), TM
->getTargetCPU(),
485 TM
->getTargetFeatureString(),
486 *static_cast<const ARMBaseTargetMachine
*>(TM
.get()), false);
487 const ARMBaseInstrInfo
*TII
= ST
.getInstrInfo();
488 auto MII
= TM
->getMCInstrInfo();
490 for (unsigned i
= 0; i
< ARM::INSTRUCTION_LIST_END
; ++i
) {
491 const MCInstrDesc
&Desc
= TII
->get(i
);
493 for (auto &Op
: Desc
.operands()) {
494 // Only check instructions that access the MQPR regs.
495 if ((Op
.OperandType
& MCOI::OPERAND_REGISTER
) == 0 ||
496 Op
.RegClass
!= ARM::MQPRRegClassID
)
499 uint64_t Flags
= MII
->get(i
).TSFlags
;
500 bool Valid
= (Flags
& ARMII::ValidForTailPredication
) != 0;
501 ASSERT_EQ(IsValidTPOpcode(i
), Valid
)
503 << ": mismatched expectation for tail-predicated safety\n";