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" |
Anton Korobeynikov | 4b73016 | 2009-07-16 14:01:27 +0000 | [diff] [blame] | 15 | #include "SystemZInstrBuilder.h" |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 16 | #include "SystemZInstrInfo.h" |
| 17 | #include "SystemZMachineFunctionInfo.h" |
| 18 | #include "SystemZTargetMachine.h" |
| 19 | #include "SystemZGenInstrInfo.inc" |
| 20 | #include "llvm/Function.h" |
| 21 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 22 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 23 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 24 | #include "llvm/CodeGen/PseudoSourceValue.h" |
| 25 | |
| 26 | using namespace llvm; |
| 27 | |
| 28 | SystemZInstrInfo::SystemZInstrInfo(SystemZTargetMachine &tm) |
| 29 | : TargetInstrInfoImpl(SystemZInsts, array_lengthof(SystemZInsts)), |
Anton Korobeynikov | ef5deca | 2009-07-16 13:51:12 +0000 | [diff] [blame] | 30 | RI(tm, *this), TM(tm) { |
| 31 | // Fill the spill offsets map |
| 32 | static const unsigned SpillOffsTab[][2] = { |
| 33 | { SystemZ::R2D, 0x10 }, |
| 34 | { SystemZ::R3D, 0x18 }, |
| 35 | { SystemZ::R4D, 0x20 }, |
| 36 | { SystemZ::R5D, 0x28 }, |
| 37 | { SystemZ::R6D, 0x30 }, |
| 38 | { SystemZ::R7D, 0x38 }, |
| 39 | { SystemZ::R8D, 0x40 }, |
| 40 | { SystemZ::R9D, 0x48 }, |
| 41 | { SystemZ::R10D, 0x50 }, |
| 42 | { SystemZ::R11D, 0x58 }, |
| 43 | { SystemZ::R12D, 0x60 }, |
| 44 | { SystemZ::R13D, 0x68 }, |
| 45 | { SystemZ::R14D, 0x70 }, |
| 46 | { SystemZ::R15D, 0x78 } |
| 47 | }; |
| 48 | |
| 49 | RegSpillOffsets.grow(SystemZ::NUM_TARGET_REGS); |
| 50 | |
| 51 | for (unsigned i = 0, e = array_lengthof(SpillOffsTab); i != e; ++i) |
| 52 | RegSpillOffsets[SpillOffsTab[i][0]] = SpillOffsTab[i][1]; |
| 53 | } |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 54 | |
Anton Korobeynikov | f1106c4 | 2009-07-16 14:33:01 +0000 | [diff] [blame^] | 55 | /// isGVStub - Return true if the GV requires an extra load to get the |
| 56 | /// real address. |
| 57 | static inline bool isGVStub(GlobalValue *GV, SystemZTargetMachine &TM) { |
| 58 | return TM.getSubtarget<SystemZSubtarget>().GVRequiresExtraLoad(GV, TM, false); |
| 59 | } |
| 60 | |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 61 | void SystemZInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB, |
| 62 | MachineBasicBlock::iterator MI, |
| 63 | unsigned SrcReg, bool isKill, int FrameIdx, |
| 64 | const TargetRegisterClass *RC) const { |
Anton Korobeynikov | 4b73016 | 2009-07-16 14:01:27 +0000 | [diff] [blame] | 65 | DebugLoc DL = DebugLoc::getUnknownLoc(); |
| 66 | if (MI != MBB.end()) DL = MI->getDebugLoc(); |
| 67 | |
| 68 | unsigned Opc = 0; |
| 69 | if (RC == &SystemZ::GR32RegClass || |
| 70 | RC == &SystemZ::ADDR32RegClass) |
| 71 | Opc = SystemZ::MOV32mr; |
| 72 | else if (RC == &SystemZ::GR64RegClass || |
| 73 | RC == &SystemZ::ADDR64RegClass) { |
| 74 | Opc = SystemZ::MOV64mr; |
Anton Korobeynikov | 92ac82a | 2009-07-16 14:21:41 +0000 | [diff] [blame] | 75 | } else if (RC == &SystemZ::FP32RegClass) { |
| 76 | Opc = SystemZ::FMOV32mr; |
| 77 | } else if (RC == &SystemZ::FP64RegClass) { |
| 78 | Opc = SystemZ::FMOV64mr; |
Anton Korobeynikov | 4b73016 | 2009-07-16 14:01:27 +0000 | [diff] [blame] | 79 | } else |
| 80 | assert(0 && "Unsupported regclass to store"); |
| 81 | |
| 82 | addFrameReference(BuildMI(MBB, MI, DL, get(Opc)), FrameIdx) |
| 83 | .addReg(SrcReg, getKillRegState(isKill)); |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | void SystemZInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB, |
| 87 | MachineBasicBlock::iterator MI, |
| 88 | unsigned DestReg, int FrameIdx, |
| 89 | const TargetRegisterClass *RC) const{ |
Anton Korobeynikov | 4b73016 | 2009-07-16 14:01:27 +0000 | [diff] [blame] | 90 | DebugLoc DL = DebugLoc::getUnknownLoc(); |
| 91 | if (MI != MBB.end()) DL = MI->getDebugLoc(); |
| 92 | |
| 93 | unsigned Opc = 0; |
| 94 | if (RC == &SystemZ::GR32RegClass || |
| 95 | RC == &SystemZ::ADDR32RegClass) |
| 96 | Opc = SystemZ::MOV32rm; |
| 97 | else if (RC == &SystemZ::GR64RegClass || |
| 98 | RC == &SystemZ::ADDR64RegClass) { |
| 99 | Opc = SystemZ::MOV64rm; |
Anton Korobeynikov | 92ac82a | 2009-07-16 14:21:41 +0000 | [diff] [blame] | 100 | } else if (RC == &SystemZ::FP32RegClass) { |
| 101 | Opc = SystemZ::FMOV32rm; |
| 102 | } else if (RC == &SystemZ::FP64RegClass) { |
| 103 | Opc = SystemZ::FMOV64rm; |
Anton Korobeynikov | 4b73016 | 2009-07-16 14:01:27 +0000 | [diff] [blame] | 104 | } else |
| 105 | assert(0 && "Unsupported regclass to store"); |
| 106 | |
| 107 | addFrameReference(BuildMI(MBB, MI, DL, get(Opc), DestReg), FrameIdx); |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | bool SystemZInstrInfo::copyRegToReg(MachineBasicBlock &MBB, |
Anton Korobeynikov | 1cc9dc7 | 2009-07-16 13:29:38 +0000 | [diff] [blame] | 111 | MachineBasicBlock::iterator I, |
| 112 | unsigned DestReg, unsigned SrcReg, |
| 113 | const TargetRegisterClass *DestRC, |
| 114 | const TargetRegisterClass *SrcRC) const { |
| 115 | DebugLoc DL = DebugLoc::getUnknownLoc(); |
| 116 | if (I != MBB.end()) DL = I->getDebugLoc(); |
| 117 | |
Anton Korobeynikov | a51752c | 2009-07-16 13:42:31 +0000 | [diff] [blame] | 118 | // Determine if DstRC and SrcRC have a common superclass. |
| 119 | const TargetRegisterClass *CommonRC = DestRC; |
| 120 | if (DestRC == SrcRC) |
| 121 | /* Same regclass for source and dest */; |
| 122 | else if (CommonRC->hasSuperClass(SrcRC)) |
| 123 | CommonRC = SrcRC; |
| 124 | else if (!CommonRC->hasSubClass(SrcRC)) |
| 125 | CommonRC = 0; |
| 126 | |
| 127 | if (CommonRC) { |
Anton Korobeynikov | 9e4816e | 2009-07-16 13:43:18 +0000 | [diff] [blame] | 128 | if (CommonRC == &SystemZ::GR64RegClass || |
| 129 | CommonRC == &SystemZ::ADDR64RegClass) { |
Anton Korobeynikov | 8d1837d | 2009-07-16 13:56:42 +0000 | [diff] [blame] | 130 | BuildMI(MBB, I, DL, get(SystemZ::MOV64rr), DestReg).addReg(SrcReg); |
Anton Korobeynikov | 9e4816e | 2009-07-16 13:43:18 +0000 | [diff] [blame] | 131 | } else if (CommonRC == &SystemZ::GR32RegClass || |
| 132 | CommonRC == &SystemZ::ADDR32RegClass) { |
Anton Korobeynikov | 8d1837d | 2009-07-16 13:56:42 +0000 | [diff] [blame] | 133 | BuildMI(MBB, I, DL, get(SystemZ::MOV32rr), DestReg).addReg(SrcReg); |
| 134 | } else if (CommonRC == &SystemZ::GR64PRegClass) { |
| 135 | BuildMI(MBB, I, DL, get(SystemZ::MOV64rrP), DestReg).addReg(SrcReg); |
| 136 | } else if (CommonRC == &SystemZ::GR128RegClass) { |
| 137 | BuildMI(MBB, I, DL, get(SystemZ::MOV128rr), DestReg).addReg(SrcReg); |
Anton Korobeynikov | 7aa03ac | 2009-07-16 14:20:24 +0000 | [diff] [blame] | 138 | } else if (CommonRC == &SystemZ::FP32RegClass) { |
| 139 | BuildMI(MBB, I, DL, get(SystemZ::FMOV32rr), DestReg).addReg(SrcReg); |
| 140 | } else if (CommonRC == &SystemZ::FP64RegClass) { |
| 141 | BuildMI(MBB, I, DL, get(SystemZ::FMOV64rr), DestReg).addReg(SrcReg); |
Anton Korobeynikov | 1cc9dc7 | 2009-07-16 13:29:38 +0000 | [diff] [blame] | 142 | } else { |
| 143 | return false; |
| 144 | } |
| 145 | |
Anton Korobeynikov | 1cc9dc7 | 2009-07-16 13:29:38 +0000 | [diff] [blame] | 146 | return true; |
| 147 | } |
| 148 | |
Anton Korobeynikov | 9e4816e | 2009-07-16 13:43:18 +0000 | [diff] [blame] | 149 | if ((SrcRC == &SystemZ::GR64RegClass && |
| 150 | DestRC == &SystemZ::ADDR64RegClass) || |
| 151 | (DestRC == &SystemZ::GR64RegClass && |
| 152 | SrcRC == &SystemZ::ADDR64RegClass)) { |
| 153 | BuildMI(MBB, I, DL, get(SystemZ::MOV64rr), DestReg).addReg(SrcReg); |
| 154 | return true; |
| 155 | } else if ((SrcRC == &SystemZ::GR32RegClass && |
| 156 | DestRC == &SystemZ::ADDR32RegClass) || |
| 157 | (DestRC == &SystemZ::GR32RegClass && |
| 158 | SrcRC == &SystemZ::ADDR32RegClass)) { |
| 159 | BuildMI(MBB, I, DL, get(SystemZ::MOV32rr), DestReg).addReg(SrcReg); |
| 160 | return true; |
| 161 | } |
| 162 | |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 163 | return false; |
| 164 | } |
| 165 | |
| 166 | bool |
| 167 | SystemZInstrInfo::isMoveInstr(const MachineInstr& MI, |
Anton Korobeynikov | 1cc9dc7 | 2009-07-16 13:29:38 +0000 | [diff] [blame] | 168 | unsigned &SrcReg, unsigned &DstReg, |
| 169 | unsigned &SrcSubIdx, unsigned &DstSubIdx) const { |
Anton Korobeynikov | 1cc9dc7 | 2009-07-16 13:29:38 +0000 | [diff] [blame] | 170 | switch (MI.getOpcode()) { |
| 171 | default: |
| 172 | return false; |
Anton Korobeynikov | a51752c | 2009-07-16 13:42:31 +0000 | [diff] [blame] | 173 | case SystemZ::MOV32rr: |
Anton Korobeynikov | 1cc9dc7 | 2009-07-16 13:29:38 +0000 | [diff] [blame] | 174 | case SystemZ::MOV64rr: |
Anton Korobeynikov | 8d1837d | 2009-07-16 13:56:42 +0000 | [diff] [blame] | 175 | case SystemZ::MOV64rrP: |
| 176 | case SystemZ::MOV128rr: |
Anton Korobeynikov | 7aa03ac | 2009-07-16 14:20:24 +0000 | [diff] [blame] | 177 | case SystemZ::FMOV32rr: |
| 178 | case SystemZ::FMOV64rr: |
Anton Korobeynikov | 1cc9dc7 | 2009-07-16 13:29:38 +0000 | [diff] [blame] | 179 | assert(MI.getNumOperands() >= 2 && |
| 180 | MI.getOperand(0).isReg() && |
| 181 | MI.getOperand(1).isReg() && |
| 182 | "invalid register-register move instruction"); |
| 183 | SrcReg = MI.getOperand(1).getReg(); |
| 184 | DstReg = MI.getOperand(0).getReg(); |
Anton Korobeynikov | 54cea74 | 2009-07-16 14:12:54 +0000 | [diff] [blame] | 185 | SrcSubIdx = MI.getOperand(1).getSubReg(); |
| 186 | DstSubIdx = MI.getOperand(0).getSubReg(); |
Anton Korobeynikov | 1cc9dc7 | 2009-07-16 13:29:38 +0000 | [diff] [blame] | 187 | return true; |
| 188 | } |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Anton Korobeynikov | 27bf677 | 2009-07-16 14:32:41 +0000 | [diff] [blame] | 191 | unsigned SystemZInstrInfo::isLoadFromStackSlot(const MachineInstr *MI, |
| 192 | int &FrameIndex) const { |
| 193 | switch (MI->getOpcode()) { |
| 194 | default: break; |
| 195 | case SystemZ::MOV32rm: |
| 196 | case SystemZ::MOV32rmy: |
| 197 | case SystemZ::MOV64rm: |
| 198 | case SystemZ::MOVSX32rm8: |
| 199 | case SystemZ::MOVSX32rm16y: |
| 200 | case SystemZ::MOVSX64rm8: |
| 201 | case SystemZ::MOVSX64rm16: |
| 202 | case SystemZ::MOVSX64rm32: |
| 203 | case SystemZ::MOVZX32rm8: |
| 204 | case SystemZ::MOVZX32rm16: |
| 205 | case SystemZ::MOVZX64rm8: |
| 206 | case SystemZ::MOVZX64rm16: |
| 207 | case SystemZ::MOVZX64rm32: |
| 208 | case SystemZ::FMOV32rm: |
| 209 | case SystemZ::FMOV32rmy: |
| 210 | case SystemZ::FMOV64rm: |
| 211 | case SystemZ::FMOV64rmy: |
| 212 | if (MI->getOperand(1).isFI() && |
| 213 | MI->getOperand(2).isImm() && MI->getOperand(3).isReg() && |
| 214 | MI->getOperand(2).getImm() == 0 && MI->getOperand(3).getReg() == 0) { |
| 215 | FrameIndex = MI->getOperand(1).getIndex(); |
| 216 | return MI->getOperand(0).getReg(); |
| 217 | } |
| 218 | break; |
| 219 | } |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | unsigned SystemZInstrInfo::isStoreToStackSlot(const MachineInstr *MI, |
| 224 | int &FrameIndex) const { |
| 225 | switch (MI->getOpcode()) { |
| 226 | default: break; |
| 227 | case SystemZ::MOV32mr: |
| 228 | case SystemZ::MOV32mry: |
| 229 | case SystemZ::MOV64mr: |
| 230 | case SystemZ::MOV32m8r: |
| 231 | case SystemZ::MOV32m8ry: |
| 232 | case SystemZ::MOV32m16r: |
| 233 | case SystemZ::MOV32m16ry: |
| 234 | case SystemZ::MOV64m8r: |
| 235 | case SystemZ::MOV64m8ry: |
| 236 | case SystemZ::MOV64m16r: |
| 237 | case SystemZ::MOV64m16ry: |
| 238 | case SystemZ::MOV64m32r: |
| 239 | case SystemZ::MOV64m32ry: |
| 240 | case SystemZ::FMOV32mr: |
| 241 | case SystemZ::FMOV32mry: |
| 242 | case SystemZ::FMOV64mr: |
| 243 | case SystemZ::FMOV64mry: |
| 244 | if (MI->getOperand(0).isFI() && |
| 245 | MI->getOperand(1).isImm() && MI->getOperand(2).isReg() && |
| 246 | MI->getOperand(1).getImm() == 0 && MI->getOperand(2).getReg() == 0) { |
| 247 | FrameIndex = MI->getOperand(0).getIndex(); |
| 248 | return MI->getOperand(3).getReg(); |
| 249 | } |
| 250 | break; |
| 251 | } |
| 252 | return 0; |
| 253 | } |
| 254 | |
Anton Korobeynikov | f1106c4 | 2009-07-16 14:33:01 +0000 | [diff] [blame^] | 255 | bool SystemZInstrInfo::isInvariantLoad(const MachineInstr *MI) const { |
| 256 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 257 | const MachineOperand &MO = MI->getOperand(i); |
| 258 | // Loads from constant pools are trivially invariant. |
| 259 | if (MO.isCPI()) |
| 260 | return true; |
| 261 | |
| 262 | if (MO.isGlobal()) |
| 263 | return isGVStub(MO.getGlobal(), TM); |
| 264 | |
| 265 | // If this is a load from an invariant stack slot, the load is a constant. |
| 266 | if (MO.isFI()) { |
| 267 | const MachineFrameInfo &MFI = |
| 268 | *MI->getParent()->getParent()->getFrameInfo(); |
| 269 | int Idx = MO.getIndex(); |
| 270 | return MFI.isFixedObjectIndex(Idx) && MFI.isImmutableObjectIndex(Idx); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | // All other instances of these instructions are presumed to have other |
| 275 | // issues. |
| 276 | return false; |
| 277 | } |
| 278 | |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 279 | bool |
| 280 | SystemZInstrInfo::spillCalleeSavedRegisters(MachineBasicBlock &MBB, |
| 281 | MachineBasicBlock::iterator MI, |
| 282 | const std::vector<CalleeSavedInfo> &CSI) const { |
Anton Korobeynikov | 1733124 | 2009-07-16 14:23:01 +0000 | [diff] [blame] | 283 | if (CSI.empty()) |
| 284 | return false; |
| 285 | |
Anton Korobeynikov | ef5deca | 2009-07-16 13:51:12 +0000 | [diff] [blame] | 286 | DebugLoc DL = DebugLoc::getUnknownLoc(); |
| 287 | if (MI != MBB.end()) DL = MI->getDebugLoc(); |
Anton Korobeynikov | ba249e4 | 2009-07-16 13:50:21 +0000 | [diff] [blame] | 288 | |
| 289 | MachineFunction &MF = *MBB.getParent(); |
| 290 | SystemZMachineFunctionInfo *MFI = MF.getInfo<SystemZMachineFunctionInfo>(); |
Anton Korobeynikov | 1733124 | 2009-07-16 14:23:01 +0000 | [diff] [blame] | 291 | unsigned CalleeFrameSize = 0; |
Anton Korobeynikov | ba249e4 | 2009-07-16 13:50:21 +0000 | [diff] [blame] | 292 | |
Anton Korobeynikov | ef5deca | 2009-07-16 13:51:12 +0000 | [diff] [blame] | 293 | // Scan the callee-saved and find the bounds of register spill area. |
| 294 | unsigned LowReg = 0, HighReg = 0, StartOffset = -1U, EndOffset = 0; |
| 295 | for (unsigned i = 0, e = CSI.size(); i != e; ++i) { |
| 296 | unsigned Reg = CSI[i].getReg(); |
Anton Korobeynikov | 1733124 | 2009-07-16 14:23:01 +0000 | [diff] [blame] | 297 | const TargetRegisterClass *RegClass = CSI[i].getRegClass(); |
| 298 | if (RegClass != &SystemZ::FP64RegClass) { |
| 299 | unsigned Offset = RegSpillOffsets[Reg]; |
| 300 | CalleeFrameSize += 8; |
| 301 | if (StartOffset > Offset) { |
| 302 | LowReg = Reg; StartOffset = Offset; |
| 303 | } |
| 304 | if (EndOffset < Offset) { |
| 305 | HighReg = Reg; EndOffset = RegSpillOffsets[Reg]; |
| 306 | } |
Anton Korobeynikov | ef5deca | 2009-07-16 13:51:12 +0000 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | |
| 310 | // Save information for epilogue inserter. |
Anton Korobeynikov | 1733124 | 2009-07-16 14:23:01 +0000 | [diff] [blame] | 311 | MFI->setCalleeSavedFrameSize(CalleeFrameSize); |
Anton Korobeynikov | ef5deca | 2009-07-16 13:51:12 +0000 | [diff] [blame] | 312 | MFI->setLowReg(LowReg); MFI->setHighReg(HighReg); |
| 313 | |
Anton Korobeynikov | 1733124 | 2009-07-16 14:23:01 +0000 | [diff] [blame] | 314 | // Save GPRs |
| 315 | if (StartOffset) { |
| 316 | // Build a store instruction. Use STORE MULTIPLE instruction if there are many |
| 317 | // registers to store, otherwise - just STORE. |
| 318 | MachineInstrBuilder MIB = |
| 319 | BuildMI(MBB, MI, DL, get((LowReg == HighReg ? |
| 320 | SystemZ::MOV64mr : SystemZ::MOV64mrm))); |
Anton Korobeynikov | ef5deca | 2009-07-16 13:51:12 +0000 | [diff] [blame] | 321 | |
Anton Korobeynikov | 1733124 | 2009-07-16 14:23:01 +0000 | [diff] [blame] | 322 | // Add store operands. |
| 323 | MIB.addReg(SystemZ::R15D).addImm(StartOffset); |
| 324 | if (LowReg == HighReg) |
| 325 | MIB.addReg(0); |
| 326 | MIB.addReg(LowReg, RegState::Kill); |
| 327 | if (LowReg != HighReg) |
| 328 | MIB.addReg(HighReg, RegState::Kill); |
Anton Korobeynikov | ef5deca | 2009-07-16 13:51:12 +0000 | [diff] [blame] | 329 | |
Anton Korobeynikov | 1733124 | 2009-07-16 14:23:01 +0000 | [diff] [blame] | 330 | // Do a second scan adding regs as being killed by instruction |
| 331 | for (unsigned i = 0, e = CSI.size(); i != e; ++i) { |
| 332 | unsigned Reg = CSI[i].getReg(); |
| 333 | // Add the callee-saved register as live-in. It's killed at the spill. |
| 334 | MBB.addLiveIn(Reg); |
| 335 | if (Reg != LowReg && Reg != HighReg) |
| 336 | MIB.addReg(Reg, RegState::ImplicitKill); |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | // Save FPRs |
Anton Korobeynikov | ef5deca | 2009-07-16 13:51:12 +0000 | [diff] [blame] | 341 | for (unsigned i = 0, e = CSI.size(); i != e; ++i) { |
| 342 | unsigned Reg = CSI[i].getReg(); |
Anton Korobeynikov | 1733124 | 2009-07-16 14:23:01 +0000 | [diff] [blame] | 343 | const TargetRegisterClass *RegClass = CSI[i].getRegClass(); |
| 344 | if (RegClass == &SystemZ::FP64RegClass) { |
| 345 | MBB.addLiveIn(Reg); |
| 346 | storeRegToStackSlot(MBB, MI, Reg, true, CSI[i].getFrameIdx(), RegClass); |
| 347 | } |
Anton Korobeynikov | ef5deca | 2009-07-16 13:51:12 +0000 | [diff] [blame] | 348 | } |
| 349 | |
Anton Korobeynikov | ba249e4 | 2009-07-16 13:50:21 +0000 | [diff] [blame] | 350 | return true; |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | bool |
| 354 | SystemZInstrInfo::restoreCalleeSavedRegisters(MachineBasicBlock &MBB, |
| 355 | MachineBasicBlock::iterator MI, |
| 356 | const std::vector<CalleeSavedInfo> &CSI) const { |
Anton Korobeynikov | ef5deca | 2009-07-16 13:51:12 +0000 | [diff] [blame] | 357 | if (CSI.empty()) |
| 358 | return false; |
| 359 | |
| 360 | DebugLoc DL = DebugLoc::getUnknownLoc(); |
| 361 | if (MI != MBB.end()) DL = MI->getDebugLoc(); |
| 362 | |
| 363 | MachineFunction &MF = *MBB.getParent(); |
| 364 | const TargetRegisterInfo *RegInfo= MF.getTarget().getRegisterInfo(); |
| 365 | SystemZMachineFunctionInfo *MFI = MF.getInfo<SystemZMachineFunctionInfo>(); |
| 366 | |
Anton Korobeynikov | 1733124 | 2009-07-16 14:23:01 +0000 | [diff] [blame] | 367 | // Restore FP registers |
| 368 | for (unsigned i = 0, e = CSI.size(); i != e; ++i) { |
| 369 | unsigned Reg = CSI[i].getReg(); |
| 370 | const TargetRegisterClass *RegClass = CSI[i].getRegClass(); |
| 371 | if (RegClass == &SystemZ::FP64RegClass) |
| 372 | loadRegFromStackSlot(MBB, MI, Reg, CSI[i].getFrameIdx(), RegClass); |
| 373 | } |
| 374 | |
| 375 | // Restore GP registers |
Anton Korobeynikov | ef5deca | 2009-07-16 13:51:12 +0000 | [diff] [blame] | 376 | unsigned LowReg = MFI->getLowReg(), HighReg = MFI->getHighReg(); |
| 377 | unsigned StartOffset = RegSpillOffsets[LowReg]; |
| 378 | |
Anton Korobeynikov | 1733124 | 2009-07-16 14:23:01 +0000 | [diff] [blame] | 379 | if (StartOffset) { |
| 380 | // Build a load instruction. Use LOAD MULTIPLE instruction if there are many |
| 381 | // registers to load, otherwise - just LOAD. |
| 382 | MachineInstrBuilder MIB = |
| 383 | BuildMI(MBB, MI, DL, get((LowReg == HighReg ? |
| 384 | SystemZ::MOV64rm : SystemZ::MOV64rmm))); |
| 385 | // Add store operands. |
| 386 | MIB.addReg(LowReg, RegState::Define); |
| 387 | if (LowReg != HighReg) |
| 388 | MIB.addReg(HighReg, RegState::Define); |
Anton Korobeynikov | ef5deca | 2009-07-16 13:51:12 +0000 | [diff] [blame] | 389 | |
Anton Korobeynikov | 1733124 | 2009-07-16 14:23:01 +0000 | [diff] [blame] | 390 | MIB.addReg((RegInfo->hasFP(MF) ? SystemZ::R11D : SystemZ::R15D)); |
| 391 | MIB.addImm(StartOffset); |
| 392 | if (LowReg == HighReg) |
| 393 | MIB.addReg(0); |
Anton Korobeynikov | ef5deca | 2009-07-16 13:51:12 +0000 | [diff] [blame] | 394 | |
Anton Korobeynikov | 1733124 | 2009-07-16 14:23:01 +0000 | [diff] [blame] | 395 | // Do a second scan adding regs as being defined by instruction |
| 396 | for (unsigned i = 0, e = CSI.size(); i != e; ++i) { |
| 397 | unsigned Reg = CSI[i].getReg(); |
| 398 | if (Reg != LowReg && Reg != HighReg) |
| 399 | MIB.addReg(Reg, RegState::ImplicitDefine); |
| 400 | } |
Anton Korobeynikov | ef5deca | 2009-07-16 13:51:12 +0000 | [diff] [blame] | 401 | } |
| 402 | |
Anton Korobeynikov | ba249e4 | 2009-07-16 13:50:21 +0000 | [diff] [blame] | 403 | return true; |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 404 | } |
| 405 | |
Anton Korobeynikov | ae46db8 | 2009-07-16 14:32:19 +0000 | [diff] [blame] | 406 | bool SystemZInstrInfo:: |
| 407 | ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const { |
| 408 | assert(Cond.size() == 1 && "Invalid Xbranch condition!"); |
| 409 | |
| 410 | SystemZCC::CondCodes CC = static_cast<SystemZCC::CondCodes>(Cond[0].getImm()); |
| 411 | Cond[0].setImm(getOppositeCondition(CC)); |
| 412 | return false; |
| 413 | } |
| 414 | |
| 415 | bool SystemZInstrInfo::BlockHasNoFallThrough(const MachineBasicBlock &MBB)const{ |
| 416 | if (MBB.empty()) return false; |
| 417 | |
| 418 | switch (MBB.back().getOpcode()) { |
| 419 | case SystemZ::RET: // Return. |
| 420 | case SystemZ::JMP: // Uncond branch. |
| 421 | case SystemZ::JMPr: // Indirect branch. |
| 422 | return true; |
| 423 | default: return false; |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | bool SystemZInstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const { |
| 428 | const TargetInstrDesc &TID = MI->getDesc(); |
| 429 | if (!TID.isTerminator()) return false; |
| 430 | |
| 431 | // Conditional branch is a special case. |
| 432 | if (TID.isBranch() && !TID.isBarrier()) |
| 433 | return true; |
| 434 | if (!TID.isPredicable()) |
| 435 | return true; |
| 436 | return !isPredicated(MI); |
| 437 | } |
| 438 | |
| 439 | bool SystemZInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB, |
| 440 | MachineBasicBlock *&TBB, |
| 441 | MachineBasicBlock *&FBB, |
| 442 | SmallVectorImpl<MachineOperand> &Cond, |
| 443 | bool AllowModify) const { |
| 444 | // Start from the bottom of the block and work up, examining the |
| 445 | // terminator instructions. |
| 446 | MachineBasicBlock::iterator I = MBB.end(); |
| 447 | while (I != MBB.begin()) { |
| 448 | --I; |
| 449 | // Working from the bottom, when we see a non-terminator |
| 450 | // instruction, we're done. |
| 451 | if (!isUnpredicatedTerminator(I)) |
| 452 | break; |
| 453 | |
| 454 | // A terminator that isn't a branch can't easily be handled |
| 455 | // by this analysis. |
| 456 | if (!I->getDesc().isBranch()) |
| 457 | return true; |
| 458 | |
| 459 | // Handle unconditional branches. |
| 460 | if (I->getOpcode() == SystemZ::JMP) { |
| 461 | if (!AllowModify) { |
| 462 | TBB = I->getOperand(0).getMBB(); |
| 463 | continue; |
| 464 | } |
| 465 | |
| 466 | // If the block has any instructions after a JMP, delete them. |
| 467 | while (next(I) != MBB.end()) |
| 468 | next(I)->eraseFromParent(); |
| 469 | Cond.clear(); |
| 470 | FBB = 0; |
| 471 | |
| 472 | // Delete the JMP if it's equivalent to a fall-through. |
| 473 | if (MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) { |
| 474 | TBB = 0; |
| 475 | I->eraseFromParent(); |
| 476 | I = MBB.end(); |
| 477 | continue; |
| 478 | } |
| 479 | |
| 480 | // TBB is used to indicate the unconditinal destination. |
| 481 | TBB = I->getOperand(0).getMBB(); |
| 482 | continue; |
| 483 | } |
| 484 | |
| 485 | // Handle conditional branches. |
| 486 | SystemZCC::CondCodes BranchCode = getCondFromBranchOpc(I->getOpcode()); |
| 487 | if (BranchCode == SystemZCC::INVALID) |
| 488 | return true; // Can't handle indirect branch. |
| 489 | |
| 490 | // Working from the bottom, handle the first conditional branch. |
| 491 | if (Cond.empty()) { |
| 492 | FBB = TBB; |
| 493 | TBB = I->getOperand(0).getMBB(); |
| 494 | Cond.push_back(MachineOperand::CreateImm(BranchCode)); |
| 495 | continue; |
| 496 | } |
| 497 | |
| 498 | // Handle subsequent conditional branches. Only handle the case where all |
| 499 | // conditional branches branch to the same destination. |
| 500 | assert(Cond.size() == 1); |
| 501 | assert(TBB); |
| 502 | |
| 503 | // Only handle the case where all conditional branches branch to |
| 504 | // the same destination. |
| 505 | if (TBB != I->getOperand(0).getMBB()) |
| 506 | return true; |
| 507 | |
| 508 | SystemZCC::CondCodes OldBranchCode = (SystemZCC::CondCodes)Cond[0].getImm(); |
| 509 | // If the conditions are the same, we can leave them alone. |
| 510 | if (OldBranchCode == BranchCode) |
| 511 | continue; |
| 512 | |
| 513 | return true; |
| 514 | } |
| 515 | |
| 516 | return false; |
| 517 | } |
| 518 | |
| 519 | unsigned SystemZInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const { |
| 520 | MachineBasicBlock::iterator I = MBB.end(); |
| 521 | unsigned Count = 0; |
| 522 | |
| 523 | while (I != MBB.begin()) { |
| 524 | --I; |
| 525 | if (I->getOpcode() != SystemZ::JMP && |
| 526 | getCondFromBranchOpc(I->getOpcode()) == SystemZCC::INVALID) |
| 527 | break; |
| 528 | // Remove the branch. |
| 529 | I->eraseFromParent(); |
| 530 | I = MBB.end(); |
| 531 | ++Count; |
| 532 | } |
| 533 | |
| 534 | return Count; |
| 535 | } |
| 536 | |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 537 | unsigned |
| 538 | SystemZInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB, |
Anton Korobeynikov | 9b812b0 | 2009-07-16 14:16:26 +0000 | [diff] [blame] | 539 | MachineBasicBlock *FBB, |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 540 | const SmallVectorImpl<MachineOperand> &Cond) const { |
Anton Korobeynikov | 9b812b0 | 2009-07-16 14:16:26 +0000 | [diff] [blame] | 541 | // FIXME: this should probably have a DebugLoc operand |
Anton Korobeynikov | 64d52d4 | 2009-07-16 14:00:10 +0000 | [diff] [blame] | 542 | DebugLoc dl = DebugLoc::getUnknownLoc(); |
| 543 | // Shouldn't be a fall through. |
| 544 | assert(TBB && "InsertBranch must not be told to insert a fallthrough"); |
| 545 | assert((Cond.size() == 1 || Cond.size() == 0) && |
| 546 | "SystemZ branch conditions have one component!"); |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 547 | |
Anton Korobeynikov | 64d52d4 | 2009-07-16 14:00:10 +0000 | [diff] [blame] | 548 | if (Cond.empty()) { |
| 549 | // Unconditional branch? |
| 550 | assert(!FBB && "Unconditional branch with multiple successors!"); |
| 551 | BuildMI(&MBB, dl, get(SystemZ::JMP)).addMBB(TBB); |
| 552 | return 1; |
| 553 | } |
| 554 | |
| 555 | // Conditional branch. |
| 556 | unsigned Count = 0; |
| 557 | SystemZCC::CondCodes CC = (SystemZCC::CondCodes)Cond[0].getImm(); |
| 558 | BuildMI(&MBB, dl, getBrCond(CC)).addMBB(TBB); |
| 559 | ++Count; |
| 560 | |
| 561 | if (FBB) { |
| 562 | // Two-way Conditional branch. Insert the second branch. |
| 563 | BuildMI(&MBB, dl, get(SystemZ::JMP)).addMBB(FBB); |
| 564 | ++Count; |
| 565 | } |
| 566 | return Count; |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 567 | } |
Anton Korobeynikov | 7d1e39b | 2009-07-16 13:52:51 +0000 | [diff] [blame] | 568 | |
| 569 | const TargetInstrDesc& |
| 570 | SystemZInstrInfo::getBrCond(SystemZCC::CondCodes CC) const { |
Anton Korobeynikov | 7d1e39b | 2009-07-16 13:52:51 +0000 | [diff] [blame] | 571 | switch (CC) { |
| 572 | default: |
| 573 | assert(0 && "Unknown condition code!"); |
Anton Korobeynikov | c3e48b0 | 2009-07-16 14:31:32 +0000 | [diff] [blame] | 574 | case SystemZCC::O: return get(SystemZ::JO); |
| 575 | case SystemZCC::H: return get(SystemZ::JH); |
| 576 | case SystemZCC::NLE: return get(SystemZ::JNLE); |
| 577 | case SystemZCC::L: return get(SystemZ::JL); |
| 578 | case SystemZCC::NHE: return get(SystemZ::JNHE); |
| 579 | case SystemZCC::LH: return get(SystemZ::JLH); |
| 580 | case SystemZCC::NE: return get(SystemZ::JNE); |
| 581 | case SystemZCC::E: return get(SystemZ::JE); |
| 582 | case SystemZCC::NLH: return get(SystemZ::JNLH); |
| 583 | case SystemZCC::HE: return get(SystemZ::JHE); |
| 584 | case SystemZCC::NL: return get(SystemZ::JNL); |
| 585 | case SystemZCC::LE: return get(SystemZ::JLE); |
| 586 | case SystemZCC::NH: return get(SystemZ::JNH); |
| 587 | case SystemZCC::NO: return get(SystemZ::JNO); |
Anton Korobeynikov | 7d1e39b | 2009-07-16 13:52:51 +0000 | [diff] [blame] | 588 | } |
Anton Korobeynikov | 7d1e39b | 2009-07-16 13:52:51 +0000 | [diff] [blame] | 589 | } |
Anton Korobeynikov | 5a11e02 | 2009-07-16 14:09:56 +0000 | [diff] [blame] | 590 | |
Anton Korobeynikov | ae46db8 | 2009-07-16 14:32:19 +0000 | [diff] [blame] | 591 | SystemZCC::CondCodes |
| 592 | SystemZInstrInfo::getCondFromBranchOpc(unsigned Opc) const { |
| 593 | switch (Opc) { |
| 594 | default: return SystemZCC::INVALID; |
| 595 | case SystemZ::JO: return SystemZCC::O; |
| 596 | case SystemZ::JH: return SystemZCC::H; |
| 597 | case SystemZ::JNLE: return SystemZCC::NLE; |
| 598 | case SystemZ::JL: return SystemZCC::L; |
| 599 | case SystemZ::JNHE: return SystemZCC::NHE; |
| 600 | case SystemZ::JLH: return SystemZCC::LH; |
| 601 | case SystemZ::JNE: return SystemZCC::NE; |
| 602 | case SystemZ::JE: return SystemZCC::E; |
| 603 | case SystemZ::JNLH: return SystemZCC::NLH; |
| 604 | case SystemZ::JHE: return SystemZCC::HE; |
| 605 | case SystemZ::JNL: return SystemZCC::NL; |
| 606 | case SystemZ::JLE: return SystemZCC::LE; |
| 607 | case SystemZ::JNH: return SystemZCC::NH; |
| 608 | case SystemZ::JNO: return SystemZCC::NO; |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | SystemZCC::CondCodes |
| 613 | SystemZInstrInfo::getOppositeCondition(SystemZCC::CondCodes CC) const { |
| 614 | switch (CC) { |
| 615 | default: |
| 616 | assert(0 && "Invalid condition!"); |
| 617 | case SystemZCC::O: return SystemZCC::NO; |
| 618 | case SystemZCC::H: return SystemZCC::NH; |
| 619 | case SystemZCC::NLE: return SystemZCC::LE; |
| 620 | case SystemZCC::L: return SystemZCC::NL; |
| 621 | case SystemZCC::NHE: return SystemZCC::HE; |
| 622 | case SystemZCC::LH: return SystemZCC::NLH; |
| 623 | case SystemZCC::NE: return SystemZCC::E; |
| 624 | case SystemZCC::E: return SystemZCC::NE; |
| 625 | case SystemZCC::NLH: return SystemZCC::LH; |
| 626 | case SystemZCC::HE: return SystemZCC::NHE; |
| 627 | case SystemZCC::NL: return SystemZCC::L; |
| 628 | case SystemZCC::LE: return SystemZCC::NLE; |
| 629 | case SystemZCC::NH: return SystemZCC::H; |
| 630 | case SystemZCC::NO: return SystemZCC::O; |
| 631 | } |
| 632 | } |
| 633 | |
Anton Korobeynikov | 5a11e02 | 2009-07-16 14:09:56 +0000 | [diff] [blame] | 634 | const TargetInstrDesc& |
| 635 | SystemZInstrInfo::getLongDispOpc(unsigned Opc) const { |
| 636 | switch (Opc) { |
Anton Korobeynikov | c3e48b0 | 2009-07-16 14:31:32 +0000 | [diff] [blame] | 637 | case SystemZ::MOV32mr: return get(SystemZ::MOV32mry); |
| 638 | case SystemZ::MOV32rm: return get(SystemZ::MOV32rmy); |
| 639 | case SystemZ::MOVSX32rm16: return get(SystemZ::MOVSX32rm16y); |
| 640 | case SystemZ::MOV32m8r: return get(SystemZ::MOV32m8ry); |
| 641 | case SystemZ::MOV32m16r: return get(SystemZ::MOV32m16ry); |
| 642 | case SystemZ::MOV64m8r: return get(SystemZ::MOV64m8ry); |
| 643 | case SystemZ::MOV64m16r: return get(SystemZ::MOV64m16ry); |
| 644 | case SystemZ::MOV64m32r: return get(SystemZ::MOV64m32ry); |
| 645 | case SystemZ::MOV8mi: return get(SystemZ::MOV8miy); |
| 646 | case SystemZ::MUL32rm: return get(SystemZ::MUL32rmy); |
| 647 | case SystemZ::CMP32rm: return get(SystemZ::CMP32rmy); |
| 648 | case SystemZ::UCMP32rm: return get(SystemZ::UCMP32rmy); |
| 649 | case SystemZ::FMOV32mr: return get(SystemZ::FMOV32mry); |
| 650 | case SystemZ::FMOV64mr: return get(SystemZ::FMOV64mry); |
Anton Korobeynikov | 27766b5 | 2009-07-16 14:31:52 +0000 | [diff] [blame] | 651 | case SystemZ::FMOV32rm: return get(SystemZ::FMOV32rmy); |
| 652 | case SystemZ::FMOV64rm: return get(SystemZ::FMOV64rmy); |
Anton Korobeynikov | c3e48b0 | 2009-07-16 14:31:32 +0000 | [diff] [blame] | 653 | default: return get(Opc); |
Anton Korobeynikov | 5a11e02 | 2009-07-16 14:09:56 +0000 | [diff] [blame] | 654 | } |
Anton Korobeynikov | 5a11e02 | 2009-07-16 14:09:56 +0000 | [diff] [blame] | 655 | } |
| 656 | |