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