58f2850f7a1deac6a6b8f8ccc69197bd36bae852
[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   const HexagonTargetMachine& HTM;
49   const HexagonSubtarget &HST;
50 public:
51   explicit HexagonDAGToDAGISel(HexagonTargetMachine &tm,
52                                CodeGenOpt::Level OptLevel)
53       : SelectionDAGISel(tm, OptLevel), HTM(tm),
54         HST(tm.getSubtarget<HexagonSubtarget>()) {
55     initializeHexagonDAGToDAGISelPass(*PassRegistry::getPassRegistry());
56   }
57   virtual void PreprocessISelDAG() override;
58
59   SDNode *Select(SDNode *N) override;
60
61   // Complex Pattern Selectors.
62   inline bool SelectAddrGA(SDValue &N, SDValue &R);
63   inline bool SelectAddrGP(SDValue &N, SDValue &R);
64   bool SelectGlobalAddress(SDValue &N, SDValue &R, bool UseGP);
65   bool SelectAddrFI(SDValue &N, SDValue &R);
66
67   const char *getPassName() const override {
68     return "Hexagon DAG->DAG Pattern Instruction Selection";
69   }
70
71   SDNode *SelectFrameIndex(SDNode *N);
72   /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
73   /// inline asm expressions.
74   bool SelectInlineAsmMemoryOperand(const SDValue &Op,
75                                     unsigned ConstraintID,
76                                     std::vector<SDValue> &OutOps) override;
77   SDNode *SelectLoad(SDNode *N);
78   SDNode *SelectBaseOffsetLoad(LoadSDNode *LD, SDLoc dl);
79   SDNode *SelectIndexedLoad(LoadSDNode *LD, SDLoc dl);
80   SDNode *SelectIndexedLoadZeroExtend64(LoadSDNode *LD, unsigned Opcode,
81                                         SDLoc dl);
82   SDNode *SelectIndexedLoadSignExtend64(LoadSDNode *LD, unsigned Opcode,
83                                         SDLoc dl);
84   SDNode *SelectBaseOffsetStore(StoreSDNode *ST, SDLoc dl);
85   SDNode *SelectIndexedStore(StoreSDNode *ST, SDLoc dl);
86   SDNode *SelectStore(SDNode *N);
87   SDNode *SelectSHL(SDNode *N);
88   SDNode *SelectMul(SDNode *N);
89   SDNode *SelectZeroExtend(SDNode *N);
90   SDNode *SelectIntrinsicWChain(SDNode *N);
91   SDNode *SelectIntrinsicWOChain(SDNode *N);
92   SDNode *SelectConstant(SDNode *N);
93   SDNode *SelectConstantFP(SDNode *N);
94   SDNode *SelectAdd(SDNode *N);
95
96   // XformMskToBitPosU5Imm - Returns the bit position which
97   // the single bit 32 bit mask represents.
98   // Used in Clr and Set bit immediate memops.
99   SDValue XformMskToBitPosU5Imm(uint32_t Imm) {
100     int32_t bitPos;
101     bitPos = Log2_32(Imm);
102     assert(bitPos >= 0 && bitPos < 32 &&
103            "Constant out of range for 32 BitPos Memops");
104     return CurDAG->getTargetConstant(bitPos, MVT::i32);
105   }
106
107   // XformMskToBitPosU4Imm - Returns the bit position which the single-bit
108   // 16 bit mask represents. Used in Clr and Set bit immediate memops.
109   SDValue XformMskToBitPosU4Imm(uint16_t Imm) {
110     return XformMskToBitPosU5Imm(Imm);
111   }
112
113   // XformMskToBitPosU3Imm - Returns the bit position which the single-bit
114   // 8 bit mask represents. Used in Clr and Set bit immediate memops.
115   SDValue XformMskToBitPosU3Imm(uint8_t Imm) {
116     return XformMskToBitPosU5Imm(Imm);
117   }
118
119   // Return true if there is exactly one bit set in V, i.e., if V is one of the
120   // following integers: 2^0, 2^1, ..., 2^31.
121   bool ImmIsSingleBit(uint32_t v) const {
122     return isPowerOf2_32(v);
123   }
124
125   // XformM5ToU5Imm - Return a target constant with the specified value, of
126   // type i32 where the negative literal is transformed into a positive literal
127   // for use in -= memops.
128   inline SDValue XformM5ToU5Imm(signed Imm) {
129      assert( (Imm >= -31 && Imm <= -1)  && "Constant out of range for Memops");
130      return CurDAG->getTargetConstant( - Imm, MVT::i32);
131   }
132
133   // XformU7ToU7M1Imm - Return a target constant decremented by 1, in range
134   // [1..128], used in cmpb.gtu instructions.
135   inline SDValue XformU7ToU7M1Imm(signed Imm) {
136     assert((Imm >= 1 && Imm <= 128) && "Constant out of range for cmpb op");
137     return CurDAG->getTargetConstant(Imm - 1, MVT::i8);
138   }
139
140   // XformS8ToS8M1Imm - Return a target constant decremented by 1.
141   inline SDValue XformSToSM1Imm(signed Imm) {
142     return CurDAG->getTargetConstant(Imm - 1, MVT::i32);
143   }
144
145   // XformU8ToU8M1Imm - Return a target constant decremented by 1.
146   inline SDValue XformUToUM1Imm(unsigned Imm) {
147     assert((Imm >= 1) && "Cannot decrement unsigned int less than 1");
148     return CurDAG->getTargetConstant(Imm - 1, MVT::i32);
149   }
150
151   // XformSToSM2Imm - Return a target constant decremented by 2.
152   inline SDValue XformSToSM2Imm(unsigned Imm) {
153     return CurDAG->getTargetConstant(Imm - 2, MVT::i32);
154   }
155
156   // XformSToSM3Imm - Return a target constant decremented by 3.
157   inline SDValue XformSToSM3Imm(unsigned Imm) {
158     return CurDAG->getTargetConstant(Imm - 3, MVT::i32);
159   }
160
161   // Include the pieces autogenerated from the target description.
162   #include "HexagonGenDAGISel.inc"
163
164 private:
165   bool isValueExtension(const SDValue &Val, unsigned FromBits, SDValue &Src);
166 }; // end HexagonDAGToDAGISel
167 }  // end anonymous namespace
168
169
170 /// createHexagonISelDag - This pass converts a legalized DAG into a
171 /// Hexagon-specific DAG, ready for instruction scheduling.
172 ///
173 namespace llvm {
174 FunctionPass *createHexagonISelDag(HexagonTargetMachine &TM,
175                                    CodeGenOpt::Level OptLevel) {
176   return new HexagonDAGToDAGISel(TM, OptLevel);
177 }
178 }
179
180 static void initializePassOnce(PassRegistry &Registry) {
181   const char *Name = "Hexagon DAG->DAG Pattern Instruction Selection";
182   PassInfo *PI = new PassInfo(Name, "hexagon-isel",
183                               &SelectionDAGISel::ID, nullptr, false, false);
184   Registry.registerPass(*PI, true);
185 }
186
187 void llvm::initializeHexagonDAGToDAGISelPass(PassRegistry &Registry) {
188   CALL_ONCE_INITIALIZATION(initializePassOnce)
189 }
190
191
192 // Intrinsics that return a a predicate.
193 static unsigned doesIntrinsicReturnPredicate(unsigned ID)
194 {
195   switch (ID) {
196     default:
197       return 0;
198     case Intrinsic::hexagon_C2_cmpeq:
199     case Intrinsic::hexagon_C2_cmpgt:
200     case Intrinsic::hexagon_C2_cmpgtu:
201     case Intrinsic::hexagon_C2_cmpgtup:
202     case Intrinsic::hexagon_C2_cmpgtp:
203     case Intrinsic::hexagon_C2_cmpeqp:
204     case Intrinsic::hexagon_C2_bitsset:
205     case Intrinsic::hexagon_C2_bitsclr:
206     case Intrinsic::hexagon_C2_cmpeqi:
207     case Intrinsic::hexagon_C2_cmpgti:
208     case Intrinsic::hexagon_C2_cmpgtui:
209     case Intrinsic::hexagon_C2_cmpgei:
210     case Intrinsic::hexagon_C2_cmpgeui:
211     case Intrinsic::hexagon_C2_cmplt:
212     case Intrinsic::hexagon_C2_cmpltu:
213     case Intrinsic::hexagon_C2_bitsclri:
214     case Intrinsic::hexagon_C2_and:
215     case Intrinsic::hexagon_C2_or:
216     case Intrinsic::hexagon_C2_xor:
217     case Intrinsic::hexagon_C2_andn:
218     case Intrinsic::hexagon_C2_not:
219     case Intrinsic::hexagon_C2_orn:
220     case Intrinsic::hexagon_C2_pxfer_map:
221     case Intrinsic::hexagon_C2_any8:
222     case Intrinsic::hexagon_C2_all8:
223     case Intrinsic::hexagon_A2_vcmpbeq:
224     case Intrinsic::hexagon_A2_vcmpbgtu:
225     case Intrinsic::hexagon_A2_vcmpheq:
226     case Intrinsic::hexagon_A2_vcmphgt:
227     case Intrinsic::hexagon_A2_vcmphgtu:
228     case Intrinsic::hexagon_A2_vcmpweq:
229     case Intrinsic::hexagon_A2_vcmpwgt:
230     case Intrinsic::hexagon_A2_vcmpwgtu:
231     case Intrinsic::hexagon_C2_tfrrp:
232     case Intrinsic::hexagon_S2_tstbit_i:
233     case Intrinsic::hexagon_S2_tstbit_r:
234       return 1;
235   }
236 }
237
238 SDNode *HexagonDAGToDAGISel::SelectIndexedLoadSignExtend64(LoadSDNode *LD,
239                                                            unsigned Opcode,
240                                                            SDLoc dl) {
241   SDValue Chain = LD->getChain();
242   EVT LoadedVT = LD->getMemoryVT();
243   SDValue Base = LD->getBasePtr();
244   SDValue Offset = LD->getOffset();
245   SDNode *OffsetNode = Offset.getNode();
246   int32_t Val = cast<ConstantSDNode>(OffsetNode)->getSExtValue();
247
248   const HexagonInstrInfo &TII = *HST.getInstrInfo();
249   if (TII.isValidAutoIncImm(LoadedVT, Val)) {
250     SDValue TargetConst = CurDAG->getTargetConstant(Val, MVT::i32);
251     SDNode *Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::i32, MVT::i32,
252                                               MVT::Other, Base, TargetConst,
253                                               Chain);
254     SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A2_sxtw, dl, MVT::i64,
255                                               SDValue(Result_1, 0));
256     MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
257     MemOp[0] = LD->getMemOperand();
258     cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
259     const SDValue Froms[] = { SDValue(LD, 0),
260                               SDValue(LD, 1),
261                               SDValue(LD, 2) };
262     const SDValue Tos[]   = { SDValue(Result_2, 0),
263                               SDValue(Result_1, 1),
264                               SDValue(Result_1, 2) };
265     ReplaceUses(Froms, Tos, 3);
266     return Result_2;
267   }
268
269   SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
270   SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
271   SDNode *Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::i32, MVT::Other,
272                                             Base, TargetConst0, Chain);
273   SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A2_sxtw, dl, MVT::i64,
274                                             SDValue(Result_1, 0));
275   SDNode* Result_3 = CurDAG->getMachineNode(Hexagon::A2_addi, dl, MVT::i32,
276                                             Base, TargetConstVal,
277                                             SDValue(Result_1, 1));
278   MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
279   MemOp[0] = LD->getMemOperand();
280   cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
281   const SDValue Froms[] = { SDValue(LD, 0),
282                             SDValue(LD, 1),
283                             SDValue(LD, 2) };
284   const SDValue Tos[]   = { SDValue(Result_2, 0),
285                             SDValue(Result_3, 0),
286                             SDValue(Result_1, 1) };
287   ReplaceUses(Froms, Tos, 3);
288   return Result_2;
289 }
290
291
292 SDNode *HexagonDAGToDAGISel::SelectIndexedLoadZeroExtend64(LoadSDNode *LD,
293                                                            unsigned Opcode,
294                                                            SDLoc dl) {
295   SDValue Chain = LD->getChain();
296   EVT LoadedVT = LD->getMemoryVT();
297   SDValue Base = LD->getBasePtr();
298   SDValue Offset = LD->getOffset();
299   SDNode *OffsetNode = Offset.getNode();
300   int32_t Val = cast<ConstantSDNode>(OffsetNode)->getSExtValue();
301
302   const HexagonInstrInfo &TII = *HST.getInstrInfo();
303   if (TII.isValidAutoIncImm(LoadedVT, Val)) {
304     SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
305     SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
306     SDNode *Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::i32,
307                                               MVT::i32, MVT::Other, Base,
308                                               TargetConstVal, Chain);
309     SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A4_combineir, dl,
310                                               MVT::i64, MVT::Other,
311                                               TargetConst0,
312                                               SDValue(Result_1,0));
313     MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
314     MemOp[0] = LD->getMemOperand();
315     cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
316     const SDValue Froms[] = { SDValue(LD, 0),
317                               SDValue(LD, 1),
318                               SDValue(LD, 2) };
319     const SDValue Tos[]   = { SDValue(Result_2, 0),
320                               SDValue(Result_1, 1),
321                               SDValue(Result_1, 2) };
322     ReplaceUses(Froms, Tos, 3);
323     return Result_2;
324   }
325
326   // Generate an indirect load.
327   SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
328   SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
329   SDNode *Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::i32,
330                                             MVT::Other, Base, TargetConst0,
331                                             Chain);
332   SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A4_combineir, dl,
333                                             MVT::i64, MVT::Other,
334                                             TargetConst0,
335                                             SDValue(Result_1,0));
336   // Add offset to base.
337   SDNode* Result_3 = CurDAG->getMachineNode(Hexagon::A2_addi, dl, MVT::i32,
338                                             Base, TargetConstVal,
339                                             SDValue(Result_1, 1));
340   MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
341   MemOp[0] = LD->getMemOperand();
342   cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
343   const SDValue Froms[] = { SDValue(LD, 0),
344                             SDValue(LD, 1),
345                             SDValue(LD, 2) };
346   const SDValue Tos[]   = { SDValue(Result_2, 0), // Load value.
347                             SDValue(Result_3, 0), // New address.
348                             SDValue(Result_1, 1) };
349   ReplaceUses(Froms, Tos, 3);
350   return Result_2;
351 }
352
353
354 SDNode *HexagonDAGToDAGISel::SelectIndexedLoad(LoadSDNode *LD, SDLoc dl) {
355   SDValue Chain = LD->getChain();
356   SDValue Base = LD->getBasePtr();
357   SDValue Offset = LD->getOffset();
358   SDNode *OffsetNode = Offset.getNode();
359   // Get the constant value.
360   int32_t Val = cast<ConstantSDNode>(OffsetNode)->getSExtValue();
361   EVT LoadedVT = LD->getMemoryVT();
362   unsigned Opcode = 0;
363
364   // Check for zero extended loads. Treat any-extend loads as zero extended
365   // loads.
366   ISD::LoadExtType ExtType = LD->getExtensionType();
367   bool IsZeroExt = (ExtType == ISD::ZEXTLOAD || ExtType == ISD::EXTLOAD);
368
369   // Figure out the opcode.
370   const HexagonInstrInfo &TII = *HST.getInstrInfo();
371   if (LoadedVT == MVT::i64) {
372     if (TII.isValidAutoIncImm(LoadedVT, Val))
373       Opcode = Hexagon::L2_loadrd_pi;
374     else
375       Opcode = Hexagon::L2_loadrd_io;
376   } else if (LoadedVT == MVT::i32) {
377     if (TII.isValidAutoIncImm(LoadedVT, Val))
378       Opcode = Hexagon::L2_loadri_pi;
379     else
380       Opcode = Hexagon::L2_loadri_io;
381   } else if (LoadedVT == MVT::i16) {
382     if (TII.isValidAutoIncImm(LoadedVT, Val))
383       Opcode = IsZeroExt ? Hexagon::L2_loadruh_pi : Hexagon::L2_loadrh_pi;
384     else
385       Opcode = IsZeroExt ? Hexagon::L2_loadruh_io : Hexagon::L2_loadrh_io;
386   } else if (LoadedVT == MVT::i8) {
387     if (TII.isValidAutoIncImm(LoadedVT, Val))
388       Opcode = IsZeroExt ? Hexagon::L2_loadrub_pi : Hexagon::L2_loadrb_pi;
389     else
390       Opcode = IsZeroExt ? Hexagon::L2_loadrub_io : Hexagon::L2_loadrb_io;
391   } else
392     llvm_unreachable("unknown memory type");
393
394   // For zero extended i64 loads, we need to add combine instructions.
395   if (LD->getValueType(0) == MVT::i64 && IsZeroExt)
396     return SelectIndexedLoadZeroExtend64(LD, Opcode, dl);
397   // Handle sign extended i64 loads.
398   if (LD->getValueType(0) == MVT::i64 && ExtType == ISD::SEXTLOAD)
399     return SelectIndexedLoadSignExtend64(LD, Opcode, dl);
400
401   if (TII.isValidAutoIncImm(LoadedVT, Val)) {
402     SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
403     SDNode* Result = CurDAG->getMachineNode(Opcode, dl,
404                                             LD->getValueType(0),
405                                             MVT::i32, MVT::Other, Base,
406                                             TargetConstVal, Chain);
407     MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
408     MemOp[0] = LD->getMemOperand();
409     cast<MachineSDNode>(Result)->setMemRefs(MemOp, MemOp + 1);
410     const SDValue Froms[] = { SDValue(LD, 0),
411                               SDValue(LD, 1),
412                               SDValue(LD, 2)
413     };
414     const SDValue Tos[]   = { SDValue(Result, 0),
415                               SDValue(Result, 1),
416                               SDValue(Result, 2)
417     };
418     ReplaceUses(Froms, Tos, 3);
419     return Result;
420   } else {
421     SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
422     SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
423     SDNode* Result_1 = CurDAG->getMachineNode(Opcode, dl,
424                                               LD->getValueType(0),
425                                               MVT::Other, Base, TargetConst0,
426                                               Chain);
427     SDNode* Result_2 = CurDAG->getMachineNode(Hexagon::A2_addi, dl, MVT::i32,
428                                               Base, TargetConstVal,
429                                               SDValue(Result_1, 1));
430     MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
431     MemOp[0] = LD->getMemOperand();
432     cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
433     const SDValue Froms[] = { SDValue(LD, 0),
434                               SDValue(LD, 1),
435                               SDValue(LD, 2)
436     };
437     const SDValue Tos[]   = { SDValue(Result_1, 0),
438                               SDValue(Result_2, 0),
439                               SDValue(Result_1, 1)
440     };
441     ReplaceUses(Froms, Tos, 3);
442     return Result_1;
443   }
444 }
445
446
447 SDNode *HexagonDAGToDAGISel::SelectLoad(SDNode *N) {
448   SDNode *result;
449   SDLoc dl(N);
450   LoadSDNode *LD = cast<LoadSDNode>(N);
451   ISD::MemIndexedMode AM = LD->getAddressingMode();
452
453   // Handle indexed loads.
454   if (AM != ISD::UNINDEXED) {
455     result = SelectIndexedLoad(LD, dl);
456   } else {
457     result = SelectCode(LD);
458   }
459
460   return result;
461 }
462
463
464 SDNode *HexagonDAGToDAGISel::SelectIndexedStore(StoreSDNode *ST, SDLoc dl) {
465   SDValue Chain = ST->getChain();
466   SDValue Base = ST->getBasePtr();
467   SDValue Offset = ST->getOffset();
468   SDValue Value = ST->getValue();
469   SDNode *OffsetNode = Offset.getNode();
470   // Get the constant value.
471   int32_t Val = cast<ConstantSDNode>(OffsetNode)->getSExtValue();
472   EVT StoredVT = ST->getMemoryVT();
473   EVT ValueVT = Value.getValueType();
474
475   // Offset value must be within representable range
476   // and must have correct alignment properties.
477   const HexagonInstrInfo &TII = *HST.getInstrInfo();
478   if (TII.isValidAutoIncImm(StoredVT, Val)) {
479     unsigned Opcode = 0;
480
481     // Figure out the post inc version of opcode.
482     if (StoredVT == MVT::i64) Opcode = Hexagon::S2_storerd_pi;
483     else if (StoredVT == MVT::i32) Opcode = Hexagon::S2_storeri_pi;
484     else if (StoredVT == MVT::i16) Opcode = Hexagon::S2_storerh_pi;
485     else if (StoredVT == MVT::i8) Opcode = Hexagon::S2_storerb_pi;
486     else llvm_unreachable("unknown memory type");
487
488     if (ST->isTruncatingStore() && ValueVT.getSizeInBits() == 64) {
489       assert(StoredVT.getSizeInBits() < 64 && "Not a truncating store");
490       Value = CurDAG->getTargetExtractSubreg(Hexagon::subreg_loreg,
491                                              dl, MVT::i32, Value);
492     }
493     SDValue Ops[] = {Base, CurDAG->getTargetConstant(Val, MVT::i32), Value,
494                      Chain};
495     // Build post increment store.
496     SDNode* Result = CurDAG->getMachineNode(Opcode, dl, MVT::i32,
497                                             MVT::Other, Ops);
498     MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
499     MemOp[0] = ST->getMemOperand();
500     cast<MachineSDNode>(Result)->setMemRefs(MemOp, MemOp + 1);
501
502     ReplaceUses(ST, Result);
503     ReplaceUses(SDValue(ST,1), SDValue(Result,1));
504     return Result;
505   }
506
507   // Note: Order of operands matches the def of instruction:
508   // def S2_storerd_io
509   //   : STInst<(outs), (ins IntRegs:$base, imm:$offset, DoubleRegs:$src1), ...
510   // and it differs for POST_ST* for instance.
511   SDValue Ops[] = { Base, CurDAG->getTargetConstant(0, MVT::i32), Value,
512                     Chain};
513   unsigned Opcode = 0;
514
515   // Figure out the opcode.
516   if (StoredVT == MVT::i64) Opcode = Hexagon::S2_storerd_io;
517   else if (StoredVT == MVT::i32) Opcode = Hexagon::S2_storeri_io;
518   else if (StoredVT == MVT::i16) Opcode = Hexagon::S2_storerh_io;
519   else if (StoredVT == MVT::i8) Opcode = Hexagon::S2_storerb_io;
520   else llvm_unreachable("unknown memory type");
521
522   // Build regular store.
523   SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
524   SDNode* Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::Other, Ops);
525   // Build splitted incriment instruction.
526   SDNode* Result_2 = CurDAG->getMachineNode(Hexagon::A2_addi, dl, MVT::i32,
527                                             Base,
528                                             TargetConstVal,
529                                             SDValue(Result_1, 0));
530   MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
531   MemOp[0] = ST->getMemOperand();
532   cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
533
534   ReplaceUses(SDValue(ST,0), SDValue(Result_2,0));
535   ReplaceUses(SDValue(ST,1), SDValue(Result_1,0));
536   return Result_2;
537 }
538
539 SDNode *HexagonDAGToDAGISel::SelectStore(SDNode *N) {
540   SDLoc dl(N);
541   StoreSDNode *ST = cast<StoreSDNode>(N);
542   ISD::MemIndexedMode AM = ST->getAddressingMode();
543
544   // Handle indexed stores.
545   if (AM != ISD::UNINDEXED) {
546     return SelectIndexedStore(ST, dl);
547   }
548
549   return SelectCode(ST);
550 }
551
552 SDNode *HexagonDAGToDAGISel::SelectMul(SDNode *N) {
553   SDLoc dl(N);
554
555   //
556   // %conv.i = sext i32 %tmp1 to i64
557   // %conv2.i = sext i32 %add to i64
558   // %mul.i = mul nsw i64 %conv2.i, %conv.i
559   //
560   //   --- match with the following ---
561   //
562   // %mul.i = mpy (%tmp1, %add)
563   //
564
565   if (N->getValueType(0) == MVT::i64) {
566     // Shifting a i64 signed multiply.
567     SDValue MulOp0 = N->getOperand(0);
568     SDValue MulOp1 = N->getOperand(1);
569
570     SDValue OP0;
571     SDValue OP1;
572
573     // Handle sign_extend and sextload.
574     if (MulOp0.getOpcode() == ISD::SIGN_EXTEND) {
575       SDValue Sext0 = MulOp0.getOperand(0);
576       if (Sext0.getNode()->getValueType(0) != MVT::i32) {
577         return SelectCode(N);
578       }
579
580       OP0 = Sext0;
581     } else if (MulOp0.getOpcode() == ISD::LOAD) {
582       LoadSDNode *LD = cast<LoadSDNode>(MulOp0.getNode());
583       if (LD->getMemoryVT() != MVT::i32 ||
584           LD->getExtensionType() != ISD::SEXTLOAD ||
585           LD->getAddressingMode() != ISD::UNINDEXED) {
586         return SelectCode(N);
587       }
588
589       SDValue Chain = LD->getChain();
590       SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
591       OP0 = SDValue(CurDAG->getMachineNode(Hexagon::L2_loadri_io, dl, MVT::i32,
592                                             MVT::Other,
593                                             LD->getBasePtr(), TargetConst0,
594                                             Chain), 0);
595     } else {
596       return SelectCode(N);
597     }
598
599     // Same goes for the second operand.
600     if (MulOp1.getOpcode() == ISD::SIGN_EXTEND) {
601       SDValue Sext1 = MulOp1.getOperand(0);
602       if (Sext1.getNode()->getValueType(0) != MVT::i32) {
603         return SelectCode(N);
604       }
605
606       OP1 = Sext1;
607     } else if (MulOp1.getOpcode() == ISD::LOAD) {
608       LoadSDNode *LD = cast<LoadSDNode>(MulOp1.getNode());
609       if (LD->getMemoryVT() != MVT::i32 ||
610           LD->getExtensionType() != ISD::SEXTLOAD ||
611           LD->getAddressingMode() != ISD::UNINDEXED) {
612         return SelectCode(N);
613       }
614
615       SDValue Chain = LD->getChain();
616       SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
617       OP1 = SDValue(CurDAG->getMachineNode(Hexagon::L2_loadri_io, dl, MVT::i32,
618                                             MVT::Other,
619                                             LD->getBasePtr(), TargetConst0,
620                                             Chain), 0);
621     } else {
622       return SelectCode(N);
623     }
624
625     // Generate a mpy instruction.
626     SDNode *Result = CurDAG->getMachineNode(Hexagon::M2_dpmpyss_s0, dl, MVT::i64,
627                                             OP0, OP1);
628     ReplaceUses(N, Result);
629     return Result;
630   }
631
632   return SelectCode(N);
633 }
634
635 SDNode *HexagonDAGToDAGISel::SelectSHL(SDNode *N) {
636   SDLoc dl(N);
637   if (N->getValueType(0) == MVT::i32) {
638     SDValue Shl_0 = N->getOperand(0);
639     SDValue Shl_1 = N->getOperand(1);
640     // RHS is const.
641     if (Shl_1.getOpcode() == ISD::Constant) {
642       if (Shl_0.getOpcode() == ISD::MUL) {
643         SDValue Mul_0 = Shl_0.getOperand(0); // Val
644         SDValue Mul_1 = Shl_0.getOperand(1); // Const
645         // RHS of mul is const.
646         if (Mul_1.getOpcode() == ISD::Constant) {
647           int32_t ShlConst =
648             cast<ConstantSDNode>(Shl_1.getNode())->getSExtValue();
649           int32_t MulConst =
650             cast<ConstantSDNode>(Mul_1.getNode())->getSExtValue();
651           int32_t ValConst = MulConst << ShlConst;
652           SDValue Val = CurDAG->getTargetConstant(ValConst,
653                                                   MVT::i32);
654           if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Val.getNode()))
655             if (isInt<9>(CN->getSExtValue())) {
656               SDNode* Result =
657                 CurDAG->getMachineNode(Hexagon::M2_mpysmi, dl,
658                                        MVT::i32, Mul_0, Val);
659               ReplaceUses(N, Result);
660               return Result;
661             }
662
663         }
664       } else if (Shl_0.getOpcode() == ISD::SUB) {
665         SDValue Sub_0 = Shl_0.getOperand(0); // Const 0
666         SDValue Sub_1 = Shl_0.getOperand(1); // Val
667         if (Sub_0.getOpcode() == ISD::Constant) {
668           int32_t SubConst =
669             cast<ConstantSDNode>(Sub_0.getNode())->getSExtValue();
670           if (SubConst == 0) {
671             if (Sub_1.getOpcode() == ISD::SHL) {
672               SDValue Shl2_0 = Sub_1.getOperand(0); // Val
673               SDValue Shl2_1 = Sub_1.getOperand(1); // Const
674               if (Shl2_1.getOpcode() == ISD::Constant) {
675                 int32_t ShlConst =
676                   cast<ConstantSDNode>(Shl_1.getNode())->getSExtValue();
677                 int32_t Shl2Const =
678                   cast<ConstantSDNode>(Shl2_1.getNode())->getSExtValue();
679                 int32_t ValConst = 1 << (ShlConst+Shl2Const);
680                 SDValue Val = CurDAG->getTargetConstant(-ValConst, MVT::i32);
681                 if (ConstantSDNode *CN =
682                     dyn_cast<ConstantSDNode>(Val.getNode()))
683                   if (isInt<9>(CN->getSExtValue())) {
684                     SDNode* Result =
685                       CurDAG->getMachineNode(Hexagon::M2_mpysmi, dl, MVT::i32,
686                                              Shl2_0, Val);
687                     ReplaceUses(N, Result);
688                     return Result;
689                   }
690               }
691             }
692           }
693         }
694       }
695     }
696   }
697   return SelectCode(N);
698 }
699
700
701 //
702 // If there is an zero_extend followed an intrinsic in DAG (this means - the
703 // result of the intrinsic is predicate); convert the zero_extend to
704 // transfer instruction.
705 //
706 // Zero extend -> transfer is lowered here. Otherwise, zero_extend will be
707 // converted into a MUX as predicate registers defined as 1 bit in the
708 // compiler. Architecture defines them as 8-bit registers.
709 // We want to preserve all the lower 8-bits and, not just 1 LSB bit.
710 //
711 SDNode *HexagonDAGToDAGISel::SelectZeroExtend(SDNode *N) {
712   SDLoc dl(N);
713   SDNode *IsIntrinsic = N->getOperand(0).getNode();
714   if ((IsIntrinsic->getOpcode() == ISD::INTRINSIC_WO_CHAIN)) {
715     unsigned ID =
716       cast<ConstantSDNode>(IsIntrinsic->getOperand(0))->getZExtValue();
717     if (doesIntrinsicReturnPredicate(ID)) {
718       // Now we need to differentiate target data types.
719       if (N->getValueType(0) == MVT::i64) {
720         // Convert the zero_extend to Rs = Pd followed by A2_combinew(0,Rs).
721         SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
722         SDNode *Result_1 = CurDAG->getMachineNode(Hexagon::C2_tfrpr, dl,
723                                                   MVT::i32,
724                                                   SDValue(IsIntrinsic, 0));
725         SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A2_tfrsi, dl,
726                                                   MVT::i32,
727                                                   TargetConst0);
728         SDNode *Result_3 = CurDAG->getMachineNode(Hexagon::A2_combinew, dl,
729                                                   MVT::i64, MVT::Other,
730                                                   SDValue(Result_2, 0),
731                                                   SDValue(Result_1, 0));
732         ReplaceUses(N, Result_3);
733         return Result_3;
734       }
735       if (N->getValueType(0) == MVT::i32) {
736         // Convert the zero_extend to Rs = Pd
737         SDNode* RsPd = CurDAG->getMachineNode(Hexagon::C2_tfrpr, dl,
738                                               MVT::i32,
739                                               SDValue(IsIntrinsic, 0));
740         ReplaceUses(N, RsPd);
741         return RsPd;
742       }
743       llvm_unreachable("Unexpected value type");
744     }
745   }
746   return SelectCode(N);
747 }
748
749 //
750 // Checking for intrinsics which have predicate registers as operand(s)
751 // and lowering to the actual intrinsic.
752 //
753 SDNode *HexagonDAGToDAGISel::SelectIntrinsicWOChain(SDNode *N) {
754   unsigned IID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
755   unsigned Bits;
756   switch (IID) {
757   case Intrinsic::hexagon_S2_vsplatrb:
758     Bits = 8;
759     break;
760   case Intrinsic::hexagon_S2_vsplatrh:
761     Bits = 16;
762     break;
763   default:
764     return SelectCode(N);
765   }
766
767   SDValue const &V = N->getOperand(1);
768   SDValue U;
769   if (isValueExtension(V, Bits, U)) {
770     SDValue R = CurDAG->getNode(N->getOpcode(), SDLoc(N), N->getValueType(0),
771       N->getOperand(0), U);
772     return SelectCode(R.getNode());
773   }
774   return SelectCode(N);
775 }
776
777 //
778 // Map floating point constant values.
779 //
780 SDNode *HexagonDAGToDAGISel::SelectConstantFP(SDNode *N) {
781   SDLoc dl(N);
782   ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N);
783   APFloat APF = CN->getValueAPF();
784   if (N->getValueType(0) == MVT::f32) {
785     return CurDAG->getMachineNode(Hexagon::TFRI_f, dl, MVT::f32,
786               CurDAG->getTargetConstantFP(APF.convertToFloat(), MVT::f32));
787   }
788   else if (N->getValueType(0) == MVT::f64) {
789     return CurDAG->getMachineNode(Hexagon::CONST64_Float_Real, dl, MVT::f64,
790               CurDAG->getTargetConstantFP(APF.convertToDouble(), MVT::f64));
791   }
792
793   return SelectCode(N);
794 }
795
796 //
797 // Map predicate true (encoded as -1 in LLVM) to a XOR.
798 //
799 SDNode *HexagonDAGToDAGISel::SelectConstant(SDNode *N) {
800   SDLoc dl(N);
801   if (N->getValueType(0) == MVT::i1) {
802     SDNode* Result;
803     int32_t Val = cast<ConstantSDNode>(N)->getSExtValue();
804     if (Val == -1) {
805       // Create the IntReg = 1 node.
806       SDNode* IntRegTFR =
807         CurDAG->getMachineNode(Hexagon::A2_tfrsi, dl, MVT::i32,
808                                CurDAG->getTargetConstant(0, MVT::i32));
809
810       // Pd = IntReg
811       SDNode* Pd = CurDAG->getMachineNode(Hexagon::C2_tfrrp, dl, MVT::i1,
812                                           SDValue(IntRegTFR, 0));
813
814       // not(Pd)
815       SDNode* NotPd = CurDAG->getMachineNode(Hexagon::C2_not, dl, MVT::i1,
816                                              SDValue(Pd, 0));
817
818       // xor(not(Pd))
819       Result = CurDAG->getMachineNode(Hexagon::C2_xor, dl, MVT::i1,
820                                       SDValue(Pd, 0), SDValue(NotPd, 0));
821
822       // We have just built:
823       // Rs = Pd
824       // Pd = xor(not(Pd), Pd)
825
826       ReplaceUses(N, Result);
827       return Result;
828     }
829   }
830
831   return SelectCode(N);
832 }
833
834
835 //
836 // Map add followed by a asr -> asr +=.
837 //
838 SDNode *HexagonDAGToDAGISel::SelectAdd(SDNode *N) {
839   SDLoc dl(N);
840   if (N->getValueType(0) != MVT::i32) {
841     return SelectCode(N);
842   }
843   // Identify nodes of the form: add(asr(...)).
844   SDNode* Src1 = N->getOperand(0).getNode();
845   if (Src1->getOpcode() != ISD::SRA || !Src1->hasOneUse()
846       || Src1->getValueType(0) != MVT::i32) {
847     return SelectCode(N);
848   }
849
850   // Build Rd = Rd' + asr(Rs, Rt). The machine constraints will ensure that
851   // Rd and Rd' are assigned to the same register
852   SDNode* Result = CurDAG->getMachineNode(Hexagon::S2_asr_r_r_acc, dl, MVT::i32,
853                                           N->getOperand(1),
854                                           Src1->getOperand(0),
855                                           Src1->getOperand(1));
856   ReplaceUses(N, Result);
857
858   return Result;
859 }
860
861 SDNode *HexagonDAGToDAGISel::SelectFrameIndex(SDNode *N) {
862   int FX = cast<FrameIndexSDNode>(N)->getIndex();
863   SDValue FI = CurDAG->getTargetFrameIndex(FX, MVT::i32);
864   SDValue Zero = CurDAG->getTargetConstant(0, MVT::i32);
865   SDLoc DL(N);
866
867   SDNode *R = CurDAG->getMachineNode(Hexagon::TFR_FI, DL, MVT::i32, FI, Zero);
868
869   if (N->getHasDebugValue())
870     CurDAG->TransferDbgValues(SDValue(N, 0), SDValue(R, 0));
871   return R;
872 }
873
874
875 SDNode *HexagonDAGToDAGISel::Select(SDNode *N) {
876   if (N->isMachineOpcode()) {
877     N->setNodeId(-1);
878     return nullptr;   // Already selected.
879   }
880
881   switch (N->getOpcode()) {
882   case ISD::Constant:
883     return SelectConstant(N);
884
885   case ISD::ConstantFP:
886     return SelectConstantFP(N);
887
888   case ISD::FrameIndex:
889     return SelectFrameIndex(N);
890
891   case ISD::ADD:
892     return SelectAdd(N);
893
894   case ISD::SHL:
895     return SelectSHL(N);
896
897   case ISD::LOAD:
898     return SelectLoad(N);
899
900   case ISD::STORE:
901     return SelectStore(N);
902
903   case ISD::MUL:
904     return SelectMul(N);
905
906   case ISD::ZERO_EXTEND:
907     return SelectZeroExtend(N);
908
909   case ISD::INTRINSIC_WO_CHAIN:
910     return SelectIntrinsicWOChain(N);
911   }
912
913   return SelectCode(N);
914 }
915
916
917 bool HexagonDAGToDAGISel::
918 SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID,
919                              std::vector<SDValue> &OutOps) {
920   SDValue Inp = Op, Res;
921
922   switch (ConstraintID) {
923   default:
924     return true;
925   case InlineAsm::Constraint_i:
926   case InlineAsm::Constraint_o: // Offsetable.
927   case InlineAsm::Constraint_v: // Not offsetable.
928   case InlineAsm::Constraint_m: // Memory.
929     if (SelectAddrFI(Inp, Res))
930       OutOps.push_back(Res);
931     else
932       OutOps.push_back(Inp);
933     break;
934   }
935
936   OutOps.push_back(CurDAG->getTargetConstant(0, MVT::i32));
937   return false;
938 }
939
940 void HexagonDAGToDAGISel::PreprocessISelDAG() {
941   SelectionDAG &DAG = *CurDAG;
942   std::vector<SDNode*> Nodes;
943   for (auto I = DAG.allnodes_begin(), E = DAG.allnodes_end(); I != E; ++I)
944     Nodes.push_back(I);
945
946   // Simplify: (or (select c x 0) z)  ->  (select c (or x z) z)
947   //           (or (select c 0 y) z)  ->  (select c z (or y z))
948   // This may not be the right thing for all targets, so do it here.
949   for (auto I: Nodes) {
950     if (I->getOpcode() != ISD::OR)
951       continue;
952
953     auto IsZero = [] (const SDValue &V) -> bool {
954       if (ConstantSDNode *SC = dyn_cast<ConstantSDNode>(V.getNode()))
955         return SC->isNullValue();
956       return false;
957     };
958     auto IsSelect0 = [IsZero] (const SDValue &Op) -> bool {
959       if (Op.getOpcode() != ISD::SELECT)
960         return false;
961       return IsZero(Op.getOperand(1))  || IsZero(Op.getOperand(2));
962     };
963
964     SDValue N0 = I->getOperand(0), N1 = I->getOperand(1);
965     EVT VT = I->getValueType(0);
966     bool SelN0 = IsSelect0(N0);
967     SDValue SOp = SelN0 ? N0 : N1;
968     SDValue VOp = SelN0 ? N1 : N0;
969
970     if (SOp.getOpcode() == ISD::SELECT && SOp.getNode()->hasOneUse()) {
971       SDValue SC = SOp.getOperand(0);
972       SDValue SX = SOp.getOperand(1);
973       SDValue SY = SOp.getOperand(2);
974       SDLoc DLS = SOp;
975       if (IsZero(SY)) {
976         SDValue NewOr = DAG.getNode(ISD::OR, DLS, VT, SX, VOp);
977         SDValue NewSel = DAG.getNode(ISD::SELECT, DLS, VT, SC, NewOr, VOp);
978         DAG.ReplaceAllUsesWith(I, NewSel.getNode());
979       } else if (IsZero(SX)) {
980         SDValue NewOr = DAG.getNode(ISD::OR, DLS, VT, SY, VOp);
981         SDValue NewSel = DAG.getNode(ISD::SELECT, DLS, VT, SC, VOp, NewOr);
982         DAG.ReplaceAllUsesWith(I, NewSel.getNode());
983       }
984     }
985   }
986 }
987
988
989 bool HexagonDAGToDAGISel::SelectAddrFI(SDValue& N, SDValue &R) {
990   if (N.getOpcode() != ISD::FrameIndex)
991     return false;
992   FrameIndexSDNode *FX = cast<FrameIndexSDNode>(N);
993   R = CurDAG->getTargetFrameIndex(FX->getIndex(), MVT::i32);
994   return true;
995 }
996
997 inline bool HexagonDAGToDAGISel::SelectAddrGA(SDValue &N, SDValue &R) {
998   return SelectGlobalAddress(N, R, false);
999 }
1000
1001 inline bool HexagonDAGToDAGISel::SelectAddrGP(SDValue &N, SDValue &R) {
1002   return SelectGlobalAddress(N, R, true);
1003 }
1004
1005 bool HexagonDAGToDAGISel::SelectGlobalAddress(SDValue &N, SDValue &R,
1006                                               bool UseGP) {
1007   switch (N.getOpcode()) {
1008   case ISD::ADD: {
1009     SDValue N0 = N.getOperand(0);
1010     SDValue N1 = N.getOperand(1);
1011     unsigned GAOpc = N0.getOpcode();
1012     if (UseGP && GAOpc != HexagonISD::CONST32_GP)
1013       return false;
1014     if (!UseGP && GAOpc != HexagonISD::CONST32)
1015       return false;
1016     if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N1)) {
1017       SDValue Addr = N0.getOperand(0);
1018       if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Addr)) {
1019         if (GA->getOpcode() == ISD::TargetGlobalAddress) {
1020           uint64_t NewOff = GA->getOffset() + (uint64_t)Const->getSExtValue();
1021           R = CurDAG->getTargetGlobalAddress(GA->getGlobal(), SDLoc(Const),
1022                                              N.getValueType(), NewOff);
1023           return true;
1024         }
1025       }
1026     }
1027     break;
1028   }
1029   case HexagonISD::CONST32:
1030     // The operand(0) of CONST32 is TargetGlobalAddress, which is what we
1031     // want in the instruction.
1032     if (!UseGP)
1033       R = N.getOperand(0);
1034     return !UseGP;
1035   case HexagonISD::CONST32_GP:
1036     if (UseGP)
1037       R = N.getOperand(0);
1038     return UseGP;
1039   default:
1040     return false;
1041   }
1042
1043   return false;
1044 }
1045
1046 bool HexagonDAGToDAGISel::isValueExtension(const SDValue &Val,
1047       unsigned FromBits, SDValue &Src) {
1048   unsigned Opc = Val.getOpcode();
1049   switch (Opc) {
1050   case ISD::SIGN_EXTEND:
1051   case ISD::ZERO_EXTEND:
1052   case ISD::ANY_EXTEND: {
1053     SDValue const &Op0 = Val.getOperand(0);
1054     EVT T = Op0.getValueType();
1055     if (T.isInteger() && T.getSizeInBits() == FromBits) {
1056       Src = Op0;
1057       return true;
1058     }
1059     break;
1060   }
1061   case ISD::SIGN_EXTEND_INREG:
1062   case ISD::AssertSext:
1063   case ISD::AssertZext:
1064     if (Val.getOperand(0).getValueType().isInteger()) {
1065       VTSDNode *T = cast<VTSDNode>(Val.getOperand(1));
1066       if (T->getVT().getSizeInBits() == FromBits) {
1067         Src = Val.getOperand(0);
1068         return true;
1069       }
1070     }
1071     break;
1072   case ISD::AND: {
1073     // Check if this is an AND with "FromBits" of lower bits set to 1.
1074     uint64_t FromMask = (1 << FromBits) - 1;
1075     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(0))) {
1076       if (C->getZExtValue() == FromMask) {
1077         Src = Val.getOperand(1);
1078         return true;
1079       }
1080     }
1081     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(1))) {
1082       if (C->getZExtValue() == FromMask) {
1083         Src = Val.getOperand(0);
1084         return true;
1085       }
1086     }
1087     break;
1088   }
1089   case ISD::OR:
1090   case ISD::XOR: {
1091     // OR/XOR with the lower "FromBits" bits set to 0.
1092     uint64_t FromMask = (1 << FromBits) - 1;
1093     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(0))) {
1094       if ((C->getZExtValue() & FromMask) == 0) {
1095         Src = Val.getOperand(1);
1096         return true;
1097       }
1098     }
1099     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(1))) {
1100       if ((C->getZExtValue() & FromMask) == 0) {
1101         Src = Val.getOperand(0);
1102         return true;
1103       }
1104     }
1105   }
1106   default:
1107     break;
1108   }
1109   return false;
1110 }