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