1 //===-- MipsSEISelLowering.h - 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
30 addRegisterClass(MVT::i32, &Mips::CPURegsRegClass);
33 addRegisterClass(MVT::i64, &Mips::CPU64RegsRegClass);
35 if (Subtarget->hasDSP()) {
36 MVT::SimpleValueType VecTys[2] = {MVT::v2i16, MVT::v4i8};
38 for (unsigned i = 0; i < array_lengthof(VecTys); ++i) {
39 addRegisterClass(VecTys[i], &Mips::DSPRegsRegClass);
41 // Expand all builtin opcodes.
42 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
43 setOperationAction(Opc, VecTys[i], Expand);
45 setOperationAction(ISD::LOAD, VecTys[i], Legal);
46 setOperationAction(ISD::STORE, VecTys[i], Legal);
47 setOperationAction(ISD::BITCAST, VecTys[i], Legal);
51 if (!TM.Options.UseSoftFloat) {
52 addRegisterClass(MVT::f32, &Mips::FGR32RegClass);
54 // When dealing with single precision only, use libcalls
55 if (!Subtarget->isSingleFloat()) {
57 addRegisterClass(MVT::f64, &Mips::FGR64RegClass);
59 addRegisterClass(MVT::f64, &Mips::AFGR64RegClass);
63 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
64 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
65 setOperationAction(ISD::LOAD, MVT::i32, Custom);
66 setOperationAction(ISD::STORE, MVT::i32, Custom);
68 computeRegisterProperties();
71 const MipsTargetLowering *
72 llvm::createMipsSETargetLowering(MipsTargetMachine &TM) {
73 return new MipsSETargetLowering(TM);
78 MipsSETargetLowering::allowsUnalignedMemoryAccesses(EVT VT, bool *Fast) const {
79 MVT::SimpleValueType SVT = VT.getSimpleVT().SimpleTy;
93 MipsSETargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
94 MachineBasicBlock *BB) const {
95 switch (MI->getOpcode()) {
97 return MipsTargetLowering::EmitInstrWithCustomInserter(MI, BB);
98 case Mips::BPOSGE32_PSEUDO:
99 return emitBPOSGE32(MI, BB);
103 bool MipsSETargetLowering::
104 isEligibleForTailCallOptimization(const MipsCC &MipsCCInfo,
105 unsigned NextStackOffset,
106 const MipsFunctionInfo& FI) const {
107 if (!EnableMipsTailCalls)
110 // No tail call optimization for mips16.
111 if (Subtarget->inMips16Mode())
114 // Return false if either the callee or caller has a byval argument.
115 if (MipsCCInfo.hasByValArg() || FI.hasByvalArg())
118 // Return true if the callee's argument area is no larger than the
120 return NextStackOffset <= FI.getIncomingArgSize();
123 void MipsSETargetLowering::
124 getOpndList(SmallVectorImpl<SDValue> &Ops,
125 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
126 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
127 CallLoweringInfo &CLI, SDValue Callee, SDValue Chain) const {
128 // T9 should contain the address of the callee function if
129 // -reloction-model=pic or it is an indirect call.
130 if (IsPICCall || !GlobalOrExternal) {
131 unsigned T9Reg = IsN64 ? Mips::T9_64 : Mips::T9;
132 RegsToPass.push_front(std::make_pair(T9Reg, Callee));
134 Ops.push_back(Callee);
136 MipsTargetLowering::getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal,
137 InternalLinkage, CLI, Callee, Chain);
140 MachineBasicBlock * MipsSETargetLowering::
141 emitBPOSGE32(MachineInstr *MI, MachineBasicBlock *BB) const{
143 // bposge32_pseudo $vr0
153 // $vr0 = phi($vr2, $fbb, $vr1, $tbb)
155 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
156 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
157 const TargetRegisterClass *RC = &Mips::CPURegsRegClass;
158 DebugLoc DL = MI->getDebugLoc();
159 const BasicBlock *LLVM_BB = BB->getBasicBlock();
160 MachineFunction::iterator It = llvm::next(MachineFunction::iterator(BB));
161 MachineFunction *F = BB->getParent();
162 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
163 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
164 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
169 // Transfer the remainder of BB and its successor edges to Sink.
170 Sink->splice(Sink->begin(), BB, llvm::next(MachineBasicBlock::iterator(MI)),
172 Sink->transferSuccessorsAndUpdatePHIs(BB);
175 BB->addSuccessor(FBB);
176 BB->addSuccessor(TBB);
177 FBB->addSuccessor(Sink);
178 TBB->addSuccessor(Sink);
180 // Insert the real bposge32 instruction to $BB.
181 BuildMI(BB, DL, TII->get(Mips::BPOSGE32)).addMBB(TBB);
184 unsigned VR2 = RegInfo.createVirtualRegister(RC);
185 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), VR2)
186 .addReg(Mips::ZERO).addImm(0);
187 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
190 unsigned VR1 = RegInfo.createVirtualRegister(RC);
191 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), VR1)
192 .addReg(Mips::ZERO).addImm(1);
194 // Insert phi function to $Sink.
195 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
196 MI->getOperand(0).getReg())
197 .addReg(VR2).addMBB(FBB).addReg(VR1).addMBB(TBB);
199 MI->eraseFromParent(); // The pseudo instruction is gone now.