Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 1 | //===-- SystemZISelLowering.cpp - SystemZ DAG Lowering Implementation -----==// |
| 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 implements the SystemZTargetLowering class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #define DEBUG_TYPE "systemz-lower" |
| 15 | |
| 16 | #include "SystemZISelLowering.h" |
| 17 | #include "SystemZ.h" |
| 18 | #include "SystemZTargetMachine.h" |
| 19 | #include "SystemZSubtarget.h" |
| 20 | #include "llvm/DerivedTypes.h" |
| 21 | #include "llvm/Function.h" |
| 22 | #include "llvm/Intrinsics.h" |
| 23 | #include "llvm/CallingConv.h" |
| 24 | #include "llvm/GlobalVariable.h" |
| 25 | #include "llvm/GlobalAlias.h" |
| 26 | #include "llvm/CodeGen/CallingConvLower.h" |
| 27 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 28 | #include "llvm/CodeGen/MachineFunction.h" |
| 29 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 30 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 31 | #include "llvm/CodeGen/PseudoSourceValue.h" |
| 32 | #include "llvm/CodeGen/SelectionDAGISel.h" |
| 33 | #include "llvm/CodeGen/ValueTypes.h" |
| 34 | #include "llvm/Support/Debug.h" |
Anton Korobeynikov | 2c97ae8 | 2009-07-16 14:19:02 +0000 | [diff] [blame] | 35 | #include "llvm/Target/TargetOptions.h" |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/VectorExtras.h" |
| 37 | using namespace llvm; |
| 38 | |
| 39 | SystemZTargetLowering::SystemZTargetLowering(SystemZTargetMachine &tm) : |
| 40 | TargetLowering(tm), Subtarget(*tm.getSubtargetImpl()), TM(tm) { |
| 41 | |
Anton Korobeynikov | 656ac6f | 2009-07-16 13:51:53 +0000 | [diff] [blame] | 42 | RegInfo = TM.getRegisterInfo(); |
| 43 | |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 44 | // Set up the register classes. |
Anton Korobeynikov | 8d1837d | 2009-07-16 13:56:42 +0000 | [diff] [blame] | 45 | addRegisterClass(MVT::i32, SystemZ::GR32RegisterClass); |
| 46 | addRegisterClass(MVT::i64, SystemZ::GR64RegisterClass); |
Anton Korobeynikov | 0a42d2b | 2009-07-16 14:14:33 +0000 | [diff] [blame] | 47 | addRegisterClass(MVT::v2i32,SystemZ::GR64PRegisterClass); |
Anton Korobeynikov | 0a42d2b | 2009-07-16 14:14:33 +0000 | [diff] [blame] | 48 | addRegisterClass(MVT::v2i64,SystemZ::GR128RegisterClass); |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 49 | |
Anton Korobeynikov | 2c97ae8 | 2009-07-16 14:19:02 +0000 | [diff] [blame] | 50 | if (!UseSoftFloat) { |
| 51 | addRegisterClass(MVT::f32, SystemZ::FP32RegisterClass); |
| 52 | addRegisterClass(MVT::f64, SystemZ::FP64RegisterClass); |
Anton Korobeynikov | 1ada84d | 2009-07-16 14:24:16 +0000 | [diff] [blame] | 53 | |
| 54 | addLegalFPImmediate(APFloat(+0.0)); // lzer |
| 55 | addLegalFPImmediate(APFloat(+0.0f)); // lzdr |
| 56 | addLegalFPImmediate(APFloat(-0.0)); // lzer + lner |
| 57 | addLegalFPImmediate(APFloat(-0.0f)); // lzdr + lndr |
Anton Korobeynikov | 2c97ae8 | 2009-07-16 14:19:02 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 60 | // Compute derived properties from the register classes |
| 61 | computeRegisterProperties(); |
| 62 | |
Anton Korobeynikov | 9e4816e | 2009-07-16 13:43:18 +0000 | [diff] [blame] | 63 | // Set shifts properties |
| 64 | setShiftAmountFlavor(Extend); |
Anton Korobeynikov | 48e8b3c | 2009-07-16 14:15:24 +0000 | [diff] [blame] | 65 | setShiftAmountType(MVT::i64); |
Anton Korobeynikov | 9e4816e | 2009-07-16 13:43:18 +0000 | [diff] [blame] | 66 | |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 67 | // Provide all sorts of operation actions |
Anton Korobeynikov | bf02217 | 2009-07-16 13:53:35 +0000 | [diff] [blame] | 68 | setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote); |
| 69 | setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote); |
| 70 | setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote); |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 71 | |
Anton Korobeynikov | 85c5c3f | 2009-07-16 14:22:46 +0000 | [diff] [blame] | 72 | setLoadExtAction(ISD::SEXTLOAD, MVT::f32, Expand); |
| 73 | setLoadExtAction(ISD::ZEXTLOAD, MVT::f32, Expand); |
| 74 | setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand); |
Anton Korobeynikov | 299dc78 | 2009-07-16 14:22:30 +0000 | [diff] [blame] | 75 | |
Anton Korobeynikov | 85c5c3f | 2009-07-16 14:22:46 +0000 | [diff] [blame] | 76 | setLoadExtAction(ISD::SEXTLOAD, MVT::f64, Expand); |
| 77 | setLoadExtAction(ISD::ZEXTLOAD, MVT::f64, Expand); |
| 78 | setLoadExtAction(ISD::EXTLOAD, MVT::f64, Expand); |
Anton Korobeynikov | 23eff5c | 2009-07-16 14:20:08 +0000 | [diff] [blame] | 79 | |
Anton Korobeynikov | e0167c1 | 2009-07-16 13:35:30 +0000 | [diff] [blame] | 80 | setStackPointerRegisterToSaveRestore(SystemZ::R15D); |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 81 | setSchedulingPreference(SchedulingForLatency); |
Anton Korobeynikov | 159ac63 | 2009-07-16 14:28:46 +0000 | [diff] [blame] | 82 | setBooleanContents(ZeroOrOneBooleanContent); |
Anton Korobeynikov | 87a24e3 | 2009-07-16 13:28:59 +0000 | [diff] [blame] | 83 | |
| 84 | setOperationAction(ISD::RET, MVT::Other, Custom); |
Anton Korobeynikov | 4ec3e5f | 2009-07-16 13:52:31 +0000 | [diff] [blame] | 85 | |
Anton Korobeynikov | 983d3a1 | 2009-07-16 14:07:24 +0000 | [diff] [blame] | 86 | setOperationAction(ISD::BR_JT, MVT::Other, Expand); |
Anton Korobeynikov | 4ec3e5f | 2009-07-16 13:52:31 +0000 | [diff] [blame] | 87 | setOperationAction(ISD::BRCOND, MVT::Other, Expand); |
| 88 | setOperationAction(ISD::BR_CC, MVT::i32, Custom); |
| 89 | setOperationAction(ISD::BR_CC, MVT::i64, Custom); |
Anton Korobeynikov | ae53567 | 2009-07-16 14:19:35 +0000 | [diff] [blame] | 90 | setOperationAction(ISD::BR_CC, MVT::f32, Custom); |
| 91 | setOperationAction(ISD::BR_CC, MVT::f64, Custom); |
| 92 | setOperationAction(ISD::ConstantPool, MVT::i32, Custom); |
| 93 | setOperationAction(ISD::ConstantPool, MVT::i64, Custom); |
Anton Korobeynikov | bad769f | 2009-07-16 13:57:27 +0000 | [diff] [blame] | 94 | setOperationAction(ISD::GlobalAddress, MVT::i64, Custom); |
Anton Korobeynikov | c16cdc5 | 2009-07-16 14:07:50 +0000 | [diff] [blame] | 95 | setOperationAction(ISD::JumpTable, MVT::i64, Custom); |
Anton Korobeynikov | c772c44 | 2009-07-16 14:08:15 +0000 | [diff] [blame] | 96 | setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand); |
Anton Korobeynikov | 7d1e39b | 2009-07-16 13:52:51 +0000 | [diff] [blame] | 97 | |
Anton Korobeynikov | 0a42d2b | 2009-07-16 14:14:33 +0000 | [diff] [blame] | 98 | setOperationAction(ISD::SDIV, MVT::i32, Expand); |
| 99 | setOperationAction(ISD::UDIV, MVT::i32, Expand); |
| 100 | setOperationAction(ISD::SDIV, MVT::i64, Expand); |
| 101 | setOperationAction(ISD::UDIV, MVT::i64, Expand); |
| 102 | setOperationAction(ISD::SREM, MVT::i32, Expand); |
| 103 | setOperationAction(ISD::UREM, MVT::i32, Expand); |
| 104 | setOperationAction(ISD::SREM, MVT::i64, Expand); |
| 105 | setOperationAction(ISD::UREM, MVT::i64, Expand); |
| 106 | |
Anton Korobeynikov | 7d1e39b | 2009-07-16 13:52:51 +0000 | [diff] [blame] | 107 | // FIXME: Can we lower these 2 efficiently? |
| 108 | setOperationAction(ISD::SETCC, MVT::i32, Expand); |
| 109 | setOperationAction(ISD::SETCC, MVT::i64, Expand); |
Anton Korobeynikov | da723d7 | 2009-07-16 14:22:15 +0000 | [diff] [blame] | 110 | setOperationAction(ISD::SETCC, MVT::f32, Expand); |
| 111 | setOperationAction(ISD::SETCC, MVT::f64, Expand); |
Anton Korobeynikov | 7d1e39b | 2009-07-16 13:52:51 +0000 | [diff] [blame] | 112 | setOperationAction(ISD::SELECT, MVT::i32, Expand); |
| 113 | setOperationAction(ISD::SELECT, MVT::i64, Expand); |
Anton Korobeynikov | da723d7 | 2009-07-16 14:22:15 +0000 | [diff] [blame] | 114 | setOperationAction(ISD::SELECT, MVT::f32, Expand); |
| 115 | setOperationAction(ISD::SELECT, MVT::f64, Expand); |
Anton Korobeynikov | 7d1e39b | 2009-07-16 13:52:51 +0000 | [diff] [blame] | 116 | setOperationAction(ISD::SELECT_CC, MVT::i32, Custom); |
| 117 | setOperationAction(ISD::SELECT_CC, MVT::i64, Custom); |
Anton Korobeynikov | ae53567 | 2009-07-16 14:19:35 +0000 | [diff] [blame] | 118 | setOperationAction(ISD::SELECT_CC, MVT::f32, Custom); |
| 119 | setOperationAction(ISD::SELECT_CC, MVT::f64, Custom); |
Anton Korobeynikov | dd0239b | 2009-07-16 13:53:55 +0000 | [diff] [blame] | 120 | |
Anton Korobeynikov | 8d1837d | 2009-07-16 13:56:42 +0000 | [diff] [blame] | 121 | // Funny enough: we don't have 64-bit signed versions of these stuff, but have |
| 122 | // unsigned. |
Anton Korobeynikov | dd0239b | 2009-07-16 13:53:55 +0000 | [diff] [blame] | 123 | setOperationAction(ISD::MULHS, MVT::i64, Expand); |
Anton Korobeynikov | dd0239b | 2009-07-16 13:53:55 +0000 | [diff] [blame] | 124 | setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand); |
Anton Korobeynikov | 9b4ae57 | 2009-07-16 14:20:56 +0000 | [diff] [blame] | 125 | |
Anton Korobeynikov | 20d062f | 2009-07-16 14:25:46 +0000 | [diff] [blame] | 126 | // Lower some FP stuff |
Anton Korobeynikov | 9b4ae57 | 2009-07-16 14:20:56 +0000 | [diff] [blame] | 127 | setOperationAction(ISD::FSIN, MVT::f32, Expand); |
| 128 | setOperationAction(ISD::FSIN, MVT::f64, Expand); |
| 129 | setOperationAction(ISD::FCOS, MVT::f32, Expand); |
| 130 | setOperationAction(ISD::FCOS, MVT::f64, Expand); |
Anton Korobeynikov | 98db78a | 2009-07-16 14:26:06 +0000 | [diff] [blame] | 131 | |
Anton Korobeynikov | 05a0b8b | 2009-07-16 14:27:01 +0000 | [diff] [blame] | 132 | // We have only 64-bit bitconverts |
Anton Korobeynikov | bb8a048 | 2009-07-16 14:30:29 +0000 | [diff] [blame^] | 133 | setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand); |
| 134 | setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand); |
Anton Korobeynikov | 05a0b8b | 2009-07-16 14:27:01 +0000 | [diff] [blame] | 135 | |
Anton Korobeynikov | a89430e | 2009-07-16 14:25:30 +0000 | [diff] [blame] | 136 | setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand); |
| 137 | setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand); |
Anton Korobeynikov | 98db78a | 2009-07-16 14:26:06 +0000 | [diff] [blame] | 138 | setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand); |
| 139 | setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand); |
Anton Korobeynikov | 20d062f | 2009-07-16 14:25:46 +0000 | [diff] [blame] | 140 | |
| 141 | setTruncStoreAction(MVT::f64, MVT::f32, Expand); |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | SDValue SystemZTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) { |
| 145 | switch (Op.getOpcode()) { |
Anton Korobeynikov | 87a24e3 | 2009-07-16 13:28:59 +0000 | [diff] [blame] | 146 | case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG); |
| 147 | case ISD::RET: return LowerRET(Op, DAG); |
Anton Korobeynikov | ba249e4 | 2009-07-16 13:50:21 +0000 | [diff] [blame] | 148 | case ISD::CALL: return LowerCALL(Op, DAG); |
Anton Korobeynikov | 4ec3e5f | 2009-07-16 13:52:31 +0000 | [diff] [blame] | 149 | case ISD::BR_CC: return LowerBR_CC(Op, DAG); |
Anton Korobeynikov | 7d1e39b | 2009-07-16 13:52:51 +0000 | [diff] [blame] | 150 | case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG); |
Anton Korobeynikov | bad769f | 2009-07-16 13:57:27 +0000 | [diff] [blame] | 151 | case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG); |
Anton Korobeynikov | c16cdc5 | 2009-07-16 14:07:50 +0000 | [diff] [blame] | 152 | case ISD::JumpTable: return LowerJumpTable(Op, DAG); |
Anton Korobeynikov | ae53567 | 2009-07-16 14:19:35 +0000 | [diff] [blame] | 153 | case ISD::ConstantPool: return LowerConstantPool(Op, DAG); |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 154 | default: |
| 155 | assert(0 && "unimplemented operand"); |
| 156 | return SDValue(); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | //===----------------------------------------------------------------------===// |
| 161 | // Calling Convention Implementation |
| 162 | //===----------------------------------------------------------------------===// |
| 163 | |
| 164 | #include "SystemZGenCallingConv.inc" |
| 165 | |
Anton Korobeynikov | 87a24e3 | 2009-07-16 13:28:59 +0000 | [diff] [blame] | 166 | SDValue SystemZTargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op, |
| 167 | SelectionDAG &DAG) { |
| 168 | unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); |
| 169 | switch (CC) { |
| 170 | default: |
| 171 | assert(0 && "Unsupported calling convention"); |
| 172 | case CallingConv::C: |
| 173 | case CallingConv::Fast: |
| 174 | return LowerCCCArguments(Op, DAG); |
| 175 | } |
| 176 | } |
| 177 | |
Anton Korobeynikov | ba249e4 | 2009-07-16 13:50:21 +0000 | [diff] [blame] | 178 | SDValue SystemZTargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) { |
| 179 | CallSDNode *TheCall = cast<CallSDNode>(Op.getNode()); |
| 180 | unsigned CallingConv = TheCall->getCallingConv(); |
| 181 | switch (CallingConv) { |
| 182 | default: |
| 183 | assert(0 && "Unsupported calling convention"); |
| 184 | case CallingConv::Fast: |
| 185 | case CallingConv::C: |
| 186 | return LowerCCCCallTo(Op, DAG, CallingConv); |
| 187 | } |
| 188 | } |
| 189 | |
Anton Korobeynikov | 87a24e3 | 2009-07-16 13:28:59 +0000 | [diff] [blame] | 190 | /// LowerCCCArguments - transform physical registers into virtual registers and |
| 191 | /// generate load operations for arguments places on the stack. |
| 192 | // FIXME: struct return stuff |
| 193 | // FIXME: varargs |
| 194 | SDValue SystemZTargetLowering::LowerCCCArguments(SDValue Op, |
| 195 | SelectionDAG &DAG) { |
| 196 | MachineFunction &MF = DAG.getMachineFunction(); |
| 197 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 198 | MachineRegisterInfo &RegInfo = MF.getRegInfo(); |
| 199 | SDValue Root = Op.getOperand(0); |
| 200 | bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0; |
| 201 | unsigned CC = MF.getFunction()->getCallingConv(); |
| 202 | DebugLoc dl = Op.getDebugLoc(); |
| 203 | |
| 204 | // Assign locations to all of the incoming arguments. |
| 205 | SmallVector<CCValAssign, 16> ArgLocs; |
| 206 | CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs); |
| 207 | CCInfo.AnalyzeFormalArguments(Op.getNode(), CC_SystemZ); |
| 208 | |
| 209 | assert(!isVarArg && "Varargs not supported yet"); |
| 210 | |
| 211 | SmallVector<SDValue, 16> ArgValues; |
| 212 | for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { |
Anton Korobeynikov | c1a1e4a | 2009-07-16 14:29:05 +0000 | [diff] [blame] | 213 | SDValue ArgValue; |
Anton Korobeynikov | 87a24e3 | 2009-07-16 13:28:59 +0000 | [diff] [blame] | 214 | CCValAssign &VA = ArgLocs[i]; |
Anton Korobeynikov | c1a1e4a | 2009-07-16 14:29:05 +0000 | [diff] [blame] | 215 | MVT LocVT = VA.getLocVT(); |
Anton Korobeynikov | 87a24e3 | 2009-07-16 13:28:59 +0000 | [diff] [blame] | 216 | if (VA.isRegLoc()) { |
| 217 | // Arguments passed in registers |
Anton Korobeynikov | 0e31d5c | 2009-07-16 14:19:16 +0000 | [diff] [blame] | 218 | TargetRegisterClass *RC; |
Anton Korobeynikov | c1a1e4a | 2009-07-16 14:29:05 +0000 | [diff] [blame] | 219 | switch (LocVT.getSimpleVT()) { |
Anton Korobeynikov | 87a24e3 | 2009-07-16 13:28:59 +0000 | [diff] [blame] | 220 | default: |
| 221 | cerr << "LowerFORMAL_ARGUMENTS Unhandled argument type: " |
Anton Korobeynikov | c1a1e4a | 2009-07-16 14:29:05 +0000 | [diff] [blame] | 222 | << LocVT.getSimpleVT() |
Anton Korobeynikov | 87a24e3 | 2009-07-16 13:28:59 +0000 | [diff] [blame] | 223 | << "\n"; |
| 224 | abort(); |
Anton Korobeynikov | 0e31d5c | 2009-07-16 14:19:16 +0000 | [diff] [blame] | 225 | case MVT::i64: |
| 226 | RC = SystemZ::GR64RegisterClass; |
| 227 | break; |
| 228 | case MVT::f32: |
| 229 | RC = SystemZ::FP32RegisterClass; |
| 230 | break; |
| 231 | case MVT::f64: |
| 232 | RC = SystemZ::FP64RegisterClass; |
| 233 | break; |
Anton Korobeynikov | 87a24e3 | 2009-07-16 13:28:59 +0000 | [diff] [blame] | 234 | } |
Anton Korobeynikov | 0e31d5c | 2009-07-16 14:19:16 +0000 | [diff] [blame] | 235 | |
| 236 | unsigned VReg = RegInfo.createVirtualRegister(RC); |
| 237 | RegInfo.addLiveIn(VA.getLocReg(), VReg); |
Anton Korobeynikov | c1a1e4a | 2009-07-16 14:29:05 +0000 | [diff] [blame] | 238 | ArgValue = DAG.getCopyFromReg(Root, dl, VReg, LocVT); |
Anton Korobeynikov | 87a24e3 | 2009-07-16 13:28:59 +0000 | [diff] [blame] | 239 | } else { |
| 240 | // Sanity check |
| 241 | assert(VA.isMemLoc()); |
Anton Korobeynikov | 980d550 | 2009-07-16 14:08:42 +0000 | [diff] [blame] | 242 | |
| 243 | // Create the nodes corresponding to a load from this parameter slot. |
Anton Korobeynikov | 87a24e3 | 2009-07-16 13:28:59 +0000 | [diff] [blame] | 244 | // Create the frame index object for this incoming parameter... |
Anton Korobeynikov | c1a1e4a | 2009-07-16 14:29:05 +0000 | [diff] [blame] | 245 | int FI = MFI->CreateFixedObject(LocVT.getSizeInBits()/8, |
Anton Korobeynikov | 980d550 | 2009-07-16 14:08:42 +0000 | [diff] [blame] | 246 | VA.getLocMemOffset()); |
Anton Korobeynikov | 87a24e3 | 2009-07-16 13:28:59 +0000 | [diff] [blame] | 247 | |
| 248 | // Create the SelectionDAG nodes corresponding to a load |
Anton Korobeynikov | c1a1e4a | 2009-07-16 14:29:05 +0000 | [diff] [blame] | 249 | // from this parameter |
Anton Korobeynikov | 980d550 | 2009-07-16 14:08:42 +0000 | [diff] [blame] | 250 | SDValue FIN = DAG.getFrameIndex(FI, getPointerTy()); |
Anton Korobeynikov | c1a1e4a | 2009-07-16 14:29:05 +0000 | [diff] [blame] | 251 | ArgValue = DAG.getLoad(LocVT, dl, Root, FIN, |
| 252 | PseudoSourceValue::getFixedStack(FI), 0); |
Anton Korobeynikov | 87a24e3 | 2009-07-16 13:28:59 +0000 | [diff] [blame] | 253 | } |
Anton Korobeynikov | c1a1e4a | 2009-07-16 14:29:05 +0000 | [diff] [blame] | 254 | |
| 255 | // If this is an 8/16/32-bit value, it is really passed promoted to 64 |
| 256 | // bits. Insert an assert[sz]ext to capture this, then truncate to the |
| 257 | // right size. |
| 258 | if (VA.getLocInfo() == CCValAssign::SExt) |
| 259 | ArgValue = DAG.getNode(ISD::AssertSext, dl, LocVT, ArgValue, |
| 260 | DAG.getValueType(VA.getValVT())); |
| 261 | else if (VA.getLocInfo() == CCValAssign::ZExt) |
| 262 | ArgValue = DAG.getNode(ISD::AssertZext, dl, LocVT, ArgValue, |
| 263 | DAG.getValueType(VA.getValVT())); |
| 264 | |
| 265 | if (VA.getLocInfo() != CCValAssign::Full) |
| 266 | ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue); |
| 267 | |
| 268 | ArgValues.push_back(ArgValue); |
Anton Korobeynikov | 87a24e3 | 2009-07-16 13:28:59 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | ArgValues.push_back(Root); |
| 272 | |
| 273 | // Return the new list of results. |
| 274 | return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(), |
| 275 | &ArgValues[0], ArgValues.size()).getValue(Op.getResNo()); |
| 276 | } |
| 277 | |
Anton Korobeynikov | ba249e4 | 2009-07-16 13:50:21 +0000 | [diff] [blame] | 278 | /// LowerCCCCallTo - functions arguments are copied from virtual regs to |
| 279 | /// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted. |
| 280 | /// TODO: sret. |
| 281 | SDValue SystemZTargetLowering::LowerCCCCallTo(SDValue Op, SelectionDAG &DAG, |
| 282 | unsigned CC) { |
| 283 | CallSDNode *TheCall = cast<CallSDNode>(Op.getNode()); |
| 284 | SDValue Chain = TheCall->getChain(); |
| 285 | SDValue Callee = TheCall->getCallee(); |
| 286 | bool isVarArg = TheCall->isVarArg(); |
| 287 | DebugLoc dl = Op.getDebugLoc(); |
Anton Korobeynikov | 656ac6f | 2009-07-16 13:51:53 +0000 | [diff] [blame] | 288 | MachineFunction &MF = DAG.getMachineFunction(); |
Anton Korobeynikov | ba249e4 | 2009-07-16 13:50:21 +0000 | [diff] [blame] | 289 | |
Anton Korobeynikov | c7b71be | 2009-07-16 13:52:10 +0000 | [diff] [blame] | 290 | // Offset to first argument stack slot. |
| 291 | const unsigned FirstArgOffset = 160; |
| 292 | |
Anton Korobeynikov | ba249e4 | 2009-07-16 13:50:21 +0000 | [diff] [blame] | 293 | // Analyze operands of the call, assigning locations to each operand. |
| 294 | SmallVector<CCValAssign, 16> ArgLocs; |
| 295 | CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs); |
| 296 | |
| 297 | CCInfo.AnalyzeCallOperands(TheCall, CC_SystemZ); |
| 298 | |
| 299 | // Get a count of how many bytes are to be pushed on the stack. |
| 300 | unsigned NumBytes = CCInfo.getNextStackOffset(); |
| 301 | |
| 302 | Chain = DAG.getCALLSEQ_START(Chain ,DAG.getConstant(NumBytes, |
| 303 | getPointerTy(), true)); |
| 304 | |
| 305 | SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass; |
| 306 | SmallVector<SDValue, 12> MemOpChains; |
| 307 | SDValue StackPtr; |
| 308 | |
| 309 | // Walk the register/memloc assignments, inserting copies/loads. |
| 310 | for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { |
| 311 | CCValAssign &VA = ArgLocs[i]; |
| 312 | |
| 313 | // Arguments start after the 5 first operands of ISD::CALL |
| 314 | SDValue Arg = TheCall->getArg(i); |
| 315 | |
| 316 | // Promote the value if needed. |
| 317 | switch (VA.getLocInfo()) { |
| 318 | default: assert(0 && "Unknown loc info!"); |
| 319 | case CCValAssign::Full: break; |
| 320 | case CCValAssign::SExt: |
| 321 | Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg); |
| 322 | break; |
| 323 | case CCValAssign::ZExt: |
| 324 | Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg); |
| 325 | break; |
| 326 | case CCValAssign::AExt: |
| 327 | Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg); |
| 328 | break; |
| 329 | } |
| 330 | |
| 331 | // Arguments that can be passed on register must be kept at RegsToPass |
| 332 | // vector |
| 333 | if (VA.isRegLoc()) { |
| 334 | RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg)); |
| 335 | } else { |
| 336 | assert(VA.isMemLoc()); |
| 337 | |
| 338 | if (StackPtr.getNode() == 0) |
Anton Korobeynikov | 656ac6f | 2009-07-16 13:51:53 +0000 | [diff] [blame] | 339 | StackPtr = |
| 340 | DAG.getCopyFromReg(Chain, dl, |
| 341 | (RegInfo->hasFP(MF) ? |
| 342 | SystemZ::R11D : SystemZ::R15D), |
| 343 | getPointerTy()); |
Anton Korobeynikov | ba249e4 | 2009-07-16 13:50:21 +0000 | [diff] [blame] | 344 | |
Anton Korobeynikov | c7b71be | 2009-07-16 13:52:10 +0000 | [diff] [blame] | 345 | unsigned Offset = FirstArgOffset + VA.getLocMemOffset(); |
| 346 | SDValue PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), |
| 347 | StackPtr, |
| 348 | DAG.getIntPtrConstant(Offset)); |
Anton Korobeynikov | ba249e4 | 2009-07-16 13:50:21 +0000 | [diff] [blame] | 349 | |
| 350 | MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff, |
Anton Korobeynikov | c7b71be | 2009-07-16 13:52:10 +0000 | [diff] [blame] | 351 | PseudoSourceValue::getStack(), Offset)); |
Anton Korobeynikov | ba249e4 | 2009-07-16 13:50:21 +0000 | [diff] [blame] | 352 | } |
| 353 | } |
| 354 | |
| 355 | // Transform all store nodes into one single node because all store nodes are |
| 356 | // independent of each other. |
| 357 | if (!MemOpChains.empty()) |
| 358 | Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, |
| 359 | &MemOpChains[0], MemOpChains.size()); |
| 360 | |
| 361 | // Build a sequence of copy-to-reg nodes chained together with token chain and |
| 362 | // flag operands which copy the outgoing args into registers. The InFlag in |
| 363 | // necessary since all emited instructions must be stuck together. |
| 364 | SDValue InFlag; |
| 365 | for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { |
| 366 | Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first, |
| 367 | RegsToPass[i].second, InFlag); |
| 368 | InFlag = Chain.getValue(1); |
| 369 | } |
| 370 | |
| 371 | // If the callee is a GlobalAddress node (quite common, every direct call is) |
| 372 | // turn it into a TargetGlobalAddress node so that legalize doesn't hack it. |
| 373 | // Likewise ExternalSymbol -> TargetExternalSymbol. |
| 374 | if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) |
| 375 | Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy()); |
| 376 | else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee)) |
| 377 | Callee = DAG.getTargetExternalSymbol(E->getSymbol(), getPointerTy()); |
| 378 | |
| 379 | // Returns a chain & a flag for retval copy to use. |
| 380 | SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag); |
| 381 | SmallVector<SDValue, 8> Ops; |
| 382 | Ops.push_back(Chain); |
| 383 | Ops.push_back(Callee); |
| 384 | |
| 385 | // Add argument registers to the end of the list so that they are |
| 386 | // known live into the call. |
| 387 | for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) |
| 388 | Ops.push_back(DAG.getRegister(RegsToPass[i].first, |
| 389 | RegsToPass[i].second.getValueType())); |
| 390 | |
| 391 | if (InFlag.getNode()) |
| 392 | Ops.push_back(InFlag); |
| 393 | |
| 394 | Chain = DAG.getNode(SystemZISD::CALL, dl, NodeTys, &Ops[0], Ops.size()); |
| 395 | InFlag = Chain.getValue(1); |
| 396 | |
| 397 | // Create the CALLSEQ_END node. |
| 398 | Chain = DAG.getCALLSEQ_END(Chain, |
| 399 | DAG.getConstant(NumBytes, getPointerTy(), true), |
| 400 | DAG.getConstant(0, getPointerTy(), true), |
| 401 | InFlag); |
| 402 | InFlag = Chain.getValue(1); |
| 403 | |
| 404 | // Handle result values, copying them out of physregs into vregs that we |
| 405 | // return. |
| 406 | return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG), |
| 407 | Op.getResNo()); |
| 408 | } |
| 409 | |
| 410 | /// LowerCallResult - Lower the result values of an ISD::CALL into the |
| 411 | /// appropriate copies out of appropriate physical registers. This assumes that |
| 412 | /// Chain/InFlag are the input chain/flag to use, and that TheCall is the call |
| 413 | /// being lowered. Returns a SDNode with the same number of values as the |
| 414 | /// ISD::CALL. |
| 415 | SDNode* |
| 416 | SystemZTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag, |
| 417 | CallSDNode *TheCall, |
| 418 | unsigned CallingConv, |
| 419 | SelectionDAG &DAG) { |
| 420 | bool isVarArg = TheCall->isVarArg(); |
| 421 | DebugLoc dl = TheCall->getDebugLoc(); |
| 422 | |
| 423 | // Assign locations to each value returned by this call. |
| 424 | SmallVector<CCValAssign, 16> RVLocs; |
| 425 | CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs); |
| 426 | |
| 427 | CCInfo.AnalyzeCallResult(TheCall, RetCC_SystemZ); |
| 428 | SmallVector<SDValue, 8> ResultVals; |
| 429 | |
| 430 | // Copy all of the result registers out of their specified physreg. |
| 431 | for (unsigned i = 0; i != RVLocs.size(); ++i) { |
Anton Korobeynikov | 22836d1 | 2009-07-16 13:58:24 +0000 | [diff] [blame] | 432 | CCValAssign &VA = RVLocs[i]; |
| 433 | |
| 434 | Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), |
| 435 | VA.getLocVT(), InFlag).getValue(1); |
| 436 | SDValue RetValue = Chain.getValue(0); |
Anton Korobeynikov | ba249e4 | 2009-07-16 13:50:21 +0000 | [diff] [blame] | 437 | InFlag = Chain.getValue(2); |
Anton Korobeynikov | 22836d1 | 2009-07-16 13:58:24 +0000 | [diff] [blame] | 438 | |
| 439 | // If this is an 8/16/32-bit value, it is really passed promoted to 64 |
| 440 | // bits. Insert an assert[sz]ext to capture this, then truncate to the |
| 441 | // right size. |
| 442 | if (VA.getLocInfo() == CCValAssign::SExt) |
| 443 | RetValue = DAG.getNode(ISD::AssertSext, dl, VA.getLocVT(), RetValue, |
| 444 | DAG.getValueType(VA.getValVT())); |
| 445 | else if (VA.getLocInfo() == CCValAssign::ZExt) |
| 446 | RetValue = DAG.getNode(ISD::AssertZext, dl, VA.getLocVT(), RetValue, |
| 447 | DAG.getValueType(VA.getValVT())); |
| 448 | |
| 449 | if (VA.getLocInfo() != CCValAssign::Full) |
| 450 | RetValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), RetValue); |
| 451 | |
| 452 | ResultVals.push_back(RetValue); |
Anton Korobeynikov | ba249e4 | 2009-07-16 13:50:21 +0000 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | ResultVals.push_back(Chain); |
| 456 | |
| 457 | // Merge everything together with a MERGE_VALUES node. |
| 458 | return DAG.getNode(ISD::MERGE_VALUES, dl, TheCall->getVTList(), |
| 459 | &ResultVals[0], ResultVals.size()).getNode(); |
| 460 | } |
| 461 | |
| 462 | |
Anton Korobeynikov | 87a24e3 | 2009-07-16 13:28:59 +0000 | [diff] [blame] | 463 | SDValue SystemZTargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) { |
| 464 | // CCValAssign - represent the assignment of the return value to a location |
| 465 | SmallVector<CCValAssign, 16> RVLocs; |
| 466 | unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv(); |
| 467 | bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg(); |
| 468 | DebugLoc dl = Op.getDebugLoc(); |
| 469 | |
| 470 | // CCState - Info about the registers and stack slot. |
| 471 | CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs); |
| 472 | |
| 473 | // Analize return values of ISD::RET |
| 474 | CCInfo.AnalyzeReturn(Op.getNode(), RetCC_SystemZ); |
| 475 | |
| 476 | // If this is the first return lowered for this function, add the regs to the |
| 477 | // liveout set for the function. |
| 478 | if (DAG.getMachineFunction().getRegInfo().liveout_empty()) { |
| 479 | for (unsigned i = 0; i != RVLocs.size(); ++i) |
| 480 | if (RVLocs[i].isRegLoc()) |
| 481 | DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg()); |
| 482 | } |
| 483 | |
| 484 | // The chain is always operand #0 |
| 485 | SDValue Chain = Op.getOperand(0); |
| 486 | SDValue Flag; |
| 487 | |
| 488 | // Copy the result values into the output registers. |
| 489 | for (unsigned i = 0; i != RVLocs.size(); ++i) { |
| 490 | CCValAssign &VA = RVLocs[i]; |
Anton Korobeynikov | a51752c | 2009-07-16 13:42:31 +0000 | [diff] [blame] | 491 | SDValue ResValue = Op.getOperand(i*2+1); |
Anton Korobeynikov | 87a24e3 | 2009-07-16 13:28:59 +0000 | [diff] [blame] | 492 | assert(VA.isRegLoc() && "Can only return in registers!"); |
| 493 | |
Anton Korobeynikov | a51752c | 2009-07-16 13:42:31 +0000 | [diff] [blame] | 494 | // If this is an 8/16/32-bit value, it is really should be passed promoted |
| 495 | // to 64 bits. |
| 496 | if (VA.getLocInfo() == CCValAssign::SExt) |
| 497 | ResValue = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ResValue); |
| 498 | else if (VA.getLocInfo() == CCValAssign::ZExt) |
| 499 | ResValue = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ResValue); |
| 500 | else if (VA.getLocInfo() == CCValAssign::AExt) |
| 501 | ResValue = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ResValue); |
| 502 | |
Anton Korobeynikov | 87a24e3 | 2009-07-16 13:28:59 +0000 | [diff] [blame] | 503 | // ISD::RET => ret chain, (regnum1,val1), ... |
| 504 | // So i*2+1 index only the regnums |
Anton Korobeynikov | a51752c | 2009-07-16 13:42:31 +0000 | [diff] [blame] | 505 | Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ResValue, Flag); |
Anton Korobeynikov | 87a24e3 | 2009-07-16 13:28:59 +0000 | [diff] [blame] | 506 | |
| 507 | // Guarantee that all emitted copies are stuck together, |
| 508 | // avoiding something bad. |
| 509 | Flag = Chain.getValue(1); |
| 510 | } |
| 511 | |
| 512 | if (Flag.getNode()) |
| 513 | return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain, Flag); |
| 514 | |
| 515 | // Return Void |
| 516 | return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain); |
| 517 | } |
| 518 | |
Anton Korobeynikov | 4ec3e5f | 2009-07-16 13:52:31 +0000 | [diff] [blame] | 519 | SDValue SystemZTargetLowering::EmitCmp(SDValue LHS, SDValue RHS, |
| 520 | ISD::CondCode CC, SDValue &SystemZCC, |
| 521 | SelectionDAG &DAG) { |
Anton Korobeynikov | 4ec3e5f | 2009-07-16 13:52:31 +0000 | [diff] [blame] | 522 | // FIXME: Emit a test if RHS is zero |
| 523 | |
| 524 | bool isUnsigned = false; |
| 525 | SystemZCC::CondCodes TCC; |
| 526 | switch (CC) { |
| 527 | default: assert(0 && "Invalid integer condition!"); |
| 528 | case ISD::SETEQ: |
Anton Korobeynikov | 10c086c | 2009-07-16 14:19:54 +0000 | [diff] [blame] | 529 | case ISD::SETOEQ: |
Anton Korobeynikov | 4ec3e5f | 2009-07-16 13:52:31 +0000 | [diff] [blame] | 530 | TCC = SystemZCC::E; |
| 531 | break; |
Anton Korobeynikov | 10c086c | 2009-07-16 14:19:54 +0000 | [diff] [blame] | 532 | case ISD::SETUEQ: |
| 533 | TCC = SystemZCC::NLH; |
| 534 | break; |
Anton Korobeynikov | 4ec3e5f | 2009-07-16 13:52:31 +0000 | [diff] [blame] | 535 | case ISD::SETNE: |
Anton Korobeynikov | 10c086c | 2009-07-16 14:19:54 +0000 | [diff] [blame] | 536 | case ISD::SETONE: |
Anton Korobeynikov | 4ec3e5f | 2009-07-16 13:52:31 +0000 | [diff] [blame] | 537 | TCC = SystemZCC::NE; |
| 538 | break; |
Anton Korobeynikov | 10c086c | 2009-07-16 14:19:54 +0000 | [diff] [blame] | 539 | case ISD::SETUNE: |
| 540 | TCC = SystemZCC::LH; |
| 541 | break; |
| 542 | case ISD::SETO: |
| 543 | TCC = SystemZCC::O; |
| 544 | break; |
| 545 | case ISD::SETUO: |
| 546 | TCC = SystemZCC::NO; |
| 547 | break; |
Anton Korobeynikov | 4ec3e5f | 2009-07-16 13:52:31 +0000 | [diff] [blame] | 548 | case ISD::SETULE: |
Anton Korobeynikov | 10c086c | 2009-07-16 14:19:54 +0000 | [diff] [blame] | 549 | if (LHS.getValueType().isFloatingPoint()) { |
| 550 | TCC = SystemZCC::NH; |
| 551 | break; |
| 552 | } |
Anton Korobeynikov | 4ec3e5f | 2009-07-16 13:52:31 +0000 | [diff] [blame] | 553 | isUnsigned = true; // FALLTHROUGH |
| 554 | case ISD::SETLE: |
Anton Korobeynikov | 10c086c | 2009-07-16 14:19:54 +0000 | [diff] [blame] | 555 | case ISD::SETOLE: |
Anton Korobeynikov | 4ec3e5f | 2009-07-16 13:52:31 +0000 | [diff] [blame] | 556 | TCC = SystemZCC::LE; |
| 557 | break; |
| 558 | case ISD::SETUGE: |
Anton Korobeynikov | 10c086c | 2009-07-16 14:19:54 +0000 | [diff] [blame] | 559 | if (LHS.getValueType().isFloatingPoint()) { |
| 560 | TCC = SystemZCC::NL; |
| 561 | break; |
| 562 | } |
Anton Korobeynikov | 4ec3e5f | 2009-07-16 13:52:31 +0000 | [diff] [blame] | 563 | isUnsigned = true; // FALLTHROUGH |
| 564 | case ISD::SETGE: |
Anton Korobeynikov | 10c086c | 2009-07-16 14:19:54 +0000 | [diff] [blame] | 565 | case ISD::SETOGE: |
Anton Korobeynikov | 4ec3e5f | 2009-07-16 13:52:31 +0000 | [diff] [blame] | 566 | TCC = SystemZCC::HE; |
| 567 | break; |
| 568 | case ISD::SETUGT: |
Anton Korobeynikov | 10c086c | 2009-07-16 14:19:54 +0000 | [diff] [blame] | 569 | if (LHS.getValueType().isFloatingPoint()) { |
| 570 | TCC = SystemZCC::NLE; |
| 571 | break; |
| 572 | } |
| 573 | isUnsigned = true; // FALLTHROUGH |
Anton Korobeynikov | 4ec3e5f | 2009-07-16 13:52:31 +0000 | [diff] [blame] | 574 | case ISD::SETGT: |
Anton Korobeynikov | 10c086c | 2009-07-16 14:19:54 +0000 | [diff] [blame] | 575 | case ISD::SETOGT: |
| 576 | TCC = SystemZCC::H; |
Anton Korobeynikov | 4ec3e5f | 2009-07-16 13:52:31 +0000 | [diff] [blame] | 577 | break; |
| 578 | case ISD::SETULT: |
Anton Korobeynikov | 10c086c | 2009-07-16 14:19:54 +0000 | [diff] [blame] | 579 | if (LHS.getValueType().isFloatingPoint()) { |
| 580 | TCC = SystemZCC::NHE; |
| 581 | break; |
| 582 | } |
| 583 | isUnsigned = true; // FALLTHROUGH |
| 584 | case ISD::SETLT: |
| 585 | case ISD::SETOLT: |
Anton Korobeynikov | 4ec3e5f | 2009-07-16 13:52:31 +0000 | [diff] [blame] | 586 | TCC = SystemZCC::L; |
| 587 | break; |
| 588 | } |
| 589 | |
| 590 | SystemZCC = DAG.getConstant(TCC, MVT::i32); |
| 591 | |
| 592 | DebugLoc dl = LHS.getDebugLoc(); |
| 593 | return DAG.getNode((isUnsigned ? SystemZISD::UCMP : SystemZISD::CMP), |
| 594 | dl, MVT::Flag, LHS, RHS); |
| 595 | } |
| 596 | |
| 597 | |
| 598 | SDValue SystemZTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) { |
| 599 | SDValue Chain = Op.getOperand(0); |
| 600 | ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get(); |
| 601 | SDValue LHS = Op.getOperand(2); |
| 602 | SDValue RHS = Op.getOperand(3); |
| 603 | SDValue Dest = Op.getOperand(4); |
| 604 | DebugLoc dl = Op.getDebugLoc(); |
| 605 | |
| 606 | SDValue SystemZCC; |
| 607 | SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG); |
| 608 | return DAG.getNode(SystemZISD::BRCOND, dl, Op.getValueType(), |
| 609 | Chain, Dest, SystemZCC, Flag); |
| 610 | } |
| 611 | |
Anton Korobeynikov | 7d1e39b | 2009-07-16 13:52:51 +0000 | [diff] [blame] | 612 | SDValue SystemZTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) { |
| 613 | SDValue LHS = Op.getOperand(0); |
| 614 | SDValue RHS = Op.getOperand(1); |
| 615 | SDValue TrueV = Op.getOperand(2); |
| 616 | SDValue FalseV = Op.getOperand(3); |
| 617 | ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get(); |
| 618 | DebugLoc dl = Op.getDebugLoc(); |
| 619 | |
| 620 | SDValue SystemZCC; |
| 621 | SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG); |
| 622 | |
| 623 | SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag); |
| 624 | SmallVector<SDValue, 4> Ops; |
| 625 | Ops.push_back(TrueV); |
| 626 | Ops.push_back(FalseV); |
| 627 | Ops.push_back(SystemZCC); |
| 628 | Ops.push_back(Flag); |
| 629 | |
| 630 | return DAG.getNode(SystemZISD::SELECT, dl, VTs, &Ops[0], Ops.size()); |
| 631 | } |
| 632 | |
Anton Korobeynikov | bad769f | 2009-07-16 13:57:27 +0000 | [diff] [blame] | 633 | SDValue SystemZTargetLowering::LowerGlobalAddress(SDValue Op, |
| 634 | SelectionDAG &DAG) { |
| 635 | DebugLoc dl = Op.getDebugLoc(); |
| 636 | GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal(); |
Anton Korobeynikov | 6fe326c | 2009-07-16 14:16:05 +0000 | [diff] [blame] | 637 | int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset(); |
Anton Korobeynikov | bad769f | 2009-07-16 13:57:27 +0000 | [diff] [blame] | 638 | |
Anton Korobeynikov | 6fe326c | 2009-07-16 14:16:05 +0000 | [diff] [blame] | 639 | bool IsPic = getTargetMachine().getRelocationModel() == Reloc::PIC_; |
| 640 | bool ExtraLoadRequired = |
| 641 | Subtarget.GVRequiresExtraLoad(GV, getTargetMachine(), false); |
| 642 | |
| 643 | SDValue Result; |
| 644 | if (!IsPic && !ExtraLoadRequired) { |
| 645 | Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset); |
| 646 | Offset = 0; |
| 647 | } else { |
| 648 | unsigned char OpFlags = 0; |
| 649 | if (ExtraLoadRequired) |
| 650 | OpFlags = SystemZII::MO_GOTENT; |
| 651 | |
| 652 | Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0, OpFlags); |
| 653 | } |
| 654 | |
| 655 | Result = DAG.getNode(SystemZISD::PCRelativeWrapper, dl, |
| 656 | getPointerTy(), Result); |
| 657 | |
| 658 | if (ExtraLoadRequired) |
| 659 | Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result, |
| 660 | PseudoSourceValue::getGOT(), 0); |
| 661 | |
| 662 | // If there was a non-zero offset that we didn't fold, create an explicit |
| 663 | // addition for it. |
| 664 | if (Offset != 0) |
| 665 | Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result, |
| 666 | DAG.getConstant(Offset, getPointerTy())); |
| 667 | |
| 668 | return Result; |
Anton Korobeynikov | bad769f | 2009-07-16 13:57:27 +0000 | [diff] [blame] | 669 | } |
| 670 | |
Anton Korobeynikov | ae53567 | 2009-07-16 14:19:35 +0000 | [diff] [blame] | 671 | // FIXME: PIC here |
Anton Korobeynikov | c16cdc5 | 2009-07-16 14:07:50 +0000 | [diff] [blame] | 672 | SDValue SystemZTargetLowering::LowerJumpTable(SDValue Op, |
| 673 | SelectionDAG &DAG) { |
| 674 | DebugLoc dl = Op.getDebugLoc(); |
| 675 | JumpTableSDNode *JT = cast<JumpTableSDNode>(Op); |
| 676 | SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy()); |
| 677 | |
| 678 | return DAG.getNode(SystemZISD::PCRelativeWrapper, dl, getPointerTy(), Result); |
| 679 | } |
| 680 | |
Anton Korobeynikov | ae53567 | 2009-07-16 14:19:35 +0000 | [diff] [blame] | 681 | |
| 682 | // FIXME: PIC here |
| 683 | // FIXME: This is just dirty hack. We need to lower cpool properly |
| 684 | SDValue SystemZTargetLowering::LowerConstantPool(SDValue Op, |
| 685 | SelectionDAG &DAG) { |
| 686 | DebugLoc dl = Op.getDebugLoc(); |
| 687 | ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op); |
| 688 | |
| 689 | SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(), |
| 690 | CP->getAlignment(), |
| 691 | CP->getOffset()); |
| 692 | |
| 693 | return DAG.getNode(SystemZISD::PCRelativeWrapper, dl, getPointerTy(), Result); |
| 694 | } |
| 695 | |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 696 | const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const { |
| 697 | switch (Opcode) { |
Anton Korobeynikov | 87a24e3 | 2009-07-16 13:28:59 +0000 | [diff] [blame] | 698 | case SystemZISD::RET_FLAG: return "SystemZISD::RET_FLAG"; |
Anton Korobeynikov | ba249e4 | 2009-07-16 13:50:21 +0000 | [diff] [blame] | 699 | case SystemZISD::CALL: return "SystemZISD::CALL"; |
Anton Korobeynikov | 4ec3e5f | 2009-07-16 13:52:31 +0000 | [diff] [blame] | 700 | case SystemZISD::BRCOND: return "SystemZISD::BRCOND"; |
| 701 | case SystemZISD::CMP: return "SystemZISD::CMP"; |
| 702 | case SystemZISD::UCMP: return "SystemZISD::UCMP"; |
Anton Korobeynikov | 7d1e39b | 2009-07-16 13:52:51 +0000 | [diff] [blame] | 703 | case SystemZISD::SELECT: return "SystemZISD::SELECT"; |
Anton Korobeynikov | bad769f | 2009-07-16 13:57:27 +0000 | [diff] [blame] | 704 | case SystemZISD::PCRelativeWrapper: return "SystemZISD::PCRelativeWrapper"; |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 705 | default: return NULL; |
| 706 | } |
| 707 | } |
| 708 | |
Anton Korobeynikov | 7d1e39b | 2009-07-16 13:52:51 +0000 | [diff] [blame] | 709 | //===----------------------------------------------------------------------===// |
| 710 | // Other Lowering Code |
| 711 | //===----------------------------------------------------------------------===// |
| 712 | |
| 713 | MachineBasicBlock* |
| 714 | SystemZTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI, |
| 715 | MachineBasicBlock *BB) const { |
| 716 | const SystemZInstrInfo &TII = *TM.getInstrInfo(); |
| 717 | DebugLoc dl = MI->getDebugLoc(); |
Anton Korobeynikov | da723d7 | 2009-07-16 14:22:15 +0000 | [diff] [blame] | 718 | assert((MI->getOpcode() == SystemZ::Select32 || |
| 719 | MI->getOpcode() == SystemZ::SelectF32 || |
| 720 | MI->getOpcode() == SystemZ::Select64 || |
| 721 | MI->getOpcode() == SystemZ::SelectF64) && |
Anton Korobeynikov | 7d1e39b | 2009-07-16 13:52:51 +0000 | [diff] [blame] | 722 | "Unexpected instr type to insert"); |
| 723 | |
| 724 | // To "insert" a SELECT instruction, we actually have to insert the diamond |
| 725 | // control-flow pattern. The incoming instruction knows the destination vreg |
| 726 | // to set, the condition code register to branch on, the true/false values to |
| 727 | // select between, and a branch opcode to use. |
| 728 | const BasicBlock *LLVM_BB = BB->getBasicBlock(); |
| 729 | MachineFunction::iterator I = BB; |
| 730 | ++I; |
| 731 | |
| 732 | // thisMBB: |
| 733 | // ... |
| 734 | // TrueVal = ... |
| 735 | // cmpTY ccX, r1, r2 |
| 736 | // jCC copy1MBB |
| 737 | // fallthrough --> copy0MBB |
| 738 | MachineBasicBlock *thisMBB = BB; |
| 739 | MachineFunction *F = BB->getParent(); |
| 740 | MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 741 | MachineBasicBlock *copy1MBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 742 | SystemZCC::CondCodes CC = (SystemZCC::CondCodes)MI->getOperand(3).getImm(); |
| 743 | BuildMI(BB, dl, TII.getBrCond(CC)).addMBB(copy1MBB); |
| 744 | F->insert(I, copy0MBB); |
| 745 | F->insert(I, copy1MBB); |
| 746 | // Update machine-CFG edges by transferring all successors of the current |
| 747 | // block to the new block which will contain the Phi node for the select. |
| 748 | copy1MBB->transferSuccessors(BB); |
| 749 | // Next, add the true and fallthrough blocks as its successors. |
| 750 | BB->addSuccessor(copy0MBB); |
| 751 | BB->addSuccessor(copy1MBB); |
| 752 | |
| 753 | // copy0MBB: |
| 754 | // %FalseValue = ... |
| 755 | // # fallthrough to copy1MBB |
| 756 | BB = copy0MBB; |
| 757 | |
| 758 | // Update machine-CFG edges |
| 759 | BB->addSuccessor(copy1MBB); |
| 760 | |
| 761 | // copy1MBB: |
| 762 | // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ] |
| 763 | // ... |
| 764 | BB = copy1MBB; |
| 765 | BuildMI(BB, dl, TII.get(SystemZ::PHI), |
| 766 | MI->getOperand(0).getReg()) |
| 767 | .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB) |
| 768 | .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB); |
| 769 | |
| 770 | F->DeleteMachineInstr(MI); // The pseudo instruction is gone now. |
| 771 | return BB; |
| 772 | } |