Clean up some signedness oddities in this code noticed by clang.
[oota-llvm.git] / lib / Target / Blackfin / BlackfinISelDAGToDAG.cpp
1 //===- BlackfinISelDAGToDAG.cpp - A dag to dag inst selector for Blackfin -===//
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 Blackfin target.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "Blackfin.h"
15 #include "BlackfinISelLowering.h"
16 #include "BlackfinTargetMachine.h"
17 #include "BlackfinRegisterInfo.h"
18 #include "llvm/Intrinsics.h"
19 #include "llvm/CodeGen/SelectionDAGISel.h"
20 #include "llvm/CodeGen/MachineRegisterInfo.h"
21 #include "llvm/Support/Compiler.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/raw_ostream.h"
25
26 using namespace llvm;
27
28 //===----------------------------------------------------------------------===//
29 // Instruction Selector Implementation
30 //===----------------------------------------------------------------------===//
31
32 //===----------------------------------------------------------------------===//
33 /// BlackfinDAGToDAGISel - Blackfin specific code to select blackfin machine
34 /// instructions for SelectionDAG operations.
35 namespace {
36   class BlackfinDAGToDAGISel : public SelectionDAGISel {
37     /// Subtarget - Keep a pointer to the Blackfin Subtarget around so that we
38     /// can make the right decision when generating code for different targets.
39     //const BlackfinSubtarget &Subtarget;
40   public:
41     BlackfinDAGToDAGISel(BlackfinTargetMachine &TM, CodeGenOpt::Level OptLevel)
42       : SelectionDAGISel(TM, OptLevel) {}
43
44     virtual void PostprocessISelDAG();
45
46     virtual const char *getPassName() const {
47       return "Blackfin DAG->DAG Pattern Instruction Selection";
48     }
49
50     // Include the pieces autogenerated from the target description.
51 #include "BlackfinGenDAGISel.inc"
52
53   private:
54     SDNode *Select(SDNode *N);
55     bool SelectADDRspii(SDNode *Op, SDValue Addr,
56                         SDValue &Base, SDValue &Offset);
57
58     // Walk the DAG after instruction selection, fixing register class issues.
59     void FixRegisterClasses(SelectionDAG &DAG);
60
61     const BlackfinInstrInfo &getInstrInfo() {
62       return *static_cast<const BlackfinTargetMachine&>(TM).getInstrInfo();
63     }
64     const BlackfinRegisterInfo *getRegisterInfo() {
65       return static_cast<const BlackfinTargetMachine&>(TM).getRegisterInfo();
66     }
67   };
68 }  // end anonymous namespace
69
70 FunctionPass *llvm::createBlackfinISelDag(BlackfinTargetMachine &TM,
71                                           CodeGenOpt::Level OptLevel) {
72   return new BlackfinDAGToDAGISel(TM, OptLevel);
73 }
74
75 void BlackfinDAGToDAGISel::PostprocessISelDAG() {
76   FixRegisterClasses(*CurDAG);
77 }
78
79 SDNode *BlackfinDAGToDAGISel::Select(SDNode *N) {
80   if (N->isMachineOpcode())
81     return NULL;   // Already selected.
82
83   switch (N->getOpcode()) {
84   default: break;
85   case ISD::FrameIndex: {
86     // Selects to ADDpp FI, 0 which in turn will become ADDimm7 SP, imm or ADDpp
87     // SP, Px
88     int FI = cast<FrameIndexSDNode>(N)->getIndex();
89     SDValue TFI = CurDAG->getTargetFrameIndex(FI, MVT::i32);
90     return CurDAG->SelectNodeTo(N, BF::ADDpp, MVT::i32, TFI,
91                                 CurDAG->getTargetConstant(0, MVT::i32));
92   }
93   }
94
95   return SelectCode(N);
96 }
97
98 bool BlackfinDAGToDAGISel::SelectADDRspii(SDNode *Op,
99                                           SDValue Addr,
100                                           SDValue &Base,
101                                           SDValue &Offset) {
102   FrameIndexSDNode *FIN = 0;
103   if ((FIN = dyn_cast<FrameIndexSDNode>(Addr))) {
104     Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
105     Offset = CurDAG->getTargetConstant(0, MVT::i32);
106     return true;
107   }
108   if (Addr.getOpcode() == ISD::ADD) {
109     ConstantSDNode *CN = 0;
110     if ((FIN = dyn_cast<FrameIndexSDNode>(Addr.getOperand(0))) &&
111         (CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1))) &&
112         (CN->getSExtValue() % 4 == 0 && CN->getSExtValue() >= 0)) {
113       // Constant positive word offset from frame index
114       Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
115       Offset = CurDAG->getTargetConstant(CN->getSExtValue(), MVT::i32);
116       return true;
117     }
118   }
119   return false;
120 }
121
122 static inline bool isCC(const TargetRegisterClass *RC) {
123   return RC == &BF::AnyCCRegClass || BF::AnyCCRegClass.hasSubClass(RC);
124 }
125
126 static inline bool isDCC(const TargetRegisterClass *RC) {
127   return RC == &BF::DRegClass || BF::DRegClass.hasSubClass(RC) || isCC(RC);
128 }
129
130 static void UpdateNodeOperand(SelectionDAG &DAG,
131                               SDNode *N,
132                               unsigned Num,
133                               SDValue Val) {
134   SmallVector<SDValue, 8> ops(N->op_begin(), N->op_end());
135   ops[Num] = Val;
136   SDValue New = DAG.UpdateNodeOperands(SDValue(N, 0), ops.data(), ops.size());
137   DAG.ReplaceAllUsesWith(N, New.getNode());
138 }
139
140 // After instruction selection, insert COPY_TO_REGCLASS nodes to help in
141 // choosing the proper register classes.
142 void BlackfinDAGToDAGISel::FixRegisterClasses(SelectionDAG &DAG) {
143   const BlackfinInstrInfo &TII = getInstrInfo();
144   const BlackfinRegisterInfo *TRI = getRegisterInfo();
145   DAG.AssignTopologicalOrder();
146   HandleSDNode Dummy(DAG.getRoot());
147
148   for (SelectionDAG::allnodes_iterator NI = DAG.allnodes_begin();
149        NI != DAG.allnodes_end(); ++NI) {
150     if (NI->use_empty() || !NI->isMachineOpcode())
151       continue;
152     const TargetInstrDesc &DefTID = TII.get(NI->getMachineOpcode());
153     for (SDNode::use_iterator UI = NI->use_begin(); !UI.atEnd(); ++UI) {
154       if (!UI->isMachineOpcode())
155         continue;
156
157       if (UI.getUse().getResNo() >= DefTID.getNumDefs())
158         continue;
159       const TargetRegisterClass *DefRC =
160         DefTID.OpInfo[UI.getUse().getResNo()].getRegClass(TRI);
161
162       const TargetInstrDesc &UseTID = TII.get(UI->getMachineOpcode());
163       if (UseTID.getNumDefs()+UI.getOperandNo() >= UseTID.getNumOperands())
164         continue;
165       const TargetRegisterClass *UseRC =
166         UseTID.OpInfo[UseTID.getNumDefs()+UI.getOperandNo()].getRegClass(TRI);
167       if (!DefRC || !UseRC)
168         continue;
169       // We cannot copy CC <-> !(CC/D)
170       if ((isCC(DefRC) && !isDCC(UseRC)) || (isCC(UseRC) && !isDCC(DefRC))) {
171         SDNode *Copy =
172           DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
173                              NI->getDebugLoc(),
174                              MVT::i32,
175                              UI.getUse().get(),
176                              DAG.getTargetConstant(BF::DRegClassID, MVT::i32));
177         UpdateNodeOperand(DAG, *UI, UI.getOperandNo(), SDValue(Copy, 0));
178       }
179     }
180   }
181   DAG.setRoot(Dummy.getValue());
182 }
183