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