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