LLVM 24.0.0git
Thumb2InstrInfo.cpp
Go to the documentation of this file.
1//===- Thumb2InstrInfo.cpp - Thumb-2 Instruction Information --------------===//
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 contains the Thumb-2 implementation of the TargetInstrInfo class.
10//
11//===----------------------------------------------------------------------===//
12
13#include "Thumb2InstrInfo.h"
15#include "ARMSubtarget.h"
26#include "llvm/IR/DebugLoc.h"
27#include "llvm/IR/Module.h"
28#include "llvm/MC/MCInst.h"
30#include "llvm/MC/MCInstrDesc.h"
33#include <cassert>
34
35using namespace llvm;
36
39
40/// Return the noop instruction to use for a noop.
42 return MCInstBuilder(ARM::tHINT).addImm(0).addImm(ARMCC::AL).addReg(0);
43}
44
45unsigned Thumb2InstrInfo::getUnindexedOpcode(unsigned Opc) const {
46 // FIXME
47 return 0;
48}
49
50void
52 MachineBasicBlock *NewDest) const {
53 MachineBasicBlock *MBB = Tail->getParent();
54 ARMFunctionInfo *AFI = MBB->getParent()->getInfo<ARMFunctionInfo>();
55 if (!AFI->hasITBlocks() || Tail->isBranch()) {
57 return;
58 }
59
60 // If the first instruction of Tail is predicated, we may have to update
61 // the IT instruction.
62 Register PredReg;
63 ARMCC::CondCodes CC = getInstrPredicate(*Tail, PredReg);
65 if (CC != ARMCC::AL)
66 // Expecting at least the t2IT instruction before it.
67 --MBBI;
68
69 // Actually replace the tail.
71
72 // Fix up IT.
73 if (CC != ARMCC::AL) {
75 unsigned Count = 4; // At most 4 instructions in an IT block.
76 while (Count && MBBI != E) {
77 if (MBBI->isDebugInstr()) {
78 --MBBI;
79 continue;
80 }
81 if (MBBI->getOpcode() == ARM::t2IT) {
82 unsigned Mask = MBBI->getOperand(1).getImm();
83 if (Count == 4)
84 MBBI->eraseFromParent();
85 else {
86 unsigned MaskOn = 1 << Count;
87 unsigned MaskOff = ~(MaskOn - 1);
88 MBBI->getOperand(1).setImm((Mask & MaskOff) | MaskOn);
89 }
90 return;
91 }
92 --MBBI;
93 --Count;
94 }
95
96 // Ctrl flow can reach here if branch folding is run before IT block
97 // formation pass.
98 }
99}
100
101bool
104 while (MBBI->isDebugInstr()) {
105 ++MBBI;
106 if (MBBI == MBB.end())
107 return false;
108 }
109
110 Register PredReg;
111 return getITInstrPredicate(*MBBI, PredReg) == ARMCC::AL;
112}
113
117 bool PreferFalse) const {
118 // Try to use the base optimizeSelect, which uses canFoldIntoMOVCC to fold the
119 // MOVCC into another instruction. If that fails on 8.1-M fall back to using a
120 // CSEL.
121 MachineInstr *RV = ARMBaseInstrInfo::optimizeSelect(MI, SeenMIs, PreferFalse);
122 if (!RV && getSubtarget().hasV8_1MMainlineOps() &&
123 !getSubtarget().getCLOpts().prefer_no_csel) {
124 Register DestReg = MI.getOperand(0).getReg();
125
126 if (!DestReg.isVirtual())
127 return nullptr;
128
129 MachineInstrBuilder NewMI = BuildMI(*MI.getParent(), MI, MI.getDebugLoc(),
130 get(ARM::t2CSEL), DestReg)
131 .add(MI.getOperand(2))
132 .add(MI.getOperand(1))
133 .add(MI.getOperand(3));
134 SeenMIs.insert(NewMI);
135 return NewMI;
136 }
137 return RV;
138}
139
142 const DebugLoc &DL, Register DestReg,
143 Register SrcReg, bool KillSrc,
144 bool RenamableDest, bool RenamableSrc) const {
145 // Handle SPR, DPR, and QPR copies.
146 if (!ARM::GPRRegClass.contains(DestReg, SrcReg))
147 return ARMBaseInstrInfo::copyPhysReg(MBB, I, DL, DestReg, SrcReg, KillSrc);
148
149 BuildMI(MBB, I, DL, get(ARM::tMOVr), DestReg)
150 .addReg(SrcReg, getKillRegState(KillSrc))
152}
153
156 Register SrcReg, bool isKill, int FI,
157 const TargetRegisterClass *RC,
158 Register VReg,
159 MachineInstr::MIFlag Flags) const {
160 DebugLoc DL;
161 if (I != MBB.end()) DL = I->getDebugLoc();
162
163 MachineFunction &MF = *MBB.getParent();
164 MachineFrameInfo &MFI = MF.getFrameInfo();
167 MFI.getObjectSize(FI), MFI.getObjectAlign(FI));
168
169 if (ARM::GPRRegClass.hasSubClassEq(RC)) {
170 BuildMI(MBB, I, DL, get(ARM::t2STRi12))
171 .addReg(SrcReg, getKillRegState(isKill))
172 .addFrameIndex(FI)
173 .addImm(0)
174 .addMemOperand(MMO)
176 return;
177 }
178
179 if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
180 // Thumb2 STRD expects its dest-registers to be in rGPR. Not a problem for
181 // gsub_0, but needs an extra constraint for gsub_1 (which could be sp
182 // otherwise).
183 if (SrcReg.isVirtual()) {
185 MRI->constrainRegClass(SrcReg, &ARM::GPRPairnospRegClass);
186 }
187
188 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::t2STRDi8));
189 AddDReg(MIB, SrcReg, ARM::gsub_0, getKillRegState(isKill));
190 AddDReg(MIB, SrcReg, ARM::gsub_1, {});
192 return;
193 }
194
195 ARMBaseInstrInfo::storeRegToStackSlot(MBB, I, SrcReg, isKill, FI, RC,
196 Register());
197}
198
201 Register DestReg, int FI,
202 const TargetRegisterClass *RC,
203 Register VReg, unsigned SubReg,
204 MachineInstr::MIFlag Flags) const {
205 MachineFunction &MF = *MBB.getParent();
206 MachineFrameInfo &MFI = MF.getFrameInfo();
209 MFI.getObjectSize(FI), MFI.getObjectAlign(FI));
210 DebugLoc DL;
211 if (I != MBB.end()) DL = I->getDebugLoc();
212
213 if (ARM::GPRRegClass.hasSubClassEq(RC)) {
214 BuildMI(MBB, I, DL, get(ARM::t2LDRi12), DestReg)
215 .addFrameIndex(FI)
216 .addImm(0)
217 .addMemOperand(MMO)
219 return;
220 }
221
222 if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
223 // Thumb2 LDRD expects its dest-registers to be in rGPR. Not a problem for
224 // gsub_0, but needs an extra constraint for gsub_1 (which could be sp
225 // otherwise).
226 if (DestReg.isVirtual()) {
228 MRI->constrainRegClass(DestReg, &ARM::GPRPairnospRegClass);
229 }
230
231 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::t2LDRDi8));
232 AddDReg(MIB, DestReg, ARM::gsub_0, RegState::DefineNoRead);
233 AddDReg(MIB, DestReg, ARM::gsub_1, RegState::DefineNoRead);
235
236 if (DestReg.isPhysical())
237 MIB.addReg(DestReg, RegState::ImplicitDefine);
238 return;
239 }
240
242}
243
244void Thumb2InstrInfo::expandLoadStackGuard(
246 MachineFunction &MF = *MI->getParent()->getParent();
247 Module &M = *MF.getFunction().getParent();
248
249 if (M.getStackProtectorGuard() == "tls") {
250 expandLoadStackGuardBase(MI, ARM::t2MRC, ARM::t2LDRi12);
251 return;
252 }
253
254 const auto *GV = cast<GlobalValue>((*MI->memoperands_begin())->getValue());
256 bool IsPIC = MF.getTarget().isPositionIndependent();
257 if (Subtarget.isTargetELF() && !GV->isDSOLocal())
258 expandLoadStackGuardBase(MI, ARM::t2LDRLIT_ga_pcrel, ARM::t2LDRi12);
259 else if (!Subtarget.useMovt())
261 MI, IsPIC ? ARM::t2LDRLIT_ga_pcrel : ARM::tLDRLIT_ga_abs,
262 ARM::t2LDRi12);
263 else if (IsPIC)
264 expandLoadStackGuardBase(MI, ARM::t2MOV_ga_pcrel, ARM::t2LDRi12);
265 else
266 expandLoadStackGuardBase(MI, ARM::t2MOVi32imm, ARM::t2LDRi12);
267}
268
270 bool NewMI,
271 unsigned OpIdx1,
272 unsigned OpIdx2) const {
273 switch (MI.getOpcode()) {
274 case ARM::MVE_VMAXNMAf16:
275 case ARM::MVE_VMAXNMAf32:
276 case ARM::MVE_VMINNMAf16:
277 case ARM::MVE_VMINNMAf32:
278 // Don't allow predicated instructions to be commuted.
280 return nullptr;
281 }
282 return ARMBaseInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
283}
284
286 const MachineBasicBlock *MBB,
287 const MachineFunction &MF) const {
288 // BTI clearing instructions shall not take part in scheduling regions as
289 // they must stay in their intended place. Although PAC isn't BTI clearing,
290 // it can be transformed into PACBTI after the pre-RA Machine Scheduling
291 // has taken place, so its movement must also be restricted.
292 switch (MI.getOpcode()) {
293 case ARM::t2BTI:
294 case ARM::t2PAC:
295 case ARM::t2PACBTI:
296 case ARM::t2SG:
297 return true;
298 default:
299 break;
300 }
302}
303
306 const DebugLoc &dl, Register DestReg,
307 Register BaseReg, int NumBytes,
308 ARMCC::CondCodes Pred, Register PredReg,
309 const ARMBaseInstrInfo &TII,
310 unsigned MIFlags) {
311 if (NumBytes == 0 && DestReg != BaseReg) {
312 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), DestReg)
313 .addReg(BaseReg, RegState::Kill)
314 .addImm((unsigned)Pred).addReg(PredReg).setMIFlags(MIFlags);
315 return;
316 }
317
318 bool isSub = NumBytes < 0;
319 if (isSub) NumBytes = -NumBytes;
320
321 // If profitable, use a movw or movt to materialize the offset.
322 // FIXME: Use the scavenger to grab a scratch register.
323 if (DestReg != ARM::SP && DestReg != BaseReg &&
324 NumBytes >= 4096 &&
325 ARM_AM::getT2SOImmVal(NumBytes) == -1) {
326 bool Fits = false;
327 if (NumBytes < 65536) {
328 // Use a movw to materialize the 16-bit constant.
329 BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVi16), DestReg)
330 .addImm(NumBytes)
331 .addImm((unsigned)Pred).addReg(PredReg).setMIFlags(MIFlags);
332 Fits = true;
333 } else if ((NumBytes & 0xffff) == 0) {
334 // Use a movt to materialize the 32-bit constant.
335 BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVTi16), DestReg)
336 .addReg(DestReg)
337 .addImm(NumBytes >> 16)
338 .addImm((unsigned)Pred).addReg(PredReg).setMIFlags(MIFlags);
339 Fits = true;
340 }
341
342 if (Fits) {
343 if (isSub) {
344 BuildMI(MBB, MBBI, dl, TII.get(ARM::t2SUBrr), DestReg)
345 .addReg(BaseReg)
346 .addReg(DestReg, RegState::Kill)
347 .add(predOps(Pred, PredReg))
348 .add(condCodeOp())
349 .setMIFlags(MIFlags);
350 } else {
351 // Here we know that DestReg is not SP but we do not
352 // know anything about BaseReg. t2ADDrr is an invalid
353 // instruction is SP is used as the second argument, but
354 // is fine if SP is the first argument. To be sure we
355 // do not generate invalid encoding, put BaseReg first.
356 BuildMI(MBB, MBBI, dl, TII.get(ARM::t2ADDrr), DestReg)
357 .addReg(BaseReg)
358 .addReg(DestReg, RegState::Kill)
359 .add(predOps(Pred, PredReg))
360 .add(condCodeOp())
361 .setMIFlags(MIFlags);
362 }
363 return;
364 }
365 }
366
367 while (NumBytes) {
368 unsigned ThisVal = NumBytes;
369 unsigned Opc = 0;
370 if (DestReg == ARM::SP && BaseReg != ARM::SP) {
371 // mov sp, rn. Note t2MOVr cannot be used.
372 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), DestReg)
373 .addReg(BaseReg)
374 .setMIFlags(MIFlags)
376 BaseReg = ARM::SP;
377 continue;
378 }
379
380 assert((DestReg != ARM::SP || BaseReg == ARM::SP) &&
381 "Writing to SP, from other register.");
382
383 // Try to use T1, as it smaller
384 if ((DestReg == ARM::SP) && (ThisVal < ((1 << 7) - 1) * 4)) {
385 assert((ThisVal & 3) == 0 && "Stack update is not multiple of 4?");
386 Opc = isSub ? ARM::tSUBspi : ARM::tADDspi;
387 BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg)
388 .addReg(BaseReg)
389 .addImm(ThisVal / 4)
390 .setMIFlags(MIFlags)
392 break;
393 }
394 bool HasCCOut = true;
395 int ImmIsT2SO = ARM_AM::getT2SOImmVal(ThisVal);
396 bool ToSP = DestReg == ARM::SP;
397 unsigned t2SUB = ToSP ? ARM::t2SUBspImm : ARM::t2SUBri;
398 unsigned t2ADD = ToSP ? ARM::t2ADDspImm : ARM::t2ADDri;
399 unsigned t2SUBi12 = ToSP ? ARM::t2SUBspImm12 : ARM::t2SUBri12;
400 unsigned t2ADDi12 = ToSP ? ARM::t2ADDspImm12 : ARM::t2ADDri12;
401 Opc = isSub ? t2SUB : t2ADD;
402 // Prefer T2: sub rd, rn, so_imm | sub sp, sp, so_imm
403 if (ImmIsT2SO != -1) {
404 NumBytes = 0;
405 } else if (ThisVal < 4096) {
406 // Prefer T3 if can make it in a single go: subw rd, rn, imm12 | subw sp,
407 // sp, imm12
408 Opc = isSub ? t2SUBi12 : t2ADDi12;
409 HasCCOut = false;
410 NumBytes = 0;
411 } else {
412 // Use one T2 instruction to reduce NumBytes
413 // FIXME: Move this to ARMAddressingModes.h?
414 unsigned RotAmt = llvm::countl_zero(ThisVal);
415 ThisVal = ThisVal & llvm::rotr<uint32_t>(0xff000000U, RotAmt);
416 NumBytes &= ~ThisVal;
417 assert(ARM_AM::getT2SOImmVal(ThisVal) != -1 &&
418 "Bit extraction didn't work?");
419 }
420
421 // Build the new ADD / SUB.
422 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg)
423 .addReg(BaseReg, RegState::Kill)
424 .addImm(ThisVal)
426 .setMIFlags(MIFlags);
427 if (HasCCOut)
428 MIB.add(condCodeOp());
429
430 BaseReg = DestReg;
431 }
432}
433
434static unsigned
435negativeOffsetOpcode(unsigned opcode)
436{
437 switch (opcode) {
438 case ARM::t2LDRi12: return ARM::t2LDRi8;
439 case ARM::t2LDRHi12: return ARM::t2LDRHi8;
440 case ARM::t2LDRBi12: return ARM::t2LDRBi8;
441 case ARM::t2LDRSHi12: return ARM::t2LDRSHi8;
442 case ARM::t2LDRSBi12: return ARM::t2LDRSBi8;
443 case ARM::t2STRi12: return ARM::t2STRi8;
444 case ARM::t2STRBi12: return ARM::t2STRBi8;
445 case ARM::t2STRHi12: return ARM::t2STRHi8;
446 case ARM::t2PLDi12: return ARM::t2PLDi8;
447 case ARM::t2PLDWi12: return ARM::t2PLDWi8;
448 case ARM::t2PLIi12: return ARM::t2PLIi8;
449
450 case ARM::t2LDRi8:
451 case ARM::t2LDRHi8:
452 case ARM::t2LDRBi8:
453 case ARM::t2LDRSHi8:
454 case ARM::t2LDRSBi8:
455 case ARM::t2STRi8:
456 case ARM::t2STRBi8:
457 case ARM::t2STRHi8:
458 case ARM::t2PLDi8:
459 case ARM::t2PLDWi8:
460 case ARM::t2PLIi8:
461 return opcode;
462
463 default:
464 llvm_unreachable("unknown thumb2 opcode.");
465 }
466}
467
468static unsigned
469positiveOffsetOpcode(unsigned opcode)
470{
471 switch (opcode) {
472 case ARM::t2LDRi8: return ARM::t2LDRi12;
473 case ARM::t2LDRHi8: return ARM::t2LDRHi12;
474 case ARM::t2LDRBi8: return ARM::t2LDRBi12;
475 case ARM::t2LDRSHi8: return ARM::t2LDRSHi12;
476 case ARM::t2LDRSBi8: return ARM::t2LDRSBi12;
477 case ARM::t2STRi8: return ARM::t2STRi12;
478 case ARM::t2STRBi8: return ARM::t2STRBi12;
479 case ARM::t2STRHi8: return ARM::t2STRHi12;
480 case ARM::t2PLDi8: return ARM::t2PLDi12;
481 case ARM::t2PLDWi8: return ARM::t2PLDWi12;
482 case ARM::t2PLIi8: return ARM::t2PLIi12;
483
484 case ARM::t2LDRi12:
485 case ARM::t2LDRHi12:
486 case ARM::t2LDRBi12:
487 case ARM::t2LDRSHi12:
488 case ARM::t2LDRSBi12:
489 case ARM::t2STRi12:
490 case ARM::t2STRBi12:
491 case ARM::t2STRHi12:
492 case ARM::t2PLDi12:
493 case ARM::t2PLDWi12:
494 case ARM::t2PLIi12:
495 return opcode;
496
497 default:
498 llvm_unreachable("unknown thumb2 opcode.");
499 }
500}
501
502static unsigned
503immediateOffsetOpcode(unsigned opcode)
504{
505 switch (opcode) {
506 case ARM::t2LDRs: return ARM::t2LDRi12;
507 case ARM::t2LDRHs: return ARM::t2LDRHi12;
508 case ARM::t2LDRBs: return ARM::t2LDRBi12;
509 case ARM::t2LDRSHs: return ARM::t2LDRSHi12;
510 case ARM::t2LDRSBs: return ARM::t2LDRSBi12;
511 case ARM::t2STRs: return ARM::t2STRi12;
512 case ARM::t2STRBs: return ARM::t2STRBi12;
513 case ARM::t2STRHs: return ARM::t2STRHi12;
514 case ARM::t2PLDs: return ARM::t2PLDi12;
515 case ARM::t2PLDWs: return ARM::t2PLDWi12;
516 case ARM::t2PLIs: return ARM::t2PLIi12;
517
518 case ARM::t2LDRi12:
519 case ARM::t2LDRHi12:
520 case ARM::t2LDRBi12:
521 case ARM::t2LDRSHi12:
522 case ARM::t2LDRSBi12:
523 case ARM::t2STRi12:
524 case ARM::t2STRBi12:
525 case ARM::t2STRHi12:
526 case ARM::t2PLDi12:
527 case ARM::t2PLDWi12:
528 case ARM::t2PLIi12:
529 case ARM::t2LDRi8:
530 case ARM::t2LDRHi8:
531 case ARM::t2LDRBi8:
532 case ARM::t2LDRSHi8:
533 case ARM::t2LDRSBi8:
534 case ARM::t2STRi8:
535 case ARM::t2STRBi8:
536 case ARM::t2STRHi8:
537 case ARM::t2PLDi8:
538 case ARM::t2PLDWi8:
539 case ARM::t2PLIi8:
540 return opcode;
541
542 default:
543 llvm_unreachable("unknown thumb2 opcode.");
544 }
545}
546
547bool llvm::rewriteT2FrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
548 Register FrameReg, int &Offset,
549 const ARMBaseInstrInfo &TII,
550 const TargetRegisterInfo *TRI) {
551 unsigned Opcode = MI.getOpcode();
552 const MCInstrDesc &Desc = MI.getDesc();
553 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
554 bool isSub = false;
555
556 MachineFunction &MF = *MI.getParent()->getParent();
557 const TargetRegisterClass *RegClass = TII.getRegClass(Desc, FrameRegIdx);
558
559 // Memory operands in inline assembly always use AddrModeT2_i12.
560 if (Opcode == ARM::INLINEASM || Opcode == ARM::INLINEASM_BR)
561 AddrMode = ARMII::AddrModeT2_i12; // FIXME. mode for thumb2?
562
563 const bool IsSP = Opcode == ARM::t2ADDspImm12 || Opcode == ARM::t2ADDspImm;
564 if (IsSP || Opcode == ARM::t2ADDri || Opcode == ARM::t2ADDri12) {
565 Offset += MI.getOperand(FrameRegIdx+1).getImm();
566
567 Register PredReg;
568 if (Offset == 0 && getInstrPredicate(MI, PredReg) == ARMCC::AL &&
569 !MI.definesRegister(ARM::CPSR, /*TRI=*/nullptr)) {
570 // Turn it into a move.
571 MI.setDesc(TII.get(ARM::tMOVr));
572 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
573 // Remove offset and remaining explicit predicate operands.
574 do MI.removeOperand(FrameRegIdx+1);
575 while (MI.getNumOperands() > FrameRegIdx+1);
576 MachineInstrBuilder MIB(*MI.getParent()->getParent(), &MI);
577 MIB.add(predOps(ARMCC::AL));
578 return true;
579 }
580
581 bool HasCCOut = (Opcode != ARM::t2ADDspImm12 && Opcode != ARM::t2ADDri12);
582
583 if (Offset < 0) {
584 Offset = -Offset;
585 isSub = true;
586 MI.setDesc(IsSP ? TII.get(ARM::t2SUBspImm) : TII.get(ARM::t2SUBri));
587 } else {
588 MI.setDesc(IsSP ? TII.get(ARM::t2ADDspImm) : TII.get(ARM::t2ADDri));
589 }
590
591 // Common case: small offset, fits into instruction.
592 if (ARM_AM::getT2SOImmVal(Offset) != -1) {
593 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
594 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset);
595 // Add cc_out operand if the original instruction did not have one.
596 if (!HasCCOut)
597 MI.addOperand(MachineOperand::CreateReg(0, false));
598 Offset = 0;
599 return true;
600 }
601 // Another common case: imm12.
602 if (Offset < 4096 &&
603 (!HasCCOut || MI.getOperand(MI.getNumOperands()-1).getReg() == 0)) {
604 unsigned NewOpc = isSub ? IsSP ? ARM::t2SUBspImm12 : ARM::t2SUBri12
605 : IsSP ? ARM::t2ADDspImm12 : ARM::t2ADDri12;
606 MI.setDesc(TII.get(NewOpc));
607 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
608 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset);
609 // Remove the cc_out operand.
610 if (HasCCOut)
611 MI.removeOperand(MI.getNumOperands()-1);
612 Offset = 0;
613 return true;
614 }
615
616 // Otherwise, extract 8 adjacent bits from the immediate into this
617 // t2ADDri/t2SUBri.
618 unsigned RotAmt = llvm::countl_zero<unsigned>(Offset);
619 unsigned ThisImmVal = Offset & llvm::rotr<uint32_t>(0xff000000U, RotAmt);
620
621 // We will handle these bits from offset, clear them.
622 Offset &= ~ThisImmVal;
623
624 assert(ARM_AM::getT2SOImmVal(ThisImmVal) != -1 &&
625 "Bit extraction didn't work?");
626 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(ThisImmVal);
627 // Add cc_out operand if the original instruction did not have one.
628 if (!HasCCOut)
629 MI.addOperand(MachineOperand::CreateReg(0, false));
630 } else {
631 // AddrMode4 and AddrMode6 cannot handle any offset.
633 return false;
634
635 // AddrModeT2_so cannot handle any offset. If there is no offset
636 // register then we change to an immediate version.
637 unsigned NewOpc = Opcode;
639 Register OffsetReg = MI.getOperand(FrameRegIdx + 1).getReg();
640 if (OffsetReg != 0) {
641 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
642 return Offset == 0;
643 }
644
645 MI.removeOperand(FrameRegIdx+1);
646 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(0);
647 NewOpc = immediateOffsetOpcode(Opcode);
649 }
650
651 unsigned NumBits = 0;
652 unsigned Scale = 1;
655 // i8 supports only negative, and i12 supports only positive, so
656 // based on Offset sign convert Opcode to the appropriate
657 // instruction
658 Offset += MI.getOperand(FrameRegIdx+1).getImm();
659 if (Offset < 0) {
660 NewOpc = negativeOffsetOpcode(Opcode);
661 NumBits = 8;
662 isSub = true;
663 Offset = -Offset;
664 } else {
665 NewOpc = positiveOffsetOpcode(Opcode);
666 NumBits = 12;
667 }
668 } else if (AddrMode == ARMII::AddrMode5) {
669 // VFP address mode.
670 const MachineOperand &OffOp = MI.getOperand(FrameRegIdx+1);
671 int InstrOffs = ARM_AM::getAM5Offset(OffOp.getImm());
672 if (ARM_AM::getAM5Op(OffOp.getImm()) == ARM_AM::sub)
673 InstrOffs *= -1;
674 NumBits = 8;
675 Scale = 4;
676 Offset += InstrOffs * 4;
677 assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
678 if (Offset < 0) {
679 Offset = -Offset;
680 isSub = true;
681 }
682 } else if (AddrMode == ARMII::AddrMode5FP16) {
683 // VFP address mode.
684 const MachineOperand &OffOp = MI.getOperand(FrameRegIdx+1);
685 int InstrOffs = ARM_AM::getAM5FP16Offset(OffOp.getImm());
687 InstrOffs *= -1;
688 NumBits = 8;
689 Scale = 2;
690 Offset += InstrOffs * 2;
691 assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
692 if (Offset < 0) {
693 Offset = -Offset;
694 isSub = true;
695 }
696 } else if (AddrMode == ARMII::AddrModeT2_i7s4 ||
699 Offset += MI.getOperand(FrameRegIdx + 1).getImm();
700 unsigned OffsetMask;
701 switch (AddrMode) {
702 case ARMII::AddrModeT2_i7s4: NumBits = 9; OffsetMask = 0x3; break;
703 case ARMII::AddrModeT2_i7s2: NumBits = 8; OffsetMask = 0x1; break;
704 default: NumBits = 7; OffsetMask = 0x0; break;
705 }
706 // MCInst operand expects already scaled value.
707 Scale = 1;
708 assert((Offset & OffsetMask) == 0 && "Can't encode this offset!");
709 (void)OffsetMask; // squash unused-variable warning at -NDEBUG
710 } else if (AddrMode == ARMII::AddrModeT2_i8s4) {
711 Offset += MI.getOperand(FrameRegIdx + 1).getImm();
712 NumBits = 8 + 2;
713 // MCInst operand expects already scaled value.
714 Scale = 1;
715 assert((Offset & 3) == 0 && "Can't encode this offset!");
716 } else if (AddrMode == ARMII::AddrModeT2_ldrex) {
717 Offset += MI.getOperand(FrameRegIdx + 1).getImm() * 4;
718 NumBits = 8; // 8 bits scaled by 4
719 Scale = 4;
720 assert((Offset & 3) == 0 && "Can't encode this offset!");
721 } else {
722 llvm_unreachable("Unsupported addressing mode!");
723 }
724
725 if (NewOpc != Opcode)
726 MI.setDesc(TII.get(NewOpc));
727
728 MachineOperand &ImmOp = MI.getOperand(FrameRegIdx+1);
729
730 // Attempt to fold address computation
731 // Common case: small offset, fits into instruction. We need to make sure
732 // the register class is correct too, for instructions like the MVE
733 // VLDRH.32, which only accepts low tGPR registers.
734 int ImmedOffset = Offset / Scale;
735 unsigned Mask = (1 << NumBits) - 1;
736 if ((unsigned)Offset <= Mask * Scale &&
737 (FrameReg.isVirtual() || RegClass->contains(FrameReg))) {
738 if (FrameReg.isVirtual()) {
739 // Make sure the register class for the virtual register is correct
741 if (!MRI->constrainRegClass(FrameReg, RegClass))
742 llvm_unreachable("Unable to constrain virtual register class.");
743 }
744
745 // Replace the FrameIndex with fp/sp
746 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
747 if (isSub) {
749 // FIXME: Not consistent.
750 ImmedOffset |= 1 << NumBits;
751 else
752 ImmedOffset = -ImmedOffset;
753 }
754 ImmOp.ChangeToImmediate(ImmedOffset);
755 Offset = 0;
756 return true;
757 }
758
759 // Otherwise, offset doesn't fit. Pull in what we can to simplify
760 ImmedOffset = ImmedOffset & Mask;
761 if (isSub) {
763 // FIXME: Not consistent.
764 ImmedOffset |= 1 << NumBits;
765 else {
766 ImmedOffset = -ImmedOffset;
767 if (ImmedOffset == 0)
768 // Change the opcode back if the encoded offset is zero.
769 MI.setDesc(TII.get(positiveOffsetOpcode(NewOpc)));
770 }
771 }
772 ImmOp.ChangeToImmediate(ImmedOffset);
773 Offset &= ~(Mask*Scale);
774 }
775
776 Offset = (isSub) ? -Offset : Offset;
777 return Offset == 0 && (FrameReg.isVirtual() || RegClass->contains(FrameReg));
778}
779
781 Register &PredReg) {
782 unsigned Opc = MI.getOpcode();
783 if (Opc == ARM::tBcc || Opc == ARM::t2Bcc)
784 return ARMCC::AL;
785 return getInstrPredicate(MI, PredReg);
786}
787
789 const MCInstrDesc &MCID = MI.getDesc();
790
791 for (unsigned i = 0, e = MCID.getNumOperands(); i != e; ++i)
792 if (ARM::isVpred(MCID.operands()[i].OperandType))
793 return i;
794
795 return -1;
796}
797
799 const MCInstrDesc &MCID = MI.getDesc();
800
801 for (unsigned i = 0, e = MCID.getNumOperands(); i != e; ++i)
802 if (MCID.operands()[i].OperandType == ARM::OPERAND_VPRED_R)
803 return i + ARM::SUBOP_vpred_r_inactive;
804
805 return -1;
806}
807
809 Register &PredReg) {
810 int PIdx = findFirstVPTPredOperandIdx(MI);
811 if (PIdx == -1) {
812 PredReg = 0;
813 return ARMVCC::None;
814 }
815
816 PredReg = MI.getOperand(PIdx+1).getReg();
817 return (ARMVCC::VPTCodes)MI.getOperand(PIdx).getImm();
818}
819
821 assert(isVPTOpcode(Instr.getOpcode()) && "Not a VPST or VPT Instruction!");
822
823 MachineOperand &MaskOp = Instr.getOperand(0);
824 assert(MaskOp.isImm() && "Operand 0 is not the block mask of the VPT/VPST?!");
825
826 MachineBasicBlock::iterator Iter = ++Instr.getIterator(),
827 End = Instr.getParent()->end();
828
829 while (Iter != End && Iter->isDebugInstr())
830 ++Iter;
831
832 // Verify that the instruction after the VPT/VPST is predicated (it should
833 // be), and skip it.
834 assert(Iter != End && "Expected some instructions in any VPT block");
835 assert(
837 "VPT/VPST should be followed by an instruction with a 'then' predicate!");
838 ++Iter;
839
840 // Iterate over the predicated instructions, updating the BlockMask as we go.
842 while (Iter != End) {
843 if (Iter->isDebugInstr()) {
844 ++Iter;
845 continue;
846 }
848 if (Pred == ARMVCC::None)
849 break;
850 BlockMask = expandPredBlockMask(BlockMask, Pred);
851 ++Iter;
852 }
853
854 // Rewrite the BlockMask.
855 MaskOp.setImm((int64_t)(BlockMask));
856}
const AArch64Subtarget & Subtarget
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
const AArch64InstrInfo & TII
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
IRTranslator LLVM IR MI
Module.h This file contains the declarations for the Module class.
unsigned Opc
#define I(x, y, z)
Definition MD5.cpp:57
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
RISCVCC::CondCode CC
MachineRegisterInfo & MRI
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition Value.cpp:479
static unsigned negativeOffsetOpcode(unsigned opcode)
static unsigned positiveOffsetOpcode(unsigned opcode)
static unsigned immediateOffsetOpcode(unsigned opcode)
void copyPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I, const DebugLoc &DL, Register DestReg, Register SrcReg, bool KillSrc, bool RenamableDest=false, bool RenamableSrc=false) const override
void loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, Register DestReg, int FrameIndex, const TargetRegisterClass *RC, Register VReg, unsigned SubReg=0, MachineInstr::MIFlag Flags=MachineInstr::NoFlags) const override
MachineInstr * optimizeSelect(MachineInstr &MI, SmallPtrSetImpl< MachineInstr * > &SeenMIs, bool) const override
const MachineInstrBuilder & AddDReg(MachineInstrBuilder &MIB, unsigned Reg, unsigned SubIdx, RegState State) const
bool isSchedulingBoundary(const MachineInstr &MI, const MachineBasicBlock *MBB, const MachineFunction &MF) const override
void expandLoadStackGuardBase(MachineBasicBlock::iterator MI, unsigned LoadImmOpc, unsigned LoadOpc) const
void storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, Register SrcReg, bool isKill, int FrameIndex, const TargetRegisterClass *RC, Register VReg, MachineInstr::MIFlag Flags=MachineInstr::NoFlags) const override
ARMBaseInstrInfo(const ARMSubtarget &STI, const ARMBaseRegisterInfo &TRI)
const ARMSubtarget & getSubtarget() const
MachineInstr * commuteInstructionImpl(MachineInstr &MI, bool NewMI, unsigned OpIdx1, unsigned OpIdx2) const override
Commutes the operands in the given instruction.
ARMFunctionInfo - This class is derived from MachineFunctionInfo and contains private ARM-specific in...
A debug info location.
Definition DebugLoc.h:126
Module * getParent()
Get the module that this global value is contained inside of...
MCInstBuilder & addReg(MCRegister Reg)
Add a new register operand.
MCInstBuilder & addImm(int64_t Val)
Add a new integer immediate operand.
Instances of this class represent a single low-level machine instruction.
Definition MCInst.h:187
Describe properties that are true of each instruction in the target description file.
bool contains(MCRegister Reg) const
contains - Return true if the specified register is included in this register class.
MachineInstrBundleIterator< MachineInstr > iterator
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
Align getObjectAlign(int ObjectIdx) const
Return the alignment of the specified stack object.
int64_t getObjectSize(int ObjectIdx) const
Return the size of the specified object.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
MachineMemOperand * getMachineMemOperand(MachinePointerInfo PtrInfo, MachineMemOperand::Flags F, LLT MemTy, Align BaseAlignment, const MMOMetadata &Metadata=MMOMetadata(), SyncScope::ID SSID=SyncScope::System, AtomicOrdering Ordering=AtomicOrdering::NotAtomic, AtomicOrdering FailureOrdering=AtomicOrdering::NotAtomic)
getMachineMemOperand - Allocate a new MachineMemOperand.
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & add(const MachineOperand &MO) const
const MachineInstrBuilder & addFrameIndex(int Idx) const
const MachineInstrBuilder & setMIFlags(unsigned Flags) const
const MachineInstrBuilder & addMemOperand(MachineMemOperand *MMO) const
Representation of each machine instruction.
A description of a memory reference used in the backend.
@ MOLoad
The memory access reads data.
@ MOStore
The memory access writes data.
MachineOperand class - Representation of each machine instruction operand.
void setImm(int64_t immVal)
int64_t getImm() const
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
LLVM_ABI void ChangeToImmediate(int64_t ImmVal, unsigned TargetFlags=0)
ChangeToImmediate - Replace this operand with a new immediate operand of the specified value.
static MachineOperand CreateReg(Register Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isEarlyClobber=false, unsigned SubReg=0, bool isDebug=false, bool isInternalRead=false, bool isRenamable=false)
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
Wrapper class representing virtual and physical registers.
Definition Register.h:20
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:79
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
Definition Register.h:83
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
virtual void ReplaceTailWithBranchTo(MachineBasicBlock::iterator Tail, MachineBasicBlock *NewDest) const
Delete the instruction OldInst and everything after it, replacing it with an unconditional branch to ...
bool isPositionIndependent() const
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
bool isLegalToSplitMBBAt(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI) const override
bool isSchedulingBoundary(const MachineInstr &MI, const MachineBasicBlock *MBB, const MachineFunction &MF) const override
unsigned getUnindexedOpcode(unsigned Opc) const override
MachineInstr * commuteInstructionImpl(MachineInstr &MI, bool NewMI, unsigned OpIdx1, unsigned OpIdx2) const override
void ReplaceTailWithBranchTo(MachineBasicBlock::iterator Tail, MachineBasicBlock *NewDest) const override
void copyPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I, const DebugLoc &DL, Register DestReg, Register SrcReg, bool KillSrc, bool RenamableDest=false, bool RenamableSrc=false) const override
MachineInstr * optimizeSelect(MachineInstr &MI, SmallPtrSetImpl< MachineInstr * > &SeenMIs, bool) const override
Thumb2InstrInfo(const ARMSubtarget &STI)
MCInst getNop() const override
Return the noop instruction to use for a noop.
void storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, Register SrcReg, bool isKill, int FrameIndex, const TargetRegisterClass *RC, Register VReg, MachineInstr::MIFlag Flags=MachineInstr::NoFlags) const override
void loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, Register DestReg, int FrameIndex, const TargetRegisterClass *RC, Register VReg, unsigned SubReg=0, MachineInstr::MIFlag Flags=MachineInstr::NoFlags) const override
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned char getAM5FP16Offset(unsigned AM5Opc)
int getT2SOImmVal(unsigned Arg)
getT2SOImmVal - Given a 32-bit immediate, if it is something that can fit into a Thumb-2 shifter_oper...
AddrOpc getAM5Op(unsigned AM5Opc)
AddrOpc getAM5FP16Op(unsigned AM5Opc)
unsigned char getAM5Offset(unsigned AM5Opc)
PredBlockMask
Mask values for IT and VPT Blocks, to be used by MCOperands.
bool isVpred(OperandType op)
This is an optimization pass for GlobalISel generic memory operations.
int findVPTInactiveOperandIdx(const MachineInstr &MI)
@ Offset
Definition DWP.cpp:577
constexpr T rotr(T V, int R)
Definition bit.h:399
int findFirstVPTPredOperandIdx(const MachineInstr &MI)
ARMVCC::VPTCodes getVPTInstrPredicate(const MachineInstr &MI, Register &PredReg)
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
@ Kill
The last use of a register.
constexpr RegState getKillRegState(bool B)
static bool isVPTOpcode(int Opc)
bool rewriteT2FrameIndex(MachineInstr &MI, unsigned FrameRegIdx, Register FrameReg, int &Offset, const ARMBaseInstrInfo &TII, const TargetRegisterInfo *TRI)
static std::array< MachineOperand, 2 > predOps(ARMCC::CondCodes Pred, unsigned PredReg=0)
Get the operands corresponding to the given Pred value.
Op::Description Desc
int countl_zero(T Val)
Count number of 0's from the most significant bit to the least stopping at the first 1.
Definition bit.h:263
ARMCC::CondCodes getITInstrPredicate(const MachineInstr &MI, Register &PredReg)
getITInstrPredicate - Valid only in Thumb2 mode.
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
ARM::PredBlockMask expandPredBlockMask(ARM::PredBlockMask BlockMask, ARMVCC::VPTCodes Kind)
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
ARMCC::CondCodes getInstrPredicate(const MachineInstr &MI, Register &PredReg)
getInstrPredicate - If instruction is predicated, returns its predicate condition,...
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
static MachineOperand condCodeOp(unsigned CCReg=0)
Get the operand corresponding to the conditional code result.
@ End
Run the machine verifier once, at the end of the pipeline.
Definition CodeGen.h:320
void recomputeVPTBlockMask(MachineInstr &Instr)
void emitT2RegPlusImmediate(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI, const DebugLoc &dl, Register DestReg, Register BaseReg, int NumBytes, ARMCC::CondCodes Pred, Register PredReg, const ARMBaseInstrInfo &TII, unsigned MIFlags=0)
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58
static LLVM_ABI MachinePointerInfo getFixedStack(MachineFunction &MF, int FI, int64_t Offset=0)
Return a MachinePointerInfo record that refers to the specified FrameIndex.