blob: 95c327c8b055c516d5de0cd9c4af8b080d33f9e5 [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//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// 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"
Jakob Stoklund Olesene3ee49f2012-05-04 02:19:22 +000015#include "llvm/ADT/BitVector.h"
Owen Anderson718cb662007-09-07 04:06:50 +000016#include "llvm/ADT/STLExtras.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000017#include "llvm/CodeGen/Analysis.h"
18#include "llvm/CodeGen/MachineFrameInfo.h"
19#include "llvm/CodeGen/MachineFunction.h"
20#include "llvm/CodeGen/MachineJumpTableInfo.h"
21#include "llvm/CodeGen/SelectionDAG.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000022#include "llvm/IR/DataLayout.h"
23#include "llvm/IR/DerivedTypes.h"
24#include "llvm/IR/GlobalVariable.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000025#include "llvm/MC/MCAsmInfo.h"
26#include "llvm/MC/MCExpr.h"
Nadav Rotemb6fbec32011-06-01 12:51:46 +000027#include "llvm/Support/CommandLine.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000028#include "llvm/Support/ErrorHandling.h"
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +000029#include "llvm/Support/MathExtras.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000030#include "llvm/Target/TargetLoweringObjectFile.h"
31#include "llvm/Target/TargetMachine.h"
32#include "llvm/Target/TargetRegisterInfo.h"
Nick Lewycky476b2422010-12-19 20:43:38 +000033#include <cctype>
Chris Lattner310968c2005-01-07 07:44:53 +000034using namespace llvm;
35
Chris Lattnerf0144122009-07-28 03:13:23 +000036/// NOTE: The constructor takes ownership of TLOF.
Dan Gohmanf0757b02010-04-21 01:34:56 +000037TargetLowering::TargetLowering(const TargetMachine &tm,
38 const TargetLoweringObjectFile *tlof)
Benjamin Kramer69e42db2013-01-11 20:05:37 +000039 : TargetLoweringBase(tm, tlof) {}
Chris Lattnercba82f92005-01-16 07:28:11 +000040
Evan Cheng72261582005-12-20 06:22:03 +000041const char *TargetLowering::getTargetNodeName(unsigned Opcode) const {
42 return NULL;
43}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000044
Tim Northover2c8cf4b2013-01-09 13:18:15 +000045/// Check whether a given call node is in tail position within its function. If
46/// so, it sets Chain to the input chain of the tail call.
47bool TargetLowering::isInTailCallPosition(SelectionDAG &DAG, SDNode *Node,
48 SDValue &Chain) const {
49 const Function *F = DAG.getMachineFunction().getFunction();
50
51 // Conservatively require the attributes of the call to match those of
52 // the return. Ignore noalias because it doesn't affect the call sequence.
Bill Wendling1b0c54f2013-01-18 21:53:16 +000053 AttributeSet CallerAttrs = F->getAttributes();
54 if (AttrBuilder(CallerAttrs, AttributeSet::ReturnIndex)
Tim Northover2c8cf4b2013-01-09 13:18:15 +000055 .removeAttribute(Attribute::NoAlias).hasAttributes())
56 return false;
57
58 // It's not safe to eliminate the sign / zero extension of the return value.
Bill Wendling1b0c54f2013-01-18 21:53:16 +000059 if (CallerAttrs.hasAttribute(AttributeSet::ReturnIndex, Attribute::ZExt) ||
60 CallerAttrs.hasAttribute(AttributeSet::ReturnIndex, Attribute::SExt))
Tim Northover2c8cf4b2013-01-09 13:18:15 +000061 return false;
62
63 // Check if the only use is a function return node.
64 return isUsedByReturnOnly(Node, Chain);
65}
66
67
68/// Generate a libcall taking the given operands as arguments and returning a
69/// result of type RetVT.
70SDValue TargetLowering::makeLibCall(SelectionDAG &DAG,
71 RTLIB::Libcall LC, EVT RetVT,
72 const SDValue *Ops, unsigned NumOps,
73 bool isSigned, DebugLoc dl) const {
74 TargetLowering::ArgListTy Args;
75 Args.reserve(NumOps);
76
77 TargetLowering::ArgListEntry Entry;
78 for (unsigned i = 0; i != NumOps; ++i) {
79 Entry.Node = Ops[i];
80 Entry.Ty = Entry.Node.getValueType().getTypeForEVT(*DAG.getContext());
81 Entry.isSExt = isSigned;
82 Entry.isZExt = !isSigned;
83 Args.push_back(Entry);
84 }
85 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC), getPointerTy());
86
87 Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
88 TargetLowering::
89 CallLoweringInfo CLI(DAG.getEntryNode(), RetTy, isSigned, !isSigned, false,
90 false, 0, getLibcallCallingConv(LC),
91 /*isTailCall=*/false,
92 /*doesNotReturn=*/false, /*isReturnValueUsed=*/true,
93 Callee, Args, DAG, dl);
94 std::pair<SDValue,SDValue> CallInfo = LowerCallTo(CLI);
95
96 return CallInfo.first;
97}
98
99
100/// SoftenSetCCOperands - Soften the operands of a comparison. This code is
101/// shared among BR_CC, SELECT_CC, and SETCC handlers.
102void TargetLowering::softenSetCCOperands(SelectionDAG &DAG, EVT VT,
103 SDValue &NewLHS, SDValue &NewRHS,
104 ISD::CondCode &CCCode,
105 DebugLoc dl) const {
106 assert((VT == MVT::f32 || VT == MVT::f64 || VT == MVT::f128)
107 && "Unsupported setcc type!");
108
109 // Expand into one or more soft-fp libcall(s).
110 RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL;
111 switch (CCCode) {
112 case ISD::SETEQ:
113 case ISD::SETOEQ:
114 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 :
115 (VT == MVT::f64) ? RTLIB::OEQ_F64 : RTLIB::OEQ_F128;
116 break;
117 case ISD::SETNE:
118 case ISD::SETUNE:
119 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 :
120 (VT == MVT::f64) ? RTLIB::UNE_F64 : RTLIB::UNE_F128;
121 break;
122 case ISD::SETGE:
123 case ISD::SETOGE:
124 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 :
125 (VT == MVT::f64) ? RTLIB::OGE_F64 : RTLIB::OGE_F128;
126 break;
127 case ISD::SETLT:
128 case ISD::SETOLT:
129 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 :
130 (VT == MVT::f64) ? RTLIB::OLT_F64 : RTLIB::OLT_F128;
131 break;
132 case ISD::SETLE:
133 case ISD::SETOLE:
134 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 :
135 (VT == MVT::f64) ? RTLIB::OLE_F64 : RTLIB::OLE_F128;
136 break;
137 case ISD::SETGT:
138 case ISD::SETOGT:
139 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 :
140 (VT == MVT::f64) ? RTLIB::OGT_F64 : RTLIB::OGT_F128;
141 break;
142 case ISD::SETUO:
143 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 :
144 (VT == MVT::f64) ? RTLIB::UO_F64 : RTLIB::UO_F128;
145 break;
146 case ISD::SETO:
147 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 :
148 (VT == MVT::f64) ? RTLIB::O_F64 : RTLIB::O_F128;
149 break;
150 default:
151 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 :
152 (VT == MVT::f64) ? RTLIB::UO_F64 : RTLIB::UO_F128;
153 switch (CCCode) {
154 case ISD::SETONE:
155 // SETONE = SETOLT | SETOGT
156 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 :
157 (VT == MVT::f64) ? RTLIB::OLT_F64 : RTLIB::OLT_F128;
158 // Fallthrough
159 case ISD::SETUGT:
160 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 :
161 (VT == MVT::f64) ? RTLIB::OGT_F64 : RTLIB::OGT_F128;
162 break;
163 case ISD::SETUGE:
164 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 :
165 (VT == MVT::f64) ? RTLIB::OGE_F64 : RTLIB::OGE_F128;
166 break;
167 case ISD::SETULT:
168 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 :
169 (VT == MVT::f64) ? RTLIB::OLT_F64 : RTLIB::OLT_F128;
170 break;
171 case ISD::SETULE:
172 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 :
173 (VT == MVT::f64) ? RTLIB::OLE_F64 : RTLIB::OLE_F128;
174 break;
175 case ISD::SETUEQ:
176 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 :
177 (VT == MVT::f64) ? RTLIB::OEQ_F64 : RTLIB::OEQ_F128;
178 break;
179 default: llvm_unreachable("Do not know how to soften this setcc!");
180 }
181 }
182
183 // Use the target specific return value for comparions lib calls.
184 EVT RetVT = getCmpLibcallReturnType();
185 SDValue Ops[2] = { NewLHS, NewRHS };
186 NewLHS = makeLibCall(DAG, LC1, RetVT, Ops, 2, false/*sign irrelevant*/, dl);
187 NewRHS = DAG.getConstant(0, RetVT);
188 CCCode = getCmpLibcallCC(LC1);
189 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
190 SDValue Tmp = DAG.getNode(ISD::SETCC, dl, getSetCCResultType(RetVT),
191 NewLHS, NewRHS, DAG.getCondCode(CCCode));
192 NewLHS = makeLibCall(DAG, LC2, RetVT, Ops, 2, false/*sign irrelevant*/, dl);
193 NewLHS = DAG.getNode(ISD::SETCC, dl, getSetCCResultType(RetVT), NewLHS,
194 NewRHS, DAG.getCondCode(getCmpLibcallCC(LC2)));
195 NewLHS = DAG.getNode(ISD::OR, dl, Tmp.getValueType(), Tmp, NewLHS);
196 NewRHS = SDValue();
197 }
198}
199
Chris Lattner071c62f2010-01-25 23:26:13 +0000200/// getJumpTableEncoding - Return the entry encoding for a jump table in the
201/// current function. The returned value is a member of the
202/// MachineJumpTableInfo::JTEntryKind enum.
203unsigned TargetLowering::getJumpTableEncoding() const {
204 // In non-pic modes, just use the address of a block.
205 if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
206 return MachineJumpTableInfo::EK_BlockAddress;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000207
Chris Lattner071c62f2010-01-25 23:26:13 +0000208 // In PIC mode, if the target supports a GPRel32 directive, use it.
209 if (getTargetMachine().getMCAsmInfo()->getGPRel32Directive() != 0)
210 return MachineJumpTableInfo::EK_GPRel32BlockAddress;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000211
Chris Lattner071c62f2010-01-25 23:26:13 +0000212 // Otherwise, use a label difference.
213 return MachineJumpTableInfo::EK_LabelDifference32;
214}
215
Dan Gohman475871a2008-07-27 21:46:04 +0000216SDValue TargetLowering::getPICJumpTableRelocBase(SDValue Table,
217 SelectionDAG &DAG) const {
Chris Lattnerf1214cb2010-01-26 06:53:37 +0000218 // If our PIC model is GP relative, use the global offset table as the base.
Akira Hatanaka787c3fd2012-04-09 20:32:12 +0000219 unsigned JTEncoding = getJumpTableEncoding();
220
221 if ((JTEncoding == MachineJumpTableInfo::EK_GPRel64BlockAddress) ||
222 (JTEncoding == MachineJumpTableInfo::EK_GPRel32BlockAddress))
Micah Villmow7d661462012-10-09 16:06:12 +0000223 return DAG.getGLOBAL_OFFSET_TABLE(getPointerTy(0));
Akira Hatanaka787c3fd2012-04-09 20:32:12 +0000224
Evan Chengcc415862007-11-09 01:32:10 +0000225 return Table;
226}
227
Chris Lattner13e97a22010-01-26 05:30:30 +0000228/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
229/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
230/// MCExpr.
231const MCExpr *
Chris Lattner589c6f62010-01-26 06:28:43 +0000232TargetLowering::getPICJumpTableRelocBaseExpr(const MachineFunction *MF,
233 unsigned JTI,MCContext &Ctx) const{
Chris Lattnerbeeb93e2010-01-26 05:58:28 +0000234 // The normal PIC reloc base is the label at the start of the jump table.
Chris Lattner589c6f62010-01-26 06:28:43 +0000235 return MCSymbolRefExpr::Create(MF->getJTISymbol(JTI, Ctx), Ctx);
Chris Lattner13e97a22010-01-26 05:30:30 +0000236}
237
Dan Gohman6520e202008-10-18 02:06:02 +0000238bool
239TargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
240 // Assume that everything is safe in static mode.
241 if (getTargetMachine().getRelocationModel() == Reloc::Static)
242 return true;
243
244 // In dynamic-no-pic mode, assume that known defined values are safe.
245 if (getTargetMachine().getRelocationModel() == Reloc::DynamicNoPIC &&
246 GA &&
247 !GA->getGlobal()->isDeclaration() &&
Duncan Sands667d4b82009-03-07 15:45:40 +0000248 !GA->getGlobal()->isWeakForLinker())
Dan Gohman6520e202008-10-18 02:06:02 +0000249 return true;
250
251 // Otherwise assume nothing is safe.
252 return false;
253}
254
Chris Lattnereb8146b2006-02-04 02:13:02 +0000255//===----------------------------------------------------------------------===//
256// Optimization Methods
257//===----------------------------------------------------------------------===//
258
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000259/// ShrinkDemandedConstant - Check to see if the specified operand of the
Nate Begeman368e18d2006-02-16 21:11:51 +0000260/// specified instruction is a constant integer. If so, check to see if there
261/// are any bits set in the constant that are not demanded. If so, shrink the
262/// constant and return true.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000263bool TargetLowering::TargetLoweringOpt::ShrinkDemandedConstant(SDValue Op,
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000264 const APInt &Demanded) {
Dale Johannesende064702009-02-06 21:50:26 +0000265 DebugLoc dl = Op.getDebugLoc();
Bill Wendling36ae6c12009-03-04 00:18:06 +0000266
Chris Lattnerec665152006-02-26 23:36:02 +0000267 // FIXME: ISD::SELECT, ISD::SELECT_CC
Dan Gohmane5af2d32009-01-29 01:59:02 +0000268 switch (Op.getOpcode()) {
Nate Begeman368e18d2006-02-16 21:11:51 +0000269 default: break;
Nate Begeman368e18d2006-02-16 21:11:51 +0000270 case ISD::XOR:
Bill Wendling36ae6c12009-03-04 00:18:06 +0000271 case ISD::AND:
272 case ISD::OR: {
273 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
274 if (!C) return false;
275
276 if (Op.getOpcode() == ISD::XOR &&
277 (C->getAPIntValue() | (~Demanded)).isAllOnesValue())
278 return false;
279
280 // if we can expand it to have all bits set, do it
281 if (C->getAPIntValue().intersects(~Demanded)) {
Owen Andersone50ed302009-08-10 22:56:29 +0000282 EVT VT = Op.getValueType();
Bill Wendling36ae6c12009-03-04 00:18:06 +0000283 SDValue New = DAG.getNode(Op.getOpcode(), dl, VT, Op.getOperand(0),
284 DAG.getConstant(Demanded &
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000285 C->getAPIntValue(),
Bill Wendling36ae6c12009-03-04 00:18:06 +0000286 VT));
287 return CombineTo(Op, New);
288 }
289
Nate Begemande996292006-02-03 22:24:05 +0000290 break;
291 }
Bill Wendling36ae6c12009-03-04 00:18:06 +0000292 }
293
Nate Begemande996292006-02-03 22:24:05 +0000294 return false;
295}
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000296
Dan Gohman97121ba2009-04-08 00:15:30 +0000297/// ShrinkDemandedOp - Convert x+y to (VT)((SmallVT)x+(SmallVT)y) if the
298/// casts are free. This uses isZExtFree and ZERO_EXTEND for the widening
299/// cast, but it could be generalized for targets with other types of
300/// implicit widening casts.
301bool
302TargetLowering::TargetLoweringOpt::ShrinkDemandedOp(SDValue Op,
303 unsigned BitWidth,
304 const APInt &Demanded,
305 DebugLoc dl) {
306 assert(Op.getNumOperands() == 2 &&
307 "ShrinkDemandedOp only supports binary operators!");
308 assert(Op.getNode()->getNumValues() == 1 &&
309 "ShrinkDemandedOp only supports nodes with one result!");
310
311 // Don't do this if the node has another user, which may require the
312 // full value.
313 if (!Op.getNode()->hasOneUse())
314 return false;
315
316 // Search for the smallest integer type with free casts to and from
317 // Op's type. For expedience, just check power-of-2 integer types.
318 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Nadav Rotembf5a2c62012-12-19 07:39:08 +0000319 unsigned DemandedSize = BitWidth - Demanded.countLeadingZeros();
320 unsigned SmallVTBits = DemandedSize;
Dan Gohman97121ba2009-04-08 00:15:30 +0000321 if (!isPowerOf2_32(SmallVTBits))
322 SmallVTBits = NextPowerOf2(SmallVTBits);
323 for (; SmallVTBits < BitWidth; SmallVTBits = NextPowerOf2(SmallVTBits)) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000324 EVT SmallVT = EVT::getIntegerVT(*DAG.getContext(), SmallVTBits);
Dan Gohman97121ba2009-04-08 00:15:30 +0000325 if (TLI.isTruncateFree(Op.getValueType(), SmallVT) &&
326 TLI.isZExtFree(SmallVT, Op.getValueType())) {
327 // We found a type with free casts.
328 SDValue X = DAG.getNode(Op.getOpcode(), dl, SmallVT,
329 DAG.getNode(ISD::TRUNCATE, dl, SmallVT,
330 Op.getNode()->getOperand(0)),
331 DAG.getNode(ISD::TRUNCATE, dl, SmallVT,
332 Op.getNode()->getOperand(1)));
Nadav Rotembf5a2c62012-12-19 07:39:08 +0000333 bool NeedZext = DemandedSize > SmallVTBits;
334 SDValue Z = DAG.getNode(NeedZext ? ISD::ZERO_EXTEND : ISD::ANY_EXTEND,
335 dl, Op.getValueType(), X);
Dan Gohman97121ba2009-04-08 00:15:30 +0000336 return CombineTo(Op, Z);
337 }
338 }
339 return false;
340}
341
Nate Begeman368e18d2006-02-16 21:11:51 +0000342/// SimplifyDemandedBits - Look at Op. At this point, we know that only the
Chad Rosier8c1ec5a2011-06-11 02:27:46 +0000343/// DemandedMask bits of the result of Op are ever used downstream. If we can
Nate Begeman368e18d2006-02-16 21:11:51 +0000344/// use this information to simplify Op, create a new simplified DAG node and
345/// return true, returning the original and new nodes in Old and New. Otherwise,
346/// analyze the expression and return a mask of KnownOne and KnownZero bits for
347/// the expression (used to simplify the caller). The KnownZero/One bits may
348/// only be accurate for those bits in the DemandedMask.
Dan Gohman475871a2008-07-27 21:46:04 +0000349bool TargetLowering::SimplifyDemandedBits(SDValue Op,
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000350 const APInt &DemandedMask,
351 APInt &KnownZero,
352 APInt &KnownOne,
Nate Begeman368e18d2006-02-16 21:11:51 +0000353 TargetLoweringOpt &TLO,
354 unsigned Depth) const {
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000355 unsigned BitWidth = DemandedMask.getBitWidth();
Dan Gohman87862e72009-12-11 21:31:27 +0000356 assert(Op.getValueType().getScalarType().getSizeInBits() == BitWidth &&
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000357 "Mask size mismatches value type size!");
358 APInt NewMask = DemandedMask;
Dale Johannesen6f38cb62009-02-07 19:59:05 +0000359 DebugLoc dl = Op.getDebugLoc();
Chris Lattner3fc5b012007-05-17 18:19:23 +0000360
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000361 // Don't know anything.
362 KnownZero = KnownOne = APInt(BitWidth, 0);
363
Nate Begeman368e18d2006-02-16 21:11:51 +0000364 // Other users may use these bits.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000365 if (!Op.getNode()->hasOneUse()) {
Nate Begeman368e18d2006-02-16 21:11:51 +0000366 if (Depth != 0) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000367 // If not at the root, Just compute the KnownZero/KnownOne bits to
Nate Begeman368e18d2006-02-16 21:11:51 +0000368 // simplify things downstream.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +0000369 TLO.DAG.ComputeMaskedBits(Op, KnownZero, KnownOne, Depth);
Nate Begeman368e18d2006-02-16 21:11:51 +0000370 return false;
371 }
372 // If this is the root being simplified, allow it to have multiple uses,
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000373 // just set the NewMask to all bits.
374 NewMask = APInt::getAllOnesValue(BitWidth);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000375 } else if (DemandedMask == 0) {
Nate Begeman368e18d2006-02-16 21:11:51 +0000376 // Not demanding any bits from Op.
377 if (Op.getOpcode() != ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +0000378 return TLO.CombineTo(Op, TLO.DAG.getUNDEF(Op.getValueType()));
Nate Begeman368e18d2006-02-16 21:11:51 +0000379 return false;
380 } else if (Depth == 6) { // Limit search depth.
381 return false;
382 }
383
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000384 APInt KnownZero2, KnownOne2, KnownZeroOut, KnownOneOut;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000385 switch (Op.getOpcode()) {
386 case ISD::Constant:
Nate Begeman368e18d2006-02-16 21:11:51 +0000387 // We know all of the bits for a constant!
Rafael Espindola26c8dcc2012-04-04 12:51:34 +0000388 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue();
389 KnownZero = ~KnownOne;
Chris Lattnerec665152006-02-26 23:36:02 +0000390 return false; // Don't fall through, will infinitely loop.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000391 case ISD::AND:
Chris Lattner81cd3552006-02-27 00:36:27 +0000392 // If the RHS is a constant, check to see if the LHS would be zero without
393 // using the bits from the RHS. Below, we use knowledge about the RHS to
394 // simplify the LHS, here we're using information from the LHS to simplify
395 // the RHS.
396 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000397 APInt LHSZero, LHSOne;
Dale Johannesen97fd9a52011-01-10 21:53:07 +0000398 // Do not increment Depth here; that can cause an infinite loop.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +0000399 TLO.DAG.ComputeMaskedBits(Op.getOperand(0), LHSZero, LHSOne, Depth);
Chris Lattner81cd3552006-02-27 00:36:27 +0000400 // If the LHS already has zeros where RHSC does, this and is dead.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000401 if ((LHSZero & NewMask) == (~RHSC->getAPIntValue() & NewMask))
Chris Lattner81cd3552006-02-27 00:36:27 +0000402 return TLO.CombineTo(Op, Op.getOperand(0));
403 // If any of the set bits in the RHS are known zero on the LHS, shrink
404 // the constant.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000405 if (TLO.ShrinkDemandedConstant(Op, ~LHSZero & NewMask))
Chris Lattner81cd3552006-02-27 00:36:27 +0000406 return true;
407 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000408
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000409 if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero,
Nate Begeman368e18d2006-02-16 21:11:51 +0000410 KnownOne, TLO, Depth+1))
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000411 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000412 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000413 if (SimplifyDemandedBits(Op.getOperand(0), ~KnownZero & NewMask,
Nate Begeman368e18d2006-02-16 21:11:51 +0000414 KnownZero2, KnownOne2, TLO, Depth+1))
415 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000416 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
417
Nate Begeman368e18d2006-02-16 21:11:51 +0000418 // If all of the demanded bits are known one on one side, return the other.
419 // These bits cannot contribute to the result of the 'and'.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000420 if ((NewMask & ~KnownZero2 & KnownOne) == (~KnownZero2 & NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +0000421 return TLO.CombineTo(Op, Op.getOperand(0));
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000422 if ((NewMask & ~KnownZero & KnownOne2) == (~KnownZero & NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +0000423 return TLO.CombineTo(Op, Op.getOperand(1));
424 // If all of the demanded bits in the inputs are known zeros, return zero.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000425 if ((NewMask & (KnownZero|KnownZero2)) == NewMask)
Nate Begeman368e18d2006-02-16 21:11:51 +0000426 return TLO.CombineTo(Op, TLO.DAG.getConstant(0, Op.getValueType()));
427 // If the RHS is a constant, see if we can simplify it.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000428 if (TLO.ShrinkDemandedConstant(Op, ~KnownZero2 & NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +0000429 return true;
Dan Gohman97121ba2009-04-08 00:15:30 +0000430 // If the operation can be done in a smaller type, do so.
Dan Gohman4e39e9d2010-06-24 14:30:44 +0000431 if (TLO.ShrinkDemandedOp(Op, BitWidth, NewMask, dl))
Dan Gohman97121ba2009-04-08 00:15:30 +0000432 return true;
433
Nate Begeman368e18d2006-02-16 21:11:51 +0000434 // Output known-1 bits are only known if set in both the LHS & RHS.
435 KnownOne &= KnownOne2;
436 // Output known-0 are known to be clear if zero in either the LHS | RHS.
437 KnownZero |= KnownZero2;
438 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000439 case ISD::OR:
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000440 if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero,
Nate Begeman368e18d2006-02-16 21:11:51 +0000441 KnownOne, TLO, Depth+1))
442 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000443 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000444 if (SimplifyDemandedBits(Op.getOperand(0), ~KnownOne & NewMask,
Nate Begeman368e18d2006-02-16 21:11:51 +0000445 KnownZero2, KnownOne2, TLO, Depth+1))
446 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000447 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
448
Nate Begeman368e18d2006-02-16 21:11:51 +0000449 // If all of the demanded bits are known zero on one side, return the other.
450 // These bits cannot contribute to the result of the 'or'.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000451 if ((NewMask & ~KnownOne2 & KnownZero) == (~KnownOne2 & NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +0000452 return TLO.CombineTo(Op, Op.getOperand(0));
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000453 if ((NewMask & ~KnownOne & KnownZero2) == (~KnownOne & NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +0000454 return TLO.CombineTo(Op, Op.getOperand(1));
455 // If all of the potentially set bits on one side are known to be set on
456 // the other side, just use the 'other' side.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000457 if ((NewMask & ~KnownZero & KnownOne2) == (~KnownZero & NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +0000458 return TLO.CombineTo(Op, Op.getOperand(0));
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000459 if ((NewMask & ~KnownZero2 & KnownOne) == (~KnownZero2 & NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +0000460 return TLO.CombineTo(Op, Op.getOperand(1));
461 // If the RHS is a constant, see if we can simplify it.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000462 if (TLO.ShrinkDemandedConstant(Op, NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +0000463 return true;
Dan Gohman97121ba2009-04-08 00:15:30 +0000464 // If the operation can be done in a smaller type, do so.
Dan Gohman4e39e9d2010-06-24 14:30:44 +0000465 if (TLO.ShrinkDemandedOp(Op, BitWidth, NewMask, dl))
Dan Gohman97121ba2009-04-08 00:15:30 +0000466 return true;
467
Nate Begeman368e18d2006-02-16 21:11:51 +0000468 // Output known-0 bits are only known if clear in both the LHS & RHS.
469 KnownZero &= KnownZero2;
470 // Output known-1 are known to be set if set in either the LHS | RHS.
471 KnownOne |= KnownOne2;
472 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000473 case ISD::XOR:
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000474 if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero,
Nate Begeman368e18d2006-02-16 21:11:51 +0000475 KnownOne, TLO, Depth+1))
476 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000477 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000478 if (SimplifyDemandedBits(Op.getOperand(0), NewMask, KnownZero2,
Nate Begeman368e18d2006-02-16 21:11:51 +0000479 KnownOne2, TLO, Depth+1))
480 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000481 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
482
Nate Begeman368e18d2006-02-16 21:11:51 +0000483 // If all of the demanded bits are known zero on one side, return the other.
484 // These bits cannot contribute to the result of the 'xor'.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000485 if ((KnownZero & NewMask) == NewMask)
Nate Begeman368e18d2006-02-16 21:11:51 +0000486 return TLO.CombineTo(Op, Op.getOperand(0));
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000487 if ((KnownZero2 & NewMask) == NewMask)
Nate Begeman368e18d2006-02-16 21:11:51 +0000488 return TLO.CombineTo(Op, Op.getOperand(1));
Dan Gohman97121ba2009-04-08 00:15:30 +0000489 // If the operation can be done in a smaller type, do so.
Dan Gohman4e39e9d2010-06-24 14:30:44 +0000490 if (TLO.ShrinkDemandedOp(Op, BitWidth, NewMask, dl))
Dan Gohman97121ba2009-04-08 00:15:30 +0000491 return true;
492
Chris Lattner3687c1a2006-11-27 21:50:02 +0000493 // If all of the unknown bits are known to be zero on one side or the other
494 // (but not both) turn this into an *inclusive* or.
Sylvestre Ledru94c22712012-09-27 10:14:43 +0000495 // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000496 if ((NewMask & ~KnownZero & ~KnownZero2) == 0)
Dale Johannesende064702009-02-06 21:50:26 +0000497 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::OR, dl, Op.getValueType(),
Chris Lattner3687c1a2006-11-27 21:50:02 +0000498 Op.getOperand(0),
499 Op.getOperand(1)));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000500
Nate Begeman368e18d2006-02-16 21:11:51 +0000501 // Output known-0 bits are known if clear or set in both the LHS & RHS.
502 KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
503 // Output known-1 are known to be set if set in only one of the LHS, RHS.
504 KnownOneOut = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000505
Nate Begeman368e18d2006-02-16 21:11:51 +0000506 // If all of the demanded bits on one side are known, and all of the set
507 // bits on that side are also known to be set on the other side, turn this
508 // into an AND, as we know the bits will be cleared.
Sylvestre Ledru94c22712012-09-27 10:14:43 +0000509 // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
Joel Jonesd16ce172012-04-17 22:23:10 +0000510 // NB: it is okay if more bits are known than are requested
511 if ((NewMask & (KnownZero|KnownOne)) == NewMask) { // all known on one side
512 if (KnownOne == KnownOne2) { // set bits are the same on both sides
Owen Andersone50ed302009-08-10 22:56:29 +0000513 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +0000514 SDValue ANDC = TLO.DAG.getConstant(~KnownOne & NewMask, VT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000515 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::AND, dl, VT,
Dale Johannesenff97d4f2009-02-03 00:47:48 +0000516 Op.getOperand(0), ANDC));
Nate Begeman368e18d2006-02-16 21:11:51 +0000517 }
518 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000519
Nate Begeman368e18d2006-02-16 21:11:51 +0000520 // If the RHS is a constant, see if we can simplify it.
Torok Edwin4fea2e92008-04-06 21:23:02 +0000521 // for XOR, we prefer to force bits to 1 if they will make a -1.
522 // if we can't force bits, try to shrink constant
523 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
524 APInt Expanded = C->getAPIntValue() | (~NewMask);
525 // if we can expand it to have all bits set, do it
526 if (Expanded.isAllOnesValue()) {
527 if (Expanded != C->getAPIntValue()) {
Owen Andersone50ed302009-08-10 22:56:29 +0000528 EVT VT = Op.getValueType();
Dale Johannesenff97d4f2009-02-03 00:47:48 +0000529 SDValue New = TLO.DAG.getNode(Op.getOpcode(), dl,VT, Op.getOperand(0),
Torok Edwin4fea2e92008-04-06 21:23:02 +0000530 TLO.DAG.getConstant(Expanded, VT));
531 return TLO.CombineTo(Op, New);
532 }
533 // if it already has all the bits set, nothing to change
534 // but don't shrink either!
535 } else if (TLO.ShrinkDemandedConstant(Op, NewMask)) {
536 return true;
537 }
538 }
539
Nate Begeman368e18d2006-02-16 21:11:51 +0000540 KnownZero = KnownZeroOut;
541 KnownOne = KnownOneOut;
542 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000543 case ISD::SELECT:
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000544 if (SimplifyDemandedBits(Op.getOperand(2), NewMask, KnownZero,
Nate Begeman368e18d2006-02-16 21:11:51 +0000545 KnownOne, TLO, Depth+1))
546 return true;
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000547 if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero2,
Nate Begeman368e18d2006-02-16 21:11:51 +0000548 KnownOne2, TLO, Depth+1))
549 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000550 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
551 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
552
Nate Begeman368e18d2006-02-16 21:11:51 +0000553 // If the operands are constants, see if we can simplify them.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000554 if (TLO.ShrinkDemandedConstant(Op, NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +0000555 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000556
Nate Begeman368e18d2006-02-16 21:11:51 +0000557 // Only known if known in both the LHS and RHS.
558 KnownOne &= KnownOne2;
559 KnownZero &= KnownZero2;
560 break;
Chris Lattnerec665152006-02-26 23:36:02 +0000561 case ISD::SELECT_CC:
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000562 if (SimplifyDemandedBits(Op.getOperand(3), NewMask, KnownZero,
Chris Lattnerec665152006-02-26 23:36:02 +0000563 KnownOne, TLO, Depth+1))
564 return true;
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000565 if (SimplifyDemandedBits(Op.getOperand(2), NewMask, KnownZero2,
Chris Lattnerec665152006-02-26 23:36:02 +0000566 KnownOne2, TLO, Depth+1))
567 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000568 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
569 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
570
Chris Lattnerec665152006-02-26 23:36:02 +0000571 // If the operands are constants, see if we can simplify them.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000572 if (TLO.ShrinkDemandedConstant(Op, NewMask))
Chris Lattnerec665152006-02-26 23:36:02 +0000573 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000574
Chris Lattnerec665152006-02-26 23:36:02 +0000575 // Only known if known in both the LHS and RHS.
576 KnownOne &= KnownOne2;
577 KnownZero &= KnownZero2;
578 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000579 case ISD::SHL:
Nate Begeman368e18d2006-02-16 21:11:51 +0000580 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000581 unsigned ShAmt = SA->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +0000582 SDValue InOp = Op.getOperand(0);
Chris Lattner895c4ab2007-04-17 21:14:16 +0000583
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000584 // If the shift count is an invalid immediate, don't do anything.
585 if (ShAmt >= BitWidth)
586 break;
587
Chris Lattner895c4ab2007-04-17 21:14:16 +0000588 // If this is ((X >>u C1) << ShAmt), see if we can simplify this into a
589 // single shift. We can do this if the bottom bits (which are shifted
590 // out) are never demanded.
591 if (InOp.getOpcode() == ISD::SRL &&
592 isa<ConstantSDNode>(InOp.getOperand(1))) {
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000593 if (ShAmt && (NewMask & APInt::getLowBitsSet(BitWidth, ShAmt)) == 0) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000594 unsigned C1= cast<ConstantSDNode>(InOp.getOperand(1))->getZExtValue();
Chris Lattner895c4ab2007-04-17 21:14:16 +0000595 unsigned Opc = ISD::SHL;
596 int Diff = ShAmt-C1;
597 if (Diff < 0) {
598 Diff = -Diff;
599 Opc = ISD::SRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000600 }
601
602 SDValue NewSA =
Chris Lattner4e7e6cd2007-05-30 16:30:06 +0000603 TLO.DAG.getConstant(Diff, Op.getOperand(1).getValueType());
Owen Andersone50ed302009-08-10 22:56:29 +0000604 EVT VT = Op.getValueType();
Dale Johannesenff97d4f2009-02-03 00:47:48 +0000605 return TLO.CombineTo(Op, TLO.DAG.getNode(Opc, dl, VT,
Chris Lattner895c4ab2007-04-17 21:14:16 +0000606 InOp.getOperand(0), NewSA));
607 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000608 }
609
Dan Gohmana4f4d692010-07-23 18:03:30 +0000610 if (SimplifyDemandedBits(InOp, NewMask.lshr(ShAmt),
Nate Begeman368e18d2006-02-16 21:11:51 +0000611 KnownZero, KnownOne, TLO, Depth+1))
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000612 return true;
Dan Gohmana4f4d692010-07-23 18:03:30 +0000613
614 // Convert (shl (anyext x, c)) to (anyext (shl x, c)) if the high bits
615 // are not demanded. This will likely allow the anyext to be folded away.
616 if (InOp.getNode()->getOpcode() == ISD::ANY_EXTEND) {
617 SDValue InnerOp = InOp.getNode()->getOperand(0);
618 EVT InnerVT = InnerOp.getValueType();
Eli Friedman2dd03532011-12-09 01:16:26 +0000619 unsigned InnerBits = InnerVT.getSizeInBits();
620 if (ShAmt < InnerBits && NewMask.lshr(InnerBits) == 0 &&
Dan Gohmana4f4d692010-07-23 18:03:30 +0000621 isTypeDesirableForOp(ISD::SHL, InnerVT)) {
Owen Anderson95771af2011-02-25 21:41:48 +0000622 EVT ShTy = getShiftAmountTy(InnerVT);
Dan Gohmancd20c6f2010-07-23 21:08:12 +0000623 if (!APInt(BitWidth, ShAmt).isIntN(ShTy.getSizeInBits()))
624 ShTy = InnerVT;
Dan Gohmana4f4d692010-07-23 18:03:30 +0000625 SDValue NarrowShl =
626 TLO.DAG.getNode(ISD::SHL, dl, InnerVT, InnerOp,
Dan Gohmancd20c6f2010-07-23 21:08:12 +0000627 TLO.DAG.getConstant(ShAmt, ShTy));
Dan Gohmana4f4d692010-07-23 18:03:30 +0000628 return
629 TLO.CombineTo(Op,
630 TLO.DAG.getNode(ISD::ANY_EXTEND, dl, Op.getValueType(),
631 NarrowShl));
632 }
633 }
634
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000635 KnownZero <<= SA->getZExtValue();
636 KnownOne <<= SA->getZExtValue();
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000637 // low bits known zero.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000638 KnownZero |= APInt::getLowBitsSet(BitWidth, SA->getZExtValue());
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000639 }
640 break;
Nate Begeman368e18d2006-02-16 21:11:51 +0000641 case ISD::SRL:
642 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Owen Andersone50ed302009-08-10 22:56:29 +0000643 EVT VT = Op.getValueType();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000644 unsigned ShAmt = SA->getZExtValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000645 unsigned VTSize = VT.getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +0000646 SDValue InOp = Op.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000647
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000648 // If the shift count is an invalid immediate, don't do anything.
649 if (ShAmt >= BitWidth)
650 break;
651
Chris Lattner895c4ab2007-04-17 21:14:16 +0000652 // If this is ((X << C1) >>u ShAmt), see if we can simplify this into a
653 // single shift. We can do this if the top bits (which are shifted out)
654 // are never demanded.
655 if (InOp.getOpcode() == ISD::SHL &&
656 isa<ConstantSDNode>(InOp.getOperand(1))) {
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000657 if (ShAmt && (NewMask & APInt::getHighBitsSet(VTSize, ShAmt)) == 0) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000658 unsigned C1= cast<ConstantSDNode>(InOp.getOperand(1))->getZExtValue();
Chris Lattner895c4ab2007-04-17 21:14:16 +0000659 unsigned Opc = ISD::SRL;
660 int Diff = ShAmt-C1;
661 if (Diff < 0) {
662 Diff = -Diff;
663 Opc = ISD::SHL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000664 }
665
Dan Gohman475871a2008-07-27 21:46:04 +0000666 SDValue NewSA =
Chris Lattner8c7d2d52007-04-17 22:53:02 +0000667 TLO.DAG.getConstant(Diff, Op.getOperand(1).getValueType());
Dale Johannesenff97d4f2009-02-03 00:47:48 +0000668 return TLO.CombineTo(Op, TLO.DAG.getNode(Opc, dl, VT,
Chris Lattner895c4ab2007-04-17 21:14:16 +0000669 InOp.getOperand(0), NewSA));
670 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000671 }
672
Nate Begeman368e18d2006-02-16 21:11:51 +0000673 // Compute the new bits that are at the top now.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000674 if (SimplifyDemandedBits(InOp, (NewMask << ShAmt),
Nate Begeman368e18d2006-02-16 21:11:51 +0000675 KnownZero, KnownOne, TLO, Depth+1))
676 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000677 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000678 KnownZero = KnownZero.lshr(ShAmt);
679 KnownOne = KnownOne.lshr(ShAmt);
Chris Lattnerc4fa6032006-06-13 16:52:37 +0000680
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000681 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Chris Lattnerc4fa6032006-06-13 16:52:37 +0000682 KnownZero |= HighBits; // High bits known zero.
Nate Begeman368e18d2006-02-16 21:11:51 +0000683 }
684 break;
685 case ISD::SRA:
Dan Gohmane5af2d32009-01-29 01:59:02 +0000686 // If this is an arithmetic shift right and only the low-bit is set, we can
687 // always convert this into a logical shr, even if the shift amount is
688 // variable. The low bit of the shift cannot be an input sign bit unless
689 // the shift amount is >= the size of the datatype, which is undefined.
Eli Friedman2dd03532011-12-09 01:16:26 +0000690 if (NewMask == 1)
Evan Chenge5b51ac2010-04-17 06:13:15 +0000691 return TLO.CombineTo(Op,
692 TLO.DAG.getNode(ISD::SRL, dl, Op.getValueType(),
693 Op.getOperand(0), Op.getOperand(1)));
Dan Gohmane5af2d32009-01-29 01:59:02 +0000694
Nate Begeman368e18d2006-02-16 21:11:51 +0000695 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Owen Andersone50ed302009-08-10 22:56:29 +0000696 EVT VT = Op.getValueType();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000697 unsigned ShAmt = SA->getZExtValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000698
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000699 // If the shift count is an invalid immediate, don't do anything.
700 if (ShAmt >= BitWidth)
701 break;
702
703 APInt InDemandedMask = (NewMask << ShAmt);
Chris Lattner1b737132006-05-08 17:22:53 +0000704
705 // If any of the demanded bits are produced by the sign extension, we also
706 // demand the input sign bit.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000707 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
708 if (HighBits.intersects(NewMask))
Dan Gohman87862e72009-12-11 21:31:27 +0000709 InDemandedMask |= APInt::getSignBit(VT.getScalarType().getSizeInBits());
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000710
Chris Lattner1b737132006-05-08 17:22:53 +0000711 if (SimplifyDemandedBits(Op.getOperand(0), InDemandedMask,
Nate Begeman368e18d2006-02-16 21:11:51 +0000712 KnownZero, KnownOne, TLO, Depth+1))
713 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000714 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000715 KnownZero = KnownZero.lshr(ShAmt);
716 KnownOne = KnownOne.lshr(ShAmt);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000717
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000718 // Handle the sign bit, adjusted to where it is now in the mask.
719 APInt SignBit = APInt::getSignBit(BitWidth).lshr(ShAmt);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000720
Nate Begeman368e18d2006-02-16 21:11:51 +0000721 // If the input sign bit is known to be zero, or if none of the top bits
722 // are demanded, turn this into an unsigned shift right.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000723 if (KnownZero.intersects(SignBit) || (HighBits & ~NewMask) == HighBits) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000724 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, dl, VT,
Dale Johannesenff97d4f2009-02-03 00:47:48 +0000725 Op.getOperand(0),
Nate Begeman368e18d2006-02-16 21:11:51 +0000726 Op.getOperand(1)));
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000727 } else if (KnownOne.intersects(SignBit)) { // New bits are known one.
Nate Begeman368e18d2006-02-16 21:11:51 +0000728 KnownOne |= HighBits;
729 }
730 }
731 break;
732 case ISD::SIGN_EXTEND_INREG: {
Nadav Rotemcc616562012-01-15 19:27:55 +0000733 EVT ExVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
734
735 APInt MsbMask = APInt::getHighBitsSet(BitWidth, 1);
736 // If we only care about the highest bit, don't bother shifting right.
Eli Friedmand49db362012-01-31 01:08:03 +0000737 if (MsbMask == DemandedMask) {
Nadav Rotemcc616562012-01-15 19:27:55 +0000738 unsigned ShAmt = ExVT.getScalarType().getSizeInBits();
739 SDValue InOp = Op.getOperand(0);
Eli Friedmand49db362012-01-31 01:08:03 +0000740
741 // Compute the correct shift amount type, which must be getShiftAmountTy
742 // for scalar types after legalization.
743 EVT ShiftAmtTy = Op.getValueType();
744 if (TLO.LegalTypes() && !ShiftAmtTy.isVector())
745 ShiftAmtTy = getShiftAmountTy(ShiftAmtTy);
746
747 SDValue ShiftAmt = TLO.DAG.getConstant(BitWidth - ShAmt, ShiftAmtTy);
Nadav Rotemcc616562012-01-15 19:27:55 +0000748 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SHL, dl,
749 Op.getValueType(), InOp, ShiftAmt));
750 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000751
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000752 // Sign extension. Compute the demanded bits in the result that are not
Nate Begeman368e18d2006-02-16 21:11:51 +0000753 // present in the input.
Dan Gohmand1996362010-01-09 02:13:55 +0000754 APInt NewBits =
755 APInt::getHighBitsSet(BitWidth,
Nadav Rotemcc616562012-01-15 19:27:55 +0000756 BitWidth - ExVT.getScalarType().getSizeInBits());
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000757
Chris Lattnerec665152006-02-26 23:36:02 +0000758 // If none of the extended bits are demanded, eliminate the sextinreg.
Eli Friedman1d17d192010-08-02 04:42:25 +0000759 if ((NewBits & NewMask) == 0)
Chris Lattnerec665152006-02-26 23:36:02 +0000760 return TLO.CombineTo(Op, Op.getOperand(0));
761
Jay Foad40f8f622010-12-07 08:25:19 +0000762 APInt InSignBit =
Nadav Rotemcc616562012-01-15 19:27:55 +0000763 APInt::getSignBit(ExVT.getScalarType().getSizeInBits()).zext(BitWidth);
Dan Gohmand1996362010-01-09 02:13:55 +0000764 APInt InputDemandedBits =
765 APInt::getLowBitsSet(BitWidth,
Nadav Rotemcc616562012-01-15 19:27:55 +0000766 ExVT.getScalarType().getSizeInBits()) &
Dan Gohmand1996362010-01-09 02:13:55 +0000767 NewMask;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000768
Chris Lattnerec665152006-02-26 23:36:02 +0000769 // Since the sign extended bits are demanded, we know that the sign
Nate Begeman368e18d2006-02-16 21:11:51 +0000770 // bit is demanded.
Chris Lattnerec665152006-02-26 23:36:02 +0000771 InputDemandedBits |= InSignBit;
Nate Begeman368e18d2006-02-16 21:11:51 +0000772
773 if (SimplifyDemandedBits(Op.getOperand(0), InputDemandedBits,
774 KnownZero, KnownOne, TLO, Depth+1))
775 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000776 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Nate Begeman368e18d2006-02-16 21:11:51 +0000777
778 // If the sign bit of the input is known set or clear, then we know the
779 // top bits of the result.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000780
Chris Lattnerec665152006-02-26 23:36:02 +0000781 // If the input sign bit is known zero, convert this into a zero extension.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000782 if (KnownZero.intersects(InSignBit))
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000783 return TLO.CombineTo(Op,
Nadav Rotemcc616562012-01-15 19:27:55 +0000784 TLO.DAG.getZeroExtendInReg(Op.getOperand(0),dl,ExVT));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000785
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000786 if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Nate Begeman368e18d2006-02-16 21:11:51 +0000787 KnownOne |= NewBits;
788 KnownZero &= ~NewBits;
Chris Lattnerec665152006-02-26 23:36:02 +0000789 } else { // Input sign bit unknown
Nate Begeman368e18d2006-02-16 21:11:51 +0000790 KnownZero &= ~NewBits;
791 KnownOne &= ~NewBits;
792 }
793 break;
794 }
Chris Lattnerec665152006-02-26 23:36:02 +0000795 case ISD::ZERO_EXTEND: {
Dan Gohmand1996362010-01-09 02:13:55 +0000796 unsigned OperandBitWidth =
797 Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
Jay Foad40f8f622010-12-07 08:25:19 +0000798 APInt InMask = NewMask.trunc(OperandBitWidth);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000799
Chris Lattnerec665152006-02-26 23:36:02 +0000800 // If none of the top bits are demanded, convert this into an any_extend.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000801 APInt NewBits =
802 APInt::getHighBitsSet(BitWidth, BitWidth - OperandBitWidth) & NewMask;
803 if (!NewBits.intersects(NewMask))
Dale Johannesenff97d4f2009-02-03 00:47:48 +0000804 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ANY_EXTEND, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000805 Op.getValueType(),
Chris Lattnerec665152006-02-26 23:36:02 +0000806 Op.getOperand(0)));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000807
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000808 if (SimplifyDemandedBits(Op.getOperand(0), InMask,
Chris Lattnerec665152006-02-26 23:36:02 +0000809 KnownZero, KnownOne, TLO, Depth+1))
810 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000811 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Jay Foad40f8f622010-12-07 08:25:19 +0000812 KnownZero = KnownZero.zext(BitWidth);
813 KnownOne = KnownOne.zext(BitWidth);
Chris Lattnerec665152006-02-26 23:36:02 +0000814 KnownZero |= NewBits;
815 break;
816 }
817 case ISD::SIGN_EXTEND: {
Owen Andersone50ed302009-08-10 22:56:29 +0000818 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohmand1996362010-01-09 02:13:55 +0000819 unsigned InBits = InVT.getScalarType().getSizeInBits();
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000820 APInt InMask = APInt::getLowBitsSet(BitWidth, InBits);
Dan Gohman97360282008-03-11 21:29:43 +0000821 APInt InSignBit = APInt::getBitsSet(BitWidth, InBits - 1, InBits);
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000822 APInt NewBits = ~InMask & NewMask;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000823
Chris Lattnerec665152006-02-26 23:36:02 +0000824 // If none of the top bits are demanded, convert this into an any_extend.
825 if (NewBits == 0)
Dale Johannesenff97d4f2009-02-03 00:47:48 +0000826 return TLO.CombineTo(Op,TLO.DAG.getNode(ISD::ANY_EXTEND, dl,
827 Op.getValueType(),
828 Op.getOperand(0)));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000829
Chris Lattnerec665152006-02-26 23:36:02 +0000830 // Since some of the sign extended bits are demanded, we know that the sign
831 // bit is demanded.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000832 APInt InDemandedBits = InMask & NewMask;
Chris Lattnerec665152006-02-26 23:36:02 +0000833 InDemandedBits |= InSignBit;
Jay Foad40f8f622010-12-07 08:25:19 +0000834 InDemandedBits = InDemandedBits.trunc(InBits);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000835
836 if (SimplifyDemandedBits(Op.getOperand(0), InDemandedBits, KnownZero,
Chris Lattnerec665152006-02-26 23:36:02 +0000837 KnownOne, TLO, Depth+1))
838 return true;
Jay Foad40f8f622010-12-07 08:25:19 +0000839 KnownZero = KnownZero.zext(BitWidth);
840 KnownOne = KnownOne.zext(BitWidth);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000841
Chris Lattnerec665152006-02-26 23:36:02 +0000842 // If the sign bit is known zero, convert this to a zero extend.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000843 if (KnownZero.intersects(InSignBit))
Dale Johannesenff97d4f2009-02-03 00:47:48 +0000844 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ZERO_EXTEND, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000845 Op.getValueType(),
Chris Lattnerec665152006-02-26 23:36:02 +0000846 Op.getOperand(0)));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000847
Chris Lattnerec665152006-02-26 23:36:02 +0000848 // If the sign bit is known one, the top bits match.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000849 if (KnownOne.intersects(InSignBit)) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +0000850 KnownOne |= NewBits;
851 assert((KnownZero & NewBits) == 0);
Chris Lattnerec665152006-02-26 23:36:02 +0000852 } else { // Otherwise, top bits aren't known.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +0000853 assert((KnownOne & NewBits) == 0);
854 assert((KnownZero & NewBits) == 0);
Chris Lattnerec665152006-02-26 23:36:02 +0000855 }
856 break;
857 }
858 case ISD::ANY_EXTEND: {
Dan Gohmand1996362010-01-09 02:13:55 +0000859 unsigned OperandBitWidth =
860 Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
Jay Foad40f8f622010-12-07 08:25:19 +0000861 APInt InMask = NewMask.trunc(OperandBitWidth);
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000862 if (SimplifyDemandedBits(Op.getOperand(0), InMask,
Chris Lattnerec665152006-02-26 23:36:02 +0000863 KnownZero, KnownOne, TLO, Depth+1))
864 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000865 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Jay Foad40f8f622010-12-07 08:25:19 +0000866 KnownZero = KnownZero.zext(BitWidth);
867 KnownOne = KnownOne.zext(BitWidth);
Chris Lattnerec665152006-02-26 23:36:02 +0000868 break;
869 }
Chris Lattnerfe8babf2006-05-05 22:32:12 +0000870 case ISD::TRUNCATE: {
Chris Lattnerc93dfda2006-05-06 00:11:52 +0000871 // Simplify the input, using demanded bit information, and compute the known
872 // zero/one bits live out.
Dan Gohman042919c2010-03-01 17:59:21 +0000873 unsigned OperandBitWidth =
874 Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
Jay Foad40f8f622010-12-07 08:25:19 +0000875 APInt TruncMask = NewMask.zext(OperandBitWidth);
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000876 if (SimplifyDemandedBits(Op.getOperand(0), TruncMask,
Chris Lattnerfe8babf2006-05-05 22:32:12 +0000877 KnownZero, KnownOne, TLO, Depth+1))
878 return true;
Jay Foad40f8f622010-12-07 08:25:19 +0000879 KnownZero = KnownZero.trunc(BitWidth);
880 KnownOne = KnownOne.trunc(BitWidth);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000881
Chris Lattnerc93dfda2006-05-06 00:11:52 +0000882 // If the input is only used by this truncate, see if we can shrink it based
883 // on the known demanded bits.
Gabor Greifba36cb52008-08-28 21:40:38 +0000884 if (Op.getOperand(0).getNode()->hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000885 SDValue In = Op.getOperand(0);
Chris Lattnerc93dfda2006-05-06 00:11:52 +0000886 switch (In.getOpcode()) {
887 default: break;
888 case ISD::SRL:
889 // Shrink SRL by a constant if none of the high bits shifted in are
890 // demanded.
Evan Chenge5b51ac2010-04-17 06:13:15 +0000891 if (TLO.LegalTypes() &&
892 !isTypeDesirableForOp(ISD::SRL, Op.getValueType()))
893 // Do not turn (vt1 truncate (vt2 srl)) into (vt1 srl) if vt1 is
894 // undesirable.
895 break;
896 ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(In.getOperand(1));
897 if (!ShAmt)
898 break;
Owen Anderson7adf8622011-04-13 23:22:23 +0000899 SDValue Shift = In.getOperand(1);
900 if (TLO.LegalTypes()) {
901 uint64_t ShVal = ShAmt->getZExtValue();
902 Shift =
903 TLO.DAG.getConstant(ShVal, getShiftAmountTy(Op.getValueType()));
904 }
905
Evan Chenge5b51ac2010-04-17 06:13:15 +0000906 APInt HighBits = APInt::getHighBitsSet(OperandBitWidth,
907 OperandBitWidth - BitWidth);
Jay Foad40f8f622010-12-07 08:25:19 +0000908 HighBits = HighBits.lshr(ShAmt->getZExtValue()).trunc(BitWidth);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000909
910 if (ShAmt->getZExtValue() < BitWidth && !(HighBits & NewMask)) {
911 // None of the shifted in bits are needed. Add a truncate of the
912 // shift input, then shift it.
913 SDValue NewTrunc = TLO.DAG.getNode(ISD::TRUNCATE, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000914 Op.getValueType(),
Evan Chenge5b51ac2010-04-17 06:13:15 +0000915 In.getOperand(0));
916 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, dl,
917 Op.getValueType(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000918 NewTrunc,
Owen Anderson7adf8622011-04-13 23:22:23 +0000919 Shift));
Chris Lattnerc93dfda2006-05-06 00:11:52 +0000920 }
921 break;
922 }
923 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000924
925 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Chris Lattnerfe8babf2006-05-05 22:32:12 +0000926 break;
927 }
Chris Lattnerec665152006-02-26 23:36:02 +0000928 case ISD::AssertZext: {
Owen Anderson7ab15f62011-09-03 00:26:49 +0000929 // AssertZext demands all of the high bits, plus any of the low bits
930 // demanded by its users.
931 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
932 APInt InMask = APInt::getLowBitsSet(BitWidth,
933 VT.getSizeInBits());
934 if (SimplifyDemandedBits(Op.getOperand(0), ~InMask | NewMask,
Chris Lattnerec665152006-02-26 23:36:02 +0000935 KnownZero, KnownOne, TLO, Depth+1))
936 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000937 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman400f75c2010-06-03 20:21:33 +0000938
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000939 KnownZero |= ~InMask & NewMask;
Chris Lattnerec665152006-02-26 23:36:02 +0000940 break;
941 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000942 case ISD::BITCAST:
Stuart Hastings57f1fde2011-06-06 16:44:31 +0000943 // If this is an FP->Int bitcast and if the sign bit is the only
944 // thing demanded, turn this into a FGETSIGN.
Eli Friedmanca072a32011-12-15 02:07:20 +0000945 if (!TLO.LegalOperations() &&
946 !Op.getValueType().isVector() &&
Eli Friedman0948f0a2011-11-09 22:25:12 +0000947 !Op.getOperand(0).getValueType().isVector() &&
Nadav Rotem0c3e6782011-06-12 14:56:55 +0000948 NewMask == APInt::getSignBit(Op.getValueType().getSizeInBits()) &&
949 Op.getOperand(0).getValueType().isFloatingPoint()) {
Stuart Hastings57f1fde2011-06-06 16:44:31 +0000950 bool OpVTLegal = isOperationLegalOrCustom(ISD::FGETSIGN, Op.getValueType());
951 bool i32Legal = isOperationLegalOrCustom(ISD::FGETSIGN, MVT::i32);
952 if ((OpVTLegal || i32Legal) && Op.getValueType().isSimple()) {
953 EVT Ty = OpVTLegal ? Op.getValueType() : MVT::i32;
Chris Lattner2ceb2cf2007-12-22 21:35:38 +0000954 // Make a FGETSIGN + SHL to move the sign bit into the appropriate
955 // place. We expect the SHL to be eliminated by other optimizations.
Stuart Hastings090bf192011-06-01 18:32:25 +0000956 SDValue Sign = TLO.DAG.getNode(ISD::FGETSIGN, dl, Ty, Op.getOperand(0));
Stuart Hastings57f1fde2011-06-06 16:44:31 +0000957 unsigned OpVTSizeInBits = Op.getValueType().getSizeInBits();
958 if (!OpVTLegal && OpVTSizeInBits > 32)
Stuart Hastings090bf192011-06-01 18:32:25 +0000959 Sign = TLO.DAG.getNode(ISD::ZERO_EXTEND, dl, Op.getValueType(), Sign);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000960 unsigned ShVal = Op.getValueType().getSizeInBits()-1;
Stuart Hastingsbdce3722011-06-01 14:04:17 +0000961 SDValue ShAmt = TLO.DAG.getConstant(ShVal, Op.getValueType());
Stuart Hastings3dfc4b122011-05-19 18:48:20 +0000962 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SHL, dl,
963 Op.getValueType(),
Chris Lattner2ceb2cf2007-12-22 21:35:38 +0000964 Sign, ShAmt));
965 }
966 }
Chris Lattner2ceb2cf2007-12-22 21:35:38 +0000967 break;
Dan Gohman97121ba2009-04-08 00:15:30 +0000968 case ISD::ADD:
969 case ISD::MUL:
970 case ISD::SUB: {
971 // Add, Sub, and Mul don't demand any bits in positions beyond that
972 // of the highest bit demanded of them.
973 APInt LoMask = APInt::getLowBitsSet(BitWidth,
974 BitWidth - NewMask.countLeadingZeros());
975 if (SimplifyDemandedBits(Op.getOperand(0), LoMask, KnownZero2,
976 KnownOne2, TLO, Depth+1))
977 return true;
978 if (SimplifyDemandedBits(Op.getOperand(1), LoMask, KnownZero2,
979 KnownOne2, TLO, Depth+1))
980 return true;
981 // See if the operation should be performed at a smaller bit width.
Dan Gohman4e39e9d2010-06-24 14:30:44 +0000982 if (TLO.ShrinkDemandedOp(Op, BitWidth, NewMask, dl))
Dan Gohman97121ba2009-04-08 00:15:30 +0000983 return true;
984 }
985 // FALL THROUGH
Dan Gohman54eed372008-05-06 00:53:29 +0000986 default:
Chris Lattner1482b5f2006-04-02 06:15:09 +0000987 // Just use ComputeMaskedBits to compute output bits.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +0000988 TLO.DAG.ComputeMaskedBits(Op, KnownZero, KnownOne, Depth);
Chris Lattnera6bc5a42006-02-27 01:00:42 +0000989 break;
Nate Begeman368e18d2006-02-16 21:11:51 +0000990 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000991
Chris Lattnerec665152006-02-26 23:36:02 +0000992 // If we know the value of all of the demanded bits, return this as a
993 // constant.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000994 if ((NewMask & (KnownZero|KnownOne)) == NewMask)
Chris Lattnerec665152006-02-26 23:36:02 +0000995 return TLO.CombineTo(Op, TLO.DAG.getConstant(KnownOne, Op.getValueType()));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000996
Nate Begeman368e18d2006-02-16 21:11:51 +0000997 return false;
998}
999
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001000/// computeMaskedBitsForTargetNode - Determine which of the bits specified
1001/// in Mask are known to be either zero or one and return them in the
Nate Begeman368e18d2006-02-16 21:11:51 +00001002/// KnownZero/KnownOne bitsets.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001003void TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001004 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001005 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00001006 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00001007 unsigned Depth) const {
Chris Lattner1b5232a2006-04-02 06:19:46 +00001008 assert((Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1009 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1010 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1011 Op.getOpcode() == ISD::INTRINSIC_VOID) &&
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001012 "Should use MaskedValueIsZero if you don't know whether Op"
1013 " is a target node!");
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001014 KnownZero = KnownOne = APInt(KnownOne.getBitWidth(), 0);
Evan Cheng3a03ebb2005-12-21 23:05:39 +00001015}
Chris Lattner4ccb0702006-01-26 20:37:03 +00001016
Chris Lattner5c3e21d2006-05-06 09:27:13 +00001017/// ComputeNumSignBitsForTargetNode - This method can be implemented by
1018/// targets that want to expose additional information about sign bits to the
1019/// DAG Combiner.
Dan Gohman475871a2008-07-27 21:46:04 +00001020unsigned TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
Chris Lattner5c3e21d2006-05-06 09:27:13 +00001021 unsigned Depth) const {
1022 assert((Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1023 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1024 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1025 Op.getOpcode() == ISD::INTRINSIC_VOID) &&
1026 "Should use ComputeNumSignBits if you don't know whether Op"
1027 " is a target node!");
1028 return 1;
1029}
1030
Dan Gohman97d11632009-02-15 23:59:32 +00001031/// ValueHasExactlyOneBitSet - Test if the given value is known to have exactly
1032/// one bit set. This differs from ComputeMaskedBits in that it doesn't need to
1033/// determine which bit is set.
1034///
Dale Johannesen85b0ede2009-02-11 19:19:41 +00001035static bool ValueHasExactlyOneBitSet(SDValue Val, const SelectionDAG &DAG) {
Dan Gohman97d11632009-02-15 23:59:32 +00001036 // A left-shift of a constant one will have exactly one bit set, because
1037 // shifting the bit off the end is undefined.
1038 if (Val.getOpcode() == ISD::SHL)
1039 if (ConstantSDNode *C =
1040 dyn_cast<ConstantSDNode>(Val.getNode()->getOperand(0)))
1041 if (C->getAPIntValue() == 1)
1042 return true;
Dan Gohmane5af2d32009-01-29 01:59:02 +00001043
Dan Gohman97d11632009-02-15 23:59:32 +00001044 // Similarly, a right-shift of a constant sign-bit will have exactly
1045 // one bit set.
1046 if (Val.getOpcode() == ISD::SRL)
1047 if (ConstantSDNode *C =
1048 dyn_cast<ConstantSDNode>(Val.getNode()->getOperand(0)))
1049 if (C->getAPIntValue().isSignBit())
1050 return true;
1051
1052 // More could be done here, though the above checks are enough
1053 // to handle some common cases.
1054
1055 // Fall back to ComputeMaskedBits to catch other known cases.
Owen Andersone50ed302009-08-10 22:56:29 +00001056 EVT OpVT = Val.getValueType();
Dan Gohman5b870af2010-03-02 02:14:38 +00001057 unsigned BitWidth = OpVT.getScalarType().getSizeInBits();
Dan Gohmane5af2d32009-01-29 01:59:02 +00001058 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001059 DAG.ComputeMaskedBits(Val, KnownZero, KnownOne);
Dale Johannesen85b0ede2009-02-11 19:19:41 +00001060 return (KnownZero.countPopulation() == BitWidth - 1) &&
1061 (KnownOne.countPopulation() == 1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00001062}
Chris Lattner5c3e21d2006-05-06 09:27:13 +00001063
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001064/// SimplifySetCC - Try to simplify a setcc built with the specified operands
Dan Gohman475871a2008-07-27 21:46:04 +00001065/// and cc. If it is unable to simplify it, return a null SDValue.
1066SDValue
Owen Andersone50ed302009-08-10 22:56:29 +00001067TargetLowering::SimplifySetCC(EVT VT, SDValue N0, SDValue N1,
Evan Chengfa1eb272007-02-08 22:13:59 +00001068 ISD::CondCode Cond, bool foldBooleans,
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001069 DAGCombinerInfo &DCI, DebugLoc dl) const {
Evan Chengfa1eb272007-02-08 22:13:59 +00001070 SelectionDAG &DAG = DCI.DAG;
1071
1072 // These setcc operations always fold.
1073 switch (Cond) {
1074 default: break;
1075 case ISD::SETFALSE:
1076 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
1077 case ISD::SETTRUE:
1078 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
1079 }
1080
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001081 // Ensure that the constant occurs on the RHS, and fold constant
1082 // comparisons.
1083 if (isa<ConstantSDNode>(N0.getNode()))
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001084 return DAG.getSetCC(dl, VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
Eric Christopher362fee92011-06-17 20:41:29 +00001085
Gabor Greifba36cb52008-08-28 21:40:38 +00001086 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
Dan Gohman6c6cd1c2008-03-03 22:22:56 +00001087 const APInt &C1 = N1C->getAPIntValue();
Dale Johannesen89217a62008-11-07 01:28:02 +00001088
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001089 // If the LHS is '(srl (ctlz x), 5)', the RHS is 0/1, and this is an
1090 // equality comparison, then we're just comparing whether X itself is
1091 // zero.
1092 if (N0.getOpcode() == ISD::SRL && (C1 == 0 || C1 == 1) &&
1093 N0.getOperand(0).getOpcode() == ISD::CTLZ &&
1094 N0.getOperand(1).getOpcode() == ISD::Constant) {
Evan Cheng347a9cb2010-01-07 20:58:44 +00001095 const APInt &ShAmt
1096 = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001097 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
1098 ShAmt == Log2_32(N0.getValueType().getSizeInBits())) {
1099 if ((C1 == 0) == (Cond == ISD::SETEQ)) {
1100 // (srl (ctlz x), 5) == 0 -> X != 0
1101 // (srl (ctlz x), 5) != 1 -> X != 0
1102 Cond = ISD::SETNE;
1103 } else {
1104 // (srl (ctlz x), 5) != 0 -> X == 0
1105 // (srl (ctlz x), 5) == 1 -> X == 0
1106 Cond = ISD::SETEQ;
1107 }
1108 SDValue Zero = DAG.getConstant(0, N0.getValueType());
1109 return DAG.getSetCC(dl, VT, N0.getOperand(0).getOperand(0),
1110 Zero, Cond);
1111 }
1112 }
1113
Benjamin Kramerd8228922011-01-17 12:04:57 +00001114 SDValue CTPOP = N0;
1115 // Look through truncs that don't change the value of a ctpop.
1116 if (N0.hasOneUse() && N0.getOpcode() == ISD::TRUNCATE)
1117 CTPOP = N0.getOperand(0);
1118
1119 if (CTPOP.hasOneUse() && CTPOP.getOpcode() == ISD::CTPOP &&
Benjamin Kramerc9b6a3e2011-01-17 18:00:28 +00001120 (N0 == CTPOP || N0.getValueType().getSizeInBits() >
Benjamin Kramerd8228922011-01-17 12:04:57 +00001121 Log2_32_Ceil(CTPOP.getValueType().getSizeInBits()))) {
1122 EVT CTVT = CTPOP.getValueType();
1123 SDValue CTOp = CTPOP.getOperand(0);
1124
1125 // (ctpop x) u< 2 -> (x & x-1) == 0
1126 // (ctpop x) u> 1 -> (x & x-1) != 0
1127 if ((Cond == ISD::SETULT && C1 == 2) || (Cond == ISD::SETUGT && C1 == 1)){
1128 SDValue Sub = DAG.getNode(ISD::SUB, dl, CTVT, CTOp,
1129 DAG.getConstant(1, CTVT));
1130 SDValue And = DAG.getNode(ISD::AND, dl, CTVT, CTOp, Sub);
1131 ISD::CondCode CC = Cond == ISD::SETULT ? ISD::SETEQ : ISD::SETNE;
1132 return DAG.getSetCC(dl, VT, And, DAG.getConstant(0, CTVT), CC);
1133 }
1134
Sylvestre Ledru94c22712012-09-27 10:14:43 +00001135 // TODO: (ctpop x) == 1 -> x && (x & x-1) == 0 iff ctpop is illegal.
Benjamin Kramerd8228922011-01-17 12:04:57 +00001136 }
1137
Benjamin Kramere7cf0622011-04-22 18:47:44 +00001138 // (zext x) == C --> x == (trunc C)
1139 if (DCI.isBeforeLegalize() && N0->hasOneUse() &&
1140 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
1141 unsigned MinBits = N0.getValueSizeInBits();
1142 SDValue PreZExt;
1143 if (N0->getOpcode() == ISD::ZERO_EXTEND) {
1144 // ZExt
1145 MinBits = N0->getOperand(0).getValueSizeInBits();
1146 PreZExt = N0->getOperand(0);
1147 } else if (N0->getOpcode() == ISD::AND) {
1148 // DAGCombine turns costly ZExts into ANDs
1149 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0->getOperand(1)))
1150 if ((C->getAPIntValue()+1).isPowerOf2()) {
1151 MinBits = C->getAPIntValue().countTrailingOnes();
1152 PreZExt = N0->getOperand(0);
1153 }
1154 } else if (LoadSDNode *LN0 = dyn_cast<LoadSDNode>(N0)) {
1155 // ZEXTLOAD
1156 if (LN0->getExtensionType() == ISD::ZEXTLOAD) {
1157 MinBits = LN0->getMemoryVT().getSizeInBits();
1158 PreZExt = N0;
1159 }
1160 }
1161
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00001162 // Make sure we're not losing bits from the constant.
Benjamin Kramer8401ed22013-05-16 18:47:58 +00001163 if (MinBits < C1.getBitWidth() && MinBits >= C1.getActiveBits()) {
Benjamin Kramere7cf0622011-04-22 18:47:44 +00001164 EVT MinVT = EVT::getIntegerVT(*DAG.getContext(), MinBits);
1165 if (isTypeDesirableForOp(ISD::SETCC, MinVT)) {
1166 // Will get folded away.
1167 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, MinVT, PreZExt);
1168 SDValue C = DAG.getConstant(C1.trunc(MinBits), MinVT);
1169 return DAG.getSetCC(dl, VT, Trunc, C, Cond);
1170 }
1171 }
1172 }
1173
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001174 // If the LHS is '(and load, const)', the RHS is 0,
1175 // the test is for equality or unsigned, and all 1 bits of the const are
1176 // in the same partial word, see if we can shorten the load.
1177 if (DCI.isBeforeLegalize() &&
1178 N0.getOpcode() == ISD::AND && C1 == 0 &&
1179 N0.getNode()->hasOneUse() &&
1180 isa<LoadSDNode>(N0.getOperand(0)) &&
1181 N0.getOperand(0).getNode()->hasOneUse() &&
1182 isa<ConstantSDNode>(N0.getOperand(1))) {
1183 LoadSDNode *Lod = cast<LoadSDNode>(N0.getOperand(0));
Evan Cheng347a9cb2010-01-07 20:58:44 +00001184 APInt bestMask;
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001185 unsigned bestWidth = 0, bestOffset = 0;
Evan Cheng347a9cb2010-01-07 20:58:44 +00001186 if (!Lod->isVolatile() && Lod->isUnindexed()) {
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001187 unsigned origWidth = N0.getValueType().getSizeInBits();
Evan Cheng347a9cb2010-01-07 20:58:44 +00001188 unsigned maskWidth = origWidth;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001189 // We can narrow (e.g.) 16-bit extending loads on 32-bit target to
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001190 // 8 bits, but have to be careful...
1191 if (Lod->getExtensionType() != ISD::NON_EXTLOAD)
1192 origWidth = Lod->getMemoryVT().getSizeInBits();
Evan Cheng347a9cb2010-01-07 20:58:44 +00001193 const APInt &Mask =
1194 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001195 for (unsigned width = origWidth / 2; width>=8; width /= 2) {
Evan Cheng347a9cb2010-01-07 20:58:44 +00001196 APInt newMask = APInt::getLowBitsSet(maskWidth, width);
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001197 for (unsigned offset=0; offset<origWidth/width; offset++) {
1198 if ((newMask & Mask) == Mask) {
Benjamin Kramer69e42db2013-01-11 20:05:37 +00001199 if (!getDataLayout()->isLittleEndian())
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001200 bestOffset = (origWidth/width - offset - 1) * (width/8);
1201 else
1202 bestOffset = (uint64_t)offset * (width/8);
Evan Cheng347a9cb2010-01-07 20:58:44 +00001203 bestMask = Mask.lshr(offset * (width/8) * 8);
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001204 bestWidth = width;
1205 break;
Dale Johannesen89217a62008-11-07 01:28:02 +00001206 }
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001207 newMask = newMask << width;
Dale Johannesen89217a62008-11-07 01:28:02 +00001208 }
1209 }
1210 }
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001211 if (bestWidth) {
Chris Lattnerc0c7fca2011-04-14 04:12:47 +00001212 EVT newVT = EVT::getIntegerVT(*DAG.getContext(), bestWidth);
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001213 if (newVT.isRound()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001214 EVT PtrType = Lod->getOperand(1).getValueType();
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001215 SDValue Ptr = Lod->getBasePtr();
1216 if (bestOffset != 0)
1217 Ptr = DAG.getNode(ISD::ADD, dl, PtrType, Lod->getBasePtr(),
1218 DAG.getConstant(bestOffset, PtrType));
1219 unsigned NewAlign = MinAlign(Lod->getAlignment(), bestOffset);
1220 SDValue NewLoad = DAG.getLoad(newVT, dl, Lod->getChain(), Ptr,
Chris Lattnerecf42c42010-09-21 16:36:31 +00001221 Lod->getPointerInfo().getWithOffset(bestOffset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001222 false, false, false, NewAlign);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001223 return DAG.getSetCC(dl, VT,
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001224 DAG.getNode(ISD::AND, dl, newVT, NewLoad,
Evan Cheng347a9cb2010-01-07 20:58:44 +00001225 DAG.getConstant(bestMask.trunc(bestWidth),
1226 newVT)),
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001227 DAG.getConstant(0LL, newVT), Cond);
Evan Chengfa1eb272007-02-08 22:13:59 +00001228 }
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001229 }
1230 }
Evan Chengfa1eb272007-02-08 22:13:59 +00001231
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001232 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
1233 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
1234 unsigned InSize = N0.getOperand(0).getValueType().getSizeInBits();
1235
1236 // If the comparison constant has bits in the upper part, the
1237 // zero-extended value could never match.
1238 if (C1.intersects(APInt::getHighBitsSet(C1.getBitWidth(),
1239 C1.getBitWidth() - InSize))) {
Evan Chengfa1eb272007-02-08 22:13:59 +00001240 switch (Cond) {
Evan Chengfa1eb272007-02-08 22:13:59 +00001241 case ISD::SETUGT:
1242 case ISD::SETUGE:
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001243 case ISD::SETEQ: return DAG.getConstant(0, VT);
Evan Chengfa1eb272007-02-08 22:13:59 +00001244 case ISD::SETULT:
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001245 case ISD::SETULE:
1246 case ISD::SETNE: return DAG.getConstant(1, VT);
1247 case ISD::SETGT:
1248 case ISD::SETGE:
1249 // True if the sign bit of C1 is set.
1250 return DAG.getConstant(C1.isNegative(), VT);
1251 case ISD::SETLT:
1252 case ISD::SETLE:
1253 // True if the sign bit of C1 isn't set.
1254 return DAG.getConstant(C1.isNonNegative(), VT);
1255 default:
Jakob Stoklund Olesen78d12642009-07-24 18:22:59 +00001256 break;
1257 }
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001258 }
Evan Chengfa1eb272007-02-08 22:13:59 +00001259
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001260 // Otherwise, we can perform the comparison with the low bits.
1261 switch (Cond) {
1262 case ISD::SETEQ:
1263 case ISD::SETNE:
1264 case ISD::SETUGT:
1265 case ISD::SETUGE:
1266 case ISD::SETULT:
1267 case ISD::SETULE: {
Patrik Hagglund34525f92012-12-11 11:14:33 +00001268 EVT newVT = N0.getOperand(0).getValueType();
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001269 if (DCI.isBeforeLegalizeOps() ||
1270 (isOperationLegal(ISD::SETCC, newVT) &&
Patrik Hagglund9c5ab932012-12-19 10:09:26 +00001271 getCondCodeAction(Cond, newVT.getSimpleVT())==Legal))
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001272 return DAG.getSetCC(dl, VT, N0.getOperand(0),
Jay Foad40f8f622010-12-07 08:25:19 +00001273 DAG.getConstant(C1.trunc(InSize), newVT),
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001274 Cond);
1275 break;
1276 }
1277 default:
1278 break; // todo, be more careful with signed comparisons
1279 }
1280 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Evan Cheng2c755ba2010-02-27 07:36:59 +00001281 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
Owen Andersone50ed302009-08-10 22:56:29 +00001282 EVT ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001283 unsigned ExtSrcTyBits = ExtSrcTy.getSizeInBits();
Owen Andersone50ed302009-08-10 22:56:29 +00001284 EVT ExtDstTy = N0.getValueType();
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001285 unsigned ExtDstTyBits = ExtDstTy.getSizeInBits();
1286
Eli Friedmanad78a882010-07-30 06:44:31 +00001287 // If the constant doesn't fit into the number of bits for the source of
1288 // the sign extension, it is impossible for both sides to be equal.
1289 if (C1.getMinSignedBits() > ExtSrcTyBits)
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001290 return DAG.getConstant(Cond == ISD::SETNE, VT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001291
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001292 SDValue ZextOp;
Owen Andersone50ed302009-08-10 22:56:29 +00001293 EVT Op0Ty = N0.getOperand(0).getValueType();
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001294 if (Op0Ty == ExtSrcTy) {
1295 ZextOp = N0.getOperand(0);
1296 } else {
1297 APInt Imm = APInt::getLowBitsSet(ExtDstTyBits, ExtSrcTyBits);
1298 ZextOp = DAG.getNode(ISD::AND, dl, Op0Ty, N0.getOperand(0),
1299 DAG.getConstant(Imm, Op0Ty));
1300 }
1301 if (!DCI.isCalledByLegalizer())
1302 DCI.AddToWorklist(ZextOp.getNode());
1303 // Otherwise, make this a use of a zext.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001304 return DAG.getSetCC(dl, VT, ZextOp,
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001305 DAG.getConstant(C1 & APInt::getLowBitsSet(
1306 ExtDstTyBits,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001307 ExtSrcTyBits),
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001308 ExtDstTy),
1309 Cond);
1310 } else if ((N1C->isNullValue() || N1C->getAPIntValue() == 1) &&
1311 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001312 // SETCC (SETCC), [0|1], [EQ|NE] -> SETCC
Evan Cheng2c755ba2010-02-27 07:36:59 +00001313 if (N0.getOpcode() == ISD::SETCC &&
1314 isTypeLegal(VT) && VT.bitsLE(N0.getValueType())) {
Evan Cheng347a9cb2010-01-07 20:58:44 +00001315 bool TrueWhenTrue = (Cond == ISD::SETEQ) ^ (N1C->getAPIntValue() != 1);
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001316 if (TrueWhenTrue)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001317 return DAG.getNode(ISD::TRUNCATE, dl, VT, N0);
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001318 // Invert the condition.
1319 ISD::CondCode CC = cast<CondCodeSDNode>(N0.getOperand(2))->get();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001320 CC = ISD::getSetCCInverse(CC,
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001321 N0.getOperand(0).getValueType().isInteger());
1322 return DAG.getSetCC(dl, VT, N0.getOperand(0), N0.getOperand(1), CC);
Evan Chengfa1eb272007-02-08 22:13:59 +00001323 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00001324
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001325 if ((N0.getOpcode() == ISD::XOR ||
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001326 (N0.getOpcode() == ISD::AND &&
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001327 N0.getOperand(0).getOpcode() == ISD::XOR &&
1328 N0.getOperand(1) == N0.getOperand(0).getOperand(1))) &&
1329 isa<ConstantSDNode>(N0.getOperand(1)) &&
1330 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue() == 1) {
1331 // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We
1332 // can only do this if the top bits are known zero.
1333 unsigned BitWidth = N0.getValueSizeInBits();
1334 if (DAG.MaskedValueIsZero(N0,
1335 APInt::getHighBitsSet(BitWidth,
1336 BitWidth-1))) {
1337 // Okay, get the un-inverted input value.
1338 SDValue Val;
1339 if (N0.getOpcode() == ISD::XOR)
1340 Val = N0.getOperand(0);
1341 else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001342 assert(N0.getOpcode() == ISD::AND &&
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001343 N0.getOperand(0).getOpcode() == ISD::XOR);
1344 // ((X^1)&1)^1 -> X & 1
1345 Val = DAG.getNode(ISD::AND, dl, N0.getValueType(),
1346 N0.getOperand(0).getOperand(0),
1347 N0.getOperand(1));
1348 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00001349
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001350 return DAG.getSetCC(dl, VT, Val, N1,
1351 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
1352 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00001353 } else if (N1C->getAPIntValue() == 1 &&
1354 (VT == MVT::i1 ||
Duncan Sands28b77e92011-09-06 19:07:46 +00001355 getBooleanContents(false) == ZeroOrOneBooleanContent)) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00001356 SDValue Op0 = N0;
1357 if (Op0.getOpcode() == ISD::TRUNCATE)
1358 Op0 = Op0.getOperand(0);
1359
1360 if ((Op0.getOpcode() == ISD::XOR) &&
1361 Op0.getOperand(0).getOpcode() == ISD::SETCC &&
1362 Op0.getOperand(1).getOpcode() == ISD::SETCC) {
1363 // (xor (setcc), (setcc)) == / != 1 -> (setcc) != / == (setcc)
1364 Cond = (Cond == ISD::SETEQ) ? ISD::SETNE : ISD::SETEQ;
1365 return DAG.getSetCC(dl, VT, Op0.getOperand(0), Op0.getOperand(1),
1366 Cond);
Craig Topper40b4a812012-12-19 06:12:28 +00001367 }
1368 if (Op0.getOpcode() == ISD::AND &&
1369 isa<ConstantSDNode>(Op0.getOperand(1)) &&
1370 cast<ConstantSDNode>(Op0.getOperand(1))->getAPIntValue() == 1) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00001371 // If this is (X&1) == / != 1, normalize it to (X&1) != / == 0.
Anton Korobeynikov17458a72010-05-01 12:52:34 +00001372 if (Op0.getValueType().bitsGT(VT))
Evan Cheng2c755ba2010-02-27 07:36:59 +00001373 Op0 = DAG.getNode(ISD::AND, dl, VT,
1374 DAG.getNode(ISD::TRUNCATE, dl, VT, Op0.getOperand(0)),
1375 DAG.getConstant(1, VT));
Anton Korobeynikov17458a72010-05-01 12:52:34 +00001376 else if (Op0.getValueType().bitsLT(VT))
1377 Op0 = DAG.getNode(ISD::AND, dl, VT,
1378 DAG.getNode(ISD::ANY_EXTEND, dl, VT, Op0.getOperand(0)),
1379 DAG.getConstant(1, VT));
1380
Evan Cheng2c755ba2010-02-27 07:36:59 +00001381 return DAG.getSetCC(dl, VT, Op0,
1382 DAG.getConstant(0, Op0.getValueType()),
1383 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
1384 }
Craig Topper40b4a812012-12-19 06:12:28 +00001385 if (Op0.getOpcode() == ISD::AssertZext &&
1386 cast<VTSDNode>(Op0.getOperand(1))->getVT() == MVT::i1)
1387 return DAG.getSetCC(dl, VT, Op0,
1388 DAG.getConstant(0, Op0.getValueType()),
1389 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
Evan Chengfa1eb272007-02-08 22:13:59 +00001390 }
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001391 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001392
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001393 APInt MinVal, MaxVal;
1394 unsigned OperandBitSize = N1C->getValueType(0).getSizeInBits();
1395 if (ISD::isSignedIntSetCC(Cond)) {
1396 MinVal = APInt::getSignedMinValue(OperandBitSize);
1397 MaxVal = APInt::getSignedMaxValue(OperandBitSize);
1398 } else {
1399 MinVal = APInt::getMinValue(OperandBitSize);
1400 MaxVal = APInt::getMaxValue(OperandBitSize);
1401 }
Evan Chengfa1eb272007-02-08 22:13:59 +00001402
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001403 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
1404 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
1405 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
1406 // X >= C0 --> X > (C0-1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001407 return DAG.getSetCC(dl, VT, N0,
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001408 DAG.getConstant(C1-1, N1.getValueType()),
1409 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
1410 }
Evan Chengfa1eb272007-02-08 22:13:59 +00001411
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001412 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
1413 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
1414 // X <= C0 --> X < (C0+1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001415 return DAG.getSetCC(dl, VT, N0,
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001416 DAG.getConstant(C1+1, N1.getValueType()),
1417 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
1418 }
Evan Chengfa1eb272007-02-08 22:13:59 +00001419
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001420 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
1421 return DAG.getConstant(0, VT); // X < MIN --> false
1422 if ((Cond == ISD::SETGE || Cond == ISD::SETUGE) && C1 == MinVal)
1423 return DAG.getConstant(1, VT); // X >= MIN --> true
1424 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal)
1425 return DAG.getConstant(0, VT); // X > MAX --> false
1426 if ((Cond == ISD::SETLE || Cond == ISD::SETULE) && C1 == MaxVal)
1427 return DAG.getConstant(1, VT); // X <= MAX --> true
Evan Chengfa1eb272007-02-08 22:13:59 +00001428
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001429 // Canonicalize setgt X, Min --> setne X, Min
1430 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
1431 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETNE);
1432 // Canonicalize setlt X, Max --> setne X, Max
1433 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
1434 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETNE);
Evan Chengfa1eb272007-02-08 22:13:59 +00001435
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001436 // If we have setult X, 1, turn it into seteq X, 0
1437 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001438 return DAG.getSetCC(dl, VT, N0,
1439 DAG.getConstant(MinVal, N0.getValueType()),
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001440 ISD::SETEQ);
1441 // If we have setugt X, Max-1, turn it into seteq X, Max
Craig Topper85022562012-12-19 06:43:58 +00001442 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001443 return DAG.getSetCC(dl, VT, N0,
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001444 DAG.getConstant(MaxVal, N0.getValueType()),
1445 ISD::SETEQ);
Evan Chengfa1eb272007-02-08 22:13:59 +00001446
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001447 // If we have "setcc X, C0", check to see if we can shrink the immediate
1448 // by changing cc.
Evan Chengfa1eb272007-02-08 22:13:59 +00001449
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001450 // SETUGT X, SINTMAX -> SETLT X, 0
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001451 if (Cond == ISD::SETUGT &&
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001452 C1 == APInt::getSignedMaxValue(OperandBitSize))
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001453 return DAG.getSetCC(dl, VT, N0,
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001454 DAG.getConstant(0, N1.getValueType()),
1455 ISD::SETLT);
Evan Chengfa1eb272007-02-08 22:13:59 +00001456
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001457 // SETULT X, SINTMIN -> SETGT X, -1
1458 if (Cond == ISD::SETULT &&
1459 C1 == APInt::getSignedMinValue(OperandBitSize)) {
1460 SDValue ConstMinusOne =
1461 DAG.getConstant(APInt::getAllOnesValue(OperandBitSize),
1462 N1.getValueType());
1463 return DAG.getSetCC(dl, VT, N0, ConstMinusOne, ISD::SETGT);
1464 }
Evan Chengfa1eb272007-02-08 22:13:59 +00001465
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001466 // Fold bit comparisons when we can.
1467 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
Evan Chengd40d03e2010-01-06 19:38:29 +00001468 (VT == N0.getValueType() ||
1469 (isTypeLegal(VT) && VT.bitsLE(N0.getValueType()))) &&
1470 N0.getOpcode() == ISD::AND)
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001471 if (ConstantSDNode *AndRHS =
1472 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Benjamin Kramerb97cebd2012-08-17 15:54:21 +00001473 EVT ShiftTy = DCI.isBeforeLegalizeOps() ?
Owen Anderson95771af2011-02-25 21:41:48 +00001474 getPointerTy() : getShiftAmountTy(N0.getValueType());
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001475 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
1476 // Perform the xform if the AND RHS is a single bit.
Evan Cheng347a9cb2010-01-07 20:58:44 +00001477 if (AndRHS->getAPIntValue().isPowerOf2()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00001478 return DAG.getNode(ISD::TRUNCATE, dl, VT,
1479 DAG.getNode(ISD::SRL, dl, N0.getValueType(), N0,
Evan Cheng347a9cb2010-01-07 20:58:44 +00001480 DAG.getConstant(AndRHS->getAPIntValue().logBase2(), ShiftTy)));
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001481 }
Evan Cheng347a9cb2010-01-07 20:58:44 +00001482 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getAPIntValue()) {
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001483 // (X & 8) == 8 --> (X & 8) >> 3
1484 // Perform the xform if C1 is a single bit.
1485 if (C1.isPowerOf2()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00001486 return DAG.getNode(ISD::TRUNCATE, dl, VT,
1487 DAG.getNode(ISD::SRL, dl, N0.getValueType(), N0,
1488 DAG.getConstant(C1.logBase2(), ShiftTy)));
Evan Chengfa1eb272007-02-08 22:13:59 +00001489 }
1490 }
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001491 }
Evan Cheng70e10d32012-07-17 06:53:39 +00001492
Evan Chengb4d49592012-07-17 07:47:50 +00001493 if (C1.getMinSignedBits() <= 64 &&
1494 !isLegalICmpImmediate(C1.getSExtValue())) {
Evan Cheng70e10d32012-07-17 06:53:39 +00001495 // (X & -256) == 256 -> (X >> 8) == 1
1496 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
1497 N0.getOpcode() == ISD::AND && N0.hasOneUse()) {
1498 if (ConstantSDNode *AndRHS =
1499 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1500 const APInt &AndRHSC = AndRHS->getAPIntValue();
1501 if ((-AndRHSC).isPowerOf2() && (AndRHSC & C1) == C1) {
1502 unsigned ShiftBits = AndRHSC.countTrailingZeros();
Benjamin Kramerb97cebd2012-08-17 15:54:21 +00001503 EVT ShiftTy = DCI.isBeforeLegalizeOps() ?
Evan Cheng70e10d32012-07-17 06:53:39 +00001504 getPointerTy() : getShiftAmountTy(N0.getValueType());
1505 EVT CmpTy = N0.getValueType();
1506 SDValue Shift = DAG.getNode(ISD::SRL, dl, CmpTy, N0.getOperand(0),
1507 DAG.getConstant(ShiftBits, ShiftTy));
1508 SDValue CmpRHS = DAG.getConstant(C1.lshr(ShiftBits), CmpTy);
1509 return DAG.getSetCC(dl, VT, Shift, CmpRHS, Cond);
1510 }
1511 }
Evan Chengf5c05392012-07-17 08:31:11 +00001512 } else if (Cond == ISD::SETULT || Cond == ISD::SETUGE ||
1513 Cond == ISD::SETULE || Cond == ISD::SETUGT) {
1514 bool AdjOne = (Cond == ISD::SETULE || Cond == ISD::SETUGT);
1515 // X < 0x100000000 -> (X >> 32) < 1
1516 // X >= 0x100000000 -> (X >> 32) >= 1
1517 // X <= 0x0ffffffff -> (X >> 32) < 1
1518 // X > 0x0ffffffff -> (X >> 32) >= 1
1519 unsigned ShiftBits;
1520 APInt NewC = C1;
1521 ISD::CondCode NewCond = Cond;
1522 if (AdjOne) {
1523 ShiftBits = C1.countTrailingOnes();
1524 NewC = NewC + 1;
1525 NewCond = (Cond == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
1526 } else {
1527 ShiftBits = C1.countTrailingZeros();
1528 }
1529 NewC = NewC.lshr(ShiftBits);
1530 if (ShiftBits && isLegalICmpImmediate(NewC.getSExtValue())) {
Benjamin Kramerb97cebd2012-08-17 15:54:21 +00001531 EVT ShiftTy = DCI.isBeforeLegalizeOps() ?
Evan Chengf5c05392012-07-17 08:31:11 +00001532 getPointerTy() : getShiftAmountTy(N0.getValueType());
1533 EVT CmpTy = N0.getValueType();
1534 SDValue Shift = DAG.getNode(ISD::SRL, dl, CmpTy, N0,
1535 DAG.getConstant(ShiftBits, ShiftTy));
1536 SDValue CmpRHS = DAG.getConstant(NewC, CmpTy);
1537 return DAG.getSetCC(dl, VT, Shift, CmpRHS, NewCond);
1538 }
Evan Cheng70e10d32012-07-17 06:53:39 +00001539 }
1540 }
Evan Chengfa1eb272007-02-08 22:13:59 +00001541 }
1542
Gabor Greifba36cb52008-08-28 21:40:38 +00001543 if (isa<ConstantFPSDNode>(N0.getNode())) {
Evan Chengfa1eb272007-02-08 22:13:59 +00001544 // Constant fold or commute setcc.
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001545 SDValue O = DAG.FoldSetCC(VT, N0, N1, Cond, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00001546 if (O.getNode()) return O;
1547 } else if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
Chris Lattner63079f02007-12-29 08:37:08 +00001548 // If the RHS of an FP comparison is a constant, simplify it away in
1549 // some cases.
1550 if (CFP->getValueAPF().isNaN()) {
1551 // If an operand is known to be a nan, we can fold it.
1552 switch (ISD::getUnorderedFlavor(Cond)) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001553 default: llvm_unreachable("Unknown flavor!");
Chris Lattner63079f02007-12-29 08:37:08 +00001554 case 0: // Known false.
1555 return DAG.getConstant(0, VT);
1556 case 1: // Known true.
1557 return DAG.getConstant(1, VT);
Chris Lattner1c3e1e22007-12-30 21:21:10 +00001558 case 2: // Undefined.
Dale Johannesene8d72302009-02-06 23:05:02 +00001559 return DAG.getUNDEF(VT);
Chris Lattner63079f02007-12-29 08:37:08 +00001560 }
1561 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001562
Chris Lattner63079f02007-12-29 08:37:08 +00001563 // Otherwise, we know the RHS is not a NaN. Simplify the node to drop the
1564 // constant if knowing that the operand is non-nan is enough. We prefer to
1565 // have SETO(x,x) instead of SETO(x, 0.0) because this avoids having to
1566 // materialize 0.0.
1567 if (Cond == ISD::SETO || Cond == ISD::SETUO)
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001568 return DAG.getSetCC(dl, VT, N0, N0, Cond);
Dan Gohman11eab022009-09-26 15:24:17 +00001569
1570 // If the condition is not legal, see if we can find an equivalent one
1571 // which is legal.
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00001572 if (!isCondCodeLegal(Cond, N0.getSimpleValueType())) {
Dan Gohman11eab022009-09-26 15:24:17 +00001573 // If the comparison was an awkward floating-point == or != and one of
1574 // the comparison operands is infinity or negative infinity, convert the
1575 // condition to a less-awkward <= or >=.
1576 if (CFP->getValueAPF().isInfinity()) {
1577 if (CFP->getValueAPF().isNegative()) {
1578 if (Cond == ISD::SETOEQ &&
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00001579 isCondCodeLegal(ISD::SETOLE, N0.getSimpleValueType()))
Dan Gohman11eab022009-09-26 15:24:17 +00001580 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOLE);
1581 if (Cond == ISD::SETUEQ &&
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00001582 isCondCodeLegal(ISD::SETOLE, N0.getSimpleValueType()))
Dan Gohman11eab022009-09-26 15:24:17 +00001583 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETULE);
1584 if (Cond == ISD::SETUNE &&
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00001585 isCondCodeLegal(ISD::SETUGT, N0.getSimpleValueType()))
Dan Gohman11eab022009-09-26 15:24:17 +00001586 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETUGT);
1587 if (Cond == ISD::SETONE &&
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00001588 isCondCodeLegal(ISD::SETUGT, N0.getSimpleValueType()))
Dan Gohman11eab022009-09-26 15:24:17 +00001589 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOGT);
1590 } else {
1591 if (Cond == ISD::SETOEQ &&
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00001592 isCondCodeLegal(ISD::SETOGE, N0.getSimpleValueType()))
Dan Gohman11eab022009-09-26 15:24:17 +00001593 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOGE);
1594 if (Cond == ISD::SETUEQ &&
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00001595 isCondCodeLegal(ISD::SETOGE, N0.getSimpleValueType()))
Dan Gohman11eab022009-09-26 15:24:17 +00001596 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETUGE);
1597 if (Cond == ISD::SETUNE &&
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00001598 isCondCodeLegal(ISD::SETULT, N0.getSimpleValueType()))
Dan Gohman11eab022009-09-26 15:24:17 +00001599 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETULT);
1600 if (Cond == ISD::SETONE &&
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00001601 isCondCodeLegal(ISD::SETULT, N0.getSimpleValueType()))
Dan Gohman11eab022009-09-26 15:24:17 +00001602 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOLT);
1603 }
1604 }
1605 }
Evan Chengfa1eb272007-02-08 22:13:59 +00001606 }
1607
1608 if (N0 == N1) {
Duncan Sandse7de3b22012-07-05 09:32:46 +00001609 // The sext(setcc()) => setcc() optimization relies on the appropriate
1610 // constant being emitted.
Nadav Roteme7576402012-09-06 11:13:55 +00001611 uint64_t EqVal = 0;
Duncan Sandse7de3b22012-07-05 09:32:46 +00001612 switch (getBooleanContents(N0.getValueType().isVector())) {
Duncan Sandse7de3b22012-07-05 09:32:46 +00001613 case UndefinedBooleanContent:
1614 case ZeroOrOneBooleanContent:
1615 EqVal = ISD::isTrueWhenEqual(Cond);
1616 break;
1617 case ZeroOrNegativeOneBooleanContent:
1618 EqVal = ISD::isTrueWhenEqual(Cond) ? -1 : 0;
1619 break;
1620 }
1621
Evan Chengfa1eb272007-02-08 22:13:59 +00001622 // We can always fold X == X for integer setcc's.
Chad Rosier9dbb0182012-04-03 20:11:24 +00001623 if (N0.getValueType().isInteger()) {
Duncan Sandse7de3b22012-07-05 09:32:46 +00001624 return DAG.getConstant(EqVal, VT);
Chad Rosier9dbb0182012-04-03 20:11:24 +00001625 }
Evan Chengfa1eb272007-02-08 22:13:59 +00001626 unsigned UOF = ISD::getUnorderedFlavor(Cond);
1627 if (UOF == 2) // FP operators that are undefined on NaNs.
Duncan Sandse7de3b22012-07-05 09:32:46 +00001628 return DAG.getConstant(EqVal, VT);
Evan Chengfa1eb272007-02-08 22:13:59 +00001629 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
Duncan Sandse7de3b22012-07-05 09:32:46 +00001630 return DAG.getConstant(EqVal, VT);
Evan Chengfa1eb272007-02-08 22:13:59 +00001631 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
1632 // if it is not already.
1633 ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
Micah Villmow8c574be2012-07-31 18:07:43 +00001634 if (NewCond != Cond && (DCI.isBeforeLegalizeOps() ||
Patrik Hagglund9c5ab932012-12-19 10:09:26 +00001635 getCondCodeAction(NewCond, N0.getSimpleValueType()) == Legal))
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001636 return DAG.getSetCC(dl, VT, N0, N1, NewCond);
Evan Chengfa1eb272007-02-08 22:13:59 +00001637 }
1638
1639 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00001640 N0.getValueType().isInteger()) {
Evan Chengfa1eb272007-02-08 22:13:59 +00001641 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
1642 N0.getOpcode() == ISD::XOR) {
1643 // Simplify (X+Y) == (X+Z) --> Y == Z
1644 if (N0.getOpcode() == N1.getOpcode()) {
1645 if (N0.getOperand(0) == N1.getOperand(0))
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001646 return DAG.getSetCC(dl, VT, N0.getOperand(1), N1.getOperand(1), Cond);
Evan Chengfa1eb272007-02-08 22:13:59 +00001647 if (N0.getOperand(1) == N1.getOperand(1))
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001648 return DAG.getSetCC(dl, VT, N0.getOperand(0), N1.getOperand(0), Cond);
Evan Chengfa1eb272007-02-08 22:13:59 +00001649 if (DAG.isCommutativeBinOp(N0.getOpcode())) {
1650 // If X op Y == Y op X, try other combinations.
1651 if (N0.getOperand(0) == N1.getOperand(1))
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001652 return DAG.getSetCC(dl, VT, N0.getOperand(1), N1.getOperand(0),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001653 Cond);
Evan Chengfa1eb272007-02-08 22:13:59 +00001654 if (N0.getOperand(1) == N1.getOperand(0))
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001655 return DAG.getSetCC(dl, VT, N0.getOperand(0), N1.getOperand(1),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001656 Cond);
Evan Chengfa1eb272007-02-08 22:13:59 +00001657 }
1658 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001659
Jakob Stoklund Olesen740cd652012-04-05 20:30:20 +00001660 // If RHS is a legal immediate value for a compare instruction, we need
1661 // to be careful about increasing register pressure needlessly.
1662 bool LegalRHSImm = false;
1663
Evan Chengfa1eb272007-02-08 22:13:59 +00001664 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
1665 if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1666 // Turn (X+C1) == C2 --> X == C2-C1
Gabor Greifba36cb52008-08-28 21:40:38 +00001667 if (N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse()) {
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001668 return DAG.getSetCC(dl, VT, N0.getOperand(0),
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001669 DAG.getConstant(RHSC->getAPIntValue()-
1670 LHSR->getAPIntValue(),
Evan Chengfa1eb272007-02-08 22:13:59 +00001671 N0.getValueType()), Cond);
1672 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001673
Sylvestre Ledru94c22712012-09-27 10:14:43 +00001674 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
Evan Chengfa1eb272007-02-08 22:13:59 +00001675 if (N0.getOpcode() == ISD::XOR)
1676 // If we know that all of the inverted bits are zero, don't bother
1677 // performing the inversion.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001678 if (DAG.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getAPIntValue()))
1679 return
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001680 DAG.getSetCC(dl, VT, N0.getOperand(0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001681 DAG.getConstant(LHSR->getAPIntValue() ^
1682 RHSC->getAPIntValue(),
1683 N0.getValueType()),
1684 Cond);
Evan Chengfa1eb272007-02-08 22:13:59 +00001685 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001686
Evan Chengfa1eb272007-02-08 22:13:59 +00001687 // Turn (C1-X) == C2 --> X == C1-C2
1688 if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001689 if (N0.getOpcode() == ISD::SUB && N0.getNode()->hasOneUse()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001690 return
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001691 DAG.getSetCC(dl, VT, N0.getOperand(1),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001692 DAG.getConstant(SUBC->getAPIntValue() -
1693 RHSC->getAPIntValue(),
1694 N0.getValueType()),
1695 Cond);
Evan Chengfa1eb272007-02-08 22:13:59 +00001696 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001697 }
Jakob Stoklund Olesen740cd652012-04-05 20:30:20 +00001698
1699 // Could RHSC fold directly into a compare?
1700 if (RHSC->getValueType(0).getSizeInBits() <= 64)
1701 LegalRHSImm = isLegalICmpImmediate(RHSC->getSExtValue());
Evan Chengfa1eb272007-02-08 22:13:59 +00001702 }
1703
1704 // Simplify (X+Z) == X --> Z == 0
Jakob Stoklund Olesen740cd652012-04-05 20:30:20 +00001705 // Don't do this if X is an immediate that can fold into a cmp
1706 // instruction and X+Z has other uses. It could be an induction variable
1707 // chain, and the transform would increase register pressure.
1708 if (!LegalRHSImm || N0.getNode()->hasOneUse()) {
1709 if (N0.getOperand(0) == N1)
1710 return DAG.getSetCC(dl, VT, N0.getOperand(1),
1711 DAG.getConstant(0, N0.getValueType()), Cond);
1712 if (N0.getOperand(1) == N1) {
1713 if (DAG.isCommutativeBinOp(N0.getOpcode()))
1714 return DAG.getSetCC(dl, VT, N0.getOperand(0),
1715 DAG.getConstant(0, N0.getValueType()), Cond);
Craig Topper85022562012-12-19 06:43:58 +00001716 if (N0.getNode()->hasOneUse()) {
Jakob Stoklund Olesen740cd652012-04-05 20:30:20 +00001717 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
1718 // (Z-X) == X --> Z == X<<1
1719 SDValue SH = DAG.getNode(ISD::SHL, dl, N1.getValueType(), N1,
Owen Anderson95771af2011-02-25 21:41:48 +00001720 DAG.getConstant(1, getShiftAmountTy(N1.getValueType())));
Jakob Stoklund Olesen740cd652012-04-05 20:30:20 +00001721 if (!DCI.isCalledByLegalizer())
1722 DCI.AddToWorklist(SH.getNode());
1723 return DAG.getSetCC(dl, VT, N0.getOperand(0), SH, Cond);
1724 }
Evan Chengfa1eb272007-02-08 22:13:59 +00001725 }
1726 }
1727 }
1728
1729 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
1730 N1.getOpcode() == ISD::XOR) {
1731 // Simplify X == (X+Z) --> Z == 0
Craig Topper85022562012-12-19 06:43:58 +00001732 if (N1.getOperand(0) == N0)
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001733 return DAG.getSetCC(dl, VT, N1.getOperand(1),
Evan Chengfa1eb272007-02-08 22:13:59 +00001734 DAG.getConstant(0, N1.getValueType()), Cond);
Craig Topper85022562012-12-19 06:43:58 +00001735 if (N1.getOperand(1) == N0) {
1736 if (DAG.isCommutativeBinOp(N1.getOpcode()))
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001737 return DAG.getSetCC(dl, VT, N1.getOperand(0),
Evan Chengfa1eb272007-02-08 22:13:59 +00001738 DAG.getConstant(0, N1.getValueType()), Cond);
Craig Topper85022562012-12-19 06:43:58 +00001739 if (N1.getNode()->hasOneUse()) {
Evan Chengfa1eb272007-02-08 22:13:59 +00001740 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
1741 // X == (Z-X) --> X<<1 == Z
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001742 SDValue SH = DAG.getNode(ISD::SHL, dl, N1.getValueType(), N0,
Owen Anderson95771af2011-02-25 21:41:48 +00001743 DAG.getConstant(1, getShiftAmountTy(N0.getValueType())));
Evan Chengfa1eb272007-02-08 22:13:59 +00001744 if (!DCI.isCalledByLegalizer())
Gabor Greifba36cb52008-08-28 21:40:38 +00001745 DCI.AddToWorklist(SH.getNode());
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001746 return DAG.getSetCC(dl, VT, SH, N1.getOperand(0), Cond);
Evan Chengfa1eb272007-02-08 22:13:59 +00001747 }
1748 }
1749 }
Dan Gohmane5af2d32009-01-29 01:59:02 +00001750
Dan Gohman2c65c3d2009-01-29 16:18:12 +00001751 // Simplify x&y == y to x&y != 0 if y has exactly one bit set.
Dale Johannesen85b0ede2009-02-11 19:19:41 +00001752 // Note that where y is variable and is known to have at most
1753 // one bit set (for example, if it is z&1) we cannot do this;
1754 // the expressions are not equivalent when y==0.
Dan Gohmane5af2d32009-01-29 01:59:02 +00001755 if (N0.getOpcode() == ISD::AND)
1756 if (N0.getOperand(0) == N1 || N0.getOperand(1) == N1) {
Dale Johannesen85b0ede2009-02-11 19:19:41 +00001757 if (ValueHasExactlyOneBitSet(N1, DAG)) {
Dan Gohmane5af2d32009-01-29 01:59:02 +00001758 Cond = ISD::getSetCCInverse(Cond, /*isInteger=*/true);
1759 SDValue Zero = DAG.getConstant(0, N1.getValueType());
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001760 return DAG.getSetCC(dl, VT, N0, Zero, Cond);
Dan Gohmane5af2d32009-01-29 01:59:02 +00001761 }
1762 }
1763 if (N1.getOpcode() == ISD::AND)
1764 if (N1.getOperand(0) == N0 || N1.getOperand(1) == N0) {
Dale Johannesen85b0ede2009-02-11 19:19:41 +00001765 if (ValueHasExactlyOneBitSet(N0, DAG)) {
Dan Gohmane5af2d32009-01-29 01:59:02 +00001766 Cond = ISD::getSetCCInverse(Cond, /*isInteger=*/true);
1767 SDValue Zero = DAG.getConstant(0, N0.getValueType());
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001768 return DAG.getSetCC(dl, VT, N1, Zero, Cond);
Dan Gohmane5af2d32009-01-29 01:59:02 +00001769 }
1770 }
Evan Chengfa1eb272007-02-08 22:13:59 +00001771 }
1772
1773 // Fold away ALL boolean setcc's.
Dan Gohman475871a2008-07-27 21:46:04 +00001774 SDValue Temp;
Owen Anderson825b72b2009-08-11 20:47:22 +00001775 if (N0.getValueType() == MVT::i1 && foldBooleans) {
Evan Chengfa1eb272007-02-08 22:13:59 +00001776 switch (Cond) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001777 default: llvm_unreachable("Unknown integer setcc!");
Bob Wilson4c245462009-01-22 17:39:32 +00001778 case ISD::SETEQ: // X == Y -> ~(X^Y)
Owen Anderson825b72b2009-08-11 20:47:22 +00001779 Temp = DAG.getNode(ISD::XOR, dl, MVT::i1, N0, N1);
1780 N0 = DAG.getNOT(dl, Temp, MVT::i1);
Evan Chengfa1eb272007-02-08 22:13:59 +00001781 if (!DCI.isCalledByLegalizer())
Gabor Greifba36cb52008-08-28 21:40:38 +00001782 DCI.AddToWorklist(Temp.getNode());
Evan Chengfa1eb272007-02-08 22:13:59 +00001783 break;
1784 case ISD::SETNE: // X != Y --> (X^Y)
Owen Anderson825b72b2009-08-11 20:47:22 +00001785 N0 = DAG.getNode(ISD::XOR, dl, MVT::i1, N0, N1);
Evan Chengfa1eb272007-02-08 22:13:59 +00001786 break;
Bob Wilson4c245462009-01-22 17:39:32 +00001787 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> ~X & Y
1788 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> ~X & Y
Owen Anderson825b72b2009-08-11 20:47:22 +00001789 Temp = DAG.getNOT(dl, N0, MVT::i1);
1790 N0 = DAG.getNode(ISD::AND, dl, MVT::i1, N1, Temp);
Evan Chengfa1eb272007-02-08 22:13:59 +00001791 if (!DCI.isCalledByLegalizer())
Gabor Greifba36cb52008-08-28 21:40:38 +00001792 DCI.AddToWorklist(Temp.getNode());
Evan Chengfa1eb272007-02-08 22:13:59 +00001793 break;
Bob Wilson4c245462009-01-22 17:39:32 +00001794 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> ~Y & X
1795 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> ~Y & X
Owen Anderson825b72b2009-08-11 20:47:22 +00001796 Temp = DAG.getNOT(dl, N1, MVT::i1);
1797 N0 = DAG.getNode(ISD::AND, dl, MVT::i1, N0, Temp);
Evan Chengfa1eb272007-02-08 22:13:59 +00001798 if (!DCI.isCalledByLegalizer())
Gabor Greifba36cb52008-08-28 21:40:38 +00001799 DCI.AddToWorklist(Temp.getNode());
Evan Chengfa1eb272007-02-08 22:13:59 +00001800 break;
Bob Wilson4c245462009-01-22 17:39:32 +00001801 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> ~X | Y
1802 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> ~X | Y
Owen Anderson825b72b2009-08-11 20:47:22 +00001803 Temp = DAG.getNOT(dl, N0, MVT::i1);
1804 N0 = DAG.getNode(ISD::OR, dl, MVT::i1, N1, Temp);
Evan Chengfa1eb272007-02-08 22:13:59 +00001805 if (!DCI.isCalledByLegalizer())
Gabor Greifba36cb52008-08-28 21:40:38 +00001806 DCI.AddToWorklist(Temp.getNode());
Evan Chengfa1eb272007-02-08 22:13:59 +00001807 break;
Bob Wilson4c245462009-01-22 17:39:32 +00001808 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> ~Y | X
1809 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> ~Y | X
Owen Anderson825b72b2009-08-11 20:47:22 +00001810 Temp = DAG.getNOT(dl, N1, MVT::i1);
1811 N0 = DAG.getNode(ISD::OR, dl, MVT::i1, N0, Temp);
Evan Chengfa1eb272007-02-08 22:13:59 +00001812 break;
1813 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001814 if (VT != MVT::i1) {
Evan Chengfa1eb272007-02-08 22:13:59 +00001815 if (!DCI.isCalledByLegalizer())
Gabor Greifba36cb52008-08-28 21:40:38 +00001816 DCI.AddToWorklist(N0.getNode());
Evan Chengfa1eb272007-02-08 22:13:59 +00001817 // FIXME: If running after legalize, we probably can't do this.
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001818 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, N0);
Evan Chengfa1eb272007-02-08 22:13:59 +00001819 }
1820 return N0;
1821 }
1822
1823 // Could not fold it.
Dan Gohman475871a2008-07-27 21:46:04 +00001824 return SDValue();
Evan Chengfa1eb272007-02-08 22:13:59 +00001825}
1826
Evan Chengad4196b2008-05-12 19:56:52 +00001827/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
1828/// node is a GlobalAddress + offset.
Chris Lattner0a9481f2011-02-13 22:25:43 +00001829bool TargetLowering::isGAPlusOffset(SDNode *N, const GlobalValue *&GA,
Evan Chengad4196b2008-05-12 19:56:52 +00001830 int64_t &Offset) const {
1831 if (isa<GlobalAddressSDNode>(N)) {
Dan Gohman9ea3f562008-06-09 22:05:52 +00001832 GlobalAddressSDNode *GASD = cast<GlobalAddressSDNode>(N);
1833 GA = GASD->getGlobal();
1834 Offset += GASD->getOffset();
Evan Chengad4196b2008-05-12 19:56:52 +00001835 return true;
1836 }
1837
1838 if (N->getOpcode() == ISD::ADD) {
Dan Gohman475871a2008-07-27 21:46:04 +00001839 SDValue N1 = N->getOperand(0);
1840 SDValue N2 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001841 if (isGAPlusOffset(N1.getNode(), GA, Offset)) {
Evan Chengad4196b2008-05-12 19:56:52 +00001842 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
1843 if (V) {
Dan Gohman7810bfe2008-09-26 21:54:37 +00001844 Offset += V->getSExtValue();
Evan Chengad4196b2008-05-12 19:56:52 +00001845 return true;
1846 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001847 } else if (isGAPlusOffset(N2.getNode(), GA, Offset)) {
Evan Chengad4196b2008-05-12 19:56:52 +00001848 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
1849 if (V) {
Dan Gohman7810bfe2008-09-26 21:54:37 +00001850 Offset += V->getSExtValue();
Evan Chengad4196b2008-05-12 19:56:52 +00001851 return true;
1852 }
1853 }
1854 }
Owen Anderson95771af2011-02-25 21:41:48 +00001855
Evan Chengad4196b2008-05-12 19:56:52 +00001856 return false;
1857}
1858
1859
Dan Gohman475871a2008-07-27 21:46:04 +00001860SDValue TargetLowering::
Chris Lattner00ffed02006-03-01 04:52:55 +00001861PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
1862 // Default implementation: no optimization.
Dan Gohman475871a2008-07-27 21:46:04 +00001863 return SDValue();
Chris Lattner00ffed02006-03-01 04:52:55 +00001864}
1865
Chris Lattnereb8146b2006-02-04 02:13:02 +00001866//===----------------------------------------------------------------------===//
1867// Inline Assembler Implementation Methods
1868//===----------------------------------------------------------------------===//
1869
Chris Lattner4376fea2008-04-27 00:09:47 +00001870
Chris Lattnereb8146b2006-02-04 02:13:02 +00001871TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00001872TargetLowering::getConstraintType(const std::string &Constraint) const {
Eric Christopherfffe3632013-01-11 18:12:39 +00001873 unsigned S = Constraint.size();
1874
1875 if (S == 1) {
Chris Lattner4234f572007-03-25 02:14:49 +00001876 switch (Constraint[0]) {
1877 default: break;
1878 case 'r': return C_RegisterClass;
1879 case 'm': // memory
1880 case 'o': // offsetable
1881 case 'V': // not offsetable
1882 return C_Memory;
1883 case 'i': // Simple Integer or Relocatable Constant
1884 case 'n': // Simple Integer
John Thompson67aff162010-09-21 22:04:54 +00001885 case 'E': // Floating Point Constant
1886 case 'F': // Floating Point Constant
Chris Lattner4234f572007-03-25 02:14:49 +00001887 case 's': // Relocatable Constant
John Thompson67aff162010-09-21 22:04:54 +00001888 case 'p': // Address.
Chris Lattnerc13dd1c2007-03-25 04:35:41 +00001889 case 'X': // Allow ANY value.
Chris Lattner4234f572007-03-25 02:14:49 +00001890 case 'I': // Target registers.
1891 case 'J':
1892 case 'K':
1893 case 'L':
1894 case 'M':
1895 case 'N':
1896 case 'O':
1897 case 'P':
John Thompson67aff162010-09-21 22:04:54 +00001898 case '<':
1899 case '>':
Chris Lattner4234f572007-03-25 02:14:49 +00001900 return C_Other;
1901 }
Chris Lattnereb8146b2006-02-04 02:13:02 +00001902 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001903
Eric Christopherfffe3632013-01-11 18:12:39 +00001904 if (S > 1 && Constraint[0] == '{' && Constraint[S-1] == '}') {
1905 if (S == 8 && !Constraint.compare(1, 6, "memory", 6)) // "{memory}"
1906 return C_Memory;
Chris Lattner065421f2007-03-25 02:18:14 +00001907 return C_Register;
Eric Christopherfffe3632013-01-11 18:12:39 +00001908 }
Chris Lattner4234f572007-03-25 02:14:49 +00001909 return C_Unknown;
Chris Lattnereb8146b2006-02-04 02:13:02 +00001910}
1911
Dale Johannesenba2a0b92008-01-29 02:21:21 +00001912/// LowerXConstraint - try to replace an X constraint, which matches anything,
1913/// with another that has more specific requirements based on the type of the
1914/// corresponding operand.
Owen Andersone50ed302009-08-10 22:56:29 +00001915const char *TargetLowering::LowerXConstraint(EVT ConstraintVT) const{
Duncan Sands83ec4b62008-06-06 12:08:01 +00001916 if (ConstraintVT.isInteger())
Chris Lattner5e764232008-04-26 23:02:14 +00001917 return "r";
Duncan Sands83ec4b62008-06-06 12:08:01 +00001918 if (ConstraintVT.isFloatingPoint())
Chris Lattner5e764232008-04-26 23:02:14 +00001919 return "f"; // works for many targets
1920 return 0;
Dale Johannesenba2a0b92008-01-29 02:21:21 +00001921}
1922
Chris Lattner48884cd2007-08-25 00:47:38 +00001923/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
1924/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +00001925void TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +00001926 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +00001927 std::vector<SDValue> &Ops,
Chris Lattner5e764232008-04-26 23:02:14 +00001928 SelectionDAG &DAG) const {
Eric Christopher362fee92011-06-17 20:41:29 +00001929
Eric Christopher100c8332011-06-02 23:16:42 +00001930 if (Constraint.length() > 1) return;
Eric Christopher362fee92011-06-17 20:41:29 +00001931
Eric Christopher100c8332011-06-02 23:16:42 +00001932 char ConstraintLetter = Constraint[0];
Chris Lattnereb8146b2006-02-04 02:13:02 +00001933 switch (ConstraintLetter) {
Chris Lattner9ff6ee82007-02-17 06:00:35 +00001934 default: break;
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001935 case 'X': // Allows any operand; labels (basic block) use this.
1936 if (Op.getOpcode() == ISD::BasicBlock) {
1937 Ops.push_back(Op);
1938 return;
1939 }
1940 // fall through
Chris Lattnereb8146b2006-02-04 02:13:02 +00001941 case 'i': // Simple Integer or Relocatable Constant
1942 case 'n': // Simple Integer
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001943 case 's': { // Relocatable Constant
Chris Lattner75c7d2b2007-05-03 16:54:34 +00001944 // These operands are interested in values of the form (GV+C), where C may
1945 // be folded in as an offset of GV, or it may be explicitly added. Also, it
1946 // is possible and fine if either GV or C are missing.
1947 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
1948 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001949
Chris Lattner75c7d2b2007-05-03 16:54:34 +00001950 // If we have "(add GV, C)", pull out GV/C
1951 if (Op.getOpcode() == ISD::ADD) {
1952 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
1953 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
1954 if (C == 0 || GA == 0) {
1955 C = dyn_cast<ConstantSDNode>(Op.getOperand(0));
1956 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(1));
1957 }
1958 if (C == 0 || GA == 0)
1959 C = 0, GA = 0;
1960 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001961
Chris Lattner75c7d2b2007-05-03 16:54:34 +00001962 // If we find a valid operand, map to the TargetXXX version so that the
1963 // value itself doesn't get selected.
1964 if (GA) { // Either &GV or &GV+C
1965 if (ConstraintLetter != 'n') {
1966 int64_t Offs = GA->getOffset();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001967 if (C) Offs += C->getZExtValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001968 Ops.push_back(DAG.getTargetGlobalAddress(GA->getGlobal(),
Devang Patel07538ad2010-07-15 18:45:27 +00001969 C ? C->getDebugLoc() : DebugLoc(),
Chris Lattner48884cd2007-08-25 00:47:38 +00001970 Op.getValueType(), Offs));
1971 return;
Chris Lattner75c7d2b2007-05-03 16:54:34 +00001972 }
1973 }
1974 if (C) { // just C, no GV.
Chris Lattner9ff6ee82007-02-17 06:00:35 +00001975 // Simple constants are not allowed for 's'.
Chris Lattner48884cd2007-08-25 00:47:38 +00001976 if (ConstraintLetter != 's') {
Dale Johannesen78e3e522009-02-12 20:58:09 +00001977 // gcc prints these as sign extended. Sign extend value to 64 bits
1978 // now; without this it would get ZExt'd later in
1979 // ScheduleDAGSDNodes::EmitNode, which is very generic.
1980 Ops.push_back(DAG.getTargetConstant(C->getAPIntValue().getSExtValue(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001981 MVT::i64));
Chris Lattner48884cd2007-08-25 00:47:38 +00001982 return;
1983 }
Chris Lattner9ff6ee82007-02-17 06:00:35 +00001984 }
Chris Lattner9ff6ee82007-02-17 06:00:35 +00001985 break;
Chris Lattnereb8146b2006-02-04 02:13:02 +00001986 }
Chris Lattner75c7d2b2007-05-03 16:54:34 +00001987 }
Chris Lattnereb8146b2006-02-04 02:13:02 +00001988}
1989
Chris Lattner1efa40f2006-02-22 00:56:39 +00001990std::pair<unsigned, const TargetRegisterClass*> TargetLowering::
Chris Lattner4217ca8dc2006-02-21 23:11:00 +00001991getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00001992 EVT VT) const {
Chris Lattner1efa40f2006-02-22 00:56:39 +00001993 if (Constraint[0] != '{')
Douglas Gregor7d9663c2010-05-11 06:17:44 +00001994 return std::make_pair(0u, static_cast<TargetRegisterClass*>(0));
Chris Lattnera55079a2006-02-01 01:29:47 +00001995 assert(*(Constraint.end()-1) == '}' && "Not a brace enclosed constraint?");
1996
1997 // Remove the braces from around the name.
Benjamin Kramer05872ea2009-11-12 20:36:59 +00001998 StringRef RegName(Constraint.data()+1, Constraint.size()-2);
Chris Lattner1efa40f2006-02-22 00:56:39 +00001999
Hal Finkelca2dd362012-12-18 17:50:58 +00002000 std::pair<unsigned, const TargetRegisterClass*> R =
2001 std::make_pair(0u, static_cast<const TargetRegisterClass*>(0));
2002
Chris Lattner1efa40f2006-02-22 00:56:39 +00002003 // Figure out which register class contains this reg.
Benjamin Kramer69e42db2013-01-11 20:05:37 +00002004 const TargetRegisterInfo *RI = getTargetMachine().getRegisterInfo();
Dan Gohman6f0d0242008-02-10 18:45:23 +00002005 for (TargetRegisterInfo::regclass_iterator RCI = RI->regclass_begin(),
Chris Lattner1efa40f2006-02-22 00:56:39 +00002006 E = RI->regclass_end(); RCI != E; ++RCI) {
2007 const TargetRegisterClass *RC = *RCI;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002008
2009 // If none of the value types for this register class are valid, we
Chris Lattnerb3befd42006-02-22 23:00:51 +00002010 // can't use it. For example, 64-bit reg classes on 32-bit targets.
Jakob Stoklund Olesen22e8a362011-10-12 01:24:51 +00002011 if (!isLegalRC(RC))
2012 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002013
2014 for (TargetRegisterClass::iterator I = RC->begin(), E = RC->end();
Chris Lattner1efa40f2006-02-22 00:56:39 +00002015 I != E; ++I) {
Hal Finkelca2dd362012-12-18 17:50:58 +00002016 if (RegName.equals_lower(RI->getName(*I))) {
2017 std::pair<unsigned, const TargetRegisterClass*> S =
2018 std::make_pair(*I, RC);
2019
2020 // If this register class has the requested value type, return it,
2021 // otherwise keep searching and return the first class found
2022 // if no other is found which explicitly has the requested type.
2023 if (RC->hasType(VT))
2024 return S;
2025 else if (!R.second)
2026 R = S;
2027 }
Chris Lattner1efa40f2006-02-22 00:56:39 +00002028 }
Chris Lattner4ccb0702006-01-26 20:37:03 +00002029 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002030
Hal Finkelca2dd362012-12-18 17:50:58 +00002031 return R;
Chris Lattner4ccb0702006-01-26 20:37:03 +00002032}
Evan Cheng30b37b52006-03-13 23:18:16 +00002033
2034//===----------------------------------------------------------------------===//
Chris Lattner4376fea2008-04-27 00:09:47 +00002035// Constraint Selection.
2036
Chris Lattner6bdcda32008-10-17 16:47:46 +00002037/// isMatchingInputConstraint - Return true of this is an input operand that is
2038/// a matching constraint like "4".
2039bool TargetLowering::AsmOperandInfo::isMatchingInputConstraint() const {
Chris Lattner58f15c42008-10-17 16:21:11 +00002040 assert(!ConstraintCode.empty() && "No known constraint!");
Guy Benyei87d0b9e2013-02-12 21:21:59 +00002041 return isdigit(static_cast<unsigned char>(ConstraintCode[0]));
Chris Lattner58f15c42008-10-17 16:21:11 +00002042}
2043
2044/// getMatchedOperand - If this is an input matching constraint, this method
2045/// returns the output operand it matches.
2046unsigned TargetLowering::AsmOperandInfo::getMatchedOperand() const {
2047 assert(!ConstraintCode.empty() && "No known constraint!");
2048 return atoi(ConstraintCode.c_str());
2049}
2050
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002051
John Thompsoneac6e1d2010-09-13 18:15:37 +00002052/// ParseConstraints - Split up the constraint string from the inline
2053/// assembly value into the specific constraints and their prefixes,
2054/// and also tie in the associated operand values.
2055/// If this returns an empty vector, and if the constraint string itself
2056/// isn't empty, there was an error parsing.
John Thompson44ab89e2010-10-29 17:29:13 +00002057TargetLowering::AsmOperandInfoVector TargetLowering::ParseConstraints(
John Thompsoneac6e1d2010-09-13 18:15:37 +00002058 ImmutableCallSite CS) const {
2059 /// ConstraintOperands - Information about all of the constraints.
John Thompson44ab89e2010-10-29 17:29:13 +00002060 AsmOperandInfoVector ConstraintOperands;
John Thompsoneac6e1d2010-09-13 18:15:37 +00002061 const InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
John Thompson67aff162010-09-21 22:04:54 +00002062 unsigned maCount = 0; // Largest number of multiple alternative constraints.
John Thompsoneac6e1d2010-09-13 18:15:37 +00002063
2064 // Do a prepass over the constraints, canonicalizing them, and building up the
2065 // ConstraintOperands list.
John Thompson44ab89e2010-10-29 17:29:13 +00002066 InlineAsm::ConstraintInfoVector
John Thompsoneac6e1d2010-09-13 18:15:37 +00002067 ConstraintInfos = IA->ParseConstraints();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002068
John Thompsoneac6e1d2010-09-13 18:15:37 +00002069 unsigned ArgNo = 0; // ArgNo - The argument of the CallInst.
2070 unsigned ResNo = 0; // ResNo - The result number of the next output.
2071
2072 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
2073 ConstraintOperands.push_back(AsmOperandInfo(ConstraintInfos[i]));
2074 AsmOperandInfo &OpInfo = ConstraintOperands.back();
2075
John Thompson67aff162010-09-21 22:04:54 +00002076 // Update multiple alternative constraint count.
2077 if (OpInfo.multipleAlternatives.size() > maCount)
2078 maCount = OpInfo.multipleAlternatives.size();
2079
John Thompson44ab89e2010-10-29 17:29:13 +00002080 OpInfo.ConstraintVT = MVT::Other;
John Thompsoneac6e1d2010-09-13 18:15:37 +00002081
2082 // Compute the value type for each operand.
2083 switch (OpInfo.Type) {
2084 case InlineAsm::isOutput:
2085 // Indirect outputs just consume an argument.
2086 if (OpInfo.isIndirect) {
2087 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
2088 break;
2089 }
2090
2091 // The return value of the call is this value. As such, there is no
2092 // corresponding argument.
2093 assert(!CS.getType()->isVoidTy() &&
2094 "Bad inline asm!");
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002095 if (StructType *STy = dyn_cast<StructType>(CS.getType())) {
Patrik Hagglundc698d3a2012-12-19 15:19:11 +00002096 OpInfo.ConstraintVT = getSimpleValueType(STy->getElementType(ResNo));
John Thompsoneac6e1d2010-09-13 18:15:37 +00002097 } else {
2098 assert(ResNo == 0 && "Asm only has one result!");
Patrik Hagglundc698d3a2012-12-19 15:19:11 +00002099 OpInfo.ConstraintVT = getSimpleValueType(CS.getType());
John Thompsoneac6e1d2010-09-13 18:15:37 +00002100 }
2101 ++ResNo;
2102 break;
2103 case InlineAsm::isInput:
2104 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
2105 break;
2106 case InlineAsm::isClobber:
2107 // Nothing to do.
2108 break;
2109 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002110
John Thompson44ab89e2010-10-29 17:29:13 +00002111 if (OpInfo.CallOperandVal) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002112 llvm::Type *OpTy = OpInfo.CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00002113 if (OpInfo.isIndirect) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002114 llvm::PointerType *PtrTy = dyn_cast<PointerType>(OpTy);
John Thompson44ab89e2010-10-29 17:29:13 +00002115 if (!PtrTy)
2116 report_fatal_error("Indirect operand for inline asm not a pointer!");
2117 OpTy = PtrTy->getElementType();
2118 }
Eric Christopher362fee92011-06-17 20:41:29 +00002119
Eric Christophercef81b72011-05-09 20:04:43 +00002120 // Look for vector wrapped in a struct. e.g. { <16 x i8> }.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002121 if (StructType *STy = dyn_cast<StructType>(OpTy))
Eric Christophercef81b72011-05-09 20:04:43 +00002122 if (STy->getNumElements() == 1)
2123 OpTy = STy->getElementType(0);
2124
John Thompson44ab89e2010-10-29 17:29:13 +00002125 // If OpTy is not a single value, it may be a struct/union that we
2126 // can tile with integers.
2127 if (!OpTy->isSingleValueType() && OpTy->isSized()) {
Benjamin Kramer69e42db2013-01-11 20:05:37 +00002128 unsigned BitSize = getDataLayout()->getTypeSizeInBits(OpTy);
John Thompson44ab89e2010-10-29 17:29:13 +00002129 switch (BitSize) {
2130 default: break;
2131 case 1:
2132 case 8:
2133 case 16:
2134 case 32:
2135 case 64:
2136 case 128:
Dale Johannesen71365d32010-11-09 01:15:07 +00002137 OpInfo.ConstraintVT =
Patrik Hagglundc698d3a2012-12-19 15:19:11 +00002138 MVT::getVT(IntegerType::get(OpTy->getContext(), BitSize), true);
John Thompson44ab89e2010-10-29 17:29:13 +00002139 break;
2140 }
Micah Villmow7d661462012-10-09 16:06:12 +00002141 } else if (PointerType *PT = dyn_cast<PointerType>(OpTy)) {
2142 OpInfo.ConstraintVT = MVT::getIntegerVT(
Benjamin Kramer69e42db2013-01-11 20:05:37 +00002143 8*getDataLayout()->getPointerSize(PT->getAddressSpace()));
John Thompson44ab89e2010-10-29 17:29:13 +00002144 } else {
Patrik Hagglundc698d3a2012-12-19 15:19:11 +00002145 OpInfo.ConstraintVT = MVT::getVT(OpTy, true);
John Thompson44ab89e2010-10-29 17:29:13 +00002146 }
2147 }
John Thompsoneac6e1d2010-09-13 18:15:37 +00002148 }
2149
2150 // If we have multiple alternative constraints, select the best alternative.
2151 if (ConstraintInfos.size()) {
John Thompsoneac6e1d2010-09-13 18:15:37 +00002152 if (maCount) {
2153 unsigned bestMAIndex = 0;
2154 int bestWeight = -1;
2155 // weight: -1 = invalid match, and 0 = so-so match to 5 = good match.
2156 int weight = -1;
2157 unsigned maIndex;
2158 // Compute the sums of the weights for each alternative, keeping track
2159 // of the best (highest weight) one so far.
2160 for (maIndex = 0; maIndex < maCount; ++maIndex) {
2161 int weightSum = 0;
2162 for (unsigned cIndex = 0, eIndex = ConstraintOperands.size();
2163 cIndex != eIndex; ++cIndex) {
2164 AsmOperandInfo& OpInfo = ConstraintOperands[cIndex];
2165 if (OpInfo.Type == InlineAsm::isClobber)
2166 continue;
John Thompsoneac6e1d2010-09-13 18:15:37 +00002167
John Thompson44ab89e2010-10-29 17:29:13 +00002168 // If this is an output operand with a matching input operand,
2169 // look up the matching input. If their types mismatch, e.g. one
2170 // is an integer, the other is floating point, or their sizes are
2171 // different, flag it as an maCantMatch.
John Thompsoneac6e1d2010-09-13 18:15:37 +00002172 if (OpInfo.hasMatchingInput()) {
2173 AsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
John Thompsoneac6e1d2010-09-13 18:15:37 +00002174 if (OpInfo.ConstraintVT != Input.ConstraintVT) {
2175 if ((OpInfo.ConstraintVT.isInteger() !=
2176 Input.ConstraintVT.isInteger()) ||
2177 (OpInfo.ConstraintVT.getSizeInBits() !=
2178 Input.ConstraintVT.getSizeInBits())) {
2179 weightSum = -1; // Can't match.
2180 break;
2181 }
John Thompsoneac6e1d2010-09-13 18:15:37 +00002182 }
2183 }
John Thompsoneac6e1d2010-09-13 18:15:37 +00002184 weight = getMultipleConstraintMatchWeight(OpInfo, maIndex);
2185 if (weight == -1) {
2186 weightSum = -1;
2187 break;
2188 }
2189 weightSum += weight;
2190 }
2191 // Update best.
2192 if (weightSum > bestWeight) {
2193 bestWeight = weightSum;
2194 bestMAIndex = maIndex;
2195 }
2196 }
2197
2198 // Now select chosen alternative in each constraint.
2199 for (unsigned cIndex = 0, eIndex = ConstraintOperands.size();
2200 cIndex != eIndex; ++cIndex) {
2201 AsmOperandInfo& cInfo = ConstraintOperands[cIndex];
2202 if (cInfo.Type == InlineAsm::isClobber)
2203 continue;
2204 cInfo.selectAlternative(bestMAIndex);
2205 }
2206 }
2207 }
2208
2209 // Check and hook up tied operands, choose constraint code to use.
2210 for (unsigned cIndex = 0, eIndex = ConstraintOperands.size();
2211 cIndex != eIndex; ++cIndex) {
2212 AsmOperandInfo& OpInfo = ConstraintOperands[cIndex];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002213
John Thompsoneac6e1d2010-09-13 18:15:37 +00002214 // If this is an output operand with a matching input operand, look up the
2215 // matching input. If their types mismatch, e.g. one is an integer, the
2216 // other is floating point, or their sizes are different, flag it as an
2217 // error.
2218 if (OpInfo.hasMatchingInput()) {
2219 AsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
John Thompson44ab89e2010-10-29 17:29:13 +00002220
John Thompsoneac6e1d2010-09-13 18:15:37 +00002221 if (OpInfo.ConstraintVT != Input.ConstraintVT) {
Bill Wendling96cb1122012-07-19 00:04:14 +00002222 std::pair<unsigned, const TargetRegisterClass*> MatchRC =
2223 getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
2224 OpInfo.ConstraintVT);
2225 std::pair<unsigned, const TargetRegisterClass*> InputRC =
2226 getRegForInlineAsmConstraint(Input.ConstraintCode,
2227 Input.ConstraintVT);
John Thompsoneac6e1d2010-09-13 18:15:37 +00002228 if ((OpInfo.ConstraintVT.isInteger() !=
2229 Input.ConstraintVT.isInteger()) ||
Eric Christopher5427ede2011-07-14 20:13:52 +00002230 (MatchRC.second != InputRC.second)) {
John Thompsoneac6e1d2010-09-13 18:15:37 +00002231 report_fatal_error("Unsupported asm: input constraint"
2232 " with a matching output constraint of"
2233 " incompatible type!");
2234 }
John Thompsoneac6e1d2010-09-13 18:15:37 +00002235 }
John Thompson44ab89e2010-10-29 17:29:13 +00002236
John Thompsoneac6e1d2010-09-13 18:15:37 +00002237 }
2238 }
2239
2240 return ConstraintOperands;
2241}
2242
Chris Lattner58f15c42008-10-17 16:21:11 +00002243
Chris Lattner4376fea2008-04-27 00:09:47 +00002244/// getConstraintGenerality - Return an integer indicating how general CT
2245/// is.
2246static unsigned getConstraintGenerality(TargetLowering::ConstraintType CT) {
2247 switch (CT) {
Chris Lattner4376fea2008-04-27 00:09:47 +00002248 case TargetLowering::C_Other:
2249 case TargetLowering::C_Unknown:
2250 return 0;
2251 case TargetLowering::C_Register:
2252 return 1;
2253 case TargetLowering::C_RegisterClass:
2254 return 2;
2255 case TargetLowering::C_Memory:
2256 return 3;
2257 }
Chandler Carruth732f05c2012-01-10 18:08:01 +00002258 llvm_unreachable("Invalid constraint type");
Chris Lattner4376fea2008-04-27 00:09:47 +00002259}
2260
John Thompson44ab89e2010-10-29 17:29:13 +00002261/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +00002262/// This object must already have been set up with the operand type
2263/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +00002264TargetLowering::ConstraintWeight
2265 TargetLowering::getMultipleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +00002266 AsmOperandInfo &info, int maIndex) const {
John Thompson44ab89e2010-10-29 17:29:13 +00002267 InlineAsm::ConstraintCodeVector *rCodes;
John Thompson67aff162010-09-21 22:04:54 +00002268 if (maIndex >= (int)info.multipleAlternatives.size())
2269 rCodes = &info.Codes;
2270 else
2271 rCodes = &info.multipleAlternatives[maIndex].Codes;
John Thompson44ab89e2010-10-29 17:29:13 +00002272 ConstraintWeight BestWeight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +00002273
2274 // Loop over the options, keeping track of the most general one.
John Thompson67aff162010-09-21 22:04:54 +00002275 for (unsigned i = 0, e = rCodes->size(); i != e; ++i) {
John Thompson44ab89e2010-10-29 17:29:13 +00002276 ConstraintWeight weight =
2277 getSingleConstraintMatchWeight(info, (*rCodes)[i].c_str());
John Thompsoneac6e1d2010-09-13 18:15:37 +00002278 if (weight > BestWeight)
2279 BestWeight = weight;
2280 }
2281
2282 return BestWeight;
2283}
2284
John Thompson44ab89e2010-10-29 17:29:13 +00002285/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +00002286/// This object must already have been set up with the operand type
2287/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +00002288TargetLowering::ConstraintWeight
2289 TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +00002290 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +00002291 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +00002292 Value *CallOperandVal = info.CallOperandVal;
2293 // If we don't have a value, we can't do a match,
2294 // but allow it at the lowest weight.
2295 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +00002296 return CW_Default;
John Thompsoneac6e1d2010-09-13 18:15:37 +00002297 // Look at the constraint type.
2298 switch (*constraint) {
2299 case 'i': // immediate integer.
2300 case 'n': // immediate integer with a known value.
John Thompson44ab89e2010-10-29 17:29:13 +00002301 if (isa<ConstantInt>(CallOperandVal))
2302 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +00002303 break;
2304 case 's': // non-explicit intregal immediate.
John Thompson44ab89e2010-10-29 17:29:13 +00002305 if (isa<GlobalValue>(CallOperandVal))
2306 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +00002307 break;
John Thompson44ab89e2010-10-29 17:29:13 +00002308 case 'E': // immediate float if host format.
2309 case 'F': // immediate float.
2310 if (isa<ConstantFP>(CallOperandVal))
2311 weight = CW_Constant;
2312 break;
2313 case '<': // memory operand with autodecrement.
2314 case '>': // memory operand with autoincrement.
John Thompsoneac6e1d2010-09-13 18:15:37 +00002315 case 'm': // memory operand.
2316 case 'o': // offsettable memory operand
2317 case 'V': // non-offsettable memory operand
John Thompson44ab89e2010-10-29 17:29:13 +00002318 weight = CW_Memory;
John Thompsoneac6e1d2010-09-13 18:15:37 +00002319 break;
John Thompson44ab89e2010-10-29 17:29:13 +00002320 case 'r': // general register.
John Thompsoneac6e1d2010-09-13 18:15:37 +00002321 case 'g': // general register, memory operand or immediate integer.
John Thompson44ab89e2010-10-29 17:29:13 +00002322 // note: Clang converts "g" to "imr".
2323 if (CallOperandVal->getType()->isIntegerTy())
2324 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +00002325 break;
John Thompson44ab89e2010-10-29 17:29:13 +00002326 case 'X': // any operand.
John Thompsoneac6e1d2010-09-13 18:15:37 +00002327 default:
John Thompson44ab89e2010-10-29 17:29:13 +00002328 weight = CW_Default;
John Thompsoneac6e1d2010-09-13 18:15:37 +00002329 break;
2330 }
2331 return weight;
2332}
2333
Chris Lattner4376fea2008-04-27 00:09:47 +00002334/// ChooseConstraint - If there are multiple different constraints that we
2335/// could pick for this operand (e.g. "imr") try to pick the 'best' one.
Chris Lattner24e1a9d2008-04-27 01:49:46 +00002336/// This is somewhat tricky: constraints fall into four classes:
Chris Lattner4376fea2008-04-27 00:09:47 +00002337/// Other -> immediates and magic values
2338/// Register -> one specific register
2339/// RegisterClass -> a group of regs
2340/// Memory -> memory
2341/// Ideally, we would pick the most specific constraint possible: if we have
2342/// something that fits into a register, we would pick it. The problem here
2343/// is that if we have something that could either be in a register or in
2344/// memory that use of the register could cause selection of *other*
2345/// operands to fail: they might only succeed if we pick memory. Because of
2346/// this the heuristic we use is:
2347///
2348/// 1) If there is an 'other' constraint, and if the operand is valid for
2349/// that constraint, use it. This makes us take advantage of 'i'
2350/// constraints when available.
2351/// 2) Otherwise, pick the most general constraint present. This prefers
2352/// 'm' over 'r', for example.
2353///
2354static void ChooseConstraint(TargetLowering::AsmOperandInfo &OpInfo,
Dale Johannesen1784d162010-06-25 21:55:36 +00002355 const TargetLowering &TLI,
Dan Gohman475871a2008-07-27 21:46:04 +00002356 SDValue Op, SelectionDAG *DAG) {
Chris Lattner4376fea2008-04-27 00:09:47 +00002357 assert(OpInfo.Codes.size() > 1 && "Doesn't have multiple constraint options");
2358 unsigned BestIdx = 0;
2359 TargetLowering::ConstraintType BestType = TargetLowering::C_Unknown;
2360 int BestGenerality = -1;
Dale Johannesena5989f82010-06-28 22:09:45 +00002361
Chris Lattner4376fea2008-04-27 00:09:47 +00002362 // Loop over the options, keeping track of the most general one.
2363 for (unsigned i = 0, e = OpInfo.Codes.size(); i != e; ++i) {
2364 TargetLowering::ConstraintType CType =
2365 TLI.getConstraintType(OpInfo.Codes[i]);
Dale Johannesena5989f82010-06-28 22:09:45 +00002366
Chris Lattner5a096902008-04-27 00:37:18 +00002367 // If this is an 'other' constraint, see if the operand is valid for it.
2368 // For example, on X86 we might have an 'rI' constraint. If the operand
2369 // is an integer in the range [0..31] we want to use I (saving a load
2370 // of a register), otherwise we must use 'r'.
Gabor Greifba36cb52008-08-28 21:40:38 +00002371 if (CType == TargetLowering::C_Other && Op.getNode()) {
Chris Lattner5a096902008-04-27 00:37:18 +00002372 assert(OpInfo.Codes[i].size() == 1 &&
2373 "Unhandled multi-letter 'other' constraint");
Dan Gohman475871a2008-07-27 21:46:04 +00002374 std::vector<SDValue> ResultOps;
Eric Christopher100c8332011-06-02 23:16:42 +00002375 TLI.LowerAsmOperandForConstraint(Op, OpInfo.Codes[i],
Chris Lattner5a096902008-04-27 00:37:18 +00002376 ResultOps, *DAG);
2377 if (!ResultOps.empty()) {
2378 BestType = CType;
2379 BestIdx = i;
2380 break;
2381 }
2382 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002383
Dale Johannesena5989f82010-06-28 22:09:45 +00002384 // Things with matching constraints can only be registers, per gcc
2385 // documentation. This mainly affects "g" constraints.
2386 if (CType == TargetLowering::C_Memory && OpInfo.hasMatchingInput())
2387 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002388
Chris Lattner4376fea2008-04-27 00:09:47 +00002389 // This constraint letter is more general than the previous one, use it.
2390 int Generality = getConstraintGenerality(CType);
2391 if (Generality > BestGenerality) {
2392 BestType = CType;
2393 BestIdx = i;
2394 BestGenerality = Generality;
2395 }
2396 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002397
Chris Lattner4376fea2008-04-27 00:09:47 +00002398 OpInfo.ConstraintCode = OpInfo.Codes[BestIdx];
2399 OpInfo.ConstraintType = BestType;
2400}
2401
2402/// ComputeConstraintToUse - Determines the constraint code and constraint
2403/// type to use for the specific AsmOperandInfo, setting
2404/// OpInfo.ConstraintCode and OpInfo.ConstraintType.
Chris Lattner5a096902008-04-27 00:37:18 +00002405void TargetLowering::ComputeConstraintToUse(AsmOperandInfo &OpInfo,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002406 SDValue Op,
Chris Lattner5a096902008-04-27 00:37:18 +00002407 SelectionDAG *DAG) const {
Chris Lattner4376fea2008-04-27 00:09:47 +00002408 assert(!OpInfo.Codes.empty() && "Must have at least one constraint");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002409
Chris Lattner4376fea2008-04-27 00:09:47 +00002410 // Single-letter constraints ('r') are very common.
2411 if (OpInfo.Codes.size() == 1) {
2412 OpInfo.ConstraintCode = OpInfo.Codes[0];
2413 OpInfo.ConstraintType = getConstraintType(OpInfo.ConstraintCode);
2414 } else {
Dale Johannesen1784d162010-06-25 21:55:36 +00002415 ChooseConstraint(OpInfo, *this, Op, DAG);
Chris Lattner4376fea2008-04-27 00:09:47 +00002416 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002417
Chris Lattner4376fea2008-04-27 00:09:47 +00002418 // 'X' matches anything.
2419 if (OpInfo.ConstraintCode == "X" && OpInfo.CallOperandVal) {
2420 // Labels and constants are handled elsewhere ('X' is the only thing
Dale Johannesen8ea5ec62009-07-07 23:26:33 +00002421 // that matches labels). For Functions, the type here is the type of
Dale Johannesen5339c552009-07-20 23:27:39 +00002422 // the result, which is not what we want to look at; leave them alone.
2423 Value *v = OpInfo.CallOperandVal;
Dale Johannesen8ea5ec62009-07-07 23:26:33 +00002424 if (isa<BasicBlock>(v) || isa<ConstantInt>(v) || isa<Function>(v)) {
2425 OpInfo.CallOperandVal = v;
Chris Lattner4376fea2008-04-27 00:09:47 +00002426 return;
Dale Johannesen8ea5ec62009-07-07 23:26:33 +00002427 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002428
Chris Lattner4376fea2008-04-27 00:09:47 +00002429 // Otherwise, try to resolve it to something we know about by looking at
2430 // the actual operand type.
2431 if (const char *Repl = LowerXConstraint(OpInfo.ConstraintVT)) {
2432 OpInfo.ConstraintCode = Repl;
2433 OpInfo.ConstraintType = getConstraintType(OpInfo.ConstraintCode);
2434 }
2435 }
2436}
2437
Benjamin Kramer9c640302011-07-08 10:31:30 +00002438/// BuildExactDiv - Given an exact SDIV by a constant, create a multiplication
2439/// with the multiplicative inverse of the constant.
2440SDValue TargetLowering::BuildExactSDIV(SDValue Op1, SDValue Op2, DebugLoc dl,
2441 SelectionDAG &DAG) const {
2442 ConstantSDNode *C = cast<ConstantSDNode>(Op2);
2443 APInt d = C->getAPIntValue();
2444 assert(d != 0 && "Division by zero!");
2445
2446 // Shift the value upfront if it is even, so the LSB is one.
2447 unsigned ShAmt = d.countTrailingZeros();
2448 if (ShAmt) {
2449 // TODO: For UDIV use SRL instead of SRA.
2450 SDValue Amt = DAG.getConstant(ShAmt, getShiftAmountTy(Op1.getValueType()));
2451 Op1 = DAG.getNode(ISD::SRA, dl, Op1.getValueType(), Op1, Amt);
2452 d = d.ashr(ShAmt);
2453 }
2454
2455 // Calculate the multiplicative inverse, using Newton's method.
2456 APInt t, xn = d;
2457 while ((t = d*xn) != 1)
2458 xn *= APInt(d.getBitWidth(), 2) - t;
2459
2460 Op2 = DAG.getConstant(xn, Op1.getValueType());
2461 return DAG.getNode(ISD::MUL, dl, Op1.getValueType(), Op1, Op2);
2462}
2463
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002464/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
2465/// return a DAG expression to select that will generate the same value by
2466/// multiplying by a magic number. See:
2467/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Richard Osborne19a4daf2011-11-07 17:09:05 +00002468SDValue TargetLowering::
2469BuildSDIV(SDNode *N, SelectionDAG &DAG, bool IsAfterLegalization,
Eric Christopher9171fb92012-12-10 22:00:20 +00002470 std::vector<SDNode*> *Created) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002471 EVT VT = N->getValueType(0);
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002472 DebugLoc dl= N->getDebugLoc();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002473
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002474 // Check to see if we can do this.
Eli Friedmanfc69cb42008-11-30 06:35:39 +00002475 // FIXME: We should be more aggressive here.
2476 if (!isTypeLegal(VT))
2477 return SDValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002478
Eli Friedmanfc69cb42008-11-30 06:35:39 +00002479 APInt d = cast<ConstantSDNode>(N->getOperand(1))->getAPIntValue();
Jay Foad4e5ea552009-04-30 10:15:35 +00002480 APInt::ms magics = d.magic();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002481
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002482 // Multiply the numerator (operand 0) by the magic value
Eli Friedmanfc69cb42008-11-30 06:35:39 +00002483 // FIXME: We should support doing a MUL in a wider type
Dan Gohman475871a2008-07-27 21:46:04 +00002484 SDValue Q;
Richard Osborne19a4daf2011-11-07 17:09:05 +00002485 if (IsAfterLegalization ? isOperationLegal(ISD::MULHS, VT) :
2486 isOperationLegalOrCustom(ISD::MULHS, VT))
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002487 Q = DAG.getNode(ISD::MULHS, dl, VT, N->getOperand(0),
Dan Gohman525178c2007-10-08 18:33:35 +00002488 DAG.getConstant(magics.m, VT));
Richard Osborne19a4daf2011-11-07 17:09:05 +00002489 else if (IsAfterLegalization ? isOperationLegal(ISD::SMUL_LOHI, VT) :
2490 isOperationLegalOrCustom(ISD::SMUL_LOHI, VT))
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002491 Q = SDValue(DAG.getNode(ISD::SMUL_LOHI, dl, DAG.getVTList(VT, VT),
Dan Gohman525178c2007-10-08 18:33:35 +00002492 N->getOperand(0),
Gabor Greifba36cb52008-08-28 21:40:38 +00002493 DAG.getConstant(magics.m, VT)).getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00002494 else
Dan Gohman475871a2008-07-27 21:46:04 +00002495 return SDValue(); // No mulhs or equvialent
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002496 // If d > 0 and m < 0, add the numerator
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002497 if (d.isStrictlyPositive() && magics.m.isNegative()) {
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002498 Q = DAG.getNode(ISD::ADD, dl, VT, Q, N->getOperand(0));
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002499 if (Created)
Gabor Greifba36cb52008-08-28 21:40:38 +00002500 Created->push_back(Q.getNode());
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002501 }
2502 // If d < 0 and m > 0, subtract the numerator.
Eli Friedmanfc69cb42008-11-30 06:35:39 +00002503 if (d.isNegative() && magics.m.isStrictlyPositive()) {
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002504 Q = DAG.getNode(ISD::SUB, dl, VT, Q, N->getOperand(0));
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002505 if (Created)
Gabor Greifba36cb52008-08-28 21:40:38 +00002506 Created->push_back(Q.getNode());
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002507 }
2508 // Shift right algebraic if shift value is nonzero
2509 if (magics.s > 0) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002510 Q = DAG.getNode(ISD::SRA, dl, VT, Q,
Owen Anderson95771af2011-02-25 21:41:48 +00002511 DAG.getConstant(magics.s, getShiftAmountTy(Q.getValueType())));
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002512 if (Created)
Gabor Greifba36cb52008-08-28 21:40:38 +00002513 Created->push_back(Q.getNode());
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002514 }
2515 // Extract the sign bit and add it to the quotient
Dan Gohman475871a2008-07-27 21:46:04 +00002516 SDValue T =
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002517 DAG.getNode(ISD::SRL, dl, VT, Q, DAG.getConstant(VT.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +00002518 getShiftAmountTy(Q.getValueType())));
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002519 if (Created)
Gabor Greifba36cb52008-08-28 21:40:38 +00002520 Created->push_back(T.getNode());
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002521 return DAG.getNode(ISD::ADD, dl, VT, Q, T);
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002522}
2523
2524/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
2525/// return a DAG expression to select that will generate the same value by
2526/// multiplying by a magic number. See:
2527/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Richard Osborne19a4daf2011-11-07 17:09:05 +00002528SDValue TargetLowering::
2529BuildUDIV(SDNode *N, SelectionDAG &DAG, bool IsAfterLegalization,
Eric Christopher9171fb92012-12-10 22:00:20 +00002530 std::vector<SDNode*> *Created) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002531 EVT VT = N->getValueType(0);
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002532 DebugLoc dl = N->getDebugLoc();
Eli Friedman201c9772008-11-30 06:02:26 +00002533
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002534 // Check to see if we can do this.
Eli Friedman201c9772008-11-30 06:02:26 +00002535 // FIXME: We should be more aggressive here.
2536 if (!isTypeLegal(VT))
2537 return SDValue();
2538
2539 // FIXME: We should use a narrower constant when the upper
2540 // bits are known to be zero.
Benjamin Kramer1c10b8d2011-03-17 20:39:14 +00002541 const APInt &N1C = cast<ConstantSDNode>(N->getOperand(1))->getAPIntValue();
2542 APInt::mu magics = N1C.magicu();
2543
2544 SDValue Q = N->getOperand(0);
2545
2546 // If the divisor is even, we can avoid using the expensive fixup by shifting
2547 // the divided value upfront.
2548 if (magics.a != 0 && !N1C[0]) {
2549 unsigned Shift = N1C.countTrailingZeros();
2550 Q = DAG.getNode(ISD::SRL, dl, VT, Q,
2551 DAG.getConstant(Shift, getShiftAmountTy(Q.getValueType())));
2552 if (Created)
2553 Created->push_back(Q.getNode());
2554
2555 // Get magic number for the shifted divisor.
2556 magics = N1C.lshr(Shift).magicu(Shift);
2557 assert(magics.a == 0 && "Should use cheap fixup now");
2558 }
Eli Friedman201c9772008-11-30 06:02:26 +00002559
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002560 // Multiply the numerator (operand 0) by the magic value
Eli Friedman201c9772008-11-30 06:02:26 +00002561 // FIXME: We should support doing a MUL in a wider type
Richard Osborne19a4daf2011-11-07 17:09:05 +00002562 if (IsAfterLegalization ? isOperationLegal(ISD::MULHU, VT) :
2563 isOperationLegalOrCustom(ISD::MULHU, VT))
Benjamin Kramer1c10b8d2011-03-17 20:39:14 +00002564 Q = DAG.getNode(ISD::MULHU, dl, VT, Q, DAG.getConstant(magics.m, VT));
Richard Osborne19a4daf2011-11-07 17:09:05 +00002565 else if (IsAfterLegalization ? isOperationLegal(ISD::UMUL_LOHI, VT) :
2566 isOperationLegalOrCustom(ISD::UMUL_LOHI, VT))
Benjamin Kramer1c10b8d2011-03-17 20:39:14 +00002567 Q = SDValue(DAG.getNode(ISD::UMUL_LOHI, dl, DAG.getVTList(VT, VT), Q,
2568 DAG.getConstant(magics.m, VT)).getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00002569 else
Dan Gohman475871a2008-07-27 21:46:04 +00002570 return SDValue(); // No mulhu or equvialent
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002571 if (Created)
Gabor Greifba36cb52008-08-28 21:40:38 +00002572 Created->push_back(Q.getNode());
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002573
2574 if (magics.a == 0) {
Benjamin Kramer1c10b8d2011-03-17 20:39:14 +00002575 assert(magics.s < N1C.getBitWidth() &&
Eli Friedman201c9772008-11-30 06:02:26 +00002576 "We shouldn't generate an undefined shift!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002577 return DAG.getNode(ISD::SRL, dl, VT, Q,
Owen Anderson95771af2011-02-25 21:41:48 +00002578 DAG.getConstant(magics.s, getShiftAmountTy(Q.getValueType())));
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002579 } else {
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002580 SDValue NPQ = DAG.getNode(ISD::SUB, dl, VT, N->getOperand(0), Q);
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002581 if (Created)
Gabor Greifba36cb52008-08-28 21:40:38 +00002582 Created->push_back(NPQ.getNode());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002583 NPQ = DAG.getNode(ISD::SRL, dl, VT, NPQ,
Owen Anderson95771af2011-02-25 21:41:48 +00002584 DAG.getConstant(1, getShiftAmountTy(NPQ.getValueType())));
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002585 if (Created)
Gabor Greifba36cb52008-08-28 21:40:38 +00002586 Created->push_back(NPQ.getNode());
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002587 NPQ = DAG.getNode(ISD::ADD, dl, VT, NPQ, Q);
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002588 if (Created)
Gabor Greifba36cb52008-08-28 21:40:38 +00002589 Created->push_back(NPQ.getNode());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002590 return DAG.getNode(ISD::SRL, dl, VT, NPQ,
Owen Anderson95771af2011-02-25 21:41:48 +00002591 DAG.getConstant(magics.s-1, getShiftAmountTy(NPQ.getValueType())));
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002592 }
2593}