1 //===-- HexagonISelDAGToDAG.cpp - A dag to dag inst selector for Hexagon --===//
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 Hexagon target.
12 //===----------------------------------------------------------------------===//
15 #include "HexagonISelLowering.h"
16 #include "HexagonTargetMachine.h"
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/CodeGen/SelectionDAGISel.h"
19 #include "llvm/IR/Intrinsics.h"
20 #include "llvm/Support/CommandLine.h"
21 #include "llvm/Support/Compiler.h"
22 #include "llvm/Support/Debug.h"
25 #define DEBUG_TYPE "hexagon-isel"
29 MaxNumOfUsesForConstExtenders("ga-max-num-uses-for-constant-extenders",
30 cl::Hidden, cl::init(2),
31 cl::desc("Maximum number of uses of a global address such that we still us a"
32 "constant extended instruction"));
34 //===----------------------------------------------------------------------===//
35 // Instruction Selector Implementation
36 //===----------------------------------------------------------------------===//
39 void initializeHexagonDAGToDAGISelPass(PassRegistry&);
42 //===--------------------------------------------------------------------===//
43 /// HexagonDAGToDAGISel - Hexagon specific code to select Hexagon machine
44 /// instructions for SelectionDAG operations.
47 class HexagonDAGToDAGISel : public SelectionDAGISel {
48 /// Subtarget - Keep a pointer to the Hexagon Subtarget around so that we can
49 /// make the right decision when generating code for different targets.
50 const HexagonSubtarget *Subtarget;
52 // Keep a reference to HexagonTargetMachine.
53 const HexagonTargetMachine& TM;
54 DenseMap<const GlobalValue *, unsigned> GlobalAddressUseCountMap;
56 explicit HexagonDAGToDAGISel(HexagonTargetMachine &targetmachine,
57 CodeGenOpt::Level OptLevel)
58 : SelectionDAGISel(targetmachine, OptLevel), TM(targetmachine) {
59 initializeHexagonDAGToDAGISelPass(*PassRegistry::getPassRegistry());
61 bool hasNumUsesBelowThresGA(SDNode *N) const;
63 SDNode *Select(SDNode *N) override;
65 // Complex Pattern Selectors.
66 inline bool foldGlobalAddress(SDValue &N, SDValue &R);
67 inline bool foldGlobalAddressGP(SDValue &N, SDValue &R);
68 bool foldGlobalAddressImpl(SDValue &N, SDValue &R, bool ShouldLookForGP);
69 bool SelectADDRri(SDValue& N, SDValue &R1, SDValue &R2);
70 bool SelectADDRriS11_0(SDValue& N, SDValue &R1, SDValue &R2);
71 bool SelectADDRriS11_1(SDValue& N, SDValue &R1, SDValue &R2);
72 bool SelectADDRriS11_2(SDValue& N, SDValue &R1, SDValue &R2);
73 bool SelectMEMriS11_2(SDValue& Addr, SDValue &Base, SDValue &Offset);
74 bool SelectADDRriS11_3(SDValue& N, SDValue &R1, SDValue &R2);
75 bool SelectADDRrr(SDValue &Addr, SDValue &Base, SDValue &Offset);
76 bool SelectADDRriU6_0(SDValue& N, SDValue &R1, SDValue &R2);
77 bool SelectADDRriU6_1(SDValue& N, SDValue &R1, SDValue &R2);
78 bool SelectADDRriU6_2(SDValue& N, SDValue &R1, SDValue &R2);
80 // Complex Pattern Selectors.
81 inline bool SelectAddrGA(SDValue &N, SDValue &R);
82 inline bool SelectAddrGP(SDValue &N, SDValue &R);
83 bool SelectGlobalAddress(SDValue &N, SDValue &R, bool UseGP);
84 bool SelectAddrFI(SDValue &N, SDValue &R);
86 const char *getPassName() const override {
87 return "Hexagon DAG->DAG Pattern Instruction Selection";
90 bool runOnMachineFunction(MachineFunction &MF) override {
91 Subtarget = &MF.getSubtarget<HexagonSubtarget>();
92 return SelectionDAGISel::runOnMachineFunction(MF);
95 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
96 /// inline asm expressions.
97 bool SelectInlineAsmMemoryOperand(const SDValue &Op,
99 std::vector<SDValue> &OutOps) override;
100 bool SelectAddr(SDNode *Op, SDValue Addr, SDValue &Base, SDValue &Offset);
102 SDNode *SelectLoad(SDNode *N);
103 SDNode *SelectBaseOffsetLoad(LoadSDNode *LD, SDLoc dl);
104 SDNode *SelectIndexedLoad(LoadSDNode *LD, SDLoc dl);
105 SDNode *SelectIndexedLoadZeroExtend64(LoadSDNode *LD, unsigned Opcode,
107 SDNode *SelectIndexedLoadSignExtend64(LoadSDNode *LD, unsigned Opcode,
109 SDNode *SelectBaseOffsetStore(StoreSDNode *ST, SDLoc dl);
110 SDNode *SelectIndexedStore(StoreSDNode *ST, SDLoc dl);
111 SDNode *SelectStore(SDNode *N);
112 SDNode *SelectSHL(SDNode *N);
113 SDNode *SelectSelect(SDNode *N);
114 SDNode *SelectTruncate(SDNode *N);
115 SDNode *SelectMul(SDNode *N);
116 SDNode *SelectZeroExtend(SDNode *N);
117 SDNode *SelectIntrinsicWOChain(SDNode *N);
118 SDNode *SelectIntrinsicWChain(SDNode *N);
119 SDNode *SelectConstant(SDNode *N);
120 SDNode *SelectConstantFP(SDNode *N);
121 SDNode *SelectAdd(SDNode *N);
122 bool isConstExtProfitable(SDNode *N) const;
124 // XformMskToBitPosU5Imm - Returns the bit position which
125 // the single bit 32 bit mask represents.
126 // Used in Clr and Set bit immediate memops.
127 SDValue XformMskToBitPosU5Imm(uint32_t Imm) {
129 bitPos = Log2_32(Imm);
130 assert(bitPos >= 0 && bitPos < 32 &&
131 "Constant out of range for 32 BitPos Memops");
132 return CurDAG->getTargetConstant(bitPos, MVT::i32);
135 // XformMskToBitPosU4Imm - Returns the bit position which the single bit 16 bit
136 // mask represents. Used in Clr and Set bit immediate memops.
137 SDValue XformMskToBitPosU4Imm(uint16_t Imm) {
138 return XformMskToBitPosU5Imm(Imm);
141 // XformMskToBitPosU3Imm - Returns the bit position which the single bit 8 bit
142 // mask represents. Used in Clr and Set bit immediate memops.
143 SDValue XformMskToBitPosU3Imm(uint8_t Imm) {
144 return XformMskToBitPosU5Imm(Imm);
147 // Return true if there is exactly one bit set in V, i.e., if V is one of the
148 // following integers: 2^0, 2^1, ..., 2^31.
149 bool ImmIsSingleBit(uint32_t v) const {
150 uint32_t c = CountPopulation_64(v);
151 // Only return true if we counted 1 bit.
155 // XformM5ToU5Imm - Return a target constant with the specified value, of type
156 // i32 where the negative literal is transformed into a positive literal for
158 inline SDValue XformM5ToU5Imm(signed Imm) {
159 assert( (Imm >= -31 && Imm <= -1) && "Constant out of range for Memops");
160 return CurDAG->getTargetConstant( - Imm, MVT::i32);
164 // XformU7ToU7M1Imm - Return a target constant decremented by 1, in range
165 // [1..128], used in cmpb.gtu instructions.
166 inline SDValue XformU7ToU7M1Imm(signed Imm) {
167 assert((Imm >= 1 && Imm <= 128) && "Constant out of range for cmpb op");
168 return CurDAG->getTargetConstant(Imm - 1, MVT::i8);
171 // XformS8ToS8M1Imm - Return a target constant decremented by 1.
172 inline SDValue XformSToSM1Imm(signed Imm) {
173 return CurDAG->getTargetConstant(Imm - 1, MVT::i32);
176 // XformU8ToU8M1Imm - Return a target constant decremented by 1.
177 inline SDValue XformUToUM1Imm(unsigned Imm) {
178 assert((Imm >= 1) && "Cannot decrement unsigned int less than 1");
179 return CurDAG->getTargetConstant(Imm - 1, MVT::i32);
182 // XformSToSM2Imm - Return a target constant decremented by 2.
183 inline SDValue XformSToSM2Imm(unsigned Imm) {
184 return CurDAG->getTargetConstant(Imm - 2, MVT::i32);
187 // XformSToSM3Imm - Return a target constant decremented by 3.
188 inline SDValue XformSToSM3Imm(unsigned Imm) {
189 return CurDAG->getTargetConstant(Imm - 3, MVT::i32);
192 // Include the pieces autogenerated from the target description.
193 #include "HexagonGenDAGISel.inc"
196 bool isValueExtension(SDValue const &Val, unsigned FromBits, SDValue &Src);
198 } // end anonymous namespace
201 /// createHexagonISelDag - This pass converts a legalized DAG into a
202 /// Hexagon-specific DAG, ready for instruction scheduling.
204 FunctionPass *llvm::createHexagonISelDag(HexagonTargetMachine &TM,
205 CodeGenOpt::Level OptLevel) {
206 return new HexagonDAGToDAGISel(TM, OptLevel);
209 static void initializePassOnce(PassRegistry &Registry) {
210 const char *Name = "Hexagon DAG->DAG Pattern Instruction Selection";
211 PassInfo *PI = new PassInfo(Name, "hexagon-isel",
212 &SelectionDAGISel::ID, nullptr, false, false);
213 Registry.registerPass(*PI, true);
216 void llvm::initializeHexagonDAGToDAGISelPass(PassRegistry &Registry) {
217 CALL_ONCE_INITIALIZATION(initializePassOnce)
221 static bool IsS11_0_Offset(SDNode * S) {
222 ConstantSDNode *N = cast<ConstantSDNode>(S);
224 // immS16 predicate - True if the immediate fits in a 16-bit sign extended
226 int64_t v = (int64_t)N->getSExtValue();
231 static bool IsS11_1_Offset(SDNode * S) {
232 ConstantSDNode *N = cast<ConstantSDNode>(S);
234 // immS16 predicate - True if the immediate fits in a 16-bit sign extended
236 int64_t v = (int64_t)N->getSExtValue();
237 return isShiftedInt<11,1>(v);
241 static bool IsS11_2_Offset(SDNode * S) {
242 ConstantSDNode *N = cast<ConstantSDNode>(S);
244 // immS16 predicate - True if the immediate fits in a 16-bit sign extended
246 int64_t v = (int64_t)N->getSExtValue();
247 return isShiftedInt<11,2>(v);
251 static bool IsS11_3_Offset(SDNode * S) {
252 ConstantSDNode *N = cast<ConstantSDNode>(S);
254 // immS16 predicate - True if the immediate fits in a 16-bit sign extended
256 int64_t v = (int64_t)N->getSExtValue();
257 return isShiftedInt<11,3>(v);
261 static bool IsU6_0_Offset(SDNode * S) {
262 ConstantSDNode *N = cast<ConstantSDNode>(S);
264 // u6 predicate - True if the immediate fits in a 6-bit unsigned extended
266 int64_t v = (int64_t)N->getSExtValue();
271 static bool IsU6_1_Offset(SDNode * S) {
272 ConstantSDNode *N = cast<ConstantSDNode>(S);
274 // u6 predicate - True if the immediate fits in a 6-bit unsigned extended
276 int64_t v = (int64_t)N->getSExtValue();
277 return isShiftedUInt<6,1>(v);
281 static bool IsU6_2_Offset(SDNode * S) {
282 ConstantSDNode *N = cast<ConstantSDNode>(S);
284 // u6 predicate - True if the immediate fits in a 6-bit unsigned extended
286 int64_t v = (int64_t)N->getSExtValue();
287 return isShiftedUInt<6,2>(v);
291 // Intrinsics that return a a predicate.
292 static unsigned doesIntrinsicReturnPredicate(unsigned ID)
297 case Intrinsic::hexagon_C2_cmpeq:
298 case Intrinsic::hexagon_C2_cmpgt:
299 case Intrinsic::hexagon_C2_cmpgtu:
300 case Intrinsic::hexagon_C2_cmpgtup:
301 case Intrinsic::hexagon_C2_cmpgtp:
302 case Intrinsic::hexagon_C2_cmpeqp:
303 case Intrinsic::hexagon_C2_bitsset:
304 case Intrinsic::hexagon_C2_bitsclr:
305 case Intrinsic::hexagon_C2_cmpeqi:
306 case Intrinsic::hexagon_C2_cmpgti:
307 case Intrinsic::hexagon_C2_cmpgtui:
308 case Intrinsic::hexagon_C2_cmpgei:
309 case Intrinsic::hexagon_C2_cmpgeui:
310 case Intrinsic::hexagon_C2_cmplt:
311 case Intrinsic::hexagon_C2_cmpltu:
312 case Intrinsic::hexagon_C2_bitsclri:
313 case Intrinsic::hexagon_C2_and:
314 case Intrinsic::hexagon_C2_or:
315 case Intrinsic::hexagon_C2_xor:
316 case Intrinsic::hexagon_C2_andn:
317 case Intrinsic::hexagon_C2_not:
318 case Intrinsic::hexagon_C2_orn:
319 case Intrinsic::hexagon_C2_pxfer_map:
320 case Intrinsic::hexagon_C2_any8:
321 case Intrinsic::hexagon_C2_all8:
322 case Intrinsic::hexagon_A2_vcmpbeq:
323 case Intrinsic::hexagon_A2_vcmpbgtu:
324 case Intrinsic::hexagon_A2_vcmpheq:
325 case Intrinsic::hexagon_A2_vcmphgt:
326 case Intrinsic::hexagon_A2_vcmphgtu:
327 case Intrinsic::hexagon_A2_vcmpweq:
328 case Intrinsic::hexagon_A2_vcmpwgt:
329 case Intrinsic::hexagon_A2_vcmpwgtu:
330 case Intrinsic::hexagon_C2_tfrrp:
331 case Intrinsic::hexagon_S2_tstbit_i:
332 case Intrinsic::hexagon_S2_tstbit_r:
337 static bool OffsetFitsS11(EVT MemType, int64_t Offset) {
338 if (MemType == MVT::i64 && isShiftedInt<11,3>(Offset)) {
341 if (MemType == MVT::i32 && isShiftedInt<11,2>(Offset)) {
344 if (MemType == MVT::i16 && isShiftedInt<11,1>(Offset)) {
347 if (MemType == MVT::i8 && isInt<11>(Offset)) {
355 // Try to lower loads of GlobalAdresses into base+offset loads. Custom
356 // lowering for GlobalAddress nodes has already turned it into a
359 SDNode *HexagonDAGToDAGISel::SelectBaseOffsetLoad(LoadSDNode *LD, SDLoc dl) {
360 SDValue Chain = LD->getChain();
361 SDNode* Const32 = LD->getBasePtr().getNode();
364 if (Const32->getOpcode() == HexagonISD::CONST32 &&
365 ISD::isNormalLoad(LD)) {
366 SDValue Base = Const32->getOperand(0);
367 EVT LoadedVT = LD->getMemoryVT();
368 int64_t Offset = cast<GlobalAddressSDNode>(Base)->getOffset();
369 if (Offset != 0 && OffsetFitsS11(LoadedVT, Offset)) {
370 MVT PointerTy = getTargetLowering()->getPointerTy();
371 const GlobalValue* GV =
372 cast<GlobalAddressSDNode>(Base)->getGlobal();
374 CurDAG->getTargetGlobalAddress(GV, dl, PointerTy, 0);
375 SDNode* NewBase = CurDAG->getMachineNode(Hexagon::CONST32_set,
378 // Figure out base + offset opcode
379 if (LoadedVT == MVT::i64) Opcode = Hexagon::L2_loadrd_io;
380 else if (LoadedVT == MVT::i32) Opcode = Hexagon::L2_loadri_io;
381 else if (LoadedVT == MVT::i16) Opcode = Hexagon::L2_loadrh_io;
382 else if (LoadedVT == MVT::i8) Opcode = Hexagon::L2_loadrb_io;
383 else llvm_unreachable("unknown memory type");
385 // Build indexed load.
386 SDValue TargetConstOff = CurDAG->getTargetConstant(Offset, PointerTy);
387 SDNode* Result = CurDAG->getMachineNode(Opcode, dl,
393 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
394 MemOp[0] = LD->getMemOperand();
395 cast<MachineSDNode>(Result)->setMemRefs(MemOp, MemOp + 1);
396 ReplaceUses(LD, Result);
401 return SelectCode(LD);
405 SDNode *HexagonDAGToDAGISel::SelectIndexedLoadSignExtend64(LoadSDNode *LD,
409 SDValue Chain = LD->getChain();
410 EVT LoadedVT = LD->getMemoryVT();
411 SDValue Base = LD->getBasePtr();
412 SDValue Offset = LD->getOffset();
413 SDNode *OffsetNode = Offset.getNode();
414 int32_t Val = cast<ConstantSDNode>(OffsetNode)->getSExtValue();
415 SDValue N1 = LD->getOperand(1);
419 if (SelectADDRriS11_2(N1, CPTmpN1_0, CPTmpN1_1) &&
420 N1.getNode()->getValueType(0) == MVT::i32) {
421 const HexagonInstrInfo *TII = Subtarget->getInstrInfo();
422 if (TII->isValidAutoIncImm(LoadedVT, Val)) {
423 SDValue TargetConst = CurDAG->getTargetConstant(Val, MVT::i32);
424 SDNode *Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::i32, MVT::i32,
425 MVT::Other, Base, TargetConst,
427 SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A2_sxtw, dl, MVT::i64,
428 SDValue(Result_1, 0));
429 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
430 MemOp[0] = LD->getMemOperand();
431 cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
432 const SDValue Froms[] = { SDValue(LD, 0),
436 const SDValue Tos[] = { SDValue(Result_2, 0),
437 SDValue(Result_1, 1),
440 ReplaceUses(Froms, Tos, 3);
443 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
444 SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
445 SDNode *Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::i32,
446 MVT::Other, Base, TargetConst0,
448 SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A2_sxtw, dl,
449 MVT::i64, SDValue(Result_1, 0));
450 SDNode* Result_3 = CurDAG->getMachineNode(Hexagon::A2_addi, dl,
451 MVT::i32, Base, TargetConstVal,
452 SDValue(Result_1, 1));
453 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
454 MemOp[0] = LD->getMemOperand();
455 cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
456 const SDValue Froms[] = { SDValue(LD, 0),
460 const SDValue Tos[] = { SDValue(Result_2, 0),
461 SDValue(Result_3, 0),
464 ReplaceUses(Froms, Tos, 3);
467 return SelectCode(LD);
471 SDNode *HexagonDAGToDAGISel::SelectIndexedLoadZeroExtend64(LoadSDNode *LD,
475 SDValue Chain = LD->getChain();
476 EVT LoadedVT = LD->getMemoryVT();
477 SDValue Base = LD->getBasePtr();
478 SDValue Offset = LD->getOffset();
479 SDNode *OffsetNode = Offset.getNode();
480 int32_t Val = cast<ConstantSDNode>(OffsetNode)->getSExtValue();
481 SDValue N1 = LD->getOperand(1);
485 if (SelectADDRriS11_2(N1, CPTmpN1_0, CPTmpN1_1) &&
486 N1.getNode()->getValueType(0) == MVT::i32) {
487 const HexagonInstrInfo *TII = Subtarget->getInstrInfo();
488 if (TII->isValidAutoIncImm(LoadedVT, Val)) {
489 SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
490 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
491 SDNode *Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::i32,
492 MVT::i32, MVT::Other, Base,
493 TargetConstVal, Chain);
494 SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A2_tfrsi, dl, MVT::i32,
496 SDNode *Result_3 = CurDAG->getMachineNode(Hexagon::A2_combinew, dl,
497 MVT::i64, MVT::Other,
499 SDValue(Result_1,0));
500 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
501 MemOp[0] = LD->getMemOperand();
502 cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
503 const SDValue Froms[] = { SDValue(LD, 0),
507 const SDValue Tos[] = { SDValue(Result_3, 0),
508 SDValue(Result_1, 1),
511 ReplaceUses(Froms, Tos, 3);
515 // Generate an indirect load.
516 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
517 SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
518 SDNode *Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::i32,
520 Base, TargetConst0, Chain);
521 SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A2_tfrsi, dl, MVT::i32,
523 SDNode *Result_3 = CurDAG->getMachineNode(Hexagon::A2_combinew, dl,
524 MVT::i64, MVT::Other,
526 SDValue(Result_1,0));
527 // Add offset to base.
528 SDNode* Result_4 = CurDAG->getMachineNode(Hexagon::A2_addi, dl, MVT::i32,
529 Base, TargetConstVal,
530 SDValue(Result_1, 1));
531 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
532 MemOp[0] = LD->getMemOperand();
533 cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
534 const SDValue Froms[] = { SDValue(LD, 0),
538 const SDValue Tos[] = { SDValue(Result_3, 0), // Load value.
539 SDValue(Result_4, 0), // New address.
542 ReplaceUses(Froms, Tos, 3);
546 return SelectCode(LD);
550 SDNode *HexagonDAGToDAGISel::SelectIndexedLoad(LoadSDNode *LD, SDLoc dl) {
551 SDValue Chain = LD->getChain();
552 SDValue Base = LD->getBasePtr();
553 SDValue Offset = LD->getOffset();
554 SDNode *OffsetNode = Offset.getNode();
555 // Get the constant value.
556 int32_t Val = cast<ConstantSDNode>(OffsetNode)->getSExtValue();
557 EVT LoadedVT = LD->getMemoryVT();
560 // Check for zero ext loads.
561 bool zextval = (LD->getExtensionType() == ISD::ZEXTLOAD);
563 // Figure out the opcode.
564 const HexagonInstrInfo *TII = Subtarget->getInstrInfo();
565 if (LoadedVT == MVT::i64) {
566 if (TII->isValidAutoIncImm(LoadedVT, Val))
567 Opcode = Hexagon::L2_loadrd_pi;
569 Opcode = Hexagon::L2_loadrd_io;
570 } else if (LoadedVT == MVT::i32) {
571 if (TII->isValidAutoIncImm(LoadedVT, Val))
572 Opcode = Hexagon::L2_loadri_pi;
574 Opcode = Hexagon::L2_loadri_io;
575 } else if (LoadedVT == MVT::i16) {
576 if (TII->isValidAutoIncImm(LoadedVT, Val))
577 Opcode = zextval ? Hexagon::L2_loadruh_pi : Hexagon::L2_loadrh_pi;
579 Opcode = zextval ? Hexagon::L2_loadruh_io : Hexagon::L2_loadrh_io;
580 } else if (LoadedVT == MVT::i8) {
581 if (TII->isValidAutoIncImm(LoadedVT, Val))
582 Opcode = zextval ? Hexagon::L2_loadrub_pi : Hexagon::L2_loadrb_pi;
584 Opcode = zextval ? Hexagon::L2_loadrub_io : Hexagon::L2_loadrb_io;
586 llvm_unreachable("unknown memory type");
588 // For zero ext i64 loads, we need to add combine instructions.
589 if (LD->getValueType(0) == MVT::i64 &&
590 LD->getExtensionType() == ISD::ZEXTLOAD) {
591 return SelectIndexedLoadZeroExtend64(LD, Opcode, dl);
593 if (LD->getValueType(0) == MVT::i64 &&
594 LD->getExtensionType() == ISD::SEXTLOAD) {
595 // Handle sign ext i64 loads.
596 return SelectIndexedLoadSignExtend64(LD, Opcode, dl);
598 if (TII->isValidAutoIncImm(LoadedVT, Val)) {
599 SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
600 SDNode* Result = CurDAG->getMachineNode(Opcode, dl,
602 MVT::i32, MVT::Other, Base,
603 TargetConstVal, Chain);
604 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
605 MemOp[0] = LD->getMemOperand();
606 cast<MachineSDNode>(Result)->setMemRefs(MemOp, MemOp + 1);
607 const SDValue Froms[] = { SDValue(LD, 0),
611 const SDValue Tos[] = { SDValue(Result, 0),
615 ReplaceUses(Froms, Tos, 3);
618 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
619 SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
620 SDNode* Result_1 = CurDAG->getMachineNode(Opcode, dl,
622 MVT::Other, Base, TargetConst0,
624 SDNode* Result_2 = CurDAG->getMachineNode(Hexagon::A2_addi, dl, MVT::i32,
625 Base, TargetConstVal,
626 SDValue(Result_1, 1));
627 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
628 MemOp[0] = LD->getMemOperand();
629 cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
630 const SDValue Froms[] = { SDValue(LD, 0),
634 const SDValue Tos[] = { SDValue(Result_1, 0),
635 SDValue(Result_2, 0),
638 ReplaceUses(Froms, Tos, 3);
644 SDNode *HexagonDAGToDAGISel::SelectLoad(SDNode *N) {
647 LoadSDNode *LD = cast<LoadSDNode>(N);
648 ISD::MemIndexedMode AM = LD->getAddressingMode();
650 // Handle indexed loads.
651 if (AM != ISD::UNINDEXED) {
652 result = SelectIndexedLoad(LD, dl);
654 result = SelectBaseOffsetLoad(LD, dl);
661 SDNode *HexagonDAGToDAGISel::SelectIndexedStore(StoreSDNode *ST, SDLoc dl) {
662 SDValue Chain = ST->getChain();
663 SDValue Base = ST->getBasePtr();
664 SDValue Offset = ST->getOffset();
665 SDValue Value = ST->getValue();
666 SDNode *OffsetNode = Offset.getNode();
667 // Get the constant value.
668 int32_t Val = cast<ConstantSDNode>(OffsetNode)->getSExtValue();
669 EVT StoredVT = ST->getMemoryVT();
671 // Offset value must be within representable range
672 // and must have correct alignment properties.
673 const HexagonInstrInfo *TII = Subtarget->getInstrInfo();
674 if (TII->isValidAutoIncImm(StoredVT, Val)) {
675 SDValue Ops[] = {Base, CurDAG->getTargetConstant(Val, MVT::i32), Value,
679 // Figure out the post inc version of opcode.
680 if (StoredVT == MVT::i64) Opcode = Hexagon::S2_storerd_pi;
681 else if (StoredVT == MVT::i32) Opcode = Hexagon::S2_storeri_pi;
682 else if (StoredVT == MVT::i16) Opcode = Hexagon::S2_storerh_pi;
683 else if (StoredVT == MVT::i8) Opcode = Hexagon::S2_storerb_pi;
684 else llvm_unreachable("unknown memory type");
686 // Build post increment store.
687 SDNode* Result = CurDAG->getMachineNode(Opcode, dl, MVT::i32,
689 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
690 MemOp[0] = ST->getMemOperand();
691 cast<MachineSDNode>(Result)->setMemRefs(MemOp, MemOp + 1);
693 ReplaceUses(ST, Result);
694 ReplaceUses(SDValue(ST,1), SDValue(Result,1));
698 // Note: Order of operands matches the def of instruction:
699 // def STrid : STInst<(outs), (ins MEMri:$addr, DoubleRegs:$src1), ...
700 // and it differs for POST_ST* for instance.
701 SDValue Ops[] = { Base, CurDAG->getTargetConstant(0, MVT::i32), Value,
705 // Figure out the opcode.
706 if (StoredVT == MVT::i64) Opcode = Hexagon::S2_storerd_io;
707 else if (StoredVT == MVT::i32) Opcode = Hexagon::S2_storeri_io;
708 else if (StoredVT == MVT::i16) Opcode = Hexagon::S2_storerh_io;
709 else if (StoredVT == MVT::i8) Opcode = Hexagon::S2_storerb_io;
710 else llvm_unreachable("unknown memory type");
712 // Build regular store.
713 SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
714 SDNode* Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::Other, Ops);
715 // Build splitted incriment instruction.
716 SDNode* Result_2 = CurDAG->getMachineNode(Hexagon::A2_addi, dl, MVT::i32,
719 SDValue(Result_1, 0));
720 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
721 MemOp[0] = ST->getMemOperand();
722 cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
724 ReplaceUses(SDValue(ST,0), SDValue(Result_2,0));
725 ReplaceUses(SDValue(ST,1), SDValue(Result_1,0));
730 SDNode *HexagonDAGToDAGISel::SelectBaseOffsetStore(StoreSDNode *ST,
732 SDValue Chain = ST->getChain();
733 SDNode* Const32 = ST->getBasePtr().getNode();
734 SDValue Value = ST->getValue();
737 // Try to lower stores of GlobalAdresses into indexed stores. Custom
738 // lowering for GlobalAddress nodes has already turned it into a
739 // CONST32. Avoid truncating stores for the moment. Post-inc stores
740 // do the same. Don't think there's a reason for it, so will file a
742 if ((Const32->getOpcode() == HexagonISD::CONST32) &&
743 !(Value.getValueType() == MVT::i64 && ST->isTruncatingStore())) {
744 SDValue Base = Const32->getOperand(0);
745 if (Base.getOpcode() == ISD::TargetGlobalAddress) {
746 EVT StoredVT = ST->getMemoryVT();
747 int64_t Offset = cast<GlobalAddressSDNode>(Base)->getOffset();
748 if (Offset != 0 && OffsetFitsS11(StoredVT, Offset)) {
749 MVT PointerTy = getTargetLowering()->getPointerTy();
750 const GlobalValue* GV =
751 cast<GlobalAddressSDNode>(Base)->getGlobal();
753 CurDAG->getTargetGlobalAddress(GV, dl, PointerTy, 0);
754 SDNode* NewBase = CurDAG->getMachineNode(Hexagon::CONST32_set,
758 // Figure out base + offset opcode
759 if (StoredVT == MVT::i64) Opcode = Hexagon::S2_storerd_io;
760 else if (StoredVT == MVT::i32) Opcode = Hexagon::S2_storeri_io;
761 else if (StoredVT == MVT::i16) Opcode = Hexagon::S2_storerh_io;
762 else if (StoredVT == MVT::i8) Opcode = Hexagon::S2_storerb_io;
763 else llvm_unreachable("unknown memory type");
765 SDValue Ops[] = {SDValue(NewBase,0),
766 CurDAG->getTargetConstant(Offset,PointerTy),
768 // build indexed store
769 SDNode* Result = CurDAG->getMachineNode(Opcode, dl,
771 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
772 MemOp[0] = ST->getMemOperand();
773 cast<MachineSDNode>(Result)->setMemRefs(MemOp, MemOp + 1);
774 ReplaceUses(ST, Result);
780 return SelectCode(ST);
784 SDNode *HexagonDAGToDAGISel::SelectStore(SDNode *N) {
786 StoreSDNode *ST = cast<StoreSDNode>(N);
787 ISD::MemIndexedMode AM = ST->getAddressingMode();
789 // Handle indexed stores.
790 if (AM != ISD::UNINDEXED) {
791 return SelectIndexedStore(ST, dl);
794 return SelectBaseOffsetStore(ST, dl);
797 SDNode *HexagonDAGToDAGISel::SelectMul(SDNode *N) {
801 // %conv.i = sext i32 %tmp1 to i64
802 // %conv2.i = sext i32 %add to i64
803 // %mul.i = mul nsw i64 %conv2.i, %conv.i
805 // --- match with the following ---
807 // %mul.i = mpy (%tmp1, %add)
810 if (N->getValueType(0) == MVT::i64) {
811 // Shifting a i64 signed multiply.
812 SDValue MulOp0 = N->getOperand(0);
813 SDValue MulOp1 = N->getOperand(1);
818 // Handle sign_extend and sextload.
819 if (MulOp0.getOpcode() == ISD::SIGN_EXTEND) {
820 SDValue Sext0 = MulOp0.getOperand(0);
821 if (Sext0.getNode()->getValueType(0) != MVT::i32) {
822 return SelectCode(N);
826 } else if (MulOp0.getOpcode() == ISD::LOAD) {
827 LoadSDNode *LD = cast<LoadSDNode>(MulOp0.getNode());
828 if (LD->getMemoryVT() != MVT::i32 ||
829 LD->getExtensionType() != ISD::SEXTLOAD ||
830 LD->getAddressingMode() != ISD::UNINDEXED) {
831 return SelectCode(N);
834 SDValue Chain = LD->getChain();
835 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
836 OP0 = SDValue(CurDAG->getMachineNode(Hexagon::L2_loadri_io, dl, MVT::i32,
838 LD->getBasePtr(), TargetConst0,
841 return SelectCode(N);
844 // Same goes for the second operand.
845 if (MulOp1.getOpcode() == ISD::SIGN_EXTEND) {
846 SDValue Sext1 = MulOp1.getOperand(0);
847 if (Sext1.getNode()->getValueType(0) != MVT::i32) {
848 return SelectCode(N);
852 } else if (MulOp1.getOpcode() == ISD::LOAD) {
853 LoadSDNode *LD = cast<LoadSDNode>(MulOp1.getNode());
854 if (LD->getMemoryVT() != MVT::i32 ||
855 LD->getExtensionType() != ISD::SEXTLOAD ||
856 LD->getAddressingMode() != ISD::UNINDEXED) {
857 return SelectCode(N);
860 SDValue Chain = LD->getChain();
861 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
862 OP1 = SDValue(CurDAG->getMachineNode(Hexagon::L2_loadri_io, dl, MVT::i32,
864 LD->getBasePtr(), TargetConst0,
867 return SelectCode(N);
870 // Generate a mpy instruction.
871 SDNode *Result = CurDAG->getMachineNode(Hexagon::M2_dpmpyss_s0, dl, MVT::i64,
873 ReplaceUses(N, Result);
877 return SelectCode(N);
881 SDNode *HexagonDAGToDAGISel::SelectSelect(SDNode *N) {
883 SDValue N0 = N->getOperand(0);
884 if (N0.getOpcode() == ISD::SETCC) {
885 SDValue N00 = N0.getOperand(0);
886 if (N00.getOpcode() == ISD::SIGN_EXTEND_INREG) {
887 SDValue N000 = N00.getOperand(0);
888 SDValue N001 = N00.getOperand(1);
889 if (cast<VTSDNode>(N001)->getVT() == MVT::i16) {
890 SDValue N01 = N0.getOperand(1);
891 SDValue N02 = N0.getOperand(2);
893 // Pattern: (select:i32 (setcc:i1 (sext_inreg:i32 IntRegs:i32:$src2,
894 // i16:Other),IntRegs:i32:$src1, SETLT:Other),IntRegs:i32:$src1,
895 // IntRegs:i32:$src2)
896 // Emits: (MAXh_rr:i32 IntRegs:i32:$src1, IntRegs:i32:$src2)
897 // Pattern complexity = 9 cost = 1 size = 0.
898 if (cast<CondCodeSDNode>(N02)->get() == ISD::SETLT) {
899 SDValue N1 = N->getOperand(1);
901 SDValue N2 = N->getOperand(2);
903 N0.getNode()->getValueType(N0.getResNo()) == MVT::i1 &&
904 N00.getNode()->getValueType(N00.getResNo()) == MVT::i32) {
905 SDNode *SextNode = CurDAG->getMachineNode(Hexagon::A2_sxth, dl,
907 SDNode *Result = CurDAG->getMachineNode(Hexagon::A2_max, dl,
909 SDValue(SextNode, 0),
911 ReplaceUses(N, Result);
917 // Pattern: (select:i32 (setcc:i1 (sext_inreg:i32 IntRegs:i32:$src2,
918 // i16:Other), IntRegs:i32:$src1, SETGT:Other), IntRegs:i32:$src1,
919 // IntRegs:i32:$src2)
920 // Emits: (MINh_rr:i32 IntRegs:i32:$src1, IntRegs:i32:$src2)
921 // Pattern complexity = 9 cost = 1 size = 0.
922 if (cast<CondCodeSDNode>(N02)->get() == ISD::SETGT) {
923 SDValue N1 = N->getOperand(1);
925 SDValue N2 = N->getOperand(2);
927 N0.getNode()->getValueType(N0.getResNo()) == MVT::i1 &&
928 N00.getNode()->getValueType(N00.getResNo()) == MVT::i32) {
929 SDNode *SextNode = CurDAG->getMachineNode(Hexagon::A2_sxth, dl,
931 SDNode *Result = CurDAG->getMachineNode(Hexagon::A2_min, dl,
933 SDValue(SextNode, 0),
935 ReplaceUses(N, Result);
944 return SelectCode(N);
948 SDNode *HexagonDAGToDAGISel::SelectTruncate(SDNode *N) {
950 SDValue Shift = N->getOperand(0);
953 // %conv.i = sext i32 %tmp1 to i64
954 // %conv2.i = sext i32 %add to i64
955 // %mul.i = mul nsw i64 %conv2.i, %conv.i
956 // %shr5.i = lshr i64 %mul.i, 32
957 // %conv3.i = trunc i64 %shr5.i to i32
959 // --- match with the following ---
961 // %conv3.i = mpy (%tmp1, %add)
964 if (N->getValueType(0) == MVT::i32) {
966 if (Shift.getNode()->getValueType(0) == MVT::i64) {
967 // Trunc child is logical shift right.
968 if (Shift.getOpcode() != ISD::SRL) {
969 return SelectCode(N);
972 SDValue ShiftOp0 = Shift.getOperand(0);
973 SDValue ShiftOp1 = Shift.getOperand(1);
976 if (ShiftOp1.getOpcode() != ISD::Constant) {
977 return SelectCode(N);
981 cast<ConstantSDNode>(ShiftOp1.getNode())->getSExtValue();
982 if (ShiftConst != 32) {
983 return SelectCode(N);
986 // Shifting a i64 signed multiply
987 SDValue Mul = ShiftOp0;
988 if (Mul.getOpcode() != ISD::MUL) {
989 return SelectCode(N);
992 SDValue MulOp0 = Mul.getOperand(0);
993 SDValue MulOp1 = Mul.getOperand(1);
998 // Handle sign_extend and sextload
999 if (MulOp0.getOpcode() == ISD::SIGN_EXTEND) {
1000 SDValue Sext0 = MulOp0.getOperand(0);
1001 if (Sext0.getNode()->getValueType(0) != MVT::i32) {
1002 return SelectCode(N);
1006 } else if (MulOp0.getOpcode() == ISD::LOAD) {
1007 LoadSDNode *LD = cast<LoadSDNode>(MulOp0.getNode());
1008 if (LD->getMemoryVT() != MVT::i32 ||
1009 LD->getExtensionType() != ISD::SEXTLOAD ||
1010 LD->getAddressingMode() != ISD::UNINDEXED) {
1011 return SelectCode(N);
1014 SDValue Chain = LD->getChain();
1015 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
1016 OP0 = SDValue(CurDAG->getMachineNode(Hexagon::L2_loadri_io, dl, MVT::i32,
1019 TargetConst0, Chain), 0);
1021 return SelectCode(N);
1024 // Same goes for the second operand.
1025 if (MulOp1.getOpcode() == ISD::SIGN_EXTEND) {
1026 SDValue Sext1 = MulOp1.getOperand(0);
1027 if (Sext1.getNode()->getValueType(0) != MVT::i32)
1028 return SelectCode(N);
1031 } else if (MulOp1.getOpcode() == ISD::LOAD) {
1032 LoadSDNode *LD = cast<LoadSDNode>(MulOp1.getNode());
1033 if (LD->getMemoryVT() != MVT::i32 ||
1034 LD->getExtensionType() != ISD::SEXTLOAD ||
1035 LD->getAddressingMode() != ISD::UNINDEXED) {
1036 return SelectCode(N);
1039 SDValue Chain = LD->getChain();
1040 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
1041 OP1 = SDValue(CurDAG->getMachineNode(Hexagon::L2_loadri_io, dl, MVT::i32,
1044 TargetConst0, Chain), 0);
1046 return SelectCode(N);
1049 // Generate a mpy instruction.
1050 SDNode *Result = CurDAG->getMachineNode(Hexagon::M2_mpy_up, dl, MVT::i32,
1052 ReplaceUses(N, Result);
1057 return SelectCode(N);
1061 SDNode *HexagonDAGToDAGISel::SelectSHL(SDNode *N) {
1063 if (N->getValueType(0) == MVT::i32) {
1064 SDValue Shl_0 = N->getOperand(0);
1065 SDValue Shl_1 = N->getOperand(1);
1067 if (Shl_1.getOpcode() == ISD::Constant) {
1068 if (Shl_0.getOpcode() == ISD::MUL) {
1069 SDValue Mul_0 = Shl_0.getOperand(0); // Val
1070 SDValue Mul_1 = Shl_0.getOperand(1); // Const
1071 // RHS of mul is const.
1072 if (Mul_1.getOpcode() == ISD::Constant) {
1074 cast<ConstantSDNode>(Shl_1.getNode())->getSExtValue();
1076 cast<ConstantSDNode>(Mul_1.getNode())->getSExtValue();
1077 int32_t ValConst = MulConst << ShlConst;
1078 SDValue Val = CurDAG->getTargetConstant(ValConst,
1080 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Val.getNode()))
1081 if (isInt<9>(CN->getSExtValue())) {
1083 CurDAG->getMachineNode(Hexagon::M2_mpysmi, dl,
1084 MVT::i32, Mul_0, Val);
1085 ReplaceUses(N, Result);
1090 } else if (Shl_0.getOpcode() == ISD::SUB) {
1091 SDValue Sub_0 = Shl_0.getOperand(0); // Const 0
1092 SDValue Sub_1 = Shl_0.getOperand(1); // Val
1093 if (Sub_0.getOpcode() == ISD::Constant) {
1095 cast<ConstantSDNode>(Sub_0.getNode())->getSExtValue();
1096 if (SubConst == 0) {
1097 if (Sub_1.getOpcode() == ISD::SHL) {
1098 SDValue Shl2_0 = Sub_1.getOperand(0); // Val
1099 SDValue Shl2_1 = Sub_1.getOperand(1); // Const
1100 if (Shl2_1.getOpcode() == ISD::Constant) {
1102 cast<ConstantSDNode>(Shl_1.getNode())->getSExtValue();
1104 cast<ConstantSDNode>(Shl2_1.getNode())->getSExtValue();
1105 int32_t ValConst = 1 << (ShlConst+Shl2Const);
1106 SDValue Val = CurDAG->getTargetConstant(-ValConst, MVT::i32);
1107 if (ConstantSDNode *CN =
1108 dyn_cast<ConstantSDNode>(Val.getNode()))
1109 if (isInt<9>(CN->getSExtValue())) {
1111 CurDAG->getMachineNode(Hexagon::M2_mpysmi, dl, MVT::i32,
1113 ReplaceUses(N, Result);
1123 return SelectCode(N);
1128 // If there is an zero_extend followed an intrinsic in DAG (this means - the
1129 // result of the intrinsic is predicate); convert the zero_extend to
1130 // transfer instruction.
1132 // Zero extend -> transfer is lowered here. Otherwise, zero_extend will be
1133 // converted into a MUX as predicate registers defined as 1 bit in the
1134 // compiler. Architecture defines them as 8-bit registers.
1135 // We want to preserve all the lower 8-bits and, not just 1 LSB bit.
1137 SDNode *HexagonDAGToDAGISel::SelectZeroExtend(SDNode *N) {
1139 SDNode *IsIntrinsic = N->getOperand(0).getNode();
1140 if ((IsIntrinsic->getOpcode() == ISD::INTRINSIC_WO_CHAIN)) {
1142 cast<ConstantSDNode>(IsIntrinsic->getOperand(0))->getZExtValue();
1143 if (doesIntrinsicReturnPredicate(ID)) {
1144 // Now we need to differentiate target data types.
1145 if (N->getValueType(0) == MVT::i64) {
1146 // Convert the zero_extend to Rs = Pd followed by COMBINE_rr(0,Rs).
1147 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
1148 SDNode *Result_1 = CurDAG->getMachineNode(Hexagon::C2_tfrpr, dl,
1150 SDValue(IsIntrinsic, 0));
1151 SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A2_tfrsi, dl,
1154 SDNode *Result_3 = CurDAG->getMachineNode(Hexagon::A2_combinew, dl,
1155 MVT::i64, MVT::Other,
1156 SDValue(Result_2, 0),
1157 SDValue(Result_1, 0));
1158 ReplaceUses(N, Result_3);
1161 if (N->getValueType(0) == MVT::i32) {
1162 // Convert the zero_extend to Rs = Pd
1163 SDNode* RsPd = CurDAG->getMachineNode(Hexagon::C2_tfrpr, dl,
1165 SDValue(IsIntrinsic, 0));
1166 ReplaceUses(N, RsPd);
1169 llvm_unreachable("Unexpected value type");
1172 return SelectCode(N);
1176 // Checking for intrinsics which have predicate registers as operand(s)
1177 // and lowering to the actual intrinsic.
1179 SDNode *HexagonDAGToDAGISel::SelectIntrinsicWOChain(SDNode *N) {
1180 unsigned IID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
1183 case Intrinsic::hexagon_S2_vsplatrb:
1186 case Intrinsic::hexagon_S2_vsplatrh:
1190 return SelectCode(N);
1193 SDValue const &V = N->getOperand(1);
1195 if (isValueExtension(V, Bits, U)) {
1196 SDValue R = CurDAG->getNode(N->getOpcode(), SDLoc(N), N->getValueType(0),
1197 N->getOperand(0), U);
1198 return SelectCode(R.getNode());
1200 return SelectCode(N);
1204 // Map floating point constant values.
1206 SDNode *HexagonDAGToDAGISel::SelectConstantFP(SDNode *N) {
1208 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N);
1209 APFloat APF = CN->getValueAPF();
1210 if (N->getValueType(0) == MVT::f32) {
1211 return CurDAG->getMachineNode(Hexagon::TFRI_f, dl, MVT::f32,
1212 CurDAG->getTargetConstantFP(APF.convertToFloat(), MVT::f32));
1214 else if (N->getValueType(0) == MVT::f64) {
1215 return CurDAG->getMachineNode(Hexagon::CONST64_Float_Real, dl, MVT::f64,
1216 CurDAG->getTargetConstantFP(APF.convertToDouble(), MVT::f64));
1219 return SelectCode(N);
1224 // Map predicate true (encoded as -1 in LLVM) to a XOR.
1226 SDNode *HexagonDAGToDAGISel::SelectConstant(SDNode *N) {
1228 if (N->getValueType(0) == MVT::i1) {
1230 int32_t Val = cast<ConstantSDNode>(N)->getSExtValue();
1232 // Create the IntReg = 1 node.
1234 CurDAG->getMachineNode(Hexagon::A2_tfrsi, dl, MVT::i32,
1235 CurDAG->getTargetConstant(0, MVT::i32));
1238 SDNode* Pd = CurDAG->getMachineNode(Hexagon::C2_tfrrp, dl, MVT::i1,
1239 SDValue(IntRegTFR, 0));
1242 SDNode* NotPd = CurDAG->getMachineNode(Hexagon::C2_not, dl, MVT::i1,
1246 Result = CurDAG->getMachineNode(Hexagon::C2_xor, dl, MVT::i1,
1247 SDValue(Pd, 0), SDValue(NotPd, 0));
1249 // We have just built:
1251 // Pd = xor(not(Pd), Pd)
1253 ReplaceUses(N, Result);
1258 return SelectCode(N);
1263 // Map add followed by a asr -> asr +=.
1265 SDNode *HexagonDAGToDAGISel::SelectAdd(SDNode *N) {
1267 if (N->getValueType(0) != MVT::i32) {
1268 return SelectCode(N);
1270 // Identify nodes of the form: add(asr(...)).
1271 SDNode* Src1 = N->getOperand(0).getNode();
1272 if (Src1->getOpcode() != ISD::SRA || !Src1->hasOneUse()
1273 || Src1->getValueType(0) != MVT::i32) {
1274 return SelectCode(N);
1277 // Build Rd = Rd' + asr(Rs, Rt). The machine constraints will ensure that
1278 // Rd and Rd' are assigned to the same register
1279 SDNode* Result = CurDAG->getMachineNode(Hexagon::S2_asr_r_r_acc, dl, MVT::i32,
1281 Src1->getOperand(0),
1282 Src1->getOperand(1));
1283 ReplaceUses(N, Result);
1289 SDNode *HexagonDAGToDAGISel::Select(SDNode *N) {
1290 if (N->isMachineOpcode()) {
1292 return nullptr; // Already selected.
1296 switch (N->getOpcode()) {
1298 return SelectConstant(N);
1300 case ISD::ConstantFP:
1301 return SelectConstantFP(N);
1304 return SelectAdd(N);
1307 return SelectSHL(N);
1310 return SelectLoad(N);
1313 return SelectStore(N);
1316 return SelectSelect(N);
1319 return SelectTruncate(N);
1322 return SelectMul(N);
1324 case ISD::ZERO_EXTEND:
1325 return SelectZeroExtend(N);
1327 case ISD::INTRINSIC_WO_CHAIN:
1328 return SelectIntrinsicWOChain(N);
1331 return SelectCode(N);
1336 // Hexagon_TODO: Five functions for ADDRri?! Surely there must be a better way
1337 // to define these instructions.
1339 bool HexagonDAGToDAGISel::SelectADDRri(SDValue& Addr, SDValue &Base,
1341 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
1342 Addr.getOpcode() == ISD::TargetGlobalAddress)
1343 return false; // Direct calls.
1345 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
1346 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
1347 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1351 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1356 bool HexagonDAGToDAGISel::SelectADDRriS11_0(SDValue& Addr, SDValue &Base,
1358 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
1359 Addr.getOpcode() == ISD::TargetGlobalAddress)
1360 return false; // Direct calls.
1362 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
1363 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
1364 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1365 return (IsS11_0_Offset(Offset.getNode()));
1368 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1369 return (IsS11_0_Offset(Offset.getNode()));
1373 bool HexagonDAGToDAGISel::SelectADDRriS11_1(SDValue& Addr, SDValue &Base,
1375 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
1376 Addr.getOpcode() == ISD::TargetGlobalAddress)
1377 return false; // Direct calls.
1379 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
1380 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
1381 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1382 return (IsS11_1_Offset(Offset.getNode()));
1385 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1386 return (IsS11_1_Offset(Offset.getNode()));
1390 bool HexagonDAGToDAGISel::SelectADDRriS11_2(SDValue& Addr, SDValue &Base,
1392 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
1393 Addr.getOpcode() == ISD::TargetGlobalAddress)
1394 return false; // Direct calls.
1396 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
1397 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
1398 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1399 return (IsS11_2_Offset(Offset.getNode()));
1402 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1403 return (IsS11_2_Offset(Offset.getNode()));
1407 bool HexagonDAGToDAGISel::SelectADDRriU6_0(SDValue& Addr, SDValue &Base,
1409 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
1410 Addr.getOpcode() == ISD::TargetGlobalAddress)
1411 return false; // Direct calls.
1413 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
1414 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
1415 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1416 return (IsU6_0_Offset(Offset.getNode()));
1419 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1420 return (IsU6_0_Offset(Offset.getNode()));
1424 bool HexagonDAGToDAGISel::SelectADDRriU6_1(SDValue& Addr, SDValue &Base,
1426 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
1427 Addr.getOpcode() == ISD::TargetGlobalAddress)
1428 return false; // Direct calls.
1430 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
1431 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
1432 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1433 return (IsU6_1_Offset(Offset.getNode()));
1436 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1437 return (IsU6_1_Offset(Offset.getNode()));
1441 bool HexagonDAGToDAGISel::SelectADDRriU6_2(SDValue& Addr, SDValue &Base,
1443 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
1444 Addr.getOpcode() == ISD::TargetGlobalAddress)
1445 return false; // Direct calls.
1447 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
1448 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
1449 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1450 return (IsU6_2_Offset(Offset.getNode()));
1453 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1454 return (IsU6_2_Offset(Offset.getNode()));
1458 bool HexagonDAGToDAGISel::SelectMEMriS11_2(SDValue& Addr, SDValue &Base,
1461 if (Addr.getOpcode() != ISD::ADD) {
1462 return(SelectADDRriS11_2(Addr, Base, Offset));
1465 return SelectADDRriS11_2(Addr, Base, Offset);
1469 bool HexagonDAGToDAGISel::SelectADDRriS11_3(SDValue& Addr, SDValue &Base,
1471 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
1472 Addr.getOpcode() == ISD::TargetGlobalAddress)
1473 return false; // Direct calls.
1475 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
1476 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
1477 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1478 return (IsS11_3_Offset(Offset.getNode()));
1481 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1482 return (IsS11_3_Offset(Offset.getNode()));
1485 bool HexagonDAGToDAGISel::SelectADDRrr(SDValue &Addr, SDValue &R1,
1487 if (Addr.getOpcode() == ISD::FrameIndex) return false;
1488 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
1489 Addr.getOpcode() == ISD::TargetGlobalAddress)
1490 return false; // Direct calls.
1492 if (Addr.getOpcode() == ISD::ADD) {
1493 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
1494 if (isInt<13>(CN->getSExtValue()))
1495 return false; // Let the reg+imm pattern catch this!
1496 R1 = Addr.getOperand(0);
1497 R2 = Addr.getOperand(1);
1507 // Handle generic address case. It is accessed from inlined asm =m constraints,
1508 // which could have any kind of pointer.
1509 bool HexagonDAGToDAGISel::SelectAddr(SDNode *Op, SDValue Addr,
1510 SDValue &Base, SDValue &Offset) {
1511 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
1512 Addr.getOpcode() == ISD::TargetGlobalAddress)
1513 return false; // Direct calls.
1515 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
1516 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
1517 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1521 if (Addr.getOpcode() == ISD::ADD) {
1522 Base = Addr.getOperand(0);
1523 Offset = Addr.getOperand(1);
1528 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1533 bool HexagonDAGToDAGISel::
1534 SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
1535 std::vector<SDValue> &OutOps) {
1538 switch (ConstraintCode) {
1539 case 'o': // Offsetable.
1540 case 'v': // Not offsetable.
1541 default: return true;
1542 case 'm': // Memory.
1543 if (!SelectAddr(Op.getNode(), Op, Op0, Op1))
1548 OutOps.push_back(Op0);
1549 OutOps.push_back(Op1);
1553 bool HexagonDAGToDAGISel::isConstExtProfitable(SDNode *N) const {
1554 unsigned UseCount = 0;
1555 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
1559 return (UseCount <= 1);
1563 //===--------------------------------------------------------------------===//
1564 // Return 'true' if use count of the global address is below threshold.
1565 //===--------------------------------------------------------------------===//
1566 bool HexagonDAGToDAGISel::hasNumUsesBelowThresGA(SDNode *N) const {
1567 assert(N->getOpcode() == ISD::TargetGlobalAddress &&
1568 "Expecting a target global address");
1570 // Always try to fold the address.
1571 if (TM.getOptLevel() == CodeGenOpt::Aggressive)
1574 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
1575 DenseMap<const GlobalValue *, unsigned>::const_iterator GI =
1576 GlobalAddressUseCountMap.find(GA->getGlobal());
1578 if (GI == GlobalAddressUseCountMap.end())
1581 return GI->second <= MaxNumOfUsesForConstExtenders;
1584 //===--------------------------------------------------------------------===//
1585 // Return true if the non-GP-relative global address can be folded.
1586 //===--------------------------------------------------------------------===//
1587 inline bool HexagonDAGToDAGISel::foldGlobalAddress(SDValue &N, SDValue &R) {
1588 return foldGlobalAddressImpl(N, R, false);
1591 //===--------------------------------------------------------------------===//
1592 // Return true if the GP-relative global address can be folded.
1593 //===--------------------------------------------------------------------===//
1594 inline bool HexagonDAGToDAGISel::foldGlobalAddressGP(SDValue &N, SDValue &R) {
1595 return foldGlobalAddressImpl(N, R, true);
1598 //===--------------------------------------------------------------------===//
1599 // Fold offset of the global address if number of uses are below threshold.
1600 //===--------------------------------------------------------------------===//
1601 bool HexagonDAGToDAGISel::foldGlobalAddressImpl(SDValue &N, SDValue &R,
1602 bool ShouldLookForGP) {
1603 if (N.getOpcode() == ISD::ADD) {
1604 SDValue N0 = N.getOperand(0);
1605 SDValue N1 = N.getOperand(1);
1606 if ((ShouldLookForGP && (N0.getOpcode() == HexagonISD::CONST32_GP)) ||
1607 (!ShouldLookForGP && (N0.getOpcode() == HexagonISD::CONST32))) {
1608 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N1);
1609 GlobalAddressSDNode *GA =
1610 dyn_cast<GlobalAddressSDNode>(N0.getOperand(0));
1613 (GA->getOpcode() == ISD::TargetGlobalAddress)) {
1614 if ((N0.getOpcode() == HexagonISD::CONST32) &&
1615 !hasNumUsesBelowThresGA(GA))
1617 R = CurDAG->getTargetGlobalAddress(GA->getGlobal(),
1621 (uint64_t)Const->getSExtValue());
1629 bool HexagonDAGToDAGISel::SelectAddrFI(SDValue& N, SDValue &R) {
1630 if (N.getOpcode() != ISD::FrameIndex)
1632 FrameIndexSDNode *FX = cast<FrameIndexSDNode>(N);
1633 R = CurDAG->getTargetFrameIndex(FX->getIndex(), MVT::i32);
1637 inline bool HexagonDAGToDAGISel::SelectAddrGA(SDValue &N, SDValue &R) {
1638 return SelectGlobalAddress(N, R, false);
1641 inline bool HexagonDAGToDAGISel::SelectAddrGP(SDValue &N, SDValue &R) {
1642 return SelectGlobalAddress(N, R, true);
1645 bool HexagonDAGToDAGISel::SelectGlobalAddress(SDValue &N, SDValue &R,
1647 switch (N.getOpcode()) {
1649 SDValue N0 = N.getOperand(0);
1650 SDValue N1 = N.getOperand(1);
1651 unsigned GAOpc = N0.getOpcode();
1652 if (UseGP && GAOpc != HexagonISD::CONST32_GP)
1654 if (!UseGP && GAOpc != HexagonISD::CONST32)
1656 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N1)) {
1657 SDValue Addr = N0.getOperand(0);
1658 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Addr)) {
1659 if (GA->getOpcode() == ISD::TargetGlobalAddress) {
1660 uint64_t NewOff = GA->getOffset() + (uint64_t)Const->getSExtValue();
1661 R = CurDAG->getTargetGlobalAddress(GA->getGlobal(), SDLoc(Const),
1662 N.getValueType(), NewOff);
1669 case HexagonISD::CONST32:
1670 // The operand(0) of CONST32 is TargetGlobalAddress, which is what we
1671 // want in the instruction.
1673 R = N.getOperand(0);
1675 case HexagonISD::CONST32_GP:
1677 R = N.getOperand(0);
1686 bool HexagonDAGToDAGISel::isValueExtension(SDValue const &Val,
1687 unsigned FromBits, SDValue &Src) {
1688 unsigned Opc = Val.getOpcode();
1690 case ISD::SIGN_EXTEND:
1691 case ISD::ZERO_EXTEND:
1692 case ISD::ANY_EXTEND: {
1693 SDValue const &Op0 = Val.getOperand(0);
1694 EVT T = Op0.getValueType();
1695 if (T.isInteger() && T.getSizeInBits() == FromBits) {
1701 case ISD::SIGN_EXTEND_INREG:
1702 case ISD::AssertSext:
1703 case ISD::AssertZext:
1704 if (Val.getOperand(0).getValueType().isInteger()) {
1705 VTSDNode *T = cast<VTSDNode>(Val.getOperand(1));
1706 if (T->getVT().getSizeInBits() == FromBits) {
1707 Src = Val.getOperand(0);
1713 // Check if this is an AND with "FromBits" of lower bits set to 1.
1714 uint64_t FromMask = (1 << FromBits) - 1;
1715 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(0))) {
1716 if (C->getZExtValue() == FromMask) {
1717 Src = Val.getOperand(1);
1721 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(1))) {
1722 if (C->getZExtValue() == FromMask) {
1723 Src = Val.getOperand(0);
1731 // OR/XOR with the lower "FromBits" bits set to 0.
1732 uint64_t FromMask = (1 << FromBits) - 1;
1733 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(0))) {
1734 if ((C->getZExtValue() & FromMask) == 0) {
1735 Src = Val.getOperand(1);
1739 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(1))) {
1740 if ((C->getZExtValue() & FromMask) == 0) {
1741 Src = Val.getOperand(0);