Jim Grosbach | 31c24bf | 2009-11-07 22:00:39 +0000 | [diff] [blame] | 1 | //===- Thumb1InstrInfo.cpp - Thumb-1 Instruction Information ----*- C++ -*-===// |
Anton Korobeynikov | d49ea77 | 2009-06-26 21:28:53 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
David Goodwin | b50ea5c | 2009-07-02 22:18:33 +0000 | [diff] [blame] | 10 | // This file contains the Thumb-1 implementation of the TargetInstrInfo class. |
Anton Korobeynikov | d49ea77 | 2009-06-26 21:28:53 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Evan Cheng | b9803a8 | 2009-11-06 23:52:48 +0000 | [diff] [blame] | 14 | #include "Thumb1InstrInfo.h" |
Anton Korobeynikov | d49ea77 | 2009-06-26 21:28:53 +0000 | [diff] [blame] | 15 | #include "ARM.h" |
| 16 | #include "ARMGenInstrInfo.inc" |
| 17 | #include "ARMMachineFunctionInfo.h" |
| 18 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 19 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Evan Cheng | 2457f2c | 2010-05-22 01:47:14 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Evan Cheng | e3ce8aa | 2009-11-01 22:04:35 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineMemOperand.h" |
| 22 | #include "llvm/CodeGen/PseudoSourceValue.h" |
Anton Korobeynikov | d49ea77 | 2009-06-26 21:28:53 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/SmallVector.h" |
David Goodwin | b50ea5c | 2009-07-02 22:18:33 +0000 | [diff] [blame] | 24 | #include "Thumb1InstrInfo.h" |
Anton Korobeynikov | d49ea77 | 2009-06-26 21:28:53 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace llvm; |
| 27 | |
Anton Korobeynikov | f95215f | 2009-11-02 00:10:38 +0000 | [diff] [blame] | 28 | Thumb1InstrInfo::Thumb1InstrInfo(const ARMSubtarget &STI) |
| 29 | : ARMBaseInstrInfo(STI), RI(*this, STI) { |
Anton Korobeynikov | d49ea77 | 2009-06-26 21:28:53 +0000 | [diff] [blame] | 30 | } |
| 31 | |
Evan Cheng | 446c428 | 2009-07-11 06:43:01 +0000 | [diff] [blame] | 32 | unsigned Thumb1InstrInfo::getUnindexedOpcode(unsigned Opc) const { |
David Goodwin | 334c264 | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 33 | return 0; |
| 34 | } |
| 35 | |
Jakob Stoklund Olesen | ac27366 | 2010-07-11 06:33:54 +0000 | [diff] [blame] | 36 | void Thumb1InstrInfo::copyPhysReg(MachineBasicBlock &MBB, |
| 37 | MachineBasicBlock::iterator I, DebugLoc DL, |
| 38 | unsigned DestReg, unsigned SrcReg, |
| 39 | bool KillSrc) const { |
| 40 | bool tDest = ARM::tGPRRegClass.contains(DestReg); |
| 41 | bool tSrc = ARM::tGPRRegClass.contains(SrcReg); |
| 42 | unsigned Opc = ARM::tMOVgpr2gpr; |
| 43 | if (tDest && tSrc) |
| 44 | Opc = ARM::tMOVr; |
| 45 | else if (tSrc) |
| 46 | Opc = ARM::tMOVtgpr2gpr; |
| 47 | else if (tDest) |
| 48 | Opc = ARM::tMOVgpr2tgpr; |
Anton Korobeynikov | d49ea77 | 2009-06-26 21:28:53 +0000 | [diff] [blame] | 49 | |
Jakob Stoklund Olesen | ac27366 | 2010-07-11 06:33:54 +0000 | [diff] [blame] | 50 | BuildMI(MBB, I, DL, get(Opc), DestReg) |
| 51 | .addReg(SrcReg, getKillRegState(KillSrc)); |
| 52 | assert(ARM::GPRRegClass.contains(DestReg, SrcReg) && |
| 53 | "Thumb1 can only copy GPR registers"); |
Anton Korobeynikov | d49ea77 | 2009-06-26 21:28:53 +0000 | [diff] [blame] | 54 | } |
| 55 | |
David Goodwin | b50ea5c | 2009-07-02 22:18:33 +0000 | [diff] [blame] | 56 | void Thumb1InstrInfo:: |
Anton Korobeynikov | d49ea77 | 2009-06-26 21:28:53 +0000 | [diff] [blame] | 57 | storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I, |
| 58 | unsigned SrcReg, bool isKill, int FI, |
Evan Cheng | 746ad69 | 2010-05-06 19:06:44 +0000 | [diff] [blame] | 59 | const TargetRegisterClass *RC, |
| 60 | const TargetRegisterInfo *TRI) const { |
Evan Cheng | 86e5f7b | 2009-08-13 05:40:51 +0000 | [diff] [blame] | 61 | assert((RC == ARM::tGPRRegisterClass || |
| 62 | (TargetRegisterInfo::isPhysicalRegister(SrcReg) && |
| 63 | isARMLowRegister(SrcReg))) && "Unknown regclass!"); |
Anton Korobeynikov | d49ea77 | 2009-06-26 21:28:53 +0000 | [diff] [blame] | 64 | |
Jim Grosbach | 98793b9 | 2010-01-15 22:21:03 +0000 | [diff] [blame] | 65 | if (RC == ARM::tGPRRegisterClass || |
| 66 | (TargetRegisterInfo::isPhysicalRegister(SrcReg) && |
| 67 | isARMLowRegister(SrcReg))) { |
Evan Cheng | 746ad69 | 2010-05-06 19:06:44 +0000 | [diff] [blame] | 68 | DebugLoc DL; |
| 69 | if (I != MBB.end()) DL = I->getDebugLoc(); |
| 70 | |
Evan Cheng | e3ce8aa | 2009-11-01 22:04:35 +0000 | [diff] [blame] | 71 | MachineFunction &MF = *MBB.getParent(); |
| 72 | MachineFrameInfo &MFI = *MF.getFrameInfo(); |
| 73 | MachineMemOperand *MMO = |
Chris Lattner | 59db549 | 2010-09-21 04:39:43 +0000 | [diff] [blame^] | 74 | MF.getMachineMemOperand( |
| 75 | MachinePointerInfo(PseudoSourceValue::getFixedStack(FI)), |
| 76 | MachineMemOperand::MOStore, |
Evan Cheng | e3ce8aa | 2009-11-01 22:04:35 +0000 | [diff] [blame] | 77 | MFI.getObjectSize(FI), |
| 78 | MFI.getObjectAlignment(FI)); |
Evan Cheng | 446c428 | 2009-07-11 06:43:01 +0000 | [diff] [blame] | 79 | AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::tSpill)) |
| 80 | .addReg(SrcReg, getKillRegState(isKill)) |
Evan Cheng | e3ce8aa | 2009-11-01 22:04:35 +0000 | [diff] [blame] | 81 | .addFrameIndex(FI).addImm(0).addMemOperand(MMO)); |
Anton Korobeynikov | d49ea77 | 2009-06-26 21:28:53 +0000 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
David Goodwin | b50ea5c | 2009-07-02 22:18:33 +0000 | [diff] [blame] | 85 | void Thumb1InstrInfo:: |
Anton Korobeynikov | d49ea77 | 2009-06-26 21:28:53 +0000 | [diff] [blame] | 86 | loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I, |
| 87 | unsigned DestReg, int FI, |
Evan Cheng | 746ad69 | 2010-05-06 19:06:44 +0000 | [diff] [blame] | 88 | const TargetRegisterClass *RC, |
| 89 | const TargetRegisterInfo *TRI) const { |
Evan Cheng | 86e5f7b | 2009-08-13 05:40:51 +0000 | [diff] [blame] | 90 | assert((RC == ARM::tGPRRegisterClass || |
| 91 | (TargetRegisterInfo::isPhysicalRegister(DestReg) && |
| 92 | isARMLowRegister(DestReg))) && "Unknown regclass!"); |
Anton Korobeynikov | d49ea77 | 2009-06-26 21:28:53 +0000 | [diff] [blame] | 93 | |
Jim Grosbach | 98793b9 | 2010-01-15 22:21:03 +0000 | [diff] [blame] | 94 | if (RC == ARM::tGPRRegisterClass || |
| 95 | (TargetRegisterInfo::isPhysicalRegister(DestReg) && |
| 96 | isARMLowRegister(DestReg))) { |
Evan Cheng | 746ad69 | 2010-05-06 19:06:44 +0000 | [diff] [blame] | 97 | DebugLoc DL; |
| 98 | if (I != MBB.end()) DL = I->getDebugLoc(); |
| 99 | |
Evan Cheng | e3ce8aa | 2009-11-01 22:04:35 +0000 | [diff] [blame] | 100 | MachineFunction &MF = *MBB.getParent(); |
| 101 | MachineFrameInfo &MFI = *MF.getFrameInfo(); |
| 102 | MachineMemOperand *MMO = |
Chris Lattner | 59db549 | 2010-09-21 04:39:43 +0000 | [diff] [blame^] | 103 | MF.getMachineMemOperand( |
| 104 | MachinePointerInfo(PseudoSourceValue::getFixedStack(FI)), |
| 105 | MachineMemOperand::MOLoad, |
Evan Cheng | e3ce8aa | 2009-11-01 22:04:35 +0000 | [diff] [blame] | 106 | MFI.getObjectSize(FI), |
| 107 | MFI.getObjectAlignment(FI)); |
Evan Cheng | 446c428 | 2009-07-11 06:43:01 +0000 | [diff] [blame] | 108 | AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::tRestore), DestReg) |
Evan Cheng | e3ce8aa | 2009-11-01 22:04:35 +0000 | [diff] [blame] | 109 | .addFrameIndex(FI).addImm(0).addMemOperand(MMO)); |
Anton Korobeynikov | d49ea77 | 2009-06-26 21:28:53 +0000 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
David Goodwin | b50ea5c | 2009-07-02 22:18:33 +0000 | [diff] [blame] | 113 | bool Thumb1InstrInfo:: |
Anton Korobeynikov | d49ea77 | 2009-06-26 21:28:53 +0000 | [diff] [blame] | 114 | spillCalleeSavedRegisters(MachineBasicBlock &MBB, |
| 115 | MachineBasicBlock::iterator MI, |
Evan Cheng | 2457f2c | 2010-05-22 01:47:14 +0000 | [diff] [blame] | 116 | const std::vector<CalleeSavedInfo> &CSI, |
| 117 | const TargetRegisterInfo *TRI) const { |
Anton Korobeynikov | d49ea77 | 2009-06-26 21:28:53 +0000 | [diff] [blame] | 118 | if (CSI.empty()) |
| 119 | return false; |
| 120 | |
Chris Lattner | c7f3ace | 2010-04-02 20:16:16 +0000 | [diff] [blame] | 121 | DebugLoc DL; |
Anton Korobeynikov | d49ea77 | 2009-06-26 21:28:53 +0000 | [diff] [blame] | 122 | if (MI != MBB.end()) DL = MI->getDebugLoc(); |
| 123 | |
| 124 | MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, get(ARM::tPUSH)); |
Evan Cheng | 4b322e5 | 2009-08-11 21:11:32 +0000 | [diff] [blame] | 125 | AddDefaultPred(MIB); |
Anton Korobeynikov | d49ea77 | 2009-06-26 21:28:53 +0000 | [diff] [blame] | 126 | for (unsigned i = CSI.size(); i != 0; --i) { |
| 127 | unsigned Reg = CSI[i-1].getReg(); |
Evan Cheng | 2457f2c | 2010-05-22 01:47:14 +0000 | [diff] [blame] | 128 | bool isKill = true; |
| 129 | |
| 130 | // Add the callee-saved register as live-in unless it's LR and |
| 131 | // @llvm.returnaddress is called. If LR is returned for @llvm.returnaddress |
| 132 | // then it's already added to the function and entry block live-in sets. |
| 133 | if (Reg == ARM::LR) { |
| 134 | MachineFunction &MF = *MBB.getParent(); |
| 135 | if (MF.getFrameInfo()->isReturnAddressTaken() && |
| 136 | MF.getRegInfo().isLiveIn(Reg)) |
| 137 | isKill = false; |
| 138 | } |
| 139 | |
Bob Wilson | a3a2046 | 2010-06-22 22:04:24 +0000 | [diff] [blame] | 140 | if (isKill) |
Evan Cheng | 2457f2c | 2010-05-22 01:47:14 +0000 | [diff] [blame] | 141 | MBB.addLiveIn(Reg); |
Bob Wilson | a3a2046 | 2010-06-22 22:04:24 +0000 | [diff] [blame] | 142 | |
| 143 | MIB.addReg(Reg, getKillRegState(isKill)); |
Anton Korobeynikov | d49ea77 | 2009-06-26 21:28:53 +0000 | [diff] [blame] | 144 | } |
| 145 | return true; |
| 146 | } |
| 147 | |
David Goodwin | b50ea5c | 2009-07-02 22:18:33 +0000 | [diff] [blame] | 148 | bool Thumb1InstrInfo:: |
Anton Korobeynikov | d49ea77 | 2009-06-26 21:28:53 +0000 | [diff] [blame] | 149 | restoreCalleeSavedRegisters(MachineBasicBlock &MBB, |
| 150 | MachineBasicBlock::iterator MI, |
Evan Cheng | 2457f2c | 2010-05-22 01:47:14 +0000 | [diff] [blame] | 151 | const std::vector<CalleeSavedInfo> &CSI, |
| 152 | const TargetRegisterInfo *TRI) const { |
Anton Korobeynikov | d49ea77 | 2009-06-26 21:28:53 +0000 | [diff] [blame] | 153 | MachineFunction &MF = *MBB.getParent(); |
| 154 | ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); |
| 155 | if (CSI.empty()) |
| 156 | return false; |
| 157 | |
| 158 | bool isVarArg = AFI->getVarArgsRegSaveSize() > 0; |
Evan Cheng | 4b322e5 | 2009-08-11 21:11:32 +0000 | [diff] [blame] | 159 | DebugLoc DL = MI->getDebugLoc(); |
| 160 | MachineInstrBuilder MIB = BuildMI(MF, DL, get(ARM::tPOP)); |
| 161 | AddDefaultPred(MIB); |
| 162 | |
John McCall | 6eeccd4 | 2009-12-16 20:31:50 +0000 | [diff] [blame] | 163 | bool NumRegs = false; |
Anton Korobeynikov | d49ea77 | 2009-06-26 21:28:53 +0000 | [diff] [blame] | 164 | for (unsigned i = CSI.size(); i != 0; --i) { |
| 165 | unsigned Reg = CSI[i-1].getReg(); |
| 166 | if (Reg == ARM::LR) { |
| 167 | // Special epilogue for vararg functions. See emitEpilogue |
| 168 | if (isVarArg) |
| 169 | continue; |
| 170 | Reg = ARM::PC; |
Evan Cheng | 4b322e5 | 2009-08-11 21:11:32 +0000 | [diff] [blame] | 171 | (*MIB).setDesc(get(ARM::tPOP_RET)); |
Anton Korobeynikov | d49ea77 | 2009-06-26 21:28:53 +0000 | [diff] [blame] | 172 | MI = MBB.erase(MI); |
| 173 | } |
Evan Cheng | 4b322e5 | 2009-08-11 21:11:32 +0000 | [diff] [blame] | 174 | MIB.addReg(Reg, getDefRegState(true)); |
John McCall | 6eeccd4 | 2009-12-16 20:31:50 +0000 | [diff] [blame] | 175 | NumRegs = true; |
Anton Korobeynikov | d49ea77 | 2009-06-26 21:28:53 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | // It's illegal to emit pop instruction without operands. |
Evan Cheng | 4b322e5 | 2009-08-11 21:11:32 +0000 | [diff] [blame] | 179 | if (NumRegs) |
| 180 | MBB.insert(MI, &*MIB); |
Jeffrey Yasskin | fa72340 | 2010-03-22 16:13:21 +0000 | [diff] [blame] | 181 | else |
| 182 | MF.DeleteMachineInstr(MIB); |
Anton Korobeynikov | d49ea77 | 2009-06-26 21:28:53 +0000 | [diff] [blame] | 183 | |
| 184 | return true; |
| 185 | } |