blob: ddddb48e8f13a2d102943abc9777e91fc7302fc1 [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- HexagonISelDAGToDAG.cpp - A dag to dag inst selector for Hexagon --===//
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines an instruction selector for the Hexagon target.
11//
12//===----------------------------------------------------------------------===//
13
Chandler Carruthed0881b2012-12-03 16:50:05 +000014#include "Hexagon.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000015#include "HexagonISelLowering.h"
16#include "HexagonTargetMachine.h"
Jyotsna Vermad9225242013-02-13 21:38:46 +000017#include "llvm/ADT/DenseMap.h"
Jyotsna Vermad9225242013-02-13 21:38:46 +000018#include "llvm/CodeGen/SelectionDAGISel.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000019#include "llvm/IR/Intrinsics.h"
Jyotsna Vermad9225242013-02-13 21:38:46 +000020#include "llvm/Support/CommandLine.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000021#include "llvm/Support/Compiler.h"
22#include "llvm/Support/Debug.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000023using namespace llvm;
24
Chandler Carruth84e68b22014-04-22 02:41:26 +000025#define DEBUG_TYPE "hexagon-isel"
26
Jyotsna Vermad9225242013-02-13 21:38:46 +000027static
28cl::opt<unsigned>
29MaxNumOfUsesForConstExtenders("ga-max-num-uses-for-constant-extenders",
30 cl::Hidden, cl::init(2),
31 cl::desc("Maximum number of uses of a global address such that we still us a"
32 "constant extended instruction"));
Tony Linthicum1213a7a2011-12-12 21:14:40 +000033
34//===----------------------------------------------------------------------===//
35// Instruction Selector Implementation
36//===----------------------------------------------------------------------===//
37
Jyotsna Vermad9225242013-02-13 21:38:46 +000038namespace llvm {
39 void initializeHexagonDAGToDAGISelPass(PassRegistry&);
40}
41
Tony Linthicum1213a7a2011-12-12 21:14:40 +000042//===--------------------------------------------------------------------===//
43/// HexagonDAGToDAGISel - Hexagon specific code to select Hexagon machine
44/// instructions for SelectionDAG operations.
45///
46namespace {
47class HexagonDAGToDAGISel : public SelectionDAGISel {
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +000048 const HexagonTargetMachine& HTM;
49 const HexagonSubtarget &HST;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000050public:
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +000051 explicit HexagonDAGToDAGISel(HexagonTargetMachine &tm,
Jyotsna Vermad9225242013-02-13 21:38:46 +000052 CodeGenOpt::Level OptLevel)
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +000053 : SelectionDAGISel(tm, OptLevel), HTM(tm),
54 HST(tm.getSubtarget<HexagonSubtarget>()) {
Jyotsna Vermad9225242013-02-13 21:38:46 +000055 initializeHexagonDAGToDAGISelPass(*PassRegistry::getPassRegistry());
Tony Linthicum1213a7a2011-12-12 21:14:40 +000056 }
Krzysztof Parzyszekae14e7b2015-03-17 21:47:16 +000057 virtual void PreprocessISelDAG() override;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000058
Craig Topper906c2cd2014-04-29 07:58:16 +000059 SDNode *Select(SDNode *N) override;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000060
61 // Complex Pattern Selectors.
Colin LeMahieu987b0942015-02-04 20:38:01 +000062 inline bool SelectAddrGA(SDValue &N, SDValue &R);
Colin LeMahieu51491352015-02-04 22:36:28 +000063 inline bool SelectAddrGP(SDValue &N, SDValue &R);
Colin LeMahieu987b0942015-02-04 20:38:01 +000064 bool SelectGlobalAddress(SDValue &N, SDValue &R, bool UseGP);
Colin LeMahieuc7522f32015-01-14 23:07:36 +000065 bool SelectAddrFI(SDValue &N, SDValue &R);
66
Craig Topper906c2cd2014-04-29 07:58:16 +000067 const char *getPassName() const override {
Tony Linthicum1213a7a2011-12-12 21:14:40 +000068 return "Hexagon DAG->DAG Pattern Instruction Selection";
69 }
70
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +000071 SDNode *SelectFrameIndex(SDNode *N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +000072 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
73 /// inline asm expressions.
Craig Topper906c2cd2014-04-29 07:58:16 +000074 bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Daniel Sanders60f1db02015-03-13 12:45:09 +000075 unsigned ConstraintID,
Craig Topper906c2cd2014-04-29 07:58:16 +000076 std::vector<SDValue> &OutOps) override;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000077 SDNode *SelectLoad(SDNode *N);
Andrew Trickef9de2a2013-05-25 02:42:55 +000078 SDNode *SelectBaseOffsetLoad(LoadSDNode *LD, SDLoc dl);
79 SDNode *SelectIndexedLoad(LoadSDNode *LD, SDLoc dl);
Tony Linthicum1213a7a2011-12-12 21:14:40 +000080 SDNode *SelectIndexedLoadZeroExtend64(LoadSDNode *LD, unsigned Opcode,
Andrew Trickef9de2a2013-05-25 02:42:55 +000081 SDLoc dl);
Tony Linthicum1213a7a2011-12-12 21:14:40 +000082 SDNode *SelectIndexedLoadSignExtend64(LoadSDNode *LD, unsigned Opcode,
Andrew Trickef9de2a2013-05-25 02:42:55 +000083 SDLoc dl);
84 SDNode *SelectBaseOffsetStore(StoreSDNode *ST, SDLoc dl);
85 SDNode *SelectIndexedStore(StoreSDNode *ST, SDLoc dl);
Tony Linthicum1213a7a2011-12-12 21:14:40 +000086 SDNode *SelectStore(SDNode *N);
87 SDNode *SelectSHL(SDNode *N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +000088 SDNode *SelectMul(SDNode *N);
89 SDNode *SelectZeroExtend(SDNode *N);
Sirish Pande69295b82012-05-10 20:20:25 +000090 SDNode *SelectIntrinsicWChain(SDNode *N);
Krzysztof Parzyszekae14e7b2015-03-17 21:47:16 +000091 SDNode *SelectIntrinsicWOChain(SDNode *N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +000092 SDNode *SelectConstant(SDNode *N);
Sirish Pande69295b82012-05-10 20:20:25 +000093 SDNode *SelectConstantFP(SDNode *N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +000094 SDNode *SelectAdd(SDNode *N);
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +000095 SDNode *SelectBitOp(SDNode *N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +000096
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +000097 // XformMskToBitPosU5Imm - Returns the bit position which
98 // the single bit 32 bit mask represents.
99 // Used in Clr and Set bit immediate memops.
100 SDValue XformMskToBitPosU5Imm(uint32_t Imm) {
101 int32_t bitPos;
102 bitPos = Log2_32(Imm);
103 assert(bitPos >= 0 && bitPos < 32 &&
104 "Constant out of range for 32 BitPos Memops");
105 return CurDAG->getTargetConstant(bitPos, MVT::i32);
106 }
Jyotsna Vermafdc660b2013-03-22 18:41:34 +0000107
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000108 // XformMskToBitPosU4Imm - Returns the bit position which the single-bit
109 // 16 bit mask represents. Used in Clr and Set bit immediate memops.
110 SDValue XformMskToBitPosU4Imm(uint16_t Imm) {
111 return XformMskToBitPosU5Imm(Imm);
112 }
Jyotsna Vermafdc660b2013-03-22 18:41:34 +0000113
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000114 // XformMskToBitPosU3Imm - Returns the bit position which the single-bit
115 // 8 bit mask represents. Used in Clr and Set bit immediate memops.
116 SDValue XformMskToBitPosU3Imm(uint8_t Imm) {
117 return XformMskToBitPosU5Imm(Imm);
118 }
Jyotsna Vermafdc660b2013-03-22 18:41:34 +0000119
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000120 // Return true if there is exactly one bit set in V, i.e., if V is one of the
121 // following integers: 2^0, 2^1, ..., 2^31.
122 bool ImmIsSingleBit(uint32_t v) const {
123 return isPowerOf2_32(v);
124 }
Jyotsna Vermafdc660b2013-03-22 18:41:34 +0000125
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000126 // XformM5ToU5Imm - Return a target constant with the specified value, of
127 // type i32 where the negative literal is transformed into a positive literal
128 // for use in -= memops.
129 inline SDValue XformM5ToU5Imm(signed Imm) {
130 assert( (Imm >= -31 && Imm <= -1) && "Constant out of range for Memops");
131 return CurDAG->getTargetConstant( - Imm, MVT::i32);
132 }
Jyotsna Vermafdc660b2013-03-22 18:41:34 +0000133
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000134 // XformU7ToU7M1Imm - Return a target constant decremented by 1, in range
135 // [1..128], used in cmpb.gtu instructions.
136 inline SDValue XformU7ToU7M1Imm(signed Imm) {
137 assert((Imm >= 1 && Imm <= 128) && "Constant out of range for cmpb op");
138 return CurDAG->getTargetConstant(Imm - 1, MVT::i8);
139 }
Jyotsna Vermafdc660b2013-03-22 18:41:34 +0000140
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000141 // XformS8ToS8M1Imm - Return a target constant decremented by 1.
142 inline SDValue XformSToSM1Imm(signed Imm) {
143 return CurDAG->getTargetConstant(Imm - 1, MVT::i32);
144 }
Jyotsna Verma60316252013-02-05 19:20:45 +0000145
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000146 // XformU8ToU8M1Imm - Return a target constant decremented by 1.
147 inline SDValue XformUToUM1Imm(unsigned Imm) {
148 assert((Imm >= 1) && "Cannot decrement unsigned int less than 1");
149 return CurDAG->getTargetConstant(Imm - 1, MVT::i32);
150 }
Jyotsna Verma89c84822013-04-23 19:15:55 +0000151
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000152 // XformSToSM2Imm - Return a target constant decremented by 2.
153 inline SDValue XformSToSM2Imm(unsigned Imm) {
154 return CurDAG->getTargetConstant(Imm - 2, MVT::i32);
155 }
Jyotsna Verma89c84822013-04-23 19:15:55 +0000156
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000157 // XformSToSM3Imm - Return a target constant decremented by 3.
158 inline SDValue XformSToSM3Imm(unsigned Imm) {
159 return CurDAG->getTargetConstant(Imm - 3, MVT::i32);
160 }
Colin LeMahieu19ed07c2015-01-28 18:29:11 +0000161
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000162 // Include the pieces autogenerated from the target description.
163 #include "HexagonGenDAGISel.inc"
Colin LeMahieu0ee02fc2015-01-19 20:31:18 +0000164
165private:
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000166 bool isValueExtension(const SDValue &Val, unsigned FromBits, SDValue &Src);
167}; // end HexagonDAGToDAGISel
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000168} // end anonymous namespace
169
170
171/// createHexagonISelDag - This pass converts a legalized DAG into a
172/// Hexagon-specific DAG, ready for instruction scheduling.
173///
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000174namespace llvm {
175FunctionPass *createHexagonISelDag(HexagonTargetMachine &TM,
176 CodeGenOpt::Level OptLevel) {
Jyotsna Vermad9225242013-02-13 21:38:46 +0000177 return new HexagonDAGToDAGISel(TM, OptLevel);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000178}
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000179}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000180
Jyotsna Vermad9225242013-02-13 21:38:46 +0000181static void initializePassOnce(PassRegistry &Registry) {
182 const char *Name = "Hexagon DAG->DAG Pattern Instruction Selection";
183 PassInfo *PI = new PassInfo(Name, "hexagon-isel",
Craig Topper062a2ba2014-04-25 05:30:21 +0000184 &SelectionDAGISel::ID, nullptr, false, false);
Jyotsna Vermad9225242013-02-13 21:38:46 +0000185 Registry.registerPass(*PI, true);
186}
187
188void llvm::initializeHexagonDAGToDAGISelPass(PassRegistry &Registry) {
189 CALL_ONCE_INITIALIZATION(initializePassOnce)
190}
191
192
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000193// Intrinsics that return a a predicate.
194static unsigned doesIntrinsicReturnPredicate(unsigned ID)
195{
196 switch (ID) {
197 default:
198 return 0;
199 case Intrinsic::hexagon_C2_cmpeq:
200 case Intrinsic::hexagon_C2_cmpgt:
201 case Intrinsic::hexagon_C2_cmpgtu:
202 case Intrinsic::hexagon_C2_cmpgtup:
203 case Intrinsic::hexagon_C2_cmpgtp:
204 case Intrinsic::hexagon_C2_cmpeqp:
205 case Intrinsic::hexagon_C2_bitsset:
206 case Intrinsic::hexagon_C2_bitsclr:
207 case Intrinsic::hexagon_C2_cmpeqi:
208 case Intrinsic::hexagon_C2_cmpgti:
209 case Intrinsic::hexagon_C2_cmpgtui:
210 case Intrinsic::hexagon_C2_cmpgei:
211 case Intrinsic::hexagon_C2_cmpgeui:
212 case Intrinsic::hexagon_C2_cmplt:
213 case Intrinsic::hexagon_C2_cmpltu:
214 case Intrinsic::hexagon_C2_bitsclri:
215 case Intrinsic::hexagon_C2_and:
216 case Intrinsic::hexagon_C2_or:
217 case Intrinsic::hexagon_C2_xor:
218 case Intrinsic::hexagon_C2_andn:
219 case Intrinsic::hexagon_C2_not:
220 case Intrinsic::hexagon_C2_orn:
221 case Intrinsic::hexagon_C2_pxfer_map:
222 case Intrinsic::hexagon_C2_any8:
223 case Intrinsic::hexagon_C2_all8:
224 case Intrinsic::hexagon_A2_vcmpbeq:
225 case Intrinsic::hexagon_A2_vcmpbgtu:
226 case Intrinsic::hexagon_A2_vcmpheq:
227 case Intrinsic::hexagon_A2_vcmphgt:
228 case Intrinsic::hexagon_A2_vcmphgtu:
229 case Intrinsic::hexagon_A2_vcmpweq:
230 case Intrinsic::hexagon_A2_vcmpwgt:
231 case Intrinsic::hexagon_A2_vcmpwgtu:
232 case Intrinsic::hexagon_C2_tfrrp:
233 case Intrinsic::hexagon_S2_tstbit_i:
234 case Intrinsic::hexagon_S2_tstbit_r:
235 return 1;
236 }
237}
238
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000239SDNode *HexagonDAGToDAGISel::SelectIndexedLoadSignExtend64(LoadSDNode *LD,
240 unsigned Opcode,
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000241 SDLoc dl) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000242 SDValue Chain = LD->getChain();
243 EVT LoadedVT = LD->getMemoryVT();
244 SDValue Base = LD->getBasePtr();
245 SDValue Offset = LD->getOffset();
246 SDNode *OffsetNode = Offset.getNode();
247 int32_t Val = cast<ConstantSDNode>(OffsetNode)->getSExtValue();
Bill Wendling4a7a4082013-06-07 06:19:56 +0000248
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000249 const HexagonInstrInfo &TII = *HST.getInstrInfo();
250 if (TII.isValidAutoIncImm(LoadedVT, Val)) {
251 SDValue TargetConst = CurDAG->getTargetConstant(Val, MVT::i32);
252 SDNode *Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::i32, MVT::i32,
253 MVT::Other, Base, TargetConst,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000254 Chain);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000255 SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A2_sxtw, dl, MVT::i64,
256 SDValue(Result_1, 0));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000257 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
258 MemOp[0] = LD->getMemOperand();
259 cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
260 const SDValue Froms[] = { SDValue(LD, 0),
261 SDValue(LD, 1),
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000262 SDValue(LD, 2) };
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000263 const SDValue Tos[] = { SDValue(Result_2, 0),
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000264 SDValue(Result_1, 1),
265 SDValue(Result_1, 2) };
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000266 ReplaceUses(Froms, Tos, 3);
267 return Result_2;
268 }
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000269
270 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
271 SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
272 SDNode *Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::i32, MVT::Other,
273 Base, TargetConst0, Chain);
274 SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A2_sxtw, dl, MVT::i64,
275 SDValue(Result_1, 0));
276 SDNode* Result_3 = CurDAG->getMachineNode(Hexagon::A2_addi, dl, MVT::i32,
277 Base, TargetConstVal,
278 SDValue(Result_1, 1));
279 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),
284 SDValue(LD, 2) };
285 const SDValue Tos[] = { SDValue(Result_2, 0),
286 SDValue(Result_3, 0),
287 SDValue(Result_1, 1) };
288 ReplaceUses(Froms, Tos, 3);
289 return Result_2;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000290}
291
292
293SDNode *HexagonDAGToDAGISel::SelectIndexedLoadZeroExtend64(LoadSDNode *LD,
294 unsigned Opcode,
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000295 SDLoc dl) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000296 SDValue Chain = LD->getChain();
297 EVT LoadedVT = LD->getMemoryVT();
298 SDValue Base = LD->getBasePtr();
299 SDValue Offset = LD->getOffset();
300 SDNode *OffsetNode = Offset.getNode();
301 int32_t Val = cast<ConstantSDNode>(OffsetNode)->getSExtValue();
Bill Wendling4a7a4082013-06-07 06:19:56 +0000302
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000303 const HexagonInstrInfo &TII = *HST.getInstrInfo();
304 if (TII.isValidAutoIncImm(LoadedVT, Val)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000305 SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000306 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000307 SDNode *Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::i32,
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000308 MVT::i32, MVT::Other, Base,
309 TargetConstVal, Chain);
310 SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A4_combineir, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000311 MVT::i64, MVT::Other,
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000312 TargetConst0,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000313 SDValue(Result_1,0));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000314 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
315 MemOp[0] = LD->getMemOperand();
316 cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
317 const SDValue Froms[] = { SDValue(LD, 0),
318 SDValue(LD, 1),
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000319 SDValue(LD, 2) };
320 const SDValue Tos[] = { SDValue(Result_2, 0),
321 SDValue(Result_1, 1),
322 SDValue(Result_1, 2) };
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000323 ReplaceUses(Froms, Tos, 3);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000324 return Result_2;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000325 }
326
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000327 // Generate an indirect load.
328 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
329 SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
330 SDNode *Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::i32,
331 MVT::Other, Base, TargetConst0,
332 Chain);
333 SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A4_combineir, dl,
334 MVT::i64, MVT::Other,
335 TargetConst0,
336 SDValue(Result_1,0));
337 // Add offset to base.
338 SDNode* Result_3 = CurDAG->getMachineNode(Hexagon::A2_addi, dl, MVT::i32,
339 Base, TargetConstVal,
340 SDValue(Result_1, 1));
341 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
342 MemOp[0] = LD->getMemOperand();
343 cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
344 const SDValue Froms[] = { SDValue(LD, 0),
345 SDValue(LD, 1),
346 SDValue(LD, 2) };
347 const SDValue Tos[] = { SDValue(Result_2, 0), // Load value.
348 SDValue(Result_3, 0), // New address.
349 SDValue(Result_1, 1) };
350 ReplaceUses(Froms, Tos, 3);
351 return Result_2;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000352}
353
354
Andrew Trickef9de2a2013-05-25 02:42:55 +0000355SDNode *HexagonDAGToDAGISel::SelectIndexedLoad(LoadSDNode *LD, SDLoc dl) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000356 SDValue Chain = LD->getChain();
357 SDValue Base = LD->getBasePtr();
358 SDValue Offset = LD->getOffset();
359 SDNode *OffsetNode = Offset.getNode();
360 // Get the constant value.
361 int32_t Val = cast<ConstantSDNode>(OffsetNode)->getSExtValue();
362 EVT LoadedVT = LD->getMemoryVT();
363 unsigned Opcode = 0;
364
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000365 // Check for zero extended loads. Treat any-extend loads as zero extended
366 // loads.
367 ISD::LoadExtType ExtType = LD->getExtensionType();
368 bool IsZeroExt = (ExtType == ISD::ZEXTLOAD || ExtType == ISD::EXTLOAD);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000369
370 // Figure out the opcode.
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000371 const HexagonInstrInfo &TII = *HST.getInstrInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000372 if (LoadedVT == MVT::i64) {
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000373 if (TII.isValidAutoIncImm(LoadedVT, Val))
Colin LeMahieuc83cbbf2014-12-26 19:31:46 +0000374 Opcode = Hexagon::L2_loadrd_pi;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000375 else
Colin LeMahieu947cd702014-12-23 20:44:59 +0000376 Opcode = Hexagon::L2_loadrd_io;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000377 } else if (LoadedVT == MVT::i32) {
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000378 if (TII.isValidAutoIncImm(LoadedVT, Val))
Colin LeMahieuc83cbbf2014-12-26 19:31:46 +0000379 Opcode = Hexagon::L2_loadri_pi;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000380 else
Colin LeMahieu026e88d2014-12-23 20:02:16 +0000381 Opcode = Hexagon::L2_loadri_io;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000382 } else if (LoadedVT == MVT::i16) {
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000383 if (TII.isValidAutoIncImm(LoadedVT, Val))
384 Opcode = IsZeroExt ? Hexagon::L2_loadruh_pi : Hexagon::L2_loadrh_pi;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000385 else
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000386 Opcode = IsZeroExt ? Hexagon::L2_loadruh_io : Hexagon::L2_loadrh_io;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000387 } else if (LoadedVT == MVT::i8) {
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000388 if (TII.isValidAutoIncImm(LoadedVT, Val))
389 Opcode = IsZeroExt ? Hexagon::L2_loadrub_pi : Hexagon::L2_loadrb_pi;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000390 else
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000391 Opcode = IsZeroExt ? Hexagon::L2_loadrub_io : Hexagon::L2_loadrb_io;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000392 } else
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000393 llvm_unreachable("unknown memory type");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000394
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000395 // For zero extended i64 loads, we need to add combine instructions.
396 if (LD->getValueType(0) == MVT::i64 && IsZeroExt)
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000397 return SelectIndexedLoadZeroExtend64(LD, Opcode, dl);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000398 // Handle sign extended i64 loads.
399 if (LD->getValueType(0) == MVT::i64 && ExtType == ISD::SEXTLOAD)
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000400 return SelectIndexedLoadSignExtend64(LD, Opcode, dl);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000401
402 if (TII.isValidAutoIncImm(LoadedVT, Val)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000403 SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
404 SDNode* Result = CurDAG->getMachineNode(Opcode, dl,
405 LD->getValueType(0),
406 MVT::i32, MVT::Other, Base,
407 TargetConstVal, Chain);
408 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
409 MemOp[0] = LD->getMemOperand();
410 cast<MachineSDNode>(Result)->setMemRefs(MemOp, MemOp + 1);
411 const SDValue Froms[] = { SDValue(LD, 0),
412 SDValue(LD, 1),
413 SDValue(LD, 2)
414 };
415 const SDValue Tos[] = { SDValue(Result, 0),
416 SDValue(Result, 1),
417 SDValue(Result, 2)
418 };
419 ReplaceUses(Froms, Tos, 3);
420 return Result;
421 } else {
422 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
423 SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
424 SDNode* Result_1 = CurDAG->getMachineNode(Opcode, dl,
425 LD->getValueType(0),
426 MVT::Other, Base, TargetConst0,
427 Chain);
Colin LeMahieuf297dbe2015-02-05 17:49:13 +0000428 SDNode* Result_2 = CurDAG->getMachineNode(Hexagon::A2_addi, dl, MVT::i32,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000429 Base, TargetConstVal,
430 SDValue(Result_1, 1));
431 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
432 MemOp[0] = LD->getMemOperand();
433 cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
434 const SDValue Froms[] = { SDValue(LD, 0),
435 SDValue(LD, 1),
436 SDValue(LD, 2)
437 };
438 const SDValue Tos[] = { SDValue(Result_1, 0),
439 SDValue(Result_2, 0),
440 SDValue(Result_1, 1)
441 };
442 ReplaceUses(Froms, Tos, 3);
443 return Result_1;
444 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000445}
446
447
448SDNode *HexagonDAGToDAGISel::SelectLoad(SDNode *N) {
449 SDNode *result;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000450 SDLoc dl(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000451 LoadSDNode *LD = cast<LoadSDNode>(N);
452 ISD::MemIndexedMode AM = LD->getAddressingMode();
453
454 // Handle indexed loads.
455 if (AM != ISD::UNINDEXED) {
456 result = SelectIndexedLoad(LD, dl);
457 } else {
Colin LeMahieu2efa2d02015-03-09 21:48:13 +0000458 result = SelectCode(LD);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000459 }
460
461 return result;
462}
463
464
Andrew Trickef9de2a2013-05-25 02:42:55 +0000465SDNode *HexagonDAGToDAGISel::SelectIndexedStore(StoreSDNode *ST, SDLoc dl) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000466 SDValue Chain = ST->getChain();
467 SDValue Base = ST->getBasePtr();
468 SDValue Offset = ST->getOffset();
469 SDValue Value = ST->getValue();
470 SDNode *OffsetNode = Offset.getNode();
471 // Get the constant value.
472 int32_t Val = cast<ConstantSDNode>(OffsetNode)->getSExtValue();
473 EVT StoredVT = ST->getMemoryVT();
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000474 EVT ValueVT = Value.getValueType();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000475
476 // Offset value must be within representable range
477 // and must have correct alignment properties.
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000478 const HexagonInstrInfo &TII = *HST.getInstrInfo();
479 if (TII.isValidAutoIncImm(StoredVT, Val)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000480 unsigned Opcode = 0;
481
482 // Figure out the post inc version of opcode.
Colin LeMahieu9a3cd3f2014-12-29 20:00:43 +0000483 if (StoredVT == MVT::i64) Opcode = Hexagon::S2_storerd_pi;
484 else if (StoredVT == MVT::i32) Opcode = Hexagon::S2_storeri_pi;
485 else if (StoredVT == MVT::i16) Opcode = Hexagon::S2_storerh_pi;
Colin LeMahieu3d34afb2014-12-29 19:42:14 +0000486 else if (StoredVT == MVT::i8) Opcode = Hexagon::S2_storerb_pi;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000487 else llvm_unreachable("unknown memory type");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000488
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000489 if (ST->isTruncatingStore() && ValueVT.getSizeInBits() == 64) {
490 assert(StoredVT.getSizeInBits() < 64 && "Not a truncating store");
491 Value = CurDAG->getTargetExtractSubreg(Hexagon::subreg_loreg,
492 dl, MVT::i32, Value);
493 }
494 SDValue Ops[] = {Base, CurDAG->getTargetConstant(Val, MVT::i32), Value,
495 Chain};
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000496 // Build post increment store.
497 SDNode* Result = CurDAG->getMachineNode(Opcode, dl, MVT::i32,
Michael Liaob53d8962013-04-19 22:22:57 +0000498 MVT::Other, Ops);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000499 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
500 MemOp[0] = ST->getMemOperand();
501 cast<MachineSDNode>(Result)->setMemRefs(MemOp, MemOp + 1);
502
503 ReplaceUses(ST, Result);
504 ReplaceUses(SDValue(ST,1), SDValue(Result,1));
505 return Result;
506 }
507
508 // Note: Order of operands matches the def of instruction:
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000509 // def S2_storerd_io
510 // : STInst<(outs), (ins IntRegs:$base, imm:$offset, DoubleRegs:$src1), ...
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000511 // and it differs for POST_ST* for instance.
512 SDValue Ops[] = { Base, CurDAG->getTargetConstant(0, MVT::i32), Value,
513 Chain};
514 unsigned Opcode = 0;
515
516 // Figure out the opcode.
Colin LeMahieubda31b42014-12-29 20:44:51 +0000517 if (StoredVT == MVT::i64) Opcode = Hexagon::S2_storerd_io;
518 else if (StoredVT == MVT::i32) Opcode = Hexagon::S2_storeri_io;
519 else if (StoredVT == MVT::i16) Opcode = Hexagon::S2_storerh_io;
520 else if (StoredVT == MVT::i8) Opcode = Hexagon::S2_storerb_io;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000521 else llvm_unreachable("unknown memory type");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000522
523 // Build regular store.
524 SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
Michael Liaob53d8962013-04-19 22:22:57 +0000525 SDNode* Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::Other, Ops);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000526 // Build splitted incriment instruction.
Colin LeMahieuf297dbe2015-02-05 17:49:13 +0000527 SDNode* Result_2 = CurDAG->getMachineNode(Hexagon::A2_addi, dl, MVT::i32,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000528 Base,
529 TargetConstVal,
530 SDValue(Result_1, 0));
531 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
532 MemOp[0] = ST->getMemOperand();
533 cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
534
535 ReplaceUses(SDValue(ST,0), SDValue(Result_2,0));
536 ReplaceUses(SDValue(ST,1), SDValue(Result_1,0));
537 return Result_2;
538}
539
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000540SDNode *HexagonDAGToDAGISel::SelectStore(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000541 SDLoc dl(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000542 StoreSDNode *ST = cast<StoreSDNode>(N);
543 ISD::MemIndexedMode AM = ST->getAddressingMode();
544
545 // Handle indexed stores.
546 if (AM != ISD::UNINDEXED) {
547 return SelectIndexedStore(ST, dl);
548 }
Sirish Pandec92c3162012-05-03 16:18:50 +0000549
Colin LeMahieu2efa2d02015-03-09 21:48:13 +0000550 return SelectCode(ST);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000551}
552
553SDNode *HexagonDAGToDAGISel::SelectMul(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000554 SDLoc dl(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000555
556 //
557 // %conv.i = sext i32 %tmp1 to i64
558 // %conv2.i = sext i32 %add to i64
559 // %mul.i = mul nsw i64 %conv2.i, %conv.i
560 //
561 // --- match with the following ---
562 //
563 // %mul.i = mpy (%tmp1, %add)
564 //
565
566 if (N->getValueType(0) == MVT::i64) {
567 // Shifting a i64 signed multiply.
568 SDValue MulOp0 = N->getOperand(0);
569 SDValue MulOp1 = N->getOperand(1);
570
571 SDValue OP0;
572 SDValue OP1;
573
574 // Handle sign_extend and sextload.
575 if (MulOp0.getOpcode() == ISD::SIGN_EXTEND) {
576 SDValue Sext0 = MulOp0.getOperand(0);
577 if (Sext0.getNode()->getValueType(0) != MVT::i32) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000578 return SelectCode(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000579 }
580
581 OP0 = Sext0;
582 } else if (MulOp0.getOpcode() == ISD::LOAD) {
583 LoadSDNode *LD = cast<LoadSDNode>(MulOp0.getNode());
584 if (LD->getMemoryVT() != MVT::i32 ||
585 LD->getExtensionType() != ISD::SEXTLOAD ||
586 LD->getAddressingMode() != ISD::UNINDEXED) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000587 return SelectCode(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000588 }
589
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000590 SDValue Chain = LD->getChain();
591 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
Colin LeMahieu026e88d2014-12-23 20:02:16 +0000592 OP0 = SDValue(CurDAG->getMachineNode(Hexagon::L2_loadri_io, dl, MVT::i32,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000593 MVT::Other,
594 LD->getBasePtr(), TargetConst0,
595 Chain), 0);
596 } else {
597 return SelectCode(N);
598 }
599
600 // Same goes for the second operand.
601 if (MulOp1.getOpcode() == ISD::SIGN_EXTEND) {
602 SDValue Sext1 = MulOp1.getOperand(0);
603 if (Sext1.getNode()->getValueType(0) != MVT::i32) {
604 return SelectCode(N);
605 }
606
607 OP1 = Sext1;
608 } else if (MulOp1.getOpcode() == ISD::LOAD) {
609 LoadSDNode *LD = cast<LoadSDNode>(MulOp1.getNode());
610 if (LD->getMemoryVT() != MVT::i32 ||
611 LD->getExtensionType() != ISD::SEXTLOAD ||
612 LD->getAddressingMode() != ISD::UNINDEXED) {
613 return SelectCode(N);
614 }
615
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000616 SDValue Chain = LD->getChain();
617 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
Colin LeMahieu026e88d2014-12-23 20:02:16 +0000618 OP1 = SDValue(CurDAG->getMachineNode(Hexagon::L2_loadri_io, dl, MVT::i32,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000619 MVT::Other,
620 LD->getBasePtr(), TargetConst0,
621 Chain), 0);
622 } else {
623 return SelectCode(N);
624 }
625
626 // Generate a mpy instruction.
Colin LeMahieud9b23502014-12-16 16:10:01 +0000627 SDNode *Result = CurDAG->getMachineNode(Hexagon::M2_dpmpyss_s0, dl, MVT::i64,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000628 OP0, OP1);
629 ReplaceUses(N, Result);
630 return Result;
631 }
632
633 return SelectCode(N);
634}
635
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000636SDNode *HexagonDAGToDAGISel::SelectSHL(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000637 SDLoc dl(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000638 if (N->getValueType(0) == MVT::i32) {
639 SDValue Shl_0 = N->getOperand(0);
640 SDValue Shl_1 = N->getOperand(1);
641 // RHS is const.
642 if (Shl_1.getOpcode() == ISD::Constant) {
643 if (Shl_0.getOpcode() == ISD::MUL) {
644 SDValue Mul_0 = Shl_0.getOperand(0); // Val
645 SDValue Mul_1 = Shl_0.getOperand(1); // Const
646 // RHS of mul is const.
647 if (Mul_1.getOpcode() == ISD::Constant) {
648 int32_t ShlConst =
649 cast<ConstantSDNode>(Shl_1.getNode())->getSExtValue();
650 int32_t MulConst =
651 cast<ConstantSDNode>(Mul_1.getNode())->getSExtValue();
652 int32_t ValConst = MulConst << ShlConst;
653 SDValue Val = CurDAG->getTargetConstant(ValConst,
654 MVT::i32);
655 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Val.getNode()))
656 if (isInt<9>(CN->getSExtValue())) {
657 SDNode* Result =
Colin LeMahieud9b23502014-12-16 16:10:01 +0000658 CurDAG->getMachineNode(Hexagon::M2_mpysmi, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000659 MVT::i32, Mul_0, Val);
660 ReplaceUses(N, Result);
661 return Result;
662 }
663
664 }
665 } else if (Shl_0.getOpcode() == ISD::SUB) {
666 SDValue Sub_0 = Shl_0.getOperand(0); // Const 0
667 SDValue Sub_1 = Shl_0.getOperand(1); // Val
668 if (Sub_0.getOpcode() == ISD::Constant) {
669 int32_t SubConst =
670 cast<ConstantSDNode>(Sub_0.getNode())->getSExtValue();
671 if (SubConst == 0) {
672 if (Sub_1.getOpcode() == ISD::SHL) {
673 SDValue Shl2_0 = Sub_1.getOperand(0); // Val
674 SDValue Shl2_1 = Sub_1.getOperand(1); // Const
675 if (Shl2_1.getOpcode() == ISD::Constant) {
676 int32_t ShlConst =
677 cast<ConstantSDNode>(Shl_1.getNode())->getSExtValue();
678 int32_t Shl2Const =
679 cast<ConstantSDNode>(Shl2_1.getNode())->getSExtValue();
680 int32_t ValConst = 1 << (ShlConst+Shl2Const);
681 SDValue Val = CurDAG->getTargetConstant(-ValConst, MVT::i32);
682 if (ConstantSDNode *CN =
683 dyn_cast<ConstantSDNode>(Val.getNode()))
684 if (isInt<9>(CN->getSExtValue())) {
685 SDNode* Result =
Colin LeMahieud9b23502014-12-16 16:10:01 +0000686 CurDAG->getMachineNode(Hexagon::M2_mpysmi, dl, MVT::i32,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000687 Shl2_0, Val);
688 ReplaceUses(N, Result);
689 return Result;
690 }
691 }
692 }
693 }
694 }
695 }
696 }
697 }
698 return SelectCode(N);
699}
700
701
702//
703// If there is an zero_extend followed an intrinsic in DAG (this means - the
704// result of the intrinsic is predicate); convert the zero_extend to
705// transfer instruction.
706//
707// Zero extend -> transfer is lowered here. Otherwise, zero_extend will be
708// converted into a MUX as predicate registers defined as 1 bit in the
709// compiler. Architecture defines them as 8-bit registers.
710// We want to preserve all the lower 8-bits and, not just 1 LSB bit.
711//
712SDNode *HexagonDAGToDAGISel::SelectZeroExtend(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000713 SDLoc dl(N);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000714
715 SDValue Op0 = N->getOperand(0);
716 EVT OpVT = Op0.getValueType();
717 unsigned OpBW = OpVT.getSizeInBits();
718
719 // Special handling for zero-extending a vector of booleans.
720 if (OpVT.isVector() && OpVT.getVectorElementType() == MVT::i1 && OpBW <= 64) {
721 SDNode *Mask = CurDAG->getMachineNode(Hexagon::C2_mask, dl, MVT::i64, Op0);
722 unsigned NE = OpVT.getVectorNumElements();
723 EVT ExVT = N->getValueType(0);
724 unsigned ES = ExVT.getVectorElementType().getSizeInBits();
725 uint64_t MV = 0, Bit = 1;
726 for (unsigned i = 0; i < NE; ++i) {
727 MV |= Bit;
728 Bit <<= ES;
729 }
730 SDValue Ones = CurDAG->getTargetConstant(MV, MVT::i64);
731 SDNode *OnesReg = CurDAG->getMachineNode(Hexagon::CONST64_Int_Real, dl,
732 MVT::i64, Ones);
733 if (ExVT.getSizeInBits() == 32) {
734 SDNode *And = CurDAG->getMachineNode(Hexagon::A2_andp, dl, MVT::i64,
735 SDValue(Mask,0), SDValue(OnesReg,0));
736 SDValue SubR = CurDAG->getTargetConstant(Hexagon::subreg_loreg, MVT::i32);
737 return CurDAG->getMachineNode(Hexagon::EXTRACT_SUBREG, dl, ExVT,
738 SDValue(And,0), SubR);
739 }
740 return CurDAG->getMachineNode(Hexagon::A2_andp, dl, ExVT,
741 SDValue(Mask,0), SDValue(OnesReg,0));
742 }
743
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000744 SDNode *IsIntrinsic = N->getOperand(0).getNode();
745 if ((IsIntrinsic->getOpcode() == ISD::INTRINSIC_WO_CHAIN)) {
746 unsigned ID =
747 cast<ConstantSDNode>(IsIntrinsic->getOperand(0))->getZExtValue();
748 if (doesIntrinsicReturnPredicate(ID)) {
749 // Now we need to differentiate target data types.
750 if (N->getValueType(0) == MVT::i64) {
Krzysztof Parzyszekae14e7b2015-03-17 21:47:16 +0000751 // Convert the zero_extend to Rs = Pd followed by A2_combinew(0,Rs).
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000752 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
Colin LeMahieu30dcb232014-12-09 18:16:49 +0000753 SDNode *Result_1 = CurDAG->getMachineNode(Hexagon::C2_tfrpr, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000754 MVT::i32,
755 SDValue(IsIntrinsic, 0));
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000756 SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A2_tfrsi, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000757 MVT::i32,
758 TargetConst0);
Colin LeMahieub580d7d2014-12-09 19:23:45 +0000759 SDNode *Result_3 = CurDAG->getMachineNode(Hexagon::A2_combinew, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000760 MVT::i64, MVT::Other,
761 SDValue(Result_2, 0),
762 SDValue(Result_1, 0));
763 ReplaceUses(N, Result_3);
764 return Result_3;
765 }
766 if (N->getValueType(0) == MVT::i32) {
767 // Convert the zero_extend to Rs = Pd
Colin LeMahieu30dcb232014-12-09 18:16:49 +0000768 SDNode* RsPd = CurDAG->getMachineNode(Hexagon::C2_tfrpr, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000769 MVT::i32,
770 SDValue(IsIntrinsic, 0));
771 ReplaceUses(N, RsPd);
772 return RsPd;
773 }
Craig Toppere55c5562012-02-07 02:50:20 +0000774 llvm_unreachable("Unexpected value type");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000775 }
776 }
777 return SelectCode(N);
778}
779
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000780//
Krzysztof Parzyszek47ab1f22015-03-18 16:23:44 +0000781// Checking for intrinsics circular load/store, and bitreverse load/store
782// instrisics in order to select the correct lowered operation.
783//
784SDNode *HexagonDAGToDAGISel::SelectIntrinsicWChain(SDNode *N) {
785 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
786 if (IntNo == Intrinsic::hexagon_circ_ldd ||
787 IntNo == Intrinsic::hexagon_circ_ldw ||
788 IntNo == Intrinsic::hexagon_circ_lduh ||
789 IntNo == Intrinsic::hexagon_circ_ldh ||
790 IntNo == Intrinsic::hexagon_circ_ldub ||
791 IntNo == Intrinsic::hexagon_circ_ldb) {
792 SDLoc dl(N);
793 SDValue Chain = N->getOperand(0);
794 SDValue Base = N->getOperand(2);
795 SDValue Load = N->getOperand(3);
796 SDValue ModifierExpr = N->getOperand(4);
797 SDValue Offset = N->getOperand(5);
798
799 // We need to add the rerurn type for the load. This intrinsic has
800 // two return types, one for the load and one for the post-increment.
801 // Only the *_ld instructions push the extra return type, and bump the
802 // result node operand number correspondingly.
803 std::vector<EVT> ResTys;
804 unsigned opc;
805 unsigned memsize, align;
806 MVT MvtSize = MVT::i32;
807
808 if (IntNo == Intrinsic::hexagon_circ_ldd) {
809 ResTys.push_back(MVT::i32);
810 ResTys.push_back(MVT::i64);
811 opc = Hexagon::L2_loadrd_pci_pseudo;
812 memsize = 8;
813 align = 8;
814 } else if (IntNo == Intrinsic::hexagon_circ_ldw) {
815 ResTys.push_back(MVT::i32);
816 ResTys.push_back(MVT::i32);
817 opc = Hexagon::L2_loadri_pci_pseudo;
818 memsize = 4;
819 align = 4;
820 } else if (IntNo == Intrinsic::hexagon_circ_ldh) {
821 ResTys.push_back(MVT::i32);
822 ResTys.push_back(MVT::i32);
823 opc = Hexagon::L2_loadrh_pci_pseudo;
824 memsize = 2;
825 align = 2;
826 MvtSize = MVT::i16;
827 } else if (IntNo == Intrinsic::hexagon_circ_lduh) {
828 ResTys.push_back(MVT::i32);
829 ResTys.push_back(MVT::i32);
830 opc = Hexagon::L2_loadruh_pci_pseudo;
831 memsize = 2;
832 align = 2;
833 MvtSize = MVT::i16;
834 } else if (IntNo == Intrinsic::hexagon_circ_ldb) {
835 ResTys.push_back(MVT::i32);
836 ResTys.push_back(MVT::i32);
837 opc = Hexagon::L2_loadrb_pci_pseudo;
838 memsize = 1;
839 align = 1;
840 MvtSize = MVT::i8;
841 } else if (IntNo == Intrinsic::hexagon_circ_ldub) {
842 ResTys.push_back(MVT::i32);
843 ResTys.push_back(MVT::i32);
844 opc = Hexagon::L2_loadrub_pci_pseudo;
845 memsize = 1;
846 align = 1;
847 MvtSize = MVT::i8;
848 } else
849 llvm_unreachable("no opc");
850
851 ResTys.push_back(MVT::Other);
852
853 // Copy over the arguments, which are the same mostly.
854 SmallVector<SDValue, 5> Ops;
855 Ops.push_back(Base);
856 Ops.push_back(Load);
857 Ops.push_back(ModifierExpr);
858 int32_t Val = cast<ConstantSDNode>(Offset.getNode())->getSExtValue();
859 Ops.push_back(CurDAG->getTargetConstant(Val, MVT::i32));
860 Ops.push_back(Chain);
861 SDNode* Result = CurDAG->getMachineNode(opc, dl, ResTys, Ops);
862
863 SDValue ST;
864 MachineMemOperand *Mem =
865 MF->getMachineMemOperand(MachinePointerInfo(),
866 MachineMemOperand::MOStore, memsize, align);
867 if (MvtSize != MVT::i32)
868 ST = CurDAG->getTruncStore(Chain, dl, SDValue(Result, 1), Load,
869 MvtSize, Mem);
870 else
871 ST = CurDAG->getStore(Chain, dl, SDValue(Result, 1), Load, Mem);
872
873 SDNode* Store = SelectStore(ST.getNode());
874
875 const SDValue Froms[] = { SDValue(N, 0),
876 SDValue(N, 1) };
877 const SDValue Tos[] = { SDValue(Result, 0),
878 SDValue(Store, 0) };
879 ReplaceUses(Froms, Tos, 2);
880 return Result;
881 }
882
883 if (IntNo == Intrinsic::hexagon_brev_ldd ||
884 IntNo == Intrinsic::hexagon_brev_ldw ||
885 IntNo == Intrinsic::hexagon_brev_ldh ||
886 IntNo == Intrinsic::hexagon_brev_lduh ||
887 IntNo == Intrinsic::hexagon_brev_ldb ||
888 IntNo == Intrinsic::hexagon_brev_ldub) {
889 SDLoc dl(N);
890 SDValue Chain = N->getOperand(0);
891 SDValue Base = N->getOperand(2);
892 SDValue Load = N->getOperand(3);
893 SDValue ModifierExpr = N->getOperand(4);
894
895 // We need to add the rerurn type for the load. This intrinsic has
896 // two return types, one for the load and one for the post-increment.
897 std::vector<EVT> ResTys;
898 unsigned opc;
899 unsigned memsize, align;
900 MVT MvtSize = MVT::i32;
901
902 if (IntNo == Intrinsic::hexagon_brev_ldd) {
903 ResTys.push_back(MVT::i32);
904 ResTys.push_back(MVT::i64);
905 opc = Hexagon::L2_loadrd_pbr_pseudo;
906 memsize = 8;
907 align = 8;
908 } else if (IntNo == Intrinsic::hexagon_brev_ldw) {
909 ResTys.push_back(MVT::i32);
910 ResTys.push_back(MVT::i32);
911 opc = Hexagon::L2_loadri_pbr_pseudo;
912 memsize = 4;
913 align = 4;
914 } else if (IntNo == Intrinsic::hexagon_brev_ldh) {
915 ResTys.push_back(MVT::i32);
916 ResTys.push_back(MVT::i32);
917 opc = Hexagon::L2_loadrh_pbr_pseudo;
918 memsize = 2;
919 align = 2;
920 MvtSize = MVT::i16;
921 } else if (IntNo == Intrinsic::hexagon_brev_lduh) {
922 ResTys.push_back(MVT::i32);
923 ResTys.push_back(MVT::i32);
924 opc = Hexagon::L2_loadruh_pbr_pseudo;
925 memsize = 2;
926 align = 2;
927 MvtSize = MVT::i16;
928 } else if (IntNo == Intrinsic::hexagon_brev_ldb) {
929 ResTys.push_back(MVT::i32);
930 ResTys.push_back(MVT::i32);
931 opc = Hexagon::L2_loadrb_pbr_pseudo;
932 memsize = 1;
933 align = 1;
934 MvtSize = MVT::i8;
935 } else if (IntNo == Intrinsic::hexagon_brev_ldub) {
936 ResTys.push_back(MVT::i32);
937 ResTys.push_back(MVT::i32);
938 opc = Hexagon::L2_loadrub_pbr_pseudo;
939 memsize = 1;
940 align = 1;
941 MvtSize = MVT::i8;
942 } else
943 llvm_unreachable("no opc");
944
945 ResTys.push_back(MVT::Other);
946
947 // Copy over the arguments, which are the same mostly.
948 SmallVector<SDValue, 4> Ops;
949 Ops.push_back(Base);
950 Ops.push_back(Load);
951 Ops.push_back(ModifierExpr);
952 Ops.push_back(Chain);
953 SDNode* Result = CurDAG->getMachineNode(opc, dl, ResTys, Ops);
954 SDValue ST;
955 MachineMemOperand *Mem =
956 MF->getMachineMemOperand(MachinePointerInfo(),
957 MachineMemOperand::MOStore, memsize, align);
958 if (MvtSize != MVT::i32)
959 ST = CurDAG->getTruncStore(Chain, dl, SDValue(Result, 1), Load,
960 MvtSize, Mem);
961 else
962 ST = CurDAG->getStore(Chain, dl, SDValue(Result, 1), Load, Mem);
963
964 SDNode* Store = SelectStore(ST.getNode());
965
966 const SDValue Froms[] = { SDValue(N, 0),
967 SDValue(N, 1) };
968 const SDValue Tos[] = { SDValue(Result, 0),
969 SDValue(Store, 0) };
970 ReplaceUses(Froms, Tos, 2);
971 return Result;
972 }
973
974 return SelectCode(N);
975}
976
977//
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000978// Checking for intrinsics which have predicate registers as operand(s)
979// and lowering to the actual intrinsic.
980//
981SDNode *HexagonDAGToDAGISel::SelectIntrinsicWOChain(SDNode *N) {
Colin LeMahieu0ee02fc2015-01-19 20:31:18 +0000982 unsigned IID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
983 unsigned Bits;
984 switch (IID) {
985 case Intrinsic::hexagon_S2_vsplatrb:
986 Bits = 8;
987 break;
988 case Intrinsic::hexagon_S2_vsplatrh:
989 Bits = 16;
990 break;
991 default:
992 return SelectCode(N);
993 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000994
Colin LeMahieu0ee02fc2015-01-19 20:31:18 +0000995 SDValue const &V = N->getOperand(1);
996 SDValue U;
997 if (isValueExtension(V, Bits, U)) {
998 SDValue R = CurDAG->getNode(N->getOpcode(), SDLoc(N), N->getValueType(0),
999 N->getOperand(0), U);
1000 return SelectCode(R.getNode());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001001 }
1002 return SelectCode(N);
1003}
1004
Sirish Pande69295b82012-05-10 20:20:25 +00001005//
1006// Map floating point constant values.
1007//
1008SDNode *HexagonDAGToDAGISel::SelectConstantFP(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001009 SDLoc dl(N);
Sirish Pande69295b82012-05-10 20:20:25 +00001010 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N);
1011 APFloat APF = CN->getValueAPF();
1012 if (N->getValueType(0) == MVT::f32) {
1013 return CurDAG->getMachineNode(Hexagon::TFRI_f, dl, MVT::f32,
1014 CurDAG->getTargetConstantFP(APF.convertToFloat(), MVT::f32));
1015 }
1016 else if (N->getValueType(0) == MVT::f64) {
1017 return CurDAG->getMachineNode(Hexagon::CONST64_Float_Real, dl, MVT::f64,
1018 CurDAG->getTargetConstantFP(APF.convertToDouble(), MVT::f64));
1019 }
1020
1021 return SelectCode(N);
1022}
1023
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001024//
1025// Map predicate true (encoded as -1 in LLVM) to a XOR.
1026//
1027SDNode *HexagonDAGToDAGISel::SelectConstant(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001028 SDLoc dl(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001029 if (N->getValueType(0) == MVT::i1) {
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +00001030 SDNode* Result = 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001031 int32_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Krzysztof Parzyszek7a9cd802015-03-18 18:50:06 +00001032 if (Val == -1) {
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +00001033 Result = CurDAG->getMachineNode(Hexagon::TFR_PdTrue, dl, MVT::i1);
1034 } else if (Val == 0) {
1035 Result = CurDAG->getMachineNode(Hexagon::TFR_PdFalse, dl, MVT::i1);
1036 }
1037 if (Result) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001038 ReplaceUses(N, Result);
1039 return Result;
1040 }
1041 }
1042
1043 return SelectCode(N);
1044}
1045
1046
1047//
1048// Map add followed by a asr -> asr +=.
1049//
1050SDNode *HexagonDAGToDAGISel::SelectAdd(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001051 SDLoc dl(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001052 if (N->getValueType(0) != MVT::i32) {
1053 return SelectCode(N);
1054 }
1055 // Identify nodes of the form: add(asr(...)).
1056 SDNode* Src1 = N->getOperand(0).getNode();
1057 if (Src1->getOpcode() != ISD::SRA || !Src1->hasOneUse()
1058 || Src1->getValueType(0) != MVT::i32) {
1059 return SelectCode(N);
1060 }
1061
1062 // Build Rd = Rd' + asr(Rs, Rt). The machine constraints will ensure that
1063 // Rd and Rd' are assigned to the same register
Colin LeMahieu0f850bd2014-12-19 20:29:29 +00001064 SDNode* Result = CurDAG->getMachineNode(Hexagon::S2_asr_r_r_acc, dl, MVT::i32,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001065 N->getOperand(1),
1066 Src1->getOperand(0),
1067 Src1->getOperand(1));
1068 ReplaceUses(N, Result);
1069
1070 return Result;
1071}
1072
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001073//
1074// Map the following, where possible.
1075// AND/FABS -> clrbit
1076// OR -> setbit
1077// XOR/FNEG ->toggle_bit.
1078//
1079SDNode *HexagonDAGToDAGISel::SelectBitOp(SDNode *N) {
1080 SDLoc dl(N);
1081 EVT ValueVT = N->getValueType(0);
1082
1083 // We handle only 32 and 64-bit bit ops.
1084 if (!(ValueVT == MVT::i32 || ValueVT == MVT::i64 ||
1085 ValueVT == MVT::f32 || ValueVT == MVT::f64))
1086 return SelectCode(N);
1087
1088 // We handly only fabs and fneg for V5.
1089 unsigned Opc = N->getOpcode();
1090 if ((Opc == ISD::FABS || Opc == ISD::FNEG) && !HST.hasV5TOps())
1091 return SelectCode(N);
1092
1093 int64_t Val = 0;
1094 if (Opc != ISD::FABS && Opc != ISD::FNEG) {
1095 if (N->getOperand(1).getOpcode() == ISD::Constant)
1096 Val = cast<ConstantSDNode>((N)->getOperand(1))->getSExtValue();
1097 else
1098 return SelectCode(N);
1099 }
1100
1101 if (Opc == ISD::AND) {
1102 if (((ValueVT == MVT::i32) &&
1103 (!((Val & 0x80000000) || (Val & 0x7fffffff)))) ||
1104 ((ValueVT == MVT::i64) &&
1105 (!((Val & 0x8000000000000000) || (Val & 0x7fffffff)))))
1106 // If it's simple AND, do the normal op.
1107 return SelectCode(N);
1108 else
1109 Val = ~Val;
1110 }
1111
1112 // If OR or AND is being fed by shl, srl and, sra don't do this change,
1113 // because Hexagon provide |= &= on shl, srl, and sra.
1114 // Traverse the DAG to see if there is shl, srl and sra.
1115 if (Opc == ISD::OR || Opc == ISD::AND) {
1116 switch (N->getOperand(0)->getOpcode()) {
1117 default: break;
1118 case ISD::SRA:
1119 case ISD::SRL:
1120 case ISD::SHL:
1121 return SelectCode(N);
1122 }
1123 }
1124
1125 // Make sure it's power of 2.
1126 unsigned bitpos = 0;
1127 if (Opc != ISD::FABS && Opc != ISD::FNEG) {
1128 if (((ValueVT == MVT::i32) && !isPowerOf2_32(Val)) ||
1129 ((ValueVT == MVT::i64) && !isPowerOf2_64(Val)))
1130 return SelectCode(N);
1131
1132 // Get the bit position.
1133 while (!(Val & 1)) {
1134 Val >>= 1;
1135 ++bitpos;
1136 }
1137 } else {
1138 // For fabs and fneg, it's always the 31st bit.
1139 bitpos = 31;
1140 }
1141
1142 unsigned BitOpc = 0;
1143 // Set the right opcode for bitwise operations.
1144 switch(Opc) {
1145 default: llvm_unreachable("Only bit-wise/abs/neg operations are allowed.");
1146 case ISD::AND:
1147 case ISD::FABS:
1148 BitOpc = Hexagon::S2_clrbit_i;
1149 break;
1150 case ISD::OR:
1151 BitOpc = Hexagon::S2_setbit_i;
1152 break;
1153 case ISD::XOR:
1154 case ISD::FNEG:
1155 BitOpc = Hexagon::S2_togglebit_i;
1156 break;
1157 }
1158
1159 SDNode *Result;
1160 // Get the right SDVal for the opcode.
1161 SDValue SDVal = CurDAG->getTargetConstant(bitpos, MVT::i32);
1162
1163 if (ValueVT == MVT::i32 || ValueVT == MVT::f32) {
1164 Result = CurDAG->getMachineNode(BitOpc, dl, ValueVT,
1165 N->getOperand(0), SDVal);
1166 } else {
1167 // 64-bit gymnastic to use REG_SEQUENCE. But it's worth it.
1168 EVT SubValueVT;
1169 if (ValueVT == MVT::i64)
1170 SubValueVT = MVT::i32;
1171 else
1172 SubValueVT = MVT::f32;
1173
1174 SDNode *Reg = N->getOperand(0).getNode();
1175 SDValue RegClass = CurDAG->getTargetConstant(Hexagon::DoubleRegsRegClassID,
1176 MVT::i64);
1177
1178 SDValue SubregHiIdx = CurDAG->getTargetConstant(Hexagon::subreg_hireg,
1179 MVT::i32);
1180 SDValue SubregLoIdx = CurDAG->getTargetConstant(Hexagon::subreg_loreg,
1181 MVT::i32);
1182
1183 SDValue SubregHI = CurDAG->getTargetExtractSubreg(Hexagon::subreg_hireg, dl,
1184 MVT::i32, SDValue(Reg, 0));
1185
1186 SDValue SubregLO = CurDAG->getTargetExtractSubreg(Hexagon::subreg_loreg, dl,
1187 MVT::i32, SDValue(Reg, 0));
1188
1189 // Clear/set/toggle hi or lo registers depending on the bit position.
1190 if (SubValueVT != MVT::f32 && bitpos < 32) {
1191 SDNode *Result0 = CurDAG->getMachineNode(BitOpc, dl, SubValueVT,
1192 SubregLO, SDVal);
1193 const SDValue Ops[] = { RegClass, SubregHI, SubregHiIdx,
1194 SDValue(Result0, 0), SubregLoIdx };
1195 Result = CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE,
1196 dl, ValueVT, Ops);
1197 } else {
1198 if (Opc != ISD::FABS && Opc != ISD::FNEG)
1199 SDVal = CurDAG->getTargetConstant(bitpos-32, MVT::i32);
1200 SDNode *Result0 = CurDAG->getMachineNode(BitOpc, dl, SubValueVT,
1201 SubregHI, SDVal);
1202 const SDValue Ops[] = { RegClass, SDValue(Result0, 0), SubregHiIdx,
1203 SubregLO, SubregLoIdx };
1204 Result = CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE,
1205 dl, ValueVT, Ops);
1206 }
1207 }
1208
1209 ReplaceUses(N, Result);
1210 return Result;
1211}
1212
1213
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001214SDNode *HexagonDAGToDAGISel::SelectFrameIndex(SDNode *N) {
1215 int FX = cast<FrameIndexSDNode>(N)->getIndex();
1216 SDValue FI = CurDAG->getTargetFrameIndex(FX, MVT::i32);
1217 SDValue Zero = CurDAG->getTargetConstant(0, MVT::i32);
1218 SDLoc DL(N);
1219
1220 SDNode *R = CurDAG->getMachineNode(Hexagon::TFR_FI, DL, MVT::i32, FI, Zero);
1221
1222 if (N->getHasDebugValue())
1223 CurDAG->TransferDbgValues(SDValue(N, 0), SDValue(R, 0));
1224 return R;
1225}
1226
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001227
1228SDNode *HexagonDAGToDAGISel::Select(SDNode *N) {
Tim Northover31d093c2013-09-22 08:21:56 +00001229 if (N->isMachineOpcode()) {
1230 N->setNodeId(-1);
Craig Topper062a2ba2014-04-25 05:30:21 +00001231 return nullptr; // Already selected.
Tim Northover31d093c2013-09-22 08:21:56 +00001232 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001233
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001234 switch (N->getOpcode()) {
1235 case ISD::Constant:
1236 return SelectConstant(N);
1237
Sirish Pande69295b82012-05-10 20:20:25 +00001238 case ISD::ConstantFP:
1239 return SelectConstantFP(N);
1240
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001241 case ISD::FrameIndex:
1242 return SelectFrameIndex(N);
1243
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001244 case ISD::ADD:
1245 return SelectAdd(N);
1246
1247 case ISD::SHL:
1248 return SelectSHL(N);
1249
1250 case ISD::LOAD:
1251 return SelectLoad(N);
1252
1253 case ISD::STORE:
1254 return SelectStore(N);
1255
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001256 case ISD::MUL:
1257 return SelectMul(N);
1258
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001259 case ISD::AND:
1260 case ISD::OR:
1261 case ISD::XOR:
1262 case ISD::FABS:
1263 case ISD::FNEG:
1264 return SelectBitOp(N);
1265
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001266 case ISD::ZERO_EXTEND:
1267 return SelectZeroExtend(N);
1268
Krzysztof Parzyszek47ab1f22015-03-18 16:23:44 +00001269 case ISD::INTRINSIC_W_CHAIN:
1270 return SelectIntrinsicWChain(N);
1271
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001272 case ISD::INTRINSIC_WO_CHAIN:
1273 return SelectIntrinsicWOChain(N);
1274 }
1275
1276 return SelectCode(N);
1277}
1278
1279
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001280bool HexagonDAGToDAGISel::
Daniel Sanders60f1db02015-03-13 12:45:09 +00001281SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001282 std::vector<SDValue> &OutOps) {
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001283 SDValue Inp = Op, Res;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001284
Daniel Sanders60f1db02015-03-13 12:45:09 +00001285 switch (ConstraintID) {
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001286 default:
1287 return true;
Daniel Sanders49f643c2015-03-17 14:37:39 +00001288 case InlineAsm::Constraint_i:
1289 case InlineAsm::Constraint_o: // Offsetable.
1290 case InlineAsm::Constraint_v: // Not offsetable.
1291 case InlineAsm::Constraint_m: // Memory.
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001292 if (SelectAddrFI(Inp, Res))
1293 OutOps.push_back(Res);
1294 else
1295 OutOps.push_back(Inp);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001296 break;
1297 }
1298
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001299 OutOps.push_back(CurDAG->getTargetConstant(0, MVT::i32));
Jyotsna Vermad9225242013-02-13 21:38:46 +00001300 return false;
1301}
Colin LeMahieuc7522f32015-01-14 23:07:36 +00001302
Krzysztof Parzyszekae14e7b2015-03-17 21:47:16 +00001303void HexagonDAGToDAGISel::PreprocessISelDAG() {
1304 SelectionDAG &DAG = *CurDAG;
1305 std::vector<SDNode*> Nodes;
1306 for (auto I = DAG.allnodes_begin(), E = DAG.allnodes_end(); I != E; ++I)
1307 Nodes.push_back(I);
1308
1309 // Simplify: (or (select c x 0) z) -> (select c (or x z) z)
1310 // (or (select c 0 y) z) -> (select c z (or y z))
1311 // This may not be the right thing for all targets, so do it here.
1312 for (auto I: Nodes) {
1313 if (I->getOpcode() != ISD::OR)
1314 continue;
1315
1316 auto IsZero = [] (const SDValue &V) -> bool {
1317 if (ConstantSDNode *SC = dyn_cast<ConstantSDNode>(V.getNode()))
1318 return SC->isNullValue();
1319 return false;
1320 };
1321 auto IsSelect0 = [IsZero] (const SDValue &Op) -> bool {
1322 if (Op.getOpcode() != ISD::SELECT)
1323 return false;
1324 return IsZero(Op.getOperand(1)) || IsZero(Op.getOperand(2));
1325 };
1326
1327 SDValue N0 = I->getOperand(0), N1 = I->getOperand(1);
1328 EVT VT = I->getValueType(0);
1329 bool SelN0 = IsSelect0(N0);
1330 SDValue SOp = SelN0 ? N0 : N1;
1331 SDValue VOp = SelN0 ? N1 : N0;
1332
1333 if (SOp.getOpcode() == ISD::SELECT && SOp.getNode()->hasOneUse()) {
1334 SDValue SC = SOp.getOperand(0);
1335 SDValue SX = SOp.getOperand(1);
1336 SDValue SY = SOp.getOperand(2);
1337 SDLoc DLS = SOp;
1338 if (IsZero(SY)) {
1339 SDValue NewOr = DAG.getNode(ISD::OR, DLS, VT, SX, VOp);
1340 SDValue NewSel = DAG.getNode(ISD::SELECT, DLS, VT, SC, NewOr, VOp);
1341 DAG.ReplaceAllUsesWith(I, NewSel.getNode());
1342 } else if (IsZero(SX)) {
1343 SDValue NewOr = DAG.getNode(ISD::OR, DLS, VT, SY, VOp);
1344 SDValue NewSel = DAG.getNode(ISD::SELECT, DLS, VT, SC, VOp, NewOr);
1345 DAG.ReplaceAllUsesWith(I, NewSel.getNode());
1346 }
1347 }
1348 }
1349}
1350
1351
Colin LeMahieuc7522f32015-01-14 23:07:36 +00001352bool HexagonDAGToDAGISel::SelectAddrFI(SDValue& N, SDValue &R) {
1353 if (N.getOpcode() != ISD::FrameIndex)
1354 return false;
1355 FrameIndexSDNode *FX = cast<FrameIndexSDNode>(N);
1356 R = CurDAG->getTargetFrameIndex(FX->getIndex(), MVT::i32);
1357 return true;
1358}
Colin LeMahieu0ee02fc2015-01-19 20:31:18 +00001359
Colin LeMahieu987b0942015-02-04 20:38:01 +00001360inline bool HexagonDAGToDAGISel::SelectAddrGA(SDValue &N, SDValue &R) {
1361 return SelectGlobalAddress(N, R, false);
1362}
1363
Colin LeMahieu51491352015-02-04 22:36:28 +00001364inline bool HexagonDAGToDAGISel::SelectAddrGP(SDValue &N, SDValue &R) {
1365 return SelectGlobalAddress(N, R, true);
1366}
1367
Colin LeMahieu987b0942015-02-04 20:38:01 +00001368bool HexagonDAGToDAGISel::SelectGlobalAddress(SDValue &N, SDValue &R,
1369 bool UseGP) {
1370 switch (N.getOpcode()) {
1371 case ISD::ADD: {
1372 SDValue N0 = N.getOperand(0);
1373 SDValue N1 = N.getOperand(1);
1374 unsigned GAOpc = N0.getOpcode();
1375 if (UseGP && GAOpc != HexagonISD::CONST32_GP)
1376 return false;
1377 if (!UseGP && GAOpc != HexagonISD::CONST32)
1378 return false;
1379 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N1)) {
1380 SDValue Addr = N0.getOperand(0);
1381 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Addr)) {
1382 if (GA->getOpcode() == ISD::TargetGlobalAddress) {
1383 uint64_t NewOff = GA->getOffset() + (uint64_t)Const->getSExtValue();
1384 R = CurDAG->getTargetGlobalAddress(GA->getGlobal(), SDLoc(Const),
1385 N.getValueType(), NewOff);
1386 return true;
1387 }
1388 }
1389 }
1390 break;
1391 }
1392 case HexagonISD::CONST32:
1393 // The operand(0) of CONST32 is TargetGlobalAddress, which is what we
1394 // want in the instruction.
1395 if (!UseGP)
1396 R = N.getOperand(0);
1397 return !UseGP;
1398 case HexagonISD::CONST32_GP:
1399 if (UseGP)
1400 R = N.getOperand(0);
1401 return UseGP;
1402 default:
1403 return false;
1404 }
1405
1406 return false;
1407}
1408
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001409bool HexagonDAGToDAGISel::isValueExtension(const SDValue &Val,
1410 unsigned FromBits, SDValue &Src) {
Colin LeMahieu0ee02fc2015-01-19 20:31:18 +00001411 unsigned Opc = Val.getOpcode();
1412 switch (Opc) {
1413 case ISD::SIGN_EXTEND:
1414 case ISD::ZERO_EXTEND:
1415 case ISD::ANY_EXTEND: {
1416 SDValue const &Op0 = Val.getOperand(0);
1417 EVT T = Op0.getValueType();
1418 if (T.isInteger() && T.getSizeInBits() == FromBits) {
1419 Src = Op0;
1420 return true;
1421 }
1422 break;
1423 }
1424 case ISD::SIGN_EXTEND_INREG:
1425 case ISD::AssertSext:
1426 case ISD::AssertZext:
1427 if (Val.getOperand(0).getValueType().isInteger()) {
1428 VTSDNode *T = cast<VTSDNode>(Val.getOperand(1));
1429 if (T->getVT().getSizeInBits() == FromBits) {
1430 Src = Val.getOperand(0);
1431 return true;
1432 }
1433 }
1434 break;
1435 case ISD::AND: {
1436 // Check if this is an AND with "FromBits" of lower bits set to 1.
1437 uint64_t FromMask = (1 << FromBits) - 1;
1438 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(0))) {
1439 if (C->getZExtValue() == FromMask) {
1440 Src = Val.getOperand(1);
1441 return true;
1442 }
1443 }
1444 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(1))) {
1445 if (C->getZExtValue() == FromMask) {
1446 Src = Val.getOperand(0);
1447 return true;
1448 }
1449 }
1450 break;
1451 }
1452 case ISD::OR:
1453 case ISD::XOR: {
1454 // OR/XOR with the lower "FromBits" bits set to 0.
1455 uint64_t FromMask = (1 << FromBits) - 1;
1456 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(0))) {
1457 if ((C->getZExtValue() & FromMask) == 0) {
1458 Src = Val.getOperand(1);
1459 return true;
1460 }
1461 }
1462 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(1))) {
1463 if ((C->getZExtValue() & FromMask) == 0) {
1464 Src = Val.getOperand(0);
1465 return true;
1466 }
1467 }
1468 }
1469 default:
1470 break;
1471 }
1472 return false;
1473}