Anton Korobeynikov | f2c3e17 | 2009-05-03 12:57:15 +0000 | [diff] [blame] | 1 | //===-- MSP430ISelLowering.cpp - MSP430 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 MSP430TargetLowering class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #define DEBUG_TYPE "msp430-lower" |
| 15 | |
| 16 | #include "MSP430ISelLowering.h" |
| 17 | #include "MSP430.h" |
| 18 | #include "MSP430TargetMachine.h" |
| 19 | #include "MSP430Subtarget.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" |
Anton Korobeynikov | c8fbb6a | 2009-05-03 12:59:33 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/PseudoSourceValue.h" |
Anton Korobeynikov | f2c3e17 | 2009-05-03 12:57:15 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/SelectionDAGISel.h" |
| 33 | #include "llvm/CodeGen/ValueTypes.h" |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 34 | #include "llvm/Target/TargetLoweringObjectFile.h" |
Anton Korobeynikov | f2c3e17 | 2009-05-03 12:57:15 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Debug.h" |
Torok Edwin | 804e0fe | 2009-07-08 19:04:27 +0000 | [diff] [blame] | 36 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | 4437ae2 | 2009-08-23 07:05:07 +0000 | [diff] [blame] | 37 | #include "llvm/Support/raw_ostream.h" |
Anton Korobeynikov | f2c3e17 | 2009-05-03 12:57:15 +0000 | [diff] [blame] | 38 | #include "llvm/ADT/VectorExtras.h" |
| 39 | using namespace llvm; |
| 40 | |
| 41 | MSP430TargetLowering::MSP430TargetLowering(MSP430TargetMachine &tm) : |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 42 | TargetLowering(tm, new TargetLoweringObjectFileELF()), |
| 43 | Subtarget(*tm.getSubtargetImpl()), TM(tm) { |
Anton Korobeynikov | f2c3e17 | 2009-05-03 12:57:15 +0000 | [diff] [blame] | 44 | |
| 45 | // Set up the register classes. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 46 | addRegisterClass(MVT::i8, MSP430::GR8RegisterClass); |
| 47 | addRegisterClass(MVT::i16, MSP430::GR16RegisterClass); |
Anton Korobeynikov | f2c3e17 | 2009-05-03 12:57:15 +0000 | [diff] [blame] | 48 | |
| 49 | // Compute derived properties from the register classes |
| 50 | computeRegisterProperties(); |
Anton Korobeynikov | fd1b7c7 | 2009-05-03 12:59:50 +0000 | [diff] [blame] | 51 | |
Anton Korobeynikov | 1476d97 | 2009-05-03 13:03:14 +0000 | [diff] [blame] | 52 | // Provide all sorts of operation actions |
| 53 | |
| 54 | // Division is expensive |
| 55 | setIntDivIsCheap(false); |
| 56 | |
Anton Korobeynikov | d2c94ae | 2009-05-03 13:03:33 +0000 | [diff] [blame] | 57 | // Even if we have only 1 bit shift here, we can perform |
| 58 | // shifts of the whole bitwidth 1 bit per step. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 59 | setShiftAmountType(MVT::i8); |
Anton Korobeynikov | d2c94ae | 2009-05-03 13:03:33 +0000 | [diff] [blame] | 60 | |
Anton Korobeynikov | c08163e | 2009-05-03 13:11:35 +0000 | [diff] [blame] | 61 | setStackPointerRegisterToSaveRestore(MSP430::SPW); |
| 62 | setBooleanContents(ZeroOrOneBooleanContent); |
| 63 | setSchedulingPreference(SchedulingForLatency); |
| 64 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 65 | setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote); |
| 66 | setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote); |
| 67 | setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote); |
| 68 | setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Expand); |
| 69 | setLoadExtAction(ISD::SEXTLOAD, MVT::i16, Expand); |
Anton Korobeynikov | 36b6e53 | 2009-05-03 13:06:03 +0000 | [diff] [blame] | 70 | |
Anton Korobeynikov | 54f30d3 | 2009-05-03 13:06:26 +0000 | [diff] [blame] | 71 | // We don't have any truncstores |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 72 | setTruncStoreAction(MVT::i16, MVT::i8, Expand); |
Anton Korobeynikov | 54f30d3 | 2009-05-03 13:06:26 +0000 | [diff] [blame] | 73 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 74 | setOperationAction(ISD::SRA, MVT::i8, Custom); |
| 75 | setOperationAction(ISD::SHL, MVT::i8, Custom); |
| 76 | setOperationAction(ISD::SRL, MVT::i8, Custom); |
| 77 | setOperationAction(ISD::SRA, MVT::i16, Custom); |
| 78 | setOperationAction(ISD::SHL, MVT::i16, Custom); |
| 79 | setOperationAction(ISD::SRL, MVT::i16, Custom); |
| 80 | setOperationAction(ISD::ROTL, MVT::i8, Expand); |
| 81 | setOperationAction(ISD::ROTR, MVT::i8, Expand); |
| 82 | setOperationAction(ISD::ROTL, MVT::i16, Expand); |
| 83 | setOperationAction(ISD::ROTR, MVT::i16, Expand); |
| 84 | setOperationAction(ISD::GlobalAddress, MVT::i16, Custom); |
| 85 | setOperationAction(ISD::ExternalSymbol, MVT::i16, Custom); |
| 86 | setOperationAction(ISD::BR_JT, MVT::Other, Expand); |
| 87 | setOperationAction(ISD::BRIND, MVT::Other, Expand); |
| 88 | setOperationAction(ISD::BR_CC, MVT::i8, Custom); |
| 89 | setOperationAction(ISD::BR_CC, MVT::i16, Custom); |
| 90 | setOperationAction(ISD::BRCOND, MVT::Other, Expand); |
| 91 | setOperationAction(ISD::SETCC, MVT::i8, Expand); |
| 92 | setOperationAction(ISD::SETCC, MVT::i16, Expand); |
| 93 | setOperationAction(ISD::SELECT, MVT::i8, Expand); |
| 94 | setOperationAction(ISD::SELECT, MVT::i16, Expand); |
| 95 | setOperationAction(ISD::SELECT_CC, MVT::i8, Custom); |
| 96 | setOperationAction(ISD::SELECT_CC, MVT::i16, Custom); |
| 97 | setOperationAction(ISD::SIGN_EXTEND, MVT::i16, Custom); |
Anton Korobeynikov | 379a087 | 2009-08-25 17:00:23 +0000 | [diff] [blame^] | 98 | setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i8, Expand); |
| 99 | setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i16, Expand); |
Anton Korobeynikov | 8725bd2 | 2009-05-03 13:14:25 +0000 | [diff] [blame] | 100 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 101 | setOperationAction(ISD::CTTZ, MVT::i8, Expand); |
| 102 | setOperationAction(ISD::CTTZ, MVT::i16, Expand); |
| 103 | setOperationAction(ISD::CTLZ, MVT::i8, Expand); |
| 104 | setOperationAction(ISD::CTLZ, MVT::i16, Expand); |
| 105 | setOperationAction(ISD::CTPOP, MVT::i8, Expand); |
| 106 | setOperationAction(ISD::CTPOP, MVT::i16, Expand); |
Eli Friedman | e4ce880 | 2009-07-17 07:28:06 +0000 | [diff] [blame] | 107 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 108 | setOperationAction(ISD::SHL_PARTS, MVT::i8, Expand); |
| 109 | setOperationAction(ISD::SHL_PARTS, MVT::i16, Expand); |
| 110 | setOperationAction(ISD::SRL_PARTS, MVT::i8, Expand); |
| 111 | setOperationAction(ISD::SRL_PARTS, MVT::i16, Expand); |
| 112 | setOperationAction(ISD::SRA_PARTS, MVT::i8, Expand); |
| 113 | setOperationAction(ISD::SRA_PARTS, MVT::i16, Expand); |
Eli Friedman | e4ce880 | 2009-07-17 07:28:06 +0000 | [diff] [blame] | 114 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 115 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand); |
Eli Friedman | e4ce880 | 2009-07-17 07:28:06 +0000 | [diff] [blame] | 116 | |
Anton Korobeynikov | 8725bd2 | 2009-05-03 13:14:25 +0000 | [diff] [blame] | 117 | // FIXME: Implement efficiently multiplication by a constant |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 118 | setOperationAction(ISD::MUL, MVT::i16, Expand); |
| 119 | setOperationAction(ISD::MULHS, MVT::i16, Expand); |
| 120 | setOperationAction(ISD::MULHU, MVT::i16, Expand); |
| 121 | setOperationAction(ISD::SMUL_LOHI, MVT::i16, Expand); |
| 122 | setOperationAction(ISD::UMUL_LOHI, MVT::i16, Expand); |
Anton Korobeynikov | f2f5402 | 2009-05-03 13:18:33 +0000 | [diff] [blame] | 123 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 124 | setOperationAction(ISD::UDIV, MVT::i16, Expand); |
| 125 | setOperationAction(ISD::UDIVREM, MVT::i16, Expand); |
| 126 | setOperationAction(ISD::UREM, MVT::i16, Expand); |
| 127 | setOperationAction(ISD::SDIV, MVT::i16, Expand); |
| 128 | setOperationAction(ISD::SDIVREM, MVT::i16, Expand); |
| 129 | setOperationAction(ISD::SREM, MVT::i16, Expand); |
Anton Korobeynikov | f2c3e17 | 2009-05-03 12:57:15 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Anton Korobeynikov | b8639f5 | 2009-05-03 13:03:50 +0000 | [diff] [blame] | 132 | SDValue MSP430TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) { |
Anton Korobeynikov | f2c3e17 | 2009-05-03 12:57:15 +0000 | [diff] [blame] | 133 | switch (Op.getOpcode()) { |
Anton Korobeynikov | ea54c98 | 2009-05-03 13:13:17 +0000 | [diff] [blame] | 134 | case ISD::SHL: // FALLTHROUGH |
Anton Korobeynikov | e699d0f | 2009-05-03 13:16:17 +0000 | [diff] [blame] | 135 | case ISD::SRL: |
Anton Korobeynikov | 4428885 | 2009-05-03 13:07:31 +0000 | [diff] [blame] | 136 | case ISD::SRA: return LowerShifts(Op, DAG); |
Anton Korobeynikov | 3513ca8 | 2009-05-03 13:08:33 +0000 | [diff] [blame] | 137 | case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG); |
Anton Korobeynikov | 5d59f68 | 2009-05-03 13:14:46 +0000 | [diff] [blame] | 138 | case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG); |
Anton Korobeynikov | 1bb8cd7 | 2009-05-03 13:19:09 +0000 | [diff] [blame] | 139 | case ISD::BR_CC: return LowerBR_CC(Op, DAG); |
| 140 | case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG); |
Anton Korobeynikov | b78e214 | 2009-05-03 13:17:49 +0000 | [diff] [blame] | 141 | case ISD::SIGN_EXTEND: return LowerSIGN_EXTEND(Op, DAG); |
Anton Korobeynikov | f2c3e17 | 2009-05-03 12:57:15 +0000 | [diff] [blame] | 142 | default: |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 143 | llvm_unreachable("unimplemented operand"); |
Anton Korobeynikov | f2c3e17 | 2009-05-03 12:57:15 +0000 | [diff] [blame] | 144 | return SDValue(); |
| 145 | } |
| 146 | } |
| 147 | |
Bill Wendling | b4202b8 | 2009-07-01 18:50:55 +0000 | [diff] [blame] | 148 | /// getFunctionAlignment - Return the Log2 alignment of this function. |
Bill Wendling | 20c568f | 2009-06-30 22:38:32 +0000 | [diff] [blame] | 149 | unsigned MSP430TargetLowering::getFunctionAlignment(const Function *F) const { |
| 150 | return F->hasFnAttr(Attribute::OptimizeForSize) ? 1 : 4; |
| 151 | } |
| 152 | |
Anton Korobeynikov | c8fbb6a | 2009-05-03 12:59:33 +0000 | [diff] [blame] | 153 | //===----------------------------------------------------------------------===// |
| 154 | // Calling Convention Implementation |
| 155 | //===----------------------------------------------------------------------===// |
| 156 | |
Anton Korobeynikov | f2c3e17 | 2009-05-03 12:57:15 +0000 | [diff] [blame] | 157 | #include "MSP430GenCallingConv.inc" |
Anton Korobeynikov | c8fbb6a | 2009-05-03 12:59:33 +0000 | [diff] [blame] | 158 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 159 | SDValue |
| 160 | MSP430TargetLowering::LowerFormalArguments(SDValue Chain, |
| 161 | unsigned CallConv, |
| 162 | bool isVarArg, |
| 163 | const SmallVectorImpl<ISD::InputArg> |
| 164 | &Ins, |
| 165 | DebugLoc dl, |
| 166 | SelectionDAG &DAG, |
| 167 | SmallVectorImpl<SDValue> &InVals) { |
| 168 | |
| 169 | switch (CallConv) { |
Anton Korobeynikov | c8fbb6a | 2009-05-03 12:59:33 +0000 | [diff] [blame] | 170 | default: |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 171 | llvm_unreachable("Unsupported calling convention"); |
Anton Korobeynikov | c8fbb6a | 2009-05-03 12:59:33 +0000 | [diff] [blame] | 172 | case CallingConv::C: |
| 173 | case CallingConv::Fast: |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 174 | return LowerCCCArguments(Chain, CallConv, isVarArg, Ins, dl, DAG, InVals); |
Anton Korobeynikov | c8fbb6a | 2009-05-03 12:59:33 +0000 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 178 | SDValue |
| 179 | MSP430TargetLowering::LowerCall(SDValue Chain, SDValue Callee, |
| 180 | unsigned CallConv, bool isVarArg, |
| 181 | bool isTailCall, |
| 182 | const SmallVectorImpl<ISD::OutputArg> &Outs, |
| 183 | const SmallVectorImpl<ISD::InputArg> &Ins, |
| 184 | DebugLoc dl, SelectionDAG &DAG, |
| 185 | SmallVectorImpl<SDValue> &InVals) { |
| 186 | |
| 187 | switch (CallConv) { |
Anton Korobeynikov | 4428885 | 2009-05-03 13:07:31 +0000 | [diff] [blame] | 188 | default: |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 189 | llvm_unreachable("Unsupported calling convention"); |
Anton Korobeynikov | 4428885 | 2009-05-03 13:07:31 +0000 | [diff] [blame] | 190 | case CallingConv::Fast: |
| 191 | case CallingConv::C: |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 192 | return LowerCCCCallTo(Chain, Callee, CallConv, isVarArg, isTailCall, |
| 193 | Outs, Ins, dl, DAG, InVals); |
Anton Korobeynikov | 4428885 | 2009-05-03 13:07:31 +0000 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
Anton Korobeynikov | c8fbb6a | 2009-05-03 12:59:33 +0000 | [diff] [blame] | 197 | /// LowerCCCArguments - transform physical registers into virtual registers and |
| 198 | /// generate load operations for arguments places on the stack. |
| 199 | // FIXME: struct return stuff |
| 200 | // FIXME: varargs |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 201 | SDValue |
| 202 | MSP430TargetLowering::LowerCCCArguments(SDValue Chain, |
| 203 | unsigned CallConv, |
| 204 | bool isVarArg, |
| 205 | const SmallVectorImpl<ISD::InputArg> |
| 206 | &Ins, |
| 207 | DebugLoc dl, |
| 208 | SelectionDAG &DAG, |
| 209 | SmallVectorImpl<SDValue> &InVals) { |
Anton Korobeynikov | c8fbb6a | 2009-05-03 12:59:33 +0000 | [diff] [blame] | 210 | MachineFunction &MF = DAG.getMachineFunction(); |
| 211 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 212 | MachineRegisterInfo &RegInfo = MF.getRegInfo(); |
Anton Korobeynikov | c8fbb6a | 2009-05-03 12:59:33 +0000 | [diff] [blame] | 213 | |
| 214 | // Assign locations to all of the incoming arguments. |
| 215 | SmallVector<CCValAssign, 16> ArgLocs; |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 216 | CCState CCInfo(CallConv, isVarArg, getTargetMachine(), |
| 217 | ArgLocs, *DAG.getContext()); |
| 218 | CCInfo.AnalyzeFormalArguments(Ins, CC_MSP430); |
Anton Korobeynikov | c8fbb6a | 2009-05-03 12:59:33 +0000 | [diff] [blame] | 219 | |
| 220 | assert(!isVarArg && "Varargs not supported yet"); |
| 221 | |
Anton Korobeynikov | c8fbb6a | 2009-05-03 12:59:33 +0000 | [diff] [blame] | 222 | for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { |
| 223 | CCValAssign &VA = ArgLocs[i]; |
| 224 | if (VA.isRegLoc()) { |
| 225 | // Arguments passed in registers |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 226 | EVT RegVT = VA.getLocVT(); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 227 | switch (RegVT.getSimpleVT().SimpleTy) { |
Torok Edwin | 804e0fe | 2009-07-08 19:04:27 +0000 | [diff] [blame] | 228 | default: |
| 229 | { |
Torok Edwin | dac237e | 2009-07-08 20:53:28 +0000 | [diff] [blame] | 230 | #ifndef NDEBUG |
Chris Lattner | 4437ae2 | 2009-08-23 07:05:07 +0000 | [diff] [blame] | 231 | errs() << "LowerFormalArguments Unhandled argument type: " |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 232 | << RegVT.getSimpleVT().SimpleTy << "\n"; |
Torok Edwin | dac237e | 2009-07-08 20:53:28 +0000 | [diff] [blame] | 233 | #endif |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 234 | llvm_unreachable(0); |
Torok Edwin | 804e0fe | 2009-07-08 19:04:27 +0000 | [diff] [blame] | 235 | } |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 236 | case MVT::i16: |
Anton Korobeynikov | c8fbb6a | 2009-05-03 12:59:33 +0000 | [diff] [blame] | 237 | unsigned VReg = |
Anton Korobeynikov | 1df221f | 2009-05-03 13:02:04 +0000 | [diff] [blame] | 238 | RegInfo.createVirtualRegister(MSP430::GR16RegisterClass); |
Anton Korobeynikov | c8fbb6a | 2009-05-03 12:59:33 +0000 | [diff] [blame] | 239 | RegInfo.addLiveIn(VA.getLocReg(), VReg); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 240 | SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, VReg, RegVT); |
Anton Korobeynikov | c8fbb6a | 2009-05-03 12:59:33 +0000 | [diff] [blame] | 241 | |
| 242 | // If this is an 8-bit value, it is really passed promoted to 16 |
| 243 | // bits. Insert an assert[sz]ext to capture this, then truncate to the |
| 244 | // right size. |
| 245 | if (VA.getLocInfo() == CCValAssign::SExt) |
| 246 | ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue, |
| 247 | DAG.getValueType(VA.getValVT())); |
| 248 | else if (VA.getLocInfo() == CCValAssign::ZExt) |
| 249 | ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue, |
| 250 | DAG.getValueType(VA.getValVT())); |
| 251 | |
| 252 | if (VA.getLocInfo() != CCValAssign::Full) |
| 253 | ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue); |
| 254 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 255 | InVals.push_back(ArgValue); |
Anton Korobeynikov | c8fbb6a | 2009-05-03 12:59:33 +0000 | [diff] [blame] | 256 | } |
| 257 | } else { |
| 258 | // Sanity check |
| 259 | assert(VA.isMemLoc()); |
| 260 | // Load the argument to a virtual register |
| 261 | unsigned ObjSize = VA.getLocVT().getSizeInBits()/8; |
| 262 | if (ObjSize > 2) { |
Chris Lattner | 4437ae2 | 2009-08-23 07:05:07 +0000 | [diff] [blame] | 263 | errs() << "LowerFormalArguments Unhandled argument type: " |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 264 | << VA.getLocVT().getSimpleVT().SimpleTy |
Anton Korobeynikov | c8fbb6a | 2009-05-03 12:59:33 +0000 | [diff] [blame] | 265 | << "\n"; |
| 266 | } |
| 267 | // Create the frame index object for this incoming parameter... |
| 268 | int FI = MFI->CreateFixedObject(ObjSize, VA.getLocMemOffset()); |
| 269 | |
| 270 | // Create the SelectionDAG nodes corresponding to a load |
| 271 | //from this parameter |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 272 | SDValue FIN = DAG.getFrameIndex(FI, MVT::i16); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 273 | InVals.push_back(DAG.getLoad(VA.getLocVT(), dl, Chain, FIN, |
| 274 | PseudoSourceValue::getFixedStack(FI), 0)); |
Anton Korobeynikov | c8fbb6a | 2009-05-03 12:59:33 +0000 | [diff] [blame] | 275 | } |
| 276 | } |
| 277 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 278 | return Chain; |
Anton Korobeynikov | c8fbb6a | 2009-05-03 12:59:33 +0000 | [diff] [blame] | 279 | } |
Anton Korobeynikov | fd1b7c7 | 2009-05-03 12:59:50 +0000 | [diff] [blame] | 280 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 281 | SDValue |
| 282 | MSP430TargetLowering::LowerReturn(SDValue Chain, |
| 283 | unsigned CallConv, bool isVarArg, |
| 284 | const SmallVectorImpl<ISD::OutputArg> &Outs, |
| 285 | DebugLoc dl, SelectionDAG &DAG) { |
| 286 | |
Anton Korobeynikov | fd1b7c7 | 2009-05-03 12:59:50 +0000 | [diff] [blame] | 287 | // CCValAssign - represent the assignment of the return value to a location |
| 288 | SmallVector<CCValAssign, 16> RVLocs; |
Anton Korobeynikov | fd1b7c7 | 2009-05-03 12:59:50 +0000 | [diff] [blame] | 289 | |
| 290 | // CCState - Info about the registers and stack slot. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 291 | CCState CCInfo(CallConv, isVarArg, getTargetMachine(), |
| 292 | RVLocs, *DAG.getContext()); |
Anton Korobeynikov | fd1b7c7 | 2009-05-03 12:59:50 +0000 | [diff] [blame] | 293 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 294 | // Analize return values. |
| 295 | CCInfo.AnalyzeReturn(Outs, RetCC_MSP430); |
Anton Korobeynikov | fd1b7c7 | 2009-05-03 12:59:50 +0000 | [diff] [blame] | 296 | |
| 297 | // If this is the first return lowered for this function, add the regs to the |
| 298 | // liveout set for the function. |
| 299 | if (DAG.getMachineFunction().getRegInfo().liveout_empty()) { |
| 300 | for (unsigned i = 0; i != RVLocs.size(); ++i) |
| 301 | if (RVLocs[i].isRegLoc()) |
| 302 | DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg()); |
| 303 | } |
| 304 | |
Anton Korobeynikov | fd1b7c7 | 2009-05-03 12:59:50 +0000 | [diff] [blame] | 305 | SDValue Flag; |
| 306 | |
| 307 | // Copy the result values into the output registers. |
| 308 | for (unsigned i = 0; i != RVLocs.size(); ++i) { |
| 309 | CCValAssign &VA = RVLocs[i]; |
| 310 | assert(VA.isRegLoc() && "Can only return in registers!"); |
| 311 | |
Anton Korobeynikov | fd1b7c7 | 2009-05-03 12:59:50 +0000 | [diff] [blame] | 312 | Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 313 | Outs[i].Val, Flag); |
Anton Korobeynikov | fd1b7c7 | 2009-05-03 12:59:50 +0000 | [diff] [blame] | 314 | |
Anton Korobeynikov | dcb802c | 2009-05-03 13:00:11 +0000 | [diff] [blame] | 315 | // Guarantee that all emitted copies are stuck together, |
| 316 | // avoiding something bad. |
Anton Korobeynikov | fd1b7c7 | 2009-05-03 12:59:50 +0000 | [diff] [blame] | 317 | Flag = Chain.getValue(1); |
| 318 | } |
| 319 | |
| 320 | if (Flag.getNode()) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 321 | return DAG.getNode(MSP430ISD::RET_FLAG, dl, MVT::Other, Chain, Flag); |
Anton Korobeynikov | fd1b7c7 | 2009-05-03 12:59:50 +0000 | [diff] [blame] | 322 | |
| 323 | // Return Void |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 324 | return DAG.getNode(MSP430ISD::RET_FLAG, dl, MVT::Other, Chain); |
Anton Korobeynikov | fd1b7c7 | 2009-05-03 12:59:50 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Anton Korobeynikov | 4428885 | 2009-05-03 13:07:31 +0000 | [diff] [blame] | 327 | /// LowerCCCCallTo - functions arguments are copied from virtual regs to |
| 328 | /// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted. |
| 329 | /// TODO: sret. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 330 | SDValue |
| 331 | MSP430TargetLowering::LowerCCCCallTo(SDValue Chain, SDValue Callee, |
| 332 | unsigned CallConv, bool isVarArg, |
| 333 | bool isTailCall, |
| 334 | const SmallVectorImpl<ISD::OutputArg> |
| 335 | &Outs, |
| 336 | const SmallVectorImpl<ISD::InputArg> &Ins, |
| 337 | DebugLoc dl, SelectionDAG &DAG, |
| 338 | SmallVectorImpl<SDValue> &InVals) { |
Anton Korobeynikov | 4428885 | 2009-05-03 13:07:31 +0000 | [diff] [blame] | 339 | // Analyze operands of the call, assigning locations to each operand. |
| 340 | SmallVector<CCValAssign, 16> ArgLocs; |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 341 | CCState CCInfo(CallConv, isVarArg, getTargetMachine(), |
| 342 | ArgLocs, *DAG.getContext()); |
Anton Korobeynikov | 4428885 | 2009-05-03 13:07:31 +0000 | [diff] [blame] | 343 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 344 | CCInfo.AnalyzeCallOperands(Outs, CC_MSP430); |
Anton Korobeynikov | 4428885 | 2009-05-03 13:07:31 +0000 | [diff] [blame] | 345 | |
| 346 | // Get a count of how many bytes are to be pushed on the stack. |
| 347 | unsigned NumBytes = CCInfo.getNextStackOffset(); |
| 348 | |
| 349 | Chain = DAG.getCALLSEQ_START(Chain ,DAG.getConstant(NumBytes, |
| 350 | getPointerTy(), true)); |
| 351 | |
| 352 | SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass; |
| 353 | SmallVector<SDValue, 12> MemOpChains; |
| 354 | SDValue StackPtr; |
| 355 | |
| 356 | // Walk the register/memloc assignments, inserting copies/loads. |
| 357 | for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { |
| 358 | CCValAssign &VA = ArgLocs[i]; |
| 359 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 360 | SDValue Arg = Outs[i].Val; |
Anton Korobeynikov | 4428885 | 2009-05-03 13:07:31 +0000 | [diff] [blame] | 361 | |
| 362 | // Promote the value if needed. |
| 363 | switch (VA.getLocInfo()) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 364 | default: llvm_unreachable("Unknown loc info!"); |
Anton Korobeynikov | 4428885 | 2009-05-03 13:07:31 +0000 | [diff] [blame] | 365 | case CCValAssign::Full: break; |
| 366 | case CCValAssign::SExt: |
| 367 | Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg); |
| 368 | break; |
| 369 | case CCValAssign::ZExt: |
| 370 | Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg); |
| 371 | break; |
| 372 | case CCValAssign::AExt: |
| 373 | Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg); |
| 374 | break; |
| 375 | } |
| 376 | |
| 377 | // Arguments that can be passed on register must be kept at RegsToPass |
| 378 | // vector |
| 379 | if (VA.isRegLoc()) { |
| 380 | RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg)); |
| 381 | } else { |
| 382 | assert(VA.isMemLoc()); |
| 383 | |
| 384 | if (StackPtr.getNode() == 0) |
| 385 | StackPtr = DAG.getCopyFromReg(Chain, dl, MSP430::SPW, getPointerTy()); |
| 386 | |
| 387 | SDValue PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), |
| 388 | StackPtr, |
| 389 | DAG.getIntPtrConstant(VA.getLocMemOffset())); |
| 390 | |
| 391 | |
| 392 | MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff, |
| 393 | PseudoSourceValue::getStack(), |
| 394 | VA.getLocMemOffset())); |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | // Transform all store nodes into one single node because all store nodes are |
| 399 | // independent of each other. |
| 400 | if (!MemOpChains.empty()) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 401 | Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, |
Anton Korobeynikov | 4428885 | 2009-05-03 13:07:31 +0000 | [diff] [blame] | 402 | &MemOpChains[0], MemOpChains.size()); |
| 403 | |
| 404 | // Build a sequence of copy-to-reg nodes chained together with token chain and |
| 405 | // flag operands which copy the outgoing args into registers. The InFlag in |
| 406 | // necessary since all emited instructions must be stuck together. |
| 407 | SDValue InFlag; |
| 408 | for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { |
| 409 | Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first, |
| 410 | RegsToPass[i].second, InFlag); |
| 411 | InFlag = Chain.getValue(1); |
| 412 | } |
| 413 | |
| 414 | // If the callee is a GlobalAddress node (quite common, every direct call is) |
| 415 | // turn it into a TargetGlobalAddress node so that legalize doesn't hack it. |
| 416 | // Likewise ExternalSymbol -> TargetExternalSymbol. |
| 417 | if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 418 | Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i16); |
Anton Korobeynikov | 4428885 | 2009-05-03 13:07:31 +0000 | [diff] [blame] | 419 | else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee)) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 420 | Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i16); |
Anton Korobeynikov | 4428885 | 2009-05-03 13:07:31 +0000 | [diff] [blame] | 421 | |
| 422 | // Returns a chain & a flag for retval copy to use. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 423 | SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag); |
Anton Korobeynikov | 4428885 | 2009-05-03 13:07:31 +0000 | [diff] [blame] | 424 | SmallVector<SDValue, 8> Ops; |
| 425 | Ops.push_back(Chain); |
| 426 | Ops.push_back(Callee); |
| 427 | |
| 428 | // Add argument registers to the end of the list so that they are |
| 429 | // known live into the call. |
| 430 | for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) |
| 431 | Ops.push_back(DAG.getRegister(RegsToPass[i].first, |
| 432 | RegsToPass[i].second.getValueType())); |
| 433 | |
| 434 | if (InFlag.getNode()) |
| 435 | Ops.push_back(InFlag); |
| 436 | |
| 437 | Chain = DAG.getNode(MSP430ISD::CALL, dl, NodeTys, &Ops[0], Ops.size()); |
| 438 | InFlag = Chain.getValue(1); |
| 439 | |
| 440 | // Create the CALLSEQ_END node. |
| 441 | Chain = DAG.getCALLSEQ_END(Chain, |
| 442 | DAG.getConstant(NumBytes, getPointerTy(), true), |
| 443 | DAG.getConstant(0, getPointerTy(), true), |
| 444 | InFlag); |
| 445 | InFlag = Chain.getValue(1); |
| 446 | |
| 447 | // Handle result values, copying them out of physregs into vregs that we |
| 448 | // return. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 449 | return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, |
| 450 | DAG, InVals); |
Anton Korobeynikov | 4428885 | 2009-05-03 13:07:31 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 453 | /// LowerCallResult - Lower the result values of a call into the |
| 454 | /// appropriate copies out of appropriate physical registers. |
| 455 | /// |
| 456 | SDValue |
Anton Korobeynikov | 4428885 | 2009-05-03 13:07:31 +0000 | [diff] [blame] | 457 | MSP430TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag, |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 458 | unsigned CallConv, bool isVarArg, |
| 459 | const SmallVectorImpl<ISD::InputArg> &Ins, |
| 460 | DebugLoc dl, SelectionDAG &DAG, |
| 461 | SmallVectorImpl<SDValue> &InVals) { |
Anton Korobeynikov | 4428885 | 2009-05-03 13:07:31 +0000 | [diff] [blame] | 462 | |
| 463 | // Assign locations to each value returned by this call. |
| 464 | SmallVector<CCValAssign, 16> RVLocs; |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 465 | CCState CCInfo(CallConv, isVarArg, getTargetMachine(), |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 466 | RVLocs, *DAG.getContext()); |
Anton Korobeynikov | 4428885 | 2009-05-03 13:07:31 +0000 | [diff] [blame] | 467 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 468 | CCInfo.AnalyzeCallResult(Ins, RetCC_MSP430); |
Anton Korobeynikov | 4428885 | 2009-05-03 13:07:31 +0000 | [diff] [blame] | 469 | |
| 470 | // Copy all of the result registers out of their specified physreg. |
| 471 | for (unsigned i = 0; i != RVLocs.size(); ++i) { |
| 472 | Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(), |
| 473 | RVLocs[i].getValVT(), InFlag).getValue(1); |
| 474 | InFlag = Chain.getValue(2); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 475 | InVals.push_back(Chain.getValue(0)); |
Anton Korobeynikov | 4428885 | 2009-05-03 13:07:31 +0000 | [diff] [blame] | 476 | } |
| 477 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 478 | return Chain; |
Anton Korobeynikov | 4428885 | 2009-05-03 13:07:31 +0000 | [diff] [blame] | 479 | } |
| 480 | |
Anton Korobeynikov | d2c94ae | 2009-05-03 13:03:33 +0000 | [diff] [blame] | 481 | SDValue MSP430TargetLowering::LowerShifts(SDValue Op, |
| 482 | SelectionDAG &DAG) { |
Anton Korobeynikov | ea54c98 | 2009-05-03 13:13:17 +0000 | [diff] [blame] | 483 | unsigned Opc = Op.getOpcode(); |
Anton Korobeynikov | d2c94ae | 2009-05-03 13:03:33 +0000 | [diff] [blame] | 484 | SDNode* N = Op.getNode(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 485 | EVT VT = Op.getValueType(); |
Anton Korobeynikov | d2c94ae | 2009-05-03 13:03:33 +0000 | [diff] [blame] | 486 | DebugLoc dl = N->getDebugLoc(); |
| 487 | |
Anton Korobeynikov | ea54c98 | 2009-05-03 13:13:17 +0000 | [diff] [blame] | 488 | // We currently only lower shifts of constant argument. |
Anton Korobeynikov | d2c94ae | 2009-05-03 13:03:33 +0000 | [diff] [blame] | 489 | if (!isa<ConstantSDNode>(N->getOperand(1))) |
| 490 | return SDValue(); |
| 491 | |
| 492 | uint64_t ShiftAmount = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue(); |
| 493 | |
| 494 | // Expand the stuff into sequence of shifts. |
| 495 | // FIXME: for some shift amounts this might be done better! |
| 496 | // E.g.: foo >> (8 + N) => sxt(swpb(foo)) >> N |
| 497 | SDValue Victim = N->getOperand(0); |
Anton Korobeynikov | e699d0f | 2009-05-03 13:16:17 +0000 | [diff] [blame] | 498 | |
| 499 | if (Opc == ISD::SRL && ShiftAmount) { |
| 500 | // Emit a special goodness here: |
| 501 | // srl A, 1 => clrc; rrc A |
Anton Korobeynikov | bf8ef3f | 2009-05-03 13:16:37 +0000 | [diff] [blame] | 502 | Victim = DAG.getNode(MSP430ISD::RRC, dl, VT, Victim); |
Anton Korobeynikov | e699d0f | 2009-05-03 13:16:17 +0000 | [diff] [blame] | 503 | ShiftAmount -= 1; |
| 504 | } |
| 505 | |
Anton Korobeynikov | d2c94ae | 2009-05-03 13:03:33 +0000 | [diff] [blame] | 506 | while (ShiftAmount--) |
Anton Korobeynikov | aceb620 | 2009-05-17 10:15:22 +0000 | [diff] [blame] | 507 | Victim = DAG.getNode((Opc == ISD::SHL ? MSP430ISD::RLA : MSP430ISD::RRA), |
Anton Korobeynikov | ea54c98 | 2009-05-03 13:13:17 +0000 | [diff] [blame] | 508 | dl, VT, Victim); |
Anton Korobeynikov | d2c94ae | 2009-05-03 13:03:33 +0000 | [diff] [blame] | 509 | |
| 510 | return Victim; |
| 511 | } |
| 512 | |
Anton Korobeynikov | 3513ca8 | 2009-05-03 13:08:33 +0000 | [diff] [blame] | 513 | SDValue MSP430TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) { |
| 514 | const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal(); |
| 515 | int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset(); |
| 516 | |
| 517 | // Create the TargetGlobalAddress node, folding in the constant offset. |
| 518 | SDValue Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset); |
| 519 | return DAG.getNode(MSP430ISD::Wrapper, Op.getDebugLoc(), |
| 520 | getPointerTy(), Result); |
| 521 | } |
| 522 | |
Anton Korobeynikov | 5d59f68 | 2009-05-03 13:14:46 +0000 | [diff] [blame] | 523 | SDValue MSP430TargetLowering::LowerExternalSymbol(SDValue Op, |
| 524 | SelectionDAG &DAG) { |
| 525 | DebugLoc dl = Op.getDebugLoc(); |
| 526 | const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol(); |
| 527 | SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy()); |
| 528 | |
| 529 | return DAG.getNode(MSP430ISD::Wrapper, dl, getPointerTy(), Result);; |
| 530 | } |
| 531 | |
Anton Korobeynikov | 1bb8cd7 | 2009-05-03 13:19:09 +0000 | [diff] [blame] | 532 | static SDValue EmitCMP(SDValue &LHS, SDValue &RHS, unsigned &TargetCC, |
| 533 | ISD::CondCode CC, |
| 534 | DebugLoc dl, SelectionDAG &DAG) { |
Anton Korobeynikov | ed1a51a | 2009-05-03 13:12:06 +0000 | [diff] [blame] | 535 | // FIXME: Handle bittests someday |
| 536 | assert(!LHS.getValueType().isFloatingPoint() && "We don't handle FP yet"); |
| 537 | |
| 538 | // FIXME: Handle jump negative someday |
Anton Korobeynikov | 1bb8cd7 | 2009-05-03 13:19:09 +0000 | [diff] [blame] | 539 | TargetCC = MSP430::COND_INVALID; |
Anton Korobeynikov | ed1a51a | 2009-05-03 13:12:06 +0000 | [diff] [blame] | 540 | switch (CC) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 541 | default: llvm_unreachable("Invalid integer condition!"); |
Anton Korobeynikov | ed1a51a | 2009-05-03 13:12:06 +0000 | [diff] [blame] | 542 | case ISD::SETEQ: |
| 543 | TargetCC = MSP430::COND_E; // aka COND_Z |
| 544 | break; |
| 545 | case ISD::SETNE: |
| 546 | TargetCC = MSP430::COND_NE; // aka COND_NZ |
| 547 | break; |
| 548 | case ISD::SETULE: |
| 549 | std::swap(LHS, RHS); // FALLTHROUGH |
| 550 | case ISD::SETUGE: |
| 551 | TargetCC = MSP430::COND_HS; // aka COND_C |
| 552 | break; |
| 553 | case ISD::SETUGT: |
| 554 | std::swap(LHS, RHS); // FALLTHROUGH |
| 555 | case ISD::SETULT: |
| 556 | TargetCC = MSP430::COND_LO; // aka COND_NC |
| 557 | break; |
| 558 | case ISD::SETLE: |
| 559 | std::swap(LHS, RHS); // FALLTHROUGH |
| 560 | case ISD::SETGE: |
| 561 | TargetCC = MSP430::COND_GE; |
| 562 | break; |
| 563 | case ISD::SETGT: |
| 564 | std::swap(LHS, RHS); // FALLTHROUGH |
| 565 | case ISD::SETLT: |
| 566 | TargetCC = MSP430::COND_L; |
| 567 | break; |
| 568 | } |
| 569 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 570 | return DAG.getNode(MSP430ISD::CMP, dl, MVT::Flag, LHS, RHS); |
Anton Korobeynikov | ed1a51a | 2009-05-03 13:12:06 +0000 | [diff] [blame] | 571 | } |
| 572 | |
Anton Korobeynikov | 1bb8cd7 | 2009-05-03 13:19:09 +0000 | [diff] [blame] | 573 | |
| 574 | SDValue MSP430TargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) { |
Anton Korobeynikov | ed1a51a | 2009-05-03 13:12:06 +0000 | [diff] [blame] | 575 | SDValue Chain = Op.getOperand(0); |
Anton Korobeynikov | 1bb8cd7 | 2009-05-03 13:19:09 +0000 | [diff] [blame] | 576 | ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get(); |
| 577 | SDValue LHS = Op.getOperand(2); |
| 578 | SDValue RHS = Op.getOperand(3); |
| 579 | SDValue Dest = Op.getOperand(4); |
| 580 | DebugLoc dl = Op.getDebugLoc(); |
Anton Korobeynikov | ed1a51a | 2009-05-03 13:12:06 +0000 | [diff] [blame] | 581 | |
Anton Korobeynikov | 1bb8cd7 | 2009-05-03 13:19:09 +0000 | [diff] [blame] | 582 | unsigned TargetCC = MSP430::COND_INVALID; |
| 583 | SDValue Flag = EmitCMP(LHS, RHS, TargetCC, CC, dl, DAG); |
Anton Korobeynikov | ed1a51a | 2009-05-03 13:12:06 +0000 | [diff] [blame] | 584 | |
Anton Korobeynikov | 1bb8cd7 | 2009-05-03 13:19:09 +0000 | [diff] [blame] | 585 | return DAG.getNode(MSP430ISD::BR_CC, dl, Op.getValueType(), |
| 586 | Chain, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 587 | Dest, DAG.getConstant(TargetCC, MVT::i8), |
Anton Korobeynikov | 1bb8cd7 | 2009-05-03 13:19:09 +0000 | [diff] [blame] | 588 | Flag); |
Anton Korobeynikov | ed1a51a | 2009-05-03 13:12:06 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Anton Korobeynikov | 1bb8cd7 | 2009-05-03 13:19:09 +0000 | [diff] [blame] | 591 | SDValue MSP430TargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) { |
| 592 | SDValue LHS = Op.getOperand(0); |
| 593 | SDValue RHS = Op.getOperand(1); |
| 594 | SDValue TrueV = Op.getOperand(2); |
| 595 | SDValue FalseV = Op.getOperand(3); |
| 596 | ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get(); |
Anton Korobeynikov | 8b528e5 | 2009-05-03 13:12:23 +0000 | [diff] [blame] | 597 | DebugLoc dl = Op.getDebugLoc(); |
Anton Korobeynikov | 8b528e5 | 2009-05-03 13:12:23 +0000 | [diff] [blame] | 598 | |
Anton Korobeynikov | 1bb8cd7 | 2009-05-03 13:19:09 +0000 | [diff] [blame] | 599 | unsigned TargetCC = MSP430::COND_INVALID; |
| 600 | SDValue Flag = EmitCMP(LHS, RHS, TargetCC, CC, dl, DAG); |
Anton Korobeynikov | 8b528e5 | 2009-05-03 13:12:23 +0000 | [diff] [blame] | 601 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 602 | SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag); |
Anton Korobeynikov | 8b528e5 | 2009-05-03 13:12:23 +0000 | [diff] [blame] | 603 | SmallVector<SDValue, 4> Ops; |
| 604 | Ops.push_back(TrueV); |
| 605 | Ops.push_back(FalseV); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 606 | Ops.push_back(DAG.getConstant(TargetCC, MVT::i8)); |
Anton Korobeynikov | 1bb8cd7 | 2009-05-03 13:19:09 +0000 | [diff] [blame] | 607 | Ops.push_back(Flag); |
Anton Korobeynikov | 8b528e5 | 2009-05-03 13:12:23 +0000 | [diff] [blame] | 608 | |
Anton Korobeynikov | 1bb8cd7 | 2009-05-03 13:19:09 +0000 | [diff] [blame] | 609 | return DAG.getNode(MSP430ISD::SELECT_CC, dl, VTs, &Ops[0], Ops.size()); |
Anton Korobeynikov | 8b528e5 | 2009-05-03 13:12:23 +0000 | [diff] [blame] | 610 | } |
| 611 | |
Anton Korobeynikov | b78e214 | 2009-05-03 13:17:49 +0000 | [diff] [blame] | 612 | SDValue MSP430TargetLowering::LowerSIGN_EXTEND(SDValue Op, |
| 613 | SelectionDAG &DAG) { |
| 614 | SDValue Val = Op.getOperand(0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 615 | EVT VT = Op.getValueType(); |
Anton Korobeynikov | b78e214 | 2009-05-03 13:17:49 +0000 | [diff] [blame] | 616 | DebugLoc dl = Op.getDebugLoc(); |
| 617 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 618 | assert(VT == MVT::i16 && "Only support i16 for now!"); |
Anton Korobeynikov | b78e214 | 2009-05-03 13:17:49 +0000 | [diff] [blame] | 619 | |
| 620 | return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, VT, |
| 621 | DAG.getNode(ISD::ANY_EXTEND, dl, VT, Val), |
| 622 | DAG.getValueType(Val.getValueType())); |
| 623 | } |
| 624 | |
Anton Korobeynikov | fd1b7c7 | 2009-05-03 12:59:50 +0000 | [diff] [blame] | 625 | const char *MSP430TargetLowering::getTargetNodeName(unsigned Opcode) const { |
| 626 | switch (Opcode) { |
| 627 | default: return NULL; |
| 628 | case MSP430ISD::RET_FLAG: return "MSP430ISD::RET_FLAG"; |
Anton Korobeynikov | d2c94ae | 2009-05-03 13:03:33 +0000 | [diff] [blame] | 629 | case MSP430ISD::RRA: return "MSP430ISD::RRA"; |
Anton Korobeynikov | e699d0f | 2009-05-03 13:16:17 +0000 | [diff] [blame] | 630 | case MSP430ISD::RLA: return "MSP430ISD::RLA"; |
| 631 | case MSP430ISD::RRC: return "MSP430ISD::RRC"; |
Anton Korobeynikov | b561264 | 2009-05-03 13:07:54 +0000 | [diff] [blame] | 632 | case MSP430ISD::CALL: return "MSP430ISD::CALL"; |
Anton Korobeynikov | 3513ca8 | 2009-05-03 13:08:33 +0000 | [diff] [blame] | 633 | case MSP430ISD::Wrapper: return "MSP430ISD::Wrapper"; |
Anton Korobeynikov | 1bb8cd7 | 2009-05-03 13:19:09 +0000 | [diff] [blame] | 634 | case MSP430ISD::BR_CC: return "MSP430ISD::BR_CC"; |
Anton Korobeynikov | ed1a51a | 2009-05-03 13:12:06 +0000 | [diff] [blame] | 635 | case MSP430ISD::CMP: return "MSP430ISD::CMP"; |
Anton Korobeynikov | 1bb8cd7 | 2009-05-03 13:19:09 +0000 | [diff] [blame] | 636 | case MSP430ISD::SELECT_CC: return "MSP430ISD::SELECT_CC"; |
Anton Korobeynikov | fd1b7c7 | 2009-05-03 12:59:50 +0000 | [diff] [blame] | 637 | } |
| 638 | } |
Anton Korobeynikov | 8b528e5 | 2009-05-03 13:12:23 +0000 | [diff] [blame] | 639 | |
| 640 | //===----------------------------------------------------------------------===// |
| 641 | // Other Lowering Code |
| 642 | //===----------------------------------------------------------------------===// |
| 643 | |
| 644 | MachineBasicBlock* |
| 645 | MSP430TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI, |
| 646 | MachineBasicBlock *BB) const { |
| 647 | const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo(); |
| 648 | DebugLoc dl = MI->getDebugLoc(); |
Anton Korobeynikov | da4d2f6 | 2009-05-08 18:51:21 +0000 | [diff] [blame] | 649 | assert((MI->getOpcode() == MSP430::Select16 || |
| 650 | MI->getOpcode() == MSP430::Select8) && |
Anton Korobeynikov | 8b528e5 | 2009-05-03 13:12:23 +0000 | [diff] [blame] | 651 | "Unexpected instr type to insert"); |
| 652 | |
| 653 | // To "insert" a SELECT instruction, we actually have to insert the diamond |
| 654 | // control-flow pattern. The incoming instruction knows the destination vreg |
| 655 | // to set, the condition code register to branch on, the true/false values to |
| 656 | // select between, and a branch opcode to use. |
| 657 | const BasicBlock *LLVM_BB = BB->getBasicBlock(); |
| 658 | MachineFunction::iterator I = BB; |
| 659 | ++I; |
| 660 | |
| 661 | // thisMBB: |
| 662 | // ... |
| 663 | // TrueVal = ... |
| 664 | // cmpTY ccX, r1, r2 |
| 665 | // jCC copy1MBB |
| 666 | // fallthrough --> copy0MBB |
| 667 | MachineBasicBlock *thisMBB = BB; |
| 668 | MachineFunction *F = BB->getParent(); |
| 669 | MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 670 | MachineBasicBlock *copy1MBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 671 | BuildMI(BB, dl, TII.get(MSP430::JCC)) |
| 672 | .addMBB(copy1MBB) |
| 673 | .addImm(MI->getOperand(3).getImm()); |
| 674 | F->insert(I, copy0MBB); |
| 675 | F->insert(I, copy1MBB); |
| 676 | // Update machine-CFG edges by transferring all successors of the current |
| 677 | // block to the new block which will contain the Phi node for the select. |
| 678 | copy1MBB->transferSuccessors(BB); |
| 679 | // Next, add the true and fallthrough blocks as its successors. |
| 680 | BB->addSuccessor(copy0MBB); |
| 681 | BB->addSuccessor(copy1MBB); |
| 682 | |
| 683 | // copy0MBB: |
| 684 | // %FalseValue = ... |
| 685 | // # fallthrough to copy1MBB |
| 686 | BB = copy0MBB; |
| 687 | |
| 688 | // Update machine-CFG edges |
| 689 | BB->addSuccessor(copy1MBB); |
| 690 | |
| 691 | // copy1MBB: |
| 692 | // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ] |
| 693 | // ... |
| 694 | BB = copy1MBB; |
| 695 | BuildMI(BB, dl, TII.get(MSP430::PHI), |
| 696 | MI->getOperand(0).getReg()) |
| 697 | .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB) |
| 698 | .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB); |
| 699 | |
| 700 | F->DeleteMachineInstr(MI); // The pseudo instruction is gone now. |
| 701 | return BB; |
| 702 | } |