blob: e36cd8f8d27e9c4b97d59387be06aad679e0ea13 [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
29cl::opt<unsigned>
30MaxNumOfUsesForConstExtenders("ga-max-num-uses-for-constant-extenders",
31 cl::Hidden, cl::init(2),
32 cl::desc("Maximum number of uses of a global address such that we still us a"
33 "constant extended instruction"));
Tony Linthicum1213a7a2011-12-12 21:14:40 +000034
35//===----------------------------------------------------------------------===//
36// Instruction Selector Implementation
37//===----------------------------------------------------------------------===//
38
Jyotsna Vermad9225242013-02-13 21:38:46 +000039namespace llvm {
40 void initializeHexagonDAGToDAGISelPass(PassRegistry&);
41}
42
Tony Linthicum1213a7a2011-12-12 21:14:40 +000043//===--------------------------------------------------------------------===//
44/// HexagonDAGToDAGISel - Hexagon specific code to select Hexagon machine
45/// instructions for SelectionDAG operations.
46///
47namespace {
48class HexagonDAGToDAGISel : public SelectionDAGISel {
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +000049 const HexagonTargetMachine &HTM;
Eric Christopher23a7d1e2015-03-21 03:12:59 +000050 const HexagonSubtarget *HST;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +000051 const HexagonInstrInfo *HII;
52 const HexagonRegisterInfo *HRI;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000053public:
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +000054 explicit HexagonDAGToDAGISel(HexagonTargetMachine &tm,
Jyotsna Vermad9225242013-02-13 21:38:46 +000055 CodeGenOpt::Level OptLevel)
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +000056 : SelectionDAGISel(tm, OptLevel), HTM(tm), HST(nullptr), HII(nullptr),
57 HRI(nullptr) {
Jyotsna Vermad9225242013-02-13 21:38:46 +000058 initializeHexagonDAGToDAGISelPass(*PassRegistry::getPassRegistry());
Tony Linthicum1213a7a2011-12-12 21:14:40 +000059 }
Eric Christopher23a7d1e2015-03-21 03:12:59 +000060
61 bool runOnMachineFunction(MachineFunction &MF) override {
62 // Reset the subtarget each time through.
63 HST = &MF.getSubtarget<HexagonSubtarget>();
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +000064 HII = HST->getInstrInfo();
65 HRI = HST->getRegisterInfo();
Eric Christopher23a7d1e2015-03-21 03:12:59 +000066 SelectionDAGISel::runOnMachineFunction(MF);
67 return true;
68 }
69
Krzysztof Parzyszekae14e7b2015-03-17 21:47:16 +000070 virtual void PreprocessISelDAG() override;
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +000071 virtual void EmitFunctionEntryCode() override;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000072
Craig Topper906c2cd2014-04-29 07:58:16 +000073 SDNode *Select(SDNode *N) override;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000074
75 // Complex Pattern Selectors.
Colin LeMahieu987b0942015-02-04 20:38:01 +000076 inline bool SelectAddrGA(SDValue &N, SDValue &R);
Colin LeMahieu51491352015-02-04 22:36:28 +000077 inline bool SelectAddrGP(SDValue &N, SDValue &R);
Colin LeMahieu987b0942015-02-04 20:38:01 +000078 bool SelectGlobalAddress(SDValue &N, SDValue &R, bool UseGP);
Colin LeMahieuc7522f32015-01-14 23:07:36 +000079 bool SelectAddrFI(SDValue &N, SDValue &R);
80
Craig Topper906c2cd2014-04-29 07:58:16 +000081 const char *getPassName() const override {
Tony Linthicum1213a7a2011-12-12 21:14:40 +000082 return "Hexagon DAG->DAG Pattern Instruction Selection";
83 }
84
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +000085 // Generate a machine instruction node corresponding to the circ/brev
86 // load intrinsic.
87 MachineSDNode *LoadInstrForLoadIntrinsic(SDNode *IntN);
88 // Given the circ/brev load intrinsic and the already generated machine
89 // instruction, generate the appropriate store (that is a part of the
90 // intrinsic's functionality).
91 SDNode *StoreInstrForLoadIntrinsic(MachineSDNode *LoadN, SDNode *IntN);
92
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +000093 SDNode *SelectFrameIndex(SDNode *N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +000094 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
95 /// inline asm expressions.
Craig Topper906c2cd2014-04-29 07:58:16 +000096 bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Daniel Sanders60f1db02015-03-13 12:45:09 +000097 unsigned ConstraintID,
Craig Topper906c2cd2014-04-29 07:58:16 +000098 std::vector<SDValue> &OutOps) override;
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +000099 SDNode *SelectLoadOfLoadIntrinsic(LoadSDNode *N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000100 SDNode *SelectLoad(SDNode *N);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000101 SDNode *SelectBaseOffsetLoad(LoadSDNode *LD, SDLoc dl);
102 SDNode *SelectIndexedLoad(LoadSDNode *LD, SDLoc dl);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000103 SDNode *SelectIndexedLoadZeroExtend64(LoadSDNode *LD, unsigned Opcode,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000104 SDLoc dl);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000105 SDNode *SelectIndexedLoadSignExtend64(LoadSDNode *LD, unsigned Opcode,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000106 SDLoc dl);
107 SDNode *SelectBaseOffsetStore(StoreSDNode *ST, SDLoc dl);
108 SDNode *SelectIndexedStore(StoreSDNode *ST, SDLoc dl);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000109 SDNode *SelectStore(SDNode *N);
110 SDNode *SelectSHL(SDNode *N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000111 SDNode *SelectMul(SDNode *N);
112 SDNode *SelectZeroExtend(SDNode *N);
Sirish Pande69295b82012-05-10 20:20:25 +0000113 SDNode *SelectIntrinsicWChain(SDNode *N);
Krzysztof Parzyszekae14e7b2015-03-17 21:47:16 +0000114 SDNode *SelectIntrinsicWOChain(SDNode *N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000115 SDNode *SelectConstant(SDNode *N);
Sirish Pande69295b82012-05-10 20:20:25 +0000116 SDNode *SelectConstantFP(SDNode *N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000117 SDNode *SelectAdd(SDNode *N);
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +0000118 SDNode *SelectBitOp(SDNode *N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000119
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000120 // XformMskToBitPosU5Imm - Returns the bit position which
121 // the single bit 32 bit mask represents.
122 // Used in Clr and Set bit immediate memops.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000123 SDValue XformMskToBitPosU5Imm(uint32_t Imm, SDLoc DL) {
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000124 int32_t bitPos;
125 bitPos = Log2_32(Imm);
126 assert(bitPos >= 0 && bitPos < 32 &&
127 "Constant out of range for 32 BitPos Memops");
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000128 return CurDAG->getTargetConstant(bitPos, DL, MVT::i32);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000129 }
Jyotsna Vermafdc660b2013-03-22 18:41:34 +0000130
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000131 // XformMskToBitPosU4Imm - Returns the bit position which the single-bit
132 // 16 bit mask represents. Used in Clr and Set bit immediate memops.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000133 SDValue XformMskToBitPosU4Imm(uint16_t Imm, SDLoc DL) {
134 return XformMskToBitPosU5Imm(Imm, DL);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000135 }
Jyotsna Vermafdc660b2013-03-22 18:41:34 +0000136
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000137 // XformMskToBitPosU3Imm - Returns the bit position which the single-bit
138 // 8 bit mask represents. Used in Clr and Set bit immediate memops.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000139 SDValue XformMskToBitPosU3Imm(uint8_t Imm, SDLoc DL) {
140 return XformMskToBitPosU5Imm(Imm, DL);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000141 }
Jyotsna Vermafdc660b2013-03-22 18:41:34 +0000142
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000143 // Return true if there is exactly one bit set in V, i.e., if V is one of the
144 // following integers: 2^0, 2^1, ..., 2^31.
145 bool ImmIsSingleBit(uint32_t v) const {
146 return isPowerOf2_32(v);
147 }
Jyotsna Vermafdc660b2013-03-22 18:41:34 +0000148
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000149 // XformM5ToU5Imm - Return a target constant with the specified value, of
150 // type i32 where the negative literal is transformed into a positive literal
151 // for use in -= memops.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000152 inline SDValue XformM5ToU5Imm(signed Imm, SDLoc DL) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000153 assert((Imm >= -31 && Imm <= -1) && "Constant out of range for Memops");
154 return CurDAG->getTargetConstant(-Imm, DL, MVT::i32);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000155 }
Jyotsna Vermafdc660b2013-03-22 18:41:34 +0000156
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000157 // XformU7ToU7M1Imm - Return a target constant decremented by 1, in range
158 // [1..128], used in cmpb.gtu instructions.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000159 inline SDValue XformU7ToU7M1Imm(signed Imm, SDLoc DL) {
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000160 assert((Imm >= 1 && Imm <= 128) && "Constant out of range for cmpb op");
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000161 return CurDAG->getTargetConstant(Imm - 1, DL, MVT::i8);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000162 }
Jyotsna Vermafdc660b2013-03-22 18:41:34 +0000163
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000164 // XformS8ToS8M1Imm - Return a target constant decremented by 1.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000165 inline SDValue XformSToSM1Imm(signed Imm, SDLoc DL) {
166 return CurDAG->getTargetConstant(Imm - 1, DL, MVT::i32);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000167 }
Jyotsna Verma60316252013-02-05 19:20:45 +0000168
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000169 // XformU8ToU8M1Imm - Return a target constant decremented by 1.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000170 inline SDValue XformUToUM1Imm(unsigned Imm, SDLoc DL) {
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000171 assert((Imm >= 1) && "Cannot decrement unsigned int less than 1");
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000172 return CurDAG->getTargetConstant(Imm - 1, DL, MVT::i32);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000173 }
Jyotsna Verma89c84822013-04-23 19:15:55 +0000174
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000175 // XformSToSM2Imm - Return a target constant decremented by 2.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000176 inline SDValue XformSToSM2Imm(unsigned Imm, SDLoc DL) {
177 return CurDAG->getTargetConstant(Imm - 2, DL, MVT::i32);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000178 }
Jyotsna Verma89c84822013-04-23 19:15:55 +0000179
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000180 // XformSToSM3Imm - Return a target constant decremented by 3.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000181 inline SDValue XformSToSM3Imm(unsigned Imm, SDLoc DL) {
182 return CurDAG->getTargetConstant(Imm - 3, DL, MVT::i32);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000183 }
Colin LeMahieu19ed07c2015-01-28 18:29:11 +0000184
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000185 // Include the pieces autogenerated from the target description.
186 #include "HexagonGenDAGISel.inc"
Colin LeMahieu0ee02fc2015-01-19 20:31:18 +0000187
188private:
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000189 bool isValueExtension(const SDValue &Val, unsigned FromBits, SDValue &Src);
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +0000190 bool isAlignedMemNode(const MemSDNode *N) const;
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000191}; // end HexagonDAGToDAGISel
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000192} // end anonymous namespace
193
194
195/// createHexagonISelDag - This pass converts a legalized DAG into a
196/// Hexagon-specific DAG, ready for instruction scheduling.
197///
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000198namespace llvm {
199FunctionPass *createHexagonISelDag(HexagonTargetMachine &TM,
200 CodeGenOpt::Level OptLevel) {
Jyotsna Vermad9225242013-02-13 21:38:46 +0000201 return new HexagonDAGToDAGISel(TM, OptLevel);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000202}
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000203}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000204
Jyotsna Vermad9225242013-02-13 21:38:46 +0000205static void initializePassOnce(PassRegistry &Registry) {
206 const char *Name = "Hexagon DAG->DAG Pattern Instruction Selection";
207 PassInfo *PI = new PassInfo(Name, "hexagon-isel",
Craig Topper062a2ba2014-04-25 05:30:21 +0000208 &SelectionDAGISel::ID, nullptr, false, false);
Jyotsna Vermad9225242013-02-13 21:38:46 +0000209 Registry.registerPass(*PI, true);
210}
211
212void llvm::initializeHexagonDAGToDAGISelPass(PassRegistry &Registry) {
213 CALL_ONCE_INITIALIZATION(initializePassOnce)
214}
215
216
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000217// Intrinsics that return a a predicate.
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000218static bool doesIntrinsicReturnPredicate(unsigned ID) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000219 switch (ID) {
220 default:
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000221 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000222 case Intrinsic::hexagon_C2_cmpeq:
223 case Intrinsic::hexagon_C2_cmpgt:
224 case Intrinsic::hexagon_C2_cmpgtu:
225 case Intrinsic::hexagon_C2_cmpgtup:
226 case Intrinsic::hexagon_C2_cmpgtp:
227 case Intrinsic::hexagon_C2_cmpeqp:
228 case Intrinsic::hexagon_C2_bitsset:
229 case Intrinsic::hexagon_C2_bitsclr:
230 case Intrinsic::hexagon_C2_cmpeqi:
231 case Intrinsic::hexagon_C2_cmpgti:
232 case Intrinsic::hexagon_C2_cmpgtui:
233 case Intrinsic::hexagon_C2_cmpgei:
234 case Intrinsic::hexagon_C2_cmpgeui:
235 case Intrinsic::hexagon_C2_cmplt:
236 case Intrinsic::hexagon_C2_cmpltu:
237 case Intrinsic::hexagon_C2_bitsclri:
238 case Intrinsic::hexagon_C2_and:
239 case Intrinsic::hexagon_C2_or:
240 case Intrinsic::hexagon_C2_xor:
241 case Intrinsic::hexagon_C2_andn:
242 case Intrinsic::hexagon_C2_not:
243 case Intrinsic::hexagon_C2_orn:
244 case Intrinsic::hexagon_C2_pxfer_map:
245 case Intrinsic::hexagon_C2_any8:
246 case Intrinsic::hexagon_C2_all8:
247 case Intrinsic::hexagon_A2_vcmpbeq:
248 case Intrinsic::hexagon_A2_vcmpbgtu:
249 case Intrinsic::hexagon_A2_vcmpheq:
250 case Intrinsic::hexagon_A2_vcmphgt:
251 case Intrinsic::hexagon_A2_vcmphgtu:
252 case Intrinsic::hexagon_A2_vcmpweq:
253 case Intrinsic::hexagon_A2_vcmpwgt:
254 case Intrinsic::hexagon_A2_vcmpwgtu:
255 case Intrinsic::hexagon_C2_tfrrp:
256 case Intrinsic::hexagon_S2_tstbit_i:
257 case Intrinsic::hexagon_S2_tstbit_r:
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000258 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000259 }
260}
261
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000262SDNode *HexagonDAGToDAGISel::SelectIndexedLoadSignExtend64(LoadSDNode *LD,
263 unsigned Opcode,
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000264 SDLoc dl) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000265 SDValue Chain = LD->getChain();
266 EVT LoadedVT = LD->getMemoryVT();
267 SDValue Base = LD->getBasePtr();
268 SDValue Offset = LD->getOffset();
269 SDNode *OffsetNode = Offset.getNode();
270 int32_t Val = cast<ConstantSDNode>(OffsetNode)->getSExtValue();
Bill Wendling4a7a4082013-06-07 06:19:56 +0000271
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000272 if (HII->isValidAutoIncImm(LoadedVT, Val)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000273 SDValue TargetConst = CurDAG->getTargetConstant(Val, dl, MVT::i32);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000274 SDNode *Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::i32, MVT::i32,
275 MVT::Other, Base, TargetConst,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000276 Chain);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000277 SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A2_sxtw, dl, MVT::i64,
278 SDValue(Result_1, 0));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000279 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
280 MemOp[0] = LD->getMemOperand();
281 cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
282 const SDValue Froms[] = { SDValue(LD, 0),
283 SDValue(LD, 1),
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000284 SDValue(LD, 2) };
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000285 const SDValue Tos[] = { SDValue(Result_2, 0),
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000286 SDValue(Result_1, 1),
287 SDValue(Result_1, 2) };
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000288 ReplaceUses(Froms, Tos, 3);
289 return Result_2;
290 }
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000291
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000292 SDValue TargetConst0 = CurDAG->getTargetConstant(0, dl, MVT::i32);
293 SDValue TargetConstVal = CurDAG->getTargetConstant(Val, dl, MVT::i32);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000294 SDNode *Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::i32, MVT::Other,
295 Base, TargetConst0, Chain);
296 SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A2_sxtw, dl, MVT::i64,
297 SDValue(Result_1, 0));
298 SDNode* Result_3 = CurDAG->getMachineNode(Hexagon::A2_addi, dl, MVT::i32,
299 Base, TargetConstVal,
300 SDValue(Result_1, 1));
301 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
302 MemOp[0] = LD->getMemOperand();
303 cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
304 const SDValue Froms[] = { SDValue(LD, 0),
305 SDValue(LD, 1),
306 SDValue(LD, 2) };
307 const SDValue Tos[] = { SDValue(Result_2, 0),
308 SDValue(Result_3, 0),
309 SDValue(Result_1, 1) };
310 ReplaceUses(Froms, Tos, 3);
311 return Result_2;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000312}
313
314
315SDNode *HexagonDAGToDAGISel::SelectIndexedLoadZeroExtend64(LoadSDNode *LD,
316 unsigned Opcode,
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000317 SDLoc dl) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000318 SDValue Chain = LD->getChain();
319 EVT LoadedVT = LD->getMemoryVT();
320 SDValue Base = LD->getBasePtr();
321 SDValue Offset = LD->getOffset();
322 SDNode *OffsetNode = Offset.getNode();
323 int32_t Val = cast<ConstantSDNode>(OffsetNode)->getSExtValue();
Bill Wendling4a7a4082013-06-07 06:19:56 +0000324
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000325 if (HII->isValidAutoIncImm(LoadedVT, Val)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000326 SDValue TargetConstVal = CurDAG->getTargetConstant(Val, dl, MVT::i32);
327 SDValue TargetConst0 = CurDAG->getTargetConstant(0, dl, MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000328 SDNode *Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::i32,
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000329 MVT::i32, MVT::Other, Base,
330 TargetConstVal, Chain);
331 SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A4_combineir, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000332 MVT::i64, MVT::Other,
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000333 TargetConst0,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000334 SDValue(Result_1,0));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000335 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
336 MemOp[0] = LD->getMemOperand();
337 cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
338 const SDValue Froms[] = { SDValue(LD, 0),
339 SDValue(LD, 1),
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000340 SDValue(LD, 2) };
341 const SDValue Tos[] = { SDValue(Result_2, 0),
342 SDValue(Result_1, 1),
343 SDValue(Result_1, 2) };
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000344 ReplaceUses(Froms, Tos, 3);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000345 return Result_2;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000346 }
347
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000348 // Generate an indirect load.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000349 SDValue TargetConst0 = CurDAG->getTargetConstant(0, dl, MVT::i32);
350 SDValue TargetConstVal = CurDAG->getTargetConstant(Val, dl, MVT::i32);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000351 SDNode *Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::i32,
352 MVT::Other, Base, TargetConst0,
353 Chain);
354 SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A4_combineir, dl,
355 MVT::i64, MVT::Other,
356 TargetConst0,
357 SDValue(Result_1,0));
358 // Add offset to base.
359 SDNode* Result_3 = CurDAG->getMachineNode(Hexagon::A2_addi, dl, MVT::i32,
360 Base, TargetConstVal,
361 SDValue(Result_1, 1));
362 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
363 MemOp[0] = LD->getMemOperand();
364 cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
365 const SDValue Froms[] = { SDValue(LD, 0),
366 SDValue(LD, 1),
367 SDValue(LD, 2) };
368 const SDValue Tos[] = { SDValue(Result_2, 0), // Load value.
369 SDValue(Result_3, 0), // New address.
370 SDValue(Result_1, 1) };
371 ReplaceUses(Froms, Tos, 3);
372 return Result_2;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000373}
374
375
Andrew Trickef9de2a2013-05-25 02:42:55 +0000376SDNode *HexagonDAGToDAGISel::SelectIndexedLoad(LoadSDNode *LD, SDLoc dl) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000377 SDValue Chain = LD->getChain();
378 SDValue Base = LD->getBasePtr();
379 SDValue Offset = LD->getOffset();
380 SDNode *OffsetNode = Offset.getNode();
381 // Get the constant value.
382 int32_t Val = cast<ConstantSDNode>(OffsetNode)->getSExtValue();
383 EVT LoadedVT = LD->getMemoryVT();
384 unsigned Opcode = 0;
385
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000386 // Check for zero extended loads. Treat any-extend loads as zero extended
387 // loads.
388 ISD::LoadExtType ExtType = LD->getExtensionType();
389 bool IsZeroExt = (ExtType == ISD::ZEXTLOAD || ExtType == ISD::EXTLOAD);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000390 bool HasVecOffset = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000391
392 // Figure out the opcode.
393 if (LoadedVT == MVT::i64) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000394 if (HII->isValidAutoIncImm(LoadedVT, Val))
Colin LeMahieuc83cbbf2014-12-26 19:31:46 +0000395 Opcode = Hexagon::L2_loadrd_pi;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000396 else
Colin LeMahieu947cd702014-12-23 20:44:59 +0000397 Opcode = Hexagon::L2_loadrd_io;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000398 } else if (LoadedVT == MVT::i32) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000399 if (HII->isValidAutoIncImm(LoadedVT, Val))
Colin LeMahieuc83cbbf2014-12-26 19:31:46 +0000400 Opcode = Hexagon::L2_loadri_pi;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000401 else
Colin LeMahieu026e88d2014-12-23 20:02:16 +0000402 Opcode = Hexagon::L2_loadri_io;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000403 } else if (LoadedVT == MVT::i16) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000404 if (HII->isValidAutoIncImm(LoadedVT, Val))
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000405 Opcode = IsZeroExt ? Hexagon::L2_loadruh_pi : Hexagon::L2_loadrh_pi;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000406 else
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000407 Opcode = IsZeroExt ? Hexagon::L2_loadruh_io : Hexagon::L2_loadrh_io;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000408 } else if (LoadedVT == MVT::i8) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000409 if (HII->isValidAutoIncImm(LoadedVT, Val))
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000410 Opcode = IsZeroExt ? Hexagon::L2_loadrub_pi : Hexagon::L2_loadrb_pi;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000411 else
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000412 Opcode = IsZeroExt ? Hexagon::L2_loadrub_io : Hexagon::L2_loadrb_io;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000413 } else if (LoadedVT == MVT::v16i32 || LoadedVT == MVT::v8i64 ||
414 LoadedVT == MVT::v32i16 || LoadedVT == MVT::v64i8) {
415 HasVecOffset = true;
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +0000416 bool Aligned = isAlignedMemNode(LD);
417 if (HII->isValidAutoIncImm(LoadedVT, Val))
418 Opcode = Aligned ? Hexagon::V6_vL32b_pi : Hexagon::V6_vL32Ub_pi;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000419 else
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +0000420 Opcode = Aligned ? Hexagon::V6_vL32b_ai : Hexagon::V6_vL32Ub_ai;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000421 // 128B
422 } else if (LoadedVT == MVT::v32i32 || LoadedVT == MVT::v16i64 ||
423 LoadedVT == MVT::v64i16 || LoadedVT == MVT::v128i8) {
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +0000424 if (HST->useHVXOps()) {
425 bool Aligned = isAlignedMemNode(LD);
426 HasVecOffset = true;
427 if (HII->isValidAutoIncImm(LoadedVT, Val))
428 Opcode = Aligned ? Hexagon::V6_vL32b_pi_128B
429 : Hexagon::V6_vL32Ub_pi_128B;
430 else
431 Opcode = Aligned ? Hexagon::V6_vL32b_ai_128B
432 : Hexagon::V6_vL32Ub_ai_128B;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000433 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000434 } else
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000435 llvm_unreachable("unknown memory type");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000436
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000437 // For zero extended i64 loads, we need to add combine instructions.
438 if (LD->getValueType(0) == MVT::i64 && IsZeroExt)
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000439 return SelectIndexedLoadZeroExtend64(LD, Opcode, dl);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000440 // Handle sign extended i64 loads.
441 if (LD->getValueType(0) == MVT::i64 && ExtType == ISD::SEXTLOAD)
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000442 return SelectIndexedLoadSignExtend64(LD, Opcode, dl);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000443
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000444 if (HII->isValidAutoIncImm(LoadedVT, Val)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000445 SDValue TargetConstVal = CurDAG->getTargetConstant(Val, dl, MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000446 SDNode* Result = CurDAG->getMachineNode(Opcode, dl,
447 LD->getValueType(0),
448 MVT::i32, MVT::Other, Base,
449 TargetConstVal, Chain);
450 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
451 MemOp[0] = LD->getMemOperand();
452 cast<MachineSDNode>(Result)->setMemRefs(MemOp, MemOp + 1);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000453 if (HasVecOffset) {
454 const SDValue Froms[] = { SDValue(LD, 0),
455 SDValue(LD, 2)
456 };
457 const SDValue Tos[] = { SDValue(Result, 0),
458 SDValue(Result, 2)
459 };
460 ReplaceUses(Froms, Tos, 2);
461 } else {
462 const SDValue Froms[] = { SDValue(LD, 0),
463 SDValue(LD, 1),
464 SDValue(LD, 2)
465 };
466 const SDValue Tos[] = { SDValue(Result, 0),
467 SDValue(Result, 1),
468 SDValue(Result, 2)
469 };
470 ReplaceUses(Froms, Tos, 3);
471 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000472 return Result;
473 } else {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000474 SDValue TargetConst0 = CurDAG->getTargetConstant(0, dl, MVT::i32);
475 SDValue TargetConstVal = CurDAG->getTargetConstant(Val, dl, MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000476 SDNode* Result_1 = CurDAG->getMachineNode(Opcode, dl,
477 LD->getValueType(0),
478 MVT::Other, Base, TargetConst0,
479 Chain);
Colin LeMahieuf297dbe2015-02-05 17:49:13 +0000480 SDNode* Result_2 = CurDAG->getMachineNode(Hexagon::A2_addi, dl, MVT::i32,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000481 Base, TargetConstVal,
482 SDValue(Result_1, 1));
483 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
484 MemOp[0] = LD->getMemOperand();
485 cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
486 const SDValue Froms[] = { SDValue(LD, 0),
487 SDValue(LD, 1),
488 SDValue(LD, 2)
489 };
490 const SDValue Tos[] = { SDValue(Result_1, 0),
491 SDValue(Result_2, 0),
492 SDValue(Result_1, 1)
493 };
494 ReplaceUses(Froms, Tos, 3);
495 return Result_1;
496 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000497}
498
499
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +0000500MachineSDNode *HexagonDAGToDAGISel::LoadInstrForLoadIntrinsic(SDNode *IntN) {
501 if (IntN->getOpcode() != ISD::INTRINSIC_W_CHAIN)
502 return nullptr;
503
504 SDLoc dl(IntN);
505 unsigned IntNo = cast<ConstantSDNode>(IntN->getOperand(1))->getZExtValue();
506
507 static std::map<unsigned,unsigned> LoadPciMap = {
508 { Intrinsic::hexagon_circ_ldb, Hexagon::L2_loadrb_pci },
509 { Intrinsic::hexagon_circ_ldub, Hexagon::L2_loadrub_pci },
510 { Intrinsic::hexagon_circ_ldh, Hexagon::L2_loadrh_pci },
511 { Intrinsic::hexagon_circ_lduh, Hexagon::L2_loadruh_pci },
512 { Intrinsic::hexagon_circ_ldw, Hexagon::L2_loadri_pci },
513 { Intrinsic::hexagon_circ_ldd, Hexagon::L2_loadrd_pci },
514 };
515 auto FLC = LoadPciMap.find(IntNo);
516 if (FLC != LoadPciMap.end()) {
517 SDNode *Mod = CurDAG->getMachineNode(Hexagon::A2_tfrrcr, dl, MVT::i32,
518 IntN->getOperand(4));
519 EVT ValTy = (IntNo == Intrinsic::hexagon_circ_ldd) ? MVT::i64 : MVT::i32;
520 EVT RTys[] = { ValTy, MVT::i32, MVT::Other };
521 // Operands: { Base, Increment, Modifier, Chain }
522 auto Inc = cast<ConstantSDNode>(IntN->getOperand(5));
523 SDValue I = CurDAG->getTargetConstant(Inc->getSExtValue(), dl, MVT::i32);
524 MachineSDNode *Res = CurDAG->getMachineNode(FLC->second, dl, RTys,
525 { IntN->getOperand(2), I, SDValue(Mod,0), IntN->getOperand(0) });
526 return Res;
527 }
528
529 static std::map<unsigned,unsigned> LoadPbrMap = {
530 { Intrinsic::hexagon_brev_ldb, Hexagon::L2_loadrb_pbr },
531 { Intrinsic::hexagon_brev_ldub, Hexagon::L2_loadrub_pbr },
532 { Intrinsic::hexagon_brev_ldh, Hexagon::L2_loadrh_pbr },
533 { Intrinsic::hexagon_brev_lduh, Hexagon::L2_loadruh_pbr },
534 { Intrinsic::hexagon_brev_ldw, Hexagon::L2_loadri_pbr },
535 { Intrinsic::hexagon_brev_ldd, Hexagon::L2_loadrd_pbr },
536 };
537 auto FLB = LoadPbrMap.find(IntNo);
538 if (FLB != LoadPbrMap.end()) {
539 SDNode *Mod = CurDAG->getMachineNode(Hexagon::A2_tfrrcr, dl, MVT::i32,
540 IntN->getOperand(4));
541 EVT ValTy = (IntNo == Intrinsic::hexagon_brev_ldd) ? MVT::i64 : MVT::i32;
542 EVT RTys[] = { ValTy, MVT::i32, MVT::Other };
543 // Operands: { Base, Modifier, Chain }
544 MachineSDNode *Res = CurDAG->getMachineNode(FLB->second, dl, RTys,
545 { IntN->getOperand(2), SDValue(Mod,0), IntN->getOperand(0) });
546 return Res;
547 }
548
549 return nullptr;
550}
551
552SDNode *HexagonDAGToDAGISel::StoreInstrForLoadIntrinsic(MachineSDNode *LoadN,
553 SDNode *IntN) {
554 // The "LoadN" is just a machine load instruction. The intrinsic also
555 // involves storing it. Generate an appropriate store to the location
556 // given in the intrinsic's operand(3).
557 uint64_t F = HII->get(LoadN->getMachineOpcode()).TSFlags;
558 unsigned SizeBits = (F >> HexagonII::MemAccessSizePos) &
559 HexagonII::MemAccesSizeMask;
560 unsigned Size = 1U << (SizeBits-1);
561
562 SDLoc dl(IntN);
563 MachinePointerInfo PI;
564 SDValue TS;
565 SDValue Loc = IntN->getOperand(3);
566
567 if (Size >= 4)
568 TS = CurDAG->getStore(SDValue(LoadN,2), dl, SDValue(LoadN, 0), Loc, PI,
569 false, false, Size);
570 else
571 TS = CurDAG->getTruncStore(SDValue(LoadN,2), dl, SDValue(LoadN,0), Loc, PI,
572 MVT::getIntegerVT(Size*8), false, false, Size);
573 SDNode *StoreN = SelectStore(TS.getNode());
574
575 // Load's results are { Loaded value, Updated pointer, Chain }
576 ReplaceUses(SDValue(IntN, 0), SDValue(LoadN, 1));
577 ReplaceUses(SDValue(IntN, 1), SDValue(StoreN, 0));
578 return StoreN;
579}
580
581SDNode *HexagonDAGToDAGISel::SelectLoadOfLoadIntrinsic(LoadSDNode *N) {
582 // The intrinsics for load circ/brev perform two operations:
583 // 1. Load a value V from the specified location, using the addressing
584 // mode corresponding to the intrinsic.
585 // 2. Store V into a specified location. This location is typically a
586 // local, temporary object.
587 // In many cases, the program using these intrinsics will immediately
588 // load V again from the local object. In those cases, when certain
589 // conditions are met, the last load can be removed.
590 // This function identifies and optimizes this pattern. If the pattern
591 // cannot be optimized, it returns nullptr, which will cause the load
592 // to be selected separately from the intrinsic (which will be handled
593 // in SelectIntrinsicWChain).
594
595 SDValue Ch = N->getOperand(0);
596 SDValue Loc = N->getOperand(1);
597
598 // Assume that the load and the intrinsic are connected directly with a
599 // chain:
600 // t1: i32,ch = int.load ..., ..., ..., Loc, ... // <-- C
601 // t2: i32,ch = load t1:1, Loc, ...
602 SDNode *C = Ch.getNode();
603
604 if (C->getOpcode() != ISD::INTRINSIC_W_CHAIN)
605 return nullptr;
606
607 // The second load can only be eliminated if its extension type matches
608 // that of the load instruction corresponding to the intrinsic. The user
609 // can provide an address of an unsigned variable to store the result of
610 // a sign-extending intrinsic into (or the other way around).
611 ISD::LoadExtType IntExt;
612 switch (cast<ConstantSDNode>(C->getOperand(1))->getZExtValue()) {
613 case Intrinsic::hexagon_brev_ldub:
614 case Intrinsic::hexagon_brev_lduh:
615 case Intrinsic::hexagon_circ_ldub:
616 case Intrinsic::hexagon_circ_lduh:
617 IntExt = ISD::ZEXTLOAD;
618 break;
619 case Intrinsic::hexagon_brev_ldw:
620 case Intrinsic::hexagon_brev_ldd:
621 case Intrinsic::hexagon_circ_ldw:
622 case Intrinsic::hexagon_circ_ldd:
623 IntExt = ISD::NON_EXTLOAD;
624 break;
625 default:
626 IntExt = ISD::SEXTLOAD;
627 break;
628 }
629 if (N->getExtensionType() != IntExt)
630 return nullptr;
631
632 // Make sure the target location for the loaded value in the load intrinsic
633 // is the location from which LD (or N) is loading.
634 if (C->getNumOperands() < 4 || Loc.getNode() != C->getOperand(3).getNode())
635 return nullptr;
636
637 if (MachineSDNode *L = LoadInstrForLoadIntrinsic(C)) {
638 SDNode *S = StoreInstrForLoadIntrinsic(L, C);
639 SDValue F[] = { SDValue(N,0), SDValue(N,1), SDValue(C,0), SDValue(C,1) };
640 SDValue T[] = { SDValue(L,0), SDValue(S,0), SDValue(L,1), SDValue(S,0) };
641 ReplaceUses(F, T, array_lengthof(T));
642 // This transformation will leave the intrinsic dead. If it remains in
643 // the DAG, the selection code will see it again, but without the load,
644 // and it will generate a store that is normally required for it.
645 CurDAG->RemoveDeadNodes();
646 return L;
647 }
648
649 return nullptr;
650}
651
652
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000653SDNode *HexagonDAGToDAGISel::SelectLoad(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000654 SDLoc dl(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000655 LoadSDNode *LD = cast<LoadSDNode>(N);
656 ISD::MemIndexedMode AM = LD->getAddressingMode();
657
658 // Handle indexed loads.
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +0000659 if (AM != ISD::UNINDEXED)
660 return SelectIndexedLoad(LD, dl);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000661
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +0000662 // Handle patterns using circ/brev load intrinsics.
663 if (SDNode *LI = SelectLoadOfLoadIntrinsic(LD))
664 return LI;
665
666 return SelectCode(LD);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000667}
668
669
Andrew Trickef9de2a2013-05-25 02:42:55 +0000670SDNode *HexagonDAGToDAGISel::SelectIndexedStore(StoreSDNode *ST, SDLoc dl) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000671 SDValue Chain = ST->getChain();
672 SDValue Base = ST->getBasePtr();
673 SDValue Offset = ST->getOffset();
674 SDValue Value = ST->getValue();
675 SDNode *OffsetNode = Offset.getNode();
676 // Get the constant value.
677 int32_t Val = cast<ConstantSDNode>(OffsetNode)->getSExtValue();
678 EVT StoredVT = ST->getMemoryVT();
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000679 EVT ValueVT = Value.getValueType();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000680
681 // Offset value must be within representable range
682 // and must have correct alignment properties.
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000683 if (HII->isValidAutoIncImm(StoredVT, Val)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000684 unsigned Opcode = 0;
685
686 // Figure out the post inc version of opcode.
Colin LeMahieu9a3cd3f2014-12-29 20:00:43 +0000687 if (StoredVT == MVT::i64) Opcode = Hexagon::S2_storerd_pi;
688 else if (StoredVT == MVT::i32) Opcode = Hexagon::S2_storeri_pi;
689 else if (StoredVT == MVT::i16) Opcode = Hexagon::S2_storerh_pi;
Colin LeMahieu3d34afb2014-12-29 19:42:14 +0000690 else if (StoredVT == MVT::i8) Opcode = Hexagon::S2_storerb_pi;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000691 else if (StoredVT == MVT::v16i32 || StoredVT == MVT::v8i64 ||
692 StoredVT == MVT::v32i16 || StoredVT == MVT::v64i8) {
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +0000693 if (isAlignedMemNode(ST))
694 Opcode = Hexagon::V6_vS32b_pi;
695 else
696 Opcode = Hexagon::V6_vS32Ub_pi;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000697 }
698 // 128B
699 else if (StoredVT == MVT::v32i32 || StoredVT == MVT::v16i64 ||
700 StoredVT == MVT::v64i16 || StoredVT == MVT::v128i8) {
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +0000701 if (HST->useHVXOps())
702 Opcode = isAlignedMemNode(ST) ? Hexagon::V6_vS32b_pi_128B
703 : Hexagon::V6_vS32Ub_pi_128B;
704 } else
705 llvm_unreachable("unknown memory type");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000706
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000707 if (ST->isTruncatingStore() && ValueVT.getSizeInBits() == 64) {
708 assert(StoredVT.getSizeInBits() < 64 && "Not a truncating store");
709 Value = CurDAG->getTargetExtractSubreg(Hexagon::subreg_loreg,
710 dl, MVT::i32, Value);
711 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000712 SDValue Ops[] = {Base, CurDAG->getTargetConstant(Val, dl, MVT::i32), Value,
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000713 Chain};
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000714 // Build post increment store.
715 SDNode* Result = CurDAG->getMachineNode(Opcode, dl, MVT::i32,
Michael Liaob53d8962013-04-19 22:22:57 +0000716 MVT::Other, Ops);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000717 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
718 MemOp[0] = ST->getMemOperand();
719 cast<MachineSDNode>(Result)->setMemRefs(MemOp, MemOp + 1);
720
721 ReplaceUses(ST, Result);
722 ReplaceUses(SDValue(ST,1), SDValue(Result,1));
723 return Result;
724 }
725
726 // Note: Order of operands matches the def of instruction:
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000727 // def S2_storerd_io
728 // : STInst<(outs), (ins IntRegs:$base, imm:$offset, DoubleRegs:$src1), ...
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000729 // and it differs for POST_ST* for instance.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000730 SDValue Ops[] = { Base, CurDAG->getTargetConstant(0, dl, MVT::i32), Value,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000731 Chain};
732 unsigned Opcode = 0;
733
734 // Figure out the opcode.
Colin LeMahieubda31b42014-12-29 20:44:51 +0000735 if (StoredVT == MVT::i64) Opcode = Hexagon::S2_storerd_io;
736 else if (StoredVT == MVT::i32) Opcode = Hexagon::S2_storeri_io;
737 else if (StoredVT == MVT::i16) Opcode = Hexagon::S2_storerh_io;
738 else if (StoredVT == MVT::i8) Opcode = Hexagon::S2_storerb_io;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000739 else if (StoredVT == MVT::v16i32 || StoredVT == MVT::v8i64 ||
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +0000740 StoredVT == MVT::v32i16 || StoredVT == MVT::v64i8) {
741 if (isAlignedMemNode(ST))
742 Opcode = Hexagon::V6_vS32b_ai;
743 else
744 Opcode = Hexagon::V6_vS32Ub_ai;
745 }
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000746 // 128B
747 else if (StoredVT == MVT::v32i32 || StoredVT == MVT::v16i64 ||
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +0000748 StoredVT == MVT::v64i16 || StoredVT == MVT::v128i8) {
749 if (isAlignedMemNode(ST))
750 Opcode = Hexagon::V6_vS32b_ai_128B;
751 else
752 Opcode = Hexagon::V6_vS32Ub_ai_128B;
753 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000754 else llvm_unreachable("unknown memory type");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000755
756 // Build regular store.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000757 SDValue TargetConstVal = CurDAG->getTargetConstant(Val, dl, MVT::i32);
Michael Liaob53d8962013-04-19 22:22:57 +0000758 SDNode* Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::Other, Ops);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000759 // Build splitted incriment instruction.
Colin LeMahieuf297dbe2015-02-05 17:49:13 +0000760 SDNode* Result_2 = CurDAG->getMachineNode(Hexagon::A2_addi, dl, MVT::i32,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000761 Base,
762 TargetConstVal,
763 SDValue(Result_1, 0));
764 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
765 MemOp[0] = ST->getMemOperand();
766 cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
767
768 ReplaceUses(SDValue(ST,0), SDValue(Result_2,0));
769 ReplaceUses(SDValue(ST,1), SDValue(Result_1,0));
770 return Result_2;
771}
772
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000773SDNode *HexagonDAGToDAGISel::SelectStore(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000774 SDLoc dl(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000775 StoreSDNode *ST = cast<StoreSDNode>(N);
776 ISD::MemIndexedMode AM = ST->getAddressingMode();
777
778 // Handle indexed stores.
779 if (AM != ISD::UNINDEXED) {
780 return SelectIndexedStore(ST, dl);
781 }
Sirish Pandec92c3162012-05-03 16:18:50 +0000782
Colin LeMahieu2efa2d02015-03-09 21:48:13 +0000783 return SelectCode(ST);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000784}
785
786SDNode *HexagonDAGToDAGISel::SelectMul(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000787 SDLoc dl(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000788
789 //
790 // %conv.i = sext i32 %tmp1 to i64
791 // %conv2.i = sext i32 %add to i64
792 // %mul.i = mul nsw i64 %conv2.i, %conv.i
793 //
794 // --- match with the following ---
795 //
796 // %mul.i = mpy (%tmp1, %add)
797 //
798
799 if (N->getValueType(0) == MVT::i64) {
800 // Shifting a i64 signed multiply.
801 SDValue MulOp0 = N->getOperand(0);
802 SDValue MulOp1 = N->getOperand(1);
803
804 SDValue OP0;
805 SDValue OP1;
806
807 // Handle sign_extend and sextload.
808 if (MulOp0.getOpcode() == ISD::SIGN_EXTEND) {
809 SDValue Sext0 = MulOp0.getOperand(0);
810 if (Sext0.getNode()->getValueType(0) != MVT::i32) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000811 return SelectCode(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000812 }
813
814 OP0 = Sext0;
815 } else if (MulOp0.getOpcode() == ISD::LOAD) {
816 LoadSDNode *LD = cast<LoadSDNode>(MulOp0.getNode());
817 if (LD->getMemoryVT() != MVT::i32 ||
818 LD->getExtensionType() != ISD::SEXTLOAD ||
819 LD->getAddressingMode() != ISD::UNINDEXED) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000820 return SelectCode(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000821 }
822
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000823 SDValue Chain = LD->getChain();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000824 SDValue TargetConst0 = CurDAG->getTargetConstant(0, dl, MVT::i32);
Colin LeMahieu026e88d2014-12-23 20:02:16 +0000825 OP0 = SDValue(CurDAG->getMachineNode(Hexagon::L2_loadri_io, dl, MVT::i32,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000826 MVT::Other,
827 LD->getBasePtr(), TargetConst0,
828 Chain), 0);
829 } else {
830 return SelectCode(N);
831 }
832
833 // Same goes for the second operand.
834 if (MulOp1.getOpcode() == ISD::SIGN_EXTEND) {
835 SDValue Sext1 = MulOp1.getOperand(0);
836 if (Sext1.getNode()->getValueType(0) != MVT::i32) {
837 return SelectCode(N);
838 }
839
840 OP1 = Sext1;
841 } else if (MulOp1.getOpcode() == ISD::LOAD) {
842 LoadSDNode *LD = cast<LoadSDNode>(MulOp1.getNode());
843 if (LD->getMemoryVT() != MVT::i32 ||
844 LD->getExtensionType() != ISD::SEXTLOAD ||
845 LD->getAddressingMode() != ISD::UNINDEXED) {
846 return SelectCode(N);
847 }
848
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000849 SDValue Chain = LD->getChain();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000850 SDValue TargetConst0 = CurDAG->getTargetConstant(0, dl, MVT::i32);
Colin LeMahieu026e88d2014-12-23 20:02:16 +0000851 OP1 = SDValue(CurDAG->getMachineNode(Hexagon::L2_loadri_io, dl, MVT::i32,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000852 MVT::Other,
853 LD->getBasePtr(), TargetConst0,
854 Chain), 0);
855 } else {
856 return SelectCode(N);
857 }
858
859 // Generate a mpy instruction.
Colin LeMahieud9b23502014-12-16 16:10:01 +0000860 SDNode *Result = CurDAG->getMachineNode(Hexagon::M2_dpmpyss_s0, dl, MVT::i64,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000861 OP0, OP1);
862 ReplaceUses(N, Result);
863 return Result;
864 }
865
866 return SelectCode(N);
867}
868
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000869SDNode *HexagonDAGToDAGISel::SelectSHL(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000870 SDLoc dl(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000871 if (N->getValueType(0) == MVT::i32) {
872 SDValue Shl_0 = N->getOperand(0);
873 SDValue Shl_1 = N->getOperand(1);
874 // RHS is const.
875 if (Shl_1.getOpcode() == ISD::Constant) {
876 if (Shl_0.getOpcode() == ISD::MUL) {
877 SDValue Mul_0 = Shl_0.getOperand(0); // Val
878 SDValue Mul_1 = Shl_0.getOperand(1); // Const
879 // RHS of mul is const.
880 if (Mul_1.getOpcode() == ISD::Constant) {
881 int32_t ShlConst =
882 cast<ConstantSDNode>(Shl_1.getNode())->getSExtValue();
883 int32_t MulConst =
884 cast<ConstantSDNode>(Mul_1.getNode())->getSExtValue();
885 int32_t ValConst = MulConst << ShlConst;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000886 SDValue Val = CurDAG->getTargetConstant(ValConst, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000887 MVT::i32);
888 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Val.getNode()))
889 if (isInt<9>(CN->getSExtValue())) {
890 SDNode* Result =
Colin LeMahieud9b23502014-12-16 16:10:01 +0000891 CurDAG->getMachineNode(Hexagon::M2_mpysmi, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000892 MVT::i32, Mul_0, Val);
893 ReplaceUses(N, Result);
894 return Result;
895 }
896
897 }
898 } else if (Shl_0.getOpcode() == ISD::SUB) {
899 SDValue Sub_0 = Shl_0.getOperand(0); // Const 0
900 SDValue Sub_1 = Shl_0.getOperand(1); // Val
901 if (Sub_0.getOpcode() == ISD::Constant) {
902 int32_t SubConst =
903 cast<ConstantSDNode>(Sub_0.getNode())->getSExtValue();
904 if (SubConst == 0) {
905 if (Sub_1.getOpcode() == ISD::SHL) {
906 SDValue Shl2_0 = Sub_1.getOperand(0); // Val
907 SDValue Shl2_1 = Sub_1.getOperand(1); // Const
908 if (Shl2_1.getOpcode() == ISD::Constant) {
909 int32_t ShlConst =
910 cast<ConstantSDNode>(Shl_1.getNode())->getSExtValue();
911 int32_t Shl2Const =
912 cast<ConstantSDNode>(Shl2_1.getNode())->getSExtValue();
913 int32_t ValConst = 1 << (ShlConst+Shl2Const);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000914 SDValue Val = CurDAG->getTargetConstant(-ValConst, dl,
915 MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000916 if (ConstantSDNode *CN =
917 dyn_cast<ConstantSDNode>(Val.getNode()))
918 if (isInt<9>(CN->getSExtValue())) {
919 SDNode* Result =
Colin LeMahieud9b23502014-12-16 16:10:01 +0000920 CurDAG->getMachineNode(Hexagon::M2_mpysmi, dl, MVT::i32,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000921 Shl2_0, Val);
922 ReplaceUses(N, Result);
923 return Result;
924 }
925 }
926 }
927 }
928 }
929 }
930 }
931 }
932 return SelectCode(N);
933}
934
935
936//
937// If there is an zero_extend followed an intrinsic in DAG (this means - the
938// result of the intrinsic is predicate); convert the zero_extend to
939// transfer instruction.
940//
941// Zero extend -> transfer is lowered here. Otherwise, zero_extend will be
942// converted into a MUX as predicate registers defined as 1 bit in the
943// compiler. Architecture defines them as 8-bit registers.
944// We want to preserve all the lower 8-bits and, not just 1 LSB bit.
945//
946SDNode *HexagonDAGToDAGISel::SelectZeroExtend(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000947 SDLoc dl(N);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000948
949 SDValue Op0 = N->getOperand(0);
950 EVT OpVT = Op0.getValueType();
951 unsigned OpBW = OpVT.getSizeInBits();
952
953 // Special handling for zero-extending a vector of booleans.
954 if (OpVT.isVector() && OpVT.getVectorElementType() == MVT::i1 && OpBW <= 64) {
955 SDNode *Mask = CurDAG->getMachineNode(Hexagon::C2_mask, dl, MVT::i64, Op0);
956 unsigned NE = OpVT.getVectorNumElements();
957 EVT ExVT = N->getValueType(0);
958 unsigned ES = ExVT.getVectorElementType().getSizeInBits();
959 uint64_t MV = 0, Bit = 1;
960 for (unsigned i = 0; i < NE; ++i) {
961 MV |= Bit;
962 Bit <<= ES;
963 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000964 SDValue Ones = CurDAG->getTargetConstant(MV, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000965 SDNode *OnesReg = CurDAG->getMachineNode(Hexagon::CONST64_Int_Real, dl,
966 MVT::i64, Ones);
967 if (ExVT.getSizeInBits() == 32) {
968 SDNode *And = CurDAG->getMachineNode(Hexagon::A2_andp, dl, MVT::i64,
969 SDValue(Mask,0), SDValue(OnesReg,0));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000970 SDValue SubR = CurDAG->getTargetConstant(Hexagon::subreg_loreg, dl,
971 MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000972 return CurDAG->getMachineNode(Hexagon::EXTRACT_SUBREG, dl, ExVT,
973 SDValue(And,0), SubR);
974 }
975 return CurDAG->getMachineNode(Hexagon::A2_andp, dl, ExVT,
976 SDValue(Mask,0), SDValue(OnesReg,0));
977 }
978
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000979 SDNode *IsIntrinsic = N->getOperand(0).getNode();
980 if ((IsIntrinsic->getOpcode() == ISD::INTRINSIC_WO_CHAIN)) {
981 unsigned ID =
982 cast<ConstantSDNode>(IsIntrinsic->getOperand(0))->getZExtValue();
983 if (doesIntrinsicReturnPredicate(ID)) {
984 // Now we need to differentiate target data types.
985 if (N->getValueType(0) == MVT::i64) {
Krzysztof Parzyszekae14e7b2015-03-17 21:47:16 +0000986 // Convert the zero_extend to Rs = Pd followed by A2_combinew(0,Rs).
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000987 SDValue TargetConst0 = CurDAG->getTargetConstant(0, dl, MVT::i32);
Colin LeMahieu30dcb232014-12-09 18:16:49 +0000988 SDNode *Result_1 = CurDAG->getMachineNode(Hexagon::C2_tfrpr, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000989 MVT::i32,
990 SDValue(IsIntrinsic, 0));
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000991 SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A2_tfrsi, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000992 MVT::i32,
993 TargetConst0);
Colin LeMahieub580d7d2014-12-09 19:23:45 +0000994 SDNode *Result_3 = CurDAG->getMachineNode(Hexagon::A2_combinew, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000995 MVT::i64, MVT::Other,
996 SDValue(Result_2, 0),
997 SDValue(Result_1, 0));
998 ReplaceUses(N, Result_3);
999 return Result_3;
1000 }
1001 if (N->getValueType(0) == MVT::i32) {
1002 // Convert the zero_extend to Rs = Pd
Colin LeMahieu30dcb232014-12-09 18:16:49 +00001003 SDNode* RsPd = CurDAG->getMachineNode(Hexagon::C2_tfrpr, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001004 MVT::i32,
1005 SDValue(IsIntrinsic, 0));
1006 ReplaceUses(N, RsPd);
1007 return RsPd;
1008 }
Craig Toppere55c5562012-02-07 02:50:20 +00001009 llvm_unreachable("Unexpected value type");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001010 }
1011 }
1012 return SelectCode(N);
1013}
1014
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +00001015
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001016//
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +00001017// Handling intrinsics for circular load and bitreverse load.
Krzysztof Parzyszek47ab1f22015-03-18 16:23:44 +00001018//
1019SDNode *HexagonDAGToDAGISel::SelectIntrinsicWChain(SDNode *N) {
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +00001020 if (MachineSDNode *L = LoadInstrForLoadIntrinsic(N))
1021 return StoreInstrForLoadIntrinsic(L, N);
Krzysztof Parzyszek47ab1f22015-03-18 16:23:44 +00001022 return SelectCode(N);
1023}
1024
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001025SDNode *HexagonDAGToDAGISel::SelectIntrinsicWOChain(SDNode *N) {
Colin LeMahieu0ee02fc2015-01-19 20:31:18 +00001026 unsigned IID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
1027 unsigned Bits;
1028 switch (IID) {
1029 case Intrinsic::hexagon_S2_vsplatrb:
1030 Bits = 8;
1031 break;
1032 case Intrinsic::hexagon_S2_vsplatrh:
1033 Bits = 16;
1034 break;
1035 default:
1036 return SelectCode(N);
1037 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001038
Colin LeMahieu0ee02fc2015-01-19 20:31:18 +00001039 SDValue const &V = N->getOperand(1);
1040 SDValue U;
1041 if (isValueExtension(V, Bits, U)) {
1042 SDValue R = CurDAG->getNode(N->getOpcode(), SDLoc(N), N->getValueType(0),
1043 N->getOperand(0), U);
1044 return SelectCode(R.getNode());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001045 }
1046 return SelectCode(N);
1047}
1048
Sirish Pande69295b82012-05-10 20:20:25 +00001049//
1050// Map floating point constant values.
1051//
1052SDNode *HexagonDAGToDAGISel::SelectConstantFP(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001053 SDLoc dl(N);
Sirish Pande69295b82012-05-10 20:20:25 +00001054 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N);
1055 APFloat APF = CN->getValueAPF();
1056 if (N->getValueType(0) == MVT::f32) {
1057 return CurDAG->getMachineNode(Hexagon::TFRI_f, dl, MVT::f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001058 CurDAG->getTargetConstantFP(APF.convertToFloat(), dl, MVT::f32));
Sirish Pande69295b82012-05-10 20:20:25 +00001059 }
1060 else if (N->getValueType(0) == MVT::f64) {
1061 return CurDAG->getMachineNode(Hexagon::CONST64_Float_Real, dl, MVT::f64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001062 CurDAG->getTargetConstantFP(APF.convertToDouble(), dl, MVT::f64));
Sirish Pande69295b82012-05-10 20:20:25 +00001063 }
1064
1065 return SelectCode(N);
1066}
1067
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001068//
1069// Map predicate true (encoded as -1 in LLVM) to a XOR.
1070//
1071SDNode *HexagonDAGToDAGISel::SelectConstant(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001072 SDLoc dl(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001073 if (N->getValueType(0) == MVT::i1) {
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +00001074 SDNode* Result = 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001075 int32_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Krzysztof Parzyszek7a9cd802015-03-18 18:50:06 +00001076 if (Val == -1) {
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +00001077 Result = CurDAG->getMachineNode(Hexagon::TFR_PdTrue, dl, MVT::i1);
1078 } else if (Val == 0) {
1079 Result = CurDAG->getMachineNode(Hexagon::TFR_PdFalse, dl, MVT::i1);
1080 }
1081 if (Result) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001082 ReplaceUses(N, Result);
1083 return Result;
1084 }
1085 }
1086
1087 return SelectCode(N);
1088}
1089
1090
1091//
1092// Map add followed by a asr -> asr +=.
1093//
1094SDNode *HexagonDAGToDAGISel::SelectAdd(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001095 SDLoc dl(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001096 if (N->getValueType(0) != MVT::i32) {
1097 return SelectCode(N);
1098 }
1099 // Identify nodes of the form: add(asr(...)).
1100 SDNode* Src1 = N->getOperand(0).getNode();
1101 if (Src1->getOpcode() != ISD::SRA || !Src1->hasOneUse()
1102 || Src1->getValueType(0) != MVT::i32) {
1103 return SelectCode(N);
1104 }
1105
1106 // Build Rd = Rd' + asr(Rs, Rt). The machine constraints will ensure that
1107 // Rd and Rd' are assigned to the same register
Colin LeMahieu0f850bd2014-12-19 20:29:29 +00001108 SDNode* Result = CurDAG->getMachineNode(Hexagon::S2_asr_r_r_acc, dl, MVT::i32,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001109 N->getOperand(1),
1110 Src1->getOperand(0),
1111 Src1->getOperand(1));
1112 ReplaceUses(N, Result);
1113
1114 return Result;
1115}
1116
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001117//
1118// Map the following, where possible.
1119// AND/FABS -> clrbit
1120// OR -> setbit
1121// XOR/FNEG ->toggle_bit.
1122//
1123SDNode *HexagonDAGToDAGISel::SelectBitOp(SDNode *N) {
1124 SDLoc dl(N);
1125 EVT ValueVT = N->getValueType(0);
1126
1127 // We handle only 32 and 64-bit bit ops.
1128 if (!(ValueVT == MVT::i32 || ValueVT == MVT::i64 ||
1129 ValueVT == MVT::f32 || ValueVT == MVT::f64))
1130 return SelectCode(N);
1131
1132 // We handly only fabs and fneg for V5.
1133 unsigned Opc = N->getOpcode();
Eric Christopher23a7d1e2015-03-21 03:12:59 +00001134 if ((Opc == ISD::FABS || Opc == ISD::FNEG) && !HST->hasV5TOps())
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001135 return SelectCode(N);
1136
1137 int64_t Val = 0;
1138 if (Opc != ISD::FABS && Opc != ISD::FNEG) {
1139 if (N->getOperand(1).getOpcode() == ISD::Constant)
1140 Val = cast<ConstantSDNode>((N)->getOperand(1))->getSExtValue();
1141 else
1142 return SelectCode(N);
1143 }
1144
1145 if (Opc == ISD::AND) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001146 // Check if this is a bit-clearing AND, if not select code the usual way.
1147 if ((ValueVT == MVT::i32 && isPowerOf2_32(~Val)) ||
1148 (ValueVT == MVT::i64 && isPowerOf2_64(~Val)))
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001149 Val = ~Val;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001150 else
1151 return SelectCode(N);
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001152 }
1153
1154 // If OR or AND is being fed by shl, srl and, sra don't do this change,
1155 // because Hexagon provide |= &= on shl, srl, and sra.
1156 // Traverse the DAG to see if there is shl, srl and sra.
1157 if (Opc == ISD::OR || Opc == ISD::AND) {
1158 switch (N->getOperand(0)->getOpcode()) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001159 default:
1160 break;
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001161 case ISD::SRA:
1162 case ISD::SRL:
1163 case ISD::SHL:
1164 return SelectCode(N);
1165 }
1166 }
1167
1168 // Make sure it's power of 2.
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001169 unsigned BitPos = 0;
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001170 if (Opc != ISD::FABS && Opc != ISD::FNEG) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001171 if ((ValueVT == MVT::i32 && !isPowerOf2_32(Val)) ||
1172 (ValueVT == MVT::i64 && !isPowerOf2_64(Val)))
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001173 return SelectCode(N);
1174
1175 // Get the bit position.
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001176 BitPos = countTrailingZeros(uint64_t(Val));
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001177 } else {
1178 // For fabs and fneg, it's always the 31st bit.
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001179 BitPos = 31;
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001180 }
1181
1182 unsigned BitOpc = 0;
1183 // Set the right opcode for bitwise operations.
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001184 switch (Opc) {
1185 default:
1186 llvm_unreachable("Only bit-wise/abs/neg operations are allowed.");
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001187 case ISD::AND:
1188 case ISD::FABS:
1189 BitOpc = Hexagon::S2_clrbit_i;
1190 break;
1191 case ISD::OR:
1192 BitOpc = Hexagon::S2_setbit_i;
1193 break;
1194 case ISD::XOR:
1195 case ISD::FNEG:
1196 BitOpc = Hexagon::S2_togglebit_i;
1197 break;
1198 }
1199
1200 SDNode *Result;
1201 // Get the right SDVal for the opcode.
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001202 SDValue SDVal = CurDAG->getTargetConstant(BitPos, dl, MVT::i32);
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001203
1204 if (ValueVT == MVT::i32 || ValueVT == MVT::f32) {
1205 Result = CurDAG->getMachineNode(BitOpc, dl, ValueVT,
1206 N->getOperand(0), SDVal);
1207 } else {
1208 // 64-bit gymnastic to use REG_SEQUENCE. But it's worth it.
1209 EVT SubValueVT;
1210 if (ValueVT == MVT::i64)
1211 SubValueVT = MVT::i32;
1212 else
1213 SubValueVT = MVT::f32;
1214
1215 SDNode *Reg = N->getOperand(0).getNode();
1216 SDValue RegClass = CurDAG->getTargetConstant(Hexagon::DoubleRegsRegClassID,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001217 dl, MVT::i64);
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001218
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001219 SDValue SubregHiIdx = CurDAG->getTargetConstant(Hexagon::subreg_hireg, dl,
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001220 MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001221 SDValue SubregLoIdx = CurDAG->getTargetConstant(Hexagon::subreg_loreg, dl,
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001222 MVT::i32);
1223
1224 SDValue SubregHI = CurDAG->getTargetExtractSubreg(Hexagon::subreg_hireg, dl,
1225 MVT::i32, SDValue(Reg, 0));
1226
1227 SDValue SubregLO = CurDAG->getTargetExtractSubreg(Hexagon::subreg_loreg, dl,
1228 MVT::i32, SDValue(Reg, 0));
1229
1230 // Clear/set/toggle hi or lo registers depending on the bit position.
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001231 if (SubValueVT != MVT::f32 && BitPos < 32) {
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001232 SDNode *Result0 = CurDAG->getMachineNode(BitOpc, dl, SubValueVT,
1233 SubregLO, SDVal);
1234 const SDValue Ops[] = { RegClass, SubregHI, SubregHiIdx,
1235 SDValue(Result0, 0), SubregLoIdx };
1236 Result = CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE,
1237 dl, ValueVT, Ops);
1238 } else {
1239 if (Opc != ISD::FABS && Opc != ISD::FNEG)
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001240 SDVal = CurDAG->getTargetConstant(BitPos-32, dl, MVT::i32);
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001241 SDNode *Result0 = CurDAG->getMachineNode(BitOpc, dl, SubValueVT,
1242 SubregHI, SDVal);
1243 const SDValue Ops[] = { RegClass, SDValue(Result0, 0), SubregHiIdx,
1244 SubregLO, SubregLoIdx };
1245 Result = CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE,
1246 dl, ValueVT, Ops);
1247 }
1248 }
1249
1250 ReplaceUses(N, Result);
1251 return Result;
1252}
1253
1254
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001255SDNode *HexagonDAGToDAGISel::SelectFrameIndex(SDNode *N) {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001256 MachineFrameInfo *MFI = MF->getFrameInfo();
1257 const HexagonFrameLowering *HFI = HST->getFrameLowering();
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001258 int FX = cast<FrameIndexSDNode>(N)->getIndex();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001259 unsigned StkA = HFI->getStackAlignment();
1260 unsigned MaxA = MFI->getMaxAlignment();
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001261 SDValue FI = CurDAG->getTargetFrameIndex(FX, MVT::i32);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001262 SDLoc DL(N);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001263 SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001264 SDNode *R = 0;
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001265
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001266 // Use TFR_FI when:
1267 // - the object is fixed, or
1268 // - there are no objects with higher-than-default alignment, or
1269 // - there are no dynamically allocated objects.
1270 // Otherwise, use TFR_FIA.
1271 if (FX < 0 || MaxA <= StkA || !MFI->hasVarSizedObjects()) {
1272 R = CurDAG->getMachineNode(Hexagon::TFR_FI, DL, MVT::i32, FI, Zero);
1273 } else {
1274 auto &HMFI = *MF->getInfo<HexagonMachineFunctionInfo>();
1275 unsigned AR = HMFI.getStackAlignBaseVReg();
1276 SDValue CH = CurDAG->getEntryNode();
1277 SDValue Ops[] = { CurDAG->getCopyFromReg(CH, DL, AR, MVT::i32), FI, Zero };
1278 R = CurDAG->getMachineNode(Hexagon::TFR_FIA, DL, MVT::i32, Ops);
1279 }
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001280
1281 if (N->getHasDebugValue())
1282 CurDAG->TransferDbgValues(SDValue(N, 0), SDValue(R, 0));
1283 return R;
1284}
1285
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001286
1287SDNode *HexagonDAGToDAGISel::Select(SDNode *N) {
Tim Northover31d093c2013-09-22 08:21:56 +00001288 if (N->isMachineOpcode()) {
1289 N->setNodeId(-1);
Craig Topper062a2ba2014-04-25 05:30:21 +00001290 return nullptr; // Already selected.
Tim Northover31d093c2013-09-22 08:21:56 +00001291 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001292
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001293 switch (N->getOpcode()) {
1294 case ISD::Constant:
1295 return SelectConstant(N);
1296
Sirish Pande69295b82012-05-10 20:20:25 +00001297 case ISD::ConstantFP:
1298 return SelectConstantFP(N);
1299
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001300 case ISD::FrameIndex:
1301 return SelectFrameIndex(N);
1302
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001303 case ISD::ADD:
1304 return SelectAdd(N);
1305
1306 case ISD::SHL:
1307 return SelectSHL(N);
1308
1309 case ISD::LOAD:
1310 return SelectLoad(N);
1311
1312 case ISD::STORE:
1313 return SelectStore(N);
1314
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001315 case ISD::MUL:
1316 return SelectMul(N);
1317
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001318 case ISD::AND:
1319 case ISD::OR:
1320 case ISD::XOR:
1321 case ISD::FABS:
1322 case ISD::FNEG:
1323 return SelectBitOp(N);
1324
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001325 case ISD::ZERO_EXTEND:
1326 return SelectZeroExtend(N);
1327
Krzysztof Parzyszek47ab1f22015-03-18 16:23:44 +00001328 case ISD::INTRINSIC_W_CHAIN:
1329 return SelectIntrinsicWChain(N);
1330
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001331 case ISD::INTRINSIC_WO_CHAIN:
1332 return SelectIntrinsicWOChain(N);
1333 }
1334
1335 return SelectCode(N);
1336}
1337
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001338bool HexagonDAGToDAGISel::
Daniel Sanders60f1db02015-03-13 12:45:09 +00001339SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001340 std::vector<SDValue> &OutOps) {
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001341 SDValue Inp = Op, Res;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001342
Daniel Sanders60f1db02015-03-13 12:45:09 +00001343 switch (ConstraintID) {
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001344 default:
1345 return true;
Daniel Sanders49f643c2015-03-17 14:37:39 +00001346 case InlineAsm::Constraint_i:
1347 case InlineAsm::Constraint_o: // Offsetable.
1348 case InlineAsm::Constraint_v: // Not offsetable.
1349 case InlineAsm::Constraint_m: // Memory.
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001350 if (SelectAddrFI(Inp, Res))
1351 OutOps.push_back(Res);
1352 else
1353 OutOps.push_back(Inp);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001354 break;
1355 }
1356
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001357 OutOps.push_back(CurDAG->getTargetConstant(0, SDLoc(Op), MVT::i32));
Jyotsna Vermad9225242013-02-13 21:38:46 +00001358 return false;
1359}
Colin LeMahieuc7522f32015-01-14 23:07:36 +00001360
Colin LeMahieu79ec0652015-06-12 19:57:32 +00001361
Krzysztof Parzyszekae14e7b2015-03-17 21:47:16 +00001362void HexagonDAGToDAGISel::PreprocessISelDAG() {
1363 SelectionDAG &DAG = *CurDAG;
1364 std::vector<SDNode*> Nodes;
Pete Cooper7e64ef02015-07-14 23:43:29 +00001365 for (SDNode &Node : DAG.allnodes())
1366 Nodes.push_back(&Node);
Krzysztof Parzyszekae14e7b2015-03-17 21:47:16 +00001367
1368 // Simplify: (or (select c x 0) z) -> (select c (or x z) z)
1369 // (or (select c 0 y) z) -> (select c z (or y z))
1370 // This may not be the right thing for all targets, so do it here.
1371 for (auto I: Nodes) {
1372 if (I->getOpcode() != ISD::OR)
1373 continue;
1374
1375 auto IsZero = [] (const SDValue &V) -> bool {
1376 if (ConstantSDNode *SC = dyn_cast<ConstantSDNode>(V.getNode()))
1377 return SC->isNullValue();
1378 return false;
1379 };
1380 auto IsSelect0 = [IsZero] (const SDValue &Op) -> bool {
1381 if (Op.getOpcode() != ISD::SELECT)
1382 return false;
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +00001383 return IsZero(Op.getOperand(1)) || IsZero(Op.getOperand(2));
Krzysztof Parzyszekae14e7b2015-03-17 21:47:16 +00001384 };
1385
1386 SDValue N0 = I->getOperand(0), N1 = I->getOperand(1);
1387 EVT VT = I->getValueType(0);
1388 bool SelN0 = IsSelect0(N0);
1389 SDValue SOp = SelN0 ? N0 : N1;
1390 SDValue VOp = SelN0 ? N1 : N0;
1391
1392 if (SOp.getOpcode() == ISD::SELECT && SOp.getNode()->hasOneUse()) {
1393 SDValue SC = SOp.getOperand(0);
1394 SDValue SX = SOp.getOperand(1);
1395 SDValue SY = SOp.getOperand(2);
1396 SDLoc DLS = SOp;
1397 if (IsZero(SY)) {
1398 SDValue NewOr = DAG.getNode(ISD::OR, DLS, VT, SX, VOp);
1399 SDValue NewSel = DAG.getNode(ISD::SELECT, DLS, VT, SC, NewOr, VOp);
1400 DAG.ReplaceAllUsesWith(I, NewSel.getNode());
1401 } else if (IsZero(SX)) {
1402 SDValue NewOr = DAG.getNode(ISD::OR, DLS, VT, SY, VOp);
1403 SDValue NewSel = DAG.getNode(ISD::SELECT, DLS, VT, SC, VOp, NewOr);
1404 DAG.ReplaceAllUsesWith(I, NewSel.getNode());
1405 }
1406 }
1407 }
1408}
1409
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001410void HexagonDAGToDAGISel::EmitFunctionEntryCode() {
1411 auto &HST = static_cast<const HexagonSubtarget&>(MF->getSubtarget());
1412 auto &HFI = *HST.getFrameLowering();
1413 if (!HFI.needsAligna(*MF))
1414 return;
Krzysztof Parzyszekae14e7b2015-03-17 21:47:16 +00001415
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001416 MachineFrameInfo *MFI = MF->getFrameInfo();
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +00001417 MachineBasicBlock *EntryBB = &MF->front();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001418 unsigned AR = FuncInfo->CreateReg(MVT::i32);
1419 unsigned MaxA = MFI->getMaxAlignment();
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001420 BuildMI(EntryBB, DebugLoc(), HII->get(Hexagon::ALIGNA), AR)
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001421 .addImm(MaxA);
1422 MF->getInfo<HexagonMachineFunctionInfo>()->setStackAlignBaseVReg(AR);
1423}
1424
1425// Match a frame index that can be used in an addressing mode.
Colin LeMahieuc7522f32015-01-14 23:07:36 +00001426bool HexagonDAGToDAGISel::SelectAddrFI(SDValue& N, SDValue &R) {
1427 if (N.getOpcode() != ISD::FrameIndex)
1428 return false;
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001429 auto &HFI = *HST->getFrameLowering();
1430 MachineFrameInfo *MFI = MF->getFrameInfo();
1431 int FX = cast<FrameIndexSDNode>(N)->getIndex();
1432 if (!MFI->isFixedObjectIndex(FX) && HFI.needsAligna(*MF))
1433 return false;
1434 R = CurDAG->getTargetFrameIndex(FX, MVT::i32);
Colin LeMahieuc7522f32015-01-14 23:07:36 +00001435 return true;
1436}
Colin LeMahieu0ee02fc2015-01-19 20:31:18 +00001437
Colin LeMahieu987b0942015-02-04 20:38:01 +00001438inline bool HexagonDAGToDAGISel::SelectAddrGA(SDValue &N, SDValue &R) {
1439 return SelectGlobalAddress(N, R, false);
1440}
1441
Colin LeMahieu51491352015-02-04 22:36:28 +00001442inline bool HexagonDAGToDAGISel::SelectAddrGP(SDValue &N, SDValue &R) {
1443 return SelectGlobalAddress(N, R, true);
1444}
1445
Colin LeMahieu987b0942015-02-04 20:38:01 +00001446bool HexagonDAGToDAGISel::SelectGlobalAddress(SDValue &N, SDValue &R,
1447 bool UseGP) {
1448 switch (N.getOpcode()) {
1449 case ISD::ADD: {
1450 SDValue N0 = N.getOperand(0);
1451 SDValue N1 = N.getOperand(1);
1452 unsigned GAOpc = N0.getOpcode();
1453 if (UseGP && GAOpc != HexagonISD::CONST32_GP)
1454 return false;
1455 if (!UseGP && GAOpc != HexagonISD::CONST32)
1456 return false;
1457 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N1)) {
1458 SDValue Addr = N0.getOperand(0);
1459 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Addr)) {
1460 if (GA->getOpcode() == ISD::TargetGlobalAddress) {
1461 uint64_t NewOff = GA->getOffset() + (uint64_t)Const->getSExtValue();
1462 R = CurDAG->getTargetGlobalAddress(GA->getGlobal(), SDLoc(Const),
1463 N.getValueType(), NewOff);
1464 return true;
1465 }
1466 }
1467 }
1468 break;
1469 }
1470 case HexagonISD::CONST32:
1471 // The operand(0) of CONST32 is TargetGlobalAddress, which is what we
1472 // want in the instruction.
1473 if (!UseGP)
1474 R = N.getOperand(0);
1475 return !UseGP;
1476 case HexagonISD::CONST32_GP:
1477 if (UseGP)
1478 R = N.getOperand(0);
1479 return UseGP;
1480 default:
1481 return false;
1482 }
1483
1484 return false;
1485}
1486
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001487bool HexagonDAGToDAGISel::isValueExtension(const SDValue &Val,
1488 unsigned FromBits, SDValue &Src) {
Colin LeMahieu0ee02fc2015-01-19 20:31:18 +00001489 unsigned Opc = Val.getOpcode();
1490 switch (Opc) {
1491 case ISD::SIGN_EXTEND:
1492 case ISD::ZERO_EXTEND:
1493 case ISD::ANY_EXTEND: {
1494 SDValue const &Op0 = Val.getOperand(0);
1495 EVT T = Op0.getValueType();
1496 if (T.isInteger() && T.getSizeInBits() == FromBits) {
1497 Src = Op0;
1498 return true;
1499 }
1500 break;
1501 }
1502 case ISD::SIGN_EXTEND_INREG:
1503 case ISD::AssertSext:
1504 case ISD::AssertZext:
1505 if (Val.getOperand(0).getValueType().isInteger()) {
1506 VTSDNode *T = cast<VTSDNode>(Val.getOperand(1));
1507 if (T->getVT().getSizeInBits() == FromBits) {
1508 Src = Val.getOperand(0);
1509 return true;
1510 }
1511 }
1512 break;
1513 case ISD::AND: {
1514 // Check if this is an AND with "FromBits" of lower bits set to 1.
1515 uint64_t FromMask = (1 << FromBits) - 1;
1516 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(0))) {
1517 if (C->getZExtValue() == FromMask) {
1518 Src = Val.getOperand(1);
1519 return true;
1520 }
1521 }
1522 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(1))) {
1523 if (C->getZExtValue() == FromMask) {
1524 Src = Val.getOperand(0);
1525 return true;
1526 }
1527 }
1528 break;
1529 }
1530 case ISD::OR:
1531 case ISD::XOR: {
1532 // OR/XOR with the lower "FromBits" bits set to 0.
1533 uint64_t FromMask = (1 << FromBits) - 1;
1534 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(0))) {
1535 if ((C->getZExtValue() & FromMask) == 0) {
1536 Src = Val.getOperand(1);
1537 return true;
1538 }
1539 }
1540 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(1))) {
1541 if ((C->getZExtValue() & FromMask) == 0) {
1542 Src = Val.getOperand(0);
1543 return true;
1544 }
1545 }
1546 }
1547 default:
1548 break;
1549 }
1550 return false;
1551}
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +00001552
1553bool HexagonDAGToDAGISel::isAlignedMemNode(const MemSDNode *N) const {
1554 return N->getAlignment() >= N->getMemoryVT().getStoreSize();
1555}