Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 1 | //===- SystemZInstrInfo.cpp - SystemZ Instruction Information --------------===// |
| 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 | // |
| 10 | // This file contains the SystemZ implementation of the TargetInstrInfo class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "SystemZ.h" |
| 15 | #include "SystemZInstrInfo.h" |
| 16 | #include "SystemZMachineFunctionInfo.h" |
| 17 | #include "SystemZTargetMachine.h" |
| 18 | #include "SystemZGenInstrInfo.inc" |
| 19 | #include "llvm/Function.h" |
| 20 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 21 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 22 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 23 | #include "llvm/CodeGen/PseudoSourceValue.h" |
| 24 | |
| 25 | using namespace llvm; |
| 26 | |
| 27 | SystemZInstrInfo::SystemZInstrInfo(SystemZTargetMachine &tm) |
| 28 | : TargetInstrInfoImpl(SystemZInsts, array_lengthof(SystemZInsts)), |
Anton Korobeynikov | ef5deca | 2009-07-16 13:51:12 +0000 | [diff] [blame] | 29 | RI(tm, *this), TM(tm) { |
| 30 | // Fill the spill offsets map |
| 31 | static const unsigned SpillOffsTab[][2] = { |
| 32 | { SystemZ::R2D, 0x10 }, |
| 33 | { SystemZ::R3D, 0x18 }, |
| 34 | { SystemZ::R4D, 0x20 }, |
| 35 | { SystemZ::R5D, 0x28 }, |
| 36 | { SystemZ::R6D, 0x30 }, |
| 37 | { SystemZ::R7D, 0x38 }, |
| 38 | { SystemZ::R8D, 0x40 }, |
| 39 | { SystemZ::R9D, 0x48 }, |
| 40 | { SystemZ::R10D, 0x50 }, |
| 41 | { SystemZ::R11D, 0x58 }, |
| 42 | { SystemZ::R12D, 0x60 }, |
| 43 | { SystemZ::R13D, 0x68 }, |
| 44 | { SystemZ::R14D, 0x70 }, |
| 45 | { SystemZ::R15D, 0x78 } |
| 46 | }; |
| 47 | |
| 48 | RegSpillOffsets.grow(SystemZ::NUM_TARGET_REGS); |
| 49 | |
| 50 | for (unsigned i = 0, e = array_lengthof(SpillOffsTab); i != e; ++i) |
| 51 | RegSpillOffsets[SpillOffsTab[i][0]] = SpillOffsTab[i][1]; |
| 52 | } |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 53 | |
| 54 | void SystemZInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB, |
| 55 | MachineBasicBlock::iterator MI, |
| 56 | unsigned SrcReg, bool isKill, int FrameIdx, |
| 57 | const TargetRegisterClass *RC) const { |
| 58 | assert(0 && "Cannot store this register to stack slot!"); |
| 59 | } |
| 60 | |
| 61 | void SystemZInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB, |
| 62 | MachineBasicBlock::iterator MI, |
| 63 | unsigned DestReg, int FrameIdx, |
| 64 | const TargetRegisterClass *RC) const{ |
| 65 | assert(0 && "Cannot store this register to stack slot!"); |
| 66 | } |
| 67 | |
| 68 | bool SystemZInstrInfo::copyRegToReg(MachineBasicBlock &MBB, |
Anton Korobeynikov | 1cc9dc7 | 2009-07-16 13:29:38 +0000 | [diff] [blame] | 69 | MachineBasicBlock::iterator I, |
| 70 | unsigned DestReg, unsigned SrcReg, |
| 71 | const TargetRegisterClass *DestRC, |
| 72 | const TargetRegisterClass *SrcRC) const { |
| 73 | DebugLoc DL = DebugLoc::getUnknownLoc(); |
| 74 | if (I != MBB.end()) DL = I->getDebugLoc(); |
| 75 | |
Anton Korobeynikov | a51752c | 2009-07-16 13:42:31 +0000 | [diff] [blame] | 76 | // Determine if DstRC and SrcRC have a common superclass. |
| 77 | const TargetRegisterClass *CommonRC = DestRC; |
| 78 | if (DestRC == SrcRC) |
| 79 | /* Same regclass for source and dest */; |
| 80 | else if (CommonRC->hasSuperClass(SrcRC)) |
| 81 | CommonRC = SrcRC; |
| 82 | else if (!CommonRC->hasSubClass(SrcRC)) |
| 83 | CommonRC = 0; |
| 84 | |
| 85 | if (CommonRC) { |
Anton Korobeynikov | 1cc9dc7 | 2009-07-16 13:29:38 +0000 | [diff] [blame] | 86 | unsigned Opc; |
Anton Korobeynikov | 9e4816e | 2009-07-16 13:43:18 +0000 | [diff] [blame] | 87 | if (CommonRC == &SystemZ::GR64RegClass || |
| 88 | CommonRC == &SystemZ::ADDR64RegClass) { |
Anton Korobeynikov | 1cc9dc7 | 2009-07-16 13:29:38 +0000 | [diff] [blame] | 89 | Opc = SystemZ::MOV64rr; |
Anton Korobeynikov | 9e4816e | 2009-07-16 13:43:18 +0000 | [diff] [blame] | 90 | } else if (CommonRC == &SystemZ::GR32RegClass || |
| 91 | CommonRC == &SystemZ::ADDR32RegClass) { |
Anton Korobeynikov | a51752c | 2009-07-16 13:42:31 +0000 | [diff] [blame] | 92 | Opc = SystemZ::MOV32rr; |
Anton Korobeynikov | 1cc9dc7 | 2009-07-16 13:29:38 +0000 | [diff] [blame] | 93 | } else { |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | BuildMI(MBB, I, DL, get(Opc), DestReg).addReg(SrcReg); |
| 98 | return true; |
| 99 | } |
| 100 | |
Anton Korobeynikov | 9e4816e | 2009-07-16 13:43:18 +0000 | [diff] [blame] | 101 | if ((SrcRC == &SystemZ::GR64RegClass && |
| 102 | DestRC == &SystemZ::ADDR64RegClass) || |
| 103 | (DestRC == &SystemZ::GR64RegClass && |
| 104 | SrcRC == &SystemZ::ADDR64RegClass)) { |
| 105 | BuildMI(MBB, I, DL, get(SystemZ::MOV64rr), DestReg).addReg(SrcReg); |
| 106 | return true; |
| 107 | } else if ((SrcRC == &SystemZ::GR32RegClass && |
| 108 | DestRC == &SystemZ::ADDR32RegClass) || |
| 109 | (DestRC == &SystemZ::GR32RegClass && |
| 110 | SrcRC == &SystemZ::ADDR32RegClass)) { |
| 111 | BuildMI(MBB, I, DL, get(SystemZ::MOV32rr), DestReg).addReg(SrcReg); |
| 112 | return true; |
| 113 | } |
| 114 | |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 115 | return false; |
| 116 | } |
| 117 | |
| 118 | bool |
| 119 | SystemZInstrInfo::isMoveInstr(const MachineInstr& MI, |
Anton Korobeynikov | 1cc9dc7 | 2009-07-16 13:29:38 +0000 | [diff] [blame] | 120 | unsigned &SrcReg, unsigned &DstReg, |
| 121 | unsigned &SrcSubIdx, unsigned &DstSubIdx) const { |
| 122 | SrcSubIdx = DstSubIdx = 0; // No sub-registers yet. |
| 123 | |
| 124 | switch (MI.getOpcode()) { |
| 125 | default: |
| 126 | return false; |
Anton Korobeynikov | a51752c | 2009-07-16 13:42:31 +0000 | [diff] [blame] | 127 | case SystemZ::MOV32rr: |
Anton Korobeynikov | 1cc9dc7 | 2009-07-16 13:29:38 +0000 | [diff] [blame] | 128 | case SystemZ::MOV64rr: |
| 129 | assert(MI.getNumOperands() >= 2 && |
| 130 | MI.getOperand(0).isReg() && |
| 131 | MI.getOperand(1).isReg() && |
| 132 | "invalid register-register move instruction"); |
| 133 | SrcReg = MI.getOperand(1).getReg(); |
| 134 | DstReg = MI.getOperand(0).getReg(); |
| 135 | return true; |
| 136 | } |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | bool |
| 140 | SystemZInstrInfo::spillCalleeSavedRegisters(MachineBasicBlock &MBB, |
| 141 | MachineBasicBlock::iterator MI, |
| 142 | const std::vector<CalleeSavedInfo> &CSI) const { |
Anton Korobeynikov | ef5deca | 2009-07-16 13:51:12 +0000 | [diff] [blame] | 143 | DebugLoc DL = DebugLoc::getUnknownLoc(); |
| 144 | if (MI != MBB.end()) DL = MI->getDebugLoc(); |
Anton Korobeynikov | ba249e4 | 2009-07-16 13:50:21 +0000 | [diff] [blame] | 145 | |
| 146 | MachineFunction &MF = *MBB.getParent(); |
| 147 | SystemZMachineFunctionInfo *MFI = MF.getInfo<SystemZMachineFunctionInfo>(); |
| 148 | MFI->setCalleeSavedFrameSize(CSI.size() * 8); |
| 149 | |
Anton Korobeynikov | ef5deca | 2009-07-16 13:51:12 +0000 | [diff] [blame] | 150 | // Scan the callee-saved and find the bounds of register spill area. |
| 151 | unsigned LowReg = 0, HighReg = 0, StartOffset = -1U, EndOffset = 0; |
| 152 | for (unsigned i = 0, e = CSI.size(); i != e; ++i) { |
| 153 | unsigned Reg = CSI[i].getReg(); |
| 154 | unsigned Offset = RegSpillOffsets[Reg]; |
| 155 | if (StartOffset > Offset) { |
| 156 | LowReg = Reg; StartOffset = Offset; |
| 157 | } |
| 158 | if (EndOffset < Offset) { |
| 159 | HighReg = Reg; EndOffset = RegSpillOffsets[Reg]; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | // Save information for epilogue inserter. |
| 164 | MFI->setLowReg(LowReg); MFI->setHighReg(HighReg); |
| 165 | |
| 166 | // Build a store instruction. Use STORE MULTIPLE instruction if there are many |
| 167 | // registers to store, otherwise - just STORE. |
| 168 | MachineInstrBuilder MIB = |
| 169 | BuildMI(MBB, MI, DL, get((LowReg == HighReg ? |
| 170 | SystemZ::MOV64mr : SystemZ::MOV64mrm))); |
| 171 | |
| 172 | // Add store operands. |
| 173 | MIB.addReg(SystemZ::R15D).addImm(StartOffset); |
| 174 | if (LowReg == HighReg) |
| 175 | MIB.addReg(0); |
| 176 | MIB.addReg(LowReg, RegState::Kill); |
| 177 | if (LowReg != HighReg) |
| 178 | MIB.addReg(HighReg, RegState::Kill); |
| 179 | |
| 180 | // Do a second scan adding regs as being killed by instruction |
| 181 | for (unsigned i = 0, e = CSI.size(); i != e; ++i) { |
| 182 | unsigned Reg = CSI[i].getReg(); |
| 183 | // Add the callee-saved register as live-in. It's killed at the spill. |
| 184 | MBB.addLiveIn(Reg); |
| 185 | if (Reg != LowReg && Reg != HighReg) |
| 186 | MIB.addReg(Reg, RegState::ImplicitKill); |
| 187 | } |
| 188 | |
Anton Korobeynikov | ba249e4 | 2009-07-16 13:50:21 +0000 | [diff] [blame] | 189 | return true; |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | bool |
| 193 | SystemZInstrInfo::restoreCalleeSavedRegisters(MachineBasicBlock &MBB, |
| 194 | MachineBasicBlock::iterator MI, |
| 195 | const std::vector<CalleeSavedInfo> &CSI) const { |
Anton Korobeynikov | ef5deca | 2009-07-16 13:51:12 +0000 | [diff] [blame] | 196 | if (CSI.empty()) |
| 197 | return false; |
| 198 | |
| 199 | DebugLoc DL = DebugLoc::getUnknownLoc(); |
| 200 | if (MI != MBB.end()) DL = MI->getDebugLoc(); |
| 201 | |
| 202 | MachineFunction &MF = *MBB.getParent(); |
| 203 | const TargetRegisterInfo *RegInfo= MF.getTarget().getRegisterInfo(); |
| 204 | SystemZMachineFunctionInfo *MFI = MF.getInfo<SystemZMachineFunctionInfo>(); |
| 205 | |
| 206 | unsigned LowReg = MFI->getLowReg(), HighReg = MFI->getHighReg(); |
| 207 | unsigned StartOffset = RegSpillOffsets[LowReg]; |
| 208 | |
| 209 | // Build a load instruction. Use LOAD MULTIPLE instruction if there are many |
| 210 | // registers to load, otherwise - just LOAD. |
| 211 | MachineInstrBuilder MIB = |
| 212 | BuildMI(MBB, MI, DL, get((LowReg == HighReg ? |
| 213 | SystemZ::MOV64rm : SystemZ::MOV64rmm))); |
| 214 | // Add store operands. |
| 215 | MIB.addReg(LowReg, RegState::Define); |
| 216 | if (LowReg != HighReg) |
| 217 | MIB.addReg(HighReg, RegState::Define); |
| 218 | |
| 219 | MIB.addReg((RegInfo->hasFP(MF) ? SystemZ::R11D : SystemZ::R15D)); |
| 220 | MIB.addImm(StartOffset); |
| 221 | if (LowReg == HighReg) |
| 222 | MIB.addReg(0); |
| 223 | |
| 224 | // Do a second scan adding regs as being defined by instruction |
| 225 | for (unsigned i = 0, e = CSI.size(); i != e; ++i) { |
| 226 | unsigned Reg = CSI[i].getReg(); |
| 227 | if (Reg != LowReg && Reg != HighReg) |
| 228 | MIB.addReg(Reg, RegState::ImplicitDefine); |
| 229 | } |
| 230 | |
Anton Korobeynikov | ba249e4 | 2009-07-16 13:50:21 +0000 | [diff] [blame] | 231 | return true; |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | unsigned |
| 235 | SystemZInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB, |
| 236 | MachineBasicBlock *FBB, |
| 237 | const SmallVectorImpl<MachineOperand> &Cond) const { |
| 238 | assert(0 && "Implement branches!"); |
| 239 | |
| 240 | return 0; |
| 241 | } |
Anton Korobeynikov | 7d1e39b | 2009-07-16 13:52:51 +0000 | [diff] [blame^] | 242 | |
| 243 | const TargetInstrDesc& |
| 244 | SystemZInstrInfo::getBrCond(SystemZCC::CondCodes CC) const { |
| 245 | unsigned Opc; |
| 246 | switch (CC) { |
| 247 | default: |
| 248 | assert(0 && "Unknown condition code!"); |
| 249 | case SystemZCC::E: |
| 250 | Opc = SystemZ::JE; |
| 251 | break; |
| 252 | case SystemZCC::NE: |
| 253 | Opc = SystemZ::JNE; |
| 254 | break; |
| 255 | case SystemZCC::H: |
| 256 | Opc = SystemZ::JH; |
| 257 | break; |
| 258 | case SystemZCC::L: |
| 259 | Opc = SystemZ::JL; |
| 260 | break; |
| 261 | case SystemZCC::HE: |
| 262 | Opc = SystemZ::JHE; |
| 263 | break; |
| 264 | case SystemZCC::LE: |
| 265 | Opc = SystemZ::JLE; |
| 266 | break; |
| 267 | } |
| 268 | |
| 269 | return get(Opc); |
| 270 | } |