1 //===-- MSP430ISelLowering.cpp - MSP430 DAG Lowering Implementation ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the MSP430TargetLowering class.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "msp430-lower"
16 #include "MSP430ISelLowering.h"
18 #include "MSP430MachineFunctionInfo.h"
19 #include "MSP430Subtarget.h"
20 #include "MSP430TargetMachine.h"
21 #include "llvm/CodeGen/CallingConvLower.h"
22 #include "llvm/CodeGen/MachineFrameInfo.h"
23 #include "llvm/CodeGen/MachineFunction.h"
24 #include "llvm/CodeGen/MachineInstrBuilder.h"
25 #include "llvm/CodeGen/MachineRegisterInfo.h"
26 #include "llvm/CodeGen/SelectionDAGISel.h"
27 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
28 #include "llvm/CodeGen/ValueTypes.h"
29 #include "llvm/IR/CallingConv.h"
30 #include "llvm/IR/DerivedTypes.h"
31 #include "llvm/IR/Function.h"
32 #include "llvm/IR/GlobalAlias.h"
33 #include "llvm/IR/GlobalVariable.h"
34 #include "llvm/IR/Intrinsics.h"
35 #include "llvm/Support/CommandLine.h"
36 #include "llvm/Support/Debug.h"
37 #include "llvm/Support/ErrorHandling.h"
38 #include "llvm/Support/raw_ostream.h"
47 static cl::opt<HWMultUseMode>
48 HWMultMode("msp430-hwmult-mode",
49 cl::desc("Hardware multiplier use mode"),
50 cl::init(HWMultNoIntr),
52 clEnumValN(NoHWMult, "no",
53 "Do not use hardware multiplier"),
54 clEnumValN(HWMultIntr, "interrupts",
55 "Assume hardware multiplier can be used inside interrupts"),
56 clEnumValN(HWMultNoIntr, "use",
57 "Assume hardware multiplier cannot be used inside interrupts"),
60 MSP430TargetLowering::MSP430TargetLowering(MSP430TargetMachine &tm) :
61 TargetLowering(tm, new TargetLoweringObjectFileELF()),
62 Subtarget(*tm.getSubtargetImpl()) {
66 // Set up the register classes.
67 addRegisterClass(MVT::i8, &MSP430::GR8RegClass);
68 addRegisterClass(MVT::i16, &MSP430::GR16RegClass);
70 // Compute derived properties from the register classes
71 computeRegisterProperties();
73 // Provide all sorts of operation actions
75 // Division is expensive
76 setIntDivIsCheap(false);
78 setStackPointerRegisterToSaveRestore(MSP430::SPW);
79 setBooleanContents(ZeroOrOneBooleanContent);
80 setBooleanVectorContents(ZeroOrOneBooleanContent); // FIXME: Is this correct?
82 // We have post-incremented loads / stores.
83 setIndexedLoadAction(ISD::POST_INC, MVT::i8, Legal);
84 setIndexedLoadAction(ISD::POST_INC, MVT::i16, Legal);
86 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
87 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
88 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
89 setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Expand);
90 setLoadExtAction(ISD::SEXTLOAD, MVT::i16, Expand);
92 // We don't have any truncstores
93 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
95 setOperationAction(ISD::SRA, MVT::i8, Custom);
96 setOperationAction(ISD::SHL, MVT::i8, Custom);
97 setOperationAction(ISD::SRL, MVT::i8, Custom);
98 setOperationAction(ISD::SRA, MVT::i16, Custom);
99 setOperationAction(ISD::SHL, MVT::i16, Custom);
100 setOperationAction(ISD::SRL, MVT::i16, Custom);
101 setOperationAction(ISD::ROTL, MVT::i8, Expand);
102 setOperationAction(ISD::ROTR, MVT::i8, Expand);
103 setOperationAction(ISD::ROTL, MVT::i16, Expand);
104 setOperationAction(ISD::ROTR, MVT::i16, Expand);
105 setOperationAction(ISD::GlobalAddress, MVT::i16, Custom);
106 setOperationAction(ISD::ExternalSymbol, MVT::i16, Custom);
107 setOperationAction(ISD::BlockAddress, MVT::i16, Custom);
108 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
109 setOperationAction(ISD::BR_CC, MVT::i8, Custom);
110 setOperationAction(ISD::BR_CC, MVT::i16, Custom);
111 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
112 setOperationAction(ISD::SETCC, MVT::i8, Custom);
113 setOperationAction(ISD::SETCC, MVT::i16, Custom);
114 setOperationAction(ISD::SELECT, MVT::i8, Expand);
115 setOperationAction(ISD::SELECT, MVT::i16, Expand);
116 setOperationAction(ISD::SELECT_CC, MVT::i8, Custom);
117 setOperationAction(ISD::SELECT_CC, MVT::i16, Custom);
118 setOperationAction(ISD::SIGN_EXTEND, MVT::i16, Custom);
119 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i8, Expand);
120 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i16, Expand);
122 setOperationAction(ISD::CTTZ, MVT::i8, Expand);
123 setOperationAction(ISD::CTTZ, MVT::i16, Expand);
124 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i8, Expand);
125 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i16, Expand);
126 setOperationAction(ISD::CTLZ, MVT::i8, Expand);
127 setOperationAction(ISD::CTLZ, MVT::i16, Expand);
128 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8, Expand);
129 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16, Expand);
130 setOperationAction(ISD::CTPOP, MVT::i8, Expand);
131 setOperationAction(ISD::CTPOP, MVT::i16, Expand);
133 setOperationAction(ISD::SHL_PARTS, MVT::i8, Expand);
134 setOperationAction(ISD::SHL_PARTS, MVT::i16, Expand);
135 setOperationAction(ISD::SRL_PARTS, MVT::i8, Expand);
136 setOperationAction(ISD::SRL_PARTS, MVT::i16, Expand);
137 setOperationAction(ISD::SRA_PARTS, MVT::i8, Expand);
138 setOperationAction(ISD::SRA_PARTS, MVT::i16, Expand);
140 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
142 // FIXME: Implement efficiently multiplication by a constant
143 setOperationAction(ISD::MUL, MVT::i8, Expand);
144 setOperationAction(ISD::MULHS, MVT::i8, Expand);
145 setOperationAction(ISD::MULHU, MVT::i8, Expand);
146 setOperationAction(ISD::SMUL_LOHI, MVT::i8, Expand);
147 setOperationAction(ISD::UMUL_LOHI, MVT::i8, Expand);
148 setOperationAction(ISD::MUL, MVT::i16, Expand);
149 setOperationAction(ISD::MULHS, MVT::i16, Expand);
150 setOperationAction(ISD::MULHU, MVT::i16, Expand);
151 setOperationAction(ISD::SMUL_LOHI, MVT::i16, Expand);
152 setOperationAction(ISD::UMUL_LOHI, MVT::i16, Expand);
154 setOperationAction(ISD::UDIV, MVT::i8, Expand);
155 setOperationAction(ISD::UDIVREM, MVT::i8, Expand);
156 setOperationAction(ISD::UREM, MVT::i8, Expand);
157 setOperationAction(ISD::SDIV, MVT::i8, Expand);
158 setOperationAction(ISD::SDIVREM, MVT::i8, Expand);
159 setOperationAction(ISD::SREM, MVT::i8, Expand);
160 setOperationAction(ISD::UDIV, MVT::i16, Expand);
161 setOperationAction(ISD::UDIVREM, MVT::i16, Expand);
162 setOperationAction(ISD::UREM, MVT::i16, Expand);
163 setOperationAction(ISD::SDIV, MVT::i16, Expand);
164 setOperationAction(ISD::SDIVREM, MVT::i16, Expand);
165 setOperationAction(ISD::SREM, MVT::i16, Expand);
168 setOperationAction(ISD::VASTART, MVT::Other, Custom);
169 setOperationAction(ISD::VAARG, MVT::Other, Expand);
170 setOperationAction(ISD::VAEND, MVT::Other, Expand);
171 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
174 if (HWMultMode == HWMultIntr) {
175 setLibcallName(RTLIB::MUL_I8, "__mulqi3hw");
176 setLibcallName(RTLIB::MUL_I16, "__mulhi3hw");
177 } else if (HWMultMode == HWMultNoIntr) {
178 setLibcallName(RTLIB::MUL_I8, "__mulqi3hw_noint");
179 setLibcallName(RTLIB::MUL_I16, "__mulhi3hw_noint");
182 setMinFunctionAlignment(1);
183 setPrefFunctionAlignment(2);
186 SDValue MSP430TargetLowering::LowerOperation(SDValue Op,
187 SelectionDAG &DAG) const {
188 switch (Op.getOpcode()) {
189 case ISD::SHL: // FALLTHROUGH
191 case ISD::SRA: return LowerShifts(Op, DAG);
192 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
193 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
194 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
195 case ISD::SETCC: return LowerSETCC(Op, DAG);
196 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
197 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
198 case ISD::SIGN_EXTEND: return LowerSIGN_EXTEND(Op, DAG);
199 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
200 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
201 case ISD::VASTART: return LowerVASTART(Op, DAG);
203 llvm_unreachable("unimplemented operand");
207 //===----------------------------------------------------------------------===//
208 // MSP430 Inline Assembly Support
209 //===----------------------------------------------------------------------===//
211 /// getConstraintType - Given a constraint letter, return the type of
212 /// constraint it is for this target.
213 TargetLowering::ConstraintType
214 MSP430TargetLowering::getConstraintType(const std::string &Constraint) const {
215 if (Constraint.size() == 1) {
216 switch (Constraint[0]) {
218 return C_RegisterClass;
223 return TargetLowering::getConstraintType(Constraint);
226 std::pair<unsigned, const TargetRegisterClass*>
227 MSP430TargetLowering::
228 getRegForInlineAsmConstraint(const std::string &Constraint,
230 if (Constraint.size() == 1) {
231 // GCC Constraint Letters
232 switch (Constraint[0]) {
234 case 'r': // GENERAL_REGS
236 return std::make_pair(0U, &MSP430::GR8RegClass);
238 return std::make_pair(0U, &MSP430::GR16RegClass);
242 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
245 //===----------------------------------------------------------------------===//
246 // Calling Convention Implementation
247 //===----------------------------------------------------------------------===//
249 #include "MSP430GenCallingConv.inc"
252 MSP430TargetLowering::LowerFormalArguments(SDValue Chain,
253 CallingConv::ID CallConv,
255 const SmallVectorImpl<ISD::InputArg>
259 SmallVectorImpl<SDValue> &InVals)
264 llvm_unreachable("Unsupported calling convention");
266 case CallingConv::Fast:
267 return LowerCCCArguments(Chain, CallConv, isVarArg, Ins, dl, DAG, InVals);
268 case CallingConv::MSP430_INTR:
271 report_fatal_error("ISRs cannot have arguments");
276 MSP430TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
277 SmallVectorImpl<SDValue> &InVals) const {
278 SelectionDAG &DAG = CLI.DAG;
280 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
281 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
282 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
283 SDValue Chain = CLI.Chain;
284 SDValue Callee = CLI.Callee;
285 bool &isTailCall = CLI.IsTailCall;
286 CallingConv::ID CallConv = CLI.CallConv;
287 bool isVarArg = CLI.IsVarArg;
289 // MSP430 target does not yet support tail call optimization.
294 llvm_unreachable("Unsupported calling convention");
295 case CallingConv::Fast:
297 return LowerCCCCallTo(Chain, Callee, CallConv, isVarArg, isTailCall,
298 Outs, OutVals, Ins, dl, DAG, InVals);
299 case CallingConv::MSP430_INTR:
300 report_fatal_error("ISRs cannot be called directly");
304 /// LowerCCCArguments - transform physical registers into virtual registers and
305 /// generate load operations for arguments places on the stack.
306 // FIXME: struct return stuff
308 MSP430TargetLowering::LowerCCCArguments(SDValue Chain,
309 CallingConv::ID CallConv,
311 const SmallVectorImpl<ISD::InputArg>
315 SmallVectorImpl<SDValue> &InVals)
317 MachineFunction &MF = DAG.getMachineFunction();
318 MachineFrameInfo *MFI = MF.getFrameInfo();
319 MachineRegisterInfo &RegInfo = MF.getRegInfo();
320 MSP430MachineFunctionInfo *FuncInfo = MF.getInfo<MSP430MachineFunctionInfo>();
322 // Assign locations to all of the incoming arguments.
323 SmallVector<CCValAssign, 16> ArgLocs;
324 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
325 getTargetMachine(), ArgLocs, *DAG.getContext());
326 CCInfo.AnalyzeFormalArguments(Ins, CC_MSP430);
328 // Create frame index for the start of the first vararg value
330 unsigned Offset = CCInfo.getNextStackOffset();
331 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, Offset, true));
334 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
335 CCValAssign &VA = ArgLocs[i];
337 // Arguments passed in registers
338 EVT RegVT = VA.getLocVT();
339 switch (RegVT.getSimpleVT().SimpleTy) {
343 errs() << "LowerFormalArguments Unhandled argument type: "
344 << RegVT.getSimpleVT().SimpleTy << "\n";
349 unsigned VReg = RegInfo.createVirtualRegister(&MSP430::GR16RegClass);
350 RegInfo.addLiveIn(VA.getLocReg(), VReg);
351 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, VReg, RegVT);
353 // If this is an 8-bit value, it is really passed promoted to 16
354 // bits. Insert an assert[sz]ext to capture this, then truncate to the
356 if (VA.getLocInfo() == CCValAssign::SExt)
357 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
358 DAG.getValueType(VA.getValVT()));
359 else if (VA.getLocInfo() == CCValAssign::ZExt)
360 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
361 DAG.getValueType(VA.getValVT()));
363 if (VA.getLocInfo() != CCValAssign::Full)
364 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
366 InVals.push_back(ArgValue);
370 assert(VA.isMemLoc());
373 ISD::ArgFlagsTy Flags = Ins[i].Flags;
375 if (Flags.isByVal()) {
376 int FI = MFI->CreateFixedObject(Flags.getByValSize(),
377 VA.getLocMemOffset(), true);
378 InVal = DAG.getFrameIndex(FI, getPointerTy());
380 // Load the argument to a virtual register
381 unsigned ObjSize = VA.getLocVT().getSizeInBits()/8;
383 errs() << "LowerFormalArguments Unhandled argument type: "
384 << EVT(VA.getLocVT()).getEVTString()
387 // Create the frame index object for this incoming parameter...
388 int FI = MFI->CreateFixedObject(ObjSize, VA.getLocMemOffset(), true);
390 // Create the SelectionDAG nodes corresponding to a load
391 //from this parameter
392 SDValue FIN = DAG.getFrameIndex(FI, MVT::i16);
393 InVal = DAG.getLoad(VA.getLocVT(), dl, Chain, FIN,
394 MachinePointerInfo::getFixedStack(FI),
395 false, false, false, 0);
398 InVals.push_back(InVal);
406 MSP430TargetLowering::LowerReturn(SDValue Chain,
407 CallingConv::ID CallConv, bool isVarArg,
408 const SmallVectorImpl<ISD::OutputArg> &Outs,
409 const SmallVectorImpl<SDValue> &OutVals,
410 SDLoc dl, SelectionDAG &DAG) const {
412 // CCValAssign - represent the assignment of the return value to a location
413 SmallVector<CCValAssign, 16> RVLocs;
415 // ISRs cannot return any value.
416 if (CallConv == CallingConv::MSP430_INTR && !Outs.empty())
417 report_fatal_error("ISRs cannot return any value");
419 // CCState - Info about the registers and stack slot.
420 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
421 getTargetMachine(), RVLocs, *DAG.getContext());
423 // Analize return values.
424 CCInfo.AnalyzeReturn(Outs, RetCC_MSP430);
427 SmallVector<SDValue, 4> RetOps(1, Chain);
429 // Copy the result values into the output registers.
430 for (unsigned i = 0; i != RVLocs.size(); ++i) {
431 CCValAssign &VA = RVLocs[i];
432 assert(VA.isRegLoc() && "Can only return in registers!");
434 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
437 // Guarantee that all emitted copies are stuck together,
438 // avoiding something bad.
439 Flag = Chain.getValue(1);
440 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
443 unsigned Opc = (CallConv == CallingConv::MSP430_INTR ?
444 MSP430ISD::RETI_FLAG : MSP430ISD::RET_FLAG);
446 RetOps[0] = Chain; // Update chain.
448 // Add the flag if we have it.
450 RetOps.push_back(Flag);
452 return DAG.getNode(Opc, dl, MVT::Other, &RetOps[0], RetOps.size());
455 /// LowerCCCCallTo - functions arguments are copied from virtual regs to
456 /// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
459 MSP430TargetLowering::LowerCCCCallTo(SDValue Chain, SDValue Callee,
460 CallingConv::ID CallConv, bool isVarArg,
462 const SmallVectorImpl<ISD::OutputArg>
464 const SmallVectorImpl<SDValue> &OutVals,
465 const SmallVectorImpl<ISD::InputArg> &Ins,
466 SDLoc dl, SelectionDAG &DAG,
467 SmallVectorImpl<SDValue> &InVals) const {
468 // Analyze operands of the call, assigning locations to each operand.
469 SmallVector<CCValAssign, 16> ArgLocs;
470 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
471 getTargetMachine(), ArgLocs, *DAG.getContext());
473 CCInfo.AnalyzeCallOperands(Outs, CC_MSP430);
475 // Get a count of how many bytes are to be pushed on the stack.
476 unsigned NumBytes = CCInfo.getNextStackOffset();
478 Chain = DAG.getCALLSEQ_START(Chain ,DAG.getConstant(NumBytes,
479 getPointerTy(), true),
482 SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass;
483 SmallVector<SDValue, 12> MemOpChains;
486 // Walk the register/memloc assignments, inserting copies/loads.
487 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
488 CCValAssign &VA = ArgLocs[i];
490 SDValue Arg = OutVals[i];
492 // Promote the value if needed.
493 switch (VA.getLocInfo()) {
494 default: llvm_unreachable("Unknown loc info!");
495 case CCValAssign::Full: break;
496 case CCValAssign::SExt:
497 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
499 case CCValAssign::ZExt:
500 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
502 case CCValAssign::AExt:
503 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
507 // Arguments that can be passed on register must be kept at RegsToPass
510 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
512 assert(VA.isMemLoc());
514 if (StackPtr.getNode() == 0)
515 StackPtr = DAG.getCopyFromReg(Chain, dl, MSP430::SPW, getPointerTy());
517 SDValue PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(),
519 DAG.getIntPtrConstant(VA.getLocMemOffset()));
522 ISD::ArgFlagsTy Flags = Outs[i].Flags;
524 if (Flags.isByVal()) {
525 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i16);
526 MemOp = DAG.getMemcpy(Chain, dl, PtrOff, Arg, SizeNode,
527 Flags.getByValAlign(),
529 /*AlwaysInline=*/true,
530 MachinePointerInfo(),
531 MachinePointerInfo());
533 MemOp = DAG.getStore(Chain, dl, Arg, PtrOff, MachinePointerInfo(),
537 MemOpChains.push_back(MemOp);
541 // Transform all store nodes into one single node because all store nodes are
542 // independent of each other.
543 if (!MemOpChains.empty())
544 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
545 &MemOpChains[0], MemOpChains.size());
547 // Build a sequence of copy-to-reg nodes chained together with token chain and
548 // flag operands which copy the outgoing args into registers. The InFlag in
549 // necessary since all emitted instructions must be stuck together.
551 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
552 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
553 RegsToPass[i].second, InFlag);
554 InFlag = Chain.getValue(1);
557 // If the callee is a GlobalAddress node (quite common, every direct call is)
558 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
559 // Likewise ExternalSymbol -> TargetExternalSymbol.
560 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
561 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, MVT::i16);
562 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
563 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i16);
565 // Returns a chain & a flag for retval copy to use.
566 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
567 SmallVector<SDValue, 8> Ops;
568 Ops.push_back(Chain);
569 Ops.push_back(Callee);
571 // Add argument registers to the end of the list so that they are
572 // known live into the call.
573 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
574 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
575 RegsToPass[i].second.getValueType()));
577 if (InFlag.getNode())
578 Ops.push_back(InFlag);
580 Chain = DAG.getNode(MSP430ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
581 InFlag = Chain.getValue(1);
583 // Create the CALLSEQ_END node.
584 Chain = DAG.getCALLSEQ_END(Chain,
585 DAG.getConstant(NumBytes, getPointerTy(), true),
586 DAG.getConstant(0, getPointerTy(), true),
588 InFlag = Chain.getValue(1);
590 // Handle result values, copying them out of physregs into vregs that we
592 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl,
596 /// LowerCallResult - Lower the result values of a call into the
597 /// appropriate copies out of appropriate physical registers.
600 MSP430TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
601 CallingConv::ID CallConv, bool isVarArg,
602 const SmallVectorImpl<ISD::InputArg> &Ins,
603 SDLoc dl, SelectionDAG &DAG,
604 SmallVectorImpl<SDValue> &InVals) const {
606 // Assign locations to each value returned by this call.
607 SmallVector<CCValAssign, 16> RVLocs;
608 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
609 getTargetMachine(), RVLocs, *DAG.getContext());
611 CCInfo.AnalyzeCallResult(Ins, RetCC_MSP430);
613 // Copy all of the result registers out of their specified physreg.
614 for (unsigned i = 0; i != RVLocs.size(); ++i) {
615 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
616 RVLocs[i].getValVT(), InFlag).getValue(1);
617 InFlag = Chain.getValue(2);
618 InVals.push_back(Chain.getValue(0));
624 SDValue MSP430TargetLowering::LowerShifts(SDValue Op,
625 SelectionDAG &DAG) const {
626 unsigned Opc = Op.getOpcode();
627 SDNode* N = Op.getNode();
628 EVT VT = Op.getValueType();
631 // Expand non-constant shifts to loops:
632 if (!isa<ConstantSDNode>(N->getOperand(1)))
634 default: llvm_unreachable("Invalid shift opcode!");
636 return DAG.getNode(MSP430ISD::SHL, dl,
637 VT, N->getOperand(0), N->getOperand(1));
639 return DAG.getNode(MSP430ISD::SRA, dl,
640 VT, N->getOperand(0), N->getOperand(1));
642 return DAG.getNode(MSP430ISD::SRL, dl,
643 VT, N->getOperand(0), N->getOperand(1));
646 uint64_t ShiftAmount = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
648 // Expand the stuff into sequence of shifts.
649 // FIXME: for some shift amounts this might be done better!
650 // E.g.: foo >> (8 + N) => sxt(swpb(foo)) >> N
651 SDValue Victim = N->getOperand(0);
653 if (Opc == ISD::SRL && ShiftAmount) {
654 // Emit a special goodness here:
655 // srl A, 1 => clrc; rrc A
656 Victim = DAG.getNode(MSP430ISD::RRC, dl, VT, Victim);
660 while (ShiftAmount--)
661 Victim = DAG.getNode((Opc == ISD::SHL ? MSP430ISD::RLA : MSP430ISD::RRA),
667 SDValue MSP430TargetLowering::LowerGlobalAddress(SDValue Op,
668 SelectionDAG &DAG) const {
669 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
670 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
672 // Create the TargetGlobalAddress node, folding in the constant offset.
673 SDValue Result = DAG.getTargetGlobalAddress(GV, SDLoc(Op),
674 getPointerTy(), Offset);
675 return DAG.getNode(MSP430ISD::Wrapper, SDLoc(Op),
676 getPointerTy(), Result);
679 SDValue MSP430TargetLowering::LowerExternalSymbol(SDValue Op,
680 SelectionDAG &DAG) const {
682 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
683 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
685 return DAG.getNode(MSP430ISD::Wrapper, dl, getPointerTy(), Result);
688 SDValue MSP430TargetLowering::LowerBlockAddress(SDValue Op,
689 SelectionDAG &DAG) const {
691 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
692 SDValue Result = DAG.getTargetBlockAddress(BA, getPointerTy());
694 return DAG.getNode(MSP430ISD::Wrapper, dl, getPointerTy(), Result);
697 static SDValue EmitCMP(SDValue &LHS, SDValue &RHS, SDValue &TargetCC,
699 SDLoc dl, SelectionDAG &DAG) {
700 // FIXME: Handle bittests someday
701 assert(!LHS.getValueType().isFloatingPoint() && "We don't handle FP yet");
703 // FIXME: Handle jump negative someday
704 MSP430CC::CondCodes TCC = MSP430CC::COND_INVALID;
706 default: llvm_unreachable("Invalid integer condition!");
708 TCC = MSP430CC::COND_E; // aka COND_Z
709 // Minor optimization: if LHS is a constant, swap operands, then the
710 // constant can be folded into comparison.
711 if (LHS.getOpcode() == ISD::Constant)
715 TCC = MSP430CC::COND_NE; // aka COND_NZ
716 // Minor optimization: if LHS is a constant, swap operands, then the
717 // constant can be folded into comparison.
718 if (LHS.getOpcode() == ISD::Constant)
722 std::swap(LHS, RHS); // FALLTHROUGH
724 // Turn lhs u>= rhs with lhs constant into rhs u< lhs+1, this allows us to
725 // fold constant into instruction.
726 if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) {
728 RHS = DAG.getConstant(C->getSExtValue() + 1, C->getValueType(0));
729 TCC = MSP430CC::COND_LO;
732 TCC = MSP430CC::COND_HS; // aka COND_C
735 std::swap(LHS, RHS); // FALLTHROUGH
737 // Turn lhs u< rhs with lhs constant into rhs u>= lhs+1, this allows us to
738 // fold constant into instruction.
739 if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) {
741 RHS = DAG.getConstant(C->getSExtValue() + 1, C->getValueType(0));
742 TCC = MSP430CC::COND_HS;
745 TCC = MSP430CC::COND_LO; // aka COND_NC
748 std::swap(LHS, RHS); // FALLTHROUGH
750 // Turn lhs >= rhs with lhs constant into rhs < lhs+1, this allows us to
751 // fold constant into instruction.
752 if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) {
754 RHS = DAG.getConstant(C->getSExtValue() + 1, C->getValueType(0));
755 TCC = MSP430CC::COND_L;
758 TCC = MSP430CC::COND_GE;
761 std::swap(LHS, RHS); // FALLTHROUGH
763 // Turn lhs < rhs with lhs constant into rhs >= lhs+1, this allows us to
764 // fold constant into instruction.
765 if (const ConstantSDNode * C = dyn_cast<ConstantSDNode>(LHS)) {
767 RHS = DAG.getConstant(C->getSExtValue() + 1, C->getValueType(0));
768 TCC = MSP430CC::COND_GE;
771 TCC = MSP430CC::COND_L;
775 TargetCC = DAG.getConstant(TCC, MVT::i8);
776 return DAG.getNode(MSP430ISD::CMP, dl, MVT::Glue, LHS, RHS);
780 SDValue MSP430TargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
781 SDValue Chain = Op.getOperand(0);
782 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
783 SDValue LHS = Op.getOperand(2);
784 SDValue RHS = Op.getOperand(3);
785 SDValue Dest = Op.getOperand(4);
789 SDValue Flag = EmitCMP(LHS, RHS, TargetCC, CC, dl, DAG);
791 return DAG.getNode(MSP430ISD::BR_CC, dl, Op.getValueType(),
792 Chain, Dest, TargetCC, Flag);
795 SDValue MSP430TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
796 SDValue LHS = Op.getOperand(0);
797 SDValue RHS = Op.getOperand(1);
800 // If we are doing an AND and testing against zero, then the CMP
801 // will not be generated. The AND (or BIT) will generate the condition codes,
802 // but they are different from CMP.
803 // FIXME: since we're doing a post-processing, use a pseudoinstr here, so
804 // lowering & isel wouldn't diverge.
806 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
807 if (RHSC->isNullValue() && LHS.hasOneUse() &&
808 (LHS.getOpcode() == ISD::AND ||
809 (LHS.getOpcode() == ISD::TRUNCATE &&
810 LHS.getOperand(0).getOpcode() == ISD::AND))) {
814 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
816 SDValue Flag = EmitCMP(LHS, RHS, TargetCC, CC, dl, DAG);
818 // Get the condition codes directly from the status register, if its easy.
819 // Otherwise a branch will be generated. Note that the AND and BIT
820 // instructions generate different flags than CMP, the carry bit can be used
825 switch (cast<ConstantSDNode>(TargetCC)->getZExtValue()) {
829 case MSP430CC::COND_HS:
830 // Res = SRW & 1, no processing is required
832 case MSP430CC::COND_LO:
836 case MSP430CC::COND_NE:
838 // C = ~Z, thus Res = SRW & 1, no processing is required
840 // Res = ~((SRW >> 1) & 1)
845 case MSP430CC::COND_E:
847 // C = ~Z for AND instruction, thus we can put Res = ~(SRW & 1), however,
848 // Res = (SRW >> 1) & 1 is 1 word shorter.
851 EVT VT = Op.getValueType();
852 SDValue One = DAG.getConstant(1, VT);
854 SDValue SR = DAG.getCopyFromReg(DAG.getEntryNode(), dl, MSP430::SRW,
857 // FIXME: somewhere this is turned into a SRL, lower it MSP specific?
858 SR = DAG.getNode(ISD::SRA, dl, MVT::i16, SR, One);
859 SR = DAG.getNode(ISD::AND, dl, MVT::i16, SR, One);
861 SR = DAG.getNode(ISD::XOR, dl, MVT::i16, SR, One);
864 SDValue Zero = DAG.getConstant(0, VT);
865 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
866 SmallVector<SDValue, 4> Ops;
869 Ops.push_back(TargetCC);
871 return DAG.getNode(MSP430ISD::SELECT_CC, dl, VTs, &Ops[0], Ops.size());
875 SDValue MSP430TargetLowering::LowerSELECT_CC(SDValue Op,
876 SelectionDAG &DAG) const {
877 SDValue LHS = Op.getOperand(0);
878 SDValue RHS = Op.getOperand(1);
879 SDValue TrueV = Op.getOperand(2);
880 SDValue FalseV = Op.getOperand(3);
881 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
885 SDValue Flag = EmitCMP(LHS, RHS, TargetCC, CC, dl, DAG);
887 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
888 SmallVector<SDValue, 4> Ops;
889 Ops.push_back(TrueV);
890 Ops.push_back(FalseV);
891 Ops.push_back(TargetCC);
894 return DAG.getNode(MSP430ISD::SELECT_CC, dl, VTs, &Ops[0], Ops.size());
897 SDValue MSP430TargetLowering::LowerSIGN_EXTEND(SDValue Op,
898 SelectionDAG &DAG) const {
899 SDValue Val = Op.getOperand(0);
900 EVT VT = Op.getValueType();
903 assert(VT == MVT::i16 && "Only support i16 for now!");
905 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, VT,
906 DAG.getNode(ISD::ANY_EXTEND, dl, VT, Val),
907 DAG.getValueType(Val.getValueType()));
911 MSP430TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
912 MachineFunction &MF = DAG.getMachineFunction();
913 MSP430MachineFunctionInfo *FuncInfo = MF.getInfo<MSP430MachineFunctionInfo>();
914 int ReturnAddrIndex = FuncInfo->getRAIndex();
916 if (ReturnAddrIndex == 0) {
917 // Set up a frame object for the return address.
918 uint64_t SlotSize = TD->getPointerSize();
919 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
921 FuncInfo->setRAIndex(ReturnAddrIndex);
924 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
927 SDValue MSP430TargetLowering::LowerRETURNADDR(SDValue Op,
928 SelectionDAG &DAG) const {
929 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
930 MFI->setReturnAddressIsTaken(true);
932 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
936 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
938 DAG.getConstant(TD->getPointerSize(), MVT::i16);
939 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
940 DAG.getNode(ISD::ADD, dl, getPointerTy(),
942 MachinePointerInfo(), false, false, false, 0);
945 // Just load the return address.
946 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
947 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
948 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
951 SDValue MSP430TargetLowering::LowerFRAMEADDR(SDValue Op,
952 SelectionDAG &DAG) const {
953 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
954 MFI->setFrameAddressIsTaken(true);
956 EVT VT = Op.getValueType();
957 SDLoc dl(Op); // FIXME probably not meaningful
958 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
959 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
962 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
963 MachinePointerInfo(),
964 false, false, false, 0);
968 SDValue MSP430TargetLowering::LowerVASTART(SDValue Op,
969 SelectionDAG &DAG) const {
970 MachineFunction &MF = DAG.getMachineFunction();
971 MSP430MachineFunctionInfo *FuncInfo = MF.getInfo<MSP430MachineFunctionInfo>();
973 // Frame index of first vararg argument
974 SDValue FrameIndex = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
976 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
978 // Create a store of the frame index to the location operand
979 return DAG.getStore(Op.getOperand(0), SDLoc(Op), FrameIndex,
980 Op.getOperand(1), MachinePointerInfo(SV),
984 /// getPostIndexedAddressParts - returns true by value, base pointer and
985 /// offset pointer and addressing mode by reference if this node can be
986 /// combined with a load / store to form a post-indexed load / store.
987 bool MSP430TargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
990 ISD::MemIndexedMode &AM,
991 SelectionDAG &DAG) const {
993 LoadSDNode *LD = cast<LoadSDNode>(N);
994 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
997 EVT VT = LD->getMemoryVT();
998 if (VT != MVT::i8 && VT != MVT::i16)
1001 if (Op->getOpcode() != ISD::ADD)
1004 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Op->getOperand(1))) {
1005 uint64_t RHSC = RHS->getZExtValue();
1006 if ((VT == MVT::i16 && RHSC != 2) ||
1007 (VT == MVT::i8 && RHSC != 1))
1010 Base = Op->getOperand(0);
1011 Offset = DAG.getConstant(RHSC, VT);
1020 const char *MSP430TargetLowering::getTargetNodeName(unsigned Opcode) const {
1022 default: return NULL;
1023 case MSP430ISD::RET_FLAG: return "MSP430ISD::RET_FLAG";
1024 case MSP430ISD::RETI_FLAG: return "MSP430ISD::RETI_FLAG";
1025 case MSP430ISD::RRA: return "MSP430ISD::RRA";
1026 case MSP430ISD::RLA: return "MSP430ISD::RLA";
1027 case MSP430ISD::RRC: return "MSP430ISD::RRC";
1028 case MSP430ISD::CALL: return "MSP430ISD::CALL";
1029 case MSP430ISD::Wrapper: return "MSP430ISD::Wrapper";
1030 case MSP430ISD::BR_CC: return "MSP430ISD::BR_CC";
1031 case MSP430ISD::CMP: return "MSP430ISD::CMP";
1032 case MSP430ISD::SELECT_CC: return "MSP430ISD::SELECT_CC";
1033 case MSP430ISD::SHL: return "MSP430ISD::SHL";
1034 case MSP430ISD::SRA: return "MSP430ISD::SRA";
1038 bool MSP430TargetLowering::isTruncateFree(Type *Ty1,
1040 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
1043 return (Ty1->getPrimitiveSizeInBits() > Ty2->getPrimitiveSizeInBits());
1046 bool MSP430TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
1047 if (!VT1.isInteger() || !VT2.isInteger())
1050 return (VT1.getSizeInBits() > VT2.getSizeInBits());
1053 bool MSP430TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
1054 // MSP430 implicitly zero-extends 8-bit results in 16-bit registers.
1055 return 0 && Ty1->isIntegerTy(8) && Ty2->isIntegerTy(16);
1058 bool MSP430TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
1059 // MSP430 implicitly zero-extends 8-bit results in 16-bit registers.
1060 return 0 && VT1 == MVT::i8 && VT2 == MVT::i16;
1063 bool MSP430TargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
1064 return isZExtFree(Val.getValueType(), VT2);
1067 //===----------------------------------------------------------------------===//
1068 // Other Lowering Code
1069 //===----------------------------------------------------------------------===//
1072 MSP430TargetLowering::EmitShiftInstr(MachineInstr *MI,
1073 MachineBasicBlock *BB) const {
1074 MachineFunction *F = BB->getParent();
1075 MachineRegisterInfo &RI = F->getRegInfo();
1076 DebugLoc dl = MI->getDebugLoc();
1077 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
1080 const TargetRegisterClass * RC;
1081 switch (MI->getOpcode()) {
1082 default: llvm_unreachable("Invalid shift opcode!");
1084 Opc = MSP430::SHL8r1;
1085 RC = &MSP430::GR8RegClass;
1088 Opc = MSP430::SHL16r1;
1089 RC = &MSP430::GR16RegClass;
1092 Opc = MSP430::SAR8r1;
1093 RC = &MSP430::GR8RegClass;
1096 Opc = MSP430::SAR16r1;
1097 RC = &MSP430::GR16RegClass;
1100 Opc = MSP430::SAR8r1c;
1101 RC = &MSP430::GR8RegClass;
1104 Opc = MSP430::SAR16r1c;
1105 RC = &MSP430::GR16RegClass;
1109 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1110 MachineFunction::iterator I = BB;
1113 // Create loop block
1114 MachineBasicBlock *LoopBB = F->CreateMachineBasicBlock(LLVM_BB);
1115 MachineBasicBlock *RemBB = F->CreateMachineBasicBlock(LLVM_BB);
1117 F->insert(I, LoopBB);
1118 F->insert(I, RemBB);
1120 // Update machine-CFG edges by transferring all successors of the current
1121 // block to the block containing instructions after shift.
1122 RemBB->splice(RemBB->begin(), BB,
1123 llvm::next(MachineBasicBlock::iterator(MI)),
1125 RemBB->transferSuccessorsAndUpdatePHIs(BB);
1127 // Add adges BB => LoopBB => RemBB, BB => RemBB, LoopBB => LoopBB
1128 BB->addSuccessor(LoopBB);
1129 BB->addSuccessor(RemBB);
1130 LoopBB->addSuccessor(RemBB);
1131 LoopBB->addSuccessor(LoopBB);
1133 unsigned ShiftAmtReg = RI.createVirtualRegister(&MSP430::GR8RegClass);
1134 unsigned ShiftAmtReg2 = RI.createVirtualRegister(&MSP430::GR8RegClass);
1135 unsigned ShiftReg = RI.createVirtualRegister(RC);
1136 unsigned ShiftReg2 = RI.createVirtualRegister(RC);
1137 unsigned ShiftAmtSrcReg = MI->getOperand(2).getReg();
1138 unsigned SrcReg = MI->getOperand(1).getReg();
1139 unsigned DstReg = MI->getOperand(0).getReg();
1144 BuildMI(BB, dl, TII.get(MSP430::CMP8ri))
1145 .addReg(ShiftAmtSrcReg).addImm(0);
1146 BuildMI(BB, dl, TII.get(MSP430::JCC))
1148 .addImm(MSP430CC::COND_E);
1151 // ShiftReg = phi [%SrcReg, BB], [%ShiftReg2, LoopBB]
1152 // ShiftAmt = phi [%N, BB], [%ShiftAmt2, LoopBB]
1153 // ShiftReg2 = shift ShiftReg
1154 // ShiftAmt2 = ShiftAmt - 1;
1155 BuildMI(LoopBB, dl, TII.get(MSP430::PHI), ShiftReg)
1156 .addReg(SrcReg).addMBB(BB)
1157 .addReg(ShiftReg2).addMBB(LoopBB);
1158 BuildMI(LoopBB, dl, TII.get(MSP430::PHI), ShiftAmtReg)
1159 .addReg(ShiftAmtSrcReg).addMBB(BB)
1160 .addReg(ShiftAmtReg2).addMBB(LoopBB);
1161 BuildMI(LoopBB, dl, TII.get(Opc), ShiftReg2)
1163 BuildMI(LoopBB, dl, TII.get(MSP430::SUB8ri), ShiftAmtReg2)
1164 .addReg(ShiftAmtReg).addImm(1);
1165 BuildMI(LoopBB, dl, TII.get(MSP430::JCC))
1167 .addImm(MSP430CC::COND_NE);
1170 // DestReg = phi [%SrcReg, BB], [%ShiftReg, LoopBB]
1171 BuildMI(*RemBB, RemBB->begin(), dl, TII.get(MSP430::PHI), DstReg)
1172 .addReg(SrcReg).addMBB(BB)
1173 .addReg(ShiftReg2).addMBB(LoopBB);
1175 MI->eraseFromParent(); // The pseudo instruction is gone now.
1180 MSP430TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
1181 MachineBasicBlock *BB) const {
1182 unsigned Opc = MI->getOpcode();
1184 if (Opc == MSP430::Shl8 || Opc == MSP430::Shl16 ||
1185 Opc == MSP430::Sra8 || Opc == MSP430::Sra16 ||
1186 Opc == MSP430::Srl8 || Opc == MSP430::Srl16)
1187 return EmitShiftInstr(MI, BB);
1189 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
1190 DebugLoc dl = MI->getDebugLoc();
1192 assert((Opc == MSP430::Select16 || Opc == MSP430::Select8) &&
1193 "Unexpected instr type to insert");
1195 // To "insert" a SELECT instruction, we actually have to insert the diamond
1196 // control-flow pattern. The incoming instruction knows the destination vreg
1197 // to set, the condition code register to branch on, the true/false values to
1198 // select between, and a branch opcode to use.
1199 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1200 MachineFunction::iterator I = BB;
1206 // cmpTY ccX, r1, r2
1208 // fallthrough --> copy0MBB
1209 MachineBasicBlock *thisMBB = BB;
1210 MachineFunction *F = BB->getParent();
1211 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
1212 MachineBasicBlock *copy1MBB = F->CreateMachineBasicBlock(LLVM_BB);
1213 F->insert(I, copy0MBB);
1214 F->insert(I, copy1MBB);
1215 // Update machine-CFG edges by transferring all successors of the current
1216 // block to the new block which will contain the Phi node for the select.
1217 copy1MBB->splice(copy1MBB->begin(), BB,
1218 llvm::next(MachineBasicBlock::iterator(MI)),
1220 copy1MBB->transferSuccessorsAndUpdatePHIs(BB);
1221 // Next, add the true and fallthrough blocks as its successors.
1222 BB->addSuccessor(copy0MBB);
1223 BB->addSuccessor(copy1MBB);
1225 BuildMI(BB, dl, TII.get(MSP430::JCC))
1227 .addImm(MI->getOperand(3).getImm());
1230 // %FalseValue = ...
1231 // # fallthrough to copy1MBB
1234 // Update machine-CFG edges
1235 BB->addSuccessor(copy1MBB);
1238 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1241 BuildMI(*BB, BB->begin(), dl, TII.get(MSP430::PHI),
1242 MI->getOperand(0).getReg())
1243 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
1244 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
1246 MI->eraseFromParent(); // The pseudo instruction is gone now.