blob: 351fe81a455e8ec062a52d6a06366b9726b29e50 [file] [log] [blame]
Chris Lattner310968c2005-01-07 07:44:53 +00001//===-- TargetLowering.cpp - Implement the TargetLowering class -----------===//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
Chris Lattner310968c2005-01-07 07:44:53 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanf976c852005-04-21 22:55:34 +00007//
Chris Lattner310968c2005-01-07 07:44:53 +00008//===----------------------------------------------------------------------===//
9//
10// This implements the TargetLowering class.
11//
12//===----------------------------------------------------------------------===//
13
Evan Cheng5c807602008-02-26 02:33:44 +000014#include "llvm/Target/TargetAsmInfo.h"
Chris Lattner310968c2005-01-07 07:44:53 +000015#include "llvm/Target/TargetLowering.h"
Rafael Espindolaf1ba1ca2007-11-05 23:12:20 +000016#include "llvm/Target/TargetSubtarget.h"
Owen Anderson07000c62006-05-12 06:33:49 +000017#include "llvm/Target/TargetData.h"
Chris Lattner310968c2005-01-07 07:44:53 +000018#include "llvm/Target/TargetMachine.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000019#include "llvm/Target/TargetRegisterInfo.h"
Evan Cheng5c807602008-02-26 02:33:44 +000020#include "llvm/CallingConv.h"
Chris Lattnerdc879292006-03-31 00:28:56 +000021#include "llvm/DerivedTypes.h"
Chris Lattner310968c2005-01-07 07:44:53 +000022#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner4ccb0702006-01-26 20:37:03 +000023#include "llvm/ADT/StringExtras.h"
Owen Anderson718cb662007-09-07 04:06:50 +000024#include "llvm/ADT/STLExtras.h"
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +000025#include "llvm/Support/MathExtras.h"
Chris Lattner310968c2005-01-07 07:44:53 +000026using namespace llvm;
27
Evan Cheng56966222007-01-12 02:11:51 +000028/// InitLibcallNames - Set default libcall names.
29///
Evan Cheng79cca502007-01-12 22:51:10 +000030static void InitLibcallNames(const char **Names) {
Evan Cheng56966222007-01-12 02:11:51 +000031 Names[RTLIB::SHL_I32] = "__ashlsi3";
32 Names[RTLIB::SHL_I64] = "__ashldi3";
33 Names[RTLIB::SRL_I32] = "__lshrsi3";
34 Names[RTLIB::SRL_I64] = "__lshrdi3";
35 Names[RTLIB::SRA_I32] = "__ashrsi3";
36 Names[RTLIB::SRA_I64] = "__ashrdi3";
37 Names[RTLIB::MUL_I32] = "__mulsi3";
38 Names[RTLIB::MUL_I64] = "__muldi3";
39 Names[RTLIB::SDIV_I32] = "__divsi3";
40 Names[RTLIB::SDIV_I64] = "__divdi3";
41 Names[RTLIB::UDIV_I32] = "__udivsi3";
42 Names[RTLIB::UDIV_I64] = "__udivdi3";
43 Names[RTLIB::SREM_I32] = "__modsi3";
44 Names[RTLIB::SREM_I64] = "__moddi3";
45 Names[RTLIB::UREM_I32] = "__umodsi3";
46 Names[RTLIB::UREM_I64] = "__umoddi3";
47 Names[RTLIB::NEG_I32] = "__negsi2";
48 Names[RTLIB::NEG_I64] = "__negdi2";
49 Names[RTLIB::ADD_F32] = "__addsf3";
50 Names[RTLIB::ADD_F64] = "__adddf3";
Duncan Sands007f9842008-01-10 10:28:30 +000051 Names[RTLIB::ADD_F80] = "__addxf3";
Dale Johannesen161e8972007-10-05 20:04:43 +000052 Names[RTLIB::ADD_PPCF128] = "__gcc_qadd";
Evan Cheng56966222007-01-12 02:11:51 +000053 Names[RTLIB::SUB_F32] = "__subsf3";
54 Names[RTLIB::SUB_F64] = "__subdf3";
Duncan Sands007f9842008-01-10 10:28:30 +000055 Names[RTLIB::SUB_F80] = "__subxf3";
Dale Johannesen161e8972007-10-05 20:04:43 +000056 Names[RTLIB::SUB_PPCF128] = "__gcc_qsub";
Evan Cheng56966222007-01-12 02:11:51 +000057 Names[RTLIB::MUL_F32] = "__mulsf3";
58 Names[RTLIB::MUL_F64] = "__muldf3";
Duncan Sands007f9842008-01-10 10:28:30 +000059 Names[RTLIB::MUL_F80] = "__mulxf3";
Dale Johannesen161e8972007-10-05 20:04:43 +000060 Names[RTLIB::MUL_PPCF128] = "__gcc_qmul";
Evan Cheng56966222007-01-12 02:11:51 +000061 Names[RTLIB::DIV_F32] = "__divsf3";
62 Names[RTLIB::DIV_F64] = "__divdf3";
Duncan Sands007f9842008-01-10 10:28:30 +000063 Names[RTLIB::DIV_F80] = "__divxf3";
Dale Johannesen161e8972007-10-05 20:04:43 +000064 Names[RTLIB::DIV_PPCF128] = "__gcc_qdiv";
Evan Cheng56966222007-01-12 02:11:51 +000065 Names[RTLIB::REM_F32] = "fmodf";
66 Names[RTLIB::REM_F64] = "fmod";
Duncan Sands007f9842008-01-10 10:28:30 +000067 Names[RTLIB::REM_F80] = "fmodl";
Dale Johannesen161e8972007-10-05 20:04:43 +000068 Names[RTLIB::REM_PPCF128] = "fmodl";
Evan Cheng56966222007-01-12 02:11:51 +000069 Names[RTLIB::POWI_F32] = "__powisf2";
70 Names[RTLIB::POWI_F64] = "__powidf2";
Dale Johannesen161e8972007-10-05 20:04:43 +000071 Names[RTLIB::POWI_F80] = "__powixf2";
72 Names[RTLIB::POWI_PPCF128] = "__powitf2";
Evan Cheng56966222007-01-12 02:11:51 +000073 Names[RTLIB::SQRT_F32] = "sqrtf";
74 Names[RTLIB::SQRT_F64] = "sqrt";
Dale Johannesen161e8972007-10-05 20:04:43 +000075 Names[RTLIB::SQRT_F80] = "sqrtl";
76 Names[RTLIB::SQRT_PPCF128] = "sqrtl";
Evan Cheng56966222007-01-12 02:11:51 +000077 Names[RTLIB::SIN_F32] = "sinf";
78 Names[RTLIB::SIN_F64] = "sin";
Duncan Sands007f9842008-01-10 10:28:30 +000079 Names[RTLIB::SIN_F80] = "sinl";
80 Names[RTLIB::SIN_PPCF128] = "sinl";
Evan Cheng56966222007-01-12 02:11:51 +000081 Names[RTLIB::COS_F32] = "cosf";
82 Names[RTLIB::COS_F64] = "cos";
Duncan Sands007f9842008-01-10 10:28:30 +000083 Names[RTLIB::COS_F80] = "cosl";
84 Names[RTLIB::COS_PPCF128] = "cosl";
Dan Gohmane54be102007-10-11 23:09:10 +000085 Names[RTLIB::POW_F32] = "powf";
86 Names[RTLIB::POW_F64] = "pow";
87 Names[RTLIB::POW_F80] = "powl";
88 Names[RTLIB::POW_PPCF128] = "powl";
Evan Cheng56966222007-01-12 02:11:51 +000089 Names[RTLIB::FPEXT_F32_F64] = "__extendsfdf2";
90 Names[RTLIB::FPROUND_F64_F32] = "__truncdfsf2";
91 Names[RTLIB::FPTOSINT_F32_I32] = "__fixsfsi";
92 Names[RTLIB::FPTOSINT_F32_I64] = "__fixsfdi";
93 Names[RTLIB::FPTOSINT_F64_I32] = "__fixdfsi";
94 Names[RTLIB::FPTOSINT_F64_I64] = "__fixdfdi";
Dale Johannesen161e8972007-10-05 20:04:43 +000095 Names[RTLIB::FPTOSINT_F80_I64] = "__fixxfdi";
96 Names[RTLIB::FPTOSINT_PPCF128_I64] = "__fixtfdi";
Evan Cheng56966222007-01-12 02:11:51 +000097 Names[RTLIB::FPTOUINT_F32_I32] = "__fixunssfsi";
98 Names[RTLIB::FPTOUINT_F32_I64] = "__fixunssfdi";
99 Names[RTLIB::FPTOUINT_F64_I32] = "__fixunsdfsi";
100 Names[RTLIB::FPTOUINT_F64_I64] = "__fixunsdfdi";
Dale Johannesen161e8972007-10-05 20:04:43 +0000101 Names[RTLIB::FPTOUINT_F80_I32] = "__fixunsxfsi";
102 Names[RTLIB::FPTOUINT_F80_I64] = "__fixunsxfdi";
103 Names[RTLIB::FPTOUINT_PPCF128_I64] = "__fixunstfdi";
Evan Cheng56966222007-01-12 02:11:51 +0000104 Names[RTLIB::SINTTOFP_I32_F32] = "__floatsisf";
105 Names[RTLIB::SINTTOFP_I32_F64] = "__floatsidf";
106 Names[RTLIB::SINTTOFP_I64_F32] = "__floatdisf";
107 Names[RTLIB::SINTTOFP_I64_F64] = "__floatdidf";
Dale Johannesen161e8972007-10-05 20:04:43 +0000108 Names[RTLIB::SINTTOFP_I64_F80] = "__floatdixf";
109 Names[RTLIB::SINTTOFP_I64_PPCF128] = "__floatditf";
Dan Gohmand91446d2008-03-05 01:08:17 +0000110 Names[RTLIB::SINTTOFP_I128_F32] = "__floattisf";
111 Names[RTLIB::SINTTOFP_I128_F64] = "__floattidf";
112 Names[RTLIB::SINTTOFP_I128_F80] = "__floattixf";
113 Names[RTLIB::SINTTOFP_I128_PPCF128] = "__floattitf";
Evan Cheng56966222007-01-12 02:11:51 +0000114 Names[RTLIB::UINTTOFP_I32_F32] = "__floatunsisf";
115 Names[RTLIB::UINTTOFP_I32_F64] = "__floatunsidf";
116 Names[RTLIB::UINTTOFP_I64_F32] = "__floatundisf";
117 Names[RTLIB::UINTTOFP_I64_F64] = "__floatundidf";
118 Names[RTLIB::OEQ_F32] = "__eqsf2";
119 Names[RTLIB::OEQ_F64] = "__eqdf2";
120 Names[RTLIB::UNE_F32] = "__nesf2";
121 Names[RTLIB::UNE_F64] = "__nedf2";
122 Names[RTLIB::OGE_F32] = "__gesf2";
123 Names[RTLIB::OGE_F64] = "__gedf2";
124 Names[RTLIB::OLT_F32] = "__ltsf2";
125 Names[RTLIB::OLT_F64] = "__ltdf2";
126 Names[RTLIB::OLE_F32] = "__lesf2";
127 Names[RTLIB::OLE_F64] = "__ledf2";
128 Names[RTLIB::OGT_F32] = "__gtsf2";
129 Names[RTLIB::OGT_F64] = "__gtdf2";
130 Names[RTLIB::UO_F32] = "__unordsf2";
131 Names[RTLIB::UO_F64] = "__unorddf2";
Evan Chengd385fd62007-01-31 09:29:11 +0000132 Names[RTLIB::O_F32] = "__unordsf2";
133 Names[RTLIB::O_F64] = "__unorddf2";
134}
135
136/// InitCmpLibcallCCs - Set default comparison libcall CC.
137///
138static void InitCmpLibcallCCs(ISD::CondCode *CCs) {
139 memset(CCs, ISD::SETCC_INVALID, sizeof(ISD::CondCode)*RTLIB::UNKNOWN_LIBCALL);
140 CCs[RTLIB::OEQ_F32] = ISD::SETEQ;
141 CCs[RTLIB::OEQ_F64] = ISD::SETEQ;
142 CCs[RTLIB::UNE_F32] = ISD::SETNE;
143 CCs[RTLIB::UNE_F64] = ISD::SETNE;
144 CCs[RTLIB::OGE_F32] = ISD::SETGE;
145 CCs[RTLIB::OGE_F64] = ISD::SETGE;
146 CCs[RTLIB::OLT_F32] = ISD::SETLT;
147 CCs[RTLIB::OLT_F64] = ISD::SETLT;
148 CCs[RTLIB::OLE_F32] = ISD::SETLE;
149 CCs[RTLIB::OLE_F64] = ISD::SETLE;
150 CCs[RTLIB::OGT_F32] = ISD::SETGT;
151 CCs[RTLIB::OGT_F64] = ISD::SETGT;
152 CCs[RTLIB::UO_F32] = ISD::SETNE;
153 CCs[RTLIB::UO_F64] = ISD::SETNE;
154 CCs[RTLIB::O_F32] = ISD::SETEQ;
155 CCs[RTLIB::O_F64] = ISD::SETEQ;
Evan Cheng56966222007-01-12 02:11:51 +0000156}
157
Chris Lattner310968c2005-01-07 07:44:53 +0000158TargetLowering::TargetLowering(TargetMachine &tm)
Chris Lattner3e6e8cc2006-01-29 08:41:12 +0000159 : TM(tm), TD(TM.getTargetData()) {
Evan Cheng33143dc2006-03-03 06:58:59 +0000160 assert(ISD::BUILTIN_OP_END <= 156 &&
Chris Lattner310968c2005-01-07 07:44:53 +0000161 "Fixed size array in TargetLowering is not large enough!");
Chris Lattnercba82f92005-01-16 07:28:11 +0000162 // All operations default to being supported.
163 memset(OpActions, 0, sizeof(OpActions));
Evan Chengc5484282006-10-04 00:56:09 +0000164 memset(LoadXActions, 0, sizeof(LoadXActions));
Chris Lattnerddf89562008-01-17 19:59:44 +0000165 memset(TruncStoreActions, 0, sizeof(TruncStoreActions));
Chris Lattnerc9133f92008-01-18 19:36:20 +0000166 memset(IndexedModeActions, 0, sizeof(IndexedModeActions));
167 memset(ConvertActions, 0, sizeof(ConvertActions));
Dan Gohman93f81e22007-07-09 20:49:44 +0000168
Chris Lattner1a3048b2007-12-22 20:47:56 +0000169 // Set default actions for various operations.
Evan Cheng5ff839f2006-11-09 18:56:43 +0000170 for (unsigned VT = 0; VT != (unsigned)MVT::LAST_VALUETYPE; ++VT) {
Chris Lattner1a3048b2007-12-22 20:47:56 +0000171 // Default all indexed load / store to expand.
Evan Cheng5ff839f2006-11-09 18:56:43 +0000172 for (unsigned IM = (unsigned)ISD::PRE_INC;
173 IM != (unsigned)ISD::LAST_INDEXED_MODE; ++IM) {
174 setIndexedLoadAction(IM, (MVT::ValueType)VT, Expand);
175 setIndexedStoreAction(IM, (MVT::ValueType)VT, Expand);
176 }
Chris Lattner1a3048b2007-12-22 20:47:56 +0000177
178 // These operations default to expand.
179 setOperationAction(ISD::FGETSIGN, (MVT::ValueType)VT, Expand);
Evan Cheng5ff839f2006-11-09 18:56:43 +0000180 }
Nate Begemane1795842008-02-14 08:57:00 +0000181
182 // ConstantFP nodes default to expand. Targets can either change this to
183 // Legal, in which case all fp constants are legal, or use addLegalFPImmediate
184 // to optimize expansions for certain constants.
185 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
186 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
187 setOperationAction(ISD::ConstantFP, MVT::f80, Expand);
Chris Lattner310968c2005-01-07 07:44:53 +0000188
Chris Lattner41bab0b2008-01-15 21:58:08 +0000189 // Default ISD::TRAP to expand (which turns it into abort).
190 setOperationAction(ISD::TRAP, MVT::Other, Expand);
191
Owen Andersona69571c2006-05-03 01:29:57 +0000192 IsLittleEndian = TD->isLittleEndian();
Chris Lattnercf9668f2006-10-06 22:52:08 +0000193 UsesGlobalOffsetTable = false;
Owen Andersona69571c2006-05-03 01:29:57 +0000194 ShiftAmountTy = SetCCResultTy = PointerTy = getValueType(TD->getIntPtrType());
Chris Lattnerd6e49672005-01-19 03:36:14 +0000195 ShiftAmtHandling = Undefined;
Chris Lattner310968c2005-01-07 07:44:53 +0000196 memset(RegClassForVT, 0,MVT::LAST_VALUETYPE*sizeof(TargetRegisterClass*));
Owen Anderson718cb662007-09-07 04:06:50 +0000197 memset(TargetDAGCombineArray, 0, array_lengthof(TargetDAGCombineArray));
Evan Chenga03a5dc2006-02-14 08:38:30 +0000198 maxStoresPerMemset = maxStoresPerMemcpy = maxStoresPerMemmove = 8;
Reid Spencer0f9beca2005-08-27 19:09:02 +0000199 allowUnalignedMemoryAccesses = false;
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000200 UseUnderscoreSetJmp = false;
201 UseUnderscoreLongJmp = false;
Chris Lattner66180392007-02-25 01:28:05 +0000202 SelectIsExpensive = false;
Nate Begeman405e3ec2005-10-21 00:02:42 +0000203 IntDivIsCheap = false;
204 Pow2DivIsCheap = false;
Chris Lattneree4a7652006-01-25 18:57:15 +0000205 StackPointerRegisterToSaveRestore = 0;
Jim Laskey9bb3c932007-02-22 18:04:49 +0000206 ExceptionPointerRegister = 0;
207 ExceptionSelectorRegister = 0;
Chris Lattnerdfe89342007-09-21 17:06:39 +0000208 SetCCResultContents = UndefinedSetCCResult;
Evan Cheng0577a222006-01-25 18:52:42 +0000209 SchedPreferenceInfo = SchedulingForLatency;
Chris Lattner7acf5f32006-09-05 17:39:15 +0000210 JumpBufSize = 0;
Duraid Madina0c9e0ff2006-09-04 07:44:11 +0000211 JumpBufAlignment = 0;
Evan Chengd60483e2007-05-16 23:45:53 +0000212 IfCvtBlockSizeLimit = 2;
Evan Chengfb8075d2008-02-28 00:43:03 +0000213 IfCvtDupBlockSizeLimit = 0;
214 PrefLoopAlignment = 0;
Evan Cheng56966222007-01-12 02:11:51 +0000215
216 InitLibcallNames(LibcallRoutineNames);
Evan Chengd385fd62007-01-31 09:29:11 +0000217 InitCmpLibcallCCs(CmpLibcallCCs);
Dan Gohmanc3b0b5c2007-09-25 15:10:49 +0000218
219 // Tell Legalize whether the assembler supports DEBUG_LOC.
220 if (!TM.getTargetAsmInfo()->hasDotLocAndDotFile())
221 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Chris Lattner310968c2005-01-07 07:44:53 +0000222}
223
Chris Lattnercba82f92005-01-16 07:28:11 +0000224TargetLowering::~TargetLowering() {}
225
Rafael Espindolaf1ba1ca2007-11-05 23:12:20 +0000226
227SDOperand TargetLowering::LowerMEMCPY(SDOperand Op, SelectionDAG &DAG) {
228 assert(getSubtarget() && "Subtarget not defined");
229 SDOperand ChainOp = Op.getOperand(0);
230 SDOperand DestOp = Op.getOperand(1);
231 SDOperand SourceOp = Op.getOperand(2);
232 SDOperand CountOp = Op.getOperand(3);
233 SDOperand AlignOp = Op.getOperand(4);
234 SDOperand AlwaysInlineOp = Op.getOperand(5);
235
236 bool AlwaysInline = (bool)cast<ConstantSDNode>(AlwaysInlineOp)->getValue();
237 unsigned Align = (unsigned)cast<ConstantSDNode>(AlignOp)->getValue();
238 if (Align == 0) Align = 1;
239
240 // If size is unknown, call memcpy.
241 ConstantSDNode *I = dyn_cast<ConstantSDNode>(CountOp);
242 if (!I) {
243 assert(!AlwaysInline && "Cannot inline copy of unknown size");
244 return LowerMEMCPYCall(ChainOp, DestOp, SourceOp, CountOp, DAG);
245 }
246
247 // If not DWORD aligned or if size is more than threshold, then call memcpy.
248 // The libc version is likely to be faster for the following cases. It can
249 // use the address value and run time information about the CPU.
250 // With glibc 2.6.1 on a core 2, coping an array of 100M longs was 30% faster
251 unsigned Size = I->getValue();
252 if (AlwaysInline ||
253 (Size <= getSubtarget()->getMaxInlineSizeThreshold() &&
254 (Align & 3) == 0))
255 return LowerMEMCPYInline(ChainOp, DestOp, SourceOp, Size, Align, DAG);
256 return LowerMEMCPYCall(ChainOp, DestOp, SourceOp, CountOp, DAG);
257}
258
259
260SDOperand TargetLowering::LowerMEMCPYCall(SDOperand Chain,
261 SDOperand Dest,
262 SDOperand Source,
263 SDOperand Count,
264 SelectionDAG &DAG) {
265 MVT::ValueType IntPtr = getPointerTy();
266 TargetLowering::ArgListTy Args;
267 TargetLowering::ArgListEntry Entry;
268 Entry.Ty = getTargetData()->getIntPtrType();
269 Entry.Node = Dest; Args.push_back(Entry);
270 Entry.Node = Source; Args.push_back(Entry);
271 Entry.Node = Count; Args.push_back(Entry);
272 std::pair<SDOperand,SDOperand> CallResult =
Duncan Sands00fee652008-02-14 17:28:50 +0000273 LowerCallTo(Chain, Type::VoidTy, false, false, false, CallingConv::C,
274 false, DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
Rafael Espindolaf1ba1ca2007-11-05 23:12:20 +0000275 return CallResult.second;
276}
277
278
Chris Lattner310968c2005-01-07 07:44:53 +0000279/// computeRegisterProperties - Once all of the register classes are added,
280/// this allows us to compute derived properties we expose.
281void TargetLowering::computeRegisterProperties() {
Nate Begeman6a648612005-11-29 05:45:29 +0000282 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattnerbb97d812005-01-16 01:10:58 +0000283 "Too many value types for ValueTypeActions to hold!");
284
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000285 // Everything defaults to needing one register.
286 for (unsigned i = 0; i != MVT::LAST_VALUETYPE; ++i) {
Dan Gohmanb9f10192007-06-21 14:42:22 +0000287 NumRegistersForVT[i] = 1;
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000288 RegisterTypeForVT[i] = TransformToType[i] = i;
289 }
290 // ...except isVoid, which doesn't need any registers.
291 NumRegistersForVT[MVT::isVoid] = 0;
Misha Brukmanf976c852005-04-21 22:55:34 +0000292
Chris Lattner310968c2005-01-07 07:44:53 +0000293 // Find the largest integer register class.
294 unsigned LargestIntReg = MVT::i128;
295 for (; RegClassForVT[LargestIntReg] == 0; --LargestIntReg)
296 assert(LargestIntReg != MVT::i1 && "No integer registers defined!");
297
298 // Every integer value type larger than this largest register takes twice as
299 // many registers to represent as the previous ValueType.
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000300 for (MVT::ValueType ExpandedReg = LargestIntReg + 1;
301 MVT::isInteger(ExpandedReg); ++ExpandedReg) {
Dan Gohmanb9f10192007-06-21 14:42:22 +0000302 NumRegistersForVT[ExpandedReg] = 2*NumRegistersForVT[ExpandedReg-1];
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000303 RegisterTypeForVT[ExpandedReg] = LargestIntReg;
304 TransformToType[ExpandedReg] = ExpandedReg - 1;
305 ValueTypeActions.setTypeAction(ExpandedReg, Expand);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +0000306 }
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000307
308 // Inspect all of the ValueType's smaller than the largest integer
309 // register to see which ones need promotion.
310 MVT::ValueType LegalIntReg = LargestIntReg;
311 for (MVT::ValueType IntReg = LargestIntReg - 1;
312 IntReg >= MVT::i1; --IntReg) {
313 if (isTypeLegal(IntReg)) {
314 LegalIntReg = IntReg;
315 } else {
316 RegisterTypeForVT[IntReg] = TransformToType[IntReg] = LegalIntReg;
317 ValueTypeActions.setTypeAction(IntReg, Promote);
318 }
319 }
320
Dale Johannesen161e8972007-10-05 20:04:43 +0000321 // ppcf128 type is really two f64's.
322 if (!isTypeLegal(MVT::ppcf128)) {
323 NumRegistersForVT[MVT::ppcf128] = 2*NumRegistersForVT[MVT::f64];
324 RegisterTypeForVT[MVT::ppcf128] = MVT::f64;
325 TransformToType[MVT::ppcf128] = MVT::f64;
326 ValueTypeActions.setTypeAction(MVT::ppcf128, Expand);
327 }
328
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000329 // Decide how to handle f64. If the target does not have native f64 support,
330 // expand it to i64 and we will be generating soft float library calls.
331 if (!isTypeLegal(MVT::f64)) {
332 NumRegistersForVT[MVT::f64] = NumRegistersForVT[MVT::i64];
333 RegisterTypeForVT[MVT::f64] = RegisterTypeForVT[MVT::i64];
334 TransformToType[MVT::f64] = MVT::i64;
335 ValueTypeActions.setTypeAction(MVT::f64, Expand);
336 }
337
338 // Decide how to handle f32. If the target does not have native support for
339 // f32, promote it to f64 if it is legal. Otherwise, expand it to i32.
340 if (!isTypeLegal(MVT::f32)) {
341 if (isTypeLegal(MVT::f64)) {
342 NumRegistersForVT[MVT::f32] = NumRegistersForVT[MVT::f64];
343 RegisterTypeForVT[MVT::f32] = RegisterTypeForVT[MVT::f64];
344 TransformToType[MVT::f32] = MVT::f64;
345 ValueTypeActions.setTypeAction(MVT::f32, Promote);
346 } else {
347 NumRegistersForVT[MVT::f32] = NumRegistersForVT[MVT::i32];
348 RegisterTypeForVT[MVT::f32] = RegisterTypeForVT[MVT::i32];
349 TransformToType[MVT::f32] = MVT::i32;
350 ValueTypeActions.setTypeAction(MVT::f32, Expand);
351 }
Evan Cheng1a8f1fe2006-12-09 02:42:38 +0000352 }
Nate Begeman4ef3b812005-11-22 01:29:36 +0000353
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000354 // Loop over all of the vector value types to see which need transformations.
355 for (MVT::ValueType i = MVT::FIRST_VECTOR_VALUETYPE;
Evan Cheng677274b2006-03-23 23:24:51 +0000356 i <= MVT::LAST_VECTOR_VALUETYPE; ++i) {
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000357 if (!isTypeLegal(i)) {
358 MVT::ValueType IntermediateVT, RegisterVT;
359 unsigned NumIntermediates;
360 NumRegistersForVT[i] =
361 getVectorTypeBreakdown(i,
362 IntermediateVT, NumIntermediates,
363 RegisterVT);
364 RegisterTypeForVT[i] = RegisterVT;
365 TransformToType[i] = MVT::Other; // this isn't actually used
366 ValueTypeActions.setTypeAction(i, Expand);
Dan Gohman7f321562007-06-25 16:23:39 +0000367 }
Chris Lattner3a5935842006-03-16 19:50:01 +0000368 }
Chris Lattnerbb97d812005-01-16 01:10:58 +0000369}
Chris Lattnercba82f92005-01-16 07:28:11 +0000370
Evan Cheng72261582005-12-20 06:22:03 +0000371const char *TargetLowering::getTargetNodeName(unsigned Opcode) const {
372 return NULL;
373}
Evan Cheng3a03ebb2005-12-21 23:05:39 +0000374
Dan Gohman7f321562007-06-25 16:23:39 +0000375/// getVectorTypeBreakdown - Vector types are broken down into some number of
376/// legal first class types. For example, MVT::v8f32 maps to 2 MVT::v4f32
Chris Lattnerdc879292006-03-31 00:28:56 +0000377/// with Altivec or SSE1, or 8 promoted MVT::f64 values with the X86 FP stack.
Dan Gohman7f321562007-06-25 16:23:39 +0000378/// Similarly, MVT::v2i64 turns into 4 MVT::i32 values with both PPC and X86.
Chris Lattnerdc879292006-03-31 00:28:56 +0000379///
Dan Gohman7f321562007-06-25 16:23:39 +0000380/// This method returns the number of registers needed, and the VT for each
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000381/// register. It also returns the VT and quantity of the intermediate values
382/// before they are promoted/expanded.
Chris Lattnerdc879292006-03-31 00:28:56 +0000383///
Dan Gohman7f321562007-06-25 16:23:39 +0000384unsigned TargetLowering::getVectorTypeBreakdown(MVT::ValueType VT,
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000385 MVT::ValueType &IntermediateVT,
386 unsigned &NumIntermediates,
387 MVT::ValueType &RegisterVT) const {
Chris Lattnerdc879292006-03-31 00:28:56 +0000388 // Figure out the right, legal destination reg to copy into.
Dan Gohman7f321562007-06-25 16:23:39 +0000389 unsigned NumElts = MVT::getVectorNumElements(VT);
390 MVT::ValueType EltTy = MVT::getVectorElementType(VT);
Chris Lattnerdc879292006-03-31 00:28:56 +0000391
392 unsigned NumVectorRegs = 1;
393
Nate Begemand73ab882007-11-27 19:28:48 +0000394 // FIXME: We don't support non-power-of-2-sized vectors for now. Ideally we
395 // could break down into LHS/RHS like LegalizeDAG does.
396 if (!isPowerOf2_32(NumElts)) {
397 NumVectorRegs = NumElts;
398 NumElts = 1;
399 }
400
Chris Lattnerdc879292006-03-31 00:28:56 +0000401 // Divide the input until we get to a supported size. This will always
402 // end with a scalar if the target doesn't support vectors.
Dan Gohman7f321562007-06-25 16:23:39 +0000403 while (NumElts > 1 &&
404 !isTypeLegal(MVT::getVectorType(EltTy, NumElts))) {
Chris Lattnerdc879292006-03-31 00:28:56 +0000405 NumElts >>= 1;
406 NumVectorRegs <<= 1;
407 }
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000408
409 NumIntermediates = NumVectorRegs;
Chris Lattnerdc879292006-03-31 00:28:56 +0000410
Dan Gohman7f321562007-06-25 16:23:39 +0000411 MVT::ValueType NewVT = MVT::getVectorType(EltTy, NumElts);
412 if (!isTypeLegal(NewVT))
413 NewVT = EltTy;
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000414 IntermediateVT = NewVT;
Chris Lattnerdc879292006-03-31 00:28:56 +0000415
Dan Gohman7f321562007-06-25 16:23:39 +0000416 MVT::ValueType DestVT = getTypeToTransformTo(NewVT);
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000417 RegisterVT = DestVT;
Dan Gohman7f321562007-06-25 16:23:39 +0000418 if (DestVT < NewVT) {
Chris Lattnerdc879292006-03-31 00:28:56 +0000419 // Value is expanded, e.g. i64 -> i16.
Dan Gohman7f321562007-06-25 16:23:39 +0000420 return NumVectorRegs*(MVT::getSizeInBits(NewVT)/MVT::getSizeInBits(DestVT));
Chris Lattnerdc879292006-03-31 00:28:56 +0000421 } else {
422 // Otherwise, promotion or legal types use the same number of registers as
423 // the vector decimated to the appropriate level.
Chris Lattner79227e22006-03-31 00:46:36 +0000424 return NumVectorRegs;
Chris Lattnerdc879292006-03-31 00:28:56 +0000425 }
426
Evan Chenge9b3da12006-05-17 18:10:06 +0000427 return 1;
Chris Lattnerdc879292006-03-31 00:28:56 +0000428}
429
Evan Cheng3ae05432008-01-24 00:22:01 +0000430/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
Dale Johannesen28d08fd2008-02-28 22:31:51 +0000431/// function arguments in the caller parameter area. This is the actual
432/// alignment, not its logarithm.
Evan Cheng3ae05432008-01-24 00:22:01 +0000433unsigned TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Dale Johannesen28d08fd2008-02-28 22:31:51 +0000434 return TD->getCallFrameTypeAlignment(Ty);
Evan Cheng3ae05432008-01-24 00:22:01 +0000435}
436
Evan Chengcc415862007-11-09 01:32:10 +0000437SDOperand TargetLowering::getPICJumpTableRelocBase(SDOperand Table,
438 SelectionDAG &DAG) const {
439 if (usesGlobalOffsetTable())
440 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
441 return Table;
442}
443
Chris Lattnereb8146b2006-02-04 02:13:02 +0000444//===----------------------------------------------------------------------===//
445// Optimization Methods
446//===----------------------------------------------------------------------===//
447
Nate Begeman368e18d2006-02-16 21:11:51 +0000448/// ShrinkDemandedConstant - Check to see if the specified operand of the
449/// specified instruction is a constant integer. If so, check to see if there
450/// are any bits set in the constant that are not demanded. If so, shrink the
451/// constant and return true.
452bool TargetLowering::TargetLoweringOpt::ShrinkDemandedConstant(SDOperand Op,
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000453 const APInt &Demanded) {
Chris Lattnerec665152006-02-26 23:36:02 +0000454 // FIXME: ISD::SELECT, ISD::SELECT_CC
Nate Begeman368e18d2006-02-16 21:11:51 +0000455 switch(Op.getOpcode()) {
456 default: break;
Nate Begemande996292006-02-03 22:24:05 +0000457 case ISD::AND:
Nate Begeman368e18d2006-02-16 21:11:51 +0000458 case ISD::OR:
459 case ISD::XOR:
460 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000461 if (C->getAPIntValue().intersects(~Demanded)) {
Nate Begeman368e18d2006-02-16 21:11:51 +0000462 MVT::ValueType VT = Op.getValueType();
463 SDOperand New = DAG.getNode(Op.getOpcode(), VT, Op.getOperand(0),
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000464 DAG.getConstant(Demanded &
465 C->getAPIntValue(),
Nate Begeman368e18d2006-02-16 21:11:51 +0000466 VT));
467 return CombineTo(Op, New);
Nate Begemande996292006-02-03 22:24:05 +0000468 }
Nate Begemande996292006-02-03 22:24:05 +0000469 break;
470 }
471 return false;
472}
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000473
Nate Begeman368e18d2006-02-16 21:11:51 +0000474/// SimplifyDemandedBits - Look at Op. At this point, we know that only the
475/// DemandedMask bits of the result of Op are ever used downstream. If we can
476/// use this information to simplify Op, create a new simplified DAG node and
477/// return true, returning the original and new nodes in Old and New. Otherwise,
478/// analyze the expression and return a mask of KnownOne and KnownZero bits for
479/// the expression (used to simplify the caller). The KnownZero/One bits may
480/// only be accurate for those bits in the DemandedMask.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000481bool TargetLowering::SimplifyDemandedBits(SDOperand Op,
482 const APInt &DemandedMask,
483 APInt &KnownZero,
484 APInt &KnownOne,
Nate Begeman368e18d2006-02-16 21:11:51 +0000485 TargetLoweringOpt &TLO,
486 unsigned Depth) const {
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000487 unsigned BitWidth = DemandedMask.getBitWidth();
488 assert(Op.getValueSizeInBits() == BitWidth &&
489 "Mask size mismatches value type size!");
490 APInt NewMask = DemandedMask;
Chris Lattner3fc5b012007-05-17 18:19:23 +0000491
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000492 // Don't know anything.
493 KnownZero = KnownOne = APInt(BitWidth, 0);
494
Nate Begeman368e18d2006-02-16 21:11:51 +0000495 // Other users may use these bits.
496 if (!Op.Val->hasOneUse()) {
497 if (Depth != 0) {
498 // If not at the root, Just compute the KnownZero/KnownOne bits to
499 // simplify things downstream.
Dan Gohmanea859be2007-06-22 14:59:07 +0000500 TLO.DAG.ComputeMaskedBits(Op, DemandedMask, KnownZero, KnownOne, Depth);
Nate Begeman368e18d2006-02-16 21:11:51 +0000501 return false;
502 }
503 // If this is the root being simplified, allow it to have multiple uses,
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000504 // just set the NewMask to all bits.
505 NewMask = APInt::getAllOnesValue(BitWidth);
Nate Begeman368e18d2006-02-16 21:11:51 +0000506 } else if (DemandedMask == 0) {
507 // Not demanding any bits from Op.
508 if (Op.getOpcode() != ISD::UNDEF)
509 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::UNDEF, Op.getValueType()));
510 return false;
511 } else if (Depth == 6) { // Limit search depth.
512 return false;
513 }
514
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000515 APInt KnownZero2, KnownOne2, KnownZeroOut, KnownOneOut;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000516 switch (Op.getOpcode()) {
517 case ISD::Constant:
Nate Begeman368e18d2006-02-16 21:11:51 +0000518 // We know all of the bits for a constant!
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000519 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & NewMask;
520 KnownZero = ~KnownOne & NewMask;
Chris Lattnerec665152006-02-26 23:36:02 +0000521 return false; // Don't fall through, will infinitely loop.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000522 case ISD::AND:
Chris Lattner81cd3552006-02-27 00:36:27 +0000523 // If the RHS is a constant, check to see if the LHS would be zero without
524 // using the bits from the RHS. Below, we use knowledge about the RHS to
525 // simplify the LHS, here we're using information from the LHS to simplify
526 // the RHS.
527 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000528 APInt LHSZero, LHSOne;
529 TLO.DAG.ComputeMaskedBits(Op.getOperand(0), NewMask,
Dan Gohmanea859be2007-06-22 14:59:07 +0000530 LHSZero, LHSOne, Depth+1);
Chris Lattner81cd3552006-02-27 00:36:27 +0000531 // If the LHS already has zeros where RHSC does, this and is dead.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000532 if ((LHSZero & NewMask) == (~RHSC->getAPIntValue() & NewMask))
Chris Lattner81cd3552006-02-27 00:36:27 +0000533 return TLO.CombineTo(Op, Op.getOperand(0));
534 // If any of the set bits in the RHS are known zero on the LHS, shrink
535 // the constant.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000536 if (TLO.ShrinkDemandedConstant(Op, ~LHSZero & NewMask))
Chris Lattner81cd3552006-02-27 00:36:27 +0000537 return true;
538 }
539
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000540 if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero,
Nate Begeman368e18d2006-02-16 21:11:51 +0000541 KnownOne, TLO, Depth+1))
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000542 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000543 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000544 if (SimplifyDemandedBits(Op.getOperand(0), ~KnownZero & NewMask,
Nate Begeman368e18d2006-02-16 21:11:51 +0000545 KnownZero2, KnownOne2, TLO, Depth+1))
546 return true;
547 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
548
549 // If all of the demanded bits are known one on one side, return the other.
550 // These bits cannot contribute to the result of the 'and'.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000551 if ((NewMask & ~KnownZero2 & KnownOne) == (~KnownZero2 & NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +0000552 return TLO.CombineTo(Op, Op.getOperand(0));
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000553 if ((NewMask & ~KnownZero & KnownOne2) == (~KnownZero & NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +0000554 return TLO.CombineTo(Op, Op.getOperand(1));
555 // If all of the demanded bits in the inputs are known zeros, return zero.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000556 if ((NewMask & (KnownZero|KnownZero2)) == NewMask)
Nate Begeman368e18d2006-02-16 21:11:51 +0000557 return TLO.CombineTo(Op, TLO.DAG.getConstant(0, Op.getValueType()));
558 // If the RHS is a constant, see if we can simplify it.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000559 if (TLO.ShrinkDemandedConstant(Op, ~KnownZero2 & NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +0000560 return true;
Chris Lattner5f0c6582006-02-27 00:22:28 +0000561
Nate Begeman368e18d2006-02-16 21:11:51 +0000562 // Output known-1 bits are only known if set in both the LHS & RHS.
563 KnownOne &= KnownOne2;
564 // Output known-0 are known to be clear if zero in either the LHS | RHS.
565 KnownZero |= KnownZero2;
566 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000567 case ISD::OR:
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000568 if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero,
Nate Begeman368e18d2006-02-16 21:11:51 +0000569 KnownOne, TLO, Depth+1))
570 return true;
571 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000572 if (SimplifyDemandedBits(Op.getOperand(0), ~KnownOne & NewMask,
Nate Begeman368e18d2006-02-16 21:11:51 +0000573 KnownZero2, KnownOne2, TLO, Depth+1))
574 return true;
575 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
576
577 // If all of the demanded bits are known zero on one side, return the other.
578 // These bits cannot contribute to the result of the 'or'.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000579 if ((NewMask & ~KnownOne2 & KnownZero) == (~KnownOne2 & NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +0000580 return TLO.CombineTo(Op, Op.getOperand(0));
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000581 if ((NewMask & ~KnownOne & KnownZero2) == (~KnownOne & NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +0000582 return TLO.CombineTo(Op, Op.getOperand(1));
583 // If all of the potentially set bits on one side are known to be set on
584 // the other side, just use the 'other' side.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000585 if ((NewMask & ~KnownZero & KnownOne2) == (~KnownZero & NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +0000586 return TLO.CombineTo(Op, Op.getOperand(0));
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000587 if ((NewMask & ~KnownZero2 & KnownOne) == (~KnownZero2 & NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +0000588 return TLO.CombineTo(Op, Op.getOperand(1));
589 // If the RHS is a constant, see if we can simplify it.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000590 if (TLO.ShrinkDemandedConstant(Op, NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +0000591 return true;
592
593 // Output known-0 bits are only known if clear in both the LHS & RHS.
594 KnownZero &= KnownZero2;
595 // Output known-1 are known to be set if set in either the LHS | RHS.
596 KnownOne |= KnownOne2;
597 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000598 case ISD::XOR:
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000599 if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero,
Nate Begeman368e18d2006-02-16 21:11:51 +0000600 KnownOne, TLO, Depth+1))
601 return true;
602 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000603 if (SimplifyDemandedBits(Op.getOperand(0), NewMask, KnownZero2,
Nate Begeman368e18d2006-02-16 21:11:51 +0000604 KnownOne2, TLO, Depth+1))
605 return true;
606 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
607
608 // If all of the demanded bits are known zero on one side, return the other.
609 // These bits cannot contribute to the result of the 'xor'.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000610 if ((KnownZero & NewMask) == NewMask)
Nate Begeman368e18d2006-02-16 21:11:51 +0000611 return TLO.CombineTo(Op, Op.getOperand(0));
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000612 if ((KnownZero2 & NewMask) == NewMask)
Nate Begeman368e18d2006-02-16 21:11:51 +0000613 return TLO.CombineTo(Op, Op.getOperand(1));
Chris Lattner3687c1a2006-11-27 21:50:02 +0000614
615 // If all of the unknown bits are known to be zero on one side or the other
616 // (but not both) turn this into an *inclusive* or.
617 // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000618 if ((NewMask & ~KnownZero & ~KnownZero2) == 0)
Chris Lattner3687c1a2006-11-27 21:50:02 +0000619 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::OR, Op.getValueType(),
620 Op.getOperand(0),
621 Op.getOperand(1)));
Nate Begeman368e18d2006-02-16 21:11:51 +0000622
623 // Output known-0 bits are known if clear or set in both the LHS & RHS.
624 KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
625 // Output known-1 are known to be set if set in only one of the LHS, RHS.
626 KnownOneOut = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
627
Nate Begeman368e18d2006-02-16 21:11:51 +0000628 // If all of the demanded bits on one side are known, and all of the set
629 // bits on that side are also known to be set on the other side, turn this
630 // into an AND, as we know the bits will be cleared.
631 // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000632 if ((NewMask & (KnownZero|KnownOne)) == NewMask) { // all known
Nate Begeman368e18d2006-02-16 21:11:51 +0000633 if ((KnownOne & KnownOne2) == KnownOne) {
634 MVT::ValueType VT = Op.getValueType();
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000635 SDOperand ANDC = TLO.DAG.getConstant(~KnownOne & NewMask, VT);
Nate Begeman368e18d2006-02-16 21:11:51 +0000636 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::AND, VT, Op.getOperand(0),
637 ANDC));
638 }
639 }
640
641 // If the RHS is a constant, see if we can simplify it.
642 // FIXME: for XOR, we prefer to force bits to 1 if they will make a -1.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000643 if (TLO.ShrinkDemandedConstant(Op, NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +0000644 return true;
645
646 KnownZero = KnownZeroOut;
647 KnownOne = KnownOneOut;
648 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000649 case ISD::SELECT:
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000650 if (SimplifyDemandedBits(Op.getOperand(2), NewMask, KnownZero,
Nate Begeman368e18d2006-02-16 21:11:51 +0000651 KnownOne, TLO, Depth+1))
652 return true;
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000653 if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero2,
Nate Begeman368e18d2006-02-16 21:11:51 +0000654 KnownOne2, TLO, Depth+1))
655 return true;
656 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
657 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
658
659 // If the operands are constants, see if we can simplify them.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000660 if (TLO.ShrinkDemandedConstant(Op, NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +0000661 return true;
662
663 // Only known if known in both the LHS and RHS.
664 KnownOne &= KnownOne2;
665 KnownZero &= KnownZero2;
666 break;
Chris Lattnerec665152006-02-26 23:36:02 +0000667 case ISD::SELECT_CC:
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000668 if (SimplifyDemandedBits(Op.getOperand(3), NewMask, KnownZero,
Chris Lattnerec665152006-02-26 23:36:02 +0000669 KnownOne, TLO, Depth+1))
670 return true;
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000671 if (SimplifyDemandedBits(Op.getOperand(2), NewMask, KnownZero2,
Chris Lattnerec665152006-02-26 23:36:02 +0000672 KnownOne2, TLO, Depth+1))
673 return true;
674 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
675 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
676
677 // If the operands are constants, see if we can simplify them.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000678 if (TLO.ShrinkDemandedConstant(Op, NewMask))
Chris Lattnerec665152006-02-26 23:36:02 +0000679 return true;
680
681 // Only known if known in both the LHS and RHS.
682 KnownOne &= KnownOne2;
683 KnownZero &= KnownZero2;
684 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000685 case ISD::SHL:
Nate Begeman368e18d2006-02-16 21:11:51 +0000686 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner895c4ab2007-04-17 21:14:16 +0000687 unsigned ShAmt = SA->getValue();
688 SDOperand InOp = Op.getOperand(0);
689
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000690 // If the shift count is an invalid immediate, don't do anything.
691 if (ShAmt >= BitWidth)
692 break;
693
Chris Lattner895c4ab2007-04-17 21:14:16 +0000694 // If this is ((X >>u C1) << ShAmt), see if we can simplify this into a
695 // single shift. We can do this if the bottom bits (which are shifted
696 // out) are never demanded.
697 if (InOp.getOpcode() == ISD::SRL &&
698 isa<ConstantSDNode>(InOp.getOperand(1))) {
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000699 if (ShAmt && (NewMask & APInt::getLowBitsSet(BitWidth, ShAmt)) == 0) {
Chris Lattner895c4ab2007-04-17 21:14:16 +0000700 unsigned C1 = cast<ConstantSDNode>(InOp.getOperand(1))->getValue();
701 unsigned Opc = ISD::SHL;
702 int Diff = ShAmt-C1;
703 if (Diff < 0) {
704 Diff = -Diff;
705 Opc = ISD::SRL;
706 }
707
708 SDOperand NewSA =
Chris Lattner4e7e6cd2007-05-30 16:30:06 +0000709 TLO.DAG.getConstant(Diff, Op.getOperand(1).getValueType());
Chris Lattner895c4ab2007-04-17 21:14:16 +0000710 MVT::ValueType VT = Op.getValueType();
Chris Lattner0a16a1f2007-04-18 03:01:40 +0000711 return TLO.CombineTo(Op, TLO.DAG.getNode(Opc, VT,
Chris Lattner895c4ab2007-04-17 21:14:16 +0000712 InOp.getOperand(0), NewSA));
713 }
714 }
715
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000716 if (SimplifyDemandedBits(Op.getOperand(0), NewMask.lshr(ShAmt),
Nate Begeman368e18d2006-02-16 21:11:51 +0000717 KnownZero, KnownOne, TLO, Depth+1))
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000718 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000719 KnownZero <<= SA->getValue();
720 KnownOne <<= SA->getValue();
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000721 // low bits known zero.
722 KnownZero |= APInt::getLowBitsSet(BitWidth, SA->getValue());
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000723 }
724 break;
Nate Begeman368e18d2006-02-16 21:11:51 +0000725 case ISD::SRL:
726 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
727 MVT::ValueType VT = Op.getValueType();
728 unsigned ShAmt = SA->getValue();
Chris Lattner895c4ab2007-04-17 21:14:16 +0000729 unsigned VTSize = MVT::getSizeInBits(VT);
730 SDOperand InOp = Op.getOperand(0);
731
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000732 // If the shift count is an invalid immediate, don't do anything.
733 if (ShAmt >= BitWidth)
734 break;
735
Chris Lattner895c4ab2007-04-17 21:14:16 +0000736 // If this is ((X << C1) >>u ShAmt), see if we can simplify this into a
737 // single shift. We can do this if the top bits (which are shifted out)
738 // are never demanded.
739 if (InOp.getOpcode() == ISD::SHL &&
740 isa<ConstantSDNode>(InOp.getOperand(1))) {
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000741 if (ShAmt && (NewMask & APInt::getHighBitsSet(VTSize, ShAmt)) == 0) {
Chris Lattner895c4ab2007-04-17 21:14:16 +0000742 unsigned C1 = cast<ConstantSDNode>(InOp.getOperand(1))->getValue();
743 unsigned Opc = ISD::SRL;
744 int Diff = ShAmt-C1;
745 if (Diff < 0) {
746 Diff = -Diff;
747 Opc = ISD::SHL;
748 }
749
750 SDOperand NewSA =
Chris Lattner8c7d2d52007-04-17 22:53:02 +0000751 TLO.DAG.getConstant(Diff, Op.getOperand(1).getValueType());
Chris Lattner895c4ab2007-04-17 21:14:16 +0000752 return TLO.CombineTo(Op, TLO.DAG.getNode(Opc, VT,
753 InOp.getOperand(0), NewSA));
754 }
755 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000756
757 // Compute the new bits that are at the top now.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000758 if (SimplifyDemandedBits(InOp, (NewMask << ShAmt),
Nate Begeman368e18d2006-02-16 21:11:51 +0000759 KnownZero, KnownOne, TLO, Depth+1))
760 return true;
761 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000762 KnownZero = KnownZero.lshr(ShAmt);
763 KnownOne = KnownOne.lshr(ShAmt);
Chris Lattnerc4fa6032006-06-13 16:52:37 +0000764
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000765 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Chris Lattnerc4fa6032006-06-13 16:52:37 +0000766 KnownZero |= HighBits; // High bits known zero.
Nate Begeman368e18d2006-02-16 21:11:51 +0000767 }
768 break;
769 case ISD::SRA:
770 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
771 MVT::ValueType VT = Op.getValueType();
772 unsigned ShAmt = SA->getValue();
773
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000774 // If the shift count is an invalid immediate, don't do anything.
775 if (ShAmt >= BitWidth)
776 break;
777
778 APInt InDemandedMask = (NewMask << ShAmt);
Chris Lattner1b737132006-05-08 17:22:53 +0000779
780 // If any of the demanded bits are produced by the sign extension, we also
781 // demand the input sign bit.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000782 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
783 if (HighBits.intersects(NewMask))
784 InDemandedMask |= APInt::getSignBit(MVT::getSizeInBits(VT));
Chris Lattner1b737132006-05-08 17:22:53 +0000785
786 if (SimplifyDemandedBits(Op.getOperand(0), InDemandedMask,
Nate Begeman368e18d2006-02-16 21:11:51 +0000787 KnownZero, KnownOne, TLO, Depth+1))
788 return true;
789 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000790 KnownZero = KnownZero.lshr(ShAmt);
791 KnownOne = KnownOne.lshr(ShAmt);
Nate Begeman368e18d2006-02-16 21:11:51 +0000792
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000793 // Handle the sign bit, adjusted to where it is now in the mask.
794 APInt SignBit = APInt::getSignBit(BitWidth).lshr(ShAmt);
Nate Begeman368e18d2006-02-16 21:11:51 +0000795
796 // If the input sign bit is known to be zero, or if none of the top bits
797 // are demanded, turn this into an unsigned shift right.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000798 if (KnownZero.intersects(SignBit) || (HighBits & ~NewMask) == HighBits) {
Nate Begeman368e18d2006-02-16 21:11:51 +0000799 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, VT, Op.getOperand(0),
800 Op.getOperand(1)));
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000801 } else if (KnownOne.intersects(SignBit)) { // New bits are known one.
Nate Begeman368e18d2006-02-16 21:11:51 +0000802 KnownOne |= HighBits;
803 }
804 }
805 break;
806 case ISD::SIGN_EXTEND_INREG: {
Nate Begeman368e18d2006-02-16 21:11:51 +0000807 MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
808
Chris Lattnerec665152006-02-26 23:36:02 +0000809 // Sign extension. Compute the demanded bits in the result that are not
Nate Begeman368e18d2006-02-16 21:11:51 +0000810 // present in the input.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000811 APInt NewBits = APInt::getHighBitsSet(BitWidth,
812 BitWidth - MVT::getSizeInBits(EVT)) &
813 NewMask;
Nate Begeman368e18d2006-02-16 21:11:51 +0000814
Chris Lattnerec665152006-02-26 23:36:02 +0000815 // If none of the extended bits are demanded, eliminate the sextinreg.
816 if (NewBits == 0)
817 return TLO.CombineTo(Op, Op.getOperand(0));
818
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000819 APInt InSignBit = APInt::getSignBit(MVT::getSizeInBits(EVT));
820 InSignBit.zext(BitWidth);
821 APInt InputDemandedBits = APInt::getLowBitsSet(BitWidth,
822 MVT::getSizeInBits(EVT)) &
823 NewMask;
Nate Begeman368e18d2006-02-16 21:11:51 +0000824
Chris Lattnerec665152006-02-26 23:36:02 +0000825 // Since the sign extended bits are demanded, we know that the sign
Nate Begeman368e18d2006-02-16 21:11:51 +0000826 // bit is demanded.
Chris Lattnerec665152006-02-26 23:36:02 +0000827 InputDemandedBits |= InSignBit;
Nate Begeman368e18d2006-02-16 21:11:51 +0000828
829 if (SimplifyDemandedBits(Op.getOperand(0), InputDemandedBits,
830 KnownZero, KnownOne, TLO, Depth+1))
831 return true;
832 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
833
834 // If the sign bit of the input is known set or clear, then we know the
835 // top bits of the result.
836
Chris Lattnerec665152006-02-26 23:36:02 +0000837 // If the input sign bit is known zero, convert this into a zero extension.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000838 if (KnownZero.intersects(InSignBit))
Chris Lattnerec665152006-02-26 23:36:02 +0000839 return TLO.CombineTo(Op,
840 TLO.DAG.getZeroExtendInReg(Op.getOperand(0), EVT));
841
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000842 if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Nate Begeman368e18d2006-02-16 21:11:51 +0000843 KnownOne |= NewBits;
844 KnownZero &= ~NewBits;
Chris Lattnerec665152006-02-26 23:36:02 +0000845 } else { // Input sign bit unknown
Nate Begeman368e18d2006-02-16 21:11:51 +0000846 KnownZero &= ~NewBits;
847 KnownOne &= ~NewBits;
848 }
849 break;
850 }
Chris Lattnerec665152006-02-26 23:36:02 +0000851 case ISD::ZERO_EXTEND: {
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000852 unsigned OperandBitWidth = Op.getOperand(0).getValueSizeInBits();
853 APInt InMask = NewMask;
854 InMask.trunc(OperandBitWidth);
Chris Lattnerec665152006-02-26 23:36:02 +0000855
856 // If none of the top bits are demanded, convert this into an any_extend.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000857 APInt NewBits =
858 APInt::getHighBitsSet(BitWidth, BitWidth - OperandBitWidth) & NewMask;
859 if (!NewBits.intersects(NewMask))
Chris Lattnerec665152006-02-26 23:36:02 +0000860 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ANY_EXTEND,
861 Op.getValueType(),
862 Op.getOperand(0)));
863
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000864 if (SimplifyDemandedBits(Op.getOperand(0), InMask,
Chris Lattnerec665152006-02-26 23:36:02 +0000865 KnownZero, KnownOne, TLO, Depth+1))
866 return true;
867 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000868 KnownZero.zext(BitWidth);
869 KnownOne.zext(BitWidth);
Chris Lattnerec665152006-02-26 23:36:02 +0000870 KnownZero |= NewBits;
871 break;
872 }
873 case ISD::SIGN_EXTEND: {
874 MVT::ValueType InVT = Op.getOperand(0).getValueType();
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000875 unsigned InBits = MVT::getSizeInBits(InVT);
876 APInt InMask = APInt::getLowBitsSet(BitWidth, InBits);
877 APInt InSignBit = APInt::getLowBitsSet(BitWidth, InBits);
878 APInt NewBits = ~InMask & NewMask;
Chris Lattnerec665152006-02-26 23:36:02 +0000879
880 // If none of the top bits are demanded, convert this into an any_extend.
881 if (NewBits == 0)
Chris Lattnerfea997a2007-02-01 04:55:59 +0000882 return TLO.CombineTo(Op,TLO.DAG.getNode(ISD::ANY_EXTEND,Op.getValueType(),
Chris Lattnerec665152006-02-26 23:36:02 +0000883 Op.getOperand(0)));
884
885 // Since some of the sign extended bits are demanded, we know that the sign
886 // bit is demanded.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000887 APInt InDemandedBits = InMask & NewMask;
Chris Lattnerec665152006-02-26 23:36:02 +0000888 InDemandedBits |= InSignBit;
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000889 InDemandedBits.trunc(InBits);
Chris Lattnerec665152006-02-26 23:36:02 +0000890
891 if (SimplifyDemandedBits(Op.getOperand(0), InDemandedBits, KnownZero,
892 KnownOne, TLO, Depth+1))
893 return true;
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000894 KnownZero.zext(BitWidth);
895 KnownOne.zext(BitWidth);
Chris Lattnerec665152006-02-26 23:36:02 +0000896
897 // If the sign bit is known zero, convert this to a zero extend.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000898 if (KnownZero.intersects(InSignBit))
Chris Lattnerec665152006-02-26 23:36:02 +0000899 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ZERO_EXTEND,
900 Op.getValueType(),
901 Op.getOperand(0)));
902
903 // If the sign bit is known one, the top bits match.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000904 if (KnownOne.intersects(InSignBit)) {
Chris Lattnerec665152006-02-26 23:36:02 +0000905 KnownOne |= NewBits;
906 KnownZero &= ~NewBits;
907 } else { // Otherwise, top bits aren't known.
908 KnownOne &= ~NewBits;
909 KnownZero &= ~NewBits;
910 }
911 break;
912 }
913 case ISD::ANY_EXTEND: {
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000914 unsigned OperandBitWidth = Op.getOperand(0).getValueSizeInBits();
915 APInt InMask = NewMask;
916 InMask.trunc(OperandBitWidth);
917 if (SimplifyDemandedBits(Op.getOperand(0), InMask,
Chris Lattnerec665152006-02-26 23:36:02 +0000918 KnownZero, KnownOne, TLO, Depth+1))
919 return true;
920 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000921 KnownZero.zext(BitWidth);
922 KnownOne.zext(BitWidth);
Chris Lattnerec665152006-02-26 23:36:02 +0000923 break;
924 }
Chris Lattnerfe8babf2006-05-05 22:32:12 +0000925 case ISD::TRUNCATE: {
Chris Lattnerc93dfda2006-05-06 00:11:52 +0000926 // Simplify the input, using demanded bit information, and compute the known
927 // zero/one bits live out.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000928 APInt TruncMask = NewMask;
929 TruncMask.zext(Op.getOperand(0).getValueSizeInBits());
930 if (SimplifyDemandedBits(Op.getOperand(0), TruncMask,
Chris Lattnerfe8babf2006-05-05 22:32:12 +0000931 KnownZero, KnownOne, TLO, Depth+1))
932 return true;
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000933 KnownZero.trunc(BitWidth);
934 KnownOne.trunc(BitWidth);
Chris Lattnerc93dfda2006-05-06 00:11:52 +0000935
936 // If the input is only used by this truncate, see if we can shrink it based
937 // on the known demanded bits.
938 if (Op.getOperand(0).Val->hasOneUse()) {
939 SDOperand In = Op.getOperand(0);
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000940 unsigned InBitWidth = In.getValueSizeInBits();
Chris Lattnerc93dfda2006-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.
946 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(In.getOperand(1))){
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000947 APInt HighBits = APInt::getHighBitsSet(InBitWidth,
948 InBitWidth - BitWidth);
949 HighBits = HighBits.lshr(ShAmt->getValue());
950 HighBits.trunc(BitWidth);
Chris Lattnerc93dfda2006-05-06 00:11:52 +0000951
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000952 if (ShAmt->getValue() < BitWidth && !(HighBits & NewMask)) {
Chris Lattnerc93dfda2006-05-06 00:11:52 +0000953 // None of the shifted in bits are needed. Add a truncate of the
954 // shift input, then shift it.
955 SDOperand NewTrunc = TLO.DAG.getNode(ISD::TRUNCATE,
956 Op.getValueType(),
957 In.getOperand(0));
958 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL,Op.getValueType(),
959 NewTrunc, In.getOperand(1)));
960 }
961 }
962 break;
963 }
964 }
965
Chris Lattnerfe8babf2006-05-05 22:32:12 +0000966 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Chris Lattnerfe8babf2006-05-05 22:32:12 +0000967 break;
968 }
Chris Lattnerec665152006-02-26 23:36:02 +0000969 case ISD::AssertZext: {
970 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000971 APInt InMask = APInt::getLowBitsSet(BitWidth,
972 MVT::getSizeInBits(VT));
973 if (SimplifyDemandedBits(Op.getOperand(0), InMask & NewMask,
Chris Lattnerec665152006-02-26 23:36:02 +0000974 KnownZero, KnownOne, TLO, Depth+1))
975 return true;
976 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000977 KnownZero |= ~InMask & NewMask;
Chris Lattnerec665152006-02-26 23:36:02 +0000978 break;
979 }
Chris Lattner2ceb2cf2007-12-22 21:35:38 +0000980 case ISD::BIT_CONVERT:
981#if 0
982 // If this is an FP->Int bitcast and if the sign bit is the only thing that
983 // is demanded, turn this into a FGETSIGN.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000984 if (NewMask == MVT::getIntVTSignBit(Op.getValueType()) &&
Chris Lattner2ceb2cf2007-12-22 21:35:38 +0000985 MVT::isFloatingPoint(Op.getOperand(0).getValueType()) &&
986 !MVT::isVector(Op.getOperand(0).getValueType())) {
987 // Only do this xform if FGETSIGN is valid or if before legalize.
988 if (!TLO.AfterLegalize ||
989 isOperationLegal(ISD::FGETSIGN, Op.getValueType())) {
990 // Make a FGETSIGN + SHL to move the sign bit into the appropriate
991 // place. We expect the SHL to be eliminated by other optimizations.
992 SDOperand Sign = TLO.DAG.getNode(ISD::FGETSIGN, Op.getValueType(),
993 Op.getOperand(0));
994 unsigned ShVal = MVT::getSizeInBits(Op.getValueType())-1;
995 SDOperand ShAmt = TLO.DAG.getConstant(ShVal, getShiftAmountTy());
996 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SHL, Op.getValueType(),
997 Sign, ShAmt));
998 }
999 }
1000#endif
1001 break;
Nate Begeman368e18d2006-02-16 21:11:51 +00001002 case ISD::ADD:
Chris Lattnera6bc5a42006-02-27 01:00:42 +00001003 case ISD::SUB:
Chris Lattner1482b5f2006-04-02 06:15:09 +00001004 case ISD::INTRINSIC_WO_CHAIN:
1005 case ISD::INTRINSIC_W_CHAIN:
1006 case ISD::INTRINSIC_VOID:
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001007 case ISD::CTTZ:
1008 case ISD::CTLZ:
1009 case ISD::CTPOP:
1010 case ISD::LOAD:
1011 case ISD::SETCC:
1012 case ISD::FGETSIGN:
Chris Lattner1482b5f2006-04-02 06:15:09 +00001013 // Just use ComputeMaskedBits to compute output bits.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001014 TLO.DAG.ComputeMaskedBits(Op, NewMask, KnownZero, KnownOne, Depth);
Chris Lattnera6bc5a42006-02-27 01:00:42 +00001015 break;
Nate Begeman368e18d2006-02-16 21:11:51 +00001016 }
Chris Lattnerec665152006-02-26 23:36:02 +00001017
1018 // If we know the value of all of the demanded bits, return this as a
1019 // constant.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001020 if ((NewMask & (KnownZero|KnownOne)) == NewMask)
Chris Lattnerec665152006-02-26 23:36:02 +00001021 return TLO.CombineTo(Op, TLO.DAG.getConstant(KnownOne, Op.getValueType()));
1022
Nate Begeman368e18d2006-02-16 21:11:51 +00001023 return false;
1024}
1025
Nate Begeman368e18d2006-02-16 21:11:51 +00001026/// computeMaskedBitsForTargetNode - Determine which of the bits specified
1027/// in Mask are known to be either zero or one and return them in the
1028/// KnownZero/KnownOne bitsets.
1029void TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00001030 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001031 APInt &KnownZero,
1032 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00001033 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00001034 unsigned Depth) const {
Chris Lattner1b5232a2006-04-02 06:19:46 +00001035 assert((Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1036 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1037 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1038 Op.getOpcode() == ISD::INTRINSIC_VOID) &&
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001039 "Should use MaskedValueIsZero if you don't know whether Op"
1040 " is a target node!");
Dan Gohman977a76f2008-02-13 22:28:48 +00001041 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Cheng3a03ebb2005-12-21 23:05:39 +00001042}
Chris Lattner4ccb0702006-01-26 20:37:03 +00001043
Chris Lattner5c3e21d2006-05-06 09:27:13 +00001044/// ComputeNumSignBitsForTargetNode - This method can be implemented by
1045/// targets that want to expose additional information about sign bits to the
1046/// DAG Combiner.
1047unsigned TargetLowering::ComputeNumSignBitsForTargetNode(SDOperand Op,
1048 unsigned Depth) const {
1049 assert((Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1050 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1051 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1052 Op.getOpcode() == ISD::INTRINSIC_VOID) &&
1053 "Should use ComputeNumSignBits if you don't know whether Op"
1054 " is a target node!");
1055 return 1;
1056}
1057
1058
Evan Chengfa1eb272007-02-08 22:13:59 +00001059/// SimplifySetCC - Try to simplify a setcc built with the specified operands
1060/// and cc. If it is unable to simplify it, return a null SDOperand.
1061SDOperand
1062TargetLowering::SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
1063 ISD::CondCode Cond, bool foldBooleans,
1064 DAGCombinerInfo &DCI) const {
1065 SelectionDAG &DAG = DCI.DAG;
1066
1067 // These setcc operations always fold.
1068 switch (Cond) {
1069 default: break;
1070 case ISD::SETFALSE:
1071 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
1072 case ISD::SETTRUE:
1073 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
1074 }
1075
1076 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
Dan Gohman6c6cd1c2008-03-03 22:22:56 +00001077 const APInt &C1 = N1C->getAPIntValue();
Evan Chengfa1eb272007-02-08 22:13:59 +00001078 if (isa<ConstantSDNode>(N0.Val)) {
1079 return DAG.FoldSetCC(VT, N0, N1, Cond);
1080 } else {
1081 // If the LHS is '(srl (ctlz x), 5)', the RHS is 0/1, and this is an
1082 // equality comparison, then we're just comparing whether X itself is
1083 // zero.
1084 if (N0.getOpcode() == ISD::SRL && (C1 == 0 || C1 == 1) &&
1085 N0.getOperand(0).getOpcode() == ISD::CTLZ &&
1086 N0.getOperand(1).getOpcode() == ISD::Constant) {
1087 unsigned ShAmt = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1088 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
1089 ShAmt == Log2_32(MVT::getSizeInBits(N0.getValueType()))) {
1090 if ((C1 == 0) == (Cond == ISD::SETEQ)) {
1091 // (srl (ctlz x), 5) == 0 -> X != 0
1092 // (srl (ctlz x), 5) != 1 -> X != 0
1093 Cond = ISD::SETNE;
1094 } else {
1095 // (srl (ctlz x), 5) != 0 -> X == 0
1096 // (srl (ctlz x), 5) == 1 -> X == 0
1097 Cond = ISD::SETEQ;
1098 }
1099 SDOperand Zero = DAG.getConstant(0, N0.getValueType());
1100 return DAG.getSetCC(VT, N0.getOperand(0).getOperand(0),
1101 Zero, Cond);
1102 }
1103 }
1104
1105 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
1106 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
1107 unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType());
1108
1109 // If the comparison constant has bits in the upper part, the
1110 // zero-extended value could never match.
Dan Gohman6c6cd1c2008-03-03 22:22:56 +00001111 if (C1.intersects(APInt::getHighBitsSet(C1.getBitWidth(),
1112 C1.getBitWidth() - InSize))) {
Evan Chengfa1eb272007-02-08 22:13:59 +00001113 switch (Cond) {
1114 case ISD::SETUGT:
1115 case ISD::SETUGE:
1116 case ISD::SETEQ: return DAG.getConstant(0, VT);
1117 case ISD::SETULT:
1118 case ISD::SETULE:
1119 case ISD::SETNE: return DAG.getConstant(1, VT);
1120 case ISD::SETGT:
1121 case ISD::SETGE:
1122 // True if the sign bit of C1 is set.
Dan Gohman6c6cd1c2008-03-03 22:22:56 +00001123 return DAG.getConstant(C1.isNegative(), VT);
Evan Chengfa1eb272007-02-08 22:13:59 +00001124 case ISD::SETLT:
1125 case ISD::SETLE:
1126 // True if the sign bit of C1 isn't set.
Dan Gohman6c6cd1c2008-03-03 22:22:56 +00001127 return DAG.getConstant(C1.isNonNegative(), VT);
Evan Chengfa1eb272007-02-08 22:13:59 +00001128 default:
1129 break;
1130 }
1131 }
1132
1133 // Otherwise, we can perform the comparison with the low bits.
1134 switch (Cond) {
1135 case ISD::SETEQ:
1136 case ISD::SETNE:
1137 case ISD::SETUGT:
1138 case ISD::SETUGE:
1139 case ISD::SETULT:
1140 case ISD::SETULE:
1141 return DAG.getSetCC(VT, N0.getOperand(0),
Dan Gohman6c6cd1c2008-03-03 22:22:56 +00001142 DAG.getConstant(APInt(C1).trunc(InSize),
1143 N0.getOperand(0).getValueType()),
Evan Chengfa1eb272007-02-08 22:13:59 +00001144 Cond);
1145 default:
1146 break; // todo, be more careful with signed comparisons
1147 }
1148 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1149 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
1150 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
1151 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
1152 MVT::ValueType ExtDstTy = N0.getValueType();
1153 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
1154
1155 // If the extended part has any inconsistent bits, it cannot ever
1156 // compare equal. In other words, they have to be all ones or all
1157 // zeros.
Dan Gohman6c6cd1c2008-03-03 22:22:56 +00001158 APInt ExtBits =
1159 APInt::getHighBitsSet(ExtDstTyBits, ExtDstTyBits - ExtSrcTyBits);
Evan Chengfa1eb272007-02-08 22:13:59 +00001160 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
1161 return DAG.getConstant(Cond == ISD::SETNE, VT);
1162
1163 SDOperand ZextOp;
1164 MVT::ValueType Op0Ty = N0.getOperand(0).getValueType();
1165 if (Op0Ty == ExtSrcTy) {
1166 ZextOp = N0.getOperand(0);
1167 } else {
Dan Gohman3370dd72008-03-03 22:37:52 +00001168 APInt Imm = APInt::getLowBitsSet(ExtDstTyBits, ExtSrcTyBits);
Evan Chengfa1eb272007-02-08 22:13:59 +00001169 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
1170 DAG.getConstant(Imm, Op0Ty));
1171 }
1172 if (!DCI.isCalledByLegalizer())
1173 DCI.AddToWorklist(ZextOp.Val);
1174 // Otherwise, make this a use of a zext.
1175 return DAG.getSetCC(VT, ZextOp,
Dan Gohman6c6cd1c2008-03-03 22:22:56 +00001176 DAG.getConstant(C1 & APInt::getLowBitsSet(
1177 ExtDstTyBits,
1178 ExtSrcTyBits),
Evan Chengfa1eb272007-02-08 22:13:59 +00001179 ExtDstTy),
1180 Cond);
Dan Gohman6c6cd1c2008-03-03 22:22:56 +00001181 } else if ((N1C->isNullValue() || N1C->getAPIntValue() == 1) &&
Evan Chengfa1eb272007-02-08 22:13:59 +00001182 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
1183
1184 // SETCC (SETCC), [0|1], [EQ|NE] -> SETCC
1185 if (N0.getOpcode() == ISD::SETCC) {
1186 bool TrueWhenTrue = (Cond == ISD::SETEQ) ^ (N1C->getValue() != 1);
1187 if (TrueWhenTrue)
1188 return N0;
1189
1190 // Invert the condition.
1191 ISD::CondCode CC = cast<CondCodeSDNode>(N0.getOperand(2))->get();
1192 CC = ISD::getSetCCInverse(CC,
1193 MVT::isInteger(N0.getOperand(0).getValueType()));
1194 return DAG.getSetCC(VT, N0.getOperand(0), N0.getOperand(1), CC);
1195 }
1196
1197 if ((N0.getOpcode() == ISD::XOR ||
1198 (N0.getOpcode() == ISD::AND &&
1199 N0.getOperand(0).getOpcode() == ISD::XOR &&
1200 N0.getOperand(1) == N0.getOperand(0).getOperand(1))) &&
1201 isa<ConstantSDNode>(N0.getOperand(1)) &&
1202 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == 1) {
1203 // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We
1204 // can only do this if the top bits are known zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001205 unsigned BitWidth = N0.getValueSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001206 if (DAG.MaskedValueIsZero(N0,
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001207 APInt::getHighBitsSet(BitWidth,
1208 BitWidth-1))) {
Evan Chengfa1eb272007-02-08 22:13:59 +00001209 // Okay, get the un-inverted input value.
1210 SDOperand Val;
1211 if (N0.getOpcode() == ISD::XOR)
1212 Val = N0.getOperand(0);
1213 else {
1214 assert(N0.getOpcode() == ISD::AND &&
1215 N0.getOperand(0).getOpcode() == ISD::XOR);
1216 // ((X^1)&1)^1 -> X & 1
1217 Val = DAG.getNode(ISD::AND, N0.getValueType(),
1218 N0.getOperand(0).getOperand(0),
1219 N0.getOperand(1));
1220 }
1221 return DAG.getSetCC(VT, Val, N1,
1222 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
1223 }
1224 }
1225 }
1226
Dan Gohman3370dd72008-03-03 22:37:52 +00001227 APInt MinVal, MaxVal;
Evan Chengfa1eb272007-02-08 22:13:59 +00001228 unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0));
1229 if (ISD::isSignedIntSetCC(Cond)) {
Dan Gohman3370dd72008-03-03 22:37:52 +00001230 MinVal = APInt::getSignedMinValue(OperandBitSize);
1231 MaxVal = APInt::getSignedMaxValue(OperandBitSize);
Evan Chengfa1eb272007-02-08 22:13:59 +00001232 } else {
Dan Gohman3370dd72008-03-03 22:37:52 +00001233 MinVal = APInt::getMinValue(OperandBitSize);
1234 MaxVal = APInt::getMaxValue(OperandBitSize);
Evan Chengfa1eb272007-02-08 22:13:59 +00001235 }
1236
1237 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
1238 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
1239 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
Dan Gohman6c6cd1c2008-03-03 22:22:56 +00001240 // X >= C0 --> X > (C0-1)
1241 return DAG.getSetCC(VT, N0, DAG.getConstant(C1-1, N1.getValueType()),
Evan Chengfa1eb272007-02-08 22:13:59 +00001242 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
1243 }
1244
1245 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
1246 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
Dan Gohman6c6cd1c2008-03-03 22:22:56 +00001247 // X <= C0 --> X < (C0+1)
1248 return DAG.getSetCC(VT, N0, DAG.getConstant(C1+1, N1.getValueType()),
Evan Chengfa1eb272007-02-08 22:13:59 +00001249 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
1250 }
1251
1252 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
1253 return DAG.getConstant(0, VT); // X < MIN --> false
1254 if ((Cond == ISD::SETGE || Cond == ISD::SETUGE) && C1 == MinVal)
1255 return DAG.getConstant(1, VT); // X >= MIN --> true
1256 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal)
1257 return DAG.getConstant(0, VT); // X > MAX --> false
1258 if ((Cond == ISD::SETLE || Cond == ISD::SETULE) && C1 == MaxVal)
1259 return DAG.getConstant(1, VT); // X <= MAX --> true
1260
1261 // Canonicalize setgt X, Min --> setne X, Min
1262 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
1263 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
1264 // Canonicalize setlt X, Max --> setne X, Max
1265 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
1266 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
1267
1268 // If we have setult X, 1, turn it into seteq X, 0
1269 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
1270 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
1271 ISD::SETEQ);
1272 // If we have setugt X, Max-1, turn it into seteq X, Max
1273 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
1274 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
1275 ISD::SETEQ);
1276
1277 // If we have "setcc X, C0", check to see if we can shrink the immediate
1278 // by changing cc.
1279
1280 // SETUGT X, SINTMAX -> SETLT X, 0
1281 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
1282 C1 == (~0ULL >> (65-OperandBitSize)))
1283 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
1284 ISD::SETLT);
1285
1286 // FIXME: Implement the rest of these.
1287
1288 // Fold bit comparisons when we can.
1289 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
1290 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
1291 if (ConstantSDNode *AndRHS =
1292 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1293 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
1294 // Perform the xform if the AND RHS is a single bit.
1295 if (isPowerOf2_64(AndRHS->getValue())) {
1296 return DAG.getNode(ISD::SRL, VT, N0,
1297 DAG.getConstant(Log2_64(AndRHS->getValue()),
1298 getShiftAmountTy()));
1299 }
1300 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
1301 // (X & 8) == 8 --> (X & 8) >> 3
1302 // Perform the xform if C1 is a single bit.
Dan Gohman6c6cd1c2008-03-03 22:22:56 +00001303 if (C1.isPowerOf2()) {
Evan Chengfa1eb272007-02-08 22:13:59 +00001304 return DAG.getNode(ISD::SRL, VT, N0,
Dan Gohman6c6cd1c2008-03-03 22:22:56 +00001305 DAG.getConstant(C1.logBase2(), getShiftAmountTy()));
Evan Chengfa1eb272007-02-08 22:13:59 +00001306 }
1307 }
1308 }
1309 }
1310 } else if (isa<ConstantSDNode>(N0.Val)) {
1311 // Ensure that the constant occurs on the RHS.
1312 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
1313 }
1314
1315 if (isa<ConstantFPSDNode>(N0.Val)) {
1316 // Constant fold or commute setcc.
1317 SDOperand O = DAG.FoldSetCC(VT, N0, N1, Cond);
1318 if (O.Val) return O;
Chris Lattner63079f02007-12-29 08:37:08 +00001319 } else if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1.Val)) {
1320 // If the RHS of an FP comparison is a constant, simplify it away in
1321 // some cases.
1322 if (CFP->getValueAPF().isNaN()) {
1323 // If an operand is known to be a nan, we can fold it.
1324 switch (ISD::getUnorderedFlavor(Cond)) {
1325 default: assert(0 && "Unknown flavor!");
1326 case 0: // Known false.
1327 return DAG.getConstant(0, VT);
1328 case 1: // Known true.
1329 return DAG.getConstant(1, VT);
Chris Lattner1c3e1e22007-12-30 21:21:10 +00001330 case 2: // Undefined.
Chris Lattner63079f02007-12-29 08:37:08 +00001331 return DAG.getNode(ISD::UNDEF, VT);
1332 }
1333 }
1334
1335 // Otherwise, we know the RHS is not a NaN. Simplify the node to drop the
1336 // constant if knowing that the operand is non-nan is enough. We prefer to
1337 // have SETO(x,x) instead of SETO(x, 0.0) because this avoids having to
1338 // materialize 0.0.
1339 if (Cond == ISD::SETO || Cond == ISD::SETUO)
1340 return DAG.getSetCC(VT, N0, N0, Cond);
Evan Chengfa1eb272007-02-08 22:13:59 +00001341 }
1342
1343 if (N0 == N1) {
1344 // We can always fold X == X for integer setcc's.
1345 if (MVT::isInteger(N0.getValueType()))
1346 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
1347 unsigned UOF = ISD::getUnorderedFlavor(Cond);
1348 if (UOF == 2) // FP operators that are undefined on NaNs.
1349 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
1350 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
1351 return DAG.getConstant(UOF, VT);
1352 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
1353 // if it is not already.
1354 ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
1355 if (NewCond != Cond)
1356 return DAG.getSetCC(VT, N0, N1, NewCond);
1357 }
1358
1359 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
1360 MVT::isInteger(N0.getValueType())) {
1361 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
1362 N0.getOpcode() == ISD::XOR) {
1363 // Simplify (X+Y) == (X+Z) --> Y == Z
1364 if (N0.getOpcode() == N1.getOpcode()) {
1365 if (N0.getOperand(0) == N1.getOperand(0))
1366 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
1367 if (N0.getOperand(1) == N1.getOperand(1))
1368 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
1369 if (DAG.isCommutativeBinOp(N0.getOpcode())) {
1370 // If X op Y == Y op X, try other combinations.
1371 if (N0.getOperand(0) == N1.getOperand(1))
1372 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
1373 if (N0.getOperand(1) == N1.getOperand(0))
1374 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(1), Cond);
1375 }
1376 }
1377
1378 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
1379 if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1380 // Turn (X+C1) == C2 --> X == C2-C1
1381 if (N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse()) {
1382 return DAG.getSetCC(VT, N0.getOperand(0),
1383 DAG.getConstant(RHSC->getValue()-LHSR->getValue(),
1384 N0.getValueType()), Cond);
1385 }
1386
1387 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
1388 if (N0.getOpcode() == ISD::XOR)
1389 // If we know that all of the inverted bits are zero, don't bother
1390 // performing the inversion.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001391 if (DAG.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getAPIntValue()))
1392 return
1393 DAG.getSetCC(VT, N0.getOperand(0),
1394 DAG.getConstant(LHSR->getAPIntValue() ^
1395 RHSC->getAPIntValue(),
1396 N0.getValueType()),
1397 Cond);
Evan Chengfa1eb272007-02-08 22:13:59 +00001398 }
1399
1400 // Turn (C1-X) == C2 --> X == C1-C2
1401 if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
1402 if (N0.getOpcode() == ISD::SUB && N0.Val->hasOneUse()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001403 return
1404 DAG.getSetCC(VT, N0.getOperand(1),
1405 DAG.getConstant(SUBC->getAPIntValue() -
1406 RHSC->getAPIntValue(),
1407 N0.getValueType()),
1408 Cond);
Evan Chengfa1eb272007-02-08 22:13:59 +00001409 }
1410 }
1411 }
1412
1413 // Simplify (X+Z) == X --> Z == 0
1414 if (N0.getOperand(0) == N1)
1415 return DAG.getSetCC(VT, N0.getOperand(1),
1416 DAG.getConstant(0, N0.getValueType()), Cond);
1417 if (N0.getOperand(1) == N1) {
1418 if (DAG.isCommutativeBinOp(N0.getOpcode()))
1419 return DAG.getSetCC(VT, N0.getOperand(0),
1420 DAG.getConstant(0, N0.getValueType()), Cond);
Chris Lattner2ad913b2007-05-19 00:43:44 +00001421 else if (N0.Val->hasOneUse()) {
Evan Chengfa1eb272007-02-08 22:13:59 +00001422 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
1423 // (Z-X) == X --> Z == X<<1
1424 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
1425 N1,
1426 DAG.getConstant(1, getShiftAmountTy()));
1427 if (!DCI.isCalledByLegalizer())
1428 DCI.AddToWorklist(SH.Val);
1429 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
1430 }
1431 }
1432 }
1433
1434 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
1435 N1.getOpcode() == ISD::XOR) {
1436 // Simplify X == (X+Z) --> Z == 0
1437 if (N1.getOperand(0) == N0) {
1438 return DAG.getSetCC(VT, N1.getOperand(1),
1439 DAG.getConstant(0, N1.getValueType()), Cond);
1440 } else if (N1.getOperand(1) == N0) {
1441 if (DAG.isCommutativeBinOp(N1.getOpcode())) {
1442 return DAG.getSetCC(VT, N1.getOperand(0),
1443 DAG.getConstant(0, N1.getValueType()), Cond);
Chris Lattner7667c0b2007-05-19 00:46:51 +00001444 } else if (N1.Val->hasOneUse()) {
Evan Chengfa1eb272007-02-08 22:13:59 +00001445 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
1446 // X == (Z-X) --> X<<1 == Z
1447 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
1448 DAG.getConstant(1, getShiftAmountTy()));
1449 if (!DCI.isCalledByLegalizer())
1450 DCI.AddToWorklist(SH.Val);
1451 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
1452 }
1453 }
1454 }
1455 }
1456
1457 // Fold away ALL boolean setcc's.
1458 SDOperand Temp;
1459 if (N0.getValueType() == MVT::i1 && foldBooleans) {
1460 switch (Cond) {
1461 default: assert(0 && "Unknown integer setcc!");
1462 case ISD::SETEQ: // X == Y -> (X^Y)^1
1463 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
1464 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
1465 if (!DCI.isCalledByLegalizer())
1466 DCI.AddToWorklist(Temp.Val);
1467 break;
1468 case ISD::SETNE: // X != Y --> (X^Y)
1469 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
1470 break;
1471 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
1472 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
1473 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
1474 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
1475 if (!DCI.isCalledByLegalizer())
1476 DCI.AddToWorklist(Temp.Val);
1477 break;
1478 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
1479 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
1480 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
1481 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
1482 if (!DCI.isCalledByLegalizer())
1483 DCI.AddToWorklist(Temp.Val);
1484 break;
1485 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
1486 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
1487 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
1488 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
1489 if (!DCI.isCalledByLegalizer())
1490 DCI.AddToWorklist(Temp.Val);
1491 break;
1492 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
1493 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
1494 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
1495 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
1496 break;
1497 }
1498 if (VT != MVT::i1) {
1499 if (!DCI.isCalledByLegalizer())
1500 DCI.AddToWorklist(N0.Val);
1501 // FIXME: If running after legalize, we probably can't do this.
1502 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
1503 }
1504 return N0;
1505 }
1506
1507 // Could not fold it.
1508 return SDOperand();
1509}
1510
Chris Lattner00ffed02006-03-01 04:52:55 +00001511SDOperand TargetLowering::
1512PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
1513 // Default implementation: no optimization.
1514 return SDOperand();
1515}
1516
Chris Lattnereb8146b2006-02-04 02:13:02 +00001517//===----------------------------------------------------------------------===//
1518// Inline Assembler Implementation Methods
1519//===----------------------------------------------------------------------===//
1520
1521TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00001522TargetLowering::getConstraintType(const std::string &Constraint) const {
Chris Lattnereb8146b2006-02-04 02:13:02 +00001523 // FIXME: lots more standard ones to handle.
Chris Lattner4234f572007-03-25 02:14:49 +00001524 if (Constraint.size() == 1) {
1525 switch (Constraint[0]) {
1526 default: break;
1527 case 'r': return C_RegisterClass;
1528 case 'm': // memory
1529 case 'o': // offsetable
1530 case 'V': // not offsetable
1531 return C_Memory;
1532 case 'i': // Simple Integer or Relocatable Constant
1533 case 'n': // Simple Integer
1534 case 's': // Relocatable Constant
Chris Lattnerc13dd1c2007-03-25 04:35:41 +00001535 case 'X': // Allow ANY value.
Chris Lattner4234f572007-03-25 02:14:49 +00001536 case 'I': // Target registers.
1537 case 'J':
1538 case 'K':
1539 case 'L':
1540 case 'M':
1541 case 'N':
1542 case 'O':
1543 case 'P':
1544 return C_Other;
1545 }
Chris Lattnereb8146b2006-02-04 02:13:02 +00001546 }
Chris Lattner065421f2007-03-25 02:18:14 +00001547
1548 if (Constraint.size() > 1 && Constraint[0] == '{' &&
1549 Constraint[Constraint.size()-1] == '}')
1550 return C_Register;
Chris Lattner4234f572007-03-25 02:14:49 +00001551 return C_Unknown;
Chris Lattnereb8146b2006-02-04 02:13:02 +00001552}
1553
Dale Johannesenba2a0b92008-01-29 02:21:21 +00001554/// LowerXConstraint - try to replace an X constraint, which matches anything,
1555/// with another that has more specific requirements based on the type of the
1556/// corresponding operand.
1557void TargetLowering::lowerXConstraint(MVT::ValueType ConstraintVT,
1558 std::string& s) const {
1559 if (MVT::isInteger(ConstraintVT))
1560 s = "r";
1561 else if (MVT::isFloatingPoint(ConstraintVT))
1562 s = "f"; // works for many targets
1563 else
1564 s = "";
1565}
1566
Chris Lattner48884cd2007-08-25 00:47:38 +00001567/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
1568/// vector. If it is invalid, don't add anything to Ops.
1569void TargetLowering::LowerAsmOperandForConstraint(SDOperand Op,
1570 char ConstraintLetter,
1571 std::vector<SDOperand> &Ops,
1572 SelectionDAG &DAG) {
Chris Lattnereb8146b2006-02-04 02:13:02 +00001573 switch (ConstraintLetter) {
Chris Lattner9ff6ee82007-02-17 06:00:35 +00001574 default: break;
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001575 case 'X': // Allows any operand; labels (basic block) use this.
1576 if (Op.getOpcode() == ISD::BasicBlock) {
1577 Ops.push_back(Op);
1578 return;
1579 }
1580 // fall through
Chris Lattnereb8146b2006-02-04 02:13:02 +00001581 case 'i': // Simple Integer or Relocatable Constant
1582 case 'n': // Simple Integer
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001583 case 's': { // Relocatable Constant
Chris Lattner75c7d2b2007-05-03 16:54:34 +00001584 // These operands are interested in values of the form (GV+C), where C may
1585 // be folded in as an offset of GV, or it may be explicitly added. Also, it
1586 // is possible and fine if either GV or C are missing.
1587 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
1588 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
1589
1590 // If we have "(add GV, C)", pull out GV/C
1591 if (Op.getOpcode() == ISD::ADD) {
1592 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
1593 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
1594 if (C == 0 || GA == 0) {
1595 C = dyn_cast<ConstantSDNode>(Op.getOperand(0));
1596 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(1));
1597 }
1598 if (C == 0 || GA == 0)
1599 C = 0, GA = 0;
1600 }
1601
1602 // If we find a valid operand, map to the TargetXXX version so that the
1603 // value itself doesn't get selected.
1604 if (GA) { // Either &GV or &GV+C
1605 if (ConstraintLetter != 'n') {
1606 int64_t Offs = GA->getOffset();
1607 if (C) Offs += C->getValue();
Chris Lattner48884cd2007-08-25 00:47:38 +00001608 Ops.push_back(DAG.getTargetGlobalAddress(GA->getGlobal(),
1609 Op.getValueType(), Offs));
1610 return;
Chris Lattner75c7d2b2007-05-03 16:54:34 +00001611 }
1612 }
1613 if (C) { // just C, no GV.
Chris Lattner9ff6ee82007-02-17 06:00:35 +00001614 // Simple constants are not allowed for 's'.
Chris Lattner48884cd2007-08-25 00:47:38 +00001615 if (ConstraintLetter != 's') {
1616 Ops.push_back(DAG.getTargetConstant(C->getValue(), Op.getValueType()));
1617 return;
1618 }
Chris Lattner9ff6ee82007-02-17 06:00:35 +00001619 }
Chris Lattner9ff6ee82007-02-17 06:00:35 +00001620 break;
Chris Lattnereb8146b2006-02-04 02:13:02 +00001621 }
Chris Lattner75c7d2b2007-05-03 16:54:34 +00001622 }
Chris Lattnereb8146b2006-02-04 02:13:02 +00001623}
1624
Chris Lattner4ccb0702006-01-26 20:37:03 +00001625std::vector<unsigned> TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00001626getRegClassForInlineAsmConstraint(const std::string &Constraint,
1627 MVT::ValueType VT) const {
1628 return std::vector<unsigned>();
1629}
1630
1631
1632std::pair<unsigned, const TargetRegisterClass*> TargetLowering::
Chris Lattner4217ca8dc2006-02-21 23:11:00 +00001633getRegForInlineAsmConstraint(const std::string &Constraint,
1634 MVT::ValueType VT) const {
Chris Lattner1efa40f2006-02-22 00:56:39 +00001635 if (Constraint[0] != '{')
1636 return std::pair<unsigned, const TargetRegisterClass*>(0, 0);
Chris Lattnera55079a2006-02-01 01:29:47 +00001637 assert(*(Constraint.end()-1) == '}' && "Not a brace enclosed constraint?");
1638
1639 // Remove the braces from around the name.
1640 std::string RegName(Constraint.begin()+1, Constraint.end()-1);
Chris Lattner1efa40f2006-02-22 00:56:39 +00001641
1642 // Figure out which register class contains this reg.
Dan Gohman6f0d0242008-02-10 18:45:23 +00001643 const TargetRegisterInfo *RI = TM.getRegisterInfo();
1644 for (TargetRegisterInfo::regclass_iterator RCI = RI->regclass_begin(),
Chris Lattner1efa40f2006-02-22 00:56:39 +00001645 E = RI->regclass_end(); RCI != E; ++RCI) {
1646 const TargetRegisterClass *RC = *RCI;
Chris Lattnerb3befd42006-02-22 23:00:51 +00001647
1648 // If none of the the value types for this register class are valid, we
1649 // can't use it. For example, 64-bit reg classes on 32-bit targets.
1650 bool isLegal = false;
1651 for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
1652 I != E; ++I) {
1653 if (isTypeLegal(*I)) {
1654 isLegal = true;
1655 break;
1656 }
1657 }
1658
1659 if (!isLegal) continue;
1660
Chris Lattner1efa40f2006-02-22 00:56:39 +00001661 for (TargetRegisterClass::iterator I = RC->begin(), E = RC->end();
1662 I != E; ++I) {
Bill Wendling74ab84c2008-02-26 21:11:01 +00001663 if (StringsEqualNoCase(RegName, RI->get(*I).AsmName))
Chris Lattner1efa40f2006-02-22 00:56:39 +00001664 return std::make_pair(*I, RC);
Chris Lattner1efa40f2006-02-22 00:56:39 +00001665 }
Chris Lattner4ccb0702006-01-26 20:37:03 +00001666 }
Chris Lattnera55079a2006-02-01 01:29:47 +00001667
Chris Lattner1efa40f2006-02-22 00:56:39 +00001668 return std::pair<unsigned, const TargetRegisterClass*>(0, 0);
Chris Lattner4ccb0702006-01-26 20:37:03 +00001669}
Evan Cheng30b37b52006-03-13 23:18:16 +00001670
1671//===----------------------------------------------------------------------===//
1672// Loop Strength Reduction hooks
1673//===----------------------------------------------------------------------===//
1674
Chris Lattner1436bb62007-03-30 23:14:50 +00001675/// isLegalAddressingMode - Return true if the addressing mode represented
1676/// by AM is legal for this target, for a load/store of the specified type.
1677bool TargetLowering::isLegalAddressingMode(const AddrMode &AM,
1678 const Type *Ty) const {
1679 // The default implementation of this implements a conservative RISCy, r+r and
1680 // r+i addr mode.
1681
1682 // Allows a sign-extended 16-bit immediate field.
1683 if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1)
1684 return false;
1685
1686 // No global is ever allowed as a base.
1687 if (AM.BaseGV)
1688 return false;
1689
1690 // Only support r+r,
1691 switch (AM.Scale) {
1692 case 0: // "r+i" or just "i", depending on HasBaseReg.
1693 break;
1694 case 1:
1695 if (AM.HasBaseReg && AM.BaseOffs) // "r+r+i" is not allowed.
1696 return false;
1697 // Otherwise we have r+r or r+i.
1698 break;
1699 case 2:
1700 if (AM.HasBaseReg || AM.BaseOffs) // 2*r+r or 2*r+i is not allowed.
1701 return false;
1702 // Allow 2*r as r+r.
1703 break;
1704 }
1705
1706 return true;
1707}
1708
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00001709// Magic for divide replacement
1710
1711struct ms {
1712 int64_t m; // magic number
1713 int64_t s; // shift amount
1714};
1715
1716struct mu {
1717 uint64_t m; // magic number
1718 int64_t a; // add indicator
1719 int64_t s; // shift amount
1720};
1721
1722/// magic - calculate the magic numbers required to codegen an integer sdiv as
1723/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
1724/// or -1.
1725static ms magic32(int32_t d) {
1726 int32_t p;
1727 uint32_t ad, anc, delta, q1, r1, q2, r2, t;
1728 const uint32_t two31 = 0x80000000U;
1729 struct ms mag;
1730
1731 ad = abs(d);
1732 t = two31 + ((uint32_t)d >> 31);
1733 anc = t - 1 - t%ad; // absolute value of nc
1734 p = 31; // initialize p
1735 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
1736 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
1737 q2 = two31/ad; // initialize q2 = 2p/abs(d)
1738 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
1739 do {
1740 p = p + 1;
1741 q1 = 2*q1; // update q1 = 2p/abs(nc)
1742 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
1743 if (r1 >= anc) { // must be unsigned comparison
1744 q1 = q1 + 1;
1745 r1 = r1 - anc;
1746 }
1747 q2 = 2*q2; // update q2 = 2p/abs(d)
1748 r2 = 2*r2; // update r2 = rem(2p/abs(d))
1749 if (r2 >= ad) { // must be unsigned comparison
1750 q2 = q2 + 1;
1751 r2 = r2 - ad;
1752 }
1753 delta = ad - r2;
1754 } while (q1 < delta || (q1 == delta && r1 == 0));
1755
1756 mag.m = (int32_t)(q2 + 1); // make sure to sign extend
1757 if (d < 0) mag.m = -mag.m; // resulting magic number
1758 mag.s = p - 32; // resulting shift
1759 return mag;
1760}
1761
1762/// magicu - calculate the magic numbers required to codegen an integer udiv as
1763/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
1764static mu magicu32(uint32_t d) {
1765 int32_t p;
1766 uint32_t nc, delta, q1, r1, q2, r2;
1767 struct mu magu;
1768 magu.a = 0; // initialize "add" indicator
1769 nc = - 1 - (-d)%d;
1770 p = 31; // initialize p
1771 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
1772 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
1773 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
1774 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
1775 do {
1776 p = p + 1;
1777 if (r1 >= nc - r1 ) {
1778 q1 = 2*q1 + 1; // update q1
1779 r1 = 2*r1 - nc; // update r1
1780 }
1781 else {
1782 q1 = 2*q1; // update q1
1783 r1 = 2*r1; // update r1
1784 }
1785 if (r2 + 1 >= d - r2) {
1786 if (q2 >= 0x7FFFFFFF) magu.a = 1;
1787 q2 = 2*q2 + 1; // update q2
1788 r2 = 2*r2 + 1 - d; // update r2
1789 }
1790 else {
1791 if (q2 >= 0x80000000) magu.a = 1;
1792 q2 = 2*q2; // update q2
1793 r2 = 2*r2 + 1; // update r2
1794 }
1795 delta = d - 1 - r2;
1796 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
1797 magu.m = q2 + 1; // resulting magic number
1798 magu.s = p - 32; // resulting shift
1799 return magu;
1800}
1801
1802/// magic - calculate the magic numbers required to codegen an integer sdiv as
1803/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
1804/// or -1.
1805static ms magic64(int64_t d) {
1806 int64_t p;
1807 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
1808 const uint64_t two63 = 9223372036854775808ULL; // 2^63
1809 struct ms mag;
1810
1811 ad = d >= 0 ? d : -d;
1812 t = two63 + ((uint64_t)d >> 63);
1813 anc = t - 1 - t%ad; // absolute value of nc
1814 p = 63; // initialize p
1815 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
1816 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
1817 q2 = two63/ad; // initialize q2 = 2p/abs(d)
1818 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
1819 do {
1820 p = p + 1;
1821 q1 = 2*q1; // update q1 = 2p/abs(nc)
1822 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
1823 if (r1 >= anc) { // must be unsigned comparison
1824 q1 = q1 + 1;
1825 r1 = r1 - anc;
1826 }
1827 q2 = 2*q2; // update q2 = 2p/abs(d)
1828 r2 = 2*r2; // update r2 = rem(2p/abs(d))
1829 if (r2 >= ad) { // must be unsigned comparison
1830 q2 = q2 + 1;
1831 r2 = r2 - ad;
1832 }
1833 delta = ad - r2;
1834 } while (q1 < delta || (q1 == delta && r1 == 0));
1835
1836 mag.m = q2 + 1;
1837 if (d < 0) mag.m = -mag.m; // resulting magic number
1838 mag.s = p - 64; // resulting shift
1839 return mag;
1840}
1841
1842/// magicu - calculate the magic numbers required to codegen an integer udiv as
1843/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
1844static mu magicu64(uint64_t d)
1845{
1846 int64_t p;
1847 uint64_t nc, delta, q1, r1, q2, r2;
1848 struct mu magu;
1849 magu.a = 0; // initialize "add" indicator
1850 nc = - 1 - (-d)%d;
1851 p = 63; // initialize p
1852 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
1853 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
1854 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
1855 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
1856 do {
1857 p = p + 1;
1858 if (r1 >= nc - r1 ) {
1859 q1 = 2*q1 + 1; // update q1
1860 r1 = 2*r1 - nc; // update r1
1861 }
1862 else {
1863 q1 = 2*q1; // update q1
1864 r1 = 2*r1; // update r1
1865 }
1866 if (r2 + 1 >= d - r2) {
1867 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
1868 q2 = 2*q2 + 1; // update q2
1869 r2 = 2*r2 + 1 - d; // update r2
1870 }
1871 else {
1872 if (q2 >= 0x8000000000000000ull) magu.a = 1;
1873 q2 = 2*q2; // update q2
1874 r2 = 2*r2 + 1; // update r2
1875 }
1876 delta = d - 1 - r2;
Andrew Lenharth3e348492006-05-16 17:45:23 +00001877 } while (p < 128 && (q1 < delta || (q1 == delta && r1 == 0)));
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00001878 magu.m = q2 + 1; // resulting magic number
1879 magu.s = p - 64; // resulting shift
1880 return magu;
1881}
1882
1883/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
1884/// return a DAG expression to select that will generate the same value by
1885/// multiplying by a magic number. See:
1886/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
1887SDOperand TargetLowering::BuildSDIV(SDNode *N, SelectionDAG &DAG,
Anton Korobeynikovbed29462007-04-16 18:10:23 +00001888 std::vector<SDNode*>* Created) const {
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00001889 MVT::ValueType VT = N->getValueType(0);
1890
1891 // Check to see if we can do this.
1892 if (!isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64))
1893 return SDOperand(); // BuildSDIV only operates on i32 or i64
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00001894
1895 int64_t d = cast<ConstantSDNode>(N->getOperand(1))->getSignExtended();
1896 ms magics = (VT == MVT::i32) ? magic32(d) : magic64(d);
1897
1898 // Multiply the numerator (operand 0) by the magic value
Dan Gohman525178c2007-10-08 18:33:35 +00001899 SDOperand Q;
1900 if (isOperationLegal(ISD::MULHS, VT))
1901 Q = DAG.getNode(ISD::MULHS, VT, N->getOperand(0),
1902 DAG.getConstant(magics.m, VT));
1903 else if (isOperationLegal(ISD::SMUL_LOHI, VT))
1904 Q = SDOperand(DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(VT, VT),
1905 N->getOperand(0),
1906 DAG.getConstant(magics.m, VT)).Val, 1);
1907 else
1908 return SDOperand(); // No mulhs or equvialent
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00001909 // If d > 0 and m < 0, add the numerator
1910 if (d > 0 && magics.m < 0) {
1911 Q = DAG.getNode(ISD::ADD, VT, Q, N->getOperand(0));
1912 if (Created)
1913 Created->push_back(Q.Val);
1914 }
1915 // If d < 0 and m > 0, subtract the numerator.
1916 if (d < 0 && magics.m > 0) {
1917 Q = DAG.getNode(ISD::SUB, VT, Q, N->getOperand(0));
1918 if (Created)
1919 Created->push_back(Q.Val);
1920 }
1921 // Shift right algebraic if shift value is nonzero
1922 if (magics.s > 0) {
1923 Q = DAG.getNode(ISD::SRA, VT, Q,
1924 DAG.getConstant(magics.s, getShiftAmountTy()));
1925 if (Created)
1926 Created->push_back(Q.Val);
1927 }
1928 // Extract the sign bit and add it to the quotient
1929 SDOperand T =
1930 DAG.getNode(ISD::SRL, VT, Q, DAG.getConstant(MVT::getSizeInBits(VT)-1,
1931 getShiftAmountTy()));
1932 if (Created)
1933 Created->push_back(T.Val);
1934 return DAG.getNode(ISD::ADD, VT, Q, T);
1935}
1936
1937/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
1938/// return a DAG expression to select that will generate the same value by
1939/// multiplying by a magic number. See:
1940/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
1941SDOperand TargetLowering::BuildUDIV(SDNode *N, SelectionDAG &DAG,
Anton Korobeynikovbed29462007-04-16 18:10:23 +00001942 std::vector<SDNode*>* Created) const {
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00001943 MVT::ValueType VT = N->getValueType(0);
1944
1945 // Check to see if we can do this.
1946 if (!isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64))
1947 return SDOperand(); // BuildUDIV only operates on i32 or i64
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00001948
1949 uint64_t d = cast<ConstantSDNode>(N->getOperand(1))->getValue();
1950 mu magics = (VT == MVT::i32) ? magicu32(d) : magicu64(d);
1951
1952 // Multiply the numerator (operand 0) by the magic value
Dan Gohman525178c2007-10-08 18:33:35 +00001953 SDOperand Q;
1954 if (isOperationLegal(ISD::MULHU, VT))
1955 Q = DAG.getNode(ISD::MULHU, VT, N->getOperand(0),
1956 DAG.getConstant(magics.m, VT));
1957 else if (isOperationLegal(ISD::UMUL_LOHI, VT))
1958 Q = SDOperand(DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(VT, VT),
1959 N->getOperand(0),
1960 DAG.getConstant(magics.m, VT)).Val, 1);
1961 else
1962 return SDOperand(); // No mulhu or equvialent
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00001963 if (Created)
1964 Created->push_back(Q.Val);
1965
1966 if (magics.a == 0) {
1967 return DAG.getNode(ISD::SRL, VT, Q,
1968 DAG.getConstant(magics.s, getShiftAmountTy()));
1969 } else {
1970 SDOperand NPQ = DAG.getNode(ISD::SUB, VT, N->getOperand(0), Q);
1971 if (Created)
1972 Created->push_back(NPQ.Val);
1973 NPQ = DAG.getNode(ISD::SRL, VT, NPQ,
1974 DAG.getConstant(1, getShiftAmountTy()));
1975 if (Created)
1976 Created->push_back(NPQ.Val);
1977 NPQ = DAG.getNode(ISD::ADD, VT, NPQ, Q);
1978 if (Created)
1979 Created->push_back(NPQ.Val);
1980 return DAG.getNode(ISD::SRL, VT, NPQ,
1981 DAG.getConstant(magics.s-1, getShiftAmountTy()));
1982 }
1983}