blob: b00b58cb102a13effaf6df1efc4b45b979e1f9d7 [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- HexagonISelDAGToDAG.cpp - A dag to dag inst selector for Hexagon --===//
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002//
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 Carruthed0881b2012-12-03 16:50:05 +000014#include "Hexagon.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000015#include "HexagonISelLowering.h"
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +000016#include "HexagonMachineFunctionInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000017#include "HexagonTargetMachine.h"
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +000018#include "llvm/CodeGen/FunctionLoweringInfo.h"
19#include "llvm/CodeGen/MachineInstrBuilder.h"
Jyotsna Vermad9225242013-02-13 21:38:46 +000020#include "llvm/CodeGen/SelectionDAGISel.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000021#include "llvm/IR/Intrinsics.h"
Jyotsna Vermad9225242013-02-13 21:38:46 +000022#include "llvm/Support/CommandLine.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000023#include "llvm/Support/Debug.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000024using namespace llvm;
25
Chandler Carruth84e68b22014-04-22 02:41:26 +000026#define DEBUG_TYPE "hexagon-isel"
27
Jyotsna Vermad9225242013-02-13 21:38:46 +000028static
Krzysztof Parzyszek0006e1a2016-07-29 15:15:35 +000029cl::opt<bool>
30EnableAddressRebalancing("isel-rebalance-addr", cl::Hidden, cl::init(true),
31 cl::desc("Rebalance address calculation trees to improve "
32 "instruction selection"));
33
34// Rebalance only if this allows e.g. combining a GA with an offset or
35// factoring out a shift.
36static
37cl::opt<bool>
38RebalanceOnlyForOptimizations("rebalance-only-opt", cl::Hidden, cl::init(false),
39 cl::desc("Rebalance address tree only if this allows optimizations"));
40
41static
42cl::opt<bool>
43RebalanceOnlyImbalancedTrees("rebalance-only-imbal", cl::Hidden,
44 cl::init(false), cl::desc("Rebalance address tree only if it is imbalanced"));
45
Tony Linthicum1213a7a2011-12-12 21:14:40 +000046//===----------------------------------------------------------------------===//
47// Instruction Selector Implementation
48//===----------------------------------------------------------------------===//
49
50//===--------------------------------------------------------------------===//
51/// HexagonDAGToDAGISel - Hexagon specific code to select Hexagon machine
52/// instructions for SelectionDAG operations.
53///
54namespace {
55class HexagonDAGToDAGISel : public SelectionDAGISel {
Eric Christopher23a7d1e2015-03-21 03:12:59 +000056 const HexagonSubtarget *HST;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +000057 const HexagonInstrInfo *HII;
58 const HexagonRegisterInfo *HRI;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000059public:
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +000060 explicit HexagonDAGToDAGISel(HexagonTargetMachine &tm,
Jyotsna Vermad9225242013-02-13 21:38:46 +000061 CodeGenOpt::Level OptLevel)
Krzysztof Parzyszek0006e1a2016-07-29 15:15:35 +000062 : SelectionDAGISel(tm, OptLevel), HST(nullptr), HII(nullptr),
Chandler Carruth9ac86ef2016-06-03 10:13:31 +000063 HRI(nullptr) {}
Eric Christopher23a7d1e2015-03-21 03:12:59 +000064
65 bool runOnMachineFunction(MachineFunction &MF) override {
66 // Reset the subtarget each time through.
67 HST = &MF.getSubtarget<HexagonSubtarget>();
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +000068 HII = HST->getInstrInfo();
69 HRI = HST->getRegisterInfo();
Eric Christopher23a7d1e2015-03-21 03:12:59 +000070 SelectionDAGISel::runOnMachineFunction(MF);
71 return true;
72 }
73
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +000074 void PreprocessISelDAG() override;
75 void EmitFunctionEntryCode() override;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000076
Justin Bognerec37a022016-05-12 21:46:18 +000077 void Select(SDNode *N) override;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000078
79 // Complex Pattern Selectors.
Colin LeMahieu987b0942015-02-04 20:38:01 +000080 inline bool SelectAddrGA(SDValue &N, SDValue &R);
Colin LeMahieu51491352015-02-04 22:36:28 +000081 inline bool SelectAddrGP(SDValue &N, SDValue &R);
Colin LeMahieu987b0942015-02-04 20:38:01 +000082 bool SelectGlobalAddress(SDValue &N, SDValue &R, bool UseGP);
Colin LeMahieuc7522f32015-01-14 23:07:36 +000083 bool SelectAddrFI(SDValue &N, SDValue &R);
84
Craig Topper906c2cd2014-04-29 07:58:16 +000085 const char *getPassName() const override {
Tony Linthicum1213a7a2011-12-12 21:14:40 +000086 return "Hexagon DAG->DAG Pattern Instruction Selection";
87 }
88
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +000089 // Generate a machine instruction node corresponding to the circ/brev
90 // load intrinsic.
91 MachineSDNode *LoadInstrForLoadIntrinsic(SDNode *IntN);
92 // Given the circ/brev load intrinsic and the already generated machine
93 // instruction, generate the appropriate store (that is a part of the
94 // intrinsic's functionality).
95 SDNode *StoreInstrForLoadIntrinsic(MachineSDNode *LoadN, SDNode *IntN);
96
Justin Bognerec37a022016-05-12 21:46:18 +000097 void SelectFrameIndex(SDNode *N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +000098 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
99 /// inline asm expressions.
Craig Topper906c2cd2014-04-29 07:58:16 +0000100 bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Daniel Sanders60f1db02015-03-13 12:45:09 +0000101 unsigned ConstraintID,
Craig Topper906c2cd2014-04-29 07:58:16 +0000102 std::vector<SDValue> &OutOps) override;
Justin Bognerec37a022016-05-12 21:46:18 +0000103 bool tryLoadOfLoadIntrinsic(LoadSDNode *N);
104 void SelectLoad(SDNode *N);
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000105 void SelectIndexedLoad(LoadSDNode *LD, const SDLoc &dl);
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000106 void SelectIndexedStore(StoreSDNode *ST, const SDLoc &dl);
Justin Bognerec37a022016-05-12 21:46:18 +0000107 void SelectStore(SDNode *N);
108 void SelectSHL(SDNode *N);
109 void SelectMul(SDNode *N);
110 void SelectZeroExtend(SDNode *N);
111 void SelectIntrinsicWChain(SDNode *N);
112 void SelectIntrinsicWOChain(SDNode *N);
113 void SelectConstant(SDNode *N);
114 void SelectConstantFP(SDNode *N);
115 void SelectAdd(SDNode *N);
Krzysztof Parzyszek5da24e52016-06-27 15:08:22 +0000116 void SelectBitcast(SDNode *N);
Justin Bognerec37a022016-05-12 21:46:18 +0000117 void SelectBitOp(SDNode *N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000118
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000119 // XformMskToBitPosU5Imm - Returns the bit position which
120 // the single bit 32 bit mask represents.
121 // Used in Clr and Set bit immediate memops.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000122 SDValue XformMskToBitPosU5Imm(uint32_t Imm, const SDLoc &DL) {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000123 unsigned BitPos = Log2_32(Imm);
124 assert(BitPos < 32 && "Constant out of range for 32 BitPos Memops");
125 return CurDAG->getTargetConstant(BitPos, DL, MVT::i32);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000126 }
Jyotsna Vermafdc660b2013-03-22 18:41:34 +0000127
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000128 // XformMskToBitPosU4Imm - Returns the bit position which the single-bit
129 // 16 bit mask represents. Used in Clr and Set bit immediate memops.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000130 SDValue XformMskToBitPosU4Imm(uint16_t Imm, const SDLoc &DL) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000131 return XformMskToBitPosU5Imm(Imm, DL);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000132 }
Jyotsna Vermafdc660b2013-03-22 18:41:34 +0000133
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000134 // XformMskToBitPosU3Imm - Returns the bit position which the single-bit
135 // 8 bit mask represents. Used in Clr and Set bit immediate memops.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000136 SDValue XformMskToBitPosU3Imm(uint8_t Imm, const SDLoc &DL) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000137 return XformMskToBitPosU5Imm(Imm, DL);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000138 }
Jyotsna Vermafdc660b2013-03-22 18:41:34 +0000139
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000140 // Return true if there is exactly one bit set in V, i.e., if V is one of the
141 // following integers: 2^0, 2^1, ..., 2^31.
142 bool ImmIsSingleBit(uint32_t v) const {
143 return isPowerOf2_32(v);
144 }
Jyotsna Vermafdc660b2013-03-22 18:41:34 +0000145
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000146 // XformM5ToU5Imm - Return a target constant with the specified value, of
147 // type i32 where the negative literal is transformed into a positive literal
148 // for use in -= memops.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000149 inline SDValue XformM5ToU5Imm(signed Imm, const SDLoc &DL) {
150 assert((Imm >= -31 && Imm <= -1) && "Constant out of range for Memops");
151 return CurDAG->getTargetConstant(-Imm, DL, MVT::i32);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000152 }
Jyotsna Vermafdc660b2013-03-22 18:41:34 +0000153
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000154 // XformU7ToU7M1Imm - Return a target constant decremented by 1, in range
155 // [1..128], used in cmpb.gtu instructions.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000156 inline SDValue XformU7ToU7M1Imm(signed Imm, const SDLoc &DL) {
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000157 assert((Imm >= 1 && Imm <= 128) && "Constant out of range for cmpb op");
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000158 return CurDAG->getTargetConstant(Imm - 1, DL, MVT::i8);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000159 }
Jyotsna Vermafdc660b2013-03-22 18:41:34 +0000160
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000161 // XformS8ToS8M1Imm - Return a target constant decremented by 1.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000162 inline SDValue XformSToSM1Imm(signed Imm, const SDLoc &DL) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000163 return CurDAG->getTargetConstant(Imm - 1, DL, MVT::i32);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000164 }
Jyotsna Verma60316252013-02-05 19:20:45 +0000165
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000166 // XformU8ToU8M1Imm - Return a target constant decremented by 1.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000167 inline SDValue XformUToUM1Imm(unsigned Imm, const SDLoc &DL) {
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000168 assert((Imm >= 1) && "Cannot decrement unsigned int less than 1");
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000169 return CurDAG->getTargetConstant(Imm - 1, DL, MVT::i32);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000170 }
Jyotsna Verma89c84822013-04-23 19:15:55 +0000171
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000172 // XformSToSM2Imm - Return a target constant decremented by 2.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000173 inline SDValue XformSToSM2Imm(unsigned Imm, const SDLoc &DL) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000174 return CurDAG->getTargetConstant(Imm - 2, DL, MVT::i32);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000175 }
Jyotsna Verma89c84822013-04-23 19:15:55 +0000176
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000177 // XformSToSM3Imm - Return a target constant decremented by 3.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000178 inline SDValue XformSToSM3Imm(unsigned Imm, const SDLoc &DL) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000179 return CurDAG->getTargetConstant(Imm - 3, DL, MVT::i32);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000180 }
Colin LeMahieu19ed07c2015-01-28 18:29:11 +0000181
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000182 // Include the pieces autogenerated from the target description.
183 #include "HexagonGenDAGISel.inc"
Colin LeMahieu0ee02fc2015-01-19 20:31:18 +0000184
185private:
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000186 bool isValueExtension(const SDValue &Val, unsigned FromBits, SDValue &Src);
Krzysztof Parzyszekbba0bf72016-07-15 15:35:52 +0000187 bool orIsAdd(const SDNode *N) const;
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +0000188 bool isAlignedMemNode(const MemSDNode *N) const;
Krzysztof Parzyszek0006e1a2016-07-29 15:15:35 +0000189
190 SmallDenseMap<SDNode *,int> RootWeights;
191 SmallDenseMap<SDNode *,int> RootHeights;
192 SmallDenseMap<const Value *,int> GAUsesInFunction;
193 int getWeight(SDNode *N);
194 int getHeight(SDNode *N);
195 SDValue getMultiplierForSHL(SDNode *N);
196 SDValue factorOutPowerOf2(SDValue V, unsigned Power);
197 unsigned getUsesInFunction(const Value *V);
198 SDValue balanceSubTree(SDNode *N, bool Factorize = false);
199 void rebalanceAddressTrees();
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000200}; // end HexagonDAGToDAGISel
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000201} // end anonymous namespace
202
203
204/// createHexagonISelDag - This pass converts a legalized DAG into a
205/// Hexagon-specific DAG, ready for instruction scheduling.
206///
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000207namespace llvm {
208FunctionPass *createHexagonISelDag(HexagonTargetMachine &TM,
209 CodeGenOpt::Level OptLevel) {
Jyotsna Vermad9225242013-02-13 21:38:46 +0000210 return new HexagonDAGToDAGISel(TM, OptLevel);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000211}
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000212}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000213
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000214// Intrinsics that return a a predicate.
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000215static bool doesIntrinsicReturnPredicate(unsigned ID) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000216 switch (ID) {
217 default:
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000218 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000219 case Intrinsic::hexagon_C2_cmpeq:
220 case Intrinsic::hexagon_C2_cmpgt:
221 case Intrinsic::hexagon_C2_cmpgtu:
222 case Intrinsic::hexagon_C2_cmpgtup:
223 case Intrinsic::hexagon_C2_cmpgtp:
224 case Intrinsic::hexagon_C2_cmpeqp:
225 case Intrinsic::hexagon_C2_bitsset:
226 case Intrinsic::hexagon_C2_bitsclr:
227 case Intrinsic::hexagon_C2_cmpeqi:
228 case Intrinsic::hexagon_C2_cmpgti:
229 case Intrinsic::hexagon_C2_cmpgtui:
230 case Intrinsic::hexagon_C2_cmpgei:
231 case Intrinsic::hexagon_C2_cmpgeui:
232 case Intrinsic::hexagon_C2_cmplt:
233 case Intrinsic::hexagon_C2_cmpltu:
234 case Intrinsic::hexagon_C2_bitsclri:
235 case Intrinsic::hexagon_C2_and:
236 case Intrinsic::hexagon_C2_or:
237 case Intrinsic::hexagon_C2_xor:
238 case Intrinsic::hexagon_C2_andn:
239 case Intrinsic::hexagon_C2_not:
240 case Intrinsic::hexagon_C2_orn:
241 case Intrinsic::hexagon_C2_pxfer_map:
242 case Intrinsic::hexagon_C2_any8:
243 case Intrinsic::hexagon_C2_all8:
244 case Intrinsic::hexagon_A2_vcmpbeq:
245 case Intrinsic::hexagon_A2_vcmpbgtu:
246 case Intrinsic::hexagon_A2_vcmpheq:
247 case Intrinsic::hexagon_A2_vcmphgt:
248 case Intrinsic::hexagon_A2_vcmphgtu:
249 case Intrinsic::hexagon_A2_vcmpweq:
250 case Intrinsic::hexagon_A2_vcmpwgt:
251 case Intrinsic::hexagon_A2_vcmpwgtu:
252 case Intrinsic::hexagon_C2_tfrrp:
253 case Intrinsic::hexagon_S2_tstbit_i:
254 case Intrinsic::hexagon_S2_tstbit_r:
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000255 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000256 }
257}
258
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000259void HexagonDAGToDAGISel::SelectIndexedLoad(LoadSDNode *LD, const SDLoc &dl) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000260 SDValue Chain = LD->getChain();
261 SDValue Base = LD->getBasePtr();
262 SDValue Offset = LD->getOffset();
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000263 int32_t Inc = cast<ConstantSDNode>(Offset.getNode())->getSExtValue();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000264 EVT LoadedVT = LD->getMemoryVT();
265 unsigned Opcode = 0;
266
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000267 // Check for zero extended loads. Treat any-extend loads as zero extended
268 // loads.
269 ISD::LoadExtType ExtType = LD->getExtensionType();
270 bool IsZeroExt = (ExtType == ISD::ZEXTLOAD || ExtType == ISD::EXTLOAD);
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000271 bool IsValidInc = HII->isValidAutoIncImm(LoadedVT, Inc);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000272
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000273 assert(LoadedVT.isSimple());
274 switch (LoadedVT.getSimpleVT().SimpleTy) {
275 case MVT::i8:
276 if (IsZeroExt)
277 Opcode = IsValidInc ? Hexagon::L2_loadrub_pi : Hexagon::L2_loadrub_io;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000278 else
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000279 Opcode = IsValidInc ? Hexagon::L2_loadrb_pi : Hexagon::L2_loadrb_io;
280 break;
281 case MVT::i16:
282 if (IsZeroExt)
283 Opcode = IsValidInc ? Hexagon::L2_loadruh_pi : Hexagon::L2_loadruh_io;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000284 else
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000285 Opcode = IsValidInc ? Hexagon::L2_loadrh_pi : Hexagon::L2_loadrh_io;
286 break;
287 case MVT::i32:
288 Opcode = IsValidInc ? Hexagon::L2_loadri_pi : Hexagon::L2_loadri_io;
289 break;
290 case MVT::i64:
291 Opcode = IsValidInc ? Hexagon::L2_loadrd_pi : Hexagon::L2_loadrd_io;
292 break;
293 // 64B
294 case MVT::v64i8:
295 case MVT::v32i16:
296 case MVT::v16i32:
297 case MVT::v8i64:
298 if (isAlignedMemNode(LD))
299 Opcode = IsValidInc ? Hexagon::V6_vL32b_pi : Hexagon::V6_vL32b_ai;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000300 else
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000301 Opcode = IsValidInc ? Hexagon::V6_vL32Ub_pi : Hexagon::V6_vL32Ub_ai;
302 break;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000303 // 128B
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000304 case MVT::v128i8:
305 case MVT::v64i16:
306 case MVT::v32i32:
307 case MVT::v16i64:
308 if (isAlignedMemNode(LD))
309 Opcode = IsValidInc ? Hexagon::V6_vL32b_pi_128B
310 : Hexagon::V6_vL32b_ai_128B;
311 else
312 Opcode = IsValidInc ? Hexagon::V6_vL32Ub_pi_128B
313 : Hexagon::V6_vL32Ub_ai_128B;
314 break;
315 default:
316 llvm_unreachable("Unexpected memory type in indexed load");
Justin Bognerec37a022016-05-12 21:46:18 +0000317 }
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000318
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000319 SDValue IncV = CurDAG->getTargetConstant(Inc, dl, MVT::i32);
320 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
321 MemOp[0] = LD->getMemOperand();
322
323 auto getExt64 = [this,ExtType] (MachineSDNode *N, const SDLoc &dl)
324 -> MachineSDNode* {
325 if (ExtType == ISD::ZEXTLOAD || ExtType == ISD::EXTLOAD) {
326 SDValue Zero = CurDAG->getTargetConstant(0, dl, MVT::i32);
327 return CurDAG->getMachineNode(Hexagon::A4_combineir, dl, MVT::i64,
328 Zero, SDValue(N, 0));
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000329 }
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000330 if (ExtType == ISD::SEXTLOAD)
331 return CurDAG->getMachineNode(Hexagon::A2_sxtw, dl, MVT::i64,
332 SDValue(N, 0));
333 return N;
334 };
335
336 // Loaded value Next address Chain
337 SDValue From[3] = { SDValue(LD,0), SDValue(LD,1), SDValue(LD,2) };
338 SDValue To[3];
339
340 EVT ValueVT = LD->getValueType(0);
341 if (ValueVT == MVT::i64 && ExtType != ISD::NON_EXTLOAD) {
342 // A load extending to i64 will actually produce i32, which will then
343 // need to be extended to i64.
344 assert(LoadedVT.getSizeInBits() <= 32);
345 ValueVT = MVT::i32;
346 }
347
348 if (IsValidInc) {
349 MachineSDNode *L = CurDAG->getMachineNode(Opcode, dl, ValueVT,
350 MVT::i32, MVT::Other, Base,
351 IncV, Chain);
352 L->setMemRefs(MemOp, MemOp+1);
353 To[1] = SDValue(L, 1); // Next address.
354 To[2] = SDValue(L, 2); // Chain.
355 // Handle special case for extension to i64.
356 if (LD->getValueType(0) == MVT::i64)
357 L = getExt64(L, dl);
358 To[0] = SDValue(L, 0); // Loaded (extended) value.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000359 } else {
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000360 SDValue Zero = CurDAG->getTargetConstant(0, dl, MVT::i32);
361 MachineSDNode *L = CurDAG->getMachineNode(Opcode, dl, ValueVT, MVT::Other,
362 Base, Zero, Chain);
363 L->setMemRefs(MemOp, MemOp+1);
364 To[2] = SDValue(L, 1); // Chain.
365 MachineSDNode *A = CurDAG->getMachineNode(Hexagon::A2_addi, dl, MVT::i32,
366 Base, IncV);
367 To[1] = SDValue(A, 0); // Next address.
368 // Handle special case for extension to i64.
369 if (LD->getValueType(0) == MVT::i64)
370 L = getExt64(L, dl);
371 To[0] = SDValue(L, 0); // Loaded (extended) value.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000372 }
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000373 ReplaceUses(From, To, 3);
374 CurDAG->RemoveDeadNode(LD);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000375}
376
377
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +0000378MachineSDNode *HexagonDAGToDAGISel::LoadInstrForLoadIntrinsic(SDNode *IntN) {
379 if (IntN->getOpcode() != ISD::INTRINSIC_W_CHAIN)
380 return nullptr;
381
382 SDLoc dl(IntN);
383 unsigned IntNo = cast<ConstantSDNode>(IntN->getOperand(1))->getZExtValue();
384
385 static std::map<unsigned,unsigned> LoadPciMap = {
386 { Intrinsic::hexagon_circ_ldb, Hexagon::L2_loadrb_pci },
387 { Intrinsic::hexagon_circ_ldub, Hexagon::L2_loadrub_pci },
388 { Intrinsic::hexagon_circ_ldh, Hexagon::L2_loadrh_pci },
389 { Intrinsic::hexagon_circ_lduh, Hexagon::L2_loadruh_pci },
390 { Intrinsic::hexagon_circ_ldw, Hexagon::L2_loadri_pci },
391 { Intrinsic::hexagon_circ_ldd, Hexagon::L2_loadrd_pci },
392 };
393 auto FLC = LoadPciMap.find(IntNo);
394 if (FLC != LoadPciMap.end()) {
395 SDNode *Mod = CurDAG->getMachineNode(Hexagon::A2_tfrrcr, dl, MVT::i32,
396 IntN->getOperand(4));
397 EVT ValTy = (IntNo == Intrinsic::hexagon_circ_ldd) ? MVT::i64 : MVT::i32;
398 EVT RTys[] = { ValTy, MVT::i32, MVT::Other };
399 // Operands: { Base, Increment, Modifier, Chain }
400 auto Inc = cast<ConstantSDNode>(IntN->getOperand(5));
401 SDValue I = CurDAG->getTargetConstant(Inc->getSExtValue(), dl, MVT::i32);
402 MachineSDNode *Res = CurDAG->getMachineNode(FLC->second, dl, RTys,
403 { IntN->getOperand(2), I, SDValue(Mod,0), IntN->getOperand(0) });
404 return Res;
405 }
406
407 static std::map<unsigned,unsigned> LoadPbrMap = {
408 { Intrinsic::hexagon_brev_ldb, Hexagon::L2_loadrb_pbr },
409 { Intrinsic::hexagon_brev_ldub, Hexagon::L2_loadrub_pbr },
410 { Intrinsic::hexagon_brev_ldh, Hexagon::L2_loadrh_pbr },
411 { Intrinsic::hexagon_brev_lduh, Hexagon::L2_loadruh_pbr },
412 { Intrinsic::hexagon_brev_ldw, Hexagon::L2_loadri_pbr },
413 { Intrinsic::hexagon_brev_ldd, Hexagon::L2_loadrd_pbr },
414 };
415 auto FLB = LoadPbrMap.find(IntNo);
416 if (FLB != LoadPbrMap.end()) {
417 SDNode *Mod = CurDAG->getMachineNode(Hexagon::A2_tfrrcr, dl, MVT::i32,
418 IntN->getOperand(4));
419 EVT ValTy = (IntNo == Intrinsic::hexagon_brev_ldd) ? MVT::i64 : MVT::i32;
420 EVT RTys[] = { ValTy, MVT::i32, MVT::Other };
421 // Operands: { Base, Modifier, Chain }
422 MachineSDNode *Res = CurDAG->getMachineNode(FLB->second, dl, RTys,
423 { IntN->getOperand(2), SDValue(Mod,0), IntN->getOperand(0) });
424 return Res;
425 }
426
427 return nullptr;
428}
429
430SDNode *HexagonDAGToDAGISel::StoreInstrForLoadIntrinsic(MachineSDNode *LoadN,
431 SDNode *IntN) {
432 // The "LoadN" is just a machine load instruction. The intrinsic also
433 // involves storing it. Generate an appropriate store to the location
434 // given in the intrinsic's operand(3).
435 uint64_t F = HII->get(LoadN->getMachineOpcode()).TSFlags;
436 unsigned SizeBits = (F >> HexagonII::MemAccessSizePos) &
437 HexagonII::MemAccesSizeMask;
438 unsigned Size = 1U << (SizeBits-1);
439
440 SDLoc dl(IntN);
441 MachinePointerInfo PI;
442 SDValue TS;
443 SDValue Loc = IntN->getOperand(3);
444
445 if (Size >= 4)
Justin Lebar9c375812016-07-15 18:27:10 +0000446 TS = CurDAG->getStore(SDValue(LoadN, 2), dl, SDValue(LoadN, 0), Loc, PI,
447 Size);
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +0000448 else
Justin Lebar9c375812016-07-15 18:27:10 +0000449 TS = CurDAG->getTruncStore(SDValue(LoadN, 2), dl, SDValue(LoadN, 0), Loc,
450 PI, MVT::getIntegerVT(Size * 8), Size);
Justin Bognerdcb7a822016-05-10 20:31:53 +0000451
452 SDNode *StoreN;
453 {
454 HandleSDNode Handle(TS);
455 SelectStore(TS.getNode());
456 StoreN = Handle.getValue().getNode();
457 }
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +0000458
459 // Load's results are { Loaded value, Updated pointer, Chain }
460 ReplaceUses(SDValue(IntN, 0), SDValue(LoadN, 1));
461 ReplaceUses(SDValue(IntN, 1), SDValue(StoreN, 0));
462 return StoreN;
463}
464
Justin Bognerec37a022016-05-12 21:46:18 +0000465bool HexagonDAGToDAGISel::tryLoadOfLoadIntrinsic(LoadSDNode *N) {
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +0000466 // The intrinsics for load circ/brev perform two operations:
467 // 1. Load a value V from the specified location, using the addressing
468 // mode corresponding to the intrinsic.
469 // 2. Store V into a specified location. This location is typically a
470 // local, temporary object.
471 // In many cases, the program using these intrinsics will immediately
472 // load V again from the local object. In those cases, when certain
473 // conditions are met, the last load can be removed.
474 // This function identifies and optimizes this pattern. If the pattern
475 // cannot be optimized, it returns nullptr, which will cause the load
476 // to be selected separately from the intrinsic (which will be handled
477 // in SelectIntrinsicWChain).
478
479 SDValue Ch = N->getOperand(0);
480 SDValue Loc = N->getOperand(1);
481
482 // Assume that the load and the intrinsic are connected directly with a
483 // chain:
484 // t1: i32,ch = int.load ..., ..., ..., Loc, ... // <-- C
485 // t2: i32,ch = load t1:1, Loc, ...
486 SDNode *C = Ch.getNode();
487
488 if (C->getOpcode() != ISD::INTRINSIC_W_CHAIN)
Justin Bognerec37a022016-05-12 21:46:18 +0000489 return false;
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +0000490
491 // The second load can only be eliminated if its extension type matches
492 // that of the load instruction corresponding to the intrinsic. The user
493 // can provide an address of an unsigned variable to store the result of
494 // a sign-extending intrinsic into (or the other way around).
495 ISD::LoadExtType IntExt;
496 switch (cast<ConstantSDNode>(C->getOperand(1))->getZExtValue()) {
497 case Intrinsic::hexagon_brev_ldub:
498 case Intrinsic::hexagon_brev_lduh:
499 case Intrinsic::hexagon_circ_ldub:
500 case Intrinsic::hexagon_circ_lduh:
501 IntExt = ISD::ZEXTLOAD;
502 break;
503 case Intrinsic::hexagon_brev_ldw:
504 case Intrinsic::hexagon_brev_ldd:
505 case Intrinsic::hexagon_circ_ldw:
506 case Intrinsic::hexagon_circ_ldd:
507 IntExt = ISD::NON_EXTLOAD;
508 break;
509 default:
510 IntExt = ISD::SEXTLOAD;
511 break;
512 }
513 if (N->getExtensionType() != IntExt)
Justin Bognerec37a022016-05-12 21:46:18 +0000514 return false;
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +0000515
516 // Make sure the target location for the loaded value in the load intrinsic
517 // is the location from which LD (or N) is loading.
518 if (C->getNumOperands() < 4 || Loc.getNode() != C->getOperand(3).getNode())
Justin Bognerec37a022016-05-12 21:46:18 +0000519 return false;
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +0000520
521 if (MachineSDNode *L = LoadInstrForLoadIntrinsic(C)) {
522 SDNode *S = StoreInstrForLoadIntrinsic(L, C);
523 SDValue F[] = { SDValue(N,0), SDValue(N,1), SDValue(C,0), SDValue(C,1) };
524 SDValue T[] = { SDValue(L,0), SDValue(S,0), SDValue(L,1), SDValue(S,0) };
525 ReplaceUses(F, T, array_lengthof(T));
526 // This transformation will leave the intrinsic dead. If it remains in
527 // the DAG, the selection code will see it again, but without the load,
528 // and it will generate a store that is normally required for it.
Krzysztof Parzyszek0f791f42016-05-13 18:48:15 +0000529 CurDAG->RemoveDeadNode(C);
Justin Bognerec37a022016-05-12 21:46:18 +0000530 return true;
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +0000531 }
532
Justin Bognerec37a022016-05-12 21:46:18 +0000533 return false;
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +0000534}
535
Justin Bognerec37a022016-05-12 21:46:18 +0000536void HexagonDAGToDAGISel::SelectLoad(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000537 SDLoc dl(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000538 LoadSDNode *LD = cast<LoadSDNode>(N);
539 ISD::MemIndexedMode AM = LD->getAddressingMode();
540
541 // Handle indexed loads.
Justin Bognerec37a022016-05-12 21:46:18 +0000542 if (AM != ISD::UNINDEXED) {
543 SelectIndexedLoad(LD, dl);
544 return;
545 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000546
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +0000547 // Handle patterns using circ/brev load intrinsics.
Justin Bognerec37a022016-05-12 21:46:18 +0000548 if (tryLoadOfLoadIntrinsic(LD))
549 return;
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +0000550
Justin Bognerec37a022016-05-12 21:46:18 +0000551 SelectCode(LD);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000552}
553
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000554void HexagonDAGToDAGISel::SelectIndexedStore(StoreSDNode *ST, const SDLoc &dl) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000555 SDValue Chain = ST->getChain();
556 SDValue Base = ST->getBasePtr();
557 SDValue Offset = ST->getOffset();
558 SDValue Value = ST->getValue();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000559 // Get the constant value.
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000560 int32_t Inc = cast<ConstantSDNode>(Offset.getNode())->getSExtValue();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000561 EVT StoredVT = ST->getMemoryVT();
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000562 EVT ValueVT = Value.getValueType();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000563
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000564 bool IsValidInc = HII->isValidAutoIncImm(StoredVT, Inc);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000565 unsigned Opcode = 0;
566
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000567 assert(StoredVT.isSimple());
568 switch (StoredVT.getSimpleVT().SimpleTy) {
569 case MVT::i8:
570 Opcode = IsValidInc ? Hexagon::S2_storerb_pi : Hexagon::S2_storerb_io;
571 break;
572 case MVT::i16:
573 Opcode = IsValidInc ? Hexagon::S2_storerh_pi : Hexagon::S2_storerh_io;
574 break;
575 case MVT::i32:
576 Opcode = IsValidInc ? Hexagon::S2_storeri_pi : Hexagon::S2_storeri_io;
577 break;
578 case MVT::i64:
579 Opcode = IsValidInc ? Hexagon::S2_storerd_pi : Hexagon::S2_storerd_io;
580 break;
581 // 64B
582 case MVT::v64i8:
583 case MVT::v32i16:
584 case MVT::v16i32:
585 case MVT::v8i64:
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +0000586 if (isAlignedMemNode(ST))
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000587 Opcode = IsValidInc ? Hexagon::V6_vS32b_pi : Hexagon::V6_vS32b_ai;
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +0000588 else
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000589 Opcode = IsValidInc ? Hexagon::V6_vS32Ub_pi : Hexagon::V6_vS32Ub_ai;
590 break;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000591 // 128B
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000592 case MVT::v128i8:
593 case MVT::v64i16:
594 case MVT::v32i32:
595 case MVT::v16i64:
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +0000596 if (isAlignedMemNode(ST))
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000597 Opcode = IsValidInc ? Hexagon::V6_vS32b_pi_128B
598 : Hexagon::V6_vS32b_ai_128B;
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +0000599 else
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000600 Opcode = IsValidInc ? Hexagon::V6_vS32Ub_pi_128B
601 : Hexagon::V6_vS32Ub_ai_128B;
602 break;
603 default:
604 llvm_unreachable("Unexpected memory type in indexed store");
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +0000605 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000606
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000607 if (ST->isTruncatingStore() && ValueVT.getSizeInBits() == 64) {
608 assert(StoredVT.getSizeInBits() < 64 && "Not a truncating store");
609 Value = CurDAG->getTargetExtractSubreg(Hexagon::subreg_loreg,
610 dl, MVT::i32, Value);
611 }
612
613 SDValue IncV = CurDAG->getTargetConstant(Inc, dl, MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000614 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
615 MemOp[0] = ST->getMemOperand();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000616
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000617 // Next address Chain
618 SDValue From[2] = { SDValue(ST,0), SDValue(ST,1) };
619 SDValue To[2];
620
621 if (IsValidInc) {
622 // Build post increment store.
623 SDValue Ops[] = { Base, IncV, Value, Chain };
624 MachineSDNode *S = CurDAG->getMachineNode(Opcode, dl, MVT::i32, MVT::Other,
625 Ops);
626 S->setMemRefs(MemOp, MemOp + 1);
627 To[0] = SDValue(S, 0);
628 To[1] = SDValue(S, 1);
629 } else {
630 SDValue Zero = CurDAG->getTargetConstant(0, dl, MVT::i32);
631 SDValue Ops[] = { Base, Zero, Value, Chain };
632 MachineSDNode *S = CurDAG->getMachineNode(Opcode, dl, MVT::Other, Ops);
633 S->setMemRefs(MemOp, MemOp + 1);
634 To[1] = SDValue(S, 0);
635 MachineSDNode *A = CurDAG->getMachineNode(Hexagon::A2_addi, dl, MVT::i32,
636 Base, IncV);
637 To[0] = SDValue(A, 0);
638 }
639
640 ReplaceUses(From, To, 2);
Justin Bognerdcb7a822016-05-10 20:31:53 +0000641 CurDAG->RemoveDeadNode(ST);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000642}
643
Justin Bognerec37a022016-05-12 21:46:18 +0000644void HexagonDAGToDAGISel::SelectStore(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000645 SDLoc dl(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000646 StoreSDNode *ST = cast<StoreSDNode>(N);
647 ISD::MemIndexedMode AM = ST->getAddressingMode();
648
649 // Handle indexed stores.
650 if (AM != ISD::UNINDEXED) {
Justin Bognerec37a022016-05-12 21:46:18 +0000651 SelectIndexedStore(ST, dl);
652 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000653 }
Sirish Pandec92c3162012-05-03 16:18:50 +0000654
Justin Bognerec37a022016-05-12 21:46:18 +0000655 SelectCode(ST);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000656}
657
Justin Bognerec37a022016-05-12 21:46:18 +0000658void HexagonDAGToDAGISel::SelectMul(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000659 SDLoc dl(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000660
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000661 // %conv.i = sext i32 %tmp1 to i64
662 // %conv2.i = sext i32 %add to i64
663 // %mul.i = mul nsw i64 %conv2.i, %conv.i
664 //
665 // --- match with the following ---
666 //
667 // %mul.i = mpy (%tmp1, %add)
668 //
669
670 if (N->getValueType(0) == MVT::i64) {
671 // Shifting a i64 signed multiply.
672 SDValue MulOp0 = N->getOperand(0);
673 SDValue MulOp1 = N->getOperand(1);
674
675 SDValue OP0;
676 SDValue OP1;
677
678 // Handle sign_extend and sextload.
679 if (MulOp0.getOpcode() == ISD::SIGN_EXTEND) {
680 SDValue Sext0 = MulOp0.getOperand(0);
681 if (Sext0.getNode()->getValueType(0) != MVT::i32) {
Justin Bognerec37a022016-05-12 21:46:18 +0000682 SelectCode(N);
683 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000684 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000685 OP0 = Sext0;
686 } else if (MulOp0.getOpcode() == ISD::LOAD) {
687 LoadSDNode *LD = cast<LoadSDNode>(MulOp0.getNode());
688 if (LD->getMemoryVT() != MVT::i32 ||
689 LD->getExtensionType() != ISD::SEXTLOAD ||
690 LD->getAddressingMode() != ISD::UNINDEXED) {
Justin Bognerec37a022016-05-12 21:46:18 +0000691 SelectCode(N);
692 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000693 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000694 SDValue Chain = LD->getChain();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000695 SDValue TargetConst0 = CurDAG->getTargetConstant(0, dl, MVT::i32);
Colin LeMahieu026e88d2014-12-23 20:02:16 +0000696 OP0 = SDValue(CurDAG->getMachineNode(Hexagon::L2_loadri_io, dl, MVT::i32,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000697 MVT::Other,
698 LD->getBasePtr(), TargetConst0,
699 Chain), 0);
700 } else {
Justin Bognerec37a022016-05-12 21:46:18 +0000701 SelectCode(N);
702 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000703 }
704
705 // Same goes for the second operand.
706 if (MulOp1.getOpcode() == ISD::SIGN_EXTEND) {
707 SDValue Sext1 = MulOp1.getOperand(0);
708 if (Sext1.getNode()->getValueType(0) != MVT::i32) {
Justin Bognerec37a022016-05-12 21:46:18 +0000709 SelectCode(N);
710 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000711 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000712 OP1 = Sext1;
713 } else if (MulOp1.getOpcode() == ISD::LOAD) {
714 LoadSDNode *LD = cast<LoadSDNode>(MulOp1.getNode());
715 if (LD->getMemoryVT() != MVT::i32 ||
716 LD->getExtensionType() != ISD::SEXTLOAD ||
717 LD->getAddressingMode() != ISD::UNINDEXED) {
Justin Bognerec37a022016-05-12 21:46:18 +0000718 SelectCode(N);
719 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000720 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000721 SDValue Chain = LD->getChain();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000722 SDValue TargetConst0 = CurDAG->getTargetConstant(0, dl, MVT::i32);
Colin LeMahieu026e88d2014-12-23 20:02:16 +0000723 OP1 = SDValue(CurDAG->getMachineNode(Hexagon::L2_loadri_io, dl, MVT::i32,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000724 MVT::Other,
725 LD->getBasePtr(), TargetConst0,
726 Chain), 0);
727 } else {
Justin Bognerec37a022016-05-12 21:46:18 +0000728 SelectCode(N);
729 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000730 }
731
732 // Generate a mpy instruction.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000733 SDNode *Result = CurDAG->getMachineNode(Hexagon::M2_dpmpyss_s0, dl,
734 MVT::i64, OP0, OP1);
Justin Bognerec37a022016-05-12 21:46:18 +0000735 ReplaceNode(N, Result);
736 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000737 }
738
Justin Bognerec37a022016-05-12 21:46:18 +0000739 SelectCode(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000740}
741
Justin Bognerec37a022016-05-12 21:46:18 +0000742void HexagonDAGToDAGISel::SelectSHL(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000743 SDLoc dl(N);
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000744 SDValue Shl_0 = N->getOperand(0);
745 SDValue Shl_1 = N->getOperand(1);
Krzysztof Parzyszekd978ae22016-08-01 20:00:33 +0000746
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000747 auto Default = [this,N] () -> void { SelectCode(N); };
748
749 if (N->getValueType(0) != MVT::i32 || Shl_1.getOpcode() != ISD::Constant)
750 return Default();
751
752 // RHS is const.
753 int32_t ShlConst = cast<ConstantSDNode>(Shl_1)->getSExtValue();
754
755 if (Shl_0.getOpcode() == ISD::MUL) {
756 SDValue Mul_0 = Shl_0.getOperand(0); // Val
757 SDValue Mul_1 = Shl_0.getOperand(1); // Const
758 // RHS of mul is const.
759 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Mul_1)) {
760 int32_t ValConst = C->getSExtValue() << ShlConst;
761 if (isInt<9>(ValConst)) {
762 SDValue Val = CurDAG->getTargetConstant(ValConst, dl, MVT::i32);
763 SDNode *Result = CurDAG->getMachineNode(Hexagon::M2_mpysmi, dl,
764 MVT::i32, Mul_0, Val);
765 ReplaceNode(N, Result);
766 return;
767 }
768 }
769 return Default();
770 }
771
772 if (Shl_0.getOpcode() == ISD::SUB) {
773 SDValue Sub_0 = Shl_0.getOperand(0); // Const 0
774 SDValue Sub_1 = Shl_0.getOperand(1); // Val
775 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Sub_0)) {
776 if (C1->getSExtValue() != 0 || Sub_1.getOpcode() != ISD::SHL)
777 return Default();
778 SDValue Shl2_0 = Sub_1.getOperand(0); // Val
779 SDValue Shl2_1 = Sub_1.getOperand(1); // Const
780 if (ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(Shl2_1)) {
781 int32_t ValConst = 1 << (ShlConst + C2->getSExtValue());
782 if (isInt<9>(-ValConst)) {
783 SDValue Val = CurDAG->getTargetConstant(-ValConst, dl, MVT::i32);
784 SDNode *Result = CurDAG->getMachineNode(Hexagon::M2_mpysmi, dl,
785 MVT::i32, Shl2_0, Val);
786 ReplaceNode(N, Result);
787 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000788 }
789 }
790 }
791 }
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000792
793 return Default();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000794}
795
796
797//
798// If there is an zero_extend followed an intrinsic in DAG (this means - the
799// result of the intrinsic is predicate); convert the zero_extend to
800// transfer instruction.
801//
802// Zero extend -> transfer is lowered here. Otherwise, zero_extend will be
803// converted into a MUX as predicate registers defined as 1 bit in the
804// compiler. Architecture defines them as 8-bit registers.
805// We want to preserve all the lower 8-bits and, not just 1 LSB bit.
806//
Justin Bognerec37a022016-05-12 21:46:18 +0000807void HexagonDAGToDAGISel::SelectZeroExtend(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000808 SDLoc dl(N);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000809
810 SDValue Op0 = N->getOperand(0);
811 EVT OpVT = Op0.getValueType();
812 unsigned OpBW = OpVT.getSizeInBits();
813
814 // Special handling for zero-extending a vector of booleans.
815 if (OpVT.isVector() && OpVT.getVectorElementType() == MVT::i1 && OpBW <= 64) {
816 SDNode *Mask = CurDAG->getMachineNode(Hexagon::C2_mask, dl, MVT::i64, Op0);
817 unsigned NE = OpVT.getVectorNumElements();
818 EVT ExVT = N->getValueType(0);
Sanjay Patel1ed771f2016-09-14 16:37:15 +0000819 unsigned ES = ExVT.getScalarSizeInBits();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000820 uint64_t MV = 0, Bit = 1;
821 for (unsigned i = 0; i < NE; ++i) {
822 MV |= Bit;
823 Bit <<= ES;
824 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000825 SDValue Ones = CurDAG->getTargetConstant(MV, dl, MVT::i64);
Krzysztof Parzyszeka3386502016-08-10 16:46:36 +0000826 SDNode *OnesReg = CurDAG->getMachineNode(Hexagon::CONST64, dl,
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000827 MVT::i64, Ones);
828 if (ExVT.getSizeInBits() == 32) {
829 SDNode *And = CurDAG->getMachineNode(Hexagon::A2_andp, dl, MVT::i64,
830 SDValue(Mask,0), SDValue(OnesReg,0));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000831 SDValue SubR = CurDAG->getTargetConstant(Hexagon::subreg_loreg, dl,
832 MVT::i32);
Justin Bognerec37a022016-05-12 21:46:18 +0000833 ReplaceNode(N, CurDAG->getMachineNode(Hexagon::EXTRACT_SUBREG, dl, ExVT,
834 SDValue(And, 0), SubR));
835 return;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000836 }
Justin Bognerec37a022016-05-12 21:46:18 +0000837 ReplaceNode(N,
838 CurDAG->getMachineNode(Hexagon::A2_andp, dl, ExVT,
839 SDValue(Mask, 0), SDValue(OnesReg, 0)));
840 return;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000841 }
842
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000843 SDNode *Int = N->getOperand(0).getNode();
844 if ((Int->getOpcode() == ISD::INTRINSIC_WO_CHAIN)) {
845 unsigned ID = cast<ConstantSDNode>(Int->getOperand(0))->getZExtValue();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000846 if (doesIntrinsicReturnPredicate(ID)) {
847 // Now we need to differentiate target data types.
848 if (N->getValueType(0) == MVT::i64) {
Krzysztof Parzyszekae14e7b2015-03-17 21:47:16 +0000849 // Convert the zero_extend to Rs = Pd followed by A2_combinew(0,Rs).
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000850 SDValue TargetConst0 = CurDAG->getTargetConstant(0, dl, MVT::i32);
Colin LeMahieu30dcb232014-12-09 18:16:49 +0000851 SDNode *Result_1 = CurDAG->getMachineNode(Hexagon::C2_tfrpr, dl,
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000852 MVT::i32, SDValue(Int, 0));
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000853 SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A2_tfrsi, dl,
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000854 MVT::i32, TargetConst0);
Colin LeMahieub580d7d2014-12-09 19:23:45 +0000855 SDNode *Result_3 = CurDAG->getMachineNode(Hexagon::A2_combinew, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000856 MVT::i64, MVT::Other,
857 SDValue(Result_2, 0),
858 SDValue(Result_1, 0));
Justin Bognerec37a022016-05-12 21:46:18 +0000859 ReplaceNode(N, Result_3);
860 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000861 }
862 if (N->getValueType(0) == MVT::i32) {
863 // Convert the zero_extend to Rs = Pd
Colin LeMahieu30dcb232014-12-09 18:16:49 +0000864 SDNode* RsPd = CurDAG->getMachineNode(Hexagon::C2_tfrpr, dl,
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000865 MVT::i32, SDValue(Int, 0));
Justin Bognerec37a022016-05-12 21:46:18 +0000866 ReplaceNode(N, RsPd);
867 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000868 }
Craig Toppere55c5562012-02-07 02:50:20 +0000869 llvm_unreachable("Unexpected value type");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000870 }
871 }
Justin Bognerec37a022016-05-12 21:46:18 +0000872 SelectCode(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000873}
874
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +0000875
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000876//
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +0000877// Handling intrinsics for circular load and bitreverse load.
Krzysztof Parzyszek47ab1f22015-03-18 16:23:44 +0000878//
Justin Bognerec37a022016-05-12 21:46:18 +0000879void HexagonDAGToDAGISel::SelectIntrinsicWChain(SDNode *N) {
880 if (MachineSDNode *L = LoadInstrForLoadIntrinsic(N)) {
881 StoreInstrForLoadIntrinsic(L, N);
Krzysztof Parzyszek0f791f42016-05-13 18:48:15 +0000882 CurDAG->RemoveDeadNode(N);
Justin Bognerec37a022016-05-12 21:46:18 +0000883 return;
884 }
885 SelectCode(N);
Krzysztof Parzyszek47ab1f22015-03-18 16:23:44 +0000886}
887
Justin Bognerec37a022016-05-12 21:46:18 +0000888void HexagonDAGToDAGISel::SelectIntrinsicWOChain(SDNode *N) {
Colin LeMahieu0ee02fc2015-01-19 20:31:18 +0000889 unsigned IID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
890 unsigned Bits;
891 switch (IID) {
892 case Intrinsic::hexagon_S2_vsplatrb:
893 Bits = 8;
894 break;
895 case Intrinsic::hexagon_S2_vsplatrh:
896 Bits = 16;
897 break;
898 default:
Justin Bognerec37a022016-05-12 21:46:18 +0000899 SelectCode(N);
900 return;
Colin LeMahieu0ee02fc2015-01-19 20:31:18 +0000901 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000902
Krzysztof Parzyszeke60e5fe2016-05-12 17:21:40 +0000903 SDValue V = N->getOperand(1);
Colin LeMahieu0ee02fc2015-01-19 20:31:18 +0000904 SDValue U;
905 if (isValueExtension(V, Bits, U)) {
906 SDValue R = CurDAG->getNode(N->getOpcode(), SDLoc(N), N->getValueType(0),
Krzysztof Parzyszeke60e5fe2016-05-12 17:21:40 +0000907 N->getOperand(0), U);
Justin Bognerd82025b2016-05-12 21:24:23 +0000908 ReplaceNode(N, R.getNode());
Justin Bognerec37a022016-05-12 21:46:18 +0000909 SelectCode(R.getNode());
910 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000911 }
Justin Bognerec37a022016-05-12 21:46:18 +0000912 SelectCode(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000913}
914
Sirish Pande69295b82012-05-10 20:20:25 +0000915//
916// Map floating point constant values.
917//
Justin Bognerec37a022016-05-12 21:46:18 +0000918void HexagonDAGToDAGISel::SelectConstantFP(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000919 SDLoc dl(N);
Sirish Pande69295b82012-05-10 20:20:25 +0000920 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N);
Krzysztof Parzyszeka3386502016-08-10 16:46:36 +0000921 APInt A = CN->getValueAPF().bitcastToAPInt();
Sirish Pande69295b82012-05-10 20:20:25 +0000922 if (N->getValueType(0) == MVT::f32) {
Krzysztof Parzyszeka3386502016-08-10 16:46:36 +0000923 SDValue V = CurDAG->getTargetConstant(A.getZExtValue(), dl, MVT::i32);
924 ReplaceNode(N, CurDAG->getMachineNode(Hexagon::A2_tfrsi, dl, MVT::f32, V));
Justin Bognerec37a022016-05-12 21:46:18 +0000925 return;
Sirish Pande69295b82012-05-10 20:20:25 +0000926 }
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000927 if (N->getValueType(0) == MVT::f64) {
Krzysztof Parzyszeka3386502016-08-10 16:46:36 +0000928 SDValue V = CurDAG->getTargetConstant(A.getZExtValue(), dl, MVT::i64);
929 ReplaceNode(N, CurDAG->getMachineNode(Hexagon::CONST64, dl, MVT::f64, V));
Justin Bognerec37a022016-05-12 21:46:18 +0000930 return;
Sirish Pande69295b82012-05-10 20:20:25 +0000931 }
932
Justin Bognerec37a022016-05-12 21:46:18 +0000933 SelectCode(N);
Sirish Pande69295b82012-05-10 20:20:25 +0000934}
935
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000936//
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +0000937// Map boolean values.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000938//
Justin Bognerec37a022016-05-12 21:46:18 +0000939void HexagonDAGToDAGISel::SelectConstant(SDNode *N) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000940 if (N->getValueType(0) == MVT::i1) {
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +0000941 assert(!(cast<ConstantSDNode>(N)->getZExtValue() >> 1));
942 unsigned Opc = (cast<ConstantSDNode>(N)->getSExtValue() != 0)
943 ? Hexagon::PS_true
944 : Hexagon::PS_false;
945 ReplaceNode(N, CurDAG->getMachineNode(Opc, SDLoc(N), MVT::i1));
946 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000947 }
948
Justin Bognerec37a022016-05-12 21:46:18 +0000949 SelectCode(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000950}
951
952
953//
954// Map add followed by a asr -> asr +=.
955//
Justin Bognerec37a022016-05-12 21:46:18 +0000956void HexagonDAGToDAGISel::SelectAdd(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000957 SDLoc dl(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000958 if (N->getValueType(0) != MVT::i32) {
Justin Bognerec37a022016-05-12 21:46:18 +0000959 SelectCode(N);
960 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000961 }
962 // Identify nodes of the form: add(asr(...)).
963 SDNode* Src1 = N->getOperand(0).getNode();
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000964 if (Src1->getOpcode() != ISD::SRA || !Src1->hasOneUse() ||
965 Src1->getValueType(0) != MVT::i32) {
Justin Bognerec37a022016-05-12 21:46:18 +0000966 SelectCode(N);
967 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000968 }
969
970 // Build Rd = Rd' + asr(Rs, Rt). The machine constraints will ensure that
971 // Rd and Rd' are assigned to the same register
Colin LeMahieu0f850bd2014-12-19 20:29:29 +0000972 SDNode* Result = CurDAG->getMachineNode(Hexagon::S2_asr_r_r_acc, dl, MVT::i32,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000973 N->getOperand(1),
974 Src1->getOperand(0),
975 Src1->getOperand(1));
Justin Bognerec37a022016-05-12 21:46:18 +0000976 ReplaceNode(N, Result);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000977}
978
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +0000979//
980// Map the following, where possible.
981// AND/FABS -> clrbit
982// OR -> setbit
983// XOR/FNEG ->toggle_bit.
984//
Justin Bognerec37a022016-05-12 21:46:18 +0000985void HexagonDAGToDAGISel::SelectBitOp(SDNode *N) {
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +0000986 SDLoc dl(N);
987 EVT ValueVT = N->getValueType(0);
988
989 // We handle only 32 and 64-bit bit ops.
990 if (!(ValueVT == MVT::i32 || ValueVT == MVT::i64 ||
Justin Bognerec37a022016-05-12 21:46:18 +0000991 ValueVT == MVT::f32 || ValueVT == MVT::f64)) {
992 SelectCode(N);
993 return;
994 }
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +0000995
996 // We handly only fabs and fneg for V5.
997 unsigned Opc = N->getOpcode();
Justin Bognerec37a022016-05-12 21:46:18 +0000998 if ((Opc == ISD::FABS || Opc == ISD::FNEG) && !HST->hasV5TOps()) {
999 SelectCode(N);
1000 return;
1001 }
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001002
1003 int64_t Val = 0;
1004 if (Opc != ISD::FABS && Opc != ISD::FNEG) {
1005 if (N->getOperand(1).getOpcode() == ISD::Constant)
1006 Val = cast<ConstantSDNode>((N)->getOperand(1))->getSExtValue();
Justin Bognerec37a022016-05-12 21:46:18 +00001007 else {
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00001008 SelectCode(N);
1009 return;
Justin Bognerec37a022016-05-12 21:46:18 +00001010 }
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001011 }
1012
1013 if (Opc == ISD::AND) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001014 // Check if this is a bit-clearing AND, if not select code the usual way.
1015 if ((ValueVT == MVT::i32 && isPowerOf2_32(~Val)) ||
1016 (ValueVT == MVT::i64 && isPowerOf2_64(~Val)))
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001017 Val = ~Val;
Justin Bognerec37a022016-05-12 21:46:18 +00001018 else {
1019 SelectCode(N);
1020 return;
1021 }
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001022 }
1023
1024 // If OR or AND is being fed by shl, srl and, sra don't do this change,
1025 // because Hexagon provide |= &= on shl, srl, and sra.
1026 // Traverse the DAG to see if there is shl, srl and sra.
1027 if (Opc == ISD::OR || Opc == ISD::AND) {
1028 switch (N->getOperand(0)->getOpcode()) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001029 default:
1030 break;
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001031 case ISD::SRA:
1032 case ISD::SRL:
1033 case ISD::SHL:
Justin Bognerec37a022016-05-12 21:46:18 +00001034 SelectCode(N);
1035 return;
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001036 }
1037 }
1038
1039 // Make sure it's power of 2.
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001040 unsigned BitPos = 0;
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001041 if (Opc != ISD::FABS && Opc != ISD::FNEG) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001042 if ((ValueVT == MVT::i32 && !isPowerOf2_32(Val)) ||
Justin Bognerec37a022016-05-12 21:46:18 +00001043 (ValueVT == MVT::i64 && !isPowerOf2_64(Val))) {
1044 SelectCode(N);
1045 return;
1046 }
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001047
1048 // Get the bit position.
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001049 BitPos = countTrailingZeros(uint64_t(Val));
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001050 } else {
1051 // For fabs and fneg, it's always the 31st bit.
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001052 BitPos = 31;
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001053 }
1054
1055 unsigned BitOpc = 0;
1056 // Set the right opcode for bitwise operations.
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001057 switch (Opc) {
1058 default:
1059 llvm_unreachable("Only bit-wise/abs/neg operations are allowed.");
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001060 case ISD::AND:
1061 case ISD::FABS:
1062 BitOpc = Hexagon::S2_clrbit_i;
1063 break;
1064 case ISD::OR:
1065 BitOpc = Hexagon::S2_setbit_i;
1066 break;
1067 case ISD::XOR:
1068 case ISD::FNEG:
1069 BitOpc = Hexagon::S2_togglebit_i;
1070 break;
1071 }
1072
1073 SDNode *Result;
1074 // Get the right SDVal for the opcode.
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001075 SDValue SDVal = CurDAG->getTargetConstant(BitPos, dl, MVT::i32);
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001076
1077 if (ValueVT == MVT::i32 || ValueVT == MVT::f32) {
1078 Result = CurDAG->getMachineNode(BitOpc, dl, ValueVT,
1079 N->getOperand(0), SDVal);
1080 } else {
1081 // 64-bit gymnastic to use REG_SEQUENCE. But it's worth it.
1082 EVT SubValueVT;
1083 if (ValueVT == MVT::i64)
1084 SubValueVT = MVT::i32;
1085 else
1086 SubValueVT = MVT::f32;
1087
1088 SDNode *Reg = N->getOperand(0).getNode();
1089 SDValue RegClass = CurDAG->getTargetConstant(Hexagon::DoubleRegsRegClassID,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001090 dl, MVT::i64);
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001091
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001092 SDValue SubregHiIdx = CurDAG->getTargetConstant(Hexagon::subreg_hireg, dl,
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001093 MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001094 SDValue SubregLoIdx = CurDAG->getTargetConstant(Hexagon::subreg_loreg, dl,
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001095 MVT::i32);
1096
1097 SDValue SubregHI = CurDAG->getTargetExtractSubreg(Hexagon::subreg_hireg, dl,
1098 MVT::i32, SDValue(Reg, 0));
1099
1100 SDValue SubregLO = CurDAG->getTargetExtractSubreg(Hexagon::subreg_loreg, dl,
1101 MVT::i32, SDValue(Reg, 0));
1102
1103 // Clear/set/toggle hi or lo registers depending on the bit position.
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001104 if (SubValueVT != MVT::f32 && BitPos < 32) {
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001105 SDNode *Result0 = CurDAG->getMachineNode(BitOpc, dl, SubValueVT,
1106 SubregLO, SDVal);
1107 const SDValue Ops[] = { RegClass, SubregHI, SubregHiIdx,
1108 SDValue(Result0, 0), SubregLoIdx };
1109 Result = CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE,
1110 dl, ValueVT, Ops);
1111 } else {
1112 if (Opc != ISD::FABS && Opc != ISD::FNEG)
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001113 SDVal = CurDAG->getTargetConstant(BitPos-32, dl, MVT::i32);
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001114 SDNode *Result0 = CurDAG->getMachineNode(BitOpc, dl, SubValueVT,
1115 SubregHI, SDVal);
1116 const SDValue Ops[] = { RegClass, SDValue(Result0, 0), SubregHiIdx,
1117 SubregLO, SubregLoIdx };
1118 Result = CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE,
1119 dl, ValueVT, Ops);
1120 }
1121 }
1122
Justin Bognerec37a022016-05-12 21:46:18 +00001123 ReplaceNode(N, Result);
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001124}
1125
1126
Justin Bognerec37a022016-05-12 21:46:18 +00001127void HexagonDAGToDAGISel::SelectFrameIndex(SDNode *N) {
Matthias Braun941a7052016-07-28 18:40:00 +00001128 MachineFrameInfo &MFI = MF->getFrameInfo();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001129 const HexagonFrameLowering *HFI = HST->getFrameLowering();
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001130 int FX = cast<FrameIndexSDNode>(N)->getIndex();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001131 unsigned StkA = HFI->getStackAlignment();
Matthias Braun941a7052016-07-28 18:40:00 +00001132 unsigned MaxA = MFI.getMaxAlignment();
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001133 SDValue FI = CurDAG->getTargetFrameIndex(FX, MVT::i32);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001134 SDLoc DL(N);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001135 SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32);
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +00001136 SDNode *R = nullptr;
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001137
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001138 // Use PS_fi when:
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001139 // - the object is fixed, or
1140 // - there are no objects with higher-than-default alignment, or
1141 // - there are no dynamically allocated objects.
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001142 // Otherwise, use PS_fia.
Matthias Braun941a7052016-07-28 18:40:00 +00001143 if (FX < 0 || MaxA <= StkA || !MFI.hasVarSizedObjects()) {
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001144 R = CurDAG->getMachineNode(Hexagon::PS_fi, DL, MVT::i32, FI, Zero);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001145 } else {
1146 auto &HMFI = *MF->getInfo<HexagonMachineFunctionInfo>();
1147 unsigned AR = HMFI.getStackAlignBaseVReg();
1148 SDValue CH = CurDAG->getEntryNode();
1149 SDValue Ops[] = { CurDAG->getCopyFromReg(CH, DL, AR, MVT::i32), FI, Zero };
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001150 R = CurDAG->getMachineNode(Hexagon::PS_fia, DL, MVT::i32, Ops);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001151 }
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001152
Justin Bognerec37a022016-05-12 21:46:18 +00001153 ReplaceNode(N, R);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001154}
1155
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001156
Krzysztof Parzyszek5da24e52016-06-27 15:08:22 +00001157void HexagonDAGToDAGISel::SelectBitcast(SDNode *N) {
1158 EVT SVT = N->getOperand(0).getValueType();
1159 EVT DVT = N->getValueType(0);
1160 if (!SVT.isVector() || !DVT.isVector() ||
1161 SVT.getVectorElementType() == MVT::i1 ||
1162 DVT.getVectorElementType() == MVT::i1 ||
1163 SVT.getSizeInBits() != DVT.getSizeInBits()) {
1164 SelectCode(N);
1165 return;
1166 }
1167
1168 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N,0), N->getOperand(0));
1169 CurDAG->RemoveDeadNode(N);
1170}
1171
1172
Justin Bognerec37a022016-05-12 21:46:18 +00001173void HexagonDAGToDAGISel::Select(SDNode *N) {
Tim Northover31d093c2013-09-22 08:21:56 +00001174 if (N->isMachineOpcode()) {
1175 N->setNodeId(-1);
Justin Bognerec37a022016-05-12 21:46:18 +00001176 return; // Already selected.
Tim Northover31d093c2013-09-22 08:21:56 +00001177 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001178
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001179 switch (N->getOpcode()) {
1180 case ISD::Constant:
Justin Bognerec37a022016-05-12 21:46:18 +00001181 SelectConstant(N);
1182 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001183
Sirish Pande69295b82012-05-10 20:20:25 +00001184 case ISD::ConstantFP:
Justin Bognerec37a022016-05-12 21:46:18 +00001185 SelectConstantFP(N);
1186 return;
Sirish Pande69295b82012-05-10 20:20:25 +00001187
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001188 case ISD::FrameIndex:
Justin Bognerec37a022016-05-12 21:46:18 +00001189 SelectFrameIndex(N);
1190 return;
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001191
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001192 case ISD::ADD:
Justin Bognerec37a022016-05-12 21:46:18 +00001193 SelectAdd(N);
1194 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001195
Krzysztof Parzyszek5da24e52016-06-27 15:08:22 +00001196 case ISD::BITCAST:
1197 SelectBitcast(N);
1198 return;
1199
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001200 case ISD::SHL:
Justin Bognerec37a022016-05-12 21:46:18 +00001201 SelectSHL(N);
1202 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001203
1204 case ISD::LOAD:
Justin Bognerec37a022016-05-12 21:46:18 +00001205 SelectLoad(N);
1206 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001207
1208 case ISD::STORE:
Justin Bognerec37a022016-05-12 21:46:18 +00001209 SelectStore(N);
1210 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001211
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001212 case ISD::MUL:
Justin Bognerec37a022016-05-12 21:46:18 +00001213 SelectMul(N);
1214 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001215
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001216 case ISD::AND:
1217 case ISD::OR:
1218 case ISD::XOR:
1219 case ISD::FABS:
1220 case ISD::FNEG:
Justin Bognerec37a022016-05-12 21:46:18 +00001221 SelectBitOp(N);
1222 return;
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001223
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001224 case ISD::ZERO_EXTEND:
Justin Bognerec37a022016-05-12 21:46:18 +00001225 SelectZeroExtend(N);
1226 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001227
Krzysztof Parzyszek47ab1f22015-03-18 16:23:44 +00001228 case ISD::INTRINSIC_W_CHAIN:
Justin Bognerec37a022016-05-12 21:46:18 +00001229 SelectIntrinsicWChain(N);
1230 return;
Krzysztof Parzyszek47ab1f22015-03-18 16:23:44 +00001231
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001232 case ISD::INTRINSIC_WO_CHAIN:
Justin Bognerec37a022016-05-12 21:46:18 +00001233 SelectIntrinsicWOChain(N);
1234 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001235 }
1236
Justin Bognerec37a022016-05-12 21:46:18 +00001237 SelectCode(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001238}
1239
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001240bool HexagonDAGToDAGISel::
Daniel Sanders60f1db02015-03-13 12:45:09 +00001241SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001242 std::vector<SDValue> &OutOps) {
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001243 SDValue Inp = Op, Res;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001244
Daniel Sanders60f1db02015-03-13 12:45:09 +00001245 switch (ConstraintID) {
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001246 default:
1247 return true;
Daniel Sanders49f643c2015-03-17 14:37:39 +00001248 case InlineAsm::Constraint_i:
1249 case InlineAsm::Constraint_o: // Offsetable.
1250 case InlineAsm::Constraint_v: // Not offsetable.
1251 case InlineAsm::Constraint_m: // Memory.
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001252 if (SelectAddrFI(Inp, Res))
1253 OutOps.push_back(Res);
1254 else
1255 OutOps.push_back(Inp);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001256 break;
1257 }
1258
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001259 OutOps.push_back(CurDAG->getTargetConstant(0, SDLoc(Op), MVT::i32));
Jyotsna Vermad9225242013-02-13 21:38:46 +00001260 return false;
1261}
Colin LeMahieuc7522f32015-01-14 23:07:36 +00001262
Colin LeMahieu79ec0652015-06-12 19:57:32 +00001263
Krzysztof Parzyszekae14e7b2015-03-17 21:47:16 +00001264void HexagonDAGToDAGISel::PreprocessISelDAG() {
1265 SelectionDAG &DAG = *CurDAG;
1266 std::vector<SDNode*> Nodes;
Pete Cooper7e64ef02015-07-14 23:43:29 +00001267 for (SDNode &Node : DAG.allnodes())
1268 Nodes.push_back(&Node);
Krzysztof Parzyszekae14e7b2015-03-17 21:47:16 +00001269
1270 // Simplify: (or (select c x 0) z) -> (select c (or x z) z)
1271 // (or (select c 0 y) z) -> (select c z (or y z))
1272 // This may not be the right thing for all targets, so do it here.
Krzysztof Parzyszekf7f70682016-06-22 20:08:27 +00001273 for (auto I : Nodes) {
Krzysztof Parzyszekae14e7b2015-03-17 21:47:16 +00001274 if (I->getOpcode() != ISD::OR)
1275 continue;
1276
1277 auto IsZero = [] (const SDValue &V) -> bool {
1278 if (ConstantSDNode *SC = dyn_cast<ConstantSDNode>(V.getNode()))
1279 return SC->isNullValue();
1280 return false;
1281 };
1282 auto IsSelect0 = [IsZero] (const SDValue &Op) -> bool {
1283 if (Op.getOpcode() != ISD::SELECT)
1284 return false;
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +00001285 return IsZero(Op.getOperand(1)) || IsZero(Op.getOperand(2));
Krzysztof Parzyszekae14e7b2015-03-17 21:47:16 +00001286 };
1287
1288 SDValue N0 = I->getOperand(0), N1 = I->getOperand(1);
1289 EVT VT = I->getValueType(0);
1290 bool SelN0 = IsSelect0(N0);
1291 SDValue SOp = SelN0 ? N0 : N1;
1292 SDValue VOp = SelN0 ? N1 : N0;
1293
1294 if (SOp.getOpcode() == ISD::SELECT && SOp.getNode()->hasOneUse()) {
1295 SDValue SC = SOp.getOperand(0);
1296 SDValue SX = SOp.getOperand(1);
1297 SDValue SY = SOp.getOperand(2);
1298 SDLoc DLS = SOp;
1299 if (IsZero(SY)) {
1300 SDValue NewOr = DAG.getNode(ISD::OR, DLS, VT, SX, VOp);
1301 SDValue NewSel = DAG.getNode(ISD::SELECT, DLS, VT, SC, NewOr, VOp);
1302 DAG.ReplaceAllUsesWith(I, NewSel.getNode());
1303 } else if (IsZero(SX)) {
1304 SDValue NewOr = DAG.getNode(ISD::OR, DLS, VT, SY, VOp);
1305 SDValue NewSel = DAG.getNode(ISD::SELECT, DLS, VT, SC, VOp, NewOr);
1306 DAG.ReplaceAllUsesWith(I, NewSel.getNode());
1307 }
1308 }
1309 }
Krzysztof Parzyszekf7f70682016-06-22 20:08:27 +00001310
1311 // Transform: (store ch addr (add x (add (shl y c) e)))
1312 // to: (store ch addr (add x (shl (add y d) c))),
1313 // where e = (shl d c) for some integer d.
1314 // The purpose of this is to enable generation of loads/stores with
1315 // shifted addressing mode, i.e. mem(x+y<<#c). For that, the shift
1316 // value c must be 0, 1 or 2.
1317 for (auto I : Nodes) {
1318 if (I->getOpcode() != ISD::STORE)
1319 continue;
1320
1321 // I matched: (store ch addr Off)
1322 SDValue Off = I->getOperand(2);
1323 // Off needs to match: (add x (add (shl y c) (shl d c))))
1324 if (Off.getOpcode() != ISD::ADD)
1325 continue;
1326 // Off matched: (add x T0)
1327 SDValue T0 = Off.getOperand(1);
1328 // T0 needs to match: (add T1 T2):
1329 if (T0.getOpcode() != ISD::ADD)
1330 continue;
1331 // T0 matched: (add T1 T2)
1332 SDValue T1 = T0.getOperand(0);
1333 SDValue T2 = T0.getOperand(1);
1334 // T1 needs to match: (shl y c)
1335 if (T1.getOpcode() != ISD::SHL)
1336 continue;
1337 SDValue C = T1.getOperand(1);
1338 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(C.getNode());
1339 if (CN == nullptr)
1340 continue;
1341 unsigned CV = CN->getZExtValue();
1342 if (CV > 2)
1343 continue;
1344 // T2 needs to match e, where e = (shl d c) for some d.
1345 ConstantSDNode *EN = dyn_cast<ConstantSDNode>(T2.getNode());
1346 if (EN == nullptr)
1347 continue;
1348 unsigned EV = EN->getZExtValue();
1349 if (EV % (1 << CV) != 0)
1350 continue;
1351 unsigned DV = EV / (1 << CV);
1352
1353 // Replace T0 with: (shl (add y d) c)
1354 SDLoc DL = SDLoc(I);
1355 EVT VT = T0.getValueType();
1356 SDValue D = DAG.getConstant(DV, DL, VT);
1357 // NewAdd = (add y d)
1358 SDValue NewAdd = DAG.getNode(ISD::ADD, DL, VT, T1.getOperand(0), D);
1359 // NewShl = (shl NewAdd c)
1360 SDValue NewShl = DAG.getNode(ISD::SHL, DL, VT, NewAdd, C);
1361 ReplaceNode(T0.getNode(), NewShl.getNode());
1362 }
Krzysztof Parzyszek0006e1a2016-07-29 15:15:35 +00001363
1364 if (EnableAddressRebalancing) {
1365 rebalanceAddressTrees();
1366
1367 DEBUG(
1368 dbgs() << "************* SelectionDAG after preprocessing: ***********\n";
1369 CurDAG->dump();
1370 dbgs() << "************* End SelectionDAG after preprocessing ********\n";
1371 );
1372 }
Krzysztof Parzyszekae14e7b2015-03-17 21:47:16 +00001373}
1374
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001375void HexagonDAGToDAGISel::EmitFunctionEntryCode() {
1376 auto &HST = static_cast<const HexagonSubtarget&>(MF->getSubtarget());
1377 auto &HFI = *HST.getFrameLowering();
1378 if (!HFI.needsAligna(*MF))
1379 return;
Krzysztof Parzyszekae14e7b2015-03-17 21:47:16 +00001380
Matthias Braun941a7052016-07-28 18:40:00 +00001381 MachineFrameInfo &MFI = MF->getFrameInfo();
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +00001382 MachineBasicBlock *EntryBB = &MF->front();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001383 unsigned AR = FuncInfo->CreateReg(MVT::i32);
Matthias Braun941a7052016-07-28 18:40:00 +00001384 unsigned MaxA = MFI.getMaxAlignment();
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001385 BuildMI(EntryBB, DebugLoc(), HII->get(Hexagon::PS_aligna), AR)
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001386 .addImm(MaxA);
1387 MF->getInfo<HexagonMachineFunctionInfo>()->setStackAlignBaseVReg(AR);
1388}
1389
1390// Match a frame index that can be used in an addressing mode.
Colin LeMahieuc7522f32015-01-14 23:07:36 +00001391bool HexagonDAGToDAGISel::SelectAddrFI(SDValue& N, SDValue &R) {
1392 if (N.getOpcode() != ISD::FrameIndex)
1393 return false;
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001394 auto &HFI = *HST->getFrameLowering();
Matthias Braun941a7052016-07-28 18:40:00 +00001395 MachineFrameInfo &MFI = MF->getFrameInfo();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001396 int FX = cast<FrameIndexSDNode>(N)->getIndex();
Matthias Braun941a7052016-07-28 18:40:00 +00001397 if (!MFI.isFixedObjectIndex(FX) && HFI.needsAligna(*MF))
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001398 return false;
1399 R = CurDAG->getTargetFrameIndex(FX, MVT::i32);
Colin LeMahieuc7522f32015-01-14 23:07:36 +00001400 return true;
1401}
Colin LeMahieu0ee02fc2015-01-19 20:31:18 +00001402
Colin LeMahieu987b0942015-02-04 20:38:01 +00001403inline bool HexagonDAGToDAGISel::SelectAddrGA(SDValue &N, SDValue &R) {
1404 return SelectGlobalAddress(N, R, false);
1405}
1406
Colin LeMahieu51491352015-02-04 22:36:28 +00001407inline bool HexagonDAGToDAGISel::SelectAddrGP(SDValue &N, SDValue &R) {
1408 return SelectGlobalAddress(N, R, true);
1409}
1410
Colin LeMahieu987b0942015-02-04 20:38:01 +00001411bool HexagonDAGToDAGISel::SelectGlobalAddress(SDValue &N, SDValue &R,
1412 bool UseGP) {
1413 switch (N.getOpcode()) {
1414 case ISD::ADD: {
1415 SDValue N0 = N.getOperand(0);
1416 SDValue N1 = N.getOperand(1);
1417 unsigned GAOpc = N0.getOpcode();
1418 if (UseGP && GAOpc != HexagonISD::CONST32_GP)
1419 return false;
1420 if (!UseGP && GAOpc != HexagonISD::CONST32)
1421 return false;
1422 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N1)) {
1423 SDValue Addr = N0.getOperand(0);
1424 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Addr)) {
1425 if (GA->getOpcode() == ISD::TargetGlobalAddress) {
1426 uint64_t NewOff = GA->getOffset() + (uint64_t)Const->getSExtValue();
1427 R = CurDAG->getTargetGlobalAddress(GA->getGlobal(), SDLoc(Const),
1428 N.getValueType(), NewOff);
1429 return true;
1430 }
1431 }
1432 }
1433 break;
1434 }
1435 case HexagonISD::CONST32:
1436 // The operand(0) of CONST32 is TargetGlobalAddress, which is what we
1437 // want in the instruction.
1438 if (!UseGP)
1439 R = N.getOperand(0);
1440 return !UseGP;
1441 case HexagonISD::CONST32_GP:
1442 if (UseGP)
1443 R = N.getOperand(0);
1444 return UseGP;
1445 default:
1446 return false;
1447 }
1448
1449 return false;
1450}
1451
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001452bool HexagonDAGToDAGISel::isValueExtension(const SDValue &Val,
1453 unsigned FromBits, SDValue &Src) {
Colin LeMahieu0ee02fc2015-01-19 20:31:18 +00001454 unsigned Opc = Val.getOpcode();
1455 switch (Opc) {
1456 case ISD::SIGN_EXTEND:
1457 case ISD::ZERO_EXTEND:
1458 case ISD::ANY_EXTEND: {
1459 SDValue const &Op0 = Val.getOperand(0);
1460 EVT T = Op0.getValueType();
1461 if (T.isInteger() && T.getSizeInBits() == FromBits) {
1462 Src = Op0;
1463 return true;
1464 }
1465 break;
1466 }
1467 case ISD::SIGN_EXTEND_INREG:
1468 case ISD::AssertSext:
1469 case ISD::AssertZext:
1470 if (Val.getOperand(0).getValueType().isInteger()) {
1471 VTSDNode *T = cast<VTSDNode>(Val.getOperand(1));
1472 if (T->getVT().getSizeInBits() == FromBits) {
1473 Src = Val.getOperand(0);
1474 return true;
1475 }
1476 }
1477 break;
1478 case ISD::AND: {
1479 // Check if this is an AND with "FromBits" of lower bits set to 1.
1480 uint64_t FromMask = (1 << FromBits) - 1;
1481 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(0))) {
1482 if (C->getZExtValue() == FromMask) {
1483 Src = Val.getOperand(1);
1484 return true;
1485 }
1486 }
1487 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(1))) {
1488 if (C->getZExtValue() == FromMask) {
1489 Src = Val.getOperand(0);
1490 return true;
1491 }
1492 }
1493 break;
1494 }
1495 case ISD::OR:
1496 case ISD::XOR: {
1497 // OR/XOR with the lower "FromBits" bits set to 0.
1498 uint64_t FromMask = (1 << FromBits) - 1;
1499 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(0))) {
1500 if ((C->getZExtValue() & FromMask) == 0) {
1501 Src = Val.getOperand(1);
1502 return true;
1503 }
1504 }
1505 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(1))) {
1506 if ((C->getZExtValue() & FromMask) == 0) {
1507 Src = Val.getOperand(0);
1508 return true;
1509 }
1510 }
1511 }
1512 default:
1513 break;
1514 }
1515 return false;
1516}
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +00001517
Krzysztof Parzyszekbba0bf72016-07-15 15:35:52 +00001518
1519bool HexagonDAGToDAGISel::orIsAdd(const SDNode *N) const {
1520 assert(N->getOpcode() == ISD::OR);
1521 auto *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
1522 assert(C);
1523
1524 // Detect when "or" is used to add an offset to a stack object.
1525 if (auto *FN = dyn_cast<FrameIndexSDNode>(N->getOperand(0))) {
Matthias Braun941a7052016-07-28 18:40:00 +00001526 MachineFrameInfo &MFI = MF->getFrameInfo();
1527 unsigned A = MFI.getObjectAlignment(FN->getIndex());
Krzysztof Parzyszekbba0bf72016-07-15 15:35:52 +00001528 assert(isPowerOf2_32(A));
1529 int32_t Off = C->getSExtValue();
1530 // If the alleged offset fits in the zero bits guaranteed by
1531 // the alignment, then this or is really an add.
1532 return (Off >= 0) && (((A-1) & Off) == unsigned(Off));
1533 }
1534 return false;
1535}
1536
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +00001537bool HexagonDAGToDAGISel::isAlignedMemNode(const MemSDNode *N) const {
1538 return N->getAlignment() >= N->getMemoryVT().getStoreSize();
1539}
Krzysztof Parzyszek0006e1a2016-07-29 15:15:35 +00001540
1541////////////////////////////////////////////////////////////////////////////////
1542// Rebalancing of address calculation trees
1543
1544static bool isOpcodeHandled(const SDNode *N) {
1545 switch (N->getOpcode()) {
1546 case ISD::ADD:
1547 case ISD::MUL:
1548 return true;
1549 case ISD::SHL:
1550 // We only handle constant shifts because these can be easily flattened
1551 // into multiplications by 2^Op1.
1552 return isa<ConstantSDNode>(N->getOperand(1).getNode());
1553 default:
1554 return false;
1555 }
1556}
1557
1558/// \brief Return the weight of an SDNode
1559int HexagonDAGToDAGISel::getWeight(SDNode *N) {
1560 if (!isOpcodeHandled(N))
1561 return 1;
1562 assert(RootWeights.count(N) && "Cannot get weight of unseen root!");
1563 assert(RootWeights[N] != -1 && "Cannot get weight of unvisited root!");
1564 assert(RootWeights[N] != -2 && "Cannot get weight of RAWU'd root!");
1565 return RootWeights[N];
1566}
1567
1568int HexagonDAGToDAGISel::getHeight(SDNode *N) {
1569 if (!isOpcodeHandled(N))
1570 return 0;
1571 assert(RootWeights.count(N) && RootWeights[N] >= 0 &&
1572 "Cannot query height of unvisited/RAUW'd node!");
1573 return RootHeights[N];
1574}
1575
Benjamin Kramerb7d33112016-08-06 11:13:10 +00001576namespace {
Krzysztof Parzyszek0006e1a2016-07-29 15:15:35 +00001577struct WeightedLeaf {
1578 SDValue Value;
1579 int Weight;
1580 int InsertionOrder;
1581
1582 WeightedLeaf() : Value(SDValue()) { }
1583
1584 WeightedLeaf(SDValue Value, int Weight, int InsertionOrder) :
1585 Value(Value), Weight(Weight), InsertionOrder(InsertionOrder) {
1586 assert(Weight >= 0 && "Weight must be >= 0");
1587 }
1588
1589 static bool Compare(const WeightedLeaf &A, const WeightedLeaf &B) {
1590 assert(A.Value.getNode() && B.Value.getNode());
1591 return A.Weight == B.Weight ?
1592 (A.InsertionOrder > B.InsertionOrder) :
1593 (A.Weight > B.Weight);
1594 }
1595};
1596
1597/// A specialized priority queue for WeigthedLeaves. It automatically folds
1598/// constants and allows removal of non-top elements while maintaining the
1599/// priority order.
1600class LeafPrioQueue {
1601 SmallVector<WeightedLeaf, 8> Q;
1602 bool HaveConst;
1603 WeightedLeaf ConstElt;
1604 unsigned Opcode;
1605
1606public:
1607 bool empty() {
1608 return (!HaveConst && Q.empty());
1609 }
1610
1611 size_t size() {
1612 return Q.size() + HaveConst;
1613 }
1614
1615 bool hasConst() {
1616 return HaveConst;
1617 }
1618
1619 const WeightedLeaf &top() {
1620 if (HaveConst)
1621 return ConstElt;
1622 return Q.front();
1623 }
1624
1625 WeightedLeaf pop() {
1626 if (HaveConst) {
1627 HaveConst = false;
1628 return ConstElt;
1629 }
1630 std::pop_heap(Q.begin(), Q.end(), WeightedLeaf::Compare);
1631 return Q.pop_back_val();
1632 }
1633
1634 void push(WeightedLeaf L, bool SeparateConst=true) {
1635 if (!HaveConst && SeparateConst && isa<ConstantSDNode>(L.Value)) {
1636 if (Opcode == ISD::MUL &&
1637 cast<ConstantSDNode>(L.Value)->getSExtValue() == 1)
1638 return;
1639 if (Opcode == ISD::ADD &&
1640 cast<ConstantSDNode>(L.Value)->getSExtValue() == 0)
1641 return;
1642
1643 HaveConst = true;
1644 ConstElt = L;
1645 } else {
1646 Q.push_back(L);
1647 std::push_heap(Q.begin(), Q.end(), WeightedLeaf::Compare);
1648 }
1649 }
1650
1651 /// Push L to the bottom of the queue regardless of its weight. If L is
1652 /// constant, it will not be folded with other constants in the queue.
1653 void pushToBottom(WeightedLeaf L) {
1654 L.Weight = 1000;
1655 push(L, false);
1656 }
1657
1658 /// Search for a SHL(x, [<=MaxAmount]) subtree in the queue, return the one of
1659 /// lowest weight and remove it from the queue.
1660 WeightedLeaf findSHL(uint64_t MaxAmount);
1661
1662 WeightedLeaf findMULbyConst();
1663
1664 LeafPrioQueue(unsigned Opcode) :
1665 HaveConst(false), Opcode(Opcode) { }
1666};
Benjamin Kramerb7d33112016-08-06 11:13:10 +00001667} // end anonymous namespace
Krzysztof Parzyszek0006e1a2016-07-29 15:15:35 +00001668
1669WeightedLeaf LeafPrioQueue::findSHL(uint64_t MaxAmount) {
1670 int ResultPos;
1671 WeightedLeaf Result;
1672
1673 for (int Pos = 0, End = Q.size(); Pos != End; ++Pos) {
1674 const WeightedLeaf &L = Q[Pos];
1675 const SDValue &Val = L.Value;
1676 if (Val.getOpcode() != ISD::SHL ||
1677 !isa<ConstantSDNode>(Val.getOperand(1)) ||
1678 Val.getConstantOperandVal(1) > MaxAmount)
1679 continue;
1680 if (!Result.Value.getNode() || Result.Weight > L.Weight ||
1681 (Result.Weight == L.Weight && Result.InsertionOrder > L.InsertionOrder))
1682 {
1683 Result = L;
1684 ResultPos = Pos;
1685 }
1686 }
1687
1688 if (Result.Value.getNode()) {
1689 Q.erase(&Q[ResultPos]);
1690 std::make_heap(Q.begin(), Q.end(), WeightedLeaf::Compare);
1691 }
1692
1693 return Result;
1694}
1695
1696WeightedLeaf LeafPrioQueue::findMULbyConst() {
1697 int ResultPos;
1698 WeightedLeaf Result;
1699
1700 for (int Pos = 0, End = Q.size(); Pos != End; ++Pos) {
1701 const WeightedLeaf &L = Q[Pos];
1702 const SDValue &Val = L.Value;
1703 if (Val.getOpcode() != ISD::MUL ||
1704 !isa<ConstantSDNode>(Val.getOperand(1)) ||
1705 Val.getConstantOperandVal(1) > 127)
1706 continue;
1707 if (!Result.Value.getNode() || Result.Weight > L.Weight ||
1708 (Result.Weight == L.Weight && Result.InsertionOrder > L.InsertionOrder))
1709 {
1710 Result = L;
1711 ResultPos = Pos;
1712 }
1713 }
1714
1715 if (Result.Value.getNode()) {
1716 Q.erase(&Q[ResultPos]);
1717 std::make_heap(Q.begin(), Q.end(), WeightedLeaf::Compare);
1718 }
1719
1720 return Result;
1721}
1722
1723SDValue HexagonDAGToDAGISel::getMultiplierForSHL(SDNode *N) {
Simon Pilgrim7c858622016-07-29 18:43:59 +00001724 uint64_t MulFactor = 1ull << N->getConstantOperandVal(1);
Krzysztof Parzyszek0006e1a2016-07-29 15:15:35 +00001725 return CurDAG->getConstant(MulFactor, SDLoc(N),
1726 N->getOperand(1).getValueType());
1727}
1728
1729/// @returns the value x for which 2^x is a factor of Val
1730static unsigned getPowerOf2Factor(SDValue Val) {
1731 if (Val.getOpcode() == ISD::MUL) {
1732 unsigned MaxFactor = 0;
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +00001733 for (int i = 0; i < 2; ++i) {
Krzysztof Parzyszek0006e1a2016-07-29 15:15:35 +00001734 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(i));
1735 if (!C)
1736 continue;
1737 const APInt &CInt = C->getAPIntValue();
1738 if (CInt.getBoolValue())
1739 MaxFactor = CInt.countTrailingZeros();
1740 }
1741 return MaxFactor;
1742 }
1743 if (Val.getOpcode() == ISD::SHL) {
1744 if (!isa<ConstantSDNode>(Val.getOperand(1).getNode()))
1745 return 0;
1746 return (unsigned) Val.getConstantOperandVal(1);
1747 }
1748
1749 return 0;
1750}
1751
1752/// @returns true if V>>Amount will eliminate V's operation on its child
1753static bool willShiftRightEliminate(SDValue V, unsigned Amount) {
1754 if (V.getOpcode() == ISD::MUL) {
1755 SDValue Ops[] = { V.getOperand(0), V.getOperand(1) };
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +00001756 for (int i = 0; i < 2; ++i)
Krzysztof Parzyszek0006e1a2016-07-29 15:15:35 +00001757 if (isa<ConstantSDNode>(Ops[i].getNode()) &&
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +00001758 V.getConstantOperandVal(i) % (1ULL << Amount) == 0) {
Krzysztof Parzyszek0006e1a2016-07-29 15:15:35 +00001759 uint64_t NewConst = V.getConstantOperandVal(i) >> Amount;
1760 return (NewConst == 1);
1761 }
1762 } else if (V.getOpcode() == ISD::SHL) {
1763 return (Amount == V.getConstantOperandVal(1));
1764 }
1765
1766 return false;
1767}
1768
1769SDValue HexagonDAGToDAGISel::factorOutPowerOf2(SDValue V, unsigned Power) {
1770 SDValue Ops[] = { V.getOperand(0), V.getOperand(1) };
1771 if (V.getOpcode() == ISD::MUL) {
1772 for (int i=0; i < 2; ++i) {
1773 if (isa<ConstantSDNode>(Ops[i].getNode()) &&
1774 V.getConstantOperandVal(i) % ((uint64_t)1 << Power) == 0) {
1775 uint64_t NewConst = V.getConstantOperandVal(i) >> Power;
1776 if (NewConst == 1)
1777 return Ops[!i];
1778 Ops[i] = CurDAG->getConstant(NewConst,
1779 SDLoc(V), V.getValueType());
1780 break;
1781 }
1782 }
1783 } else if (V.getOpcode() == ISD::SHL) {
1784 uint64_t ShiftAmount = V.getConstantOperandVal(1);
1785 if (ShiftAmount == Power)
1786 return Ops[0];
1787 Ops[1] = CurDAG->getConstant(ShiftAmount - Power,
1788 SDLoc(V), V.getValueType());
1789 }
1790
1791 return CurDAG->getNode(V.getOpcode(), SDLoc(V), V.getValueType(), Ops);
1792}
1793
1794static bool isTargetConstant(const SDValue &V) {
1795 return V.getOpcode() == HexagonISD::CONST32 ||
1796 V.getOpcode() == HexagonISD::CONST32_GP;
1797}
1798
1799unsigned HexagonDAGToDAGISel::getUsesInFunction(const Value *V) {
1800 if (GAUsesInFunction.count(V))
1801 return GAUsesInFunction[V];
1802
1803 unsigned Result = 0;
1804 const Function *CurF = CurDAG->getMachineFunction().getFunction();
1805 for (const User *U : V->users()) {
1806 if (isa<Instruction>(U) &&
1807 cast<Instruction>(U)->getParent()->getParent() == CurF)
1808 ++Result;
1809 }
1810
1811 GAUsesInFunction[V] = Result;
1812
1813 return Result;
1814}
1815
1816/// Note - After calling this, N may be dead. It may have been replaced by a
1817/// new node, so always use the returned value in place of N.
1818///
1819/// @returns The SDValue taking the place of N (which could be N if it is
1820/// unchanged)
1821SDValue HexagonDAGToDAGISel::balanceSubTree(SDNode *N, bool TopLevel) {
1822 assert(RootWeights.count(N) && "Cannot balance non-root node.");
1823 assert(RootWeights[N] != -2 && "This node was RAUW'd!");
1824 assert(!TopLevel || N->getOpcode() == ISD::ADD);
1825
1826 // Return early if this node was already visited
1827 if (RootWeights[N] != -1)
1828 return SDValue(N, 0);
1829
1830 assert(isOpcodeHandled(N));
1831
1832 SDValue Op0 = N->getOperand(0);
1833 SDValue Op1 = N->getOperand(1);
1834
1835 // Return early if the operands will remain unchanged or are all roots
1836 if ((!isOpcodeHandled(Op0.getNode()) || RootWeights.count(Op0.getNode())) &&
1837 (!isOpcodeHandled(Op1.getNode()) || RootWeights.count(Op1.getNode()))) {
1838 SDNode *Op0N = Op0.getNode();
1839 int Weight;
1840 if (isOpcodeHandled(Op0N) && RootWeights[Op0N] == -1) {
1841 Weight = getWeight(balanceSubTree(Op0N).getNode());
1842 // Weight = calculateWeight(Op0N);
1843 } else
1844 Weight = getWeight(Op0N);
1845
1846 SDNode *Op1N = N->getOperand(1).getNode(); // Op1 may have been RAUWd
1847 if (isOpcodeHandled(Op1N) && RootWeights[Op1N] == -1) {
1848 Weight += getWeight(balanceSubTree(Op1N).getNode());
1849 // Weight += calculateWeight(Op1N);
1850 } else
1851 Weight += getWeight(Op1N);
1852
1853 RootWeights[N] = Weight;
1854 RootHeights[N] = std::max(getHeight(N->getOperand(0).getNode()),
1855 getHeight(N->getOperand(1).getNode())) + 1;
1856
1857 DEBUG(dbgs() << "--> No need to balance root (Weight=" << Weight
1858 << " Height=" << RootHeights[N] << "): ");
1859 DEBUG(N->dump());
1860
1861 return SDValue(N, 0);
1862 }
1863
1864 DEBUG(dbgs() << "** Balancing root node: ");
1865 DEBUG(N->dump());
1866
1867 unsigned NOpcode = N->getOpcode();
1868
1869 LeafPrioQueue Leaves(NOpcode);
1870 SmallVector<SDValue, 4> Worklist;
1871 Worklist.push_back(SDValue(N, 0));
1872
1873 // SHL nodes will be converted to MUL nodes
1874 if (NOpcode == ISD::SHL)
1875 NOpcode = ISD::MUL;
1876
1877 bool CanFactorize = false;
1878 WeightedLeaf Mul1, Mul2;
1879 unsigned MaxPowerOf2 = 0;
1880 WeightedLeaf GA;
1881
1882 // Do not try to factor out a shift if there is already a shift at the tip of
1883 // the tree.
1884 bool HaveTopLevelShift = false;
1885 if (TopLevel &&
1886 ((isOpcodeHandled(Op0.getNode()) && Op0.getOpcode() == ISD::SHL &&
1887 Op0.getConstantOperandVal(1) < 4) ||
1888 (isOpcodeHandled(Op1.getNode()) && Op1.getOpcode() == ISD::SHL &&
1889 Op1.getConstantOperandVal(1) < 4)))
1890 HaveTopLevelShift = true;
1891
1892 // Flatten the subtree into an ordered list of leaves; at the same time
1893 // determine whether the tree is already balanced.
1894 int InsertionOrder = 0;
1895 SmallDenseMap<SDValue, int> NodeHeights;
1896 bool Imbalanced = false;
1897 int CurrentWeight = 0;
1898 while (!Worklist.empty()) {
1899 SDValue Child = Worklist.pop_back_val();
1900
1901 if (Child.getNode() != N && RootWeights.count(Child.getNode())) {
1902 // CASE 1: Child is a root note
1903
1904 int Weight = RootWeights[Child.getNode()];
1905 if (Weight == -1) {
1906 Child = balanceSubTree(Child.getNode());
1907 // calculateWeight(Child.getNode());
1908 Weight = getWeight(Child.getNode());
1909 } else if (Weight == -2) {
1910 // Whoops, this node was RAUWd by one of the balanceSubTree calls we
1911 // made. Our worklist isn't up to date anymore.
1912 // Restart the whole process.
1913 DEBUG(dbgs() << "--> Subtree was RAUWd. Restarting...\n");
1914 return balanceSubTree(N, TopLevel);
1915 }
1916
1917 NodeHeights[Child] = 1;
1918 CurrentWeight += Weight;
1919
1920 unsigned PowerOf2;
1921 if (TopLevel && !CanFactorize && !HaveTopLevelShift &&
1922 (Child.getOpcode() == ISD::MUL || Child.getOpcode() == ISD::SHL) &&
1923 Child.hasOneUse() && (PowerOf2 = getPowerOf2Factor(Child))) {
1924 // Try to identify two factorizable MUL/SHL children greedily. Leave
1925 // them out of the priority queue for now so we can deal with them
1926 // after.
1927 if (!Mul1.Value.getNode()) {
1928 Mul1 = WeightedLeaf(Child, Weight, InsertionOrder++);
1929 MaxPowerOf2 = PowerOf2;
1930 } else {
1931 Mul2 = WeightedLeaf(Child, Weight, InsertionOrder++);
1932 MaxPowerOf2 = std::min(MaxPowerOf2, PowerOf2);
1933
1934 // Our addressing modes can only shift by a maximum of 3
1935 if (MaxPowerOf2 > 3)
1936 MaxPowerOf2 = 3;
1937
1938 CanFactorize = true;
1939 }
1940 } else
1941 Leaves.push(WeightedLeaf(Child, Weight, InsertionOrder++));
1942 } else if (!isOpcodeHandled(Child.getNode())) {
1943 // CASE 2: Child is an unhandled kind of node (e.g. constant)
1944 int Weight = getWeight(Child.getNode());
1945
1946 NodeHeights[Child] = getHeight(Child.getNode());
1947 CurrentWeight += Weight;
1948
1949 if (isTargetConstant(Child) && !GA.Value.getNode())
1950 GA = WeightedLeaf(Child, Weight, InsertionOrder++);
1951 else
1952 Leaves.push(WeightedLeaf(Child, Weight, InsertionOrder++));
1953 } else {
1954 // CASE 3: Child is a subtree of same opcode
1955 // Visit children first, then flatten.
1956 unsigned ChildOpcode = Child.getOpcode();
1957 assert(ChildOpcode == NOpcode ||
1958 (NOpcode == ISD::MUL && ChildOpcode == ISD::SHL));
1959
1960 // Convert SHL to MUL
1961 SDValue Op1;
1962 if (ChildOpcode == ISD::SHL)
1963 Op1 = getMultiplierForSHL(Child.getNode());
1964 else
1965 Op1 = Child->getOperand(1);
1966
1967 if (!NodeHeights.count(Op1) || !NodeHeights.count(Child->getOperand(0))) {
1968 assert(!NodeHeights.count(Child) && "Parent visited before children?");
1969 // Visit children first, then re-visit this node
1970 Worklist.push_back(Child);
1971 Worklist.push_back(Op1);
1972 Worklist.push_back(Child->getOperand(0));
1973 } else {
1974 // Back at this node after visiting the children
1975 if (std::abs(NodeHeights[Op1] - NodeHeights[Child->getOperand(0)]) > 1)
1976 Imbalanced = true;
1977
1978 NodeHeights[Child] = std::max(NodeHeights[Op1],
1979 NodeHeights[Child->getOperand(0)]) + 1;
1980 }
1981 }
1982 }
1983
1984 DEBUG(dbgs() << "--> Current height=" << NodeHeights[SDValue(N, 0)]
1985 << " weight=" << CurrentWeight << " imbalanced="
1986 << Imbalanced << "\n");
1987
1988 // Transform MUL(x, C * 2^Y) + SHL(z, Y) -> SHL(ADD(MUL(x, C), z), Y)
1989 // This factors out a shift in order to match memw(a<<Y+b).
1990 if (CanFactorize && (willShiftRightEliminate(Mul1.Value, MaxPowerOf2) ||
1991 willShiftRightEliminate(Mul2.Value, MaxPowerOf2))) {
1992 DEBUG(dbgs() << "--> Found common factor for two MUL children!\n");
1993 int Weight = Mul1.Weight + Mul2.Weight;
1994 int Height = std::max(NodeHeights[Mul1.Value], NodeHeights[Mul2.Value]) + 1;
1995 SDValue Mul1Factored = factorOutPowerOf2(Mul1.Value, MaxPowerOf2);
1996 SDValue Mul2Factored = factorOutPowerOf2(Mul2.Value, MaxPowerOf2);
1997 SDValue Sum = CurDAG->getNode(ISD::ADD, SDLoc(N), Mul1.Value.getValueType(),
1998 Mul1Factored, Mul2Factored);
1999 SDValue Const = CurDAG->getConstant(MaxPowerOf2, SDLoc(N),
2000 Mul1.Value.getValueType());
2001 SDValue New = CurDAG->getNode(ISD::SHL, SDLoc(N), Mul1.Value.getValueType(),
2002 Sum, Const);
2003 NodeHeights[New] = Height;
2004 Leaves.push(WeightedLeaf(New, Weight, Mul1.InsertionOrder));
2005 } else if (Mul1.Value.getNode()) {
2006 // We failed to factorize two MULs, so now the Muls are left outside the
2007 // queue... add them back.
2008 Leaves.push(Mul1);
2009 if (Mul2.Value.getNode())
2010 Leaves.push(Mul2);
2011 CanFactorize = false;
2012 }
2013
2014 // Combine GA + Constant -> GA+Offset, but only if GA is not used elsewhere
2015 // and the root node itself is not used more than twice. This reduces the
2016 // amount of additional constant extenders introduced by this optimization.
2017 bool CombinedGA = false;
2018 if (NOpcode == ISD::ADD && GA.Value.getNode() && Leaves.hasConst() &&
2019 GA.Value.hasOneUse() && N->use_size() < 3) {
2020 GlobalAddressSDNode *GANode =
2021 cast<GlobalAddressSDNode>(GA.Value.getOperand(0));
2022 ConstantSDNode *Offset = cast<ConstantSDNode>(Leaves.top().Value);
2023
2024 if (getUsesInFunction(GANode->getGlobal()) == 1 && Offset->hasOneUse() &&
2025 getTargetLowering()->isOffsetFoldingLegal(GANode)) {
2026 DEBUG(dbgs() << "--> Combining GA and offset (" << Offset->getSExtValue()
2027 << "): ");
2028 DEBUG(GANode->dump());
2029
2030 SDValue NewTGA =
2031 CurDAG->getTargetGlobalAddress(GANode->getGlobal(), SDLoc(GA.Value),
2032 GANode->getValueType(0),
2033 GANode->getOffset() + (uint64_t)Offset->getSExtValue());
2034 GA.Value = CurDAG->getNode(GA.Value.getOpcode(), SDLoc(GA.Value),
2035 GA.Value.getValueType(), NewTGA);
2036 GA.Weight += Leaves.top().Weight;
2037
2038 NodeHeights[GA.Value] = getHeight(GA.Value.getNode());
2039 CombinedGA = true;
2040
2041 Leaves.pop(); // Remove the offset constant from the queue
2042 }
2043 }
2044
2045 if ((RebalanceOnlyForOptimizations && !CanFactorize && !CombinedGA) ||
2046 (RebalanceOnlyImbalancedTrees && !Imbalanced)) {
2047 RootWeights[N] = CurrentWeight;
2048 RootHeights[N] = NodeHeights[SDValue(N, 0)];
2049
2050 return SDValue(N, 0);
2051 }
2052
2053 // Combine GA + SHL(x, C<=31) so we will match Rx=add(#u8,asl(Rx,#U5))
2054 if (NOpcode == ISD::ADD && GA.Value.getNode()) {
2055 WeightedLeaf SHL = Leaves.findSHL(31);
2056 if (SHL.Value.getNode()) {
2057 int Height = std::max(NodeHeights[GA.Value], NodeHeights[SHL.Value]) + 1;
2058 GA.Value = CurDAG->getNode(ISD::ADD, SDLoc(GA.Value),
2059 GA.Value.getValueType(),
2060 GA.Value, SHL.Value);
2061 GA.Weight = SHL.Weight; // Specifically ignore the GA weight here
2062 NodeHeights[GA.Value] = Height;
2063 }
2064 }
2065
2066 if (GA.Value.getNode())
2067 Leaves.push(GA);
2068
2069 // If this is the top level and we haven't factored out a shift, we should try
2070 // to move a constant to the bottom to match addressing modes like memw(rX+C)
2071 if (TopLevel && !CanFactorize && Leaves.hasConst()) {
2072 DEBUG(dbgs() << "--> Pushing constant to tip of tree.");
2073 Leaves.pushToBottom(Leaves.pop());
2074 }
2075
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +00002076 const DataLayout &DL = CurDAG->getDataLayout();
2077 const TargetLowering &TLI = *getTargetLowering();
2078
Krzysztof Parzyszek0006e1a2016-07-29 15:15:35 +00002079 // Rebuild the tree using Huffman's algorithm
2080 while (Leaves.size() > 1) {
2081 WeightedLeaf L0 = Leaves.pop();
2082
2083 // See whether we can grab a MUL to form an add(Rx,mpyi(Ry,#u6)),
2084 // otherwise just get the next leaf
2085 WeightedLeaf L1 = Leaves.findMULbyConst();
2086 if (!L1.Value.getNode())
2087 L1 = Leaves.pop();
2088
2089 assert(L0.Weight <= L1.Weight && "Priority queue is broken!");
2090
2091 SDValue V0 = L0.Value;
2092 int V0Weight = L0.Weight;
2093 SDValue V1 = L1.Value;
2094 int V1Weight = L1.Weight;
2095
2096 // Make sure that none of these nodes have been RAUW'd
2097 if ((RootWeights.count(V0.getNode()) && RootWeights[V0.getNode()] == -2) ||
2098 (RootWeights.count(V1.getNode()) && RootWeights[V1.getNode()] == -2)) {
2099 DEBUG(dbgs() << "--> Subtree was RAUWd. Restarting...\n");
2100 return balanceSubTree(N, TopLevel);
2101 }
2102
2103 ConstantSDNode *V0C = dyn_cast<ConstantSDNode>(V0);
2104 ConstantSDNode *V1C = dyn_cast<ConstantSDNode>(V1);
2105 EVT VT = N->getValueType(0);
2106 SDValue NewNode;
2107
2108 if (V0C && !V1C) {
2109 std::swap(V0, V1);
2110 std::swap(V0C, V1C);
2111 }
2112
2113 // Calculate height of this node
2114 assert(NodeHeights.count(V0) && NodeHeights.count(V1) &&
2115 "Children must have been visited before re-combining them!");
2116 int Height = std::max(NodeHeights[V0], NodeHeights[V1]) + 1;
2117
2118 // Rebuild this node (and restore SHL from MUL if needed)
2119 if (V1C && NOpcode == ISD::MUL && V1C->getAPIntValue().isPowerOf2())
2120 NewNode = CurDAG->getNode(
2121 ISD::SHL, SDLoc(V0), VT, V0,
2122 CurDAG->getConstant(
2123 V1C->getAPIntValue().logBase2(), SDLoc(N),
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +00002124 TLI.getScalarShiftAmountTy(DL, V0.getValueType())));
Krzysztof Parzyszek0006e1a2016-07-29 15:15:35 +00002125 else
2126 NewNode = CurDAG->getNode(NOpcode, SDLoc(N), VT, V0, V1);
2127
2128 NodeHeights[NewNode] = Height;
2129
2130 int Weight = V0Weight + V1Weight;
2131 Leaves.push(WeightedLeaf(NewNode, Weight, L0.InsertionOrder));
2132
2133 DEBUG(dbgs() << "--> Built new node (Weight=" << Weight << ",Height="
2134 << Height << "):\n");
2135 DEBUG(NewNode.dump());
2136 }
2137
2138 assert(Leaves.size() == 1);
2139 SDValue NewRoot = Leaves.top().Value;
2140
2141 assert(NodeHeights.count(NewRoot));
2142 int Height = NodeHeights[NewRoot];
2143
2144 // Restore SHL if we earlier converted it to a MUL
2145 if (NewRoot.getOpcode() == ISD::MUL) {
2146 ConstantSDNode *V1C = dyn_cast<ConstantSDNode>(NewRoot.getOperand(1));
2147 if (V1C && V1C->getAPIntValue().isPowerOf2()) {
2148 EVT VT = NewRoot.getValueType();
2149 SDValue V0 = NewRoot.getOperand(0);
2150 NewRoot = CurDAG->getNode(
2151 ISD::SHL, SDLoc(NewRoot), VT, V0,
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +00002152 CurDAG->getConstant(
2153 V1C->getAPIntValue().logBase2(), SDLoc(NewRoot),
2154 TLI.getScalarShiftAmountTy(DL, V0.getValueType())));
Krzysztof Parzyszek0006e1a2016-07-29 15:15:35 +00002155 }
2156 }
2157
2158 if (N != NewRoot.getNode()) {
2159 DEBUG(dbgs() << "--> Root is now: ");
2160 DEBUG(NewRoot.dump());
2161
2162 // Replace all uses of old root by new root
2163 CurDAG->ReplaceAllUsesWith(N, NewRoot.getNode());
2164 // Mark that we have RAUW'd N
2165 RootWeights[N] = -2;
2166 } else {
2167 DEBUG(dbgs() << "--> Root unchanged.\n");
2168 }
2169
2170 RootWeights[NewRoot.getNode()] = Leaves.top().Weight;
2171 RootHeights[NewRoot.getNode()] = Height;
2172
2173 return NewRoot;
2174}
2175
2176void HexagonDAGToDAGISel::rebalanceAddressTrees() {
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +00002177 for (auto I = CurDAG->allnodes_begin(), E = CurDAG->allnodes_end(); I != E;) {
Krzysztof Parzyszek0006e1a2016-07-29 15:15:35 +00002178 SDNode *N = &*I++;
2179 if (N->getOpcode() != ISD::LOAD && N->getOpcode() != ISD::STORE)
2180 continue;
2181
2182 SDValue BasePtr = cast<MemSDNode>(N)->getBasePtr();
2183 if (BasePtr.getOpcode() != ISD::ADD)
2184 continue;
2185
2186 // We've already processed this node
2187 if (RootWeights.count(BasePtr.getNode()))
2188 continue;
2189
2190 DEBUG(dbgs() << "** Rebalancing address calculation in node: ");
2191 DEBUG(N->dump());
2192
2193 // FindRoots
2194 SmallVector<SDNode *, 4> Worklist;
2195
2196 Worklist.push_back(BasePtr.getOperand(0).getNode());
2197 Worklist.push_back(BasePtr.getOperand(1).getNode());
2198
2199 while (!Worklist.empty()) {
2200 SDNode *N = Worklist.pop_back_val();
2201 unsigned Opcode = N->getOpcode();
2202
2203 if (!isOpcodeHandled(N))
2204 continue;
2205
2206 Worklist.push_back(N->getOperand(0).getNode());
2207 Worklist.push_back(N->getOperand(1).getNode());
2208
2209 // Not a root if it has only one use and same opcode as its parent
2210 if (N->hasOneUse() && Opcode == N->use_begin()->getOpcode())
2211 continue;
2212
2213 // This root node has already been processed
2214 if (RootWeights.count(N))
2215 continue;
2216
2217 RootWeights[N] = -1;
2218 }
2219
2220 // Balance node itself
2221 RootWeights[BasePtr.getNode()] = -1;
2222 SDValue NewBasePtr = balanceSubTree(BasePtr.getNode(), /*TopLevel=*/ true);
2223
2224 if (N->getOpcode() == ISD::LOAD)
2225 N = CurDAG->UpdateNodeOperands(N, N->getOperand(0),
2226 NewBasePtr, N->getOperand(2));
2227 else
2228 N = CurDAG->UpdateNodeOperands(N, N->getOperand(0), N->getOperand(1),
2229 NewBasePtr, N->getOperand(3));
2230
2231 DEBUG(dbgs() << "--> Final node: ");
2232 DEBUG(N->dump());
2233 }
2234
2235 CurDAG->RemoveDeadNodes();
2236 GAUsesInFunction.clear();
2237 RootHeights.clear();
2238 RootWeights.clear();
2239}
2240