Remove cyclic dependency in AArch64 libraries
[oota-llvm.git] / lib / Target / AArch64 / AArch64ISelDAGToDAG.cpp
1 //===-- AArch64ISelDAGToDAG.cpp - A dag to dag inst selector for AArch64 --===//
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 AArch64 target.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #define DEBUG_TYPE "aarch64-isel"
15 #include "AArch64.h"
16 #include "AArch64InstrInfo.h"
17 #include "AArch64Subtarget.h"
18 #include "AArch64TargetMachine.h"
19 #include "Utils/AArch64BaseInfo.h"
20 #include "llvm/ADT/APSInt.h"
21 #include "llvm/CodeGen/SelectionDAGISel.h"
22 #include "llvm/IR/GlobalValue.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/raw_ostream.h"
25
26 using namespace llvm;
27
28 //===--------------------------------------------------------------------===//
29 /// AArch64 specific code to select AArch64 machine instructions for
30 /// SelectionDAG operations.
31 ///
32 namespace {
33
34 class AArch64DAGToDAGISel : public SelectionDAGISel {
35   AArch64TargetMachine &TM;
36   const AArch64InstrInfo *TII;
37
38   /// Keep a pointer to the AArch64Subtarget around so that we can
39   /// make the right decision when generating code for different targets.
40   const AArch64Subtarget *Subtarget;
41
42 public:
43   explicit AArch64DAGToDAGISel(AArch64TargetMachine &tm,
44                                CodeGenOpt::Level OptLevel)
45     : SelectionDAGISel(tm, OptLevel), TM(tm),
46       TII(static_cast<const AArch64InstrInfo*>(TM.getInstrInfo())),
47       Subtarget(&TM.getSubtarget<AArch64Subtarget>()) {
48   }
49
50   virtual const char *getPassName() const {
51     return "AArch64 Instruction Selection";
52   }
53
54   // Include the pieces autogenerated from the target description.
55 #include "AArch64GenDAGISel.inc"
56
57   template<unsigned MemSize>
58   bool SelectOffsetUImm12(SDValue N, SDValue &UImm12) {
59     const ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N);
60     if (!CN || CN->getZExtValue() % MemSize != 0
61         || CN->getZExtValue() / MemSize > 0xfff)
62       return false;
63
64     UImm12 =  CurDAG->getTargetConstant(CN->getZExtValue() / MemSize, MVT::i64);
65     return true;
66   }
67
68   template<unsigned RegWidth>
69   bool SelectCVTFixedPosOperand(SDValue N, SDValue &FixedPos) {
70     return SelectCVTFixedPosOperand(N, FixedPos, RegWidth);
71   }
72
73   bool SelectFPZeroOperand(SDValue N, SDValue &Dummy);
74
75   bool SelectCVTFixedPosOperand(SDValue N, SDValue &FixedPos, unsigned RegWidth);
76
77   bool SelectInlineAsmMemoryOperand(const SDValue &Op,
78                                     char ConstraintCode,
79                                     std::vector<SDValue> &OutOps);
80
81   bool SelectLogicalImm(SDValue N, SDValue &Imm);
82
83   template<unsigned RegWidth>
84   bool SelectTSTBOperand(SDValue N, SDValue &FixedPos) {
85     return SelectTSTBOperand(N, FixedPos, RegWidth);
86   }
87
88   bool SelectTSTBOperand(SDValue N, SDValue &FixedPos, unsigned RegWidth);
89
90   SDNode *TrySelectToMoveImm(SDNode *N);
91   SDNode *SelectToLitPool(SDNode *N);
92   SDNode *SelectToFPLitPool(SDNode *N);
93
94   SDNode* Select(SDNode*);
95 private:
96 };
97 }
98
99 bool
100 AArch64DAGToDAGISel::SelectCVTFixedPosOperand(SDValue N, SDValue &FixedPos,
101                                               unsigned RegWidth) {
102   const ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N);
103   if (!CN) return false;
104
105   // An FCVT[SU] instruction performs: convertToInt(Val * 2^fbits) where fbits
106   // is between 1 and 32 for a destination w-register, or 1 and 64 for an
107   // x-register.
108   //
109   // By this stage, we've detected (fp_to_[su]int (fmul Val, THIS_NODE)) so we
110   // want THIS_NODE to be 2^fbits. This is much easier to deal with using
111   // integers.
112   bool IsExact;
113
114   // fbits is between 1 and 64 in the worst-case, which means the fmul
115   // could have 2^64 as an actual operand. Need 65 bits of precision.
116   APSInt IntVal(65, true);
117   CN->getValueAPF().convertToInteger(IntVal, APFloat::rmTowardZero, &IsExact);
118
119   // N.b. isPowerOf2 also checks for > 0.
120   if (!IsExact || !IntVal.isPowerOf2()) return false;
121   unsigned FBits = IntVal.logBase2();
122
123   // Checks above should have guaranteed that we haven't lost information in
124   // finding FBits, but it must still be in range.
125   if (FBits == 0 || FBits > RegWidth) return false;
126
127   FixedPos = CurDAG->getTargetConstant(64 - FBits, MVT::i32);
128   return true;
129 }
130
131 bool
132 AArch64DAGToDAGISel::SelectInlineAsmMemoryOperand(const SDValue &Op,
133                                                   char ConstraintCode,
134                                                   std::vector<SDValue> &OutOps) {
135   switch (ConstraintCode) {
136   default: llvm_unreachable("Unrecognised AArch64 memory constraint");
137   case 'm':
138     // FIXME: more freedom is actually permitted for 'm'. We can go
139     // hunting for a base and an offset if we want. Of course, since
140     // we don't really know how the operand is going to be used we're
141     // probably restricted to the load/store pair's simm7 as an offset
142     // range anyway.
143   case 'Q':
144     OutOps.push_back(Op);
145   }
146
147   return false;
148 }
149
150 bool
151 AArch64DAGToDAGISel::SelectFPZeroOperand(SDValue N, SDValue &Dummy) {
152   ConstantFPSDNode *Imm = dyn_cast<ConstantFPSDNode>(N);
153   if (!Imm || !Imm->getValueAPF().isPosZero())
154     return false;
155   
156   // Doesn't actually carry any information, but keeps TableGen quiet.
157   Dummy = CurDAG->getTargetConstant(0, MVT::i32);
158   return true;
159 }
160
161 bool AArch64DAGToDAGISel::SelectLogicalImm(SDValue N, SDValue &Imm) {
162   uint32_t Bits;
163   uint32_t RegWidth = N.getValueType().getSizeInBits();
164
165   ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N);
166   if (!CN) return false;
167
168   if (!A64Imms::isLogicalImm(RegWidth, CN->getZExtValue(), Bits))
169     return false;
170
171   Imm = CurDAG->getTargetConstant(Bits, MVT::i32);
172   return true;
173 }
174
175 SDNode *AArch64DAGToDAGISel::TrySelectToMoveImm(SDNode *Node) {
176   SDNode *ResNode;
177   DebugLoc dl = Node->getDebugLoc();
178   EVT DestType = Node->getValueType(0);
179   unsigned DestWidth = DestType.getSizeInBits();
180
181   unsigned MOVOpcode;
182   EVT MOVType;
183   int UImm16, Shift;
184   uint32_t LogicalBits;
185
186   uint64_t BitPat = cast<ConstantSDNode>(Node)->getZExtValue();
187   if (A64Imms::isMOVZImm(DestWidth, BitPat, UImm16, Shift)) {
188     MOVType = DestType;
189     MOVOpcode = DestWidth == 64 ? AArch64::MOVZxii : AArch64::MOVZwii;
190   } else if (A64Imms::isMOVNImm(DestWidth, BitPat, UImm16, Shift)) {
191     MOVType = DestType;
192     MOVOpcode = DestWidth == 64 ? AArch64::MOVNxii : AArch64::MOVNwii;
193   } else if (DestWidth == 64 && A64Imms::isMOVNImm(32, BitPat, UImm16, Shift)) {
194     // To get something like 0x0000_0000_ffff_1234 into a 64-bit register we can
195     // use a 32-bit instruction: "movn w0, 0xedbc".
196     MOVType = MVT::i32;
197     MOVOpcode = AArch64::MOVNwii;
198   } else if (A64Imms::isLogicalImm(DestWidth, BitPat, LogicalBits))  {
199     MOVOpcode = DestWidth == 64 ? AArch64::ORRxxi : AArch64::ORRwwi;
200     uint16_t ZR = DestWidth == 64 ? AArch64::XZR : AArch64::WZR;
201
202     return CurDAG->getMachineNode(MOVOpcode, dl, DestType,
203                               CurDAG->getRegister(ZR, DestType),
204                               CurDAG->getTargetConstant(LogicalBits, MVT::i32));
205   } else {
206     // Can't handle it in one instruction. There's scope for permitting two (or
207     // more) instructions, but that'll need more thought.
208     return NULL;
209   }
210
211   ResNode = CurDAG->getMachineNode(MOVOpcode, dl, MOVType,
212                                    CurDAG->getTargetConstant(UImm16, MVT::i32),
213                                    CurDAG->getTargetConstant(Shift, MVT::i32));
214
215   if (MOVType != DestType) {
216     ResNode = CurDAG->getMachineNode(TargetOpcode::SUBREG_TO_REG, dl,
217                           MVT::i64, MVT::i32, MVT::Other,
218                           CurDAG->getTargetConstant(0, MVT::i64),
219                           SDValue(ResNode, 0),
220                           CurDAG->getTargetConstant(AArch64::sub_32, MVT::i32));
221   }
222
223   return ResNode;
224 }
225
226 SDNode *AArch64DAGToDAGISel::SelectToLitPool(SDNode *Node) {
227   DebugLoc dl = Node->getDebugLoc();
228   uint64_t UnsignedVal = cast<ConstantSDNode>(Node)->getZExtValue();
229   int64_t SignedVal = cast<ConstantSDNode>(Node)->getSExtValue();
230   EVT DestType = Node->getValueType(0);
231
232   // Since we may end up loading a 64-bit constant from a 32-bit entry the
233   // constant in the pool may have a different type to the eventual node.
234   SDValue PoolEntry;
235   EVT LoadType;
236   unsigned LoadInst;
237
238   assert((DestType == MVT::i64 || DestType == MVT::i32)
239          && "Only expect integer constants at the moment");
240
241   if (DestType == MVT::i32 || UnsignedVal <= UINT32_MAX) {
242     // LDR w3, lbl
243     LoadInst = AArch64::LDRw_lit;
244     LoadType = MVT::i32;
245
246     PoolEntry = CurDAG->getTargetConstantPool(
247       ConstantInt::get(Type::getInt32Ty(*CurDAG->getContext()), UnsignedVal),
248       MVT::i32);
249   } else if (SignedVal >= INT32_MIN && SignedVal <= INT32_MAX) {
250     // We can use a sign-extending 32-bit load: LDRSW x3, lbl
251     LoadInst = AArch64::LDRSWx_lit;
252     LoadType = MVT::i64;
253
254     PoolEntry = CurDAG->getTargetConstantPool(
255       ConstantInt::getSigned(Type::getInt32Ty(*CurDAG->getContext()),
256                              SignedVal),
257       MVT::i32);
258   } else {
259     // Full 64-bit load needed: LDR x3, lbl
260     LoadInst = AArch64::LDRx_lit;
261     LoadType = MVT::i64;
262
263     PoolEntry = CurDAG->getTargetConstantPool(
264       ConstantInt::get(Type::getInt64Ty(*CurDAG->getContext()), UnsignedVal),
265       MVT::i64);
266   }
267
268   SDNode *ResNode = CurDAG->getMachineNode(LoadInst, dl,
269                                            LoadType, MVT::Other,
270                                            PoolEntry, CurDAG->getEntryNode());
271
272   if (DestType != LoadType) {
273     // We used the implicit zero-extension of "LDR w3, lbl", tell LLVM this
274     // fact.
275     assert(DestType == MVT::i64 && LoadType == MVT::i32
276            && "Unexpected load combination");
277
278     ResNode = CurDAG->getMachineNode(TargetOpcode::SUBREG_TO_REG, dl,
279                           MVT::i64, MVT::i32, MVT::Other,
280                           CurDAG->getTargetConstant(0, MVT::i64),
281                           SDValue(ResNode, 0),
282                           CurDAG->getTargetConstant(AArch64::sub_32, MVT::i32));
283   }
284
285   return ResNode;
286 }
287
288 SDNode *AArch64DAGToDAGISel::SelectToFPLitPool(SDNode *Node) {
289   DebugLoc dl = Node->getDebugLoc();
290   const ConstantFP *FV = cast<ConstantFPSDNode>(Node)->getConstantFPValue();
291   EVT DestType = Node->getValueType(0);
292
293   unsigned LoadInst;
294   switch (DestType.getSizeInBits()) {
295   case 32:
296       LoadInst = AArch64::LDRs_lit;
297       break;
298   case 64:
299       LoadInst = AArch64::LDRd_lit;
300       break;
301   case 128:
302       LoadInst = AArch64::LDRq_lit;
303       break;
304   default: llvm_unreachable("cannot select floating-point litpool");
305   }
306
307   SDValue PoolEntry = CurDAG->getTargetConstantPool(FV, DestType);
308   SDNode *ResNode = CurDAG->getMachineNode(LoadInst, dl,
309                                            DestType, MVT::Other,
310                                            PoolEntry, CurDAG->getEntryNode());
311
312   return ResNode;
313 }
314
315 bool
316 AArch64DAGToDAGISel::SelectTSTBOperand(SDValue N, SDValue &FixedPos,
317                                        unsigned RegWidth) {
318   const ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N);
319   if (!CN) return false;
320
321   uint64_t Val = CN->getZExtValue();
322
323   if (!isPowerOf2_64(Val)) return false;
324
325   unsigned TestedBit = Log2_64(Val);
326   // Checks above should have guaranteed that we haven't lost information in
327   // finding TestedBit, but it must still be in range.
328   if (TestedBit >= RegWidth) return false;
329
330   FixedPos = CurDAG->getTargetConstant(TestedBit, MVT::i64);
331   return true;
332 }
333
334 SDNode *AArch64DAGToDAGISel::Select(SDNode *Node) {
335   // Dump information about the Node being selected
336   DEBUG(dbgs() << "Selecting: "; Node->dump(CurDAG); dbgs() << "\n");
337
338   if (Node->isMachineOpcode()) {
339     DEBUG(dbgs() << "== "; Node->dump(CurDAG); dbgs() << "\n");
340     return NULL;
341   }
342
343   switch (Node->getOpcode()) {
344   case ISD::FrameIndex: {
345     int FI = cast<FrameIndexSDNode>(Node)->getIndex();
346     EVT PtrTy = TLI.getPointerTy();
347     SDValue TFI = CurDAG->getTargetFrameIndex(FI, PtrTy);
348     return CurDAG->SelectNodeTo(Node, AArch64::ADDxxi_lsl0_s, PtrTy,
349                                 TFI, CurDAG->getTargetConstant(0, PtrTy));
350   }
351   case ISD::ConstantPool: {
352     // Constant pools are fine, just create a Target entry.
353     ConstantPoolSDNode *CN = cast<ConstantPoolSDNode>(Node);
354     const Constant *C = CN->getConstVal();
355     SDValue CP = CurDAG->getTargetConstantPool(C, CN->getValueType(0));
356
357     ReplaceUses(SDValue(Node, 0), CP);
358     return NULL;
359   }
360   case ISD::Constant: {
361     SDNode *ResNode = 0;
362     if (cast<ConstantSDNode>(Node)->getZExtValue() == 0) {
363       // XZR and WZR are probably even better than an actual move: most of the
364       // time they can be folded into another instruction with *no* cost.
365
366       EVT Ty = Node->getValueType(0);
367       assert((Ty == MVT::i32 || Ty == MVT::i64) && "unexpected type");
368       uint16_t Register = Ty == MVT::i32 ? AArch64::WZR : AArch64::XZR;
369       ResNode = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
370                                        Node->getDebugLoc(),
371                                        Register, Ty).getNode();
372     }
373
374     // Next best option is a move-immediate, see if we can do that.
375     if (!ResNode) {
376       ResNode = TrySelectToMoveImm(Node);
377     }
378
379     // If even that fails we fall back to a lit-pool entry at the moment. Future
380     // tuning or restrictions like non-readable code-sections may mandate a
381     // sequence of MOVZ/MOVN/MOVK instructions.
382     if (!ResNode) {
383       ResNode = SelectToLitPool(Node);
384     }
385
386     assert(ResNode && "We need *some* way to materialise a constant");
387
388     ReplaceUses(SDValue(Node, 0), SDValue(ResNode, 0));
389     return NULL;
390   }
391   case ISD::ConstantFP: {
392     if (A64Imms::isFPImm(cast<ConstantFPSDNode>(Node)->getValueAPF())) {
393       // FMOV will take care of it from TableGen
394       break;
395     }
396
397     SDNode *ResNode = SelectToFPLitPool(Node);
398     ReplaceUses(SDValue(Node, 0), SDValue(ResNode, 0));
399     return NULL;
400   }
401   default:
402     break; // Let generic code handle it
403   }
404
405   SDNode *ResNode = SelectCode(Node);
406
407   DEBUG(dbgs() << "=> ";
408         if (ResNode == NULL || ResNode == Node)
409           Node->dump(CurDAG);
410         else
411           ResNode->dump(CurDAG);
412         dbgs() << "\n");
413
414   return ResNode;
415 }
416
417 /// This pass converts a legalized DAG into a AArch64-specific DAG, ready for
418 /// instruction scheduling.
419 FunctionPass *llvm::createAArch64ISelDAG(AArch64TargetMachine &TM,
420                                          CodeGenOpt::Level OptLevel) {
421   return new AArch64DAGToDAGISel(TM, OptLevel);
422 }