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 const HexagonTargetMachine& HTM;
49 const HexagonSubtarget &HST;
51 explicit HexagonDAGToDAGISel(HexagonTargetMachine &tm,
52 CodeGenOpt::Level OptLevel)
53 : SelectionDAGISel(tm, OptLevel), HTM(tm),
54 HST(tm.getSubtarget<HexagonSubtarget>()) {
55 initializeHexagonDAGToDAGISelPass(*PassRegistry::getPassRegistry());
57 virtual void PreprocessISelDAG() override;
59 SDNode *Select(SDNode *N) override;
61 // Complex Pattern Selectors.
62 inline bool SelectAddrGA(SDValue &N, SDValue &R);
63 inline bool SelectAddrGP(SDValue &N, SDValue &R);
64 bool SelectGlobalAddress(SDValue &N, SDValue &R, bool UseGP);
65 bool SelectAddrFI(SDValue &N, SDValue &R);
67 const char *getPassName() const override {
68 return "Hexagon DAG->DAG Pattern Instruction Selection";
71 SDNode *SelectFrameIndex(SDNode *N);
72 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
73 /// inline asm expressions.
74 bool SelectInlineAsmMemoryOperand(const SDValue &Op,
75 unsigned ConstraintID,
76 std::vector<SDValue> &OutOps) override;
77 SDNode *SelectLoad(SDNode *N);
78 SDNode *SelectBaseOffsetLoad(LoadSDNode *LD, SDLoc dl);
79 SDNode *SelectIndexedLoad(LoadSDNode *LD, SDLoc dl);
80 SDNode *SelectIndexedLoadZeroExtend64(LoadSDNode *LD, unsigned Opcode,
82 SDNode *SelectIndexedLoadSignExtend64(LoadSDNode *LD, unsigned Opcode,
84 SDNode *SelectBaseOffsetStore(StoreSDNode *ST, SDLoc dl);
85 SDNode *SelectIndexedStore(StoreSDNode *ST, SDLoc dl);
86 SDNode *SelectStore(SDNode *N);
87 SDNode *SelectSHL(SDNode *N);
88 SDNode *SelectMul(SDNode *N);
89 SDNode *SelectZeroExtend(SDNode *N);
90 SDNode *SelectIntrinsicWChain(SDNode *N);
91 SDNode *SelectIntrinsicWOChain(SDNode *N);
92 SDNode *SelectConstant(SDNode *N);
93 SDNode *SelectConstantFP(SDNode *N);
94 SDNode *SelectAdd(SDNode *N);
95 SDNode *SelectBitOp(SDNode *N);
97 // XformMskToBitPosU5Imm - Returns the bit position which
98 // the single bit 32 bit mask represents.
99 // Used in Clr and Set bit immediate memops.
100 SDValue XformMskToBitPosU5Imm(uint32_t Imm) {
102 bitPos = Log2_32(Imm);
103 assert(bitPos >= 0 && bitPos < 32 &&
104 "Constant out of range for 32 BitPos Memops");
105 return CurDAG->getTargetConstant(bitPos, MVT::i32);
108 // XformMskToBitPosU4Imm - Returns the bit position which the single-bit
109 // 16 bit mask represents. Used in Clr and Set bit immediate memops.
110 SDValue XformMskToBitPosU4Imm(uint16_t Imm) {
111 return XformMskToBitPosU5Imm(Imm);
114 // XformMskToBitPosU3Imm - Returns the bit position which the single-bit
115 // 8 bit mask represents. Used in Clr and Set bit immediate memops.
116 SDValue XformMskToBitPosU3Imm(uint8_t Imm) {
117 return XformMskToBitPosU5Imm(Imm);
120 // Return true if there is exactly one bit set in V, i.e., if V is one of the
121 // following integers: 2^0, 2^1, ..., 2^31.
122 bool ImmIsSingleBit(uint32_t v) const {
123 return isPowerOf2_32(v);
126 // XformM5ToU5Imm - Return a target constant with the specified value, of
127 // type i32 where the negative literal is transformed into a positive literal
128 // for use in -= memops.
129 inline SDValue XformM5ToU5Imm(signed Imm) {
130 assert( (Imm >= -31 && Imm <= -1) && "Constant out of range for Memops");
131 return CurDAG->getTargetConstant( - Imm, MVT::i32);
134 // XformU7ToU7M1Imm - Return a target constant decremented by 1, in range
135 // [1..128], used in cmpb.gtu instructions.
136 inline SDValue XformU7ToU7M1Imm(signed Imm) {
137 assert((Imm >= 1 && Imm <= 128) && "Constant out of range for cmpb op");
138 return CurDAG->getTargetConstant(Imm - 1, MVT::i8);
141 // XformS8ToS8M1Imm - Return a target constant decremented by 1.
142 inline SDValue XformSToSM1Imm(signed Imm) {
143 return CurDAG->getTargetConstant(Imm - 1, MVT::i32);
146 // XformU8ToU8M1Imm - Return a target constant decremented by 1.
147 inline SDValue XformUToUM1Imm(unsigned Imm) {
148 assert((Imm >= 1) && "Cannot decrement unsigned int less than 1");
149 return CurDAG->getTargetConstant(Imm - 1, MVT::i32);
152 // XformSToSM2Imm - Return a target constant decremented by 2.
153 inline SDValue XformSToSM2Imm(unsigned Imm) {
154 return CurDAG->getTargetConstant(Imm - 2, MVT::i32);
157 // XformSToSM3Imm - Return a target constant decremented by 3.
158 inline SDValue XformSToSM3Imm(unsigned Imm) {
159 return CurDAG->getTargetConstant(Imm - 3, MVT::i32);
162 // Include the pieces autogenerated from the target description.
163 #include "HexagonGenDAGISel.inc"
166 bool isValueExtension(const SDValue &Val, unsigned FromBits, SDValue &Src);
167 }; // end HexagonDAGToDAGISel
168 } // end anonymous namespace
171 /// createHexagonISelDag - This pass converts a legalized DAG into a
172 /// Hexagon-specific DAG, ready for instruction scheduling.
175 FunctionPass *createHexagonISelDag(HexagonTargetMachine &TM,
176 CodeGenOpt::Level OptLevel) {
177 return new HexagonDAGToDAGISel(TM, OptLevel);
181 static void initializePassOnce(PassRegistry &Registry) {
182 const char *Name = "Hexagon DAG->DAG Pattern Instruction Selection";
183 PassInfo *PI = new PassInfo(Name, "hexagon-isel",
184 &SelectionDAGISel::ID, nullptr, false, false);
185 Registry.registerPass(*PI, true);
188 void llvm::initializeHexagonDAGToDAGISelPass(PassRegistry &Registry) {
189 CALL_ONCE_INITIALIZATION(initializePassOnce)
193 // Intrinsics that return a a predicate.
194 static unsigned doesIntrinsicReturnPredicate(unsigned ID)
199 case Intrinsic::hexagon_C2_cmpeq:
200 case Intrinsic::hexagon_C2_cmpgt:
201 case Intrinsic::hexagon_C2_cmpgtu:
202 case Intrinsic::hexagon_C2_cmpgtup:
203 case Intrinsic::hexagon_C2_cmpgtp:
204 case Intrinsic::hexagon_C2_cmpeqp:
205 case Intrinsic::hexagon_C2_bitsset:
206 case Intrinsic::hexagon_C2_bitsclr:
207 case Intrinsic::hexagon_C2_cmpeqi:
208 case Intrinsic::hexagon_C2_cmpgti:
209 case Intrinsic::hexagon_C2_cmpgtui:
210 case Intrinsic::hexagon_C2_cmpgei:
211 case Intrinsic::hexagon_C2_cmpgeui:
212 case Intrinsic::hexagon_C2_cmplt:
213 case Intrinsic::hexagon_C2_cmpltu:
214 case Intrinsic::hexagon_C2_bitsclri:
215 case Intrinsic::hexagon_C2_and:
216 case Intrinsic::hexagon_C2_or:
217 case Intrinsic::hexagon_C2_xor:
218 case Intrinsic::hexagon_C2_andn:
219 case Intrinsic::hexagon_C2_not:
220 case Intrinsic::hexagon_C2_orn:
221 case Intrinsic::hexagon_C2_pxfer_map:
222 case Intrinsic::hexagon_C2_any8:
223 case Intrinsic::hexagon_C2_all8:
224 case Intrinsic::hexagon_A2_vcmpbeq:
225 case Intrinsic::hexagon_A2_vcmpbgtu:
226 case Intrinsic::hexagon_A2_vcmpheq:
227 case Intrinsic::hexagon_A2_vcmphgt:
228 case Intrinsic::hexagon_A2_vcmphgtu:
229 case Intrinsic::hexagon_A2_vcmpweq:
230 case Intrinsic::hexagon_A2_vcmpwgt:
231 case Intrinsic::hexagon_A2_vcmpwgtu:
232 case Intrinsic::hexagon_C2_tfrrp:
233 case Intrinsic::hexagon_S2_tstbit_i:
234 case Intrinsic::hexagon_S2_tstbit_r:
239 SDNode *HexagonDAGToDAGISel::SelectIndexedLoadSignExtend64(LoadSDNode *LD,
242 SDValue Chain = LD->getChain();
243 EVT LoadedVT = LD->getMemoryVT();
244 SDValue Base = LD->getBasePtr();
245 SDValue Offset = LD->getOffset();
246 SDNode *OffsetNode = Offset.getNode();
247 int32_t Val = cast<ConstantSDNode>(OffsetNode)->getSExtValue();
249 const HexagonInstrInfo &TII = *HST.getInstrInfo();
250 if (TII.isValidAutoIncImm(LoadedVT, Val)) {
251 SDValue TargetConst = CurDAG->getTargetConstant(Val, MVT::i32);
252 SDNode *Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::i32, MVT::i32,
253 MVT::Other, Base, TargetConst,
255 SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A2_sxtw, dl, MVT::i64,
256 SDValue(Result_1, 0));
257 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
258 MemOp[0] = LD->getMemOperand();
259 cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
260 const SDValue Froms[] = { SDValue(LD, 0),
263 const SDValue Tos[] = { SDValue(Result_2, 0),
264 SDValue(Result_1, 1),
265 SDValue(Result_1, 2) };
266 ReplaceUses(Froms, Tos, 3);
270 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
271 SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
272 SDNode *Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::i32, MVT::Other,
273 Base, TargetConst0, Chain);
274 SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A2_sxtw, dl, MVT::i64,
275 SDValue(Result_1, 0));
276 SDNode* Result_3 = CurDAG->getMachineNode(Hexagon::A2_addi, dl, MVT::i32,
277 Base, TargetConstVal,
278 SDValue(Result_1, 1));
279 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
280 MemOp[0] = LD->getMemOperand();
281 cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
282 const SDValue Froms[] = { SDValue(LD, 0),
285 const SDValue Tos[] = { SDValue(Result_2, 0),
286 SDValue(Result_3, 0),
287 SDValue(Result_1, 1) };
288 ReplaceUses(Froms, Tos, 3);
293 SDNode *HexagonDAGToDAGISel::SelectIndexedLoadZeroExtend64(LoadSDNode *LD,
296 SDValue Chain = LD->getChain();
297 EVT LoadedVT = LD->getMemoryVT();
298 SDValue Base = LD->getBasePtr();
299 SDValue Offset = LD->getOffset();
300 SDNode *OffsetNode = Offset.getNode();
301 int32_t Val = cast<ConstantSDNode>(OffsetNode)->getSExtValue();
303 const HexagonInstrInfo &TII = *HST.getInstrInfo();
304 if (TII.isValidAutoIncImm(LoadedVT, Val)) {
305 SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
306 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
307 SDNode *Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::i32,
308 MVT::i32, MVT::Other, Base,
309 TargetConstVal, Chain);
310 SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A4_combineir, dl,
311 MVT::i64, MVT::Other,
313 SDValue(Result_1,0));
314 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
315 MemOp[0] = LD->getMemOperand();
316 cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
317 const SDValue Froms[] = { SDValue(LD, 0),
320 const SDValue Tos[] = { SDValue(Result_2, 0),
321 SDValue(Result_1, 1),
322 SDValue(Result_1, 2) };
323 ReplaceUses(Froms, Tos, 3);
327 // Generate an indirect load.
328 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
329 SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
330 SDNode *Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::i32,
331 MVT::Other, Base, TargetConst0,
333 SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A4_combineir, dl,
334 MVT::i64, MVT::Other,
336 SDValue(Result_1,0));
337 // Add offset to base.
338 SDNode* Result_3 = CurDAG->getMachineNode(Hexagon::A2_addi, dl, MVT::i32,
339 Base, TargetConstVal,
340 SDValue(Result_1, 1));
341 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
342 MemOp[0] = LD->getMemOperand();
343 cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
344 const SDValue Froms[] = { SDValue(LD, 0),
347 const SDValue Tos[] = { SDValue(Result_2, 0), // Load value.
348 SDValue(Result_3, 0), // New address.
349 SDValue(Result_1, 1) };
350 ReplaceUses(Froms, Tos, 3);
355 SDNode *HexagonDAGToDAGISel::SelectIndexedLoad(LoadSDNode *LD, SDLoc dl) {
356 SDValue Chain = LD->getChain();
357 SDValue Base = LD->getBasePtr();
358 SDValue Offset = LD->getOffset();
359 SDNode *OffsetNode = Offset.getNode();
360 // Get the constant value.
361 int32_t Val = cast<ConstantSDNode>(OffsetNode)->getSExtValue();
362 EVT LoadedVT = LD->getMemoryVT();
365 // Check for zero extended loads. Treat any-extend loads as zero extended
367 ISD::LoadExtType ExtType = LD->getExtensionType();
368 bool IsZeroExt = (ExtType == ISD::ZEXTLOAD || ExtType == ISD::EXTLOAD);
370 // Figure out the opcode.
371 const HexagonInstrInfo &TII = *HST.getInstrInfo();
372 if (LoadedVT == MVT::i64) {
373 if (TII.isValidAutoIncImm(LoadedVT, Val))
374 Opcode = Hexagon::L2_loadrd_pi;
376 Opcode = Hexagon::L2_loadrd_io;
377 } else if (LoadedVT == MVT::i32) {
378 if (TII.isValidAutoIncImm(LoadedVT, Val))
379 Opcode = Hexagon::L2_loadri_pi;
381 Opcode = Hexagon::L2_loadri_io;
382 } else if (LoadedVT == MVT::i16) {
383 if (TII.isValidAutoIncImm(LoadedVT, Val))
384 Opcode = IsZeroExt ? Hexagon::L2_loadruh_pi : Hexagon::L2_loadrh_pi;
386 Opcode = IsZeroExt ? Hexagon::L2_loadruh_io : Hexagon::L2_loadrh_io;
387 } else if (LoadedVT == MVT::i8) {
388 if (TII.isValidAutoIncImm(LoadedVT, Val))
389 Opcode = IsZeroExt ? Hexagon::L2_loadrub_pi : Hexagon::L2_loadrb_pi;
391 Opcode = IsZeroExt ? Hexagon::L2_loadrub_io : Hexagon::L2_loadrb_io;
393 llvm_unreachable("unknown memory type");
395 // For zero extended i64 loads, we need to add combine instructions.
396 if (LD->getValueType(0) == MVT::i64 && IsZeroExt)
397 return SelectIndexedLoadZeroExtend64(LD, Opcode, dl);
398 // Handle sign extended i64 loads.
399 if (LD->getValueType(0) == MVT::i64 && ExtType == ISD::SEXTLOAD)
400 return SelectIndexedLoadSignExtend64(LD, Opcode, dl);
402 if (TII.isValidAutoIncImm(LoadedVT, Val)) {
403 SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
404 SDNode* Result = CurDAG->getMachineNode(Opcode, dl,
406 MVT::i32, MVT::Other, Base,
407 TargetConstVal, Chain);
408 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
409 MemOp[0] = LD->getMemOperand();
410 cast<MachineSDNode>(Result)->setMemRefs(MemOp, MemOp + 1);
411 const SDValue Froms[] = { SDValue(LD, 0),
415 const SDValue Tos[] = { SDValue(Result, 0),
419 ReplaceUses(Froms, Tos, 3);
422 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
423 SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
424 SDNode* Result_1 = CurDAG->getMachineNode(Opcode, dl,
426 MVT::Other, Base, TargetConst0,
428 SDNode* Result_2 = CurDAG->getMachineNode(Hexagon::A2_addi, dl, MVT::i32,
429 Base, TargetConstVal,
430 SDValue(Result_1, 1));
431 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
432 MemOp[0] = LD->getMemOperand();
433 cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
434 const SDValue Froms[] = { SDValue(LD, 0),
438 const SDValue Tos[] = { SDValue(Result_1, 0),
439 SDValue(Result_2, 0),
442 ReplaceUses(Froms, Tos, 3);
448 SDNode *HexagonDAGToDAGISel::SelectLoad(SDNode *N) {
451 LoadSDNode *LD = cast<LoadSDNode>(N);
452 ISD::MemIndexedMode AM = LD->getAddressingMode();
454 // Handle indexed loads.
455 if (AM != ISD::UNINDEXED) {
456 result = SelectIndexedLoad(LD, dl);
458 result = SelectCode(LD);
465 SDNode *HexagonDAGToDAGISel::SelectIndexedStore(StoreSDNode *ST, SDLoc dl) {
466 SDValue Chain = ST->getChain();
467 SDValue Base = ST->getBasePtr();
468 SDValue Offset = ST->getOffset();
469 SDValue Value = ST->getValue();
470 SDNode *OffsetNode = Offset.getNode();
471 // Get the constant value.
472 int32_t Val = cast<ConstantSDNode>(OffsetNode)->getSExtValue();
473 EVT StoredVT = ST->getMemoryVT();
474 EVT ValueVT = Value.getValueType();
476 // Offset value must be within representable range
477 // and must have correct alignment properties.
478 const HexagonInstrInfo &TII = *HST.getInstrInfo();
479 if (TII.isValidAutoIncImm(StoredVT, Val)) {
482 // Figure out the post inc version of opcode.
483 if (StoredVT == MVT::i64) Opcode = Hexagon::S2_storerd_pi;
484 else if (StoredVT == MVT::i32) Opcode = Hexagon::S2_storeri_pi;
485 else if (StoredVT == MVT::i16) Opcode = Hexagon::S2_storerh_pi;
486 else if (StoredVT == MVT::i8) Opcode = Hexagon::S2_storerb_pi;
487 else llvm_unreachable("unknown memory type");
489 if (ST->isTruncatingStore() && ValueVT.getSizeInBits() == 64) {
490 assert(StoredVT.getSizeInBits() < 64 && "Not a truncating store");
491 Value = CurDAG->getTargetExtractSubreg(Hexagon::subreg_loreg,
492 dl, MVT::i32, Value);
494 SDValue Ops[] = {Base, CurDAG->getTargetConstant(Val, MVT::i32), Value,
496 // Build post increment store.
497 SDNode* Result = CurDAG->getMachineNode(Opcode, dl, MVT::i32,
499 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
500 MemOp[0] = ST->getMemOperand();
501 cast<MachineSDNode>(Result)->setMemRefs(MemOp, MemOp + 1);
503 ReplaceUses(ST, Result);
504 ReplaceUses(SDValue(ST,1), SDValue(Result,1));
508 // Note: Order of operands matches the def of instruction:
510 // : STInst<(outs), (ins IntRegs:$base, imm:$offset, DoubleRegs:$src1), ...
511 // and it differs for POST_ST* for instance.
512 SDValue Ops[] = { Base, CurDAG->getTargetConstant(0, MVT::i32), Value,
516 // Figure out the opcode.
517 if (StoredVT == MVT::i64) Opcode = Hexagon::S2_storerd_io;
518 else if (StoredVT == MVT::i32) Opcode = Hexagon::S2_storeri_io;
519 else if (StoredVT == MVT::i16) Opcode = Hexagon::S2_storerh_io;
520 else if (StoredVT == MVT::i8) Opcode = Hexagon::S2_storerb_io;
521 else llvm_unreachable("unknown memory type");
523 // Build regular store.
524 SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
525 SDNode* Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::Other, Ops);
526 // Build splitted incriment instruction.
527 SDNode* Result_2 = CurDAG->getMachineNode(Hexagon::A2_addi, dl, MVT::i32,
530 SDValue(Result_1, 0));
531 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
532 MemOp[0] = ST->getMemOperand();
533 cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
535 ReplaceUses(SDValue(ST,0), SDValue(Result_2,0));
536 ReplaceUses(SDValue(ST,1), SDValue(Result_1,0));
540 SDNode *HexagonDAGToDAGISel::SelectStore(SDNode *N) {
542 StoreSDNode *ST = cast<StoreSDNode>(N);
543 ISD::MemIndexedMode AM = ST->getAddressingMode();
545 // Handle indexed stores.
546 if (AM != ISD::UNINDEXED) {
547 return SelectIndexedStore(ST, dl);
550 return SelectCode(ST);
553 SDNode *HexagonDAGToDAGISel::SelectMul(SDNode *N) {
557 // %conv.i = sext i32 %tmp1 to i64
558 // %conv2.i = sext i32 %add to i64
559 // %mul.i = mul nsw i64 %conv2.i, %conv.i
561 // --- match with the following ---
563 // %mul.i = mpy (%tmp1, %add)
566 if (N->getValueType(0) == MVT::i64) {
567 // Shifting a i64 signed multiply.
568 SDValue MulOp0 = N->getOperand(0);
569 SDValue MulOp1 = N->getOperand(1);
574 // Handle sign_extend and sextload.
575 if (MulOp0.getOpcode() == ISD::SIGN_EXTEND) {
576 SDValue Sext0 = MulOp0.getOperand(0);
577 if (Sext0.getNode()->getValueType(0) != MVT::i32) {
578 return SelectCode(N);
582 } else if (MulOp0.getOpcode() == ISD::LOAD) {
583 LoadSDNode *LD = cast<LoadSDNode>(MulOp0.getNode());
584 if (LD->getMemoryVT() != MVT::i32 ||
585 LD->getExtensionType() != ISD::SEXTLOAD ||
586 LD->getAddressingMode() != ISD::UNINDEXED) {
587 return SelectCode(N);
590 SDValue Chain = LD->getChain();
591 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
592 OP0 = SDValue(CurDAG->getMachineNode(Hexagon::L2_loadri_io, dl, MVT::i32,
594 LD->getBasePtr(), TargetConst0,
597 return SelectCode(N);
600 // Same goes for the second operand.
601 if (MulOp1.getOpcode() == ISD::SIGN_EXTEND) {
602 SDValue Sext1 = MulOp1.getOperand(0);
603 if (Sext1.getNode()->getValueType(0) != MVT::i32) {
604 return SelectCode(N);
608 } else if (MulOp1.getOpcode() == ISD::LOAD) {
609 LoadSDNode *LD = cast<LoadSDNode>(MulOp1.getNode());
610 if (LD->getMemoryVT() != MVT::i32 ||
611 LD->getExtensionType() != ISD::SEXTLOAD ||
612 LD->getAddressingMode() != ISD::UNINDEXED) {
613 return SelectCode(N);
616 SDValue Chain = LD->getChain();
617 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
618 OP1 = SDValue(CurDAG->getMachineNode(Hexagon::L2_loadri_io, dl, MVT::i32,
620 LD->getBasePtr(), TargetConst0,
623 return SelectCode(N);
626 // Generate a mpy instruction.
627 SDNode *Result = CurDAG->getMachineNode(Hexagon::M2_dpmpyss_s0, dl, MVT::i64,
629 ReplaceUses(N, Result);
633 return SelectCode(N);
636 SDNode *HexagonDAGToDAGISel::SelectSHL(SDNode *N) {
638 if (N->getValueType(0) == MVT::i32) {
639 SDValue Shl_0 = N->getOperand(0);
640 SDValue Shl_1 = N->getOperand(1);
642 if (Shl_1.getOpcode() == ISD::Constant) {
643 if (Shl_0.getOpcode() == ISD::MUL) {
644 SDValue Mul_0 = Shl_0.getOperand(0); // Val
645 SDValue Mul_1 = Shl_0.getOperand(1); // Const
646 // RHS of mul is const.
647 if (Mul_1.getOpcode() == ISD::Constant) {
649 cast<ConstantSDNode>(Shl_1.getNode())->getSExtValue();
651 cast<ConstantSDNode>(Mul_1.getNode())->getSExtValue();
652 int32_t ValConst = MulConst << ShlConst;
653 SDValue Val = CurDAG->getTargetConstant(ValConst,
655 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Val.getNode()))
656 if (isInt<9>(CN->getSExtValue())) {
658 CurDAG->getMachineNode(Hexagon::M2_mpysmi, dl,
659 MVT::i32, Mul_0, Val);
660 ReplaceUses(N, Result);
665 } else if (Shl_0.getOpcode() == ISD::SUB) {
666 SDValue Sub_0 = Shl_0.getOperand(0); // Const 0
667 SDValue Sub_1 = Shl_0.getOperand(1); // Val
668 if (Sub_0.getOpcode() == ISD::Constant) {
670 cast<ConstantSDNode>(Sub_0.getNode())->getSExtValue();
672 if (Sub_1.getOpcode() == ISD::SHL) {
673 SDValue Shl2_0 = Sub_1.getOperand(0); // Val
674 SDValue Shl2_1 = Sub_1.getOperand(1); // Const
675 if (Shl2_1.getOpcode() == ISD::Constant) {
677 cast<ConstantSDNode>(Shl_1.getNode())->getSExtValue();
679 cast<ConstantSDNode>(Shl2_1.getNode())->getSExtValue();
680 int32_t ValConst = 1 << (ShlConst+Shl2Const);
681 SDValue Val = CurDAG->getTargetConstant(-ValConst, MVT::i32);
682 if (ConstantSDNode *CN =
683 dyn_cast<ConstantSDNode>(Val.getNode()))
684 if (isInt<9>(CN->getSExtValue())) {
686 CurDAG->getMachineNode(Hexagon::M2_mpysmi, dl, MVT::i32,
688 ReplaceUses(N, Result);
698 return SelectCode(N);
703 // If there is an zero_extend followed an intrinsic in DAG (this means - the
704 // result of the intrinsic is predicate); convert the zero_extend to
705 // transfer instruction.
707 // Zero extend -> transfer is lowered here. Otherwise, zero_extend will be
708 // converted into a MUX as predicate registers defined as 1 bit in the
709 // compiler. Architecture defines them as 8-bit registers.
710 // We want to preserve all the lower 8-bits and, not just 1 LSB bit.
712 SDNode *HexagonDAGToDAGISel::SelectZeroExtend(SDNode *N) {
714 SDNode *IsIntrinsic = N->getOperand(0).getNode();
715 if ((IsIntrinsic->getOpcode() == ISD::INTRINSIC_WO_CHAIN)) {
717 cast<ConstantSDNode>(IsIntrinsic->getOperand(0))->getZExtValue();
718 if (doesIntrinsicReturnPredicate(ID)) {
719 // Now we need to differentiate target data types.
720 if (N->getValueType(0) == MVT::i64) {
721 // Convert the zero_extend to Rs = Pd followed by A2_combinew(0,Rs).
722 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
723 SDNode *Result_1 = CurDAG->getMachineNode(Hexagon::C2_tfrpr, dl,
725 SDValue(IsIntrinsic, 0));
726 SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A2_tfrsi, dl,
729 SDNode *Result_3 = CurDAG->getMachineNode(Hexagon::A2_combinew, dl,
730 MVT::i64, MVT::Other,
731 SDValue(Result_2, 0),
732 SDValue(Result_1, 0));
733 ReplaceUses(N, Result_3);
736 if (N->getValueType(0) == MVT::i32) {
737 // Convert the zero_extend to Rs = Pd
738 SDNode* RsPd = CurDAG->getMachineNode(Hexagon::C2_tfrpr, dl,
740 SDValue(IsIntrinsic, 0));
741 ReplaceUses(N, RsPd);
744 llvm_unreachable("Unexpected value type");
747 return SelectCode(N);
751 // Checking for intrinsics which have predicate registers as operand(s)
752 // and lowering to the actual intrinsic.
754 SDNode *HexagonDAGToDAGISel::SelectIntrinsicWOChain(SDNode *N) {
755 unsigned IID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
758 case Intrinsic::hexagon_S2_vsplatrb:
761 case Intrinsic::hexagon_S2_vsplatrh:
765 return SelectCode(N);
768 SDValue const &V = N->getOperand(1);
770 if (isValueExtension(V, Bits, U)) {
771 SDValue R = CurDAG->getNode(N->getOpcode(), SDLoc(N), N->getValueType(0),
772 N->getOperand(0), U);
773 return SelectCode(R.getNode());
775 return SelectCode(N);
779 // Map floating point constant values.
781 SDNode *HexagonDAGToDAGISel::SelectConstantFP(SDNode *N) {
783 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N);
784 APFloat APF = CN->getValueAPF();
785 if (N->getValueType(0) == MVT::f32) {
786 return CurDAG->getMachineNode(Hexagon::TFRI_f, dl, MVT::f32,
787 CurDAG->getTargetConstantFP(APF.convertToFloat(), MVT::f32));
789 else if (N->getValueType(0) == MVT::f64) {
790 return CurDAG->getMachineNode(Hexagon::CONST64_Float_Real, dl, MVT::f64,
791 CurDAG->getTargetConstantFP(APF.convertToDouble(), MVT::f64));
794 return SelectCode(N);
798 // Map predicate true (encoded as -1 in LLVM) to a XOR.
800 SDNode *HexagonDAGToDAGISel::SelectConstant(SDNode *N) {
802 if (N->getValueType(0) == MVT::i1) {
804 int32_t Val = cast<ConstantSDNode>(N)->getSExtValue();
806 // Create the IntReg = 1 node.
808 CurDAG->getMachineNode(Hexagon::A2_tfrsi, dl, MVT::i32,
809 CurDAG->getTargetConstant(0, MVT::i32));
812 SDNode* Pd = CurDAG->getMachineNode(Hexagon::C2_tfrrp, dl, MVT::i1,
813 SDValue(IntRegTFR, 0));
816 SDNode* NotPd = CurDAG->getMachineNode(Hexagon::C2_not, dl, MVT::i1,
820 Result = CurDAG->getMachineNode(Hexagon::C2_xor, dl, MVT::i1,
821 SDValue(Pd, 0), SDValue(NotPd, 0));
823 // We have just built:
825 // Pd = xor(not(Pd), Pd)
827 ReplaceUses(N, Result);
832 return SelectCode(N);
837 // Map add followed by a asr -> asr +=.
839 SDNode *HexagonDAGToDAGISel::SelectAdd(SDNode *N) {
841 if (N->getValueType(0) != MVT::i32) {
842 return SelectCode(N);
844 // Identify nodes of the form: add(asr(...)).
845 SDNode* Src1 = N->getOperand(0).getNode();
846 if (Src1->getOpcode() != ISD::SRA || !Src1->hasOneUse()
847 || Src1->getValueType(0) != MVT::i32) {
848 return SelectCode(N);
851 // Build Rd = Rd' + asr(Rs, Rt). The machine constraints will ensure that
852 // Rd and Rd' are assigned to the same register
853 SDNode* Result = CurDAG->getMachineNode(Hexagon::S2_asr_r_r_acc, dl, MVT::i32,
856 Src1->getOperand(1));
857 ReplaceUses(N, Result);
863 // Map the following, where possible.
864 // AND/FABS -> clrbit
866 // XOR/FNEG ->toggle_bit.
868 SDNode *HexagonDAGToDAGISel::SelectBitOp(SDNode *N) {
870 EVT ValueVT = N->getValueType(0);
872 // We handle only 32 and 64-bit bit ops.
873 if (!(ValueVT == MVT::i32 || ValueVT == MVT::i64 ||
874 ValueVT == MVT::f32 || ValueVT == MVT::f64))
875 return SelectCode(N);
877 // We handly only fabs and fneg for V5.
878 unsigned Opc = N->getOpcode();
879 if ((Opc == ISD::FABS || Opc == ISD::FNEG) && !HST.hasV5TOps())
880 return SelectCode(N);
883 if (Opc != ISD::FABS && Opc != ISD::FNEG) {
884 if (N->getOperand(1).getOpcode() == ISD::Constant)
885 Val = cast<ConstantSDNode>((N)->getOperand(1))->getSExtValue();
887 return SelectCode(N);
890 if (Opc == ISD::AND) {
891 if (((ValueVT == MVT::i32) &&
892 (!((Val & 0x80000000) || (Val & 0x7fffffff)))) ||
893 ((ValueVT == MVT::i64) &&
894 (!((Val & 0x8000000000000000) || (Val & 0x7fffffff)))))
895 // If it's simple AND, do the normal op.
896 return SelectCode(N);
901 // If OR or AND is being fed by shl, srl and, sra don't do this change,
902 // because Hexagon provide |= &= on shl, srl, and sra.
903 // Traverse the DAG to see if there is shl, srl and sra.
904 if (Opc == ISD::OR || Opc == ISD::AND) {
905 switch (N->getOperand(0)->getOpcode()) {
910 return SelectCode(N);
914 // Make sure it's power of 2.
916 if (Opc != ISD::FABS && Opc != ISD::FNEG) {
917 if (((ValueVT == MVT::i32) && !isPowerOf2_32(Val)) ||
918 ((ValueVT == MVT::i64) && !isPowerOf2_64(Val)))
919 return SelectCode(N);
921 // Get the bit position.
927 // For fabs and fneg, it's always the 31st bit.
932 // Set the right opcode for bitwise operations.
934 default: llvm_unreachable("Only bit-wise/abs/neg operations are allowed.");
937 BitOpc = Hexagon::S2_clrbit_i;
940 BitOpc = Hexagon::S2_setbit_i;
944 BitOpc = Hexagon::S2_togglebit_i;
949 // Get the right SDVal for the opcode.
950 SDValue SDVal = CurDAG->getTargetConstant(bitpos, MVT::i32);
952 if (ValueVT == MVT::i32 || ValueVT == MVT::f32) {
953 Result = CurDAG->getMachineNode(BitOpc, dl, ValueVT,
954 N->getOperand(0), SDVal);
956 // 64-bit gymnastic to use REG_SEQUENCE. But it's worth it.
958 if (ValueVT == MVT::i64)
959 SubValueVT = MVT::i32;
961 SubValueVT = MVT::f32;
963 SDNode *Reg = N->getOperand(0).getNode();
964 SDValue RegClass = CurDAG->getTargetConstant(Hexagon::DoubleRegsRegClassID,
967 SDValue SubregHiIdx = CurDAG->getTargetConstant(Hexagon::subreg_hireg,
969 SDValue SubregLoIdx = CurDAG->getTargetConstant(Hexagon::subreg_loreg,
972 SDValue SubregHI = CurDAG->getTargetExtractSubreg(Hexagon::subreg_hireg, dl,
973 MVT::i32, SDValue(Reg, 0));
975 SDValue SubregLO = CurDAG->getTargetExtractSubreg(Hexagon::subreg_loreg, dl,
976 MVT::i32, SDValue(Reg, 0));
978 // Clear/set/toggle hi or lo registers depending on the bit position.
979 if (SubValueVT != MVT::f32 && bitpos < 32) {
980 SDNode *Result0 = CurDAG->getMachineNode(BitOpc, dl, SubValueVT,
982 const SDValue Ops[] = { RegClass, SubregHI, SubregHiIdx,
983 SDValue(Result0, 0), SubregLoIdx };
984 Result = CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE,
987 if (Opc != ISD::FABS && Opc != ISD::FNEG)
988 SDVal = CurDAG->getTargetConstant(bitpos-32, MVT::i32);
989 SDNode *Result0 = CurDAG->getMachineNode(BitOpc, dl, SubValueVT,
991 const SDValue Ops[] = { RegClass, SDValue(Result0, 0), SubregHiIdx,
992 SubregLO, SubregLoIdx };
993 Result = CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE,
998 ReplaceUses(N, Result);
1003 SDNode *HexagonDAGToDAGISel::SelectFrameIndex(SDNode *N) {
1004 int FX = cast<FrameIndexSDNode>(N)->getIndex();
1005 SDValue FI = CurDAG->getTargetFrameIndex(FX, MVT::i32);
1006 SDValue Zero = CurDAG->getTargetConstant(0, MVT::i32);
1009 SDNode *R = CurDAG->getMachineNode(Hexagon::TFR_FI, DL, MVT::i32, FI, Zero);
1011 if (N->getHasDebugValue())
1012 CurDAG->TransferDbgValues(SDValue(N, 0), SDValue(R, 0));
1017 SDNode *HexagonDAGToDAGISel::Select(SDNode *N) {
1018 if (N->isMachineOpcode()) {
1020 return nullptr; // Already selected.
1023 switch (N->getOpcode()) {
1025 return SelectConstant(N);
1027 case ISD::ConstantFP:
1028 return SelectConstantFP(N);
1030 case ISD::FrameIndex:
1031 return SelectFrameIndex(N);
1034 return SelectAdd(N);
1037 return SelectSHL(N);
1040 return SelectLoad(N);
1043 return SelectStore(N);
1046 return SelectMul(N);
1053 return SelectBitOp(N);
1055 case ISD::ZERO_EXTEND:
1056 return SelectZeroExtend(N);
1058 case ISD::INTRINSIC_WO_CHAIN:
1059 return SelectIntrinsicWOChain(N);
1062 return SelectCode(N);
1066 bool HexagonDAGToDAGISel::
1067 SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID,
1068 std::vector<SDValue> &OutOps) {
1069 SDValue Inp = Op, Res;
1071 switch (ConstraintID) {
1074 case InlineAsm::Constraint_i:
1075 case InlineAsm::Constraint_o: // Offsetable.
1076 case InlineAsm::Constraint_v: // Not offsetable.
1077 case InlineAsm::Constraint_m: // Memory.
1078 if (SelectAddrFI(Inp, Res))
1079 OutOps.push_back(Res);
1081 OutOps.push_back(Inp);
1085 OutOps.push_back(CurDAG->getTargetConstant(0, MVT::i32));
1089 void HexagonDAGToDAGISel::PreprocessISelDAG() {
1090 SelectionDAG &DAG = *CurDAG;
1091 std::vector<SDNode*> Nodes;
1092 for (auto I = DAG.allnodes_begin(), E = DAG.allnodes_end(); I != E; ++I)
1095 // Simplify: (or (select c x 0) z) -> (select c (or x z) z)
1096 // (or (select c 0 y) z) -> (select c z (or y z))
1097 // This may not be the right thing for all targets, so do it here.
1098 for (auto I: Nodes) {
1099 if (I->getOpcode() != ISD::OR)
1102 auto IsZero = [] (const SDValue &V) -> bool {
1103 if (ConstantSDNode *SC = dyn_cast<ConstantSDNode>(V.getNode()))
1104 return SC->isNullValue();
1107 auto IsSelect0 = [IsZero] (const SDValue &Op) -> bool {
1108 if (Op.getOpcode() != ISD::SELECT)
1110 return IsZero(Op.getOperand(1)) || IsZero(Op.getOperand(2));
1113 SDValue N0 = I->getOperand(0), N1 = I->getOperand(1);
1114 EVT VT = I->getValueType(0);
1115 bool SelN0 = IsSelect0(N0);
1116 SDValue SOp = SelN0 ? N0 : N1;
1117 SDValue VOp = SelN0 ? N1 : N0;
1119 if (SOp.getOpcode() == ISD::SELECT && SOp.getNode()->hasOneUse()) {
1120 SDValue SC = SOp.getOperand(0);
1121 SDValue SX = SOp.getOperand(1);
1122 SDValue SY = SOp.getOperand(2);
1125 SDValue NewOr = DAG.getNode(ISD::OR, DLS, VT, SX, VOp);
1126 SDValue NewSel = DAG.getNode(ISD::SELECT, DLS, VT, SC, NewOr, VOp);
1127 DAG.ReplaceAllUsesWith(I, NewSel.getNode());
1128 } else if (IsZero(SX)) {
1129 SDValue NewOr = DAG.getNode(ISD::OR, DLS, VT, SY, VOp);
1130 SDValue NewSel = DAG.getNode(ISD::SELECT, DLS, VT, SC, VOp, NewOr);
1131 DAG.ReplaceAllUsesWith(I, NewSel.getNode());
1138 bool HexagonDAGToDAGISel::SelectAddrFI(SDValue& N, SDValue &R) {
1139 if (N.getOpcode() != ISD::FrameIndex)
1141 FrameIndexSDNode *FX = cast<FrameIndexSDNode>(N);
1142 R = CurDAG->getTargetFrameIndex(FX->getIndex(), MVT::i32);
1146 inline bool HexagonDAGToDAGISel::SelectAddrGA(SDValue &N, SDValue &R) {
1147 return SelectGlobalAddress(N, R, false);
1150 inline bool HexagonDAGToDAGISel::SelectAddrGP(SDValue &N, SDValue &R) {
1151 return SelectGlobalAddress(N, R, true);
1154 bool HexagonDAGToDAGISel::SelectGlobalAddress(SDValue &N, SDValue &R,
1156 switch (N.getOpcode()) {
1158 SDValue N0 = N.getOperand(0);
1159 SDValue N1 = N.getOperand(1);
1160 unsigned GAOpc = N0.getOpcode();
1161 if (UseGP && GAOpc != HexagonISD::CONST32_GP)
1163 if (!UseGP && GAOpc != HexagonISD::CONST32)
1165 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N1)) {
1166 SDValue Addr = N0.getOperand(0);
1167 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Addr)) {
1168 if (GA->getOpcode() == ISD::TargetGlobalAddress) {
1169 uint64_t NewOff = GA->getOffset() + (uint64_t)Const->getSExtValue();
1170 R = CurDAG->getTargetGlobalAddress(GA->getGlobal(), SDLoc(Const),
1171 N.getValueType(), NewOff);
1178 case HexagonISD::CONST32:
1179 // The operand(0) of CONST32 is TargetGlobalAddress, which is what we
1180 // want in the instruction.
1182 R = N.getOperand(0);
1184 case HexagonISD::CONST32_GP:
1186 R = N.getOperand(0);
1195 bool HexagonDAGToDAGISel::isValueExtension(const SDValue &Val,
1196 unsigned FromBits, SDValue &Src) {
1197 unsigned Opc = Val.getOpcode();
1199 case ISD::SIGN_EXTEND:
1200 case ISD::ZERO_EXTEND:
1201 case ISD::ANY_EXTEND: {
1202 SDValue const &Op0 = Val.getOperand(0);
1203 EVT T = Op0.getValueType();
1204 if (T.isInteger() && T.getSizeInBits() == FromBits) {
1210 case ISD::SIGN_EXTEND_INREG:
1211 case ISD::AssertSext:
1212 case ISD::AssertZext:
1213 if (Val.getOperand(0).getValueType().isInteger()) {
1214 VTSDNode *T = cast<VTSDNode>(Val.getOperand(1));
1215 if (T->getVT().getSizeInBits() == FromBits) {
1216 Src = Val.getOperand(0);
1222 // Check if this is an AND with "FromBits" of lower bits set to 1.
1223 uint64_t FromMask = (1 << FromBits) - 1;
1224 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(0))) {
1225 if (C->getZExtValue() == FromMask) {
1226 Src = Val.getOperand(1);
1230 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(1))) {
1231 if (C->getZExtValue() == FromMask) {
1232 Src = Val.getOperand(0);
1240 // OR/XOR with the lower "FromBits" bits set to 0.
1241 uint64_t FromMask = (1 << FromBits) - 1;
1242 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(0))) {
1243 if ((C->getZExtValue() & FromMask) == 0) {
1244 Src = Val.getOperand(1);
1248 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(1))) {
1249 if ((C->getZExtValue() & FromMask) == 0) {
1250 Src = Val.getOperand(0);