blob: 922245f55355ac679223c8c2b4ace16f96915b3e [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///
Chris Lattner79227e22006-03-31 00:46:36 +0000151unsigned TargetLowering::getPackedTypeBreakdown(const PackedType *PTy,
152 MVT::ValueType &PTyElementVT,
153 MVT::ValueType &PTyLegalElementVT) const {
Chris Lattnerdc879292006-03-31 00:28:56 +0000154 // Figure out the right, legal destination reg to copy into.
155 unsigned NumElts = PTy->getNumElements();
156 MVT::ValueType EltTy = getValueType(PTy->getElementType());
Chris Lattner79227e22006-03-31 00:46:36 +0000157 PTyElementVT = EltTy;
Chris Lattnerdc879292006-03-31 00:28:56 +0000158
159 unsigned NumVectorRegs = 1;
160
161 // Divide the input until we get to a supported size. This will always
162 // end with a scalar if the target doesn't support vectors.
163 while (NumElts > 1 && !isTypeLegal(getVectorType(EltTy, NumElts))) {
164 NumElts >>= 1;
165 NumVectorRegs <<= 1;
166 }
167
168 MVT::ValueType VT;
169 if (NumElts == 1)
170 VT = EltTy;
171 else
172 VT = getVectorType(EltTy, NumElts);
173
174 MVT::ValueType DestVT = getTypeToTransformTo(VT);
Chris Lattner79227e22006-03-31 00:46:36 +0000175 PTyLegalElementVT = DestVT;
Chris Lattnerdc879292006-03-31 00:28:56 +0000176 if (DestVT < VT) {
177 // Value is expanded, e.g. i64 -> i16.
Chris Lattner79227e22006-03-31 00:46:36 +0000178 return NumVectorRegs*(MVT::getSizeInBits(VT)/MVT::getSizeInBits(DestVT));
Chris Lattnerdc879292006-03-31 00:28:56 +0000179 } else {
180 // Otherwise, promotion or legal types use the same number of registers as
181 // the vector decimated to the appropriate level.
Chris Lattner79227e22006-03-31 00:46:36 +0000182 return NumVectorRegs;
Chris Lattnerdc879292006-03-31 00:28:56 +0000183 }
184
185 return DestVT;
186}
187
Chris Lattnereb8146b2006-02-04 02:13:02 +0000188//===----------------------------------------------------------------------===//
189// Optimization Methods
190//===----------------------------------------------------------------------===//
191
Nate Begeman368e18d2006-02-16 21:11:51 +0000192/// ShrinkDemandedConstant - Check to see if the specified operand of the
193/// specified instruction is a constant integer. If so, check to see if there
194/// are any bits set in the constant that are not demanded. If so, shrink the
195/// constant and return true.
196bool TargetLowering::TargetLoweringOpt::ShrinkDemandedConstant(SDOperand Op,
197 uint64_t Demanded) {
Chris Lattnerec665152006-02-26 23:36:02 +0000198 // FIXME: ISD::SELECT, ISD::SELECT_CC
Nate Begeman368e18d2006-02-16 21:11:51 +0000199 switch(Op.getOpcode()) {
200 default: break;
Nate Begemande996292006-02-03 22:24:05 +0000201 case ISD::AND:
Nate Begeman368e18d2006-02-16 21:11:51 +0000202 case ISD::OR:
203 case ISD::XOR:
204 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
205 if ((~Demanded & C->getValue()) != 0) {
206 MVT::ValueType VT = Op.getValueType();
207 SDOperand New = DAG.getNode(Op.getOpcode(), VT, Op.getOperand(0),
208 DAG.getConstant(Demanded & C->getValue(),
209 VT));
210 return CombineTo(Op, New);
Nate Begemande996292006-02-03 22:24:05 +0000211 }
Nate Begemande996292006-02-03 22:24:05 +0000212 break;
213 }
214 return false;
215}
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000216
Nate Begeman368e18d2006-02-16 21:11:51 +0000217/// SimplifyDemandedBits - Look at Op. At this point, we know that only the
218/// DemandedMask bits of the result of Op are ever used downstream. If we can
219/// use this information to simplify Op, create a new simplified DAG node and
220/// return true, returning the original and new nodes in Old and New. Otherwise,
221/// analyze the expression and return a mask of KnownOne and KnownZero bits for
222/// the expression (used to simplify the caller). The KnownZero/One bits may
223/// only be accurate for those bits in the DemandedMask.
224bool TargetLowering::SimplifyDemandedBits(SDOperand Op, uint64_t DemandedMask,
225 uint64_t &KnownZero,
226 uint64_t &KnownOne,
227 TargetLoweringOpt &TLO,
228 unsigned Depth) const {
229 KnownZero = KnownOne = 0; // Don't know anything.
230 // Other users may use these bits.
231 if (!Op.Val->hasOneUse()) {
232 if (Depth != 0) {
233 // If not at the root, Just compute the KnownZero/KnownOne bits to
234 // simplify things downstream.
235 ComputeMaskedBits(Op, DemandedMask, KnownZero, KnownOne, Depth);
236 return false;
237 }
238 // If this is the root being simplified, allow it to have multiple uses,
239 // just set the DemandedMask to all bits.
240 DemandedMask = MVT::getIntVTBitMask(Op.getValueType());
241 } else if (DemandedMask == 0) {
242 // Not demanding any bits from Op.
243 if (Op.getOpcode() != ISD::UNDEF)
244 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::UNDEF, Op.getValueType()));
245 return false;
246 } else if (Depth == 6) { // Limit search depth.
247 return false;
248 }
249
250 uint64_t KnownZero2, KnownOne2, KnownZeroOut, KnownOneOut;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000251 switch (Op.getOpcode()) {
252 case ISD::Constant:
Nate Begeman368e18d2006-02-16 21:11:51 +0000253 // We know all of the bits for a constant!
254 KnownOne = cast<ConstantSDNode>(Op)->getValue() & DemandedMask;
255 KnownZero = ~KnownOne & DemandedMask;
Chris Lattnerec665152006-02-26 23:36:02 +0000256 return false; // Don't fall through, will infinitely loop.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000257 case ISD::AND:
Chris Lattner81cd3552006-02-27 00:36:27 +0000258 // If the RHS is a constant, check to see if the LHS would be zero without
259 // using the bits from the RHS. Below, we use knowledge about the RHS to
260 // simplify the LHS, here we're using information from the LHS to simplify
261 // the RHS.
262 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
263 uint64_t LHSZero, LHSOne;
264 ComputeMaskedBits(Op.getOperand(0), DemandedMask,
265 LHSZero, LHSOne, Depth+1);
266 // If the LHS already has zeros where RHSC does, this and is dead.
267 if ((LHSZero & DemandedMask) == (~RHSC->getValue() & DemandedMask))
268 return TLO.CombineTo(Op, Op.getOperand(0));
269 // If any of the set bits in the RHS are known zero on the LHS, shrink
270 // the constant.
271 if (TLO.ShrinkDemandedConstant(Op, ~LHSZero & DemandedMask))
272 return true;
273 }
274
Nate Begeman368e18d2006-02-16 21:11:51 +0000275 if (SimplifyDemandedBits(Op.getOperand(1), DemandedMask, KnownZero,
276 KnownOne, TLO, Depth+1))
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000277 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000278 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Nate Begeman368e18d2006-02-16 21:11:51 +0000279 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & ~KnownZero,
280 KnownZero2, KnownOne2, TLO, Depth+1))
281 return true;
282 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
283
284 // If all of the demanded bits are known one on one side, return the other.
285 // These bits cannot contribute to the result of the 'and'.
286 if ((DemandedMask & ~KnownZero2 & KnownOne)==(DemandedMask & ~KnownZero2))
287 return TLO.CombineTo(Op, Op.getOperand(0));
288 if ((DemandedMask & ~KnownZero & KnownOne2)==(DemandedMask & ~KnownZero))
289 return TLO.CombineTo(Op, Op.getOperand(1));
290 // If all of the demanded bits in the inputs are known zeros, return zero.
291 if ((DemandedMask & (KnownZero|KnownZero2)) == DemandedMask)
292 return TLO.CombineTo(Op, TLO.DAG.getConstant(0, Op.getValueType()));
293 // If the RHS is a constant, see if we can simplify it.
294 if (TLO.ShrinkDemandedConstant(Op, DemandedMask & ~KnownZero2))
295 return true;
Chris Lattner5f0c6582006-02-27 00:22:28 +0000296
Nate Begeman368e18d2006-02-16 21:11:51 +0000297 // Output known-1 bits are only known if set in both the LHS & RHS.
298 KnownOne &= KnownOne2;
299 // Output known-0 are known to be clear if zero in either the LHS | RHS.
300 KnownZero |= KnownZero2;
301 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000302 case ISD::OR:
Nate Begeman368e18d2006-02-16 21:11:51 +0000303 if (SimplifyDemandedBits(Op.getOperand(1), DemandedMask, KnownZero,
304 KnownOne, TLO, Depth+1))
305 return true;
306 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
307 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & ~KnownOne,
308 KnownZero2, KnownOne2, TLO, Depth+1))
309 return true;
310 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
311
312 // If all of the demanded bits are known zero on one side, return the other.
313 // These bits cannot contribute to the result of the 'or'.
Jeff Cohen5755b172006-02-17 02:12:18 +0000314 if ((DemandedMask & ~KnownOne2 & KnownZero) == (DemandedMask & ~KnownOne2))
Nate Begeman368e18d2006-02-16 21:11:51 +0000315 return TLO.CombineTo(Op, Op.getOperand(0));
Jeff Cohen5755b172006-02-17 02:12:18 +0000316 if ((DemandedMask & ~KnownOne & KnownZero2) == (DemandedMask & ~KnownOne))
Nate Begeman368e18d2006-02-16 21:11:51 +0000317 return TLO.CombineTo(Op, Op.getOperand(1));
318 // If all of the potentially set bits on one side are known to be set on
319 // the other side, just use the 'other' side.
320 if ((DemandedMask & (~KnownZero) & KnownOne2) ==
321 (DemandedMask & (~KnownZero)))
322 return TLO.CombineTo(Op, Op.getOperand(0));
323 if ((DemandedMask & (~KnownZero2) & KnownOne) ==
324 (DemandedMask & (~KnownZero2)))
325 return TLO.CombineTo(Op, Op.getOperand(1));
326 // If the RHS is a constant, see if we can simplify it.
327 if (TLO.ShrinkDemandedConstant(Op, DemandedMask))
328 return true;
329
330 // Output known-0 bits are only known if clear in both the LHS & RHS.
331 KnownZero &= KnownZero2;
332 // Output known-1 are known to be set if set in either the LHS | RHS.
333 KnownOne |= KnownOne2;
334 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000335 case ISD::XOR:
Nate Begeman368e18d2006-02-16 21:11:51 +0000336 if (SimplifyDemandedBits(Op.getOperand(1), DemandedMask, KnownZero,
337 KnownOne, TLO, Depth+1))
338 return true;
339 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
340 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask, KnownZero2,
341 KnownOne2, TLO, Depth+1))
342 return true;
343 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
344
345 // If all of the demanded bits are known zero on one side, return the other.
346 // These bits cannot contribute to the result of the 'xor'.
347 if ((DemandedMask & KnownZero) == DemandedMask)
348 return TLO.CombineTo(Op, Op.getOperand(0));
349 if ((DemandedMask & KnownZero2) == DemandedMask)
350 return TLO.CombineTo(Op, Op.getOperand(1));
351
352 // Output known-0 bits are known if clear or set in both the LHS & RHS.
353 KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
354 // Output known-1 are known to be set if set in only one of the LHS, RHS.
355 KnownOneOut = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
356
357 // If all of the unknown bits are known to be zero on one side or the other
358 // (but not both) turn this into an *inclusive* or.
359 // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
360 if (uint64_t UnknownBits = DemandedMask & ~(KnownZeroOut|KnownOneOut))
361 if ((UnknownBits & (KnownZero|KnownZero2)) == UnknownBits)
362 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::OR, Op.getValueType(),
363 Op.getOperand(0),
364 Op.getOperand(1)));
365 // If all of the demanded bits on one side are known, and all of the set
366 // bits on that side are also known to be set on the other side, turn this
367 // into an AND, as we know the bits will be cleared.
368 // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
369 if ((DemandedMask & (KnownZero|KnownOne)) == DemandedMask) { // all known
370 if ((KnownOne & KnownOne2) == KnownOne) {
371 MVT::ValueType VT = Op.getValueType();
372 SDOperand ANDC = TLO.DAG.getConstant(~KnownOne & DemandedMask, VT);
373 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::AND, VT, Op.getOperand(0),
374 ANDC));
375 }
376 }
377
378 // If the RHS is a constant, see if we can simplify it.
379 // FIXME: for XOR, we prefer to force bits to 1 if they will make a -1.
380 if (TLO.ShrinkDemandedConstant(Op, DemandedMask))
381 return true;
382
383 KnownZero = KnownZeroOut;
384 KnownOne = KnownOneOut;
385 break;
386 case ISD::SETCC:
387 // If we know the result of a setcc has the top bits zero, use this info.
388 if (getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult)
389 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
390 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000391 case ISD::SELECT:
Nate Begeman368e18d2006-02-16 21:11:51 +0000392 if (SimplifyDemandedBits(Op.getOperand(2), DemandedMask, KnownZero,
393 KnownOne, TLO, Depth+1))
394 return true;
395 if (SimplifyDemandedBits(Op.getOperand(1), DemandedMask, KnownZero2,
396 KnownOne2, TLO, Depth+1))
397 return true;
398 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
399 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
400
401 // If the operands are constants, see if we can simplify them.
402 if (TLO.ShrinkDemandedConstant(Op, DemandedMask))
403 return true;
404
405 // Only known if known in both the LHS and RHS.
406 KnownOne &= KnownOne2;
407 KnownZero &= KnownZero2;
408 break;
Chris Lattnerec665152006-02-26 23:36:02 +0000409 case ISD::SELECT_CC:
410 if (SimplifyDemandedBits(Op.getOperand(3), DemandedMask, KnownZero,
411 KnownOne, TLO, Depth+1))
412 return true;
413 if (SimplifyDemandedBits(Op.getOperand(2), DemandedMask, KnownZero2,
414 KnownOne2, TLO, Depth+1))
415 return true;
416 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
417 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
418
419 // If the operands are constants, see if we can simplify them.
420 if (TLO.ShrinkDemandedConstant(Op, DemandedMask))
421 return true;
422
423 // Only known if known in both the LHS and RHS.
424 KnownOne &= KnownOne2;
425 KnownZero &= KnownZero2;
426 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000427 case ISD::SHL:
Nate Begeman368e18d2006-02-16 21:11:51 +0000428 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
429 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask >> SA->getValue(),
430 KnownZero, KnownOne, TLO, Depth+1))
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000431 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000432 KnownZero <<= SA->getValue();
433 KnownOne <<= SA->getValue();
434 KnownZero |= (1ULL << SA->getValue())-1; // low bits known zero.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000435 }
436 break;
Nate Begeman368e18d2006-02-16 21:11:51 +0000437 case ISD::SRL:
438 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
439 MVT::ValueType VT = Op.getValueType();
440 unsigned ShAmt = SA->getValue();
441
442 // Compute the new bits that are at the top now.
443 uint64_t HighBits = (1ULL << ShAmt)-1;
444 HighBits <<= MVT::getSizeInBits(VT) - ShAmt;
445 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
446
447 if (SimplifyDemandedBits(Op.getOperand(0),
448 (DemandedMask << ShAmt) & TypeMask,
449 KnownZero, KnownOne, TLO, Depth+1))
450 return true;
451 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
452 KnownZero &= TypeMask;
453 KnownOne &= TypeMask;
454 KnownZero >>= ShAmt;
455 KnownOne >>= ShAmt;
456 KnownZero |= HighBits; // high bits known zero.
457 }
458 break;
459 case ISD::SRA:
460 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
461 MVT::ValueType VT = Op.getValueType();
462 unsigned ShAmt = SA->getValue();
463
464 // Compute the new bits that are at the top now.
465 uint64_t HighBits = (1ULL << ShAmt)-1;
466 HighBits <<= MVT::getSizeInBits(VT) - ShAmt;
467 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
468
469 if (SimplifyDemandedBits(Op.getOperand(0),
470 (DemandedMask << ShAmt) & TypeMask,
471 KnownZero, KnownOne, TLO, Depth+1))
472 return true;
473 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
474 KnownZero &= TypeMask;
475 KnownOne &= TypeMask;
476 KnownZero >>= SA->getValue();
477 KnownOne >>= SA->getValue();
478
479 // Handle the sign bits.
480 uint64_t SignBit = MVT::getIntVTSignBit(VT);
481 SignBit >>= SA->getValue(); // Adjust to where it is now in the mask.
482
483 // If the input sign bit is known to be zero, or if none of the top bits
484 // are demanded, turn this into an unsigned shift right.
485 if ((KnownZero & SignBit) || (HighBits & ~DemandedMask) == HighBits) {
486 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, VT, Op.getOperand(0),
487 Op.getOperand(1)));
488 } else if (KnownOne & SignBit) { // New bits are known one.
489 KnownOne |= HighBits;
490 }
491 }
492 break;
493 case ISD::SIGN_EXTEND_INREG: {
494 MVT::ValueType VT = Op.getValueType();
495 MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
496
Chris Lattnerec665152006-02-26 23:36:02 +0000497 // Sign extension. Compute the demanded bits in the result that are not
Nate Begeman368e18d2006-02-16 21:11:51 +0000498 // present in the input.
Chris Lattnerec665152006-02-26 23:36:02 +0000499 uint64_t NewBits = ~MVT::getIntVTBitMask(EVT) & DemandedMask;
Nate Begeman368e18d2006-02-16 21:11:51 +0000500
Chris Lattnerec665152006-02-26 23:36:02 +0000501 // If none of the extended bits are demanded, eliminate the sextinreg.
502 if (NewBits == 0)
503 return TLO.CombineTo(Op, Op.getOperand(0));
504
Nate Begeman368e18d2006-02-16 21:11:51 +0000505 uint64_t InSignBit = MVT::getIntVTSignBit(EVT);
506 int64_t InputDemandedBits = DemandedMask & MVT::getIntVTBitMask(EVT);
507
Chris Lattnerec665152006-02-26 23:36:02 +0000508 // Since the sign extended bits are demanded, we know that the sign
Nate Begeman368e18d2006-02-16 21:11:51 +0000509 // bit is demanded.
Chris Lattnerec665152006-02-26 23:36:02 +0000510 InputDemandedBits |= InSignBit;
Nate Begeman368e18d2006-02-16 21:11:51 +0000511
512 if (SimplifyDemandedBits(Op.getOperand(0), InputDemandedBits,
513 KnownZero, KnownOne, TLO, Depth+1))
514 return true;
515 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
516
517 // If the sign bit of the input is known set or clear, then we know the
518 // top bits of the result.
519
Chris Lattnerec665152006-02-26 23:36:02 +0000520 // If the input sign bit is known zero, convert this into a zero extension.
521 if (KnownZero & InSignBit)
522 return TLO.CombineTo(Op,
523 TLO.DAG.getZeroExtendInReg(Op.getOperand(0), EVT));
524
525 if (KnownOne & InSignBit) { // Input sign bit known set
Nate Begeman368e18d2006-02-16 21:11:51 +0000526 KnownOne |= NewBits;
527 KnownZero &= ~NewBits;
Chris Lattnerec665152006-02-26 23:36:02 +0000528 } else { // Input sign bit unknown
Nate Begeman368e18d2006-02-16 21:11:51 +0000529 KnownZero &= ~NewBits;
530 KnownOne &= ~NewBits;
531 }
532 break;
533 }
Chris Lattnerec665152006-02-26 23:36:02 +0000534 case ISD::CTTZ:
535 case ISD::CTLZ:
536 case ISD::CTPOP: {
537 MVT::ValueType VT = Op.getValueType();
538 unsigned LowBits = Log2_32(MVT::getSizeInBits(VT))+1;
539 KnownZero = ~((1ULL << LowBits)-1) & MVT::getIntVTBitMask(VT);
540 KnownOne = 0;
541 break;
542 }
543 case ISD::ZEXTLOAD: {
544 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(3))->getVT();
545 KnownZero |= ~MVT::getIntVTBitMask(VT) & DemandedMask;
546 break;
547 }
548 case ISD::ZERO_EXTEND: {
549 uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType());
550
551 // If none of the top bits are demanded, convert this into an any_extend.
552 uint64_t NewBits = (~InMask) & DemandedMask;
553 if (NewBits == 0)
554 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ANY_EXTEND,
555 Op.getValueType(),
556 Op.getOperand(0)));
557
558 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & InMask,
559 KnownZero, KnownOne, TLO, Depth+1))
560 return true;
561 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
562 KnownZero |= NewBits;
563 break;
564 }
565 case ISD::SIGN_EXTEND: {
566 MVT::ValueType InVT = Op.getOperand(0).getValueType();
567 uint64_t InMask = MVT::getIntVTBitMask(InVT);
568 uint64_t InSignBit = MVT::getIntVTSignBit(InVT);
569 uint64_t NewBits = (~InMask) & DemandedMask;
570
571 // If none of the top bits are demanded, convert this into an any_extend.
572 if (NewBits == 0)
573 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ANY_EXTEND,Op.getValueType(),
574 Op.getOperand(0)));
575
576 // Since some of the sign extended bits are demanded, we know that the sign
577 // bit is demanded.
578 uint64_t InDemandedBits = DemandedMask & InMask;
579 InDemandedBits |= InSignBit;
580
581 if (SimplifyDemandedBits(Op.getOperand(0), InDemandedBits, KnownZero,
582 KnownOne, TLO, Depth+1))
583 return true;
584
585 // If the sign bit is known zero, convert this to a zero extend.
586 if (KnownZero & InSignBit)
587 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ZERO_EXTEND,
588 Op.getValueType(),
589 Op.getOperand(0)));
590
591 // If the sign bit is known one, the top bits match.
592 if (KnownOne & InSignBit) {
593 KnownOne |= NewBits;
594 KnownZero &= ~NewBits;
595 } else { // Otherwise, top bits aren't known.
596 KnownOne &= ~NewBits;
597 KnownZero &= ~NewBits;
598 }
599 break;
600 }
601 case ISD::ANY_EXTEND: {
602 uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType());
603 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & InMask,
604 KnownZero, KnownOne, TLO, Depth+1))
605 return true;
606 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
607 break;
608 }
609 case ISD::AssertZext: {
610 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
611 uint64_t InMask = MVT::getIntVTBitMask(VT);
612 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & InMask,
613 KnownZero, KnownOne, TLO, Depth+1))
614 return true;
615 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
616 KnownZero |= ~InMask & DemandedMask;
617 break;
618 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000619 case ISD::ADD:
Chris Lattnera6bc5a42006-02-27 01:00:42 +0000620 case ISD::SUB:
621 // Just use ComputeMaskedBits to compute output bits, there are no
622 // simplifications that can be done here, and sub always demands all input
623 // bits.
624 ComputeMaskedBits(Op, DemandedMask, KnownZero, KnownOne, Depth);
625 break;
Nate Begeman368e18d2006-02-16 21:11:51 +0000626 }
Chris Lattnerec665152006-02-26 23:36:02 +0000627
628 // If we know the value of all of the demanded bits, return this as a
629 // constant.
630 if ((DemandedMask & (KnownZero|KnownOne)) == DemandedMask)
631 return TLO.CombineTo(Op, TLO.DAG.getConstant(KnownOne, Op.getValueType()));
632
Nate Begeman368e18d2006-02-16 21:11:51 +0000633 return false;
634}
635
636/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
637/// this predicate to simplify operations downstream. Mask is known to be zero
638/// for bits that V cannot have.
639bool TargetLowering::MaskedValueIsZero(SDOperand Op, uint64_t Mask,
640 unsigned Depth) const {
641 uint64_t KnownZero, KnownOne;
642 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
643 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
644 return (KnownZero & Mask) == Mask;
645}
646
647/// ComputeMaskedBits - Determine which of the bits specified in Mask are
648/// known to be either zero or one and return them in the KnownZero/KnownOne
649/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
650/// processing.
651void TargetLowering::ComputeMaskedBits(SDOperand Op, uint64_t Mask,
652 uint64_t &KnownZero, uint64_t &KnownOne,
653 unsigned Depth) const {
654 KnownZero = KnownOne = 0; // Don't know anything.
655 if (Depth == 6 || Mask == 0)
656 return; // Limit search depth.
657
658 uint64_t KnownZero2, KnownOne2;
659
660 switch (Op.getOpcode()) {
661 case ISD::Constant:
662 // We know all of the bits for a constant!
663 KnownOne = cast<ConstantSDNode>(Op)->getValue() & Mask;
664 KnownZero = ~KnownOne & Mask;
665 return;
666 case ISD::AND:
667 // If either the LHS or the RHS are Zero, the result is zero.
668 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
669 Mask &= ~KnownZero;
670 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
671 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
672 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
673
674 // Output known-1 bits are only known if set in both the LHS & RHS.
675 KnownOne &= KnownOne2;
676 // Output known-0 are known to be clear if zero in either the LHS | RHS.
677 KnownZero |= KnownZero2;
678 return;
679 case ISD::OR:
680 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
681 Mask &= ~KnownOne;
682 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
683 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
684 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
685
686 // Output known-0 bits are only known if clear in both the LHS & RHS.
687 KnownZero &= KnownZero2;
688 // Output known-1 are known to be set if set in either the LHS | RHS.
689 KnownOne |= KnownOne2;
690 return;
691 case ISD::XOR: {
692 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
693 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
694 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
695 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
696
697 // Output known-0 bits are known if clear or set in both the LHS & RHS.
698 uint64_t KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
699 // Output known-1 are known to be set if set in only one of the LHS, RHS.
700 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
701 KnownZero = KnownZeroOut;
702 return;
703 }
704 case ISD::SELECT:
705 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
706 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
707 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
708 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
709
710 // Only known if known in both the LHS and RHS.
711 KnownOne &= KnownOne2;
712 KnownZero &= KnownZero2;
713 return;
714 case ISD::SELECT_CC:
715 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
716 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
717 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
718 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
719
720 // Only known if known in both the LHS and RHS.
721 KnownOne &= KnownOne2;
722 KnownZero &= KnownZero2;
723 return;
724 case ISD::SETCC:
725 // If we know the result of a setcc has the top bits zero, use this info.
726 if (getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult)
727 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
728 return;
729 case ISD::SHL:
730 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
731 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
732 Mask >>= SA->getValue();
733 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
734 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
735 KnownZero <<= SA->getValue();
736 KnownOne <<= SA->getValue();
737 KnownZero |= (1ULL << SA->getValue())-1; // low bits known zero.
738 }
Nate Begeman003a2722006-02-18 02:43:25 +0000739 return;
Nate Begeman368e18d2006-02-16 21:11:51 +0000740 case ISD::SRL:
741 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
742 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
743 uint64_t HighBits = (1ULL << SA->getValue())-1;
744 HighBits <<= MVT::getSizeInBits(Op.getValueType())-SA->getValue();
745 Mask <<= SA->getValue();
746 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Nate Begeman003a2722006-02-18 02:43:25 +0000747 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Nate Begeman368e18d2006-02-16 21:11:51 +0000748 KnownZero >>= SA->getValue();
749 KnownOne >>= SA->getValue();
750 KnownZero |= HighBits; // high bits known zero.
751 }
Nate Begeman003a2722006-02-18 02:43:25 +0000752 return;
Nate Begeman368e18d2006-02-16 21:11:51 +0000753 case ISD::SRA:
754 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
755 uint64_t HighBits = (1ULL << SA->getValue())-1;
756 HighBits <<= MVT::getSizeInBits(Op.getValueType())-SA->getValue();
757 Mask <<= SA->getValue();
758 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
759 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
760 KnownZero >>= SA->getValue();
761 KnownOne >>= SA->getValue();
762
763 // Handle the sign bits.
764 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(Op.getValueType())-1);
765 SignBit >>= SA->getValue(); // Adjust to where it is now in the mask.
766
767 if (KnownZero & SignBit) { // New bits are known zero.
768 KnownZero |= HighBits;
769 } else if (KnownOne & SignBit) { // New bits are known one.
770 KnownOne |= HighBits;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000771 }
772 }
Nate Begeman003a2722006-02-18 02:43:25 +0000773 return;
Chris Lattnerec665152006-02-26 23:36:02 +0000774 case ISD::SIGN_EXTEND_INREG: {
775 MVT::ValueType VT = Op.getValueType();
776 MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
777
778 // Sign extension. Compute the demanded bits in the result that are not
779 // present in the input.
780 uint64_t NewBits = ~MVT::getIntVTBitMask(EVT) & Mask;
781
782 uint64_t InSignBit = MVT::getIntVTSignBit(EVT);
783 int64_t InputDemandedBits = Mask & MVT::getIntVTBitMask(EVT);
784
785 // If the sign extended bits are demanded, we know that the sign
786 // bit is demanded.
787 if (NewBits)
788 InputDemandedBits |= InSignBit;
789
790 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
791 KnownZero, KnownOne, Depth+1);
792 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
793
794 // If the sign bit of the input is known set or clear, then we know the
795 // top bits of the result.
796 if (KnownZero & InSignBit) { // Input sign bit known clear
797 KnownZero |= NewBits;
798 KnownOne &= ~NewBits;
799 } else if (KnownOne & InSignBit) { // Input sign bit known set
800 KnownOne |= NewBits;
801 KnownZero &= ~NewBits;
802 } else { // Input sign bit unknown
803 KnownZero &= ~NewBits;
804 KnownOne &= ~NewBits;
805 }
806 return;
807 }
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000808 case ISD::CTTZ:
809 case ISD::CTLZ:
Nate Begeman368e18d2006-02-16 21:11:51 +0000810 case ISD::CTPOP: {
811 MVT::ValueType VT = Op.getValueType();
812 unsigned LowBits = Log2_32(MVT::getSizeInBits(VT))+1;
813 KnownZero = ~((1ULL << LowBits)-1) & MVT::getIntVTBitMask(VT);
814 KnownOne = 0;
815 return;
816 }
817 case ISD::ZEXTLOAD: {
Chris Lattnerec665152006-02-26 23:36:02 +0000818 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(3))->getVT();
819 KnownZero |= ~MVT::getIntVTBitMask(VT) & Mask;
Nate Begeman368e18d2006-02-16 21:11:51 +0000820 return;
821 }
822 case ISD::ZERO_EXTEND: {
Chris Lattnerec665152006-02-26 23:36:02 +0000823 uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType());
824 uint64_t NewBits = (~InMask) & Mask;
825 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
826 KnownOne, Depth+1);
827 KnownZero |= NewBits & Mask;
828 KnownOne &= ~NewBits;
829 return;
830 }
831 case ISD::SIGN_EXTEND: {
832 MVT::ValueType InVT = Op.getOperand(0).getValueType();
833 unsigned InBits = MVT::getSizeInBits(InVT);
834 uint64_t InMask = MVT::getIntVTBitMask(InVT);
835 uint64_t InSignBit = 1ULL << (InBits-1);
836 uint64_t NewBits = (~InMask) & Mask;
837 uint64_t InDemandedBits = Mask & InMask;
838
839 // If any of the sign extended bits are demanded, we know that the sign
840 // bit is demanded.
841 if (NewBits & Mask)
842 InDemandedBits |= InSignBit;
843
844 ComputeMaskedBits(Op.getOperand(0), InDemandedBits, KnownZero,
845 KnownOne, Depth+1);
846 // If the sign bit is known zero or one, the top bits match.
847 if (KnownZero & InSignBit) {
848 KnownZero |= NewBits;
849 KnownOne &= ~NewBits;
850 } else if (KnownOne & InSignBit) {
851 KnownOne |= NewBits;
852 KnownZero &= ~NewBits;
853 } else { // Otherwise, top bits aren't known.
854 KnownOne &= ~NewBits;
855 KnownZero &= ~NewBits;
856 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000857 return;
858 }
859 case ISD::ANY_EXTEND: {
Chris Lattnerec665152006-02-26 23:36:02 +0000860 MVT::ValueType VT = Op.getOperand(0).getValueType();
861 ComputeMaskedBits(Op.getOperand(0), Mask & MVT::getIntVTBitMask(VT),
862 KnownZero, KnownOne, Depth+1);
Nate Begeman368e18d2006-02-16 21:11:51 +0000863 return;
864 }
865 case ISD::AssertZext: {
Chris Lattnerec665152006-02-26 23:36:02 +0000866 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
867 uint64_t InMask = MVT::getIntVTBitMask(VT);
868 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
869 KnownOne, Depth+1);
870 KnownZero |= (~InMask) & Mask;
Nate Begeman368e18d2006-02-16 21:11:51 +0000871 return;
872 }
873 case ISD::ADD: {
874 // If either the LHS or the RHS are Zero, the result is zero.
875 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
876 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
877 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
878 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
879
880 // Output known-0 bits are known if clear or set in both the low clear bits
Chris Lattnerb6b17ff2006-03-13 06:42:16 +0000881 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
882 // low 3 bits clear.
Nate Begeman368e18d2006-02-16 21:11:51 +0000883 uint64_t KnownZeroOut = std::min(CountTrailingZeros_64(~KnownZero),
884 CountTrailingZeros_64(~KnownZero2));
885
886 KnownZero = (1ULL << KnownZeroOut) - 1;
887 KnownOne = 0;
888 return;
889 }
Chris Lattnera6bc5a42006-02-27 01:00:42 +0000890 case ISD::SUB: {
891 ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0));
892 if (!CLHS) return;
893
Nate Begeman368e18d2006-02-16 21:11:51 +0000894 // We know that the top bits of C-X are clear if X contains less bits
895 // than C (i.e. no wrap-around can happen). For example, 20-X is
Chris Lattnera6bc5a42006-02-27 01:00:42 +0000896 // positive if we can prove that X is >= 0 and < 16.
897 MVT::ValueType VT = CLHS->getValueType(0);
898 if ((CLHS->getValue() & MVT::getIntVTSignBit(VT)) == 0) { // sign bit clear
899 unsigned NLZ = CountLeadingZeros_64(CLHS->getValue()+1);
900 uint64_t MaskV = (1ULL << (63-NLZ))-1; // NLZ can't be 64 with no sign bit
901 MaskV = ~MaskV & MVT::getIntVTBitMask(VT);
902 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero, KnownOne, Depth+1);
903
904 // If all of the MaskV bits are known to be zero, then we know the output
905 // top bits are zero, because we now know that the output is from [0-C].
906 if ((KnownZero & MaskV) == MaskV) {
907 unsigned NLZ2 = CountLeadingZeros_64(CLHS->getValue());
908 KnownZero = ~((1ULL << (64-NLZ2))-1) & Mask; // Top bits known zero.
909 KnownOne = 0; // No one bits known.
910 } else {
911 KnownOne = KnownOne = 0; // Otherwise, nothing known.
912 }
913 }
Nate Begeman003a2722006-02-18 02:43:25 +0000914 return;
Chris Lattnera6bc5a42006-02-27 01:00:42 +0000915 }
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000916 default:
917 // Allow the target to implement this method for its nodes.
918 if (Op.getOpcode() >= ISD::BUILTIN_OP_END)
Nate Begeman368e18d2006-02-16 21:11:51 +0000919 computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne);
Nate Begeman003a2722006-02-18 02:43:25 +0000920 return;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000921 }
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000922}
923
Nate Begeman368e18d2006-02-16 21:11:51 +0000924/// computeMaskedBitsForTargetNode - Determine which of the bits specified
925/// in Mask are known to be either zero or one and return them in the
926/// KnownZero/KnownOne bitsets.
927void TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
928 uint64_t Mask,
929 uint64_t &KnownZero,
930 uint64_t &KnownOne,
931 unsigned Depth) const {
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000932 assert(Op.getOpcode() >= ISD::BUILTIN_OP_END &&
933 "Should use MaskedValueIsZero if you don't know whether Op"
934 " is a target node!");
Nate Begeman368e18d2006-02-16 21:11:51 +0000935 KnownZero = 0;
936 KnownOne = 0;
Evan Cheng3a03ebb2005-12-21 23:05:39 +0000937}
Chris Lattner4ccb0702006-01-26 20:37:03 +0000938
Chris Lattner00ffed02006-03-01 04:52:55 +0000939SDOperand TargetLowering::
940PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
941 // Default implementation: no optimization.
942 return SDOperand();
943}
944
Chris Lattnereb8146b2006-02-04 02:13:02 +0000945//===----------------------------------------------------------------------===//
946// Inline Assembler Implementation Methods
947//===----------------------------------------------------------------------===//
948
949TargetLowering::ConstraintType
950TargetLowering::getConstraintType(char ConstraintLetter) const {
951 // FIXME: lots more standard ones to handle.
952 switch (ConstraintLetter) {
953 default: return C_Unknown;
954 case 'r': return C_RegisterClass;
Chris Lattner2b7401e2006-02-24 01:10:46 +0000955 case 'm': // memory
956 case 'o': // offsetable
957 case 'V': // not offsetable
958 return C_Memory;
Chris Lattnereb8146b2006-02-04 02:13:02 +0000959 case 'i': // Simple Integer or Relocatable Constant
960 case 'n': // Simple Integer
961 case 's': // Relocatable Constant
962 case 'I': // Target registers.
963 case 'J':
964 case 'K':
965 case 'L':
966 case 'M':
967 case 'N':
968 case 'O':
Chris Lattner2b7401e2006-02-24 01:10:46 +0000969 case 'P':
970 return C_Other;
Chris Lattnereb8146b2006-02-04 02:13:02 +0000971 }
972}
973
974bool TargetLowering::isOperandValidForConstraint(SDOperand Op,
975 char ConstraintLetter) {
976 switch (ConstraintLetter) {
977 default: return false;
978 case 'i': // Simple Integer or Relocatable Constant
979 case 'n': // Simple Integer
980 case 's': // Relocatable Constant
981 return true; // FIXME: not right.
982 }
983}
984
985
Chris Lattner4ccb0702006-01-26 20:37:03 +0000986std::vector<unsigned> TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +0000987getRegClassForInlineAsmConstraint(const std::string &Constraint,
988 MVT::ValueType VT) const {
989 return std::vector<unsigned>();
990}
991
992
993std::pair<unsigned, const TargetRegisterClass*> TargetLowering::
Chris Lattner4217ca8dc2006-02-21 23:11:00 +0000994getRegForInlineAsmConstraint(const std::string &Constraint,
995 MVT::ValueType VT) const {
Chris Lattner1efa40f2006-02-22 00:56:39 +0000996 if (Constraint[0] != '{')
997 return std::pair<unsigned, const TargetRegisterClass*>(0, 0);
Chris Lattnera55079a2006-02-01 01:29:47 +0000998 assert(*(Constraint.end()-1) == '}' && "Not a brace enclosed constraint?");
999
1000 // Remove the braces from around the name.
1001 std::string RegName(Constraint.begin()+1, Constraint.end()-1);
Chris Lattner1efa40f2006-02-22 00:56:39 +00001002
1003 // Figure out which register class contains this reg.
Chris Lattner4ccb0702006-01-26 20:37:03 +00001004 const MRegisterInfo *RI = TM.getRegisterInfo();
Chris Lattner1efa40f2006-02-22 00:56:39 +00001005 for (MRegisterInfo::regclass_iterator RCI = RI->regclass_begin(),
1006 E = RI->regclass_end(); RCI != E; ++RCI) {
1007 const TargetRegisterClass *RC = *RCI;
Chris Lattnerb3befd42006-02-22 23:00:51 +00001008
1009 // If none of the the value types for this register class are valid, we
1010 // can't use it. For example, 64-bit reg classes on 32-bit targets.
1011 bool isLegal = false;
1012 for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
1013 I != E; ++I) {
1014 if (isTypeLegal(*I)) {
1015 isLegal = true;
1016 break;
1017 }
1018 }
1019
1020 if (!isLegal) continue;
1021
Chris Lattner1efa40f2006-02-22 00:56:39 +00001022 for (TargetRegisterClass::iterator I = RC->begin(), E = RC->end();
1023 I != E; ++I) {
Chris Lattnerb3befd42006-02-22 23:00:51 +00001024 if (StringsEqualNoCase(RegName, RI->get(*I).Name))
Chris Lattner1efa40f2006-02-22 00:56:39 +00001025 return std::make_pair(*I, RC);
Chris Lattner1efa40f2006-02-22 00:56:39 +00001026 }
Chris Lattner4ccb0702006-01-26 20:37:03 +00001027 }
Chris Lattnera55079a2006-02-01 01:29:47 +00001028
Chris Lattner1efa40f2006-02-22 00:56:39 +00001029 return std::pair<unsigned, const TargetRegisterClass*>(0, 0);
Chris Lattner4ccb0702006-01-26 20:37:03 +00001030}
Evan Cheng30b37b52006-03-13 23:18:16 +00001031
1032//===----------------------------------------------------------------------===//
1033// Loop Strength Reduction hooks
1034//===----------------------------------------------------------------------===//
1035
1036/// isLegalAddressImmediate - Return true if the integer value or
1037/// GlobalValue can be used as the offset of the target addressing mode.
1038bool TargetLowering::isLegalAddressImmediate(int64_t V) const {
1039 return false;
1040}
1041bool TargetLowering::isLegalAddressImmediate(GlobalValue *GV) const {
1042 return false;
1043}