1 //===- XCoreInstrInfo.cpp - XCore Instruction Information -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the XCore implementation of the TargetInstrInfo class.
12 //===----------------------------------------------------------------------===//
14 #include "XCoreMachineFunctionInfo.h"
15 #include "XCoreInstrInfo.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/CodeGen/MachineInstrBuilder.h"
19 #include "llvm/CodeGen/MachineFrameInfo.h"
20 #include "llvm/CodeGen/MachineLocation.h"
21 #include "llvm/CodeGen/MachineModuleInfo.h"
22 #include "XCoreGenInstrInfo.inc"
23 #include "llvm/Support/Debug.h"
28 // XCore Condition Codes
39 XCoreInstrInfo::XCoreInstrInfo(void)
40 : TargetInstrInfoImpl(XCoreInsts, array_lengthof(XCoreInsts)),
44 static bool isZeroImm(const MachineOperand &op) {
45 return op.isImm() && op.getImm() == 0;
48 /// Return true if the instruction is a register to register move and
49 /// leave the source and dest operands in the passed parameters.
51 bool XCoreInstrInfo::isMoveInstr(const MachineInstr &MI,
52 unsigned &SrcReg, unsigned &DstReg) const {
53 // We look for 4 kinds of patterns here:
58 if ((MI.getOpcode() == XCore::ADD_2rus || MI.getOpcode() == XCore::SUB_2rus)
59 && isZeroImm(MI.getOperand(2))) {
60 DstReg = MI.getOperand(0).getReg();
61 SrcReg = MI.getOperand(1).getReg();
63 } else if ((MI.getOpcode() == XCore::OR_3r || MI.getOpcode() == XCore::AND_3r)
64 && MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) {
65 DstReg = MI.getOperand(0).getReg();
66 SrcReg = MI.getOperand(1).getReg();
72 /// isLoadFromStackSlot - If the specified machine instruction is a direct
73 /// load from a stack slot, return the virtual or physical register number of
74 /// the destination along with the FrameIndex of the loaded stack slot. If
75 /// not, return 0. This predicate must return 0 if the instruction has
76 /// any side effects other than loading from the stack slot.
78 XCoreInstrInfo::isLoadFromStackSlot(MachineInstr *MI, int &FrameIndex) const{
79 int Opcode = MI->getOpcode();
80 if (Opcode == XCore::LDWSP_ru6 || Opcode == XCore::LDWSP_lru6)
82 if ((MI->getOperand(1).isFI()) && // is a stack slot
83 (MI->getOperand(2).isImm()) && // the imm is zero
84 (isZeroImm(MI->getOperand(2))))
86 FrameIndex = MI->getOperand(1).getIndex();
87 return MI->getOperand(0).getReg();
93 /// isStoreToStackSlot - If the specified machine instruction is a direct
94 /// store to a stack slot, return the virtual or physical register number of
95 /// the source reg along with the FrameIndex of the loaded stack slot. If
96 /// not, return 0. This predicate must return 0 if the instruction has
97 /// any side effects other than storing to the stack slot.
99 XCoreInstrInfo::isStoreToStackSlot(MachineInstr *MI, int &FrameIndex) const {
100 int Opcode = MI->getOpcode();
101 if (Opcode == XCore::STWSP_ru6 || Opcode == XCore::STWSP_lru6)
103 if ((MI->getOperand(1).isFI()) && // is a stack slot
104 (MI->getOperand(2).isImm()) && // the imm is zero
105 (isZeroImm(MI->getOperand(2))))
107 FrameIndex = MI->getOperand(1).getIndex();
108 return MI->getOperand(0).getReg();
111 else if (Opcode == XCore::STWSP_ru6_2 || Opcode == XCore::STWSP_lru6_2)
113 if (MI->getOperand(1).isFI())
115 FrameIndex = MI->getOperand(1).getIndex();
116 return MI->getOperand(0).getReg();
122 /// isInvariantLoad - Return true if the specified instruction (which is marked
123 /// mayLoad) is loading from a location whose value is invariant across the
124 /// function. For example, loading a value from the constant pool or from
125 /// from the argument area of a function if it does not change. This should
126 /// only return true of *all* loads the instruction does are invariant (if it
127 /// does multiple loads).
129 XCoreInstrInfo::isInvariantLoad(MachineInstr *MI) const {
130 // Loads from constants pools and loads from invariant argument slots are
132 int Opcode = MI->getOpcode();
133 if (Opcode == XCore::LDWCP_ru6 || Opcode == XCore::LDWCP_lru6) {
134 return MI->getOperand(1).isCPI();
137 if (isLoadFromStackSlot(MI, FrameIndex)) {
138 const MachineFrameInfo &MFI =
139 *MI->getParent()->getParent()->getFrameInfo();
140 return MFI.isFixedObjectIndex(FrameIndex) &&
141 MFI.isImmutableObjectIndex(FrameIndex);
146 //===----------------------------------------------------------------------===//
148 //===----------------------------------------------------------------------===//
150 static inline bool IsBRU(unsigned BrOpc) {
151 return BrOpc == XCore::BRFU_u6
152 || BrOpc == XCore::BRFU_lu6
153 || BrOpc == XCore::BRBU_u6
154 || BrOpc == XCore::BRBU_lu6;
157 static inline bool IsBRT(unsigned BrOpc) {
158 return BrOpc == XCore::BRFT_ru6
159 || BrOpc == XCore::BRFT_lru6
160 || BrOpc == XCore::BRBT_ru6
161 || BrOpc == XCore::BRBT_lru6;
164 static inline bool IsBRF(unsigned BrOpc) {
165 return BrOpc == XCore::BRFF_ru6
166 || BrOpc == XCore::BRFF_lru6
167 || BrOpc == XCore::BRBF_ru6
168 || BrOpc == XCore::BRBF_lru6;
171 static inline bool IsCondBranch(unsigned BrOpc) {
172 return IsBRF(BrOpc) || IsBRT(BrOpc);
175 /// GetCondFromBranchOpc - Return the XCore CC that matches
176 /// the correspondent Branch instruction opcode.
177 static XCore::CondCode GetCondFromBranchOpc(unsigned BrOpc)
180 return XCore::COND_TRUE;
181 } else if (IsBRF(BrOpc)) {
182 return XCore::COND_FALSE;
184 return XCore::COND_INVALID;
188 /// GetCondBranchFromCond - Return the Branch instruction
189 /// opcode that matches the cc.
190 static inline unsigned GetCondBranchFromCond(XCore::CondCode CC)
193 default: assert(0 && "Illegal condition code!");
194 case XCore::COND_TRUE : return XCore::BRFT_lru6;
195 case XCore::COND_FALSE : return XCore::BRFF_lru6;
199 /// GetOppositeBranchCondition - Return the inverse of the specified
200 /// condition, e.g. turning COND_E to COND_NE.
201 static inline XCore::CondCode GetOppositeBranchCondition(XCore::CondCode CC)
204 default: assert(0 && "Illegal condition code!");
205 case XCore::COND_TRUE : return XCore::COND_FALSE;
206 case XCore::COND_FALSE : return XCore::COND_TRUE;
210 /// AnalyzeBranch - Analyze the branching code at the end of MBB, returning
211 /// true if it cannot be understood (e.g. it's a switch dispatch or isn't
212 /// implemented for a target). Upon success, this returns false and returns
213 /// with the following information in various cases:
215 /// 1. If this block ends with no branches (it just falls through to its succ)
216 /// just return false, leaving TBB/FBB null.
217 /// 2. If this block ends with only an unconditional branch, it sets TBB to be
218 /// the destination block.
219 /// 3. If this block ends with an conditional branch and it falls through to
220 /// an successor block, it sets TBB to be the branch destination block and a
221 /// list of operands that evaluate the condition. These
222 /// operands can be passed to other TargetInstrInfo methods to create new
224 /// 4. If this block ends with an conditional branch and an unconditional
225 /// block, it returns the 'true' destination in TBB, the 'false' destination
226 /// in FBB, and a list of operands that evaluate the condition. These
227 /// operands can be passed to other TargetInstrInfo methods to create new
230 /// Note that RemoveBranch and InsertBranch must be implemented to support
231 /// cases where this method returns success.
234 XCoreInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
235 MachineBasicBlock *&FBB,
236 SmallVectorImpl<MachineOperand> &Cond) const {
237 // If the block has no terminators, it just falls into the block after it.
238 MachineBasicBlock::iterator I = MBB.end();
239 if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
242 // Get the last instruction in the block.
243 MachineInstr *LastInst = I;
245 // If there is only one terminator instruction, process it.
246 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
247 if (IsBRU(LastInst->getOpcode())) {
248 TBB = LastInst->getOperand(0).getMBB();
252 XCore::CondCode BranchCode = GetCondFromBranchOpc(LastInst->getOpcode());
253 if (BranchCode == XCore::COND_INVALID)
254 return true; // Can't handle indirect branch.
256 // Conditional branch
257 // Block ends with fall-through condbranch.
259 TBB = LastInst->getOperand(1).getMBB();
260 Cond.push_back(MachineOperand::CreateImm(BranchCode));
261 Cond.push_back(LastInst->getOperand(0));
265 // Get the instruction before it if it's a terminator.
266 MachineInstr *SecondLastInst = I;
268 // If there are three terminators, we don't know what sort of block this is.
269 if (SecondLastInst && I != MBB.begin() &&
270 isUnpredicatedTerminator(--I))
273 unsigned SecondLastOpc = SecondLastInst->getOpcode();
274 XCore::CondCode BranchCode = GetCondFromBranchOpc(SecondLastOpc);
276 // If the block ends with conditional branch followed by unconditional,
278 if (BranchCode != XCore::COND_INVALID
279 && IsBRU(LastInst->getOpcode())) {
281 TBB = SecondLastInst->getOperand(1).getMBB();
282 Cond.push_back(MachineOperand::CreateImm(BranchCode));
283 Cond.push_back(SecondLastInst->getOperand(0));
285 FBB = LastInst->getOperand(0).getMBB();
289 // If the block ends with two unconditional branches, handle it. The second
290 // one is not executed, so remove it.
291 if (IsBRU(SecondLastInst->getOpcode()) &&
292 IsBRU(LastInst->getOpcode())) {
293 TBB = SecondLastInst->getOperand(0).getMBB();
295 I->eraseFromParent();
299 // Otherwise, can't handle this.
304 XCoreInstrInfo::InsertBranch(MachineBasicBlock &MBB,MachineBasicBlock *TBB,
305 MachineBasicBlock *FBB,
306 const SmallVectorImpl<MachineOperand> &Cond)const{
307 // Shouldn't be a fall through.
308 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
309 assert((Cond.size() == 2 || Cond.size() == 0) &&
310 "Unexpected number of components!");
312 if (FBB == 0) { // One way branch.
314 // Unconditional branch
315 BuildMI(&MBB, get(XCore::BRFU_lu6)).addMBB(TBB);
317 // Conditional branch.
318 unsigned Opc = GetCondBranchFromCond((XCore::CondCode)Cond[0].getImm());
319 BuildMI(&MBB, get(Opc)).addReg(Cond[1].getReg())
325 // Two-way Conditional branch.
326 assert(Cond.size() == 2 && "Unexpected number of components!");
327 unsigned Opc = GetCondBranchFromCond((XCore::CondCode)Cond[0].getImm());
328 BuildMI(&MBB, get(Opc)).addReg(Cond[1].getReg())
330 BuildMI(&MBB, get(XCore::BRFU_lu6)).addMBB(FBB);
335 XCoreInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
336 MachineBasicBlock::iterator I = MBB.end();
337 if (I == MBB.begin()) return 0;
339 if (!IsBRU(I->getOpcode()) && !IsCondBranch(I->getOpcode()))
342 // Remove the branch.
343 I->eraseFromParent();
347 if (I == MBB.begin()) return 1;
349 if (!IsCondBranch(I->getOpcode()))
352 // Remove the branch.
353 I->eraseFromParent();
357 bool XCoreInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
358 MachineBasicBlock::iterator I,
359 unsigned DestReg, unsigned SrcReg,
360 const TargetRegisterClass *DestRC,
361 const TargetRegisterClass *SrcRC) const {
362 if (DestRC == SrcRC) {
363 if (DestRC == XCore::GRRegsRegisterClass) {
364 BuildMI(MBB, I, get(XCore::ADD_2rus), DestReg).addReg(SrcReg).addImm(0);
371 if (SrcRC == XCore::RRegsRegisterClass && SrcReg == XCore::SP &&
372 DestRC == XCore::GRRegsRegisterClass) {
373 BuildMI(MBB, I, get(XCore::LDAWSP_ru6), DestReg).addImm(0).addImm(0);
376 if (DestRC == XCore::RRegsRegisterClass && DestReg == XCore::SP &&
377 SrcRC == XCore::GRRegsRegisterClass) {
378 BuildMI(MBB, I, get(XCore::SETSP_1r)).addReg(SrcReg);
384 void XCoreInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
385 MachineBasicBlock::iterator I,
386 unsigned SrcReg, bool isKill, int FrameIndex,
387 const TargetRegisterClass *RC) const
389 BuildMI(MBB, I, get(XCore::STWSP_lru6)).addReg(SrcReg, false, false, isKill)
390 .addFrameIndex(FrameIndex).addImm(0);
393 void XCoreInstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
394 bool isKill, SmallVectorImpl<MachineOperand> &Addr,
395 const TargetRegisterClass *RC,
396 SmallVectorImpl<MachineInstr*> &NewMIs) const
398 assert(0 && "unimplemented\n");
401 void XCoreInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
402 MachineBasicBlock::iterator I,
403 unsigned DestReg, int FrameIndex,
404 const TargetRegisterClass *RC) const
406 BuildMI(MBB, I, get(XCore::LDWSP_lru6), DestReg).addFrameIndex(FrameIndex)
410 void XCoreInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
411 SmallVectorImpl<MachineOperand> &Addr,
412 const TargetRegisterClass *RC,
413 SmallVectorImpl<MachineInstr*> &NewMIs) const
415 assert(0 && "unimplemented\n");
418 bool XCoreInstrInfo::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
419 MachineBasicBlock::iterator MI,
420 const std::vector<CalleeSavedInfo> &CSI) const
425 MachineFunction *MF = MBB.getParent();
426 const MachineFrameInfo *MFI = MF->getFrameInfo();
427 MachineModuleInfo *MMI = MFI->getMachineModuleInfo();
428 XCoreFunctionInfo *XFI = MF->getInfo<XCoreFunctionInfo>();
430 bool emitFrameMoves = XCoreRegisterInfo::needsFrameMoves(*MF);
432 for (std::vector<CalleeSavedInfo>::const_iterator it = CSI.begin();
433 it != CSI.end(); ++it) {
434 // Add the callee-saved register as live-in. It's killed at the spill.
435 MBB.addLiveIn(it->getReg());
437 storeRegToStackSlot(MBB, MI, it->getReg(), true,
438 it->getFrameIdx(), it->getRegClass());
439 if (emitFrameMoves) {
440 unsigned SaveLabelId = MMI->NextLabelID();
441 BuildMI(MBB, MI, get(XCore::DBG_LABEL)).addImm(SaveLabelId);
442 XFI->getSpillLabels().push_back(
443 std::pair<unsigned, CalleeSavedInfo>(SaveLabelId, *it));
449 bool XCoreInstrInfo::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
450 MachineBasicBlock::iterator MI,
451 const std::vector<CalleeSavedInfo> &CSI) const
453 bool AtStart = MI == MBB.begin();
454 MachineBasicBlock::iterator BeforeI = MI;
457 for (std::vector<CalleeSavedInfo>::const_iterator it = CSI.begin();
458 it != CSI.end(); ++it) {
460 loadRegFromStackSlot(MBB, MI, it->getReg(),
463 assert(MI != MBB.begin() &&
464 "loadRegFromStackSlot didn't insert any code!");
465 // Insert in reverse order. loadRegFromStackSlot can insert multiple
477 /// BlockHasNoFallThrough - Analyse if MachineBasicBlock does not
478 /// fall-through into its successor block.
479 bool XCoreInstrInfo::
480 BlockHasNoFallThrough(const MachineBasicBlock &MBB) const
482 if (MBB.empty()) return false;
484 switch (MBB.back().getOpcode()) {
485 case XCore::RETSP_u6: // Return.
486 case XCore::RETSP_lu6:
487 case XCore::BAU_1r: // Indirect branch.
488 case XCore::BRFU_u6: // Uncond branch.
489 case XCore::BRFU_lu6:
491 case XCore::BRBU_lu6:
493 default: return false;
497 /// ReverseBranchCondition - Return the inverse opcode of the
498 /// specified Branch instruction.
499 bool XCoreInstrInfo::
500 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const
502 assert((Cond.size() == 2) &&
503 "Invalid XCore branch condition!");
504 Cond[0].setImm(GetOppositeBranchCondition((XCore::CondCode)Cond[0].getImm()));