1 //===-- MipsSEISelLowering.cpp - MipsSE DAG Lowering Interface --*- C++ -*-===//
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 // Subclass of MipsTargetLowering specialized for mips32/64.
12 //===----------------------------------------------------------------------===//
13 #include "MipsSEISelLowering.h"
14 #include "MipsRegisterInfo.h"
15 #include "MipsTargetMachine.h"
16 #include "llvm/CodeGen/MachineInstrBuilder.h"
17 #include "llvm/CodeGen/MachineRegisterInfo.h"
18 #include "llvm/Support/CommandLine.h"
19 #include "llvm/Target/TargetInstrInfo.h"
24 EnableMipsTailCalls("enable-mips-tail-calls", cl::Hidden,
25 cl::desc("MIPS: Enable tail calls."), cl::init(false));
27 MipsSETargetLowering::MipsSETargetLowering(MipsTargetMachine &TM)
28 : MipsTargetLowering(TM) {
29 // Set up the register classes
31 clearRegisterClasses();
33 addRegisterClass(MVT::i32, &Mips::CPURegsRegClass);
36 addRegisterClass(MVT::i64, &Mips::CPU64RegsRegClass);
38 if (Subtarget->hasDSP()) {
39 MVT::SimpleValueType VecTys[2] = {MVT::v2i16, MVT::v4i8};
41 for (unsigned i = 0; i < array_lengthof(VecTys); ++i) {
42 addRegisterClass(VecTys[i], &Mips::DSPRegsRegClass);
44 // Expand all builtin opcodes.
45 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
46 setOperationAction(Opc, VecTys[i], Expand);
48 setOperationAction(ISD::ADD, VecTys[i], Legal);
49 setOperationAction(ISD::SUB, VecTys[i], Legal);
50 setOperationAction(ISD::LOAD, VecTys[i], Legal);
51 setOperationAction(ISD::STORE, VecTys[i], Legal);
52 setOperationAction(ISD::BITCAST, VecTys[i], Legal);
56 if (Subtarget->hasDSPR2())
57 setOperationAction(ISD::MUL, MVT::v2i16, Legal);
59 if (!TM.Options.UseSoftFloat) {
60 addRegisterClass(MVT::f32, &Mips::FGR32RegClass);
62 // When dealing with single precision only, use libcalls
63 if (!Subtarget->isSingleFloat()) {
65 addRegisterClass(MVT::f64, &Mips::FGR64RegClass);
67 addRegisterClass(MVT::f64, &Mips::AFGR64RegClass);
71 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Custom);
72 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Custom);
73 setOperationAction(ISD::MULHS, MVT::i32, Custom);
74 setOperationAction(ISD::MULHU, MVT::i32, Custom);
77 setOperationAction(ISD::MULHS, MVT::i64, Custom);
78 setOperationAction(ISD::MULHU, MVT::i64, Custom);
79 setOperationAction(ISD::MUL, MVT::i64, Custom);
82 setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
83 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
84 setOperationAction(ISD::SDIVREM, MVT::i64, Custom);
85 setOperationAction(ISD::UDIVREM, MVT::i64, Custom);
86 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
87 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
88 setOperationAction(ISD::LOAD, MVT::i32, Custom);
89 setOperationAction(ISD::STORE, MVT::i32, Custom);
91 setTargetDAGCombine(ISD::ADDE);
92 setTargetDAGCombine(ISD::SUBE);
94 computeRegisterProperties();
97 const MipsTargetLowering *
98 llvm::createMipsSETargetLowering(MipsTargetMachine &TM) {
99 return new MipsSETargetLowering(TM);
104 MipsSETargetLowering::allowsUnalignedMemoryAccesses(EVT VT, bool *Fast) const {
105 MVT::SimpleValueType SVT = VT.getSimpleVT().SimpleTy;
118 SDValue MipsSETargetLowering::LowerOperation(SDValue Op,
119 SelectionDAG &DAG) const {
120 switch(Op.getOpcode()) {
121 case ISD::SMUL_LOHI: return lowerMulDiv(Op, MipsISD::Mult, true, true, DAG);
122 case ISD::UMUL_LOHI: return lowerMulDiv(Op, MipsISD::Multu, true, true, DAG);
123 case ISD::MULHS: return lowerMulDiv(Op, MipsISD::Mult, false, true, DAG);
124 case ISD::MULHU: return lowerMulDiv(Op, MipsISD::Multu, false, true, DAG);
125 case ISD::MUL: return lowerMulDiv(Op, MipsISD::Mult, true, false, DAG);
126 case ISD::SDIVREM: return lowerMulDiv(Op, MipsISD::DivRem, true, true, DAG);
127 case ISD::UDIVREM: return lowerMulDiv(Op, MipsISD::DivRemU, true, true, DAG);
130 return MipsTargetLowering::LowerOperation(Op, DAG);
134 // Transforms a subgraph in CurDAG if the following pattern is found:
135 // (addc multLo, Lo0), (adde multHi, Hi0),
137 // multHi/Lo: product of multiplication
138 // Lo0: initial value of Lo register
139 // Hi0: initial value of Hi register
140 // Return true if pattern matching was successful.
141 static bool selectMADD(SDNode *ADDENode, SelectionDAG *CurDAG) {
142 // ADDENode's second operand must be a flag output of an ADDC node in order
143 // for the matching to be successful.
144 SDNode *ADDCNode = ADDENode->getOperand(2).getNode();
146 if (ADDCNode->getOpcode() != ISD::ADDC)
149 SDValue MultHi = ADDENode->getOperand(0);
150 SDValue MultLo = ADDCNode->getOperand(0);
151 SDNode *MultNode = MultHi.getNode();
152 unsigned MultOpc = MultHi.getOpcode();
154 // MultHi and MultLo must be generated by the same node,
155 if (MultLo.getNode() != MultNode)
158 // and it must be a multiplication.
159 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
162 // MultLo amd MultHi must be the first and second output of MultNode
164 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
167 // Transform this to a MADD only if ADDENode and ADDCNode are the only users
168 // of the values of MultNode, in which case MultNode will be removed in later
170 // If there exist users other than ADDENode or ADDCNode, this function returns
171 // here, which will result in MultNode being mapped to a single MULT
172 // instruction node rather than a pair of MULT and MADD instructions being
174 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
177 DebugLoc DL = ADDENode->getDebugLoc();
179 // Initialize accumulator.
180 SDValue ACCIn = CurDAG->getNode(MipsISD::InsertLOHI, DL, MVT::Untyped,
181 ADDCNode->getOperand(1),
182 ADDENode->getOperand(1));
184 // create MipsMAdd(u) node
185 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
187 SDValue MAdd = CurDAG->getNode(MultOpc, DL, MVT::Untyped,
188 MultNode->getOperand(0),// Factor 0
189 MultNode->getOperand(1),// Factor 1
192 // replace uses of adde and addc here
193 if (!SDValue(ADDCNode, 0).use_empty()) {
194 SDValue LoIdx = CurDAG->getConstant(Mips::sub_lo, MVT::i32);
195 SDValue LoOut = CurDAG->getNode(MipsISD::ExtractLOHI, DL, MVT::i32, MAdd,
197 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), LoOut);
199 if (!SDValue(ADDENode, 0).use_empty()) {
200 SDValue HiIdx = CurDAG->getConstant(Mips::sub_hi, MVT::i32);
201 SDValue HiOut = CurDAG->getNode(MipsISD::ExtractLOHI, DL, MVT::i32, MAdd,
203 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), HiOut);
210 // Transforms a subgraph in CurDAG if the following pattern is found:
211 // (addc Lo0, multLo), (sube Hi0, multHi),
213 // multHi/Lo: product of multiplication
214 // Lo0: initial value of Lo register
215 // Hi0: initial value of Hi register
216 // Return true if pattern matching was successful.
217 static bool selectMSUB(SDNode *SUBENode, SelectionDAG *CurDAG) {
218 // SUBENode's second operand must be a flag output of an SUBC node in order
219 // for the matching to be successful.
220 SDNode *SUBCNode = SUBENode->getOperand(2).getNode();
222 if (SUBCNode->getOpcode() != ISD::SUBC)
225 SDValue MultHi = SUBENode->getOperand(1);
226 SDValue MultLo = SUBCNode->getOperand(1);
227 SDNode *MultNode = MultHi.getNode();
228 unsigned MultOpc = MultHi.getOpcode();
230 // MultHi and MultLo must be generated by the same node,
231 if (MultLo.getNode() != MultNode)
234 // and it must be a multiplication.
235 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
238 // MultLo amd MultHi must be the first and second output of MultNode
240 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
243 // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
244 // of the values of MultNode, in which case MultNode will be removed in later
246 // If there exist users other than SUBENode or SUBCNode, this function returns
247 // here, which will result in MultNode being mapped to a single MULT
248 // instruction node rather than a pair of MULT and MSUB instructions being
250 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
253 DebugLoc DL = SUBENode->getDebugLoc();
255 // Initialize accumulator.
256 SDValue ACCIn = CurDAG->getNode(MipsISD::InsertLOHI, DL, MVT::Untyped,
257 SUBCNode->getOperand(0),
258 SUBENode->getOperand(0));
260 // create MipsSub(u) node
261 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
263 SDValue MSub = CurDAG->getNode(MultOpc, DL, MVT::Glue,
264 MultNode->getOperand(0),// Factor 0
265 MultNode->getOperand(1),// Factor 1
268 // replace uses of sube and subc here
269 if (!SDValue(SUBCNode, 0).use_empty()) {
270 SDValue LoIdx = CurDAG->getConstant(Mips::sub_lo, MVT::i32);
271 SDValue LoOut = CurDAG->getNode(MipsISD::ExtractLOHI, DL, MVT::i32, MSub,
273 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), LoOut);
275 if (!SDValue(SUBENode, 0).use_empty()) {
276 SDValue HiIdx = CurDAG->getConstant(Mips::sub_hi, MVT::i32);
277 SDValue HiOut = CurDAG->getNode(MipsISD::ExtractLOHI, DL, MVT::i32, MSub,
279 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), HiOut);
285 static SDValue performADDECombine(SDNode *N, SelectionDAG &DAG,
286 TargetLowering::DAGCombinerInfo &DCI,
287 const MipsSubtarget *Subtarget) {
288 if (DCI.isBeforeLegalize())
291 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
293 return SDValue(N, 0);
298 static SDValue performSUBECombine(SDNode *N, SelectionDAG &DAG,
299 TargetLowering::DAGCombinerInfo &DCI,
300 const MipsSubtarget *Subtarget) {
301 if (DCI.isBeforeLegalize())
304 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
306 return SDValue(N, 0);
312 MipsSETargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
313 SelectionDAG &DAG = DCI.DAG;
315 switch (N->getOpcode()) {
317 return performADDECombine(N, DAG, DCI, Subtarget);
319 return performSUBECombine(N, DAG, DCI, Subtarget);
321 return MipsTargetLowering::PerformDAGCombine(N, DCI);
326 MipsSETargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
327 MachineBasicBlock *BB) const {
328 switch (MI->getOpcode()) {
330 return MipsTargetLowering::EmitInstrWithCustomInserter(MI, BB);
331 case Mips::BPOSGE32_PSEUDO:
332 return emitBPOSGE32(MI, BB);
336 bool MipsSETargetLowering::
337 isEligibleForTailCallOptimization(const MipsCC &MipsCCInfo,
338 unsigned NextStackOffset,
339 const MipsFunctionInfo& FI) const {
340 if (!EnableMipsTailCalls)
343 // Return false if either the callee or caller has a byval argument.
344 if (MipsCCInfo.hasByValArg() || FI.hasByvalArg())
347 // Return true if the callee's argument area is no larger than the
349 return NextStackOffset <= FI.getIncomingArgSize();
352 void MipsSETargetLowering::
353 getOpndList(SmallVectorImpl<SDValue> &Ops,
354 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
355 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
356 CallLoweringInfo &CLI, SDValue Callee, SDValue Chain) const {
357 // T9 should contain the address of the callee function if
358 // -reloction-model=pic or it is an indirect call.
359 if (IsPICCall || !GlobalOrExternal) {
360 unsigned T9Reg = IsN64 ? Mips::T9_64 : Mips::T9;
361 RegsToPass.push_front(std::make_pair(T9Reg, Callee));
363 Ops.push_back(Callee);
365 MipsTargetLowering::getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal,
366 InternalLinkage, CLI, Callee, Chain);
369 SDValue MipsSETargetLowering::lowerMulDiv(SDValue Op, unsigned NewOpc,
370 bool HasLo, bool HasHi,
371 SelectionDAG &DAG) const {
372 EVT Ty = Op.getOperand(0).getValueType();
373 DebugLoc DL = Op.getDebugLoc();
374 SDValue Mult = DAG.getNode(NewOpc, DL, MVT::Untyped,
375 Op.getOperand(0), Op.getOperand(1));
379 Lo = DAG.getNode(MipsISD::ExtractLOHI, DL, Ty, Mult,
380 DAG.getConstant(Mips::sub_lo, MVT::i32));
382 Hi = DAG.getNode(MipsISD::ExtractLOHI, DL, Ty, Mult,
383 DAG.getConstant(Mips::sub_hi, MVT::i32));
385 if (!HasLo || !HasHi)
386 return HasLo ? Lo : Hi;
388 SDValue Vals[] = { Lo, Hi };
389 return DAG.getMergeValues(Vals, 2, DL);
392 MachineBasicBlock * MipsSETargetLowering::
393 emitBPOSGE32(MachineInstr *MI, MachineBasicBlock *BB) const{
395 // bposge32_pseudo $vr0
405 // $vr0 = phi($vr2, $fbb, $vr1, $tbb)
407 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
408 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
409 const TargetRegisterClass *RC = &Mips::CPURegsRegClass;
410 DebugLoc DL = MI->getDebugLoc();
411 const BasicBlock *LLVM_BB = BB->getBasicBlock();
412 MachineFunction::iterator It = llvm::next(MachineFunction::iterator(BB));
413 MachineFunction *F = BB->getParent();
414 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
415 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
416 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
421 // Transfer the remainder of BB and its successor edges to Sink.
422 Sink->splice(Sink->begin(), BB, llvm::next(MachineBasicBlock::iterator(MI)),
424 Sink->transferSuccessorsAndUpdatePHIs(BB);
427 BB->addSuccessor(FBB);
428 BB->addSuccessor(TBB);
429 FBB->addSuccessor(Sink);
430 TBB->addSuccessor(Sink);
432 // Insert the real bposge32 instruction to $BB.
433 BuildMI(BB, DL, TII->get(Mips::BPOSGE32)).addMBB(TBB);
436 unsigned VR2 = RegInfo.createVirtualRegister(RC);
437 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), VR2)
438 .addReg(Mips::ZERO).addImm(0);
439 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
442 unsigned VR1 = RegInfo.createVirtualRegister(RC);
443 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), VR1)
444 .addReg(Mips::ZERO).addImm(1);
446 // Insert phi function to $Sink.
447 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
448 MI->getOperand(0).getReg())
449 .addReg(VR2).addMBB(FBB).addReg(VR1).addMBB(TBB);
451 MI->eraseFromParent(); // The pseudo instruction is gone now.