1 //===-- AVRInstrInfo.cpp - AVR Instruction Information --------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file contains the AVR implementation of the TargetInstrInfo class.
11 //===----------------------------------------------------------------------===//
13 #include "AVRInstrInfo.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/CodeGen/MachineConstantPool.h"
17 #include "llvm/CodeGen/MachineFrameInfo.h"
18 #include "llvm/CodeGen/MachineInstrBuilder.h"
19 #include "llvm/CodeGen/MachineMemOperand.h"
20 #include "llvm/IR/Constants.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/MC/MCContext.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/TargetRegistry.h"
28 #include "AVRMachineFunctionInfo.h"
29 #include "AVRRegisterInfo.h"
30 #include "AVRTargetMachine.h"
31 #include "MCTargetDesc/AVRMCTargetDesc.h"
33 #define GET_INSTRINFO_CTOR_DTOR
34 #include "AVRGenInstrInfo.inc"
38 AVRInstrInfo::AVRInstrInfo()
39 : AVRGenInstrInfo(AVR::ADJCALLSTACKDOWN, AVR::ADJCALLSTACKUP), RI() {}
41 void AVRInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
42 MachineBasicBlock::iterator MI,
43 const DebugLoc &DL, unsigned DestReg,
44 unsigned SrcReg, bool KillSrc) const {
45 const AVRSubtarget &STI = MBB.getParent()->getSubtarget<AVRSubtarget>();
46 const AVRRegisterInfo &TRI = *STI.getRegisterInfo();
49 // Not all AVR devices support the 16-bit `MOVW` instruction.
50 if (AVR::DREGSRegClass.contains(DestReg, SrcReg)) {
52 BuildMI(MBB, MI, DL, get(AVR::MOVWRdRr), DestReg)
53 .addReg(SrcReg, getKillRegState(KillSrc));
55 unsigned DestLo, DestHi, SrcLo, SrcHi;
57 TRI.splitReg(DestReg, DestLo, DestHi);
58 TRI.splitReg(SrcReg, SrcLo, SrcHi);
60 // Copy each individual register with the `MOV` instruction.
61 BuildMI(MBB, MI, DL, get(AVR::MOVRdRr), DestLo)
62 .addReg(SrcLo, getKillRegState(KillSrc));
63 BuildMI(MBB, MI, DL, get(AVR::MOVRdRr), DestHi)
64 .addReg(SrcHi, getKillRegState(KillSrc));
67 if (AVR::GPR8RegClass.contains(DestReg, SrcReg)) {
69 } else if (SrcReg == AVR::SP && AVR::DREGSRegClass.contains(DestReg)) {
71 } else if (DestReg == AVR::SP && AVR::DREGSRegClass.contains(SrcReg)) {
74 llvm_unreachable("Impossible reg-to-reg copy");
77 BuildMI(MBB, MI, DL, get(Opc), DestReg)
78 .addReg(SrcReg, getKillRegState(KillSrc));
82 unsigned AVRInstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
83 int &FrameIndex) const {
84 switch (MI.getOpcode()) {
86 case AVR::LDDWRdYQ: { //:FIXME: remove this once PR13375 gets fixed
87 if (MI.getOperand(1).isFI() && MI.getOperand(2).isImm() &&
88 MI.getOperand(2).getImm() == 0) {
89 FrameIndex = MI.getOperand(1).getIndex();
90 return MI.getOperand(0).getReg();
101 unsigned AVRInstrInfo::isStoreToStackSlot(const MachineInstr &MI,
102 int &FrameIndex) const {
103 switch (MI.getOpcode()) {
105 case AVR::STDWPtrQRr: {
106 if (MI.getOperand(0).isFI() && MI.getOperand(1).isImm() &&
107 MI.getOperand(1).getImm() == 0) {
108 FrameIndex = MI.getOperand(0).getIndex();
109 return MI.getOperand(2).getReg();
120 void AVRInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
121 MachineBasicBlock::iterator MI,
122 unsigned SrcReg, bool isKill,
124 const TargetRegisterClass *RC,
125 const TargetRegisterInfo *TRI) const {
126 MachineFunction &MF = *MBB.getParent();
127 AVRMachineFunctionInfo *AFI = MF.getInfo<AVRMachineFunctionInfo>();
129 AFI->setHasSpills(true);
132 if (MI != MBB.end()) {
133 DL = MI->getDebugLoc();
136 const MachineFrameInfo &MFI = MF.getFrameInfo();
138 MachineMemOperand *MMO = MF.getMachineMemOperand(
139 MachinePointerInfo::getFixedStack(MF, FrameIndex),
140 MachineMemOperand::MOStore, MFI.getObjectSize(FrameIndex),
141 MFI.getObjectAlignment(FrameIndex));
144 if (TRI->isTypeLegalForClass(*RC, MVT::i8)) {
145 Opcode = AVR::STDPtrQRr;
146 } else if (TRI->isTypeLegalForClass(*RC, MVT::i16)) {
147 Opcode = AVR::STDWPtrQRr;
149 llvm_unreachable("Cannot store this register into a stack slot!");
152 BuildMI(MBB, MI, DL, get(Opcode))
153 .addFrameIndex(FrameIndex)
155 .addReg(SrcReg, getKillRegState(isKill))
159 void AVRInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
160 MachineBasicBlock::iterator MI,
161 unsigned DestReg, int FrameIndex,
162 const TargetRegisterClass *RC,
163 const TargetRegisterInfo *TRI) const {
165 if (MI != MBB.end()) {
166 DL = MI->getDebugLoc();
169 MachineFunction &MF = *MBB.getParent();
170 const MachineFrameInfo &MFI = MF.getFrameInfo();
172 MachineMemOperand *MMO = MF.getMachineMemOperand(
173 MachinePointerInfo::getFixedStack(MF, FrameIndex),
174 MachineMemOperand::MOLoad, MFI.getObjectSize(FrameIndex),
175 MFI.getObjectAlignment(FrameIndex));
178 if (TRI->isTypeLegalForClass(*RC, MVT::i8)) {
179 Opcode = AVR::LDDRdPtrQ;
180 } else if (TRI->isTypeLegalForClass(*RC, MVT::i16)) {
181 // Opcode = AVR::LDDWRdPtrQ;
182 //:FIXME: remove this once PR13375 gets fixed
183 Opcode = AVR::LDDWRdYQ;
185 llvm_unreachable("Cannot load this register from a stack slot!");
188 BuildMI(MBB, MI, DL, get(Opcode), DestReg)
189 .addFrameIndex(FrameIndex)
194 const MCInstrDesc &AVRInstrInfo::getBrCond(AVRCC::CondCodes CC) const {
197 llvm_unreachable("Unknown condition code!");
199 return get(AVR::BREQk);
201 return get(AVR::BRNEk);
203 return get(AVR::BRGEk);
205 return get(AVR::BRLTk);
207 return get(AVR::BRSHk);
209 return get(AVR::BRLOk);
211 return get(AVR::BRMIk);
213 return get(AVR::BRPLk);
217 AVRCC::CondCodes AVRInstrInfo::getCondFromBranchOpc(unsigned Opc) const {
220 return AVRCC::COND_INVALID;
222 return AVRCC::COND_EQ;
224 return AVRCC::COND_NE;
226 return AVRCC::COND_SH;
228 return AVRCC::COND_LO;
230 return AVRCC::COND_MI;
232 return AVRCC::COND_PL;
234 return AVRCC::COND_GE;
236 return AVRCC::COND_LT;
240 AVRCC::CondCodes AVRInstrInfo::getOppositeCondition(AVRCC::CondCodes CC) const {
243 llvm_unreachable("Invalid condition!");
245 return AVRCC::COND_NE;
247 return AVRCC::COND_EQ;
249 return AVRCC::COND_LO;
251 return AVRCC::COND_SH;
253 return AVRCC::COND_LT;
255 return AVRCC::COND_GE;
257 return AVRCC::COND_PL;
259 return AVRCC::COND_MI;
263 bool AVRInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
264 MachineBasicBlock *&TBB,
265 MachineBasicBlock *&FBB,
266 SmallVectorImpl<MachineOperand> &Cond,
267 bool AllowModify) const {
268 // Start from the bottom of the block and work up, examining the
269 // terminator instructions.
270 MachineBasicBlock::iterator I = MBB.end();
271 MachineBasicBlock::iterator UnCondBrIter = MBB.end();
273 while (I != MBB.begin()) {
275 if (I->isDebugInstr()) {
279 // Working from the bottom, when we see a non-terminator
280 // instruction, we're done.
281 if (!isUnpredicatedTerminator(*I)) {
285 // A terminator that isn't a branch can't easily be handled
287 if (!I->getDesc().isBranch()) {
291 // Handle unconditional branches.
292 //:TODO: add here jmp
293 if (I->getOpcode() == AVR::RJMPk) {
297 TBB = I->getOperand(0).getMBB();
301 // If the block has any instructions after a JMP, delete them.
302 while (std::next(I) != MBB.end()) {
303 std::next(I)->eraseFromParent();
309 // Delete the JMP if it's equivalent to a fall-through.
310 if (MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
312 I->eraseFromParent();
314 UnCondBrIter = MBB.end();
318 // TBB is used to indicate the unconditinal destination.
319 TBB = I->getOperand(0).getMBB();
323 // Handle conditional branches.
324 AVRCC::CondCodes BranchCode = getCondFromBranchOpc(I->getOpcode());
325 if (BranchCode == AVRCC::COND_INVALID) {
326 return true; // Can't handle indirect branch.
329 // Working from the bottom, handle the first conditional branch.
331 MachineBasicBlock *TargetBB = I->getOperand(0).getMBB();
332 if (AllowModify && UnCondBrIter != MBB.end() &&
333 MBB.isLayoutSuccessor(TargetBB)) {
334 // If we can modify the code and it ends in something like:
342 // Then we can change this to:
349 // Which is a bit more efficient.
350 // We conditionally jump to the fall-through block.
351 BranchCode = getOppositeCondition(BranchCode);
352 unsigned JNCC = getBrCond(BranchCode).getOpcode();
353 MachineBasicBlock::iterator OldInst = I;
355 BuildMI(MBB, UnCondBrIter, MBB.findDebugLoc(I), get(JNCC))
356 .addMBB(UnCondBrIter->getOperand(0).getMBB());
357 BuildMI(MBB, UnCondBrIter, MBB.findDebugLoc(I), get(AVR::RJMPk))
360 OldInst->eraseFromParent();
361 UnCondBrIter->eraseFromParent();
363 // Restart the analysis.
364 UnCondBrIter = MBB.end();
370 TBB = I->getOperand(0).getMBB();
371 Cond.push_back(MachineOperand::CreateImm(BranchCode));
375 // Handle subsequent conditional branches. Only handle the case where all
376 // conditional branches branch to the same destination.
377 assert(Cond.size() == 1);
380 // Only handle the case where all conditional branches branch to
381 // the same destination.
382 if (TBB != I->getOperand(0).getMBB()) {
386 AVRCC::CondCodes OldBranchCode = (AVRCC::CondCodes)Cond[0].getImm();
387 // If the conditions are the same, we can leave them alone.
388 if (OldBranchCode == BranchCode) {
398 unsigned AVRInstrInfo::insertBranch(MachineBasicBlock &MBB,
399 MachineBasicBlock *TBB,
400 MachineBasicBlock *FBB,
401 ArrayRef<MachineOperand> Cond,
403 int *BytesAdded) const {
404 if (BytesAdded) *BytesAdded = 0;
406 // Shouldn't be a fall through.
407 assert(TBB && "insertBranch must not be told to insert a fallthrough");
408 assert((Cond.size() == 1 || Cond.size() == 0) &&
409 "AVR branch conditions have one component!");
412 assert(!FBB && "Unconditional branch with multiple successors!");
413 auto &MI = *BuildMI(&MBB, DL, get(AVR::RJMPk)).addMBB(TBB);
415 *BytesAdded += getInstSizeInBytes(MI);
419 // Conditional branch.
421 AVRCC::CondCodes CC = (AVRCC::CondCodes)Cond[0].getImm();
422 auto &CondMI = *BuildMI(&MBB, DL, getBrCond(CC)).addMBB(TBB);
424 if (BytesAdded) *BytesAdded += getInstSizeInBytes(CondMI);
428 // Two-way Conditional branch. Insert the second branch.
429 auto &MI = *BuildMI(&MBB, DL, get(AVR::RJMPk)).addMBB(FBB);
430 if (BytesAdded) *BytesAdded += getInstSizeInBytes(MI);
437 unsigned AVRInstrInfo::removeBranch(MachineBasicBlock &MBB,
438 int *BytesRemoved) const {
439 if (BytesRemoved) *BytesRemoved = 0;
441 MachineBasicBlock::iterator I = MBB.end();
444 while (I != MBB.begin()) {
446 if (I->isDebugInstr()) {
449 //:TODO: add here the missing jmp instructions once they are implemented
450 // like jmp, {e}ijmp, and other cond branches, ...
451 if (I->getOpcode() != AVR::RJMPk &&
452 getCondFromBranchOpc(I->getOpcode()) == AVRCC::COND_INVALID) {
456 // Remove the branch.
457 if (BytesRemoved) *BytesRemoved += getInstSizeInBytes(*I);
458 I->eraseFromParent();
466 bool AVRInstrInfo::reverseBranchCondition(
467 SmallVectorImpl<MachineOperand> &Cond) const {
468 assert(Cond.size() == 1 && "Invalid AVR branch condition!");
470 AVRCC::CondCodes CC = static_cast<AVRCC::CondCodes>(Cond[0].getImm());
471 Cond[0].setImm(getOppositeCondition(CC));
476 unsigned AVRInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const {
477 unsigned Opcode = MI.getOpcode();
480 // A regular instruction
482 const MCInstrDesc &Desc = get(Opcode);
483 return Desc.getSize();
485 case TargetOpcode::EH_LABEL:
486 case TargetOpcode::IMPLICIT_DEF:
487 case TargetOpcode::KILL:
488 case TargetOpcode::DBG_VALUE:
490 case TargetOpcode::INLINEASM:
491 case TargetOpcode::INLINEASM_BR: {
492 const MachineFunction &MF = *MI.getParent()->getParent();
493 const AVRTargetMachine &TM = static_cast<const AVRTargetMachine&>(MF.getTarget());
494 const AVRSubtarget &STI = MF.getSubtarget<AVRSubtarget>();
495 const TargetInstrInfo &TII = *STI.getInstrInfo();
497 return TII.getInlineAsmLength(MI.getOperand(0).getSymbolName(),
504 AVRInstrInfo::getBranchDestBlock(const MachineInstr &MI) const {
505 switch (MI.getOpcode()) {
507 llvm_unreachable("unexpected opcode!");
520 return MI.getOperand(0).getMBB();
523 return MI.getOperand(1).getMBB();
528 llvm_unreachable("unimplemented branch instructions");
532 bool AVRInstrInfo::isBranchOffsetInRange(unsigned BranchOp,
533 int64_t BrOffset) const {
537 llvm_unreachable("unexpected opcode!");
543 return isIntN(13, BrOffset);
554 return isIntN(7, BrOffset);
558 unsigned AVRInstrInfo::insertIndirectBranch(MachineBasicBlock &MBB,
559 MachineBasicBlock &NewDestBB,
562 RegScavenger *RS) const {
563 // This method inserts a *direct* branch (JMP), despite its name.
564 // LLVM calls this method to fixup unconditional branches; it never calls
565 // insertBranch or some hypothetical "insertDirectBranch".
566 // See lib/CodeGen/RegisterRelaxation.cpp for details.
567 // We end up here when a jump is too long for a RJMP instruction.
568 auto &MI = *BuildMI(&MBB, DL, get(AVR::JMPk)).addMBB(&NewDestBB);
570 return getInstSizeInBytes(MI);
573 } // end of namespace llvm