1 //===-- AArch64ISelDAGToDAG.cpp - A dag to dag inst selector for AArch64 --===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines an instruction selector for the AArch64 target.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "aarch64-isel"
16 #include "AArch64InstrInfo.h"
17 #include "AArch64Subtarget.h"
18 #include "AArch64TargetMachine.h"
19 #include "MCTargetDesc/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"
28 //===--------------------------------------------------------------------===//
29 /// AArch64 specific code to select AArch64 machine instructions for
30 /// SelectionDAG operations.
34 class AArch64DAGToDAGISel : public SelectionDAGISel {
35 AArch64TargetMachine &TM;
36 const AArch64InstrInfo *TII;
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;
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>()) {
50 virtual const char *getPassName() const {
51 return "AArch64 Instruction Selection";
54 // Include the pieces autogenerated from the target description.
55 #include "AArch64GenDAGISel.inc"
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)
64 UImm12 = CurDAG->getTargetConstant(CN->getZExtValue() / MemSize, MVT::i64);
68 template<unsigned RegWidth>
69 bool SelectCVTFixedPosOperand(SDValue N, SDValue &FixedPos) {
70 return SelectCVTFixedPosOperand(N, FixedPos, RegWidth);
73 bool SelectFPZeroOperand(SDValue N, SDValue &Dummy);
75 bool SelectCVTFixedPosOperand(SDValue N, SDValue &FixedPos, unsigned RegWidth);
77 bool SelectInlineAsmMemoryOperand(const SDValue &Op,
79 std::vector<SDValue> &OutOps);
81 bool SelectLogicalImm(SDValue N, SDValue &Imm);
83 template<unsigned RegWidth>
84 bool SelectTSTBOperand(SDValue N, SDValue &FixedPos) {
85 return SelectTSTBOperand(N, FixedPos, RegWidth);
88 bool SelectTSTBOperand(SDValue N, SDValue &FixedPos, unsigned RegWidth);
90 SDNode *TrySelectToMoveImm(SDNode *N);
91 SDNode *SelectToLitPool(SDNode *N);
92 SDNode *SelectToFPLitPool(SDNode *N);
94 SDNode* Select(SDNode*);
100 AArch64DAGToDAGISel::SelectCVTFixedPosOperand(SDValue N, SDValue &FixedPos,
102 const ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N);
103 if (!CN) return false;
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
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
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);
119 // N.b. isPowerOf2 also checks for > 0.
120 if (!IsExact || !IntVal.isPowerOf2()) return false;
121 unsigned FBits = IntVal.logBase2();
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;
127 FixedPos = CurDAG->getTargetConstant(64 - FBits, MVT::i32);
132 AArch64DAGToDAGISel::SelectInlineAsmMemoryOperand(const SDValue &Op,
134 std::vector<SDValue> &OutOps) {
135 switch (ConstraintCode) {
136 default: llvm_unreachable("Unrecognised AArch64 memory constraint");
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
144 OutOps.push_back(Op);
151 AArch64DAGToDAGISel::SelectFPZeroOperand(SDValue N, SDValue &Dummy) {
152 ConstantFPSDNode *Imm = dyn_cast<ConstantFPSDNode>(N);
153 if (!Imm || !Imm->getValueAPF().isPosZero())
156 // Doesn't actually carry any information, but keeps TableGen quiet.
157 Dummy = CurDAG->getTargetConstant(0, MVT::i32);
161 bool AArch64DAGToDAGISel::SelectLogicalImm(SDValue N, SDValue &Imm) {
163 uint32_t RegWidth = N.getValueType().getSizeInBits();
165 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N);
166 if (!CN) return false;
168 if (!A64Imms::isLogicalImm(RegWidth, CN->getZExtValue(), Bits))
171 Imm = CurDAG->getTargetConstant(Bits, MVT::i32);
175 SDNode *AArch64DAGToDAGISel::TrySelectToMoveImm(SDNode *Node) {
177 DebugLoc dl = Node->getDebugLoc();
178 EVT DestType = Node->getValueType(0);
179 unsigned DestWidth = DestType.getSizeInBits();
184 uint32_t LogicalBits;
186 uint64_t BitPat = cast<ConstantSDNode>(Node)->getZExtValue();
187 if (A64Imms::isMOVZImm(DestWidth, BitPat, UImm16, Shift)) {
189 MOVOpcode = DestWidth == 64 ? AArch64::MOVZxii : AArch64::MOVZwii;
190 } else if (A64Imms::isMOVNImm(DestWidth, BitPat, UImm16, Shift)) {
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".
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;
202 return CurDAG->getMachineNode(MOVOpcode, dl, DestType,
203 CurDAG->getRegister(ZR, DestType),
204 CurDAG->getTargetConstant(LogicalBits, MVT::i32));
206 // Can't handle it in one instruction. There's scope for permitting two (or
207 // more) instructions, but that'll need more thought.
211 ResNode = CurDAG->getMachineNode(MOVOpcode, dl, MOVType,
212 CurDAG->getTargetConstant(UImm16, MVT::i32),
213 CurDAG->getTargetConstant(Shift, MVT::i32));
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),
220 CurDAG->getTargetConstant(AArch64::sub_32, MVT::i32));
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);
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.
238 assert((DestType == MVT::i64 || DestType == MVT::i32)
239 && "Only expect integer constants at the moment");
241 if (DestType == MVT::i32 || UnsignedVal <= UINT32_MAX) {
243 LoadInst = AArch64::LDRw_lit;
246 PoolEntry = CurDAG->getTargetConstantPool(
247 ConstantInt::get(Type::getInt32Ty(*CurDAG->getContext()), UnsignedVal),
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;
254 PoolEntry = CurDAG->getTargetConstantPool(
255 ConstantInt::getSigned(Type::getInt32Ty(*CurDAG->getContext()),
259 // Full 64-bit load needed: LDR x3, lbl
260 LoadInst = AArch64::LDRx_lit;
263 PoolEntry = CurDAG->getTargetConstantPool(
264 ConstantInt::get(Type::getInt64Ty(*CurDAG->getContext()), UnsignedVal),
268 SDNode *ResNode = CurDAG->getMachineNode(LoadInst, dl,
269 LoadType, MVT::Other,
270 PoolEntry, CurDAG->getEntryNode());
272 if (DestType != LoadType) {
273 // We used the implicit zero-extension of "LDR w3, lbl", tell LLVM this
275 assert(DestType == MVT::i64 && LoadType == MVT::i32
276 && "Unexpected load combination");
278 ResNode = CurDAG->getMachineNode(TargetOpcode::SUBREG_TO_REG, dl,
279 MVT::i64, MVT::i32, MVT::Other,
280 CurDAG->getTargetConstant(0, MVT::i64),
282 CurDAG->getTargetConstant(AArch64::sub_32, MVT::i32));
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);
294 switch (DestType.getSizeInBits()) {
296 LoadInst = AArch64::LDRs_lit;
299 LoadInst = AArch64::LDRd_lit;
302 LoadInst = AArch64::LDRq_lit;
304 default: llvm_unreachable("cannot select floating-point litpool");
307 SDValue PoolEntry = CurDAG->getTargetConstantPool(FV, DestType);
308 SDNode *ResNode = CurDAG->getMachineNode(LoadInst, dl,
309 DestType, MVT::Other,
310 PoolEntry, CurDAG->getEntryNode());
316 AArch64DAGToDAGISel::SelectTSTBOperand(SDValue N, SDValue &FixedPos,
318 const ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N);
319 if (!CN) return false;
321 uint64_t Val = CN->getZExtValue();
323 if (!isPowerOf2_64(Val)) return false;
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;
330 FixedPos = CurDAG->getTargetConstant(TestedBit, MVT::i64);
334 SDNode *AArch64DAGToDAGISel::Select(SDNode *Node) {
335 // Dump information about the Node being selected
336 DEBUG(dbgs() << "Selecting: "; Node->dump(CurDAG); dbgs() << "\n");
338 if (Node->isMachineOpcode()) {
339 DEBUG(dbgs() << "== "; Node->dump(CurDAG); dbgs() << "\n");
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));
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));
357 ReplaceUses(SDValue(Node, 0), CP);
360 case ISD::Constant: {
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.
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(),
371 Register, Ty).getNode();
374 // Next best option is a move-immediate, see if we can do that.
376 ResNode = TrySelectToMoveImm(Node);
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.
383 ResNode = SelectToLitPool(Node);
386 assert(ResNode && "We need *some* way to materialise a constant");
388 ReplaceUses(SDValue(Node, 0), SDValue(ResNode, 0));
391 case ISD::ConstantFP: {
392 if (A64Imms::isFPImm(cast<ConstantFPSDNode>(Node)->getValueAPF())) {
393 // FMOV will take care of it from TableGen
397 SDNode *ResNode = SelectToFPLitPool(Node);
398 ReplaceUses(SDValue(Node, 0), SDValue(ResNode, 0));
402 break; // Let generic code handle it
405 SDNode *ResNode = SelectCode(Node);
407 DEBUG(dbgs() << "=> ";
408 if (ResNode == NULL || ResNode == Node)
411 ResNode->dump(CurDAG);
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);