LLVM  mainline
HexagonISelDAGToDAG.cpp
Go to the documentation of this file.
00001 //===-- HexagonISelDAGToDAG.cpp - A dag to dag inst selector for Hexagon --===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines an instruction selector for the Hexagon target.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "Hexagon.h"
00015 #include "HexagonISelLowering.h"
00016 #include "HexagonMachineFunctionInfo.h"
00017 #include "HexagonTargetMachine.h"
00018 #include "llvm/ADT/DenseMap.h"
00019 #include "llvm/CodeGen/FunctionLoweringInfo.h"
00020 #include "llvm/CodeGen/MachineInstrBuilder.h"
00021 #include "llvm/CodeGen/SelectionDAGISel.h"
00022 #include "llvm/IR/Intrinsics.h"
00023 #include "llvm/Support/CommandLine.h"
00024 #include "llvm/Support/Compiler.h"
00025 #include "llvm/Support/Debug.h"
00026 using namespace llvm;
00027 
00028 #define DEBUG_TYPE "hexagon-isel"
00029 
00030 static
00031 cl::opt<unsigned>
00032 MaxNumOfUsesForConstExtenders("ga-max-num-uses-for-constant-extenders",
00033   cl::Hidden, cl::init(2),
00034   cl::desc("Maximum number of uses of a global address such that we still us a"
00035            "constant extended instruction"));
00036 
00037 //===----------------------------------------------------------------------===//
00038 // Instruction Selector Implementation
00039 //===----------------------------------------------------------------------===//
00040 
00041 namespace llvm {
00042   void initializeHexagonDAGToDAGISelPass(PassRegistry&);
00043 }
00044 
00045 //===--------------------------------------------------------------------===//
00046 /// HexagonDAGToDAGISel - Hexagon specific code to select Hexagon machine
00047 /// instructions for SelectionDAG operations.
00048 ///
00049 namespace {
00050 class HexagonDAGToDAGISel : public SelectionDAGISel {
00051   const HexagonTargetMachine& HTM;
00052   const HexagonSubtarget *HST;
00053 public:
00054   explicit HexagonDAGToDAGISel(HexagonTargetMachine &tm,
00055                                CodeGenOpt::Level OptLevel)
00056       : SelectionDAGISel(tm, OptLevel), HTM(tm) {
00057     initializeHexagonDAGToDAGISelPass(*PassRegistry::getPassRegistry());
00058   }
00059 
00060   bool runOnMachineFunction(MachineFunction &MF) override {
00061     // Reset the subtarget each time through.
00062     HST = &MF.getSubtarget<HexagonSubtarget>();
00063     SelectionDAGISel::runOnMachineFunction(MF);
00064     return true;
00065   }
00066 
00067   virtual void PreprocessISelDAG() override;
00068   virtual void EmitFunctionEntryCode() override;
00069 
00070   SDNode *Select(SDNode *N) override;
00071 
00072   // Complex Pattern Selectors.
00073   inline bool SelectAddrGA(SDValue &N, SDValue &R);
00074   inline bool SelectAddrGP(SDValue &N, SDValue &R);
00075   bool SelectGlobalAddress(SDValue &N, SDValue &R, bool UseGP);
00076   bool SelectAddrFI(SDValue &N, SDValue &R);
00077 
00078   const char *getPassName() const override {
00079     return "Hexagon DAG->DAG Pattern Instruction Selection";
00080   }
00081 
00082   SDNode *SelectFrameIndex(SDNode *N);
00083   /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
00084   /// inline asm expressions.
00085   bool SelectInlineAsmMemoryOperand(const SDValue &Op,
00086                                     unsigned ConstraintID,
00087                                     std::vector<SDValue> &OutOps) override;
00088   SDNode *SelectLoad(SDNode *N);
00089   SDNode *SelectBaseOffsetLoad(LoadSDNode *LD, SDLoc dl);
00090   SDNode *SelectIndexedLoad(LoadSDNode *LD, SDLoc dl);
00091   SDNode *SelectIndexedLoadZeroExtend64(LoadSDNode *LD, unsigned Opcode,
00092                                         SDLoc dl);
00093   SDNode *SelectIndexedLoadSignExtend64(LoadSDNode *LD, unsigned Opcode,
00094                                         SDLoc dl);
00095   SDNode *SelectBaseOffsetStore(StoreSDNode *ST, SDLoc dl);
00096   SDNode *SelectIndexedStore(StoreSDNode *ST, SDLoc dl);
00097   SDNode *SelectStore(SDNode *N);
00098   SDNode *SelectSHL(SDNode *N);
00099   SDNode *SelectMul(SDNode *N);
00100   SDNode *SelectZeroExtend(SDNode *N);
00101   SDNode *SelectIntrinsicWChain(SDNode *N);
00102   SDNode *SelectIntrinsicWOChain(SDNode *N);
00103   SDNode *SelectConstant(SDNode *N);
00104   SDNode *SelectConstantFP(SDNode *N);
00105   SDNode *SelectAdd(SDNode *N);
00106   SDNode *SelectBitOp(SDNode *N);
00107 
00108   // XformMskToBitPosU5Imm - Returns the bit position which
00109   // the single bit 32 bit mask represents.
00110   // Used in Clr and Set bit immediate memops.
00111   SDValue XformMskToBitPosU5Imm(uint32_t Imm) {
00112     int32_t bitPos;
00113     bitPos = Log2_32(Imm);
00114     assert(bitPos >= 0 && bitPos < 32 &&
00115            "Constant out of range for 32 BitPos Memops");
00116     return CurDAG->getTargetConstant(bitPos, MVT::i32);
00117   }
00118 
00119   // XformMskToBitPosU4Imm - Returns the bit position which the single-bit
00120   // 16 bit mask represents. Used in Clr and Set bit immediate memops.
00121   SDValue XformMskToBitPosU4Imm(uint16_t Imm) {
00122     return XformMskToBitPosU5Imm(Imm);
00123   }
00124 
00125   // XformMskToBitPosU3Imm - Returns the bit position which the single-bit
00126   // 8 bit mask represents. Used in Clr and Set bit immediate memops.
00127   SDValue XformMskToBitPosU3Imm(uint8_t Imm) {
00128     return XformMskToBitPosU5Imm(Imm);
00129   }
00130 
00131   // Return true if there is exactly one bit set in V, i.e., if V is one of the
00132   // following integers: 2^0, 2^1, ..., 2^31.
00133   bool ImmIsSingleBit(uint32_t v) const {
00134     return isPowerOf2_32(v);
00135   }
00136 
00137   // XformM5ToU5Imm - Return a target constant with the specified value, of
00138   // type i32 where the negative literal is transformed into a positive literal
00139   // for use in -= memops.
00140   inline SDValue XformM5ToU5Imm(signed Imm) {
00141      assert( (Imm >= -31 && Imm <= -1)  && "Constant out of range for Memops");
00142      return CurDAG->getTargetConstant( - Imm, MVT::i32);
00143   }
00144 
00145   // XformU7ToU7M1Imm - Return a target constant decremented by 1, in range
00146   // [1..128], used in cmpb.gtu instructions.
00147   inline SDValue XformU7ToU7M1Imm(signed Imm) {
00148     assert((Imm >= 1 && Imm <= 128) && "Constant out of range for cmpb op");
00149     return CurDAG->getTargetConstant(Imm - 1, MVT::i8);
00150   }
00151 
00152   // XformS8ToS8M1Imm - Return a target constant decremented by 1.
00153   inline SDValue XformSToSM1Imm(signed Imm) {
00154     return CurDAG->getTargetConstant(Imm - 1, MVT::i32);
00155   }
00156 
00157   // XformU8ToU8M1Imm - Return a target constant decremented by 1.
00158   inline SDValue XformUToUM1Imm(unsigned Imm) {
00159     assert((Imm >= 1) && "Cannot decrement unsigned int less than 1");
00160     return CurDAG->getTargetConstant(Imm - 1, MVT::i32);
00161   }
00162 
00163   // XformSToSM2Imm - Return a target constant decremented by 2.
00164   inline SDValue XformSToSM2Imm(unsigned Imm) {
00165     return CurDAG->getTargetConstant(Imm - 2, MVT::i32);
00166   }
00167 
00168   // XformSToSM3Imm - Return a target constant decremented by 3.
00169   inline SDValue XformSToSM3Imm(unsigned Imm) {
00170     return CurDAG->getTargetConstant(Imm - 3, MVT::i32);
00171   }
00172 
00173   // Include the pieces autogenerated from the target description.
00174   #include "HexagonGenDAGISel.inc"
00175 
00176 private:
00177   bool isValueExtension(const SDValue &Val, unsigned FromBits, SDValue &Src);
00178 }; // end HexagonDAGToDAGISel
00179 }  // end anonymous namespace
00180 
00181 
00182 /// createHexagonISelDag - This pass converts a legalized DAG into a
00183 /// Hexagon-specific DAG, ready for instruction scheduling.
00184 ///
00185 namespace llvm {
00186 FunctionPass *createHexagonISelDag(HexagonTargetMachine &TM,
00187                                    CodeGenOpt::Level OptLevel) {
00188   return new HexagonDAGToDAGISel(TM, OptLevel);
00189 }
00190 }
00191 
00192 static void initializePassOnce(PassRegistry &Registry) {
00193   const char *Name = "Hexagon DAG->DAG Pattern Instruction Selection";
00194   PassInfo *PI = new PassInfo(Name, "hexagon-isel",
00195                               &SelectionDAGISel::ID, nullptr, false, false);
00196   Registry.registerPass(*PI, true);
00197 }
00198 
00199 void llvm::initializeHexagonDAGToDAGISelPass(PassRegistry &Registry) {
00200   CALL_ONCE_INITIALIZATION(initializePassOnce)
00201 }
00202 
00203 
00204 // Intrinsics that return a a predicate.
00205 static unsigned doesIntrinsicReturnPredicate(unsigned ID)
00206 {
00207   switch (ID) {
00208     default:
00209       return 0;
00210     case Intrinsic::hexagon_C2_cmpeq:
00211     case Intrinsic::hexagon_C2_cmpgt:
00212     case Intrinsic::hexagon_C2_cmpgtu:
00213     case Intrinsic::hexagon_C2_cmpgtup:
00214     case Intrinsic::hexagon_C2_cmpgtp:
00215     case Intrinsic::hexagon_C2_cmpeqp:
00216     case Intrinsic::hexagon_C2_bitsset:
00217     case Intrinsic::hexagon_C2_bitsclr:
00218     case Intrinsic::hexagon_C2_cmpeqi:
00219     case Intrinsic::hexagon_C2_cmpgti:
00220     case Intrinsic::hexagon_C2_cmpgtui:
00221     case Intrinsic::hexagon_C2_cmpgei:
00222     case Intrinsic::hexagon_C2_cmpgeui:
00223     case Intrinsic::hexagon_C2_cmplt:
00224     case Intrinsic::hexagon_C2_cmpltu:
00225     case Intrinsic::hexagon_C2_bitsclri:
00226     case Intrinsic::hexagon_C2_and:
00227     case Intrinsic::hexagon_C2_or:
00228     case Intrinsic::hexagon_C2_xor:
00229     case Intrinsic::hexagon_C2_andn:
00230     case Intrinsic::hexagon_C2_not:
00231     case Intrinsic::hexagon_C2_orn:
00232     case Intrinsic::hexagon_C2_pxfer_map:
00233     case Intrinsic::hexagon_C2_any8:
00234     case Intrinsic::hexagon_C2_all8:
00235     case Intrinsic::hexagon_A2_vcmpbeq:
00236     case Intrinsic::hexagon_A2_vcmpbgtu:
00237     case Intrinsic::hexagon_A2_vcmpheq:
00238     case Intrinsic::hexagon_A2_vcmphgt:
00239     case Intrinsic::hexagon_A2_vcmphgtu:
00240     case Intrinsic::hexagon_A2_vcmpweq:
00241     case Intrinsic::hexagon_A2_vcmpwgt:
00242     case Intrinsic::hexagon_A2_vcmpwgtu:
00243     case Intrinsic::hexagon_C2_tfrrp:
00244     case Intrinsic::hexagon_S2_tstbit_i:
00245     case Intrinsic::hexagon_S2_tstbit_r:
00246       return 1;
00247   }
00248 }
00249 
00250 SDNode *HexagonDAGToDAGISel::SelectIndexedLoadSignExtend64(LoadSDNode *LD,
00251                                                            unsigned Opcode,
00252                                                            SDLoc dl) {
00253   SDValue Chain = LD->getChain();
00254   EVT LoadedVT = LD->getMemoryVT();
00255   SDValue Base = LD->getBasePtr();
00256   SDValue Offset = LD->getOffset();
00257   SDNode *OffsetNode = Offset.getNode();
00258   int32_t Val = cast<ConstantSDNode>(OffsetNode)->getSExtValue();
00259 
00260   const HexagonInstrInfo &TII = *HST->getInstrInfo();
00261   if (TII.isValidAutoIncImm(LoadedVT, Val)) {
00262     SDValue TargetConst = CurDAG->getTargetConstant(Val, MVT::i32);
00263     SDNode *Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::i32, MVT::i32,
00264                                               MVT::Other, Base, TargetConst,
00265                                               Chain);
00266     SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A2_sxtw, dl, MVT::i64,
00267                                               SDValue(Result_1, 0));
00268     MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
00269     MemOp[0] = LD->getMemOperand();
00270     cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
00271     const SDValue Froms[] = { SDValue(LD, 0),
00272                               SDValue(LD, 1),
00273                               SDValue(LD, 2) };
00274     const SDValue Tos[]   = { SDValue(Result_2, 0),
00275                               SDValue(Result_1, 1),
00276                               SDValue(Result_1, 2) };
00277     ReplaceUses(Froms, Tos, 3);
00278     return Result_2;
00279   }
00280 
00281   SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
00282   SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
00283   SDNode *Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::i32, MVT::Other,
00284                                             Base, TargetConst0, Chain);
00285   SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A2_sxtw, dl, MVT::i64,
00286                                             SDValue(Result_1, 0));
00287   SDNode* Result_3 = CurDAG->getMachineNode(Hexagon::A2_addi, dl, MVT::i32,
00288                                             Base, TargetConstVal,
00289                                             SDValue(Result_1, 1));
00290   MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
00291   MemOp[0] = LD->getMemOperand();
00292   cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
00293   const SDValue Froms[] = { SDValue(LD, 0),
00294                             SDValue(LD, 1),
00295                             SDValue(LD, 2) };
00296   const SDValue Tos[]   = { SDValue(Result_2, 0),
00297                             SDValue(Result_3, 0),
00298                             SDValue(Result_1, 1) };
00299   ReplaceUses(Froms, Tos, 3);
00300   return Result_2;
00301 }
00302 
00303 
00304 SDNode *HexagonDAGToDAGISel::SelectIndexedLoadZeroExtend64(LoadSDNode *LD,
00305                                                            unsigned Opcode,
00306                                                            SDLoc dl) {
00307   SDValue Chain = LD->getChain();
00308   EVT LoadedVT = LD->getMemoryVT();
00309   SDValue Base = LD->getBasePtr();
00310   SDValue Offset = LD->getOffset();
00311   SDNode *OffsetNode = Offset.getNode();
00312   int32_t Val = cast<ConstantSDNode>(OffsetNode)->getSExtValue();
00313 
00314   const HexagonInstrInfo &TII = *HST->getInstrInfo();
00315   if (TII.isValidAutoIncImm(LoadedVT, Val)) {
00316     SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
00317     SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
00318     SDNode *Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::i32,
00319                                               MVT::i32, MVT::Other, Base,
00320                                               TargetConstVal, Chain);
00321     SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A4_combineir, dl,
00322                                               MVT::i64, MVT::Other,
00323                                               TargetConst0,
00324                                               SDValue(Result_1,0));
00325     MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
00326     MemOp[0] = LD->getMemOperand();
00327     cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
00328     const SDValue Froms[] = { SDValue(LD, 0),
00329                               SDValue(LD, 1),
00330                               SDValue(LD, 2) };
00331     const SDValue Tos[]   = { SDValue(Result_2, 0),
00332                               SDValue(Result_1, 1),
00333                               SDValue(Result_1, 2) };
00334     ReplaceUses(Froms, Tos, 3);
00335     return Result_2;
00336   }
00337 
00338   // Generate an indirect load.
00339   SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
00340   SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
00341   SDNode *Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::i32,
00342                                             MVT::Other, Base, TargetConst0,
00343                                             Chain);
00344   SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A4_combineir, dl,
00345                                             MVT::i64, MVT::Other,
00346                                             TargetConst0,
00347                                             SDValue(Result_1,0));
00348   // Add offset to base.
00349   SDNode* Result_3 = CurDAG->getMachineNode(Hexagon::A2_addi, dl, MVT::i32,
00350                                             Base, TargetConstVal,
00351                                             SDValue(Result_1, 1));
00352   MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
00353   MemOp[0] = LD->getMemOperand();
00354   cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
00355   const SDValue Froms[] = { SDValue(LD, 0),
00356                             SDValue(LD, 1),
00357                             SDValue(LD, 2) };
00358   const SDValue Tos[]   = { SDValue(Result_2, 0), // Load value.
00359                             SDValue(Result_3, 0), // New address.
00360                             SDValue(Result_1, 1) };
00361   ReplaceUses(Froms, Tos, 3);
00362   return Result_2;
00363 }
00364 
00365 
00366 SDNode *HexagonDAGToDAGISel::SelectIndexedLoad(LoadSDNode *LD, SDLoc dl) {
00367   SDValue Chain = LD->getChain();
00368   SDValue Base = LD->getBasePtr();
00369   SDValue Offset = LD->getOffset();
00370   SDNode *OffsetNode = Offset.getNode();
00371   // Get the constant value.
00372   int32_t Val = cast<ConstantSDNode>(OffsetNode)->getSExtValue();
00373   EVT LoadedVT = LD->getMemoryVT();
00374   unsigned Opcode = 0;
00375 
00376   // Check for zero extended loads. Treat any-extend loads as zero extended
00377   // loads.
00378   ISD::LoadExtType ExtType = LD->getExtensionType();
00379   bool IsZeroExt = (ExtType == ISD::ZEXTLOAD || ExtType == ISD::EXTLOAD);
00380 
00381   // Figure out the opcode.
00382   const HexagonInstrInfo &TII = *HST->getInstrInfo();
00383   if (LoadedVT == MVT::i64) {
00384     if (TII.isValidAutoIncImm(LoadedVT, Val))
00385       Opcode = Hexagon::L2_loadrd_pi;
00386     else
00387       Opcode = Hexagon::L2_loadrd_io;
00388   } else if (LoadedVT == MVT::i32) {
00389     if (TII.isValidAutoIncImm(LoadedVT, Val))
00390       Opcode = Hexagon::L2_loadri_pi;
00391     else
00392       Opcode = Hexagon::L2_loadri_io;
00393   } else if (LoadedVT == MVT::i16) {
00394     if (TII.isValidAutoIncImm(LoadedVT, Val))
00395       Opcode = IsZeroExt ? Hexagon::L2_loadruh_pi : Hexagon::L2_loadrh_pi;
00396     else
00397       Opcode = IsZeroExt ? Hexagon::L2_loadruh_io : Hexagon::L2_loadrh_io;
00398   } else if (LoadedVT == MVT::i8) {
00399     if (TII.isValidAutoIncImm(LoadedVT, Val))
00400       Opcode = IsZeroExt ? Hexagon::L2_loadrub_pi : Hexagon::L2_loadrb_pi;
00401     else
00402       Opcode = IsZeroExt ? Hexagon::L2_loadrub_io : Hexagon::L2_loadrb_io;
00403   } else
00404     llvm_unreachable("unknown memory type");
00405 
00406   // For zero extended i64 loads, we need to add combine instructions.
00407   if (LD->getValueType(0) == MVT::i64 && IsZeroExt)
00408     return SelectIndexedLoadZeroExtend64(LD, Opcode, dl);
00409   // Handle sign extended i64 loads.
00410   if (LD->getValueType(0) == MVT::i64 && ExtType == ISD::SEXTLOAD)
00411     return SelectIndexedLoadSignExtend64(LD, Opcode, dl);
00412 
00413   if (TII.isValidAutoIncImm(LoadedVT, Val)) {
00414     SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
00415     SDNode* Result = CurDAG->getMachineNode(Opcode, dl,
00416                                             LD->getValueType(0),
00417                                             MVT::i32, MVT::Other, Base,
00418                                             TargetConstVal, Chain);
00419     MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
00420     MemOp[0] = LD->getMemOperand();
00421     cast<MachineSDNode>(Result)->setMemRefs(MemOp, MemOp + 1);
00422     const SDValue Froms[] = { SDValue(LD, 0),
00423                               SDValue(LD, 1),
00424                               SDValue(LD, 2)
00425     };
00426     const SDValue Tos[]   = { SDValue(Result, 0),
00427                               SDValue(Result, 1),
00428                               SDValue(Result, 2)
00429     };
00430     ReplaceUses(Froms, Tos, 3);
00431     return Result;
00432   } else {
00433     SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
00434     SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
00435     SDNode* Result_1 = CurDAG->getMachineNode(Opcode, dl,
00436                                               LD->getValueType(0),
00437                                               MVT::Other, Base, TargetConst0,
00438                                               Chain);
00439     SDNode* Result_2 = CurDAG->getMachineNode(Hexagon::A2_addi, dl, MVT::i32,
00440                                               Base, TargetConstVal,
00441                                               SDValue(Result_1, 1));
00442     MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
00443     MemOp[0] = LD->getMemOperand();
00444     cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
00445     const SDValue Froms[] = { SDValue(LD, 0),
00446                               SDValue(LD, 1),
00447                               SDValue(LD, 2)
00448     };
00449     const SDValue Tos[]   = { SDValue(Result_1, 0),
00450                               SDValue(Result_2, 0),
00451                               SDValue(Result_1, 1)
00452     };
00453     ReplaceUses(Froms, Tos, 3);
00454     return Result_1;
00455   }
00456 }
00457 
00458 
00459 SDNode *HexagonDAGToDAGISel::SelectLoad(SDNode *N) {
00460   SDNode *result;
00461   SDLoc dl(N);
00462   LoadSDNode *LD = cast<LoadSDNode>(N);
00463   ISD::MemIndexedMode AM = LD->getAddressingMode();
00464 
00465   // Handle indexed loads.
00466   if (AM != ISD::UNINDEXED) {
00467     result = SelectIndexedLoad(LD, dl);
00468   } else {
00469     result = SelectCode(LD);
00470   }
00471 
00472   return result;
00473 }
00474 
00475 
00476 SDNode *HexagonDAGToDAGISel::SelectIndexedStore(StoreSDNode *ST, SDLoc dl) {
00477   SDValue Chain = ST->getChain();
00478   SDValue Base = ST->getBasePtr();
00479   SDValue Offset = ST->getOffset();
00480   SDValue Value = ST->getValue();
00481   SDNode *OffsetNode = Offset.getNode();
00482   // Get the constant value.
00483   int32_t Val = cast<ConstantSDNode>(OffsetNode)->getSExtValue();
00484   EVT StoredVT = ST->getMemoryVT();
00485   EVT ValueVT = Value.getValueType();
00486 
00487   // Offset value must be within representable range
00488   // and must have correct alignment properties.
00489   const HexagonInstrInfo &TII = *HST->getInstrInfo();
00490   if (TII.isValidAutoIncImm(StoredVT, Val)) {
00491     unsigned Opcode = 0;
00492 
00493     // Figure out the post inc version of opcode.
00494     if (StoredVT == MVT::i64) Opcode = Hexagon::S2_storerd_pi;
00495     else if (StoredVT == MVT::i32) Opcode = Hexagon::S2_storeri_pi;
00496     else if (StoredVT == MVT::i16) Opcode = Hexagon::S2_storerh_pi;
00497     else if (StoredVT == MVT::i8) Opcode = Hexagon::S2_storerb_pi;
00498     else llvm_unreachable("unknown memory type");
00499 
00500     if (ST->isTruncatingStore() && ValueVT.getSizeInBits() == 64) {
00501       assert(StoredVT.getSizeInBits() < 64 && "Not a truncating store");
00502       Value = CurDAG->getTargetExtractSubreg(Hexagon::subreg_loreg,
00503                                              dl, MVT::i32, Value);
00504     }
00505     SDValue Ops[] = {Base, CurDAG->getTargetConstant(Val, MVT::i32), Value,
00506                      Chain};
00507     // Build post increment store.
00508     SDNode* Result = CurDAG->getMachineNode(Opcode, dl, MVT::i32,
00509                                             MVT::Other, Ops);
00510     MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
00511     MemOp[0] = ST->getMemOperand();
00512     cast<MachineSDNode>(Result)->setMemRefs(MemOp, MemOp + 1);
00513 
00514     ReplaceUses(ST, Result);
00515     ReplaceUses(SDValue(ST,1), SDValue(Result,1));
00516     return Result;
00517   }
00518 
00519   // Note: Order of operands matches the def of instruction:
00520   // def S2_storerd_io
00521   //   : STInst<(outs), (ins IntRegs:$base, imm:$offset, DoubleRegs:$src1), ...
00522   // and it differs for POST_ST* for instance.
00523   SDValue Ops[] = { Base, CurDAG->getTargetConstant(0, MVT::i32), Value,
00524                     Chain};
00525   unsigned Opcode = 0;
00526 
00527   // Figure out the opcode.
00528   if (StoredVT == MVT::i64) Opcode = Hexagon::S2_storerd_io;
00529   else if (StoredVT == MVT::i32) Opcode = Hexagon::S2_storeri_io;
00530   else if (StoredVT == MVT::i16) Opcode = Hexagon::S2_storerh_io;
00531   else if (StoredVT == MVT::i8) Opcode = Hexagon::S2_storerb_io;
00532   else llvm_unreachable("unknown memory type");
00533 
00534   // Build regular store.
00535   SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
00536   SDNode* Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::Other, Ops);
00537   // Build splitted incriment instruction.
00538   SDNode* Result_2 = CurDAG->getMachineNode(Hexagon::A2_addi, dl, MVT::i32,
00539                                             Base,
00540                                             TargetConstVal,
00541                                             SDValue(Result_1, 0));
00542   MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
00543   MemOp[0] = ST->getMemOperand();
00544   cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
00545 
00546   ReplaceUses(SDValue(ST,0), SDValue(Result_2,0));
00547   ReplaceUses(SDValue(ST,1), SDValue(Result_1,0));
00548   return Result_2;
00549 }
00550 
00551 SDNode *HexagonDAGToDAGISel::SelectStore(SDNode *N) {
00552   SDLoc dl(N);
00553   StoreSDNode *ST = cast<StoreSDNode>(N);
00554   ISD::MemIndexedMode AM = ST->getAddressingMode();
00555 
00556   // Handle indexed stores.
00557   if (AM != ISD::UNINDEXED) {
00558     return SelectIndexedStore(ST, dl);
00559   }
00560 
00561   return SelectCode(ST);
00562 }
00563 
00564 SDNode *HexagonDAGToDAGISel::SelectMul(SDNode *N) {
00565   SDLoc dl(N);
00566 
00567   //
00568   // %conv.i = sext i32 %tmp1 to i64
00569   // %conv2.i = sext i32 %add to i64
00570   // %mul.i = mul nsw i64 %conv2.i, %conv.i
00571   //
00572   //   --- match with the following ---
00573   //
00574   // %mul.i = mpy (%tmp1, %add)
00575   //
00576 
00577   if (N->getValueType(0) == MVT::i64) {
00578     // Shifting a i64 signed multiply.
00579     SDValue MulOp0 = N->getOperand(0);
00580     SDValue MulOp1 = N->getOperand(1);
00581 
00582     SDValue OP0;
00583     SDValue OP1;
00584 
00585     // Handle sign_extend and sextload.
00586     if (MulOp0.getOpcode() == ISD::SIGN_EXTEND) {
00587       SDValue Sext0 = MulOp0.getOperand(0);
00588       if (Sext0.getNode()->getValueType(0) != MVT::i32) {
00589         return SelectCode(N);
00590       }
00591 
00592       OP0 = Sext0;
00593     } else if (MulOp0.getOpcode() == ISD::LOAD) {
00594       LoadSDNode *LD = cast<LoadSDNode>(MulOp0.getNode());
00595       if (LD->getMemoryVT() != MVT::i32 ||
00596           LD->getExtensionType() != ISD::SEXTLOAD ||
00597           LD->getAddressingMode() != ISD::UNINDEXED) {
00598         return SelectCode(N);
00599       }
00600 
00601       SDValue Chain = LD->getChain();
00602       SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
00603       OP0 = SDValue(CurDAG->getMachineNode(Hexagon::L2_loadri_io, dl, MVT::i32,
00604                                             MVT::Other,
00605                                             LD->getBasePtr(), TargetConst0,
00606                                             Chain), 0);
00607     } else {
00608       return SelectCode(N);
00609     }
00610 
00611     // Same goes for the second operand.
00612     if (MulOp1.getOpcode() == ISD::SIGN_EXTEND) {
00613       SDValue Sext1 = MulOp1.getOperand(0);
00614       if (Sext1.getNode()->getValueType(0) != MVT::i32) {
00615         return SelectCode(N);
00616       }
00617 
00618       OP1 = Sext1;
00619     } else if (MulOp1.getOpcode() == ISD::LOAD) {
00620       LoadSDNode *LD = cast<LoadSDNode>(MulOp1.getNode());
00621       if (LD->getMemoryVT() != MVT::i32 ||
00622           LD->getExtensionType() != ISD::SEXTLOAD ||
00623           LD->getAddressingMode() != ISD::UNINDEXED) {
00624         return SelectCode(N);
00625       }
00626 
00627       SDValue Chain = LD->getChain();
00628       SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
00629       OP1 = SDValue(CurDAG->getMachineNode(Hexagon::L2_loadri_io, dl, MVT::i32,
00630                                             MVT::Other,
00631                                             LD->getBasePtr(), TargetConst0,
00632                                             Chain), 0);
00633     } else {
00634       return SelectCode(N);
00635     }
00636 
00637     // Generate a mpy instruction.
00638     SDNode *Result = CurDAG->getMachineNode(Hexagon::M2_dpmpyss_s0, dl, MVT::i64,
00639                                             OP0, OP1);
00640     ReplaceUses(N, Result);
00641     return Result;
00642   }
00643 
00644   return SelectCode(N);
00645 }
00646 
00647 SDNode *HexagonDAGToDAGISel::SelectSHL(SDNode *N) {
00648   SDLoc dl(N);
00649   if (N->getValueType(0) == MVT::i32) {
00650     SDValue Shl_0 = N->getOperand(0);
00651     SDValue Shl_1 = N->getOperand(1);
00652     // RHS is const.
00653     if (Shl_1.getOpcode() == ISD::Constant) {
00654       if (Shl_0.getOpcode() == ISD::MUL) {
00655         SDValue Mul_0 = Shl_0.getOperand(0); // Val
00656         SDValue Mul_1 = Shl_0.getOperand(1); // Const
00657         // RHS of mul is const.
00658         if (Mul_1.getOpcode() == ISD::Constant) {
00659           int32_t ShlConst =
00660             cast<ConstantSDNode>(Shl_1.getNode())->getSExtValue();
00661           int32_t MulConst =
00662             cast<ConstantSDNode>(Mul_1.getNode())->getSExtValue();
00663           int32_t ValConst = MulConst << ShlConst;
00664           SDValue Val = CurDAG->getTargetConstant(ValConst,
00665                                                   MVT::i32);
00666           if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Val.getNode()))
00667             if (isInt<9>(CN->getSExtValue())) {
00668               SDNode* Result =
00669                 CurDAG->getMachineNode(Hexagon::M2_mpysmi, dl,
00670                                        MVT::i32, Mul_0, Val);
00671               ReplaceUses(N, Result);
00672               return Result;
00673             }
00674 
00675         }
00676       } else if (Shl_0.getOpcode() == ISD::SUB) {
00677         SDValue Sub_0 = Shl_0.getOperand(0); // Const 0
00678         SDValue Sub_1 = Shl_0.getOperand(1); // Val
00679         if (Sub_0.getOpcode() == ISD::Constant) {
00680           int32_t SubConst =
00681             cast<ConstantSDNode>(Sub_0.getNode())->getSExtValue();
00682           if (SubConst == 0) {
00683             if (Sub_1.getOpcode() == ISD::SHL) {
00684               SDValue Shl2_0 = Sub_1.getOperand(0); // Val
00685               SDValue Shl2_1 = Sub_1.getOperand(1); // Const
00686               if (Shl2_1.getOpcode() == ISD::Constant) {
00687                 int32_t ShlConst =
00688                   cast<ConstantSDNode>(Shl_1.getNode())->getSExtValue();
00689                 int32_t Shl2Const =
00690                   cast<ConstantSDNode>(Shl2_1.getNode())->getSExtValue();
00691                 int32_t ValConst = 1 << (ShlConst+Shl2Const);
00692                 SDValue Val = CurDAG->getTargetConstant(-ValConst, MVT::i32);
00693                 if (ConstantSDNode *CN =
00694                     dyn_cast<ConstantSDNode>(Val.getNode()))
00695                   if (isInt<9>(CN->getSExtValue())) {
00696                     SDNode* Result =
00697                       CurDAG->getMachineNode(Hexagon::M2_mpysmi, dl, MVT::i32,
00698                                              Shl2_0, Val);
00699                     ReplaceUses(N, Result);
00700                     return Result;
00701                   }
00702               }
00703             }
00704           }
00705         }
00706       }
00707     }
00708   }
00709   return SelectCode(N);
00710 }
00711 
00712 
00713 //
00714 // If there is an zero_extend followed an intrinsic in DAG (this means - the
00715 // result of the intrinsic is predicate); convert the zero_extend to
00716 // transfer instruction.
00717 //
00718 // Zero extend -> transfer is lowered here. Otherwise, zero_extend will be
00719 // converted into a MUX as predicate registers defined as 1 bit in the
00720 // compiler. Architecture defines them as 8-bit registers.
00721 // We want to preserve all the lower 8-bits and, not just 1 LSB bit.
00722 //
00723 SDNode *HexagonDAGToDAGISel::SelectZeroExtend(SDNode *N) {
00724   SDLoc dl(N);
00725 
00726   SDValue Op0 = N->getOperand(0);
00727   EVT OpVT = Op0.getValueType();
00728   unsigned OpBW = OpVT.getSizeInBits();
00729 
00730   // Special handling for zero-extending a vector of booleans.
00731   if (OpVT.isVector() && OpVT.getVectorElementType() == MVT::i1 && OpBW <= 64) {
00732     SDNode *Mask = CurDAG->getMachineNode(Hexagon::C2_mask, dl, MVT::i64, Op0);
00733     unsigned NE = OpVT.getVectorNumElements();
00734     EVT ExVT = N->getValueType(0);
00735     unsigned ES = ExVT.getVectorElementType().getSizeInBits();
00736     uint64_t MV = 0, Bit = 1;
00737     for (unsigned i = 0; i < NE; ++i) {
00738       MV |= Bit;
00739       Bit <<= ES;
00740     }
00741     SDValue Ones = CurDAG->getTargetConstant(MV, MVT::i64);
00742     SDNode *OnesReg = CurDAG->getMachineNode(Hexagon::CONST64_Int_Real, dl,
00743                                              MVT::i64, Ones);
00744     if (ExVT.getSizeInBits() == 32) {
00745       SDNode *And = CurDAG->getMachineNode(Hexagon::A2_andp, dl, MVT::i64,
00746                                            SDValue(Mask,0), SDValue(OnesReg,0));
00747       SDValue SubR = CurDAG->getTargetConstant(Hexagon::subreg_loreg, MVT::i32);
00748       return CurDAG->getMachineNode(Hexagon::EXTRACT_SUBREG, dl, ExVT,
00749                                     SDValue(And,0), SubR);
00750     }
00751     return CurDAG->getMachineNode(Hexagon::A2_andp, dl, ExVT,
00752                                   SDValue(Mask,0), SDValue(OnesReg,0));
00753   }
00754 
00755   SDNode *IsIntrinsic = N->getOperand(0).getNode();
00756   if ((IsIntrinsic->getOpcode() == ISD::INTRINSIC_WO_CHAIN)) {
00757     unsigned ID =
00758       cast<ConstantSDNode>(IsIntrinsic->getOperand(0))->getZExtValue();
00759     if (doesIntrinsicReturnPredicate(ID)) {
00760       // Now we need to differentiate target data types.
00761       if (N->getValueType(0) == MVT::i64) {
00762         // Convert the zero_extend to Rs = Pd followed by A2_combinew(0,Rs).
00763         SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
00764         SDNode *Result_1 = CurDAG->getMachineNode(Hexagon::C2_tfrpr, dl,
00765                                                   MVT::i32,
00766                                                   SDValue(IsIntrinsic, 0));
00767         SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A2_tfrsi, dl,
00768                                                   MVT::i32,
00769                                                   TargetConst0);
00770         SDNode *Result_3 = CurDAG->getMachineNode(Hexagon::A2_combinew, dl,
00771                                                   MVT::i64, MVT::Other,
00772                                                   SDValue(Result_2, 0),
00773                                                   SDValue(Result_1, 0));
00774         ReplaceUses(N, Result_3);
00775         return Result_3;
00776       }
00777       if (N->getValueType(0) == MVT::i32) {
00778         // Convert the zero_extend to Rs = Pd
00779         SDNode* RsPd = CurDAG->getMachineNode(Hexagon::C2_tfrpr, dl,
00780                                               MVT::i32,
00781                                               SDValue(IsIntrinsic, 0));
00782         ReplaceUses(N, RsPd);
00783         return RsPd;
00784       }
00785       llvm_unreachable("Unexpected value type");
00786     }
00787   }
00788   return SelectCode(N);
00789 }
00790 
00791 //
00792 // Checking for intrinsics circular load/store, and bitreverse load/store
00793 // instrisics in order to select the correct lowered operation.
00794 //
00795 SDNode *HexagonDAGToDAGISel::SelectIntrinsicWChain(SDNode *N) {
00796   unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
00797   if (IntNo == Intrinsic::hexagon_circ_ldd  ||
00798       IntNo == Intrinsic::hexagon_circ_ldw  ||
00799       IntNo == Intrinsic::hexagon_circ_lduh ||
00800       IntNo == Intrinsic::hexagon_circ_ldh  ||
00801       IntNo == Intrinsic::hexagon_circ_ldub ||
00802       IntNo == Intrinsic::hexagon_circ_ldb) {
00803     SDLoc dl(N);
00804     SDValue Chain = N->getOperand(0);
00805     SDValue Base = N->getOperand(2);
00806     SDValue Load = N->getOperand(3);
00807     SDValue ModifierExpr = N->getOperand(4);
00808     SDValue Offset = N->getOperand(5);
00809 
00810     // We need to add the rerurn type for the load.  This intrinsic has
00811     // two return types, one for the load and one for the post-increment.
00812     // Only the *_ld instructions push the extra return type, and bump the
00813     // result node operand number correspondingly.
00814     std::vector<EVT> ResTys;
00815     unsigned opc;
00816     unsigned memsize, align;
00817     MVT MvtSize = MVT::i32;
00818 
00819     if (IntNo == Intrinsic::hexagon_circ_ldd) {
00820       ResTys.push_back(MVT::i32);
00821       ResTys.push_back(MVT::i64);
00822       opc = Hexagon::L2_loadrd_pci_pseudo;
00823       memsize = 8;
00824       align = 8;
00825     } else if (IntNo == Intrinsic::hexagon_circ_ldw) {
00826       ResTys.push_back(MVT::i32);
00827       ResTys.push_back(MVT::i32);
00828       opc = Hexagon::L2_loadri_pci_pseudo;
00829       memsize = 4;
00830       align = 4;
00831     } else if (IntNo == Intrinsic::hexagon_circ_ldh) {
00832       ResTys.push_back(MVT::i32);
00833       ResTys.push_back(MVT::i32);
00834       opc = Hexagon::L2_loadrh_pci_pseudo;
00835       memsize = 2;
00836       align = 2;
00837       MvtSize = MVT::i16;
00838     } else if (IntNo == Intrinsic::hexagon_circ_lduh) {
00839       ResTys.push_back(MVT::i32);
00840       ResTys.push_back(MVT::i32);
00841       opc = Hexagon::L2_loadruh_pci_pseudo;
00842       memsize = 2;
00843       align = 2;
00844       MvtSize = MVT::i16;
00845     } else if (IntNo == Intrinsic::hexagon_circ_ldb) {
00846       ResTys.push_back(MVT::i32);
00847       ResTys.push_back(MVT::i32);
00848       opc = Hexagon::L2_loadrb_pci_pseudo;
00849       memsize = 1;
00850       align = 1;
00851       MvtSize = MVT::i8;
00852     } else if (IntNo == Intrinsic::hexagon_circ_ldub) {
00853       ResTys.push_back(MVT::i32);
00854       ResTys.push_back(MVT::i32);
00855       opc = Hexagon::L2_loadrub_pci_pseudo;
00856       memsize = 1;
00857       align = 1;
00858       MvtSize = MVT::i8;
00859     } else
00860       llvm_unreachable("no opc");
00861 
00862     ResTys.push_back(MVT::Other);
00863 
00864     // Copy over the arguments, which are the same mostly.
00865     SmallVector<SDValue, 5> Ops;
00866     Ops.push_back(Base);
00867     Ops.push_back(Load);
00868     Ops.push_back(ModifierExpr);
00869     int32_t Val = cast<ConstantSDNode>(Offset.getNode())->getSExtValue();
00870     Ops.push_back(CurDAG->getTargetConstant(Val, MVT::i32));
00871     Ops.push_back(Chain);
00872     SDNode* Result = CurDAG->getMachineNode(opc, dl, ResTys, Ops);
00873 
00874     SDValue ST;
00875     MachineMemOperand *Mem =
00876       MF->getMachineMemOperand(MachinePointerInfo(),
00877                                MachineMemOperand::MOStore, memsize, align);
00878     if (MvtSize != MVT::i32)
00879       ST = CurDAG->getTruncStore(Chain, dl, SDValue(Result, 1), Load,
00880                                  MvtSize, Mem);
00881     else
00882       ST = CurDAG->getStore(Chain, dl, SDValue(Result, 1), Load, Mem);
00883 
00884     SDNode* Store = SelectStore(ST.getNode());
00885 
00886     const SDValue Froms[] = { SDValue(N, 0),
00887                               SDValue(N, 1) };
00888     const SDValue Tos[]   = { SDValue(Result, 0),
00889                               SDValue(Store, 0) };
00890     ReplaceUses(Froms, Tos, 2);
00891     return Result;
00892   }
00893 
00894   if (IntNo == Intrinsic::hexagon_brev_ldd  ||
00895       IntNo == Intrinsic::hexagon_brev_ldw  ||
00896       IntNo == Intrinsic::hexagon_brev_ldh  ||
00897       IntNo == Intrinsic::hexagon_brev_lduh ||
00898       IntNo == Intrinsic::hexagon_brev_ldb  ||
00899       IntNo == Intrinsic::hexagon_brev_ldub) {
00900     SDLoc dl(N);
00901     SDValue Chain = N->getOperand(0);
00902     SDValue Base = N->getOperand(2);
00903     SDValue Load = N->getOperand(3);
00904     SDValue ModifierExpr = N->getOperand(4);
00905 
00906     // We need to add the rerurn type for the load.  This intrinsic has
00907     // two return types, one for the load and one for the post-increment.
00908     std::vector<EVT> ResTys;
00909     unsigned opc;
00910     unsigned memsize, align;
00911     MVT MvtSize = MVT::i32;
00912 
00913     if (IntNo == Intrinsic::hexagon_brev_ldd) {
00914       ResTys.push_back(MVT::i32);
00915       ResTys.push_back(MVT::i64);
00916       opc = Hexagon::L2_loadrd_pbr_pseudo;
00917       memsize = 8;
00918       align = 8;
00919     } else if (IntNo == Intrinsic::hexagon_brev_ldw) {
00920       ResTys.push_back(MVT::i32);
00921       ResTys.push_back(MVT::i32);
00922       opc = Hexagon::L2_loadri_pbr_pseudo;
00923       memsize = 4;
00924       align = 4;
00925     } else if (IntNo == Intrinsic::hexagon_brev_ldh) {
00926       ResTys.push_back(MVT::i32);
00927       ResTys.push_back(MVT::i32);
00928       opc = Hexagon::L2_loadrh_pbr_pseudo;
00929       memsize = 2;
00930       align = 2;
00931       MvtSize = MVT::i16;
00932     } else if (IntNo == Intrinsic::hexagon_brev_lduh) {
00933       ResTys.push_back(MVT::i32);
00934       ResTys.push_back(MVT::i32);
00935       opc = Hexagon::L2_loadruh_pbr_pseudo;
00936       memsize = 2;
00937       align = 2;
00938       MvtSize = MVT::i16;
00939     } else if (IntNo == Intrinsic::hexagon_brev_ldb) {
00940       ResTys.push_back(MVT::i32);
00941       ResTys.push_back(MVT::i32);
00942       opc = Hexagon::L2_loadrb_pbr_pseudo;
00943       memsize = 1;
00944       align = 1;
00945       MvtSize = MVT::i8;
00946     } else if (IntNo == Intrinsic::hexagon_brev_ldub) {
00947       ResTys.push_back(MVT::i32);
00948       ResTys.push_back(MVT::i32);
00949       opc = Hexagon::L2_loadrub_pbr_pseudo;
00950       memsize = 1;
00951       align = 1;
00952       MvtSize = MVT::i8;
00953     } else
00954       llvm_unreachable("no opc");
00955 
00956     ResTys.push_back(MVT::Other);
00957 
00958     // Copy over the arguments, which are the same mostly.
00959     SmallVector<SDValue, 4> Ops;
00960     Ops.push_back(Base);
00961     Ops.push_back(Load);
00962     Ops.push_back(ModifierExpr);
00963     Ops.push_back(Chain);
00964     SDNode* Result = CurDAG->getMachineNode(opc, dl, ResTys, Ops);
00965     SDValue ST;
00966     MachineMemOperand *Mem =
00967       MF->getMachineMemOperand(MachinePointerInfo(),
00968                                MachineMemOperand::MOStore, memsize, align);
00969     if (MvtSize != MVT::i32)
00970       ST = CurDAG->getTruncStore(Chain, dl, SDValue(Result, 1), Load,
00971                                  MvtSize, Mem);
00972     else
00973       ST = CurDAG->getStore(Chain, dl, SDValue(Result, 1), Load, Mem);
00974 
00975     SDNode* Store = SelectStore(ST.getNode());
00976 
00977     const SDValue Froms[] = { SDValue(N, 0),
00978                               SDValue(N, 1) };
00979     const SDValue Tos[]   = { SDValue(Result, 0),
00980                               SDValue(Store, 0) };
00981     ReplaceUses(Froms, Tos, 2);
00982     return Result;
00983   }
00984 
00985   return SelectCode(N);
00986 }
00987 
00988 //
00989 // Checking for intrinsics which have predicate registers as operand(s)
00990 // and lowering to the actual intrinsic.
00991 //
00992 SDNode *HexagonDAGToDAGISel::SelectIntrinsicWOChain(SDNode *N) {
00993   unsigned IID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
00994   unsigned Bits;
00995   switch (IID) {
00996   case Intrinsic::hexagon_S2_vsplatrb:
00997     Bits = 8;
00998     break;
00999   case Intrinsic::hexagon_S2_vsplatrh:
01000     Bits = 16;
01001     break;
01002   default:
01003     return SelectCode(N);
01004   }
01005 
01006   SDValue const &V = N->getOperand(1);
01007   SDValue U;
01008   if (isValueExtension(V, Bits, U)) {
01009     SDValue R = CurDAG->getNode(N->getOpcode(), SDLoc(N), N->getValueType(0),
01010       N->getOperand(0), U);
01011     return SelectCode(R.getNode());
01012   }
01013   return SelectCode(N);
01014 }
01015 
01016 //
01017 // Map floating point constant values.
01018 //
01019 SDNode *HexagonDAGToDAGISel::SelectConstantFP(SDNode *N) {
01020   SDLoc dl(N);
01021   ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N);
01022   APFloat APF = CN->getValueAPF();
01023   if (N->getValueType(0) == MVT::f32) {
01024     return CurDAG->getMachineNode(Hexagon::TFRI_f, dl, MVT::f32,
01025               CurDAG->getTargetConstantFP(APF.convertToFloat(), MVT::f32));
01026   }
01027   else if (N->getValueType(0) == MVT::f64) {
01028     return CurDAG->getMachineNode(Hexagon::CONST64_Float_Real, dl, MVT::f64,
01029               CurDAG->getTargetConstantFP(APF.convertToDouble(), MVT::f64));
01030   }
01031 
01032   return SelectCode(N);
01033 }
01034 
01035 //
01036 // Map predicate true (encoded as -1 in LLVM) to a XOR.
01037 //
01038 SDNode *HexagonDAGToDAGISel::SelectConstant(SDNode *N) {
01039   SDLoc dl(N);
01040   if (N->getValueType(0) == MVT::i1) {
01041     SDNode* Result = 0;
01042     int32_t Val = cast<ConstantSDNode>(N)->getSExtValue();
01043     if (Val == -1) {
01044       Result = CurDAG->getMachineNode(Hexagon::TFR_PdTrue, dl, MVT::i1);
01045     } else if (Val == 0) {
01046       Result = CurDAG->getMachineNode(Hexagon::TFR_PdFalse, dl, MVT::i1);
01047     }
01048     if (Result) {
01049       ReplaceUses(N, Result);
01050       return Result;
01051     }
01052   }
01053 
01054   return SelectCode(N);
01055 }
01056 
01057 
01058 //
01059 // Map add followed by a asr -> asr +=.
01060 //
01061 SDNode *HexagonDAGToDAGISel::SelectAdd(SDNode *N) {
01062   SDLoc dl(N);
01063   if (N->getValueType(0) != MVT::i32) {
01064     return SelectCode(N);
01065   }
01066   // Identify nodes of the form: add(asr(...)).
01067   SDNode* Src1 = N->getOperand(0).getNode();
01068   if (Src1->getOpcode() != ISD::SRA || !Src1->hasOneUse()
01069       || Src1->getValueType(0) != MVT::i32) {
01070     return SelectCode(N);
01071   }
01072 
01073   // Build Rd = Rd' + asr(Rs, Rt). The machine constraints will ensure that
01074   // Rd and Rd' are assigned to the same register
01075   SDNode* Result = CurDAG->getMachineNode(Hexagon::S2_asr_r_r_acc, dl, MVT::i32,
01076                                           N->getOperand(1),
01077                                           Src1->getOperand(0),
01078                                           Src1->getOperand(1));
01079   ReplaceUses(N, Result);
01080 
01081   return Result;
01082 }
01083 
01084 //
01085 // Map the following, where possible.
01086 // AND/FABS -> clrbit
01087 // OR -> setbit
01088 // XOR/FNEG ->toggle_bit.
01089 //
01090 SDNode *HexagonDAGToDAGISel::SelectBitOp(SDNode *N) {
01091   SDLoc dl(N);
01092   EVT ValueVT = N->getValueType(0);
01093 
01094   // We handle only 32 and 64-bit bit ops.
01095   if (!(ValueVT == MVT::i32 || ValueVT == MVT::i64 ||
01096         ValueVT == MVT::f32 || ValueVT == MVT::f64))
01097     return SelectCode(N);
01098 
01099   // We handly only fabs and fneg for V5.
01100   unsigned Opc = N->getOpcode();
01101   if ((Opc == ISD::FABS || Opc == ISD::FNEG) && !HST->hasV5TOps())
01102     return SelectCode(N);
01103 
01104   int64_t Val = 0;
01105   if (Opc != ISD::FABS && Opc != ISD::FNEG) {
01106     if (N->getOperand(1).getOpcode() == ISD::Constant)
01107       Val = cast<ConstantSDNode>((N)->getOperand(1))->getSExtValue();
01108     else
01109      return SelectCode(N);
01110   }
01111 
01112   if (Opc == ISD::AND) {
01113     if (((ValueVT == MVT::i32) &&
01114                   (!((Val & 0x80000000) || (Val & 0x7fffffff)))) ||
01115         ((ValueVT == MVT::i64) &&
01116                   (!((Val & 0x8000000000000000) || (Val & 0x7fffffff)))))
01117       // If it's simple AND, do the normal op.
01118       return SelectCode(N);
01119     else
01120       Val = ~Val;
01121   }
01122 
01123   // If OR or AND is being fed by shl, srl and, sra don't do this change,
01124   // because Hexagon provide |= &= on shl, srl, and sra.
01125   // Traverse the DAG to see if there is shl, srl and sra.
01126   if (Opc == ISD::OR || Opc == ISD::AND) {
01127     switch (N->getOperand(0)->getOpcode()) {
01128       default: break;
01129       case ISD::SRA:
01130       case ISD::SRL:
01131       case ISD::SHL:
01132         return SelectCode(N);
01133     }
01134   }
01135 
01136   // Make sure it's power of 2.
01137   unsigned bitpos = 0;
01138   if (Opc != ISD::FABS && Opc != ISD::FNEG) {
01139     if (((ValueVT == MVT::i32) && !isPowerOf2_32(Val)) ||
01140         ((ValueVT == MVT::i64) && !isPowerOf2_64(Val)))
01141       return SelectCode(N);
01142 
01143     // Get the bit position.
01144     bitpos = countTrailingZeros(uint64_t(Val));
01145   } else {
01146     // For fabs and fneg, it's always the 31st bit.
01147     bitpos = 31;
01148   }
01149 
01150   unsigned BitOpc = 0;
01151   // Set the right opcode for bitwise operations.
01152   switch(Opc) {
01153     default: llvm_unreachable("Only bit-wise/abs/neg operations are allowed.");
01154     case ISD::AND:
01155     case ISD::FABS:
01156       BitOpc = Hexagon::S2_clrbit_i;
01157       break;
01158     case ISD::OR:
01159       BitOpc = Hexagon::S2_setbit_i;
01160       break;
01161     case ISD::XOR:
01162     case ISD::FNEG:
01163       BitOpc = Hexagon::S2_togglebit_i;
01164       break;
01165   }
01166 
01167   SDNode *Result;
01168   // Get the right SDVal for the opcode.
01169   SDValue SDVal = CurDAG->getTargetConstant(bitpos, MVT::i32);
01170 
01171   if (ValueVT == MVT::i32 || ValueVT == MVT::f32) {
01172     Result = CurDAG->getMachineNode(BitOpc, dl, ValueVT,
01173                                     N->getOperand(0), SDVal);
01174   } else {
01175     // 64-bit gymnastic to use REG_SEQUENCE. But it's worth it.
01176     EVT SubValueVT;
01177     if (ValueVT == MVT::i64)
01178       SubValueVT = MVT::i32;
01179     else
01180       SubValueVT = MVT::f32;
01181 
01182     SDNode *Reg = N->getOperand(0).getNode();
01183     SDValue RegClass = CurDAG->getTargetConstant(Hexagon::DoubleRegsRegClassID,
01184                                                  MVT::i64);
01185 
01186     SDValue SubregHiIdx = CurDAG->getTargetConstant(Hexagon::subreg_hireg,
01187                                                     MVT::i32);
01188     SDValue SubregLoIdx = CurDAG->getTargetConstant(Hexagon::subreg_loreg,
01189                                                     MVT::i32);
01190 
01191     SDValue SubregHI = CurDAG->getTargetExtractSubreg(Hexagon::subreg_hireg, dl,
01192                                                     MVT::i32, SDValue(Reg, 0));
01193 
01194     SDValue SubregLO = CurDAG->getTargetExtractSubreg(Hexagon::subreg_loreg, dl,
01195                                                     MVT::i32, SDValue(Reg, 0));
01196 
01197     // Clear/set/toggle hi or lo registers depending on the bit position.
01198     if (SubValueVT != MVT::f32 && bitpos < 32) {
01199       SDNode *Result0 = CurDAG->getMachineNode(BitOpc, dl, SubValueVT,
01200                                                SubregLO, SDVal);
01201       const SDValue Ops[] = { RegClass, SubregHI, SubregHiIdx,
01202                               SDValue(Result0, 0), SubregLoIdx };
01203       Result = CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE,
01204                                       dl, ValueVT, Ops);
01205     } else {
01206       if (Opc != ISD::FABS && Opc != ISD::FNEG)
01207         SDVal = CurDAG->getTargetConstant(bitpos-32, MVT::i32);
01208       SDNode *Result0 = CurDAG->getMachineNode(BitOpc, dl, SubValueVT,
01209                                                SubregHI, SDVal);
01210       const SDValue Ops[] = { RegClass, SDValue(Result0, 0), SubregHiIdx,
01211                               SubregLO, SubregLoIdx };
01212       Result = CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE,
01213                                       dl, ValueVT, Ops);
01214     }
01215   }
01216 
01217   ReplaceUses(N, Result);
01218   return Result;
01219 }
01220 
01221 
01222 SDNode *HexagonDAGToDAGISel::SelectFrameIndex(SDNode *N) {
01223   MachineFrameInfo *MFI = MF->getFrameInfo();
01224   const HexagonFrameLowering *HFI = HST->getFrameLowering();
01225   int FX = cast<FrameIndexSDNode>(N)->getIndex();
01226   unsigned StkA = HFI->getStackAlignment();
01227   unsigned MaxA = MFI->getMaxAlignment();
01228   SDValue FI = CurDAG->getTargetFrameIndex(FX, MVT::i32);
01229   SDValue Zero = CurDAG->getTargetConstant(0, MVT::i32);
01230   SDLoc DL(N);
01231   SDNode *R = 0;
01232 
01233   // Use TFR_FI when:
01234   // - the object is fixed, or
01235   // - there are no objects with higher-than-default alignment, or
01236   // - there are no dynamically allocated objects.
01237   // Otherwise, use TFR_FIA.
01238   if (FX < 0 || MaxA <= StkA || !MFI->hasVarSizedObjects()) {
01239     R = CurDAG->getMachineNode(Hexagon::TFR_FI, DL, MVT::i32, FI, Zero);
01240   } else {
01241     auto &HMFI = *MF->getInfo<HexagonMachineFunctionInfo>();
01242     unsigned AR = HMFI.getStackAlignBaseVReg();
01243     SDValue CH = CurDAG->getEntryNode();
01244     SDValue Ops[] = { CurDAG->getCopyFromReg(CH, DL, AR, MVT::i32), FI, Zero };
01245     R = CurDAG->getMachineNode(Hexagon::TFR_FIA, DL, MVT::i32, Ops);
01246   }
01247 
01248   if (N->getHasDebugValue())
01249     CurDAG->TransferDbgValues(SDValue(N, 0), SDValue(R, 0));
01250   return R;
01251 }
01252 
01253 
01254 SDNode *HexagonDAGToDAGISel::Select(SDNode *N) {
01255   if (N->isMachineOpcode()) {
01256     N->setNodeId(-1);
01257     return nullptr;   // Already selected.
01258   }
01259 
01260   switch (N->getOpcode()) {
01261   case ISD::Constant:
01262     return SelectConstant(N);
01263 
01264   case ISD::ConstantFP:
01265     return SelectConstantFP(N);
01266 
01267   case ISD::FrameIndex:
01268     return SelectFrameIndex(N);
01269 
01270   case ISD::ADD:
01271     return SelectAdd(N);
01272 
01273   case ISD::SHL:
01274     return SelectSHL(N);
01275 
01276   case ISD::LOAD:
01277     return SelectLoad(N);
01278 
01279   case ISD::STORE:
01280     return SelectStore(N);
01281 
01282   case ISD::MUL:
01283     return SelectMul(N);
01284 
01285   case ISD::AND:
01286   case ISD::OR:
01287   case ISD::XOR:
01288   case ISD::FABS:
01289   case ISD::FNEG:
01290     return SelectBitOp(N);
01291 
01292   case ISD::ZERO_EXTEND:
01293     return SelectZeroExtend(N);
01294 
01295   case ISD::INTRINSIC_W_CHAIN:
01296     return SelectIntrinsicWChain(N);
01297 
01298   case ISD::INTRINSIC_WO_CHAIN:
01299     return SelectIntrinsicWOChain(N);
01300   }
01301 
01302   return SelectCode(N);
01303 }
01304 
01305 bool HexagonDAGToDAGISel::
01306 SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID,
01307                              std::vector<SDValue> &OutOps) {
01308   SDValue Inp = Op, Res;
01309 
01310   switch (ConstraintID) {
01311   default:
01312     return true;
01313   case InlineAsm::Constraint_i:
01314   case InlineAsm::Constraint_o: // Offsetable.
01315   case InlineAsm::Constraint_v: // Not offsetable.
01316   case InlineAsm::Constraint_m: // Memory.
01317     if (SelectAddrFI(Inp, Res))
01318       OutOps.push_back(Res);
01319     else
01320       OutOps.push_back(Inp);
01321     break;
01322   }
01323 
01324   OutOps.push_back(CurDAG->getTargetConstant(0, MVT::i32));
01325   return false;
01326 }
01327 
01328 void HexagonDAGToDAGISel::PreprocessISelDAG() {
01329   SelectionDAG &DAG = *CurDAG;
01330   std::vector<SDNode*> Nodes;
01331   for (auto I = DAG.allnodes_begin(), E = DAG.allnodes_end(); I != E; ++I)
01332     Nodes.push_back(I);
01333 
01334   // Simplify: (or (select c x 0) z)  ->  (select c (or x z) z)
01335   //           (or (select c 0 y) z)  ->  (select c z (or y z))
01336   // This may not be the right thing for all targets, so do it here.
01337   for (auto I: Nodes) {
01338     if (I->getOpcode() != ISD::OR)
01339       continue;
01340 
01341     auto IsZero = [] (const SDValue &V) -> bool {
01342       if (ConstantSDNode *SC = dyn_cast<ConstantSDNode>(V.getNode()))
01343         return SC->isNullValue();
01344       return false;
01345     };
01346     auto IsSelect0 = [IsZero] (const SDValue &Op) -> bool {
01347       if (Op.getOpcode() != ISD::SELECT)
01348         return false;
01349       return IsZero(Op.getOperand(1))  || IsZero(Op.getOperand(2));
01350     };
01351 
01352     SDValue N0 = I->getOperand(0), N1 = I->getOperand(1);
01353     EVT VT = I->getValueType(0);
01354     bool SelN0 = IsSelect0(N0);
01355     SDValue SOp = SelN0 ? N0 : N1;
01356     SDValue VOp = SelN0 ? N1 : N0;
01357 
01358     if (SOp.getOpcode() == ISD::SELECT && SOp.getNode()->hasOneUse()) {
01359       SDValue SC = SOp.getOperand(0);
01360       SDValue SX = SOp.getOperand(1);
01361       SDValue SY = SOp.getOperand(2);
01362       SDLoc DLS = SOp;
01363       if (IsZero(SY)) {
01364         SDValue NewOr = DAG.getNode(ISD::OR, DLS, VT, SX, VOp);
01365         SDValue NewSel = DAG.getNode(ISD::SELECT, DLS, VT, SC, NewOr, VOp);
01366         DAG.ReplaceAllUsesWith(I, NewSel.getNode());
01367       } else if (IsZero(SX)) {
01368         SDValue NewOr = DAG.getNode(ISD::OR, DLS, VT, SY, VOp);
01369         SDValue NewSel = DAG.getNode(ISD::SELECT, DLS, VT, SC, VOp, NewOr);
01370         DAG.ReplaceAllUsesWith(I, NewSel.getNode());
01371       }
01372     }
01373   }
01374 }
01375 
01376 void HexagonDAGToDAGISel::EmitFunctionEntryCode() {
01377   auto &HST = static_cast<const HexagonSubtarget&>(MF->getSubtarget());
01378   auto &HFI = *HST.getFrameLowering();
01379   if (!HFI.needsAligna(*MF))
01380     return;
01381 
01382   MachineFrameInfo *MFI = MF->getFrameInfo();
01383   MachineBasicBlock *EntryBB = MF->begin();
01384   unsigned AR = FuncInfo->CreateReg(MVT::i32);
01385   unsigned MaxA = MFI->getMaxAlignment();
01386   auto &HII = *HST.getInstrInfo();
01387   BuildMI(EntryBB, DebugLoc(), HII.get(Hexagon::ALIGNA), AR)
01388       .addImm(MaxA);
01389   MF->getInfo<HexagonMachineFunctionInfo>()->setStackAlignBaseVReg(AR);
01390 }
01391 
01392 // Match a frame index that can be used in an addressing mode.
01393 bool HexagonDAGToDAGISel::SelectAddrFI(SDValue& N, SDValue &R) {
01394   if (N.getOpcode() != ISD::FrameIndex)
01395     return false;
01396   auto &HFI = *HST->getFrameLowering();
01397   MachineFrameInfo *MFI = MF->getFrameInfo();
01398   int FX = cast<FrameIndexSDNode>(N)->getIndex();
01399   if (!MFI->isFixedObjectIndex(FX) && HFI.needsAligna(*MF))
01400     return false;
01401   R = CurDAG->getTargetFrameIndex(FX, MVT::i32);
01402   return true;
01403 }
01404 
01405 inline bool HexagonDAGToDAGISel::SelectAddrGA(SDValue &N, SDValue &R) {
01406   return SelectGlobalAddress(N, R, false);
01407 }
01408 
01409 inline bool HexagonDAGToDAGISel::SelectAddrGP(SDValue &N, SDValue &R) {
01410   return SelectGlobalAddress(N, R, true);
01411 }
01412 
01413 bool HexagonDAGToDAGISel::SelectGlobalAddress(SDValue &N, SDValue &R,
01414                                               bool UseGP) {
01415   switch (N.getOpcode()) {
01416   case ISD::ADD: {
01417     SDValue N0 = N.getOperand(0);
01418     SDValue N1 = N.getOperand(1);
01419     unsigned GAOpc = N0.getOpcode();
01420     if (UseGP && GAOpc != HexagonISD::CONST32_GP)
01421       return false;
01422     if (!UseGP && GAOpc != HexagonISD::CONST32)
01423       return false;
01424     if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N1)) {
01425       SDValue Addr = N0.getOperand(0);
01426       if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Addr)) {
01427         if (GA->getOpcode() == ISD::TargetGlobalAddress) {
01428           uint64_t NewOff = GA->getOffset() + (uint64_t)Const->getSExtValue();
01429           R = CurDAG->getTargetGlobalAddress(GA->getGlobal(), SDLoc(Const),
01430                                              N.getValueType(), NewOff);
01431           return true;
01432         }
01433       }
01434     }
01435     break;
01436   }
01437   case HexagonISD::CONST32:
01438     // The operand(0) of CONST32 is TargetGlobalAddress, which is what we
01439     // want in the instruction.
01440     if (!UseGP)
01441       R = N.getOperand(0);
01442     return !UseGP;
01443   case HexagonISD::CONST32_GP:
01444     if (UseGP)
01445       R = N.getOperand(0);
01446     return UseGP;
01447   default:
01448     return false;
01449   }
01450 
01451   return false;
01452 }
01453 
01454 bool HexagonDAGToDAGISel::isValueExtension(const SDValue &Val,
01455       unsigned FromBits, SDValue &Src) {
01456   unsigned Opc = Val.getOpcode();
01457   switch (Opc) {
01458   case ISD::SIGN_EXTEND:
01459   case ISD::ZERO_EXTEND:
01460   case ISD::ANY_EXTEND: {
01461     SDValue const &Op0 = Val.getOperand(0);
01462     EVT T = Op0.getValueType();
01463     if (T.isInteger() && T.getSizeInBits() == FromBits) {
01464       Src = Op0;
01465       return true;
01466     }
01467     break;
01468   }
01469   case ISD::SIGN_EXTEND_INREG:
01470   case ISD::AssertSext:
01471   case ISD::AssertZext:
01472     if (Val.getOperand(0).getValueType().isInteger()) {
01473       VTSDNode *T = cast<VTSDNode>(Val.getOperand(1));
01474       if (T->getVT().getSizeInBits() == FromBits) {
01475         Src = Val.getOperand(0);
01476         return true;
01477       }
01478     }
01479     break;
01480   case ISD::AND: {
01481     // Check if this is an AND with "FromBits" of lower bits set to 1.
01482     uint64_t FromMask = (1 << FromBits) - 1;
01483     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(0))) {
01484       if (C->getZExtValue() == FromMask) {
01485         Src = Val.getOperand(1);
01486         return true;
01487       }
01488     }
01489     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(1))) {
01490       if (C->getZExtValue() == FromMask) {
01491         Src = Val.getOperand(0);
01492         return true;
01493       }
01494     }
01495     break;
01496   }
01497   case ISD::OR:
01498   case ISD::XOR: {
01499     // OR/XOR with the lower "FromBits" bits set to 0.
01500     uint64_t FromMask = (1 << FromBits) - 1;
01501     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(0))) {
01502       if ((C->getZExtValue() & FromMask) == 0) {
01503         Src = Val.getOperand(1);
01504         return true;
01505       }
01506     }
01507     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(1))) {
01508       if ((C->getZExtValue() & FromMask) == 0) {
01509         Src = Val.getOperand(0);
01510         return true;
01511       }
01512     }
01513   }
01514   default:
01515     break;
01516   }
01517   return false;
01518 }