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