blob: a46a335353a3272435b91353e056e2b77aa4fdf9 [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"
16#include "HexagonTargetMachine.h"
Jyotsna Vermad9225242013-02-13 21:38:46 +000017#include "llvm/ADT/DenseMap.h"
Jyotsna Vermad9225242013-02-13 21:38:46 +000018#include "llvm/CodeGen/SelectionDAGISel.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000019#include "llvm/IR/Intrinsics.h"
Jyotsna Vermad9225242013-02-13 21:38:46 +000020#include "llvm/Support/CommandLine.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000021#include "llvm/Support/Compiler.h"
22#include "llvm/Support/Debug.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000023using namespace llvm;
24
Chandler Carruth84e68b22014-04-22 02:41:26 +000025#define DEBUG_TYPE "hexagon-isel"
26
Jyotsna Vermad9225242013-02-13 21:38:46 +000027static
28cl::opt<unsigned>
29MaxNumOfUsesForConstExtenders("ga-max-num-uses-for-constant-extenders",
30 cl::Hidden, cl::init(2),
31 cl::desc("Maximum number of uses of a global address such that we still us a"
32 "constant extended instruction"));
Tony Linthicum1213a7a2011-12-12 21:14:40 +000033
34//===----------------------------------------------------------------------===//
35// Instruction Selector Implementation
36//===----------------------------------------------------------------------===//
37
Jyotsna Vermad9225242013-02-13 21:38:46 +000038namespace llvm {
39 void initializeHexagonDAGToDAGISelPass(PassRegistry&);
40}
41
Tony Linthicum1213a7a2011-12-12 21:14:40 +000042//===--------------------------------------------------------------------===//
43/// HexagonDAGToDAGISel - Hexagon specific code to select Hexagon machine
44/// instructions for SelectionDAG operations.
45///
46namespace {
47class HexagonDAGToDAGISel : public SelectionDAGISel {
48 /// Subtarget - Keep a pointer to the Hexagon Subtarget around so that we can
49 /// make the right decision when generating code for different targets.
50 const HexagonSubtarget &Subtarget;
51
52 // Keep a reference to HexagonTargetMachine.
Krzysztof Parzyszekd5007472013-05-06 18:38:37 +000053 const HexagonTargetMachine& TM;
Jyotsna Vermad9225242013-02-13 21:38:46 +000054 DenseMap<const GlobalValue *, unsigned> GlobalAddressUseCountMap;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000055public:
Bill Wendlinga3cd3502013-06-19 21:36:55 +000056 explicit HexagonDAGToDAGISel(HexagonTargetMachine &targetmachine,
Jyotsna Vermad9225242013-02-13 21:38:46 +000057 CodeGenOpt::Level OptLevel)
58 : SelectionDAGISel(targetmachine, OptLevel),
Tony Linthicum1213a7a2011-12-12 21:14:40 +000059 Subtarget(targetmachine.getSubtarget<HexagonSubtarget>()),
Bill Wendling4a7a4082013-06-07 06:19:56 +000060 TM(targetmachine) {
Jyotsna Vermad9225242013-02-13 21:38:46 +000061 initializeHexagonDAGToDAGISelPass(*PassRegistry::getPassRegistry());
Tony Linthicum1213a7a2011-12-12 21:14:40 +000062 }
Jyotsna Vermad9225242013-02-13 21:38:46 +000063 bool hasNumUsesBelowThresGA(SDNode *N) const;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000064
Craig Topper906c2cd2014-04-29 07:58:16 +000065 SDNode *Select(SDNode *N) override;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000066
67 // Complex Pattern Selectors.
Jyotsna Vermad9225242013-02-13 21:38:46 +000068 inline bool foldGlobalAddress(SDValue &N, SDValue &R);
69 inline bool foldGlobalAddressGP(SDValue &N, SDValue &R);
70 bool foldGlobalAddressImpl(SDValue &N, SDValue &R, bool ShouldLookForGP);
Tony Linthicum1213a7a2011-12-12 21:14:40 +000071 bool SelectADDRri(SDValue& N, SDValue &R1, SDValue &R2);
72 bool SelectADDRriS11_0(SDValue& N, SDValue &R1, SDValue &R2);
73 bool SelectADDRriS11_1(SDValue& N, SDValue &R1, SDValue &R2);
74 bool SelectADDRriS11_2(SDValue& N, SDValue &R1, SDValue &R2);
75 bool SelectMEMriS11_2(SDValue& Addr, SDValue &Base, SDValue &Offset);
76 bool SelectADDRriS11_3(SDValue& N, SDValue &R1, SDValue &R2);
77 bool SelectADDRrr(SDValue &Addr, SDValue &Base, SDValue &Offset);
78 bool SelectADDRriU6_0(SDValue& N, SDValue &R1, SDValue &R2);
79 bool SelectADDRriU6_1(SDValue& N, SDValue &R1, SDValue &R2);
80 bool SelectADDRriU6_2(SDValue& N, SDValue &R1, SDValue &R2);
81
Colin LeMahieuc7522f32015-01-14 23:07:36 +000082 bool SelectAddrFI(SDValue &N, SDValue &R);
83
Craig Topper906c2cd2014-04-29 07:58:16 +000084 const char *getPassName() const override {
Tony Linthicum1213a7a2011-12-12 21:14:40 +000085 return "Hexagon DAG->DAG Pattern Instruction Selection";
86 }
87
88 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
89 /// inline asm expressions.
Craig Topper906c2cd2014-04-29 07:58:16 +000090 bool SelectInlineAsmMemoryOperand(const SDValue &Op,
91 char ConstraintCode,
92 std::vector<SDValue> &OutOps) override;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000093 bool SelectAddr(SDNode *Op, SDValue Addr, SDValue &Base, SDValue &Offset);
94
95 SDNode *SelectLoad(SDNode *N);
Andrew Trickef9de2a2013-05-25 02:42:55 +000096 SDNode *SelectBaseOffsetLoad(LoadSDNode *LD, SDLoc dl);
97 SDNode *SelectIndexedLoad(LoadSDNode *LD, SDLoc dl);
Tony Linthicum1213a7a2011-12-12 21:14:40 +000098 SDNode *SelectIndexedLoadZeroExtend64(LoadSDNode *LD, unsigned Opcode,
Andrew Trickef9de2a2013-05-25 02:42:55 +000099 SDLoc dl);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000100 SDNode *SelectIndexedLoadSignExtend64(LoadSDNode *LD, unsigned Opcode,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000101 SDLoc dl);
102 SDNode *SelectBaseOffsetStore(StoreSDNode *ST, SDLoc dl);
103 SDNode *SelectIndexedStore(StoreSDNode *ST, SDLoc dl);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000104 SDNode *SelectStore(SDNode *N);
105 SDNode *SelectSHL(SDNode *N);
106 SDNode *SelectSelect(SDNode *N);
107 SDNode *SelectTruncate(SDNode *N);
108 SDNode *SelectMul(SDNode *N);
109 SDNode *SelectZeroExtend(SDNode *N);
110 SDNode *SelectIntrinsicWOChain(SDNode *N);
Sirish Pande69295b82012-05-10 20:20:25 +0000111 SDNode *SelectIntrinsicWChain(SDNode *N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000112 SDNode *SelectConstant(SDNode *N);
Sirish Pande69295b82012-05-10 20:20:25 +0000113 SDNode *SelectConstantFP(SDNode *N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000114 SDNode *SelectAdd(SDNode *N);
Jyotsna Verma519b3852012-11-28 20:58:14 +0000115 bool isConstExtProfitable(SDNode *N) const;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000116
Jyotsna Vermafdc660b2013-03-22 18:41:34 +0000117// XformMskToBitPosU5Imm - Returns the bit position which
118// the single bit 32 bit mask represents.
119// Used in Clr and Set bit immediate memops.
120SDValue XformMskToBitPosU5Imm(uint32_t Imm) {
121 int32_t bitPos;
122 bitPos = Log2_32(Imm);
123 assert(bitPos >= 0 && bitPos < 32 &&
124 "Constant out of range for 32 BitPos Memops");
125 return CurDAG->getTargetConstant(bitPos, MVT::i32);
126}
127
128// XformMskToBitPosU4Imm - Returns the bit position which the single bit 16 bit
129// mask represents. Used in Clr and Set bit immediate memops.
130SDValue XformMskToBitPosU4Imm(uint16_t Imm) {
131 return XformMskToBitPosU5Imm(Imm);
132}
133
134// XformMskToBitPosU3Imm - Returns the bit position which the single bit 8 bit
135// mask represents. Used in Clr and Set bit immediate memops.
136SDValue XformMskToBitPosU3Imm(uint8_t Imm) {
137 return XformMskToBitPosU5Imm(Imm);
138}
139
140// Return true if there is exactly one bit set in V, i.e., if V is one of the
141// following integers: 2^0, 2^1, ..., 2^31.
142bool ImmIsSingleBit(uint32_t v) const {
143 uint32_t c = CountPopulation_64(v);
144 // Only return true if we counted 1 bit.
145 return c == 1;
146}
147
148// XformM5ToU5Imm - Return a target constant with the specified value, of type
149// i32 where the negative literal is transformed into a positive literal for
150// use in -= memops.
151inline SDValue XformM5ToU5Imm(signed Imm) {
152 assert( (Imm >= -31 && Imm <= -1) && "Constant out of range for Memops");
153 return CurDAG->getTargetConstant( - Imm, MVT::i32);
154}
155
156
Jyotsna Verma60316252013-02-05 19:20:45 +0000157// XformU7ToU7M1Imm - Return a target constant decremented by 1, in range
158// [1..128], used in cmpb.gtu instructions.
159inline SDValue XformU7ToU7M1Imm(signed Imm) {
160 assert((Imm >= 1 && Imm <= 128) && "Constant out of range for cmpb op");
161 return CurDAG->getTargetConstant(Imm - 1, MVT::i8);
162}
163
Jyotsna Verma89c84822013-04-23 19:15:55 +0000164// XformS8ToS8M1Imm - Return a target constant decremented by 1.
165inline SDValue XformSToSM1Imm(signed Imm) {
166 return CurDAG->getTargetConstant(Imm - 1, MVT::i32);
167}
168
169// XformU8ToU8M1Imm - Return a target constant decremented by 1.
170inline SDValue XformUToUM1Imm(unsigned Imm) {
171 assert((Imm >= 1) && "Cannot decrement unsigned int less than 1");
172 return CurDAG->getTargetConstant(Imm - 1, MVT::i32);
173}
174
Jyotsna Verma60316252013-02-05 19:20:45 +0000175// Include the pieces autogenerated from the target description.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000176#include "HexagonGenDAGISel.inc"
177};
178} // end anonymous namespace
179
180
181/// createHexagonISelDag - This pass converts a legalized DAG into a
182/// Hexagon-specific DAG, ready for instruction scheduling.
183///
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000184FunctionPass *llvm::createHexagonISelDag(HexagonTargetMachine &TM,
Jyotsna Vermad9225242013-02-13 21:38:46 +0000185 CodeGenOpt::Level OptLevel) {
186 return new HexagonDAGToDAGISel(TM, OptLevel);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000187}
188
Jyotsna Vermad9225242013-02-13 21:38:46 +0000189static void initializePassOnce(PassRegistry &Registry) {
190 const char *Name = "Hexagon DAG->DAG Pattern Instruction Selection";
191 PassInfo *PI = new PassInfo(Name, "hexagon-isel",
Craig Topper062a2ba2014-04-25 05:30:21 +0000192 &SelectionDAGISel::ID, nullptr, false, false);
Jyotsna Vermad9225242013-02-13 21:38:46 +0000193 Registry.registerPass(*PI, true);
194}
195
196void llvm::initializeHexagonDAGToDAGISelPass(PassRegistry &Registry) {
197 CALL_ONCE_INITIALIZATION(initializePassOnce)
198}
199
200
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000201static bool IsS11_0_Offset(SDNode * S) {
202 ConstantSDNode *N = cast<ConstantSDNode>(S);
203
204 // immS16 predicate - True if the immediate fits in a 16-bit sign extended
205 // field.
206 int64_t v = (int64_t)N->getSExtValue();
207 return isInt<11>(v);
208}
209
210
211static bool IsS11_1_Offset(SDNode * S) {
212 ConstantSDNode *N = cast<ConstantSDNode>(S);
213
214 // immS16 predicate - True if the immediate fits in a 16-bit sign extended
215 // field.
216 int64_t v = (int64_t)N->getSExtValue();
217 return isShiftedInt<11,1>(v);
218}
219
220
221static bool IsS11_2_Offset(SDNode * S) {
222 ConstantSDNode *N = cast<ConstantSDNode>(S);
223
224 // immS16 predicate - True if the immediate fits in a 16-bit sign extended
225 // field.
226 int64_t v = (int64_t)N->getSExtValue();
227 return isShiftedInt<11,2>(v);
228}
229
230
231static bool IsS11_3_Offset(SDNode * S) {
232 ConstantSDNode *N = cast<ConstantSDNode>(S);
233
234 // immS16 predicate - True if the immediate fits in a 16-bit sign extended
235 // field.
236 int64_t v = (int64_t)N->getSExtValue();
237 return isShiftedInt<11,3>(v);
238}
239
240
241static bool IsU6_0_Offset(SDNode * S) {
242 ConstantSDNode *N = cast<ConstantSDNode>(S);
243
244 // u6 predicate - True if the immediate fits in a 6-bit unsigned extended
245 // field.
246 int64_t v = (int64_t)N->getSExtValue();
247 return isUInt<6>(v);
248}
249
250
251static bool IsU6_1_Offset(SDNode * S) {
252 ConstantSDNode *N = cast<ConstantSDNode>(S);
253
254 // u6 predicate - True if the immediate fits in a 6-bit unsigned extended
255 // field.
256 int64_t v = (int64_t)N->getSExtValue();
257 return isShiftedUInt<6,1>(v);
258}
259
260
261static bool IsU6_2_Offset(SDNode * S) {
262 ConstantSDNode *N = cast<ConstantSDNode>(S);
263
264 // u6 predicate - True if the immediate fits in a 6-bit unsigned extended
265 // field.
266 int64_t v = (int64_t)N->getSExtValue();
267 return isShiftedUInt<6,2>(v);
268}
269
270
271// Intrinsics that return a a predicate.
272static unsigned doesIntrinsicReturnPredicate(unsigned ID)
273{
274 switch (ID) {
275 default:
276 return 0;
277 case Intrinsic::hexagon_C2_cmpeq:
278 case Intrinsic::hexagon_C2_cmpgt:
279 case Intrinsic::hexagon_C2_cmpgtu:
280 case Intrinsic::hexagon_C2_cmpgtup:
281 case Intrinsic::hexagon_C2_cmpgtp:
282 case Intrinsic::hexagon_C2_cmpeqp:
283 case Intrinsic::hexagon_C2_bitsset:
284 case Intrinsic::hexagon_C2_bitsclr:
285 case Intrinsic::hexagon_C2_cmpeqi:
286 case Intrinsic::hexagon_C2_cmpgti:
287 case Intrinsic::hexagon_C2_cmpgtui:
288 case Intrinsic::hexagon_C2_cmpgei:
289 case Intrinsic::hexagon_C2_cmpgeui:
290 case Intrinsic::hexagon_C2_cmplt:
291 case Intrinsic::hexagon_C2_cmpltu:
292 case Intrinsic::hexagon_C2_bitsclri:
293 case Intrinsic::hexagon_C2_and:
294 case Intrinsic::hexagon_C2_or:
295 case Intrinsic::hexagon_C2_xor:
296 case Intrinsic::hexagon_C2_andn:
297 case Intrinsic::hexagon_C2_not:
298 case Intrinsic::hexagon_C2_orn:
299 case Intrinsic::hexagon_C2_pxfer_map:
300 case Intrinsic::hexagon_C2_any8:
301 case Intrinsic::hexagon_C2_all8:
302 case Intrinsic::hexagon_A2_vcmpbeq:
303 case Intrinsic::hexagon_A2_vcmpbgtu:
304 case Intrinsic::hexagon_A2_vcmpheq:
305 case Intrinsic::hexagon_A2_vcmphgt:
306 case Intrinsic::hexagon_A2_vcmphgtu:
307 case Intrinsic::hexagon_A2_vcmpweq:
308 case Intrinsic::hexagon_A2_vcmpwgt:
309 case Intrinsic::hexagon_A2_vcmpwgtu:
310 case Intrinsic::hexagon_C2_tfrrp:
311 case Intrinsic::hexagon_S2_tstbit_i:
312 case Intrinsic::hexagon_S2_tstbit_r:
313 return 1;
314 }
315}
316
317
318// Intrinsics that have predicate operands.
319static unsigned doesIntrinsicContainPredicate(unsigned ID)
320{
321 switch (ID) {
322 default:
323 return 0;
324 case Intrinsic::hexagon_C2_tfrpr:
Colin LeMahieu30dcb232014-12-09 18:16:49 +0000325 return Hexagon::C2_tfrpr;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000326 case Intrinsic::hexagon_C2_and:
Colin LeMahieu5cf56322014-12-08 23:55:43 +0000327 return Hexagon::C2_and;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000328 case Intrinsic::hexagon_C2_xor:
Colin LeMahieu5cf56322014-12-08 23:55:43 +0000329 return Hexagon::C2_xor;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000330 case Intrinsic::hexagon_C2_or:
Colin LeMahieu5cf56322014-12-08 23:55:43 +0000331 return Hexagon::C2_or;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000332 case Intrinsic::hexagon_C2_not:
Colin LeMahieu5cf56322014-12-08 23:55:43 +0000333 return Hexagon::C2_not;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000334 case Intrinsic::hexagon_C2_any8:
Colin LeMahieu5cf56322014-12-08 23:55:43 +0000335 return Hexagon::C2_any8;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000336 case Intrinsic::hexagon_C2_all8:
Colin LeMahieu5cf56322014-12-08 23:55:43 +0000337 return Hexagon::C2_all8;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000338 case Intrinsic::hexagon_C2_vitpack:
Colin LeMahieu5cf56322014-12-08 23:55:43 +0000339 return Hexagon::C2_vitpack;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000340 case Intrinsic::hexagon_C2_mask:
Colin LeMahieu5cf56322014-12-08 23:55:43 +0000341 return Hexagon::C2_mask;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000342 case Intrinsic::hexagon_C2_mux:
Colin LeMahieue83bc742014-11-25 20:20:09 +0000343 return Hexagon::C2_mux;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000344
345 // Mapping hexagon_C2_muxir to MUX_pri. This is pretty weird - but
346 // that's how it's mapped in q6protos.h.
347 case Intrinsic::hexagon_C2_muxir:
Colin LeMahieu9665f982014-12-05 21:09:27 +0000348 return Hexagon::C2_muxri;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000349
350 // Mapping hexagon_C2_muxri to MUX_pir. This is pretty weird - but
351 // that's how it's mapped in q6protos.h.
352 case Intrinsic::hexagon_C2_muxri:
Colin LeMahieu9665f982014-12-05 21:09:27 +0000353 return Hexagon::C2_muxir;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000354
355 case Intrinsic::hexagon_C2_muxii:
Colin LeMahieu9665f982014-12-05 21:09:27 +0000356 return Hexagon::C2_muxii;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000357 case Intrinsic::hexagon_C2_vmux:
358 return Hexagon::VMUX_prr64;
359 case Intrinsic::hexagon_S2_valignrb:
360 return Hexagon::VALIGN_rrp;
361 case Intrinsic::hexagon_S2_vsplicerb:
362 return Hexagon::VSPLICE_rrp;
363 }
364}
365
366
367static bool OffsetFitsS11(EVT MemType, int64_t Offset) {
368 if (MemType == MVT::i64 && isShiftedInt<11,3>(Offset)) {
369 return true;
370 }
371 if (MemType == MVT::i32 && isShiftedInt<11,2>(Offset)) {
372 return true;
373 }
374 if (MemType == MVT::i16 && isShiftedInt<11,1>(Offset)) {
375 return true;
376 }
377 if (MemType == MVT::i8 && isInt<11>(Offset)) {
378 return true;
379 }
380 return false;
381}
382
383
384//
385// Try to lower loads of GlobalAdresses into base+offset loads. Custom
386// lowering for GlobalAddress nodes has already turned it into a
387// CONST32.
388//
Andrew Trickef9de2a2013-05-25 02:42:55 +0000389SDNode *HexagonDAGToDAGISel::SelectBaseOffsetLoad(LoadSDNode *LD, SDLoc dl) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000390 SDValue Chain = LD->getChain();
391 SDNode* Const32 = LD->getBasePtr().getNode();
392 unsigned Opcode = 0;
393
394 if (Const32->getOpcode() == HexagonISD::CONST32 &&
395 ISD::isNormalLoad(LD)) {
396 SDValue Base = Const32->getOperand(0);
397 EVT LoadedVT = LD->getMemoryVT();
398 int64_t Offset = cast<GlobalAddressSDNode>(Base)->getOffset();
399 if (Offset != 0 && OffsetFitsS11(LoadedVT, Offset)) {
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000400 MVT PointerTy = getTargetLowering()->getPointerTy();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000401 const GlobalValue* GV =
402 cast<GlobalAddressSDNode>(Base)->getGlobal();
403 SDValue TargAddr =
404 CurDAG->getTargetGlobalAddress(GV, dl, PointerTy, 0);
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000405 SDNode* NewBase = CurDAG->getMachineNode(Hexagon::CONST32_set,
406 dl, PointerTy,
407 TargAddr);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000408 // Figure out base + offset opcode
Colin LeMahieu947cd702014-12-23 20:44:59 +0000409 if (LoadedVT == MVT::i64) Opcode = Hexagon::L2_loadrd_io;
Colin LeMahieu026e88d2014-12-23 20:02:16 +0000410 else if (LoadedVT == MVT::i32) Opcode = Hexagon::L2_loadri_io;
Colin LeMahieu8e39cad2014-12-23 17:25:57 +0000411 else if (LoadedVT == MVT::i16) Opcode = Hexagon::L2_loadrh_io;
Colin LeMahieu4b1eac42014-12-22 21:40:43 +0000412 else if (LoadedVT == MVT::i8) Opcode = Hexagon::L2_loadrb_io;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000413 else llvm_unreachable("unknown memory type");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000414
415 // Build indexed load.
416 SDValue TargetConstOff = CurDAG->getTargetConstant(Offset, PointerTy);
417 SDNode* Result = CurDAG->getMachineNode(Opcode, dl,
418 LD->getValueType(0),
419 MVT::Other,
420 SDValue(NewBase,0),
421 TargetConstOff,
422 Chain);
423 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
424 MemOp[0] = LD->getMemOperand();
425 cast<MachineSDNode>(Result)->setMemRefs(MemOp, MemOp + 1);
426 ReplaceUses(LD, Result);
427 return Result;
428 }
429 }
430
431 return SelectCode(LD);
432}
433
434
435SDNode *HexagonDAGToDAGISel::SelectIndexedLoadSignExtend64(LoadSDNode *LD,
436 unsigned Opcode,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000437 SDLoc dl)
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000438{
439 SDValue Chain = LD->getChain();
440 EVT LoadedVT = LD->getMemoryVT();
441 SDValue Base = LD->getBasePtr();
442 SDValue Offset = LD->getOffset();
443 SDNode *OffsetNode = Offset.getNode();
444 int32_t Val = cast<ConstantSDNode>(OffsetNode)->getSExtValue();
445 SDValue N1 = LD->getOperand(1);
446 SDValue CPTmpN1_0;
447 SDValue CPTmpN1_1;
Bill Wendling4a7a4082013-06-07 06:19:56 +0000448
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000449 if (SelectADDRriS11_2(N1, CPTmpN1_0, CPTmpN1_1) &&
450 N1.getNode()->getValueType(0) == MVT::i32) {
Eric Christopherd9134482014-08-04 21:25:23 +0000451 const HexagonInstrInfo *TII = static_cast<const HexagonInstrInfo *>(
452 TM.getSubtargetImpl()->getInstrInfo());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000453 if (TII->isValidAutoIncImm(LoadedVT, Val)) {
454 SDValue TargetConst = CurDAG->getTargetConstant(Val, MVT::i32);
455 SDNode *Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::i32, MVT::i32,
456 MVT::Other, Base, TargetConst,
457 Chain);
Colin LeMahieueb52f692014-12-11 16:43:06 +0000458 SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A2_sxtw, dl, MVT::i64,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000459 SDValue(Result_1, 0));
460 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
461 MemOp[0] = LD->getMemOperand();
462 cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
463 const SDValue Froms[] = { SDValue(LD, 0),
464 SDValue(LD, 1),
465 SDValue(LD, 2)
466 };
467 const SDValue Tos[] = { SDValue(Result_2, 0),
468 SDValue(Result_1, 1),
469 SDValue(Result_1, 2)
470 };
471 ReplaceUses(Froms, Tos, 3);
472 return Result_2;
Sirish Pandec92c3162012-05-03 16:18:50 +0000473 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000474 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
475 SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
476 SDNode *Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::i32,
477 MVT::Other, Base, TargetConst0,
478 Chain);
Colin LeMahieueb52f692014-12-11 16:43:06 +0000479 SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A2_sxtw, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000480 MVT::i64, SDValue(Result_1, 0));
481 SDNode* Result_3 = CurDAG->getMachineNode(Hexagon::ADD_ri, dl,
482 MVT::i32, Base, TargetConstVal,
483 SDValue(Result_1, 1));
484 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
485 MemOp[0] = LD->getMemOperand();
486 cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
487 const SDValue Froms[] = { SDValue(LD, 0),
488 SDValue(LD, 1),
489 SDValue(LD, 2)
490 };
491 const SDValue Tos[] = { SDValue(Result_2, 0),
492 SDValue(Result_3, 0),
493 SDValue(Result_1, 1)
494 };
495 ReplaceUses(Froms, Tos, 3);
496 return Result_2;
497 }
498 return SelectCode(LD);
499}
500
501
502SDNode *HexagonDAGToDAGISel::SelectIndexedLoadZeroExtend64(LoadSDNode *LD,
503 unsigned Opcode,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000504 SDLoc dl)
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000505{
506 SDValue Chain = LD->getChain();
507 EVT LoadedVT = LD->getMemoryVT();
508 SDValue Base = LD->getBasePtr();
509 SDValue Offset = LD->getOffset();
510 SDNode *OffsetNode = Offset.getNode();
511 int32_t Val = cast<ConstantSDNode>(OffsetNode)->getSExtValue();
512 SDValue N1 = LD->getOperand(1);
513 SDValue CPTmpN1_0;
514 SDValue CPTmpN1_1;
Bill Wendling4a7a4082013-06-07 06:19:56 +0000515
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000516 if (SelectADDRriS11_2(N1, CPTmpN1_0, CPTmpN1_1) &&
517 N1.getNode()->getValueType(0) == MVT::i32) {
Eric Christopherd9134482014-08-04 21:25:23 +0000518 const HexagonInstrInfo *TII = static_cast<const HexagonInstrInfo *>(
519 TM.getSubtargetImpl()->getInstrInfo());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000520 if (TII->isValidAutoIncImm(LoadedVT, Val)) {
521 SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
522 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
523 SDNode *Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::i32,
524 MVT::i32, MVT::Other, Base,
525 TargetConstVal, Chain);
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000526 SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A2_tfrsi, dl, MVT::i32,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000527 TargetConst0);
Colin LeMahieub580d7d2014-12-09 19:23:45 +0000528 SDNode *Result_3 = CurDAG->getMachineNode(Hexagon::A2_combinew, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000529 MVT::i64, MVT::Other,
530 SDValue(Result_2,0),
531 SDValue(Result_1,0));
532 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
533 MemOp[0] = LD->getMemOperand();
534 cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
535 const SDValue Froms[] = { SDValue(LD, 0),
536 SDValue(LD, 1),
537 SDValue(LD, 2)
538 };
539 const SDValue Tos[] = { SDValue(Result_3, 0),
540 SDValue(Result_1, 1),
541 SDValue(Result_1, 2)
542 };
543 ReplaceUses(Froms, Tos, 3);
544 return Result_3;
545 }
546
547 // Generate an indirect load.
548 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
549 SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
550 SDNode *Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::i32,
551 MVT::Other,
552 Base, TargetConst0, Chain);
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000553 SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A2_tfrsi, dl, MVT::i32,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000554 TargetConst0);
Colin LeMahieub580d7d2014-12-09 19:23:45 +0000555 SDNode *Result_3 = CurDAG->getMachineNode(Hexagon::A2_combinew, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000556 MVT::i64, MVT::Other,
557 SDValue(Result_2,0),
558 SDValue(Result_1,0));
559 // Add offset to base.
560 SDNode* Result_4 = CurDAG->getMachineNode(Hexagon::ADD_ri, dl, MVT::i32,
561 Base, TargetConstVal,
562 SDValue(Result_1, 1));
563 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
564 MemOp[0] = LD->getMemOperand();
565 cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
566 const SDValue Froms[] = { SDValue(LD, 0),
567 SDValue(LD, 1),
568 SDValue(LD, 2)
569 };
570 const SDValue Tos[] = { SDValue(Result_3, 0), // Load value.
571 SDValue(Result_4, 0), // New address.
572 SDValue(Result_1, 1)
573 };
574 ReplaceUses(Froms, Tos, 3);
575 return Result_3;
576 }
577
578 return SelectCode(LD);
579}
580
581
Andrew Trickef9de2a2013-05-25 02:42:55 +0000582SDNode *HexagonDAGToDAGISel::SelectIndexedLoad(LoadSDNode *LD, SDLoc dl) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000583 SDValue Chain = LD->getChain();
584 SDValue Base = LD->getBasePtr();
585 SDValue Offset = LD->getOffset();
586 SDNode *OffsetNode = Offset.getNode();
587 // Get the constant value.
588 int32_t Val = cast<ConstantSDNode>(OffsetNode)->getSExtValue();
589 EVT LoadedVT = LD->getMemoryVT();
590 unsigned Opcode = 0;
591
592 // Check for zero ext loads.
593 bool zextval = (LD->getExtensionType() == ISD::ZEXTLOAD);
594
595 // Figure out the opcode.
Eric Christopherd9134482014-08-04 21:25:23 +0000596 const HexagonInstrInfo *TII = static_cast<const HexagonInstrInfo *>(
597 TM.getSubtargetImpl()->getInstrInfo());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000598 if (LoadedVT == MVT::i64) {
599 if (TII->isValidAutoIncImm(LoadedVT, Val))
Colin LeMahieuc83cbbf2014-12-26 19:31:46 +0000600 Opcode = Hexagon::L2_loadrd_pi;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000601 else
Colin LeMahieu947cd702014-12-23 20:44:59 +0000602 Opcode = Hexagon::L2_loadrd_io;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000603 } else if (LoadedVT == MVT::i32) {
604 if (TII->isValidAutoIncImm(LoadedVT, Val))
Colin LeMahieuc83cbbf2014-12-26 19:31:46 +0000605 Opcode = Hexagon::L2_loadri_pi;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000606 else
Colin LeMahieu026e88d2014-12-23 20:02:16 +0000607 Opcode = Hexagon::L2_loadri_io;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000608 } else if (LoadedVT == MVT::i16) {
609 if (TII->isValidAutoIncImm(LoadedVT, Val))
Colin LeMahieuc83cbbf2014-12-26 19:31:46 +0000610 Opcode = zextval ? Hexagon::L2_loadruh_pi : Hexagon::L2_loadrh_pi;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000611 else
Colin LeMahieu8e39cad2014-12-23 17:25:57 +0000612 Opcode = zextval ? Hexagon::L2_loadruh_io : Hexagon::L2_loadrh_io;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000613 } else if (LoadedVT == MVT::i8) {
614 if (TII->isValidAutoIncImm(LoadedVT, Val))
Colin LeMahieufe9612e2014-12-26 19:12:11 +0000615 Opcode = zextval ? Hexagon::L2_loadrub_pi : Hexagon::L2_loadrb_pi;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000616 else
Colin LeMahieu4b1eac42014-12-22 21:40:43 +0000617 Opcode = zextval ? Hexagon::L2_loadrub_io : Hexagon::L2_loadrb_io;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000618 } else
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000619 llvm_unreachable("unknown memory type");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000620
621 // For zero ext i64 loads, we need to add combine instructions.
622 if (LD->getValueType(0) == MVT::i64 &&
623 LD->getExtensionType() == ISD::ZEXTLOAD) {
624 return SelectIndexedLoadZeroExtend64(LD, Opcode, dl);
625 }
626 if (LD->getValueType(0) == MVT::i64 &&
627 LD->getExtensionType() == ISD::SEXTLOAD) {
628 // Handle sign ext i64 loads.
629 return SelectIndexedLoadSignExtend64(LD, Opcode, dl);
630 }
631 if (TII->isValidAutoIncImm(LoadedVT, Val)) {
632 SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
633 SDNode* Result = CurDAG->getMachineNode(Opcode, dl,
634 LD->getValueType(0),
635 MVT::i32, MVT::Other, Base,
636 TargetConstVal, Chain);
637 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
638 MemOp[0] = LD->getMemOperand();
639 cast<MachineSDNode>(Result)->setMemRefs(MemOp, MemOp + 1);
640 const SDValue Froms[] = { SDValue(LD, 0),
641 SDValue(LD, 1),
642 SDValue(LD, 2)
643 };
644 const SDValue Tos[] = { SDValue(Result, 0),
645 SDValue(Result, 1),
646 SDValue(Result, 2)
647 };
648 ReplaceUses(Froms, Tos, 3);
649 return Result;
650 } else {
651 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
652 SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
653 SDNode* Result_1 = CurDAG->getMachineNode(Opcode, dl,
654 LD->getValueType(0),
655 MVT::Other, Base, TargetConst0,
656 Chain);
657 SDNode* Result_2 = CurDAG->getMachineNode(Hexagon::ADD_ri, dl, MVT::i32,
658 Base, TargetConstVal,
659 SDValue(Result_1, 1));
660 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
661 MemOp[0] = LD->getMemOperand();
662 cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
663 const SDValue Froms[] = { SDValue(LD, 0),
664 SDValue(LD, 1),
665 SDValue(LD, 2)
666 };
667 const SDValue Tos[] = { SDValue(Result_1, 0),
668 SDValue(Result_2, 0),
669 SDValue(Result_1, 1)
670 };
671 ReplaceUses(Froms, Tos, 3);
672 return Result_1;
673 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000674}
675
676
677SDNode *HexagonDAGToDAGISel::SelectLoad(SDNode *N) {
678 SDNode *result;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000679 SDLoc dl(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000680 LoadSDNode *LD = cast<LoadSDNode>(N);
681 ISD::MemIndexedMode AM = LD->getAddressingMode();
682
683 // Handle indexed loads.
684 if (AM != ISD::UNINDEXED) {
685 result = SelectIndexedLoad(LD, dl);
686 } else {
687 result = SelectBaseOffsetLoad(LD, dl);
688 }
689
690 return result;
691}
692
693
Andrew Trickef9de2a2013-05-25 02:42:55 +0000694SDNode *HexagonDAGToDAGISel::SelectIndexedStore(StoreSDNode *ST, SDLoc dl) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000695 SDValue Chain = ST->getChain();
696 SDValue Base = ST->getBasePtr();
697 SDValue Offset = ST->getOffset();
698 SDValue Value = ST->getValue();
699 SDNode *OffsetNode = Offset.getNode();
700 // Get the constant value.
701 int32_t Val = cast<ConstantSDNode>(OffsetNode)->getSExtValue();
702 EVT StoredVT = ST->getMemoryVT();
703
704 // Offset value must be within representable range
705 // and must have correct alignment properties.
Eric Christopherd9134482014-08-04 21:25:23 +0000706 const HexagonInstrInfo *TII = static_cast<const HexagonInstrInfo *>(
707 TM.getSubtargetImpl()->getInstrInfo());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000708 if (TII->isValidAutoIncImm(StoredVT, Val)) {
Jyotsna Vermab16a9cb2013-01-29 18:42:41 +0000709 SDValue Ops[] = {Base, CurDAG->getTargetConstant(Val, MVT::i32), Value,
710 Chain};
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000711 unsigned Opcode = 0;
712
713 // Figure out the post inc version of opcode.
Colin LeMahieu9a3cd3f2014-12-29 20:00:43 +0000714 if (StoredVT == MVT::i64) Opcode = Hexagon::S2_storerd_pi;
715 else if (StoredVT == MVT::i32) Opcode = Hexagon::S2_storeri_pi;
716 else if (StoredVT == MVT::i16) Opcode = Hexagon::S2_storerh_pi;
Colin LeMahieu3d34afb2014-12-29 19:42:14 +0000717 else if (StoredVT == MVT::i8) Opcode = Hexagon::S2_storerb_pi;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000718 else llvm_unreachable("unknown memory type");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000719
720 // Build post increment store.
721 SDNode* Result = CurDAG->getMachineNode(Opcode, dl, MVT::i32,
Michael Liaob53d8962013-04-19 22:22:57 +0000722 MVT::Other, Ops);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000723 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
724 MemOp[0] = ST->getMemOperand();
725 cast<MachineSDNode>(Result)->setMemRefs(MemOp, MemOp + 1);
726
727 ReplaceUses(ST, Result);
728 ReplaceUses(SDValue(ST,1), SDValue(Result,1));
729 return Result;
730 }
731
732 // Note: Order of operands matches the def of instruction:
733 // def STrid : STInst<(outs), (ins MEMri:$addr, DoubleRegs:$src1), ...
734 // and it differs for POST_ST* for instance.
735 SDValue Ops[] = { Base, CurDAG->getTargetConstant(0, MVT::i32), Value,
736 Chain};
737 unsigned Opcode = 0;
738
739 // Figure out the opcode.
Colin LeMahieubda31b42014-12-29 20:44:51 +0000740 if (StoredVT == MVT::i64) Opcode = Hexagon::S2_storerd_io;
741 else if (StoredVT == MVT::i32) Opcode = Hexagon::S2_storeri_io;
742 else if (StoredVT == MVT::i16) Opcode = Hexagon::S2_storerh_io;
743 else if (StoredVT == MVT::i8) Opcode = Hexagon::S2_storerb_io;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000744 else llvm_unreachable("unknown memory type");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000745
746 // Build regular store.
747 SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
Michael Liaob53d8962013-04-19 22:22:57 +0000748 SDNode* Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::Other, Ops);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000749 // Build splitted incriment instruction.
750 SDNode* Result_2 = CurDAG->getMachineNode(Hexagon::ADD_ri, dl, MVT::i32,
751 Base,
752 TargetConstVal,
753 SDValue(Result_1, 0));
754 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
755 MemOp[0] = ST->getMemOperand();
756 cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
757
758 ReplaceUses(SDValue(ST,0), SDValue(Result_2,0));
759 ReplaceUses(SDValue(ST,1), SDValue(Result_1,0));
760 return Result_2;
761}
762
763
764SDNode *HexagonDAGToDAGISel::SelectBaseOffsetStore(StoreSDNode *ST,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000765 SDLoc dl) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000766 SDValue Chain = ST->getChain();
767 SDNode* Const32 = ST->getBasePtr().getNode();
768 SDValue Value = ST->getValue();
769 unsigned Opcode = 0;
770
771 // Try to lower stores of GlobalAdresses into indexed stores. Custom
772 // lowering for GlobalAddress nodes has already turned it into a
773 // CONST32. Avoid truncating stores for the moment. Post-inc stores
774 // do the same. Don't think there's a reason for it, so will file a
775 // bug to fix.
776 if ((Const32->getOpcode() == HexagonISD::CONST32) &&
777 !(Value.getValueType() == MVT::i64 && ST->isTruncatingStore())) {
778 SDValue Base = Const32->getOperand(0);
779 if (Base.getOpcode() == ISD::TargetGlobalAddress) {
780 EVT StoredVT = ST->getMemoryVT();
781 int64_t Offset = cast<GlobalAddressSDNode>(Base)->getOffset();
782 if (Offset != 0 && OffsetFitsS11(StoredVT, Offset)) {
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000783 MVT PointerTy = getTargetLowering()->getPointerTy();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000784 const GlobalValue* GV =
785 cast<GlobalAddressSDNode>(Base)->getGlobal();
786 SDValue TargAddr =
787 CurDAG->getTargetGlobalAddress(GV, dl, PointerTy, 0);
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000788 SDNode* NewBase = CurDAG->getMachineNode(Hexagon::CONST32_set,
789 dl, PointerTy,
790 TargAddr);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000791
792 // Figure out base + offset opcode
Colin LeMahieubda31b42014-12-29 20:44:51 +0000793 if (StoredVT == MVT::i64) Opcode = Hexagon::S2_storerd_io;
794 else if (StoredVT == MVT::i32) Opcode = Hexagon::S2_storeri_io;
795 else if (StoredVT == MVT::i16) Opcode = Hexagon::S2_storerh_io;
796 else if (StoredVT == MVT::i8) Opcode = Hexagon::S2_storerb_io;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000797 else llvm_unreachable("unknown memory type");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000798
799 SDValue Ops[] = {SDValue(NewBase,0),
800 CurDAG->getTargetConstant(Offset,PointerTy),
801 Value, Chain};
802 // build indexed store
803 SDNode* Result = CurDAG->getMachineNode(Opcode, dl,
Michael Liaob53d8962013-04-19 22:22:57 +0000804 MVT::Other, Ops);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000805 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
806 MemOp[0] = ST->getMemOperand();
807 cast<MachineSDNode>(Result)->setMemRefs(MemOp, MemOp + 1);
808 ReplaceUses(ST, Result);
809 return Result;
810 }
811 }
812 }
813
814 return SelectCode(ST);
815}
816
817
818SDNode *HexagonDAGToDAGISel::SelectStore(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000819 SDLoc dl(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000820 StoreSDNode *ST = cast<StoreSDNode>(N);
821 ISD::MemIndexedMode AM = ST->getAddressingMode();
822
823 // Handle indexed stores.
824 if (AM != ISD::UNINDEXED) {
825 return SelectIndexedStore(ST, dl);
826 }
Sirish Pandec92c3162012-05-03 16:18:50 +0000827
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000828 return SelectBaseOffsetStore(ST, dl);
829}
830
831SDNode *HexagonDAGToDAGISel::SelectMul(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000832 SDLoc dl(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000833
834 //
835 // %conv.i = sext i32 %tmp1 to i64
836 // %conv2.i = sext i32 %add to i64
837 // %mul.i = mul nsw i64 %conv2.i, %conv.i
838 //
839 // --- match with the following ---
840 //
841 // %mul.i = mpy (%tmp1, %add)
842 //
843
844 if (N->getValueType(0) == MVT::i64) {
845 // Shifting a i64 signed multiply.
846 SDValue MulOp0 = N->getOperand(0);
847 SDValue MulOp1 = N->getOperand(1);
848
849 SDValue OP0;
850 SDValue OP1;
851
852 // Handle sign_extend and sextload.
853 if (MulOp0.getOpcode() == ISD::SIGN_EXTEND) {
854 SDValue Sext0 = MulOp0.getOperand(0);
855 if (Sext0.getNode()->getValueType(0) != MVT::i32) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000856 return SelectCode(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000857 }
858
859 OP0 = Sext0;
860 } else if (MulOp0.getOpcode() == ISD::LOAD) {
861 LoadSDNode *LD = cast<LoadSDNode>(MulOp0.getNode());
862 if (LD->getMemoryVT() != MVT::i32 ||
863 LD->getExtensionType() != ISD::SEXTLOAD ||
864 LD->getAddressingMode() != ISD::UNINDEXED) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000865 return SelectCode(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000866 }
867
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000868 SDValue Chain = LD->getChain();
869 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
Colin LeMahieu026e88d2014-12-23 20:02:16 +0000870 OP0 = SDValue(CurDAG->getMachineNode(Hexagon::L2_loadri_io, dl, MVT::i32,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000871 MVT::Other,
872 LD->getBasePtr(), TargetConst0,
873 Chain), 0);
874 } else {
875 return SelectCode(N);
876 }
877
878 // Same goes for the second operand.
879 if (MulOp1.getOpcode() == ISD::SIGN_EXTEND) {
880 SDValue Sext1 = MulOp1.getOperand(0);
881 if (Sext1.getNode()->getValueType(0) != MVT::i32) {
882 return SelectCode(N);
883 }
884
885 OP1 = Sext1;
886 } else if (MulOp1.getOpcode() == ISD::LOAD) {
887 LoadSDNode *LD = cast<LoadSDNode>(MulOp1.getNode());
888 if (LD->getMemoryVT() != MVT::i32 ||
889 LD->getExtensionType() != ISD::SEXTLOAD ||
890 LD->getAddressingMode() != ISD::UNINDEXED) {
891 return SelectCode(N);
892 }
893
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000894 SDValue Chain = LD->getChain();
895 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
Colin LeMahieu026e88d2014-12-23 20:02:16 +0000896 OP1 = SDValue(CurDAG->getMachineNode(Hexagon::L2_loadri_io, dl, MVT::i32,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000897 MVT::Other,
898 LD->getBasePtr(), TargetConst0,
899 Chain), 0);
900 } else {
901 return SelectCode(N);
902 }
903
904 // Generate a mpy instruction.
Colin LeMahieud9b23502014-12-16 16:10:01 +0000905 SDNode *Result = CurDAG->getMachineNode(Hexagon::M2_dpmpyss_s0, dl, MVT::i64,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000906 OP0, OP1);
907 ReplaceUses(N, Result);
908 return Result;
909 }
910
911 return SelectCode(N);
912}
913
914
915SDNode *HexagonDAGToDAGISel::SelectSelect(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000916 SDLoc dl(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000917 SDValue N0 = N->getOperand(0);
918 if (N0.getOpcode() == ISD::SETCC) {
919 SDValue N00 = N0.getOperand(0);
920 if (N00.getOpcode() == ISD::SIGN_EXTEND_INREG) {
921 SDValue N000 = N00.getOperand(0);
922 SDValue N001 = N00.getOperand(1);
923 if (cast<VTSDNode>(N001)->getVT() == MVT::i16) {
924 SDValue N01 = N0.getOperand(1);
925 SDValue N02 = N0.getOperand(2);
926
927 // Pattern: (select:i32 (setcc:i1 (sext_inreg:i32 IntRegs:i32:$src2,
928 // i16:Other),IntRegs:i32:$src1, SETLT:Other),IntRegs:i32:$src1,
929 // IntRegs:i32:$src2)
930 // Emits: (MAXh_rr:i32 IntRegs:i32:$src1, IntRegs:i32:$src2)
931 // Pattern complexity = 9 cost = 1 size = 0.
932 if (cast<CondCodeSDNode>(N02)->get() == ISD::SETLT) {
933 SDValue N1 = N->getOperand(1);
934 if (N01 == N1) {
935 SDValue N2 = N->getOperand(2);
936 if (N000 == N2 &&
937 N0.getNode()->getValueType(N0.getResNo()) == MVT::i1 &&
938 N00.getNode()->getValueType(N00.getResNo()) == MVT::i32) {
Colin LeMahieu310991c2014-11-21 21:54:59 +0000939 SDNode *SextNode = CurDAG->getMachineNode(Hexagon::A2_sxth, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000940 MVT::i32, N000);
Colin LeMahieu5cf56322014-12-08 23:55:43 +0000941 SDNode *Result = CurDAG->getMachineNode(Hexagon::A2_max, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000942 MVT::i32,
943 SDValue(SextNode, 0),
944 N1);
945 ReplaceUses(N, Result);
946 return Result;
947 }
948 }
949 }
950
951 // Pattern: (select:i32 (setcc:i1 (sext_inreg:i32 IntRegs:i32:$src2,
952 // i16:Other), IntRegs:i32:$src1, SETGT:Other), IntRegs:i32:$src1,
953 // IntRegs:i32:$src2)
954 // Emits: (MINh_rr:i32 IntRegs:i32:$src1, IntRegs:i32:$src2)
955 // Pattern complexity = 9 cost = 1 size = 0.
956 if (cast<CondCodeSDNode>(N02)->get() == ISD::SETGT) {
957 SDValue N1 = N->getOperand(1);
958 if (N01 == N1) {
959 SDValue N2 = N->getOperand(2);
960 if (N000 == N2 &&
961 N0.getNode()->getValueType(N0.getResNo()) == MVT::i1 &&
962 N00.getNode()->getValueType(N00.getResNo()) == MVT::i32) {
Colin LeMahieu310991c2014-11-21 21:54:59 +0000963 SDNode *SextNode = CurDAG->getMachineNode(Hexagon::A2_sxth, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000964 MVT::i32, N000);
Colin LeMahieu5cf56322014-12-08 23:55:43 +0000965 SDNode *Result = CurDAG->getMachineNode(Hexagon::A2_min, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000966 MVT::i32,
967 SDValue(SextNode, 0),
968 N1);
969 ReplaceUses(N, Result);
970 return Result;
971 }
972 }
973 }
974 }
975 }
976 }
977
978 return SelectCode(N);
979}
980
981
982SDNode *HexagonDAGToDAGISel::SelectTruncate(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000983 SDLoc dl(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000984 SDValue Shift = N->getOperand(0);
985
986 //
987 // %conv.i = sext i32 %tmp1 to i64
988 // %conv2.i = sext i32 %add to i64
989 // %mul.i = mul nsw i64 %conv2.i, %conv.i
990 // %shr5.i = lshr i64 %mul.i, 32
991 // %conv3.i = trunc i64 %shr5.i to i32
992 //
993 // --- match with the following ---
994 //
995 // %conv3.i = mpy (%tmp1, %add)
996 //
997 // Trunc to i32.
998 if (N->getValueType(0) == MVT::i32) {
999 // Trunc from i64.
1000 if (Shift.getNode()->getValueType(0) == MVT::i64) {
1001 // Trunc child is logical shift right.
1002 if (Shift.getOpcode() != ISD::SRL) {
1003 return SelectCode(N);
1004 }
1005
1006 SDValue ShiftOp0 = Shift.getOperand(0);
1007 SDValue ShiftOp1 = Shift.getOperand(1);
1008
1009 // Shift by const 32
1010 if (ShiftOp1.getOpcode() != ISD::Constant) {
1011 return SelectCode(N);
1012 }
1013
1014 int32_t ShiftConst =
1015 cast<ConstantSDNode>(ShiftOp1.getNode())->getSExtValue();
1016 if (ShiftConst != 32) {
1017 return SelectCode(N);
1018 }
1019
1020 // Shifting a i64 signed multiply
1021 SDValue Mul = ShiftOp0;
1022 if (Mul.getOpcode() != ISD::MUL) {
1023 return SelectCode(N);
1024 }
1025
1026 SDValue MulOp0 = Mul.getOperand(0);
1027 SDValue MulOp1 = Mul.getOperand(1);
1028
1029 SDValue OP0;
1030 SDValue OP1;
1031
1032 // Handle sign_extend and sextload
1033 if (MulOp0.getOpcode() == ISD::SIGN_EXTEND) {
1034 SDValue Sext0 = MulOp0.getOperand(0);
1035 if (Sext0.getNode()->getValueType(0) != MVT::i32) {
1036 return SelectCode(N);
1037 }
1038
1039 OP0 = Sext0;
1040 } else if (MulOp0.getOpcode() == ISD::LOAD) {
1041 LoadSDNode *LD = cast<LoadSDNode>(MulOp0.getNode());
1042 if (LD->getMemoryVT() != MVT::i32 ||
1043 LD->getExtensionType() != ISD::SEXTLOAD ||
1044 LD->getAddressingMode() != ISD::UNINDEXED) {
1045 return SelectCode(N);
1046 }
1047
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001048 SDValue Chain = LD->getChain();
1049 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
Colin LeMahieu026e88d2014-12-23 20:02:16 +00001050 OP0 = SDValue(CurDAG->getMachineNode(Hexagon::L2_loadri_io, dl, MVT::i32,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001051 MVT::Other,
1052 LD->getBasePtr(),
1053 TargetConst0, Chain), 0);
1054 } else {
1055 return SelectCode(N);
1056 }
1057
1058 // Same goes for the second operand.
1059 if (MulOp1.getOpcode() == ISD::SIGN_EXTEND) {
1060 SDValue Sext1 = MulOp1.getOperand(0);
1061 if (Sext1.getNode()->getValueType(0) != MVT::i32)
1062 return SelectCode(N);
1063
1064 OP1 = Sext1;
1065 } else if (MulOp1.getOpcode() == ISD::LOAD) {
1066 LoadSDNode *LD = cast<LoadSDNode>(MulOp1.getNode());
1067 if (LD->getMemoryVT() != MVT::i32 ||
1068 LD->getExtensionType() != ISD::SEXTLOAD ||
1069 LD->getAddressingMode() != ISD::UNINDEXED) {
1070 return SelectCode(N);
1071 }
1072
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001073 SDValue Chain = LD->getChain();
1074 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
Colin LeMahieu026e88d2014-12-23 20:02:16 +00001075 OP1 = SDValue(CurDAG->getMachineNode(Hexagon::L2_loadri_io, dl, MVT::i32,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001076 MVT::Other,
1077 LD->getBasePtr(),
1078 TargetConst0, Chain), 0);
1079 } else {
1080 return SelectCode(N);
1081 }
1082
1083 // Generate a mpy instruction.
Colin LeMahieud9b23502014-12-16 16:10:01 +00001084 SDNode *Result = CurDAG->getMachineNode(Hexagon::M2_mpy_up, dl, MVT::i32,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001085 OP0, OP1);
1086 ReplaceUses(N, Result);
1087 return Result;
1088 }
1089 }
1090
1091 return SelectCode(N);
1092}
1093
1094
1095SDNode *HexagonDAGToDAGISel::SelectSHL(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001096 SDLoc dl(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001097 if (N->getValueType(0) == MVT::i32) {
1098 SDValue Shl_0 = N->getOperand(0);
1099 SDValue Shl_1 = N->getOperand(1);
1100 // RHS is const.
1101 if (Shl_1.getOpcode() == ISD::Constant) {
1102 if (Shl_0.getOpcode() == ISD::MUL) {
1103 SDValue Mul_0 = Shl_0.getOperand(0); // Val
1104 SDValue Mul_1 = Shl_0.getOperand(1); // Const
1105 // RHS of mul is const.
1106 if (Mul_1.getOpcode() == ISD::Constant) {
1107 int32_t ShlConst =
1108 cast<ConstantSDNode>(Shl_1.getNode())->getSExtValue();
1109 int32_t MulConst =
1110 cast<ConstantSDNode>(Mul_1.getNode())->getSExtValue();
1111 int32_t ValConst = MulConst << ShlConst;
1112 SDValue Val = CurDAG->getTargetConstant(ValConst,
1113 MVT::i32);
1114 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Val.getNode()))
1115 if (isInt<9>(CN->getSExtValue())) {
1116 SDNode* Result =
Colin LeMahieud9b23502014-12-16 16:10:01 +00001117 CurDAG->getMachineNode(Hexagon::M2_mpysmi, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001118 MVT::i32, Mul_0, Val);
1119 ReplaceUses(N, Result);
1120 return Result;
1121 }
1122
1123 }
1124 } else if (Shl_0.getOpcode() == ISD::SUB) {
1125 SDValue Sub_0 = Shl_0.getOperand(0); // Const 0
1126 SDValue Sub_1 = Shl_0.getOperand(1); // Val
1127 if (Sub_0.getOpcode() == ISD::Constant) {
1128 int32_t SubConst =
1129 cast<ConstantSDNode>(Sub_0.getNode())->getSExtValue();
1130 if (SubConst == 0) {
1131 if (Sub_1.getOpcode() == ISD::SHL) {
1132 SDValue Shl2_0 = Sub_1.getOperand(0); // Val
1133 SDValue Shl2_1 = Sub_1.getOperand(1); // Const
1134 if (Shl2_1.getOpcode() == ISD::Constant) {
1135 int32_t ShlConst =
1136 cast<ConstantSDNode>(Shl_1.getNode())->getSExtValue();
1137 int32_t Shl2Const =
1138 cast<ConstantSDNode>(Shl2_1.getNode())->getSExtValue();
1139 int32_t ValConst = 1 << (ShlConst+Shl2Const);
1140 SDValue Val = CurDAG->getTargetConstant(-ValConst, MVT::i32);
1141 if (ConstantSDNode *CN =
1142 dyn_cast<ConstantSDNode>(Val.getNode()))
1143 if (isInt<9>(CN->getSExtValue())) {
1144 SDNode* Result =
Colin LeMahieud9b23502014-12-16 16:10:01 +00001145 CurDAG->getMachineNode(Hexagon::M2_mpysmi, dl, MVT::i32,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001146 Shl2_0, Val);
1147 ReplaceUses(N, Result);
1148 return Result;
1149 }
1150 }
1151 }
1152 }
1153 }
1154 }
1155 }
1156 }
1157 return SelectCode(N);
1158}
1159
1160
1161//
1162// If there is an zero_extend followed an intrinsic in DAG (this means - the
1163// result of the intrinsic is predicate); convert the zero_extend to
1164// transfer instruction.
1165//
1166// Zero extend -> transfer is lowered here. Otherwise, zero_extend will be
1167// converted into a MUX as predicate registers defined as 1 bit in the
1168// compiler. Architecture defines them as 8-bit registers.
1169// We want to preserve all the lower 8-bits and, not just 1 LSB bit.
1170//
1171SDNode *HexagonDAGToDAGISel::SelectZeroExtend(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001172 SDLoc dl(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001173 SDNode *IsIntrinsic = N->getOperand(0).getNode();
1174 if ((IsIntrinsic->getOpcode() == ISD::INTRINSIC_WO_CHAIN)) {
1175 unsigned ID =
1176 cast<ConstantSDNode>(IsIntrinsic->getOperand(0))->getZExtValue();
1177 if (doesIntrinsicReturnPredicate(ID)) {
1178 // Now we need to differentiate target data types.
1179 if (N->getValueType(0) == MVT::i64) {
1180 // Convert the zero_extend to Rs = Pd followed by COMBINE_rr(0,Rs).
1181 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
Colin LeMahieu30dcb232014-12-09 18:16:49 +00001182 SDNode *Result_1 = CurDAG->getMachineNode(Hexagon::C2_tfrpr, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001183 MVT::i32,
1184 SDValue(IsIntrinsic, 0));
Colin LeMahieu4af437f2014-12-09 20:23:30 +00001185 SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A2_tfrsi, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001186 MVT::i32,
1187 TargetConst0);
Colin LeMahieub580d7d2014-12-09 19:23:45 +00001188 SDNode *Result_3 = CurDAG->getMachineNode(Hexagon::A2_combinew, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001189 MVT::i64, MVT::Other,
1190 SDValue(Result_2, 0),
1191 SDValue(Result_1, 0));
1192 ReplaceUses(N, Result_3);
1193 return Result_3;
1194 }
1195 if (N->getValueType(0) == MVT::i32) {
1196 // Convert the zero_extend to Rs = Pd
Colin LeMahieu30dcb232014-12-09 18:16:49 +00001197 SDNode* RsPd = CurDAG->getMachineNode(Hexagon::C2_tfrpr, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001198 MVT::i32,
1199 SDValue(IsIntrinsic, 0));
1200 ReplaceUses(N, RsPd);
1201 return RsPd;
1202 }
Craig Toppere55c5562012-02-07 02:50:20 +00001203 llvm_unreachable("Unexpected value type");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001204 }
1205 }
1206 return SelectCode(N);
1207}
1208
1209
1210//
1211// Checking for intrinsics which have predicate registers as operand(s)
1212// and lowering to the actual intrinsic.
1213//
1214SDNode *HexagonDAGToDAGISel::SelectIntrinsicWOChain(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001215 SDLoc dl(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001216 unsigned ID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
1217 unsigned IntrinsicWithPred = doesIntrinsicContainPredicate(ID);
1218
1219 // We are concerned with only those intrinsics that have predicate registers
1220 // as at least one of the operands.
1221 if (IntrinsicWithPred) {
1222 SmallVector<SDValue, 8> Ops;
Eric Christopherd9134482014-08-04 21:25:23 +00001223 const HexagonInstrInfo *TII = static_cast<const HexagonInstrInfo *>(
1224 TM.getSubtargetImpl()->getInstrInfo());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001225 const MCInstrDesc &MCID = TII->get(IntrinsicWithPred);
Eric Christopherd9134482014-08-04 21:25:23 +00001226 const TargetRegisterInfo *TRI = TM.getSubtargetImpl()->getRegisterInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001227
1228 // Iterate over all the operands of the intrinsics.
1229 // For PredRegs, do the transfer.
1230 // For Double/Int Regs, just preserve the value
1231 // For immediates, lower it.
1232 for (unsigned i = 1; i < N->getNumOperands(); ++i) {
1233 SDNode *Arg = N->getOperand(i).getNode();
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +00001234 const TargetRegisterClass *RC = TII->getRegClass(MCID, i, TRI, *MF);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001235
Craig Topperc7242e02012-04-20 07:30:17 +00001236 if (RC == &Hexagon::IntRegsRegClass ||
1237 RC == &Hexagon::DoubleRegsRegClass) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001238 Ops.push_back(SDValue(Arg, 0));
Craig Topperc7242e02012-04-20 07:30:17 +00001239 } else if (RC == &Hexagon::PredRegsRegClass) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001240 // Do the transfer.
Colin LeMahieu30dcb232014-12-09 18:16:49 +00001241 SDNode *PdRs = CurDAG->getMachineNode(Hexagon::C2_tfrrp, dl, MVT::i1,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001242 SDValue(Arg, 0));
1243 Ops.push_back(SDValue(PdRs,0));
Craig Topper062a2ba2014-04-25 05:30:21 +00001244 } else if (!RC && (dyn_cast<ConstantSDNode>(Arg) != nullptr)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001245 // This is immediate operand. Lower it here making sure that we DO have
1246 // const SDNode for immediate value.
1247 int32_t Val = cast<ConstantSDNode>(Arg)->getSExtValue();
1248 SDValue SDVal = CurDAG->getTargetConstant(Val, MVT::i32);
1249 Ops.push_back(SDVal);
1250 } else {
Craig Toppere55c5562012-02-07 02:50:20 +00001251 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001252 }
1253 }
1254 EVT ReturnValueVT = N->getValueType(0);
1255 SDNode *Result = CurDAG->getMachineNode(IntrinsicWithPred, dl,
Michael Liaob53d8962013-04-19 22:22:57 +00001256 ReturnValueVT, Ops);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001257 ReplaceUses(N, Result);
1258 return Result;
1259 }
1260 return SelectCode(N);
1261}
1262
Sirish Pande69295b82012-05-10 20:20:25 +00001263//
1264// Map floating point constant values.
1265//
1266SDNode *HexagonDAGToDAGISel::SelectConstantFP(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001267 SDLoc dl(N);
Sirish Pande69295b82012-05-10 20:20:25 +00001268 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N);
1269 APFloat APF = CN->getValueAPF();
1270 if (N->getValueType(0) == MVT::f32) {
1271 return CurDAG->getMachineNode(Hexagon::TFRI_f, dl, MVT::f32,
1272 CurDAG->getTargetConstantFP(APF.convertToFloat(), MVT::f32));
1273 }
1274 else if (N->getValueType(0) == MVT::f64) {
1275 return CurDAG->getMachineNode(Hexagon::CONST64_Float_Real, dl, MVT::f64,
1276 CurDAG->getTargetConstantFP(APF.convertToDouble(), MVT::f64));
1277 }
1278
1279 return SelectCode(N);
1280}
1281
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001282
1283//
1284// Map predicate true (encoded as -1 in LLVM) to a XOR.
1285//
1286SDNode *HexagonDAGToDAGISel::SelectConstant(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001287 SDLoc dl(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001288 if (N->getValueType(0) == MVT::i1) {
1289 SDNode* Result;
1290 int32_t Val = cast<ConstantSDNode>(N)->getSExtValue();
1291 if (Val == -1) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001292 // Create the IntReg = 1 node.
1293 SDNode* IntRegTFR =
Colin LeMahieu4af437f2014-12-09 20:23:30 +00001294 CurDAG->getMachineNode(Hexagon::A2_tfrsi, dl, MVT::i32,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001295 CurDAG->getTargetConstant(0, MVT::i32));
1296
1297 // Pd = IntReg
Colin LeMahieu30dcb232014-12-09 18:16:49 +00001298 SDNode* Pd = CurDAG->getMachineNode(Hexagon::C2_tfrrp, dl, MVT::i1,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001299 SDValue(IntRegTFR, 0));
1300
1301 // not(Pd)
Colin LeMahieu5cf56322014-12-08 23:55:43 +00001302 SDNode* NotPd = CurDAG->getMachineNode(Hexagon::C2_not, dl, MVT::i1,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001303 SDValue(Pd, 0));
1304
1305 // xor(not(Pd))
Colin LeMahieu5cf56322014-12-08 23:55:43 +00001306 Result = CurDAG->getMachineNode(Hexagon::C2_xor, dl, MVT::i1,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001307 SDValue(Pd, 0), SDValue(NotPd, 0));
1308
1309 // We have just built:
1310 // Rs = Pd
1311 // Pd = xor(not(Pd), Pd)
1312
1313 ReplaceUses(N, Result);
1314 return Result;
1315 }
1316 }
1317
1318 return SelectCode(N);
1319}
1320
1321
1322//
1323// Map add followed by a asr -> asr +=.
1324//
1325SDNode *HexagonDAGToDAGISel::SelectAdd(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001326 SDLoc dl(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001327 if (N->getValueType(0) != MVT::i32) {
1328 return SelectCode(N);
1329 }
1330 // Identify nodes of the form: add(asr(...)).
1331 SDNode* Src1 = N->getOperand(0).getNode();
1332 if (Src1->getOpcode() != ISD::SRA || !Src1->hasOneUse()
1333 || Src1->getValueType(0) != MVT::i32) {
1334 return SelectCode(N);
1335 }
1336
1337 // Build Rd = Rd' + asr(Rs, Rt). The machine constraints will ensure that
1338 // Rd and Rd' are assigned to the same register
Colin LeMahieu0f850bd2014-12-19 20:29:29 +00001339 SDNode* Result = CurDAG->getMachineNode(Hexagon::S2_asr_r_r_acc, dl, MVT::i32,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001340 N->getOperand(1),
1341 Src1->getOperand(0),
1342 Src1->getOperand(1));
1343 ReplaceUses(N, Result);
1344
1345 return Result;
1346}
1347
1348
1349SDNode *HexagonDAGToDAGISel::Select(SDNode *N) {
Tim Northover31d093c2013-09-22 08:21:56 +00001350 if (N->isMachineOpcode()) {
1351 N->setNodeId(-1);
Craig Topper062a2ba2014-04-25 05:30:21 +00001352 return nullptr; // Already selected.
Tim Northover31d093c2013-09-22 08:21:56 +00001353 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001354
1355
1356 switch (N->getOpcode()) {
1357 case ISD::Constant:
1358 return SelectConstant(N);
1359
Sirish Pande69295b82012-05-10 20:20:25 +00001360 case ISD::ConstantFP:
1361 return SelectConstantFP(N);
1362
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001363 case ISD::ADD:
1364 return SelectAdd(N);
1365
1366 case ISD::SHL:
1367 return SelectSHL(N);
1368
1369 case ISD::LOAD:
1370 return SelectLoad(N);
1371
1372 case ISD::STORE:
1373 return SelectStore(N);
1374
1375 case ISD::SELECT:
1376 return SelectSelect(N);
1377
1378 case ISD::TRUNCATE:
1379 return SelectTruncate(N);
1380
1381 case ISD::MUL:
1382 return SelectMul(N);
1383
1384 case ISD::ZERO_EXTEND:
1385 return SelectZeroExtend(N);
1386
1387 case ISD::INTRINSIC_WO_CHAIN:
1388 return SelectIntrinsicWOChain(N);
1389 }
1390
1391 return SelectCode(N);
1392}
1393
1394
1395//
1396// Hexagon_TODO: Five functions for ADDRri?! Surely there must be a better way
1397// to define these instructions.
1398//
1399bool HexagonDAGToDAGISel::SelectADDRri(SDValue& Addr, SDValue &Base,
1400 SDValue &Offset) {
1401 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
1402 Addr.getOpcode() == ISD::TargetGlobalAddress)
1403 return false; // Direct calls.
1404
1405 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
1406 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
1407 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1408 return true;
1409 }
1410 Base = Addr;
1411 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1412 return true;
1413}
1414
1415
1416bool HexagonDAGToDAGISel::SelectADDRriS11_0(SDValue& Addr, SDValue &Base,
1417 SDValue &Offset) {
1418 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
1419 Addr.getOpcode() == ISD::TargetGlobalAddress)
1420 return false; // Direct calls.
1421
1422 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
1423 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
1424 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1425 return (IsS11_0_Offset(Offset.getNode()));
1426 }
1427 Base = Addr;
1428 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1429 return (IsS11_0_Offset(Offset.getNode()));
1430}
1431
1432
1433bool HexagonDAGToDAGISel::SelectADDRriS11_1(SDValue& Addr, SDValue &Base,
1434 SDValue &Offset) {
1435 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
1436 Addr.getOpcode() == ISD::TargetGlobalAddress)
1437 return false; // Direct calls.
1438
1439 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
1440 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
1441 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1442 return (IsS11_1_Offset(Offset.getNode()));
1443 }
1444 Base = Addr;
1445 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1446 return (IsS11_1_Offset(Offset.getNode()));
1447}
1448
1449
1450bool HexagonDAGToDAGISel::SelectADDRriS11_2(SDValue& Addr, SDValue &Base,
1451 SDValue &Offset) {
1452 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
1453 Addr.getOpcode() == ISD::TargetGlobalAddress)
1454 return false; // Direct calls.
1455
1456 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
1457 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
1458 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1459 return (IsS11_2_Offset(Offset.getNode()));
1460 }
1461 Base = Addr;
1462 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1463 return (IsS11_2_Offset(Offset.getNode()));
1464}
1465
1466
1467bool HexagonDAGToDAGISel::SelectADDRriU6_0(SDValue& Addr, SDValue &Base,
1468 SDValue &Offset) {
1469 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
1470 Addr.getOpcode() == ISD::TargetGlobalAddress)
1471 return false; // Direct calls.
1472
1473 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
1474 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
1475 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1476 return (IsU6_0_Offset(Offset.getNode()));
1477 }
1478 Base = Addr;
1479 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1480 return (IsU6_0_Offset(Offset.getNode()));
1481}
1482
1483
1484bool HexagonDAGToDAGISel::SelectADDRriU6_1(SDValue& Addr, SDValue &Base,
1485 SDValue &Offset) {
1486 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
1487 Addr.getOpcode() == ISD::TargetGlobalAddress)
1488 return false; // Direct calls.
1489
1490 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
1491 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
1492 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1493 return (IsU6_1_Offset(Offset.getNode()));
1494 }
1495 Base = Addr;
1496 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1497 return (IsU6_1_Offset(Offset.getNode()));
1498}
1499
1500
1501bool HexagonDAGToDAGISel::SelectADDRriU6_2(SDValue& Addr, SDValue &Base,
1502 SDValue &Offset) {
1503 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
1504 Addr.getOpcode() == ISD::TargetGlobalAddress)
1505 return false; // Direct calls.
1506
1507 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
1508 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
1509 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1510 return (IsU6_2_Offset(Offset.getNode()));
1511 }
1512 Base = Addr;
1513 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1514 return (IsU6_2_Offset(Offset.getNode()));
1515}
1516
1517
1518bool HexagonDAGToDAGISel::SelectMEMriS11_2(SDValue& Addr, SDValue &Base,
1519 SDValue &Offset) {
1520
1521 if (Addr.getOpcode() != ISD::ADD) {
1522 return(SelectADDRriS11_2(Addr, Base, Offset));
1523 }
1524
1525 return SelectADDRriS11_2(Addr, Base, Offset);
1526}
1527
1528
1529bool HexagonDAGToDAGISel::SelectADDRriS11_3(SDValue& Addr, SDValue &Base,
1530 SDValue &Offset) {
1531 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
1532 Addr.getOpcode() == ISD::TargetGlobalAddress)
1533 return false; // Direct calls.
1534
1535 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
1536 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
1537 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1538 return (IsS11_3_Offset(Offset.getNode()));
1539 }
1540 Base = Addr;
1541 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1542 return (IsS11_3_Offset(Offset.getNode()));
1543}
1544
1545bool HexagonDAGToDAGISel::SelectADDRrr(SDValue &Addr, SDValue &R1,
1546 SDValue &R2) {
1547 if (Addr.getOpcode() == ISD::FrameIndex) return false;
1548 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
1549 Addr.getOpcode() == ISD::TargetGlobalAddress)
1550 return false; // Direct calls.
1551
1552 if (Addr.getOpcode() == ISD::ADD) {
1553 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
1554 if (isInt<13>(CN->getSExtValue()))
1555 return false; // Let the reg+imm pattern catch this!
1556 R1 = Addr.getOperand(0);
1557 R2 = Addr.getOperand(1);
1558 return true;
1559 }
1560
1561 R1 = Addr;
1562
1563 return true;
1564}
1565
1566
1567// Handle generic address case. It is accessed from inlined asm =m constraints,
1568// which could have any kind of pointer.
1569bool HexagonDAGToDAGISel::SelectAddr(SDNode *Op, SDValue Addr,
1570 SDValue &Base, SDValue &Offset) {
1571 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
1572 Addr.getOpcode() == ISD::TargetGlobalAddress)
1573 return false; // Direct calls.
1574
1575 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
1576 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
1577 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1578 return true;
1579 }
1580
1581 if (Addr.getOpcode() == ISD::ADD) {
1582 Base = Addr.getOperand(0);
1583 Offset = Addr.getOperand(1);
1584 return true;
1585 }
1586
1587 Base = Addr;
1588 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1589 return true;
1590}
1591
1592
1593bool HexagonDAGToDAGISel::
1594SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
1595 std::vector<SDValue> &OutOps) {
1596 SDValue Op0, Op1;
1597
1598 switch (ConstraintCode) {
1599 case 'o': // Offsetable.
1600 case 'v': // Not offsetable.
1601 default: return true;
1602 case 'm': // Memory.
1603 if (!SelectAddr(Op.getNode(), Op, Op0, Op1))
1604 return true;
1605 break;
1606 }
1607
1608 OutOps.push_back(Op0);
1609 OutOps.push_back(Op1);
1610 return false;
1611}
Jyotsna Verma519b3852012-11-28 20:58:14 +00001612
1613bool HexagonDAGToDAGISel::isConstExtProfitable(SDNode *N) const {
1614 unsigned UseCount = 0;
1615 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
1616 UseCount++;
1617 }
1618
1619 return (UseCount <= 1);
1620
1621}
Jyotsna Vermad9225242013-02-13 21:38:46 +00001622
1623//===--------------------------------------------------------------------===//
1624// Return 'true' if use count of the global address is below threshold.
1625//===--------------------------------------------------------------------===//
1626bool HexagonDAGToDAGISel::hasNumUsesBelowThresGA(SDNode *N) const {
1627 assert(N->getOpcode() == ISD::TargetGlobalAddress &&
1628 "Expecting a target global address");
1629
1630 // Always try to fold the address.
1631 if (TM.getOptLevel() == CodeGenOpt::Aggressive)
1632 return true;
1633
1634 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
1635 DenseMap<const GlobalValue *, unsigned>::const_iterator GI =
1636 GlobalAddressUseCountMap.find(GA->getGlobal());
1637
1638 if (GI == GlobalAddressUseCountMap.end())
1639 return false;
1640
1641 return GI->second <= MaxNumOfUsesForConstExtenders;
1642}
1643
1644//===--------------------------------------------------------------------===//
Alp Tokerf907b892013-12-05 05:44:44 +00001645// Return true if the non-GP-relative global address can be folded.
Jyotsna Vermad9225242013-02-13 21:38:46 +00001646//===--------------------------------------------------------------------===//
1647inline bool HexagonDAGToDAGISel::foldGlobalAddress(SDValue &N, SDValue &R) {
1648 return foldGlobalAddressImpl(N, R, false);
1649}
1650
1651//===--------------------------------------------------------------------===//
1652// Return true if the GP-relative global address can be folded.
1653//===--------------------------------------------------------------------===//
1654inline bool HexagonDAGToDAGISel::foldGlobalAddressGP(SDValue &N, SDValue &R) {
1655 return foldGlobalAddressImpl(N, R, true);
1656}
1657
1658//===--------------------------------------------------------------------===//
1659// Fold offset of the global address if number of uses are below threshold.
1660//===--------------------------------------------------------------------===//
1661bool HexagonDAGToDAGISel::foldGlobalAddressImpl(SDValue &N, SDValue &R,
1662 bool ShouldLookForGP) {
1663 if (N.getOpcode() == ISD::ADD) {
1664 SDValue N0 = N.getOperand(0);
1665 SDValue N1 = N.getOperand(1);
1666 if ((ShouldLookForGP && (N0.getOpcode() == HexagonISD::CONST32_GP)) ||
1667 (!ShouldLookForGP && (N0.getOpcode() == HexagonISD::CONST32))) {
1668 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N1);
1669 GlobalAddressSDNode *GA =
1670 dyn_cast<GlobalAddressSDNode>(N0.getOperand(0));
1671
1672 if (Const && GA &&
1673 (GA->getOpcode() == ISD::TargetGlobalAddress)) {
1674 if ((N0.getOpcode() == HexagonISD::CONST32) &&
1675 !hasNumUsesBelowThresGA(GA))
1676 return false;
1677 R = CurDAG->getTargetGlobalAddress(GA->getGlobal(),
Andrew Trickef9de2a2013-05-25 02:42:55 +00001678 SDLoc(Const),
Jyotsna Vermad9225242013-02-13 21:38:46 +00001679 N.getValueType(),
1680 GA->getOffset() +
1681 (uint64_t)Const->getSExtValue());
1682 return true;
1683 }
1684 }
1685 }
1686 return false;
1687}
Colin LeMahieuc7522f32015-01-14 23:07:36 +00001688
1689bool HexagonDAGToDAGISel::SelectAddrFI(SDValue& N, SDValue &R) {
1690 if (N.getOpcode() != ISD::FrameIndex)
1691 return false;
1692 FrameIndexSDNode *FX = cast<FrameIndexSDNode>(N);
1693 R = CurDAG->getTargetFrameIndex(FX->getIndex(), MVT::i32);
1694 return true;
1695}