blob: 7a4d269f799795b4d6bc854da48837cb1f41f562 [file] [log] [blame]
Chris Lattner310968c2005-01-07 07:44:53 +00001//===-- TargetLowering.cpp - Implement the TargetLowering class -----------===//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
Chris Lattner310968c2005-01-07 07:44:53 +00003// 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.
Misha Brukmanf976c852005-04-21 22:55:34 +00007//
Chris Lattner310968c2005-01-07 07:44:53 +00008//===----------------------------------------------------------------------===//
9//
10// This implements the TargetLowering class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Target/TargetLowering.h"
15#include "llvm/Target/TargetMachine.h"
Chris Lattner4ccb0702006-01-26 20:37:03 +000016#include "llvm/Target/MRegisterInfo.h"
Chris Lattner310968c2005-01-07 07:44:53 +000017#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner4ccb0702006-01-26 20:37:03 +000018#include "llvm/ADT/StringExtras.h"
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +000019#include "llvm/Support/MathExtras.h"
Chris Lattner310968c2005-01-07 07:44:53 +000020using namespace llvm;
21
22TargetLowering::TargetLowering(TargetMachine &tm)
Chris Lattner3e6e8cc2006-01-29 08:41:12 +000023 : TM(tm), TD(TM.getTargetData()) {
Evan Cheng33143dc2006-03-03 06:58:59 +000024 assert(ISD::BUILTIN_OP_END <= 156 &&
Chris Lattner310968c2005-01-07 07:44:53 +000025 "Fixed size array in TargetLowering is not large enough!");
Chris Lattnercba82f92005-01-16 07:28:11 +000026 // All operations default to being supported.
27 memset(OpActions, 0, sizeof(OpActions));
Chris Lattner310968c2005-01-07 07:44:53 +000028
29 IsLittleEndian = TD.isLittleEndian();
Chris Lattner714b69d2005-01-16 23:59:48 +000030 ShiftAmountTy = SetCCResultTy = PointerTy = getValueType(TD.getIntPtrType());
Chris Lattnerd6e49672005-01-19 03:36:14 +000031 ShiftAmtHandling = Undefined;
Chris Lattner310968c2005-01-07 07:44:53 +000032 memset(RegClassForVT, 0,MVT::LAST_VALUETYPE*sizeof(TargetRegisterClass*));
Chris Lattner00ffed02006-03-01 04:52:55 +000033 memset(TargetDAGCombineArray, 0,
34 sizeof(TargetDAGCombineArray)/sizeof(TargetDAGCombineArray[0]));
Evan Chenga03a5dc2006-02-14 08:38:30 +000035 maxStoresPerMemset = maxStoresPerMemcpy = maxStoresPerMemmove = 8;
Reid Spencer0f9beca2005-08-27 19:09:02 +000036 allowUnalignedMemoryAccesses = false;
Chris Lattner8e6be8b2005-09-27 22:13:56 +000037 UseUnderscoreSetJmpLongJmp = false;
Nate Begeman405e3ec2005-10-21 00:02:42 +000038 IntDivIsCheap = false;
39 Pow2DivIsCheap = false;
Chris Lattneree4a7652006-01-25 18:57:15 +000040 StackPointerRegisterToSaveRestore = 0;
Evan Cheng0577a222006-01-25 18:52:42 +000041 SchedPreferenceInfo = SchedulingForLatency;
Chris Lattner310968c2005-01-07 07:44:53 +000042}
43
Chris Lattnercba82f92005-01-16 07:28:11 +000044TargetLowering::~TargetLowering() {}
45
Chris Lattnerbb97d812005-01-16 01:10:58 +000046/// setValueTypeAction - Set the action for a particular value type. This
47/// assumes an action has not already been set for this value type.
Chris Lattnercba82f92005-01-16 07:28:11 +000048static void SetValueTypeAction(MVT::ValueType VT,
49 TargetLowering::LegalizeAction Action,
Chris Lattnerbb97d812005-01-16 01:10:58 +000050 TargetLowering &TLI,
51 MVT::ValueType *TransformToType,
Chris Lattner3e6e8cc2006-01-29 08:41:12 +000052 TargetLowering::ValueTypeActionImpl &ValueTypeActions) {
53 ValueTypeActions.setTypeAction(VT, Action);
Chris Lattnercba82f92005-01-16 07:28:11 +000054 if (Action == TargetLowering::Promote) {
Chris Lattnerbb97d812005-01-16 01:10:58 +000055 MVT::ValueType PromoteTo;
56 if (VT == MVT::f32)
57 PromoteTo = MVT::f64;
58 else {
59 unsigned LargerReg = VT+1;
Chris Lattner9ed62c12005-08-24 16:34:12 +000060 while (!TLI.isTypeLegal((MVT::ValueType)LargerReg)) {
Chris Lattnerbb97d812005-01-16 01:10:58 +000061 ++LargerReg;
62 assert(MVT::isInteger((MVT::ValueType)LargerReg) &&
63 "Nothing to promote to??");
64 }
65 PromoteTo = (MVT::ValueType)LargerReg;
66 }
67
68 assert(MVT::isInteger(VT) == MVT::isInteger(PromoteTo) &&
69 MVT::isFloatingPoint(VT) == MVT::isFloatingPoint(PromoteTo) &&
70 "Can only promote from int->int or fp->fp!");
71 assert(VT < PromoteTo && "Must promote to a larger type!");
72 TransformToType[VT] = PromoteTo;
Chris Lattnercba82f92005-01-16 07:28:11 +000073 } else if (Action == TargetLowering::Expand) {
Nate Begeman4ef3b812005-11-22 01:29:36 +000074 assert((VT == MVT::Vector || MVT::isInteger(VT)) && VT > MVT::i8 &&
Chris Lattnerbb97d812005-01-16 01:10:58 +000075 "Cannot expand this type: target must support SOME integer reg!");
76 // Expand to the next smaller integer type!
77 TransformToType[VT] = (MVT::ValueType)(VT-1);
78 }
79}
80
81
Chris Lattner310968c2005-01-07 07:44:53 +000082/// computeRegisterProperties - Once all of the register classes are added,
83/// this allows us to compute derived properties we expose.
84void TargetLowering::computeRegisterProperties() {
Nate Begeman6a648612005-11-29 05:45:29 +000085 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattnerbb97d812005-01-16 01:10:58 +000086 "Too many value types for ValueTypeActions to hold!");
87
Chris Lattner310968c2005-01-07 07:44:53 +000088 // Everything defaults to one.
89 for (unsigned i = 0; i != MVT::LAST_VALUETYPE; ++i)
90 NumElementsForVT[i] = 1;
Misha Brukmanf976c852005-04-21 22:55:34 +000091
Chris Lattner310968c2005-01-07 07:44:53 +000092 // Find the largest integer register class.
93 unsigned LargestIntReg = MVT::i128;
94 for (; RegClassForVT[LargestIntReg] == 0; --LargestIntReg)
95 assert(LargestIntReg != MVT::i1 && "No integer registers defined!");
96
97 // Every integer value type larger than this largest register takes twice as
98 // many registers to represent as the previous ValueType.
99 unsigned ExpandedReg = LargestIntReg; ++LargestIntReg;
100 for (++ExpandedReg; MVT::isInteger((MVT::ValueType)ExpandedReg);++ExpandedReg)
101 NumElementsForVT[ExpandedReg] = 2*NumElementsForVT[ExpandedReg-1];
Chris Lattner310968c2005-01-07 07:44:53 +0000102
Chris Lattnerbb97d812005-01-16 01:10:58 +0000103 // Inspect all of the ValueType's possible, deciding how to process them.
104 for (unsigned IntReg = MVT::i1; IntReg <= MVT::i128; ++IntReg)
105 // If we are expanding this type, expand it!
106 if (getNumElements((MVT::ValueType)IntReg) != 1)
Chris Lattnercba82f92005-01-16 07:28:11 +0000107 SetValueTypeAction((MVT::ValueType)IntReg, Expand, *this, TransformToType,
Chris Lattnerbb97d812005-01-16 01:10:58 +0000108 ValueTypeActions);
Chris Lattner9ed62c12005-08-24 16:34:12 +0000109 else if (!isTypeLegal((MVT::ValueType)IntReg))
Chris Lattnerbb97d812005-01-16 01:10:58 +0000110 // Otherwise, if we don't have native support, we must promote to a
111 // larger type.
Chris Lattnercba82f92005-01-16 07:28:11 +0000112 SetValueTypeAction((MVT::ValueType)IntReg, Promote, *this,
113 TransformToType, ValueTypeActions);
Chris Lattnercfdfe4c2005-01-16 01:20:18 +0000114 else
115 TransformToType[(MVT::ValueType)IntReg] = (MVT::ValueType)IntReg;
Misha Brukmanf976c852005-04-21 22:55:34 +0000116
Chris Lattnerbb97d812005-01-16 01:10:58 +0000117 // If the target does not have native support for F32, promote it to F64.
Chris Lattner9ed62c12005-08-24 16:34:12 +0000118 if (!isTypeLegal(MVT::f32))
Chris Lattnercba82f92005-01-16 07:28:11 +0000119 SetValueTypeAction(MVT::f32, Promote, *this,
120 TransformToType, ValueTypeActions);
Chris Lattnercfdfe4c2005-01-16 01:20:18 +0000121 else
122 TransformToType[MVT::f32] = MVT::f32;
Nate Begeman4ef3b812005-11-22 01:29:36 +0000123
124 // Set MVT::Vector to always be Expanded
125 SetValueTypeAction(MVT::Vector, Expand, *this, TransformToType,
126 ValueTypeActions);
Chris Lattnercfdfe4c2005-01-16 01:20:18 +0000127
Chris Lattner9ed62c12005-08-24 16:34:12 +0000128 assert(isTypeLegal(MVT::f64) && "Target does not support FP?");
Chris Lattnercfdfe4c2005-01-16 01:20:18 +0000129 TransformToType[MVT::f64] = MVT::f64;
Chris Lattnerbb97d812005-01-16 01:10:58 +0000130}
Chris Lattnercba82f92005-01-16 07:28:11 +0000131
Evan Cheng72261582005-12-20 06:22:03 +0000132const char *TargetLowering::getTargetNodeName(unsigned Opcode) const {
133 return NULL;
134}
Evan Cheng3a03ebb2005-12-21 23:05:39 +0000135
Chris Lattnereb8146b2006-02-04 02:13:02 +0000136//===----------------------------------------------------------------------===//
137// Optimization Methods
138//===----------------------------------------------------------------------===//
139
Nate Begeman368e18d2006-02-16 21:11:51 +0000140/// ShrinkDemandedConstant - Check to see if the specified operand of the
141/// specified instruction is a constant integer. If so, check to see if there
142/// are any bits set in the constant that are not demanded. If so, shrink the
143/// constant and return true.
144bool TargetLowering::TargetLoweringOpt::ShrinkDemandedConstant(SDOperand Op,
145 uint64_t Demanded) {
Chris Lattnerec665152006-02-26 23:36:02 +0000146 // FIXME: ISD::SELECT, ISD::SELECT_CC
Nate Begeman368e18d2006-02-16 21:11:51 +0000147 switch(Op.getOpcode()) {
148 default: break;
Nate Begemande996292006-02-03 22:24:05 +0000149 case ISD::AND:
Nate Begeman368e18d2006-02-16 21:11:51 +0000150 case ISD::OR:
151 case ISD::XOR:
152 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
153 if ((~Demanded & C->getValue()) != 0) {
154 MVT::ValueType VT = Op.getValueType();
155 SDOperand New = DAG.getNode(Op.getOpcode(), VT, Op.getOperand(0),
156 DAG.getConstant(Demanded & C->getValue(),
157 VT));
158 return CombineTo(Op, New);
Nate Begemande996292006-02-03 22:24:05 +0000159 }
Nate Begemande996292006-02-03 22:24:05 +0000160 break;
161 }
162 return false;
163}
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000164
Nate Begeman368e18d2006-02-16 21:11:51 +0000165/// SimplifyDemandedBits - Look at Op. At this point, we know that only the
166/// DemandedMask bits of the result of Op are ever used downstream. If we can
167/// use this information to simplify Op, create a new simplified DAG node and
168/// return true, returning the original and new nodes in Old and New. Otherwise,
169/// analyze the expression and return a mask of KnownOne and KnownZero bits for
170/// the expression (used to simplify the caller). The KnownZero/One bits may
171/// only be accurate for those bits in the DemandedMask.
172bool TargetLowering::SimplifyDemandedBits(SDOperand Op, uint64_t DemandedMask,
173 uint64_t &KnownZero,
174 uint64_t &KnownOne,
175 TargetLoweringOpt &TLO,
176 unsigned Depth) const {
177 KnownZero = KnownOne = 0; // Don't know anything.
178 // Other users may use these bits.
179 if (!Op.Val->hasOneUse()) {
180 if (Depth != 0) {
181 // If not at the root, Just compute the KnownZero/KnownOne bits to
182 // simplify things downstream.
183 ComputeMaskedBits(Op, DemandedMask, KnownZero, KnownOne, Depth);
184 return false;
185 }
186 // If this is the root being simplified, allow it to have multiple uses,
187 // just set the DemandedMask to all bits.
188 DemandedMask = MVT::getIntVTBitMask(Op.getValueType());
189 } else if (DemandedMask == 0) {
190 // Not demanding any bits from Op.
191 if (Op.getOpcode() != ISD::UNDEF)
192 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::UNDEF, Op.getValueType()));
193 return false;
194 } else if (Depth == 6) { // Limit search depth.
195 return false;
196 }
197
198 uint64_t KnownZero2, KnownOne2, KnownZeroOut, KnownOneOut;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000199 switch (Op.getOpcode()) {
200 case ISD::Constant:
Nate Begeman368e18d2006-02-16 21:11:51 +0000201 // We know all of the bits for a constant!
202 KnownOne = cast<ConstantSDNode>(Op)->getValue() & DemandedMask;
203 KnownZero = ~KnownOne & DemandedMask;
Chris Lattnerec665152006-02-26 23:36:02 +0000204 return false; // Don't fall through, will infinitely loop.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000205 case ISD::AND:
Chris Lattner81cd3552006-02-27 00:36:27 +0000206 // If the RHS is a constant, check to see if the LHS would be zero without
207 // using the bits from the RHS. Below, we use knowledge about the RHS to
208 // simplify the LHS, here we're using information from the LHS to simplify
209 // the RHS.
210 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
211 uint64_t LHSZero, LHSOne;
212 ComputeMaskedBits(Op.getOperand(0), DemandedMask,
213 LHSZero, LHSOne, Depth+1);
214 // If the LHS already has zeros where RHSC does, this and is dead.
215 if ((LHSZero & DemandedMask) == (~RHSC->getValue() & DemandedMask))
216 return TLO.CombineTo(Op, Op.getOperand(0));
217 // If any of the set bits in the RHS are known zero on the LHS, shrink
218 // the constant.
219 if (TLO.ShrinkDemandedConstant(Op, ~LHSZero & DemandedMask))
220 return true;
221 }
222
Nate Begeman368e18d2006-02-16 21:11:51 +0000223 if (SimplifyDemandedBits(Op.getOperand(1), DemandedMask, KnownZero,
224 KnownOne, TLO, Depth+1))
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000225 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000226 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Nate Begeman368e18d2006-02-16 21:11:51 +0000227 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & ~KnownZero,
228 KnownZero2, KnownOne2, TLO, Depth+1))
229 return true;
230 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
231
232 // If all of the demanded bits are known one on one side, return the other.
233 // These bits cannot contribute to the result of the 'and'.
234 if ((DemandedMask & ~KnownZero2 & KnownOne)==(DemandedMask & ~KnownZero2))
235 return TLO.CombineTo(Op, Op.getOperand(0));
236 if ((DemandedMask & ~KnownZero & KnownOne2)==(DemandedMask & ~KnownZero))
237 return TLO.CombineTo(Op, Op.getOperand(1));
238 // If all of the demanded bits in the inputs are known zeros, return zero.
239 if ((DemandedMask & (KnownZero|KnownZero2)) == DemandedMask)
240 return TLO.CombineTo(Op, TLO.DAG.getConstant(0, Op.getValueType()));
241 // If the RHS is a constant, see if we can simplify it.
242 if (TLO.ShrinkDemandedConstant(Op, DemandedMask & ~KnownZero2))
243 return true;
Chris Lattner5f0c6582006-02-27 00:22:28 +0000244
Nate Begeman368e18d2006-02-16 21:11:51 +0000245 // Output known-1 bits are only known if set in both the LHS & RHS.
246 KnownOne &= KnownOne2;
247 // Output known-0 are known to be clear if zero in either the LHS | RHS.
248 KnownZero |= KnownZero2;
249 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000250 case ISD::OR:
Nate Begeman368e18d2006-02-16 21:11:51 +0000251 if (SimplifyDemandedBits(Op.getOperand(1), DemandedMask, KnownZero,
252 KnownOne, TLO, Depth+1))
253 return true;
254 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
255 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & ~KnownOne,
256 KnownZero2, KnownOne2, TLO, Depth+1))
257 return true;
258 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
259
260 // If all of the demanded bits are known zero on one side, return the other.
261 // These bits cannot contribute to the result of the 'or'.
Jeff Cohen5755b172006-02-17 02:12:18 +0000262 if ((DemandedMask & ~KnownOne2 & KnownZero) == (DemandedMask & ~KnownOne2))
Nate Begeman368e18d2006-02-16 21:11:51 +0000263 return TLO.CombineTo(Op, Op.getOperand(0));
Jeff Cohen5755b172006-02-17 02:12:18 +0000264 if ((DemandedMask & ~KnownOne & KnownZero2) == (DemandedMask & ~KnownOne))
Nate Begeman368e18d2006-02-16 21:11:51 +0000265 return TLO.CombineTo(Op, Op.getOperand(1));
266 // If all of the potentially set bits on one side are known to be set on
267 // the other side, just use the 'other' side.
268 if ((DemandedMask & (~KnownZero) & KnownOne2) ==
269 (DemandedMask & (~KnownZero)))
270 return TLO.CombineTo(Op, Op.getOperand(0));
271 if ((DemandedMask & (~KnownZero2) & KnownOne) ==
272 (DemandedMask & (~KnownZero2)))
273 return TLO.CombineTo(Op, Op.getOperand(1));
274 // If the RHS is a constant, see if we can simplify it.
275 if (TLO.ShrinkDemandedConstant(Op, DemandedMask))
276 return true;
277
278 // Output known-0 bits are only known if clear in both the LHS & RHS.
279 KnownZero &= KnownZero2;
280 // Output known-1 are known to be set if set in either the LHS | RHS.
281 KnownOne |= KnownOne2;
282 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000283 case ISD::XOR:
Nate Begeman368e18d2006-02-16 21:11:51 +0000284 if (SimplifyDemandedBits(Op.getOperand(1), DemandedMask, KnownZero,
285 KnownOne, TLO, Depth+1))
286 return true;
287 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
288 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask, KnownZero2,
289 KnownOne2, TLO, Depth+1))
290 return true;
291 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
292
293 // If all of the demanded bits are known zero on one side, return the other.
294 // These bits cannot contribute to the result of the 'xor'.
295 if ((DemandedMask & KnownZero) == DemandedMask)
296 return TLO.CombineTo(Op, Op.getOperand(0));
297 if ((DemandedMask & KnownZero2) == DemandedMask)
298 return TLO.CombineTo(Op, Op.getOperand(1));
299
300 // Output known-0 bits are known if clear or set in both the LHS & RHS.
301 KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
302 // Output known-1 are known to be set if set in only one of the LHS, RHS.
303 KnownOneOut = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
304
305 // If all of the unknown bits are known to be zero on one side or the other
306 // (but not both) turn this into an *inclusive* or.
307 // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
308 if (uint64_t UnknownBits = DemandedMask & ~(KnownZeroOut|KnownOneOut))
309 if ((UnknownBits & (KnownZero|KnownZero2)) == UnknownBits)
310 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::OR, Op.getValueType(),
311 Op.getOperand(0),
312 Op.getOperand(1)));
313 // If all of the demanded bits on one side are known, and all of the set
314 // bits on that side are also known to be set on the other side, turn this
315 // into an AND, as we know the bits will be cleared.
316 // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
317 if ((DemandedMask & (KnownZero|KnownOne)) == DemandedMask) { // all known
318 if ((KnownOne & KnownOne2) == KnownOne) {
319 MVT::ValueType VT = Op.getValueType();
320 SDOperand ANDC = TLO.DAG.getConstant(~KnownOne & DemandedMask, VT);
321 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::AND, VT, Op.getOperand(0),
322 ANDC));
323 }
324 }
325
326 // If the RHS is a constant, see if we can simplify it.
327 // FIXME: for XOR, we prefer to force bits to 1 if they will make a -1.
328 if (TLO.ShrinkDemandedConstant(Op, DemandedMask))
329 return true;
330
331 KnownZero = KnownZeroOut;
332 KnownOne = KnownOneOut;
333 break;
334 case ISD::SETCC:
335 // If we know the result of a setcc has the top bits zero, use this info.
336 if (getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult)
337 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
338 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000339 case ISD::SELECT:
Nate Begeman368e18d2006-02-16 21:11:51 +0000340 if (SimplifyDemandedBits(Op.getOperand(2), DemandedMask, KnownZero,
341 KnownOne, TLO, Depth+1))
342 return true;
343 if (SimplifyDemandedBits(Op.getOperand(1), DemandedMask, KnownZero2,
344 KnownOne2, TLO, Depth+1))
345 return true;
346 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
347 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
348
349 // If the operands are constants, see if we can simplify them.
350 if (TLO.ShrinkDemandedConstant(Op, DemandedMask))
351 return true;
352
353 // Only known if known in both the LHS and RHS.
354 KnownOne &= KnownOne2;
355 KnownZero &= KnownZero2;
356 break;
Chris Lattnerec665152006-02-26 23:36:02 +0000357 case ISD::SELECT_CC:
358 if (SimplifyDemandedBits(Op.getOperand(3), DemandedMask, KnownZero,
359 KnownOne, TLO, Depth+1))
360 return true;
361 if (SimplifyDemandedBits(Op.getOperand(2), DemandedMask, KnownZero2,
362 KnownOne2, TLO, Depth+1))
363 return true;
364 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
365 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
366
367 // If the operands are constants, see if we can simplify them.
368 if (TLO.ShrinkDemandedConstant(Op, DemandedMask))
369 return true;
370
371 // Only known if known in both the LHS and RHS.
372 KnownOne &= KnownOne2;
373 KnownZero &= KnownZero2;
374 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000375 case ISD::SHL:
Nate Begeman368e18d2006-02-16 21:11:51 +0000376 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
377 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask >> SA->getValue(),
378 KnownZero, KnownOne, TLO, Depth+1))
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000379 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000380 KnownZero <<= SA->getValue();
381 KnownOne <<= SA->getValue();
382 KnownZero |= (1ULL << SA->getValue())-1; // low bits known zero.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000383 }
384 break;
Nate Begeman368e18d2006-02-16 21:11:51 +0000385 case ISD::SRL:
386 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
387 MVT::ValueType VT = Op.getValueType();
388 unsigned ShAmt = SA->getValue();
389
390 // Compute the new bits that are at the top now.
391 uint64_t HighBits = (1ULL << ShAmt)-1;
392 HighBits <<= MVT::getSizeInBits(VT) - ShAmt;
393 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
394
395 if (SimplifyDemandedBits(Op.getOperand(0),
396 (DemandedMask << ShAmt) & TypeMask,
397 KnownZero, KnownOne, TLO, Depth+1))
398 return true;
399 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
400 KnownZero &= TypeMask;
401 KnownOne &= TypeMask;
402 KnownZero >>= ShAmt;
403 KnownOne >>= ShAmt;
404 KnownZero |= HighBits; // high bits known zero.
405 }
406 break;
407 case ISD::SRA:
408 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
409 MVT::ValueType VT = Op.getValueType();
410 unsigned ShAmt = SA->getValue();
411
412 // Compute the new bits that are at the top now.
413 uint64_t HighBits = (1ULL << ShAmt)-1;
414 HighBits <<= MVT::getSizeInBits(VT) - ShAmt;
415 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
416
417 if (SimplifyDemandedBits(Op.getOperand(0),
418 (DemandedMask << ShAmt) & TypeMask,
419 KnownZero, KnownOne, TLO, Depth+1))
420 return true;
421 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
422 KnownZero &= TypeMask;
423 KnownOne &= TypeMask;
424 KnownZero >>= SA->getValue();
425 KnownOne >>= SA->getValue();
426
427 // Handle the sign bits.
428 uint64_t SignBit = MVT::getIntVTSignBit(VT);
429 SignBit >>= SA->getValue(); // Adjust to where it is now in the mask.
430
431 // If the input sign bit is known to be zero, or if none of the top bits
432 // are demanded, turn this into an unsigned shift right.
433 if ((KnownZero & SignBit) || (HighBits & ~DemandedMask) == HighBits) {
434 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, VT, Op.getOperand(0),
435 Op.getOperand(1)));
436 } else if (KnownOne & SignBit) { // New bits are known one.
437 KnownOne |= HighBits;
438 }
439 }
440 break;
441 case ISD::SIGN_EXTEND_INREG: {
442 MVT::ValueType VT = Op.getValueType();
443 MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
444
Chris Lattnerec665152006-02-26 23:36:02 +0000445 // Sign extension. Compute the demanded bits in the result that are not
Nate Begeman368e18d2006-02-16 21:11:51 +0000446 // present in the input.
Chris Lattnerec665152006-02-26 23:36:02 +0000447 uint64_t NewBits = ~MVT::getIntVTBitMask(EVT) & DemandedMask;
Nate Begeman368e18d2006-02-16 21:11:51 +0000448
Chris Lattnerec665152006-02-26 23:36:02 +0000449 // If none of the extended bits are demanded, eliminate the sextinreg.
450 if (NewBits == 0)
451 return TLO.CombineTo(Op, Op.getOperand(0));
452
Nate Begeman368e18d2006-02-16 21:11:51 +0000453 uint64_t InSignBit = MVT::getIntVTSignBit(EVT);
454 int64_t InputDemandedBits = DemandedMask & MVT::getIntVTBitMask(EVT);
455
Chris Lattnerec665152006-02-26 23:36:02 +0000456 // Since the sign extended bits are demanded, we know that the sign
Nate Begeman368e18d2006-02-16 21:11:51 +0000457 // bit is demanded.
Chris Lattnerec665152006-02-26 23:36:02 +0000458 InputDemandedBits |= InSignBit;
Nate Begeman368e18d2006-02-16 21:11:51 +0000459
460 if (SimplifyDemandedBits(Op.getOperand(0), InputDemandedBits,
461 KnownZero, KnownOne, TLO, Depth+1))
462 return true;
463 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
464
465 // If the sign bit of the input is known set or clear, then we know the
466 // top bits of the result.
467
Chris Lattnerec665152006-02-26 23:36:02 +0000468 // If the input sign bit is known zero, convert this into a zero extension.
469 if (KnownZero & InSignBit)
470 return TLO.CombineTo(Op,
471 TLO.DAG.getZeroExtendInReg(Op.getOperand(0), EVT));
472
473 if (KnownOne & InSignBit) { // Input sign bit known set
Nate Begeman368e18d2006-02-16 21:11:51 +0000474 KnownOne |= NewBits;
475 KnownZero &= ~NewBits;
Chris Lattnerec665152006-02-26 23:36:02 +0000476 } else { // Input sign bit unknown
Nate Begeman368e18d2006-02-16 21:11:51 +0000477 KnownZero &= ~NewBits;
478 KnownOne &= ~NewBits;
479 }
480 break;
481 }
Chris Lattnerec665152006-02-26 23:36:02 +0000482 case ISD::CTTZ:
483 case ISD::CTLZ:
484 case ISD::CTPOP: {
485 MVT::ValueType VT = Op.getValueType();
486 unsigned LowBits = Log2_32(MVT::getSizeInBits(VT))+1;
487 KnownZero = ~((1ULL << LowBits)-1) & MVT::getIntVTBitMask(VT);
488 KnownOne = 0;
489 break;
490 }
491 case ISD::ZEXTLOAD: {
492 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(3))->getVT();
493 KnownZero |= ~MVT::getIntVTBitMask(VT) & DemandedMask;
494 break;
495 }
496 case ISD::ZERO_EXTEND: {
497 uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType());
498
499 // If none of the top bits are demanded, convert this into an any_extend.
500 uint64_t NewBits = (~InMask) & DemandedMask;
501 if (NewBits == 0)
502 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ANY_EXTEND,
503 Op.getValueType(),
504 Op.getOperand(0)));
505
506 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & InMask,
507 KnownZero, KnownOne, TLO, Depth+1))
508 return true;
509 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
510 KnownZero |= NewBits;
511 break;
512 }
513 case ISD::SIGN_EXTEND: {
514 MVT::ValueType InVT = Op.getOperand(0).getValueType();
515 uint64_t InMask = MVT::getIntVTBitMask(InVT);
516 uint64_t InSignBit = MVT::getIntVTSignBit(InVT);
517 uint64_t NewBits = (~InMask) & DemandedMask;
518
519 // If none of the top bits are demanded, convert this into an any_extend.
520 if (NewBits == 0)
521 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ANY_EXTEND,Op.getValueType(),
522 Op.getOperand(0)));
523
524 // Since some of the sign extended bits are demanded, we know that the sign
525 // bit is demanded.
526 uint64_t InDemandedBits = DemandedMask & InMask;
527 InDemandedBits |= InSignBit;
528
529 if (SimplifyDemandedBits(Op.getOperand(0), InDemandedBits, KnownZero,
530 KnownOne, TLO, Depth+1))
531 return true;
532
533 // If the sign bit is known zero, convert this to a zero extend.
534 if (KnownZero & InSignBit)
535 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ZERO_EXTEND,
536 Op.getValueType(),
537 Op.getOperand(0)));
538
539 // If the sign bit is known one, the top bits match.
540 if (KnownOne & InSignBit) {
541 KnownOne |= NewBits;
542 KnownZero &= ~NewBits;
543 } else { // Otherwise, top bits aren't known.
544 KnownOne &= ~NewBits;
545 KnownZero &= ~NewBits;
546 }
547 break;
548 }
549 case ISD::ANY_EXTEND: {
550 uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType());
551 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & InMask,
552 KnownZero, KnownOne, TLO, Depth+1))
553 return true;
554 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
555 break;
556 }
557 case ISD::AssertZext: {
558 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
559 uint64_t InMask = MVT::getIntVTBitMask(VT);
560 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & InMask,
561 KnownZero, KnownOne, TLO, Depth+1))
562 return true;
563 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
564 KnownZero |= ~InMask & DemandedMask;
565 break;
566 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000567 case ISD::ADD:
568 if (ConstantSDNode *AA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
569 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask, KnownZero,
570 KnownOne, TLO, Depth+1))
571 return true;
572 // Compute the KnownOne/KnownZero masks for the constant, so we can set
573 // KnownZero appropriately if we're adding a constant that has all low
574 // bits cleared.
575 ComputeMaskedBits(Op.getOperand(1),
576 MVT::getIntVTBitMask(Op.getValueType()),
577 KnownZero2, KnownOne2, Depth+1);
578
579 uint64_t KnownZeroOut = std::min(CountTrailingZeros_64(~KnownZero),
580 CountTrailingZeros_64(~KnownZero2));
581 KnownZero = (1ULL << KnownZeroOut) - 1;
582 KnownOne = 0;
583 }
584 break;
Chris Lattnera6bc5a42006-02-27 01:00:42 +0000585 case ISD::SUB:
586 // Just use ComputeMaskedBits to compute output bits, there are no
587 // simplifications that can be done here, and sub always demands all input
588 // bits.
589 ComputeMaskedBits(Op, DemandedMask, KnownZero, KnownOne, Depth);
590 break;
Nate Begeman368e18d2006-02-16 21:11:51 +0000591 }
Chris Lattnerec665152006-02-26 23:36:02 +0000592
593 // If we know the value of all of the demanded bits, return this as a
594 // constant.
595 if ((DemandedMask & (KnownZero|KnownOne)) == DemandedMask)
596 return TLO.CombineTo(Op, TLO.DAG.getConstant(KnownOne, Op.getValueType()));
597
Nate Begeman368e18d2006-02-16 21:11:51 +0000598 return false;
599}
600
601/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
602/// this predicate to simplify operations downstream. Mask is known to be zero
603/// for bits that V cannot have.
604bool TargetLowering::MaskedValueIsZero(SDOperand Op, uint64_t Mask,
605 unsigned Depth) const {
606 uint64_t KnownZero, KnownOne;
607 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
608 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
609 return (KnownZero & Mask) == Mask;
610}
611
612/// ComputeMaskedBits - Determine which of the bits specified in Mask are
613/// known to be either zero or one and return them in the KnownZero/KnownOne
614/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
615/// processing.
616void TargetLowering::ComputeMaskedBits(SDOperand Op, uint64_t Mask,
617 uint64_t &KnownZero, uint64_t &KnownOne,
618 unsigned Depth) const {
619 KnownZero = KnownOne = 0; // Don't know anything.
620 if (Depth == 6 || Mask == 0)
621 return; // Limit search depth.
622
623 uint64_t KnownZero2, KnownOne2;
624
625 switch (Op.getOpcode()) {
626 case ISD::Constant:
627 // We know all of the bits for a constant!
628 KnownOne = cast<ConstantSDNode>(Op)->getValue() & Mask;
629 KnownZero = ~KnownOne & Mask;
630 return;
631 case ISD::AND:
632 // If either the LHS or the RHS are Zero, the result is zero.
633 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
634 Mask &= ~KnownZero;
635 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
636 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
637 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
638
639 // Output known-1 bits are only known if set in both the LHS & RHS.
640 KnownOne &= KnownOne2;
641 // Output known-0 are known to be clear if zero in either the LHS | RHS.
642 KnownZero |= KnownZero2;
643 return;
644 case ISD::OR:
645 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
646 Mask &= ~KnownOne;
647 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
648 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
649 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
650
651 // Output known-0 bits are only known if clear in both the LHS & RHS.
652 KnownZero &= KnownZero2;
653 // Output known-1 are known to be set if set in either the LHS | RHS.
654 KnownOne |= KnownOne2;
655 return;
656 case ISD::XOR: {
657 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
658 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
659 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
660 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
661
662 // Output known-0 bits are known if clear or set in both the LHS & RHS.
663 uint64_t KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
664 // Output known-1 are known to be set if set in only one of the LHS, RHS.
665 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
666 KnownZero = KnownZeroOut;
667 return;
668 }
669 case ISD::SELECT:
670 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
671 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
672 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
673 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
674
675 // Only known if known in both the LHS and RHS.
676 KnownOne &= KnownOne2;
677 KnownZero &= KnownZero2;
678 return;
679 case ISD::SELECT_CC:
680 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
681 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
682 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
683 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
684
685 // Only known if known in both the LHS and RHS.
686 KnownOne &= KnownOne2;
687 KnownZero &= KnownZero2;
688 return;
689 case ISD::SETCC:
690 // If we know the result of a setcc has the top bits zero, use this info.
691 if (getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult)
692 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
693 return;
694 case ISD::SHL:
695 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
696 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
697 Mask >>= SA->getValue();
698 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
699 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
700 KnownZero <<= SA->getValue();
701 KnownOne <<= SA->getValue();
702 KnownZero |= (1ULL << SA->getValue())-1; // low bits known zero.
703 }
Nate Begeman003a2722006-02-18 02:43:25 +0000704 return;
Nate Begeman368e18d2006-02-16 21:11:51 +0000705 case ISD::SRL:
706 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
707 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
708 uint64_t HighBits = (1ULL << SA->getValue())-1;
709 HighBits <<= MVT::getSizeInBits(Op.getValueType())-SA->getValue();
710 Mask <<= SA->getValue();
711 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Nate Begeman003a2722006-02-18 02:43:25 +0000712 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Nate Begeman368e18d2006-02-16 21:11:51 +0000713 KnownZero >>= SA->getValue();
714 KnownOne >>= SA->getValue();
715 KnownZero |= HighBits; // high bits known zero.
716 }
Nate Begeman003a2722006-02-18 02:43:25 +0000717 return;
Nate Begeman368e18d2006-02-16 21:11:51 +0000718 case ISD::SRA:
719 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
720 uint64_t HighBits = (1ULL << SA->getValue())-1;
721 HighBits <<= MVT::getSizeInBits(Op.getValueType())-SA->getValue();
722 Mask <<= SA->getValue();
723 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
724 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
725 KnownZero >>= SA->getValue();
726 KnownOne >>= SA->getValue();
727
728 // Handle the sign bits.
729 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(Op.getValueType())-1);
730 SignBit >>= SA->getValue(); // Adjust to where it is now in the mask.
731
732 if (KnownZero & SignBit) { // New bits are known zero.
733 KnownZero |= HighBits;
734 } else if (KnownOne & SignBit) { // New bits are known one.
735 KnownOne |= HighBits;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000736 }
737 }
Nate Begeman003a2722006-02-18 02:43:25 +0000738 return;
Chris Lattnerec665152006-02-26 23:36:02 +0000739 case ISD::SIGN_EXTEND_INREG: {
740 MVT::ValueType VT = Op.getValueType();
741 MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
742
743 // Sign extension. Compute the demanded bits in the result that are not
744 // present in the input.
745 uint64_t NewBits = ~MVT::getIntVTBitMask(EVT) & Mask;
746
747 uint64_t InSignBit = MVT::getIntVTSignBit(EVT);
748 int64_t InputDemandedBits = Mask & MVT::getIntVTBitMask(EVT);
749
750 // If the sign extended bits are demanded, we know that the sign
751 // bit is demanded.
752 if (NewBits)
753 InputDemandedBits |= InSignBit;
754
755 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
756 KnownZero, KnownOne, Depth+1);
757 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
758
759 // If the sign bit of the input is known set or clear, then we know the
760 // top bits of the result.
761 if (KnownZero & InSignBit) { // Input sign bit known clear
762 KnownZero |= NewBits;
763 KnownOne &= ~NewBits;
764 } else if (KnownOne & InSignBit) { // Input sign bit known set
765 KnownOne |= NewBits;
766 KnownZero &= ~NewBits;
767 } else { // Input sign bit unknown
768 KnownZero &= ~NewBits;
769 KnownOne &= ~NewBits;
770 }
771 return;
772 }
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000773 case ISD::CTTZ:
774 case ISD::CTLZ:
Nate Begeman368e18d2006-02-16 21:11:51 +0000775 case ISD::CTPOP: {
776 MVT::ValueType VT = Op.getValueType();
777 unsigned LowBits = Log2_32(MVT::getSizeInBits(VT))+1;
778 KnownZero = ~((1ULL << LowBits)-1) & MVT::getIntVTBitMask(VT);
779 KnownOne = 0;
780 return;
781 }
782 case ISD::ZEXTLOAD: {
Chris Lattnerec665152006-02-26 23:36:02 +0000783 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(3))->getVT();
784 KnownZero |= ~MVT::getIntVTBitMask(VT) & Mask;
Nate Begeman368e18d2006-02-16 21:11:51 +0000785 return;
786 }
787 case ISD::ZERO_EXTEND: {
Chris Lattnerec665152006-02-26 23:36:02 +0000788 uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType());
789 uint64_t NewBits = (~InMask) & Mask;
790 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
791 KnownOne, Depth+1);
792 KnownZero |= NewBits & Mask;
793 KnownOne &= ~NewBits;
794 return;
795 }
796 case ISD::SIGN_EXTEND: {
797 MVT::ValueType InVT = Op.getOperand(0).getValueType();
798 unsigned InBits = MVT::getSizeInBits(InVT);
799 uint64_t InMask = MVT::getIntVTBitMask(InVT);
800 uint64_t InSignBit = 1ULL << (InBits-1);
801 uint64_t NewBits = (~InMask) & Mask;
802 uint64_t InDemandedBits = Mask & InMask;
803
804 // If any of the sign extended bits are demanded, we know that the sign
805 // bit is demanded.
806 if (NewBits & Mask)
807 InDemandedBits |= InSignBit;
808
809 ComputeMaskedBits(Op.getOperand(0), InDemandedBits, KnownZero,
810 KnownOne, Depth+1);
811 // If the sign bit is known zero or one, the top bits match.
812 if (KnownZero & InSignBit) {
813 KnownZero |= NewBits;
814 KnownOne &= ~NewBits;
815 } else if (KnownOne & InSignBit) {
816 KnownOne |= NewBits;
817 KnownZero &= ~NewBits;
818 } else { // Otherwise, top bits aren't known.
819 KnownOne &= ~NewBits;
820 KnownZero &= ~NewBits;
821 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000822 return;
823 }
824 case ISD::ANY_EXTEND: {
Chris Lattnerec665152006-02-26 23:36:02 +0000825 MVT::ValueType VT = Op.getOperand(0).getValueType();
826 ComputeMaskedBits(Op.getOperand(0), Mask & MVT::getIntVTBitMask(VT),
827 KnownZero, KnownOne, Depth+1);
Nate Begeman368e18d2006-02-16 21:11:51 +0000828 return;
829 }
830 case ISD::AssertZext: {
Chris Lattnerec665152006-02-26 23:36:02 +0000831 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
832 uint64_t InMask = MVT::getIntVTBitMask(VT);
833 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
834 KnownOne, Depth+1);
835 KnownZero |= (~InMask) & Mask;
Nate Begeman368e18d2006-02-16 21:11:51 +0000836 return;
837 }
838 case ISD::ADD: {
839 // If either the LHS or the RHS are Zero, the result is zero.
840 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
841 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
842 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
843 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
844
845 // Output known-0 bits are known if clear or set in both the low clear bits
846 // common to both LHS & RHS;
847 uint64_t KnownZeroOut = std::min(CountTrailingZeros_64(~KnownZero),
848 CountTrailingZeros_64(~KnownZero2));
849
850 KnownZero = (1ULL << KnownZeroOut) - 1;
851 KnownOne = 0;
852 return;
853 }
Chris Lattnera6bc5a42006-02-27 01:00:42 +0000854 case ISD::SUB: {
855 ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0));
856 if (!CLHS) return;
857
Nate Begeman368e18d2006-02-16 21:11:51 +0000858 // We know that the top bits of C-X are clear if X contains less bits
859 // than C (i.e. no wrap-around can happen). For example, 20-X is
Chris Lattnera6bc5a42006-02-27 01:00:42 +0000860 // positive if we can prove that X is >= 0 and < 16.
861 MVT::ValueType VT = CLHS->getValueType(0);
862 if ((CLHS->getValue() & MVT::getIntVTSignBit(VT)) == 0) { // sign bit clear
863 unsigned NLZ = CountLeadingZeros_64(CLHS->getValue()+1);
864 uint64_t MaskV = (1ULL << (63-NLZ))-1; // NLZ can't be 64 with no sign bit
865 MaskV = ~MaskV & MVT::getIntVTBitMask(VT);
866 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero, KnownOne, Depth+1);
867
868 // If all of the MaskV bits are known to be zero, then we know the output
869 // top bits are zero, because we now know that the output is from [0-C].
870 if ((KnownZero & MaskV) == MaskV) {
871 unsigned NLZ2 = CountLeadingZeros_64(CLHS->getValue());
872 KnownZero = ~((1ULL << (64-NLZ2))-1) & Mask; // Top bits known zero.
873 KnownOne = 0; // No one bits known.
874 } else {
875 KnownOne = KnownOne = 0; // Otherwise, nothing known.
876 }
877 }
Nate Begeman003a2722006-02-18 02:43:25 +0000878 return;
Chris Lattnera6bc5a42006-02-27 01:00:42 +0000879 }
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000880 default:
881 // Allow the target to implement this method for its nodes.
882 if (Op.getOpcode() >= ISD::BUILTIN_OP_END)
Nate Begeman368e18d2006-02-16 21:11:51 +0000883 computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne);
Nate Begeman003a2722006-02-18 02:43:25 +0000884 return;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000885 }
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000886}
887
Nate Begeman368e18d2006-02-16 21:11:51 +0000888/// computeMaskedBitsForTargetNode - Determine which of the bits specified
889/// in Mask are known to be either zero or one and return them in the
890/// KnownZero/KnownOne bitsets.
891void TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
892 uint64_t Mask,
893 uint64_t &KnownZero,
894 uint64_t &KnownOne,
895 unsigned Depth) const {
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000896 assert(Op.getOpcode() >= ISD::BUILTIN_OP_END &&
897 "Should use MaskedValueIsZero if you don't know whether Op"
898 " is a target node!");
Nate Begeman368e18d2006-02-16 21:11:51 +0000899 KnownZero = 0;
900 KnownOne = 0;
Evan Cheng3a03ebb2005-12-21 23:05:39 +0000901}
Chris Lattner4ccb0702006-01-26 20:37:03 +0000902
Chris Lattner00ffed02006-03-01 04:52:55 +0000903SDOperand TargetLowering::
904PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
905 // Default implementation: no optimization.
906 return SDOperand();
907}
908
Chris Lattnereb8146b2006-02-04 02:13:02 +0000909//===----------------------------------------------------------------------===//
910// Inline Assembler Implementation Methods
911//===----------------------------------------------------------------------===//
912
913TargetLowering::ConstraintType
914TargetLowering::getConstraintType(char ConstraintLetter) const {
915 // FIXME: lots more standard ones to handle.
916 switch (ConstraintLetter) {
917 default: return C_Unknown;
918 case 'r': return C_RegisterClass;
Chris Lattner2b7401e2006-02-24 01:10:46 +0000919 case 'm': // memory
920 case 'o': // offsetable
921 case 'V': // not offsetable
922 return C_Memory;
Chris Lattnereb8146b2006-02-04 02:13:02 +0000923 case 'i': // Simple Integer or Relocatable Constant
924 case 'n': // Simple Integer
925 case 's': // Relocatable Constant
926 case 'I': // Target registers.
927 case 'J':
928 case 'K':
929 case 'L':
930 case 'M':
931 case 'N':
932 case 'O':
Chris Lattner2b7401e2006-02-24 01:10:46 +0000933 case 'P':
934 return C_Other;
Chris Lattnereb8146b2006-02-04 02:13:02 +0000935 }
936}
937
938bool TargetLowering::isOperandValidForConstraint(SDOperand Op,
939 char ConstraintLetter) {
940 switch (ConstraintLetter) {
941 default: return false;
942 case 'i': // Simple Integer or Relocatable Constant
943 case 'n': // Simple Integer
944 case 's': // Relocatable Constant
945 return true; // FIXME: not right.
946 }
947}
948
949
Chris Lattner4ccb0702006-01-26 20:37:03 +0000950std::vector<unsigned> TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +0000951getRegClassForInlineAsmConstraint(const std::string &Constraint,
952 MVT::ValueType VT) const {
953 return std::vector<unsigned>();
954}
955
956
957std::pair<unsigned, const TargetRegisterClass*> TargetLowering::
Chris Lattner4217ca8dc2006-02-21 23:11:00 +0000958getRegForInlineAsmConstraint(const std::string &Constraint,
959 MVT::ValueType VT) const {
Chris Lattner1efa40f2006-02-22 00:56:39 +0000960 if (Constraint[0] != '{')
961 return std::pair<unsigned, const TargetRegisterClass*>(0, 0);
Chris Lattnera55079a2006-02-01 01:29:47 +0000962 assert(*(Constraint.end()-1) == '}' && "Not a brace enclosed constraint?");
963
964 // Remove the braces from around the name.
965 std::string RegName(Constraint.begin()+1, Constraint.end()-1);
Chris Lattner1efa40f2006-02-22 00:56:39 +0000966
967 // Figure out which register class contains this reg.
Chris Lattner4ccb0702006-01-26 20:37:03 +0000968 const MRegisterInfo *RI = TM.getRegisterInfo();
Chris Lattner1efa40f2006-02-22 00:56:39 +0000969 for (MRegisterInfo::regclass_iterator RCI = RI->regclass_begin(),
970 E = RI->regclass_end(); RCI != E; ++RCI) {
971 const TargetRegisterClass *RC = *RCI;
Chris Lattnerb3befd42006-02-22 23:00:51 +0000972
973 // If none of the the value types for this register class are valid, we
974 // can't use it. For example, 64-bit reg classes on 32-bit targets.
975 bool isLegal = false;
976 for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
977 I != E; ++I) {
978 if (isTypeLegal(*I)) {
979 isLegal = true;
980 break;
981 }
982 }
983
984 if (!isLegal) continue;
985
Chris Lattner1efa40f2006-02-22 00:56:39 +0000986 for (TargetRegisterClass::iterator I = RC->begin(), E = RC->end();
987 I != E; ++I) {
Chris Lattnerb3befd42006-02-22 23:00:51 +0000988 if (StringsEqualNoCase(RegName, RI->get(*I).Name))
Chris Lattner1efa40f2006-02-22 00:56:39 +0000989 return std::make_pair(*I, RC);
Chris Lattner1efa40f2006-02-22 00:56:39 +0000990 }
Chris Lattner4ccb0702006-01-26 20:37:03 +0000991 }
Chris Lattnera55079a2006-02-01 01:29:47 +0000992
Chris Lattner1efa40f2006-02-22 00:56:39 +0000993 return std::pair<unsigned, const TargetRegisterClass*>(0, 0);
Chris Lattner4ccb0702006-01-26 20:37:03 +0000994}