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"
24 #include "llvm/Support/ErrorHandling.h"
29 // XCore Condition Codes
40 XCoreInstrInfo::XCoreInstrInfo()
41 : TargetInstrInfoImpl(XCoreInsts, array_lengthof(XCoreInsts)),
45 static bool isZeroImm(const MachineOperand &op) {
46 return op.isImm() && op.getImm() == 0;
49 /// Return true if the instruction is a register to register move and
50 /// leave the source and dest operands in the passed parameters.
52 bool XCoreInstrInfo::isMoveInstr(const MachineInstr &MI,
53 unsigned &SrcReg, unsigned &DstReg,
54 unsigned &SrcSR, unsigned &DstSR) const {
55 SrcSR = DstSR = 0; // No sub-registers.
57 // We look for 4 kinds of patterns here:
62 if ((MI.getOpcode() == XCore::ADD_2rus || MI.getOpcode() == XCore::SUB_2rus)
63 && isZeroImm(MI.getOperand(2))) {
64 DstReg = MI.getOperand(0).getReg();
65 SrcReg = MI.getOperand(1).getReg();
67 } else if ((MI.getOpcode() == XCore::OR_3r || MI.getOpcode() == XCore::AND_3r)
68 && MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) {
69 DstReg = MI.getOperand(0).getReg();
70 SrcReg = MI.getOperand(1).getReg();
76 /// isLoadFromStackSlot - If the specified machine instruction is a direct
77 /// load from a stack slot, return the virtual or physical register number of
78 /// the destination along with the FrameIndex of the loaded stack slot. If
79 /// not, return 0. This predicate must return 0 if the instruction has
80 /// any side effects other than loading from the stack slot.
82 XCoreInstrInfo::isLoadFromStackSlot(const MachineInstr *MI, int &FrameIndex) const{
83 int Opcode = MI->getOpcode();
84 if (Opcode == XCore::LDWFI)
86 if ((MI->getOperand(1).isFI()) && // is a stack slot
87 (MI->getOperand(2).isImm()) && // the imm is zero
88 (isZeroImm(MI->getOperand(2))))
90 FrameIndex = MI->getOperand(1).getIndex();
91 return MI->getOperand(0).getReg();
97 /// isStoreToStackSlot - If the specified machine instruction is a direct
98 /// store to a stack slot, return the virtual or physical register number of
99 /// the source reg along with the FrameIndex of the loaded stack slot. If
100 /// not, return 0. This predicate must return 0 if the instruction has
101 /// any side effects other than storing to the stack slot.
103 XCoreInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
104 int &FrameIndex) const {
105 int Opcode = MI->getOpcode();
106 if (Opcode == XCore::STWFI)
108 if ((MI->getOperand(1).isFI()) && // is a stack slot
109 (MI->getOperand(2).isImm()) && // the imm is zero
110 (isZeroImm(MI->getOperand(2))))
112 FrameIndex = MI->getOperand(1).getIndex();
113 return MI->getOperand(0).getReg();
119 //===----------------------------------------------------------------------===//
121 //===----------------------------------------------------------------------===//
123 static inline bool IsBRU(unsigned BrOpc) {
124 return BrOpc == XCore::BRFU_u6
125 || BrOpc == XCore::BRFU_lu6
126 || BrOpc == XCore::BRBU_u6
127 || BrOpc == XCore::BRBU_lu6;
130 static inline bool IsBRT(unsigned BrOpc) {
131 return BrOpc == XCore::BRFT_ru6
132 || BrOpc == XCore::BRFT_lru6
133 || BrOpc == XCore::BRBT_ru6
134 || BrOpc == XCore::BRBT_lru6;
137 static inline bool IsBRF(unsigned BrOpc) {
138 return BrOpc == XCore::BRFF_ru6
139 || BrOpc == XCore::BRFF_lru6
140 || BrOpc == XCore::BRBF_ru6
141 || BrOpc == XCore::BRBF_lru6;
144 static inline bool IsCondBranch(unsigned BrOpc) {
145 return IsBRF(BrOpc) || IsBRT(BrOpc);
148 static inline bool IsBR_JT(unsigned BrOpc) {
149 return BrOpc == XCore::BR_JT
150 || BrOpc == XCore::BR_JT32;
153 /// GetCondFromBranchOpc - Return the XCore CC that matches
154 /// the correspondent Branch instruction opcode.
155 static XCore::CondCode GetCondFromBranchOpc(unsigned BrOpc)
158 return XCore::COND_TRUE;
159 } else if (IsBRF(BrOpc)) {
160 return XCore::COND_FALSE;
162 return XCore::COND_INVALID;
166 /// GetCondBranchFromCond - Return the Branch instruction
167 /// opcode that matches the cc.
168 static inline unsigned GetCondBranchFromCond(XCore::CondCode CC)
171 default: llvm_unreachable("Illegal condition code!");
172 case XCore::COND_TRUE : return XCore::BRFT_lru6;
173 case XCore::COND_FALSE : return XCore::BRFF_lru6;
177 /// GetOppositeBranchCondition - Return the inverse of the specified
178 /// condition, e.g. turning COND_E to COND_NE.
179 static inline XCore::CondCode GetOppositeBranchCondition(XCore::CondCode CC)
182 default: llvm_unreachable("Illegal condition code!");
183 case XCore::COND_TRUE : return XCore::COND_FALSE;
184 case XCore::COND_FALSE : return XCore::COND_TRUE;
188 /// AnalyzeBranch - Analyze the branching code at the end of MBB, returning
189 /// true if it cannot be understood (e.g. it's a switch dispatch or isn't
190 /// implemented for a target). Upon success, this returns false and returns
191 /// with the following information in various cases:
193 /// 1. If this block ends with no branches (it just falls through to its succ)
194 /// just return false, leaving TBB/FBB null.
195 /// 2. If this block ends with only an unconditional branch, it sets TBB to be
196 /// the destination block.
197 /// 3. If this block ends with an conditional branch and it falls through to
198 /// an successor block, it sets TBB to be the branch destination block and a
199 /// list of operands that evaluate the condition. These
200 /// operands can be passed to other TargetInstrInfo methods to create new
202 /// 4. If this block ends with an conditional branch and an unconditional
203 /// block, it returns the 'true' destination in TBB, the 'false' destination
204 /// in FBB, and a list of operands that evaluate the condition. These
205 /// operands can be passed to other TargetInstrInfo methods to create new
208 /// Note that RemoveBranch and InsertBranch must be implemented to support
209 /// cases where this method returns success.
212 XCoreInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
213 MachineBasicBlock *&FBB,
214 SmallVectorImpl<MachineOperand> &Cond,
215 bool AllowModify) const {
216 // If the block has no terminators, it just falls into the block after it.
217 MachineBasicBlock::iterator I = MBB.end();
218 if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
221 // Get the last instruction in the block.
222 MachineInstr *LastInst = I;
224 // If there is only one terminator instruction, process it.
225 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
226 if (IsBRU(LastInst->getOpcode())) {
227 TBB = LastInst->getOperand(0).getMBB();
231 XCore::CondCode BranchCode = GetCondFromBranchOpc(LastInst->getOpcode());
232 if (BranchCode == XCore::COND_INVALID)
233 return true; // Can't handle indirect branch.
235 // Conditional branch
236 // Block ends with fall-through condbranch.
238 TBB = LastInst->getOperand(1).getMBB();
239 Cond.push_back(MachineOperand::CreateImm(BranchCode));
240 Cond.push_back(LastInst->getOperand(0));
244 // Get the instruction before it if it's a terminator.
245 MachineInstr *SecondLastInst = I;
247 // If there are three terminators, we don't know what sort of block this is.
248 if (SecondLastInst && I != MBB.begin() &&
249 isUnpredicatedTerminator(--I))
252 unsigned SecondLastOpc = SecondLastInst->getOpcode();
253 XCore::CondCode BranchCode = GetCondFromBranchOpc(SecondLastOpc);
255 // If the block ends with conditional branch followed by unconditional,
257 if (BranchCode != XCore::COND_INVALID
258 && IsBRU(LastInst->getOpcode())) {
260 TBB = SecondLastInst->getOperand(1).getMBB();
261 Cond.push_back(MachineOperand::CreateImm(BranchCode));
262 Cond.push_back(SecondLastInst->getOperand(0));
264 FBB = LastInst->getOperand(0).getMBB();
268 // If the block ends with two unconditional branches, handle it. The second
269 // one is not executed, so remove it.
270 if (IsBRU(SecondLastInst->getOpcode()) &&
271 IsBRU(LastInst->getOpcode())) {
272 TBB = SecondLastInst->getOperand(0).getMBB();
275 I->eraseFromParent();
279 // Likewise if it ends with a branch table followed by an unconditional branch.
280 if (IsBR_JT(SecondLastInst->getOpcode()) && IsBRU(LastInst->getOpcode())) {
283 I->eraseFromParent();
287 // Otherwise, can't handle this.
292 XCoreInstrInfo::InsertBranch(MachineBasicBlock &MBB,MachineBasicBlock *TBB,
293 MachineBasicBlock *FBB,
294 const SmallVectorImpl<MachineOperand> &Cond)const{
295 // FIXME there should probably be a DebugLoc argument here
296 DebugLoc dl = DebugLoc::getUnknownLoc();
297 // Shouldn't be a fall through.
298 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
299 assert((Cond.size() == 2 || Cond.size() == 0) &&
300 "Unexpected number of components!");
302 if (FBB == 0) { // One way branch.
304 // Unconditional branch
305 BuildMI(&MBB, dl, get(XCore::BRFU_lu6)).addMBB(TBB);
307 // Conditional branch.
308 unsigned Opc = GetCondBranchFromCond((XCore::CondCode)Cond[0].getImm());
309 BuildMI(&MBB, dl, get(Opc)).addReg(Cond[1].getReg())
315 // Two-way Conditional branch.
316 assert(Cond.size() == 2 && "Unexpected number of components!");
317 unsigned Opc = GetCondBranchFromCond((XCore::CondCode)Cond[0].getImm());
318 BuildMI(&MBB, dl, get(Opc)).addReg(Cond[1].getReg())
320 BuildMI(&MBB, dl, get(XCore::BRFU_lu6)).addMBB(FBB);
325 XCoreInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
326 MachineBasicBlock::iterator I = MBB.end();
327 if (I == MBB.begin()) return 0;
329 if (!IsBRU(I->getOpcode()) && !IsCondBranch(I->getOpcode()))
332 // Remove the branch.
333 I->eraseFromParent();
337 if (I == MBB.begin()) return 1;
339 if (!IsCondBranch(I->getOpcode()))
342 // Remove the branch.
343 I->eraseFromParent();
347 bool XCoreInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
348 MachineBasicBlock::iterator I,
349 unsigned DestReg, unsigned SrcReg,
350 const TargetRegisterClass *DestRC,
351 const TargetRegisterClass *SrcRC) const {
352 DebugLoc DL = DebugLoc::getUnknownLoc();
353 if (I != MBB.end()) DL = I->getDebugLoc();
355 if (DestRC == SrcRC) {
356 if (DestRC == XCore::GRRegsRegisterClass) {
357 BuildMI(MBB, I, DL, get(XCore::ADD_2rus), DestReg)
366 if (SrcRC == XCore::RRegsRegisterClass && SrcReg == XCore::SP &&
367 DestRC == XCore::GRRegsRegisterClass) {
368 BuildMI(MBB, I, DL, get(XCore::LDAWSP_ru6), DestReg)
372 if (DestRC == XCore::RRegsRegisterClass && DestReg == XCore::SP &&
373 SrcRC == XCore::GRRegsRegisterClass) {
374 BuildMI(MBB, I, DL, get(XCore::SETSP_1r))
381 void XCoreInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
382 MachineBasicBlock::iterator I,
383 unsigned SrcReg, bool isKill,
385 const TargetRegisterClass *RC) const
387 DebugLoc DL = DebugLoc::getUnknownLoc();
388 if (I != MBB.end()) DL = I->getDebugLoc();
389 BuildMI(MBB, I, DL, get(XCore::STWFI))
390 .addReg(SrcReg, getKillRegState(isKill))
391 .addFrameIndex(FrameIndex)
395 void XCoreInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
396 MachineBasicBlock::iterator I,
397 unsigned DestReg, int FrameIndex,
398 const TargetRegisterClass *RC) const
400 DebugLoc DL = DebugLoc::getUnknownLoc();
401 if (I != MBB.end()) DL = I->getDebugLoc();
402 BuildMI(MBB, I, DL, get(XCore::LDWFI), DestReg)
403 .addFrameIndex(FrameIndex)
407 bool XCoreInstrInfo::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
408 MachineBasicBlock::iterator MI,
409 const std::vector<CalleeSavedInfo> &CSI) const
414 MachineFunction *MF = MBB.getParent();
415 const MachineFrameInfo *MFI = MF->getFrameInfo();
416 MachineModuleInfo *MMI = MFI->getMachineModuleInfo();
417 XCoreFunctionInfo *XFI = MF->getInfo<XCoreFunctionInfo>();
419 bool emitFrameMoves = XCoreRegisterInfo::needsFrameMoves(*MF);
421 DebugLoc DL = DebugLoc::getUnknownLoc();
422 if (MI != MBB.end()) DL = MI->getDebugLoc();
424 for (std::vector<CalleeSavedInfo>::const_iterator it = CSI.begin();
425 it != CSI.end(); ++it) {
426 // Add the callee-saved register as live-in. It's killed at the spill.
427 MBB.addLiveIn(it->getReg());
429 storeRegToStackSlot(MBB, MI, it->getReg(), true,
430 it->getFrameIdx(), it->getRegClass());
431 if (emitFrameMoves) {
432 unsigned SaveLabelId = MMI->NextLabelID();
433 BuildMI(MBB, MI, DL, get(XCore::DBG_LABEL)).addImm(SaveLabelId);
434 XFI->getSpillLabels().push_back(
435 std::pair<unsigned, CalleeSavedInfo>(SaveLabelId, *it));
441 bool XCoreInstrInfo::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
442 MachineBasicBlock::iterator MI,
443 const std::vector<CalleeSavedInfo> &CSI) const
445 bool AtStart = MI == MBB.begin();
446 MachineBasicBlock::iterator BeforeI = MI;
449 for (std::vector<CalleeSavedInfo>::const_iterator it = CSI.begin();
450 it != CSI.end(); ++it) {
452 loadRegFromStackSlot(MBB, MI, it->getReg(),
455 assert(MI != MBB.begin() &&
456 "loadRegFromStackSlot didn't insert any code!");
457 // Insert in reverse order. loadRegFromStackSlot can insert multiple
469 /// ReverseBranchCondition - Return the inverse opcode of the
470 /// specified Branch instruction.
471 bool XCoreInstrInfo::
472 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const
474 assert((Cond.size() == 2) &&
475 "Invalid XCore branch condition!");
476 Cond[0].setImm(GetOppositeBranchCondition((XCore::CondCode)Cond[0].getImm()));