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