Fix build break introduced in r231992
[oota-llvm.git] / lib / Target / Hexagon / HexagonISelDAGToDAG.cpp
1 //===-- HexagonISelDAGToDAG.cpp - A dag to dag inst selector for Hexagon --===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines an instruction selector for the Hexagon target.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "Hexagon.h"
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"
23 using namespace llvm;
24
25 #define DEBUG_TYPE "hexagon-isel"
26
27 static
28 cl::opt<unsigned>
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"));
33
34 //===----------------------------------------------------------------------===//
35 // Instruction Selector Implementation
36 //===----------------------------------------------------------------------===//
37
38 namespace llvm {
39   void initializeHexagonDAGToDAGISelPass(PassRegistry&);
40 }
41
42 //===--------------------------------------------------------------------===//
43 /// HexagonDAGToDAGISel - Hexagon specific code to select Hexagon machine
44 /// instructions for SelectionDAG operations.
45 ///
46 namespace {
47 class HexagonDAGToDAGISel : public SelectionDAGISel {
48   /// Subtarget - Keep a pointer to the Hexagon Subtarget around so that we can
49   /// make the right decision when generating code for different targets.
50   const HexagonSubtarget *Subtarget;
51
52   // Keep a reference to HexagonTargetMachine.
53   const HexagonTargetMachine& TM;
54 public:
55   explicit HexagonDAGToDAGISel(HexagonTargetMachine &targetmachine,
56                                CodeGenOpt::Level OptLevel)
57       : SelectionDAGISel(targetmachine, OptLevel), TM(targetmachine) {
58     initializeHexagonDAGToDAGISelPass(*PassRegistry::getPassRegistry());
59   }
60
61   SDNode *Select(SDNode *N) override;
62
63   // Complex Pattern Selectors.
64   inline bool foldGlobalAddress(SDValue &N, SDValue &R);
65   inline bool foldGlobalAddressGP(SDValue &N, SDValue &R);
66   bool foldGlobalAddressImpl(SDValue &N, SDValue &R, bool ShouldLookForGP);
67   bool SelectADDRri(SDValue& N, SDValue &R1, SDValue &R2);
68   bool SelectADDRriS11_0(SDValue& N, SDValue &R1, SDValue &R2);
69   bool SelectADDRriS11_1(SDValue& N, SDValue &R1, SDValue &R2);
70   bool SelectADDRriS11_2(SDValue& N, SDValue &R1, SDValue &R2);
71   bool SelectMEMriS11_2(SDValue& Addr, SDValue &Base, SDValue &Offset);
72   bool SelectADDRriS11_3(SDValue& N, SDValue &R1, SDValue &R2);
73   bool SelectADDRrr(SDValue &Addr, SDValue &Base, SDValue &Offset);
74   bool SelectADDRriU6_0(SDValue& N, SDValue &R1, SDValue &R2);
75   bool SelectADDRriU6_1(SDValue& N, SDValue &R1, SDValue &R2);
76   bool SelectADDRriU6_2(SDValue& N, SDValue &R1, SDValue &R2);
77
78   // Complex Pattern Selectors.
79   inline bool SelectAddrGA(SDValue &N, SDValue &R);
80   inline bool SelectAddrGP(SDValue &N, SDValue &R);
81   bool SelectGlobalAddress(SDValue &N, SDValue &R, bool UseGP);
82   bool SelectAddrFI(SDValue &N, SDValue &R);
83
84   const char *getPassName() const override {
85     return "Hexagon DAG->DAG Pattern Instruction Selection";
86   }
87
88   bool runOnMachineFunction(MachineFunction &MF) override {
89     Subtarget = &MF.getSubtarget<HexagonSubtarget>();
90     return SelectionDAGISel::runOnMachineFunction(MF);
91   }
92
93   /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
94   /// inline asm expressions.
95   bool SelectInlineAsmMemoryOperand(const SDValue &Op,
96                                     char ConstraintCode,
97                                     std::vector<SDValue> &OutOps) override;
98   bool SelectAddr(SDNode *Op, SDValue Addr, SDValue &Base, SDValue &Offset);
99
100   SDNode *SelectLoad(SDNode *N);
101   SDNode *SelectBaseOffsetLoad(LoadSDNode *LD, SDLoc dl);
102   SDNode *SelectIndexedLoad(LoadSDNode *LD, SDLoc dl);
103   SDNode *SelectIndexedLoadZeroExtend64(LoadSDNode *LD, unsigned Opcode,
104                                         SDLoc dl);
105   SDNode *SelectIndexedLoadSignExtend64(LoadSDNode *LD, unsigned Opcode,
106                                         SDLoc dl);
107   SDNode *SelectBaseOffsetStore(StoreSDNode *ST, SDLoc dl);
108   SDNode *SelectIndexedStore(StoreSDNode *ST, SDLoc dl);
109   SDNode *SelectStore(SDNode *N);
110   SDNode *SelectSHL(SDNode *N);
111   SDNode *SelectSelect(SDNode *N);
112   SDNode *SelectTruncate(SDNode *N);
113   SDNode *SelectMul(SDNode *N);
114   SDNode *SelectZeroExtend(SDNode *N);
115   SDNode *SelectIntrinsicWOChain(SDNode *N);
116   SDNode *SelectIntrinsicWChain(SDNode *N);
117   SDNode *SelectConstant(SDNode *N);
118   SDNode *SelectConstantFP(SDNode *N);
119   SDNode *SelectAdd(SDNode *N);
120
121 // XformMskToBitPosU5Imm - Returns the bit position which
122 // the single bit 32 bit mask represents.
123 // Used in Clr and Set bit immediate memops.
124 SDValue XformMskToBitPosU5Imm(uint32_t Imm) {
125   int32_t bitPos;
126   bitPos = Log2_32(Imm);
127   assert(bitPos >= 0 && bitPos < 32 &&
128          "Constant out of range for 32 BitPos Memops");
129   return CurDAG->getTargetConstant(bitPos, MVT::i32);
130 }
131
132 // XformMskToBitPosU4Imm - Returns the bit position which the single bit 16 bit
133 // mask represents. Used in Clr and Set bit immediate memops.
134 SDValue XformMskToBitPosU4Imm(uint16_t Imm) {
135   return XformMskToBitPosU5Imm(Imm);
136 }
137
138 // XformMskToBitPosU3Imm - Returns the bit position which the single bit 8 bit
139 // mask represents. Used in Clr and Set bit immediate memops.
140 SDValue XformMskToBitPosU3Imm(uint8_t Imm) {
141   return XformMskToBitPosU5Imm(Imm);
142 }
143
144 // Return true if there is exactly one bit set in V, i.e., if V is one of the
145 // following integers: 2^0, 2^1, ..., 2^31.
146 bool ImmIsSingleBit(uint32_t v) const {
147   return isPowerOf2_32(v);
148 }
149
150 // XformM5ToU5Imm - Return a target constant with the specified value, of type
151 // i32 where the negative literal is transformed into a positive literal for
152 // use in -= memops.
153 inline SDValue XformM5ToU5Imm(signed Imm) {
154    assert( (Imm >= -31 && Imm <= -1)  && "Constant out of range for Memops");
155    return CurDAG->getTargetConstant( - Imm, MVT::i32);
156 }
157
158
159 // XformU7ToU7M1Imm - Return a target constant decremented by 1, in range
160 // [1..128], used in cmpb.gtu instructions.
161 inline SDValue XformU7ToU7M1Imm(signed Imm) {
162   assert((Imm >= 1 && Imm <= 128) && "Constant out of range for cmpb op");
163   return CurDAG->getTargetConstant(Imm - 1, MVT::i8);
164 }
165
166 // XformS8ToS8M1Imm - Return a target constant decremented by 1.
167 inline SDValue XformSToSM1Imm(signed Imm) {
168   return CurDAG->getTargetConstant(Imm - 1, MVT::i32);
169 }
170
171 // XformU8ToU8M1Imm - Return a target constant decremented by 1.
172 inline SDValue XformUToUM1Imm(unsigned Imm) {
173   assert((Imm >= 1) && "Cannot decrement unsigned int less than 1");
174   return CurDAG->getTargetConstant(Imm - 1, MVT::i32);
175 }
176
177 // XformSToSM2Imm - Return a target constant decremented by 2.
178 inline SDValue XformSToSM2Imm(unsigned Imm) {
179   return CurDAG->getTargetConstant(Imm - 2, MVT::i32);
180 }
181
182 // XformSToSM3Imm - Return a target constant decremented by 3.
183 inline SDValue XformSToSM3Imm(unsigned Imm) {
184   return CurDAG->getTargetConstant(Imm - 3, MVT::i32);
185 }
186
187 // Include the pieces autogenerated from the target description.
188 #include "HexagonGenDAGISel.inc"
189
190 private:
191   bool isValueExtension(SDValue const &Val, unsigned FromBits, SDValue &Src);
192 };
193 }  // end anonymous namespace
194
195
196 /// createHexagonISelDag - This pass converts a legalized DAG into a
197 /// Hexagon-specific DAG, ready for instruction scheduling.
198 ///
199 FunctionPass *llvm::createHexagonISelDag(HexagonTargetMachine &TM,
200                                          CodeGenOpt::Level OptLevel) {
201   return new HexagonDAGToDAGISel(TM, OptLevel);
202 }
203
204 static void initializePassOnce(PassRegistry &Registry) {
205   const char *Name = "Hexagon DAG->DAG Pattern Instruction Selection";
206   PassInfo *PI = new PassInfo(Name, "hexagon-isel",
207                               &SelectionDAGISel::ID, nullptr, false, false);
208   Registry.registerPass(*PI, true);
209 }
210
211 void llvm::initializeHexagonDAGToDAGISelPass(PassRegistry &Registry) {
212   CALL_ONCE_INITIALIZATION(initializePassOnce)
213 }
214
215
216 static bool IsS11_0_Offset(SDNode * S) {
217     ConstantSDNode *N = cast<ConstantSDNode>(S);
218
219   // immS16 predicate - True if the immediate fits in a 16-bit sign extended
220   // field.
221   int64_t v = (int64_t)N->getSExtValue();
222   return isInt<11>(v);
223 }
224
225
226 static bool IsS11_1_Offset(SDNode * S) {
227     ConstantSDNode *N = cast<ConstantSDNode>(S);
228
229   // immS16 predicate - True if the immediate fits in a 16-bit sign extended
230   // field.
231   int64_t v = (int64_t)N->getSExtValue();
232   return isShiftedInt<11,1>(v);
233 }
234
235
236 static bool IsS11_2_Offset(SDNode * S) {
237     ConstantSDNode *N = cast<ConstantSDNode>(S);
238
239   // immS16 predicate - True if the immediate fits in a 16-bit sign extended
240   // field.
241   int64_t v = (int64_t)N->getSExtValue();
242   return isShiftedInt<11,2>(v);
243 }
244
245
246 static bool IsS11_3_Offset(SDNode * S) {
247     ConstantSDNode *N = cast<ConstantSDNode>(S);
248
249   // immS16 predicate - True if the immediate fits in a 16-bit sign extended
250   // field.
251   int64_t v = (int64_t)N->getSExtValue();
252   return isShiftedInt<11,3>(v);
253 }
254
255
256 static bool IsU6_0_Offset(SDNode * S) {
257     ConstantSDNode *N = cast<ConstantSDNode>(S);
258
259   // u6 predicate - True if the immediate fits in a 6-bit unsigned extended
260   // field.
261   int64_t v = (int64_t)N->getSExtValue();
262   return isUInt<6>(v);
263 }
264
265
266 static bool IsU6_1_Offset(SDNode * S) {
267     ConstantSDNode *N = cast<ConstantSDNode>(S);
268
269   // u6 predicate - True if the immediate fits in a 6-bit unsigned extended
270   // field.
271   int64_t v = (int64_t)N->getSExtValue();
272   return isShiftedUInt<6,1>(v);
273 }
274
275
276 static bool IsU6_2_Offset(SDNode * S) {
277     ConstantSDNode *N = cast<ConstantSDNode>(S);
278
279   // u6 predicate - True if the immediate fits in a 6-bit unsigned extended
280   // field.
281   int64_t v = (int64_t)N->getSExtValue();
282   return isShiftedUInt<6,2>(v);
283 }
284
285
286 // Intrinsics that return a a predicate.
287 static unsigned doesIntrinsicReturnPredicate(unsigned ID)
288 {
289   switch (ID) {
290     default:
291       return 0;
292     case Intrinsic::hexagon_C2_cmpeq:
293     case Intrinsic::hexagon_C2_cmpgt:
294     case Intrinsic::hexagon_C2_cmpgtu:
295     case Intrinsic::hexagon_C2_cmpgtup:
296     case Intrinsic::hexagon_C2_cmpgtp:
297     case Intrinsic::hexagon_C2_cmpeqp:
298     case Intrinsic::hexagon_C2_bitsset:
299     case Intrinsic::hexagon_C2_bitsclr:
300     case Intrinsic::hexagon_C2_cmpeqi:
301     case Intrinsic::hexagon_C2_cmpgti:
302     case Intrinsic::hexagon_C2_cmpgtui:
303     case Intrinsic::hexagon_C2_cmpgei:
304     case Intrinsic::hexagon_C2_cmpgeui:
305     case Intrinsic::hexagon_C2_cmplt:
306     case Intrinsic::hexagon_C2_cmpltu:
307     case Intrinsic::hexagon_C2_bitsclri:
308     case Intrinsic::hexagon_C2_and:
309     case Intrinsic::hexagon_C2_or:
310     case Intrinsic::hexagon_C2_xor:
311     case Intrinsic::hexagon_C2_andn:
312     case Intrinsic::hexagon_C2_not:
313     case Intrinsic::hexagon_C2_orn:
314     case Intrinsic::hexagon_C2_pxfer_map:
315     case Intrinsic::hexagon_C2_any8:
316     case Intrinsic::hexagon_C2_all8:
317     case Intrinsic::hexagon_A2_vcmpbeq:
318     case Intrinsic::hexagon_A2_vcmpbgtu:
319     case Intrinsic::hexagon_A2_vcmpheq:
320     case Intrinsic::hexagon_A2_vcmphgt:
321     case Intrinsic::hexagon_A2_vcmphgtu:
322     case Intrinsic::hexagon_A2_vcmpweq:
323     case Intrinsic::hexagon_A2_vcmpwgt:
324     case Intrinsic::hexagon_A2_vcmpwgtu:
325     case Intrinsic::hexagon_C2_tfrrp:
326     case Intrinsic::hexagon_S2_tstbit_i:
327     case Intrinsic::hexagon_S2_tstbit_r:
328       return 1;
329   }
330 }
331
332
333 SDNode *HexagonDAGToDAGISel::SelectIndexedLoadSignExtend64(LoadSDNode *LD,
334                                                            unsigned Opcode,
335                                                            SDLoc dl)
336 {
337   SDValue Chain = LD->getChain();
338   EVT LoadedVT = LD->getMemoryVT();
339   SDValue Base = LD->getBasePtr();
340   SDValue Offset = LD->getOffset();
341   SDNode *OffsetNode = Offset.getNode();
342   int32_t Val = cast<ConstantSDNode>(OffsetNode)->getSExtValue();
343   SDValue N1 = LD->getOperand(1);
344   SDValue CPTmpN1_0;
345   SDValue CPTmpN1_1;
346
347   if (SelectADDRriS11_2(N1, CPTmpN1_0, CPTmpN1_1) &&
348       N1.getNode()->getValueType(0) == MVT::i32) {
349     const HexagonInstrInfo *TII = Subtarget->getInstrInfo();
350     if (TII->isValidAutoIncImm(LoadedVT, Val)) {
351       SDValue TargetConst = CurDAG->getTargetConstant(Val, MVT::i32);
352       SDNode *Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::i32, MVT::i32,
353                                                 MVT::Other, Base, TargetConst,
354                                                 Chain);
355       SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A2_sxtw, dl, MVT::i64,
356                                                 SDValue(Result_1, 0));
357       MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
358       MemOp[0] = LD->getMemOperand();
359       cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
360       const SDValue Froms[] = { SDValue(LD, 0),
361                                 SDValue(LD, 1),
362                                 SDValue(LD, 2)
363       };
364       const SDValue Tos[]   = { SDValue(Result_2, 0),
365                                 SDValue(Result_1, 1),
366                                 SDValue(Result_1, 2)
367       };
368       ReplaceUses(Froms, Tos, 3);
369       return Result_2;
370     }
371     SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
372     SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
373     SDNode *Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::i32,
374                                               MVT::Other, Base, TargetConst0,
375                                               Chain);
376     SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A2_sxtw, dl,
377                                                 MVT::i64, SDValue(Result_1, 0));
378     SDNode* Result_3 = CurDAG->getMachineNode(Hexagon::A2_addi, dl,
379                                               MVT::i32, Base, TargetConstVal,
380                                                 SDValue(Result_1, 1));
381     MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
382     MemOp[0] = LD->getMemOperand();
383     cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
384     const SDValue Froms[] = { SDValue(LD, 0),
385                               SDValue(LD, 1),
386                               SDValue(LD, 2)
387     };
388     const SDValue Tos[]   = { SDValue(Result_2, 0),
389                               SDValue(Result_3, 0),
390                               SDValue(Result_1, 1)
391     };
392     ReplaceUses(Froms, Tos, 3);
393     return Result_2;
394   }
395   return SelectCode(LD);
396 }
397
398
399 SDNode *HexagonDAGToDAGISel::SelectIndexedLoadZeroExtend64(LoadSDNode *LD,
400                                                            unsigned Opcode,
401                                                            SDLoc dl)
402 {
403   SDValue Chain = LD->getChain();
404   EVT LoadedVT = LD->getMemoryVT();
405   SDValue Base = LD->getBasePtr();
406   SDValue Offset = LD->getOffset();
407   SDNode *OffsetNode = Offset.getNode();
408   int32_t Val = cast<ConstantSDNode>(OffsetNode)->getSExtValue();
409   SDValue N1 = LD->getOperand(1);
410   SDValue CPTmpN1_0;
411   SDValue CPTmpN1_1;
412
413   if (SelectADDRriS11_2(N1, CPTmpN1_0, CPTmpN1_1) &&
414       N1.getNode()->getValueType(0) == MVT::i32) {
415     const HexagonInstrInfo *TII = Subtarget->getInstrInfo();
416     if (TII->isValidAutoIncImm(LoadedVT, Val)) {
417       SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
418       SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
419       SDNode *Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::i32,
420                                                 MVT::i32, MVT::Other, Base,
421                                                 TargetConstVal, Chain);
422       SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A2_tfrsi, dl, MVT::i32,
423                                                 TargetConst0);
424       SDNode *Result_3 = CurDAG->getMachineNode(Hexagon::A2_combinew, dl,
425                                                 MVT::i64, MVT::Other,
426                                                 SDValue(Result_2,0),
427                                                 SDValue(Result_1,0));
428       MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
429       MemOp[0] = LD->getMemOperand();
430       cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
431       const SDValue Froms[] = { SDValue(LD, 0),
432                                 SDValue(LD, 1),
433                                 SDValue(LD, 2)
434       };
435       const SDValue Tos[]   = { SDValue(Result_3, 0),
436                                 SDValue(Result_1, 1),
437                                 SDValue(Result_1, 2)
438       };
439       ReplaceUses(Froms, Tos, 3);
440       return Result_3;
441     }
442
443     // Generate an indirect load.
444     SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
445     SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
446     SDNode *Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::i32,
447                                               MVT::Other,
448                                               Base, TargetConst0, Chain);
449     SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A2_tfrsi, dl, MVT::i32,
450                                               TargetConst0);
451     SDNode *Result_3 = CurDAG->getMachineNode(Hexagon::A2_combinew, dl,
452                                               MVT::i64, MVT::Other,
453                                               SDValue(Result_2,0),
454                                               SDValue(Result_1,0));
455     // Add offset to base.
456     SDNode* Result_4 = CurDAG->getMachineNode(Hexagon::A2_addi, dl, MVT::i32,
457                                               Base, TargetConstVal,
458                                               SDValue(Result_1, 1));
459     MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
460     MemOp[0] = LD->getMemOperand();
461     cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
462     const SDValue Froms[] = { SDValue(LD, 0),
463                               SDValue(LD, 1),
464                               SDValue(LD, 2)
465     };
466     const SDValue Tos[]   = { SDValue(Result_3, 0), // Load value.
467                               SDValue(Result_4, 0), // New address.
468                               SDValue(Result_1, 1)
469     };
470     ReplaceUses(Froms, Tos, 3);
471     return Result_3;
472   }
473
474   return SelectCode(LD);
475 }
476
477
478 SDNode *HexagonDAGToDAGISel::SelectIndexedLoad(LoadSDNode *LD, SDLoc dl) {
479   SDValue Chain = LD->getChain();
480   SDValue Base = LD->getBasePtr();
481   SDValue Offset = LD->getOffset();
482   SDNode *OffsetNode = Offset.getNode();
483   // Get the constant value.
484   int32_t Val = cast<ConstantSDNode>(OffsetNode)->getSExtValue();
485   EVT LoadedVT = LD->getMemoryVT();
486   unsigned Opcode = 0;
487
488   // Check for zero ext loads.
489   bool zextval = (LD->getExtensionType() == ISD::ZEXTLOAD);
490
491   // Figure out the opcode.
492   const HexagonInstrInfo *TII = Subtarget->getInstrInfo();
493   if (LoadedVT == MVT::i64) {
494     if (TII->isValidAutoIncImm(LoadedVT, Val))
495       Opcode = Hexagon::L2_loadrd_pi;
496     else
497       Opcode = Hexagon::L2_loadrd_io;
498   } else if (LoadedVT == MVT::i32) {
499     if (TII->isValidAutoIncImm(LoadedVT, Val))
500       Opcode = Hexagon::L2_loadri_pi;
501     else
502       Opcode = Hexagon::L2_loadri_io;
503   } else if (LoadedVT == MVT::i16) {
504     if (TII->isValidAutoIncImm(LoadedVT, Val))
505       Opcode = zextval ? Hexagon::L2_loadruh_pi : Hexagon::L2_loadrh_pi;
506     else
507       Opcode = zextval ? Hexagon::L2_loadruh_io : Hexagon::L2_loadrh_io;
508   } else if (LoadedVT == MVT::i8) {
509     if (TII->isValidAutoIncImm(LoadedVT, Val))
510       Opcode = zextval ? Hexagon::L2_loadrub_pi : Hexagon::L2_loadrb_pi;
511     else
512       Opcode = zextval ? Hexagon::L2_loadrub_io : Hexagon::L2_loadrb_io;
513   } else
514     llvm_unreachable("unknown memory type");
515
516   // For zero ext i64 loads, we need to add combine instructions.
517   if (LD->getValueType(0) == MVT::i64 &&
518       LD->getExtensionType() == ISD::ZEXTLOAD) {
519     return SelectIndexedLoadZeroExtend64(LD, Opcode, dl);
520   }
521   if (LD->getValueType(0) == MVT::i64 &&
522              LD->getExtensionType() == ISD::SEXTLOAD) {
523     // Handle sign ext i64 loads.
524     return SelectIndexedLoadSignExtend64(LD, Opcode, dl);
525   }
526   if (TII->isValidAutoIncImm(LoadedVT, Val)) {
527     SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
528     SDNode* Result = CurDAG->getMachineNode(Opcode, dl,
529                                             LD->getValueType(0),
530                                             MVT::i32, MVT::Other, Base,
531                                             TargetConstVal, Chain);
532     MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
533     MemOp[0] = LD->getMemOperand();
534     cast<MachineSDNode>(Result)->setMemRefs(MemOp, MemOp + 1);
535     const SDValue Froms[] = { SDValue(LD, 0),
536                               SDValue(LD, 1),
537                               SDValue(LD, 2)
538     };
539     const SDValue Tos[]   = { SDValue(Result, 0),
540                               SDValue(Result, 1),
541                               SDValue(Result, 2)
542     };
543     ReplaceUses(Froms, Tos, 3);
544     return Result;
545   } else {
546     SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
547     SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
548     SDNode* Result_1 = CurDAG->getMachineNode(Opcode, dl,
549                                               LD->getValueType(0),
550                                               MVT::Other, Base, TargetConst0,
551                                               Chain);
552     SDNode* Result_2 = CurDAG->getMachineNode(Hexagon::A2_addi, dl, MVT::i32,
553                                               Base, TargetConstVal,
554                                               SDValue(Result_1, 1));
555     MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
556     MemOp[0] = LD->getMemOperand();
557     cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
558     const SDValue Froms[] = { SDValue(LD, 0),
559                               SDValue(LD, 1),
560                               SDValue(LD, 2)
561     };
562     const SDValue Tos[]   = { SDValue(Result_1, 0),
563                               SDValue(Result_2, 0),
564                               SDValue(Result_1, 1)
565     };
566     ReplaceUses(Froms, Tos, 3);
567     return Result_1;
568   }
569 }
570
571
572 SDNode *HexagonDAGToDAGISel::SelectLoad(SDNode *N) {
573   SDNode *result;
574   SDLoc dl(N);
575   LoadSDNode *LD = cast<LoadSDNode>(N);
576   ISD::MemIndexedMode AM = LD->getAddressingMode();
577
578   // Handle indexed loads.
579   if (AM != ISD::UNINDEXED) {
580     result = SelectIndexedLoad(LD, dl);
581   } else {
582     result = SelectCode(LD);
583   }
584
585   return result;
586 }
587
588
589 SDNode *HexagonDAGToDAGISel::SelectIndexedStore(StoreSDNode *ST, SDLoc dl) {
590   SDValue Chain = ST->getChain();
591   SDValue Base = ST->getBasePtr();
592   SDValue Offset = ST->getOffset();
593   SDValue Value = ST->getValue();
594   SDNode *OffsetNode = Offset.getNode();
595   // Get the constant value.
596   int32_t Val = cast<ConstantSDNode>(OffsetNode)->getSExtValue();
597   EVT StoredVT = ST->getMemoryVT();
598
599   // Offset value must be within representable range
600   // and must have correct alignment properties.
601   const HexagonInstrInfo *TII = Subtarget->getInstrInfo();
602   if (TII->isValidAutoIncImm(StoredVT, Val)) {
603     SDValue Ops[] = {Base, CurDAG->getTargetConstant(Val, MVT::i32), Value,
604                      Chain};
605     unsigned Opcode = 0;
606
607     // Figure out the post inc version of opcode.
608     if (StoredVT == MVT::i64) Opcode = Hexagon::S2_storerd_pi;
609     else if (StoredVT == MVT::i32) Opcode = Hexagon::S2_storeri_pi;
610     else if (StoredVT == MVT::i16) Opcode = Hexagon::S2_storerh_pi;
611     else if (StoredVT == MVT::i8) Opcode = Hexagon::S2_storerb_pi;
612     else llvm_unreachable("unknown memory type");
613
614     // Build post increment store.
615     SDNode* Result = CurDAG->getMachineNode(Opcode, dl, MVT::i32,
616                                             MVT::Other, Ops);
617     MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
618     MemOp[0] = ST->getMemOperand();
619     cast<MachineSDNode>(Result)->setMemRefs(MemOp, MemOp + 1);
620
621     ReplaceUses(ST, Result);
622     ReplaceUses(SDValue(ST,1), SDValue(Result,1));
623     return Result;
624   }
625
626   // Note: Order of operands matches the def of instruction:
627   // def STrid : STInst<(outs), (ins MEMri:$addr, DoubleRegs:$src1), ...
628   // and it differs for POST_ST* for instance.
629   SDValue Ops[] = { Base, CurDAG->getTargetConstant(0, MVT::i32), Value,
630                     Chain};
631   unsigned Opcode = 0;
632
633   // Figure out the opcode.
634   if (StoredVT == MVT::i64) Opcode = Hexagon::S2_storerd_io;
635   else if (StoredVT == MVT::i32) Opcode = Hexagon::S2_storeri_io;
636   else if (StoredVT == MVT::i16) Opcode = Hexagon::S2_storerh_io;
637   else if (StoredVT == MVT::i8) Opcode = Hexagon::S2_storerb_io;
638   else llvm_unreachable("unknown memory type");
639
640   // Build regular store.
641   SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
642   SDNode* Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::Other, Ops);
643   // Build splitted incriment instruction.
644   SDNode* Result_2 = CurDAG->getMachineNode(Hexagon::A2_addi, dl, MVT::i32,
645                                             Base,
646                                             TargetConstVal,
647                                             SDValue(Result_1, 0));
648   MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
649   MemOp[0] = ST->getMemOperand();
650   cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
651
652   ReplaceUses(SDValue(ST,0), SDValue(Result_2,0));
653   ReplaceUses(SDValue(ST,1), SDValue(Result_1,0));
654   return Result_2;
655 }
656
657
658 SDNode *HexagonDAGToDAGISel::SelectStore(SDNode *N) {
659   SDLoc dl(N);
660   StoreSDNode *ST = cast<StoreSDNode>(N);
661   ISD::MemIndexedMode AM = ST->getAddressingMode();
662
663   // Handle indexed stores.
664   if (AM != ISD::UNINDEXED) {
665     return SelectIndexedStore(ST, dl);
666   }
667
668   return SelectCode(ST);
669 }
670
671 SDNode *HexagonDAGToDAGISel::SelectMul(SDNode *N) {
672   SDLoc dl(N);
673
674   //
675   // %conv.i = sext i32 %tmp1 to i64
676   // %conv2.i = sext i32 %add to i64
677   // %mul.i = mul nsw i64 %conv2.i, %conv.i
678   //
679   //   --- match with the following ---
680   //
681   // %mul.i = mpy (%tmp1, %add)
682   //
683
684   if (N->getValueType(0) == MVT::i64) {
685     // Shifting a i64 signed multiply.
686     SDValue MulOp0 = N->getOperand(0);
687     SDValue MulOp1 = N->getOperand(1);
688
689     SDValue OP0;
690     SDValue OP1;
691
692     // Handle sign_extend and sextload.
693     if (MulOp0.getOpcode() == ISD::SIGN_EXTEND) {
694       SDValue Sext0 = MulOp0.getOperand(0);
695       if (Sext0.getNode()->getValueType(0) != MVT::i32) {
696         return SelectCode(N);
697       }
698
699       OP0 = Sext0;
700     } else if (MulOp0.getOpcode() == ISD::LOAD) {
701       LoadSDNode *LD = cast<LoadSDNode>(MulOp0.getNode());
702       if (LD->getMemoryVT() != MVT::i32 ||
703           LD->getExtensionType() != ISD::SEXTLOAD ||
704           LD->getAddressingMode() != ISD::UNINDEXED) {
705         return SelectCode(N);
706       }
707
708       SDValue Chain = LD->getChain();
709       SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
710       OP0 = SDValue(CurDAG->getMachineNode(Hexagon::L2_loadri_io, dl, MVT::i32,
711                                             MVT::Other,
712                                             LD->getBasePtr(), TargetConst0,
713                                             Chain), 0);
714     } else {
715       return SelectCode(N);
716     }
717
718     // Same goes for the second operand.
719     if (MulOp1.getOpcode() == ISD::SIGN_EXTEND) {
720       SDValue Sext1 = MulOp1.getOperand(0);
721       if (Sext1.getNode()->getValueType(0) != MVT::i32) {
722         return SelectCode(N);
723       }
724
725       OP1 = Sext1;
726     } else if (MulOp1.getOpcode() == ISD::LOAD) {
727       LoadSDNode *LD = cast<LoadSDNode>(MulOp1.getNode());
728       if (LD->getMemoryVT() != MVT::i32 ||
729           LD->getExtensionType() != ISD::SEXTLOAD ||
730           LD->getAddressingMode() != ISD::UNINDEXED) {
731         return SelectCode(N);
732       }
733
734       SDValue Chain = LD->getChain();
735       SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
736       OP1 = SDValue(CurDAG->getMachineNode(Hexagon::L2_loadri_io, dl, MVT::i32,
737                                             MVT::Other,
738                                             LD->getBasePtr(), TargetConst0,
739                                             Chain), 0);
740     } else {
741       return SelectCode(N);
742     }
743
744     // Generate a mpy instruction.
745     SDNode *Result = CurDAG->getMachineNode(Hexagon::M2_dpmpyss_s0, dl, MVT::i64,
746                                             OP0, OP1);
747     ReplaceUses(N, Result);
748     return Result;
749   }
750
751   return SelectCode(N);
752 }
753
754
755 SDNode *HexagonDAGToDAGISel::SelectSelect(SDNode *N) {
756   SDLoc dl(N);
757   SDValue N0 = N->getOperand(0);
758   if (N0.getOpcode() == ISD::SETCC) {
759     SDValue N00 = N0.getOperand(0);
760     if (N00.getOpcode() == ISD::SIGN_EXTEND_INREG) {
761       SDValue N000 = N00.getOperand(0);
762       SDValue N001 = N00.getOperand(1);
763       if (cast<VTSDNode>(N001)->getVT() == MVT::i16) {
764         SDValue N01 = N0.getOperand(1);
765         SDValue N02 = N0.getOperand(2);
766
767         // Pattern: (select:i32 (setcc:i1 (sext_inreg:i32 IntRegs:i32:$src2,
768         // i16:Other),IntRegs:i32:$src1, SETLT:Other),IntRegs:i32:$src1,
769         // IntRegs:i32:$src2)
770         // Emits: (MAXh_rr:i32 IntRegs:i32:$src1, IntRegs:i32:$src2)
771         // Pattern complexity = 9  cost = 1  size = 0.
772         if (cast<CondCodeSDNode>(N02)->get() == ISD::SETLT) {
773           SDValue N1 = N->getOperand(1);
774           if (N01 == N1) {
775             SDValue N2 = N->getOperand(2);
776             if (N000 == N2 &&
777                 N0.getNode()->getValueType(N0.getResNo()) == MVT::i1 &&
778                 N00.getNode()->getValueType(N00.getResNo()) == MVT::i32) {
779               SDNode *SextNode = CurDAG->getMachineNode(Hexagon::A2_sxth, dl,
780                                                         MVT::i32, N000);
781               SDNode *Result = CurDAG->getMachineNode(Hexagon::A2_max, dl,
782                                                       MVT::i32,
783                                                       SDValue(SextNode, 0),
784                                                       N1);
785               ReplaceUses(N, Result);
786               return Result;
787             }
788           }
789         }
790
791         // Pattern: (select:i32 (setcc:i1 (sext_inreg:i32 IntRegs:i32:$src2,
792         // i16:Other), IntRegs:i32:$src1, SETGT:Other), IntRegs:i32:$src1,
793         // IntRegs:i32:$src2)
794         // Emits: (MINh_rr:i32 IntRegs:i32:$src1, IntRegs:i32:$src2)
795         // Pattern complexity = 9  cost = 1  size = 0.
796         if (cast<CondCodeSDNode>(N02)->get() == ISD::SETGT) {
797           SDValue N1 = N->getOperand(1);
798           if (N01 == N1) {
799             SDValue N2 = N->getOperand(2);
800             if (N000 == N2 &&
801                 N0.getNode()->getValueType(N0.getResNo()) == MVT::i1 &&
802                 N00.getNode()->getValueType(N00.getResNo()) == MVT::i32) {
803               SDNode *SextNode = CurDAG->getMachineNode(Hexagon::A2_sxth, dl,
804                                                         MVT::i32, N000);
805               SDNode *Result = CurDAG->getMachineNode(Hexagon::A2_min, dl,
806                                                       MVT::i32,
807                                                       SDValue(SextNode, 0),
808                                                       N1);
809               ReplaceUses(N, Result);
810               return Result;
811             }
812           }
813         }
814       }
815     }
816   }
817
818   return SelectCode(N);
819 }
820
821
822 SDNode *HexagonDAGToDAGISel::SelectTruncate(SDNode *N) {
823   SDLoc dl(N);
824   SDValue Shift = N->getOperand(0);
825
826   //
827   // %conv.i = sext i32 %tmp1 to i64
828   // %conv2.i = sext i32 %add to i64
829   // %mul.i = mul nsw i64 %conv2.i, %conv.i
830   // %shr5.i = lshr i64 %mul.i, 32
831   // %conv3.i = trunc i64 %shr5.i to i32
832   //
833   //   --- match with the following ---
834   //
835   // %conv3.i = mpy (%tmp1, %add)
836   //
837   // Trunc to i32.
838   if (N->getValueType(0) == MVT::i32) {
839     // Trunc from i64.
840     if (Shift.getNode()->getValueType(0) == MVT::i64) {
841       // Trunc child is logical shift right.
842       if (Shift.getOpcode() != ISD::SRL) {
843         return SelectCode(N);
844       }
845
846       SDValue ShiftOp0 = Shift.getOperand(0);
847       SDValue ShiftOp1 = Shift.getOperand(1);
848
849       // Shift by const 32
850       if (ShiftOp1.getOpcode() != ISD::Constant) {
851         return SelectCode(N);
852       }
853
854       int32_t ShiftConst =
855         cast<ConstantSDNode>(ShiftOp1.getNode())->getSExtValue();
856       if (ShiftConst != 32) {
857         return SelectCode(N);
858       }
859
860       // Shifting a i64 signed multiply
861       SDValue Mul = ShiftOp0;
862       if (Mul.getOpcode() != ISD::MUL) {
863         return SelectCode(N);
864       }
865
866       SDValue MulOp0 = Mul.getOperand(0);
867       SDValue MulOp1 = Mul.getOperand(1);
868
869       SDValue OP0;
870       SDValue OP1;
871
872       // Handle sign_extend and sextload
873       if (MulOp0.getOpcode() == ISD::SIGN_EXTEND) {
874         SDValue Sext0 = MulOp0.getOperand(0);
875         if (Sext0.getNode()->getValueType(0) != MVT::i32) {
876           return SelectCode(N);
877         }
878
879         OP0 = Sext0;
880       } else if (MulOp0.getOpcode() == ISD::LOAD) {
881         LoadSDNode *LD = cast<LoadSDNode>(MulOp0.getNode());
882         if (LD->getMemoryVT() != MVT::i32 ||
883             LD->getExtensionType() != ISD::SEXTLOAD ||
884             LD->getAddressingMode() != ISD::UNINDEXED) {
885           return SelectCode(N);
886         }
887
888         SDValue Chain = LD->getChain();
889         SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
890         OP0 = SDValue(CurDAG->getMachineNode(Hexagon::L2_loadri_io, dl, MVT::i32,
891                                               MVT::Other,
892                                               LD->getBasePtr(),
893                                               TargetConst0, Chain), 0);
894       } else {
895         return SelectCode(N);
896       }
897
898       // Same goes for the second operand.
899       if (MulOp1.getOpcode() == ISD::SIGN_EXTEND) {
900         SDValue Sext1 = MulOp1.getOperand(0);
901         if (Sext1.getNode()->getValueType(0) != MVT::i32)
902           return SelectCode(N);
903
904         OP1 = Sext1;
905       } else if (MulOp1.getOpcode() == ISD::LOAD) {
906         LoadSDNode *LD = cast<LoadSDNode>(MulOp1.getNode());
907         if (LD->getMemoryVT() != MVT::i32 ||
908             LD->getExtensionType() != ISD::SEXTLOAD ||
909             LD->getAddressingMode() != ISD::UNINDEXED) {
910           return SelectCode(N);
911         }
912
913         SDValue Chain = LD->getChain();
914         SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
915         OP1 = SDValue(CurDAG->getMachineNode(Hexagon::L2_loadri_io, dl, MVT::i32,
916                                               MVT::Other,
917                                               LD->getBasePtr(),
918                                               TargetConst0, Chain), 0);
919       } else {
920         return SelectCode(N);
921       }
922
923       // Generate a mpy instruction.
924       SDNode *Result = CurDAG->getMachineNode(Hexagon::M2_mpy_up, dl, MVT::i32,
925                                               OP0, OP1);
926       ReplaceUses(N, Result);
927       return Result;
928     }
929   }
930
931   return SelectCode(N);
932 }
933
934
935 SDNode *HexagonDAGToDAGISel::SelectSHL(SDNode *N) {
936   SDLoc dl(N);
937   if (N->getValueType(0) == MVT::i32) {
938     SDValue Shl_0 = N->getOperand(0);
939     SDValue Shl_1 = N->getOperand(1);
940     // RHS is const.
941     if (Shl_1.getOpcode() == ISD::Constant) {
942       if (Shl_0.getOpcode() == ISD::MUL) {
943         SDValue Mul_0 = Shl_0.getOperand(0); // Val
944         SDValue Mul_1 = Shl_0.getOperand(1); // Const
945         // RHS of mul is const.
946         if (Mul_1.getOpcode() == ISD::Constant) {
947           int32_t ShlConst =
948             cast<ConstantSDNode>(Shl_1.getNode())->getSExtValue();
949           int32_t MulConst =
950             cast<ConstantSDNode>(Mul_1.getNode())->getSExtValue();
951           int32_t ValConst = MulConst << ShlConst;
952           SDValue Val = CurDAG->getTargetConstant(ValConst,
953                                                   MVT::i32);
954           if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Val.getNode()))
955             if (isInt<9>(CN->getSExtValue())) {
956               SDNode* Result =
957                 CurDAG->getMachineNode(Hexagon::M2_mpysmi, dl,
958                                        MVT::i32, Mul_0, Val);
959               ReplaceUses(N, Result);
960               return Result;
961             }
962
963         }
964       } else if (Shl_0.getOpcode() == ISD::SUB) {
965         SDValue Sub_0 = Shl_0.getOperand(0); // Const 0
966         SDValue Sub_1 = Shl_0.getOperand(1); // Val
967         if (Sub_0.getOpcode() == ISD::Constant) {
968           int32_t SubConst =
969             cast<ConstantSDNode>(Sub_0.getNode())->getSExtValue();
970           if (SubConst == 0) {
971             if (Sub_1.getOpcode() == ISD::SHL) {
972               SDValue Shl2_0 = Sub_1.getOperand(0); // Val
973               SDValue Shl2_1 = Sub_1.getOperand(1); // Const
974               if (Shl2_1.getOpcode() == ISD::Constant) {
975                 int32_t ShlConst =
976                   cast<ConstantSDNode>(Shl_1.getNode())->getSExtValue();
977                 int32_t Shl2Const =
978                   cast<ConstantSDNode>(Shl2_1.getNode())->getSExtValue();
979                 int32_t ValConst = 1 << (ShlConst+Shl2Const);
980                 SDValue Val = CurDAG->getTargetConstant(-ValConst, MVT::i32);
981                 if (ConstantSDNode *CN =
982                     dyn_cast<ConstantSDNode>(Val.getNode()))
983                   if (isInt<9>(CN->getSExtValue())) {
984                     SDNode* Result =
985                       CurDAG->getMachineNode(Hexagon::M2_mpysmi, dl, MVT::i32,
986                                              Shl2_0, Val);
987                     ReplaceUses(N, Result);
988                     return Result;
989                   }
990               }
991             }
992           }
993         }
994       }
995     }
996   }
997   return SelectCode(N);
998 }
999
1000
1001 //
1002 // If there is an zero_extend followed an intrinsic in DAG (this means - the
1003 // result of the intrinsic is predicate); convert the zero_extend to
1004 // transfer instruction.
1005 //
1006 // Zero extend -> transfer is lowered here. Otherwise, zero_extend will be
1007 // converted into a MUX as predicate registers defined as 1 bit in the
1008 // compiler. Architecture defines them as 8-bit registers.
1009 // We want to preserve all the lower 8-bits and, not just 1 LSB bit.
1010 //
1011 SDNode *HexagonDAGToDAGISel::SelectZeroExtend(SDNode *N) {
1012   SDLoc dl(N);
1013   SDNode *IsIntrinsic = N->getOperand(0).getNode();
1014   if ((IsIntrinsic->getOpcode() == ISD::INTRINSIC_WO_CHAIN)) {
1015     unsigned ID =
1016       cast<ConstantSDNode>(IsIntrinsic->getOperand(0))->getZExtValue();
1017     if (doesIntrinsicReturnPredicate(ID)) {
1018       // Now we need to differentiate target data types.
1019       if (N->getValueType(0) == MVT::i64) {
1020         // Convert the zero_extend to Rs = Pd followed by COMBINE_rr(0,Rs).
1021         SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
1022         SDNode *Result_1 = CurDAG->getMachineNode(Hexagon::C2_tfrpr, dl,
1023                                                   MVT::i32,
1024                                                   SDValue(IsIntrinsic, 0));
1025         SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A2_tfrsi, dl,
1026                                                   MVT::i32,
1027                                                   TargetConst0);
1028         SDNode *Result_3 = CurDAG->getMachineNode(Hexagon::A2_combinew, dl,
1029                                                   MVT::i64, MVT::Other,
1030                                                   SDValue(Result_2, 0),
1031                                                   SDValue(Result_1, 0));
1032         ReplaceUses(N, Result_3);
1033         return Result_3;
1034       }
1035       if (N->getValueType(0) == MVT::i32) {
1036         // Convert the zero_extend to Rs = Pd
1037         SDNode* RsPd = CurDAG->getMachineNode(Hexagon::C2_tfrpr, dl,
1038                                               MVT::i32,
1039                                               SDValue(IsIntrinsic, 0));
1040         ReplaceUses(N, RsPd);
1041         return RsPd;
1042       }
1043       llvm_unreachable("Unexpected value type");
1044     }
1045   }
1046   return SelectCode(N);
1047 }
1048
1049 //
1050 // Checking for intrinsics which have predicate registers as operand(s)
1051 // and lowering to the actual intrinsic.
1052 //
1053 SDNode *HexagonDAGToDAGISel::SelectIntrinsicWOChain(SDNode *N) {
1054   unsigned IID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
1055   unsigned Bits;
1056   switch (IID) {
1057   case Intrinsic::hexagon_S2_vsplatrb:
1058     Bits = 8;
1059     break;
1060   case Intrinsic::hexagon_S2_vsplatrh:
1061     Bits = 16;
1062     break;
1063   default:
1064     return SelectCode(N);
1065   }
1066
1067   SDValue const &V = N->getOperand(1);
1068   SDValue U;
1069   if (isValueExtension(V, Bits, U)) {
1070     SDValue R = CurDAG->getNode(N->getOpcode(), SDLoc(N), N->getValueType(0),
1071       N->getOperand(0), U);
1072     return SelectCode(R.getNode());
1073   }
1074   return SelectCode(N);
1075 }
1076
1077 //
1078 // Map floating point constant values.
1079 //
1080 SDNode *HexagonDAGToDAGISel::SelectConstantFP(SDNode *N) {
1081   SDLoc dl(N);
1082   ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N);
1083   APFloat APF = CN->getValueAPF();
1084   if (N->getValueType(0) == MVT::f32) {
1085     return CurDAG->getMachineNode(Hexagon::TFRI_f, dl, MVT::f32,
1086               CurDAG->getTargetConstantFP(APF.convertToFloat(), MVT::f32));
1087   }
1088   else if (N->getValueType(0) == MVT::f64) {
1089     return CurDAG->getMachineNode(Hexagon::CONST64_Float_Real, dl, MVT::f64,
1090               CurDAG->getTargetConstantFP(APF.convertToDouble(), MVT::f64));
1091   }
1092
1093   return SelectCode(N);
1094 }
1095
1096
1097 //
1098 // Map predicate true (encoded as -1 in LLVM) to a XOR.
1099 //
1100 SDNode *HexagonDAGToDAGISel::SelectConstant(SDNode *N) {
1101   SDLoc dl(N);
1102   if (N->getValueType(0) == MVT::i1) {
1103     SDNode* Result;
1104     int32_t Val = cast<ConstantSDNode>(N)->getSExtValue();
1105     if (Val == -1) {
1106       // Create the IntReg = 1 node.
1107       SDNode* IntRegTFR =
1108         CurDAG->getMachineNode(Hexagon::A2_tfrsi, dl, MVT::i32,
1109                                CurDAG->getTargetConstant(0, MVT::i32));
1110
1111       // Pd = IntReg
1112       SDNode* Pd = CurDAG->getMachineNode(Hexagon::C2_tfrrp, dl, MVT::i1,
1113                                           SDValue(IntRegTFR, 0));
1114
1115       // not(Pd)
1116       SDNode* NotPd = CurDAG->getMachineNode(Hexagon::C2_not, dl, MVT::i1,
1117                                              SDValue(Pd, 0));
1118
1119       // xor(not(Pd))
1120       Result = CurDAG->getMachineNode(Hexagon::C2_xor, dl, MVT::i1,
1121                                       SDValue(Pd, 0), SDValue(NotPd, 0));
1122
1123       // We have just built:
1124       // Rs = Pd
1125       // Pd = xor(not(Pd), Pd)
1126
1127       ReplaceUses(N, Result);
1128       return Result;
1129     }
1130   }
1131
1132   return SelectCode(N);
1133 }
1134
1135
1136 //
1137 // Map add followed by a asr -> asr +=.
1138 //
1139 SDNode *HexagonDAGToDAGISel::SelectAdd(SDNode *N) {
1140   SDLoc dl(N);
1141   if (N->getValueType(0) != MVT::i32) {
1142     return SelectCode(N);
1143   }
1144   // Identify nodes of the form: add(asr(...)).
1145   SDNode* Src1 = N->getOperand(0).getNode();
1146   if (Src1->getOpcode() != ISD::SRA || !Src1->hasOneUse()
1147       || Src1->getValueType(0) != MVT::i32) {
1148     return SelectCode(N);
1149   }
1150
1151   // Build Rd = Rd' + asr(Rs, Rt). The machine constraints will ensure that
1152   // Rd and Rd' are assigned to the same register
1153   SDNode* Result = CurDAG->getMachineNode(Hexagon::S2_asr_r_r_acc, dl, MVT::i32,
1154                                           N->getOperand(1),
1155                                           Src1->getOperand(0),
1156                                           Src1->getOperand(1));
1157   ReplaceUses(N, Result);
1158
1159   return Result;
1160 }
1161
1162
1163 SDNode *HexagonDAGToDAGISel::Select(SDNode *N) {
1164   if (N->isMachineOpcode()) {
1165     N->setNodeId(-1);
1166     return nullptr;   // Already selected.
1167   }
1168
1169
1170   switch (N->getOpcode()) {
1171   case ISD::Constant:
1172     return SelectConstant(N);
1173
1174   case ISD::ConstantFP:
1175     return SelectConstantFP(N);
1176
1177   case ISD::ADD:
1178     return SelectAdd(N);
1179
1180   case ISD::SHL:
1181     return SelectSHL(N);
1182
1183   case ISD::LOAD:
1184     return SelectLoad(N);
1185
1186   case ISD::STORE:
1187     return SelectStore(N);
1188
1189   case ISD::SELECT:
1190     return SelectSelect(N);
1191
1192   case ISD::TRUNCATE:
1193     return SelectTruncate(N);
1194
1195   case ISD::MUL:
1196     return SelectMul(N);
1197
1198   case ISD::ZERO_EXTEND:
1199     return SelectZeroExtend(N);
1200
1201   case ISD::INTRINSIC_WO_CHAIN:
1202     return SelectIntrinsicWOChain(N);
1203   }
1204
1205   return SelectCode(N);
1206 }
1207
1208
1209 //
1210 // Hexagon_TODO: Five functions for ADDRri?! Surely there must be a better way
1211 // to define these instructions.
1212 //
1213 bool HexagonDAGToDAGISel::SelectADDRri(SDValue& Addr, SDValue &Base,
1214                                        SDValue &Offset) {
1215   if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
1216       Addr.getOpcode() == ISD::TargetGlobalAddress)
1217     return false;  // Direct calls.
1218
1219   if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
1220     Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
1221     Offset = CurDAG->getTargetConstant(0, MVT::i32);
1222     return true;
1223   }
1224   Base = Addr;
1225   Offset = CurDAG->getTargetConstant(0, MVT::i32);
1226   return true;
1227 }
1228
1229
1230 bool HexagonDAGToDAGISel::SelectADDRriS11_0(SDValue& Addr, SDValue &Base,
1231                                             SDValue &Offset) {
1232   if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
1233       Addr.getOpcode() == ISD::TargetGlobalAddress)
1234     return false;  // Direct calls.
1235
1236   if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
1237     Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
1238     Offset = CurDAG->getTargetConstant(0, MVT::i32);
1239     return (IsS11_0_Offset(Offset.getNode()));
1240   }
1241   Base = Addr;
1242   Offset = CurDAG->getTargetConstant(0, MVT::i32);
1243   return (IsS11_0_Offset(Offset.getNode()));
1244 }
1245
1246
1247 bool HexagonDAGToDAGISel::SelectADDRriS11_1(SDValue& Addr, SDValue &Base,
1248                                             SDValue &Offset) {
1249   if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
1250       Addr.getOpcode() == ISD::TargetGlobalAddress)
1251     return false;  // Direct calls.
1252
1253   if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
1254     Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
1255     Offset = CurDAG->getTargetConstant(0, MVT::i32);
1256     return (IsS11_1_Offset(Offset.getNode()));
1257   }
1258   Base = Addr;
1259   Offset = CurDAG->getTargetConstant(0, MVT::i32);
1260   return (IsS11_1_Offset(Offset.getNode()));
1261 }
1262
1263
1264 bool HexagonDAGToDAGISel::SelectADDRriS11_2(SDValue& Addr, SDValue &Base,
1265                                             SDValue &Offset) {
1266   if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
1267       Addr.getOpcode() == ISD::TargetGlobalAddress)
1268     return false;  // Direct calls.
1269
1270   if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
1271     Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
1272     Offset = CurDAG->getTargetConstant(0, MVT::i32);
1273     return (IsS11_2_Offset(Offset.getNode()));
1274   }
1275   Base = Addr;
1276   Offset = CurDAG->getTargetConstant(0, MVT::i32);
1277   return (IsS11_2_Offset(Offset.getNode()));
1278 }
1279
1280
1281 bool HexagonDAGToDAGISel::SelectADDRriU6_0(SDValue& Addr, SDValue &Base,
1282                                             SDValue &Offset) {
1283   if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
1284       Addr.getOpcode() == ISD::TargetGlobalAddress)
1285     return false;  // Direct calls.
1286
1287   if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
1288     Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
1289     Offset = CurDAG->getTargetConstant(0, MVT::i32);
1290     return (IsU6_0_Offset(Offset.getNode()));
1291   }
1292   Base = Addr;
1293   Offset = CurDAG->getTargetConstant(0, MVT::i32);
1294   return (IsU6_0_Offset(Offset.getNode()));
1295 }
1296
1297
1298 bool HexagonDAGToDAGISel::SelectADDRriU6_1(SDValue& Addr, SDValue &Base,
1299                                             SDValue &Offset) {
1300   if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
1301       Addr.getOpcode() == ISD::TargetGlobalAddress)
1302     return false;  // Direct calls.
1303
1304   if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
1305     Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
1306     Offset = CurDAG->getTargetConstant(0, MVT::i32);
1307     return (IsU6_1_Offset(Offset.getNode()));
1308   }
1309   Base = Addr;
1310   Offset = CurDAG->getTargetConstant(0, MVT::i32);
1311   return (IsU6_1_Offset(Offset.getNode()));
1312 }
1313
1314
1315 bool HexagonDAGToDAGISel::SelectADDRriU6_2(SDValue& Addr, SDValue &Base,
1316                                             SDValue &Offset) {
1317   if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
1318       Addr.getOpcode() == ISD::TargetGlobalAddress)
1319     return false;  // Direct calls.
1320
1321   if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
1322     Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
1323     Offset = CurDAG->getTargetConstant(0, MVT::i32);
1324     return (IsU6_2_Offset(Offset.getNode()));
1325   }
1326   Base = Addr;
1327   Offset = CurDAG->getTargetConstant(0, MVT::i32);
1328   return (IsU6_2_Offset(Offset.getNode()));
1329 }
1330
1331
1332 bool HexagonDAGToDAGISel::SelectMEMriS11_2(SDValue& Addr, SDValue &Base,
1333                                            SDValue &Offset) {
1334
1335   if (Addr.getOpcode() != ISD::ADD) {
1336     return(SelectADDRriS11_2(Addr, Base, Offset));
1337   }
1338
1339   return SelectADDRriS11_2(Addr, Base, Offset);
1340 }
1341
1342
1343 bool HexagonDAGToDAGISel::SelectADDRriS11_3(SDValue& Addr, SDValue &Base,
1344                                             SDValue &Offset) {
1345   if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
1346       Addr.getOpcode() == ISD::TargetGlobalAddress)
1347     return false;  // Direct calls.
1348
1349   if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
1350     Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
1351     Offset = CurDAG->getTargetConstant(0, MVT::i32);
1352     return (IsS11_3_Offset(Offset.getNode()));
1353   }
1354   Base = Addr;
1355   Offset = CurDAG->getTargetConstant(0, MVT::i32);
1356   return (IsS11_3_Offset(Offset.getNode()));
1357 }
1358
1359 bool HexagonDAGToDAGISel::SelectADDRrr(SDValue &Addr, SDValue &R1,
1360                                        SDValue &R2) {
1361   if (Addr.getOpcode() == ISD::FrameIndex) return false;
1362   if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
1363       Addr.getOpcode() == ISD::TargetGlobalAddress)
1364     return false;  // Direct calls.
1365
1366   if (Addr.getOpcode() == ISD::ADD) {
1367     if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
1368       if (isInt<13>(CN->getSExtValue()))
1369         return false;  // Let the reg+imm pattern catch this!
1370     R1 = Addr.getOperand(0);
1371     R2 = Addr.getOperand(1);
1372     return true;
1373   }
1374
1375   R1 = Addr;
1376
1377   return true;
1378 }
1379
1380
1381 // Handle generic address case. It is accessed from inlined asm =m constraints,
1382 // which could have any kind of pointer.
1383 bool HexagonDAGToDAGISel::SelectAddr(SDNode *Op, SDValue Addr,
1384                                           SDValue &Base, SDValue &Offset) {
1385   if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
1386       Addr.getOpcode() == ISD::TargetGlobalAddress)
1387     return false;  // Direct calls.
1388
1389   if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
1390     Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
1391     Offset = CurDAG->getTargetConstant(0, MVT::i32);
1392     return true;
1393   }
1394
1395   if (Addr.getOpcode() == ISD::ADD) {
1396     Base = Addr.getOperand(0);
1397     Offset = Addr.getOperand(1);
1398     return true;
1399   }
1400
1401   Base = Addr;
1402   Offset = CurDAG->getTargetConstant(0, MVT::i32);
1403   return true;
1404 }
1405
1406
1407 bool HexagonDAGToDAGISel::
1408 SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
1409                              std::vector<SDValue> &OutOps) {
1410   SDValue Op0, Op1;
1411
1412   switch (ConstraintCode) {
1413   case 'o':   // Offsetable.
1414   case 'v':   // Not offsetable.
1415   default: return true;
1416   case 'm':   // Memory.
1417     if (!SelectAddr(Op.getNode(), Op, Op0, Op1))
1418       return true;
1419     break;
1420   }
1421
1422   OutOps.push_back(Op0);
1423   OutOps.push_back(Op1);
1424   return false;
1425 }
1426
1427 //===--------------------------------------------------------------------===//
1428 // Return true if the non-GP-relative global address can be folded.
1429 //===--------------------------------------------------------------------===//
1430 inline bool HexagonDAGToDAGISel::foldGlobalAddress(SDValue &N, SDValue &R) {
1431   return foldGlobalAddressImpl(N, R, false);
1432 }
1433
1434 //===--------------------------------------------------------------------===//
1435 // Return true if the GP-relative global address can be folded.
1436 //===--------------------------------------------------------------------===//
1437 inline bool HexagonDAGToDAGISel::foldGlobalAddressGP(SDValue &N, SDValue &R) {
1438   return foldGlobalAddressImpl(N, R, true);
1439 }
1440
1441 //===--------------------------------------------------------------------===//
1442 // Fold offset of the global address if number of uses are below threshold.
1443 //===--------------------------------------------------------------------===//
1444 bool HexagonDAGToDAGISel::foldGlobalAddressImpl(SDValue &N, SDValue &R,
1445                                                 bool ShouldLookForGP) {
1446   if (N.getOpcode() == ISD::ADD) {
1447     SDValue N0 = N.getOperand(0);
1448     SDValue N1 = N.getOperand(1);
1449     if ((ShouldLookForGP && (N0.getOpcode() == HexagonISD::CONST32_GP)) ||
1450         (!ShouldLookForGP && (N0.getOpcode() == HexagonISD::CONST32))) {
1451       ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N1);
1452       GlobalAddressSDNode *GA =
1453         dyn_cast<GlobalAddressSDNode>(N0.getOperand(0));
1454
1455       if (Const && GA &&
1456           (GA->getOpcode() == ISD::TargetGlobalAddress)) {
1457         if (N0.getOpcode() == HexagonISD::CONST32)
1458           return false;
1459         R = CurDAG->getTargetGlobalAddress(GA->getGlobal(),
1460                                           SDLoc(Const),
1461                                           N.getValueType(),
1462                                           GA->getOffset() +
1463                                           (uint64_t)Const->getSExtValue());
1464         return true;
1465       }
1466     }
1467   }
1468   return false;
1469 }
1470
1471 bool HexagonDAGToDAGISel::SelectAddrFI(SDValue& N, SDValue &R) {
1472   if (N.getOpcode() != ISD::FrameIndex)
1473     return false;
1474   FrameIndexSDNode *FX = cast<FrameIndexSDNode>(N);
1475   R = CurDAG->getTargetFrameIndex(FX->getIndex(), MVT::i32);
1476   return true;
1477 }
1478
1479 inline bool HexagonDAGToDAGISel::SelectAddrGA(SDValue &N, SDValue &R) {
1480   return SelectGlobalAddress(N, R, false);
1481 }
1482
1483 inline bool HexagonDAGToDAGISel::SelectAddrGP(SDValue &N, SDValue &R) {
1484   return SelectGlobalAddress(N, R, true);
1485 }
1486
1487 bool HexagonDAGToDAGISel::SelectGlobalAddress(SDValue &N, SDValue &R,
1488                                               bool UseGP) {
1489   switch (N.getOpcode()) {
1490   case ISD::ADD: {
1491     SDValue N0 = N.getOperand(0);
1492     SDValue N1 = N.getOperand(1);
1493     unsigned GAOpc = N0.getOpcode();
1494     if (UseGP && GAOpc != HexagonISD::CONST32_GP)
1495       return false;
1496     if (!UseGP && GAOpc != HexagonISD::CONST32)
1497       return false;
1498     if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N1)) {
1499       SDValue Addr = N0.getOperand(0);
1500       if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Addr)) {
1501         if (GA->getOpcode() == ISD::TargetGlobalAddress) {
1502           uint64_t NewOff = GA->getOffset() + (uint64_t)Const->getSExtValue();
1503           R = CurDAG->getTargetGlobalAddress(GA->getGlobal(), SDLoc(Const),
1504                                              N.getValueType(), NewOff);
1505           return true;
1506         }
1507       }
1508     }
1509     break;
1510   }
1511   case HexagonISD::CONST32:
1512     // The operand(0) of CONST32 is TargetGlobalAddress, which is what we
1513     // want in the instruction.
1514     if (!UseGP)
1515       R = N.getOperand(0);
1516     return !UseGP;
1517   case HexagonISD::CONST32_GP:
1518     if (UseGP)
1519       R = N.getOperand(0);
1520     return UseGP;
1521   default:
1522     return false;
1523   }
1524
1525   return false;
1526 }
1527
1528 bool HexagonDAGToDAGISel::isValueExtension(SDValue const &Val,
1529                                            unsigned FromBits, SDValue &Src) {
1530   unsigned Opc = Val.getOpcode();
1531   switch (Opc) {
1532   case ISD::SIGN_EXTEND:
1533   case ISD::ZERO_EXTEND:
1534   case ISD::ANY_EXTEND: {
1535     SDValue const &Op0 = Val.getOperand(0);
1536     EVT T = Op0.getValueType();
1537     if (T.isInteger() && T.getSizeInBits() == FromBits) {
1538       Src = Op0;
1539       return true;
1540     }
1541     break;
1542   }
1543   case ISD::SIGN_EXTEND_INREG:
1544   case ISD::AssertSext:
1545   case ISD::AssertZext:
1546     if (Val.getOperand(0).getValueType().isInteger()) {
1547       VTSDNode *T = cast<VTSDNode>(Val.getOperand(1));
1548       if (T->getVT().getSizeInBits() == FromBits) {
1549         Src = Val.getOperand(0);
1550         return true;
1551       }
1552     }
1553     break;
1554   case ISD::AND: {
1555     // Check if this is an AND with "FromBits" of lower bits set to 1.
1556     uint64_t FromMask = (1 << FromBits) - 1;
1557     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(0))) {
1558       if (C->getZExtValue() == FromMask) {
1559         Src = Val.getOperand(1);
1560         return true;
1561       }
1562     }
1563     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(1))) {
1564       if (C->getZExtValue() == FromMask) {
1565         Src = Val.getOperand(0);
1566         return true;
1567       }
1568     }
1569     break;
1570   }
1571   case ISD::OR:
1572   case ISD::XOR: {
1573     // OR/XOR with the lower "FromBits" bits set to 0.
1574     uint64_t FromMask = (1 << FromBits) - 1;
1575     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(0))) {
1576       if ((C->getZExtValue() & FromMask) == 0) {
1577         Src = Val.getOperand(1);
1578         return true;
1579       }
1580     }
1581     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(1))) {
1582       if ((C->getZExtValue() & FromMask) == 0) {
1583         Src = Val.getOperand(0);
1584         return true;
1585       }
1586     }
1587   }
1588   default:
1589     break;
1590   }
1591   return false;
1592 }