blob: 73533eb0edfcb70330ef9b001c40645ae3abdcd2 [file] [log] [blame]
Jia Liuc5707112012-02-17 08:55:11 +00001//===-- MipsISelDAGToDAG.cpp - A Dag to Dag Inst Selector for Mips --------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00007//
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00008//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00009//
10// This file defines an instruction selector for the MIPS target.
11//
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000012//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000013
14#define DEBUG_TYPE "mips-isel"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000015#include "Mips.h"
Akira Hatanaka57fa3822012-01-25 03:01:35 +000016#include "MipsAnalyzeImmediate.h"
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +000017#include "MipsMachineFunction.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000018#include "MipsRegisterInfo.h"
19#include "MipsSubtarget.h"
20#include "MipsTargetMachine.h"
21#include "llvm/GlobalValue.h"
22#include "llvm/Instructions.h"
23#include "llvm/Intrinsics.h"
24#include "llvm/Support/CFG.h"
25#include "llvm/Type.h"
26#include "llvm/CodeGen/MachineConstantPool.h"
27#include "llvm/CodeGen/MachineFunction.h"
28#include "llvm/CodeGen/MachineFrameInfo.h"
29#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000030#include "llvm/CodeGen/MachineRegisterInfo.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000031#include "llvm/CodeGen/SelectionDAGISel.h"
32#include "llvm/Target/TargetMachine.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000033#include "llvm/Support/Debug.h"
Torok Edwindac237e2009-07-08 20:53:28 +000034#include "llvm/Support/ErrorHandling.h"
35#include "llvm/Support/raw_ostream.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000036using namespace llvm;
37
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000038//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000039// Instruction Selector Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000040//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000041
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000042//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000043// MipsDAGToDAGISel - MIPS specific code to select MIPS machine
44// instructions for SelectionDAG operations.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000045//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000046namespace {
47
Nick Lewycky6726b6d2009-10-25 06:33:48 +000048class MipsDAGToDAGISel : public SelectionDAGISel {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000049
50 /// TM - Keep a reference to MipsTargetMachine.
51 MipsTargetMachine &TM;
52
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000053 /// Subtarget - Keep a pointer to the MipsSubtarget around so that we can
54 /// make the right decision when generating code for different targets.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000055 const MipsSubtarget &Subtarget;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000056
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000057public:
Dan Gohman1002c022008-07-07 18:00:37 +000058 explicit MipsDAGToDAGISel(MipsTargetMachine &tm) :
Dan Gohman79ce2762009-01-15 19:20:50 +000059 SelectionDAGISel(tm),
Dan Gohmanda8ac5f2008-10-03 16:55:19 +000060 TM(tm), Subtarget(tm.getSubtarget<MipsSubtarget>()) {}
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000061
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000062 // Pass Name
63 virtual const char *getPassName() const {
64 return "MIPS DAG->DAG Pattern Instruction Selection";
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000065 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000066
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000067
68private:
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000069 // Include the pieces autogenerated from the target description.
70 #include "MipsGenDAGISel.inc"
71
Dan Gohman99114052009-06-03 20:30:14 +000072 /// getTargetMachine - Return a reference to the TargetMachine, casted
73 /// to the target-specific type.
74 const MipsTargetMachine &getTargetMachine() {
75 return static_cast<const MipsTargetMachine &>(TM);
76 }
77
78 /// getInstrInfo - Return a reference to the TargetInstrInfo, casted
79 /// to the target-specific type.
80 const MipsInstrInfo *getInstrInfo() {
81 return getTargetMachine().getInstrInfo();
82 }
83
84 SDNode *getGlobalBaseReg();
Akira Hatanaka2fd04752011-12-20 23:10:57 +000085
86 std::pair<SDNode*, SDNode*> SelectMULT(SDNode *N, unsigned Opc, DebugLoc dl,
87 EVT Ty, bool HasLo, bool HasHi);
88
Dan Gohmaneeb3a002010-01-05 01:24:18 +000089 SDNode *Select(SDNode *N);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000090
91 // Complex Pattern.
Chris Lattner52a261b2010-09-21 20:31:19 +000092 bool SelectAddr(SDValue N, SDValue &Base, SDValue &Offset);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000093
Akira Hatanakabd150902011-12-07 20:15:01 +000094 // getImm - Return a target constant with the specified value.
Akira Hatanaka4d0eb632011-12-07 20:10:24 +000095 inline SDValue getImm(const SDNode *Node, unsigned Imm) {
96 return CurDAG->getTargetConstant(Imm, Node->getValueType(0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000097 }
Akira Hatanaka21afc632011-06-21 00:40:49 +000098
99 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
100 char ConstraintCode,
101 std::vector<SDValue> &OutOps);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000102};
103
104}
105
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000106
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000107/// getGlobalBaseReg - Output the instructions required to put the
108/// GOT address into a register.
Dan Gohman99114052009-06-03 20:30:14 +0000109SDNode *MipsDAGToDAGISel::getGlobalBaseReg() {
Dan Gohman99114052009-06-03 20:30:14 +0000110 unsigned GlobalBaseReg = getInstrInfo()->getGlobalBaseReg(MF);
111 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000112}
113
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000114/// ComplexPattern used on MipsInstrInfo
115/// Used on Mips Load/Store instructions
116bool MipsDAGToDAGISel::
Akira Hatanakad3ac47f2011-07-07 18:57:00 +0000117SelectAddr(SDValue Addr, SDValue &Base, SDValue &Offset) {
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000118 EVT ValTy = Addr.getValueType();
119 unsigned GPReg = ValTy == MVT::i32 ? Mips::GP : Mips::GP_64;
120
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000121 // if Address is FI, get the TargetFrameIndex.
122 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000123 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy);
124 Offset = CurDAG->getTargetConstant(0, ValTy);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000125 return true;
126 }
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000127
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000128 // on PIC code Load GA
Akira Hatanaka6df7e232011-12-09 01:53:17 +0000129 if (Addr.getOpcode() == MipsISD::Wrapper) {
Akira Hatanakaca074792011-12-08 20:34:32 +0000130 Base = CurDAG->getRegister(GPReg, ValTy);
131 Offset = Addr.getOperand(0);
132 return true;
133 }
134
135 if (TM.getRelocationModel() != Reloc::PIC_) {
Bill Wendling056292f2008-09-16 21:48:12 +0000136 if ((Addr.getOpcode() == ISD::TargetExternalSymbol ||
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000137 Addr.getOpcode() == ISD::TargetGlobalAddress))
138 return false;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000139 }
140
Akira Hatanaka5e069032011-06-02 01:03:14 +0000141 // Addresses of the form FI+const or FI|const
142 if (CurDAG->isBaseWithConstantOffset(Addr)) {
143 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1));
144 if (isInt<16>(CN->getSExtValue())) {
145
146 // If the first operand is a FI, get the TargetFI Node
147 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>
148 (Addr.getOperand(0)))
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000149 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy);
Akira Hatanaka5e069032011-06-02 01:03:14 +0000150 else
151 Base = Addr.getOperand(0);
152
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000153 Offset = CurDAG->getTargetConstant(CN->getZExtValue(), ValTy);
Akira Hatanaka5e069032011-06-02 01:03:14 +0000154 return true;
155 }
156 }
157
Bruno Cardoso Lopes7ff6fa22007-08-18 02:16:30 +0000158 // Operand is a result from an ADD.
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000159 if (Addr.getOpcode() == ISD::ADD) {
Bruno Cardoso Lopes6e0b6582009-11-16 04:33:42 +0000160 // When loading from constant pools, load the lower address part in
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000161 // the instruction itself. Example, instead of:
Bruno Cardoso Lopes6e0b6582009-11-16 04:33:42 +0000162 // lui $2, %hi($CPI1_0)
163 // addiu $2, $2, %lo($CPI1_0)
164 // lwc1 $f0, 0($2)
165 // Generate:
166 // lui $2, %hi($CPI1_0)
167 // lwc1 $f0, %lo($CPI1_0)($2)
Akira Hatanaka89dc8d72011-12-19 19:28:37 +0000168 if (Addr.getOperand(1).getOpcode() == MipsISD::Lo) {
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000169 SDValue LoVal = Addr.getOperand(1);
Akira Hatanaka8b2b7132011-06-24 17:55:19 +0000170 if (isa<ConstantPoolSDNode>(LoVal.getOperand(0)) ||
171 isa<GlobalAddressSDNode>(LoVal.getOperand(0))) {
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000172 Base = Addr.getOperand(0);
173 Offset = LoVal.getOperand(0);
174 return true;
Bruno Cardoso Lopes6e0b6582009-11-16 04:33:42 +0000175 }
176 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000177 }
178
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000179 Base = Addr;
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000180 Offset = CurDAG->getTargetConstant(0, ValTy);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000181 return true;
182}
183
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000184/// Select multiply instructions.
185std::pair<SDNode*, SDNode*>
186MipsDAGToDAGISel::SelectMULT(SDNode *N, unsigned Opc, DebugLoc dl, EVT Ty,
187 bool HasLo, bool HasHi) {
Chad Rosiera32a08c2012-01-06 20:02:49 +0000188 SDNode *Lo = 0, *Hi = 0;
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000189 SDNode *Mul = CurDAG->getMachineNode(Opc, dl, MVT::Glue, N->getOperand(0),
190 N->getOperand(1));
191 SDValue InFlag = SDValue(Mul, 0);
192
193 if (HasLo) {
194 Lo = CurDAG->getMachineNode(Ty == MVT::i32 ? Mips::MFLO : Mips::MFLO64, dl,
195 Ty, MVT::Glue, InFlag);
196 InFlag = SDValue(Lo, 1);
197 }
198 if (HasHi)
199 Hi = CurDAG->getMachineNode(Ty == MVT::i32 ? Mips::MFHI : Mips::MFHI64, dl,
200 Ty, InFlag);
201
202 return std::make_pair(Lo, Hi);
203}
204
205
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000206/// Select instructions not customized! Used for
207/// expanded, promoted and normal instructions
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000208SDNode* MipsDAGToDAGISel::Select(SDNode *Node) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000209 unsigned Opcode = Node->getOpcode();
Dale Johannesena05dca42009-02-04 23:02:30 +0000210 DebugLoc dl = Node->getDebugLoc();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000211
212 // Dump information about the Node being selected
Chris Lattner7c306da2010-03-02 06:34:30 +0000213 DEBUG(errs() << "Selecting: "; Node->dump(CurDAG); errs() << "\n");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000214
215 // If we have a custom node, we already have selected!
Dan Gohmane8be6c62008-07-17 19:10:17 +0000216 if (Node->isMachineOpcode()) {
Chris Lattner7c306da2010-03-02 06:34:30 +0000217 DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000218 return NULL;
219 }
220
221 ///
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000222 // Instruction Selection not handled by the auto-generated
Bruno Cardoso Lopesb42abeb2007-09-24 20:15:11 +0000223 // tablegen selection should be handled here.
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000224 ///
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000225 EVT NodeTy = Node->getValueType(0);
226 unsigned MultOpc;
227
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000228 switch(Opcode) {
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000229 default: break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000230
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000231 case ISD::SUBE:
232 case ISD::ADDE: {
233 SDValue InFlag = Node->getOperand(2), CmpLHS;
234 unsigned Opc = InFlag.getOpcode(); (void)Opc;
235 assert(((Opc == ISD::ADDC || Opc == ISD::ADDE) ||
236 (Opc == ISD::SUBC || Opc == ISD::SUBE)) &&
237 "(ADD|SUB)E flag operand must come from (ADD|SUB)C/E insn");
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000238
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000239 unsigned MOp;
240 if (Opcode == ISD::ADDE) {
241 CmpLHS = InFlag.getValue(0);
242 MOp = Mips::ADDu;
243 } else {
244 CmpLHS = InFlag.getOperand(0);
245 MOp = Mips::SUBu;
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000246 }
247
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000248 SDValue Ops[] = { CmpLHS, InFlag.getOperand(1) };
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000249
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000250 SDValue LHS = Node->getOperand(0);
251 SDValue RHS = Node->getOperand(1);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000252
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000253 EVT VT = LHS.getValueType();
254 SDNode *Carry = CurDAG->getMachineNode(Mips::SLTu, dl, VT, Ops, 2);
255 SDNode *AddCarry = CurDAG->getMachineNode(Mips::ADDu, dl, VT,
256 SDValue(Carry,0), RHS);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000257
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000258 return CurDAG->SelectNodeTo(Node, MOp, VT, MVT::Glue,
259 LHS, SDValue(AddCarry,0));
260 }
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000261
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000262 /// Mul with two results
263 case ISD::SMUL_LOHI:
264 case ISD::UMUL_LOHI: {
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000265 if (NodeTy == MVT::i32)
266 MultOpc = (Opcode == ISD::UMUL_LOHI ? Mips::MULTu : Mips::MULT);
267 else
268 MultOpc = (Opcode == ISD::UMUL_LOHI ? Mips::DMULTu : Mips::DMULT);
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000269
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000270 std::pair<SDNode*, SDNode*> LoHi = SelectMULT(Node, MultOpc, dl, NodeTy,
271 true, true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000272
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000273 if (!SDValue(Node, 0).use_empty())
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000274 ReplaceUses(SDValue(Node, 0), SDValue(LoHi.first, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000275
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000276 if (!SDValue(Node, 1).use_empty())
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000277 ReplaceUses(SDValue(Node, 1), SDValue(LoHi.second, 0));
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000278
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000279 return NULL;
280 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000281
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000282 /// Special Muls
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000283 case ISD::MUL: {
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000284 // Mips32 has a 32-bit three operand mul instruction.
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000285 if (Subtarget.hasMips32() && NodeTy == MVT::i32)
Bruno Cardoso Lopesa8173b92009-11-13 18:49:59 +0000286 break;
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000287 return SelectMULT(Node, NodeTy == MVT::i32 ? Mips::MULT : Mips::DMULT,
288 dl, NodeTy, true, false).first;
289 }
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000290 case ISD::MULHS:
291 case ISD::MULHU: {
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000292 if (NodeTy == MVT::i32)
293 MultOpc = (Opcode == ISD::MULHU ? Mips::MULTu : Mips::MULT);
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000294 else
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000295 MultOpc = (Opcode == ISD::MULHU ? Mips::DMULTu : Mips::DMULT);
296
297 return SelectMULT(Node, MultOpc, dl, NodeTy, false, true).second;
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000298 }
Bruno Cardoso Lopesa8173b92009-11-13 18:49:59 +0000299
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000300 // Get target GOT address.
301 case ISD::GLOBAL_OFFSET_TABLE:
302 return getGlobalBaseReg();
Akira Hatanakaca074792011-12-08 20:34:32 +0000303
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000304 case ISD::ConstantFP: {
305 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(Node);
306 if (Node->getValueType(0) == MVT::f64 && CN->isExactlyValue(+0.0)) {
307 if (Subtarget.hasMips64()) {
308 SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
309 Mips::ZERO_64, MVT::i64);
310 return CurDAG->getMachineNode(Mips::DMTC1, dl, MVT::f64, Zero);
Akira Hatanakaca074792011-12-08 20:34:32 +0000311 }
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000312
313 SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
314 Mips::ZERO, MVT::i32);
315 return CurDAG->getMachineNode(Mips::BuildPairF64, dl, MVT::f64, Zero,
316 Zero);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +0000317 }
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000318 break;
319 }
320
Akira Hatanaka57fa3822012-01-25 03:01:35 +0000321 case ISD::Constant: {
322 const ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Node);
323 unsigned Size = CN->getValueSizeInBits(0);
324
325 if (Size == 32)
326 break;
327
328 MipsAnalyzeImmediate AnalyzeImm;
329 int64_t Imm = CN->getSExtValue();
330
331 const MipsAnalyzeImmediate::InstSeq &Seq =
332 AnalyzeImm.Analyze(Imm, Size, false);
333
334 MipsAnalyzeImmediate::InstSeq::const_iterator Inst = Seq.begin();
335 DebugLoc DL = CN->getDebugLoc();
336 SDNode *RegOpnd;
337 SDValue ImmOpnd = CurDAG->getTargetConstant(SignExtend64<16>(Inst->ImmOpnd),
338 MVT::i64);
339
340 // The first instruction can be a LUi which is different from other
341 // instructions (ADDiu, ORI and SLL) in that it does not have a register
342 // operand.
343 if (Inst->Opc == Mips::LUi64)
344 RegOpnd = CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64, ImmOpnd);
345 else
346 RegOpnd =
347 CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64,
348 CurDAG->getRegister(Mips::ZERO_64, MVT::i64),
349 ImmOpnd);
350
351 // The remaining instructions in the sequence are handled here.
352 for (++Inst; Inst != Seq.end(); ++Inst) {
353 ImmOpnd = CurDAG->getTargetConstant(SignExtend64<16>(Inst->ImmOpnd),
354 MVT::i64);
355 RegOpnd = CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64,
356 SDValue(RegOpnd, 0), ImmOpnd);
357 }
358
359 return RegOpnd;
360 }
361
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000362 case MipsISD::ThreadPointer: {
363 EVT PtrVT = TLI.getPointerTy();
364 unsigned RdhwrOpc, SrcReg, DestReg;
365
366 if (PtrVT == MVT::i32) {
367 RdhwrOpc = Mips::RDHWR;
368 SrcReg = Mips::HWR29;
369 DestReg = Mips::V1;
370 } else {
371 RdhwrOpc = Mips::RDHWR64;
372 SrcReg = Mips::HWR29_64;
373 DestReg = Mips::V1_64;
374 }
375
376 SDNode *Rdhwr =
377 CurDAG->getMachineNode(RdhwrOpc, Node->getDebugLoc(),
378 Node->getValueType(0),
379 CurDAG->getRegister(SrcReg, PtrVT));
380 SDValue Chain = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, DestReg,
381 SDValue(Rdhwr, 0));
382 SDValue ResNode = CurDAG->getCopyFromReg(Chain, dl, DestReg, PtrVT);
383 ReplaceUses(SDValue(Node, 0), ResNode);
384 return ResNode.getNode();
385 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000386 }
387
388 // Select the default instruction
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000389 SDNode *ResNode = SelectCode(Node);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000390
Chris Lattner7c306da2010-03-02 06:34:30 +0000391 DEBUG(errs() << "=> ");
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000392 if (ResNode == NULL || ResNode == Node)
393 DEBUG(Node->dump(CurDAG));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000394 else
395 DEBUG(ResNode->dump(CurDAG));
Chris Lattner893e1c92009-08-23 06:49:22 +0000396 DEBUG(errs() << "\n");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000397 return ResNode;
398}
399
Akira Hatanaka21afc632011-06-21 00:40:49 +0000400bool MipsDAGToDAGISel::
401SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
402 std::vector<SDValue> &OutOps) {
403 assert(ConstraintCode == 'm' && "unexpected asm memory constraint");
404 OutOps.push_back(Op);
405 return false;
406}
407
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000408/// createMipsISelDag - This pass converts a legalized DAG into a
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000409/// MIPS-specific DAG, ready for instruction scheduling.
410FunctionPass *llvm::createMipsISelDag(MipsTargetMachine &TM) {
411 return new MipsDAGToDAGISel(TM);
412}