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