blob: 6052a4864089c759f455804b52d5c214d8b14b91 [file] [log] [blame]
Chris Lattner3a4d1b22005-01-07 07:44:53 +00001//===-- TargetLowering.cpp - Implement the TargetLowering class -----------===//
Misha Brukman10468d82005-04-21 22:55:34 +00002//
Chris Lattner3a4d1b22005-01-07 07:44:53 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Brukman10468d82005-04-21 22:55:34 +00007//
Chris Lattner3a4d1b22005-01-07 07:44:53 +00008//===----------------------------------------------------------------------===//
9//
10// This implements the TargetLowering class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Target/TargetLowering.h"
Jakob Stoklund Olesen75fbe902012-05-04 02:19:22 +000015#include "llvm/ADT/BitVector.h"
Owen Andersone2f23a32007-09-07 04:06:50 +000016#include "llvm/ADT/STLExtras.h"
Chandler Carruthed0881b2012-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 Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/DataLayout.h"
23#include "llvm/IR/DerivedTypes.h"
24#include "llvm/IR/GlobalVariable.h"
Bill Wendling908bf812014-01-06 00:43:20 +000025#include "llvm/IR/LLVMContext.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000026#include "llvm/MC/MCAsmInfo.h"
27#include "llvm/MC/MCExpr.h"
Nadav Rotem8b24a732011-06-01 12:51:46 +000028#include "llvm/Support/CommandLine.h"
Torok Edwin56d06592009-07-11 20:10:48 +000029#include "llvm/Support/ErrorHandling.h"
Chris Lattnerf0b24d22006-01-30 04:09:27 +000030#include "llvm/Support/MathExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000031#include "llvm/Target/TargetLoweringObjectFile.h"
32#include "llvm/Target/TargetMachine.h"
33#include "llvm/Target/TargetRegisterInfo.h"
Nick Lewycky0de20af2010-12-19 20:43:38 +000034#include <cctype>
Chris Lattner3a4d1b22005-01-07 07:44:53 +000035using namespace llvm;
36
Chris Lattner5e693ed2009-07-28 03:13:23 +000037/// NOTE: The constructor takes ownership of TLOF.
Dan Gohman57c732b2010-04-21 01:34:56 +000038TargetLowering::TargetLowering(const TargetMachine &tm,
39 const TargetLoweringObjectFile *tlof)
Benjamin Kramer56b31bd2013-01-11 20:05:37 +000040 : TargetLoweringBase(tm, tlof) {}
Chris Lattner6f809792005-01-16 07:28:11 +000041
Evan Cheng6af02632005-12-20 06:22:03 +000042const char *TargetLowering::getTargetNodeName(unsigned Opcode) const {
43 return NULL;
44}
Evan Cheng9cdc16c2005-12-21 23:05:39 +000045
Tim Northoverf1450d82013-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 Wendling658d24d2013-01-18 21:53:16 +000054 AttributeSet CallerAttrs = F->getAttributes();
55 if (AttrBuilder(CallerAttrs, AttributeSet::ReturnIndex)
Tim Northoverf1450d82013-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 Wendling658d24d2013-01-18 21:53:16 +000060 if (CallerAttrs.hasAttribute(AttributeSet::ReturnIndex, Attribute::ZExt) ||
61 CallerAttrs.hasAttribute(AttributeSet::ReturnIndex, Attribute::SExt))
Tim Northoverf1450d82013-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 Trick74f4c742013-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);
78 isReturned = CS->paramHasAttr(AttrIdx, Attribute::Returned);
79 Alignment = CS->getParamAlignment(AttrIdx);
80}
Tim Northoverf1450d82013-01-09 13:18:15 +000081
82/// Generate a libcall taking the given operands as arguments and returning a
83/// result of type RetVT.
Michael Gottesman7a801722013-08-13 17:54:56 +000084std::pair<SDValue, SDValue>
85TargetLowering::makeLibCall(SelectionDAG &DAG,
86 RTLIB::Libcall LC, EVT RetVT,
87 const SDValue *Ops, unsigned NumOps,
88 bool isSigned, SDLoc dl,
89 bool doesNotReturn,
90 bool isReturnValueUsed) const {
Tim Northoverf1450d82013-01-09 13:18:15 +000091 TargetLowering::ArgListTy Args;
92 Args.reserve(NumOps);
93
94 TargetLowering::ArgListEntry Entry;
95 for (unsigned i = 0; i != NumOps; ++i) {
96 Entry.Node = Ops[i];
97 Entry.Ty = Entry.Node.getValueType().getTypeForEVT(*DAG.getContext());
98 Entry.isSExt = isSigned;
99 Entry.isZExt = !isSigned;
100 Args.push_back(Entry);
101 }
102 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC), getPointerTy());
103
104 Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
105 TargetLowering::
106 CallLoweringInfo CLI(DAG.getEntryNode(), RetTy, isSigned, !isSigned, false,
107 false, 0, getLibcallCallingConv(LC),
108 /*isTailCall=*/false,
Michael Gottesman7a801722013-08-13 17:54:56 +0000109 doesNotReturn, isReturnValueUsed, Callee, Args,
110 DAG, dl);
111 return LowerCallTo(CLI);
Tim Northoverf1450d82013-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 Trickef9de2a2013-05-25 02:42:55 +0000120 SDLoc dl) const {
Tim Northoverf1450d82013-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 Gottesman7a801722013-08-13 17:54:56 +0000201 NewLHS = makeLibCall(DAG, LC1, RetVT, Ops, 2, false/*sign irrelevant*/,
202 dl).first;
Tim Northoverf1450d82013-01-09 13:18:15 +0000203 NewRHS = DAG.getConstant(0, RetVT);
204 CCCode = getCmpLibcallCC(LC1);
205 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Matt Arsenault758659232013-05-18 00:21:46 +0000206 SDValue Tmp = DAG.getNode(ISD::SETCC, dl,
207 getSetCCResultType(*DAG.getContext(), RetVT),
Tim Northoverf1450d82013-01-09 13:18:15 +0000208 NewLHS, NewRHS, DAG.getCondCode(CCCode));
Michael Gottesman7a801722013-08-13 17:54:56 +0000209 NewLHS = makeLibCall(DAG, LC2, RetVT, Ops, 2, false/*sign irrelevant*/,
210 dl).first;
Matt Arsenault758659232013-05-18 00:21:46 +0000211 NewLHS = DAG.getNode(ISD::SETCC, dl,
212 getSetCCResultType(*DAG.getContext(), RetVT), NewLHS,
Tim Northoverf1450d82013-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 Lattnerb6db2c62010-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 Peck527da1b2010-11-23 03:31:01 +0000226
Chris Lattnerb6db2c62010-01-25 23:26:13 +0000227 // In PIC mode, if the target supports a GPRel32 directive, use it.
228 if (getTargetMachine().getMCAsmInfo()->getGPRel32Directive() != 0)
229 return MachineJumpTableInfo::EK_GPRel32BlockAddress;
Wesley Peck527da1b2010-11-23 03:31:01 +0000230
Chris Lattnerb6db2c62010-01-25 23:26:13 +0000231 // Otherwise, use a label difference.
232 return MachineJumpTableInfo::EK_LabelDifference32;
233}
234
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000235SDValue TargetLowering::getPICJumpTableRelocBase(SDValue Table,
236 SelectionDAG &DAG) const {
Chris Lattner547c7612010-01-26 06:53:37 +0000237 // If our PIC model is GP relative, use the global offset table as the base.
Akira Hatanaka8483a6c2012-04-09 20:32:12 +0000238 unsigned JTEncoding = getJumpTableEncoding();
239
240 if ((JTEncoding == MachineJumpTableInfo::EK_GPRel64BlockAddress) ||
241 (JTEncoding == MachineJumpTableInfo::EK_GPRel32BlockAddress))
Micah Villmow89021e42012-10-09 16:06:12 +0000242 return DAG.getGLOBAL_OFFSET_TABLE(getPointerTy(0));
Akira Hatanaka8483a6c2012-04-09 20:32:12 +0000243
Evan Cheng797d56f2007-11-09 01:32:10 +0000244 return Table;
245}
246
Chris Lattner8a6c1ea2010-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 Lattner8a785d72010-01-26 06:28:43 +0000251TargetLowering::getPICJumpTableRelocBaseExpr(const MachineFunction *MF,
252 unsigned JTI,MCContext &Ctx) const{
Chris Lattner273735b2010-01-26 05:58:28 +0000253 // The normal PIC reloc base is the label at the start of the jump table.
Chris Lattner8a785d72010-01-26 06:28:43 +0000254 return MCSymbolRefExpr::Create(MF->getJTISymbol(JTI, Ctx), Ctx);
Chris Lattner8a6c1ea2010-01-26 05:30:30 +0000255}
256
Dan Gohman2fe6bee2008-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 Sands12da8ce2009-03-07 15:45:40 +0000267 !GA->getGlobal()->isWeakForLinker())
Dan Gohman2fe6bee2008-10-18 02:06:02 +0000268 return true;
269
270 // Otherwise assume nothing is safe.
271 return false;
272}
273
Chris Lattneree1dadb2006-02-04 02:13:02 +0000274//===----------------------------------------------------------------------===//
275// Optimization Methods
276//===----------------------------------------------------------------------===//
277
Wesley Peck527da1b2010-11-23 03:31:01 +0000278/// ShrinkDemandedConstant - Check to see if the specified operand of the
Nate Begeman8a77efe2006-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 Peck527da1b2010-11-23 03:31:01 +0000282bool TargetLowering::TargetLoweringOpt::ShrinkDemandedConstant(SDValue Op,
Dan Gohmanae2b6fb2008-02-27 00:25:32 +0000283 const APInt &Demanded) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000284 SDLoc dl(Op);
Bill Wendling6d271472009-03-04 00:18:06 +0000285
Chris Lattner118ddba2006-02-26 23:36:02 +0000286 // FIXME: ISD::SELECT, ISD::SELECT_CC
Dan Gohmane58ab792009-01-29 01:59:02 +0000287 switch (Op.getOpcode()) {
Nate Begeman8a77efe2006-02-16 21:11:51 +0000288 default: break;
Nate Begeman8a77efe2006-02-16 21:11:51 +0000289 case ISD::XOR:
Bill Wendling6d271472009-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 Anderson53aa7a92009-08-10 22:56:29 +0000301 EVT VT = Op.getValueType();
Bill Wendling6d271472009-03-04 00:18:06 +0000302 SDValue New = DAG.getNode(Op.getOpcode(), dl, VT, Op.getOperand(0),
303 DAG.getConstant(Demanded &
Wesley Peck527da1b2010-11-23 03:31:01 +0000304 C->getAPIntValue(),
Bill Wendling6d271472009-03-04 00:18:06 +0000305 VT));
306 return CombineTo(Op, New);
307 }
308
Nate Begemandc7bba92006-02-03 22:24:05 +0000309 break;
310 }
Bill Wendling6d271472009-03-04 00:18:06 +0000311 }
312
Nate Begemandc7bba92006-02-03 22:24:05 +0000313 return false;
314}
Chris Lattnerf0b24d22006-01-30 04:09:27 +0000315
Dan Gohmanad3e5492009-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 Trickef9de2a2013-05-25 02:42:55 +0000324 SDLoc dl) {
Dan Gohmanad3e5492009-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 Rotem33360d82012-12-19 07:39:08 +0000338 unsigned DemandedSize = BitWidth - Demanded.countLeadingZeros();
339 unsigned SmallVTBits = DemandedSize;
Dan Gohmanad3e5492009-04-08 00:15:30 +0000340 if (!isPowerOf2_32(SmallVTBits))
341 SmallVTBits = NextPowerOf2(SmallVTBits);
342 for (; SmallVTBits < BitWidth; SmallVTBits = NextPowerOf2(SmallVTBits)) {
Owen Anderson117c9e82009-08-12 00:36:31 +0000343 EVT SmallVT = EVT::getIntegerVT(*DAG.getContext(), SmallVTBits);
Dan Gohmanad3e5492009-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 Rotem33360d82012-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 Gohmanad3e5492009-04-08 00:15:30 +0000355 return CombineTo(Op, Z);
356 }
357 }
358 return false;
359}
360
Nate Begeman8a77efe2006-02-16 21:11:51 +0000361/// SimplifyDemandedBits - Look at Op. At this point, we know that only the
Chad Rosier79044db2011-06-11 02:27:46 +0000362/// DemandedMask bits of the result of Op are ever used downstream. If we can
Nate Begeman8a77efe2006-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 Gohman2ce6f2a2008-07-27 21:46:04 +0000368bool TargetLowering::SimplifyDemandedBits(SDValue Op,
Dan Gohmanae2b6fb2008-02-27 00:25:32 +0000369 const APInt &DemandedMask,
370 APInt &KnownZero,
371 APInt &KnownOne,
Nate Begeman8a77efe2006-02-16 21:11:51 +0000372 TargetLoweringOpt &TLO,
373 unsigned Depth) const {
Dan Gohmanae2b6fb2008-02-27 00:25:32 +0000374 unsigned BitWidth = DemandedMask.getBitWidth();
Dan Gohman1d459e42009-12-11 21:31:27 +0000375 assert(Op.getValueType().getScalarType().getSizeInBits() == BitWidth &&
Dan Gohmanae2b6fb2008-02-27 00:25:32 +0000376 "Mask size mismatches value type size!");
377 APInt NewMask = DemandedMask;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000378 SDLoc dl(Op);
Chris Lattner0184f882007-05-17 18:19:23 +0000379
Dan Gohmanae2b6fb2008-02-27 00:25:32 +0000380 // Don't know anything.
381 KnownZero = KnownOne = APInt(BitWidth, 0);
382
Nate Begeman8a77efe2006-02-16 21:11:51 +0000383 // Other users may use these bits.
Wesley Peck527da1b2010-11-23 03:31:01 +0000384 if (!Op.getNode()->hasOneUse()) {
Nate Begeman8a77efe2006-02-16 21:11:51 +0000385 if (Depth != 0) {
Wesley Peck527da1b2010-11-23 03:31:01 +0000386 // If not at the root, Just compute the KnownZero/KnownOne bits to
Nate Begeman8a77efe2006-02-16 21:11:51 +0000387 // simplify things downstream.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +0000388 TLO.DAG.ComputeMaskedBits(Op, KnownZero, KnownOne, Depth);
Nate Begeman8a77efe2006-02-16 21:11:51 +0000389 return false;
390 }
391 // If this is the root being simplified, allow it to have multiple uses,
Dan Gohmanae2b6fb2008-02-27 00:25:32 +0000392 // just set the NewMask to all bits.
393 NewMask = APInt::getAllOnesValue(BitWidth);
Wesley Peck527da1b2010-11-23 03:31:01 +0000394 } else if (DemandedMask == 0) {
Nate Begeman8a77efe2006-02-16 21:11:51 +0000395 // Not demanding any bits from Op.
396 if (Op.getOpcode() != ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +0000397 return TLO.CombineTo(Op, TLO.DAG.getUNDEF(Op.getValueType()));
Nate Begeman8a77efe2006-02-16 21:11:51 +0000398 return false;
399 } else if (Depth == 6) { // Limit search depth.
400 return false;
401 }
402
Dan Gohmanae2b6fb2008-02-27 00:25:32 +0000403 APInt KnownZero2, KnownOne2, KnownZeroOut, KnownOneOut;
Chris Lattnerf0b24d22006-01-30 04:09:27 +0000404 switch (Op.getOpcode()) {
405 case ISD::Constant:
Nate Begeman8a77efe2006-02-16 21:11:51 +0000406 // We know all of the bits for a constant!
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +0000407 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue();
408 KnownZero = ~KnownOne;
Chris Lattner118ddba2006-02-26 23:36:02 +0000409 return false; // Don't fall through, will infinitely loop.
Chris Lattnerf0b24d22006-01-30 04:09:27 +0000410 case ISD::AND:
Chris Lattnera60751d2006-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 Gohmanae2b6fb2008-02-27 00:25:32 +0000416 APInt LHSZero, LHSOne;
Dale Johannesend2b48112011-01-10 21:53:07 +0000417 // Do not increment Depth here; that can cause an infinite loop.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +0000418 TLO.DAG.ComputeMaskedBits(Op.getOperand(0), LHSZero, LHSOne, Depth);
Chris Lattnera60751d2006-02-27 00:36:27 +0000419 // If the LHS already has zeros where RHSC does, this and is dead.
Dan Gohmanae2b6fb2008-02-27 00:25:32 +0000420 if ((LHSZero & NewMask) == (~RHSC->getAPIntValue() & NewMask))
Chris Lattnera60751d2006-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 Gohmanae2b6fb2008-02-27 00:25:32 +0000424 if (TLO.ShrinkDemandedConstant(Op, ~LHSZero & NewMask))
Chris Lattnera60751d2006-02-27 00:36:27 +0000425 return true;
426 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000427
Dan Gohmanae2b6fb2008-02-27 00:25:32 +0000428 if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero,
Nate Begeman8a77efe2006-02-16 21:11:51 +0000429 KnownOne, TLO, Depth+1))
Chris Lattnerf0b24d22006-01-30 04:09:27 +0000430 return true;
Wesley Peck527da1b2010-11-23 03:31:01 +0000431 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanae2b6fb2008-02-27 00:25:32 +0000432 if (SimplifyDemandedBits(Op.getOperand(0), ~KnownZero & NewMask,
Nate Begeman8a77efe2006-02-16 21:11:51 +0000433 KnownZero2, KnownOne2, TLO, Depth+1))
434 return true;
Wesley Peck527da1b2010-11-23 03:31:01 +0000435 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
436
Nate Begeman8a77efe2006-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 Gohmanae2b6fb2008-02-27 00:25:32 +0000439 if ((NewMask & ~KnownZero2 & KnownOne) == (~KnownZero2 & NewMask))
Nate Begeman8a77efe2006-02-16 21:11:51 +0000440 return TLO.CombineTo(Op, Op.getOperand(0));
Dan Gohmanae2b6fb2008-02-27 00:25:32 +0000441 if ((NewMask & ~KnownZero & KnownOne2) == (~KnownZero & NewMask))
Nate Begeman8a77efe2006-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 Gohmanae2b6fb2008-02-27 00:25:32 +0000444 if ((NewMask & (KnownZero|KnownZero2)) == NewMask)
Nate Begeman8a77efe2006-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 Gohmanae2b6fb2008-02-27 00:25:32 +0000447 if (TLO.ShrinkDemandedConstant(Op, ~KnownZero2 & NewMask))
Nate Begeman8a77efe2006-02-16 21:11:51 +0000448 return true;
Dan Gohmanad3e5492009-04-08 00:15:30 +0000449 // If the operation can be done in a smaller type, do so.
Dan Gohman600f62b2010-06-24 14:30:44 +0000450 if (TLO.ShrinkDemandedOp(Op, BitWidth, NewMask, dl))
Dan Gohmanad3e5492009-04-08 00:15:30 +0000451 return true;
452
Nate Begeman8a77efe2006-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 Lattnerf0b24d22006-01-30 04:09:27 +0000458 case ISD::OR:
Wesley Peck527da1b2010-11-23 03:31:01 +0000459 if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero,
Nate Begeman8a77efe2006-02-16 21:11:51 +0000460 KnownOne, TLO, Depth+1))
461 return true;
Wesley Peck527da1b2010-11-23 03:31:01 +0000462 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanae2b6fb2008-02-27 00:25:32 +0000463 if (SimplifyDemandedBits(Op.getOperand(0), ~KnownOne & NewMask,
Nate Begeman8a77efe2006-02-16 21:11:51 +0000464 KnownZero2, KnownOne2, TLO, Depth+1))
465 return true;
Wesley Peck527da1b2010-11-23 03:31:01 +0000466 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
467
Nate Begeman8a77efe2006-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 Gohmanae2b6fb2008-02-27 00:25:32 +0000470 if ((NewMask & ~KnownOne2 & KnownZero) == (~KnownOne2 & NewMask))
Nate Begeman8a77efe2006-02-16 21:11:51 +0000471 return TLO.CombineTo(Op, Op.getOperand(0));
Dan Gohmanae2b6fb2008-02-27 00:25:32 +0000472 if ((NewMask & ~KnownOne & KnownZero2) == (~KnownOne & NewMask))
Nate Begeman8a77efe2006-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 Gohmanae2b6fb2008-02-27 00:25:32 +0000476 if ((NewMask & ~KnownZero & KnownOne2) == (~KnownZero & NewMask))
Nate Begeman8a77efe2006-02-16 21:11:51 +0000477 return TLO.CombineTo(Op, Op.getOperand(0));
Dan Gohmanae2b6fb2008-02-27 00:25:32 +0000478 if ((NewMask & ~KnownZero2 & KnownOne) == (~KnownZero2 & NewMask))
Nate Begeman8a77efe2006-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 Gohmanae2b6fb2008-02-27 00:25:32 +0000481 if (TLO.ShrinkDemandedConstant(Op, NewMask))
Nate Begeman8a77efe2006-02-16 21:11:51 +0000482 return true;
Dan Gohmanad3e5492009-04-08 00:15:30 +0000483 // If the operation can be done in a smaller type, do so.
Dan Gohman600f62b2010-06-24 14:30:44 +0000484 if (TLO.ShrinkDemandedOp(Op, BitWidth, NewMask, dl))
Dan Gohmanad3e5492009-04-08 00:15:30 +0000485 return true;
486
Nate Begeman8a77efe2006-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 Lattnerf0b24d22006-01-30 04:09:27 +0000492 case ISD::XOR:
Wesley Peck527da1b2010-11-23 03:31:01 +0000493 if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero,
Nate Begeman8a77efe2006-02-16 21:11:51 +0000494 KnownOne, TLO, Depth+1))
495 return true;
Wesley Peck527da1b2010-11-23 03:31:01 +0000496 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanae2b6fb2008-02-27 00:25:32 +0000497 if (SimplifyDemandedBits(Op.getOperand(0), NewMask, KnownZero2,
Nate Begeman8a77efe2006-02-16 21:11:51 +0000498 KnownOne2, TLO, Depth+1))
499 return true;
Wesley Peck527da1b2010-11-23 03:31:01 +0000500 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
501
Nate Begeman8a77efe2006-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 Gohmanae2b6fb2008-02-27 00:25:32 +0000504 if ((KnownZero & NewMask) == NewMask)
Nate Begeman8a77efe2006-02-16 21:11:51 +0000505 return TLO.CombineTo(Op, Op.getOperand(0));
Dan Gohmanae2b6fb2008-02-27 00:25:32 +0000506 if ((KnownZero2 & NewMask) == NewMask)
Nate Begeman8a77efe2006-02-16 21:11:51 +0000507 return TLO.CombineTo(Op, Op.getOperand(1));
Dan Gohmanad3e5492009-04-08 00:15:30 +0000508 // If the operation can be done in a smaller type, do so.
Dan Gohman600f62b2010-06-24 14:30:44 +0000509 if (TLO.ShrinkDemandedOp(Op, BitWidth, NewMask, dl))
Dan Gohmanad3e5492009-04-08 00:15:30 +0000510 return true;
511
Chris Lattner5d5916b2006-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 Ledru91ce36c2012-09-27 10:14:43 +0000514 // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
Dan Gohmanae2b6fb2008-02-27 00:25:32 +0000515 if ((NewMask & ~KnownZero & ~KnownZero2) == 0)
Dale Johannesen400dc2e2009-02-06 21:50:26 +0000516 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::OR, dl, Op.getValueType(),
Chris Lattner5d5916b2006-11-27 21:50:02 +0000517 Op.getOperand(0),
518 Op.getOperand(1)));
Wesley Peck527da1b2010-11-23 03:31:01 +0000519
Nate Begeman8a77efe2006-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 Peck527da1b2010-11-23 03:31:01 +0000524
Nate Begeman8a77efe2006-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 Ledru91ce36c2012-09-27 10:14:43 +0000528 // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
Joel Jones828531f2012-04-17 22:23:10 +0000529 // NB: it is okay if more bits are known than are requested
Stephen Lincfe7f352013-07-08 00:37:03 +0000530 if ((NewMask & (KnownZero|KnownOne)) == NewMask) { // all known on one side
Joel Jones828531f2012-04-17 22:23:10 +0000531 if (KnownOne == KnownOne2) { // set bits are the same on both sides
Owen Anderson53aa7a92009-08-10 22:56:29 +0000532 EVT VT = Op.getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000533 SDValue ANDC = TLO.DAG.getConstant(~KnownOne & NewMask, VT);
Wesley Peck527da1b2010-11-23 03:31:01 +0000534 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::AND, dl, VT,
Dale Johannesenf1163e92009-02-03 00:47:48 +0000535 Op.getOperand(0), ANDC));
Nate Begeman8a77efe2006-02-16 21:11:51 +0000536 }
537 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000538
Nate Begeman8a77efe2006-02-16 21:11:51 +0000539 // If the RHS is a constant, see if we can simplify it.
Torok Edwin613d7af2008-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 Anderson53aa7a92009-08-10 22:56:29 +0000547 EVT VT = Op.getValueType();
Dale Johannesenf1163e92009-02-03 00:47:48 +0000548 SDValue New = TLO.DAG.getNode(Op.getOpcode(), dl,VT, Op.getOperand(0),
Torok Edwin613d7af2008-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 Begeman8a77efe2006-02-16 21:11:51 +0000559 KnownZero = KnownZeroOut;
560 KnownOne = KnownOneOut;
561 break;
Chris Lattnerf0b24d22006-01-30 04:09:27 +0000562 case ISD::SELECT:
Wesley Peck527da1b2010-11-23 03:31:01 +0000563 if (SimplifyDemandedBits(Op.getOperand(2), NewMask, KnownZero,
Nate Begeman8a77efe2006-02-16 21:11:51 +0000564 KnownOne, TLO, Depth+1))
565 return true;
Dan Gohmanae2b6fb2008-02-27 00:25:32 +0000566 if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero2,
Nate Begeman8a77efe2006-02-16 21:11:51 +0000567 KnownOne2, TLO, Depth+1))
568 return true;
Wesley Peck527da1b2010-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 Begeman8a77efe2006-02-16 21:11:51 +0000572 // If the operands are constants, see if we can simplify them.
Dan Gohmanae2b6fb2008-02-27 00:25:32 +0000573 if (TLO.ShrinkDemandedConstant(Op, NewMask))
Nate Begeman8a77efe2006-02-16 21:11:51 +0000574 return true;
Wesley Peck527da1b2010-11-23 03:31:01 +0000575
Nate Begeman8a77efe2006-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 Lattner118ddba2006-02-26 23:36:02 +0000580 case ISD::SELECT_CC:
Wesley Peck527da1b2010-11-23 03:31:01 +0000581 if (SimplifyDemandedBits(Op.getOperand(3), NewMask, KnownZero,
Chris Lattner118ddba2006-02-26 23:36:02 +0000582 KnownOne, TLO, Depth+1))
583 return true;
Dan Gohmanae2b6fb2008-02-27 00:25:32 +0000584 if (SimplifyDemandedBits(Op.getOperand(2), NewMask, KnownZero2,
Chris Lattner118ddba2006-02-26 23:36:02 +0000585 KnownOne2, TLO, Depth+1))
586 return true;
Wesley Peck527da1b2010-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 Lattner118ddba2006-02-26 23:36:02 +0000590 // If the operands are constants, see if we can simplify them.
Dan Gohmanae2b6fb2008-02-27 00:25:32 +0000591 if (TLO.ShrinkDemandedConstant(Op, NewMask))
Chris Lattner118ddba2006-02-26 23:36:02 +0000592 return true;
Wesley Peck527da1b2010-11-23 03:31:01 +0000593
Chris Lattner118ddba2006-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 Lattnerf0b24d22006-01-30 04:09:27 +0000598 case ISD::SHL:
Nate Begeman8a77efe2006-02-16 21:11:51 +0000599 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +0000600 unsigned ShAmt = SA->getZExtValue();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000601 SDValue InOp = Op.getOperand(0);
Chris Lattner9a861a82007-04-17 21:14:16 +0000602
Dan Gohmanae2b6fb2008-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 Lattner9a861a82007-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 Gohmanae2b6fb2008-02-27 00:25:32 +0000612 if (ShAmt && (NewMask & APInt::getLowBitsSet(BitWidth, ShAmt)) == 0) {
Dan Gohmaneffb8942008-09-12 16:56:44 +0000613 unsigned C1= cast<ConstantSDNode>(InOp.getOperand(1))->getZExtValue();
Chris Lattner9a861a82007-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 Peck527da1b2010-11-23 03:31:01 +0000619 }
620
621 SDValue NewSA =
Chris Lattner397c4d92007-05-30 16:30:06 +0000622 TLO.DAG.getConstant(Diff, Op.getOperand(1).getValueType());
Owen Anderson53aa7a92009-08-10 22:56:29 +0000623 EVT VT = Op.getValueType();
Dale Johannesenf1163e92009-02-03 00:47:48 +0000624 return TLO.CombineTo(Op, TLO.DAG.getNode(Opc, dl, VT,
Chris Lattner9a861a82007-04-17 21:14:16 +0000625 InOp.getOperand(0), NewSA));
626 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000627 }
628
Dan Gohman08186842010-07-23 18:03:30 +0000629 if (SimplifyDemandedBits(InOp, NewMask.lshr(ShAmt),
Nate Begeman8a77efe2006-02-16 21:11:51 +0000630 KnownZero, KnownOne, TLO, Depth+1))
Chris Lattnerf0b24d22006-01-30 04:09:27 +0000631 return true;
Dan Gohman08186842010-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 Friedman053a7242011-12-09 01:16:26 +0000638 unsigned InnerBits = InnerVT.getSizeInBits();
639 if (ShAmt < InnerBits && NewMask.lshr(InnerBits) == 0 &&
Dan Gohman08186842010-07-23 18:03:30 +0000640 isTypeDesirableForOp(ISD::SHL, InnerVT)) {
Owen Andersonb2c80da2011-02-25 21:41:48 +0000641 EVT ShTy = getShiftAmountTy(InnerVT);
Dan Gohman55e24462010-07-23 21:08:12 +0000642 if (!APInt(BitWidth, ShAmt).isIntN(ShTy.getSizeInBits()))
643 ShTy = InnerVT;
Dan Gohman08186842010-07-23 18:03:30 +0000644 SDValue NarrowShl =
645 TLO.DAG.getNode(ISD::SHL, dl, InnerVT, InnerOp,
Dan Gohman55e24462010-07-23 21:08:12 +0000646 TLO.DAG.getConstant(ShAmt, ShTy));
Dan Gohman08186842010-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 Sandiford374a0e52013-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 Gohman08186842010-07-23 18:03:30 +0000677 }
678
Dan Gohmaneffb8942008-09-12 16:56:44 +0000679 KnownZero <<= SA->getZExtValue();
680 KnownOne <<= SA->getZExtValue();
Dan Gohmanae2b6fb2008-02-27 00:25:32 +0000681 // low bits known zero.
Dan Gohmaneffb8942008-09-12 16:56:44 +0000682 KnownZero |= APInt::getLowBitsSet(BitWidth, SA->getZExtValue());
Chris Lattnerf0b24d22006-01-30 04:09:27 +0000683 }
684 break;
Nate Begeman8a77efe2006-02-16 21:11:51 +0000685 case ISD::SRL:
686 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000687 EVT VT = Op.getValueType();
Dan Gohmaneffb8942008-09-12 16:56:44 +0000688 unsigned ShAmt = SA->getZExtValue();
Duncan Sands13237ac2008-06-06 12:08:01 +0000689 unsigned VTSize = VT.getSizeInBits();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000690 SDValue InOp = Op.getOperand(0);
Wesley Peck527da1b2010-11-23 03:31:01 +0000691
Dan Gohmanae2b6fb2008-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 Lattner9a861a82007-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 Gohmanae2b6fb2008-02-27 00:25:32 +0000701 if (ShAmt && (NewMask & APInt::getHighBitsSet(VTSize, ShAmt)) == 0) {
Dan Gohmaneffb8942008-09-12 16:56:44 +0000702 unsigned C1= cast<ConstantSDNode>(InOp.getOperand(1))->getZExtValue();
Chris Lattner9a861a82007-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 Peck527da1b2010-11-23 03:31:01 +0000708 }
709
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000710 SDValue NewSA =
Chris Lattner4aff52b2007-04-17 22:53:02 +0000711 TLO.DAG.getConstant(Diff, Op.getOperand(1).getValueType());
Dale Johannesenf1163e92009-02-03 00:47:48 +0000712 return TLO.CombineTo(Op, TLO.DAG.getNode(Opc, dl, VT,
Chris Lattner9a861a82007-04-17 21:14:16 +0000713 InOp.getOperand(0), NewSA));
714 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000715 }
716
Nate Begeman8a77efe2006-02-16 21:11:51 +0000717 // Compute the new bits that are at the top now.
Dan Gohmanae2b6fb2008-02-27 00:25:32 +0000718 if (SimplifyDemandedBits(InOp, (NewMask << ShAmt),
Nate Begeman8a77efe2006-02-16 21:11:51 +0000719 KnownZero, KnownOne, TLO, Depth+1))
720 return true;
Wesley Peck527da1b2010-11-23 03:31:01 +0000721 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanae2b6fb2008-02-27 00:25:32 +0000722 KnownZero = KnownZero.lshr(ShAmt);
723 KnownOne = KnownOne.lshr(ShAmt);
Chris Lattnerc5bb8ab2006-06-13 16:52:37 +0000724
Dan Gohmanae2b6fb2008-02-27 00:25:32 +0000725 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Chris Lattnerc5bb8ab2006-06-13 16:52:37 +0000726 KnownZero |= HighBits; // High bits known zero.
Nate Begeman8a77efe2006-02-16 21:11:51 +0000727 }
728 break;
729 case ISD::SRA:
Dan Gohmane58ab792009-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 Friedman053a7242011-12-09 01:16:26 +0000734 if (NewMask == 1)
Evan Chengf1bd5fc2010-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 Gohmane58ab792009-01-29 01:59:02 +0000738
Nate Begeman8a77efe2006-02-16 21:11:51 +0000739 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000740 EVT VT = Op.getValueType();
Dan Gohmaneffb8942008-09-12 16:56:44 +0000741 unsigned ShAmt = SA->getZExtValue();
Wesley Peck527da1b2010-11-23 03:31:01 +0000742
Dan Gohmanae2b6fb2008-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 Lattner10c65372006-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 Gohmanae2b6fb2008-02-27 00:25:32 +0000751 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
752 if (HighBits.intersects(NewMask))
Dan Gohman1d459e42009-12-11 21:31:27 +0000753 InDemandedMask |= APInt::getSignBit(VT.getScalarType().getSizeInBits());
Wesley Peck527da1b2010-11-23 03:31:01 +0000754
Chris Lattner10c65372006-05-08 17:22:53 +0000755 if (SimplifyDemandedBits(Op.getOperand(0), InDemandedMask,
Nate Begeman8a77efe2006-02-16 21:11:51 +0000756 KnownZero, KnownOne, TLO, Depth+1))
757 return true;
Wesley Peck527da1b2010-11-23 03:31:01 +0000758 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanae2b6fb2008-02-27 00:25:32 +0000759 KnownZero = KnownZero.lshr(ShAmt);
760 KnownOne = KnownOne.lshr(ShAmt);
Wesley Peck527da1b2010-11-23 03:31:01 +0000761
Dan Gohmanae2b6fb2008-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 Peck527da1b2010-11-23 03:31:01 +0000764
Nate Begeman8a77efe2006-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 Sandiford95f7ba92013-10-17 11:16:57 +0000767 if (KnownZero.intersects(SignBit) || (HighBits & ~NewMask) == HighBits)
Wesley Peck527da1b2010-11-23 03:31:01 +0000768 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, dl, VT,
Dale Johannesenf1163e92009-02-03 00:47:48 +0000769 Op.getOperand(0),
Nate Begeman8a77efe2006-02-16 21:11:51 +0000770 Op.getOperand(1)));
Richard Sandiford95f7ba92013-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 Begeman8a77efe2006-02-16 21:11:51 +0000780 }
Richard Sandiford95f7ba92013-10-17 11:16:57 +0000781
782 if (KnownOne.intersects(SignBit))
783 // New bits are known one.
784 KnownOne |= HighBits;
Nate Begeman8a77efe2006-02-16 21:11:51 +0000785 }
786 break;
787 case ISD::SIGN_EXTEND_INREG: {
Nadav Rotem57935242012-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 Friedman18a4c312012-01-31 01:08:03 +0000792 if (MsbMask == DemandedMask) {
Nadav Rotem57935242012-01-15 19:27:55 +0000793 unsigned ShAmt = ExVT.getScalarType().getSizeInBits();
794 SDValue InOp = Op.getOperand(0);
Eli Friedman18a4c312012-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 Rotem57935242012-01-15 19:27:55 +0000803 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SHL, dl,
804 Op.getValueType(), InOp, ShiftAmt));
805 }
Nate Begeman8a77efe2006-02-16 21:11:51 +0000806
Wesley Peck527da1b2010-11-23 03:31:01 +0000807 // Sign extension. Compute the demanded bits in the result that are not
Nate Begeman8a77efe2006-02-16 21:11:51 +0000808 // present in the input.
Dan Gohman6bd3ef82010-01-09 02:13:55 +0000809 APInt NewBits =
810 APInt::getHighBitsSet(BitWidth,
Nadav Rotem57935242012-01-15 19:27:55 +0000811 BitWidth - ExVT.getScalarType().getSizeInBits());
Wesley Peck527da1b2010-11-23 03:31:01 +0000812
Chris Lattner118ddba2006-02-26 23:36:02 +0000813 // If none of the extended bits are demanded, eliminate the sextinreg.
Eli Friedman460ad412010-08-02 04:42:25 +0000814 if ((NewBits & NewMask) == 0)
Chris Lattner118ddba2006-02-26 23:36:02 +0000815 return TLO.CombineTo(Op, Op.getOperand(0));
816
Jay Foad583abbc2010-12-07 08:25:19 +0000817 APInt InSignBit =
Nadav Rotem57935242012-01-15 19:27:55 +0000818 APInt::getSignBit(ExVT.getScalarType().getSizeInBits()).zext(BitWidth);
Dan Gohman6bd3ef82010-01-09 02:13:55 +0000819 APInt InputDemandedBits =
820 APInt::getLowBitsSet(BitWidth,
Nadav Rotem57935242012-01-15 19:27:55 +0000821 ExVT.getScalarType().getSizeInBits()) &
Dan Gohman6bd3ef82010-01-09 02:13:55 +0000822 NewMask;
Wesley Peck527da1b2010-11-23 03:31:01 +0000823
Chris Lattner118ddba2006-02-26 23:36:02 +0000824 // Since the sign extended bits are demanded, we know that the sign
Nate Begeman8a77efe2006-02-16 21:11:51 +0000825 // bit is demanded.
Chris Lattner118ddba2006-02-26 23:36:02 +0000826 InputDemandedBits |= InSignBit;
Nate Begeman8a77efe2006-02-16 21:11:51 +0000827
828 if (SimplifyDemandedBits(Op.getOperand(0), InputDemandedBits,
829 KnownZero, KnownOne, TLO, Depth+1))
830 return true;
Wesley Peck527da1b2010-11-23 03:31:01 +0000831 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Nate Begeman8a77efe2006-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 Peck527da1b2010-11-23 03:31:01 +0000835
Chris Lattner118ddba2006-02-26 23:36:02 +0000836 // If the input sign bit is known zero, convert this into a zero extension.
Dan Gohmanae2b6fb2008-02-27 00:25:32 +0000837 if (KnownZero.intersects(InSignBit))
Wesley Peck527da1b2010-11-23 03:31:01 +0000838 return TLO.CombineTo(Op,
Nadav Rotem57935242012-01-15 19:27:55 +0000839 TLO.DAG.getZeroExtendInReg(Op.getOperand(0),dl,ExVT));
Wesley Peck527da1b2010-11-23 03:31:01 +0000840
Dan Gohmanae2b6fb2008-02-27 00:25:32 +0000841 if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Nate Begeman8a77efe2006-02-16 21:11:51 +0000842 KnownOne |= NewBits;
843 KnownZero &= ~NewBits;
Chris Lattner118ddba2006-02-26 23:36:02 +0000844 } else { // Input sign bit unknown
Nate Begeman8a77efe2006-02-16 21:11:51 +0000845 KnownZero &= ~NewBits;
846 KnownOne &= ~NewBits;
847 }
848 break;
849 }
Chris Lattner118ddba2006-02-26 23:36:02 +0000850 case ISD::ZERO_EXTEND: {
Dan Gohman6bd3ef82010-01-09 02:13:55 +0000851 unsigned OperandBitWidth =
852 Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
Jay Foad583abbc2010-12-07 08:25:19 +0000853 APInt InMask = NewMask.trunc(OperandBitWidth);
Wesley Peck527da1b2010-11-23 03:31:01 +0000854
Chris Lattner118ddba2006-02-26 23:36:02 +0000855 // If none of the top bits are demanded, convert this into an any_extend.
Dan Gohmanae2b6fb2008-02-27 00:25:32 +0000856 APInt NewBits =
857 APInt::getHighBitsSet(BitWidth, BitWidth - OperandBitWidth) & NewMask;
858 if (!NewBits.intersects(NewMask))
Dale Johannesenf1163e92009-02-03 00:47:48 +0000859 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ANY_EXTEND, dl,
Wesley Peck527da1b2010-11-23 03:31:01 +0000860 Op.getValueType(),
Chris Lattner118ddba2006-02-26 23:36:02 +0000861 Op.getOperand(0)));
Wesley Peck527da1b2010-11-23 03:31:01 +0000862
Dan Gohmanae2b6fb2008-02-27 00:25:32 +0000863 if (SimplifyDemandedBits(Op.getOperand(0), InMask,
Chris Lattner118ddba2006-02-26 23:36:02 +0000864 KnownZero, KnownOne, TLO, Depth+1))
865 return true;
Wesley Peck527da1b2010-11-23 03:31:01 +0000866 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Jay Foad583abbc2010-12-07 08:25:19 +0000867 KnownZero = KnownZero.zext(BitWidth);
868 KnownOne = KnownOne.zext(BitWidth);
Chris Lattner118ddba2006-02-26 23:36:02 +0000869 KnownZero |= NewBits;
870 break;
871 }
872 case ISD::SIGN_EXTEND: {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000873 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman6bd3ef82010-01-09 02:13:55 +0000874 unsigned InBits = InVT.getScalarType().getSizeInBits();
Dan Gohmanae2b6fb2008-02-27 00:25:32 +0000875 APInt InMask = APInt::getLowBitsSet(BitWidth, InBits);
Dan Gohman44b4c072008-03-11 21:29:43 +0000876 APInt InSignBit = APInt::getBitsSet(BitWidth, InBits - 1, InBits);
Dan Gohmanae2b6fb2008-02-27 00:25:32 +0000877 APInt NewBits = ~InMask & NewMask;
Wesley Peck527da1b2010-11-23 03:31:01 +0000878
Chris Lattner118ddba2006-02-26 23:36:02 +0000879 // If none of the top bits are demanded, convert this into an any_extend.
880 if (NewBits == 0)
Dale Johannesenf1163e92009-02-03 00:47:48 +0000881 return TLO.CombineTo(Op,TLO.DAG.getNode(ISD::ANY_EXTEND, dl,
882 Op.getValueType(),
883 Op.getOperand(0)));
Wesley Peck527da1b2010-11-23 03:31:01 +0000884
Chris Lattner118ddba2006-02-26 23:36:02 +0000885 // Since some of the sign extended bits are demanded, we know that the sign
886 // bit is demanded.
Dan Gohmanae2b6fb2008-02-27 00:25:32 +0000887 APInt InDemandedBits = InMask & NewMask;
Chris Lattner118ddba2006-02-26 23:36:02 +0000888 InDemandedBits |= InSignBit;
Jay Foad583abbc2010-12-07 08:25:19 +0000889 InDemandedBits = InDemandedBits.trunc(InBits);
Wesley Peck527da1b2010-11-23 03:31:01 +0000890
891 if (SimplifyDemandedBits(Op.getOperand(0), InDemandedBits, KnownZero,
Chris Lattner118ddba2006-02-26 23:36:02 +0000892 KnownOne, TLO, Depth+1))
893 return true;
Jay Foad583abbc2010-12-07 08:25:19 +0000894 KnownZero = KnownZero.zext(BitWidth);
895 KnownOne = KnownOne.zext(BitWidth);
Wesley Peck527da1b2010-11-23 03:31:01 +0000896
Chris Lattner118ddba2006-02-26 23:36:02 +0000897 // If the sign bit is known zero, convert this to a zero extend.
Dan Gohmanae2b6fb2008-02-27 00:25:32 +0000898 if (KnownZero.intersects(InSignBit))
Dale Johannesenf1163e92009-02-03 00:47:48 +0000899 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ZERO_EXTEND, dl,
Wesley Peck527da1b2010-11-23 03:31:01 +0000900 Op.getValueType(),
Chris Lattner118ddba2006-02-26 23:36:02 +0000901 Op.getOperand(0)));
Wesley Peck527da1b2010-11-23 03:31:01 +0000902
Chris Lattner118ddba2006-02-26 23:36:02 +0000903 // If the sign bit is known one, the top bits match.
Dan Gohmanae2b6fb2008-02-27 00:25:32 +0000904 if (KnownOne.intersects(InSignBit)) {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +0000905 KnownOne |= NewBits;
906 assert((KnownZero & NewBits) == 0);
Chris Lattner118ddba2006-02-26 23:36:02 +0000907 } else { // Otherwise, top bits aren't known.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +0000908 assert((KnownOne & NewBits) == 0);
909 assert((KnownZero & NewBits) == 0);
Chris Lattner118ddba2006-02-26 23:36:02 +0000910 }
911 break;
912 }
913 case ISD::ANY_EXTEND: {
Dan Gohman6bd3ef82010-01-09 02:13:55 +0000914 unsigned OperandBitWidth =
915 Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
Jay Foad583abbc2010-12-07 08:25:19 +0000916 APInt InMask = NewMask.trunc(OperandBitWidth);
Dan Gohmanae2b6fb2008-02-27 00:25:32 +0000917 if (SimplifyDemandedBits(Op.getOperand(0), InMask,
Chris Lattner118ddba2006-02-26 23:36:02 +0000918 KnownZero, KnownOne, TLO, Depth+1))
919 return true;
Wesley Peck527da1b2010-11-23 03:31:01 +0000920 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Jay Foad583abbc2010-12-07 08:25:19 +0000921 KnownZero = KnownZero.zext(BitWidth);
922 KnownOne = KnownOne.zext(BitWidth);
Chris Lattner118ddba2006-02-26 23:36:02 +0000923 break;
924 }
Chris Lattner0f649322006-05-05 22:32:12 +0000925 case ISD::TRUNCATE: {
Chris Lattner86a14672006-05-06 00:11:52 +0000926 // Simplify the input, using demanded bit information, and compute the known
927 // zero/one bits live out.
Dan Gohmanc3c3c682010-03-01 17:59:21 +0000928 unsigned OperandBitWidth =
929 Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
Jay Foad583abbc2010-12-07 08:25:19 +0000930 APInt TruncMask = NewMask.zext(OperandBitWidth);
Dan Gohmanae2b6fb2008-02-27 00:25:32 +0000931 if (SimplifyDemandedBits(Op.getOperand(0), TruncMask,
Chris Lattner0f649322006-05-05 22:32:12 +0000932 KnownZero, KnownOne, TLO, Depth+1))
933 return true;
Jay Foad583abbc2010-12-07 08:25:19 +0000934 KnownZero = KnownZero.trunc(BitWidth);
935 KnownOne = KnownOne.trunc(BitWidth);
Wesley Peck527da1b2010-11-23 03:31:01 +0000936
Chris Lattner86a14672006-05-06 00:11:52 +0000937 // If the input is only used by this truncate, see if we can shrink it based
938 // on the known demanded bits.
Gabor Greiff304a7a2008-08-28 21:40:38 +0000939 if (Op.getOperand(0).getNode()->hasOneUse()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000940 SDValue In = Op.getOperand(0);
Chris Lattner86a14672006-05-06 00:11:52 +0000941 switch (In.getOpcode()) {
942 default: break;
943 case ISD::SRL:
944 // Shrink SRL by a constant if none of the high bits shifted in are
945 // demanded.
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000946 if (TLO.LegalTypes() &&
947 !isTypeDesirableForOp(ISD::SRL, Op.getValueType()))
948 // Do not turn (vt1 truncate (vt2 srl)) into (vt1 srl) if vt1 is
949 // undesirable.
950 break;
951 ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(In.getOperand(1));
952 if (!ShAmt)
953 break;
Owen Anderson9c128342011-04-13 23:22:23 +0000954 SDValue Shift = In.getOperand(1);
955 if (TLO.LegalTypes()) {
956 uint64_t ShVal = ShAmt->getZExtValue();
957 Shift =
958 TLO.DAG.getConstant(ShVal, getShiftAmountTy(Op.getValueType()));
959 }
960
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000961 APInt HighBits = APInt::getHighBitsSet(OperandBitWidth,
962 OperandBitWidth - BitWidth);
Jay Foad583abbc2010-12-07 08:25:19 +0000963 HighBits = HighBits.lshr(ShAmt->getZExtValue()).trunc(BitWidth);
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000964
965 if (ShAmt->getZExtValue() < BitWidth && !(HighBits & NewMask)) {
966 // None of the shifted in bits are needed. Add a truncate of the
967 // shift input, then shift it.
968 SDValue NewTrunc = TLO.DAG.getNode(ISD::TRUNCATE, dl,
Wesley Peck527da1b2010-11-23 03:31:01 +0000969 Op.getValueType(),
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000970 In.getOperand(0));
971 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, dl,
972 Op.getValueType(),
Wesley Peck527da1b2010-11-23 03:31:01 +0000973 NewTrunc,
Owen Anderson9c128342011-04-13 23:22:23 +0000974 Shift));
Chris Lattner86a14672006-05-06 00:11:52 +0000975 }
976 break;
977 }
978 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000979
980 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Chris Lattner0f649322006-05-05 22:32:12 +0000981 break;
982 }
Chris Lattner118ddba2006-02-26 23:36:02 +0000983 case ISD::AssertZext: {
Owen Anderson40d756e2011-09-03 00:26:49 +0000984 // AssertZext demands all of the high bits, plus any of the low bits
985 // demanded by its users.
986 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
987 APInt InMask = APInt::getLowBitsSet(BitWidth,
988 VT.getSizeInBits());
989 if (SimplifyDemandedBits(Op.getOperand(0), ~InMask | NewMask,
Chris Lattner118ddba2006-02-26 23:36:02 +0000990 KnownZero, KnownOne, TLO, Depth+1))
991 return true;
Wesley Peck527da1b2010-11-23 03:31:01 +0000992 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmand83e3e72010-06-03 20:21:33 +0000993
Dan Gohmanae2b6fb2008-02-27 00:25:32 +0000994 KnownZero |= ~InMask & NewMask;
Chris Lattner118ddba2006-02-26 23:36:02 +0000995 break;
996 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000997 case ISD::BITCAST:
Stuart Hastingsbee6fcc2011-06-06 16:44:31 +0000998 // If this is an FP->Int bitcast and if the sign bit is the only
999 // thing demanded, turn this into a FGETSIGN.
Eli Friedman2ec82492011-12-15 02:07:20 +00001000 if (!TLO.LegalOperations() &&
1001 !Op.getValueType().isVector() &&
Eli Friedman53218b62011-11-09 22:25:12 +00001002 !Op.getOperand(0).getValueType().isVector() &&
Nadav Rotem504cf0c2011-06-12 14:56:55 +00001003 NewMask == APInt::getSignBit(Op.getValueType().getSizeInBits()) &&
1004 Op.getOperand(0).getValueType().isFloatingPoint()) {
Stuart Hastingsbee6fcc2011-06-06 16:44:31 +00001005 bool OpVTLegal = isOperationLegalOrCustom(ISD::FGETSIGN, Op.getValueType());
1006 bool i32Legal = isOperationLegalOrCustom(ISD::FGETSIGN, MVT::i32);
1007 if ((OpVTLegal || i32Legal) && Op.getValueType().isSimple()) {
1008 EVT Ty = OpVTLegal ? Op.getValueType() : MVT::i32;
Chris Lattnerde272b12007-12-22 21:35:38 +00001009 // Make a FGETSIGN + SHL to move the sign bit into the appropriate
1010 // place. We expect the SHL to be eliminated by other optimizations.
Stuart Hastings3ae49c02011-06-01 18:32:25 +00001011 SDValue Sign = TLO.DAG.getNode(ISD::FGETSIGN, dl, Ty, Op.getOperand(0));
Stuart Hastingsbee6fcc2011-06-06 16:44:31 +00001012 unsigned OpVTSizeInBits = Op.getValueType().getSizeInBits();
1013 if (!OpVTLegal && OpVTSizeInBits > 32)
Stuart Hastings3ae49c02011-06-01 18:32:25 +00001014 Sign = TLO.DAG.getNode(ISD::ZERO_EXTEND, dl, Op.getValueType(), Sign);
Duncan Sands13237ac2008-06-06 12:08:01 +00001015 unsigned ShVal = Op.getValueType().getSizeInBits()-1;
Stuart Hastingsfd5ecd02011-06-01 14:04:17 +00001016 SDValue ShAmt = TLO.DAG.getConstant(ShVal, Op.getValueType());
Stuart Hastings4a4e5a22011-05-19 18:48:20 +00001017 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SHL, dl,
1018 Op.getValueType(),
Chris Lattnerde272b12007-12-22 21:35:38 +00001019 Sign, ShAmt));
1020 }
1021 }
Chris Lattnerde272b12007-12-22 21:35:38 +00001022 break;
Dan Gohmanad3e5492009-04-08 00:15:30 +00001023 case ISD::ADD:
1024 case ISD::MUL:
1025 case ISD::SUB: {
1026 // Add, Sub, and Mul don't demand any bits in positions beyond that
1027 // of the highest bit demanded of them.
1028 APInt LoMask = APInt::getLowBitsSet(BitWidth,
1029 BitWidth - NewMask.countLeadingZeros());
1030 if (SimplifyDemandedBits(Op.getOperand(0), LoMask, KnownZero2,
1031 KnownOne2, TLO, Depth+1))
1032 return true;
1033 if (SimplifyDemandedBits(Op.getOperand(1), LoMask, KnownZero2,
1034 KnownOne2, TLO, Depth+1))
1035 return true;
1036 // See if the operation should be performed at a smaller bit width.
Dan Gohman600f62b2010-06-24 14:30:44 +00001037 if (TLO.ShrinkDemandedOp(Op, BitWidth, NewMask, dl))
Dan Gohmanad3e5492009-04-08 00:15:30 +00001038 return true;
1039 }
1040 // FALL THROUGH
Dan Gohman38dc08f2008-05-06 00:53:29 +00001041 default:
Chris Lattnere6025522006-04-02 06:15:09 +00001042 // Just use ComputeMaskedBits to compute output bits.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001043 TLO.DAG.ComputeMaskedBits(Op, KnownZero, KnownOne, Depth);
Chris Lattnerab816402006-02-27 01:00:42 +00001044 break;
Nate Begeman8a77efe2006-02-16 21:11:51 +00001045 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001046
Chris Lattner118ddba2006-02-26 23:36:02 +00001047 // If we know the value of all of the demanded bits, return this as a
1048 // constant.
Dan Gohmanae2b6fb2008-02-27 00:25:32 +00001049 if ((NewMask & (KnownZero|KnownOne)) == NewMask)
Chris Lattner118ddba2006-02-26 23:36:02 +00001050 return TLO.CombineTo(Op, TLO.DAG.getConstant(KnownOne, Op.getValueType()));
Wesley Peck527da1b2010-11-23 03:31:01 +00001051
Nate Begeman8a77efe2006-02-16 21:11:51 +00001052 return false;
1053}
1054
Wesley Peck527da1b2010-11-23 03:31:01 +00001055/// computeMaskedBitsForTargetNode - Determine which of the bits specified
1056/// in Mask are known to be either zero or one and return them in the
Nate Begeman8a77efe2006-02-16 21:11:51 +00001057/// KnownZero/KnownOne bitsets.
Wesley Peck527da1b2010-11-23 03:31:01 +00001058void TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Wesley Peck527da1b2010-11-23 03:31:01 +00001059 APInt &KnownZero,
Dan Gohmanf990faf2008-02-13 00:35:47 +00001060 APInt &KnownOne,
Dan Gohman309d3d52007-06-22 14:59:07 +00001061 const SelectionDAG &DAG,
Nate Begeman8a77efe2006-02-16 21:11:51 +00001062 unsigned Depth) const {
Chris Lattner6c1321c2006-04-02 06:19:46 +00001063 assert((Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1064 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1065 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1066 Op.getOpcode() == ISD::INTRINSIC_VOID) &&
Chris Lattnerf0b24d22006-01-30 04:09:27 +00001067 "Should use MaskedValueIsZero if you don't know whether Op"
1068 " is a target node!");
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001069 KnownZero = KnownOne = APInt(KnownOne.getBitWidth(), 0);
Evan Cheng9cdc16c2005-12-21 23:05:39 +00001070}
Chris Lattner32fef532006-01-26 20:37:03 +00001071
Chris Lattner7206d742006-05-06 09:27:13 +00001072/// ComputeNumSignBitsForTargetNode - This method can be implemented by
1073/// targets that want to expose additional information about sign bits to the
1074/// DAG Combiner.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001075unsigned TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
Chris Lattner7206d742006-05-06 09:27:13 +00001076 unsigned Depth) const {
1077 assert((Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1078 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1079 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1080 Op.getOpcode() == ISD::INTRINSIC_VOID) &&
1081 "Should use ComputeNumSignBits if you don't know whether Op"
1082 " is a target node!");
1083 return 1;
1084}
1085
Dan Gohmanaaee6c92009-02-15 23:59:32 +00001086/// ValueHasExactlyOneBitSet - Test if the given value is known to have exactly
1087/// one bit set. This differs from ComputeMaskedBits in that it doesn't need to
1088/// determine which bit is set.
1089///
Dale Johannesencc5fc442009-02-11 19:19:41 +00001090static bool ValueHasExactlyOneBitSet(SDValue Val, const SelectionDAG &DAG) {
Dan Gohmanaaee6c92009-02-15 23:59:32 +00001091 // A left-shift of a constant one will have exactly one bit set, because
1092 // shifting the bit off the end is undefined.
1093 if (Val.getOpcode() == ISD::SHL)
1094 if (ConstantSDNode *C =
1095 dyn_cast<ConstantSDNode>(Val.getNode()->getOperand(0)))
1096 if (C->getAPIntValue() == 1)
1097 return true;
Dan Gohmane58ab792009-01-29 01:59:02 +00001098
Dan Gohmanaaee6c92009-02-15 23:59:32 +00001099 // Similarly, a right-shift of a constant sign-bit will have exactly
1100 // one bit set.
1101 if (Val.getOpcode() == ISD::SRL)
1102 if (ConstantSDNode *C =
1103 dyn_cast<ConstantSDNode>(Val.getNode()->getOperand(0)))
1104 if (C->getAPIntValue().isSignBit())
1105 return true;
1106
1107 // More could be done here, though the above checks are enough
1108 // to handle some common cases.
1109
1110 // Fall back to ComputeMaskedBits to catch other known cases.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001111 EVT OpVT = Val.getValueType();
Dan Gohman4cec5432010-03-02 02:14:38 +00001112 unsigned BitWidth = OpVT.getScalarType().getSizeInBits();
Dan Gohmane58ab792009-01-29 01:59:02 +00001113 APInt KnownZero, KnownOne;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001114 DAG.ComputeMaskedBits(Val, KnownZero, KnownOne);
Dale Johannesencc5fc442009-02-11 19:19:41 +00001115 return (KnownZero.countPopulation() == BitWidth - 1) &&
1116 (KnownOne.countPopulation() == 1);
Dan Gohmane58ab792009-01-29 01:59:02 +00001117}
Chris Lattner7206d742006-05-06 09:27:13 +00001118
Wesley Peck527da1b2010-11-23 03:31:01 +00001119/// SimplifySetCC - Try to simplify a setcc built with the specified operands
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001120/// and cc. If it is unable to simplify it, return a null SDValue.
1121SDValue
Owen Anderson53aa7a92009-08-10 22:56:29 +00001122TargetLowering::SimplifySetCC(EVT VT, SDValue N0, SDValue N1,
Evan Cheng92658d52007-02-08 22:13:59 +00001123 ISD::CondCode Cond, bool foldBooleans,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001124 DAGCombinerInfo &DCI, SDLoc dl) const {
Evan Cheng92658d52007-02-08 22:13:59 +00001125 SelectionDAG &DAG = DCI.DAG;
1126
1127 // These setcc operations always fold.
1128 switch (Cond) {
1129 default: break;
1130 case ISD::SETFALSE:
1131 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
1132 case ISD::SETTRUE:
Tim Northover950fcc02013-09-06 12:38:12 +00001133 case ISD::SETTRUE2: {
1134 TargetLowering::BooleanContent Cnt = getBooleanContents(VT.isVector());
1135 return DAG.getConstant(
1136 Cnt == TargetLowering::ZeroOrNegativeOneBooleanContent ? -1ULL : 1, VT);
1137 }
Evan Cheng92658d52007-02-08 22:13:59 +00001138 }
1139
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001140 // Ensure that the constant occurs on the RHS, and fold constant
1141 // comparisons.
Tom Stellardcd428182013-09-28 02:50:38 +00001142 ISD::CondCode SwappedCC = ISD::getSetCCSwappedOperands(Cond);
1143 if (isa<ConstantSDNode>(N0.getNode()) &&
1144 (DCI.isBeforeLegalizeOps() ||
1145 isCondCodeLegal(SwappedCC, N0.getSimpleValueType())))
1146 return DAG.getSetCC(dl, VT, N1, N0, SwappedCC);
Eric Christopher5bbb2bd2011-06-17 20:41:29 +00001147
Gabor Greiff304a7a2008-08-28 21:40:38 +00001148 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
Dan Gohman2fa65b72008-03-03 22:22:56 +00001149 const APInt &C1 = N1C->getAPIntValue();
Dale Johannesen7aad5422008-11-07 01:28:02 +00001150
Eli Friedman65919b52009-07-26 23:47:17 +00001151 // If the LHS is '(srl (ctlz x), 5)', the RHS is 0/1, and this is an
1152 // equality comparison, then we're just comparing whether X itself is
1153 // zero.
1154 if (N0.getOpcode() == ISD::SRL && (C1 == 0 || C1 == 1) &&
1155 N0.getOperand(0).getOpcode() == ISD::CTLZ &&
1156 N0.getOperand(1).getOpcode() == ISD::Constant) {
Evan Cheng16b75ce2010-01-07 20:58:44 +00001157 const APInt &ShAmt
1158 = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Eli Friedman65919b52009-07-26 23:47:17 +00001159 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
1160 ShAmt == Log2_32(N0.getValueType().getSizeInBits())) {
1161 if ((C1 == 0) == (Cond == ISD::SETEQ)) {
1162 // (srl (ctlz x), 5) == 0 -> X != 0
1163 // (srl (ctlz x), 5) != 1 -> X != 0
1164 Cond = ISD::SETNE;
1165 } else {
1166 // (srl (ctlz x), 5) != 0 -> X == 0
1167 // (srl (ctlz x), 5) == 1 -> X == 0
1168 Cond = ISD::SETEQ;
1169 }
1170 SDValue Zero = DAG.getConstant(0, N0.getValueType());
1171 return DAG.getSetCC(dl, VT, N0.getOperand(0).getOperand(0),
1172 Zero, Cond);
1173 }
1174 }
1175
Benjamin Kramer24c5184d2011-01-17 12:04:57 +00001176 SDValue CTPOP = N0;
1177 // Look through truncs that don't change the value of a ctpop.
1178 if (N0.hasOneUse() && N0.getOpcode() == ISD::TRUNCATE)
1179 CTPOP = N0.getOperand(0);
1180
1181 if (CTPOP.hasOneUse() && CTPOP.getOpcode() == ISD::CTPOP &&
Benjamin Kramer45d183c2011-01-17 18:00:28 +00001182 (N0 == CTPOP || N0.getValueType().getSizeInBits() >
Benjamin Kramer24c5184d2011-01-17 12:04:57 +00001183 Log2_32_Ceil(CTPOP.getValueType().getSizeInBits()))) {
1184 EVT CTVT = CTPOP.getValueType();
1185 SDValue CTOp = CTPOP.getOperand(0);
1186
1187 // (ctpop x) u< 2 -> (x & x-1) == 0
1188 // (ctpop x) u> 1 -> (x & x-1) != 0
1189 if ((Cond == ISD::SETULT && C1 == 2) || (Cond == ISD::SETUGT && C1 == 1)){
1190 SDValue Sub = DAG.getNode(ISD::SUB, dl, CTVT, CTOp,
1191 DAG.getConstant(1, CTVT));
1192 SDValue And = DAG.getNode(ISD::AND, dl, CTVT, CTOp, Sub);
1193 ISD::CondCode CC = Cond == ISD::SETULT ? ISD::SETEQ : ISD::SETNE;
1194 return DAG.getSetCC(dl, VT, And, DAG.getConstant(0, CTVT), CC);
1195 }
1196
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001197 // TODO: (ctpop x) == 1 -> x && (x & x-1) == 0 iff ctpop is illegal.
Benjamin Kramer24c5184d2011-01-17 12:04:57 +00001198 }
1199
Benjamin Kramer341c11d2011-04-22 18:47:44 +00001200 // (zext x) == C --> x == (trunc C)
1201 if (DCI.isBeforeLegalize() && N0->hasOneUse() &&
1202 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
1203 unsigned MinBits = N0.getValueSizeInBits();
1204 SDValue PreZExt;
1205 if (N0->getOpcode() == ISD::ZERO_EXTEND) {
1206 // ZExt
1207 MinBits = N0->getOperand(0).getValueSizeInBits();
1208 PreZExt = N0->getOperand(0);
1209 } else if (N0->getOpcode() == ISD::AND) {
1210 // DAGCombine turns costly ZExts into ANDs
1211 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0->getOperand(1)))
1212 if ((C->getAPIntValue()+1).isPowerOf2()) {
1213 MinBits = C->getAPIntValue().countTrailingOnes();
1214 PreZExt = N0->getOperand(0);
1215 }
1216 } else if (LoadSDNode *LN0 = dyn_cast<LoadSDNode>(N0)) {
1217 // ZEXTLOAD
1218 if (LN0->getExtensionType() == ISD::ZEXTLOAD) {
1219 MinBits = LN0->getMemoryVT().getSizeInBits();
1220 PreZExt = N0;
1221 }
1222 }
1223
Benjamin Kramerbde91762012-06-02 10:20:22 +00001224 // Make sure we're not losing bits from the constant.
Benjamin Kramer8aaf1972013-05-21 08:51:09 +00001225 if (MinBits > 0 &&
1226 MinBits < C1.getBitWidth() && MinBits >= C1.getActiveBits()) {
Benjamin Kramer341c11d2011-04-22 18:47:44 +00001227 EVT MinVT = EVT::getIntegerVT(*DAG.getContext(), MinBits);
1228 if (isTypeDesirableForOp(ISD::SETCC, MinVT)) {
1229 // Will get folded away.
1230 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, MinVT, PreZExt);
1231 SDValue C = DAG.getConstant(C1.trunc(MinBits), MinVT);
1232 return DAG.getSetCC(dl, VT, Trunc, C, Cond);
1233 }
1234 }
1235 }
1236
Eli Friedman65919b52009-07-26 23:47:17 +00001237 // If the LHS is '(and load, const)', the RHS is 0,
1238 // the test is for equality or unsigned, and all 1 bits of the const are
1239 // in the same partial word, see if we can shorten the load.
1240 if (DCI.isBeforeLegalize() &&
Eli Friedmana961d692013-09-24 22:50:14 +00001241 !ISD::isSignedIntSetCC(Cond) &&
Eli Friedman65919b52009-07-26 23:47:17 +00001242 N0.getOpcode() == ISD::AND && C1 == 0 &&
1243 N0.getNode()->hasOneUse() &&
1244 isa<LoadSDNode>(N0.getOperand(0)) &&
1245 N0.getOperand(0).getNode()->hasOneUse() &&
1246 isa<ConstantSDNode>(N0.getOperand(1))) {
1247 LoadSDNode *Lod = cast<LoadSDNode>(N0.getOperand(0));
Evan Cheng16b75ce2010-01-07 20:58:44 +00001248 APInt bestMask;
Eli Friedman65919b52009-07-26 23:47:17 +00001249 unsigned bestWidth = 0, bestOffset = 0;
Evan Cheng16b75ce2010-01-07 20:58:44 +00001250 if (!Lod->isVolatile() && Lod->isUnindexed()) {
Eli Friedman65919b52009-07-26 23:47:17 +00001251 unsigned origWidth = N0.getValueType().getSizeInBits();
Evan Cheng16b75ce2010-01-07 20:58:44 +00001252 unsigned maskWidth = origWidth;
Wesley Peck527da1b2010-11-23 03:31:01 +00001253 // We can narrow (e.g.) 16-bit extending loads on 32-bit target to
Eli Friedman65919b52009-07-26 23:47:17 +00001254 // 8 bits, but have to be careful...
1255 if (Lod->getExtensionType() != ISD::NON_EXTLOAD)
1256 origWidth = Lod->getMemoryVT().getSizeInBits();
Evan Cheng16b75ce2010-01-07 20:58:44 +00001257 const APInt &Mask =
1258 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Eli Friedman65919b52009-07-26 23:47:17 +00001259 for (unsigned width = origWidth / 2; width>=8; width /= 2) {
Evan Cheng16b75ce2010-01-07 20:58:44 +00001260 APInt newMask = APInt::getLowBitsSet(maskWidth, width);
Eli Friedman65919b52009-07-26 23:47:17 +00001261 for (unsigned offset=0; offset<origWidth/width; offset++) {
1262 if ((newMask & Mask) == Mask) {
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00001263 if (!getDataLayout()->isLittleEndian())
Eli Friedman65919b52009-07-26 23:47:17 +00001264 bestOffset = (origWidth/width - offset - 1) * (width/8);
1265 else
1266 bestOffset = (uint64_t)offset * (width/8);
Evan Cheng16b75ce2010-01-07 20:58:44 +00001267 bestMask = Mask.lshr(offset * (width/8) * 8);
Eli Friedman65919b52009-07-26 23:47:17 +00001268 bestWidth = width;
1269 break;
Dale Johannesen7aad5422008-11-07 01:28:02 +00001270 }
Eli Friedman65919b52009-07-26 23:47:17 +00001271 newMask = newMask << width;
Dale Johannesen7aad5422008-11-07 01:28:02 +00001272 }
1273 }
1274 }
Eli Friedman65919b52009-07-26 23:47:17 +00001275 if (bestWidth) {
Chris Lattner493b3e72011-04-14 04:12:47 +00001276 EVT newVT = EVT::getIntegerVT(*DAG.getContext(), bestWidth);
Eli Friedman65919b52009-07-26 23:47:17 +00001277 if (newVT.isRound()) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001278 EVT PtrType = Lod->getOperand(1).getValueType();
Eli Friedman65919b52009-07-26 23:47:17 +00001279 SDValue Ptr = Lod->getBasePtr();
1280 if (bestOffset != 0)
1281 Ptr = DAG.getNode(ISD::ADD, dl, PtrType, Lod->getBasePtr(),
1282 DAG.getConstant(bestOffset, PtrType));
1283 unsigned NewAlign = MinAlign(Lod->getAlignment(), bestOffset);
1284 SDValue NewLoad = DAG.getLoad(newVT, dl, Lod->getChain(), Ptr,
Chris Lattner1ffcf522010-09-21 16:36:31 +00001285 Lod->getPointerInfo().getWithOffset(bestOffset),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001286 false, false, false, NewAlign);
Wesley Peck527da1b2010-11-23 03:31:01 +00001287 return DAG.getSetCC(dl, VT,
Eli Friedman65919b52009-07-26 23:47:17 +00001288 DAG.getNode(ISD::AND, dl, newVT, NewLoad,
Evan Cheng16b75ce2010-01-07 20:58:44 +00001289 DAG.getConstant(bestMask.trunc(bestWidth),
1290 newVT)),
Eli Friedman65919b52009-07-26 23:47:17 +00001291 DAG.getConstant(0LL, newVT), Cond);
Evan Cheng92658d52007-02-08 22:13:59 +00001292 }
Eli Friedman65919b52009-07-26 23:47:17 +00001293 }
1294 }
Evan Cheng92658d52007-02-08 22:13:59 +00001295
Eli Friedman65919b52009-07-26 23:47:17 +00001296 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
1297 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
1298 unsigned InSize = N0.getOperand(0).getValueType().getSizeInBits();
1299
1300 // If the comparison constant has bits in the upper part, the
1301 // zero-extended value could never match.
1302 if (C1.intersects(APInt::getHighBitsSet(C1.getBitWidth(),
1303 C1.getBitWidth() - InSize))) {
Evan Cheng92658d52007-02-08 22:13:59 +00001304 switch (Cond) {
Evan Cheng92658d52007-02-08 22:13:59 +00001305 case ISD::SETUGT:
1306 case ISD::SETUGE:
Eli Friedman65919b52009-07-26 23:47:17 +00001307 case ISD::SETEQ: return DAG.getConstant(0, VT);
Evan Cheng92658d52007-02-08 22:13:59 +00001308 case ISD::SETULT:
Eli Friedman65919b52009-07-26 23:47:17 +00001309 case ISD::SETULE:
1310 case ISD::SETNE: return DAG.getConstant(1, VT);
1311 case ISD::SETGT:
1312 case ISD::SETGE:
1313 // True if the sign bit of C1 is set.
1314 return DAG.getConstant(C1.isNegative(), VT);
1315 case ISD::SETLT:
1316 case ISD::SETLE:
1317 // True if the sign bit of C1 isn't set.
1318 return DAG.getConstant(C1.isNonNegative(), VT);
1319 default:
Jakob Stoklund Olesen1ae07362009-07-24 18:22:59 +00001320 break;
1321 }
Eli Friedman65919b52009-07-26 23:47:17 +00001322 }
Evan Cheng92658d52007-02-08 22:13:59 +00001323
Eli Friedman65919b52009-07-26 23:47:17 +00001324 // Otherwise, we can perform the comparison with the low bits.
1325 switch (Cond) {
1326 case ISD::SETEQ:
1327 case ISD::SETNE:
1328 case ISD::SETUGT:
1329 case ISD::SETUGE:
1330 case ISD::SETULT:
1331 case ISD::SETULE: {
Patrik Hagglunde98b7a02012-12-11 11:14:33 +00001332 EVT newVT = N0.getOperand(0).getValueType();
Eli Friedman65919b52009-07-26 23:47:17 +00001333 if (DCI.isBeforeLegalizeOps() ||
1334 (isOperationLegal(ISD::SETCC, newVT) &&
Patrik Hagglunddeee9002012-12-19 10:09:26 +00001335 getCondCodeAction(Cond, newVT.getSimpleVT())==Legal))
Eli Friedman65919b52009-07-26 23:47:17 +00001336 return DAG.getSetCC(dl, VT, N0.getOperand(0),
Jay Foad583abbc2010-12-07 08:25:19 +00001337 DAG.getConstant(C1.trunc(InSize), newVT),
Eli Friedman65919b52009-07-26 23:47:17 +00001338 Cond);
1339 break;
1340 }
1341 default:
1342 break; // todo, be more careful with signed comparisons
1343 }
1344 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Evan Cheng228c31f2010-02-27 07:36:59 +00001345 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001346 EVT ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
Eli Friedman65919b52009-07-26 23:47:17 +00001347 unsigned ExtSrcTyBits = ExtSrcTy.getSizeInBits();
Owen Anderson53aa7a92009-08-10 22:56:29 +00001348 EVT ExtDstTy = N0.getValueType();
Eli Friedman65919b52009-07-26 23:47:17 +00001349 unsigned ExtDstTyBits = ExtDstTy.getSizeInBits();
1350
Eli Friedmanffe64c02010-07-30 06:44:31 +00001351 // If the constant doesn't fit into the number of bits for the source of
1352 // the sign extension, it is impossible for both sides to be equal.
1353 if (C1.getMinSignedBits() > ExtSrcTyBits)
Eli Friedman65919b52009-07-26 23:47:17 +00001354 return DAG.getConstant(Cond == ISD::SETNE, VT);
Wesley Peck527da1b2010-11-23 03:31:01 +00001355
Eli Friedman65919b52009-07-26 23:47:17 +00001356 SDValue ZextOp;
Owen Anderson53aa7a92009-08-10 22:56:29 +00001357 EVT Op0Ty = N0.getOperand(0).getValueType();
Eli Friedman65919b52009-07-26 23:47:17 +00001358 if (Op0Ty == ExtSrcTy) {
1359 ZextOp = N0.getOperand(0);
1360 } else {
1361 APInt Imm = APInt::getLowBitsSet(ExtDstTyBits, ExtSrcTyBits);
1362 ZextOp = DAG.getNode(ISD::AND, dl, Op0Ty, N0.getOperand(0),
1363 DAG.getConstant(Imm, Op0Ty));
1364 }
1365 if (!DCI.isCalledByLegalizer())
1366 DCI.AddToWorklist(ZextOp.getNode());
1367 // Otherwise, make this a use of a zext.
Wesley Peck527da1b2010-11-23 03:31:01 +00001368 return DAG.getSetCC(dl, VT, ZextOp,
Eli Friedman65919b52009-07-26 23:47:17 +00001369 DAG.getConstant(C1 & APInt::getLowBitsSet(
1370 ExtDstTyBits,
Wesley Peck527da1b2010-11-23 03:31:01 +00001371 ExtSrcTyBits),
Eli Friedman65919b52009-07-26 23:47:17 +00001372 ExtDstTy),
1373 Cond);
1374 } else if ((N1C->isNullValue() || N1C->getAPIntValue() == 1) &&
1375 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
Eli Friedman65919b52009-07-26 23:47:17 +00001376 // SETCC (SETCC), [0|1], [EQ|NE] -> SETCC
Evan Cheng228c31f2010-02-27 07:36:59 +00001377 if (N0.getOpcode() == ISD::SETCC &&
1378 isTypeLegal(VT) && VT.bitsLE(N0.getValueType())) {
Evan Cheng16b75ce2010-01-07 20:58:44 +00001379 bool TrueWhenTrue = (Cond == ISD::SETEQ) ^ (N1C->getAPIntValue() != 1);
Eli Friedman65919b52009-07-26 23:47:17 +00001380 if (TrueWhenTrue)
Wesley Peck527da1b2010-11-23 03:31:01 +00001381 return DAG.getNode(ISD::TRUNCATE, dl, VT, N0);
Eli Friedman65919b52009-07-26 23:47:17 +00001382 // Invert the condition.
1383 ISD::CondCode CC = cast<CondCodeSDNode>(N0.getOperand(2))->get();
Wesley Peck527da1b2010-11-23 03:31:01 +00001384 CC = ISD::getSetCCInverse(CC,
Eli Friedman65919b52009-07-26 23:47:17 +00001385 N0.getOperand(0).getValueType().isInteger());
Tom Stellardcd428182013-09-28 02:50:38 +00001386 if (DCI.isBeforeLegalizeOps() ||
1387 isCondCodeLegal(CC, N0.getOperand(0).getSimpleValueType()))
1388 return DAG.getSetCC(dl, VT, N0.getOperand(0), N0.getOperand(1), CC);
Evan Cheng92658d52007-02-08 22:13:59 +00001389 }
Evan Cheng228c31f2010-02-27 07:36:59 +00001390
Eli Friedman65919b52009-07-26 23:47:17 +00001391 if ((N0.getOpcode() == ISD::XOR ||
Wesley Peck527da1b2010-11-23 03:31:01 +00001392 (N0.getOpcode() == ISD::AND &&
Eli Friedman65919b52009-07-26 23:47:17 +00001393 N0.getOperand(0).getOpcode() == ISD::XOR &&
1394 N0.getOperand(1) == N0.getOperand(0).getOperand(1))) &&
1395 isa<ConstantSDNode>(N0.getOperand(1)) &&
1396 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue() == 1) {
1397 // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We
1398 // can only do this if the top bits are known zero.
1399 unsigned BitWidth = N0.getValueSizeInBits();
1400 if (DAG.MaskedValueIsZero(N0,
1401 APInt::getHighBitsSet(BitWidth,
1402 BitWidth-1))) {
1403 // Okay, get the un-inverted input value.
1404 SDValue Val;
1405 if (N0.getOpcode() == ISD::XOR)
1406 Val = N0.getOperand(0);
1407 else {
Wesley Peck527da1b2010-11-23 03:31:01 +00001408 assert(N0.getOpcode() == ISD::AND &&
Eli Friedman65919b52009-07-26 23:47:17 +00001409 N0.getOperand(0).getOpcode() == ISD::XOR);
1410 // ((X^1)&1)^1 -> X & 1
1411 Val = DAG.getNode(ISD::AND, dl, N0.getValueType(),
1412 N0.getOperand(0).getOperand(0),
1413 N0.getOperand(1));
1414 }
Evan Cheng228c31f2010-02-27 07:36:59 +00001415
Eli Friedman65919b52009-07-26 23:47:17 +00001416 return DAG.getSetCC(dl, VT, Val, N1,
1417 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
1418 }
Evan Cheng228c31f2010-02-27 07:36:59 +00001419 } else if (N1C->getAPIntValue() == 1 &&
1420 (VT == MVT::i1 ||
Duncan Sandsf2641e12011-09-06 19:07:46 +00001421 getBooleanContents(false) == ZeroOrOneBooleanContent)) {
Evan Cheng228c31f2010-02-27 07:36:59 +00001422 SDValue Op0 = N0;
1423 if (Op0.getOpcode() == ISD::TRUNCATE)
1424 Op0 = Op0.getOperand(0);
1425
1426 if ((Op0.getOpcode() == ISD::XOR) &&
1427 Op0.getOperand(0).getOpcode() == ISD::SETCC &&
1428 Op0.getOperand(1).getOpcode() == ISD::SETCC) {
1429 // (xor (setcc), (setcc)) == / != 1 -> (setcc) != / == (setcc)
1430 Cond = (Cond == ISD::SETEQ) ? ISD::SETNE : ISD::SETEQ;
1431 return DAG.getSetCC(dl, VT, Op0.getOperand(0), Op0.getOperand(1),
1432 Cond);
Craig Topper63f59212012-12-19 06:12:28 +00001433 }
1434 if (Op0.getOpcode() == ISD::AND &&
1435 isa<ConstantSDNode>(Op0.getOperand(1)) &&
1436 cast<ConstantSDNode>(Op0.getOperand(1))->getAPIntValue() == 1) {
Evan Cheng228c31f2010-02-27 07:36:59 +00001437 // If this is (X&1) == / != 1, normalize it to (X&1) != / == 0.
Anton Korobeynikov737718d2010-05-01 12:52:34 +00001438 if (Op0.getValueType().bitsGT(VT))
Evan Cheng228c31f2010-02-27 07:36:59 +00001439 Op0 = DAG.getNode(ISD::AND, dl, VT,
1440 DAG.getNode(ISD::TRUNCATE, dl, VT, Op0.getOperand(0)),
1441 DAG.getConstant(1, VT));
Anton Korobeynikov737718d2010-05-01 12:52:34 +00001442 else if (Op0.getValueType().bitsLT(VT))
1443 Op0 = DAG.getNode(ISD::AND, dl, VT,
1444 DAG.getNode(ISD::ANY_EXTEND, dl, VT, Op0.getOperand(0)),
1445 DAG.getConstant(1, VT));
1446
Evan Cheng228c31f2010-02-27 07:36:59 +00001447 return DAG.getSetCC(dl, VT, Op0,
1448 DAG.getConstant(0, Op0.getValueType()),
1449 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
1450 }
Craig Topper63f59212012-12-19 06:12:28 +00001451 if (Op0.getOpcode() == ISD::AssertZext &&
1452 cast<VTSDNode>(Op0.getOperand(1))->getVT() == MVT::i1)
1453 return DAG.getSetCC(dl, VT, Op0,
1454 DAG.getConstant(0, Op0.getValueType()),
1455 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
Evan Cheng92658d52007-02-08 22:13:59 +00001456 }
Eli Friedman65919b52009-07-26 23:47:17 +00001457 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001458
Eli Friedman65919b52009-07-26 23:47:17 +00001459 APInt MinVal, MaxVal;
1460 unsigned OperandBitSize = N1C->getValueType(0).getSizeInBits();
1461 if (ISD::isSignedIntSetCC(Cond)) {
1462 MinVal = APInt::getSignedMinValue(OperandBitSize);
1463 MaxVal = APInt::getSignedMaxValue(OperandBitSize);
1464 } else {
1465 MinVal = APInt::getMinValue(OperandBitSize);
1466 MaxVal = APInt::getMaxValue(OperandBitSize);
1467 }
Evan Cheng92658d52007-02-08 22:13:59 +00001468
Eli Friedman65919b52009-07-26 23:47:17 +00001469 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
1470 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
1471 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
1472 // X >= C0 --> X > (C0-1)
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001473 APInt C = C1-1;
1474 if (!N1C->isOpaque() || (N1C->isOpaque() && C.getBitWidth() <= 64 &&
1475 isLegalICmpImmediate(C.getSExtValue())))
1476 return DAG.getSetCC(dl, VT, N0,
1477 DAG.getConstant(C, N1.getValueType()),
1478 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
Eli Friedman65919b52009-07-26 23:47:17 +00001479 }
Evan Cheng92658d52007-02-08 22:13:59 +00001480
Eli Friedman65919b52009-07-26 23:47:17 +00001481 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
1482 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
1483 // X <= C0 --> X < (C0+1)
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001484 APInt C = C1+1;
1485 if (!N1C->isOpaque() || (N1C->isOpaque() && C.getBitWidth() <= 64 &&
1486 isLegalICmpImmediate(C.getSExtValue())))
1487 return DAG.getSetCC(dl, VT, N0,
1488 DAG.getConstant(C, N1.getValueType()),
1489 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
Eli Friedman65919b52009-07-26 23:47:17 +00001490 }
Evan Cheng92658d52007-02-08 22:13:59 +00001491
Eli Friedman65919b52009-07-26 23:47:17 +00001492 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
1493 return DAG.getConstant(0, VT); // X < MIN --> false
1494 if ((Cond == ISD::SETGE || Cond == ISD::SETUGE) && C1 == MinVal)
1495 return DAG.getConstant(1, VT); // X >= MIN --> true
1496 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal)
1497 return DAG.getConstant(0, VT); // X > MAX --> false
1498 if ((Cond == ISD::SETLE || Cond == ISD::SETULE) && C1 == MaxVal)
1499 return DAG.getConstant(1, VT); // X <= MAX --> true
Evan Cheng92658d52007-02-08 22:13:59 +00001500
Eli Friedman65919b52009-07-26 23:47:17 +00001501 // Canonicalize setgt X, Min --> setne X, Min
1502 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
1503 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETNE);
1504 // Canonicalize setlt X, Max --> setne X, Max
1505 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
1506 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETNE);
Evan Cheng92658d52007-02-08 22:13:59 +00001507
Eli Friedman65919b52009-07-26 23:47:17 +00001508 // If we have setult X, 1, turn it into seteq X, 0
1509 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
Wesley Peck527da1b2010-11-23 03:31:01 +00001510 return DAG.getSetCC(dl, VT, N0,
1511 DAG.getConstant(MinVal, N0.getValueType()),
Eli Friedman65919b52009-07-26 23:47:17 +00001512 ISD::SETEQ);
1513 // If we have setugt X, Max-1, turn it into seteq X, Max
Craig Topper3f194c82012-12-19 06:43:58 +00001514 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
Wesley Peck527da1b2010-11-23 03:31:01 +00001515 return DAG.getSetCC(dl, VT, N0,
Eli Friedman65919b52009-07-26 23:47:17 +00001516 DAG.getConstant(MaxVal, N0.getValueType()),
1517 ISD::SETEQ);
Evan Cheng92658d52007-02-08 22:13:59 +00001518
Eli Friedman65919b52009-07-26 23:47:17 +00001519 // If we have "setcc X, C0", check to see if we can shrink the immediate
1520 // by changing cc.
Evan Cheng92658d52007-02-08 22:13:59 +00001521
Eli Friedman65919b52009-07-26 23:47:17 +00001522 // SETUGT X, SINTMAX -> SETLT X, 0
Wesley Peck527da1b2010-11-23 03:31:01 +00001523 if (Cond == ISD::SETUGT &&
Eli Friedman65919b52009-07-26 23:47:17 +00001524 C1 == APInt::getSignedMaxValue(OperandBitSize))
Wesley Peck527da1b2010-11-23 03:31:01 +00001525 return DAG.getSetCC(dl, VT, N0,
Eli Friedman65919b52009-07-26 23:47:17 +00001526 DAG.getConstant(0, N1.getValueType()),
1527 ISD::SETLT);
Evan Cheng92658d52007-02-08 22:13:59 +00001528
Eli Friedman65919b52009-07-26 23:47:17 +00001529 // SETULT X, SINTMIN -> SETGT X, -1
1530 if (Cond == ISD::SETULT &&
1531 C1 == APInt::getSignedMinValue(OperandBitSize)) {
1532 SDValue ConstMinusOne =
1533 DAG.getConstant(APInt::getAllOnesValue(OperandBitSize),
1534 N1.getValueType());
1535 return DAG.getSetCC(dl, VT, N0, ConstMinusOne, ISD::SETGT);
1536 }
Evan Cheng92658d52007-02-08 22:13:59 +00001537
Eli Friedman65919b52009-07-26 23:47:17 +00001538 // Fold bit comparisons when we can.
1539 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
Evan Cheng166a4e62010-01-06 19:38:29 +00001540 (VT == N0.getValueType() ||
1541 (isTypeLegal(VT) && VT.bitsLE(N0.getValueType()))) &&
1542 N0.getOpcode() == ISD::AND)
Eli Friedman65919b52009-07-26 23:47:17 +00001543 if (ConstantSDNode *AndRHS =
1544 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Owen Anderson77e4d442014-01-22 22:34:17 +00001545 EVT ShiftTy = DCI.isBeforeLegalize() ?
Owen Andersonb2c80da2011-02-25 21:41:48 +00001546 getPointerTy() : getShiftAmountTy(N0.getValueType());
Eli Friedman65919b52009-07-26 23:47:17 +00001547 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
1548 // Perform the xform if the AND RHS is a single bit.
Evan Cheng16b75ce2010-01-07 20:58:44 +00001549 if (AndRHS->getAPIntValue().isPowerOf2()) {
Evan Cheng166a4e62010-01-06 19:38:29 +00001550 return DAG.getNode(ISD::TRUNCATE, dl, VT,
1551 DAG.getNode(ISD::SRL, dl, N0.getValueType(), N0,
Evan Cheng16b75ce2010-01-07 20:58:44 +00001552 DAG.getConstant(AndRHS->getAPIntValue().logBase2(), ShiftTy)));
Eli Friedman65919b52009-07-26 23:47:17 +00001553 }
Evan Cheng16b75ce2010-01-07 20:58:44 +00001554 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getAPIntValue()) {
Eli Friedman65919b52009-07-26 23:47:17 +00001555 // (X & 8) == 8 --> (X & 8) >> 3
1556 // Perform the xform if C1 is a single bit.
1557 if (C1.isPowerOf2()) {
Evan Cheng166a4e62010-01-06 19:38:29 +00001558 return DAG.getNode(ISD::TRUNCATE, dl, VT,
1559 DAG.getNode(ISD::SRL, dl, N0.getValueType(), N0,
1560 DAG.getConstant(C1.logBase2(), ShiftTy)));
Evan Cheng92658d52007-02-08 22:13:59 +00001561 }
1562 }
Eli Friedman65919b52009-07-26 23:47:17 +00001563 }
Evan Chengf579bec2012-07-17 06:53:39 +00001564
Evan Cheng47d7be92012-07-17 07:47:50 +00001565 if (C1.getMinSignedBits() <= 64 &&
1566 !isLegalICmpImmediate(C1.getSExtValue())) {
Evan Chengf579bec2012-07-17 06:53:39 +00001567 // (X & -256) == 256 -> (X >> 8) == 1
1568 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
1569 N0.getOpcode() == ISD::AND && N0.hasOneUse()) {
1570 if (ConstantSDNode *AndRHS =
1571 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1572 const APInt &AndRHSC = AndRHS->getAPIntValue();
1573 if ((-AndRHSC).isPowerOf2() && (AndRHSC & C1) == C1) {
1574 unsigned ShiftBits = AndRHSC.countTrailingZeros();
Owen Anderson77e4d442014-01-22 22:34:17 +00001575 EVT ShiftTy = DCI.isBeforeLegalize() ?
Evan Chengf579bec2012-07-17 06:53:39 +00001576 getPointerTy() : getShiftAmountTy(N0.getValueType());
1577 EVT CmpTy = N0.getValueType();
1578 SDValue Shift = DAG.getNode(ISD::SRL, dl, CmpTy, N0.getOperand(0),
1579 DAG.getConstant(ShiftBits, ShiftTy));
1580 SDValue CmpRHS = DAG.getConstant(C1.lshr(ShiftBits), CmpTy);
1581 return DAG.getSetCC(dl, VT, Shift, CmpRHS, Cond);
1582 }
1583 }
Evan Cheng780f9b52012-07-17 08:31:11 +00001584 } else if (Cond == ISD::SETULT || Cond == ISD::SETUGE ||
1585 Cond == ISD::SETULE || Cond == ISD::SETUGT) {
1586 bool AdjOne = (Cond == ISD::SETULE || Cond == ISD::SETUGT);
1587 // X < 0x100000000 -> (X >> 32) < 1
1588 // X >= 0x100000000 -> (X >> 32) >= 1
1589 // X <= 0x0ffffffff -> (X >> 32) < 1
1590 // X > 0x0ffffffff -> (X >> 32) >= 1
1591 unsigned ShiftBits;
1592 APInt NewC = C1;
1593 ISD::CondCode NewCond = Cond;
1594 if (AdjOne) {
1595 ShiftBits = C1.countTrailingOnes();
1596 NewC = NewC + 1;
1597 NewCond = (Cond == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
1598 } else {
1599 ShiftBits = C1.countTrailingZeros();
1600 }
1601 NewC = NewC.lshr(ShiftBits);
1602 if (ShiftBits && isLegalICmpImmediate(NewC.getSExtValue())) {
Owen Anderson77e4d442014-01-22 22:34:17 +00001603 EVT ShiftTy = DCI.isBeforeLegalize() ?
Evan Cheng780f9b52012-07-17 08:31:11 +00001604 getPointerTy() : getShiftAmountTy(N0.getValueType());
1605 EVT CmpTy = N0.getValueType();
1606 SDValue Shift = DAG.getNode(ISD::SRL, dl, CmpTy, N0,
1607 DAG.getConstant(ShiftBits, ShiftTy));
1608 SDValue CmpRHS = DAG.getConstant(NewC, CmpTy);
1609 return DAG.getSetCC(dl, VT, Shift, CmpRHS, NewCond);
1610 }
Evan Chengf579bec2012-07-17 06:53:39 +00001611 }
1612 }
Evan Cheng92658d52007-02-08 22:13:59 +00001613 }
1614
Gabor Greiff304a7a2008-08-28 21:40:38 +00001615 if (isa<ConstantFPSDNode>(N0.getNode())) {
Evan Cheng92658d52007-02-08 22:13:59 +00001616 // Constant fold or commute setcc.
Dale Johannesenf1163e92009-02-03 00:47:48 +00001617 SDValue O = DAG.FoldSetCC(VT, N0, N1, Cond, dl);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001618 if (O.getNode()) return O;
1619 } else if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
Chris Lattner3b6a8212007-12-29 08:37:08 +00001620 // If the RHS of an FP comparison is a constant, simplify it away in
1621 // some cases.
1622 if (CFP->getValueAPF().isNaN()) {
1623 // If an operand is known to be a nan, we can fold it.
1624 switch (ISD::getUnorderedFlavor(Cond)) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001625 default: llvm_unreachable("Unknown flavor!");
Chris Lattner3b6a8212007-12-29 08:37:08 +00001626 case 0: // Known false.
1627 return DAG.getConstant(0, VT);
1628 case 1: // Known true.
1629 return DAG.getConstant(1, VT);
Chris Lattner96317d22007-12-30 21:21:10 +00001630 case 2: // Undefined.
Dale Johannesen84935752009-02-06 23:05:02 +00001631 return DAG.getUNDEF(VT);
Chris Lattner3b6a8212007-12-29 08:37:08 +00001632 }
1633 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001634
Chris Lattner3b6a8212007-12-29 08:37:08 +00001635 // Otherwise, we know the RHS is not a NaN. Simplify the node to drop the
1636 // constant if knowing that the operand is non-nan is enough. We prefer to
1637 // have SETO(x,x) instead of SETO(x, 0.0) because this avoids having to
1638 // materialize 0.0.
1639 if (Cond == ISD::SETO || Cond == ISD::SETUO)
Dale Johannesenf1163e92009-02-03 00:47:48 +00001640 return DAG.getSetCC(dl, VT, N0, N0, Cond);
Dan Gohman832800a2009-09-26 15:24:17 +00001641
1642 // If the condition is not legal, see if we can find an equivalent one
1643 // which is legal.
Patrik Hagglundffd057a2012-12-19 10:19:55 +00001644 if (!isCondCodeLegal(Cond, N0.getSimpleValueType())) {
Dan Gohman832800a2009-09-26 15:24:17 +00001645 // If the comparison was an awkward floating-point == or != and one of
1646 // the comparison operands is infinity or negative infinity, convert the
1647 // condition to a less-awkward <= or >=.
1648 if (CFP->getValueAPF().isInfinity()) {
1649 if (CFP->getValueAPF().isNegative()) {
1650 if (Cond == ISD::SETOEQ &&
Patrik Hagglundffd057a2012-12-19 10:19:55 +00001651 isCondCodeLegal(ISD::SETOLE, N0.getSimpleValueType()))
Dan Gohman832800a2009-09-26 15:24:17 +00001652 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOLE);
1653 if (Cond == ISD::SETUEQ &&
Patrik Hagglundffd057a2012-12-19 10:19:55 +00001654 isCondCodeLegal(ISD::SETOLE, N0.getSimpleValueType()))
Dan Gohman832800a2009-09-26 15:24:17 +00001655 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETULE);
1656 if (Cond == ISD::SETUNE &&
Patrik Hagglundffd057a2012-12-19 10:19:55 +00001657 isCondCodeLegal(ISD::SETUGT, N0.getSimpleValueType()))
Dan Gohman832800a2009-09-26 15:24:17 +00001658 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETUGT);
1659 if (Cond == ISD::SETONE &&
Patrik Hagglundffd057a2012-12-19 10:19:55 +00001660 isCondCodeLegal(ISD::SETUGT, N0.getSimpleValueType()))
Dan Gohman832800a2009-09-26 15:24:17 +00001661 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOGT);
1662 } else {
1663 if (Cond == ISD::SETOEQ &&
Patrik Hagglundffd057a2012-12-19 10:19:55 +00001664 isCondCodeLegal(ISD::SETOGE, N0.getSimpleValueType()))
Dan Gohman832800a2009-09-26 15:24:17 +00001665 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOGE);
1666 if (Cond == ISD::SETUEQ &&
Patrik Hagglundffd057a2012-12-19 10:19:55 +00001667 isCondCodeLegal(ISD::SETOGE, N0.getSimpleValueType()))
Dan Gohman832800a2009-09-26 15:24:17 +00001668 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETUGE);
1669 if (Cond == ISD::SETUNE &&
Patrik Hagglundffd057a2012-12-19 10:19:55 +00001670 isCondCodeLegal(ISD::SETULT, N0.getSimpleValueType()))
Dan Gohman832800a2009-09-26 15:24:17 +00001671 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETULT);
1672 if (Cond == ISD::SETONE &&
Patrik Hagglundffd057a2012-12-19 10:19:55 +00001673 isCondCodeLegal(ISD::SETULT, N0.getSimpleValueType()))
Dan Gohman832800a2009-09-26 15:24:17 +00001674 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOLT);
1675 }
1676 }
1677 }
Evan Cheng92658d52007-02-08 22:13:59 +00001678 }
1679
1680 if (N0 == N1) {
Duncan Sands0552a2c2012-07-05 09:32:46 +00001681 // The sext(setcc()) => setcc() optimization relies on the appropriate
1682 // constant being emitted.
Nadav Rotema8e15b02012-09-06 11:13:55 +00001683 uint64_t EqVal = 0;
Duncan Sands0552a2c2012-07-05 09:32:46 +00001684 switch (getBooleanContents(N0.getValueType().isVector())) {
Duncan Sands0552a2c2012-07-05 09:32:46 +00001685 case UndefinedBooleanContent:
1686 case ZeroOrOneBooleanContent:
1687 EqVal = ISD::isTrueWhenEqual(Cond);
1688 break;
1689 case ZeroOrNegativeOneBooleanContent:
1690 EqVal = ISD::isTrueWhenEqual(Cond) ? -1 : 0;
1691 break;
1692 }
1693
Evan Cheng92658d52007-02-08 22:13:59 +00001694 // We can always fold X == X for integer setcc's.
Chad Rosier2a02fe12012-04-03 20:11:24 +00001695 if (N0.getValueType().isInteger()) {
Duncan Sands0552a2c2012-07-05 09:32:46 +00001696 return DAG.getConstant(EqVal, VT);
Chad Rosier2a02fe12012-04-03 20:11:24 +00001697 }
Evan Cheng92658d52007-02-08 22:13:59 +00001698 unsigned UOF = ISD::getUnorderedFlavor(Cond);
1699 if (UOF == 2) // FP operators that are undefined on NaNs.
Duncan Sands0552a2c2012-07-05 09:32:46 +00001700 return DAG.getConstant(EqVal, VT);
Evan Cheng92658d52007-02-08 22:13:59 +00001701 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
Duncan Sands0552a2c2012-07-05 09:32:46 +00001702 return DAG.getConstant(EqVal, VT);
Evan Cheng92658d52007-02-08 22:13:59 +00001703 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
1704 // if it is not already.
1705 ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
Micah Villmowb67d7a32012-07-31 18:07:43 +00001706 if (NewCond != Cond && (DCI.isBeforeLegalizeOps() ||
Patrik Hagglunddeee9002012-12-19 10:09:26 +00001707 getCondCodeAction(NewCond, N0.getSimpleValueType()) == Legal))
Dale Johannesenf1163e92009-02-03 00:47:48 +00001708 return DAG.getSetCC(dl, VT, N0, N1, NewCond);
Evan Cheng92658d52007-02-08 22:13:59 +00001709 }
1710
1711 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
Duncan Sands13237ac2008-06-06 12:08:01 +00001712 N0.getValueType().isInteger()) {
Evan Cheng92658d52007-02-08 22:13:59 +00001713 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
1714 N0.getOpcode() == ISD::XOR) {
1715 // Simplify (X+Y) == (X+Z) --> Y == Z
1716 if (N0.getOpcode() == N1.getOpcode()) {
1717 if (N0.getOperand(0) == N1.getOperand(0))
Dale Johannesenf1163e92009-02-03 00:47:48 +00001718 return DAG.getSetCC(dl, VT, N0.getOperand(1), N1.getOperand(1), Cond);
Evan Cheng92658d52007-02-08 22:13:59 +00001719 if (N0.getOperand(1) == N1.getOperand(1))
Dale Johannesenf1163e92009-02-03 00:47:48 +00001720 return DAG.getSetCC(dl, VT, N0.getOperand(0), N1.getOperand(0), Cond);
Evan Cheng92658d52007-02-08 22:13:59 +00001721 if (DAG.isCommutativeBinOp(N0.getOpcode())) {
1722 // If X op Y == Y op X, try other combinations.
1723 if (N0.getOperand(0) == N1.getOperand(1))
Wesley Peck527da1b2010-11-23 03:31:01 +00001724 return DAG.getSetCC(dl, VT, N0.getOperand(1), N1.getOperand(0),
Dale Johannesenf1163e92009-02-03 00:47:48 +00001725 Cond);
Evan Cheng92658d52007-02-08 22:13:59 +00001726 if (N0.getOperand(1) == N1.getOperand(0))
Wesley Peck527da1b2010-11-23 03:31:01 +00001727 return DAG.getSetCC(dl, VT, N0.getOperand(0), N1.getOperand(1),
Dale Johannesenf1163e92009-02-03 00:47:48 +00001728 Cond);
Evan Cheng92658d52007-02-08 22:13:59 +00001729 }
1730 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001731
Jakob Stoklund Olesen37492ea2012-04-05 20:30:20 +00001732 // If RHS is a legal immediate value for a compare instruction, we need
1733 // to be careful about increasing register pressure needlessly.
1734 bool LegalRHSImm = false;
1735
Evan Cheng92658d52007-02-08 22:13:59 +00001736 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
1737 if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1738 // Turn (X+C1) == C2 --> X == C2-C1
Gabor Greiff304a7a2008-08-28 21:40:38 +00001739 if (N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse()) {
Dale Johannesenf1163e92009-02-03 00:47:48 +00001740 return DAG.getSetCC(dl, VT, N0.getOperand(0),
Dan Gohmaneffb8942008-09-12 16:56:44 +00001741 DAG.getConstant(RHSC->getAPIntValue()-
1742 LHSR->getAPIntValue(),
Evan Cheng92658d52007-02-08 22:13:59 +00001743 N0.getValueType()), Cond);
1744 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001745
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001746 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
Evan Cheng92658d52007-02-08 22:13:59 +00001747 if (N0.getOpcode() == ISD::XOR)
1748 // If we know that all of the inverted bits are zero, don't bother
1749 // performing the inversion.
Dan Gohman1f372ed2008-02-25 21:11:39 +00001750 if (DAG.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getAPIntValue()))
1751 return
Dale Johannesenf1163e92009-02-03 00:47:48 +00001752 DAG.getSetCC(dl, VT, N0.getOperand(0),
Dan Gohman1f372ed2008-02-25 21:11:39 +00001753 DAG.getConstant(LHSR->getAPIntValue() ^
1754 RHSC->getAPIntValue(),
1755 N0.getValueType()),
1756 Cond);
Evan Cheng92658d52007-02-08 22:13:59 +00001757 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001758
Evan Cheng92658d52007-02-08 22:13:59 +00001759 // Turn (C1-X) == C2 --> X == C1-C2
1760 if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00001761 if (N0.getOpcode() == ISD::SUB && N0.getNode()->hasOneUse()) {
Dan Gohman1f372ed2008-02-25 21:11:39 +00001762 return
Dale Johannesenf1163e92009-02-03 00:47:48 +00001763 DAG.getSetCC(dl, VT, N0.getOperand(1),
Dan Gohman1f372ed2008-02-25 21:11:39 +00001764 DAG.getConstant(SUBC->getAPIntValue() -
1765 RHSC->getAPIntValue(),
1766 N0.getValueType()),
1767 Cond);
Evan Cheng92658d52007-02-08 22:13:59 +00001768 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001769 }
Jakob Stoklund Olesen37492ea2012-04-05 20:30:20 +00001770
1771 // Could RHSC fold directly into a compare?
1772 if (RHSC->getValueType(0).getSizeInBits() <= 64)
1773 LegalRHSImm = isLegalICmpImmediate(RHSC->getSExtValue());
Evan Cheng92658d52007-02-08 22:13:59 +00001774 }
1775
1776 // Simplify (X+Z) == X --> Z == 0
Jakob Stoklund Olesen37492ea2012-04-05 20:30:20 +00001777 // Don't do this if X is an immediate that can fold into a cmp
1778 // instruction and X+Z has other uses. It could be an induction variable
1779 // chain, and the transform would increase register pressure.
1780 if (!LegalRHSImm || N0.getNode()->hasOneUse()) {
1781 if (N0.getOperand(0) == N1)
1782 return DAG.getSetCC(dl, VT, N0.getOperand(1),
1783 DAG.getConstant(0, N0.getValueType()), Cond);
1784 if (N0.getOperand(1) == N1) {
1785 if (DAG.isCommutativeBinOp(N0.getOpcode()))
1786 return DAG.getSetCC(dl, VT, N0.getOperand(0),
1787 DAG.getConstant(0, N0.getValueType()), Cond);
Craig Topper3f194c82012-12-19 06:43:58 +00001788 if (N0.getNode()->hasOneUse()) {
Jakob Stoklund Olesen37492ea2012-04-05 20:30:20 +00001789 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
1790 // (Z-X) == X --> Z == X<<1
1791 SDValue SH = DAG.getNode(ISD::SHL, dl, N1.getValueType(), N1,
Owen Andersonb2c80da2011-02-25 21:41:48 +00001792 DAG.getConstant(1, getShiftAmountTy(N1.getValueType())));
Jakob Stoklund Olesen37492ea2012-04-05 20:30:20 +00001793 if (!DCI.isCalledByLegalizer())
1794 DCI.AddToWorklist(SH.getNode());
1795 return DAG.getSetCC(dl, VT, N0.getOperand(0), SH, Cond);
1796 }
Evan Cheng92658d52007-02-08 22:13:59 +00001797 }
1798 }
1799 }
1800
1801 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
1802 N1.getOpcode() == ISD::XOR) {
1803 // Simplify X == (X+Z) --> Z == 0
Craig Topper3f194c82012-12-19 06:43:58 +00001804 if (N1.getOperand(0) == N0)
Dale Johannesenf1163e92009-02-03 00:47:48 +00001805 return DAG.getSetCC(dl, VT, N1.getOperand(1),
Evan Cheng92658d52007-02-08 22:13:59 +00001806 DAG.getConstant(0, N1.getValueType()), Cond);
Craig Topper3f194c82012-12-19 06:43:58 +00001807 if (N1.getOperand(1) == N0) {
1808 if (DAG.isCommutativeBinOp(N1.getOpcode()))
Dale Johannesenf1163e92009-02-03 00:47:48 +00001809 return DAG.getSetCC(dl, VT, N1.getOperand(0),
Evan Cheng92658d52007-02-08 22:13:59 +00001810 DAG.getConstant(0, N1.getValueType()), Cond);
Craig Topper3f194c82012-12-19 06:43:58 +00001811 if (N1.getNode()->hasOneUse()) {
Evan Cheng92658d52007-02-08 22:13:59 +00001812 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
1813 // X == (Z-X) --> X<<1 == Z
Wesley Peck527da1b2010-11-23 03:31:01 +00001814 SDValue SH = DAG.getNode(ISD::SHL, dl, N1.getValueType(), N0,
Owen Andersonb2c80da2011-02-25 21:41:48 +00001815 DAG.getConstant(1, getShiftAmountTy(N0.getValueType())));
Evan Cheng92658d52007-02-08 22:13:59 +00001816 if (!DCI.isCalledByLegalizer())
Gabor Greiff304a7a2008-08-28 21:40:38 +00001817 DCI.AddToWorklist(SH.getNode());
Dale Johannesenf1163e92009-02-03 00:47:48 +00001818 return DAG.getSetCC(dl, VT, SH, N1.getOperand(0), Cond);
Evan Cheng92658d52007-02-08 22:13:59 +00001819 }
1820 }
1821 }
Dan Gohmane58ab792009-01-29 01:59:02 +00001822
Dan Gohman8b437cc2009-01-29 16:18:12 +00001823 // Simplify x&y == y to x&y != 0 if y has exactly one bit set.
Dale Johannesencc5fc442009-02-11 19:19:41 +00001824 // Note that where y is variable and is known to have at most
1825 // one bit set (for example, if it is z&1) we cannot do this;
1826 // the expressions are not equivalent when y==0.
Dan Gohmane58ab792009-01-29 01:59:02 +00001827 if (N0.getOpcode() == ISD::AND)
1828 if (N0.getOperand(0) == N1 || N0.getOperand(1) == N1) {
Dale Johannesencc5fc442009-02-11 19:19:41 +00001829 if (ValueHasExactlyOneBitSet(N1, DAG)) {
Dan Gohmane58ab792009-01-29 01:59:02 +00001830 Cond = ISD::getSetCCInverse(Cond, /*isInteger=*/true);
Tom Stellardcd428182013-09-28 02:50:38 +00001831 if (DCI.isBeforeLegalizeOps() ||
1832 isCondCodeLegal(Cond, N0.getSimpleValueType())) {
1833 SDValue Zero = DAG.getConstant(0, N1.getValueType());
1834 return DAG.getSetCC(dl, VT, N0, Zero, Cond);
1835 }
Dan Gohmane58ab792009-01-29 01:59:02 +00001836 }
1837 }
1838 if (N1.getOpcode() == ISD::AND)
1839 if (N1.getOperand(0) == N0 || N1.getOperand(1) == N0) {
Dale Johannesencc5fc442009-02-11 19:19:41 +00001840 if (ValueHasExactlyOneBitSet(N0, DAG)) {
Dan Gohmane58ab792009-01-29 01:59:02 +00001841 Cond = ISD::getSetCCInverse(Cond, /*isInteger=*/true);
Tom Stellardcd428182013-09-28 02:50:38 +00001842 if (DCI.isBeforeLegalizeOps() ||
1843 isCondCodeLegal(Cond, N1.getSimpleValueType())) {
1844 SDValue Zero = DAG.getConstant(0, N0.getValueType());
1845 return DAG.getSetCC(dl, VT, N1, Zero, Cond);
1846 }
Dan Gohmane58ab792009-01-29 01:59:02 +00001847 }
1848 }
Evan Cheng92658d52007-02-08 22:13:59 +00001849 }
1850
1851 // Fold away ALL boolean setcc's.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001852 SDValue Temp;
Owen Anderson9f944592009-08-11 20:47:22 +00001853 if (N0.getValueType() == MVT::i1 && foldBooleans) {
Evan Cheng92658d52007-02-08 22:13:59 +00001854 switch (Cond) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001855 default: llvm_unreachable("Unknown integer setcc!");
Bob Wilsonc5890052009-01-22 17:39:32 +00001856 case ISD::SETEQ: // X == Y -> ~(X^Y)
Owen Anderson9f944592009-08-11 20:47:22 +00001857 Temp = DAG.getNode(ISD::XOR, dl, MVT::i1, N0, N1);
1858 N0 = DAG.getNOT(dl, Temp, MVT::i1);
Evan Cheng92658d52007-02-08 22:13:59 +00001859 if (!DCI.isCalledByLegalizer())
Gabor Greiff304a7a2008-08-28 21:40:38 +00001860 DCI.AddToWorklist(Temp.getNode());
Evan Cheng92658d52007-02-08 22:13:59 +00001861 break;
1862 case ISD::SETNE: // X != Y --> (X^Y)
Owen Anderson9f944592009-08-11 20:47:22 +00001863 N0 = DAG.getNode(ISD::XOR, dl, MVT::i1, N0, N1);
Evan Cheng92658d52007-02-08 22:13:59 +00001864 break;
Bob Wilsonc5890052009-01-22 17:39:32 +00001865 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> ~X & Y
1866 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> ~X & Y
Owen Anderson9f944592009-08-11 20:47:22 +00001867 Temp = DAG.getNOT(dl, N0, MVT::i1);
1868 N0 = DAG.getNode(ISD::AND, dl, MVT::i1, N1, Temp);
Evan Cheng92658d52007-02-08 22:13:59 +00001869 if (!DCI.isCalledByLegalizer())
Gabor Greiff304a7a2008-08-28 21:40:38 +00001870 DCI.AddToWorklist(Temp.getNode());
Evan Cheng92658d52007-02-08 22:13:59 +00001871 break;
Bob Wilsonc5890052009-01-22 17:39:32 +00001872 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> ~Y & X
1873 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> ~Y & X
Owen Anderson9f944592009-08-11 20:47:22 +00001874 Temp = DAG.getNOT(dl, N1, MVT::i1);
1875 N0 = DAG.getNode(ISD::AND, dl, MVT::i1, N0, Temp);
Evan Cheng92658d52007-02-08 22:13:59 +00001876 if (!DCI.isCalledByLegalizer())
Gabor Greiff304a7a2008-08-28 21:40:38 +00001877 DCI.AddToWorklist(Temp.getNode());
Evan Cheng92658d52007-02-08 22:13:59 +00001878 break;
Bob Wilsonc5890052009-01-22 17:39:32 +00001879 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> ~X | Y
1880 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> ~X | Y
Owen Anderson9f944592009-08-11 20:47:22 +00001881 Temp = DAG.getNOT(dl, N0, MVT::i1);
1882 N0 = DAG.getNode(ISD::OR, dl, MVT::i1, N1, Temp);
Evan Cheng92658d52007-02-08 22:13:59 +00001883 if (!DCI.isCalledByLegalizer())
Gabor Greiff304a7a2008-08-28 21:40:38 +00001884 DCI.AddToWorklist(Temp.getNode());
Evan Cheng92658d52007-02-08 22:13:59 +00001885 break;
Bob Wilsonc5890052009-01-22 17:39:32 +00001886 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> ~Y | X
1887 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> ~Y | X
Owen Anderson9f944592009-08-11 20:47:22 +00001888 Temp = DAG.getNOT(dl, N1, MVT::i1);
1889 N0 = DAG.getNode(ISD::OR, dl, MVT::i1, N0, Temp);
Evan Cheng92658d52007-02-08 22:13:59 +00001890 break;
1891 }
Owen Anderson9f944592009-08-11 20:47:22 +00001892 if (VT != MVT::i1) {
Evan Cheng92658d52007-02-08 22:13:59 +00001893 if (!DCI.isCalledByLegalizer())
Gabor Greiff304a7a2008-08-28 21:40:38 +00001894 DCI.AddToWorklist(N0.getNode());
Evan Cheng92658d52007-02-08 22:13:59 +00001895 // FIXME: If running after legalize, we probably can't do this.
Dale Johannesenf1163e92009-02-03 00:47:48 +00001896 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, N0);
Evan Cheng92658d52007-02-08 22:13:59 +00001897 }
1898 return N0;
1899 }
1900
1901 // Could not fold it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001902 return SDValue();
Evan Cheng92658d52007-02-08 22:13:59 +00001903}
1904
Evan Cheng2609d5e2008-05-12 19:56:52 +00001905/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
1906/// node is a GlobalAddress + offset.
Chris Lattner46c01a32011-02-13 22:25:43 +00001907bool TargetLowering::isGAPlusOffset(SDNode *N, const GlobalValue *&GA,
Evan Cheng2609d5e2008-05-12 19:56:52 +00001908 int64_t &Offset) const {
1909 if (isa<GlobalAddressSDNode>(N)) {
Dan Gohmane38cc012008-06-09 22:05:52 +00001910 GlobalAddressSDNode *GASD = cast<GlobalAddressSDNode>(N);
1911 GA = GASD->getGlobal();
1912 Offset += GASD->getOffset();
Evan Cheng2609d5e2008-05-12 19:56:52 +00001913 return true;
1914 }
1915
1916 if (N->getOpcode() == ISD::ADD) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001917 SDValue N1 = N->getOperand(0);
1918 SDValue N2 = N->getOperand(1);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001919 if (isGAPlusOffset(N1.getNode(), GA, Offset)) {
Evan Cheng2609d5e2008-05-12 19:56:52 +00001920 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
1921 if (V) {
Dan Gohman6e054832008-09-26 21:54:37 +00001922 Offset += V->getSExtValue();
Evan Cheng2609d5e2008-05-12 19:56:52 +00001923 return true;
1924 }
Gabor Greiff304a7a2008-08-28 21:40:38 +00001925 } else if (isGAPlusOffset(N2.getNode(), GA, Offset)) {
Evan Cheng2609d5e2008-05-12 19:56:52 +00001926 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
1927 if (V) {
Dan Gohman6e054832008-09-26 21:54:37 +00001928 Offset += V->getSExtValue();
Evan Cheng2609d5e2008-05-12 19:56:52 +00001929 return true;
1930 }
1931 }
1932 }
Owen Andersonb2c80da2011-02-25 21:41:48 +00001933
Evan Cheng2609d5e2008-05-12 19:56:52 +00001934 return false;
1935}
1936
1937
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001938SDValue TargetLowering::
Chris Lattner4a2eeea2006-03-01 04:52:55 +00001939PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
1940 // Default implementation: no optimization.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001941 return SDValue();
Chris Lattner4a2eeea2006-03-01 04:52:55 +00001942}
1943
Chris Lattneree1dadb2006-02-04 02:13:02 +00001944//===----------------------------------------------------------------------===//
1945// Inline Assembler Implementation Methods
1946//===----------------------------------------------------------------------===//
1947
Chris Lattner47935152008-04-27 00:09:47 +00001948
Chris Lattneree1dadb2006-02-04 02:13:02 +00001949TargetLowering::ConstraintType
Chris Lattnerd6855142007-03-25 02:14:49 +00001950TargetLowering::getConstraintType(const std::string &Constraint) const {
Eric Christopher0cb6fd92013-01-11 18:12:39 +00001951 unsigned S = Constraint.size();
1952
1953 if (S == 1) {
Chris Lattnerd6855142007-03-25 02:14:49 +00001954 switch (Constraint[0]) {
1955 default: break;
1956 case 'r': return C_RegisterClass;
1957 case 'm': // memory
1958 case 'o': // offsetable
1959 case 'V': // not offsetable
1960 return C_Memory;
1961 case 'i': // Simple Integer or Relocatable Constant
1962 case 'n': // Simple Integer
John Thompsonc467aa22010-09-21 22:04:54 +00001963 case 'E': // Floating Point Constant
1964 case 'F': // Floating Point Constant
Chris Lattnerd6855142007-03-25 02:14:49 +00001965 case 's': // Relocatable Constant
John Thompsonc467aa22010-09-21 22:04:54 +00001966 case 'p': // Address.
Chris Lattner3d7efa22007-03-25 04:35:41 +00001967 case 'X': // Allow ANY value.
Chris Lattnerd6855142007-03-25 02:14:49 +00001968 case 'I': // Target registers.
1969 case 'J':
1970 case 'K':
1971 case 'L':
1972 case 'M':
1973 case 'N':
1974 case 'O':
1975 case 'P':
John Thompsonc467aa22010-09-21 22:04:54 +00001976 case '<':
1977 case '>':
Chris Lattnerd6855142007-03-25 02:14:49 +00001978 return C_Other;
1979 }
Chris Lattneree1dadb2006-02-04 02:13:02 +00001980 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001981
Eric Christopher0cb6fd92013-01-11 18:12:39 +00001982 if (S > 1 && Constraint[0] == '{' && Constraint[S-1] == '}') {
1983 if (S == 8 && !Constraint.compare(1, 6, "memory", 6)) // "{memory}"
1984 return C_Memory;
Chris Lattner843e4452007-03-25 02:18:14 +00001985 return C_Register;
Eric Christopher0cb6fd92013-01-11 18:12:39 +00001986 }
Chris Lattnerd6855142007-03-25 02:14:49 +00001987 return C_Unknown;
Chris Lattneree1dadb2006-02-04 02:13:02 +00001988}
1989
Dale Johannesen2b3bc302008-01-29 02:21:21 +00001990/// LowerXConstraint - try to replace an X constraint, which matches anything,
1991/// with another that has more specific requirements based on the type of the
1992/// corresponding operand.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001993const char *TargetLowering::LowerXConstraint(EVT ConstraintVT) const{
Duncan Sands13237ac2008-06-06 12:08:01 +00001994 if (ConstraintVT.isInteger())
Chris Lattner724539c2008-04-26 23:02:14 +00001995 return "r";
Duncan Sands13237ac2008-06-06 12:08:01 +00001996 if (ConstraintVT.isFloatingPoint())
Chris Lattner724539c2008-04-26 23:02:14 +00001997 return "f"; // works for many targets
1998 return 0;
Dale Johannesen2b3bc302008-01-29 02:21:21 +00001999}
2000
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00002001/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
2002/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002003void TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopherde9399b2011-06-02 23:16:42 +00002004 std::string &Constraint,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002005 std::vector<SDValue> &Ops,
Chris Lattner724539c2008-04-26 23:02:14 +00002006 SelectionDAG &DAG) const {
Eric Christopher5bbb2bd2011-06-17 20:41:29 +00002007
Eric Christopherde9399b2011-06-02 23:16:42 +00002008 if (Constraint.length() > 1) return;
Eric Christopher5bbb2bd2011-06-17 20:41:29 +00002009
Eric Christopherde9399b2011-06-02 23:16:42 +00002010 char ConstraintLetter = Constraint[0];
Chris Lattneree1dadb2006-02-04 02:13:02 +00002011 switch (ConstraintLetter) {
Chris Lattnera9f917a2007-02-17 06:00:35 +00002012 default: break;
Dale Johannesen4646aa32007-11-05 21:20:28 +00002013 case 'X': // Allows any operand; labels (basic block) use this.
2014 if (Op.getOpcode() == ISD::BasicBlock) {
2015 Ops.push_back(Op);
2016 return;
2017 }
2018 // fall through
Chris Lattneree1dadb2006-02-04 02:13:02 +00002019 case 'i': // Simple Integer or Relocatable Constant
2020 case 'n': // Simple Integer
Dale Johannesen4646aa32007-11-05 21:20:28 +00002021 case 's': { // Relocatable Constant
Chris Lattner44a2ed62007-05-03 16:54:34 +00002022 // These operands are interested in values of the form (GV+C), where C may
2023 // be folded in as an offset of GV, or it may be explicitly added. Also, it
2024 // is possible and fine if either GV or C are missing.
2025 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
2026 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
Wesley Peck527da1b2010-11-23 03:31:01 +00002027
Chris Lattner44a2ed62007-05-03 16:54:34 +00002028 // If we have "(add GV, C)", pull out GV/C
2029 if (Op.getOpcode() == ISD::ADD) {
2030 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
2031 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
2032 if (C == 0 || GA == 0) {
2033 C = dyn_cast<ConstantSDNode>(Op.getOperand(0));
2034 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(1));
2035 }
2036 if (C == 0 || GA == 0)
2037 C = 0, GA = 0;
2038 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002039
Chris Lattner44a2ed62007-05-03 16:54:34 +00002040 // If we find a valid operand, map to the TargetXXX version so that the
2041 // value itself doesn't get selected.
2042 if (GA) { // Either &GV or &GV+C
2043 if (ConstraintLetter != 'n') {
2044 int64_t Offs = GA->getOffset();
Dan Gohmaneffb8942008-09-12 16:56:44 +00002045 if (C) Offs += C->getZExtValue();
Wesley Peck527da1b2010-11-23 03:31:01 +00002046 Ops.push_back(DAG.getTargetGlobalAddress(GA->getGlobal(),
Andrew Trickef9de2a2013-05-25 02:42:55 +00002047 C ? SDLoc(C) : SDLoc(),
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00002048 Op.getValueType(), Offs));
2049 return;
Chris Lattner44a2ed62007-05-03 16:54:34 +00002050 }
2051 }
2052 if (C) { // just C, no GV.
Chris Lattnera9f917a2007-02-17 06:00:35 +00002053 // Simple constants are not allowed for 's'.
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00002054 if (ConstraintLetter != 's') {
Dale Johannesen65577522009-02-12 20:58:09 +00002055 // gcc prints these as sign extended. Sign extend value to 64 bits
2056 // now; without this it would get ZExt'd later in
2057 // ScheduleDAGSDNodes::EmitNode, which is very generic.
2058 Ops.push_back(DAG.getTargetConstant(C->getAPIntValue().getSExtValue(),
Owen Anderson9f944592009-08-11 20:47:22 +00002059 MVT::i64));
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00002060 return;
2061 }
Chris Lattnera9f917a2007-02-17 06:00:35 +00002062 }
Chris Lattnera9f917a2007-02-17 06:00:35 +00002063 break;
Chris Lattneree1dadb2006-02-04 02:13:02 +00002064 }
Chris Lattner44a2ed62007-05-03 16:54:34 +00002065 }
Chris Lattneree1dadb2006-02-04 02:13:02 +00002066}
2067
Chris Lattner7ad77df2006-02-22 00:56:39 +00002068std::pair<unsigned, const TargetRegisterClass*> TargetLowering::
Chris Lattner7bb46962006-02-21 23:11:00 +00002069getRegForInlineAsmConstraint(const std::string &Constraint,
Chad Rosier295bd432013-06-22 18:37:38 +00002070 MVT VT) const {
Will Dietzae726a92013-10-13 03:08:49 +00002071 if (Constraint.empty() || Constraint[0] != '{')
Douglas Gregor6739a892010-05-11 06:17:44 +00002072 return std::make_pair(0u, static_cast<TargetRegisterClass*>(0));
Chris Lattner7ed31012006-02-01 01:29:47 +00002073 assert(*(Constraint.end()-1) == '}' && "Not a brace enclosed constraint?");
2074
2075 // Remove the braces from around the name.
Benjamin Kramer68e49452009-11-12 20:36:59 +00002076 StringRef RegName(Constraint.data()+1, Constraint.size()-2);
Chris Lattner7ad77df2006-02-22 00:56:39 +00002077
Hal Finkel943f76d2012-12-18 17:50:58 +00002078 std::pair<unsigned, const TargetRegisterClass*> R =
2079 std::make_pair(0u, static_cast<const TargetRegisterClass*>(0));
2080
Chris Lattner7ad77df2006-02-22 00:56:39 +00002081 // Figure out which register class contains this reg.
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00002082 const TargetRegisterInfo *RI = getTargetMachine().getRegisterInfo();
Dan Gohman3a4be0f2008-02-10 18:45:23 +00002083 for (TargetRegisterInfo::regclass_iterator RCI = RI->regclass_begin(),
Chris Lattner7ad77df2006-02-22 00:56:39 +00002084 E = RI->regclass_end(); RCI != E; ++RCI) {
2085 const TargetRegisterClass *RC = *RCI;
Wesley Peck527da1b2010-11-23 03:31:01 +00002086
2087 // If none of the value types for this register class are valid, we
Chris Lattner2e124af2006-02-22 23:00:51 +00002088 // can't use it. For example, 64-bit reg classes on 32-bit targets.
Jakob Stoklund Olesen35163e22011-10-12 01:24:51 +00002089 if (!isLegalRC(RC))
2090 continue;
Wesley Peck527da1b2010-11-23 03:31:01 +00002091
2092 for (TargetRegisterClass::iterator I = RC->begin(), E = RC->end();
Chris Lattner7ad77df2006-02-22 00:56:39 +00002093 I != E; ++I) {
Hal Finkel943f76d2012-12-18 17:50:58 +00002094 if (RegName.equals_lower(RI->getName(*I))) {
2095 std::pair<unsigned, const TargetRegisterClass*> S =
2096 std::make_pair(*I, RC);
2097
2098 // If this register class has the requested value type, return it,
2099 // otherwise keep searching and return the first class found
2100 // if no other is found which explicitly has the requested type.
2101 if (RC->hasType(VT))
2102 return S;
2103 else if (!R.second)
2104 R = S;
2105 }
Chris Lattner7ad77df2006-02-22 00:56:39 +00002106 }
Chris Lattner32fef532006-01-26 20:37:03 +00002107 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002108
Hal Finkel943f76d2012-12-18 17:50:58 +00002109 return R;
Chris Lattner32fef532006-01-26 20:37:03 +00002110}
Evan Chengaf598d22006-03-13 23:18:16 +00002111
2112//===----------------------------------------------------------------------===//
Chris Lattner47935152008-04-27 00:09:47 +00002113// Constraint Selection.
2114
Chris Lattner860df6e2008-10-17 16:47:46 +00002115/// isMatchingInputConstraint - Return true of this is an input operand that is
2116/// a matching constraint like "4".
2117bool TargetLowering::AsmOperandInfo::isMatchingInputConstraint() const {
Chris Lattneref890172008-10-17 16:21:11 +00002118 assert(!ConstraintCode.empty() && "No known constraint!");
Guy Benyei83c74e92013-02-12 21:21:59 +00002119 return isdigit(static_cast<unsigned char>(ConstraintCode[0]));
Chris Lattneref890172008-10-17 16:21:11 +00002120}
2121
2122/// getMatchedOperand - If this is an input matching constraint, this method
2123/// returns the output operand it matches.
2124unsigned TargetLowering::AsmOperandInfo::getMatchedOperand() const {
2125 assert(!ConstraintCode.empty() && "No known constraint!");
2126 return atoi(ConstraintCode.c_str());
2127}
2128
Wesley Peck527da1b2010-11-23 03:31:01 +00002129
John Thompson1094c802010-09-13 18:15:37 +00002130/// ParseConstraints - Split up the constraint string from the inline
2131/// assembly value into the specific constraints and their prefixes,
2132/// and also tie in the associated operand values.
2133/// If this returns an empty vector, and if the constraint string itself
2134/// isn't empty, there was an error parsing.
John Thompsone8360b72010-10-29 17:29:13 +00002135TargetLowering::AsmOperandInfoVector TargetLowering::ParseConstraints(
John Thompson1094c802010-09-13 18:15:37 +00002136 ImmutableCallSite CS) const {
2137 /// ConstraintOperands - Information about all of the constraints.
John Thompsone8360b72010-10-29 17:29:13 +00002138 AsmOperandInfoVector ConstraintOperands;
John Thompson1094c802010-09-13 18:15:37 +00002139 const InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
John Thompsonc467aa22010-09-21 22:04:54 +00002140 unsigned maCount = 0; // Largest number of multiple alternative constraints.
John Thompson1094c802010-09-13 18:15:37 +00002141
2142 // Do a prepass over the constraints, canonicalizing them, and building up the
2143 // ConstraintOperands list.
John Thompsone8360b72010-10-29 17:29:13 +00002144 InlineAsm::ConstraintInfoVector
John Thompson1094c802010-09-13 18:15:37 +00002145 ConstraintInfos = IA->ParseConstraints();
Wesley Peck527da1b2010-11-23 03:31:01 +00002146
John Thompson1094c802010-09-13 18:15:37 +00002147 unsigned ArgNo = 0; // ArgNo - The argument of the CallInst.
2148 unsigned ResNo = 0; // ResNo - The result number of the next output.
2149
2150 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
2151 ConstraintOperands.push_back(AsmOperandInfo(ConstraintInfos[i]));
2152 AsmOperandInfo &OpInfo = ConstraintOperands.back();
2153
John Thompsonc467aa22010-09-21 22:04:54 +00002154 // Update multiple alternative constraint count.
2155 if (OpInfo.multipleAlternatives.size() > maCount)
2156 maCount = OpInfo.multipleAlternatives.size();
2157
John Thompsone8360b72010-10-29 17:29:13 +00002158 OpInfo.ConstraintVT = MVT::Other;
John Thompson1094c802010-09-13 18:15:37 +00002159
2160 // Compute the value type for each operand.
2161 switch (OpInfo.Type) {
2162 case InlineAsm::isOutput:
2163 // Indirect outputs just consume an argument.
2164 if (OpInfo.isIndirect) {
2165 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
2166 break;
2167 }
2168
2169 // The return value of the call is this value. As such, there is no
2170 // corresponding argument.
2171 assert(!CS.getType()->isVoidTy() &&
2172 "Bad inline asm!");
Chris Lattner229907c2011-07-18 04:54:35 +00002173 if (StructType *STy = dyn_cast<StructType>(CS.getType())) {
Patrik Hagglundf9934612012-12-19 15:19:11 +00002174 OpInfo.ConstraintVT = getSimpleValueType(STy->getElementType(ResNo));
John Thompson1094c802010-09-13 18:15:37 +00002175 } else {
2176 assert(ResNo == 0 && "Asm only has one result!");
Patrik Hagglundf9934612012-12-19 15:19:11 +00002177 OpInfo.ConstraintVT = getSimpleValueType(CS.getType());
John Thompson1094c802010-09-13 18:15:37 +00002178 }
2179 ++ResNo;
2180 break;
2181 case InlineAsm::isInput:
2182 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
2183 break;
2184 case InlineAsm::isClobber:
2185 // Nothing to do.
2186 break;
2187 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002188
John Thompsone8360b72010-10-29 17:29:13 +00002189 if (OpInfo.CallOperandVal) {
Chris Lattner229907c2011-07-18 04:54:35 +00002190 llvm::Type *OpTy = OpInfo.CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +00002191 if (OpInfo.isIndirect) {
Chris Lattner229907c2011-07-18 04:54:35 +00002192 llvm::PointerType *PtrTy = dyn_cast<PointerType>(OpTy);
John Thompsone8360b72010-10-29 17:29:13 +00002193 if (!PtrTy)
2194 report_fatal_error("Indirect operand for inline asm not a pointer!");
2195 OpTy = PtrTy->getElementType();
2196 }
Eric Christopher5bbb2bd2011-06-17 20:41:29 +00002197
Eric Christopher44804282011-05-09 20:04:43 +00002198 // Look for vector wrapped in a struct. e.g. { <16 x i8> }.
Chris Lattner229907c2011-07-18 04:54:35 +00002199 if (StructType *STy = dyn_cast<StructType>(OpTy))
Eric Christopher44804282011-05-09 20:04:43 +00002200 if (STy->getNumElements() == 1)
2201 OpTy = STy->getElementType(0);
2202
John Thompsone8360b72010-10-29 17:29:13 +00002203 // If OpTy is not a single value, it may be a struct/union that we
2204 // can tile with integers.
2205 if (!OpTy->isSingleValueType() && OpTy->isSized()) {
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00002206 unsigned BitSize = getDataLayout()->getTypeSizeInBits(OpTy);
John Thompsone8360b72010-10-29 17:29:13 +00002207 switch (BitSize) {
2208 default: break;
2209 case 1:
2210 case 8:
2211 case 16:
2212 case 32:
2213 case 64:
2214 case 128:
Dale Johannesenf11ea9c2010-11-09 01:15:07 +00002215 OpInfo.ConstraintVT =
Patrik Hagglundf9934612012-12-19 15:19:11 +00002216 MVT::getVT(IntegerType::get(OpTy->getContext(), BitSize), true);
John Thompsone8360b72010-10-29 17:29:13 +00002217 break;
2218 }
Micah Villmow89021e42012-10-09 16:06:12 +00002219 } else if (PointerType *PT = dyn_cast<PointerType>(OpTy)) {
Matt Arsenaulta98c3b12013-10-10 19:09:05 +00002220 unsigned PtrSize
2221 = getDataLayout()->getPointerSizeInBits(PT->getAddressSpace());
2222 OpInfo.ConstraintVT = MVT::getIntegerVT(PtrSize);
John Thompsone8360b72010-10-29 17:29:13 +00002223 } else {
Patrik Hagglundf9934612012-12-19 15:19:11 +00002224 OpInfo.ConstraintVT = MVT::getVT(OpTy, true);
John Thompsone8360b72010-10-29 17:29:13 +00002225 }
2226 }
John Thompson1094c802010-09-13 18:15:37 +00002227 }
2228
2229 // If we have multiple alternative constraints, select the best alternative.
2230 if (ConstraintInfos.size()) {
John Thompson1094c802010-09-13 18:15:37 +00002231 if (maCount) {
2232 unsigned bestMAIndex = 0;
2233 int bestWeight = -1;
2234 // weight: -1 = invalid match, and 0 = so-so match to 5 = good match.
2235 int weight = -1;
2236 unsigned maIndex;
2237 // Compute the sums of the weights for each alternative, keeping track
2238 // of the best (highest weight) one so far.
2239 for (maIndex = 0; maIndex < maCount; ++maIndex) {
2240 int weightSum = 0;
2241 for (unsigned cIndex = 0, eIndex = ConstraintOperands.size();
2242 cIndex != eIndex; ++cIndex) {
2243 AsmOperandInfo& OpInfo = ConstraintOperands[cIndex];
2244 if (OpInfo.Type == InlineAsm::isClobber)
2245 continue;
John Thompson1094c802010-09-13 18:15:37 +00002246
John Thompsone8360b72010-10-29 17:29:13 +00002247 // If this is an output operand with a matching input operand,
2248 // look up the matching input. If their types mismatch, e.g. one
2249 // is an integer, the other is floating point, or their sizes are
2250 // different, flag it as an maCantMatch.
John Thompson1094c802010-09-13 18:15:37 +00002251 if (OpInfo.hasMatchingInput()) {
2252 AsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
John Thompson1094c802010-09-13 18:15:37 +00002253 if (OpInfo.ConstraintVT != Input.ConstraintVT) {
2254 if ((OpInfo.ConstraintVT.isInteger() !=
2255 Input.ConstraintVT.isInteger()) ||
2256 (OpInfo.ConstraintVT.getSizeInBits() !=
2257 Input.ConstraintVT.getSizeInBits())) {
2258 weightSum = -1; // Can't match.
2259 break;
2260 }
John Thompson1094c802010-09-13 18:15:37 +00002261 }
2262 }
John Thompson1094c802010-09-13 18:15:37 +00002263 weight = getMultipleConstraintMatchWeight(OpInfo, maIndex);
2264 if (weight == -1) {
2265 weightSum = -1;
2266 break;
2267 }
2268 weightSum += weight;
2269 }
2270 // Update best.
2271 if (weightSum > bestWeight) {
2272 bestWeight = weightSum;
2273 bestMAIndex = maIndex;
2274 }
2275 }
2276
2277 // Now select chosen alternative in each constraint.
2278 for (unsigned cIndex = 0, eIndex = ConstraintOperands.size();
2279 cIndex != eIndex; ++cIndex) {
2280 AsmOperandInfo& cInfo = ConstraintOperands[cIndex];
2281 if (cInfo.Type == InlineAsm::isClobber)
2282 continue;
2283 cInfo.selectAlternative(bestMAIndex);
2284 }
2285 }
2286 }
2287
2288 // Check and hook up tied operands, choose constraint code to use.
2289 for (unsigned cIndex = 0, eIndex = ConstraintOperands.size();
2290 cIndex != eIndex; ++cIndex) {
2291 AsmOperandInfo& OpInfo = ConstraintOperands[cIndex];
Wesley Peck527da1b2010-11-23 03:31:01 +00002292
John Thompson1094c802010-09-13 18:15:37 +00002293 // If this is an output operand with a matching input operand, look up the
2294 // matching input. If their types mismatch, e.g. one is an integer, the
2295 // other is floating point, or their sizes are different, flag it as an
2296 // error.
2297 if (OpInfo.hasMatchingInput()) {
2298 AsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
John Thompsone8360b72010-10-29 17:29:13 +00002299
John Thompson1094c802010-09-13 18:15:37 +00002300 if (OpInfo.ConstraintVT != Input.ConstraintVT) {
Bill Wendlingd1634052012-07-19 00:04:14 +00002301 std::pair<unsigned, const TargetRegisterClass*> MatchRC =
2302 getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
2303 OpInfo.ConstraintVT);
2304 std::pair<unsigned, const TargetRegisterClass*> InputRC =
2305 getRegForInlineAsmConstraint(Input.ConstraintCode,
2306 Input.ConstraintVT);
John Thompson1094c802010-09-13 18:15:37 +00002307 if ((OpInfo.ConstraintVT.isInteger() !=
2308 Input.ConstraintVT.isInteger()) ||
Eric Christopher92464be2011-07-14 20:13:52 +00002309 (MatchRC.second != InputRC.second)) {
John Thompson1094c802010-09-13 18:15:37 +00002310 report_fatal_error("Unsupported asm: input constraint"
2311 " with a matching output constraint of"
2312 " incompatible type!");
2313 }
John Thompson1094c802010-09-13 18:15:37 +00002314 }
John Thompsone8360b72010-10-29 17:29:13 +00002315
John Thompson1094c802010-09-13 18:15:37 +00002316 }
2317 }
2318
2319 return ConstraintOperands;
2320}
2321
Chris Lattneref890172008-10-17 16:21:11 +00002322
Chris Lattner47935152008-04-27 00:09:47 +00002323/// getConstraintGenerality - Return an integer indicating how general CT
2324/// is.
2325static unsigned getConstraintGenerality(TargetLowering::ConstraintType CT) {
2326 switch (CT) {
Chris Lattner47935152008-04-27 00:09:47 +00002327 case TargetLowering::C_Other:
2328 case TargetLowering::C_Unknown:
2329 return 0;
2330 case TargetLowering::C_Register:
2331 return 1;
2332 case TargetLowering::C_RegisterClass:
2333 return 2;
2334 case TargetLowering::C_Memory:
2335 return 3;
2336 }
Chandler Carruthf3e85022012-01-10 18:08:01 +00002337 llvm_unreachable("Invalid constraint type");
Chris Lattner47935152008-04-27 00:09:47 +00002338}
2339
John Thompsone8360b72010-10-29 17:29:13 +00002340/// Examine constraint type and operand type and determine a weight value.
John Thompson1094c802010-09-13 18:15:37 +00002341/// This object must already have been set up with the operand type
2342/// and the current alternative constraint selected.
John Thompsone8360b72010-10-29 17:29:13 +00002343TargetLowering::ConstraintWeight
2344 TargetLowering::getMultipleConstraintMatchWeight(
John Thompson1094c802010-09-13 18:15:37 +00002345 AsmOperandInfo &info, int maIndex) const {
John Thompsone8360b72010-10-29 17:29:13 +00002346 InlineAsm::ConstraintCodeVector *rCodes;
John Thompsonc467aa22010-09-21 22:04:54 +00002347 if (maIndex >= (int)info.multipleAlternatives.size())
2348 rCodes = &info.Codes;
2349 else
2350 rCodes = &info.multipleAlternatives[maIndex].Codes;
John Thompsone8360b72010-10-29 17:29:13 +00002351 ConstraintWeight BestWeight = CW_Invalid;
John Thompson1094c802010-09-13 18:15:37 +00002352
2353 // Loop over the options, keeping track of the most general one.
John Thompsonc467aa22010-09-21 22:04:54 +00002354 for (unsigned i = 0, e = rCodes->size(); i != e; ++i) {
John Thompsone8360b72010-10-29 17:29:13 +00002355 ConstraintWeight weight =
2356 getSingleConstraintMatchWeight(info, (*rCodes)[i].c_str());
John Thompson1094c802010-09-13 18:15:37 +00002357 if (weight > BestWeight)
2358 BestWeight = weight;
2359 }
2360
2361 return BestWeight;
2362}
2363
John Thompsone8360b72010-10-29 17:29:13 +00002364/// Examine constraint type and operand type and determine a weight value.
John Thompson1094c802010-09-13 18:15:37 +00002365/// This object must already have been set up with the operand type
2366/// and the current alternative constraint selected.
John Thompsone8360b72010-10-29 17:29:13 +00002367TargetLowering::ConstraintWeight
2368 TargetLowering::getSingleConstraintMatchWeight(
John Thompson1094c802010-09-13 18:15:37 +00002369 AsmOperandInfo &info, const char *constraint) const {
John Thompsone8360b72010-10-29 17:29:13 +00002370 ConstraintWeight weight = CW_Invalid;
John Thompson1094c802010-09-13 18:15:37 +00002371 Value *CallOperandVal = info.CallOperandVal;
2372 // If we don't have a value, we can't do a match,
2373 // but allow it at the lowest weight.
2374 if (CallOperandVal == NULL)
John Thompsone8360b72010-10-29 17:29:13 +00002375 return CW_Default;
John Thompson1094c802010-09-13 18:15:37 +00002376 // Look at the constraint type.
2377 switch (*constraint) {
2378 case 'i': // immediate integer.
2379 case 'n': // immediate integer with a known value.
John Thompsone8360b72010-10-29 17:29:13 +00002380 if (isa<ConstantInt>(CallOperandVal))
2381 weight = CW_Constant;
John Thompson1094c802010-09-13 18:15:37 +00002382 break;
2383 case 's': // non-explicit intregal immediate.
John Thompsone8360b72010-10-29 17:29:13 +00002384 if (isa<GlobalValue>(CallOperandVal))
2385 weight = CW_Constant;
John Thompson1094c802010-09-13 18:15:37 +00002386 break;
John Thompsone8360b72010-10-29 17:29:13 +00002387 case 'E': // immediate float if host format.
2388 case 'F': // immediate float.
2389 if (isa<ConstantFP>(CallOperandVal))
2390 weight = CW_Constant;
2391 break;
2392 case '<': // memory operand with autodecrement.
2393 case '>': // memory operand with autoincrement.
John Thompson1094c802010-09-13 18:15:37 +00002394 case 'm': // memory operand.
2395 case 'o': // offsettable memory operand
2396 case 'V': // non-offsettable memory operand
John Thompsone8360b72010-10-29 17:29:13 +00002397 weight = CW_Memory;
John Thompson1094c802010-09-13 18:15:37 +00002398 break;
John Thompsone8360b72010-10-29 17:29:13 +00002399 case 'r': // general register.
John Thompson1094c802010-09-13 18:15:37 +00002400 case 'g': // general register, memory operand or immediate integer.
John Thompsone8360b72010-10-29 17:29:13 +00002401 // note: Clang converts "g" to "imr".
2402 if (CallOperandVal->getType()->isIntegerTy())
2403 weight = CW_Register;
John Thompson1094c802010-09-13 18:15:37 +00002404 break;
John Thompsone8360b72010-10-29 17:29:13 +00002405 case 'X': // any operand.
John Thompson1094c802010-09-13 18:15:37 +00002406 default:
John Thompsone8360b72010-10-29 17:29:13 +00002407 weight = CW_Default;
John Thompson1094c802010-09-13 18:15:37 +00002408 break;
2409 }
2410 return weight;
2411}
2412
Chris Lattner47935152008-04-27 00:09:47 +00002413/// ChooseConstraint - If there are multiple different constraints that we
2414/// could pick for this operand (e.g. "imr") try to pick the 'best' one.
Chris Lattner58b9ece2008-04-27 01:49:46 +00002415/// This is somewhat tricky: constraints fall into four classes:
Chris Lattner47935152008-04-27 00:09:47 +00002416/// Other -> immediates and magic values
2417/// Register -> one specific register
2418/// RegisterClass -> a group of regs
2419/// Memory -> memory
2420/// Ideally, we would pick the most specific constraint possible: if we have
2421/// something that fits into a register, we would pick it. The problem here
2422/// is that if we have something that could either be in a register or in
2423/// memory that use of the register could cause selection of *other*
2424/// operands to fail: they might only succeed if we pick memory. Because of
2425/// this the heuristic we use is:
2426///
2427/// 1) If there is an 'other' constraint, and if the operand is valid for
2428/// that constraint, use it. This makes us take advantage of 'i'
2429/// constraints when available.
2430/// 2) Otherwise, pick the most general constraint present. This prefers
2431/// 'm' over 'r', for example.
2432///
2433static void ChooseConstraint(TargetLowering::AsmOperandInfo &OpInfo,
Dale Johannesence97d552010-06-25 21:55:36 +00002434 const TargetLowering &TLI,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002435 SDValue Op, SelectionDAG *DAG) {
Chris Lattner47935152008-04-27 00:09:47 +00002436 assert(OpInfo.Codes.size() > 1 && "Doesn't have multiple constraint options");
2437 unsigned BestIdx = 0;
2438 TargetLowering::ConstraintType BestType = TargetLowering::C_Unknown;
2439 int BestGenerality = -1;
Dale Johannesen17feb072010-06-28 22:09:45 +00002440
Chris Lattner47935152008-04-27 00:09:47 +00002441 // Loop over the options, keeping track of the most general one.
2442 for (unsigned i = 0, e = OpInfo.Codes.size(); i != e; ++i) {
2443 TargetLowering::ConstraintType CType =
2444 TLI.getConstraintType(OpInfo.Codes[i]);
Dale Johannesen17feb072010-06-28 22:09:45 +00002445
Chris Lattner22379732008-04-27 00:37:18 +00002446 // If this is an 'other' constraint, see if the operand is valid for it.
2447 // For example, on X86 we might have an 'rI' constraint. If the operand
2448 // is an integer in the range [0..31] we want to use I (saving a load
2449 // of a register), otherwise we must use 'r'.
Gabor Greiff304a7a2008-08-28 21:40:38 +00002450 if (CType == TargetLowering::C_Other && Op.getNode()) {
Chris Lattner22379732008-04-27 00:37:18 +00002451 assert(OpInfo.Codes[i].size() == 1 &&
2452 "Unhandled multi-letter 'other' constraint");
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002453 std::vector<SDValue> ResultOps;
Eric Christopherde9399b2011-06-02 23:16:42 +00002454 TLI.LowerAsmOperandForConstraint(Op, OpInfo.Codes[i],
Chris Lattner22379732008-04-27 00:37:18 +00002455 ResultOps, *DAG);
2456 if (!ResultOps.empty()) {
2457 BestType = CType;
2458 BestIdx = i;
2459 break;
2460 }
2461 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002462
Dale Johannesen17feb072010-06-28 22:09:45 +00002463 // Things with matching constraints can only be registers, per gcc
2464 // documentation. This mainly affects "g" constraints.
2465 if (CType == TargetLowering::C_Memory && OpInfo.hasMatchingInput())
2466 continue;
Wesley Peck527da1b2010-11-23 03:31:01 +00002467
Chris Lattner47935152008-04-27 00:09:47 +00002468 // This constraint letter is more general than the previous one, use it.
2469 int Generality = getConstraintGenerality(CType);
2470 if (Generality > BestGenerality) {
2471 BestType = CType;
2472 BestIdx = i;
2473 BestGenerality = Generality;
2474 }
2475 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002476
Chris Lattner47935152008-04-27 00:09:47 +00002477 OpInfo.ConstraintCode = OpInfo.Codes[BestIdx];
2478 OpInfo.ConstraintType = BestType;
2479}
2480
2481/// ComputeConstraintToUse - Determines the constraint code and constraint
2482/// type to use for the specific AsmOperandInfo, setting
2483/// OpInfo.ConstraintCode and OpInfo.ConstraintType.
Chris Lattner22379732008-04-27 00:37:18 +00002484void TargetLowering::ComputeConstraintToUse(AsmOperandInfo &OpInfo,
Wesley Peck527da1b2010-11-23 03:31:01 +00002485 SDValue Op,
Chris Lattner22379732008-04-27 00:37:18 +00002486 SelectionDAG *DAG) const {
Chris Lattner47935152008-04-27 00:09:47 +00002487 assert(!OpInfo.Codes.empty() && "Must have at least one constraint");
Wesley Peck527da1b2010-11-23 03:31:01 +00002488
Chris Lattner47935152008-04-27 00:09:47 +00002489 // Single-letter constraints ('r') are very common.
2490 if (OpInfo.Codes.size() == 1) {
2491 OpInfo.ConstraintCode = OpInfo.Codes[0];
2492 OpInfo.ConstraintType = getConstraintType(OpInfo.ConstraintCode);
2493 } else {
Dale Johannesence97d552010-06-25 21:55:36 +00002494 ChooseConstraint(OpInfo, *this, Op, DAG);
Chris Lattner47935152008-04-27 00:09:47 +00002495 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002496
Chris Lattner47935152008-04-27 00:09:47 +00002497 // 'X' matches anything.
2498 if (OpInfo.ConstraintCode == "X" && OpInfo.CallOperandVal) {
2499 // Labels and constants are handled elsewhere ('X' is the only thing
Dale Johannesen4e331152009-07-07 23:26:33 +00002500 // that matches labels). For Functions, the type here is the type of
Dale Johannesenade297d2009-07-20 23:27:39 +00002501 // the result, which is not what we want to look at; leave them alone.
2502 Value *v = OpInfo.CallOperandVal;
Dale Johannesen4e331152009-07-07 23:26:33 +00002503 if (isa<BasicBlock>(v) || isa<ConstantInt>(v) || isa<Function>(v)) {
2504 OpInfo.CallOperandVal = v;
Chris Lattner47935152008-04-27 00:09:47 +00002505 return;
Dale Johannesen4e331152009-07-07 23:26:33 +00002506 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002507
Chris Lattner47935152008-04-27 00:09:47 +00002508 // Otherwise, try to resolve it to something we know about by looking at
2509 // the actual operand type.
2510 if (const char *Repl = LowerXConstraint(OpInfo.ConstraintVT)) {
2511 OpInfo.ConstraintCode = Repl;
2512 OpInfo.ConstraintType = getConstraintType(OpInfo.ConstraintCode);
2513 }
2514 }
2515}
2516
David Majnemer0fc86702013-06-08 23:51:45 +00002517/// \brief Given an exact SDIV by a constant, create a multiplication
Benjamin Kramer9960a252011-07-08 10:31:30 +00002518/// with the multiplicative inverse of the constant.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002519SDValue TargetLowering::BuildExactSDIV(SDValue Op1, SDValue Op2, SDLoc dl,
Benjamin Kramer9960a252011-07-08 10:31:30 +00002520 SelectionDAG &DAG) const {
2521 ConstantSDNode *C = cast<ConstantSDNode>(Op2);
2522 APInt d = C->getAPIntValue();
2523 assert(d != 0 && "Division by zero!");
2524
2525 // Shift the value upfront if it is even, so the LSB is one.
2526 unsigned ShAmt = d.countTrailingZeros();
2527 if (ShAmt) {
2528 // TODO: For UDIV use SRL instead of SRA.
2529 SDValue Amt = DAG.getConstant(ShAmt, getShiftAmountTy(Op1.getValueType()));
2530 Op1 = DAG.getNode(ISD::SRA, dl, Op1.getValueType(), Op1, Amt);
2531 d = d.ashr(ShAmt);
2532 }
2533
2534 // Calculate the multiplicative inverse, using Newton's method.
2535 APInt t, xn = d;
2536 while ((t = d*xn) != 1)
2537 xn *= APInt(d.getBitWidth(), 2) - t;
2538
2539 Op2 = DAG.getConstant(xn, Op1.getValueType());
2540 return DAG.getNode(ISD::MUL, dl, Op1.getValueType(), Op1, Op2);
2541}
2542
David Majnemer0fc86702013-06-08 23:51:45 +00002543/// \brief Given an ISD::SDIV node expressing a divide by constant,
Andrew Lenharth1dc9ec52006-05-16 17:42:15 +00002544/// return a DAG expression to select that will generate the same value by
2545/// multiplying by a magic number. See:
2546/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Richard Osborne561fac42011-11-07 17:09:05 +00002547SDValue TargetLowering::
2548BuildSDIV(SDNode *N, SelectionDAG &DAG, bool IsAfterLegalization,
Eric Christopher200dd762012-12-10 22:00:20 +00002549 std::vector<SDNode*> *Created) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002550 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002551 SDLoc dl(N);
Wesley Peck527da1b2010-11-23 03:31:01 +00002552
Andrew Lenharth1dc9ec52006-05-16 17:42:15 +00002553 // Check to see if we can do this.
Eli Friedmanc8228d22008-11-30 06:35:39 +00002554 // FIXME: We should be more aggressive here.
2555 if (!isTypeLegal(VT))
2556 return SDValue();
Wesley Peck527da1b2010-11-23 03:31:01 +00002557
Eli Friedmanc8228d22008-11-30 06:35:39 +00002558 APInt d = cast<ConstantSDNode>(N->getOperand(1))->getAPIntValue();
Jay Foadfe0c6482009-04-30 10:15:35 +00002559 APInt::ms magics = d.magic();
Wesley Peck527da1b2010-11-23 03:31:01 +00002560
Andrew Lenharth1dc9ec52006-05-16 17:42:15 +00002561 // Multiply the numerator (operand 0) by the magic value
Eli Friedmanc8228d22008-11-30 06:35:39 +00002562 // FIXME: We should support doing a MUL in a wider type
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002563 SDValue Q;
Richard Osborne561fac42011-11-07 17:09:05 +00002564 if (IsAfterLegalization ? isOperationLegal(ISD::MULHS, VT) :
2565 isOperationLegalOrCustom(ISD::MULHS, VT))
Dale Johannesenf1163e92009-02-03 00:47:48 +00002566 Q = DAG.getNode(ISD::MULHS, dl, VT, N->getOperand(0),
Dan Gohmana1603612007-10-08 18:33:35 +00002567 DAG.getConstant(magics.m, VT));
Richard Osborne561fac42011-11-07 17:09:05 +00002568 else if (IsAfterLegalization ? isOperationLegal(ISD::SMUL_LOHI, VT) :
2569 isOperationLegalOrCustom(ISD::SMUL_LOHI, VT))
Dale Johannesenf1163e92009-02-03 00:47:48 +00002570 Q = SDValue(DAG.getNode(ISD::SMUL_LOHI, dl, DAG.getVTList(VT, VT),
Dan Gohmana1603612007-10-08 18:33:35 +00002571 N->getOperand(0),
Gabor Greiff304a7a2008-08-28 21:40:38 +00002572 DAG.getConstant(magics.m, VT)).getNode(), 1);
Dan Gohmana1603612007-10-08 18:33:35 +00002573 else
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002574 return SDValue(); // No mulhs or equvialent
Andrew Lenharth1dc9ec52006-05-16 17:42:15 +00002575 // If d > 0 and m < 0, add the numerator
Wesley Peck527da1b2010-11-23 03:31:01 +00002576 if (d.isStrictlyPositive() && magics.m.isNegative()) {
Dale Johannesenf1163e92009-02-03 00:47:48 +00002577 Q = DAG.getNode(ISD::ADD, dl, VT, Q, N->getOperand(0));
Andrew Lenharth1dc9ec52006-05-16 17:42:15 +00002578 if (Created)
Gabor Greiff304a7a2008-08-28 21:40:38 +00002579 Created->push_back(Q.getNode());
Andrew Lenharth1dc9ec52006-05-16 17:42:15 +00002580 }
2581 // If d < 0 and m > 0, subtract the numerator.
Eli Friedmanc8228d22008-11-30 06:35:39 +00002582 if (d.isNegative() && magics.m.isStrictlyPositive()) {
Dale Johannesenf1163e92009-02-03 00:47:48 +00002583 Q = DAG.getNode(ISD::SUB, dl, VT, Q, N->getOperand(0));
Andrew Lenharth1dc9ec52006-05-16 17:42:15 +00002584 if (Created)
Gabor Greiff304a7a2008-08-28 21:40:38 +00002585 Created->push_back(Q.getNode());
Andrew Lenharth1dc9ec52006-05-16 17:42:15 +00002586 }
2587 // Shift right algebraic if shift value is nonzero
2588 if (magics.s > 0) {
Wesley Peck527da1b2010-11-23 03:31:01 +00002589 Q = DAG.getNode(ISD::SRA, dl, VT, Q,
Owen Andersonb2c80da2011-02-25 21:41:48 +00002590 DAG.getConstant(magics.s, getShiftAmountTy(Q.getValueType())));
Andrew Lenharth1dc9ec52006-05-16 17:42:15 +00002591 if (Created)
Gabor Greiff304a7a2008-08-28 21:40:38 +00002592 Created->push_back(Q.getNode());
Andrew Lenharth1dc9ec52006-05-16 17:42:15 +00002593 }
2594 // Extract the sign bit and add it to the quotient
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002595 SDValue T =
Dale Johannesenf1163e92009-02-03 00:47:48 +00002596 DAG.getNode(ISD::SRL, dl, VT, Q, DAG.getConstant(VT.getSizeInBits()-1,
Owen Andersonb2c80da2011-02-25 21:41:48 +00002597 getShiftAmountTy(Q.getValueType())));
Andrew Lenharth1dc9ec52006-05-16 17:42:15 +00002598 if (Created)
Gabor Greiff304a7a2008-08-28 21:40:38 +00002599 Created->push_back(T.getNode());
Dale Johannesenf1163e92009-02-03 00:47:48 +00002600 return DAG.getNode(ISD::ADD, dl, VT, Q, T);
Andrew Lenharth1dc9ec52006-05-16 17:42:15 +00002601}
2602
David Majnemer0fc86702013-06-08 23:51:45 +00002603/// \brief Given an ISD::UDIV node expressing a divide by constant,
Andrew Lenharth1dc9ec52006-05-16 17:42:15 +00002604/// return a DAG expression to select that will generate the same value by
2605/// multiplying by a magic number. See:
2606/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Richard Osborne561fac42011-11-07 17:09:05 +00002607SDValue TargetLowering::
2608BuildUDIV(SDNode *N, SelectionDAG &DAG, bool IsAfterLegalization,
Eric Christopher200dd762012-12-10 22:00:20 +00002609 std::vector<SDNode*> *Created) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002610 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002611 SDLoc dl(N);
Eli Friedman1b7fc152008-11-30 06:02:26 +00002612
Andrew Lenharth1dc9ec52006-05-16 17:42:15 +00002613 // Check to see if we can do this.
Eli Friedman1b7fc152008-11-30 06:02:26 +00002614 // FIXME: We should be more aggressive here.
2615 if (!isTypeLegal(VT))
2616 return SDValue();
2617
2618 // FIXME: We should use a narrower constant when the upper
2619 // bits are known to be zero.
Benjamin Kramercfcea122011-03-17 20:39:14 +00002620 const APInt &N1C = cast<ConstantSDNode>(N->getOperand(1))->getAPIntValue();
2621 APInt::mu magics = N1C.magicu();
2622
2623 SDValue Q = N->getOperand(0);
2624
2625 // If the divisor is even, we can avoid using the expensive fixup by shifting
2626 // the divided value upfront.
2627 if (magics.a != 0 && !N1C[0]) {
2628 unsigned Shift = N1C.countTrailingZeros();
2629 Q = DAG.getNode(ISD::SRL, dl, VT, Q,
2630 DAG.getConstant(Shift, getShiftAmountTy(Q.getValueType())));
2631 if (Created)
2632 Created->push_back(Q.getNode());
2633
2634 // Get magic number for the shifted divisor.
2635 magics = N1C.lshr(Shift).magicu(Shift);
2636 assert(magics.a == 0 && "Should use cheap fixup now");
2637 }
Eli Friedman1b7fc152008-11-30 06:02:26 +00002638
Andrew Lenharth1dc9ec52006-05-16 17:42:15 +00002639 // Multiply the numerator (operand 0) by the magic value
Eli Friedman1b7fc152008-11-30 06:02:26 +00002640 // FIXME: We should support doing a MUL in a wider type
Richard Osborne561fac42011-11-07 17:09:05 +00002641 if (IsAfterLegalization ? isOperationLegal(ISD::MULHU, VT) :
2642 isOperationLegalOrCustom(ISD::MULHU, VT))
Benjamin Kramercfcea122011-03-17 20:39:14 +00002643 Q = DAG.getNode(ISD::MULHU, dl, VT, Q, DAG.getConstant(magics.m, VT));
Richard Osborne561fac42011-11-07 17:09:05 +00002644 else if (IsAfterLegalization ? isOperationLegal(ISD::UMUL_LOHI, VT) :
2645 isOperationLegalOrCustom(ISD::UMUL_LOHI, VT))
Benjamin Kramercfcea122011-03-17 20:39:14 +00002646 Q = SDValue(DAG.getNode(ISD::UMUL_LOHI, dl, DAG.getVTList(VT, VT), Q,
2647 DAG.getConstant(magics.m, VT)).getNode(), 1);
Dan Gohmana1603612007-10-08 18:33:35 +00002648 else
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002649 return SDValue(); // No mulhu or equvialent
Andrew Lenharth1dc9ec52006-05-16 17:42:15 +00002650 if (Created)
Gabor Greiff304a7a2008-08-28 21:40:38 +00002651 Created->push_back(Q.getNode());
Andrew Lenharth1dc9ec52006-05-16 17:42:15 +00002652
2653 if (magics.a == 0) {
Benjamin Kramercfcea122011-03-17 20:39:14 +00002654 assert(magics.s < N1C.getBitWidth() &&
Eli Friedman1b7fc152008-11-30 06:02:26 +00002655 "We shouldn't generate an undefined shift!");
Wesley Peck527da1b2010-11-23 03:31:01 +00002656 return DAG.getNode(ISD::SRL, dl, VT, Q,
Owen Andersonb2c80da2011-02-25 21:41:48 +00002657 DAG.getConstant(magics.s, getShiftAmountTy(Q.getValueType())));
Andrew Lenharth1dc9ec52006-05-16 17:42:15 +00002658 } else {
Dale Johannesenf1163e92009-02-03 00:47:48 +00002659 SDValue NPQ = DAG.getNode(ISD::SUB, dl, VT, N->getOperand(0), Q);
Andrew Lenharth1dc9ec52006-05-16 17:42:15 +00002660 if (Created)
Gabor Greiff304a7a2008-08-28 21:40:38 +00002661 Created->push_back(NPQ.getNode());
Wesley Peck527da1b2010-11-23 03:31:01 +00002662 NPQ = DAG.getNode(ISD::SRL, dl, VT, NPQ,
Owen Andersonb2c80da2011-02-25 21:41:48 +00002663 DAG.getConstant(1, getShiftAmountTy(NPQ.getValueType())));
Andrew Lenharth1dc9ec52006-05-16 17:42:15 +00002664 if (Created)
Gabor Greiff304a7a2008-08-28 21:40:38 +00002665 Created->push_back(NPQ.getNode());
Dale Johannesenf1163e92009-02-03 00:47:48 +00002666 NPQ = DAG.getNode(ISD::ADD, dl, VT, NPQ, Q);
Andrew Lenharth1dc9ec52006-05-16 17:42:15 +00002667 if (Created)
Gabor Greiff304a7a2008-08-28 21:40:38 +00002668 Created->push_back(NPQ.getNode());
Wesley Peck527da1b2010-11-23 03:31:01 +00002669 return DAG.getNode(ISD::SRL, dl, VT, NPQ,
Owen Andersonb2c80da2011-02-25 21:41:48 +00002670 DAG.getConstant(magics.s-1, getShiftAmountTy(NPQ.getValueType())));
Andrew Lenharth1dc9ec52006-05-16 17:42:15 +00002671 }
2672}
Bill Wendling908bf812014-01-06 00:43:20 +00002673
2674bool TargetLowering::
2675verifyReturnAddressArgumentIsConstant(SDValue Op, SelectionDAG &DAG) const {
2676 if (!isa<ConstantSDNode>(Op.getOperand(0))) {
2677 DAG.getContext()->emitError("argument to '__builtin_return_address' must "
2678 "be a constant integer");
2679 return true;
2680 }
2681
2682 return false;
2683}