LLVM API Documentation

SystemZISelLowering.cpp

Go to the documentation of this file.
00001 //===-- SystemZISelLowering.cpp - SystemZ DAG Lowering Implementation  -----==//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the SystemZTargetLowering class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #define DEBUG_TYPE "systemz-lower"
00015 
00016 #include "SystemZISelLowering.h"
00017 #include "SystemZ.h"
00018 #include "SystemZTargetMachine.h"
00019 #include "SystemZSubtarget.h"
00020 #include "llvm/DerivedTypes.h"
00021 #include "llvm/Function.h"
00022 #include "llvm/Intrinsics.h"
00023 #include "llvm/CallingConv.h"
00024 #include "llvm/GlobalVariable.h"
00025 #include "llvm/GlobalAlias.h"
00026 #include "llvm/CodeGen/CallingConvLower.h"
00027 #include "llvm/CodeGen/MachineFrameInfo.h"
00028 #include "llvm/CodeGen/MachineFunction.h"
00029 #include "llvm/CodeGen/MachineInstrBuilder.h"
00030 #include "llvm/CodeGen/MachineRegisterInfo.h"
00031 #include "llvm/CodeGen/PseudoSourceValue.h"
00032 #include "llvm/CodeGen/SelectionDAGISel.h"
00033 #include "llvm/CodeGen/ValueTypes.h"
00034 #include "llvm/Target/TargetOptions.h"
00035 #include "llvm/Target/TargetLoweringObjectFile.h"
00036 #include "llvm/Support/Debug.h"
00037 #include "llvm/ADT/VectorExtras.h"
00038 using namespace llvm;
00039 
00040 SystemZTargetLowering::SystemZTargetLowering(SystemZTargetMachine &tm) :
00041   TargetLowering(tm, new TargetLoweringObjectFileELF()),
00042   Subtarget(*tm.getSubtargetImpl()), TM(tm) {
00043 
00044   RegInfo = TM.getRegisterInfo();
00045 
00046   // Set up the register classes.
00047   addRegisterClass(MVT::i32,  SystemZ::GR32RegisterClass);
00048   addRegisterClass(MVT::i64,  SystemZ::GR64RegisterClass);
00049   addRegisterClass(MVT::v2i32,SystemZ::GR64PRegisterClass);
00050   addRegisterClass(MVT::v2i64,SystemZ::GR128RegisterClass);
00051 
00052   if (!UseSoftFloat) {
00053     addRegisterClass(MVT::f32, SystemZ::FP32RegisterClass);
00054     addRegisterClass(MVT::f64, SystemZ::FP64RegisterClass);
00055 
00056     addLegalFPImmediate(APFloat(+0.0));  // lzer
00057     addLegalFPImmediate(APFloat(+0.0f)); // lzdr
00058     addLegalFPImmediate(APFloat(-0.0));  // lzer + lner
00059     addLegalFPImmediate(APFloat(-0.0f)); // lzdr + lndr
00060   }
00061 
00062   // Compute derived properties from the register classes
00063   computeRegisterProperties();
00064 
00065   // Set shifts properties
00066   setShiftAmountType(MVT::i64);
00067 
00068   // Provide all sorts of operation actions
00069   setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
00070   setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
00071   setLoadExtAction(ISD::EXTLOAD,  MVT::i1, Promote);
00072 
00073   setLoadExtAction(ISD::SEXTLOAD, MVT::f32, Expand);
00074   setLoadExtAction(ISD::ZEXTLOAD, MVT::f32, Expand);
00075   setLoadExtAction(ISD::EXTLOAD,  MVT::f32, Expand);
00076 
00077   setLoadExtAction(ISD::SEXTLOAD, MVT::f64, Expand);
00078   setLoadExtAction(ISD::ZEXTLOAD, MVT::f64, Expand);
00079   setLoadExtAction(ISD::EXTLOAD,  MVT::f64, Expand);
00080 
00081   setStackPointerRegisterToSaveRestore(SystemZ::R15D);
00082   setSchedulingPreference(SchedulingForLatency);
00083   setBooleanContents(ZeroOrOneBooleanContent);
00084 
00085   setOperationAction(ISD::BR_JT,            MVT::Other, Expand);
00086   setOperationAction(ISD::BRCOND,           MVT::Other, Expand);
00087   setOperationAction(ISD::BR_CC,            MVT::i32, Custom);
00088   setOperationAction(ISD::BR_CC,            MVT::i64, Custom);
00089   setOperationAction(ISD::BR_CC,            MVT::f32, Custom);
00090   setOperationAction(ISD::BR_CC,            MVT::f64, Custom);
00091   setOperationAction(ISD::ConstantPool,     MVT::i32, Custom);
00092   setOperationAction(ISD::ConstantPool,     MVT::i64, Custom);
00093   setOperationAction(ISD::GlobalAddress,    MVT::i64, Custom);
00094   setOperationAction(ISD::JumpTable,        MVT::i64, Custom);
00095   setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
00096 
00097   setOperationAction(ISD::SDIV,             MVT::i32, Expand);
00098   setOperationAction(ISD::UDIV,             MVT::i32, Expand);
00099   setOperationAction(ISD::SDIV,             MVT::i64, Expand);
00100   setOperationAction(ISD::UDIV,             MVT::i64, Expand);
00101   setOperationAction(ISD::SREM,             MVT::i32, Expand);
00102   setOperationAction(ISD::UREM,             MVT::i32, Expand);
00103   setOperationAction(ISD::SREM,             MVT::i64, Expand);
00104   setOperationAction(ISD::UREM,             MVT::i64, Expand);
00105 
00106   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
00107 
00108   setOperationAction(ISD::CTPOP,            MVT::i32, Expand);
00109   setOperationAction(ISD::CTPOP,            MVT::i64, Expand);
00110   setOperationAction(ISD::CTTZ,             MVT::i32, Expand);
00111   setOperationAction(ISD::CTTZ,             MVT::i64, Expand);
00112   setOperationAction(ISD::CTLZ,             MVT::i32, Promote);
00113   setOperationAction(ISD::CTLZ,             MVT::i64, Legal);
00114 
00115   // FIXME: Can we lower these 2 efficiently?
00116   setOperationAction(ISD::SETCC,            MVT::i32, Expand);
00117   setOperationAction(ISD::SETCC,            MVT::i64, Expand);
00118   setOperationAction(ISD::SETCC,            MVT::f32, Expand);
00119   setOperationAction(ISD::SETCC,            MVT::f64, Expand);
00120   setOperationAction(ISD::SELECT,           MVT::i32, Expand);
00121   setOperationAction(ISD::SELECT,           MVT::i64, Expand);
00122   setOperationAction(ISD::SELECT,           MVT::f32, Expand);
00123   setOperationAction(ISD::SELECT,           MVT::f64, Expand);
00124   setOperationAction(ISD::SELECT_CC,        MVT::i32, Custom);
00125   setOperationAction(ISD::SELECT_CC,        MVT::i64, Custom);
00126   setOperationAction(ISD::SELECT_CC,        MVT::f32, Custom);
00127   setOperationAction(ISD::SELECT_CC,        MVT::f64, Custom);
00128 
00129   setOperationAction(ISD::MULHS,            MVT::i64, Expand);
00130   setOperationAction(ISD::SMUL_LOHI,        MVT::i64, Expand);
00131 
00132   // FIXME: Can we support these natively?
00133   setOperationAction(ISD::UMUL_LOHI,        MVT::i64, Expand);
00134   setOperationAction(ISD::SRL_PARTS,        MVT::i64, Expand);
00135   setOperationAction(ISD::SHL_PARTS,        MVT::i64, Expand);
00136   setOperationAction(ISD::SRA_PARTS,        MVT::i64, Expand);
00137 
00138   // Lower some FP stuff
00139   setOperationAction(ISD::FSIN,             MVT::f32, Expand);
00140   setOperationAction(ISD::FSIN,             MVT::f64, Expand);
00141   setOperationAction(ISD::FCOS,             MVT::f32, Expand);
00142   setOperationAction(ISD::FCOS,             MVT::f64, Expand);
00143   setOperationAction(ISD::FREM,             MVT::f32, Expand);
00144   setOperationAction(ISD::FREM,             MVT::f64, Expand);
00145 
00146   // We have only 64-bit bitconverts
00147   setOperationAction(ISD::BIT_CONVERT,      MVT::f32, Expand);
00148   setOperationAction(ISD::BIT_CONVERT,      MVT::i32, Expand);
00149 
00150   setOperationAction(ISD::UINT_TO_FP,       MVT::i32, Expand);
00151   setOperationAction(ISD::UINT_TO_FP,       MVT::i64, Expand);
00152   setOperationAction(ISD::FP_TO_UINT,       MVT::i32, Expand);
00153   setOperationAction(ISD::FP_TO_UINT,       MVT::i64, Expand);
00154 
00155   setTruncStoreAction(MVT::f64, MVT::f32, Expand);
00156 }
00157 
00158 SDValue SystemZTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
00159   switch (Op.getOpcode()) {
00160   case ISD::BR_CC:            return LowerBR_CC(Op, DAG);
00161   case ISD::SELECT_CC:        return LowerSELECT_CC(Op, DAG);
00162   case ISD::GlobalAddress:    return LowerGlobalAddress(Op, DAG);
00163   case ISD::JumpTable:        return LowerJumpTable(Op, DAG);
00164   case ISD::ConstantPool:     return LowerConstantPool(Op, DAG);
00165   default:
00166     llvm_unreachable("Should not custom lower this!");
00167     return SDValue();
00168   }
00169 }
00170 
00171 //===----------------------------------------------------------------------===//
00172 //                       SystemZ Inline Assembly Support
00173 //===----------------------------------------------------------------------===//
00174 
00175 /// getConstraintType - Given a constraint letter, return the type of
00176 /// constraint it is for this target.
00177 TargetLowering::ConstraintType
00178 SystemZTargetLowering::getConstraintType(const std::string &Constraint) const {
00179   if (Constraint.size() == 1) {
00180     switch (Constraint[0]) {
00181     case 'r':
00182       return C_RegisterClass;
00183     default:
00184       break;
00185     }
00186   }
00187   return TargetLowering::getConstraintType(Constraint);
00188 }
00189 
00190 std::pair<unsigned, const TargetRegisterClass*>
00191 SystemZTargetLowering::
00192 getRegForInlineAsmConstraint(const std::string &Constraint,
00193                              EVT VT) const {
00194   if (Constraint.size() == 1) {
00195     // GCC Constraint Letters
00196     switch (Constraint[0]) {
00197     default: break;
00198     case 'r':   // GENERAL_REGS
00199       if (VT == MVT::i32)
00200         return std::make_pair(0U, SystemZ::GR32RegisterClass);
00201       else if (VT == MVT::i128)
00202         return std::make_pair(0U, SystemZ::GR128RegisterClass);
00203 
00204       return std::make_pair(0U, SystemZ::GR64RegisterClass);
00205     }
00206   }
00207 
00208   return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
00209 }
00210 
00211 //===----------------------------------------------------------------------===//
00212 //                      Calling Convention Implementation
00213 //===----------------------------------------------------------------------===//
00214 
00215 #include "SystemZGenCallingConv.inc"
00216 
00217 SDValue
00218 SystemZTargetLowering::LowerFormalArguments(SDValue Chain,
00219                                             unsigned CallConv,
00220                                             bool isVarArg,
00221                                             const SmallVectorImpl<ISD::InputArg>
00222                                               &Ins,
00223                                             DebugLoc dl,
00224                                             SelectionDAG &DAG,
00225                                             SmallVectorImpl<SDValue> &InVals) {
00226 
00227   switch (CallConv) {
00228   default:
00229     llvm_unreachable("Unsupported calling convention");
00230   case CallingConv::C:
00231   case CallingConv::Fast:
00232     return LowerCCCArguments(Chain, CallConv, isVarArg, Ins, dl, DAG, InVals);
00233   }
00234 }
00235 
00236 SDValue
00237 SystemZTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
00238                                  unsigned CallConv, bool isVarArg,
00239                                  bool isTailCall,
00240                                  const SmallVectorImpl<ISD::OutputArg> &Outs,
00241                                  const SmallVectorImpl<ISD::InputArg> &Ins,
00242                                  DebugLoc dl, SelectionDAG &DAG,
00243                                  SmallVectorImpl<SDValue> &InVals) {
00244 
00245   switch (CallConv) {
00246   default:
00247     llvm_unreachable("Unsupported calling convention");
00248   case CallingConv::Fast:
00249   case CallingConv::C:
00250     return LowerCCCCallTo(Chain, Callee, CallConv, isVarArg, isTailCall,
00251                           Outs, Ins, dl, DAG, InVals);
00252   }
00253 }
00254 
00255 /// LowerCCCArguments - transform physical registers into virtual registers and
00256 /// generate load operations for arguments places on the stack.
00257 // FIXME: struct return stuff
00258 // FIXME: varargs
00259 SDValue
00260 SystemZTargetLowering::LowerCCCArguments(SDValue Chain,
00261                                          unsigned CallConv,
00262                                          bool isVarArg,
00263                                          const SmallVectorImpl<ISD::InputArg>
00264                                            &Ins,
00265                                          DebugLoc dl,
00266                                          SelectionDAG &DAG,
00267                                          SmallVectorImpl<SDValue> &InVals) {
00268 
00269   MachineFunction &MF = DAG.getMachineFunction();
00270   MachineFrameInfo *MFI = MF.getFrameInfo();
00271   MachineRegisterInfo &RegInfo = MF.getRegInfo();
00272 
00273   // Assign locations to all of the incoming arguments.
00274   SmallVector<CCValAssign, 16> ArgLocs;
00275   CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
00276                  ArgLocs, *DAG.getContext());
00277   CCInfo.AnalyzeFormalArguments(Ins, CC_SystemZ);
00278 
00279   if (isVarArg)
00280     llvm_report_error("Varargs not supported yet");
00281 
00282   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
00283     SDValue ArgValue;
00284     CCValAssign &VA = ArgLocs[i];
00285     EVT LocVT = VA.getLocVT();
00286     if (VA.isRegLoc()) {
00287       // Arguments passed in registers
00288       TargetRegisterClass *RC;
00289       switch (LocVT.getSimpleVT().SimpleTy) {
00290       default:
00291 #ifndef NDEBUG
00292         cerr << "LowerFormalArguments Unhandled argument type: "
00293              << LocVT.getSimpleVT().SimpleTy
00294              << "\n";
00295 #endif
00296         llvm_unreachable(0);
00297       case MVT::i64:
00298         RC = SystemZ::GR64RegisterClass;
00299         break;
00300       case MVT::f32:
00301         RC = SystemZ::FP32RegisterClass;
00302         break;
00303       case MVT::f64:
00304         RC = SystemZ::FP64RegisterClass;
00305         break;
00306       }
00307 
00308       unsigned VReg = RegInfo.createVirtualRegister(RC);
00309       RegInfo.addLiveIn(VA.getLocReg(), VReg);
00310       ArgValue = DAG.getCopyFromReg(Chain, dl, VReg, LocVT);
00311     } else {
00312       // Sanity check
00313       assert(VA.isMemLoc());
00314 
00315       // Create the nodes corresponding to a load from this parameter slot.
00316       // Create the frame index object for this incoming parameter...
00317       int FI = MFI->CreateFixedObject(LocVT.getSizeInBits()/8,
00318                                       VA.getLocMemOffset());
00319 
00320       // Create the SelectionDAG nodes corresponding to a load
00321       // from this parameter
00322       SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
00323       ArgValue = DAG.getLoad(LocVT, dl, Chain, FIN,
00324                              PseudoSourceValue::getFixedStack(FI), 0);
00325     }
00326 
00327     // If this is an 8/16/32-bit value, it is really passed promoted to 64
00328     // bits. Insert an assert[sz]ext to capture this, then truncate to the
00329     // right size.
00330     if (VA.getLocInfo() == CCValAssign::SExt)
00331       ArgValue = DAG.getNode(ISD::AssertSext, dl, LocVT, ArgValue,
00332                              DAG.getValueType(VA.getValVT()));
00333     else if (VA.getLocInfo() == CCValAssign::ZExt)
00334       ArgValue = DAG.getNode(ISD::AssertZext, dl, LocVT, ArgValue,
00335                              DAG.getValueType(VA.getValVT()));
00336 
00337     if (VA.getLocInfo() != CCValAssign::Full)
00338       ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
00339 
00340     InVals.push_back(ArgValue);
00341   }
00342 
00343   return Chain;
00344 }
00345 
00346 /// LowerCCCCallTo - functions arguments are copied from virtual regs to
00347 /// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
00348 /// TODO: sret.
00349 SDValue
00350 SystemZTargetLowering::LowerCCCCallTo(SDValue Chain, SDValue Callee,
00351                                       unsigned CallConv, bool isVarArg,
00352                                       bool isTailCall,
00353                                       const SmallVectorImpl<ISD::OutputArg>
00354                                         &Outs,
00355                                       const SmallVectorImpl<ISD::InputArg> &Ins,
00356                                       DebugLoc dl, SelectionDAG &DAG,
00357                                       SmallVectorImpl<SDValue> &InVals) {
00358 
00359   MachineFunction &MF = DAG.getMachineFunction();
00360 
00361   // Offset to first argument stack slot.
00362   const unsigned FirstArgOffset = 160;
00363 
00364   // Analyze operands of the call, assigning locations to each operand.
00365   SmallVector<CCValAssign, 16> ArgLocs;
00366   CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
00367                  ArgLocs, *DAG.getContext());
00368 
00369   CCInfo.AnalyzeCallOperands(Outs, CC_SystemZ);
00370 
00371   // Get a count of how many bytes are to be pushed on the stack.
00372   unsigned NumBytes = CCInfo.getNextStackOffset();
00373 
00374   Chain = DAG.getCALLSEQ_START(Chain ,DAG.getConstant(NumBytes,
00375                                                       getPointerTy(), true));
00376 
00377   SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass;
00378   SmallVector<SDValue, 12> MemOpChains;
00379   SDValue StackPtr;
00380 
00381   // Walk the register/memloc assignments, inserting copies/loads.
00382   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
00383     CCValAssign &VA = ArgLocs[i];
00384 
00385     SDValue Arg = Outs[i].Val;
00386 
00387     // Promote the value if needed.
00388     switch (VA.getLocInfo()) {
00389       default: assert(0 && "Unknown loc info!");
00390       case CCValAssign::Full: break;
00391       case CCValAssign::SExt:
00392         Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
00393         break;
00394       case CCValAssign::ZExt:
00395         Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
00396         break;
00397       case CCValAssign::AExt:
00398         Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
00399         break;
00400     }
00401 
00402     // Arguments that can be passed on register must be kept at RegsToPass
00403     // vector
00404     if (VA.isRegLoc()) {
00405       RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
00406     } else {
00407       assert(VA.isMemLoc());
00408 
00409       if (StackPtr.getNode() == 0)
00410         StackPtr =
00411           DAG.getCopyFromReg(Chain, dl,
00412                              (RegInfo->hasFP(MF) ?
00413                               SystemZ::R11D : SystemZ::R15D),
00414                              getPointerTy());
00415 
00416       unsigned Offset = FirstArgOffset + VA.getLocMemOffset();
00417       SDValue PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(),
00418                                    StackPtr,
00419                                    DAG.getIntPtrConstant(Offset));
00420 
00421       MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
00422                                          PseudoSourceValue::getStack(), Offset));
00423     }
00424   }
00425 
00426   // Transform all store nodes into one single node because all store nodes are
00427   // independent of each other.
00428   if (!MemOpChains.empty())
00429     Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
00430                         &MemOpChains[0], MemOpChains.size());
00431 
00432   // Build a sequence of copy-to-reg nodes chained together with token chain and
00433   // flag operands which copy the outgoing args into registers.  The InFlag in
00434   // necessary since all emited instructions must be stuck together.
00435   SDValue InFlag;
00436   for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
00437     Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
00438                              RegsToPass[i].second, InFlag);
00439     InFlag = Chain.getValue(1);
00440   }
00441 
00442   // If the callee is a GlobalAddress node (quite common, every direct call is)
00443   // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
00444   // Likewise ExternalSymbol -> TargetExternalSymbol.
00445   if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
00446     Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
00447   else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
00448     Callee = DAG.getTargetExternalSymbol(E->getSymbol(), getPointerTy());
00449 
00450   // Returns a chain & a flag for retval copy to use.
00451   SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
00452   SmallVector<SDValue, 8> Ops;
00453   Ops.push_back(Chain);
00454   Ops.push_back(Callee);
00455 
00456   // Add argument registers to the end of the list so that they are
00457   // known live into the call.
00458   for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
00459     Ops.push_back(DAG.getRegister(RegsToPass[i].first,
00460                                   RegsToPass[i].second.getValueType()));
00461 
00462   if (InFlag.getNode())
00463     Ops.push_back(InFlag);
00464 
00465   Chain = DAG.getNode(SystemZISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
00466   InFlag = Chain.getValue(1);
00467 
00468   // Create the CALLSEQ_END node.
00469   Chain = DAG.getCALLSEQ_END(Chain,
00470                              DAG.getConstant(NumBytes, getPointerTy(), true),
00471                              DAG.getConstant(0, getPointerTy(), true),
00472                              InFlag);
00473   InFlag = Chain.getValue(1);
00474 
00475   // Handle result values, copying them out of physregs into vregs that we
00476   // return.
00477   return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl,
00478                          DAG, InVals);
00479 }
00480 
00481 /// LowerCallResult - Lower the result values of a call into the
00482 /// appropriate copies out of appropriate physical registers.
00483 ///
00484 SDValue
00485 SystemZTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
00486                                        unsigned CallConv, bool isVarArg,
00487                                        const SmallVectorImpl<ISD::InputArg>
00488                                          &Ins,
00489                                        DebugLoc dl, SelectionDAG &DAG,
00490                                        SmallVectorImpl<SDValue> &InVals) {
00491 
00492   // Assign locations to each value returned by this call.
00493   SmallVector<CCValAssign, 16> RVLocs;
00494   CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
00495                  *DAG.getContext());
00496 
00497   CCInfo.AnalyzeCallResult(Ins, RetCC_SystemZ);
00498 
00499   // Copy all of the result registers out of their specified physreg.
00500   for (unsigned i = 0; i != RVLocs.size(); ++i) {
00501     CCValAssign &VA = RVLocs[i];
00502 
00503     Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
00504                                VA.getLocVT(), InFlag).getValue(1);
00505     SDValue RetValue = Chain.getValue(0);
00506     InFlag = Chain.getValue(2);
00507 
00508     // If this is an 8/16/32-bit value, it is really passed promoted to 64
00509     // bits. Insert an assert[sz]ext to capture this, then truncate to the
00510     // right size.
00511     if (VA.getLocInfo() == CCValAssign::SExt)
00512       RetValue = DAG.getNode(ISD::AssertSext, dl, VA.getLocVT(), RetValue,
00513                              DAG.getValueType(VA.getValVT()));
00514     else if (VA.getLocInfo() == CCValAssign::ZExt)
00515       RetValue = DAG.getNode(ISD::AssertZext, dl, VA.getLocVT(), RetValue,
00516                              DAG.getValueType(VA.getValVT()));
00517 
00518     if (VA.getLocInfo() != CCValAssign::Full)
00519       RetValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), RetValue);
00520 
00521     InVals.push_back(RetValue);
00522   }
00523 
00524   return Chain;
00525 }
00526 
00527 
00528 SDValue
00529 SystemZTargetLowering::LowerReturn(SDValue Chain,
00530                                    unsigned CallConv, bool isVarArg,
00531                                    const SmallVectorImpl<ISD::OutputArg> &Outs,
00532                                    DebugLoc dl, SelectionDAG &DAG) {
00533 
00534   // CCValAssign - represent the assignment of the return value to a location
00535   SmallVector<CCValAssign, 16> RVLocs;
00536 
00537   // CCState - Info about the registers and stack slot.
00538   CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
00539                  RVLocs, *DAG.getContext());
00540 
00541   // Analize return values.
00542   CCInfo.AnalyzeReturn(Outs, RetCC_SystemZ);
00543 
00544   // If this is the first return lowered for this function, add the regs to the
00545   // liveout set for the function.
00546   if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
00547     for (unsigned i = 0; i != RVLocs.size(); ++i)
00548       if (RVLocs[i].isRegLoc())
00549         DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
00550   }
00551 
00552   SDValue Flag;
00553 
00554   // Copy the result values into the output registers.
00555   for (unsigned i = 0; i != RVLocs.size(); ++i) {
00556     CCValAssign &VA = RVLocs[i];
00557     SDValue ResValue = Outs[i].Val;
00558     assert(VA.isRegLoc() && "Can only return in registers!");
00559 
00560     // If this is an 8/16/32-bit value, it is really should be passed promoted
00561     // to 64 bits.
00562     if (VA.getLocInfo() == CCValAssign::SExt)
00563       ResValue = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ResValue);
00564     else if (VA.getLocInfo() == CCValAssign::ZExt)
00565       ResValue = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ResValue);
00566     else if (VA.getLocInfo() == CCValAssign::AExt)
00567       ResValue = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ResValue);
00568 
00569     Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ResValue, Flag);
00570 
00571     // Guarantee that all emitted copies are stuck together,
00572     // avoiding something bad.
00573     Flag = Chain.getValue(1);
00574   }
00575 
00576   if (Flag.getNode())
00577     return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
00578 
00579   // Return Void
00580   return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain);
00581 }
00582 
00583 SDValue SystemZTargetLowering::EmitCmp(SDValue LHS, SDValue RHS,
00584                                        ISD::CondCode CC, SDValue &SystemZCC,
00585                                        SelectionDAG &DAG) {
00586   // FIXME: Emit a test if RHS is zero
00587 
00588   bool isUnsigned = false;
00589   SystemZCC::CondCodes TCC;
00590   switch (CC) {
00591   default:
00592     llvm_unreachable("Invalid integer condition!");
00593   case ISD::SETEQ:
00594   case ISD::SETOEQ:
00595     TCC = SystemZCC::E;
00596     break;
00597   case ISD::SETUEQ:
00598     TCC = SystemZCC::NLH;
00599     break;
00600   case ISD::SETNE:
00601   case ISD::SETONE:
00602     TCC = SystemZCC::NE;
00603     break;
00604   case ISD::SETUNE:
00605     TCC = SystemZCC::LH;
00606     break;
00607   case ISD::SETO:
00608     TCC = SystemZCC::O;
00609     break;
00610   case ISD::SETUO:
00611     TCC = SystemZCC::NO;
00612     break;
00613   case ISD::SETULE:
00614     if (LHS.getValueType().isFloatingPoint()) {
00615       TCC = SystemZCC::NH;
00616       break;
00617     }
00618     isUnsigned = true;   // FALLTHROUGH
00619   case ISD::SETLE:
00620   case ISD::SETOLE:
00621     TCC = SystemZCC::LE;
00622     break;
00623   case ISD::SETUGE:
00624     if (LHS.getValueType().isFloatingPoint()) {
00625       TCC = SystemZCC::NL;
00626       break;
00627     }
00628     isUnsigned = true;   // FALLTHROUGH
00629   case ISD::SETGE:
00630   case ISD::SETOGE:
00631     TCC = SystemZCC::HE;
00632     break;
00633   case ISD::SETUGT:
00634     if (LHS.getValueType().isFloatingPoint()) {
00635       TCC = SystemZCC::NLE;
00636       break;
00637     }
00638     isUnsigned = true;  // FALLTHROUGH
00639   case ISD::SETGT:
00640   case ISD::SETOGT:
00641     TCC = SystemZCC::H;
00642     break;
00643   case ISD::SETULT:
00644     if (LHS.getValueType().isFloatingPoint()) {
00645       TCC = SystemZCC::NHE;
00646       break;
00647     }
00648     isUnsigned = true;  // FALLTHROUGH
00649   case ISD::SETLT:
00650   case ISD::SETOLT:
00651     TCC = SystemZCC::L;
00652     break;
00653   }
00654 
00655   SystemZCC = DAG.getConstant(TCC, MVT::i32);
00656 
00657   DebugLoc dl = LHS.getDebugLoc();
00658   return DAG.getNode((isUnsigned ? SystemZISD::UCMP : SystemZISD::CMP),
00659                      dl, MVT::Flag, LHS, RHS);
00660 }
00661 
00662 
00663 SDValue SystemZTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
00664   SDValue Chain = Op.getOperand(0);
00665   ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
00666   SDValue LHS   = Op.getOperand(2);
00667   SDValue RHS   = Op.getOperand(3);
00668   SDValue Dest  = Op.getOperand(4);
00669   DebugLoc dl   = Op.getDebugLoc();
00670 
00671   SDValue SystemZCC;
00672   SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG);
00673   return DAG.getNode(SystemZISD::BRCOND, dl, Op.getValueType(),
00674                      Chain, Dest, SystemZCC, Flag);
00675 }
00676 
00677 SDValue SystemZTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
00678   SDValue LHS    = Op.getOperand(0);
00679   SDValue RHS    = Op.getOperand(1);
00680   SDValue TrueV  = Op.getOperand(2);
00681   SDValue FalseV = Op.getOperand(3);
00682   ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
00683   DebugLoc dl   = Op.getDebugLoc();
00684 
00685   SDValue SystemZCC;
00686   SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG);
00687 
00688   SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
00689   SmallVector<SDValue, 4> Ops;
00690   Ops.push_back(TrueV);
00691   Ops.push_back(FalseV);
00692   Ops.push_back(SystemZCC);
00693   Ops.push_back(Flag);
00694 
00695   return DAG.getNode(SystemZISD::SELECT, dl, VTs, &Ops[0], Ops.size());
00696 }
00697 
00698 SDValue SystemZTargetLowering::LowerGlobalAddress(SDValue Op,
00699                                                   SelectionDAG &DAG) {
00700   DebugLoc dl = Op.getDebugLoc();
00701   GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
00702   int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
00703 
00704   bool IsPic = getTargetMachine().getRelocationModel() == Reloc::PIC_;
00705   bool ExtraLoadRequired =
00706     Subtarget.GVRequiresExtraLoad(GV, getTargetMachine(), false);
00707 
00708   SDValue Result;
00709   if (!IsPic && !ExtraLoadRequired) {
00710     Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
00711     Offset = 0;
00712   } else {
00713     unsigned char OpFlags = 0;
00714     if (ExtraLoadRequired)
00715       OpFlags = SystemZII::MO_GOTENT;
00716 
00717     Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0, OpFlags);
00718   }
00719 
00720   Result = DAG.getNode(SystemZISD::PCRelativeWrapper, dl,
00721                        getPointerTy(), Result);
00722 
00723   if (ExtraLoadRequired)
00724     Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
00725                          PseudoSourceValue::getGOT(), 0);
00726 
00727   // If there was a non-zero offset that we didn't fold, create an explicit
00728   // addition for it.
00729   if (Offset != 0)
00730     Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
00731                          DAG.getConstant(Offset, getPointerTy()));
00732 
00733   return Result;
00734 }
00735 
00736 // FIXME: PIC here
00737 SDValue SystemZTargetLowering::LowerJumpTable(SDValue Op,
00738                                               SelectionDAG &DAG) {
00739   DebugLoc dl = Op.getDebugLoc();
00740   JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
00741   SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
00742 
00743   return DAG.getNode(SystemZISD::PCRelativeWrapper, dl, getPointerTy(), Result);
00744 }
00745 
00746 
00747 // FIXME: PIC here
00748 // FIXME: This is just dirty hack. We need to lower cpool properly
00749 SDValue SystemZTargetLowering::LowerConstantPool(SDValue Op,
00750                                                  SelectionDAG &DAG) {
00751   DebugLoc dl = Op.getDebugLoc();
00752   ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
00753 
00754   SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
00755                                              CP->getAlignment(),
00756                                              CP->getOffset());
00757 
00758   return DAG.getNode(SystemZISD::PCRelativeWrapper, dl, getPointerTy(), Result);
00759 }
00760 
00761 const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
00762   switch (Opcode) {
00763   case SystemZISD::RET_FLAG:           return "SystemZISD::RET_FLAG";
00764   case SystemZISD::CALL:               return "SystemZISD::CALL";
00765   case SystemZISD::BRCOND:             return "SystemZISD::BRCOND";
00766   case SystemZISD::CMP:                return "SystemZISD::CMP";
00767   case SystemZISD::UCMP:               return "SystemZISD::UCMP";
00768   case SystemZISD::SELECT:             return "SystemZISD::SELECT";
00769   case SystemZISD::PCRelativeWrapper:  return "SystemZISD::PCRelativeWrapper";
00770   default: return NULL;
00771   }
00772 }
00773 
00774 //===----------------------------------------------------------------------===//
00775 //  Other Lowering Code
00776 //===----------------------------------------------------------------------===//
00777 
00778 MachineBasicBlock*
00779 SystemZTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
00780                                                    MachineBasicBlock *BB) const {
00781   const SystemZInstrInfo &TII = *TM.getInstrInfo();
00782   DebugLoc dl = MI->getDebugLoc();
00783   assert((MI->getOpcode() == SystemZ::Select32  ||
00784           MI->getOpcode() == SystemZ::SelectF32 ||
00785           MI->getOpcode() == SystemZ::Select64  ||
00786           MI->getOpcode() == SystemZ::SelectF64) &&
00787          "Unexpected instr type to insert");
00788 
00789   // To "insert" a SELECT instruction, we actually have to insert the diamond
00790   // control-flow pattern.  The incoming instruction knows the destination vreg
00791   // to set, the condition code register to branch on, the true/false values to
00792   // select between, and a branch opcode to use.
00793   const BasicBlock *LLVM_BB = BB->getBasicBlock();
00794   MachineFunction::iterator I = BB;
00795   ++I;
00796 
00797   //  thisMBB:
00798   //  ...
00799   //   TrueVal = ...
00800   //   cmpTY ccX, r1, r2
00801   //   jCC copy1MBB
00802   //   fallthrough --> copy0MBB
00803   MachineBasicBlock *thisMBB = BB;
00804   MachineFunction *F = BB->getParent();
00805   MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
00806   MachineBasicBlock *copy1MBB = F->CreateMachineBasicBlock(LLVM_BB);
00807   SystemZCC::CondCodes CC = (SystemZCC::CondCodes)MI->getOperand(3).getImm();
00808   BuildMI(BB, dl, TII.getBrCond(CC)).addMBB(copy1MBB);
00809   F->insert(I, copy0MBB);
00810   F->insert(I, copy1MBB);
00811   // Update machine-CFG edges by transferring all successors of the current
00812   // block to the new block which will contain the Phi node for the select.
00813   copy1MBB->transferSuccessors(BB);
00814   // Next, add the true and fallthrough blocks as its successors.
00815   BB->addSuccessor(copy0MBB);
00816   BB->addSuccessor(copy1MBB);
00817 
00818   //  copy0MBB:
00819   //   %FalseValue = ...
00820   //   # fallthrough to copy1MBB
00821   BB = copy0MBB;
00822 
00823   // Update machine-CFG edges
00824   BB->addSuccessor(copy1MBB);
00825 
00826   //  copy1MBB:
00827   //   %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
00828   //  ...
00829   BB = copy1MBB;
00830   BuildMI(BB, dl, TII.get(SystemZ::PHI),
00831           MI->getOperand(0).getReg())
00832     .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
00833     .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
00834 
00835   F->DeleteMachineInstr(MI);   // The pseudo instruction is gone now.
00836   return BB;
00837 }