blob: 2ab2074bd18fae870f0445ebdf9e100859274214 [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()) {
Chris Lattner310968c2005-01-07 07:44:53 +000024 assert(ISD::BUILTIN_OP_END <= 128 &&
25 "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*));
Evan Chenga03a5dc2006-02-14 08:38:30 +000033 maxStoresPerMemset = maxStoresPerMemcpy = maxStoresPerMemmove = 8;
Reid Spencer0f9beca2005-08-27 19:09:02 +000034 allowUnalignedMemoryAccesses = false;
Chris Lattner8e6be8b2005-09-27 22:13:56 +000035 UseUnderscoreSetJmpLongJmp = false;
Nate Begeman405e3ec2005-10-21 00:02:42 +000036 IntDivIsCheap = false;
37 Pow2DivIsCheap = false;
Chris Lattneree4a7652006-01-25 18:57:15 +000038 StackPointerRegisterToSaveRestore = 0;
Evan Cheng0577a222006-01-25 18:52:42 +000039 SchedPreferenceInfo = SchedulingForLatency;
Chris Lattner310968c2005-01-07 07:44:53 +000040}
41
Chris Lattnercba82f92005-01-16 07:28:11 +000042TargetLowering::~TargetLowering() {}
43
Chris Lattnerbb97d812005-01-16 01:10:58 +000044/// setValueTypeAction - Set the action for a particular value type. This
45/// assumes an action has not already been set for this value type.
Chris Lattnercba82f92005-01-16 07:28:11 +000046static void SetValueTypeAction(MVT::ValueType VT,
47 TargetLowering::LegalizeAction Action,
Chris Lattnerbb97d812005-01-16 01:10:58 +000048 TargetLowering &TLI,
49 MVT::ValueType *TransformToType,
Chris Lattner3e6e8cc2006-01-29 08:41:12 +000050 TargetLowering::ValueTypeActionImpl &ValueTypeActions) {
51 ValueTypeActions.setTypeAction(VT, Action);
Chris Lattnercba82f92005-01-16 07:28:11 +000052 if (Action == TargetLowering::Promote) {
Chris Lattnerbb97d812005-01-16 01:10:58 +000053 MVT::ValueType PromoteTo;
54 if (VT == MVT::f32)
55 PromoteTo = MVT::f64;
56 else {
57 unsigned LargerReg = VT+1;
Chris Lattner9ed62c12005-08-24 16:34:12 +000058 while (!TLI.isTypeLegal((MVT::ValueType)LargerReg)) {
Chris Lattnerbb97d812005-01-16 01:10:58 +000059 ++LargerReg;
60 assert(MVT::isInteger((MVT::ValueType)LargerReg) &&
61 "Nothing to promote to??");
62 }
63 PromoteTo = (MVT::ValueType)LargerReg;
64 }
65
66 assert(MVT::isInteger(VT) == MVT::isInteger(PromoteTo) &&
67 MVT::isFloatingPoint(VT) == MVT::isFloatingPoint(PromoteTo) &&
68 "Can only promote from int->int or fp->fp!");
69 assert(VT < PromoteTo && "Must promote to a larger type!");
70 TransformToType[VT] = PromoteTo;
Chris Lattnercba82f92005-01-16 07:28:11 +000071 } else if (Action == TargetLowering::Expand) {
Nate Begeman4ef3b812005-11-22 01:29:36 +000072 assert((VT == MVT::Vector || MVT::isInteger(VT)) && VT > MVT::i8 &&
Chris Lattnerbb97d812005-01-16 01:10:58 +000073 "Cannot expand this type: target must support SOME integer reg!");
74 // Expand to the next smaller integer type!
75 TransformToType[VT] = (MVT::ValueType)(VT-1);
76 }
77}
78
79
Chris Lattner310968c2005-01-07 07:44:53 +000080/// computeRegisterProperties - Once all of the register classes are added,
81/// this allows us to compute derived properties we expose.
82void TargetLowering::computeRegisterProperties() {
Nate Begeman6a648612005-11-29 05:45:29 +000083 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattnerbb97d812005-01-16 01:10:58 +000084 "Too many value types for ValueTypeActions to hold!");
85
Chris Lattner310968c2005-01-07 07:44:53 +000086 // Everything defaults to one.
87 for (unsigned i = 0; i != MVT::LAST_VALUETYPE; ++i)
88 NumElementsForVT[i] = 1;
Misha Brukmanf976c852005-04-21 22:55:34 +000089
Chris Lattner310968c2005-01-07 07:44:53 +000090 // Find the largest integer register class.
91 unsigned LargestIntReg = MVT::i128;
92 for (; RegClassForVT[LargestIntReg] == 0; --LargestIntReg)
93 assert(LargestIntReg != MVT::i1 && "No integer registers defined!");
94
95 // Every integer value type larger than this largest register takes twice as
96 // many registers to represent as the previous ValueType.
97 unsigned ExpandedReg = LargestIntReg; ++LargestIntReg;
98 for (++ExpandedReg; MVT::isInteger((MVT::ValueType)ExpandedReg);++ExpandedReg)
99 NumElementsForVT[ExpandedReg] = 2*NumElementsForVT[ExpandedReg-1];
Chris Lattner310968c2005-01-07 07:44:53 +0000100
Chris Lattnerbb97d812005-01-16 01:10:58 +0000101 // Inspect all of the ValueType's possible, deciding how to process them.
102 for (unsigned IntReg = MVT::i1; IntReg <= MVT::i128; ++IntReg)
103 // If we are expanding this type, expand it!
104 if (getNumElements((MVT::ValueType)IntReg) != 1)
Chris Lattnercba82f92005-01-16 07:28:11 +0000105 SetValueTypeAction((MVT::ValueType)IntReg, Expand, *this, TransformToType,
Chris Lattnerbb97d812005-01-16 01:10:58 +0000106 ValueTypeActions);
Chris Lattner9ed62c12005-08-24 16:34:12 +0000107 else if (!isTypeLegal((MVT::ValueType)IntReg))
Chris Lattnerbb97d812005-01-16 01:10:58 +0000108 // Otherwise, if we don't have native support, we must promote to a
109 // larger type.
Chris Lattnercba82f92005-01-16 07:28:11 +0000110 SetValueTypeAction((MVT::ValueType)IntReg, Promote, *this,
111 TransformToType, ValueTypeActions);
Chris Lattnercfdfe4c2005-01-16 01:20:18 +0000112 else
113 TransformToType[(MVT::ValueType)IntReg] = (MVT::ValueType)IntReg;
Misha Brukmanf976c852005-04-21 22:55:34 +0000114
Chris Lattnerbb97d812005-01-16 01:10:58 +0000115 // If the target does not have native support for F32, promote it to F64.
Chris Lattner9ed62c12005-08-24 16:34:12 +0000116 if (!isTypeLegal(MVT::f32))
Chris Lattnercba82f92005-01-16 07:28:11 +0000117 SetValueTypeAction(MVT::f32, Promote, *this,
118 TransformToType, ValueTypeActions);
Chris Lattnercfdfe4c2005-01-16 01:20:18 +0000119 else
120 TransformToType[MVT::f32] = MVT::f32;
Nate Begeman4ef3b812005-11-22 01:29:36 +0000121
122 // Set MVT::Vector to always be Expanded
123 SetValueTypeAction(MVT::Vector, Expand, *this, TransformToType,
124 ValueTypeActions);
Chris Lattnercfdfe4c2005-01-16 01:20:18 +0000125
Chris Lattner9ed62c12005-08-24 16:34:12 +0000126 assert(isTypeLegal(MVT::f64) && "Target does not support FP?");
Chris Lattnercfdfe4c2005-01-16 01:20:18 +0000127 TransformToType[MVT::f64] = MVT::f64;
Chris Lattnerbb97d812005-01-16 01:10:58 +0000128}
Chris Lattnercba82f92005-01-16 07:28:11 +0000129
Evan Cheng72261582005-12-20 06:22:03 +0000130const char *TargetLowering::getTargetNodeName(unsigned Opcode) const {
131 return NULL;
132}
Evan Cheng3a03ebb2005-12-21 23:05:39 +0000133
Chris Lattnereb8146b2006-02-04 02:13:02 +0000134//===----------------------------------------------------------------------===//
135// Optimization Methods
136//===----------------------------------------------------------------------===//
137
Nate Begeman368e18d2006-02-16 21:11:51 +0000138/// ShrinkDemandedConstant - Check to see if the specified operand of the
139/// specified instruction is a constant integer. If so, check to see if there
140/// are any bits set in the constant that are not demanded. If so, shrink the
141/// constant and return true.
142bool TargetLowering::TargetLoweringOpt::ShrinkDemandedConstant(SDOperand Op,
143 uint64_t Demanded) {
Chris Lattnerec665152006-02-26 23:36:02 +0000144 // FIXME: ISD::SELECT, ISD::SELECT_CC
Nate Begeman368e18d2006-02-16 21:11:51 +0000145 switch(Op.getOpcode()) {
146 default: break;
Nate Begemande996292006-02-03 22:24:05 +0000147 case ISD::AND:
Nate Begeman368e18d2006-02-16 21:11:51 +0000148 case ISD::OR:
149 case ISD::XOR:
150 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
151 if ((~Demanded & C->getValue()) != 0) {
152 MVT::ValueType VT = Op.getValueType();
153 SDOperand New = DAG.getNode(Op.getOpcode(), VT, Op.getOperand(0),
154 DAG.getConstant(Demanded & C->getValue(),
155 VT));
156 return CombineTo(Op, New);
Nate Begemande996292006-02-03 22:24:05 +0000157 }
Nate Begemande996292006-02-03 22:24:05 +0000158 break;
159 }
160 return false;
161}
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000162
Nate Begeman368e18d2006-02-16 21:11:51 +0000163/// SimplifyDemandedBits - Look at Op. At this point, we know that only the
164/// DemandedMask bits of the result of Op are ever used downstream. If we can
165/// use this information to simplify Op, create a new simplified DAG node and
166/// return true, returning the original and new nodes in Old and New. Otherwise,
167/// analyze the expression and return a mask of KnownOne and KnownZero bits for
168/// the expression (used to simplify the caller). The KnownZero/One bits may
169/// only be accurate for those bits in the DemandedMask.
170bool TargetLowering::SimplifyDemandedBits(SDOperand Op, uint64_t DemandedMask,
171 uint64_t &KnownZero,
172 uint64_t &KnownOne,
173 TargetLoweringOpt &TLO,
174 unsigned Depth) const {
175 KnownZero = KnownOne = 0; // Don't know anything.
176 // Other users may use these bits.
177 if (!Op.Val->hasOneUse()) {
178 if (Depth != 0) {
179 // If not at the root, Just compute the KnownZero/KnownOne bits to
180 // simplify things downstream.
181 ComputeMaskedBits(Op, DemandedMask, KnownZero, KnownOne, Depth);
182 return false;
183 }
184 // If this is the root being simplified, allow it to have multiple uses,
185 // just set the DemandedMask to all bits.
186 DemandedMask = MVT::getIntVTBitMask(Op.getValueType());
187 } else if (DemandedMask == 0) {
188 // Not demanding any bits from Op.
189 if (Op.getOpcode() != ISD::UNDEF)
190 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::UNDEF, Op.getValueType()));
191 return false;
192 } else if (Depth == 6) { // Limit search depth.
193 return false;
194 }
195
196 uint64_t KnownZero2, KnownOne2, KnownZeroOut, KnownOneOut;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000197 switch (Op.getOpcode()) {
198 case ISD::Constant:
Nate Begeman368e18d2006-02-16 21:11:51 +0000199 // We know all of the bits for a constant!
200 KnownOne = cast<ConstantSDNode>(Op)->getValue() & DemandedMask;
201 KnownZero = ~KnownOne & DemandedMask;
Chris Lattnerec665152006-02-26 23:36:02 +0000202 return false; // Don't fall through, will infinitely loop.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000203 case ISD::AND:
Nate Begeman368e18d2006-02-16 21:11:51 +0000204 // If either the LHS or the RHS are Zero, the result is zero.
205 if (SimplifyDemandedBits(Op.getOperand(1), DemandedMask, KnownZero,
206 KnownOne, TLO, Depth+1))
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000207 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000208 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Nate Begeman368e18d2006-02-16 21:11:51 +0000209 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & ~KnownZero,
210 KnownZero2, KnownOne2, TLO, Depth+1))
211 return true;
212 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
213
214 // If all of the demanded bits are known one on one side, return the other.
215 // These bits cannot contribute to the result of the 'and'.
216 if ((DemandedMask & ~KnownZero2 & KnownOne)==(DemandedMask & ~KnownZero2))
217 return TLO.CombineTo(Op, Op.getOperand(0));
218 if ((DemandedMask & ~KnownZero & KnownOne2)==(DemandedMask & ~KnownZero))
219 return TLO.CombineTo(Op, Op.getOperand(1));
220 // If all of the demanded bits in the inputs are known zeros, return zero.
221 if ((DemandedMask & (KnownZero|KnownZero2)) == DemandedMask)
222 return TLO.CombineTo(Op, TLO.DAG.getConstant(0, Op.getValueType()));
223 // If the RHS is a constant, see if we can simplify it.
224 if (TLO.ShrinkDemandedConstant(Op, DemandedMask & ~KnownZero2))
225 return true;
226
227 // Output known-1 bits are only known if set in both the LHS & RHS.
228 KnownOne &= KnownOne2;
229 // Output known-0 are known to be clear if zero in either the LHS | RHS.
230 KnownZero |= KnownZero2;
231 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000232 case ISD::OR:
Nate Begeman368e18d2006-02-16 21:11:51 +0000233 if (SimplifyDemandedBits(Op.getOperand(1), DemandedMask, KnownZero,
234 KnownOne, TLO, Depth+1))
235 return true;
236 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
237 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & ~KnownOne,
238 KnownZero2, KnownOne2, TLO, Depth+1))
239 return true;
240 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
241
242 // If all of the demanded bits are known zero on one side, return the other.
243 // These bits cannot contribute to the result of the 'or'.
Jeff Cohen5755b172006-02-17 02:12:18 +0000244 if ((DemandedMask & ~KnownOne2 & KnownZero) == (DemandedMask & ~KnownOne2))
Nate Begeman368e18d2006-02-16 21:11:51 +0000245 return TLO.CombineTo(Op, Op.getOperand(0));
Jeff Cohen5755b172006-02-17 02:12:18 +0000246 if ((DemandedMask & ~KnownOne & KnownZero2) == (DemandedMask & ~KnownOne))
Nate Begeman368e18d2006-02-16 21:11:51 +0000247 return TLO.CombineTo(Op, Op.getOperand(1));
248 // If all of the potentially set bits on one side are known to be set on
249 // the other side, just use the 'other' side.
250 if ((DemandedMask & (~KnownZero) & KnownOne2) ==
251 (DemandedMask & (~KnownZero)))
252 return TLO.CombineTo(Op, Op.getOperand(0));
253 if ((DemandedMask & (~KnownZero2) & KnownOne) ==
254 (DemandedMask & (~KnownZero2)))
255 return TLO.CombineTo(Op, Op.getOperand(1));
256 // If the RHS is a constant, see if we can simplify it.
257 if (TLO.ShrinkDemandedConstant(Op, DemandedMask))
258 return true;
259
260 // Output known-0 bits are only known if clear in both the LHS & RHS.
261 KnownZero &= KnownZero2;
262 // Output known-1 are known to be set if set in either the LHS | RHS.
263 KnownOne |= KnownOne2;
264 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000265 case ISD::XOR:
Nate Begeman368e18d2006-02-16 21:11:51 +0000266 if (SimplifyDemandedBits(Op.getOperand(1), DemandedMask, KnownZero,
267 KnownOne, TLO, Depth+1))
268 return true;
269 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
270 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask, KnownZero2,
271 KnownOne2, TLO, Depth+1))
272 return true;
273 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
274
275 // If all of the demanded bits are known zero on one side, return the other.
276 // These bits cannot contribute to the result of the 'xor'.
277 if ((DemandedMask & KnownZero) == DemandedMask)
278 return TLO.CombineTo(Op, Op.getOperand(0));
279 if ((DemandedMask & KnownZero2) == DemandedMask)
280 return TLO.CombineTo(Op, Op.getOperand(1));
281
282 // Output known-0 bits are known if clear or set in both the LHS & RHS.
283 KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
284 // Output known-1 are known to be set if set in only one of the LHS, RHS.
285 KnownOneOut = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
286
287 // If all of the unknown bits are known to be zero on one side or the other
288 // (but not both) turn this into an *inclusive* or.
289 // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
290 if (uint64_t UnknownBits = DemandedMask & ~(KnownZeroOut|KnownOneOut))
291 if ((UnknownBits & (KnownZero|KnownZero2)) == UnknownBits)
292 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::OR, Op.getValueType(),
293 Op.getOperand(0),
294 Op.getOperand(1)));
295 // If all of the demanded bits on one side are known, and all of the set
296 // bits on that side are also known to be set on the other side, turn this
297 // into an AND, as we know the bits will be cleared.
298 // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
299 if ((DemandedMask & (KnownZero|KnownOne)) == DemandedMask) { // all known
300 if ((KnownOne & KnownOne2) == KnownOne) {
301 MVT::ValueType VT = Op.getValueType();
302 SDOperand ANDC = TLO.DAG.getConstant(~KnownOne & DemandedMask, VT);
303 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::AND, VT, Op.getOperand(0),
304 ANDC));
305 }
306 }
307
308 // If the RHS is a constant, see if we can simplify it.
309 // FIXME: for XOR, we prefer to force bits to 1 if they will make a -1.
310 if (TLO.ShrinkDemandedConstant(Op, DemandedMask))
311 return true;
312
313 KnownZero = KnownZeroOut;
314 KnownOne = KnownOneOut;
315 break;
316 case ISD::SETCC:
317 // If we know the result of a setcc has the top bits zero, use this info.
318 if (getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult)
319 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
320 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000321 case ISD::SELECT:
Nate Begeman368e18d2006-02-16 21:11:51 +0000322 if (SimplifyDemandedBits(Op.getOperand(2), DemandedMask, KnownZero,
323 KnownOne, TLO, Depth+1))
324 return true;
325 if (SimplifyDemandedBits(Op.getOperand(1), DemandedMask, KnownZero2,
326 KnownOne2, TLO, Depth+1))
327 return true;
328 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
329 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
330
331 // If the operands are constants, see if we can simplify them.
332 if (TLO.ShrinkDemandedConstant(Op, DemandedMask))
333 return true;
334
335 // Only known if known in both the LHS and RHS.
336 KnownOne &= KnownOne2;
337 KnownZero &= KnownZero2;
338 break;
Chris Lattnerec665152006-02-26 23:36:02 +0000339 case ISD::SELECT_CC:
340 if (SimplifyDemandedBits(Op.getOperand(3), DemandedMask, KnownZero,
341 KnownOne, TLO, Depth+1))
342 return true;
343 if (SimplifyDemandedBits(Op.getOperand(2), DemandedMask, KnownZero2,
344 KnownOne2, TLO, Depth+1))
345 return true;
346 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
347 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
348
349 // If the operands are constants, see if we can simplify them.
350 if (TLO.ShrinkDemandedConstant(Op, DemandedMask))
351 return true;
352
353 // Only known if known in both the LHS and RHS.
354 KnownOne &= KnownOne2;
355 KnownZero &= KnownZero2;
356 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000357 case ISD::SHL:
Nate Begeman368e18d2006-02-16 21:11:51 +0000358 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
359 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask >> SA->getValue(),
360 KnownZero, KnownOne, TLO, Depth+1))
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000361 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000362 KnownZero <<= SA->getValue();
363 KnownOne <<= SA->getValue();
364 KnownZero |= (1ULL << SA->getValue())-1; // low bits known zero.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000365 }
366 break;
Nate Begeman368e18d2006-02-16 21:11:51 +0000367 case ISD::SRL:
368 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
369 MVT::ValueType VT = Op.getValueType();
370 unsigned ShAmt = SA->getValue();
371
372 // Compute the new bits that are at the top now.
373 uint64_t HighBits = (1ULL << ShAmt)-1;
374 HighBits <<= MVT::getSizeInBits(VT) - ShAmt;
375 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
376
377 if (SimplifyDemandedBits(Op.getOperand(0),
378 (DemandedMask << ShAmt) & TypeMask,
379 KnownZero, KnownOne, TLO, Depth+1))
380 return true;
381 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
382 KnownZero &= TypeMask;
383 KnownOne &= TypeMask;
384 KnownZero >>= ShAmt;
385 KnownOne >>= ShAmt;
386 KnownZero |= HighBits; // high bits known zero.
387 }
388 break;
389 case ISD::SRA:
390 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
391 MVT::ValueType VT = Op.getValueType();
392 unsigned ShAmt = SA->getValue();
393
394 // Compute the new bits that are at the top now.
395 uint64_t HighBits = (1ULL << ShAmt)-1;
396 HighBits <<= MVT::getSizeInBits(VT) - ShAmt;
397 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
398
399 if (SimplifyDemandedBits(Op.getOperand(0),
400 (DemandedMask << ShAmt) & TypeMask,
401 KnownZero, KnownOne, TLO, Depth+1))
402 return true;
403 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
404 KnownZero &= TypeMask;
405 KnownOne &= TypeMask;
406 KnownZero >>= SA->getValue();
407 KnownOne >>= SA->getValue();
408
409 // Handle the sign bits.
410 uint64_t SignBit = MVT::getIntVTSignBit(VT);
411 SignBit >>= SA->getValue(); // Adjust to where it is now in the mask.
412
413 // If the input sign bit is known to be zero, or if none of the top bits
414 // are demanded, turn this into an unsigned shift right.
415 if ((KnownZero & SignBit) || (HighBits & ~DemandedMask) == HighBits) {
416 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, VT, Op.getOperand(0),
417 Op.getOperand(1)));
418 } else if (KnownOne & SignBit) { // New bits are known one.
419 KnownOne |= HighBits;
420 }
421 }
422 break;
423 case ISD::SIGN_EXTEND_INREG: {
424 MVT::ValueType VT = Op.getValueType();
425 MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
426
Chris Lattnerec665152006-02-26 23:36:02 +0000427 // Sign extension. Compute the demanded bits in the result that are not
Nate Begeman368e18d2006-02-16 21:11:51 +0000428 // present in the input.
Chris Lattnerec665152006-02-26 23:36:02 +0000429 uint64_t NewBits = ~MVT::getIntVTBitMask(EVT) & DemandedMask;
Nate Begeman368e18d2006-02-16 21:11:51 +0000430
Chris Lattnerec665152006-02-26 23:36:02 +0000431 // If none of the extended bits are demanded, eliminate the sextinreg.
432 if (NewBits == 0)
433 return TLO.CombineTo(Op, Op.getOperand(0));
434
Nate Begeman368e18d2006-02-16 21:11:51 +0000435 uint64_t InSignBit = MVT::getIntVTSignBit(EVT);
436 int64_t InputDemandedBits = DemandedMask & MVT::getIntVTBitMask(EVT);
437
Chris Lattnerec665152006-02-26 23:36:02 +0000438 // Since the sign extended bits are demanded, we know that the sign
Nate Begeman368e18d2006-02-16 21:11:51 +0000439 // bit is demanded.
Chris Lattnerec665152006-02-26 23:36:02 +0000440 InputDemandedBits |= InSignBit;
Nate Begeman368e18d2006-02-16 21:11:51 +0000441
442 if (SimplifyDemandedBits(Op.getOperand(0), InputDemandedBits,
443 KnownZero, KnownOne, TLO, Depth+1))
444 return true;
445 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
446
447 // If the sign bit of the input is known set or clear, then we know the
448 // top bits of the result.
449
Chris Lattnerec665152006-02-26 23:36:02 +0000450 // If the input sign bit is known zero, convert this into a zero extension.
451 if (KnownZero & InSignBit)
452 return TLO.CombineTo(Op,
453 TLO.DAG.getZeroExtendInReg(Op.getOperand(0), EVT));
454
455 if (KnownOne & InSignBit) { // Input sign bit known set
Nate Begeman368e18d2006-02-16 21:11:51 +0000456 KnownOne |= NewBits;
457 KnownZero &= ~NewBits;
Chris Lattnerec665152006-02-26 23:36:02 +0000458 } else { // Input sign bit unknown
Nate Begeman368e18d2006-02-16 21:11:51 +0000459 KnownZero &= ~NewBits;
460 KnownOne &= ~NewBits;
461 }
462 break;
463 }
Chris Lattnerec665152006-02-26 23:36:02 +0000464 case ISD::CTTZ:
465 case ISD::CTLZ:
466 case ISD::CTPOP: {
467 MVT::ValueType VT = Op.getValueType();
468 unsigned LowBits = Log2_32(MVT::getSizeInBits(VT))+1;
469 KnownZero = ~((1ULL << LowBits)-1) & MVT::getIntVTBitMask(VT);
470 KnownOne = 0;
471 break;
472 }
473 case ISD::ZEXTLOAD: {
474 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(3))->getVT();
475 KnownZero |= ~MVT::getIntVTBitMask(VT) & DemandedMask;
476 break;
477 }
478 case ISD::ZERO_EXTEND: {
479 uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType());
480
481 // If none of the top bits are demanded, convert this into an any_extend.
482 uint64_t NewBits = (~InMask) & DemandedMask;
483 if (NewBits == 0)
484 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ANY_EXTEND,
485 Op.getValueType(),
486 Op.getOperand(0)));
487
488 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & InMask,
489 KnownZero, KnownOne, TLO, Depth+1))
490 return true;
491 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
492 KnownZero |= NewBits;
493 break;
494 }
495 case ISD::SIGN_EXTEND: {
496 MVT::ValueType InVT = Op.getOperand(0).getValueType();
497 uint64_t InMask = MVT::getIntVTBitMask(InVT);
498 uint64_t InSignBit = MVT::getIntVTSignBit(InVT);
499 uint64_t NewBits = (~InMask) & DemandedMask;
500
501 // If none of the top bits are demanded, convert this into an any_extend.
502 if (NewBits == 0)
503 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ANY_EXTEND,Op.getValueType(),
504 Op.getOperand(0)));
505
506 // Since some of the sign extended bits are demanded, we know that the sign
507 // bit is demanded.
508 uint64_t InDemandedBits = DemandedMask & InMask;
509 InDemandedBits |= InSignBit;
510
511 if (SimplifyDemandedBits(Op.getOperand(0), InDemandedBits, KnownZero,
512 KnownOne, TLO, Depth+1))
513 return true;
514
515 // If the sign bit is known zero, convert this to a zero extend.
516 if (KnownZero & InSignBit)
517 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ZERO_EXTEND,
518 Op.getValueType(),
519 Op.getOperand(0)));
520
521 // If the sign bit is known one, the top bits match.
522 if (KnownOne & InSignBit) {
523 KnownOne |= NewBits;
524 KnownZero &= ~NewBits;
525 } else { // Otherwise, top bits aren't known.
526 KnownOne &= ~NewBits;
527 KnownZero &= ~NewBits;
528 }
529 break;
530 }
531 case ISD::ANY_EXTEND: {
532 uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType());
533 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & InMask,
534 KnownZero, KnownOne, TLO, Depth+1))
535 return true;
536 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
537 break;
538 }
539 case ISD::AssertZext: {
540 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
541 uint64_t InMask = MVT::getIntVTBitMask(VT);
542 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & InMask,
543 KnownZero, KnownOne, TLO, Depth+1))
544 return true;
545 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
546 KnownZero |= ~InMask & DemandedMask;
547 break;
548 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000549 case ISD::ADD:
550 if (ConstantSDNode *AA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
551 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask, KnownZero,
552 KnownOne, TLO, Depth+1))
553 return true;
554 // Compute the KnownOne/KnownZero masks for the constant, so we can set
555 // KnownZero appropriately if we're adding a constant that has all low
556 // bits cleared.
557 ComputeMaskedBits(Op.getOperand(1),
558 MVT::getIntVTBitMask(Op.getValueType()),
559 KnownZero2, KnownOne2, Depth+1);
560
561 uint64_t KnownZeroOut = std::min(CountTrailingZeros_64(~KnownZero),
562 CountTrailingZeros_64(~KnownZero2));
563 KnownZero = (1ULL << KnownZeroOut) - 1;
564 KnownOne = 0;
Nate Begeman003a2722006-02-18 02:43:25 +0000565
566 SDOperand SH = Op.getOperand(0);
567 // fold (add (shl x, c1), (shl c2, c1)) -> (shl (add x, c2), c1)
568 if (KnownZero && SH.getOpcode() == ISD::SHL && SH.Val->hasOneUse() &&
569 Op.Val->hasOneUse()) {
570 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(SH.getOperand(1))) {
571 MVT::ValueType VT = Op.getValueType();
572 unsigned ShiftAmt = SA->getValue();
573 uint64_t AddAmt = AA->getValue();
574 uint64_t AddShr = AddAmt >> ShiftAmt;
575 if (AddAmt == (AddShr << ShiftAmt)) {
576 SDOperand ADD = TLO.DAG.getNode(ISD::ADD, VT, SH.getOperand(0),
577 TLO.DAG.getConstant(AddShr, VT));
578 SDOperand SHL = TLO.DAG.getNode(ISD::SHL, VT, ADD,SH.getOperand(1));
579 return TLO.CombineTo(Op, SHL);
580 }
581 }
582 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000583 }
584 break;
Nate Begeman368e18d2006-02-16 21:11:51 +0000585 }
Chris Lattnerec665152006-02-26 23:36:02 +0000586
587 // If we know the value of all of the demanded bits, return this as a
588 // constant.
589 if ((DemandedMask & (KnownZero|KnownOne)) == DemandedMask)
590 return TLO.CombineTo(Op, TLO.DAG.getConstant(KnownOne, Op.getValueType()));
591
Nate Begeman368e18d2006-02-16 21:11:51 +0000592 return false;
593}
594
595/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
596/// this predicate to simplify operations downstream. Mask is known to be zero
597/// for bits that V cannot have.
598bool TargetLowering::MaskedValueIsZero(SDOperand Op, uint64_t Mask,
599 unsigned Depth) const {
600 uint64_t KnownZero, KnownOne;
601 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
602 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
603 return (KnownZero & Mask) == Mask;
604}
605
606/// ComputeMaskedBits - Determine which of the bits specified in Mask are
607/// known to be either zero or one and return them in the KnownZero/KnownOne
608/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
609/// processing.
610void TargetLowering::ComputeMaskedBits(SDOperand Op, uint64_t Mask,
611 uint64_t &KnownZero, uint64_t &KnownOne,
612 unsigned Depth) const {
613 KnownZero = KnownOne = 0; // Don't know anything.
614 if (Depth == 6 || Mask == 0)
615 return; // Limit search depth.
616
617 uint64_t KnownZero2, KnownOne2;
618
619 switch (Op.getOpcode()) {
620 case ISD::Constant:
621 // We know all of the bits for a constant!
622 KnownOne = cast<ConstantSDNode>(Op)->getValue() & Mask;
623 KnownZero = ~KnownOne & Mask;
624 return;
625 case ISD::AND:
626 // If either the LHS or the RHS are Zero, the result is zero.
627 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
628 Mask &= ~KnownZero;
629 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
630 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
631 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
632
633 // Output known-1 bits are only known if set in both the LHS & RHS.
634 KnownOne &= KnownOne2;
635 // Output known-0 are known to be clear if zero in either the LHS | RHS.
636 KnownZero |= KnownZero2;
637 return;
638 case ISD::OR:
639 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
640 Mask &= ~KnownOne;
641 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
642 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
643 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
644
645 // Output known-0 bits are only known if clear in both the LHS & RHS.
646 KnownZero &= KnownZero2;
647 // Output known-1 are known to be set if set in either the LHS | RHS.
648 KnownOne |= KnownOne2;
649 return;
650 case ISD::XOR: {
651 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
652 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
653 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
654 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
655
656 // Output known-0 bits are known if clear or set in both the LHS & RHS.
657 uint64_t KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
658 // Output known-1 are known to be set if set in only one of the LHS, RHS.
659 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
660 KnownZero = KnownZeroOut;
661 return;
662 }
663 case ISD::SELECT:
664 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
665 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
666 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
667 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
668
669 // Only known if known in both the LHS and RHS.
670 KnownOne &= KnownOne2;
671 KnownZero &= KnownZero2;
672 return;
673 case ISD::SELECT_CC:
674 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
675 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
676 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
677 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
678
679 // Only known if known in both the LHS and RHS.
680 KnownOne &= KnownOne2;
681 KnownZero &= KnownZero2;
682 return;
683 case ISD::SETCC:
684 // If we know the result of a setcc has the top bits zero, use this info.
685 if (getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult)
686 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
687 return;
688 case ISD::SHL:
689 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
690 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
691 Mask >>= SA->getValue();
692 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
693 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
694 KnownZero <<= SA->getValue();
695 KnownOne <<= SA->getValue();
696 KnownZero |= (1ULL << SA->getValue())-1; // low bits known zero.
697 }
Nate Begeman003a2722006-02-18 02:43:25 +0000698 return;
Nate Begeman368e18d2006-02-16 21:11:51 +0000699 case ISD::SRL:
700 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
701 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
702 uint64_t HighBits = (1ULL << SA->getValue())-1;
703 HighBits <<= MVT::getSizeInBits(Op.getValueType())-SA->getValue();
704 Mask <<= SA->getValue();
705 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Nate Begeman003a2722006-02-18 02:43:25 +0000706 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Nate Begeman368e18d2006-02-16 21:11:51 +0000707 KnownZero >>= SA->getValue();
708 KnownOne >>= SA->getValue();
709 KnownZero |= HighBits; // high bits known zero.
710 }
Nate Begeman003a2722006-02-18 02:43:25 +0000711 return;
Nate Begeman368e18d2006-02-16 21:11:51 +0000712 case ISD::SRA:
713 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
714 uint64_t HighBits = (1ULL << SA->getValue())-1;
715 HighBits <<= MVT::getSizeInBits(Op.getValueType())-SA->getValue();
716 Mask <<= SA->getValue();
717 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
718 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
719 KnownZero >>= SA->getValue();
720 KnownOne >>= SA->getValue();
721
722 // Handle the sign bits.
723 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(Op.getValueType())-1);
724 SignBit >>= SA->getValue(); // Adjust to where it is now in the mask.
725
726 if (KnownZero & SignBit) { // New bits are known zero.
727 KnownZero |= HighBits;
728 } else if (KnownOne & SignBit) { // New bits are known one.
729 KnownOne |= HighBits;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000730 }
731 }
Nate Begeman003a2722006-02-18 02:43:25 +0000732 return;
Chris Lattnerec665152006-02-26 23:36:02 +0000733 case ISD::SIGN_EXTEND_INREG: {
734 MVT::ValueType VT = Op.getValueType();
735 MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
736
737 // Sign extension. Compute the demanded bits in the result that are not
738 // present in the input.
739 uint64_t NewBits = ~MVT::getIntVTBitMask(EVT) & Mask;
740
741 uint64_t InSignBit = MVT::getIntVTSignBit(EVT);
742 int64_t InputDemandedBits = Mask & MVT::getIntVTBitMask(EVT);
743
744 // If the sign extended bits are demanded, we know that the sign
745 // bit is demanded.
746 if (NewBits)
747 InputDemandedBits |= InSignBit;
748
749 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
750 KnownZero, KnownOne, Depth+1);
751 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
752
753 // If the sign bit of the input is known set or clear, then we know the
754 // top bits of the result.
755 if (KnownZero & InSignBit) { // Input sign bit known clear
756 KnownZero |= NewBits;
757 KnownOne &= ~NewBits;
758 } else if (KnownOne & InSignBit) { // Input sign bit known set
759 KnownOne |= NewBits;
760 KnownZero &= ~NewBits;
761 } else { // Input sign bit unknown
762 KnownZero &= ~NewBits;
763 KnownOne &= ~NewBits;
764 }
765 return;
766 }
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000767 case ISD::CTTZ:
768 case ISD::CTLZ:
Nate Begeman368e18d2006-02-16 21:11:51 +0000769 case ISD::CTPOP: {
770 MVT::ValueType VT = Op.getValueType();
771 unsigned LowBits = Log2_32(MVT::getSizeInBits(VT))+1;
772 KnownZero = ~((1ULL << LowBits)-1) & MVT::getIntVTBitMask(VT);
773 KnownOne = 0;
774 return;
775 }
776 case ISD::ZEXTLOAD: {
Chris Lattnerec665152006-02-26 23:36:02 +0000777 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(3))->getVT();
778 KnownZero |= ~MVT::getIntVTBitMask(VT) & Mask;
Nate Begeman368e18d2006-02-16 21:11:51 +0000779 return;
780 }
781 case ISD::ZERO_EXTEND: {
Chris Lattnerec665152006-02-26 23:36:02 +0000782 uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType());
783 uint64_t NewBits = (~InMask) & Mask;
784 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
785 KnownOne, Depth+1);
786 KnownZero |= NewBits & Mask;
787 KnownOne &= ~NewBits;
788 return;
789 }
790 case ISD::SIGN_EXTEND: {
791 MVT::ValueType InVT = Op.getOperand(0).getValueType();
792 unsigned InBits = MVT::getSizeInBits(InVT);
793 uint64_t InMask = MVT::getIntVTBitMask(InVT);
794 uint64_t InSignBit = 1ULL << (InBits-1);
795 uint64_t NewBits = (~InMask) & Mask;
796 uint64_t InDemandedBits = Mask & InMask;
797
798 // If any of the sign extended bits are demanded, we know that the sign
799 // bit is demanded.
800 if (NewBits & Mask)
801 InDemandedBits |= InSignBit;
802
803 ComputeMaskedBits(Op.getOperand(0), InDemandedBits, KnownZero,
804 KnownOne, Depth+1);
805 // If the sign bit is known zero or one, the top bits match.
806 if (KnownZero & InSignBit) {
807 KnownZero |= NewBits;
808 KnownOne &= ~NewBits;
809 } else if (KnownOne & InSignBit) {
810 KnownOne |= NewBits;
811 KnownZero &= ~NewBits;
812 } else { // Otherwise, top bits aren't known.
813 KnownOne &= ~NewBits;
814 KnownZero &= ~NewBits;
815 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000816 return;
817 }
818 case ISD::ANY_EXTEND: {
Chris Lattnerec665152006-02-26 23:36:02 +0000819 MVT::ValueType VT = Op.getOperand(0).getValueType();
820 ComputeMaskedBits(Op.getOperand(0), Mask & MVT::getIntVTBitMask(VT),
821 KnownZero, KnownOne, Depth+1);
Nate Begeman368e18d2006-02-16 21:11:51 +0000822 return;
823 }
824 case ISD::AssertZext: {
Chris Lattnerec665152006-02-26 23:36:02 +0000825 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
826 uint64_t InMask = MVT::getIntVTBitMask(VT);
827 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
828 KnownOne, Depth+1);
829 KnownZero |= (~InMask) & Mask;
Nate Begeman368e18d2006-02-16 21:11:51 +0000830 return;
831 }
832 case ISD::ADD: {
833 // If either the LHS or the RHS are Zero, the result is zero.
834 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
835 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
836 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
837 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
838
839 // Output known-0 bits are known if clear or set in both the low clear bits
840 // common to both LHS & RHS;
841 uint64_t KnownZeroOut = std::min(CountTrailingZeros_64(~KnownZero),
842 CountTrailingZeros_64(~KnownZero2));
843
844 KnownZero = (1ULL << KnownZeroOut) - 1;
845 KnownOne = 0;
846 return;
847 }
848 case ISD::SUB:
849 // We know that the top bits of C-X are clear if X contains less bits
850 // than C (i.e. no wrap-around can happen). For example, 20-X is
Chris Lattnerec665152006-02-26 23:36:02 +0000851 // positive if we can prove that X is >= 0 and < 16. Remember to update
852 // SimplifyDemandedBits if/when this is implemented.
Nate Begeman003a2722006-02-18 02:43:25 +0000853 return;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000854 default:
855 // Allow the target to implement this method for its nodes.
856 if (Op.getOpcode() >= ISD::BUILTIN_OP_END)
Nate Begeman368e18d2006-02-16 21:11:51 +0000857 computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne);
Nate Begeman003a2722006-02-18 02:43:25 +0000858 return;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000859 }
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000860}
861
Nate Begeman368e18d2006-02-16 21:11:51 +0000862/// computeMaskedBitsForTargetNode - Determine which of the bits specified
863/// in Mask are known to be either zero or one and return them in the
864/// KnownZero/KnownOne bitsets.
865void TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
866 uint64_t Mask,
867 uint64_t &KnownZero,
868 uint64_t &KnownOne,
869 unsigned Depth) const {
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000870 assert(Op.getOpcode() >= ISD::BUILTIN_OP_END &&
871 "Should use MaskedValueIsZero if you don't know whether Op"
872 " is a target node!");
Nate Begeman368e18d2006-02-16 21:11:51 +0000873 KnownZero = 0;
874 KnownOne = 0;
Evan Cheng3a03ebb2005-12-21 23:05:39 +0000875}
Chris Lattner4ccb0702006-01-26 20:37:03 +0000876
Chris Lattnereb8146b2006-02-04 02:13:02 +0000877//===----------------------------------------------------------------------===//
878// Inline Assembler Implementation Methods
879//===----------------------------------------------------------------------===//
880
881TargetLowering::ConstraintType
882TargetLowering::getConstraintType(char ConstraintLetter) const {
883 // FIXME: lots more standard ones to handle.
884 switch (ConstraintLetter) {
885 default: return C_Unknown;
886 case 'r': return C_RegisterClass;
Chris Lattner2b7401e2006-02-24 01:10:46 +0000887 case 'm': // memory
888 case 'o': // offsetable
889 case 'V': // not offsetable
890 return C_Memory;
Chris Lattnereb8146b2006-02-04 02:13:02 +0000891 case 'i': // Simple Integer or Relocatable Constant
892 case 'n': // Simple Integer
893 case 's': // Relocatable Constant
894 case 'I': // Target registers.
895 case 'J':
896 case 'K':
897 case 'L':
898 case 'M':
899 case 'N':
900 case 'O':
Chris Lattner2b7401e2006-02-24 01:10:46 +0000901 case 'P':
902 return C_Other;
Chris Lattnereb8146b2006-02-04 02:13:02 +0000903 }
904}
905
906bool TargetLowering::isOperandValidForConstraint(SDOperand Op,
907 char ConstraintLetter) {
908 switch (ConstraintLetter) {
909 default: return false;
910 case 'i': // Simple Integer or Relocatable Constant
911 case 'n': // Simple Integer
912 case 's': // Relocatable Constant
913 return true; // FIXME: not right.
914 }
915}
916
917
Chris Lattner4ccb0702006-01-26 20:37:03 +0000918std::vector<unsigned> TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +0000919getRegClassForInlineAsmConstraint(const std::string &Constraint,
920 MVT::ValueType VT) const {
921 return std::vector<unsigned>();
922}
923
924
925std::pair<unsigned, const TargetRegisterClass*> TargetLowering::
Chris Lattner4217ca8dc2006-02-21 23:11:00 +0000926getRegForInlineAsmConstraint(const std::string &Constraint,
927 MVT::ValueType VT) const {
Chris Lattner1efa40f2006-02-22 00:56:39 +0000928 if (Constraint[0] != '{')
929 return std::pair<unsigned, const TargetRegisterClass*>(0, 0);
Chris Lattnera55079a2006-02-01 01:29:47 +0000930 assert(*(Constraint.end()-1) == '}' && "Not a brace enclosed constraint?");
931
932 // Remove the braces from around the name.
933 std::string RegName(Constraint.begin()+1, Constraint.end()-1);
Chris Lattner1efa40f2006-02-22 00:56:39 +0000934
935 // Figure out which register class contains this reg.
Chris Lattner4ccb0702006-01-26 20:37:03 +0000936 const MRegisterInfo *RI = TM.getRegisterInfo();
Chris Lattner1efa40f2006-02-22 00:56:39 +0000937 for (MRegisterInfo::regclass_iterator RCI = RI->regclass_begin(),
938 E = RI->regclass_end(); RCI != E; ++RCI) {
939 const TargetRegisterClass *RC = *RCI;
Chris Lattnerb3befd42006-02-22 23:00:51 +0000940
941 // If none of the the value types for this register class are valid, we
942 // can't use it. For example, 64-bit reg classes on 32-bit targets.
943 bool isLegal = false;
944 for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
945 I != E; ++I) {
946 if (isTypeLegal(*I)) {
947 isLegal = true;
948 break;
949 }
950 }
951
952 if (!isLegal) continue;
953
Chris Lattner1efa40f2006-02-22 00:56:39 +0000954 for (TargetRegisterClass::iterator I = RC->begin(), E = RC->end();
955 I != E; ++I) {
Chris Lattnerb3befd42006-02-22 23:00:51 +0000956 if (StringsEqualNoCase(RegName, RI->get(*I).Name))
Chris Lattner1efa40f2006-02-22 00:56:39 +0000957 return std::make_pair(*I, RC);
Chris Lattner1efa40f2006-02-22 00:56:39 +0000958 }
Chris Lattner4ccb0702006-01-26 20:37:03 +0000959 }
Chris Lattnera55079a2006-02-01 01:29:47 +0000960
Chris Lattner1efa40f2006-02-22 00:56:39 +0000961 return std::pair<unsigned, const TargetRegisterClass*>(0, 0);
Chris Lattner4ccb0702006-01-26 20:37:03 +0000962}