Implement some feedback from sabre
[oota-llvm.git] / lib / CodeGen / SelectionDAG / TargetLowering.cpp
1 //===-- TargetLowering.cpp - Implement the TargetLowering class -----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This implements the TargetLowering class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Target/TargetLowering.h"
15 #include "llvm/Target/TargetMachine.h"
16 #include "llvm/Target/MRegisterInfo.h"
17 #include "llvm/CodeGen/SelectionDAG.h"
18 #include "llvm/ADT/StringExtras.h"
19 #include "llvm/Support/MathExtras.h"
20 using namespace llvm;
21
22 TargetLowering::TargetLowering(TargetMachine &tm)
23   : TM(tm), TD(TM.getTargetData()) {
24   assert(ISD::BUILTIN_OP_END <= 128 &&
25          "Fixed size array in TargetLowering is not large enough!");
26   // All operations default to being supported.
27   memset(OpActions, 0, sizeof(OpActions));
28
29   IsLittleEndian = TD.isLittleEndian();
30   ShiftAmountTy = SetCCResultTy = PointerTy = getValueType(TD.getIntPtrType());
31   ShiftAmtHandling = Undefined;
32   memset(RegClassForVT, 0,MVT::LAST_VALUETYPE*sizeof(TargetRegisterClass*));
33   maxStoresPerMemSet = maxStoresPerMemCpy = maxStoresPerMemMove = 8;
34   allowUnalignedMemoryAccesses = false;
35   UseUnderscoreSetJmpLongJmp = false;
36   IntDivIsCheap = false;
37   Pow2DivIsCheap = false;
38   StackPointerRegisterToSaveRestore = 0;
39   SchedPreferenceInfo = SchedulingForLatency;
40 }
41
42 TargetLowering::~TargetLowering() {}
43
44 /// setValueTypeAction - Set the action for a particular value type.  This
45 /// assumes an action has not already been set for this value type.
46 static void SetValueTypeAction(MVT::ValueType VT,
47                                TargetLowering::LegalizeAction Action,
48                                TargetLowering &TLI,
49                                MVT::ValueType *TransformToType,
50                         TargetLowering::ValueTypeActionImpl &ValueTypeActions) {
51   ValueTypeActions.setTypeAction(VT, Action);
52   if (Action == TargetLowering::Promote) {
53     MVT::ValueType PromoteTo;
54     if (VT == MVT::f32)
55       PromoteTo = MVT::f64;
56     else {
57       unsigned LargerReg = VT+1;
58       while (!TLI.isTypeLegal((MVT::ValueType)LargerReg)) {
59         ++LargerReg;
60         assert(MVT::isInteger((MVT::ValueType)LargerReg) &&
61                "Nothing to promote to??");
62       }
63       PromoteTo = (MVT::ValueType)LargerReg;
64     }
65
66     assert(MVT::isInteger(VT) == MVT::isInteger(PromoteTo) &&
67            MVT::isFloatingPoint(VT) == MVT::isFloatingPoint(PromoteTo) &&
68            "Can only promote from int->int or fp->fp!");
69     assert(VT < PromoteTo && "Must promote to a larger type!");
70     TransformToType[VT] = PromoteTo;
71   } else if (Action == TargetLowering::Expand) {
72     assert((VT == MVT::Vector || MVT::isInteger(VT)) && VT > MVT::i8 &&
73            "Cannot expand this type: target must support SOME integer reg!");
74     // Expand to the next smaller integer type!
75     TransformToType[VT] = (MVT::ValueType)(VT-1);
76   }
77 }
78
79
80 /// computeRegisterProperties - Once all of the register classes are added,
81 /// this allows us to compute derived properties we expose.
82 void TargetLowering::computeRegisterProperties() {
83   assert(MVT::LAST_VALUETYPE <= 32 &&
84          "Too many value types for ValueTypeActions to hold!");
85
86   // Everything defaults to one.
87   for (unsigned i = 0; i != MVT::LAST_VALUETYPE; ++i)
88     NumElementsForVT[i] = 1;
89
90   // Find the largest integer register class.
91   unsigned LargestIntReg = MVT::i128;
92   for (; RegClassForVT[LargestIntReg] == 0; --LargestIntReg)
93     assert(LargestIntReg != MVT::i1 && "No integer registers defined!");
94
95   // Every integer value type larger than this largest register takes twice as
96   // many registers to represent as the previous ValueType.
97   unsigned ExpandedReg = LargestIntReg; ++LargestIntReg;
98   for (++ExpandedReg; MVT::isInteger((MVT::ValueType)ExpandedReg);++ExpandedReg)
99     NumElementsForVT[ExpandedReg] = 2*NumElementsForVT[ExpandedReg-1];
100
101   // Inspect all of the ValueType's possible, deciding how to process them.
102   for (unsigned IntReg = MVT::i1; IntReg <= MVT::i128; ++IntReg)
103     // If we are expanding this type, expand it!
104     if (getNumElements((MVT::ValueType)IntReg) != 1)
105       SetValueTypeAction((MVT::ValueType)IntReg, Expand, *this, TransformToType,
106                          ValueTypeActions);
107     else if (!isTypeLegal((MVT::ValueType)IntReg))
108       // Otherwise, if we don't have native support, we must promote to a
109       // larger type.
110       SetValueTypeAction((MVT::ValueType)IntReg, Promote, *this,
111                          TransformToType, ValueTypeActions);
112     else
113       TransformToType[(MVT::ValueType)IntReg] = (MVT::ValueType)IntReg;
114
115   // If the target does not have native support for F32, promote it to F64.
116   if (!isTypeLegal(MVT::f32))
117     SetValueTypeAction(MVT::f32, Promote, *this,
118                        TransformToType, ValueTypeActions);
119   else
120     TransformToType[MVT::f32] = MVT::f32;
121   
122   // Set MVT::Vector to always be Expanded
123   SetValueTypeAction(MVT::Vector, Expand, *this, TransformToType, 
124                      ValueTypeActions);
125
126   assert(isTypeLegal(MVT::f64) && "Target does not support FP?");
127   TransformToType[MVT::f64] = MVT::f64;
128 }
129
130 const char *TargetLowering::getTargetNodeName(unsigned Opcode) const {
131   return NULL;
132 }
133
134 /// DemandedBitsAreZero - Return true if 'Op & Mask' demands no bits from a bit
135 /// set operation such as a sign extend or or/xor with constant whose only
136 /// use is Op.  If it returns true, the old node that sets bits which are
137 /// not demanded is returned in Old, and its replacement node is returned in
138 /// New, such that callers of DemandedBitsAreZero may call CombineTo on them if
139 /// desired.
140 bool TargetLowering::DemandedBitsAreZero(const SDOperand &Op, uint64_t Mask, 
141                                          SDOperand &Old, SDOperand &New,
142                                          SelectionDAG &DAG) {
143   // If the operation has more than one use, we're not interested in it.
144   // Tracking down and checking all uses would be problematic and slow.
145   if (!Op.Val->hasOneUse())
146     return false;
147   
148   switch (Op.getOpcode()) {
149   case ISD::AND:
150     // (X & C1) & C2 == 0   iff   C1 & C2 == 0.
151     if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
152       uint64_t NewVal = Mask & AndRHS->getValue();
153       return DemandedBitsAreZero(Op.getOperand(0), NewVal, Old, New, DAG);
154     }
155     break;
156   case ISD::SHL:
157     // (ushl X, C1) & C2 == 0   iff  X & (C2 >> C1) == 0
158     if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
159       uint64_t NewVal = Mask >> ShAmt->getValue();
160       return DemandedBitsAreZero(Op.getOperand(0), NewVal, Old, New, DAG);
161     }
162     break;
163   case ISD::SIGN_EXTEND_INREG: {
164     MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
165     unsigned ExtendBits = MVT::getSizeInBits(EVT);
166     // If we're extending from something smaller than MVT::i64 and all of the
167     // sign extension bits are masked, return true and set New to be the
168     // first operand, since we no longer care what the high bits are.
169     if (ExtendBits < 64 && ((Mask & (~0ULL << ExtendBits)) == 0)) {
170       Old = Op;
171       New = Op.getOperand(0);
172       return true;
173     }
174     break;
175   }
176   case ISD::SRA:
177     if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
178       unsigned OpBits = MVT::getSizeInBits(Op.getValueType());
179       unsigned SH = ShAmt->getValue();
180       if (SH && ((Mask & (~0ULL << (OpBits-SH))) == 0)) {
181         Old = Op;
182         New = DAG.getNode(ISD::SRL, Op.getValueType(), Op.getOperand(0), 
183                           Op.getOperand(1));
184         return true;
185       }
186     }
187     break;
188   }
189   return false;
190 }
191
192 /// MaskedValueIsZero - Return true if 'Op & Mask' is known to be zero.  We use
193 /// this predicate to simplify operations downstream.  Op and Mask are known to
194 /// be the same type.
195 bool TargetLowering::MaskedValueIsZero(const SDOperand &Op,
196                                        uint64_t Mask) const {
197   unsigned SrcBits;
198   if (Mask == 0) return true;
199   
200   // If we know the result of a setcc has the top bits zero, use this info.
201   switch (Op.getOpcode()) {
202   case ISD::Constant:
203     return (cast<ConstantSDNode>(Op)->getValue() & Mask) == 0;
204   case ISD::SETCC:
205     return ((Mask & 1) == 0) &&
206       getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult;
207   case ISD::ZEXTLOAD:
208     SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(3))->getVT());
209     return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
210   case ISD::ZERO_EXTEND:
211     SrcBits = MVT::getSizeInBits(Op.getOperand(0).getValueType());
212     return MaskedValueIsZero(Op.getOperand(0),Mask & (~0ULL >> (64-SrcBits)));
213   case ISD::ANY_EXTEND:
214     // If the mask only includes bits in the low part, recurse.
215     SrcBits = MVT::getSizeInBits(Op.getOperand(0).getValueType());
216     if (Mask >> SrcBits) return false;  // Use of unknown top bits.
217     return MaskedValueIsZero(Op.getOperand(0), Mask);
218   case ISD::AssertZext:
219     SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
220     return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
221   case ISD::AND:
222     // If either of the operands has zero bits, the result will too.
223     if (MaskedValueIsZero(Op.getOperand(1), Mask) ||
224         MaskedValueIsZero(Op.getOperand(0), Mask))
225       return true;
226     // (X & C1) & C2 == 0   iff   C1 & C2 == 0.
227     if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
228       return MaskedValueIsZero(Op.getOperand(0),AndRHS->getValue() & Mask);
229     return false;
230   case ISD::OR:
231   case ISD::XOR:
232     return MaskedValueIsZero(Op.getOperand(0), Mask) &&
233            MaskedValueIsZero(Op.getOperand(1), Mask);
234   case ISD::SELECT:
235     return MaskedValueIsZero(Op.getOperand(1), Mask) &&
236            MaskedValueIsZero(Op.getOperand(2), Mask);
237   case ISD::SELECT_CC:
238     return MaskedValueIsZero(Op.getOperand(2), Mask) &&
239            MaskedValueIsZero(Op.getOperand(3), Mask);
240   case ISD::SRL:
241     // (ushr X, C1) & C2 == 0   iff  X & (C2 << C1) == 0
242     if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
243       uint64_t NewVal = Mask << ShAmt->getValue();
244       SrcBits = MVT::getSizeInBits(Op.getValueType());
245       if (SrcBits != 64) NewVal &= (1ULL << SrcBits)-1;
246       return MaskedValueIsZero(Op.getOperand(0), NewVal);
247     }
248     return false;
249   case ISD::SHL:
250     // (ushl X, C1) & C2 == 0   iff  X & (C2 >> C1) == 0
251     if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
252       uint64_t NewVal = Mask >> ShAmt->getValue();
253       return MaskedValueIsZero(Op.getOperand(0), NewVal);
254     }
255     return false;
256   case ISD::ADD:
257     // (add X, Y) & C == 0 iff (X&C)|(Y&C) == 0 and all bits are low bits.
258     if ((Mask&(Mask+1)) == 0) {  // All low bits
259       if (MaskedValueIsZero(Op.getOperand(0), Mask) &&
260           MaskedValueIsZero(Op.getOperand(1), Mask))
261         return true;
262     }
263     break;
264   case ISD::SUB:
265     if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
266       // We know that the top bits of C-X are clear if X contains less bits
267       // than C (i.e. no wrap-around can happen).  For example, 20-X is
268       // positive if we can prove that X is >= 0 and < 16.
269       unsigned Bits = MVT::getSizeInBits(CLHS->getValueType(0));
270       if ((CLHS->getValue() & (1 << (Bits-1))) == 0) {  // sign bit clear
271         unsigned NLZ = CountLeadingZeros_64(CLHS->getValue()+1);
272         uint64_t MaskV = (1ULL << (63-NLZ))-1;
273         if (MaskedValueIsZero(Op.getOperand(1), ~MaskV)) {
274           // High bits are clear this value is known to be >= C.
275           unsigned NLZ2 = CountLeadingZeros_64(CLHS->getValue());
276           if ((Mask & ((1ULL << (64-NLZ2))-1)) == 0)
277             return true;
278         }
279       }
280     }
281     break;
282   case ISD::CTTZ:
283   case ISD::CTLZ:
284   case ISD::CTPOP:
285     // Bit counting instructions can not set the high bits of the result
286     // register.  The max number of bits sets depends on the input.
287     return (Mask & (MVT::getSizeInBits(Op.getValueType())*2-1)) == 0;
288   default:
289     // Allow the target to implement this method for its nodes.
290     if (Op.getOpcode() >= ISD::BUILTIN_OP_END)
291       return isMaskedValueZeroForTargetNode(Op, Mask);
292     break;
293   }
294   return false;
295 }
296
297 bool TargetLowering::isMaskedValueZeroForTargetNode(const SDOperand &Op,
298                                                     uint64_t Mask) const {
299   assert(Op.getOpcode() >= ISD::BUILTIN_OP_END &&
300          "Should use MaskedValueIsZero if you don't know whether Op"
301          " is a target node!");
302   return false;
303 }
304
305 std::vector<unsigned> TargetLowering::
306 getRegForInlineAsmConstraint(const std::string &Constraint) const {
307   // Not a physreg, must not be a register reference or something.
308   if (Constraint[0] != '{') return std::vector<unsigned>();
309   assert(*(Constraint.end()-1) == '}' && "Not a brace enclosed constraint?");
310
311   // Remove the braces from around the name.
312   std::string RegName(Constraint.begin()+1, Constraint.end()-1);
313   
314   // Scan to see if this constraint is a register name.
315   const MRegisterInfo *RI = TM.getRegisterInfo();
316   for (unsigned i = 1, e = RI->getNumRegs(); i != e; ++i) {
317     if (const char *Name = RI->get(i).Name)
318       if (StringsEqualNoCase(RegName, Name))
319         return std::vector<unsigned>(1, i);
320   }
321   
322   // Unknown physreg.
323   return std::vector<unsigned>();
324 }
325