blob: 6497a3e936740a6e157b0990db983aec132400a3 [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- HexagonISelDAGToDAG.cpp - A dag to dag inst selector for Hexagon --===//
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines an instruction selector for the Hexagon target.
11//
12//===----------------------------------------------------------------------===//
13
Chandler Carruthed0881b2012-12-03 16:50:05 +000014#include "Hexagon.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000015#include "HexagonISelLowering.h"
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +000016#include "HexagonMachineFunctionInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000017#include "HexagonTargetMachine.h"
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +000018#include "llvm/CodeGen/FunctionLoweringInfo.h"
19#include "llvm/CodeGen/MachineInstrBuilder.h"
Jyotsna Vermad9225242013-02-13 21:38:46 +000020#include "llvm/CodeGen/SelectionDAGISel.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000021#include "llvm/IR/Intrinsics.h"
Jyotsna Vermad9225242013-02-13 21:38:46 +000022#include "llvm/Support/CommandLine.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000023#include "llvm/Support/Debug.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000024using namespace llvm;
25
Chandler Carruth84e68b22014-04-22 02:41:26 +000026#define DEBUG_TYPE "hexagon-isel"
27
Jyotsna Vermad9225242013-02-13 21:38:46 +000028static
29cl::opt<unsigned>
30MaxNumOfUsesForConstExtenders("ga-max-num-uses-for-constant-extenders",
31 cl::Hidden, cl::init(2),
32 cl::desc("Maximum number of uses of a global address such that we still us a"
33 "constant extended instruction"));
Tony Linthicum1213a7a2011-12-12 21:14:40 +000034
Krzysztof Parzyszek0006e1a2016-07-29 15:15:35 +000035static
36cl::opt<bool>
37EnableAddressRebalancing("isel-rebalance-addr", cl::Hidden, cl::init(true),
38 cl::desc("Rebalance address calculation trees to improve "
39 "instruction selection"));
40
41// Rebalance only if this allows e.g. combining a GA with an offset or
42// factoring out a shift.
43static
44cl::opt<bool>
45RebalanceOnlyForOptimizations("rebalance-only-opt", cl::Hidden, cl::init(false),
46 cl::desc("Rebalance address tree only if this allows optimizations"));
47
48static
49cl::opt<bool>
50RebalanceOnlyImbalancedTrees("rebalance-only-imbal", cl::Hidden,
51 cl::init(false), cl::desc("Rebalance address tree only if it is imbalanced"));
52
Tony Linthicum1213a7a2011-12-12 21:14:40 +000053//===----------------------------------------------------------------------===//
54// Instruction Selector Implementation
55//===----------------------------------------------------------------------===//
56
57//===--------------------------------------------------------------------===//
58/// HexagonDAGToDAGISel - Hexagon specific code to select Hexagon machine
59/// instructions for SelectionDAG operations.
60///
61namespace {
62class HexagonDAGToDAGISel : public SelectionDAGISel {
Eric Christopher23a7d1e2015-03-21 03:12:59 +000063 const HexagonSubtarget *HST;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +000064 const HexagonInstrInfo *HII;
65 const HexagonRegisterInfo *HRI;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000066public:
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +000067 explicit HexagonDAGToDAGISel(HexagonTargetMachine &tm,
Jyotsna Vermad9225242013-02-13 21:38:46 +000068 CodeGenOpt::Level OptLevel)
Krzysztof Parzyszek0006e1a2016-07-29 15:15:35 +000069 : SelectionDAGISel(tm, OptLevel), HST(nullptr), HII(nullptr),
Chandler Carruth9ac86ef2016-06-03 10:13:31 +000070 HRI(nullptr) {}
Eric Christopher23a7d1e2015-03-21 03:12:59 +000071
72 bool runOnMachineFunction(MachineFunction &MF) override {
73 // Reset the subtarget each time through.
74 HST = &MF.getSubtarget<HexagonSubtarget>();
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +000075 HII = HST->getInstrInfo();
76 HRI = HST->getRegisterInfo();
Eric Christopher23a7d1e2015-03-21 03:12:59 +000077 SelectionDAGISel::runOnMachineFunction(MF);
78 return true;
79 }
80
Krzysztof Parzyszekae14e7b2015-03-17 21:47:16 +000081 virtual void PreprocessISelDAG() override;
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +000082 virtual void EmitFunctionEntryCode() override;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000083
Justin Bognerec37a022016-05-12 21:46:18 +000084 void Select(SDNode *N) override;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000085
86 // Complex Pattern Selectors.
Colin LeMahieu987b0942015-02-04 20:38:01 +000087 inline bool SelectAddrGA(SDValue &N, SDValue &R);
Colin LeMahieu51491352015-02-04 22:36:28 +000088 inline bool SelectAddrGP(SDValue &N, SDValue &R);
Colin LeMahieu987b0942015-02-04 20:38:01 +000089 bool SelectGlobalAddress(SDValue &N, SDValue &R, bool UseGP);
Colin LeMahieuc7522f32015-01-14 23:07:36 +000090 bool SelectAddrFI(SDValue &N, SDValue &R);
91
Craig Topper906c2cd2014-04-29 07:58:16 +000092 const char *getPassName() const override {
Tony Linthicum1213a7a2011-12-12 21:14:40 +000093 return "Hexagon DAG->DAG Pattern Instruction Selection";
94 }
95
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +000096 // Generate a machine instruction node corresponding to the circ/brev
97 // load intrinsic.
98 MachineSDNode *LoadInstrForLoadIntrinsic(SDNode *IntN);
99 // Given the circ/brev load intrinsic and the already generated machine
100 // instruction, generate the appropriate store (that is a part of the
101 // intrinsic's functionality).
102 SDNode *StoreInstrForLoadIntrinsic(MachineSDNode *LoadN, SDNode *IntN);
103
Justin Bognerec37a022016-05-12 21:46:18 +0000104 void SelectFrameIndex(SDNode *N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000105 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
106 /// inline asm expressions.
Craig Topper906c2cd2014-04-29 07:58:16 +0000107 bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Daniel Sanders60f1db02015-03-13 12:45:09 +0000108 unsigned ConstraintID,
Craig Topper906c2cd2014-04-29 07:58:16 +0000109 std::vector<SDValue> &OutOps) override;
Justin Bognerec37a022016-05-12 21:46:18 +0000110 bool tryLoadOfLoadIntrinsic(LoadSDNode *N);
111 void SelectLoad(SDNode *N);
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000112 void SelectIndexedLoad(LoadSDNode *LD, const SDLoc &dl);
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000113 void SelectIndexedStore(StoreSDNode *ST, const SDLoc &dl);
Justin Bognerec37a022016-05-12 21:46:18 +0000114 void SelectStore(SDNode *N);
115 void SelectSHL(SDNode *N);
116 void SelectMul(SDNode *N);
117 void SelectZeroExtend(SDNode *N);
118 void SelectIntrinsicWChain(SDNode *N);
119 void SelectIntrinsicWOChain(SDNode *N);
120 void SelectConstant(SDNode *N);
121 void SelectConstantFP(SDNode *N);
122 void SelectAdd(SDNode *N);
Krzysztof Parzyszek5da24e52016-06-27 15:08:22 +0000123 void SelectBitcast(SDNode *N);
Justin Bognerec37a022016-05-12 21:46:18 +0000124 void SelectBitOp(SDNode *N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000125
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000126 // XformMskToBitPosU5Imm - Returns the bit position which
127 // the single bit 32 bit mask represents.
128 // Used in Clr and Set bit immediate memops.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000129 SDValue XformMskToBitPosU5Imm(uint32_t Imm, const SDLoc &DL) {
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000130 int32_t bitPos;
131 bitPos = Log2_32(Imm);
132 assert(bitPos >= 0 && bitPos < 32 &&
133 "Constant out of range for 32 BitPos Memops");
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000134 return CurDAG->getTargetConstant(bitPos, DL, MVT::i32);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000135 }
Jyotsna Vermafdc660b2013-03-22 18:41:34 +0000136
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000137 // XformMskToBitPosU4Imm - Returns the bit position which the single-bit
138 // 16 bit mask represents. Used in Clr and Set bit immediate memops.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000139 SDValue XformMskToBitPosU4Imm(uint16_t Imm, const SDLoc &DL) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000140 return XformMskToBitPosU5Imm(Imm, DL);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000141 }
Jyotsna Vermafdc660b2013-03-22 18:41:34 +0000142
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000143 // XformMskToBitPosU3Imm - Returns the bit position which the single-bit
144 // 8 bit mask represents. Used in Clr and Set bit immediate memops.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000145 SDValue XformMskToBitPosU3Imm(uint8_t Imm, const SDLoc &DL) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000146 return XformMskToBitPosU5Imm(Imm, DL);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000147 }
Jyotsna Vermafdc660b2013-03-22 18:41:34 +0000148
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000149 // Return true if there is exactly one bit set in V, i.e., if V is one of the
150 // following integers: 2^0, 2^1, ..., 2^31.
151 bool ImmIsSingleBit(uint32_t v) const {
152 return isPowerOf2_32(v);
153 }
Jyotsna Vermafdc660b2013-03-22 18:41:34 +0000154
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000155 // XformM5ToU5Imm - Return a target constant with the specified value, of
156 // type i32 where the negative literal is transformed into a positive literal
157 // for use in -= memops.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000158 inline SDValue XformM5ToU5Imm(signed Imm, const SDLoc &DL) {
159 assert((Imm >= -31 && Imm <= -1) && "Constant out of range for Memops");
160 return CurDAG->getTargetConstant(-Imm, DL, MVT::i32);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000161 }
Jyotsna Vermafdc660b2013-03-22 18:41:34 +0000162
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000163 // XformU7ToU7M1Imm - Return a target constant decremented by 1, in range
164 // [1..128], used in cmpb.gtu instructions.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000165 inline SDValue XformU7ToU7M1Imm(signed Imm, const SDLoc &DL) {
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000166 assert((Imm >= 1 && Imm <= 128) && "Constant out of range for cmpb op");
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000167 return CurDAG->getTargetConstant(Imm - 1, DL, MVT::i8);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000168 }
Jyotsna Vermafdc660b2013-03-22 18:41:34 +0000169
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000170 // XformS8ToS8M1Imm - Return a target constant decremented by 1.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000171 inline SDValue XformSToSM1Imm(signed Imm, const SDLoc &DL) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000172 return CurDAG->getTargetConstant(Imm - 1, DL, MVT::i32);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000173 }
Jyotsna Verma60316252013-02-05 19:20:45 +0000174
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000175 // XformU8ToU8M1Imm - Return a target constant decremented by 1.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000176 inline SDValue XformUToUM1Imm(unsigned Imm, const SDLoc &DL) {
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000177 assert((Imm >= 1) && "Cannot decrement unsigned int less than 1");
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000178 return CurDAG->getTargetConstant(Imm - 1, DL, MVT::i32);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000179 }
Jyotsna Verma89c84822013-04-23 19:15:55 +0000180
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000181 // XformSToSM2Imm - Return a target constant decremented by 2.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000182 inline SDValue XformSToSM2Imm(unsigned Imm, const SDLoc &DL) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000183 return CurDAG->getTargetConstant(Imm - 2, DL, MVT::i32);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000184 }
Jyotsna Verma89c84822013-04-23 19:15:55 +0000185
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000186 // XformSToSM3Imm - Return a target constant decremented by 3.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000187 inline SDValue XformSToSM3Imm(unsigned Imm, const SDLoc &DL) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000188 return CurDAG->getTargetConstant(Imm - 3, DL, MVT::i32);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000189 }
Colin LeMahieu19ed07c2015-01-28 18:29:11 +0000190
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000191 // Include the pieces autogenerated from the target description.
192 #include "HexagonGenDAGISel.inc"
Colin LeMahieu0ee02fc2015-01-19 20:31:18 +0000193
194private:
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000195 bool isValueExtension(const SDValue &Val, unsigned FromBits, SDValue &Src);
Krzysztof Parzyszekbba0bf72016-07-15 15:35:52 +0000196 bool orIsAdd(const SDNode *N) const;
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +0000197 bool isAlignedMemNode(const MemSDNode *N) const;
Krzysztof Parzyszek0006e1a2016-07-29 15:15:35 +0000198
199 SmallDenseMap<SDNode *,int> RootWeights;
200 SmallDenseMap<SDNode *,int> RootHeights;
201 SmallDenseMap<const Value *,int> GAUsesInFunction;
202 int getWeight(SDNode *N);
203 int getHeight(SDNode *N);
204 SDValue getMultiplierForSHL(SDNode *N);
205 SDValue factorOutPowerOf2(SDValue V, unsigned Power);
206 unsigned getUsesInFunction(const Value *V);
207 SDValue balanceSubTree(SDNode *N, bool Factorize = false);
208 void rebalanceAddressTrees();
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000209}; // end HexagonDAGToDAGISel
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000210} // end anonymous namespace
211
212
213/// createHexagonISelDag - This pass converts a legalized DAG into a
214/// Hexagon-specific DAG, ready for instruction scheduling.
215///
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000216namespace llvm {
217FunctionPass *createHexagonISelDag(HexagonTargetMachine &TM,
218 CodeGenOpt::Level OptLevel) {
Jyotsna Vermad9225242013-02-13 21:38:46 +0000219 return new HexagonDAGToDAGISel(TM, OptLevel);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000220}
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000221}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000222
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000223// Intrinsics that return a a predicate.
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000224static bool doesIntrinsicReturnPredicate(unsigned ID) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000225 switch (ID) {
226 default:
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000227 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000228 case Intrinsic::hexagon_C2_cmpeq:
229 case Intrinsic::hexagon_C2_cmpgt:
230 case Intrinsic::hexagon_C2_cmpgtu:
231 case Intrinsic::hexagon_C2_cmpgtup:
232 case Intrinsic::hexagon_C2_cmpgtp:
233 case Intrinsic::hexagon_C2_cmpeqp:
234 case Intrinsic::hexagon_C2_bitsset:
235 case Intrinsic::hexagon_C2_bitsclr:
236 case Intrinsic::hexagon_C2_cmpeqi:
237 case Intrinsic::hexagon_C2_cmpgti:
238 case Intrinsic::hexagon_C2_cmpgtui:
239 case Intrinsic::hexagon_C2_cmpgei:
240 case Intrinsic::hexagon_C2_cmpgeui:
241 case Intrinsic::hexagon_C2_cmplt:
242 case Intrinsic::hexagon_C2_cmpltu:
243 case Intrinsic::hexagon_C2_bitsclri:
244 case Intrinsic::hexagon_C2_and:
245 case Intrinsic::hexagon_C2_or:
246 case Intrinsic::hexagon_C2_xor:
247 case Intrinsic::hexagon_C2_andn:
248 case Intrinsic::hexagon_C2_not:
249 case Intrinsic::hexagon_C2_orn:
250 case Intrinsic::hexagon_C2_pxfer_map:
251 case Intrinsic::hexagon_C2_any8:
252 case Intrinsic::hexagon_C2_all8:
253 case Intrinsic::hexagon_A2_vcmpbeq:
254 case Intrinsic::hexagon_A2_vcmpbgtu:
255 case Intrinsic::hexagon_A2_vcmpheq:
256 case Intrinsic::hexagon_A2_vcmphgt:
257 case Intrinsic::hexagon_A2_vcmphgtu:
258 case Intrinsic::hexagon_A2_vcmpweq:
259 case Intrinsic::hexagon_A2_vcmpwgt:
260 case Intrinsic::hexagon_A2_vcmpwgtu:
261 case Intrinsic::hexagon_C2_tfrrp:
262 case Intrinsic::hexagon_S2_tstbit_i:
263 case Intrinsic::hexagon_S2_tstbit_r:
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000264 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000265 }
266}
267
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000268void HexagonDAGToDAGISel::SelectIndexedLoad(LoadSDNode *LD, const SDLoc &dl) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000269 SDValue Chain = LD->getChain();
270 SDValue Base = LD->getBasePtr();
271 SDValue Offset = LD->getOffset();
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000272 int32_t Inc = cast<ConstantSDNode>(Offset.getNode())->getSExtValue();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000273 EVT LoadedVT = LD->getMemoryVT();
274 unsigned Opcode = 0;
275
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000276 // Check for zero extended loads. Treat any-extend loads as zero extended
277 // loads.
278 ISD::LoadExtType ExtType = LD->getExtensionType();
279 bool IsZeroExt = (ExtType == ISD::ZEXTLOAD || ExtType == ISD::EXTLOAD);
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000280 bool IsValidInc = HII->isValidAutoIncImm(LoadedVT, Inc);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000281
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000282 assert(LoadedVT.isSimple());
283 switch (LoadedVT.getSimpleVT().SimpleTy) {
284 case MVT::i8:
285 if (IsZeroExt)
286 Opcode = IsValidInc ? Hexagon::L2_loadrub_pi : Hexagon::L2_loadrub_io;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000287 else
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000288 Opcode = IsValidInc ? Hexagon::L2_loadrb_pi : Hexagon::L2_loadrb_io;
289 break;
290 case MVT::i16:
291 if (IsZeroExt)
292 Opcode = IsValidInc ? Hexagon::L2_loadruh_pi : Hexagon::L2_loadruh_io;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000293 else
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000294 Opcode = IsValidInc ? Hexagon::L2_loadrh_pi : Hexagon::L2_loadrh_io;
295 break;
296 case MVT::i32:
297 Opcode = IsValidInc ? Hexagon::L2_loadri_pi : Hexagon::L2_loadri_io;
298 break;
299 case MVT::i64:
300 Opcode = IsValidInc ? Hexagon::L2_loadrd_pi : Hexagon::L2_loadrd_io;
301 break;
302 // 64B
303 case MVT::v64i8:
304 case MVT::v32i16:
305 case MVT::v16i32:
306 case MVT::v8i64:
307 if (isAlignedMemNode(LD))
308 Opcode = IsValidInc ? Hexagon::V6_vL32b_pi : Hexagon::V6_vL32b_ai;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000309 else
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000310 Opcode = IsValidInc ? Hexagon::V6_vL32Ub_pi : Hexagon::V6_vL32Ub_ai;
311 break;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000312 // 128B
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000313 case MVT::v128i8:
314 case MVT::v64i16:
315 case MVT::v32i32:
316 case MVT::v16i64:
317 if (isAlignedMemNode(LD))
318 Opcode = IsValidInc ? Hexagon::V6_vL32b_pi_128B
319 : Hexagon::V6_vL32b_ai_128B;
320 else
321 Opcode = IsValidInc ? Hexagon::V6_vL32Ub_pi_128B
322 : Hexagon::V6_vL32Ub_ai_128B;
323 break;
324 default:
325 llvm_unreachable("Unexpected memory type in indexed load");
Justin Bognerec37a022016-05-12 21:46:18 +0000326 }
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000327
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000328 SDValue IncV = CurDAG->getTargetConstant(Inc, dl, MVT::i32);
329 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
330 MemOp[0] = LD->getMemOperand();
331
332 auto getExt64 = [this,ExtType] (MachineSDNode *N, const SDLoc &dl)
333 -> MachineSDNode* {
334 if (ExtType == ISD::ZEXTLOAD || ExtType == ISD::EXTLOAD) {
335 SDValue Zero = CurDAG->getTargetConstant(0, dl, MVT::i32);
336 return CurDAG->getMachineNode(Hexagon::A4_combineir, dl, MVT::i64,
337 Zero, SDValue(N, 0));
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000338 }
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000339 if (ExtType == ISD::SEXTLOAD)
340 return CurDAG->getMachineNode(Hexagon::A2_sxtw, dl, MVT::i64,
341 SDValue(N, 0));
342 return N;
343 };
344
345 // Loaded value Next address Chain
346 SDValue From[3] = { SDValue(LD,0), SDValue(LD,1), SDValue(LD,2) };
347 SDValue To[3];
348
349 EVT ValueVT = LD->getValueType(0);
350 if (ValueVT == MVT::i64 && ExtType != ISD::NON_EXTLOAD) {
351 // A load extending to i64 will actually produce i32, which will then
352 // need to be extended to i64.
353 assert(LoadedVT.getSizeInBits() <= 32);
354 ValueVT = MVT::i32;
355 }
356
357 if (IsValidInc) {
358 MachineSDNode *L = CurDAG->getMachineNode(Opcode, dl, ValueVT,
359 MVT::i32, MVT::Other, Base,
360 IncV, Chain);
361 L->setMemRefs(MemOp, MemOp+1);
362 To[1] = SDValue(L, 1); // Next address.
363 To[2] = SDValue(L, 2); // Chain.
364 // Handle special case for extension to i64.
365 if (LD->getValueType(0) == MVT::i64)
366 L = getExt64(L, dl);
367 To[0] = SDValue(L, 0); // Loaded (extended) value.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000368 } else {
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000369 SDValue Zero = CurDAG->getTargetConstant(0, dl, MVT::i32);
370 MachineSDNode *L = CurDAG->getMachineNode(Opcode, dl, ValueVT, MVT::Other,
371 Base, Zero, Chain);
372 L->setMemRefs(MemOp, MemOp+1);
373 To[2] = SDValue(L, 1); // Chain.
374 MachineSDNode *A = CurDAG->getMachineNode(Hexagon::A2_addi, dl, MVT::i32,
375 Base, IncV);
376 To[1] = SDValue(A, 0); // Next address.
377 // Handle special case for extension to i64.
378 if (LD->getValueType(0) == MVT::i64)
379 L = getExt64(L, dl);
380 To[0] = SDValue(L, 0); // Loaded (extended) value.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000381 }
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000382 ReplaceUses(From, To, 3);
383 CurDAG->RemoveDeadNode(LD);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000384}
385
386
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +0000387MachineSDNode *HexagonDAGToDAGISel::LoadInstrForLoadIntrinsic(SDNode *IntN) {
388 if (IntN->getOpcode() != ISD::INTRINSIC_W_CHAIN)
389 return nullptr;
390
391 SDLoc dl(IntN);
392 unsigned IntNo = cast<ConstantSDNode>(IntN->getOperand(1))->getZExtValue();
393
394 static std::map<unsigned,unsigned> LoadPciMap = {
395 { Intrinsic::hexagon_circ_ldb, Hexagon::L2_loadrb_pci },
396 { Intrinsic::hexagon_circ_ldub, Hexagon::L2_loadrub_pci },
397 { Intrinsic::hexagon_circ_ldh, Hexagon::L2_loadrh_pci },
398 { Intrinsic::hexagon_circ_lduh, Hexagon::L2_loadruh_pci },
399 { Intrinsic::hexagon_circ_ldw, Hexagon::L2_loadri_pci },
400 { Intrinsic::hexagon_circ_ldd, Hexagon::L2_loadrd_pci },
401 };
402 auto FLC = LoadPciMap.find(IntNo);
403 if (FLC != LoadPciMap.end()) {
404 SDNode *Mod = CurDAG->getMachineNode(Hexagon::A2_tfrrcr, dl, MVT::i32,
405 IntN->getOperand(4));
406 EVT ValTy = (IntNo == Intrinsic::hexagon_circ_ldd) ? MVT::i64 : MVT::i32;
407 EVT RTys[] = { ValTy, MVT::i32, MVT::Other };
408 // Operands: { Base, Increment, Modifier, Chain }
409 auto Inc = cast<ConstantSDNode>(IntN->getOperand(5));
410 SDValue I = CurDAG->getTargetConstant(Inc->getSExtValue(), dl, MVT::i32);
411 MachineSDNode *Res = CurDAG->getMachineNode(FLC->second, dl, RTys,
412 { IntN->getOperand(2), I, SDValue(Mod,0), IntN->getOperand(0) });
413 return Res;
414 }
415
416 static std::map<unsigned,unsigned> LoadPbrMap = {
417 { Intrinsic::hexagon_brev_ldb, Hexagon::L2_loadrb_pbr },
418 { Intrinsic::hexagon_brev_ldub, Hexagon::L2_loadrub_pbr },
419 { Intrinsic::hexagon_brev_ldh, Hexagon::L2_loadrh_pbr },
420 { Intrinsic::hexagon_brev_lduh, Hexagon::L2_loadruh_pbr },
421 { Intrinsic::hexagon_brev_ldw, Hexagon::L2_loadri_pbr },
422 { Intrinsic::hexagon_brev_ldd, Hexagon::L2_loadrd_pbr },
423 };
424 auto FLB = LoadPbrMap.find(IntNo);
425 if (FLB != LoadPbrMap.end()) {
426 SDNode *Mod = CurDAG->getMachineNode(Hexagon::A2_tfrrcr, dl, MVT::i32,
427 IntN->getOperand(4));
428 EVT ValTy = (IntNo == Intrinsic::hexagon_brev_ldd) ? MVT::i64 : MVT::i32;
429 EVT RTys[] = { ValTy, MVT::i32, MVT::Other };
430 // Operands: { Base, Modifier, Chain }
431 MachineSDNode *Res = CurDAG->getMachineNode(FLB->second, dl, RTys,
432 { IntN->getOperand(2), SDValue(Mod,0), IntN->getOperand(0) });
433 return Res;
434 }
435
436 return nullptr;
437}
438
439SDNode *HexagonDAGToDAGISel::StoreInstrForLoadIntrinsic(MachineSDNode *LoadN,
440 SDNode *IntN) {
441 // The "LoadN" is just a machine load instruction. The intrinsic also
442 // involves storing it. Generate an appropriate store to the location
443 // given in the intrinsic's operand(3).
444 uint64_t F = HII->get(LoadN->getMachineOpcode()).TSFlags;
445 unsigned SizeBits = (F >> HexagonII::MemAccessSizePos) &
446 HexagonII::MemAccesSizeMask;
447 unsigned Size = 1U << (SizeBits-1);
448
449 SDLoc dl(IntN);
450 MachinePointerInfo PI;
451 SDValue TS;
452 SDValue Loc = IntN->getOperand(3);
453
454 if (Size >= 4)
Justin Lebar9c375812016-07-15 18:27:10 +0000455 TS = CurDAG->getStore(SDValue(LoadN, 2), dl, SDValue(LoadN, 0), Loc, PI,
456 Size);
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +0000457 else
Justin Lebar9c375812016-07-15 18:27:10 +0000458 TS = CurDAG->getTruncStore(SDValue(LoadN, 2), dl, SDValue(LoadN, 0), Loc,
459 PI, MVT::getIntegerVT(Size * 8), Size);
Justin Bognerdcb7a822016-05-10 20:31:53 +0000460
461 SDNode *StoreN;
462 {
463 HandleSDNode Handle(TS);
464 SelectStore(TS.getNode());
465 StoreN = Handle.getValue().getNode();
466 }
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +0000467
468 // Load's results are { Loaded value, Updated pointer, Chain }
469 ReplaceUses(SDValue(IntN, 0), SDValue(LoadN, 1));
470 ReplaceUses(SDValue(IntN, 1), SDValue(StoreN, 0));
471 return StoreN;
472}
473
Justin Bognerec37a022016-05-12 21:46:18 +0000474bool HexagonDAGToDAGISel::tryLoadOfLoadIntrinsic(LoadSDNode *N) {
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +0000475 // The intrinsics for load circ/brev perform two operations:
476 // 1. Load a value V from the specified location, using the addressing
477 // mode corresponding to the intrinsic.
478 // 2. Store V into a specified location. This location is typically a
479 // local, temporary object.
480 // In many cases, the program using these intrinsics will immediately
481 // load V again from the local object. In those cases, when certain
482 // conditions are met, the last load can be removed.
483 // This function identifies and optimizes this pattern. If the pattern
484 // cannot be optimized, it returns nullptr, which will cause the load
485 // to be selected separately from the intrinsic (which will be handled
486 // in SelectIntrinsicWChain).
487
488 SDValue Ch = N->getOperand(0);
489 SDValue Loc = N->getOperand(1);
490
491 // Assume that the load and the intrinsic are connected directly with a
492 // chain:
493 // t1: i32,ch = int.load ..., ..., ..., Loc, ... // <-- C
494 // t2: i32,ch = load t1:1, Loc, ...
495 SDNode *C = Ch.getNode();
496
497 if (C->getOpcode() != ISD::INTRINSIC_W_CHAIN)
Justin Bognerec37a022016-05-12 21:46:18 +0000498 return false;
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +0000499
500 // The second load can only be eliminated if its extension type matches
501 // that of the load instruction corresponding to the intrinsic. The user
502 // can provide an address of an unsigned variable to store the result of
503 // a sign-extending intrinsic into (or the other way around).
504 ISD::LoadExtType IntExt;
505 switch (cast<ConstantSDNode>(C->getOperand(1))->getZExtValue()) {
506 case Intrinsic::hexagon_brev_ldub:
507 case Intrinsic::hexagon_brev_lduh:
508 case Intrinsic::hexagon_circ_ldub:
509 case Intrinsic::hexagon_circ_lduh:
510 IntExt = ISD::ZEXTLOAD;
511 break;
512 case Intrinsic::hexagon_brev_ldw:
513 case Intrinsic::hexagon_brev_ldd:
514 case Intrinsic::hexagon_circ_ldw:
515 case Intrinsic::hexagon_circ_ldd:
516 IntExt = ISD::NON_EXTLOAD;
517 break;
518 default:
519 IntExt = ISD::SEXTLOAD;
520 break;
521 }
522 if (N->getExtensionType() != IntExt)
Justin Bognerec37a022016-05-12 21:46:18 +0000523 return false;
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +0000524
525 // Make sure the target location for the loaded value in the load intrinsic
526 // is the location from which LD (or N) is loading.
527 if (C->getNumOperands() < 4 || Loc.getNode() != C->getOperand(3).getNode())
Justin Bognerec37a022016-05-12 21:46:18 +0000528 return false;
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +0000529
530 if (MachineSDNode *L = LoadInstrForLoadIntrinsic(C)) {
531 SDNode *S = StoreInstrForLoadIntrinsic(L, C);
532 SDValue F[] = { SDValue(N,0), SDValue(N,1), SDValue(C,0), SDValue(C,1) };
533 SDValue T[] = { SDValue(L,0), SDValue(S,0), SDValue(L,1), SDValue(S,0) };
534 ReplaceUses(F, T, array_lengthof(T));
535 // This transformation will leave the intrinsic dead. If it remains in
536 // the DAG, the selection code will see it again, but without the load,
537 // and it will generate a store that is normally required for it.
Krzysztof Parzyszek0f791f42016-05-13 18:48:15 +0000538 CurDAG->RemoveDeadNode(C);
Justin Bognerec37a022016-05-12 21:46:18 +0000539 return true;
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +0000540 }
541
Justin Bognerec37a022016-05-12 21:46:18 +0000542 return false;
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +0000543}
544
Justin Bognerec37a022016-05-12 21:46:18 +0000545void HexagonDAGToDAGISel::SelectLoad(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000546 SDLoc dl(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000547 LoadSDNode *LD = cast<LoadSDNode>(N);
548 ISD::MemIndexedMode AM = LD->getAddressingMode();
549
550 // Handle indexed loads.
Justin Bognerec37a022016-05-12 21:46:18 +0000551 if (AM != ISD::UNINDEXED) {
552 SelectIndexedLoad(LD, dl);
553 return;
554 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000555
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +0000556 // Handle patterns using circ/brev load intrinsics.
Justin Bognerec37a022016-05-12 21:46:18 +0000557 if (tryLoadOfLoadIntrinsic(LD))
558 return;
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +0000559
Justin Bognerec37a022016-05-12 21:46:18 +0000560 SelectCode(LD);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000561}
562
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000563void HexagonDAGToDAGISel::SelectIndexedStore(StoreSDNode *ST, const SDLoc &dl) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000564 SDValue Chain = ST->getChain();
565 SDValue Base = ST->getBasePtr();
566 SDValue Offset = ST->getOffset();
567 SDValue Value = ST->getValue();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000568 // Get the constant value.
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000569 int32_t Inc = cast<ConstantSDNode>(Offset.getNode())->getSExtValue();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000570 EVT StoredVT = ST->getMemoryVT();
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +0000571 EVT ValueVT = Value.getValueType();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000572
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000573 bool IsValidInc = HII->isValidAutoIncImm(StoredVT, Inc);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000574 unsigned Opcode = 0;
575
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000576 assert(StoredVT.isSimple());
577 switch (StoredVT.getSimpleVT().SimpleTy) {
578 case MVT::i8:
579 Opcode = IsValidInc ? Hexagon::S2_storerb_pi : Hexagon::S2_storerb_io;
580 break;
581 case MVT::i16:
582 Opcode = IsValidInc ? Hexagon::S2_storerh_pi : Hexagon::S2_storerh_io;
583 break;
584 case MVT::i32:
585 Opcode = IsValidInc ? Hexagon::S2_storeri_pi : Hexagon::S2_storeri_io;
586 break;
587 case MVT::i64:
588 Opcode = IsValidInc ? Hexagon::S2_storerd_pi : Hexagon::S2_storerd_io;
589 break;
590 // 64B
591 case MVT::v64i8:
592 case MVT::v32i16:
593 case MVT::v16i32:
594 case MVT::v8i64:
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +0000595 if (isAlignedMemNode(ST))
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000596 Opcode = IsValidInc ? Hexagon::V6_vS32b_pi : Hexagon::V6_vS32b_ai;
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +0000597 else
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000598 Opcode = IsValidInc ? Hexagon::V6_vS32Ub_pi : Hexagon::V6_vS32Ub_ai;
599 break;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000600 // 128B
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000601 case MVT::v128i8:
602 case MVT::v64i16:
603 case MVT::v32i32:
604 case MVT::v16i64:
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +0000605 if (isAlignedMemNode(ST))
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000606 Opcode = IsValidInc ? Hexagon::V6_vS32b_pi_128B
607 : Hexagon::V6_vS32b_ai_128B;
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +0000608 else
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000609 Opcode = IsValidInc ? Hexagon::V6_vS32Ub_pi_128B
610 : Hexagon::V6_vS32Ub_ai_128B;
611 break;
612 default:
613 llvm_unreachable("Unexpected memory type in indexed store");
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +0000614 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000615
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000616 if (ST->isTruncatingStore() && ValueVT.getSizeInBits() == 64) {
617 assert(StoredVT.getSizeInBits() < 64 && "Not a truncating store");
618 Value = CurDAG->getTargetExtractSubreg(Hexagon::subreg_loreg,
619 dl, MVT::i32, Value);
620 }
621
622 SDValue IncV = CurDAG->getTargetConstant(Inc, dl, MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000623 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
624 MemOp[0] = ST->getMemOperand();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000625
Krzysztof Parzyszek709a6262016-06-24 21:27:17 +0000626 // Next address Chain
627 SDValue From[2] = { SDValue(ST,0), SDValue(ST,1) };
628 SDValue To[2];
629
630 if (IsValidInc) {
631 // Build post increment store.
632 SDValue Ops[] = { Base, IncV, Value, Chain };
633 MachineSDNode *S = CurDAG->getMachineNode(Opcode, dl, MVT::i32, MVT::Other,
634 Ops);
635 S->setMemRefs(MemOp, MemOp + 1);
636 To[0] = SDValue(S, 0);
637 To[1] = SDValue(S, 1);
638 } else {
639 SDValue Zero = CurDAG->getTargetConstant(0, dl, MVT::i32);
640 SDValue Ops[] = { Base, Zero, Value, Chain };
641 MachineSDNode *S = CurDAG->getMachineNode(Opcode, dl, MVT::Other, Ops);
642 S->setMemRefs(MemOp, MemOp + 1);
643 To[1] = SDValue(S, 0);
644 MachineSDNode *A = CurDAG->getMachineNode(Hexagon::A2_addi, dl, MVT::i32,
645 Base, IncV);
646 To[0] = SDValue(A, 0);
647 }
648
649 ReplaceUses(From, To, 2);
Justin Bognerdcb7a822016-05-10 20:31:53 +0000650 CurDAG->RemoveDeadNode(ST);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000651}
652
Justin Bognerec37a022016-05-12 21:46:18 +0000653void HexagonDAGToDAGISel::SelectStore(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000654 SDLoc dl(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000655 StoreSDNode *ST = cast<StoreSDNode>(N);
656 ISD::MemIndexedMode AM = ST->getAddressingMode();
657
658 // Handle indexed stores.
659 if (AM != ISD::UNINDEXED) {
Justin Bognerec37a022016-05-12 21:46:18 +0000660 SelectIndexedStore(ST, dl);
661 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000662 }
Sirish Pandec92c3162012-05-03 16:18:50 +0000663
Justin Bognerec37a022016-05-12 21:46:18 +0000664 SelectCode(ST);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000665}
666
Justin Bognerec37a022016-05-12 21:46:18 +0000667void HexagonDAGToDAGISel::SelectMul(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000668 SDLoc dl(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000669
670 //
671 // %conv.i = sext i32 %tmp1 to i64
672 // %conv2.i = sext i32 %add to i64
673 // %mul.i = mul nsw i64 %conv2.i, %conv.i
674 //
675 // --- match with the following ---
676 //
677 // %mul.i = mpy (%tmp1, %add)
678 //
679
680 if (N->getValueType(0) == MVT::i64) {
681 // Shifting a i64 signed multiply.
682 SDValue MulOp0 = N->getOperand(0);
683 SDValue MulOp1 = N->getOperand(1);
684
685 SDValue OP0;
686 SDValue OP1;
687
688 // Handle sign_extend and sextload.
689 if (MulOp0.getOpcode() == ISD::SIGN_EXTEND) {
690 SDValue Sext0 = MulOp0.getOperand(0);
691 if (Sext0.getNode()->getValueType(0) != MVT::i32) {
Justin Bognerec37a022016-05-12 21:46:18 +0000692 SelectCode(N);
693 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000694 }
695
696 OP0 = Sext0;
697 } else if (MulOp0.getOpcode() == ISD::LOAD) {
698 LoadSDNode *LD = cast<LoadSDNode>(MulOp0.getNode());
699 if (LD->getMemoryVT() != MVT::i32 ||
700 LD->getExtensionType() != ISD::SEXTLOAD ||
701 LD->getAddressingMode() != ISD::UNINDEXED) {
Justin Bognerec37a022016-05-12 21:46:18 +0000702 SelectCode(N);
703 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000704 }
705
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000706 SDValue Chain = LD->getChain();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000707 SDValue TargetConst0 = CurDAG->getTargetConstant(0, dl, MVT::i32);
Colin LeMahieu026e88d2014-12-23 20:02:16 +0000708 OP0 = SDValue(CurDAG->getMachineNode(Hexagon::L2_loadri_io, dl, MVT::i32,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000709 MVT::Other,
710 LD->getBasePtr(), TargetConst0,
711 Chain), 0);
712 } else {
Justin Bognerec37a022016-05-12 21:46:18 +0000713 SelectCode(N);
714 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000715 }
716
717 // Same goes for the second operand.
718 if (MulOp1.getOpcode() == ISD::SIGN_EXTEND) {
719 SDValue Sext1 = MulOp1.getOperand(0);
720 if (Sext1.getNode()->getValueType(0) != MVT::i32) {
Justin Bognerec37a022016-05-12 21:46:18 +0000721 SelectCode(N);
722 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000723 }
724
725 OP1 = Sext1;
726 } else if (MulOp1.getOpcode() == ISD::LOAD) {
727 LoadSDNode *LD = cast<LoadSDNode>(MulOp1.getNode());
728 if (LD->getMemoryVT() != MVT::i32 ||
729 LD->getExtensionType() != ISD::SEXTLOAD ||
730 LD->getAddressingMode() != ISD::UNINDEXED) {
Justin Bognerec37a022016-05-12 21:46:18 +0000731 SelectCode(N);
732 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000733 }
734
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000735 SDValue Chain = LD->getChain();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000736 SDValue TargetConst0 = CurDAG->getTargetConstant(0, dl, MVT::i32);
Colin LeMahieu026e88d2014-12-23 20:02:16 +0000737 OP1 = SDValue(CurDAG->getMachineNode(Hexagon::L2_loadri_io, dl, MVT::i32,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000738 MVT::Other,
739 LD->getBasePtr(), TargetConst0,
740 Chain), 0);
741 } else {
Justin Bognerec37a022016-05-12 21:46:18 +0000742 SelectCode(N);
743 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000744 }
745
746 // Generate a mpy instruction.
Colin LeMahieud9b23502014-12-16 16:10:01 +0000747 SDNode *Result = CurDAG->getMachineNode(Hexagon::M2_dpmpyss_s0, dl, MVT::i64,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000748 OP0, OP1);
Justin Bognerec37a022016-05-12 21:46:18 +0000749 ReplaceNode(N, Result);
750 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000751 }
752
Justin Bognerec37a022016-05-12 21:46:18 +0000753 SelectCode(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000754}
755
Justin Bognerec37a022016-05-12 21:46:18 +0000756void HexagonDAGToDAGISel::SelectSHL(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000757 SDLoc dl(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000758 if (N->getValueType(0) == MVT::i32) {
759 SDValue Shl_0 = N->getOperand(0);
760 SDValue Shl_1 = N->getOperand(1);
761 // RHS is const.
762 if (Shl_1.getOpcode() == ISD::Constant) {
763 if (Shl_0.getOpcode() == ISD::MUL) {
764 SDValue Mul_0 = Shl_0.getOperand(0); // Val
765 SDValue Mul_1 = Shl_0.getOperand(1); // Const
766 // RHS of mul is const.
767 if (Mul_1.getOpcode() == ISD::Constant) {
768 int32_t ShlConst =
769 cast<ConstantSDNode>(Shl_1.getNode())->getSExtValue();
770 int32_t MulConst =
771 cast<ConstantSDNode>(Mul_1.getNode())->getSExtValue();
772 int32_t ValConst = MulConst << ShlConst;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000773 SDValue Val = CurDAG->getTargetConstant(ValConst, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000774 MVT::i32);
775 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Val.getNode()))
776 if (isInt<9>(CN->getSExtValue())) {
777 SDNode* Result =
Colin LeMahieud9b23502014-12-16 16:10:01 +0000778 CurDAG->getMachineNode(Hexagon::M2_mpysmi, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000779 MVT::i32, Mul_0, Val);
Justin Bognerec37a022016-05-12 21:46:18 +0000780 ReplaceNode(N, Result);
781 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000782 }
783
784 }
785 } else if (Shl_0.getOpcode() == ISD::SUB) {
786 SDValue Sub_0 = Shl_0.getOperand(0); // Const 0
787 SDValue Sub_1 = Shl_0.getOperand(1); // Val
788 if (Sub_0.getOpcode() == ISD::Constant) {
789 int32_t SubConst =
790 cast<ConstantSDNode>(Sub_0.getNode())->getSExtValue();
791 if (SubConst == 0) {
792 if (Sub_1.getOpcode() == ISD::SHL) {
793 SDValue Shl2_0 = Sub_1.getOperand(0); // Val
794 SDValue Shl2_1 = Sub_1.getOperand(1); // Const
795 if (Shl2_1.getOpcode() == ISD::Constant) {
796 int32_t ShlConst =
797 cast<ConstantSDNode>(Shl_1.getNode())->getSExtValue();
798 int32_t Shl2Const =
799 cast<ConstantSDNode>(Shl2_1.getNode())->getSExtValue();
800 int32_t ValConst = 1 << (ShlConst+Shl2Const);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000801 SDValue Val = CurDAG->getTargetConstant(-ValConst, dl,
802 MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000803 if (ConstantSDNode *CN =
804 dyn_cast<ConstantSDNode>(Val.getNode()))
805 if (isInt<9>(CN->getSExtValue())) {
806 SDNode* Result =
Colin LeMahieud9b23502014-12-16 16:10:01 +0000807 CurDAG->getMachineNode(Hexagon::M2_mpysmi, dl, MVT::i32,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000808 Shl2_0, Val);
Justin Bognerec37a022016-05-12 21:46:18 +0000809 ReplaceNode(N, Result);
810 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000811 }
812 }
813 }
814 }
815 }
816 }
817 }
818 }
Justin Bognerec37a022016-05-12 21:46:18 +0000819 SelectCode(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000820}
821
822
823//
824// If there is an zero_extend followed an intrinsic in DAG (this means - the
825// result of the intrinsic is predicate); convert the zero_extend to
826// transfer instruction.
827//
828// Zero extend -> transfer is lowered here. Otherwise, zero_extend will be
829// converted into a MUX as predicate registers defined as 1 bit in the
830// compiler. Architecture defines them as 8-bit registers.
831// We want to preserve all the lower 8-bits and, not just 1 LSB bit.
832//
Justin Bognerec37a022016-05-12 21:46:18 +0000833void HexagonDAGToDAGISel::SelectZeroExtend(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000834 SDLoc dl(N);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000835
836 SDValue Op0 = N->getOperand(0);
837 EVT OpVT = Op0.getValueType();
838 unsigned OpBW = OpVT.getSizeInBits();
839
840 // Special handling for zero-extending a vector of booleans.
841 if (OpVT.isVector() && OpVT.getVectorElementType() == MVT::i1 && OpBW <= 64) {
842 SDNode *Mask = CurDAG->getMachineNode(Hexagon::C2_mask, dl, MVT::i64, Op0);
843 unsigned NE = OpVT.getVectorNumElements();
844 EVT ExVT = N->getValueType(0);
845 unsigned ES = ExVT.getVectorElementType().getSizeInBits();
846 uint64_t MV = 0, Bit = 1;
847 for (unsigned i = 0; i < NE; ++i) {
848 MV |= Bit;
849 Bit <<= ES;
850 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000851 SDValue Ones = CurDAG->getTargetConstant(MV, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000852 SDNode *OnesReg = CurDAG->getMachineNode(Hexagon::CONST64_Int_Real, dl,
853 MVT::i64, Ones);
854 if (ExVT.getSizeInBits() == 32) {
855 SDNode *And = CurDAG->getMachineNode(Hexagon::A2_andp, dl, MVT::i64,
856 SDValue(Mask,0), SDValue(OnesReg,0));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000857 SDValue SubR = CurDAG->getTargetConstant(Hexagon::subreg_loreg, dl,
858 MVT::i32);
Justin Bognerec37a022016-05-12 21:46:18 +0000859 ReplaceNode(N, CurDAG->getMachineNode(Hexagon::EXTRACT_SUBREG, dl, ExVT,
860 SDValue(And, 0), SubR));
861 return;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000862 }
Justin Bognerec37a022016-05-12 21:46:18 +0000863 ReplaceNode(N,
864 CurDAG->getMachineNode(Hexagon::A2_andp, dl, ExVT,
865 SDValue(Mask, 0), SDValue(OnesReg, 0)));
866 return;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000867 }
868
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000869 SDNode *IsIntrinsic = N->getOperand(0).getNode();
870 if ((IsIntrinsic->getOpcode() == ISD::INTRINSIC_WO_CHAIN)) {
871 unsigned ID =
872 cast<ConstantSDNode>(IsIntrinsic->getOperand(0))->getZExtValue();
873 if (doesIntrinsicReturnPredicate(ID)) {
874 // Now we need to differentiate target data types.
875 if (N->getValueType(0) == MVT::i64) {
Krzysztof Parzyszekae14e7b2015-03-17 21:47:16 +0000876 // Convert the zero_extend to Rs = Pd followed by A2_combinew(0,Rs).
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000877 SDValue TargetConst0 = CurDAG->getTargetConstant(0, dl, MVT::i32);
Colin LeMahieu30dcb232014-12-09 18:16:49 +0000878 SDNode *Result_1 = CurDAG->getMachineNode(Hexagon::C2_tfrpr, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000879 MVT::i32,
880 SDValue(IsIntrinsic, 0));
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000881 SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A2_tfrsi, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000882 MVT::i32,
883 TargetConst0);
Colin LeMahieub580d7d2014-12-09 19:23:45 +0000884 SDNode *Result_3 = CurDAG->getMachineNode(Hexagon::A2_combinew, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000885 MVT::i64, MVT::Other,
886 SDValue(Result_2, 0),
887 SDValue(Result_1, 0));
Justin Bognerec37a022016-05-12 21:46:18 +0000888 ReplaceNode(N, Result_3);
889 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000890 }
891 if (N->getValueType(0) == MVT::i32) {
892 // Convert the zero_extend to Rs = Pd
Colin LeMahieu30dcb232014-12-09 18:16:49 +0000893 SDNode* RsPd = CurDAG->getMachineNode(Hexagon::C2_tfrpr, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000894 MVT::i32,
895 SDValue(IsIntrinsic, 0));
Justin Bognerec37a022016-05-12 21:46:18 +0000896 ReplaceNode(N, RsPd);
897 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000898 }
Craig Toppere55c5562012-02-07 02:50:20 +0000899 llvm_unreachable("Unexpected value type");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000900 }
901 }
Justin Bognerec37a022016-05-12 21:46:18 +0000902 SelectCode(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000903}
904
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +0000905
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000906//
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +0000907// Handling intrinsics for circular load and bitreverse load.
Krzysztof Parzyszek47ab1f22015-03-18 16:23:44 +0000908//
Justin Bognerec37a022016-05-12 21:46:18 +0000909void HexagonDAGToDAGISel::SelectIntrinsicWChain(SDNode *N) {
910 if (MachineSDNode *L = LoadInstrForLoadIntrinsic(N)) {
911 StoreInstrForLoadIntrinsic(L, N);
Krzysztof Parzyszek0f791f42016-05-13 18:48:15 +0000912 CurDAG->RemoveDeadNode(N);
Justin Bognerec37a022016-05-12 21:46:18 +0000913 return;
914 }
915 SelectCode(N);
Krzysztof Parzyszek47ab1f22015-03-18 16:23:44 +0000916}
917
Justin Bognerec37a022016-05-12 21:46:18 +0000918void HexagonDAGToDAGISel::SelectIntrinsicWOChain(SDNode *N) {
Colin LeMahieu0ee02fc2015-01-19 20:31:18 +0000919 unsigned IID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
920 unsigned Bits;
921 switch (IID) {
922 case Intrinsic::hexagon_S2_vsplatrb:
923 Bits = 8;
924 break;
925 case Intrinsic::hexagon_S2_vsplatrh:
926 Bits = 16;
927 break;
928 default:
Justin Bognerec37a022016-05-12 21:46:18 +0000929 SelectCode(N);
930 return;
Colin LeMahieu0ee02fc2015-01-19 20:31:18 +0000931 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000932
Krzysztof Parzyszeke60e5fe2016-05-12 17:21:40 +0000933 SDValue V = N->getOperand(1);
Colin LeMahieu0ee02fc2015-01-19 20:31:18 +0000934 SDValue U;
935 if (isValueExtension(V, Bits, U)) {
936 SDValue R = CurDAG->getNode(N->getOpcode(), SDLoc(N), N->getValueType(0),
Krzysztof Parzyszeke60e5fe2016-05-12 17:21:40 +0000937 N->getOperand(0), U);
Justin Bognerd82025b2016-05-12 21:24:23 +0000938 ReplaceNode(N, R.getNode());
Justin Bognerec37a022016-05-12 21:46:18 +0000939 SelectCode(R.getNode());
940 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000941 }
Justin Bognerec37a022016-05-12 21:46:18 +0000942 SelectCode(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000943}
944
Sirish Pande69295b82012-05-10 20:20:25 +0000945//
946// Map floating point constant values.
947//
Justin Bognerec37a022016-05-12 21:46:18 +0000948void HexagonDAGToDAGISel::SelectConstantFP(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000949 SDLoc dl(N);
Sirish Pande69295b82012-05-10 20:20:25 +0000950 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N);
Benjamin Kramer46e38f32016-06-08 10:01:20 +0000951 const APFloat &APF = CN->getValueAPF();
Sirish Pande69295b82012-05-10 20:20:25 +0000952 if (N->getValueType(0) == MVT::f32) {
Justin Bognerec37a022016-05-12 21:46:18 +0000953 ReplaceNode(
954 N, CurDAG->getMachineNode(Hexagon::TFRI_f, dl, MVT::f32,
955 CurDAG->getTargetConstantFP(
956 APF.convertToFloat(), dl, MVT::f32)));
957 return;
Sirish Pande69295b82012-05-10 20:20:25 +0000958 }
959 else if (N->getValueType(0) == MVT::f64) {
Justin Bognerec37a022016-05-12 21:46:18 +0000960 ReplaceNode(
961 N, CurDAG->getMachineNode(Hexagon::CONST64_Float_Real, dl, MVT::f64,
962 CurDAG->getTargetConstantFP(
963 APF.convertToDouble(), dl, MVT::f64)));
964 return;
Sirish Pande69295b82012-05-10 20:20:25 +0000965 }
966
Justin Bognerec37a022016-05-12 21:46:18 +0000967 SelectCode(N);
Sirish Pande69295b82012-05-10 20:20:25 +0000968}
969
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000970//
971// Map predicate true (encoded as -1 in LLVM) to a XOR.
972//
Justin Bognerec37a022016-05-12 21:46:18 +0000973void HexagonDAGToDAGISel::SelectConstant(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000974 SDLoc dl(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000975 if (N->getValueType(0) == MVT::i1) {
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +0000976 SDNode* Result = 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000977 int32_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Krzysztof Parzyszek7a9cd802015-03-18 18:50:06 +0000978 if (Val == -1) {
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +0000979 Result = CurDAG->getMachineNode(Hexagon::TFR_PdTrue, dl, MVT::i1);
980 } else if (Val == 0) {
981 Result = CurDAG->getMachineNode(Hexagon::TFR_PdFalse, dl, MVT::i1);
982 }
983 if (Result) {
Justin Bognerec37a022016-05-12 21:46:18 +0000984 ReplaceNode(N, Result);
985 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000986 }
987 }
988
Justin Bognerec37a022016-05-12 21:46:18 +0000989 SelectCode(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000990}
991
992
993//
994// Map add followed by a asr -> asr +=.
995//
Justin Bognerec37a022016-05-12 21:46:18 +0000996void HexagonDAGToDAGISel::SelectAdd(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000997 SDLoc dl(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000998 if (N->getValueType(0) != MVT::i32) {
Justin Bognerec37a022016-05-12 21:46:18 +0000999 SelectCode(N);
1000 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001001 }
1002 // Identify nodes of the form: add(asr(...)).
1003 SDNode* Src1 = N->getOperand(0).getNode();
1004 if (Src1->getOpcode() != ISD::SRA || !Src1->hasOneUse()
1005 || Src1->getValueType(0) != MVT::i32) {
Justin Bognerec37a022016-05-12 21:46:18 +00001006 SelectCode(N);
1007 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001008 }
1009
1010 // Build Rd = Rd' + asr(Rs, Rt). The machine constraints will ensure that
1011 // Rd and Rd' are assigned to the same register
Colin LeMahieu0f850bd2014-12-19 20:29:29 +00001012 SDNode* Result = CurDAG->getMachineNode(Hexagon::S2_asr_r_r_acc, dl, MVT::i32,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001013 N->getOperand(1),
1014 Src1->getOperand(0),
1015 Src1->getOperand(1));
Justin Bognerec37a022016-05-12 21:46:18 +00001016 ReplaceNode(N, Result);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001017}
1018
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001019//
1020// Map the following, where possible.
1021// AND/FABS -> clrbit
1022// OR -> setbit
1023// XOR/FNEG ->toggle_bit.
1024//
Justin Bognerec37a022016-05-12 21:46:18 +00001025void HexagonDAGToDAGISel::SelectBitOp(SDNode *N) {
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001026 SDLoc dl(N);
1027 EVT ValueVT = N->getValueType(0);
1028
1029 // We handle only 32 and 64-bit bit ops.
1030 if (!(ValueVT == MVT::i32 || ValueVT == MVT::i64 ||
Justin Bognerec37a022016-05-12 21:46:18 +00001031 ValueVT == MVT::f32 || ValueVT == MVT::f64)) {
1032 SelectCode(N);
1033 return;
1034 }
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001035
1036 // We handly only fabs and fneg for V5.
1037 unsigned Opc = N->getOpcode();
Justin Bognerec37a022016-05-12 21:46:18 +00001038 if ((Opc == ISD::FABS || Opc == ISD::FNEG) && !HST->hasV5TOps()) {
1039 SelectCode(N);
1040 return;
1041 }
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001042
1043 int64_t Val = 0;
1044 if (Opc != ISD::FABS && Opc != ISD::FNEG) {
1045 if (N->getOperand(1).getOpcode() == ISD::Constant)
1046 Val = cast<ConstantSDNode>((N)->getOperand(1))->getSExtValue();
Justin Bognerec37a022016-05-12 21:46:18 +00001047 else {
1048 SelectCode(N);
1049 return;
1050 }
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001051 }
1052
1053 if (Opc == ISD::AND) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001054 // Check if this is a bit-clearing AND, if not select code the usual way.
1055 if ((ValueVT == MVT::i32 && isPowerOf2_32(~Val)) ||
1056 (ValueVT == MVT::i64 && isPowerOf2_64(~Val)))
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001057 Val = ~Val;
Justin Bognerec37a022016-05-12 21:46:18 +00001058 else {
1059 SelectCode(N);
1060 return;
1061 }
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001062 }
1063
1064 // If OR or AND is being fed by shl, srl and, sra don't do this change,
1065 // because Hexagon provide |= &= on shl, srl, and sra.
1066 // Traverse the DAG to see if there is shl, srl and sra.
1067 if (Opc == ISD::OR || Opc == ISD::AND) {
1068 switch (N->getOperand(0)->getOpcode()) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001069 default:
1070 break;
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001071 case ISD::SRA:
1072 case ISD::SRL:
1073 case ISD::SHL:
Justin Bognerec37a022016-05-12 21:46:18 +00001074 SelectCode(N);
1075 return;
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001076 }
1077 }
1078
1079 // Make sure it's power of 2.
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001080 unsigned BitPos = 0;
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001081 if (Opc != ISD::FABS && Opc != ISD::FNEG) {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001082 if ((ValueVT == MVT::i32 && !isPowerOf2_32(Val)) ||
Justin Bognerec37a022016-05-12 21:46:18 +00001083 (ValueVT == MVT::i64 && !isPowerOf2_64(Val))) {
1084 SelectCode(N);
1085 return;
1086 }
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001087
1088 // Get the bit position.
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001089 BitPos = countTrailingZeros(uint64_t(Val));
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001090 } else {
1091 // For fabs and fneg, it's always the 31st bit.
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001092 BitPos = 31;
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001093 }
1094
1095 unsigned BitOpc = 0;
1096 // Set the right opcode for bitwise operations.
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001097 switch (Opc) {
1098 default:
1099 llvm_unreachable("Only bit-wise/abs/neg operations are allowed.");
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001100 case ISD::AND:
1101 case ISD::FABS:
1102 BitOpc = Hexagon::S2_clrbit_i;
1103 break;
1104 case ISD::OR:
1105 BitOpc = Hexagon::S2_setbit_i;
1106 break;
1107 case ISD::XOR:
1108 case ISD::FNEG:
1109 BitOpc = Hexagon::S2_togglebit_i;
1110 break;
1111 }
1112
1113 SDNode *Result;
1114 // Get the right SDVal for the opcode.
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001115 SDValue SDVal = CurDAG->getTargetConstant(BitPos, dl, MVT::i32);
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001116
1117 if (ValueVT == MVT::i32 || ValueVT == MVT::f32) {
1118 Result = CurDAG->getMachineNode(BitOpc, dl, ValueVT,
1119 N->getOperand(0), SDVal);
1120 } else {
1121 // 64-bit gymnastic to use REG_SEQUENCE. But it's worth it.
1122 EVT SubValueVT;
1123 if (ValueVT == MVT::i64)
1124 SubValueVT = MVT::i32;
1125 else
1126 SubValueVT = MVT::f32;
1127
1128 SDNode *Reg = N->getOperand(0).getNode();
1129 SDValue RegClass = CurDAG->getTargetConstant(Hexagon::DoubleRegsRegClassID,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001130 dl, MVT::i64);
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001131
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001132 SDValue SubregHiIdx = CurDAG->getTargetConstant(Hexagon::subreg_hireg, dl,
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001133 MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001134 SDValue SubregLoIdx = CurDAG->getTargetConstant(Hexagon::subreg_loreg, dl,
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001135 MVT::i32);
1136
1137 SDValue SubregHI = CurDAG->getTargetExtractSubreg(Hexagon::subreg_hireg, dl,
1138 MVT::i32, SDValue(Reg, 0));
1139
1140 SDValue SubregLO = CurDAG->getTargetExtractSubreg(Hexagon::subreg_loreg, dl,
1141 MVT::i32, SDValue(Reg, 0));
1142
1143 // Clear/set/toggle hi or lo registers depending on the bit position.
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001144 if (SubValueVT != MVT::f32 && BitPos < 32) {
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001145 SDNode *Result0 = CurDAG->getMachineNode(BitOpc, dl, SubValueVT,
1146 SubregLO, SDVal);
1147 const SDValue Ops[] = { RegClass, SubregHI, SubregHiIdx,
1148 SDValue(Result0, 0), SubregLoIdx };
1149 Result = CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE,
1150 dl, ValueVT, Ops);
1151 } else {
1152 if (Opc != ISD::FABS && Opc != ISD::FNEG)
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001153 SDVal = CurDAG->getTargetConstant(BitPos-32, dl, MVT::i32);
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001154 SDNode *Result0 = CurDAG->getMachineNode(BitOpc, dl, SubValueVT,
1155 SubregHI, SDVal);
1156 const SDValue Ops[] = { RegClass, SDValue(Result0, 0), SubregHiIdx,
1157 SubregLO, SubregLoIdx };
1158 Result = CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE,
1159 dl, ValueVT, Ops);
1160 }
1161 }
1162
Justin Bognerec37a022016-05-12 21:46:18 +00001163 ReplaceNode(N, Result);
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001164}
1165
1166
Justin Bognerec37a022016-05-12 21:46:18 +00001167void HexagonDAGToDAGISel::SelectFrameIndex(SDNode *N) {
Matthias Braun941a7052016-07-28 18:40:00 +00001168 MachineFrameInfo &MFI = MF->getFrameInfo();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001169 const HexagonFrameLowering *HFI = HST->getFrameLowering();
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001170 int FX = cast<FrameIndexSDNode>(N)->getIndex();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001171 unsigned StkA = HFI->getStackAlignment();
Matthias Braun941a7052016-07-28 18:40:00 +00001172 unsigned MaxA = MFI.getMaxAlignment();
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001173 SDValue FI = CurDAG->getTargetFrameIndex(FX, MVT::i32);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001174 SDLoc DL(N);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001175 SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001176 SDNode *R = 0;
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001177
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001178 // Use TFR_FI when:
1179 // - the object is fixed, or
1180 // - there are no objects with higher-than-default alignment, or
1181 // - there are no dynamically allocated objects.
1182 // Otherwise, use TFR_FIA.
Matthias Braun941a7052016-07-28 18:40:00 +00001183 if (FX < 0 || MaxA <= StkA || !MFI.hasVarSizedObjects()) {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001184 R = CurDAG->getMachineNode(Hexagon::TFR_FI, DL, MVT::i32, FI, Zero);
1185 } else {
1186 auto &HMFI = *MF->getInfo<HexagonMachineFunctionInfo>();
1187 unsigned AR = HMFI.getStackAlignBaseVReg();
1188 SDValue CH = CurDAG->getEntryNode();
1189 SDValue Ops[] = { CurDAG->getCopyFromReg(CH, DL, AR, MVT::i32), FI, Zero };
1190 R = CurDAG->getMachineNode(Hexagon::TFR_FIA, DL, MVT::i32, Ops);
1191 }
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001192
Justin Bognerec37a022016-05-12 21:46:18 +00001193 ReplaceNode(N, R);
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001194}
1195
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001196
Krzysztof Parzyszek5da24e52016-06-27 15:08:22 +00001197void HexagonDAGToDAGISel::SelectBitcast(SDNode *N) {
1198 EVT SVT = N->getOperand(0).getValueType();
1199 EVT DVT = N->getValueType(0);
1200 if (!SVT.isVector() || !DVT.isVector() ||
1201 SVT.getVectorElementType() == MVT::i1 ||
1202 DVT.getVectorElementType() == MVT::i1 ||
1203 SVT.getSizeInBits() != DVT.getSizeInBits()) {
1204 SelectCode(N);
1205 return;
1206 }
1207
1208 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N,0), N->getOperand(0));
1209 CurDAG->RemoveDeadNode(N);
1210}
1211
1212
Justin Bognerec37a022016-05-12 21:46:18 +00001213void HexagonDAGToDAGISel::Select(SDNode *N) {
Tim Northover31d093c2013-09-22 08:21:56 +00001214 if (N->isMachineOpcode()) {
1215 N->setNodeId(-1);
Justin Bognerec37a022016-05-12 21:46:18 +00001216 return; // Already selected.
Tim Northover31d093c2013-09-22 08:21:56 +00001217 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001218
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001219 switch (N->getOpcode()) {
1220 case ISD::Constant:
Justin Bognerec37a022016-05-12 21:46:18 +00001221 SelectConstant(N);
1222 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001223
Sirish Pande69295b82012-05-10 20:20:25 +00001224 case ISD::ConstantFP:
Justin Bognerec37a022016-05-12 21:46:18 +00001225 SelectConstantFP(N);
1226 return;
Sirish Pande69295b82012-05-10 20:20:25 +00001227
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001228 case ISD::FrameIndex:
Justin Bognerec37a022016-05-12 21:46:18 +00001229 SelectFrameIndex(N);
1230 return;
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001231
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001232 case ISD::ADD:
Justin Bognerec37a022016-05-12 21:46:18 +00001233 SelectAdd(N);
1234 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001235
Krzysztof Parzyszek5da24e52016-06-27 15:08:22 +00001236 case ISD::BITCAST:
1237 SelectBitcast(N);
1238 return;
1239
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001240 case ISD::SHL:
Justin Bognerec37a022016-05-12 21:46:18 +00001241 SelectSHL(N);
1242 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001243
1244 case ISD::LOAD:
Justin Bognerec37a022016-05-12 21:46:18 +00001245 SelectLoad(N);
1246 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001247
1248 case ISD::STORE:
Justin Bognerec37a022016-05-12 21:46:18 +00001249 SelectStore(N);
1250 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001251
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001252 case ISD::MUL:
Justin Bognerec37a022016-05-12 21:46:18 +00001253 SelectMul(N);
1254 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001255
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001256 case ISD::AND:
1257 case ISD::OR:
1258 case ISD::XOR:
1259 case ISD::FABS:
1260 case ISD::FNEG:
Justin Bognerec37a022016-05-12 21:46:18 +00001261 SelectBitOp(N);
1262 return;
Krzysztof Parzyszek8c1cab92015-03-18 00:43:46 +00001263
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001264 case ISD::ZERO_EXTEND:
Justin Bognerec37a022016-05-12 21:46:18 +00001265 SelectZeroExtend(N);
1266 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001267
Krzysztof Parzyszek47ab1f22015-03-18 16:23:44 +00001268 case ISD::INTRINSIC_W_CHAIN:
Justin Bognerec37a022016-05-12 21:46:18 +00001269 SelectIntrinsicWChain(N);
1270 return;
Krzysztof Parzyszek47ab1f22015-03-18 16:23:44 +00001271
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001272 case ISD::INTRINSIC_WO_CHAIN:
Justin Bognerec37a022016-05-12 21:46:18 +00001273 SelectIntrinsicWOChain(N);
1274 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001275 }
1276
Justin Bognerec37a022016-05-12 21:46:18 +00001277 SelectCode(N);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001278}
1279
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001280bool HexagonDAGToDAGISel::
Daniel Sanders60f1db02015-03-13 12:45:09 +00001281SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001282 std::vector<SDValue> &OutOps) {
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001283 SDValue Inp = Op, Res;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001284
Daniel Sanders60f1db02015-03-13 12:45:09 +00001285 switch (ConstraintID) {
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001286 default:
1287 return true;
Daniel Sanders49f643c2015-03-17 14:37:39 +00001288 case InlineAsm::Constraint_i:
1289 case InlineAsm::Constraint_o: // Offsetable.
1290 case InlineAsm::Constraint_v: // Not offsetable.
1291 case InlineAsm::Constraint_m: // Memory.
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001292 if (SelectAddrFI(Inp, Res))
1293 OutOps.push_back(Res);
1294 else
1295 OutOps.push_back(Inp);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001296 break;
1297 }
1298
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001299 OutOps.push_back(CurDAG->getTargetConstant(0, SDLoc(Op), MVT::i32));
Jyotsna Vermad9225242013-02-13 21:38:46 +00001300 return false;
1301}
Colin LeMahieuc7522f32015-01-14 23:07:36 +00001302
Colin LeMahieu79ec0652015-06-12 19:57:32 +00001303
Krzysztof Parzyszekae14e7b2015-03-17 21:47:16 +00001304void HexagonDAGToDAGISel::PreprocessISelDAG() {
1305 SelectionDAG &DAG = *CurDAG;
1306 std::vector<SDNode*> Nodes;
Pete Cooper7e64ef02015-07-14 23:43:29 +00001307 for (SDNode &Node : DAG.allnodes())
1308 Nodes.push_back(&Node);
Krzysztof Parzyszekae14e7b2015-03-17 21:47:16 +00001309
1310 // Simplify: (or (select c x 0) z) -> (select c (or x z) z)
1311 // (or (select c 0 y) z) -> (select c z (or y z))
1312 // This may not be the right thing for all targets, so do it here.
Krzysztof Parzyszekf7f70682016-06-22 20:08:27 +00001313 for (auto I : Nodes) {
Krzysztof Parzyszekae14e7b2015-03-17 21:47:16 +00001314 if (I->getOpcode() != ISD::OR)
1315 continue;
1316
1317 auto IsZero = [] (const SDValue &V) -> bool {
1318 if (ConstantSDNode *SC = dyn_cast<ConstantSDNode>(V.getNode()))
1319 return SC->isNullValue();
1320 return false;
1321 };
1322 auto IsSelect0 = [IsZero] (const SDValue &Op) -> bool {
1323 if (Op.getOpcode() != ISD::SELECT)
1324 return false;
Krzysztof Parzyszek7d5b4db2016-02-12 17:01:51 +00001325 return IsZero(Op.getOperand(1)) || IsZero(Op.getOperand(2));
Krzysztof Parzyszekae14e7b2015-03-17 21:47:16 +00001326 };
1327
1328 SDValue N0 = I->getOperand(0), N1 = I->getOperand(1);
1329 EVT VT = I->getValueType(0);
1330 bool SelN0 = IsSelect0(N0);
1331 SDValue SOp = SelN0 ? N0 : N1;
1332 SDValue VOp = SelN0 ? N1 : N0;
1333
1334 if (SOp.getOpcode() == ISD::SELECT && SOp.getNode()->hasOneUse()) {
1335 SDValue SC = SOp.getOperand(0);
1336 SDValue SX = SOp.getOperand(1);
1337 SDValue SY = SOp.getOperand(2);
1338 SDLoc DLS = SOp;
1339 if (IsZero(SY)) {
1340 SDValue NewOr = DAG.getNode(ISD::OR, DLS, VT, SX, VOp);
1341 SDValue NewSel = DAG.getNode(ISD::SELECT, DLS, VT, SC, NewOr, VOp);
1342 DAG.ReplaceAllUsesWith(I, NewSel.getNode());
1343 } else if (IsZero(SX)) {
1344 SDValue NewOr = DAG.getNode(ISD::OR, DLS, VT, SY, VOp);
1345 SDValue NewSel = DAG.getNode(ISD::SELECT, DLS, VT, SC, VOp, NewOr);
1346 DAG.ReplaceAllUsesWith(I, NewSel.getNode());
1347 }
1348 }
1349 }
Krzysztof Parzyszekf7f70682016-06-22 20:08:27 +00001350
1351 // Transform: (store ch addr (add x (add (shl y c) e)))
1352 // to: (store ch addr (add x (shl (add y d) c))),
1353 // where e = (shl d c) for some integer d.
1354 // The purpose of this is to enable generation of loads/stores with
1355 // shifted addressing mode, i.e. mem(x+y<<#c). For that, the shift
1356 // value c must be 0, 1 or 2.
1357 for (auto I : Nodes) {
1358 if (I->getOpcode() != ISD::STORE)
1359 continue;
1360
1361 // I matched: (store ch addr Off)
1362 SDValue Off = I->getOperand(2);
1363 // Off needs to match: (add x (add (shl y c) (shl d c))))
1364 if (Off.getOpcode() != ISD::ADD)
1365 continue;
1366 // Off matched: (add x T0)
1367 SDValue T0 = Off.getOperand(1);
1368 // T0 needs to match: (add T1 T2):
1369 if (T0.getOpcode() != ISD::ADD)
1370 continue;
1371 // T0 matched: (add T1 T2)
1372 SDValue T1 = T0.getOperand(0);
1373 SDValue T2 = T0.getOperand(1);
1374 // T1 needs to match: (shl y c)
1375 if (T1.getOpcode() != ISD::SHL)
1376 continue;
1377 SDValue C = T1.getOperand(1);
1378 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(C.getNode());
1379 if (CN == nullptr)
1380 continue;
1381 unsigned CV = CN->getZExtValue();
1382 if (CV > 2)
1383 continue;
1384 // T2 needs to match e, where e = (shl d c) for some d.
1385 ConstantSDNode *EN = dyn_cast<ConstantSDNode>(T2.getNode());
1386 if (EN == nullptr)
1387 continue;
1388 unsigned EV = EN->getZExtValue();
1389 if (EV % (1 << CV) != 0)
1390 continue;
1391 unsigned DV = EV / (1 << CV);
1392
1393 // Replace T0 with: (shl (add y d) c)
1394 SDLoc DL = SDLoc(I);
1395 EVT VT = T0.getValueType();
1396 SDValue D = DAG.getConstant(DV, DL, VT);
1397 // NewAdd = (add y d)
1398 SDValue NewAdd = DAG.getNode(ISD::ADD, DL, VT, T1.getOperand(0), D);
1399 // NewShl = (shl NewAdd c)
1400 SDValue NewShl = DAG.getNode(ISD::SHL, DL, VT, NewAdd, C);
1401 ReplaceNode(T0.getNode(), NewShl.getNode());
1402 }
Krzysztof Parzyszek0006e1a2016-07-29 15:15:35 +00001403
1404 if (EnableAddressRebalancing) {
1405 rebalanceAddressTrees();
1406
1407 DEBUG(
1408 dbgs() << "************* SelectionDAG after preprocessing: ***********\n";
1409 CurDAG->dump();
1410 dbgs() << "************* End SelectionDAG after preprocessing ********\n";
1411 );
1412 }
Krzysztof Parzyszekae14e7b2015-03-17 21:47:16 +00001413}
1414
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001415void HexagonDAGToDAGISel::EmitFunctionEntryCode() {
1416 auto &HST = static_cast<const HexagonSubtarget&>(MF->getSubtarget());
1417 auto &HFI = *HST.getFrameLowering();
1418 if (!HFI.needsAligna(*MF))
1419 return;
Krzysztof Parzyszekae14e7b2015-03-17 21:47:16 +00001420
Matthias Braun941a7052016-07-28 18:40:00 +00001421 MachineFrameInfo &MFI = MF->getFrameInfo();
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +00001422 MachineBasicBlock *EntryBB = &MF->front();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001423 unsigned AR = FuncInfo->CreateReg(MVT::i32);
Matthias Braun941a7052016-07-28 18:40:00 +00001424 unsigned MaxA = MFI.getMaxAlignment();
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001425 BuildMI(EntryBB, DebugLoc(), HII->get(Hexagon::ALIGNA), AR)
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001426 .addImm(MaxA);
1427 MF->getInfo<HexagonMachineFunctionInfo>()->setStackAlignBaseVReg(AR);
1428}
1429
1430// Match a frame index that can be used in an addressing mode.
Colin LeMahieuc7522f32015-01-14 23:07:36 +00001431bool HexagonDAGToDAGISel::SelectAddrFI(SDValue& N, SDValue &R) {
1432 if (N.getOpcode() != ISD::FrameIndex)
1433 return false;
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001434 auto &HFI = *HST->getFrameLowering();
Matthias Braun941a7052016-07-28 18:40:00 +00001435 MachineFrameInfo &MFI = MF->getFrameInfo();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001436 int FX = cast<FrameIndexSDNode>(N)->getIndex();
Matthias Braun941a7052016-07-28 18:40:00 +00001437 if (!MFI.isFixedObjectIndex(FX) && HFI.needsAligna(*MF))
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001438 return false;
1439 R = CurDAG->getTargetFrameIndex(FX, MVT::i32);
Colin LeMahieuc7522f32015-01-14 23:07:36 +00001440 return true;
1441}
Colin LeMahieu0ee02fc2015-01-19 20:31:18 +00001442
Colin LeMahieu987b0942015-02-04 20:38:01 +00001443inline bool HexagonDAGToDAGISel::SelectAddrGA(SDValue &N, SDValue &R) {
1444 return SelectGlobalAddress(N, R, false);
1445}
1446
Colin LeMahieu51491352015-02-04 22:36:28 +00001447inline bool HexagonDAGToDAGISel::SelectAddrGP(SDValue &N, SDValue &R) {
1448 return SelectGlobalAddress(N, R, true);
1449}
1450
Colin LeMahieu987b0942015-02-04 20:38:01 +00001451bool HexagonDAGToDAGISel::SelectGlobalAddress(SDValue &N, SDValue &R,
1452 bool UseGP) {
1453 switch (N.getOpcode()) {
1454 case ISD::ADD: {
1455 SDValue N0 = N.getOperand(0);
1456 SDValue N1 = N.getOperand(1);
1457 unsigned GAOpc = N0.getOpcode();
1458 if (UseGP && GAOpc != HexagonISD::CONST32_GP)
1459 return false;
1460 if (!UseGP && GAOpc != HexagonISD::CONST32)
1461 return false;
1462 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N1)) {
1463 SDValue Addr = N0.getOperand(0);
1464 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Addr)) {
1465 if (GA->getOpcode() == ISD::TargetGlobalAddress) {
1466 uint64_t NewOff = GA->getOffset() + (uint64_t)Const->getSExtValue();
1467 R = CurDAG->getTargetGlobalAddress(GA->getGlobal(), SDLoc(Const),
1468 N.getValueType(), NewOff);
1469 return true;
1470 }
1471 }
1472 }
1473 break;
1474 }
1475 case HexagonISD::CONST32:
1476 // The operand(0) of CONST32 is TargetGlobalAddress, which is what we
1477 // want in the instruction.
1478 if (!UseGP)
1479 R = N.getOperand(0);
1480 return !UseGP;
1481 case HexagonISD::CONST32_GP:
1482 if (UseGP)
1483 R = N.getOperand(0);
1484 return UseGP;
1485 default:
1486 return false;
1487 }
1488
1489 return false;
1490}
1491
Krzysztof Parzyszeka29622a2015-03-12 16:44:50 +00001492bool HexagonDAGToDAGISel::isValueExtension(const SDValue &Val,
1493 unsigned FromBits, SDValue &Src) {
Colin LeMahieu0ee02fc2015-01-19 20:31:18 +00001494 unsigned Opc = Val.getOpcode();
1495 switch (Opc) {
1496 case ISD::SIGN_EXTEND:
1497 case ISD::ZERO_EXTEND:
1498 case ISD::ANY_EXTEND: {
1499 SDValue const &Op0 = Val.getOperand(0);
1500 EVT T = Op0.getValueType();
1501 if (T.isInteger() && T.getSizeInBits() == FromBits) {
1502 Src = Op0;
1503 return true;
1504 }
1505 break;
1506 }
1507 case ISD::SIGN_EXTEND_INREG:
1508 case ISD::AssertSext:
1509 case ISD::AssertZext:
1510 if (Val.getOperand(0).getValueType().isInteger()) {
1511 VTSDNode *T = cast<VTSDNode>(Val.getOperand(1));
1512 if (T->getVT().getSizeInBits() == FromBits) {
1513 Src = Val.getOperand(0);
1514 return true;
1515 }
1516 }
1517 break;
1518 case ISD::AND: {
1519 // Check if this is an AND with "FromBits" of lower bits set to 1.
1520 uint64_t FromMask = (1 << FromBits) - 1;
1521 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(0))) {
1522 if (C->getZExtValue() == FromMask) {
1523 Src = Val.getOperand(1);
1524 return true;
1525 }
1526 }
1527 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(1))) {
1528 if (C->getZExtValue() == FromMask) {
1529 Src = Val.getOperand(0);
1530 return true;
1531 }
1532 }
1533 break;
1534 }
1535 case ISD::OR:
1536 case ISD::XOR: {
1537 // OR/XOR with the lower "FromBits" bits set to 0.
1538 uint64_t FromMask = (1 << FromBits) - 1;
1539 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(0))) {
1540 if ((C->getZExtValue() & FromMask) == 0) {
1541 Src = Val.getOperand(1);
1542 return true;
1543 }
1544 }
1545 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(1))) {
1546 if ((C->getZExtValue() & FromMask) == 0) {
1547 Src = Val.getOperand(0);
1548 return true;
1549 }
1550 }
1551 }
1552 default:
1553 break;
1554 }
1555 return false;
1556}
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +00001557
Krzysztof Parzyszekbba0bf72016-07-15 15:35:52 +00001558
1559bool HexagonDAGToDAGISel::orIsAdd(const SDNode *N) const {
1560 assert(N->getOpcode() == ISD::OR);
1561 auto *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
1562 assert(C);
1563
1564 // Detect when "or" is used to add an offset to a stack object.
1565 if (auto *FN = dyn_cast<FrameIndexSDNode>(N->getOperand(0))) {
Matthias Braun941a7052016-07-28 18:40:00 +00001566 MachineFrameInfo &MFI = MF->getFrameInfo();
1567 unsigned A = MFI.getObjectAlignment(FN->getIndex());
Krzysztof Parzyszekbba0bf72016-07-15 15:35:52 +00001568 assert(isPowerOf2_32(A));
1569 int32_t Off = C->getSExtValue();
1570 // If the alleged offset fits in the zero bits guaranteed by
1571 // the alignment, then this or is really an add.
1572 return (Off >= 0) && (((A-1) & Off) == unsigned(Off));
1573 }
1574 return false;
1575}
1576
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +00001577bool HexagonDAGToDAGISel::isAlignedMemNode(const MemSDNode *N) const {
1578 return N->getAlignment() >= N->getMemoryVT().getStoreSize();
1579}
Krzysztof Parzyszek0006e1a2016-07-29 15:15:35 +00001580
1581////////////////////////////////////////////////////////////////////////////////
1582// Rebalancing of address calculation trees
1583
1584static bool isOpcodeHandled(const SDNode *N) {
1585 switch (N->getOpcode()) {
1586 case ISD::ADD:
1587 case ISD::MUL:
1588 return true;
1589 case ISD::SHL:
1590 // We only handle constant shifts because these can be easily flattened
1591 // into multiplications by 2^Op1.
1592 return isa<ConstantSDNode>(N->getOperand(1).getNode());
1593 default:
1594 return false;
1595 }
1596}
1597
1598/// \brief Return the weight of an SDNode
1599int HexagonDAGToDAGISel::getWeight(SDNode *N) {
1600 if (!isOpcodeHandled(N))
1601 return 1;
1602 assert(RootWeights.count(N) && "Cannot get weight of unseen root!");
1603 assert(RootWeights[N] != -1 && "Cannot get weight of unvisited root!");
1604 assert(RootWeights[N] != -2 && "Cannot get weight of RAWU'd root!");
1605 return RootWeights[N];
1606}
1607
1608int HexagonDAGToDAGISel::getHeight(SDNode *N) {
1609 if (!isOpcodeHandled(N))
1610 return 0;
1611 assert(RootWeights.count(N) && RootWeights[N] >= 0 &&
1612 "Cannot query height of unvisited/RAUW'd node!");
1613 return RootHeights[N];
1614}
1615
1616struct WeightedLeaf {
1617 SDValue Value;
1618 int Weight;
1619 int InsertionOrder;
1620
1621 WeightedLeaf() : Value(SDValue()) { }
1622
1623 WeightedLeaf(SDValue Value, int Weight, int InsertionOrder) :
1624 Value(Value), Weight(Weight), InsertionOrder(InsertionOrder) {
1625 assert(Weight >= 0 && "Weight must be >= 0");
1626 }
1627
1628 static bool Compare(const WeightedLeaf &A, const WeightedLeaf &B) {
1629 assert(A.Value.getNode() && B.Value.getNode());
1630 return A.Weight == B.Weight ?
1631 (A.InsertionOrder > B.InsertionOrder) :
1632 (A.Weight > B.Weight);
1633 }
1634};
1635
1636/// A specialized priority queue for WeigthedLeaves. It automatically folds
1637/// constants and allows removal of non-top elements while maintaining the
1638/// priority order.
1639class LeafPrioQueue {
1640 SmallVector<WeightedLeaf, 8> Q;
1641 bool HaveConst;
1642 WeightedLeaf ConstElt;
1643 unsigned Opcode;
1644
1645public:
1646 bool empty() {
1647 return (!HaveConst && Q.empty());
1648 }
1649
1650 size_t size() {
1651 return Q.size() + HaveConst;
1652 }
1653
1654 bool hasConst() {
1655 return HaveConst;
1656 }
1657
1658 const WeightedLeaf &top() {
1659 if (HaveConst)
1660 return ConstElt;
1661 return Q.front();
1662 }
1663
1664 WeightedLeaf pop() {
1665 if (HaveConst) {
1666 HaveConst = false;
1667 return ConstElt;
1668 }
1669 std::pop_heap(Q.begin(), Q.end(), WeightedLeaf::Compare);
1670 return Q.pop_back_val();
1671 }
1672
1673 void push(WeightedLeaf L, bool SeparateConst=true) {
1674 if (!HaveConst && SeparateConst && isa<ConstantSDNode>(L.Value)) {
1675 if (Opcode == ISD::MUL &&
1676 cast<ConstantSDNode>(L.Value)->getSExtValue() == 1)
1677 return;
1678 if (Opcode == ISD::ADD &&
1679 cast<ConstantSDNode>(L.Value)->getSExtValue() == 0)
1680 return;
1681
1682 HaveConst = true;
1683 ConstElt = L;
1684 } else {
1685 Q.push_back(L);
1686 std::push_heap(Q.begin(), Q.end(), WeightedLeaf::Compare);
1687 }
1688 }
1689
1690 /// Push L to the bottom of the queue regardless of its weight. If L is
1691 /// constant, it will not be folded with other constants in the queue.
1692 void pushToBottom(WeightedLeaf L) {
1693 L.Weight = 1000;
1694 push(L, false);
1695 }
1696
1697 /// Search for a SHL(x, [<=MaxAmount]) subtree in the queue, return the one of
1698 /// lowest weight and remove it from the queue.
1699 WeightedLeaf findSHL(uint64_t MaxAmount);
1700
1701 WeightedLeaf findMULbyConst();
1702
1703 LeafPrioQueue(unsigned Opcode) :
1704 HaveConst(false), Opcode(Opcode) { }
1705};
1706
1707WeightedLeaf LeafPrioQueue::findSHL(uint64_t MaxAmount) {
1708 int ResultPos;
1709 WeightedLeaf Result;
1710
1711 for (int Pos = 0, End = Q.size(); Pos != End; ++Pos) {
1712 const WeightedLeaf &L = Q[Pos];
1713 const SDValue &Val = L.Value;
1714 if (Val.getOpcode() != ISD::SHL ||
1715 !isa<ConstantSDNode>(Val.getOperand(1)) ||
1716 Val.getConstantOperandVal(1) > MaxAmount)
1717 continue;
1718 if (!Result.Value.getNode() || Result.Weight > L.Weight ||
1719 (Result.Weight == L.Weight && Result.InsertionOrder > L.InsertionOrder))
1720 {
1721 Result = L;
1722 ResultPos = Pos;
1723 }
1724 }
1725
1726 if (Result.Value.getNode()) {
1727 Q.erase(&Q[ResultPos]);
1728 std::make_heap(Q.begin(), Q.end(), WeightedLeaf::Compare);
1729 }
1730
1731 return Result;
1732}
1733
1734WeightedLeaf LeafPrioQueue::findMULbyConst() {
1735 int ResultPos;
1736 WeightedLeaf Result;
1737
1738 for (int Pos = 0, End = Q.size(); Pos != End; ++Pos) {
1739 const WeightedLeaf &L = Q[Pos];
1740 const SDValue &Val = L.Value;
1741 if (Val.getOpcode() != ISD::MUL ||
1742 !isa<ConstantSDNode>(Val.getOperand(1)) ||
1743 Val.getConstantOperandVal(1) > 127)
1744 continue;
1745 if (!Result.Value.getNode() || Result.Weight > L.Weight ||
1746 (Result.Weight == L.Weight && Result.InsertionOrder > L.InsertionOrder))
1747 {
1748 Result = L;
1749 ResultPos = Pos;
1750 }
1751 }
1752
1753 if (Result.Value.getNode()) {
1754 Q.erase(&Q[ResultPos]);
1755 std::make_heap(Q.begin(), Q.end(), WeightedLeaf::Compare);
1756 }
1757
1758 return Result;
1759}
1760
1761SDValue HexagonDAGToDAGISel::getMultiplierForSHL(SDNode *N) {
1762 uint64_t MulFactor = 1 << N->getConstantOperandVal(1);
1763 return CurDAG->getConstant(MulFactor, SDLoc(N),
1764 N->getOperand(1).getValueType());
1765}
1766
1767/// @returns the value x for which 2^x is a factor of Val
1768static unsigned getPowerOf2Factor(SDValue Val) {
1769 if (Val.getOpcode() == ISD::MUL) {
1770 unsigned MaxFactor = 0;
1771 for (int i=0; i < 2; ++i) {
1772 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val.getOperand(i));
1773 if (!C)
1774 continue;
1775 const APInt &CInt = C->getAPIntValue();
1776 if (CInt.getBoolValue())
1777 MaxFactor = CInt.countTrailingZeros();
1778 }
1779 return MaxFactor;
1780 }
1781 if (Val.getOpcode() == ISD::SHL) {
1782 if (!isa<ConstantSDNode>(Val.getOperand(1).getNode()))
1783 return 0;
1784 return (unsigned) Val.getConstantOperandVal(1);
1785 }
1786
1787 return 0;
1788}
1789
1790/// @returns true if V>>Amount will eliminate V's operation on its child
1791static bool willShiftRightEliminate(SDValue V, unsigned Amount) {
1792 if (V.getOpcode() == ISD::MUL) {
1793 SDValue Ops[] = { V.getOperand(0), V.getOperand(1) };
1794 for (int i=0; i < 2; ++i)
1795 if (isa<ConstantSDNode>(Ops[i].getNode()) &&
1796 V.getConstantOperandVal(i) % ((uint64_t)1 << Amount) == 0) {
1797 uint64_t NewConst = V.getConstantOperandVal(i) >> Amount;
1798 return (NewConst == 1);
1799 }
1800 } else if (V.getOpcode() == ISD::SHL) {
1801 return (Amount == V.getConstantOperandVal(1));
1802 }
1803
1804 return false;
1805}
1806
1807SDValue HexagonDAGToDAGISel::factorOutPowerOf2(SDValue V, unsigned Power) {
1808 SDValue Ops[] = { V.getOperand(0), V.getOperand(1) };
1809 if (V.getOpcode() == ISD::MUL) {
1810 for (int i=0; i < 2; ++i) {
1811 if (isa<ConstantSDNode>(Ops[i].getNode()) &&
1812 V.getConstantOperandVal(i) % ((uint64_t)1 << Power) == 0) {
1813 uint64_t NewConst = V.getConstantOperandVal(i) >> Power;
1814 if (NewConst == 1)
1815 return Ops[!i];
1816 Ops[i] = CurDAG->getConstant(NewConst,
1817 SDLoc(V), V.getValueType());
1818 break;
1819 }
1820 }
1821 } else if (V.getOpcode() == ISD::SHL) {
1822 uint64_t ShiftAmount = V.getConstantOperandVal(1);
1823 if (ShiftAmount == Power)
1824 return Ops[0];
1825 Ops[1] = CurDAG->getConstant(ShiftAmount - Power,
1826 SDLoc(V), V.getValueType());
1827 }
1828
1829 return CurDAG->getNode(V.getOpcode(), SDLoc(V), V.getValueType(), Ops);
1830}
1831
1832static bool isTargetConstant(const SDValue &V) {
1833 return V.getOpcode() == HexagonISD::CONST32 ||
1834 V.getOpcode() == HexagonISD::CONST32_GP;
1835}
1836
1837unsigned HexagonDAGToDAGISel::getUsesInFunction(const Value *V) {
1838 if (GAUsesInFunction.count(V))
1839 return GAUsesInFunction[V];
1840
1841 unsigned Result = 0;
1842 const Function *CurF = CurDAG->getMachineFunction().getFunction();
1843 for (const User *U : V->users()) {
1844 if (isa<Instruction>(U) &&
1845 cast<Instruction>(U)->getParent()->getParent() == CurF)
1846 ++Result;
1847 }
1848
1849 GAUsesInFunction[V] = Result;
1850
1851 return Result;
1852}
1853
1854/// Note - After calling this, N may be dead. It may have been replaced by a
1855/// new node, so always use the returned value in place of N.
1856///
1857/// @returns The SDValue taking the place of N (which could be N if it is
1858/// unchanged)
1859SDValue HexagonDAGToDAGISel::balanceSubTree(SDNode *N, bool TopLevel) {
1860 assert(RootWeights.count(N) && "Cannot balance non-root node.");
1861 assert(RootWeights[N] != -2 && "This node was RAUW'd!");
1862 assert(!TopLevel || N->getOpcode() == ISD::ADD);
1863
1864 // Return early if this node was already visited
1865 if (RootWeights[N] != -1)
1866 return SDValue(N, 0);
1867
1868 assert(isOpcodeHandled(N));
1869
1870 SDValue Op0 = N->getOperand(0);
1871 SDValue Op1 = N->getOperand(1);
1872
1873 // Return early if the operands will remain unchanged or are all roots
1874 if ((!isOpcodeHandled(Op0.getNode()) || RootWeights.count(Op0.getNode())) &&
1875 (!isOpcodeHandled(Op1.getNode()) || RootWeights.count(Op1.getNode()))) {
1876 SDNode *Op0N = Op0.getNode();
1877 int Weight;
1878 if (isOpcodeHandled(Op0N) && RootWeights[Op0N] == -1) {
1879 Weight = getWeight(balanceSubTree(Op0N).getNode());
1880 // Weight = calculateWeight(Op0N);
1881 } else
1882 Weight = getWeight(Op0N);
1883
1884 SDNode *Op1N = N->getOperand(1).getNode(); // Op1 may have been RAUWd
1885 if (isOpcodeHandled(Op1N) && RootWeights[Op1N] == -1) {
1886 Weight += getWeight(balanceSubTree(Op1N).getNode());
1887 // Weight += calculateWeight(Op1N);
1888 } else
1889 Weight += getWeight(Op1N);
1890
1891 RootWeights[N] = Weight;
1892 RootHeights[N] = std::max(getHeight(N->getOperand(0).getNode()),
1893 getHeight(N->getOperand(1).getNode())) + 1;
1894
1895 DEBUG(dbgs() << "--> No need to balance root (Weight=" << Weight
1896 << " Height=" << RootHeights[N] << "): ");
1897 DEBUG(N->dump());
1898
1899 return SDValue(N, 0);
1900 }
1901
1902 DEBUG(dbgs() << "** Balancing root node: ");
1903 DEBUG(N->dump());
1904
1905 unsigned NOpcode = N->getOpcode();
1906
1907 LeafPrioQueue Leaves(NOpcode);
1908 SmallVector<SDValue, 4> Worklist;
1909 Worklist.push_back(SDValue(N, 0));
1910
1911 // SHL nodes will be converted to MUL nodes
1912 if (NOpcode == ISD::SHL)
1913 NOpcode = ISD::MUL;
1914
1915 bool CanFactorize = false;
1916 WeightedLeaf Mul1, Mul2;
1917 unsigned MaxPowerOf2 = 0;
1918 WeightedLeaf GA;
1919
1920 // Do not try to factor out a shift if there is already a shift at the tip of
1921 // the tree.
1922 bool HaveTopLevelShift = false;
1923 if (TopLevel &&
1924 ((isOpcodeHandled(Op0.getNode()) && Op0.getOpcode() == ISD::SHL &&
1925 Op0.getConstantOperandVal(1) < 4) ||
1926 (isOpcodeHandled(Op1.getNode()) && Op1.getOpcode() == ISD::SHL &&
1927 Op1.getConstantOperandVal(1) < 4)))
1928 HaveTopLevelShift = true;
1929
1930 // Flatten the subtree into an ordered list of leaves; at the same time
1931 // determine whether the tree is already balanced.
1932 int InsertionOrder = 0;
1933 SmallDenseMap<SDValue, int> NodeHeights;
1934 bool Imbalanced = false;
1935 int CurrentWeight = 0;
1936 while (!Worklist.empty()) {
1937 SDValue Child = Worklist.pop_back_val();
1938
1939 if (Child.getNode() != N && RootWeights.count(Child.getNode())) {
1940 // CASE 1: Child is a root note
1941
1942 int Weight = RootWeights[Child.getNode()];
1943 if (Weight == -1) {
1944 Child = balanceSubTree(Child.getNode());
1945 // calculateWeight(Child.getNode());
1946 Weight = getWeight(Child.getNode());
1947 } else if (Weight == -2) {
1948 // Whoops, this node was RAUWd by one of the balanceSubTree calls we
1949 // made. Our worklist isn't up to date anymore.
1950 // Restart the whole process.
1951 DEBUG(dbgs() << "--> Subtree was RAUWd. Restarting...\n");
1952 return balanceSubTree(N, TopLevel);
1953 }
1954
1955 NodeHeights[Child] = 1;
1956 CurrentWeight += Weight;
1957
1958 unsigned PowerOf2;
1959 if (TopLevel && !CanFactorize && !HaveTopLevelShift &&
1960 (Child.getOpcode() == ISD::MUL || Child.getOpcode() == ISD::SHL) &&
1961 Child.hasOneUse() && (PowerOf2 = getPowerOf2Factor(Child))) {
1962 // Try to identify two factorizable MUL/SHL children greedily. Leave
1963 // them out of the priority queue for now so we can deal with them
1964 // after.
1965 if (!Mul1.Value.getNode()) {
1966 Mul1 = WeightedLeaf(Child, Weight, InsertionOrder++);
1967 MaxPowerOf2 = PowerOf2;
1968 } else {
1969 Mul2 = WeightedLeaf(Child, Weight, InsertionOrder++);
1970 MaxPowerOf2 = std::min(MaxPowerOf2, PowerOf2);
1971
1972 // Our addressing modes can only shift by a maximum of 3
1973 if (MaxPowerOf2 > 3)
1974 MaxPowerOf2 = 3;
1975
1976 CanFactorize = true;
1977 }
1978 } else
1979 Leaves.push(WeightedLeaf(Child, Weight, InsertionOrder++));
1980 } else if (!isOpcodeHandled(Child.getNode())) {
1981 // CASE 2: Child is an unhandled kind of node (e.g. constant)
1982 int Weight = getWeight(Child.getNode());
1983
1984 NodeHeights[Child] = getHeight(Child.getNode());
1985 CurrentWeight += Weight;
1986
1987 if (isTargetConstant(Child) && !GA.Value.getNode())
1988 GA = WeightedLeaf(Child, Weight, InsertionOrder++);
1989 else
1990 Leaves.push(WeightedLeaf(Child, Weight, InsertionOrder++));
1991 } else {
1992 // CASE 3: Child is a subtree of same opcode
1993 // Visit children first, then flatten.
1994 unsigned ChildOpcode = Child.getOpcode();
1995 assert(ChildOpcode == NOpcode ||
1996 (NOpcode == ISD::MUL && ChildOpcode == ISD::SHL));
1997
1998 // Convert SHL to MUL
1999 SDValue Op1;
2000 if (ChildOpcode == ISD::SHL)
2001 Op1 = getMultiplierForSHL(Child.getNode());
2002 else
2003 Op1 = Child->getOperand(1);
2004
2005 if (!NodeHeights.count(Op1) || !NodeHeights.count(Child->getOperand(0))) {
2006 assert(!NodeHeights.count(Child) && "Parent visited before children?");
2007 // Visit children first, then re-visit this node
2008 Worklist.push_back(Child);
2009 Worklist.push_back(Op1);
2010 Worklist.push_back(Child->getOperand(0));
2011 } else {
2012 // Back at this node after visiting the children
2013 if (std::abs(NodeHeights[Op1] - NodeHeights[Child->getOperand(0)]) > 1)
2014 Imbalanced = true;
2015
2016 NodeHeights[Child] = std::max(NodeHeights[Op1],
2017 NodeHeights[Child->getOperand(0)]) + 1;
2018 }
2019 }
2020 }
2021
2022 DEBUG(dbgs() << "--> Current height=" << NodeHeights[SDValue(N, 0)]
2023 << " weight=" << CurrentWeight << " imbalanced="
2024 << Imbalanced << "\n");
2025
2026 // Transform MUL(x, C * 2^Y) + SHL(z, Y) -> SHL(ADD(MUL(x, C), z), Y)
2027 // This factors out a shift in order to match memw(a<<Y+b).
2028 if (CanFactorize && (willShiftRightEliminate(Mul1.Value, MaxPowerOf2) ||
2029 willShiftRightEliminate(Mul2.Value, MaxPowerOf2))) {
2030 DEBUG(dbgs() << "--> Found common factor for two MUL children!\n");
2031 int Weight = Mul1.Weight + Mul2.Weight;
2032 int Height = std::max(NodeHeights[Mul1.Value], NodeHeights[Mul2.Value]) + 1;
2033 SDValue Mul1Factored = factorOutPowerOf2(Mul1.Value, MaxPowerOf2);
2034 SDValue Mul2Factored = factorOutPowerOf2(Mul2.Value, MaxPowerOf2);
2035 SDValue Sum = CurDAG->getNode(ISD::ADD, SDLoc(N), Mul1.Value.getValueType(),
2036 Mul1Factored, Mul2Factored);
2037 SDValue Const = CurDAG->getConstant(MaxPowerOf2, SDLoc(N),
2038 Mul1.Value.getValueType());
2039 SDValue New = CurDAG->getNode(ISD::SHL, SDLoc(N), Mul1.Value.getValueType(),
2040 Sum, Const);
2041 NodeHeights[New] = Height;
2042 Leaves.push(WeightedLeaf(New, Weight, Mul1.InsertionOrder));
2043 } else if (Mul1.Value.getNode()) {
2044 // We failed to factorize two MULs, so now the Muls are left outside the
2045 // queue... add them back.
2046 Leaves.push(Mul1);
2047 if (Mul2.Value.getNode())
2048 Leaves.push(Mul2);
2049 CanFactorize = false;
2050 }
2051
2052 // Combine GA + Constant -> GA+Offset, but only if GA is not used elsewhere
2053 // and the root node itself is not used more than twice. This reduces the
2054 // amount of additional constant extenders introduced by this optimization.
2055 bool CombinedGA = false;
2056 if (NOpcode == ISD::ADD && GA.Value.getNode() && Leaves.hasConst() &&
2057 GA.Value.hasOneUse() && N->use_size() < 3) {
2058 GlobalAddressSDNode *GANode =
2059 cast<GlobalAddressSDNode>(GA.Value.getOperand(0));
2060 ConstantSDNode *Offset = cast<ConstantSDNode>(Leaves.top().Value);
2061
2062 if (getUsesInFunction(GANode->getGlobal()) == 1 && Offset->hasOneUse() &&
2063 getTargetLowering()->isOffsetFoldingLegal(GANode)) {
2064 DEBUG(dbgs() << "--> Combining GA and offset (" << Offset->getSExtValue()
2065 << "): ");
2066 DEBUG(GANode->dump());
2067
2068 SDValue NewTGA =
2069 CurDAG->getTargetGlobalAddress(GANode->getGlobal(), SDLoc(GA.Value),
2070 GANode->getValueType(0),
2071 GANode->getOffset() + (uint64_t)Offset->getSExtValue());
2072 GA.Value = CurDAG->getNode(GA.Value.getOpcode(), SDLoc(GA.Value),
2073 GA.Value.getValueType(), NewTGA);
2074 GA.Weight += Leaves.top().Weight;
2075
2076 NodeHeights[GA.Value] = getHeight(GA.Value.getNode());
2077 CombinedGA = true;
2078
2079 Leaves.pop(); // Remove the offset constant from the queue
2080 }
2081 }
2082
2083 if ((RebalanceOnlyForOptimizations && !CanFactorize && !CombinedGA) ||
2084 (RebalanceOnlyImbalancedTrees && !Imbalanced)) {
2085 RootWeights[N] = CurrentWeight;
2086 RootHeights[N] = NodeHeights[SDValue(N, 0)];
2087
2088 return SDValue(N, 0);
2089 }
2090
2091 // Combine GA + SHL(x, C<=31) so we will match Rx=add(#u8,asl(Rx,#U5))
2092 if (NOpcode == ISD::ADD && GA.Value.getNode()) {
2093 WeightedLeaf SHL = Leaves.findSHL(31);
2094 if (SHL.Value.getNode()) {
2095 int Height = std::max(NodeHeights[GA.Value], NodeHeights[SHL.Value]) + 1;
2096 GA.Value = CurDAG->getNode(ISD::ADD, SDLoc(GA.Value),
2097 GA.Value.getValueType(),
2098 GA.Value, SHL.Value);
2099 GA.Weight = SHL.Weight; // Specifically ignore the GA weight here
2100 NodeHeights[GA.Value] = Height;
2101 }
2102 }
2103
2104 if (GA.Value.getNode())
2105 Leaves.push(GA);
2106
2107 // If this is the top level and we haven't factored out a shift, we should try
2108 // to move a constant to the bottom to match addressing modes like memw(rX+C)
2109 if (TopLevel && !CanFactorize && Leaves.hasConst()) {
2110 DEBUG(dbgs() << "--> Pushing constant to tip of tree.");
2111 Leaves.pushToBottom(Leaves.pop());
2112 }
2113
2114 // Rebuild the tree using Huffman's algorithm
2115 while (Leaves.size() > 1) {
2116 WeightedLeaf L0 = Leaves.pop();
2117
2118 // See whether we can grab a MUL to form an add(Rx,mpyi(Ry,#u6)),
2119 // otherwise just get the next leaf
2120 WeightedLeaf L1 = Leaves.findMULbyConst();
2121 if (!L1.Value.getNode())
2122 L1 = Leaves.pop();
2123
2124 assert(L0.Weight <= L1.Weight && "Priority queue is broken!");
2125
2126 SDValue V0 = L0.Value;
2127 int V0Weight = L0.Weight;
2128 SDValue V1 = L1.Value;
2129 int V1Weight = L1.Weight;
2130
2131 // Make sure that none of these nodes have been RAUW'd
2132 if ((RootWeights.count(V0.getNode()) && RootWeights[V0.getNode()] == -2) ||
2133 (RootWeights.count(V1.getNode()) && RootWeights[V1.getNode()] == -2)) {
2134 DEBUG(dbgs() << "--> Subtree was RAUWd. Restarting...\n");
2135 return balanceSubTree(N, TopLevel);
2136 }
2137
2138 ConstantSDNode *V0C = dyn_cast<ConstantSDNode>(V0);
2139 ConstantSDNode *V1C = dyn_cast<ConstantSDNode>(V1);
2140 EVT VT = N->getValueType(0);
2141 SDValue NewNode;
2142
2143 if (V0C && !V1C) {
2144 std::swap(V0, V1);
2145 std::swap(V0C, V1C);
2146 }
2147
2148 // Calculate height of this node
2149 assert(NodeHeights.count(V0) && NodeHeights.count(V1) &&
2150 "Children must have been visited before re-combining them!");
2151 int Height = std::max(NodeHeights[V0], NodeHeights[V1]) + 1;
2152
2153 // Rebuild this node (and restore SHL from MUL if needed)
2154 if (V1C && NOpcode == ISD::MUL && V1C->getAPIntValue().isPowerOf2())
2155 NewNode = CurDAG->getNode(
2156 ISD::SHL, SDLoc(V0), VT, V0,
2157 CurDAG->getConstant(
2158 V1C->getAPIntValue().logBase2(), SDLoc(N),
2159 getTargetLowering()->getScalarShiftAmountTy(CurDAG->getDataLayout(), V0.getValueType())));
2160 else
2161 NewNode = CurDAG->getNode(NOpcode, SDLoc(N), VT, V0, V1);
2162
2163 NodeHeights[NewNode] = Height;
2164
2165 int Weight = V0Weight + V1Weight;
2166 Leaves.push(WeightedLeaf(NewNode, Weight, L0.InsertionOrder));
2167
2168 DEBUG(dbgs() << "--> Built new node (Weight=" << Weight << ",Height="
2169 << Height << "):\n");
2170 DEBUG(NewNode.dump());
2171 }
2172
2173 assert(Leaves.size() == 1);
2174 SDValue NewRoot = Leaves.top().Value;
2175
2176 assert(NodeHeights.count(NewRoot));
2177 int Height = NodeHeights[NewRoot];
2178
2179 // Restore SHL if we earlier converted it to a MUL
2180 if (NewRoot.getOpcode() == ISD::MUL) {
2181 ConstantSDNode *V1C = dyn_cast<ConstantSDNode>(NewRoot.getOperand(1));
2182 if (V1C && V1C->getAPIntValue().isPowerOf2()) {
2183 EVT VT = NewRoot.getValueType();
2184 SDValue V0 = NewRoot.getOperand(0);
2185 NewRoot = CurDAG->getNode(
2186 ISD::SHL, SDLoc(NewRoot), VT, V0,
2187 CurDAG->getConstant(V1C->getAPIntValue().logBase2(), SDLoc(NewRoot),
2188 getTargetLowering()->getScalarShiftAmountTy(
2189 CurDAG->getDataLayout(), V0.getValueType())));
2190 }
2191 }
2192
2193 if (N != NewRoot.getNode()) {
2194 DEBUG(dbgs() << "--> Root is now: ");
2195 DEBUG(NewRoot.dump());
2196
2197 // Replace all uses of old root by new root
2198 CurDAG->ReplaceAllUsesWith(N, NewRoot.getNode());
2199 // Mark that we have RAUW'd N
2200 RootWeights[N] = -2;
2201 } else {
2202 DEBUG(dbgs() << "--> Root unchanged.\n");
2203 }
2204
2205 RootWeights[NewRoot.getNode()] = Leaves.top().Weight;
2206 RootHeights[NewRoot.getNode()] = Height;
2207
2208 return NewRoot;
2209}
2210
2211void HexagonDAGToDAGISel::rebalanceAddressTrees() {
2212 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
2213 E = CurDAG->allnodes_end(); I != E;) {
2214 SDNode *N = &*I++;
2215 if (N->getOpcode() != ISD::LOAD && N->getOpcode() != ISD::STORE)
2216 continue;
2217
2218 SDValue BasePtr = cast<MemSDNode>(N)->getBasePtr();
2219 if (BasePtr.getOpcode() != ISD::ADD)
2220 continue;
2221
2222 // We've already processed this node
2223 if (RootWeights.count(BasePtr.getNode()))
2224 continue;
2225
2226 DEBUG(dbgs() << "** Rebalancing address calculation in node: ");
2227 DEBUG(N->dump());
2228
2229 // FindRoots
2230 SmallVector<SDNode *, 4> Worklist;
2231
2232 Worklist.push_back(BasePtr.getOperand(0).getNode());
2233 Worklist.push_back(BasePtr.getOperand(1).getNode());
2234
2235 while (!Worklist.empty()) {
2236 SDNode *N = Worklist.pop_back_val();
2237 unsigned Opcode = N->getOpcode();
2238
2239 if (!isOpcodeHandled(N))
2240 continue;
2241
2242 Worklist.push_back(N->getOperand(0).getNode());
2243 Worklist.push_back(N->getOperand(1).getNode());
2244
2245 // Not a root if it has only one use and same opcode as its parent
2246 if (N->hasOneUse() && Opcode == N->use_begin()->getOpcode())
2247 continue;
2248
2249 // This root node has already been processed
2250 if (RootWeights.count(N))
2251 continue;
2252
2253 RootWeights[N] = -1;
2254 }
2255
2256 // Balance node itself
2257 RootWeights[BasePtr.getNode()] = -1;
2258 SDValue NewBasePtr = balanceSubTree(BasePtr.getNode(), /*TopLevel=*/ true);
2259
2260 if (N->getOpcode() == ISD::LOAD)
2261 N = CurDAG->UpdateNodeOperands(N, N->getOperand(0),
2262 NewBasePtr, N->getOperand(2));
2263 else
2264 N = CurDAG->UpdateNodeOperands(N, N->getOperand(0), N->getOperand(1),
2265 NewBasePtr, N->getOperand(3));
2266
2267 DEBUG(dbgs() << "--> Final node: ");
2268 DEBUG(N->dump());
2269 }
2270
2271 CurDAG->RemoveDeadNodes();
2272 GAUsesInFunction.clear();
2273 RootHeights.clear();
2274 RootWeights.clear();
2275}
2276
2277