blob: ba307ae5f0a92269f02a60ce0cbf6f07b017bdb8 [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);
88 SDNode *SelectSelect(SDNode *N);
89 SDNode *SelectTruncate(SDNode *N);
90 SDNode *SelectMul(SDNode *N);
91 SDNode *SelectZeroExtend(SDNode *N);
Sirish Pande69295b82012-05-10 20:20:25 +000092 SDNode *SelectIntrinsicWChain(SDNode *N);
Krzysztof Parzyszekae14e7b2015-03-17 21:47:16 +000093 SDNode *SelectIntrinsicWOChain(SDNode *N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +000094 SDNode *SelectConstant(SDNode *N);
Sirish Pande69295b82012-05-10 20:20:25 +000095 SDNode *SelectConstantFP(SDNode *N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +000096 SDNode *SelectAdd(SDNode *N);
97
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +000098 // XformMskToBitPosU5Imm - Returns the bit position which
99 // the single bit 32 bit mask represents.
100 // Used in Clr and Set bit immediate memops.
101 SDValue XformMskToBitPosU5Imm(uint32_t Imm) {
102 int32_t bitPos;
103 bitPos = Log2_32(Imm);
104 assert(bitPos >= 0 && bitPos < 32 &&
105 "Constant out of range for 32 BitPos Memops");
106 return CurDAG->getTargetConstant(bitPos, MVT::i32);
107 }
Jyotsna Vermafdc660b2013-03-22 18:41:34 +0000108
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000109 // XformMskToBitPosU4Imm - Returns the bit position which the single-bit
110 // 16 bit mask represents. Used in Clr and Set bit immediate memops.
111 SDValue XformMskToBitPosU4Imm(uint16_t Imm) {
112 return XformMskToBitPosU5Imm(Imm);
113 }
Jyotsna Vermafdc660b2013-03-22 18:41:34 +0000114
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000115 // XformMskToBitPosU3Imm - Returns the bit position which the single-bit
116 // 8 bit mask represents. Used in Clr and Set bit immediate memops.
117 SDValue XformMskToBitPosU3Imm(uint8_t Imm) {
118 return XformMskToBitPosU5Imm(Imm);
119 }
Jyotsna Vermafdc660b2013-03-22 18:41:34 +0000120
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000121 // Return true if there is exactly one bit set in V, i.e., if V is one of the
122 // following integers: 2^0, 2^1, ..., 2^31.
123 bool ImmIsSingleBit(uint32_t v) const {
124 return isPowerOf2_32(v);
125 }
Jyotsna Vermafdc660b2013-03-22 18:41:34 +0000126
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000127 // XformM5ToU5Imm - Return a target constant with the specified value, of
128 // type i32 where the negative literal is transformed into a positive literal
129 // for use in -= memops.
130 inline SDValue XformM5ToU5Imm(signed Imm) {
131 assert( (Imm >= -31 && Imm <= -1) && "Constant out of range for Memops");
132 return CurDAG->getTargetConstant( - Imm, MVT::i32);
133 }
Jyotsna Vermafdc660b2013-03-22 18:41:34 +0000134
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000135 // XformU7ToU7M1Imm - Return a target constant decremented by 1, in range
136 // [1..128], used in cmpb.gtu instructions.
137 inline SDValue XformU7ToU7M1Imm(signed Imm) {
138 assert((Imm >= 1 && Imm <= 128) && "Constant out of range for cmpb op");
139 return CurDAG->getTargetConstant(Imm - 1, MVT::i8);
140 }
Jyotsna Vermafdc660b2013-03-22 18:41:34 +0000141
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000142 // XformS8ToS8M1Imm - Return a target constant decremented by 1.
143 inline SDValue XformSToSM1Imm(signed Imm) {
144 return CurDAG->getTargetConstant(Imm - 1, MVT::i32);
145 }
Jyotsna Verma60316252013-02-05 19:20:45 +0000146
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000147 // XformU8ToU8M1Imm - Return a target constant decremented by 1.
148 inline SDValue XformUToUM1Imm(unsigned Imm) {
149 assert((Imm >= 1) && "Cannot decrement unsigned int less than 1");
150 return CurDAG->getTargetConstant(Imm - 1, MVT::i32);
151 }
Jyotsna Verma89c84822013-04-23 19:15:55 +0000152
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000153 // XformSToSM2Imm - Return a target constant decremented by 2.
154 inline SDValue XformSToSM2Imm(unsigned Imm) {
155 return CurDAG->getTargetConstant(Imm - 2, MVT::i32);
156 }
Jyotsna Verma89c84822013-04-23 19:15:55 +0000157
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000158 // XformSToSM3Imm - Return a target constant decremented by 3.
159 inline SDValue XformSToSM3Imm(unsigned Imm) {
160 return CurDAG->getTargetConstant(Imm - 3, MVT::i32);
161 }
Colin LeMahieu19ed07c2015-01-28 18:29:11 +0000162
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000163 // Include the pieces autogenerated from the target description.
164 #include "HexagonGenDAGISel.inc"
Colin LeMahieu0ee02fc2015-01-19 20:31:18 +0000165
166private:
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000167 bool isValueExtension(const SDValue &Val, unsigned FromBits, SDValue &Src);
168}; // end HexagonDAGToDAGISel
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000169} // end anonymous namespace
170
171
172/// createHexagonISelDag - This pass converts a legalized DAG into a
173/// Hexagon-specific DAG, ready for instruction scheduling.
174///
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000175namespace llvm {
176FunctionPass *createHexagonISelDag(HexagonTargetMachine &TM,
177 CodeGenOpt::Level OptLevel) {
Jyotsna Vermad9225242013-02-13 21:38:46 +0000178 return new HexagonDAGToDAGISel(TM, OptLevel);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000179}
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000180}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000181
Jyotsna Vermad9225242013-02-13 21:38:46 +0000182static void initializePassOnce(PassRegistry &Registry) {
183 const char *Name = "Hexagon DAG->DAG Pattern Instruction Selection";
184 PassInfo *PI = new PassInfo(Name, "hexagon-isel",
Craig Topper062a2ba2014-04-25 05:30:21 +0000185 &SelectionDAGISel::ID, nullptr, false, false);
Jyotsna Vermad9225242013-02-13 21:38:46 +0000186 Registry.registerPass(*PI, true);
187}
188
189void llvm::initializeHexagonDAGToDAGISelPass(PassRegistry &Registry) {
190 CALL_ONCE_INITIALIZATION(initializePassOnce)
191}
192
193
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000194// Intrinsics that return a a predicate.
195static unsigned doesIntrinsicReturnPredicate(unsigned ID)
196{
197 switch (ID) {
198 default:
199 return 0;
200 case Intrinsic::hexagon_C2_cmpeq:
201 case Intrinsic::hexagon_C2_cmpgt:
202 case Intrinsic::hexagon_C2_cmpgtu:
203 case Intrinsic::hexagon_C2_cmpgtup:
204 case Intrinsic::hexagon_C2_cmpgtp:
205 case Intrinsic::hexagon_C2_cmpeqp:
206 case Intrinsic::hexagon_C2_bitsset:
207 case Intrinsic::hexagon_C2_bitsclr:
208 case Intrinsic::hexagon_C2_cmpeqi:
209 case Intrinsic::hexagon_C2_cmpgti:
210 case Intrinsic::hexagon_C2_cmpgtui:
211 case Intrinsic::hexagon_C2_cmpgei:
212 case Intrinsic::hexagon_C2_cmpgeui:
213 case Intrinsic::hexagon_C2_cmplt:
214 case Intrinsic::hexagon_C2_cmpltu:
215 case Intrinsic::hexagon_C2_bitsclri:
216 case Intrinsic::hexagon_C2_and:
217 case Intrinsic::hexagon_C2_or:
218 case Intrinsic::hexagon_C2_xor:
219 case Intrinsic::hexagon_C2_andn:
220 case Intrinsic::hexagon_C2_not:
221 case Intrinsic::hexagon_C2_orn:
222 case Intrinsic::hexagon_C2_pxfer_map:
223 case Intrinsic::hexagon_C2_any8:
224 case Intrinsic::hexagon_C2_all8:
225 case Intrinsic::hexagon_A2_vcmpbeq:
226 case Intrinsic::hexagon_A2_vcmpbgtu:
227 case Intrinsic::hexagon_A2_vcmpheq:
228 case Intrinsic::hexagon_A2_vcmphgt:
229 case Intrinsic::hexagon_A2_vcmphgtu:
230 case Intrinsic::hexagon_A2_vcmpweq:
231 case Intrinsic::hexagon_A2_vcmpwgt:
232 case Intrinsic::hexagon_A2_vcmpwgtu:
233 case Intrinsic::hexagon_C2_tfrrp:
234 case Intrinsic::hexagon_S2_tstbit_i:
235 case Intrinsic::hexagon_S2_tstbit_r:
236 return 1;
237 }
238}
239
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000240SDNode *HexagonDAGToDAGISel::SelectIndexedLoadSignExtend64(LoadSDNode *LD,
241 unsigned Opcode,
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000242 SDLoc dl) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000243 SDValue Chain = LD->getChain();
244 EVT LoadedVT = LD->getMemoryVT();
245 SDValue Base = LD->getBasePtr();
246 SDValue Offset = LD->getOffset();
247 SDNode *OffsetNode = Offset.getNode();
248 int32_t Val = cast<ConstantSDNode>(OffsetNode)->getSExtValue();
Bill Wendling4a7a4082013-06-07 06:19:56 +0000249
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000250 const HexagonInstrInfo &TII = *HST.getInstrInfo();
251 if (TII.isValidAutoIncImm(LoadedVT, Val)) {
252 SDValue TargetConst = CurDAG->getTargetConstant(Val, MVT::i32);
253 SDNode *Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::i32, MVT::i32,
254 MVT::Other, Base, TargetConst,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000255 Chain);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000256 SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A2_sxtw, dl, MVT::i64,
257 SDValue(Result_1, 0));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000258 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
259 MemOp[0] = LD->getMemOperand();
260 cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
261 const SDValue Froms[] = { SDValue(LD, 0),
262 SDValue(LD, 1),
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000263 SDValue(LD, 2) };
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000264 const SDValue Tos[] = { SDValue(Result_2, 0),
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000265 SDValue(Result_1, 1),
266 SDValue(Result_1, 2) };
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000267 ReplaceUses(Froms, Tos, 3);
268 return Result_2;
269 }
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000270
271 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
272 SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
273 SDNode *Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::i32, MVT::Other,
274 Base, TargetConst0, Chain);
275 SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A2_sxtw, dl, MVT::i64,
276 SDValue(Result_1, 0));
277 SDNode* Result_3 = CurDAG->getMachineNode(Hexagon::A2_addi, dl, MVT::i32,
278 Base, TargetConstVal,
279 SDValue(Result_1, 1));
280 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
281 MemOp[0] = LD->getMemOperand();
282 cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
283 const SDValue Froms[] = { SDValue(LD, 0),
284 SDValue(LD, 1),
285 SDValue(LD, 2) };
286 const SDValue Tos[] = { SDValue(Result_2, 0),
287 SDValue(Result_3, 0),
288 SDValue(Result_1, 1) };
289 ReplaceUses(Froms, Tos, 3);
290 return Result_2;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000291}
292
293
294SDNode *HexagonDAGToDAGISel::SelectIndexedLoadZeroExtend64(LoadSDNode *LD,
295 unsigned Opcode,
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000296 SDLoc dl) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000297 SDValue Chain = LD->getChain();
298 EVT LoadedVT = LD->getMemoryVT();
299 SDValue Base = LD->getBasePtr();
300 SDValue Offset = LD->getOffset();
301 SDNode *OffsetNode = Offset.getNode();
302 int32_t Val = cast<ConstantSDNode>(OffsetNode)->getSExtValue();
Bill Wendling4a7a4082013-06-07 06:19:56 +0000303
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000304 const HexagonInstrInfo &TII = *HST.getInstrInfo();
305 if (TII.isValidAutoIncImm(LoadedVT, Val)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000306 SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000307 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000308 SDNode *Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::i32,
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000309 MVT::i32, MVT::Other, Base,
310 TargetConstVal, Chain);
311 SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A4_combineir, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000312 MVT::i64, MVT::Other,
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000313 TargetConst0,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000314 SDValue(Result_1,0));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000315 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
316 MemOp[0] = LD->getMemOperand();
317 cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
318 const SDValue Froms[] = { SDValue(LD, 0),
319 SDValue(LD, 1),
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000320 SDValue(LD, 2) };
321 const SDValue Tos[] = { SDValue(Result_2, 0),
322 SDValue(Result_1, 1),
323 SDValue(Result_1, 2) };
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000324 ReplaceUses(Froms, Tos, 3);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000325 return Result_2;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000326 }
327
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000328 // Generate an indirect load.
329 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
330 SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
331 SDNode *Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::i32,
332 MVT::Other, Base, TargetConst0,
333 Chain);
334 SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A4_combineir, dl,
335 MVT::i64, MVT::Other,
336 TargetConst0,
337 SDValue(Result_1,0));
338 // Add offset to base.
339 SDNode* Result_3 = CurDAG->getMachineNode(Hexagon::A2_addi, dl, MVT::i32,
340 Base, TargetConstVal,
341 SDValue(Result_1, 1));
342 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
343 MemOp[0] = LD->getMemOperand();
344 cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
345 const SDValue Froms[] = { SDValue(LD, 0),
346 SDValue(LD, 1),
347 SDValue(LD, 2) };
348 const SDValue Tos[] = { SDValue(Result_2, 0), // Load value.
349 SDValue(Result_3, 0), // New address.
350 SDValue(Result_1, 1) };
351 ReplaceUses(Froms, Tos, 3);
352 return Result_2;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000353}
354
355
Andrew Trickef9de2a2013-05-25 02:42:55 +0000356SDNode *HexagonDAGToDAGISel::SelectIndexedLoad(LoadSDNode *LD, SDLoc dl) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000357 SDValue Chain = LD->getChain();
358 SDValue Base = LD->getBasePtr();
359 SDValue Offset = LD->getOffset();
360 SDNode *OffsetNode = Offset.getNode();
361 // Get the constant value.
362 int32_t Val = cast<ConstantSDNode>(OffsetNode)->getSExtValue();
363 EVT LoadedVT = LD->getMemoryVT();
364 unsigned Opcode = 0;
365
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000366 // Check for zero extended loads. Treat any-extend loads as zero extended
367 // loads.
368 ISD::LoadExtType ExtType = LD->getExtensionType();
369 bool IsZeroExt = (ExtType == ISD::ZEXTLOAD || ExtType == ISD::EXTLOAD);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000370
371 // Figure out the opcode.
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000372 const HexagonInstrInfo &TII = *HST.getInstrInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000373 if (LoadedVT == MVT::i64) {
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000374 if (TII.isValidAutoIncImm(LoadedVT, Val))
Colin LeMahieuc83cbbf2014-12-26 19:31:46 +0000375 Opcode = Hexagon::L2_loadrd_pi;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000376 else
Colin LeMahieu947cd702014-12-23 20:44:59 +0000377 Opcode = Hexagon::L2_loadrd_io;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000378 } else if (LoadedVT == MVT::i32) {
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000379 if (TII.isValidAutoIncImm(LoadedVT, Val))
Colin LeMahieuc83cbbf2014-12-26 19:31:46 +0000380 Opcode = Hexagon::L2_loadri_pi;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000381 else
Colin LeMahieu026e88d2014-12-23 20:02:16 +0000382 Opcode = Hexagon::L2_loadri_io;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000383 } else if (LoadedVT == MVT::i16) {
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000384 if (TII.isValidAutoIncImm(LoadedVT, Val))
385 Opcode = IsZeroExt ? Hexagon::L2_loadruh_pi : Hexagon::L2_loadrh_pi;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000386 else
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000387 Opcode = IsZeroExt ? Hexagon::L2_loadruh_io : Hexagon::L2_loadrh_io;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000388 } else if (LoadedVT == MVT::i8) {
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000389 if (TII.isValidAutoIncImm(LoadedVT, Val))
390 Opcode = IsZeroExt ? Hexagon::L2_loadrub_pi : Hexagon::L2_loadrb_pi;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000391 else
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000392 Opcode = IsZeroExt ? Hexagon::L2_loadrub_io : Hexagon::L2_loadrb_io;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000393 } else
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000394 llvm_unreachable("unknown memory type");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000395
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000396 // For zero extended i64 loads, we need to add combine instructions.
397 if (LD->getValueType(0) == MVT::i64 && IsZeroExt)
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000398 return SelectIndexedLoadZeroExtend64(LD, Opcode, dl);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000399 // Handle sign extended i64 loads.
400 if (LD->getValueType(0) == MVT::i64 && ExtType == ISD::SEXTLOAD)
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000401 return SelectIndexedLoadSignExtend64(LD, Opcode, dl);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000402
403 if (TII.isValidAutoIncImm(LoadedVT, Val)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000404 SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
405 SDNode* Result = CurDAG->getMachineNode(Opcode, dl,
406 LD->getValueType(0),
407 MVT::i32, MVT::Other, Base,
408 TargetConstVal, Chain);
409 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
410 MemOp[0] = LD->getMemOperand();
411 cast<MachineSDNode>(Result)->setMemRefs(MemOp, MemOp + 1);
412 const SDValue Froms[] = { SDValue(LD, 0),
413 SDValue(LD, 1),
414 SDValue(LD, 2)
415 };
416 const SDValue Tos[] = { SDValue(Result, 0),
417 SDValue(Result, 1),
418 SDValue(Result, 2)
419 };
420 ReplaceUses(Froms, Tos, 3);
421 return Result;
422 } else {
423 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
424 SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
425 SDNode* Result_1 = CurDAG->getMachineNode(Opcode, dl,
426 LD->getValueType(0),
427 MVT::Other, Base, TargetConst0,
428 Chain);
Colin LeMahieuf297dbe2015-02-05 17:49:13 +0000429 SDNode* Result_2 = CurDAG->getMachineNode(Hexagon::A2_addi, dl, MVT::i32,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000430 Base, TargetConstVal,
431 SDValue(Result_1, 1));
432 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
433 MemOp[0] = LD->getMemOperand();
434 cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
435 const SDValue Froms[] = { SDValue(LD, 0),
436 SDValue(LD, 1),
437 SDValue(LD, 2)
438 };
439 const SDValue Tos[] = { SDValue(Result_1, 0),
440 SDValue(Result_2, 0),
441 SDValue(Result_1, 1)
442 };
443 ReplaceUses(Froms, Tos, 3);
444 return Result_1;
445 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000446}
447
448
449SDNode *HexagonDAGToDAGISel::SelectLoad(SDNode *N) {
450 SDNode *result;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000451 SDLoc dl(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000452 LoadSDNode *LD = cast<LoadSDNode>(N);
453 ISD::MemIndexedMode AM = LD->getAddressingMode();
454
455 // Handle indexed loads.
456 if (AM != ISD::UNINDEXED) {
457 result = SelectIndexedLoad(LD, dl);
458 } else {
Colin LeMahieu2efa2d02015-03-09 21:48:13 +0000459 result = SelectCode(LD);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000460 }
461
462 return result;
463}
464
465
Andrew Trickef9de2a2013-05-25 02:42:55 +0000466SDNode *HexagonDAGToDAGISel::SelectIndexedStore(StoreSDNode *ST, SDLoc dl) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000467 SDValue Chain = ST->getChain();
468 SDValue Base = ST->getBasePtr();
469 SDValue Offset = ST->getOffset();
470 SDValue Value = ST->getValue();
471 SDNode *OffsetNode = Offset.getNode();
472 // Get the constant value.
473 int32_t Val = cast<ConstantSDNode>(OffsetNode)->getSExtValue();
474 EVT StoredVT = ST->getMemoryVT();
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000475 EVT ValueVT = Value.getValueType();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000476
477 // Offset value must be within representable range
478 // and must have correct alignment properties.
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000479 const HexagonInstrInfo &TII = *HST.getInstrInfo();
480 if (TII.isValidAutoIncImm(StoredVT, Val)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000481 unsigned Opcode = 0;
482
483 // Figure out the post inc version of opcode.
Colin LeMahieu9a3cd3f2014-12-29 20:00:43 +0000484 if (StoredVT == MVT::i64) Opcode = Hexagon::S2_storerd_pi;
485 else if (StoredVT == MVT::i32) Opcode = Hexagon::S2_storeri_pi;
486 else if (StoredVT == MVT::i16) Opcode = Hexagon::S2_storerh_pi;
Colin LeMahieu3d34afb2014-12-29 19:42:14 +0000487 else if (StoredVT == MVT::i8) Opcode = Hexagon::S2_storerb_pi;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000488 else llvm_unreachable("unknown memory type");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000489
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000490 if (ST->isTruncatingStore() && ValueVT.getSizeInBits() == 64) {
491 assert(StoredVT.getSizeInBits() < 64 && "Not a truncating store");
492 Value = CurDAG->getTargetExtractSubreg(Hexagon::subreg_loreg,
493 dl, MVT::i32, Value);
494 }
495 SDValue Ops[] = {Base, CurDAG->getTargetConstant(Val, MVT::i32), Value,
496 Chain};
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000497 // Build post increment store.
498 SDNode* Result = CurDAG->getMachineNode(Opcode, dl, MVT::i32,
Michael Liaob53d8962013-04-19 22:22:57 +0000499 MVT::Other, Ops);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000500 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
501 MemOp[0] = ST->getMemOperand();
502 cast<MachineSDNode>(Result)->setMemRefs(MemOp, MemOp + 1);
503
504 ReplaceUses(ST, Result);
505 ReplaceUses(SDValue(ST,1), SDValue(Result,1));
506 return Result;
507 }
508
509 // Note: Order of operands matches the def of instruction:
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000510 // def S2_storerd_io
511 // : STInst<(outs), (ins IntRegs:$base, imm:$offset, DoubleRegs:$src1), ...
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000512 // and it differs for POST_ST* for instance.
513 SDValue Ops[] = { Base, CurDAG->getTargetConstant(0, MVT::i32), Value,
514 Chain};
515 unsigned Opcode = 0;
516
517 // Figure out the opcode.
Colin LeMahieubda31b42014-12-29 20:44:51 +0000518 if (StoredVT == MVT::i64) Opcode = Hexagon::S2_storerd_io;
519 else if (StoredVT == MVT::i32) Opcode = Hexagon::S2_storeri_io;
520 else if (StoredVT == MVT::i16) Opcode = Hexagon::S2_storerh_io;
521 else if (StoredVT == MVT::i8) Opcode = Hexagon::S2_storerb_io;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000522 else llvm_unreachable("unknown memory type");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000523
524 // Build regular store.
525 SDValue TargetConstVal = CurDAG->getTargetConstant(Val, MVT::i32);
Michael Liaob53d8962013-04-19 22:22:57 +0000526 SDNode* Result_1 = CurDAG->getMachineNode(Opcode, dl, MVT::Other, Ops);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000527 // Build splitted incriment instruction.
Colin LeMahieuf297dbe2015-02-05 17:49:13 +0000528 SDNode* Result_2 = CurDAG->getMachineNode(Hexagon::A2_addi, dl, MVT::i32,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000529 Base,
530 TargetConstVal,
531 SDValue(Result_1, 0));
532 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
533 MemOp[0] = ST->getMemOperand();
534 cast<MachineSDNode>(Result_1)->setMemRefs(MemOp, MemOp + 1);
535
536 ReplaceUses(SDValue(ST,0), SDValue(Result_2,0));
537 ReplaceUses(SDValue(ST,1), SDValue(Result_1,0));
538 return Result_2;
539}
540
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000541SDNode *HexagonDAGToDAGISel::SelectStore(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000542 SDLoc dl(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000543 StoreSDNode *ST = cast<StoreSDNode>(N);
544 ISD::MemIndexedMode AM = ST->getAddressingMode();
545
546 // Handle indexed stores.
547 if (AM != ISD::UNINDEXED) {
548 return SelectIndexedStore(ST, dl);
549 }
Sirish Pandec92c3162012-05-03 16:18:50 +0000550
Colin LeMahieu2efa2d02015-03-09 21:48:13 +0000551 return SelectCode(ST);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000552}
553
554SDNode *HexagonDAGToDAGISel::SelectMul(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000555 SDLoc dl(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000556
557 //
558 // %conv.i = sext i32 %tmp1 to i64
559 // %conv2.i = sext i32 %add to i64
560 // %mul.i = mul nsw i64 %conv2.i, %conv.i
561 //
562 // --- match with the following ---
563 //
564 // %mul.i = mpy (%tmp1, %add)
565 //
566
567 if (N->getValueType(0) == MVT::i64) {
568 // Shifting a i64 signed multiply.
569 SDValue MulOp0 = N->getOperand(0);
570 SDValue MulOp1 = N->getOperand(1);
571
572 SDValue OP0;
573 SDValue OP1;
574
575 // Handle sign_extend and sextload.
576 if (MulOp0.getOpcode() == ISD::SIGN_EXTEND) {
577 SDValue Sext0 = MulOp0.getOperand(0);
578 if (Sext0.getNode()->getValueType(0) != MVT::i32) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000579 return SelectCode(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000580 }
581
582 OP0 = Sext0;
583 } else if (MulOp0.getOpcode() == ISD::LOAD) {
584 LoadSDNode *LD = cast<LoadSDNode>(MulOp0.getNode());
585 if (LD->getMemoryVT() != MVT::i32 ||
586 LD->getExtensionType() != ISD::SEXTLOAD ||
587 LD->getAddressingMode() != ISD::UNINDEXED) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000588 return SelectCode(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000589 }
590
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000591 SDValue Chain = LD->getChain();
592 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
Colin LeMahieu026e88d2014-12-23 20:02:16 +0000593 OP0 = SDValue(CurDAG->getMachineNode(Hexagon::L2_loadri_io, dl, MVT::i32,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000594 MVT::Other,
595 LD->getBasePtr(), TargetConst0,
596 Chain), 0);
597 } else {
598 return SelectCode(N);
599 }
600
601 // Same goes for the second operand.
602 if (MulOp1.getOpcode() == ISD::SIGN_EXTEND) {
603 SDValue Sext1 = MulOp1.getOperand(0);
604 if (Sext1.getNode()->getValueType(0) != MVT::i32) {
605 return SelectCode(N);
606 }
607
608 OP1 = Sext1;
609 } else if (MulOp1.getOpcode() == ISD::LOAD) {
610 LoadSDNode *LD = cast<LoadSDNode>(MulOp1.getNode());
611 if (LD->getMemoryVT() != MVT::i32 ||
612 LD->getExtensionType() != ISD::SEXTLOAD ||
613 LD->getAddressingMode() != ISD::UNINDEXED) {
614 return SelectCode(N);
615 }
616
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000617 SDValue Chain = LD->getChain();
618 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
Colin LeMahieu026e88d2014-12-23 20:02:16 +0000619 OP1 = SDValue(CurDAG->getMachineNode(Hexagon::L2_loadri_io, dl, MVT::i32,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000620 MVT::Other,
621 LD->getBasePtr(), TargetConst0,
622 Chain), 0);
623 } else {
624 return SelectCode(N);
625 }
626
627 // Generate a mpy instruction.
Colin LeMahieud9b23502014-12-16 16:10:01 +0000628 SDNode *Result = CurDAG->getMachineNode(Hexagon::M2_dpmpyss_s0, dl, MVT::i64,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000629 OP0, OP1);
630 ReplaceUses(N, Result);
631 return Result;
632 }
633
634 return SelectCode(N);
635}
636
637
638SDNode *HexagonDAGToDAGISel::SelectSelect(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000639 SDLoc dl(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000640 SDValue N0 = N->getOperand(0);
641 if (N0.getOpcode() == ISD::SETCC) {
642 SDValue N00 = N0.getOperand(0);
643 if (N00.getOpcode() == ISD::SIGN_EXTEND_INREG) {
644 SDValue N000 = N00.getOperand(0);
645 SDValue N001 = N00.getOperand(1);
646 if (cast<VTSDNode>(N001)->getVT() == MVT::i16) {
647 SDValue N01 = N0.getOperand(1);
648 SDValue N02 = N0.getOperand(2);
649
650 // Pattern: (select:i32 (setcc:i1 (sext_inreg:i32 IntRegs:i32:$src2,
651 // i16:Other),IntRegs:i32:$src1, SETLT:Other),IntRegs:i32:$src1,
652 // IntRegs:i32:$src2)
653 // Emits: (MAXh_rr:i32 IntRegs:i32:$src1, IntRegs:i32:$src2)
654 // Pattern complexity = 9 cost = 1 size = 0.
655 if (cast<CondCodeSDNode>(N02)->get() == ISD::SETLT) {
656 SDValue N1 = N->getOperand(1);
657 if (N01 == N1) {
658 SDValue N2 = N->getOperand(2);
659 if (N000 == N2 &&
660 N0.getNode()->getValueType(N0.getResNo()) == MVT::i1 &&
661 N00.getNode()->getValueType(N00.getResNo()) == MVT::i32) {
Colin LeMahieu310991c2014-11-21 21:54:59 +0000662 SDNode *SextNode = CurDAG->getMachineNode(Hexagon::A2_sxth, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000663 MVT::i32, N000);
Colin LeMahieu5cf56322014-12-08 23:55:43 +0000664 SDNode *Result = CurDAG->getMachineNode(Hexagon::A2_max, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000665 MVT::i32,
666 SDValue(SextNode, 0),
667 N1);
668 ReplaceUses(N, Result);
669 return Result;
670 }
671 }
672 }
673
674 // Pattern: (select:i32 (setcc:i1 (sext_inreg:i32 IntRegs:i32:$src2,
675 // i16:Other), IntRegs:i32:$src1, SETGT:Other), IntRegs:i32:$src1,
676 // IntRegs:i32:$src2)
677 // Emits: (MINh_rr:i32 IntRegs:i32:$src1, IntRegs:i32:$src2)
678 // Pattern complexity = 9 cost = 1 size = 0.
679 if (cast<CondCodeSDNode>(N02)->get() == ISD::SETGT) {
680 SDValue N1 = N->getOperand(1);
681 if (N01 == N1) {
682 SDValue N2 = N->getOperand(2);
683 if (N000 == N2 &&
684 N0.getNode()->getValueType(N0.getResNo()) == MVT::i1 &&
685 N00.getNode()->getValueType(N00.getResNo()) == MVT::i32) {
Colin LeMahieu310991c2014-11-21 21:54:59 +0000686 SDNode *SextNode = CurDAG->getMachineNode(Hexagon::A2_sxth, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000687 MVT::i32, N000);
Colin LeMahieu5cf56322014-12-08 23:55:43 +0000688 SDNode *Result = CurDAG->getMachineNode(Hexagon::A2_min, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000689 MVT::i32,
690 SDValue(SextNode, 0),
691 N1);
692 ReplaceUses(N, Result);
693 return Result;
694 }
695 }
696 }
697 }
698 }
699 }
700
701 return SelectCode(N);
702}
703
704
705SDNode *HexagonDAGToDAGISel::SelectTruncate(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000706 SDLoc dl(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000707 SDValue Shift = N->getOperand(0);
708
709 //
710 // %conv.i = sext i32 %tmp1 to i64
711 // %conv2.i = sext i32 %add to i64
712 // %mul.i = mul nsw i64 %conv2.i, %conv.i
713 // %shr5.i = lshr i64 %mul.i, 32
714 // %conv3.i = trunc i64 %shr5.i to i32
715 //
716 // --- match with the following ---
717 //
718 // %conv3.i = mpy (%tmp1, %add)
719 //
720 // Trunc to i32.
721 if (N->getValueType(0) == MVT::i32) {
722 // Trunc from i64.
723 if (Shift.getNode()->getValueType(0) == MVT::i64) {
724 // Trunc child is logical shift right.
725 if (Shift.getOpcode() != ISD::SRL) {
726 return SelectCode(N);
727 }
728
729 SDValue ShiftOp0 = Shift.getOperand(0);
730 SDValue ShiftOp1 = Shift.getOperand(1);
731
732 // Shift by const 32
733 if (ShiftOp1.getOpcode() != ISD::Constant) {
734 return SelectCode(N);
735 }
736
737 int32_t ShiftConst =
738 cast<ConstantSDNode>(ShiftOp1.getNode())->getSExtValue();
739 if (ShiftConst != 32) {
740 return SelectCode(N);
741 }
742
743 // Shifting a i64 signed multiply
744 SDValue Mul = ShiftOp0;
745 if (Mul.getOpcode() != ISD::MUL) {
746 return SelectCode(N);
747 }
748
749 SDValue MulOp0 = Mul.getOperand(0);
750 SDValue MulOp1 = Mul.getOperand(1);
751
752 SDValue OP0;
753 SDValue OP1;
754
755 // Handle sign_extend and sextload
756 if (MulOp0.getOpcode() == ISD::SIGN_EXTEND) {
757 SDValue Sext0 = MulOp0.getOperand(0);
758 if (Sext0.getNode()->getValueType(0) != MVT::i32) {
759 return SelectCode(N);
760 }
761
762 OP0 = Sext0;
763 } else if (MulOp0.getOpcode() == ISD::LOAD) {
764 LoadSDNode *LD = cast<LoadSDNode>(MulOp0.getNode());
765 if (LD->getMemoryVT() != MVT::i32 ||
766 LD->getExtensionType() != ISD::SEXTLOAD ||
767 LD->getAddressingMode() != ISD::UNINDEXED) {
768 return SelectCode(N);
769 }
770
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000771 SDValue Chain = LD->getChain();
772 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
Colin LeMahieu026e88d2014-12-23 20:02:16 +0000773 OP0 = SDValue(CurDAG->getMachineNode(Hexagon::L2_loadri_io, dl, MVT::i32,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000774 MVT::Other,
775 LD->getBasePtr(),
776 TargetConst0, Chain), 0);
777 } else {
778 return SelectCode(N);
779 }
780
781 // Same goes for the second operand.
782 if (MulOp1.getOpcode() == ISD::SIGN_EXTEND) {
783 SDValue Sext1 = MulOp1.getOperand(0);
784 if (Sext1.getNode()->getValueType(0) != MVT::i32)
785 return SelectCode(N);
786
787 OP1 = Sext1;
788 } else if (MulOp1.getOpcode() == ISD::LOAD) {
789 LoadSDNode *LD = cast<LoadSDNode>(MulOp1.getNode());
790 if (LD->getMemoryVT() != MVT::i32 ||
791 LD->getExtensionType() != ISD::SEXTLOAD ||
792 LD->getAddressingMode() != ISD::UNINDEXED) {
793 return SelectCode(N);
794 }
795
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000796 SDValue Chain = LD->getChain();
797 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
Colin LeMahieu026e88d2014-12-23 20:02:16 +0000798 OP1 = SDValue(CurDAG->getMachineNode(Hexagon::L2_loadri_io, dl, MVT::i32,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000799 MVT::Other,
800 LD->getBasePtr(),
801 TargetConst0, Chain), 0);
802 } else {
803 return SelectCode(N);
804 }
805
806 // Generate a mpy instruction.
Colin LeMahieud9b23502014-12-16 16:10:01 +0000807 SDNode *Result = CurDAG->getMachineNode(Hexagon::M2_mpy_up, dl, MVT::i32,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000808 OP0, OP1);
809 ReplaceUses(N, Result);
810 return Result;
811 }
812 }
813
814 return SelectCode(N);
815}
816
817
818SDNode *HexagonDAGToDAGISel::SelectSHL(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000819 SDLoc dl(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000820 if (N->getValueType(0) == MVT::i32) {
821 SDValue Shl_0 = N->getOperand(0);
822 SDValue Shl_1 = N->getOperand(1);
823 // RHS is const.
824 if (Shl_1.getOpcode() == ISD::Constant) {
825 if (Shl_0.getOpcode() == ISD::MUL) {
826 SDValue Mul_0 = Shl_0.getOperand(0); // Val
827 SDValue Mul_1 = Shl_0.getOperand(1); // Const
828 // RHS of mul is const.
829 if (Mul_1.getOpcode() == ISD::Constant) {
830 int32_t ShlConst =
831 cast<ConstantSDNode>(Shl_1.getNode())->getSExtValue();
832 int32_t MulConst =
833 cast<ConstantSDNode>(Mul_1.getNode())->getSExtValue();
834 int32_t ValConst = MulConst << ShlConst;
835 SDValue Val = CurDAG->getTargetConstant(ValConst,
836 MVT::i32);
837 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Val.getNode()))
838 if (isInt<9>(CN->getSExtValue())) {
839 SDNode* Result =
Colin LeMahieud9b23502014-12-16 16:10:01 +0000840 CurDAG->getMachineNode(Hexagon::M2_mpysmi, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000841 MVT::i32, Mul_0, Val);
842 ReplaceUses(N, Result);
843 return Result;
844 }
845
846 }
847 } else if (Shl_0.getOpcode() == ISD::SUB) {
848 SDValue Sub_0 = Shl_0.getOperand(0); // Const 0
849 SDValue Sub_1 = Shl_0.getOperand(1); // Val
850 if (Sub_0.getOpcode() == ISD::Constant) {
851 int32_t SubConst =
852 cast<ConstantSDNode>(Sub_0.getNode())->getSExtValue();
853 if (SubConst == 0) {
854 if (Sub_1.getOpcode() == ISD::SHL) {
855 SDValue Shl2_0 = Sub_1.getOperand(0); // Val
856 SDValue Shl2_1 = Sub_1.getOperand(1); // Const
857 if (Shl2_1.getOpcode() == ISD::Constant) {
858 int32_t ShlConst =
859 cast<ConstantSDNode>(Shl_1.getNode())->getSExtValue();
860 int32_t Shl2Const =
861 cast<ConstantSDNode>(Shl2_1.getNode())->getSExtValue();
862 int32_t ValConst = 1 << (ShlConst+Shl2Const);
863 SDValue Val = CurDAG->getTargetConstant(-ValConst, MVT::i32);
864 if (ConstantSDNode *CN =
865 dyn_cast<ConstantSDNode>(Val.getNode()))
866 if (isInt<9>(CN->getSExtValue())) {
867 SDNode* Result =
Colin LeMahieud9b23502014-12-16 16:10:01 +0000868 CurDAG->getMachineNode(Hexagon::M2_mpysmi, dl, MVT::i32,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000869 Shl2_0, Val);
870 ReplaceUses(N, Result);
871 return Result;
872 }
873 }
874 }
875 }
876 }
877 }
878 }
879 }
880 return SelectCode(N);
881}
882
883
884//
885// If there is an zero_extend followed an intrinsic in DAG (this means - the
886// result of the intrinsic is predicate); convert the zero_extend to
887// transfer instruction.
888//
889// Zero extend -> transfer is lowered here. Otherwise, zero_extend will be
890// converted into a MUX as predicate registers defined as 1 bit in the
891// compiler. Architecture defines them as 8-bit registers.
892// We want to preserve all the lower 8-bits and, not just 1 LSB bit.
893//
894SDNode *HexagonDAGToDAGISel::SelectZeroExtend(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000895 SDLoc dl(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000896 SDNode *IsIntrinsic = N->getOperand(0).getNode();
897 if ((IsIntrinsic->getOpcode() == ISD::INTRINSIC_WO_CHAIN)) {
898 unsigned ID =
899 cast<ConstantSDNode>(IsIntrinsic->getOperand(0))->getZExtValue();
900 if (doesIntrinsicReturnPredicate(ID)) {
901 // Now we need to differentiate target data types.
902 if (N->getValueType(0) == MVT::i64) {
Krzysztof Parzyszekae14e7b2015-03-17 21:47:16 +0000903 // Convert the zero_extend to Rs = Pd followed by A2_combinew(0,Rs).
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000904 SDValue TargetConst0 = CurDAG->getTargetConstant(0, MVT::i32);
Colin LeMahieu30dcb232014-12-09 18:16:49 +0000905 SDNode *Result_1 = CurDAG->getMachineNode(Hexagon::C2_tfrpr, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000906 MVT::i32,
907 SDValue(IsIntrinsic, 0));
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000908 SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A2_tfrsi, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000909 MVT::i32,
910 TargetConst0);
Colin LeMahieub580d7d2014-12-09 19:23:45 +0000911 SDNode *Result_3 = CurDAG->getMachineNode(Hexagon::A2_combinew, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000912 MVT::i64, MVT::Other,
913 SDValue(Result_2, 0),
914 SDValue(Result_1, 0));
915 ReplaceUses(N, Result_3);
916 return Result_3;
917 }
918 if (N->getValueType(0) == MVT::i32) {
919 // Convert the zero_extend to Rs = Pd
Colin LeMahieu30dcb232014-12-09 18:16:49 +0000920 SDNode* RsPd = CurDAG->getMachineNode(Hexagon::C2_tfrpr, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000921 MVT::i32,
922 SDValue(IsIntrinsic, 0));
923 ReplaceUses(N, RsPd);
924 return RsPd;
925 }
Craig Toppere55c5562012-02-07 02:50:20 +0000926 llvm_unreachable("Unexpected value type");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000927 }
928 }
929 return SelectCode(N);
930}
931
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000932//
933// Checking for intrinsics which have predicate registers as operand(s)
934// and lowering to the actual intrinsic.
935//
936SDNode *HexagonDAGToDAGISel::SelectIntrinsicWOChain(SDNode *N) {
Colin LeMahieu0ee02fc2015-01-19 20:31:18 +0000937 unsigned IID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
938 unsigned Bits;
939 switch (IID) {
940 case Intrinsic::hexagon_S2_vsplatrb:
941 Bits = 8;
942 break;
943 case Intrinsic::hexagon_S2_vsplatrh:
944 Bits = 16;
945 break;
946 default:
947 return SelectCode(N);
948 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000949
Colin LeMahieu0ee02fc2015-01-19 20:31:18 +0000950 SDValue const &V = N->getOperand(1);
951 SDValue U;
952 if (isValueExtension(V, Bits, U)) {
953 SDValue R = CurDAG->getNode(N->getOpcode(), SDLoc(N), N->getValueType(0),
954 N->getOperand(0), U);
955 return SelectCode(R.getNode());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000956 }
957 return SelectCode(N);
958}
959
Sirish Pande69295b82012-05-10 20:20:25 +0000960//
961// Map floating point constant values.
962//
963SDNode *HexagonDAGToDAGISel::SelectConstantFP(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000964 SDLoc dl(N);
Sirish Pande69295b82012-05-10 20:20:25 +0000965 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N);
966 APFloat APF = CN->getValueAPF();
967 if (N->getValueType(0) == MVT::f32) {
968 return CurDAG->getMachineNode(Hexagon::TFRI_f, dl, MVT::f32,
969 CurDAG->getTargetConstantFP(APF.convertToFloat(), MVT::f32));
970 }
971 else if (N->getValueType(0) == MVT::f64) {
972 return CurDAG->getMachineNode(Hexagon::CONST64_Float_Real, dl, MVT::f64,
973 CurDAG->getTargetConstantFP(APF.convertToDouble(), MVT::f64));
974 }
975
976 return SelectCode(N);
977}
978
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000979//
980// Map predicate true (encoded as -1 in LLVM) to a XOR.
981//
982SDNode *HexagonDAGToDAGISel::SelectConstant(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000983 SDLoc dl(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000984 if (N->getValueType(0) == MVT::i1) {
985 SDNode* Result;
986 int32_t Val = cast<ConstantSDNode>(N)->getSExtValue();
987 if (Val == -1) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000988 // Create the IntReg = 1 node.
989 SDNode* IntRegTFR =
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000990 CurDAG->getMachineNode(Hexagon::A2_tfrsi, dl, MVT::i32,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000991 CurDAG->getTargetConstant(0, MVT::i32));
992
993 // Pd = IntReg
Colin LeMahieu30dcb232014-12-09 18:16:49 +0000994 SDNode* Pd = CurDAG->getMachineNode(Hexagon::C2_tfrrp, dl, MVT::i1,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000995 SDValue(IntRegTFR, 0));
996
997 // not(Pd)
Colin LeMahieu5cf56322014-12-08 23:55:43 +0000998 SDNode* NotPd = CurDAG->getMachineNode(Hexagon::C2_not, dl, MVT::i1,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000999 SDValue(Pd, 0));
1000
1001 // xor(not(Pd))
Colin LeMahieu5cf56322014-12-08 23:55:43 +00001002 Result = CurDAG->getMachineNode(Hexagon::C2_xor, dl, MVT::i1,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001003 SDValue(Pd, 0), SDValue(NotPd, 0));
1004
1005 // We have just built:
1006 // Rs = Pd
1007 // Pd = xor(not(Pd), Pd)
1008
1009 ReplaceUses(N, Result);
1010 return Result;
1011 }
1012 }
1013
1014 return SelectCode(N);
1015}
1016
1017
1018//
1019// Map add followed by a asr -> asr +=.
1020//
1021SDNode *HexagonDAGToDAGISel::SelectAdd(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001022 SDLoc dl(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001023 if (N->getValueType(0) != MVT::i32) {
1024 return SelectCode(N);
1025 }
1026 // Identify nodes of the form: add(asr(...)).
1027 SDNode* Src1 = N->getOperand(0).getNode();
1028 if (Src1->getOpcode() != ISD::SRA || !Src1->hasOneUse()
1029 || Src1->getValueType(0) != MVT::i32) {
1030 return SelectCode(N);
1031 }
1032
1033 // Build Rd = Rd' + asr(Rs, Rt). The machine constraints will ensure that
1034 // Rd and Rd' are assigned to the same register
Colin LeMahieu0f850bd2014-12-19 20:29:29 +00001035 SDNode* Result = CurDAG->getMachineNode(Hexagon::S2_asr_r_r_acc, dl, MVT::i32,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001036 N->getOperand(1),
1037 Src1->getOperand(0),
1038 Src1->getOperand(1));
1039 ReplaceUses(N, Result);
1040
1041 return Result;
1042}
1043
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001044SDNode *HexagonDAGToDAGISel::SelectFrameIndex(SDNode *N) {
1045 int FX = cast<FrameIndexSDNode>(N)->getIndex();
1046 SDValue FI = CurDAG->getTargetFrameIndex(FX, MVT::i32);
1047 SDValue Zero = CurDAG->getTargetConstant(0, MVT::i32);
1048 SDLoc DL(N);
1049
1050 SDNode *R = CurDAG->getMachineNode(Hexagon::TFR_FI, DL, MVT::i32, FI, Zero);
1051
1052 if (N->getHasDebugValue())
1053 CurDAG->TransferDbgValues(SDValue(N, 0), SDValue(R, 0));
1054 return R;
1055}
1056
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001057
1058SDNode *HexagonDAGToDAGISel::Select(SDNode *N) {
Tim Northover31d093c2013-09-22 08:21:56 +00001059 if (N->isMachineOpcode()) {
1060 N->setNodeId(-1);
Craig Topper062a2ba2014-04-25 05:30:21 +00001061 return nullptr; // Already selected.
Tim Northover31d093c2013-09-22 08:21:56 +00001062 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001063
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001064 switch (N->getOpcode()) {
1065 case ISD::Constant:
1066 return SelectConstant(N);
1067
Sirish Pande69295b82012-05-10 20:20:25 +00001068 case ISD::ConstantFP:
1069 return SelectConstantFP(N);
1070
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001071 case ISD::FrameIndex:
1072 return SelectFrameIndex(N);
1073
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001074 case ISD::ADD:
1075 return SelectAdd(N);
1076
1077 case ISD::SHL:
1078 return SelectSHL(N);
1079
1080 case ISD::LOAD:
1081 return SelectLoad(N);
1082
1083 case ISD::STORE:
1084 return SelectStore(N);
1085
1086 case ISD::SELECT:
1087 return SelectSelect(N);
1088
1089 case ISD::TRUNCATE:
1090 return SelectTruncate(N);
1091
1092 case ISD::MUL:
1093 return SelectMul(N);
1094
1095 case ISD::ZERO_EXTEND:
1096 return SelectZeroExtend(N);
1097
1098 case ISD::INTRINSIC_WO_CHAIN:
1099 return SelectIntrinsicWOChain(N);
1100 }
1101
1102 return SelectCode(N);
1103}
1104
1105
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001106bool HexagonDAGToDAGISel::
Daniel Sanders60f1db02015-03-13 12:45:09 +00001107SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001108 std::vector<SDValue> &OutOps) {
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001109 SDValue Inp = Op, Res;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001110
Daniel Sanders60f1db02015-03-13 12:45:09 +00001111 switch (ConstraintID) {
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001112 default:
1113 return true;
Daniel Sanders49f643c2015-03-17 14:37:39 +00001114 case InlineAsm::Constraint_i:
1115 case InlineAsm::Constraint_o: // Offsetable.
1116 case InlineAsm::Constraint_v: // Not offsetable.
1117 case InlineAsm::Constraint_m: // Memory.
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001118 if (SelectAddrFI(Inp, Res))
1119 OutOps.push_back(Res);
1120 else
1121 OutOps.push_back(Inp);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001122 break;
1123 }
1124
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001125 OutOps.push_back(CurDAG->getTargetConstant(0, MVT::i32));
Jyotsna Vermad9225242013-02-13 21:38:46 +00001126 return false;
1127}
Colin LeMahieuc7522f32015-01-14 23:07:36 +00001128
Krzysztof Parzyszekae14e7b2015-03-17 21:47:16 +00001129void HexagonDAGToDAGISel::PreprocessISelDAG() {
1130 SelectionDAG &DAG = *CurDAG;
1131 std::vector<SDNode*> Nodes;
1132 for (auto I = DAG.allnodes_begin(), E = DAG.allnodes_end(); I != E; ++I)
1133 Nodes.push_back(I);
1134
1135 // Simplify: (or (select c x 0) z) -> (select c (or x z) z)
1136 // (or (select c 0 y) z) -> (select c z (or y z))
1137 // This may not be the right thing for all targets, so do it here.
1138 for (auto I: Nodes) {
1139 if (I->getOpcode() != ISD::OR)
1140 continue;
1141
1142 auto IsZero = [] (const SDValue &V) -> bool {
1143 if (ConstantSDNode *SC = dyn_cast<ConstantSDNode>(V.getNode()))
1144 return SC->isNullValue();
1145 return false;
1146 };
1147 auto IsSelect0 = [IsZero] (const SDValue &Op) -> bool {
1148 if (Op.getOpcode() != ISD::SELECT)
1149 return false;
1150 return IsZero(Op.getOperand(1)) || IsZero(Op.getOperand(2));
1151 };
1152
1153 SDValue N0 = I->getOperand(0), N1 = I->getOperand(1);
1154 EVT VT = I->getValueType(0);
1155 bool SelN0 = IsSelect0(N0);
1156 SDValue SOp = SelN0 ? N0 : N1;
1157 SDValue VOp = SelN0 ? N1 : N0;
1158
1159 if (SOp.getOpcode() == ISD::SELECT && SOp.getNode()->hasOneUse()) {
1160 SDValue SC = SOp.getOperand(0);
1161 SDValue SX = SOp.getOperand(1);
1162 SDValue SY = SOp.getOperand(2);
1163 SDLoc DLS = SOp;
1164 if (IsZero(SY)) {
1165 SDValue NewOr = DAG.getNode(ISD::OR, DLS, VT, SX, VOp);
1166 SDValue NewSel = DAG.getNode(ISD::SELECT, DLS, VT, SC, NewOr, VOp);
1167 DAG.ReplaceAllUsesWith(I, NewSel.getNode());
1168 } else if (IsZero(SX)) {
1169 SDValue NewOr = DAG.getNode(ISD::OR, DLS, VT, SY, VOp);
1170 SDValue NewSel = DAG.getNode(ISD::SELECT, DLS, VT, SC, VOp, NewOr);
1171 DAG.ReplaceAllUsesWith(I, NewSel.getNode());
1172 }
1173 }
1174 }
1175}
1176
1177
Colin LeMahieuc7522f32015-01-14 23:07:36 +00001178bool HexagonDAGToDAGISel::SelectAddrFI(SDValue& N, SDValue &R) {
1179 if (N.getOpcode() != ISD::FrameIndex)
1180 return false;
1181 FrameIndexSDNode *FX = cast<FrameIndexSDNode>(N);
1182 R = CurDAG->getTargetFrameIndex(FX->getIndex(), MVT::i32);
1183 return true;
1184}
Colin LeMahieu0ee02fc2015-01-19 20:31:18 +00001185
Colin LeMahieu987b0942015-02-04 20:38:01 +00001186inline bool HexagonDAGToDAGISel::SelectAddrGA(SDValue &N, SDValue &R) {
1187 return SelectGlobalAddress(N, R, false);
1188}
1189
Colin LeMahieu51491352015-02-04 22:36:28 +00001190inline bool HexagonDAGToDAGISel::SelectAddrGP(SDValue &N, SDValue &R) {
1191 return SelectGlobalAddress(N, R, true);
1192}
1193
Colin LeMahieu987b0942015-02-04 20:38:01 +00001194bool HexagonDAGToDAGISel::SelectGlobalAddress(SDValue &N, SDValue &R,
1195 bool UseGP) {
1196 switch (N.getOpcode()) {
1197 case ISD::ADD: {
1198 SDValue N0 = N.getOperand(0);
1199 SDValue N1 = N.getOperand(1);
1200 unsigned GAOpc = N0.getOpcode();
1201 if (UseGP && GAOpc != HexagonISD::CONST32_GP)
1202 return false;
1203 if (!UseGP && GAOpc != HexagonISD::CONST32)
1204 return false;
1205 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N1)) {
1206 SDValue Addr = N0.getOperand(0);
1207 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Addr)) {
1208 if (GA->getOpcode() == ISD::TargetGlobalAddress) {
1209 uint64_t NewOff = GA->getOffset() + (uint64_t)Const->getSExtValue();
1210 R = CurDAG->getTargetGlobalAddress(GA->getGlobal(), SDLoc(Const),
1211 N.getValueType(), NewOff);
1212 return true;
1213 }
1214 }
1215 }
1216 break;
1217 }
1218 case HexagonISD::CONST32:
1219 // The operand(0) of CONST32 is TargetGlobalAddress, which is what we
1220 // want in the instruction.
1221 if (!UseGP)
1222 R = N.getOperand(0);
1223 return !UseGP;
1224 case HexagonISD::CONST32_GP:
1225 if (UseGP)
1226 R = N.getOperand(0);
1227 return UseGP;
1228 default:
1229 return false;
1230 }
1231
1232 return false;
1233}
1234
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001235bool HexagonDAGToDAGISel::isValueExtension(const SDValue &Val,
1236 unsigned FromBits, SDValue &Src) {
Colin LeMahieu0ee02fc2015-01-19 20:31:18 +00001237 unsigned Opc = Val.getOpcode();
1238 switch (Opc) {
1239 case ISD::SIGN_EXTEND:
1240 case ISD::ZERO_EXTEND:
1241 case ISD::ANY_EXTEND: {
1242 SDValue const &Op0 = Val.getOperand(0);
1243 EVT T = Op0.getValueType();
1244 if (T.isInteger() && T.getSizeInBits() == FromBits) {
1245 Src = Op0;
1246 return true;
1247 }
1248 break;
1249 }
1250 case ISD::SIGN_EXTEND_INREG:
1251 case ISD::AssertSext:
1252 case ISD::AssertZext:
1253 if (Val.getOperand(0).getValueType().isInteger()) {
1254 VTSDNode *T = cast<VTSDNode>(Val.getOperand(1));
1255 if (T->getVT().getSizeInBits() == FromBits) {
1256 Src = Val.getOperand(0);
1257 return true;
1258 }
1259 }
1260 break;
1261 case ISD::AND: {
1262 // Check if this is an AND with "FromBits" of lower bits set to 1.
1263 uint64_t FromMask = (1 << FromBits) - 1;
1264 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(0))) {
1265 if (C->getZExtValue() == FromMask) {
1266 Src = Val.getOperand(1);
1267 return true;
1268 }
1269 }
1270 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(1))) {
1271 if (C->getZExtValue() == FromMask) {
1272 Src = Val.getOperand(0);
1273 return true;
1274 }
1275 }
1276 break;
1277 }
1278 case ISD::OR:
1279 case ISD::XOR: {
1280 // OR/XOR with the lower "FromBits" bits set to 0.
1281 uint64_t FromMask = (1 << FromBits) - 1;
1282 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(0))) {
1283 if ((C->getZExtValue() & FromMask) == 0) {
1284 Src = Val.getOperand(1);
1285 return true;
1286 }
1287 }
1288 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(1))) {
1289 if ((C->getZExtValue() & FromMask) == 0) {
1290 Src = Val.getOperand(0);
1291 return true;
1292 }
1293 }
1294 }
1295 default:
1296 break;
1297 }
1298 return false;
1299}