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);
96 // XformMskToBitPosU5Imm - Returns the bit position which
97 // the single bit 32 bit mask represents.
98 // Used in Clr and Set bit immediate memops.
99 SDValue XformMskToBitPosU5Imm(uint32_t Imm) {
101 bitPos = Log2_32(Imm);
102 assert(bitPos >= 0 && bitPos < 32 &&
103 "Constant out of range for 32 BitPos Memops");
104 return CurDAG->getTargetConstant(bitPos, MVT::i32);
107 // XformMskToBitPosU4Imm - Returns the bit position which the single-bit
108 // 16 bit mask represents. Used in Clr and Set bit immediate memops.
109 SDValue XformMskToBitPosU4Imm(uint16_t Imm) {
110 return XformMskToBitPosU5Imm(Imm);
113 // XformMskToBitPosU3Imm - Returns the bit position which the single-bit
114 // 8 bit mask represents. Used in Clr and Set bit immediate memops.
115 SDValue XformMskToBitPosU3Imm(uint8_t Imm) {
116 return XformMskToBitPosU5Imm(Imm);
119 // Return true if there is exactly one bit set in V, i.e., if V is one of the
120 // following integers: 2^0, 2^1, ..., 2^31.
121 bool ImmIsSingleBit(uint32_t v) const {
122 return isPowerOf2_32(v);
125 // XformM5ToU5Imm - Return a target constant with the specified value, of
126 // type i32 where the negative literal is transformed into a positive literal
127 // for use in -= memops.
128 inline SDValue XformM5ToU5Imm(signed Imm) {
129 assert( (Imm >= -31 && Imm <= -1) && "Constant out of range for Memops");
130 return CurDAG->getTargetConstant( - Imm, MVT::i32);
133 // XformU7ToU7M1Imm - Return a target constant decremented by 1, in range
134 // [1..128], used in cmpb.gtu instructions.
135 inline SDValue XformU7ToU7M1Imm(signed Imm) {
136 assert((Imm >= 1 && Imm <= 128) && "Constant out of range for cmpb op");
137 return CurDAG->getTargetConstant(Imm - 1, MVT::i8);
140 // XformS8ToS8M1Imm - Return a target constant decremented by 1.
141 inline SDValue XformSToSM1Imm(signed Imm) {
142 return CurDAG->getTargetConstant(Imm - 1, MVT::i32);
145 // XformU8ToU8M1Imm - Return a target constant decremented by 1.
146 inline SDValue XformUToUM1Imm(unsigned Imm) {
147 assert((Imm >= 1) && "Cannot decrement unsigned int less than 1");
148 return CurDAG->getTargetConstant(Imm - 1, MVT::i32);
151 // XformSToSM2Imm - Return a target constant decremented by 2.
152 inline SDValue XformSToSM2Imm(unsigned Imm) {
153 return CurDAG->getTargetConstant(Imm - 2, MVT::i32);
156 // XformSToSM3Imm - Return a target constant decremented by 3.
157 inline SDValue XformSToSM3Imm(unsigned Imm) {
158 return CurDAG->getTargetConstant(Imm - 3, MVT::i32);
161 // Include the pieces autogenerated from the target description.
162 #include "HexagonGenDAGISel.inc"
165 bool isValueExtension(const SDValue &Val, unsigned FromBits, SDValue &Src);
166 }; // end HexagonDAGToDAGISel
167 } // end anonymous namespace
170 /// createHexagonISelDag - This pass converts a legalized DAG into a
171 /// Hexagon-specific DAG, ready for instruction scheduling.
174 FunctionPass *createHexagonISelDag(HexagonTargetMachine &TM,
175 CodeGenOpt::Level OptLevel) {
176 return new HexagonDAGToDAGISel(TM, OptLevel);
180 static void initializePassOnce(PassRegistry &Registry) {
181 const char *Name = "Hexagon DAG->DAG Pattern Instruction Selection";
182 PassInfo *PI = new PassInfo(Name, "hexagon-isel",
183 &SelectionDAGISel::ID, nullptr, false, false);
184 Registry.registerPass(*PI, true);
187 void llvm::initializeHexagonDAGToDAGISelPass(PassRegistry &Registry) {
188 CALL_ONCE_INITIALIZATION(initializePassOnce)
192 // Intrinsics that return a a predicate.
193 static unsigned doesIntrinsicReturnPredicate(unsigned ID)
198 case Intrinsic::hexagon_C2_cmpeq:
199 case Intrinsic::hexagon_C2_cmpgt:
200 case Intrinsic::hexagon_C2_cmpgtu:
201 case Intrinsic::hexagon_C2_cmpgtup:
202 case Intrinsic::hexagon_C2_cmpgtp:
203 case Intrinsic::hexagon_C2_cmpeqp:
204 case Intrinsic::hexagon_C2_bitsset:
205 case Intrinsic::hexagon_C2_bitsclr:
206 case Intrinsic::hexagon_C2_cmpeqi:
207 case Intrinsic::hexagon_C2_cmpgti:
208 case Intrinsic::hexagon_C2_cmpgtui:
209 case Intrinsic::hexagon_C2_cmpgei:
210 case Intrinsic::hexagon_C2_cmpgeui:
211 case Intrinsic::hexagon_C2_cmplt:
212 case Intrinsic::hexagon_C2_cmpltu:
213 case Intrinsic::hexagon_C2_bitsclri:
214 case Intrinsic::hexagon_C2_and:
215 case Intrinsic::hexagon_C2_or:
216 case Intrinsic::hexagon_C2_xor:
217 case Intrinsic::hexagon_C2_andn:
218 case Intrinsic::hexagon_C2_not:
219 case Intrinsic::hexagon_C2_orn:
220 case Intrinsic::hexagon_C2_pxfer_map:
221 case Intrinsic::hexagon_C2_any8:
222 case Intrinsic::hexagon_C2_all8:
223 case Intrinsic::hexagon_A2_vcmpbeq:
224 case Intrinsic::hexagon_A2_vcmpbgtu:
225 case Intrinsic::hexagon_A2_vcmpheq:
226 case Intrinsic::hexagon_A2_vcmphgt:
227 case Intrinsic::hexagon_A2_vcmphgtu:
228 case Intrinsic::hexagon_A2_vcmpweq:
229 case Intrinsic::hexagon_A2_vcmpwgt:
230 case Intrinsic::hexagon_A2_vcmpwgtu:
231 case Intrinsic::hexagon_C2_tfrrp:
232 case Intrinsic::hexagon_S2_tstbit_i:
233 case Intrinsic::hexagon_S2_tstbit_r:
238 SDNode *HexagonDAGToDAGISel::SelectIndexedLoadSignExtend64(LoadSDNode *LD,
241 SDValue Chain = LD->getChain();
242 EVT LoadedVT = LD->getMemoryVT();
243 SDValue Base = LD->getBasePtr();
244 SDValue Offset = LD->getOffset();
245 SDNode *OffsetNode = Offset.getNode();
246 int32_t Val = cast<ConstantSDNode>(OffsetNode)->getSExtValue();
248 const HexagonInstrInfo &TII = *HST.getInstrInfo();
249 if (TII.isValidAutoIncImm(LoadedVT, Val)) {
250 SDValue TargetConst = CurDAG->getTargetConstant(Val, MVT::i32);
251 SDNode *Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::i32, MVT::i32,
252 MVT::Other, Base, TargetConst,
254 SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A2_sxtw, dl, MVT::i64,
255 SDValue(Result_1, 0));
256 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
257 MemOp[0] = LD->getMemOperand();
258 cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
259 const SDValue Froms[] = { SDValue(LD, 0),
262 const SDValue Tos[] = { SDValue(Result_2, 0),
263 SDValue(Result_1, 1),
264 SDValue(Result_1, 2) };
265 ReplaceUses(Froms, Tos, 3);
269 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
270 SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
271 SDNode *Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::i32, MVT::Other,
272 Base, TargetConst0, Chain);
273 SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A2_sxtw, dl, MVT::i64,
274 SDValue(Result_1, 0));
275 SDNode* Result_3 = CurDAG->getMachineNode(Hexagon::A2_addi, dl, MVT::i32,
276 Base, TargetConstVal,
277 SDValue(Result_1, 1));
278 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
279 MemOp[0] = LD->getMemOperand();
280 cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
281 const SDValue Froms[] = { SDValue(LD, 0),
284 const SDValue Tos[] = { SDValue(Result_2, 0),
285 SDValue(Result_3, 0),
286 SDValue(Result_1, 1) };
287 ReplaceUses(Froms, Tos, 3);
292 SDNode *HexagonDAGToDAGISel::SelectIndexedLoadZeroExtend64(LoadSDNode *LD,
295 SDValue Chain = LD->getChain();
296 EVT LoadedVT = LD->getMemoryVT();
297 SDValue Base = LD->getBasePtr();
298 SDValue Offset = LD->getOffset();
299 SDNode *OffsetNode = Offset.getNode();
300 int32_t Val = cast<ConstantSDNode>(OffsetNode)->getSExtValue();
302 const HexagonInstrInfo &TII = *HST.getInstrInfo();
303 if (TII.isValidAutoIncImm(LoadedVT, Val)) {
304 SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
305 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
306 SDNode *Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::i32,
307 MVT::i32, MVT::Other, Base,
308 TargetConstVal, Chain);
309 SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A4_combineir, dl,
310 MVT::i64, MVT::Other,
312 SDValue(Result_1,0));
313 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
314 MemOp[0] = LD->getMemOperand();
315 cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
316 const SDValue Froms[] = { SDValue(LD, 0),
319 const SDValue Tos[] = { SDValue(Result_2, 0),
320 SDValue(Result_1, 1),
321 SDValue(Result_1, 2) };
322 ReplaceUses(Froms, Tos, 3);
326 // Generate an indirect load.
327 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
328 SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
329 SDNode *Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::i32,
330 MVT::Other, Base, TargetConst0,
332 SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A4_combineir, dl,
333 MVT::i64, MVT::Other,
335 SDValue(Result_1,0));
336 // Add offset to base.
337 SDNode* Result_3 = CurDAG->getMachineNode(Hexagon::A2_addi, dl, MVT::i32,
338 Base, TargetConstVal,
339 SDValue(Result_1, 1));
340 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
341 MemOp[0] = LD->getMemOperand();
342 cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
343 const SDValue Froms[] = { SDValue(LD, 0),
346 const SDValue Tos[] = { SDValue(Result_2, 0), // Load value.
347 SDValue(Result_3, 0), // New address.
348 SDValue(Result_1, 1) };
349 ReplaceUses(Froms, Tos, 3);
354 SDNode *HexagonDAGToDAGISel::SelectIndexedLoad(LoadSDNode *LD, SDLoc dl) {
355 SDValue Chain = LD->getChain();
356 SDValue Base = LD->getBasePtr();
357 SDValue Offset = LD->getOffset();
358 SDNode *OffsetNode = Offset.getNode();
359 // Get the constant value.
360 int32_t Val = cast<ConstantSDNode>(OffsetNode)->getSExtValue();
361 EVT LoadedVT = LD->getMemoryVT();
364 // Check for zero extended loads. Treat any-extend loads as zero extended
366 ISD::LoadExtType ExtType = LD->getExtensionType();
367 bool IsZeroExt = (ExtType == ISD::ZEXTLOAD || ExtType == ISD::EXTLOAD);
369 // Figure out the opcode.
370 const HexagonInstrInfo &TII = *HST.getInstrInfo();
371 if (LoadedVT == MVT::i64) {
372 if (TII.isValidAutoIncImm(LoadedVT, Val))
373 Opcode = Hexagon::L2_loadrd_pi;
375 Opcode = Hexagon::L2_loadrd_io;
376 } else if (LoadedVT == MVT::i32) {
377 if (TII.isValidAutoIncImm(LoadedVT, Val))
378 Opcode = Hexagon::L2_loadri_pi;
380 Opcode = Hexagon::L2_loadri_io;
381 } else if (LoadedVT == MVT::i16) {
382 if (TII.isValidAutoIncImm(LoadedVT, Val))
383 Opcode = IsZeroExt ? Hexagon::L2_loadruh_pi : Hexagon::L2_loadrh_pi;
385 Opcode = IsZeroExt ? Hexagon::L2_loadruh_io : Hexagon::L2_loadrh_io;
386 } else if (LoadedVT == MVT::i8) {
387 if (TII.isValidAutoIncImm(LoadedVT, Val))
388 Opcode = IsZeroExt ? Hexagon::L2_loadrub_pi : Hexagon::L2_loadrb_pi;
390 Opcode = IsZeroExt ? Hexagon::L2_loadrub_io : Hexagon::L2_loadrb_io;
392 llvm_unreachable("unknown memory type");
394 // For zero extended i64 loads, we need to add combine instructions.
395 if (LD->getValueType(0) == MVT::i64 && IsZeroExt)
396 return SelectIndexedLoadZeroExtend64(LD, Opcode, dl);
397 // Handle sign extended i64 loads.
398 if (LD->getValueType(0) == MVT::i64 && ExtType == ISD::SEXTLOAD)
399 return SelectIndexedLoadSignExtend64(LD, Opcode, dl);
401 if (TII.isValidAutoIncImm(LoadedVT, Val)) {
402 SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
403 SDNode* Result = CurDAG->getMachineNode(Opcode, dl,
405 MVT::i32, MVT::Other, Base,
406 TargetConstVal, Chain);
407 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
408 MemOp[0] = LD->getMemOperand();
409 cast<MachineSDNode>(Result)->setMemRefs(MemOp, MemOp + 1);
410 const SDValue Froms[] = { SDValue(LD, 0),
414 const SDValue Tos[] = { SDValue(Result, 0),
418 ReplaceUses(Froms, Tos, 3);
421 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
422 SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
423 SDNode* Result_1 = CurDAG->getMachineNode(Opcode, dl,
425 MVT::Other, Base, TargetConst0,
427 SDNode* Result_2 = CurDAG->getMachineNode(Hexagon::A2_addi, dl, MVT::i32,
428 Base, TargetConstVal,
429 SDValue(Result_1, 1));
430 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
431 MemOp[0] = LD->getMemOperand();
432 cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
433 const SDValue Froms[] = { SDValue(LD, 0),
437 const SDValue Tos[] = { SDValue(Result_1, 0),
438 SDValue(Result_2, 0),
441 ReplaceUses(Froms, Tos, 3);
447 SDNode *HexagonDAGToDAGISel::SelectLoad(SDNode *N) {
450 LoadSDNode *LD = cast<LoadSDNode>(N);
451 ISD::MemIndexedMode AM = LD->getAddressingMode();
453 // Handle indexed loads.
454 if (AM != ISD::UNINDEXED) {
455 result = SelectIndexedLoad(LD, dl);
457 result = SelectCode(LD);
464 SDNode *HexagonDAGToDAGISel::SelectIndexedStore(StoreSDNode *ST, SDLoc dl) {
465 SDValue Chain = ST->getChain();
466 SDValue Base = ST->getBasePtr();
467 SDValue Offset = ST->getOffset();
468 SDValue Value = ST->getValue();
469 SDNode *OffsetNode = Offset.getNode();
470 // Get the constant value.
471 int32_t Val = cast<ConstantSDNode>(OffsetNode)->getSExtValue();
472 EVT StoredVT = ST->getMemoryVT();
473 EVT ValueVT = Value.getValueType();
475 // Offset value must be within representable range
476 // and must have correct alignment properties.
477 const HexagonInstrInfo &TII = *HST.getInstrInfo();
478 if (TII.isValidAutoIncImm(StoredVT, Val)) {
481 // Figure out the post inc version of opcode.
482 if (StoredVT == MVT::i64) Opcode = Hexagon::S2_storerd_pi;
483 else if (StoredVT == MVT::i32) Opcode = Hexagon::S2_storeri_pi;
484 else if (StoredVT == MVT::i16) Opcode = Hexagon::S2_storerh_pi;
485 else if (StoredVT == MVT::i8) Opcode = Hexagon::S2_storerb_pi;
486 else llvm_unreachable("unknown memory type");
488 if (ST->isTruncatingStore() && ValueVT.getSizeInBits() == 64) {
489 assert(StoredVT.getSizeInBits() < 64 && "Not a truncating store");
490 Value = CurDAG->getTargetExtractSubreg(Hexagon::subreg_loreg,
491 dl, MVT::i32, Value);
493 SDValue Ops[] = {Base, CurDAG->getTargetConstant(Val, MVT::i32), Value,
495 // Build post increment store.
496 SDNode* Result = CurDAG->getMachineNode(Opcode, dl, MVT::i32,
498 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
499 MemOp[0] = ST->getMemOperand();
500 cast<MachineSDNode>(Result)->setMemRefs(MemOp, MemOp + 1);
502 ReplaceUses(ST, Result);
503 ReplaceUses(SDValue(ST,1), SDValue(Result,1));
507 // Note: Order of operands matches the def of instruction:
509 // : STInst<(outs), (ins IntRegs:$base, imm:$offset, DoubleRegs:$src1), ...
510 // and it differs for POST_ST* for instance.
511 SDValue Ops[] = { Base, CurDAG->getTargetConstant(0, MVT::i32), Value,
515 // Figure out the opcode.
516 if (StoredVT == MVT::i64) Opcode = Hexagon::S2_storerd_io;
517 else if (StoredVT == MVT::i32) Opcode = Hexagon::S2_storeri_io;
518 else if (StoredVT == MVT::i16) Opcode = Hexagon::S2_storerh_io;
519 else if (StoredVT == MVT::i8) Opcode = Hexagon::S2_storerb_io;
520 else llvm_unreachable("unknown memory type");
522 // Build regular store.
523 SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
524 SDNode* Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::Other, Ops);
525 // Build splitted incriment instruction.
526 SDNode* Result_2 = CurDAG->getMachineNode(Hexagon::A2_addi, dl, MVT::i32,
529 SDValue(Result_1, 0));
530 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
531 MemOp[0] = ST->getMemOperand();
532 cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
534 ReplaceUses(SDValue(ST,0), SDValue(Result_2,0));
535 ReplaceUses(SDValue(ST,1), SDValue(Result_1,0));
539 SDNode *HexagonDAGToDAGISel::SelectStore(SDNode *N) {
541 StoreSDNode *ST = cast<StoreSDNode>(N);
542 ISD::MemIndexedMode AM = ST->getAddressingMode();
544 // Handle indexed stores.
545 if (AM != ISD::UNINDEXED) {
546 return SelectIndexedStore(ST, dl);
549 return SelectCode(ST);
552 SDNode *HexagonDAGToDAGISel::SelectMul(SDNode *N) {
556 // %conv.i = sext i32 %tmp1 to i64
557 // %conv2.i = sext i32 %add to i64
558 // %mul.i = mul nsw i64 %conv2.i, %conv.i
560 // --- match with the following ---
562 // %mul.i = mpy (%tmp1, %add)
565 if (N->getValueType(0) == MVT::i64) {
566 // Shifting a i64 signed multiply.
567 SDValue MulOp0 = N->getOperand(0);
568 SDValue MulOp1 = N->getOperand(1);
573 // Handle sign_extend and sextload.
574 if (MulOp0.getOpcode() == ISD::SIGN_EXTEND) {
575 SDValue Sext0 = MulOp0.getOperand(0);
576 if (Sext0.getNode()->getValueType(0) != MVT::i32) {
577 return SelectCode(N);
581 } else if (MulOp0.getOpcode() == ISD::LOAD) {
582 LoadSDNode *LD = cast<LoadSDNode>(MulOp0.getNode());
583 if (LD->getMemoryVT() != MVT::i32 ||
584 LD->getExtensionType() != ISD::SEXTLOAD ||
585 LD->getAddressingMode() != ISD::UNINDEXED) {
586 return SelectCode(N);
589 SDValue Chain = LD->getChain();
590 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
591 OP0 = SDValue(CurDAG->getMachineNode(Hexagon::L2_loadri_io, dl, MVT::i32,
593 LD->getBasePtr(), TargetConst0,
596 return SelectCode(N);
599 // Same goes for the second operand.
600 if (MulOp1.getOpcode() == ISD::SIGN_EXTEND) {
601 SDValue Sext1 = MulOp1.getOperand(0);
602 if (Sext1.getNode()->getValueType(0) != MVT::i32) {
603 return SelectCode(N);
607 } else if (MulOp1.getOpcode() == ISD::LOAD) {
608 LoadSDNode *LD = cast<LoadSDNode>(MulOp1.getNode());
609 if (LD->getMemoryVT() != MVT::i32 ||
610 LD->getExtensionType() != ISD::SEXTLOAD ||
611 LD->getAddressingMode() != ISD::UNINDEXED) {
612 return SelectCode(N);
615 SDValue Chain = LD->getChain();
616 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
617 OP1 = SDValue(CurDAG->getMachineNode(Hexagon::L2_loadri_io, dl, MVT::i32,
619 LD->getBasePtr(), TargetConst0,
622 return SelectCode(N);
625 // Generate a mpy instruction.
626 SDNode *Result = CurDAG->getMachineNode(Hexagon::M2_dpmpyss_s0, dl, MVT::i64,
628 ReplaceUses(N, Result);
632 return SelectCode(N);
635 SDNode *HexagonDAGToDAGISel::SelectSHL(SDNode *N) {
637 if (N->getValueType(0) == MVT::i32) {
638 SDValue Shl_0 = N->getOperand(0);
639 SDValue Shl_1 = N->getOperand(1);
641 if (Shl_1.getOpcode() == ISD::Constant) {
642 if (Shl_0.getOpcode() == ISD::MUL) {
643 SDValue Mul_0 = Shl_0.getOperand(0); // Val
644 SDValue Mul_1 = Shl_0.getOperand(1); // Const
645 // RHS of mul is const.
646 if (Mul_1.getOpcode() == ISD::Constant) {
648 cast<ConstantSDNode>(Shl_1.getNode())->getSExtValue();
650 cast<ConstantSDNode>(Mul_1.getNode())->getSExtValue();
651 int32_t ValConst = MulConst << ShlConst;
652 SDValue Val = CurDAG->getTargetConstant(ValConst,
654 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Val.getNode()))
655 if (isInt<9>(CN->getSExtValue())) {
657 CurDAG->getMachineNode(Hexagon::M2_mpysmi, dl,
658 MVT::i32, Mul_0, Val);
659 ReplaceUses(N, Result);
664 } else if (Shl_0.getOpcode() == ISD::SUB) {
665 SDValue Sub_0 = Shl_0.getOperand(0); // Const 0
666 SDValue Sub_1 = Shl_0.getOperand(1); // Val
667 if (Sub_0.getOpcode() == ISD::Constant) {
669 cast<ConstantSDNode>(Sub_0.getNode())->getSExtValue();
671 if (Sub_1.getOpcode() == ISD::SHL) {
672 SDValue Shl2_0 = Sub_1.getOperand(0); // Val
673 SDValue Shl2_1 = Sub_1.getOperand(1); // Const
674 if (Shl2_1.getOpcode() == ISD::Constant) {
676 cast<ConstantSDNode>(Shl_1.getNode())->getSExtValue();
678 cast<ConstantSDNode>(Shl2_1.getNode())->getSExtValue();
679 int32_t ValConst = 1 << (ShlConst+Shl2Const);
680 SDValue Val = CurDAG->getTargetConstant(-ValConst, MVT::i32);
681 if (ConstantSDNode *CN =
682 dyn_cast<ConstantSDNode>(Val.getNode()))
683 if (isInt<9>(CN->getSExtValue())) {
685 CurDAG->getMachineNode(Hexagon::M2_mpysmi, dl, MVT::i32,
687 ReplaceUses(N, Result);
697 return SelectCode(N);
702 // If there is an zero_extend followed an intrinsic in DAG (this means - the
703 // result of the intrinsic is predicate); convert the zero_extend to
704 // transfer instruction.
706 // Zero extend -> transfer is lowered here. Otherwise, zero_extend will be
707 // converted into a MUX as predicate registers defined as 1 bit in the
708 // compiler. Architecture defines them as 8-bit registers.
709 // We want to preserve all the lower 8-bits and, not just 1 LSB bit.
711 SDNode *HexagonDAGToDAGISel::SelectZeroExtend(SDNode *N) {
713 SDNode *IsIntrinsic = N->getOperand(0).getNode();
714 if ((IsIntrinsic->getOpcode() == ISD::INTRINSIC_WO_CHAIN)) {
716 cast<ConstantSDNode>(IsIntrinsic->getOperand(0))->getZExtValue();
717 if (doesIntrinsicReturnPredicate(ID)) {
718 // Now we need to differentiate target data types.
719 if (N->getValueType(0) == MVT::i64) {
720 // Convert the zero_extend to Rs = Pd followed by A2_combinew(0,Rs).
721 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
722 SDNode *Result_1 = CurDAG->getMachineNode(Hexagon::C2_tfrpr, dl,
724 SDValue(IsIntrinsic, 0));
725 SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A2_tfrsi, dl,
728 SDNode *Result_3 = CurDAG->getMachineNode(Hexagon::A2_combinew, dl,
729 MVT::i64, MVT::Other,
730 SDValue(Result_2, 0),
731 SDValue(Result_1, 0));
732 ReplaceUses(N, Result_3);
735 if (N->getValueType(0) == MVT::i32) {
736 // Convert the zero_extend to Rs = Pd
737 SDNode* RsPd = CurDAG->getMachineNode(Hexagon::C2_tfrpr, dl,
739 SDValue(IsIntrinsic, 0));
740 ReplaceUses(N, RsPd);
743 llvm_unreachable("Unexpected value type");
746 return SelectCode(N);
750 // Checking for intrinsics which have predicate registers as operand(s)
751 // and lowering to the actual intrinsic.
753 SDNode *HexagonDAGToDAGISel::SelectIntrinsicWOChain(SDNode *N) {
754 unsigned IID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
757 case Intrinsic::hexagon_S2_vsplatrb:
760 case Intrinsic::hexagon_S2_vsplatrh:
764 return SelectCode(N);
767 SDValue const &V = N->getOperand(1);
769 if (isValueExtension(V, Bits, U)) {
770 SDValue R = CurDAG->getNode(N->getOpcode(), SDLoc(N), N->getValueType(0),
771 N->getOperand(0), U);
772 return SelectCode(R.getNode());
774 return SelectCode(N);
778 // Map floating point constant values.
780 SDNode *HexagonDAGToDAGISel::SelectConstantFP(SDNode *N) {
782 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N);
783 APFloat APF = CN->getValueAPF();
784 if (N->getValueType(0) == MVT::f32) {
785 return CurDAG->getMachineNode(Hexagon::TFRI_f, dl, MVT::f32,
786 CurDAG->getTargetConstantFP(APF.convertToFloat(), MVT::f32));
788 else if (N->getValueType(0) == MVT::f64) {
789 return CurDAG->getMachineNode(Hexagon::CONST64_Float_Real, dl, MVT::f64,
790 CurDAG->getTargetConstantFP(APF.convertToDouble(), MVT::f64));
793 return SelectCode(N);
797 // Map predicate true (encoded as -1 in LLVM) to a XOR.
799 SDNode *HexagonDAGToDAGISel::SelectConstant(SDNode *N) {
801 if (N->getValueType(0) == MVT::i1) {
803 int32_t Val = cast<ConstantSDNode>(N)->getSExtValue();
805 // Create the IntReg = 1 node.
807 CurDAG->getMachineNode(Hexagon::A2_tfrsi, dl, MVT::i32,
808 CurDAG->getTargetConstant(0, MVT::i32));
811 SDNode* Pd = CurDAG->getMachineNode(Hexagon::C2_tfrrp, dl, MVT::i1,
812 SDValue(IntRegTFR, 0));
815 SDNode* NotPd = CurDAG->getMachineNode(Hexagon::C2_not, dl, MVT::i1,
819 Result = CurDAG->getMachineNode(Hexagon::C2_xor, dl, MVT::i1,
820 SDValue(Pd, 0), SDValue(NotPd, 0));
822 // We have just built:
824 // Pd = xor(not(Pd), Pd)
826 ReplaceUses(N, Result);
831 return SelectCode(N);
836 // Map add followed by a asr -> asr +=.
838 SDNode *HexagonDAGToDAGISel::SelectAdd(SDNode *N) {
840 if (N->getValueType(0) != MVT::i32) {
841 return SelectCode(N);
843 // Identify nodes of the form: add(asr(...)).
844 SDNode* Src1 = N->getOperand(0).getNode();
845 if (Src1->getOpcode() != ISD::SRA || !Src1->hasOneUse()
846 || Src1->getValueType(0) != MVT::i32) {
847 return SelectCode(N);
850 // Build Rd = Rd' + asr(Rs, Rt). The machine constraints will ensure that
851 // Rd and Rd' are assigned to the same register
852 SDNode* Result = CurDAG->getMachineNode(Hexagon::S2_asr_r_r_acc, dl, MVT::i32,
855 Src1->getOperand(1));
856 ReplaceUses(N, Result);
861 SDNode *HexagonDAGToDAGISel::SelectFrameIndex(SDNode *N) {
862 int FX = cast<FrameIndexSDNode>(N)->getIndex();
863 SDValue FI = CurDAG->getTargetFrameIndex(FX, MVT::i32);
864 SDValue Zero = CurDAG->getTargetConstant(0, MVT::i32);
867 SDNode *R = CurDAG->getMachineNode(Hexagon::TFR_FI, DL, MVT::i32, FI, Zero);
869 if (N->getHasDebugValue())
870 CurDAG->TransferDbgValues(SDValue(N, 0), SDValue(R, 0));
875 SDNode *HexagonDAGToDAGISel::Select(SDNode *N) {
876 if (N->isMachineOpcode()) {
878 return nullptr; // Already selected.
881 switch (N->getOpcode()) {
883 return SelectConstant(N);
885 case ISD::ConstantFP:
886 return SelectConstantFP(N);
888 case ISD::FrameIndex:
889 return SelectFrameIndex(N);
898 return SelectLoad(N);
901 return SelectStore(N);
906 case ISD::ZERO_EXTEND:
907 return SelectZeroExtend(N);
909 case ISD::INTRINSIC_WO_CHAIN:
910 return SelectIntrinsicWOChain(N);
913 return SelectCode(N);
917 bool HexagonDAGToDAGISel::
918 SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID,
919 std::vector<SDValue> &OutOps) {
920 SDValue Inp = Op, Res;
922 switch (ConstraintID) {
925 case InlineAsm::Constraint_i:
926 case InlineAsm::Constraint_o: // Offsetable.
927 case InlineAsm::Constraint_v: // Not offsetable.
928 case InlineAsm::Constraint_m: // Memory.
929 if (SelectAddrFI(Inp, Res))
930 OutOps.push_back(Res);
932 OutOps.push_back(Inp);
936 OutOps.push_back(CurDAG->getTargetConstant(0, MVT::i32));
940 void HexagonDAGToDAGISel::PreprocessISelDAG() {
941 SelectionDAG &DAG = *CurDAG;
942 std::vector<SDNode*> Nodes;
943 for (auto I = DAG.allnodes_begin(), E = DAG.allnodes_end(); I != E; ++I)
946 // Simplify: (or (select c x 0) z) -> (select c (or x z) z)
947 // (or (select c 0 y) z) -> (select c z (or y z))
948 // This may not be the right thing for all targets, so do it here.
949 for (auto I: Nodes) {
950 if (I->getOpcode() != ISD::OR)
953 auto IsZero = [] (const SDValue &V) -> bool {
954 if (ConstantSDNode *SC = dyn_cast<ConstantSDNode>(V.getNode()))
955 return SC->isNullValue();
958 auto IsSelect0 = [IsZero] (const SDValue &Op) -> bool {
959 if (Op.getOpcode() != ISD::SELECT)
961 return IsZero(Op.getOperand(1)) || IsZero(Op.getOperand(2));
964 SDValue N0 = I->getOperand(0), N1 = I->getOperand(1);
965 EVT VT = I->getValueType(0);
966 bool SelN0 = IsSelect0(N0);
967 SDValue SOp = SelN0 ? N0 : N1;
968 SDValue VOp = SelN0 ? N1 : N0;
970 if (SOp.getOpcode() == ISD::SELECT && SOp.getNode()->hasOneUse()) {
971 SDValue SC = SOp.getOperand(0);
972 SDValue SX = SOp.getOperand(1);
973 SDValue SY = SOp.getOperand(2);
976 SDValue NewOr = DAG.getNode(ISD::OR, DLS, VT, SX, VOp);
977 SDValue NewSel = DAG.getNode(ISD::SELECT, DLS, VT, SC, NewOr, VOp);
978 DAG.ReplaceAllUsesWith(I, NewSel.getNode());
979 } else if (IsZero(SX)) {
980 SDValue NewOr = DAG.getNode(ISD::OR, DLS, VT, SY, VOp);
981 SDValue NewSel = DAG.getNode(ISD::SELECT, DLS, VT, SC, VOp, NewOr);
982 DAG.ReplaceAllUsesWith(I, NewSel.getNode());
989 bool HexagonDAGToDAGISel::SelectAddrFI(SDValue& N, SDValue &R) {
990 if (N.getOpcode() != ISD::FrameIndex)
992 FrameIndexSDNode *FX = cast<FrameIndexSDNode>(N);
993 R = CurDAG->getTargetFrameIndex(FX->getIndex(), MVT::i32);
997 inline bool HexagonDAGToDAGISel::SelectAddrGA(SDValue &N, SDValue &R) {
998 return SelectGlobalAddress(N, R, false);
1001 inline bool HexagonDAGToDAGISel::SelectAddrGP(SDValue &N, SDValue &R) {
1002 return SelectGlobalAddress(N, R, true);
1005 bool HexagonDAGToDAGISel::SelectGlobalAddress(SDValue &N, SDValue &R,
1007 switch (N.getOpcode()) {
1009 SDValue N0 = N.getOperand(0);
1010 SDValue N1 = N.getOperand(1);
1011 unsigned GAOpc = N0.getOpcode();
1012 if (UseGP && GAOpc != HexagonISD::CONST32_GP)
1014 if (!UseGP && GAOpc != HexagonISD::CONST32)
1016 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N1)) {
1017 SDValue Addr = N0.getOperand(0);
1018 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Addr)) {
1019 if (GA->getOpcode() == ISD::TargetGlobalAddress) {
1020 uint64_t NewOff = GA->getOffset() + (uint64_t)Const->getSExtValue();
1021 R = CurDAG->getTargetGlobalAddress(GA->getGlobal(), SDLoc(Const),
1022 N.getValueType(), NewOff);
1029 case HexagonISD::CONST32:
1030 // The operand(0) of CONST32 is TargetGlobalAddress, which is what we
1031 // want in the instruction.
1033 R = N.getOperand(0);
1035 case HexagonISD::CONST32_GP:
1037 R = N.getOperand(0);
1046 bool HexagonDAGToDAGISel::isValueExtension(const SDValue &Val,
1047 unsigned FromBits, SDValue &Src) {
1048 unsigned Opc = Val.getOpcode();
1050 case ISD::SIGN_EXTEND:
1051 case ISD::ZERO_EXTEND:
1052 case ISD::ANY_EXTEND: {
1053 SDValue const &Op0 = Val.getOperand(0);
1054 EVT T = Op0.getValueType();
1055 if (T.isInteger() && T.getSizeInBits() == FromBits) {
1061 case ISD::SIGN_EXTEND_INREG:
1062 case ISD::AssertSext:
1063 case ISD::AssertZext:
1064 if (Val.getOperand(0).getValueType().isInteger()) {
1065 VTSDNode *T = cast<VTSDNode>(Val.getOperand(1));
1066 if (T->getVT().getSizeInBits() == FromBits) {
1067 Src = Val.getOperand(0);
1073 // Check if this is an AND with "FromBits" of lower bits set to 1.
1074 uint64_t FromMask = (1 << FromBits) - 1;
1075 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(0))) {
1076 if (C->getZExtValue() == FromMask) {
1077 Src = Val.getOperand(1);
1081 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(1))) {
1082 if (C->getZExtValue() == FromMask) {
1083 Src = Val.getOperand(0);
1091 // OR/XOR with the lower "FromBits" bits set to 0.
1092 uint64_t FromMask = (1 << FromBits) - 1;
1093 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(0))) {
1094 if ((C->getZExtValue() & FromMask) == 0) {
1095 Src = Val.getOperand(1);
1099 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(1))) {
1100 if ((C->getZExtValue() & FromMask) == 0) {
1101 Src = Val.getOperand(0);