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 | 9e4816e | 2009-07-16 13:43:18 +0000 | [diff] [blame] | 86 | if (CommonRC == &SystemZ::GR64RegClass || |
| 87 | CommonRC == &SystemZ::ADDR64RegClass) { |
Anton Korobeynikov | 8d1837d | 2009-07-16 13:56:42 +0000 | [diff] [blame] | 88 | BuildMI(MBB, I, DL, get(SystemZ::MOV64rr), DestReg).addReg(SrcReg); |
Anton Korobeynikov | 9e4816e | 2009-07-16 13:43:18 +0000 | [diff] [blame] | 89 | } else if (CommonRC == &SystemZ::GR32RegClass || |
| 90 | CommonRC == &SystemZ::ADDR32RegClass) { |
Anton Korobeynikov | 8d1837d | 2009-07-16 13:56:42 +0000 | [diff] [blame] | 91 | BuildMI(MBB, I, DL, get(SystemZ::MOV32rr), DestReg).addReg(SrcReg); |
| 92 | } else if (CommonRC == &SystemZ::GR64PRegClass) { |
| 93 | BuildMI(MBB, I, DL, get(SystemZ::MOV64rrP), DestReg).addReg(SrcReg); |
| 94 | } else if (CommonRC == &SystemZ::GR128RegClass) { |
| 95 | BuildMI(MBB, I, DL, get(SystemZ::MOV128rr), DestReg).addReg(SrcReg); |
Anton Korobeynikov | 1cc9dc7 | 2009-07-16 13:29:38 +0000 | [diff] [blame] | 96 | } else { |
| 97 | return false; |
| 98 | } |
| 99 | |
Anton Korobeynikov | 1cc9dc7 | 2009-07-16 13:29:38 +0000 | [diff] [blame] | 100 | return true; |
| 101 | } |
| 102 | |
Anton Korobeynikov | 9e4816e | 2009-07-16 13:43:18 +0000 | [diff] [blame] | 103 | if ((SrcRC == &SystemZ::GR64RegClass && |
| 104 | DestRC == &SystemZ::ADDR64RegClass) || |
| 105 | (DestRC == &SystemZ::GR64RegClass && |
| 106 | SrcRC == &SystemZ::ADDR64RegClass)) { |
| 107 | BuildMI(MBB, I, DL, get(SystemZ::MOV64rr), DestReg).addReg(SrcReg); |
| 108 | return true; |
| 109 | } else if ((SrcRC == &SystemZ::GR32RegClass && |
| 110 | DestRC == &SystemZ::ADDR32RegClass) || |
| 111 | (DestRC == &SystemZ::GR32RegClass && |
| 112 | SrcRC == &SystemZ::ADDR32RegClass)) { |
| 113 | BuildMI(MBB, I, DL, get(SystemZ::MOV32rr), DestReg).addReg(SrcReg); |
| 114 | return true; |
| 115 | } |
| 116 | |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 117 | return false; |
| 118 | } |
| 119 | |
| 120 | bool |
| 121 | SystemZInstrInfo::isMoveInstr(const MachineInstr& MI, |
Anton Korobeynikov | 1cc9dc7 | 2009-07-16 13:29:38 +0000 | [diff] [blame] | 122 | unsigned &SrcReg, unsigned &DstReg, |
| 123 | unsigned &SrcSubIdx, unsigned &DstSubIdx) const { |
| 124 | SrcSubIdx = DstSubIdx = 0; // No sub-registers yet. |
| 125 | |
| 126 | switch (MI.getOpcode()) { |
| 127 | default: |
| 128 | return false; |
Anton Korobeynikov | a51752c | 2009-07-16 13:42:31 +0000 | [diff] [blame] | 129 | case SystemZ::MOV32rr: |
Anton Korobeynikov | 1cc9dc7 | 2009-07-16 13:29:38 +0000 | [diff] [blame] | 130 | case SystemZ::MOV64rr: |
Anton Korobeynikov | 8d1837d | 2009-07-16 13:56:42 +0000 | [diff] [blame] | 131 | case SystemZ::MOV64rrP: |
| 132 | case SystemZ::MOV128rr: |
Anton Korobeynikov | 1cc9dc7 | 2009-07-16 13:29:38 +0000 | [diff] [blame] | 133 | assert(MI.getNumOperands() >= 2 && |
| 134 | MI.getOperand(0).isReg() && |
| 135 | MI.getOperand(1).isReg() && |
| 136 | "invalid register-register move instruction"); |
| 137 | SrcReg = MI.getOperand(1).getReg(); |
| 138 | DstReg = MI.getOperand(0).getReg(); |
| 139 | return true; |
| 140 | } |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | bool |
| 144 | SystemZInstrInfo::spillCalleeSavedRegisters(MachineBasicBlock &MBB, |
| 145 | MachineBasicBlock::iterator MI, |
| 146 | const std::vector<CalleeSavedInfo> &CSI) const { |
Anton Korobeynikov | ef5deca | 2009-07-16 13:51:12 +0000 | [diff] [blame] | 147 | DebugLoc DL = DebugLoc::getUnknownLoc(); |
| 148 | if (MI != MBB.end()) DL = MI->getDebugLoc(); |
Anton Korobeynikov | ba249e4 | 2009-07-16 13:50:21 +0000 | [diff] [blame] | 149 | |
| 150 | MachineFunction &MF = *MBB.getParent(); |
| 151 | SystemZMachineFunctionInfo *MFI = MF.getInfo<SystemZMachineFunctionInfo>(); |
| 152 | MFI->setCalleeSavedFrameSize(CSI.size() * 8); |
| 153 | |
Anton Korobeynikov | ef5deca | 2009-07-16 13:51:12 +0000 | [diff] [blame] | 154 | // Scan the callee-saved and find the bounds of register spill area. |
| 155 | unsigned LowReg = 0, HighReg = 0, StartOffset = -1U, EndOffset = 0; |
| 156 | for (unsigned i = 0, e = CSI.size(); i != e; ++i) { |
| 157 | unsigned Reg = CSI[i].getReg(); |
| 158 | unsigned Offset = RegSpillOffsets[Reg]; |
| 159 | if (StartOffset > Offset) { |
| 160 | LowReg = Reg; StartOffset = Offset; |
| 161 | } |
| 162 | if (EndOffset < Offset) { |
| 163 | HighReg = Reg; EndOffset = RegSpillOffsets[Reg]; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | // Save information for epilogue inserter. |
| 168 | MFI->setLowReg(LowReg); MFI->setHighReg(HighReg); |
| 169 | |
| 170 | // Build a store instruction. Use STORE MULTIPLE instruction if there are many |
| 171 | // registers to store, otherwise - just STORE. |
| 172 | MachineInstrBuilder MIB = |
| 173 | BuildMI(MBB, MI, DL, get((LowReg == HighReg ? |
| 174 | SystemZ::MOV64mr : SystemZ::MOV64mrm))); |
| 175 | |
| 176 | // Add store operands. |
| 177 | MIB.addReg(SystemZ::R15D).addImm(StartOffset); |
| 178 | if (LowReg == HighReg) |
| 179 | MIB.addReg(0); |
| 180 | MIB.addReg(LowReg, RegState::Kill); |
| 181 | if (LowReg != HighReg) |
| 182 | MIB.addReg(HighReg, RegState::Kill); |
| 183 | |
| 184 | // Do a second scan adding regs as being killed by instruction |
| 185 | for (unsigned i = 0, e = CSI.size(); i != e; ++i) { |
| 186 | unsigned Reg = CSI[i].getReg(); |
| 187 | // Add the callee-saved register as live-in. It's killed at the spill. |
| 188 | MBB.addLiveIn(Reg); |
| 189 | if (Reg != LowReg && Reg != HighReg) |
| 190 | MIB.addReg(Reg, RegState::ImplicitKill); |
| 191 | } |
| 192 | |
Anton Korobeynikov | ba249e4 | 2009-07-16 13:50:21 +0000 | [diff] [blame] | 193 | return true; |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | bool |
| 197 | SystemZInstrInfo::restoreCalleeSavedRegisters(MachineBasicBlock &MBB, |
| 198 | MachineBasicBlock::iterator MI, |
| 199 | const std::vector<CalleeSavedInfo> &CSI) const { |
Anton Korobeynikov | ef5deca | 2009-07-16 13:51:12 +0000 | [diff] [blame] | 200 | if (CSI.empty()) |
| 201 | return false; |
| 202 | |
| 203 | DebugLoc DL = DebugLoc::getUnknownLoc(); |
| 204 | if (MI != MBB.end()) DL = MI->getDebugLoc(); |
| 205 | |
| 206 | MachineFunction &MF = *MBB.getParent(); |
| 207 | const TargetRegisterInfo *RegInfo= MF.getTarget().getRegisterInfo(); |
| 208 | SystemZMachineFunctionInfo *MFI = MF.getInfo<SystemZMachineFunctionInfo>(); |
| 209 | |
| 210 | unsigned LowReg = MFI->getLowReg(), HighReg = MFI->getHighReg(); |
| 211 | unsigned StartOffset = RegSpillOffsets[LowReg]; |
| 212 | |
| 213 | // Build a load instruction. Use LOAD MULTIPLE instruction if there are many |
| 214 | // registers to load, otherwise - just LOAD. |
| 215 | MachineInstrBuilder MIB = |
| 216 | BuildMI(MBB, MI, DL, get((LowReg == HighReg ? |
| 217 | SystemZ::MOV64rm : SystemZ::MOV64rmm))); |
| 218 | // Add store operands. |
| 219 | MIB.addReg(LowReg, RegState::Define); |
| 220 | if (LowReg != HighReg) |
| 221 | MIB.addReg(HighReg, RegState::Define); |
| 222 | |
| 223 | MIB.addReg((RegInfo->hasFP(MF) ? SystemZ::R11D : SystemZ::R15D)); |
| 224 | MIB.addImm(StartOffset); |
| 225 | if (LowReg == HighReg) |
| 226 | MIB.addReg(0); |
| 227 | |
| 228 | // Do a second scan adding regs as being defined by instruction |
| 229 | for (unsigned i = 0, e = CSI.size(); i != e; ++i) { |
| 230 | unsigned Reg = CSI[i].getReg(); |
| 231 | if (Reg != LowReg && Reg != HighReg) |
| 232 | MIB.addReg(Reg, RegState::ImplicitDefine); |
| 233 | } |
| 234 | |
Anton Korobeynikov | ba249e4 | 2009-07-16 13:50:21 +0000 | [diff] [blame] | 235 | return true; |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | unsigned |
| 239 | SystemZInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB, |
| 240 | MachineBasicBlock *FBB, |
| 241 | const SmallVectorImpl<MachineOperand> &Cond) const { |
Anton Korobeynikov | 64d52d4 | 2009-07-16 14:00:10 +0000 | [diff] [blame^] | 242 | // FIXME this should probably have a DebugLoc operand |
| 243 | DebugLoc dl = DebugLoc::getUnknownLoc(); |
| 244 | // Shouldn't be a fall through. |
| 245 | assert(TBB && "InsertBranch must not be told to insert a fallthrough"); |
| 246 | assert((Cond.size() == 1 || Cond.size() == 0) && |
| 247 | "SystemZ branch conditions have one component!"); |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 248 | |
Anton Korobeynikov | 64d52d4 | 2009-07-16 14:00:10 +0000 | [diff] [blame^] | 249 | if (Cond.empty()) { |
| 250 | // Unconditional branch? |
| 251 | assert(!FBB && "Unconditional branch with multiple successors!"); |
| 252 | BuildMI(&MBB, dl, get(SystemZ::JMP)).addMBB(TBB); |
| 253 | return 1; |
| 254 | } |
| 255 | |
| 256 | // Conditional branch. |
| 257 | unsigned Count = 0; |
| 258 | SystemZCC::CondCodes CC = (SystemZCC::CondCodes)Cond[0].getImm(); |
| 259 | BuildMI(&MBB, dl, getBrCond(CC)).addMBB(TBB); |
| 260 | ++Count; |
| 261 | |
| 262 | if (FBB) { |
| 263 | // Two-way Conditional branch. Insert the second branch. |
| 264 | BuildMI(&MBB, dl, get(SystemZ::JMP)).addMBB(FBB); |
| 265 | ++Count; |
| 266 | } |
| 267 | return Count; |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 268 | } |
Anton Korobeynikov | 7d1e39b | 2009-07-16 13:52:51 +0000 | [diff] [blame] | 269 | |
| 270 | const TargetInstrDesc& |
| 271 | SystemZInstrInfo::getBrCond(SystemZCC::CondCodes CC) const { |
| 272 | unsigned Opc; |
| 273 | switch (CC) { |
| 274 | default: |
| 275 | assert(0 && "Unknown condition code!"); |
| 276 | case SystemZCC::E: |
| 277 | Opc = SystemZ::JE; |
| 278 | break; |
| 279 | case SystemZCC::NE: |
| 280 | Opc = SystemZ::JNE; |
| 281 | break; |
| 282 | case SystemZCC::H: |
| 283 | Opc = SystemZ::JH; |
| 284 | break; |
| 285 | case SystemZCC::L: |
| 286 | Opc = SystemZ::JL; |
| 287 | break; |
| 288 | case SystemZCC::HE: |
| 289 | Opc = SystemZ::JHE; |
| 290 | break; |
| 291 | case SystemZCC::LE: |
| 292 | Opc = SystemZ::JLE; |
| 293 | break; |
| 294 | } |
| 295 | |
| 296 | return get(Opc); |
| 297 | } |