blob: c782638123b4c17041c94bfbe1ef83f2cf14611e [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
Justin Bognerb0126992016-05-05 23:19:08 +000073 SDNode *SelectImpl(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);
Justin Bognerdcb7a822016-05-10 20:31:53 +0000573
574 SDNode *StoreN;
575 {
576 HandleSDNode Handle(TS);
577 SelectStore(TS.getNode());
578 StoreN = Handle.getValue().getNode();
579 }
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +0000580
581 // Load's results are { Loaded value, Updated pointer, Chain }
582 ReplaceUses(SDValue(IntN, 0), SDValue(LoadN, 1));
583 ReplaceUses(SDValue(IntN, 1), SDValue(StoreN, 0));
584 return StoreN;
585}
586
587SDNode *HexagonDAGToDAGISel::SelectLoadOfLoadIntrinsic(LoadSDNode *N) {
588 // The intrinsics for load circ/brev perform two operations:
589 // 1. Load a value V from the specified location, using the addressing
590 // mode corresponding to the intrinsic.
591 // 2. Store V into a specified location. This location is typically a
592 // local, temporary object.
593 // In many cases, the program using these intrinsics will immediately
594 // load V again from the local object. In those cases, when certain
595 // conditions are met, the last load can be removed.
596 // This function identifies and optimizes this pattern. If the pattern
597 // cannot be optimized, it returns nullptr, which will cause the load
598 // to be selected separately from the intrinsic (which will be handled
599 // in SelectIntrinsicWChain).
600
601 SDValue Ch = N->getOperand(0);
602 SDValue Loc = N->getOperand(1);
603
604 // Assume that the load and the intrinsic are connected directly with a
605 // chain:
606 // t1: i32,ch = int.load ..., ..., ..., Loc, ... // <-- C
607 // t2: i32,ch = load t1:1, Loc, ...
608 SDNode *C = Ch.getNode();
609
610 if (C->getOpcode() != ISD::INTRINSIC_W_CHAIN)
611 return nullptr;
612
613 // The second load can only be eliminated if its extension type matches
614 // that of the load instruction corresponding to the intrinsic. The user
615 // can provide an address of an unsigned variable to store the result of
616 // a sign-extending intrinsic into (or the other way around).
617 ISD::LoadExtType IntExt;
618 switch (cast<ConstantSDNode>(C->getOperand(1))->getZExtValue()) {
619 case Intrinsic::hexagon_brev_ldub:
620 case Intrinsic::hexagon_brev_lduh:
621 case Intrinsic::hexagon_circ_ldub:
622 case Intrinsic::hexagon_circ_lduh:
623 IntExt = ISD::ZEXTLOAD;
624 break;
625 case Intrinsic::hexagon_brev_ldw:
626 case Intrinsic::hexagon_brev_ldd:
627 case Intrinsic::hexagon_circ_ldw:
628 case Intrinsic::hexagon_circ_ldd:
629 IntExt = ISD::NON_EXTLOAD;
630 break;
631 default:
632 IntExt = ISD::SEXTLOAD;
633 break;
634 }
635 if (N->getExtensionType() != IntExt)
636 return nullptr;
637
638 // Make sure the target location for the loaded value in the load intrinsic
639 // is the location from which LD (or N) is loading.
640 if (C->getNumOperands() < 4 || Loc.getNode() != C->getOperand(3).getNode())
641 return nullptr;
642
643 if (MachineSDNode *L = LoadInstrForLoadIntrinsic(C)) {
644 SDNode *S = StoreInstrForLoadIntrinsic(L, C);
645 SDValue F[] = { SDValue(N,0), SDValue(N,1), SDValue(C,0), SDValue(C,1) };
646 SDValue T[] = { SDValue(L,0), SDValue(S,0), SDValue(L,1), SDValue(S,0) };
647 ReplaceUses(F, T, array_lengthof(T));
648 // This transformation will leave the intrinsic dead. If it remains in
649 // the DAG, the selection code will see it again, but without the load,
650 // and it will generate a store that is normally required for it.
651 CurDAG->RemoveDeadNodes();
652 return L;
653 }
654
655 return nullptr;
656}
657
658
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000659SDNode *HexagonDAGToDAGISel::SelectLoad(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000660 SDLoc dl(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000661 LoadSDNode *LD = cast<LoadSDNode>(N);
662 ISD::MemIndexedMode AM = LD->getAddressingMode();
663
664 // Handle indexed loads.
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +0000665 if (AM != ISD::UNINDEXED)
666 return SelectIndexedLoad(LD, dl);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000667
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +0000668 // Handle patterns using circ/brev load intrinsics.
669 if (SDNode *LI = SelectLoadOfLoadIntrinsic(LD))
670 return LI;
671
672 return SelectCode(LD);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000673}
674
675
Andrew Trickef9de2a2013-05-25 02:42:55 +0000676SDNode *HexagonDAGToDAGISel::SelectIndexedStore(StoreSDNode *ST, SDLoc dl) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000677 SDValue Chain = ST->getChain();
678 SDValue Base = ST->getBasePtr();
679 SDValue Offset = ST->getOffset();
680 SDValue Value = ST->getValue();
681 SDNode *OffsetNode = Offset.getNode();
682 // Get the constant value.
683 int32_t Val = cast<ConstantSDNode>(OffsetNode)->getSExtValue();
684 EVT StoredVT = ST->getMemoryVT();
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000685 EVT ValueVT = Value.getValueType();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000686
687 // Offset value must be within representable range
688 // and must have correct alignment properties.
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000689 if (HII->isValidAutoIncImm(StoredVT, Val)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000690 unsigned Opcode = 0;
691
692 // Figure out the post inc version of opcode.
Colin LeMahieu9a3cd3f2014-12-29 20:00:43 +0000693 if (StoredVT == MVT::i64) Opcode = Hexagon::S2_storerd_pi;
694 else if (StoredVT == MVT::i32) Opcode = Hexagon::S2_storeri_pi;
695 else if (StoredVT == MVT::i16) Opcode = Hexagon::S2_storerh_pi;
Colin LeMahieu3d34afb2014-12-29 19:42:14 +0000696 else if (StoredVT == MVT::i8) Opcode = Hexagon::S2_storerb_pi;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000697 else if (StoredVT == MVT::v16i32 || StoredVT == MVT::v8i64 ||
698 StoredVT == MVT::v32i16 || StoredVT == MVT::v64i8) {
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +0000699 if (isAlignedMemNode(ST))
700 Opcode = Hexagon::V6_vS32b_pi;
701 else
702 Opcode = Hexagon::V6_vS32Ub_pi;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000703 }
704 // 128B
705 else if (StoredVT == MVT::v32i32 || StoredVT == MVT::v16i64 ||
706 StoredVT == MVT::v64i16 || StoredVT == MVT::v128i8) {
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +0000707 if (HST->useHVXOps())
708 Opcode = isAlignedMemNode(ST) ? Hexagon::V6_vS32b_pi_128B
709 : Hexagon::V6_vS32Ub_pi_128B;
710 } else
711 llvm_unreachable("unknown memory type");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000712
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000713 if (ST->isTruncatingStore() && ValueVT.getSizeInBits() == 64) {
714 assert(StoredVT.getSizeInBits() < 64 && "Not a truncating store");
715 Value = CurDAG->getTargetExtractSubreg(Hexagon::subreg_loreg,
716 dl, MVT::i32, Value);
717 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000718 SDValue Ops[] = {Base, CurDAG->getTargetConstant(Val, dl, MVT::i32), Value,
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000719 Chain};
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000720 // 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));
Justin Bognerdcb7a822016-05-10 20:31:53 +0000729 CurDAG->RemoveDeadNode(ST);
730 return nullptr;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000731 }
732
733 // Note: Order of operands matches the def of instruction:
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000734 // def S2_storerd_io
735 // : STInst<(outs), (ins IntRegs:$base, imm:$offset, DoubleRegs:$src1), ...
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000736 // and it differs for POST_ST* for instance.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000737 SDValue Ops[] = { Base, CurDAG->getTargetConstant(0, dl, MVT::i32), Value,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000738 Chain};
739 unsigned Opcode = 0;
740
741 // Figure out the opcode.
Colin LeMahieubda31b42014-12-29 20:44:51 +0000742 if (StoredVT == MVT::i64) Opcode = Hexagon::S2_storerd_io;
743 else if (StoredVT == MVT::i32) Opcode = Hexagon::S2_storeri_io;
744 else if (StoredVT == MVT::i16) Opcode = Hexagon::S2_storerh_io;
745 else if (StoredVT == MVT::i8) Opcode = Hexagon::S2_storerb_io;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000746 else if (StoredVT == MVT::v16i32 || StoredVT == MVT::v8i64 ||
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +0000747 StoredVT == MVT::v32i16 || StoredVT == MVT::v64i8) {
748 if (isAlignedMemNode(ST))
749 Opcode = Hexagon::V6_vS32b_ai;
750 else
751 Opcode = Hexagon::V6_vS32Ub_ai;
752 }
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000753 // 128B
754 else if (StoredVT == MVT::v32i32 || StoredVT == MVT::v16i64 ||
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +0000755 StoredVT == MVT::v64i16 || StoredVT == MVT::v128i8) {
756 if (isAlignedMemNode(ST))
757 Opcode = Hexagon::V6_vS32b_ai_128B;
758 else
759 Opcode = Hexagon::V6_vS32Ub_ai_128B;
760 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000761 else llvm_unreachable("unknown memory type");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000762
763 // Build regular store.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000764 SDValue TargetConstVal = CurDAG->getTargetConstant(Val, dl, MVT::i32);
Michael Liaob53d8962013-04-19 22:22:57 +0000765 SDNode* Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::Other, Ops);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000766 // Build splitted incriment instruction.
Colin LeMahieuf297dbe2015-02-05 17:49:13 +0000767 SDNode* Result_2 = CurDAG->getMachineNode(Hexagon::A2_addi, dl, MVT::i32,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000768 Base,
769 TargetConstVal,
770 SDValue(Result_1, 0));
771 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
772 MemOp[0] = ST->getMemOperand();
773 cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
774
775 ReplaceUses(SDValue(ST,0), SDValue(Result_2,0));
776 ReplaceUses(SDValue(ST,1), SDValue(Result_1,0));
Justin Bognerdcb7a822016-05-10 20:31:53 +0000777 CurDAG->RemoveDeadNode(ST);
778 return nullptr;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000779}
780
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000781SDNode *HexagonDAGToDAGISel::SelectStore(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000782 SDLoc dl(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000783 StoreSDNode *ST = cast<StoreSDNode>(N);
784 ISD::MemIndexedMode AM = ST->getAddressingMode();
785
786 // Handle indexed stores.
787 if (AM != ISD::UNINDEXED) {
788 return SelectIndexedStore(ST, dl);
789 }
Sirish Pandec92c3162012-05-03 16:18:50 +0000790
Colin LeMahieu2efa2d02015-03-09 21:48:13 +0000791 return SelectCode(ST);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000792}
793
794SDNode *HexagonDAGToDAGISel::SelectMul(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000795 SDLoc dl(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000796
797 //
798 // %conv.i = sext i32 %tmp1 to i64
799 // %conv2.i = sext i32 %add to i64
800 // %mul.i = mul nsw i64 %conv2.i, %conv.i
801 //
802 // --- match with the following ---
803 //
804 // %mul.i = mpy (%tmp1, %add)
805 //
806
807 if (N->getValueType(0) == MVT::i64) {
808 // Shifting a i64 signed multiply.
809 SDValue MulOp0 = N->getOperand(0);
810 SDValue MulOp1 = N->getOperand(1);
811
812 SDValue OP0;
813 SDValue OP1;
814
815 // Handle sign_extend and sextload.
816 if (MulOp0.getOpcode() == ISD::SIGN_EXTEND) {
817 SDValue Sext0 = MulOp0.getOperand(0);
818 if (Sext0.getNode()->getValueType(0) != MVT::i32) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000819 return SelectCode(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000820 }
821
822 OP0 = Sext0;
823 } else if (MulOp0.getOpcode() == ISD::LOAD) {
824 LoadSDNode *LD = cast<LoadSDNode>(MulOp0.getNode());
825 if (LD->getMemoryVT() != MVT::i32 ||
826 LD->getExtensionType() != ISD::SEXTLOAD ||
827 LD->getAddressingMode() != ISD::UNINDEXED) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000828 return SelectCode(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000829 }
830
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000831 SDValue Chain = LD->getChain();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000832 SDValue TargetConst0 = CurDAG->getTargetConstant(0, dl, MVT::i32);
Colin LeMahieu026e88d2014-12-23 20:02:16 +0000833 OP0 = SDValue(CurDAG->getMachineNode(Hexagon::L2_loadri_io, dl, MVT::i32,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000834 MVT::Other,
835 LD->getBasePtr(), TargetConst0,
836 Chain), 0);
837 } else {
838 return SelectCode(N);
839 }
840
841 // Same goes for the second operand.
842 if (MulOp1.getOpcode() == ISD::SIGN_EXTEND) {
843 SDValue Sext1 = MulOp1.getOperand(0);
844 if (Sext1.getNode()->getValueType(0) != MVT::i32) {
845 return SelectCode(N);
846 }
847
848 OP1 = Sext1;
849 } else if (MulOp1.getOpcode() == ISD::LOAD) {
850 LoadSDNode *LD = cast<LoadSDNode>(MulOp1.getNode());
851 if (LD->getMemoryVT() != MVT::i32 ||
852 LD->getExtensionType() != ISD::SEXTLOAD ||
853 LD->getAddressingMode() != ISD::UNINDEXED) {
854 return SelectCode(N);
855 }
856
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000857 SDValue Chain = LD->getChain();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000858 SDValue TargetConst0 = CurDAG->getTargetConstant(0, dl, MVT::i32);
Colin LeMahieu026e88d2014-12-23 20:02:16 +0000859 OP1 = SDValue(CurDAG->getMachineNode(Hexagon::L2_loadri_io, dl, MVT::i32,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000860 MVT::Other,
861 LD->getBasePtr(), TargetConst0,
862 Chain), 0);
863 } else {
864 return SelectCode(N);
865 }
866
867 // Generate a mpy instruction.
Colin LeMahieud9b23502014-12-16 16:10:01 +0000868 SDNode *Result = CurDAG->getMachineNode(Hexagon::M2_dpmpyss_s0, dl, MVT::i64,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000869 OP0, OP1);
870 ReplaceUses(N, Result);
871 return Result;
872 }
873
874 return SelectCode(N);
875}
876
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000877SDNode *HexagonDAGToDAGISel::SelectSHL(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000878 SDLoc dl(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000879 if (N->getValueType(0) == MVT::i32) {
880 SDValue Shl_0 = N->getOperand(0);
881 SDValue Shl_1 = N->getOperand(1);
882 // RHS is const.
883 if (Shl_1.getOpcode() == ISD::Constant) {
884 if (Shl_0.getOpcode() == ISD::MUL) {
885 SDValue Mul_0 = Shl_0.getOperand(0); // Val
886 SDValue Mul_1 = Shl_0.getOperand(1); // Const
887 // RHS of mul is const.
888 if (Mul_1.getOpcode() == ISD::Constant) {
889 int32_t ShlConst =
890 cast<ConstantSDNode>(Shl_1.getNode())->getSExtValue();
891 int32_t MulConst =
892 cast<ConstantSDNode>(Mul_1.getNode())->getSExtValue();
893 int32_t ValConst = MulConst << ShlConst;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000894 SDValue Val = CurDAG->getTargetConstant(ValConst, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000895 MVT::i32);
896 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Val.getNode()))
897 if (isInt<9>(CN->getSExtValue())) {
898 SDNode* Result =
Colin LeMahieud9b23502014-12-16 16:10:01 +0000899 CurDAG->getMachineNode(Hexagon::M2_mpysmi, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000900 MVT::i32, Mul_0, Val);
901 ReplaceUses(N, Result);
902 return Result;
903 }
904
905 }
906 } else if (Shl_0.getOpcode() == ISD::SUB) {
907 SDValue Sub_0 = Shl_0.getOperand(0); // Const 0
908 SDValue Sub_1 = Shl_0.getOperand(1); // Val
909 if (Sub_0.getOpcode() == ISD::Constant) {
910 int32_t SubConst =
911 cast<ConstantSDNode>(Sub_0.getNode())->getSExtValue();
912 if (SubConst == 0) {
913 if (Sub_1.getOpcode() == ISD::SHL) {
914 SDValue Shl2_0 = Sub_1.getOperand(0); // Val
915 SDValue Shl2_1 = Sub_1.getOperand(1); // Const
916 if (Shl2_1.getOpcode() == ISD::Constant) {
917 int32_t ShlConst =
918 cast<ConstantSDNode>(Shl_1.getNode())->getSExtValue();
919 int32_t Shl2Const =
920 cast<ConstantSDNode>(Shl2_1.getNode())->getSExtValue();
921 int32_t ValConst = 1 << (ShlConst+Shl2Const);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000922 SDValue Val = CurDAG->getTargetConstant(-ValConst, dl,
923 MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000924 if (ConstantSDNode *CN =
925 dyn_cast<ConstantSDNode>(Val.getNode()))
926 if (isInt<9>(CN->getSExtValue())) {
927 SDNode* Result =
Colin LeMahieud9b23502014-12-16 16:10:01 +0000928 CurDAG->getMachineNode(Hexagon::M2_mpysmi, dl, MVT::i32,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000929 Shl2_0, Val);
930 ReplaceUses(N, Result);
931 return Result;
932 }
933 }
934 }
935 }
936 }
937 }
938 }
939 }
940 return SelectCode(N);
941}
942
943
944//
945// If there is an zero_extend followed an intrinsic in DAG (this means - the
946// result of the intrinsic is predicate); convert the zero_extend to
947// transfer instruction.
948//
949// Zero extend -> transfer is lowered here. Otherwise, zero_extend will be
950// converted into a MUX as predicate registers defined as 1 bit in the
951// compiler. Architecture defines them as 8-bit registers.
952// We want to preserve all the lower 8-bits and, not just 1 LSB bit.
953//
954SDNode *HexagonDAGToDAGISel::SelectZeroExtend(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000955 SDLoc dl(N);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000956
957 SDValue Op0 = N->getOperand(0);
958 EVT OpVT = Op0.getValueType();
959 unsigned OpBW = OpVT.getSizeInBits();
960
961 // Special handling for zero-extending a vector of booleans.
962 if (OpVT.isVector() && OpVT.getVectorElementType() == MVT::i1 && OpBW <= 64) {
963 SDNode *Mask = CurDAG->getMachineNode(Hexagon::C2_mask, dl, MVT::i64, Op0);
964 unsigned NE = OpVT.getVectorNumElements();
965 EVT ExVT = N->getValueType(0);
966 unsigned ES = ExVT.getVectorElementType().getSizeInBits();
967 uint64_t MV = 0, Bit = 1;
968 for (unsigned i = 0; i < NE; ++i) {
969 MV |= Bit;
970 Bit <<= ES;
971 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000972 SDValue Ones = CurDAG->getTargetConstant(MV, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000973 SDNode *OnesReg = CurDAG->getMachineNode(Hexagon::CONST64_Int_Real, dl,
974 MVT::i64, Ones);
975 if (ExVT.getSizeInBits() == 32) {
976 SDNode *And = CurDAG->getMachineNode(Hexagon::A2_andp, dl, MVT::i64,
977 SDValue(Mask,0), SDValue(OnesReg,0));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000978 SDValue SubR = CurDAG->getTargetConstant(Hexagon::subreg_loreg, dl,
979 MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000980 return CurDAG->getMachineNode(Hexagon::EXTRACT_SUBREG, dl, ExVT,
981 SDValue(And,0), SubR);
982 }
983 return CurDAG->getMachineNode(Hexagon::A2_andp, dl, ExVT,
984 SDValue(Mask,0), SDValue(OnesReg,0));
985 }
986
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000987 SDNode *IsIntrinsic = N->getOperand(0).getNode();
988 if ((IsIntrinsic->getOpcode() == ISD::INTRINSIC_WO_CHAIN)) {
989 unsigned ID =
990 cast<ConstantSDNode>(IsIntrinsic->getOperand(0))->getZExtValue();
991 if (doesIntrinsicReturnPredicate(ID)) {
992 // Now we need to differentiate target data types.
993 if (N->getValueType(0) == MVT::i64) {
Krzysztof Parzyszekae14e7b2015-03-17 21:47:16 +0000994 // Convert the zero_extend to Rs = Pd followed by A2_combinew(0,Rs).
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000995 SDValue TargetConst0 = CurDAG->getTargetConstant(0, dl, MVT::i32);
Colin LeMahieu30dcb232014-12-09 18:16:49 +0000996 SDNode *Result_1 = CurDAG->getMachineNode(Hexagon::C2_tfrpr, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000997 MVT::i32,
998 SDValue(IsIntrinsic, 0));
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000999 SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A2_tfrsi, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001000 MVT::i32,
1001 TargetConst0);
Colin LeMahieub580d7d2014-12-09 19:23:45 +00001002 SDNode *Result_3 = CurDAG->getMachineNode(Hexagon::A2_combinew, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001003 MVT::i64, MVT::Other,
1004 SDValue(Result_2, 0),
1005 SDValue(Result_1, 0));
1006 ReplaceUses(N, Result_3);
1007 return Result_3;
1008 }
1009 if (N->getValueType(0) == MVT::i32) {
1010 // Convert the zero_extend to Rs = Pd
Colin LeMahieu30dcb232014-12-09 18:16:49 +00001011 SDNode* RsPd = CurDAG->getMachineNode(Hexagon::C2_tfrpr, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001012 MVT::i32,
1013 SDValue(IsIntrinsic, 0));
1014 ReplaceUses(N, RsPd);
1015 return RsPd;
1016 }
Craig Toppere55c5562012-02-07 02:50:20 +00001017 llvm_unreachable("Unexpected value type");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001018 }
1019 }
1020 return SelectCode(N);
1021}
1022
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +00001023
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001024//
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +00001025// Handling intrinsics for circular load and bitreverse load.
Krzysztof Parzyszek47ab1f22015-03-18 16:23:44 +00001026//
1027SDNode *HexagonDAGToDAGISel::SelectIntrinsicWChain(SDNode *N) {
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +00001028 if (MachineSDNode *L = LoadInstrForLoadIntrinsic(N))
1029 return StoreInstrForLoadIntrinsic(L, N);
Krzysztof Parzyszek47ab1f22015-03-18 16:23:44 +00001030 return SelectCode(N);
1031}
1032
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001033SDNode *HexagonDAGToDAGISel::SelectIntrinsicWOChain(SDNode *N) {
Colin LeMahieu0ee02fc2015-01-19 20:31:18 +00001034 unsigned IID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
1035 unsigned Bits;
1036 switch (IID) {
1037 case Intrinsic::hexagon_S2_vsplatrb:
1038 Bits = 8;
1039 break;
1040 case Intrinsic::hexagon_S2_vsplatrh:
1041 Bits = 16;
1042 break;
1043 default:
1044 return SelectCode(N);
1045 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001046
Colin LeMahieu0ee02fc2015-01-19 20:31:18 +00001047 SDValue const &V = N->getOperand(1);
1048 SDValue U;
1049 if (isValueExtension(V, Bits, U)) {
1050 SDValue R = CurDAG->getNode(N->getOpcode(), SDLoc(N), N->getValueType(0),
1051 N->getOperand(0), U);
1052 return SelectCode(R.getNode());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001053 }
1054 return SelectCode(N);
1055}
1056
Sirish Pande69295b82012-05-10 20:20:25 +00001057//
1058// Map floating point constant values.
1059//
1060SDNode *HexagonDAGToDAGISel::SelectConstantFP(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001061 SDLoc dl(N);
Sirish Pande69295b82012-05-10 20:20:25 +00001062 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N);
1063 APFloat APF = CN->getValueAPF();
1064 if (N->getValueType(0) == MVT::f32) {
1065 return CurDAG->getMachineNode(Hexagon::TFRI_f, dl, MVT::f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001066 CurDAG->getTargetConstantFP(APF.convertToFloat(), dl, MVT::f32));
Sirish Pande69295b82012-05-10 20:20:25 +00001067 }
1068 else if (N->getValueType(0) == MVT::f64) {
1069 return CurDAG->getMachineNode(Hexagon::CONST64_Float_Real, dl, MVT::f64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001070 CurDAG->getTargetConstantFP(APF.convertToDouble(), dl, MVT::f64));
Sirish Pande69295b82012-05-10 20:20:25 +00001071 }
1072
1073 return SelectCode(N);
1074}
1075
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001076//
1077// Map predicate true (encoded as -1 in LLVM) to a XOR.
1078//
1079SDNode *HexagonDAGToDAGISel::SelectConstant(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001080 SDLoc dl(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001081 if (N->getValueType(0) == MVT::i1) {
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +00001082 SDNode* Result = 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001083 int32_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Krzysztof Parzyszek7a9cd802015-03-18 18:50:06 +00001084 if (Val == -1) {
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +00001085 Result = CurDAG->getMachineNode(Hexagon::TFR_PdTrue, dl, MVT::i1);
1086 } else if (Val == 0) {
1087 Result = CurDAG->getMachineNode(Hexagon::TFR_PdFalse, dl, MVT::i1);
1088 }
1089 if (Result) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001090 ReplaceUses(N, Result);
1091 return Result;
1092 }
1093 }
1094
1095 return SelectCode(N);
1096}
1097
1098
1099//
1100// Map add followed by a asr -> asr +=.
1101//
1102SDNode *HexagonDAGToDAGISel::SelectAdd(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001103 SDLoc dl(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001104 if (N->getValueType(0) != MVT::i32) {
1105 return SelectCode(N);
1106 }
1107 // Identify nodes of the form: add(asr(...)).
1108 SDNode* Src1 = N->getOperand(0).getNode();
1109 if (Src1->getOpcode() != ISD::SRA || !Src1->hasOneUse()
1110 || Src1->getValueType(0) != MVT::i32) {
1111 return SelectCode(N);
1112 }
1113
1114 // Build Rd = Rd' + asr(Rs, Rt). The machine constraints will ensure that
1115 // Rd and Rd' are assigned to the same register
Colin LeMahieu0f850bd2014-12-19 20:29:29 +00001116 SDNode* Result = CurDAG->getMachineNode(Hexagon::S2_asr_r_r_acc, dl, MVT::i32,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001117 N->getOperand(1),
1118 Src1->getOperand(0),
1119 Src1->getOperand(1));
1120 ReplaceUses(N, Result);
1121
1122 return Result;
1123}
1124
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001125//
1126// Map the following, where possible.
1127// AND/FABS -> clrbit
1128// OR -> setbit
1129// XOR/FNEG ->toggle_bit.
1130//
1131SDNode *HexagonDAGToDAGISel::SelectBitOp(SDNode *N) {
1132 SDLoc dl(N);
1133 EVT ValueVT = N->getValueType(0);
1134
1135 // We handle only 32 and 64-bit bit ops.
1136 if (!(ValueVT == MVT::i32 || ValueVT == MVT::i64 ||
1137 ValueVT == MVT::f32 || ValueVT == MVT::f64))
1138 return SelectCode(N);
1139
1140 // We handly only fabs and fneg for V5.
1141 unsigned Opc = N->getOpcode();
Eric Christopher23a7d1e2015-03-21 03:12:59 +00001142 if ((Opc == ISD::FABS || Opc == ISD::FNEG) && !HST->hasV5TOps())
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001143 return SelectCode(N);
1144
1145 int64_t Val = 0;
1146 if (Opc != ISD::FABS && Opc != ISD::FNEG) {
1147 if (N->getOperand(1).getOpcode() == ISD::Constant)
1148 Val = cast<ConstantSDNode>((N)->getOperand(1))->getSExtValue();
1149 else
1150 return SelectCode(N);
1151 }
1152
1153 if (Opc == ISD::AND) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001154 // Check if this is a bit-clearing AND, if not select code the usual way.
1155 if ((ValueVT == MVT::i32 && isPowerOf2_32(~Val)) ||
1156 (ValueVT == MVT::i64 && isPowerOf2_64(~Val)))
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001157 Val = ~Val;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001158 else
1159 return SelectCode(N);
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001160 }
1161
1162 // If OR or AND is being fed by shl, srl and, sra don't do this change,
1163 // because Hexagon provide |= &= on shl, srl, and sra.
1164 // Traverse the DAG to see if there is shl, srl and sra.
1165 if (Opc == ISD::OR || Opc == ISD::AND) {
1166 switch (N->getOperand(0)->getOpcode()) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001167 default:
1168 break;
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001169 case ISD::SRA:
1170 case ISD::SRL:
1171 case ISD::SHL:
1172 return SelectCode(N);
1173 }
1174 }
1175
1176 // Make sure it's power of 2.
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001177 unsigned BitPos = 0;
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001178 if (Opc != ISD::FABS && Opc != ISD::FNEG) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001179 if ((ValueVT == MVT::i32 && !isPowerOf2_32(Val)) ||
1180 (ValueVT == MVT::i64 && !isPowerOf2_64(Val)))
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001181 return SelectCode(N);
1182
1183 // Get the bit position.
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001184 BitPos = countTrailingZeros(uint64_t(Val));
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001185 } else {
1186 // For fabs and fneg, it's always the 31st bit.
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001187 BitPos = 31;
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001188 }
1189
1190 unsigned BitOpc = 0;
1191 // Set the right opcode for bitwise operations.
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001192 switch (Opc) {
1193 default:
1194 llvm_unreachable("Only bit-wise/abs/neg operations are allowed.");
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001195 case ISD::AND:
1196 case ISD::FABS:
1197 BitOpc = Hexagon::S2_clrbit_i;
1198 break;
1199 case ISD::OR:
1200 BitOpc = Hexagon::S2_setbit_i;
1201 break;
1202 case ISD::XOR:
1203 case ISD::FNEG:
1204 BitOpc = Hexagon::S2_togglebit_i;
1205 break;
1206 }
1207
1208 SDNode *Result;
1209 // Get the right SDVal for the opcode.
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001210 SDValue SDVal = CurDAG->getTargetConstant(BitPos, dl, MVT::i32);
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001211
1212 if (ValueVT == MVT::i32 || ValueVT == MVT::f32) {
1213 Result = CurDAG->getMachineNode(BitOpc, dl, ValueVT,
1214 N->getOperand(0), SDVal);
1215 } else {
1216 // 64-bit gymnastic to use REG_SEQUENCE. But it's worth it.
1217 EVT SubValueVT;
1218 if (ValueVT == MVT::i64)
1219 SubValueVT = MVT::i32;
1220 else
1221 SubValueVT = MVT::f32;
1222
1223 SDNode *Reg = N->getOperand(0).getNode();
1224 SDValue RegClass = CurDAG->getTargetConstant(Hexagon::DoubleRegsRegClassID,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001225 dl, MVT::i64);
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001226
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001227 SDValue SubregHiIdx = CurDAG->getTargetConstant(Hexagon::subreg_hireg, dl,
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001228 MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001229 SDValue SubregLoIdx = CurDAG->getTargetConstant(Hexagon::subreg_loreg, dl,
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001230 MVT::i32);
1231
1232 SDValue SubregHI = CurDAG->getTargetExtractSubreg(Hexagon::subreg_hireg, dl,
1233 MVT::i32, SDValue(Reg, 0));
1234
1235 SDValue SubregLO = CurDAG->getTargetExtractSubreg(Hexagon::subreg_loreg, dl,
1236 MVT::i32, SDValue(Reg, 0));
1237
1238 // Clear/set/toggle hi or lo registers depending on the bit position.
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001239 if (SubValueVT != MVT::f32 && BitPos < 32) {
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001240 SDNode *Result0 = CurDAG->getMachineNode(BitOpc, dl, SubValueVT,
1241 SubregLO, SDVal);
1242 const SDValue Ops[] = { RegClass, SubregHI, SubregHiIdx,
1243 SDValue(Result0, 0), SubregLoIdx };
1244 Result = CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE,
1245 dl, ValueVT, Ops);
1246 } else {
1247 if (Opc != ISD::FABS && Opc != ISD::FNEG)
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001248 SDVal = CurDAG->getTargetConstant(BitPos-32, dl, MVT::i32);
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001249 SDNode *Result0 = CurDAG->getMachineNode(BitOpc, dl, SubValueVT,
1250 SubregHI, SDVal);
1251 const SDValue Ops[] = { RegClass, SDValue(Result0, 0), SubregHiIdx,
1252 SubregLO, SubregLoIdx };
1253 Result = CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE,
1254 dl, ValueVT, Ops);
1255 }
1256 }
1257
1258 ReplaceUses(N, Result);
1259 return Result;
1260}
1261
1262
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001263SDNode *HexagonDAGToDAGISel::SelectFrameIndex(SDNode *N) {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001264 MachineFrameInfo *MFI = MF->getFrameInfo();
1265 const HexagonFrameLowering *HFI = HST->getFrameLowering();
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001266 int FX = cast<FrameIndexSDNode>(N)->getIndex();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001267 unsigned StkA = HFI->getStackAlignment();
1268 unsigned MaxA = MFI->getMaxAlignment();
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001269 SDValue FI = CurDAG->getTargetFrameIndex(FX, MVT::i32);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001270 SDLoc DL(N);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001271 SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001272 SDNode *R = 0;
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001273
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001274 // Use TFR_FI when:
1275 // - the object is fixed, or
1276 // - there are no objects with higher-than-default alignment, or
1277 // - there are no dynamically allocated objects.
1278 // Otherwise, use TFR_FIA.
1279 if (FX < 0 || MaxA <= StkA || !MFI->hasVarSizedObjects()) {
1280 R = CurDAG->getMachineNode(Hexagon::TFR_FI, DL, MVT::i32, FI, Zero);
1281 } else {
1282 auto &HMFI = *MF->getInfo<HexagonMachineFunctionInfo>();
1283 unsigned AR = HMFI.getStackAlignBaseVReg();
1284 SDValue CH = CurDAG->getEntryNode();
1285 SDValue Ops[] = { CurDAG->getCopyFromReg(CH, DL, AR, MVT::i32), FI, Zero };
1286 R = CurDAG->getMachineNode(Hexagon::TFR_FIA, DL, MVT::i32, Ops);
1287 }
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001288
1289 if (N->getHasDebugValue())
1290 CurDAG->TransferDbgValues(SDValue(N, 0), SDValue(R, 0));
1291 return R;
1292}
1293
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001294
Justin Bognerb0126992016-05-05 23:19:08 +00001295SDNode *HexagonDAGToDAGISel::SelectImpl(SDNode *N) {
Tim Northover31d093c2013-09-22 08:21:56 +00001296 if (N->isMachineOpcode()) {
1297 N->setNodeId(-1);
Craig Topper062a2ba2014-04-25 05:30:21 +00001298 return nullptr; // Already selected.
Tim Northover31d093c2013-09-22 08:21:56 +00001299 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001300
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001301 switch (N->getOpcode()) {
1302 case ISD::Constant:
1303 return SelectConstant(N);
1304
Sirish Pande69295b82012-05-10 20:20:25 +00001305 case ISD::ConstantFP:
1306 return SelectConstantFP(N);
1307
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001308 case ISD::FrameIndex:
1309 return SelectFrameIndex(N);
1310
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001311 case ISD::ADD:
1312 return SelectAdd(N);
1313
1314 case ISD::SHL:
1315 return SelectSHL(N);
1316
1317 case ISD::LOAD:
1318 return SelectLoad(N);
1319
1320 case ISD::STORE:
1321 return SelectStore(N);
1322
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001323 case ISD::MUL:
1324 return SelectMul(N);
1325
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001326 case ISD::AND:
1327 case ISD::OR:
1328 case ISD::XOR:
1329 case ISD::FABS:
1330 case ISD::FNEG:
1331 return SelectBitOp(N);
1332
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001333 case ISD::ZERO_EXTEND:
1334 return SelectZeroExtend(N);
1335
Krzysztof Parzyszek47ab1f22015-03-18 16:23:44 +00001336 case ISD::INTRINSIC_W_CHAIN:
1337 return SelectIntrinsicWChain(N);
1338
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001339 case ISD::INTRINSIC_WO_CHAIN:
1340 return SelectIntrinsicWOChain(N);
1341 }
1342
1343 return SelectCode(N);
1344}
1345
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001346bool HexagonDAGToDAGISel::
Daniel Sanders60f1db02015-03-13 12:45:09 +00001347SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001348 std::vector<SDValue> &OutOps) {
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001349 SDValue Inp = Op, Res;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001350
Daniel Sanders60f1db02015-03-13 12:45:09 +00001351 switch (ConstraintID) {
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001352 default:
1353 return true;
Daniel Sanders49f643c2015-03-17 14:37:39 +00001354 case InlineAsm::Constraint_i:
1355 case InlineAsm::Constraint_o: // Offsetable.
1356 case InlineAsm::Constraint_v: // Not offsetable.
1357 case InlineAsm::Constraint_m: // Memory.
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001358 if (SelectAddrFI(Inp, Res))
1359 OutOps.push_back(Res);
1360 else
1361 OutOps.push_back(Inp);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001362 break;
1363 }
1364
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001365 OutOps.push_back(CurDAG->getTargetConstant(0, SDLoc(Op), MVT::i32));
Jyotsna Vermad9225242013-02-13 21:38:46 +00001366 return false;
1367}
Colin LeMahieuc7522f32015-01-14 23:07:36 +00001368
Colin LeMahieu79ec0652015-06-12 19:57:32 +00001369
Krzysztof Parzyszekae14e7b2015-03-17 21:47:16 +00001370void HexagonDAGToDAGISel::PreprocessISelDAG() {
1371 SelectionDAG &DAG = *CurDAG;
1372 std::vector<SDNode*> Nodes;
Pete Cooper7e64ef02015-07-14 23:43:29 +00001373 for (SDNode &Node : DAG.allnodes())
1374 Nodes.push_back(&Node);
Krzysztof Parzyszekae14e7b2015-03-17 21:47:16 +00001375
1376 // Simplify: (or (select c x 0) z) -> (select c (or x z) z)
1377 // (or (select c 0 y) z) -> (select c z (or y z))
1378 // This may not be the right thing for all targets, so do it here.
1379 for (auto I: Nodes) {
1380 if (I->getOpcode() != ISD::OR)
1381 continue;
1382
1383 auto IsZero = [] (const SDValue &V) -> bool {
1384 if (ConstantSDNode *SC = dyn_cast<ConstantSDNode>(V.getNode()))
1385 return SC->isNullValue();
1386 return false;
1387 };
1388 auto IsSelect0 = [IsZero] (const SDValue &Op) -> bool {
1389 if (Op.getOpcode() != ISD::SELECT)
1390 return false;
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +00001391 return IsZero(Op.getOperand(1)) || IsZero(Op.getOperand(2));
Krzysztof Parzyszekae14e7b2015-03-17 21:47:16 +00001392 };
1393
1394 SDValue N0 = I->getOperand(0), N1 = I->getOperand(1);
1395 EVT VT = I->getValueType(0);
1396 bool SelN0 = IsSelect0(N0);
1397 SDValue SOp = SelN0 ? N0 : N1;
1398 SDValue VOp = SelN0 ? N1 : N0;
1399
1400 if (SOp.getOpcode() == ISD::SELECT && SOp.getNode()->hasOneUse()) {
1401 SDValue SC = SOp.getOperand(0);
1402 SDValue SX = SOp.getOperand(1);
1403 SDValue SY = SOp.getOperand(2);
1404 SDLoc DLS = SOp;
1405 if (IsZero(SY)) {
1406 SDValue NewOr = DAG.getNode(ISD::OR, DLS, VT, SX, VOp);
1407 SDValue NewSel = DAG.getNode(ISD::SELECT, DLS, VT, SC, NewOr, VOp);
1408 DAG.ReplaceAllUsesWith(I, NewSel.getNode());
1409 } else if (IsZero(SX)) {
1410 SDValue NewOr = DAG.getNode(ISD::OR, DLS, VT, SY, VOp);
1411 SDValue NewSel = DAG.getNode(ISD::SELECT, DLS, VT, SC, VOp, NewOr);
1412 DAG.ReplaceAllUsesWith(I, NewSel.getNode());
1413 }
1414 }
1415 }
1416}
1417
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001418void HexagonDAGToDAGISel::EmitFunctionEntryCode() {
1419 auto &HST = static_cast<const HexagonSubtarget&>(MF->getSubtarget());
1420 auto &HFI = *HST.getFrameLowering();
1421 if (!HFI.needsAligna(*MF))
1422 return;
Krzysztof Parzyszekae14e7b2015-03-17 21:47:16 +00001423
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001424 MachineFrameInfo *MFI = MF->getFrameInfo();
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +00001425 MachineBasicBlock *EntryBB = &MF->front();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001426 unsigned AR = FuncInfo->CreateReg(MVT::i32);
1427 unsigned MaxA = MFI->getMaxAlignment();
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001428 BuildMI(EntryBB, DebugLoc(), HII->get(Hexagon::ALIGNA), AR)
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001429 .addImm(MaxA);
1430 MF->getInfo<HexagonMachineFunctionInfo>()->setStackAlignBaseVReg(AR);
1431}
1432
1433// Match a frame index that can be used in an addressing mode.
Colin LeMahieuc7522f32015-01-14 23:07:36 +00001434bool HexagonDAGToDAGISel::SelectAddrFI(SDValue& N, SDValue &R) {
1435 if (N.getOpcode() != ISD::FrameIndex)
1436 return false;
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001437 auto &HFI = *HST->getFrameLowering();
1438 MachineFrameInfo *MFI = MF->getFrameInfo();
1439 int FX = cast<FrameIndexSDNode>(N)->getIndex();
1440 if (!MFI->isFixedObjectIndex(FX) && HFI.needsAligna(*MF))
1441 return false;
1442 R = CurDAG->getTargetFrameIndex(FX, MVT::i32);
Colin LeMahieuc7522f32015-01-14 23:07:36 +00001443 return true;
1444}
Colin LeMahieu0ee02fc2015-01-19 20:31:18 +00001445
Colin LeMahieu987b0942015-02-04 20:38:01 +00001446inline bool HexagonDAGToDAGISel::SelectAddrGA(SDValue &N, SDValue &R) {
1447 return SelectGlobalAddress(N, R, false);
1448}
1449
Colin LeMahieu51491352015-02-04 22:36:28 +00001450inline bool HexagonDAGToDAGISel::SelectAddrGP(SDValue &N, SDValue &R) {
1451 return SelectGlobalAddress(N, R, true);
1452}
1453
Colin LeMahieu987b0942015-02-04 20:38:01 +00001454bool HexagonDAGToDAGISel::SelectGlobalAddress(SDValue &N, SDValue &R,
1455 bool UseGP) {
1456 switch (N.getOpcode()) {
1457 case ISD::ADD: {
1458 SDValue N0 = N.getOperand(0);
1459 SDValue N1 = N.getOperand(1);
1460 unsigned GAOpc = N0.getOpcode();
1461 if (UseGP && GAOpc != HexagonISD::CONST32_GP)
1462 return false;
1463 if (!UseGP && GAOpc != HexagonISD::CONST32)
1464 return false;
1465 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N1)) {
1466 SDValue Addr = N0.getOperand(0);
1467 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Addr)) {
1468 if (GA->getOpcode() == ISD::TargetGlobalAddress) {
1469 uint64_t NewOff = GA->getOffset() + (uint64_t)Const->getSExtValue();
1470 R = CurDAG->getTargetGlobalAddress(GA->getGlobal(), SDLoc(Const),
1471 N.getValueType(), NewOff);
1472 return true;
1473 }
1474 }
1475 }
1476 break;
1477 }
1478 case HexagonISD::CONST32:
1479 // The operand(0) of CONST32 is TargetGlobalAddress, which is what we
1480 // want in the instruction.
1481 if (!UseGP)
1482 R = N.getOperand(0);
1483 return !UseGP;
1484 case HexagonISD::CONST32_GP:
1485 if (UseGP)
1486 R = N.getOperand(0);
1487 return UseGP;
1488 default:
1489 return false;
1490 }
1491
1492 return false;
1493}
1494
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001495bool HexagonDAGToDAGISel::isValueExtension(const SDValue &Val,
1496 unsigned FromBits, SDValue &Src) {
Colin LeMahieu0ee02fc2015-01-19 20:31:18 +00001497 unsigned Opc = Val.getOpcode();
1498 switch (Opc) {
1499 case ISD::SIGN_EXTEND:
1500 case ISD::ZERO_EXTEND:
1501 case ISD::ANY_EXTEND: {
1502 SDValue const &Op0 = Val.getOperand(0);
1503 EVT T = Op0.getValueType();
1504 if (T.isInteger() && T.getSizeInBits() == FromBits) {
1505 Src = Op0;
1506 return true;
1507 }
1508 break;
1509 }
1510 case ISD::SIGN_EXTEND_INREG:
1511 case ISD::AssertSext:
1512 case ISD::AssertZext:
1513 if (Val.getOperand(0).getValueType().isInteger()) {
1514 VTSDNode *T = cast<VTSDNode>(Val.getOperand(1));
1515 if (T->getVT().getSizeInBits() == FromBits) {
1516 Src = Val.getOperand(0);
1517 return true;
1518 }
1519 }
1520 break;
1521 case ISD::AND: {
1522 // Check if this is an AND with "FromBits" of lower bits set to 1.
1523 uint64_t FromMask = (1 << FromBits) - 1;
1524 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(0))) {
1525 if (C->getZExtValue() == FromMask) {
1526 Src = Val.getOperand(1);
1527 return true;
1528 }
1529 }
1530 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(1))) {
1531 if (C->getZExtValue() == FromMask) {
1532 Src = Val.getOperand(0);
1533 return true;
1534 }
1535 }
1536 break;
1537 }
1538 case ISD::OR:
1539 case ISD::XOR: {
1540 // OR/XOR with the lower "FromBits" bits set to 0.
1541 uint64_t FromMask = (1 << FromBits) - 1;
1542 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(0))) {
1543 if ((C->getZExtValue() & FromMask) == 0) {
1544 Src = Val.getOperand(1);
1545 return true;
1546 }
1547 }
1548 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(1))) {
1549 if ((C->getZExtValue() & FromMask) == 0) {
1550 Src = Val.getOperand(0);
1551 return true;
1552 }
1553 }
1554 }
1555 default:
1556 break;
1557 }
1558 return false;
1559}
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +00001560
1561bool HexagonDAGToDAGISel::isAlignedMemNode(const MemSDNode *N) const {
1562 return N->getAlignment() >= N->getMemoryVT().getStoreSize();
1563}