626eeb52cfa9295cb5b5b01acebf8234b03dde69
[oota-llvm.git] / lib / Target / MBlaze / MBlazeISelDAGToDAG.cpp
1 //===-- 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"
16 #include "MBlazeMachineFunction.h"
17 #include "MBlazeRegisterInfo.h"
18 #include "MBlazeSubtarget.h"
19 #include "MBlazeTargetMachine.h"
20 #include "llvm/CodeGen/MachineConstantPool.h"
21 #include "llvm/CodeGen/MachineFrameInfo.h"
22 #include "llvm/CodeGen/MachineFunction.h"
23 #include "llvm/CodeGen/MachineInstrBuilder.h"
24 #include "llvm/CodeGen/MachineRegisterInfo.h"
25 #include "llvm/CodeGen/SelectionDAGISel.h"
26 #include "llvm/IR/GlobalValue.h"
27 #include "llvm/IR/Instructions.h"
28 #include "llvm/IR/Intrinsics.h"
29 #include "llvm/IR/Type.h"
30 #include "llvm/Support/CFG.h"
31 #include "llvm/Support/Debug.h"
32 #include "llvm/Support/ErrorHandling.h"
33 #include "llvm/Support/raw_ostream.h"
34 #include "llvm/Target/TargetMachine.h"
35 using 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 //===----------------------------------------------------------------------===//
45 namespace {
46
47 class 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
56 public:
57   explicit MBlazeDAGToDAGISel(MBlazeTargetMachine &tm) :
58   SelectionDAGISel(tm),
59   TM(tm), Subtarget(tm.getSubtarget<MBlazeSubtarget>()) {}
60
61   // Pass Name
62   virtual const char *getPassName() const {
63     return "MBlaze DAG->DAG Pattern Instruction Selection";
64   }
65 private:
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
84   // Address Selection
85   bool SelectAddrRegReg(SDValue N, SDValue &Base, SDValue &Index);
86   bool SelectAddrRegImm(SDValue N, SDValue &Disp, SDValue &Base);
87
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   }
92 };
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.
100 static 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
112 static bool isIntS32Immediate(SDValue Op, int32_t &Imm) {
113   return isIntS32Immediate(Op.getNode(), Imm);
114 }
115
116
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].
120 bool MBlazeDAGToDAGISel::
121 SelectAddrRegReg(SDValue N, SDValue &Base, SDValue &Index) {
122   if (N.getOpcode() == ISD::FrameIndex) return false;
123   if (N.getOpcode() == ISD::TargetExternalSymbol ||
124       N.getOpcode() == ISD::TargetGlobalAddress)
125     return false;  // direct calls.
126
127   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
132     if (N.getOperand(0).getOpcode() == ISD::TargetJumpTable ||
133         N.getOperand(1).getOpcode() == ISD::TargetJumpTable)
134       return false; // jump tables.
135
136     Base = N.getOperand(0);
137     Index = N.getOperand(1);
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.
147 bool MBlazeDAGToDAGISel::
148 SelectAddrRegImm(SDValue N, SDValue &Base, SDValue &Disp) {
149   // If this can be more profitably realized as r+r, fail.
150   if (SelectAddrRegReg(N, Base, Disp))
151     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       return true; // [r+i]
163     }
164   } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
165     // Loading from a constant address.
166     uint32_t Imm = CN->getZExtValue();
167     Disp = CurDAG->getTargetConstant(Imm, CN->getValueType(0));
168     Base = CurDAG->getRegister(MBlaze::R0, CN->getValueType(0));
169     return true;
170   }
171
172   Disp = CurDAG->getTargetConstant(0, TM.getTargetLowering()->getPointerTy());
173   if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
174     Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
175   else
176     Base = N;
177   return true;      // [r+0]
178 }
179
180 /// getGlobalBaseReg - Output the instructions required to put the
181 /// GOT address into a register.
182 SDNode *MBlazeDAGToDAGISel::getGlobalBaseReg() {
183   unsigned GlobalBaseReg = getInstrInfo()->getGlobalBaseReg(MF);
184   return CurDAG->getRegister(GlobalBaseReg,
185                              getTargetLowering()->getPointerTy()).getNode();
186 }
187
188 /// Select instructions not customized! Used for
189 /// expanded, promoted and normal instructions
190 SDNode* MBlazeDAGToDAGISel::Select(SDNode *Node) {
191   unsigned Opcode = Node->getOpcode();
192   SDLoc dl(Node);
193
194   // If we have a custom node, we already have selected!
195   if (Node->isMachineOpcode())
196     return NULL;
197
198   ///
199   // Instruction Selection not handled by the auto-generated
200   // tablegen selection should be handled here.
201   ///
202   switch (Opcode) {
203     default: break;
204
205     // Get target GOT address.
206     case ISD::GLOBAL_OFFSET_TABLE:
207       return getGlobalBaseReg();
208
209     case ISD::FrameIndex: {
210         SDValue imm = CurDAG->getTargetConstant(0, MVT::i32);
211         int FI = dyn_cast<FrameIndexSDNode>(Node)->getIndex();
212         EVT VT = Node->getValueType(0);
213         SDValue TFI = CurDAG->getTargetFrameIndex(FI, VT);
214         unsigned Opc = MBlaze::ADDIK;
215         if (Node->hasOneUse())
216           return CurDAG->SelectNodeTo(Node, Opc, VT, TFI, imm);
217         return CurDAG->getMachineNode(Opc, dl, VT, TFI, imm);
218     }
219
220
221     /// Handle direct and indirect calls when using PIC. On PIC, when
222     /// GOT is smaller than about 64k (small code) the GA target is
223     /// loaded with only one instruction. Otherwise GA's target must
224     /// be loaded with 3 instructions.
225     case MBlazeISD::JmpLink: {
226       if (TM.getRelocationModel() == Reloc::PIC_) {
227         SDValue Chain  = Node->getOperand(0);
228         SDValue Callee = Node->getOperand(1);
229         SDValue R20Reg = CurDAG->getRegister(MBlaze::R20, MVT::i32);
230         SDValue InFlag(0, 0);
231
232         if ((isa<GlobalAddressSDNode>(Callee)) ||
233             (isa<ExternalSymbolSDNode>(Callee)))
234         {
235           /// Direct call for global addresses and external symbols
236           SDValue GPReg = CurDAG->getRegister(MBlaze::R15, MVT::i32);
237
238           // Use load to get GOT target
239           SDValue Ops[] = { Callee, GPReg, Chain };
240           SDValue Load = SDValue(CurDAG->getMachineNode(MBlaze::LW, dl,
241                                  MVT::i32, MVT::Other, Ops), 0);
242           Chain = Load.getValue(1);
243
244           // Call target must be on T9
245           Chain = CurDAG->getCopyToReg(Chain, dl, R20Reg, Load, InFlag);
246         } else
247           /// Indirect call
248           Chain = CurDAG->getCopyToReg(Chain, dl, R20Reg, Callee, InFlag);
249
250         // Emit Jump and Link Register
251         SDNode *ResNode = CurDAG->getMachineNode(MBlaze::BRLID, dl, MVT::Other,
252                                                  MVT::Glue, R20Reg, Chain);
253         Chain  = SDValue(ResNode, 0);
254         InFlag = SDValue(ResNode, 1);
255         ReplaceUses(SDValue(Node, 0), Chain);
256         ReplaceUses(SDValue(Node, 1), InFlag);
257         return ResNode;
258       }
259     }
260   }
261
262   // Select the default instruction
263   SDNode *ResNode = SelectCode(Node);
264
265   DEBUG(errs() << "=> ");
266   if (ResNode == NULL || ResNode == Node)
267     DEBUG(Node->dump(CurDAG));
268   else
269     DEBUG(ResNode->dump(CurDAG));
270   DEBUG(errs() << "\n");
271   return ResNode;
272 }
273
274 /// createMBlazeISelDag - This pass converts a legalized DAG into a
275 /// MBlaze-specific DAG, ready for instruction scheduling.
276 FunctionPass *llvm::createMBlazeISelDag(MBlazeTargetMachine &TM) {
277   return new MBlazeDAGToDAGISel(TM);
278 }