LLVM 20.0.0git
ThumbRegisterInfo.cpp
Go to the documentation of this file.
1//===-- ThumbRegisterInfo.cpp - Thumb-1 Register 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-1 implementation of the TargetRegisterInfo
10// class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "ThumbRegisterInfo.h"
15#include "ARMBaseInstrInfo.h"
17#include "ARMSubtarget.h"
25#include "llvm/IR/Constants.h"
26#include "llvm/IR/Function.h"
27#include "llvm/IR/LLVMContext.h"
31
32namespace llvm {
34}
35
36using namespace llvm;
37
39
42 const MachineFunction &MF) const {
45
46 if (ARM::tGPRRegClass.hasSubClassEq(RC))
47 return &ARM::tGPRRegClass;
49}
50
53 unsigned Kind) const {
56 return &ARM::tGPRRegClass;
57}
58
61 const DebugLoc &dl, unsigned DestReg,
62 unsigned SubIdx, int Val,
63 ARMCC::CondCodes Pred, unsigned PredReg,
64 unsigned MIFlags) {
66 const ARMSubtarget &STI = MF.getSubtarget<ARMSubtarget>();
67 const TargetInstrInfo &TII = *STI.getInstrInfo();
69 const Constant *C = ConstantInt::get(
71 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align(4));
72
73 BuildMI(MBB, MBBI, dl, TII.get(ARM::tLDRpci))
74 .addReg(DestReg, getDefRegState(true), SubIdx)
75 .addConstantPoolIndex(Idx).addImm(Pred).addReg(PredReg)
76 .setMIFlags(MIFlags);
77}
78
81 const DebugLoc &dl, unsigned DestReg,
82 unsigned SubIdx, int Val,
83 ARMCC::CondCodes Pred, unsigned PredReg,
84 unsigned MIFlags) {
88 const Constant *C = ConstantInt::get(
90 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align(4));
91
92 BuildMI(MBB, MBBI, dl, TII.get(ARM::t2LDRpci))
93 .addReg(DestReg, getDefRegState(true), SubIdx)
96 .setMIFlags(MIFlags);
97}
98
99/// emitLoadConstPool - Emits a load from constpool to materialize the
100/// specified immediate.
103 const DebugLoc &dl, Register DestReg, unsigned SubIdx, int Val,
104 ARMCC::CondCodes Pred, Register PredReg, unsigned MIFlags) const {
106 const ARMSubtarget &STI = MF.getSubtarget<ARMSubtarget>();
107 if (STI.isThumb1Only()) {
108 assert((isARMLowRegister(DestReg) || DestReg.isVirtual()) &&
109 "Thumb1 does not have ldr to high register");
110 return emitThumb1LoadConstPool(MBB, MBBI, dl, DestReg, SubIdx, Val, Pred,
111 PredReg, MIFlags);
112 }
113 return emitThumb2LoadConstPool(MBB, MBBI, dl, DestReg, SubIdx, Val, Pred,
114 PredReg, MIFlags);
115}
116
117/// emitThumbRegPlusImmInReg - Emits a series of instructions to materialize a
118/// destreg = basereg + immediate in Thumb code. Materialize the immediate in a
119/// register using mov / mvn (armv6-M >) sequences, movs / lsls / adds / lsls /
120/// adds / lsls / adds sequences (armv6-M) or load the immediate from a
121/// constpool entry.
124 const DebugLoc &dl, Register DestReg, Register BaseReg, int NumBytes,
125 bool CanChangeCC, const TargetInstrInfo &TII,
126 const ARMBaseRegisterInfo &MRI, unsigned MIFlags = MachineInstr::NoFlags) {
128 const ARMSubtarget &ST = MF.getSubtarget<ARMSubtarget>();
129
130 // Use a single sp-relative add if the immediate is small enough.
131 if (BaseReg == ARM::SP &&
132 (DestReg.isVirtual() || isARMLowRegister(DestReg)) && NumBytes >= 0 &&
133 NumBytes <= 1020 && (NumBytes % 4) == 0) {
134 BuildMI(MBB, MBBI, dl, TII.get(ARM::tADDrSPi), DestReg)
135 .addReg(ARM::SP)
136 .addImm(NumBytes / 4)
138 .setMIFlags(MIFlags);
139 return;
140 }
141
142 bool isHigh = !isARMLowRegister(DestReg) ||
143 (BaseReg != 0 && !isARMLowRegister(BaseReg));
144 bool isSub = false;
145 // Subtract doesn't have high register version. Load the negative value
146 // if either base or dest register is a high register. Also, if do not
147 // issue sub as part of the sequence if condition register is to be
148 // preserved.
149 if (NumBytes < 0 && !isHigh && CanChangeCC) {
150 isSub = true;
151 NumBytes = -NumBytes;
152 }
153 Register LdReg = DestReg;
154 if (DestReg == ARM::SP)
155 assert(BaseReg == ARM::SP && "Unexpected!");
156 if (!isARMLowRegister(DestReg) && !DestReg.isVirtual())
157 LdReg = MF.getRegInfo().createVirtualRegister(&ARM::tGPRRegClass);
158
159 if (NumBytes <= 255 && NumBytes >= 0 && CanChangeCC) {
160 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVi8), LdReg)
161 .add(t1CondCodeOp())
162 .addImm(NumBytes)
163 .setMIFlags(MIFlags);
164 } else if (NumBytes < 0 && NumBytes >= -255 && CanChangeCC) {
165 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVi8), LdReg)
166 .add(t1CondCodeOp())
167 .addImm(NumBytes)
168 .setMIFlags(MIFlags);
169 BuildMI(MBB, MBBI, dl, TII.get(ARM::tRSB), LdReg)
170 .add(t1CondCodeOp())
171 .addReg(LdReg, RegState::Kill)
172 .setMIFlags(MIFlags);
173 } else if (ST.genExecuteOnly()) {
174 if (ST.useMovt()) {
175 BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVi32imm ), LdReg)
176 .addImm(NumBytes)
177 .setMIFlags(MIFlags);
178 } else if (!CanChangeCC) {
179 // tMOVi32imm is lowered to a sequence of flag-setting instructions, so
180 // if CPSR is live we need to save and restore CPSR around it.
181 // TODO Try inserting the tMOVi32imm at an earlier point, where CPSR is
182 // dead.
183 bool LiveCpsr = false, CpsrWrite = false;
184 auto isCpsr = [](auto &MO) { return MO.getReg() == ARM::CPSR; };
185 for (auto Iter = MBBI; Iter != MBB.instr_end(); ++Iter) {
186 // If CPSR is used after this instruction (and there's not a def before
187 // that) then CPSR is live.
188 if (any_of(Iter->all_uses(), isCpsr)) {
189 LiveCpsr = true;
190 break;
191 }
192 if (any_of(Iter->all_defs(), isCpsr)) {
193 CpsrWrite = true;
194 break;
195 }
196 }
197 // If there's no use or def of CPSR then it may be live if it's a
198 // live-out value.
199 auto liveOutIsCpsr = [](auto &Out) { return Out.PhysReg == ARM::CPSR; };
200 if (!LiveCpsr && !CpsrWrite)
201 LiveCpsr = any_of(MBB.liveouts(), liveOutIsCpsr);
202
203 Register CPSRSaveReg;
204 unsigned APSREncoding;
205 if (LiveCpsr) {
206 CPSRSaveReg = MF.getRegInfo().createVirtualRegister(&ARM::tGPRRegClass);
207 APSREncoding =
208 ARMSysReg::lookupMClassSysRegByName("apsr_nzcvq")->Encoding;
209 BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MRS_M), CPSRSaveReg)
210 .addImm(APSREncoding)
212 .addReg(ARM::CPSR, RegState::Implicit);
213 }
214 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVi32imm), LdReg)
215 .addImm(NumBytes)
216 .setMIFlags(MIFlags);
217 if (LiveCpsr) {
218 BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MSR_M))
219 .addImm(APSREncoding)
220 .addReg(CPSRSaveReg, RegState::Kill)
222 }
223 } else {
224 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVi32imm), LdReg)
225 .addImm(NumBytes)
226 .setMIFlags(MIFlags);
227 }
228 } else
229 MRI.emitLoadConstPool(MBB, MBBI, dl, LdReg, 0, NumBytes, ARMCC::AL, 0,
230 MIFlags);
231
232 // Emit add / sub.
233 int Opc = (isSub) ? ARM::tSUBrr
234 : ((isHigh || !CanChangeCC) ? ARM::tADDhirr : ARM::tADDrr);
235 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg);
236 if (Opc != ARM::tADDhirr)
237 MIB = MIB.add(t1CondCodeOp());
238 if (DestReg == ARM::SP || isSub)
239 MIB.addReg(BaseReg).addReg(LdReg, RegState::Kill);
240 else
241 MIB.addReg(LdReg).addReg(BaseReg, RegState::Kill);
242 MIB.add(predOps(ARMCC::AL));
243}
244
245/// emitThumbRegPlusImmediate - Emits a series of instructions to materialize
246/// a destreg = basereg + immediate in Thumb code. Tries a series of ADDs or
247/// SUBs first, and uses a constant pool value if the instruction sequence would
248/// be too long. This is allowed to modify the condition flags.
251 const DebugLoc &dl, Register DestReg,
252 Register BaseReg, int NumBytes,
253 const TargetInstrInfo &TII,
255 unsigned MIFlags) {
256 bool isSub = NumBytes < 0;
257 unsigned Bytes = (unsigned)NumBytes;
258 if (isSub) Bytes = -NumBytes;
259
260 int CopyOpc = 0;
261 unsigned CopyBits = 0;
262 unsigned CopyScale = 1;
263 bool CopyNeedsCC = false;
264 int ExtraOpc = 0;
265 unsigned ExtraBits = 0;
266 unsigned ExtraScale = 1;
267 bool ExtraNeedsCC = false;
268
269 // Strategy:
270 // We need to select two types of instruction, maximizing the available
271 // immediate range of each. The instructions we use will depend on whether
272 // DestReg and BaseReg are low, high or the stack pointer.
273 // * CopyOpc - DestReg = BaseReg + imm
274 // This will be emitted once if DestReg != BaseReg, and never if
275 // DestReg == BaseReg.
276 // * ExtraOpc - DestReg = DestReg + imm
277 // This will be emitted as many times as necessary to add the
278 // full immediate.
279 // If the immediate ranges of these instructions are not large enough to cover
280 // NumBytes with a reasonable number of instructions, we fall back to using a
281 // value loaded from a constant pool.
282 if (DestReg == ARM::SP) {
283 if (BaseReg == ARM::SP) {
284 // sp -> sp
285 // Already in right reg, no copy needed
286 } else {
287 // low -> sp or high -> sp
288 CopyOpc = ARM::tMOVr;
289 CopyBits = 0;
290 }
291 ExtraOpc = isSub ? ARM::tSUBspi : ARM::tADDspi;
292 ExtraBits = 7;
293 ExtraScale = 4;
294 } else if (isARMLowRegister(DestReg)) {
295 if (BaseReg == ARM::SP) {
296 // sp -> low
297 assert(!isSub && "Thumb1 does not have tSUBrSPi");
298 CopyOpc = ARM::tADDrSPi;
299 CopyBits = 8;
300 CopyScale = 4;
301 } else if (DestReg == BaseReg) {
302 // low -> same low
303 // Already in right reg, no copy needed
304 } else if (isARMLowRegister(BaseReg)) {
305 // low -> different low
306 CopyOpc = isSub ? ARM::tSUBi3 : ARM::tADDi3;
307 CopyBits = 3;
308 CopyNeedsCC = true;
309 } else {
310 // high -> low
311 CopyOpc = ARM::tMOVr;
312 CopyBits = 0;
313 }
314 ExtraOpc = isSub ? ARM::tSUBi8 : ARM::tADDi8;
315 ExtraBits = 8;
316 ExtraNeedsCC = true;
317 } else /* DestReg is high */ {
318 if (DestReg == BaseReg) {
319 // high -> same high
320 // Already in right reg, no copy needed
321 } else {
322 // {low,high,sp} -> high
323 CopyOpc = ARM::tMOVr;
324 CopyBits = 0;
325 }
326 ExtraOpc = 0;
327 }
328
329 // We could handle an unaligned immediate with an unaligned copy instruction
330 // and an aligned extra instruction, but this case is not currently needed.
331 assert(((Bytes & 3) == 0 || ExtraScale == 1) &&
332 "Unaligned offset, but all instructions require alignment");
333
334 unsigned CopyRange = ((1 << CopyBits) - 1) * CopyScale;
335 // If we would emit the copy with an immediate of 0, just use tMOVr.
336 if (CopyOpc && Bytes < CopyScale) {
337 CopyOpc = ARM::tMOVr;
338 CopyScale = 1;
339 CopyNeedsCC = false;
340 CopyRange = 0;
341 }
342 unsigned ExtraRange = ((1 << ExtraBits) - 1) * ExtraScale; // per instruction
343 unsigned RequiredCopyInstrs = CopyOpc ? 1 : 0;
344 unsigned RangeAfterCopy = (CopyRange > Bytes) ? 0 : (Bytes - CopyRange);
345
346 // We could handle this case when the copy instruction does not require an
347 // aligned immediate, but we do not currently do this.
348 assert(RangeAfterCopy % ExtraScale == 0 &&
349 "Extra instruction requires immediate to be aligned");
350
351 unsigned RequiredExtraInstrs;
352 if (ExtraRange)
353 RequiredExtraInstrs = alignTo(RangeAfterCopy, ExtraRange) / ExtraRange;
354 else if (RangeAfterCopy > 0)
355 // We need an extra instruction but none is available
356 RequiredExtraInstrs = 1000000;
357 else
358 RequiredExtraInstrs = 0;
359 unsigned RequiredInstrs = RequiredCopyInstrs + RequiredExtraInstrs;
360 unsigned Threshold = (DestReg == ARM::SP) ? 3 : 2;
361
362 // Use a constant pool, if the sequence of ADDs/SUBs is too expensive.
363 if (RequiredInstrs > Threshold) {
365 DestReg, BaseReg, NumBytes, true,
366 TII, MRI, MIFlags);
367 return;
368 }
369
370 // Emit zero or one copy instructions
371 if (CopyOpc) {
372 unsigned CopyImm = std::min(Bytes, CopyRange) / CopyScale;
373 Bytes -= CopyImm * CopyScale;
374
375 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, dl, TII.get(CopyOpc), DestReg);
376 if (CopyNeedsCC)
377 MIB = MIB.add(t1CondCodeOp());
378 MIB.addReg(BaseReg, RegState::Kill);
379 if (CopyOpc != ARM::tMOVr) {
380 MIB.addImm(CopyImm);
381 }
382 MIB.setMIFlags(MIFlags).add(predOps(ARMCC::AL));
383
384 BaseReg = DestReg;
385 }
386
387 // Emit zero or more in-place add/sub instructions
388 while (Bytes) {
389 unsigned ExtraImm = std::min(Bytes, ExtraRange) / ExtraScale;
390 Bytes -= ExtraImm * ExtraScale;
391
392 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, dl, TII.get(ExtraOpc), DestReg);
393 if (ExtraNeedsCC)
394 MIB = MIB.add(t1CondCodeOp());
395 MIB.addReg(BaseReg)
396 .addImm(ExtraImm)
398 .setMIFlags(MIFlags);
399 }
400}
401
402static void removeOperands(MachineInstr &MI, unsigned i) {
403 unsigned Op = i;
404 for (unsigned e = MI.getNumOperands(); i != e; ++i)
405 MI.removeOperand(Op);
406}
407
408/// convertToNonSPOpcode - Change the opcode to the non-SP version, because
409/// we're replacing the frame index with a non-SP register.
410static unsigned convertToNonSPOpcode(unsigned Opcode) {
411 switch (Opcode) {
412 case ARM::tLDRspi:
413 return ARM::tLDRi;
414
415 case ARM::tSTRspi:
416 return ARM::tSTRi;
417 }
418
419 return Opcode;
420}
421
423 unsigned FrameRegIdx,
424 Register FrameReg, int &Offset,
425 const ARMBaseInstrInfo &TII) const {
426 MachineInstr &MI = *II;
427 MachineBasicBlock &MBB = *MI.getParent();
430 "This isn't needed for thumb2!");
431 DebugLoc dl = MI.getDebugLoc();
433 unsigned Opcode = MI.getOpcode();
434 const MCInstrDesc &Desc = MI.getDesc();
435 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
436
437 if (Opcode == ARM::tADDframe) {
438 Offset += MI.getOperand(FrameRegIdx+1).getImm();
439 Register DestReg = MI.getOperand(0).getReg();
440
441 emitThumbRegPlusImmediate(MBB, II, dl, DestReg, FrameReg, Offset, TII,
442 *this);
443 MBB.erase(II);
444 return true;
445 } else {
447 llvm_unreachable("Unsupported addressing mode!");
448
449 unsigned ImmIdx = FrameRegIdx + 1;
450 int InstrOffs = MI.getOperand(ImmIdx).getImm();
451 unsigned NumBits = (FrameReg == ARM::SP) ? 8 : 5;
452 unsigned Scale = 4;
453
454 Offset += InstrOffs * Scale;
455 assert((Offset & (Scale - 1)) == 0 && "Can't encode this offset!");
456
457 // Common case: small offset, fits into instruction.
458 MachineOperand &ImmOp = MI.getOperand(ImmIdx);
459 int ImmedOffset = Offset / Scale;
460 unsigned Mask = (1 << NumBits) - 1;
461
462 if ((unsigned)Offset <= Mask * Scale) {
463 // Replace the FrameIndex with the frame register (e.g., sp).
464 Register DestReg = FrameReg;
465
466 // In case FrameReg is a high register, move it to a low reg to ensure it
467 // can be used as an operand.
468 if (ARM::hGPRRegClass.contains(FrameReg) && FrameReg != ARM::SP) {
469 DestReg = MF.getRegInfo().createVirtualRegister(&ARM::tGPRRegClass);
470 BuildMI(MBB, II, dl, TII.get(ARM::tMOVr), DestReg)
471 .addReg(FrameReg)
473 }
474
475 MI.getOperand(FrameRegIdx).ChangeToRegister(DestReg, false);
476 ImmOp.ChangeToImmediate(ImmedOffset);
477
478 // If we're using a register where sp was stored, convert the instruction
479 // to the non-SP version.
480 unsigned NewOpc = convertToNonSPOpcode(Opcode);
481 if (NewOpc != Opcode && FrameReg != ARM::SP)
482 MI.setDesc(TII.get(NewOpc));
483
484 return true;
485 }
486
487 // The offset doesn't fit, but we may be able to put some of the offset into
488 // the ldr to simplify the generation of the rest of it.
489 NumBits = 5;
490 Mask = (1 << NumBits) - 1;
491 InstrOffs = 0;
492 auto &ST = MF.getSubtarget<ARMSubtarget>();
493 // If using the maximum ldr offset will put the rest into the range of a
494 // single sp-relative add then do so.
495 if (FrameReg == ARM::SP && Offset - (Mask * Scale) <= 1020) {
496 InstrOffs = Mask;
497 } else if (ST.genExecuteOnly()) {
498 // With execute-only the offset is generated either with movw+movt or an
499 // add+lsl sequence. If subtracting an offset will make the top half zero
500 // then that saves a movt or lsl+add. Otherwise if we don't have movw then
501 // we may be able to subtract a value such that it makes the bottom byte
502 // zero, saving an add.
503 unsigned BottomBits = (Offset / Scale) & Mask;
504 bool CanMakeBottomByteZero = ((Offset - BottomBits * Scale) & 0xff) == 0;
505 bool TopHalfZero = (Offset & 0xffff0000) == 0;
506 bool CanMakeTopHalfZero = ((Offset - Mask * Scale) & 0xffff0000) == 0;
507 if (!TopHalfZero && CanMakeTopHalfZero)
508 InstrOffs = Mask;
509 else if (!ST.useMovt() && CanMakeBottomByteZero)
510 InstrOffs = BottomBits;
511 }
512 ImmOp.ChangeToImmediate(InstrOffs);
513 Offset -= InstrOffs * Scale;
514 }
515
516 return Offset == 0;
517}
518
520 int64_t Offset) const {
521 const MachineFunction &MF = *MI.getParent()->getParent();
522 const ARMSubtarget &STI = MF.getSubtarget<ARMSubtarget>();
523 if (!STI.isThumb1Only())
525
526 const ARMBaseInstrInfo &TII = *STI.getInstrInfo();
527 int Off = Offset; // ARM doesn't need the general 64-bit offsets
528 unsigned i = 0;
529
530 while (!MI.getOperand(i).isFI()) {
531 ++i;
532 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
533 }
534 bool Done = rewriteFrameIndex(MI, i, BaseReg, Off, TII);
535 assert (Done && "Unable to resolve frame index!");
536 (void)Done;
537}
538
540 int SPAdj, unsigned FIOperandNum,
541 RegScavenger *RS) const {
542 MachineInstr &MI = *II;
543 MachineBasicBlock &MBB = *MI.getParent();
545 const ARMSubtarget &STI = MF.getSubtarget<ARMSubtarget>();
546 if (!STI.isThumb1Only())
547 return ARMBaseRegisterInfo::eliminateFrameIndex(II, SPAdj, FIOperandNum,
548 RS);
549
550 Register VReg;
551 const ARMBaseInstrInfo &TII = *STI.getInstrInfo();
552 DebugLoc dl = MI.getDebugLoc();
554
555 Register FrameReg;
556 int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
557 const ARMFrameLowering *TFI = getFrameLowering(MF);
558 int Offset = TFI->ResolveFrameIndexReference(MF, FrameIndex, FrameReg, SPAdj);
559
560 // PEI::scavengeFrameVirtualRegs() cannot accurately track SPAdj because the
561 // call frame setup/destroy instructions have already been eliminated. That
562 // means the stack pointer cannot be used to access the emergency spill slot
563 // when !hasReservedCallFrame().
564#ifndef NDEBUG
565 if (RS && FrameReg == ARM::SP && RS->isScavengingFrameIndex(FrameIndex)){
567 "Cannot use SP to access the emergency spill slot in "
568 "functions without a reserved call frame");
570 "Cannot use SP to access the emergency spill slot in "
571 "functions with variable sized frame objects");
572 }
573#endif // NDEBUG
574
575 // Special handling of dbg_value instructions.
576 if (MI.isDebugValue()) {
577 MI.getOperand(FIOperandNum). ChangeToRegister(FrameReg, false /*isDef*/);
578 MI.getOperand(FIOperandNum+1).ChangeToImmediate(Offset);
579 return false;
580 }
581
582 // Modify MI as necessary to handle as much of 'Offset' as possible
584 "This eliminateFrameIndex only supports Thumb1!");
585 if (rewriteFrameIndex(MI, FIOperandNum, FrameReg, Offset, TII))
586 return true;
587
588 // If we get here, the immediate doesn't fit into the instruction. We folded
589 // as much as possible above, handle the rest, providing a register that is
590 // SP+LargeImm.
591 assert(Offset && "This code isn't needed if offset already handled!");
592
593 unsigned Opcode = MI.getOpcode();
594
595 // Remove predicate first.
596 int PIdx = MI.findFirstPredOperandIdx();
597 if (PIdx != -1)
598 removeOperands(MI, PIdx);
599
600 if (MI.mayLoad()) {
601 // Use the destination register to materialize sp + offset.
602 Register TmpReg = MI.getOperand(0).getReg();
603 bool UseRR = false;
604 if (Opcode == ARM::tLDRspi) {
605 if (FrameReg == ARM::SP || STI.genExecuteOnly())
606 emitThumbRegPlusImmInReg(MBB, II, dl, TmpReg, FrameReg,
607 Offset, false, TII, *this);
608 else {
609 emitLoadConstPool(MBB, II, dl, TmpReg, 0, Offset);
610 if (!ARM::hGPRRegClass.contains(FrameReg)) {
611 UseRR = true;
612 } else {
613 // If FrameReg is a high register, add the reg values in a separate
614 // instruction as the load won't be able to access it.
615 BuildMI(MBB, II, dl, TII.get(ARM::tADDhirr), TmpReg)
616 .addReg(TmpReg)
617 .addReg(FrameReg)
619 }
620 }
621 } else {
622 emitThumbRegPlusImmediate(MBB, II, dl, TmpReg, FrameReg, Offset, TII,
623 *this);
624 }
625
626 MI.setDesc(TII.get(UseRR ? ARM::tLDRr : ARM::tLDRi));
627 MI.getOperand(FIOperandNum).ChangeToRegister(TmpReg, false, false, true);
628 if (UseRR) {
629 assert(!ARM::hGPRRegClass.contains(FrameReg) &&
630 "Thumb1 loads can't use high register");
631 // Use [reg, reg] addrmode. Replace the immediate operand w/ the frame
632 // register. The offset is already handled in the vreg value.
633 MI.getOperand(FIOperandNum+1).ChangeToRegister(FrameReg, false, false,
634 false);
635 }
636 } else if (MI.mayStore()) {
637 VReg = MF.getRegInfo().createVirtualRegister(&ARM::tGPRRegClass);
638 bool UseRR = false;
639
640 if (Opcode == ARM::tSTRspi) {
641 if (FrameReg == ARM::SP || STI.genExecuteOnly())
642 emitThumbRegPlusImmInReg(MBB, II, dl, VReg, FrameReg,
643 Offset, false, TII, *this);
644 else {
645 emitLoadConstPool(MBB, II, dl, VReg, 0, Offset);
646 if (!ARM::hGPRRegClass.contains(FrameReg)) {
647 UseRR = true;
648 } else {
649 // If FrameReg is a high register, add the reg values in a separate
650 // instruction as the load won't be able to access it.
651 BuildMI(MBB, II, dl, TII.get(ARM::tADDhirr), VReg)
652 .addReg(VReg)
653 .addReg(FrameReg)
655 }
656 }
657 } else
658 emitThumbRegPlusImmediate(MBB, II, dl, VReg, FrameReg, Offset, TII,
659 *this);
660 MI.setDesc(TII.get(UseRR ? ARM::tSTRr : ARM::tSTRi));
661 MI.getOperand(FIOperandNum).ChangeToRegister(VReg, false, false, true);
662 if (UseRR) {
663 assert(!ARM::hGPRRegClass.contains(FrameReg) &&
664 "Thumb1 stores can't use high register");
665 // Use [reg, reg] addrmode. Replace the immediate operand w/ the frame
666 // register. The offset is already handled in the vreg value.
667 MI.getOperand(FIOperandNum+1).ChangeToRegister(FrameReg, false, false,
668 false);
669 }
670 } else {
671 llvm_unreachable("Unexpected opcode!");
672 }
673
674 // Add predicate back if it's needed.
675 if (MI.isPredicable())
676 MIB.add(predOps(ARMCC::AL));
677 return false;
678}
679
680bool
683 // For Thumb1, the emergency spill slot must be some small positive
684 // offset from the base/stack pointer.
685 return false;
686 }
687 // For Thumb2, put the emergency spill slot next to FP.
688 return true;
689}
unsigned const MachineRegisterInfo * MRI
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator MBBI
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
This file declares the MachineConstantPool class which is an abstract constant pool to keep track of ...
uint64_t IntrinsicInst * II
This file declares the machine register scavenger class.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition: Value.cpp:469
static void emitThumbRegPlusImmInReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI, const DebugLoc &dl, Register DestReg, Register BaseReg, int NumBytes, bool CanChangeCC, const TargetInstrInfo &TII, const ARMBaseRegisterInfo &MRI, unsigned MIFlags=MachineInstr::NoFlags)
emitThumbRegPlusImmInReg - Emits a series of instructions to materialize a destreg = basereg + immedi...
static void removeOperands(MachineInstr &MI, unsigned i)
static void emitThumb1LoadConstPool(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI, const DebugLoc &dl, unsigned DestReg, unsigned SubIdx, int Val, ARMCC::CondCodes Pred, unsigned PredReg, unsigned MIFlags)
static void emitThumb2LoadConstPool(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI, const DebugLoc &dl, unsigned DestReg, unsigned SubIdx, int Val, ARMCC::CondCodes Pred, unsigned PredReg, unsigned MIFlags)
static unsigned convertToNonSPOpcode(unsigned Opcode)
convertToNonSPOpcode - Change the opcode to the non-SP version, because we're replacing the frame ind...
bool eliminateFrameIndex(MachineBasicBlock::iterator II, int SPAdj, unsigned FIOperandNum, RegScavenger *RS=nullptr) const override
const TargetRegisterClass * getLargestLegalSuperClass(const TargetRegisterClass *RC, const MachineFunction &MF) const override
void resolveFrameIndex(MachineInstr &MI, Register BaseReg, int64_t Offset) const override
const TargetRegisterClass * getPointerRegClass(const MachineFunction &MF, unsigned Kind=0) const override
bool hasReservedCallFrame(const MachineFunction &MF) const override
hasReservedCallFrame - Under normal circumstances, when a frame pointer is not required,...
int ResolveFrameIndexReference(const MachineFunction &MF, int FI, Register &FrameReg, int SPAdj) const
ARMFunctionInfo - This class is derived from MachineFunctionInfo and contains private ARM-specific in...
const ARMBaseInstrInfo * getInstrInfo() const override
Definition: ARMSubtarget.h:238
bool isThumb1Only() const
Definition: ARMSubtarget.h:403
const ARMFrameLowering * getFrameLowering() const override
Definition: ARMSubtarget.h:246
This is an important base class in LLVM.
Definition: Constant.h:42
This class represents an Operation in the Expression.
A debug info location.
Definition: DebugLoc.h:33
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition: Function.cpp:369
Describe properties that are true of each instruction in the target description file.
Definition: MCInstrDesc.h:198
iterator_range< liveout_iterator > liveouts() const
instr_iterator instr_end()
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
instr_iterator erase(instr_iterator I)
Remove an instruction from the instruction list and delete it.
The MachineConstantPool class keeps track of constants referenced by a function which must be spilled...
bool hasVarSizedObjects() const
This method may be called any time after instruction selection is complete to determine if the stack ...
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.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
MachineConstantPool * getConstantPool()
getConstantPool - Return the constant pool object for the current function.
Register getReg(unsigned Idx) const
Get the register for the operand index.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & add(const MachineOperand &MO) const
const MachineInstrBuilder & addConstantPoolIndex(unsigned Idx, int Offset=0, unsigned TargetFlags=0) const
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & setMIFlags(unsigned Flags) const
Representation of each machine instruction.
Definition: MachineInstr.h:69
MachineOperand class - Representation of each machine instruction operand.
void ChangeToImmediate(int64_t ImmVal, unsigned TargetFlags=0)
ChangeToImmediate - Replace this operand with a new immediate operand of the specified value.
Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
bool isScavengingFrameIndex(int FI) const
Query whether a frame index is a scavenging frame index.
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition: Register.h:91
TargetInstrInfo - Interface to description of machine instruction set.
virtual const TargetInstrInfo * getInstrInfo() const
static IntegerType * getInt32Ty(LLVMContext &C)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
@ Implicit
Not emitted register (e.g. carry, or temporary result).
@ Kill
The last use of a register.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
cl::opt< bool > ReuseFrameIndexVals
@ Offset
Definition: DWP.cpp:480
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
@ Done
Definition: Threading.h:61
static bool isARMLowRegister(MCRegister Reg)
isARMLowRegister - Returns true if the register is a low register (r0-r7).
Definition: ARMBaseInfo.h:160
static std::array< MachineOperand, 2 > predOps(ARMCC::CondCodes Pred, unsigned PredReg=0)
Get the operands corresponding to the given Pred value.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1746
unsigned getDefRegState(bool B)
void emitThumbRegPlusImmediate(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI, const DebugLoc &dl, Register DestReg, Register BaseReg, int NumBytes, const TargetInstrInfo &TII, const ARMBaseRegisterInfo &MRI, unsigned MIFlags=0)
emitThumbRegPlusImmediate - Emits a series of instructions to materialize a destreg = basereg + immed...
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:155
static MachineOperand t1CondCodeOp(bool isDead=false)
Get the operand corresponding to the conditional code result for Thumb1.
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
Description of the encoding of one expression Op.
bool useFPForScavengingIndex(const MachineFunction &MF) const override
void emitLoadConstPool(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI, const DebugLoc &dl, Register DestReg, unsigned SubIdx, int Val, ARMCC::CondCodes Pred=ARMCC::AL, Register PredReg=Register(), unsigned MIFlags=MachineInstr::NoFlags) const override
emitLoadConstPool - Emits a load from constpool to materialize the specified immediate.
bool eliminateFrameIndex(MachineBasicBlock::iterator II, int SPAdj, unsigned FIOperandNum, RegScavenger *RS=nullptr) const override
const TargetRegisterClass * getLargestLegalSuperClass(const TargetRegisterClass *RC, const MachineFunction &MF) const override
const TargetRegisterClass * getPointerRegClass(const MachineFunction &MF, unsigned Kind=0) const override
void resolveFrameIndex(MachineInstr &MI, Register BaseReg, int64_t Offset) const override
bool rewriteFrameIndex(MachineBasicBlock::iterator II, unsigned FrameRegIdx, Register FrameReg, int &Offset, const ARMBaseInstrInfo &TII) const