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