blob: 688ad70c06b02e08d0286a6ad37c26ca26952cf9 [file] [log] [blame]
Wesley Pecka70f28c2010-02-23 19:15:24 +00001//===-- MBlazeISelDAGToDAG.cpp - A dag to dag inst selector for MBlaze ----===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines an instruction selector for the MBlaze target.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "mblaze-isel"
15#include "MBlaze.h"
Wesley Pecka70f28c2010-02-23 19:15:24 +000016#include "MBlazeMachineFunction.h"
17#include "MBlazeRegisterInfo.h"
18#include "MBlazeSubtarget.h"
19#include "MBlazeTargetMachine.h"
20#include "llvm/GlobalValue.h"
21#include "llvm/Instructions.h"
22#include "llvm/Intrinsics.h"
23#include "llvm/Support/CFG.h"
24#include "llvm/Type.h"
25#include "llvm/CodeGen/MachineConstantPool.h"
26#include "llvm/CodeGen/MachineFunction.h"
27#include "llvm/CodeGen/MachineFrameInfo.h"
28#include "llvm/CodeGen/MachineInstrBuilder.h"
29#include "llvm/CodeGen/MachineRegisterInfo.h"
30#include "llvm/CodeGen/SelectionDAGISel.h"
31#include "llvm/Target/TargetMachine.h"
32#include "llvm/Support/Debug.h"
33#include "llvm/Support/ErrorHandling.h"
34#include "llvm/Support/raw_ostream.h"
35using namespace llvm;
36
37//===----------------------------------------------------------------------===//
38// Instruction Selector Implementation
39//===----------------------------------------------------------------------===//
40
41//===----------------------------------------------------------------------===//
42// MBlazeDAGToDAGISel - MBlaze specific code to select MBlaze machine
43// instructions for SelectionDAG operations.
44//===----------------------------------------------------------------------===//
45namespace {
46
47class MBlazeDAGToDAGISel : public SelectionDAGISel {
48
49 /// TM - Keep a reference to MBlazeTargetMachine.
50 MBlazeTargetMachine &TM;
51
52 /// Subtarget - Keep a pointer to the MBlazeSubtarget around so that we can
53 /// make the right decision when generating code for different targets.
54 const MBlazeSubtarget &Subtarget;
55
56public:
57 explicit MBlazeDAGToDAGISel(MBlazeTargetMachine &tm) :
58 SelectionDAGISel(tm),
59 TM(tm), Subtarget(tm.getSubtarget<MBlazeSubtarget>()) {}
60
Wesley Pecka70f28c2010-02-23 19:15:24 +000061 // Pass Name
62 virtual const char *getPassName() const {
63 return "MBlaze DAG->DAG Pattern Instruction Selection";
64 }
65private:
66 // Include the pieces autogenerated from the target description.
67 #include "MBlazeGenDAGISel.inc"
68
69 /// getTargetMachine - Return a reference to the TargetMachine, casted
70 /// to the target-specific type.
71 const MBlazeTargetMachine &getTargetMachine() {
72 return static_cast<const MBlazeTargetMachine &>(TM);
73 }
74
75 /// getInstrInfo - Return a reference to the TargetInstrInfo, casted
76 /// to the target-specific type.
77 const MBlazeInstrInfo *getInstrInfo() {
78 return getTargetMachine().getInstrInfo();
79 }
80
81 SDNode *getGlobalBaseReg();
82 SDNode *Select(SDNode *N);
83
Wesley Pecka70f28c2010-02-23 19:15:24 +000084 // Address Selection
Chris Lattner52a261b2010-09-21 20:31:19 +000085 bool SelectAddrRegReg(SDValue N, SDValue &Base, SDValue &Index);
86 bool SelectAddrRegImm(SDValue N, SDValue &Disp, SDValue &Base);
Wesley Pecka70f28c2010-02-23 19:15:24 +000087
88 // getI32Imm - Return a target constant with the specified value, of type i32.
89 inline SDValue getI32Imm(unsigned Imm) {
90 return CurDAG->getTargetConstant(Imm, MVT::i32);
91 }
Wesley Pecka70f28c2010-02-23 19:15:24 +000092};
93
94}
95
96/// isIntS32Immediate - This method tests to see if the node is either a 32-bit
97/// or 64-bit immediate, and if the value can be accurately represented as a
98/// sign extension from a 32-bit value. If so, this returns true and the
99/// immediate.
100static bool isIntS32Immediate(SDNode *N, int32_t &Imm) {
101 unsigned Opc = N->getOpcode();
102 if (Opc != ISD::Constant)
103 return false;
104
105 Imm = (int32_t)cast<ConstantSDNode>(N)->getZExtValue();
106 if (N->getValueType(0) == MVT::i32)
107 return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue();
108 else
109 return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue();
110}
111
112static bool isIntS32Immediate(SDValue Op, int32_t &Imm) {
113 return isIntS32Immediate(Op.getNode(), Imm);
114}
115
Wesley Pecka70f28c2010-02-23 19:15:24 +0000116
117/// SelectAddressRegReg - Given the specified addressed, check to see if it
118/// can be represented as an indexed [r+r] operation. Returns false if it
119/// can be more efficiently represented with [r+imm].
120bool MBlazeDAGToDAGISel::
Chris Lattner52a261b2010-09-21 20:31:19 +0000121SelectAddrRegReg(SDValue N, SDValue &Base, SDValue &Index) {
Wesley Pecka70f28c2010-02-23 19:15:24 +0000122 if (N.getOpcode() == ISD::FrameIndex) return false;
123 if (N.getOpcode() == ISD::TargetExternalSymbol ||
124 N.getOpcode() == ISD::TargetGlobalAddress)
125 return false; // direct calls.
126
Wesley Pecka70f28c2010-02-23 19:15:24 +0000127 int32_t imm = 0;
128 if (N.getOpcode() == ISD::ADD || N.getOpcode() == ISD::OR) {
129 if (isIntS32Immediate(N.getOperand(1), imm))
130 return false; // r+i
131
Wesley Peckc2bf2bb2010-03-06 23:23:12 +0000132 if (N.getOperand(0).getOpcode() == ISD::TargetJumpTable ||
133 N.getOperand(1).getOpcode() == ISD::TargetJumpTable)
134 return false; // jump tables.
135
Wesley Pecka70f28c2010-02-23 19:15:24 +0000136 Base = N.getOperand(1);
137 Index = N.getOperand(0);
138 return true;
139 }
140
141 return false;
142}
143
144/// Returns true if the address N can be represented by a base register plus
145/// a signed 32-bit displacement [r+imm], and if it is not better
146/// represented as reg+reg.
147bool MBlazeDAGToDAGISel::
Chris Lattner52a261b2010-09-21 20:31:19 +0000148SelectAddrRegImm(SDValue N, SDValue &Disp, SDValue &Base) {
Wesley Pecka70f28c2010-02-23 19:15:24 +0000149 // If this can be more profitably realized as r+r, fail.
Chris Lattner52a261b2010-09-21 20:31:19 +0000150 if (SelectAddrRegReg(N, Disp, Base))
Wesley Pecka70f28c2010-02-23 19:15:24 +0000151 return false;
152
153 if (N.getOpcode() == ISD::ADD || N.getOpcode() == ISD::OR) {
154 int32_t imm = 0;
155 if (isIntS32Immediate(N.getOperand(1), imm)) {
156 Disp = CurDAG->getTargetConstant(imm, MVT::i32);
157 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
158 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
159 } else {
160 Base = N.getOperand(0);
161 }
162 DEBUG( errs() << "WESLEY: Using Operand Immediate\n" );
163 return true; // [r+i]
164 }
165 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
166 // Loading from a constant address.
167 uint32_t Imm = CN->getZExtValue();
168 Disp = CurDAG->getTargetConstant(Imm, CN->getValueType(0));
169 Base = CurDAG->getRegister(MBlaze::R0, CN->getValueType(0));
170 DEBUG( errs() << "WESLEY: Using Constant Node\n" );
171 return true;
172 }
173
174 Disp = CurDAG->getTargetConstant(0, TM.getTargetLowering()->getPointerTy());
175 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
176 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
177 else
178 Base = N;
179 return true; // [r+0]
180}
181
182/// getGlobalBaseReg - Output the instructions required to put the
183/// GOT address into a register.
184SDNode *MBlazeDAGToDAGISel::getGlobalBaseReg() {
185 unsigned GlobalBaseReg = getInstrInfo()->getGlobalBaseReg(MF);
186 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
187}
188
Wesley Pecka70f28c2010-02-23 19:15:24 +0000189/// Select instructions not customized! Used for
190/// expanded, promoted and normal instructions
191SDNode* MBlazeDAGToDAGISel::Select(SDNode *Node) {
192 unsigned Opcode = Node->getOpcode();
193 DebugLoc dl = Node->getDebugLoc();
194
195 // Dump information about the Node being selected
Chris Lattner7c306da2010-03-02 06:34:30 +0000196 DEBUG(errs() << "Selecting: "; Node->dump(CurDAG); errs() << "\n");
Wesley Pecka70f28c2010-02-23 19:15:24 +0000197
198 // If we have a custom node, we already have selected!
199 if (Node->isMachineOpcode()) {
Chris Lattner7c306da2010-03-02 06:34:30 +0000200 DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n");
Wesley Pecka70f28c2010-02-23 19:15:24 +0000201 return NULL;
202 }
203
204 ///
205 // Instruction Selection not handled by the auto-generated
206 // tablegen selection should be handled here.
207 ///
208 switch(Opcode) {
209 default: break;
210
211 // Get target GOT address.
212 case ISD::GLOBAL_OFFSET_TABLE:
213 return getGlobalBaseReg();
214
215 case ISD::FrameIndex: {
216 SDValue imm = CurDAG->getTargetConstant(0, MVT::i32);
217 int FI = dyn_cast<FrameIndexSDNode>(Node)->getIndex();
218 EVT VT = Node->getValueType(0);
219 SDValue TFI = CurDAG->getTargetFrameIndex(FI, VT);
220 unsigned Opc = MBlaze::ADDI;
221 if (Node->hasOneUse())
222 return CurDAG->SelectNodeTo(Node, Opc, VT, TFI, imm);
223 return CurDAG->getMachineNode(Opc, dl, VT, TFI, imm);
224 }
225
226
227 /// Handle direct and indirect calls when using PIC. On PIC, when
228 /// GOT is smaller than about 64k (small code) the GA target is
229 /// loaded with only one instruction. Otherwise GA's target must
230 /// be loaded with 3 instructions.
231 case MBlazeISD::JmpLink: {
232 if (TM.getRelocationModel() == Reloc::PIC_) {
233 SDValue Chain = Node->getOperand(0);
234 SDValue Callee = Node->getOperand(1);
235 SDValue R20Reg = CurDAG->getRegister(MBlaze::R20, MVT::i32);
236 SDValue InFlag(0, 0);
237
238 if ( (isa<GlobalAddressSDNode>(Callee)) ||
239 (isa<ExternalSymbolSDNode>(Callee)) )
240 {
241 /// Direct call for global addresses and external symbols
242 SDValue GPReg = CurDAG->getRegister(MBlaze::R15, MVT::i32);
243
244 // Use load to get GOT target
245 SDValue Ops[] = { Callee, GPReg, Chain };
246 SDValue Load = SDValue(CurDAG->getMachineNode(MBlaze::LW, dl,
247 MVT::i32, MVT::Other, Ops, 3), 0);
248 Chain = Load.getValue(1);
249
250 // Call target must be on T9
251 Chain = CurDAG->getCopyToReg(Chain, dl, R20Reg, Load, InFlag);
252 } else
253 /// Indirect call
254 Chain = CurDAG->getCopyToReg(Chain, dl, R20Reg, Callee, InFlag);
255
256 // Emit Jump and Link Register
257 SDNode *ResNode = CurDAG->getMachineNode(MBlaze::BRLID, dl, MVT::Other,
258 MVT::Flag, R20Reg, Chain);
259 Chain = SDValue(ResNode, 0);
260 InFlag = SDValue(ResNode, 1);
261 ReplaceUses(SDValue(Node, 0), Chain);
262 ReplaceUses(SDValue(Node, 1), InFlag);
263 return ResNode;
264 }
265 }
266 }
267
268 // Select the default instruction
269 SDNode *ResNode = SelectCode(Node);
270
Chris Lattner7c306da2010-03-02 06:34:30 +0000271 DEBUG(errs() << "=> ");
Wesley Pecka70f28c2010-02-23 19:15:24 +0000272 if (ResNode == NULL || ResNode == Node)
273 DEBUG(Node->dump(CurDAG));
274 else
275 DEBUG(ResNode->dump(CurDAG));
276 DEBUG(errs() << "\n");
Wesley Pecka70f28c2010-02-23 19:15:24 +0000277 return ResNode;
278}
279
280/// createMBlazeISelDag - This pass converts a legalized DAG into a
281/// MBlaze-specific DAG, ready for instruction scheduling.
282FunctionPass *llvm::createMBlazeISelDag(MBlazeTargetMachine &TM) {
283 return new MBlazeDAGToDAGISel(TM);
284}