blob: 1b680358ecc8a2f7825feb20604300b4a802215c [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- HexagonISelDAGToDAG.cpp - A dag to dag inst selector for Hexagon --===//
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines an instruction selector for the Hexagon target.
11//
12//===----------------------------------------------------------------------===//
13
Chandler Carruthed0881b2012-12-03 16:50:05 +000014#include "Hexagon.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000015#include "HexagonISelLowering.h"
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +000016#include "HexagonMachineFunctionInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000017#include "HexagonTargetMachine.h"
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +000018#include "llvm/CodeGen/FunctionLoweringInfo.h"
19#include "llvm/CodeGen/MachineInstrBuilder.h"
Jyotsna Vermad9225242013-02-13 21:38:46 +000020#include "llvm/CodeGen/SelectionDAGISel.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000021#include "llvm/IR/Intrinsics.h"
Jyotsna Vermad9225242013-02-13 21:38:46 +000022#include "llvm/Support/CommandLine.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000023#include "llvm/Support/Debug.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000024using namespace llvm;
25
Chandler Carruth84e68b22014-04-22 02:41:26 +000026#define DEBUG_TYPE "hexagon-isel"
27
Jyotsna Vermad9225242013-02-13 21:38:46 +000028static
29cl::opt<unsigned>
30MaxNumOfUsesForConstExtenders("ga-max-num-uses-for-constant-extenders",
31 cl::Hidden, cl::init(2),
32 cl::desc("Maximum number of uses of a global address such that we still us a"
33 "constant extended instruction"));
Tony Linthicum1213a7a2011-12-12 21:14:40 +000034
35//===----------------------------------------------------------------------===//
36// Instruction Selector Implementation
37//===----------------------------------------------------------------------===//
38
39//===--------------------------------------------------------------------===//
40/// HexagonDAGToDAGISel - Hexagon specific code to select Hexagon machine
41/// instructions for SelectionDAG operations.
42///
43namespace {
44class HexagonDAGToDAGISel : public SelectionDAGISel {
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +000045 const HexagonTargetMachine &HTM;
Eric Christopher23a7d1e2015-03-21 03:12:59 +000046 const HexagonSubtarget *HST;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +000047 const HexagonInstrInfo *HII;
48 const HexagonRegisterInfo *HRI;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000049public:
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +000050 explicit HexagonDAGToDAGISel(HexagonTargetMachine &tm,
Jyotsna Vermad9225242013-02-13 21:38:46 +000051 CodeGenOpt::Level OptLevel)
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +000052 : SelectionDAGISel(tm, OptLevel), HTM(tm), HST(nullptr), HII(nullptr),
Chandler Carruth9ac86ef2016-06-03 10:13:31 +000053 HRI(nullptr) {}
Eric Christopher23a7d1e2015-03-21 03:12:59 +000054
55 bool runOnMachineFunction(MachineFunction &MF) override {
56 // Reset the subtarget each time through.
57 HST = &MF.getSubtarget<HexagonSubtarget>();
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +000058 HII = HST->getInstrInfo();
59 HRI = HST->getRegisterInfo();
Eric Christopher23a7d1e2015-03-21 03:12:59 +000060 SelectionDAGISel::runOnMachineFunction(MF);
61 return true;
62 }
63
Krzysztof Parzyszekae14e7b2015-03-17 21:47:16 +000064 virtual void PreprocessISelDAG() override;
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +000065 virtual void EmitFunctionEntryCode() override;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000066
Justin Bognerec37a022016-05-12 21:46:18 +000067 void Select(SDNode *N) override;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000068
69 // Complex Pattern Selectors.
Colin LeMahieu987b0942015-02-04 20:38:01 +000070 inline bool SelectAddrGA(SDValue &N, SDValue &R);
Colin LeMahieu51491352015-02-04 22:36:28 +000071 inline bool SelectAddrGP(SDValue &N, SDValue &R);
Colin LeMahieu987b0942015-02-04 20:38:01 +000072 bool SelectGlobalAddress(SDValue &N, SDValue &R, bool UseGP);
Colin LeMahieuc7522f32015-01-14 23:07:36 +000073 bool SelectAddrFI(SDValue &N, SDValue &R);
74
Craig Topper906c2cd2014-04-29 07:58:16 +000075 const char *getPassName() const override {
Tony Linthicum1213a7a2011-12-12 21:14:40 +000076 return "Hexagon DAG->DAG Pattern Instruction Selection";
77 }
78
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +000079 // Generate a machine instruction node corresponding to the circ/brev
80 // load intrinsic.
81 MachineSDNode *LoadInstrForLoadIntrinsic(SDNode *IntN);
82 // Given the circ/brev load intrinsic and the already generated machine
83 // instruction, generate the appropriate store (that is a part of the
84 // intrinsic's functionality).
85 SDNode *StoreInstrForLoadIntrinsic(MachineSDNode *LoadN, SDNode *IntN);
86
Justin Bognerec37a022016-05-12 21:46:18 +000087 void SelectFrameIndex(SDNode *N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +000088 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
89 /// inline asm expressions.
Craig Topper906c2cd2014-04-29 07:58:16 +000090 bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Daniel Sanders60f1db02015-03-13 12:45:09 +000091 unsigned ConstraintID,
Craig Topper906c2cd2014-04-29 07:58:16 +000092 std::vector<SDValue> &OutOps) override;
Justin Bognerec37a022016-05-12 21:46:18 +000093 bool tryLoadOfLoadIntrinsic(LoadSDNode *N);
94 void SelectLoad(SDNode *N);
95 void SelectBaseOffsetLoad(LoadSDNode *LD, SDLoc dl);
Benjamin Kramerbdc49562016-06-12 15:39:02 +000096 void SelectIndexedLoad(LoadSDNode *LD, const SDLoc &dl);
Benjamin Kramerbdc49562016-06-12 15:39:02 +000097 void SelectIndexedStore(StoreSDNode *ST, const SDLoc &dl);
Justin Bognerec37a022016-05-12 21:46:18 +000098 void SelectStore(SDNode *N);
99 void SelectSHL(SDNode *N);
100 void SelectMul(SDNode *N);
101 void SelectZeroExtend(SDNode *N);
102 void SelectIntrinsicWChain(SDNode *N);
103 void SelectIntrinsicWOChain(SDNode *N);
104 void SelectConstant(SDNode *N);
105 void SelectConstantFP(SDNode *N);
106 void SelectAdd(SDNode *N);
Krzysztof Parzyszek5da24e52016-06-27 15:08:22 +0000107 void SelectBitcast(SDNode *N);
Justin Bognerec37a022016-05-12 21:46:18 +0000108 void SelectBitOp(SDNode *N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000109
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000110 // XformMskToBitPosU5Imm - Returns the bit position which
111 // the single bit 32 bit mask represents.
112 // Used in Clr and Set bit immediate memops.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000113 SDValue XformMskToBitPosU5Imm(uint32_t Imm, const SDLoc &DL) {
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000114 int32_t bitPos;
115 bitPos = Log2_32(Imm);
116 assert(bitPos >= 0 && bitPos < 32 &&
117 "Constant out of range for 32 BitPos Memops");
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000118 return CurDAG->getTargetConstant(bitPos, DL, MVT::i32);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000119 }
Jyotsna Vermafdc660b2013-03-22 18:41:34 +0000120
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000121 // XformMskToBitPosU4Imm - Returns the bit position which the single-bit
122 // 16 bit mask represents. Used in Clr and Set bit immediate memops.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000123 SDValue XformMskToBitPosU4Imm(uint16_t Imm, const SDLoc &DL) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000124 return XformMskToBitPosU5Imm(Imm, DL);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000125 }
Jyotsna Vermafdc660b2013-03-22 18:41:34 +0000126
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000127 // XformMskToBitPosU3Imm - Returns the bit position which the single-bit
128 // 8 bit mask represents. Used in Clr and Set bit immediate memops.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000129 SDValue XformMskToBitPosU3Imm(uint8_t Imm, const SDLoc &DL) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000130 return XformMskToBitPosU5Imm(Imm, DL);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000131 }
Jyotsna Vermafdc660b2013-03-22 18:41:34 +0000132
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000133 // Return true if there is exactly one bit set in V, i.e., if V is one of the
134 // following integers: 2^0, 2^1, ..., 2^31.
135 bool ImmIsSingleBit(uint32_t v) const {
136 return isPowerOf2_32(v);
137 }
Jyotsna Vermafdc660b2013-03-22 18:41:34 +0000138
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000139 // XformM5ToU5Imm - Return a target constant with the specified value, of
140 // type i32 where the negative literal is transformed into a positive literal
141 // for use in -= memops.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000142 inline SDValue XformM5ToU5Imm(signed Imm, const SDLoc &DL) {
143 assert((Imm >= -31 && Imm <= -1) && "Constant out of range for Memops");
144 return CurDAG->getTargetConstant(-Imm, DL, MVT::i32);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000145 }
Jyotsna Vermafdc660b2013-03-22 18:41:34 +0000146
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000147 // XformU7ToU7M1Imm - Return a target constant decremented by 1, in range
148 // [1..128], used in cmpb.gtu instructions.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000149 inline SDValue XformU7ToU7M1Imm(signed Imm, const SDLoc &DL) {
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000150 assert((Imm >= 1 && Imm <= 128) && "Constant out of range for cmpb op");
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000151 return CurDAG->getTargetConstant(Imm - 1, DL, MVT::i8);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000152 }
Jyotsna Vermafdc660b2013-03-22 18:41:34 +0000153
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000154 // XformS8ToS8M1Imm - Return a target constant decremented by 1.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000155 inline SDValue XformSToSM1Imm(signed Imm, const SDLoc &DL) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000156 return CurDAG->getTargetConstant(Imm - 1, DL, MVT::i32);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000157 }
Jyotsna Verma60316252013-02-05 19:20:45 +0000158
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000159 // XformU8ToU8M1Imm - Return a target constant decremented by 1.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000160 inline SDValue XformUToUM1Imm(unsigned Imm, const SDLoc &DL) {
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000161 assert((Imm >= 1) && "Cannot decrement unsigned int less than 1");
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000162 return CurDAG->getTargetConstant(Imm - 1, DL, MVT::i32);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000163 }
Jyotsna Verma89c84822013-04-23 19:15:55 +0000164
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000165 // XformSToSM2Imm - Return a target constant decremented by 2.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000166 inline SDValue XformSToSM2Imm(unsigned Imm, const SDLoc &DL) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000167 return CurDAG->getTargetConstant(Imm - 2, DL, MVT::i32);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000168 }
Jyotsna Verma89c84822013-04-23 19:15:55 +0000169
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000170 // XformSToSM3Imm - Return a target constant decremented by 3.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000171 inline SDValue XformSToSM3Imm(unsigned Imm, const SDLoc &DL) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000172 return CurDAG->getTargetConstant(Imm - 3, DL, MVT::i32);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000173 }
Colin LeMahieu19ed07c2015-01-28 18:29:11 +0000174
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000175 // Include the pieces autogenerated from the target description.
176 #include "HexagonGenDAGISel.inc"
Colin LeMahieu0ee02fc2015-01-19 20:31:18 +0000177
178private:
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000179 bool isValueExtension(const SDValue &Val, unsigned FromBits, SDValue &Src);
Krzysztof Parzyszekbba0bf72016-07-15 15:35:52 +0000180 bool orIsAdd(const SDNode *N) const;
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +0000181 bool isAlignedMemNode(const MemSDNode *N) const;
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000182}; // end HexagonDAGToDAGISel
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000183} // end anonymous namespace
184
185
186/// createHexagonISelDag - This pass converts a legalized DAG into a
187/// Hexagon-specific DAG, ready for instruction scheduling.
188///
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000189namespace llvm {
190FunctionPass *createHexagonISelDag(HexagonTargetMachine &TM,
191 CodeGenOpt::Level OptLevel) {
Jyotsna Vermad9225242013-02-13 21:38:46 +0000192 return new HexagonDAGToDAGISel(TM, OptLevel);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000193}
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000194}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000195
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000196// Intrinsics that return a a predicate.
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000197static bool doesIntrinsicReturnPredicate(unsigned ID) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000198 switch (ID) {
199 default:
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000200 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000201 case Intrinsic::hexagon_C2_cmpeq:
202 case Intrinsic::hexagon_C2_cmpgt:
203 case Intrinsic::hexagon_C2_cmpgtu:
204 case Intrinsic::hexagon_C2_cmpgtup:
205 case Intrinsic::hexagon_C2_cmpgtp:
206 case Intrinsic::hexagon_C2_cmpeqp:
207 case Intrinsic::hexagon_C2_bitsset:
208 case Intrinsic::hexagon_C2_bitsclr:
209 case Intrinsic::hexagon_C2_cmpeqi:
210 case Intrinsic::hexagon_C2_cmpgti:
211 case Intrinsic::hexagon_C2_cmpgtui:
212 case Intrinsic::hexagon_C2_cmpgei:
213 case Intrinsic::hexagon_C2_cmpgeui:
214 case Intrinsic::hexagon_C2_cmplt:
215 case Intrinsic::hexagon_C2_cmpltu:
216 case Intrinsic::hexagon_C2_bitsclri:
217 case Intrinsic::hexagon_C2_and:
218 case Intrinsic::hexagon_C2_or:
219 case Intrinsic::hexagon_C2_xor:
220 case Intrinsic::hexagon_C2_andn:
221 case Intrinsic::hexagon_C2_not:
222 case Intrinsic::hexagon_C2_orn:
223 case Intrinsic::hexagon_C2_pxfer_map:
224 case Intrinsic::hexagon_C2_any8:
225 case Intrinsic::hexagon_C2_all8:
226 case Intrinsic::hexagon_A2_vcmpbeq:
227 case Intrinsic::hexagon_A2_vcmpbgtu:
228 case Intrinsic::hexagon_A2_vcmpheq:
229 case Intrinsic::hexagon_A2_vcmphgt:
230 case Intrinsic::hexagon_A2_vcmphgtu:
231 case Intrinsic::hexagon_A2_vcmpweq:
232 case Intrinsic::hexagon_A2_vcmpwgt:
233 case Intrinsic::hexagon_A2_vcmpwgtu:
234 case Intrinsic::hexagon_C2_tfrrp:
235 case Intrinsic::hexagon_S2_tstbit_i:
236 case Intrinsic::hexagon_S2_tstbit_r:
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000237 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000238 }
239}
240
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000241void HexagonDAGToDAGISel::SelectIndexedLoad(LoadSDNode *LD, const SDLoc &dl) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000242 SDValue Chain = LD->getChain();
243 SDValue Base = LD->getBasePtr();
244 SDValue Offset = LD->getOffset();
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000245 int32_t Inc = cast<ConstantSDNode>(Offset.getNode())->getSExtValue();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000246 EVT LoadedVT = LD->getMemoryVT();
247 unsigned Opcode = 0;
248
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000249 // Check for zero extended loads. Treat any-extend loads as zero extended
250 // loads.
251 ISD::LoadExtType ExtType = LD->getExtensionType();
252 bool IsZeroExt = (ExtType == ISD::ZEXTLOAD || ExtType == ISD::EXTLOAD);
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000253 bool IsValidInc = HII->isValidAutoIncImm(LoadedVT, Inc);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000254
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000255 assert(LoadedVT.isSimple());
256 switch (LoadedVT.getSimpleVT().SimpleTy) {
257 case MVT::i8:
258 if (IsZeroExt)
259 Opcode = IsValidInc ? Hexagon::L2_loadrub_pi : Hexagon::L2_loadrub_io;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000260 else
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000261 Opcode = IsValidInc ? Hexagon::L2_loadrb_pi : Hexagon::L2_loadrb_io;
262 break;
263 case MVT::i16:
264 if (IsZeroExt)
265 Opcode = IsValidInc ? Hexagon::L2_loadruh_pi : Hexagon::L2_loadruh_io;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000266 else
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000267 Opcode = IsValidInc ? Hexagon::L2_loadrh_pi : Hexagon::L2_loadrh_io;
268 break;
269 case MVT::i32:
270 Opcode = IsValidInc ? Hexagon::L2_loadri_pi : Hexagon::L2_loadri_io;
271 break;
272 case MVT::i64:
273 Opcode = IsValidInc ? Hexagon::L2_loadrd_pi : Hexagon::L2_loadrd_io;
274 break;
275 // 64B
276 case MVT::v64i8:
277 case MVT::v32i16:
278 case MVT::v16i32:
279 case MVT::v8i64:
280 if (isAlignedMemNode(LD))
281 Opcode = IsValidInc ? Hexagon::V6_vL32b_pi : Hexagon::V6_vL32b_ai;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000282 else
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000283 Opcode = IsValidInc ? Hexagon::V6_vL32Ub_pi : Hexagon::V6_vL32Ub_ai;
284 break;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000285 // 128B
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000286 case MVT::v128i8:
287 case MVT::v64i16:
288 case MVT::v32i32:
289 case MVT::v16i64:
290 if (isAlignedMemNode(LD))
291 Opcode = IsValidInc ? Hexagon::V6_vL32b_pi_128B
292 : Hexagon::V6_vL32b_ai_128B;
293 else
294 Opcode = IsValidInc ? Hexagon::V6_vL32Ub_pi_128B
295 : Hexagon::V6_vL32Ub_ai_128B;
296 break;
297 default:
298 llvm_unreachable("Unexpected memory type in indexed load");
Justin Bognerec37a022016-05-12 21:46:18 +0000299 }
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000300
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000301 SDValue IncV = CurDAG->getTargetConstant(Inc, dl, MVT::i32);
302 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
303 MemOp[0] = LD->getMemOperand();
304
305 auto getExt64 = [this,ExtType] (MachineSDNode *N, const SDLoc &dl)
306 -> MachineSDNode* {
307 if (ExtType == ISD::ZEXTLOAD || ExtType == ISD::EXTLOAD) {
308 SDValue Zero = CurDAG->getTargetConstant(0, dl, MVT::i32);
309 return CurDAG->getMachineNode(Hexagon::A4_combineir, dl, MVT::i64,
310 Zero, SDValue(N, 0));
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000311 }
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000312 if (ExtType == ISD::SEXTLOAD)
313 return CurDAG->getMachineNode(Hexagon::A2_sxtw, dl, MVT::i64,
314 SDValue(N, 0));
315 return N;
316 };
317
318 // Loaded value Next address Chain
319 SDValue From[3] = { SDValue(LD,0), SDValue(LD,1), SDValue(LD,2) };
320 SDValue To[3];
321
322 EVT ValueVT = LD->getValueType(0);
323 if (ValueVT == MVT::i64 && ExtType != ISD::NON_EXTLOAD) {
324 // A load extending to i64 will actually produce i32, which will then
325 // need to be extended to i64.
326 assert(LoadedVT.getSizeInBits() <= 32);
327 ValueVT = MVT::i32;
328 }
329
330 if (IsValidInc) {
331 MachineSDNode *L = CurDAG->getMachineNode(Opcode, dl, ValueVT,
332 MVT::i32, MVT::Other, Base,
333 IncV, Chain);
334 L->setMemRefs(MemOp, MemOp+1);
335 To[1] = SDValue(L, 1); // Next address.
336 To[2] = SDValue(L, 2); // Chain.
337 // Handle special case for extension to i64.
338 if (LD->getValueType(0) == MVT::i64)
339 L = getExt64(L, dl);
340 To[0] = SDValue(L, 0); // Loaded (extended) value.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000341 } else {
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000342 SDValue Zero = CurDAG->getTargetConstant(0, dl, MVT::i32);
343 MachineSDNode *L = CurDAG->getMachineNode(Opcode, dl, ValueVT, MVT::Other,
344 Base, Zero, Chain);
345 L->setMemRefs(MemOp, MemOp+1);
346 To[2] = SDValue(L, 1); // Chain.
347 MachineSDNode *A = CurDAG->getMachineNode(Hexagon::A2_addi, dl, MVT::i32,
348 Base, IncV);
349 To[1] = SDValue(A, 0); // Next address.
350 // Handle special case for extension to i64.
351 if (LD->getValueType(0) == MVT::i64)
352 L = getExt64(L, dl);
353 To[0] = SDValue(L, 0); // Loaded (extended) value.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000354 }
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000355 ReplaceUses(From, To, 3);
356 CurDAG->RemoveDeadNode(LD);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000357}
358
359
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +0000360MachineSDNode *HexagonDAGToDAGISel::LoadInstrForLoadIntrinsic(SDNode *IntN) {
361 if (IntN->getOpcode() != ISD::INTRINSIC_W_CHAIN)
362 return nullptr;
363
364 SDLoc dl(IntN);
365 unsigned IntNo = cast<ConstantSDNode>(IntN->getOperand(1))->getZExtValue();
366
367 static std::map<unsigned,unsigned> LoadPciMap = {
368 { Intrinsic::hexagon_circ_ldb, Hexagon::L2_loadrb_pci },
369 { Intrinsic::hexagon_circ_ldub, Hexagon::L2_loadrub_pci },
370 { Intrinsic::hexagon_circ_ldh, Hexagon::L2_loadrh_pci },
371 { Intrinsic::hexagon_circ_lduh, Hexagon::L2_loadruh_pci },
372 { Intrinsic::hexagon_circ_ldw, Hexagon::L2_loadri_pci },
373 { Intrinsic::hexagon_circ_ldd, Hexagon::L2_loadrd_pci },
374 };
375 auto FLC = LoadPciMap.find(IntNo);
376 if (FLC != LoadPciMap.end()) {
377 SDNode *Mod = CurDAG->getMachineNode(Hexagon::A2_tfrrcr, dl, MVT::i32,
378 IntN->getOperand(4));
379 EVT ValTy = (IntNo == Intrinsic::hexagon_circ_ldd) ? MVT::i64 : MVT::i32;
380 EVT RTys[] = { ValTy, MVT::i32, MVT::Other };
381 // Operands: { Base, Increment, Modifier, Chain }
382 auto Inc = cast<ConstantSDNode>(IntN->getOperand(5));
383 SDValue I = CurDAG->getTargetConstant(Inc->getSExtValue(), dl, MVT::i32);
384 MachineSDNode *Res = CurDAG->getMachineNode(FLC->second, dl, RTys,
385 { IntN->getOperand(2), I, SDValue(Mod,0), IntN->getOperand(0) });
386 return Res;
387 }
388
389 static std::map<unsigned,unsigned> LoadPbrMap = {
390 { Intrinsic::hexagon_brev_ldb, Hexagon::L2_loadrb_pbr },
391 { Intrinsic::hexagon_brev_ldub, Hexagon::L2_loadrub_pbr },
392 { Intrinsic::hexagon_brev_ldh, Hexagon::L2_loadrh_pbr },
393 { Intrinsic::hexagon_brev_lduh, Hexagon::L2_loadruh_pbr },
394 { Intrinsic::hexagon_brev_ldw, Hexagon::L2_loadri_pbr },
395 { Intrinsic::hexagon_brev_ldd, Hexagon::L2_loadrd_pbr },
396 };
397 auto FLB = LoadPbrMap.find(IntNo);
398 if (FLB != LoadPbrMap.end()) {
399 SDNode *Mod = CurDAG->getMachineNode(Hexagon::A2_tfrrcr, dl, MVT::i32,
400 IntN->getOperand(4));
401 EVT ValTy = (IntNo == Intrinsic::hexagon_brev_ldd) ? MVT::i64 : MVT::i32;
402 EVT RTys[] = { ValTy, MVT::i32, MVT::Other };
403 // Operands: { Base, Modifier, Chain }
404 MachineSDNode *Res = CurDAG->getMachineNode(FLB->second, dl, RTys,
405 { IntN->getOperand(2), SDValue(Mod,0), IntN->getOperand(0) });
406 return Res;
407 }
408
409 return nullptr;
410}
411
412SDNode *HexagonDAGToDAGISel::StoreInstrForLoadIntrinsic(MachineSDNode *LoadN,
413 SDNode *IntN) {
414 // The "LoadN" is just a machine load instruction. The intrinsic also
415 // involves storing it. Generate an appropriate store to the location
416 // given in the intrinsic's operand(3).
417 uint64_t F = HII->get(LoadN->getMachineOpcode()).TSFlags;
418 unsigned SizeBits = (F >> HexagonII::MemAccessSizePos) &
419 HexagonII::MemAccesSizeMask;
420 unsigned Size = 1U << (SizeBits-1);
421
422 SDLoc dl(IntN);
423 MachinePointerInfo PI;
424 SDValue TS;
425 SDValue Loc = IntN->getOperand(3);
426
427 if (Size >= 4)
Justin Lebar9c375812016-07-15 18:27:10 +0000428 TS = CurDAG->getStore(SDValue(LoadN, 2), dl, SDValue(LoadN, 0), Loc, PI,
429 Size);
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +0000430 else
Justin Lebar9c375812016-07-15 18:27:10 +0000431 TS = CurDAG->getTruncStore(SDValue(LoadN, 2), dl, SDValue(LoadN, 0), Loc,
432 PI, MVT::getIntegerVT(Size * 8), Size);
Justin Bognerdcb7a822016-05-10 20:31:53 +0000433
434 SDNode *StoreN;
435 {
436 HandleSDNode Handle(TS);
437 SelectStore(TS.getNode());
438 StoreN = Handle.getValue().getNode();
439 }
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +0000440
441 // Load's results are { Loaded value, Updated pointer, Chain }
442 ReplaceUses(SDValue(IntN, 0), SDValue(LoadN, 1));
443 ReplaceUses(SDValue(IntN, 1), SDValue(StoreN, 0));
444 return StoreN;
445}
446
Justin Bognerec37a022016-05-12 21:46:18 +0000447bool HexagonDAGToDAGISel::tryLoadOfLoadIntrinsic(LoadSDNode *N) {
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +0000448 // The intrinsics for load circ/brev perform two operations:
449 // 1. Load a value V from the specified location, using the addressing
450 // mode corresponding to the intrinsic.
451 // 2. Store V into a specified location. This location is typically a
452 // local, temporary object.
453 // In many cases, the program using these intrinsics will immediately
454 // load V again from the local object. In those cases, when certain
455 // conditions are met, the last load can be removed.
456 // This function identifies and optimizes this pattern. If the pattern
457 // cannot be optimized, it returns nullptr, which will cause the load
458 // to be selected separately from the intrinsic (which will be handled
459 // in SelectIntrinsicWChain).
460
461 SDValue Ch = N->getOperand(0);
462 SDValue Loc = N->getOperand(1);
463
464 // Assume that the load and the intrinsic are connected directly with a
465 // chain:
466 // t1: i32,ch = int.load ..., ..., ..., Loc, ... // <-- C
467 // t2: i32,ch = load t1:1, Loc, ...
468 SDNode *C = Ch.getNode();
469
470 if (C->getOpcode() != ISD::INTRINSIC_W_CHAIN)
Justin Bognerec37a022016-05-12 21:46:18 +0000471 return false;
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +0000472
473 // The second load can only be eliminated if its extension type matches
474 // that of the load instruction corresponding to the intrinsic. The user
475 // can provide an address of an unsigned variable to store the result of
476 // a sign-extending intrinsic into (or the other way around).
477 ISD::LoadExtType IntExt;
478 switch (cast<ConstantSDNode>(C->getOperand(1))->getZExtValue()) {
479 case Intrinsic::hexagon_brev_ldub:
480 case Intrinsic::hexagon_brev_lduh:
481 case Intrinsic::hexagon_circ_ldub:
482 case Intrinsic::hexagon_circ_lduh:
483 IntExt = ISD::ZEXTLOAD;
484 break;
485 case Intrinsic::hexagon_brev_ldw:
486 case Intrinsic::hexagon_brev_ldd:
487 case Intrinsic::hexagon_circ_ldw:
488 case Intrinsic::hexagon_circ_ldd:
489 IntExt = ISD::NON_EXTLOAD;
490 break;
491 default:
492 IntExt = ISD::SEXTLOAD;
493 break;
494 }
495 if (N->getExtensionType() != IntExt)
Justin Bognerec37a022016-05-12 21:46:18 +0000496 return false;
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +0000497
498 // Make sure the target location for the loaded value in the load intrinsic
499 // is the location from which LD (or N) is loading.
500 if (C->getNumOperands() < 4 || Loc.getNode() != C->getOperand(3).getNode())
Justin Bognerec37a022016-05-12 21:46:18 +0000501 return false;
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +0000502
503 if (MachineSDNode *L = LoadInstrForLoadIntrinsic(C)) {
504 SDNode *S = StoreInstrForLoadIntrinsic(L, C);
505 SDValue F[] = { SDValue(N,0), SDValue(N,1), SDValue(C,0), SDValue(C,1) };
506 SDValue T[] = { SDValue(L,0), SDValue(S,0), SDValue(L,1), SDValue(S,0) };
507 ReplaceUses(F, T, array_lengthof(T));
508 // This transformation will leave the intrinsic dead. If it remains in
509 // the DAG, the selection code will see it again, but without the load,
510 // and it will generate a store that is normally required for it.
Krzysztof Parzyszek0f791f42016-05-13 18:48:15 +0000511 CurDAG->RemoveDeadNode(C);
Justin Bognerec37a022016-05-12 21:46:18 +0000512 return true;
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +0000513 }
514
Justin Bognerec37a022016-05-12 21:46:18 +0000515 return false;
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +0000516}
517
Justin Bognerec37a022016-05-12 21:46:18 +0000518void HexagonDAGToDAGISel::SelectLoad(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000519 SDLoc dl(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000520 LoadSDNode *LD = cast<LoadSDNode>(N);
521 ISD::MemIndexedMode AM = LD->getAddressingMode();
522
523 // Handle indexed loads.
Justin Bognerec37a022016-05-12 21:46:18 +0000524 if (AM != ISD::UNINDEXED) {
525 SelectIndexedLoad(LD, dl);
526 return;
527 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000528
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +0000529 // Handle patterns using circ/brev load intrinsics.
Justin Bognerec37a022016-05-12 21:46:18 +0000530 if (tryLoadOfLoadIntrinsic(LD))
531 return;
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +0000532
Justin Bognerec37a022016-05-12 21:46:18 +0000533 SelectCode(LD);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000534}
535
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000536void HexagonDAGToDAGISel::SelectIndexedStore(StoreSDNode *ST, const SDLoc &dl) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000537 SDValue Chain = ST->getChain();
538 SDValue Base = ST->getBasePtr();
539 SDValue Offset = ST->getOffset();
540 SDValue Value = ST->getValue();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000541 // Get the constant value.
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000542 int32_t Inc = cast<ConstantSDNode>(Offset.getNode())->getSExtValue();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000543 EVT StoredVT = ST->getMemoryVT();
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000544 EVT ValueVT = Value.getValueType();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000545
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000546 bool IsValidInc = HII->isValidAutoIncImm(StoredVT, Inc);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000547 unsigned Opcode = 0;
548
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000549 assert(StoredVT.isSimple());
550 switch (StoredVT.getSimpleVT().SimpleTy) {
551 case MVT::i8:
552 Opcode = IsValidInc ? Hexagon::S2_storerb_pi : Hexagon::S2_storerb_io;
553 break;
554 case MVT::i16:
555 Opcode = IsValidInc ? Hexagon::S2_storerh_pi : Hexagon::S2_storerh_io;
556 break;
557 case MVT::i32:
558 Opcode = IsValidInc ? Hexagon::S2_storeri_pi : Hexagon::S2_storeri_io;
559 break;
560 case MVT::i64:
561 Opcode = IsValidInc ? Hexagon::S2_storerd_pi : Hexagon::S2_storerd_io;
562 break;
563 // 64B
564 case MVT::v64i8:
565 case MVT::v32i16:
566 case MVT::v16i32:
567 case MVT::v8i64:
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +0000568 if (isAlignedMemNode(ST))
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000569 Opcode = IsValidInc ? Hexagon::V6_vS32b_pi : Hexagon::V6_vS32b_ai;
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +0000570 else
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000571 Opcode = IsValidInc ? Hexagon::V6_vS32Ub_pi : Hexagon::V6_vS32Ub_ai;
572 break;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000573 // 128B
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000574 case MVT::v128i8:
575 case MVT::v64i16:
576 case MVT::v32i32:
577 case MVT::v16i64:
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +0000578 if (isAlignedMemNode(ST))
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000579 Opcode = IsValidInc ? Hexagon::V6_vS32b_pi_128B
580 : Hexagon::V6_vS32b_ai_128B;
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +0000581 else
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000582 Opcode = IsValidInc ? Hexagon::V6_vS32Ub_pi_128B
583 : Hexagon::V6_vS32Ub_ai_128B;
584 break;
585 default:
586 llvm_unreachable("Unexpected memory type in indexed store");
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +0000587 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000588
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000589 if (ST->isTruncatingStore() && ValueVT.getSizeInBits() == 64) {
590 assert(StoredVT.getSizeInBits() < 64 && "Not a truncating store");
591 Value = CurDAG->getTargetExtractSubreg(Hexagon::subreg_loreg,
592 dl, MVT::i32, Value);
593 }
594
595 SDValue IncV = CurDAG->getTargetConstant(Inc, dl, MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000596 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
597 MemOp[0] = ST->getMemOperand();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000598
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000599 // Next address Chain
600 SDValue From[2] = { SDValue(ST,0), SDValue(ST,1) };
601 SDValue To[2];
602
603 if (IsValidInc) {
604 // Build post increment store.
605 SDValue Ops[] = { Base, IncV, Value, Chain };
606 MachineSDNode *S = CurDAG->getMachineNode(Opcode, dl, MVT::i32, MVT::Other,
607 Ops);
608 S->setMemRefs(MemOp, MemOp + 1);
609 To[0] = SDValue(S, 0);
610 To[1] = SDValue(S, 1);
611 } else {
612 SDValue Zero = CurDAG->getTargetConstant(0, dl, MVT::i32);
613 SDValue Ops[] = { Base, Zero, Value, Chain };
614 MachineSDNode *S = CurDAG->getMachineNode(Opcode, dl, MVT::Other, Ops);
615 S->setMemRefs(MemOp, MemOp + 1);
616 To[1] = SDValue(S, 0);
617 MachineSDNode *A = CurDAG->getMachineNode(Hexagon::A2_addi, dl, MVT::i32,
618 Base, IncV);
619 To[0] = SDValue(A, 0);
620 }
621
622 ReplaceUses(From, To, 2);
Justin Bognerdcb7a822016-05-10 20:31:53 +0000623 CurDAG->RemoveDeadNode(ST);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000624}
625
Justin Bognerec37a022016-05-12 21:46:18 +0000626void HexagonDAGToDAGISel::SelectStore(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000627 SDLoc dl(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000628 StoreSDNode *ST = cast<StoreSDNode>(N);
629 ISD::MemIndexedMode AM = ST->getAddressingMode();
630
631 // Handle indexed stores.
632 if (AM != ISD::UNINDEXED) {
Justin Bognerec37a022016-05-12 21:46:18 +0000633 SelectIndexedStore(ST, dl);
634 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000635 }
Sirish Pandec92c3162012-05-03 16:18:50 +0000636
Justin Bognerec37a022016-05-12 21:46:18 +0000637 SelectCode(ST);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000638}
639
Justin Bognerec37a022016-05-12 21:46:18 +0000640void HexagonDAGToDAGISel::SelectMul(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000641 SDLoc dl(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000642
643 //
644 // %conv.i = sext i32 %tmp1 to i64
645 // %conv2.i = sext i32 %add to i64
646 // %mul.i = mul nsw i64 %conv2.i, %conv.i
647 //
648 // --- match with the following ---
649 //
650 // %mul.i = mpy (%tmp1, %add)
651 //
652
653 if (N->getValueType(0) == MVT::i64) {
654 // Shifting a i64 signed multiply.
655 SDValue MulOp0 = N->getOperand(0);
656 SDValue MulOp1 = N->getOperand(1);
657
658 SDValue OP0;
659 SDValue OP1;
660
661 // Handle sign_extend and sextload.
662 if (MulOp0.getOpcode() == ISD::SIGN_EXTEND) {
663 SDValue Sext0 = MulOp0.getOperand(0);
664 if (Sext0.getNode()->getValueType(0) != MVT::i32) {
Justin Bognerec37a022016-05-12 21:46:18 +0000665 SelectCode(N);
666 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000667 }
668
669 OP0 = Sext0;
670 } else if (MulOp0.getOpcode() == ISD::LOAD) {
671 LoadSDNode *LD = cast<LoadSDNode>(MulOp0.getNode());
672 if (LD->getMemoryVT() != MVT::i32 ||
673 LD->getExtensionType() != ISD::SEXTLOAD ||
674 LD->getAddressingMode() != ISD::UNINDEXED) {
Justin Bognerec37a022016-05-12 21:46:18 +0000675 SelectCode(N);
676 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000677 }
678
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000679 SDValue Chain = LD->getChain();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000680 SDValue TargetConst0 = CurDAG->getTargetConstant(0, dl, MVT::i32);
Colin LeMahieu026e88d2014-12-23 20:02:16 +0000681 OP0 = SDValue(CurDAG->getMachineNode(Hexagon::L2_loadri_io, dl, MVT::i32,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000682 MVT::Other,
683 LD->getBasePtr(), TargetConst0,
684 Chain), 0);
685 } else {
Justin Bognerec37a022016-05-12 21:46:18 +0000686 SelectCode(N);
687 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000688 }
689
690 // Same goes for the second operand.
691 if (MulOp1.getOpcode() == ISD::SIGN_EXTEND) {
692 SDValue Sext1 = MulOp1.getOperand(0);
693 if (Sext1.getNode()->getValueType(0) != MVT::i32) {
Justin Bognerec37a022016-05-12 21:46:18 +0000694 SelectCode(N);
695 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000696 }
697
698 OP1 = Sext1;
699 } else if (MulOp1.getOpcode() == ISD::LOAD) {
700 LoadSDNode *LD = cast<LoadSDNode>(MulOp1.getNode());
701 if (LD->getMemoryVT() != MVT::i32 ||
702 LD->getExtensionType() != ISD::SEXTLOAD ||
703 LD->getAddressingMode() != ISD::UNINDEXED) {
Justin Bognerec37a022016-05-12 21:46:18 +0000704 SelectCode(N);
705 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000706 }
707
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000708 SDValue Chain = LD->getChain();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000709 SDValue TargetConst0 = CurDAG->getTargetConstant(0, dl, MVT::i32);
Colin LeMahieu026e88d2014-12-23 20:02:16 +0000710 OP1 = SDValue(CurDAG->getMachineNode(Hexagon::L2_loadri_io, dl, MVT::i32,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000711 MVT::Other,
712 LD->getBasePtr(), TargetConst0,
713 Chain), 0);
714 } else {
Justin Bognerec37a022016-05-12 21:46:18 +0000715 SelectCode(N);
716 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000717 }
718
719 // Generate a mpy instruction.
Colin LeMahieud9b23502014-12-16 16:10:01 +0000720 SDNode *Result = CurDAG->getMachineNode(Hexagon::M2_dpmpyss_s0, dl, MVT::i64,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000721 OP0, OP1);
Justin Bognerec37a022016-05-12 21:46:18 +0000722 ReplaceNode(N, Result);
723 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000724 }
725
Justin Bognerec37a022016-05-12 21:46:18 +0000726 SelectCode(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000727}
728
Justin Bognerec37a022016-05-12 21:46:18 +0000729void HexagonDAGToDAGISel::SelectSHL(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000730 SDLoc dl(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000731 if (N->getValueType(0) == MVT::i32) {
732 SDValue Shl_0 = N->getOperand(0);
733 SDValue Shl_1 = N->getOperand(1);
734 // RHS is const.
735 if (Shl_1.getOpcode() == ISD::Constant) {
736 if (Shl_0.getOpcode() == ISD::MUL) {
737 SDValue Mul_0 = Shl_0.getOperand(0); // Val
738 SDValue Mul_1 = Shl_0.getOperand(1); // Const
739 // RHS of mul is const.
740 if (Mul_1.getOpcode() == ISD::Constant) {
741 int32_t ShlConst =
742 cast<ConstantSDNode>(Shl_1.getNode())->getSExtValue();
743 int32_t MulConst =
744 cast<ConstantSDNode>(Mul_1.getNode())->getSExtValue();
745 int32_t ValConst = MulConst << ShlConst;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000746 SDValue Val = CurDAG->getTargetConstant(ValConst, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000747 MVT::i32);
748 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Val.getNode()))
749 if (isInt<9>(CN->getSExtValue())) {
750 SDNode* Result =
Colin LeMahieud9b23502014-12-16 16:10:01 +0000751 CurDAG->getMachineNode(Hexagon::M2_mpysmi, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000752 MVT::i32, Mul_0, Val);
Justin Bognerec37a022016-05-12 21:46:18 +0000753 ReplaceNode(N, Result);
754 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000755 }
756
757 }
758 } else if (Shl_0.getOpcode() == ISD::SUB) {
759 SDValue Sub_0 = Shl_0.getOperand(0); // Const 0
760 SDValue Sub_1 = Shl_0.getOperand(1); // Val
761 if (Sub_0.getOpcode() == ISD::Constant) {
762 int32_t SubConst =
763 cast<ConstantSDNode>(Sub_0.getNode())->getSExtValue();
764 if (SubConst == 0) {
765 if (Sub_1.getOpcode() == ISD::SHL) {
766 SDValue Shl2_0 = Sub_1.getOperand(0); // Val
767 SDValue Shl2_1 = Sub_1.getOperand(1); // Const
768 if (Shl2_1.getOpcode() == ISD::Constant) {
769 int32_t ShlConst =
770 cast<ConstantSDNode>(Shl_1.getNode())->getSExtValue();
771 int32_t Shl2Const =
772 cast<ConstantSDNode>(Shl2_1.getNode())->getSExtValue();
773 int32_t ValConst = 1 << (ShlConst+Shl2Const);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000774 SDValue Val = CurDAG->getTargetConstant(-ValConst, dl,
775 MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000776 if (ConstantSDNode *CN =
777 dyn_cast<ConstantSDNode>(Val.getNode()))
778 if (isInt<9>(CN->getSExtValue())) {
779 SDNode* Result =
Colin LeMahieud9b23502014-12-16 16:10:01 +0000780 CurDAG->getMachineNode(Hexagon::M2_mpysmi, dl, MVT::i32,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000781 Shl2_0, Val);
Justin Bognerec37a022016-05-12 21:46:18 +0000782 ReplaceNode(N, Result);
783 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000784 }
785 }
786 }
787 }
788 }
789 }
790 }
791 }
Justin Bognerec37a022016-05-12 21:46:18 +0000792 SelectCode(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000793}
794
795
796//
797// If there is an zero_extend followed an intrinsic in DAG (this means - the
798// result of the intrinsic is predicate); convert the zero_extend to
799// transfer instruction.
800//
801// Zero extend -> transfer is lowered here. Otherwise, zero_extend will be
802// converted into a MUX as predicate registers defined as 1 bit in the
803// compiler. Architecture defines them as 8-bit registers.
804// We want to preserve all the lower 8-bits and, not just 1 LSB bit.
805//
Justin Bognerec37a022016-05-12 21:46:18 +0000806void HexagonDAGToDAGISel::SelectZeroExtend(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000807 SDLoc dl(N);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000808
809 SDValue Op0 = N->getOperand(0);
810 EVT OpVT = Op0.getValueType();
811 unsigned OpBW = OpVT.getSizeInBits();
812
813 // Special handling for zero-extending a vector of booleans.
814 if (OpVT.isVector() && OpVT.getVectorElementType() == MVT::i1 && OpBW <= 64) {
815 SDNode *Mask = CurDAG->getMachineNode(Hexagon::C2_mask, dl, MVT::i64, Op0);
816 unsigned NE = OpVT.getVectorNumElements();
817 EVT ExVT = N->getValueType(0);
818 unsigned ES = ExVT.getVectorElementType().getSizeInBits();
819 uint64_t MV = 0, Bit = 1;
820 for (unsigned i = 0; i < NE; ++i) {
821 MV |= Bit;
822 Bit <<= ES;
823 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000824 SDValue Ones = CurDAG->getTargetConstant(MV, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000825 SDNode *OnesReg = CurDAG->getMachineNode(Hexagon::CONST64_Int_Real, dl,
826 MVT::i64, Ones);
827 if (ExVT.getSizeInBits() == 32) {
828 SDNode *And = CurDAG->getMachineNode(Hexagon::A2_andp, dl, MVT::i64,
829 SDValue(Mask,0), SDValue(OnesReg,0));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000830 SDValue SubR = CurDAG->getTargetConstant(Hexagon::subreg_loreg, dl,
831 MVT::i32);
Justin Bognerec37a022016-05-12 21:46:18 +0000832 ReplaceNode(N, CurDAG->getMachineNode(Hexagon::EXTRACT_SUBREG, dl, ExVT,
833 SDValue(And, 0), SubR));
834 return;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000835 }
Justin Bognerec37a022016-05-12 21:46:18 +0000836 ReplaceNode(N,
837 CurDAG->getMachineNode(Hexagon::A2_andp, dl, ExVT,
838 SDValue(Mask, 0), SDValue(OnesReg, 0)));
839 return;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000840 }
841
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000842 SDNode *IsIntrinsic = N->getOperand(0).getNode();
843 if ((IsIntrinsic->getOpcode() == ISD::INTRINSIC_WO_CHAIN)) {
844 unsigned ID =
845 cast<ConstantSDNode>(IsIntrinsic->getOperand(0))->getZExtValue();
846 if (doesIntrinsicReturnPredicate(ID)) {
847 // Now we need to differentiate target data types.
848 if (N->getValueType(0) == MVT::i64) {
Krzysztof Parzyszekae14e7b2015-03-17 21:47:16 +0000849 // Convert the zero_extend to Rs = Pd followed by A2_combinew(0,Rs).
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000850 SDValue TargetConst0 = CurDAG->getTargetConstant(0, dl, MVT::i32);
Colin LeMahieu30dcb232014-12-09 18:16:49 +0000851 SDNode *Result_1 = CurDAG->getMachineNode(Hexagon::C2_tfrpr, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000852 MVT::i32,
853 SDValue(IsIntrinsic, 0));
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000854 SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A2_tfrsi, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000855 MVT::i32,
856 TargetConst0);
Colin LeMahieub580d7d2014-12-09 19:23:45 +0000857 SDNode *Result_3 = CurDAG->getMachineNode(Hexagon::A2_combinew, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000858 MVT::i64, MVT::Other,
859 SDValue(Result_2, 0),
860 SDValue(Result_1, 0));
Justin Bognerec37a022016-05-12 21:46:18 +0000861 ReplaceNode(N, Result_3);
862 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000863 }
864 if (N->getValueType(0) == MVT::i32) {
865 // Convert the zero_extend to Rs = Pd
Colin LeMahieu30dcb232014-12-09 18:16:49 +0000866 SDNode* RsPd = CurDAG->getMachineNode(Hexagon::C2_tfrpr, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000867 MVT::i32,
868 SDValue(IsIntrinsic, 0));
Justin Bognerec37a022016-05-12 21:46:18 +0000869 ReplaceNode(N, RsPd);
870 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000871 }
Craig Toppere55c5562012-02-07 02:50:20 +0000872 llvm_unreachable("Unexpected value type");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000873 }
874 }
Justin Bognerec37a022016-05-12 21:46:18 +0000875 SelectCode(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000876}
877
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +0000878
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000879//
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +0000880// Handling intrinsics for circular load and bitreverse load.
Krzysztof Parzyszek47ab1f22015-03-18 16:23:44 +0000881//
Justin Bognerec37a022016-05-12 21:46:18 +0000882void HexagonDAGToDAGISel::SelectIntrinsicWChain(SDNode *N) {
883 if (MachineSDNode *L = LoadInstrForLoadIntrinsic(N)) {
884 StoreInstrForLoadIntrinsic(L, N);
Krzysztof Parzyszek0f791f42016-05-13 18:48:15 +0000885 CurDAG->RemoveDeadNode(N);
Justin Bognerec37a022016-05-12 21:46:18 +0000886 return;
887 }
888 SelectCode(N);
Krzysztof Parzyszek47ab1f22015-03-18 16:23:44 +0000889}
890
Justin Bognerec37a022016-05-12 21:46:18 +0000891void HexagonDAGToDAGISel::SelectIntrinsicWOChain(SDNode *N) {
Colin LeMahieu0ee02fc2015-01-19 20:31:18 +0000892 unsigned IID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
893 unsigned Bits;
894 switch (IID) {
895 case Intrinsic::hexagon_S2_vsplatrb:
896 Bits = 8;
897 break;
898 case Intrinsic::hexagon_S2_vsplatrh:
899 Bits = 16;
900 break;
901 default:
Justin Bognerec37a022016-05-12 21:46:18 +0000902 SelectCode(N);
903 return;
Colin LeMahieu0ee02fc2015-01-19 20:31:18 +0000904 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000905
Krzysztof Parzyszeke60e5fe2016-05-12 17:21:40 +0000906 SDValue V = N->getOperand(1);
Colin LeMahieu0ee02fc2015-01-19 20:31:18 +0000907 SDValue U;
908 if (isValueExtension(V, Bits, U)) {
909 SDValue R = CurDAG->getNode(N->getOpcode(), SDLoc(N), N->getValueType(0),
Krzysztof Parzyszeke60e5fe2016-05-12 17:21:40 +0000910 N->getOperand(0), U);
Justin Bognerd82025b2016-05-12 21:24:23 +0000911 ReplaceNode(N, R.getNode());
Justin Bognerec37a022016-05-12 21:46:18 +0000912 SelectCode(R.getNode());
913 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000914 }
Justin Bognerec37a022016-05-12 21:46:18 +0000915 SelectCode(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000916}
917
Sirish Pande69295b82012-05-10 20:20:25 +0000918//
919// Map floating point constant values.
920//
Justin Bognerec37a022016-05-12 21:46:18 +0000921void HexagonDAGToDAGISel::SelectConstantFP(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000922 SDLoc dl(N);
Sirish Pande69295b82012-05-10 20:20:25 +0000923 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N);
Benjamin Kramer46e38f32016-06-08 10:01:20 +0000924 const APFloat &APF = CN->getValueAPF();
Sirish Pande69295b82012-05-10 20:20:25 +0000925 if (N->getValueType(0) == MVT::f32) {
Justin Bognerec37a022016-05-12 21:46:18 +0000926 ReplaceNode(
927 N, CurDAG->getMachineNode(Hexagon::TFRI_f, dl, MVT::f32,
928 CurDAG->getTargetConstantFP(
929 APF.convertToFloat(), dl, MVT::f32)));
930 return;
Sirish Pande69295b82012-05-10 20:20:25 +0000931 }
932 else if (N->getValueType(0) == MVT::f64) {
Justin Bognerec37a022016-05-12 21:46:18 +0000933 ReplaceNode(
934 N, CurDAG->getMachineNode(Hexagon::CONST64_Float_Real, dl, MVT::f64,
935 CurDAG->getTargetConstantFP(
936 APF.convertToDouble(), dl, MVT::f64)));
937 return;
Sirish Pande69295b82012-05-10 20:20:25 +0000938 }
939
Justin Bognerec37a022016-05-12 21:46:18 +0000940 SelectCode(N);
Sirish Pande69295b82012-05-10 20:20:25 +0000941}
942
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000943//
944// Map predicate true (encoded as -1 in LLVM) to a XOR.
945//
Justin Bognerec37a022016-05-12 21:46:18 +0000946void HexagonDAGToDAGISel::SelectConstant(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000947 SDLoc dl(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000948 if (N->getValueType(0) == MVT::i1) {
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +0000949 SDNode* Result = 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000950 int32_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Krzysztof Parzyszek7a9cd802015-03-18 18:50:06 +0000951 if (Val == -1) {
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +0000952 Result = CurDAG->getMachineNode(Hexagon::TFR_PdTrue, dl, MVT::i1);
953 } else if (Val == 0) {
954 Result = CurDAG->getMachineNode(Hexagon::TFR_PdFalse, dl, MVT::i1);
955 }
956 if (Result) {
Justin Bognerec37a022016-05-12 21:46:18 +0000957 ReplaceNode(N, Result);
958 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000959 }
960 }
961
Justin Bognerec37a022016-05-12 21:46:18 +0000962 SelectCode(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000963}
964
965
966//
967// Map add followed by a asr -> asr +=.
968//
Justin Bognerec37a022016-05-12 21:46:18 +0000969void HexagonDAGToDAGISel::SelectAdd(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000970 SDLoc dl(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000971 if (N->getValueType(0) != MVT::i32) {
Justin Bognerec37a022016-05-12 21:46:18 +0000972 SelectCode(N);
973 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000974 }
975 // Identify nodes of the form: add(asr(...)).
976 SDNode* Src1 = N->getOperand(0).getNode();
977 if (Src1->getOpcode() != ISD::SRA || !Src1->hasOneUse()
978 || Src1->getValueType(0) != MVT::i32) {
Justin Bognerec37a022016-05-12 21:46:18 +0000979 SelectCode(N);
980 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000981 }
982
983 // Build Rd = Rd' + asr(Rs, Rt). The machine constraints will ensure that
984 // Rd and Rd' are assigned to the same register
Colin LeMahieu0f850bd2014-12-19 20:29:29 +0000985 SDNode* Result = CurDAG->getMachineNode(Hexagon::S2_asr_r_r_acc, dl, MVT::i32,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000986 N->getOperand(1),
987 Src1->getOperand(0),
988 Src1->getOperand(1));
Justin Bognerec37a022016-05-12 21:46:18 +0000989 ReplaceNode(N, Result);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000990}
991
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +0000992//
993// Map the following, where possible.
994// AND/FABS -> clrbit
995// OR -> setbit
996// XOR/FNEG ->toggle_bit.
997//
Justin Bognerec37a022016-05-12 21:46:18 +0000998void HexagonDAGToDAGISel::SelectBitOp(SDNode *N) {
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +0000999 SDLoc dl(N);
1000 EVT ValueVT = N->getValueType(0);
1001
1002 // We handle only 32 and 64-bit bit ops.
1003 if (!(ValueVT == MVT::i32 || ValueVT == MVT::i64 ||
Justin Bognerec37a022016-05-12 21:46:18 +00001004 ValueVT == MVT::f32 || ValueVT == MVT::f64)) {
1005 SelectCode(N);
1006 return;
1007 }
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001008
1009 // We handly only fabs and fneg for V5.
1010 unsigned Opc = N->getOpcode();
Justin Bognerec37a022016-05-12 21:46:18 +00001011 if ((Opc == ISD::FABS || Opc == ISD::FNEG) && !HST->hasV5TOps()) {
1012 SelectCode(N);
1013 return;
1014 }
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001015
1016 int64_t Val = 0;
1017 if (Opc != ISD::FABS && Opc != ISD::FNEG) {
1018 if (N->getOperand(1).getOpcode() == ISD::Constant)
1019 Val = cast<ConstantSDNode>((N)->getOperand(1))->getSExtValue();
Justin Bognerec37a022016-05-12 21:46:18 +00001020 else {
1021 SelectCode(N);
1022 return;
1023 }
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001024 }
1025
1026 if (Opc == ISD::AND) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001027 // Check if this is a bit-clearing AND, if not select code the usual way.
1028 if ((ValueVT == MVT::i32 && isPowerOf2_32(~Val)) ||
1029 (ValueVT == MVT::i64 && isPowerOf2_64(~Val)))
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001030 Val = ~Val;
Justin Bognerec37a022016-05-12 21:46:18 +00001031 else {
1032 SelectCode(N);
1033 return;
1034 }
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001035 }
1036
1037 // If OR or AND is being fed by shl, srl and, sra don't do this change,
1038 // because Hexagon provide |= &= on shl, srl, and sra.
1039 // Traverse the DAG to see if there is shl, srl and sra.
1040 if (Opc == ISD::OR || Opc == ISD::AND) {
1041 switch (N->getOperand(0)->getOpcode()) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001042 default:
1043 break;
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001044 case ISD::SRA:
1045 case ISD::SRL:
1046 case ISD::SHL:
Justin Bognerec37a022016-05-12 21:46:18 +00001047 SelectCode(N);
1048 return;
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001049 }
1050 }
1051
1052 // Make sure it's power of 2.
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001053 unsigned BitPos = 0;
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001054 if (Opc != ISD::FABS && Opc != ISD::FNEG) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001055 if ((ValueVT == MVT::i32 && !isPowerOf2_32(Val)) ||
Justin Bognerec37a022016-05-12 21:46:18 +00001056 (ValueVT == MVT::i64 && !isPowerOf2_64(Val))) {
1057 SelectCode(N);
1058 return;
1059 }
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001060
1061 // Get the bit position.
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001062 BitPos = countTrailingZeros(uint64_t(Val));
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001063 } else {
1064 // For fabs and fneg, it's always the 31st bit.
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001065 BitPos = 31;
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001066 }
1067
1068 unsigned BitOpc = 0;
1069 // Set the right opcode for bitwise operations.
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001070 switch (Opc) {
1071 default:
1072 llvm_unreachable("Only bit-wise/abs/neg operations are allowed.");
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001073 case ISD::AND:
1074 case ISD::FABS:
1075 BitOpc = Hexagon::S2_clrbit_i;
1076 break;
1077 case ISD::OR:
1078 BitOpc = Hexagon::S2_setbit_i;
1079 break;
1080 case ISD::XOR:
1081 case ISD::FNEG:
1082 BitOpc = Hexagon::S2_togglebit_i;
1083 break;
1084 }
1085
1086 SDNode *Result;
1087 // Get the right SDVal for the opcode.
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001088 SDValue SDVal = CurDAG->getTargetConstant(BitPos, dl, MVT::i32);
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001089
1090 if (ValueVT == MVT::i32 || ValueVT == MVT::f32) {
1091 Result = CurDAG->getMachineNode(BitOpc, dl, ValueVT,
1092 N->getOperand(0), SDVal);
1093 } else {
1094 // 64-bit gymnastic to use REG_SEQUENCE. But it's worth it.
1095 EVT SubValueVT;
1096 if (ValueVT == MVT::i64)
1097 SubValueVT = MVT::i32;
1098 else
1099 SubValueVT = MVT::f32;
1100
1101 SDNode *Reg = N->getOperand(0).getNode();
1102 SDValue RegClass = CurDAG->getTargetConstant(Hexagon::DoubleRegsRegClassID,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001103 dl, MVT::i64);
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001104
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001105 SDValue SubregHiIdx = CurDAG->getTargetConstant(Hexagon::subreg_hireg, dl,
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001106 MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001107 SDValue SubregLoIdx = CurDAG->getTargetConstant(Hexagon::subreg_loreg, dl,
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001108 MVT::i32);
1109
1110 SDValue SubregHI = CurDAG->getTargetExtractSubreg(Hexagon::subreg_hireg, dl,
1111 MVT::i32, SDValue(Reg, 0));
1112
1113 SDValue SubregLO = CurDAG->getTargetExtractSubreg(Hexagon::subreg_loreg, dl,
1114 MVT::i32, SDValue(Reg, 0));
1115
1116 // Clear/set/toggle hi or lo registers depending on the bit position.
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001117 if (SubValueVT != MVT::f32 && BitPos < 32) {
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001118 SDNode *Result0 = CurDAG->getMachineNode(BitOpc, dl, SubValueVT,
1119 SubregLO, SDVal);
1120 const SDValue Ops[] = { RegClass, SubregHI, SubregHiIdx,
1121 SDValue(Result0, 0), SubregLoIdx };
1122 Result = CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE,
1123 dl, ValueVT, Ops);
1124 } else {
1125 if (Opc != ISD::FABS && Opc != ISD::FNEG)
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001126 SDVal = CurDAG->getTargetConstant(BitPos-32, dl, MVT::i32);
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001127 SDNode *Result0 = CurDAG->getMachineNode(BitOpc, dl, SubValueVT,
1128 SubregHI, SDVal);
1129 const SDValue Ops[] = { RegClass, SDValue(Result0, 0), SubregHiIdx,
1130 SubregLO, SubregLoIdx };
1131 Result = CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE,
1132 dl, ValueVT, Ops);
1133 }
1134 }
1135
Justin Bognerec37a022016-05-12 21:46:18 +00001136 ReplaceNode(N, Result);
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001137}
1138
1139
Justin Bognerec37a022016-05-12 21:46:18 +00001140void HexagonDAGToDAGISel::SelectFrameIndex(SDNode *N) {
Matthias Braun941a7052016-07-28 18:40:00 +00001141 MachineFrameInfo &MFI = MF->getFrameInfo();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001142 const HexagonFrameLowering *HFI = HST->getFrameLowering();
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001143 int FX = cast<FrameIndexSDNode>(N)->getIndex();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001144 unsigned StkA = HFI->getStackAlignment();
Matthias Braun941a7052016-07-28 18:40:00 +00001145 unsigned MaxA = MFI.getMaxAlignment();
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001146 SDValue FI = CurDAG->getTargetFrameIndex(FX, MVT::i32);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001147 SDLoc DL(N);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001148 SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001149 SDNode *R = 0;
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001150
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001151 // Use TFR_FI when:
1152 // - the object is fixed, or
1153 // - there are no objects with higher-than-default alignment, or
1154 // - there are no dynamically allocated objects.
1155 // Otherwise, use TFR_FIA.
Matthias Braun941a7052016-07-28 18:40:00 +00001156 if (FX < 0 || MaxA <= StkA || !MFI.hasVarSizedObjects()) {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001157 R = CurDAG->getMachineNode(Hexagon::TFR_FI, DL, MVT::i32, FI, Zero);
1158 } else {
1159 auto &HMFI = *MF->getInfo<HexagonMachineFunctionInfo>();
1160 unsigned AR = HMFI.getStackAlignBaseVReg();
1161 SDValue CH = CurDAG->getEntryNode();
1162 SDValue Ops[] = { CurDAG->getCopyFromReg(CH, DL, AR, MVT::i32), FI, Zero };
1163 R = CurDAG->getMachineNode(Hexagon::TFR_FIA, DL, MVT::i32, Ops);
1164 }
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001165
Justin Bognerec37a022016-05-12 21:46:18 +00001166 ReplaceNode(N, R);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001167}
1168
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001169
Krzysztof Parzyszek5da24e52016-06-27 15:08:22 +00001170void HexagonDAGToDAGISel::SelectBitcast(SDNode *N) {
1171 EVT SVT = N->getOperand(0).getValueType();
1172 EVT DVT = N->getValueType(0);
1173 if (!SVT.isVector() || !DVT.isVector() ||
1174 SVT.getVectorElementType() == MVT::i1 ||
1175 DVT.getVectorElementType() == MVT::i1 ||
1176 SVT.getSizeInBits() != DVT.getSizeInBits()) {
1177 SelectCode(N);
1178 return;
1179 }
1180
1181 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N,0), N->getOperand(0));
1182 CurDAG->RemoveDeadNode(N);
1183}
1184
1185
Justin Bognerec37a022016-05-12 21:46:18 +00001186void HexagonDAGToDAGISel::Select(SDNode *N) {
Tim Northover31d093c2013-09-22 08:21:56 +00001187 if (N->isMachineOpcode()) {
1188 N->setNodeId(-1);
Justin Bognerec37a022016-05-12 21:46:18 +00001189 return; // Already selected.
Tim Northover31d093c2013-09-22 08:21:56 +00001190 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001191
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001192 switch (N->getOpcode()) {
1193 case ISD::Constant:
Justin Bognerec37a022016-05-12 21:46:18 +00001194 SelectConstant(N);
1195 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001196
Sirish Pande69295b82012-05-10 20:20:25 +00001197 case ISD::ConstantFP:
Justin Bognerec37a022016-05-12 21:46:18 +00001198 SelectConstantFP(N);
1199 return;
Sirish Pande69295b82012-05-10 20:20:25 +00001200
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001201 case ISD::FrameIndex:
Justin Bognerec37a022016-05-12 21:46:18 +00001202 SelectFrameIndex(N);
1203 return;
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001204
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001205 case ISD::ADD:
Justin Bognerec37a022016-05-12 21:46:18 +00001206 SelectAdd(N);
1207 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001208
Krzysztof Parzyszek5da24e52016-06-27 15:08:22 +00001209 case ISD::BITCAST:
1210 SelectBitcast(N);
1211 return;
1212
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001213 case ISD::SHL:
Justin Bognerec37a022016-05-12 21:46:18 +00001214 SelectSHL(N);
1215 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001216
1217 case ISD::LOAD:
Justin Bognerec37a022016-05-12 21:46:18 +00001218 SelectLoad(N);
1219 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001220
1221 case ISD::STORE:
Justin Bognerec37a022016-05-12 21:46:18 +00001222 SelectStore(N);
1223 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001224
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001225 case ISD::MUL:
Justin Bognerec37a022016-05-12 21:46:18 +00001226 SelectMul(N);
1227 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001228
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001229 case ISD::AND:
1230 case ISD::OR:
1231 case ISD::XOR:
1232 case ISD::FABS:
1233 case ISD::FNEG:
Justin Bognerec37a022016-05-12 21:46:18 +00001234 SelectBitOp(N);
1235 return;
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001236
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001237 case ISD::ZERO_EXTEND:
Justin Bognerec37a022016-05-12 21:46:18 +00001238 SelectZeroExtend(N);
1239 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001240
Krzysztof Parzyszek47ab1f22015-03-18 16:23:44 +00001241 case ISD::INTRINSIC_W_CHAIN:
Justin Bognerec37a022016-05-12 21:46:18 +00001242 SelectIntrinsicWChain(N);
1243 return;
Krzysztof Parzyszek47ab1f22015-03-18 16:23:44 +00001244
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001245 case ISD::INTRINSIC_WO_CHAIN:
Justin Bognerec37a022016-05-12 21:46:18 +00001246 SelectIntrinsicWOChain(N);
1247 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001248 }
1249
Justin Bognerec37a022016-05-12 21:46:18 +00001250 SelectCode(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001251}
1252
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001253bool HexagonDAGToDAGISel::
Daniel Sanders60f1db02015-03-13 12:45:09 +00001254SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001255 std::vector<SDValue> &OutOps) {
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001256 SDValue Inp = Op, Res;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001257
Daniel Sanders60f1db02015-03-13 12:45:09 +00001258 switch (ConstraintID) {
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001259 default:
1260 return true;
Daniel Sanders49f643c2015-03-17 14:37:39 +00001261 case InlineAsm::Constraint_i:
1262 case InlineAsm::Constraint_o: // Offsetable.
1263 case InlineAsm::Constraint_v: // Not offsetable.
1264 case InlineAsm::Constraint_m: // Memory.
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001265 if (SelectAddrFI(Inp, Res))
1266 OutOps.push_back(Res);
1267 else
1268 OutOps.push_back(Inp);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001269 break;
1270 }
1271
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001272 OutOps.push_back(CurDAG->getTargetConstant(0, SDLoc(Op), MVT::i32));
Jyotsna Vermad9225242013-02-13 21:38:46 +00001273 return false;
1274}
Colin LeMahieuc7522f32015-01-14 23:07:36 +00001275
Colin LeMahieu79ec0652015-06-12 19:57:32 +00001276
Krzysztof Parzyszekae14e7b2015-03-17 21:47:16 +00001277void HexagonDAGToDAGISel::PreprocessISelDAG() {
1278 SelectionDAG &DAG = *CurDAG;
1279 std::vector<SDNode*> Nodes;
Pete Cooper7e64ef02015-07-14 23:43:29 +00001280 for (SDNode &Node : DAG.allnodes())
1281 Nodes.push_back(&Node);
Krzysztof Parzyszekae14e7b2015-03-17 21:47:16 +00001282
1283 // Simplify: (or (select c x 0) z) -> (select c (or x z) z)
1284 // (or (select c 0 y) z) -> (select c z (or y z))
1285 // This may not be the right thing for all targets, so do it here.
Krzysztof Parzyszekf7f70682016-06-22 20:08:27 +00001286 for (auto I : Nodes) {
Krzysztof Parzyszekae14e7b2015-03-17 21:47:16 +00001287 if (I->getOpcode() != ISD::OR)
1288 continue;
1289
1290 auto IsZero = [] (const SDValue &V) -> bool {
1291 if (ConstantSDNode *SC = dyn_cast<ConstantSDNode>(V.getNode()))
1292 return SC->isNullValue();
1293 return false;
1294 };
1295 auto IsSelect0 = [IsZero] (const SDValue &Op) -> bool {
1296 if (Op.getOpcode() != ISD::SELECT)
1297 return false;
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +00001298 return IsZero(Op.getOperand(1)) || IsZero(Op.getOperand(2));
Krzysztof Parzyszekae14e7b2015-03-17 21:47:16 +00001299 };
1300
1301 SDValue N0 = I->getOperand(0), N1 = I->getOperand(1);
1302 EVT VT = I->getValueType(0);
1303 bool SelN0 = IsSelect0(N0);
1304 SDValue SOp = SelN0 ? N0 : N1;
1305 SDValue VOp = SelN0 ? N1 : N0;
1306
1307 if (SOp.getOpcode() == ISD::SELECT && SOp.getNode()->hasOneUse()) {
1308 SDValue SC = SOp.getOperand(0);
1309 SDValue SX = SOp.getOperand(1);
1310 SDValue SY = SOp.getOperand(2);
1311 SDLoc DLS = SOp;
1312 if (IsZero(SY)) {
1313 SDValue NewOr = DAG.getNode(ISD::OR, DLS, VT, SX, VOp);
1314 SDValue NewSel = DAG.getNode(ISD::SELECT, DLS, VT, SC, NewOr, VOp);
1315 DAG.ReplaceAllUsesWith(I, NewSel.getNode());
1316 } else if (IsZero(SX)) {
1317 SDValue NewOr = DAG.getNode(ISD::OR, DLS, VT, SY, VOp);
1318 SDValue NewSel = DAG.getNode(ISD::SELECT, DLS, VT, SC, VOp, NewOr);
1319 DAG.ReplaceAllUsesWith(I, NewSel.getNode());
1320 }
1321 }
1322 }
Krzysztof Parzyszekf7f70682016-06-22 20:08:27 +00001323
1324 // Transform: (store ch addr (add x (add (shl y c) e)))
1325 // to: (store ch addr (add x (shl (add y d) c))),
1326 // where e = (shl d c) for some integer d.
1327 // The purpose of this is to enable generation of loads/stores with
1328 // shifted addressing mode, i.e. mem(x+y<<#c). For that, the shift
1329 // value c must be 0, 1 or 2.
1330 for (auto I : Nodes) {
1331 if (I->getOpcode() != ISD::STORE)
1332 continue;
1333
1334 // I matched: (store ch addr Off)
1335 SDValue Off = I->getOperand(2);
1336 // Off needs to match: (add x (add (shl y c) (shl d c))))
1337 if (Off.getOpcode() != ISD::ADD)
1338 continue;
1339 // Off matched: (add x T0)
1340 SDValue T0 = Off.getOperand(1);
1341 // T0 needs to match: (add T1 T2):
1342 if (T0.getOpcode() != ISD::ADD)
1343 continue;
1344 // T0 matched: (add T1 T2)
1345 SDValue T1 = T0.getOperand(0);
1346 SDValue T2 = T0.getOperand(1);
1347 // T1 needs to match: (shl y c)
1348 if (T1.getOpcode() != ISD::SHL)
1349 continue;
1350 SDValue C = T1.getOperand(1);
1351 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(C.getNode());
1352 if (CN == nullptr)
1353 continue;
1354 unsigned CV = CN->getZExtValue();
1355 if (CV > 2)
1356 continue;
1357 // T2 needs to match e, where e = (shl d c) for some d.
1358 ConstantSDNode *EN = dyn_cast<ConstantSDNode>(T2.getNode());
1359 if (EN == nullptr)
1360 continue;
1361 unsigned EV = EN->getZExtValue();
1362 if (EV % (1 << CV) != 0)
1363 continue;
1364 unsigned DV = EV / (1 << CV);
1365
1366 // Replace T0 with: (shl (add y d) c)
1367 SDLoc DL = SDLoc(I);
1368 EVT VT = T0.getValueType();
1369 SDValue D = DAG.getConstant(DV, DL, VT);
1370 // NewAdd = (add y d)
1371 SDValue NewAdd = DAG.getNode(ISD::ADD, DL, VT, T1.getOperand(0), D);
1372 // NewShl = (shl NewAdd c)
1373 SDValue NewShl = DAG.getNode(ISD::SHL, DL, VT, NewAdd, C);
1374 ReplaceNode(T0.getNode(), NewShl.getNode());
1375 }
Krzysztof Parzyszekae14e7b2015-03-17 21:47:16 +00001376}
1377
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001378void HexagonDAGToDAGISel::EmitFunctionEntryCode() {
1379 auto &HST = static_cast<const HexagonSubtarget&>(MF->getSubtarget());
1380 auto &HFI = *HST.getFrameLowering();
1381 if (!HFI.needsAligna(*MF))
1382 return;
Krzysztof Parzyszekae14e7b2015-03-17 21:47:16 +00001383
Matthias Braun941a7052016-07-28 18:40:00 +00001384 MachineFrameInfo &MFI = MF->getFrameInfo();
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +00001385 MachineBasicBlock *EntryBB = &MF->front();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001386 unsigned AR = FuncInfo->CreateReg(MVT::i32);
Matthias Braun941a7052016-07-28 18:40:00 +00001387 unsigned MaxA = MFI.getMaxAlignment();
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001388 BuildMI(EntryBB, DebugLoc(), HII->get(Hexagon::ALIGNA), AR)
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001389 .addImm(MaxA);
1390 MF->getInfo<HexagonMachineFunctionInfo>()->setStackAlignBaseVReg(AR);
1391}
1392
1393// Match a frame index that can be used in an addressing mode.
Colin LeMahieuc7522f32015-01-14 23:07:36 +00001394bool HexagonDAGToDAGISel::SelectAddrFI(SDValue& N, SDValue &R) {
1395 if (N.getOpcode() != ISD::FrameIndex)
1396 return false;
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001397 auto &HFI = *HST->getFrameLowering();
Matthias Braun941a7052016-07-28 18:40:00 +00001398 MachineFrameInfo &MFI = MF->getFrameInfo();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001399 int FX = cast<FrameIndexSDNode>(N)->getIndex();
Matthias Braun941a7052016-07-28 18:40:00 +00001400 if (!MFI.isFixedObjectIndex(FX) && HFI.needsAligna(*MF))
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001401 return false;
1402 R = CurDAG->getTargetFrameIndex(FX, MVT::i32);
Colin LeMahieuc7522f32015-01-14 23:07:36 +00001403 return true;
1404}
Colin LeMahieu0ee02fc2015-01-19 20:31:18 +00001405
Colin LeMahieu987b0942015-02-04 20:38:01 +00001406inline bool HexagonDAGToDAGISel::SelectAddrGA(SDValue &N, SDValue &R) {
1407 return SelectGlobalAddress(N, R, false);
1408}
1409
Colin LeMahieu51491352015-02-04 22:36:28 +00001410inline bool HexagonDAGToDAGISel::SelectAddrGP(SDValue &N, SDValue &R) {
1411 return SelectGlobalAddress(N, R, true);
1412}
1413
Colin LeMahieu987b0942015-02-04 20:38:01 +00001414bool HexagonDAGToDAGISel::SelectGlobalAddress(SDValue &N, SDValue &R,
1415 bool UseGP) {
1416 switch (N.getOpcode()) {
1417 case ISD::ADD: {
1418 SDValue N0 = N.getOperand(0);
1419 SDValue N1 = N.getOperand(1);
1420 unsigned GAOpc = N0.getOpcode();
1421 if (UseGP && GAOpc != HexagonISD::CONST32_GP)
1422 return false;
1423 if (!UseGP && GAOpc != HexagonISD::CONST32)
1424 return false;
1425 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N1)) {
1426 SDValue Addr = N0.getOperand(0);
1427 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Addr)) {
1428 if (GA->getOpcode() == ISD::TargetGlobalAddress) {
1429 uint64_t NewOff = GA->getOffset() + (uint64_t)Const->getSExtValue();
1430 R = CurDAG->getTargetGlobalAddress(GA->getGlobal(), SDLoc(Const),
1431 N.getValueType(), NewOff);
1432 return true;
1433 }
1434 }
1435 }
1436 break;
1437 }
1438 case HexagonISD::CONST32:
1439 // The operand(0) of CONST32 is TargetGlobalAddress, which is what we
1440 // want in the instruction.
1441 if (!UseGP)
1442 R = N.getOperand(0);
1443 return !UseGP;
1444 case HexagonISD::CONST32_GP:
1445 if (UseGP)
1446 R = N.getOperand(0);
1447 return UseGP;
1448 default:
1449 return false;
1450 }
1451
1452 return false;
1453}
1454
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001455bool HexagonDAGToDAGISel::isValueExtension(const SDValue &Val,
1456 unsigned FromBits, SDValue &Src) {
Colin LeMahieu0ee02fc2015-01-19 20:31:18 +00001457 unsigned Opc = Val.getOpcode();
1458 switch (Opc) {
1459 case ISD::SIGN_EXTEND:
1460 case ISD::ZERO_EXTEND:
1461 case ISD::ANY_EXTEND: {
1462 SDValue const &Op0 = Val.getOperand(0);
1463 EVT T = Op0.getValueType();
1464 if (T.isInteger() && T.getSizeInBits() == FromBits) {
1465 Src = Op0;
1466 return true;
1467 }
1468 break;
1469 }
1470 case ISD::SIGN_EXTEND_INREG:
1471 case ISD::AssertSext:
1472 case ISD::AssertZext:
1473 if (Val.getOperand(0).getValueType().isInteger()) {
1474 VTSDNode *T = cast<VTSDNode>(Val.getOperand(1));
1475 if (T->getVT().getSizeInBits() == FromBits) {
1476 Src = Val.getOperand(0);
1477 return true;
1478 }
1479 }
1480 break;
1481 case ISD::AND: {
1482 // Check if this is an AND with "FromBits" of lower bits set to 1.
1483 uint64_t FromMask = (1 << FromBits) - 1;
1484 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(0))) {
1485 if (C->getZExtValue() == FromMask) {
1486 Src = Val.getOperand(1);
1487 return true;
1488 }
1489 }
1490 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(1))) {
1491 if (C->getZExtValue() == FromMask) {
1492 Src = Val.getOperand(0);
1493 return true;
1494 }
1495 }
1496 break;
1497 }
1498 case ISD::OR:
1499 case ISD::XOR: {
1500 // OR/XOR with the lower "FromBits" bits set to 0.
1501 uint64_t FromMask = (1 << FromBits) - 1;
1502 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(0))) {
1503 if ((C->getZExtValue() & FromMask) == 0) {
1504 Src = Val.getOperand(1);
1505 return true;
1506 }
1507 }
1508 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(1))) {
1509 if ((C->getZExtValue() & FromMask) == 0) {
1510 Src = Val.getOperand(0);
1511 return true;
1512 }
1513 }
1514 }
1515 default:
1516 break;
1517 }
1518 return false;
1519}
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +00001520
Krzysztof Parzyszekbba0bf72016-07-15 15:35:52 +00001521
1522bool HexagonDAGToDAGISel::orIsAdd(const SDNode *N) const {
1523 assert(N->getOpcode() == ISD::OR);
1524 auto *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
1525 assert(C);
1526
1527 // Detect when "or" is used to add an offset to a stack object.
1528 if (auto *FN = dyn_cast<FrameIndexSDNode>(N->getOperand(0))) {
Matthias Braun941a7052016-07-28 18:40:00 +00001529 MachineFrameInfo &MFI = MF->getFrameInfo();
1530 unsigned A = MFI.getObjectAlignment(FN->getIndex());
Krzysztof Parzyszekbba0bf72016-07-15 15:35:52 +00001531 assert(isPowerOf2_32(A));
1532 int32_t Off = C->getSExtValue();
1533 // If the alleged offset fits in the zero bits guaranteed by
1534 // the alignment, then this or is really an add.
1535 return (Off >= 0) && (((A-1) & Off) == unsigned(Off));
1536 }
1537 return false;
1538}
1539
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +00001540bool HexagonDAGToDAGISel::isAlignedMemNode(const MemSDNode *N) const {
1541 return N->getAlignment() >= N->getMemoryVT().getStoreSize();
1542}