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::LOAD, VecTys[i], Legal);
49 setOperationAction(ISD::STORE, VecTys[i], Legal);
50 setOperationAction(ISD::BITCAST, VecTys[i], Legal);
54 if (!TM.Options.UseSoftFloat) {
55 addRegisterClass(MVT::f32, &Mips::FGR32RegClass);
57 // When dealing with single precision only, use libcalls
58 if (!Subtarget->isSingleFloat()) {
60 addRegisterClass(MVT::f64, &Mips::FGR64RegClass);
62 addRegisterClass(MVT::f64, &Mips::AFGR64RegClass);
66 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Custom);
67 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Custom);
68 setOperationAction(ISD::MULHS, MVT::i32, Custom);
69 setOperationAction(ISD::MULHU, MVT::i32, Custom);
72 setOperationAction(ISD::MULHS, MVT::i64, Custom);
73 setOperationAction(ISD::MULHU, MVT::i64, Custom);
74 setOperationAction(ISD::MUL, MVT::i64, Custom);
77 setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
78 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
79 setOperationAction(ISD::SDIVREM, MVT::i64, Custom);
80 setOperationAction(ISD::UDIVREM, MVT::i64, Custom);
81 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
82 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
83 setOperationAction(ISD::LOAD, MVT::i32, Custom);
84 setOperationAction(ISD::STORE, MVT::i32, Custom);
86 setTargetDAGCombine(ISD::ADDE);
87 setTargetDAGCombine(ISD::SUBE);
89 computeRegisterProperties();
92 const MipsTargetLowering *
93 llvm::createMipsSETargetLowering(MipsTargetMachine &TM) {
94 return new MipsSETargetLowering(TM);
99 MipsSETargetLowering::allowsUnalignedMemoryAccesses(EVT VT, bool *Fast) const {
100 MVT::SimpleValueType SVT = VT.getSimpleVT().SimpleTy;
113 SDValue MipsSETargetLowering::LowerOperation(SDValue Op,
114 SelectionDAG &DAG) const {
115 switch(Op.getOpcode()) {
116 case ISD::SMUL_LOHI: return lowerMulDiv(Op, MipsISD::Mult, true, true, DAG);
117 case ISD::UMUL_LOHI: return lowerMulDiv(Op, MipsISD::Multu, true, true, DAG);
118 case ISD::MULHS: return lowerMulDiv(Op, MipsISD::Mult, false, true, DAG);
119 case ISD::MULHU: return lowerMulDiv(Op, MipsISD::Multu, false, true, DAG);
120 case ISD::MUL: return lowerMulDiv(Op, MipsISD::Mult, true, false, DAG);
121 case ISD::SDIVREM: return lowerMulDiv(Op, MipsISD::DivRem, true, true, DAG);
122 case ISD::UDIVREM: return lowerMulDiv(Op, MipsISD::DivRemU, true, true, DAG);
125 return MipsTargetLowering::LowerOperation(Op, DAG);
129 // Transforms a subgraph in CurDAG if the following pattern is found:
130 // (addc multLo, Lo0), (adde multHi, Hi0),
132 // multHi/Lo: product of multiplication
133 // Lo0: initial value of Lo register
134 // Hi0: initial value of Hi register
135 // Return true if pattern matching was successful.
136 static bool selectMADD(SDNode *ADDENode, SelectionDAG *CurDAG) {
137 // ADDENode's second operand must be a flag output of an ADDC node in order
138 // for the matching to be successful.
139 SDNode *ADDCNode = ADDENode->getOperand(2).getNode();
141 if (ADDCNode->getOpcode() != ISD::ADDC)
144 SDValue MultHi = ADDENode->getOperand(0);
145 SDValue MultLo = ADDCNode->getOperand(0);
146 SDNode *MultNode = MultHi.getNode();
147 unsigned MultOpc = MultHi.getOpcode();
149 // MultHi and MultLo must be generated by the same node,
150 if (MultLo.getNode() != MultNode)
153 // and it must be a multiplication.
154 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
157 // MultLo amd MultHi must be the first and second output of MultNode
159 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
162 // Transform this to a MADD only if ADDENode and ADDCNode are the only users
163 // of the values of MultNode, in which case MultNode will be removed in later
165 // If there exist users other than ADDENode or ADDCNode, this function returns
166 // here, which will result in MultNode being mapped to a single MULT
167 // instruction node rather than a pair of MULT and MADD instructions being
169 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
172 DebugLoc DL = ADDENode->getDebugLoc();
174 // Initialize accumulator.
175 SDValue ACCIn = CurDAG->getNode(MipsISD::InsertLOHI, DL, MVT::Untyped,
176 ADDCNode->getOperand(1),
177 ADDENode->getOperand(1));
179 // create MipsMAdd(u) node
180 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
182 SDValue MAdd = CurDAG->getNode(MultOpc, DL, MVT::Untyped,
183 MultNode->getOperand(0),// Factor 0
184 MultNode->getOperand(1),// Factor 1
187 // replace uses of adde and addc here
188 if (!SDValue(ADDCNode, 0).use_empty()) {
189 SDValue LoIdx = CurDAG->getConstant(Mips::sub_lo, MVT::i32);
190 SDValue LoOut = CurDAG->getNode(MipsISD::ExtractLOHI, DL, MVT::i32, MAdd,
192 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), LoOut);
194 if (!SDValue(ADDENode, 0).use_empty()) {
195 SDValue HiIdx = CurDAG->getConstant(Mips::sub_hi, MVT::i32);
196 SDValue HiOut = CurDAG->getNode(MipsISD::ExtractLOHI, DL, MVT::i32, MAdd,
198 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), HiOut);
205 // Transforms a subgraph in CurDAG if the following pattern is found:
206 // (addc Lo0, multLo), (sube Hi0, multHi),
208 // multHi/Lo: product of multiplication
209 // Lo0: initial value of Lo register
210 // Hi0: initial value of Hi register
211 // Return true if pattern matching was successful.
212 static bool selectMSUB(SDNode *SUBENode, SelectionDAG *CurDAG) {
213 // SUBENode's second operand must be a flag output of an SUBC node in order
214 // for the matching to be successful.
215 SDNode *SUBCNode = SUBENode->getOperand(2).getNode();
217 if (SUBCNode->getOpcode() != ISD::SUBC)
220 SDValue MultHi = SUBENode->getOperand(1);
221 SDValue MultLo = SUBCNode->getOperand(1);
222 SDNode *MultNode = MultHi.getNode();
223 unsigned MultOpc = MultHi.getOpcode();
225 // MultHi and MultLo must be generated by the same node,
226 if (MultLo.getNode() != MultNode)
229 // and it must be a multiplication.
230 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
233 // MultLo amd MultHi must be the first and second output of MultNode
235 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
238 // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
239 // of the values of MultNode, in which case MultNode will be removed in later
241 // If there exist users other than SUBENode or SUBCNode, this function returns
242 // here, which will result in MultNode being mapped to a single MULT
243 // instruction node rather than a pair of MULT and MSUB instructions being
245 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
248 DebugLoc DL = SUBENode->getDebugLoc();
250 // Initialize accumulator.
251 SDValue ACCIn = CurDAG->getNode(MipsISD::InsertLOHI, DL, MVT::Untyped,
252 SUBCNode->getOperand(0),
253 SUBENode->getOperand(0));
255 // create MipsSub(u) node
256 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
258 SDValue MSub = CurDAG->getNode(MultOpc, DL, MVT::Glue,
259 MultNode->getOperand(0),// Factor 0
260 MultNode->getOperand(1),// Factor 1
263 // replace uses of sube and subc here
264 if (!SDValue(SUBCNode, 0).use_empty()) {
265 SDValue LoIdx = CurDAG->getConstant(Mips::sub_lo, MVT::i32);
266 SDValue LoOut = CurDAG->getNode(MipsISD::ExtractLOHI, DL, MVT::i32, MSub,
268 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), LoOut);
270 if (!SDValue(SUBENode, 0).use_empty()) {
271 SDValue HiIdx = CurDAG->getConstant(Mips::sub_hi, MVT::i32);
272 SDValue HiOut = CurDAG->getNode(MipsISD::ExtractLOHI, DL, MVT::i32, MSub,
274 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), HiOut);
280 static SDValue performADDECombine(SDNode *N, SelectionDAG &DAG,
281 TargetLowering::DAGCombinerInfo &DCI,
282 const MipsSubtarget *Subtarget) {
283 if (DCI.isBeforeLegalize())
286 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
288 return SDValue(N, 0);
293 static SDValue performSUBECombine(SDNode *N, SelectionDAG &DAG,
294 TargetLowering::DAGCombinerInfo &DCI,
295 const MipsSubtarget *Subtarget) {
296 if (DCI.isBeforeLegalize())
299 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
301 return SDValue(N, 0);
307 MipsSETargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
308 SelectionDAG &DAG = DCI.DAG;
310 switch (N->getOpcode()) {
312 return performADDECombine(N, DAG, DCI, Subtarget);
314 return performSUBECombine(N, DAG, DCI, Subtarget);
316 return MipsTargetLowering::PerformDAGCombine(N, DCI);
321 MipsSETargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
322 MachineBasicBlock *BB) const {
323 switch (MI->getOpcode()) {
325 return MipsTargetLowering::EmitInstrWithCustomInserter(MI, BB);
326 case Mips::BPOSGE32_PSEUDO:
327 return emitBPOSGE32(MI, BB);
331 bool MipsSETargetLowering::
332 isEligibleForTailCallOptimization(const MipsCC &MipsCCInfo,
333 unsigned NextStackOffset,
334 const MipsFunctionInfo& FI) const {
335 if (!EnableMipsTailCalls)
338 // Return false if either the callee or caller has a byval argument.
339 if (MipsCCInfo.hasByValArg() || FI.hasByvalArg())
342 // Return true if the callee's argument area is no larger than the
344 return NextStackOffset <= FI.getIncomingArgSize();
347 void MipsSETargetLowering::
348 getOpndList(SmallVectorImpl<SDValue> &Ops,
349 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
350 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
351 CallLoweringInfo &CLI, SDValue Callee, SDValue Chain) const {
352 // T9 should contain the address of the callee function if
353 // -reloction-model=pic or it is an indirect call.
354 if (IsPICCall || !GlobalOrExternal) {
355 unsigned T9Reg = IsN64 ? Mips::T9_64 : Mips::T9;
356 RegsToPass.push_front(std::make_pair(T9Reg, Callee));
358 Ops.push_back(Callee);
360 MipsTargetLowering::getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal,
361 InternalLinkage, CLI, Callee, Chain);
364 SDValue MipsSETargetLowering::lowerMulDiv(SDValue Op, unsigned NewOpc,
365 bool HasLo, bool HasHi,
366 SelectionDAG &DAG) const {
367 EVT Ty = Op.getOperand(0).getValueType();
368 DebugLoc DL = Op.getDebugLoc();
369 SDValue Mult = DAG.getNode(NewOpc, DL, MVT::Untyped,
370 Op.getOperand(0), Op.getOperand(1));
374 Lo = DAG.getNode(MipsISD::ExtractLOHI, DL, Ty, Mult,
375 DAG.getConstant(Mips::sub_lo, MVT::i32));
377 Hi = DAG.getNode(MipsISD::ExtractLOHI, DL, Ty, Mult,
378 DAG.getConstant(Mips::sub_hi, MVT::i32));
380 if (!HasLo || !HasHi)
381 return HasLo ? Lo : Hi;
383 SDValue Vals[] = { Lo, Hi };
384 return DAG.getMergeValues(Vals, 2, DL);
387 MachineBasicBlock * MipsSETargetLowering::
388 emitBPOSGE32(MachineInstr *MI, MachineBasicBlock *BB) const{
390 // bposge32_pseudo $vr0
400 // $vr0 = phi($vr2, $fbb, $vr1, $tbb)
402 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
403 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
404 const TargetRegisterClass *RC = &Mips::CPURegsRegClass;
405 DebugLoc DL = MI->getDebugLoc();
406 const BasicBlock *LLVM_BB = BB->getBasicBlock();
407 MachineFunction::iterator It = llvm::next(MachineFunction::iterator(BB));
408 MachineFunction *F = BB->getParent();
409 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
410 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
411 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
416 // Transfer the remainder of BB and its successor edges to Sink.
417 Sink->splice(Sink->begin(), BB, llvm::next(MachineBasicBlock::iterator(MI)),
419 Sink->transferSuccessorsAndUpdatePHIs(BB);
422 BB->addSuccessor(FBB);
423 BB->addSuccessor(TBB);
424 FBB->addSuccessor(Sink);
425 TBB->addSuccessor(Sink);
427 // Insert the real bposge32 instruction to $BB.
428 BuildMI(BB, DL, TII->get(Mips::BPOSGE32)).addMBB(TBB);
431 unsigned VR2 = RegInfo.createVirtualRegister(RC);
432 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), VR2)
433 .addReg(Mips::ZERO).addImm(0);
434 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
437 unsigned VR1 = RegInfo.createVirtualRegister(RC);
438 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), VR1)
439 .addReg(Mips::ZERO).addImm(1);
441 // Insert phi function to $Sink.
442 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
443 MI->getOperand(0).getReg())
444 .addReg(VR2).addMBB(FBB).addReg(VR1).addMBB(TBB);
446 MI->eraseFromParent(); // The pseudo instruction is gone now.