blob: c4cb8c2dc996efc63f77ee73583b2a42b6c603af [file] [log] [blame]
Chris Lattner310968c2005-01-07 07:44:53 +00001//===-- TargetLowering.cpp - Implement the TargetLowering class -----------===//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
Chris Lattner310968c2005-01-07 07:44:53 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanf976c852005-04-21 22:55:34 +00007//
Chris Lattner310968c2005-01-07 07:44:53 +00008//===----------------------------------------------------------------------===//
9//
10// This implements the TargetLowering class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Target/TargetLowering.h"
15#include "llvm/Target/TargetMachine.h"
Chris Lattner4ccb0702006-01-26 20:37:03 +000016#include "llvm/Target/MRegisterInfo.h"
Chris Lattner310968c2005-01-07 07:44:53 +000017#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner4ccb0702006-01-26 20:37:03 +000018#include "llvm/ADT/StringExtras.h"
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +000019#include "llvm/Support/MathExtras.h"
Chris Lattner310968c2005-01-07 07:44:53 +000020using namespace llvm;
21
22TargetLowering::TargetLowering(TargetMachine &tm)
Chris Lattner3e6e8cc2006-01-29 08:41:12 +000023 : TM(tm), TD(TM.getTargetData()) {
Evan Cheng33143dc2006-03-03 06:58:59 +000024 assert(ISD::BUILTIN_OP_END <= 156 &&
Chris Lattner310968c2005-01-07 07:44:53 +000025 "Fixed size array in TargetLowering is not large enough!");
Chris Lattnercba82f92005-01-16 07:28:11 +000026 // All operations default to being supported.
27 memset(OpActions, 0, sizeof(OpActions));
Chris Lattner310968c2005-01-07 07:44:53 +000028
29 IsLittleEndian = TD.isLittleEndian();
Chris Lattner714b69d2005-01-16 23:59:48 +000030 ShiftAmountTy = SetCCResultTy = PointerTy = getValueType(TD.getIntPtrType());
Chris Lattnerd6e49672005-01-19 03:36:14 +000031 ShiftAmtHandling = Undefined;
Chris Lattner310968c2005-01-07 07:44:53 +000032 memset(RegClassForVT, 0,MVT::LAST_VALUETYPE*sizeof(TargetRegisterClass*));
Chris Lattner00ffed02006-03-01 04:52:55 +000033 memset(TargetDAGCombineArray, 0,
34 sizeof(TargetDAGCombineArray)/sizeof(TargetDAGCombineArray[0]));
Evan Chenga03a5dc2006-02-14 08:38:30 +000035 maxStoresPerMemset = maxStoresPerMemcpy = maxStoresPerMemmove = 8;
Reid Spencer0f9beca2005-08-27 19:09:02 +000036 allowUnalignedMemoryAccesses = false;
Chris Lattner8e6be8b2005-09-27 22:13:56 +000037 UseUnderscoreSetJmpLongJmp = false;
Nate Begeman405e3ec2005-10-21 00:02:42 +000038 IntDivIsCheap = false;
39 Pow2DivIsCheap = false;
Chris Lattneree4a7652006-01-25 18:57:15 +000040 StackPointerRegisterToSaveRestore = 0;
Evan Cheng0577a222006-01-25 18:52:42 +000041 SchedPreferenceInfo = SchedulingForLatency;
Chris Lattner310968c2005-01-07 07:44:53 +000042}
43
Chris Lattnercba82f92005-01-16 07:28:11 +000044TargetLowering::~TargetLowering() {}
45
Chris Lattnerbb97d812005-01-16 01:10:58 +000046/// setValueTypeAction - Set the action for a particular value type. This
47/// assumes an action has not already been set for this value type.
Chris Lattnercba82f92005-01-16 07:28:11 +000048static void SetValueTypeAction(MVT::ValueType VT,
49 TargetLowering::LegalizeAction Action,
Chris Lattnerbb97d812005-01-16 01:10:58 +000050 TargetLowering &TLI,
51 MVT::ValueType *TransformToType,
Chris Lattner3e6e8cc2006-01-29 08:41:12 +000052 TargetLowering::ValueTypeActionImpl &ValueTypeActions) {
53 ValueTypeActions.setTypeAction(VT, Action);
Chris Lattnercba82f92005-01-16 07:28:11 +000054 if (Action == TargetLowering::Promote) {
Chris Lattnerbb97d812005-01-16 01:10:58 +000055 MVT::ValueType PromoteTo;
56 if (VT == MVT::f32)
57 PromoteTo = MVT::f64;
58 else {
59 unsigned LargerReg = VT+1;
Chris Lattner9ed62c12005-08-24 16:34:12 +000060 while (!TLI.isTypeLegal((MVT::ValueType)LargerReg)) {
Chris Lattnerbb97d812005-01-16 01:10:58 +000061 ++LargerReg;
62 assert(MVT::isInteger((MVT::ValueType)LargerReg) &&
63 "Nothing to promote to??");
64 }
65 PromoteTo = (MVT::ValueType)LargerReg;
66 }
67
68 assert(MVT::isInteger(VT) == MVT::isInteger(PromoteTo) &&
69 MVT::isFloatingPoint(VT) == MVT::isFloatingPoint(PromoteTo) &&
70 "Can only promote from int->int or fp->fp!");
71 assert(VT < PromoteTo && "Must promote to a larger type!");
72 TransformToType[VT] = PromoteTo;
Chris Lattnercba82f92005-01-16 07:28:11 +000073 } else if (Action == TargetLowering::Expand) {
Nate Begeman4ef3b812005-11-22 01:29:36 +000074 assert((VT == MVT::Vector || MVT::isInteger(VT)) && VT > MVT::i8 &&
Chris Lattnerbb97d812005-01-16 01:10:58 +000075 "Cannot expand this type: target must support SOME integer reg!");
76 // Expand to the next smaller integer type!
77 TransformToType[VT] = (MVT::ValueType)(VT-1);
78 }
79}
80
81
Chris Lattner310968c2005-01-07 07:44:53 +000082/// computeRegisterProperties - Once all of the register classes are added,
83/// this allows us to compute derived properties we expose.
84void TargetLowering::computeRegisterProperties() {
Nate Begeman6a648612005-11-29 05:45:29 +000085 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattnerbb97d812005-01-16 01:10:58 +000086 "Too many value types for ValueTypeActions to hold!");
87
Chris Lattner310968c2005-01-07 07:44:53 +000088 // Everything defaults to one.
89 for (unsigned i = 0; i != MVT::LAST_VALUETYPE; ++i)
90 NumElementsForVT[i] = 1;
Misha Brukmanf976c852005-04-21 22:55:34 +000091
Chris Lattner310968c2005-01-07 07:44:53 +000092 // Find the largest integer register class.
93 unsigned LargestIntReg = MVT::i128;
94 for (; RegClassForVT[LargestIntReg] == 0; --LargestIntReg)
95 assert(LargestIntReg != MVT::i1 && "No integer registers defined!");
96
97 // Every integer value type larger than this largest register takes twice as
98 // many registers to represent as the previous ValueType.
99 unsigned ExpandedReg = LargestIntReg; ++LargestIntReg;
100 for (++ExpandedReg; MVT::isInteger((MVT::ValueType)ExpandedReg);++ExpandedReg)
101 NumElementsForVT[ExpandedReg] = 2*NumElementsForVT[ExpandedReg-1];
Chris Lattner310968c2005-01-07 07:44:53 +0000102
Chris Lattnerbb97d812005-01-16 01:10:58 +0000103 // Inspect all of the ValueType's possible, deciding how to process them.
104 for (unsigned IntReg = MVT::i1; IntReg <= MVT::i128; ++IntReg)
105 // If we are expanding this type, expand it!
106 if (getNumElements((MVT::ValueType)IntReg) != 1)
Chris Lattnercba82f92005-01-16 07:28:11 +0000107 SetValueTypeAction((MVT::ValueType)IntReg, Expand, *this, TransformToType,
Chris Lattnerbb97d812005-01-16 01:10:58 +0000108 ValueTypeActions);
Chris Lattner9ed62c12005-08-24 16:34:12 +0000109 else if (!isTypeLegal((MVT::ValueType)IntReg))
Chris Lattnerbb97d812005-01-16 01:10:58 +0000110 // Otherwise, if we don't have native support, we must promote to a
111 // larger type.
Chris Lattnercba82f92005-01-16 07:28:11 +0000112 SetValueTypeAction((MVT::ValueType)IntReg, Promote, *this,
113 TransformToType, ValueTypeActions);
Chris Lattnercfdfe4c2005-01-16 01:20:18 +0000114 else
115 TransformToType[(MVT::ValueType)IntReg] = (MVT::ValueType)IntReg;
Misha Brukmanf976c852005-04-21 22:55:34 +0000116
Chris Lattnerbb97d812005-01-16 01:10:58 +0000117 // If the target does not have native support for F32, promote it to F64.
Chris Lattner9ed62c12005-08-24 16:34:12 +0000118 if (!isTypeLegal(MVT::f32))
Chris Lattnercba82f92005-01-16 07:28:11 +0000119 SetValueTypeAction(MVT::f32, Promote, *this,
120 TransformToType, ValueTypeActions);
Chris Lattnercfdfe4c2005-01-16 01:20:18 +0000121 else
122 TransformToType[MVT::f32] = MVT::f32;
Nate Begeman4ef3b812005-11-22 01:29:36 +0000123
124 // Set MVT::Vector to always be Expanded
125 SetValueTypeAction(MVT::Vector, Expand, *this, TransformToType,
126 ValueTypeActions);
Chris Lattner3a5935842006-03-16 19:50:01 +0000127
128 // Loop over all of the legal vector value types, specifying an identity type
129 // transformation.
130 for (unsigned i = MVT::FIRST_VECTOR_VALUETYPE;
131 i != MVT::LAST_VECTOR_VALUETYPE; ++i) {
132 if (isTypeLegal((MVT::ValueType)i))
133 TransformToType[i] = (MVT::ValueType)i;
134 }
Chris Lattnercfdfe4c2005-01-16 01:20:18 +0000135
Chris Lattner9ed62c12005-08-24 16:34:12 +0000136 assert(isTypeLegal(MVT::f64) && "Target does not support FP?");
Chris Lattnercfdfe4c2005-01-16 01:20:18 +0000137 TransformToType[MVT::f64] = MVT::f64;
Chris Lattnerbb97d812005-01-16 01:10:58 +0000138}
Chris Lattnercba82f92005-01-16 07:28:11 +0000139
Evan Cheng72261582005-12-20 06:22:03 +0000140const char *TargetLowering::getTargetNodeName(unsigned Opcode) const {
141 return NULL;
142}
Evan Cheng3a03ebb2005-12-21 23:05:39 +0000143
Chris Lattnereb8146b2006-02-04 02:13:02 +0000144//===----------------------------------------------------------------------===//
145// Optimization Methods
146//===----------------------------------------------------------------------===//
147
Nate Begeman368e18d2006-02-16 21:11:51 +0000148/// ShrinkDemandedConstant - Check to see if the specified operand of the
149/// specified instruction is a constant integer. If so, check to see if there
150/// are any bits set in the constant that are not demanded. If so, shrink the
151/// constant and return true.
152bool TargetLowering::TargetLoweringOpt::ShrinkDemandedConstant(SDOperand Op,
153 uint64_t Demanded) {
Chris Lattnerec665152006-02-26 23:36:02 +0000154 // FIXME: ISD::SELECT, ISD::SELECT_CC
Nate Begeman368e18d2006-02-16 21:11:51 +0000155 switch(Op.getOpcode()) {
156 default: break;
Nate Begemande996292006-02-03 22:24:05 +0000157 case ISD::AND:
Nate Begeman368e18d2006-02-16 21:11:51 +0000158 case ISD::OR:
159 case ISD::XOR:
160 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
161 if ((~Demanded & C->getValue()) != 0) {
162 MVT::ValueType VT = Op.getValueType();
163 SDOperand New = DAG.getNode(Op.getOpcode(), VT, Op.getOperand(0),
164 DAG.getConstant(Demanded & C->getValue(),
165 VT));
166 return CombineTo(Op, New);
Nate Begemande996292006-02-03 22:24:05 +0000167 }
Nate Begemande996292006-02-03 22:24:05 +0000168 break;
169 }
170 return false;
171}
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000172
Nate Begeman368e18d2006-02-16 21:11:51 +0000173/// SimplifyDemandedBits - Look at Op. At this point, we know that only the
174/// DemandedMask bits of the result of Op are ever used downstream. If we can
175/// use this information to simplify Op, create a new simplified DAG node and
176/// return true, returning the original and new nodes in Old and New. Otherwise,
177/// analyze the expression and return a mask of KnownOne and KnownZero bits for
178/// the expression (used to simplify the caller). The KnownZero/One bits may
179/// only be accurate for those bits in the DemandedMask.
180bool TargetLowering::SimplifyDemandedBits(SDOperand Op, uint64_t DemandedMask,
181 uint64_t &KnownZero,
182 uint64_t &KnownOne,
183 TargetLoweringOpt &TLO,
184 unsigned Depth) const {
185 KnownZero = KnownOne = 0; // Don't know anything.
186 // Other users may use these bits.
187 if (!Op.Val->hasOneUse()) {
188 if (Depth != 0) {
189 // If not at the root, Just compute the KnownZero/KnownOne bits to
190 // simplify things downstream.
191 ComputeMaskedBits(Op, DemandedMask, KnownZero, KnownOne, Depth);
192 return false;
193 }
194 // If this is the root being simplified, allow it to have multiple uses,
195 // just set the DemandedMask to all bits.
196 DemandedMask = MVT::getIntVTBitMask(Op.getValueType());
197 } else if (DemandedMask == 0) {
198 // Not demanding any bits from Op.
199 if (Op.getOpcode() != ISD::UNDEF)
200 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::UNDEF, Op.getValueType()));
201 return false;
202 } else if (Depth == 6) { // Limit search depth.
203 return false;
204 }
205
206 uint64_t KnownZero2, KnownOne2, KnownZeroOut, KnownOneOut;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000207 switch (Op.getOpcode()) {
208 case ISD::Constant:
Nate Begeman368e18d2006-02-16 21:11:51 +0000209 // We know all of the bits for a constant!
210 KnownOne = cast<ConstantSDNode>(Op)->getValue() & DemandedMask;
211 KnownZero = ~KnownOne & DemandedMask;
Chris Lattnerec665152006-02-26 23:36:02 +0000212 return false; // Don't fall through, will infinitely loop.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000213 case ISD::AND:
Chris Lattner81cd3552006-02-27 00:36:27 +0000214 // If the RHS is a constant, check to see if the LHS would be zero without
215 // using the bits from the RHS. Below, we use knowledge about the RHS to
216 // simplify the LHS, here we're using information from the LHS to simplify
217 // the RHS.
218 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
219 uint64_t LHSZero, LHSOne;
220 ComputeMaskedBits(Op.getOperand(0), DemandedMask,
221 LHSZero, LHSOne, Depth+1);
222 // If the LHS already has zeros where RHSC does, this and is dead.
223 if ((LHSZero & DemandedMask) == (~RHSC->getValue() & DemandedMask))
224 return TLO.CombineTo(Op, Op.getOperand(0));
225 // If any of the set bits in the RHS are known zero on the LHS, shrink
226 // the constant.
227 if (TLO.ShrinkDemandedConstant(Op, ~LHSZero & DemandedMask))
228 return true;
229 }
230
Nate Begeman368e18d2006-02-16 21:11:51 +0000231 if (SimplifyDemandedBits(Op.getOperand(1), DemandedMask, KnownZero,
232 KnownOne, TLO, Depth+1))
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000233 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000234 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Nate Begeman368e18d2006-02-16 21:11:51 +0000235 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & ~KnownZero,
236 KnownZero2, KnownOne2, TLO, Depth+1))
237 return true;
238 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
239
240 // If all of the demanded bits are known one on one side, return the other.
241 // These bits cannot contribute to the result of the 'and'.
242 if ((DemandedMask & ~KnownZero2 & KnownOne)==(DemandedMask & ~KnownZero2))
243 return TLO.CombineTo(Op, Op.getOperand(0));
244 if ((DemandedMask & ~KnownZero & KnownOne2)==(DemandedMask & ~KnownZero))
245 return TLO.CombineTo(Op, Op.getOperand(1));
246 // If all of the demanded bits in the inputs are known zeros, return zero.
247 if ((DemandedMask & (KnownZero|KnownZero2)) == DemandedMask)
248 return TLO.CombineTo(Op, TLO.DAG.getConstant(0, Op.getValueType()));
249 // If the RHS is a constant, see if we can simplify it.
250 if (TLO.ShrinkDemandedConstant(Op, DemandedMask & ~KnownZero2))
251 return true;
Chris Lattner5f0c6582006-02-27 00:22:28 +0000252
Nate Begeman368e18d2006-02-16 21:11:51 +0000253 // Output known-1 bits are only known if set in both the LHS & RHS.
254 KnownOne &= KnownOne2;
255 // Output known-0 are known to be clear if zero in either the LHS | RHS.
256 KnownZero |= KnownZero2;
257 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000258 case ISD::OR:
Nate Begeman368e18d2006-02-16 21:11:51 +0000259 if (SimplifyDemandedBits(Op.getOperand(1), DemandedMask, KnownZero,
260 KnownOne, TLO, Depth+1))
261 return true;
262 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
263 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & ~KnownOne,
264 KnownZero2, KnownOne2, TLO, Depth+1))
265 return true;
266 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
267
268 // If all of the demanded bits are known zero on one side, return the other.
269 // These bits cannot contribute to the result of the 'or'.
Jeff Cohen5755b172006-02-17 02:12:18 +0000270 if ((DemandedMask & ~KnownOne2 & KnownZero) == (DemandedMask & ~KnownOne2))
Nate Begeman368e18d2006-02-16 21:11:51 +0000271 return TLO.CombineTo(Op, Op.getOperand(0));
Jeff Cohen5755b172006-02-17 02:12:18 +0000272 if ((DemandedMask & ~KnownOne & KnownZero2) == (DemandedMask & ~KnownOne))
Nate Begeman368e18d2006-02-16 21:11:51 +0000273 return TLO.CombineTo(Op, Op.getOperand(1));
274 // If all of the potentially set bits on one side are known to be set on
275 // the other side, just use the 'other' side.
276 if ((DemandedMask & (~KnownZero) & KnownOne2) ==
277 (DemandedMask & (~KnownZero)))
278 return TLO.CombineTo(Op, Op.getOperand(0));
279 if ((DemandedMask & (~KnownZero2) & KnownOne) ==
280 (DemandedMask & (~KnownZero2)))
281 return TLO.CombineTo(Op, Op.getOperand(1));
282 // If the RHS is a constant, see if we can simplify it.
283 if (TLO.ShrinkDemandedConstant(Op, DemandedMask))
284 return true;
285
286 // Output known-0 bits are only known if clear in both the LHS & RHS.
287 KnownZero &= KnownZero2;
288 // Output known-1 are known to be set if set in either the LHS | RHS.
289 KnownOne |= KnownOne2;
290 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000291 case ISD::XOR:
Nate Begeman368e18d2006-02-16 21:11:51 +0000292 if (SimplifyDemandedBits(Op.getOperand(1), DemandedMask, KnownZero,
293 KnownOne, TLO, Depth+1))
294 return true;
295 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
296 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask, KnownZero2,
297 KnownOne2, TLO, Depth+1))
298 return true;
299 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
300
301 // If all of the demanded bits are known zero on one side, return the other.
302 // These bits cannot contribute to the result of the 'xor'.
303 if ((DemandedMask & KnownZero) == DemandedMask)
304 return TLO.CombineTo(Op, Op.getOperand(0));
305 if ((DemandedMask & KnownZero2) == DemandedMask)
306 return TLO.CombineTo(Op, Op.getOperand(1));
307
308 // Output known-0 bits are known if clear or set in both the LHS & RHS.
309 KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
310 // Output known-1 are known to be set if set in only one of the LHS, RHS.
311 KnownOneOut = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
312
313 // If all of the unknown bits are known to be zero on one side or the other
314 // (but not both) turn this into an *inclusive* or.
315 // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
316 if (uint64_t UnknownBits = DemandedMask & ~(KnownZeroOut|KnownOneOut))
317 if ((UnknownBits & (KnownZero|KnownZero2)) == UnknownBits)
318 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::OR, Op.getValueType(),
319 Op.getOperand(0),
320 Op.getOperand(1)));
321 // If all of the demanded bits on one side are known, and all of the set
322 // bits on that side are also known to be set on the other side, turn this
323 // into an AND, as we know the bits will be cleared.
324 // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
325 if ((DemandedMask & (KnownZero|KnownOne)) == DemandedMask) { // all known
326 if ((KnownOne & KnownOne2) == KnownOne) {
327 MVT::ValueType VT = Op.getValueType();
328 SDOperand ANDC = TLO.DAG.getConstant(~KnownOne & DemandedMask, VT);
329 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::AND, VT, Op.getOperand(0),
330 ANDC));
331 }
332 }
333
334 // If the RHS is a constant, see if we can simplify it.
335 // FIXME: for XOR, we prefer to force bits to 1 if they will make a -1.
336 if (TLO.ShrinkDemandedConstant(Op, DemandedMask))
337 return true;
338
339 KnownZero = KnownZeroOut;
340 KnownOne = KnownOneOut;
341 break;
342 case ISD::SETCC:
343 // If we know the result of a setcc has the top bits zero, use this info.
344 if (getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult)
345 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
346 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000347 case ISD::SELECT:
Nate Begeman368e18d2006-02-16 21:11:51 +0000348 if (SimplifyDemandedBits(Op.getOperand(2), DemandedMask, KnownZero,
349 KnownOne, TLO, Depth+1))
350 return true;
351 if (SimplifyDemandedBits(Op.getOperand(1), DemandedMask, KnownZero2,
352 KnownOne2, TLO, Depth+1))
353 return true;
354 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
355 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
356
357 // If the operands are constants, see if we can simplify them.
358 if (TLO.ShrinkDemandedConstant(Op, DemandedMask))
359 return true;
360
361 // Only known if known in both the LHS and RHS.
362 KnownOne &= KnownOne2;
363 KnownZero &= KnownZero2;
364 break;
Chris Lattnerec665152006-02-26 23:36:02 +0000365 case ISD::SELECT_CC:
366 if (SimplifyDemandedBits(Op.getOperand(3), DemandedMask, KnownZero,
367 KnownOne, TLO, Depth+1))
368 return true;
369 if (SimplifyDemandedBits(Op.getOperand(2), DemandedMask, KnownZero2,
370 KnownOne2, TLO, Depth+1))
371 return true;
372 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
373 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
374
375 // If the operands are constants, see if we can simplify them.
376 if (TLO.ShrinkDemandedConstant(Op, DemandedMask))
377 return true;
378
379 // Only known if known in both the LHS and RHS.
380 KnownOne &= KnownOne2;
381 KnownZero &= KnownZero2;
382 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000383 case ISD::SHL:
Nate Begeman368e18d2006-02-16 21:11:51 +0000384 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
385 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask >> SA->getValue(),
386 KnownZero, KnownOne, TLO, Depth+1))
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000387 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000388 KnownZero <<= SA->getValue();
389 KnownOne <<= SA->getValue();
390 KnownZero |= (1ULL << SA->getValue())-1; // low bits known zero.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000391 }
392 break;
Nate Begeman368e18d2006-02-16 21:11:51 +0000393 case ISD::SRL:
394 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
395 MVT::ValueType VT = Op.getValueType();
396 unsigned ShAmt = SA->getValue();
397
398 // Compute the new bits that are at the top now.
399 uint64_t HighBits = (1ULL << ShAmt)-1;
400 HighBits <<= MVT::getSizeInBits(VT) - ShAmt;
401 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
402
403 if (SimplifyDemandedBits(Op.getOperand(0),
404 (DemandedMask << ShAmt) & TypeMask,
405 KnownZero, KnownOne, TLO, Depth+1))
406 return true;
407 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
408 KnownZero &= TypeMask;
409 KnownOne &= TypeMask;
410 KnownZero >>= ShAmt;
411 KnownOne >>= ShAmt;
412 KnownZero |= HighBits; // high bits known zero.
413 }
414 break;
415 case ISD::SRA:
416 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
417 MVT::ValueType VT = Op.getValueType();
418 unsigned ShAmt = SA->getValue();
419
420 // Compute the new bits that are at the top now.
421 uint64_t HighBits = (1ULL << ShAmt)-1;
422 HighBits <<= MVT::getSizeInBits(VT) - ShAmt;
423 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
424
425 if (SimplifyDemandedBits(Op.getOperand(0),
426 (DemandedMask << ShAmt) & TypeMask,
427 KnownZero, KnownOne, TLO, Depth+1))
428 return true;
429 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
430 KnownZero &= TypeMask;
431 KnownOne &= TypeMask;
432 KnownZero >>= SA->getValue();
433 KnownOne >>= SA->getValue();
434
435 // Handle the sign bits.
436 uint64_t SignBit = MVT::getIntVTSignBit(VT);
437 SignBit >>= SA->getValue(); // Adjust to where it is now in the mask.
438
439 // If the input sign bit is known to be zero, or if none of the top bits
440 // are demanded, turn this into an unsigned shift right.
441 if ((KnownZero & SignBit) || (HighBits & ~DemandedMask) == HighBits) {
442 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, VT, Op.getOperand(0),
443 Op.getOperand(1)));
444 } else if (KnownOne & SignBit) { // New bits are known one.
445 KnownOne |= HighBits;
446 }
447 }
448 break;
449 case ISD::SIGN_EXTEND_INREG: {
450 MVT::ValueType VT = Op.getValueType();
451 MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
452
Chris Lattnerec665152006-02-26 23:36:02 +0000453 // Sign extension. Compute the demanded bits in the result that are not
Nate Begeman368e18d2006-02-16 21:11:51 +0000454 // present in the input.
Chris Lattnerec665152006-02-26 23:36:02 +0000455 uint64_t NewBits = ~MVT::getIntVTBitMask(EVT) & DemandedMask;
Nate Begeman368e18d2006-02-16 21:11:51 +0000456
Chris Lattnerec665152006-02-26 23:36:02 +0000457 // If none of the extended bits are demanded, eliminate the sextinreg.
458 if (NewBits == 0)
459 return TLO.CombineTo(Op, Op.getOperand(0));
460
Nate Begeman368e18d2006-02-16 21:11:51 +0000461 uint64_t InSignBit = MVT::getIntVTSignBit(EVT);
462 int64_t InputDemandedBits = DemandedMask & MVT::getIntVTBitMask(EVT);
463
Chris Lattnerec665152006-02-26 23:36:02 +0000464 // Since the sign extended bits are demanded, we know that the sign
Nate Begeman368e18d2006-02-16 21:11:51 +0000465 // bit is demanded.
Chris Lattnerec665152006-02-26 23:36:02 +0000466 InputDemandedBits |= InSignBit;
Nate Begeman368e18d2006-02-16 21:11:51 +0000467
468 if (SimplifyDemandedBits(Op.getOperand(0), InputDemandedBits,
469 KnownZero, KnownOne, TLO, Depth+1))
470 return true;
471 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
472
473 // If the sign bit of the input is known set or clear, then we know the
474 // top bits of the result.
475
Chris Lattnerec665152006-02-26 23:36:02 +0000476 // If the input sign bit is known zero, convert this into a zero extension.
477 if (KnownZero & InSignBit)
478 return TLO.CombineTo(Op,
479 TLO.DAG.getZeroExtendInReg(Op.getOperand(0), EVT));
480
481 if (KnownOne & InSignBit) { // Input sign bit known set
Nate Begeman368e18d2006-02-16 21:11:51 +0000482 KnownOne |= NewBits;
483 KnownZero &= ~NewBits;
Chris Lattnerec665152006-02-26 23:36:02 +0000484 } else { // Input sign bit unknown
Nate Begeman368e18d2006-02-16 21:11:51 +0000485 KnownZero &= ~NewBits;
486 KnownOne &= ~NewBits;
487 }
488 break;
489 }
Chris Lattnerec665152006-02-26 23:36:02 +0000490 case ISD::CTTZ:
491 case ISD::CTLZ:
492 case ISD::CTPOP: {
493 MVT::ValueType VT = Op.getValueType();
494 unsigned LowBits = Log2_32(MVT::getSizeInBits(VT))+1;
495 KnownZero = ~((1ULL << LowBits)-1) & MVT::getIntVTBitMask(VT);
496 KnownOne = 0;
497 break;
498 }
499 case ISD::ZEXTLOAD: {
500 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(3))->getVT();
501 KnownZero |= ~MVT::getIntVTBitMask(VT) & DemandedMask;
502 break;
503 }
504 case ISD::ZERO_EXTEND: {
505 uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType());
506
507 // If none of the top bits are demanded, convert this into an any_extend.
508 uint64_t NewBits = (~InMask) & DemandedMask;
509 if (NewBits == 0)
510 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ANY_EXTEND,
511 Op.getValueType(),
512 Op.getOperand(0)));
513
514 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & InMask,
515 KnownZero, KnownOne, TLO, Depth+1))
516 return true;
517 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
518 KnownZero |= NewBits;
519 break;
520 }
521 case ISD::SIGN_EXTEND: {
522 MVT::ValueType InVT = Op.getOperand(0).getValueType();
523 uint64_t InMask = MVT::getIntVTBitMask(InVT);
524 uint64_t InSignBit = MVT::getIntVTSignBit(InVT);
525 uint64_t NewBits = (~InMask) & DemandedMask;
526
527 // If none of the top bits are demanded, convert this into an any_extend.
528 if (NewBits == 0)
529 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ANY_EXTEND,Op.getValueType(),
530 Op.getOperand(0)));
531
532 // Since some of the sign extended bits are demanded, we know that the sign
533 // bit is demanded.
534 uint64_t InDemandedBits = DemandedMask & InMask;
535 InDemandedBits |= InSignBit;
536
537 if (SimplifyDemandedBits(Op.getOperand(0), InDemandedBits, KnownZero,
538 KnownOne, TLO, Depth+1))
539 return true;
540
541 // If the sign bit is known zero, convert this to a zero extend.
542 if (KnownZero & InSignBit)
543 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ZERO_EXTEND,
544 Op.getValueType(),
545 Op.getOperand(0)));
546
547 // If the sign bit is known one, the top bits match.
548 if (KnownOne & InSignBit) {
549 KnownOne |= NewBits;
550 KnownZero &= ~NewBits;
551 } else { // Otherwise, top bits aren't known.
552 KnownOne &= ~NewBits;
553 KnownZero &= ~NewBits;
554 }
555 break;
556 }
557 case ISD::ANY_EXTEND: {
558 uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType());
559 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & InMask,
560 KnownZero, KnownOne, TLO, Depth+1))
561 return true;
562 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
563 break;
564 }
565 case ISD::AssertZext: {
566 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
567 uint64_t InMask = MVT::getIntVTBitMask(VT);
568 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & InMask,
569 KnownZero, KnownOne, TLO, Depth+1))
570 return true;
571 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
572 KnownZero |= ~InMask & DemandedMask;
573 break;
574 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000575 case ISD::ADD:
Chris Lattnera6bc5a42006-02-27 01:00:42 +0000576 case ISD::SUB:
577 // Just use ComputeMaskedBits to compute output bits, there are no
578 // simplifications that can be done here, and sub always demands all input
579 // bits.
580 ComputeMaskedBits(Op, DemandedMask, KnownZero, KnownOne, Depth);
581 break;
Nate Begeman368e18d2006-02-16 21:11:51 +0000582 }
Chris Lattnerec665152006-02-26 23:36:02 +0000583
584 // If we know the value of all of the demanded bits, return this as a
585 // constant.
586 if ((DemandedMask & (KnownZero|KnownOne)) == DemandedMask)
587 return TLO.CombineTo(Op, TLO.DAG.getConstant(KnownOne, Op.getValueType()));
588
Nate Begeman368e18d2006-02-16 21:11:51 +0000589 return false;
590}
591
592/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
593/// this predicate to simplify operations downstream. Mask is known to be zero
594/// for bits that V cannot have.
595bool TargetLowering::MaskedValueIsZero(SDOperand Op, uint64_t Mask,
596 unsigned Depth) const {
597 uint64_t KnownZero, KnownOne;
598 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
599 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
600 return (KnownZero & Mask) == Mask;
601}
602
603/// ComputeMaskedBits - Determine which of the bits specified in Mask are
604/// known to be either zero or one and return them in the KnownZero/KnownOne
605/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
606/// processing.
607void TargetLowering::ComputeMaskedBits(SDOperand Op, uint64_t Mask,
608 uint64_t &KnownZero, uint64_t &KnownOne,
609 unsigned Depth) const {
610 KnownZero = KnownOne = 0; // Don't know anything.
611 if (Depth == 6 || Mask == 0)
612 return; // Limit search depth.
613
614 uint64_t KnownZero2, KnownOne2;
615
616 switch (Op.getOpcode()) {
617 case ISD::Constant:
618 // We know all of the bits for a constant!
619 KnownOne = cast<ConstantSDNode>(Op)->getValue() & Mask;
620 KnownZero = ~KnownOne & Mask;
621 return;
622 case ISD::AND:
623 // If either the LHS or the RHS are Zero, the result is zero.
624 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
625 Mask &= ~KnownZero;
626 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
627 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
628 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
629
630 // Output known-1 bits are only known if set in both the LHS & RHS.
631 KnownOne &= KnownOne2;
632 // Output known-0 are known to be clear if zero in either the LHS | RHS.
633 KnownZero |= KnownZero2;
634 return;
635 case ISD::OR:
636 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
637 Mask &= ~KnownOne;
638 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
639 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
640 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
641
642 // Output known-0 bits are only known if clear in both the LHS & RHS.
643 KnownZero &= KnownZero2;
644 // Output known-1 are known to be set if set in either the LHS | RHS.
645 KnownOne |= KnownOne2;
646 return;
647 case ISD::XOR: {
648 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
649 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
650 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
651 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
652
653 // Output known-0 bits are known if clear or set in both the LHS & RHS.
654 uint64_t KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
655 // Output known-1 are known to be set if set in only one of the LHS, RHS.
656 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
657 KnownZero = KnownZeroOut;
658 return;
659 }
660 case ISD::SELECT:
661 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
662 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
663 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
664 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
665
666 // Only known if known in both the LHS and RHS.
667 KnownOne &= KnownOne2;
668 KnownZero &= KnownZero2;
669 return;
670 case ISD::SELECT_CC:
671 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
672 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
673 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
674 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
675
676 // Only known if known in both the LHS and RHS.
677 KnownOne &= KnownOne2;
678 KnownZero &= KnownZero2;
679 return;
680 case ISD::SETCC:
681 // If we know the result of a setcc has the top bits zero, use this info.
682 if (getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult)
683 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
684 return;
685 case ISD::SHL:
686 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
687 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
688 Mask >>= SA->getValue();
689 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
690 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
691 KnownZero <<= SA->getValue();
692 KnownOne <<= SA->getValue();
693 KnownZero |= (1ULL << SA->getValue())-1; // low bits known zero.
694 }
Nate Begeman003a2722006-02-18 02:43:25 +0000695 return;
Nate Begeman368e18d2006-02-16 21:11:51 +0000696 case ISD::SRL:
697 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
698 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
699 uint64_t HighBits = (1ULL << SA->getValue())-1;
700 HighBits <<= MVT::getSizeInBits(Op.getValueType())-SA->getValue();
701 Mask <<= SA->getValue();
702 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Nate Begeman003a2722006-02-18 02:43:25 +0000703 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Nate Begeman368e18d2006-02-16 21:11:51 +0000704 KnownZero >>= SA->getValue();
705 KnownOne >>= SA->getValue();
706 KnownZero |= HighBits; // high bits known zero.
707 }
Nate Begeman003a2722006-02-18 02:43:25 +0000708 return;
Nate Begeman368e18d2006-02-16 21:11:51 +0000709 case ISD::SRA:
710 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
711 uint64_t HighBits = (1ULL << SA->getValue())-1;
712 HighBits <<= MVT::getSizeInBits(Op.getValueType())-SA->getValue();
713 Mask <<= SA->getValue();
714 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
715 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
716 KnownZero >>= SA->getValue();
717 KnownOne >>= SA->getValue();
718
719 // Handle the sign bits.
720 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(Op.getValueType())-1);
721 SignBit >>= SA->getValue(); // Adjust to where it is now in the mask.
722
723 if (KnownZero & SignBit) { // New bits are known zero.
724 KnownZero |= HighBits;
725 } else if (KnownOne & SignBit) { // New bits are known one.
726 KnownOne |= HighBits;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000727 }
728 }
Nate Begeman003a2722006-02-18 02:43:25 +0000729 return;
Chris Lattnerec665152006-02-26 23:36:02 +0000730 case ISD::SIGN_EXTEND_INREG: {
731 MVT::ValueType VT = Op.getValueType();
732 MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
733
734 // Sign extension. Compute the demanded bits in the result that are not
735 // present in the input.
736 uint64_t NewBits = ~MVT::getIntVTBitMask(EVT) & Mask;
737
738 uint64_t InSignBit = MVT::getIntVTSignBit(EVT);
739 int64_t InputDemandedBits = Mask & MVT::getIntVTBitMask(EVT);
740
741 // If the sign extended bits are demanded, we know that the sign
742 // bit is demanded.
743 if (NewBits)
744 InputDemandedBits |= InSignBit;
745
746 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
747 KnownZero, KnownOne, Depth+1);
748 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
749
750 // If the sign bit of the input is known set or clear, then we know the
751 // top bits of the result.
752 if (KnownZero & InSignBit) { // Input sign bit known clear
753 KnownZero |= NewBits;
754 KnownOne &= ~NewBits;
755 } else if (KnownOne & InSignBit) { // Input sign bit known set
756 KnownOne |= NewBits;
757 KnownZero &= ~NewBits;
758 } else { // Input sign bit unknown
759 KnownZero &= ~NewBits;
760 KnownOne &= ~NewBits;
761 }
762 return;
763 }
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000764 case ISD::CTTZ:
765 case ISD::CTLZ:
Nate Begeman368e18d2006-02-16 21:11:51 +0000766 case ISD::CTPOP: {
767 MVT::ValueType VT = Op.getValueType();
768 unsigned LowBits = Log2_32(MVT::getSizeInBits(VT))+1;
769 KnownZero = ~((1ULL << LowBits)-1) & MVT::getIntVTBitMask(VT);
770 KnownOne = 0;
771 return;
772 }
773 case ISD::ZEXTLOAD: {
Chris Lattnerec665152006-02-26 23:36:02 +0000774 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(3))->getVT();
775 KnownZero |= ~MVT::getIntVTBitMask(VT) & Mask;
Nate Begeman368e18d2006-02-16 21:11:51 +0000776 return;
777 }
778 case ISD::ZERO_EXTEND: {
Chris Lattnerec665152006-02-26 23:36:02 +0000779 uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType());
780 uint64_t NewBits = (~InMask) & Mask;
781 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
782 KnownOne, Depth+1);
783 KnownZero |= NewBits & Mask;
784 KnownOne &= ~NewBits;
785 return;
786 }
787 case ISD::SIGN_EXTEND: {
788 MVT::ValueType InVT = Op.getOperand(0).getValueType();
789 unsigned InBits = MVT::getSizeInBits(InVT);
790 uint64_t InMask = MVT::getIntVTBitMask(InVT);
791 uint64_t InSignBit = 1ULL << (InBits-1);
792 uint64_t NewBits = (~InMask) & Mask;
793 uint64_t InDemandedBits = Mask & InMask;
794
795 // If any of the sign extended bits are demanded, we know that the sign
796 // bit is demanded.
797 if (NewBits & Mask)
798 InDemandedBits |= InSignBit;
799
800 ComputeMaskedBits(Op.getOperand(0), InDemandedBits, KnownZero,
801 KnownOne, Depth+1);
802 // If the sign bit is known zero or one, the top bits match.
803 if (KnownZero & InSignBit) {
804 KnownZero |= NewBits;
805 KnownOne &= ~NewBits;
806 } else if (KnownOne & InSignBit) {
807 KnownOne |= NewBits;
808 KnownZero &= ~NewBits;
809 } else { // Otherwise, top bits aren't known.
810 KnownOne &= ~NewBits;
811 KnownZero &= ~NewBits;
812 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000813 return;
814 }
815 case ISD::ANY_EXTEND: {
Chris Lattnerec665152006-02-26 23:36:02 +0000816 MVT::ValueType VT = Op.getOperand(0).getValueType();
817 ComputeMaskedBits(Op.getOperand(0), Mask & MVT::getIntVTBitMask(VT),
818 KnownZero, KnownOne, Depth+1);
Nate Begeman368e18d2006-02-16 21:11:51 +0000819 return;
820 }
821 case ISD::AssertZext: {
Chris Lattnerec665152006-02-26 23:36:02 +0000822 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
823 uint64_t InMask = MVT::getIntVTBitMask(VT);
824 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
825 KnownOne, Depth+1);
826 KnownZero |= (~InMask) & Mask;
Nate Begeman368e18d2006-02-16 21:11:51 +0000827 return;
828 }
829 case ISD::ADD: {
830 // If either the LHS or the RHS are Zero, the result is zero.
831 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
832 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
833 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
834 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
835
836 // Output known-0 bits are known if clear or set in both the low clear bits
Chris Lattnerb6b17ff2006-03-13 06:42:16 +0000837 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
838 // low 3 bits clear.
Nate Begeman368e18d2006-02-16 21:11:51 +0000839 uint64_t KnownZeroOut = std::min(CountTrailingZeros_64(~KnownZero),
840 CountTrailingZeros_64(~KnownZero2));
841
842 KnownZero = (1ULL << KnownZeroOut) - 1;
843 KnownOne = 0;
844 return;
845 }
Chris Lattnera6bc5a42006-02-27 01:00:42 +0000846 case ISD::SUB: {
847 ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0));
848 if (!CLHS) return;
849
Nate Begeman368e18d2006-02-16 21:11:51 +0000850 // We know that the top bits of C-X are clear if X contains less bits
851 // than C (i.e. no wrap-around can happen). For example, 20-X is
Chris Lattnera6bc5a42006-02-27 01:00:42 +0000852 // positive if we can prove that X is >= 0 and < 16.
853 MVT::ValueType VT = CLHS->getValueType(0);
854 if ((CLHS->getValue() & MVT::getIntVTSignBit(VT)) == 0) { // sign bit clear
855 unsigned NLZ = CountLeadingZeros_64(CLHS->getValue()+1);
856 uint64_t MaskV = (1ULL << (63-NLZ))-1; // NLZ can't be 64 with no sign bit
857 MaskV = ~MaskV & MVT::getIntVTBitMask(VT);
858 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero, KnownOne, Depth+1);
859
860 // If all of the MaskV bits are known to be zero, then we know the output
861 // top bits are zero, because we now know that the output is from [0-C].
862 if ((KnownZero & MaskV) == MaskV) {
863 unsigned NLZ2 = CountLeadingZeros_64(CLHS->getValue());
864 KnownZero = ~((1ULL << (64-NLZ2))-1) & Mask; // Top bits known zero.
865 KnownOne = 0; // No one bits known.
866 } else {
867 KnownOne = KnownOne = 0; // Otherwise, nothing known.
868 }
869 }
Nate Begeman003a2722006-02-18 02:43:25 +0000870 return;
Chris Lattnera6bc5a42006-02-27 01:00:42 +0000871 }
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000872 default:
873 // Allow the target to implement this method for its nodes.
874 if (Op.getOpcode() >= ISD::BUILTIN_OP_END)
Nate Begeman368e18d2006-02-16 21:11:51 +0000875 computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne);
Nate Begeman003a2722006-02-18 02:43:25 +0000876 return;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000877 }
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000878}
879
Nate Begeman368e18d2006-02-16 21:11:51 +0000880/// computeMaskedBitsForTargetNode - Determine which of the bits specified
881/// in Mask are known to be either zero or one and return them in the
882/// KnownZero/KnownOne bitsets.
883void TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
884 uint64_t Mask,
885 uint64_t &KnownZero,
886 uint64_t &KnownOne,
887 unsigned Depth) const {
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000888 assert(Op.getOpcode() >= ISD::BUILTIN_OP_END &&
889 "Should use MaskedValueIsZero if you don't know whether Op"
890 " is a target node!");
Nate Begeman368e18d2006-02-16 21:11:51 +0000891 KnownZero = 0;
892 KnownOne = 0;
Evan Cheng3a03ebb2005-12-21 23:05:39 +0000893}
Chris Lattner4ccb0702006-01-26 20:37:03 +0000894
Chris Lattner00ffed02006-03-01 04:52:55 +0000895SDOperand TargetLowering::
896PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
897 // Default implementation: no optimization.
898 return SDOperand();
899}
900
Chris Lattnereb8146b2006-02-04 02:13:02 +0000901//===----------------------------------------------------------------------===//
902// Inline Assembler Implementation Methods
903//===----------------------------------------------------------------------===//
904
905TargetLowering::ConstraintType
906TargetLowering::getConstraintType(char ConstraintLetter) const {
907 // FIXME: lots more standard ones to handle.
908 switch (ConstraintLetter) {
909 default: return C_Unknown;
910 case 'r': return C_RegisterClass;
Chris Lattner2b7401e2006-02-24 01:10:46 +0000911 case 'm': // memory
912 case 'o': // offsetable
913 case 'V': // not offsetable
914 return C_Memory;
Chris Lattnereb8146b2006-02-04 02:13:02 +0000915 case 'i': // Simple Integer or Relocatable Constant
916 case 'n': // Simple Integer
917 case 's': // Relocatable Constant
918 case 'I': // Target registers.
919 case 'J':
920 case 'K':
921 case 'L':
922 case 'M':
923 case 'N':
924 case 'O':
Chris Lattner2b7401e2006-02-24 01:10:46 +0000925 case 'P':
926 return C_Other;
Chris Lattnereb8146b2006-02-04 02:13:02 +0000927 }
928}
929
930bool TargetLowering::isOperandValidForConstraint(SDOperand Op,
931 char ConstraintLetter) {
932 switch (ConstraintLetter) {
933 default: return false;
934 case 'i': // Simple Integer or Relocatable Constant
935 case 'n': // Simple Integer
936 case 's': // Relocatable Constant
937 return true; // FIXME: not right.
938 }
939}
940
941
Chris Lattner4ccb0702006-01-26 20:37:03 +0000942std::vector<unsigned> TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +0000943getRegClassForInlineAsmConstraint(const std::string &Constraint,
944 MVT::ValueType VT) const {
945 return std::vector<unsigned>();
946}
947
948
949std::pair<unsigned, const TargetRegisterClass*> TargetLowering::
Chris Lattner4217ca8dc2006-02-21 23:11:00 +0000950getRegForInlineAsmConstraint(const std::string &Constraint,
951 MVT::ValueType VT) const {
Chris Lattner1efa40f2006-02-22 00:56:39 +0000952 if (Constraint[0] != '{')
953 return std::pair<unsigned, const TargetRegisterClass*>(0, 0);
Chris Lattnera55079a2006-02-01 01:29:47 +0000954 assert(*(Constraint.end()-1) == '}' && "Not a brace enclosed constraint?");
955
956 // Remove the braces from around the name.
957 std::string RegName(Constraint.begin()+1, Constraint.end()-1);
Chris Lattner1efa40f2006-02-22 00:56:39 +0000958
959 // Figure out which register class contains this reg.
Chris Lattner4ccb0702006-01-26 20:37:03 +0000960 const MRegisterInfo *RI = TM.getRegisterInfo();
Chris Lattner1efa40f2006-02-22 00:56:39 +0000961 for (MRegisterInfo::regclass_iterator RCI = RI->regclass_begin(),
962 E = RI->regclass_end(); RCI != E; ++RCI) {
963 const TargetRegisterClass *RC = *RCI;
Chris Lattnerb3befd42006-02-22 23:00:51 +0000964
965 // If none of the the value types for this register class are valid, we
966 // can't use it. For example, 64-bit reg classes on 32-bit targets.
967 bool isLegal = false;
968 for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
969 I != E; ++I) {
970 if (isTypeLegal(*I)) {
971 isLegal = true;
972 break;
973 }
974 }
975
976 if (!isLegal) continue;
977
Chris Lattner1efa40f2006-02-22 00:56:39 +0000978 for (TargetRegisterClass::iterator I = RC->begin(), E = RC->end();
979 I != E; ++I) {
Chris Lattnerb3befd42006-02-22 23:00:51 +0000980 if (StringsEqualNoCase(RegName, RI->get(*I).Name))
Chris Lattner1efa40f2006-02-22 00:56:39 +0000981 return std::make_pair(*I, RC);
Chris Lattner1efa40f2006-02-22 00:56:39 +0000982 }
Chris Lattner4ccb0702006-01-26 20:37:03 +0000983 }
Chris Lattnera55079a2006-02-01 01:29:47 +0000984
Chris Lattner1efa40f2006-02-22 00:56:39 +0000985 return std::pair<unsigned, const TargetRegisterClass*>(0, 0);
Chris Lattner4ccb0702006-01-26 20:37:03 +0000986}
Evan Cheng30b37b52006-03-13 23:18:16 +0000987
988//===----------------------------------------------------------------------===//
989// Loop Strength Reduction hooks
990//===----------------------------------------------------------------------===//
991
992/// isLegalAddressImmediate - Return true if the integer value or
993/// GlobalValue can be used as the offset of the target addressing mode.
994bool TargetLowering::isLegalAddressImmediate(int64_t V) const {
995 return false;
996}
997bool TargetLowering::isLegalAddressImmediate(GlobalValue *GV) const {
998 return false;
999}