blob: b9c10ce103ef979c733c7488be781b34dfba5e2f [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 Lattnerdc879292006-03-31 00:28:56 +000017#include "llvm/DerivedTypes.h"
Chris Lattner310968c2005-01-07 07:44:53 +000018#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner4ccb0702006-01-26 20:37:03 +000019#include "llvm/ADT/StringExtras.h"
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +000020#include "llvm/Support/MathExtras.h"
Chris Lattner310968c2005-01-07 07:44:53 +000021using namespace llvm;
22
23TargetLowering::TargetLowering(TargetMachine &tm)
Chris Lattner3e6e8cc2006-01-29 08:41:12 +000024 : TM(tm), TD(TM.getTargetData()) {
Evan Cheng33143dc2006-03-03 06:58:59 +000025 assert(ISD::BUILTIN_OP_END <= 156 &&
Chris Lattner310968c2005-01-07 07:44:53 +000026 "Fixed size array in TargetLowering is not large enough!");
Chris Lattnercba82f92005-01-16 07:28:11 +000027 // All operations default to being supported.
28 memset(OpActions, 0, sizeof(OpActions));
Chris Lattner310968c2005-01-07 07:44:53 +000029
30 IsLittleEndian = TD.isLittleEndian();
Chris Lattner714b69d2005-01-16 23:59:48 +000031 ShiftAmountTy = SetCCResultTy = PointerTy = getValueType(TD.getIntPtrType());
Chris Lattnerd6e49672005-01-19 03:36:14 +000032 ShiftAmtHandling = Undefined;
Chris Lattner310968c2005-01-07 07:44:53 +000033 memset(RegClassForVT, 0,MVT::LAST_VALUETYPE*sizeof(TargetRegisterClass*));
Chris Lattner00ffed02006-03-01 04:52:55 +000034 memset(TargetDAGCombineArray, 0,
35 sizeof(TargetDAGCombineArray)/sizeof(TargetDAGCombineArray[0]));
Evan Chenga03a5dc2006-02-14 08:38:30 +000036 maxStoresPerMemset = maxStoresPerMemcpy = maxStoresPerMemmove = 8;
Reid Spencer0f9beca2005-08-27 19:09:02 +000037 allowUnalignedMemoryAccesses = false;
Chris Lattner8e6be8b2005-09-27 22:13:56 +000038 UseUnderscoreSetJmpLongJmp = false;
Nate Begeman405e3ec2005-10-21 00:02:42 +000039 IntDivIsCheap = false;
40 Pow2DivIsCheap = false;
Chris Lattneree4a7652006-01-25 18:57:15 +000041 StackPointerRegisterToSaveRestore = 0;
Evan Cheng0577a222006-01-25 18:52:42 +000042 SchedPreferenceInfo = SchedulingForLatency;
Chris Lattner310968c2005-01-07 07:44:53 +000043}
44
Chris Lattnercba82f92005-01-16 07:28:11 +000045TargetLowering::~TargetLowering() {}
46
Chris Lattnerbb97d812005-01-16 01:10:58 +000047/// setValueTypeAction - Set the action for a particular value type. This
48/// assumes an action has not already been set for this value type.
Chris Lattnercba82f92005-01-16 07:28:11 +000049static void SetValueTypeAction(MVT::ValueType VT,
50 TargetLowering::LegalizeAction Action,
Chris Lattnerbb97d812005-01-16 01:10:58 +000051 TargetLowering &TLI,
52 MVT::ValueType *TransformToType,
Chris Lattner3e6e8cc2006-01-29 08:41:12 +000053 TargetLowering::ValueTypeActionImpl &ValueTypeActions) {
54 ValueTypeActions.setTypeAction(VT, Action);
Chris Lattnercba82f92005-01-16 07:28:11 +000055 if (Action == TargetLowering::Promote) {
Chris Lattnerbb97d812005-01-16 01:10:58 +000056 MVT::ValueType PromoteTo;
57 if (VT == MVT::f32)
58 PromoteTo = MVT::f64;
59 else {
60 unsigned LargerReg = VT+1;
Chris Lattner9ed62c12005-08-24 16:34:12 +000061 while (!TLI.isTypeLegal((MVT::ValueType)LargerReg)) {
Chris Lattnerbb97d812005-01-16 01:10:58 +000062 ++LargerReg;
63 assert(MVT::isInteger((MVT::ValueType)LargerReg) &&
64 "Nothing to promote to??");
65 }
66 PromoteTo = (MVT::ValueType)LargerReg;
67 }
68
69 assert(MVT::isInteger(VT) == MVT::isInteger(PromoteTo) &&
70 MVT::isFloatingPoint(VT) == MVT::isFloatingPoint(PromoteTo) &&
71 "Can only promote from int->int or fp->fp!");
72 assert(VT < PromoteTo && "Must promote to a larger type!");
73 TransformToType[VT] = PromoteTo;
Chris Lattnercba82f92005-01-16 07:28:11 +000074 } else if (Action == TargetLowering::Expand) {
Nate Begeman4ef3b812005-11-22 01:29:36 +000075 assert((VT == MVT::Vector || MVT::isInteger(VT)) && VT > MVT::i8 &&
Chris Lattnerbb97d812005-01-16 01:10:58 +000076 "Cannot expand this type: target must support SOME integer reg!");
77 // Expand to the next smaller integer type!
78 TransformToType[VT] = (MVT::ValueType)(VT-1);
79 }
80}
81
82
Chris Lattner310968c2005-01-07 07:44:53 +000083/// computeRegisterProperties - Once all of the register classes are added,
84/// this allows us to compute derived properties we expose.
85void TargetLowering::computeRegisterProperties() {
Nate Begeman6a648612005-11-29 05:45:29 +000086 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattnerbb97d812005-01-16 01:10:58 +000087 "Too many value types for ValueTypeActions to hold!");
88
Chris Lattner310968c2005-01-07 07:44:53 +000089 // Everything defaults to one.
90 for (unsigned i = 0; i != MVT::LAST_VALUETYPE; ++i)
91 NumElementsForVT[i] = 1;
Misha Brukmanf976c852005-04-21 22:55:34 +000092
Chris Lattner310968c2005-01-07 07:44:53 +000093 // Find the largest integer register class.
94 unsigned LargestIntReg = MVT::i128;
95 for (; RegClassForVT[LargestIntReg] == 0; --LargestIntReg)
96 assert(LargestIntReg != MVT::i1 && "No integer registers defined!");
97
98 // Every integer value type larger than this largest register takes twice as
99 // many registers to represent as the previous ValueType.
100 unsigned ExpandedReg = LargestIntReg; ++LargestIntReg;
101 for (++ExpandedReg; MVT::isInteger((MVT::ValueType)ExpandedReg);++ExpandedReg)
102 NumElementsForVT[ExpandedReg] = 2*NumElementsForVT[ExpandedReg-1];
Chris Lattner310968c2005-01-07 07:44:53 +0000103
Chris Lattnerbb97d812005-01-16 01:10:58 +0000104 // Inspect all of the ValueType's possible, deciding how to process them.
105 for (unsigned IntReg = MVT::i1; IntReg <= MVT::i128; ++IntReg)
106 // If we are expanding this type, expand it!
107 if (getNumElements((MVT::ValueType)IntReg) != 1)
Chris Lattnercba82f92005-01-16 07:28:11 +0000108 SetValueTypeAction((MVT::ValueType)IntReg, Expand, *this, TransformToType,
Chris Lattnerbb97d812005-01-16 01:10:58 +0000109 ValueTypeActions);
Chris Lattner9ed62c12005-08-24 16:34:12 +0000110 else if (!isTypeLegal((MVT::ValueType)IntReg))
Chris Lattnerbb97d812005-01-16 01:10:58 +0000111 // Otherwise, if we don't have native support, we must promote to a
112 // larger type.
Chris Lattnercba82f92005-01-16 07:28:11 +0000113 SetValueTypeAction((MVT::ValueType)IntReg, Promote, *this,
114 TransformToType, ValueTypeActions);
Chris Lattnercfdfe4c2005-01-16 01:20:18 +0000115 else
116 TransformToType[(MVT::ValueType)IntReg] = (MVT::ValueType)IntReg;
Misha Brukmanf976c852005-04-21 22:55:34 +0000117
Chris Lattnerbb97d812005-01-16 01:10:58 +0000118 // If the target does not have native support for F32, promote it to F64.
Chris Lattner9ed62c12005-08-24 16:34:12 +0000119 if (!isTypeLegal(MVT::f32))
Chris Lattnercba82f92005-01-16 07:28:11 +0000120 SetValueTypeAction(MVT::f32, Promote, *this,
121 TransformToType, ValueTypeActions);
Chris Lattnercfdfe4c2005-01-16 01:20:18 +0000122 else
123 TransformToType[MVT::f32] = MVT::f32;
Nate Begeman4ef3b812005-11-22 01:29:36 +0000124
125 // Set MVT::Vector to always be Expanded
126 SetValueTypeAction(MVT::Vector, Expand, *this, TransformToType,
127 ValueTypeActions);
Chris Lattner3a5935842006-03-16 19:50:01 +0000128
129 // Loop over all of the legal vector value types, specifying an identity type
130 // transformation.
131 for (unsigned i = MVT::FIRST_VECTOR_VALUETYPE;
Evan Cheng677274b2006-03-23 23:24:51 +0000132 i <= MVT::LAST_VECTOR_VALUETYPE; ++i) {
Chris Lattner3a5935842006-03-16 19:50:01 +0000133 if (isTypeLegal((MVT::ValueType)i))
134 TransformToType[i] = (MVT::ValueType)i;
135 }
Chris Lattnercfdfe4c2005-01-16 01:20:18 +0000136
Chris Lattner9ed62c12005-08-24 16:34:12 +0000137 assert(isTypeLegal(MVT::f64) && "Target does not support FP?");
Chris Lattnercfdfe4c2005-01-16 01:20:18 +0000138 TransformToType[MVT::f64] = MVT::f64;
Chris Lattnerbb97d812005-01-16 01:10:58 +0000139}
Chris Lattnercba82f92005-01-16 07:28:11 +0000140
Evan Cheng72261582005-12-20 06:22:03 +0000141const char *TargetLowering::getTargetNodeName(unsigned Opcode) const {
142 return NULL;
143}
Evan Cheng3a03ebb2005-12-21 23:05:39 +0000144
Chris Lattnerdc879292006-03-31 00:28:56 +0000145/// getPackedTypeBreakdown - Packed types are broken down into some number of
146/// legal scalar types. For example, <8 x float> maps to 2 MVT::v2f32 values
147/// with Altivec or SSE1, or 8 promoted MVT::f64 values with the X86 FP stack.
148///
149/// This method returns the number and type of the resultant breakdown.
150///
151MVT::ValueType TargetLowering::getPackedTypeBreakdown(const PackedType *PTy,
152 unsigned &NumVals) const {
153 // Figure out the right, legal destination reg to copy into.
154 unsigned NumElts = PTy->getNumElements();
155 MVT::ValueType EltTy = getValueType(PTy->getElementType());
156
157 unsigned NumVectorRegs = 1;
158
159 // Divide the input until we get to a supported size. This will always
160 // end with a scalar if the target doesn't support vectors.
161 while (NumElts > 1 && !isTypeLegal(getVectorType(EltTy, NumElts))) {
162 NumElts >>= 1;
163 NumVectorRegs <<= 1;
164 }
165
166 MVT::ValueType VT;
167 if (NumElts == 1)
168 VT = EltTy;
169 else
170 VT = getVectorType(EltTy, NumElts);
171
172 MVT::ValueType DestVT = getTypeToTransformTo(VT);
173 if (DestVT < VT) {
174 // Value is expanded, e.g. i64 -> i16.
175 NumVals = NumVectorRegs*(MVT::getSizeInBits(VT)/MVT::getSizeInBits(DestVT));
176 } else {
177 // Otherwise, promotion or legal types use the same number of registers as
178 // the vector decimated to the appropriate level.
179 NumVals = NumVectorRegs;
180 }
181
182 return DestVT;
183}
184
Chris Lattnereb8146b2006-02-04 02:13:02 +0000185//===----------------------------------------------------------------------===//
186// Optimization Methods
187//===----------------------------------------------------------------------===//
188
Nate Begeman368e18d2006-02-16 21:11:51 +0000189/// ShrinkDemandedConstant - Check to see if the specified operand of the
190/// specified instruction is a constant integer. If so, check to see if there
191/// are any bits set in the constant that are not demanded. If so, shrink the
192/// constant and return true.
193bool TargetLowering::TargetLoweringOpt::ShrinkDemandedConstant(SDOperand Op,
194 uint64_t Demanded) {
Chris Lattnerec665152006-02-26 23:36:02 +0000195 // FIXME: ISD::SELECT, ISD::SELECT_CC
Nate Begeman368e18d2006-02-16 21:11:51 +0000196 switch(Op.getOpcode()) {
197 default: break;
Nate Begemande996292006-02-03 22:24:05 +0000198 case ISD::AND:
Nate Begeman368e18d2006-02-16 21:11:51 +0000199 case ISD::OR:
200 case ISD::XOR:
201 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
202 if ((~Demanded & C->getValue()) != 0) {
203 MVT::ValueType VT = Op.getValueType();
204 SDOperand New = DAG.getNode(Op.getOpcode(), VT, Op.getOperand(0),
205 DAG.getConstant(Demanded & C->getValue(),
206 VT));
207 return CombineTo(Op, New);
Nate Begemande996292006-02-03 22:24:05 +0000208 }
Nate Begemande996292006-02-03 22:24:05 +0000209 break;
210 }
211 return false;
212}
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000213
Nate Begeman368e18d2006-02-16 21:11:51 +0000214/// SimplifyDemandedBits - Look at Op. At this point, we know that only the
215/// DemandedMask bits of the result of Op are ever used downstream. If we can
216/// use this information to simplify Op, create a new simplified DAG node and
217/// return true, returning the original and new nodes in Old and New. Otherwise,
218/// analyze the expression and return a mask of KnownOne and KnownZero bits for
219/// the expression (used to simplify the caller). The KnownZero/One bits may
220/// only be accurate for those bits in the DemandedMask.
221bool TargetLowering::SimplifyDemandedBits(SDOperand Op, uint64_t DemandedMask,
222 uint64_t &KnownZero,
223 uint64_t &KnownOne,
224 TargetLoweringOpt &TLO,
225 unsigned Depth) const {
226 KnownZero = KnownOne = 0; // Don't know anything.
227 // Other users may use these bits.
228 if (!Op.Val->hasOneUse()) {
229 if (Depth != 0) {
230 // If not at the root, Just compute the KnownZero/KnownOne bits to
231 // simplify things downstream.
232 ComputeMaskedBits(Op, DemandedMask, KnownZero, KnownOne, Depth);
233 return false;
234 }
235 // If this is the root being simplified, allow it to have multiple uses,
236 // just set the DemandedMask to all bits.
237 DemandedMask = MVT::getIntVTBitMask(Op.getValueType());
238 } else if (DemandedMask == 0) {
239 // Not demanding any bits from Op.
240 if (Op.getOpcode() != ISD::UNDEF)
241 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::UNDEF, Op.getValueType()));
242 return false;
243 } else if (Depth == 6) { // Limit search depth.
244 return false;
245 }
246
247 uint64_t KnownZero2, KnownOne2, KnownZeroOut, KnownOneOut;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000248 switch (Op.getOpcode()) {
249 case ISD::Constant:
Nate Begeman368e18d2006-02-16 21:11:51 +0000250 // We know all of the bits for a constant!
251 KnownOne = cast<ConstantSDNode>(Op)->getValue() & DemandedMask;
252 KnownZero = ~KnownOne & DemandedMask;
Chris Lattnerec665152006-02-26 23:36:02 +0000253 return false; // Don't fall through, will infinitely loop.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000254 case ISD::AND:
Chris Lattner81cd3552006-02-27 00:36:27 +0000255 // If the RHS is a constant, check to see if the LHS would be zero without
256 // using the bits from the RHS. Below, we use knowledge about the RHS to
257 // simplify the LHS, here we're using information from the LHS to simplify
258 // the RHS.
259 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
260 uint64_t LHSZero, LHSOne;
261 ComputeMaskedBits(Op.getOperand(0), DemandedMask,
262 LHSZero, LHSOne, Depth+1);
263 // If the LHS already has zeros where RHSC does, this and is dead.
264 if ((LHSZero & DemandedMask) == (~RHSC->getValue() & DemandedMask))
265 return TLO.CombineTo(Op, Op.getOperand(0));
266 // If any of the set bits in the RHS are known zero on the LHS, shrink
267 // the constant.
268 if (TLO.ShrinkDemandedConstant(Op, ~LHSZero & DemandedMask))
269 return true;
270 }
271
Nate Begeman368e18d2006-02-16 21:11:51 +0000272 if (SimplifyDemandedBits(Op.getOperand(1), DemandedMask, KnownZero,
273 KnownOne, TLO, Depth+1))
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000274 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000275 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Nate Begeman368e18d2006-02-16 21:11:51 +0000276 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & ~KnownZero,
277 KnownZero2, KnownOne2, TLO, Depth+1))
278 return true;
279 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
280
281 // If all of the demanded bits are known one on one side, return the other.
282 // These bits cannot contribute to the result of the 'and'.
283 if ((DemandedMask & ~KnownZero2 & KnownOne)==(DemandedMask & ~KnownZero2))
284 return TLO.CombineTo(Op, Op.getOperand(0));
285 if ((DemandedMask & ~KnownZero & KnownOne2)==(DemandedMask & ~KnownZero))
286 return TLO.CombineTo(Op, Op.getOperand(1));
287 // If all of the demanded bits in the inputs are known zeros, return zero.
288 if ((DemandedMask & (KnownZero|KnownZero2)) == DemandedMask)
289 return TLO.CombineTo(Op, TLO.DAG.getConstant(0, Op.getValueType()));
290 // If the RHS is a constant, see if we can simplify it.
291 if (TLO.ShrinkDemandedConstant(Op, DemandedMask & ~KnownZero2))
292 return true;
Chris Lattner5f0c6582006-02-27 00:22:28 +0000293
Nate Begeman368e18d2006-02-16 21:11:51 +0000294 // Output known-1 bits are only known if set in both the LHS & RHS.
295 KnownOne &= KnownOne2;
296 // Output known-0 are known to be clear if zero in either the LHS | RHS.
297 KnownZero |= KnownZero2;
298 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000299 case ISD::OR:
Nate Begeman368e18d2006-02-16 21:11:51 +0000300 if (SimplifyDemandedBits(Op.getOperand(1), DemandedMask, KnownZero,
301 KnownOne, TLO, Depth+1))
302 return true;
303 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
304 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & ~KnownOne,
305 KnownZero2, KnownOne2, TLO, Depth+1))
306 return true;
307 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
308
309 // If all of the demanded bits are known zero on one side, return the other.
310 // These bits cannot contribute to the result of the 'or'.
Jeff Cohen5755b172006-02-17 02:12:18 +0000311 if ((DemandedMask & ~KnownOne2 & KnownZero) == (DemandedMask & ~KnownOne2))
Nate Begeman368e18d2006-02-16 21:11:51 +0000312 return TLO.CombineTo(Op, Op.getOperand(0));
Jeff Cohen5755b172006-02-17 02:12:18 +0000313 if ((DemandedMask & ~KnownOne & KnownZero2) == (DemandedMask & ~KnownOne))
Nate Begeman368e18d2006-02-16 21:11:51 +0000314 return TLO.CombineTo(Op, Op.getOperand(1));
315 // If all of the potentially set bits on one side are known to be set on
316 // the other side, just use the 'other' side.
317 if ((DemandedMask & (~KnownZero) & KnownOne2) ==
318 (DemandedMask & (~KnownZero)))
319 return TLO.CombineTo(Op, Op.getOperand(0));
320 if ((DemandedMask & (~KnownZero2) & KnownOne) ==
321 (DemandedMask & (~KnownZero2)))
322 return TLO.CombineTo(Op, Op.getOperand(1));
323 // If the RHS is a constant, see if we can simplify it.
324 if (TLO.ShrinkDemandedConstant(Op, DemandedMask))
325 return true;
326
327 // Output known-0 bits are only known if clear in both the LHS & RHS.
328 KnownZero &= KnownZero2;
329 // Output known-1 are known to be set if set in either the LHS | RHS.
330 KnownOne |= KnownOne2;
331 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000332 case ISD::XOR:
Nate Begeman368e18d2006-02-16 21:11:51 +0000333 if (SimplifyDemandedBits(Op.getOperand(1), DemandedMask, KnownZero,
334 KnownOne, TLO, Depth+1))
335 return true;
336 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
337 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask, KnownZero2,
338 KnownOne2, TLO, Depth+1))
339 return true;
340 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
341
342 // If all of the demanded bits are known zero on one side, return the other.
343 // These bits cannot contribute to the result of the 'xor'.
344 if ((DemandedMask & KnownZero) == DemandedMask)
345 return TLO.CombineTo(Op, Op.getOperand(0));
346 if ((DemandedMask & KnownZero2) == DemandedMask)
347 return TLO.CombineTo(Op, Op.getOperand(1));
348
349 // Output known-0 bits are known if clear or set in both the LHS & RHS.
350 KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
351 // Output known-1 are known to be set if set in only one of the LHS, RHS.
352 KnownOneOut = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
353
354 // If all of the unknown bits are known to be zero on one side or the other
355 // (but not both) turn this into an *inclusive* or.
356 // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
357 if (uint64_t UnknownBits = DemandedMask & ~(KnownZeroOut|KnownOneOut))
358 if ((UnknownBits & (KnownZero|KnownZero2)) == UnknownBits)
359 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::OR, Op.getValueType(),
360 Op.getOperand(0),
361 Op.getOperand(1)));
362 // If all of the demanded bits on one side are known, and all of the set
363 // bits on that side are also known to be set on the other side, turn this
364 // into an AND, as we know the bits will be cleared.
365 // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
366 if ((DemandedMask & (KnownZero|KnownOne)) == DemandedMask) { // all known
367 if ((KnownOne & KnownOne2) == KnownOne) {
368 MVT::ValueType VT = Op.getValueType();
369 SDOperand ANDC = TLO.DAG.getConstant(~KnownOne & DemandedMask, VT);
370 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::AND, VT, Op.getOperand(0),
371 ANDC));
372 }
373 }
374
375 // If the RHS is a constant, see if we can simplify it.
376 // FIXME: for XOR, we prefer to force bits to 1 if they will make a -1.
377 if (TLO.ShrinkDemandedConstant(Op, DemandedMask))
378 return true;
379
380 KnownZero = KnownZeroOut;
381 KnownOne = KnownOneOut;
382 break;
383 case ISD::SETCC:
384 // If we know the result of a setcc has the top bits zero, use this info.
385 if (getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult)
386 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
387 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000388 case ISD::SELECT:
Nate Begeman368e18d2006-02-16 21:11:51 +0000389 if (SimplifyDemandedBits(Op.getOperand(2), DemandedMask, KnownZero,
390 KnownOne, TLO, Depth+1))
391 return true;
392 if (SimplifyDemandedBits(Op.getOperand(1), DemandedMask, KnownZero2,
393 KnownOne2, TLO, Depth+1))
394 return true;
395 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
396 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
397
398 // If the operands are constants, see if we can simplify them.
399 if (TLO.ShrinkDemandedConstant(Op, DemandedMask))
400 return true;
401
402 // Only known if known in both the LHS and RHS.
403 KnownOne &= KnownOne2;
404 KnownZero &= KnownZero2;
405 break;
Chris Lattnerec665152006-02-26 23:36:02 +0000406 case ISD::SELECT_CC:
407 if (SimplifyDemandedBits(Op.getOperand(3), DemandedMask, KnownZero,
408 KnownOne, TLO, Depth+1))
409 return true;
410 if (SimplifyDemandedBits(Op.getOperand(2), DemandedMask, KnownZero2,
411 KnownOne2, TLO, Depth+1))
412 return true;
413 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
414 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
415
416 // If the operands are constants, see if we can simplify them.
417 if (TLO.ShrinkDemandedConstant(Op, DemandedMask))
418 return true;
419
420 // Only known if known in both the LHS and RHS.
421 KnownOne &= KnownOne2;
422 KnownZero &= KnownZero2;
423 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000424 case ISD::SHL:
Nate Begeman368e18d2006-02-16 21:11:51 +0000425 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
426 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask >> SA->getValue(),
427 KnownZero, KnownOne, TLO, Depth+1))
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000428 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000429 KnownZero <<= SA->getValue();
430 KnownOne <<= SA->getValue();
431 KnownZero |= (1ULL << SA->getValue())-1; // low bits known zero.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000432 }
433 break;
Nate Begeman368e18d2006-02-16 21:11:51 +0000434 case ISD::SRL:
435 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
436 MVT::ValueType VT = Op.getValueType();
437 unsigned ShAmt = SA->getValue();
438
439 // Compute the new bits that are at the top now.
440 uint64_t HighBits = (1ULL << ShAmt)-1;
441 HighBits <<= MVT::getSizeInBits(VT) - ShAmt;
442 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
443
444 if (SimplifyDemandedBits(Op.getOperand(0),
445 (DemandedMask << ShAmt) & TypeMask,
446 KnownZero, KnownOne, TLO, Depth+1))
447 return true;
448 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
449 KnownZero &= TypeMask;
450 KnownOne &= TypeMask;
451 KnownZero >>= ShAmt;
452 KnownOne >>= ShAmt;
453 KnownZero |= HighBits; // high bits known zero.
454 }
455 break;
456 case ISD::SRA:
457 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
458 MVT::ValueType VT = Op.getValueType();
459 unsigned ShAmt = SA->getValue();
460
461 // Compute the new bits that are at the top now.
462 uint64_t HighBits = (1ULL << ShAmt)-1;
463 HighBits <<= MVT::getSizeInBits(VT) - ShAmt;
464 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
465
466 if (SimplifyDemandedBits(Op.getOperand(0),
467 (DemandedMask << ShAmt) & TypeMask,
468 KnownZero, KnownOne, TLO, Depth+1))
469 return true;
470 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
471 KnownZero &= TypeMask;
472 KnownOne &= TypeMask;
473 KnownZero >>= SA->getValue();
474 KnownOne >>= SA->getValue();
475
476 // Handle the sign bits.
477 uint64_t SignBit = MVT::getIntVTSignBit(VT);
478 SignBit >>= SA->getValue(); // Adjust to where it is now in the mask.
479
480 // If the input sign bit is known to be zero, or if none of the top bits
481 // are demanded, turn this into an unsigned shift right.
482 if ((KnownZero & SignBit) || (HighBits & ~DemandedMask) == HighBits) {
483 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, VT, Op.getOperand(0),
484 Op.getOperand(1)));
485 } else if (KnownOne & SignBit) { // New bits are known one.
486 KnownOne |= HighBits;
487 }
488 }
489 break;
490 case ISD::SIGN_EXTEND_INREG: {
491 MVT::ValueType VT = Op.getValueType();
492 MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
493
Chris Lattnerec665152006-02-26 23:36:02 +0000494 // Sign extension. Compute the demanded bits in the result that are not
Nate Begeman368e18d2006-02-16 21:11:51 +0000495 // present in the input.
Chris Lattnerec665152006-02-26 23:36:02 +0000496 uint64_t NewBits = ~MVT::getIntVTBitMask(EVT) & DemandedMask;
Nate Begeman368e18d2006-02-16 21:11:51 +0000497
Chris Lattnerec665152006-02-26 23:36:02 +0000498 // If none of the extended bits are demanded, eliminate the sextinreg.
499 if (NewBits == 0)
500 return TLO.CombineTo(Op, Op.getOperand(0));
501
Nate Begeman368e18d2006-02-16 21:11:51 +0000502 uint64_t InSignBit = MVT::getIntVTSignBit(EVT);
503 int64_t InputDemandedBits = DemandedMask & MVT::getIntVTBitMask(EVT);
504
Chris Lattnerec665152006-02-26 23:36:02 +0000505 // Since the sign extended bits are demanded, we know that the sign
Nate Begeman368e18d2006-02-16 21:11:51 +0000506 // bit is demanded.
Chris Lattnerec665152006-02-26 23:36:02 +0000507 InputDemandedBits |= InSignBit;
Nate Begeman368e18d2006-02-16 21:11:51 +0000508
509 if (SimplifyDemandedBits(Op.getOperand(0), InputDemandedBits,
510 KnownZero, KnownOne, TLO, Depth+1))
511 return true;
512 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
513
514 // If the sign bit of the input is known set or clear, then we know the
515 // top bits of the result.
516
Chris Lattnerec665152006-02-26 23:36:02 +0000517 // If the input sign bit is known zero, convert this into a zero extension.
518 if (KnownZero & InSignBit)
519 return TLO.CombineTo(Op,
520 TLO.DAG.getZeroExtendInReg(Op.getOperand(0), EVT));
521
522 if (KnownOne & InSignBit) { // Input sign bit known set
Nate Begeman368e18d2006-02-16 21:11:51 +0000523 KnownOne |= NewBits;
524 KnownZero &= ~NewBits;
Chris Lattnerec665152006-02-26 23:36:02 +0000525 } else { // Input sign bit unknown
Nate Begeman368e18d2006-02-16 21:11:51 +0000526 KnownZero &= ~NewBits;
527 KnownOne &= ~NewBits;
528 }
529 break;
530 }
Chris Lattnerec665152006-02-26 23:36:02 +0000531 case ISD::CTTZ:
532 case ISD::CTLZ:
533 case ISD::CTPOP: {
534 MVT::ValueType VT = Op.getValueType();
535 unsigned LowBits = Log2_32(MVT::getSizeInBits(VT))+1;
536 KnownZero = ~((1ULL << LowBits)-1) & MVT::getIntVTBitMask(VT);
537 KnownOne = 0;
538 break;
539 }
540 case ISD::ZEXTLOAD: {
541 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(3))->getVT();
542 KnownZero |= ~MVT::getIntVTBitMask(VT) & DemandedMask;
543 break;
544 }
545 case ISD::ZERO_EXTEND: {
546 uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType());
547
548 // If none of the top bits are demanded, convert this into an any_extend.
549 uint64_t NewBits = (~InMask) & DemandedMask;
550 if (NewBits == 0)
551 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ANY_EXTEND,
552 Op.getValueType(),
553 Op.getOperand(0)));
554
555 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & InMask,
556 KnownZero, KnownOne, TLO, Depth+1))
557 return true;
558 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
559 KnownZero |= NewBits;
560 break;
561 }
562 case ISD::SIGN_EXTEND: {
563 MVT::ValueType InVT = Op.getOperand(0).getValueType();
564 uint64_t InMask = MVT::getIntVTBitMask(InVT);
565 uint64_t InSignBit = MVT::getIntVTSignBit(InVT);
566 uint64_t NewBits = (~InMask) & DemandedMask;
567
568 // If none of the top bits are demanded, convert this into an any_extend.
569 if (NewBits == 0)
570 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ANY_EXTEND,Op.getValueType(),
571 Op.getOperand(0)));
572
573 // Since some of the sign extended bits are demanded, we know that the sign
574 // bit is demanded.
575 uint64_t InDemandedBits = DemandedMask & InMask;
576 InDemandedBits |= InSignBit;
577
578 if (SimplifyDemandedBits(Op.getOperand(0), InDemandedBits, KnownZero,
579 KnownOne, TLO, Depth+1))
580 return true;
581
582 // If the sign bit is known zero, convert this to a zero extend.
583 if (KnownZero & InSignBit)
584 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ZERO_EXTEND,
585 Op.getValueType(),
586 Op.getOperand(0)));
587
588 // If the sign bit is known one, the top bits match.
589 if (KnownOne & InSignBit) {
590 KnownOne |= NewBits;
591 KnownZero &= ~NewBits;
592 } else { // Otherwise, top bits aren't known.
593 KnownOne &= ~NewBits;
594 KnownZero &= ~NewBits;
595 }
596 break;
597 }
598 case ISD::ANY_EXTEND: {
599 uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType());
600 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & InMask,
601 KnownZero, KnownOne, TLO, Depth+1))
602 return true;
603 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
604 break;
605 }
606 case ISD::AssertZext: {
607 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
608 uint64_t InMask = MVT::getIntVTBitMask(VT);
609 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & InMask,
610 KnownZero, KnownOne, TLO, Depth+1))
611 return true;
612 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
613 KnownZero |= ~InMask & DemandedMask;
614 break;
615 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000616 case ISD::ADD:
Chris Lattnera6bc5a42006-02-27 01:00:42 +0000617 case ISD::SUB:
618 // Just use ComputeMaskedBits to compute output bits, there are no
619 // simplifications that can be done here, and sub always demands all input
620 // bits.
621 ComputeMaskedBits(Op, DemandedMask, KnownZero, KnownOne, Depth);
622 break;
Nate Begeman368e18d2006-02-16 21:11:51 +0000623 }
Chris Lattnerec665152006-02-26 23:36:02 +0000624
625 // If we know the value of all of the demanded bits, return this as a
626 // constant.
627 if ((DemandedMask & (KnownZero|KnownOne)) == DemandedMask)
628 return TLO.CombineTo(Op, TLO.DAG.getConstant(KnownOne, Op.getValueType()));
629
Nate Begeman368e18d2006-02-16 21:11:51 +0000630 return false;
631}
632
633/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
634/// this predicate to simplify operations downstream. Mask is known to be zero
635/// for bits that V cannot have.
636bool TargetLowering::MaskedValueIsZero(SDOperand Op, uint64_t Mask,
637 unsigned Depth) const {
638 uint64_t KnownZero, KnownOne;
639 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
640 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
641 return (KnownZero & Mask) == Mask;
642}
643
644/// ComputeMaskedBits - Determine which of the bits specified in Mask are
645/// known to be either zero or one and return them in the KnownZero/KnownOne
646/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
647/// processing.
648void TargetLowering::ComputeMaskedBits(SDOperand Op, uint64_t Mask,
649 uint64_t &KnownZero, uint64_t &KnownOne,
650 unsigned Depth) const {
651 KnownZero = KnownOne = 0; // Don't know anything.
652 if (Depth == 6 || Mask == 0)
653 return; // Limit search depth.
654
655 uint64_t KnownZero2, KnownOne2;
656
657 switch (Op.getOpcode()) {
658 case ISD::Constant:
659 // We know all of the bits for a constant!
660 KnownOne = cast<ConstantSDNode>(Op)->getValue() & Mask;
661 KnownZero = ~KnownOne & Mask;
662 return;
663 case ISD::AND:
664 // If either the LHS or the RHS are Zero, the result is zero.
665 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
666 Mask &= ~KnownZero;
667 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
668 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
669 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
670
671 // Output known-1 bits are only known if set in both the LHS & RHS.
672 KnownOne &= KnownOne2;
673 // Output known-0 are known to be clear if zero in either the LHS | RHS.
674 KnownZero |= KnownZero2;
675 return;
676 case ISD::OR:
677 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
678 Mask &= ~KnownOne;
679 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
680 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
681 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
682
683 // Output known-0 bits are only known if clear in both the LHS & RHS.
684 KnownZero &= KnownZero2;
685 // Output known-1 are known to be set if set in either the LHS | RHS.
686 KnownOne |= KnownOne2;
687 return;
688 case ISD::XOR: {
689 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
690 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
691 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
692 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
693
694 // Output known-0 bits are known if clear or set in both the LHS & RHS.
695 uint64_t KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
696 // Output known-1 are known to be set if set in only one of the LHS, RHS.
697 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
698 KnownZero = KnownZeroOut;
699 return;
700 }
701 case ISD::SELECT:
702 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
703 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
704 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
705 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
706
707 // Only known if known in both the LHS and RHS.
708 KnownOne &= KnownOne2;
709 KnownZero &= KnownZero2;
710 return;
711 case ISD::SELECT_CC:
712 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
713 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
714 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
715 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
716
717 // Only known if known in both the LHS and RHS.
718 KnownOne &= KnownOne2;
719 KnownZero &= KnownZero2;
720 return;
721 case ISD::SETCC:
722 // If we know the result of a setcc has the top bits zero, use this info.
723 if (getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult)
724 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
725 return;
726 case ISD::SHL:
727 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
728 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
729 Mask >>= SA->getValue();
730 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
731 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
732 KnownZero <<= SA->getValue();
733 KnownOne <<= SA->getValue();
734 KnownZero |= (1ULL << SA->getValue())-1; // low bits known zero.
735 }
Nate Begeman003a2722006-02-18 02:43:25 +0000736 return;
Nate Begeman368e18d2006-02-16 21:11:51 +0000737 case ISD::SRL:
738 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
739 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
740 uint64_t HighBits = (1ULL << SA->getValue())-1;
741 HighBits <<= MVT::getSizeInBits(Op.getValueType())-SA->getValue();
742 Mask <<= SA->getValue();
743 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Nate Begeman003a2722006-02-18 02:43:25 +0000744 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Nate Begeman368e18d2006-02-16 21:11:51 +0000745 KnownZero >>= SA->getValue();
746 KnownOne >>= SA->getValue();
747 KnownZero |= HighBits; // high bits known zero.
748 }
Nate Begeman003a2722006-02-18 02:43:25 +0000749 return;
Nate Begeman368e18d2006-02-16 21:11:51 +0000750 case ISD::SRA:
751 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
752 uint64_t HighBits = (1ULL << SA->getValue())-1;
753 HighBits <<= MVT::getSizeInBits(Op.getValueType())-SA->getValue();
754 Mask <<= SA->getValue();
755 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
756 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
757 KnownZero >>= SA->getValue();
758 KnownOne >>= SA->getValue();
759
760 // Handle the sign bits.
761 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(Op.getValueType())-1);
762 SignBit >>= SA->getValue(); // Adjust to where it is now in the mask.
763
764 if (KnownZero & SignBit) { // New bits are known zero.
765 KnownZero |= HighBits;
766 } else if (KnownOne & SignBit) { // New bits are known one.
767 KnownOne |= HighBits;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000768 }
769 }
Nate Begeman003a2722006-02-18 02:43:25 +0000770 return;
Chris Lattnerec665152006-02-26 23:36:02 +0000771 case ISD::SIGN_EXTEND_INREG: {
772 MVT::ValueType VT = Op.getValueType();
773 MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
774
775 // Sign extension. Compute the demanded bits in the result that are not
776 // present in the input.
777 uint64_t NewBits = ~MVT::getIntVTBitMask(EVT) & Mask;
778
779 uint64_t InSignBit = MVT::getIntVTSignBit(EVT);
780 int64_t InputDemandedBits = Mask & MVT::getIntVTBitMask(EVT);
781
782 // If the sign extended bits are demanded, we know that the sign
783 // bit is demanded.
784 if (NewBits)
785 InputDemandedBits |= InSignBit;
786
787 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
788 KnownZero, KnownOne, Depth+1);
789 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
790
791 // If the sign bit of the input is known set or clear, then we know the
792 // top bits of the result.
793 if (KnownZero & InSignBit) { // Input sign bit known clear
794 KnownZero |= NewBits;
795 KnownOne &= ~NewBits;
796 } else if (KnownOne & InSignBit) { // Input sign bit known set
797 KnownOne |= NewBits;
798 KnownZero &= ~NewBits;
799 } else { // Input sign bit unknown
800 KnownZero &= ~NewBits;
801 KnownOne &= ~NewBits;
802 }
803 return;
804 }
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000805 case ISD::CTTZ:
806 case ISD::CTLZ:
Nate Begeman368e18d2006-02-16 21:11:51 +0000807 case ISD::CTPOP: {
808 MVT::ValueType VT = Op.getValueType();
809 unsigned LowBits = Log2_32(MVT::getSizeInBits(VT))+1;
810 KnownZero = ~((1ULL << LowBits)-1) & MVT::getIntVTBitMask(VT);
811 KnownOne = 0;
812 return;
813 }
814 case ISD::ZEXTLOAD: {
Chris Lattnerec665152006-02-26 23:36:02 +0000815 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(3))->getVT();
816 KnownZero |= ~MVT::getIntVTBitMask(VT) & Mask;
Nate Begeman368e18d2006-02-16 21:11:51 +0000817 return;
818 }
819 case ISD::ZERO_EXTEND: {
Chris Lattnerec665152006-02-26 23:36:02 +0000820 uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType());
821 uint64_t NewBits = (~InMask) & Mask;
822 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
823 KnownOne, Depth+1);
824 KnownZero |= NewBits & Mask;
825 KnownOne &= ~NewBits;
826 return;
827 }
828 case ISD::SIGN_EXTEND: {
829 MVT::ValueType InVT = Op.getOperand(0).getValueType();
830 unsigned InBits = MVT::getSizeInBits(InVT);
831 uint64_t InMask = MVT::getIntVTBitMask(InVT);
832 uint64_t InSignBit = 1ULL << (InBits-1);
833 uint64_t NewBits = (~InMask) & Mask;
834 uint64_t InDemandedBits = Mask & InMask;
835
836 // If any of the sign extended bits are demanded, we know that the sign
837 // bit is demanded.
838 if (NewBits & Mask)
839 InDemandedBits |= InSignBit;
840
841 ComputeMaskedBits(Op.getOperand(0), InDemandedBits, KnownZero,
842 KnownOne, Depth+1);
843 // If the sign bit is known zero or one, the top bits match.
844 if (KnownZero & InSignBit) {
845 KnownZero |= NewBits;
846 KnownOne &= ~NewBits;
847 } else if (KnownOne & InSignBit) {
848 KnownOne |= NewBits;
849 KnownZero &= ~NewBits;
850 } else { // Otherwise, top bits aren't known.
851 KnownOne &= ~NewBits;
852 KnownZero &= ~NewBits;
853 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000854 return;
855 }
856 case ISD::ANY_EXTEND: {
Chris Lattnerec665152006-02-26 23:36:02 +0000857 MVT::ValueType VT = Op.getOperand(0).getValueType();
858 ComputeMaskedBits(Op.getOperand(0), Mask & MVT::getIntVTBitMask(VT),
859 KnownZero, KnownOne, Depth+1);
Nate Begeman368e18d2006-02-16 21:11:51 +0000860 return;
861 }
862 case ISD::AssertZext: {
Chris Lattnerec665152006-02-26 23:36:02 +0000863 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
864 uint64_t InMask = MVT::getIntVTBitMask(VT);
865 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
866 KnownOne, Depth+1);
867 KnownZero |= (~InMask) & Mask;
Nate Begeman368e18d2006-02-16 21:11:51 +0000868 return;
869 }
870 case ISD::ADD: {
871 // If either the LHS or the RHS are Zero, the result is zero.
872 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
873 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
874 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
875 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
876
877 // Output known-0 bits are known if clear or set in both the low clear bits
Chris Lattnerb6b17ff2006-03-13 06:42:16 +0000878 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
879 // low 3 bits clear.
Nate Begeman368e18d2006-02-16 21:11:51 +0000880 uint64_t KnownZeroOut = std::min(CountTrailingZeros_64(~KnownZero),
881 CountTrailingZeros_64(~KnownZero2));
882
883 KnownZero = (1ULL << KnownZeroOut) - 1;
884 KnownOne = 0;
885 return;
886 }
Chris Lattnera6bc5a42006-02-27 01:00:42 +0000887 case ISD::SUB: {
888 ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0));
889 if (!CLHS) return;
890
Nate Begeman368e18d2006-02-16 21:11:51 +0000891 // We know that the top bits of C-X are clear if X contains less bits
892 // than C (i.e. no wrap-around can happen). For example, 20-X is
Chris Lattnera6bc5a42006-02-27 01:00:42 +0000893 // positive if we can prove that X is >= 0 and < 16.
894 MVT::ValueType VT = CLHS->getValueType(0);
895 if ((CLHS->getValue() & MVT::getIntVTSignBit(VT)) == 0) { // sign bit clear
896 unsigned NLZ = CountLeadingZeros_64(CLHS->getValue()+1);
897 uint64_t MaskV = (1ULL << (63-NLZ))-1; // NLZ can't be 64 with no sign bit
898 MaskV = ~MaskV & MVT::getIntVTBitMask(VT);
899 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero, KnownOne, Depth+1);
900
901 // If all of the MaskV bits are known to be zero, then we know the output
902 // top bits are zero, because we now know that the output is from [0-C].
903 if ((KnownZero & MaskV) == MaskV) {
904 unsigned NLZ2 = CountLeadingZeros_64(CLHS->getValue());
905 KnownZero = ~((1ULL << (64-NLZ2))-1) & Mask; // Top bits known zero.
906 KnownOne = 0; // No one bits known.
907 } else {
908 KnownOne = KnownOne = 0; // Otherwise, nothing known.
909 }
910 }
Nate Begeman003a2722006-02-18 02:43:25 +0000911 return;
Chris Lattnera6bc5a42006-02-27 01:00:42 +0000912 }
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000913 default:
914 // Allow the target to implement this method for its nodes.
915 if (Op.getOpcode() >= ISD::BUILTIN_OP_END)
Nate Begeman368e18d2006-02-16 21:11:51 +0000916 computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne);
Nate Begeman003a2722006-02-18 02:43:25 +0000917 return;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000918 }
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000919}
920
Nate Begeman368e18d2006-02-16 21:11:51 +0000921/// computeMaskedBitsForTargetNode - Determine which of the bits specified
922/// in Mask are known to be either zero or one and return them in the
923/// KnownZero/KnownOne bitsets.
924void TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
925 uint64_t Mask,
926 uint64_t &KnownZero,
927 uint64_t &KnownOne,
928 unsigned Depth) const {
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000929 assert(Op.getOpcode() >= ISD::BUILTIN_OP_END &&
930 "Should use MaskedValueIsZero if you don't know whether Op"
931 " is a target node!");
Nate Begeman368e18d2006-02-16 21:11:51 +0000932 KnownZero = 0;
933 KnownOne = 0;
Evan Cheng3a03ebb2005-12-21 23:05:39 +0000934}
Chris Lattner4ccb0702006-01-26 20:37:03 +0000935
Chris Lattner00ffed02006-03-01 04:52:55 +0000936SDOperand TargetLowering::
937PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
938 // Default implementation: no optimization.
939 return SDOperand();
940}
941
Chris Lattnereb8146b2006-02-04 02:13:02 +0000942//===----------------------------------------------------------------------===//
943// Inline Assembler Implementation Methods
944//===----------------------------------------------------------------------===//
945
946TargetLowering::ConstraintType
947TargetLowering::getConstraintType(char ConstraintLetter) const {
948 // FIXME: lots more standard ones to handle.
949 switch (ConstraintLetter) {
950 default: return C_Unknown;
951 case 'r': return C_RegisterClass;
Chris Lattner2b7401e2006-02-24 01:10:46 +0000952 case 'm': // memory
953 case 'o': // offsetable
954 case 'V': // not offsetable
955 return C_Memory;
Chris Lattnereb8146b2006-02-04 02:13:02 +0000956 case 'i': // Simple Integer or Relocatable Constant
957 case 'n': // Simple Integer
958 case 's': // Relocatable Constant
959 case 'I': // Target registers.
960 case 'J':
961 case 'K':
962 case 'L':
963 case 'M':
964 case 'N':
965 case 'O':
Chris Lattner2b7401e2006-02-24 01:10:46 +0000966 case 'P':
967 return C_Other;
Chris Lattnereb8146b2006-02-04 02:13:02 +0000968 }
969}
970
971bool TargetLowering::isOperandValidForConstraint(SDOperand Op,
972 char ConstraintLetter) {
973 switch (ConstraintLetter) {
974 default: return false;
975 case 'i': // Simple Integer or Relocatable Constant
976 case 'n': // Simple Integer
977 case 's': // Relocatable Constant
978 return true; // FIXME: not right.
979 }
980}
981
982
Chris Lattner4ccb0702006-01-26 20:37:03 +0000983std::vector<unsigned> TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +0000984getRegClassForInlineAsmConstraint(const std::string &Constraint,
985 MVT::ValueType VT) const {
986 return std::vector<unsigned>();
987}
988
989
990std::pair<unsigned, const TargetRegisterClass*> TargetLowering::
Chris Lattner4217ca8dc2006-02-21 23:11:00 +0000991getRegForInlineAsmConstraint(const std::string &Constraint,
992 MVT::ValueType VT) const {
Chris Lattner1efa40f2006-02-22 00:56:39 +0000993 if (Constraint[0] != '{')
994 return std::pair<unsigned, const TargetRegisterClass*>(0, 0);
Chris Lattnera55079a2006-02-01 01:29:47 +0000995 assert(*(Constraint.end()-1) == '}' && "Not a brace enclosed constraint?");
996
997 // Remove the braces from around the name.
998 std::string RegName(Constraint.begin()+1, Constraint.end()-1);
Chris Lattner1efa40f2006-02-22 00:56:39 +0000999
1000 // Figure out which register class contains this reg.
Chris Lattner4ccb0702006-01-26 20:37:03 +00001001 const MRegisterInfo *RI = TM.getRegisterInfo();
Chris Lattner1efa40f2006-02-22 00:56:39 +00001002 for (MRegisterInfo::regclass_iterator RCI = RI->regclass_begin(),
1003 E = RI->regclass_end(); RCI != E; ++RCI) {
1004 const TargetRegisterClass *RC = *RCI;
Chris Lattnerb3befd42006-02-22 23:00:51 +00001005
1006 // If none of the the value types for this register class are valid, we
1007 // can't use it. For example, 64-bit reg classes on 32-bit targets.
1008 bool isLegal = false;
1009 for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
1010 I != E; ++I) {
1011 if (isTypeLegal(*I)) {
1012 isLegal = true;
1013 break;
1014 }
1015 }
1016
1017 if (!isLegal) continue;
1018
Chris Lattner1efa40f2006-02-22 00:56:39 +00001019 for (TargetRegisterClass::iterator I = RC->begin(), E = RC->end();
1020 I != E; ++I) {
Chris Lattnerb3befd42006-02-22 23:00:51 +00001021 if (StringsEqualNoCase(RegName, RI->get(*I).Name))
Chris Lattner1efa40f2006-02-22 00:56:39 +00001022 return std::make_pair(*I, RC);
Chris Lattner1efa40f2006-02-22 00:56:39 +00001023 }
Chris Lattner4ccb0702006-01-26 20:37:03 +00001024 }
Chris Lattnera55079a2006-02-01 01:29:47 +00001025
Chris Lattner1efa40f2006-02-22 00:56:39 +00001026 return std::pair<unsigned, const TargetRegisterClass*>(0, 0);
Chris Lattner4ccb0702006-01-26 20:37:03 +00001027}
Evan Cheng30b37b52006-03-13 23:18:16 +00001028
1029//===----------------------------------------------------------------------===//
1030// Loop Strength Reduction hooks
1031//===----------------------------------------------------------------------===//
1032
1033/// isLegalAddressImmediate - Return true if the integer value or
1034/// GlobalValue can be used as the offset of the target addressing mode.
1035bool TargetLowering::isLegalAddressImmediate(int64_t V) const {
1036 return false;
1037}
1038bool TargetLowering::isLegalAddressImmediate(GlobalValue *GV) const {
1039 return false;
1040}