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(const MachineInstr *MI, int &FrameIndex) const{
79 int Opcode = MI->getOpcode();
80 if (Opcode == XCore::LDWFI)
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(const MachineInstr *MI,
100 int &FrameIndex) const {
101 int Opcode = MI->getOpcode();
102 if (Opcode == XCore::STWFI)
104 if ((MI->getOperand(1).isFI()) && // is a stack slot
105 (MI->getOperand(2).isImm()) && // the imm is zero
106 (isZeroImm(MI->getOperand(2))))
108 FrameIndex = MI->getOperand(1).getIndex();
109 return MI->getOperand(0).getReg();
115 /// isInvariantLoad - Return true if the specified instruction (which is marked
116 /// mayLoad) is loading from a location whose value is invariant across the
117 /// function. For example, loading a value from the constant pool or from
118 /// from the argument area of a function if it does not change. This should
119 /// only return true of *all* loads the instruction does are invariant (if it
120 /// does multiple loads).
122 XCoreInstrInfo::isInvariantLoad(const MachineInstr *MI) const {
123 // Loads from constants pools and loads from invariant argument slots are
125 int Opcode = MI->getOpcode();
126 if (Opcode == XCore::LDWCP_ru6 || Opcode == XCore::LDWCP_lru6) {
127 return MI->getOperand(1).isCPI();
130 if (isLoadFromStackSlot(MI, FrameIndex)) {
131 const MachineFrameInfo &MFI =
132 *MI->getParent()->getParent()->getFrameInfo();
133 return MFI.isFixedObjectIndex(FrameIndex) &&
134 MFI.isImmutableObjectIndex(FrameIndex);
139 //===----------------------------------------------------------------------===//
141 //===----------------------------------------------------------------------===//
143 static inline bool IsBRU(unsigned BrOpc) {
144 return BrOpc == XCore::BRFU_u6
145 || BrOpc == XCore::BRFU_lu6
146 || BrOpc == XCore::BRBU_u6
147 || BrOpc == XCore::BRBU_lu6;
150 static inline bool IsBRT(unsigned BrOpc) {
151 return BrOpc == XCore::BRFT_ru6
152 || BrOpc == XCore::BRFT_lru6
153 || BrOpc == XCore::BRBT_ru6
154 || BrOpc == XCore::BRBT_lru6;
157 static inline bool IsBRF(unsigned BrOpc) {
158 return BrOpc == XCore::BRFF_ru6
159 || BrOpc == XCore::BRFF_lru6
160 || BrOpc == XCore::BRBF_ru6
161 || BrOpc == XCore::BRBF_lru6;
164 static inline bool IsCondBranch(unsigned BrOpc) {
165 return IsBRF(BrOpc) || IsBRT(BrOpc);
168 /// GetCondFromBranchOpc - Return the XCore CC that matches
169 /// the correspondent Branch instruction opcode.
170 static XCore::CondCode GetCondFromBranchOpc(unsigned BrOpc)
173 return XCore::COND_TRUE;
174 } else if (IsBRF(BrOpc)) {
175 return XCore::COND_FALSE;
177 return XCore::COND_INVALID;
181 /// GetCondBranchFromCond - Return the Branch instruction
182 /// opcode that matches the cc.
183 static inline unsigned GetCondBranchFromCond(XCore::CondCode CC)
186 default: assert(0 && "Illegal condition code!");
187 case XCore::COND_TRUE : return XCore::BRFT_lru6;
188 case XCore::COND_FALSE : return XCore::BRFF_lru6;
192 /// GetOppositeBranchCondition - Return the inverse of the specified
193 /// condition, e.g. turning COND_E to COND_NE.
194 static inline XCore::CondCode GetOppositeBranchCondition(XCore::CondCode CC)
197 default: assert(0 && "Illegal condition code!");
198 case XCore::COND_TRUE : return XCore::COND_FALSE;
199 case XCore::COND_FALSE : return XCore::COND_TRUE;
203 /// AnalyzeBranch - Analyze the branching code at the end of MBB, returning
204 /// true if it cannot be understood (e.g. it's a switch dispatch or isn't
205 /// implemented for a target). Upon success, this returns false and returns
206 /// with the following information in various cases:
208 /// 1. If this block ends with no branches (it just falls through to its succ)
209 /// just return false, leaving TBB/FBB null.
210 /// 2. If this block ends with only an unconditional branch, it sets TBB to be
211 /// the destination block.
212 /// 3. If this block ends with an conditional branch and it falls through to
213 /// an successor block, it sets TBB to be the branch destination block and a
214 /// list of operands that evaluate the condition. These
215 /// operands can be passed to other TargetInstrInfo methods to create new
217 /// 4. If this block ends with an conditional branch and an unconditional
218 /// block, it returns the 'true' destination in TBB, the 'false' destination
219 /// in FBB, and a list of operands that evaluate the condition. These
220 /// operands can be passed to other TargetInstrInfo methods to create new
223 /// Note that RemoveBranch and InsertBranch must be implemented to support
224 /// cases where this method returns success.
227 XCoreInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
228 MachineBasicBlock *&FBB,
229 SmallVectorImpl<MachineOperand> &Cond) const {
230 // If the block has no terminators, it just falls into the block after it.
231 MachineBasicBlock::iterator I = MBB.end();
232 if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
235 // Get the last instruction in the block.
236 MachineInstr *LastInst = I;
238 // If there is only one terminator instruction, process it.
239 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
240 if (IsBRU(LastInst->getOpcode())) {
241 TBB = LastInst->getOperand(0).getMBB();
245 XCore::CondCode BranchCode = GetCondFromBranchOpc(LastInst->getOpcode());
246 if (BranchCode == XCore::COND_INVALID)
247 return true; // Can't handle indirect branch.
249 // Conditional branch
250 // Block ends with fall-through condbranch.
252 TBB = LastInst->getOperand(1).getMBB();
253 Cond.push_back(MachineOperand::CreateImm(BranchCode));
254 Cond.push_back(LastInst->getOperand(0));
258 // Get the instruction before it if it's a terminator.
259 MachineInstr *SecondLastInst = I;
261 // If there are three terminators, we don't know what sort of block this is.
262 if (SecondLastInst && I != MBB.begin() &&
263 isUnpredicatedTerminator(--I))
266 unsigned SecondLastOpc = SecondLastInst->getOpcode();
267 XCore::CondCode BranchCode = GetCondFromBranchOpc(SecondLastOpc);
269 // If the block ends with conditional branch followed by unconditional,
271 if (BranchCode != XCore::COND_INVALID
272 && IsBRU(LastInst->getOpcode())) {
274 TBB = SecondLastInst->getOperand(1).getMBB();
275 Cond.push_back(MachineOperand::CreateImm(BranchCode));
276 Cond.push_back(SecondLastInst->getOperand(0));
278 FBB = LastInst->getOperand(0).getMBB();
282 // If the block ends with two unconditional branches, handle it. The second
283 // one is not executed, so remove it.
284 if (IsBRU(SecondLastInst->getOpcode()) &&
285 IsBRU(LastInst->getOpcode())) {
286 TBB = SecondLastInst->getOperand(0).getMBB();
288 I->eraseFromParent();
292 // Otherwise, can't handle this.
297 XCoreInstrInfo::InsertBranch(MachineBasicBlock &MBB,MachineBasicBlock *TBB,
298 MachineBasicBlock *FBB,
299 const SmallVectorImpl<MachineOperand> &Cond)const{
300 // Shouldn't be a fall through.
301 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
302 assert((Cond.size() == 2 || Cond.size() == 0) &&
303 "Unexpected number of components!");
305 if (FBB == 0) { // One way branch.
307 // Unconditional branch
308 BuildMI(&MBB, get(XCore::BRFU_lu6)).addMBB(TBB);
310 // Conditional branch.
311 unsigned Opc = GetCondBranchFromCond((XCore::CondCode)Cond[0].getImm());
312 BuildMI(&MBB, get(Opc)).addReg(Cond[1].getReg())
318 // Two-way Conditional branch.
319 assert(Cond.size() == 2 && "Unexpected number of components!");
320 unsigned Opc = GetCondBranchFromCond((XCore::CondCode)Cond[0].getImm());
321 BuildMI(&MBB, get(Opc)).addReg(Cond[1].getReg())
323 BuildMI(&MBB, get(XCore::BRFU_lu6)).addMBB(FBB);
328 XCoreInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
329 MachineBasicBlock::iterator I = MBB.end();
330 if (I == MBB.begin()) return 0;
332 if (!IsBRU(I->getOpcode()) && !IsCondBranch(I->getOpcode()))
335 // Remove the branch.
336 I->eraseFromParent();
340 if (I == MBB.begin()) return 1;
342 if (!IsCondBranch(I->getOpcode()))
345 // Remove the branch.
346 I->eraseFromParent();
350 bool XCoreInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
351 MachineBasicBlock::iterator I,
352 unsigned DestReg, unsigned SrcReg,
353 const TargetRegisterClass *DestRC,
354 const TargetRegisterClass *SrcRC) const {
355 if (DestRC == SrcRC) {
356 if (DestRC == XCore::GRRegsRegisterClass) {
357 BuildMI(MBB, I, get(XCore::ADD_2rus), DestReg).addReg(SrcReg).addImm(0);
364 if (SrcRC == XCore::RRegsRegisterClass && SrcReg == XCore::SP &&
365 DestRC == XCore::GRRegsRegisterClass) {
366 BuildMI(MBB, I, get(XCore::LDAWSP_ru6), DestReg).addImm(0).addImm(0);
369 if (DestRC == XCore::RRegsRegisterClass && DestReg == XCore::SP &&
370 SrcRC == XCore::GRRegsRegisterClass) {
371 BuildMI(MBB, I, get(XCore::SETSP_1r)).addReg(SrcReg);
377 void XCoreInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
378 MachineBasicBlock::iterator I,
379 unsigned SrcReg, bool isKill, int FrameIndex,
380 const TargetRegisterClass *RC) const
382 BuildMI(MBB, I, get(XCore::STWFI)).addReg(SrcReg, false, false, isKill)
383 .addFrameIndex(FrameIndex).addImm(0);
386 void XCoreInstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
387 bool isKill, SmallVectorImpl<MachineOperand> &Addr,
388 const TargetRegisterClass *RC,
389 SmallVectorImpl<MachineInstr*> &NewMIs) const
391 assert(0 && "unimplemented\n");
394 void XCoreInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
395 MachineBasicBlock::iterator I,
396 unsigned DestReg, int FrameIndex,
397 const TargetRegisterClass *RC) const
399 BuildMI(MBB, I, get(XCore::LDWFI), DestReg).addFrameIndex(FrameIndex)
403 void XCoreInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
404 SmallVectorImpl<MachineOperand> &Addr,
405 const TargetRegisterClass *RC,
406 SmallVectorImpl<MachineInstr*> &NewMIs) const
408 assert(0 && "unimplemented\n");
411 bool XCoreInstrInfo::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
412 MachineBasicBlock::iterator MI,
413 const std::vector<CalleeSavedInfo> &CSI) const
418 MachineFunction *MF = MBB.getParent();
419 const MachineFrameInfo *MFI = MF->getFrameInfo();
420 MachineModuleInfo *MMI = MFI->getMachineModuleInfo();
421 XCoreFunctionInfo *XFI = MF->getInfo<XCoreFunctionInfo>();
423 bool emitFrameMoves = XCoreRegisterInfo::needsFrameMoves(*MF);
425 for (std::vector<CalleeSavedInfo>::const_iterator it = CSI.begin();
426 it != CSI.end(); ++it) {
427 // Add the callee-saved register as live-in. It's killed at the spill.
428 MBB.addLiveIn(it->getReg());
430 storeRegToStackSlot(MBB, MI, it->getReg(), true,
431 it->getFrameIdx(), it->getRegClass());
432 if (emitFrameMoves) {
433 unsigned SaveLabelId = MMI->NextLabelID();
434 BuildMI(MBB, MI, get(XCore::DBG_LABEL)).addImm(SaveLabelId);
435 XFI->getSpillLabels().push_back(
436 std::pair<unsigned, CalleeSavedInfo>(SaveLabelId, *it));
442 bool XCoreInstrInfo::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
443 MachineBasicBlock::iterator MI,
444 const std::vector<CalleeSavedInfo> &CSI) const
446 bool AtStart = MI == MBB.begin();
447 MachineBasicBlock::iterator BeforeI = MI;
450 for (std::vector<CalleeSavedInfo>::const_iterator it = CSI.begin();
451 it != CSI.end(); ++it) {
453 loadRegFromStackSlot(MBB, MI, it->getReg(),
456 assert(MI != MBB.begin() &&
457 "loadRegFromStackSlot didn't insert any code!");
458 // Insert in reverse order. loadRegFromStackSlot can insert multiple
470 /// BlockHasNoFallThrough - Analyse if MachineBasicBlock does not
471 /// fall-through into its successor block.
472 bool XCoreInstrInfo::
473 BlockHasNoFallThrough(const MachineBasicBlock &MBB) const
475 if (MBB.empty()) return false;
477 switch (MBB.back().getOpcode()) {
478 case XCore::RETSP_u6: // Return.
479 case XCore::RETSP_lu6:
480 case XCore::BAU_1r: // Indirect branch.
481 case XCore::BRFU_u6: // Uncond branch.
482 case XCore::BRFU_lu6:
484 case XCore::BRBU_lu6:
486 default: return false;
490 /// ReverseBranchCondition - Return the inverse opcode of the
491 /// specified Branch instruction.
492 bool XCoreInstrInfo::
493 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const
495 assert((Cond.size() == 2) &&
496 "Invalid XCore branch condition!");
497 Cond[0].setImm(GetOppositeBranchCondition((XCore::CondCode)Cond[0].getImm()));