blob: 176ddac02493335543fdeef7fe900e9510341b47 [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:
Chris Lattner81cd3552006-02-27 00:36:27 +0000204 // If the RHS is a constant, check to see if the LHS would be zero without
205 // using the bits from the RHS. Below, we use knowledge about the RHS to
206 // simplify the LHS, here we're using information from the LHS to simplify
207 // the RHS.
208 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
209 uint64_t LHSZero, LHSOne;
210 ComputeMaskedBits(Op.getOperand(0), DemandedMask,
211 LHSZero, LHSOne, Depth+1);
212 // If the LHS already has zeros where RHSC does, this and is dead.
213 if ((LHSZero & DemandedMask) == (~RHSC->getValue() & DemandedMask))
214 return TLO.CombineTo(Op, Op.getOperand(0));
215 // If any of the set bits in the RHS are known zero on the LHS, shrink
216 // the constant.
217 if (TLO.ShrinkDemandedConstant(Op, ~LHSZero & DemandedMask))
218 return true;
219 }
220
Nate Begeman368e18d2006-02-16 21:11:51 +0000221 if (SimplifyDemandedBits(Op.getOperand(1), DemandedMask, KnownZero,
222 KnownOne, TLO, Depth+1))
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000223 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000224 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Nate Begeman368e18d2006-02-16 21:11:51 +0000225 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & ~KnownZero,
226 KnownZero2, KnownOne2, TLO, Depth+1))
227 return true;
228 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
229
230 // If all of the demanded bits are known one on one side, return the other.
231 // These bits cannot contribute to the result of the 'and'.
232 if ((DemandedMask & ~KnownZero2 & KnownOne)==(DemandedMask & ~KnownZero2))
233 return TLO.CombineTo(Op, Op.getOperand(0));
234 if ((DemandedMask & ~KnownZero & KnownOne2)==(DemandedMask & ~KnownZero))
235 return TLO.CombineTo(Op, Op.getOperand(1));
236 // If all of the demanded bits in the inputs are known zeros, return zero.
237 if ((DemandedMask & (KnownZero|KnownZero2)) == DemandedMask)
238 return TLO.CombineTo(Op, TLO.DAG.getConstant(0, Op.getValueType()));
239 // If the RHS is a constant, see if we can simplify it.
240 if (TLO.ShrinkDemandedConstant(Op, DemandedMask & ~KnownZero2))
241 return true;
Chris Lattner5f0c6582006-02-27 00:22:28 +0000242
Nate Begeman368e18d2006-02-16 21:11:51 +0000243 // Output known-1 bits are only known if set in both the LHS & RHS.
244 KnownOne &= KnownOne2;
245 // Output known-0 are known to be clear if zero in either the LHS | RHS.
246 KnownZero |= KnownZero2;
247 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000248 case ISD::OR:
Nate Begeman368e18d2006-02-16 21:11:51 +0000249 if (SimplifyDemandedBits(Op.getOperand(1), DemandedMask, KnownZero,
250 KnownOne, TLO, Depth+1))
251 return true;
252 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
253 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & ~KnownOne,
254 KnownZero2, KnownOne2, TLO, Depth+1))
255 return true;
256 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
257
258 // If all of the demanded bits are known zero on one side, return the other.
259 // These bits cannot contribute to the result of the 'or'.
Jeff Cohen5755b172006-02-17 02:12:18 +0000260 if ((DemandedMask & ~KnownOne2 & KnownZero) == (DemandedMask & ~KnownOne2))
Nate Begeman368e18d2006-02-16 21:11:51 +0000261 return TLO.CombineTo(Op, Op.getOperand(0));
Jeff Cohen5755b172006-02-17 02:12:18 +0000262 if ((DemandedMask & ~KnownOne & KnownZero2) == (DemandedMask & ~KnownOne))
Nate Begeman368e18d2006-02-16 21:11:51 +0000263 return TLO.CombineTo(Op, Op.getOperand(1));
264 // If all of the potentially set bits on one side are known to be set on
265 // the other side, just use the 'other' side.
266 if ((DemandedMask & (~KnownZero) & KnownOne2) ==
267 (DemandedMask & (~KnownZero)))
268 return TLO.CombineTo(Op, Op.getOperand(0));
269 if ((DemandedMask & (~KnownZero2) & KnownOne) ==
270 (DemandedMask & (~KnownZero2)))
271 return TLO.CombineTo(Op, Op.getOperand(1));
272 // If the RHS is a constant, see if we can simplify it.
273 if (TLO.ShrinkDemandedConstant(Op, DemandedMask))
274 return true;
275
276 // Output known-0 bits are only known if clear in both the LHS & RHS.
277 KnownZero &= KnownZero2;
278 // Output known-1 are known to be set if set in either the LHS | RHS.
279 KnownOne |= KnownOne2;
280 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000281 case ISD::XOR:
Nate Begeman368e18d2006-02-16 21:11:51 +0000282 if (SimplifyDemandedBits(Op.getOperand(1), DemandedMask, KnownZero,
283 KnownOne, TLO, Depth+1))
284 return true;
285 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
286 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask, KnownZero2,
287 KnownOne2, TLO, Depth+1))
288 return true;
289 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
290
291 // If all of the demanded bits are known zero on one side, return the other.
292 // These bits cannot contribute to the result of the 'xor'.
293 if ((DemandedMask & KnownZero) == DemandedMask)
294 return TLO.CombineTo(Op, Op.getOperand(0));
295 if ((DemandedMask & KnownZero2) == DemandedMask)
296 return TLO.CombineTo(Op, Op.getOperand(1));
297
298 // Output known-0 bits are known if clear or set in both the LHS & RHS.
299 KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
300 // Output known-1 are known to be set if set in only one of the LHS, RHS.
301 KnownOneOut = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
302
303 // If all of the unknown bits are known to be zero on one side or the other
304 // (but not both) turn this into an *inclusive* or.
305 // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
306 if (uint64_t UnknownBits = DemandedMask & ~(KnownZeroOut|KnownOneOut))
307 if ((UnknownBits & (KnownZero|KnownZero2)) == UnknownBits)
308 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::OR, Op.getValueType(),
309 Op.getOperand(0),
310 Op.getOperand(1)));
311 // If all of the demanded bits on one side are known, and all of the set
312 // bits on that side are also known to be set on the other side, turn this
313 // into an AND, as we know the bits will be cleared.
314 // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
315 if ((DemandedMask & (KnownZero|KnownOne)) == DemandedMask) { // all known
316 if ((KnownOne & KnownOne2) == KnownOne) {
317 MVT::ValueType VT = Op.getValueType();
318 SDOperand ANDC = TLO.DAG.getConstant(~KnownOne & DemandedMask, VT);
319 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::AND, VT, Op.getOperand(0),
320 ANDC));
321 }
322 }
323
324 // If the RHS is a constant, see if we can simplify it.
325 // FIXME: for XOR, we prefer to force bits to 1 if they will make a -1.
326 if (TLO.ShrinkDemandedConstant(Op, DemandedMask))
327 return true;
328
329 KnownZero = KnownZeroOut;
330 KnownOne = KnownOneOut;
331 break;
332 case ISD::SETCC:
333 // If we know the result of a setcc has the top bits zero, use this info.
334 if (getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult)
335 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
336 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000337 case ISD::SELECT:
Nate Begeman368e18d2006-02-16 21:11:51 +0000338 if (SimplifyDemandedBits(Op.getOperand(2), DemandedMask, KnownZero,
339 KnownOne, TLO, Depth+1))
340 return true;
341 if (SimplifyDemandedBits(Op.getOperand(1), DemandedMask, KnownZero2,
342 KnownOne2, TLO, Depth+1))
343 return true;
344 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
345 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
346
347 // If the operands are constants, see if we can simplify them.
348 if (TLO.ShrinkDemandedConstant(Op, DemandedMask))
349 return true;
350
351 // Only known if known in both the LHS and RHS.
352 KnownOne &= KnownOne2;
353 KnownZero &= KnownZero2;
354 break;
Chris Lattnerec665152006-02-26 23:36:02 +0000355 case ISD::SELECT_CC:
356 if (SimplifyDemandedBits(Op.getOperand(3), DemandedMask, KnownZero,
357 KnownOne, TLO, Depth+1))
358 return true;
359 if (SimplifyDemandedBits(Op.getOperand(2), DemandedMask, KnownZero2,
360 KnownOne2, TLO, Depth+1))
361 return true;
362 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
363 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
364
365 // If the operands are constants, see if we can simplify them.
366 if (TLO.ShrinkDemandedConstant(Op, DemandedMask))
367 return true;
368
369 // Only known if known in both the LHS and RHS.
370 KnownOne &= KnownOne2;
371 KnownZero &= KnownZero2;
372 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000373 case ISD::SHL:
Nate Begeman368e18d2006-02-16 21:11:51 +0000374 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
375 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask >> SA->getValue(),
376 KnownZero, KnownOne, TLO, Depth+1))
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000377 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000378 KnownZero <<= SA->getValue();
379 KnownOne <<= SA->getValue();
380 KnownZero |= (1ULL << SA->getValue())-1; // low bits known zero.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000381 }
382 break;
Nate Begeman368e18d2006-02-16 21:11:51 +0000383 case ISD::SRL:
384 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
385 MVT::ValueType VT = Op.getValueType();
386 unsigned ShAmt = SA->getValue();
387
388 // Compute the new bits that are at the top now.
389 uint64_t HighBits = (1ULL << ShAmt)-1;
390 HighBits <<= MVT::getSizeInBits(VT) - ShAmt;
391 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
392
393 if (SimplifyDemandedBits(Op.getOperand(0),
394 (DemandedMask << ShAmt) & TypeMask,
395 KnownZero, KnownOne, TLO, Depth+1))
396 return true;
397 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
398 KnownZero &= TypeMask;
399 KnownOne &= TypeMask;
400 KnownZero >>= ShAmt;
401 KnownOne >>= ShAmt;
402 KnownZero |= HighBits; // high bits known zero.
403 }
404 break;
405 case ISD::SRA:
406 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
407 MVT::ValueType VT = Op.getValueType();
408 unsigned ShAmt = SA->getValue();
409
410 // Compute the new bits that are at the top now.
411 uint64_t HighBits = (1ULL << ShAmt)-1;
412 HighBits <<= MVT::getSizeInBits(VT) - ShAmt;
413 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
414
415 if (SimplifyDemandedBits(Op.getOperand(0),
416 (DemandedMask << ShAmt) & TypeMask,
417 KnownZero, KnownOne, TLO, Depth+1))
418 return true;
419 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
420 KnownZero &= TypeMask;
421 KnownOne &= TypeMask;
422 KnownZero >>= SA->getValue();
423 KnownOne >>= SA->getValue();
424
425 // Handle the sign bits.
426 uint64_t SignBit = MVT::getIntVTSignBit(VT);
427 SignBit >>= SA->getValue(); // Adjust to where it is now in the mask.
428
429 // If the input sign bit is known to be zero, or if none of the top bits
430 // are demanded, turn this into an unsigned shift right.
431 if ((KnownZero & SignBit) || (HighBits & ~DemandedMask) == HighBits) {
432 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, VT, Op.getOperand(0),
433 Op.getOperand(1)));
434 } else if (KnownOne & SignBit) { // New bits are known one.
435 KnownOne |= HighBits;
436 }
437 }
438 break;
439 case ISD::SIGN_EXTEND_INREG: {
440 MVT::ValueType VT = Op.getValueType();
441 MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
442
Chris Lattnerec665152006-02-26 23:36:02 +0000443 // Sign extension. Compute the demanded bits in the result that are not
Nate Begeman368e18d2006-02-16 21:11:51 +0000444 // present in the input.
Chris Lattnerec665152006-02-26 23:36:02 +0000445 uint64_t NewBits = ~MVT::getIntVTBitMask(EVT) & DemandedMask;
Nate Begeman368e18d2006-02-16 21:11:51 +0000446
Chris Lattnerec665152006-02-26 23:36:02 +0000447 // If none of the extended bits are demanded, eliminate the sextinreg.
448 if (NewBits == 0)
449 return TLO.CombineTo(Op, Op.getOperand(0));
450
Nate Begeman368e18d2006-02-16 21:11:51 +0000451 uint64_t InSignBit = MVT::getIntVTSignBit(EVT);
452 int64_t InputDemandedBits = DemandedMask & MVT::getIntVTBitMask(EVT);
453
Chris Lattnerec665152006-02-26 23:36:02 +0000454 // Since the sign extended bits are demanded, we know that the sign
Nate Begeman368e18d2006-02-16 21:11:51 +0000455 // bit is demanded.
Chris Lattnerec665152006-02-26 23:36:02 +0000456 InputDemandedBits |= InSignBit;
Nate Begeman368e18d2006-02-16 21:11:51 +0000457
458 if (SimplifyDemandedBits(Op.getOperand(0), InputDemandedBits,
459 KnownZero, KnownOne, TLO, Depth+1))
460 return true;
461 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
462
463 // If the sign bit of the input is known set or clear, then we know the
464 // top bits of the result.
465
Chris Lattnerec665152006-02-26 23:36:02 +0000466 // If the input sign bit is known zero, convert this into a zero extension.
467 if (KnownZero & InSignBit)
468 return TLO.CombineTo(Op,
469 TLO.DAG.getZeroExtendInReg(Op.getOperand(0), EVT));
470
471 if (KnownOne & InSignBit) { // Input sign bit known set
Nate Begeman368e18d2006-02-16 21:11:51 +0000472 KnownOne |= NewBits;
473 KnownZero &= ~NewBits;
Chris Lattnerec665152006-02-26 23:36:02 +0000474 } else { // Input sign bit unknown
Nate Begeman368e18d2006-02-16 21:11:51 +0000475 KnownZero &= ~NewBits;
476 KnownOne &= ~NewBits;
477 }
478 break;
479 }
Chris Lattnerec665152006-02-26 23:36:02 +0000480 case ISD::CTTZ:
481 case ISD::CTLZ:
482 case ISD::CTPOP: {
483 MVT::ValueType VT = Op.getValueType();
484 unsigned LowBits = Log2_32(MVT::getSizeInBits(VT))+1;
485 KnownZero = ~((1ULL << LowBits)-1) & MVT::getIntVTBitMask(VT);
486 KnownOne = 0;
487 break;
488 }
489 case ISD::ZEXTLOAD: {
490 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(3))->getVT();
491 KnownZero |= ~MVT::getIntVTBitMask(VT) & DemandedMask;
492 break;
493 }
494 case ISD::ZERO_EXTEND: {
495 uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType());
496
497 // If none of the top bits are demanded, convert this into an any_extend.
498 uint64_t NewBits = (~InMask) & DemandedMask;
499 if (NewBits == 0)
500 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ANY_EXTEND,
501 Op.getValueType(),
502 Op.getOperand(0)));
503
504 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & InMask,
505 KnownZero, KnownOne, TLO, Depth+1))
506 return true;
507 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
508 KnownZero |= NewBits;
509 break;
510 }
511 case ISD::SIGN_EXTEND: {
512 MVT::ValueType InVT = Op.getOperand(0).getValueType();
513 uint64_t InMask = MVT::getIntVTBitMask(InVT);
514 uint64_t InSignBit = MVT::getIntVTSignBit(InVT);
515 uint64_t NewBits = (~InMask) & DemandedMask;
516
517 // If none of the top bits are demanded, convert this into an any_extend.
518 if (NewBits == 0)
519 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ANY_EXTEND,Op.getValueType(),
520 Op.getOperand(0)));
521
522 // Since some of the sign extended bits are demanded, we know that the sign
523 // bit is demanded.
524 uint64_t InDemandedBits = DemandedMask & InMask;
525 InDemandedBits |= InSignBit;
526
527 if (SimplifyDemandedBits(Op.getOperand(0), InDemandedBits, KnownZero,
528 KnownOne, TLO, Depth+1))
529 return true;
530
531 // If the sign bit is known zero, convert this to a zero extend.
532 if (KnownZero & InSignBit)
533 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ZERO_EXTEND,
534 Op.getValueType(),
535 Op.getOperand(0)));
536
537 // If the sign bit is known one, the top bits match.
538 if (KnownOne & InSignBit) {
539 KnownOne |= NewBits;
540 KnownZero &= ~NewBits;
541 } else { // Otherwise, top bits aren't known.
542 KnownOne &= ~NewBits;
543 KnownZero &= ~NewBits;
544 }
545 break;
546 }
547 case ISD::ANY_EXTEND: {
548 uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType());
549 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & InMask,
550 KnownZero, KnownOne, TLO, Depth+1))
551 return true;
552 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
553 break;
554 }
555 case ISD::AssertZext: {
556 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
557 uint64_t InMask = MVT::getIntVTBitMask(VT);
558 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & InMask,
559 KnownZero, KnownOne, TLO, Depth+1))
560 return true;
561 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
562 KnownZero |= ~InMask & DemandedMask;
563 break;
564 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000565 case ISD::ADD:
566 if (ConstantSDNode *AA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
567 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask, KnownZero,
568 KnownOne, TLO, Depth+1))
569 return true;
570 // Compute the KnownOne/KnownZero masks for the constant, so we can set
571 // KnownZero appropriately if we're adding a constant that has all low
572 // bits cleared.
573 ComputeMaskedBits(Op.getOperand(1),
574 MVT::getIntVTBitMask(Op.getValueType()),
575 KnownZero2, KnownOne2, Depth+1);
576
577 uint64_t KnownZeroOut = std::min(CountTrailingZeros_64(~KnownZero),
578 CountTrailingZeros_64(~KnownZero2));
579 KnownZero = (1ULL << KnownZeroOut) - 1;
580 KnownOne = 0;
Nate Begeman003a2722006-02-18 02:43:25 +0000581
582 SDOperand SH = Op.getOperand(0);
583 // fold (add (shl x, c1), (shl c2, c1)) -> (shl (add x, c2), c1)
584 if (KnownZero && SH.getOpcode() == ISD::SHL && SH.Val->hasOneUse() &&
585 Op.Val->hasOneUse()) {
586 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(SH.getOperand(1))) {
587 MVT::ValueType VT = Op.getValueType();
588 unsigned ShiftAmt = SA->getValue();
589 uint64_t AddAmt = AA->getValue();
590 uint64_t AddShr = AddAmt >> ShiftAmt;
591 if (AddAmt == (AddShr << ShiftAmt)) {
592 SDOperand ADD = TLO.DAG.getNode(ISD::ADD, VT, SH.getOperand(0),
593 TLO.DAG.getConstant(AddShr, VT));
594 SDOperand SHL = TLO.DAG.getNode(ISD::SHL, VT, ADD,SH.getOperand(1));
595 return TLO.CombineTo(Op, SHL);
596 }
597 }
598 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000599 }
600 break;
Nate Begeman368e18d2006-02-16 21:11:51 +0000601 }
Chris Lattnerec665152006-02-26 23:36:02 +0000602
603 // If we know the value of all of the demanded bits, return this as a
604 // constant.
605 if ((DemandedMask & (KnownZero|KnownOne)) == DemandedMask)
606 return TLO.CombineTo(Op, TLO.DAG.getConstant(KnownOne, Op.getValueType()));
607
Nate Begeman368e18d2006-02-16 21:11:51 +0000608 return false;
609}
610
611/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
612/// this predicate to simplify operations downstream. Mask is known to be zero
613/// for bits that V cannot have.
614bool TargetLowering::MaskedValueIsZero(SDOperand Op, uint64_t Mask,
615 unsigned Depth) const {
616 uint64_t KnownZero, KnownOne;
617 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
618 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
619 return (KnownZero & Mask) == Mask;
620}
621
622/// ComputeMaskedBits - Determine which of the bits specified in Mask are
623/// known to be either zero or one and return them in the KnownZero/KnownOne
624/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
625/// processing.
626void TargetLowering::ComputeMaskedBits(SDOperand Op, uint64_t Mask,
627 uint64_t &KnownZero, uint64_t &KnownOne,
628 unsigned Depth) const {
629 KnownZero = KnownOne = 0; // Don't know anything.
630 if (Depth == 6 || Mask == 0)
631 return; // Limit search depth.
632
633 uint64_t KnownZero2, KnownOne2;
634
635 switch (Op.getOpcode()) {
636 case ISD::Constant:
637 // We know all of the bits for a constant!
638 KnownOne = cast<ConstantSDNode>(Op)->getValue() & Mask;
639 KnownZero = ~KnownOne & Mask;
640 return;
641 case ISD::AND:
642 // If either the LHS or the RHS are Zero, the result is zero.
643 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
644 Mask &= ~KnownZero;
645 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
646 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
647 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
648
649 // Output known-1 bits are only known if set in both the LHS & RHS.
650 KnownOne &= KnownOne2;
651 // Output known-0 are known to be clear if zero in either the LHS | RHS.
652 KnownZero |= KnownZero2;
653 return;
654 case ISD::OR:
655 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
656 Mask &= ~KnownOne;
657 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
658 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
659 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
660
661 // Output known-0 bits are only known if clear in both the LHS & RHS.
662 KnownZero &= KnownZero2;
663 // Output known-1 are known to be set if set in either the LHS | RHS.
664 KnownOne |= KnownOne2;
665 return;
666 case ISD::XOR: {
667 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
668 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
669 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
670 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
671
672 // Output known-0 bits are known if clear or set in both the LHS & RHS.
673 uint64_t KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
674 // Output known-1 are known to be set if set in only one of the LHS, RHS.
675 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
676 KnownZero = KnownZeroOut;
677 return;
678 }
679 case ISD::SELECT:
680 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
681 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
682 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
683 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
684
685 // Only known if known in both the LHS and RHS.
686 KnownOne &= KnownOne2;
687 KnownZero &= KnownZero2;
688 return;
689 case ISD::SELECT_CC:
690 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
691 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
692 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
693 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
694
695 // Only known if known in both the LHS and RHS.
696 KnownOne &= KnownOne2;
697 KnownZero &= KnownZero2;
698 return;
699 case ISD::SETCC:
700 // If we know the result of a setcc has the top bits zero, use this info.
701 if (getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult)
702 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
703 return;
704 case ISD::SHL:
705 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
706 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
707 Mask >>= SA->getValue();
708 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
709 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
710 KnownZero <<= SA->getValue();
711 KnownOne <<= SA->getValue();
712 KnownZero |= (1ULL << SA->getValue())-1; // low bits known zero.
713 }
Nate Begeman003a2722006-02-18 02:43:25 +0000714 return;
Nate Begeman368e18d2006-02-16 21:11:51 +0000715 case ISD::SRL:
716 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
717 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
718 uint64_t HighBits = (1ULL << SA->getValue())-1;
719 HighBits <<= MVT::getSizeInBits(Op.getValueType())-SA->getValue();
720 Mask <<= SA->getValue();
721 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Nate Begeman003a2722006-02-18 02:43:25 +0000722 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Nate Begeman368e18d2006-02-16 21:11:51 +0000723 KnownZero >>= SA->getValue();
724 KnownOne >>= SA->getValue();
725 KnownZero |= HighBits; // high bits known zero.
726 }
Nate Begeman003a2722006-02-18 02:43:25 +0000727 return;
Nate Begeman368e18d2006-02-16 21:11:51 +0000728 case ISD::SRA:
729 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
730 uint64_t HighBits = (1ULL << SA->getValue())-1;
731 HighBits <<= MVT::getSizeInBits(Op.getValueType())-SA->getValue();
732 Mask <<= SA->getValue();
733 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
734 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
735 KnownZero >>= SA->getValue();
736 KnownOne >>= SA->getValue();
737
738 // Handle the sign bits.
739 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(Op.getValueType())-1);
740 SignBit >>= SA->getValue(); // Adjust to where it is now in the mask.
741
742 if (KnownZero & SignBit) { // New bits are known zero.
743 KnownZero |= HighBits;
744 } else if (KnownOne & SignBit) { // New bits are known one.
745 KnownOne |= HighBits;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000746 }
747 }
Nate Begeman003a2722006-02-18 02:43:25 +0000748 return;
Chris Lattnerec665152006-02-26 23:36:02 +0000749 case ISD::SIGN_EXTEND_INREG: {
750 MVT::ValueType VT = Op.getValueType();
751 MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
752
753 // Sign extension. Compute the demanded bits in the result that are not
754 // present in the input.
755 uint64_t NewBits = ~MVT::getIntVTBitMask(EVT) & Mask;
756
757 uint64_t InSignBit = MVT::getIntVTSignBit(EVT);
758 int64_t InputDemandedBits = Mask & MVT::getIntVTBitMask(EVT);
759
760 // If the sign extended bits are demanded, we know that the sign
761 // bit is demanded.
762 if (NewBits)
763 InputDemandedBits |= InSignBit;
764
765 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
766 KnownZero, KnownOne, Depth+1);
767 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
768
769 // If the sign bit of the input is known set or clear, then we know the
770 // top bits of the result.
771 if (KnownZero & InSignBit) { // Input sign bit known clear
772 KnownZero |= NewBits;
773 KnownOne &= ~NewBits;
774 } else if (KnownOne & InSignBit) { // Input sign bit known set
775 KnownOne |= NewBits;
776 KnownZero &= ~NewBits;
777 } else { // Input sign bit unknown
778 KnownZero &= ~NewBits;
779 KnownOne &= ~NewBits;
780 }
781 return;
782 }
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000783 case ISD::CTTZ:
784 case ISD::CTLZ:
Nate Begeman368e18d2006-02-16 21:11:51 +0000785 case ISD::CTPOP: {
786 MVT::ValueType VT = Op.getValueType();
787 unsigned LowBits = Log2_32(MVT::getSizeInBits(VT))+1;
788 KnownZero = ~((1ULL << LowBits)-1) & MVT::getIntVTBitMask(VT);
789 KnownOne = 0;
790 return;
791 }
792 case ISD::ZEXTLOAD: {
Chris Lattnerec665152006-02-26 23:36:02 +0000793 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(3))->getVT();
794 KnownZero |= ~MVT::getIntVTBitMask(VT) & Mask;
Nate Begeman368e18d2006-02-16 21:11:51 +0000795 return;
796 }
797 case ISD::ZERO_EXTEND: {
Chris Lattnerec665152006-02-26 23:36:02 +0000798 uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType());
799 uint64_t NewBits = (~InMask) & Mask;
800 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
801 KnownOne, Depth+1);
802 KnownZero |= NewBits & Mask;
803 KnownOne &= ~NewBits;
804 return;
805 }
806 case ISD::SIGN_EXTEND: {
807 MVT::ValueType InVT = Op.getOperand(0).getValueType();
808 unsigned InBits = MVT::getSizeInBits(InVT);
809 uint64_t InMask = MVT::getIntVTBitMask(InVT);
810 uint64_t InSignBit = 1ULL << (InBits-1);
811 uint64_t NewBits = (~InMask) & Mask;
812 uint64_t InDemandedBits = Mask & InMask;
813
814 // If any of the sign extended bits are demanded, we know that the sign
815 // bit is demanded.
816 if (NewBits & Mask)
817 InDemandedBits |= InSignBit;
818
819 ComputeMaskedBits(Op.getOperand(0), InDemandedBits, KnownZero,
820 KnownOne, Depth+1);
821 // If the sign bit is known zero or one, the top bits match.
822 if (KnownZero & InSignBit) {
823 KnownZero |= NewBits;
824 KnownOne &= ~NewBits;
825 } else if (KnownOne & InSignBit) {
826 KnownOne |= NewBits;
827 KnownZero &= ~NewBits;
828 } else { // Otherwise, top bits aren't known.
829 KnownOne &= ~NewBits;
830 KnownZero &= ~NewBits;
831 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000832 return;
833 }
834 case ISD::ANY_EXTEND: {
Chris Lattnerec665152006-02-26 23:36:02 +0000835 MVT::ValueType VT = Op.getOperand(0).getValueType();
836 ComputeMaskedBits(Op.getOperand(0), Mask & MVT::getIntVTBitMask(VT),
837 KnownZero, KnownOne, Depth+1);
Nate Begeman368e18d2006-02-16 21:11:51 +0000838 return;
839 }
840 case ISD::AssertZext: {
Chris Lattnerec665152006-02-26 23:36:02 +0000841 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
842 uint64_t InMask = MVT::getIntVTBitMask(VT);
843 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
844 KnownOne, Depth+1);
845 KnownZero |= (~InMask) & Mask;
Nate Begeman368e18d2006-02-16 21:11:51 +0000846 return;
847 }
848 case ISD::ADD: {
849 // If either the LHS or the RHS are Zero, the result is zero.
850 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
851 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
852 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
853 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
854
855 // Output known-0 bits are known if clear or set in both the low clear bits
856 // common to both LHS & RHS;
857 uint64_t KnownZeroOut = std::min(CountTrailingZeros_64(~KnownZero),
858 CountTrailingZeros_64(~KnownZero2));
859
860 KnownZero = (1ULL << KnownZeroOut) - 1;
861 KnownOne = 0;
862 return;
863 }
864 case ISD::SUB:
865 // We know that the top bits of C-X are clear if X contains less bits
866 // than C (i.e. no wrap-around can happen). For example, 20-X is
Chris Lattnerec665152006-02-26 23:36:02 +0000867 // positive if we can prove that X is >= 0 and < 16. Remember to update
868 // SimplifyDemandedBits if/when this is implemented.
Nate Begeman003a2722006-02-18 02:43:25 +0000869 return;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000870 default:
871 // Allow the target to implement this method for its nodes.
872 if (Op.getOpcode() >= ISD::BUILTIN_OP_END)
Nate Begeman368e18d2006-02-16 21:11:51 +0000873 computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne);
Nate Begeman003a2722006-02-18 02:43:25 +0000874 return;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000875 }
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000876}
877
Nate Begeman368e18d2006-02-16 21:11:51 +0000878/// computeMaskedBitsForTargetNode - Determine which of the bits specified
879/// in Mask are known to be either zero or one and return them in the
880/// KnownZero/KnownOne bitsets.
881void TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
882 uint64_t Mask,
883 uint64_t &KnownZero,
884 uint64_t &KnownOne,
885 unsigned Depth) const {
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000886 assert(Op.getOpcode() >= ISD::BUILTIN_OP_END &&
887 "Should use MaskedValueIsZero if you don't know whether Op"
888 " is a target node!");
Nate Begeman368e18d2006-02-16 21:11:51 +0000889 KnownZero = 0;
890 KnownOne = 0;
Evan Cheng3a03ebb2005-12-21 23:05:39 +0000891}
Chris Lattner4ccb0702006-01-26 20:37:03 +0000892
Chris Lattnereb8146b2006-02-04 02:13:02 +0000893//===----------------------------------------------------------------------===//
894// Inline Assembler Implementation Methods
895//===----------------------------------------------------------------------===//
896
897TargetLowering::ConstraintType
898TargetLowering::getConstraintType(char ConstraintLetter) const {
899 // FIXME: lots more standard ones to handle.
900 switch (ConstraintLetter) {
901 default: return C_Unknown;
902 case 'r': return C_RegisterClass;
Chris Lattner2b7401e2006-02-24 01:10:46 +0000903 case 'm': // memory
904 case 'o': // offsetable
905 case 'V': // not offsetable
906 return C_Memory;
Chris Lattnereb8146b2006-02-04 02:13:02 +0000907 case 'i': // Simple Integer or Relocatable Constant
908 case 'n': // Simple Integer
909 case 's': // Relocatable Constant
910 case 'I': // Target registers.
911 case 'J':
912 case 'K':
913 case 'L':
914 case 'M':
915 case 'N':
916 case 'O':
Chris Lattner2b7401e2006-02-24 01:10:46 +0000917 case 'P':
918 return C_Other;
Chris Lattnereb8146b2006-02-04 02:13:02 +0000919 }
920}
921
922bool TargetLowering::isOperandValidForConstraint(SDOperand Op,
923 char ConstraintLetter) {
924 switch (ConstraintLetter) {
925 default: return false;
926 case 'i': // Simple Integer or Relocatable Constant
927 case 'n': // Simple Integer
928 case 's': // Relocatable Constant
929 return true; // FIXME: not right.
930 }
931}
932
933
Chris Lattner4ccb0702006-01-26 20:37:03 +0000934std::vector<unsigned> TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +0000935getRegClassForInlineAsmConstraint(const std::string &Constraint,
936 MVT::ValueType VT) const {
937 return std::vector<unsigned>();
938}
939
940
941std::pair<unsigned, const TargetRegisterClass*> TargetLowering::
Chris Lattner4217ca8dc2006-02-21 23:11:00 +0000942getRegForInlineAsmConstraint(const std::string &Constraint,
943 MVT::ValueType VT) const {
Chris Lattner1efa40f2006-02-22 00:56:39 +0000944 if (Constraint[0] != '{')
945 return std::pair<unsigned, const TargetRegisterClass*>(0, 0);
Chris Lattnera55079a2006-02-01 01:29:47 +0000946 assert(*(Constraint.end()-1) == '}' && "Not a brace enclosed constraint?");
947
948 // Remove the braces from around the name.
949 std::string RegName(Constraint.begin()+1, Constraint.end()-1);
Chris Lattner1efa40f2006-02-22 00:56:39 +0000950
951 // Figure out which register class contains this reg.
Chris Lattner4ccb0702006-01-26 20:37:03 +0000952 const MRegisterInfo *RI = TM.getRegisterInfo();
Chris Lattner1efa40f2006-02-22 00:56:39 +0000953 for (MRegisterInfo::regclass_iterator RCI = RI->regclass_begin(),
954 E = RI->regclass_end(); RCI != E; ++RCI) {
955 const TargetRegisterClass *RC = *RCI;
Chris Lattnerb3befd42006-02-22 23:00:51 +0000956
957 // If none of the the value types for this register class are valid, we
958 // can't use it. For example, 64-bit reg classes on 32-bit targets.
959 bool isLegal = false;
960 for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
961 I != E; ++I) {
962 if (isTypeLegal(*I)) {
963 isLegal = true;
964 break;
965 }
966 }
967
968 if (!isLegal) continue;
969
Chris Lattner1efa40f2006-02-22 00:56:39 +0000970 for (TargetRegisterClass::iterator I = RC->begin(), E = RC->end();
971 I != E; ++I) {
Chris Lattnerb3befd42006-02-22 23:00:51 +0000972 if (StringsEqualNoCase(RegName, RI->get(*I).Name))
Chris Lattner1efa40f2006-02-22 00:56:39 +0000973 return std::make_pair(*I, RC);
Chris Lattner1efa40f2006-02-22 00:56:39 +0000974 }
Chris Lattner4ccb0702006-01-26 20:37:03 +0000975 }
Chris Lattnera55079a2006-02-01 01:29:47 +0000976
Chris Lattner1efa40f2006-02-22 00:56:39 +0000977 return std::pair<unsigned, const TargetRegisterClass*>(0, 0);
Chris Lattner4ccb0702006-01-26 20:37:03 +0000978}