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