| 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 |
| Krzysztof Parzyszek | 0006e1a | 2016-07-29 15:15:35 +0000 | [diff] [blame] | 29 | cl::opt<bool> |
| 30 | EnableAddressRebalancing("isel-rebalance-addr", cl::Hidden, cl::init(true), |
| 31 | cl::desc("Rebalance address calculation trees to improve " |
| 32 | "instruction selection")); |
| 33 | |
| 34 | // Rebalance only if this allows e.g. combining a GA with an offset or |
| 35 | // factoring out a shift. |
| 36 | static |
| 37 | cl::opt<bool> |
| 38 | RebalanceOnlyForOptimizations("rebalance-only-opt", cl::Hidden, cl::init(false), |
| 39 | cl::desc("Rebalance address tree only if this allows optimizations")); |
| 40 | |
| 41 | static |
| 42 | cl::opt<bool> |
| 43 | RebalanceOnlyImbalancedTrees("rebalance-only-imbal", cl::Hidden, |
| 44 | cl::init(false), cl::desc("Rebalance address tree only if it is imbalanced")); |
| 45 | |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 46 | //===----------------------------------------------------------------------===// |
| 47 | // Instruction Selector Implementation |
| 48 | //===----------------------------------------------------------------------===// |
| 49 | |
| 50 | //===--------------------------------------------------------------------===// |
| 51 | /// HexagonDAGToDAGISel - Hexagon specific code to select Hexagon machine |
| 52 | /// instructions for SelectionDAG operations. |
| 53 | /// |
| 54 | namespace { |
| 55 | class HexagonDAGToDAGISel : public SelectionDAGISel { |
| Eric Christopher | 23a7d1e | 2015-03-21 03:12:59 +0000 | [diff] [blame] | 56 | const HexagonSubtarget *HST; |
| Krzysztof Parzyszek | 08ff888 | 2015-11-26 18:38:27 +0000 | [diff] [blame] | 57 | const HexagonInstrInfo *HII; |
| 58 | const HexagonRegisterInfo *HRI; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 59 | public: |
| Krzysztof Parzyszek | a29622a | 2015-03-12 16:44:50 +0000 | [diff] [blame] | 60 | explicit HexagonDAGToDAGISel(HexagonTargetMachine &tm, |
| Jyotsna Verma | d922524 | 2013-02-13 21:38:46 +0000 | [diff] [blame] | 61 | CodeGenOpt::Level OptLevel) |
| Krzysztof Parzyszek | 0006e1a | 2016-07-29 15:15:35 +0000 | [diff] [blame] | 62 | : SelectionDAGISel(tm, OptLevel), HST(nullptr), HII(nullptr), |
| Chandler Carruth | 9ac86ef | 2016-06-03 10:13:31 +0000 | [diff] [blame] | 63 | HRI(nullptr) {} |
| Eric Christopher | 23a7d1e | 2015-03-21 03:12:59 +0000 | [diff] [blame] | 64 | |
| 65 | bool runOnMachineFunction(MachineFunction &MF) override { |
| 66 | // Reset the subtarget each time through. |
| 67 | HST = &MF.getSubtarget<HexagonSubtarget>(); |
| Krzysztof Parzyszek | 08ff888 | 2015-11-26 18:38:27 +0000 | [diff] [blame] | 68 | HII = HST->getInstrInfo(); |
| 69 | HRI = HST->getRegisterInfo(); |
| Eric Christopher | 23a7d1e | 2015-03-21 03:12:59 +0000 | [diff] [blame] | 70 | SelectionDAGISel::runOnMachineFunction(MF); |
| 71 | return true; |
| 72 | } |
| 73 | |
| Krzysztof Parzyszek | 317d42c | 2016-08-01 20:31:50 +0000 | [diff] [blame] | 74 | void PreprocessISelDAG() override; |
| 75 | void EmitFunctionEntryCode() override; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 76 | |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 77 | void Select(SDNode *N) override; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 78 | |
| 79 | // Complex Pattern Selectors. |
| Colin LeMahieu | 987b094 | 2015-02-04 20:38:01 +0000 | [diff] [blame] | 80 | inline bool SelectAddrGA(SDValue &N, SDValue &R); |
| Colin LeMahieu | 5149135 | 2015-02-04 22:36:28 +0000 | [diff] [blame] | 81 | inline bool SelectAddrGP(SDValue &N, SDValue &R); |
| Colin LeMahieu | 987b094 | 2015-02-04 20:38:01 +0000 | [diff] [blame] | 82 | bool SelectGlobalAddress(SDValue &N, SDValue &R, bool UseGP); |
| Colin LeMahieu | c7522f3 | 2015-01-14 23:07:36 +0000 | [diff] [blame] | 83 | bool SelectAddrFI(SDValue &N, SDValue &R); |
| 84 | |
| Craig Topper | 906c2cd | 2014-04-29 07:58:16 +0000 | [diff] [blame] | 85 | const char *getPassName() const override { |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 86 | return "Hexagon DAG->DAG Pattern Instruction Selection"; |
| 87 | } |
| 88 | |
| Krzysztof Parzyszek | 7d5b4db | 2016-02-12 17:01:51 +0000 | [diff] [blame] | 89 | // Generate a machine instruction node corresponding to the circ/brev |
| 90 | // load intrinsic. |
| 91 | MachineSDNode *LoadInstrForLoadIntrinsic(SDNode *IntN); |
| 92 | // Given the circ/brev load intrinsic and the already generated machine |
| 93 | // instruction, generate the appropriate store (that is a part of the |
| 94 | // intrinsic's functionality). |
| 95 | SDNode *StoreInstrForLoadIntrinsic(MachineSDNode *LoadN, SDNode *IntN); |
| 96 | |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 97 | void SelectFrameIndex(SDNode *N); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 98 | /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for |
| 99 | /// inline asm expressions. |
| Craig Topper | 906c2cd | 2014-04-29 07:58:16 +0000 | [diff] [blame] | 100 | bool SelectInlineAsmMemoryOperand(const SDValue &Op, |
| Daniel Sanders | 60f1db0 | 2015-03-13 12:45:09 +0000 | [diff] [blame] | 101 | unsigned ConstraintID, |
| Craig Topper | 906c2cd | 2014-04-29 07:58:16 +0000 | [diff] [blame] | 102 | std::vector<SDValue> &OutOps) override; |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 103 | bool tryLoadOfLoadIntrinsic(LoadSDNode *N); |
| 104 | void SelectLoad(SDNode *N); |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 105 | void SelectIndexedLoad(LoadSDNode *LD, const SDLoc &dl); |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 106 | void SelectIndexedStore(StoreSDNode *ST, const SDLoc &dl); |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 107 | void SelectStore(SDNode *N); |
| 108 | void SelectSHL(SDNode *N); |
| 109 | void SelectMul(SDNode *N); |
| 110 | void SelectZeroExtend(SDNode *N); |
| 111 | void SelectIntrinsicWChain(SDNode *N); |
| 112 | void SelectIntrinsicWOChain(SDNode *N); |
| 113 | void SelectConstant(SDNode *N); |
| 114 | void SelectConstantFP(SDNode *N); |
| 115 | void SelectAdd(SDNode *N); |
| Krzysztof Parzyszek | 5da24e5 | 2016-06-27 15:08:22 +0000 | [diff] [blame] | 116 | void SelectBitcast(SDNode *N); |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 117 | void SelectBitOp(SDNode *N); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 118 | |
| Krzysztof Parzyszek | a29622a | 2015-03-12 16:44:50 +0000 | [diff] [blame] | 119 | // XformMskToBitPosU5Imm - Returns the bit position which |
| 120 | // the single bit 32 bit mask represents. |
| 121 | // Used in Clr and Set bit immediate memops. |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 122 | SDValue XformMskToBitPosU5Imm(uint32_t Imm, const SDLoc &DL) { |
| Krzysztof Parzyszek | 317d42c | 2016-08-01 20:31:50 +0000 | [diff] [blame] | 123 | unsigned BitPos = Log2_32(Imm); |
| 124 | assert(BitPos < 32 && "Constant out of range for 32 BitPos Memops"); |
| 125 | return CurDAG->getTargetConstant(BitPos, DL, MVT::i32); |
| Krzysztof Parzyszek | a29622a | 2015-03-12 16:44:50 +0000 | [diff] [blame] | 126 | } |
| Jyotsna Verma | fdc660b | 2013-03-22 18:41:34 +0000 | [diff] [blame] | 127 | |
| Krzysztof Parzyszek | a29622a | 2015-03-12 16:44:50 +0000 | [diff] [blame] | 128 | // XformMskToBitPosU4Imm - Returns the bit position which the single-bit |
| 129 | // 16 bit mask represents. Used in Clr and Set bit immediate memops. |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 130 | SDValue XformMskToBitPosU4Imm(uint16_t Imm, const SDLoc &DL) { |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 131 | return XformMskToBitPosU5Imm(Imm, DL); |
| Krzysztof Parzyszek | a29622a | 2015-03-12 16:44:50 +0000 | [diff] [blame] | 132 | } |
| Jyotsna Verma | fdc660b | 2013-03-22 18:41:34 +0000 | [diff] [blame] | 133 | |
| Krzysztof Parzyszek | a29622a | 2015-03-12 16:44:50 +0000 | [diff] [blame] | 134 | // XformMskToBitPosU3Imm - Returns the bit position which the single-bit |
| 135 | // 8 bit mask represents. Used in Clr and Set bit immediate memops. |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 136 | SDValue XformMskToBitPosU3Imm(uint8_t Imm, const SDLoc &DL) { |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 137 | return XformMskToBitPosU5Imm(Imm, DL); |
| Krzysztof Parzyszek | a29622a | 2015-03-12 16:44:50 +0000 | [diff] [blame] | 138 | } |
| Jyotsna Verma | fdc660b | 2013-03-22 18:41:34 +0000 | [diff] [blame] | 139 | |
| Krzysztof Parzyszek | a29622a | 2015-03-12 16:44:50 +0000 | [diff] [blame] | 140 | // Return true if there is exactly one bit set in V, i.e., if V is one of the |
| 141 | // following integers: 2^0, 2^1, ..., 2^31. |
| 142 | bool ImmIsSingleBit(uint32_t v) const { |
| 143 | return isPowerOf2_32(v); |
| 144 | } |
| Jyotsna Verma | fdc660b | 2013-03-22 18:41:34 +0000 | [diff] [blame] | 145 | |
| Krzysztof Parzyszek | a29622a | 2015-03-12 16:44:50 +0000 | [diff] [blame] | 146 | // XformM5ToU5Imm - Return a target constant with the specified value, of |
| 147 | // type i32 where the negative literal is transformed into a positive literal |
| 148 | // for use in -= memops. |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 149 | inline SDValue XformM5ToU5Imm(signed Imm, const SDLoc &DL) { |
| 150 | assert((Imm >= -31 && Imm <= -1) && "Constant out of range for Memops"); |
| 151 | return CurDAG->getTargetConstant(-Imm, DL, MVT::i32); |
| 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 | // XformU7ToU7M1Imm - Return a target constant decremented by 1, in range |
| 155 | // [1..128], used in cmpb.gtu instructions. |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 156 | inline SDValue XformU7ToU7M1Imm(signed Imm, const SDLoc &DL) { |
| Krzysztof Parzyszek | a29622a | 2015-03-12 16:44:50 +0000 | [diff] [blame] | 157 | assert((Imm >= 1 && Imm <= 128) && "Constant out of range for cmpb op"); |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 158 | return CurDAG->getTargetConstant(Imm - 1, DL, MVT::i8); |
| Krzysztof Parzyszek | a29622a | 2015-03-12 16:44:50 +0000 | [diff] [blame] | 159 | } |
| Jyotsna Verma | fdc660b | 2013-03-22 18:41:34 +0000 | [diff] [blame] | 160 | |
| Krzysztof Parzyszek | a29622a | 2015-03-12 16:44:50 +0000 | [diff] [blame] | 161 | // XformS8ToS8M1Imm - Return a target constant decremented by 1. |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 162 | inline SDValue XformSToSM1Imm(signed Imm, const SDLoc &DL) { |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 163 | return CurDAG->getTargetConstant(Imm - 1, DL, MVT::i32); |
| Krzysztof Parzyszek | a29622a | 2015-03-12 16:44:50 +0000 | [diff] [blame] | 164 | } |
| Jyotsna Verma | 6031625 | 2013-02-05 19:20:45 +0000 | [diff] [blame] | 165 | |
| Krzysztof Parzyszek | a29622a | 2015-03-12 16:44:50 +0000 | [diff] [blame] | 166 | // XformU8ToU8M1Imm - Return a target constant decremented by 1. |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 167 | inline SDValue XformUToUM1Imm(unsigned Imm, const SDLoc &DL) { |
| Krzysztof Parzyszek | a29622a | 2015-03-12 16:44:50 +0000 | [diff] [blame] | 168 | assert((Imm >= 1) && "Cannot decrement unsigned int less than 1"); |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 169 | return CurDAG->getTargetConstant(Imm - 1, DL, MVT::i32); |
| Krzysztof Parzyszek | a29622a | 2015-03-12 16:44:50 +0000 | [diff] [blame] | 170 | } |
| Jyotsna Verma | 89c8482 | 2013-04-23 19:15:55 +0000 | [diff] [blame] | 171 | |
| Krzysztof Parzyszek | a29622a | 2015-03-12 16:44:50 +0000 | [diff] [blame] | 172 | // XformSToSM2Imm - Return a target constant decremented by 2. |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 173 | inline SDValue XformSToSM2Imm(unsigned Imm, const SDLoc &DL) { |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 174 | return CurDAG->getTargetConstant(Imm - 2, DL, MVT::i32); |
| Krzysztof Parzyszek | a29622a | 2015-03-12 16:44:50 +0000 | [diff] [blame] | 175 | } |
| Jyotsna Verma | 89c8482 | 2013-04-23 19:15:55 +0000 | [diff] [blame] | 176 | |
| Krzysztof Parzyszek | a29622a | 2015-03-12 16:44:50 +0000 | [diff] [blame] | 177 | // XformSToSM3Imm - Return a target constant decremented by 3. |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 178 | inline SDValue XformSToSM3Imm(unsigned Imm, const SDLoc &DL) { |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 179 | return CurDAG->getTargetConstant(Imm - 3, DL, MVT::i32); |
| Krzysztof Parzyszek | a29622a | 2015-03-12 16:44:50 +0000 | [diff] [blame] | 180 | } |
| Colin LeMahieu | 19ed07c | 2015-01-28 18:29:11 +0000 | [diff] [blame] | 181 | |
| Krzysztof Parzyszek | a29622a | 2015-03-12 16:44:50 +0000 | [diff] [blame] | 182 | // Include the pieces autogenerated from the target description. |
| 183 | #include "HexagonGenDAGISel.inc" |
| Colin LeMahieu | 0ee02fc | 2015-01-19 20:31:18 +0000 | [diff] [blame] | 184 | |
| 185 | private: |
| Krzysztof Parzyszek | a29622a | 2015-03-12 16:44:50 +0000 | [diff] [blame] | 186 | bool isValueExtension(const SDValue &Val, unsigned FromBits, SDValue &Src); |
| Krzysztof Parzyszek | bba0bf7 | 2016-07-15 15:35:52 +0000 | [diff] [blame] | 187 | bool orIsAdd(const SDNode *N) const; |
| Krzysztof Parzyszek | 2d65ea7 | 2016-03-28 15:43:03 +0000 | [diff] [blame] | 188 | bool isAlignedMemNode(const MemSDNode *N) const; |
| Krzysztof Parzyszek | 0006e1a | 2016-07-29 15:15:35 +0000 | [diff] [blame] | 189 | |
| 190 | SmallDenseMap<SDNode *,int> RootWeights; |
| 191 | SmallDenseMap<SDNode *,int> RootHeights; |
| 192 | SmallDenseMap<const Value *,int> GAUsesInFunction; |
| 193 | int getWeight(SDNode *N); |
| 194 | int getHeight(SDNode *N); |
| 195 | SDValue getMultiplierForSHL(SDNode *N); |
| 196 | SDValue factorOutPowerOf2(SDValue V, unsigned Power); |
| 197 | unsigned getUsesInFunction(const Value *V); |
| 198 | SDValue balanceSubTree(SDNode *N, bool Factorize = false); |
| 199 | void rebalanceAddressTrees(); |
| Krzysztof Parzyszek | a29622a | 2015-03-12 16:44:50 +0000 | [diff] [blame] | 200 | }; // end HexagonDAGToDAGISel |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 201 | } // end anonymous namespace |
| 202 | |
| 203 | |
| 204 | /// createHexagonISelDag - This pass converts a legalized DAG into a |
| 205 | /// Hexagon-specific DAG, ready for instruction scheduling. |
| 206 | /// |
| Krzysztof Parzyszek | a29622a | 2015-03-12 16:44:50 +0000 | [diff] [blame] | 207 | namespace llvm { |
| 208 | FunctionPass *createHexagonISelDag(HexagonTargetMachine &TM, |
| 209 | CodeGenOpt::Level OptLevel) { |
| Jyotsna Verma | d922524 | 2013-02-13 21:38:46 +0000 | [diff] [blame] | 210 | return new HexagonDAGToDAGISel(TM, OptLevel); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 211 | } |
| Krzysztof Parzyszek | a29622a | 2015-03-12 16:44:50 +0000 | [diff] [blame] | 212 | } |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 213 | |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 214 | // Intrinsics that return a a predicate. |
| Krzysztof Parzyszek | 08ff888 | 2015-11-26 18:38:27 +0000 | [diff] [blame] | 215 | static bool doesIntrinsicReturnPredicate(unsigned ID) { |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 216 | switch (ID) { |
| 217 | default: |
| Krzysztof Parzyszek | 08ff888 | 2015-11-26 18:38:27 +0000 | [diff] [blame] | 218 | return false; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 219 | case Intrinsic::hexagon_C2_cmpeq: |
| 220 | case Intrinsic::hexagon_C2_cmpgt: |
| 221 | case Intrinsic::hexagon_C2_cmpgtu: |
| 222 | case Intrinsic::hexagon_C2_cmpgtup: |
| 223 | case Intrinsic::hexagon_C2_cmpgtp: |
| 224 | case Intrinsic::hexagon_C2_cmpeqp: |
| 225 | case Intrinsic::hexagon_C2_bitsset: |
| 226 | case Intrinsic::hexagon_C2_bitsclr: |
| 227 | case Intrinsic::hexagon_C2_cmpeqi: |
| 228 | case Intrinsic::hexagon_C2_cmpgti: |
| 229 | case Intrinsic::hexagon_C2_cmpgtui: |
| 230 | case Intrinsic::hexagon_C2_cmpgei: |
| 231 | case Intrinsic::hexagon_C2_cmpgeui: |
| 232 | case Intrinsic::hexagon_C2_cmplt: |
| 233 | case Intrinsic::hexagon_C2_cmpltu: |
| 234 | case Intrinsic::hexagon_C2_bitsclri: |
| 235 | case Intrinsic::hexagon_C2_and: |
| 236 | case Intrinsic::hexagon_C2_or: |
| 237 | case Intrinsic::hexagon_C2_xor: |
| 238 | case Intrinsic::hexagon_C2_andn: |
| 239 | case Intrinsic::hexagon_C2_not: |
| 240 | case Intrinsic::hexagon_C2_orn: |
| 241 | case Intrinsic::hexagon_C2_pxfer_map: |
| 242 | case Intrinsic::hexagon_C2_any8: |
| 243 | case Intrinsic::hexagon_C2_all8: |
| 244 | case Intrinsic::hexagon_A2_vcmpbeq: |
| 245 | case Intrinsic::hexagon_A2_vcmpbgtu: |
| 246 | case Intrinsic::hexagon_A2_vcmpheq: |
| 247 | case Intrinsic::hexagon_A2_vcmphgt: |
| 248 | case Intrinsic::hexagon_A2_vcmphgtu: |
| 249 | case Intrinsic::hexagon_A2_vcmpweq: |
| 250 | case Intrinsic::hexagon_A2_vcmpwgt: |
| 251 | case Intrinsic::hexagon_A2_vcmpwgtu: |
| 252 | case Intrinsic::hexagon_C2_tfrrp: |
| 253 | case Intrinsic::hexagon_S2_tstbit_i: |
| 254 | case Intrinsic::hexagon_S2_tstbit_r: |
| Krzysztof Parzyszek | 08ff888 | 2015-11-26 18:38:27 +0000 | [diff] [blame] | 255 | return true; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 259 | void HexagonDAGToDAGISel::SelectIndexedLoad(LoadSDNode *LD, const SDLoc &dl) { |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 260 | SDValue Chain = LD->getChain(); |
| 261 | SDValue Base = LD->getBasePtr(); |
| 262 | SDValue Offset = LD->getOffset(); |
| Krzysztof Parzyszek | 709a626 | 2016-06-24 21:27:17 +0000 | [diff] [blame] | 263 | int32_t Inc = cast<ConstantSDNode>(Offset.getNode())->getSExtValue(); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 264 | EVT LoadedVT = LD->getMemoryVT(); |
| 265 | unsigned Opcode = 0; |
| 266 | |
| Krzysztof Parzyszek | a29622a | 2015-03-12 16:44:50 +0000 | [diff] [blame] | 267 | // Check for zero extended loads. Treat any-extend loads as zero extended |
| 268 | // loads. |
| 269 | ISD::LoadExtType ExtType = LD->getExtensionType(); |
| 270 | bool IsZeroExt = (ExtType == ISD::ZEXTLOAD || ExtType == ISD::EXTLOAD); |
| Krzysztof Parzyszek | 709a626 | 2016-06-24 21:27:17 +0000 | [diff] [blame] | 271 | bool IsValidInc = HII->isValidAutoIncImm(LoadedVT, Inc); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 272 | |
| Krzysztof Parzyszek | 709a626 | 2016-06-24 21:27:17 +0000 | [diff] [blame] | 273 | assert(LoadedVT.isSimple()); |
| 274 | switch (LoadedVT.getSimpleVT().SimpleTy) { |
| 275 | case MVT::i8: |
| 276 | if (IsZeroExt) |
| 277 | Opcode = IsValidInc ? Hexagon::L2_loadrub_pi : Hexagon::L2_loadrub_io; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 278 | else |
| Krzysztof Parzyszek | 709a626 | 2016-06-24 21:27:17 +0000 | [diff] [blame] | 279 | Opcode = IsValidInc ? Hexagon::L2_loadrb_pi : Hexagon::L2_loadrb_io; |
| 280 | break; |
| 281 | case MVT::i16: |
| 282 | if (IsZeroExt) |
| 283 | Opcode = IsValidInc ? Hexagon::L2_loadruh_pi : Hexagon::L2_loadruh_io; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 284 | else |
| Krzysztof Parzyszek | 709a626 | 2016-06-24 21:27:17 +0000 | [diff] [blame] | 285 | Opcode = IsValidInc ? Hexagon::L2_loadrh_pi : Hexagon::L2_loadrh_io; |
| 286 | break; |
| 287 | case MVT::i32: |
| 288 | Opcode = IsValidInc ? Hexagon::L2_loadri_pi : Hexagon::L2_loadri_io; |
| 289 | break; |
| 290 | case MVT::i64: |
| 291 | Opcode = IsValidInc ? Hexagon::L2_loadrd_pi : Hexagon::L2_loadrd_io; |
| 292 | break; |
| 293 | // 64B |
| 294 | case MVT::v64i8: |
| 295 | case MVT::v32i16: |
| 296 | case MVT::v16i32: |
| 297 | case MVT::v8i64: |
| 298 | if (isAlignedMemNode(LD)) |
| 299 | Opcode = IsValidInc ? Hexagon::V6_vL32b_pi : Hexagon::V6_vL32b_ai; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 300 | else |
| Krzysztof Parzyszek | 709a626 | 2016-06-24 21:27:17 +0000 | [diff] [blame] | 301 | Opcode = IsValidInc ? Hexagon::V6_vL32Ub_pi : Hexagon::V6_vL32Ub_ai; |
| 302 | break; |
| Krzysztof Parzyszek | 08ff888 | 2015-11-26 18:38:27 +0000 | [diff] [blame] | 303 | // 128B |
| Krzysztof Parzyszek | 709a626 | 2016-06-24 21:27:17 +0000 | [diff] [blame] | 304 | case MVT::v128i8: |
| 305 | case MVT::v64i16: |
| 306 | case MVT::v32i32: |
| 307 | case MVT::v16i64: |
| 308 | if (isAlignedMemNode(LD)) |
| 309 | Opcode = IsValidInc ? Hexagon::V6_vL32b_pi_128B |
| 310 | : Hexagon::V6_vL32b_ai_128B; |
| 311 | else |
| 312 | Opcode = IsValidInc ? Hexagon::V6_vL32Ub_pi_128B |
| 313 | : Hexagon::V6_vL32Ub_ai_128B; |
| 314 | break; |
| 315 | default: |
| 316 | llvm_unreachable("Unexpected memory type in indexed load"); |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 317 | } |
| Krzysztof Parzyszek | a29622a | 2015-03-12 16:44:50 +0000 | [diff] [blame] | 318 | |
| Krzysztof Parzyszek | 709a626 | 2016-06-24 21:27:17 +0000 | [diff] [blame] | 319 | SDValue IncV = CurDAG->getTargetConstant(Inc, dl, MVT::i32); |
| 320 | MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1); |
| 321 | MemOp[0] = LD->getMemOperand(); |
| 322 | |
| 323 | auto getExt64 = [this,ExtType] (MachineSDNode *N, const SDLoc &dl) |
| 324 | -> MachineSDNode* { |
| 325 | if (ExtType == ISD::ZEXTLOAD || ExtType == ISD::EXTLOAD) { |
| 326 | SDValue Zero = CurDAG->getTargetConstant(0, dl, MVT::i32); |
| 327 | return CurDAG->getMachineNode(Hexagon::A4_combineir, dl, MVT::i64, |
| 328 | Zero, SDValue(N, 0)); |
| Krzysztof Parzyszek | 08ff888 | 2015-11-26 18:38:27 +0000 | [diff] [blame] | 329 | } |
| Krzysztof Parzyszek | 709a626 | 2016-06-24 21:27:17 +0000 | [diff] [blame] | 330 | if (ExtType == ISD::SEXTLOAD) |
| 331 | return CurDAG->getMachineNode(Hexagon::A2_sxtw, dl, MVT::i64, |
| 332 | SDValue(N, 0)); |
| 333 | return N; |
| 334 | }; |
| 335 | |
| 336 | // Loaded value Next address Chain |
| 337 | SDValue From[3] = { SDValue(LD,0), SDValue(LD,1), SDValue(LD,2) }; |
| 338 | SDValue To[3]; |
| 339 | |
| 340 | EVT ValueVT = LD->getValueType(0); |
| 341 | if (ValueVT == MVT::i64 && ExtType != ISD::NON_EXTLOAD) { |
| 342 | // A load extending to i64 will actually produce i32, which will then |
| 343 | // need to be extended to i64. |
| 344 | assert(LoadedVT.getSizeInBits() <= 32); |
| 345 | ValueVT = MVT::i32; |
| 346 | } |
| 347 | |
| 348 | if (IsValidInc) { |
| 349 | MachineSDNode *L = CurDAG->getMachineNode(Opcode, dl, ValueVT, |
| 350 | MVT::i32, MVT::Other, Base, |
| 351 | IncV, Chain); |
| 352 | L->setMemRefs(MemOp, MemOp+1); |
| 353 | To[1] = SDValue(L, 1); // Next address. |
| 354 | To[2] = SDValue(L, 2); // Chain. |
| 355 | // Handle special case for extension to i64. |
| 356 | if (LD->getValueType(0) == MVT::i64) |
| 357 | L = getExt64(L, dl); |
| 358 | To[0] = SDValue(L, 0); // Loaded (extended) value. |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 359 | } else { |
| Krzysztof Parzyszek | 709a626 | 2016-06-24 21:27:17 +0000 | [diff] [blame] | 360 | SDValue Zero = CurDAG->getTargetConstant(0, dl, MVT::i32); |
| 361 | MachineSDNode *L = CurDAG->getMachineNode(Opcode, dl, ValueVT, MVT::Other, |
| 362 | Base, Zero, Chain); |
| 363 | L->setMemRefs(MemOp, MemOp+1); |
| 364 | To[2] = SDValue(L, 1); // Chain. |
| 365 | MachineSDNode *A = CurDAG->getMachineNode(Hexagon::A2_addi, dl, MVT::i32, |
| 366 | Base, IncV); |
| 367 | To[1] = SDValue(A, 0); // Next address. |
| 368 | // Handle special case for extension to i64. |
| 369 | if (LD->getValueType(0) == MVT::i64) |
| 370 | L = getExt64(L, dl); |
| 371 | To[0] = SDValue(L, 0); // Loaded (extended) value. |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 372 | } |
| Krzysztof Parzyszek | 709a626 | 2016-06-24 21:27:17 +0000 | [diff] [blame] | 373 | ReplaceUses(From, To, 3); |
| 374 | CurDAG->RemoveDeadNode(LD); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | |
| Krzysztof Parzyszek | 7d5b4db | 2016-02-12 17:01:51 +0000 | [diff] [blame] | 378 | MachineSDNode *HexagonDAGToDAGISel::LoadInstrForLoadIntrinsic(SDNode *IntN) { |
| 379 | if (IntN->getOpcode() != ISD::INTRINSIC_W_CHAIN) |
| 380 | return nullptr; |
| 381 | |
| 382 | SDLoc dl(IntN); |
| 383 | unsigned IntNo = cast<ConstantSDNode>(IntN->getOperand(1))->getZExtValue(); |
| 384 | |
| 385 | static std::map<unsigned,unsigned> LoadPciMap = { |
| 386 | { Intrinsic::hexagon_circ_ldb, Hexagon::L2_loadrb_pci }, |
| 387 | { Intrinsic::hexagon_circ_ldub, Hexagon::L2_loadrub_pci }, |
| 388 | { Intrinsic::hexagon_circ_ldh, Hexagon::L2_loadrh_pci }, |
| 389 | { Intrinsic::hexagon_circ_lduh, Hexagon::L2_loadruh_pci }, |
| 390 | { Intrinsic::hexagon_circ_ldw, Hexagon::L2_loadri_pci }, |
| 391 | { Intrinsic::hexagon_circ_ldd, Hexagon::L2_loadrd_pci }, |
| 392 | }; |
| 393 | auto FLC = LoadPciMap.find(IntNo); |
| 394 | if (FLC != LoadPciMap.end()) { |
| 395 | SDNode *Mod = CurDAG->getMachineNode(Hexagon::A2_tfrrcr, dl, MVT::i32, |
| 396 | IntN->getOperand(4)); |
| 397 | EVT ValTy = (IntNo == Intrinsic::hexagon_circ_ldd) ? MVT::i64 : MVT::i32; |
| 398 | EVT RTys[] = { ValTy, MVT::i32, MVT::Other }; |
| 399 | // Operands: { Base, Increment, Modifier, Chain } |
| 400 | auto Inc = cast<ConstantSDNode>(IntN->getOperand(5)); |
| 401 | SDValue I = CurDAG->getTargetConstant(Inc->getSExtValue(), dl, MVT::i32); |
| 402 | MachineSDNode *Res = CurDAG->getMachineNode(FLC->second, dl, RTys, |
| 403 | { IntN->getOperand(2), I, SDValue(Mod,0), IntN->getOperand(0) }); |
| 404 | return Res; |
| 405 | } |
| 406 | |
| 407 | static std::map<unsigned,unsigned> LoadPbrMap = { |
| 408 | { Intrinsic::hexagon_brev_ldb, Hexagon::L2_loadrb_pbr }, |
| 409 | { Intrinsic::hexagon_brev_ldub, Hexagon::L2_loadrub_pbr }, |
| 410 | { Intrinsic::hexagon_brev_ldh, Hexagon::L2_loadrh_pbr }, |
| 411 | { Intrinsic::hexagon_brev_lduh, Hexagon::L2_loadruh_pbr }, |
| 412 | { Intrinsic::hexagon_brev_ldw, Hexagon::L2_loadri_pbr }, |
| 413 | { Intrinsic::hexagon_brev_ldd, Hexagon::L2_loadrd_pbr }, |
| 414 | }; |
| 415 | auto FLB = LoadPbrMap.find(IntNo); |
| 416 | if (FLB != LoadPbrMap.end()) { |
| 417 | SDNode *Mod = CurDAG->getMachineNode(Hexagon::A2_tfrrcr, dl, MVT::i32, |
| 418 | IntN->getOperand(4)); |
| 419 | EVT ValTy = (IntNo == Intrinsic::hexagon_brev_ldd) ? MVT::i64 : MVT::i32; |
| 420 | EVT RTys[] = { ValTy, MVT::i32, MVT::Other }; |
| 421 | // Operands: { Base, Modifier, Chain } |
| 422 | MachineSDNode *Res = CurDAG->getMachineNode(FLB->second, dl, RTys, |
| 423 | { IntN->getOperand(2), SDValue(Mod,0), IntN->getOperand(0) }); |
| 424 | return Res; |
| 425 | } |
| 426 | |
| 427 | return nullptr; |
| 428 | } |
| 429 | |
| 430 | SDNode *HexagonDAGToDAGISel::StoreInstrForLoadIntrinsic(MachineSDNode *LoadN, |
| 431 | SDNode *IntN) { |
| 432 | // The "LoadN" is just a machine load instruction. The intrinsic also |
| 433 | // involves storing it. Generate an appropriate store to the location |
| 434 | // given in the intrinsic's operand(3). |
| 435 | uint64_t F = HII->get(LoadN->getMachineOpcode()).TSFlags; |
| 436 | unsigned SizeBits = (F >> HexagonII::MemAccessSizePos) & |
| 437 | HexagonII::MemAccesSizeMask; |
| 438 | unsigned Size = 1U << (SizeBits-1); |
| 439 | |
| 440 | SDLoc dl(IntN); |
| 441 | MachinePointerInfo PI; |
| 442 | SDValue TS; |
| 443 | SDValue Loc = IntN->getOperand(3); |
| 444 | |
| 445 | if (Size >= 4) |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 446 | TS = CurDAG->getStore(SDValue(LoadN, 2), dl, SDValue(LoadN, 0), Loc, PI, |
| 447 | Size); |
| Krzysztof Parzyszek | 7d5b4db | 2016-02-12 17:01:51 +0000 | [diff] [blame] | 448 | else |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 449 | TS = CurDAG->getTruncStore(SDValue(LoadN, 2), dl, SDValue(LoadN, 0), Loc, |
| 450 | PI, MVT::getIntegerVT(Size * 8), Size); |
| Justin Bogner | dcb7a82 | 2016-05-10 20:31:53 +0000 | [diff] [blame] | 451 | |
| 452 | SDNode *StoreN; |
| 453 | { |
| 454 | HandleSDNode Handle(TS); |
| 455 | SelectStore(TS.getNode()); |
| 456 | StoreN = Handle.getValue().getNode(); |
| 457 | } |
| Krzysztof Parzyszek | 7d5b4db | 2016-02-12 17:01:51 +0000 | [diff] [blame] | 458 | |
| 459 | // Load's results are { Loaded value, Updated pointer, Chain } |
| 460 | ReplaceUses(SDValue(IntN, 0), SDValue(LoadN, 1)); |
| 461 | ReplaceUses(SDValue(IntN, 1), SDValue(StoreN, 0)); |
| 462 | return StoreN; |
| 463 | } |
| 464 | |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 465 | bool HexagonDAGToDAGISel::tryLoadOfLoadIntrinsic(LoadSDNode *N) { |
| Krzysztof Parzyszek | 7d5b4db | 2016-02-12 17:01:51 +0000 | [diff] [blame] | 466 | // The intrinsics for load circ/brev perform two operations: |
| 467 | // 1. Load a value V from the specified location, using the addressing |
| 468 | // mode corresponding to the intrinsic. |
| 469 | // 2. Store V into a specified location. This location is typically a |
| 470 | // local, temporary object. |
| 471 | // In many cases, the program using these intrinsics will immediately |
| 472 | // load V again from the local object. In those cases, when certain |
| 473 | // conditions are met, the last load can be removed. |
| 474 | // This function identifies and optimizes this pattern. If the pattern |
| 475 | // cannot be optimized, it returns nullptr, which will cause the load |
| 476 | // to be selected separately from the intrinsic (which will be handled |
| 477 | // in SelectIntrinsicWChain). |
| 478 | |
| 479 | SDValue Ch = N->getOperand(0); |
| 480 | SDValue Loc = N->getOperand(1); |
| 481 | |
| 482 | // Assume that the load and the intrinsic are connected directly with a |
| 483 | // chain: |
| 484 | // t1: i32,ch = int.load ..., ..., ..., Loc, ... // <-- C |
| 485 | // t2: i32,ch = load t1:1, Loc, ... |
| 486 | SDNode *C = Ch.getNode(); |
| 487 | |
| 488 | if (C->getOpcode() != ISD::INTRINSIC_W_CHAIN) |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 489 | return false; |
| Krzysztof Parzyszek | 7d5b4db | 2016-02-12 17:01:51 +0000 | [diff] [blame] | 490 | |
| 491 | // The second load can only be eliminated if its extension type matches |
| 492 | // that of the load instruction corresponding to the intrinsic. The user |
| 493 | // can provide an address of an unsigned variable to store the result of |
| 494 | // a sign-extending intrinsic into (or the other way around). |
| 495 | ISD::LoadExtType IntExt; |
| 496 | switch (cast<ConstantSDNode>(C->getOperand(1))->getZExtValue()) { |
| 497 | case Intrinsic::hexagon_brev_ldub: |
| 498 | case Intrinsic::hexagon_brev_lduh: |
| 499 | case Intrinsic::hexagon_circ_ldub: |
| 500 | case Intrinsic::hexagon_circ_lduh: |
| 501 | IntExt = ISD::ZEXTLOAD; |
| 502 | break; |
| 503 | case Intrinsic::hexagon_brev_ldw: |
| 504 | case Intrinsic::hexagon_brev_ldd: |
| 505 | case Intrinsic::hexagon_circ_ldw: |
| 506 | case Intrinsic::hexagon_circ_ldd: |
| 507 | IntExt = ISD::NON_EXTLOAD; |
| 508 | break; |
| 509 | default: |
| 510 | IntExt = ISD::SEXTLOAD; |
| 511 | break; |
| 512 | } |
| 513 | if (N->getExtensionType() != IntExt) |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 514 | return false; |
| Krzysztof Parzyszek | 7d5b4db | 2016-02-12 17:01:51 +0000 | [diff] [blame] | 515 | |
| 516 | // Make sure the target location for the loaded value in the load intrinsic |
| 517 | // is the location from which LD (or N) is loading. |
| 518 | if (C->getNumOperands() < 4 || Loc.getNode() != C->getOperand(3).getNode()) |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 519 | return false; |
| Krzysztof Parzyszek | 7d5b4db | 2016-02-12 17:01:51 +0000 | [diff] [blame] | 520 | |
| 521 | if (MachineSDNode *L = LoadInstrForLoadIntrinsic(C)) { |
| 522 | SDNode *S = StoreInstrForLoadIntrinsic(L, C); |
| 523 | SDValue F[] = { SDValue(N,0), SDValue(N,1), SDValue(C,0), SDValue(C,1) }; |
| 524 | SDValue T[] = { SDValue(L,0), SDValue(S,0), SDValue(L,1), SDValue(S,0) }; |
| 525 | ReplaceUses(F, T, array_lengthof(T)); |
| 526 | // This transformation will leave the intrinsic dead. If it remains in |
| 527 | // the DAG, the selection code will see it again, but without the load, |
| 528 | // and it will generate a store that is normally required for it. |
| Krzysztof Parzyszek | 0f791f4 | 2016-05-13 18:48:15 +0000 | [diff] [blame] | 529 | CurDAG->RemoveDeadNode(C); |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 530 | return true; |
| Krzysztof Parzyszek | 7d5b4db | 2016-02-12 17:01:51 +0000 | [diff] [blame] | 531 | } |
| 532 | |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 533 | return false; |
| Krzysztof Parzyszek | 7d5b4db | 2016-02-12 17:01:51 +0000 | [diff] [blame] | 534 | } |
| 535 | |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 536 | void HexagonDAGToDAGISel::SelectLoad(SDNode *N) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 537 | SDLoc dl(N); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 538 | LoadSDNode *LD = cast<LoadSDNode>(N); |
| 539 | ISD::MemIndexedMode AM = LD->getAddressingMode(); |
| 540 | |
| 541 | // Handle indexed loads. |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 542 | if (AM != ISD::UNINDEXED) { |
| 543 | SelectIndexedLoad(LD, dl); |
| 544 | return; |
| 545 | } |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 546 | |
| Krzysztof Parzyszek | 7d5b4db | 2016-02-12 17:01:51 +0000 | [diff] [blame] | 547 | // Handle patterns using circ/brev load intrinsics. |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 548 | if (tryLoadOfLoadIntrinsic(LD)) |
| 549 | return; |
| Krzysztof Parzyszek | 7d5b4db | 2016-02-12 17:01:51 +0000 | [diff] [blame] | 550 | |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 551 | SelectCode(LD); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 552 | } |
| 553 | |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 554 | void HexagonDAGToDAGISel::SelectIndexedStore(StoreSDNode *ST, const SDLoc &dl) { |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 555 | SDValue Chain = ST->getChain(); |
| 556 | SDValue Base = ST->getBasePtr(); |
| 557 | SDValue Offset = ST->getOffset(); |
| 558 | SDValue Value = ST->getValue(); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 559 | // Get the constant value. |
| Krzysztof Parzyszek | 709a626 | 2016-06-24 21:27:17 +0000 | [diff] [blame] | 560 | int32_t Inc = cast<ConstantSDNode>(Offset.getNode())->getSExtValue(); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 561 | EVT StoredVT = ST->getMemoryVT(); |
| Krzysztof Parzyszek | a29622a | 2015-03-12 16:44:50 +0000 | [diff] [blame] | 562 | EVT ValueVT = Value.getValueType(); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 563 | |
| Krzysztof Parzyszek | 709a626 | 2016-06-24 21:27:17 +0000 | [diff] [blame] | 564 | bool IsValidInc = HII->isValidAutoIncImm(StoredVT, Inc); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 565 | unsigned Opcode = 0; |
| 566 | |
| Krzysztof Parzyszek | 709a626 | 2016-06-24 21:27:17 +0000 | [diff] [blame] | 567 | assert(StoredVT.isSimple()); |
| 568 | switch (StoredVT.getSimpleVT().SimpleTy) { |
| 569 | case MVT::i8: |
| 570 | Opcode = IsValidInc ? Hexagon::S2_storerb_pi : Hexagon::S2_storerb_io; |
| 571 | break; |
| 572 | case MVT::i16: |
| 573 | Opcode = IsValidInc ? Hexagon::S2_storerh_pi : Hexagon::S2_storerh_io; |
| 574 | break; |
| 575 | case MVT::i32: |
| 576 | Opcode = IsValidInc ? Hexagon::S2_storeri_pi : Hexagon::S2_storeri_io; |
| 577 | break; |
| 578 | case MVT::i64: |
| 579 | Opcode = IsValidInc ? Hexagon::S2_storerd_pi : Hexagon::S2_storerd_io; |
| 580 | break; |
| 581 | // 64B |
| 582 | case MVT::v64i8: |
| 583 | case MVT::v32i16: |
| 584 | case MVT::v16i32: |
| 585 | case MVT::v8i64: |
| Krzysztof Parzyszek | 2d65ea7 | 2016-03-28 15:43:03 +0000 | [diff] [blame] | 586 | if (isAlignedMemNode(ST)) |
| Krzysztof Parzyszek | 709a626 | 2016-06-24 21:27:17 +0000 | [diff] [blame] | 587 | Opcode = IsValidInc ? Hexagon::V6_vS32b_pi : Hexagon::V6_vS32b_ai; |
| Krzysztof Parzyszek | 2d65ea7 | 2016-03-28 15:43:03 +0000 | [diff] [blame] | 588 | else |
| Krzysztof Parzyszek | 709a626 | 2016-06-24 21:27:17 +0000 | [diff] [blame] | 589 | Opcode = IsValidInc ? Hexagon::V6_vS32Ub_pi : Hexagon::V6_vS32Ub_ai; |
| 590 | break; |
| Krzysztof Parzyszek | 08ff888 | 2015-11-26 18:38:27 +0000 | [diff] [blame] | 591 | // 128B |
| Krzysztof Parzyszek | 709a626 | 2016-06-24 21:27:17 +0000 | [diff] [blame] | 592 | case MVT::v128i8: |
| 593 | case MVT::v64i16: |
| 594 | case MVT::v32i32: |
| 595 | case MVT::v16i64: |
| Krzysztof Parzyszek | 2d65ea7 | 2016-03-28 15:43:03 +0000 | [diff] [blame] | 596 | if (isAlignedMemNode(ST)) |
| Krzysztof Parzyszek | 709a626 | 2016-06-24 21:27:17 +0000 | [diff] [blame] | 597 | Opcode = IsValidInc ? Hexagon::V6_vS32b_pi_128B |
| 598 | : Hexagon::V6_vS32b_ai_128B; |
| Krzysztof Parzyszek | 2d65ea7 | 2016-03-28 15:43:03 +0000 | [diff] [blame] | 599 | else |
| Krzysztof Parzyszek | 709a626 | 2016-06-24 21:27:17 +0000 | [diff] [blame] | 600 | Opcode = IsValidInc ? Hexagon::V6_vS32Ub_pi_128B |
| 601 | : Hexagon::V6_vS32Ub_ai_128B; |
| 602 | break; |
| 603 | default: |
| 604 | llvm_unreachable("Unexpected memory type in indexed store"); |
| Krzysztof Parzyszek | 2d65ea7 | 2016-03-28 15:43:03 +0000 | [diff] [blame] | 605 | } |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 606 | |
| Krzysztof Parzyszek | 709a626 | 2016-06-24 21:27:17 +0000 | [diff] [blame] | 607 | if (ST->isTruncatingStore() && ValueVT.getSizeInBits() == 64) { |
| 608 | assert(StoredVT.getSizeInBits() < 64 && "Not a truncating store"); |
| 609 | Value = CurDAG->getTargetExtractSubreg(Hexagon::subreg_loreg, |
| 610 | dl, MVT::i32, Value); |
| 611 | } |
| 612 | |
| 613 | SDValue IncV = CurDAG->getTargetConstant(Inc, dl, MVT::i32); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 614 | MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1); |
| 615 | MemOp[0] = ST->getMemOperand(); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 616 | |
| Krzysztof Parzyszek | 709a626 | 2016-06-24 21:27:17 +0000 | [diff] [blame] | 617 | // Next address Chain |
| 618 | SDValue From[2] = { SDValue(ST,0), SDValue(ST,1) }; |
| 619 | SDValue To[2]; |
| 620 | |
| 621 | if (IsValidInc) { |
| 622 | // Build post increment store. |
| 623 | SDValue Ops[] = { Base, IncV, Value, Chain }; |
| 624 | MachineSDNode *S = CurDAG->getMachineNode(Opcode, dl, MVT::i32, MVT::Other, |
| 625 | Ops); |
| 626 | S->setMemRefs(MemOp, MemOp + 1); |
| 627 | To[0] = SDValue(S, 0); |
| 628 | To[1] = SDValue(S, 1); |
| 629 | } else { |
| 630 | SDValue Zero = CurDAG->getTargetConstant(0, dl, MVT::i32); |
| 631 | SDValue Ops[] = { Base, Zero, Value, Chain }; |
| 632 | MachineSDNode *S = CurDAG->getMachineNode(Opcode, dl, MVT::Other, Ops); |
| 633 | S->setMemRefs(MemOp, MemOp + 1); |
| 634 | To[1] = SDValue(S, 0); |
| 635 | MachineSDNode *A = CurDAG->getMachineNode(Hexagon::A2_addi, dl, MVT::i32, |
| 636 | Base, IncV); |
| 637 | To[0] = SDValue(A, 0); |
| 638 | } |
| 639 | |
| 640 | ReplaceUses(From, To, 2); |
| Justin Bogner | dcb7a82 | 2016-05-10 20:31:53 +0000 | [diff] [blame] | 641 | CurDAG->RemoveDeadNode(ST); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 642 | } |
| 643 | |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 644 | void HexagonDAGToDAGISel::SelectStore(SDNode *N) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 645 | SDLoc dl(N); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 646 | StoreSDNode *ST = cast<StoreSDNode>(N); |
| 647 | ISD::MemIndexedMode AM = ST->getAddressingMode(); |
| 648 | |
| 649 | // Handle indexed stores. |
| 650 | if (AM != ISD::UNINDEXED) { |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 651 | SelectIndexedStore(ST, dl); |
| 652 | return; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 653 | } |
| Sirish Pande | c92c316 | 2012-05-03 16:18:50 +0000 | [diff] [blame] | 654 | |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 655 | SelectCode(ST); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 656 | } |
| 657 | |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 658 | void HexagonDAGToDAGISel::SelectMul(SDNode *N) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 659 | SDLoc dl(N); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 660 | |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 661 | // %conv.i = sext i32 %tmp1 to i64 |
| 662 | // %conv2.i = sext i32 %add to i64 |
| 663 | // %mul.i = mul nsw i64 %conv2.i, %conv.i |
| 664 | // |
| 665 | // --- match with the following --- |
| 666 | // |
| 667 | // %mul.i = mpy (%tmp1, %add) |
| 668 | // |
| 669 | |
| 670 | if (N->getValueType(0) == MVT::i64) { |
| 671 | // Shifting a i64 signed multiply. |
| 672 | SDValue MulOp0 = N->getOperand(0); |
| 673 | SDValue MulOp1 = N->getOperand(1); |
| 674 | |
| 675 | SDValue OP0; |
| 676 | SDValue OP1; |
| 677 | |
| 678 | // Handle sign_extend and sextload. |
| 679 | if (MulOp0.getOpcode() == ISD::SIGN_EXTEND) { |
| 680 | SDValue Sext0 = MulOp0.getOperand(0); |
| 681 | if (Sext0.getNode()->getValueType(0) != MVT::i32) { |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 682 | SelectCode(N); |
| 683 | return; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 684 | } |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 685 | OP0 = Sext0; |
| 686 | } else if (MulOp0.getOpcode() == ISD::LOAD) { |
| 687 | LoadSDNode *LD = cast<LoadSDNode>(MulOp0.getNode()); |
| 688 | if (LD->getMemoryVT() != MVT::i32 || |
| 689 | LD->getExtensionType() != ISD::SEXTLOAD || |
| 690 | LD->getAddressingMode() != ISD::UNINDEXED) { |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 691 | SelectCode(N); |
| 692 | return; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 693 | } |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 694 | SDValue Chain = LD->getChain(); |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 695 | SDValue TargetConst0 = CurDAG->getTargetConstant(0, dl, MVT::i32); |
| Colin LeMahieu | 026e88d | 2014-12-23 20:02:16 +0000 | [diff] [blame] | 696 | OP0 = SDValue(CurDAG->getMachineNode(Hexagon::L2_loadri_io, dl, MVT::i32, |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 697 | MVT::Other, |
| 698 | LD->getBasePtr(), TargetConst0, |
| 699 | Chain), 0); |
| 700 | } else { |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 701 | SelectCode(N); |
| 702 | return; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | // Same goes for the second operand. |
| 706 | if (MulOp1.getOpcode() == ISD::SIGN_EXTEND) { |
| 707 | SDValue Sext1 = MulOp1.getOperand(0); |
| 708 | if (Sext1.getNode()->getValueType(0) != MVT::i32) { |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 709 | SelectCode(N); |
| 710 | return; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 711 | } |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 712 | OP1 = Sext1; |
| 713 | } else if (MulOp1.getOpcode() == ISD::LOAD) { |
| 714 | LoadSDNode *LD = cast<LoadSDNode>(MulOp1.getNode()); |
| 715 | if (LD->getMemoryVT() != MVT::i32 || |
| 716 | LD->getExtensionType() != ISD::SEXTLOAD || |
| 717 | LD->getAddressingMode() != ISD::UNINDEXED) { |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 718 | SelectCode(N); |
| 719 | return; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 720 | } |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 721 | SDValue Chain = LD->getChain(); |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 722 | SDValue TargetConst0 = CurDAG->getTargetConstant(0, dl, MVT::i32); |
| Colin LeMahieu | 026e88d | 2014-12-23 20:02:16 +0000 | [diff] [blame] | 723 | OP1 = SDValue(CurDAG->getMachineNode(Hexagon::L2_loadri_io, dl, MVT::i32, |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 724 | MVT::Other, |
| 725 | LD->getBasePtr(), TargetConst0, |
| 726 | Chain), 0); |
| 727 | } else { |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 728 | SelectCode(N); |
| 729 | return; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | // Generate a mpy instruction. |
| Krzysztof Parzyszek | 317d42c | 2016-08-01 20:31:50 +0000 | [diff] [blame] | 733 | SDNode *Result = CurDAG->getMachineNode(Hexagon::M2_dpmpyss_s0, dl, |
| 734 | MVT::i64, OP0, OP1); |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 735 | ReplaceNode(N, Result); |
| 736 | return; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 737 | } |
| 738 | |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 739 | SelectCode(N); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 740 | } |
| 741 | |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 742 | void HexagonDAGToDAGISel::SelectSHL(SDNode *N) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 743 | SDLoc dl(N); |
| Krzysztof Parzyszek | 317d42c | 2016-08-01 20:31:50 +0000 | [diff] [blame] | 744 | SDValue Shl_0 = N->getOperand(0); |
| 745 | SDValue Shl_1 = N->getOperand(1); |
| Krzysztof Parzyszek | d978ae2 | 2016-08-01 20:00:33 +0000 | [diff] [blame] | 746 | |
| Krzysztof Parzyszek | 317d42c | 2016-08-01 20:31:50 +0000 | [diff] [blame] | 747 | auto Default = [this,N] () -> void { SelectCode(N); }; |
| 748 | |
| 749 | if (N->getValueType(0) != MVT::i32 || Shl_1.getOpcode() != ISD::Constant) |
| 750 | return Default(); |
| 751 | |
| 752 | // RHS is const. |
| 753 | int32_t ShlConst = cast<ConstantSDNode>(Shl_1)->getSExtValue(); |
| 754 | |
| 755 | if (Shl_0.getOpcode() == ISD::MUL) { |
| 756 | SDValue Mul_0 = Shl_0.getOperand(0); // Val |
| 757 | SDValue Mul_1 = Shl_0.getOperand(1); // Const |
| 758 | // RHS of mul is const. |
| 759 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Mul_1)) { |
| 760 | int32_t ValConst = C->getSExtValue() << ShlConst; |
| 761 | if (isInt<9>(ValConst)) { |
| 762 | SDValue Val = CurDAG->getTargetConstant(ValConst, dl, MVT::i32); |
| 763 | SDNode *Result = CurDAG->getMachineNode(Hexagon::M2_mpysmi, dl, |
| 764 | MVT::i32, Mul_0, Val); |
| 765 | ReplaceNode(N, Result); |
| 766 | return; |
| 767 | } |
| 768 | } |
| 769 | return Default(); |
| 770 | } |
| 771 | |
| 772 | if (Shl_0.getOpcode() == ISD::SUB) { |
| 773 | SDValue Sub_0 = Shl_0.getOperand(0); // Const 0 |
| 774 | SDValue Sub_1 = Shl_0.getOperand(1); // Val |
| 775 | if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Sub_0)) { |
| 776 | if (C1->getSExtValue() != 0 || Sub_1.getOpcode() != ISD::SHL) |
| 777 | return Default(); |
| 778 | SDValue Shl2_0 = Sub_1.getOperand(0); // Val |
| 779 | SDValue Shl2_1 = Sub_1.getOperand(1); // Const |
| 780 | if (ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(Shl2_1)) { |
| 781 | int32_t ValConst = 1 << (ShlConst + C2->getSExtValue()); |
| 782 | if (isInt<9>(-ValConst)) { |
| 783 | SDValue Val = CurDAG->getTargetConstant(-ValConst, dl, MVT::i32); |
| 784 | SDNode *Result = CurDAG->getMachineNode(Hexagon::M2_mpysmi, dl, |
| 785 | MVT::i32, Shl2_0, Val); |
| 786 | ReplaceNode(N, Result); |
| 787 | return; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 788 | } |
| 789 | } |
| 790 | } |
| 791 | } |
| Krzysztof Parzyszek | 317d42c | 2016-08-01 20:31:50 +0000 | [diff] [blame] | 792 | |
| 793 | return Default(); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | |
| 797 | // |
| 798 | // If there is an zero_extend followed an intrinsic in DAG (this means - the |
| 799 | // result of the intrinsic is predicate); convert the zero_extend to |
| 800 | // transfer instruction. |
| 801 | // |
| 802 | // Zero extend -> transfer is lowered here. Otherwise, zero_extend will be |
| 803 | // converted into a MUX as predicate registers defined as 1 bit in the |
| 804 | // compiler. Architecture defines them as 8-bit registers. |
| 805 | // We want to preserve all the lower 8-bits and, not just 1 LSB bit. |
| 806 | // |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 807 | void HexagonDAGToDAGISel::SelectZeroExtend(SDNode *N) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 808 | SDLoc dl(N); |
| Krzysztof Parzyszek | 4211334 | 2015-03-19 16:33:08 +0000 | [diff] [blame] | 809 | |
| 810 | SDValue Op0 = N->getOperand(0); |
| 811 | EVT OpVT = Op0.getValueType(); |
| 812 | unsigned OpBW = OpVT.getSizeInBits(); |
| 813 | |
| 814 | // Special handling for zero-extending a vector of booleans. |
| 815 | if (OpVT.isVector() && OpVT.getVectorElementType() == MVT::i1 && OpBW <= 64) { |
| 816 | SDNode *Mask = CurDAG->getMachineNode(Hexagon::C2_mask, dl, MVT::i64, Op0); |
| 817 | unsigned NE = OpVT.getVectorNumElements(); |
| 818 | EVT ExVT = N->getValueType(0); |
| Sanjay Patel | 1ed771f | 2016-09-14 16:37:15 +0000 | [diff] [blame] | 819 | unsigned ES = ExVT.getScalarSizeInBits(); |
| Krzysztof Parzyszek | 4211334 | 2015-03-19 16:33:08 +0000 | [diff] [blame] | 820 | uint64_t MV = 0, Bit = 1; |
| 821 | for (unsigned i = 0; i < NE; ++i) { |
| 822 | MV |= Bit; |
| 823 | Bit <<= ES; |
| 824 | } |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 825 | SDValue Ones = CurDAG->getTargetConstant(MV, dl, MVT::i64); |
| Krzysztof Parzyszek | a338650 | 2016-08-10 16:46:36 +0000 | [diff] [blame] | 826 | SDNode *OnesReg = CurDAG->getMachineNode(Hexagon::CONST64, dl, |
| Krzysztof Parzyszek | 4211334 | 2015-03-19 16:33:08 +0000 | [diff] [blame] | 827 | MVT::i64, Ones); |
| 828 | if (ExVT.getSizeInBits() == 32) { |
| 829 | SDNode *And = CurDAG->getMachineNode(Hexagon::A2_andp, dl, MVT::i64, |
| 830 | SDValue(Mask,0), SDValue(OnesReg,0)); |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 831 | SDValue SubR = CurDAG->getTargetConstant(Hexagon::subreg_loreg, dl, |
| 832 | MVT::i32); |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 833 | ReplaceNode(N, CurDAG->getMachineNode(Hexagon::EXTRACT_SUBREG, dl, ExVT, |
| 834 | SDValue(And, 0), SubR)); |
| 835 | return; |
| Krzysztof Parzyszek | 4211334 | 2015-03-19 16:33:08 +0000 | [diff] [blame] | 836 | } |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 837 | ReplaceNode(N, |
| 838 | CurDAG->getMachineNode(Hexagon::A2_andp, dl, ExVT, |
| 839 | SDValue(Mask, 0), SDValue(OnesReg, 0))); |
| 840 | return; |
| Krzysztof Parzyszek | 4211334 | 2015-03-19 16:33:08 +0000 | [diff] [blame] | 841 | } |
| 842 | |
| Krzysztof Parzyszek | 317d42c | 2016-08-01 20:31:50 +0000 | [diff] [blame] | 843 | SDNode *Int = N->getOperand(0).getNode(); |
| 844 | if ((Int->getOpcode() == ISD::INTRINSIC_WO_CHAIN)) { |
| 845 | unsigned ID = cast<ConstantSDNode>(Int->getOperand(0))->getZExtValue(); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 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, |
| Krzysztof Parzyszek | 317d42c | 2016-08-01 20:31:50 +0000 | [diff] [blame] | 852 | MVT::i32, SDValue(Int, 0)); |
| Colin LeMahieu | 4af437f | 2014-12-09 20:23:30 +0000 | [diff] [blame] | 853 | SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A2_tfrsi, dl, |
| Krzysztof Parzyszek | 317d42c | 2016-08-01 20:31:50 +0000 | [diff] [blame] | 854 | MVT::i32, TargetConst0); |
| Colin LeMahieu | b580d7d | 2014-12-09 19:23:45 +0000 | [diff] [blame] | 855 | SDNode *Result_3 = CurDAG->getMachineNode(Hexagon::A2_combinew, dl, |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 856 | MVT::i64, MVT::Other, |
| 857 | SDValue(Result_2, 0), |
| 858 | SDValue(Result_1, 0)); |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 859 | ReplaceNode(N, Result_3); |
| 860 | return; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 861 | } |
| 862 | if (N->getValueType(0) == MVT::i32) { |
| 863 | // Convert the zero_extend to Rs = Pd |
| Colin LeMahieu | 30dcb23 | 2014-12-09 18:16:49 +0000 | [diff] [blame] | 864 | SDNode* RsPd = CurDAG->getMachineNode(Hexagon::C2_tfrpr, dl, |
| Krzysztof Parzyszek | 317d42c | 2016-08-01 20:31:50 +0000 | [diff] [blame] | 865 | MVT::i32, SDValue(Int, 0)); |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 866 | ReplaceNode(N, RsPd); |
| 867 | return; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 868 | } |
| Craig Topper | e55c556 | 2012-02-07 02:50:20 +0000 | [diff] [blame] | 869 | llvm_unreachable("Unexpected value type"); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 870 | } |
| 871 | } |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 872 | SelectCode(N); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 873 | } |
| 874 | |
| Krzysztof Parzyszek | 7d5b4db | 2016-02-12 17:01:51 +0000 | [diff] [blame] | 875 | |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 876 | // |
| Krzysztof Parzyszek | 7d5b4db | 2016-02-12 17:01:51 +0000 | [diff] [blame] | 877 | // Handling intrinsics for circular load and bitreverse load. |
| Krzysztof Parzyszek | 47ab1f2 | 2015-03-18 16:23:44 +0000 | [diff] [blame] | 878 | // |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 879 | void HexagonDAGToDAGISel::SelectIntrinsicWChain(SDNode *N) { |
| 880 | if (MachineSDNode *L = LoadInstrForLoadIntrinsic(N)) { |
| 881 | StoreInstrForLoadIntrinsic(L, N); |
| Krzysztof Parzyszek | 0f791f4 | 2016-05-13 18:48:15 +0000 | [diff] [blame] | 882 | CurDAG->RemoveDeadNode(N); |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 883 | return; |
| 884 | } |
| 885 | SelectCode(N); |
| Krzysztof Parzyszek | 47ab1f2 | 2015-03-18 16:23:44 +0000 | [diff] [blame] | 886 | } |
| 887 | |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 888 | void HexagonDAGToDAGISel::SelectIntrinsicWOChain(SDNode *N) { |
| Colin LeMahieu | 0ee02fc | 2015-01-19 20:31:18 +0000 | [diff] [blame] | 889 | unsigned IID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue(); |
| 890 | unsigned Bits; |
| 891 | switch (IID) { |
| 892 | case Intrinsic::hexagon_S2_vsplatrb: |
| 893 | Bits = 8; |
| 894 | break; |
| 895 | case Intrinsic::hexagon_S2_vsplatrh: |
| 896 | Bits = 16; |
| 897 | break; |
| 898 | default: |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 899 | SelectCode(N); |
| 900 | return; |
| Colin LeMahieu | 0ee02fc | 2015-01-19 20:31:18 +0000 | [diff] [blame] | 901 | } |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 902 | |
| Krzysztof Parzyszek | e60e5fe | 2016-05-12 17:21:40 +0000 | [diff] [blame] | 903 | SDValue V = N->getOperand(1); |
| Colin LeMahieu | 0ee02fc | 2015-01-19 20:31:18 +0000 | [diff] [blame] | 904 | SDValue U; |
| 905 | if (isValueExtension(V, Bits, U)) { |
| 906 | SDValue R = CurDAG->getNode(N->getOpcode(), SDLoc(N), N->getValueType(0), |
| Krzysztof Parzyszek | e60e5fe | 2016-05-12 17:21:40 +0000 | [diff] [blame] | 907 | N->getOperand(0), U); |
| Justin Bogner | d82025b | 2016-05-12 21:24:23 +0000 | [diff] [blame] | 908 | ReplaceNode(N, R.getNode()); |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 909 | SelectCode(R.getNode()); |
| 910 | return; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 911 | } |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 912 | SelectCode(N); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 913 | } |
| 914 | |
| Sirish Pande | 69295b8 | 2012-05-10 20:20:25 +0000 | [diff] [blame] | 915 | // |
| 916 | // Map floating point constant values. |
| 917 | // |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 918 | void HexagonDAGToDAGISel::SelectConstantFP(SDNode *N) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 919 | SDLoc dl(N); |
| Sirish Pande | 69295b8 | 2012-05-10 20:20:25 +0000 | [diff] [blame] | 920 | ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N); |
| Krzysztof Parzyszek | a338650 | 2016-08-10 16:46:36 +0000 | [diff] [blame] | 921 | APInt A = CN->getValueAPF().bitcastToAPInt(); |
| Sirish Pande | 69295b8 | 2012-05-10 20:20:25 +0000 | [diff] [blame] | 922 | if (N->getValueType(0) == MVT::f32) { |
| Krzysztof Parzyszek | a338650 | 2016-08-10 16:46:36 +0000 | [diff] [blame] | 923 | SDValue V = CurDAG->getTargetConstant(A.getZExtValue(), dl, MVT::i32); |
| 924 | ReplaceNode(N, CurDAG->getMachineNode(Hexagon::A2_tfrsi, dl, MVT::f32, V)); |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 925 | return; |
| Sirish Pande | 69295b8 | 2012-05-10 20:20:25 +0000 | [diff] [blame] | 926 | } |
| Krzysztof Parzyszek | 317d42c | 2016-08-01 20:31:50 +0000 | [diff] [blame] | 927 | if (N->getValueType(0) == MVT::f64) { |
| Krzysztof Parzyszek | a338650 | 2016-08-10 16:46:36 +0000 | [diff] [blame] | 928 | SDValue V = CurDAG->getTargetConstant(A.getZExtValue(), dl, MVT::i64); |
| 929 | ReplaceNode(N, CurDAG->getMachineNode(Hexagon::CONST64, dl, MVT::f64, V)); |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 930 | return; |
| Sirish Pande | 69295b8 | 2012-05-10 20:20:25 +0000 | [diff] [blame] | 931 | } |
| 932 | |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 933 | SelectCode(N); |
| Sirish Pande | 69295b8 | 2012-05-10 20:20:25 +0000 | [diff] [blame] | 934 | } |
| 935 | |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 936 | // |
| Krzysztof Parzyszek | 1d01a79 | 2016-08-16 18:08:40 +0000 | [diff] [blame] | 937 | // Map boolean values. |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 938 | // |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 939 | void HexagonDAGToDAGISel::SelectConstant(SDNode *N) { |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 940 | if (N->getValueType(0) == MVT::i1) { |
| Krzysztof Parzyszek | 1d01a79 | 2016-08-16 18:08:40 +0000 | [diff] [blame] | 941 | assert(!(cast<ConstantSDNode>(N)->getZExtValue() >> 1)); |
| 942 | unsigned Opc = (cast<ConstantSDNode>(N)->getSExtValue() != 0) |
| 943 | ? Hexagon::PS_true |
| 944 | : Hexagon::PS_false; |
| 945 | ReplaceNode(N, CurDAG->getMachineNode(Opc, SDLoc(N), MVT::i1)); |
| 946 | return; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 947 | } |
| 948 | |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 949 | SelectCode(N); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | |
| 953 | // |
| 954 | // Map add followed by a asr -> asr +=. |
| 955 | // |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 956 | void HexagonDAGToDAGISel::SelectAdd(SDNode *N) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 957 | SDLoc dl(N); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 958 | if (N->getValueType(0) != MVT::i32) { |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 959 | SelectCode(N); |
| 960 | return; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 961 | } |
| 962 | // Identify nodes of the form: add(asr(...)). |
| 963 | SDNode* Src1 = N->getOperand(0).getNode(); |
| Krzysztof Parzyszek | 317d42c | 2016-08-01 20:31:50 +0000 | [diff] [blame] | 964 | if (Src1->getOpcode() != ISD::SRA || !Src1->hasOneUse() || |
| 965 | Src1->getValueType(0) != MVT::i32) { |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 966 | SelectCode(N); |
| 967 | return; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 968 | } |
| 969 | |
| 970 | // Build Rd = Rd' + asr(Rs, Rt). The machine constraints will ensure that |
| 971 | // Rd and Rd' are assigned to the same register |
| Colin LeMahieu | 0f850bd | 2014-12-19 20:29:29 +0000 | [diff] [blame] | 972 | 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] | 973 | N->getOperand(1), |
| 974 | Src1->getOperand(0), |
| 975 | Src1->getOperand(1)); |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 976 | ReplaceNode(N, Result); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 977 | } |
| 978 | |
| Krzysztof Parzyszek | 8c1cab9 | 2015-03-18 00:43:46 +0000 | [diff] [blame] | 979 | // |
| 980 | // Map the following, where possible. |
| 981 | // AND/FABS -> clrbit |
| 982 | // OR -> setbit |
| 983 | // XOR/FNEG ->toggle_bit. |
| 984 | // |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 985 | void HexagonDAGToDAGISel::SelectBitOp(SDNode *N) { |
| Krzysztof Parzyszek | 8c1cab9 | 2015-03-18 00:43:46 +0000 | [diff] [blame] | 986 | SDLoc dl(N); |
| 987 | EVT ValueVT = N->getValueType(0); |
| 988 | |
| 989 | // We handle only 32 and 64-bit bit ops. |
| 990 | if (!(ValueVT == MVT::i32 || ValueVT == MVT::i64 || |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 991 | ValueVT == MVT::f32 || ValueVT == MVT::f64)) { |
| 992 | SelectCode(N); |
| 993 | return; |
| 994 | } |
| Krzysztof Parzyszek | 8c1cab9 | 2015-03-18 00:43:46 +0000 | [diff] [blame] | 995 | |
| 996 | // We handly only fabs and fneg for V5. |
| 997 | unsigned Opc = N->getOpcode(); |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 998 | if ((Opc == ISD::FABS || Opc == ISD::FNEG) && !HST->hasV5TOps()) { |
| 999 | SelectCode(N); |
| 1000 | return; |
| 1001 | } |
| Krzysztof Parzyszek | 8c1cab9 | 2015-03-18 00:43:46 +0000 | [diff] [blame] | 1002 | |
| 1003 | int64_t Val = 0; |
| 1004 | if (Opc != ISD::FABS && Opc != ISD::FNEG) { |
| 1005 | if (N->getOperand(1).getOpcode() == ISD::Constant) |
| 1006 | Val = cast<ConstantSDNode>((N)->getOperand(1))->getSExtValue(); |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 1007 | else { |
| Krzysztof Parzyszek | fb4c417 | 2016-08-19 19:29:15 +0000 | [diff] [blame] | 1008 | SelectCode(N); |
| 1009 | return; |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 1010 | } |
| Krzysztof Parzyszek | 8c1cab9 | 2015-03-18 00:43:46 +0000 | [diff] [blame] | 1011 | } |
| 1012 | |
| 1013 | if (Opc == ISD::AND) { |
| Krzysztof Parzyszek | 08ff888 | 2015-11-26 18:38:27 +0000 | [diff] [blame] | 1014 | // Check if this is a bit-clearing AND, if not select code the usual way. |
| 1015 | if ((ValueVT == MVT::i32 && isPowerOf2_32(~Val)) || |
| 1016 | (ValueVT == MVT::i64 && isPowerOf2_64(~Val))) |
| Krzysztof Parzyszek | 8c1cab9 | 2015-03-18 00:43:46 +0000 | [diff] [blame] | 1017 | Val = ~Val; |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 1018 | else { |
| 1019 | SelectCode(N); |
| 1020 | return; |
| 1021 | } |
| Krzysztof Parzyszek | 8c1cab9 | 2015-03-18 00:43:46 +0000 | [diff] [blame] | 1022 | } |
| 1023 | |
| 1024 | // If OR or AND is being fed by shl, srl and, sra don't do this change, |
| 1025 | // because Hexagon provide |= &= on shl, srl, and sra. |
| 1026 | // Traverse the DAG to see if there is shl, srl and sra. |
| 1027 | if (Opc == ISD::OR || Opc == ISD::AND) { |
| 1028 | switch (N->getOperand(0)->getOpcode()) { |
| Krzysztof Parzyszek | 08ff888 | 2015-11-26 18:38:27 +0000 | [diff] [blame] | 1029 | default: |
| 1030 | break; |
| Krzysztof Parzyszek | 8c1cab9 | 2015-03-18 00:43:46 +0000 | [diff] [blame] | 1031 | case ISD::SRA: |
| 1032 | case ISD::SRL: |
| 1033 | case ISD::SHL: |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 1034 | SelectCode(N); |
| 1035 | return; |
| Krzysztof Parzyszek | 8c1cab9 | 2015-03-18 00:43:46 +0000 | [diff] [blame] | 1036 | } |
| 1037 | } |
| 1038 | |
| 1039 | // Make sure it's power of 2. |
| Krzysztof Parzyszek | 08ff888 | 2015-11-26 18:38:27 +0000 | [diff] [blame] | 1040 | unsigned BitPos = 0; |
| Krzysztof Parzyszek | 8c1cab9 | 2015-03-18 00:43:46 +0000 | [diff] [blame] | 1041 | if (Opc != ISD::FABS && Opc != ISD::FNEG) { |
| Krzysztof Parzyszek | 08ff888 | 2015-11-26 18:38:27 +0000 | [diff] [blame] | 1042 | if ((ValueVT == MVT::i32 && !isPowerOf2_32(Val)) || |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 1043 | (ValueVT == MVT::i64 && !isPowerOf2_64(Val))) { |
| 1044 | SelectCode(N); |
| 1045 | return; |
| 1046 | } |
| Krzysztof Parzyszek | 8c1cab9 | 2015-03-18 00:43:46 +0000 | [diff] [blame] | 1047 | |
| 1048 | // Get the bit position. |
| Krzysztof Parzyszek | 08ff888 | 2015-11-26 18:38:27 +0000 | [diff] [blame] | 1049 | BitPos = countTrailingZeros(uint64_t(Val)); |
| Krzysztof Parzyszek | 8c1cab9 | 2015-03-18 00:43:46 +0000 | [diff] [blame] | 1050 | } else { |
| 1051 | // For fabs and fneg, it's always the 31st bit. |
| Krzysztof Parzyszek | 08ff888 | 2015-11-26 18:38:27 +0000 | [diff] [blame] | 1052 | BitPos = 31; |
| Krzysztof Parzyszek | 8c1cab9 | 2015-03-18 00:43:46 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | unsigned BitOpc = 0; |
| 1056 | // Set the right opcode for bitwise operations. |
| Krzysztof Parzyszek | 08ff888 | 2015-11-26 18:38:27 +0000 | [diff] [blame] | 1057 | switch (Opc) { |
| 1058 | default: |
| 1059 | llvm_unreachable("Only bit-wise/abs/neg operations are allowed."); |
| Krzysztof Parzyszek | 8c1cab9 | 2015-03-18 00:43:46 +0000 | [diff] [blame] | 1060 | case ISD::AND: |
| 1061 | case ISD::FABS: |
| 1062 | BitOpc = Hexagon::S2_clrbit_i; |
| 1063 | break; |
| 1064 | case ISD::OR: |
| 1065 | BitOpc = Hexagon::S2_setbit_i; |
| 1066 | break; |
| 1067 | case ISD::XOR: |
| 1068 | case ISD::FNEG: |
| 1069 | BitOpc = Hexagon::S2_togglebit_i; |
| 1070 | break; |
| 1071 | } |
| 1072 | |
| 1073 | SDNode *Result; |
| 1074 | // Get the right SDVal for the opcode. |
| Krzysztof Parzyszek | 08ff888 | 2015-11-26 18:38:27 +0000 | [diff] [blame] | 1075 | SDValue SDVal = CurDAG->getTargetConstant(BitPos, dl, MVT::i32); |
| Krzysztof Parzyszek | 8c1cab9 | 2015-03-18 00:43:46 +0000 | [diff] [blame] | 1076 | |
| 1077 | if (ValueVT == MVT::i32 || ValueVT == MVT::f32) { |
| 1078 | Result = CurDAG->getMachineNode(BitOpc, dl, ValueVT, |
| 1079 | N->getOperand(0), SDVal); |
| 1080 | } else { |
| 1081 | // 64-bit gymnastic to use REG_SEQUENCE. But it's worth it. |
| 1082 | EVT SubValueVT; |
| 1083 | if (ValueVT == MVT::i64) |
| 1084 | SubValueVT = MVT::i32; |
| 1085 | else |
| 1086 | SubValueVT = MVT::f32; |
| 1087 | |
| 1088 | SDNode *Reg = N->getOperand(0).getNode(); |
| 1089 | SDValue RegClass = CurDAG->getTargetConstant(Hexagon::DoubleRegsRegClassID, |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1090 | dl, MVT::i64); |
| Krzysztof Parzyszek | 8c1cab9 | 2015-03-18 00:43:46 +0000 | [diff] [blame] | 1091 | |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1092 | SDValue SubregHiIdx = CurDAG->getTargetConstant(Hexagon::subreg_hireg, dl, |
| Krzysztof Parzyszek | 8c1cab9 | 2015-03-18 00:43:46 +0000 | [diff] [blame] | 1093 | MVT::i32); |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1094 | SDValue SubregLoIdx = CurDAG->getTargetConstant(Hexagon::subreg_loreg, dl, |
| Krzysztof Parzyszek | 8c1cab9 | 2015-03-18 00:43:46 +0000 | [diff] [blame] | 1095 | MVT::i32); |
| 1096 | |
| 1097 | SDValue SubregHI = CurDAG->getTargetExtractSubreg(Hexagon::subreg_hireg, dl, |
| 1098 | MVT::i32, SDValue(Reg, 0)); |
| 1099 | |
| 1100 | SDValue SubregLO = CurDAG->getTargetExtractSubreg(Hexagon::subreg_loreg, dl, |
| 1101 | MVT::i32, SDValue(Reg, 0)); |
| 1102 | |
| 1103 | // Clear/set/toggle hi or lo registers depending on the bit position. |
| Krzysztof Parzyszek | 08ff888 | 2015-11-26 18:38:27 +0000 | [diff] [blame] | 1104 | if (SubValueVT != MVT::f32 && BitPos < 32) { |
| Krzysztof Parzyszek | 8c1cab9 | 2015-03-18 00:43:46 +0000 | [diff] [blame] | 1105 | SDNode *Result0 = CurDAG->getMachineNode(BitOpc, dl, SubValueVT, |
| 1106 | SubregLO, SDVal); |
| 1107 | const SDValue Ops[] = { RegClass, SubregHI, SubregHiIdx, |
| 1108 | SDValue(Result0, 0), SubregLoIdx }; |
| 1109 | Result = CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, |
| 1110 | dl, ValueVT, Ops); |
| 1111 | } else { |
| 1112 | if (Opc != ISD::FABS && Opc != ISD::FNEG) |
| Krzysztof Parzyszek | 08ff888 | 2015-11-26 18:38:27 +0000 | [diff] [blame] | 1113 | SDVal = CurDAG->getTargetConstant(BitPos-32, dl, MVT::i32); |
| Krzysztof Parzyszek | 8c1cab9 | 2015-03-18 00:43:46 +0000 | [diff] [blame] | 1114 | SDNode *Result0 = CurDAG->getMachineNode(BitOpc, dl, SubValueVT, |
| 1115 | SubregHI, SDVal); |
| 1116 | const SDValue Ops[] = { RegClass, SDValue(Result0, 0), SubregHiIdx, |
| 1117 | SubregLO, SubregLoIdx }; |
| 1118 | Result = CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, |
| 1119 | dl, ValueVT, Ops); |
| 1120 | } |
| 1121 | } |
| 1122 | |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 1123 | ReplaceNode(N, Result); |
| Krzysztof Parzyszek | 8c1cab9 | 2015-03-18 00:43:46 +0000 | [diff] [blame] | 1124 | } |
| 1125 | |
| 1126 | |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 1127 | void HexagonDAGToDAGISel::SelectFrameIndex(SDNode *N) { |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1128 | MachineFrameInfo &MFI = MF->getFrameInfo(); |
| Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1129 | const HexagonFrameLowering *HFI = HST->getFrameLowering(); |
| Krzysztof Parzyszek | a29622a | 2015-03-12 16:44:50 +0000 | [diff] [blame] | 1130 | int FX = cast<FrameIndexSDNode>(N)->getIndex(); |
| Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1131 | unsigned StkA = HFI->getStackAlignment(); |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1132 | unsigned MaxA = MFI.getMaxAlignment(); |
| Krzysztof Parzyszek | a29622a | 2015-03-12 16:44:50 +0000 | [diff] [blame] | 1133 | SDValue FI = CurDAG->getTargetFrameIndex(FX, MVT::i32); |
| Krzysztof Parzyszek | a29622a | 2015-03-12 16:44:50 +0000 | [diff] [blame] | 1134 | SDLoc DL(N); |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1135 | SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32); |
| Krzysztof Parzyszek | 317d42c | 2016-08-01 20:31:50 +0000 | [diff] [blame] | 1136 | SDNode *R = nullptr; |
| Krzysztof Parzyszek | a29622a | 2015-03-12 16:44:50 +0000 | [diff] [blame] | 1137 | |
| Krzysztof Parzyszek | 1d01a79 | 2016-08-16 18:08:40 +0000 | [diff] [blame] | 1138 | // Use PS_fi when: |
| Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1139 | // - the object is fixed, or |
| 1140 | // - there are no objects with higher-than-default alignment, or |
| 1141 | // - there are no dynamically allocated objects. |
| Krzysztof Parzyszek | 1d01a79 | 2016-08-16 18:08:40 +0000 | [diff] [blame] | 1142 | // Otherwise, use PS_fia. |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1143 | if (FX < 0 || MaxA <= StkA || !MFI.hasVarSizedObjects()) { |
| Krzysztof Parzyszek | 1d01a79 | 2016-08-16 18:08:40 +0000 | [diff] [blame] | 1144 | R = CurDAG->getMachineNode(Hexagon::PS_fi, DL, MVT::i32, FI, Zero); |
| Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1145 | } else { |
| 1146 | auto &HMFI = *MF->getInfo<HexagonMachineFunctionInfo>(); |
| 1147 | unsigned AR = HMFI.getStackAlignBaseVReg(); |
| 1148 | SDValue CH = CurDAG->getEntryNode(); |
| 1149 | SDValue Ops[] = { CurDAG->getCopyFromReg(CH, DL, AR, MVT::i32), FI, Zero }; |
| Krzysztof Parzyszek | 1d01a79 | 2016-08-16 18:08:40 +0000 | [diff] [blame] | 1150 | R = CurDAG->getMachineNode(Hexagon::PS_fia, DL, MVT::i32, Ops); |
| Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1151 | } |
| Krzysztof Parzyszek | a29622a | 2015-03-12 16:44:50 +0000 | [diff] [blame] | 1152 | |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 1153 | ReplaceNode(N, R); |
| Krzysztof Parzyszek | a29622a | 2015-03-12 16:44:50 +0000 | [diff] [blame] | 1154 | } |
| 1155 | |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1156 | |
| Krzysztof Parzyszek | 5da24e5 | 2016-06-27 15:08:22 +0000 | [diff] [blame] | 1157 | void HexagonDAGToDAGISel::SelectBitcast(SDNode *N) { |
| 1158 | EVT SVT = N->getOperand(0).getValueType(); |
| 1159 | EVT DVT = N->getValueType(0); |
| 1160 | if (!SVT.isVector() || !DVT.isVector() || |
| 1161 | SVT.getVectorElementType() == MVT::i1 || |
| 1162 | DVT.getVectorElementType() == MVT::i1 || |
| 1163 | SVT.getSizeInBits() != DVT.getSizeInBits()) { |
| 1164 | SelectCode(N); |
| 1165 | return; |
| 1166 | } |
| 1167 | |
| 1168 | CurDAG->ReplaceAllUsesOfValueWith(SDValue(N,0), N->getOperand(0)); |
| 1169 | CurDAG->RemoveDeadNode(N); |
| 1170 | } |
| 1171 | |
| 1172 | |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 1173 | void HexagonDAGToDAGISel::Select(SDNode *N) { |
| Tim Northover | 31d093c | 2013-09-22 08:21:56 +0000 | [diff] [blame] | 1174 | if (N->isMachineOpcode()) { |
| 1175 | N->setNodeId(-1); |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 1176 | return; // Already selected. |
| Tim Northover | 31d093c | 2013-09-22 08:21:56 +0000 | [diff] [blame] | 1177 | } |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1178 | |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1179 | switch (N->getOpcode()) { |
| 1180 | case ISD::Constant: |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 1181 | SelectConstant(N); |
| 1182 | return; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1183 | |
| Sirish Pande | 69295b8 | 2012-05-10 20:20:25 +0000 | [diff] [blame] | 1184 | case ISD::ConstantFP: |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 1185 | SelectConstantFP(N); |
| 1186 | return; |
| Sirish Pande | 69295b8 | 2012-05-10 20:20:25 +0000 | [diff] [blame] | 1187 | |
| Krzysztof Parzyszek | a29622a | 2015-03-12 16:44:50 +0000 | [diff] [blame] | 1188 | case ISD::FrameIndex: |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 1189 | SelectFrameIndex(N); |
| 1190 | return; |
| Krzysztof Parzyszek | a29622a | 2015-03-12 16:44:50 +0000 | [diff] [blame] | 1191 | |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1192 | case ISD::ADD: |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 1193 | SelectAdd(N); |
| 1194 | return; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1195 | |
| Krzysztof Parzyszek | 5da24e5 | 2016-06-27 15:08:22 +0000 | [diff] [blame] | 1196 | case ISD::BITCAST: |
| 1197 | SelectBitcast(N); |
| 1198 | return; |
| 1199 | |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1200 | case ISD::SHL: |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 1201 | SelectSHL(N); |
| 1202 | return; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1203 | |
| 1204 | case ISD::LOAD: |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 1205 | SelectLoad(N); |
| 1206 | return; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1207 | |
| 1208 | case ISD::STORE: |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 1209 | SelectStore(N); |
| 1210 | return; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1211 | |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1212 | case ISD::MUL: |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 1213 | SelectMul(N); |
| 1214 | return; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1215 | |
| Krzysztof Parzyszek | 8c1cab9 | 2015-03-18 00:43:46 +0000 | [diff] [blame] | 1216 | case ISD::AND: |
| 1217 | case ISD::OR: |
| 1218 | case ISD::XOR: |
| 1219 | case ISD::FABS: |
| 1220 | case ISD::FNEG: |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 1221 | SelectBitOp(N); |
| 1222 | return; |
| Krzysztof Parzyszek | 8c1cab9 | 2015-03-18 00:43:46 +0000 | [diff] [blame] | 1223 | |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1224 | case ISD::ZERO_EXTEND: |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 1225 | SelectZeroExtend(N); |
| 1226 | return; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1227 | |
| Krzysztof Parzyszek | 47ab1f2 | 2015-03-18 16:23:44 +0000 | [diff] [blame] | 1228 | case ISD::INTRINSIC_W_CHAIN: |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 1229 | SelectIntrinsicWChain(N); |
| 1230 | return; |
| Krzysztof Parzyszek | 47ab1f2 | 2015-03-18 16:23:44 +0000 | [diff] [blame] | 1231 | |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1232 | case ISD::INTRINSIC_WO_CHAIN: |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 1233 | SelectIntrinsicWOChain(N); |
| 1234 | return; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1235 | } |
| 1236 | |
| Justin Bogner | ec37a02 | 2016-05-12 21:46:18 +0000 | [diff] [blame] | 1237 | SelectCode(N); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1238 | } |
| 1239 | |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1240 | bool HexagonDAGToDAGISel:: |
| Daniel Sanders | 60f1db0 | 2015-03-13 12:45:09 +0000 | [diff] [blame] | 1241 | SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID, |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1242 | std::vector<SDValue> &OutOps) { |
| Krzysztof Parzyszek | a29622a | 2015-03-12 16:44:50 +0000 | [diff] [blame] | 1243 | SDValue Inp = Op, Res; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1244 | |
| Daniel Sanders | 60f1db0 | 2015-03-13 12:45:09 +0000 | [diff] [blame] | 1245 | switch (ConstraintID) { |
| Krzysztof Parzyszek | a29622a | 2015-03-12 16:44:50 +0000 | [diff] [blame] | 1246 | default: |
| 1247 | return true; |
| Daniel Sanders | 49f643c | 2015-03-17 14:37:39 +0000 | [diff] [blame] | 1248 | case InlineAsm::Constraint_i: |
| 1249 | case InlineAsm::Constraint_o: // Offsetable. |
| 1250 | case InlineAsm::Constraint_v: // Not offsetable. |
| 1251 | case InlineAsm::Constraint_m: // Memory. |
| Krzysztof Parzyszek | a29622a | 2015-03-12 16:44:50 +0000 | [diff] [blame] | 1252 | if (SelectAddrFI(Inp, Res)) |
| 1253 | OutOps.push_back(Res); |
| 1254 | else |
| 1255 | OutOps.push_back(Inp); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1256 | break; |
| 1257 | } |
| 1258 | |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1259 | OutOps.push_back(CurDAG->getTargetConstant(0, SDLoc(Op), MVT::i32)); |
| Jyotsna Verma | d922524 | 2013-02-13 21:38:46 +0000 | [diff] [blame] | 1260 | return false; |
| 1261 | } |
| Colin LeMahieu | c7522f3 | 2015-01-14 23:07:36 +0000 | [diff] [blame] | 1262 | |
| Colin LeMahieu | 79ec065 | 2015-06-12 19:57:32 +0000 | [diff] [blame] | 1263 | |
| Krzysztof Parzyszek | ae14e7b | 2015-03-17 21:47:16 +0000 | [diff] [blame] | 1264 | void HexagonDAGToDAGISel::PreprocessISelDAG() { |
| 1265 | SelectionDAG &DAG = *CurDAG; |
| 1266 | std::vector<SDNode*> Nodes; |
| Pete Cooper | 7e64ef0 | 2015-07-14 23:43:29 +0000 | [diff] [blame] | 1267 | for (SDNode &Node : DAG.allnodes()) |
| 1268 | Nodes.push_back(&Node); |
| Krzysztof Parzyszek | ae14e7b | 2015-03-17 21:47:16 +0000 | [diff] [blame] | 1269 | |
| 1270 | // Simplify: (or (select c x 0) z) -> (select c (or x z) z) |
| 1271 | // (or (select c 0 y) z) -> (select c z (or y z)) |
| 1272 | // 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] | 1273 | for (auto I : Nodes) { |
| Krzysztof Parzyszek | ae14e7b | 2015-03-17 21:47:16 +0000 | [diff] [blame] | 1274 | if (I->getOpcode() != ISD::OR) |
| 1275 | continue; |
| 1276 | |
| 1277 | auto IsZero = [] (const SDValue &V) -> bool { |
| 1278 | if (ConstantSDNode *SC = dyn_cast<ConstantSDNode>(V.getNode())) |
| 1279 | return SC->isNullValue(); |
| 1280 | return false; |
| 1281 | }; |
| 1282 | auto IsSelect0 = [IsZero] (const SDValue &Op) -> bool { |
| 1283 | if (Op.getOpcode() != ISD::SELECT) |
| 1284 | return false; |
| Krzysztof Parzyszek | 7d5b4db | 2016-02-12 17:01:51 +0000 | [diff] [blame] | 1285 | return IsZero(Op.getOperand(1)) || IsZero(Op.getOperand(2)); |
| Krzysztof Parzyszek | ae14e7b | 2015-03-17 21:47:16 +0000 | [diff] [blame] | 1286 | }; |
| 1287 | |
| 1288 | SDValue N0 = I->getOperand(0), N1 = I->getOperand(1); |
| 1289 | EVT VT = I->getValueType(0); |
| 1290 | bool SelN0 = IsSelect0(N0); |
| 1291 | SDValue SOp = SelN0 ? N0 : N1; |
| 1292 | SDValue VOp = SelN0 ? N1 : N0; |
| 1293 | |
| 1294 | if (SOp.getOpcode() == ISD::SELECT && SOp.getNode()->hasOneUse()) { |
| 1295 | SDValue SC = SOp.getOperand(0); |
| 1296 | SDValue SX = SOp.getOperand(1); |
| 1297 | SDValue SY = SOp.getOperand(2); |
| 1298 | SDLoc DLS = SOp; |
| 1299 | if (IsZero(SY)) { |
| 1300 | SDValue NewOr = DAG.getNode(ISD::OR, DLS, VT, SX, VOp); |
| 1301 | SDValue NewSel = DAG.getNode(ISD::SELECT, DLS, VT, SC, NewOr, VOp); |
| 1302 | DAG.ReplaceAllUsesWith(I, NewSel.getNode()); |
| 1303 | } else if (IsZero(SX)) { |
| 1304 | SDValue NewOr = DAG.getNode(ISD::OR, DLS, VT, SY, VOp); |
| 1305 | SDValue NewSel = DAG.getNode(ISD::SELECT, DLS, VT, SC, VOp, NewOr); |
| 1306 | DAG.ReplaceAllUsesWith(I, NewSel.getNode()); |
| 1307 | } |
| 1308 | } |
| 1309 | } |
| Krzysztof Parzyszek | f7f7068 | 2016-06-22 20:08:27 +0000 | [diff] [blame] | 1310 | |
| 1311 | // Transform: (store ch addr (add x (add (shl y c) e))) |
| 1312 | // to: (store ch addr (add x (shl (add y d) c))), |
| 1313 | // where e = (shl d c) for some integer d. |
| 1314 | // The purpose of this is to enable generation of loads/stores with |
| 1315 | // shifted addressing mode, i.e. mem(x+y<<#c). For that, the shift |
| 1316 | // value c must be 0, 1 or 2. |
| 1317 | for (auto I : Nodes) { |
| 1318 | if (I->getOpcode() != ISD::STORE) |
| 1319 | continue; |
| 1320 | |
| 1321 | // I matched: (store ch addr Off) |
| 1322 | SDValue Off = I->getOperand(2); |
| 1323 | // Off needs to match: (add x (add (shl y c) (shl d c)))) |
| 1324 | if (Off.getOpcode() != ISD::ADD) |
| 1325 | continue; |
| 1326 | // Off matched: (add x T0) |
| 1327 | SDValue T0 = Off.getOperand(1); |
| 1328 | // T0 needs to match: (add T1 T2): |
| 1329 | if (T0.getOpcode() != ISD::ADD) |
| 1330 | continue; |
| 1331 | // T0 matched: (add T1 T2) |
| 1332 | SDValue T1 = T0.getOperand(0); |
| 1333 | SDValue T2 = T0.getOperand(1); |
| 1334 | // T1 needs to match: (shl y c) |
| 1335 | if (T1.getOpcode() != ISD::SHL) |
| 1336 | continue; |
| 1337 | SDValue C = T1.getOperand(1); |
| 1338 | ConstantSDNode *CN = dyn_cast<ConstantSDNode>(C.getNode()); |
| 1339 | if (CN == nullptr) |
| 1340 | continue; |
| 1341 | unsigned CV = CN->getZExtValue(); |
| 1342 | if (CV > 2) |
| 1343 | continue; |
| 1344 | // T2 needs to match e, where e = (shl d c) for some d. |
| 1345 | ConstantSDNode *EN = dyn_cast<ConstantSDNode>(T2.getNode()); |
| 1346 | if (EN == nullptr) |
| 1347 | continue; |
| 1348 | unsigned EV = EN->getZExtValue(); |
| 1349 | if (EV % (1 << CV) != 0) |
| 1350 | continue; |
| 1351 | unsigned DV = EV / (1 << CV); |
| 1352 | |
| 1353 | // Replace T0 with: (shl (add y d) c) |
| 1354 | SDLoc DL = SDLoc(I); |
| 1355 | EVT VT = T0.getValueType(); |
| 1356 | SDValue D = DAG.getConstant(DV, DL, VT); |
| 1357 | // NewAdd = (add y d) |
| 1358 | SDValue NewAdd = DAG.getNode(ISD::ADD, DL, VT, T1.getOperand(0), D); |
| 1359 | // NewShl = (shl NewAdd c) |
| 1360 | SDValue NewShl = DAG.getNode(ISD::SHL, DL, VT, NewAdd, C); |
| 1361 | ReplaceNode(T0.getNode(), NewShl.getNode()); |
| 1362 | } |
| Krzysztof Parzyszek | 0006e1a | 2016-07-29 15:15:35 +0000 | [diff] [blame] | 1363 | |
| 1364 | if (EnableAddressRebalancing) { |
| 1365 | rebalanceAddressTrees(); |
| 1366 | |
| 1367 | DEBUG( |
| 1368 | dbgs() << "************* SelectionDAG after preprocessing: ***********\n"; |
| 1369 | CurDAG->dump(); |
| 1370 | dbgs() << "************* End SelectionDAG after preprocessing ********\n"; |
| 1371 | ); |
| 1372 | } |
| Krzysztof Parzyszek | ae14e7b | 2015-03-17 21:47:16 +0000 | [diff] [blame] | 1373 | } |
| 1374 | |
| Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1375 | void HexagonDAGToDAGISel::EmitFunctionEntryCode() { |
| 1376 | auto &HST = static_cast<const HexagonSubtarget&>(MF->getSubtarget()); |
| 1377 | auto &HFI = *HST.getFrameLowering(); |
| 1378 | if (!HFI.needsAligna(*MF)) |
| 1379 | return; |
| Krzysztof Parzyszek | ae14e7b | 2015-03-17 21:47:16 +0000 | [diff] [blame] | 1380 | |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1381 | MachineFrameInfo &MFI = MF->getFrameInfo(); |
| Duncan P. N. Exon Smith | a72c6e2 | 2015-10-20 00:46:39 +0000 | [diff] [blame] | 1382 | MachineBasicBlock *EntryBB = &MF->front(); |
| Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1383 | unsigned AR = FuncInfo->CreateReg(MVT::i32); |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1384 | unsigned MaxA = MFI.getMaxAlignment(); |
| Krzysztof Parzyszek | 1d01a79 | 2016-08-16 18:08:40 +0000 | [diff] [blame] | 1385 | BuildMI(EntryBB, DebugLoc(), HII->get(Hexagon::PS_aligna), AR) |
| Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1386 | .addImm(MaxA); |
| 1387 | MF->getInfo<HexagonMachineFunctionInfo>()->setStackAlignBaseVReg(AR); |
| 1388 | } |
| 1389 | |
| 1390 | // Match a frame index that can be used in an addressing mode. |
| Colin LeMahieu | c7522f3 | 2015-01-14 23:07:36 +0000 | [diff] [blame] | 1391 | bool HexagonDAGToDAGISel::SelectAddrFI(SDValue& N, SDValue &R) { |
| 1392 | if (N.getOpcode() != ISD::FrameIndex) |
| 1393 | return false; |
| Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1394 | auto &HFI = *HST->getFrameLowering(); |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1395 | MachineFrameInfo &MFI = MF->getFrameInfo(); |
| Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1396 | int FX = cast<FrameIndexSDNode>(N)->getIndex(); |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1397 | if (!MFI.isFixedObjectIndex(FX) && HFI.needsAligna(*MF)) |
| Krzysztof Parzyszek | 4fa2a9f | 2015-04-22 16:43:53 +0000 | [diff] [blame] | 1398 | return false; |
| 1399 | R = CurDAG->getTargetFrameIndex(FX, MVT::i32); |
| Colin LeMahieu | c7522f3 | 2015-01-14 23:07:36 +0000 | [diff] [blame] | 1400 | return true; |
| 1401 | } |
| Colin LeMahieu | 0ee02fc | 2015-01-19 20:31:18 +0000 | [diff] [blame] | 1402 | |
| Colin LeMahieu | 987b094 | 2015-02-04 20:38:01 +0000 | [diff] [blame] | 1403 | inline bool HexagonDAGToDAGISel::SelectAddrGA(SDValue &N, SDValue &R) { |
| 1404 | return SelectGlobalAddress(N, R, false); |
| 1405 | } |
| 1406 | |
| Colin LeMahieu | 5149135 | 2015-02-04 22:36:28 +0000 | [diff] [blame] | 1407 | inline bool HexagonDAGToDAGISel::SelectAddrGP(SDValue &N, SDValue &R) { |
| 1408 | return SelectGlobalAddress(N, R, true); |
| 1409 | } |
| 1410 | |
| Colin LeMahieu | 987b094 | 2015-02-04 20:38:01 +0000 | [diff] [blame] | 1411 | bool HexagonDAGToDAGISel::SelectGlobalAddress(SDValue &N, SDValue &R, |
| 1412 | bool UseGP) { |
| 1413 | switch (N.getOpcode()) { |
| 1414 | case ISD::ADD: { |
| 1415 | SDValue N0 = N.getOperand(0); |
| 1416 | SDValue N1 = N.getOperand(1); |
| 1417 | unsigned GAOpc = N0.getOpcode(); |
| 1418 | if (UseGP && GAOpc != HexagonISD::CONST32_GP) |
| 1419 | return false; |
| 1420 | if (!UseGP && GAOpc != HexagonISD::CONST32) |
| 1421 | return false; |
| 1422 | if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N1)) { |
| 1423 | SDValue Addr = N0.getOperand(0); |
| 1424 | if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Addr)) { |
| 1425 | if (GA->getOpcode() == ISD::TargetGlobalAddress) { |
| 1426 | uint64_t NewOff = GA->getOffset() + (uint64_t)Const->getSExtValue(); |
| 1427 | R = CurDAG->getTargetGlobalAddress(GA->getGlobal(), SDLoc(Const), |
| 1428 | N.getValueType(), NewOff); |
| 1429 | return true; |
| 1430 | } |
| 1431 | } |
| 1432 | } |
| 1433 | break; |
| 1434 | } |
| 1435 | case HexagonISD::CONST32: |
| 1436 | // The operand(0) of CONST32 is TargetGlobalAddress, which is what we |
| 1437 | // want in the instruction. |
| 1438 | if (!UseGP) |
| 1439 | R = N.getOperand(0); |
| 1440 | return !UseGP; |
| 1441 | case HexagonISD::CONST32_GP: |
| 1442 | if (UseGP) |
| 1443 | R = N.getOperand(0); |
| 1444 | return UseGP; |
| 1445 | default: |
| 1446 | return false; |
| 1447 | } |
| 1448 | |
| 1449 | return false; |
| 1450 | } |
| 1451 | |
| Krzysztof Parzyszek | a29622a | 2015-03-12 16:44:50 +0000 | [diff] [blame] | 1452 | bool HexagonDAGToDAGISel::isValueExtension(const SDValue &Val, |
| 1453 | unsigned FromBits, SDValue &Src) { |
| Colin LeMahieu | 0ee02fc | 2015-01-19 20:31:18 +0000 | [diff] [blame] | 1454 | unsigned Opc = Val.getOpcode(); |
| 1455 | switch (Opc) { |
| 1456 | case ISD::SIGN_EXTEND: |
| 1457 | case ISD::ZERO_EXTEND: |
| 1458 | case ISD::ANY_EXTEND: { |
| 1459 | SDValue const &Op0 = Val.getOperand(0); |
| 1460 | EVT T = Op0.getValueType(); |
| 1461 | if (T.isInteger() && T.getSizeInBits() == FromBits) { |
| 1462 | Src = Op0; |
| 1463 | return true; |
| 1464 | } |
| 1465 | break; |
| 1466 | } |
| 1467 | case ISD::SIGN_EXTEND_INREG: |
| 1468 | case ISD::AssertSext: |
| 1469 | case ISD::AssertZext: |
| 1470 | if (Val.getOperand(0).getValueType().isInteger()) { |
| 1471 | VTSDNode *T = cast<VTSDNode>(Val.getOperand(1)); |
| 1472 | if (T->getVT().getSizeInBits() == FromBits) { |
| 1473 | Src = Val.getOperand(0); |
| 1474 | return true; |
| 1475 | } |
| 1476 | } |
| 1477 | break; |
| 1478 | case ISD::AND: { |
| 1479 | // Check if this is an AND with "FromBits" of lower bits set to 1. |
| 1480 | uint64_t FromMask = (1 << FromBits) - 1; |
| 1481 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(0))) { |
| 1482 | if (C->getZExtValue() == FromMask) { |
| 1483 | Src = Val.getOperand(1); |
| 1484 | return true; |
| 1485 | } |
| 1486 | } |
| 1487 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(1))) { |
| 1488 | if (C->getZExtValue() == FromMask) { |
| 1489 | Src = Val.getOperand(0); |
| 1490 | return true; |
| 1491 | } |
| 1492 | } |
| 1493 | break; |
| 1494 | } |
| 1495 | case ISD::OR: |
| 1496 | case ISD::XOR: { |
| 1497 | // OR/XOR with the lower "FromBits" bits set to 0. |
| 1498 | uint64_t FromMask = (1 << FromBits) - 1; |
| 1499 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(0))) { |
| 1500 | if ((C->getZExtValue() & FromMask) == 0) { |
| 1501 | Src = Val.getOperand(1); |
| 1502 | return true; |
| 1503 | } |
| 1504 | } |
| 1505 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(1))) { |
| 1506 | if ((C->getZExtValue() & FromMask) == 0) { |
| 1507 | Src = Val.getOperand(0); |
| 1508 | return true; |
| 1509 | } |
| 1510 | } |
| 1511 | } |
| 1512 | default: |
| 1513 | break; |
| 1514 | } |
| 1515 | return false; |
| 1516 | } |
| Krzysztof Parzyszek | 2d65ea7 | 2016-03-28 15:43:03 +0000 | [diff] [blame] | 1517 | |
| Krzysztof Parzyszek | bba0bf7 | 2016-07-15 15:35:52 +0000 | [diff] [blame] | 1518 | |
| 1519 | bool HexagonDAGToDAGISel::orIsAdd(const SDNode *N) const { |
| 1520 | assert(N->getOpcode() == ISD::OR); |
| 1521 | auto *C = dyn_cast<ConstantSDNode>(N->getOperand(1)); |
| 1522 | assert(C); |
| 1523 | |
| 1524 | // Detect when "or" is used to add an offset to a stack object. |
| 1525 | if (auto *FN = dyn_cast<FrameIndexSDNode>(N->getOperand(0))) { |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1526 | MachineFrameInfo &MFI = MF->getFrameInfo(); |
| 1527 | unsigned A = MFI.getObjectAlignment(FN->getIndex()); |
| Krzysztof Parzyszek | bba0bf7 | 2016-07-15 15:35:52 +0000 | [diff] [blame] | 1528 | assert(isPowerOf2_32(A)); |
| 1529 | int32_t Off = C->getSExtValue(); |
| 1530 | // If the alleged offset fits in the zero bits guaranteed by |
| 1531 | // the alignment, then this or is really an add. |
| 1532 | return (Off >= 0) && (((A-1) & Off) == unsigned(Off)); |
| 1533 | } |
| 1534 | return false; |
| 1535 | } |
| 1536 | |
| Krzysztof Parzyszek | 2d65ea7 | 2016-03-28 15:43:03 +0000 | [diff] [blame] | 1537 | bool HexagonDAGToDAGISel::isAlignedMemNode(const MemSDNode *N) const { |
| 1538 | return N->getAlignment() >= N->getMemoryVT().getStoreSize(); |
| 1539 | } |
| Krzysztof Parzyszek | 0006e1a | 2016-07-29 15:15:35 +0000 | [diff] [blame] | 1540 | |
| 1541 | //////////////////////////////////////////////////////////////////////////////// |
| 1542 | // Rebalancing of address calculation trees |
| 1543 | |
| 1544 | static bool isOpcodeHandled(const SDNode *N) { |
| 1545 | switch (N->getOpcode()) { |
| 1546 | case ISD::ADD: |
| 1547 | case ISD::MUL: |
| 1548 | return true; |
| 1549 | case ISD::SHL: |
| 1550 | // We only handle constant shifts because these can be easily flattened |
| 1551 | // into multiplications by 2^Op1. |
| 1552 | return isa<ConstantSDNode>(N->getOperand(1).getNode()); |
| 1553 | default: |
| 1554 | return false; |
| 1555 | } |
| 1556 | } |
| 1557 | |
| 1558 | /// \brief Return the weight of an SDNode |
| 1559 | int HexagonDAGToDAGISel::getWeight(SDNode *N) { |
| 1560 | if (!isOpcodeHandled(N)) |
| 1561 | return 1; |
| 1562 | assert(RootWeights.count(N) && "Cannot get weight of unseen root!"); |
| 1563 | assert(RootWeights[N] != -1 && "Cannot get weight of unvisited root!"); |
| 1564 | assert(RootWeights[N] != -2 && "Cannot get weight of RAWU'd root!"); |
| 1565 | return RootWeights[N]; |
| 1566 | } |
| 1567 | |
| 1568 | int HexagonDAGToDAGISel::getHeight(SDNode *N) { |
| 1569 | if (!isOpcodeHandled(N)) |
| 1570 | return 0; |
| 1571 | assert(RootWeights.count(N) && RootWeights[N] >= 0 && |
| 1572 | "Cannot query height of unvisited/RAUW'd node!"); |
| 1573 | return RootHeights[N]; |
| 1574 | } |
| 1575 | |
| Benjamin Kramer | b7d3311 | 2016-08-06 11:13:10 +0000 | [diff] [blame] | 1576 | namespace { |
| Krzysztof Parzyszek | 0006e1a | 2016-07-29 15:15:35 +0000 | [diff] [blame] | 1577 | struct WeightedLeaf { |
| 1578 | SDValue Value; |
| 1579 | int Weight; |
| 1580 | int InsertionOrder; |
| 1581 | |
| 1582 | WeightedLeaf() : Value(SDValue()) { } |
| 1583 | |
| 1584 | WeightedLeaf(SDValue Value, int Weight, int InsertionOrder) : |
| 1585 | Value(Value), Weight(Weight), InsertionOrder(InsertionOrder) { |
| 1586 | assert(Weight >= 0 && "Weight must be >= 0"); |
| 1587 | } |
| 1588 | |
| 1589 | static bool Compare(const WeightedLeaf &A, const WeightedLeaf &B) { |
| 1590 | assert(A.Value.getNode() && B.Value.getNode()); |
| 1591 | return A.Weight == B.Weight ? |
| 1592 | (A.InsertionOrder > B.InsertionOrder) : |
| 1593 | (A.Weight > B.Weight); |
| 1594 | } |
| 1595 | }; |
| 1596 | |
| 1597 | /// A specialized priority queue for WeigthedLeaves. It automatically folds |
| 1598 | /// constants and allows removal of non-top elements while maintaining the |
| 1599 | /// priority order. |
| 1600 | class LeafPrioQueue { |
| 1601 | SmallVector<WeightedLeaf, 8> Q; |
| 1602 | bool HaveConst; |
| 1603 | WeightedLeaf ConstElt; |
| 1604 | unsigned Opcode; |
| 1605 | |
| 1606 | public: |
| 1607 | bool empty() { |
| 1608 | return (!HaveConst && Q.empty()); |
| 1609 | } |
| 1610 | |
| 1611 | size_t size() { |
| 1612 | return Q.size() + HaveConst; |
| 1613 | } |
| 1614 | |
| 1615 | bool hasConst() { |
| 1616 | return HaveConst; |
| 1617 | } |
| 1618 | |
| 1619 | const WeightedLeaf &top() { |
| 1620 | if (HaveConst) |
| 1621 | return ConstElt; |
| 1622 | return Q.front(); |
| 1623 | } |
| 1624 | |
| 1625 | WeightedLeaf pop() { |
| 1626 | if (HaveConst) { |
| 1627 | HaveConst = false; |
| 1628 | return ConstElt; |
| 1629 | } |
| 1630 | std::pop_heap(Q.begin(), Q.end(), WeightedLeaf::Compare); |
| 1631 | return Q.pop_back_val(); |
| 1632 | } |
| 1633 | |
| 1634 | void push(WeightedLeaf L, bool SeparateConst=true) { |
| 1635 | if (!HaveConst && SeparateConst && isa<ConstantSDNode>(L.Value)) { |
| 1636 | if (Opcode == ISD::MUL && |
| 1637 | cast<ConstantSDNode>(L.Value)->getSExtValue() == 1) |
| 1638 | return; |
| 1639 | if (Opcode == ISD::ADD && |
| 1640 | cast<ConstantSDNode>(L.Value)->getSExtValue() == 0) |
| 1641 | return; |
| 1642 | |
| 1643 | HaveConst = true; |
| 1644 | ConstElt = L; |
| 1645 | } else { |
| 1646 | Q.push_back(L); |
| 1647 | std::push_heap(Q.begin(), Q.end(), WeightedLeaf::Compare); |
| 1648 | } |
| 1649 | } |
| 1650 | |
| 1651 | /// Push L to the bottom of the queue regardless of its weight. If L is |
| 1652 | /// constant, it will not be folded with other constants in the queue. |
| 1653 | void pushToBottom(WeightedLeaf L) { |
| 1654 | L.Weight = 1000; |
| 1655 | push(L, false); |
| 1656 | } |
| 1657 | |
| 1658 | /// Search for a SHL(x, [<=MaxAmount]) subtree in the queue, return the one of |
| 1659 | /// lowest weight and remove it from the queue. |
| 1660 | WeightedLeaf findSHL(uint64_t MaxAmount); |
| 1661 | |
| 1662 | WeightedLeaf findMULbyConst(); |
| 1663 | |
| 1664 | LeafPrioQueue(unsigned Opcode) : |
| 1665 | HaveConst(false), Opcode(Opcode) { } |
| 1666 | }; |
| Benjamin Kramer | b7d3311 | 2016-08-06 11:13:10 +0000 | [diff] [blame] | 1667 | } // end anonymous namespace |
| Krzysztof Parzyszek | 0006e1a | 2016-07-29 15:15:35 +0000 | [diff] [blame] | 1668 | |
| 1669 | WeightedLeaf LeafPrioQueue::findSHL(uint64_t MaxAmount) { |
| 1670 | int ResultPos; |
| 1671 | WeightedLeaf Result; |
| 1672 | |
| 1673 | for (int Pos = 0, End = Q.size(); Pos != End; ++Pos) { |
| 1674 | const WeightedLeaf &L = Q[Pos]; |
| 1675 | const SDValue &Val = L.Value; |
| 1676 | if (Val.getOpcode() != ISD::SHL || |
| 1677 | !isa<ConstantSDNode>(Val.getOperand(1)) || |
| 1678 | Val.getConstantOperandVal(1) > MaxAmount) |
| 1679 | continue; |
| 1680 | if (!Result.Value.getNode() || Result.Weight > L.Weight || |
| 1681 | (Result.Weight == L.Weight && Result.InsertionOrder > L.InsertionOrder)) |
| 1682 | { |
| 1683 | Result = L; |
| 1684 | ResultPos = Pos; |
| 1685 | } |
| 1686 | } |
| 1687 | |
| 1688 | if (Result.Value.getNode()) { |
| 1689 | Q.erase(&Q[ResultPos]); |
| 1690 | std::make_heap(Q.begin(), Q.end(), WeightedLeaf::Compare); |
| 1691 | } |
| 1692 | |
| 1693 | return Result; |
| 1694 | } |
| 1695 | |
| 1696 | WeightedLeaf LeafPrioQueue::findMULbyConst() { |
| 1697 | int ResultPos; |
| 1698 | WeightedLeaf Result; |
| 1699 | |
| 1700 | for (int Pos = 0, End = Q.size(); Pos != End; ++Pos) { |
| 1701 | const WeightedLeaf &L = Q[Pos]; |
| 1702 | const SDValue &Val = L.Value; |
| 1703 | if (Val.getOpcode() != ISD::MUL || |
| 1704 | !isa<ConstantSDNode>(Val.getOperand(1)) || |
| 1705 | Val.getConstantOperandVal(1) > 127) |
| 1706 | continue; |
| 1707 | if (!Result.Value.getNode() || Result.Weight > L.Weight || |
| 1708 | (Result.Weight == L.Weight && Result.InsertionOrder > L.InsertionOrder)) |
| 1709 | { |
| 1710 | Result = L; |
| 1711 | ResultPos = Pos; |
| 1712 | } |
| 1713 | } |
| 1714 | |
| 1715 | if (Result.Value.getNode()) { |
| 1716 | Q.erase(&Q[ResultPos]); |
| 1717 | std::make_heap(Q.begin(), Q.end(), WeightedLeaf::Compare); |
| 1718 | } |
| 1719 | |
| 1720 | return Result; |
| 1721 | } |
| 1722 | |
| 1723 | SDValue HexagonDAGToDAGISel::getMultiplierForSHL(SDNode *N) { |
| Simon Pilgrim | 7c85862 | 2016-07-29 18:43:59 +0000 | [diff] [blame] | 1724 | uint64_t MulFactor = 1ull << N->getConstantOperandVal(1); |
| Krzysztof Parzyszek | 0006e1a | 2016-07-29 15:15:35 +0000 | [diff] [blame] | 1725 | return CurDAG->getConstant(MulFactor, SDLoc(N), |
| 1726 | N->getOperand(1).getValueType()); |
| 1727 | } |
| 1728 | |
| 1729 | /// @returns the value x for which 2^x is a factor of Val |
| 1730 | static unsigned getPowerOf2Factor(SDValue Val) { |
| 1731 | if (Val.getOpcode() == ISD::MUL) { |
| 1732 | unsigned MaxFactor = 0; |
| Krzysztof Parzyszek | 317d42c | 2016-08-01 20:31:50 +0000 | [diff] [blame] | 1733 | for (int i = 0; i < 2; ++i) { |
| Krzysztof Parzyszek | 0006e1a | 2016-07-29 15:15:35 +0000 | [diff] [blame] | 1734 | ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(i)); |
| 1735 | if (!C) |
| 1736 | continue; |
| 1737 | const APInt &CInt = C->getAPIntValue(); |
| 1738 | if (CInt.getBoolValue()) |
| 1739 | MaxFactor = CInt.countTrailingZeros(); |
| 1740 | } |
| 1741 | return MaxFactor; |
| 1742 | } |
| 1743 | if (Val.getOpcode() == ISD::SHL) { |
| 1744 | if (!isa<ConstantSDNode>(Val.getOperand(1).getNode())) |
| 1745 | return 0; |
| 1746 | return (unsigned) Val.getConstantOperandVal(1); |
| 1747 | } |
| 1748 | |
| 1749 | return 0; |
| 1750 | } |
| 1751 | |
| 1752 | /// @returns true if V>>Amount will eliminate V's operation on its child |
| 1753 | static bool willShiftRightEliminate(SDValue V, unsigned Amount) { |
| 1754 | if (V.getOpcode() == ISD::MUL) { |
| 1755 | SDValue Ops[] = { V.getOperand(0), V.getOperand(1) }; |
| Krzysztof Parzyszek | 317d42c | 2016-08-01 20:31:50 +0000 | [diff] [blame] | 1756 | for (int i = 0; i < 2; ++i) |
| Krzysztof Parzyszek | 0006e1a | 2016-07-29 15:15:35 +0000 | [diff] [blame] | 1757 | if (isa<ConstantSDNode>(Ops[i].getNode()) && |
| Krzysztof Parzyszek | 317d42c | 2016-08-01 20:31:50 +0000 | [diff] [blame] | 1758 | V.getConstantOperandVal(i) % (1ULL << Amount) == 0) { |
| Krzysztof Parzyszek | 0006e1a | 2016-07-29 15:15:35 +0000 | [diff] [blame] | 1759 | uint64_t NewConst = V.getConstantOperandVal(i) >> Amount; |
| 1760 | return (NewConst == 1); |
| 1761 | } |
| 1762 | } else if (V.getOpcode() == ISD::SHL) { |
| 1763 | return (Amount == V.getConstantOperandVal(1)); |
| 1764 | } |
| 1765 | |
| 1766 | return false; |
| 1767 | } |
| 1768 | |
| 1769 | SDValue HexagonDAGToDAGISel::factorOutPowerOf2(SDValue V, unsigned Power) { |
| 1770 | SDValue Ops[] = { V.getOperand(0), V.getOperand(1) }; |
| 1771 | if (V.getOpcode() == ISD::MUL) { |
| 1772 | for (int i=0; i < 2; ++i) { |
| 1773 | if (isa<ConstantSDNode>(Ops[i].getNode()) && |
| 1774 | V.getConstantOperandVal(i) % ((uint64_t)1 << Power) == 0) { |
| 1775 | uint64_t NewConst = V.getConstantOperandVal(i) >> Power; |
| 1776 | if (NewConst == 1) |
| 1777 | return Ops[!i]; |
| 1778 | Ops[i] = CurDAG->getConstant(NewConst, |
| 1779 | SDLoc(V), V.getValueType()); |
| 1780 | break; |
| 1781 | } |
| 1782 | } |
| 1783 | } else if (V.getOpcode() == ISD::SHL) { |
| 1784 | uint64_t ShiftAmount = V.getConstantOperandVal(1); |
| 1785 | if (ShiftAmount == Power) |
| 1786 | return Ops[0]; |
| 1787 | Ops[1] = CurDAG->getConstant(ShiftAmount - Power, |
| 1788 | SDLoc(V), V.getValueType()); |
| 1789 | } |
| 1790 | |
| 1791 | return CurDAG->getNode(V.getOpcode(), SDLoc(V), V.getValueType(), Ops); |
| 1792 | } |
| 1793 | |
| 1794 | static bool isTargetConstant(const SDValue &V) { |
| 1795 | return V.getOpcode() == HexagonISD::CONST32 || |
| 1796 | V.getOpcode() == HexagonISD::CONST32_GP; |
| 1797 | } |
| 1798 | |
| 1799 | unsigned HexagonDAGToDAGISel::getUsesInFunction(const Value *V) { |
| 1800 | if (GAUsesInFunction.count(V)) |
| 1801 | return GAUsesInFunction[V]; |
| 1802 | |
| 1803 | unsigned Result = 0; |
| 1804 | const Function *CurF = CurDAG->getMachineFunction().getFunction(); |
| 1805 | for (const User *U : V->users()) { |
| 1806 | if (isa<Instruction>(U) && |
| 1807 | cast<Instruction>(U)->getParent()->getParent() == CurF) |
| 1808 | ++Result; |
| 1809 | } |
| 1810 | |
| 1811 | GAUsesInFunction[V] = Result; |
| 1812 | |
| 1813 | return Result; |
| 1814 | } |
| 1815 | |
| 1816 | /// Note - After calling this, N may be dead. It may have been replaced by a |
| 1817 | /// new node, so always use the returned value in place of N. |
| 1818 | /// |
| 1819 | /// @returns The SDValue taking the place of N (which could be N if it is |
| 1820 | /// unchanged) |
| 1821 | SDValue HexagonDAGToDAGISel::balanceSubTree(SDNode *N, bool TopLevel) { |
| 1822 | assert(RootWeights.count(N) && "Cannot balance non-root node."); |
| 1823 | assert(RootWeights[N] != -2 && "This node was RAUW'd!"); |
| 1824 | assert(!TopLevel || N->getOpcode() == ISD::ADD); |
| 1825 | |
| 1826 | // Return early if this node was already visited |
| 1827 | if (RootWeights[N] != -1) |
| 1828 | return SDValue(N, 0); |
| 1829 | |
| 1830 | assert(isOpcodeHandled(N)); |
| 1831 | |
| 1832 | SDValue Op0 = N->getOperand(0); |
| 1833 | SDValue Op1 = N->getOperand(1); |
| 1834 | |
| 1835 | // Return early if the operands will remain unchanged or are all roots |
| 1836 | if ((!isOpcodeHandled(Op0.getNode()) || RootWeights.count(Op0.getNode())) && |
| 1837 | (!isOpcodeHandled(Op1.getNode()) || RootWeights.count(Op1.getNode()))) { |
| 1838 | SDNode *Op0N = Op0.getNode(); |
| 1839 | int Weight; |
| 1840 | if (isOpcodeHandled(Op0N) && RootWeights[Op0N] == -1) { |
| 1841 | Weight = getWeight(balanceSubTree(Op0N).getNode()); |
| 1842 | // Weight = calculateWeight(Op0N); |
| 1843 | } else |
| 1844 | Weight = getWeight(Op0N); |
| 1845 | |
| 1846 | SDNode *Op1N = N->getOperand(1).getNode(); // Op1 may have been RAUWd |
| 1847 | if (isOpcodeHandled(Op1N) && RootWeights[Op1N] == -1) { |
| 1848 | Weight += getWeight(balanceSubTree(Op1N).getNode()); |
| 1849 | // Weight += calculateWeight(Op1N); |
| 1850 | } else |
| 1851 | Weight += getWeight(Op1N); |
| 1852 | |
| 1853 | RootWeights[N] = Weight; |
| 1854 | RootHeights[N] = std::max(getHeight(N->getOperand(0).getNode()), |
| 1855 | getHeight(N->getOperand(1).getNode())) + 1; |
| 1856 | |
| 1857 | DEBUG(dbgs() << "--> No need to balance root (Weight=" << Weight |
| 1858 | << " Height=" << RootHeights[N] << "): "); |
| 1859 | DEBUG(N->dump()); |
| 1860 | |
| 1861 | return SDValue(N, 0); |
| 1862 | } |
| 1863 | |
| 1864 | DEBUG(dbgs() << "** Balancing root node: "); |
| 1865 | DEBUG(N->dump()); |
| 1866 | |
| 1867 | unsigned NOpcode = N->getOpcode(); |
| 1868 | |
| 1869 | LeafPrioQueue Leaves(NOpcode); |
| 1870 | SmallVector<SDValue, 4> Worklist; |
| 1871 | Worklist.push_back(SDValue(N, 0)); |
| 1872 | |
| 1873 | // SHL nodes will be converted to MUL nodes |
| 1874 | if (NOpcode == ISD::SHL) |
| 1875 | NOpcode = ISD::MUL; |
| 1876 | |
| 1877 | bool CanFactorize = false; |
| 1878 | WeightedLeaf Mul1, Mul2; |
| 1879 | unsigned MaxPowerOf2 = 0; |
| 1880 | WeightedLeaf GA; |
| 1881 | |
| 1882 | // Do not try to factor out a shift if there is already a shift at the tip of |
| 1883 | // the tree. |
| 1884 | bool HaveTopLevelShift = false; |
| 1885 | if (TopLevel && |
| 1886 | ((isOpcodeHandled(Op0.getNode()) && Op0.getOpcode() == ISD::SHL && |
| 1887 | Op0.getConstantOperandVal(1) < 4) || |
| 1888 | (isOpcodeHandled(Op1.getNode()) && Op1.getOpcode() == ISD::SHL && |
| 1889 | Op1.getConstantOperandVal(1) < 4))) |
| 1890 | HaveTopLevelShift = true; |
| 1891 | |
| 1892 | // Flatten the subtree into an ordered list of leaves; at the same time |
| 1893 | // determine whether the tree is already balanced. |
| 1894 | int InsertionOrder = 0; |
| 1895 | SmallDenseMap<SDValue, int> NodeHeights; |
| 1896 | bool Imbalanced = false; |
| 1897 | int CurrentWeight = 0; |
| 1898 | while (!Worklist.empty()) { |
| 1899 | SDValue Child = Worklist.pop_back_val(); |
| 1900 | |
| 1901 | if (Child.getNode() != N && RootWeights.count(Child.getNode())) { |
| 1902 | // CASE 1: Child is a root note |
| 1903 | |
| 1904 | int Weight = RootWeights[Child.getNode()]; |
| 1905 | if (Weight == -1) { |
| 1906 | Child = balanceSubTree(Child.getNode()); |
| 1907 | // calculateWeight(Child.getNode()); |
| 1908 | Weight = getWeight(Child.getNode()); |
| 1909 | } else if (Weight == -2) { |
| 1910 | // Whoops, this node was RAUWd by one of the balanceSubTree calls we |
| 1911 | // made. Our worklist isn't up to date anymore. |
| 1912 | // Restart the whole process. |
| 1913 | DEBUG(dbgs() << "--> Subtree was RAUWd. Restarting...\n"); |
| 1914 | return balanceSubTree(N, TopLevel); |
| 1915 | } |
| 1916 | |
| 1917 | NodeHeights[Child] = 1; |
| 1918 | CurrentWeight += Weight; |
| 1919 | |
| 1920 | unsigned PowerOf2; |
| 1921 | if (TopLevel && !CanFactorize && !HaveTopLevelShift && |
| 1922 | (Child.getOpcode() == ISD::MUL || Child.getOpcode() == ISD::SHL) && |
| 1923 | Child.hasOneUse() && (PowerOf2 = getPowerOf2Factor(Child))) { |
| 1924 | // Try to identify two factorizable MUL/SHL children greedily. Leave |
| 1925 | // them out of the priority queue for now so we can deal with them |
| 1926 | // after. |
| 1927 | if (!Mul1.Value.getNode()) { |
| 1928 | Mul1 = WeightedLeaf(Child, Weight, InsertionOrder++); |
| 1929 | MaxPowerOf2 = PowerOf2; |
| 1930 | } else { |
| 1931 | Mul2 = WeightedLeaf(Child, Weight, InsertionOrder++); |
| 1932 | MaxPowerOf2 = std::min(MaxPowerOf2, PowerOf2); |
| 1933 | |
| 1934 | // Our addressing modes can only shift by a maximum of 3 |
| 1935 | if (MaxPowerOf2 > 3) |
| 1936 | MaxPowerOf2 = 3; |
| 1937 | |
| 1938 | CanFactorize = true; |
| 1939 | } |
| 1940 | } else |
| 1941 | Leaves.push(WeightedLeaf(Child, Weight, InsertionOrder++)); |
| 1942 | } else if (!isOpcodeHandled(Child.getNode())) { |
| 1943 | // CASE 2: Child is an unhandled kind of node (e.g. constant) |
| 1944 | int Weight = getWeight(Child.getNode()); |
| 1945 | |
| 1946 | NodeHeights[Child] = getHeight(Child.getNode()); |
| 1947 | CurrentWeight += Weight; |
| 1948 | |
| 1949 | if (isTargetConstant(Child) && !GA.Value.getNode()) |
| 1950 | GA = WeightedLeaf(Child, Weight, InsertionOrder++); |
| 1951 | else |
| 1952 | Leaves.push(WeightedLeaf(Child, Weight, InsertionOrder++)); |
| 1953 | } else { |
| 1954 | // CASE 3: Child is a subtree of same opcode |
| 1955 | // Visit children first, then flatten. |
| 1956 | unsigned ChildOpcode = Child.getOpcode(); |
| 1957 | assert(ChildOpcode == NOpcode || |
| 1958 | (NOpcode == ISD::MUL && ChildOpcode == ISD::SHL)); |
| 1959 | |
| 1960 | // Convert SHL to MUL |
| 1961 | SDValue Op1; |
| 1962 | if (ChildOpcode == ISD::SHL) |
| 1963 | Op1 = getMultiplierForSHL(Child.getNode()); |
| 1964 | else |
| 1965 | Op1 = Child->getOperand(1); |
| 1966 | |
| 1967 | if (!NodeHeights.count(Op1) || !NodeHeights.count(Child->getOperand(0))) { |
| 1968 | assert(!NodeHeights.count(Child) && "Parent visited before children?"); |
| 1969 | // Visit children first, then re-visit this node |
| 1970 | Worklist.push_back(Child); |
| 1971 | Worklist.push_back(Op1); |
| 1972 | Worklist.push_back(Child->getOperand(0)); |
| 1973 | } else { |
| 1974 | // Back at this node after visiting the children |
| 1975 | if (std::abs(NodeHeights[Op1] - NodeHeights[Child->getOperand(0)]) > 1) |
| 1976 | Imbalanced = true; |
| 1977 | |
| 1978 | NodeHeights[Child] = std::max(NodeHeights[Op1], |
| 1979 | NodeHeights[Child->getOperand(0)]) + 1; |
| 1980 | } |
| 1981 | } |
| 1982 | } |
| 1983 | |
| 1984 | DEBUG(dbgs() << "--> Current height=" << NodeHeights[SDValue(N, 0)] |
| 1985 | << " weight=" << CurrentWeight << " imbalanced=" |
| 1986 | << Imbalanced << "\n"); |
| 1987 | |
| 1988 | // Transform MUL(x, C * 2^Y) + SHL(z, Y) -> SHL(ADD(MUL(x, C), z), Y) |
| 1989 | // This factors out a shift in order to match memw(a<<Y+b). |
| 1990 | if (CanFactorize && (willShiftRightEliminate(Mul1.Value, MaxPowerOf2) || |
| 1991 | willShiftRightEliminate(Mul2.Value, MaxPowerOf2))) { |
| 1992 | DEBUG(dbgs() << "--> Found common factor for two MUL children!\n"); |
| 1993 | int Weight = Mul1.Weight + Mul2.Weight; |
| 1994 | int Height = std::max(NodeHeights[Mul1.Value], NodeHeights[Mul2.Value]) + 1; |
| 1995 | SDValue Mul1Factored = factorOutPowerOf2(Mul1.Value, MaxPowerOf2); |
| 1996 | SDValue Mul2Factored = factorOutPowerOf2(Mul2.Value, MaxPowerOf2); |
| 1997 | SDValue Sum = CurDAG->getNode(ISD::ADD, SDLoc(N), Mul1.Value.getValueType(), |
| 1998 | Mul1Factored, Mul2Factored); |
| 1999 | SDValue Const = CurDAG->getConstant(MaxPowerOf2, SDLoc(N), |
| 2000 | Mul1.Value.getValueType()); |
| 2001 | SDValue New = CurDAG->getNode(ISD::SHL, SDLoc(N), Mul1.Value.getValueType(), |
| 2002 | Sum, Const); |
| 2003 | NodeHeights[New] = Height; |
| 2004 | Leaves.push(WeightedLeaf(New, Weight, Mul1.InsertionOrder)); |
| 2005 | } else if (Mul1.Value.getNode()) { |
| 2006 | // We failed to factorize two MULs, so now the Muls are left outside the |
| 2007 | // queue... add them back. |
| 2008 | Leaves.push(Mul1); |
| 2009 | if (Mul2.Value.getNode()) |
| 2010 | Leaves.push(Mul2); |
| 2011 | CanFactorize = false; |
| 2012 | } |
| 2013 | |
| 2014 | // Combine GA + Constant -> GA+Offset, but only if GA is not used elsewhere |
| 2015 | // and the root node itself is not used more than twice. This reduces the |
| 2016 | // amount of additional constant extenders introduced by this optimization. |
| 2017 | bool CombinedGA = false; |
| 2018 | if (NOpcode == ISD::ADD && GA.Value.getNode() && Leaves.hasConst() && |
| 2019 | GA.Value.hasOneUse() && N->use_size() < 3) { |
| 2020 | GlobalAddressSDNode *GANode = |
| 2021 | cast<GlobalAddressSDNode>(GA.Value.getOperand(0)); |
| 2022 | ConstantSDNode *Offset = cast<ConstantSDNode>(Leaves.top().Value); |
| 2023 | |
| 2024 | if (getUsesInFunction(GANode->getGlobal()) == 1 && Offset->hasOneUse() && |
| 2025 | getTargetLowering()->isOffsetFoldingLegal(GANode)) { |
| 2026 | DEBUG(dbgs() << "--> Combining GA and offset (" << Offset->getSExtValue() |
| 2027 | << "): "); |
| 2028 | DEBUG(GANode->dump()); |
| 2029 | |
| 2030 | SDValue NewTGA = |
| 2031 | CurDAG->getTargetGlobalAddress(GANode->getGlobal(), SDLoc(GA.Value), |
| 2032 | GANode->getValueType(0), |
| 2033 | GANode->getOffset() + (uint64_t)Offset->getSExtValue()); |
| 2034 | GA.Value = CurDAG->getNode(GA.Value.getOpcode(), SDLoc(GA.Value), |
| 2035 | GA.Value.getValueType(), NewTGA); |
| 2036 | GA.Weight += Leaves.top().Weight; |
| 2037 | |
| 2038 | NodeHeights[GA.Value] = getHeight(GA.Value.getNode()); |
| 2039 | CombinedGA = true; |
| 2040 | |
| 2041 | Leaves.pop(); // Remove the offset constant from the queue |
| 2042 | } |
| 2043 | } |
| 2044 | |
| 2045 | if ((RebalanceOnlyForOptimizations && !CanFactorize && !CombinedGA) || |
| 2046 | (RebalanceOnlyImbalancedTrees && !Imbalanced)) { |
| 2047 | RootWeights[N] = CurrentWeight; |
| 2048 | RootHeights[N] = NodeHeights[SDValue(N, 0)]; |
| 2049 | |
| 2050 | return SDValue(N, 0); |
| 2051 | } |
| 2052 | |
| 2053 | // Combine GA + SHL(x, C<=31) so we will match Rx=add(#u8,asl(Rx,#U5)) |
| 2054 | if (NOpcode == ISD::ADD && GA.Value.getNode()) { |
| 2055 | WeightedLeaf SHL = Leaves.findSHL(31); |
| 2056 | if (SHL.Value.getNode()) { |
| 2057 | int Height = std::max(NodeHeights[GA.Value], NodeHeights[SHL.Value]) + 1; |
| 2058 | GA.Value = CurDAG->getNode(ISD::ADD, SDLoc(GA.Value), |
| 2059 | GA.Value.getValueType(), |
| 2060 | GA.Value, SHL.Value); |
| 2061 | GA.Weight = SHL.Weight; // Specifically ignore the GA weight here |
| 2062 | NodeHeights[GA.Value] = Height; |
| 2063 | } |
| 2064 | } |
| 2065 | |
| 2066 | if (GA.Value.getNode()) |
| 2067 | Leaves.push(GA); |
| 2068 | |
| 2069 | // If this is the top level and we haven't factored out a shift, we should try |
| 2070 | // to move a constant to the bottom to match addressing modes like memw(rX+C) |
| 2071 | if (TopLevel && !CanFactorize && Leaves.hasConst()) { |
| 2072 | DEBUG(dbgs() << "--> Pushing constant to tip of tree."); |
| 2073 | Leaves.pushToBottom(Leaves.pop()); |
| 2074 | } |
| 2075 | |
| Krzysztof Parzyszek | 317d42c | 2016-08-01 20:31:50 +0000 | [diff] [blame] | 2076 | const DataLayout &DL = CurDAG->getDataLayout(); |
| 2077 | const TargetLowering &TLI = *getTargetLowering(); |
| 2078 | |
| Krzysztof Parzyszek | 0006e1a | 2016-07-29 15:15:35 +0000 | [diff] [blame] | 2079 | // Rebuild the tree using Huffman's algorithm |
| 2080 | while (Leaves.size() > 1) { |
| 2081 | WeightedLeaf L0 = Leaves.pop(); |
| 2082 | |
| 2083 | // See whether we can grab a MUL to form an add(Rx,mpyi(Ry,#u6)), |
| 2084 | // otherwise just get the next leaf |
| 2085 | WeightedLeaf L1 = Leaves.findMULbyConst(); |
| 2086 | if (!L1.Value.getNode()) |
| 2087 | L1 = Leaves.pop(); |
| 2088 | |
| 2089 | assert(L0.Weight <= L1.Weight && "Priority queue is broken!"); |
| 2090 | |
| 2091 | SDValue V0 = L0.Value; |
| 2092 | int V0Weight = L0.Weight; |
| 2093 | SDValue V1 = L1.Value; |
| 2094 | int V1Weight = L1.Weight; |
| 2095 | |
| 2096 | // Make sure that none of these nodes have been RAUW'd |
| 2097 | if ((RootWeights.count(V0.getNode()) && RootWeights[V0.getNode()] == -2) || |
| 2098 | (RootWeights.count(V1.getNode()) && RootWeights[V1.getNode()] == -2)) { |
| 2099 | DEBUG(dbgs() << "--> Subtree was RAUWd. Restarting...\n"); |
| 2100 | return balanceSubTree(N, TopLevel); |
| 2101 | } |
| 2102 | |
| 2103 | ConstantSDNode *V0C = dyn_cast<ConstantSDNode>(V0); |
| 2104 | ConstantSDNode *V1C = dyn_cast<ConstantSDNode>(V1); |
| 2105 | EVT VT = N->getValueType(0); |
| 2106 | SDValue NewNode; |
| 2107 | |
| 2108 | if (V0C && !V1C) { |
| 2109 | std::swap(V0, V1); |
| 2110 | std::swap(V0C, V1C); |
| 2111 | } |
| 2112 | |
| 2113 | // Calculate height of this node |
| 2114 | assert(NodeHeights.count(V0) && NodeHeights.count(V1) && |
| 2115 | "Children must have been visited before re-combining them!"); |
| 2116 | int Height = std::max(NodeHeights[V0], NodeHeights[V1]) + 1; |
| 2117 | |
| 2118 | // Rebuild this node (and restore SHL from MUL if needed) |
| 2119 | if (V1C && NOpcode == ISD::MUL && V1C->getAPIntValue().isPowerOf2()) |
| 2120 | NewNode = CurDAG->getNode( |
| 2121 | ISD::SHL, SDLoc(V0), VT, V0, |
| 2122 | CurDAG->getConstant( |
| 2123 | V1C->getAPIntValue().logBase2(), SDLoc(N), |
| Krzysztof Parzyszek | 317d42c | 2016-08-01 20:31:50 +0000 | [diff] [blame] | 2124 | TLI.getScalarShiftAmountTy(DL, V0.getValueType()))); |
| Krzysztof Parzyszek | 0006e1a | 2016-07-29 15:15:35 +0000 | [diff] [blame] | 2125 | else |
| 2126 | NewNode = CurDAG->getNode(NOpcode, SDLoc(N), VT, V0, V1); |
| 2127 | |
| 2128 | NodeHeights[NewNode] = Height; |
| 2129 | |
| 2130 | int Weight = V0Weight + V1Weight; |
| 2131 | Leaves.push(WeightedLeaf(NewNode, Weight, L0.InsertionOrder)); |
| 2132 | |
| 2133 | DEBUG(dbgs() << "--> Built new node (Weight=" << Weight << ",Height=" |
| 2134 | << Height << "):\n"); |
| 2135 | DEBUG(NewNode.dump()); |
| 2136 | } |
| 2137 | |
| 2138 | assert(Leaves.size() == 1); |
| 2139 | SDValue NewRoot = Leaves.top().Value; |
| 2140 | |
| 2141 | assert(NodeHeights.count(NewRoot)); |
| 2142 | int Height = NodeHeights[NewRoot]; |
| 2143 | |
| 2144 | // Restore SHL if we earlier converted it to a MUL |
| 2145 | if (NewRoot.getOpcode() == ISD::MUL) { |
| 2146 | ConstantSDNode *V1C = dyn_cast<ConstantSDNode>(NewRoot.getOperand(1)); |
| 2147 | if (V1C && V1C->getAPIntValue().isPowerOf2()) { |
| 2148 | EVT VT = NewRoot.getValueType(); |
| 2149 | SDValue V0 = NewRoot.getOperand(0); |
| 2150 | NewRoot = CurDAG->getNode( |
| 2151 | ISD::SHL, SDLoc(NewRoot), VT, V0, |
| Krzysztof Parzyszek | 317d42c | 2016-08-01 20:31:50 +0000 | [diff] [blame] | 2152 | CurDAG->getConstant( |
| 2153 | V1C->getAPIntValue().logBase2(), SDLoc(NewRoot), |
| 2154 | TLI.getScalarShiftAmountTy(DL, V0.getValueType()))); |
| Krzysztof Parzyszek | 0006e1a | 2016-07-29 15:15:35 +0000 | [diff] [blame] | 2155 | } |
| 2156 | } |
| 2157 | |
| 2158 | if (N != NewRoot.getNode()) { |
| 2159 | DEBUG(dbgs() << "--> Root is now: "); |
| 2160 | DEBUG(NewRoot.dump()); |
| 2161 | |
| 2162 | // Replace all uses of old root by new root |
| 2163 | CurDAG->ReplaceAllUsesWith(N, NewRoot.getNode()); |
| 2164 | // Mark that we have RAUW'd N |
| 2165 | RootWeights[N] = -2; |
| 2166 | } else { |
| 2167 | DEBUG(dbgs() << "--> Root unchanged.\n"); |
| 2168 | } |
| 2169 | |
| 2170 | RootWeights[NewRoot.getNode()] = Leaves.top().Weight; |
| 2171 | RootHeights[NewRoot.getNode()] = Height; |
| 2172 | |
| 2173 | return NewRoot; |
| 2174 | } |
| 2175 | |
| 2176 | void HexagonDAGToDAGISel::rebalanceAddressTrees() { |
| Krzysztof Parzyszek | 317d42c | 2016-08-01 20:31:50 +0000 | [diff] [blame] | 2177 | for (auto I = CurDAG->allnodes_begin(), E = CurDAG->allnodes_end(); I != E;) { |
| Krzysztof Parzyszek | 0006e1a | 2016-07-29 15:15:35 +0000 | [diff] [blame] | 2178 | SDNode *N = &*I++; |
| 2179 | if (N->getOpcode() != ISD::LOAD && N->getOpcode() != ISD::STORE) |
| 2180 | continue; |
| 2181 | |
| 2182 | SDValue BasePtr = cast<MemSDNode>(N)->getBasePtr(); |
| 2183 | if (BasePtr.getOpcode() != ISD::ADD) |
| 2184 | continue; |
| 2185 | |
| 2186 | // We've already processed this node |
| 2187 | if (RootWeights.count(BasePtr.getNode())) |
| 2188 | continue; |
| 2189 | |
| 2190 | DEBUG(dbgs() << "** Rebalancing address calculation in node: "); |
| 2191 | DEBUG(N->dump()); |
| 2192 | |
| 2193 | // FindRoots |
| 2194 | SmallVector<SDNode *, 4> Worklist; |
| 2195 | |
| 2196 | Worklist.push_back(BasePtr.getOperand(0).getNode()); |
| 2197 | Worklist.push_back(BasePtr.getOperand(1).getNode()); |
| 2198 | |
| 2199 | while (!Worklist.empty()) { |
| 2200 | SDNode *N = Worklist.pop_back_val(); |
| 2201 | unsigned Opcode = N->getOpcode(); |
| 2202 | |
| 2203 | if (!isOpcodeHandled(N)) |
| 2204 | continue; |
| 2205 | |
| 2206 | Worklist.push_back(N->getOperand(0).getNode()); |
| 2207 | Worklist.push_back(N->getOperand(1).getNode()); |
| 2208 | |
| 2209 | // Not a root if it has only one use and same opcode as its parent |
| 2210 | if (N->hasOneUse() && Opcode == N->use_begin()->getOpcode()) |
| 2211 | continue; |
| 2212 | |
| 2213 | // This root node has already been processed |
| 2214 | if (RootWeights.count(N)) |
| 2215 | continue; |
| 2216 | |
| 2217 | RootWeights[N] = -1; |
| 2218 | } |
| 2219 | |
| 2220 | // Balance node itself |
| 2221 | RootWeights[BasePtr.getNode()] = -1; |
| 2222 | SDValue NewBasePtr = balanceSubTree(BasePtr.getNode(), /*TopLevel=*/ true); |
| 2223 | |
| 2224 | if (N->getOpcode() == ISD::LOAD) |
| 2225 | N = CurDAG->UpdateNodeOperands(N, N->getOperand(0), |
| 2226 | NewBasePtr, N->getOperand(2)); |
| 2227 | else |
| 2228 | N = CurDAG->UpdateNodeOperands(N, N->getOperand(0), N->getOperand(1), |
| 2229 | NewBasePtr, N->getOperand(3)); |
| 2230 | |
| 2231 | DEBUG(dbgs() << "--> Final node: "); |
| 2232 | DEBUG(N->dump()); |
| 2233 | } |
| 2234 | |
| 2235 | CurDAG->RemoveDeadNodes(); |
| 2236 | GAUsesInFunction.clear(); |
| 2237 | RootHeights.clear(); |
| 2238 | RootWeights.clear(); |
| 2239 | } |
| 2240 | |