blob: 6e8d431ee100fc79a5560ffd3ed2fee947f72b07 [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);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000714 SDNode *IsIntrinsic = N->getOperand(0).getNode();
715 if ((IsIntrinsic->getOpcode() == ISD::INTRINSIC_WO_CHAIN)) {
716 unsigned ID =
717 cast<ConstantSDNode>(IsIntrinsic->getOperand(0))->getZExtValue();
718 if (doesIntrinsicReturnPredicate(ID)) {
719 // Now we need to differentiate target data types.
720 if (N->getValueType(0) == MVT::i64) {
Krzysztof Parzyszekae14e7b2015-03-17 21:47:16 +0000721 // Convert the zero_extend to Rs = Pd followed by A2_combinew(0,Rs).
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000722 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
Colin LeMahieu30dcb232014-12-09 18:16:49 +0000723 SDNode *Result_1 = CurDAG->getMachineNode(Hexagon::C2_tfrpr, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000724 MVT::i32,
725 SDValue(IsIntrinsic, 0));
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000726 SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A2_tfrsi, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000727 MVT::i32,
728 TargetConst0);
Colin LeMahieub580d7d2014-12-09 19:23:45 +0000729 SDNode *Result_3 = CurDAG->getMachineNode(Hexagon::A2_combinew, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000730 MVT::i64, MVT::Other,
731 SDValue(Result_2, 0),
732 SDValue(Result_1, 0));
733 ReplaceUses(N, Result_3);
734 return Result_3;
735 }
736 if (N->getValueType(0) == MVT::i32) {
737 // Convert the zero_extend to Rs = Pd
Colin LeMahieu30dcb232014-12-09 18:16:49 +0000738 SDNode* RsPd = CurDAG->getMachineNode(Hexagon::C2_tfrpr, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000739 MVT::i32,
740 SDValue(IsIntrinsic, 0));
741 ReplaceUses(N, RsPd);
742 return RsPd;
743 }
Craig Toppere55c5562012-02-07 02:50:20 +0000744 llvm_unreachable("Unexpected value type");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000745 }
746 }
747 return SelectCode(N);
748}
749
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000750//
Krzysztof Parzyszek47ab1f22015-03-18 16:23:44 +0000751// Checking for intrinsics circular load/store, and bitreverse load/store
752// instrisics in order to select the correct lowered operation.
753//
754SDNode *HexagonDAGToDAGISel::SelectIntrinsicWChain(SDNode *N) {
755 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
756 if (IntNo == Intrinsic::hexagon_circ_ldd ||
757 IntNo == Intrinsic::hexagon_circ_ldw ||
758 IntNo == Intrinsic::hexagon_circ_lduh ||
759 IntNo == Intrinsic::hexagon_circ_ldh ||
760 IntNo == Intrinsic::hexagon_circ_ldub ||
761 IntNo == Intrinsic::hexagon_circ_ldb) {
762 SDLoc dl(N);
763 SDValue Chain = N->getOperand(0);
764 SDValue Base = N->getOperand(2);
765 SDValue Load = N->getOperand(3);
766 SDValue ModifierExpr = N->getOperand(4);
767 SDValue Offset = N->getOperand(5);
768
769 // We need to add the rerurn type for the load. This intrinsic has
770 // two return types, one for the load and one for the post-increment.
771 // Only the *_ld instructions push the extra return type, and bump the
772 // result node operand number correspondingly.
773 std::vector<EVT> ResTys;
774 unsigned opc;
775 unsigned memsize, align;
776 MVT MvtSize = MVT::i32;
777
778 if (IntNo == Intrinsic::hexagon_circ_ldd) {
779 ResTys.push_back(MVT::i32);
780 ResTys.push_back(MVT::i64);
781 opc = Hexagon::L2_loadrd_pci_pseudo;
782 memsize = 8;
783 align = 8;
784 } else if (IntNo == Intrinsic::hexagon_circ_ldw) {
785 ResTys.push_back(MVT::i32);
786 ResTys.push_back(MVT::i32);
787 opc = Hexagon::L2_loadri_pci_pseudo;
788 memsize = 4;
789 align = 4;
790 } else if (IntNo == Intrinsic::hexagon_circ_ldh) {
791 ResTys.push_back(MVT::i32);
792 ResTys.push_back(MVT::i32);
793 opc = Hexagon::L2_loadrh_pci_pseudo;
794 memsize = 2;
795 align = 2;
796 MvtSize = MVT::i16;
797 } else if (IntNo == Intrinsic::hexagon_circ_lduh) {
798 ResTys.push_back(MVT::i32);
799 ResTys.push_back(MVT::i32);
800 opc = Hexagon::L2_loadruh_pci_pseudo;
801 memsize = 2;
802 align = 2;
803 MvtSize = MVT::i16;
804 } else if (IntNo == Intrinsic::hexagon_circ_ldb) {
805 ResTys.push_back(MVT::i32);
806 ResTys.push_back(MVT::i32);
807 opc = Hexagon::L2_loadrb_pci_pseudo;
808 memsize = 1;
809 align = 1;
810 MvtSize = MVT::i8;
811 } else if (IntNo == Intrinsic::hexagon_circ_ldub) {
812 ResTys.push_back(MVT::i32);
813 ResTys.push_back(MVT::i32);
814 opc = Hexagon::L2_loadrub_pci_pseudo;
815 memsize = 1;
816 align = 1;
817 MvtSize = MVT::i8;
818 } else
819 llvm_unreachable("no opc");
820
821 ResTys.push_back(MVT::Other);
822
823 // Copy over the arguments, which are the same mostly.
824 SmallVector<SDValue, 5> Ops;
825 Ops.push_back(Base);
826 Ops.push_back(Load);
827 Ops.push_back(ModifierExpr);
828 int32_t Val = cast<ConstantSDNode>(Offset.getNode())->getSExtValue();
829 Ops.push_back(CurDAG->getTargetConstant(Val, MVT::i32));
830 Ops.push_back(Chain);
831 SDNode* Result = CurDAG->getMachineNode(opc, dl, ResTys, Ops);
832
833 SDValue ST;
834 MachineMemOperand *Mem =
835 MF->getMachineMemOperand(MachinePointerInfo(),
836 MachineMemOperand::MOStore, memsize, align);
837 if (MvtSize != MVT::i32)
838 ST = CurDAG->getTruncStore(Chain, dl, SDValue(Result, 1), Load,
839 MvtSize, Mem);
840 else
841 ST = CurDAG->getStore(Chain, dl, SDValue(Result, 1), Load, Mem);
842
843 SDNode* Store = SelectStore(ST.getNode());
844
845 const SDValue Froms[] = { SDValue(N, 0),
846 SDValue(N, 1) };
847 const SDValue Tos[] = { SDValue(Result, 0),
848 SDValue(Store, 0) };
849 ReplaceUses(Froms, Tos, 2);
850 return Result;
851 }
852
853 if (IntNo == Intrinsic::hexagon_brev_ldd ||
854 IntNo == Intrinsic::hexagon_brev_ldw ||
855 IntNo == Intrinsic::hexagon_brev_ldh ||
856 IntNo == Intrinsic::hexagon_brev_lduh ||
857 IntNo == Intrinsic::hexagon_brev_ldb ||
858 IntNo == Intrinsic::hexagon_brev_ldub) {
859 SDLoc dl(N);
860 SDValue Chain = N->getOperand(0);
861 SDValue Base = N->getOperand(2);
862 SDValue Load = N->getOperand(3);
863 SDValue ModifierExpr = N->getOperand(4);
864
865 // We need to add the rerurn type for the load. This intrinsic has
866 // two return types, one for the load and one for the post-increment.
867 std::vector<EVT> ResTys;
868 unsigned opc;
869 unsigned memsize, align;
870 MVT MvtSize = MVT::i32;
871
872 if (IntNo == Intrinsic::hexagon_brev_ldd) {
873 ResTys.push_back(MVT::i32);
874 ResTys.push_back(MVT::i64);
875 opc = Hexagon::L2_loadrd_pbr_pseudo;
876 memsize = 8;
877 align = 8;
878 } else if (IntNo == Intrinsic::hexagon_brev_ldw) {
879 ResTys.push_back(MVT::i32);
880 ResTys.push_back(MVT::i32);
881 opc = Hexagon::L2_loadri_pbr_pseudo;
882 memsize = 4;
883 align = 4;
884 } else if (IntNo == Intrinsic::hexagon_brev_ldh) {
885 ResTys.push_back(MVT::i32);
886 ResTys.push_back(MVT::i32);
887 opc = Hexagon::L2_loadrh_pbr_pseudo;
888 memsize = 2;
889 align = 2;
890 MvtSize = MVT::i16;
891 } else if (IntNo == Intrinsic::hexagon_brev_lduh) {
892 ResTys.push_back(MVT::i32);
893 ResTys.push_back(MVT::i32);
894 opc = Hexagon::L2_loadruh_pbr_pseudo;
895 memsize = 2;
896 align = 2;
897 MvtSize = MVT::i16;
898 } else if (IntNo == Intrinsic::hexagon_brev_ldb) {
899 ResTys.push_back(MVT::i32);
900 ResTys.push_back(MVT::i32);
901 opc = Hexagon::L2_loadrb_pbr_pseudo;
902 memsize = 1;
903 align = 1;
904 MvtSize = MVT::i8;
905 } else if (IntNo == Intrinsic::hexagon_brev_ldub) {
906 ResTys.push_back(MVT::i32);
907 ResTys.push_back(MVT::i32);
908 opc = Hexagon::L2_loadrub_pbr_pseudo;
909 memsize = 1;
910 align = 1;
911 MvtSize = MVT::i8;
912 } else
913 llvm_unreachable("no opc");
914
915 ResTys.push_back(MVT::Other);
916
917 // Copy over the arguments, which are the same mostly.
918 SmallVector<SDValue, 4> Ops;
919 Ops.push_back(Base);
920 Ops.push_back(Load);
921 Ops.push_back(ModifierExpr);
922 Ops.push_back(Chain);
923 SDNode* Result = CurDAG->getMachineNode(opc, dl, ResTys, Ops);
924 SDValue ST;
925 MachineMemOperand *Mem =
926 MF->getMachineMemOperand(MachinePointerInfo(),
927 MachineMemOperand::MOStore, memsize, align);
928 if (MvtSize != MVT::i32)
929 ST = CurDAG->getTruncStore(Chain, dl, SDValue(Result, 1), Load,
930 MvtSize, Mem);
931 else
932 ST = CurDAG->getStore(Chain, dl, SDValue(Result, 1), Load, Mem);
933
934 SDNode* Store = SelectStore(ST.getNode());
935
936 const SDValue Froms[] = { SDValue(N, 0),
937 SDValue(N, 1) };
938 const SDValue Tos[] = { SDValue(Result, 0),
939 SDValue(Store, 0) };
940 ReplaceUses(Froms, Tos, 2);
941 return Result;
942 }
943
944 return SelectCode(N);
945}
946
947//
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000948// Checking for intrinsics which have predicate registers as operand(s)
949// and lowering to the actual intrinsic.
950//
951SDNode *HexagonDAGToDAGISel::SelectIntrinsicWOChain(SDNode *N) {
Colin LeMahieu0ee02fc2015-01-19 20:31:18 +0000952 unsigned IID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
953 unsigned Bits;
954 switch (IID) {
955 case Intrinsic::hexagon_S2_vsplatrb:
956 Bits = 8;
957 break;
958 case Intrinsic::hexagon_S2_vsplatrh:
959 Bits = 16;
960 break;
961 default:
962 return SelectCode(N);
963 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000964
Colin LeMahieu0ee02fc2015-01-19 20:31:18 +0000965 SDValue const &V = N->getOperand(1);
966 SDValue U;
967 if (isValueExtension(V, Bits, U)) {
968 SDValue R = CurDAG->getNode(N->getOpcode(), SDLoc(N), N->getValueType(0),
969 N->getOperand(0), U);
970 return SelectCode(R.getNode());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000971 }
972 return SelectCode(N);
973}
974
Sirish Pande69295b82012-05-10 20:20:25 +0000975//
976// Map floating point constant values.
977//
978SDNode *HexagonDAGToDAGISel::SelectConstantFP(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000979 SDLoc dl(N);
Sirish Pande69295b82012-05-10 20:20:25 +0000980 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N);
981 APFloat APF = CN->getValueAPF();
982 if (N->getValueType(0) == MVT::f32) {
983 return CurDAG->getMachineNode(Hexagon::TFRI_f, dl, MVT::f32,
984 CurDAG->getTargetConstantFP(APF.convertToFloat(), MVT::f32));
985 }
986 else if (N->getValueType(0) == MVT::f64) {
987 return CurDAG->getMachineNode(Hexagon::CONST64_Float_Real, dl, MVT::f64,
988 CurDAG->getTargetConstantFP(APF.convertToDouble(), MVT::f64));
989 }
990
991 return SelectCode(N);
992}
993
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000994//
995// Map predicate true (encoded as -1 in LLVM) to a XOR.
996//
997SDNode *HexagonDAGToDAGISel::SelectConstant(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000998 SDLoc dl(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000999 if (N->getValueType(0) == MVT::i1) {
1000 SDNode* Result;
1001 int32_t Val = cast<ConstantSDNode>(N)->getSExtValue();
1002 if (Val == -1) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001003 // Create the IntReg = 1 node.
1004 SDNode* IntRegTFR =
Colin LeMahieu4af437f2014-12-09 20:23:30 +00001005 CurDAG->getMachineNode(Hexagon::A2_tfrsi, dl, MVT::i32,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001006 CurDAG->getTargetConstant(0, MVT::i32));
1007
1008 // Pd = IntReg
Colin LeMahieu30dcb232014-12-09 18:16:49 +00001009 SDNode* Pd = CurDAG->getMachineNode(Hexagon::C2_tfrrp, dl, MVT::i1,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001010 SDValue(IntRegTFR, 0));
1011
1012 // not(Pd)
Colin LeMahieu5cf56322014-12-08 23:55:43 +00001013 SDNode* NotPd = CurDAG->getMachineNode(Hexagon::C2_not, dl, MVT::i1,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001014 SDValue(Pd, 0));
1015
1016 // xor(not(Pd))
Colin LeMahieu5cf56322014-12-08 23:55:43 +00001017 Result = CurDAG->getMachineNode(Hexagon::C2_xor, dl, MVT::i1,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001018 SDValue(Pd, 0), SDValue(NotPd, 0));
1019
1020 // We have just built:
1021 // Rs = Pd
1022 // Pd = xor(not(Pd), Pd)
1023
1024 ReplaceUses(N, Result);
1025 return Result;
1026 }
1027 }
1028
1029 return SelectCode(N);
1030}
1031
1032
1033//
1034// Map add followed by a asr -> asr +=.
1035//
1036SDNode *HexagonDAGToDAGISel::SelectAdd(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001037 SDLoc dl(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001038 if (N->getValueType(0) != MVT::i32) {
1039 return SelectCode(N);
1040 }
1041 // Identify nodes of the form: add(asr(...)).
1042 SDNode* Src1 = N->getOperand(0).getNode();
1043 if (Src1->getOpcode() != ISD::SRA || !Src1->hasOneUse()
1044 || Src1->getValueType(0) != MVT::i32) {
1045 return SelectCode(N);
1046 }
1047
1048 // Build Rd = Rd' + asr(Rs, Rt). The machine constraints will ensure that
1049 // Rd and Rd' are assigned to the same register
Colin LeMahieu0f850bd2014-12-19 20:29:29 +00001050 SDNode* Result = CurDAG->getMachineNode(Hexagon::S2_asr_r_r_acc, dl, MVT::i32,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001051 N->getOperand(1),
1052 Src1->getOperand(0),
1053 Src1->getOperand(1));
1054 ReplaceUses(N, Result);
1055
1056 return Result;
1057}
1058
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001059//
1060// Map the following, where possible.
1061// AND/FABS -> clrbit
1062// OR -> setbit
1063// XOR/FNEG ->toggle_bit.
1064//
1065SDNode *HexagonDAGToDAGISel::SelectBitOp(SDNode *N) {
1066 SDLoc dl(N);
1067 EVT ValueVT = N->getValueType(0);
1068
1069 // We handle only 32 and 64-bit bit ops.
1070 if (!(ValueVT == MVT::i32 || ValueVT == MVT::i64 ||
1071 ValueVT == MVT::f32 || ValueVT == MVT::f64))
1072 return SelectCode(N);
1073
1074 // We handly only fabs and fneg for V5.
1075 unsigned Opc = N->getOpcode();
1076 if ((Opc == ISD::FABS || Opc == ISD::FNEG) && !HST.hasV5TOps())
1077 return SelectCode(N);
1078
1079 int64_t Val = 0;
1080 if (Opc != ISD::FABS && Opc != ISD::FNEG) {
1081 if (N->getOperand(1).getOpcode() == ISD::Constant)
1082 Val = cast<ConstantSDNode>((N)->getOperand(1))->getSExtValue();
1083 else
1084 return SelectCode(N);
1085 }
1086
1087 if (Opc == ISD::AND) {
1088 if (((ValueVT == MVT::i32) &&
1089 (!((Val & 0x80000000) || (Val & 0x7fffffff)))) ||
1090 ((ValueVT == MVT::i64) &&
1091 (!((Val & 0x8000000000000000) || (Val & 0x7fffffff)))))
1092 // If it's simple AND, do the normal op.
1093 return SelectCode(N);
1094 else
1095 Val = ~Val;
1096 }
1097
1098 // If OR or AND is being fed by shl, srl and, sra don't do this change,
1099 // because Hexagon provide |= &= on shl, srl, and sra.
1100 // Traverse the DAG to see if there is shl, srl and sra.
1101 if (Opc == ISD::OR || Opc == ISD::AND) {
1102 switch (N->getOperand(0)->getOpcode()) {
1103 default: break;
1104 case ISD::SRA:
1105 case ISD::SRL:
1106 case ISD::SHL:
1107 return SelectCode(N);
1108 }
1109 }
1110
1111 // Make sure it's power of 2.
1112 unsigned bitpos = 0;
1113 if (Opc != ISD::FABS && Opc != ISD::FNEG) {
1114 if (((ValueVT == MVT::i32) && !isPowerOf2_32(Val)) ||
1115 ((ValueVT == MVT::i64) && !isPowerOf2_64(Val)))
1116 return SelectCode(N);
1117
1118 // Get the bit position.
1119 while (!(Val & 1)) {
1120 Val >>= 1;
1121 ++bitpos;
1122 }
1123 } else {
1124 // For fabs and fneg, it's always the 31st bit.
1125 bitpos = 31;
1126 }
1127
1128 unsigned BitOpc = 0;
1129 // Set the right opcode for bitwise operations.
1130 switch(Opc) {
1131 default: llvm_unreachable("Only bit-wise/abs/neg operations are allowed.");
1132 case ISD::AND:
1133 case ISD::FABS:
1134 BitOpc = Hexagon::S2_clrbit_i;
1135 break;
1136 case ISD::OR:
1137 BitOpc = Hexagon::S2_setbit_i;
1138 break;
1139 case ISD::XOR:
1140 case ISD::FNEG:
1141 BitOpc = Hexagon::S2_togglebit_i;
1142 break;
1143 }
1144
1145 SDNode *Result;
1146 // Get the right SDVal for the opcode.
1147 SDValue SDVal = CurDAG->getTargetConstant(bitpos, MVT::i32);
1148
1149 if (ValueVT == MVT::i32 || ValueVT == MVT::f32) {
1150 Result = CurDAG->getMachineNode(BitOpc, dl, ValueVT,
1151 N->getOperand(0), SDVal);
1152 } else {
1153 // 64-bit gymnastic to use REG_SEQUENCE. But it's worth it.
1154 EVT SubValueVT;
1155 if (ValueVT == MVT::i64)
1156 SubValueVT = MVT::i32;
1157 else
1158 SubValueVT = MVT::f32;
1159
1160 SDNode *Reg = N->getOperand(0).getNode();
1161 SDValue RegClass = CurDAG->getTargetConstant(Hexagon::DoubleRegsRegClassID,
1162 MVT::i64);
1163
1164 SDValue SubregHiIdx = CurDAG->getTargetConstant(Hexagon::subreg_hireg,
1165 MVT::i32);
1166 SDValue SubregLoIdx = CurDAG->getTargetConstant(Hexagon::subreg_loreg,
1167 MVT::i32);
1168
1169 SDValue SubregHI = CurDAG->getTargetExtractSubreg(Hexagon::subreg_hireg, dl,
1170 MVT::i32, SDValue(Reg, 0));
1171
1172 SDValue SubregLO = CurDAG->getTargetExtractSubreg(Hexagon::subreg_loreg, dl,
1173 MVT::i32, SDValue(Reg, 0));
1174
1175 // Clear/set/toggle hi or lo registers depending on the bit position.
1176 if (SubValueVT != MVT::f32 && bitpos < 32) {
1177 SDNode *Result0 = CurDAG->getMachineNode(BitOpc, dl, SubValueVT,
1178 SubregLO, SDVal);
1179 const SDValue Ops[] = { RegClass, SubregHI, SubregHiIdx,
1180 SDValue(Result0, 0), SubregLoIdx };
1181 Result = CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE,
1182 dl, ValueVT, Ops);
1183 } else {
1184 if (Opc != ISD::FABS && Opc != ISD::FNEG)
1185 SDVal = CurDAG->getTargetConstant(bitpos-32, MVT::i32);
1186 SDNode *Result0 = CurDAG->getMachineNode(BitOpc, dl, SubValueVT,
1187 SubregHI, SDVal);
1188 const SDValue Ops[] = { RegClass, SDValue(Result0, 0), SubregHiIdx,
1189 SubregLO, SubregLoIdx };
1190 Result = CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE,
1191 dl, ValueVT, Ops);
1192 }
1193 }
1194
1195 ReplaceUses(N, Result);
1196 return Result;
1197}
1198
1199
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001200SDNode *HexagonDAGToDAGISel::SelectFrameIndex(SDNode *N) {
1201 int FX = cast<FrameIndexSDNode>(N)->getIndex();
1202 SDValue FI = CurDAG->getTargetFrameIndex(FX, MVT::i32);
1203 SDValue Zero = CurDAG->getTargetConstant(0, MVT::i32);
1204 SDLoc DL(N);
1205
1206 SDNode *R = CurDAG->getMachineNode(Hexagon::TFR_FI, DL, MVT::i32, FI, Zero);
1207
1208 if (N->getHasDebugValue())
1209 CurDAG->TransferDbgValues(SDValue(N, 0), SDValue(R, 0));
1210 return R;
1211}
1212
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001213
1214SDNode *HexagonDAGToDAGISel::Select(SDNode *N) {
Tim Northover31d093c2013-09-22 08:21:56 +00001215 if (N->isMachineOpcode()) {
1216 N->setNodeId(-1);
Craig Topper062a2ba2014-04-25 05:30:21 +00001217 return nullptr; // Already selected.
Tim Northover31d093c2013-09-22 08:21:56 +00001218 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001219
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001220 switch (N->getOpcode()) {
1221 case ISD::Constant:
1222 return SelectConstant(N);
1223
Sirish Pande69295b82012-05-10 20:20:25 +00001224 case ISD::ConstantFP:
1225 return SelectConstantFP(N);
1226
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001227 case ISD::FrameIndex:
1228 return SelectFrameIndex(N);
1229
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001230 case ISD::ADD:
1231 return SelectAdd(N);
1232
1233 case ISD::SHL:
1234 return SelectSHL(N);
1235
1236 case ISD::LOAD:
1237 return SelectLoad(N);
1238
1239 case ISD::STORE:
1240 return SelectStore(N);
1241
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001242 case ISD::MUL:
1243 return SelectMul(N);
1244
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001245 case ISD::AND:
1246 case ISD::OR:
1247 case ISD::XOR:
1248 case ISD::FABS:
1249 case ISD::FNEG:
1250 return SelectBitOp(N);
1251
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001252 case ISD::ZERO_EXTEND:
1253 return SelectZeroExtend(N);
1254
Krzysztof Parzyszek47ab1f22015-03-18 16:23:44 +00001255 case ISD::INTRINSIC_W_CHAIN:
1256 return SelectIntrinsicWChain(N);
1257
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001258 case ISD::INTRINSIC_WO_CHAIN:
1259 return SelectIntrinsicWOChain(N);
1260 }
1261
1262 return SelectCode(N);
1263}
1264
1265
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001266bool HexagonDAGToDAGISel::
Daniel Sanders60f1db02015-03-13 12:45:09 +00001267SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001268 std::vector<SDValue> &OutOps) {
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001269 SDValue Inp = Op, Res;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001270
Daniel Sanders60f1db02015-03-13 12:45:09 +00001271 switch (ConstraintID) {
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001272 default:
1273 return true;
Daniel Sanders49f643c2015-03-17 14:37:39 +00001274 case InlineAsm::Constraint_i:
1275 case InlineAsm::Constraint_o: // Offsetable.
1276 case InlineAsm::Constraint_v: // Not offsetable.
1277 case InlineAsm::Constraint_m: // Memory.
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001278 if (SelectAddrFI(Inp, Res))
1279 OutOps.push_back(Res);
1280 else
1281 OutOps.push_back(Inp);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001282 break;
1283 }
1284
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001285 OutOps.push_back(CurDAG->getTargetConstant(0, MVT::i32));
Jyotsna Vermad9225242013-02-13 21:38:46 +00001286 return false;
1287}
Colin LeMahieuc7522f32015-01-14 23:07:36 +00001288
Krzysztof Parzyszekae14e7b2015-03-17 21:47:16 +00001289void HexagonDAGToDAGISel::PreprocessISelDAG() {
1290 SelectionDAG &DAG = *CurDAG;
1291 std::vector<SDNode*> Nodes;
1292 for (auto I = DAG.allnodes_begin(), E = DAG.allnodes_end(); I != E; ++I)
1293 Nodes.push_back(I);
1294
1295 // Simplify: (or (select c x 0) z) -> (select c (or x z) z)
1296 // (or (select c 0 y) z) -> (select c z (or y z))
1297 // This may not be the right thing for all targets, so do it here.
1298 for (auto I: Nodes) {
1299 if (I->getOpcode() != ISD::OR)
1300 continue;
1301
1302 auto IsZero = [] (const SDValue &V) -> bool {
1303 if (ConstantSDNode *SC = dyn_cast<ConstantSDNode>(V.getNode()))
1304 return SC->isNullValue();
1305 return false;
1306 };
1307 auto IsSelect0 = [IsZero] (const SDValue &Op) -> bool {
1308 if (Op.getOpcode() != ISD::SELECT)
1309 return false;
1310 return IsZero(Op.getOperand(1)) || IsZero(Op.getOperand(2));
1311 };
1312
1313 SDValue N0 = I->getOperand(0), N1 = I->getOperand(1);
1314 EVT VT = I->getValueType(0);
1315 bool SelN0 = IsSelect0(N0);
1316 SDValue SOp = SelN0 ? N0 : N1;
1317 SDValue VOp = SelN0 ? N1 : N0;
1318
1319 if (SOp.getOpcode() == ISD::SELECT && SOp.getNode()->hasOneUse()) {
1320 SDValue SC = SOp.getOperand(0);
1321 SDValue SX = SOp.getOperand(1);
1322 SDValue SY = SOp.getOperand(2);
1323 SDLoc DLS = SOp;
1324 if (IsZero(SY)) {
1325 SDValue NewOr = DAG.getNode(ISD::OR, DLS, VT, SX, VOp);
1326 SDValue NewSel = DAG.getNode(ISD::SELECT, DLS, VT, SC, NewOr, VOp);
1327 DAG.ReplaceAllUsesWith(I, NewSel.getNode());
1328 } else if (IsZero(SX)) {
1329 SDValue NewOr = DAG.getNode(ISD::OR, DLS, VT, SY, VOp);
1330 SDValue NewSel = DAG.getNode(ISD::SELECT, DLS, VT, SC, VOp, NewOr);
1331 DAG.ReplaceAllUsesWith(I, NewSel.getNode());
1332 }
1333 }
1334 }
1335}
1336
1337
Colin LeMahieuc7522f32015-01-14 23:07:36 +00001338bool HexagonDAGToDAGISel::SelectAddrFI(SDValue& N, SDValue &R) {
1339 if (N.getOpcode() != ISD::FrameIndex)
1340 return false;
1341 FrameIndexSDNode *FX = cast<FrameIndexSDNode>(N);
1342 R = CurDAG->getTargetFrameIndex(FX->getIndex(), MVT::i32);
1343 return true;
1344}
Colin LeMahieu0ee02fc2015-01-19 20:31:18 +00001345
Colin LeMahieu987b0942015-02-04 20:38:01 +00001346inline bool HexagonDAGToDAGISel::SelectAddrGA(SDValue &N, SDValue &R) {
1347 return SelectGlobalAddress(N, R, false);
1348}
1349
Colin LeMahieu51491352015-02-04 22:36:28 +00001350inline bool HexagonDAGToDAGISel::SelectAddrGP(SDValue &N, SDValue &R) {
1351 return SelectGlobalAddress(N, R, true);
1352}
1353
Colin LeMahieu987b0942015-02-04 20:38:01 +00001354bool HexagonDAGToDAGISel::SelectGlobalAddress(SDValue &N, SDValue &R,
1355 bool UseGP) {
1356 switch (N.getOpcode()) {
1357 case ISD::ADD: {
1358 SDValue N0 = N.getOperand(0);
1359 SDValue N1 = N.getOperand(1);
1360 unsigned GAOpc = N0.getOpcode();
1361 if (UseGP && GAOpc != HexagonISD::CONST32_GP)
1362 return false;
1363 if (!UseGP && GAOpc != HexagonISD::CONST32)
1364 return false;
1365 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N1)) {
1366 SDValue Addr = N0.getOperand(0);
1367 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Addr)) {
1368 if (GA->getOpcode() == ISD::TargetGlobalAddress) {
1369 uint64_t NewOff = GA->getOffset() + (uint64_t)Const->getSExtValue();
1370 R = CurDAG->getTargetGlobalAddress(GA->getGlobal(), SDLoc(Const),
1371 N.getValueType(), NewOff);
1372 return true;
1373 }
1374 }
1375 }
1376 break;
1377 }
1378 case HexagonISD::CONST32:
1379 // The operand(0) of CONST32 is TargetGlobalAddress, which is what we
1380 // want in the instruction.
1381 if (!UseGP)
1382 R = N.getOperand(0);
1383 return !UseGP;
1384 case HexagonISD::CONST32_GP:
1385 if (UseGP)
1386 R = N.getOperand(0);
1387 return UseGP;
1388 default:
1389 return false;
1390 }
1391
1392 return false;
1393}
1394
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001395bool HexagonDAGToDAGISel::isValueExtension(const SDValue &Val,
1396 unsigned FromBits, SDValue &Src) {
Colin LeMahieu0ee02fc2015-01-19 20:31:18 +00001397 unsigned Opc = Val.getOpcode();
1398 switch (Opc) {
1399 case ISD::SIGN_EXTEND:
1400 case ISD::ZERO_EXTEND:
1401 case ISD::ANY_EXTEND: {
1402 SDValue const &Op0 = Val.getOperand(0);
1403 EVT T = Op0.getValueType();
1404 if (T.isInteger() && T.getSizeInBits() == FromBits) {
1405 Src = Op0;
1406 return true;
1407 }
1408 break;
1409 }
1410 case ISD::SIGN_EXTEND_INREG:
1411 case ISD::AssertSext:
1412 case ISD::AssertZext:
1413 if (Val.getOperand(0).getValueType().isInteger()) {
1414 VTSDNode *T = cast<VTSDNode>(Val.getOperand(1));
1415 if (T->getVT().getSizeInBits() == FromBits) {
1416 Src = Val.getOperand(0);
1417 return true;
1418 }
1419 }
1420 break;
1421 case ISD::AND: {
1422 // Check if this is an AND with "FromBits" of lower bits set to 1.
1423 uint64_t FromMask = (1 << FromBits) - 1;
1424 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(0))) {
1425 if (C->getZExtValue() == FromMask) {
1426 Src = Val.getOperand(1);
1427 return true;
1428 }
1429 }
1430 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(1))) {
1431 if (C->getZExtValue() == FromMask) {
1432 Src = Val.getOperand(0);
1433 return true;
1434 }
1435 }
1436 break;
1437 }
1438 case ISD::OR:
1439 case ISD::XOR: {
1440 // OR/XOR with the lower "FromBits" bits set to 0.
1441 uint64_t FromMask = (1 << FromBits) - 1;
1442 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(0))) {
1443 if ((C->getZExtValue() & FromMask) == 0) {
1444 Src = Val.getOperand(1);
1445 return true;
1446 }
1447 }
1448 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(1))) {
1449 if ((C->getZExtValue() & FromMask) == 0) {
1450 Src = Val.getOperand(0);
1451 return true;
1452 }
1453 }
1454 }
1455 default:
1456 break;
1457 }
1458 return false;
1459}