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 "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"
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,
78 bool SelectInlineAsmMemoryOperand(const SDValue &Op,
80 std::vector<SDValue> &OutOps);
82 bool SelectLogicalImm(SDValue N, SDValue &Imm);
84 template<unsigned RegWidth>
85 bool SelectTSTBOperand(SDValue N, SDValue &FixedPos) {
86 return SelectTSTBOperand(N, FixedPos, RegWidth);
89 bool SelectTSTBOperand(SDValue N, SDValue &FixedPos, unsigned RegWidth);
91 SDNode *TrySelectToMoveImm(SDNode *N);
92 SDNode *SelectToLitPool(SDNode *N);
93 SDNode *SelectToFPLitPool(SDNode *N);
95 SDNode* Select(SDNode*);
101 AArch64DAGToDAGISel::SelectCVTFixedPosOperand(SDValue N, SDValue &FixedPos,
103 const ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N);
104 if (!CN) return false;
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
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
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);
120 // N.b. isPowerOf2 also checks for > 0.
121 if (!IsExact || !IntVal.isPowerOf2()) return false;
122 unsigned FBits = IntVal.logBase2();
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;
128 FixedPos = CurDAG->getTargetConstant(64 - FBits, MVT::i32);
133 AArch64DAGToDAGISel::SelectInlineAsmMemoryOperand(const SDValue &Op,
135 std::vector<SDValue> &OutOps) {
136 switch (ConstraintCode) {
137 default: llvm_unreachable("Unrecognised AArch64 memory constraint");
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
145 OutOps.push_back(Op);
152 AArch64DAGToDAGISel::SelectFPZeroOperand(SDValue N, SDValue &Dummy) {
153 ConstantFPSDNode *Imm = dyn_cast<ConstantFPSDNode>(N);
154 if (!Imm || !Imm->getValueAPF().isPosZero())
157 // Doesn't actually carry any information, but keeps TableGen quiet.
158 Dummy = CurDAG->getTargetConstant(0, MVT::i32);
162 bool AArch64DAGToDAGISel::SelectLogicalImm(SDValue N, SDValue &Imm) {
164 uint32_t RegWidth = N.getValueType().getSizeInBits();
166 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N);
167 if (!CN) return false;
169 if (!A64Imms::isLogicalImm(RegWidth, CN->getZExtValue(), Bits))
172 Imm = CurDAG->getTargetConstant(Bits, MVT::i32);
176 SDNode *AArch64DAGToDAGISel::TrySelectToMoveImm(SDNode *Node) {
178 DebugLoc dl = Node->getDebugLoc();
179 EVT DestType = Node->getValueType(0);
180 unsigned DestWidth = DestType.getSizeInBits();
185 uint32_t LogicalBits;
187 uint64_t BitPat = cast<ConstantSDNode>(Node)->getZExtValue();
188 if (A64Imms::isMOVZImm(DestWidth, BitPat, UImm16, Shift)) {
190 MOVOpcode = DestWidth == 64 ? AArch64::MOVZxii : AArch64::MOVZwii;
191 } else if (A64Imms::isMOVNImm(DestWidth, BitPat, UImm16, Shift)) {
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".
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;
203 return CurDAG->getMachineNode(MOVOpcode, dl, DestType,
204 CurDAG->getRegister(ZR, DestType),
205 CurDAG->getTargetConstant(LogicalBits, MVT::i32));
207 // Can't handle it in one instruction. There's scope for permitting two (or
208 // more) instructions, but that'll need more thought.
212 ResNode = CurDAG->getMachineNode(MOVOpcode, dl, MOVType,
213 CurDAG->getTargetConstant(UImm16, MVT::i32),
214 CurDAG->getTargetConstant(Shift, MVT::i32));
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),
221 CurDAG->getTargetConstant(AArch64::sub_32, MVT::i32));
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);
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.
239 assert((DestType == MVT::i64 || DestType == MVT::i32)
240 && "Only expect integer constants at the moment");
242 if (DestType == MVT::i32 || UnsignedVal <= UINT32_MAX) {
244 LoadInst = AArch64::LDRw_lit;
247 PoolEntry = CurDAG->getTargetConstantPool(
248 ConstantInt::get(Type::getInt32Ty(*CurDAG->getContext()), UnsignedVal),
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;
255 PoolEntry = CurDAG->getTargetConstantPool(
256 ConstantInt::getSigned(Type::getInt32Ty(*CurDAG->getContext()),
260 // Full 64-bit load needed: LDR x3, lbl
261 LoadInst = AArch64::LDRx_lit;
264 PoolEntry = CurDAG->getTargetConstantPool(
265 ConstantInt::get(Type::getInt64Ty(*CurDAG->getContext()), UnsignedVal),
269 SDNode *ResNode = CurDAG->getMachineNode(LoadInst, dl,
270 LoadType, MVT::Other,
271 PoolEntry, CurDAG->getEntryNode());
273 if (DestType != LoadType) {
274 // We used the implicit zero-extension of "LDR w3, lbl", tell LLVM this
276 assert(DestType == MVT::i64 && LoadType == MVT::i32
277 && "Unexpected load combination");
279 ResNode = CurDAG->getMachineNode(TargetOpcode::SUBREG_TO_REG, dl,
280 MVT::i64, MVT::i32, MVT::Other,
281 CurDAG->getTargetConstant(0, MVT::i64),
283 CurDAG->getTargetConstant(AArch64::sub_32, MVT::i32));
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);
295 switch (DestType.getSizeInBits()) {
297 LoadInst = AArch64::LDRs_lit;
300 LoadInst = AArch64::LDRd_lit;
303 LoadInst = AArch64::LDRq_lit;
305 default: llvm_unreachable("cannot select floating-point litpool");
308 SDValue PoolEntry = CurDAG->getTargetConstantPool(FV, DestType);
309 SDNode *ResNode = CurDAG->getMachineNode(LoadInst, dl,
310 DestType, MVT::Other,
311 PoolEntry, CurDAG->getEntryNode());
317 AArch64DAGToDAGISel::SelectTSTBOperand(SDValue N, SDValue &FixedPos,
319 const ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N);
320 if (!CN) return false;
322 uint64_t Val = CN->getZExtValue();
324 if (!isPowerOf2_64(Val)) return false;
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;
331 FixedPos = CurDAG->getTargetConstant(TestedBit, MVT::i64);
335 SDNode *AArch64DAGToDAGISel::Select(SDNode *Node) {
336 // Dump information about the Node being selected
337 DEBUG(dbgs() << "Selecting: "; Node->dump(CurDAG); dbgs() << "\n");
339 if (Node->isMachineOpcode()) {
340 DEBUG(dbgs() << "== "; Node->dump(CurDAG); dbgs() << "\n");
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));
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));
358 ReplaceUses(SDValue(Node, 0), CP);
361 case ISD::Constant: {
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.
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(),
372 Register, Ty).getNode();
375 // Next best option is a move-immediate, see if we can do that.
377 ResNode = TrySelectToMoveImm(Node);
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.
384 ResNode = SelectToLitPool(Node);
387 assert(ResNode && "We need *some* way to materialise a constant");
389 ReplaceUses(SDValue(Node, 0), SDValue(ResNode, 0));
392 case ISD::ConstantFP: {
393 if (A64Imms::isFPImm(cast<ConstantFPSDNode>(Node)->getValueAPF())) {
394 // FMOV will take care of it from TableGen
398 SDNode *ResNode = SelectToFPLitPool(Node);
399 ReplaceUses(SDValue(Node, 0), SDValue(ResNode, 0));
403 break; // Let generic code handle it
406 SDNode *ResNode = SelectCode(Node);
408 DEBUG(dbgs() << "=> ";
409 if (ResNode == NULL || ResNode == Node)
412 ResNode->dump(CurDAG);
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);