]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/Target/Hexagon/HexagonISelDAGToDAG.cpp
Update compiler-rt to release_39 branch r288513. Since this contains a
[FreeBSD/FreeBSD.git] / contrib / llvm / 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 "HexagonMachineFunctionInfo.h"
17 #include "HexagonTargetMachine.h"
18 #include "llvm/CodeGen/FunctionLoweringInfo.h"
19 #include "llvm/CodeGen/MachineInstrBuilder.h"
20 #include "llvm/CodeGen/SelectionDAGISel.h"
21 #include "llvm/IR/Intrinsics.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/Debug.h"
24 using namespace llvm;
25
26 #define DEBUG_TYPE "hexagon-isel"
27
28 static
29 cl::opt<unsigned>
30 MaxNumOfUsesForConstExtenders("ga-max-num-uses-for-constant-extenders",
31   cl::Hidden, cl::init(2),
32   cl::desc("Maximum number of uses of a global address such that we still us a"
33            "constant extended instruction"));
34
35 //===----------------------------------------------------------------------===//
36 // Instruction Selector Implementation
37 //===----------------------------------------------------------------------===//
38
39 //===--------------------------------------------------------------------===//
40 /// HexagonDAGToDAGISel - Hexagon specific code to select Hexagon machine
41 /// instructions for SelectionDAG operations.
42 ///
43 namespace {
44 class HexagonDAGToDAGISel : public SelectionDAGISel {
45   const HexagonTargetMachine &HTM;
46   const HexagonSubtarget *HST;
47   const HexagonInstrInfo *HII;
48   const HexagonRegisterInfo *HRI;
49 public:
50   explicit HexagonDAGToDAGISel(HexagonTargetMachine &tm,
51                                CodeGenOpt::Level OptLevel)
52       : SelectionDAGISel(tm, OptLevel), HTM(tm), HST(nullptr), HII(nullptr),
53         HRI(nullptr) {}
54
55   bool runOnMachineFunction(MachineFunction &MF) override {
56     // Reset the subtarget each time through.
57     HST = &MF.getSubtarget<HexagonSubtarget>();
58     HII = HST->getInstrInfo();
59     HRI = HST->getRegisterInfo();
60     SelectionDAGISel::runOnMachineFunction(MF);
61     return true;
62   }
63
64   virtual void PreprocessISelDAG() override;
65   virtual void EmitFunctionEntryCode() override;
66
67   void Select(SDNode *N) override;
68
69   // Complex Pattern Selectors.
70   inline bool SelectAddrGA(SDValue &N, SDValue &R);
71   inline bool SelectAddrGP(SDValue &N, SDValue &R);
72   bool SelectGlobalAddress(SDValue &N, SDValue &R, bool UseGP);
73   bool SelectAddrFI(SDValue &N, SDValue &R);
74
75   const char *getPassName() const override {
76     return "Hexagon DAG->DAG Pattern Instruction Selection";
77   }
78
79   // Generate a machine instruction node corresponding to the circ/brev
80   // load intrinsic.
81   MachineSDNode *LoadInstrForLoadIntrinsic(SDNode *IntN);
82   // Given the circ/brev load intrinsic and the already generated machine
83   // instruction, generate the appropriate store (that is a part of the
84   // intrinsic's functionality).
85   SDNode *StoreInstrForLoadIntrinsic(MachineSDNode *LoadN, SDNode *IntN);
86
87   void SelectFrameIndex(SDNode *N);
88   /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
89   /// inline asm expressions.
90   bool SelectInlineAsmMemoryOperand(const SDValue &Op,
91                                     unsigned ConstraintID,
92                                     std::vector<SDValue> &OutOps) override;
93   bool tryLoadOfLoadIntrinsic(LoadSDNode *N);
94   void SelectLoad(SDNode *N);
95   void SelectBaseOffsetLoad(LoadSDNode *LD, SDLoc dl);
96   void SelectIndexedLoad(LoadSDNode *LD, const SDLoc &dl);
97   void SelectIndexedStore(StoreSDNode *ST, const SDLoc &dl);
98   void SelectStore(SDNode *N);
99   void SelectSHL(SDNode *N);
100   void SelectMul(SDNode *N);
101   void SelectZeroExtend(SDNode *N);
102   void SelectIntrinsicWChain(SDNode *N);
103   void SelectIntrinsicWOChain(SDNode *N);
104   void SelectConstant(SDNode *N);
105   void SelectConstantFP(SDNode *N);
106   void SelectAdd(SDNode *N);
107   void SelectBitcast(SDNode *N);
108   void SelectBitOp(SDNode *N);
109
110   // XformMskToBitPosU5Imm - Returns the bit position which
111   // the single bit 32 bit mask represents.
112   // Used in Clr and Set bit immediate memops.
113   SDValue XformMskToBitPosU5Imm(uint32_t Imm, const SDLoc &DL) {
114     int32_t bitPos;
115     bitPos = Log2_32(Imm);
116     assert(bitPos >= 0 && bitPos < 32 &&
117            "Constant out of range for 32 BitPos Memops");
118     return CurDAG->getTargetConstant(bitPos, DL, MVT::i32);
119   }
120
121   // XformMskToBitPosU4Imm - Returns the bit position which the single-bit
122   // 16 bit mask represents. Used in Clr and Set bit immediate memops.
123   SDValue XformMskToBitPosU4Imm(uint16_t Imm, const SDLoc &DL) {
124     return XformMskToBitPosU5Imm(Imm, DL);
125   }
126
127   // XformMskToBitPosU3Imm - Returns the bit position which the single-bit
128   // 8 bit mask represents. Used in Clr and Set bit immediate memops.
129   SDValue XformMskToBitPosU3Imm(uint8_t Imm, const SDLoc &DL) {
130     return XformMskToBitPosU5Imm(Imm, DL);
131   }
132
133   // Return true if there is exactly one bit set in V, i.e., if V is one of the
134   // following integers: 2^0, 2^1, ..., 2^31.
135   bool ImmIsSingleBit(uint32_t v) const {
136     return isPowerOf2_32(v);
137   }
138
139   // XformM5ToU5Imm - Return a target constant with the specified value, of
140   // type i32 where the negative literal is transformed into a positive literal
141   // for use in -= memops.
142   inline SDValue XformM5ToU5Imm(signed Imm, const SDLoc &DL) {
143     assert((Imm >= -31 && Imm <= -1) && "Constant out of range for Memops");
144     return CurDAG->getTargetConstant(-Imm, DL, MVT::i32);
145   }
146
147   // XformU7ToU7M1Imm - Return a target constant decremented by 1, in range
148   // [1..128], used in cmpb.gtu instructions.
149   inline SDValue XformU7ToU7M1Imm(signed Imm, const SDLoc &DL) {
150     assert((Imm >= 1 && Imm <= 128) && "Constant out of range for cmpb op");
151     return CurDAG->getTargetConstant(Imm - 1, DL, MVT::i8);
152   }
153
154   // XformS8ToS8M1Imm - Return a target constant decremented by 1.
155   inline SDValue XformSToSM1Imm(signed Imm, const SDLoc &DL) {
156     return CurDAG->getTargetConstant(Imm - 1, DL, MVT::i32);
157   }
158
159   // XformU8ToU8M1Imm - Return a target constant decremented by 1.
160   inline SDValue XformUToUM1Imm(unsigned Imm, const SDLoc &DL) {
161     assert((Imm >= 1) && "Cannot decrement unsigned int less than 1");
162     return CurDAG->getTargetConstant(Imm - 1, DL, MVT::i32);
163   }
164
165   // XformSToSM2Imm - Return a target constant decremented by 2.
166   inline SDValue XformSToSM2Imm(unsigned Imm, const SDLoc &DL) {
167     return CurDAG->getTargetConstant(Imm - 2, DL, MVT::i32);
168   }
169
170   // XformSToSM3Imm - Return a target constant decremented by 3.
171   inline SDValue XformSToSM3Imm(unsigned Imm, const SDLoc &DL) {
172     return CurDAG->getTargetConstant(Imm - 3, DL, MVT::i32);
173   }
174
175   // Include the pieces autogenerated from the target description.
176   #include "HexagonGenDAGISel.inc"
177
178 private:
179   bool isValueExtension(const SDValue &Val, unsigned FromBits, SDValue &Src);
180   bool orIsAdd(const SDNode *N) const;
181   bool isAlignedMemNode(const MemSDNode *N) const;
182 }; // end HexagonDAGToDAGISel
183 }  // end anonymous namespace
184
185
186 /// createHexagonISelDag - This pass converts a legalized DAG into a
187 /// Hexagon-specific DAG, ready for instruction scheduling.
188 ///
189 namespace llvm {
190 FunctionPass *createHexagonISelDag(HexagonTargetMachine &TM,
191                                    CodeGenOpt::Level OptLevel) {
192   return new HexagonDAGToDAGISel(TM, OptLevel);
193 }
194 }
195
196 // Intrinsics that return a a predicate.
197 static bool doesIntrinsicReturnPredicate(unsigned ID) {
198   switch (ID) {
199     default:
200       return false;
201     case Intrinsic::hexagon_C2_cmpeq:
202     case Intrinsic::hexagon_C2_cmpgt:
203     case Intrinsic::hexagon_C2_cmpgtu:
204     case Intrinsic::hexagon_C2_cmpgtup:
205     case Intrinsic::hexagon_C2_cmpgtp:
206     case Intrinsic::hexagon_C2_cmpeqp:
207     case Intrinsic::hexagon_C2_bitsset:
208     case Intrinsic::hexagon_C2_bitsclr:
209     case Intrinsic::hexagon_C2_cmpeqi:
210     case Intrinsic::hexagon_C2_cmpgti:
211     case Intrinsic::hexagon_C2_cmpgtui:
212     case Intrinsic::hexagon_C2_cmpgei:
213     case Intrinsic::hexagon_C2_cmpgeui:
214     case Intrinsic::hexagon_C2_cmplt:
215     case Intrinsic::hexagon_C2_cmpltu:
216     case Intrinsic::hexagon_C2_bitsclri:
217     case Intrinsic::hexagon_C2_and:
218     case Intrinsic::hexagon_C2_or:
219     case Intrinsic::hexagon_C2_xor:
220     case Intrinsic::hexagon_C2_andn:
221     case Intrinsic::hexagon_C2_not:
222     case Intrinsic::hexagon_C2_orn:
223     case Intrinsic::hexagon_C2_pxfer_map:
224     case Intrinsic::hexagon_C2_any8:
225     case Intrinsic::hexagon_C2_all8:
226     case Intrinsic::hexagon_A2_vcmpbeq:
227     case Intrinsic::hexagon_A2_vcmpbgtu:
228     case Intrinsic::hexagon_A2_vcmpheq:
229     case Intrinsic::hexagon_A2_vcmphgt:
230     case Intrinsic::hexagon_A2_vcmphgtu:
231     case Intrinsic::hexagon_A2_vcmpweq:
232     case Intrinsic::hexagon_A2_vcmpwgt:
233     case Intrinsic::hexagon_A2_vcmpwgtu:
234     case Intrinsic::hexagon_C2_tfrrp:
235     case Intrinsic::hexagon_S2_tstbit_i:
236     case Intrinsic::hexagon_S2_tstbit_r:
237       return true;
238   }
239 }
240
241 void HexagonDAGToDAGISel::SelectIndexedLoad(LoadSDNode *LD, const SDLoc &dl) {
242   SDValue Chain = LD->getChain();
243   SDValue Base = LD->getBasePtr();
244   SDValue Offset = LD->getOffset();
245   int32_t Inc = cast<ConstantSDNode>(Offset.getNode())->getSExtValue();
246   EVT LoadedVT = LD->getMemoryVT();
247   unsigned Opcode = 0;
248
249   // Check for zero extended loads. Treat any-extend loads as zero extended
250   // loads.
251   ISD::LoadExtType ExtType = LD->getExtensionType();
252   bool IsZeroExt = (ExtType == ISD::ZEXTLOAD || ExtType == ISD::EXTLOAD);
253   bool IsValidInc = HII->isValidAutoIncImm(LoadedVT, Inc);
254
255   assert(LoadedVT.isSimple());
256   switch (LoadedVT.getSimpleVT().SimpleTy) {
257   case MVT::i8:
258     if (IsZeroExt)
259       Opcode = IsValidInc ? Hexagon::L2_loadrub_pi : Hexagon::L2_loadrub_io;
260     else
261       Opcode = IsValidInc ? Hexagon::L2_loadrb_pi : Hexagon::L2_loadrb_io;
262     break;
263   case MVT::i16:
264     if (IsZeroExt)
265       Opcode = IsValidInc ? Hexagon::L2_loadruh_pi : Hexagon::L2_loadruh_io;
266     else
267       Opcode = IsValidInc ? Hexagon::L2_loadrh_pi : Hexagon::L2_loadrh_io;
268     break;
269   case MVT::i32:
270     Opcode = IsValidInc ? Hexagon::L2_loadri_pi : Hexagon::L2_loadri_io;
271     break;
272   case MVT::i64:
273     Opcode = IsValidInc ? Hexagon::L2_loadrd_pi : Hexagon::L2_loadrd_io;
274     break;
275   // 64B
276   case MVT::v64i8:
277   case MVT::v32i16:
278   case MVT::v16i32:
279   case MVT::v8i64:
280     if (isAlignedMemNode(LD))
281       Opcode = IsValidInc ? Hexagon::V6_vL32b_pi : Hexagon::V6_vL32b_ai;
282     else
283       Opcode = IsValidInc ? Hexagon::V6_vL32Ub_pi : Hexagon::V6_vL32Ub_ai;
284     break;
285   // 128B
286   case MVT::v128i8:
287   case MVT::v64i16:
288   case MVT::v32i32:
289   case MVT::v16i64:
290     if (isAlignedMemNode(LD))
291       Opcode = IsValidInc ? Hexagon::V6_vL32b_pi_128B
292                           : Hexagon::V6_vL32b_ai_128B;
293     else
294       Opcode = IsValidInc ? Hexagon::V6_vL32Ub_pi_128B
295                           : Hexagon::V6_vL32Ub_ai_128B;
296     break;
297   default:
298     llvm_unreachable("Unexpected memory type in indexed load");
299   }
300
301   SDValue IncV = CurDAG->getTargetConstant(Inc, dl, MVT::i32);
302   MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
303   MemOp[0] = LD->getMemOperand();
304
305   auto getExt64 = [this,ExtType] (MachineSDNode *N, const SDLoc &dl)
306         -> MachineSDNode* {
307     if (ExtType == ISD::ZEXTLOAD || ExtType == ISD::EXTLOAD) {
308       SDValue Zero = CurDAG->getTargetConstant(0, dl, MVT::i32);
309       return CurDAG->getMachineNode(Hexagon::A4_combineir, dl, MVT::i64,
310                                     Zero, SDValue(N, 0));
311     }
312     if (ExtType == ISD::SEXTLOAD)
313       return CurDAG->getMachineNode(Hexagon::A2_sxtw, dl, MVT::i64,
314                                     SDValue(N, 0));
315     return N;
316   };
317
318   //                  Loaded value   Next address   Chain
319   SDValue From[3] = { SDValue(LD,0), SDValue(LD,1), SDValue(LD,2) };
320   SDValue To[3];
321
322   EVT ValueVT = LD->getValueType(0);
323   if (ValueVT == MVT::i64 && ExtType != ISD::NON_EXTLOAD) {
324     // A load extending to i64 will actually produce i32, which will then
325     // need to be extended to i64.
326     assert(LoadedVT.getSizeInBits() <= 32);
327     ValueVT = MVT::i32;
328   }
329
330   if (IsValidInc) {
331     MachineSDNode *L = CurDAG->getMachineNode(Opcode, dl, ValueVT,
332                                               MVT::i32, MVT::Other, Base,
333                                               IncV, Chain);
334     L->setMemRefs(MemOp, MemOp+1);
335     To[1] = SDValue(L, 1); // Next address.
336     To[2] = SDValue(L, 2); // Chain.
337     // Handle special case for extension to i64.
338     if (LD->getValueType(0) == MVT::i64)
339       L = getExt64(L, dl);
340     To[0] = SDValue(L, 0); // Loaded (extended) value.
341   } else {
342     SDValue Zero = CurDAG->getTargetConstant(0, dl, MVT::i32);
343     MachineSDNode *L = CurDAG->getMachineNode(Opcode, dl, ValueVT, MVT::Other,
344                                               Base, Zero, Chain);
345     L->setMemRefs(MemOp, MemOp+1);
346     To[2] = SDValue(L, 1); // Chain.
347     MachineSDNode *A = CurDAG->getMachineNode(Hexagon::A2_addi, dl, MVT::i32,
348                                               Base, IncV);
349     To[1] = SDValue(A, 0); // Next address.
350     // Handle special case for extension to i64.
351     if (LD->getValueType(0) == MVT::i64)
352       L = getExt64(L, dl);
353     To[0] = SDValue(L, 0); // Loaded (extended) value.
354   }
355   ReplaceUses(From, To, 3);
356   CurDAG->RemoveDeadNode(LD);
357 }
358
359
360 MachineSDNode *HexagonDAGToDAGISel::LoadInstrForLoadIntrinsic(SDNode *IntN) {
361   if (IntN->getOpcode() != ISD::INTRINSIC_W_CHAIN)
362     return nullptr;
363
364   SDLoc dl(IntN);
365   unsigned IntNo = cast<ConstantSDNode>(IntN->getOperand(1))->getZExtValue();
366
367   static std::map<unsigned,unsigned> LoadPciMap = {
368     { Intrinsic::hexagon_circ_ldb,  Hexagon::L2_loadrb_pci  },
369     { Intrinsic::hexagon_circ_ldub, Hexagon::L2_loadrub_pci },
370     { Intrinsic::hexagon_circ_ldh,  Hexagon::L2_loadrh_pci  },
371     { Intrinsic::hexagon_circ_lduh, Hexagon::L2_loadruh_pci },
372     { Intrinsic::hexagon_circ_ldw,  Hexagon::L2_loadri_pci  },
373     { Intrinsic::hexagon_circ_ldd,  Hexagon::L2_loadrd_pci  },
374   };
375   auto FLC = LoadPciMap.find(IntNo);
376   if (FLC != LoadPciMap.end()) {
377     SDNode *Mod = CurDAG->getMachineNode(Hexagon::A2_tfrrcr, dl, MVT::i32,
378           IntN->getOperand(4));
379     EVT ValTy = (IntNo == Intrinsic::hexagon_circ_ldd) ? MVT::i64 : MVT::i32;
380     EVT RTys[] = { ValTy, MVT::i32, MVT::Other };
381     // Operands: { Base, Increment, Modifier, Chain }
382     auto Inc = cast<ConstantSDNode>(IntN->getOperand(5));
383     SDValue I = CurDAG->getTargetConstant(Inc->getSExtValue(), dl, MVT::i32);
384     MachineSDNode *Res = CurDAG->getMachineNode(FLC->second, dl, RTys,
385           { IntN->getOperand(2), I, SDValue(Mod,0), IntN->getOperand(0) });
386     return Res;
387   }
388
389   static std::map<unsigned,unsigned> LoadPbrMap = {
390     { Intrinsic::hexagon_brev_ldb,  Hexagon::L2_loadrb_pbr  },
391     { Intrinsic::hexagon_brev_ldub, Hexagon::L2_loadrub_pbr },
392     { Intrinsic::hexagon_brev_ldh,  Hexagon::L2_loadrh_pbr  },
393     { Intrinsic::hexagon_brev_lduh, Hexagon::L2_loadruh_pbr },
394     { Intrinsic::hexagon_brev_ldw,  Hexagon::L2_loadri_pbr  },
395     { Intrinsic::hexagon_brev_ldd,  Hexagon::L2_loadrd_pbr  },
396   };
397   auto FLB = LoadPbrMap.find(IntNo);
398   if (FLB != LoadPbrMap.end()) {
399     SDNode *Mod = CurDAG->getMachineNode(Hexagon::A2_tfrrcr, dl, MVT::i32,
400             IntN->getOperand(4));
401     EVT ValTy = (IntNo == Intrinsic::hexagon_brev_ldd) ? MVT::i64 : MVT::i32;
402     EVT RTys[] = { ValTy, MVT::i32, MVT::Other };
403     // Operands: { Base, Modifier, Chain }
404     MachineSDNode *Res = CurDAG->getMachineNode(FLB->second, dl, RTys,
405           { IntN->getOperand(2), SDValue(Mod,0), IntN->getOperand(0) });
406     return Res;
407   }
408
409   return nullptr;
410 }
411
412 SDNode *HexagonDAGToDAGISel::StoreInstrForLoadIntrinsic(MachineSDNode *LoadN,
413       SDNode *IntN) {
414   // The "LoadN" is just a machine load instruction. The intrinsic also
415   // involves storing it. Generate an appropriate store to the location
416   // given in the intrinsic's operand(3).
417   uint64_t F = HII->get(LoadN->getMachineOpcode()).TSFlags;
418   unsigned SizeBits = (F >> HexagonII::MemAccessSizePos) &
419                       HexagonII::MemAccesSizeMask;
420   unsigned Size = 1U << (SizeBits-1);
421
422   SDLoc dl(IntN);
423   MachinePointerInfo PI;
424   SDValue TS;
425   SDValue Loc = IntN->getOperand(3);
426
427   if (Size >= 4)
428     TS = CurDAG->getStore(SDValue(LoadN, 2), dl, SDValue(LoadN, 0), Loc, PI,
429                           Size);
430   else
431     TS = CurDAG->getTruncStore(SDValue(LoadN, 2), dl, SDValue(LoadN, 0), Loc,
432                                PI, MVT::getIntegerVT(Size * 8), Size);
433
434   SDNode *StoreN;
435   {
436     HandleSDNode Handle(TS);
437     SelectStore(TS.getNode());
438     StoreN = Handle.getValue().getNode();
439   }
440
441   // Load's results are { Loaded value, Updated pointer, Chain }
442   ReplaceUses(SDValue(IntN, 0), SDValue(LoadN, 1));
443   ReplaceUses(SDValue(IntN, 1), SDValue(StoreN, 0));
444   return StoreN;
445 }
446
447 bool HexagonDAGToDAGISel::tryLoadOfLoadIntrinsic(LoadSDNode *N) {
448   // The intrinsics for load circ/brev perform two operations:
449   // 1. Load a value V from the specified location, using the addressing
450   //    mode corresponding to the intrinsic.
451   // 2. Store V into a specified location. This location is typically a
452   //    local, temporary object.
453   // In many cases, the program using these intrinsics will immediately
454   // load V again from the local object. In those cases, when certain
455   // conditions are met, the last load can be removed.
456   // This function identifies and optimizes this pattern. If the pattern
457   // cannot be optimized, it returns nullptr, which will cause the load
458   // to be selected separately from the intrinsic (which will be handled
459   // in SelectIntrinsicWChain).
460
461   SDValue Ch = N->getOperand(0);
462   SDValue Loc = N->getOperand(1);
463
464   // Assume that the load and the intrinsic are connected directly with a
465   // chain:
466   //   t1: i32,ch = int.load ..., ..., ..., Loc, ...    // <-- C
467   //   t2: i32,ch = load t1:1, Loc, ...
468   SDNode *C = Ch.getNode();
469
470   if (C->getOpcode() != ISD::INTRINSIC_W_CHAIN)
471     return false;
472
473   // The second load can only be eliminated if its extension type matches
474   // that of the load instruction corresponding to the intrinsic. The user
475   // can provide an address of an unsigned variable to store the result of
476   // a sign-extending intrinsic into (or the other way around).
477   ISD::LoadExtType IntExt;
478   switch (cast<ConstantSDNode>(C->getOperand(1))->getZExtValue()) {
479     case Intrinsic::hexagon_brev_ldub:
480     case Intrinsic::hexagon_brev_lduh:
481     case Intrinsic::hexagon_circ_ldub:
482     case Intrinsic::hexagon_circ_lduh:
483       IntExt = ISD::ZEXTLOAD;
484       break;
485     case Intrinsic::hexagon_brev_ldw:
486     case Intrinsic::hexagon_brev_ldd:
487     case Intrinsic::hexagon_circ_ldw:
488     case Intrinsic::hexagon_circ_ldd:
489       IntExt = ISD::NON_EXTLOAD;
490       break;
491     default:
492       IntExt = ISD::SEXTLOAD;
493       break;
494   }
495   if (N->getExtensionType() != IntExt)
496     return false;
497
498   // Make sure the target location for the loaded value in the load intrinsic
499   // is the location from which LD (or N) is loading.
500   if (C->getNumOperands() < 4 || Loc.getNode() != C->getOperand(3).getNode())
501     return false;
502
503   if (MachineSDNode *L = LoadInstrForLoadIntrinsic(C)) {
504     SDNode *S = StoreInstrForLoadIntrinsic(L, C);
505     SDValue F[] = { SDValue(N,0), SDValue(N,1), SDValue(C,0), SDValue(C,1) };
506     SDValue T[] = { SDValue(L,0), SDValue(S,0), SDValue(L,1), SDValue(S,0) };
507     ReplaceUses(F, T, array_lengthof(T));
508     // This transformation will leave the intrinsic dead. If it remains in
509     // the DAG, the selection code will see it again, but without the load,
510     // and it will generate a store that is normally required for it.
511     CurDAG->RemoveDeadNode(C);
512     return true;
513   }
514
515   return false;
516 }
517
518 void HexagonDAGToDAGISel::SelectLoad(SDNode *N) {
519   SDLoc dl(N);
520   LoadSDNode *LD = cast<LoadSDNode>(N);
521   ISD::MemIndexedMode AM = LD->getAddressingMode();
522
523   // Handle indexed loads.
524   if (AM != ISD::UNINDEXED) {
525     SelectIndexedLoad(LD, dl);
526     return;
527   }
528
529   // Handle patterns using circ/brev load intrinsics.
530   if (tryLoadOfLoadIntrinsic(LD))
531     return;
532
533   SelectCode(LD);
534 }
535
536 void HexagonDAGToDAGISel::SelectIndexedStore(StoreSDNode *ST, const SDLoc &dl) {
537   SDValue Chain = ST->getChain();
538   SDValue Base = ST->getBasePtr();
539   SDValue Offset = ST->getOffset();
540   SDValue Value = ST->getValue();
541   // Get the constant value.
542   int32_t Inc = cast<ConstantSDNode>(Offset.getNode())->getSExtValue();
543   EVT StoredVT = ST->getMemoryVT();
544   EVT ValueVT = Value.getValueType();
545
546   bool IsValidInc = HII->isValidAutoIncImm(StoredVT, Inc);
547   unsigned Opcode = 0;
548
549   assert(StoredVT.isSimple());
550   switch (StoredVT.getSimpleVT().SimpleTy) {
551   case MVT::i8:
552     Opcode = IsValidInc ? Hexagon::S2_storerb_pi : Hexagon::S2_storerb_io;
553     break;
554   case MVT::i16:
555     Opcode = IsValidInc ? Hexagon::S2_storerh_pi : Hexagon::S2_storerh_io;
556     break;
557   case MVT::i32:
558     Opcode = IsValidInc ? Hexagon::S2_storeri_pi : Hexagon::S2_storeri_io;
559     break;
560   case MVT::i64:
561     Opcode = IsValidInc ? Hexagon::S2_storerd_pi : Hexagon::S2_storerd_io;
562     break;
563   // 64B
564   case MVT::v64i8:
565   case MVT::v32i16:
566   case MVT::v16i32:
567   case MVT::v8i64:
568     if (isAlignedMemNode(ST))
569       Opcode = IsValidInc ? Hexagon::V6_vS32b_pi : Hexagon::V6_vS32b_ai;
570     else
571       Opcode = IsValidInc ? Hexagon::V6_vS32Ub_pi : Hexagon::V6_vS32Ub_ai;
572     break;
573   // 128B
574   case MVT::v128i8:
575   case MVT::v64i16:
576   case MVT::v32i32:
577   case MVT::v16i64:
578     if (isAlignedMemNode(ST))
579       Opcode = IsValidInc ? Hexagon::V6_vS32b_pi_128B
580                           : Hexagon::V6_vS32b_ai_128B;
581     else
582       Opcode = IsValidInc ? Hexagon::V6_vS32Ub_pi_128B
583                           : Hexagon::V6_vS32Ub_ai_128B;
584     break;
585   default:
586     llvm_unreachable("Unexpected memory type in indexed store");
587   }
588
589   if (ST->isTruncatingStore() && ValueVT.getSizeInBits() == 64) {
590     assert(StoredVT.getSizeInBits() < 64 && "Not a truncating store");
591     Value = CurDAG->getTargetExtractSubreg(Hexagon::subreg_loreg,
592                                            dl, MVT::i32, Value);
593   }
594
595   SDValue IncV = CurDAG->getTargetConstant(Inc, dl, MVT::i32);
596   MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
597   MemOp[0] = ST->getMemOperand();
598
599   //                  Next address   Chain
600   SDValue From[2] = { SDValue(ST,0), SDValue(ST,1) };
601   SDValue To[2];
602
603   if (IsValidInc) {
604     // Build post increment store.
605     SDValue Ops[] = { Base, IncV, Value, Chain };
606     MachineSDNode *S = CurDAG->getMachineNode(Opcode, dl, MVT::i32, MVT::Other,
607                                               Ops);
608     S->setMemRefs(MemOp, MemOp + 1);
609     To[0] = SDValue(S, 0);
610     To[1] = SDValue(S, 1);
611   } else {
612     SDValue Zero = CurDAG->getTargetConstant(0, dl, MVT::i32);
613     SDValue Ops[] = { Base, Zero, Value, Chain };
614     MachineSDNode *S = CurDAG->getMachineNode(Opcode, dl, MVT::Other, Ops);
615     S->setMemRefs(MemOp, MemOp + 1);
616     To[1] = SDValue(S, 0);
617     MachineSDNode *A = CurDAG->getMachineNode(Hexagon::A2_addi, dl, MVT::i32,
618                                               Base, IncV);
619     To[0] = SDValue(A, 0);
620   }
621
622   ReplaceUses(From, To, 2);
623   CurDAG->RemoveDeadNode(ST);
624 }
625
626 void HexagonDAGToDAGISel::SelectStore(SDNode *N) {
627   SDLoc dl(N);
628   StoreSDNode *ST = cast<StoreSDNode>(N);
629   ISD::MemIndexedMode AM = ST->getAddressingMode();
630
631   // Handle indexed stores.
632   if (AM != ISD::UNINDEXED) {
633     SelectIndexedStore(ST, dl);
634     return;
635   }
636
637   SelectCode(ST);
638 }
639
640 void HexagonDAGToDAGISel::SelectMul(SDNode *N) {
641   SDLoc dl(N);
642
643   //
644   // %conv.i = sext i32 %tmp1 to i64
645   // %conv2.i = sext i32 %add to i64
646   // %mul.i = mul nsw i64 %conv2.i, %conv.i
647   //
648   //   --- match with the following ---
649   //
650   // %mul.i = mpy (%tmp1, %add)
651   //
652
653   if (N->getValueType(0) == MVT::i64) {
654     // Shifting a i64 signed multiply.
655     SDValue MulOp0 = N->getOperand(0);
656     SDValue MulOp1 = N->getOperand(1);
657
658     SDValue OP0;
659     SDValue OP1;
660
661     // Handle sign_extend and sextload.
662     if (MulOp0.getOpcode() == ISD::SIGN_EXTEND) {
663       SDValue Sext0 = MulOp0.getOperand(0);
664       if (Sext0.getNode()->getValueType(0) != MVT::i32) {
665         SelectCode(N);
666         return;
667       }
668
669       OP0 = Sext0;
670     } else if (MulOp0.getOpcode() == ISD::LOAD) {
671       LoadSDNode *LD = cast<LoadSDNode>(MulOp0.getNode());
672       if (LD->getMemoryVT() != MVT::i32 ||
673           LD->getExtensionType() != ISD::SEXTLOAD ||
674           LD->getAddressingMode() != ISD::UNINDEXED) {
675         SelectCode(N);
676         return;
677       }
678
679       SDValue Chain = LD->getChain();
680       SDValue TargetConst0 = CurDAG->getTargetConstant(0, dl, MVT::i32);
681       OP0 = SDValue(CurDAG->getMachineNode(Hexagon::L2_loadri_io, dl, MVT::i32,
682                                             MVT::Other,
683                                             LD->getBasePtr(), TargetConst0,
684                                             Chain), 0);
685     } else {
686       SelectCode(N);
687       return;
688     }
689
690     // Same goes for the second operand.
691     if (MulOp1.getOpcode() == ISD::SIGN_EXTEND) {
692       SDValue Sext1 = MulOp1.getOperand(0);
693       if (Sext1.getNode()->getValueType(0) != MVT::i32) {
694         SelectCode(N);
695         return;
696       }
697
698       OP1 = Sext1;
699     } else if (MulOp1.getOpcode() == ISD::LOAD) {
700       LoadSDNode *LD = cast<LoadSDNode>(MulOp1.getNode());
701       if (LD->getMemoryVT() != MVT::i32 ||
702           LD->getExtensionType() != ISD::SEXTLOAD ||
703           LD->getAddressingMode() != ISD::UNINDEXED) {
704         SelectCode(N);
705         return;
706       }
707
708       SDValue Chain = LD->getChain();
709       SDValue TargetConst0 = CurDAG->getTargetConstant(0, dl, MVT::i32);
710       OP1 = SDValue(CurDAG->getMachineNode(Hexagon::L2_loadri_io, dl, MVT::i32,
711                                             MVT::Other,
712                                             LD->getBasePtr(), TargetConst0,
713                                             Chain), 0);
714     } else {
715       SelectCode(N);
716       return;
717     }
718
719     // Generate a mpy instruction.
720     SDNode *Result = CurDAG->getMachineNode(Hexagon::M2_dpmpyss_s0, dl, MVT::i64,
721                                             OP0, OP1);
722     ReplaceNode(N, Result);
723     return;
724   }
725
726   SelectCode(N);
727 }
728
729 void HexagonDAGToDAGISel::SelectSHL(SDNode *N) {
730   SDLoc dl(N);
731   if (N->getValueType(0) == MVT::i32) {
732     SDValue Shl_0 = N->getOperand(0);
733     SDValue Shl_1 = N->getOperand(1);
734     // RHS is const.
735     if (Shl_1.getOpcode() == ISD::Constant) {
736       if (Shl_0.getOpcode() == ISD::MUL) {
737         SDValue Mul_0 = Shl_0.getOperand(0); // Val
738         SDValue Mul_1 = Shl_0.getOperand(1); // Const
739         // RHS of mul is const.
740         if (Mul_1.getOpcode() == ISD::Constant) {
741           int32_t ShlConst =
742             cast<ConstantSDNode>(Shl_1.getNode())->getSExtValue();
743           int32_t MulConst =
744             cast<ConstantSDNode>(Mul_1.getNode())->getSExtValue();
745           int32_t ValConst = MulConst << ShlConst;
746           SDValue Val = CurDAG->getTargetConstant(ValConst, dl,
747                                                   MVT::i32);
748           if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Val.getNode()))
749             if (isInt<9>(CN->getSExtValue())) {
750               SDNode* Result =
751                 CurDAG->getMachineNode(Hexagon::M2_mpysmi, dl,
752                                        MVT::i32, Mul_0, Val);
753               ReplaceNode(N, Result);
754               return;
755             }
756
757         }
758       } else if (Shl_0.getOpcode() == ISD::SUB) {
759         SDValue Sub_0 = Shl_0.getOperand(0); // Const 0
760         SDValue Sub_1 = Shl_0.getOperand(1); // Val
761         if (Sub_0.getOpcode() == ISD::Constant) {
762           int32_t SubConst =
763             cast<ConstantSDNode>(Sub_0.getNode())->getSExtValue();
764           if (SubConst == 0) {
765             if (Sub_1.getOpcode() == ISD::SHL) {
766               SDValue Shl2_0 = Sub_1.getOperand(0); // Val
767               SDValue Shl2_1 = Sub_1.getOperand(1); // Const
768               if (Shl2_1.getOpcode() == ISD::Constant) {
769                 int32_t ShlConst =
770                   cast<ConstantSDNode>(Shl_1.getNode())->getSExtValue();
771                 int32_t Shl2Const =
772                   cast<ConstantSDNode>(Shl2_1.getNode())->getSExtValue();
773                 int32_t ValConst = 1 << (ShlConst+Shl2Const);
774                 SDValue Val = CurDAG->getTargetConstant(-ValConst, dl,
775                                                         MVT::i32);
776                 if (ConstantSDNode *CN =
777                     dyn_cast<ConstantSDNode>(Val.getNode()))
778                   if (isInt<9>(CN->getSExtValue())) {
779                     SDNode* Result =
780                       CurDAG->getMachineNode(Hexagon::M2_mpysmi, dl, MVT::i32,
781                                              Shl2_0, Val);
782                     ReplaceNode(N, Result);
783                     return;
784                   }
785               }
786             }
787           }
788         }
789       }
790     }
791   }
792   SelectCode(N);
793 }
794
795
796 //
797 // If there is an zero_extend followed an intrinsic in DAG (this means - the
798 // result of the intrinsic is predicate); convert the zero_extend to
799 // transfer instruction.
800 //
801 // Zero extend -> transfer is lowered here. Otherwise, zero_extend will be
802 // converted into a MUX as predicate registers defined as 1 bit in the
803 // compiler. Architecture defines them as 8-bit registers.
804 // We want to preserve all the lower 8-bits and, not just 1 LSB bit.
805 //
806 void HexagonDAGToDAGISel::SelectZeroExtend(SDNode *N) {
807   SDLoc dl(N);
808
809   SDValue Op0 = N->getOperand(0);
810   EVT OpVT = Op0.getValueType();
811   unsigned OpBW = OpVT.getSizeInBits();
812
813   // Special handling for zero-extending a vector of booleans.
814   if (OpVT.isVector() && OpVT.getVectorElementType() == MVT::i1 && OpBW <= 64) {
815     SDNode *Mask = CurDAG->getMachineNode(Hexagon::C2_mask, dl, MVT::i64, Op0);
816     unsigned NE = OpVT.getVectorNumElements();
817     EVT ExVT = N->getValueType(0);
818     unsigned ES = ExVT.getVectorElementType().getSizeInBits();
819     uint64_t MV = 0, Bit = 1;
820     for (unsigned i = 0; i < NE; ++i) {
821       MV |= Bit;
822       Bit <<= ES;
823     }
824     SDValue Ones = CurDAG->getTargetConstant(MV, dl, MVT::i64);
825     SDNode *OnesReg = CurDAG->getMachineNode(Hexagon::CONST64_Int_Real, dl,
826                                              MVT::i64, Ones);
827     if (ExVT.getSizeInBits() == 32) {
828       SDNode *And = CurDAG->getMachineNode(Hexagon::A2_andp, dl, MVT::i64,
829                                            SDValue(Mask,0), SDValue(OnesReg,0));
830       SDValue SubR = CurDAG->getTargetConstant(Hexagon::subreg_loreg, dl,
831                                                MVT::i32);
832       ReplaceNode(N, CurDAG->getMachineNode(Hexagon::EXTRACT_SUBREG, dl, ExVT,
833                                             SDValue(And, 0), SubR));
834       return;
835     }
836     ReplaceNode(N,
837                 CurDAG->getMachineNode(Hexagon::A2_andp, dl, ExVT,
838                                        SDValue(Mask, 0), SDValue(OnesReg, 0)));
839     return;
840   }
841
842   SDNode *IsIntrinsic = N->getOperand(0).getNode();
843   if ((IsIntrinsic->getOpcode() == ISD::INTRINSIC_WO_CHAIN)) {
844     unsigned ID =
845       cast<ConstantSDNode>(IsIntrinsic->getOperand(0))->getZExtValue();
846     if (doesIntrinsicReturnPredicate(ID)) {
847       // Now we need to differentiate target data types.
848       if (N->getValueType(0) == MVT::i64) {
849         // Convert the zero_extend to Rs = Pd followed by A2_combinew(0,Rs).
850         SDValue TargetConst0 = CurDAG->getTargetConstant(0, dl, MVT::i32);
851         SDNode *Result_1 = CurDAG->getMachineNode(Hexagon::C2_tfrpr, dl,
852                                                   MVT::i32,
853                                                   SDValue(IsIntrinsic, 0));
854         SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A2_tfrsi, dl,
855                                                   MVT::i32,
856                                                   TargetConst0);
857         SDNode *Result_3 = CurDAG->getMachineNode(Hexagon::A2_combinew, dl,
858                                                   MVT::i64, MVT::Other,
859                                                   SDValue(Result_2, 0),
860                                                   SDValue(Result_1, 0));
861         ReplaceNode(N, Result_3);
862         return;
863       }
864       if (N->getValueType(0) == MVT::i32) {
865         // Convert the zero_extend to Rs = Pd
866         SDNode* RsPd = CurDAG->getMachineNode(Hexagon::C2_tfrpr, dl,
867                                               MVT::i32,
868                                               SDValue(IsIntrinsic, 0));
869         ReplaceNode(N, RsPd);
870         return;
871       }
872       llvm_unreachable("Unexpected value type");
873     }
874   }
875   SelectCode(N);
876 }
877
878
879 //
880 // Handling intrinsics for circular load and bitreverse load.
881 //
882 void HexagonDAGToDAGISel::SelectIntrinsicWChain(SDNode *N) {
883   if (MachineSDNode *L = LoadInstrForLoadIntrinsic(N)) {
884     StoreInstrForLoadIntrinsic(L, N);
885     CurDAG->RemoveDeadNode(N);
886     return;
887   }
888   SelectCode(N);
889 }
890
891 void HexagonDAGToDAGISel::SelectIntrinsicWOChain(SDNode *N) {
892   unsigned IID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
893   unsigned Bits;
894   switch (IID) {
895   case Intrinsic::hexagon_S2_vsplatrb:
896     Bits = 8;
897     break;
898   case Intrinsic::hexagon_S2_vsplatrh:
899     Bits = 16;
900     break;
901   default:
902     SelectCode(N);
903     return;
904   }
905
906   SDValue V = N->getOperand(1);
907   SDValue U;
908   if (isValueExtension(V, Bits, U)) {
909     SDValue R = CurDAG->getNode(N->getOpcode(), SDLoc(N), N->getValueType(0),
910                                 N->getOperand(0), U);
911     ReplaceNode(N, R.getNode());
912     SelectCode(R.getNode());
913     return;
914   }
915   SelectCode(N);
916 }
917
918 //
919 // Map floating point constant values.
920 //
921 void HexagonDAGToDAGISel::SelectConstantFP(SDNode *N) {
922   SDLoc dl(N);
923   ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N);
924   const APFloat &APF = CN->getValueAPF();
925   if (N->getValueType(0) == MVT::f32) {
926     ReplaceNode(
927         N, CurDAG->getMachineNode(Hexagon::TFRI_f, dl, MVT::f32,
928                                   CurDAG->getTargetConstantFP(
929                                       APF.convertToFloat(), dl, MVT::f32)));
930     return;
931   }
932   else if (N->getValueType(0) == MVT::f64) {
933     ReplaceNode(
934         N, CurDAG->getMachineNode(Hexagon::CONST64_Float_Real, dl, MVT::f64,
935                                   CurDAG->getTargetConstantFP(
936                                       APF.convertToDouble(), dl, MVT::f64)));
937     return;
938   }
939
940   SelectCode(N);
941 }
942
943 //
944 // Map predicate true (encoded as -1 in LLVM) to a XOR.
945 //
946 void HexagonDAGToDAGISel::SelectConstant(SDNode *N) {
947   SDLoc dl(N);
948   if (N->getValueType(0) == MVT::i1) {
949     SDNode* Result = 0;
950     int32_t Val = cast<ConstantSDNode>(N)->getSExtValue();
951     if (Val == -1) {
952       Result = CurDAG->getMachineNode(Hexagon::TFR_PdTrue, dl, MVT::i1);
953     } else if (Val == 0) {
954       Result = CurDAG->getMachineNode(Hexagon::TFR_PdFalse, dl, MVT::i1);
955     }
956     if (Result) {
957       ReplaceNode(N, Result);
958       return;
959     }
960   }
961
962   SelectCode(N);
963 }
964
965
966 //
967 // Map add followed by a asr -> asr +=.
968 //
969 void HexagonDAGToDAGISel::SelectAdd(SDNode *N) {
970   SDLoc dl(N);
971   if (N->getValueType(0) != MVT::i32) {
972     SelectCode(N);
973     return;
974   }
975   // Identify nodes of the form: add(asr(...)).
976   SDNode* Src1 = N->getOperand(0).getNode();
977   if (Src1->getOpcode() != ISD::SRA || !Src1->hasOneUse()
978       || Src1->getValueType(0) != MVT::i32) {
979     SelectCode(N);
980     return;
981   }
982
983   // Build Rd = Rd' + asr(Rs, Rt). The machine constraints will ensure that
984   // Rd and Rd' are assigned to the same register
985   SDNode* Result = CurDAG->getMachineNode(Hexagon::S2_asr_r_r_acc, dl, MVT::i32,
986                                           N->getOperand(1),
987                                           Src1->getOperand(0),
988                                           Src1->getOperand(1));
989   ReplaceNode(N, Result);
990 }
991
992 //
993 // Map the following, where possible.
994 // AND/FABS -> clrbit
995 // OR -> setbit
996 // XOR/FNEG ->toggle_bit.
997 //
998 void HexagonDAGToDAGISel::SelectBitOp(SDNode *N) {
999   SDLoc dl(N);
1000   EVT ValueVT = N->getValueType(0);
1001
1002   // We handle only 32 and 64-bit bit ops.
1003   if (!(ValueVT == MVT::i32 || ValueVT == MVT::i64 ||
1004         ValueVT == MVT::f32 || ValueVT == MVT::f64)) {
1005     SelectCode(N);
1006     return;
1007   }
1008
1009   // We handly only fabs and fneg for V5.
1010   unsigned Opc = N->getOpcode();
1011   if ((Opc == ISD::FABS || Opc == ISD::FNEG) && !HST->hasV5TOps()) {
1012     SelectCode(N);
1013     return;
1014   }
1015
1016   int64_t Val = 0;
1017   if (Opc != ISD::FABS && Opc != ISD::FNEG) {
1018     if (N->getOperand(1).getOpcode() == ISD::Constant)
1019       Val = cast<ConstantSDNode>((N)->getOperand(1))->getSExtValue();
1020     else {
1021      SelectCode(N);
1022      return;
1023     }
1024   }
1025
1026   if (Opc == ISD::AND) {
1027     // Check if this is a bit-clearing AND, if not select code the usual way.
1028     if ((ValueVT == MVT::i32 && isPowerOf2_32(~Val)) ||
1029         (ValueVT == MVT::i64 && isPowerOf2_64(~Val)))
1030       Val = ~Val;
1031     else {
1032       SelectCode(N);
1033       return;
1034     }
1035   }
1036
1037   // If OR or AND is being fed by shl, srl and, sra don't do this change,
1038   // because Hexagon provide |= &= on shl, srl, and sra.
1039   // Traverse the DAG to see if there is shl, srl and sra.
1040   if (Opc == ISD::OR || Opc == ISD::AND) {
1041     switch (N->getOperand(0)->getOpcode()) {
1042       default:
1043         break;
1044       case ISD::SRA:
1045       case ISD::SRL:
1046       case ISD::SHL:
1047         SelectCode(N);
1048         return;
1049     }
1050   }
1051
1052   // Make sure it's power of 2.
1053   unsigned BitPos = 0;
1054   if (Opc != ISD::FABS && Opc != ISD::FNEG) {
1055     if ((ValueVT == MVT::i32 && !isPowerOf2_32(Val)) ||
1056         (ValueVT == MVT::i64 && !isPowerOf2_64(Val))) {
1057       SelectCode(N);
1058       return;
1059     }
1060
1061     // Get the bit position.
1062     BitPos = countTrailingZeros(uint64_t(Val));
1063   } else {
1064     // For fabs and fneg, it's always the 31st bit.
1065     BitPos = 31;
1066   }
1067
1068   unsigned BitOpc = 0;
1069   // Set the right opcode for bitwise operations.
1070   switch (Opc) {
1071     default:
1072       llvm_unreachable("Only bit-wise/abs/neg operations are allowed.");
1073     case ISD::AND:
1074     case ISD::FABS:
1075       BitOpc = Hexagon::S2_clrbit_i;
1076       break;
1077     case ISD::OR:
1078       BitOpc = Hexagon::S2_setbit_i;
1079       break;
1080     case ISD::XOR:
1081     case ISD::FNEG:
1082       BitOpc = Hexagon::S2_togglebit_i;
1083       break;
1084   }
1085
1086   SDNode *Result;
1087   // Get the right SDVal for the opcode.
1088   SDValue SDVal = CurDAG->getTargetConstant(BitPos, dl, MVT::i32);
1089
1090   if (ValueVT == MVT::i32 || ValueVT == MVT::f32) {
1091     Result = CurDAG->getMachineNode(BitOpc, dl, ValueVT,
1092                                     N->getOperand(0), SDVal);
1093   } else {
1094     // 64-bit gymnastic to use REG_SEQUENCE. But it's worth it.
1095     EVT SubValueVT;
1096     if (ValueVT == MVT::i64)
1097       SubValueVT = MVT::i32;
1098     else
1099       SubValueVT = MVT::f32;
1100
1101     SDNode *Reg = N->getOperand(0).getNode();
1102     SDValue RegClass = CurDAG->getTargetConstant(Hexagon::DoubleRegsRegClassID,
1103                                                  dl, MVT::i64);
1104
1105     SDValue SubregHiIdx = CurDAG->getTargetConstant(Hexagon::subreg_hireg, dl,
1106                                                     MVT::i32);
1107     SDValue SubregLoIdx = CurDAG->getTargetConstant(Hexagon::subreg_loreg, dl,
1108                                                     MVT::i32);
1109
1110     SDValue SubregHI = CurDAG->getTargetExtractSubreg(Hexagon::subreg_hireg, dl,
1111                                                     MVT::i32, SDValue(Reg, 0));
1112
1113     SDValue SubregLO = CurDAG->getTargetExtractSubreg(Hexagon::subreg_loreg, dl,
1114                                                     MVT::i32, SDValue(Reg, 0));
1115
1116     // Clear/set/toggle hi or lo registers depending on the bit position.
1117     if (SubValueVT != MVT::f32 && BitPos < 32) {
1118       SDNode *Result0 = CurDAG->getMachineNode(BitOpc, dl, SubValueVT,
1119                                                SubregLO, SDVal);
1120       const SDValue Ops[] = { RegClass, SubregHI, SubregHiIdx,
1121                               SDValue(Result0, 0), SubregLoIdx };
1122       Result = CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE,
1123                                       dl, ValueVT, Ops);
1124     } else {
1125       if (Opc != ISD::FABS && Opc != ISD::FNEG)
1126         SDVal = CurDAG->getTargetConstant(BitPos-32, dl, MVT::i32);
1127       SDNode *Result0 = CurDAG->getMachineNode(BitOpc, dl, SubValueVT,
1128                                                SubregHI, SDVal);
1129       const SDValue Ops[] = { RegClass, SDValue(Result0, 0), SubregHiIdx,
1130                               SubregLO, SubregLoIdx };
1131       Result = CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE,
1132                                       dl, ValueVT, Ops);
1133     }
1134   }
1135
1136   ReplaceNode(N, Result);
1137 }
1138
1139
1140 void HexagonDAGToDAGISel::SelectFrameIndex(SDNode *N) {
1141   MachineFrameInfo *MFI = MF->getFrameInfo();
1142   const HexagonFrameLowering *HFI = HST->getFrameLowering();
1143   int FX = cast<FrameIndexSDNode>(N)->getIndex();
1144   unsigned StkA = HFI->getStackAlignment();
1145   unsigned MaxA = MFI->getMaxAlignment();
1146   SDValue FI = CurDAG->getTargetFrameIndex(FX, MVT::i32);
1147   SDLoc DL(N);
1148   SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32);
1149   SDNode *R = 0;
1150
1151   // Use TFR_FI when:
1152   // - the object is fixed, or
1153   // - there are no objects with higher-than-default alignment, or
1154   // - there are no dynamically allocated objects.
1155   // Otherwise, use TFR_FIA.
1156   if (FX < 0 || MaxA <= StkA || !MFI->hasVarSizedObjects()) {
1157     R = CurDAG->getMachineNode(Hexagon::TFR_FI, DL, MVT::i32, FI, Zero);
1158   } else {
1159     auto &HMFI = *MF->getInfo<HexagonMachineFunctionInfo>();
1160     unsigned AR = HMFI.getStackAlignBaseVReg();
1161     SDValue CH = CurDAG->getEntryNode();
1162     SDValue Ops[] = { CurDAG->getCopyFromReg(CH, DL, AR, MVT::i32), FI, Zero };
1163     R = CurDAG->getMachineNode(Hexagon::TFR_FIA, DL, MVT::i32, Ops);
1164   }
1165
1166   ReplaceNode(N, R);
1167 }
1168
1169
1170 void HexagonDAGToDAGISel::SelectBitcast(SDNode *N) {
1171   EVT SVT = N->getOperand(0).getValueType();
1172   EVT DVT = N->getValueType(0);
1173   if (!SVT.isVector() || !DVT.isVector() ||
1174       SVT.getVectorElementType() == MVT::i1 ||
1175       DVT.getVectorElementType() == MVT::i1 ||
1176       SVT.getSizeInBits() != DVT.getSizeInBits()) {
1177     SelectCode(N);
1178     return;
1179   }
1180
1181   CurDAG->ReplaceAllUsesOfValueWith(SDValue(N,0), N->getOperand(0));
1182   CurDAG->RemoveDeadNode(N);
1183 }
1184
1185
1186 void HexagonDAGToDAGISel::Select(SDNode *N) {
1187   if (N->isMachineOpcode()) {
1188     N->setNodeId(-1);
1189     return;   // Already selected.
1190   }
1191
1192   switch (N->getOpcode()) {
1193   case ISD::Constant:
1194     SelectConstant(N);
1195     return;
1196
1197   case ISD::ConstantFP:
1198     SelectConstantFP(N);
1199     return;
1200
1201   case ISD::FrameIndex:
1202     SelectFrameIndex(N);
1203     return;
1204
1205   case ISD::ADD:
1206     SelectAdd(N);
1207     return;
1208
1209   case ISD::BITCAST:
1210     SelectBitcast(N);
1211     return;
1212
1213   case ISD::SHL:
1214     SelectSHL(N);
1215     return;
1216
1217   case ISD::LOAD:
1218     SelectLoad(N);
1219     return;
1220
1221   case ISD::STORE:
1222     SelectStore(N);
1223     return;
1224
1225   case ISD::MUL:
1226     SelectMul(N);
1227     return;
1228
1229   case ISD::AND:
1230   case ISD::OR:
1231   case ISD::XOR:
1232   case ISD::FABS:
1233   case ISD::FNEG:
1234     SelectBitOp(N);
1235     return;
1236
1237   case ISD::ZERO_EXTEND:
1238     SelectZeroExtend(N);
1239     return;
1240
1241   case ISD::INTRINSIC_W_CHAIN:
1242     SelectIntrinsicWChain(N);
1243     return;
1244
1245   case ISD::INTRINSIC_WO_CHAIN:
1246     SelectIntrinsicWOChain(N);
1247     return;
1248   }
1249
1250   SelectCode(N);
1251 }
1252
1253 bool HexagonDAGToDAGISel::
1254 SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID,
1255                              std::vector<SDValue> &OutOps) {
1256   SDValue Inp = Op, Res;
1257
1258   switch (ConstraintID) {
1259   default:
1260     return true;
1261   case InlineAsm::Constraint_i:
1262   case InlineAsm::Constraint_o: // Offsetable.
1263   case InlineAsm::Constraint_v: // Not offsetable.
1264   case InlineAsm::Constraint_m: // Memory.
1265     if (SelectAddrFI(Inp, Res))
1266       OutOps.push_back(Res);
1267     else
1268       OutOps.push_back(Inp);
1269     break;
1270   }
1271
1272   OutOps.push_back(CurDAG->getTargetConstant(0, SDLoc(Op), MVT::i32));
1273   return false;
1274 }
1275
1276
1277 void HexagonDAGToDAGISel::PreprocessISelDAG() {
1278   SelectionDAG &DAG = *CurDAG;
1279   std::vector<SDNode*> Nodes;
1280   for (SDNode &Node : DAG.allnodes())
1281     Nodes.push_back(&Node);
1282
1283   // Simplify: (or (select c x 0) z)  ->  (select c (or x z) z)
1284   //           (or (select c 0 y) z)  ->  (select c z (or y z))
1285   // This may not be the right thing for all targets, so do it here.
1286   for (auto I : Nodes) {
1287     if (I->getOpcode() != ISD::OR)
1288       continue;
1289
1290     auto IsZero = [] (const SDValue &V) -> bool {
1291       if (ConstantSDNode *SC = dyn_cast<ConstantSDNode>(V.getNode()))
1292         return SC->isNullValue();
1293       return false;
1294     };
1295     auto IsSelect0 = [IsZero] (const SDValue &Op) -> bool {
1296       if (Op.getOpcode() != ISD::SELECT)
1297         return false;
1298       return IsZero(Op.getOperand(1)) || IsZero(Op.getOperand(2));
1299     };
1300
1301     SDValue N0 = I->getOperand(0), N1 = I->getOperand(1);
1302     EVT VT = I->getValueType(0);
1303     bool SelN0 = IsSelect0(N0);
1304     SDValue SOp = SelN0 ? N0 : N1;
1305     SDValue VOp = SelN0 ? N1 : N0;
1306
1307     if (SOp.getOpcode() == ISD::SELECT && SOp.getNode()->hasOneUse()) {
1308       SDValue SC = SOp.getOperand(0);
1309       SDValue SX = SOp.getOperand(1);
1310       SDValue SY = SOp.getOperand(2);
1311       SDLoc DLS = SOp;
1312       if (IsZero(SY)) {
1313         SDValue NewOr = DAG.getNode(ISD::OR, DLS, VT, SX, VOp);
1314         SDValue NewSel = DAG.getNode(ISD::SELECT, DLS, VT, SC, NewOr, VOp);
1315         DAG.ReplaceAllUsesWith(I, NewSel.getNode());
1316       } else if (IsZero(SX)) {
1317         SDValue NewOr = DAG.getNode(ISD::OR, DLS, VT, SY, VOp);
1318         SDValue NewSel = DAG.getNode(ISD::SELECT, DLS, VT, SC, VOp, NewOr);
1319         DAG.ReplaceAllUsesWith(I, NewSel.getNode());
1320       }
1321     }
1322   }
1323
1324   // Transform: (store ch addr (add x (add (shl y c) e)))
1325   //        to: (store ch addr (add x (shl (add y d) c))),
1326   // where e = (shl d c) for some integer d.
1327   // The purpose of this is to enable generation of loads/stores with
1328   // shifted addressing mode, i.e. mem(x+y<<#c). For that, the shift
1329   // value c must be 0, 1 or 2.
1330   for (auto I : Nodes) {
1331     if (I->getOpcode() != ISD::STORE)
1332       continue;
1333
1334     // I matched: (store ch addr Off)
1335     SDValue Off = I->getOperand(2);
1336     // Off needs to match: (add x (add (shl y c) (shl d c))))
1337     if (Off.getOpcode() != ISD::ADD)
1338       continue;
1339     // Off matched: (add x T0)
1340     SDValue T0 = Off.getOperand(1);
1341     // T0 needs to match: (add T1 T2):
1342     if (T0.getOpcode() != ISD::ADD)
1343       continue;
1344     // T0 matched: (add T1 T2)
1345     SDValue T1 = T0.getOperand(0);
1346     SDValue T2 = T0.getOperand(1);
1347     // T1 needs to match: (shl y c)
1348     if (T1.getOpcode() != ISD::SHL)
1349       continue;
1350     SDValue C = T1.getOperand(1);
1351     ConstantSDNode *CN = dyn_cast<ConstantSDNode>(C.getNode());
1352     if (CN == nullptr)
1353       continue;
1354     unsigned CV = CN->getZExtValue();
1355     if (CV > 2)
1356       continue;
1357     // T2 needs to match e, where e = (shl d c) for some d.
1358     ConstantSDNode *EN = dyn_cast<ConstantSDNode>(T2.getNode());
1359     if (EN == nullptr)
1360       continue;
1361     unsigned EV = EN->getZExtValue();
1362     if (EV % (1 << CV) != 0)
1363       continue;
1364     unsigned DV = EV / (1 << CV);
1365
1366     // Replace T0 with: (shl (add y d) c)
1367     SDLoc DL = SDLoc(I);
1368     EVT VT = T0.getValueType();
1369     SDValue D = DAG.getConstant(DV, DL, VT);
1370     // NewAdd = (add y d)
1371     SDValue NewAdd = DAG.getNode(ISD::ADD, DL, VT, T1.getOperand(0), D);
1372     // NewShl = (shl NewAdd c)
1373     SDValue NewShl = DAG.getNode(ISD::SHL, DL, VT, NewAdd, C);
1374     ReplaceNode(T0.getNode(), NewShl.getNode());
1375   }
1376 }
1377
1378 void HexagonDAGToDAGISel::EmitFunctionEntryCode() {
1379   auto &HST = static_cast<const HexagonSubtarget&>(MF->getSubtarget());
1380   auto &HFI = *HST.getFrameLowering();
1381   if (!HFI.needsAligna(*MF))
1382     return;
1383
1384   MachineFrameInfo *MFI = MF->getFrameInfo();
1385   MachineBasicBlock *EntryBB = &MF->front();
1386   unsigned AR = FuncInfo->CreateReg(MVT::i32);
1387   unsigned MaxA = MFI->getMaxAlignment();
1388   BuildMI(EntryBB, DebugLoc(), HII->get(Hexagon::ALIGNA), AR)
1389       .addImm(MaxA);
1390   MF->getInfo<HexagonMachineFunctionInfo>()->setStackAlignBaseVReg(AR);
1391 }
1392
1393 // Match a frame index that can be used in an addressing mode.
1394 bool HexagonDAGToDAGISel::SelectAddrFI(SDValue& N, SDValue &R) {
1395   if (N.getOpcode() != ISD::FrameIndex)
1396     return false;
1397   auto &HFI = *HST->getFrameLowering();
1398   MachineFrameInfo *MFI = MF->getFrameInfo();
1399   int FX = cast<FrameIndexSDNode>(N)->getIndex();
1400   if (!MFI->isFixedObjectIndex(FX) && HFI.needsAligna(*MF))
1401     return false;
1402   R = CurDAG->getTargetFrameIndex(FX, MVT::i32);
1403   return true;
1404 }
1405
1406 inline bool HexagonDAGToDAGISel::SelectAddrGA(SDValue &N, SDValue &R) {
1407   return SelectGlobalAddress(N, R, false);
1408 }
1409
1410 inline bool HexagonDAGToDAGISel::SelectAddrGP(SDValue &N, SDValue &R) {
1411   return SelectGlobalAddress(N, R, true);
1412 }
1413
1414 bool HexagonDAGToDAGISel::SelectGlobalAddress(SDValue &N, SDValue &R,
1415                                               bool UseGP) {
1416   switch (N.getOpcode()) {
1417   case ISD::ADD: {
1418     SDValue N0 = N.getOperand(0);
1419     SDValue N1 = N.getOperand(1);
1420     unsigned GAOpc = N0.getOpcode();
1421     if (UseGP && GAOpc != HexagonISD::CONST32_GP)
1422       return false;
1423     if (!UseGP && GAOpc != HexagonISD::CONST32)
1424       return false;
1425     if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N1)) {
1426       SDValue Addr = N0.getOperand(0);
1427       if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Addr)) {
1428         if (GA->getOpcode() == ISD::TargetGlobalAddress) {
1429           uint64_t NewOff = GA->getOffset() + (uint64_t)Const->getSExtValue();
1430           R = CurDAG->getTargetGlobalAddress(GA->getGlobal(), SDLoc(Const),
1431                                              N.getValueType(), NewOff);
1432           return true;
1433         }
1434       }
1435     }
1436     break;
1437   }
1438   case HexagonISD::CONST32:
1439     // The operand(0) of CONST32 is TargetGlobalAddress, which is what we
1440     // want in the instruction.
1441     if (!UseGP)
1442       R = N.getOperand(0);
1443     return !UseGP;
1444   case HexagonISD::CONST32_GP:
1445     if (UseGP)
1446       R = N.getOperand(0);
1447     return UseGP;
1448   default:
1449     return false;
1450   }
1451
1452   return false;
1453 }
1454
1455 bool HexagonDAGToDAGISel::isValueExtension(const SDValue &Val,
1456       unsigned FromBits, SDValue &Src) {
1457   unsigned Opc = Val.getOpcode();
1458   switch (Opc) {
1459   case ISD::SIGN_EXTEND:
1460   case ISD::ZERO_EXTEND:
1461   case ISD::ANY_EXTEND: {
1462     SDValue const &Op0 = Val.getOperand(0);
1463     EVT T = Op0.getValueType();
1464     if (T.isInteger() && T.getSizeInBits() == FromBits) {
1465       Src = Op0;
1466       return true;
1467     }
1468     break;
1469   }
1470   case ISD::SIGN_EXTEND_INREG:
1471   case ISD::AssertSext:
1472   case ISD::AssertZext:
1473     if (Val.getOperand(0).getValueType().isInteger()) {
1474       VTSDNode *T = cast<VTSDNode>(Val.getOperand(1));
1475       if (T->getVT().getSizeInBits() == FromBits) {
1476         Src = Val.getOperand(0);
1477         return true;
1478       }
1479     }
1480     break;
1481   case ISD::AND: {
1482     // Check if this is an AND with "FromBits" of lower bits set to 1.
1483     uint64_t FromMask = (1 << FromBits) - 1;
1484     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(0))) {
1485       if (C->getZExtValue() == FromMask) {
1486         Src = Val.getOperand(1);
1487         return true;
1488       }
1489     }
1490     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(1))) {
1491       if (C->getZExtValue() == FromMask) {
1492         Src = Val.getOperand(0);
1493         return true;
1494       }
1495     }
1496     break;
1497   }
1498   case ISD::OR:
1499   case ISD::XOR: {
1500     // OR/XOR with the lower "FromBits" bits set to 0.
1501     uint64_t FromMask = (1 << FromBits) - 1;
1502     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(0))) {
1503       if ((C->getZExtValue() & FromMask) == 0) {
1504         Src = Val.getOperand(1);
1505         return true;
1506       }
1507     }
1508     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(1))) {
1509       if ((C->getZExtValue() & FromMask) == 0) {
1510         Src = Val.getOperand(0);
1511         return true;
1512       }
1513     }
1514   }
1515   default:
1516     break;
1517   }
1518   return false;
1519 }
1520
1521
1522 bool HexagonDAGToDAGISel::orIsAdd(const SDNode *N) const {
1523   assert(N->getOpcode() == ISD::OR);
1524   auto *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
1525   assert(C);
1526
1527   // Detect when "or" is used to add an offset to a stack object.
1528   if (auto *FN = dyn_cast<FrameIndexSDNode>(N->getOperand(0))) {
1529     MachineFrameInfo *MFI = MF->getFrameInfo();
1530     unsigned A = MFI->getObjectAlignment(FN->getIndex());
1531     assert(isPowerOf2_32(A));
1532     int32_t Off = C->getSExtValue();
1533     // If the alleged offset fits in the zero bits guaranteed by
1534     // the alignment, then this or is really an add.
1535     return (Off >= 0) && (((A-1) & Off) == unsigned(Off));
1536   }
1537   return false;
1538 }
1539
1540 bool HexagonDAGToDAGISel::isAlignedMemNode(const MemSDNode *N) const {
1541   return N->getAlignment() >= N->getMemoryVT().getStoreSize();
1542 }