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