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 initializeHexagonDAGToDAGISelPass(*PassRegistry::getPassRegistry());
57 bool runOnMachineFunction(MachineFunction &MF) override {
58 // Reset the subtarget each time through.
59 HST = &MF.getSubtarget<HexagonSubtarget>();
60 SelectionDAGISel::runOnMachineFunction(MF);
64 virtual void PreprocessISelDAG() override;
66 SDNode *Select(SDNode *N) override;
68 // Complex Pattern Selectors.
69 inline bool SelectAddrGA(SDValue &N, SDValue &R);
70 inline bool SelectAddrGP(SDValue &N, SDValue &R);
71 bool SelectGlobalAddress(SDValue &N, SDValue &R, bool UseGP);
72 bool SelectAddrFI(SDValue &N, SDValue &R);
74 const char *getPassName() const override {
75 return "Hexagon DAG->DAG Pattern Instruction Selection";
78 SDNode *SelectFrameIndex(SDNode *N);
79 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
80 /// inline asm expressions.
81 bool SelectInlineAsmMemoryOperand(const SDValue &Op,
82 unsigned ConstraintID,
83 std::vector<SDValue> &OutOps) override;
84 SDNode *SelectLoad(SDNode *N);
85 SDNode *SelectBaseOffsetLoad(LoadSDNode *LD, SDLoc dl);
86 SDNode *SelectIndexedLoad(LoadSDNode *LD, SDLoc dl);
87 SDNode *SelectIndexedLoadZeroExtend64(LoadSDNode *LD, unsigned Opcode,
89 SDNode *SelectIndexedLoadSignExtend64(LoadSDNode *LD, unsigned Opcode,
91 SDNode *SelectBaseOffsetStore(StoreSDNode *ST, SDLoc dl);
92 SDNode *SelectIndexedStore(StoreSDNode *ST, SDLoc dl);
93 SDNode *SelectStore(SDNode *N);
94 SDNode *SelectSHL(SDNode *N);
95 SDNode *SelectMul(SDNode *N);
96 SDNode *SelectZeroExtend(SDNode *N);
97 SDNode *SelectIntrinsicWChain(SDNode *N);
98 SDNode *SelectIntrinsicWOChain(SDNode *N);
99 SDNode *SelectConstant(SDNode *N);
100 SDNode *SelectConstantFP(SDNode *N);
101 SDNode *SelectAdd(SDNode *N);
102 SDNode *SelectBitOp(SDNode *N);
104 // XformMskToBitPosU5Imm - Returns the bit position which
105 // the single bit 32 bit mask represents.
106 // Used in Clr and Set bit immediate memops.
107 SDValue XformMskToBitPosU5Imm(uint32_t Imm) {
109 bitPos = Log2_32(Imm);
110 assert(bitPos >= 0 && bitPos < 32 &&
111 "Constant out of range for 32 BitPos Memops");
112 return CurDAG->getTargetConstant(bitPos, MVT::i32);
115 // XformMskToBitPosU4Imm - Returns the bit position which the single-bit
116 // 16 bit mask represents. Used in Clr and Set bit immediate memops.
117 SDValue XformMskToBitPosU4Imm(uint16_t Imm) {
118 return XformMskToBitPosU5Imm(Imm);
121 // XformMskToBitPosU3Imm - Returns the bit position which the single-bit
122 // 8 bit mask represents. Used in Clr and Set bit immediate memops.
123 SDValue XformMskToBitPosU3Imm(uint8_t Imm) {
124 return XformMskToBitPosU5Imm(Imm);
127 // Return true if there is exactly one bit set in V, i.e., if V is one of the
128 // following integers: 2^0, 2^1, ..., 2^31.
129 bool ImmIsSingleBit(uint32_t v) const {
130 return isPowerOf2_32(v);
133 // XformM5ToU5Imm - Return a target constant with the specified value, of
134 // type i32 where the negative literal is transformed into a positive literal
135 // for use in -= memops.
136 inline SDValue XformM5ToU5Imm(signed Imm) {
137 assert( (Imm >= -31 && Imm <= -1) && "Constant out of range for Memops");
138 return CurDAG->getTargetConstant( - Imm, MVT::i32);
141 // XformU7ToU7M1Imm - Return a target constant decremented by 1, in range
142 // [1..128], used in cmpb.gtu instructions.
143 inline SDValue XformU7ToU7M1Imm(signed Imm) {
144 assert((Imm >= 1 && Imm <= 128) && "Constant out of range for cmpb op");
145 return CurDAG->getTargetConstant(Imm - 1, MVT::i8);
148 // XformS8ToS8M1Imm - Return a target constant decremented by 1.
149 inline SDValue XformSToSM1Imm(signed Imm) {
150 return CurDAG->getTargetConstant(Imm - 1, MVT::i32);
153 // XformU8ToU8M1Imm - Return a target constant decremented by 1.
154 inline SDValue XformUToUM1Imm(unsigned Imm) {
155 assert((Imm >= 1) && "Cannot decrement unsigned int less than 1");
156 return CurDAG->getTargetConstant(Imm - 1, MVT::i32);
159 // XformSToSM2Imm - Return a target constant decremented by 2.
160 inline SDValue XformSToSM2Imm(unsigned Imm) {
161 return CurDAG->getTargetConstant(Imm - 2, MVT::i32);
164 // XformSToSM3Imm - Return a target constant decremented by 3.
165 inline SDValue XformSToSM3Imm(unsigned Imm) {
166 return CurDAG->getTargetConstant(Imm - 3, MVT::i32);
169 // Include the pieces autogenerated from the target description.
170 #include "HexagonGenDAGISel.inc"
173 bool isValueExtension(const SDValue &Val, unsigned FromBits, SDValue &Src);
174 }; // end HexagonDAGToDAGISel
175 } // end anonymous namespace
178 /// createHexagonISelDag - This pass converts a legalized DAG into a
179 /// Hexagon-specific DAG, ready for instruction scheduling.
182 FunctionPass *createHexagonISelDag(HexagonTargetMachine &TM,
183 CodeGenOpt::Level OptLevel) {
184 return new HexagonDAGToDAGISel(TM, OptLevel);
188 static void initializePassOnce(PassRegistry &Registry) {
189 const char *Name = "Hexagon DAG->DAG Pattern Instruction Selection";
190 PassInfo *PI = new PassInfo(Name, "hexagon-isel",
191 &SelectionDAGISel::ID, nullptr, false, false);
192 Registry.registerPass(*PI, true);
195 void llvm::initializeHexagonDAGToDAGISelPass(PassRegistry &Registry) {
196 CALL_ONCE_INITIALIZATION(initializePassOnce)
200 // Intrinsics that return a a predicate.
201 static unsigned doesIntrinsicReturnPredicate(unsigned ID)
206 case Intrinsic::hexagon_C2_cmpeq:
207 case Intrinsic::hexagon_C2_cmpgt:
208 case Intrinsic::hexagon_C2_cmpgtu:
209 case Intrinsic::hexagon_C2_cmpgtup:
210 case Intrinsic::hexagon_C2_cmpgtp:
211 case Intrinsic::hexagon_C2_cmpeqp:
212 case Intrinsic::hexagon_C2_bitsset:
213 case Intrinsic::hexagon_C2_bitsclr:
214 case Intrinsic::hexagon_C2_cmpeqi:
215 case Intrinsic::hexagon_C2_cmpgti:
216 case Intrinsic::hexagon_C2_cmpgtui:
217 case Intrinsic::hexagon_C2_cmpgei:
218 case Intrinsic::hexagon_C2_cmpgeui:
219 case Intrinsic::hexagon_C2_cmplt:
220 case Intrinsic::hexagon_C2_cmpltu:
221 case Intrinsic::hexagon_C2_bitsclri:
222 case Intrinsic::hexagon_C2_and:
223 case Intrinsic::hexagon_C2_or:
224 case Intrinsic::hexagon_C2_xor:
225 case Intrinsic::hexagon_C2_andn:
226 case Intrinsic::hexagon_C2_not:
227 case Intrinsic::hexagon_C2_orn:
228 case Intrinsic::hexagon_C2_pxfer_map:
229 case Intrinsic::hexagon_C2_any8:
230 case Intrinsic::hexagon_C2_all8:
231 case Intrinsic::hexagon_A2_vcmpbeq:
232 case Intrinsic::hexagon_A2_vcmpbgtu:
233 case Intrinsic::hexagon_A2_vcmpheq:
234 case Intrinsic::hexagon_A2_vcmphgt:
235 case Intrinsic::hexagon_A2_vcmphgtu:
236 case Intrinsic::hexagon_A2_vcmpweq:
237 case Intrinsic::hexagon_A2_vcmpwgt:
238 case Intrinsic::hexagon_A2_vcmpwgtu:
239 case Intrinsic::hexagon_C2_tfrrp:
240 case Intrinsic::hexagon_S2_tstbit_i:
241 case Intrinsic::hexagon_S2_tstbit_r:
246 SDNode *HexagonDAGToDAGISel::SelectIndexedLoadSignExtend64(LoadSDNode *LD,
249 SDValue Chain = LD->getChain();
250 EVT LoadedVT = LD->getMemoryVT();
251 SDValue Base = LD->getBasePtr();
252 SDValue Offset = LD->getOffset();
253 SDNode *OffsetNode = Offset.getNode();
254 int32_t Val = cast<ConstantSDNode>(OffsetNode)->getSExtValue();
256 const HexagonInstrInfo &TII = *HST->getInstrInfo();
257 if (TII.isValidAutoIncImm(LoadedVT, Val)) {
258 SDValue TargetConst = CurDAG->getTargetConstant(Val, MVT::i32);
259 SDNode *Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::i32, MVT::i32,
260 MVT::Other, Base, TargetConst,
262 SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A2_sxtw, dl, MVT::i64,
263 SDValue(Result_1, 0));
264 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
265 MemOp[0] = LD->getMemOperand();
266 cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
267 const SDValue Froms[] = { SDValue(LD, 0),
270 const SDValue Tos[] = { SDValue(Result_2, 0),
271 SDValue(Result_1, 1),
272 SDValue(Result_1, 2) };
273 ReplaceUses(Froms, Tos, 3);
277 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
278 SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
279 SDNode *Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::i32, MVT::Other,
280 Base, TargetConst0, Chain);
281 SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A2_sxtw, dl, MVT::i64,
282 SDValue(Result_1, 0));
283 SDNode* Result_3 = CurDAG->getMachineNode(Hexagon::A2_addi, dl, MVT::i32,
284 Base, TargetConstVal,
285 SDValue(Result_1, 1));
286 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
287 MemOp[0] = LD->getMemOperand();
288 cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
289 const SDValue Froms[] = { SDValue(LD, 0),
292 const SDValue Tos[] = { SDValue(Result_2, 0),
293 SDValue(Result_3, 0),
294 SDValue(Result_1, 1) };
295 ReplaceUses(Froms, Tos, 3);
300 SDNode *HexagonDAGToDAGISel::SelectIndexedLoadZeroExtend64(LoadSDNode *LD,
303 SDValue Chain = LD->getChain();
304 EVT LoadedVT = LD->getMemoryVT();
305 SDValue Base = LD->getBasePtr();
306 SDValue Offset = LD->getOffset();
307 SDNode *OffsetNode = Offset.getNode();
308 int32_t Val = cast<ConstantSDNode>(OffsetNode)->getSExtValue();
310 const HexagonInstrInfo &TII = *HST->getInstrInfo();
311 if (TII.isValidAutoIncImm(LoadedVT, Val)) {
312 SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
313 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
314 SDNode *Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::i32,
315 MVT::i32, MVT::Other, Base,
316 TargetConstVal, Chain);
317 SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A4_combineir, dl,
318 MVT::i64, MVT::Other,
320 SDValue(Result_1,0));
321 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
322 MemOp[0] = LD->getMemOperand();
323 cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
324 const SDValue Froms[] = { SDValue(LD, 0),
327 const SDValue Tos[] = { SDValue(Result_2, 0),
328 SDValue(Result_1, 1),
329 SDValue(Result_1, 2) };
330 ReplaceUses(Froms, Tos, 3);
334 // Generate an indirect load.
335 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
336 SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
337 SDNode *Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::i32,
338 MVT::Other, Base, TargetConst0,
340 SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A4_combineir, dl,
341 MVT::i64, MVT::Other,
343 SDValue(Result_1,0));
344 // Add offset to base.
345 SDNode* Result_3 = CurDAG->getMachineNode(Hexagon::A2_addi, dl, MVT::i32,
346 Base, TargetConstVal,
347 SDValue(Result_1, 1));
348 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
349 MemOp[0] = LD->getMemOperand();
350 cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
351 const SDValue Froms[] = { SDValue(LD, 0),
354 const SDValue Tos[] = { SDValue(Result_2, 0), // Load value.
355 SDValue(Result_3, 0), // New address.
356 SDValue(Result_1, 1) };
357 ReplaceUses(Froms, Tos, 3);
362 SDNode *HexagonDAGToDAGISel::SelectIndexedLoad(LoadSDNode *LD, SDLoc dl) {
363 SDValue Chain = LD->getChain();
364 SDValue Base = LD->getBasePtr();
365 SDValue Offset = LD->getOffset();
366 SDNode *OffsetNode = Offset.getNode();
367 // Get the constant value.
368 int32_t Val = cast<ConstantSDNode>(OffsetNode)->getSExtValue();
369 EVT LoadedVT = LD->getMemoryVT();
372 // Check for zero extended loads. Treat any-extend loads as zero extended
374 ISD::LoadExtType ExtType = LD->getExtensionType();
375 bool IsZeroExt = (ExtType == ISD::ZEXTLOAD || ExtType == ISD::EXTLOAD);
377 // Figure out the opcode.
378 const HexagonInstrInfo &TII = *HST->getInstrInfo();
379 if (LoadedVT == MVT::i64) {
380 if (TII.isValidAutoIncImm(LoadedVT, Val))
381 Opcode = Hexagon::L2_loadrd_pi;
383 Opcode = Hexagon::L2_loadrd_io;
384 } else if (LoadedVT == MVT::i32) {
385 if (TII.isValidAutoIncImm(LoadedVT, Val))
386 Opcode = Hexagon::L2_loadri_pi;
388 Opcode = Hexagon::L2_loadri_io;
389 } else if (LoadedVT == MVT::i16) {
390 if (TII.isValidAutoIncImm(LoadedVT, Val))
391 Opcode = IsZeroExt ? Hexagon::L2_loadruh_pi : Hexagon::L2_loadrh_pi;
393 Opcode = IsZeroExt ? Hexagon::L2_loadruh_io : Hexagon::L2_loadrh_io;
394 } else if (LoadedVT == MVT::i8) {
395 if (TII.isValidAutoIncImm(LoadedVT, Val))
396 Opcode = IsZeroExt ? Hexagon::L2_loadrub_pi : Hexagon::L2_loadrb_pi;
398 Opcode = IsZeroExt ? Hexagon::L2_loadrub_io : Hexagon::L2_loadrb_io;
400 llvm_unreachable("unknown memory type");
402 // For zero extended i64 loads, we need to add combine instructions.
403 if (LD->getValueType(0) == MVT::i64 && IsZeroExt)
404 return SelectIndexedLoadZeroExtend64(LD, Opcode, dl);
405 // Handle sign extended i64 loads.
406 if (LD->getValueType(0) == MVT::i64 && ExtType == ISD::SEXTLOAD)
407 return SelectIndexedLoadSignExtend64(LD, Opcode, dl);
409 if (TII.isValidAutoIncImm(LoadedVT, Val)) {
410 SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
411 SDNode* Result = CurDAG->getMachineNode(Opcode, dl,
413 MVT::i32, MVT::Other, Base,
414 TargetConstVal, Chain);
415 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
416 MemOp[0] = LD->getMemOperand();
417 cast<MachineSDNode>(Result)->setMemRefs(MemOp, MemOp + 1);
418 const SDValue Froms[] = { SDValue(LD, 0),
422 const SDValue Tos[] = { SDValue(Result, 0),
426 ReplaceUses(Froms, Tos, 3);
429 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
430 SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
431 SDNode* Result_1 = CurDAG->getMachineNode(Opcode, dl,
433 MVT::Other, Base, TargetConst0,
435 SDNode* Result_2 = CurDAG->getMachineNode(Hexagon::A2_addi, dl, MVT::i32,
436 Base, TargetConstVal,
437 SDValue(Result_1, 1));
438 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
439 MemOp[0] = LD->getMemOperand();
440 cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
441 const SDValue Froms[] = { SDValue(LD, 0),
445 const SDValue Tos[] = { SDValue(Result_1, 0),
446 SDValue(Result_2, 0),
449 ReplaceUses(Froms, Tos, 3);
455 SDNode *HexagonDAGToDAGISel::SelectLoad(SDNode *N) {
458 LoadSDNode *LD = cast<LoadSDNode>(N);
459 ISD::MemIndexedMode AM = LD->getAddressingMode();
461 // Handle indexed loads.
462 if (AM != ISD::UNINDEXED) {
463 result = SelectIndexedLoad(LD, dl);
465 result = SelectCode(LD);
472 SDNode *HexagonDAGToDAGISel::SelectIndexedStore(StoreSDNode *ST, SDLoc dl) {
473 SDValue Chain = ST->getChain();
474 SDValue Base = ST->getBasePtr();
475 SDValue Offset = ST->getOffset();
476 SDValue Value = ST->getValue();
477 SDNode *OffsetNode = Offset.getNode();
478 // Get the constant value.
479 int32_t Val = cast<ConstantSDNode>(OffsetNode)->getSExtValue();
480 EVT StoredVT = ST->getMemoryVT();
481 EVT ValueVT = Value.getValueType();
483 // Offset value must be within representable range
484 // and must have correct alignment properties.
485 const HexagonInstrInfo &TII = *HST->getInstrInfo();
486 if (TII.isValidAutoIncImm(StoredVT, Val)) {
489 // Figure out the post inc version of opcode.
490 if (StoredVT == MVT::i64) Opcode = Hexagon::S2_storerd_pi;
491 else if (StoredVT == MVT::i32) Opcode = Hexagon::S2_storeri_pi;
492 else if (StoredVT == MVT::i16) Opcode = Hexagon::S2_storerh_pi;
493 else if (StoredVT == MVT::i8) Opcode = Hexagon::S2_storerb_pi;
494 else llvm_unreachable("unknown memory type");
496 if (ST->isTruncatingStore() && ValueVT.getSizeInBits() == 64) {
497 assert(StoredVT.getSizeInBits() < 64 && "Not a truncating store");
498 Value = CurDAG->getTargetExtractSubreg(Hexagon::subreg_loreg,
499 dl, MVT::i32, Value);
501 SDValue Ops[] = {Base, CurDAG->getTargetConstant(Val, MVT::i32), Value,
503 // Build post increment store.
504 SDNode* Result = CurDAG->getMachineNode(Opcode, dl, MVT::i32,
506 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
507 MemOp[0] = ST->getMemOperand();
508 cast<MachineSDNode>(Result)->setMemRefs(MemOp, MemOp + 1);
510 ReplaceUses(ST, Result);
511 ReplaceUses(SDValue(ST,1), SDValue(Result,1));
515 // Note: Order of operands matches the def of instruction:
517 // : STInst<(outs), (ins IntRegs:$base, imm:$offset, DoubleRegs:$src1), ...
518 // and it differs for POST_ST* for instance.
519 SDValue Ops[] = { Base, CurDAG->getTargetConstant(0, MVT::i32), Value,
523 // Figure out the opcode.
524 if (StoredVT == MVT::i64) Opcode = Hexagon::S2_storerd_io;
525 else if (StoredVT == MVT::i32) Opcode = Hexagon::S2_storeri_io;
526 else if (StoredVT == MVT::i16) Opcode = Hexagon::S2_storerh_io;
527 else if (StoredVT == MVT::i8) Opcode = Hexagon::S2_storerb_io;
528 else llvm_unreachable("unknown memory type");
530 // Build regular store.
531 SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
532 SDNode* Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::Other, Ops);
533 // Build splitted incriment instruction.
534 SDNode* Result_2 = CurDAG->getMachineNode(Hexagon::A2_addi, dl, MVT::i32,
537 SDValue(Result_1, 0));
538 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
539 MemOp[0] = ST->getMemOperand();
540 cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
542 ReplaceUses(SDValue(ST,0), SDValue(Result_2,0));
543 ReplaceUses(SDValue(ST,1), SDValue(Result_1,0));
547 SDNode *HexagonDAGToDAGISel::SelectStore(SDNode *N) {
549 StoreSDNode *ST = cast<StoreSDNode>(N);
550 ISD::MemIndexedMode AM = ST->getAddressingMode();
552 // Handle indexed stores.
553 if (AM != ISD::UNINDEXED) {
554 return SelectIndexedStore(ST, dl);
557 return SelectCode(ST);
560 SDNode *HexagonDAGToDAGISel::SelectMul(SDNode *N) {
564 // %conv.i = sext i32 %tmp1 to i64
565 // %conv2.i = sext i32 %add to i64
566 // %mul.i = mul nsw i64 %conv2.i, %conv.i
568 // --- match with the following ---
570 // %mul.i = mpy (%tmp1, %add)
573 if (N->getValueType(0) == MVT::i64) {
574 // Shifting a i64 signed multiply.
575 SDValue MulOp0 = N->getOperand(0);
576 SDValue MulOp1 = N->getOperand(1);
581 // Handle sign_extend and sextload.
582 if (MulOp0.getOpcode() == ISD::SIGN_EXTEND) {
583 SDValue Sext0 = MulOp0.getOperand(0);
584 if (Sext0.getNode()->getValueType(0) != MVT::i32) {
585 return SelectCode(N);
589 } else if (MulOp0.getOpcode() == ISD::LOAD) {
590 LoadSDNode *LD = cast<LoadSDNode>(MulOp0.getNode());
591 if (LD->getMemoryVT() != MVT::i32 ||
592 LD->getExtensionType() != ISD::SEXTLOAD ||
593 LD->getAddressingMode() != ISD::UNINDEXED) {
594 return SelectCode(N);
597 SDValue Chain = LD->getChain();
598 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
599 OP0 = SDValue(CurDAG->getMachineNode(Hexagon::L2_loadri_io, dl, MVT::i32,
601 LD->getBasePtr(), TargetConst0,
604 return SelectCode(N);
607 // Same goes for the second operand.
608 if (MulOp1.getOpcode() == ISD::SIGN_EXTEND) {
609 SDValue Sext1 = MulOp1.getOperand(0);
610 if (Sext1.getNode()->getValueType(0) != MVT::i32) {
611 return SelectCode(N);
615 } else if (MulOp1.getOpcode() == ISD::LOAD) {
616 LoadSDNode *LD = cast<LoadSDNode>(MulOp1.getNode());
617 if (LD->getMemoryVT() != MVT::i32 ||
618 LD->getExtensionType() != ISD::SEXTLOAD ||
619 LD->getAddressingMode() != ISD::UNINDEXED) {
620 return SelectCode(N);
623 SDValue Chain = LD->getChain();
624 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
625 OP1 = SDValue(CurDAG->getMachineNode(Hexagon::L2_loadri_io, dl, MVT::i32,
627 LD->getBasePtr(), TargetConst0,
630 return SelectCode(N);
633 // Generate a mpy instruction.
634 SDNode *Result = CurDAG->getMachineNode(Hexagon::M2_dpmpyss_s0, dl, MVT::i64,
636 ReplaceUses(N, Result);
640 return SelectCode(N);
643 SDNode *HexagonDAGToDAGISel::SelectSHL(SDNode *N) {
645 if (N->getValueType(0) == MVT::i32) {
646 SDValue Shl_0 = N->getOperand(0);
647 SDValue Shl_1 = N->getOperand(1);
649 if (Shl_1.getOpcode() == ISD::Constant) {
650 if (Shl_0.getOpcode() == ISD::MUL) {
651 SDValue Mul_0 = Shl_0.getOperand(0); // Val
652 SDValue Mul_1 = Shl_0.getOperand(1); // Const
653 // RHS of mul is const.
654 if (Mul_1.getOpcode() == ISD::Constant) {
656 cast<ConstantSDNode>(Shl_1.getNode())->getSExtValue();
658 cast<ConstantSDNode>(Mul_1.getNode())->getSExtValue();
659 int32_t ValConst = MulConst << ShlConst;
660 SDValue Val = CurDAG->getTargetConstant(ValConst,
662 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Val.getNode()))
663 if (isInt<9>(CN->getSExtValue())) {
665 CurDAG->getMachineNode(Hexagon::M2_mpysmi, dl,
666 MVT::i32, Mul_0, Val);
667 ReplaceUses(N, Result);
672 } else if (Shl_0.getOpcode() == ISD::SUB) {
673 SDValue Sub_0 = Shl_0.getOperand(0); // Const 0
674 SDValue Sub_1 = Shl_0.getOperand(1); // Val
675 if (Sub_0.getOpcode() == ISD::Constant) {
677 cast<ConstantSDNode>(Sub_0.getNode())->getSExtValue();
679 if (Sub_1.getOpcode() == ISD::SHL) {
680 SDValue Shl2_0 = Sub_1.getOperand(0); // Val
681 SDValue Shl2_1 = Sub_1.getOperand(1); // Const
682 if (Shl2_1.getOpcode() == ISD::Constant) {
684 cast<ConstantSDNode>(Shl_1.getNode())->getSExtValue();
686 cast<ConstantSDNode>(Shl2_1.getNode())->getSExtValue();
687 int32_t ValConst = 1 << (ShlConst+Shl2Const);
688 SDValue Val = CurDAG->getTargetConstant(-ValConst, MVT::i32);
689 if (ConstantSDNode *CN =
690 dyn_cast<ConstantSDNode>(Val.getNode()))
691 if (isInt<9>(CN->getSExtValue())) {
693 CurDAG->getMachineNode(Hexagon::M2_mpysmi, dl, MVT::i32,
695 ReplaceUses(N, Result);
705 return SelectCode(N);
710 // If there is an zero_extend followed an intrinsic in DAG (this means - the
711 // result of the intrinsic is predicate); convert the zero_extend to
712 // transfer instruction.
714 // Zero extend -> transfer is lowered here. Otherwise, zero_extend will be
715 // converted into a MUX as predicate registers defined as 1 bit in the
716 // compiler. Architecture defines them as 8-bit registers.
717 // We want to preserve all the lower 8-bits and, not just 1 LSB bit.
719 SDNode *HexagonDAGToDAGISel::SelectZeroExtend(SDNode *N) {
722 SDValue Op0 = N->getOperand(0);
723 EVT OpVT = Op0.getValueType();
724 unsigned OpBW = OpVT.getSizeInBits();
726 // Special handling for zero-extending a vector of booleans.
727 if (OpVT.isVector() && OpVT.getVectorElementType() == MVT::i1 && OpBW <= 64) {
728 SDNode *Mask = CurDAG->getMachineNode(Hexagon::C2_mask, dl, MVT::i64, Op0);
729 unsigned NE = OpVT.getVectorNumElements();
730 EVT ExVT = N->getValueType(0);
731 unsigned ES = ExVT.getVectorElementType().getSizeInBits();
732 uint64_t MV = 0, Bit = 1;
733 for (unsigned i = 0; i < NE; ++i) {
737 SDValue Ones = CurDAG->getTargetConstant(MV, MVT::i64);
738 SDNode *OnesReg = CurDAG->getMachineNode(Hexagon::CONST64_Int_Real, dl,
740 if (ExVT.getSizeInBits() == 32) {
741 SDNode *And = CurDAG->getMachineNode(Hexagon::A2_andp, dl, MVT::i64,
742 SDValue(Mask,0), SDValue(OnesReg,0));
743 SDValue SubR = CurDAG->getTargetConstant(Hexagon::subreg_loreg, MVT::i32);
744 return CurDAG->getMachineNode(Hexagon::EXTRACT_SUBREG, dl, ExVT,
745 SDValue(And,0), SubR);
747 return CurDAG->getMachineNode(Hexagon::A2_andp, dl, ExVT,
748 SDValue(Mask,0), SDValue(OnesReg,0));
751 SDNode *IsIntrinsic = N->getOperand(0).getNode();
752 if ((IsIntrinsic->getOpcode() == ISD::INTRINSIC_WO_CHAIN)) {
754 cast<ConstantSDNode>(IsIntrinsic->getOperand(0))->getZExtValue();
755 if (doesIntrinsicReturnPredicate(ID)) {
756 // Now we need to differentiate target data types.
757 if (N->getValueType(0) == MVT::i64) {
758 // Convert the zero_extend to Rs = Pd followed by A2_combinew(0,Rs).
759 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
760 SDNode *Result_1 = CurDAG->getMachineNode(Hexagon::C2_tfrpr, dl,
762 SDValue(IsIntrinsic, 0));
763 SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A2_tfrsi, dl,
766 SDNode *Result_3 = CurDAG->getMachineNode(Hexagon::A2_combinew, dl,
767 MVT::i64, MVT::Other,
768 SDValue(Result_2, 0),
769 SDValue(Result_1, 0));
770 ReplaceUses(N, Result_3);
773 if (N->getValueType(0) == MVT::i32) {
774 // Convert the zero_extend to Rs = Pd
775 SDNode* RsPd = CurDAG->getMachineNode(Hexagon::C2_tfrpr, dl,
777 SDValue(IsIntrinsic, 0));
778 ReplaceUses(N, RsPd);
781 llvm_unreachable("Unexpected value type");
784 return SelectCode(N);
788 // Checking for intrinsics circular load/store, and bitreverse load/store
789 // instrisics in order to select the correct lowered operation.
791 SDNode *HexagonDAGToDAGISel::SelectIntrinsicWChain(SDNode *N) {
792 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
793 if (IntNo == Intrinsic::hexagon_circ_ldd ||
794 IntNo == Intrinsic::hexagon_circ_ldw ||
795 IntNo == Intrinsic::hexagon_circ_lduh ||
796 IntNo == Intrinsic::hexagon_circ_ldh ||
797 IntNo == Intrinsic::hexagon_circ_ldub ||
798 IntNo == Intrinsic::hexagon_circ_ldb) {
800 SDValue Chain = N->getOperand(0);
801 SDValue Base = N->getOperand(2);
802 SDValue Load = N->getOperand(3);
803 SDValue ModifierExpr = N->getOperand(4);
804 SDValue Offset = N->getOperand(5);
806 // We need to add the rerurn type for the load. This intrinsic has
807 // two return types, one for the load and one for the post-increment.
808 // Only the *_ld instructions push the extra return type, and bump the
809 // result node operand number correspondingly.
810 std::vector<EVT> ResTys;
812 unsigned memsize, align;
813 MVT MvtSize = MVT::i32;
815 if (IntNo == Intrinsic::hexagon_circ_ldd) {
816 ResTys.push_back(MVT::i32);
817 ResTys.push_back(MVT::i64);
818 opc = Hexagon::L2_loadrd_pci_pseudo;
821 } else if (IntNo == Intrinsic::hexagon_circ_ldw) {
822 ResTys.push_back(MVT::i32);
823 ResTys.push_back(MVT::i32);
824 opc = Hexagon::L2_loadri_pci_pseudo;
827 } else if (IntNo == Intrinsic::hexagon_circ_ldh) {
828 ResTys.push_back(MVT::i32);
829 ResTys.push_back(MVT::i32);
830 opc = Hexagon::L2_loadrh_pci_pseudo;
834 } else if (IntNo == Intrinsic::hexagon_circ_lduh) {
835 ResTys.push_back(MVT::i32);
836 ResTys.push_back(MVT::i32);
837 opc = Hexagon::L2_loadruh_pci_pseudo;
841 } else if (IntNo == Intrinsic::hexagon_circ_ldb) {
842 ResTys.push_back(MVT::i32);
843 ResTys.push_back(MVT::i32);
844 opc = Hexagon::L2_loadrb_pci_pseudo;
848 } else if (IntNo == Intrinsic::hexagon_circ_ldub) {
849 ResTys.push_back(MVT::i32);
850 ResTys.push_back(MVT::i32);
851 opc = Hexagon::L2_loadrub_pci_pseudo;
856 llvm_unreachable("no opc");
858 ResTys.push_back(MVT::Other);
860 // Copy over the arguments, which are the same mostly.
861 SmallVector<SDValue, 5> Ops;
864 Ops.push_back(ModifierExpr);
865 int32_t Val = cast<ConstantSDNode>(Offset.getNode())->getSExtValue();
866 Ops.push_back(CurDAG->getTargetConstant(Val, MVT::i32));
867 Ops.push_back(Chain);
868 SDNode* Result = CurDAG->getMachineNode(opc, dl, ResTys, Ops);
871 MachineMemOperand *Mem =
872 MF->getMachineMemOperand(MachinePointerInfo(),
873 MachineMemOperand::MOStore, memsize, align);
874 if (MvtSize != MVT::i32)
875 ST = CurDAG->getTruncStore(Chain, dl, SDValue(Result, 1), Load,
878 ST = CurDAG->getStore(Chain, dl, SDValue(Result, 1), Load, Mem);
880 SDNode* Store = SelectStore(ST.getNode());
882 const SDValue Froms[] = { SDValue(N, 0),
884 const SDValue Tos[] = { SDValue(Result, 0),
886 ReplaceUses(Froms, Tos, 2);
890 if (IntNo == Intrinsic::hexagon_brev_ldd ||
891 IntNo == Intrinsic::hexagon_brev_ldw ||
892 IntNo == Intrinsic::hexagon_brev_ldh ||
893 IntNo == Intrinsic::hexagon_brev_lduh ||
894 IntNo == Intrinsic::hexagon_brev_ldb ||
895 IntNo == Intrinsic::hexagon_brev_ldub) {
897 SDValue Chain = N->getOperand(0);
898 SDValue Base = N->getOperand(2);
899 SDValue Load = N->getOperand(3);
900 SDValue ModifierExpr = N->getOperand(4);
902 // We need to add the rerurn type for the load. This intrinsic has
903 // two return types, one for the load and one for the post-increment.
904 std::vector<EVT> ResTys;
906 unsigned memsize, align;
907 MVT MvtSize = MVT::i32;
909 if (IntNo == Intrinsic::hexagon_brev_ldd) {
910 ResTys.push_back(MVT::i32);
911 ResTys.push_back(MVT::i64);
912 opc = Hexagon::L2_loadrd_pbr_pseudo;
915 } else if (IntNo == Intrinsic::hexagon_brev_ldw) {
916 ResTys.push_back(MVT::i32);
917 ResTys.push_back(MVT::i32);
918 opc = Hexagon::L2_loadri_pbr_pseudo;
921 } else if (IntNo == Intrinsic::hexagon_brev_ldh) {
922 ResTys.push_back(MVT::i32);
923 ResTys.push_back(MVT::i32);
924 opc = Hexagon::L2_loadrh_pbr_pseudo;
928 } else if (IntNo == Intrinsic::hexagon_brev_lduh) {
929 ResTys.push_back(MVT::i32);
930 ResTys.push_back(MVT::i32);
931 opc = Hexagon::L2_loadruh_pbr_pseudo;
935 } else if (IntNo == Intrinsic::hexagon_brev_ldb) {
936 ResTys.push_back(MVT::i32);
937 ResTys.push_back(MVT::i32);
938 opc = Hexagon::L2_loadrb_pbr_pseudo;
942 } else if (IntNo == Intrinsic::hexagon_brev_ldub) {
943 ResTys.push_back(MVT::i32);
944 ResTys.push_back(MVT::i32);
945 opc = Hexagon::L2_loadrub_pbr_pseudo;
950 llvm_unreachable("no opc");
952 ResTys.push_back(MVT::Other);
954 // Copy over the arguments, which are the same mostly.
955 SmallVector<SDValue, 4> Ops;
958 Ops.push_back(ModifierExpr);
959 Ops.push_back(Chain);
960 SDNode* Result = CurDAG->getMachineNode(opc, dl, ResTys, Ops);
962 MachineMemOperand *Mem =
963 MF->getMachineMemOperand(MachinePointerInfo(),
964 MachineMemOperand::MOStore, memsize, align);
965 if (MvtSize != MVT::i32)
966 ST = CurDAG->getTruncStore(Chain, dl, SDValue(Result, 1), Load,
969 ST = CurDAG->getStore(Chain, dl, SDValue(Result, 1), Load, Mem);
971 SDNode* Store = SelectStore(ST.getNode());
973 const SDValue Froms[] = { SDValue(N, 0),
975 const SDValue Tos[] = { SDValue(Result, 0),
977 ReplaceUses(Froms, Tos, 2);
981 return SelectCode(N);
985 // Checking for intrinsics which have predicate registers as operand(s)
986 // and lowering to the actual intrinsic.
988 SDNode *HexagonDAGToDAGISel::SelectIntrinsicWOChain(SDNode *N) {
989 unsigned IID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
992 case Intrinsic::hexagon_S2_vsplatrb:
995 case Intrinsic::hexagon_S2_vsplatrh:
999 return SelectCode(N);
1002 SDValue const &V = N->getOperand(1);
1004 if (isValueExtension(V, Bits, U)) {
1005 SDValue R = CurDAG->getNode(N->getOpcode(), SDLoc(N), N->getValueType(0),
1006 N->getOperand(0), U);
1007 return SelectCode(R.getNode());
1009 return SelectCode(N);
1013 // Map floating point constant values.
1015 SDNode *HexagonDAGToDAGISel::SelectConstantFP(SDNode *N) {
1017 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N);
1018 APFloat APF = CN->getValueAPF();
1019 if (N->getValueType(0) == MVT::f32) {
1020 return CurDAG->getMachineNode(Hexagon::TFRI_f, dl, MVT::f32,
1021 CurDAG->getTargetConstantFP(APF.convertToFloat(), MVT::f32));
1023 else if (N->getValueType(0) == MVT::f64) {
1024 return CurDAG->getMachineNode(Hexagon::CONST64_Float_Real, dl, MVT::f64,
1025 CurDAG->getTargetConstantFP(APF.convertToDouble(), MVT::f64));
1028 return SelectCode(N);
1032 // Map predicate true (encoded as -1 in LLVM) to a XOR.
1034 SDNode *HexagonDAGToDAGISel::SelectConstant(SDNode *N) {
1036 if (N->getValueType(0) == MVT::i1) {
1038 int32_t Val = cast<ConstantSDNode>(N)->getSExtValue();
1040 Result = CurDAG->getMachineNode(Hexagon::TFR_PdTrue, dl, MVT::i1);
1041 } else if (Val == 0) {
1042 Result = CurDAG->getMachineNode(Hexagon::TFR_PdFalse, dl, MVT::i1);
1045 ReplaceUses(N, Result);
1050 return SelectCode(N);
1055 // Map add followed by a asr -> asr +=.
1057 SDNode *HexagonDAGToDAGISel::SelectAdd(SDNode *N) {
1059 if (N->getValueType(0) != MVT::i32) {
1060 return SelectCode(N);
1062 // Identify nodes of the form: add(asr(...)).
1063 SDNode* Src1 = N->getOperand(0).getNode();
1064 if (Src1->getOpcode() != ISD::SRA || !Src1->hasOneUse()
1065 || Src1->getValueType(0) != MVT::i32) {
1066 return SelectCode(N);
1069 // Build Rd = Rd' + asr(Rs, Rt). The machine constraints will ensure that
1070 // Rd and Rd' are assigned to the same register
1071 SDNode* Result = CurDAG->getMachineNode(Hexagon::S2_asr_r_r_acc, dl, MVT::i32,
1073 Src1->getOperand(0),
1074 Src1->getOperand(1));
1075 ReplaceUses(N, Result);
1081 // Map the following, where possible.
1082 // AND/FABS -> clrbit
1084 // XOR/FNEG ->toggle_bit.
1086 SDNode *HexagonDAGToDAGISel::SelectBitOp(SDNode *N) {
1088 EVT ValueVT = N->getValueType(0);
1090 // We handle only 32 and 64-bit bit ops.
1091 if (!(ValueVT == MVT::i32 || ValueVT == MVT::i64 ||
1092 ValueVT == MVT::f32 || ValueVT == MVT::f64))
1093 return SelectCode(N);
1095 // We handly only fabs and fneg for V5.
1096 unsigned Opc = N->getOpcode();
1097 if ((Opc == ISD::FABS || Opc == ISD::FNEG) && !HST->hasV5TOps())
1098 return SelectCode(N);
1101 if (Opc != ISD::FABS && Opc != ISD::FNEG) {
1102 if (N->getOperand(1).getOpcode() == ISD::Constant)
1103 Val = cast<ConstantSDNode>((N)->getOperand(1))->getSExtValue();
1105 return SelectCode(N);
1108 if (Opc == ISD::AND) {
1109 if (((ValueVT == MVT::i32) &&
1110 (!((Val & 0x80000000) || (Val & 0x7fffffff)))) ||
1111 ((ValueVT == MVT::i64) &&
1112 (!((Val & 0x8000000000000000) || (Val & 0x7fffffff)))))
1113 // If it's simple AND, do the normal op.
1114 return SelectCode(N);
1119 // If OR or AND is being fed by shl, srl and, sra don't do this change,
1120 // because Hexagon provide |= &= on shl, srl, and sra.
1121 // Traverse the DAG to see if there is shl, srl and sra.
1122 if (Opc == ISD::OR || Opc == ISD::AND) {
1123 switch (N->getOperand(0)->getOpcode()) {
1128 return SelectCode(N);
1132 // Make sure it's power of 2.
1133 unsigned bitpos = 0;
1134 if (Opc != ISD::FABS && Opc != ISD::FNEG) {
1135 if (((ValueVT == MVT::i32) && !isPowerOf2_32(Val)) ||
1136 ((ValueVT == MVT::i64) && !isPowerOf2_64(Val)))
1137 return SelectCode(N);
1139 // Get the bit position.
1140 bitpos = countTrailingZeros(uint64_t(Val));
1142 // For fabs and fneg, it's always the 31st bit.
1146 unsigned BitOpc = 0;
1147 // Set the right opcode for bitwise operations.
1149 default: llvm_unreachable("Only bit-wise/abs/neg operations are allowed.");
1152 BitOpc = Hexagon::S2_clrbit_i;
1155 BitOpc = Hexagon::S2_setbit_i;
1159 BitOpc = Hexagon::S2_togglebit_i;
1164 // Get the right SDVal for the opcode.
1165 SDValue SDVal = CurDAG->getTargetConstant(bitpos, MVT::i32);
1167 if (ValueVT == MVT::i32 || ValueVT == MVT::f32) {
1168 Result = CurDAG->getMachineNode(BitOpc, dl, ValueVT,
1169 N->getOperand(0), SDVal);
1171 // 64-bit gymnastic to use REG_SEQUENCE. But it's worth it.
1173 if (ValueVT == MVT::i64)
1174 SubValueVT = MVT::i32;
1176 SubValueVT = MVT::f32;
1178 SDNode *Reg = N->getOperand(0).getNode();
1179 SDValue RegClass = CurDAG->getTargetConstant(Hexagon::DoubleRegsRegClassID,
1182 SDValue SubregHiIdx = CurDAG->getTargetConstant(Hexagon::subreg_hireg,
1184 SDValue SubregLoIdx = CurDAG->getTargetConstant(Hexagon::subreg_loreg,
1187 SDValue SubregHI = CurDAG->getTargetExtractSubreg(Hexagon::subreg_hireg, dl,
1188 MVT::i32, SDValue(Reg, 0));
1190 SDValue SubregLO = CurDAG->getTargetExtractSubreg(Hexagon::subreg_loreg, dl,
1191 MVT::i32, SDValue(Reg, 0));
1193 // Clear/set/toggle hi or lo registers depending on the bit position.
1194 if (SubValueVT != MVT::f32 && bitpos < 32) {
1195 SDNode *Result0 = CurDAG->getMachineNode(BitOpc, dl, SubValueVT,
1197 const SDValue Ops[] = { RegClass, SubregHI, SubregHiIdx,
1198 SDValue(Result0, 0), SubregLoIdx };
1199 Result = CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE,
1202 if (Opc != ISD::FABS && Opc != ISD::FNEG)
1203 SDVal = CurDAG->getTargetConstant(bitpos-32, MVT::i32);
1204 SDNode *Result0 = CurDAG->getMachineNode(BitOpc, dl, SubValueVT,
1206 const SDValue Ops[] = { RegClass, SDValue(Result0, 0), SubregHiIdx,
1207 SubregLO, SubregLoIdx };
1208 Result = CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE,
1213 ReplaceUses(N, Result);
1218 SDNode *HexagonDAGToDAGISel::SelectFrameIndex(SDNode *N) {
1219 int FX = cast<FrameIndexSDNode>(N)->getIndex();
1220 SDValue FI = CurDAG->getTargetFrameIndex(FX, MVT::i32);
1221 SDValue Zero = CurDAG->getTargetConstant(0, MVT::i32);
1224 SDNode *R = CurDAG->getMachineNode(Hexagon::TFR_FI, DL, MVT::i32, FI, Zero);
1226 if (N->getHasDebugValue())
1227 CurDAG->TransferDbgValues(SDValue(N, 0), SDValue(R, 0));
1232 SDNode *HexagonDAGToDAGISel::Select(SDNode *N) {
1233 if (N->isMachineOpcode()) {
1235 return nullptr; // Already selected.
1238 switch (N->getOpcode()) {
1240 return SelectConstant(N);
1242 case ISD::ConstantFP:
1243 return SelectConstantFP(N);
1245 case ISD::FrameIndex:
1246 return SelectFrameIndex(N);
1249 return SelectAdd(N);
1252 return SelectSHL(N);
1255 return SelectLoad(N);
1258 return SelectStore(N);
1261 return SelectMul(N);
1268 return SelectBitOp(N);
1270 case ISD::ZERO_EXTEND:
1271 return SelectZeroExtend(N);
1273 case ISD::INTRINSIC_W_CHAIN:
1274 return SelectIntrinsicWChain(N);
1276 case ISD::INTRINSIC_WO_CHAIN:
1277 return SelectIntrinsicWOChain(N);
1280 return SelectCode(N);
1284 bool HexagonDAGToDAGISel::
1285 SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID,
1286 std::vector<SDValue> &OutOps) {
1287 SDValue Inp = Op, Res;
1289 switch (ConstraintID) {
1292 case InlineAsm::Constraint_i:
1293 case InlineAsm::Constraint_o: // Offsetable.
1294 case InlineAsm::Constraint_v: // Not offsetable.
1295 case InlineAsm::Constraint_m: // Memory.
1296 if (SelectAddrFI(Inp, Res))
1297 OutOps.push_back(Res);
1299 OutOps.push_back(Inp);
1303 OutOps.push_back(CurDAG->getTargetConstant(0, MVT::i32));
1307 void HexagonDAGToDAGISel::PreprocessISelDAG() {
1308 SelectionDAG &DAG = *CurDAG;
1309 std::vector<SDNode*> Nodes;
1310 for (auto I = DAG.allnodes_begin(), E = DAG.allnodes_end(); I != E; ++I)
1313 // Simplify: (or (select c x 0) z) -> (select c (or x z) z)
1314 // (or (select c 0 y) z) -> (select c z (or y z))
1315 // This may not be the right thing for all targets, so do it here.
1316 for (auto I: Nodes) {
1317 if (I->getOpcode() != ISD::OR)
1320 auto IsZero = [] (const SDValue &V) -> bool {
1321 if (ConstantSDNode *SC = dyn_cast<ConstantSDNode>(V.getNode()))
1322 return SC->isNullValue();
1325 auto IsSelect0 = [IsZero] (const SDValue &Op) -> bool {
1326 if (Op.getOpcode() != ISD::SELECT)
1328 return IsZero(Op.getOperand(1)) || IsZero(Op.getOperand(2));
1331 SDValue N0 = I->getOperand(0), N1 = I->getOperand(1);
1332 EVT VT = I->getValueType(0);
1333 bool SelN0 = IsSelect0(N0);
1334 SDValue SOp = SelN0 ? N0 : N1;
1335 SDValue VOp = SelN0 ? N1 : N0;
1337 if (SOp.getOpcode() == ISD::SELECT && SOp.getNode()->hasOneUse()) {
1338 SDValue SC = SOp.getOperand(0);
1339 SDValue SX = SOp.getOperand(1);
1340 SDValue SY = SOp.getOperand(2);
1343 SDValue NewOr = DAG.getNode(ISD::OR, DLS, VT, SX, VOp);
1344 SDValue NewSel = DAG.getNode(ISD::SELECT, DLS, VT, SC, NewOr, VOp);
1345 DAG.ReplaceAllUsesWith(I, NewSel.getNode());
1346 } else if (IsZero(SX)) {
1347 SDValue NewOr = DAG.getNode(ISD::OR, DLS, VT, SY, VOp);
1348 SDValue NewSel = DAG.getNode(ISD::SELECT, DLS, VT, SC, VOp, NewOr);
1349 DAG.ReplaceAllUsesWith(I, NewSel.getNode());
1356 bool HexagonDAGToDAGISel::SelectAddrFI(SDValue& N, SDValue &R) {
1357 if (N.getOpcode() != ISD::FrameIndex)
1359 FrameIndexSDNode *FX = cast<FrameIndexSDNode>(N);
1360 R = CurDAG->getTargetFrameIndex(FX->getIndex(), MVT::i32);
1364 inline bool HexagonDAGToDAGISel::SelectAddrGA(SDValue &N, SDValue &R) {
1365 return SelectGlobalAddress(N, R, false);
1368 inline bool HexagonDAGToDAGISel::SelectAddrGP(SDValue &N, SDValue &R) {
1369 return SelectGlobalAddress(N, R, true);
1372 bool HexagonDAGToDAGISel::SelectGlobalAddress(SDValue &N, SDValue &R,
1374 switch (N.getOpcode()) {
1376 SDValue N0 = N.getOperand(0);
1377 SDValue N1 = N.getOperand(1);
1378 unsigned GAOpc = N0.getOpcode();
1379 if (UseGP && GAOpc != HexagonISD::CONST32_GP)
1381 if (!UseGP && GAOpc != HexagonISD::CONST32)
1383 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N1)) {
1384 SDValue Addr = N0.getOperand(0);
1385 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Addr)) {
1386 if (GA->getOpcode() == ISD::TargetGlobalAddress) {
1387 uint64_t NewOff = GA->getOffset() + (uint64_t)Const->getSExtValue();
1388 R = CurDAG->getTargetGlobalAddress(GA->getGlobal(), SDLoc(Const),
1389 N.getValueType(), NewOff);
1396 case HexagonISD::CONST32:
1397 // The operand(0) of CONST32 is TargetGlobalAddress, which is what we
1398 // want in the instruction.
1400 R = N.getOperand(0);
1402 case HexagonISD::CONST32_GP:
1404 R = N.getOperand(0);
1413 bool HexagonDAGToDAGISel::isValueExtension(const SDValue &Val,
1414 unsigned FromBits, SDValue &Src) {
1415 unsigned Opc = Val.getOpcode();
1417 case ISD::SIGN_EXTEND:
1418 case ISD::ZERO_EXTEND:
1419 case ISD::ANY_EXTEND: {
1420 SDValue const &Op0 = Val.getOperand(0);
1421 EVT T = Op0.getValueType();
1422 if (T.isInteger() && T.getSizeInBits() == FromBits) {
1428 case ISD::SIGN_EXTEND_INREG:
1429 case ISD::AssertSext:
1430 case ISD::AssertZext:
1431 if (Val.getOperand(0).getValueType().isInteger()) {
1432 VTSDNode *T = cast<VTSDNode>(Val.getOperand(1));
1433 if (T->getVT().getSizeInBits() == FromBits) {
1434 Src = Val.getOperand(0);
1440 // Check if this is an AND with "FromBits" of lower bits set to 1.
1441 uint64_t FromMask = (1 << FromBits) - 1;
1442 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(0))) {
1443 if (C->getZExtValue() == FromMask) {
1444 Src = Val.getOperand(1);
1448 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(1))) {
1449 if (C->getZExtValue() == FromMask) {
1450 Src = Val.getOperand(0);
1458 // OR/XOR with the lower "FromBits" bits set to 0.
1459 uint64_t FromMask = (1 << FromBits) - 1;
1460 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(0))) {
1461 if ((C->getZExtValue() & FromMask) == 0) {
1462 Src = Val.getOperand(1);
1466 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(1))) {
1467 if ((C->getZExtValue() & FromMask) == 0) {
1468 Src = Val.getOperand(0);