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 bool SelectAddrFI(SDValue &N, SDValue &R);
82 const char *getPassName() const override {
83 return "Hexagon DAG->DAG Pattern Instruction Selection";
86 bool runOnMachineFunction(MachineFunction &MF) override {
87 Subtarget = &MF.getSubtarget<HexagonSubtarget>();
88 return SelectionDAGISel::runOnMachineFunction(MF);
91 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
92 /// inline asm expressions.
93 bool SelectInlineAsmMemoryOperand(const SDValue &Op,
95 std::vector<SDValue> &OutOps) override;
96 bool SelectAddr(SDNode *Op, SDValue Addr, SDValue &Base, SDValue &Offset);
98 SDNode *SelectLoad(SDNode *N);
99 SDNode *SelectBaseOffsetLoad(LoadSDNode *LD, SDLoc dl);
100 SDNode *SelectIndexedLoad(LoadSDNode *LD, SDLoc dl);
101 SDNode *SelectIndexedLoadZeroExtend64(LoadSDNode *LD, unsigned Opcode,
103 SDNode *SelectIndexedLoadSignExtend64(LoadSDNode *LD, unsigned Opcode,
105 SDNode *SelectBaseOffsetStore(StoreSDNode *ST, SDLoc dl);
106 SDNode *SelectIndexedStore(StoreSDNode *ST, SDLoc dl);
107 SDNode *SelectStore(SDNode *N);
108 SDNode *SelectSHL(SDNode *N);
109 SDNode *SelectSelect(SDNode *N);
110 SDNode *SelectTruncate(SDNode *N);
111 SDNode *SelectMul(SDNode *N);
112 SDNode *SelectZeroExtend(SDNode *N);
113 SDNode *SelectIntrinsicWOChain(SDNode *N);
114 SDNode *SelectIntrinsicWChain(SDNode *N);
115 SDNode *SelectConstant(SDNode *N);
116 SDNode *SelectConstantFP(SDNode *N);
117 SDNode *SelectAdd(SDNode *N);
118 bool isConstExtProfitable(SDNode *N) const;
120 // XformMskToBitPosU5Imm - Returns the bit position which
121 // the single bit 32 bit mask represents.
122 // Used in Clr and Set bit immediate memops.
123 SDValue XformMskToBitPosU5Imm(uint32_t Imm) {
125 bitPos = Log2_32(Imm);
126 assert(bitPos >= 0 && bitPos < 32 &&
127 "Constant out of range for 32 BitPos Memops");
128 return CurDAG->getTargetConstant(bitPos, MVT::i32);
131 // XformMskToBitPosU4Imm - Returns the bit position which the single bit 16 bit
132 // mask represents. Used in Clr and Set bit immediate memops.
133 SDValue XformMskToBitPosU4Imm(uint16_t Imm) {
134 return XformMskToBitPosU5Imm(Imm);
137 // XformMskToBitPosU3Imm - Returns the bit position which the single bit 8 bit
138 // mask represents. Used in Clr and Set bit immediate memops.
139 SDValue XformMskToBitPosU3Imm(uint8_t Imm) {
140 return XformMskToBitPosU5Imm(Imm);
143 // Return true if there is exactly one bit set in V, i.e., if V is one of the
144 // following integers: 2^0, 2^1, ..., 2^31.
145 bool ImmIsSingleBit(uint32_t v) const {
146 uint32_t c = CountPopulation_64(v);
147 // Only return true if we counted 1 bit.
151 // XformM5ToU5Imm - Return a target constant with the specified value, of type
152 // i32 where the negative literal is transformed into a positive literal for
154 inline SDValue XformM5ToU5Imm(signed Imm) {
155 assert( (Imm >= -31 && Imm <= -1) && "Constant out of range for Memops");
156 return CurDAG->getTargetConstant( - Imm, MVT::i32);
160 // XformU7ToU7M1Imm - Return a target constant decremented by 1, in range
161 // [1..128], used in cmpb.gtu instructions.
162 inline SDValue XformU7ToU7M1Imm(signed Imm) {
163 assert((Imm >= 1 && Imm <= 128) && "Constant out of range for cmpb op");
164 return CurDAG->getTargetConstant(Imm - 1, MVT::i8);
167 // XformS8ToS8M1Imm - Return a target constant decremented by 1.
168 inline SDValue XformSToSM1Imm(signed Imm) {
169 return CurDAG->getTargetConstant(Imm - 1, MVT::i32);
172 // XformU8ToU8M1Imm - Return a target constant decremented by 1.
173 inline SDValue XformUToUM1Imm(unsigned Imm) {
174 assert((Imm >= 1) && "Cannot decrement unsigned int less than 1");
175 return CurDAG->getTargetConstant(Imm - 1, MVT::i32);
178 // XformSToSM2Imm - Return a target constant decremented by 2.
179 inline SDValue XformSToSM2Imm(unsigned Imm) {
180 return CurDAG->getTargetConstant(Imm - 2, MVT::i32);
183 // XformSToSM3Imm - Return a target constant decremented by 3.
184 inline SDValue XformSToSM3Imm(unsigned Imm) {
185 return CurDAG->getTargetConstant(Imm - 3, MVT::i32);
188 // Include the pieces autogenerated from the target description.
189 #include "HexagonGenDAGISel.inc"
192 bool isValueExtension(SDValue const &Val, unsigned FromBits, SDValue &Src);
194 } // end anonymous namespace
197 /// createHexagonISelDag - This pass converts a legalized DAG into a
198 /// Hexagon-specific DAG, ready for instruction scheduling.
200 FunctionPass *llvm::createHexagonISelDag(HexagonTargetMachine &TM,
201 CodeGenOpt::Level OptLevel) {
202 return new HexagonDAGToDAGISel(TM, OptLevel);
205 static void initializePassOnce(PassRegistry &Registry) {
206 const char *Name = "Hexagon DAG->DAG Pattern Instruction Selection";
207 PassInfo *PI = new PassInfo(Name, "hexagon-isel",
208 &SelectionDAGISel::ID, nullptr, false, false);
209 Registry.registerPass(*PI, true);
212 void llvm::initializeHexagonDAGToDAGISelPass(PassRegistry &Registry) {
213 CALL_ONCE_INITIALIZATION(initializePassOnce)
217 static bool IsS11_0_Offset(SDNode * S) {
218 ConstantSDNode *N = cast<ConstantSDNode>(S);
220 // immS16 predicate - True if the immediate fits in a 16-bit sign extended
222 int64_t v = (int64_t)N->getSExtValue();
227 static bool IsS11_1_Offset(SDNode * S) {
228 ConstantSDNode *N = cast<ConstantSDNode>(S);
230 // immS16 predicate - True if the immediate fits in a 16-bit sign extended
232 int64_t v = (int64_t)N->getSExtValue();
233 return isShiftedInt<11,1>(v);
237 static bool IsS11_2_Offset(SDNode * S) {
238 ConstantSDNode *N = cast<ConstantSDNode>(S);
240 // immS16 predicate - True if the immediate fits in a 16-bit sign extended
242 int64_t v = (int64_t)N->getSExtValue();
243 return isShiftedInt<11,2>(v);
247 static bool IsS11_3_Offset(SDNode * S) {
248 ConstantSDNode *N = cast<ConstantSDNode>(S);
250 // immS16 predicate - True if the immediate fits in a 16-bit sign extended
252 int64_t v = (int64_t)N->getSExtValue();
253 return isShiftedInt<11,3>(v);
257 static bool IsU6_0_Offset(SDNode * S) {
258 ConstantSDNode *N = cast<ConstantSDNode>(S);
260 // u6 predicate - True if the immediate fits in a 6-bit unsigned extended
262 int64_t v = (int64_t)N->getSExtValue();
267 static bool IsU6_1_Offset(SDNode * S) {
268 ConstantSDNode *N = cast<ConstantSDNode>(S);
270 // u6 predicate - True if the immediate fits in a 6-bit unsigned extended
272 int64_t v = (int64_t)N->getSExtValue();
273 return isShiftedUInt<6,1>(v);
277 static bool IsU6_2_Offset(SDNode * S) {
278 ConstantSDNode *N = cast<ConstantSDNode>(S);
280 // u6 predicate - True if the immediate fits in a 6-bit unsigned extended
282 int64_t v = (int64_t)N->getSExtValue();
283 return isShiftedUInt<6,2>(v);
287 // Intrinsics that return a a predicate.
288 static unsigned doesIntrinsicReturnPredicate(unsigned ID)
293 case Intrinsic::hexagon_C2_cmpeq:
294 case Intrinsic::hexagon_C2_cmpgt:
295 case Intrinsic::hexagon_C2_cmpgtu:
296 case Intrinsic::hexagon_C2_cmpgtup:
297 case Intrinsic::hexagon_C2_cmpgtp:
298 case Intrinsic::hexagon_C2_cmpeqp:
299 case Intrinsic::hexagon_C2_bitsset:
300 case Intrinsic::hexagon_C2_bitsclr:
301 case Intrinsic::hexagon_C2_cmpeqi:
302 case Intrinsic::hexagon_C2_cmpgti:
303 case Intrinsic::hexagon_C2_cmpgtui:
304 case Intrinsic::hexagon_C2_cmpgei:
305 case Intrinsic::hexagon_C2_cmpgeui:
306 case Intrinsic::hexagon_C2_cmplt:
307 case Intrinsic::hexagon_C2_cmpltu:
308 case Intrinsic::hexagon_C2_bitsclri:
309 case Intrinsic::hexagon_C2_and:
310 case Intrinsic::hexagon_C2_or:
311 case Intrinsic::hexagon_C2_xor:
312 case Intrinsic::hexagon_C2_andn:
313 case Intrinsic::hexagon_C2_not:
314 case Intrinsic::hexagon_C2_orn:
315 case Intrinsic::hexagon_C2_pxfer_map:
316 case Intrinsic::hexagon_C2_any8:
317 case Intrinsic::hexagon_C2_all8:
318 case Intrinsic::hexagon_A2_vcmpbeq:
319 case Intrinsic::hexagon_A2_vcmpbgtu:
320 case Intrinsic::hexagon_A2_vcmpheq:
321 case Intrinsic::hexagon_A2_vcmphgt:
322 case Intrinsic::hexagon_A2_vcmphgtu:
323 case Intrinsic::hexagon_A2_vcmpweq:
324 case Intrinsic::hexagon_A2_vcmpwgt:
325 case Intrinsic::hexagon_A2_vcmpwgtu:
326 case Intrinsic::hexagon_C2_tfrrp:
327 case Intrinsic::hexagon_S2_tstbit_i:
328 case Intrinsic::hexagon_S2_tstbit_r:
333 static bool OffsetFitsS11(EVT MemType, int64_t Offset) {
334 if (MemType == MVT::i64 && isShiftedInt<11,3>(Offset)) {
337 if (MemType == MVT::i32 && isShiftedInt<11,2>(Offset)) {
340 if (MemType == MVT::i16 && isShiftedInt<11,1>(Offset)) {
343 if (MemType == MVT::i8 && isInt<11>(Offset)) {
351 // Try to lower loads of GlobalAdresses into base+offset loads. Custom
352 // lowering for GlobalAddress nodes has already turned it into a
355 SDNode *HexagonDAGToDAGISel::SelectBaseOffsetLoad(LoadSDNode *LD, SDLoc dl) {
356 SDValue Chain = LD->getChain();
357 SDNode* Const32 = LD->getBasePtr().getNode();
360 if (Const32->getOpcode() == HexagonISD::CONST32 &&
361 ISD::isNormalLoad(LD)) {
362 SDValue Base = Const32->getOperand(0);
363 EVT LoadedVT = LD->getMemoryVT();
364 int64_t Offset = cast<GlobalAddressSDNode>(Base)->getOffset();
365 if (Offset != 0 && OffsetFitsS11(LoadedVT, Offset)) {
366 MVT PointerTy = getTargetLowering()->getPointerTy();
367 const GlobalValue* GV =
368 cast<GlobalAddressSDNode>(Base)->getGlobal();
370 CurDAG->getTargetGlobalAddress(GV, dl, PointerTy, 0);
371 SDNode* NewBase = CurDAG->getMachineNode(Hexagon::CONST32_set,
374 // Figure out base + offset opcode
375 if (LoadedVT == MVT::i64) Opcode = Hexagon::L2_loadrd_io;
376 else if (LoadedVT == MVT::i32) Opcode = Hexagon::L2_loadri_io;
377 else if (LoadedVT == MVT::i16) Opcode = Hexagon::L2_loadrh_io;
378 else if (LoadedVT == MVT::i8) Opcode = Hexagon::L2_loadrb_io;
379 else llvm_unreachable("unknown memory type");
381 // Build indexed load.
382 SDValue TargetConstOff = CurDAG->getTargetConstant(Offset, PointerTy);
383 SDNode* Result = CurDAG->getMachineNode(Opcode, dl,
389 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
390 MemOp[0] = LD->getMemOperand();
391 cast<MachineSDNode>(Result)->setMemRefs(MemOp, MemOp + 1);
392 ReplaceUses(LD, Result);
397 return SelectCode(LD);
401 SDNode *HexagonDAGToDAGISel::SelectIndexedLoadSignExtend64(LoadSDNode *LD,
405 SDValue Chain = LD->getChain();
406 EVT LoadedVT = LD->getMemoryVT();
407 SDValue Base = LD->getBasePtr();
408 SDValue Offset = LD->getOffset();
409 SDNode *OffsetNode = Offset.getNode();
410 int32_t Val = cast<ConstantSDNode>(OffsetNode)->getSExtValue();
411 SDValue N1 = LD->getOperand(1);
415 if (SelectADDRriS11_2(N1, CPTmpN1_0, CPTmpN1_1) &&
416 N1.getNode()->getValueType(0) == MVT::i32) {
417 const HexagonInstrInfo *TII = Subtarget->getInstrInfo();
418 if (TII->isValidAutoIncImm(LoadedVT, Val)) {
419 SDValue TargetConst = CurDAG->getTargetConstant(Val, MVT::i32);
420 SDNode *Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::i32, MVT::i32,
421 MVT::Other, Base, TargetConst,
423 SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A2_sxtw, dl, MVT::i64,
424 SDValue(Result_1, 0));
425 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
426 MemOp[0] = LD->getMemOperand();
427 cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
428 const SDValue Froms[] = { SDValue(LD, 0),
432 const SDValue Tos[] = { SDValue(Result_2, 0),
433 SDValue(Result_1, 1),
436 ReplaceUses(Froms, Tos, 3);
439 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
440 SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
441 SDNode *Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::i32,
442 MVT::Other, Base, TargetConst0,
444 SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A2_sxtw, dl,
445 MVT::i64, SDValue(Result_1, 0));
446 SDNode* Result_3 = CurDAG->getMachineNode(Hexagon::ADD_ri, dl,
447 MVT::i32, Base, TargetConstVal,
448 SDValue(Result_1, 1));
449 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
450 MemOp[0] = LD->getMemOperand();
451 cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
452 const SDValue Froms[] = { SDValue(LD, 0),
456 const SDValue Tos[] = { SDValue(Result_2, 0),
457 SDValue(Result_3, 0),
460 ReplaceUses(Froms, Tos, 3);
463 return SelectCode(LD);
467 SDNode *HexagonDAGToDAGISel::SelectIndexedLoadZeroExtend64(LoadSDNode *LD,
471 SDValue Chain = LD->getChain();
472 EVT LoadedVT = LD->getMemoryVT();
473 SDValue Base = LD->getBasePtr();
474 SDValue Offset = LD->getOffset();
475 SDNode *OffsetNode = Offset.getNode();
476 int32_t Val = cast<ConstantSDNode>(OffsetNode)->getSExtValue();
477 SDValue N1 = LD->getOperand(1);
481 if (SelectADDRriS11_2(N1, CPTmpN1_0, CPTmpN1_1) &&
482 N1.getNode()->getValueType(0) == MVT::i32) {
483 const HexagonInstrInfo *TII = Subtarget->getInstrInfo();
484 if (TII->isValidAutoIncImm(LoadedVT, Val)) {
485 SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
486 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
487 SDNode *Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::i32,
488 MVT::i32, MVT::Other, Base,
489 TargetConstVal, Chain);
490 SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A2_tfrsi, dl, MVT::i32,
492 SDNode *Result_3 = CurDAG->getMachineNode(Hexagon::A2_combinew, dl,
493 MVT::i64, MVT::Other,
495 SDValue(Result_1,0));
496 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
497 MemOp[0] = LD->getMemOperand();
498 cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
499 const SDValue Froms[] = { SDValue(LD, 0),
503 const SDValue Tos[] = { SDValue(Result_3, 0),
504 SDValue(Result_1, 1),
507 ReplaceUses(Froms, Tos, 3);
511 // Generate an indirect load.
512 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
513 SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
514 SDNode *Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::i32,
516 Base, TargetConst0, Chain);
517 SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A2_tfrsi, dl, MVT::i32,
519 SDNode *Result_3 = CurDAG->getMachineNode(Hexagon::A2_combinew, dl,
520 MVT::i64, MVT::Other,
522 SDValue(Result_1,0));
523 // Add offset to base.
524 SDNode* Result_4 = CurDAG->getMachineNode(Hexagon::ADD_ri, dl, MVT::i32,
525 Base, TargetConstVal,
526 SDValue(Result_1, 1));
527 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
528 MemOp[0] = LD->getMemOperand();
529 cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
530 const SDValue Froms[] = { SDValue(LD, 0),
534 const SDValue Tos[] = { SDValue(Result_3, 0), // Load value.
535 SDValue(Result_4, 0), // New address.
538 ReplaceUses(Froms, Tos, 3);
542 return SelectCode(LD);
546 SDNode *HexagonDAGToDAGISel::SelectIndexedLoad(LoadSDNode *LD, SDLoc dl) {
547 SDValue Chain = LD->getChain();
548 SDValue Base = LD->getBasePtr();
549 SDValue Offset = LD->getOffset();
550 SDNode *OffsetNode = Offset.getNode();
551 // Get the constant value.
552 int32_t Val = cast<ConstantSDNode>(OffsetNode)->getSExtValue();
553 EVT LoadedVT = LD->getMemoryVT();
556 // Check for zero ext loads.
557 bool zextval = (LD->getExtensionType() == ISD::ZEXTLOAD);
559 // Figure out the opcode.
560 const HexagonInstrInfo *TII = Subtarget->getInstrInfo();
561 if (LoadedVT == MVT::i64) {
562 if (TII->isValidAutoIncImm(LoadedVT, Val))
563 Opcode = Hexagon::L2_loadrd_pi;
565 Opcode = Hexagon::L2_loadrd_io;
566 } else if (LoadedVT == MVT::i32) {
567 if (TII->isValidAutoIncImm(LoadedVT, Val))
568 Opcode = Hexagon::L2_loadri_pi;
570 Opcode = Hexagon::L2_loadri_io;
571 } else if (LoadedVT == MVT::i16) {
572 if (TII->isValidAutoIncImm(LoadedVT, Val))
573 Opcode = zextval ? Hexagon::L2_loadruh_pi : Hexagon::L2_loadrh_pi;
575 Opcode = zextval ? Hexagon::L2_loadruh_io : Hexagon::L2_loadrh_io;
576 } else if (LoadedVT == MVT::i8) {
577 if (TII->isValidAutoIncImm(LoadedVT, Val))
578 Opcode = zextval ? Hexagon::L2_loadrub_pi : Hexagon::L2_loadrb_pi;
580 Opcode = zextval ? Hexagon::L2_loadrub_io : Hexagon::L2_loadrb_io;
582 llvm_unreachable("unknown memory type");
584 // For zero ext i64 loads, we need to add combine instructions.
585 if (LD->getValueType(0) == MVT::i64 &&
586 LD->getExtensionType() == ISD::ZEXTLOAD) {
587 return SelectIndexedLoadZeroExtend64(LD, Opcode, dl);
589 if (LD->getValueType(0) == MVT::i64 &&
590 LD->getExtensionType() == ISD::SEXTLOAD) {
591 // Handle sign ext i64 loads.
592 return SelectIndexedLoadSignExtend64(LD, Opcode, dl);
594 if (TII->isValidAutoIncImm(LoadedVT, Val)) {
595 SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
596 SDNode* Result = CurDAG->getMachineNode(Opcode, dl,
598 MVT::i32, MVT::Other, Base,
599 TargetConstVal, Chain);
600 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
601 MemOp[0] = LD->getMemOperand();
602 cast<MachineSDNode>(Result)->setMemRefs(MemOp, MemOp + 1);
603 const SDValue Froms[] = { SDValue(LD, 0),
607 const SDValue Tos[] = { SDValue(Result, 0),
611 ReplaceUses(Froms, Tos, 3);
614 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
615 SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
616 SDNode* Result_1 = CurDAG->getMachineNode(Opcode, dl,
618 MVT::Other, Base, TargetConst0,
620 SDNode* Result_2 = CurDAG->getMachineNode(Hexagon::ADD_ri, dl, MVT::i32,
621 Base, TargetConstVal,
622 SDValue(Result_1, 1));
623 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
624 MemOp[0] = LD->getMemOperand();
625 cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
626 const SDValue Froms[] = { SDValue(LD, 0),
630 const SDValue Tos[] = { SDValue(Result_1, 0),
631 SDValue(Result_2, 0),
634 ReplaceUses(Froms, Tos, 3);
640 SDNode *HexagonDAGToDAGISel::SelectLoad(SDNode *N) {
643 LoadSDNode *LD = cast<LoadSDNode>(N);
644 ISD::MemIndexedMode AM = LD->getAddressingMode();
646 // Handle indexed loads.
647 if (AM != ISD::UNINDEXED) {
648 result = SelectIndexedLoad(LD, dl);
650 result = SelectBaseOffsetLoad(LD, dl);
657 SDNode *HexagonDAGToDAGISel::SelectIndexedStore(StoreSDNode *ST, SDLoc dl) {
658 SDValue Chain = ST->getChain();
659 SDValue Base = ST->getBasePtr();
660 SDValue Offset = ST->getOffset();
661 SDValue Value = ST->getValue();
662 SDNode *OffsetNode = Offset.getNode();
663 // Get the constant value.
664 int32_t Val = cast<ConstantSDNode>(OffsetNode)->getSExtValue();
665 EVT StoredVT = ST->getMemoryVT();
667 // Offset value must be within representable range
668 // and must have correct alignment properties.
669 const HexagonInstrInfo *TII = Subtarget->getInstrInfo();
670 if (TII->isValidAutoIncImm(StoredVT, Val)) {
671 SDValue Ops[] = {Base, CurDAG->getTargetConstant(Val, MVT::i32), Value,
675 // Figure out the post inc version of opcode.
676 if (StoredVT == MVT::i64) Opcode = Hexagon::S2_storerd_pi;
677 else if (StoredVT == MVT::i32) Opcode = Hexagon::S2_storeri_pi;
678 else if (StoredVT == MVT::i16) Opcode = Hexagon::S2_storerh_pi;
679 else if (StoredVT == MVT::i8) Opcode = Hexagon::S2_storerb_pi;
680 else llvm_unreachable("unknown memory type");
682 // Build post increment store.
683 SDNode* Result = CurDAG->getMachineNode(Opcode, dl, MVT::i32,
685 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
686 MemOp[0] = ST->getMemOperand();
687 cast<MachineSDNode>(Result)->setMemRefs(MemOp, MemOp + 1);
689 ReplaceUses(ST, Result);
690 ReplaceUses(SDValue(ST,1), SDValue(Result,1));
694 // Note: Order of operands matches the def of instruction:
695 // def STrid : STInst<(outs), (ins MEMri:$addr, DoubleRegs:$src1), ...
696 // and it differs for POST_ST* for instance.
697 SDValue Ops[] = { Base, CurDAG->getTargetConstant(0, MVT::i32), Value,
701 // Figure out the opcode.
702 if (StoredVT == MVT::i64) Opcode = Hexagon::S2_storerd_io;
703 else if (StoredVT == MVT::i32) Opcode = Hexagon::S2_storeri_io;
704 else if (StoredVT == MVT::i16) Opcode = Hexagon::S2_storerh_io;
705 else if (StoredVT == MVT::i8) Opcode = Hexagon::S2_storerb_io;
706 else llvm_unreachable("unknown memory type");
708 // Build regular store.
709 SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
710 SDNode* Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::Other, Ops);
711 // Build splitted incriment instruction.
712 SDNode* Result_2 = CurDAG->getMachineNode(Hexagon::ADD_ri, dl, MVT::i32,
715 SDValue(Result_1, 0));
716 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
717 MemOp[0] = ST->getMemOperand();
718 cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
720 ReplaceUses(SDValue(ST,0), SDValue(Result_2,0));
721 ReplaceUses(SDValue(ST,1), SDValue(Result_1,0));
726 SDNode *HexagonDAGToDAGISel::SelectBaseOffsetStore(StoreSDNode *ST,
728 SDValue Chain = ST->getChain();
729 SDNode* Const32 = ST->getBasePtr().getNode();
730 SDValue Value = ST->getValue();
733 // Try to lower stores of GlobalAdresses into indexed stores. Custom
734 // lowering for GlobalAddress nodes has already turned it into a
735 // CONST32. Avoid truncating stores for the moment. Post-inc stores
736 // do the same. Don't think there's a reason for it, so will file a
738 if ((Const32->getOpcode() == HexagonISD::CONST32) &&
739 !(Value.getValueType() == MVT::i64 && ST->isTruncatingStore())) {
740 SDValue Base = Const32->getOperand(0);
741 if (Base.getOpcode() == ISD::TargetGlobalAddress) {
742 EVT StoredVT = ST->getMemoryVT();
743 int64_t Offset = cast<GlobalAddressSDNode>(Base)->getOffset();
744 if (Offset != 0 && OffsetFitsS11(StoredVT, Offset)) {
745 MVT PointerTy = getTargetLowering()->getPointerTy();
746 const GlobalValue* GV =
747 cast<GlobalAddressSDNode>(Base)->getGlobal();
749 CurDAG->getTargetGlobalAddress(GV, dl, PointerTy, 0);
750 SDNode* NewBase = CurDAG->getMachineNode(Hexagon::CONST32_set,
754 // Figure out base + offset opcode
755 if (StoredVT == MVT::i64) Opcode = Hexagon::S2_storerd_io;
756 else if (StoredVT == MVT::i32) Opcode = Hexagon::S2_storeri_io;
757 else if (StoredVT == MVT::i16) Opcode = Hexagon::S2_storerh_io;
758 else if (StoredVT == MVT::i8) Opcode = Hexagon::S2_storerb_io;
759 else llvm_unreachable("unknown memory type");
761 SDValue Ops[] = {SDValue(NewBase,0),
762 CurDAG->getTargetConstant(Offset,PointerTy),
764 // build indexed store
765 SDNode* Result = CurDAG->getMachineNode(Opcode, dl,
767 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
768 MemOp[0] = ST->getMemOperand();
769 cast<MachineSDNode>(Result)->setMemRefs(MemOp, MemOp + 1);
770 ReplaceUses(ST, Result);
776 return SelectCode(ST);
780 SDNode *HexagonDAGToDAGISel::SelectStore(SDNode *N) {
782 StoreSDNode *ST = cast<StoreSDNode>(N);
783 ISD::MemIndexedMode AM = ST->getAddressingMode();
785 // Handle indexed stores.
786 if (AM != ISD::UNINDEXED) {
787 return SelectIndexedStore(ST, dl);
790 return SelectBaseOffsetStore(ST, dl);
793 SDNode *HexagonDAGToDAGISel::SelectMul(SDNode *N) {
797 // %conv.i = sext i32 %tmp1 to i64
798 // %conv2.i = sext i32 %add to i64
799 // %mul.i = mul nsw i64 %conv2.i, %conv.i
801 // --- match with the following ---
803 // %mul.i = mpy (%tmp1, %add)
806 if (N->getValueType(0) == MVT::i64) {
807 // Shifting a i64 signed multiply.
808 SDValue MulOp0 = N->getOperand(0);
809 SDValue MulOp1 = N->getOperand(1);
814 // Handle sign_extend and sextload.
815 if (MulOp0.getOpcode() == ISD::SIGN_EXTEND) {
816 SDValue Sext0 = MulOp0.getOperand(0);
817 if (Sext0.getNode()->getValueType(0) != MVT::i32) {
818 return SelectCode(N);
822 } else if (MulOp0.getOpcode() == ISD::LOAD) {
823 LoadSDNode *LD = cast<LoadSDNode>(MulOp0.getNode());
824 if (LD->getMemoryVT() != MVT::i32 ||
825 LD->getExtensionType() != ISD::SEXTLOAD ||
826 LD->getAddressingMode() != ISD::UNINDEXED) {
827 return SelectCode(N);
830 SDValue Chain = LD->getChain();
831 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
832 OP0 = SDValue(CurDAG->getMachineNode(Hexagon::L2_loadri_io, dl, MVT::i32,
834 LD->getBasePtr(), TargetConst0,
837 return SelectCode(N);
840 // Same goes for the second operand.
841 if (MulOp1.getOpcode() == ISD::SIGN_EXTEND) {
842 SDValue Sext1 = MulOp1.getOperand(0);
843 if (Sext1.getNode()->getValueType(0) != MVT::i32) {
844 return SelectCode(N);
848 } else if (MulOp1.getOpcode() == ISD::LOAD) {
849 LoadSDNode *LD = cast<LoadSDNode>(MulOp1.getNode());
850 if (LD->getMemoryVT() != MVT::i32 ||
851 LD->getExtensionType() != ISD::SEXTLOAD ||
852 LD->getAddressingMode() != ISD::UNINDEXED) {
853 return SelectCode(N);
856 SDValue Chain = LD->getChain();
857 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
858 OP1 = SDValue(CurDAG->getMachineNode(Hexagon::L2_loadri_io, dl, MVT::i32,
860 LD->getBasePtr(), TargetConst0,
863 return SelectCode(N);
866 // Generate a mpy instruction.
867 SDNode *Result = CurDAG->getMachineNode(Hexagon::M2_dpmpyss_s0, dl, MVT::i64,
869 ReplaceUses(N, Result);
873 return SelectCode(N);
877 SDNode *HexagonDAGToDAGISel::SelectSelect(SDNode *N) {
879 SDValue N0 = N->getOperand(0);
880 if (N0.getOpcode() == ISD::SETCC) {
881 SDValue N00 = N0.getOperand(0);
882 if (N00.getOpcode() == ISD::SIGN_EXTEND_INREG) {
883 SDValue N000 = N00.getOperand(0);
884 SDValue N001 = N00.getOperand(1);
885 if (cast<VTSDNode>(N001)->getVT() == MVT::i16) {
886 SDValue N01 = N0.getOperand(1);
887 SDValue N02 = N0.getOperand(2);
889 // Pattern: (select:i32 (setcc:i1 (sext_inreg:i32 IntRegs:i32:$src2,
890 // i16:Other),IntRegs:i32:$src1, SETLT:Other),IntRegs:i32:$src1,
891 // IntRegs:i32:$src2)
892 // Emits: (MAXh_rr:i32 IntRegs:i32:$src1, IntRegs:i32:$src2)
893 // Pattern complexity = 9 cost = 1 size = 0.
894 if (cast<CondCodeSDNode>(N02)->get() == ISD::SETLT) {
895 SDValue N1 = N->getOperand(1);
897 SDValue N2 = N->getOperand(2);
899 N0.getNode()->getValueType(N0.getResNo()) == MVT::i1 &&
900 N00.getNode()->getValueType(N00.getResNo()) == MVT::i32) {
901 SDNode *SextNode = CurDAG->getMachineNode(Hexagon::A2_sxth, dl,
903 SDNode *Result = CurDAG->getMachineNode(Hexagon::A2_max, dl,
905 SDValue(SextNode, 0),
907 ReplaceUses(N, Result);
913 // Pattern: (select:i32 (setcc:i1 (sext_inreg:i32 IntRegs:i32:$src2,
914 // i16:Other), IntRegs:i32:$src1, SETGT:Other), IntRegs:i32:$src1,
915 // IntRegs:i32:$src2)
916 // Emits: (MINh_rr:i32 IntRegs:i32:$src1, IntRegs:i32:$src2)
917 // Pattern complexity = 9 cost = 1 size = 0.
918 if (cast<CondCodeSDNode>(N02)->get() == ISD::SETGT) {
919 SDValue N1 = N->getOperand(1);
921 SDValue N2 = N->getOperand(2);
923 N0.getNode()->getValueType(N0.getResNo()) == MVT::i1 &&
924 N00.getNode()->getValueType(N00.getResNo()) == MVT::i32) {
925 SDNode *SextNode = CurDAG->getMachineNode(Hexagon::A2_sxth, dl,
927 SDNode *Result = CurDAG->getMachineNode(Hexagon::A2_min, dl,
929 SDValue(SextNode, 0),
931 ReplaceUses(N, Result);
940 return SelectCode(N);
944 SDNode *HexagonDAGToDAGISel::SelectTruncate(SDNode *N) {
946 SDValue Shift = N->getOperand(0);
949 // %conv.i = sext i32 %tmp1 to i64
950 // %conv2.i = sext i32 %add to i64
951 // %mul.i = mul nsw i64 %conv2.i, %conv.i
952 // %shr5.i = lshr i64 %mul.i, 32
953 // %conv3.i = trunc i64 %shr5.i to i32
955 // --- match with the following ---
957 // %conv3.i = mpy (%tmp1, %add)
960 if (N->getValueType(0) == MVT::i32) {
962 if (Shift.getNode()->getValueType(0) == MVT::i64) {
963 // Trunc child is logical shift right.
964 if (Shift.getOpcode() != ISD::SRL) {
965 return SelectCode(N);
968 SDValue ShiftOp0 = Shift.getOperand(0);
969 SDValue ShiftOp1 = Shift.getOperand(1);
972 if (ShiftOp1.getOpcode() != ISD::Constant) {
973 return SelectCode(N);
977 cast<ConstantSDNode>(ShiftOp1.getNode())->getSExtValue();
978 if (ShiftConst != 32) {
979 return SelectCode(N);
982 // Shifting a i64 signed multiply
983 SDValue Mul = ShiftOp0;
984 if (Mul.getOpcode() != ISD::MUL) {
985 return SelectCode(N);
988 SDValue MulOp0 = Mul.getOperand(0);
989 SDValue MulOp1 = Mul.getOperand(1);
994 // Handle sign_extend and sextload
995 if (MulOp0.getOpcode() == ISD::SIGN_EXTEND) {
996 SDValue Sext0 = MulOp0.getOperand(0);
997 if (Sext0.getNode()->getValueType(0) != MVT::i32) {
998 return SelectCode(N);
1002 } else if (MulOp0.getOpcode() == ISD::LOAD) {
1003 LoadSDNode *LD = cast<LoadSDNode>(MulOp0.getNode());
1004 if (LD->getMemoryVT() != MVT::i32 ||
1005 LD->getExtensionType() != ISD::SEXTLOAD ||
1006 LD->getAddressingMode() != ISD::UNINDEXED) {
1007 return SelectCode(N);
1010 SDValue Chain = LD->getChain();
1011 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
1012 OP0 = SDValue(CurDAG->getMachineNode(Hexagon::L2_loadri_io, dl, MVT::i32,
1015 TargetConst0, Chain), 0);
1017 return SelectCode(N);
1020 // Same goes for the second operand.
1021 if (MulOp1.getOpcode() == ISD::SIGN_EXTEND) {
1022 SDValue Sext1 = MulOp1.getOperand(0);
1023 if (Sext1.getNode()->getValueType(0) != MVT::i32)
1024 return SelectCode(N);
1027 } else if (MulOp1.getOpcode() == ISD::LOAD) {
1028 LoadSDNode *LD = cast<LoadSDNode>(MulOp1.getNode());
1029 if (LD->getMemoryVT() != MVT::i32 ||
1030 LD->getExtensionType() != ISD::SEXTLOAD ||
1031 LD->getAddressingMode() != ISD::UNINDEXED) {
1032 return SelectCode(N);
1035 SDValue Chain = LD->getChain();
1036 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
1037 OP1 = SDValue(CurDAG->getMachineNode(Hexagon::L2_loadri_io, dl, MVT::i32,
1040 TargetConst0, Chain), 0);
1042 return SelectCode(N);
1045 // Generate a mpy instruction.
1046 SDNode *Result = CurDAG->getMachineNode(Hexagon::M2_mpy_up, dl, MVT::i32,
1048 ReplaceUses(N, Result);
1053 return SelectCode(N);
1057 SDNode *HexagonDAGToDAGISel::SelectSHL(SDNode *N) {
1059 if (N->getValueType(0) == MVT::i32) {
1060 SDValue Shl_0 = N->getOperand(0);
1061 SDValue Shl_1 = N->getOperand(1);
1063 if (Shl_1.getOpcode() == ISD::Constant) {
1064 if (Shl_0.getOpcode() == ISD::MUL) {
1065 SDValue Mul_0 = Shl_0.getOperand(0); // Val
1066 SDValue Mul_1 = Shl_0.getOperand(1); // Const
1067 // RHS of mul is const.
1068 if (Mul_1.getOpcode() == ISD::Constant) {
1070 cast<ConstantSDNode>(Shl_1.getNode())->getSExtValue();
1072 cast<ConstantSDNode>(Mul_1.getNode())->getSExtValue();
1073 int32_t ValConst = MulConst << ShlConst;
1074 SDValue Val = CurDAG->getTargetConstant(ValConst,
1076 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Val.getNode()))
1077 if (isInt<9>(CN->getSExtValue())) {
1079 CurDAG->getMachineNode(Hexagon::M2_mpysmi, dl,
1080 MVT::i32, Mul_0, Val);
1081 ReplaceUses(N, Result);
1086 } else if (Shl_0.getOpcode() == ISD::SUB) {
1087 SDValue Sub_0 = Shl_0.getOperand(0); // Const 0
1088 SDValue Sub_1 = Shl_0.getOperand(1); // Val
1089 if (Sub_0.getOpcode() == ISD::Constant) {
1091 cast<ConstantSDNode>(Sub_0.getNode())->getSExtValue();
1092 if (SubConst == 0) {
1093 if (Sub_1.getOpcode() == ISD::SHL) {
1094 SDValue Shl2_0 = Sub_1.getOperand(0); // Val
1095 SDValue Shl2_1 = Sub_1.getOperand(1); // Const
1096 if (Shl2_1.getOpcode() == ISD::Constant) {
1098 cast<ConstantSDNode>(Shl_1.getNode())->getSExtValue();
1100 cast<ConstantSDNode>(Shl2_1.getNode())->getSExtValue();
1101 int32_t ValConst = 1 << (ShlConst+Shl2Const);
1102 SDValue Val = CurDAG->getTargetConstant(-ValConst, MVT::i32);
1103 if (ConstantSDNode *CN =
1104 dyn_cast<ConstantSDNode>(Val.getNode()))
1105 if (isInt<9>(CN->getSExtValue())) {
1107 CurDAG->getMachineNode(Hexagon::M2_mpysmi, dl, MVT::i32,
1109 ReplaceUses(N, Result);
1119 return SelectCode(N);
1124 // If there is an zero_extend followed an intrinsic in DAG (this means - the
1125 // result of the intrinsic is predicate); convert the zero_extend to
1126 // transfer instruction.
1128 // Zero extend -> transfer is lowered here. Otherwise, zero_extend will be
1129 // converted into a MUX as predicate registers defined as 1 bit in the
1130 // compiler. Architecture defines them as 8-bit registers.
1131 // We want to preserve all the lower 8-bits and, not just 1 LSB bit.
1133 SDNode *HexagonDAGToDAGISel::SelectZeroExtend(SDNode *N) {
1135 SDNode *IsIntrinsic = N->getOperand(0).getNode();
1136 if ((IsIntrinsic->getOpcode() == ISD::INTRINSIC_WO_CHAIN)) {
1138 cast<ConstantSDNode>(IsIntrinsic->getOperand(0))->getZExtValue();
1139 if (doesIntrinsicReturnPredicate(ID)) {
1140 // Now we need to differentiate target data types.
1141 if (N->getValueType(0) == MVT::i64) {
1142 // Convert the zero_extend to Rs = Pd followed by COMBINE_rr(0,Rs).
1143 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
1144 SDNode *Result_1 = CurDAG->getMachineNode(Hexagon::C2_tfrpr, dl,
1146 SDValue(IsIntrinsic, 0));
1147 SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A2_tfrsi, dl,
1150 SDNode *Result_3 = CurDAG->getMachineNode(Hexagon::A2_combinew, dl,
1151 MVT::i64, MVT::Other,
1152 SDValue(Result_2, 0),
1153 SDValue(Result_1, 0));
1154 ReplaceUses(N, Result_3);
1157 if (N->getValueType(0) == MVT::i32) {
1158 // Convert the zero_extend to Rs = Pd
1159 SDNode* RsPd = CurDAG->getMachineNode(Hexagon::C2_tfrpr, dl,
1161 SDValue(IsIntrinsic, 0));
1162 ReplaceUses(N, RsPd);
1165 llvm_unreachable("Unexpected value type");
1168 return SelectCode(N);
1172 // Checking for intrinsics which have predicate registers as operand(s)
1173 // and lowering to the actual intrinsic.
1175 SDNode *HexagonDAGToDAGISel::SelectIntrinsicWOChain(SDNode *N) {
1176 unsigned IID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
1179 case Intrinsic::hexagon_S2_vsplatrb:
1182 case Intrinsic::hexagon_S2_vsplatrh:
1186 return SelectCode(N);
1189 SDValue const &V = N->getOperand(1);
1191 if (isValueExtension(V, Bits, U)) {
1192 SDValue R = CurDAG->getNode(N->getOpcode(), SDLoc(N), N->getValueType(0),
1193 N->getOperand(0), U);
1194 return SelectCode(R.getNode());
1196 return SelectCode(N);
1200 // Map floating point constant values.
1202 SDNode *HexagonDAGToDAGISel::SelectConstantFP(SDNode *N) {
1204 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N);
1205 APFloat APF = CN->getValueAPF();
1206 if (N->getValueType(0) == MVT::f32) {
1207 return CurDAG->getMachineNode(Hexagon::TFRI_f, dl, MVT::f32,
1208 CurDAG->getTargetConstantFP(APF.convertToFloat(), MVT::f32));
1210 else if (N->getValueType(0) == MVT::f64) {
1211 return CurDAG->getMachineNode(Hexagon::CONST64_Float_Real, dl, MVT::f64,
1212 CurDAG->getTargetConstantFP(APF.convertToDouble(), MVT::f64));
1215 return SelectCode(N);
1220 // Map predicate true (encoded as -1 in LLVM) to a XOR.
1222 SDNode *HexagonDAGToDAGISel::SelectConstant(SDNode *N) {
1224 if (N->getValueType(0) == MVT::i1) {
1226 int32_t Val = cast<ConstantSDNode>(N)->getSExtValue();
1228 // Create the IntReg = 1 node.
1230 CurDAG->getMachineNode(Hexagon::A2_tfrsi, dl, MVT::i32,
1231 CurDAG->getTargetConstant(0, MVT::i32));
1234 SDNode* Pd = CurDAG->getMachineNode(Hexagon::C2_tfrrp, dl, MVT::i1,
1235 SDValue(IntRegTFR, 0));
1238 SDNode* NotPd = CurDAG->getMachineNode(Hexagon::C2_not, dl, MVT::i1,
1242 Result = CurDAG->getMachineNode(Hexagon::C2_xor, dl, MVT::i1,
1243 SDValue(Pd, 0), SDValue(NotPd, 0));
1245 // We have just built:
1247 // Pd = xor(not(Pd), Pd)
1249 ReplaceUses(N, Result);
1254 return SelectCode(N);
1259 // Map add followed by a asr -> asr +=.
1261 SDNode *HexagonDAGToDAGISel::SelectAdd(SDNode *N) {
1263 if (N->getValueType(0) != MVT::i32) {
1264 return SelectCode(N);
1266 // Identify nodes of the form: add(asr(...)).
1267 SDNode* Src1 = N->getOperand(0).getNode();
1268 if (Src1->getOpcode() != ISD::SRA || !Src1->hasOneUse()
1269 || Src1->getValueType(0) != MVT::i32) {
1270 return SelectCode(N);
1273 // Build Rd = Rd' + asr(Rs, Rt). The machine constraints will ensure that
1274 // Rd and Rd' are assigned to the same register
1275 SDNode* Result = CurDAG->getMachineNode(Hexagon::S2_asr_r_r_acc, dl, MVT::i32,
1277 Src1->getOperand(0),
1278 Src1->getOperand(1));
1279 ReplaceUses(N, Result);
1285 SDNode *HexagonDAGToDAGISel::Select(SDNode *N) {
1286 if (N->isMachineOpcode()) {
1288 return nullptr; // Already selected.
1292 switch (N->getOpcode()) {
1294 return SelectConstant(N);
1296 case ISD::ConstantFP:
1297 return SelectConstantFP(N);
1300 return SelectAdd(N);
1303 return SelectSHL(N);
1306 return SelectLoad(N);
1309 return SelectStore(N);
1312 return SelectSelect(N);
1315 return SelectTruncate(N);
1318 return SelectMul(N);
1320 case ISD::ZERO_EXTEND:
1321 return SelectZeroExtend(N);
1323 case ISD::INTRINSIC_WO_CHAIN:
1324 return SelectIntrinsicWOChain(N);
1327 return SelectCode(N);
1332 // Hexagon_TODO: Five functions for ADDRri?! Surely there must be a better way
1333 // to define these instructions.
1335 bool HexagonDAGToDAGISel::SelectADDRri(SDValue& Addr, SDValue &Base,
1337 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
1338 Addr.getOpcode() == ISD::TargetGlobalAddress)
1339 return false; // Direct calls.
1341 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
1342 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
1343 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1347 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1352 bool HexagonDAGToDAGISel::SelectADDRriS11_0(SDValue& Addr, SDValue &Base,
1354 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
1355 Addr.getOpcode() == ISD::TargetGlobalAddress)
1356 return false; // Direct calls.
1358 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
1359 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
1360 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1361 return (IsS11_0_Offset(Offset.getNode()));
1364 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1365 return (IsS11_0_Offset(Offset.getNode()));
1369 bool HexagonDAGToDAGISel::SelectADDRriS11_1(SDValue& Addr, SDValue &Base,
1371 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
1372 Addr.getOpcode() == ISD::TargetGlobalAddress)
1373 return false; // Direct calls.
1375 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
1376 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
1377 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1378 return (IsS11_1_Offset(Offset.getNode()));
1381 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1382 return (IsS11_1_Offset(Offset.getNode()));
1386 bool HexagonDAGToDAGISel::SelectADDRriS11_2(SDValue& Addr, SDValue &Base,
1388 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
1389 Addr.getOpcode() == ISD::TargetGlobalAddress)
1390 return false; // Direct calls.
1392 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
1393 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
1394 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1395 return (IsS11_2_Offset(Offset.getNode()));
1398 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1399 return (IsS11_2_Offset(Offset.getNode()));
1403 bool HexagonDAGToDAGISel::SelectADDRriU6_0(SDValue& Addr, SDValue &Base,
1405 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
1406 Addr.getOpcode() == ISD::TargetGlobalAddress)
1407 return false; // Direct calls.
1409 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
1410 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
1411 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1412 return (IsU6_0_Offset(Offset.getNode()));
1415 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1416 return (IsU6_0_Offset(Offset.getNode()));
1420 bool HexagonDAGToDAGISel::SelectADDRriU6_1(SDValue& Addr, SDValue &Base,
1422 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
1423 Addr.getOpcode() == ISD::TargetGlobalAddress)
1424 return false; // Direct calls.
1426 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
1427 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
1428 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1429 return (IsU6_1_Offset(Offset.getNode()));
1432 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1433 return (IsU6_1_Offset(Offset.getNode()));
1437 bool HexagonDAGToDAGISel::SelectADDRriU6_2(SDValue& Addr, SDValue &Base,
1439 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
1440 Addr.getOpcode() == ISD::TargetGlobalAddress)
1441 return false; // Direct calls.
1443 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
1444 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
1445 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1446 return (IsU6_2_Offset(Offset.getNode()));
1449 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1450 return (IsU6_2_Offset(Offset.getNode()));
1454 bool HexagonDAGToDAGISel::SelectMEMriS11_2(SDValue& Addr, SDValue &Base,
1457 if (Addr.getOpcode() != ISD::ADD) {
1458 return(SelectADDRriS11_2(Addr, Base, Offset));
1461 return SelectADDRriS11_2(Addr, Base, Offset);
1465 bool HexagonDAGToDAGISel::SelectADDRriS11_3(SDValue& Addr, SDValue &Base,
1467 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
1468 Addr.getOpcode() == ISD::TargetGlobalAddress)
1469 return false; // Direct calls.
1471 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
1472 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
1473 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1474 return (IsS11_3_Offset(Offset.getNode()));
1477 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1478 return (IsS11_3_Offset(Offset.getNode()));
1481 bool HexagonDAGToDAGISel::SelectADDRrr(SDValue &Addr, SDValue &R1,
1483 if (Addr.getOpcode() == ISD::FrameIndex) return false;
1484 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
1485 Addr.getOpcode() == ISD::TargetGlobalAddress)
1486 return false; // Direct calls.
1488 if (Addr.getOpcode() == ISD::ADD) {
1489 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
1490 if (isInt<13>(CN->getSExtValue()))
1491 return false; // Let the reg+imm pattern catch this!
1492 R1 = Addr.getOperand(0);
1493 R2 = Addr.getOperand(1);
1503 // Handle generic address case. It is accessed from inlined asm =m constraints,
1504 // which could have any kind of pointer.
1505 bool HexagonDAGToDAGISel::SelectAddr(SDNode *Op, SDValue Addr,
1506 SDValue &Base, SDValue &Offset) {
1507 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
1508 Addr.getOpcode() == ISD::TargetGlobalAddress)
1509 return false; // Direct calls.
1511 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
1512 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
1513 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1517 if (Addr.getOpcode() == ISD::ADD) {
1518 Base = Addr.getOperand(0);
1519 Offset = Addr.getOperand(1);
1524 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1529 bool HexagonDAGToDAGISel::
1530 SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
1531 std::vector<SDValue> &OutOps) {
1534 switch (ConstraintCode) {
1535 case 'o': // Offsetable.
1536 case 'v': // Not offsetable.
1537 default: return true;
1538 case 'm': // Memory.
1539 if (!SelectAddr(Op.getNode(), Op, Op0, Op1))
1544 OutOps.push_back(Op0);
1545 OutOps.push_back(Op1);
1549 bool HexagonDAGToDAGISel::isConstExtProfitable(SDNode *N) const {
1550 unsigned UseCount = 0;
1551 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
1555 return (UseCount <= 1);
1559 //===--------------------------------------------------------------------===//
1560 // Return 'true' if use count of the global address is below threshold.
1561 //===--------------------------------------------------------------------===//
1562 bool HexagonDAGToDAGISel::hasNumUsesBelowThresGA(SDNode *N) const {
1563 assert(N->getOpcode() == ISD::TargetGlobalAddress &&
1564 "Expecting a target global address");
1566 // Always try to fold the address.
1567 if (TM.getOptLevel() == CodeGenOpt::Aggressive)
1570 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
1571 DenseMap<const GlobalValue *, unsigned>::const_iterator GI =
1572 GlobalAddressUseCountMap.find(GA->getGlobal());
1574 if (GI == GlobalAddressUseCountMap.end())
1577 return GI->second <= MaxNumOfUsesForConstExtenders;
1580 //===--------------------------------------------------------------------===//
1581 // Return true if the non-GP-relative global address can be folded.
1582 //===--------------------------------------------------------------------===//
1583 inline bool HexagonDAGToDAGISel::foldGlobalAddress(SDValue &N, SDValue &R) {
1584 return foldGlobalAddressImpl(N, R, false);
1587 //===--------------------------------------------------------------------===//
1588 // Return true if the GP-relative global address can be folded.
1589 //===--------------------------------------------------------------------===//
1590 inline bool HexagonDAGToDAGISel::foldGlobalAddressGP(SDValue &N, SDValue &R) {
1591 return foldGlobalAddressImpl(N, R, true);
1594 //===--------------------------------------------------------------------===//
1595 // Fold offset of the global address if number of uses are below threshold.
1596 //===--------------------------------------------------------------------===//
1597 bool HexagonDAGToDAGISel::foldGlobalAddressImpl(SDValue &N, SDValue &R,
1598 bool ShouldLookForGP) {
1599 if (N.getOpcode() == ISD::ADD) {
1600 SDValue N0 = N.getOperand(0);
1601 SDValue N1 = N.getOperand(1);
1602 if ((ShouldLookForGP && (N0.getOpcode() == HexagonISD::CONST32_GP)) ||
1603 (!ShouldLookForGP && (N0.getOpcode() == HexagonISD::CONST32))) {
1604 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N1);
1605 GlobalAddressSDNode *GA =
1606 dyn_cast<GlobalAddressSDNode>(N0.getOperand(0));
1609 (GA->getOpcode() == ISD::TargetGlobalAddress)) {
1610 if ((N0.getOpcode() == HexagonISD::CONST32) &&
1611 !hasNumUsesBelowThresGA(GA))
1613 R = CurDAG->getTargetGlobalAddress(GA->getGlobal(),
1617 (uint64_t)Const->getSExtValue());
1625 bool HexagonDAGToDAGISel::SelectAddrFI(SDValue& N, SDValue &R) {
1626 if (N.getOpcode() != ISD::FrameIndex)
1628 FrameIndexSDNode *FX = cast<FrameIndexSDNode>(N);
1629 R = CurDAG->getTargetFrameIndex(FX->getIndex(), MVT::i32);
1633 bool HexagonDAGToDAGISel::isValueExtension(SDValue const &Val,
1634 unsigned FromBits, SDValue &Src) {
1635 unsigned Opc = Val.getOpcode();
1637 case ISD::SIGN_EXTEND:
1638 case ISD::ZERO_EXTEND:
1639 case ISD::ANY_EXTEND: {
1640 SDValue const &Op0 = Val.getOperand(0);
1641 EVT T = Op0.getValueType();
1642 if (T.isInteger() && T.getSizeInBits() == FromBits) {
1648 case ISD::SIGN_EXTEND_INREG:
1649 case ISD::AssertSext:
1650 case ISD::AssertZext:
1651 if (Val.getOperand(0).getValueType().isInteger()) {
1652 VTSDNode *T = cast<VTSDNode>(Val.getOperand(1));
1653 if (T->getVT().getSizeInBits() == FromBits) {
1654 Src = Val.getOperand(0);
1660 // Check if this is an AND with "FromBits" of lower bits set to 1.
1661 uint64_t FromMask = (1 << FromBits) - 1;
1662 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(0))) {
1663 if (C->getZExtValue() == FromMask) {
1664 Src = Val.getOperand(1);
1668 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(1))) {
1669 if (C->getZExtValue() == FromMask) {
1670 Src = Val.getOperand(0);
1678 // OR/XOR with the lower "FromBits" bits set to 0.
1679 uint64_t FromMask = (1 << FromBits) - 1;
1680 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(0))) {
1681 if ((C->getZExtValue() & FromMask) == 0) {
1682 Src = Val.getOperand(1);
1686 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(1))) {
1687 if ((C->getZExtValue() & FromMask) == 0) {
1688 Src = Val.getOperand(0);