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/MC/TargetRegistry.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/ErrorHandling.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(AVRSubtarget
&STI
)
39 : AVRGenInstrInfo(AVR::ADJCALLSTACKDOWN
, AVR::ADJCALLSTACKUP
), RI(),
42 void AVRInstrInfo::copyPhysReg(MachineBasicBlock
&MBB
,
43 MachineBasicBlock::iterator MI
,
44 const DebugLoc
&DL
, MCRegister DestReg
,
45 MCRegister SrcReg
, bool KillSrc
) const {
46 const AVRRegisterInfo
&TRI
= *STI
.getRegisterInfo();
49 if (AVR::DREGSRegClass
.contains(DestReg
, SrcReg
)) {
50 // If our AVR has `movw`, let's emit that; otherwise let's emit two separate
52 if (STI
.hasMOVW() && AVR::DREGSMOVWRegClass
.contains(DestReg
, SrcReg
)) {
53 BuildMI(MBB
, MI
, DL
, get(AVR::MOVWRdRr
), DestReg
)
54 .addReg(SrcReg
, getKillRegState(KillSrc
));
56 Register DestLo
, DestHi
, SrcLo
, SrcHi
;
58 TRI
.splitReg(DestReg
, DestLo
, DestHi
);
59 TRI
.splitReg(SrcReg
, SrcLo
, SrcHi
);
62 // The original instruction was for a register pair, of which only one
63 // register might have been live. Add 'undef' to satisfy the machine
64 // verifier, when subreg liveness is enabled.
65 // TODO: Eliminate these unnecessary copies.
66 if (DestLo
== SrcHi
) {
67 BuildMI(MBB
, MI
, DL
, get(AVR::MOVRdRr
), DestHi
)
68 .addReg(SrcHi
, getKillRegState(KillSrc
) | RegState::Undef
);
69 BuildMI(MBB
, MI
, DL
, get(AVR::MOVRdRr
), DestLo
)
70 .addReg(SrcLo
, getKillRegState(KillSrc
) | RegState::Undef
);
72 BuildMI(MBB
, MI
, DL
, get(AVR::MOVRdRr
), DestLo
)
73 .addReg(SrcLo
, getKillRegState(KillSrc
) | RegState::Undef
);
74 BuildMI(MBB
, MI
, DL
, get(AVR::MOVRdRr
), DestHi
)
75 .addReg(SrcHi
, getKillRegState(KillSrc
) | RegState::Undef
);
79 if (AVR::GPR8RegClass
.contains(DestReg
, SrcReg
)) {
81 } else if (SrcReg
== AVR::SP
&& AVR::DREGSRegClass
.contains(DestReg
)) {
83 } else if (DestReg
== AVR::SP
&& AVR::DREGSRegClass
.contains(SrcReg
)) {
86 llvm_unreachable("Impossible reg-to-reg copy");
89 BuildMI(MBB
, MI
, DL
, get(Opc
), DestReg
)
90 .addReg(SrcReg
, getKillRegState(KillSrc
));
94 Register
AVRInstrInfo::isLoadFromStackSlot(const MachineInstr
&MI
,
95 int &FrameIndex
) const {
96 switch (MI
.getOpcode()) {
98 case AVR::LDDWRdYQ
: { //: FIXME: remove this once PR13375 gets fixed
99 if (MI
.getOperand(1).isFI() && MI
.getOperand(2).isImm() &&
100 MI
.getOperand(2).getImm() == 0) {
101 FrameIndex
= MI
.getOperand(1).getIndex();
102 return MI
.getOperand(0).getReg();
113 Register
AVRInstrInfo::isStoreToStackSlot(const MachineInstr
&MI
,
114 int &FrameIndex
) const {
115 switch (MI
.getOpcode()) {
117 case AVR::STDWPtrQRr
: {
118 if (MI
.getOperand(0).isFI() && MI
.getOperand(1).isImm() &&
119 MI
.getOperand(1).getImm() == 0) {
120 FrameIndex
= MI
.getOperand(0).getIndex();
121 return MI
.getOperand(2).getReg();
132 void AVRInstrInfo::storeRegToStackSlot(
133 MachineBasicBlock
&MBB
, MachineBasicBlock::iterator MI
, Register SrcReg
,
134 bool isKill
, int FrameIndex
, const TargetRegisterClass
*RC
,
135 const TargetRegisterInfo
*TRI
, Register VReg
) const {
136 MachineFunction
&MF
= *MBB
.getParent();
137 AVRMachineFunctionInfo
*AFI
= MF
.getInfo
<AVRMachineFunctionInfo
>();
139 AFI
->setHasSpills(true);
141 const MachineFrameInfo
&MFI
= MF
.getFrameInfo();
143 MachineMemOperand
*MMO
= MF
.getMachineMemOperand(
144 MachinePointerInfo::getFixedStack(MF
, FrameIndex
),
145 MachineMemOperand::MOStore
, MFI
.getObjectSize(FrameIndex
),
146 MFI
.getObjectAlign(FrameIndex
));
149 if (TRI
->isTypeLegalForClass(*RC
, MVT::i8
)) {
150 Opcode
= AVR::STDPtrQRr
;
151 } else if (TRI
->isTypeLegalForClass(*RC
, MVT::i16
)) {
152 Opcode
= AVR::STDWPtrQRr
;
154 llvm_unreachable("Cannot store this register into a stack slot!");
157 BuildMI(MBB
, MI
, DebugLoc(), get(Opcode
))
158 .addFrameIndex(FrameIndex
)
160 .addReg(SrcReg
, getKillRegState(isKill
))
164 void AVRInstrInfo::loadRegFromStackSlot(MachineBasicBlock
&MBB
,
165 MachineBasicBlock::iterator MI
,
166 Register DestReg
, int FrameIndex
,
167 const TargetRegisterClass
*RC
,
168 const TargetRegisterInfo
*TRI
,
169 Register VReg
) const {
170 MachineFunction
&MF
= *MBB
.getParent();
171 const MachineFrameInfo
&MFI
= MF
.getFrameInfo();
173 MachineMemOperand
*MMO
= MF
.getMachineMemOperand(
174 MachinePointerInfo::getFixedStack(MF
, FrameIndex
),
175 MachineMemOperand::MOLoad
, MFI
.getObjectSize(FrameIndex
),
176 MFI
.getObjectAlign(FrameIndex
));
179 if (TRI
->isTypeLegalForClass(*RC
, MVT::i8
)) {
180 Opcode
= AVR::LDDRdPtrQ
;
181 } else if (TRI
->isTypeLegalForClass(*RC
, MVT::i16
)) {
182 // Opcode = AVR::LDDWRdPtrQ;
183 //: FIXME: remove this once PR13375 gets fixed
184 Opcode
= AVR::LDDWRdYQ
;
186 llvm_unreachable("Cannot load this register from a stack slot!");
189 BuildMI(MBB
, MI
, DebugLoc(), get(Opcode
), DestReg
)
190 .addFrameIndex(FrameIndex
)
195 const MCInstrDesc
&AVRInstrInfo::getBrCond(AVRCC::CondCodes CC
) const {
198 llvm_unreachable("Unknown condition code!");
200 return get(AVR::BREQk
);
202 return get(AVR::BRNEk
);
204 return get(AVR::BRGEk
);
206 return get(AVR::BRLTk
);
208 return get(AVR::BRSHk
);
210 return get(AVR::BRLOk
);
212 return get(AVR::BRMIk
);
214 return get(AVR::BRPLk
);
218 AVRCC::CondCodes
AVRInstrInfo::getCondFromBranchOpc(unsigned Opc
) const {
221 return AVRCC::COND_INVALID
;
223 return AVRCC::COND_EQ
;
225 return AVRCC::COND_NE
;
227 return AVRCC::COND_SH
;
229 return AVRCC::COND_LO
;
231 return AVRCC::COND_MI
;
233 return AVRCC::COND_PL
;
235 return AVRCC::COND_GE
;
237 return AVRCC::COND_LT
;
241 AVRCC::CondCodes
AVRInstrInfo::getOppositeCondition(AVRCC::CondCodes CC
) const {
244 llvm_unreachable("Invalid condition!");
246 return AVRCC::COND_NE
;
248 return AVRCC::COND_EQ
;
250 return AVRCC::COND_LO
;
252 return AVRCC::COND_SH
;
254 return AVRCC::COND_LT
;
256 return AVRCC::COND_GE
;
258 return AVRCC::COND_PL
;
260 return AVRCC::COND_MI
;
264 bool AVRInstrInfo::analyzeBranch(MachineBasicBlock
&MBB
,
265 MachineBasicBlock
*&TBB
,
266 MachineBasicBlock
*&FBB
,
267 SmallVectorImpl
<MachineOperand
> &Cond
,
268 bool AllowModify
) const {
269 // Start from the bottom of the block and work up, examining the
270 // terminator instructions.
271 MachineBasicBlock::iterator I
= MBB
.end();
272 MachineBasicBlock::iterator UnCondBrIter
= MBB
.end();
274 while (I
!= MBB
.begin()) {
276 if (I
->isDebugInstr()) {
280 // Working from the bottom, when we see a non-terminator
281 // instruction, we're done.
282 if (!isUnpredicatedTerminator(*I
)) {
286 // A terminator that isn't a branch can't easily be handled
288 if (!I
->getDesc().isBranch()) {
292 // Handle unconditional branches.
293 //: TODO: add here jmp
294 if (I
->getOpcode() == AVR::RJMPk
) {
298 TBB
= I
->getOperand(0).getMBB();
302 // If the block has any instructions after a JMP, delete them.
303 MBB
.erase(std::next(I
), MBB
.end());
308 // Delete the JMP if it's equivalent to a fall-through.
309 if (MBB
.isLayoutSuccessor(I
->getOperand(0).getMBB())) {
311 I
->eraseFromParent();
313 UnCondBrIter
= MBB
.end();
317 // TBB is used to indicate the unconditinal destination.
318 TBB
= I
->getOperand(0).getMBB();
322 // Handle conditional branches.
323 AVRCC::CondCodes BranchCode
= getCondFromBranchOpc(I
->getOpcode());
324 if (BranchCode
== AVRCC::COND_INVALID
) {
325 return true; // Can't handle indirect branch.
328 // Working from the bottom, handle the first conditional branch.
330 MachineBasicBlock
*TargetBB
= I
->getOperand(0).getMBB();
331 if (AllowModify
&& UnCondBrIter
!= MBB
.end() &&
332 MBB
.isLayoutSuccessor(TargetBB
)) {
333 // If we can modify the code and it ends in something like:
341 // Then we can change this to:
348 // Which is a bit more efficient.
349 // We conditionally jump to the fall-through block.
350 BranchCode
= getOppositeCondition(BranchCode
);
351 unsigned JNCC
= getBrCond(BranchCode
).getOpcode();
352 MachineBasicBlock::iterator OldInst
= I
;
354 BuildMI(MBB
, UnCondBrIter
, MBB
.findDebugLoc(I
), get(JNCC
))
355 .addMBB(UnCondBrIter
->getOperand(0).getMBB());
356 BuildMI(MBB
, UnCondBrIter
, MBB
.findDebugLoc(I
), get(AVR::RJMPk
))
359 OldInst
->eraseFromParent();
360 UnCondBrIter
->eraseFromParent();
362 // Restart the analysis.
363 UnCondBrIter
= MBB
.end();
369 TBB
= I
->getOperand(0).getMBB();
370 Cond
.push_back(MachineOperand::CreateImm(BranchCode
));
374 // Handle subsequent conditional branches. Only handle the case where all
375 // conditional branches branch to the same destination.
376 assert(Cond
.size() == 1);
379 // Only handle the case where all conditional branches branch to
380 // the same destination.
381 if (TBB
!= I
->getOperand(0).getMBB()) {
385 AVRCC::CondCodes OldBranchCode
= (AVRCC::CondCodes
)Cond
[0].getImm();
386 // If the conditions are the same, we can leave them alone.
387 if (OldBranchCode
== BranchCode
) {
397 unsigned AVRInstrInfo::insertBranch(MachineBasicBlock
&MBB
,
398 MachineBasicBlock
*TBB
,
399 MachineBasicBlock
*FBB
,
400 ArrayRef
<MachineOperand
> Cond
,
401 const DebugLoc
&DL
, int *BytesAdded
) const {
405 // Shouldn't be a fall through.
406 assert(TBB
&& "insertBranch must not be told to insert a fallthrough");
407 assert((Cond
.size() == 1 || Cond
.size() == 0) &&
408 "AVR branch conditions have one component!");
411 assert(!FBB
&& "Unconditional branch with multiple successors!");
412 auto &MI
= *BuildMI(&MBB
, DL
, get(AVR::RJMPk
)).addMBB(TBB
);
414 *BytesAdded
+= getInstSizeInBytes(MI
);
418 // Conditional branch.
420 AVRCC::CondCodes CC
= (AVRCC::CondCodes
)Cond
[0].getImm();
421 auto &CondMI
= *BuildMI(&MBB
, DL
, getBrCond(CC
)).addMBB(TBB
);
424 *BytesAdded
+= getInstSizeInBytes(CondMI
);
428 // Two-way Conditional branch. Insert the second branch.
429 auto &MI
= *BuildMI(&MBB
, DL
, get(AVR::RJMPk
)).addMBB(FBB
);
431 *BytesAdded
+= getInstSizeInBytes(MI
);
438 unsigned AVRInstrInfo::removeBranch(MachineBasicBlock
&MBB
,
439 int *BytesRemoved
) const {
443 MachineBasicBlock::iterator I
= MBB
.end();
446 while (I
!= MBB
.begin()) {
448 if (I
->isDebugInstr()) {
451 //: TODO: add here the missing jmp instructions once they are implemented
452 // like jmp, {e}ijmp, and other cond branches, ...
453 if (I
->getOpcode() != AVR::RJMPk
&&
454 getCondFromBranchOpc(I
->getOpcode()) == AVRCC::COND_INVALID
) {
458 // Remove the branch.
460 *BytesRemoved
+= getInstSizeInBytes(*I
);
461 I
->eraseFromParent();
469 bool AVRInstrInfo::reverseBranchCondition(
470 SmallVectorImpl
<MachineOperand
> &Cond
) const {
471 assert(Cond
.size() == 1 && "Invalid AVR branch condition!");
473 AVRCC::CondCodes CC
= static_cast<AVRCC::CondCodes
>(Cond
[0].getImm());
474 Cond
[0].setImm(getOppositeCondition(CC
));
479 unsigned AVRInstrInfo::getInstSizeInBytes(const MachineInstr
&MI
) const {
480 unsigned Opcode
= MI
.getOpcode();
483 // A regular instruction
485 const MCInstrDesc
&Desc
= get(Opcode
);
486 return Desc
.getSize();
488 case TargetOpcode::EH_LABEL
:
489 case TargetOpcode::IMPLICIT_DEF
:
490 case TargetOpcode::KILL
:
491 case TargetOpcode::DBG_VALUE
:
493 case TargetOpcode::INLINEASM
:
494 case TargetOpcode::INLINEASM_BR
: {
495 const MachineFunction
&MF
= *MI
.getParent()->getParent();
496 const AVRTargetMachine
&TM
=
497 static_cast<const AVRTargetMachine
&>(MF
.getTarget());
498 const TargetInstrInfo
&TII
= *STI
.getInstrInfo();
499 return TII
.getInlineAsmLength(MI
.getOperand(0).getSymbolName(),
506 AVRInstrInfo::getBranchDestBlock(const MachineInstr
&MI
) const {
507 switch (MI
.getOpcode()) {
509 llvm_unreachable("unexpected opcode!");
522 return MI
.getOperand(0).getMBB();
525 return MI
.getOperand(1).getMBB();
530 llvm_unreachable("unimplemented branch instructions");
534 bool AVRInstrInfo::isBranchOffsetInRange(unsigned BranchOp
,
535 int64_t BrOffset
) const {
539 llvm_unreachable("unexpected opcode!");
542 return STI
.hasJMPCALL();
545 return isIntN(13, BrOffset
);
556 return isIntN(7, BrOffset
);
560 void AVRInstrInfo::insertIndirectBranch(MachineBasicBlock
&MBB
,
561 MachineBasicBlock
&NewDestBB
,
562 MachineBasicBlock
&RestoreBB
,
563 const DebugLoc
&DL
, int64_t BrOffset
,
564 RegScavenger
*RS
) const {
565 // This method inserts a *direct* branch (JMP), despite its name.
566 // LLVM calls this method to fixup unconditional branches; it never calls
567 // insertBranch or some hypothetical "insertDirectBranch".
568 // See lib/CodeGen/RegisterRelaxation.cpp for details.
569 // We end up here when a jump is too long for a RJMP instruction.
570 if (STI
.hasJMPCALL())
571 BuildMI(&MBB
, DL
, get(AVR::JMPk
)).addMBB(&NewDestBB
);
573 // The RJMP may jump to a far place beyond its legal range. We let the
574 // linker to report 'out of range' rather than crash, or silently emit
575 // incorrect assembly code.
576 BuildMI(&MBB
, DL
, get(AVR::RJMPk
)).addMBB(&NewDestBB
);
579 } // end of namespace llvm