blob: d111360319542deec9156490026b91fcb5253a3b [file] [log] [blame]
Rafael Espindolaffdc24b2006-05-14 22:18:28 +00001//===-- ARMISelDAGToDAG.cpp - A dag to dag inst selector for ARM ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Rafael Espindolaffdc24b2006-05-14 22:18:28 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines an instruction selector for the ARM target.
11//
12//===----------------------------------------------------------------------===//
13
14#include "ARM.h"
Evan Cheng62c7b5b2010-12-05 22:04:16 +000015#include "ARMBaseInstrInfo.h"
Rafael Espindolaffdc24b2006-05-14 22:18:28 +000016#include "ARMTargetMachine.h"
Evan Chenga20cde32011-07-20 23:34:39 +000017#include "MCTargetDesc/ARMAddressingModes.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000018#include "Utils/ARMBaseInfo.h"
Luke Cheeseman85fd06d2015-06-01 12:02:47 +000019#include "llvm/ADT/StringSwitch.h"
Rafael Espindolaffdc24b2006-05-14 22:18:28 +000020#include "llvm/CodeGen/MachineFrameInfo.h"
21#include "llvm/CodeGen/MachineFunction.h"
22#include "llvm/CodeGen/MachineInstrBuilder.h"
Weiming Zhaoc5987002013-02-14 18:10:21 +000023#include "llvm/CodeGen/MachineRegisterInfo.h"
Rafael Espindolaffdc24b2006-05-14 22:18:28 +000024#include "llvm/CodeGen/SelectionDAG.h"
25#include "llvm/CodeGen/SelectionDAGISel.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000026#include "llvm/CodeGen/TargetLowering.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000027#include "llvm/IR/CallingConv.h"
28#include "llvm/IR/Constants.h"
29#include "llvm/IR/DerivedTypes.h"
30#include "llvm/IR/Function.h"
31#include "llvm/IR/Intrinsics.h"
32#include "llvm/IR/LLVMContext.h"
Evan Cheng8e6b40a2010-05-04 20:39:49 +000033#include "llvm/Support/CommandLine.h"
Rafael Espindolaffdc24b2006-05-14 22:18:28 +000034#include "llvm/Support/Debug.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000035#include "llvm/Support/ErrorHandling.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000036#include "llvm/Target/TargetOptions.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000037
Rafael Espindolaffdc24b2006-05-14 22:18:28 +000038using namespace llvm;
39
Chandler Carruth84e68b22014-04-22 02:41:26 +000040#define DEBUG_TYPE "arm-isel"
41
Evan Cheng59069ec2010-07-30 23:33:54 +000042static cl::opt<bool>
43DisableShifterOp("disable-shifter-op", cl::Hidden,
44 cl::desc("Disable isel of shifter-op"),
45 cl::init(false));
46
Rafael Espindolaffdc24b2006-05-14 22:18:28 +000047//===--------------------------------------------------------------------===//
48/// ARMDAGToDAGISel - ARM specific code to select ARM machine
49/// instructions for SelectionDAG operations.
50///
51namespace {
Jim Grosbach08605202010-09-29 19:03:54 +000052
Rafael Espindolaffdc24b2006-05-14 22:18:28 +000053class ARMDAGToDAGISel : public SelectionDAGISel {
Evan Cheng10043e22007-01-19 07:51:42 +000054 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
55 /// make the right decision when generating code for different targets.
56 const ARMSubtarget *Subtarget;
57
Rafael Espindolaffdc24b2006-05-14 22:18:28 +000058public:
Eric Christopher2f991c92014-07-03 22:24:49 +000059 explicit ARMDAGToDAGISel(ARMBaseTargetMachine &tm, CodeGenOpt::Level OptLevel)
60 : SelectionDAGISel(tm, OptLevel) {}
Rafael Espindolaffdc24b2006-05-14 22:18:28 +000061
Eric Christopher0e6e7cf2014-05-22 02:00:27 +000062 bool runOnMachineFunction(MachineFunction &MF) override {
63 // Reset the subtarget each time through.
Eric Christopher22b2ad22015-02-20 08:24:37 +000064 Subtarget = &MF.getSubtarget<ARMSubtarget>();
Eric Christopher0e6e7cf2014-05-22 02:00:27 +000065 SelectionDAGISel::runOnMachineFunction(MF);
66 return true;
67 }
68
Mehdi Amini117296c2016-10-01 02:56:57 +000069 StringRef getPassName() const override { return "ARM Instruction Selection"; }
Anton Korobeynikov02bb33c2009-06-17 18:13:58 +000070
Craig Topper6bc27bf2014-03-10 02:09:33 +000071 void PreprocessISelDAG() override;
Evan Chengeae6d2c2012-12-19 20:16:09 +000072
Bob Wilson4facd962009-10-08 18:51:31 +000073 /// getI32Imm - Return a target constant of type i32 with the specified
74 /// value.
Benjamin Kramerbdc49562016-06-12 15:39:02 +000075 inline SDValue getI32Imm(unsigned Imm, const SDLoc &dl) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000076 return CurDAG->getTargetConstant(Imm, dl, MVT::i32);
Anton Korobeynikov02bb33c2009-06-17 18:13:58 +000077 }
78
Justin Bogner45571362016-05-12 00:31:09 +000079 void Select(SDNode *N) override;
Evan Cheng62c7b5b2010-12-05 22:04:16 +000080
81 bool hasNoVMLxHazardUse(SDNode *N) const;
Evan Cheng59bbc542010-10-27 23:41:30 +000082 bool isShifterOpProfitable(const SDValue &Shift,
83 ARM_AM::ShiftOpc ShOpcVal, unsigned ShAmt);
Owen Andersonb595ed02011-07-21 18:54:16 +000084 bool SelectRegShifterOperand(SDValue N, SDValue &A,
85 SDValue &B, SDValue &C,
86 bool CheckProfitability = true);
87 bool SelectImmShifterOperand(SDValue N, SDValue &A,
Owen Anderson04912702011-07-21 23:38:37 +000088 SDValue &B, bool CheckProfitability = true);
89 bool SelectShiftRegShifterOperand(SDValue N, SDValue &A,
Owen Anderson6d557452011-03-18 19:46:58 +000090 SDValue &B, SDValue &C) {
91 // Don't apply the profitability check
Owen Anderson04912702011-07-21 23:38:37 +000092 return SelectRegShifterOperand(N, A, B, C, false);
93 }
94 bool SelectShiftImmShifterOperand(SDValue N, SDValue &A,
95 SDValue &B) {
96 // Don't apply the profitability check
97 return SelectImmShifterOperand(N, A, B, false);
Owen Anderson6d557452011-03-18 19:46:58 +000098 }
99
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000100 bool SelectAddrModeImm12(SDValue N, SDValue &Base, SDValue &OffImm);
101 bool SelectLdStSOReg(SDValue N, SDValue &Base, SDValue &Offset, SDValue &Opc);
102
Tim Northover42180442013-08-22 09:57:11 +0000103 bool SelectCMOVPred(SDValue N, SDValue &Pred, SDValue &Reg) {
104 const ConstantSDNode *CN = cast<ConstantSDNode>(N);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000105 Pred = CurDAG->getTargetConstant(CN->getZExtValue(), SDLoc(N), MVT::i32);
Tim Northover42180442013-08-22 09:57:11 +0000106 Reg = CurDAG->getRegister(ARM::CPSR, MVT::i32);
107 return true;
108 }
109
Owen Anderson2aedba62011-07-26 20:54:26 +0000110 bool SelectAddrMode2OffsetReg(SDNode *Op, SDValue N,
111 SDValue &Offset, SDValue &Opc);
112 bool SelectAddrMode2OffsetImm(SDNode *Op, SDValue N,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000113 SDValue &Offset, SDValue &Opc);
Owen Anderson4d5c8f82011-08-29 20:16:50 +0000114 bool SelectAddrMode2OffsetImmPre(SDNode *Op, SDValue N,
115 SDValue &Offset, SDValue &Opc);
Jim Grosbachf0c95ca2011-08-05 20:35:44 +0000116 bool SelectAddrOffsetNone(SDValue N, SDValue &Base);
Chris Lattner0e023ea2010-09-21 20:31:19 +0000117 bool SelectAddrMode3(SDValue N, SDValue &Base,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000118 SDValue &Offset, SDValue &Opc);
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000119 bool SelectAddrMode3Offset(SDNode *Op, SDValue N,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000120 SDValue &Offset, SDValue &Opc);
Sjoerd Meijer011de9c2018-01-26 09:26:40 +0000121 bool IsAddressingMode5(SDValue N, SDValue &Base, SDValue &Offset,
122 int Lwb, int Upb, bool FP16);
123 bool SelectAddrMode5(SDValue N, SDValue &Base, SDValue &Offset);
124 bool SelectAddrMode5FP16(SDValue N, SDValue &Base, SDValue &Offset);
Bob Wilsondd9fbaa2010-11-01 23:40:51 +0000125 bool SelectAddrMode6(SDNode *Parent, SDValue N, SDValue &Addr,SDValue &Align);
Bob Wilsone3ecd5f2011-02-25 06:42:42 +0000126 bool SelectAddrMode6Offset(SDNode *Op, SDValue N, SDValue &Offset);
Rafael Espindolaffdc24b2006-05-14 22:18:28 +0000127
Evan Chengdfce83c2011-01-17 08:03:18 +0000128 bool SelectAddrModePC(SDValue N, SDValue &Offset, SDValue &Label);
Evan Cheng10043e22007-01-19 07:51:42 +0000129
Bill Wendling092a7bd2010-12-14 03:36:38 +0000130 // Thumb Addressing Modes:
Chris Lattner0e023ea2010-09-21 20:31:19 +0000131 bool SelectThumbAddrModeRR(SDValue N, SDValue &Base, SDValue &Offset);
Bill Wendling092a7bd2010-12-14 03:36:38 +0000132 bool SelectThumbAddrModeImm5S(SDValue N, unsigned Scale, SDValue &Base,
133 SDValue &OffImm);
134 bool SelectThumbAddrModeImm5S1(SDValue N, SDValue &Base,
135 SDValue &OffImm);
136 bool SelectThumbAddrModeImm5S2(SDValue N, SDValue &Base,
137 SDValue &OffImm);
138 bool SelectThumbAddrModeImm5S4(SDValue N, SDValue &Base,
139 SDValue &OffImm);
Chris Lattner0e023ea2010-09-21 20:31:19 +0000140 bool SelectThumbAddrModeSP(SDValue N, SDValue &Base, SDValue &OffImm);
Evan Cheng10043e22007-01-19 07:51:42 +0000141
Bill Wendling092a7bd2010-12-14 03:36:38 +0000142 // Thumb 2 Addressing Modes:
Chris Lattner0e023ea2010-09-21 20:31:19 +0000143 bool SelectT2AddrModeImm12(SDValue N, SDValue &Base, SDValue &OffImm);
144 bool SelectT2AddrModeImm8(SDValue N, SDValue &Base,
Evan Chengb23b50d2009-06-29 07:51:04 +0000145 SDValue &OffImm);
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000146 bool SelectT2AddrModeImm8Offset(SDNode *Op, SDValue N,
Evan Cheng84c6cda2009-07-02 07:28:31 +0000147 SDValue &OffImm);
Chris Lattner0e023ea2010-09-21 20:31:19 +0000148 bool SelectT2AddrModeSoReg(SDValue N, SDValue &Base,
Evan Chengb23b50d2009-06-29 07:51:04 +0000149 SDValue &OffReg, SDValue &ShImm);
Tim Northovera7ecd242013-07-16 09:46:55 +0000150 bool SelectT2AddrModeExclusive(SDValue N, SDValue &Base, SDValue &OffImm);
Evan Chengb23b50d2009-06-29 07:51:04 +0000151
Evan Cheng0fc80842010-11-12 22:42:47 +0000152 inline bool is_so_imm(unsigned Imm) const {
153 return ARM_AM::getSOImmVal(Imm) != -1;
154 }
155
156 inline bool is_so_imm_not(unsigned Imm) const {
157 return ARM_AM::getSOImmVal(~Imm) != -1;
158 }
159
160 inline bool is_t2_so_imm(unsigned Imm) const {
161 return ARM_AM::getT2SOImmVal(Imm) != -1;
162 }
163
164 inline bool is_t2_so_imm_not(unsigned Imm) const {
165 return ARM_AM::getT2SOImmVal(~Imm) != -1;
166 }
167
Rafael Espindolaffdc24b2006-05-14 22:18:28 +0000168 // Include the pieces autogenerated from the target description.
169#include "ARMGenDAGISel.inc"
Bob Wilsona2c462b2009-05-19 05:53:42 +0000170
171private:
Tim Northovereaee28b2016-09-19 09:11:09 +0000172 void transferMemOperands(SDNode *Src, SDNode *Dst);
173
Justin Bogner45571362016-05-12 00:31:09 +0000174 /// Indexed (pre/post inc/dec) load matching code for ARM.
175 bool tryARMIndexedLoad(SDNode *N);
James Molloyb3326df2016-07-15 08:03:56 +0000176 bool tryT1IndexedLoad(SDNode *N);
Justin Bogner45571362016-05-12 00:31:09 +0000177 bool tryT2IndexedLoad(SDNode *N);
Evan Cheng84c6cda2009-07-02 07:28:31 +0000178
Bob Wilson340861d2010-03-23 05:25:43 +0000179 /// SelectVLD - Select NEON load intrinsics. NumVecs should be
180 /// 1, 2, 3 or 4. The opcode arrays specify the instructions used for
Bob Wilson12b47992009-10-14 17:28:52 +0000181 /// loads of D registers and even subregs and odd subregs of Q registers.
Bob Wilson340861d2010-03-23 05:25:43 +0000182 /// For NumVecs <= 2, QOpcodes1 is not used.
Justin Bogner45571362016-05-12 00:31:09 +0000183 void SelectVLD(SDNode *N, bool isUpdating, unsigned NumVecs,
184 const uint16_t *DOpcodes, const uint16_t *QOpcodes0,
185 const uint16_t *QOpcodes1);
Bob Wilson12b47992009-10-14 17:28:52 +0000186
Bob Wilsonc350cdf2009-10-14 18:32:29 +0000187 /// SelectVST - Select NEON store intrinsics. NumVecs should
Bob Wilsoncc0a2a72010-03-23 06:20:33 +0000188 /// be 1, 2, 3 or 4. The opcode arrays specify the instructions used for
Bob Wilsonc350cdf2009-10-14 18:32:29 +0000189 /// stores of D registers and even subregs and odd subregs of Q registers.
Bob Wilsoncc0a2a72010-03-23 06:20:33 +0000190 /// For NumVecs <= 2, QOpcodes1 is not used.
Justin Bogner45571362016-05-12 00:31:09 +0000191 void SelectVST(SDNode *N, bool isUpdating, unsigned NumVecs,
192 const uint16_t *DOpcodes, const uint16_t *QOpcodes0,
193 const uint16_t *QOpcodes1);
Bob Wilsonc350cdf2009-10-14 18:32:29 +0000194
Bob Wilson93117bc2009-10-14 16:46:45 +0000195 /// SelectVLDSTLane - Select NEON load/store lane intrinsics. NumVecs should
Bob Wilson4145e3a2009-10-14 16:19:03 +0000196 /// be 2, 3 or 4. The opcode arrays specify the instructions used for
Bob Wilsond5c57a52010-09-13 23:01:35 +0000197 /// load/store of D registers and Q registers.
Justin Bogner45571362016-05-12 00:31:09 +0000198 void SelectVLDSTLane(SDNode *N, bool IsLoad, bool isUpdating,
199 unsigned NumVecs, const uint16_t *DOpcodes,
200 const uint16_t *QOpcodes);
Bob Wilson4145e3a2009-10-14 16:19:03 +0000201
Bob Wilson2d790df2010-11-28 06:51:26 +0000202 /// SelectVLDDup - Select NEON load-duplicate intrinsics. NumVecs
Eli Friedmanf624ec22016-12-16 18:44:08 +0000203 /// should be 1, 2, 3 or 4. The opcode array specifies the instructions used
Bob Wilson2d790df2010-11-28 06:51:26 +0000204 /// for loading D registers. (Q registers are not supported.)
Justin Bogner45571362016-05-12 00:31:09 +0000205 void SelectVLDDup(SDNode *N, bool isUpdating, unsigned NumVecs,
Eli Friedmanf624ec22016-12-16 18:44:08 +0000206 const uint16_t *DOpcodes,
207 const uint16_t *QOpcodes = nullptr);
Bob Wilson2d790df2010-11-28 06:51:26 +0000208
Justin Bogner45571362016-05-12 00:31:09 +0000209 /// Try to select SBFX/UBFX instructions for ARM.
210 bool tryV6T2BitfieldExtractOp(SDNode *N, bool isSigned);
Sandeep Patel423e42b2009-10-13 18:59:48 +0000211
Bill Wendlinga7d697e2011-10-10 22:59:55 +0000212 // Select special operations if node forms integer ABS pattern
Justin Bogner45571362016-05-12 00:31:09 +0000213 bool tryABSOp(SDNode *N);
Bill Wendlinga7d697e2011-10-10 22:59:55 +0000214
Justin Bogner45571362016-05-12 00:31:09 +0000215 bool tryReadRegister(SDNode *N);
216 bool tryWriteRegister(SDNode *N);
Luke Cheeseman85fd06d2015-06-01 12:02:47 +0000217
Justin Bogner45571362016-05-12 00:31:09 +0000218 bool tryInlineAsm(SDNode *N);
Weiming Zhaoc5987002013-02-14 18:10:21 +0000219
Sjoerd Meijer96e10b52016-12-15 09:38:59 +0000220 void SelectCMPZ(SDNode *N, bool &SwitchEQNEToPLMI);
James Molloye7d97362016-11-03 14:08:01 +0000221
Justin Bogner45571362016-05-12 00:31:09 +0000222 void SelectCMP_SWAP(SDNode *N);
Tim Northoverb629c772016-04-18 21:48:55 +0000223
Evan Chengd9c55362009-07-02 01:23:32 +0000224 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
225 /// inline asm expressions.
Daniel Sanders60f1db02015-03-13 12:45:09 +0000226 bool SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID,
Craig Topper6bc27bf2014-03-10 02:09:33 +0000227 std::vector<SDValue> &OutOps) override;
Bob Wilsone6b778d2009-10-06 22:01:59 +0000228
Weiming Zhao95782222012-11-17 00:23:35 +0000229 // Form pairs of consecutive R, S, D, or Q registers.
Weiming Zhao8f56f882012-11-16 21:55:34 +0000230 SDNode *createGPRPairNode(EVT VT, SDValue V0, SDValue V1);
Weiming Zhao95782222012-11-17 00:23:35 +0000231 SDNode *createSRegPairNode(EVT VT, SDValue V0, SDValue V1);
232 SDNode *createDRegPairNode(EVT VT, SDValue V0, SDValue V1);
233 SDNode *createQRegPairNode(EVT VT, SDValue V0, SDValue V1);
Evan Chengc2ae5f52010-05-10 17:34:18 +0000234
Bob Wilsond8a9a042010-06-04 00:04:02 +0000235 // Form sequences of 4 consecutive S, D, or Q registers.
Weiming Zhao95782222012-11-17 00:23:35 +0000236 SDNode *createQuadSRegsNode(EVT VT, SDValue V0, SDValue V1, SDValue V2, SDValue V3);
237 SDNode *createQuadDRegsNode(EVT VT, SDValue V0, SDValue V1, SDValue V2, SDValue V3);
238 SDNode *createQuadQRegsNode(EVT VT, SDValue V0, SDValue V1, SDValue V2, SDValue V3);
Bob Wilsondd9fbaa2010-11-01 23:40:51 +0000239
240 // Get the alignment operand for a NEON VLD or VST instruction.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000241 SDValue GetVLDSTAlign(SDValue Align, const SDLoc &dl, unsigned NumVecs,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000242 bool is64BitVector);
John Brawn056e6782015-09-14 15:19:41 +0000243
244 /// Returns the number of instructions required to materialize the given
245 /// constant in a register, or 3 if a literal pool load is needed.
246 unsigned ConstantMaterializationCost(unsigned Val) const;
247
248 /// Checks if N is a multiplication by a constant where we can extract out a
249 /// power of two from the constant so that it can be used in a shift, but only
250 /// if it simplifies the materialization of the constant. Returns true if it
251 /// is, and assigns to PowerOfTwo the power of two that should be extracted
252 /// out and to NewMulConst the new constant to be multiplied by.
253 bool canExtractShiftFromMul(const SDValue &N, unsigned MaxShift,
254 unsigned &PowerOfTwo, SDValue &NewMulConst) const;
255
256 /// Replace N with M in CurDAG, in a way that also ensures that M gets
257 /// selected when N would have been selected.
258 void replaceDAGValue(const SDValue &N, SDValue M);
Rafael Espindolaffdc24b2006-05-14 22:18:28 +0000259};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000260}
Rafael Espindolaffdc24b2006-05-14 22:18:28 +0000261
Sandeep Patel423e42b2009-10-13 18:59:48 +0000262/// isInt32Immediate - This method tests to see if the node is a 32-bit constant
263/// operand. If so Imm will receive the 32-bit value.
264static bool isInt32Immediate(SDNode *N, unsigned &Imm) {
265 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
266 Imm = cast<ConstantSDNode>(N)->getZExtValue();
267 return true;
268 }
269 return false;
270}
271
272// isInt32Immediate - This method tests to see if a constant operand.
273// If so Imm will receive the 32 bit value.
274static bool isInt32Immediate(SDValue N, unsigned &Imm) {
275 return isInt32Immediate(N.getNode(), Imm);
276}
277
278// isOpcWithIntImmediate - This method tests to see if the node is a specific
279// opcode and that it has a immediate integer right operand.
280// If so Imm will receive the 32 bit value.
281static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
282 return N->getOpcode() == Opc &&
283 isInt32Immediate(N->getOperand(1).getNode(), Imm);
284}
285
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +0000286/// \brief Check whether a particular node is a constant value representable as
Dmitri Gribenko5485acd2012-09-14 14:57:36 +0000287/// (N * Scale) where (N in [\p RangeMin, \p RangeMax).
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +0000288///
289/// \param ScaledConstant [out] - On success, the pre-scaled constant value.
Jakob Stoklund Olesen2056d152011-09-23 22:10:33 +0000290static bool isScaledConstantInRange(SDValue Node, int Scale,
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +0000291 int RangeMin, int RangeMax,
292 int &ScaledConstant) {
Jakob Stoklund Olesen2056d152011-09-23 22:10:33 +0000293 assert(Scale > 0 && "Invalid scale!");
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +0000294
295 // Check that this is a constant.
296 const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Node);
297 if (!C)
298 return false;
299
300 ScaledConstant = (int) C->getZExtValue();
301 if ((ScaledConstant % Scale) != 0)
302 return false;
303
304 ScaledConstant /= Scale;
305 return ScaledConstant >= RangeMin && ScaledConstant < RangeMax;
306}
307
Evan Chengeae6d2c2012-12-19 20:16:09 +0000308void ARMDAGToDAGISel::PreprocessISelDAG() {
309 if (!Subtarget->hasV6T2Ops())
310 return;
311
312 bool isThumb2 = Subtarget->isThumb();
313 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
314 E = CurDAG->allnodes_end(); I != E; ) {
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +0000315 SDNode *N = &*I++; // Preincrement iterator to avoid invalidation issues.
Evan Chengeae6d2c2012-12-19 20:16:09 +0000316
317 if (N->getOpcode() != ISD::ADD)
318 continue;
319
320 // Look for (add X1, (and (srl X2, c1), c2)) where c2 is constant with
321 // leading zeros, followed by consecutive set bits, followed by 1 or 2
322 // trailing zeros, e.g. 1020.
323 // Transform the expression to
324 // (add X1, (shl (and (srl X2, c1), (c2>>tz)), tz)) where tz is the number
325 // of trailing zeros of c2. The left shift would be folded as an shifter
326 // operand of 'add' and the 'and' and 'srl' would become a bits extraction
327 // node (UBFX).
328
329 SDValue N0 = N->getOperand(0);
330 SDValue N1 = N->getOperand(1);
331 unsigned And_imm = 0;
332 if (!isOpcWithIntImmediate(N1.getNode(), ISD::AND, And_imm)) {
333 if (isOpcWithIntImmediate(N0.getNode(), ISD::AND, And_imm))
334 std::swap(N0, N1);
335 }
336 if (!And_imm)
337 continue;
338
339 // Check if the AND mask is an immediate of the form: 000.....1111111100
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000340 unsigned TZ = countTrailingZeros(And_imm);
Evan Chengeae6d2c2012-12-19 20:16:09 +0000341 if (TZ != 1 && TZ != 2)
342 // Be conservative here. Shifter operands aren't always free. e.g. On
343 // Swift, left shifter operand of 1 / 2 for free but others are not.
344 // e.g.
345 // ubfx r3, r1, #16, #8
346 // ldr.w r3, [r0, r3, lsl #2]
347 // vs.
348 // mov.w r9, #1020
349 // and.w r2, r9, r1, lsr #14
350 // ldr r2, [r0, r2]
351 continue;
352 And_imm >>= TZ;
353 if (And_imm & (And_imm + 1))
354 continue;
355
356 // Look for (and (srl X, c1), c2).
357 SDValue Srl = N1.getOperand(0);
358 unsigned Srl_imm = 0;
359 if (!isOpcWithIntImmediate(Srl.getNode(), ISD::SRL, Srl_imm) ||
360 (Srl_imm <= 2))
361 continue;
362
363 // Make sure first operand is not a shifter operand which would prevent
364 // folding of the left shift.
365 SDValue CPTmp0;
366 SDValue CPTmp1;
367 SDValue CPTmp2;
368 if (isThumb2) {
John Brawnd8b405a2015-09-07 11:45:18 +0000369 if (SelectImmShifterOperand(N0, CPTmp0, CPTmp1))
Evan Chengeae6d2c2012-12-19 20:16:09 +0000370 continue;
371 } else {
372 if (SelectImmShifterOperand(N0, CPTmp0, CPTmp1) ||
373 SelectRegShifterOperand(N0, CPTmp0, CPTmp1, CPTmp2))
374 continue;
375 }
376
377 // Now make the transformation.
Andrew Trickef9de2a2013-05-25 02:42:55 +0000378 Srl = CurDAG->getNode(ISD::SRL, SDLoc(Srl), MVT::i32,
Evan Chengeae6d2c2012-12-19 20:16:09 +0000379 Srl.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000380 CurDAG->getConstant(Srl_imm + TZ, SDLoc(Srl),
381 MVT::i32));
Andrew Trickef9de2a2013-05-25 02:42:55 +0000382 N1 = CurDAG->getNode(ISD::AND, SDLoc(N1), MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000383 Srl,
384 CurDAG->getConstant(And_imm, SDLoc(Srl), MVT::i32));
Andrew Trickef9de2a2013-05-25 02:42:55 +0000385 N1 = CurDAG->getNode(ISD::SHL, SDLoc(N1), MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000386 N1, CurDAG->getConstant(TZ, SDLoc(Srl), MVT::i32));
Evan Chengeae6d2c2012-12-19 20:16:09 +0000387 CurDAG->UpdateNodeOperands(N, N0, N1);
Jim Grosbach1a597112014-04-03 23:43:18 +0000388 }
Evan Chengeae6d2c2012-12-19 20:16:09 +0000389}
390
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000391/// hasNoVMLxHazardUse - Return true if it's desirable to select a FP MLA / MLS
392/// node. VFP / NEON fp VMLA / VMLS instructions have special RAW hazards (at
393/// least on current ARM implementations) which should be avoidded.
394bool ARMDAGToDAGISel::hasNoVMLxHazardUse(SDNode *N) const {
395 if (OptLevel == CodeGenOpt::None)
396 return true;
397
Diana Picus575f2bb2016-07-07 09:11:39 +0000398 if (!Subtarget->hasVMLxHazards())
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000399 return true;
400
401 if (!N->hasOneUse())
402 return false;
403
404 SDNode *Use = *N->use_begin();
405 if (Use->getOpcode() == ISD::CopyToReg)
406 return true;
407 if (Use->isMachineOpcode()) {
Eric Christopher2f991c92014-07-03 22:24:49 +0000408 const ARMBaseInstrInfo *TII = static_cast<const ARMBaseInstrInfo *>(
Eric Christopherfc6de422014-08-05 02:39:49 +0000409 CurDAG->getSubtarget().getInstrInfo());
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000410
Evan Cheng6cc775f2011-06-28 19:10:37 +0000411 const MCInstrDesc &MCID = TII->get(Use->getMachineOpcode());
412 if (MCID.mayStore())
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000413 return true;
Evan Cheng6cc775f2011-06-28 19:10:37 +0000414 unsigned Opcode = MCID.getOpcode();
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000415 if (Opcode == ARM::VMOVRS || Opcode == ARM::VMOVRRD)
416 return true;
417 // vmlx feeding into another vmlx. We actually want to unfold
418 // the use later in the MLxExpansion pass. e.g.
419 // vmla
420 // vmla (stall 8 cycles)
421 //
422 // vmul (5 cycles)
423 // vadd (5 cycles)
424 // vmla
425 // This adds up to about 18 - 19 cycles.
426 //
427 // vmla
428 // vmul (stall 4 cycles)
429 // vadd adds up to about 14 cycles.
430 return TII->isFpMLxInstruction(Opcode);
431 }
432
433 return false;
434}
Sandeep Patel423e42b2009-10-13 18:59:48 +0000435
Evan Cheng59bbc542010-10-27 23:41:30 +0000436bool ARMDAGToDAGISel::isShifterOpProfitable(const SDValue &Shift,
437 ARM_AM::ShiftOpc ShOpcVal,
438 unsigned ShAmt) {
Bob Wilsone8a549c2012-09-29 21:43:49 +0000439 if (!Subtarget->isLikeA9() && !Subtarget->isSwift())
Evan Cheng59bbc542010-10-27 23:41:30 +0000440 return true;
441 if (Shift.hasOneUse())
442 return true;
443 // R << 2 is free.
Bob Wilsone8a549c2012-09-29 21:43:49 +0000444 return ShOpcVal == ARM_AM::lsl &&
445 (ShAmt == 2 || (Subtarget->isSwift() && ShAmt == 1));
Evan Cheng59bbc542010-10-27 23:41:30 +0000446}
447
John Brawn056e6782015-09-14 15:19:41 +0000448unsigned ARMDAGToDAGISel::ConstantMaterializationCost(unsigned Val) const {
449 if (Subtarget->isThumb()) {
450 if (Val <= 255) return 1; // MOV
Weiming Zhaof68a6a72016-08-05 20:58:29 +0000451 if (Subtarget->hasV6T2Ops() &&
452 (Val <= 0xffff || ARM_AM::getT2SOImmValSplatVal(Val) != -1))
453 return 1; // MOVW
James Molloy65b6be12016-06-14 13:33:07 +0000454 if (Val <= 510) return 2; // MOV + ADDi8
John Brawn056e6782015-09-14 15:19:41 +0000455 if (~Val <= 255) return 2; // MOV + MVN
456 if (ARM_AM::isThumbImmShiftedVal(Val)) return 2; // MOV + LSL
457 } else {
458 if (ARM_AM::getSOImmVal(Val) != -1) return 1; // MOV
459 if (ARM_AM::getSOImmVal(~Val) != -1) return 1; // MVN
460 if (Subtarget->hasV6T2Ops() && Val <= 0xffff) return 1; // MOVW
461 if (ARM_AM::isSOImmTwoPartVal(Val)) return 2; // two instrs
462 }
463 if (Subtarget->useMovt(*MF)) return 2; // MOVW + MOVT
464 return 3; // Literal pool load
465}
466
467bool ARMDAGToDAGISel::canExtractShiftFromMul(const SDValue &N,
468 unsigned MaxShift,
469 unsigned &PowerOfTwo,
470 SDValue &NewMulConst) const {
471 assert(N.getOpcode() == ISD::MUL);
472 assert(MaxShift > 0);
473
474 // If the multiply is used in more than one place then changing the constant
475 // will make other uses incorrect, so don't.
476 if (!N.hasOneUse()) return false;
477 // Check if the multiply is by a constant
478 ConstantSDNode *MulConst = dyn_cast<ConstantSDNode>(N.getOperand(1));
479 if (!MulConst) return false;
480 // If the constant is used in more than one place then modifying it will mean
481 // we need to materialize two constants instead of one, which is a bad idea.
482 if (!MulConst->hasOneUse()) return false;
483 unsigned MulConstVal = MulConst->getZExtValue();
484 if (MulConstVal == 0) return false;
485
486 // Find the largest power of 2 that MulConstVal is a multiple of
487 PowerOfTwo = MaxShift;
488 while ((MulConstVal % (1 << PowerOfTwo)) != 0) {
489 --PowerOfTwo;
490 if (PowerOfTwo == 0) return false;
491 }
492
493 // Only optimise if the new cost is better
494 unsigned NewMulConstVal = MulConstVal / (1 << PowerOfTwo);
495 NewMulConst = CurDAG->getConstant(NewMulConstVal, SDLoc(N), MVT::i32);
496 unsigned OldCost = ConstantMaterializationCost(MulConstVal);
497 unsigned NewCost = ConstantMaterializationCost(NewMulConstVal);
498 return NewCost < OldCost;
499}
500
501void ARMDAGToDAGISel::replaceDAGValue(const SDValue &N, SDValue M) {
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +0000502 CurDAG->RepositionNode(N.getNode()->getIterator(), M.getNode());
John Brawn056e6782015-09-14 15:19:41 +0000503 CurDAG->ReplaceAllUsesWith(N, M);
504}
505
Owen Andersonb595ed02011-07-21 18:54:16 +0000506bool ARMDAGToDAGISel::SelectImmShifterOperand(SDValue N,
Evan Chengb23b50d2009-06-29 07:51:04 +0000507 SDValue &BaseReg,
Owen Anderson6d557452011-03-18 19:46:58 +0000508 SDValue &Opc,
509 bool CheckProfitability) {
Evan Cheng59069ec2010-07-30 23:33:54 +0000510 if (DisableShifterOp)
511 return false;
512
John Brawn056e6782015-09-14 15:19:41 +0000513 // If N is a multiply-by-constant and it's profitable to extract a shift and
514 // use it in a shifted operand do so.
515 if (N.getOpcode() == ISD::MUL) {
516 unsigned PowerOfTwo = 0;
517 SDValue NewMulConst;
518 if (canExtractShiftFromMul(N, 31, PowerOfTwo, NewMulConst)) {
Justin Bogner8752be72016-05-05 01:43:49 +0000519 HandleSDNode Handle(N);
Benjamin Kramer58dadd52017-04-20 18:29:14 +0000520 SDLoc Loc(N);
John Brawn056e6782015-09-14 15:19:41 +0000521 replaceDAGValue(N.getOperand(1), NewMulConst);
Justin Bogner8752be72016-05-05 01:43:49 +0000522 BaseReg = Handle.getValue();
Benjamin Kramer58dadd52017-04-20 18:29:14 +0000523 Opc = CurDAG->getTargetConstant(
524 ARM_AM::getSORegOpc(ARM_AM::lsl, PowerOfTwo), Loc, MVT::i32);
John Brawn056e6782015-09-14 15:19:41 +0000525 return true;
526 }
527 }
528
Evan Chenga20cde32011-07-20 23:34:39 +0000529 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOpcode());
Evan Chengb23b50d2009-06-29 07:51:04 +0000530
531 // Don't match base register only case. That is matched to a separate
532 // lower complexity pattern with explicit register operand.
533 if (ShOpcVal == ARM_AM::no_shift) return false;
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000534
Evan Chengb23b50d2009-06-29 07:51:04 +0000535 BaseReg = N.getOperand(0);
536 unsigned ShImmVal = 0;
Owen Andersonb595ed02011-07-21 18:54:16 +0000537 ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1));
538 if (!RHS) return false;
Owen Andersonb595ed02011-07-21 18:54:16 +0000539 ShImmVal = RHS->getZExtValue() & 31;
Evan Cheng59bbc542010-10-27 23:41:30 +0000540 Opc = CurDAG->getTargetConstant(ARM_AM::getSORegOpc(ShOpcVal, ShImmVal),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000541 SDLoc(N), MVT::i32);
Evan Cheng59bbc542010-10-27 23:41:30 +0000542 return true;
543}
544
Owen Andersonb595ed02011-07-21 18:54:16 +0000545bool ARMDAGToDAGISel::SelectRegShifterOperand(SDValue N,
546 SDValue &BaseReg,
547 SDValue &ShReg,
548 SDValue &Opc,
549 bool CheckProfitability) {
550 if (DisableShifterOp)
551 return false;
552
553 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOpcode());
554
555 // Don't match base register only case. That is matched to a separate
556 // lower complexity pattern with explicit register operand.
557 if (ShOpcVal == ARM_AM::no_shift) return false;
558
559 BaseReg = N.getOperand(0);
560 unsigned ShImmVal = 0;
561 ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1));
562 if (RHS) return false;
563
564 ShReg = N.getOperand(1);
565 if (CheckProfitability && !isShifterOpProfitable(N, ShOpcVal, ShImmVal))
566 return false;
567 Opc = CurDAG->getTargetConstant(ARM_AM::getSORegOpc(ShOpcVal, ShImmVal),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000568 SDLoc(N), MVT::i32);
Owen Andersonb595ed02011-07-21 18:54:16 +0000569 return true;
570}
571
572
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000573bool ARMDAGToDAGISel::SelectAddrModeImm12(SDValue N,
574 SDValue &Base,
575 SDValue &OffImm) {
576 // Match simple R + imm12 operands.
577
578 // Base only.
Chris Lattner46c01a32011-02-13 22:25:43 +0000579 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB &&
580 !CurDAG->isBaseWithConstantOffset(N)) {
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000581 if (N.getOpcode() == ISD::FrameIndex) {
Chris Lattner46c01a32011-02-13 22:25:43 +0000582 // Match frame index.
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000583 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Mehdi Amini44ede332015-07-09 02:09:04 +0000584 Base = CurDAG->getTargetFrameIndex(
585 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000586 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32);
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000587 return true;
Chris Lattner46c01a32011-02-13 22:25:43 +0000588 }
Owen Anderson6d557452011-03-18 19:46:58 +0000589
Chris Lattner46c01a32011-02-13 22:25:43 +0000590 if (N.getOpcode() == ARMISD::Wrapper &&
Tim Northoverbd41cf82016-01-07 09:03:03 +0000591 N.getOperand(0).getOpcode() != ISD::TargetGlobalAddress &&
Saleem Abdulrasoolf36005a2016-02-03 18:21:59 +0000592 N.getOperand(0).getOpcode() != ISD::TargetExternalSymbol &&
Tim Northoverbd41cf82016-01-07 09:03:03 +0000593 N.getOperand(0).getOpcode() != ISD::TargetGlobalTLSAddress) {
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000594 Base = N.getOperand(0);
595 } else
596 Base = N;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000597 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32);
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000598 return true;
599 }
600
601 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Renato Golin63e27982014-09-09 09:57:59 +0000602 int RHSC = (int)RHS->getSExtValue();
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000603 if (N.getOpcode() == ISD::SUB)
604 RHSC = -RHSC;
605
Renato Golin63e27982014-09-09 09:57:59 +0000606 if (RHSC > -0x1000 && RHSC < 0x1000) { // 12 bits
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000607 Base = N.getOperand(0);
608 if (Base.getOpcode() == ISD::FrameIndex) {
609 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
Mehdi Amini44ede332015-07-09 02:09:04 +0000610 Base = CurDAG->getTargetFrameIndex(
611 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000612 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000613 OffImm = CurDAG->getTargetConstant(RHSC, SDLoc(N), MVT::i32);
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000614 return true;
615 }
616 }
617
618 // Base only.
619 Base = N;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000620 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32);
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000621 return true;
622}
623
624
625
626bool ARMDAGToDAGISel::SelectLdStSOReg(SDValue N, SDValue &Base, SDValue &Offset,
627 SDValue &Opc) {
Evan Cheng59bbc542010-10-27 23:41:30 +0000628 if (N.getOpcode() == ISD::MUL &&
Bob Wilsone8a549c2012-09-29 21:43:49 +0000629 ((!Subtarget->isLikeA9() && !Subtarget->isSwift()) || N.hasOneUse())) {
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000630 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
631 // X * [3,5,9] -> X + X * [2,4,8] etc.
632 int RHSC = (int)RHS->getZExtValue();
633 if (RHSC & 1) {
634 RHSC = RHSC & ~1;
635 ARM_AM::AddrOpc AddSub = ARM_AM::add;
636 if (RHSC < 0) {
637 AddSub = ARM_AM::sub;
638 RHSC = - RHSC;
639 }
640 if (isPowerOf2_32(RHSC)) {
641 unsigned ShAmt = Log2_32(RHSC);
642 Base = Offset = N.getOperand(0);
643 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt,
644 ARM_AM::lsl),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000645 SDLoc(N), MVT::i32);
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000646 return true;
647 }
648 }
649 }
650 }
651
Chris Lattner46c01a32011-02-13 22:25:43 +0000652 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB &&
653 // ISD::OR that is equivalent to an ISD::ADD.
654 !CurDAG->isBaseWithConstantOffset(N))
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000655 return false;
656
657 // Leave simple R +/- imm12 operands for LDRi12
Chris Lattner46c01a32011-02-13 22:25:43 +0000658 if (N.getOpcode() == ISD::ADD || N.getOpcode() == ISD::OR) {
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +0000659 int RHSC;
660 if (isScaledConstantInRange(N.getOperand(1), /*Scale=*/1,
661 -0x1000+1, 0x1000, RHSC)) // 12 bits.
662 return false;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000663 }
664
665 // Otherwise this is R +/- [possibly shifted] R.
Chris Lattner46c01a32011-02-13 22:25:43 +0000666 ARM_AM::AddrOpc AddSub = N.getOpcode() == ISD::SUB ? ARM_AM::sub:ARM_AM::add;
Evan Chenga20cde32011-07-20 23:34:39 +0000667 ARM_AM::ShiftOpc ShOpcVal =
668 ARM_AM::getShiftOpcForNode(N.getOperand(1).getOpcode());
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000669 unsigned ShAmt = 0;
670
671 Base = N.getOperand(0);
672 Offset = N.getOperand(1);
673
674 if (ShOpcVal != ARM_AM::no_shift) {
675 // Check to see if the RHS of the shift is a constant, if not, we can't fold
676 // it.
677 if (ConstantSDNode *Sh =
678 dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) {
679 ShAmt = Sh->getZExtValue();
Evan Cheng59bbc542010-10-27 23:41:30 +0000680 if (isShifterOpProfitable(Offset, ShOpcVal, ShAmt))
681 Offset = N.getOperand(1).getOperand(0);
682 else {
683 ShAmt = 0;
684 ShOpcVal = ARM_AM::no_shift;
685 }
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000686 } else {
687 ShOpcVal = ARM_AM::no_shift;
688 }
689 }
690
691 // Try matching (R shl C) + (R).
Chris Lattner46c01a32011-02-13 22:25:43 +0000692 if (N.getOpcode() != ISD::SUB && ShOpcVal == ARM_AM::no_shift &&
Bob Wilsone8a549c2012-09-29 21:43:49 +0000693 !(Subtarget->isLikeA9() || Subtarget->isSwift() ||
694 N.getOperand(0).hasOneUse())) {
Evan Chenga20cde32011-07-20 23:34:39 +0000695 ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(0).getOpcode());
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000696 if (ShOpcVal != ARM_AM::no_shift) {
697 // Check to see if the RHS of the shift is a constant, if not, we can't
698 // fold it.
699 if (ConstantSDNode *Sh =
700 dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) {
701 ShAmt = Sh->getZExtValue();
Cameron Zwarich842f99a2011-10-05 23:39:02 +0000702 if (isShifterOpProfitable(N.getOperand(0), ShOpcVal, ShAmt)) {
Evan Cheng59bbc542010-10-27 23:41:30 +0000703 Offset = N.getOperand(0).getOperand(0);
704 Base = N.getOperand(1);
705 } else {
706 ShAmt = 0;
707 ShOpcVal = ARM_AM::no_shift;
708 }
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000709 } else {
710 ShOpcVal = ARM_AM::no_shift;
711 }
712 }
713 }
714
John Brawn056e6782015-09-14 15:19:41 +0000715 // If Offset is a multiply-by-constant and it's profitable to extract a shift
716 // and use it in a shifted operand do so.
Tim Northoverc4093c32016-01-29 19:18:46 +0000717 if (Offset.getOpcode() == ISD::MUL && N.hasOneUse()) {
John Brawn056e6782015-09-14 15:19:41 +0000718 unsigned PowerOfTwo = 0;
719 SDValue NewMulConst;
720 if (canExtractShiftFromMul(Offset, 31, PowerOfTwo, NewMulConst)) {
Tim Northover4a01ffb2017-05-02 22:45:19 +0000721 HandleSDNode Handle(Offset);
John Brawn056e6782015-09-14 15:19:41 +0000722 replaceDAGValue(Offset.getOperand(1), NewMulConst);
Tim Northover4a01ffb2017-05-02 22:45:19 +0000723 Offset = Handle.getValue();
John Brawn056e6782015-09-14 15:19:41 +0000724 ShAmt = PowerOfTwo;
725 ShOpcVal = ARM_AM::lsl;
726 }
727 }
728
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000729 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000730 SDLoc(N), MVT::i32);
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000731 return true;
732}
733
Owen Anderson2aedba62011-07-26 20:54:26 +0000734bool ARMDAGToDAGISel::SelectAddrMode2OffsetReg(SDNode *Op, SDValue N,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000735 SDValue &Offset, SDValue &Opc) {
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000736 unsigned Opcode = Op->getOpcode();
Evan Cheng10043e22007-01-19 07:51:42 +0000737 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
738 ? cast<LoadSDNode>(Op)->getAddressingMode()
739 : cast<StoreSDNode>(Op)->getAddressingMode();
740 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
741 ? ARM_AM::add : ARM_AM::sub;
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +0000742 int Val;
Owen Anderson2aedba62011-07-26 20:54:26 +0000743 if (isScaledConstantInRange(N, /*Scale=*/1, 0, 0x1000, Val))
744 return false;
Evan Cheng10043e22007-01-19 07:51:42 +0000745
746 Offset = N;
Evan Chenga20cde32011-07-20 23:34:39 +0000747 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOpcode());
Evan Cheng10043e22007-01-19 07:51:42 +0000748 unsigned ShAmt = 0;
749 if (ShOpcVal != ARM_AM::no_shift) {
750 // Check to see if the RHS of the shift is a constant, if not, we can't fold
751 // it.
752 if (ConstantSDNode *Sh = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +0000753 ShAmt = Sh->getZExtValue();
Evan Cheng59bbc542010-10-27 23:41:30 +0000754 if (isShifterOpProfitable(N, ShOpcVal, ShAmt))
755 Offset = N.getOperand(0);
756 else {
757 ShAmt = 0;
758 ShOpcVal = ARM_AM::no_shift;
759 }
Evan Cheng10043e22007-01-19 07:51:42 +0000760 } else {
761 ShOpcVal = ARM_AM::no_shift;
762 }
763 }
764
765 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000766 SDLoc(N), MVT::i32);
Rafael Espindola19398ec2006-10-17 18:04:53 +0000767 return true;
768}
769
Owen Anderson4d5c8f82011-08-29 20:16:50 +0000770bool ARMDAGToDAGISel::SelectAddrMode2OffsetImmPre(SDNode *Op, SDValue N,
771 SDValue &Offset, SDValue &Opc) {
Owen Anderson939cd212011-08-31 20:00:11 +0000772 unsigned Opcode = Op->getOpcode();
773 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
774 ? cast<LoadSDNode>(Op)->getAddressingMode()
775 : cast<StoreSDNode>(Op)->getAddressingMode();
776 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
777 ? ARM_AM::add : ARM_AM::sub;
Owen Anderson4d5c8f82011-08-29 20:16:50 +0000778 int Val;
779 if (isScaledConstantInRange(N, /*Scale=*/1, 0, 0x1000, Val)) { // 12 bits.
Owen Anderson939cd212011-08-31 20:00:11 +0000780 if (AddSub == ARM_AM::sub) Val *= -1;
Owen Anderson4d5c8f82011-08-29 20:16:50 +0000781 Offset = CurDAG->getRegister(0, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000782 Opc = CurDAG->getTargetConstant(Val, SDLoc(Op), MVT::i32);
Owen Anderson4d5c8f82011-08-29 20:16:50 +0000783 return true;
784 }
785
786 return false;
787}
788
789
Owen Anderson2aedba62011-07-26 20:54:26 +0000790bool ARMDAGToDAGISel::SelectAddrMode2OffsetImm(SDNode *Op, SDValue N,
791 SDValue &Offset, SDValue &Opc) {
792 unsigned Opcode = Op->getOpcode();
793 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
794 ? cast<LoadSDNode>(Op)->getAddressingMode()
795 : cast<StoreSDNode>(Op)->getAddressingMode();
796 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
797 ? ARM_AM::add : ARM_AM::sub;
798 int Val;
799 if (isScaledConstantInRange(N, /*Scale=*/1, 0, 0x1000, Val)) { // 12 bits.
800 Offset = CurDAG->getRegister(0, MVT::i32);
801 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, Val,
802 ARM_AM::no_shift),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000803 SDLoc(Op), MVT::i32);
Owen Anderson2aedba62011-07-26 20:54:26 +0000804 return true;
805 }
806
807 return false;
808}
809
Jim Grosbachf0c95ca2011-08-05 20:35:44 +0000810bool ARMDAGToDAGISel::SelectAddrOffsetNone(SDValue N, SDValue &Base) {
811 Base = N;
812 return true;
813}
Evan Cheng10043e22007-01-19 07:51:42 +0000814
Chris Lattner0e023ea2010-09-21 20:31:19 +0000815bool ARMDAGToDAGISel::SelectAddrMode3(SDValue N,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000816 SDValue &Base, SDValue &Offset,
817 SDValue &Opc) {
Evan Cheng10043e22007-01-19 07:51:42 +0000818 if (N.getOpcode() == ISD::SUB) {
819 // X - C is canonicalize to X + -C, no need to handle it here.
820 Base = N.getOperand(0);
821 Offset = N.getOperand(1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000822 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::sub, 0), SDLoc(N),
823 MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +0000824 return true;
825 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000826
Chris Lattner46c01a32011-02-13 22:25:43 +0000827 if (!CurDAG->isBaseWithConstantOffset(N)) {
Evan Cheng10043e22007-01-19 07:51:42 +0000828 Base = N;
829 if (N.getOpcode() == ISD::FrameIndex) {
830 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Mehdi Amini44ede332015-07-09 02:09:04 +0000831 Base = CurDAG->getTargetFrameIndex(
832 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
Evan Cheng10043e22007-01-19 07:51:42 +0000833 }
Owen Anderson9f944592009-08-11 20:47:22 +0000834 Offset = CurDAG->getRegister(0, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000835 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0), SDLoc(N),
836 MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +0000837 return true;
838 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000839
Evan Cheng10043e22007-01-19 07:51:42 +0000840 // If the RHS is +/- imm8, fold into addr mode.
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +0000841 int RHSC;
842 if (isScaledConstantInRange(N.getOperand(1), /*Scale=*/1,
843 -256 + 1, 256, RHSC)) { // 8 bits.
844 Base = N.getOperand(0);
845 if (Base.getOpcode() == ISD::FrameIndex) {
846 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
Mehdi Amini44ede332015-07-09 02:09:04 +0000847 Base = CurDAG->getTargetFrameIndex(
848 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
Evan Cheng10043e22007-01-19 07:51:42 +0000849 }
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +0000850 Offset = CurDAG->getRegister(0, MVT::i32);
851
852 ARM_AM::AddrOpc AddSub = ARM_AM::add;
853 if (RHSC < 0) {
854 AddSub = ARM_AM::sub;
Chris Lattner46c01a32011-02-13 22:25:43 +0000855 RHSC = -RHSC;
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +0000856 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000857 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, RHSC), SDLoc(N),
858 MVT::i32);
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +0000859 return true;
Evan Cheng10043e22007-01-19 07:51:42 +0000860 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000861
Evan Cheng10043e22007-01-19 07:51:42 +0000862 Base = N.getOperand(0);
863 Offset = N.getOperand(1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000864 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0), SDLoc(N),
865 MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +0000866 return true;
867}
868
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000869bool ARMDAGToDAGISel::SelectAddrMode3Offset(SDNode *Op, SDValue N,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000870 SDValue &Offset, SDValue &Opc) {
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000871 unsigned Opcode = Op->getOpcode();
Evan Cheng10043e22007-01-19 07:51:42 +0000872 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
873 ? cast<LoadSDNode>(Op)->getAddressingMode()
874 : cast<StoreSDNode>(Op)->getAddressingMode();
875 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
876 ? ARM_AM::add : ARM_AM::sub;
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +0000877 int Val;
878 if (isScaledConstantInRange(N, /*Scale=*/1, 0, 256, Val)) { // 12 bits.
879 Offset = CurDAG->getRegister(0, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000880 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, Val), SDLoc(Op),
881 MVT::i32);
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +0000882 return true;
Evan Cheng10043e22007-01-19 07:51:42 +0000883 }
884
885 Offset = N;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000886 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, 0), SDLoc(Op),
887 MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +0000888 return true;
889}
890
Sjoerd Meijer011de9c2018-01-26 09:26:40 +0000891bool ARMDAGToDAGISel::IsAddressingMode5(SDValue N, SDValue &Base, SDValue &Offset,
892 int Lwb, int Upb, bool FP16) {
Chris Lattner46c01a32011-02-13 22:25:43 +0000893 if (!CurDAG->isBaseWithConstantOffset(N)) {
Evan Cheng10043e22007-01-19 07:51:42 +0000894 Base = N;
895 if (N.getOpcode() == ISD::FrameIndex) {
896 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Mehdi Amini44ede332015-07-09 02:09:04 +0000897 Base = CurDAG->getTargetFrameIndex(
898 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
Anton Korobeynikov25229082009-11-24 00:44:37 +0000899 } else if (N.getOpcode() == ARMISD::Wrapper &&
Tim Northoverbd41cf82016-01-07 09:03:03 +0000900 N.getOperand(0).getOpcode() != ISD::TargetGlobalAddress &&
Saleem Abdulrasoolf36005a2016-02-03 18:21:59 +0000901 N.getOperand(0).getOpcode() != ISD::TargetExternalSymbol &&
Tim Northoverbd41cf82016-01-07 09:03:03 +0000902 N.getOperand(0).getOpcode() != ISD::TargetGlobalTLSAddress) {
Evan Cheng10043e22007-01-19 07:51:42 +0000903 Base = N.getOperand(0);
904 }
905 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000906 SDLoc(N), MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +0000907 return true;
908 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000909
Evan Cheng10043e22007-01-19 07:51:42 +0000910 // If the RHS is +/- imm8, fold into addr mode.
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +0000911 int RHSC;
Sjoerd Meijer011de9c2018-01-26 09:26:40 +0000912 const int Scale = FP16 ? 2 : 4;
913
914 if (isScaledConstantInRange(N.getOperand(1), Scale, Lwb, Upb, RHSC)) {
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +0000915 Base = N.getOperand(0);
916 if (Base.getOpcode() == ISD::FrameIndex) {
917 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
Mehdi Amini44ede332015-07-09 02:09:04 +0000918 Base = CurDAG->getTargetFrameIndex(
919 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
Evan Cheng10043e22007-01-19 07:51:42 +0000920 }
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +0000921
922 ARM_AM::AddrOpc AddSub = ARM_AM::add;
923 if (RHSC < 0) {
924 AddSub = ARM_AM::sub;
Chris Lattner46c01a32011-02-13 22:25:43 +0000925 RHSC = -RHSC;
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +0000926 }
Sjoerd Meijer011de9c2018-01-26 09:26:40 +0000927
928 if (FP16)
929 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5FP16Opc(AddSub, RHSC),
930 SDLoc(N), MVT::i32);
931 else
932 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(AddSub, RHSC),
933 SDLoc(N), MVT::i32);
934
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +0000935 return true;
Evan Cheng10043e22007-01-19 07:51:42 +0000936 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000937
Evan Cheng10043e22007-01-19 07:51:42 +0000938 Base = N;
Sjoerd Meijer011de9c2018-01-26 09:26:40 +0000939
940 if (FP16)
941 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5FP16Opc(ARM_AM::add, 0),
942 SDLoc(N), MVT::i32);
943 else
944 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0),
945 SDLoc(N), MVT::i32);
946
Evan Cheng10043e22007-01-19 07:51:42 +0000947 return true;
948}
949
Sjoerd Meijer011de9c2018-01-26 09:26:40 +0000950bool ARMDAGToDAGISel::SelectAddrMode5(SDValue N,
951 SDValue &Base, SDValue &Offset) {
952 int Lwb = -256 + 1;
953 int Upb = 256;
954 return IsAddressingMode5(N, Base, Offset, Lwb, Upb, /*FP16=*/ false);
955}
956
957bool ARMDAGToDAGISel::SelectAddrMode5FP16(SDValue N,
958 SDValue &Base, SDValue &Offset) {
959 int Lwb = -512 + 1;
960 int Upb = 512;
961 return IsAddressingMode5(N, Base, Offset, Lwb, Upb, /*FP16=*/ true);
962}
963
Bob Wilsondd9fbaa2010-11-01 23:40:51 +0000964bool ARMDAGToDAGISel::SelectAddrMode6(SDNode *Parent, SDValue N, SDValue &Addr,
965 SDValue &Align) {
Bob Wilsondeb35af2009-07-01 23:16:05 +0000966 Addr = N;
Bob Wilsondd9fbaa2010-11-01 23:40:51 +0000967
968 unsigned Alignment = 0;
Ahmed Bougachadb141ac2015-02-19 23:52:41 +0000969
970 MemSDNode *MemN = cast<MemSDNode>(Parent);
971
972 if (isa<LSBaseSDNode>(MemN) ||
973 ((MemN->getOpcode() == ARMISD::VST1_UPD ||
974 MemN->getOpcode() == ARMISD::VLD1_UPD) &&
975 MemN->getConstantOperandVal(MemN->getNumOperands() - 1) == 1)) {
Bob Wilsondd9fbaa2010-11-01 23:40:51 +0000976 // This case occurs only for VLD1-lane/dup and VST1-lane instructions.
977 // The maximum alignment is equal to the memory size being referenced.
Ahmed Bougachadb141ac2015-02-19 23:52:41 +0000978 unsigned MMOAlign = MemN->getAlignment();
979 unsigned MemSize = MemN->getMemoryVT().getSizeInBits() / 8;
980 if (MMOAlign >= MemSize && MemSize > 1)
Bob Wilsondd9fbaa2010-11-01 23:40:51 +0000981 Alignment = MemSize;
982 } else {
983 // All other uses of addrmode6 are for intrinsics. For now just record
984 // the raw alignment value; it will be refined later based on the legal
985 // alignment operands for the intrinsic.
Ahmed Bougachadb141ac2015-02-19 23:52:41 +0000986 Alignment = MemN->getAlignment();
Bob Wilsondd9fbaa2010-11-01 23:40:51 +0000987 }
988
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000989 Align = CurDAG->getTargetConstant(Alignment, SDLoc(N), MVT::i32);
Bob Wilsondeb35af2009-07-01 23:16:05 +0000990 return true;
991}
992
Bob Wilsone3ecd5f2011-02-25 06:42:42 +0000993bool ARMDAGToDAGISel::SelectAddrMode6Offset(SDNode *Op, SDValue N,
994 SDValue &Offset) {
995 LSBaseSDNode *LdSt = cast<LSBaseSDNode>(Op);
996 ISD::MemIndexedMode AM = LdSt->getAddressingMode();
997 if (AM != ISD::POST_INC)
998 return false;
999 Offset = N;
1000 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(N)) {
1001 if (NC->getZExtValue() * 8 == LdSt->getMemoryVT().getSizeInBits())
1002 Offset = CurDAG->getRegister(0, MVT::i32);
1003 }
1004 return true;
1005}
1006
Chris Lattner0e023ea2010-09-21 20:31:19 +00001007bool ARMDAGToDAGISel::SelectAddrModePC(SDValue N,
Evan Cheng9a58aff2009-08-14 19:01:37 +00001008 SDValue &Offset, SDValue &Label) {
Evan Cheng10043e22007-01-19 07:51:42 +00001009 if (N.getOpcode() == ARMISD::PIC_ADD && N.hasOneUse()) {
1010 Offset = N.getOperand(0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001011 SDValue N1 = N.getOperand(1);
Evan Chengb8b0ad82011-01-20 08:34:58 +00001012 Label = CurDAG->getTargetConstant(cast<ConstantSDNode>(N1)->getZExtValue(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001013 SDLoc(N), MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00001014 return true;
1015 }
Bill Wendling092a7bd2010-12-14 03:36:38 +00001016
Evan Cheng10043e22007-01-19 07:51:42 +00001017 return false;
1018}
1019
Bill Wendling092a7bd2010-12-14 03:36:38 +00001020
1021//===----------------------------------------------------------------------===//
1022// Thumb Addressing Modes
1023//===----------------------------------------------------------------------===//
1024
Chris Lattner0e023ea2010-09-21 20:31:19 +00001025bool ARMDAGToDAGISel::SelectThumbAddrModeRR(SDValue N,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001026 SDValue &Base, SDValue &Offset){
Chris Lattner46c01a32011-02-13 22:25:43 +00001027 if (N.getOpcode() != ISD::ADD && !CurDAG->isBaseWithConstantOffset(N)) {
Evan Cheng0794c6a2009-07-11 07:08:13 +00001028 ConstantSDNode *NC = dyn_cast<ConstantSDNode>(N);
Dan Gohmanf1d83042010-06-18 14:22:04 +00001029 if (!NC || !NC->isNullValue())
Evan Cheng0794c6a2009-07-11 07:08:13 +00001030 return false;
1031
1032 Base = Offset = N;
Evan Chengc0b73662007-01-23 22:59:13 +00001033 return true;
1034 }
1035
Evan Cheng10043e22007-01-19 07:51:42 +00001036 Base = N.getOperand(0);
1037 Offset = N.getOperand(1);
1038 return true;
1039}
1040
Evan Cheng139edae2007-01-24 02:21:22 +00001041bool
Bill Wendling092a7bd2010-12-14 03:36:38 +00001042ARMDAGToDAGISel::SelectThumbAddrModeImm5S(SDValue N, unsigned Scale,
1043 SDValue &Base, SDValue &OffImm) {
Chris Lattner46c01a32011-02-13 22:25:43 +00001044 if (!CurDAG->isBaseWithConstantOffset(N)) {
John Brawn68acdcb2015-08-13 10:48:22 +00001045 if (N.getOpcode() == ISD::ADD) {
1046 return false; // We want to select register offset instead
1047 } else if (N.getOpcode() == ARMISD::Wrapper &&
Tim Northoverbd41cf82016-01-07 09:03:03 +00001048 N.getOperand(0).getOpcode() != ISD::TargetGlobalAddress &&
Saleem Abdulrasoolf36005a2016-02-03 18:21:59 +00001049 N.getOperand(0).getOpcode() != ISD::TargetExternalSymbol &&
James Molloyb7de4972016-10-05 14:52:13 +00001050 N.getOperand(0).getOpcode() != ISD::TargetConstantPool &&
Tim Northoverbd41cf82016-01-07 09:03:03 +00001051 N.getOperand(0).getOpcode() != ISD::TargetGlobalTLSAddress) {
Bill Wendling092a7bd2010-12-14 03:36:38 +00001052 Base = N.getOperand(0);
1053 } else {
1054 Base = N;
1055 }
1056
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001057 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32);
Evan Cheng650d0672007-02-06 00:22:06 +00001058 return true;
1059 }
1060
Evan Cheng10043e22007-01-19 07:51:42 +00001061 // If the RHS is + imm5 * scale, fold into addr mode.
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +00001062 int RHSC;
1063 if (isScaledConstantInRange(N.getOperand(1), Scale, 0, 32, RHSC)) {
1064 Base = N.getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001065 OffImm = CurDAG->getTargetConstant(RHSC, SDLoc(N), MVT::i32);
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +00001066 return true;
Evan Cheng10043e22007-01-19 07:51:42 +00001067 }
1068
John Brawn68acdcb2015-08-13 10:48:22 +00001069 // Offset is too large, so use register offset instead.
1070 return false;
Evan Cheng10043e22007-01-19 07:51:42 +00001071}
1072
Bill Wendling092a7bd2010-12-14 03:36:38 +00001073bool
1074ARMDAGToDAGISel::SelectThumbAddrModeImm5S4(SDValue N, SDValue &Base,
1075 SDValue &OffImm) {
1076 return SelectThumbAddrModeImm5S(N, 4, Base, OffImm);
Evan Cheng10043e22007-01-19 07:51:42 +00001077}
1078
Bill Wendling092a7bd2010-12-14 03:36:38 +00001079bool
1080ARMDAGToDAGISel::SelectThumbAddrModeImm5S2(SDValue N, SDValue &Base,
1081 SDValue &OffImm) {
1082 return SelectThumbAddrModeImm5S(N, 2, Base, OffImm);
Evan Cheng10043e22007-01-19 07:51:42 +00001083}
1084
Bill Wendling092a7bd2010-12-14 03:36:38 +00001085bool
1086ARMDAGToDAGISel::SelectThumbAddrModeImm5S1(SDValue N, SDValue &Base,
1087 SDValue &OffImm) {
1088 return SelectThumbAddrModeImm5S(N, 1, Base, OffImm);
Evan Cheng10043e22007-01-19 07:51:42 +00001089}
1090
Chris Lattner0e023ea2010-09-21 20:31:19 +00001091bool ARMDAGToDAGISel::SelectThumbAddrModeSP(SDValue N,
1092 SDValue &Base, SDValue &OffImm) {
Evan Cheng10043e22007-01-19 07:51:42 +00001093 if (N.getOpcode() == ISD::FrameIndex) {
1094 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Renato Golinb9887ef2015-02-25 14:41:06 +00001095 // Only multiples of 4 are allowed for the offset, so the frame object
1096 // alignment must be at least 4.
Matthias Braun941a7052016-07-28 18:40:00 +00001097 MachineFrameInfo &MFI = MF->getFrameInfo();
1098 if (MFI.getObjectAlignment(FI) < 4)
1099 MFI.setObjectAlignment(FI, 4);
Mehdi Amini44ede332015-07-09 02:09:04 +00001100 Base = CurDAG->getTargetFrameIndex(
1101 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001102 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00001103 return true;
1104 }
Evan Cheng139edae2007-01-24 02:21:22 +00001105
Chris Lattner46c01a32011-02-13 22:25:43 +00001106 if (!CurDAG->isBaseWithConstantOffset(N))
Evan Cheng650d0672007-02-06 00:22:06 +00001107 return false;
1108
1109 RegisterSDNode *LHSR = dyn_cast<RegisterSDNode>(N.getOperand(0));
Evan Chenga9740312007-02-06 09:11:20 +00001110 if (N.getOperand(0).getOpcode() == ISD::FrameIndex ||
1111 (LHSR && LHSR->getReg() == ARM::SP)) {
Evan Cheng139edae2007-01-24 02:21:22 +00001112 // If the RHS is + imm8 * scale, fold into addr mode.
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +00001113 int RHSC;
1114 if (isScaledConstantInRange(N.getOperand(1), /*Scale=*/4, 0, 256, RHSC)) {
1115 Base = N.getOperand(0);
1116 if (Base.getOpcode() == ISD::FrameIndex) {
1117 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
Renato Golinb9887ef2015-02-25 14:41:06 +00001118 // For LHS+RHS to result in an offset that's a multiple of 4 the object
1119 // indexed by the LHS must be 4-byte aligned.
Matthias Braun941a7052016-07-28 18:40:00 +00001120 MachineFrameInfo &MFI = MF->getFrameInfo();
1121 if (MFI.getObjectAlignment(FI) < 4)
1122 MFI.setObjectAlignment(FI, 4);
Mehdi Amini44ede332015-07-09 02:09:04 +00001123 Base = CurDAG->getTargetFrameIndex(
1124 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
Evan Cheng139edae2007-01-24 02:21:22 +00001125 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001126 OffImm = CurDAG->getTargetConstant(RHSC, SDLoc(N), MVT::i32);
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +00001127 return true;
Evan Cheng139edae2007-01-24 02:21:22 +00001128 }
1129 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +00001130
Evan Cheng10043e22007-01-19 07:51:42 +00001131 return false;
1132}
1133
Bill Wendling092a7bd2010-12-14 03:36:38 +00001134
1135//===----------------------------------------------------------------------===//
1136// Thumb 2 Addressing Modes
1137//===----------------------------------------------------------------------===//
1138
1139
Chris Lattner0e023ea2010-09-21 20:31:19 +00001140bool ARMDAGToDAGISel::SelectT2AddrModeImm12(SDValue N,
Evan Chengb23b50d2009-06-29 07:51:04 +00001141 SDValue &Base, SDValue &OffImm) {
1142 // Match simple R + imm12 operands.
David Goodwin802a0b52009-07-20 15:55:39 +00001143
Evan Cheng36064672009-08-11 08:52:18 +00001144 // Base only.
Chris Lattner46c01a32011-02-13 22:25:43 +00001145 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB &&
1146 !CurDAG->isBaseWithConstantOffset(N)) {
David Goodwin802a0b52009-07-20 15:55:39 +00001147 if (N.getOpcode() == ISD::FrameIndex) {
Chris Lattner46c01a32011-02-13 22:25:43 +00001148 // Match frame index.
David Goodwin802a0b52009-07-20 15:55:39 +00001149 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Mehdi Amini44ede332015-07-09 02:09:04 +00001150 Base = CurDAG->getTargetFrameIndex(
1151 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001152 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32);
David Goodwin802a0b52009-07-20 15:55:39 +00001153 return true;
Chris Lattner46c01a32011-02-13 22:25:43 +00001154 }
Owen Anderson6d557452011-03-18 19:46:58 +00001155
Chris Lattner46c01a32011-02-13 22:25:43 +00001156 if (N.getOpcode() == ARMISD::Wrapper &&
Tim Northoverbd41cf82016-01-07 09:03:03 +00001157 N.getOperand(0).getOpcode() != ISD::TargetGlobalAddress &&
Saleem Abdulrasoolf36005a2016-02-03 18:21:59 +00001158 N.getOperand(0).getOpcode() != ISD::TargetExternalSymbol &&
Tim Northoverbd41cf82016-01-07 09:03:03 +00001159 N.getOperand(0).getOpcode() != ISD::TargetGlobalTLSAddress) {
Evan Cheng36064672009-08-11 08:52:18 +00001160 Base = N.getOperand(0);
1161 if (Base.getOpcode() == ISD::TargetConstantPool)
1162 return false; // We want to select t2LDRpci instead.
1163 } else
1164 Base = N;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001165 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32);
Evan Cheng36064672009-08-11 08:52:18 +00001166 return true;
David Goodwin802a0b52009-07-20 15:55:39 +00001167 }
Evan Chengb23b50d2009-06-29 07:51:04 +00001168
1169 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner0e023ea2010-09-21 20:31:19 +00001170 if (SelectT2AddrModeImm8(N, Base, OffImm))
Evan Cheng36064672009-08-11 08:52:18 +00001171 // Let t2LDRi8 handle (R - imm8).
1172 return false;
1173
Evan Chengb23b50d2009-06-29 07:51:04 +00001174 int RHSC = (int)RHS->getZExtValue();
David Goodwin79c079b2009-07-30 18:56:48 +00001175 if (N.getOpcode() == ISD::SUB)
1176 RHSC = -RHSC;
1177
1178 if (RHSC >= 0 && RHSC < 0x1000) { // 12 bits (unsigned)
Evan Chengb23b50d2009-06-29 07:51:04 +00001179 Base = N.getOperand(0);
David Goodwin79c079b2009-07-30 18:56:48 +00001180 if (Base.getOpcode() == ISD::FrameIndex) {
1181 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
Mehdi Amini44ede332015-07-09 02:09:04 +00001182 Base = CurDAG->getTargetFrameIndex(
1183 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
David Goodwin79c079b2009-07-30 18:56:48 +00001184 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001185 OffImm = CurDAG->getTargetConstant(RHSC, SDLoc(N), MVT::i32);
Evan Chengb23b50d2009-06-29 07:51:04 +00001186 return true;
1187 }
1188 }
1189
Evan Cheng36064672009-08-11 08:52:18 +00001190 // Base only.
1191 Base = N;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001192 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32);
Evan Cheng36064672009-08-11 08:52:18 +00001193 return true;
Evan Chengb23b50d2009-06-29 07:51:04 +00001194}
1195
Chris Lattner0e023ea2010-09-21 20:31:19 +00001196bool ARMDAGToDAGISel::SelectT2AddrModeImm8(SDValue N,
Evan Chengb23b50d2009-06-29 07:51:04 +00001197 SDValue &Base, SDValue &OffImm) {
David Goodwin79c079b2009-07-30 18:56:48 +00001198 // Match simple R - imm8 operands.
Chris Lattner46c01a32011-02-13 22:25:43 +00001199 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB &&
1200 !CurDAG->isBaseWithConstantOffset(N))
1201 return false;
Owen Anderson6d557452011-03-18 19:46:58 +00001202
Chris Lattner46c01a32011-02-13 22:25:43 +00001203 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1204 int RHSC = (int)RHS->getSExtValue();
1205 if (N.getOpcode() == ISD::SUB)
1206 RHSC = -RHSC;
Jim Grosbachf24f9d92009-08-11 15:33:49 +00001207
Chris Lattner46c01a32011-02-13 22:25:43 +00001208 if ((RHSC >= -255) && (RHSC < 0)) { // 8 bits (always negative)
1209 Base = N.getOperand(0);
1210 if (Base.getOpcode() == ISD::FrameIndex) {
1211 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
Mehdi Amini44ede332015-07-09 02:09:04 +00001212 Base = CurDAG->getTargetFrameIndex(
1213 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
Evan Chengb23b50d2009-06-29 07:51:04 +00001214 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001215 OffImm = CurDAG->getTargetConstant(RHSC, SDLoc(N), MVT::i32);
Chris Lattner46c01a32011-02-13 22:25:43 +00001216 return true;
Evan Chengb23b50d2009-06-29 07:51:04 +00001217 }
1218 }
1219
1220 return false;
1221}
1222
Dan Gohmanea6f91f2010-01-05 01:24:18 +00001223bool ARMDAGToDAGISel::SelectT2AddrModeImm8Offset(SDNode *Op, SDValue N,
Evan Cheng84c6cda2009-07-02 07:28:31 +00001224 SDValue &OffImm){
Dan Gohmanea6f91f2010-01-05 01:24:18 +00001225 unsigned Opcode = Op->getOpcode();
Evan Cheng84c6cda2009-07-02 07:28:31 +00001226 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
1227 ? cast<LoadSDNode>(Op)->getAddressingMode()
1228 : cast<StoreSDNode>(Op)->getAddressingMode();
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +00001229 int RHSC;
1230 if (isScaledConstantInRange(N, /*Scale=*/1, 0, 0x100, RHSC)) { // 8 bits.
1231 OffImm = ((AM == ISD::PRE_INC) || (AM == ISD::POST_INC))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001232 ? CurDAG->getTargetConstant(RHSC, SDLoc(N), MVT::i32)
1233 : CurDAG->getTargetConstant(-RHSC, SDLoc(N), MVT::i32);
Daniel Dunbare0cd9ac2011-01-19 15:12:16 +00001234 return true;
Evan Cheng84c6cda2009-07-02 07:28:31 +00001235 }
1236
1237 return false;
1238}
1239
Chris Lattner0e023ea2010-09-21 20:31:19 +00001240bool ARMDAGToDAGISel::SelectT2AddrModeSoReg(SDValue N,
Evan Chengb23b50d2009-06-29 07:51:04 +00001241 SDValue &Base,
1242 SDValue &OffReg, SDValue &ShImm) {
Evan Cheng36064672009-08-11 08:52:18 +00001243 // (R - imm8) should be handled by t2LDRi8. The rest are handled by t2LDRi12.
Chris Lattner46c01a32011-02-13 22:25:43 +00001244 if (N.getOpcode() != ISD::ADD && !CurDAG->isBaseWithConstantOffset(N))
Evan Cheng36064672009-08-11 08:52:18 +00001245 return false;
Evan Chengb23b50d2009-06-29 07:51:04 +00001246
Evan Cheng36064672009-08-11 08:52:18 +00001247 // Leave (R + imm12) for t2LDRi12, (R - imm8) for t2LDRi8.
1248 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1249 int RHSC = (int)RHS->getZExtValue();
1250 if (RHSC >= 0 && RHSC < 0x1000) // 12 bits (unsigned)
1251 return false;
1252 else if (RHSC < 0 && RHSC >= -255) // 8 bits
David Goodwin79c079b2009-07-30 18:56:48 +00001253 return false;
1254 }
1255
Evan Chengb23b50d2009-06-29 07:51:04 +00001256 // Look for (R + R) or (R + (R << [1,2,3])).
1257 unsigned ShAmt = 0;
1258 Base = N.getOperand(0);
1259 OffReg = N.getOperand(1);
1260
1261 // Swap if it is ((R << c) + R).
Evan Chenga20cde32011-07-20 23:34:39 +00001262 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(OffReg.getOpcode());
Evan Chengb23b50d2009-06-29 07:51:04 +00001263 if (ShOpcVal != ARM_AM::lsl) {
Evan Chenga20cde32011-07-20 23:34:39 +00001264 ShOpcVal = ARM_AM::getShiftOpcForNode(Base.getOpcode());
Evan Chengb23b50d2009-06-29 07:51:04 +00001265 if (ShOpcVal == ARM_AM::lsl)
1266 std::swap(Base, OffReg);
Jim Grosbachf24f9d92009-08-11 15:33:49 +00001267 }
1268
Evan Chengb23b50d2009-06-29 07:51:04 +00001269 if (ShOpcVal == ARM_AM::lsl) {
1270 // Check to see if the RHS of the shift is a constant, if not, we can't fold
1271 // it.
1272 if (ConstantSDNode *Sh = dyn_cast<ConstantSDNode>(OffReg.getOperand(1))) {
1273 ShAmt = Sh->getZExtValue();
Evan Cheng59bbc542010-10-27 23:41:30 +00001274 if (ShAmt < 4 && isShifterOpProfitable(OffReg, ShOpcVal, ShAmt))
1275 OffReg = OffReg.getOperand(0);
1276 else {
Evan Chengb23b50d2009-06-29 07:51:04 +00001277 ShAmt = 0;
Evan Cheng59bbc542010-10-27 23:41:30 +00001278 }
Evan Chengb23b50d2009-06-29 07:51:04 +00001279 }
David Goodwinf3912052009-07-15 15:50:19 +00001280 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +00001281
John Brawn056e6782015-09-14 15:19:41 +00001282 // If OffReg is a multiply-by-constant and it's profitable to extract a shift
1283 // and use it in a shifted operand do so.
Tim Northoverc4093c32016-01-29 19:18:46 +00001284 if (OffReg.getOpcode() == ISD::MUL && N.hasOneUse()) {
John Brawn056e6782015-09-14 15:19:41 +00001285 unsigned PowerOfTwo = 0;
1286 SDValue NewMulConst;
1287 if (canExtractShiftFromMul(OffReg, 3, PowerOfTwo, NewMulConst)) {
Tim Northover4a01ffb2017-05-02 22:45:19 +00001288 HandleSDNode Handle(OffReg);
John Brawn056e6782015-09-14 15:19:41 +00001289 replaceDAGValue(OffReg.getOperand(1), NewMulConst);
Tim Northover4a01ffb2017-05-02 22:45:19 +00001290 OffReg = Handle.getValue();
John Brawn056e6782015-09-14 15:19:41 +00001291 ShAmt = PowerOfTwo;
1292 }
1293 }
1294
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001295 ShImm = CurDAG->getTargetConstant(ShAmt, SDLoc(N), MVT::i32);
Evan Chengb23b50d2009-06-29 07:51:04 +00001296
1297 return true;
1298}
1299
Tim Northovera7ecd242013-07-16 09:46:55 +00001300bool ARMDAGToDAGISel::SelectT2AddrModeExclusive(SDValue N, SDValue &Base,
1301 SDValue &OffImm) {
Alp Tokercb402912014-01-24 17:20:08 +00001302 // This *must* succeed since it's used for the irreplaceable ldrex and strex
Tim Northovera7ecd242013-07-16 09:46:55 +00001303 // instructions.
1304 Base = N;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001305 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32);
Tim Northovera7ecd242013-07-16 09:46:55 +00001306
1307 if (N.getOpcode() != ISD::ADD || !CurDAG->isBaseWithConstantOffset(N))
1308 return true;
1309
1310 ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1));
1311 if (!RHS)
1312 return true;
1313
1314 uint32_t RHSC = (int)RHS->getZExtValue();
1315 if (RHSC > 1020 || RHSC % 4 != 0)
1316 return true;
1317
1318 Base = N.getOperand(0);
1319 if (Base.getOpcode() == ISD::FrameIndex) {
1320 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
Mehdi Amini44ede332015-07-09 02:09:04 +00001321 Base = CurDAG->getTargetFrameIndex(
1322 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
Tim Northovera7ecd242013-07-16 09:46:55 +00001323 }
1324
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001325 OffImm = CurDAG->getTargetConstant(RHSC/4, SDLoc(N), MVT::i32);
Tim Northovera7ecd242013-07-16 09:46:55 +00001326 return true;
1327}
1328
Evan Chengb23b50d2009-06-29 07:51:04 +00001329//===--------------------------------------------------------------------===//
1330
Evan Cheng7e90b112007-07-05 07:15:27 +00001331/// getAL - Returns a ARMCC::AL immediate node.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001332static inline SDValue getAL(SelectionDAG *CurDAG, const SDLoc &dl) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001333 return CurDAG->getTargetConstant((uint64_t)ARMCC::AL, dl, MVT::i32);
Evan Cheng0f7cbe82007-05-15 01:29:07 +00001334}
1335
Tim Northovereaee28b2016-09-19 09:11:09 +00001336void ARMDAGToDAGISel::transferMemOperands(SDNode *N, SDNode *Result) {
1337 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1338 MemOp[0] = cast<MemSDNode>(N)->getMemOperand();
1339 cast<MachineSDNode>(Result)->setMemRefs(MemOp, MemOp + 1);
1340}
1341
Justin Bogner45571362016-05-12 00:31:09 +00001342bool ARMDAGToDAGISel::tryARMIndexedLoad(SDNode *N) {
Dan Gohmanea6f91f2010-01-05 01:24:18 +00001343 LoadSDNode *LD = cast<LoadSDNode>(N);
Evan Chengd9c55362009-07-02 01:23:32 +00001344 ISD::MemIndexedMode AM = LD->getAddressingMode();
1345 if (AM == ISD::UNINDEXED)
Justin Bogner45571362016-05-12 00:31:09 +00001346 return false;
Evan Chengd9c55362009-07-02 01:23:32 +00001347
Owen Anderson53aa7a92009-08-10 22:56:29 +00001348 EVT LoadedVT = LD->getMemoryVT();
Evan Chengd9c55362009-07-02 01:23:32 +00001349 SDValue Offset, AMOpc;
1350 bool isPre = (AM == ISD::PRE_INC) || (AM == ISD::PRE_DEC);
1351 unsigned Opcode = 0;
1352 bool Match = false;
Owen Anderson4d5c8f82011-08-29 20:16:50 +00001353 if (LoadedVT == MVT::i32 && isPre &&
1354 SelectAddrMode2OffsetImmPre(N, LD->getOffset(), Offset, AMOpc)) {
1355 Opcode = ARM::LDR_PRE_IMM;
1356 Match = true;
1357 } else if (LoadedVT == MVT::i32 && !isPre &&
Owen Anderson2aedba62011-07-26 20:54:26 +00001358 SelectAddrMode2OffsetImm(N, LD->getOffset(), Offset, AMOpc)) {
Owen Anderson4d5c8f82011-08-29 20:16:50 +00001359 Opcode = ARM::LDR_POST_IMM;
Evan Chengd9c55362009-07-02 01:23:32 +00001360 Match = true;
Owen Anderson2aedba62011-07-26 20:54:26 +00001361 } else if (LoadedVT == MVT::i32 &&
1362 SelectAddrMode2OffsetReg(N, LD->getOffset(), Offset, AMOpc)) {
Owen Anderson16d33f32011-08-26 20:43:14 +00001363 Opcode = isPre ? ARM::LDR_PRE_REG : ARM::LDR_POST_REG;
Owen Anderson2aedba62011-07-26 20:54:26 +00001364 Match = true;
1365
Owen Anderson9f944592009-08-11 20:47:22 +00001366 } else if (LoadedVT == MVT::i16 &&
Dan Gohmanea6f91f2010-01-05 01:24:18 +00001367 SelectAddrMode3Offset(N, LD->getOffset(), Offset, AMOpc)) {
Evan Chengd9c55362009-07-02 01:23:32 +00001368 Match = true;
1369 Opcode = (LD->getExtensionType() == ISD::SEXTLOAD)
1370 ? (isPre ? ARM::LDRSH_PRE : ARM::LDRSH_POST)
1371 : (isPre ? ARM::LDRH_PRE : ARM::LDRH_POST);
Owen Anderson9f944592009-08-11 20:47:22 +00001372 } else if (LoadedVT == MVT::i8 || LoadedVT == MVT::i1) {
Evan Chengd9c55362009-07-02 01:23:32 +00001373 if (LD->getExtensionType() == ISD::SEXTLOAD) {
Dan Gohmanea6f91f2010-01-05 01:24:18 +00001374 if (SelectAddrMode3Offset(N, LD->getOffset(), Offset, AMOpc)) {
Evan Chengd9c55362009-07-02 01:23:32 +00001375 Match = true;
1376 Opcode = isPre ? ARM::LDRSB_PRE : ARM::LDRSB_POST;
1377 }
1378 } else {
Owen Anderson4d5c8f82011-08-29 20:16:50 +00001379 if (isPre &&
1380 SelectAddrMode2OffsetImmPre(N, LD->getOffset(), Offset, AMOpc)) {
Evan Chengd9c55362009-07-02 01:23:32 +00001381 Match = true;
Owen Anderson4d5c8f82011-08-29 20:16:50 +00001382 Opcode = ARM::LDRB_PRE_IMM;
1383 } else if (!isPre &&
1384 SelectAddrMode2OffsetImm(N, LD->getOffset(), Offset, AMOpc)) {
1385 Match = true;
1386 Opcode = ARM::LDRB_POST_IMM;
Owen Anderson2aedba62011-07-26 20:54:26 +00001387 } else if (SelectAddrMode2OffsetReg(N, LD->getOffset(), Offset, AMOpc)) {
1388 Match = true;
Owen Anderson16d33f32011-08-26 20:43:14 +00001389 Opcode = isPre ? ARM::LDRB_PRE_REG : ARM::LDRB_POST_REG;
Evan Chengd9c55362009-07-02 01:23:32 +00001390 }
1391 }
1392 }
1393
1394 if (Match) {
Owen Andersonfd60f602011-08-26 21:12:37 +00001395 if (Opcode == ARM::LDR_PRE_IMM || Opcode == ARM::LDRB_PRE_IMM) {
1396 SDValue Chain = LD->getChain();
1397 SDValue Base = LD->getBasePtr();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001398 SDValue Ops[]= { Base, AMOpc, getAL(CurDAG, SDLoc(N)),
Owen Andersonfd60f602011-08-26 21:12:37 +00001399 CurDAG->getRegister(0, MVT::i32), Chain };
Tim Northovereaee28b2016-09-19 09:11:09 +00001400 SDNode *New = CurDAG->getMachineNode(Opcode, SDLoc(N), MVT::i32, MVT::i32,
1401 MVT::Other, Ops);
1402 transferMemOperands(N, New);
1403 ReplaceNode(N, New);
Justin Bogner45571362016-05-12 00:31:09 +00001404 return true;
Owen Andersonfd60f602011-08-26 21:12:37 +00001405 } else {
1406 SDValue Chain = LD->getChain();
1407 SDValue Base = LD->getBasePtr();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001408 SDValue Ops[]= { Base, Offset, AMOpc, getAL(CurDAG, SDLoc(N)),
Owen Andersonfd60f602011-08-26 21:12:37 +00001409 CurDAG->getRegister(0, MVT::i32), Chain };
Tim Northovereaee28b2016-09-19 09:11:09 +00001410 SDNode *New = CurDAG->getMachineNode(Opcode, SDLoc(N), MVT::i32, MVT::i32,
1411 MVT::Other, Ops);
1412 transferMemOperands(N, New);
1413 ReplaceNode(N, New);
Justin Bogner45571362016-05-12 00:31:09 +00001414 return true;
Owen Andersonfd60f602011-08-26 21:12:37 +00001415 }
Evan Chengd9c55362009-07-02 01:23:32 +00001416 }
1417
Justin Bogner45571362016-05-12 00:31:09 +00001418 return false;
Evan Chengd9c55362009-07-02 01:23:32 +00001419}
1420
James Molloyb3326df2016-07-15 08:03:56 +00001421bool ARMDAGToDAGISel::tryT1IndexedLoad(SDNode *N) {
1422 LoadSDNode *LD = cast<LoadSDNode>(N);
1423 EVT LoadedVT = LD->getMemoryVT();
1424 ISD::MemIndexedMode AM = LD->getAddressingMode();
Chandler Carruth5589aa62016-11-03 17:42:02 +00001425 if (AM != ISD::POST_INC || LD->getExtensionType() != ISD::NON_EXTLOAD ||
1426 LoadedVT.getSimpleVT().SimpleTy != MVT::i32)
James Molloyb3326df2016-07-15 08:03:56 +00001427 return false;
1428
1429 auto *COffs = dyn_cast<ConstantSDNode>(LD->getOffset());
1430 if (!COffs || COffs->getZExtValue() != 4)
1431 return false;
1432
1433 // A T1 post-indexed load is just a single register LDM: LDM r0!, {r1}.
1434 // The encoding of LDM is not how the rest of ISel expects a post-inc load to
1435 // look however, so we use a pseudo here and switch it for a tLDMIA_UPD after
1436 // ISel.
1437 SDValue Chain = LD->getChain();
1438 SDValue Base = LD->getBasePtr();
1439 SDValue Ops[]= { Base, getAL(CurDAG, SDLoc(N)),
1440 CurDAG->getRegister(0, MVT::i32), Chain };
Tim Northovereaee28b2016-09-19 09:11:09 +00001441 SDNode *New = CurDAG->getMachineNode(ARM::tLDR_postidx, SDLoc(N), MVT::i32,
1442 MVT::i32, MVT::Other, Ops);
1443 transferMemOperands(N, New);
1444 ReplaceNode(N, New);
James Molloyb3326df2016-07-15 08:03:56 +00001445 return true;
1446}
1447
Justin Bogner45571362016-05-12 00:31:09 +00001448bool ARMDAGToDAGISel::tryT2IndexedLoad(SDNode *N) {
Dan Gohmanea6f91f2010-01-05 01:24:18 +00001449 LoadSDNode *LD = cast<LoadSDNode>(N);
Evan Cheng84c6cda2009-07-02 07:28:31 +00001450 ISD::MemIndexedMode AM = LD->getAddressingMode();
1451 if (AM == ISD::UNINDEXED)
Justin Bogner45571362016-05-12 00:31:09 +00001452 return false;
Evan Cheng84c6cda2009-07-02 07:28:31 +00001453
Owen Anderson53aa7a92009-08-10 22:56:29 +00001454 EVT LoadedVT = LD->getMemoryVT();
Evan Cheng8ecd7eb2009-07-02 23:16:11 +00001455 bool isSExtLd = LD->getExtensionType() == ISD::SEXTLOAD;
Evan Cheng84c6cda2009-07-02 07:28:31 +00001456 SDValue Offset;
1457 bool isPre = (AM == ISD::PRE_INC) || (AM == ISD::PRE_DEC);
1458 unsigned Opcode = 0;
1459 bool Match = false;
Dan Gohmanea6f91f2010-01-05 01:24:18 +00001460 if (SelectT2AddrModeImm8Offset(N, LD->getOffset(), Offset)) {
Owen Anderson9f944592009-08-11 20:47:22 +00001461 switch (LoadedVT.getSimpleVT().SimpleTy) {
1462 case MVT::i32:
Evan Cheng84c6cda2009-07-02 07:28:31 +00001463 Opcode = isPre ? ARM::t2LDR_PRE : ARM::t2LDR_POST;
1464 break;
Owen Anderson9f944592009-08-11 20:47:22 +00001465 case MVT::i16:
Evan Cheng8ecd7eb2009-07-02 23:16:11 +00001466 if (isSExtLd)
1467 Opcode = isPre ? ARM::t2LDRSH_PRE : ARM::t2LDRSH_POST;
1468 else
1469 Opcode = isPre ? ARM::t2LDRH_PRE : ARM::t2LDRH_POST;
Evan Cheng84c6cda2009-07-02 07:28:31 +00001470 break;
Owen Anderson9f944592009-08-11 20:47:22 +00001471 case MVT::i8:
1472 case MVT::i1:
Evan Cheng8ecd7eb2009-07-02 23:16:11 +00001473 if (isSExtLd)
1474 Opcode = isPre ? ARM::t2LDRSB_PRE : ARM::t2LDRSB_POST;
1475 else
1476 Opcode = isPre ? ARM::t2LDRB_PRE : ARM::t2LDRB_POST;
Evan Cheng84c6cda2009-07-02 07:28:31 +00001477 break;
1478 default:
Justin Bogner45571362016-05-12 00:31:09 +00001479 return false;
Evan Cheng84c6cda2009-07-02 07:28:31 +00001480 }
1481 Match = true;
1482 }
1483
1484 if (Match) {
1485 SDValue Chain = LD->getChain();
1486 SDValue Base = LD->getBasePtr();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001487 SDValue Ops[]= { Base, Offset, getAL(CurDAG, SDLoc(N)),
Owen Anderson9f944592009-08-11 20:47:22 +00001488 CurDAG->getRegister(0, MVT::i32), Chain };
Tim Northovereaee28b2016-09-19 09:11:09 +00001489 SDNode *New = CurDAG->getMachineNode(Opcode, SDLoc(N), MVT::i32, MVT::i32,
1490 MVT::Other, Ops);
1491 transferMemOperands(N, New);
1492 ReplaceNode(N, New);
Justin Bogner45571362016-05-12 00:31:09 +00001493 return true;
Evan Cheng84c6cda2009-07-02 07:28:31 +00001494 }
1495
Justin Bogner45571362016-05-12 00:31:09 +00001496 return false;
Evan Cheng84c6cda2009-07-02 07:28:31 +00001497}
1498
Weiming Zhao8f56f882012-11-16 21:55:34 +00001499/// \brief Form a GPRPair pseudo register from a pair of GPR regs.
1500SDNode *ARMDAGToDAGISel::createGPRPairNode(EVT VT, SDValue V0, SDValue V1) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001501 SDLoc dl(V0.getNode());
Weiming Zhao8f56f882012-11-16 21:55:34 +00001502 SDValue RegClass =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001503 CurDAG->getTargetConstant(ARM::GPRPairRegClassID, dl, MVT::i32);
1504 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::gsub_0, dl, MVT::i32);
1505 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::gsub_1, dl, MVT::i32);
Weiming Zhao8f56f882012-11-16 21:55:34 +00001506 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1 };
Michael Liaob53d8962013-04-19 22:22:57 +00001507 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops);
Weiming Zhao8f56f882012-11-16 21:55:34 +00001508}
1509
Weiming Zhao95782222012-11-17 00:23:35 +00001510/// \brief Form a D register from a pair of S registers.
1511SDNode *ARMDAGToDAGISel::createSRegPairNode(EVT VT, SDValue V0, SDValue V1) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001512 SDLoc dl(V0.getNode());
Owen Anderson5fc8b772011-06-16 18:17:13 +00001513 SDValue RegClass =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001514 CurDAG->getTargetConstant(ARM::DPR_VFP2RegClassID, dl, MVT::i32);
1515 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::ssub_0, dl, MVT::i32);
1516 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::ssub_1, dl, MVT::i32);
Owen Anderson5fc8b772011-06-16 18:17:13 +00001517 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1 };
Michael Liaob53d8962013-04-19 22:22:57 +00001518 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops);
Bob Wilsond8a9a042010-06-04 00:04:02 +00001519}
1520
Weiming Zhao95782222012-11-17 00:23:35 +00001521/// \brief Form a quad register from a pair of D registers.
1522SDNode *ARMDAGToDAGISel::createDRegPairNode(EVT VT, SDValue V0, SDValue V1) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001523 SDLoc dl(V0.getNode());
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001524 SDValue RegClass = CurDAG->getTargetConstant(ARM::QPRRegClassID, dl,
1525 MVT::i32);
1526 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::dsub_0, dl, MVT::i32);
1527 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::dsub_1, dl, MVT::i32);
Owen Anderson5fc8b772011-06-16 18:17:13 +00001528 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1 };
Michael Liaob53d8962013-04-19 22:22:57 +00001529 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops);
Bob Wilsone6b778d2009-10-06 22:01:59 +00001530}
1531
Weiming Zhao95782222012-11-17 00:23:35 +00001532/// \brief Form 4 consecutive D registers from a pair of Q registers.
1533SDNode *ARMDAGToDAGISel::createQRegPairNode(EVT VT, SDValue V0, SDValue V1) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001534 SDLoc dl(V0.getNode());
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001535 SDValue RegClass = CurDAG->getTargetConstant(ARM::QQPRRegClassID, dl,
1536 MVT::i32);
1537 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::qsub_0, dl, MVT::i32);
1538 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::qsub_1, dl, MVT::i32);
Owen Anderson5fc8b772011-06-16 18:17:13 +00001539 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1 };
Michael Liaob53d8962013-04-19 22:22:57 +00001540 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops);
Evan Chengc2ae5f52010-05-10 17:34:18 +00001541}
1542
Weiming Zhao95782222012-11-17 00:23:35 +00001543/// \brief Form 4 consecutive S registers.
1544SDNode *ARMDAGToDAGISel::createQuadSRegsNode(EVT VT, SDValue V0, SDValue V1,
Bob Wilsond8a9a042010-06-04 00:04:02 +00001545 SDValue V2, SDValue V3) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001546 SDLoc dl(V0.getNode());
Owen Anderson5fc8b772011-06-16 18:17:13 +00001547 SDValue RegClass =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001548 CurDAG->getTargetConstant(ARM::QPR_VFP2RegClassID, dl, MVT::i32);
1549 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::ssub_0, dl, MVT::i32);
1550 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::ssub_1, dl, MVT::i32);
1551 SDValue SubReg2 = CurDAG->getTargetConstant(ARM::ssub_2, dl, MVT::i32);
1552 SDValue SubReg3 = CurDAG->getTargetConstant(ARM::ssub_3, dl, MVT::i32);
Owen Anderson5fc8b772011-06-16 18:17:13 +00001553 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1,
1554 V2, SubReg2, V3, SubReg3 };
Michael Liaob53d8962013-04-19 22:22:57 +00001555 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops);
Bob Wilsond8a9a042010-06-04 00:04:02 +00001556}
1557
Weiming Zhao95782222012-11-17 00:23:35 +00001558/// \brief Form 4 consecutive D registers.
1559SDNode *ARMDAGToDAGISel::createQuadDRegsNode(EVT VT, SDValue V0, SDValue V1,
Evan Chengc2ae5f52010-05-10 17:34:18 +00001560 SDValue V2, SDValue V3) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001561 SDLoc dl(V0.getNode());
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001562 SDValue RegClass = CurDAG->getTargetConstant(ARM::QQPRRegClassID, dl,
1563 MVT::i32);
1564 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::dsub_0, dl, MVT::i32);
1565 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::dsub_1, dl, MVT::i32);
1566 SDValue SubReg2 = CurDAG->getTargetConstant(ARM::dsub_2, dl, MVT::i32);
1567 SDValue SubReg3 = CurDAG->getTargetConstant(ARM::dsub_3, dl, MVT::i32);
Owen Anderson5fc8b772011-06-16 18:17:13 +00001568 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1,
1569 V2, SubReg2, V3, SubReg3 };
Michael Liaob53d8962013-04-19 22:22:57 +00001570 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops);
Evan Chengc2ae5f52010-05-10 17:34:18 +00001571}
1572
Weiming Zhao95782222012-11-17 00:23:35 +00001573/// \brief Form 4 consecutive Q registers.
1574SDNode *ARMDAGToDAGISel::createQuadQRegsNode(EVT VT, SDValue V0, SDValue V1,
Evan Cheng298e6b82010-05-16 03:27:48 +00001575 SDValue V2, SDValue V3) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001576 SDLoc dl(V0.getNode());
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001577 SDValue RegClass = CurDAG->getTargetConstant(ARM::QQQQPRRegClassID, dl,
1578 MVT::i32);
1579 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::qsub_0, dl, MVT::i32);
1580 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::qsub_1, dl, MVT::i32);
1581 SDValue SubReg2 = CurDAG->getTargetConstant(ARM::qsub_2, dl, MVT::i32);
1582 SDValue SubReg3 = CurDAG->getTargetConstant(ARM::qsub_3, dl, MVT::i32);
Owen Anderson5fc8b772011-06-16 18:17:13 +00001583 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1,
1584 V2, SubReg2, V3, SubReg3 };
Michael Liaob53d8962013-04-19 22:22:57 +00001585 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops);
Evan Cheng298e6b82010-05-16 03:27:48 +00001586}
1587
Bob Wilson7fbbe9a2010-09-23 23:42:37 +00001588/// GetVLDSTAlign - Get the alignment (in bytes) for the alignment operand
1589/// of a NEON VLD or VST instruction. The supported values depend on the
1590/// number of registers being loaded.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001591SDValue ARMDAGToDAGISel::GetVLDSTAlign(SDValue Align, const SDLoc &dl,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001592 unsigned NumVecs, bool is64BitVector) {
Bob Wilson7fbbe9a2010-09-23 23:42:37 +00001593 unsigned NumRegs = NumVecs;
1594 if (!is64BitVector && NumVecs < 3)
1595 NumRegs *= 2;
1596
Bob Wilsondd9fbaa2010-11-01 23:40:51 +00001597 unsigned Alignment = cast<ConstantSDNode>(Align)->getZExtValue();
Bob Wilson7fbbe9a2010-09-23 23:42:37 +00001598 if (Alignment >= 32 && NumRegs == 4)
Bob Wilsondd9fbaa2010-11-01 23:40:51 +00001599 Alignment = 32;
1600 else if (Alignment >= 16 && (NumRegs == 2 || NumRegs == 4))
1601 Alignment = 16;
1602 else if (Alignment >= 8)
1603 Alignment = 8;
1604 else
1605 Alignment = 0;
1606
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001607 return CurDAG->getTargetConstant(Alignment, dl, MVT::i32);
Bob Wilson7fbbe9a2010-09-23 23:42:37 +00001608}
1609
Jiangning Liu4df23632014-01-16 09:16:13 +00001610static bool isVLDfixed(unsigned Opc)
1611{
1612 switch (Opc) {
1613 default: return false;
1614 case ARM::VLD1d8wb_fixed : return true;
1615 case ARM::VLD1d16wb_fixed : return true;
1616 case ARM::VLD1d64Qwb_fixed : return true;
1617 case ARM::VLD1d32wb_fixed : return true;
1618 case ARM::VLD1d64wb_fixed : return true;
1619 case ARM::VLD1d64TPseudoWB_fixed : return true;
1620 case ARM::VLD1d64QPseudoWB_fixed : return true;
1621 case ARM::VLD1q8wb_fixed : return true;
1622 case ARM::VLD1q16wb_fixed : return true;
1623 case ARM::VLD1q32wb_fixed : return true;
1624 case ARM::VLD1q64wb_fixed : return true;
Eli Friedmanf624ec22016-12-16 18:44:08 +00001625 case ARM::VLD1DUPd8wb_fixed : return true;
1626 case ARM::VLD1DUPd16wb_fixed : return true;
1627 case ARM::VLD1DUPd32wb_fixed : return true;
1628 case ARM::VLD1DUPq8wb_fixed : return true;
1629 case ARM::VLD1DUPq16wb_fixed : return true;
1630 case ARM::VLD1DUPq32wb_fixed : return true;
Jiangning Liu4df23632014-01-16 09:16:13 +00001631 case ARM::VLD2d8wb_fixed : return true;
1632 case ARM::VLD2d16wb_fixed : return true;
1633 case ARM::VLD2d32wb_fixed : return true;
1634 case ARM::VLD2q8PseudoWB_fixed : return true;
1635 case ARM::VLD2q16PseudoWB_fixed : return true;
1636 case ARM::VLD2q32PseudoWB_fixed : return true;
1637 case ARM::VLD2DUPd8wb_fixed : return true;
1638 case ARM::VLD2DUPd16wb_fixed : return true;
1639 case ARM::VLD2DUPd32wb_fixed : return true;
1640 }
1641}
1642
1643static bool isVSTfixed(unsigned Opc)
1644{
1645 switch (Opc) {
1646 default: return false;
1647 case ARM::VST1d8wb_fixed : return true;
1648 case ARM::VST1d16wb_fixed : return true;
1649 case ARM::VST1d32wb_fixed : return true;
1650 case ARM::VST1d64wb_fixed : return true;
Jim Grosbach1a597112014-04-03 23:43:18 +00001651 case ARM::VST1q8wb_fixed : return true;
1652 case ARM::VST1q16wb_fixed : return true;
1653 case ARM::VST1q32wb_fixed : return true;
1654 case ARM::VST1q64wb_fixed : return true;
Jiangning Liu4df23632014-01-16 09:16:13 +00001655 case ARM::VST1d64TPseudoWB_fixed : return true;
1656 case ARM::VST1d64QPseudoWB_fixed : return true;
1657 case ARM::VST2d8wb_fixed : return true;
1658 case ARM::VST2d16wb_fixed : return true;
1659 case ARM::VST2d32wb_fixed : return true;
1660 case ARM::VST2q8PseudoWB_fixed : return true;
1661 case ARM::VST2q16PseudoWB_fixed : return true;
1662 case ARM::VST2q32PseudoWB_fixed : return true;
1663 }
1664}
1665
Jim Grosbach2098cb12011-10-24 21:45:13 +00001666// Get the register stride update opcode of a VLD/VST instruction that
1667// is otherwise equivalent to the given fixed stride updating instruction.
1668static unsigned getVLDSTRegisterUpdateOpcode(unsigned Opc) {
Jiangning Liu4df23632014-01-16 09:16:13 +00001669 assert((isVLDfixed(Opc) || isVSTfixed(Opc))
1670 && "Incorrect fixed stride updating instruction.");
Jim Grosbach2098cb12011-10-24 21:45:13 +00001671 switch (Opc) {
1672 default: break;
1673 case ARM::VLD1d8wb_fixed: return ARM::VLD1d8wb_register;
1674 case ARM::VLD1d16wb_fixed: return ARM::VLD1d16wb_register;
1675 case ARM::VLD1d32wb_fixed: return ARM::VLD1d32wb_register;
1676 case ARM::VLD1d64wb_fixed: return ARM::VLD1d64wb_register;
1677 case ARM::VLD1q8wb_fixed: return ARM::VLD1q8wb_register;
1678 case ARM::VLD1q16wb_fixed: return ARM::VLD1q16wb_register;
1679 case ARM::VLD1q32wb_fixed: return ARM::VLD1q32wb_register;
1680 case ARM::VLD1q64wb_fixed: return ARM::VLD1q64wb_register;
Jiangning Liu4df23632014-01-16 09:16:13 +00001681 case ARM::VLD1d64Twb_fixed: return ARM::VLD1d64Twb_register;
1682 case ARM::VLD1d64Qwb_fixed: return ARM::VLD1d64Qwb_register;
1683 case ARM::VLD1d64TPseudoWB_fixed: return ARM::VLD1d64TPseudoWB_register;
1684 case ARM::VLD1d64QPseudoWB_fixed: return ARM::VLD1d64QPseudoWB_register;
Eli Friedmanf624ec22016-12-16 18:44:08 +00001685 case ARM::VLD1DUPd8wb_fixed : return ARM::VLD1DUPd8wb_register;
1686 case ARM::VLD1DUPd16wb_fixed : return ARM::VLD1DUPd16wb_register;
1687 case ARM::VLD1DUPd32wb_fixed : return ARM::VLD1DUPd32wb_register;
1688 case ARM::VLD1DUPq8wb_fixed : return ARM::VLD1DUPq8wb_register;
1689 case ARM::VLD1DUPq16wb_fixed : return ARM::VLD1DUPq16wb_register;
1690 case ARM::VLD1DUPq32wb_fixed : return ARM::VLD1DUPq32wb_register;
Jim Grosbach05df4602011-10-31 21:50:31 +00001691
1692 case ARM::VST1d8wb_fixed: return ARM::VST1d8wb_register;
1693 case ARM::VST1d16wb_fixed: return ARM::VST1d16wb_register;
1694 case ARM::VST1d32wb_fixed: return ARM::VST1d32wb_register;
1695 case ARM::VST1d64wb_fixed: return ARM::VST1d64wb_register;
1696 case ARM::VST1q8wb_fixed: return ARM::VST1q8wb_register;
1697 case ARM::VST1q16wb_fixed: return ARM::VST1q16wb_register;
1698 case ARM::VST1q32wb_fixed: return ARM::VST1q32wb_register;
1699 case ARM::VST1q64wb_fixed: return ARM::VST1q64wb_register;
Jim Grosbach98d032f2011-11-29 22:38:04 +00001700 case ARM::VST1d64TPseudoWB_fixed: return ARM::VST1d64TPseudoWB_register;
Jim Grosbach5ee209c2011-11-29 22:58:48 +00001701 case ARM::VST1d64QPseudoWB_fixed: return ARM::VST1d64QPseudoWB_register;
Jim Grosbachd146a022011-12-09 21:28:25 +00001702
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001703 case ARM::VLD2d8wb_fixed: return ARM::VLD2d8wb_register;
1704 case ARM::VLD2d16wb_fixed: return ARM::VLD2d16wb_register;
1705 case ARM::VLD2d32wb_fixed: return ARM::VLD2d32wb_register;
Jim Grosbachd146a022011-12-09 21:28:25 +00001706 case ARM::VLD2q8PseudoWB_fixed: return ARM::VLD2q8PseudoWB_register;
1707 case ARM::VLD2q16PseudoWB_fixed: return ARM::VLD2q16PseudoWB_register;
1708 case ARM::VLD2q32PseudoWB_fixed: return ARM::VLD2q32PseudoWB_register;
1709
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001710 case ARM::VST2d8wb_fixed: return ARM::VST2d8wb_register;
1711 case ARM::VST2d16wb_fixed: return ARM::VST2d16wb_register;
1712 case ARM::VST2d32wb_fixed: return ARM::VST2d32wb_register;
Jim Grosbach88ac7612011-12-14 21:32:11 +00001713 case ARM::VST2q8PseudoWB_fixed: return ARM::VST2q8PseudoWB_register;
1714 case ARM::VST2q16PseudoWB_fixed: return ARM::VST2q16PseudoWB_register;
1715 case ARM::VST2q32PseudoWB_fixed: return ARM::VST2q32PseudoWB_register;
Jim Grosbachc80a2642011-12-21 19:40:55 +00001716
Jim Grosbach13a292c2012-03-06 22:01:44 +00001717 case ARM::VLD2DUPd8wb_fixed: return ARM::VLD2DUPd8wb_register;
1718 case ARM::VLD2DUPd16wb_fixed: return ARM::VLD2DUPd16wb_register;
1719 case ARM::VLD2DUPd32wb_fixed: return ARM::VLD2DUPd32wb_register;
Jim Grosbach2098cb12011-10-24 21:45:13 +00001720 }
1721 return Opc; // If not one we handle, return it unchanged.
1722}
1723
Tim Northover8b1240b2017-04-20 19:54:02 +00001724/// Returns true if the given increment is a Constant known to be equal to the
1725/// access size performed by a NEON load/store. This means the "[rN]!" form can
1726/// be used.
1727static bool isPerfectIncrement(SDValue Inc, EVT VecTy, unsigned NumVecs) {
1728 auto C = dyn_cast<ConstantSDNode>(Inc);
1729 return C && C->getZExtValue() == VecTy.getSizeInBits() / 8 * NumVecs;
1730}
1731
Justin Bogner45571362016-05-12 00:31:09 +00001732void ARMDAGToDAGISel::SelectVLD(SDNode *N, bool isUpdating, unsigned NumVecs,
1733 const uint16_t *DOpcodes,
1734 const uint16_t *QOpcodes0,
1735 const uint16_t *QOpcodes1) {
Bob Wilson340861d2010-03-23 05:25:43 +00001736 assert(NumVecs >= 1 && NumVecs <= 4 && "VLD NumVecs out-of-range");
Andrew Trickef9de2a2013-05-25 02:42:55 +00001737 SDLoc dl(N);
Bob Wilson12b47992009-10-14 17:28:52 +00001738
Bob Wilsonae08a732010-03-20 22:13:40 +00001739 SDValue MemAddr, Align;
Bob Wilson06fce872011-02-07 17:43:21 +00001740 unsigned AddrOpIdx = isUpdating ? 1 : 2;
1741 if (!SelectAddrMode6(N, N->getOperand(AddrOpIdx), MemAddr, Align))
Justin Bogner45571362016-05-12 00:31:09 +00001742 return;
Bob Wilson12b47992009-10-14 17:28:52 +00001743
1744 SDValue Chain = N->getOperand(0);
1745 EVT VT = N->getValueType(0);
1746 bool is64BitVector = VT.is64BitVector();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001747 Align = GetVLDSTAlign(Align, dl, NumVecs, is64BitVector);
Bob Wilson9eeb8902010-09-23 21:43:54 +00001748
Bob Wilson12b47992009-10-14 17:28:52 +00001749 unsigned OpcodeIndex;
1750 switch (VT.getSimpleVT().SimpleTy) {
1751 default: llvm_unreachable("unhandled vld type");
1752 // Double-register operations:
1753 case MVT::v8i8: OpcodeIndex = 0; break;
1754 case MVT::v4i16: OpcodeIndex = 1; break;
1755 case MVT::v2f32:
1756 case MVT::v2i32: OpcodeIndex = 2; break;
1757 case MVT::v1i64: OpcodeIndex = 3; break;
1758 // Quad-register operations:
1759 case MVT::v16i8: OpcodeIndex = 0; break;
1760 case MVT::v8i16: OpcodeIndex = 1; break;
1761 case MVT::v4f32:
1762 case MVT::v4i32: OpcodeIndex = 2; break;
Ahmed Bougachabe0b2272014-12-09 21:25:00 +00001763 case MVT::v2f64:
Bob Wilson340861d2010-03-23 05:25:43 +00001764 case MVT::v2i64: OpcodeIndex = 3;
Bob Wilsoncc0a2a72010-03-23 06:20:33 +00001765 assert(NumVecs == 1 && "v2i64 type only supported for VLD1");
Bob Wilson340861d2010-03-23 05:25:43 +00001766 break;
Bob Wilson12b47992009-10-14 17:28:52 +00001767 }
1768
Bob Wilson35fafca2010-09-03 18:16:02 +00001769 EVT ResTy;
1770 if (NumVecs == 1)
1771 ResTy = VT;
1772 else {
1773 unsigned ResTyElts = (NumVecs == 3) ? 4 : NumVecs;
1774 if (!is64BitVector)
1775 ResTyElts *= 2;
1776 ResTy = EVT::getVectorVT(*CurDAG->getContext(), MVT::i64, ResTyElts);
1777 }
Bob Wilson06fce872011-02-07 17:43:21 +00001778 std::vector<EVT> ResTys;
1779 ResTys.push_back(ResTy);
1780 if (isUpdating)
1781 ResTys.push_back(MVT::i32);
1782 ResTys.push_back(MVT::Other);
Bob Wilson35fafca2010-09-03 18:16:02 +00001783
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001784 SDValue Pred = getAL(CurDAG, dl);
Bob Wilsonae08a732010-03-20 22:13:40 +00001785 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Bob Wilson06fce872011-02-07 17:43:21 +00001786 SDNode *VLd;
1787 SmallVector<SDValue, 7> Ops;
Evan Cheng630063a2010-05-10 21:26:24 +00001788
Bob Wilson06fce872011-02-07 17:43:21 +00001789 // Double registers and VLD1/VLD2 quad registers are directly supported.
1790 if (is64BitVector || NumVecs <= 2) {
1791 unsigned Opc = (is64BitVector ? DOpcodes[OpcodeIndex] :
1792 QOpcodes0[OpcodeIndex]);
1793 Ops.push_back(MemAddr);
1794 Ops.push_back(Align);
1795 if (isUpdating) {
1796 SDValue Inc = N->getOperand(AddrOpIdx + 1);
Jim Grosbachd146a022011-12-09 21:28:25 +00001797 // FIXME: VLD1/VLD2 fixed increment doesn't need Reg0. Remove the reg0
Jim Grosbach2098cb12011-10-24 21:45:13 +00001798 // case entirely when the rest are updated to that form, too.
Tim Northover8b1240b2017-04-20 19:54:02 +00001799 bool IsImmUpdate = isPerfectIncrement(Inc, VT, NumVecs);
1800 if ((NumVecs <= 2) && !IsImmUpdate)
Jim Grosbach2098cb12011-10-24 21:45:13 +00001801 Opc = getVLDSTRegisterUpdateOpcode(Opc);
Jiangning Liu4df23632014-01-16 09:16:13 +00001802 // FIXME: We use a VLD1 for v1i64 even if the pseudo says vld2/3/4, so
Jim Grosbach05df4602011-10-31 21:50:31 +00001803 // check for that explicitly too. Horribly hacky, but temporary.
Tim Northover8b1240b2017-04-20 19:54:02 +00001804 if ((NumVecs > 2 && !isVLDfixed(Opc)) || !IsImmUpdate)
1805 Ops.push_back(IsImmUpdate ? Reg0 : Inc);
Evan Cheng630063a2010-05-10 21:26:24 +00001806 }
Bob Wilson06fce872011-02-07 17:43:21 +00001807 Ops.push_back(Pred);
1808 Ops.push_back(Reg0);
1809 Ops.push_back(Chain);
Michael Liaob53d8962013-04-19 22:22:57 +00001810 VLd = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
Bob Wilson75a64082010-09-02 16:00:54 +00001811
Bob Wilson12b47992009-10-14 17:28:52 +00001812 } else {
1813 // Otherwise, quad registers are loaded with two separate instructions,
1814 // where one loads the even registers and the other loads the odd registers.
Bob Wilson35fafca2010-09-03 18:16:02 +00001815 EVT AddrTy = MemAddr.getValueType();
Bob Wilson12b47992009-10-14 17:28:52 +00001816
Bob Wilson06fce872011-02-07 17:43:21 +00001817 // Load the even subregs. This is always an updating load, so that it
1818 // provides the address to the second load for the odd subregs.
Bob Wilson35fafca2010-09-03 18:16:02 +00001819 SDValue ImplDef =
1820 SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, ResTy), 0);
1821 const SDValue OpsA[] = { MemAddr, Align, Reg0, ImplDef, Pred, Reg0, Chain };
Bob Wilsona609b892011-02-07 17:43:15 +00001822 SDNode *VLdA = CurDAG->getMachineNode(QOpcodes0[OpcodeIndex], dl,
Michael Liaob53d8962013-04-19 22:22:57 +00001823 ResTy, AddrTy, MVT::Other, OpsA);
Bob Wilson35fafca2010-09-03 18:16:02 +00001824 Chain = SDValue(VLdA, 2);
Bob Wilson12b47992009-10-14 17:28:52 +00001825
Bob Wilsonc350cdf2009-10-14 18:32:29 +00001826 // Load the odd subregs.
Bob Wilson06fce872011-02-07 17:43:21 +00001827 Ops.push_back(SDValue(VLdA, 1));
1828 Ops.push_back(Align);
1829 if (isUpdating) {
1830 SDValue Inc = N->getOperand(AddrOpIdx + 1);
1831 assert(isa<ConstantSDNode>(Inc.getNode()) &&
1832 "only constant post-increment update allowed for VLD3/4");
1833 (void)Inc;
1834 Ops.push_back(Reg0);
1835 }
1836 Ops.push_back(SDValue(VLdA, 0));
1837 Ops.push_back(Pred);
1838 Ops.push_back(Reg0);
1839 Ops.push_back(Chain);
Michael Liaob53d8962013-04-19 22:22:57 +00001840 VLd = CurDAG->getMachineNode(QOpcodes1[OpcodeIndex], dl, ResTys, Ops);
Bob Wilson35fafca2010-09-03 18:16:02 +00001841 }
Bob Wilson12b47992009-10-14 17:28:52 +00001842
Evan Cheng40791332011-04-19 00:04:03 +00001843 // Transfer memoperands.
1844 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1845 MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
1846 cast<MachineSDNode>(VLd)->setMemRefs(MemOp, MemOp + 1);
1847
Justin Bogner45571362016-05-12 00:31:09 +00001848 if (NumVecs == 1) {
1849 ReplaceNode(N, VLd);
1850 return;
1851 }
Bob Wilson06fce872011-02-07 17:43:21 +00001852
1853 // Extract out the subregisters.
1854 SDValue SuperReg = SDValue(VLd, 0);
Benjamin Kramer3e9a5d32016-05-27 11:36:04 +00001855 static_assert(ARM::dsub_7 == ARM::dsub_0 + 7 &&
1856 ARM::qsub_3 == ARM::qsub_0 + 3,
1857 "Unexpected subreg numbering");
Bob Wilson06fce872011-02-07 17:43:21 +00001858 unsigned Sub0 = (is64BitVector ? ARM::dsub_0 : ARM::qsub_0);
1859 for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
1860 ReplaceUses(SDValue(N, Vec),
1861 CurDAG->getTargetExtractSubreg(Sub0 + Vec, dl, VT, SuperReg));
1862 ReplaceUses(SDValue(N, NumVecs), SDValue(VLd, 1));
1863 if (isUpdating)
1864 ReplaceUses(SDValue(N, NumVecs + 1), SDValue(VLd, 2));
Justin Bognered4f3782016-05-12 00:20:19 +00001865 CurDAG->RemoveDeadNode(N);
Bob Wilson12b47992009-10-14 17:28:52 +00001866}
1867
Justin Bogner45571362016-05-12 00:31:09 +00001868void ARMDAGToDAGISel::SelectVST(SDNode *N, bool isUpdating, unsigned NumVecs,
1869 const uint16_t *DOpcodes,
1870 const uint16_t *QOpcodes0,
1871 const uint16_t *QOpcodes1) {
Bob Wilson3ed511b2010-07-06 23:36:25 +00001872 assert(NumVecs >= 1 && NumVecs <= 4 && "VST NumVecs out-of-range");
Andrew Trickef9de2a2013-05-25 02:42:55 +00001873 SDLoc dl(N);
Bob Wilsonc350cdf2009-10-14 18:32:29 +00001874
Bob Wilsonae08a732010-03-20 22:13:40 +00001875 SDValue MemAddr, Align;
Bob Wilson06fce872011-02-07 17:43:21 +00001876 unsigned AddrOpIdx = isUpdating ? 1 : 2;
1877 unsigned Vec0Idx = 3; // AddrOpIdx + (isUpdating ? 2 : 1)
1878 if (!SelectAddrMode6(N, N->getOperand(AddrOpIdx), MemAddr, Align))
Justin Bogner45571362016-05-12 00:31:09 +00001879 return;
Bob Wilsonc350cdf2009-10-14 18:32:29 +00001880
Evan Cheng40791332011-04-19 00:04:03 +00001881 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1882 MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
1883
Bob Wilsonc350cdf2009-10-14 18:32:29 +00001884 SDValue Chain = N->getOperand(0);
Bob Wilson06fce872011-02-07 17:43:21 +00001885 EVT VT = N->getOperand(Vec0Idx).getValueType();
Bob Wilsonc350cdf2009-10-14 18:32:29 +00001886 bool is64BitVector = VT.is64BitVector();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001887 Align = GetVLDSTAlign(Align, dl, NumVecs, is64BitVector);
Bob Wilson7fbbe9a2010-09-23 23:42:37 +00001888
Bob Wilsonc350cdf2009-10-14 18:32:29 +00001889 unsigned OpcodeIndex;
1890 switch (VT.getSimpleVT().SimpleTy) {
1891 default: llvm_unreachable("unhandled vst type");
1892 // Double-register operations:
1893 case MVT::v8i8: OpcodeIndex = 0; break;
1894 case MVT::v4i16: OpcodeIndex = 1; break;
1895 case MVT::v2f32:
1896 case MVT::v2i32: OpcodeIndex = 2; break;
1897 case MVT::v1i64: OpcodeIndex = 3; break;
1898 // Quad-register operations:
1899 case MVT::v16i8: OpcodeIndex = 0; break;
1900 case MVT::v8i16: OpcodeIndex = 1; break;
1901 case MVT::v4f32:
1902 case MVT::v4i32: OpcodeIndex = 2; break;
Ahmed Bougachabe0b2272014-12-09 21:25:00 +00001903 case MVT::v2f64:
Bob Wilsoncc0a2a72010-03-23 06:20:33 +00001904 case MVT::v2i64: OpcodeIndex = 3;
1905 assert(NumVecs == 1 && "v2i64 type only supported for VST1");
1906 break;
Bob Wilsonc350cdf2009-10-14 18:32:29 +00001907 }
1908
Bob Wilson06fce872011-02-07 17:43:21 +00001909 std::vector<EVT> ResTys;
1910 if (isUpdating)
1911 ResTys.push_back(MVT::i32);
1912 ResTys.push_back(MVT::Other);
1913
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001914 SDValue Pred = getAL(CurDAG, dl);
Bob Wilsonae08a732010-03-20 22:13:40 +00001915 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Bob Wilson06fce872011-02-07 17:43:21 +00001916 SmallVector<SDValue, 7> Ops;
Evan Chenga33fc862009-11-21 06:21:52 +00001917
Bob Wilson06fce872011-02-07 17:43:21 +00001918 // Double registers and VST1/VST2 quad registers are directly supported.
1919 if (is64BitVector || NumVecs <= 2) {
Bob Wilsona609b892011-02-07 17:43:15 +00001920 SDValue SrcReg;
Bob Wilson950882b2010-08-28 05:12:57 +00001921 if (NumVecs == 1) {
Bob Wilson06fce872011-02-07 17:43:21 +00001922 SrcReg = N->getOperand(Vec0Idx);
1923 } else if (is64BitVector) {
Evan Chenge276c182010-05-11 01:19:40 +00001924 // Form a REG_SEQUENCE to force register allocation.
Bob Wilson06fce872011-02-07 17:43:21 +00001925 SDValue V0 = N->getOperand(Vec0Idx + 0);
1926 SDValue V1 = N->getOperand(Vec0Idx + 1);
Evan Chenge276c182010-05-11 01:19:40 +00001927 if (NumVecs == 2)
Weiming Zhao95782222012-11-17 00:23:35 +00001928 SrcReg = SDValue(createDRegPairNode(MVT::v2i64, V0, V1), 0);
Evan Chenge276c182010-05-11 01:19:40 +00001929 else {
Bob Wilson06fce872011-02-07 17:43:21 +00001930 SDValue V2 = N->getOperand(Vec0Idx + 2);
Bob Wilsona609b892011-02-07 17:43:15 +00001931 // If it's a vst3, form a quad D-register and leave the last part as
Evan Chenge276c182010-05-11 01:19:40 +00001932 // an undef.
1933 SDValue V3 = (NumVecs == 3)
1934 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,dl,VT), 0)
Bob Wilson06fce872011-02-07 17:43:21 +00001935 : N->getOperand(Vec0Idx + 3);
Weiming Zhao95782222012-11-17 00:23:35 +00001936 SrcReg = SDValue(createQuadDRegsNode(MVT::v4i64, V0, V1, V2, V3), 0);
Evan Chenge276c182010-05-11 01:19:40 +00001937 }
Bob Wilson950882b2010-08-28 05:12:57 +00001938 } else {
1939 // Form a QQ register.
Bob Wilson06fce872011-02-07 17:43:21 +00001940 SDValue Q0 = N->getOperand(Vec0Idx);
1941 SDValue Q1 = N->getOperand(Vec0Idx + 1);
Weiming Zhao95782222012-11-17 00:23:35 +00001942 SrcReg = SDValue(createQRegPairNode(MVT::v4i64, Q0, Q1), 0);
Bob Wilsonc350cdf2009-10-14 18:32:29 +00001943 }
Bob Wilson06fce872011-02-07 17:43:21 +00001944
1945 unsigned Opc = (is64BitVector ? DOpcodes[OpcodeIndex] :
1946 QOpcodes0[OpcodeIndex]);
1947 Ops.push_back(MemAddr);
1948 Ops.push_back(Align);
1949 if (isUpdating) {
1950 SDValue Inc = N->getOperand(AddrOpIdx + 1);
Jim Grosbach88ac7612011-12-14 21:32:11 +00001951 // FIXME: VST1/VST2 fixed increment doesn't need Reg0. Remove the reg0
Jim Grosbach05df4602011-10-31 21:50:31 +00001952 // case entirely when the rest are updated to that form, too.
Tim Northover8b1240b2017-04-20 19:54:02 +00001953 bool IsImmUpdate = isPerfectIncrement(Inc, VT, NumVecs);
1954 if (NumVecs <= 2 && !IsImmUpdate)
Jim Grosbach05df4602011-10-31 21:50:31 +00001955 Opc = getVLDSTRegisterUpdateOpcode(Opc);
Jiangning Liu4df23632014-01-16 09:16:13 +00001956 // FIXME: We use a VST1 for v1i64 even if the pseudo says vld2/3/4, so
Jim Grosbach05df4602011-10-31 21:50:31 +00001957 // check for that explicitly too. Horribly hacky, but temporary.
Tim Northover8b1240b2017-04-20 19:54:02 +00001958 if (!IsImmUpdate)
Jiangning Liu4df23632014-01-16 09:16:13 +00001959 Ops.push_back(Inc);
1960 else if (NumVecs > 2 && !isVSTfixed(Opc))
1961 Ops.push_back(Reg0);
Bob Wilson06fce872011-02-07 17:43:21 +00001962 }
1963 Ops.push_back(SrcReg);
1964 Ops.push_back(Pred);
1965 Ops.push_back(Reg0);
1966 Ops.push_back(Chain);
Michael Liaob53d8962013-04-19 22:22:57 +00001967 SDNode *VSt = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
Evan Cheng40791332011-04-19 00:04:03 +00001968
1969 // Transfer memoperands.
1970 cast<MachineSDNode>(VSt)->setMemRefs(MemOp, MemOp + 1);
1971
Justin Bogner45571362016-05-12 00:31:09 +00001972 ReplaceNode(N, VSt);
1973 return;
Bob Wilsonc350cdf2009-10-14 18:32:29 +00001974 }
1975
1976 // Otherwise, quad registers are stored with two separate instructions,
1977 // where one stores the even registers and the other stores the odd registers.
Evan Cheng9e688cb2010-05-15 07:53:37 +00001978
Bob Wilson01ac8f92010-06-16 21:34:01 +00001979 // Form the QQQQ REG_SEQUENCE.
Bob Wilson06fce872011-02-07 17:43:21 +00001980 SDValue V0 = N->getOperand(Vec0Idx + 0);
1981 SDValue V1 = N->getOperand(Vec0Idx + 1);
1982 SDValue V2 = N->getOperand(Vec0Idx + 2);
Bob Wilson950882b2010-08-28 05:12:57 +00001983 SDValue V3 = (NumVecs == 3)
1984 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, VT), 0)
Bob Wilson06fce872011-02-07 17:43:21 +00001985 : N->getOperand(Vec0Idx + 3);
Weiming Zhao95782222012-11-17 00:23:35 +00001986 SDValue RegSeq = SDValue(createQuadQRegsNode(MVT::v8i64, V0, V1, V2, V3), 0);
Bob Wilson01ac8f92010-06-16 21:34:01 +00001987
Bob Wilson06fce872011-02-07 17:43:21 +00001988 // Store the even D registers. This is always an updating store, so that it
1989 // provides the address to the second store for the odd subregs.
Bob Wilsona609b892011-02-07 17:43:15 +00001990 const SDValue OpsA[] = { MemAddr, Align, Reg0, RegSeq, Pred, Reg0, Chain };
1991 SDNode *VStA = CurDAG->getMachineNode(QOpcodes0[OpcodeIndex], dl,
1992 MemAddr.getValueType(),
Michael Liaob53d8962013-04-19 22:22:57 +00001993 MVT::Other, OpsA);
Evan Cheng40791332011-04-19 00:04:03 +00001994 cast<MachineSDNode>(VStA)->setMemRefs(MemOp, MemOp + 1);
Bob Wilson01ac8f92010-06-16 21:34:01 +00001995 Chain = SDValue(VStA, 1);
1996
1997 // Store the odd D registers.
Bob Wilson06fce872011-02-07 17:43:21 +00001998 Ops.push_back(SDValue(VStA, 0));
1999 Ops.push_back(Align);
2000 if (isUpdating) {
2001 SDValue Inc = N->getOperand(AddrOpIdx + 1);
2002 assert(isa<ConstantSDNode>(Inc.getNode()) &&
2003 "only constant post-increment update allowed for VST3/4");
2004 (void)Inc;
2005 Ops.push_back(Reg0);
2006 }
2007 Ops.push_back(RegSeq);
2008 Ops.push_back(Pred);
2009 Ops.push_back(Reg0);
2010 Ops.push_back(Chain);
Evan Cheng40791332011-04-19 00:04:03 +00002011 SDNode *VStB = CurDAG->getMachineNode(QOpcodes1[OpcodeIndex], dl, ResTys,
Michael Liaob53d8962013-04-19 22:22:57 +00002012 Ops);
Evan Cheng40791332011-04-19 00:04:03 +00002013 cast<MachineSDNode>(VStB)->setMemRefs(MemOp, MemOp + 1);
Justin Bogner45571362016-05-12 00:31:09 +00002014 ReplaceNode(N, VStB);
Bob Wilsonc350cdf2009-10-14 18:32:29 +00002015}
2016
Justin Bogner45571362016-05-12 00:31:09 +00002017void ARMDAGToDAGISel::SelectVLDSTLane(SDNode *N, bool IsLoad, bool isUpdating,
2018 unsigned NumVecs,
2019 const uint16_t *DOpcodes,
2020 const uint16_t *QOpcodes) {
Bob Wilson93117bc2009-10-14 16:46:45 +00002021 assert(NumVecs >=2 && NumVecs <= 4 && "VLDSTLane NumVecs out-of-range");
Andrew Trickef9de2a2013-05-25 02:42:55 +00002022 SDLoc dl(N);
Bob Wilson4145e3a2009-10-14 16:19:03 +00002023
Bob Wilsonae08a732010-03-20 22:13:40 +00002024 SDValue MemAddr, Align;
Bob Wilson06fce872011-02-07 17:43:21 +00002025 unsigned AddrOpIdx = isUpdating ? 1 : 2;
2026 unsigned Vec0Idx = 3; // AddrOpIdx + (isUpdating ? 2 : 1)
2027 if (!SelectAddrMode6(N, N->getOperand(AddrOpIdx), MemAddr, Align))
Justin Bogner45571362016-05-12 00:31:09 +00002028 return;
Bob Wilson4145e3a2009-10-14 16:19:03 +00002029
Evan Cheng40791332011-04-19 00:04:03 +00002030 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
2031 MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
2032
Bob Wilson4145e3a2009-10-14 16:19:03 +00002033 SDValue Chain = N->getOperand(0);
2034 unsigned Lane =
Bob Wilson06fce872011-02-07 17:43:21 +00002035 cast<ConstantSDNode>(N->getOperand(Vec0Idx + NumVecs))->getZExtValue();
2036 EVT VT = N->getOperand(Vec0Idx).getValueType();
Bob Wilson4145e3a2009-10-14 16:19:03 +00002037 bool is64BitVector = VT.is64BitVector();
2038
Bob Wilsondd9fbaa2010-11-01 23:40:51 +00002039 unsigned Alignment = 0;
Bob Wilsonb6d61dc2010-10-19 00:16:32 +00002040 if (NumVecs != 3) {
Bob Wilsondd9fbaa2010-11-01 23:40:51 +00002041 Alignment = cast<ConstantSDNode>(Align)->getZExtValue();
Sanjay Patel1ed771f2016-09-14 16:37:15 +00002042 unsigned NumBytes = NumVecs * VT.getScalarSizeInBits() / 8;
Bob Wilsonb6d61dc2010-10-19 00:16:32 +00002043 if (Alignment > NumBytes)
2044 Alignment = NumBytes;
Bob Wilsond29b38c2010-12-10 19:37:42 +00002045 if (Alignment < 8 && Alignment < NumBytes)
2046 Alignment = 0;
Bob Wilsonb6d61dc2010-10-19 00:16:32 +00002047 // Alignment must be a power of two; make sure of that.
2048 Alignment = (Alignment & -Alignment);
Bob Wilsondd9fbaa2010-11-01 23:40:51 +00002049 if (Alignment == 1)
2050 Alignment = 0;
Bob Wilsonb6d61dc2010-10-19 00:16:32 +00002051 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002052 Align = CurDAG->getTargetConstant(Alignment, dl, MVT::i32);
Bob Wilsonb6d61dc2010-10-19 00:16:32 +00002053
Bob Wilson4145e3a2009-10-14 16:19:03 +00002054 unsigned OpcodeIndex;
2055 switch (VT.getSimpleVT().SimpleTy) {
Bob Wilson93117bc2009-10-14 16:46:45 +00002056 default: llvm_unreachable("unhandled vld/vst lane type");
Bob Wilson4145e3a2009-10-14 16:19:03 +00002057 // Double-register operations:
2058 case MVT::v8i8: OpcodeIndex = 0; break;
2059 case MVT::v4i16: OpcodeIndex = 1; break;
2060 case MVT::v2f32:
2061 case MVT::v2i32: OpcodeIndex = 2; break;
2062 // Quad-register operations:
2063 case MVT::v8i16: OpcodeIndex = 0; break;
2064 case MVT::v4f32:
2065 case MVT::v4i32: OpcodeIndex = 1; break;
2066 }
2067
Bob Wilson06fce872011-02-07 17:43:21 +00002068 std::vector<EVT> ResTys;
2069 if (IsLoad) {
2070 unsigned ResTyElts = (NumVecs == 3) ? 4 : NumVecs;
2071 if (!is64BitVector)
2072 ResTyElts *= 2;
2073 ResTys.push_back(EVT::getVectorVT(*CurDAG->getContext(),
2074 MVT::i64, ResTyElts));
2075 }
2076 if (isUpdating)
2077 ResTys.push_back(MVT::i32);
2078 ResTys.push_back(MVT::Other);
2079
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002080 SDValue Pred = getAL(CurDAG, dl);
Bob Wilsonae08a732010-03-20 22:13:40 +00002081 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Evan Chenga33fc862009-11-21 06:21:52 +00002082
Bob Wilson06fce872011-02-07 17:43:21 +00002083 SmallVector<SDValue, 8> Ops;
Bob Wilson4145e3a2009-10-14 16:19:03 +00002084 Ops.push_back(MemAddr);
Jim Grosbachd1d002a2009-11-07 21:25:39 +00002085 Ops.push_back(Align);
Bob Wilson06fce872011-02-07 17:43:21 +00002086 if (isUpdating) {
2087 SDValue Inc = N->getOperand(AddrOpIdx + 1);
Tim Northover8b1240b2017-04-20 19:54:02 +00002088 bool IsImmUpdate =
2089 isPerfectIncrement(Inc, VT.getVectorElementType(), NumVecs);
2090 Ops.push_back(IsImmUpdate ? Reg0 : Inc);
Bob Wilson06fce872011-02-07 17:43:21 +00002091 }
Bob Wilson01ac8f92010-06-16 21:34:01 +00002092
Bob Wilsond5c57a52010-09-13 23:01:35 +00002093 SDValue SuperReg;
Bob Wilson06fce872011-02-07 17:43:21 +00002094 SDValue V0 = N->getOperand(Vec0Idx + 0);
2095 SDValue V1 = N->getOperand(Vec0Idx + 1);
Bob Wilsond5c57a52010-09-13 23:01:35 +00002096 if (NumVecs == 2) {
2097 if (is64BitVector)
Weiming Zhao95782222012-11-17 00:23:35 +00002098 SuperReg = SDValue(createDRegPairNode(MVT::v2i64, V0, V1), 0);
Bob Wilsond5c57a52010-09-13 23:01:35 +00002099 else
Weiming Zhao95782222012-11-17 00:23:35 +00002100 SuperReg = SDValue(createQRegPairNode(MVT::v4i64, V0, V1), 0);
Bob Wilson4145e3a2009-10-14 16:19:03 +00002101 } else {
Bob Wilson06fce872011-02-07 17:43:21 +00002102 SDValue V2 = N->getOperand(Vec0Idx + 2);
Bob Wilsond5c57a52010-09-13 23:01:35 +00002103 SDValue V3 = (NumVecs == 3)
Bob Wilson06fce872011-02-07 17:43:21 +00002104 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, VT), 0)
2105 : N->getOperand(Vec0Idx + 3);
Bob Wilsond5c57a52010-09-13 23:01:35 +00002106 if (is64BitVector)
Weiming Zhao95782222012-11-17 00:23:35 +00002107 SuperReg = SDValue(createQuadDRegsNode(MVT::v4i64, V0, V1, V2, V3), 0);
Bob Wilsond5c57a52010-09-13 23:01:35 +00002108 else
Weiming Zhao95782222012-11-17 00:23:35 +00002109 SuperReg = SDValue(createQuadQRegsNode(MVT::v8i64, V0, V1, V2, V3), 0);
Bob Wilson4145e3a2009-10-14 16:19:03 +00002110 }
Bob Wilsond5c57a52010-09-13 23:01:35 +00002111 Ops.push_back(SuperReg);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002112 Ops.push_back(getI32Imm(Lane, dl));
Evan Chenga33fc862009-11-21 06:21:52 +00002113 Ops.push_back(Pred);
Bob Wilsonae08a732010-03-20 22:13:40 +00002114 Ops.push_back(Reg0);
Bob Wilson4145e3a2009-10-14 16:19:03 +00002115 Ops.push_back(Chain);
2116
Bob Wilson06fce872011-02-07 17:43:21 +00002117 unsigned Opc = (is64BitVector ? DOpcodes[OpcodeIndex] :
2118 QOpcodes[OpcodeIndex]);
Michael Liaob53d8962013-04-19 22:22:57 +00002119 SDNode *VLdLn = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
Evan Cheng40791332011-04-19 00:04:03 +00002120 cast<MachineSDNode>(VLdLn)->setMemRefs(MemOp, MemOp + 1);
Justin Bogner45571362016-05-12 00:31:09 +00002121 if (!IsLoad) {
2122 ReplaceNode(N, VLdLn);
2123 return;
2124 }
Evan Cheng0cbd11d2010-05-15 01:36:29 +00002125
Bob Wilsond5c57a52010-09-13 23:01:35 +00002126 // Extract the subregisters.
Bob Wilson06fce872011-02-07 17:43:21 +00002127 SuperReg = SDValue(VLdLn, 0);
Benjamin Kramer3e9a5d32016-05-27 11:36:04 +00002128 static_assert(ARM::dsub_7 == ARM::dsub_0 + 7 &&
2129 ARM::qsub_3 == ARM::qsub_0 + 3,
2130 "Unexpected subreg numbering");
Bob Wilson06fce872011-02-07 17:43:21 +00002131 unsigned Sub0 = is64BitVector ? ARM::dsub_0 : ARM::qsub_0;
Bob Wilson01ac8f92010-06-16 21:34:01 +00002132 for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
2133 ReplaceUses(SDValue(N, Vec),
Bob Wilson06fce872011-02-07 17:43:21 +00002134 CurDAG->getTargetExtractSubreg(Sub0 + Vec, dl, VT, SuperReg));
2135 ReplaceUses(SDValue(N, NumVecs), SDValue(VLdLn, 1));
2136 if (isUpdating)
2137 ReplaceUses(SDValue(N, NumVecs + 1), SDValue(VLdLn, 2));
Justin Bognered4f3782016-05-12 00:20:19 +00002138 CurDAG->RemoveDeadNode(N);
Bob Wilson4145e3a2009-10-14 16:19:03 +00002139}
2140
Justin Bogner45571362016-05-12 00:31:09 +00002141void ARMDAGToDAGISel::SelectVLDDup(SDNode *N, bool isUpdating, unsigned NumVecs,
Eli Friedmanf624ec22016-12-16 18:44:08 +00002142 const uint16_t *DOpcodes,
2143 const uint16_t *QOpcodes) {
2144 assert(NumVecs >= 1 && NumVecs <= 4 && "VLDDup NumVecs out-of-range");
Andrew Trickef9de2a2013-05-25 02:42:55 +00002145 SDLoc dl(N);
Bob Wilson2d790df2010-11-28 06:51:26 +00002146
2147 SDValue MemAddr, Align;
2148 if (!SelectAddrMode6(N, N->getOperand(1), MemAddr, Align))
Justin Bogner45571362016-05-12 00:31:09 +00002149 return;
Bob Wilson2d790df2010-11-28 06:51:26 +00002150
Evan Cheng40791332011-04-19 00:04:03 +00002151 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
2152 MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
2153
Bob Wilson2d790df2010-11-28 06:51:26 +00002154 SDValue Chain = N->getOperand(0);
2155 EVT VT = N->getValueType(0);
2156
2157 unsigned Alignment = 0;
2158 if (NumVecs != 3) {
2159 Alignment = cast<ConstantSDNode>(Align)->getZExtValue();
Sanjay Patel1ed771f2016-09-14 16:37:15 +00002160 unsigned NumBytes = NumVecs * VT.getScalarSizeInBits() / 8;
Bob Wilson2d790df2010-11-28 06:51:26 +00002161 if (Alignment > NumBytes)
2162 Alignment = NumBytes;
Bob Wilsond29b38c2010-12-10 19:37:42 +00002163 if (Alignment < 8 && Alignment < NumBytes)
2164 Alignment = 0;
Bob Wilson2d790df2010-11-28 06:51:26 +00002165 // Alignment must be a power of two; make sure of that.
2166 Alignment = (Alignment & -Alignment);
2167 if (Alignment == 1)
2168 Alignment = 0;
2169 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002170 Align = CurDAG->getTargetConstant(Alignment, dl, MVT::i32);
Bob Wilson2d790df2010-11-28 06:51:26 +00002171
Eli Friedmanf624ec22016-12-16 18:44:08 +00002172 unsigned Opc;
Bob Wilson2d790df2010-11-28 06:51:26 +00002173 switch (VT.getSimpleVT().SimpleTy) {
2174 default: llvm_unreachable("unhandled vld-dup type");
Eli Friedmanf624ec22016-12-16 18:44:08 +00002175 case MVT::v8i8: Opc = DOpcodes[0]; break;
2176 case MVT::v16i8: Opc = QOpcodes[0]; break;
2177 case MVT::v4i16: Opc = DOpcodes[1]; break;
2178 case MVT::v8i16: Opc = QOpcodes[1]; break;
Bob Wilson2d790df2010-11-28 06:51:26 +00002179 case MVT::v2f32:
Eli Friedmanf624ec22016-12-16 18:44:08 +00002180 case MVT::v2i32: Opc = DOpcodes[2]; break;
2181 case MVT::v4f32:
2182 case MVT::v4i32: Opc = QOpcodes[2]; break;
Bob Wilson2d790df2010-11-28 06:51:26 +00002183 }
2184
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002185 SDValue Pred = getAL(CurDAG, dl);
Bob Wilson2d790df2010-11-28 06:51:26 +00002186 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Bob Wilson06fce872011-02-07 17:43:21 +00002187 SmallVector<SDValue, 6> Ops;
2188 Ops.push_back(MemAddr);
2189 Ops.push_back(Align);
2190 if (isUpdating) {
Jim Grosbachc80a2642011-12-21 19:40:55 +00002191 // fixed-stride update instructions don't have an explicit writeback
2192 // operand. It's implicit in the opcode itself.
Bob Wilson06fce872011-02-07 17:43:21 +00002193 SDValue Inc = N->getOperand(2);
Tim Northover8b1240b2017-04-20 19:54:02 +00002194 bool IsImmUpdate =
2195 isPerfectIncrement(Inc, VT.getVectorElementType(), NumVecs);
2196 if (NumVecs <= 2 && !IsImmUpdate)
Eli Friedmanf624ec22016-12-16 18:44:08 +00002197 Opc = getVLDSTRegisterUpdateOpcode(Opc);
Tim Northover8b1240b2017-04-20 19:54:02 +00002198 if (!IsImmUpdate)
Jim Grosbachc80a2642011-12-21 19:40:55 +00002199 Ops.push_back(Inc);
2200 // FIXME: VLD3 and VLD4 haven't been updated to that form yet.
2201 else if (NumVecs > 2)
2202 Ops.push_back(Reg0);
Bob Wilson06fce872011-02-07 17:43:21 +00002203 }
2204 Ops.push_back(Pred);
2205 Ops.push_back(Reg0);
2206 Ops.push_back(Chain);
Bob Wilson2d790df2010-11-28 06:51:26 +00002207
2208 unsigned ResTyElts = (NumVecs == 3) ? 4 : NumVecs;
Bob Wilson06fce872011-02-07 17:43:21 +00002209 std::vector<EVT> ResTys;
Evan Cheng40791332011-04-19 00:04:03 +00002210 ResTys.push_back(EVT::getVectorVT(*CurDAG->getContext(), MVT::i64,ResTyElts));
Bob Wilson06fce872011-02-07 17:43:21 +00002211 if (isUpdating)
2212 ResTys.push_back(MVT::i32);
2213 ResTys.push_back(MVT::Other);
Michael Liaob53d8962013-04-19 22:22:57 +00002214 SDNode *VLdDup = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
Evan Cheng40791332011-04-19 00:04:03 +00002215 cast<MachineSDNode>(VLdDup)->setMemRefs(MemOp, MemOp + 1);
Bob Wilson2d790df2010-11-28 06:51:26 +00002216
2217 // Extract the subregisters.
Eli Friedmanf624ec22016-12-16 18:44:08 +00002218 if (NumVecs == 1) {
2219 ReplaceUses(SDValue(N, 0), SDValue(VLdDup, 0));
2220 } else {
2221 SDValue SuperReg = SDValue(VLdDup, 0);
2222 static_assert(ARM::dsub_7 == ARM::dsub_0 + 7, "Unexpected subreg numbering");
2223 unsigned SubIdx = ARM::dsub_0;
2224 for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
2225 ReplaceUses(SDValue(N, Vec),
2226 CurDAG->getTargetExtractSubreg(SubIdx+Vec, dl, VT, SuperReg));
2227 }
Bob Wilson06fce872011-02-07 17:43:21 +00002228 ReplaceUses(SDValue(N, NumVecs), SDValue(VLdDup, 1));
2229 if (isUpdating)
2230 ReplaceUses(SDValue(N, NumVecs + 1), SDValue(VLdDup, 2));
Justin Bognered4f3782016-05-12 00:20:19 +00002231 CurDAG->RemoveDeadNode(N);
Bob Wilson2d790df2010-11-28 06:51:26 +00002232}
2233
Justin Bogner45571362016-05-12 00:31:09 +00002234bool ARMDAGToDAGISel::tryV6T2BitfieldExtractOp(SDNode *N, bool isSigned) {
Sandeep Patel423e42b2009-10-13 18:59:48 +00002235 if (!Subtarget->hasV6T2Ops())
Justin Bogner45571362016-05-12 00:31:09 +00002236 return false;
Bob Wilson93117bc2009-10-14 16:46:45 +00002237
Evan Chengeae6d2c2012-12-19 20:16:09 +00002238 unsigned Opc = isSigned
2239 ? (Subtarget->isThumb() ? ARM::t2SBFX : ARM::SBFX)
Jim Grosbach825cb292010-04-22 23:24:18 +00002240 : (Subtarget->isThumb() ? ARM::t2UBFX : ARM::UBFX);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002241 SDLoc dl(N);
Jim Grosbach825cb292010-04-22 23:24:18 +00002242
Jim Grosbach825cb292010-04-22 23:24:18 +00002243 // For unsigned extracts, check for a shift right and mask
2244 unsigned And_imm = 0;
2245 if (N->getOpcode() == ISD::AND) {
2246 if (isOpcWithIntImmediate(N, ISD::AND, And_imm)) {
2247
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002248 // The immediate is a mask of the low bits iff imm & (imm+1) == 0
Jim Grosbach825cb292010-04-22 23:24:18 +00002249 if (And_imm & (And_imm + 1))
Justin Bogner45571362016-05-12 00:31:09 +00002250 return false;
Jim Grosbach825cb292010-04-22 23:24:18 +00002251
2252 unsigned Srl_imm = 0;
2253 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SRL,
2254 Srl_imm)) {
2255 assert(Srl_imm > 0 && Srl_imm < 32 && "bad amount in shift node!");
2256
Jim Grosbach03f56d92011-07-27 21:09:25 +00002257 // Note: The width operand is encoded as width-1.
Benjamin Kramer5f6a9072015-02-12 15:35:40 +00002258 unsigned Width = countTrailingOnes(And_imm) - 1;
Jim Grosbach825cb292010-04-22 23:24:18 +00002259 unsigned LSB = Srl_imm;
Evan Chengeae6d2c2012-12-19 20:16:09 +00002260
Jim Grosbach825cb292010-04-22 23:24:18 +00002261 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Evan Chengeae6d2c2012-12-19 20:16:09 +00002262
2263 if ((LSB + Width + 1) == N->getValueType(0).getSizeInBits()) {
2264 // It's cheaper to use a right shift to extract the top bits.
2265 if (Subtarget->isThumb()) {
2266 Opc = isSigned ? ARM::t2ASRri : ARM::t2LSRri;
2267 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002268 CurDAG->getTargetConstant(LSB, dl, MVT::i32),
2269 getAL(CurDAG, dl), Reg0, Reg0 };
Justin Bogner45571362016-05-12 00:31:09 +00002270 CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops);
2271 return true;
Evan Chengeae6d2c2012-12-19 20:16:09 +00002272 }
2273
2274 // ARM models shift instructions as MOVsi with shifter operand.
2275 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(ISD::SRL);
2276 SDValue ShOpc =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002277 CurDAG->getTargetConstant(ARM_AM::getSORegOpc(ShOpcVal, LSB), dl,
Evan Chengeae6d2c2012-12-19 20:16:09 +00002278 MVT::i32);
2279 SDValue Ops[] = { N->getOperand(0).getOperand(0), ShOpc,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002280 getAL(CurDAG, dl), Reg0, Reg0 };
Justin Bogner45571362016-05-12 00:31:09 +00002281 CurDAG->SelectNodeTo(N, ARM::MOVsi, MVT::i32, Ops);
2282 return true;
Evan Chengeae6d2c2012-12-19 20:16:09 +00002283 }
2284
Jim Grosbach825cb292010-04-22 23:24:18 +00002285 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002286 CurDAG->getTargetConstant(LSB, dl, MVT::i32),
2287 CurDAG->getTargetConstant(Width, dl, MVT::i32),
2288 getAL(CurDAG, dl), Reg0 };
Justin Bogner45571362016-05-12 00:31:09 +00002289 CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops);
2290 return true;
Jim Grosbach825cb292010-04-22 23:24:18 +00002291 }
2292 }
Justin Bogner45571362016-05-12 00:31:09 +00002293 return false;
Jim Grosbach825cb292010-04-22 23:24:18 +00002294 }
2295
2296 // Otherwise, we're looking for a shift of a shift
Sandeep Patel423e42b2009-10-13 18:59:48 +00002297 unsigned Shl_imm = 0;
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002298 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SHL, Shl_imm)) {
Sandeep Patel423e42b2009-10-13 18:59:48 +00002299 assert(Shl_imm > 0 && Shl_imm < 32 && "bad amount in shift node!");
2300 unsigned Srl_imm = 0;
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002301 if (isInt32Immediate(N->getOperand(1), Srl_imm)) {
Sandeep Patel423e42b2009-10-13 18:59:48 +00002302 assert(Srl_imm > 0 && Srl_imm < 32 && "bad amount in shift node!");
Jim Grosbach03f56d92011-07-27 21:09:25 +00002303 // Note: The width operand is encoded as width-1.
2304 unsigned Width = 32 - Srl_imm - 1;
Sandeep Patel423e42b2009-10-13 18:59:48 +00002305 int LSB = Srl_imm - Shl_imm;
Evan Cheng0f55e9c2009-10-22 00:40:00 +00002306 if (LSB < 0)
Justin Bogner45571362016-05-12 00:31:09 +00002307 return false;
Sandeep Patel423e42b2009-10-13 18:59:48 +00002308 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002309 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002310 CurDAG->getTargetConstant(LSB, dl, MVT::i32),
2311 CurDAG->getTargetConstant(Width, dl, MVT::i32),
2312 getAL(CurDAG, dl), Reg0 };
Justin Bogner45571362016-05-12 00:31:09 +00002313 CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops);
2314 return true;
Sandeep Patel423e42b2009-10-13 18:59:48 +00002315 }
2316 }
Tim Northover14ff2df2014-07-23 13:59:12 +00002317
Oliver Stannard92ca83c2016-06-01 12:01:01 +00002318 // Or we are looking for a shift of an and, with a mask operand
2319 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, And_imm) &&
2320 isShiftedMask_32(And_imm)) {
2321 unsigned Srl_imm = 0;
2322 unsigned LSB = countTrailingZeros(And_imm);
2323 // Shift must be the same as the ands lsb
2324 if (isInt32Immediate(N->getOperand(1), Srl_imm) && Srl_imm == LSB) {
2325 assert(Srl_imm > 0 && Srl_imm < 32 && "bad amount in shift node!");
2326 unsigned MSB = 31 - countLeadingZeros(And_imm);
2327 // Note: The width operand is encoded as width-1.
2328 unsigned Width = MSB - LSB;
2329 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
2330 SDValue Ops[] = { N->getOperand(0).getOperand(0),
2331 CurDAG->getTargetConstant(Srl_imm, dl, MVT::i32),
2332 CurDAG->getTargetConstant(Width, dl, MVT::i32),
2333 getAL(CurDAG, dl), Reg0 };
2334 CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops);
2335 return true;
2336 }
2337 }
2338
Tim Northover14ff2df2014-07-23 13:59:12 +00002339 if (N->getOpcode() == ISD::SIGN_EXTEND_INREG) {
2340 unsigned Width = cast<VTSDNode>(N->getOperand(1))->getVT().getSizeInBits();
2341 unsigned LSB = 0;
2342 if (!isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SRL, LSB) &&
2343 !isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SRA, LSB))
Justin Bogner45571362016-05-12 00:31:09 +00002344 return false;
Tim Northover14ff2df2014-07-23 13:59:12 +00002345
2346 if (LSB + Width > 32)
Justin Bogner45571362016-05-12 00:31:09 +00002347 return false;
Tim Northover14ff2df2014-07-23 13:59:12 +00002348
2349 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
2350 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002351 CurDAG->getTargetConstant(LSB, dl, MVT::i32),
2352 CurDAG->getTargetConstant(Width - 1, dl, MVT::i32),
2353 getAL(CurDAG, dl), Reg0 };
Justin Bogner45571362016-05-12 00:31:09 +00002354 CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops);
2355 return true;
Tim Northover14ff2df2014-07-23 13:59:12 +00002356 }
2357
Justin Bogner45571362016-05-12 00:31:09 +00002358 return false;
Sandeep Patel423e42b2009-10-13 18:59:48 +00002359}
2360
Bill Wendlinga7d697e2011-10-10 22:59:55 +00002361/// Target-specific DAG combining for ISD::XOR.
2362/// Target-independent combining lowers SELECT_CC nodes of the form
2363/// select_cc setg[ge] X, 0, X, -X
2364/// select_cc setgt X, -1, X, -X
2365/// select_cc setl[te] X, 0, -X, X
2366/// select_cc setlt X, 1, -X, X
2367/// which represent Integer ABS into:
2368/// Y = sra (X, size(X)-1); xor (add (X, Y), Y)
2369/// ARM instruction selection detects the latter and matches it to
2370/// ARM::ABS or ARM::t2ABS machine node.
Justin Bogner45571362016-05-12 00:31:09 +00002371bool ARMDAGToDAGISel::tryABSOp(SDNode *N){
Bill Wendlinga7d697e2011-10-10 22:59:55 +00002372 SDValue XORSrc0 = N->getOperand(0);
2373 SDValue XORSrc1 = N->getOperand(1);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00002374 EVT VT = N->getValueType(0);
2375
Bill Wendlinga7d697e2011-10-10 22:59:55 +00002376 if (Subtarget->isThumb1Only())
Justin Bogner45571362016-05-12 00:31:09 +00002377 return false;
Bill Wendlinga7d697e2011-10-10 22:59:55 +00002378
Jim Grosbachb437a8c2012-08-01 20:33:00 +00002379 if (XORSrc0.getOpcode() != ISD::ADD || XORSrc1.getOpcode() != ISD::SRA)
Justin Bogner45571362016-05-12 00:31:09 +00002380 return false;
Bill Wendlinga7d697e2011-10-10 22:59:55 +00002381
2382 SDValue ADDSrc0 = XORSrc0.getOperand(0);
2383 SDValue ADDSrc1 = XORSrc0.getOperand(1);
2384 SDValue SRASrc0 = XORSrc1.getOperand(0);
2385 SDValue SRASrc1 = XORSrc1.getOperand(1);
2386 ConstantSDNode *SRAConstant = dyn_cast<ConstantSDNode>(SRASrc1);
2387 EVT XType = SRASrc0.getValueType();
2388 unsigned Size = XType.getSizeInBits() - 1;
2389
Jim Grosbachb437a8c2012-08-01 20:33:00 +00002390 if (ADDSrc1 == XORSrc1 && ADDSrc0 == SRASrc0 &&
Craig Topper062a2ba2014-04-25 05:30:21 +00002391 XType.isInteger() && SRAConstant != nullptr &&
Bill Wendlinga7d697e2011-10-10 22:59:55 +00002392 Size == SRAConstant->getZExtValue()) {
Jim Grosbachb437a8c2012-08-01 20:33:00 +00002393 unsigned Opcode = Subtarget->isThumb2() ? ARM::t2ABS : ARM::ABS;
Justin Bogner45571362016-05-12 00:31:09 +00002394 CurDAG->SelectNodeTo(N, Opcode, VT, ADDSrc0);
2395 return true;
Bill Wendlinga7d697e2011-10-10 22:59:55 +00002396 }
2397
Justin Bogner45571362016-05-12 00:31:09 +00002398 return false;
Bill Wendlinga7d697e2011-10-10 22:59:55 +00002399}
2400
Tim Northoverb629c772016-04-18 21:48:55 +00002401/// We've got special pseudo-instructions for these
Justin Bogner45571362016-05-12 00:31:09 +00002402void ARMDAGToDAGISel::SelectCMP_SWAP(SDNode *N) {
Tim Northoverb629c772016-04-18 21:48:55 +00002403 unsigned Opcode;
2404 EVT MemTy = cast<MemSDNode>(N)->getMemoryVT();
2405 if (MemTy == MVT::i8)
2406 Opcode = ARM::CMP_SWAP_8;
2407 else if (MemTy == MVT::i16)
2408 Opcode = ARM::CMP_SWAP_16;
2409 else if (MemTy == MVT::i32)
2410 Opcode = ARM::CMP_SWAP_32;
2411 else
2412 llvm_unreachable("Unknown AtomicCmpSwap type");
2413
2414 SDValue Ops[] = {N->getOperand(1), N->getOperand(2), N->getOperand(3),
2415 N->getOperand(0)};
2416 SDNode *CmpSwap = CurDAG->getMachineNode(
2417 Opcode, SDLoc(N),
2418 CurDAG->getVTList(MVT::i32, MVT::i32, MVT::Other), Ops);
2419
2420 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
2421 MemOp[0] = cast<MemSDNode>(N)->getMemOperand();
2422 cast<MachineSDNode>(CmpSwap)->setMemRefs(MemOp, MemOp + 1);
2423
2424 ReplaceUses(SDValue(N, 0), SDValue(CmpSwap, 0));
2425 ReplaceUses(SDValue(N, 1), SDValue(CmpSwap, 2));
Justin Bognered4f3782016-05-12 00:20:19 +00002426 CurDAG->RemoveDeadNode(N);
Tim Northoverb629c772016-04-18 21:48:55 +00002427}
2428
Sjoerd Meijer96e10b52016-12-15 09:38:59 +00002429static Optional<std::pair<unsigned, unsigned>>
2430getContiguousRangeOfSetBits(const APInt &A) {
2431 unsigned FirstOne = A.getBitWidth() - A.countLeadingZeros() - 1;
2432 unsigned LastOne = A.countTrailingZeros();
2433 if (A.countPopulation() != (FirstOne - LastOne + 1))
2434 return Optional<std::pair<unsigned,unsigned>>();
2435 return std::make_pair(FirstOne, LastOne);
2436}
2437
2438void ARMDAGToDAGISel::SelectCMPZ(SDNode *N, bool &SwitchEQNEToPLMI) {
2439 assert(N->getOpcode() == ARMISD::CMPZ);
2440 SwitchEQNEToPLMI = false;
2441
2442 if (!Subtarget->isThumb())
2443 // FIXME: Work out whether it is profitable to do this in A32 mode - LSL and
2444 // LSR don't exist as standalone instructions - they need the barrel shifter.
2445 return;
2446
2447 // select (cmpz (and X, C), #0) -> (LSLS X) or (LSRS X) or (LSRS (LSLS X))
2448 SDValue And = N->getOperand(0);
2449 if (!And->hasOneUse())
2450 return;
2451
2452 SDValue Zero = N->getOperand(1);
2453 if (!isa<ConstantSDNode>(Zero) || !cast<ConstantSDNode>(Zero)->isNullValue() ||
2454 And->getOpcode() != ISD::AND)
2455 return;
2456 SDValue X = And.getOperand(0);
2457 auto C = dyn_cast<ConstantSDNode>(And.getOperand(1));
2458
2459 if (!C || !X->hasOneUse())
2460 return;
2461 auto Range = getContiguousRangeOfSetBits(C->getAPIntValue());
2462 if (!Range)
2463 return;
2464
2465 // There are several ways to lower this:
2466 SDNode *NewN;
2467 SDLoc dl(N);
2468
2469 auto EmitShift = [&](unsigned Opc, SDValue Src, unsigned Imm) -> SDNode* {
2470 if (Subtarget->isThumb2()) {
2471 Opc = (Opc == ARM::tLSLri) ? ARM::t2LSLri : ARM::t2LSRri;
2472 SDValue Ops[] = { Src, CurDAG->getTargetConstant(Imm, dl, MVT::i32),
2473 getAL(CurDAG, dl), CurDAG->getRegister(0, MVT::i32),
2474 CurDAG->getRegister(0, MVT::i32) };
2475 return CurDAG->getMachineNode(Opc, dl, MVT::i32, Ops);
2476 } else {
2477 SDValue Ops[] = {CurDAG->getRegister(ARM::CPSR, MVT::i32), Src,
2478 CurDAG->getTargetConstant(Imm, dl, MVT::i32),
2479 getAL(CurDAG, dl), CurDAG->getRegister(0, MVT::i32)};
2480 return CurDAG->getMachineNode(Opc, dl, MVT::i32, Ops);
2481 }
2482 };
2483
2484 if (Range->second == 0) {
2485 // 1. Mask includes the LSB -> Simply shift the top N bits off
2486 NewN = EmitShift(ARM::tLSLri, X, 31 - Range->first);
2487 ReplaceNode(And.getNode(), NewN);
2488 } else if (Range->first == 31) {
2489 // 2. Mask includes the MSB -> Simply shift the bottom N bits off
2490 NewN = EmitShift(ARM::tLSRri, X, Range->second);
2491 ReplaceNode(And.getNode(), NewN);
2492 } else if (Range->first == Range->second) {
2493 // 3. Only one bit is set. We can shift this into the sign bit and use a
2494 // PL/MI comparison.
2495 NewN = EmitShift(ARM::tLSLri, X, 31 - Range->first);
2496 ReplaceNode(And.getNode(), NewN);
2497
2498 SwitchEQNEToPLMI = true;
2499 } else if (!Subtarget->hasV6T2Ops()) {
2500 // 4. Do a double shift to clear bottom and top bits, but only in
2501 // thumb-1 mode as in thumb-2 we can use UBFX.
2502 NewN = EmitShift(ARM::tLSLri, X, 31 - Range->first);
2503 NewN = EmitShift(ARM::tLSRri, SDValue(NewN, 0),
2504 Range->second + (31 - Range->first));
2505 ReplaceNode(And.getNode(), NewN);
2506 }
2507
2508}
2509
Justin Bogner45571362016-05-12 00:31:09 +00002510void ARMDAGToDAGISel::Select(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002511 SDLoc dl(N);
Evan Cheng10043e22007-01-19 07:51:42 +00002512
Tim Northover31d093c2013-09-22 08:21:56 +00002513 if (N->isMachineOpcode()) {
2514 N->setNodeId(-1);
Justin Bogner45571362016-05-12 00:31:09 +00002515 return; // Already selected.
Tim Northover31d093c2013-09-22 08:21:56 +00002516 }
Rafael Espindola4e760152006-06-12 12:28:08 +00002517
2518 switch (N->getOpcode()) {
Evan Cheng10043e22007-01-19 07:51:42 +00002519 default: break;
Justin Bogner45571362016-05-12 00:31:09 +00002520 case ISD::WRITE_REGISTER:
2521 if (tryWriteRegister(N))
2522 return;
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002523 break;
Justin Bogner45571362016-05-12 00:31:09 +00002524 case ISD::READ_REGISTER:
2525 if (tryReadRegister(N))
2526 return;
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002527 break;
Justin Bogner45571362016-05-12 00:31:09 +00002528 case ISD::INLINEASM:
2529 if (tryInlineAsm(N))
2530 return;
Weiming Zhaoc5987002013-02-14 18:10:21 +00002531 break;
Justin Bogner45571362016-05-12 00:31:09 +00002532 case ISD::XOR:
Bill Wendlinga7d697e2011-10-10 22:59:55 +00002533 // Select special operations if XOR node forms integer ABS pattern
Justin Bogner45571362016-05-12 00:31:09 +00002534 if (tryABSOp(N))
2535 return;
Bill Wendlinga7d697e2011-10-10 22:59:55 +00002536 // Other cases are autogenerated.
2537 break;
Evan Cheng10043e22007-01-19 07:51:42 +00002538 case ISD::Constant: {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002539 unsigned Val = cast<ConstantSDNode>(N)->getZExtValue();
John Brawn056e6782015-09-14 15:19:41 +00002540 // If we can't materialize the constant we need to use a literal pool
2541 if (ConstantMaterializationCost(Val) > 2) {
Eric Christopherb17140d2014-10-08 07:32:17 +00002542 SDValue CPIdx = CurDAG->getTargetConstantPool(
2543 ConstantInt::get(Type::getInt32Ty(*CurDAG->getContext()), Val),
Mehdi Amini44ede332015-07-09 02:09:04 +00002544 TLI->getPointerTy(CurDAG->getDataLayout()));
Evan Cheng1526ba52007-01-24 08:53:17 +00002545
2546 SDNode *ResNode;
Tim Northover55c625f2014-01-23 13:43:47 +00002547 if (Subtarget->isThumb()) {
Sam Parker28934482017-07-14 08:23:56 +00002548 SDValue Ops[] = {
2549 CPIdx,
2550 getAL(CurDAG, dl),
2551 CurDAG->getRegister(0, MVT::i32),
2552 CurDAG->getEntryNode()
2553 };
Jim Grosbachbfef3092010-12-15 23:52:36 +00002554 ResNode = CurDAG->getMachineNode(ARM::tLDRpci, dl, MVT::i32, MVT::Other,
Michael Liaob53d8962013-04-19 22:22:57 +00002555 Ops);
Evan Chengcd4cdd12009-07-11 06:43:01 +00002556 } else {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002557 SDValue Ops[] = {
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002558 CPIdx,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002559 CurDAG->getTargetConstant(0, dl, MVT::i32),
2560 getAL(CurDAG, dl),
Owen Anderson9f944592009-08-11 20:47:22 +00002561 CurDAG->getRegister(0, MVT::i32),
Evan Cheng1526ba52007-01-24 08:53:17 +00002562 CurDAG->getEntryNode()
2563 };
Justin Bogner45571362016-05-12 00:31:09 +00002564 ResNode = CurDAG->getMachineNode(ARM::LDRcp, dl, MVT::i32, MVT::Other,
2565 Ops);
Evan Cheng1526ba52007-01-24 08:53:17 +00002566 }
Sam Parker28934482017-07-14 08:23:56 +00002567 // Annotate the Node with memory operand information so that MachineInstr
2568 // queries work properly. This e.g. gives the register allocation the
2569 // required information for rematerialization.
2570 MachineFunction& MF = CurDAG->getMachineFunction();
2571 MachineSDNode::mmo_iterator MemOp = MF.allocateMemRefsArray(1);
2572 MemOp[0] = MF.getMachineMemOperand(
2573 MachinePointerInfo::getConstantPool(MF),
2574 MachineMemOperand::MOLoad, 4, 4);
2575
2576 cast<MachineSDNode>(ResNode)->setMemRefs(MemOp, MemOp+1);
2577
Justin Bognered4f3782016-05-12 00:20:19 +00002578 ReplaceNode(N, ResNode);
Justin Bogner45571362016-05-12 00:31:09 +00002579 return;
Evan Cheng10043e22007-01-19 07:51:42 +00002580 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002581
Evan Cheng10043e22007-01-19 07:51:42 +00002582 // Other cases are autogenerated.
Rafael Espindola4e760152006-06-12 12:28:08 +00002583 break;
Evan Cheng10043e22007-01-19 07:51:42 +00002584 }
Rafael Espindola5f7ab1b2006-11-09 13:58:55 +00002585 case ISD::FrameIndex: {
Evan Cheng10043e22007-01-19 07:51:42 +00002586 // Selects to ADDri FI, 0 which in turn will become ADDri SP, imm.
Rafael Espindola5f7ab1b2006-11-09 13:58:55 +00002587 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Mehdi Amini44ede332015-07-09 02:09:04 +00002588 SDValue TFI = CurDAG->getTargetFrameIndex(
2589 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
David Goodwin22c2fba2009-07-08 23:10:31 +00002590 if (Subtarget->isThumb1Only()) {
Renato Golinb9887ef2015-02-25 14:41:06 +00002591 // Set the alignment of the frame object to 4, to avoid having to generate
2592 // more than one ADD
Matthias Braun941a7052016-07-28 18:40:00 +00002593 MachineFrameInfo &MFI = MF->getFrameInfo();
2594 if (MFI.getObjectAlignment(FI) < 4)
2595 MFI.setObjectAlignment(FI, 4);
Justin Bogner45571362016-05-12 00:31:09 +00002596 CurDAG->SelectNodeTo(N, ARM::tADDframe, MVT::i32, TFI,
2597 CurDAG->getTargetConstant(0, dl, MVT::i32));
2598 return;
Jim Grosbachfde21102009-04-07 20:34:09 +00002599 } else {
David Goodwin4ad77972009-07-14 18:48:51 +00002600 unsigned Opc = ((Subtarget->isThumb() && Subtarget->hasThumb2()) ?
2601 ARM::t2ADDri : ARM::ADDri);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002602 SDValue Ops[] = { TFI, CurDAG->getTargetConstant(0, dl, MVT::i32),
2603 getAL(CurDAG, dl), CurDAG->getRegister(0, MVT::i32),
Owen Anderson9f944592009-08-11 20:47:22 +00002604 CurDAG->getRegister(0, MVT::i32) };
Justin Bogner45571362016-05-12 00:31:09 +00002605 CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops);
2606 return;
Evan Cheng7e90b112007-07-05 07:15:27 +00002607 }
Evan Cheng10043e22007-01-19 07:51:42 +00002608 }
Sandeep Patel423e42b2009-10-13 18:59:48 +00002609 case ISD::SRL:
Justin Bogner45571362016-05-12 00:31:09 +00002610 if (tryV6T2BitfieldExtractOp(N, false))
2611 return;
Sandeep Patel423e42b2009-10-13 18:59:48 +00002612 break;
Tim Northover14ff2df2014-07-23 13:59:12 +00002613 case ISD::SIGN_EXTEND_INREG:
Sandeep Patel423e42b2009-10-13 18:59:48 +00002614 case ISD::SRA:
Justin Bogner45571362016-05-12 00:31:09 +00002615 if (tryV6T2BitfieldExtractOp(N, true))
2616 return;
Sandeep Patel423e42b2009-10-13 18:59:48 +00002617 break;
Evan Cheng10043e22007-01-19 07:51:42 +00002618 case ISD::MUL:
Evan Chengb24e51e2009-07-07 01:17:28 +00002619 if (Subtarget->isThumb1Only())
Evan Cheng139edae2007-01-24 02:21:22 +00002620 break;
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002621 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002622 unsigned RHSV = C->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +00002623 if (!RHSV) break;
2624 if (isPowerOf2_32(RHSV-1)) { // 2^n+1?
Evan Cheng0d8b0cf2009-07-21 00:31:12 +00002625 unsigned ShImm = Log2_32(RHSV-1);
2626 if (ShImm >= 32)
2627 break;
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002628 SDValue V = N->getOperand(0);
Evan Cheng0d8b0cf2009-07-21 00:31:12 +00002629 ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, ShImm);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002630 SDValue ShImmOp = CurDAG->getTargetConstant(ShImm, dl, MVT::i32);
Owen Anderson9f944592009-08-11 20:47:22 +00002631 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Evan Cheng1ec43962009-07-22 18:08:05 +00002632 if (Subtarget->isThumb()) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002633 SDValue Ops[] = { V, V, ShImmOp, getAL(CurDAG, dl), Reg0, Reg0 };
Justin Bogner45571362016-05-12 00:31:09 +00002634 CurDAG->SelectNodeTo(N, ARM::t2ADDrs, MVT::i32, Ops);
2635 return;
Evan Cheng0d8b0cf2009-07-21 00:31:12 +00002636 } else {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002637 SDValue Ops[] = { V, V, Reg0, ShImmOp, getAL(CurDAG, dl), Reg0,
2638 Reg0 };
Justin Bogner45571362016-05-12 00:31:09 +00002639 CurDAG->SelectNodeTo(N, ARM::ADDrsi, MVT::i32, Ops);
2640 return;
Evan Cheng0d8b0cf2009-07-21 00:31:12 +00002641 }
Evan Cheng10043e22007-01-19 07:51:42 +00002642 }
2643 if (isPowerOf2_32(RHSV+1)) { // 2^n-1?
Evan Cheng0d8b0cf2009-07-21 00:31:12 +00002644 unsigned ShImm = Log2_32(RHSV+1);
2645 if (ShImm >= 32)
2646 break;
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002647 SDValue V = N->getOperand(0);
Evan Cheng0d8b0cf2009-07-21 00:31:12 +00002648 ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, ShImm);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002649 SDValue ShImmOp = CurDAG->getTargetConstant(ShImm, dl, MVT::i32);
Owen Anderson9f944592009-08-11 20:47:22 +00002650 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
Evan Cheng1ec43962009-07-22 18:08:05 +00002651 if (Subtarget->isThumb()) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002652 SDValue Ops[] = { V, V, ShImmOp, getAL(CurDAG, dl), Reg0, Reg0 };
Justin Bogner45571362016-05-12 00:31:09 +00002653 CurDAG->SelectNodeTo(N, ARM::t2RSBrs, MVT::i32, Ops);
2654 return;
Evan Cheng0d8b0cf2009-07-21 00:31:12 +00002655 } else {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002656 SDValue Ops[] = { V, V, Reg0, ShImmOp, getAL(CurDAG, dl), Reg0,
2657 Reg0 };
Justin Bogner45571362016-05-12 00:31:09 +00002658 CurDAG->SelectNodeTo(N, ARM::RSBrsi, MVT::i32, Ops);
2659 return;
Evan Cheng0d8b0cf2009-07-21 00:31:12 +00002660 }
Evan Cheng10043e22007-01-19 07:51:42 +00002661 }
2662 }
2663 break;
Evan Cheng786b15f2009-10-21 08:15:52 +00002664 case ISD::AND: {
Jim Grosbach825cb292010-04-22 23:24:18 +00002665 // Check for unsigned bitfield extract
Justin Bogner45571362016-05-12 00:31:09 +00002666 if (tryV6T2BitfieldExtractOp(N, false))
2667 return;
Jim Grosbach825cb292010-04-22 23:24:18 +00002668
James Molloyae5ff992016-07-05 12:37:13 +00002669 // If an immediate is used in an AND node, it is possible that the immediate
2670 // can be more optimally materialized when negated. If this is the case we
2671 // can negate the immediate and use a BIC instead.
2672 auto *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1));
2673 if (N1C && N1C->hasOneUse() && Subtarget->isThumb()) {
2674 uint32_t Imm = (uint32_t) N1C->getZExtValue();
2675
2676 // In Thumb2 mode, an AND can take a 12-bit immediate. If this
2677 // immediate can be negated and fit in the immediate operand of
2678 // a t2BIC, don't do any manual transform here as this can be
2679 // handled by the generic ISel machinery.
2680 bool PreferImmediateEncoding =
2681 Subtarget->hasThumb2() && (is_t2_so_imm(Imm) || is_t2_so_imm_not(Imm));
2682 if (!PreferImmediateEncoding &&
2683 ConstantMaterializationCost(Imm) >
2684 ConstantMaterializationCost(~Imm)) {
2685 // The current immediate costs more to materialize than a negated
2686 // immediate, so negate the immediate and use a BIC.
2687 SDValue NewImm =
2688 CurDAG->getConstant(~N1C->getZExtValue(), dl, MVT::i32);
2689 // If the new constant didn't exist before, reposition it in the topological
2690 // ordering so it is just before N. Otherwise, don't touch its location.
2691 if (NewImm->getNodeId() == -1)
2692 CurDAG->RepositionNode(N->getIterator(), NewImm.getNode());
2693
2694 if (!Subtarget->hasThumb2()) {
2695 SDValue Ops[] = {CurDAG->getRegister(ARM::CPSR, MVT::i32),
2696 N->getOperand(0), NewImm, getAL(CurDAG, dl),
2697 CurDAG->getRegister(0, MVT::i32)};
2698 ReplaceNode(N, CurDAG->getMachineNode(ARM::tBIC, dl, MVT::i32, Ops));
2699 return;
2700 } else {
2701 SDValue Ops[] = {N->getOperand(0), NewImm, getAL(CurDAG, dl),
2702 CurDAG->getRegister(0, MVT::i32),
2703 CurDAG->getRegister(0, MVT::i32)};
2704 ReplaceNode(N,
2705 CurDAG->getMachineNode(ARM::t2BICrr, dl, MVT::i32, Ops));
2706 return;
2707 }
2708 }
2709 }
2710
Evan Cheng786b15f2009-10-21 08:15:52 +00002711 // (and (or x, c2), c1) and top 16-bits of c1 and c2 match, lower 16-bits
2712 // of c1 are 0xffff, and lower 16-bit of c2 are 0. That is, the top 16-bits
2713 // are entirely contributed by c2 and lower 16-bits are entirely contributed
2714 // by x. That's equal to (or (and x, 0xffff), (and c1, 0xffff0000)).
2715 // Select it to: "movt x, ((c1 & 0xffff) >> 16)
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002716 EVT VT = N->getValueType(0);
Evan Cheng786b15f2009-10-21 08:15:52 +00002717 if (VT != MVT::i32)
2718 break;
2719 unsigned Opc = (Subtarget->isThumb() && Subtarget->hasThumb2())
2720 ? ARM::t2MOVTi16
2721 : (Subtarget->hasV6T2Ops() ? ARM::MOVTi16 : 0);
2722 if (!Opc)
2723 break;
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002724 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
James Molloyae5ff992016-07-05 12:37:13 +00002725 N1C = dyn_cast<ConstantSDNode>(N1);
Evan Cheng786b15f2009-10-21 08:15:52 +00002726 if (!N1C)
2727 break;
2728 if (N0.getOpcode() == ISD::OR && N0.getNode()->hasOneUse()) {
2729 SDValue N2 = N0.getOperand(1);
2730 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
2731 if (!N2C)
2732 break;
2733 unsigned N1CVal = N1C->getZExtValue();
2734 unsigned N2CVal = N2C->getZExtValue();
2735 if ((N1CVal & 0xffff0000U) == (N2CVal & 0xffff0000U) &&
2736 (N1CVal & 0xffffU) == 0xffffU &&
2737 (N2CVal & 0xffffU) == 0x0U) {
2738 SDValue Imm16 = CurDAG->getTargetConstant((N2CVal & 0xFFFF0000U) >> 16,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002739 dl, MVT::i32);
Evan Cheng786b15f2009-10-21 08:15:52 +00002740 SDValue Ops[] = { N0.getOperand(0), Imm16,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002741 getAL(CurDAG, dl), CurDAG->getRegister(0, MVT::i32) };
Justin Bogner45571362016-05-12 00:31:09 +00002742 ReplaceNode(N, CurDAG->getMachineNode(Opc, dl, VT, Ops));
2743 return;
Evan Cheng786b15f2009-10-21 08:15:52 +00002744 }
2745 }
Sjoerd Meijer96e10b52016-12-15 09:38:59 +00002746
Evan Cheng786b15f2009-10-21 08:15:52 +00002747 break;
2748 }
Sam Parkerd616cf02016-06-20 16:47:09 +00002749 case ARMISD::UMAAL: {
2750 unsigned Opc = Subtarget->isThumb() ? ARM::t2UMAAL : ARM::UMAAL;
2751 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
2752 N->getOperand(2), N->getOperand(3),
2753 getAL(CurDAG, dl),
2754 CurDAG->getRegister(0, MVT::i32) };
2755 ReplaceNode(N, CurDAG->getMachineNode(Opc, dl, MVT::i32, MVT::i32, Ops));
2756 return;
2757 }
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00002758 case ARMISD::UMLAL:{
2759 if (Subtarget->isThumb()) {
2760 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), N->getOperand(2),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002761 N->getOperand(3), getAL(CurDAG, dl),
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00002762 CurDAG->getRegister(0, MVT::i32)};
Justin Bogner45571362016-05-12 00:31:09 +00002763 ReplaceNode(
2764 N, CurDAG->getMachineNode(ARM::t2UMLAL, dl, MVT::i32, MVT::i32, Ops));
2765 return;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00002766 }else{
2767 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), N->getOperand(2),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002768 N->getOperand(3), getAL(CurDAG, dl),
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00002769 CurDAG->getRegister(0, MVT::i32),
2770 CurDAG->getRegister(0, MVT::i32) };
Justin Bogner45571362016-05-12 00:31:09 +00002771 ReplaceNode(N, CurDAG->getMachineNode(
2772 Subtarget->hasV6Ops() ? ARM::UMLAL : ARM::UMLALv5, dl,
2773 MVT::i32, MVT::i32, Ops));
2774 return;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00002775 }
2776 }
2777 case ARMISD::SMLAL:{
2778 if (Subtarget->isThumb()) {
2779 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), N->getOperand(2),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002780 N->getOperand(3), getAL(CurDAG, dl),
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00002781 CurDAG->getRegister(0, MVT::i32)};
Justin Bogner45571362016-05-12 00:31:09 +00002782 ReplaceNode(
2783 N, CurDAG->getMachineNode(ARM::t2SMLAL, dl, MVT::i32, MVT::i32, Ops));
2784 return;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00002785 }else{
2786 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), N->getOperand(2),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002787 N->getOperand(3), getAL(CurDAG, dl),
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00002788 CurDAG->getRegister(0, MVT::i32),
2789 CurDAG->getRegister(0, MVT::i32) };
Justin Bogner45571362016-05-12 00:31:09 +00002790 ReplaceNode(N, CurDAG->getMachineNode(
2791 Subtarget->hasV6Ops() ? ARM::SMLAL : ARM::SMLALv5, dl,
2792 MVT::i32, MVT::i32, Ops));
2793 return;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00002794 }
2795 }
Sam Parker68c71cd2016-07-25 09:20:20 +00002796 case ARMISD::SUBE: {
Andre Vieira26b9de92018-01-12 09:21:09 +00002797 if (!Subtarget->hasV6Ops() || !Subtarget->hasDSP())
Sam Parker68c71cd2016-07-25 09:20:20 +00002798 break;
2799 // Look for a pattern to match SMMLS
2800 // (sube a, (smul_loHi a, b), (subc 0, (smul_LOhi(a, b))))
2801 if (N->getOperand(1).getOpcode() != ISD::SMUL_LOHI ||
Tim Northover765777c2016-08-02 23:12:36 +00002802 N->getOperand(2).getOpcode() != ARMISD::SUBC ||
2803 !SDValue(N, 1).use_empty())
Sam Parker68c71cd2016-07-25 09:20:20 +00002804 break;
2805
2806 if (Subtarget->isThumb())
2807 assert(Subtarget->hasThumb2() &&
2808 "This pattern should not be generated for Thumb");
2809
2810 SDValue SmulLoHi = N->getOperand(1);
2811 SDValue Subc = N->getOperand(2);
2812 auto *Zero = dyn_cast<ConstantSDNode>(Subc.getOperand(0));
2813
2814 if (!Zero || Zero->getZExtValue() != 0 ||
2815 Subc.getOperand(1) != SmulLoHi.getValue(0) ||
2816 N->getOperand(1) != SmulLoHi.getValue(1) ||
2817 N->getOperand(2) != Subc.getValue(1))
2818 break;
2819
2820 unsigned Opc = Subtarget->isThumb2() ? ARM::t2SMMLS : ARM::SMMLS;
2821 SDValue Ops[] = { SmulLoHi.getOperand(0), SmulLoHi.getOperand(1),
2822 N->getOperand(0), getAL(CurDAG, dl),
2823 CurDAG->getRegister(0, MVT::i32) };
2824 ReplaceNode(N, CurDAG->getMachineNode(Opc, dl, MVT::i32, Ops));
2825 return;
2826 }
Evan Cheng10043e22007-01-19 07:51:42 +00002827 case ISD::LOAD: {
Justin Bogner45571362016-05-12 00:31:09 +00002828 if (Subtarget->isThumb() && Subtarget->hasThumb2()) {
2829 if (tryT2IndexedLoad(N))
2830 return;
James Molloyb3326df2016-07-15 08:03:56 +00002831 } else if (Subtarget->isThumb()) {
2832 if (tryT1IndexedLoad(N))
2833 return;
Justin Bogner45571362016-05-12 00:31:09 +00002834 } else if (tryARMIndexedLoad(N))
2835 return;
Evan Cheng10043e22007-01-19 07:51:42 +00002836 // Other cases are autogenerated.
Rafael Espindola5f7ab1b2006-11-09 13:58:55 +00002837 break;
Rafael Espindola4e760152006-06-12 12:28:08 +00002838 }
Evan Cheng7e90b112007-07-05 07:15:27 +00002839 case ARMISD::BRCOND: {
2840 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
2841 // Emits: (Bcc:void (bb:Other):$dst, (imm:i32):$cc)
2842 // Pattern complexity = 6 cost = 1 size = 0
Rafael Espindolaffdc24b2006-05-14 22:18:28 +00002843
Evan Cheng7e90b112007-07-05 07:15:27 +00002844 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
2845 // Emits: (tBcc:void (bb:Other):$dst, (imm:i32):$cc)
2846 // Pattern complexity = 6 cost = 1 size = 0
2847
David Goodwin27303cd2009-06-30 18:04:13 +00002848 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
2849 // Emits: (t2Bcc:void (bb:Other):$dst, (imm:i32):$cc)
2850 // Pattern complexity = 6 cost = 1 size = 0
2851
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002852 unsigned Opc = Subtarget->isThumb() ?
David Goodwin27303cd2009-06-30 18:04:13 +00002853 ((Subtarget->hasThumb2()) ? ARM::t2Bcc : ARM::tBcc) : ARM::Bcc;
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002854 SDValue Chain = N->getOperand(0);
2855 SDValue N1 = N->getOperand(1);
2856 SDValue N2 = N->getOperand(2);
2857 SDValue N3 = N->getOperand(3);
2858 SDValue InFlag = N->getOperand(4);
Evan Cheng7e90b112007-07-05 07:15:27 +00002859 assert(N1.getOpcode() == ISD::BasicBlock);
2860 assert(N2.getOpcode() == ISD::Constant);
2861 assert(N3.getOpcode() == ISD::Register);
2862
Sjoerd Meijer96e10b52016-12-15 09:38:59 +00002863 unsigned CC = (unsigned) cast<ConstantSDNode>(N2)->getZExtValue();
2864
2865 if (InFlag.getOpcode() == ARMISD::CMPZ) {
2866 bool SwitchEQNEToPLMI;
2867 SelectCMPZ(InFlag.getNode(), SwitchEQNEToPLMI);
2868 InFlag = N->getOperand(4);
2869
2870 if (SwitchEQNEToPLMI) {
2871 switch ((ARMCC::CondCodes)CC) {
2872 default: llvm_unreachable("CMPZ must be either NE or EQ!");
2873 case ARMCC::NE:
2874 CC = (unsigned)ARMCC::MI;
2875 break;
2876 case ARMCC::EQ:
2877 CC = (unsigned)ARMCC::PL;
2878 break;
2879 }
2880 }
2881 }
2882
2883 SDValue Tmp2 = CurDAG->getTargetConstant(CC, dl, MVT::i32);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002884 SDValue Ops[] = { N1, Tmp2, N3, Chain, InFlag };
Dan Gohman32f71d72009-09-25 18:54:59 +00002885 SDNode *ResNode = CurDAG->getMachineNode(Opc, dl, MVT::Other,
Michael Liaob53d8962013-04-19 22:22:57 +00002886 MVT::Glue, Ops);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002887 Chain = SDValue(ResNode, 0);
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002888 if (N->getNumValues() == 2) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002889 InFlag = SDValue(ResNode, 1);
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002890 ReplaceUses(SDValue(N, 1), InFlag);
Chris Lattnere99faac2008-02-03 03:20:59 +00002891 }
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002892 ReplaceUses(SDValue(N, 0),
Evan Cheng82adca82009-11-19 08:16:50 +00002893 SDValue(Chain.getNode(), Chain.getResNo()));
Justin Bognered4f3782016-05-12 00:20:19 +00002894 CurDAG->RemoveDeadNode(N);
Justin Bogner45571362016-05-12 00:31:09 +00002895 return;
Evan Cheng7e90b112007-07-05 07:15:27 +00002896 }
James Molloy4d86bed2016-09-09 12:52:24 +00002897
2898 case ARMISD::CMPZ: {
2899 // select (CMPZ X, #-C) -> (CMPZ (ADDS X, #C), #0)
2900 // This allows us to avoid materializing the expensive negative constant.
2901 // The CMPZ #0 is useless and will be peepholed away but we need to keep it
2902 // for its glue output.
2903 SDValue X = N->getOperand(0);
2904 auto *C = dyn_cast<ConstantSDNode>(N->getOperand(1).getNode());
2905 if (C && C->getSExtValue() < 0 && Subtarget->isThumb()) {
2906 int64_t Addend = -C->getSExtValue();
2907
2908 SDNode *Add = nullptr;
Artyom Skrobov4592f622017-02-17 18:59:16 +00002909 // ADDS can be better than CMN if the immediate fits in a
James Molloy4d86bed2016-09-09 12:52:24 +00002910 // 16-bit ADDS, which means either [0,256) for tADDi8 or [0,8) for tADDi3.
2911 // Outside that range we can just use a CMN which is 32-bit but has a
2912 // 12-bit immediate range.
Artyom Skrobov4592f622017-02-17 18:59:16 +00002913 if (Addend < 1<<8) {
2914 if (Subtarget->isThumb2()) {
2915 SDValue Ops[] = { X, CurDAG->getTargetConstant(Addend, dl, MVT::i32),
2916 getAL(CurDAG, dl), CurDAG->getRegister(0, MVT::i32),
2917 CurDAG->getRegister(0, MVT::i32) };
2918 Add = CurDAG->getMachineNode(ARM::t2ADDri, dl, MVT::i32, Ops);
2919 } else {
2920 unsigned Opc = (Addend < 1<<3) ? ARM::tADDi3 : ARM::tADDi8;
2921 SDValue Ops[] = {CurDAG->getRegister(ARM::CPSR, MVT::i32), X,
2922 CurDAG->getTargetConstant(Addend, dl, MVT::i32),
2923 getAL(CurDAG, dl), CurDAG->getRegister(0, MVT::i32)};
2924 Add = CurDAG->getMachineNode(Opc, dl, MVT::i32, Ops);
2925 }
James Molloy4d86bed2016-09-09 12:52:24 +00002926 }
2927 if (Add) {
2928 SDValue Ops2[] = {SDValue(Add, 0), CurDAG->getConstant(0, dl, MVT::i32)};
2929 CurDAG->MorphNodeTo(N, ARMISD::CMPZ, CurDAG->getVTList(MVT::Glue), Ops2);
2930 }
2931 }
2932 // Other cases are autogenerated.
2933 break;
2934 }
Sjoerd Meijer96e10b52016-12-15 09:38:59 +00002935
2936 case ARMISD::CMOV: {
2937 SDValue InFlag = N->getOperand(4);
2938
2939 if (InFlag.getOpcode() == ARMISD::CMPZ) {
2940 bool SwitchEQNEToPLMI;
2941 SelectCMPZ(InFlag.getNode(), SwitchEQNEToPLMI);
2942
2943 if (SwitchEQNEToPLMI) {
2944 SDValue ARMcc = N->getOperand(2);
2945 ARMCC::CondCodes CC =
2946 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
2947
2948 switch (CC) {
2949 default: llvm_unreachable("CMPZ must be either NE or EQ!");
2950 case ARMCC::NE:
2951 CC = ARMCC::MI;
2952 break;
2953 case ARMCC::EQ:
2954 CC = ARMCC::PL;
2955 break;
2956 }
2957 SDValue NewARMcc = CurDAG->getConstant((unsigned)CC, dl, MVT::i32);
2958 SDValue Ops[] = {N->getOperand(0), N->getOperand(1), NewARMcc,
2959 N->getOperand(3), N->getOperand(4)};
2960 CurDAG->MorphNodeTo(N, ARMISD::CMOV, N->getVTList(), Ops);
2961 }
2962
2963 }
2964 // Other cases are autogenerated.
2965 break;
2966 }
James Molloy4d86bed2016-09-09 12:52:24 +00002967
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00002968 case ARMISD::VZIP: {
2969 unsigned Opc = 0;
Anton Korobeynikovce3ff1b2009-08-21 12:40:50 +00002970 EVT VT = N->getValueType(0);
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00002971 switch (VT.getSimpleVT().SimpleTy) {
Justin Bogner45571362016-05-12 00:31:09 +00002972 default: return;
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00002973 case MVT::v8i8: Opc = ARM::VZIPd8; break;
2974 case MVT::v4i16: Opc = ARM::VZIPd16; break;
2975 case MVT::v2f32:
Jim Grosbach4640c812012-04-11 16:53:25 +00002976 // vzip.32 Dd, Dm is a pseudo-instruction expanded to vtrn.32 Dd, Dm.
2977 case MVT::v2i32: Opc = ARM::VTRNd32; break;
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00002978 case MVT::v16i8: Opc = ARM::VZIPq8; break;
2979 case MVT::v8i16: Opc = ARM::VZIPq16; break;
2980 case MVT::v4f32:
2981 case MVT::v4i32: Opc = ARM::VZIPq32; break;
2982 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002983 SDValue Pred = getAL(CurDAG, dl);
Evan Chenga33fc862009-11-21 06:21:52 +00002984 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
2985 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
Justin Bogner45571362016-05-12 00:31:09 +00002986 ReplaceNode(N, CurDAG->getMachineNode(Opc, dl, VT, VT, Ops));
2987 return;
Anton Korobeynikovce3ff1b2009-08-21 12:40:50 +00002988 }
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00002989 case ARMISD::VUZP: {
2990 unsigned Opc = 0;
Anton Korobeynikovce3ff1b2009-08-21 12:40:50 +00002991 EVT VT = N->getValueType(0);
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00002992 switch (VT.getSimpleVT().SimpleTy) {
Justin Bogner45571362016-05-12 00:31:09 +00002993 default: return;
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00002994 case MVT::v8i8: Opc = ARM::VUZPd8; break;
2995 case MVT::v4i16: Opc = ARM::VUZPd16; break;
2996 case MVT::v2f32:
Jim Grosbach6e536de2012-04-11 17:40:18 +00002997 // vuzp.32 Dd, Dm is a pseudo-instruction expanded to vtrn.32 Dd, Dm.
2998 case MVT::v2i32: Opc = ARM::VTRNd32; break;
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00002999 case MVT::v16i8: Opc = ARM::VUZPq8; break;
3000 case MVT::v8i16: Opc = ARM::VUZPq16; break;
3001 case MVT::v4f32:
3002 case MVT::v4i32: Opc = ARM::VUZPq32; break;
3003 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003004 SDValue Pred = getAL(CurDAG, dl);
Evan Chenga33fc862009-11-21 06:21:52 +00003005 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
3006 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
Justin Bogner45571362016-05-12 00:31:09 +00003007 ReplaceNode(N, CurDAG->getMachineNode(Opc, dl, VT, VT, Ops));
3008 return;
Anton Korobeynikovce3ff1b2009-08-21 12:40:50 +00003009 }
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00003010 case ARMISD::VTRN: {
3011 unsigned Opc = 0;
Anton Korobeynikovce3ff1b2009-08-21 12:40:50 +00003012 EVT VT = N->getValueType(0);
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00003013 switch (VT.getSimpleVT().SimpleTy) {
Justin Bogner45571362016-05-12 00:31:09 +00003014 default: return;
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00003015 case MVT::v8i8: Opc = ARM::VTRNd8; break;
3016 case MVT::v4i16: Opc = ARM::VTRNd16; break;
3017 case MVT::v2f32:
3018 case MVT::v2i32: Opc = ARM::VTRNd32; break;
3019 case MVT::v16i8: Opc = ARM::VTRNq8; break;
3020 case MVT::v8i16: Opc = ARM::VTRNq16; break;
3021 case MVT::v4f32:
3022 case MVT::v4i32: Opc = ARM::VTRNq32; break;
3023 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003024 SDValue Pred = getAL(CurDAG, dl);
Evan Chenga33fc862009-11-21 06:21:52 +00003025 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
3026 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
Justin Bogner45571362016-05-12 00:31:09 +00003027 ReplaceNode(N, CurDAG->getMachineNode(Opc, dl, VT, VT, Ops));
3028 return;
Anton Korobeynikovce3ff1b2009-08-21 12:40:50 +00003029 }
Bob Wilsond8a9a042010-06-04 00:04:02 +00003030 case ARMISD::BUILD_VECTOR: {
3031 EVT VecVT = N->getValueType(0);
3032 EVT EltVT = VecVT.getVectorElementType();
3033 unsigned NumElts = VecVT.getVectorNumElements();
Duncan Sands14627772010-11-03 12:17:33 +00003034 if (EltVT == MVT::f64) {
Bob Wilsond8a9a042010-06-04 00:04:02 +00003035 assert(NumElts == 2 && "unexpected type for BUILD_VECTOR");
Justin Bogner45571362016-05-12 00:31:09 +00003036 ReplaceNode(
3037 N, createDRegPairNode(VecVT, N->getOperand(0), N->getOperand(1)));
3038 return;
Bob Wilsond8a9a042010-06-04 00:04:02 +00003039 }
Duncan Sands14627772010-11-03 12:17:33 +00003040 assert(EltVT == MVT::f32 && "unexpected type for BUILD_VECTOR");
Justin Bogner45571362016-05-12 00:31:09 +00003041 if (NumElts == 2) {
3042 ReplaceNode(
3043 N, createSRegPairNode(VecVT, N->getOperand(0), N->getOperand(1)));
3044 return;
3045 }
Bob Wilsond8a9a042010-06-04 00:04:02 +00003046 assert(NumElts == 4 && "unexpected type for BUILD_VECTOR");
Justin Bogner45571362016-05-12 00:31:09 +00003047 ReplaceNode(N,
3048 createQuadSRegsNode(VecVT, N->getOperand(0), N->getOperand(1),
3049 N->getOperand(2), N->getOperand(3)));
3050 return;
Bob Wilsond8a9a042010-06-04 00:04:02 +00003051 }
Bob Wilsone0636a72009-08-26 17:39:53 +00003052
Eli Friedmanf624ec22016-12-16 18:44:08 +00003053 case ARMISD::VLD1DUP: {
3054 static const uint16_t DOpcodes[] = { ARM::VLD1DUPd8, ARM::VLD1DUPd16,
3055 ARM::VLD1DUPd32 };
3056 static const uint16_t QOpcodes[] = { ARM::VLD1DUPq8, ARM::VLD1DUPq16,
3057 ARM::VLD1DUPq32 };
3058 SelectVLDDup(N, false, 1, DOpcodes, QOpcodes);
3059 return;
3060 }
3061
Bob Wilson2d790df2010-11-28 06:51:26 +00003062 case ARMISD::VLD2DUP: {
Craig Topper01736f82012-05-24 05:17:00 +00003063 static const uint16_t Opcodes[] = { ARM::VLD2DUPd8, ARM::VLD2DUPd16,
3064 ARM::VLD2DUPd32 };
Justin Bogner45571362016-05-12 00:31:09 +00003065 SelectVLDDup(N, false, 2, Opcodes);
3066 return;
Bob Wilson2d790df2010-11-28 06:51:26 +00003067 }
3068
Bob Wilson77ab1652010-11-29 19:35:29 +00003069 case ARMISD::VLD3DUP: {
Craig Topper01736f82012-05-24 05:17:00 +00003070 static const uint16_t Opcodes[] = { ARM::VLD3DUPd8Pseudo,
3071 ARM::VLD3DUPd16Pseudo,
3072 ARM::VLD3DUPd32Pseudo };
Justin Bogner45571362016-05-12 00:31:09 +00003073 SelectVLDDup(N, false, 3, Opcodes);
3074 return;
Bob Wilson77ab1652010-11-29 19:35:29 +00003075 }
3076
Bob Wilson431ac4ef2010-11-30 00:00:35 +00003077 case ARMISD::VLD4DUP: {
Craig Topper01736f82012-05-24 05:17:00 +00003078 static const uint16_t Opcodes[] = { ARM::VLD4DUPd8Pseudo,
3079 ARM::VLD4DUPd16Pseudo,
3080 ARM::VLD4DUPd32Pseudo };
Justin Bogner45571362016-05-12 00:31:09 +00003081 SelectVLDDup(N, false, 4, Opcodes);
3082 return;
Bob Wilson06fce872011-02-07 17:43:21 +00003083 }
3084
Eli Friedmanf624ec22016-12-16 18:44:08 +00003085 case ARMISD::VLD1DUP_UPD: {
3086 static const uint16_t DOpcodes[] = { ARM::VLD1DUPd8wb_fixed,
3087 ARM::VLD1DUPd16wb_fixed,
3088 ARM::VLD1DUPd32wb_fixed };
3089 static const uint16_t QOpcodes[] = { ARM::VLD1DUPq8wb_fixed,
3090 ARM::VLD1DUPq16wb_fixed,
3091 ARM::VLD1DUPq32wb_fixed };
3092 SelectVLDDup(N, true, 1, DOpcodes, QOpcodes);
3093 return;
3094 }
3095
Bob Wilson06fce872011-02-07 17:43:21 +00003096 case ARMISD::VLD2DUP_UPD: {
Craig Topper01736f82012-05-24 05:17:00 +00003097 static const uint16_t Opcodes[] = { ARM::VLD2DUPd8wb_fixed,
3098 ARM::VLD2DUPd16wb_fixed,
3099 ARM::VLD2DUPd32wb_fixed };
Justin Bogner45571362016-05-12 00:31:09 +00003100 SelectVLDDup(N, true, 2, Opcodes);
3101 return;
Bob Wilson06fce872011-02-07 17:43:21 +00003102 }
3103
3104 case ARMISD::VLD3DUP_UPD: {
Craig Topper01736f82012-05-24 05:17:00 +00003105 static const uint16_t Opcodes[] = { ARM::VLD3DUPd8Pseudo_UPD,
3106 ARM::VLD3DUPd16Pseudo_UPD,
3107 ARM::VLD3DUPd32Pseudo_UPD };
Justin Bogner45571362016-05-12 00:31:09 +00003108 SelectVLDDup(N, true, 3, Opcodes);
3109 return;
Bob Wilson06fce872011-02-07 17:43:21 +00003110 }
3111
3112 case ARMISD::VLD4DUP_UPD: {
Craig Topper01736f82012-05-24 05:17:00 +00003113 static const uint16_t Opcodes[] = { ARM::VLD4DUPd8Pseudo_UPD,
3114 ARM::VLD4DUPd16Pseudo_UPD,
3115 ARM::VLD4DUPd32Pseudo_UPD };
Justin Bogner45571362016-05-12 00:31:09 +00003116 SelectVLDDup(N, true, 4, Opcodes);
3117 return;
Bob Wilson06fce872011-02-07 17:43:21 +00003118 }
3119
3120 case ARMISD::VLD1_UPD: {
Craig Topper01736f82012-05-24 05:17:00 +00003121 static const uint16_t DOpcodes[] = { ARM::VLD1d8wb_fixed,
3122 ARM::VLD1d16wb_fixed,
3123 ARM::VLD1d32wb_fixed,
3124 ARM::VLD1d64wb_fixed };
3125 static const uint16_t QOpcodes[] = { ARM::VLD1q8wb_fixed,
3126 ARM::VLD1q16wb_fixed,
3127 ARM::VLD1q32wb_fixed,
3128 ARM::VLD1q64wb_fixed };
Justin Bogner45571362016-05-12 00:31:09 +00003129 SelectVLD(N, true, 1, DOpcodes, QOpcodes, nullptr);
3130 return;
Bob Wilson06fce872011-02-07 17:43:21 +00003131 }
3132
3133 case ARMISD::VLD2_UPD: {
Craig Topper01736f82012-05-24 05:17:00 +00003134 static const uint16_t DOpcodes[] = { ARM::VLD2d8wb_fixed,
3135 ARM::VLD2d16wb_fixed,
3136 ARM::VLD2d32wb_fixed,
3137 ARM::VLD1q64wb_fixed};
3138 static const uint16_t QOpcodes[] = { ARM::VLD2q8PseudoWB_fixed,
3139 ARM::VLD2q16PseudoWB_fixed,
3140 ARM::VLD2q32PseudoWB_fixed };
Justin Bogner45571362016-05-12 00:31:09 +00003141 SelectVLD(N, true, 2, DOpcodes, QOpcodes, nullptr);
3142 return;
Bob Wilson06fce872011-02-07 17:43:21 +00003143 }
3144
3145 case ARMISD::VLD3_UPD: {
Craig Topper01736f82012-05-24 05:17:00 +00003146 static const uint16_t DOpcodes[] = { ARM::VLD3d8Pseudo_UPD,
3147 ARM::VLD3d16Pseudo_UPD,
3148 ARM::VLD3d32Pseudo_UPD,
Jiangning Liu4df23632014-01-16 09:16:13 +00003149 ARM::VLD1d64TPseudoWB_fixed};
Craig Topper01736f82012-05-24 05:17:00 +00003150 static const uint16_t QOpcodes0[] = { ARM::VLD3q8Pseudo_UPD,
3151 ARM::VLD3q16Pseudo_UPD,
3152 ARM::VLD3q32Pseudo_UPD };
3153 static const uint16_t QOpcodes1[] = { ARM::VLD3q8oddPseudo_UPD,
3154 ARM::VLD3q16oddPseudo_UPD,
3155 ARM::VLD3q32oddPseudo_UPD };
Justin Bogner45571362016-05-12 00:31:09 +00003156 SelectVLD(N, true, 3, DOpcodes, QOpcodes0, QOpcodes1);
3157 return;
Bob Wilson06fce872011-02-07 17:43:21 +00003158 }
3159
3160 case ARMISD::VLD4_UPD: {
Craig Topper01736f82012-05-24 05:17:00 +00003161 static const uint16_t DOpcodes[] = { ARM::VLD4d8Pseudo_UPD,
3162 ARM::VLD4d16Pseudo_UPD,
3163 ARM::VLD4d32Pseudo_UPD,
Jiangning Liu4df23632014-01-16 09:16:13 +00003164 ARM::VLD1d64QPseudoWB_fixed};
Craig Topper01736f82012-05-24 05:17:00 +00003165 static const uint16_t QOpcodes0[] = { ARM::VLD4q8Pseudo_UPD,
3166 ARM::VLD4q16Pseudo_UPD,
3167 ARM::VLD4q32Pseudo_UPD };
3168 static const uint16_t QOpcodes1[] = { ARM::VLD4q8oddPseudo_UPD,
3169 ARM::VLD4q16oddPseudo_UPD,
3170 ARM::VLD4q32oddPseudo_UPD };
Justin Bogner45571362016-05-12 00:31:09 +00003171 SelectVLD(N, true, 4, DOpcodes, QOpcodes0, QOpcodes1);
3172 return;
Bob Wilson06fce872011-02-07 17:43:21 +00003173 }
3174
3175 case ARMISD::VLD2LN_UPD: {
Craig Topper01736f82012-05-24 05:17:00 +00003176 static const uint16_t DOpcodes[] = { ARM::VLD2LNd8Pseudo_UPD,
3177 ARM::VLD2LNd16Pseudo_UPD,
3178 ARM::VLD2LNd32Pseudo_UPD };
3179 static const uint16_t QOpcodes[] = { ARM::VLD2LNq16Pseudo_UPD,
3180 ARM::VLD2LNq32Pseudo_UPD };
Justin Bogner45571362016-05-12 00:31:09 +00003181 SelectVLDSTLane(N, true, true, 2, DOpcodes, QOpcodes);
3182 return;
Bob Wilson06fce872011-02-07 17:43:21 +00003183 }
3184
3185 case ARMISD::VLD3LN_UPD: {
Craig Topper01736f82012-05-24 05:17:00 +00003186 static const uint16_t DOpcodes[] = { ARM::VLD3LNd8Pseudo_UPD,
3187 ARM::VLD3LNd16Pseudo_UPD,
3188 ARM::VLD3LNd32Pseudo_UPD };
3189 static const uint16_t QOpcodes[] = { ARM::VLD3LNq16Pseudo_UPD,
3190 ARM::VLD3LNq32Pseudo_UPD };
Justin Bogner45571362016-05-12 00:31:09 +00003191 SelectVLDSTLane(N, true, true, 3, DOpcodes, QOpcodes);
3192 return;
Bob Wilson06fce872011-02-07 17:43:21 +00003193 }
3194
3195 case ARMISD::VLD4LN_UPD: {
Craig Topper01736f82012-05-24 05:17:00 +00003196 static const uint16_t DOpcodes[] = { ARM::VLD4LNd8Pseudo_UPD,
3197 ARM::VLD4LNd16Pseudo_UPD,
3198 ARM::VLD4LNd32Pseudo_UPD };
3199 static const uint16_t QOpcodes[] = { ARM::VLD4LNq16Pseudo_UPD,
3200 ARM::VLD4LNq32Pseudo_UPD };
Justin Bogner45571362016-05-12 00:31:09 +00003201 SelectVLDSTLane(N, true, true, 4, DOpcodes, QOpcodes);
3202 return;
Bob Wilson06fce872011-02-07 17:43:21 +00003203 }
3204
3205 case ARMISD::VST1_UPD: {
Craig Topper01736f82012-05-24 05:17:00 +00003206 static const uint16_t DOpcodes[] = { ARM::VST1d8wb_fixed,
3207 ARM::VST1d16wb_fixed,
3208 ARM::VST1d32wb_fixed,
3209 ARM::VST1d64wb_fixed };
3210 static const uint16_t QOpcodes[] = { ARM::VST1q8wb_fixed,
3211 ARM::VST1q16wb_fixed,
3212 ARM::VST1q32wb_fixed,
3213 ARM::VST1q64wb_fixed };
Justin Bogner45571362016-05-12 00:31:09 +00003214 SelectVST(N, true, 1, DOpcodes, QOpcodes, nullptr);
3215 return;
Bob Wilson06fce872011-02-07 17:43:21 +00003216 }
3217
3218 case ARMISD::VST2_UPD: {
Craig Topper01736f82012-05-24 05:17:00 +00003219 static const uint16_t DOpcodes[] = { ARM::VST2d8wb_fixed,
3220 ARM::VST2d16wb_fixed,
3221 ARM::VST2d32wb_fixed,
3222 ARM::VST1q64wb_fixed};
3223 static const uint16_t QOpcodes[] = { ARM::VST2q8PseudoWB_fixed,
3224 ARM::VST2q16PseudoWB_fixed,
3225 ARM::VST2q32PseudoWB_fixed };
Justin Bogner45571362016-05-12 00:31:09 +00003226 SelectVST(N, true, 2, DOpcodes, QOpcodes, nullptr);
3227 return;
Bob Wilson06fce872011-02-07 17:43:21 +00003228 }
3229
3230 case ARMISD::VST3_UPD: {
Craig Topper01736f82012-05-24 05:17:00 +00003231 static const uint16_t DOpcodes[] = { ARM::VST3d8Pseudo_UPD,
3232 ARM::VST3d16Pseudo_UPD,
3233 ARM::VST3d32Pseudo_UPD,
3234 ARM::VST1d64TPseudoWB_fixed};
3235 static const uint16_t QOpcodes0[] = { ARM::VST3q8Pseudo_UPD,
3236 ARM::VST3q16Pseudo_UPD,
3237 ARM::VST3q32Pseudo_UPD };
3238 static const uint16_t QOpcodes1[] = { ARM::VST3q8oddPseudo_UPD,
3239 ARM::VST3q16oddPseudo_UPD,
3240 ARM::VST3q32oddPseudo_UPD };
Justin Bogner45571362016-05-12 00:31:09 +00003241 SelectVST(N, true, 3, DOpcodes, QOpcodes0, QOpcodes1);
3242 return;
Bob Wilson06fce872011-02-07 17:43:21 +00003243 }
3244
3245 case ARMISD::VST4_UPD: {
Craig Topper01736f82012-05-24 05:17:00 +00003246 static const uint16_t DOpcodes[] = { ARM::VST4d8Pseudo_UPD,
3247 ARM::VST4d16Pseudo_UPD,
3248 ARM::VST4d32Pseudo_UPD,
3249 ARM::VST1d64QPseudoWB_fixed};
3250 static const uint16_t QOpcodes0[] = { ARM::VST4q8Pseudo_UPD,
3251 ARM::VST4q16Pseudo_UPD,
3252 ARM::VST4q32Pseudo_UPD };
3253 static const uint16_t QOpcodes1[] = { ARM::VST4q8oddPseudo_UPD,
3254 ARM::VST4q16oddPseudo_UPD,
3255 ARM::VST4q32oddPseudo_UPD };
Justin Bogner45571362016-05-12 00:31:09 +00003256 SelectVST(N, true, 4, DOpcodes, QOpcodes0, QOpcodes1);
3257 return;
Bob Wilson06fce872011-02-07 17:43:21 +00003258 }
3259
3260 case ARMISD::VST2LN_UPD: {
Craig Topper01736f82012-05-24 05:17:00 +00003261 static const uint16_t DOpcodes[] = { ARM::VST2LNd8Pseudo_UPD,
3262 ARM::VST2LNd16Pseudo_UPD,
3263 ARM::VST2LNd32Pseudo_UPD };
3264 static const uint16_t QOpcodes[] = { ARM::VST2LNq16Pseudo_UPD,
3265 ARM::VST2LNq32Pseudo_UPD };
Justin Bogner45571362016-05-12 00:31:09 +00003266 SelectVLDSTLane(N, false, true, 2, DOpcodes, QOpcodes);
3267 return;
Bob Wilson06fce872011-02-07 17:43:21 +00003268 }
3269
3270 case ARMISD::VST3LN_UPD: {
Craig Topper01736f82012-05-24 05:17:00 +00003271 static const uint16_t DOpcodes[] = { ARM::VST3LNd8Pseudo_UPD,
3272 ARM::VST3LNd16Pseudo_UPD,
3273 ARM::VST3LNd32Pseudo_UPD };
3274 static const uint16_t QOpcodes[] = { ARM::VST3LNq16Pseudo_UPD,
3275 ARM::VST3LNq32Pseudo_UPD };
Justin Bogner45571362016-05-12 00:31:09 +00003276 SelectVLDSTLane(N, false, true, 3, DOpcodes, QOpcodes);
3277 return;
Bob Wilson06fce872011-02-07 17:43:21 +00003278 }
3279
3280 case ARMISD::VST4LN_UPD: {
Craig Topper01736f82012-05-24 05:17:00 +00003281 static const uint16_t DOpcodes[] = { ARM::VST4LNd8Pseudo_UPD,
3282 ARM::VST4LNd16Pseudo_UPD,
3283 ARM::VST4LNd32Pseudo_UPD };
3284 static const uint16_t QOpcodes[] = { ARM::VST4LNq16Pseudo_UPD,
3285 ARM::VST4LNq32Pseudo_UPD };
Justin Bogner45571362016-05-12 00:31:09 +00003286 SelectVLDSTLane(N, false, true, 4, DOpcodes, QOpcodes);
3287 return;
Bob Wilson431ac4ef2010-11-30 00:00:35 +00003288 }
3289
Bob Wilsone0636a72009-08-26 17:39:53 +00003290 case ISD::INTRINSIC_VOID:
3291 case ISD::INTRINSIC_W_CHAIN: {
3292 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
Bob Wilsone0636a72009-08-26 17:39:53 +00003293 switch (IntNo) {
3294 default:
Bob Wilsonf765e1f2010-05-06 16:05:26 +00003295 break;
Bob Wilsone0636a72009-08-26 17:39:53 +00003296
Ranjeet Singh39d2d092016-06-17 00:52:41 +00003297 case Intrinsic::arm_mrrc:
3298 case Intrinsic::arm_mrrc2: {
3299 SDLoc dl(N);
3300 SDValue Chain = N->getOperand(0);
3301 unsigned Opc;
3302
3303 if (Subtarget->isThumb())
3304 Opc = (IntNo == Intrinsic::arm_mrrc ? ARM::t2MRRC : ARM::t2MRRC2);
3305 else
3306 Opc = (IntNo == Intrinsic::arm_mrrc ? ARM::MRRC : ARM::MRRC2);
3307
3308 SmallVector<SDValue, 5> Ops;
3309 Ops.push_back(getI32Imm(cast<ConstantSDNode>(N->getOperand(2))->getZExtValue(), dl)); /* coproc */
3310 Ops.push_back(getI32Imm(cast<ConstantSDNode>(N->getOperand(3))->getZExtValue(), dl)); /* opc */
3311 Ops.push_back(getI32Imm(cast<ConstantSDNode>(N->getOperand(4))->getZExtValue(), dl)); /* CRm */
3312
3313 // The mrrc2 instruction in ARM doesn't allow predicates, the top 4 bits of the encoded
3314 // instruction will always be '1111' but it is possible in assembly language to specify
3315 // AL as a predicate to mrrc2 but it doesn't make any difference to the encoded instruction.
3316 if (Opc != ARM::MRRC2) {
3317 Ops.push_back(getAL(CurDAG, dl));
3318 Ops.push_back(CurDAG->getRegister(0, MVT::i32));
3319 }
3320
3321 Ops.push_back(Chain);
3322
3323 // Writes to two registers.
Benjamin Kramerf690da42016-06-17 14:14:29 +00003324 const EVT RetType[] = {MVT::i32, MVT::i32, MVT::Other};
Ranjeet Singh39d2d092016-06-17 00:52:41 +00003325
3326 ReplaceNode(N, CurDAG->getMachineNode(Opc, dl, RetType, Ops));
3327 return;
3328 }
Tim Northover1ff5f292014-03-26 14:39:31 +00003329 case Intrinsic::arm_ldaexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003330 case Intrinsic::arm_ldrexd: {
Andrew Trickef9de2a2013-05-25 02:42:55 +00003331 SDLoc dl(N);
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003332 SDValue Chain = N->getOperand(0);
Tim Northover1ff5f292014-03-26 14:39:31 +00003333 SDValue MemAddr = N->getOperand(2);
Bradley Smith433c22e2016-01-15 10:26:51 +00003334 bool isThumb = Subtarget->isThumb() && Subtarget->hasV8MBaselineOps();
Tim Northover1ff5f292014-03-26 14:39:31 +00003335
3336 bool IsAcquire = IntNo == Intrinsic::arm_ldaexd;
3337 unsigned NewOpc = isThumb ? (IsAcquire ? ARM::t2LDAEXD : ARM::t2LDREXD)
3338 : (IsAcquire ? ARM::LDAEXD : ARM::LDREXD);
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003339
3340 // arm_ldrexd returns a i64 value in {i32, i32}
3341 std::vector<EVT> ResTys;
Weiming Zhao8f56f882012-11-16 21:55:34 +00003342 if (isThumb) {
3343 ResTys.push_back(MVT::i32);
3344 ResTys.push_back(MVT::i32);
3345 } else
3346 ResTys.push_back(MVT::Untyped);
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003347 ResTys.push_back(MVT::Other);
3348
Weiming Zhao8f56f882012-11-16 21:55:34 +00003349 // Place arguments in the right order.
Benjamin Kramerf690da42016-06-17 14:14:29 +00003350 SDValue Ops[] = {MemAddr, getAL(CurDAG, dl),
3351 CurDAG->getRegister(0, MVT::i32), Chain};
Michael Liaob53d8962013-04-19 22:22:57 +00003352 SDNode *Ld = CurDAG->getMachineNode(NewOpc, dl, ResTys, Ops);
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003353 // Transfer memoperands.
3354 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
3355 MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
3356 cast<MachineSDNode>(Ld)->setMemRefs(MemOp, MemOp + 1);
3357
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003358 // Remap uses.
Lang Hamesbe3d9712013-03-09 22:56:09 +00003359 SDValue OutChain = isThumb ? SDValue(Ld, 2) : SDValue(Ld, 1);
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003360 if (!SDValue(N, 0).use_empty()) {
Weiming Zhao8f56f882012-11-16 21:55:34 +00003361 SDValue Result;
3362 if (isThumb)
3363 Result = SDValue(Ld, 0);
3364 else {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003365 SDValue SubRegIdx =
3366 CurDAG->getTargetConstant(ARM::gsub_0, dl, MVT::i32);
Weiming Zhao8f56f882012-11-16 21:55:34 +00003367 SDNode *ResNode = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
Lang Hamesbe3d9712013-03-09 22:56:09 +00003368 dl, MVT::i32, SDValue(Ld, 0), SubRegIdx);
Weiming Zhao8f56f882012-11-16 21:55:34 +00003369 Result = SDValue(ResNode,0);
Weiming Zhao8f56f882012-11-16 21:55:34 +00003370 }
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003371 ReplaceUses(SDValue(N, 0), Result);
3372 }
3373 if (!SDValue(N, 1).use_empty()) {
Weiming Zhao8f56f882012-11-16 21:55:34 +00003374 SDValue Result;
3375 if (isThumb)
3376 Result = SDValue(Ld, 1);
3377 else {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003378 SDValue SubRegIdx =
3379 CurDAG->getTargetConstant(ARM::gsub_1, dl, MVT::i32);
Weiming Zhao8f56f882012-11-16 21:55:34 +00003380 SDNode *ResNode = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
Lang Hamesbe3d9712013-03-09 22:56:09 +00003381 dl, MVT::i32, SDValue(Ld, 0), SubRegIdx);
Weiming Zhao8f56f882012-11-16 21:55:34 +00003382 Result = SDValue(ResNode,0);
Weiming Zhao8f56f882012-11-16 21:55:34 +00003383 }
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003384 ReplaceUses(SDValue(N, 1), Result);
3385 }
Lang Hamesbe3d9712013-03-09 22:56:09 +00003386 ReplaceUses(SDValue(N, 2), OutChain);
Justin Bognered4f3782016-05-12 00:20:19 +00003387 CurDAG->RemoveDeadNode(N);
Justin Bogner45571362016-05-12 00:31:09 +00003388 return;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003389 }
Tim Northover1ff5f292014-03-26 14:39:31 +00003390 case Intrinsic::arm_stlexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003391 case Intrinsic::arm_strexd: {
Andrew Trickef9de2a2013-05-25 02:42:55 +00003392 SDLoc dl(N);
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003393 SDValue Chain = N->getOperand(0);
3394 SDValue Val0 = N->getOperand(2);
3395 SDValue Val1 = N->getOperand(3);
3396 SDValue MemAddr = N->getOperand(4);
3397
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003398 // Store exclusive double return a i32 value which is the return status
3399 // of the issued store.
Benjamin Kramer867bfc52015-03-07 17:41:00 +00003400 const EVT ResTys[] = {MVT::i32, MVT::Other};
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003401
Weiming Zhao8f56f882012-11-16 21:55:34 +00003402 bool isThumb = Subtarget->isThumb() && Subtarget->hasThumb2();
3403 // Place arguments in the right order.
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003404 SmallVector<SDValue, 7> Ops;
Weiming Zhao8f56f882012-11-16 21:55:34 +00003405 if (isThumb) {
3406 Ops.push_back(Val0);
3407 Ops.push_back(Val1);
3408 } else
3409 // arm_strexd uses GPRPair.
3410 Ops.push_back(SDValue(createGPRPairNode(MVT::Untyped, Val0, Val1), 0));
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003411 Ops.push_back(MemAddr);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003412 Ops.push_back(getAL(CurDAG, dl));
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003413 Ops.push_back(CurDAG->getRegister(0, MVT::i32));
3414 Ops.push_back(Chain);
3415
Tim Northover1ff5f292014-03-26 14:39:31 +00003416 bool IsRelease = IntNo == Intrinsic::arm_stlexd;
3417 unsigned NewOpc = isThumb ? (IsRelease ? ARM::t2STLEXD : ARM::t2STREXD)
3418 : (IsRelease ? ARM::STLEXD : ARM::STREXD);
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003419
Michael Liaob53d8962013-04-19 22:22:57 +00003420 SDNode *St = CurDAG->getMachineNode(NewOpc, dl, ResTys, Ops);
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003421 // Transfer memoperands.
3422 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
3423 MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
3424 cast<MachineSDNode>(St)->setMemRefs(MemOp, MemOp + 1);
3425
Justin Bogner45571362016-05-12 00:31:09 +00003426 ReplaceNode(N, St);
3427 return;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +00003428 }
3429
Bob Wilson340861d2010-03-23 05:25:43 +00003430 case Intrinsic::arm_neon_vld1: {
Craig Topper01736f82012-05-24 05:17:00 +00003431 static const uint16_t DOpcodes[] = { ARM::VLD1d8, ARM::VLD1d16,
3432 ARM::VLD1d32, ARM::VLD1d64 };
3433 static const uint16_t QOpcodes[] = { ARM::VLD1q8, ARM::VLD1q16,
3434 ARM::VLD1q32, ARM::VLD1q64};
Justin Bogner45571362016-05-12 00:31:09 +00003435 SelectVLD(N, false, 1, DOpcodes, QOpcodes, nullptr);
3436 return;
Bob Wilson340861d2010-03-23 05:25:43 +00003437 }
3438
Bob Wilsone0636a72009-08-26 17:39:53 +00003439 case Intrinsic::arm_neon_vld2: {
Craig Topper01736f82012-05-24 05:17:00 +00003440 static const uint16_t DOpcodes[] = { ARM::VLD2d8, ARM::VLD2d16,
3441 ARM::VLD2d32, ARM::VLD1q64 };
3442 static const uint16_t QOpcodes[] = { ARM::VLD2q8Pseudo, ARM::VLD2q16Pseudo,
3443 ARM::VLD2q32Pseudo };
Justin Bogner45571362016-05-12 00:31:09 +00003444 SelectVLD(N, false, 2, DOpcodes, QOpcodes, nullptr);
3445 return;
Bob Wilsone0636a72009-08-26 17:39:53 +00003446 }
3447
3448 case Intrinsic::arm_neon_vld3: {
Craig Topper01736f82012-05-24 05:17:00 +00003449 static const uint16_t DOpcodes[] = { ARM::VLD3d8Pseudo,
3450 ARM::VLD3d16Pseudo,
3451 ARM::VLD3d32Pseudo,
3452 ARM::VLD1d64TPseudo };
3453 static const uint16_t QOpcodes0[] = { ARM::VLD3q8Pseudo_UPD,
3454 ARM::VLD3q16Pseudo_UPD,
3455 ARM::VLD3q32Pseudo_UPD };
3456 static const uint16_t QOpcodes1[] = { ARM::VLD3q8oddPseudo,
3457 ARM::VLD3q16oddPseudo,
3458 ARM::VLD3q32oddPseudo };
Justin Bogner45571362016-05-12 00:31:09 +00003459 SelectVLD(N, false, 3, DOpcodes, QOpcodes0, QOpcodes1);
3460 return;
Bob Wilsone0636a72009-08-26 17:39:53 +00003461 }
3462
3463 case Intrinsic::arm_neon_vld4: {
Craig Topper01736f82012-05-24 05:17:00 +00003464 static const uint16_t DOpcodes[] = { ARM::VLD4d8Pseudo,
3465 ARM::VLD4d16Pseudo,
3466 ARM::VLD4d32Pseudo,
3467 ARM::VLD1d64QPseudo };
3468 static const uint16_t QOpcodes0[] = { ARM::VLD4q8Pseudo_UPD,
3469 ARM::VLD4q16Pseudo_UPD,
3470 ARM::VLD4q32Pseudo_UPD };
3471 static const uint16_t QOpcodes1[] = { ARM::VLD4q8oddPseudo,
3472 ARM::VLD4q16oddPseudo,
3473 ARM::VLD4q32oddPseudo };
Justin Bogner45571362016-05-12 00:31:09 +00003474 SelectVLD(N, false, 4, DOpcodes, QOpcodes0, QOpcodes1);
3475 return;
Bob Wilsone0636a72009-08-26 17:39:53 +00003476 }
3477
Bob Wilsonda9817c2009-09-01 04:26:28 +00003478 case Intrinsic::arm_neon_vld2lane: {
Craig Topper01736f82012-05-24 05:17:00 +00003479 static const uint16_t DOpcodes[] = { ARM::VLD2LNd8Pseudo,
3480 ARM::VLD2LNd16Pseudo,
3481 ARM::VLD2LNd32Pseudo };
3482 static const uint16_t QOpcodes[] = { ARM::VLD2LNq16Pseudo,
3483 ARM::VLD2LNq32Pseudo };
Justin Bogner45571362016-05-12 00:31:09 +00003484 SelectVLDSTLane(N, true, false, 2, DOpcodes, QOpcodes);
3485 return;
Bob Wilsonda9817c2009-09-01 04:26:28 +00003486 }
3487
3488 case Intrinsic::arm_neon_vld3lane: {
Craig Topper01736f82012-05-24 05:17:00 +00003489 static const uint16_t DOpcodes[] = { ARM::VLD3LNd8Pseudo,
3490 ARM::VLD3LNd16Pseudo,
3491 ARM::VLD3LNd32Pseudo };
3492 static const uint16_t QOpcodes[] = { ARM::VLD3LNq16Pseudo,
3493 ARM::VLD3LNq32Pseudo };
Justin Bogner45571362016-05-12 00:31:09 +00003494 SelectVLDSTLane(N, true, false, 3, DOpcodes, QOpcodes);
3495 return;
Bob Wilsonda9817c2009-09-01 04:26:28 +00003496 }
3497
3498 case Intrinsic::arm_neon_vld4lane: {
Craig Topper01736f82012-05-24 05:17:00 +00003499 static const uint16_t DOpcodes[] = { ARM::VLD4LNd8Pseudo,
3500 ARM::VLD4LNd16Pseudo,
3501 ARM::VLD4LNd32Pseudo };
3502 static const uint16_t QOpcodes[] = { ARM::VLD4LNq16Pseudo,
3503 ARM::VLD4LNq32Pseudo };
Justin Bogner45571362016-05-12 00:31:09 +00003504 SelectVLDSTLane(N, true, false, 4, DOpcodes, QOpcodes);
3505 return;
Bob Wilsonda9817c2009-09-01 04:26:28 +00003506 }
3507
Bob Wilsoncc0a2a72010-03-23 06:20:33 +00003508 case Intrinsic::arm_neon_vst1: {
Craig Topper01736f82012-05-24 05:17:00 +00003509 static const uint16_t DOpcodes[] = { ARM::VST1d8, ARM::VST1d16,
3510 ARM::VST1d32, ARM::VST1d64 };
3511 static const uint16_t QOpcodes[] = { ARM::VST1q8, ARM::VST1q16,
3512 ARM::VST1q32, ARM::VST1q64 };
Justin Bogner45571362016-05-12 00:31:09 +00003513 SelectVST(N, false, 1, DOpcodes, QOpcodes, nullptr);
3514 return;
Bob Wilsoncc0a2a72010-03-23 06:20:33 +00003515 }
3516
Bob Wilsone0636a72009-08-26 17:39:53 +00003517 case Intrinsic::arm_neon_vst2: {
Craig Topper01736f82012-05-24 05:17:00 +00003518 static const uint16_t DOpcodes[] = { ARM::VST2d8, ARM::VST2d16,
3519 ARM::VST2d32, ARM::VST1q64 };
Benjamin Kramerf690da42016-06-17 14:14:29 +00003520 static const uint16_t QOpcodes[] = { ARM::VST2q8Pseudo, ARM::VST2q16Pseudo,
3521 ARM::VST2q32Pseudo };
Justin Bogner45571362016-05-12 00:31:09 +00003522 SelectVST(N, false, 2, DOpcodes, QOpcodes, nullptr);
3523 return;
Bob Wilsone0636a72009-08-26 17:39:53 +00003524 }
3525
3526 case Intrinsic::arm_neon_vst3: {
Craig Topper01736f82012-05-24 05:17:00 +00003527 static const uint16_t DOpcodes[] = { ARM::VST3d8Pseudo,
3528 ARM::VST3d16Pseudo,
3529 ARM::VST3d32Pseudo,
3530 ARM::VST1d64TPseudo };
3531 static const uint16_t QOpcodes0[] = { ARM::VST3q8Pseudo_UPD,
3532 ARM::VST3q16Pseudo_UPD,
3533 ARM::VST3q32Pseudo_UPD };
3534 static const uint16_t QOpcodes1[] = { ARM::VST3q8oddPseudo,
3535 ARM::VST3q16oddPseudo,
3536 ARM::VST3q32oddPseudo };
Justin Bogner45571362016-05-12 00:31:09 +00003537 SelectVST(N, false, 3, DOpcodes, QOpcodes0, QOpcodes1);
3538 return;
Bob Wilsone0636a72009-08-26 17:39:53 +00003539 }
3540
3541 case Intrinsic::arm_neon_vst4: {
Craig Topper01736f82012-05-24 05:17:00 +00003542 static const uint16_t DOpcodes[] = { ARM::VST4d8Pseudo,
3543 ARM::VST4d16Pseudo,
3544 ARM::VST4d32Pseudo,
3545 ARM::VST1d64QPseudo };
3546 static const uint16_t QOpcodes0[] = { ARM::VST4q8Pseudo_UPD,
3547 ARM::VST4q16Pseudo_UPD,
3548 ARM::VST4q32Pseudo_UPD };
3549 static const uint16_t QOpcodes1[] = { ARM::VST4q8oddPseudo,
3550 ARM::VST4q16oddPseudo,
3551 ARM::VST4q32oddPseudo };
Justin Bogner45571362016-05-12 00:31:09 +00003552 SelectVST(N, false, 4, DOpcodes, QOpcodes0, QOpcodes1);
3553 return;
Bob Wilsone0636a72009-08-26 17:39:53 +00003554 }
Bob Wilsond7797752009-09-01 18:51:56 +00003555
3556 case Intrinsic::arm_neon_vst2lane: {
Craig Topper01736f82012-05-24 05:17:00 +00003557 static const uint16_t DOpcodes[] = { ARM::VST2LNd8Pseudo,
3558 ARM::VST2LNd16Pseudo,
3559 ARM::VST2LNd32Pseudo };
3560 static const uint16_t QOpcodes[] = { ARM::VST2LNq16Pseudo,
3561 ARM::VST2LNq32Pseudo };
Justin Bogner45571362016-05-12 00:31:09 +00003562 SelectVLDSTLane(N, false, false, 2, DOpcodes, QOpcodes);
3563 return;
Bob Wilsond7797752009-09-01 18:51:56 +00003564 }
3565
3566 case Intrinsic::arm_neon_vst3lane: {
Craig Topper01736f82012-05-24 05:17:00 +00003567 static const uint16_t DOpcodes[] = { ARM::VST3LNd8Pseudo,
3568 ARM::VST3LNd16Pseudo,
3569 ARM::VST3LNd32Pseudo };
3570 static const uint16_t QOpcodes[] = { ARM::VST3LNq16Pseudo,
3571 ARM::VST3LNq32Pseudo };
Justin Bogner45571362016-05-12 00:31:09 +00003572 SelectVLDSTLane(N, false, false, 3, DOpcodes, QOpcodes);
3573 return;
Bob Wilsond7797752009-09-01 18:51:56 +00003574 }
3575
3576 case Intrinsic::arm_neon_vst4lane: {
Craig Topper01736f82012-05-24 05:17:00 +00003577 static const uint16_t DOpcodes[] = { ARM::VST4LNd8Pseudo,
3578 ARM::VST4LNd16Pseudo,
3579 ARM::VST4LNd32Pseudo };
3580 static const uint16_t QOpcodes[] = { ARM::VST4LNq16Pseudo,
3581 ARM::VST4LNq32Pseudo };
Justin Bogner45571362016-05-12 00:31:09 +00003582 SelectVLDSTLane(N, false, false, 4, DOpcodes, QOpcodes);
3583 return;
Bob Wilsond7797752009-09-01 18:51:56 +00003584 }
Bob Wilsone0636a72009-08-26 17:39:53 +00003585 }
Bob Wilsonf765e1f2010-05-06 16:05:26 +00003586 break;
Bob Wilsone0636a72009-08-26 17:39:53 +00003587 }
Evan Chengd85631e2010-05-05 18:28:36 +00003588
Tim Northoverb629c772016-04-18 21:48:55 +00003589 case ISD::ATOMIC_CMP_SWAP:
Justin Bogner45571362016-05-12 00:31:09 +00003590 SelectCMP_SWAP(N);
3591 return;
Evan Chengd85631e2010-05-05 18:28:36 +00003592 }
Evan Chengd5021732008-12-10 21:54:21 +00003593
Justin Bogner45571362016-05-12 00:31:09 +00003594 SelectCode(N);
Evan Cheng10043e22007-01-19 07:51:42 +00003595}
Rafael Espindolaffdc24b2006-05-14 22:18:28 +00003596
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00003597// Inspect a register string of the form
3598// cp<coprocessor>:<opc1>:c<CRn>:c<CRm>:<opc2> (32bit) or
3599// cp<coprocessor>:<opc1>:c<CRm> (64bit) inspect the fields of the string
3600// and obtain the integer operands from them, adding these operands to the
3601// provided vector.
3602static void getIntOperandsFromRegisterString(StringRef RegString,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003603 SelectionDAG *CurDAG,
3604 const SDLoc &DL,
3605 std::vector<SDValue> &Ops) {
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00003606 SmallVector<StringRef, 5> Fields;
Chandler Carruthe4405e92015-09-10 06:12:31 +00003607 RegString.split(Fields, ':');
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00003608
3609 if (Fields.size() > 1) {
3610 bool AllIntFields = true;
3611
3612 for (StringRef Field : Fields) {
3613 // Need to trim out leading 'cp' characters and get the integer field.
3614 unsigned IntField;
3615 AllIntFields &= !Field.trim("CPcp").getAsInteger(10, IntField);
3616 Ops.push_back(CurDAG->getTargetConstant(IntField, DL, MVT::i32));
3617 }
3618
3619 assert(AllIntFields &&
3620 "Unexpected non-integer value in special register string.");
3621 }
3622}
3623
3624// Maps a Banked Register string to its mask value. The mask value returned is
3625// for use in the MRSbanked / MSRbanked instruction nodes as the Banked Register
3626// mask operand, which expresses which register is to be used, e.g. r8, and in
3627// which mode it is to be used, e.g. usr. Returns -1 to signify that the string
3628// was invalid.
3629static inline int getBankedRegisterMask(StringRef RegString) {
Javed Absar054d1ae2017-08-03 01:24:12 +00003630 auto TheReg = ARMBankedReg::lookupBankedRegByName(RegString.lower());
3631 if (!TheReg)
3632 return -1;
3633 return TheReg->Encoding;
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00003634}
3635
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00003636// The flags here are common to those allowed for apsr in the A class cores and
3637// those allowed for the special registers in the M class cores. Returns a
3638// value representing which flags were present, -1 if invalid.
John Brawne60f4e42017-02-10 17:41:08 +00003639static inline int getMClassFlagsMask(StringRef Flags) {
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00003640 return StringSwitch<int>(Flags)
John Brawne60f4e42017-02-10 17:41:08 +00003641 .Case("", 0x2) // no flags means nzcvq for psr registers, and 0x2 is
3642 // correct when flags are not permitted
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00003643 .Case("g", 0x1)
3644 .Case("nzcvq", 0x2)
3645 .Case("nzcvqg", 0x3)
3646 .Default(-1);
3647}
3648
Javed Absar2cb0c952017-07-19 12:57:16 +00003649// Maps MClass special registers string to its value for use in the
3650// t2MRS_M/t2MSR_M instruction nodes as the SYSm value operand.
3651// Returns -1 to signify that the string was invalid.
3652static int getMClassRegisterMask(StringRef Reg, const ARMSubtarget *Subtarget) {
3653 auto TheReg = ARMSysReg::lookupMClassSysRegByName(Reg);
3654 const FeatureBitset &FeatureBits = Subtarget->getFeatureBits();
3655 if (!TheReg || !TheReg->hasRequiredFeatures(FeatureBits))
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00003656 return -1;
Javed Absar2cb0c952017-07-19 12:57:16 +00003657 return (int)(TheReg->Encoding & 0xFFF); // SYSm value
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00003658}
3659
3660static int getARClassRegisterMask(StringRef Reg, StringRef Flags) {
3661 // The mask operand contains the special register (R Bit) in bit 4, whether
3662 // the register is spsr (R bit is 1) or one of cpsr/apsr (R bit is 0), and
3663 // bits 3-0 contains the fields to be accessed in the special register, set by
3664 // the flags provided with the register.
3665 int Mask = 0;
3666 if (Reg == "apsr") {
3667 // The flags permitted for apsr are the same flags that are allowed in
3668 // M class registers. We get the flag value and then shift the flags into
3669 // the correct place to combine with the mask.
John Brawne60f4e42017-02-10 17:41:08 +00003670 Mask = getMClassFlagsMask(Flags);
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00003671 if (Mask == -1)
3672 return -1;
3673 return Mask << 2;
3674 }
3675
3676 if (Reg != "cpsr" && Reg != "spsr") {
3677 return -1;
3678 }
3679
3680 // This is the same as if the flags were "fc"
3681 if (Flags.empty() || Flags == "all")
3682 return Mask | 0x9;
3683
3684 // Inspect the supplied flags string and set the bits in the mask for
3685 // the relevant and valid flags allowed for cpsr and spsr.
3686 for (char Flag : Flags) {
3687 int FlagVal;
3688 switch (Flag) {
3689 case 'c':
3690 FlagVal = 0x1;
3691 break;
3692 case 'x':
3693 FlagVal = 0x2;
3694 break;
3695 case 's':
3696 FlagVal = 0x4;
3697 break;
3698 case 'f':
3699 FlagVal = 0x8;
3700 break;
3701 default:
3702 FlagVal = 0;
3703 }
3704
3705 // This avoids allowing strings where the same flag bit appears twice.
3706 if (!FlagVal || (Mask & FlagVal))
3707 return -1;
3708 Mask |= FlagVal;
3709 }
3710
3711 // If the register is spsr then we need to set the R bit.
3712 if (Reg == "spsr")
3713 Mask |= 0x10;
3714
3715 return Mask;
3716}
3717
3718// Lower the read_register intrinsic to ARM specific DAG nodes
3719// using the supplied metadata string to select the instruction node to use
3720// and the registers/masks to construct as operands for the node.
Justin Bogner45571362016-05-12 00:31:09 +00003721bool ARMDAGToDAGISel::tryReadRegister(SDNode *N){
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00003722 const MDNodeSDNode *MD = dyn_cast<MDNodeSDNode>(N->getOperand(1));
3723 const MDString *RegString = dyn_cast<MDString>(MD->getMD()->getOperand(0));
3724 bool IsThumb2 = Subtarget->isThumb2();
3725 SDLoc DL(N);
3726
3727 std::vector<SDValue> Ops;
3728 getIntOperandsFromRegisterString(RegString->getString(), CurDAG, DL, Ops);
3729
3730 if (!Ops.empty()) {
3731 // If the special register string was constructed of fields (as defined
3732 // in the ACLE) then need to lower to MRC node (32 bit) or
3733 // MRRC node(64 bit), we can make the distinction based on the number of
3734 // operands we have.
3735 unsigned Opcode;
3736 SmallVector<EVT, 3> ResTypes;
3737 if (Ops.size() == 5){
3738 Opcode = IsThumb2 ? ARM::t2MRC : ARM::MRC;
3739 ResTypes.append({ MVT::i32, MVT::Other });
3740 } else {
3741 assert(Ops.size() == 3 &&
3742 "Invalid number of fields in special register string.");
3743 Opcode = IsThumb2 ? ARM::t2MRRC : ARM::MRRC;
3744 ResTypes.append({ MVT::i32, MVT::i32, MVT::Other });
3745 }
3746
3747 Ops.push_back(getAL(CurDAG, DL));
3748 Ops.push_back(CurDAG->getRegister(0, MVT::i32));
3749 Ops.push_back(N->getOperand(0));
Justin Bogner45571362016-05-12 00:31:09 +00003750 ReplaceNode(N, CurDAG->getMachineNode(Opcode, DL, ResTypes, Ops));
3751 return true;
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00003752 }
3753
3754 std::string SpecialReg = RegString->getString().lower();
3755
3756 int BankedReg = getBankedRegisterMask(SpecialReg);
3757 if (BankedReg != -1) {
3758 Ops = { CurDAG->getTargetConstant(BankedReg, DL, MVT::i32),
3759 getAL(CurDAG, DL), CurDAG->getRegister(0, MVT::i32),
3760 N->getOperand(0) };
Justin Bogner45571362016-05-12 00:31:09 +00003761 ReplaceNode(
3762 N, CurDAG->getMachineNode(IsThumb2 ? ARM::t2MRSbanked : ARM::MRSbanked,
3763 DL, MVT::i32, MVT::Other, Ops));
3764 return true;
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00003765 }
3766
3767 // The VFP registers are read by creating SelectionDAG nodes with opcodes
3768 // corresponding to the register that is being read from. So we switch on the
3769 // string to find which opcode we need to use.
3770 unsigned Opcode = StringSwitch<unsigned>(SpecialReg)
3771 .Case("fpscr", ARM::VMRS)
3772 .Case("fpexc", ARM::VMRS_FPEXC)
3773 .Case("fpsid", ARM::VMRS_FPSID)
3774 .Case("mvfr0", ARM::VMRS_MVFR0)
3775 .Case("mvfr1", ARM::VMRS_MVFR1)
3776 .Case("mvfr2", ARM::VMRS_MVFR2)
3777 .Case("fpinst", ARM::VMRS_FPINST)
3778 .Case("fpinst2", ARM::VMRS_FPINST2)
3779 .Default(0);
3780
3781 // If an opcode was found then we can lower the read to a VFP instruction.
3782 if (Opcode) {
3783 if (!Subtarget->hasVFP2())
Justin Bogner45571362016-05-12 00:31:09 +00003784 return false;
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00003785 if (Opcode == ARM::VMRS_MVFR2 && !Subtarget->hasFPARMv8())
Justin Bogner45571362016-05-12 00:31:09 +00003786 return false;
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00003787
3788 Ops = { getAL(CurDAG, DL), CurDAG->getRegister(0, MVT::i32),
3789 N->getOperand(0) };
Justin Bogner45571362016-05-12 00:31:09 +00003790 ReplaceNode(N,
3791 CurDAG->getMachineNode(Opcode, DL, MVT::i32, MVT::Other, Ops));
3792 return true;
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00003793 }
3794
3795 // If the target is M Class then need to validate that the register string
3796 // is an acceptable value, so check that a mask can be constructed from the
3797 // string.
3798 if (Subtarget->isMClass()) {
Javed Absar2cb0c952017-07-19 12:57:16 +00003799 int SYSmValue = getMClassRegisterMask(SpecialReg, Subtarget);
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00003800 if (SYSmValue == -1)
Justin Bogner45571362016-05-12 00:31:09 +00003801 return false;
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00003802
3803 SDValue Ops[] = { CurDAG->getTargetConstant(SYSmValue, DL, MVT::i32),
3804 getAL(CurDAG, DL), CurDAG->getRegister(0, MVT::i32),
3805 N->getOperand(0) };
Justin Bogner45571362016-05-12 00:31:09 +00003806 ReplaceNode(
3807 N, CurDAG->getMachineNode(ARM::t2MRS_M, DL, MVT::i32, MVT::Other, Ops));
3808 return true;
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00003809 }
3810
3811 // Here we know the target is not M Class so we need to check if it is one
3812 // of the remaining possible values which are apsr, cpsr or spsr.
3813 if (SpecialReg == "apsr" || SpecialReg == "cpsr") {
3814 Ops = { getAL(CurDAG, DL), CurDAG->getRegister(0, MVT::i32),
3815 N->getOperand(0) };
Justin Bogner45571362016-05-12 00:31:09 +00003816 ReplaceNode(N, CurDAG->getMachineNode(IsThumb2 ? ARM::t2MRS_AR : ARM::MRS,
3817 DL, MVT::i32, MVT::Other, Ops));
3818 return true;
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00003819 }
3820
3821 if (SpecialReg == "spsr") {
3822 Ops = { getAL(CurDAG, DL), CurDAG->getRegister(0, MVT::i32),
3823 N->getOperand(0) };
Justin Bogner45571362016-05-12 00:31:09 +00003824 ReplaceNode(
3825 N, CurDAG->getMachineNode(IsThumb2 ? ARM::t2MRSsys_AR : ARM::MRSsys, DL,
3826 MVT::i32, MVT::Other, Ops));
3827 return true;
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00003828 }
3829
Justin Bogner45571362016-05-12 00:31:09 +00003830 return false;
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00003831}
3832
3833// Lower the write_register intrinsic to ARM specific DAG nodes
3834// using the supplied metadata string to select the instruction node to use
3835// and the registers/masks to use in the nodes
Justin Bogner45571362016-05-12 00:31:09 +00003836bool ARMDAGToDAGISel::tryWriteRegister(SDNode *N){
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00003837 const MDNodeSDNode *MD = dyn_cast<MDNodeSDNode>(N->getOperand(1));
3838 const MDString *RegString = dyn_cast<MDString>(MD->getMD()->getOperand(0));
3839 bool IsThumb2 = Subtarget->isThumb2();
3840 SDLoc DL(N);
3841
3842 std::vector<SDValue> Ops;
3843 getIntOperandsFromRegisterString(RegString->getString(), CurDAG, DL, Ops);
3844
3845 if (!Ops.empty()) {
3846 // If the special register string was constructed of fields (as defined
3847 // in the ACLE) then need to lower to MCR node (32 bit) or
3848 // MCRR node(64 bit), we can make the distinction based on the number of
3849 // operands we have.
3850 unsigned Opcode;
3851 if (Ops.size() == 5) {
3852 Opcode = IsThumb2 ? ARM::t2MCR : ARM::MCR;
3853 Ops.insert(Ops.begin()+2, N->getOperand(2));
3854 } else {
3855 assert(Ops.size() == 3 &&
3856 "Invalid number of fields in special register string.");
3857 Opcode = IsThumb2 ? ARM::t2MCRR : ARM::MCRR;
3858 SDValue WriteValue[] = { N->getOperand(2), N->getOperand(3) };
3859 Ops.insert(Ops.begin()+2, WriteValue, WriteValue+2);
3860 }
3861
3862 Ops.push_back(getAL(CurDAG, DL));
3863 Ops.push_back(CurDAG->getRegister(0, MVT::i32));
3864 Ops.push_back(N->getOperand(0));
3865
Justin Bogner45571362016-05-12 00:31:09 +00003866 ReplaceNode(N, CurDAG->getMachineNode(Opcode, DL, MVT::Other, Ops));
3867 return true;
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00003868 }
3869
3870 std::string SpecialReg = RegString->getString().lower();
3871 int BankedReg = getBankedRegisterMask(SpecialReg);
3872 if (BankedReg != -1) {
3873 Ops = { CurDAG->getTargetConstant(BankedReg, DL, MVT::i32), N->getOperand(2),
3874 getAL(CurDAG, DL), CurDAG->getRegister(0, MVT::i32),
3875 N->getOperand(0) };
Justin Bogner45571362016-05-12 00:31:09 +00003876 ReplaceNode(
3877 N, CurDAG->getMachineNode(IsThumb2 ? ARM::t2MSRbanked : ARM::MSRbanked,
3878 DL, MVT::Other, Ops));
3879 return true;
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00003880 }
3881
3882 // The VFP registers are written to by creating SelectionDAG nodes with
3883 // opcodes corresponding to the register that is being written. So we switch
3884 // on the string to find which opcode we need to use.
3885 unsigned Opcode = StringSwitch<unsigned>(SpecialReg)
3886 .Case("fpscr", ARM::VMSR)
3887 .Case("fpexc", ARM::VMSR_FPEXC)
3888 .Case("fpsid", ARM::VMSR_FPSID)
3889 .Case("fpinst", ARM::VMSR_FPINST)
3890 .Case("fpinst2", ARM::VMSR_FPINST2)
3891 .Default(0);
3892
3893 if (Opcode) {
3894 if (!Subtarget->hasVFP2())
Justin Bogner45571362016-05-12 00:31:09 +00003895 return false;
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00003896 Ops = { N->getOperand(2), getAL(CurDAG, DL),
3897 CurDAG->getRegister(0, MVT::i32), N->getOperand(0) };
Justin Bogner45571362016-05-12 00:31:09 +00003898 ReplaceNode(N, CurDAG->getMachineNode(Opcode, DL, MVT::Other, Ops));
3899 return true;
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00003900 }
3901
Bradley Smithf277c8a2016-01-25 11:25:36 +00003902 std::pair<StringRef, StringRef> Fields;
3903 Fields = StringRef(SpecialReg).rsplit('_');
3904 std::string Reg = Fields.first.str();
3905 StringRef Flags = Fields.second;
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00003906
3907 // If the target was M Class then need to validate the special register value
3908 // and retrieve the mask for use in the instruction node.
3909 if (Subtarget->isMClass()) {
Javed Absar2cb0c952017-07-19 12:57:16 +00003910 int SYSmValue = getMClassRegisterMask(SpecialReg, Subtarget);
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00003911 if (SYSmValue == -1)
Justin Bogner45571362016-05-12 00:31:09 +00003912 return false;
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00003913
3914 SDValue Ops[] = { CurDAG->getTargetConstant(SYSmValue, DL, MVT::i32),
3915 N->getOperand(2), getAL(CurDAG, DL),
3916 CurDAG->getRegister(0, MVT::i32), N->getOperand(0) };
Justin Bogner45571362016-05-12 00:31:09 +00003917 ReplaceNode(N, CurDAG->getMachineNode(ARM::t2MSR_M, DL, MVT::Other, Ops));
3918 return true;
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00003919 }
3920
3921 // We then check to see if a valid mask can be constructed for one of the
3922 // register string values permitted for the A and R class cores. These values
3923 // are apsr, spsr and cpsr; these are also valid on older cores.
3924 int Mask = getARClassRegisterMask(Reg, Flags);
3925 if (Mask != -1) {
3926 Ops = { CurDAG->getTargetConstant(Mask, DL, MVT::i32), N->getOperand(2),
3927 getAL(CurDAG, DL), CurDAG->getRegister(0, MVT::i32),
3928 N->getOperand(0) };
Justin Bogner45571362016-05-12 00:31:09 +00003929 ReplaceNode(N, CurDAG->getMachineNode(IsThumb2 ? ARM::t2MSR_AR : ARM::MSR,
3930 DL, MVT::Other, Ops));
3931 return true;
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00003932 }
3933
Justin Bogner45571362016-05-12 00:31:09 +00003934 return false;
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00003935}
3936
Justin Bogner45571362016-05-12 00:31:09 +00003937bool ARMDAGToDAGISel::tryInlineAsm(SDNode *N){
Weiming Zhaoc5987002013-02-14 18:10:21 +00003938 std::vector<SDValue> AsmNodeOperands;
3939 unsigned Flag, Kind;
3940 bool Changed = false;
3941 unsigned NumOps = N->getNumOperands();
3942
Weiming Zhaoc5987002013-02-14 18:10:21 +00003943 // Normally, i64 data is bounded to two arbitrary GRPs for "%r" constraint.
3944 // However, some instrstions (e.g. ldrexd/strexd in ARM mode) require
3945 // (even/even+1) GPRs and use %n and %Hn to refer to the individual regs
3946 // respectively. Since there is no constraint to explicitly specify a
Weiming Zhaoa3d87a12013-06-28 17:26:02 +00003947 // reg pair, we use GPRPair reg class for "%r" for 64-bit data. For Thumb,
3948 // the 64-bit data may be referred by H, Q, R modifiers, so we still pack
3949 // them into a GPRPair.
Weiming Zhaoc5987002013-02-14 18:10:21 +00003950
Andrew Trickef9de2a2013-05-25 02:42:55 +00003951 SDLoc dl(N);
Craig Topper062a2ba2014-04-25 05:30:21 +00003952 SDValue Glue = N->getGluedNode() ? N->getOperand(NumOps-1)
3953 : SDValue(nullptr,0);
Weiming Zhaoc5987002013-02-14 18:10:21 +00003954
Weiming Zhaoa3d87a12013-06-28 17:26:02 +00003955 SmallVector<bool, 8> OpChanged;
Weiming Zhaoc5987002013-02-14 18:10:21 +00003956 // Glue node will be appended late.
Weiming Zhaoa3d87a12013-06-28 17:26:02 +00003957 for(unsigned i = 0, e = N->getGluedNode() ? NumOps - 1 : NumOps; i < e; ++i) {
Weiming Zhaoc5987002013-02-14 18:10:21 +00003958 SDValue op = N->getOperand(i);
3959 AsmNodeOperands.push_back(op);
3960
3961 if (i < InlineAsm::Op_FirstOperand)
3962 continue;
3963
3964 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(i))) {
3965 Flag = C->getZExtValue();
3966 Kind = InlineAsm::getKind(Flag);
3967 }
3968 else
3969 continue;
3970
Joey Gouly392cdad2013-07-08 19:52:51 +00003971 // Immediate operands to inline asm in the SelectionDAG are modeled with
3972 // two operands. The first is a constant of value InlineAsm::Kind_Imm, and
3973 // the second is a constant with the value of the immediate. If we get here
3974 // and we have a Kind_Imm, skip the next operand, and continue.
Joey Gouly606f3fb2013-07-05 10:19:40 +00003975 if (Kind == InlineAsm::Kind_Imm) {
3976 SDValue op = N->getOperand(++i);
3977 AsmNodeOperands.push_back(op);
3978 continue;
3979 }
3980
Weiming Zhaoa3d87a12013-06-28 17:26:02 +00003981 unsigned NumRegs = InlineAsm::getNumOperandRegisters(Flag);
3982 if (NumRegs)
3983 OpChanged.push_back(false);
3984
3985 unsigned DefIdx = 0;
3986 bool IsTiedToChangedOp = false;
3987 // If it's a use that is tied with a previous def, it has no
3988 // reg class constraint.
3989 if (Changed && InlineAsm::isUseOperandTiedToDef(Flag, DefIdx))
3990 IsTiedToChangedOp = OpChanged[DefIdx];
3991
Diana Picusf345d402016-07-20 09:48:24 +00003992 // Memory operands to inline asm in the SelectionDAG are modeled with two
3993 // operands: a constant of value InlineAsm::Kind_Mem followed by the input
3994 // operand. If we get here and we have a Kind_Mem, skip the next operand (so
3995 // it doesn't get misinterpreted), and continue. We do this here because
3996 // it's important to update the OpChanged array correctly before moving on.
3997 if (Kind == InlineAsm::Kind_Mem) {
3998 SDValue op = N->getOperand(++i);
3999 AsmNodeOperands.push_back(op);
4000 continue;
4001 }
4002
Weiming Zhaoc5987002013-02-14 18:10:21 +00004003 if (Kind != InlineAsm::Kind_RegUse && Kind != InlineAsm::Kind_RegDef
4004 && Kind != InlineAsm::Kind_RegDefEarlyClobber)
4005 continue;
4006
Weiming Zhaoc5987002013-02-14 18:10:21 +00004007 unsigned RC;
4008 bool HasRC = InlineAsm::hasRegClassConstraint(Flag, RC);
Weiming Zhaoa3d87a12013-06-28 17:26:02 +00004009 if ((!IsTiedToChangedOp && (!HasRC || RC != ARM::GPRRegClassID))
4010 || NumRegs != 2)
Weiming Zhaoc5987002013-02-14 18:10:21 +00004011 continue;
4012
Weiming Zhaoa3d87a12013-06-28 17:26:02 +00004013 assert((i+2 < NumOps) && "Invalid number of operands in inline asm");
Weiming Zhaoc5987002013-02-14 18:10:21 +00004014 SDValue V0 = N->getOperand(i+1);
4015 SDValue V1 = N->getOperand(i+2);
4016 unsigned Reg0 = cast<RegisterSDNode>(V0)->getReg();
4017 unsigned Reg1 = cast<RegisterSDNode>(V1)->getReg();
4018 SDValue PairedReg;
4019 MachineRegisterInfo &MRI = MF->getRegInfo();
4020
4021 if (Kind == InlineAsm::Kind_RegDef ||
4022 Kind == InlineAsm::Kind_RegDefEarlyClobber) {
4023 // Replace the two GPRs with 1 GPRPair and copy values from GPRPair to
4024 // the original GPRs.
4025
4026 unsigned GPVR = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
4027 PairedReg = CurDAG->getRegister(GPVR, MVT::Untyped);
4028 SDValue Chain = SDValue(N,0);
4029
4030 SDNode *GU = N->getGluedUser();
4031 SDValue RegCopy = CurDAG->getCopyFromReg(Chain, dl, GPVR, MVT::Untyped,
4032 Chain.getValue(1));
4033
4034 // Extract values from a GPRPair reg and copy to the original GPR reg.
4035 SDValue Sub0 = CurDAG->getTargetExtractSubreg(ARM::gsub_0, dl, MVT::i32,
4036 RegCopy);
4037 SDValue Sub1 = CurDAG->getTargetExtractSubreg(ARM::gsub_1, dl, MVT::i32,
4038 RegCopy);
4039 SDValue T0 = CurDAG->getCopyToReg(Sub0, dl, Reg0, Sub0,
4040 RegCopy.getValue(1));
4041 SDValue T1 = CurDAG->getCopyToReg(Sub1, dl, Reg1, Sub1, T0.getValue(1));
4042
4043 // Update the original glue user.
4044 std::vector<SDValue> Ops(GU->op_begin(), GU->op_end()-1);
4045 Ops.push_back(T1.getValue(1));
Craig Topper8c0b4d02014-04-28 05:57:50 +00004046 CurDAG->UpdateNodeOperands(GU, Ops);
Weiming Zhaoc5987002013-02-14 18:10:21 +00004047 }
4048 else {
4049 // For Kind == InlineAsm::Kind_RegUse, we first copy two GPRs into a
4050 // GPRPair and then pass the GPRPair to the inline asm.
4051 SDValue Chain = AsmNodeOperands[InlineAsm::Op_InputChain];
4052
4053 // As REG_SEQ doesn't take RegisterSDNode, we copy them first.
4054 SDValue T0 = CurDAG->getCopyFromReg(Chain, dl, Reg0, MVT::i32,
4055 Chain.getValue(1));
4056 SDValue T1 = CurDAG->getCopyFromReg(Chain, dl, Reg1, MVT::i32,
4057 T0.getValue(1));
4058 SDValue Pair = SDValue(createGPRPairNode(MVT::Untyped, T0, T1), 0);
4059
4060 // Copy REG_SEQ into a GPRPair-typed VR and replace the original two
4061 // i32 VRs of inline asm with it.
4062 unsigned GPVR = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
4063 PairedReg = CurDAG->getRegister(GPVR, MVT::Untyped);
4064 Chain = CurDAG->getCopyToReg(T1, dl, GPVR, Pair, T1.getValue(1));
4065
4066 AsmNodeOperands[InlineAsm::Op_InputChain] = Chain;
4067 Glue = Chain.getValue(1);
4068 }
4069
4070 Changed = true;
4071
4072 if(PairedReg.getNode()) {
Weiming Zhaoa3d87a12013-06-28 17:26:02 +00004073 OpChanged[OpChanged.size() -1 ] = true;
Weiming Zhaoc5987002013-02-14 18:10:21 +00004074 Flag = InlineAsm::getFlagWord(Kind, 1 /* RegNum*/);
Tim Northover55349a22013-08-18 18:06:03 +00004075 if (IsTiedToChangedOp)
4076 Flag = InlineAsm::getFlagWordForMatchingOp(Flag, DefIdx);
4077 else
4078 Flag = InlineAsm::getFlagWordForRegClass(Flag, ARM::GPRPairRegClassID);
Weiming Zhaoc5987002013-02-14 18:10:21 +00004079 // Replace the current flag.
4080 AsmNodeOperands[AsmNodeOperands.size() -1] = CurDAG->getTargetConstant(
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004081 Flag, dl, MVT::i32);
Weiming Zhaoc5987002013-02-14 18:10:21 +00004082 // Add the new register node and skip the original two GPRs.
4083 AsmNodeOperands.push_back(PairedReg);
4084 // Skip the next two GPRs.
4085 i += 2;
4086 }
4087 }
4088
Weiming Zhaoa3d87a12013-06-28 17:26:02 +00004089 if (Glue.getNode())
4090 AsmNodeOperands.push_back(Glue);
Weiming Zhaoc5987002013-02-14 18:10:21 +00004091 if (!Changed)
Justin Bogner45571362016-05-12 00:31:09 +00004092 return false;
Weiming Zhaoc5987002013-02-14 18:10:21 +00004093
Andrew Trickef9de2a2013-05-25 02:42:55 +00004094 SDValue New = CurDAG->getNode(ISD::INLINEASM, SDLoc(N),
Craig Topper48d114b2014-04-26 18:35:24 +00004095 CurDAG->getVTList(MVT::Other, MVT::Glue), AsmNodeOperands);
Weiming Zhaoc5987002013-02-14 18:10:21 +00004096 New->setNodeId(-1);
Justin Bogner45571362016-05-12 00:31:09 +00004097 ReplaceNode(N, New.getNode());
4098 return true;
Weiming Zhaoc5987002013-02-14 18:10:21 +00004099}
4100
4101
Bob Wilsona2c462b2009-05-19 05:53:42 +00004102bool ARMDAGToDAGISel::
Daniel Sanders60f1db02015-03-13 12:45:09 +00004103SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID,
Bob Wilsona2c462b2009-05-19 05:53:42 +00004104 std::vector<SDValue> &OutOps) {
Daniel Sanders1f58ef72015-06-03 12:33:56 +00004105 switch(ConstraintID) {
4106 default:
4107 llvm_unreachable("Unexpected asm memory constraint");
Daniel Sanders43a79bf2015-06-03 14:17:18 +00004108 case InlineAsm::Constraint_i:
4109 // FIXME: It seems strange that 'i' is needed here since it's supposed to
4110 // be an immediate and not a memory constraint.
Justin Bognerb03fd122016-08-17 05:10:15 +00004111 LLVM_FALLTHROUGH;
Daniel Sanders1f58ef72015-06-03 12:33:56 +00004112 case InlineAsm::Constraint_m:
James Molloy72222f52015-10-26 10:04:52 +00004113 case InlineAsm::Constraint_o:
Daniel Sanders1f58ef72015-06-03 12:33:56 +00004114 case InlineAsm::Constraint_Q:
4115 case InlineAsm::Constraint_Um:
4116 case InlineAsm::Constraint_Un:
4117 case InlineAsm::Constraint_Uq:
4118 case InlineAsm::Constraint_Us:
4119 case InlineAsm::Constraint_Ut:
4120 case InlineAsm::Constraint_Uv:
4121 case InlineAsm::Constraint_Uy:
4122 // Require the address to be in a register. That is safe for all ARM
4123 // variants and it is hard to do anything much smarter without knowing
4124 // how the operand is used.
4125 OutOps.push_back(Op);
4126 return false;
4127 }
4128 return true;
Bob Wilsona2c462b2009-05-19 05:53:42 +00004129}
4130
Rafael Espindolaffdc24b2006-05-14 22:18:28 +00004131/// createARMISelDag - This pass converts a legalized DAG into a
4132/// ARM-specific DAG, ready for instruction scheduling.
4133///
Bob Wilson2dd957f2009-09-28 14:30:20 +00004134FunctionPass *llvm::createARMISelDag(ARMBaseTargetMachine &TM,
4135 CodeGenOpt::Level OptLevel) {
4136 return new ARMDAGToDAGISel(TM, OptLevel);
Rafael Espindolaffdc24b2006-05-14 22:18:28 +00004137}