blob: 71c7d90f7d6380646582d074598a5d460a223f6f [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) {
144 // FIXME: ISD::SELECT
145 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;
202 return false;
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?");
209 // If something is known zero on the RHS, the bits aren't demanded on the
210 // LHS.
211 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & ~KnownZero,
212 KnownZero2, KnownOne2, TLO, Depth+1))
213 return true;
214 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
215
216 // If all of the demanded bits are known one on one side, return the other.
217 // These bits cannot contribute to the result of the 'and'.
218 if ((DemandedMask & ~KnownZero2 & KnownOne)==(DemandedMask & ~KnownZero2))
219 return TLO.CombineTo(Op, Op.getOperand(0));
220 if ((DemandedMask & ~KnownZero & KnownOne2)==(DemandedMask & ~KnownZero))
221 return TLO.CombineTo(Op, Op.getOperand(1));
222 // If all of the demanded bits in the inputs are known zeros, return zero.
223 if ((DemandedMask & (KnownZero|KnownZero2)) == DemandedMask)
224 return TLO.CombineTo(Op, TLO.DAG.getConstant(0, Op.getValueType()));
225 // If the RHS is a constant, see if we can simplify it.
226 if (TLO.ShrinkDemandedConstant(Op, DemandedMask & ~KnownZero2))
227 return true;
228
229 // Output known-1 bits are only known if set in both the LHS & RHS.
230 KnownOne &= KnownOne2;
231 // Output known-0 are known to be clear if zero in either the LHS | RHS.
232 KnownZero |= KnownZero2;
233 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000234 case ISD::OR:
Nate Begeman368e18d2006-02-16 21:11:51 +0000235 if (SimplifyDemandedBits(Op.getOperand(1), DemandedMask, KnownZero,
236 KnownOne, TLO, Depth+1))
237 return true;
238 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
239 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & ~KnownOne,
240 KnownZero2, KnownOne2, TLO, Depth+1))
241 return true;
242 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
243
244 // If all of the demanded bits are known zero on one side, return the other.
245 // These bits cannot contribute to the result of the 'or'.
246 if ((DemandedMask & ~KnownOne2 & KnownZero) == DemandedMask & ~KnownOne2)
247 return TLO.CombineTo(Op, Op.getOperand(0));
248 if ((DemandedMask & ~KnownOne & KnownZero2) == DemandedMask & ~KnownOne)
249 return TLO.CombineTo(Op, Op.getOperand(1));
250 // If all of the potentially set bits on one side are known to be set on
251 // the other side, just use the 'other' side.
252 if ((DemandedMask & (~KnownZero) & KnownOne2) ==
253 (DemandedMask & (~KnownZero)))
254 return TLO.CombineTo(Op, Op.getOperand(0));
255 if ((DemandedMask & (~KnownZero2) & KnownOne) ==
256 (DemandedMask & (~KnownZero2)))
257 return TLO.CombineTo(Op, Op.getOperand(1));
258 // If the RHS is a constant, see if we can simplify it.
259 if (TLO.ShrinkDemandedConstant(Op, DemandedMask))
260 return true;
261
262 // Output known-0 bits are only known if clear in both the LHS & RHS.
263 KnownZero &= KnownZero2;
264 // Output known-1 are known to be set if set in either the LHS | RHS.
265 KnownOne |= KnownOne2;
266 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000267 case ISD::XOR:
Nate Begeman368e18d2006-02-16 21:11:51 +0000268 if (SimplifyDemandedBits(Op.getOperand(1), DemandedMask, KnownZero,
269 KnownOne, TLO, Depth+1))
270 return true;
271 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
272 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask, KnownZero2,
273 KnownOne2, TLO, Depth+1))
274 return true;
275 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
276
277 // If all of the demanded bits are known zero on one side, return the other.
278 // These bits cannot contribute to the result of the 'xor'.
279 if ((DemandedMask & KnownZero) == DemandedMask)
280 return TLO.CombineTo(Op, Op.getOperand(0));
281 if ((DemandedMask & KnownZero2) == DemandedMask)
282 return TLO.CombineTo(Op, Op.getOperand(1));
283
284 // Output known-0 bits are known if clear or set in both the LHS & RHS.
285 KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
286 // Output known-1 are known to be set if set in only one of the LHS, RHS.
287 KnownOneOut = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
288
289 // If all of the unknown bits are known to be zero on one side or the other
290 // (but not both) turn this into an *inclusive* or.
291 // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
292 if (uint64_t UnknownBits = DemandedMask & ~(KnownZeroOut|KnownOneOut))
293 if ((UnknownBits & (KnownZero|KnownZero2)) == UnknownBits)
294 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::OR, Op.getValueType(),
295 Op.getOperand(0),
296 Op.getOperand(1)));
297 // If all of the demanded bits on one side are known, and all of the set
298 // bits on that side are also known to be set on the other side, turn this
299 // into an AND, as we know the bits will be cleared.
300 // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
301 if ((DemandedMask & (KnownZero|KnownOne)) == DemandedMask) { // all known
302 if ((KnownOne & KnownOne2) == KnownOne) {
303 MVT::ValueType VT = Op.getValueType();
304 SDOperand ANDC = TLO.DAG.getConstant(~KnownOne & DemandedMask, VT);
305 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::AND, VT, Op.getOperand(0),
306 ANDC));
307 }
308 }
309
310 // If the RHS is a constant, see if we can simplify it.
311 // FIXME: for XOR, we prefer to force bits to 1 if they will make a -1.
312 if (TLO.ShrinkDemandedConstant(Op, DemandedMask))
313 return true;
314
315 KnownZero = KnownZeroOut;
316 KnownOne = KnownOneOut;
317 break;
318 case ISD::SETCC:
319 // If we know the result of a setcc has the top bits zero, use this info.
320 if (getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult)
321 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
322 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000323 case ISD::SELECT:
Nate Begeman368e18d2006-02-16 21:11:51 +0000324 if (SimplifyDemandedBits(Op.getOperand(2), DemandedMask, KnownZero,
325 KnownOne, TLO, Depth+1))
326 return true;
327 if (SimplifyDemandedBits(Op.getOperand(1), DemandedMask, KnownZero2,
328 KnownOne2, TLO, Depth+1))
329 return true;
330 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
331 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
332
333 // If the operands are constants, see if we can simplify them.
334 if (TLO.ShrinkDemandedConstant(Op, DemandedMask))
335 return true;
336
337 // Only known if known in both the LHS and RHS.
338 KnownOne &= KnownOne2;
339 KnownZero &= KnownZero2;
340 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000341 case ISD::SHL:
Nate Begeman368e18d2006-02-16 21:11:51 +0000342 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
343 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask >> SA->getValue(),
344 KnownZero, KnownOne, TLO, Depth+1))
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000345 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000346 KnownZero <<= SA->getValue();
347 KnownOne <<= SA->getValue();
348 KnownZero |= (1ULL << SA->getValue())-1; // low bits known zero.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000349 }
350 break;
Nate Begeman368e18d2006-02-16 21:11:51 +0000351 case ISD::SRL:
352 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
353 MVT::ValueType VT = Op.getValueType();
354 unsigned ShAmt = SA->getValue();
355
356 // Compute the new bits that are at the top now.
357 uint64_t HighBits = (1ULL << ShAmt)-1;
358 HighBits <<= MVT::getSizeInBits(VT) - ShAmt;
359 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
360
361 if (SimplifyDemandedBits(Op.getOperand(0),
362 (DemandedMask << ShAmt) & TypeMask,
363 KnownZero, KnownOne, TLO, Depth+1))
364 return true;
365 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
366 KnownZero &= TypeMask;
367 KnownOne &= TypeMask;
368 KnownZero >>= ShAmt;
369 KnownOne >>= ShAmt;
370 KnownZero |= HighBits; // high bits known zero.
371 }
372 break;
373 case ISD::SRA:
374 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
375 MVT::ValueType VT = Op.getValueType();
376 unsigned ShAmt = SA->getValue();
377
378 // Compute the new bits that are at the top now.
379 uint64_t HighBits = (1ULL << ShAmt)-1;
380 HighBits <<= MVT::getSizeInBits(VT) - ShAmt;
381 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
382
383 if (SimplifyDemandedBits(Op.getOperand(0),
384 (DemandedMask << ShAmt) & TypeMask,
385 KnownZero, KnownOne, TLO, Depth+1))
386 return true;
387 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
388 KnownZero &= TypeMask;
389 KnownOne &= TypeMask;
390 KnownZero >>= SA->getValue();
391 KnownOne >>= SA->getValue();
392
393 // Handle the sign bits.
394 uint64_t SignBit = MVT::getIntVTSignBit(VT);
395 SignBit >>= SA->getValue(); // Adjust to where it is now in the mask.
396
397 // If the input sign bit is known to be zero, or if none of the top bits
398 // are demanded, turn this into an unsigned shift right.
399 if ((KnownZero & SignBit) || (HighBits & ~DemandedMask) == HighBits) {
400 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, VT, Op.getOperand(0),
401 Op.getOperand(1)));
402 } else if (KnownOne & SignBit) { // New bits are known one.
403 KnownOne |= HighBits;
404 }
405 }
406 break;
407 case ISD::SIGN_EXTEND_INREG: {
408 MVT::ValueType VT = Op.getValueType();
409 MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
410
411 // Sign or Zero extension. Compute the bits in the result that are not
412 // present in the input.
413 uint64_t NotIn = ~MVT::getIntVTBitMask(EVT);
414 uint64_t NewBits = MVT::getIntVTBitMask(VT) & NotIn;
415
416 // Sign extension.
417 uint64_t InSignBit = MVT::getIntVTSignBit(EVT);
418 int64_t InputDemandedBits = DemandedMask & MVT::getIntVTBitMask(EVT);
419
420 // If any of the sign extended bits are demanded, we know that the sign
421 // bit is demanded.
422 if (NewBits & DemandedMask)
423 InputDemandedBits |= InSignBit;
424
425 if (SimplifyDemandedBits(Op.getOperand(0), InputDemandedBits,
426 KnownZero, KnownOne, TLO, Depth+1))
427 return true;
428 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
429
430 // If the sign bit of the input is known set or clear, then we know the
431 // top bits of the result.
432
433 // If the input sign bit is known zero, or if the NewBits are not demanded
434 // convert this into a zero extension.
435 if ((KnownZero & InSignBit) || (NewBits & ~DemandedMask) == NewBits) {
436 return TLO.CombineTo(Op, Op.getOperand(0));
437 } else if (KnownOne & InSignBit) { // Input sign bit known set
438 KnownOne |= NewBits;
439 KnownZero &= ~NewBits;
440 } else { // Input sign bit unknown
441 KnownZero &= ~NewBits;
442 KnownOne &= ~NewBits;
443 }
444 break;
445 }
446 case ISD::ADD:
447 if (ConstantSDNode *AA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
448 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask, KnownZero,
449 KnownOne, TLO, Depth+1))
450 return true;
451 // Compute the KnownOne/KnownZero masks for the constant, so we can set
452 // KnownZero appropriately if we're adding a constant that has all low
453 // bits cleared.
454 ComputeMaskedBits(Op.getOperand(1),
455 MVT::getIntVTBitMask(Op.getValueType()),
456 KnownZero2, KnownOne2, Depth+1);
457
458 uint64_t KnownZeroOut = std::min(CountTrailingZeros_64(~KnownZero),
459 CountTrailingZeros_64(~KnownZero2));
460 KnownZero = (1ULL << KnownZeroOut) - 1;
461 KnownOne = 0;
462 }
463 break;
464 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 }
474 return false;
475}
476
477/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
478/// this predicate to simplify operations downstream. Mask is known to be zero
479/// for bits that V cannot have.
480bool TargetLowering::MaskedValueIsZero(SDOperand Op, uint64_t Mask,
481 unsigned Depth) const {
482 uint64_t KnownZero, KnownOne;
483 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
484 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
485 return (KnownZero & Mask) == Mask;
486}
487
488/// ComputeMaskedBits - Determine which of the bits specified in Mask are
489/// known to be either zero or one and return them in the KnownZero/KnownOne
490/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
491/// processing.
492void TargetLowering::ComputeMaskedBits(SDOperand Op, uint64_t Mask,
493 uint64_t &KnownZero, uint64_t &KnownOne,
494 unsigned Depth) const {
495 KnownZero = KnownOne = 0; // Don't know anything.
496 if (Depth == 6 || Mask == 0)
497 return; // Limit search depth.
498
499 uint64_t KnownZero2, KnownOne2;
500
501 switch (Op.getOpcode()) {
502 case ISD::Constant:
503 // We know all of the bits for a constant!
504 KnownOne = cast<ConstantSDNode>(Op)->getValue() & Mask;
505 KnownZero = ~KnownOne & Mask;
506 return;
507 case ISD::AND:
508 // If either the LHS or the RHS are Zero, the result is zero.
509 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
510 Mask &= ~KnownZero;
511 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
512 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
513 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
514
515 // Output known-1 bits are only known if set in both the LHS & RHS.
516 KnownOne &= KnownOne2;
517 // Output known-0 are known to be clear if zero in either the LHS | RHS.
518 KnownZero |= KnownZero2;
519 return;
520 case ISD::OR:
521 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
522 Mask &= ~KnownOne;
523 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
524 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
525 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
526
527 // Output known-0 bits are only known if clear in both the LHS & RHS.
528 KnownZero &= KnownZero2;
529 // Output known-1 are known to be set if set in either the LHS | RHS.
530 KnownOne |= KnownOne2;
531 return;
532 case ISD::XOR: {
533 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
534 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
535 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
536 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
537
538 // Output known-0 bits are known if clear or set in both the LHS & RHS.
539 uint64_t KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
540 // Output known-1 are known to be set if set in only one of the LHS, RHS.
541 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
542 KnownZero = KnownZeroOut;
543 return;
544 }
545 case ISD::SELECT:
546 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
547 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
548 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
549 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
550
551 // Only known if known in both the LHS and RHS.
552 KnownOne &= KnownOne2;
553 KnownZero &= KnownZero2;
554 return;
555 case ISD::SELECT_CC:
556 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
557 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
558 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
559 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
560
561 // Only known if known in both the LHS and RHS.
562 KnownOne &= KnownOne2;
563 KnownZero &= KnownZero2;
564 return;
565 case ISD::SETCC:
566 // If we know the result of a setcc has the top bits zero, use this info.
567 if (getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult)
568 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
569 return;
570 case ISD::SHL:
571 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
572 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
573 Mask >>= SA->getValue();
574 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
575 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
576 KnownZero <<= SA->getValue();
577 KnownOne <<= SA->getValue();
578 KnownZero |= (1ULL << SA->getValue())-1; // low bits known zero.
579 }
580 break;
581 case ISD::SRL:
582 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
583 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
584 uint64_t HighBits = (1ULL << SA->getValue())-1;
585 HighBits <<= MVT::getSizeInBits(Op.getValueType())-SA->getValue();
586 Mask <<= SA->getValue();
587 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
588 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
589 KnownZero >>= SA->getValue();
590 KnownOne >>= SA->getValue();
591 KnownZero |= HighBits; // high bits known zero.
592 }
593 break;
594 case ISD::SRA:
595 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
596 uint64_t HighBits = (1ULL << SA->getValue())-1;
597 HighBits <<= MVT::getSizeInBits(Op.getValueType())-SA->getValue();
598 Mask <<= SA->getValue();
599 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
600 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
601 KnownZero >>= SA->getValue();
602 KnownOne >>= SA->getValue();
603
604 // Handle the sign bits.
605 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(Op.getValueType())-1);
606 SignBit >>= SA->getValue(); // Adjust to where it is now in the mask.
607
608 if (KnownZero & SignBit) { // New bits are known zero.
609 KnownZero |= HighBits;
610 } else if (KnownOne & SignBit) { // New bits are known one.
611 KnownOne |= HighBits;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000612 }
613 }
614 break;
615 case ISD::CTTZ:
616 case ISD::CTLZ:
Nate Begeman368e18d2006-02-16 21:11:51 +0000617 case ISD::CTPOP: {
618 MVT::ValueType VT = Op.getValueType();
619 unsigned LowBits = Log2_32(MVT::getSizeInBits(VT))+1;
620 KnownZero = ~((1ULL << LowBits)-1) & MVT::getIntVTBitMask(VT);
621 KnownOne = 0;
622 return;
623 }
624 case ISD::ZEXTLOAD: {
625 unsigned SrcBits =
626 MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(3))->getVT());
627 KnownZero |= ~((1ULL << SrcBits)-1);
628 return;
629 }
630 case ISD::ZERO_EXTEND: {
631 unsigned SrcBits =
632 MVT::getSizeInBits(Op.getOperand(0).getValueType());
633 KnownZero |= ~((1ULL << SrcBits)-1);
634 return;
635 }
636 case ISD::ANY_EXTEND: {
637 unsigned SrcBits =
638 MVT::getSizeInBits(Op.getOperand(0).getValueType());
639 KnownZero &= ((1ULL << SrcBits)-1);
640 KnownOne &= ((1ULL << SrcBits)-1);
641 return;
642 }
643 case ISD::AssertZext: {
644 unsigned SrcBits =
645 MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
646 KnownZero |= ~((1ULL << SrcBits)-1);
647 return;
648 }
649 case ISD::ADD: {
650 // If either the LHS or the RHS are Zero, the result is zero.
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 low clear bits
657 // common to both LHS & RHS;
658 uint64_t KnownZeroOut = std::min(CountTrailingZeros_64(~KnownZero),
659 CountTrailingZeros_64(~KnownZero2));
660
661 KnownZero = (1ULL << KnownZeroOut) - 1;
662 KnownOne = 0;
663 return;
664 }
665 case ISD::SUB:
666 // We know that the top bits of C-X are clear if X contains less bits
667 // than C (i.e. no wrap-around can happen). For example, 20-X is
668 // positive if we can prove that X is >= 0 and < 16.
669 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000670 default:
671 // Allow the target to implement this method for its nodes.
672 if (Op.getOpcode() >= ISD::BUILTIN_OP_END)
Nate Begeman368e18d2006-02-16 21:11:51 +0000673 computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000674 break;
675 }
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000676}
677
Nate Begeman368e18d2006-02-16 21:11:51 +0000678/// computeMaskedBitsForTargetNode - Determine which of the bits specified
679/// in Mask are known to be either zero or one and return them in the
680/// KnownZero/KnownOne bitsets.
681void TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
682 uint64_t Mask,
683 uint64_t &KnownZero,
684 uint64_t &KnownOne,
685 unsigned Depth) const {
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000686 assert(Op.getOpcode() >= ISD::BUILTIN_OP_END &&
687 "Should use MaskedValueIsZero if you don't know whether Op"
688 " is a target node!");
Nate Begeman368e18d2006-02-16 21:11:51 +0000689 KnownZero = 0;
690 KnownOne = 0;
Evan Cheng3a03ebb2005-12-21 23:05:39 +0000691}
Chris Lattner4ccb0702006-01-26 20:37:03 +0000692
Chris Lattnereb8146b2006-02-04 02:13:02 +0000693//===----------------------------------------------------------------------===//
694// Inline Assembler Implementation Methods
695//===----------------------------------------------------------------------===//
696
697TargetLowering::ConstraintType
698TargetLowering::getConstraintType(char ConstraintLetter) const {
699 // FIXME: lots more standard ones to handle.
700 switch (ConstraintLetter) {
701 default: return C_Unknown;
702 case 'r': return C_RegisterClass;
703 case 'i': // Simple Integer or Relocatable Constant
704 case 'n': // Simple Integer
705 case 's': // Relocatable Constant
706 case 'I': // Target registers.
707 case 'J':
708 case 'K':
709 case 'L':
710 case 'M':
711 case 'N':
712 case 'O':
713 case 'P': return C_Other;
714 }
715}
716
717bool TargetLowering::isOperandValidForConstraint(SDOperand Op,
718 char ConstraintLetter) {
719 switch (ConstraintLetter) {
720 default: return false;
721 case 'i': // Simple Integer or Relocatable Constant
722 case 'n': // Simple Integer
723 case 's': // Relocatable Constant
724 return true; // FIXME: not right.
725 }
726}
727
728
Chris Lattner4ccb0702006-01-26 20:37:03 +0000729std::vector<unsigned> TargetLowering::
730getRegForInlineAsmConstraint(const std::string &Constraint) const {
Chris Lattnera55079a2006-02-01 01:29:47 +0000731 // Not a physreg, must not be a register reference or something.
732 if (Constraint[0] != '{') return std::vector<unsigned>();
733 assert(*(Constraint.end()-1) == '}' && "Not a brace enclosed constraint?");
734
735 // Remove the braces from around the name.
736 std::string RegName(Constraint.begin()+1, Constraint.end()-1);
737
Chris Lattner4ccb0702006-01-26 20:37:03 +0000738 // Scan to see if this constraint is a register name.
739 const MRegisterInfo *RI = TM.getRegisterInfo();
740 for (unsigned i = 1, e = RI->getNumRegs(); i != e; ++i) {
741 if (const char *Name = RI->get(i).Name)
Chris Lattnera55079a2006-02-01 01:29:47 +0000742 if (StringsEqualNoCase(RegName, Name))
Chris Lattner4ccb0702006-01-26 20:37:03 +0000743 return std::vector<unsigned>(1, i);
744 }
Chris Lattnera55079a2006-02-01 01:29:47 +0000745
746 // Unknown physreg.
Chris Lattner4ccb0702006-01-26 20:37:03 +0000747 return std::vector<unsigned>();
748}
749