blob: b75d80541ea22ddd2cb2ed9c70158705f342b7dc [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"
Stephen Hines36b56882014-04-23 16:57:46 -070025#include "llvm/IR/LLVMContext.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000026#include "llvm/MC/MCAsmInfo.h"
27#include "llvm/MC/MCExpr.h"
Nadav Rotemb6fbec32011-06-01 12:51:46 +000028#include "llvm/Support/CommandLine.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000029#include "llvm/Support/ErrorHandling.h"
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +000030#include "llvm/Support/MathExtras.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000031#include "llvm/Target/TargetLoweringObjectFile.h"
32#include "llvm/Target/TargetMachine.h"
33#include "llvm/Target/TargetRegisterInfo.h"
Nick Lewycky476b2422010-12-19 20:43:38 +000034#include <cctype>
Chris Lattner310968c2005-01-07 07:44:53 +000035using namespace llvm;
36
Chris Lattnerf0144122009-07-28 03:13:23 +000037/// NOTE: The constructor takes ownership of TLOF.
Dan Gohmanf0757b02010-04-21 01:34:56 +000038TargetLowering::TargetLowering(const TargetMachine &tm,
39 const TargetLoweringObjectFile *tlof)
Benjamin Kramer69e42db2013-01-11 20:05:37 +000040 : TargetLoweringBase(tm, tlof) {}
Chris Lattnercba82f92005-01-16 07:28:11 +000041
Evan Cheng72261582005-12-20 06:22:03 +000042const char *TargetLowering::getTargetNodeName(unsigned Opcode) const {
Stephen Hinesdce4a402014-05-29 02:49:00 -070043 return nullptr;
Evan Cheng72261582005-12-20 06:22:03 +000044}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000045
Tim Northover2c8cf4b2013-01-09 13:18:15 +000046/// Check whether a given call node is in tail position within its function. If
47/// so, it sets Chain to the input chain of the tail call.
48bool TargetLowering::isInTailCallPosition(SelectionDAG &DAG, SDNode *Node,
49 SDValue &Chain) const {
50 const Function *F = DAG.getMachineFunction().getFunction();
51
52 // Conservatively require the attributes of the call to match those of
53 // the return. Ignore noalias because it doesn't affect the call sequence.
Bill Wendling1b0c54f2013-01-18 21:53:16 +000054 AttributeSet CallerAttrs = F->getAttributes();
55 if (AttrBuilder(CallerAttrs, AttributeSet::ReturnIndex)
Tim Northover2c8cf4b2013-01-09 13:18:15 +000056 .removeAttribute(Attribute::NoAlias).hasAttributes())
57 return false;
58
59 // It's not safe to eliminate the sign / zero extension of the return value.
Bill Wendling1b0c54f2013-01-18 21:53:16 +000060 if (CallerAttrs.hasAttribute(AttributeSet::ReturnIndex, Attribute::ZExt) ||
61 CallerAttrs.hasAttribute(AttributeSet::ReturnIndex, Attribute::SExt))
Tim Northover2c8cf4b2013-01-09 13:18:15 +000062 return false;
63
64 // Check if the only use is a function return node.
65 return isUsedByReturnOnly(Node, Chain);
66}
67
Andrew Trick2343e3b2013-10-31 17:18:24 +000068/// \brief Set CallLoweringInfo attribute flags based on a call instruction
69/// and called function attributes.
70void TargetLowering::ArgListEntry::setAttributes(ImmutableCallSite *CS,
71 unsigned AttrIdx) {
72 isSExt = CS->paramHasAttr(AttrIdx, Attribute::SExt);
73 isZExt = CS->paramHasAttr(AttrIdx, Attribute::ZExt);
74 isInReg = CS->paramHasAttr(AttrIdx, Attribute::InReg);
75 isSRet = CS->paramHasAttr(AttrIdx, Attribute::StructRet);
76 isNest = CS->paramHasAttr(AttrIdx, Attribute::Nest);
77 isByVal = CS->paramHasAttr(AttrIdx, Attribute::ByVal);
Stephen Hines36b56882014-04-23 16:57:46 -070078 isInAlloca = CS->paramHasAttr(AttrIdx, Attribute::InAlloca);
Andrew Trick2343e3b2013-10-31 17:18:24 +000079 isReturned = CS->paramHasAttr(AttrIdx, Attribute::Returned);
80 Alignment = CS->getParamAlignment(AttrIdx);
81}
Tim Northover2c8cf4b2013-01-09 13:18:15 +000082
83/// Generate a libcall taking the given operands as arguments and returning a
84/// result of type RetVT.
Michael Gottesman3add0672013-08-13 17:54:56 +000085std::pair<SDValue, SDValue>
86TargetLowering::makeLibCall(SelectionDAG &DAG,
87 RTLIB::Libcall LC, EVT RetVT,
88 const SDValue *Ops, unsigned NumOps,
89 bool isSigned, SDLoc dl,
90 bool doesNotReturn,
91 bool isReturnValueUsed) const {
Tim Northover2c8cf4b2013-01-09 13:18:15 +000092 TargetLowering::ArgListTy Args;
93 Args.reserve(NumOps);
94
95 TargetLowering::ArgListEntry Entry;
96 for (unsigned i = 0; i != NumOps; ++i) {
97 Entry.Node = Ops[i];
98 Entry.Ty = Entry.Node.getValueType().getTypeForEVT(*DAG.getContext());
99 Entry.isSExt = isSigned;
100 Entry.isZExt = !isSigned;
101 Args.push_back(Entry);
102 }
103 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC), getPointerTy());
104
105 Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
Stephen Hinesdce4a402014-05-29 02:49:00 -0700106 TargetLowering::CallLoweringInfo CLI(DAG);
107 CLI.setDebugLoc(dl).setChain(DAG.getEntryNode())
108 .setCallee(getLibcallCallingConv(LC), RetTy, Callee, &Args, 0)
109 .setNoReturn(doesNotReturn).setDiscardResult(!isReturnValueUsed)
110 .setSExtResult(isSigned).setZExtResult(!isSigned);
Michael Gottesman3add0672013-08-13 17:54:56 +0000111 return LowerCallTo(CLI);
Tim Northover2c8cf4b2013-01-09 13:18:15 +0000112}
113
114
115/// SoftenSetCCOperands - Soften the operands of a comparison. This code is
116/// shared among BR_CC, SELECT_CC, and SETCC handlers.
117void TargetLowering::softenSetCCOperands(SelectionDAG &DAG, EVT VT,
118 SDValue &NewLHS, SDValue &NewRHS,
119 ISD::CondCode &CCCode,
Andrew Trickac6d9be2013-05-25 02:42:55 +0000120 SDLoc dl) const {
Tim Northover2c8cf4b2013-01-09 13:18:15 +0000121 assert((VT == MVT::f32 || VT == MVT::f64 || VT == MVT::f128)
122 && "Unsupported setcc type!");
123
124 // Expand into one or more soft-fp libcall(s).
125 RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL;
126 switch (CCCode) {
127 case ISD::SETEQ:
128 case ISD::SETOEQ:
129 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 :
130 (VT == MVT::f64) ? RTLIB::OEQ_F64 : RTLIB::OEQ_F128;
131 break;
132 case ISD::SETNE:
133 case ISD::SETUNE:
134 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 :
135 (VT == MVT::f64) ? RTLIB::UNE_F64 : RTLIB::UNE_F128;
136 break;
137 case ISD::SETGE:
138 case ISD::SETOGE:
139 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 :
140 (VT == MVT::f64) ? RTLIB::OGE_F64 : RTLIB::OGE_F128;
141 break;
142 case ISD::SETLT:
143 case ISD::SETOLT:
144 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 :
145 (VT == MVT::f64) ? RTLIB::OLT_F64 : RTLIB::OLT_F128;
146 break;
147 case ISD::SETLE:
148 case ISD::SETOLE:
149 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 :
150 (VT == MVT::f64) ? RTLIB::OLE_F64 : RTLIB::OLE_F128;
151 break;
152 case ISD::SETGT:
153 case ISD::SETOGT:
154 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 :
155 (VT == MVT::f64) ? RTLIB::OGT_F64 : RTLIB::OGT_F128;
156 break;
157 case ISD::SETUO:
158 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 :
159 (VT == MVT::f64) ? RTLIB::UO_F64 : RTLIB::UO_F128;
160 break;
161 case ISD::SETO:
162 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 :
163 (VT == MVT::f64) ? RTLIB::O_F64 : RTLIB::O_F128;
164 break;
165 default:
166 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 :
167 (VT == MVT::f64) ? RTLIB::UO_F64 : RTLIB::UO_F128;
168 switch (CCCode) {
169 case ISD::SETONE:
170 // SETONE = SETOLT | SETOGT
171 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 :
172 (VT == MVT::f64) ? RTLIB::OLT_F64 : RTLIB::OLT_F128;
173 // Fallthrough
174 case ISD::SETUGT:
175 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 :
176 (VT == MVT::f64) ? RTLIB::OGT_F64 : RTLIB::OGT_F128;
177 break;
178 case ISD::SETUGE:
179 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 :
180 (VT == MVT::f64) ? RTLIB::OGE_F64 : RTLIB::OGE_F128;
181 break;
182 case ISD::SETULT:
183 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 :
184 (VT == MVT::f64) ? RTLIB::OLT_F64 : RTLIB::OLT_F128;
185 break;
186 case ISD::SETULE:
187 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 :
188 (VT == MVT::f64) ? RTLIB::OLE_F64 : RTLIB::OLE_F128;
189 break;
190 case ISD::SETUEQ:
191 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 :
192 (VT == MVT::f64) ? RTLIB::OEQ_F64 : RTLIB::OEQ_F128;
193 break;
194 default: llvm_unreachable("Do not know how to soften this setcc!");
195 }
196 }
197
198 // Use the target specific return value for comparions lib calls.
199 EVT RetVT = getCmpLibcallReturnType();
200 SDValue Ops[2] = { NewLHS, NewRHS };
Michael Gottesman3add0672013-08-13 17:54:56 +0000201 NewLHS = makeLibCall(DAG, LC1, RetVT, Ops, 2, false/*sign irrelevant*/,
202 dl).first;
Tim Northover2c8cf4b2013-01-09 13:18:15 +0000203 NewRHS = DAG.getConstant(0, RetVT);
204 CCCode = getCmpLibcallCC(LC1);
205 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Matt Arsenault225ed702013-05-18 00:21:46 +0000206 SDValue Tmp = DAG.getNode(ISD::SETCC, dl,
207 getSetCCResultType(*DAG.getContext(), RetVT),
Tim Northover2c8cf4b2013-01-09 13:18:15 +0000208 NewLHS, NewRHS, DAG.getCondCode(CCCode));
Michael Gottesman3add0672013-08-13 17:54:56 +0000209 NewLHS = makeLibCall(DAG, LC2, RetVT, Ops, 2, false/*sign irrelevant*/,
210 dl).first;
Matt Arsenault225ed702013-05-18 00:21:46 +0000211 NewLHS = DAG.getNode(ISD::SETCC, dl,
212 getSetCCResultType(*DAG.getContext(), RetVT), NewLHS,
Tim Northover2c8cf4b2013-01-09 13:18:15 +0000213 NewRHS, DAG.getCondCode(getCmpLibcallCC(LC2)));
214 NewLHS = DAG.getNode(ISD::OR, dl, Tmp.getValueType(), Tmp, NewLHS);
215 NewRHS = SDValue();
216 }
217}
218
Chris Lattner071c62f2010-01-25 23:26:13 +0000219/// getJumpTableEncoding - Return the entry encoding for a jump table in the
220/// current function. The returned value is a member of the
221/// MachineJumpTableInfo::JTEntryKind enum.
222unsigned TargetLowering::getJumpTableEncoding() const {
223 // In non-pic modes, just use the address of a block.
224 if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
225 return MachineJumpTableInfo::EK_BlockAddress;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000226
Chris Lattner071c62f2010-01-25 23:26:13 +0000227 // In PIC mode, if the target supports a GPRel32 directive, use it.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700228 if (getTargetMachine().getMCAsmInfo()->getGPRel32Directive() != nullptr)
Chris Lattner071c62f2010-01-25 23:26:13 +0000229 return MachineJumpTableInfo::EK_GPRel32BlockAddress;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000230
Chris Lattner071c62f2010-01-25 23:26:13 +0000231 // Otherwise, use a label difference.
232 return MachineJumpTableInfo::EK_LabelDifference32;
233}
234
Dan Gohman475871a2008-07-27 21:46:04 +0000235SDValue TargetLowering::getPICJumpTableRelocBase(SDValue Table,
236 SelectionDAG &DAG) const {
Chris Lattnerf1214cb2010-01-26 06:53:37 +0000237 // If our PIC model is GP relative, use the global offset table as the base.
Akira Hatanaka787c3fd2012-04-09 20:32:12 +0000238 unsigned JTEncoding = getJumpTableEncoding();
239
240 if ((JTEncoding == MachineJumpTableInfo::EK_GPRel64BlockAddress) ||
241 (JTEncoding == MachineJumpTableInfo::EK_GPRel32BlockAddress))
Micah Villmow7d661462012-10-09 16:06:12 +0000242 return DAG.getGLOBAL_OFFSET_TABLE(getPointerTy(0));
Akira Hatanaka787c3fd2012-04-09 20:32:12 +0000243
Evan Chengcc415862007-11-09 01:32:10 +0000244 return Table;
245}
246
Chris Lattner13e97a22010-01-26 05:30:30 +0000247/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
248/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
249/// MCExpr.
250const MCExpr *
Chris Lattner589c6f62010-01-26 06:28:43 +0000251TargetLowering::getPICJumpTableRelocBaseExpr(const MachineFunction *MF,
252 unsigned JTI,MCContext &Ctx) const{
Chris Lattnerbeeb93e2010-01-26 05:58:28 +0000253 // The normal PIC reloc base is the label at the start of the jump table.
Chris Lattner589c6f62010-01-26 06:28:43 +0000254 return MCSymbolRefExpr::Create(MF->getJTISymbol(JTI, Ctx), Ctx);
Chris Lattner13e97a22010-01-26 05:30:30 +0000255}
256
Dan Gohman6520e202008-10-18 02:06:02 +0000257bool
258TargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
259 // Assume that everything is safe in static mode.
260 if (getTargetMachine().getRelocationModel() == Reloc::Static)
261 return true;
262
263 // In dynamic-no-pic mode, assume that known defined values are safe.
264 if (getTargetMachine().getRelocationModel() == Reloc::DynamicNoPIC &&
265 GA &&
266 !GA->getGlobal()->isDeclaration() &&
Duncan Sands667d4b82009-03-07 15:45:40 +0000267 !GA->getGlobal()->isWeakForLinker())
Dan Gohman6520e202008-10-18 02:06:02 +0000268 return true;
269
270 // Otherwise assume nothing is safe.
271 return false;
272}
273
Chris Lattnereb8146b2006-02-04 02:13:02 +0000274//===----------------------------------------------------------------------===//
275// Optimization Methods
276//===----------------------------------------------------------------------===//
277
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000278/// ShrinkDemandedConstant - Check to see if the specified operand of the
Nate Begeman368e18d2006-02-16 21:11:51 +0000279/// specified instruction is a constant integer. If so, check to see if there
280/// are any bits set in the constant that are not demanded. If so, shrink the
281/// constant and return true.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000282bool TargetLowering::TargetLoweringOpt::ShrinkDemandedConstant(SDValue Op,
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000283 const APInt &Demanded) {
Andrew Trickac6d9be2013-05-25 02:42:55 +0000284 SDLoc dl(Op);
Bill Wendling36ae6c12009-03-04 00:18:06 +0000285
Chris Lattnerec665152006-02-26 23:36:02 +0000286 // FIXME: ISD::SELECT, ISD::SELECT_CC
Dan Gohmane5af2d32009-01-29 01:59:02 +0000287 switch (Op.getOpcode()) {
Nate Begeman368e18d2006-02-16 21:11:51 +0000288 default: break;
Nate Begeman368e18d2006-02-16 21:11:51 +0000289 case ISD::XOR:
Bill Wendling36ae6c12009-03-04 00:18:06 +0000290 case ISD::AND:
291 case ISD::OR: {
292 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
293 if (!C) return false;
294
295 if (Op.getOpcode() == ISD::XOR &&
296 (C->getAPIntValue() | (~Demanded)).isAllOnesValue())
297 return false;
298
299 // if we can expand it to have all bits set, do it
300 if (C->getAPIntValue().intersects(~Demanded)) {
Owen Andersone50ed302009-08-10 22:56:29 +0000301 EVT VT = Op.getValueType();
Bill Wendling36ae6c12009-03-04 00:18:06 +0000302 SDValue New = DAG.getNode(Op.getOpcode(), dl, VT, Op.getOperand(0),
303 DAG.getConstant(Demanded &
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000304 C->getAPIntValue(),
Bill Wendling36ae6c12009-03-04 00:18:06 +0000305 VT));
306 return CombineTo(Op, New);
307 }
308
Nate Begemande996292006-02-03 22:24:05 +0000309 break;
310 }
Bill Wendling36ae6c12009-03-04 00:18:06 +0000311 }
312
Nate Begemande996292006-02-03 22:24:05 +0000313 return false;
314}
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000315
Dan Gohman97121ba2009-04-08 00:15:30 +0000316/// ShrinkDemandedOp - Convert x+y to (VT)((SmallVT)x+(SmallVT)y) if the
317/// casts are free. This uses isZExtFree and ZERO_EXTEND for the widening
318/// cast, but it could be generalized for targets with other types of
319/// implicit widening casts.
320bool
321TargetLowering::TargetLoweringOpt::ShrinkDemandedOp(SDValue Op,
322 unsigned BitWidth,
323 const APInt &Demanded,
Andrew Trickac6d9be2013-05-25 02:42:55 +0000324 SDLoc dl) {
Dan Gohman97121ba2009-04-08 00:15:30 +0000325 assert(Op.getNumOperands() == 2 &&
326 "ShrinkDemandedOp only supports binary operators!");
327 assert(Op.getNode()->getNumValues() == 1 &&
328 "ShrinkDemandedOp only supports nodes with one result!");
329
330 // Don't do this if the node has another user, which may require the
331 // full value.
332 if (!Op.getNode()->hasOneUse())
333 return false;
334
335 // Search for the smallest integer type with free casts to and from
336 // Op's type. For expedience, just check power-of-2 integer types.
337 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Nadav Rotembf5a2c62012-12-19 07:39:08 +0000338 unsigned DemandedSize = BitWidth - Demanded.countLeadingZeros();
339 unsigned SmallVTBits = DemandedSize;
Dan Gohman97121ba2009-04-08 00:15:30 +0000340 if (!isPowerOf2_32(SmallVTBits))
341 SmallVTBits = NextPowerOf2(SmallVTBits);
342 for (; SmallVTBits < BitWidth; SmallVTBits = NextPowerOf2(SmallVTBits)) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000343 EVT SmallVT = EVT::getIntegerVT(*DAG.getContext(), SmallVTBits);
Dan Gohman97121ba2009-04-08 00:15:30 +0000344 if (TLI.isTruncateFree(Op.getValueType(), SmallVT) &&
345 TLI.isZExtFree(SmallVT, Op.getValueType())) {
346 // We found a type with free casts.
347 SDValue X = DAG.getNode(Op.getOpcode(), dl, SmallVT,
348 DAG.getNode(ISD::TRUNCATE, dl, SmallVT,
349 Op.getNode()->getOperand(0)),
350 DAG.getNode(ISD::TRUNCATE, dl, SmallVT,
351 Op.getNode()->getOperand(1)));
Nadav Rotembf5a2c62012-12-19 07:39:08 +0000352 bool NeedZext = DemandedSize > SmallVTBits;
353 SDValue Z = DAG.getNode(NeedZext ? ISD::ZERO_EXTEND : ISD::ANY_EXTEND,
354 dl, Op.getValueType(), X);
Dan Gohman97121ba2009-04-08 00:15:30 +0000355 return CombineTo(Op, Z);
356 }
357 }
358 return false;
359}
360
Nate Begeman368e18d2006-02-16 21:11:51 +0000361/// SimplifyDemandedBits - Look at Op. At this point, we know that only the
Chad Rosier8c1ec5a2011-06-11 02:27:46 +0000362/// DemandedMask bits of the result of Op are ever used downstream. If we can
Nate Begeman368e18d2006-02-16 21:11:51 +0000363/// use this information to simplify Op, create a new simplified DAG node and
364/// return true, returning the original and new nodes in Old and New. Otherwise,
365/// analyze the expression and return a mask of KnownOne and KnownZero bits for
366/// the expression (used to simplify the caller). The KnownZero/One bits may
367/// only be accurate for those bits in the DemandedMask.
Dan Gohman475871a2008-07-27 21:46:04 +0000368bool TargetLowering::SimplifyDemandedBits(SDValue Op,
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000369 const APInt &DemandedMask,
370 APInt &KnownZero,
371 APInt &KnownOne,
Nate Begeman368e18d2006-02-16 21:11:51 +0000372 TargetLoweringOpt &TLO,
373 unsigned Depth) const {
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000374 unsigned BitWidth = DemandedMask.getBitWidth();
Dan Gohman87862e72009-12-11 21:31:27 +0000375 assert(Op.getValueType().getScalarType().getSizeInBits() == BitWidth &&
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000376 "Mask size mismatches value type size!");
377 APInt NewMask = DemandedMask;
Andrew Trickac6d9be2013-05-25 02:42:55 +0000378 SDLoc dl(Op);
Chris Lattner3fc5b012007-05-17 18:19:23 +0000379
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000380 // Don't know anything.
381 KnownZero = KnownOne = APInt(BitWidth, 0);
382
Nate Begeman368e18d2006-02-16 21:11:51 +0000383 // Other users may use these bits.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000384 if (!Op.getNode()->hasOneUse()) {
Nate Begeman368e18d2006-02-16 21:11:51 +0000385 if (Depth != 0) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000386 // If not at the root, Just compute the KnownZero/KnownOne bits to
Nate Begeman368e18d2006-02-16 21:11:51 +0000387 // simplify things downstream.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700388 TLO.DAG.computeKnownBits(Op, KnownZero, KnownOne, Depth);
Nate Begeman368e18d2006-02-16 21:11:51 +0000389 return false;
390 }
391 // If this is the root being simplified, allow it to have multiple uses,
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000392 // just set the NewMask to all bits.
393 NewMask = APInt::getAllOnesValue(BitWidth);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000394 } else if (DemandedMask == 0) {
Nate Begeman368e18d2006-02-16 21:11:51 +0000395 // Not demanding any bits from Op.
396 if (Op.getOpcode() != ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +0000397 return TLO.CombineTo(Op, TLO.DAG.getUNDEF(Op.getValueType()));
Nate Begeman368e18d2006-02-16 21:11:51 +0000398 return false;
399 } else if (Depth == 6) { // Limit search depth.
400 return false;
401 }
402
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000403 APInt KnownZero2, KnownOne2, KnownZeroOut, KnownOneOut;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000404 switch (Op.getOpcode()) {
405 case ISD::Constant:
Nate Begeman368e18d2006-02-16 21:11:51 +0000406 // We know all of the bits for a constant!
Rafael Espindola26c8dcc2012-04-04 12:51:34 +0000407 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue();
408 KnownZero = ~KnownOne;
Chris Lattnerec665152006-02-26 23:36:02 +0000409 return false; // Don't fall through, will infinitely loop.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000410 case ISD::AND:
Chris Lattner81cd3552006-02-27 00:36:27 +0000411 // If the RHS is a constant, check to see if the LHS would be zero without
412 // using the bits from the RHS. Below, we use knowledge about the RHS to
413 // simplify the LHS, here we're using information from the LHS to simplify
414 // the RHS.
415 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000416 APInt LHSZero, LHSOne;
Dale Johannesen97fd9a52011-01-10 21:53:07 +0000417 // Do not increment Depth here; that can cause an infinite loop.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700418 TLO.DAG.computeKnownBits(Op.getOperand(0), LHSZero, LHSOne, Depth);
Chris Lattner81cd3552006-02-27 00:36:27 +0000419 // If the LHS already has zeros where RHSC does, this and is dead.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000420 if ((LHSZero & NewMask) == (~RHSC->getAPIntValue() & NewMask))
Chris Lattner81cd3552006-02-27 00:36:27 +0000421 return TLO.CombineTo(Op, Op.getOperand(0));
422 // If any of the set bits in the RHS are known zero on the LHS, shrink
423 // the constant.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000424 if (TLO.ShrinkDemandedConstant(Op, ~LHSZero & NewMask))
Chris Lattner81cd3552006-02-27 00:36:27 +0000425 return true;
426 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000427
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000428 if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero,
Nate Begeman368e18d2006-02-16 21:11:51 +0000429 KnownOne, TLO, Depth+1))
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000430 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000431 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000432 if (SimplifyDemandedBits(Op.getOperand(0), ~KnownZero & NewMask,
Nate Begeman368e18d2006-02-16 21:11:51 +0000433 KnownZero2, KnownOne2, TLO, Depth+1))
434 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000435 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
436
Nate Begeman368e18d2006-02-16 21:11:51 +0000437 // If all of the demanded bits are known one on one side, return the other.
438 // These bits cannot contribute to the result of the 'and'.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000439 if ((NewMask & ~KnownZero2 & KnownOne) == (~KnownZero2 & NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +0000440 return TLO.CombineTo(Op, Op.getOperand(0));
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000441 if ((NewMask & ~KnownZero & KnownOne2) == (~KnownZero & NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +0000442 return TLO.CombineTo(Op, Op.getOperand(1));
443 // If all of the demanded bits in the inputs are known zeros, return zero.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000444 if ((NewMask & (KnownZero|KnownZero2)) == NewMask)
Nate Begeman368e18d2006-02-16 21:11:51 +0000445 return TLO.CombineTo(Op, TLO.DAG.getConstant(0, Op.getValueType()));
446 // If the RHS is a constant, see if we can simplify it.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000447 if (TLO.ShrinkDemandedConstant(Op, ~KnownZero2 & NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +0000448 return true;
Dan Gohman97121ba2009-04-08 00:15:30 +0000449 // If the operation can be done in a smaller type, do so.
Dan Gohman4e39e9d2010-06-24 14:30:44 +0000450 if (TLO.ShrinkDemandedOp(Op, BitWidth, NewMask, dl))
Dan Gohman97121ba2009-04-08 00:15:30 +0000451 return true;
452
Nate Begeman368e18d2006-02-16 21:11:51 +0000453 // Output known-1 bits are only known if set in both the LHS & RHS.
454 KnownOne &= KnownOne2;
455 // Output known-0 are known to be clear if zero in either the LHS | RHS.
456 KnownZero |= KnownZero2;
457 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000458 case ISD::OR:
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000459 if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero,
Nate Begeman368e18d2006-02-16 21:11:51 +0000460 KnownOne, TLO, Depth+1))
461 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000462 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000463 if (SimplifyDemandedBits(Op.getOperand(0), ~KnownOne & NewMask,
Nate Begeman368e18d2006-02-16 21:11:51 +0000464 KnownZero2, KnownOne2, TLO, Depth+1))
465 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000466 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
467
Nate Begeman368e18d2006-02-16 21:11:51 +0000468 // If all of the demanded bits are known zero on one side, return the other.
469 // These bits cannot contribute to the result of the 'or'.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000470 if ((NewMask & ~KnownOne2 & KnownZero) == (~KnownOne2 & NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +0000471 return TLO.CombineTo(Op, Op.getOperand(0));
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000472 if ((NewMask & ~KnownOne & KnownZero2) == (~KnownOne & NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +0000473 return TLO.CombineTo(Op, Op.getOperand(1));
474 // If all of the potentially set bits on one side are known to be set on
475 // the other side, just use the 'other' side.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000476 if ((NewMask & ~KnownZero & KnownOne2) == (~KnownZero & NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +0000477 return TLO.CombineTo(Op, Op.getOperand(0));
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000478 if ((NewMask & ~KnownZero2 & KnownOne) == (~KnownZero2 & NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +0000479 return TLO.CombineTo(Op, Op.getOperand(1));
480 // If the RHS is a constant, see if we can simplify it.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000481 if (TLO.ShrinkDemandedConstant(Op, NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +0000482 return true;
Dan Gohman97121ba2009-04-08 00:15:30 +0000483 // If the operation can be done in a smaller type, do so.
Dan Gohman4e39e9d2010-06-24 14:30:44 +0000484 if (TLO.ShrinkDemandedOp(Op, BitWidth, NewMask, dl))
Dan Gohman97121ba2009-04-08 00:15:30 +0000485 return true;
486
Nate Begeman368e18d2006-02-16 21:11:51 +0000487 // Output known-0 bits are only known if clear in both the LHS & RHS.
488 KnownZero &= KnownZero2;
489 // Output known-1 are known to be set if set in either the LHS | RHS.
490 KnownOne |= KnownOne2;
491 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000492 case ISD::XOR:
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000493 if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero,
Nate Begeman368e18d2006-02-16 21:11:51 +0000494 KnownOne, TLO, Depth+1))
495 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000496 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000497 if (SimplifyDemandedBits(Op.getOperand(0), NewMask, KnownZero2,
Nate Begeman368e18d2006-02-16 21:11:51 +0000498 KnownOne2, TLO, Depth+1))
499 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000500 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
501
Nate Begeman368e18d2006-02-16 21:11:51 +0000502 // If all of the demanded bits are known zero on one side, return the other.
503 // These bits cannot contribute to the result of the 'xor'.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000504 if ((KnownZero & NewMask) == NewMask)
Nate Begeman368e18d2006-02-16 21:11:51 +0000505 return TLO.CombineTo(Op, Op.getOperand(0));
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000506 if ((KnownZero2 & NewMask) == NewMask)
Nate Begeman368e18d2006-02-16 21:11:51 +0000507 return TLO.CombineTo(Op, Op.getOperand(1));
Dan Gohman97121ba2009-04-08 00:15:30 +0000508 // If the operation can be done in a smaller type, do so.
Dan Gohman4e39e9d2010-06-24 14:30:44 +0000509 if (TLO.ShrinkDemandedOp(Op, BitWidth, NewMask, dl))
Dan Gohman97121ba2009-04-08 00:15:30 +0000510 return true;
511
Chris Lattner3687c1a2006-11-27 21:50:02 +0000512 // If all of the unknown bits are known to be zero on one side or the other
513 // (but not both) turn this into an *inclusive* or.
Sylvestre Ledru94c22712012-09-27 10:14:43 +0000514 // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000515 if ((NewMask & ~KnownZero & ~KnownZero2) == 0)
Dale Johannesende064702009-02-06 21:50:26 +0000516 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::OR, dl, Op.getValueType(),
Chris Lattner3687c1a2006-11-27 21:50:02 +0000517 Op.getOperand(0),
518 Op.getOperand(1)));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000519
Nate Begeman368e18d2006-02-16 21:11:51 +0000520 // Output known-0 bits are known if clear or set in both the LHS & RHS.
521 KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
522 // Output known-1 are known to be set if set in only one of the LHS, RHS.
523 KnownOneOut = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000524
Nate Begeman368e18d2006-02-16 21:11:51 +0000525 // If all of the demanded bits on one side are known, and all of the set
526 // bits on that side are also known to be set on the other side, turn this
527 // into an AND, as we know the bits will be cleared.
Sylvestre Ledru94c22712012-09-27 10:14:43 +0000528 // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
Joel Jonesd16ce172012-04-17 22:23:10 +0000529 // NB: it is okay if more bits are known than are requested
Stephen Lin155615d2013-07-08 00:37:03 +0000530 if ((NewMask & (KnownZero|KnownOne)) == NewMask) { // all known on one side
Joel Jonesd16ce172012-04-17 22:23:10 +0000531 if (KnownOne == KnownOne2) { // set bits are the same on both sides
Owen Andersone50ed302009-08-10 22:56:29 +0000532 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +0000533 SDValue ANDC = TLO.DAG.getConstant(~KnownOne & NewMask, VT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000534 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::AND, dl, VT,
Dale Johannesenff97d4f2009-02-03 00:47:48 +0000535 Op.getOperand(0), ANDC));
Nate Begeman368e18d2006-02-16 21:11:51 +0000536 }
537 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000538
Nate Begeman368e18d2006-02-16 21:11:51 +0000539 // If the RHS is a constant, see if we can simplify it.
Torok Edwin4fea2e92008-04-06 21:23:02 +0000540 // for XOR, we prefer to force bits to 1 if they will make a -1.
541 // if we can't force bits, try to shrink constant
542 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
543 APInt Expanded = C->getAPIntValue() | (~NewMask);
544 // if we can expand it to have all bits set, do it
545 if (Expanded.isAllOnesValue()) {
546 if (Expanded != C->getAPIntValue()) {
Owen Andersone50ed302009-08-10 22:56:29 +0000547 EVT VT = Op.getValueType();
Dale Johannesenff97d4f2009-02-03 00:47:48 +0000548 SDValue New = TLO.DAG.getNode(Op.getOpcode(), dl,VT, Op.getOperand(0),
Torok Edwin4fea2e92008-04-06 21:23:02 +0000549 TLO.DAG.getConstant(Expanded, VT));
550 return TLO.CombineTo(Op, New);
551 }
552 // if it already has all the bits set, nothing to change
553 // but don't shrink either!
554 } else if (TLO.ShrinkDemandedConstant(Op, NewMask)) {
555 return true;
556 }
557 }
558
Nate Begeman368e18d2006-02-16 21:11:51 +0000559 KnownZero = KnownZeroOut;
560 KnownOne = KnownOneOut;
561 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000562 case ISD::SELECT:
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000563 if (SimplifyDemandedBits(Op.getOperand(2), NewMask, KnownZero,
Nate Begeman368e18d2006-02-16 21:11:51 +0000564 KnownOne, TLO, Depth+1))
565 return true;
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000566 if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero2,
Nate Begeman368e18d2006-02-16 21:11:51 +0000567 KnownOne2, TLO, Depth+1))
568 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000569 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
570 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
571
Nate Begeman368e18d2006-02-16 21:11:51 +0000572 // If the operands are constants, see if we can simplify them.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000573 if (TLO.ShrinkDemandedConstant(Op, NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +0000574 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000575
Nate Begeman368e18d2006-02-16 21:11:51 +0000576 // Only known if known in both the LHS and RHS.
577 KnownOne &= KnownOne2;
578 KnownZero &= KnownZero2;
579 break;
Chris Lattnerec665152006-02-26 23:36:02 +0000580 case ISD::SELECT_CC:
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000581 if (SimplifyDemandedBits(Op.getOperand(3), NewMask, KnownZero,
Chris Lattnerec665152006-02-26 23:36:02 +0000582 KnownOne, TLO, Depth+1))
583 return true;
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000584 if (SimplifyDemandedBits(Op.getOperand(2), NewMask, KnownZero2,
Chris Lattnerec665152006-02-26 23:36:02 +0000585 KnownOne2, TLO, Depth+1))
586 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000587 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
588 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
589
Chris Lattnerec665152006-02-26 23:36:02 +0000590 // If the operands are constants, see if we can simplify them.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000591 if (TLO.ShrinkDemandedConstant(Op, NewMask))
Chris Lattnerec665152006-02-26 23:36:02 +0000592 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000593
Chris Lattnerec665152006-02-26 23:36:02 +0000594 // Only known if known in both the LHS and RHS.
595 KnownOne &= KnownOne2;
596 KnownZero &= KnownZero2;
597 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000598 case ISD::SHL:
Nate Begeman368e18d2006-02-16 21:11:51 +0000599 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000600 unsigned ShAmt = SA->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +0000601 SDValue InOp = Op.getOperand(0);
Chris Lattner895c4ab2007-04-17 21:14:16 +0000602
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000603 // If the shift count is an invalid immediate, don't do anything.
604 if (ShAmt >= BitWidth)
605 break;
606
Chris Lattner895c4ab2007-04-17 21:14:16 +0000607 // If this is ((X >>u C1) << ShAmt), see if we can simplify this into a
608 // single shift. We can do this if the bottom bits (which are shifted
609 // out) are never demanded.
610 if (InOp.getOpcode() == ISD::SRL &&
611 isa<ConstantSDNode>(InOp.getOperand(1))) {
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000612 if (ShAmt && (NewMask & APInt::getLowBitsSet(BitWidth, ShAmt)) == 0) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000613 unsigned C1= cast<ConstantSDNode>(InOp.getOperand(1))->getZExtValue();
Chris Lattner895c4ab2007-04-17 21:14:16 +0000614 unsigned Opc = ISD::SHL;
615 int Diff = ShAmt-C1;
616 if (Diff < 0) {
617 Diff = -Diff;
618 Opc = ISD::SRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000619 }
620
621 SDValue NewSA =
Chris Lattner4e7e6cd2007-05-30 16:30:06 +0000622 TLO.DAG.getConstant(Diff, Op.getOperand(1).getValueType());
Owen Andersone50ed302009-08-10 22:56:29 +0000623 EVT VT = Op.getValueType();
Dale Johannesenff97d4f2009-02-03 00:47:48 +0000624 return TLO.CombineTo(Op, TLO.DAG.getNode(Opc, dl, VT,
Chris Lattner895c4ab2007-04-17 21:14:16 +0000625 InOp.getOperand(0), NewSA));
626 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000627 }
628
Dan Gohmana4f4d692010-07-23 18:03:30 +0000629 if (SimplifyDemandedBits(InOp, NewMask.lshr(ShAmt),
Nate Begeman368e18d2006-02-16 21:11:51 +0000630 KnownZero, KnownOne, TLO, Depth+1))
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000631 return true;
Dan Gohmana4f4d692010-07-23 18:03:30 +0000632
633 // Convert (shl (anyext x, c)) to (anyext (shl x, c)) if the high bits
634 // are not demanded. This will likely allow the anyext to be folded away.
635 if (InOp.getNode()->getOpcode() == ISD::ANY_EXTEND) {
636 SDValue InnerOp = InOp.getNode()->getOperand(0);
637 EVT InnerVT = InnerOp.getValueType();
Eli Friedman2dd03532011-12-09 01:16:26 +0000638 unsigned InnerBits = InnerVT.getSizeInBits();
639 if (ShAmt < InnerBits && NewMask.lshr(InnerBits) == 0 &&
Dan Gohmana4f4d692010-07-23 18:03:30 +0000640 isTypeDesirableForOp(ISD::SHL, InnerVT)) {
Owen Anderson95771af2011-02-25 21:41:48 +0000641 EVT ShTy = getShiftAmountTy(InnerVT);
Dan Gohmancd20c6f2010-07-23 21:08:12 +0000642 if (!APInt(BitWidth, ShAmt).isIntN(ShTy.getSizeInBits()))
643 ShTy = InnerVT;
Dan Gohmana4f4d692010-07-23 18:03:30 +0000644 SDValue NarrowShl =
645 TLO.DAG.getNode(ISD::SHL, dl, InnerVT, InnerOp,
Dan Gohmancd20c6f2010-07-23 21:08:12 +0000646 TLO.DAG.getConstant(ShAmt, ShTy));
Dan Gohmana4f4d692010-07-23 18:03:30 +0000647 return
648 TLO.CombineTo(Op,
649 TLO.DAG.getNode(ISD::ANY_EXTEND, dl, Op.getValueType(),
650 NarrowShl));
651 }
Richard Sandiford5d7e93c2013-10-16 10:26:19 +0000652 // Repeat the SHL optimization above in cases where an extension
653 // intervenes: (shl (anyext (shr x, c1)), c2) to
654 // (shl (anyext x), c2-c1). This requires that the bottom c1 bits
655 // aren't demanded (as above) and that the shifted upper c1 bits of
656 // x aren't demanded.
657 if (InOp.hasOneUse() &&
658 InnerOp.getOpcode() == ISD::SRL &&
659 InnerOp.hasOneUse() &&
660 isa<ConstantSDNode>(InnerOp.getOperand(1))) {
661 uint64_t InnerShAmt = cast<ConstantSDNode>(InnerOp.getOperand(1))
662 ->getZExtValue();
663 if (InnerShAmt < ShAmt &&
664 InnerShAmt < InnerBits &&
665 NewMask.lshr(InnerBits - InnerShAmt + ShAmt) == 0 &&
666 NewMask.trunc(ShAmt) == 0) {
667 SDValue NewSA =
668 TLO.DAG.getConstant(ShAmt - InnerShAmt,
669 Op.getOperand(1).getValueType());
670 EVT VT = Op.getValueType();
671 SDValue NewExt = TLO.DAG.getNode(ISD::ANY_EXTEND, dl, VT,
672 InnerOp.getOperand(0));
673 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SHL, dl, VT,
674 NewExt, NewSA));
675 }
676 }
Dan Gohmana4f4d692010-07-23 18:03:30 +0000677 }
678
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000679 KnownZero <<= SA->getZExtValue();
680 KnownOne <<= SA->getZExtValue();
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000681 // low bits known zero.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000682 KnownZero |= APInt::getLowBitsSet(BitWidth, SA->getZExtValue());
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000683 }
684 break;
Nate Begeman368e18d2006-02-16 21:11:51 +0000685 case ISD::SRL:
686 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Owen Andersone50ed302009-08-10 22:56:29 +0000687 EVT VT = Op.getValueType();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000688 unsigned ShAmt = SA->getZExtValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000689 unsigned VTSize = VT.getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +0000690 SDValue InOp = Op.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000691
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000692 // If the shift count is an invalid immediate, don't do anything.
693 if (ShAmt >= BitWidth)
694 break;
695
Chris Lattner895c4ab2007-04-17 21:14:16 +0000696 // If this is ((X << C1) >>u ShAmt), see if we can simplify this into a
697 // single shift. We can do this if the top bits (which are shifted out)
698 // are never demanded.
699 if (InOp.getOpcode() == ISD::SHL &&
700 isa<ConstantSDNode>(InOp.getOperand(1))) {
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000701 if (ShAmt && (NewMask & APInt::getHighBitsSet(VTSize, ShAmt)) == 0) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000702 unsigned C1= cast<ConstantSDNode>(InOp.getOperand(1))->getZExtValue();
Chris Lattner895c4ab2007-04-17 21:14:16 +0000703 unsigned Opc = ISD::SRL;
704 int Diff = ShAmt-C1;
705 if (Diff < 0) {
706 Diff = -Diff;
707 Opc = ISD::SHL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000708 }
709
Dan Gohman475871a2008-07-27 21:46:04 +0000710 SDValue NewSA =
Chris Lattner8c7d2d52007-04-17 22:53:02 +0000711 TLO.DAG.getConstant(Diff, Op.getOperand(1).getValueType());
Dale Johannesenff97d4f2009-02-03 00:47:48 +0000712 return TLO.CombineTo(Op, TLO.DAG.getNode(Opc, dl, VT,
Chris Lattner895c4ab2007-04-17 21:14:16 +0000713 InOp.getOperand(0), NewSA));
714 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000715 }
716
Nate Begeman368e18d2006-02-16 21:11:51 +0000717 // Compute the new bits that are at the top now.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000718 if (SimplifyDemandedBits(InOp, (NewMask << ShAmt),
Nate Begeman368e18d2006-02-16 21:11:51 +0000719 KnownZero, KnownOne, TLO, Depth+1))
720 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000721 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000722 KnownZero = KnownZero.lshr(ShAmt);
723 KnownOne = KnownOne.lshr(ShAmt);
Chris Lattnerc4fa6032006-06-13 16:52:37 +0000724
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000725 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Chris Lattnerc4fa6032006-06-13 16:52:37 +0000726 KnownZero |= HighBits; // High bits known zero.
Nate Begeman368e18d2006-02-16 21:11:51 +0000727 }
728 break;
729 case ISD::SRA:
Dan Gohmane5af2d32009-01-29 01:59:02 +0000730 // If this is an arithmetic shift right and only the low-bit is set, we can
731 // always convert this into a logical shr, even if the shift amount is
732 // variable. The low bit of the shift cannot be an input sign bit unless
733 // the shift amount is >= the size of the datatype, which is undefined.
Eli Friedman2dd03532011-12-09 01:16:26 +0000734 if (NewMask == 1)
Evan Chenge5b51ac2010-04-17 06:13:15 +0000735 return TLO.CombineTo(Op,
736 TLO.DAG.getNode(ISD::SRL, dl, Op.getValueType(),
737 Op.getOperand(0), Op.getOperand(1)));
Dan Gohmane5af2d32009-01-29 01:59:02 +0000738
Nate Begeman368e18d2006-02-16 21:11:51 +0000739 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Owen Andersone50ed302009-08-10 22:56:29 +0000740 EVT VT = Op.getValueType();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000741 unsigned ShAmt = SA->getZExtValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000742
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000743 // If the shift count is an invalid immediate, don't do anything.
744 if (ShAmt >= BitWidth)
745 break;
746
747 APInt InDemandedMask = (NewMask << ShAmt);
Chris Lattner1b737132006-05-08 17:22:53 +0000748
749 // If any of the demanded bits are produced by the sign extension, we also
750 // demand the input sign bit.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000751 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
752 if (HighBits.intersects(NewMask))
Dan Gohman87862e72009-12-11 21:31:27 +0000753 InDemandedMask |= APInt::getSignBit(VT.getScalarType().getSizeInBits());
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000754
Chris Lattner1b737132006-05-08 17:22:53 +0000755 if (SimplifyDemandedBits(Op.getOperand(0), InDemandedMask,
Nate Begeman368e18d2006-02-16 21:11:51 +0000756 KnownZero, KnownOne, TLO, Depth+1))
757 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000758 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000759 KnownZero = KnownZero.lshr(ShAmt);
760 KnownOne = KnownOne.lshr(ShAmt);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000761
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000762 // Handle the sign bit, adjusted to where it is now in the mask.
763 APInt SignBit = APInt::getSignBit(BitWidth).lshr(ShAmt);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000764
Nate Begeman368e18d2006-02-16 21:11:51 +0000765 // If the input sign bit is known to be zero, or if none of the top bits
766 // are demanded, turn this into an unsigned shift right.
Richard Sandifordf9a5e402013-10-17 11:16:57 +0000767 if (KnownZero.intersects(SignBit) || (HighBits & ~NewMask) == HighBits)
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000768 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, dl, VT,
Dale Johannesenff97d4f2009-02-03 00:47:48 +0000769 Op.getOperand(0),
Nate Begeman368e18d2006-02-16 21:11:51 +0000770 Op.getOperand(1)));
Richard Sandifordf9a5e402013-10-17 11:16:57 +0000771
772 int Log2 = NewMask.exactLogBase2();
773 if (Log2 >= 0) {
774 // The bit must come from the sign.
775 SDValue NewSA =
776 TLO.DAG.getConstant(BitWidth - 1 - Log2,
777 Op.getOperand(1).getValueType());
778 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, dl, VT,
779 Op.getOperand(0), NewSA));
Nate Begeman368e18d2006-02-16 21:11:51 +0000780 }
Richard Sandifordf9a5e402013-10-17 11:16:57 +0000781
782 if (KnownOne.intersects(SignBit))
783 // New bits are known one.
784 KnownOne |= HighBits;
Nate Begeman368e18d2006-02-16 21:11:51 +0000785 }
786 break;
787 case ISD::SIGN_EXTEND_INREG: {
Nadav Rotemcc616562012-01-15 19:27:55 +0000788 EVT ExVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
789
790 APInt MsbMask = APInt::getHighBitsSet(BitWidth, 1);
791 // If we only care about the highest bit, don't bother shifting right.
Eli Friedmand49db362012-01-31 01:08:03 +0000792 if (MsbMask == DemandedMask) {
Nadav Rotemcc616562012-01-15 19:27:55 +0000793 unsigned ShAmt = ExVT.getScalarType().getSizeInBits();
794 SDValue InOp = Op.getOperand(0);
Eli Friedmand49db362012-01-31 01:08:03 +0000795
796 // Compute the correct shift amount type, which must be getShiftAmountTy
797 // for scalar types after legalization.
798 EVT ShiftAmtTy = Op.getValueType();
799 if (TLO.LegalTypes() && !ShiftAmtTy.isVector())
800 ShiftAmtTy = getShiftAmountTy(ShiftAmtTy);
801
802 SDValue ShiftAmt = TLO.DAG.getConstant(BitWidth - ShAmt, ShiftAmtTy);
Nadav Rotemcc616562012-01-15 19:27:55 +0000803 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SHL, dl,
804 Op.getValueType(), InOp, ShiftAmt));
805 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000806
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000807 // Sign extension. Compute the demanded bits in the result that are not
Nate Begeman368e18d2006-02-16 21:11:51 +0000808 // present in the input.
Dan Gohmand1996362010-01-09 02:13:55 +0000809 APInt NewBits =
810 APInt::getHighBitsSet(BitWidth,
Nadav Rotemcc616562012-01-15 19:27:55 +0000811 BitWidth - ExVT.getScalarType().getSizeInBits());
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000812
Chris Lattnerec665152006-02-26 23:36:02 +0000813 // If none of the extended bits are demanded, eliminate the sextinreg.
Eli Friedman1d17d192010-08-02 04:42:25 +0000814 if ((NewBits & NewMask) == 0)
Chris Lattnerec665152006-02-26 23:36:02 +0000815 return TLO.CombineTo(Op, Op.getOperand(0));
816
Jay Foad40f8f622010-12-07 08:25:19 +0000817 APInt InSignBit =
Nadav Rotemcc616562012-01-15 19:27:55 +0000818 APInt::getSignBit(ExVT.getScalarType().getSizeInBits()).zext(BitWidth);
Dan Gohmand1996362010-01-09 02:13:55 +0000819 APInt InputDemandedBits =
820 APInt::getLowBitsSet(BitWidth,
Nadav Rotemcc616562012-01-15 19:27:55 +0000821 ExVT.getScalarType().getSizeInBits()) &
Dan Gohmand1996362010-01-09 02:13:55 +0000822 NewMask;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000823
Chris Lattnerec665152006-02-26 23:36:02 +0000824 // Since the sign extended bits are demanded, we know that the sign
Nate Begeman368e18d2006-02-16 21:11:51 +0000825 // bit is demanded.
Chris Lattnerec665152006-02-26 23:36:02 +0000826 InputDemandedBits |= InSignBit;
Nate Begeman368e18d2006-02-16 21:11:51 +0000827
828 if (SimplifyDemandedBits(Op.getOperand(0), InputDemandedBits,
829 KnownZero, KnownOne, TLO, Depth+1))
830 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000831 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Nate Begeman368e18d2006-02-16 21:11:51 +0000832
833 // If the sign bit of the input is known set or clear, then we know the
834 // top bits of the result.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000835
Chris Lattnerec665152006-02-26 23:36:02 +0000836 // If the input sign bit is known zero, convert this into a zero extension.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000837 if (KnownZero.intersects(InSignBit))
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000838 return TLO.CombineTo(Op,
Nadav Rotemcc616562012-01-15 19:27:55 +0000839 TLO.DAG.getZeroExtendInReg(Op.getOperand(0),dl,ExVT));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000840
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000841 if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Nate Begeman368e18d2006-02-16 21:11:51 +0000842 KnownOne |= NewBits;
843 KnownZero &= ~NewBits;
Chris Lattnerec665152006-02-26 23:36:02 +0000844 } else { // Input sign bit unknown
Nate Begeman368e18d2006-02-16 21:11:51 +0000845 KnownZero &= ~NewBits;
846 KnownOne &= ~NewBits;
847 }
848 break;
849 }
Stephen Hinesdce4a402014-05-29 02:49:00 -0700850 case ISD::BUILD_PAIR: {
851 EVT HalfVT = Op.getOperand(0).getValueType();
852 unsigned HalfBitWidth = HalfVT.getScalarSizeInBits();
853
854 APInt MaskLo = NewMask.getLoBits(HalfBitWidth).trunc(HalfBitWidth);
855 APInt MaskHi = NewMask.getHiBits(HalfBitWidth).trunc(HalfBitWidth);
856
857 APInt KnownZeroLo, KnownOneLo;
858 APInt KnownZeroHi, KnownOneHi;
859
860 if (SimplifyDemandedBits(Op.getOperand(0), MaskLo, KnownZeroLo,
861 KnownOneLo, TLO, Depth + 1))
862 return true;
863
864 if (SimplifyDemandedBits(Op.getOperand(1), MaskHi, KnownZeroHi,
865 KnownOneHi, TLO, Depth + 1))
866 return true;
867
868 KnownZero = KnownZeroLo.zext(BitWidth) |
869 KnownZeroHi.zext(BitWidth).shl(HalfBitWidth);
870
871 KnownOne = KnownOneLo.zext(BitWidth) |
872 KnownOneHi.zext(BitWidth).shl(HalfBitWidth);
873 break;
874 }
Chris Lattnerec665152006-02-26 23:36:02 +0000875 case ISD::ZERO_EXTEND: {
Dan Gohmand1996362010-01-09 02:13:55 +0000876 unsigned OperandBitWidth =
877 Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
Jay Foad40f8f622010-12-07 08:25:19 +0000878 APInt InMask = NewMask.trunc(OperandBitWidth);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000879
Chris Lattnerec665152006-02-26 23:36:02 +0000880 // If none of the top bits are demanded, convert this into an any_extend.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000881 APInt NewBits =
882 APInt::getHighBitsSet(BitWidth, BitWidth - OperandBitWidth) & NewMask;
883 if (!NewBits.intersects(NewMask))
Dale Johannesenff97d4f2009-02-03 00:47:48 +0000884 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ANY_EXTEND, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000885 Op.getValueType(),
Chris Lattnerec665152006-02-26 23:36:02 +0000886 Op.getOperand(0)));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000887
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000888 if (SimplifyDemandedBits(Op.getOperand(0), InMask,
Chris Lattnerec665152006-02-26 23:36:02 +0000889 KnownZero, KnownOne, TLO, Depth+1))
890 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000891 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Jay Foad40f8f622010-12-07 08:25:19 +0000892 KnownZero = KnownZero.zext(BitWidth);
893 KnownOne = KnownOne.zext(BitWidth);
Chris Lattnerec665152006-02-26 23:36:02 +0000894 KnownZero |= NewBits;
895 break;
896 }
897 case ISD::SIGN_EXTEND: {
Owen Andersone50ed302009-08-10 22:56:29 +0000898 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohmand1996362010-01-09 02:13:55 +0000899 unsigned InBits = InVT.getScalarType().getSizeInBits();
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000900 APInt InMask = APInt::getLowBitsSet(BitWidth, InBits);
Dan Gohman97360282008-03-11 21:29:43 +0000901 APInt InSignBit = APInt::getBitsSet(BitWidth, InBits - 1, InBits);
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000902 APInt NewBits = ~InMask & NewMask;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000903
Chris Lattnerec665152006-02-26 23:36:02 +0000904 // If none of the top bits are demanded, convert this into an any_extend.
905 if (NewBits == 0)
Dale Johannesenff97d4f2009-02-03 00:47:48 +0000906 return TLO.CombineTo(Op,TLO.DAG.getNode(ISD::ANY_EXTEND, dl,
907 Op.getValueType(),
908 Op.getOperand(0)));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000909
Chris Lattnerec665152006-02-26 23:36:02 +0000910 // Since some of the sign extended bits are demanded, we know that the sign
911 // bit is demanded.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000912 APInt InDemandedBits = InMask & NewMask;
Chris Lattnerec665152006-02-26 23:36:02 +0000913 InDemandedBits |= InSignBit;
Jay Foad40f8f622010-12-07 08:25:19 +0000914 InDemandedBits = InDemandedBits.trunc(InBits);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000915
916 if (SimplifyDemandedBits(Op.getOperand(0), InDemandedBits, KnownZero,
Chris Lattnerec665152006-02-26 23:36:02 +0000917 KnownOne, TLO, Depth+1))
918 return true;
Jay Foad40f8f622010-12-07 08:25:19 +0000919 KnownZero = KnownZero.zext(BitWidth);
920 KnownOne = KnownOne.zext(BitWidth);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000921
Chris Lattnerec665152006-02-26 23:36:02 +0000922 // If the sign bit is known zero, convert this to a zero extend.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000923 if (KnownZero.intersects(InSignBit))
Dale Johannesenff97d4f2009-02-03 00:47:48 +0000924 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ZERO_EXTEND, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000925 Op.getValueType(),
Chris Lattnerec665152006-02-26 23:36:02 +0000926 Op.getOperand(0)));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000927
Chris Lattnerec665152006-02-26 23:36:02 +0000928 // If the sign bit is known one, the top bits match.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000929 if (KnownOne.intersects(InSignBit)) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +0000930 KnownOne |= NewBits;
931 assert((KnownZero & NewBits) == 0);
Chris Lattnerec665152006-02-26 23:36:02 +0000932 } else { // Otherwise, top bits aren't known.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +0000933 assert((KnownOne & NewBits) == 0);
934 assert((KnownZero & NewBits) == 0);
Chris Lattnerec665152006-02-26 23:36:02 +0000935 }
936 break;
937 }
938 case ISD::ANY_EXTEND: {
Dan Gohmand1996362010-01-09 02:13:55 +0000939 unsigned OperandBitWidth =
940 Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
Jay Foad40f8f622010-12-07 08:25:19 +0000941 APInt InMask = NewMask.trunc(OperandBitWidth);
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000942 if (SimplifyDemandedBits(Op.getOperand(0), InMask,
Chris Lattnerec665152006-02-26 23:36:02 +0000943 KnownZero, KnownOne, TLO, Depth+1))
944 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000945 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Jay Foad40f8f622010-12-07 08:25:19 +0000946 KnownZero = KnownZero.zext(BitWidth);
947 KnownOne = KnownOne.zext(BitWidth);
Chris Lattnerec665152006-02-26 23:36:02 +0000948 break;
949 }
Chris Lattnerfe8babf2006-05-05 22:32:12 +0000950 case ISD::TRUNCATE: {
Chris Lattnerc93dfda2006-05-06 00:11:52 +0000951 // Simplify the input, using demanded bit information, and compute the known
952 // zero/one bits live out.
Dan Gohman042919c2010-03-01 17:59:21 +0000953 unsigned OperandBitWidth =
954 Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
Jay Foad40f8f622010-12-07 08:25:19 +0000955 APInt TruncMask = NewMask.zext(OperandBitWidth);
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000956 if (SimplifyDemandedBits(Op.getOperand(0), TruncMask,
Chris Lattnerfe8babf2006-05-05 22:32:12 +0000957 KnownZero, KnownOne, TLO, Depth+1))
958 return true;
Jay Foad40f8f622010-12-07 08:25:19 +0000959 KnownZero = KnownZero.trunc(BitWidth);
960 KnownOne = KnownOne.trunc(BitWidth);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000961
Chris Lattnerc93dfda2006-05-06 00:11:52 +0000962 // If the input is only used by this truncate, see if we can shrink it based
963 // on the known demanded bits.
Gabor Greifba36cb52008-08-28 21:40:38 +0000964 if (Op.getOperand(0).getNode()->hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000965 SDValue In = Op.getOperand(0);
Chris Lattnerc93dfda2006-05-06 00:11:52 +0000966 switch (In.getOpcode()) {
967 default: break;
968 case ISD::SRL:
969 // Shrink SRL by a constant if none of the high bits shifted in are
970 // demanded.
Evan Chenge5b51ac2010-04-17 06:13:15 +0000971 if (TLO.LegalTypes() &&
972 !isTypeDesirableForOp(ISD::SRL, Op.getValueType()))
973 // Do not turn (vt1 truncate (vt2 srl)) into (vt1 srl) if vt1 is
974 // undesirable.
975 break;
976 ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(In.getOperand(1));
977 if (!ShAmt)
978 break;
Owen Anderson7adf8622011-04-13 23:22:23 +0000979 SDValue Shift = In.getOperand(1);
980 if (TLO.LegalTypes()) {
981 uint64_t ShVal = ShAmt->getZExtValue();
982 Shift =
983 TLO.DAG.getConstant(ShVal, getShiftAmountTy(Op.getValueType()));
984 }
985
Evan Chenge5b51ac2010-04-17 06:13:15 +0000986 APInt HighBits = APInt::getHighBitsSet(OperandBitWidth,
987 OperandBitWidth - BitWidth);
Jay Foad40f8f622010-12-07 08:25:19 +0000988 HighBits = HighBits.lshr(ShAmt->getZExtValue()).trunc(BitWidth);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000989
990 if (ShAmt->getZExtValue() < BitWidth && !(HighBits & NewMask)) {
991 // None of the shifted in bits are needed. Add a truncate of the
992 // shift input, then shift it.
993 SDValue NewTrunc = TLO.DAG.getNode(ISD::TRUNCATE, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000994 Op.getValueType(),
Evan Chenge5b51ac2010-04-17 06:13:15 +0000995 In.getOperand(0));
996 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, dl,
997 Op.getValueType(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000998 NewTrunc,
Owen Anderson7adf8622011-04-13 23:22:23 +0000999 Shift));
Chris Lattnerc93dfda2006-05-06 00:11:52 +00001000 }
1001 break;
1002 }
1003 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001004
1005 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Chris Lattnerfe8babf2006-05-05 22:32:12 +00001006 break;
1007 }
Chris Lattnerec665152006-02-26 23:36:02 +00001008 case ISD::AssertZext: {
Owen Anderson7ab15f62011-09-03 00:26:49 +00001009 // AssertZext demands all of the high bits, plus any of the low bits
1010 // demanded by its users.
1011 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1012 APInt InMask = APInt::getLowBitsSet(BitWidth,
1013 VT.getSizeInBits());
1014 if (SimplifyDemandedBits(Op.getOperand(0), ~InMask | NewMask,
Chris Lattnerec665152006-02-26 23:36:02 +00001015 KnownZero, KnownOne, TLO, Depth+1))
1016 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001017 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman400f75c2010-06-03 20:21:33 +00001018
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001019 KnownZero |= ~InMask & NewMask;
Chris Lattnerec665152006-02-26 23:36:02 +00001020 break;
1021 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001022 case ISD::BITCAST:
Stuart Hastings57f1fde2011-06-06 16:44:31 +00001023 // If this is an FP->Int bitcast and if the sign bit is the only
1024 // thing demanded, turn this into a FGETSIGN.
Eli Friedmanca072a32011-12-15 02:07:20 +00001025 if (!TLO.LegalOperations() &&
1026 !Op.getValueType().isVector() &&
Eli Friedman0948f0a2011-11-09 22:25:12 +00001027 !Op.getOperand(0).getValueType().isVector() &&
Nadav Rotem0c3e6782011-06-12 14:56:55 +00001028 NewMask == APInt::getSignBit(Op.getValueType().getSizeInBits()) &&
1029 Op.getOperand(0).getValueType().isFloatingPoint()) {
Stuart Hastings57f1fde2011-06-06 16:44:31 +00001030 bool OpVTLegal = isOperationLegalOrCustom(ISD::FGETSIGN, Op.getValueType());
1031 bool i32Legal = isOperationLegalOrCustom(ISD::FGETSIGN, MVT::i32);
1032 if ((OpVTLegal || i32Legal) && Op.getValueType().isSimple()) {
1033 EVT Ty = OpVTLegal ? Op.getValueType() : MVT::i32;
Chris Lattner2ceb2cf2007-12-22 21:35:38 +00001034 // Make a FGETSIGN + SHL to move the sign bit into the appropriate
1035 // place. We expect the SHL to be eliminated by other optimizations.
Stuart Hastings090bf192011-06-01 18:32:25 +00001036 SDValue Sign = TLO.DAG.getNode(ISD::FGETSIGN, dl, Ty, Op.getOperand(0));
Stuart Hastings57f1fde2011-06-06 16:44:31 +00001037 unsigned OpVTSizeInBits = Op.getValueType().getSizeInBits();
1038 if (!OpVTLegal && OpVTSizeInBits > 32)
Stuart Hastings090bf192011-06-01 18:32:25 +00001039 Sign = TLO.DAG.getNode(ISD::ZERO_EXTEND, dl, Op.getValueType(), Sign);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001040 unsigned ShVal = Op.getValueType().getSizeInBits()-1;
Stuart Hastingsbdce3722011-06-01 14:04:17 +00001041 SDValue ShAmt = TLO.DAG.getConstant(ShVal, Op.getValueType());
Stuart Hastings3dfc4b122011-05-19 18:48:20 +00001042 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SHL, dl,
1043 Op.getValueType(),
Chris Lattner2ceb2cf2007-12-22 21:35:38 +00001044 Sign, ShAmt));
1045 }
1046 }
Chris Lattner2ceb2cf2007-12-22 21:35:38 +00001047 break;
Dan Gohman97121ba2009-04-08 00:15:30 +00001048 case ISD::ADD:
1049 case ISD::MUL:
1050 case ISD::SUB: {
1051 // Add, Sub, and Mul don't demand any bits in positions beyond that
1052 // of the highest bit demanded of them.
1053 APInt LoMask = APInt::getLowBitsSet(BitWidth,
1054 BitWidth - NewMask.countLeadingZeros());
1055 if (SimplifyDemandedBits(Op.getOperand(0), LoMask, KnownZero2,
1056 KnownOne2, TLO, Depth+1))
1057 return true;
1058 if (SimplifyDemandedBits(Op.getOperand(1), LoMask, KnownZero2,
1059 KnownOne2, TLO, Depth+1))
1060 return true;
1061 // See if the operation should be performed at a smaller bit width.
Dan Gohman4e39e9d2010-06-24 14:30:44 +00001062 if (TLO.ShrinkDemandedOp(Op, BitWidth, NewMask, dl))
Dan Gohman97121ba2009-04-08 00:15:30 +00001063 return true;
1064 }
1065 // FALL THROUGH
Dan Gohman54eed372008-05-06 00:53:29 +00001066 default:
Stephen Hinesdce4a402014-05-29 02:49:00 -07001067 // Just use computeKnownBits to compute output bits.
1068 TLO.DAG.computeKnownBits(Op, KnownZero, KnownOne, Depth);
Chris Lattnera6bc5a42006-02-27 01:00:42 +00001069 break;
Nate Begeman368e18d2006-02-16 21:11:51 +00001070 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001071
Chris Lattnerec665152006-02-26 23:36:02 +00001072 // If we know the value of all of the demanded bits, return this as a
1073 // constant.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001074 if ((NewMask & (KnownZero|KnownOne)) == NewMask)
Chris Lattnerec665152006-02-26 23:36:02 +00001075 return TLO.CombineTo(Op, TLO.DAG.getConstant(KnownOne, Op.getValueType()));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001076
Nate Begeman368e18d2006-02-16 21:11:51 +00001077 return false;
1078}
1079
Stephen Hinesdce4a402014-05-29 02:49:00 -07001080/// computeKnownBitsForTargetNode - Determine which of the bits specified
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001081/// in Mask are known to be either zero or one and return them in the
Nate Begeman368e18d2006-02-16 21:11:51 +00001082/// KnownZero/KnownOne bitsets.
Stephen Hinesdce4a402014-05-29 02:49:00 -07001083void TargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
1084 APInt &KnownZero,
1085 APInt &KnownOne,
1086 const SelectionDAG &DAG,
1087 unsigned Depth) const {
Chris Lattner1b5232a2006-04-02 06:19:46 +00001088 assert((Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1089 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1090 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1091 Op.getOpcode() == ISD::INTRINSIC_VOID) &&
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001092 "Should use MaskedValueIsZero if you don't know whether Op"
1093 " is a target node!");
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001094 KnownZero = KnownOne = APInt(KnownOne.getBitWidth(), 0);
Evan Cheng3a03ebb2005-12-21 23:05:39 +00001095}
Chris Lattner4ccb0702006-01-26 20:37:03 +00001096
Chris Lattner5c3e21d2006-05-06 09:27:13 +00001097/// ComputeNumSignBitsForTargetNode - This method can be implemented by
1098/// targets that want to expose additional information about sign bits to the
1099/// DAG Combiner.
Dan Gohman475871a2008-07-27 21:46:04 +00001100unsigned TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
Stephen Hinesdce4a402014-05-29 02:49:00 -07001101 const SelectionDAG &,
Chris Lattner5c3e21d2006-05-06 09:27:13 +00001102 unsigned Depth) const {
1103 assert((Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1104 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1105 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1106 Op.getOpcode() == ISD::INTRINSIC_VOID) &&
1107 "Should use ComputeNumSignBits if you don't know whether Op"
1108 " is a target node!");
1109 return 1;
1110}
1111
Dan Gohman97d11632009-02-15 23:59:32 +00001112/// ValueHasExactlyOneBitSet - Test if the given value is known to have exactly
Stephen Hinesdce4a402014-05-29 02:49:00 -07001113/// one bit set. This differs from computeKnownBits in that it doesn't need to
Dan Gohman97d11632009-02-15 23:59:32 +00001114/// determine which bit is set.
1115///
Dale Johannesen85b0ede2009-02-11 19:19:41 +00001116static bool ValueHasExactlyOneBitSet(SDValue Val, const SelectionDAG &DAG) {
Dan Gohman97d11632009-02-15 23:59:32 +00001117 // A left-shift of a constant one will have exactly one bit set, because
1118 // shifting the bit off the end is undefined.
1119 if (Val.getOpcode() == ISD::SHL)
1120 if (ConstantSDNode *C =
1121 dyn_cast<ConstantSDNode>(Val.getNode()->getOperand(0)))
1122 if (C->getAPIntValue() == 1)
1123 return true;
Dan Gohmane5af2d32009-01-29 01:59:02 +00001124
Dan Gohman97d11632009-02-15 23:59:32 +00001125 // Similarly, a right-shift of a constant sign-bit will have exactly
1126 // one bit set.
1127 if (Val.getOpcode() == ISD::SRL)
1128 if (ConstantSDNode *C =
1129 dyn_cast<ConstantSDNode>(Val.getNode()->getOperand(0)))
1130 if (C->getAPIntValue().isSignBit())
1131 return true;
1132
1133 // More could be done here, though the above checks are enough
1134 // to handle some common cases.
1135
Stephen Hinesdce4a402014-05-29 02:49:00 -07001136 // Fall back to computeKnownBits to catch other known cases.
Owen Andersone50ed302009-08-10 22:56:29 +00001137 EVT OpVT = Val.getValueType();
Dan Gohman5b870af2010-03-02 02:14:38 +00001138 unsigned BitWidth = OpVT.getScalarType().getSizeInBits();
Dan Gohmane5af2d32009-01-29 01:59:02 +00001139 APInt KnownZero, KnownOne;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001140 DAG.computeKnownBits(Val, KnownZero, KnownOne);
Dale Johannesen85b0ede2009-02-11 19:19:41 +00001141 return (KnownZero.countPopulation() == BitWidth - 1) &&
1142 (KnownOne.countPopulation() == 1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00001143}
Chris Lattner5c3e21d2006-05-06 09:27:13 +00001144
Stephen Hines36b56882014-04-23 16:57:46 -07001145bool TargetLowering::isConstTrueVal(const SDNode *N) const {
1146 if (!N)
1147 return false;
1148
1149 bool IsVec = false;
1150 const ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N);
1151 if (!CN) {
1152 const BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N);
1153 if (!BV)
1154 return false;
1155
1156 IsVec = true;
1157 CN = BV->getConstantSplatValue();
1158 }
1159
1160 switch (getBooleanContents(IsVec)) {
1161 case UndefinedBooleanContent:
1162 return CN->getAPIntValue()[0];
1163 case ZeroOrOneBooleanContent:
1164 return CN->isOne();
1165 case ZeroOrNegativeOneBooleanContent:
1166 return CN->isAllOnesValue();
1167 }
1168
1169 llvm_unreachable("Invalid boolean contents");
1170}
1171
1172bool TargetLowering::isConstFalseVal(const SDNode *N) const {
1173 if (!N)
1174 return false;
1175
1176 bool IsVec = false;
1177 const ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N);
1178 if (!CN) {
1179 const BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N);
1180 if (!BV)
1181 return false;
1182
1183 IsVec = true;
1184 CN = BV->getConstantSplatValue();
1185 }
1186
1187 if (getBooleanContents(IsVec) == UndefinedBooleanContent)
1188 return !CN->getAPIntValue()[0];
1189
1190 return CN->isNullValue();
1191}
1192
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001193/// SimplifySetCC - Try to simplify a setcc built with the specified operands
Dan Gohman475871a2008-07-27 21:46:04 +00001194/// and cc. If it is unable to simplify it, return a null SDValue.
1195SDValue
Owen Andersone50ed302009-08-10 22:56:29 +00001196TargetLowering::SimplifySetCC(EVT VT, SDValue N0, SDValue N1,
Evan Chengfa1eb272007-02-08 22:13:59 +00001197 ISD::CondCode Cond, bool foldBooleans,
Andrew Trickac6d9be2013-05-25 02:42:55 +00001198 DAGCombinerInfo &DCI, SDLoc dl) const {
Evan Chengfa1eb272007-02-08 22:13:59 +00001199 SelectionDAG &DAG = DCI.DAG;
1200
1201 // These setcc operations always fold.
1202 switch (Cond) {
1203 default: break;
1204 case ISD::SETFALSE:
1205 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
1206 case ISD::SETTRUE:
Tim Northovera5eeb9d2013-09-06 12:38:12 +00001207 case ISD::SETTRUE2: {
1208 TargetLowering::BooleanContent Cnt = getBooleanContents(VT.isVector());
1209 return DAG.getConstant(
1210 Cnt == TargetLowering::ZeroOrNegativeOneBooleanContent ? -1ULL : 1, VT);
1211 }
Evan Chengfa1eb272007-02-08 22:13:59 +00001212 }
1213
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001214 // Ensure that the constant occurs on the RHS, and fold constant
1215 // comparisons.
Tom Stellard12d43f92013-09-28 02:50:38 +00001216 ISD::CondCode SwappedCC = ISD::getSetCCSwappedOperands(Cond);
1217 if (isa<ConstantSDNode>(N0.getNode()) &&
1218 (DCI.isBeforeLegalizeOps() ||
1219 isCondCodeLegal(SwappedCC, N0.getSimpleValueType())))
1220 return DAG.getSetCC(dl, VT, N1, N0, SwappedCC);
Eric Christopher362fee92011-06-17 20:41:29 +00001221
Gabor Greifba36cb52008-08-28 21:40:38 +00001222 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
Dan Gohman6c6cd1c2008-03-03 22:22:56 +00001223 const APInt &C1 = N1C->getAPIntValue();
Dale Johannesen89217a62008-11-07 01:28:02 +00001224
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001225 // If the LHS is '(srl (ctlz x), 5)', the RHS is 0/1, and this is an
1226 // equality comparison, then we're just comparing whether X itself is
1227 // zero.
1228 if (N0.getOpcode() == ISD::SRL && (C1 == 0 || C1 == 1) &&
1229 N0.getOperand(0).getOpcode() == ISD::CTLZ &&
1230 N0.getOperand(1).getOpcode() == ISD::Constant) {
Evan Cheng347a9cb2010-01-07 20:58:44 +00001231 const APInt &ShAmt
1232 = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001233 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
1234 ShAmt == Log2_32(N0.getValueType().getSizeInBits())) {
1235 if ((C1 == 0) == (Cond == ISD::SETEQ)) {
1236 // (srl (ctlz x), 5) == 0 -> X != 0
1237 // (srl (ctlz x), 5) != 1 -> X != 0
1238 Cond = ISD::SETNE;
1239 } else {
1240 // (srl (ctlz x), 5) != 0 -> X == 0
1241 // (srl (ctlz x), 5) == 1 -> X == 0
1242 Cond = ISD::SETEQ;
1243 }
1244 SDValue Zero = DAG.getConstant(0, N0.getValueType());
1245 return DAG.getSetCC(dl, VT, N0.getOperand(0).getOperand(0),
1246 Zero, Cond);
1247 }
1248 }
1249
Benjamin Kramerd8228922011-01-17 12:04:57 +00001250 SDValue CTPOP = N0;
1251 // Look through truncs that don't change the value of a ctpop.
1252 if (N0.hasOneUse() && N0.getOpcode() == ISD::TRUNCATE)
1253 CTPOP = N0.getOperand(0);
1254
1255 if (CTPOP.hasOneUse() && CTPOP.getOpcode() == ISD::CTPOP &&
Benjamin Kramerc9b6a3e2011-01-17 18:00:28 +00001256 (N0 == CTPOP || N0.getValueType().getSizeInBits() >
Benjamin Kramerd8228922011-01-17 12:04:57 +00001257 Log2_32_Ceil(CTPOP.getValueType().getSizeInBits()))) {
1258 EVT CTVT = CTPOP.getValueType();
1259 SDValue CTOp = CTPOP.getOperand(0);
1260
1261 // (ctpop x) u< 2 -> (x & x-1) == 0
1262 // (ctpop x) u> 1 -> (x & x-1) != 0
1263 if ((Cond == ISD::SETULT && C1 == 2) || (Cond == ISD::SETUGT && C1 == 1)){
1264 SDValue Sub = DAG.getNode(ISD::SUB, dl, CTVT, CTOp,
1265 DAG.getConstant(1, CTVT));
1266 SDValue And = DAG.getNode(ISD::AND, dl, CTVT, CTOp, Sub);
1267 ISD::CondCode CC = Cond == ISD::SETULT ? ISD::SETEQ : ISD::SETNE;
1268 return DAG.getSetCC(dl, VT, And, DAG.getConstant(0, CTVT), CC);
1269 }
1270
Sylvestre Ledru94c22712012-09-27 10:14:43 +00001271 // TODO: (ctpop x) == 1 -> x && (x & x-1) == 0 iff ctpop is illegal.
Benjamin Kramerd8228922011-01-17 12:04:57 +00001272 }
1273
Benjamin Kramere7cf0622011-04-22 18:47:44 +00001274 // (zext x) == C --> x == (trunc C)
1275 if (DCI.isBeforeLegalize() && N0->hasOneUse() &&
1276 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
1277 unsigned MinBits = N0.getValueSizeInBits();
1278 SDValue PreZExt;
1279 if (N0->getOpcode() == ISD::ZERO_EXTEND) {
1280 // ZExt
1281 MinBits = N0->getOperand(0).getValueSizeInBits();
1282 PreZExt = N0->getOperand(0);
1283 } else if (N0->getOpcode() == ISD::AND) {
1284 // DAGCombine turns costly ZExts into ANDs
1285 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0->getOperand(1)))
1286 if ((C->getAPIntValue()+1).isPowerOf2()) {
1287 MinBits = C->getAPIntValue().countTrailingOnes();
1288 PreZExt = N0->getOperand(0);
1289 }
1290 } else if (LoadSDNode *LN0 = dyn_cast<LoadSDNode>(N0)) {
1291 // ZEXTLOAD
1292 if (LN0->getExtensionType() == ISD::ZEXTLOAD) {
1293 MinBits = LN0->getMemoryVT().getSizeInBits();
1294 PreZExt = N0;
1295 }
1296 }
1297
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00001298 // Make sure we're not losing bits from the constant.
Benjamin Kramerf19b8b02013-05-21 08:51:09 +00001299 if (MinBits > 0 &&
1300 MinBits < C1.getBitWidth() && MinBits >= C1.getActiveBits()) {
Benjamin Kramere7cf0622011-04-22 18:47:44 +00001301 EVT MinVT = EVT::getIntegerVT(*DAG.getContext(), MinBits);
1302 if (isTypeDesirableForOp(ISD::SETCC, MinVT)) {
1303 // Will get folded away.
1304 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, MinVT, PreZExt);
1305 SDValue C = DAG.getConstant(C1.trunc(MinBits), MinVT);
1306 return DAG.getSetCC(dl, VT, Trunc, C, Cond);
1307 }
1308 }
1309 }
1310
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001311 // If the LHS is '(and load, const)', the RHS is 0,
1312 // the test is for equality or unsigned, and all 1 bits of the const are
1313 // in the same partial word, see if we can shorten the load.
1314 if (DCI.isBeforeLegalize() &&
Eli Friedman85509802013-09-24 22:50:14 +00001315 !ISD::isSignedIntSetCC(Cond) &&
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001316 N0.getOpcode() == ISD::AND && C1 == 0 &&
1317 N0.getNode()->hasOneUse() &&
1318 isa<LoadSDNode>(N0.getOperand(0)) &&
1319 N0.getOperand(0).getNode()->hasOneUse() &&
1320 isa<ConstantSDNode>(N0.getOperand(1))) {
1321 LoadSDNode *Lod = cast<LoadSDNode>(N0.getOperand(0));
Evan Cheng347a9cb2010-01-07 20:58:44 +00001322 APInt bestMask;
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001323 unsigned bestWidth = 0, bestOffset = 0;
Evan Cheng347a9cb2010-01-07 20:58:44 +00001324 if (!Lod->isVolatile() && Lod->isUnindexed()) {
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001325 unsigned origWidth = N0.getValueType().getSizeInBits();
Evan Cheng347a9cb2010-01-07 20:58:44 +00001326 unsigned maskWidth = origWidth;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001327 // We can narrow (e.g.) 16-bit extending loads on 32-bit target to
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001328 // 8 bits, but have to be careful...
1329 if (Lod->getExtensionType() != ISD::NON_EXTLOAD)
1330 origWidth = Lod->getMemoryVT().getSizeInBits();
Evan Cheng347a9cb2010-01-07 20:58:44 +00001331 const APInt &Mask =
1332 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001333 for (unsigned width = origWidth / 2; width>=8; width /= 2) {
Evan Cheng347a9cb2010-01-07 20:58:44 +00001334 APInt newMask = APInt::getLowBitsSet(maskWidth, width);
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001335 for (unsigned offset=0; offset<origWidth/width; offset++) {
1336 if ((newMask & Mask) == Mask) {
Benjamin Kramer69e42db2013-01-11 20:05:37 +00001337 if (!getDataLayout()->isLittleEndian())
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001338 bestOffset = (origWidth/width - offset - 1) * (width/8);
1339 else
1340 bestOffset = (uint64_t)offset * (width/8);
Evan Cheng347a9cb2010-01-07 20:58:44 +00001341 bestMask = Mask.lshr(offset * (width/8) * 8);
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001342 bestWidth = width;
1343 break;
Dale Johannesen89217a62008-11-07 01:28:02 +00001344 }
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001345 newMask = newMask << width;
Dale Johannesen89217a62008-11-07 01:28:02 +00001346 }
1347 }
1348 }
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001349 if (bestWidth) {
Chris Lattnerc0c7fca2011-04-14 04:12:47 +00001350 EVT newVT = EVT::getIntegerVT(*DAG.getContext(), bestWidth);
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001351 if (newVT.isRound()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001352 EVT PtrType = Lod->getOperand(1).getValueType();
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001353 SDValue Ptr = Lod->getBasePtr();
1354 if (bestOffset != 0)
1355 Ptr = DAG.getNode(ISD::ADD, dl, PtrType, Lod->getBasePtr(),
1356 DAG.getConstant(bestOffset, PtrType));
1357 unsigned NewAlign = MinAlign(Lod->getAlignment(), bestOffset);
1358 SDValue NewLoad = DAG.getLoad(newVT, dl, Lod->getChain(), Ptr,
Chris Lattnerecf42c42010-09-21 16:36:31 +00001359 Lod->getPointerInfo().getWithOffset(bestOffset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001360 false, false, false, NewAlign);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001361 return DAG.getSetCC(dl, VT,
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001362 DAG.getNode(ISD::AND, dl, newVT, NewLoad,
Evan Cheng347a9cb2010-01-07 20:58:44 +00001363 DAG.getConstant(bestMask.trunc(bestWidth),
1364 newVT)),
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001365 DAG.getConstant(0LL, newVT), Cond);
Evan Chengfa1eb272007-02-08 22:13:59 +00001366 }
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001367 }
1368 }
Evan Chengfa1eb272007-02-08 22:13:59 +00001369
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001370 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
1371 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
1372 unsigned InSize = N0.getOperand(0).getValueType().getSizeInBits();
1373
1374 // If the comparison constant has bits in the upper part, the
1375 // zero-extended value could never match.
1376 if (C1.intersects(APInt::getHighBitsSet(C1.getBitWidth(),
1377 C1.getBitWidth() - InSize))) {
Evan Chengfa1eb272007-02-08 22:13:59 +00001378 switch (Cond) {
Evan Chengfa1eb272007-02-08 22:13:59 +00001379 case ISD::SETUGT:
1380 case ISD::SETUGE:
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001381 case ISD::SETEQ: return DAG.getConstant(0, VT);
Evan Chengfa1eb272007-02-08 22:13:59 +00001382 case ISD::SETULT:
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001383 case ISD::SETULE:
1384 case ISD::SETNE: return DAG.getConstant(1, VT);
1385 case ISD::SETGT:
1386 case ISD::SETGE:
1387 // True if the sign bit of C1 is set.
1388 return DAG.getConstant(C1.isNegative(), VT);
1389 case ISD::SETLT:
1390 case ISD::SETLE:
1391 // True if the sign bit of C1 isn't set.
1392 return DAG.getConstant(C1.isNonNegative(), VT);
1393 default:
Jakob Stoklund Olesen78d12642009-07-24 18:22:59 +00001394 break;
1395 }
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001396 }
Evan Chengfa1eb272007-02-08 22:13:59 +00001397
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001398 // Otherwise, we can perform the comparison with the low bits.
1399 switch (Cond) {
1400 case ISD::SETEQ:
1401 case ISD::SETNE:
1402 case ISD::SETUGT:
1403 case ISD::SETUGE:
1404 case ISD::SETULT:
1405 case ISD::SETULE: {
Patrik Hagglund34525f92012-12-11 11:14:33 +00001406 EVT newVT = N0.getOperand(0).getValueType();
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001407 if (DCI.isBeforeLegalizeOps() ||
1408 (isOperationLegal(ISD::SETCC, newVT) &&
Stephen Hinesdce4a402014-05-29 02:49:00 -07001409 getCondCodeAction(Cond, newVT.getSimpleVT()) == Legal)) {
1410 EVT NewSetCCVT = getSetCCResultType(*DAG.getContext(), newVT);
1411 SDValue NewConst = DAG.getConstant(C1.trunc(InSize), newVT);
1412
1413 SDValue NewSetCC = DAG.getSetCC(dl, NewSetCCVT, N0.getOperand(0),
1414 NewConst, Cond);
1415 return DAG.getBoolExtOrTrunc(NewSetCC, dl, VT);
1416 }
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001417 break;
1418 }
1419 default:
1420 break; // todo, be more careful with signed comparisons
1421 }
1422 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Evan Cheng2c755ba2010-02-27 07:36:59 +00001423 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
Owen Andersone50ed302009-08-10 22:56:29 +00001424 EVT ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001425 unsigned ExtSrcTyBits = ExtSrcTy.getSizeInBits();
Owen Andersone50ed302009-08-10 22:56:29 +00001426 EVT ExtDstTy = N0.getValueType();
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001427 unsigned ExtDstTyBits = ExtDstTy.getSizeInBits();
1428
Eli Friedmanad78a882010-07-30 06:44:31 +00001429 // If the constant doesn't fit into the number of bits for the source of
1430 // the sign extension, it is impossible for both sides to be equal.
1431 if (C1.getMinSignedBits() > ExtSrcTyBits)
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001432 return DAG.getConstant(Cond == ISD::SETNE, VT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001433
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001434 SDValue ZextOp;
Owen Andersone50ed302009-08-10 22:56:29 +00001435 EVT Op0Ty = N0.getOperand(0).getValueType();
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001436 if (Op0Ty == ExtSrcTy) {
1437 ZextOp = N0.getOperand(0);
1438 } else {
1439 APInt Imm = APInt::getLowBitsSet(ExtDstTyBits, ExtSrcTyBits);
1440 ZextOp = DAG.getNode(ISD::AND, dl, Op0Ty, N0.getOperand(0),
1441 DAG.getConstant(Imm, Op0Ty));
1442 }
1443 if (!DCI.isCalledByLegalizer())
1444 DCI.AddToWorklist(ZextOp.getNode());
1445 // Otherwise, make this a use of a zext.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001446 return DAG.getSetCC(dl, VT, ZextOp,
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001447 DAG.getConstant(C1 & APInt::getLowBitsSet(
1448 ExtDstTyBits,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001449 ExtSrcTyBits),
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001450 ExtDstTy),
1451 Cond);
1452 } else if ((N1C->isNullValue() || N1C->getAPIntValue() == 1) &&
1453 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001454 // SETCC (SETCC), [0|1], [EQ|NE] -> SETCC
Evan Cheng2c755ba2010-02-27 07:36:59 +00001455 if (N0.getOpcode() == ISD::SETCC &&
1456 isTypeLegal(VT) && VT.bitsLE(N0.getValueType())) {
Evan Cheng347a9cb2010-01-07 20:58:44 +00001457 bool TrueWhenTrue = (Cond == ISD::SETEQ) ^ (N1C->getAPIntValue() != 1);
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001458 if (TrueWhenTrue)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001459 return DAG.getNode(ISD::TRUNCATE, dl, VT, N0);
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001460 // Invert the condition.
1461 ISD::CondCode CC = cast<CondCodeSDNode>(N0.getOperand(2))->get();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001462 CC = ISD::getSetCCInverse(CC,
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001463 N0.getOperand(0).getValueType().isInteger());
Tom Stellard12d43f92013-09-28 02:50:38 +00001464 if (DCI.isBeforeLegalizeOps() ||
1465 isCondCodeLegal(CC, N0.getOperand(0).getSimpleValueType()))
1466 return DAG.getSetCC(dl, VT, N0.getOperand(0), N0.getOperand(1), CC);
Evan Chengfa1eb272007-02-08 22:13:59 +00001467 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00001468
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001469 if ((N0.getOpcode() == ISD::XOR ||
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001470 (N0.getOpcode() == ISD::AND &&
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001471 N0.getOperand(0).getOpcode() == ISD::XOR &&
1472 N0.getOperand(1) == N0.getOperand(0).getOperand(1))) &&
1473 isa<ConstantSDNode>(N0.getOperand(1)) &&
1474 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue() == 1) {
1475 // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We
1476 // can only do this if the top bits are known zero.
1477 unsigned BitWidth = N0.getValueSizeInBits();
1478 if (DAG.MaskedValueIsZero(N0,
1479 APInt::getHighBitsSet(BitWidth,
1480 BitWidth-1))) {
1481 // Okay, get the un-inverted input value.
1482 SDValue Val;
1483 if (N0.getOpcode() == ISD::XOR)
1484 Val = N0.getOperand(0);
1485 else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001486 assert(N0.getOpcode() == ISD::AND &&
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001487 N0.getOperand(0).getOpcode() == ISD::XOR);
1488 // ((X^1)&1)^1 -> X & 1
1489 Val = DAG.getNode(ISD::AND, dl, N0.getValueType(),
1490 N0.getOperand(0).getOperand(0),
1491 N0.getOperand(1));
1492 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00001493
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001494 return DAG.getSetCC(dl, VT, Val, N1,
1495 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
1496 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00001497 } else if (N1C->getAPIntValue() == 1 &&
1498 (VT == MVT::i1 ||
Duncan Sands28b77e92011-09-06 19:07:46 +00001499 getBooleanContents(false) == ZeroOrOneBooleanContent)) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00001500 SDValue Op0 = N0;
1501 if (Op0.getOpcode() == ISD::TRUNCATE)
1502 Op0 = Op0.getOperand(0);
1503
1504 if ((Op0.getOpcode() == ISD::XOR) &&
1505 Op0.getOperand(0).getOpcode() == ISD::SETCC &&
1506 Op0.getOperand(1).getOpcode() == ISD::SETCC) {
1507 // (xor (setcc), (setcc)) == / != 1 -> (setcc) != / == (setcc)
1508 Cond = (Cond == ISD::SETEQ) ? ISD::SETNE : ISD::SETEQ;
1509 return DAG.getSetCC(dl, VT, Op0.getOperand(0), Op0.getOperand(1),
1510 Cond);
Craig Topper40b4a812012-12-19 06:12:28 +00001511 }
1512 if (Op0.getOpcode() == ISD::AND &&
1513 isa<ConstantSDNode>(Op0.getOperand(1)) &&
1514 cast<ConstantSDNode>(Op0.getOperand(1))->getAPIntValue() == 1) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00001515 // If this is (X&1) == / != 1, normalize it to (X&1) != / == 0.
Anton Korobeynikov17458a72010-05-01 12:52:34 +00001516 if (Op0.getValueType().bitsGT(VT))
Evan Cheng2c755ba2010-02-27 07:36:59 +00001517 Op0 = DAG.getNode(ISD::AND, dl, VT,
1518 DAG.getNode(ISD::TRUNCATE, dl, VT, Op0.getOperand(0)),
1519 DAG.getConstant(1, VT));
Anton Korobeynikov17458a72010-05-01 12:52:34 +00001520 else if (Op0.getValueType().bitsLT(VT))
1521 Op0 = DAG.getNode(ISD::AND, dl, VT,
1522 DAG.getNode(ISD::ANY_EXTEND, dl, VT, Op0.getOperand(0)),
1523 DAG.getConstant(1, VT));
1524
Evan Cheng2c755ba2010-02-27 07:36:59 +00001525 return DAG.getSetCC(dl, VT, Op0,
1526 DAG.getConstant(0, Op0.getValueType()),
1527 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
1528 }
Craig Topper40b4a812012-12-19 06:12:28 +00001529 if (Op0.getOpcode() == ISD::AssertZext &&
1530 cast<VTSDNode>(Op0.getOperand(1))->getVT() == MVT::i1)
1531 return DAG.getSetCC(dl, VT, Op0,
1532 DAG.getConstant(0, Op0.getValueType()),
1533 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
Evan Chengfa1eb272007-02-08 22:13:59 +00001534 }
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001535 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001536
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001537 APInt MinVal, MaxVal;
1538 unsigned OperandBitSize = N1C->getValueType(0).getSizeInBits();
1539 if (ISD::isSignedIntSetCC(Cond)) {
1540 MinVal = APInt::getSignedMinValue(OperandBitSize);
1541 MaxVal = APInt::getSignedMaxValue(OperandBitSize);
1542 } else {
1543 MinVal = APInt::getMinValue(OperandBitSize);
1544 MaxVal = APInt::getMaxValue(OperandBitSize);
1545 }
Evan Chengfa1eb272007-02-08 22:13:59 +00001546
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001547 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
1548 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
1549 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
Stephen Hines36b56882014-04-23 16:57:46 -07001550 // X >= C0 --> X > (C0 - 1)
1551 APInt C = C1 - 1;
1552 ISD::CondCode NewCC = (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT;
1553 if ((DCI.isBeforeLegalizeOps() ||
1554 isCondCodeLegal(NewCC, VT.getSimpleVT())) &&
1555 (!N1C->isOpaque() || (N1C->isOpaque() && C.getBitWidth() <= 64 &&
1556 isLegalICmpImmediate(C.getSExtValue())))) {
1557 return DAG.getSetCC(dl, VT, N0,
1558 DAG.getConstant(C, N1.getValueType()),
1559 NewCC);
1560 }
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001561 }
Evan Chengfa1eb272007-02-08 22:13:59 +00001562
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001563 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
1564 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
Stephen Hines36b56882014-04-23 16:57:46 -07001565 // X <= C0 --> X < (C0 + 1)
1566 APInt C = C1 + 1;
1567 ISD::CondCode NewCC = (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT;
1568 if ((DCI.isBeforeLegalizeOps() ||
1569 isCondCodeLegal(NewCC, VT.getSimpleVT())) &&
1570 (!N1C->isOpaque() || (N1C->isOpaque() && C.getBitWidth() <= 64 &&
1571 isLegalICmpImmediate(C.getSExtValue())))) {
1572 return DAG.getSetCC(dl, VT, N0,
1573 DAG.getConstant(C, N1.getValueType()),
1574 NewCC);
1575 }
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001576 }
Evan Chengfa1eb272007-02-08 22:13:59 +00001577
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001578 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
1579 return DAG.getConstant(0, VT); // X < MIN --> false
1580 if ((Cond == ISD::SETGE || Cond == ISD::SETUGE) && C1 == MinVal)
1581 return DAG.getConstant(1, VT); // X >= MIN --> true
1582 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal)
1583 return DAG.getConstant(0, VT); // X > MAX --> false
1584 if ((Cond == ISD::SETLE || Cond == ISD::SETULE) && C1 == MaxVal)
1585 return DAG.getConstant(1, VT); // X <= MAX --> true
Evan Chengfa1eb272007-02-08 22:13:59 +00001586
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001587 // Canonicalize setgt X, Min --> setne X, Min
1588 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
1589 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETNE);
1590 // Canonicalize setlt X, Max --> setne X, Max
1591 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
1592 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETNE);
Evan Chengfa1eb272007-02-08 22:13:59 +00001593
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001594 // If we have setult X, 1, turn it into seteq X, 0
1595 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001596 return DAG.getSetCC(dl, VT, N0,
1597 DAG.getConstant(MinVal, N0.getValueType()),
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001598 ISD::SETEQ);
1599 // If we have setugt X, Max-1, turn it into seteq X, Max
Craig Topper85022562012-12-19 06:43:58 +00001600 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001601 return DAG.getSetCC(dl, VT, N0,
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001602 DAG.getConstant(MaxVal, N0.getValueType()),
1603 ISD::SETEQ);
Evan Chengfa1eb272007-02-08 22:13:59 +00001604
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001605 // If we have "setcc X, C0", check to see if we can shrink the immediate
1606 // by changing cc.
Evan Chengfa1eb272007-02-08 22:13:59 +00001607
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001608 // SETUGT X, SINTMAX -> SETLT X, 0
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001609 if (Cond == ISD::SETUGT &&
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001610 C1 == APInt::getSignedMaxValue(OperandBitSize))
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001611 return DAG.getSetCC(dl, VT, N0,
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001612 DAG.getConstant(0, N1.getValueType()),
1613 ISD::SETLT);
Evan Chengfa1eb272007-02-08 22:13:59 +00001614
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001615 // SETULT X, SINTMIN -> SETGT X, -1
1616 if (Cond == ISD::SETULT &&
1617 C1 == APInt::getSignedMinValue(OperandBitSize)) {
1618 SDValue ConstMinusOne =
1619 DAG.getConstant(APInt::getAllOnesValue(OperandBitSize),
1620 N1.getValueType());
1621 return DAG.getSetCC(dl, VT, N0, ConstMinusOne, ISD::SETGT);
1622 }
Evan Chengfa1eb272007-02-08 22:13:59 +00001623
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001624 // Fold bit comparisons when we can.
1625 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
Evan Chengd40d03e2010-01-06 19:38:29 +00001626 (VT == N0.getValueType() ||
1627 (isTypeLegal(VT) && VT.bitsLE(N0.getValueType()))) &&
1628 N0.getOpcode() == ISD::AND)
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001629 if (ConstantSDNode *AndRHS =
1630 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Stephen Hines36b56882014-04-23 16:57:46 -07001631 EVT ShiftTy = DCI.isBeforeLegalize() ?
Owen Anderson95771af2011-02-25 21:41:48 +00001632 getPointerTy() : getShiftAmountTy(N0.getValueType());
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001633 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
1634 // Perform the xform if the AND RHS is a single bit.
Evan Cheng347a9cb2010-01-07 20:58:44 +00001635 if (AndRHS->getAPIntValue().isPowerOf2()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00001636 return DAG.getNode(ISD::TRUNCATE, dl, VT,
1637 DAG.getNode(ISD::SRL, dl, N0.getValueType(), N0,
Evan Cheng347a9cb2010-01-07 20:58:44 +00001638 DAG.getConstant(AndRHS->getAPIntValue().logBase2(), ShiftTy)));
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001639 }
Evan Cheng347a9cb2010-01-07 20:58:44 +00001640 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getAPIntValue()) {
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001641 // (X & 8) == 8 --> (X & 8) >> 3
1642 // Perform the xform if C1 is a single bit.
1643 if (C1.isPowerOf2()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00001644 return DAG.getNode(ISD::TRUNCATE, dl, VT,
1645 DAG.getNode(ISD::SRL, dl, N0.getValueType(), N0,
1646 DAG.getConstant(C1.logBase2(), ShiftTy)));
Evan Chengfa1eb272007-02-08 22:13:59 +00001647 }
1648 }
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001649 }
Evan Cheng70e10d32012-07-17 06:53:39 +00001650
Evan Chengb4d49592012-07-17 07:47:50 +00001651 if (C1.getMinSignedBits() <= 64 &&
1652 !isLegalICmpImmediate(C1.getSExtValue())) {
Evan Cheng70e10d32012-07-17 06:53:39 +00001653 // (X & -256) == 256 -> (X >> 8) == 1
1654 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
1655 N0.getOpcode() == ISD::AND && N0.hasOneUse()) {
1656 if (ConstantSDNode *AndRHS =
1657 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1658 const APInt &AndRHSC = AndRHS->getAPIntValue();
1659 if ((-AndRHSC).isPowerOf2() && (AndRHSC & C1) == C1) {
1660 unsigned ShiftBits = AndRHSC.countTrailingZeros();
Stephen Hines36b56882014-04-23 16:57:46 -07001661 EVT ShiftTy = DCI.isBeforeLegalize() ?
Evan Cheng70e10d32012-07-17 06:53:39 +00001662 getPointerTy() : getShiftAmountTy(N0.getValueType());
1663 EVT CmpTy = N0.getValueType();
1664 SDValue Shift = DAG.getNode(ISD::SRL, dl, CmpTy, N0.getOperand(0),
1665 DAG.getConstant(ShiftBits, ShiftTy));
1666 SDValue CmpRHS = DAG.getConstant(C1.lshr(ShiftBits), CmpTy);
1667 return DAG.getSetCC(dl, VT, Shift, CmpRHS, Cond);
1668 }
1669 }
Evan Chengf5c05392012-07-17 08:31:11 +00001670 } else if (Cond == ISD::SETULT || Cond == ISD::SETUGE ||
1671 Cond == ISD::SETULE || Cond == ISD::SETUGT) {
1672 bool AdjOne = (Cond == ISD::SETULE || Cond == ISD::SETUGT);
1673 // X < 0x100000000 -> (X >> 32) < 1
1674 // X >= 0x100000000 -> (X >> 32) >= 1
1675 // X <= 0x0ffffffff -> (X >> 32) < 1
1676 // X > 0x0ffffffff -> (X >> 32) >= 1
1677 unsigned ShiftBits;
1678 APInt NewC = C1;
1679 ISD::CondCode NewCond = Cond;
1680 if (AdjOne) {
1681 ShiftBits = C1.countTrailingOnes();
1682 NewC = NewC + 1;
1683 NewCond = (Cond == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
1684 } else {
1685 ShiftBits = C1.countTrailingZeros();
1686 }
1687 NewC = NewC.lshr(ShiftBits);
1688 if (ShiftBits && isLegalICmpImmediate(NewC.getSExtValue())) {
Stephen Hines36b56882014-04-23 16:57:46 -07001689 EVT ShiftTy = DCI.isBeforeLegalize() ?
Evan Chengf5c05392012-07-17 08:31:11 +00001690 getPointerTy() : getShiftAmountTy(N0.getValueType());
1691 EVT CmpTy = N0.getValueType();
1692 SDValue Shift = DAG.getNode(ISD::SRL, dl, CmpTy, N0,
1693 DAG.getConstant(ShiftBits, ShiftTy));
1694 SDValue CmpRHS = DAG.getConstant(NewC, CmpTy);
1695 return DAG.getSetCC(dl, VT, Shift, CmpRHS, NewCond);
1696 }
Evan Cheng70e10d32012-07-17 06:53:39 +00001697 }
1698 }
Evan Chengfa1eb272007-02-08 22:13:59 +00001699 }
1700
Gabor Greifba36cb52008-08-28 21:40:38 +00001701 if (isa<ConstantFPSDNode>(N0.getNode())) {
Evan Chengfa1eb272007-02-08 22:13:59 +00001702 // Constant fold or commute setcc.
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001703 SDValue O = DAG.FoldSetCC(VT, N0, N1, Cond, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00001704 if (O.getNode()) return O;
1705 } else if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
Chris Lattner63079f02007-12-29 08:37:08 +00001706 // If the RHS of an FP comparison is a constant, simplify it away in
1707 // some cases.
1708 if (CFP->getValueAPF().isNaN()) {
1709 // If an operand is known to be a nan, we can fold it.
1710 switch (ISD::getUnorderedFlavor(Cond)) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001711 default: llvm_unreachable("Unknown flavor!");
Chris Lattner63079f02007-12-29 08:37:08 +00001712 case 0: // Known false.
1713 return DAG.getConstant(0, VT);
1714 case 1: // Known true.
1715 return DAG.getConstant(1, VT);
Chris Lattner1c3e1e22007-12-30 21:21:10 +00001716 case 2: // Undefined.
Dale Johannesene8d72302009-02-06 23:05:02 +00001717 return DAG.getUNDEF(VT);
Chris Lattner63079f02007-12-29 08:37:08 +00001718 }
1719 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001720
Chris Lattner63079f02007-12-29 08:37:08 +00001721 // Otherwise, we know the RHS is not a NaN. Simplify the node to drop the
1722 // constant if knowing that the operand is non-nan is enough. We prefer to
1723 // have SETO(x,x) instead of SETO(x, 0.0) because this avoids having to
1724 // materialize 0.0.
1725 if (Cond == ISD::SETO || Cond == ISD::SETUO)
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001726 return DAG.getSetCC(dl, VT, N0, N0, Cond);
Dan Gohman11eab022009-09-26 15:24:17 +00001727
1728 // If the condition is not legal, see if we can find an equivalent one
1729 // which is legal.
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00001730 if (!isCondCodeLegal(Cond, N0.getSimpleValueType())) {
Dan Gohman11eab022009-09-26 15:24:17 +00001731 // If the comparison was an awkward floating-point == or != and one of
1732 // the comparison operands is infinity or negative infinity, convert the
1733 // condition to a less-awkward <= or >=.
1734 if (CFP->getValueAPF().isInfinity()) {
1735 if (CFP->getValueAPF().isNegative()) {
1736 if (Cond == ISD::SETOEQ &&
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00001737 isCondCodeLegal(ISD::SETOLE, N0.getSimpleValueType()))
Dan Gohman11eab022009-09-26 15:24:17 +00001738 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOLE);
1739 if (Cond == ISD::SETUEQ &&
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00001740 isCondCodeLegal(ISD::SETOLE, N0.getSimpleValueType()))
Dan Gohman11eab022009-09-26 15:24:17 +00001741 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETULE);
1742 if (Cond == ISD::SETUNE &&
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00001743 isCondCodeLegal(ISD::SETUGT, N0.getSimpleValueType()))
Dan Gohman11eab022009-09-26 15:24:17 +00001744 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETUGT);
1745 if (Cond == ISD::SETONE &&
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00001746 isCondCodeLegal(ISD::SETUGT, N0.getSimpleValueType()))
Dan Gohman11eab022009-09-26 15:24:17 +00001747 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOGT);
1748 } else {
1749 if (Cond == ISD::SETOEQ &&
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00001750 isCondCodeLegal(ISD::SETOGE, N0.getSimpleValueType()))
Dan Gohman11eab022009-09-26 15:24:17 +00001751 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOGE);
1752 if (Cond == ISD::SETUEQ &&
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00001753 isCondCodeLegal(ISD::SETOGE, N0.getSimpleValueType()))
Dan Gohman11eab022009-09-26 15:24:17 +00001754 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETUGE);
1755 if (Cond == ISD::SETUNE &&
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00001756 isCondCodeLegal(ISD::SETULT, N0.getSimpleValueType()))
Dan Gohman11eab022009-09-26 15:24:17 +00001757 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETULT);
1758 if (Cond == ISD::SETONE &&
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00001759 isCondCodeLegal(ISD::SETULT, N0.getSimpleValueType()))
Dan Gohman11eab022009-09-26 15:24:17 +00001760 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOLT);
1761 }
1762 }
1763 }
Evan Chengfa1eb272007-02-08 22:13:59 +00001764 }
1765
1766 if (N0 == N1) {
Duncan Sandse7de3b22012-07-05 09:32:46 +00001767 // The sext(setcc()) => setcc() optimization relies on the appropriate
1768 // constant being emitted.
Nadav Roteme7576402012-09-06 11:13:55 +00001769 uint64_t EqVal = 0;
Duncan Sandse7de3b22012-07-05 09:32:46 +00001770 switch (getBooleanContents(N0.getValueType().isVector())) {
Duncan Sandse7de3b22012-07-05 09:32:46 +00001771 case UndefinedBooleanContent:
1772 case ZeroOrOneBooleanContent:
1773 EqVal = ISD::isTrueWhenEqual(Cond);
1774 break;
1775 case ZeroOrNegativeOneBooleanContent:
1776 EqVal = ISD::isTrueWhenEqual(Cond) ? -1 : 0;
1777 break;
1778 }
1779
Evan Chengfa1eb272007-02-08 22:13:59 +00001780 // We can always fold X == X for integer setcc's.
Chad Rosier9dbb0182012-04-03 20:11:24 +00001781 if (N0.getValueType().isInteger()) {
Duncan Sandse7de3b22012-07-05 09:32:46 +00001782 return DAG.getConstant(EqVal, VT);
Chad Rosier9dbb0182012-04-03 20:11:24 +00001783 }
Evan Chengfa1eb272007-02-08 22:13:59 +00001784 unsigned UOF = ISD::getUnorderedFlavor(Cond);
1785 if (UOF == 2) // FP operators that are undefined on NaNs.
Duncan Sandse7de3b22012-07-05 09:32:46 +00001786 return DAG.getConstant(EqVal, VT);
Evan Chengfa1eb272007-02-08 22:13:59 +00001787 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
Duncan Sandse7de3b22012-07-05 09:32:46 +00001788 return DAG.getConstant(EqVal, VT);
Evan Chengfa1eb272007-02-08 22:13:59 +00001789 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
1790 // if it is not already.
1791 ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
Micah Villmow8c574be2012-07-31 18:07:43 +00001792 if (NewCond != Cond && (DCI.isBeforeLegalizeOps() ||
Patrik Hagglund9c5ab932012-12-19 10:09:26 +00001793 getCondCodeAction(NewCond, N0.getSimpleValueType()) == Legal))
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001794 return DAG.getSetCC(dl, VT, N0, N1, NewCond);
Evan Chengfa1eb272007-02-08 22:13:59 +00001795 }
1796
1797 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00001798 N0.getValueType().isInteger()) {
Evan Chengfa1eb272007-02-08 22:13:59 +00001799 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
1800 N0.getOpcode() == ISD::XOR) {
1801 // Simplify (X+Y) == (X+Z) --> Y == Z
1802 if (N0.getOpcode() == N1.getOpcode()) {
1803 if (N0.getOperand(0) == N1.getOperand(0))
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001804 return DAG.getSetCC(dl, VT, N0.getOperand(1), N1.getOperand(1), Cond);
Evan Chengfa1eb272007-02-08 22:13:59 +00001805 if (N0.getOperand(1) == N1.getOperand(1))
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001806 return DAG.getSetCC(dl, VT, N0.getOperand(0), N1.getOperand(0), Cond);
Evan Chengfa1eb272007-02-08 22:13:59 +00001807 if (DAG.isCommutativeBinOp(N0.getOpcode())) {
1808 // If X op Y == Y op X, try other combinations.
1809 if (N0.getOperand(0) == N1.getOperand(1))
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001810 return DAG.getSetCC(dl, VT, N0.getOperand(1), N1.getOperand(0),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001811 Cond);
Evan Chengfa1eb272007-02-08 22:13:59 +00001812 if (N0.getOperand(1) == N1.getOperand(0))
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001813 return DAG.getSetCC(dl, VT, N0.getOperand(0), N1.getOperand(1),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001814 Cond);
Evan Chengfa1eb272007-02-08 22:13:59 +00001815 }
1816 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001817
Jakob Stoklund Olesen740cd652012-04-05 20:30:20 +00001818 // If RHS is a legal immediate value for a compare instruction, we need
1819 // to be careful about increasing register pressure needlessly.
1820 bool LegalRHSImm = false;
1821
Evan Chengfa1eb272007-02-08 22:13:59 +00001822 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
1823 if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1824 // Turn (X+C1) == C2 --> X == C2-C1
Gabor Greifba36cb52008-08-28 21:40:38 +00001825 if (N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse()) {
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001826 return DAG.getSetCC(dl, VT, N0.getOperand(0),
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001827 DAG.getConstant(RHSC->getAPIntValue()-
1828 LHSR->getAPIntValue(),
Evan Chengfa1eb272007-02-08 22:13:59 +00001829 N0.getValueType()), Cond);
1830 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001831
Sylvestre Ledru94c22712012-09-27 10:14:43 +00001832 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
Evan Chengfa1eb272007-02-08 22:13:59 +00001833 if (N0.getOpcode() == ISD::XOR)
1834 // If we know that all of the inverted bits are zero, don't bother
1835 // performing the inversion.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001836 if (DAG.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getAPIntValue()))
1837 return
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001838 DAG.getSetCC(dl, VT, N0.getOperand(0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001839 DAG.getConstant(LHSR->getAPIntValue() ^
1840 RHSC->getAPIntValue(),
1841 N0.getValueType()),
1842 Cond);
Evan Chengfa1eb272007-02-08 22:13:59 +00001843 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001844
Evan Chengfa1eb272007-02-08 22:13:59 +00001845 // Turn (C1-X) == C2 --> X == C1-C2
1846 if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001847 if (N0.getOpcode() == ISD::SUB && N0.getNode()->hasOneUse()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001848 return
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001849 DAG.getSetCC(dl, VT, N0.getOperand(1),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001850 DAG.getConstant(SUBC->getAPIntValue() -
1851 RHSC->getAPIntValue(),
1852 N0.getValueType()),
1853 Cond);
Evan Chengfa1eb272007-02-08 22:13:59 +00001854 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001855 }
Jakob Stoklund Olesen740cd652012-04-05 20:30:20 +00001856
1857 // Could RHSC fold directly into a compare?
1858 if (RHSC->getValueType(0).getSizeInBits() <= 64)
1859 LegalRHSImm = isLegalICmpImmediate(RHSC->getSExtValue());
Evan Chengfa1eb272007-02-08 22:13:59 +00001860 }
1861
1862 // Simplify (X+Z) == X --> Z == 0
Jakob Stoklund Olesen740cd652012-04-05 20:30:20 +00001863 // Don't do this if X is an immediate that can fold into a cmp
1864 // instruction and X+Z has other uses. It could be an induction variable
1865 // chain, and the transform would increase register pressure.
1866 if (!LegalRHSImm || N0.getNode()->hasOneUse()) {
1867 if (N0.getOperand(0) == N1)
1868 return DAG.getSetCC(dl, VT, N0.getOperand(1),
1869 DAG.getConstant(0, N0.getValueType()), Cond);
1870 if (N0.getOperand(1) == N1) {
1871 if (DAG.isCommutativeBinOp(N0.getOpcode()))
1872 return DAG.getSetCC(dl, VT, N0.getOperand(0),
1873 DAG.getConstant(0, N0.getValueType()), Cond);
Craig Topper85022562012-12-19 06:43:58 +00001874 if (N0.getNode()->hasOneUse()) {
Jakob Stoklund Olesen740cd652012-04-05 20:30:20 +00001875 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
1876 // (Z-X) == X --> Z == X<<1
1877 SDValue SH = DAG.getNode(ISD::SHL, dl, N1.getValueType(), N1,
Owen Anderson95771af2011-02-25 21:41:48 +00001878 DAG.getConstant(1, getShiftAmountTy(N1.getValueType())));
Jakob Stoklund Olesen740cd652012-04-05 20:30:20 +00001879 if (!DCI.isCalledByLegalizer())
1880 DCI.AddToWorklist(SH.getNode());
1881 return DAG.getSetCC(dl, VT, N0.getOperand(0), SH, Cond);
1882 }
Evan Chengfa1eb272007-02-08 22:13:59 +00001883 }
1884 }
1885 }
1886
1887 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
1888 N1.getOpcode() == ISD::XOR) {
1889 // Simplify X == (X+Z) --> Z == 0
Craig Topper85022562012-12-19 06:43:58 +00001890 if (N1.getOperand(0) == N0)
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001891 return DAG.getSetCC(dl, VT, N1.getOperand(1),
Evan Chengfa1eb272007-02-08 22:13:59 +00001892 DAG.getConstant(0, N1.getValueType()), Cond);
Craig Topper85022562012-12-19 06:43:58 +00001893 if (N1.getOperand(1) == N0) {
1894 if (DAG.isCommutativeBinOp(N1.getOpcode()))
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001895 return DAG.getSetCC(dl, VT, N1.getOperand(0),
Evan Chengfa1eb272007-02-08 22:13:59 +00001896 DAG.getConstant(0, N1.getValueType()), Cond);
Craig Topper85022562012-12-19 06:43:58 +00001897 if (N1.getNode()->hasOneUse()) {
Evan Chengfa1eb272007-02-08 22:13:59 +00001898 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
1899 // X == (Z-X) --> X<<1 == Z
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001900 SDValue SH = DAG.getNode(ISD::SHL, dl, N1.getValueType(), N0,
Owen Anderson95771af2011-02-25 21:41:48 +00001901 DAG.getConstant(1, getShiftAmountTy(N0.getValueType())));
Evan Chengfa1eb272007-02-08 22:13:59 +00001902 if (!DCI.isCalledByLegalizer())
Gabor Greifba36cb52008-08-28 21:40:38 +00001903 DCI.AddToWorklist(SH.getNode());
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001904 return DAG.getSetCC(dl, VT, SH, N1.getOperand(0), Cond);
Evan Chengfa1eb272007-02-08 22:13:59 +00001905 }
1906 }
1907 }
Dan Gohmane5af2d32009-01-29 01:59:02 +00001908
Dan Gohman2c65c3d2009-01-29 16:18:12 +00001909 // Simplify x&y == y to x&y != 0 if y has exactly one bit set.
Dale Johannesen85b0ede2009-02-11 19:19:41 +00001910 // Note that where y is variable and is known to have at most
1911 // one bit set (for example, if it is z&1) we cannot do this;
1912 // the expressions are not equivalent when y==0.
Dan Gohmane5af2d32009-01-29 01:59:02 +00001913 if (N0.getOpcode() == ISD::AND)
1914 if (N0.getOperand(0) == N1 || N0.getOperand(1) == N1) {
Dale Johannesen85b0ede2009-02-11 19:19:41 +00001915 if (ValueHasExactlyOneBitSet(N1, DAG)) {
Dan Gohmane5af2d32009-01-29 01:59:02 +00001916 Cond = ISD::getSetCCInverse(Cond, /*isInteger=*/true);
Tom Stellard12d43f92013-09-28 02:50:38 +00001917 if (DCI.isBeforeLegalizeOps() ||
1918 isCondCodeLegal(Cond, N0.getSimpleValueType())) {
1919 SDValue Zero = DAG.getConstant(0, N1.getValueType());
1920 return DAG.getSetCC(dl, VT, N0, Zero, Cond);
1921 }
Dan Gohmane5af2d32009-01-29 01:59:02 +00001922 }
1923 }
1924 if (N1.getOpcode() == ISD::AND)
1925 if (N1.getOperand(0) == N0 || N1.getOperand(1) == N0) {
Dale Johannesen85b0ede2009-02-11 19:19:41 +00001926 if (ValueHasExactlyOneBitSet(N0, DAG)) {
Dan Gohmane5af2d32009-01-29 01:59:02 +00001927 Cond = ISD::getSetCCInverse(Cond, /*isInteger=*/true);
Tom Stellard12d43f92013-09-28 02:50:38 +00001928 if (DCI.isBeforeLegalizeOps() ||
1929 isCondCodeLegal(Cond, N1.getSimpleValueType())) {
1930 SDValue Zero = DAG.getConstant(0, N0.getValueType());
1931 return DAG.getSetCC(dl, VT, N1, Zero, Cond);
1932 }
Dan Gohmane5af2d32009-01-29 01:59:02 +00001933 }
1934 }
Evan Chengfa1eb272007-02-08 22:13:59 +00001935 }
1936
1937 // Fold away ALL boolean setcc's.
Dan Gohman475871a2008-07-27 21:46:04 +00001938 SDValue Temp;
Owen Anderson825b72b2009-08-11 20:47:22 +00001939 if (N0.getValueType() == MVT::i1 && foldBooleans) {
Evan Chengfa1eb272007-02-08 22:13:59 +00001940 switch (Cond) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001941 default: llvm_unreachable("Unknown integer setcc!");
Bob Wilson4c245462009-01-22 17:39:32 +00001942 case ISD::SETEQ: // X == Y -> ~(X^Y)
Owen Anderson825b72b2009-08-11 20:47:22 +00001943 Temp = DAG.getNode(ISD::XOR, dl, MVT::i1, N0, N1);
1944 N0 = DAG.getNOT(dl, Temp, MVT::i1);
Evan Chengfa1eb272007-02-08 22:13:59 +00001945 if (!DCI.isCalledByLegalizer())
Gabor Greifba36cb52008-08-28 21:40:38 +00001946 DCI.AddToWorklist(Temp.getNode());
Evan Chengfa1eb272007-02-08 22:13:59 +00001947 break;
1948 case ISD::SETNE: // X != Y --> (X^Y)
Owen Anderson825b72b2009-08-11 20:47:22 +00001949 N0 = DAG.getNode(ISD::XOR, dl, MVT::i1, N0, N1);
Evan Chengfa1eb272007-02-08 22:13:59 +00001950 break;
Bob Wilson4c245462009-01-22 17:39:32 +00001951 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> ~X & Y
1952 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> ~X & Y
Owen Anderson825b72b2009-08-11 20:47:22 +00001953 Temp = DAG.getNOT(dl, N0, MVT::i1);
1954 N0 = DAG.getNode(ISD::AND, dl, MVT::i1, N1, Temp);
Evan Chengfa1eb272007-02-08 22:13:59 +00001955 if (!DCI.isCalledByLegalizer())
Gabor Greifba36cb52008-08-28 21:40:38 +00001956 DCI.AddToWorklist(Temp.getNode());
Evan Chengfa1eb272007-02-08 22:13:59 +00001957 break;
Bob Wilson4c245462009-01-22 17:39:32 +00001958 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> ~Y & X
1959 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> ~Y & X
Owen Anderson825b72b2009-08-11 20:47:22 +00001960 Temp = DAG.getNOT(dl, N1, MVT::i1);
1961 N0 = DAG.getNode(ISD::AND, dl, MVT::i1, N0, Temp);
Evan Chengfa1eb272007-02-08 22:13:59 +00001962 if (!DCI.isCalledByLegalizer())
Gabor Greifba36cb52008-08-28 21:40:38 +00001963 DCI.AddToWorklist(Temp.getNode());
Evan Chengfa1eb272007-02-08 22:13:59 +00001964 break;
Bob Wilson4c245462009-01-22 17:39:32 +00001965 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> ~X | Y
1966 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> ~X | Y
Owen Anderson825b72b2009-08-11 20:47:22 +00001967 Temp = DAG.getNOT(dl, N0, MVT::i1);
1968 N0 = DAG.getNode(ISD::OR, dl, MVT::i1, N1, Temp);
Evan Chengfa1eb272007-02-08 22:13:59 +00001969 if (!DCI.isCalledByLegalizer())
Gabor Greifba36cb52008-08-28 21:40:38 +00001970 DCI.AddToWorklist(Temp.getNode());
Evan Chengfa1eb272007-02-08 22:13:59 +00001971 break;
Bob Wilson4c245462009-01-22 17:39:32 +00001972 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> ~Y | X
1973 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> ~Y | X
Owen Anderson825b72b2009-08-11 20:47:22 +00001974 Temp = DAG.getNOT(dl, N1, MVT::i1);
1975 N0 = DAG.getNode(ISD::OR, dl, MVT::i1, N0, Temp);
Evan Chengfa1eb272007-02-08 22:13:59 +00001976 break;
1977 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001978 if (VT != MVT::i1) {
Evan Chengfa1eb272007-02-08 22:13:59 +00001979 if (!DCI.isCalledByLegalizer())
Gabor Greifba36cb52008-08-28 21:40:38 +00001980 DCI.AddToWorklist(N0.getNode());
Evan Chengfa1eb272007-02-08 22:13:59 +00001981 // FIXME: If running after legalize, we probably can't do this.
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001982 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, N0);
Evan Chengfa1eb272007-02-08 22:13:59 +00001983 }
1984 return N0;
1985 }
1986
1987 // Could not fold it.
Dan Gohman475871a2008-07-27 21:46:04 +00001988 return SDValue();
Evan Chengfa1eb272007-02-08 22:13:59 +00001989}
1990
Evan Chengad4196b2008-05-12 19:56:52 +00001991/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
1992/// node is a GlobalAddress + offset.
Chris Lattner0a9481f2011-02-13 22:25:43 +00001993bool TargetLowering::isGAPlusOffset(SDNode *N, const GlobalValue *&GA,
Evan Chengad4196b2008-05-12 19:56:52 +00001994 int64_t &Offset) const {
1995 if (isa<GlobalAddressSDNode>(N)) {
Dan Gohman9ea3f562008-06-09 22:05:52 +00001996 GlobalAddressSDNode *GASD = cast<GlobalAddressSDNode>(N);
1997 GA = GASD->getGlobal();
1998 Offset += GASD->getOffset();
Evan Chengad4196b2008-05-12 19:56:52 +00001999 return true;
2000 }
2001
2002 if (N->getOpcode() == ISD::ADD) {
Dan Gohman475871a2008-07-27 21:46:04 +00002003 SDValue N1 = N->getOperand(0);
2004 SDValue N2 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002005 if (isGAPlusOffset(N1.getNode(), GA, Offset)) {
Evan Chengad4196b2008-05-12 19:56:52 +00002006 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
2007 if (V) {
Dan Gohman7810bfe2008-09-26 21:54:37 +00002008 Offset += V->getSExtValue();
Evan Chengad4196b2008-05-12 19:56:52 +00002009 return true;
2010 }
Gabor Greifba36cb52008-08-28 21:40:38 +00002011 } else if (isGAPlusOffset(N2.getNode(), GA, Offset)) {
Evan Chengad4196b2008-05-12 19:56:52 +00002012 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
2013 if (V) {
Dan Gohman7810bfe2008-09-26 21:54:37 +00002014 Offset += V->getSExtValue();
Evan Chengad4196b2008-05-12 19:56:52 +00002015 return true;
2016 }
2017 }
2018 }
Owen Anderson95771af2011-02-25 21:41:48 +00002019
Evan Chengad4196b2008-05-12 19:56:52 +00002020 return false;
2021}
2022
2023
Dan Gohman475871a2008-07-27 21:46:04 +00002024SDValue TargetLowering::
Chris Lattner00ffed02006-03-01 04:52:55 +00002025PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
2026 // Default implementation: no optimization.
Dan Gohman475871a2008-07-27 21:46:04 +00002027 return SDValue();
Chris Lattner00ffed02006-03-01 04:52:55 +00002028}
2029
Chris Lattnereb8146b2006-02-04 02:13:02 +00002030//===----------------------------------------------------------------------===//
2031// Inline Assembler Implementation Methods
2032//===----------------------------------------------------------------------===//
2033
Chris Lattner4376fea2008-04-27 00:09:47 +00002034
Chris Lattnereb8146b2006-02-04 02:13:02 +00002035TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00002036TargetLowering::getConstraintType(const std::string &Constraint) const {
Eric Christopherfffe3632013-01-11 18:12:39 +00002037 unsigned S = Constraint.size();
2038
2039 if (S == 1) {
Chris Lattner4234f572007-03-25 02:14:49 +00002040 switch (Constraint[0]) {
2041 default: break;
2042 case 'r': return C_RegisterClass;
2043 case 'm': // memory
2044 case 'o': // offsetable
2045 case 'V': // not offsetable
2046 return C_Memory;
2047 case 'i': // Simple Integer or Relocatable Constant
2048 case 'n': // Simple Integer
John Thompson67aff162010-09-21 22:04:54 +00002049 case 'E': // Floating Point Constant
2050 case 'F': // Floating Point Constant
Chris Lattner4234f572007-03-25 02:14:49 +00002051 case 's': // Relocatable Constant
John Thompson67aff162010-09-21 22:04:54 +00002052 case 'p': // Address.
Chris Lattnerc13dd1c2007-03-25 04:35:41 +00002053 case 'X': // Allow ANY value.
Chris Lattner4234f572007-03-25 02:14:49 +00002054 case 'I': // Target registers.
2055 case 'J':
2056 case 'K':
2057 case 'L':
2058 case 'M':
2059 case 'N':
2060 case 'O':
2061 case 'P':
John Thompson67aff162010-09-21 22:04:54 +00002062 case '<':
2063 case '>':
Chris Lattner4234f572007-03-25 02:14:49 +00002064 return C_Other;
2065 }
Chris Lattnereb8146b2006-02-04 02:13:02 +00002066 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002067
Eric Christopherfffe3632013-01-11 18:12:39 +00002068 if (S > 1 && Constraint[0] == '{' && Constraint[S-1] == '}') {
2069 if (S == 8 && !Constraint.compare(1, 6, "memory", 6)) // "{memory}"
2070 return C_Memory;
Chris Lattner065421f2007-03-25 02:18:14 +00002071 return C_Register;
Eric Christopherfffe3632013-01-11 18:12:39 +00002072 }
Chris Lattner4234f572007-03-25 02:14:49 +00002073 return C_Unknown;
Chris Lattnereb8146b2006-02-04 02:13:02 +00002074}
2075
Dale Johannesenba2a0b92008-01-29 02:21:21 +00002076/// LowerXConstraint - try to replace an X constraint, which matches anything,
2077/// with another that has more specific requirements based on the type of the
2078/// corresponding operand.
Owen Andersone50ed302009-08-10 22:56:29 +00002079const char *TargetLowering::LowerXConstraint(EVT ConstraintVT) const{
Duncan Sands83ec4b62008-06-06 12:08:01 +00002080 if (ConstraintVT.isInteger())
Chris Lattner5e764232008-04-26 23:02:14 +00002081 return "r";
Duncan Sands83ec4b62008-06-06 12:08:01 +00002082 if (ConstraintVT.isFloatingPoint())
Chris Lattner5e764232008-04-26 23:02:14 +00002083 return "f"; // works for many targets
Stephen Hinesdce4a402014-05-29 02:49:00 -07002084 return nullptr;
Dale Johannesenba2a0b92008-01-29 02:21:21 +00002085}
2086
Chris Lattner48884cd2007-08-25 00:47:38 +00002087/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
2088/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +00002089void TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +00002090 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +00002091 std::vector<SDValue> &Ops,
Chris Lattner5e764232008-04-26 23:02:14 +00002092 SelectionDAG &DAG) const {
Eric Christopher362fee92011-06-17 20:41:29 +00002093
Eric Christopher100c8332011-06-02 23:16:42 +00002094 if (Constraint.length() > 1) return;
Eric Christopher362fee92011-06-17 20:41:29 +00002095
Eric Christopher100c8332011-06-02 23:16:42 +00002096 char ConstraintLetter = Constraint[0];
Chris Lattnereb8146b2006-02-04 02:13:02 +00002097 switch (ConstraintLetter) {
Chris Lattner9ff6ee82007-02-17 06:00:35 +00002098 default: break;
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002099 case 'X': // Allows any operand; labels (basic block) use this.
2100 if (Op.getOpcode() == ISD::BasicBlock) {
2101 Ops.push_back(Op);
2102 return;
2103 }
2104 // fall through
Chris Lattnereb8146b2006-02-04 02:13:02 +00002105 case 'i': // Simple Integer or Relocatable Constant
2106 case 'n': // Simple Integer
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002107 case 's': { // Relocatable Constant
Chris Lattner75c7d2b2007-05-03 16:54:34 +00002108 // These operands are interested in values of the form (GV+C), where C may
2109 // be folded in as an offset of GV, or it may be explicitly added. Also, it
2110 // is possible and fine if either GV or C are missing.
2111 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
2112 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002113
Chris Lattner75c7d2b2007-05-03 16:54:34 +00002114 // If we have "(add GV, C)", pull out GV/C
2115 if (Op.getOpcode() == ISD::ADD) {
2116 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
2117 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
Stephen Hinesdce4a402014-05-29 02:49:00 -07002118 if (!C || !GA) {
Chris Lattner75c7d2b2007-05-03 16:54:34 +00002119 C = dyn_cast<ConstantSDNode>(Op.getOperand(0));
2120 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(1));
2121 }
Stephen Hinesdce4a402014-05-29 02:49:00 -07002122 if (!C || !GA)
2123 C = nullptr, GA = nullptr;
Chris Lattner75c7d2b2007-05-03 16:54:34 +00002124 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002125
Chris Lattner75c7d2b2007-05-03 16:54:34 +00002126 // If we find a valid operand, map to the TargetXXX version so that the
2127 // value itself doesn't get selected.
2128 if (GA) { // Either &GV or &GV+C
2129 if (ConstraintLetter != 'n') {
2130 int64_t Offs = GA->getOffset();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002131 if (C) Offs += C->getZExtValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002132 Ops.push_back(DAG.getTargetGlobalAddress(GA->getGlobal(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00002133 C ? SDLoc(C) : SDLoc(),
Chris Lattner48884cd2007-08-25 00:47:38 +00002134 Op.getValueType(), Offs));
2135 return;
Chris Lattner75c7d2b2007-05-03 16:54:34 +00002136 }
2137 }
2138 if (C) { // just C, no GV.
Chris Lattner9ff6ee82007-02-17 06:00:35 +00002139 // Simple constants are not allowed for 's'.
Chris Lattner48884cd2007-08-25 00:47:38 +00002140 if (ConstraintLetter != 's') {
Dale Johannesen78e3e522009-02-12 20:58:09 +00002141 // gcc prints these as sign extended. Sign extend value to 64 bits
2142 // now; without this it would get ZExt'd later in
2143 // ScheduleDAGSDNodes::EmitNode, which is very generic.
2144 Ops.push_back(DAG.getTargetConstant(C->getAPIntValue().getSExtValue(),
Owen Anderson825b72b2009-08-11 20:47:22 +00002145 MVT::i64));
Chris Lattner48884cd2007-08-25 00:47:38 +00002146 return;
2147 }
Chris Lattner9ff6ee82007-02-17 06:00:35 +00002148 }
Chris Lattner9ff6ee82007-02-17 06:00:35 +00002149 break;
Chris Lattnereb8146b2006-02-04 02:13:02 +00002150 }
Chris Lattner75c7d2b2007-05-03 16:54:34 +00002151 }
Chris Lattnereb8146b2006-02-04 02:13:02 +00002152}
2153
Chris Lattner1efa40f2006-02-22 00:56:39 +00002154std::pair<unsigned, const TargetRegisterClass*> TargetLowering::
Chris Lattner4217ca8dc2006-02-21 23:11:00 +00002155getRegForInlineAsmConstraint(const std::string &Constraint,
Chad Rosier5b3fca52013-06-22 18:37:38 +00002156 MVT VT) const {
Will Dietz833a29c2013-10-13 03:08:49 +00002157 if (Constraint.empty() || Constraint[0] != '{')
Stephen Hinesdce4a402014-05-29 02:49:00 -07002158 return std::make_pair(0u, static_cast<TargetRegisterClass*>(nullptr));
Chris Lattnera55079a2006-02-01 01:29:47 +00002159 assert(*(Constraint.end()-1) == '}' && "Not a brace enclosed constraint?");
2160
2161 // Remove the braces from around the name.
Benjamin Kramer05872ea2009-11-12 20:36:59 +00002162 StringRef RegName(Constraint.data()+1, Constraint.size()-2);
Chris Lattner1efa40f2006-02-22 00:56:39 +00002163
Hal Finkelca2dd362012-12-18 17:50:58 +00002164 std::pair<unsigned, const TargetRegisterClass*> R =
Stephen Hinesdce4a402014-05-29 02:49:00 -07002165 std::make_pair(0u, static_cast<const TargetRegisterClass*>(nullptr));
Hal Finkelca2dd362012-12-18 17:50:58 +00002166
Chris Lattner1efa40f2006-02-22 00:56:39 +00002167 // Figure out which register class contains this reg.
Benjamin Kramer69e42db2013-01-11 20:05:37 +00002168 const TargetRegisterInfo *RI = getTargetMachine().getRegisterInfo();
Dan Gohman6f0d0242008-02-10 18:45:23 +00002169 for (TargetRegisterInfo::regclass_iterator RCI = RI->regclass_begin(),
Chris Lattner1efa40f2006-02-22 00:56:39 +00002170 E = RI->regclass_end(); RCI != E; ++RCI) {
2171 const TargetRegisterClass *RC = *RCI;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002172
2173 // If none of the value types for this register class are valid, we
Chris Lattnerb3befd42006-02-22 23:00:51 +00002174 // can't use it. For example, 64-bit reg classes on 32-bit targets.
Jakob Stoklund Olesen22e8a362011-10-12 01:24:51 +00002175 if (!isLegalRC(RC))
2176 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002177
2178 for (TargetRegisterClass::iterator I = RC->begin(), E = RC->end();
Chris Lattner1efa40f2006-02-22 00:56:39 +00002179 I != E; ++I) {
Hal Finkelca2dd362012-12-18 17:50:58 +00002180 if (RegName.equals_lower(RI->getName(*I))) {
2181 std::pair<unsigned, const TargetRegisterClass*> S =
2182 std::make_pair(*I, RC);
2183
2184 // If this register class has the requested value type, return it,
2185 // otherwise keep searching and return the first class found
2186 // if no other is found which explicitly has the requested type.
2187 if (RC->hasType(VT))
2188 return S;
2189 else if (!R.second)
2190 R = S;
2191 }
Chris Lattner1efa40f2006-02-22 00:56:39 +00002192 }
Chris Lattner4ccb0702006-01-26 20:37:03 +00002193 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002194
Hal Finkelca2dd362012-12-18 17:50:58 +00002195 return R;
Chris Lattner4ccb0702006-01-26 20:37:03 +00002196}
Evan Cheng30b37b52006-03-13 23:18:16 +00002197
2198//===----------------------------------------------------------------------===//
Chris Lattner4376fea2008-04-27 00:09:47 +00002199// Constraint Selection.
2200
Chris Lattner6bdcda32008-10-17 16:47:46 +00002201/// isMatchingInputConstraint - Return true of this is an input operand that is
2202/// a matching constraint like "4".
2203bool TargetLowering::AsmOperandInfo::isMatchingInputConstraint() const {
Chris Lattner58f15c42008-10-17 16:21:11 +00002204 assert(!ConstraintCode.empty() && "No known constraint!");
Guy Benyei87d0b9e2013-02-12 21:21:59 +00002205 return isdigit(static_cast<unsigned char>(ConstraintCode[0]));
Chris Lattner58f15c42008-10-17 16:21:11 +00002206}
2207
2208/// getMatchedOperand - If this is an input matching constraint, this method
2209/// returns the output operand it matches.
2210unsigned TargetLowering::AsmOperandInfo::getMatchedOperand() const {
2211 assert(!ConstraintCode.empty() && "No known constraint!");
2212 return atoi(ConstraintCode.c_str());
2213}
2214
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002215
John Thompsoneac6e1d2010-09-13 18:15:37 +00002216/// ParseConstraints - Split up the constraint string from the inline
2217/// assembly value into the specific constraints and their prefixes,
2218/// and also tie in the associated operand values.
2219/// If this returns an empty vector, and if the constraint string itself
2220/// isn't empty, there was an error parsing.
John Thompson44ab89e2010-10-29 17:29:13 +00002221TargetLowering::AsmOperandInfoVector TargetLowering::ParseConstraints(
John Thompsoneac6e1d2010-09-13 18:15:37 +00002222 ImmutableCallSite CS) const {
2223 /// ConstraintOperands - Information about all of the constraints.
John Thompson44ab89e2010-10-29 17:29:13 +00002224 AsmOperandInfoVector ConstraintOperands;
John Thompsoneac6e1d2010-09-13 18:15:37 +00002225 const InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
John Thompson67aff162010-09-21 22:04:54 +00002226 unsigned maCount = 0; // Largest number of multiple alternative constraints.
John Thompsoneac6e1d2010-09-13 18:15:37 +00002227
2228 // Do a prepass over the constraints, canonicalizing them, and building up the
2229 // ConstraintOperands list.
John Thompson44ab89e2010-10-29 17:29:13 +00002230 InlineAsm::ConstraintInfoVector
John Thompsoneac6e1d2010-09-13 18:15:37 +00002231 ConstraintInfos = IA->ParseConstraints();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002232
John Thompsoneac6e1d2010-09-13 18:15:37 +00002233 unsigned ArgNo = 0; // ArgNo - The argument of the CallInst.
2234 unsigned ResNo = 0; // ResNo - The result number of the next output.
2235
2236 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
2237 ConstraintOperands.push_back(AsmOperandInfo(ConstraintInfos[i]));
2238 AsmOperandInfo &OpInfo = ConstraintOperands.back();
2239
John Thompson67aff162010-09-21 22:04:54 +00002240 // Update multiple alternative constraint count.
2241 if (OpInfo.multipleAlternatives.size() > maCount)
2242 maCount = OpInfo.multipleAlternatives.size();
2243
John Thompson44ab89e2010-10-29 17:29:13 +00002244 OpInfo.ConstraintVT = MVT::Other;
John Thompsoneac6e1d2010-09-13 18:15:37 +00002245
2246 // Compute the value type for each operand.
2247 switch (OpInfo.Type) {
2248 case InlineAsm::isOutput:
2249 // Indirect outputs just consume an argument.
2250 if (OpInfo.isIndirect) {
2251 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
2252 break;
2253 }
2254
2255 // The return value of the call is this value. As such, there is no
2256 // corresponding argument.
2257 assert(!CS.getType()->isVoidTy() &&
2258 "Bad inline asm!");
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002259 if (StructType *STy = dyn_cast<StructType>(CS.getType())) {
Patrik Hagglundc698d3a2012-12-19 15:19:11 +00002260 OpInfo.ConstraintVT = getSimpleValueType(STy->getElementType(ResNo));
John Thompsoneac6e1d2010-09-13 18:15:37 +00002261 } else {
2262 assert(ResNo == 0 && "Asm only has one result!");
Patrik Hagglundc698d3a2012-12-19 15:19:11 +00002263 OpInfo.ConstraintVT = getSimpleValueType(CS.getType());
John Thompsoneac6e1d2010-09-13 18:15:37 +00002264 }
2265 ++ResNo;
2266 break;
2267 case InlineAsm::isInput:
2268 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
2269 break;
2270 case InlineAsm::isClobber:
2271 // Nothing to do.
2272 break;
2273 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002274
John Thompson44ab89e2010-10-29 17:29:13 +00002275 if (OpInfo.CallOperandVal) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002276 llvm::Type *OpTy = OpInfo.CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00002277 if (OpInfo.isIndirect) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002278 llvm::PointerType *PtrTy = dyn_cast<PointerType>(OpTy);
John Thompson44ab89e2010-10-29 17:29:13 +00002279 if (!PtrTy)
2280 report_fatal_error("Indirect operand for inline asm not a pointer!");
2281 OpTy = PtrTy->getElementType();
2282 }
Eric Christopher362fee92011-06-17 20:41:29 +00002283
Eric Christophercef81b72011-05-09 20:04:43 +00002284 // Look for vector wrapped in a struct. e.g. { <16 x i8> }.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002285 if (StructType *STy = dyn_cast<StructType>(OpTy))
Eric Christophercef81b72011-05-09 20:04:43 +00002286 if (STy->getNumElements() == 1)
2287 OpTy = STy->getElementType(0);
2288
John Thompson44ab89e2010-10-29 17:29:13 +00002289 // If OpTy is not a single value, it may be a struct/union that we
2290 // can tile with integers.
2291 if (!OpTy->isSingleValueType() && OpTy->isSized()) {
Benjamin Kramer69e42db2013-01-11 20:05:37 +00002292 unsigned BitSize = getDataLayout()->getTypeSizeInBits(OpTy);
John Thompson44ab89e2010-10-29 17:29:13 +00002293 switch (BitSize) {
2294 default: break;
2295 case 1:
2296 case 8:
2297 case 16:
2298 case 32:
2299 case 64:
2300 case 128:
Dale Johannesen71365d32010-11-09 01:15:07 +00002301 OpInfo.ConstraintVT =
Patrik Hagglundc698d3a2012-12-19 15:19:11 +00002302 MVT::getVT(IntegerType::get(OpTy->getContext(), BitSize), true);
John Thompson44ab89e2010-10-29 17:29:13 +00002303 break;
2304 }
Micah Villmow7d661462012-10-09 16:06:12 +00002305 } else if (PointerType *PT = dyn_cast<PointerType>(OpTy)) {
Matt Arsenault828c9e72013-10-10 19:09:05 +00002306 unsigned PtrSize
2307 = getDataLayout()->getPointerSizeInBits(PT->getAddressSpace());
2308 OpInfo.ConstraintVT = MVT::getIntegerVT(PtrSize);
John Thompson44ab89e2010-10-29 17:29:13 +00002309 } else {
Patrik Hagglundc698d3a2012-12-19 15:19:11 +00002310 OpInfo.ConstraintVT = MVT::getVT(OpTy, true);
John Thompson44ab89e2010-10-29 17:29:13 +00002311 }
2312 }
John Thompsoneac6e1d2010-09-13 18:15:37 +00002313 }
2314
2315 // If we have multiple alternative constraints, select the best alternative.
2316 if (ConstraintInfos.size()) {
John Thompsoneac6e1d2010-09-13 18:15:37 +00002317 if (maCount) {
2318 unsigned bestMAIndex = 0;
2319 int bestWeight = -1;
2320 // weight: -1 = invalid match, and 0 = so-so match to 5 = good match.
2321 int weight = -1;
2322 unsigned maIndex;
2323 // Compute the sums of the weights for each alternative, keeping track
2324 // of the best (highest weight) one so far.
2325 for (maIndex = 0; maIndex < maCount; ++maIndex) {
2326 int weightSum = 0;
2327 for (unsigned cIndex = 0, eIndex = ConstraintOperands.size();
2328 cIndex != eIndex; ++cIndex) {
2329 AsmOperandInfo& OpInfo = ConstraintOperands[cIndex];
2330 if (OpInfo.Type == InlineAsm::isClobber)
2331 continue;
John Thompsoneac6e1d2010-09-13 18:15:37 +00002332
John Thompson44ab89e2010-10-29 17:29:13 +00002333 // If this is an output operand with a matching input operand,
2334 // look up the matching input. If their types mismatch, e.g. one
2335 // is an integer, the other is floating point, or their sizes are
2336 // different, flag it as an maCantMatch.
John Thompsoneac6e1d2010-09-13 18:15:37 +00002337 if (OpInfo.hasMatchingInput()) {
2338 AsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
John Thompsoneac6e1d2010-09-13 18:15:37 +00002339 if (OpInfo.ConstraintVT != Input.ConstraintVT) {
2340 if ((OpInfo.ConstraintVT.isInteger() !=
2341 Input.ConstraintVT.isInteger()) ||
2342 (OpInfo.ConstraintVT.getSizeInBits() !=
2343 Input.ConstraintVT.getSizeInBits())) {
2344 weightSum = -1; // Can't match.
2345 break;
2346 }
John Thompsoneac6e1d2010-09-13 18:15:37 +00002347 }
2348 }
John Thompsoneac6e1d2010-09-13 18:15:37 +00002349 weight = getMultipleConstraintMatchWeight(OpInfo, maIndex);
2350 if (weight == -1) {
2351 weightSum = -1;
2352 break;
2353 }
2354 weightSum += weight;
2355 }
2356 // Update best.
2357 if (weightSum > bestWeight) {
2358 bestWeight = weightSum;
2359 bestMAIndex = maIndex;
2360 }
2361 }
2362
2363 // Now select chosen alternative in each constraint.
2364 for (unsigned cIndex = 0, eIndex = ConstraintOperands.size();
2365 cIndex != eIndex; ++cIndex) {
2366 AsmOperandInfo& cInfo = ConstraintOperands[cIndex];
2367 if (cInfo.Type == InlineAsm::isClobber)
2368 continue;
2369 cInfo.selectAlternative(bestMAIndex);
2370 }
2371 }
2372 }
2373
2374 // Check and hook up tied operands, choose constraint code to use.
2375 for (unsigned cIndex = 0, eIndex = ConstraintOperands.size();
2376 cIndex != eIndex; ++cIndex) {
2377 AsmOperandInfo& OpInfo = ConstraintOperands[cIndex];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002378
John Thompsoneac6e1d2010-09-13 18:15:37 +00002379 // If this is an output operand with a matching input operand, look up the
2380 // matching input. If their types mismatch, e.g. one is an integer, the
2381 // other is floating point, or their sizes are different, flag it as an
2382 // error.
2383 if (OpInfo.hasMatchingInput()) {
2384 AsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
John Thompson44ab89e2010-10-29 17:29:13 +00002385
John Thompsoneac6e1d2010-09-13 18:15:37 +00002386 if (OpInfo.ConstraintVT != Input.ConstraintVT) {
Bill Wendling96cb1122012-07-19 00:04:14 +00002387 std::pair<unsigned, const TargetRegisterClass*> MatchRC =
2388 getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
2389 OpInfo.ConstraintVT);
2390 std::pair<unsigned, const TargetRegisterClass*> InputRC =
2391 getRegForInlineAsmConstraint(Input.ConstraintCode,
2392 Input.ConstraintVT);
John Thompsoneac6e1d2010-09-13 18:15:37 +00002393 if ((OpInfo.ConstraintVT.isInteger() !=
2394 Input.ConstraintVT.isInteger()) ||
Eric Christopher5427ede2011-07-14 20:13:52 +00002395 (MatchRC.second != InputRC.second)) {
John Thompsoneac6e1d2010-09-13 18:15:37 +00002396 report_fatal_error("Unsupported asm: input constraint"
2397 " with a matching output constraint of"
2398 " incompatible type!");
2399 }
John Thompsoneac6e1d2010-09-13 18:15:37 +00002400 }
John Thompson44ab89e2010-10-29 17:29:13 +00002401
John Thompsoneac6e1d2010-09-13 18:15:37 +00002402 }
2403 }
2404
2405 return ConstraintOperands;
2406}
2407
Chris Lattner58f15c42008-10-17 16:21:11 +00002408
Chris Lattner4376fea2008-04-27 00:09:47 +00002409/// getConstraintGenerality - Return an integer indicating how general CT
2410/// is.
2411static unsigned getConstraintGenerality(TargetLowering::ConstraintType CT) {
2412 switch (CT) {
Chris Lattner4376fea2008-04-27 00:09:47 +00002413 case TargetLowering::C_Other:
2414 case TargetLowering::C_Unknown:
2415 return 0;
2416 case TargetLowering::C_Register:
2417 return 1;
2418 case TargetLowering::C_RegisterClass:
2419 return 2;
2420 case TargetLowering::C_Memory:
2421 return 3;
2422 }
Chandler Carruth732f05c2012-01-10 18:08:01 +00002423 llvm_unreachable("Invalid constraint type");
Chris Lattner4376fea2008-04-27 00:09:47 +00002424}
2425
John Thompson44ab89e2010-10-29 17:29:13 +00002426/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +00002427/// This object must already have been set up with the operand type
2428/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +00002429TargetLowering::ConstraintWeight
2430 TargetLowering::getMultipleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +00002431 AsmOperandInfo &info, int maIndex) const {
John Thompson44ab89e2010-10-29 17:29:13 +00002432 InlineAsm::ConstraintCodeVector *rCodes;
John Thompson67aff162010-09-21 22:04:54 +00002433 if (maIndex >= (int)info.multipleAlternatives.size())
2434 rCodes = &info.Codes;
2435 else
2436 rCodes = &info.multipleAlternatives[maIndex].Codes;
John Thompson44ab89e2010-10-29 17:29:13 +00002437 ConstraintWeight BestWeight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +00002438
2439 // Loop over the options, keeping track of the most general one.
John Thompson67aff162010-09-21 22:04:54 +00002440 for (unsigned i = 0, e = rCodes->size(); i != e; ++i) {
John Thompson44ab89e2010-10-29 17:29:13 +00002441 ConstraintWeight weight =
2442 getSingleConstraintMatchWeight(info, (*rCodes)[i].c_str());
John Thompsoneac6e1d2010-09-13 18:15:37 +00002443 if (weight > BestWeight)
2444 BestWeight = weight;
2445 }
2446
2447 return BestWeight;
2448}
2449
John Thompson44ab89e2010-10-29 17:29:13 +00002450/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +00002451/// This object must already have been set up with the operand type
2452/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +00002453TargetLowering::ConstraintWeight
2454 TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +00002455 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +00002456 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +00002457 Value *CallOperandVal = info.CallOperandVal;
2458 // If we don't have a value, we can't do a match,
2459 // but allow it at the lowest weight.
Stephen Hinesdce4a402014-05-29 02:49:00 -07002460 if (!CallOperandVal)
John Thompson44ab89e2010-10-29 17:29:13 +00002461 return CW_Default;
John Thompsoneac6e1d2010-09-13 18:15:37 +00002462 // Look at the constraint type.
2463 switch (*constraint) {
2464 case 'i': // immediate integer.
2465 case 'n': // immediate integer with a known value.
John Thompson44ab89e2010-10-29 17:29:13 +00002466 if (isa<ConstantInt>(CallOperandVal))
2467 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +00002468 break;
2469 case 's': // non-explicit intregal immediate.
John Thompson44ab89e2010-10-29 17:29:13 +00002470 if (isa<GlobalValue>(CallOperandVal))
2471 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +00002472 break;
John Thompson44ab89e2010-10-29 17:29:13 +00002473 case 'E': // immediate float if host format.
2474 case 'F': // immediate float.
2475 if (isa<ConstantFP>(CallOperandVal))
2476 weight = CW_Constant;
2477 break;
2478 case '<': // memory operand with autodecrement.
2479 case '>': // memory operand with autoincrement.
John Thompsoneac6e1d2010-09-13 18:15:37 +00002480 case 'm': // memory operand.
2481 case 'o': // offsettable memory operand
2482 case 'V': // non-offsettable memory operand
John Thompson44ab89e2010-10-29 17:29:13 +00002483 weight = CW_Memory;
John Thompsoneac6e1d2010-09-13 18:15:37 +00002484 break;
John Thompson44ab89e2010-10-29 17:29:13 +00002485 case 'r': // general register.
John Thompsoneac6e1d2010-09-13 18:15:37 +00002486 case 'g': // general register, memory operand or immediate integer.
John Thompson44ab89e2010-10-29 17:29:13 +00002487 // note: Clang converts "g" to "imr".
2488 if (CallOperandVal->getType()->isIntegerTy())
2489 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +00002490 break;
John Thompson44ab89e2010-10-29 17:29:13 +00002491 case 'X': // any operand.
John Thompsoneac6e1d2010-09-13 18:15:37 +00002492 default:
John Thompson44ab89e2010-10-29 17:29:13 +00002493 weight = CW_Default;
John Thompsoneac6e1d2010-09-13 18:15:37 +00002494 break;
2495 }
2496 return weight;
2497}
2498
Chris Lattner4376fea2008-04-27 00:09:47 +00002499/// ChooseConstraint - If there are multiple different constraints that we
2500/// could pick for this operand (e.g. "imr") try to pick the 'best' one.
Chris Lattner24e1a9d2008-04-27 01:49:46 +00002501/// This is somewhat tricky: constraints fall into four classes:
Chris Lattner4376fea2008-04-27 00:09:47 +00002502/// Other -> immediates and magic values
2503/// Register -> one specific register
2504/// RegisterClass -> a group of regs
2505/// Memory -> memory
2506/// Ideally, we would pick the most specific constraint possible: if we have
2507/// something that fits into a register, we would pick it. The problem here
2508/// is that if we have something that could either be in a register or in
2509/// memory that use of the register could cause selection of *other*
2510/// operands to fail: they might only succeed if we pick memory. Because of
2511/// this the heuristic we use is:
2512///
2513/// 1) If there is an 'other' constraint, and if the operand is valid for
2514/// that constraint, use it. This makes us take advantage of 'i'
2515/// constraints when available.
2516/// 2) Otherwise, pick the most general constraint present. This prefers
2517/// 'm' over 'r', for example.
2518///
2519static void ChooseConstraint(TargetLowering::AsmOperandInfo &OpInfo,
Dale Johannesen1784d162010-06-25 21:55:36 +00002520 const TargetLowering &TLI,
Dan Gohman475871a2008-07-27 21:46:04 +00002521 SDValue Op, SelectionDAG *DAG) {
Chris Lattner4376fea2008-04-27 00:09:47 +00002522 assert(OpInfo.Codes.size() > 1 && "Doesn't have multiple constraint options");
2523 unsigned BestIdx = 0;
2524 TargetLowering::ConstraintType BestType = TargetLowering::C_Unknown;
2525 int BestGenerality = -1;
Dale Johannesena5989f82010-06-28 22:09:45 +00002526
Chris Lattner4376fea2008-04-27 00:09:47 +00002527 // Loop over the options, keeping track of the most general one.
2528 for (unsigned i = 0, e = OpInfo.Codes.size(); i != e; ++i) {
2529 TargetLowering::ConstraintType CType =
2530 TLI.getConstraintType(OpInfo.Codes[i]);
Dale Johannesena5989f82010-06-28 22:09:45 +00002531
Chris Lattner5a096902008-04-27 00:37:18 +00002532 // If this is an 'other' constraint, see if the operand is valid for it.
2533 // For example, on X86 we might have an 'rI' constraint. If the operand
2534 // is an integer in the range [0..31] we want to use I (saving a load
2535 // of a register), otherwise we must use 'r'.
Gabor Greifba36cb52008-08-28 21:40:38 +00002536 if (CType == TargetLowering::C_Other && Op.getNode()) {
Chris Lattner5a096902008-04-27 00:37:18 +00002537 assert(OpInfo.Codes[i].size() == 1 &&
2538 "Unhandled multi-letter 'other' constraint");
Dan Gohman475871a2008-07-27 21:46:04 +00002539 std::vector<SDValue> ResultOps;
Eric Christopher100c8332011-06-02 23:16:42 +00002540 TLI.LowerAsmOperandForConstraint(Op, OpInfo.Codes[i],
Chris Lattner5a096902008-04-27 00:37:18 +00002541 ResultOps, *DAG);
2542 if (!ResultOps.empty()) {
2543 BestType = CType;
2544 BestIdx = i;
2545 break;
2546 }
2547 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002548
Dale Johannesena5989f82010-06-28 22:09:45 +00002549 // Things with matching constraints can only be registers, per gcc
2550 // documentation. This mainly affects "g" constraints.
2551 if (CType == TargetLowering::C_Memory && OpInfo.hasMatchingInput())
2552 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002553
Chris Lattner4376fea2008-04-27 00:09:47 +00002554 // This constraint letter is more general than the previous one, use it.
2555 int Generality = getConstraintGenerality(CType);
2556 if (Generality > BestGenerality) {
2557 BestType = CType;
2558 BestIdx = i;
2559 BestGenerality = Generality;
2560 }
2561 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002562
Chris Lattner4376fea2008-04-27 00:09:47 +00002563 OpInfo.ConstraintCode = OpInfo.Codes[BestIdx];
2564 OpInfo.ConstraintType = BestType;
2565}
2566
2567/// ComputeConstraintToUse - Determines the constraint code and constraint
2568/// type to use for the specific AsmOperandInfo, setting
2569/// OpInfo.ConstraintCode and OpInfo.ConstraintType.
Chris Lattner5a096902008-04-27 00:37:18 +00002570void TargetLowering::ComputeConstraintToUse(AsmOperandInfo &OpInfo,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002571 SDValue Op,
Chris Lattner5a096902008-04-27 00:37:18 +00002572 SelectionDAG *DAG) const {
Chris Lattner4376fea2008-04-27 00:09:47 +00002573 assert(!OpInfo.Codes.empty() && "Must have at least one constraint");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002574
Chris Lattner4376fea2008-04-27 00:09:47 +00002575 // Single-letter constraints ('r') are very common.
2576 if (OpInfo.Codes.size() == 1) {
2577 OpInfo.ConstraintCode = OpInfo.Codes[0];
2578 OpInfo.ConstraintType = getConstraintType(OpInfo.ConstraintCode);
2579 } else {
Dale Johannesen1784d162010-06-25 21:55:36 +00002580 ChooseConstraint(OpInfo, *this, Op, DAG);
Chris Lattner4376fea2008-04-27 00:09:47 +00002581 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002582
Chris Lattner4376fea2008-04-27 00:09:47 +00002583 // 'X' matches anything.
2584 if (OpInfo.ConstraintCode == "X" && OpInfo.CallOperandVal) {
2585 // Labels and constants are handled elsewhere ('X' is the only thing
Dale Johannesen8ea5ec62009-07-07 23:26:33 +00002586 // that matches labels). For Functions, the type here is the type of
Dale Johannesen5339c552009-07-20 23:27:39 +00002587 // the result, which is not what we want to look at; leave them alone.
2588 Value *v = OpInfo.CallOperandVal;
Dale Johannesen8ea5ec62009-07-07 23:26:33 +00002589 if (isa<BasicBlock>(v) || isa<ConstantInt>(v) || isa<Function>(v)) {
2590 OpInfo.CallOperandVal = v;
Chris Lattner4376fea2008-04-27 00:09:47 +00002591 return;
Dale Johannesen8ea5ec62009-07-07 23:26:33 +00002592 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002593
Chris Lattner4376fea2008-04-27 00:09:47 +00002594 // Otherwise, try to resolve it to something we know about by looking at
2595 // the actual operand type.
2596 if (const char *Repl = LowerXConstraint(OpInfo.ConstraintVT)) {
2597 OpInfo.ConstraintCode = Repl;
2598 OpInfo.ConstraintType = getConstraintType(OpInfo.ConstraintCode);
2599 }
2600 }
2601}
2602
David Majnemera2f8d372013-06-08 23:51:45 +00002603/// \brief Given an exact SDIV by a constant, create a multiplication
Benjamin Kramer9c640302011-07-08 10:31:30 +00002604/// with the multiplicative inverse of the constant.
Andrew Trickac6d9be2013-05-25 02:42:55 +00002605SDValue TargetLowering::BuildExactSDIV(SDValue Op1, SDValue Op2, SDLoc dl,
Benjamin Kramer9c640302011-07-08 10:31:30 +00002606 SelectionDAG &DAG) const {
2607 ConstantSDNode *C = cast<ConstantSDNode>(Op2);
2608 APInt d = C->getAPIntValue();
2609 assert(d != 0 && "Division by zero!");
2610
2611 // Shift the value upfront if it is even, so the LSB is one.
2612 unsigned ShAmt = d.countTrailingZeros();
2613 if (ShAmt) {
2614 // TODO: For UDIV use SRL instead of SRA.
2615 SDValue Amt = DAG.getConstant(ShAmt, getShiftAmountTy(Op1.getValueType()));
2616 Op1 = DAG.getNode(ISD::SRA, dl, Op1.getValueType(), Op1, Amt);
2617 d = d.ashr(ShAmt);
2618 }
2619
2620 // Calculate the multiplicative inverse, using Newton's method.
2621 APInt t, xn = d;
2622 while ((t = d*xn) != 1)
2623 xn *= APInt(d.getBitWidth(), 2) - t;
2624
2625 Op2 = DAG.getConstant(xn, Op1.getValueType());
2626 return DAG.getNode(ISD::MUL, dl, Op1.getValueType(), Op1, Op2);
2627}
2628
David Majnemera2f8d372013-06-08 23:51:45 +00002629/// \brief Given an ISD::SDIV node expressing a divide by constant,
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002630/// return a DAG expression to select that will generate the same value by
2631/// multiplying by a magic number. See:
2632/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Stephen Hinesdce4a402014-05-29 02:49:00 -07002633SDValue TargetLowering::BuildSDIV(SDNode *N, const APInt &Divisor,
2634 SelectionDAG &DAG, bool IsAfterLegalization,
2635 std::vector<SDNode *> *Created) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002636 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002637 SDLoc dl(N);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002638
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002639 // Check to see if we can do this.
Eli Friedmanfc69cb42008-11-30 06:35:39 +00002640 // FIXME: We should be more aggressive here.
2641 if (!isTypeLegal(VT))
2642 return SDValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002643
Stephen Hinesdce4a402014-05-29 02:49:00 -07002644 APInt::ms magics = Divisor.magic();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002645
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002646 // Multiply the numerator (operand 0) by the magic value
Eli Friedmanfc69cb42008-11-30 06:35:39 +00002647 // FIXME: We should support doing a MUL in a wider type
Dan Gohman475871a2008-07-27 21:46:04 +00002648 SDValue Q;
Richard Osborne19a4daf2011-11-07 17:09:05 +00002649 if (IsAfterLegalization ? isOperationLegal(ISD::MULHS, VT) :
2650 isOperationLegalOrCustom(ISD::MULHS, VT))
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002651 Q = DAG.getNode(ISD::MULHS, dl, VT, N->getOperand(0),
Dan Gohman525178c2007-10-08 18:33:35 +00002652 DAG.getConstant(magics.m, VT));
Richard Osborne19a4daf2011-11-07 17:09:05 +00002653 else if (IsAfterLegalization ? isOperationLegal(ISD::SMUL_LOHI, VT) :
2654 isOperationLegalOrCustom(ISD::SMUL_LOHI, VT))
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002655 Q = SDValue(DAG.getNode(ISD::SMUL_LOHI, dl, DAG.getVTList(VT, VT),
Dan Gohman525178c2007-10-08 18:33:35 +00002656 N->getOperand(0),
Gabor Greifba36cb52008-08-28 21:40:38 +00002657 DAG.getConstant(magics.m, VT)).getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00002658 else
Dan Gohman475871a2008-07-27 21:46:04 +00002659 return SDValue(); // No mulhs or equvialent
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002660 // If d > 0 and m < 0, add the numerator
Stephen Hinesdce4a402014-05-29 02:49:00 -07002661 if (Divisor.isStrictlyPositive() && magics.m.isNegative()) {
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002662 Q = DAG.getNode(ISD::ADD, dl, VT, Q, N->getOperand(0));
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002663 if (Created)
Gabor Greifba36cb52008-08-28 21:40:38 +00002664 Created->push_back(Q.getNode());
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002665 }
2666 // If d < 0 and m > 0, subtract the numerator.
Stephen Hinesdce4a402014-05-29 02:49:00 -07002667 if (Divisor.isNegative() && magics.m.isStrictlyPositive()) {
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002668 Q = DAG.getNode(ISD::SUB, dl, VT, Q, N->getOperand(0));
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002669 if (Created)
Gabor Greifba36cb52008-08-28 21:40:38 +00002670 Created->push_back(Q.getNode());
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002671 }
2672 // Shift right algebraic if shift value is nonzero
2673 if (magics.s > 0) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002674 Q = DAG.getNode(ISD::SRA, dl, VT, Q,
Owen Anderson95771af2011-02-25 21:41:48 +00002675 DAG.getConstant(magics.s, getShiftAmountTy(Q.getValueType())));
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002676 if (Created)
Gabor Greifba36cb52008-08-28 21:40:38 +00002677 Created->push_back(Q.getNode());
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002678 }
2679 // Extract the sign bit and add it to the quotient
Stephen Hinesdce4a402014-05-29 02:49:00 -07002680 SDValue T = DAG.getNode(ISD::SRL, dl, VT, Q,
2681 DAG.getConstant(VT.getScalarSizeInBits() - 1,
2682 getShiftAmountTy(Q.getValueType())));
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002683 if (Created)
Gabor Greifba36cb52008-08-28 21:40:38 +00002684 Created->push_back(T.getNode());
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002685 return DAG.getNode(ISD::ADD, dl, VT, Q, T);
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002686}
2687
David Majnemera2f8d372013-06-08 23:51:45 +00002688/// \brief Given an ISD::UDIV node expressing a divide by constant,
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002689/// return a DAG expression to select that will generate the same value by
2690/// multiplying by a magic number. See:
2691/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Stephen Hinesdce4a402014-05-29 02:49:00 -07002692SDValue TargetLowering::BuildUDIV(SDNode *N, const APInt &Divisor,
2693 SelectionDAG &DAG, bool IsAfterLegalization,
2694 std::vector<SDNode *> *Created) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002695 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002696 SDLoc dl(N);
Eli Friedman201c9772008-11-30 06:02:26 +00002697
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002698 // Check to see if we can do this.
Eli Friedman201c9772008-11-30 06:02:26 +00002699 // FIXME: We should be more aggressive here.
2700 if (!isTypeLegal(VT))
2701 return SDValue();
2702
2703 // FIXME: We should use a narrower constant when the upper
2704 // bits are known to be zero.
Stephen Hinesdce4a402014-05-29 02:49:00 -07002705 APInt::mu magics = Divisor.magicu();
Benjamin Kramer1c10b8d2011-03-17 20:39:14 +00002706
2707 SDValue Q = N->getOperand(0);
2708
2709 // If the divisor is even, we can avoid using the expensive fixup by shifting
2710 // the divided value upfront.
Stephen Hinesdce4a402014-05-29 02:49:00 -07002711 if (magics.a != 0 && !Divisor[0]) {
2712 unsigned Shift = Divisor.countTrailingZeros();
Benjamin Kramer1c10b8d2011-03-17 20:39:14 +00002713 Q = DAG.getNode(ISD::SRL, dl, VT, Q,
2714 DAG.getConstant(Shift, getShiftAmountTy(Q.getValueType())));
2715 if (Created)
2716 Created->push_back(Q.getNode());
2717
2718 // Get magic number for the shifted divisor.
Stephen Hinesdce4a402014-05-29 02:49:00 -07002719 magics = Divisor.lshr(Shift).magicu(Shift);
Benjamin Kramer1c10b8d2011-03-17 20:39:14 +00002720 assert(magics.a == 0 && "Should use cheap fixup now");
2721 }
Eli Friedman201c9772008-11-30 06:02:26 +00002722
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002723 // Multiply the numerator (operand 0) by the magic value
Eli Friedman201c9772008-11-30 06:02:26 +00002724 // FIXME: We should support doing a MUL in a wider type
Richard Osborne19a4daf2011-11-07 17:09:05 +00002725 if (IsAfterLegalization ? isOperationLegal(ISD::MULHU, VT) :
2726 isOperationLegalOrCustom(ISD::MULHU, VT))
Benjamin Kramer1c10b8d2011-03-17 20:39:14 +00002727 Q = DAG.getNode(ISD::MULHU, dl, VT, Q, DAG.getConstant(magics.m, VT));
Richard Osborne19a4daf2011-11-07 17:09:05 +00002728 else if (IsAfterLegalization ? isOperationLegal(ISD::UMUL_LOHI, VT) :
2729 isOperationLegalOrCustom(ISD::UMUL_LOHI, VT))
Benjamin Kramer1c10b8d2011-03-17 20:39:14 +00002730 Q = SDValue(DAG.getNode(ISD::UMUL_LOHI, dl, DAG.getVTList(VT, VT), Q,
2731 DAG.getConstant(magics.m, VT)).getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00002732 else
Dan Gohman475871a2008-07-27 21:46:04 +00002733 return SDValue(); // No mulhu or equvialent
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002734 if (Created)
Gabor Greifba36cb52008-08-28 21:40:38 +00002735 Created->push_back(Q.getNode());
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002736
2737 if (magics.a == 0) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07002738 assert(magics.s < Divisor.getBitWidth() &&
Eli Friedman201c9772008-11-30 06:02:26 +00002739 "We shouldn't generate an undefined shift!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002740 return DAG.getNode(ISD::SRL, dl, VT, Q,
Owen Anderson95771af2011-02-25 21:41:48 +00002741 DAG.getConstant(magics.s, getShiftAmountTy(Q.getValueType())));
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002742 } else {
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002743 SDValue NPQ = DAG.getNode(ISD::SUB, dl, VT, N->getOperand(0), Q);
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002744 if (Created)
Gabor Greifba36cb52008-08-28 21:40:38 +00002745 Created->push_back(NPQ.getNode());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002746 NPQ = DAG.getNode(ISD::SRL, dl, VT, NPQ,
Owen Anderson95771af2011-02-25 21:41:48 +00002747 DAG.getConstant(1, getShiftAmountTy(NPQ.getValueType())));
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002748 if (Created)
Gabor Greifba36cb52008-08-28 21:40:38 +00002749 Created->push_back(NPQ.getNode());
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002750 NPQ = DAG.getNode(ISD::ADD, dl, VT, NPQ, Q);
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002751 if (Created)
Gabor Greifba36cb52008-08-28 21:40:38 +00002752 Created->push_back(NPQ.getNode());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002753 return DAG.getNode(ISD::SRL, dl, VT, NPQ,
Owen Anderson95771af2011-02-25 21:41:48 +00002754 DAG.getConstant(magics.s-1, getShiftAmountTy(NPQ.getValueType())));
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002755 }
2756}
Stephen Hines36b56882014-04-23 16:57:46 -07002757
2758bool TargetLowering::
2759verifyReturnAddressArgumentIsConstant(SDValue Op, SelectionDAG &DAG) const {
2760 if (!isa<ConstantSDNode>(Op.getOperand(0))) {
2761 DAG.getContext()->emitError("argument to '__builtin_return_address' must "
2762 "be a constant integer");
2763 return true;
2764 }
2765
2766 return false;
2767}
Stephen Hinesdce4a402014-05-29 02:49:00 -07002768
2769//===----------------------------------------------------------------------===//
2770// Legalization Utilities
2771//===----------------------------------------------------------------------===//
2772
2773bool TargetLowering::expandMUL(SDNode *N, SDValue &Lo, SDValue &Hi, EVT HiLoVT,
2774 SelectionDAG &DAG, SDValue LL, SDValue LH,
2775 SDValue RL, SDValue RH) const {
2776 EVT VT = N->getValueType(0);
2777 SDLoc dl(N);
2778
2779 bool HasMULHS = isOperationLegalOrCustom(ISD::MULHS, HiLoVT);
2780 bool HasMULHU = isOperationLegalOrCustom(ISD::MULHU, HiLoVT);
2781 bool HasSMUL_LOHI = isOperationLegalOrCustom(ISD::SMUL_LOHI, HiLoVT);
2782 bool HasUMUL_LOHI = isOperationLegalOrCustom(ISD::UMUL_LOHI, HiLoVT);
2783 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
2784 unsigned OuterBitSize = VT.getSizeInBits();
2785 unsigned InnerBitSize = HiLoVT.getSizeInBits();
2786 unsigned LHSSB = DAG.ComputeNumSignBits(N->getOperand(0));
2787 unsigned RHSSB = DAG.ComputeNumSignBits(N->getOperand(1));
2788
2789 // LL, LH, RL, and RH must be either all NULL or all set to a value.
2790 assert((LL.getNode() && LH.getNode() && RL.getNode() && RH.getNode()) ||
2791 (!LL.getNode() && !LH.getNode() && !RL.getNode() && !RH.getNode()));
2792
2793 if (!LL.getNode() && !RL.getNode() &&
2794 isOperationLegalOrCustom(ISD::TRUNCATE, HiLoVT)) {
2795 LL = DAG.getNode(ISD::TRUNCATE, dl, HiLoVT, N->getOperand(0));
2796 RL = DAG.getNode(ISD::TRUNCATE, dl, HiLoVT, N->getOperand(1));
2797 }
2798
2799 if (!LL.getNode())
2800 return false;
2801
2802 APInt HighMask = APInt::getHighBitsSet(OuterBitSize, InnerBitSize);
2803 if (DAG.MaskedValueIsZero(N->getOperand(0), HighMask) &&
2804 DAG.MaskedValueIsZero(N->getOperand(1), HighMask)) {
2805 // The inputs are both zero-extended.
2806 if (HasUMUL_LOHI) {
2807 // We can emit a umul_lohi.
2808 Lo = DAG.getNode(ISD::UMUL_LOHI, dl,
2809 DAG.getVTList(HiLoVT, HiLoVT), LL, RL);
2810 Hi = SDValue(Lo.getNode(), 1);
2811 return true;
2812 }
2813 if (HasMULHU) {
2814 // We can emit a mulhu+mul.
2815 Lo = DAG.getNode(ISD::MUL, dl, HiLoVT, LL, RL);
2816 Hi = DAG.getNode(ISD::MULHU, dl, HiLoVT, LL, RL);
2817 return true;
2818 }
2819 }
2820 if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) {
2821 // The input values are both sign-extended.
2822 if (HasSMUL_LOHI) {
2823 // We can emit a smul_lohi.
2824 Lo = DAG.getNode(ISD::SMUL_LOHI, dl,
2825 DAG.getVTList(HiLoVT, HiLoVT), LL, RL);
2826 Hi = SDValue(Lo.getNode(), 1);
2827 return true;
2828 }
2829 if (HasMULHS) {
2830 // We can emit a mulhs+mul.
2831 Lo = DAG.getNode(ISD::MUL, dl, HiLoVT, LL, RL);
2832 Hi = DAG.getNode(ISD::MULHS, dl, HiLoVT, LL, RL);
2833 return true;
2834 }
2835 }
2836
2837 if (!LH.getNode() && !RH.getNode() &&
2838 isOperationLegalOrCustom(ISD::SRL, VT) &&
2839 isOperationLegalOrCustom(ISD::TRUNCATE, HiLoVT)) {
2840 unsigned ShiftAmt = VT.getSizeInBits() - HiLoVT.getSizeInBits();
2841 SDValue Shift = DAG.getConstant(ShiftAmt, getShiftAmountTy(VT));
2842 LH = DAG.getNode(ISD::SRL, dl, VT, N->getOperand(0), Shift);
2843 LH = DAG.getNode(ISD::TRUNCATE, dl, HiLoVT, LH);
2844 RH = DAG.getNode(ISD::SRL, dl, VT, N->getOperand(1), Shift);
2845 RH = DAG.getNode(ISD::TRUNCATE, dl, HiLoVT, RH);
2846 }
2847
2848 if (!LH.getNode())
2849 return false;
2850
2851 if (HasUMUL_LOHI) {
2852 // Lo,Hi = umul LHS, RHS.
2853 SDValue UMulLOHI = DAG.getNode(ISD::UMUL_LOHI, dl,
2854 DAG.getVTList(HiLoVT, HiLoVT), LL, RL);
2855 Lo = UMulLOHI;
2856 Hi = UMulLOHI.getValue(1);
2857 RH = DAG.getNode(ISD::MUL, dl, HiLoVT, LL, RH);
2858 LH = DAG.getNode(ISD::MUL, dl, HiLoVT, LH, RL);
2859 Hi = DAG.getNode(ISD::ADD, dl, HiLoVT, Hi, RH);
2860 Hi = DAG.getNode(ISD::ADD, dl, HiLoVT, Hi, LH);
2861 return true;
2862 }
2863 if (HasMULHU) {
2864 Lo = DAG.getNode(ISD::MUL, dl, HiLoVT, LL, RL);
2865 Hi = DAG.getNode(ISD::MULHU, dl, HiLoVT, LL, RL);
2866 RH = DAG.getNode(ISD::MUL, dl, HiLoVT, LL, RH);
2867 LH = DAG.getNode(ISD::MUL, dl, HiLoVT, LH, RL);
2868 Hi = DAG.getNode(ISD::ADD, dl, HiLoVT, Hi, RH);
2869 Hi = DAG.getNode(ISD::ADD, dl, HiLoVT, Hi, LH);
2870 return true;
2871 }
2872 }
2873 return false;
2874}