blob: b45690b57a2d2c20091ab1e011eee809f14e4f43 [file] [log] [blame]
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +00001//===-- MSP430ISelDAGToDAG.cpp - A dag to dag inst selector for MSP430 ----===//
2//
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 MSP430 target.
11//
12//===----------------------------------------------------------------------===//
13
14#include "MSP430.h"
15#include "MSP430ISelLowering.h"
16#include "MSP430TargetMachine.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Function.h"
19#include "llvm/Intrinsics.h"
20#include "llvm/CallingConv.h"
21#include "llvm/Constants.h"
22#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/CodeGen/MachineInstrBuilder.h"
25#include "llvm/CodeGen/MachineRegisterInfo.h"
26#include "llvm/CodeGen/SelectionDAG.h"
27#include "llvm/CodeGen/SelectionDAGISel.h"
28#include "llvm/Target/TargetLowering.h"
Anton Korobeynikova91f4c52009-10-21 19:18:28 +000029#include "llvm/Support/CommandLine.h"
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000030#include "llvm/Support/Compiler.h"
31#include "llvm/Support/Debug.h"
Torok Edwindac237e2009-07-08 20:53:28 +000032#include "llvm/Support/ErrorHandling.h"
33#include "llvm/Support/raw_ostream.h"
Anton Korobeynikovafac8ab2009-10-11 23:03:28 +000034#include "llvm/ADT/Statistic.h"
35
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000036using namespace llvm;
37
Anton Korobeynikova91f4c52009-10-21 19:18:28 +000038#ifndef NDEBUG
39static cl::opt<bool>
40ViewRMWDAGs("view-msp430-rmw-dags", cl::Hidden,
41 cl::desc("Pop up a window to show isel dags after RMW preprocess"));
42#else
43static const bool ViewRMWDAGs = false;
44#endif
45
Anton Korobeynikovafac8ab2009-10-11 23:03:28 +000046STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor");
47
Anton Korobeynikov123ed8f2009-11-07 17:13:35 +000048
49namespace {
50 struct MSP430ISelAddressMode {
51 enum {
52 RegBase,
53 FrameIndexBase
54 } BaseType;
55
56 struct { // This is really a union, discriminated by BaseType!
57 SDValue Reg;
58 int FrameIndex;
59 } Base;
60
61 int16_t Disp;
62 GlobalValue *GV;
63 Constant *CP;
64 BlockAddress *BlockAddr;
65 const char *ES;
66 int JT;
67 unsigned Align; // CP alignment.
68
69 MSP430ISelAddressMode()
70 : BaseType(RegBase), Disp(0), GV(0), CP(0), BlockAddr(0),
71 ES(0), JT(-1), Align(0) {
72 }
73
74 bool hasSymbolicDisplacement() const {
75 return GV != 0 || CP != 0 || ES != 0 || JT != -1;
76 }
77
78 bool hasBaseReg() const {
79 return Base.Reg.getNode() != 0;
80 }
81
82 void setBaseReg(SDValue Reg) {
83 BaseType = RegBase;
84 Base.Reg = Reg;
85 }
86
87 void dump() {
88 errs() << "MSP430ISelAddressMode " << this << '\n';
89 if (Base.Reg.getNode() != 0) {
90 errs() << "Base.Reg ";
91 Base.Reg.getNode()->dump();
92 } else {
93 errs() << " Base.FrameIndex " << Base.FrameIndex << '\n';
94 }
95 errs() << " Disp " << Disp << '\n';
96 if (GV) {
97 errs() << "GV ";
98 GV->dump();
99 } else if (CP) {
100 errs() << " CP ";
101 CP->dump();
102 errs() << " Align" << Align << '\n';
103 } else if (ES) {
104 errs() << "ES ";
105 errs() << ES << '\n';
106 } else if (JT != -1)
107 errs() << " JT" << JT << " Align" << Align << '\n';
108 }
109 };
110}
111
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000112/// MSP430DAGToDAGISel - MSP430 specific code to select MSP430 machine
113/// instructions for SelectionDAG operations.
114///
115namespace {
116 class MSP430DAGToDAGISel : public SelectionDAGISel {
117 MSP430TargetLowering &Lowering;
118 const MSP430Subtarget &Subtarget;
119
120 public:
Anton Korobeynikov60871cb2009-05-03 13:19:42 +0000121 MSP430DAGToDAGISel(MSP430TargetMachine &TM, CodeGenOpt::Level OptLevel)
122 : SelectionDAGISel(TM, OptLevel),
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000123 Lowering(*TM.getTargetLowering()),
124 Subtarget(*TM.getSubtargetImpl()) { }
125
126 virtual void InstructionSelect();
127
128 virtual const char *getPassName() const {
129 return "MSP430 DAG->DAG Pattern Instruction Selection";
130 }
131
Anton Korobeynikov123ed8f2009-11-07 17:13:35 +0000132 bool MatchAddress(SDValue N, MSP430ISelAddressMode &AM);
133 bool MatchWrapper(SDValue N, MSP430ISelAddressMode &AM);
134 bool MatchAddressBase(SDValue N, MSP430ISelAddressMode &AM);
135
Anton Korobeynikovf32df4c2009-10-22 00:16:00 +0000136 bool IsLegalAndProfitableToFold(SDNode *N, SDNode *U,
137 SDNode *Root) const;
138
Anton Korobeynikov95eb4702009-10-11 19:14:21 +0000139 virtual bool
140 SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
141 std::vector<SDValue> &OutOps);
142
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000143 // Include the pieces autogenerated from the target description.
144 #include "MSP430GenDAGISel.inc"
145
146 private:
Anton Korobeynikovf32df4c2009-10-22 00:16:00 +0000147 DenseMap<SDNode*, SDNode*> RMWStores;
Anton Korobeynikovafac8ab2009-10-11 23:03:28 +0000148 void PreprocessForRMW();
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000149 SDNode *Select(SDValue Op);
Anton Korobeynikov6534f832009-11-07 17:15:06 +0000150 SDNode *SelectIndexedLoad(SDValue Op);
Anton Korobeynikovcf14ae52009-05-03 13:09:40 +0000151 bool SelectAddr(SDValue Op, SDValue Addr, SDValue &Base, SDValue &Disp);
Anton Korobeynikov43ed64a2009-05-03 12:58:58 +0000152
153 #ifndef NDEBUG
154 unsigned Indent;
155 #endif
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000156 };
157} // end anonymous namespace
158
159/// createMSP430ISelDag - This pass converts a legalized DAG into a
160/// MSP430-specific DAG, ready for instruction scheduling.
161///
Anton Korobeynikov60871cb2009-05-03 13:19:42 +0000162FunctionPass *llvm::createMSP430ISelDag(MSP430TargetMachine &TM,
163 CodeGenOpt::Level OptLevel) {
164 return new MSP430DAGToDAGISel(TM, OptLevel);
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000165}
166
Anton Korobeynikov123ed8f2009-11-07 17:13:35 +0000167
168/// MatchWrapper - Try to match MSP430ISD::Wrapper node into an addressing mode.
169/// These wrap things that will resolve down into a symbol reference. If no
170/// match is possible, this returns true, otherwise it returns false.
171bool MSP430DAGToDAGISel::MatchWrapper(SDValue N, MSP430ISelAddressMode &AM) {
172 // If the addressing mode already has a symbol as the displacement, we can
173 // never match another symbol.
174 if (AM.hasSymbolicDisplacement())
175 return true;
176
177 SDValue N0 = N.getOperand(0);
178
179 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
180 AM.GV = G->getGlobal();
181 AM.Disp += G->getOffset();
182 //AM.SymbolFlags = G->getTargetFlags();
183 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
184 AM.CP = CP->getConstVal();
185 AM.Align = CP->getAlignment();
186 AM.Disp += CP->getOffset();
187 //AM.SymbolFlags = CP->getTargetFlags();
188 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(N0)) {
189 AM.ES = S->getSymbol();
190 //AM.SymbolFlags = S->getTargetFlags();
191 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
192 AM.JT = J->getIndex();
193 //AM.SymbolFlags = J->getTargetFlags();
194 } else {
195 AM.BlockAddr = cast<BlockAddressSDNode>(N0)->getBlockAddress();
196 //AM.SymbolFlags = cast<BlockAddressSDNode>(N0)->getTargetFlags();
197 }
198 return false;
199}
200
201/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
202/// specified addressing mode without any further recursion.
203bool MSP430DAGToDAGISel::MatchAddressBase(SDValue N, MSP430ISelAddressMode &AM) {
204 // Is the base register already occupied?
205 if (AM.BaseType != MSP430ISelAddressMode::RegBase || AM.Base.Reg.getNode()) {
206 // If so, we cannot select it.
Anton Korobeynikov82e46c22009-05-03 13:10:11 +0000207 return true;
208 }
Anton Korobeynikov36b6e532009-05-03 13:06:03 +0000209
Anton Korobeynikov123ed8f2009-11-07 17:13:35 +0000210 // Default, generate it as a register.
211 AM.BaseType = MSP430ISelAddressMode::RegBase;
212 AM.Base.Reg = N;
213 return false;
214}
Anton Korobeynikov36b6e532009-05-03 13:06:03 +0000215
Anton Korobeynikov123ed8f2009-11-07 17:13:35 +0000216bool MSP430DAGToDAGISel::MatchAddress(SDValue N, MSP430ISelAddressMode &AM) {
217 DebugLoc dl = N.getDebugLoc();
218 DEBUG({
219 errs() << "MatchAddress: ";
220 AM.dump();
221 });
222
223 switch (N.getOpcode()) {
224 default: break;
225 case ISD::Constant: {
226 uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
227 AM.Disp += Val;
228 return false;
229 }
230
Anton Korobeynikov0eb6af42009-05-03 13:08:51 +0000231 case MSP430ISD::Wrapper:
Anton Korobeynikov123ed8f2009-11-07 17:13:35 +0000232 if (!MatchWrapper(N, AM))
233 return false;
234 break;
235
236 case ISD::FrameIndex:
237 if (AM.BaseType == MSP430ISelAddressMode::RegBase
238 && AM.Base.Reg.getNode() == 0) {
239 AM.BaseType = MSP430ISelAddressMode::FrameIndexBase;
240 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
241 return false;
Anton Korobeynikov0eb6af42009-05-03 13:08:51 +0000242 }
243 break;
Anton Korobeynikov36b6e532009-05-03 13:06:03 +0000244
Anton Korobeynikov123ed8f2009-11-07 17:13:35 +0000245 case ISD::ADD: {
246 MSP430ISelAddressMode Backup = AM;
247 if (!MatchAddress(N.getNode()->getOperand(0), AM) &&
248 !MatchAddress(N.getNode()->getOperand(1), AM))
249 return false;
250 AM = Backup;
251 if (!MatchAddress(N.getNode()->getOperand(1), AM) &&
252 !MatchAddress(N.getNode()->getOperand(0), AM))
253 return false;
254 AM = Backup;
255
256 break;
257 }
258
259 case ISD::OR:
260 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
261 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
262 MSP430ISelAddressMode Backup = AM;
263 uint64_t Offset = CN->getSExtValue();
264 // Start with the LHS as an addr mode.
265 if (!MatchAddress(N.getOperand(0), AM) &&
266 // Address could not have picked a GV address for the displacement.
267 AM.GV == NULL &&
268 // Check to see if the LHS & C is zero.
269 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
270 AM.Disp += Offset;
271 return false;
272 }
273 AM = Backup;
274 }
275 break;
276 }
277
278 return MatchAddressBase(N, AM);
279}
280
281/// SelectAddr - returns true if it is able pattern match an addressing mode.
282/// It returns the operands which make up the maximal addressing mode it can
283/// match by reference.
284bool MSP430DAGToDAGISel::SelectAddr(SDValue Op, SDValue N,
285 SDValue &Base, SDValue &Disp) {
286 MSP430ISelAddressMode AM;
287
288 if (MatchAddress(N, AM))
289 return false;
290
291 EVT VT = N.getValueType();
292 if (AM.BaseType == MSP430ISelAddressMode::RegBase) {
293 if (!AM.Base.Reg.getNode())
294 AM.Base.Reg = CurDAG->getRegister(0, VT);
295 }
296
297 Base = (AM.BaseType == MSP430ISelAddressMode::FrameIndexBase) ?
298 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
299 AM.Base.Reg;
300
301 if (AM.GV)
302 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i16, AM.Disp,
303 0/*AM.SymbolFlags*/);
304 else if (AM.CP)
305 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i16,
306 AM.Align, AM.Disp, 0/*AM.SymbolFlags*/);
307 else if (AM.ES)
308 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i16, 0/*AM.SymbolFlags*/);
309 else if (AM.JT != -1)
310 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i16, 0/*AM.SymbolFlags*/);
311 else if (AM.BlockAddr)
312 Disp = CurDAG->getBlockAddress(AM.BlockAddr, DebugLoc()/*MVT::i32*/,
313 true /*AM.SymbolFlags*/);
314 else
315 Disp = CurDAG->getTargetConstant(AM.Disp, MVT::i16);
Anton Korobeynikov36b6e532009-05-03 13:06:03 +0000316
317 return true;
318}
319
Anton Korobeynikov95eb4702009-10-11 19:14:21 +0000320bool MSP430DAGToDAGISel::
321SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
322 std::vector<SDValue> &OutOps) {
323 SDValue Op0, Op1;
324 switch (ConstraintCode) {
325 default: return true;
326 case 'm': // memory
327 if (!SelectAddr(Op, Op, Op0, Op1))
328 return true;
329 break;
330 }
331
332 OutOps.push_back(Op0);
333 OutOps.push_back(Op1);
334 return false;
335}
Anton Korobeynikov36b6e532009-05-03 13:06:03 +0000336
Anton Korobeynikovf32df4c2009-10-22 00:16:00 +0000337bool MSP430DAGToDAGISel::IsLegalAndProfitableToFold(SDNode *N, SDNode *U,
338 SDNode *Root) const {
339 if (OptLevel == CodeGenOpt::None) return false;
340
341 /// RMW preprocessing creates the following code:
Benjamin Kramer1395d1d2009-10-22 09:28:49 +0000342 /// [Load1]
343 /// ^ ^
344 /// / |
345 /// / |
Anton Korobeynikovf32df4c2009-10-22 00:16:00 +0000346 /// [Load2] |
347 /// ^ ^ |
348 /// | | |
349 /// | \-|
350 /// | |
351 /// | [Op]
352 /// | ^
353 /// | |
354 /// \ /
355 /// \ /
356 /// [Store]
357 ///
358 /// The path Store => Load2 => Load1 is via chain. Note that in general it is
359 /// not allowed to fold Load1 into Op (and Store) since it will creates a
360 /// cycle. However, this is perfectly legal for the loads moved below the
361 /// TokenFactor by PreprocessForRMW. Query the map Store => Load1 (created
362 /// during preprocessing) to determine whether it's legal to introduce such
363 /// "cycle" for a moment.
364 DenseMap<SDNode*, SDNode*>::iterator I = RMWStores.find(Root);
365 if (I != RMWStores.end() && I->second == N)
366 return true;
367
368 // Proceed to 'generic' cycle finder code
369 return SelectionDAGISel::IsLegalAndProfitableToFold(N, U, Root);
370}
371
372
Anton Korobeynikovafac8ab2009-10-11 23:03:28 +0000373/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
374/// and move load below the TokenFactor. Replace store's chain operand with
375/// load's chain result.
Anton Korobeynikovafac8ab2009-10-11 23:03:28 +0000376static void MoveBelowTokenFactor(SelectionDAG *CurDAG, SDValue Load,
377 SDValue Store, SDValue TF) {
378 SmallVector<SDValue, 4> Ops;
Anton Korobeynikovafac8ab2009-10-11 23:03:28 +0000379 for (unsigned i = 0, e = TF.getNode()->getNumOperands(); i != e; ++i)
Anton Korobeynikov83fceb92009-10-21 19:17:55 +0000380 if (Load.getNode() == TF.getOperand(i).getNode())
381 Ops.push_back(Load.getOperand(0));
382 else
383 Ops.push_back(TF.getOperand(i));
384 SDValue NewTF = CurDAG->UpdateNodeOperands(TF, &Ops[0], Ops.size());
Anton Korobeynikovafac8ab2009-10-11 23:03:28 +0000385 SDValue NewLoad = CurDAG->UpdateNodeOperands(Load, NewTF,
386 Load.getOperand(1),
387 Load.getOperand(2));
388 CurDAG->UpdateNodeOperands(Store, NewLoad.getValue(1), Store.getOperand(1),
389 Store.getOperand(2), Store.getOperand(3));
390}
391
Anton Korobeynikovf32df4c2009-10-22 00:16:00 +0000392/// MoveBelowTokenFactor2 - Replace TokenFactor operand with load's chain operand
393/// and move load below the TokenFactor. Replace store's chain operand with
394/// load's chain result. This a version which sinks two loads below token factor.
395/// Look into PreprocessForRMW comments for explanation of transform.
396static void MoveBelowTokenFactor2(SelectionDAG *CurDAG,
397 SDValue Load1, SDValue Load2,
398 SDValue Store, SDValue TF) {
399 SmallVector<SDValue, 4> Ops;
400 for (unsigned i = 0, e = TF.getNode()->getNumOperands(); i != e; ++i) {
401 SDNode* N = TF.getOperand(i).getNode();
402 if (Load2.getNode() == N)
403 Ops.push_back(Load2.getOperand(0));
404 else if (Load1.getNode() != N)
405 Ops.push_back(TF.getOperand(i));
406 }
407
408 SDValue NewTF = SDValue(CurDAG->MorphNodeTo(TF.getNode(),
409 TF.getOpcode(),
410 TF.getNode()->getVTList(),
411 &Ops[0], Ops.size()), TF.getResNo());
412 SDValue NewLoad2 = CurDAG->UpdateNodeOperands(Load2, NewTF,
413 Load2.getOperand(1),
414 Load2.getOperand(2));
415
416 SDValue NewLoad1 = CurDAG->UpdateNodeOperands(Load1, NewLoad2.getValue(1),
417 Load1.getOperand(1),
418 Load1.getOperand(2));
419
420 CurDAG->UpdateNodeOperands(Store,
421 NewLoad1.getValue(1),
422 Store.getOperand(1),
423 Store.getOperand(2), Store.getOperand(3));
424}
425
426/// isAllowedToSink - return true if N a load which can be moved below token
427/// factor. Basically, the load should be non-volatile and has single use.
428static bool isLoadAllowedToSink(SDValue N, SDValue Chain) {
Anton Korobeynikovafac8ab2009-10-11 23:03:28 +0000429 if (N.getOpcode() == ISD::BIT_CONVERT)
430 N = N.getOperand(0);
431
432 LoadSDNode *LD = dyn_cast<LoadSDNode>(N);
433 if (!LD || LD->isVolatile())
434 return false;
435 if (LD->getAddressingMode() != ISD::UNINDEXED)
436 return false;
437
438 ISD::LoadExtType ExtType = LD->getExtensionType();
439 if (ExtType != ISD::NON_EXTLOAD && ExtType != ISD::EXTLOAD)
440 return false;
441
Anton Korobeynikovf32df4c2009-10-22 00:16:00 +0000442 return (N.hasOneUse() &&
443 LD->hasNUsesOfValue(1, 1) &&
444 LD->isOperandOf(Chain.getNode()));
445}
446
447
448/// isRMWLoad - Return true if N is a load that's part of RMW sub-DAG.
449/// The chain produced by the load must only be used by the store's chain
450/// operand, otherwise this may produce a cycle in the DAG.
451static bool isRMWLoad(SDValue N, SDValue Chain, SDValue Address,
452 SDValue &Load) {
453 if (isLoadAllowedToSink(N, Chain) &&
454 N.getOperand(1) == Address) {
Anton Korobeynikovafac8ab2009-10-11 23:03:28 +0000455 Load = N;
456 return true;
457 }
458 return false;
459}
460
461/// PreprocessForRMW - Preprocess the DAG to make instruction selection better.
Anton Korobeynikov83fceb92009-10-21 19:17:55 +0000462/// This is only run if not in -O0 mode.
463/// This allows the instruction selector to pick more read-modify-write
464/// instructions. This is a common case:
465///
466/// [Load chain]
467/// ^
468/// |
469/// [Load]
470/// ^ ^
471/// | |
472/// / \-
473/// / |
474/// [TokenFactor] [Op]
475/// ^ ^
476/// | |
477/// \ /
478/// \ /
479/// [Store]
480///
481/// The fact the store's chain operand != load's chain will prevent the
482/// (store (op (load))) instruction from being selected. We can transform it to:
483///
484/// [Load chain]
485/// ^
486/// |
487/// [TokenFactor]
488/// ^
489/// |
490/// [Load]
491/// ^ ^
492/// | |
493/// | \-
494/// | |
495/// | [Op]
496/// | ^
497/// | |
498/// \ /
499/// \ /
500/// [Store]
Anton Korobeynikovf32df4c2009-10-22 00:16:00 +0000501///
502/// We also recognize the case where second operand of Op is load as well and
503/// move it below token factor as well creating DAG as follows:
504///
Benjamin Kramer1395d1d2009-10-22 09:28:49 +0000505/// [Load chain]
506/// ^
507/// |
508/// [TokenFactor]
509/// ^
510/// |
511/// [Load1]
512/// ^ ^
513/// / |
514/// / |
Anton Korobeynikovf32df4c2009-10-22 00:16:00 +0000515/// [Load2] |
516/// ^ ^ |
517/// | | |
518/// | \-|
519/// | |
520/// | [Op]
521/// | ^
522/// | |
523/// \ /
524/// \ /
525/// [Store]
526///
527/// This allows selection of mem-mem instructions. Yay!
528
Anton Korobeynikovafac8ab2009-10-11 23:03:28 +0000529void MSP430DAGToDAGISel::PreprocessForRMW() {
530 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
531 E = CurDAG->allnodes_end(); I != E; ++I) {
532 if (!ISD::isNON_TRUNCStore(I))
533 continue;
Anton Korobeynikovafac8ab2009-10-11 23:03:28 +0000534 SDValue Chain = I->getOperand(0);
Anton Korobeynikov83fceb92009-10-21 19:17:55 +0000535
Anton Korobeynikovafac8ab2009-10-11 23:03:28 +0000536 if (Chain.getNode()->getOpcode() != ISD::TokenFactor)
537 continue;
538
Anton Korobeynikov83fceb92009-10-21 19:17:55 +0000539 SDValue N1 = I->getOperand(1);
540 SDValue N2 = I->getOperand(2);
541 if ((N1.getValueType().isFloatingPoint() &&
542 !N1.getValueType().isVector()) ||
543 !N1.hasOneUse())
Anton Korobeynikovafac8ab2009-10-11 23:03:28 +0000544 continue;
545
Anton Korobeynikovf32df4c2009-10-22 00:16:00 +0000546 unsigned RModW = 0;
547 SDValue Load1, Load2;
Anton Korobeynikovafac8ab2009-10-11 23:03:28 +0000548 unsigned Opcode = N1.getNode()->getOpcode();
549 switch (Opcode) {
Anton Korobeynikov83fceb92009-10-21 19:17:55 +0000550 case ISD::ADD:
551 case ISD::AND:
552 case ISD::OR:
553 case ISD::XOR:
554 case ISD::ADDC:
555 case ISD::ADDE: {
556 SDValue N10 = N1.getOperand(0);
557 SDValue N11 = N1.getOperand(1);
Anton Korobeynikovf32df4c2009-10-22 00:16:00 +0000558 if (isRMWLoad(N10, Chain, N2, Load1)) {
559 if (isLoadAllowedToSink(N11, Chain)) {
560 Load2 = N11;
561 RModW = 2;
562 } else
563 RModW = 1;
564 } else if (isRMWLoad(N11, Chain, N2, Load1)) {
565 if (isLoadAllowedToSink(N10, Chain)) {
566 Load2 = N10;
567 RModW = 2;
568 } else
569 RModW = 1;
570 }
Anton Korobeynikov83fceb92009-10-21 19:17:55 +0000571 break;
572 }
573 case ISD::SUB:
574 case ISD::SUBC:
575 case ISD::SUBE: {
576 SDValue N10 = N1.getOperand(0);
Anton Korobeynikovf32df4c2009-10-22 00:16:00 +0000577 SDValue N11 = N1.getOperand(1);
578 if (isRMWLoad(N10, Chain, N2, Load1)) {
579 if (isLoadAllowedToSink(N11, Chain)) {
580 Load2 = N11;
581 RModW = 2;
582 } else
583 RModW = 1;
584 }
Anton Korobeynikov83fceb92009-10-21 19:17:55 +0000585 break;
586 }
Anton Korobeynikovafac8ab2009-10-11 23:03:28 +0000587 }
588
Anton Korobeynikovf32df4c2009-10-22 00:16:00 +0000589 NumLoadMoved += RModW;
590 if (RModW == 1)
591 MoveBelowTokenFactor(CurDAG, Load1, SDValue(I, 0), Chain);
592 else if (RModW == 2) {
593 MoveBelowTokenFactor2(CurDAG, Load1, Load2, SDValue(I, 0), Chain);
594 SDNode* Store = I;
595 RMWStores[Store] = Load2.getNode();
Anton Korobeynikovafac8ab2009-10-11 23:03:28 +0000596 }
597 }
598}
599
Anton Korobeynikov6534f832009-11-07 17:15:06 +0000600
601SDNode *MSP430DAGToDAGISel::SelectIndexedLoad(SDValue Op) {
602 LoadSDNode *LD = cast<LoadSDNode>(Op);
603 ISD::MemIndexedMode AM = LD->getAddressingMode();
604 if (AM != ISD::POST_INC || LD->getExtensionType() != ISD::NON_EXTLOAD)
605 return NULL;
606
607 EVT VT = LD->getMemoryVT();
608
609 unsigned Opcode = 0;
610 switch (VT.getSimpleVT().SimpleTy) {
611 case MVT::i8:
612 // Sanity check
613 if (cast<ConstantSDNode>(LD->getOffset())->getZExtValue() != 1)
614 return NULL;
615
616 Opcode = MSP430::MOV8rm_POST;
617 break;
618 case MVT::i16:
619 // Sanity check
620 if (cast<ConstantSDNode>(LD->getOffset())->getZExtValue() != 2)
621 return NULL;
622
623 Opcode = MSP430::MOV16rm_POST;
624 break;
625 default:
626 return NULL;
627 }
628
629 return CurDAG->getMachineNode(Opcode, Op.getDebugLoc(),
630 VT.getSimpleVT().SimpleTy, MVT::i16, MVT::Other,
631 LD->getBasePtr(), LD->getChain());
632}
633
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000634/// InstructionSelect - This callback is invoked by
635/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Anton Korobeynikov9e123392009-05-03 12:58:40 +0000636void MSP430DAGToDAGISel::InstructionSelect() {
Anton Korobeynikova91f4c52009-10-21 19:18:28 +0000637 std::string BlockName;
638 if (ViewRMWDAGs)
639 BlockName = MF->getFunction()->getNameStr() + ":" +
640 BB->getBasicBlock()->getNameStr();
641
Anton Korobeynikovafac8ab2009-10-11 23:03:28 +0000642 PreprocessForRMW();
643
Anton Korobeynikova91f4c52009-10-21 19:18:28 +0000644 if (ViewRMWDAGs) CurDAG->viewGraph("RMW preprocessed:" + BlockName);
645
Anton Korobeynikovafac8ab2009-10-11 23:03:28 +0000646 DEBUG(errs() << "Selection DAG after RMW preprocessing:\n");
647 DEBUG(CurDAG->dump());
648
Anton Korobeynikovbf8ef3f2009-05-03 13:16:37 +0000649 // Codegen the basic block.
Chris Lattner893e1c92009-08-23 06:49:22 +0000650 DEBUG(errs() << "===== Instruction selection begins:\n");
Daniel Dunbar43ed2672009-08-23 08:50:52 +0000651 DEBUG(Indent = 0);
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000652 SelectRoot(*CurDAG);
Chris Lattner893e1c92009-08-23 06:49:22 +0000653 DEBUG(errs() << "===== Instruction selection ends:\n");
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000654
655 CurDAG->RemoveDeadNodes();
Anton Korobeynikovf32df4c2009-10-22 00:16:00 +0000656 RMWStores.clear();
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000657}
658
659SDNode *MSP430DAGToDAGISel::Select(SDValue Op) {
Anton Korobeynikov43ed64a2009-05-03 12:58:58 +0000660 SDNode *Node = Op.getNode();
Anton Korobeynikov40477312009-05-03 13:10:26 +0000661 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov43ed64a2009-05-03 12:58:58 +0000662
663 // Dump information about the Node being selected
Chris Lattner893e1c92009-08-23 06:49:22 +0000664 DEBUG(errs().indent(Indent) << "Selecting: ");
Anton Korobeynikov43ed64a2009-05-03 12:58:58 +0000665 DEBUG(Node->dump(CurDAG));
Chris Lattner893e1c92009-08-23 06:49:22 +0000666 DEBUG(errs() << "\n");
Daniel Dunbar43ed2672009-08-23 08:50:52 +0000667 DEBUG(Indent += 2);
Anton Korobeynikov43ed64a2009-05-03 12:58:58 +0000668
669 // If we have a custom node, we already have selected!
670 if (Node->isMachineOpcode()) {
Chris Lattner893e1c92009-08-23 06:49:22 +0000671 DEBUG(errs().indent(Indent-2) << "== ";
672 Node->dump(CurDAG);
673 errs() << "\n");
Daniel Dunbar43ed2672009-08-23 08:50:52 +0000674 DEBUG(Indent -= 2);
Anton Korobeynikov43ed64a2009-05-03 12:58:58 +0000675 return NULL;
676 }
677
Anton Korobeynikov40477312009-05-03 13:10:26 +0000678 // Few custom selection stuff.
679 switch (Node->getOpcode()) {
680 default: break;
681 case ISD::FrameIndex: {
Owen Anderson825b72b2009-08-11 20:47:22 +0000682 assert(Op.getValueType() == MVT::i16);
Anton Korobeynikov40477312009-05-03 13:10:26 +0000683 int FI = cast<FrameIndexSDNode>(Node)->getIndex();
Owen Anderson825b72b2009-08-11 20:47:22 +0000684 SDValue TFI = CurDAG->getTargetFrameIndex(FI, MVT::i16);
Anton Korobeynikov40477312009-05-03 13:10:26 +0000685 if (Node->hasOneUse())
Owen Anderson825b72b2009-08-11 20:47:22 +0000686 return CurDAG->SelectNodeTo(Node, MSP430::ADD16ri, MVT::i16,
687 TFI, CurDAG->getTargetConstant(0, MVT::i16));
Dan Gohman602b0c82009-09-25 18:54:59 +0000688 return CurDAG->getMachineNode(MSP430::ADD16ri, dl, MVT::i16,
689 TFI, CurDAG->getTargetConstant(0, MVT::i16));
Anton Korobeynikov40477312009-05-03 13:10:26 +0000690 }
Anton Korobeynikov6534f832009-11-07 17:15:06 +0000691 case ISD::LOAD:
692 if (SDNode *ResNode = SelectIndexedLoad(Op))
693 return ResNode;
694 // Other cases are autogenerated.
695 break;
Anton Korobeynikov40477312009-05-03 13:10:26 +0000696 }
Anton Korobeynikov43ed64a2009-05-03 12:58:58 +0000697
698 // Select the default instruction
699 SDNode *ResNode = SelectCode(Op);
700
Chris Lattner893e1c92009-08-23 06:49:22 +0000701 DEBUG(errs() << std::string(Indent-2, ' ') << "=> ");
Anton Korobeynikov43ed64a2009-05-03 12:58:58 +0000702 if (ResNode == NULL || ResNode == Op.getNode())
703 DEBUG(Op.getNode()->dump(CurDAG));
704 else
705 DEBUG(ResNode->dump(CurDAG));
Chris Lattner893e1c92009-08-23 06:49:22 +0000706 DEBUG(errs() << "\n");
Daniel Dunbar43ed2672009-08-23 08:50:52 +0000707 DEBUG(Indent -= 2);
Anton Korobeynikov43ed64a2009-05-03 12:58:58 +0000708
709 return ResNode;
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000710}