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 "HexagonMachineFunctionInfo.h"
17 #include "HexagonTargetMachine.h"
18 #include "llvm/CodeGen/FunctionLoweringInfo.h"
19 #include "llvm/CodeGen/MachineInstrBuilder.h"
20 #include "llvm/CodeGen/SelectionDAGISel.h"
21 #include "llvm/IR/Intrinsics.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/Debug.h"
26 #define DEBUG_TYPE "hexagon-isel"
30 MaxNumOfUsesForConstExtenders("ga-max-num-uses-for-constant-extenders",
31 cl::Hidden, cl::init(2),
32 cl::desc("Maximum number of uses of a global address such that we still us a"
33 "constant extended instruction"));
35 //===----------------------------------------------------------------------===//
36 // Instruction Selector Implementation
37 //===----------------------------------------------------------------------===//
39 //===--------------------------------------------------------------------===//
40 /// HexagonDAGToDAGISel - Hexagon specific code to select Hexagon machine
41 /// instructions for SelectionDAG operations.
44 class HexagonDAGToDAGISel : public SelectionDAGISel {
45 const HexagonTargetMachine &HTM;
46 const HexagonSubtarget *HST;
47 const HexagonInstrInfo *HII;
48 const HexagonRegisterInfo *HRI;
50 explicit HexagonDAGToDAGISel(HexagonTargetMachine &tm,
51 CodeGenOpt::Level OptLevel)
52 : SelectionDAGISel(tm, OptLevel), HTM(tm), HST(nullptr), HII(nullptr),
55 bool runOnMachineFunction(MachineFunction &MF) override {
56 // Reset the subtarget each time through.
57 HST = &MF.getSubtarget<HexagonSubtarget>();
58 HII = HST->getInstrInfo();
59 HRI = HST->getRegisterInfo();
60 SelectionDAGISel::runOnMachineFunction(MF);
64 virtual void PreprocessISelDAG() override;
65 virtual void EmitFunctionEntryCode() override;
67 void Select(SDNode *N) override;
69 // Complex Pattern Selectors.
70 inline bool SelectAddrGA(SDValue &N, SDValue &R);
71 inline bool SelectAddrGP(SDValue &N, SDValue &R);
72 bool SelectGlobalAddress(SDValue &N, SDValue &R, bool UseGP);
73 bool SelectAddrFI(SDValue &N, SDValue &R);
75 const char *getPassName() const override {
76 return "Hexagon DAG->DAG Pattern Instruction Selection";
79 // Generate a machine instruction node corresponding to the circ/brev
81 MachineSDNode *LoadInstrForLoadIntrinsic(SDNode *IntN);
82 // Given the circ/brev load intrinsic and the already generated machine
83 // instruction, generate the appropriate store (that is a part of the
84 // intrinsic's functionality).
85 SDNode *StoreInstrForLoadIntrinsic(MachineSDNode *LoadN, SDNode *IntN);
87 void SelectFrameIndex(SDNode *N);
88 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
89 /// inline asm expressions.
90 bool SelectInlineAsmMemoryOperand(const SDValue &Op,
91 unsigned ConstraintID,
92 std::vector<SDValue> &OutOps) override;
93 bool tryLoadOfLoadIntrinsic(LoadSDNode *N);
94 void SelectLoad(SDNode *N);
95 void SelectBaseOffsetLoad(LoadSDNode *LD, SDLoc dl);
96 void SelectIndexedLoad(LoadSDNode *LD, const SDLoc &dl);
97 void SelectIndexedStore(StoreSDNode *ST, const SDLoc &dl);
98 void SelectStore(SDNode *N);
99 void SelectSHL(SDNode *N);
100 void SelectMul(SDNode *N);
101 void SelectZeroExtend(SDNode *N);
102 void SelectIntrinsicWChain(SDNode *N);
103 void SelectIntrinsicWOChain(SDNode *N);
104 void SelectConstant(SDNode *N);
105 void SelectConstantFP(SDNode *N);
106 void SelectAdd(SDNode *N);
107 void SelectBitcast(SDNode *N);
108 void SelectBitOp(SDNode *N);
110 // XformMskToBitPosU5Imm - Returns the bit position which
111 // the single bit 32 bit mask represents.
112 // Used in Clr and Set bit immediate memops.
113 SDValue XformMskToBitPosU5Imm(uint32_t Imm, const SDLoc &DL) {
115 bitPos = Log2_32(Imm);
116 assert(bitPos >= 0 && bitPos < 32 &&
117 "Constant out of range for 32 BitPos Memops");
118 return CurDAG->getTargetConstant(bitPos, DL, MVT::i32);
121 // XformMskToBitPosU4Imm - Returns the bit position which the single-bit
122 // 16 bit mask represents. Used in Clr and Set bit immediate memops.
123 SDValue XformMskToBitPosU4Imm(uint16_t Imm, const SDLoc &DL) {
124 return XformMskToBitPosU5Imm(Imm, DL);
127 // XformMskToBitPosU3Imm - Returns the bit position which the single-bit
128 // 8 bit mask represents. Used in Clr and Set bit immediate memops.
129 SDValue XformMskToBitPosU3Imm(uint8_t Imm, const SDLoc &DL) {
130 return XformMskToBitPosU5Imm(Imm, DL);
133 // Return true if there is exactly one bit set in V, i.e., if V is one of the
134 // following integers: 2^0, 2^1, ..., 2^31.
135 bool ImmIsSingleBit(uint32_t v) const {
136 return isPowerOf2_32(v);
139 // XformM5ToU5Imm - Return a target constant with the specified value, of
140 // type i32 where the negative literal is transformed into a positive literal
141 // for use in -= memops.
142 inline SDValue XformM5ToU5Imm(signed Imm, const SDLoc &DL) {
143 assert((Imm >= -31 && Imm <= -1) && "Constant out of range for Memops");
144 return CurDAG->getTargetConstant(-Imm, DL, MVT::i32);
147 // XformU7ToU7M1Imm - Return a target constant decremented by 1, in range
148 // [1..128], used in cmpb.gtu instructions.
149 inline SDValue XformU7ToU7M1Imm(signed Imm, const SDLoc &DL) {
150 assert((Imm >= 1 && Imm <= 128) && "Constant out of range for cmpb op");
151 return CurDAG->getTargetConstant(Imm - 1, DL, MVT::i8);
154 // XformS8ToS8M1Imm - Return a target constant decremented by 1.
155 inline SDValue XformSToSM1Imm(signed Imm, const SDLoc &DL) {
156 return CurDAG->getTargetConstant(Imm - 1, DL, MVT::i32);
159 // XformU8ToU8M1Imm - Return a target constant decremented by 1.
160 inline SDValue XformUToUM1Imm(unsigned Imm, const SDLoc &DL) {
161 assert((Imm >= 1) && "Cannot decrement unsigned int less than 1");
162 return CurDAG->getTargetConstant(Imm - 1, DL, MVT::i32);
165 // XformSToSM2Imm - Return a target constant decremented by 2.
166 inline SDValue XformSToSM2Imm(unsigned Imm, const SDLoc &DL) {
167 return CurDAG->getTargetConstant(Imm - 2, DL, MVT::i32);
170 // XformSToSM3Imm - Return a target constant decremented by 3.
171 inline SDValue XformSToSM3Imm(unsigned Imm, const SDLoc &DL) {
172 return CurDAG->getTargetConstant(Imm - 3, DL, MVT::i32);
175 // Include the pieces autogenerated from the target description.
176 #include "HexagonGenDAGISel.inc"
179 bool isValueExtension(const SDValue &Val, unsigned FromBits, SDValue &Src);
180 bool orIsAdd(const SDNode *N) const;
181 bool isAlignedMemNode(const MemSDNode *N) const;
182 }; // end HexagonDAGToDAGISel
183 } // end anonymous namespace
186 /// createHexagonISelDag - This pass converts a legalized DAG into a
187 /// Hexagon-specific DAG, ready for instruction scheduling.
190 FunctionPass *createHexagonISelDag(HexagonTargetMachine &TM,
191 CodeGenOpt::Level OptLevel) {
192 return new HexagonDAGToDAGISel(TM, OptLevel);
196 // Intrinsics that return a a predicate.
197 static bool doesIntrinsicReturnPredicate(unsigned ID) {
201 case Intrinsic::hexagon_C2_cmpeq:
202 case Intrinsic::hexagon_C2_cmpgt:
203 case Intrinsic::hexagon_C2_cmpgtu:
204 case Intrinsic::hexagon_C2_cmpgtup:
205 case Intrinsic::hexagon_C2_cmpgtp:
206 case Intrinsic::hexagon_C2_cmpeqp:
207 case Intrinsic::hexagon_C2_bitsset:
208 case Intrinsic::hexagon_C2_bitsclr:
209 case Intrinsic::hexagon_C2_cmpeqi:
210 case Intrinsic::hexagon_C2_cmpgti:
211 case Intrinsic::hexagon_C2_cmpgtui:
212 case Intrinsic::hexagon_C2_cmpgei:
213 case Intrinsic::hexagon_C2_cmpgeui:
214 case Intrinsic::hexagon_C2_cmplt:
215 case Intrinsic::hexagon_C2_cmpltu:
216 case Intrinsic::hexagon_C2_bitsclri:
217 case Intrinsic::hexagon_C2_and:
218 case Intrinsic::hexagon_C2_or:
219 case Intrinsic::hexagon_C2_xor:
220 case Intrinsic::hexagon_C2_andn:
221 case Intrinsic::hexagon_C2_not:
222 case Intrinsic::hexagon_C2_orn:
223 case Intrinsic::hexagon_C2_pxfer_map:
224 case Intrinsic::hexagon_C2_any8:
225 case Intrinsic::hexagon_C2_all8:
226 case Intrinsic::hexagon_A2_vcmpbeq:
227 case Intrinsic::hexagon_A2_vcmpbgtu:
228 case Intrinsic::hexagon_A2_vcmpheq:
229 case Intrinsic::hexagon_A2_vcmphgt:
230 case Intrinsic::hexagon_A2_vcmphgtu:
231 case Intrinsic::hexagon_A2_vcmpweq:
232 case Intrinsic::hexagon_A2_vcmpwgt:
233 case Intrinsic::hexagon_A2_vcmpwgtu:
234 case Intrinsic::hexagon_C2_tfrrp:
235 case Intrinsic::hexagon_S2_tstbit_i:
236 case Intrinsic::hexagon_S2_tstbit_r:
241 void HexagonDAGToDAGISel::SelectIndexedLoad(LoadSDNode *LD, const SDLoc &dl) {
242 SDValue Chain = LD->getChain();
243 SDValue Base = LD->getBasePtr();
244 SDValue Offset = LD->getOffset();
245 int32_t Inc = cast<ConstantSDNode>(Offset.getNode())->getSExtValue();
246 EVT LoadedVT = LD->getMemoryVT();
249 // Check for zero extended loads. Treat any-extend loads as zero extended
251 ISD::LoadExtType ExtType = LD->getExtensionType();
252 bool IsZeroExt = (ExtType == ISD::ZEXTLOAD || ExtType == ISD::EXTLOAD);
253 bool IsValidInc = HII->isValidAutoIncImm(LoadedVT, Inc);
255 assert(LoadedVT.isSimple());
256 switch (LoadedVT.getSimpleVT().SimpleTy) {
259 Opcode = IsValidInc ? Hexagon::L2_loadrub_pi : Hexagon::L2_loadrub_io;
261 Opcode = IsValidInc ? Hexagon::L2_loadrb_pi : Hexagon::L2_loadrb_io;
265 Opcode = IsValidInc ? Hexagon::L2_loadruh_pi : Hexagon::L2_loadruh_io;
267 Opcode = IsValidInc ? Hexagon::L2_loadrh_pi : Hexagon::L2_loadrh_io;
270 Opcode = IsValidInc ? Hexagon::L2_loadri_pi : Hexagon::L2_loadri_io;
273 Opcode = IsValidInc ? Hexagon::L2_loadrd_pi : Hexagon::L2_loadrd_io;
280 if (isAlignedMemNode(LD))
281 Opcode = IsValidInc ? Hexagon::V6_vL32b_pi : Hexagon::V6_vL32b_ai;
283 Opcode = IsValidInc ? Hexagon::V6_vL32Ub_pi : Hexagon::V6_vL32Ub_ai;
290 if (isAlignedMemNode(LD))
291 Opcode = IsValidInc ? Hexagon::V6_vL32b_pi_128B
292 : Hexagon::V6_vL32b_ai_128B;
294 Opcode = IsValidInc ? Hexagon::V6_vL32Ub_pi_128B
295 : Hexagon::V6_vL32Ub_ai_128B;
298 llvm_unreachable("Unexpected memory type in indexed load");
301 SDValue IncV = CurDAG->getTargetConstant(Inc, dl, MVT::i32);
302 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
303 MemOp[0] = LD->getMemOperand();
305 auto getExt64 = [this,ExtType] (MachineSDNode *N, const SDLoc &dl)
307 if (ExtType == ISD::ZEXTLOAD || ExtType == ISD::EXTLOAD) {
308 SDValue Zero = CurDAG->getTargetConstant(0, dl, MVT::i32);
309 return CurDAG->getMachineNode(Hexagon::A4_combineir, dl, MVT::i64,
310 Zero, SDValue(N, 0));
312 if (ExtType == ISD::SEXTLOAD)
313 return CurDAG->getMachineNode(Hexagon::A2_sxtw, dl, MVT::i64,
318 // Loaded value Next address Chain
319 SDValue From[3] = { SDValue(LD,0), SDValue(LD,1), SDValue(LD,2) };
322 EVT ValueVT = LD->getValueType(0);
323 if (ValueVT == MVT::i64 && ExtType != ISD::NON_EXTLOAD) {
324 // A load extending to i64 will actually produce i32, which will then
325 // need to be extended to i64.
326 assert(LoadedVT.getSizeInBits() <= 32);
331 MachineSDNode *L = CurDAG->getMachineNode(Opcode, dl, ValueVT,
332 MVT::i32, MVT::Other, Base,
334 L->setMemRefs(MemOp, MemOp+1);
335 To[1] = SDValue(L, 1); // Next address.
336 To[2] = SDValue(L, 2); // Chain.
337 // Handle special case for extension to i64.
338 if (LD->getValueType(0) == MVT::i64)
340 To[0] = SDValue(L, 0); // Loaded (extended) value.
342 SDValue Zero = CurDAG->getTargetConstant(0, dl, MVT::i32);
343 MachineSDNode *L = CurDAG->getMachineNode(Opcode, dl, ValueVT, MVT::Other,
345 L->setMemRefs(MemOp, MemOp+1);
346 To[2] = SDValue(L, 1); // Chain.
347 MachineSDNode *A = CurDAG->getMachineNode(Hexagon::A2_addi, dl, MVT::i32,
349 To[1] = SDValue(A, 0); // Next address.
350 // Handle special case for extension to i64.
351 if (LD->getValueType(0) == MVT::i64)
353 To[0] = SDValue(L, 0); // Loaded (extended) value.
355 ReplaceUses(From, To, 3);
356 CurDAG->RemoveDeadNode(LD);
360 MachineSDNode *HexagonDAGToDAGISel::LoadInstrForLoadIntrinsic(SDNode *IntN) {
361 if (IntN->getOpcode() != ISD::INTRINSIC_W_CHAIN)
365 unsigned IntNo = cast<ConstantSDNode>(IntN->getOperand(1))->getZExtValue();
367 static std::map<unsigned,unsigned> LoadPciMap = {
368 { Intrinsic::hexagon_circ_ldb, Hexagon::L2_loadrb_pci },
369 { Intrinsic::hexagon_circ_ldub, Hexagon::L2_loadrub_pci },
370 { Intrinsic::hexagon_circ_ldh, Hexagon::L2_loadrh_pci },
371 { Intrinsic::hexagon_circ_lduh, Hexagon::L2_loadruh_pci },
372 { Intrinsic::hexagon_circ_ldw, Hexagon::L2_loadri_pci },
373 { Intrinsic::hexagon_circ_ldd, Hexagon::L2_loadrd_pci },
375 auto FLC = LoadPciMap.find(IntNo);
376 if (FLC != LoadPciMap.end()) {
377 SDNode *Mod = CurDAG->getMachineNode(Hexagon::A2_tfrrcr, dl, MVT::i32,
378 IntN->getOperand(4));
379 EVT ValTy = (IntNo == Intrinsic::hexagon_circ_ldd) ? MVT::i64 : MVT::i32;
380 EVT RTys[] = { ValTy, MVT::i32, MVT::Other };
381 // Operands: { Base, Increment, Modifier, Chain }
382 auto Inc = cast<ConstantSDNode>(IntN->getOperand(5));
383 SDValue I = CurDAG->getTargetConstant(Inc->getSExtValue(), dl, MVT::i32);
384 MachineSDNode *Res = CurDAG->getMachineNode(FLC->second, dl, RTys,
385 { IntN->getOperand(2), I, SDValue(Mod,0), IntN->getOperand(0) });
389 static std::map<unsigned,unsigned> LoadPbrMap = {
390 { Intrinsic::hexagon_brev_ldb, Hexagon::L2_loadrb_pbr },
391 { Intrinsic::hexagon_brev_ldub, Hexagon::L2_loadrub_pbr },
392 { Intrinsic::hexagon_brev_ldh, Hexagon::L2_loadrh_pbr },
393 { Intrinsic::hexagon_brev_lduh, Hexagon::L2_loadruh_pbr },
394 { Intrinsic::hexagon_brev_ldw, Hexagon::L2_loadri_pbr },
395 { Intrinsic::hexagon_brev_ldd, Hexagon::L2_loadrd_pbr },
397 auto FLB = LoadPbrMap.find(IntNo);
398 if (FLB != LoadPbrMap.end()) {
399 SDNode *Mod = CurDAG->getMachineNode(Hexagon::A2_tfrrcr, dl, MVT::i32,
400 IntN->getOperand(4));
401 EVT ValTy = (IntNo == Intrinsic::hexagon_brev_ldd) ? MVT::i64 : MVT::i32;
402 EVT RTys[] = { ValTy, MVT::i32, MVT::Other };
403 // Operands: { Base, Modifier, Chain }
404 MachineSDNode *Res = CurDAG->getMachineNode(FLB->second, dl, RTys,
405 { IntN->getOperand(2), SDValue(Mod,0), IntN->getOperand(0) });
412 SDNode *HexagonDAGToDAGISel::StoreInstrForLoadIntrinsic(MachineSDNode *LoadN,
414 // The "LoadN" is just a machine load instruction. The intrinsic also
415 // involves storing it. Generate an appropriate store to the location
416 // given in the intrinsic's operand(3).
417 uint64_t F = HII->get(LoadN->getMachineOpcode()).TSFlags;
418 unsigned SizeBits = (F >> HexagonII::MemAccessSizePos) &
419 HexagonII::MemAccesSizeMask;
420 unsigned Size = 1U << (SizeBits-1);
423 MachinePointerInfo PI;
425 SDValue Loc = IntN->getOperand(3);
428 TS = CurDAG->getStore(SDValue(LoadN, 2), dl, SDValue(LoadN, 0), Loc, PI,
431 TS = CurDAG->getTruncStore(SDValue(LoadN, 2), dl, SDValue(LoadN, 0), Loc,
432 PI, MVT::getIntegerVT(Size * 8), Size);
436 HandleSDNode Handle(TS);
437 SelectStore(TS.getNode());
438 StoreN = Handle.getValue().getNode();
441 // Load's results are { Loaded value, Updated pointer, Chain }
442 ReplaceUses(SDValue(IntN, 0), SDValue(LoadN, 1));
443 ReplaceUses(SDValue(IntN, 1), SDValue(StoreN, 0));
447 bool HexagonDAGToDAGISel::tryLoadOfLoadIntrinsic(LoadSDNode *N) {
448 // The intrinsics for load circ/brev perform two operations:
449 // 1. Load a value V from the specified location, using the addressing
450 // mode corresponding to the intrinsic.
451 // 2. Store V into a specified location. This location is typically a
452 // local, temporary object.
453 // In many cases, the program using these intrinsics will immediately
454 // load V again from the local object. In those cases, when certain
455 // conditions are met, the last load can be removed.
456 // This function identifies and optimizes this pattern. If the pattern
457 // cannot be optimized, it returns nullptr, which will cause the load
458 // to be selected separately from the intrinsic (which will be handled
459 // in SelectIntrinsicWChain).
461 SDValue Ch = N->getOperand(0);
462 SDValue Loc = N->getOperand(1);
464 // Assume that the load and the intrinsic are connected directly with a
466 // t1: i32,ch = int.load ..., ..., ..., Loc, ... // <-- C
467 // t2: i32,ch = load t1:1, Loc, ...
468 SDNode *C = Ch.getNode();
470 if (C->getOpcode() != ISD::INTRINSIC_W_CHAIN)
473 // The second load can only be eliminated if its extension type matches
474 // that of the load instruction corresponding to the intrinsic. The user
475 // can provide an address of an unsigned variable to store the result of
476 // a sign-extending intrinsic into (or the other way around).
477 ISD::LoadExtType IntExt;
478 switch (cast<ConstantSDNode>(C->getOperand(1))->getZExtValue()) {
479 case Intrinsic::hexagon_brev_ldub:
480 case Intrinsic::hexagon_brev_lduh:
481 case Intrinsic::hexagon_circ_ldub:
482 case Intrinsic::hexagon_circ_lduh:
483 IntExt = ISD::ZEXTLOAD;
485 case Intrinsic::hexagon_brev_ldw:
486 case Intrinsic::hexagon_brev_ldd:
487 case Intrinsic::hexagon_circ_ldw:
488 case Intrinsic::hexagon_circ_ldd:
489 IntExt = ISD::NON_EXTLOAD;
492 IntExt = ISD::SEXTLOAD;
495 if (N->getExtensionType() != IntExt)
498 // Make sure the target location for the loaded value in the load intrinsic
499 // is the location from which LD (or N) is loading.
500 if (C->getNumOperands() < 4 || Loc.getNode() != C->getOperand(3).getNode())
503 if (MachineSDNode *L = LoadInstrForLoadIntrinsic(C)) {
504 SDNode *S = StoreInstrForLoadIntrinsic(L, C);
505 SDValue F[] = { SDValue(N,0), SDValue(N,1), SDValue(C,0), SDValue(C,1) };
506 SDValue T[] = { SDValue(L,0), SDValue(S,0), SDValue(L,1), SDValue(S,0) };
507 ReplaceUses(F, T, array_lengthof(T));
508 // This transformation will leave the intrinsic dead. If it remains in
509 // the DAG, the selection code will see it again, but without the load,
510 // and it will generate a store that is normally required for it.
511 CurDAG->RemoveDeadNode(C);
518 void HexagonDAGToDAGISel::SelectLoad(SDNode *N) {
520 LoadSDNode *LD = cast<LoadSDNode>(N);
521 ISD::MemIndexedMode AM = LD->getAddressingMode();
523 // Handle indexed loads.
524 if (AM != ISD::UNINDEXED) {
525 SelectIndexedLoad(LD, dl);
529 // Handle patterns using circ/brev load intrinsics.
530 if (tryLoadOfLoadIntrinsic(LD))
536 void HexagonDAGToDAGISel::SelectIndexedStore(StoreSDNode *ST, const SDLoc &dl) {
537 SDValue Chain = ST->getChain();
538 SDValue Base = ST->getBasePtr();
539 SDValue Offset = ST->getOffset();
540 SDValue Value = ST->getValue();
541 // Get the constant value.
542 int32_t Inc = cast<ConstantSDNode>(Offset.getNode())->getSExtValue();
543 EVT StoredVT = ST->getMemoryVT();
544 EVT ValueVT = Value.getValueType();
546 bool IsValidInc = HII->isValidAutoIncImm(StoredVT, Inc);
549 assert(StoredVT.isSimple());
550 switch (StoredVT.getSimpleVT().SimpleTy) {
552 Opcode = IsValidInc ? Hexagon::S2_storerb_pi : Hexagon::S2_storerb_io;
555 Opcode = IsValidInc ? Hexagon::S2_storerh_pi : Hexagon::S2_storerh_io;
558 Opcode = IsValidInc ? Hexagon::S2_storeri_pi : Hexagon::S2_storeri_io;
561 Opcode = IsValidInc ? Hexagon::S2_storerd_pi : Hexagon::S2_storerd_io;
568 if (isAlignedMemNode(ST))
569 Opcode = IsValidInc ? Hexagon::V6_vS32b_pi : Hexagon::V6_vS32b_ai;
571 Opcode = IsValidInc ? Hexagon::V6_vS32Ub_pi : Hexagon::V6_vS32Ub_ai;
578 if (isAlignedMemNode(ST))
579 Opcode = IsValidInc ? Hexagon::V6_vS32b_pi_128B
580 : Hexagon::V6_vS32b_ai_128B;
582 Opcode = IsValidInc ? Hexagon::V6_vS32Ub_pi_128B
583 : Hexagon::V6_vS32Ub_ai_128B;
586 llvm_unreachable("Unexpected memory type in indexed store");
589 if (ST->isTruncatingStore() && ValueVT.getSizeInBits() == 64) {
590 assert(StoredVT.getSizeInBits() < 64 && "Not a truncating store");
591 Value = CurDAG->getTargetExtractSubreg(Hexagon::subreg_loreg,
592 dl, MVT::i32, Value);
595 SDValue IncV = CurDAG->getTargetConstant(Inc, dl, MVT::i32);
596 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
597 MemOp[0] = ST->getMemOperand();
599 // Next address Chain
600 SDValue From[2] = { SDValue(ST,0), SDValue(ST,1) };
604 // Build post increment store.
605 SDValue Ops[] = { Base, IncV, Value, Chain };
606 MachineSDNode *S = CurDAG->getMachineNode(Opcode, dl, MVT::i32, MVT::Other,
608 S->setMemRefs(MemOp, MemOp + 1);
609 To[0] = SDValue(S, 0);
610 To[1] = SDValue(S, 1);
612 SDValue Zero = CurDAG->getTargetConstant(0, dl, MVT::i32);
613 SDValue Ops[] = { Base, Zero, Value, Chain };
614 MachineSDNode *S = CurDAG->getMachineNode(Opcode, dl, MVT::Other, Ops);
615 S->setMemRefs(MemOp, MemOp + 1);
616 To[1] = SDValue(S, 0);
617 MachineSDNode *A = CurDAG->getMachineNode(Hexagon::A2_addi, dl, MVT::i32,
619 To[0] = SDValue(A, 0);
622 ReplaceUses(From, To, 2);
623 CurDAG->RemoveDeadNode(ST);
626 void HexagonDAGToDAGISel::SelectStore(SDNode *N) {
628 StoreSDNode *ST = cast<StoreSDNode>(N);
629 ISD::MemIndexedMode AM = ST->getAddressingMode();
631 // Handle indexed stores.
632 if (AM != ISD::UNINDEXED) {
633 SelectIndexedStore(ST, dl);
640 void HexagonDAGToDAGISel::SelectMul(SDNode *N) {
644 // %conv.i = sext i32 %tmp1 to i64
645 // %conv2.i = sext i32 %add to i64
646 // %mul.i = mul nsw i64 %conv2.i, %conv.i
648 // --- match with the following ---
650 // %mul.i = mpy (%tmp1, %add)
653 if (N->getValueType(0) == MVT::i64) {
654 // Shifting a i64 signed multiply.
655 SDValue MulOp0 = N->getOperand(0);
656 SDValue MulOp1 = N->getOperand(1);
661 // Handle sign_extend and sextload.
662 if (MulOp0.getOpcode() == ISD::SIGN_EXTEND) {
663 SDValue Sext0 = MulOp0.getOperand(0);
664 if (Sext0.getNode()->getValueType(0) != MVT::i32) {
670 } else if (MulOp0.getOpcode() == ISD::LOAD) {
671 LoadSDNode *LD = cast<LoadSDNode>(MulOp0.getNode());
672 if (LD->getMemoryVT() != MVT::i32 ||
673 LD->getExtensionType() != ISD::SEXTLOAD ||
674 LD->getAddressingMode() != ISD::UNINDEXED) {
679 SDValue Chain = LD->getChain();
680 SDValue TargetConst0 = CurDAG->getTargetConstant(0, dl, MVT::i32);
681 OP0 = SDValue(CurDAG->getMachineNode(Hexagon::L2_loadri_io, dl, MVT::i32,
683 LD->getBasePtr(), TargetConst0,
690 // Same goes for the second operand.
691 if (MulOp1.getOpcode() == ISD::SIGN_EXTEND) {
692 SDValue Sext1 = MulOp1.getOperand(0);
693 if (Sext1.getNode()->getValueType(0) != MVT::i32) {
699 } else if (MulOp1.getOpcode() == ISD::LOAD) {
700 LoadSDNode *LD = cast<LoadSDNode>(MulOp1.getNode());
701 if (LD->getMemoryVT() != MVT::i32 ||
702 LD->getExtensionType() != ISD::SEXTLOAD ||
703 LD->getAddressingMode() != ISD::UNINDEXED) {
708 SDValue Chain = LD->getChain();
709 SDValue TargetConst0 = CurDAG->getTargetConstant(0, dl, MVT::i32);
710 OP1 = SDValue(CurDAG->getMachineNode(Hexagon::L2_loadri_io, dl, MVT::i32,
712 LD->getBasePtr(), TargetConst0,
719 // Generate a mpy instruction.
720 SDNode *Result = CurDAG->getMachineNode(Hexagon::M2_dpmpyss_s0, dl, MVT::i64,
722 ReplaceNode(N, Result);
729 void HexagonDAGToDAGISel::SelectSHL(SDNode *N) {
731 if (N->getValueType(0) == MVT::i32) {
732 SDValue Shl_0 = N->getOperand(0);
733 SDValue Shl_1 = N->getOperand(1);
735 if (Shl_1.getOpcode() == ISD::Constant) {
736 if (Shl_0.getOpcode() == ISD::MUL) {
737 SDValue Mul_0 = Shl_0.getOperand(0); // Val
738 SDValue Mul_1 = Shl_0.getOperand(1); // Const
739 // RHS of mul is const.
740 if (Mul_1.getOpcode() == ISD::Constant) {
742 cast<ConstantSDNode>(Shl_1.getNode())->getSExtValue();
744 cast<ConstantSDNode>(Mul_1.getNode())->getSExtValue();
745 int32_t ValConst = MulConst << ShlConst;
746 SDValue Val = CurDAG->getTargetConstant(ValConst, dl,
748 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Val.getNode()))
749 if (isInt<9>(CN->getSExtValue())) {
751 CurDAG->getMachineNode(Hexagon::M2_mpysmi, dl,
752 MVT::i32, Mul_0, Val);
753 ReplaceNode(N, Result);
758 } else if (Shl_0.getOpcode() == ISD::SUB) {
759 SDValue Sub_0 = Shl_0.getOperand(0); // Const 0
760 SDValue Sub_1 = Shl_0.getOperand(1); // Val
761 if (Sub_0.getOpcode() == ISD::Constant) {
763 cast<ConstantSDNode>(Sub_0.getNode())->getSExtValue();
765 if (Sub_1.getOpcode() == ISD::SHL) {
766 SDValue Shl2_0 = Sub_1.getOperand(0); // Val
767 SDValue Shl2_1 = Sub_1.getOperand(1); // Const
768 if (Shl2_1.getOpcode() == ISD::Constant) {
770 cast<ConstantSDNode>(Shl_1.getNode())->getSExtValue();
772 cast<ConstantSDNode>(Shl2_1.getNode())->getSExtValue();
773 int32_t ValConst = 1 << (ShlConst+Shl2Const);
774 SDValue Val = CurDAG->getTargetConstant(-ValConst, dl,
776 if (ConstantSDNode *CN =
777 dyn_cast<ConstantSDNode>(Val.getNode()))
778 if (isInt<9>(CN->getSExtValue())) {
780 CurDAG->getMachineNode(Hexagon::M2_mpysmi, dl, MVT::i32,
782 ReplaceNode(N, Result);
797 // If there is an zero_extend followed an intrinsic in DAG (this means - the
798 // result of the intrinsic is predicate); convert the zero_extend to
799 // transfer instruction.
801 // Zero extend -> transfer is lowered here. Otherwise, zero_extend will be
802 // converted into a MUX as predicate registers defined as 1 bit in the
803 // compiler. Architecture defines them as 8-bit registers.
804 // We want to preserve all the lower 8-bits and, not just 1 LSB bit.
806 void HexagonDAGToDAGISel::SelectZeroExtend(SDNode *N) {
809 SDValue Op0 = N->getOperand(0);
810 EVT OpVT = Op0.getValueType();
811 unsigned OpBW = OpVT.getSizeInBits();
813 // Special handling for zero-extending a vector of booleans.
814 if (OpVT.isVector() && OpVT.getVectorElementType() == MVT::i1 && OpBW <= 64) {
815 SDNode *Mask = CurDAG->getMachineNode(Hexagon::C2_mask, dl, MVT::i64, Op0);
816 unsigned NE = OpVT.getVectorNumElements();
817 EVT ExVT = N->getValueType(0);
818 unsigned ES = ExVT.getVectorElementType().getSizeInBits();
819 uint64_t MV = 0, Bit = 1;
820 for (unsigned i = 0; i < NE; ++i) {
824 SDValue Ones = CurDAG->getTargetConstant(MV, dl, MVT::i64);
825 SDNode *OnesReg = CurDAG->getMachineNode(Hexagon::CONST64_Int_Real, dl,
827 if (ExVT.getSizeInBits() == 32) {
828 SDNode *And = CurDAG->getMachineNode(Hexagon::A2_andp, dl, MVT::i64,
829 SDValue(Mask,0), SDValue(OnesReg,0));
830 SDValue SubR = CurDAG->getTargetConstant(Hexagon::subreg_loreg, dl,
832 ReplaceNode(N, CurDAG->getMachineNode(Hexagon::EXTRACT_SUBREG, dl, ExVT,
833 SDValue(And, 0), SubR));
837 CurDAG->getMachineNode(Hexagon::A2_andp, dl, ExVT,
838 SDValue(Mask, 0), SDValue(OnesReg, 0)));
842 SDNode *IsIntrinsic = N->getOperand(0).getNode();
843 if ((IsIntrinsic->getOpcode() == ISD::INTRINSIC_WO_CHAIN)) {
845 cast<ConstantSDNode>(IsIntrinsic->getOperand(0))->getZExtValue();
846 if (doesIntrinsicReturnPredicate(ID)) {
847 // Now we need to differentiate target data types.
848 if (N->getValueType(0) == MVT::i64) {
849 // Convert the zero_extend to Rs = Pd followed by A2_combinew(0,Rs).
850 SDValue TargetConst0 = CurDAG->getTargetConstant(0, dl, MVT::i32);
851 SDNode *Result_1 = CurDAG->getMachineNode(Hexagon::C2_tfrpr, dl,
853 SDValue(IsIntrinsic, 0));
854 SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A2_tfrsi, dl,
857 SDNode *Result_3 = CurDAG->getMachineNode(Hexagon::A2_combinew, dl,
858 MVT::i64, MVT::Other,
859 SDValue(Result_2, 0),
860 SDValue(Result_1, 0));
861 ReplaceNode(N, Result_3);
864 if (N->getValueType(0) == MVT::i32) {
865 // Convert the zero_extend to Rs = Pd
866 SDNode* RsPd = CurDAG->getMachineNode(Hexagon::C2_tfrpr, dl,
868 SDValue(IsIntrinsic, 0));
869 ReplaceNode(N, RsPd);
872 llvm_unreachable("Unexpected value type");
880 // Handling intrinsics for circular load and bitreverse load.
882 void HexagonDAGToDAGISel::SelectIntrinsicWChain(SDNode *N) {
883 if (MachineSDNode *L = LoadInstrForLoadIntrinsic(N)) {
884 StoreInstrForLoadIntrinsic(L, N);
885 CurDAG->RemoveDeadNode(N);
891 void HexagonDAGToDAGISel::SelectIntrinsicWOChain(SDNode *N) {
892 unsigned IID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
895 case Intrinsic::hexagon_S2_vsplatrb:
898 case Intrinsic::hexagon_S2_vsplatrh:
906 SDValue V = N->getOperand(1);
908 if (isValueExtension(V, Bits, U)) {
909 SDValue R = CurDAG->getNode(N->getOpcode(), SDLoc(N), N->getValueType(0),
910 N->getOperand(0), U);
911 ReplaceNode(N, R.getNode());
912 SelectCode(R.getNode());
919 // Map floating point constant values.
921 void HexagonDAGToDAGISel::SelectConstantFP(SDNode *N) {
923 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N);
924 const APFloat &APF = CN->getValueAPF();
925 if (N->getValueType(0) == MVT::f32) {
927 N, CurDAG->getMachineNode(Hexagon::TFRI_f, dl, MVT::f32,
928 CurDAG->getTargetConstantFP(
929 APF.convertToFloat(), dl, MVT::f32)));
932 else if (N->getValueType(0) == MVT::f64) {
934 N, CurDAG->getMachineNode(Hexagon::CONST64_Float_Real, dl, MVT::f64,
935 CurDAG->getTargetConstantFP(
936 APF.convertToDouble(), dl, MVT::f64)));
944 // Map predicate true (encoded as -1 in LLVM) to a XOR.
946 void HexagonDAGToDAGISel::SelectConstant(SDNode *N) {
948 if (N->getValueType(0) == MVT::i1) {
950 int32_t Val = cast<ConstantSDNode>(N)->getSExtValue();
952 Result = CurDAG->getMachineNode(Hexagon::TFR_PdTrue, dl, MVT::i1);
953 } else if (Val == 0) {
954 Result = CurDAG->getMachineNode(Hexagon::TFR_PdFalse, dl, MVT::i1);
957 ReplaceNode(N, Result);
967 // Map add followed by a asr -> asr +=.
969 void HexagonDAGToDAGISel::SelectAdd(SDNode *N) {
971 if (N->getValueType(0) != MVT::i32) {
975 // Identify nodes of the form: add(asr(...)).
976 SDNode* Src1 = N->getOperand(0).getNode();
977 if (Src1->getOpcode() != ISD::SRA || !Src1->hasOneUse()
978 || Src1->getValueType(0) != MVT::i32) {
983 // Build Rd = Rd' + asr(Rs, Rt). The machine constraints will ensure that
984 // Rd and Rd' are assigned to the same register
985 SDNode* Result = CurDAG->getMachineNode(Hexagon::S2_asr_r_r_acc, dl, MVT::i32,
988 Src1->getOperand(1));
989 ReplaceNode(N, Result);
993 // Map the following, where possible.
994 // AND/FABS -> clrbit
996 // XOR/FNEG ->toggle_bit.
998 void HexagonDAGToDAGISel::SelectBitOp(SDNode *N) {
1000 EVT ValueVT = N->getValueType(0);
1002 // We handle only 32 and 64-bit bit ops.
1003 if (!(ValueVT == MVT::i32 || ValueVT == MVT::i64 ||
1004 ValueVT == MVT::f32 || ValueVT == MVT::f64)) {
1009 // We handly only fabs and fneg for V5.
1010 unsigned Opc = N->getOpcode();
1011 if ((Opc == ISD::FABS || Opc == ISD::FNEG) && !HST->hasV5TOps()) {
1017 if (Opc != ISD::FABS && Opc != ISD::FNEG) {
1018 if (N->getOperand(1).getOpcode() == ISD::Constant)
1019 Val = cast<ConstantSDNode>((N)->getOperand(1))->getSExtValue();
1026 if (Opc == ISD::AND) {
1027 // Check if this is a bit-clearing AND, if not select code the usual way.
1028 if ((ValueVT == MVT::i32 && isPowerOf2_32(~Val)) ||
1029 (ValueVT == MVT::i64 && isPowerOf2_64(~Val)))
1037 // If OR or AND is being fed by shl, srl and, sra don't do this change,
1038 // because Hexagon provide |= &= on shl, srl, and sra.
1039 // Traverse the DAG to see if there is shl, srl and sra.
1040 if (Opc == ISD::OR || Opc == ISD::AND) {
1041 switch (N->getOperand(0)->getOpcode()) {
1052 // Make sure it's power of 2.
1053 unsigned BitPos = 0;
1054 if (Opc != ISD::FABS && Opc != ISD::FNEG) {
1055 if ((ValueVT == MVT::i32 && !isPowerOf2_32(Val)) ||
1056 (ValueVT == MVT::i64 && !isPowerOf2_64(Val))) {
1061 // Get the bit position.
1062 BitPos = countTrailingZeros(uint64_t(Val));
1064 // For fabs and fneg, it's always the 31st bit.
1068 unsigned BitOpc = 0;
1069 // Set the right opcode for bitwise operations.
1072 llvm_unreachable("Only bit-wise/abs/neg operations are allowed.");
1075 BitOpc = Hexagon::S2_clrbit_i;
1078 BitOpc = Hexagon::S2_setbit_i;
1082 BitOpc = Hexagon::S2_togglebit_i;
1087 // Get the right SDVal for the opcode.
1088 SDValue SDVal = CurDAG->getTargetConstant(BitPos, dl, MVT::i32);
1090 if (ValueVT == MVT::i32 || ValueVT == MVT::f32) {
1091 Result = CurDAG->getMachineNode(BitOpc, dl, ValueVT,
1092 N->getOperand(0), SDVal);
1094 // 64-bit gymnastic to use REG_SEQUENCE. But it's worth it.
1096 if (ValueVT == MVT::i64)
1097 SubValueVT = MVT::i32;
1099 SubValueVT = MVT::f32;
1101 SDNode *Reg = N->getOperand(0).getNode();
1102 SDValue RegClass = CurDAG->getTargetConstant(Hexagon::DoubleRegsRegClassID,
1105 SDValue SubregHiIdx = CurDAG->getTargetConstant(Hexagon::subreg_hireg, dl,
1107 SDValue SubregLoIdx = CurDAG->getTargetConstant(Hexagon::subreg_loreg, dl,
1110 SDValue SubregHI = CurDAG->getTargetExtractSubreg(Hexagon::subreg_hireg, dl,
1111 MVT::i32, SDValue(Reg, 0));
1113 SDValue SubregLO = CurDAG->getTargetExtractSubreg(Hexagon::subreg_loreg, dl,
1114 MVT::i32, SDValue(Reg, 0));
1116 // Clear/set/toggle hi or lo registers depending on the bit position.
1117 if (SubValueVT != MVT::f32 && BitPos < 32) {
1118 SDNode *Result0 = CurDAG->getMachineNode(BitOpc, dl, SubValueVT,
1120 const SDValue Ops[] = { RegClass, SubregHI, SubregHiIdx,
1121 SDValue(Result0, 0), SubregLoIdx };
1122 Result = CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE,
1125 if (Opc != ISD::FABS && Opc != ISD::FNEG)
1126 SDVal = CurDAG->getTargetConstant(BitPos-32, dl, MVT::i32);
1127 SDNode *Result0 = CurDAG->getMachineNode(BitOpc, dl, SubValueVT,
1129 const SDValue Ops[] = { RegClass, SDValue(Result0, 0), SubregHiIdx,
1130 SubregLO, SubregLoIdx };
1131 Result = CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE,
1136 ReplaceNode(N, Result);
1140 void HexagonDAGToDAGISel::SelectFrameIndex(SDNode *N) {
1141 MachineFrameInfo *MFI = MF->getFrameInfo();
1142 const HexagonFrameLowering *HFI = HST->getFrameLowering();
1143 int FX = cast<FrameIndexSDNode>(N)->getIndex();
1144 unsigned StkA = HFI->getStackAlignment();
1145 unsigned MaxA = MFI->getMaxAlignment();
1146 SDValue FI = CurDAG->getTargetFrameIndex(FX, MVT::i32);
1148 SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32);
1152 // - the object is fixed, or
1153 // - there are no objects with higher-than-default alignment, or
1154 // - there are no dynamically allocated objects.
1155 // Otherwise, use TFR_FIA.
1156 if (FX < 0 || MaxA <= StkA || !MFI->hasVarSizedObjects()) {
1157 R = CurDAG->getMachineNode(Hexagon::TFR_FI, DL, MVT::i32, FI, Zero);
1159 auto &HMFI = *MF->getInfo<HexagonMachineFunctionInfo>();
1160 unsigned AR = HMFI.getStackAlignBaseVReg();
1161 SDValue CH = CurDAG->getEntryNode();
1162 SDValue Ops[] = { CurDAG->getCopyFromReg(CH, DL, AR, MVT::i32), FI, Zero };
1163 R = CurDAG->getMachineNode(Hexagon::TFR_FIA, DL, MVT::i32, Ops);
1170 void HexagonDAGToDAGISel::SelectBitcast(SDNode *N) {
1171 EVT SVT = N->getOperand(0).getValueType();
1172 EVT DVT = N->getValueType(0);
1173 if (!SVT.isVector() || !DVT.isVector() ||
1174 SVT.getVectorElementType() == MVT::i1 ||
1175 DVT.getVectorElementType() == MVT::i1 ||
1176 SVT.getSizeInBits() != DVT.getSizeInBits()) {
1181 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N,0), N->getOperand(0));
1182 CurDAG->RemoveDeadNode(N);
1186 void HexagonDAGToDAGISel::Select(SDNode *N) {
1187 if (N->isMachineOpcode()) {
1189 return; // Already selected.
1192 switch (N->getOpcode()) {
1197 case ISD::ConstantFP:
1198 SelectConstantFP(N);
1201 case ISD::FrameIndex:
1202 SelectFrameIndex(N);
1237 case ISD::ZERO_EXTEND:
1238 SelectZeroExtend(N);
1241 case ISD::INTRINSIC_W_CHAIN:
1242 SelectIntrinsicWChain(N);
1245 case ISD::INTRINSIC_WO_CHAIN:
1246 SelectIntrinsicWOChain(N);
1253 bool HexagonDAGToDAGISel::
1254 SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID,
1255 std::vector<SDValue> &OutOps) {
1256 SDValue Inp = Op, Res;
1258 switch (ConstraintID) {
1261 case InlineAsm::Constraint_i:
1262 case InlineAsm::Constraint_o: // Offsetable.
1263 case InlineAsm::Constraint_v: // Not offsetable.
1264 case InlineAsm::Constraint_m: // Memory.
1265 if (SelectAddrFI(Inp, Res))
1266 OutOps.push_back(Res);
1268 OutOps.push_back(Inp);
1272 OutOps.push_back(CurDAG->getTargetConstant(0, SDLoc(Op), MVT::i32));
1277 void HexagonDAGToDAGISel::PreprocessISelDAG() {
1278 SelectionDAG &DAG = *CurDAG;
1279 std::vector<SDNode*> Nodes;
1280 for (SDNode &Node : DAG.allnodes())
1281 Nodes.push_back(&Node);
1283 // Simplify: (or (select c x 0) z) -> (select c (or x z) z)
1284 // (or (select c 0 y) z) -> (select c z (or y z))
1285 // This may not be the right thing for all targets, so do it here.
1286 for (auto I : Nodes) {
1287 if (I->getOpcode() != ISD::OR)
1290 auto IsZero = [] (const SDValue &V) -> bool {
1291 if (ConstantSDNode *SC = dyn_cast<ConstantSDNode>(V.getNode()))
1292 return SC->isNullValue();
1295 auto IsSelect0 = [IsZero] (const SDValue &Op) -> bool {
1296 if (Op.getOpcode() != ISD::SELECT)
1298 return IsZero(Op.getOperand(1)) || IsZero(Op.getOperand(2));
1301 SDValue N0 = I->getOperand(0), N1 = I->getOperand(1);
1302 EVT VT = I->getValueType(0);
1303 bool SelN0 = IsSelect0(N0);
1304 SDValue SOp = SelN0 ? N0 : N1;
1305 SDValue VOp = SelN0 ? N1 : N0;
1307 if (SOp.getOpcode() == ISD::SELECT && SOp.getNode()->hasOneUse()) {
1308 SDValue SC = SOp.getOperand(0);
1309 SDValue SX = SOp.getOperand(1);
1310 SDValue SY = SOp.getOperand(2);
1313 SDValue NewOr = DAG.getNode(ISD::OR, DLS, VT, SX, VOp);
1314 SDValue NewSel = DAG.getNode(ISD::SELECT, DLS, VT, SC, NewOr, VOp);
1315 DAG.ReplaceAllUsesWith(I, NewSel.getNode());
1316 } else if (IsZero(SX)) {
1317 SDValue NewOr = DAG.getNode(ISD::OR, DLS, VT, SY, VOp);
1318 SDValue NewSel = DAG.getNode(ISD::SELECT, DLS, VT, SC, VOp, NewOr);
1319 DAG.ReplaceAllUsesWith(I, NewSel.getNode());
1324 // Transform: (store ch addr (add x (add (shl y c) e)))
1325 // to: (store ch addr (add x (shl (add y d) c))),
1326 // where e = (shl d c) for some integer d.
1327 // The purpose of this is to enable generation of loads/stores with
1328 // shifted addressing mode, i.e. mem(x+y<<#c). For that, the shift
1329 // value c must be 0, 1 or 2.
1330 for (auto I : Nodes) {
1331 if (I->getOpcode() != ISD::STORE)
1334 // I matched: (store ch addr Off)
1335 SDValue Off = I->getOperand(2);
1336 // Off needs to match: (add x (add (shl y c) (shl d c))))
1337 if (Off.getOpcode() != ISD::ADD)
1339 // Off matched: (add x T0)
1340 SDValue T0 = Off.getOperand(1);
1341 // T0 needs to match: (add T1 T2):
1342 if (T0.getOpcode() != ISD::ADD)
1344 // T0 matched: (add T1 T2)
1345 SDValue T1 = T0.getOperand(0);
1346 SDValue T2 = T0.getOperand(1);
1347 // T1 needs to match: (shl y c)
1348 if (T1.getOpcode() != ISD::SHL)
1350 SDValue C = T1.getOperand(1);
1351 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(C.getNode());
1354 unsigned CV = CN->getZExtValue();
1357 // T2 needs to match e, where e = (shl d c) for some d.
1358 ConstantSDNode *EN = dyn_cast<ConstantSDNode>(T2.getNode());
1361 unsigned EV = EN->getZExtValue();
1362 if (EV % (1 << CV) != 0)
1364 unsigned DV = EV / (1 << CV);
1366 // Replace T0 with: (shl (add y d) c)
1367 SDLoc DL = SDLoc(I);
1368 EVT VT = T0.getValueType();
1369 SDValue D = DAG.getConstant(DV, DL, VT);
1370 // NewAdd = (add y d)
1371 SDValue NewAdd = DAG.getNode(ISD::ADD, DL, VT, T1.getOperand(0), D);
1372 // NewShl = (shl NewAdd c)
1373 SDValue NewShl = DAG.getNode(ISD::SHL, DL, VT, NewAdd, C);
1374 ReplaceNode(T0.getNode(), NewShl.getNode());
1378 void HexagonDAGToDAGISel::EmitFunctionEntryCode() {
1379 auto &HST = static_cast<const HexagonSubtarget&>(MF->getSubtarget());
1380 auto &HFI = *HST.getFrameLowering();
1381 if (!HFI.needsAligna(*MF))
1384 MachineFrameInfo *MFI = MF->getFrameInfo();
1385 MachineBasicBlock *EntryBB = &MF->front();
1386 unsigned AR = FuncInfo->CreateReg(MVT::i32);
1387 unsigned MaxA = MFI->getMaxAlignment();
1388 BuildMI(EntryBB, DebugLoc(), HII->get(Hexagon::ALIGNA), AR)
1390 MF->getInfo<HexagonMachineFunctionInfo>()->setStackAlignBaseVReg(AR);
1393 // Match a frame index that can be used in an addressing mode.
1394 bool HexagonDAGToDAGISel::SelectAddrFI(SDValue& N, SDValue &R) {
1395 if (N.getOpcode() != ISD::FrameIndex)
1397 auto &HFI = *HST->getFrameLowering();
1398 MachineFrameInfo *MFI = MF->getFrameInfo();
1399 int FX = cast<FrameIndexSDNode>(N)->getIndex();
1400 if (!MFI->isFixedObjectIndex(FX) && HFI.needsAligna(*MF))
1402 R = CurDAG->getTargetFrameIndex(FX, MVT::i32);
1406 inline bool HexagonDAGToDAGISel::SelectAddrGA(SDValue &N, SDValue &R) {
1407 return SelectGlobalAddress(N, R, false);
1410 inline bool HexagonDAGToDAGISel::SelectAddrGP(SDValue &N, SDValue &R) {
1411 return SelectGlobalAddress(N, R, true);
1414 bool HexagonDAGToDAGISel::SelectGlobalAddress(SDValue &N, SDValue &R,
1416 switch (N.getOpcode()) {
1418 SDValue N0 = N.getOperand(0);
1419 SDValue N1 = N.getOperand(1);
1420 unsigned GAOpc = N0.getOpcode();
1421 if (UseGP && GAOpc != HexagonISD::CONST32_GP)
1423 if (!UseGP && GAOpc != HexagonISD::CONST32)
1425 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N1)) {
1426 SDValue Addr = N0.getOperand(0);
1427 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Addr)) {
1428 if (GA->getOpcode() == ISD::TargetGlobalAddress) {
1429 uint64_t NewOff = GA->getOffset() + (uint64_t)Const->getSExtValue();
1430 R = CurDAG->getTargetGlobalAddress(GA->getGlobal(), SDLoc(Const),
1431 N.getValueType(), NewOff);
1438 case HexagonISD::CONST32:
1439 // The operand(0) of CONST32 is TargetGlobalAddress, which is what we
1440 // want in the instruction.
1442 R = N.getOperand(0);
1444 case HexagonISD::CONST32_GP:
1446 R = N.getOperand(0);
1455 bool HexagonDAGToDAGISel::isValueExtension(const SDValue &Val,
1456 unsigned FromBits, SDValue &Src) {
1457 unsigned Opc = Val.getOpcode();
1459 case ISD::SIGN_EXTEND:
1460 case ISD::ZERO_EXTEND:
1461 case ISD::ANY_EXTEND: {
1462 SDValue const &Op0 = Val.getOperand(0);
1463 EVT T = Op0.getValueType();
1464 if (T.isInteger() && T.getSizeInBits() == FromBits) {
1470 case ISD::SIGN_EXTEND_INREG:
1471 case ISD::AssertSext:
1472 case ISD::AssertZext:
1473 if (Val.getOperand(0).getValueType().isInteger()) {
1474 VTSDNode *T = cast<VTSDNode>(Val.getOperand(1));
1475 if (T->getVT().getSizeInBits() == FromBits) {
1476 Src = Val.getOperand(0);
1482 // Check if this is an AND with "FromBits" of lower bits set to 1.
1483 uint64_t FromMask = (1 << FromBits) - 1;
1484 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(0))) {
1485 if (C->getZExtValue() == FromMask) {
1486 Src = Val.getOperand(1);
1490 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(1))) {
1491 if (C->getZExtValue() == FromMask) {
1492 Src = Val.getOperand(0);
1500 // OR/XOR with the lower "FromBits" bits set to 0.
1501 uint64_t FromMask = (1 << FromBits) - 1;
1502 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(0))) {
1503 if ((C->getZExtValue() & FromMask) == 0) {
1504 Src = Val.getOperand(1);
1508 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(1))) {
1509 if ((C->getZExtValue() & FromMask) == 0) {
1510 Src = Val.getOperand(0);
1522 bool HexagonDAGToDAGISel::orIsAdd(const SDNode *N) const {
1523 assert(N->getOpcode() == ISD::OR);
1524 auto *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
1527 // Detect when "or" is used to add an offset to a stack object.
1528 if (auto *FN = dyn_cast<FrameIndexSDNode>(N->getOperand(0))) {
1529 MachineFrameInfo *MFI = MF->getFrameInfo();
1530 unsigned A = MFI->getObjectAlignment(FN->getIndex());
1531 assert(isPowerOf2_32(A));
1532 int32_t Off = C->getSExtValue();
1533 // If the alleged offset fits in the zero bits guaranteed by
1534 // the alignment, then this or is really an add.
1535 return (Off >= 0) && (((A-1) & Off) == unsigned(Off));
1540 bool HexagonDAGToDAGISel::isAlignedMemNode(const MemSDNode *N) const {
1541 return N->getAlignment() >= N->getMemoryVT().getStoreSize();