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