blob: e58c2f76ff1fd45eeaec1f2fb3dd928cdd0de782 [file] [log] [blame]
Chris Lattner310968c2005-01-07 07:44:53 +00001//===-- TargetLowering.cpp - Implement the TargetLowering class -----------===//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
Chris Lattner310968c2005-01-07 07:44:53 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanf976c852005-04-21 22:55:34 +00007//
Chris Lattner310968c2005-01-07 07:44:53 +00008//===----------------------------------------------------------------------===//
9//
10// This implements the TargetLowering class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Target/TargetLowering.h"
Rafael Espindolaf1ba1ca2007-11-05 23:12:20 +000015#include "llvm/Target/TargetSubtarget.h"
Owen Anderson07000c62006-05-12 06:33:49 +000016#include "llvm/Target/TargetData.h"
Chris Lattner310968c2005-01-07 07:44:53 +000017#include "llvm/Target/TargetMachine.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000018#include "llvm/Target/TargetRegisterInfo.h"
Chris Lattnerdc879292006-03-31 00:28:56 +000019#include "llvm/DerivedTypes.h"
Chris Lattner310968c2005-01-07 07:44:53 +000020#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner4ccb0702006-01-26 20:37:03 +000021#include "llvm/ADT/StringExtras.h"
Owen Anderson718cb662007-09-07 04:06:50 +000022#include "llvm/ADT/STLExtras.h"
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +000023#include "llvm/Support/MathExtras.h"
Dan Gohmanc3b0b5c2007-09-25 15:10:49 +000024#include "llvm/Target/TargetAsmInfo.h"
Rafael Espindolaf1ba1ca2007-11-05 23:12:20 +000025#include "llvm/CallingConv.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";
Evan Cheng56966222007-01-12 02:11:51 +0000110 Names[RTLIB::UINTTOFP_I32_F32] = "__floatunsisf";
111 Names[RTLIB::UINTTOFP_I32_F64] = "__floatunsidf";
112 Names[RTLIB::UINTTOFP_I64_F32] = "__floatundisf";
113 Names[RTLIB::UINTTOFP_I64_F64] = "__floatundidf";
114 Names[RTLIB::OEQ_F32] = "__eqsf2";
115 Names[RTLIB::OEQ_F64] = "__eqdf2";
116 Names[RTLIB::UNE_F32] = "__nesf2";
117 Names[RTLIB::UNE_F64] = "__nedf2";
118 Names[RTLIB::OGE_F32] = "__gesf2";
119 Names[RTLIB::OGE_F64] = "__gedf2";
120 Names[RTLIB::OLT_F32] = "__ltsf2";
121 Names[RTLIB::OLT_F64] = "__ltdf2";
122 Names[RTLIB::OLE_F32] = "__lesf2";
123 Names[RTLIB::OLE_F64] = "__ledf2";
124 Names[RTLIB::OGT_F32] = "__gtsf2";
125 Names[RTLIB::OGT_F64] = "__gtdf2";
126 Names[RTLIB::UO_F32] = "__unordsf2";
127 Names[RTLIB::UO_F64] = "__unorddf2";
Evan Chengd385fd62007-01-31 09:29:11 +0000128 Names[RTLIB::O_F32] = "__unordsf2";
129 Names[RTLIB::O_F64] = "__unorddf2";
130}
131
132/// InitCmpLibcallCCs - Set default comparison libcall CC.
133///
134static void InitCmpLibcallCCs(ISD::CondCode *CCs) {
135 memset(CCs, ISD::SETCC_INVALID, sizeof(ISD::CondCode)*RTLIB::UNKNOWN_LIBCALL);
136 CCs[RTLIB::OEQ_F32] = ISD::SETEQ;
137 CCs[RTLIB::OEQ_F64] = ISD::SETEQ;
138 CCs[RTLIB::UNE_F32] = ISD::SETNE;
139 CCs[RTLIB::UNE_F64] = ISD::SETNE;
140 CCs[RTLIB::OGE_F32] = ISD::SETGE;
141 CCs[RTLIB::OGE_F64] = ISD::SETGE;
142 CCs[RTLIB::OLT_F32] = ISD::SETLT;
143 CCs[RTLIB::OLT_F64] = ISD::SETLT;
144 CCs[RTLIB::OLE_F32] = ISD::SETLE;
145 CCs[RTLIB::OLE_F64] = ISD::SETLE;
146 CCs[RTLIB::OGT_F32] = ISD::SETGT;
147 CCs[RTLIB::OGT_F64] = ISD::SETGT;
148 CCs[RTLIB::UO_F32] = ISD::SETNE;
149 CCs[RTLIB::UO_F64] = ISD::SETNE;
150 CCs[RTLIB::O_F32] = ISD::SETEQ;
151 CCs[RTLIB::O_F64] = ISD::SETEQ;
Evan Cheng56966222007-01-12 02:11:51 +0000152}
153
Chris Lattner310968c2005-01-07 07:44:53 +0000154TargetLowering::TargetLowering(TargetMachine &tm)
Chris Lattner3e6e8cc2006-01-29 08:41:12 +0000155 : TM(tm), TD(TM.getTargetData()) {
Evan Cheng33143dc2006-03-03 06:58:59 +0000156 assert(ISD::BUILTIN_OP_END <= 156 &&
Chris Lattner310968c2005-01-07 07:44:53 +0000157 "Fixed size array in TargetLowering is not large enough!");
Chris Lattnercba82f92005-01-16 07:28:11 +0000158 // All operations default to being supported.
159 memset(OpActions, 0, sizeof(OpActions));
Evan Chengc5484282006-10-04 00:56:09 +0000160 memset(LoadXActions, 0, sizeof(LoadXActions));
Chris Lattnerddf89562008-01-17 19:59:44 +0000161 memset(TruncStoreActions, 0, sizeof(TruncStoreActions));
Chris Lattnerc9133f92008-01-18 19:36:20 +0000162 memset(IndexedModeActions, 0, sizeof(IndexedModeActions));
163 memset(ConvertActions, 0, sizeof(ConvertActions));
Dan Gohman93f81e22007-07-09 20:49:44 +0000164
Chris Lattner1a3048b2007-12-22 20:47:56 +0000165 // Set default actions for various operations.
Evan Cheng5ff839f2006-11-09 18:56:43 +0000166 for (unsigned VT = 0; VT != (unsigned)MVT::LAST_VALUETYPE; ++VT) {
Chris Lattner1a3048b2007-12-22 20:47:56 +0000167 // Default all indexed load / store to expand.
Evan Cheng5ff839f2006-11-09 18:56:43 +0000168 for (unsigned IM = (unsigned)ISD::PRE_INC;
169 IM != (unsigned)ISD::LAST_INDEXED_MODE; ++IM) {
170 setIndexedLoadAction(IM, (MVT::ValueType)VT, Expand);
171 setIndexedStoreAction(IM, (MVT::ValueType)VT, Expand);
172 }
Chris Lattner1a3048b2007-12-22 20:47:56 +0000173
174 // These operations default to expand.
175 setOperationAction(ISD::FGETSIGN, (MVT::ValueType)VT, Expand);
Evan Cheng5ff839f2006-11-09 18:56:43 +0000176 }
Nate Begemane1795842008-02-14 08:57:00 +0000177
178 // ConstantFP nodes default to expand. Targets can either change this to
179 // Legal, in which case all fp constants are legal, or use addLegalFPImmediate
180 // to optimize expansions for certain constants.
181 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
182 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
183 setOperationAction(ISD::ConstantFP, MVT::f80, Expand);
Chris Lattner310968c2005-01-07 07:44:53 +0000184
Chris Lattner41bab0b2008-01-15 21:58:08 +0000185 // Default ISD::TRAP to expand (which turns it into abort).
186 setOperationAction(ISD::TRAP, MVT::Other, Expand);
187
Owen Andersona69571c2006-05-03 01:29:57 +0000188 IsLittleEndian = TD->isLittleEndian();
Chris Lattnercf9668f2006-10-06 22:52:08 +0000189 UsesGlobalOffsetTable = false;
Owen Andersona69571c2006-05-03 01:29:57 +0000190 ShiftAmountTy = SetCCResultTy = PointerTy = getValueType(TD->getIntPtrType());
Chris Lattnerd6e49672005-01-19 03:36:14 +0000191 ShiftAmtHandling = Undefined;
Chris Lattner310968c2005-01-07 07:44:53 +0000192 memset(RegClassForVT, 0,MVT::LAST_VALUETYPE*sizeof(TargetRegisterClass*));
Owen Anderson718cb662007-09-07 04:06:50 +0000193 memset(TargetDAGCombineArray, 0, array_lengthof(TargetDAGCombineArray));
Evan Chenga03a5dc2006-02-14 08:38:30 +0000194 maxStoresPerMemset = maxStoresPerMemcpy = maxStoresPerMemmove = 8;
Reid Spencer0f9beca2005-08-27 19:09:02 +0000195 allowUnalignedMemoryAccesses = false;
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000196 UseUnderscoreSetJmp = false;
197 UseUnderscoreLongJmp = false;
Chris Lattner66180392007-02-25 01:28:05 +0000198 SelectIsExpensive = false;
Nate Begeman405e3ec2005-10-21 00:02:42 +0000199 IntDivIsCheap = false;
200 Pow2DivIsCheap = false;
Chris Lattneree4a7652006-01-25 18:57:15 +0000201 StackPointerRegisterToSaveRestore = 0;
Jim Laskey9bb3c932007-02-22 18:04:49 +0000202 ExceptionPointerRegister = 0;
203 ExceptionSelectorRegister = 0;
Chris Lattnerdfe89342007-09-21 17:06:39 +0000204 SetCCResultContents = UndefinedSetCCResult;
Evan Cheng0577a222006-01-25 18:52:42 +0000205 SchedPreferenceInfo = SchedulingForLatency;
Chris Lattner7acf5f32006-09-05 17:39:15 +0000206 JumpBufSize = 0;
Duraid Madina0c9e0ff2006-09-04 07:44:11 +0000207 JumpBufAlignment = 0;
Evan Chengd60483e2007-05-16 23:45:53 +0000208 IfCvtBlockSizeLimit = 2;
Evan Cheng56966222007-01-12 02:11:51 +0000209
210 InitLibcallNames(LibcallRoutineNames);
Evan Chengd385fd62007-01-31 09:29:11 +0000211 InitCmpLibcallCCs(CmpLibcallCCs);
Dan Gohmanc3b0b5c2007-09-25 15:10:49 +0000212
213 // Tell Legalize whether the assembler supports DEBUG_LOC.
214 if (!TM.getTargetAsmInfo()->hasDotLocAndDotFile())
215 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Chris Lattner310968c2005-01-07 07:44:53 +0000216}
217
Chris Lattnercba82f92005-01-16 07:28:11 +0000218TargetLowering::~TargetLowering() {}
219
Rafael Espindolaf1ba1ca2007-11-05 23:12:20 +0000220
221SDOperand TargetLowering::LowerMEMCPY(SDOperand Op, SelectionDAG &DAG) {
222 assert(getSubtarget() && "Subtarget not defined");
223 SDOperand ChainOp = Op.getOperand(0);
224 SDOperand DestOp = Op.getOperand(1);
225 SDOperand SourceOp = Op.getOperand(2);
226 SDOperand CountOp = Op.getOperand(3);
227 SDOperand AlignOp = Op.getOperand(4);
228 SDOperand AlwaysInlineOp = Op.getOperand(5);
229
230 bool AlwaysInline = (bool)cast<ConstantSDNode>(AlwaysInlineOp)->getValue();
231 unsigned Align = (unsigned)cast<ConstantSDNode>(AlignOp)->getValue();
232 if (Align == 0) Align = 1;
233
234 // If size is unknown, call memcpy.
235 ConstantSDNode *I = dyn_cast<ConstantSDNode>(CountOp);
236 if (!I) {
237 assert(!AlwaysInline && "Cannot inline copy of unknown size");
238 return LowerMEMCPYCall(ChainOp, DestOp, SourceOp, CountOp, DAG);
239 }
240
241 // If not DWORD aligned or if size is more than threshold, then call memcpy.
242 // The libc version is likely to be faster for the following cases. It can
243 // use the address value and run time information about the CPU.
244 // With glibc 2.6.1 on a core 2, coping an array of 100M longs was 30% faster
245 unsigned Size = I->getValue();
246 if (AlwaysInline ||
247 (Size <= getSubtarget()->getMaxInlineSizeThreshold() &&
248 (Align & 3) == 0))
249 return LowerMEMCPYInline(ChainOp, DestOp, SourceOp, Size, Align, DAG);
250 return LowerMEMCPYCall(ChainOp, DestOp, SourceOp, CountOp, DAG);
251}
252
253
254SDOperand TargetLowering::LowerMEMCPYCall(SDOperand Chain,
255 SDOperand Dest,
256 SDOperand Source,
257 SDOperand Count,
258 SelectionDAG &DAG) {
259 MVT::ValueType IntPtr = getPointerTy();
260 TargetLowering::ArgListTy Args;
261 TargetLowering::ArgListEntry Entry;
262 Entry.Ty = getTargetData()->getIntPtrType();
263 Entry.Node = Dest; Args.push_back(Entry);
264 Entry.Node = Source; Args.push_back(Entry);
265 Entry.Node = Count; Args.push_back(Entry);
266 std::pair<SDOperand,SDOperand> CallResult =
267 LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false,
268 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
269 return CallResult.second;
270}
271
272
Chris Lattner310968c2005-01-07 07:44:53 +0000273/// computeRegisterProperties - Once all of the register classes are added,
274/// this allows us to compute derived properties we expose.
275void TargetLowering::computeRegisterProperties() {
Nate Begeman6a648612005-11-29 05:45:29 +0000276 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattnerbb97d812005-01-16 01:10:58 +0000277 "Too many value types for ValueTypeActions to hold!");
278
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000279 // Everything defaults to needing one register.
280 for (unsigned i = 0; i != MVT::LAST_VALUETYPE; ++i) {
Dan Gohmanb9f10192007-06-21 14:42:22 +0000281 NumRegistersForVT[i] = 1;
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000282 RegisterTypeForVT[i] = TransformToType[i] = i;
283 }
284 // ...except isVoid, which doesn't need any registers.
285 NumRegistersForVT[MVT::isVoid] = 0;
Misha Brukmanf976c852005-04-21 22:55:34 +0000286
Chris Lattner310968c2005-01-07 07:44:53 +0000287 // Find the largest integer register class.
288 unsigned LargestIntReg = MVT::i128;
289 for (; RegClassForVT[LargestIntReg] == 0; --LargestIntReg)
290 assert(LargestIntReg != MVT::i1 && "No integer registers defined!");
291
292 // Every integer value type larger than this largest register takes twice as
293 // many registers to represent as the previous ValueType.
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000294 for (MVT::ValueType ExpandedReg = LargestIntReg + 1;
295 MVT::isInteger(ExpandedReg); ++ExpandedReg) {
Dan Gohmanb9f10192007-06-21 14:42:22 +0000296 NumRegistersForVT[ExpandedReg] = 2*NumRegistersForVT[ExpandedReg-1];
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000297 RegisterTypeForVT[ExpandedReg] = LargestIntReg;
298 TransformToType[ExpandedReg] = ExpandedReg - 1;
299 ValueTypeActions.setTypeAction(ExpandedReg, Expand);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +0000300 }
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000301
302 // Inspect all of the ValueType's smaller than the largest integer
303 // register to see which ones need promotion.
304 MVT::ValueType LegalIntReg = LargestIntReg;
305 for (MVT::ValueType IntReg = LargestIntReg - 1;
306 IntReg >= MVT::i1; --IntReg) {
307 if (isTypeLegal(IntReg)) {
308 LegalIntReg = IntReg;
309 } else {
310 RegisterTypeForVT[IntReg] = TransformToType[IntReg] = LegalIntReg;
311 ValueTypeActions.setTypeAction(IntReg, Promote);
312 }
313 }
314
Dale Johannesen161e8972007-10-05 20:04:43 +0000315 // ppcf128 type is really two f64's.
316 if (!isTypeLegal(MVT::ppcf128)) {
317 NumRegistersForVT[MVT::ppcf128] = 2*NumRegistersForVT[MVT::f64];
318 RegisterTypeForVT[MVT::ppcf128] = MVT::f64;
319 TransformToType[MVT::ppcf128] = MVT::f64;
320 ValueTypeActions.setTypeAction(MVT::ppcf128, Expand);
321 }
322
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000323 // Decide how to handle f64. If the target does not have native f64 support,
324 // expand it to i64 and we will be generating soft float library calls.
325 if (!isTypeLegal(MVT::f64)) {
326 NumRegistersForVT[MVT::f64] = NumRegistersForVT[MVT::i64];
327 RegisterTypeForVT[MVT::f64] = RegisterTypeForVT[MVT::i64];
328 TransformToType[MVT::f64] = MVT::i64;
329 ValueTypeActions.setTypeAction(MVT::f64, Expand);
330 }
331
332 // Decide how to handle f32. If the target does not have native support for
333 // f32, promote it to f64 if it is legal. Otherwise, expand it to i32.
334 if (!isTypeLegal(MVT::f32)) {
335 if (isTypeLegal(MVT::f64)) {
336 NumRegistersForVT[MVT::f32] = NumRegistersForVT[MVT::f64];
337 RegisterTypeForVT[MVT::f32] = RegisterTypeForVT[MVT::f64];
338 TransformToType[MVT::f32] = MVT::f64;
339 ValueTypeActions.setTypeAction(MVT::f32, Promote);
340 } else {
341 NumRegistersForVT[MVT::f32] = NumRegistersForVT[MVT::i32];
342 RegisterTypeForVT[MVT::f32] = RegisterTypeForVT[MVT::i32];
343 TransformToType[MVT::f32] = MVT::i32;
344 ValueTypeActions.setTypeAction(MVT::f32, Expand);
345 }
Evan Cheng1a8f1fe2006-12-09 02:42:38 +0000346 }
Nate Begeman4ef3b812005-11-22 01:29:36 +0000347
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000348 // Loop over all of the vector value types to see which need transformations.
349 for (MVT::ValueType i = MVT::FIRST_VECTOR_VALUETYPE;
Evan Cheng677274b2006-03-23 23:24:51 +0000350 i <= MVT::LAST_VECTOR_VALUETYPE; ++i) {
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000351 if (!isTypeLegal(i)) {
352 MVT::ValueType IntermediateVT, RegisterVT;
353 unsigned NumIntermediates;
354 NumRegistersForVT[i] =
355 getVectorTypeBreakdown(i,
356 IntermediateVT, NumIntermediates,
357 RegisterVT);
358 RegisterTypeForVT[i] = RegisterVT;
359 TransformToType[i] = MVT::Other; // this isn't actually used
360 ValueTypeActions.setTypeAction(i, Expand);
Dan Gohman7f321562007-06-25 16:23:39 +0000361 }
Chris Lattner3a5935842006-03-16 19:50:01 +0000362 }
Chris Lattnerbb97d812005-01-16 01:10:58 +0000363}
Chris Lattnercba82f92005-01-16 07:28:11 +0000364
Evan Cheng72261582005-12-20 06:22:03 +0000365const char *TargetLowering::getTargetNodeName(unsigned Opcode) const {
366 return NULL;
367}
Evan Cheng3a03ebb2005-12-21 23:05:39 +0000368
Dan Gohman7f321562007-06-25 16:23:39 +0000369/// getVectorTypeBreakdown - Vector types are broken down into some number of
370/// legal first class types. For example, MVT::v8f32 maps to 2 MVT::v4f32
Chris Lattnerdc879292006-03-31 00:28:56 +0000371/// with Altivec or SSE1, or 8 promoted MVT::f64 values with the X86 FP stack.
Dan Gohman7f321562007-06-25 16:23:39 +0000372/// Similarly, MVT::v2i64 turns into 4 MVT::i32 values with both PPC and X86.
Chris Lattnerdc879292006-03-31 00:28:56 +0000373///
Dan Gohman7f321562007-06-25 16:23:39 +0000374/// This method returns the number of registers needed, and the VT for each
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000375/// register. It also returns the VT and quantity of the intermediate values
376/// before they are promoted/expanded.
Chris Lattnerdc879292006-03-31 00:28:56 +0000377///
Dan Gohman7f321562007-06-25 16:23:39 +0000378unsigned TargetLowering::getVectorTypeBreakdown(MVT::ValueType VT,
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000379 MVT::ValueType &IntermediateVT,
380 unsigned &NumIntermediates,
381 MVT::ValueType &RegisterVT) const {
Chris Lattnerdc879292006-03-31 00:28:56 +0000382 // Figure out the right, legal destination reg to copy into.
Dan Gohman7f321562007-06-25 16:23:39 +0000383 unsigned NumElts = MVT::getVectorNumElements(VT);
384 MVT::ValueType EltTy = MVT::getVectorElementType(VT);
Chris Lattnerdc879292006-03-31 00:28:56 +0000385
386 unsigned NumVectorRegs = 1;
387
Nate Begemand73ab882007-11-27 19:28:48 +0000388 // FIXME: We don't support non-power-of-2-sized vectors for now. Ideally we
389 // could break down into LHS/RHS like LegalizeDAG does.
390 if (!isPowerOf2_32(NumElts)) {
391 NumVectorRegs = NumElts;
392 NumElts = 1;
393 }
394
Chris Lattnerdc879292006-03-31 00:28:56 +0000395 // Divide the input until we get to a supported size. This will always
396 // end with a scalar if the target doesn't support vectors.
Dan Gohman7f321562007-06-25 16:23:39 +0000397 while (NumElts > 1 &&
398 !isTypeLegal(MVT::getVectorType(EltTy, NumElts))) {
Chris Lattnerdc879292006-03-31 00:28:56 +0000399 NumElts >>= 1;
400 NumVectorRegs <<= 1;
401 }
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000402
403 NumIntermediates = NumVectorRegs;
Chris Lattnerdc879292006-03-31 00:28:56 +0000404
Dan Gohman7f321562007-06-25 16:23:39 +0000405 MVT::ValueType NewVT = MVT::getVectorType(EltTy, NumElts);
406 if (!isTypeLegal(NewVT))
407 NewVT = EltTy;
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000408 IntermediateVT = NewVT;
Chris Lattnerdc879292006-03-31 00:28:56 +0000409
Dan Gohman7f321562007-06-25 16:23:39 +0000410 MVT::ValueType DestVT = getTypeToTransformTo(NewVT);
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000411 RegisterVT = DestVT;
Dan Gohman7f321562007-06-25 16:23:39 +0000412 if (DestVT < NewVT) {
Chris Lattnerdc879292006-03-31 00:28:56 +0000413 // Value is expanded, e.g. i64 -> i16.
Dan Gohman7f321562007-06-25 16:23:39 +0000414 return NumVectorRegs*(MVT::getSizeInBits(NewVT)/MVT::getSizeInBits(DestVT));
Chris Lattnerdc879292006-03-31 00:28:56 +0000415 } else {
416 // Otherwise, promotion or legal types use the same number of registers as
417 // the vector decimated to the appropriate level.
Chris Lattner79227e22006-03-31 00:46:36 +0000418 return NumVectorRegs;
Chris Lattnerdc879292006-03-31 00:28:56 +0000419 }
420
Evan Chenge9b3da12006-05-17 18:10:06 +0000421 return 1;
Chris Lattnerdc879292006-03-31 00:28:56 +0000422}
423
Evan Cheng3ae05432008-01-24 00:22:01 +0000424/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
425/// function arguments in the caller parameter area.
426unsigned TargetLowering::getByValTypeAlignment(const Type *Ty) const {
427 return Log2_32(TD->getCallFrameTypeAlignment(Ty));
428}
429
Evan Chengcc415862007-11-09 01:32:10 +0000430SDOperand TargetLowering::getPICJumpTableRelocBase(SDOperand Table,
431 SelectionDAG &DAG) const {
432 if (usesGlobalOffsetTable())
433 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
434 return Table;
435}
436
Chris Lattnereb8146b2006-02-04 02:13:02 +0000437//===----------------------------------------------------------------------===//
438// Optimization Methods
439//===----------------------------------------------------------------------===//
440
Nate Begeman368e18d2006-02-16 21:11:51 +0000441/// ShrinkDemandedConstant - Check to see if the specified operand of the
442/// specified instruction is a constant integer. If so, check to see if there
443/// are any bits set in the constant that are not demanded. If so, shrink the
444/// constant and return true.
445bool TargetLowering::TargetLoweringOpt::ShrinkDemandedConstant(SDOperand Op,
446 uint64_t Demanded) {
Chris Lattnerec665152006-02-26 23:36:02 +0000447 // FIXME: ISD::SELECT, ISD::SELECT_CC
Nate Begeman368e18d2006-02-16 21:11:51 +0000448 switch(Op.getOpcode()) {
449 default: break;
Nate Begemande996292006-02-03 22:24:05 +0000450 case ISD::AND:
Nate Begeman368e18d2006-02-16 21:11:51 +0000451 case ISD::OR:
452 case ISD::XOR:
453 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
454 if ((~Demanded & C->getValue()) != 0) {
455 MVT::ValueType VT = Op.getValueType();
456 SDOperand New = DAG.getNode(Op.getOpcode(), VT, Op.getOperand(0),
457 DAG.getConstant(Demanded & C->getValue(),
458 VT));
459 return CombineTo(Op, New);
Nate Begemande996292006-02-03 22:24:05 +0000460 }
Nate Begemande996292006-02-03 22:24:05 +0000461 break;
462 }
463 return false;
464}
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000465
Nate Begeman368e18d2006-02-16 21:11:51 +0000466/// SimplifyDemandedBits - Look at Op. At this point, we know that only the
467/// DemandedMask bits of the result of Op are ever used downstream. If we can
468/// use this information to simplify Op, create a new simplified DAG node and
469/// return true, returning the original and new nodes in Old and New. Otherwise,
470/// analyze the expression and return a mask of KnownOne and KnownZero bits for
471/// the expression (used to simplify the caller). The KnownZero/One bits may
472/// only be accurate for those bits in the DemandedMask.
473bool TargetLowering::SimplifyDemandedBits(SDOperand Op, uint64_t DemandedMask,
474 uint64_t &KnownZero,
475 uint64_t &KnownOne,
476 TargetLoweringOpt &TLO,
477 unsigned Depth) const {
478 KnownZero = KnownOne = 0; // Don't know anything.
Chris Lattner3fc5b012007-05-17 18:19:23 +0000479
480 // The masks are not wide enough to represent this type! Should use APInt.
481 if (Op.getValueType() == MVT::i128)
482 return false;
483
Nate Begeman368e18d2006-02-16 21:11:51 +0000484 // Other users may use these bits.
485 if (!Op.Val->hasOneUse()) {
486 if (Depth != 0) {
487 // If not at the root, Just compute the KnownZero/KnownOne bits to
488 // simplify things downstream.
Dan Gohmanea859be2007-06-22 14:59:07 +0000489 TLO.DAG.ComputeMaskedBits(Op, DemandedMask, KnownZero, KnownOne, Depth);
Nate Begeman368e18d2006-02-16 21:11:51 +0000490 return false;
491 }
492 // If this is the root being simplified, allow it to have multiple uses,
493 // just set the DemandedMask to all bits.
494 DemandedMask = MVT::getIntVTBitMask(Op.getValueType());
495 } else if (DemandedMask == 0) {
496 // Not demanding any bits from Op.
497 if (Op.getOpcode() != ISD::UNDEF)
498 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::UNDEF, Op.getValueType()));
499 return false;
500 } else if (Depth == 6) { // Limit search depth.
501 return false;
502 }
503
504 uint64_t KnownZero2, KnownOne2, KnownZeroOut, KnownOneOut;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000505 switch (Op.getOpcode()) {
506 case ISD::Constant:
Nate Begeman368e18d2006-02-16 21:11:51 +0000507 // We know all of the bits for a constant!
508 KnownOne = cast<ConstantSDNode>(Op)->getValue() & DemandedMask;
509 KnownZero = ~KnownOne & DemandedMask;
Chris Lattnerec665152006-02-26 23:36:02 +0000510 return false; // Don't fall through, will infinitely loop.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000511 case ISD::AND:
Chris Lattner81cd3552006-02-27 00:36:27 +0000512 // If the RHS is a constant, check to see if the LHS would be zero without
513 // using the bits from the RHS. Below, we use knowledge about the RHS to
514 // simplify the LHS, here we're using information from the LHS to simplify
515 // the RHS.
516 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
517 uint64_t LHSZero, LHSOne;
Dan Gohmanea859be2007-06-22 14:59:07 +0000518 TLO.DAG.ComputeMaskedBits(Op.getOperand(0), DemandedMask,
519 LHSZero, LHSOne, Depth+1);
Chris Lattner81cd3552006-02-27 00:36:27 +0000520 // If the LHS already has zeros where RHSC does, this and is dead.
521 if ((LHSZero & DemandedMask) == (~RHSC->getValue() & DemandedMask))
522 return TLO.CombineTo(Op, Op.getOperand(0));
523 // If any of the set bits in the RHS are known zero on the LHS, shrink
524 // the constant.
525 if (TLO.ShrinkDemandedConstant(Op, ~LHSZero & DemandedMask))
526 return true;
527 }
528
Nate Begeman368e18d2006-02-16 21:11:51 +0000529 if (SimplifyDemandedBits(Op.getOperand(1), DemandedMask, KnownZero,
530 KnownOne, TLO, Depth+1))
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000531 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000532 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Nate Begeman368e18d2006-02-16 21:11:51 +0000533 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & ~KnownZero,
534 KnownZero2, KnownOne2, TLO, Depth+1))
535 return true;
536 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
537
538 // If all of the demanded bits are known one on one side, return the other.
539 // These bits cannot contribute to the result of the 'and'.
540 if ((DemandedMask & ~KnownZero2 & KnownOne)==(DemandedMask & ~KnownZero2))
541 return TLO.CombineTo(Op, Op.getOperand(0));
542 if ((DemandedMask & ~KnownZero & KnownOne2)==(DemandedMask & ~KnownZero))
543 return TLO.CombineTo(Op, Op.getOperand(1));
544 // If all of the demanded bits in the inputs are known zeros, return zero.
545 if ((DemandedMask & (KnownZero|KnownZero2)) == DemandedMask)
546 return TLO.CombineTo(Op, TLO.DAG.getConstant(0, Op.getValueType()));
547 // If the RHS is a constant, see if we can simplify it.
548 if (TLO.ShrinkDemandedConstant(Op, DemandedMask & ~KnownZero2))
549 return true;
Chris Lattner5f0c6582006-02-27 00:22:28 +0000550
Nate Begeman368e18d2006-02-16 21:11:51 +0000551 // Output known-1 bits are only known if set in both the LHS & RHS.
552 KnownOne &= KnownOne2;
553 // Output known-0 are known to be clear if zero in either the LHS | RHS.
554 KnownZero |= KnownZero2;
555 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000556 case ISD::OR:
Nate Begeman368e18d2006-02-16 21:11:51 +0000557 if (SimplifyDemandedBits(Op.getOperand(1), DemandedMask, KnownZero,
558 KnownOne, TLO, Depth+1))
559 return true;
560 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
561 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & ~KnownOne,
562 KnownZero2, KnownOne2, TLO, Depth+1))
563 return true;
564 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
565
566 // If all of the demanded bits are known zero on one side, return the other.
567 // These bits cannot contribute to the result of the 'or'.
Jeff Cohen5755b172006-02-17 02:12:18 +0000568 if ((DemandedMask & ~KnownOne2 & KnownZero) == (DemandedMask & ~KnownOne2))
Nate Begeman368e18d2006-02-16 21:11:51 +0000569 return TLO.CombineTo(Op, Op.getOperand(0));
Jeff Cohen5755b172006-02-17 02:12:18 +0000570 if ((DemandedMask & ~KnownOne & KnownZero2) == (DemandedMask & ~KnownOne))
Nate Begeman368e18d2006-02-16 21:11:51 +0000571 return TLO.CombineTo(Op, Op.getOperand(1));
572 // If all of the potentially set bits on one side are known to be set on
573 // the other side, just use the 'other' side.
574 if ((DemandedMask & (~KnownZero) & KnownOne2) ==
575 (DemandedMask & (~KnownZero)))
576 return TLO.CombineTo(Op, Op.getOperand(0));
577 if ((DemandedMask & (~KnownZero2) & KnownOne) ==
578 (DemandedMask & (~KnownZero2)))
579 return TLO.CombineTo(Op, Op.getOperand(1));
580 // If the RHS is a constant, see if we can simplify it.
581 if (TLO.ShrinkDemandedConstant(Op, DemandedMask))
582 return true;
583
584 // Output known-0 bits are only known if clear in both the LHS & RHS.
585 KnownZero &= KnownZero2;
586 // Output known-1 are known to be set if set in either the LHS | RHS.
587 KnownOne |= KnownOne2;
588 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000589 case ISD::XOR:
Nate Begeman368e18d2006-02-16 21:11:51 +0000590 if (SimplifyDemandedBits(Op.getOperand(1), DemandedMask, KnownZero,
591 KnownOne, TLO, Depth+1))
592 return true;
593 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
594 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask, KnownZero2,
595 KnownOne2, TLO, Depth+1))
596 return true;
597 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
598
599 // If all of the demanded bits are known zero on one side, return the other.
600 // These bits cannot contribute to the result of the 'xor'.
601 if ((DemandedMask & KnownZero) == DemandedMask)
602 return TLO.CombineTo(Op, Op.getOperand(0));
603 if ((DemandedMask & KnownZero2) == DemandedMask)
604 return TLO.CombineTo(Op, Op.getOperand(1));
Chris Lattner3687c1a2006-11-27 21:50:02 +0000605
606 // If all of the unknown bits are known to be zero on one side or the other
607 // (but not both) turn this into an *inclusive* or.
608 // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
609 if ((DemandedMask & ~KnownZero & ~KnownZero2) == 0)
610 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::OR, Op.getValueType(),
611 Op.getOperand(0),
612 Op.getOperand(1)));
Nate Begeman368e18d2006-02-16 21:11:51 +0000613
614 // Output known-0 bits are known if clear or set in both the LHS & RHS.
615 KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
616 // Output known-1 are known to be set if set in only one of the LHS, RHS.
617 KnownOneOut = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
618
Nate Begeman368e18d2006-02-16 21:11:51 +0000619 // If all of the demanded bits on one side are known, and all of the set
620 // bits on that side are also known to be set on the other side, turn this
621 // into an AND, as we know the bits will be cleared.
622 // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
623 if ((DemandedMask & (KnownZero|KnownOne)) == DemandedMask) { // all known
624 if ((KnownOne & KnownOne2) == KnownOne) {
625 MVT::ValueType VT = Op.getValueType();
626 SDOperand ANDC = TLO.DAG.getConstant(~KnownOne & DemandedMask, VT);
627 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::AND, VT, Op.getOperand(0),
628 ANDC));
629 }
630 }
631
632 // If the RHS is a constant, see if we can simplify it.
633 // FIXME: for XOR, we prefer to force bits to 1 if they will make a -1.
634 if (TLO.ShrinkDemandedConstant(Op, DemandedMask))
635 return true;
636
637 KnownZero = KnownZeroOut;
638 KnownOne = KnownOneOut;
639 break;
640 case ISD::SETCC:
641 // If we know the result of a setcc has the top bits zero, use this info.
642 if (getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult)
643 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
644 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000645 case ISD::SELECT:
Nate Begeman368e18d2006-02-16 21:11:51 +0000646 if (SimplifyDemandedBits(Op.getOperand(2), DemandedMask, KnownZero,
647 KnownOne, TLO, Depth+1))
648 return true;
649 if (SimplifyDemandedBits(Op.getOperand(1), DemandedMask, KnownZero2,
650 KnownOne2, TLO, Depth+1))
651 return true;
652 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
653 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
654
655 // If the operands are constants, see if we can simplify them.
656 if (TLO.ShrinkDemandedConstant(Op, DemandedMask))
657 return true;
658
659 // Only known if known in both the LHS and RHS.
660 KnownOne &= KnownOne2;
661 KnownZero &= KnownZero2;
662 break;
Chris Lattnerec665152006-02-26 23:36:02 +0000663 case ISD::SELECT_CC:
664 if (SimplifyDemandedBits(Op.getOperand(3), DemandedMask, KnownZero,
665 KnownOne, TLO, Depth+1))
666 return true;
667 if (SimplifyDemandedBits(Op.getOperand(2), DemandedMask, KnownZero2,
668 KnownOne2, TLO, Depth+1))
669 return true;
670 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
671 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
672
673 // If the operands are constants, see if we can simplify them.
674 if (TLO.ShrinkDemandedConstant(Op, DemandedMask))
675 return true;
676
677 // Only known if known in both the LHS and RHS.
678 KnownOne &= KnownOne2;
679 KnownZero &= KnownZero2;
680 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000681 case ISD::SHL:
Nate Begeman368e18d2006-02-16 21:11:51 +0000682 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner895c4ab2007-04-17 21:14:16 +0000683 unsigned ShAmt = SA->getValue();
684 SDOperand InOp = Op.getOperand(0);
685
686 // If this is ((X >>u C1) << ShAmt), see if we can simplify this into a
687 // single shift. We can do this if the bottom bits (which are shifted
688 // out) are never demanded.
689 if (InOp.getOpcode() == ISD::SRL &&
690 isa<ConstantSDNode>(InOp.getOperand(1))) {
691 if (ShAmt && (DemandedMask & ((1ULL << ShAmt)-1)) == 0) {
692 unsigned C1 = cast<ConstantSDNode>(InOp.getOperand(1))->getValue();
693 unsigned Opc = ISD::SHL;
694 int Diff = ShAmt-C1;
695 if (Diff < 0) {
696 Diff = -Diff;
697 Opc = ISD::SRL;
698 }
699
700 SDOperand NewSA =
Chris Lattner4e7e6cd2007-05-30 16:30:06 +0000701 TLO.DAG.getConstant(Diff, Op.getOperand(1).getValueType());
Chris Lattner895c4ab2007-04-17 21:14:16 +0000702 MVT::ValueType VT = Op.getValueType();
Chris Lattner0a16a1f2007-04-18 03:01:40 +0000703 return TLO.CombineTo(Op, TLO.DAG.getNode(Opc, VT,
Chris Lattner895c4ab2007-04-17 21:14:16 +0000704 InOp.getOperand(0), NewSA));
705 }
706 }
707
708 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask >> ShAmt,
Nate Begeman368e18d2006-02-16 21:11:51 +0000709 KnownZero, KnownOne, TLO, Depth+1))
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000710 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000711 KnownZero <<= SA->getValue();
712 KnownOne <<= SA->getValue();
713 KnownZero |= (1ULL << SA->getValue())-1; // low bits known zero.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000714 }
715 break;
Nate Begeman368e18d2006-02-16 21:11:51 +0000716 case ISD::SRL:
717 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
718 MVT::ValueType VT = Op.getValueType();
719 unsigned ShAmt = SA->getValue();
Chris Lattner895c4ab2007-04-17 21:14:16 +0000720 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
721 unsigned VTSize = MVT::getSizeInBits(VT);
722 SDOperand InOp = Op.getOperand(0);
723
724 // If this is ((X << C1) >>u ShAmt), see if we can simplify this into a
725 // single shift. We can do this if the top bits (which are shifted out)
726 // are never demanded.
727 if (InOp.getOpcode() == ISD::SHL &&
728 isa<ConstantSDNode>(InOp.getOperand(1))) {
729 if (ShAmt && (DemandedMask & (~0ULL << (VTSize-ShAmt))) == 0) {
730 unsigned C1 = cast<ConstantSDNode>(InOp.getOperand(1))->getValue();
731 unsigned Opc = ISD::SRL;
732 int Diff = ShAmt-C1;
733 if (Diff < 0) {
734 Diff = -Diff;
735 Opc = ISD::SHL;
736 }
737
738 SDOperand NewSA =
Chris Lattner8c7d2d52007-04-17 22:53:02 +0000739 TLO.DAG.getConstant(Diff, Op.getOperand(1).getValueType());
Chris Lattner895c4ab2007-04-17 21:14:16 +0000740 return TLO.CombineTo(Op, TLO.DAG.getNode(Opc, VT,
741 InOp.getOperand(0), NewSA));
742 }
743 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000744
745 // Compute the new bits that are at the top now.
Chris Lattner895c4ab2007-04-17 21:14:16 +0000746 if (SimplifyDemandedBits(InOp, (DemandedMask << ShAmt) & TypeMask,
Nate Begeman368e18d2006-02-16 21:11:51 +0000747 KnownZero, KnownOne, TLO, Depth+1))
748 return true;
749 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
750 KnownZero &= TypeMask;
751 KnownOne &= TypeMask;
752 KnownZero >>= ShAmt;
753 KnownOne >>= ShAmt;
Chris Lattnerc4fa6032006-06-13 16:52:37 +0000754
755 uint64_t HighBits = (1ULL << ShAmt)-1;
Chris Lattner895c4ab2007-04-17 21:14:16 +0000756 HighBits <<= VTSize - ShAmt;
Chris Lattnerc4fa6032006-06-13 16:52:37 +0000757 KnownZero |= HighBits; // High bits known zero.
Nate Begeman368e18d2006-02-16 21:11:51 +0000758 }
759 break;
760 case ISD::SRA:
761 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
762 MVT::ValueType VT = Op.getValueType();
763 unsigned ShAmt = SA->getValue();
764
765 // Compute the new bits that are at the top now.
Nate Begeman368e18d2006-02-16 21:11:51 +0000766 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
767
Chris Lattner1b737132006-05-08 17:22:53 +0000768 uint64_t InDemandedMask = (DemandedMask << ShAmt) & TypeMask;
769
770 // If any of the demanded bits are produced by the sign extension, we also
771 // demand the input sign bit.
Chris Lattnerc4fa6032006-06-13 16:52:37 +0000772 uint64_t HighBits = (1ULL << ShAmt)-1;
773 HighBits <<= MVT::getSizeInBits(VT) - ShAmt;
Chris Lattner1b737132006-05-08 17:22:53 +0000774 if (HighBits & DemandedMask)
775 InDemandedMask |= MVT::getIntVTSignBit(VT);
776
777 if (SimplifyDemandedBits(Op.getOperand(0), InDemandedMask,
Nate Begeman368e18d2006-02-16 21:11:51 +0000778 KnownZero, KnownOne, TLO, Depth+1))
779 return true;
780 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
781 KnownZero &= TypeMask;
782 KnownOne &= TypeMask;
Chris Lattnerc4fa6032006-06-13 16:52:37 +0000783 KnownZero >>= ShAmt;
784 KnownOne >>= ShAmt;
Nate Begeman368e18d2006-02-16 21:11:51 +0000785
786 // Handle the sign bits.
787 uint64_t SignBit = MVT::getIntVTSignBit(VT);
Chris Lattnerc4fa6032006-06-13 16:52:37 +0000788 SignBit >>= ShAmt; // Adjust to where it is now in the mask.
Nate Begeman368e18d2006-02-16 21:11:51 +0000789
790 // If the input sign bit is known to be zero, or if none of the top bits
791 // are demanded, turn this into an unsigned shift right.
792 if ((KnownZero & SignBit) || (HighBits & ~DemandedMask) == HighBits) {
793 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, VT, Op.getOperand(0),
794 Op.getOperand(1)));
795 } else if (KnownOne & SignBit) { // New bits are known one.
796 KnownOne |= HighBits;
797 }
798 }
799 break;
800 case ISD::SIGN_EXTEND_INREG: {
Nate Begeman368e18d2006-02-16 21:11:51 +0000801 MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
802
Chris Lattnerec665152006-02-26 23:36:02 +0000803 // Sign extension. Compute the demanded bits in the result that are not
Nate Begeman368e18d2006-02-16 21:11:51 +0000804 // present in the input.
Chris Lattnerec665152006-02-26 23:36:02 +0000805 uint64_t NewBits = ~MVT::getIntVTBitMask(EVT) & DemandedMask;
Nate Begeman368e18d2006-02-16 21:11:51 +0000806
Chris Lattnerec665152006-02-26 23:36:02 +0000807 // If none of the extended bits are demanded, eliminate the sextinreg.
808 if (NewBits == 0)
809 return TLO.CombineTo(Op, Op.getOperand(0));
810
Nate Begeman368e18d2006-02-16 21:11:51 +0000811 uint64_t InSignBit = MVT::getIntVTSignBit(EVT);
812 int64_t InputDemandedBits = DemandedMask & MVT::getIntVTBitMask(EVT);
813
Chris Lattnerec665152006-02-26 23:36:02 +0000814 // Since the sign extended bits are demanded, we know that the sign
Nate Begeman368e18d2006-02-16 21:11:51 +0000815 // bit is demanded.
Chris Lattnerec665152006-02-26 23:36:02 +0000816 InputDemandedBits |= InSignBit;
Nate Begeman368e18d2006-02-16 21:11:51 +0000817
818 if (SimplifyDemandedBits(Op.getOperand(0), InputDemandedBits,
819 KnownZero, KnownOne, TLO, Depth+1))
820 return true;
821 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
822
823 // If the sign bit of the input is known set or clear, then we know the
824 // top bits of the result.
825
Chris Lattnerec665152006-02-26 23:36:02 +0000826 // If the input sign bit is known zero, convert this into a zero extension.
827 if (KnownZero & InSignBit)
828 return TLO.CombineTo(Op,
829 TLO.DAG.getZeroExtendInReg(Op.getOperand(0), EVT));
830
831 if (KnownOne & InSignBit) { // Input sign bit known set
Nate Begeman368e18d2006-02-16 21:11:51 +0000832 KnownOne |= NewBits;
833 KnownZero &= ~NewBits;
Chris Lattnerec665152006-02-26 23:36:02 +0000834 } else { // Input sign bit unknown
Nate Begeman368e18d2006-02-16 21:11:51 +0000835 KnownZero &= ~NewBits;
836 KnownOne &= ~NewBits;
837 }
838 break;
839 }
Chris Lattnerec665152006-02-26 23:36:02 +0000840 case ISD::CTTZ:
841 case ISD::CTLZ:
842 case ISD::CTPOP: {
843 MVT::ValueType VT = Op.getValueType();
844 unsigned LowBits = Log2_32(MVT::getSizeInBits(VT))+1;
845 KnownZero = ~((1ULL << LowBits)-1) & MVT::getIntVTBitMask(VT);
846 KnownOne = 0;
847 break;
848 }
Evan Cheng466685d2006-10-09 20:57:25 +0000849 case ISD::LOAD: {
Evan Chengc5484282006-10-04 00:56:09 +0000850 if (ISD::isZEXTLoad(Op.Val)) {
Evan Cheng466685d2006-10-09 20:57:25 +0000851 LoadSDNode *LD = cast<LoadSDNode>(Op);
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000852 MVT::ValueType VT = LD->getMemoryVT();
Evan Chengc5484282006-10-04 00:56:09 +0000853 KnownZero |= ~MVT::getIntVTBitMask(VT) & DemandedMask;
854 }
Chris Lattnerec665152006-02-26 23:36:02 +0000855 break;
856 }
857 case ISD::ZERO_EXTEND: {
858 uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType());
859
860 // If none of the top bits are demanded, convert this into an any_extend.
861 uint64_t NewBits = (~InMask) & DemandedMask;
862 if (NewBits == 0)
863 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ANY_EXTEND,
864 Op.getValueType(),
865 Op.getOperand(0)));
866
867 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & InMask,
868 KnownZero, KnownOne, TLO, Depth+1))
869 return true;
870 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
871 KnownZero |= NewBits;
872 break;
873 }
874 case ISD::SIGN_EXTEND: {
875 MVT::ValueType InVT = Op.getOperand(0).getValueType();
876 uint64_t InMask = MVT::getIntVTBitMask(InVT);
877 uint64_t InSignBit = MVT::getIntVTSignBit(InVT);
878 uint64_t NewBits = (~InMask) & DemandedMask;
879
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.
887 uint64_t InDemandedBits = DemandedMask & InMask;
888 InDemandedBits |= InSignBit;
889
890 if (SimplifyDemandedBits(Op.getOperand(0), InDemandedBits, KnownZero,
891 KnownOne, TLO, Depth+1))
892 return true;
893
894 // If the sign bit is known zero, convert this to a zero extend.
895 if (KnownZero & InSignBit)
896 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ZERO_EXTEND,
897 Op.getValueType(),
898 Op.getOperand(0)));
899
900 // If the sign bit is known one, the top bits match.
901 if (KnownOne & InSignBit) {
902 KnownOne |= NewBits;
903 KnownZero &= ~NewBits;
904 } else { // Otherwise, top bits aren't known.
905 KnownOne &= ~NewBits;
906 KnownZero &= ~NewBits;
907 }
908 break;
909 }
910 case ISD::ANY_EXTEND: {
911 uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType());
912 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & InMask,
913 KnownZero, KnownOne, TLO, Depth+1))
914 return true;
915 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
916 break;
917 }
Chris Lattnerfe8babf2006-05-05 22:32:12 +0000918 case ISD::TRUNCATE: {
Chris Lattnerc93dfda2006-05-06 00:11:52 +0000919 // Simplify the input, using demanded bit information, and compute the known
920 // zero/one bits live out.
Chris Lattnerfe8babf2006-05-05 22:32:12 +0000921 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask,
922 KnownZero, KnownOne, TLO, Depth+1))
923 return true;
Chris Lattnerc93dfda2006-05-06 00:11:52 +0000924
925 // If the input is only used by this truncate, see if we can shrink it based
926 // on the known demanded bits.
927 if (Op.getOperand(0).Val->hasOneUse()) {
928 SDOperand In = Op.getOperand(0);
929 switch (In.getOpcode()) {
930 default: break;
931 case ISD::SRL:
932 // Shrink SRL by a constant if none of the high bits shifted in are
933 // demanded.
934 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(In.getOperand(1))){
935 uint64_t HighBits = MVT::getIntVTBitMask(In.getValueType());
936 HighBits &= ~MVT::getIntVTBitMask(Op.getValueType());
937 HighBits >>= ShAmt->getValue();
938
939 if (ShAmt->getValue() < MVT::getSizeInBits(Op.getValueType()) &&
940 (DemandedMask & HighBits) == 0) {
941 // None of the shifted in bits are needed. Add a truncate of the
942 // shift input, then shift it.
943 SDOperand NewTrunc = TLO.DAG.getNode(ISD::TRUNCATE,
944 Op.getValueType(),
945 In.getOperand(0));
946 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL,Op.getValueType(),
947 NewTrunc, In.getOperand(1)));
948 }
949 }
950 break;
951 }
952 }
953
Chris Lattnerfe8babf2006-05-05 22:32:12 +0000954 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
955 uint64_t OutMask = MVT::getIntVTBitMask(Op.getValueType());
956 KnownZero &= OutMask;
957 KnownOne &= OutMask;
958 break;
959 }
Chris Lattnerec665152006-02-26 23:36:02 +0000960 case ISD::AssertZext: {
961 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
962 uint64_t InMask = MVT::getIntVTBitMask(VT);
963 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & InMask,
964 KnownZero, KnownOne, TLO, Depth+1))
965 return true;
966 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
967 KnownZero |= ~InMask & DemandedMask;
968 break;
969 }
Chris Lattner2ceb2cf2007-12-22 21:35:38 +0000970 case ISD::FGETSIGN:
971 // All bits are zero except the low bit.
972 KnownZero = MVT::getIntVTBitMask(Op.getValueType()) ^ 1;
973 break;
974 case ISD::BIT_CONVERT:
975#if 0
976 // If this is an FP->Int bitcast and if the sign bit is the only thing that
977 // is demanded, turn this into a FGETSIGN.
978 if (DemandedMask == MVT::getIntVTSignBit(Op.getValueType()) &&
979 MVT::isFloatingPoint(Op.getOperand(0).getValueType()) &&
980 !MVT::isVector(Op.getOperand(0).getValueType())) {
981 // Only do this xform if FGETSIGN is valid or if before legalize.
982 if (!TLO.AfterLegalize ||
983 isOperationLegal(ISD::FGETSIGN, Op.getValueType())) {
984 // Make a FGETSIGN + SHL to move the sign bit into the appropriate
985 // place. We expect the SHL to be eliminated by other optimizations.
986 SDOperand Sign = TLO.DAG.getNode(ISD::FGETSIGN, Op.getValueType(),
987 Op.getOperand(0));
988 unsigned ShVal = MVT::getSizeInBits(Op.getValueType())-1;
989 SDOperand ShAmt = TLO.DAG.getConstant(ShVal, getShiftAmountTy());
990 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SHL, Op.getValueType(),
991 Sign, ShAmt));
992 }
993 }
994#endif
995 break;
Nate Begeman368e18d2006-02-16 21:11:51 +0000996 case ISD::ADD:
Chris Lattnera6bc5a42006-02-27 01:00:42 +0000997 case ISD::SUB:
Chris Lattner1482b5f2006-04-02 06:15:09 +0000998 case ISD::INTRINSIC_WO_CHAIN:
999 case ISD::INTRINSIC_W_CHAIN:
1000 case ISD::INTRINSIC_VOID:
1001 // Just use ComputeMaskedBits to compute output bits.
Dan Gohmanea859be2007-06-22 14:59:07 +00001002 TLO.DAG.ComputeMaskedBits(Op, DemandedMask, KnownZero, KnownOne, Depth);
Chris Lattnera6bc5a42006-02-27 01:00:42 +00001003 break;
Nate Begeman368e18d2006-02-16 21:11:51 +00001004 }
Chris Lattnerec665152006-02-26 23:36:02 +00001005
1006 // If we know the value of all of the demanded bits, return this as a
1007 // constant.
1008 if ((DemandedMask & (KnownZero|KnownOne)) == DemandedMask)
1009 return TLO.CombineTo(Op, TLO.DAG.getConstant(KnownOne, Op.getValueType()));
1010
Nate Begeman368e18d2006-02-16 21:11:51 +00001011 return false;
1012}
1013
Nate Begeman368e18d2006-02-16 21:11:51 +00001014/// computeMaskedBitsForTargetNode - Determine which of the bits specified
1015/// in Mask are known to be either zero or one and return them in the
1016/// KnownZero/KnownOne bitsets.
1017void TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00001018 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001019 APInt &KnownZero,
1020 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00001021 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00001022 unsigned Depth) const {
Chris Lattner1b5232a2006-04-02 06:19:46 +00001023 assert((Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1024 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1025 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1026 Op.getOpcode() == ISD::INTRINSIC_VOID) &&
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001027 "Should use MaskedValueIsZero if you don't know whether Op"
1028 " is a target node!");
Dan Gohman977a76f2008-02-13 22:28:48 +00001029 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Cheng3a03ebb2005-12-21 23:05:39 +00001030}
Chris Lattner4ccb0702006-01-26 20:37:03 +00001031
Chris Lattner5c3e21d2006-05-06 09:27:13 +00001032/// ComputeNumSignBitsForTargetNode - This method can be implemented by
1033/// targets that want to expose additional information about sign bits to the
1034/// DAG Combiner.
1035unsigned TargetLowering::ComputeNumSignBitsForTargetNode(SDOperand Op,
1036 unsigned Depth) const {
1037 assert((Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1038 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1039 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1040 Op.getOpcode() == ISD::INTRINSIC_VOID) &&
1041 "Should use ComputeNumSignBits if you don't know whether Op"
1042 " is a target node!");
1043 return 1;
1044}
1045
1046
Evan Chengfa1eb272007-02-08 22:13:59 +00001047/// SimplifySetCC - Try to simplify a setcc built with the specified operands
1048/// and cc. If it is unable to simplify it, return a null SDOperand.
1049SDOperand
1050TargetLowering::SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
1051 ISD::CondCode Cond, bool foldBooleans,
1052 DAGCombinerInfo &DCI) const {
1053 SelectionDAG &DAG = DCI.DAG;
1054
1055 // These setcc operations always fold.
1056 switch (Cond) {
1057 default: break;
1058 case ISD::SETFALSE:
1059 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
1060 case ISD::SETTRUE:
1061 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
1062 }
1063
1064 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
1065 uint64_t C1 = N1C->getValue();
1066 if (isa<ConstantSDNode>(N0.Val)) {
1067 return DAG.FoldSetCC(VT, N0, N1, Cond);
1068 } else {
1069 // If the LHS is '(srl (ctlz x), 5)', the RHS is 0/1, and this is an
1070 // equality comparison, then we're just comparing whether X itself is
1071 // zero.
1072 if (N0.getOpcode() == ISD::SRL && (C1 == 0 || C1 == 1) &&
1073 N0.getOperand(0).getOpcode() == ISD::CTLZ &&
1074 N0.getOperand(1).getOpcode() == ISD::Constant) {
1075 unsigned ShAmt = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1076 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
1077 ShAmt == Log2_32(MVT::getSizeInBits(N0.getValueType()))) {
1078 if ((C1 == 0) == (Cond == ISD::SETEQ)) {
1079 // (srl (ctlz x), 5) == 0 -> X != 0
1080 // (srl (ctlz x), 5) != 1 -> X != 0
1081 Cond = ISD::SETNE;
1082 } else {
1083 // (srl (ctlz x), 5) != 0 -> X == 0
1084 // (srl (ctlz x), 5) == 1 -> X == 0
1085 Cond = ISD::SETEQ;
1086 }
1087 SDOperand Zero = DAG.getConstant(0, N0.getValueType());
1088 return DAG.getSetCC(VT, N0.getOperand(0).getOperand(0),
1089 Zero, Cond);
1090 }
1091 }
1092
1093 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
1094 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
1095 unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType());
1096
1097 // If the comparison constant has bits in the upper part, the
1098 // zero-extended value could never match.
1099 if (C1 & (~0ULL << InSize)) {
1100 unsigned VSize = MVT::getSizeInBits(N0.getValueType());
1101 switch (Cond) {
1102 case ISD::SETUGT:
1103 case ISD::SETUGE:
1104 case ISD::SETEQ: return DAG.getConstant(0, VT);
1105 case ISD::SETULT:
1106 case ISD::SETULE:
1107 case ISD::SETNE: return DAG.getConstant(1, VT);
1108 case ISD::SETGT:
1109 case ISD::SETGE:
1110 // True if the sign bit of C1 is set.
Chris Lattner01ca65b2007-02-24 02:09:29 +00001111 return DAG.getConstant((C1 & (1ULL << (VSize-1))) != 0, VT);
Evan Chengfa1eb272007-02-08 22:13:59 +00001112 case ISD::SETLT:
1113 case ISD::SETLE:
1114 // True if the sign bit of C1 isn't set.
Chris Lattner01ca65b2007-02-24 02:09:29 +00001115 return DAG.getConstant((C1 & (1ULL << (VSize-1))) == 0, VT);
Evan Chengfa1eb272007-02-08 22:13:59 +00001116 default:
1117 break;
1118 }
1119 }
1120
1121 // Otherwise, we can perform the comparison with the low bits.
1122 switch (Cond) {
1123 case ISD::SETEQ:
1124 case ISD::SETNE:
1125 case ISD::SETUGT:
1126 case ISD::SETUGE:
1127 case ISD::SETULT:
1128 case ISD::SETULE:
1129 return DAG.getSetCC(VT, N0.getOperand(0),
1130 DAG.getConstant(C1, N0.getOperand(0).getValueType()),
1131 Cond);
1132 default:
1133 break; // todo, be more careful with signed comparisons
1134 }
1135 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1136 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
1137 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
1138 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
1139 MVT::ValueType ExtDstTy = N0.getValueType();
1140 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
1141
1142 // If the extended part has any inconsistent bits, it cannot ever
1143 // compare equal. In other words, they have to be all ones or all
1144 // zeros.
1145 uint64_t ExtBits =
1146 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
1147 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
1148 return DAG.getConstant(Cond == ISD::SETNE, VT);
1149
1150 SDOperand ZextOp;
1151 MVT::ValueType Op0Ty = N0.getOperand(0).getValueType();
1152 if (Op0Ty == ExtSrcTy) {
1153 ZextOp = N0.getOperand(0);
1154 } else {
1155 int64_t Imm = ~0ULL >> (64-ExtSrcTyBits);
1156 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
1157 DAG.getConstant(Imm, Op0Ty));
1158 }
1159 if (!DCI.isCalledByLegalizer())
1160 DCI.AddToWorklist(ZextOp.Val);
1161 // Otherwise, make this a use of a zext.
1162 return DAG.getSetCC(VT, ZextOp,
1163 DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)),
1164 ExtDstTy),
1165 Cond);
1166 } else if ((N1C->getValue() == 0 || N1C->getValue() == 1) &&
1167 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
1168
1169 // SETCC (SETCC), [0|1], [EQ|NE] -> SETCC
1170 if (N0.getOpcode() == ISD::SETCC) {
1171 bool TrueWhenTrue = (Cond == ISD::SETEQ) ^ (N1C->getValue() != 1);
1172 if (TrueWhenTrue)
1173 return N0;
1174
1175 // Invert the condition.
1176 ISD::CondCode CC = cast<CondCodeSDNode>(N0.getOperand(2))->get();
1177 CC = ISD::getSetCCInverse(CC,
1178 MVT::isInteger(N0.getOperand(0).getValueType()));
1179 return DAG.getSetCC(VT, N0.getOperand(0), N0.getOperand(1), CC);
1180 }
1181
1182 if ((N0.getOpcode() == ISD::XOR ||
1183 (N0.getOpcode() == ISD::AND &&
1184 N0.getOperand(0).getOpcode() == ISD::XOR &&
1185 N0.getOperand(1) == N0.getOperand(0).getOperand(1))) &&
1186 isa<ConstantSDNode>(N0.getOperand(1)) &&
1187 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == 1) {
1188 // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We
1189 // can only do this if the top bits are known zero.
Dan Gohmanea859be2007-06-22 14:59:07 +00001190 if (DAG.MaskedValueIsZero(N0,
1191 MVT::getIntVTBitMask(N0.getValueType())-1)){
Evan Chengfa1eb272007-02-08 22:13:59 +00001192 // Okay, get the un-inverted input value.
1193 SDOperand Val;
1194 if (N0.getOpcode() == ISD::XOR)
1195 Val = N0.getOperand(0);
1196 else {
1197 assert(N0.getOpcode() == ISD::AND &&
1198 N0.getOperand(0).getOpcode() == ISD::XOR);
1199 // ((X^1)&1)^1 -> X & 1
1200 Val = DAG.getNode(ISD::AND, N0.getValueType(),
1201 N0.getOperand(0).getOperand(0),
1202 N0.getOperand(1));
1203 }
1204 return DAG.getSetCC(VT, Val, N1,
1205 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
1206 }
1207 }
1208 }
1209
1210 uint64_t MinVal, MaxVal;
1211 unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0));
1212 if (ISD::isSignedIntSetCC(Cond)) {
1213 MinVal = 1ULL << (OperandBitSize-1);
1214 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
1215 MaxVal = ~0ULL >> (65-OperandBitSize);
1216 else
1217 MaxVal = 0;
1218 } else {
1219 MinVal = 0;
1220 MaxVal = ~0ULL >> (64-OperandBitSize);
1221 }
1222
1223 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
1224 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
1225 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
1226 --C1; // X >= C0 --> X > (C0-1)
1227 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
1228 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
1229 }
1230
1231 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
1232 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
1233 ++C1; // X <= C0 --> X < (C0+1)
1234 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
1235 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
1236 }
1237
1238 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
1239 return DAG.getConstant(0, VT); // X < MIN --> false
1240 if ((Cond == ISD::SETGE || Cond == ISD::SETUGE) && C1 == MinVal)
1241 return DAG.getConstant(1, VT); // X >= MIN --> true
1242 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal)
1243 return DAG.getConstant(0, VT); // X > MAX --> false
1244 if ((Cond == ISD::SETLE || Cond == ISD::SETULE) && C1 == MaxVal)
1245 return DAG.getConstant(1, VT); // X <= MAX --> true
1246
1247 // Canonicalize setgt X, Min --> setne X, Min
1248 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
1249 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
1250 // Canonicalize setlt X, Max --> setne X, Max
1251 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
1252 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
1253
1254 // If we have setult X, 1, turn it into seteq X, 0
1255 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
1256 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
1257 ISD::SETEQ);
1258 // If we have setugt X, Max-1, turn it into seteq X, Max
1259 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
1260 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
1261 ISD::SETEQ);
1262
1263 // If we have "setcc X, C0", check to see if we can shrink the immediate
1264 // by changing cc.
1265
1266 // SETUGT X, SINTMAX -> SETLT X, 0
1267 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
1268 C1 == (~0ULL >> (65-OperandBitSize)))
1269 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
1270 ISD::SETLT);
1271
1272 // FIXME: Implement the rest of these.
1273
1274 // Fold bit comparisons when we can.
1275 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
1276 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
1277 if (ConstantSDNode *AndRHS =
1278 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1279 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
1280 // Perform the xform if the AND RHS is a single bit.
1281 if (isPowerOf2_64(AndRHS->getValue())) {
1282 return DAG.getNode(ISD::SRL, VT, N0,
1283 DAG.getConstant(Log2_64(AndRHS->getValue()),
1284 getShiftAmountTy()));
1285 }
1286 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
1287 // (X & 8) == 8 --> (X & 8) >> 3
1288 // Perform the xform if C1 is a single bit.
1289 if (isPowerOf2_64(C1)) {
1290 return DAG.getNode(ISD::SRL, VT, N0,
1291 DAG.getConstant(Log2_64(C1), getShiftAmountTy()));
1292 }
1293 }
1294 }
1295 }
1296 } else if (isa<ConstantSDNode>(N0.Val)) {
1297 // Ensure that the constant occurs on the RHS.
1298 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
1299 }
1300
1301 if (isa<ConstantFPSDNode>(N0.Val)) {
1302 // Constant fold or commute setcc.
1303 SDOperand O = DAG.FoldSetCC(VT, N0, N1, Cond);
1304 if (O.Val) return O;
Chris Lattner63079f02007-12-29 08:37:08 +00001305 } else if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1.Val)) {
1306 // If the RHS of an FP comparison is a constant, simplify it away in
1307 // some cases.
1308 if (CFP->getValueAPF().isNaN()) {
1309 // If an operand is known to be a nan, we can fold it.
1310 switch (ISD::getUnorderedFlavor(Cond)) {
1311 default: assert(0 && "Unknown flavor!");
1312 case 0: // Known false.
1313 return DAG.getConstant(0, VT);
1314 case 1: // Known true.
1315 return DAG.getConstant(1, VT);
Chris Lattner1c3e1e22007-12-30 21:21:10 +00001316 case 2: // Undefined.
Chris Lattner63079f02007-12-29 08:37:08 +00001317 return DAG.getNode(ISD::UNDEF, VT);
1318 }
1319 }
1320
1321 // Otherwise, we know the RHS is not a NaN. Simplify the node to drop the
1322 // constant if knowing that the operand is non-nan is enough. We prefer to
1323 // have SETO(x,x) instead of SETO(x, 0.0) because this avoids having to
1324 // materialize 0.0.
1325 if (Cond == ISD::SETO || Cond == ISD::SETUO)
1326 return DAG.getSetCC(VT, N0, N0, Cond);
Evan Chengfa1eb272007-02-08 22:13:59 +00001327 }
1328
1329 if (N0 == N1) {
1330 // We can always fold X == X for integer setcc's.
1331 if (MVT::isInteger(N0.getValueType()))
1332 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
1333 unsigned UOF = ISD::getUnorderedFlavor(Cond);
1334 if (UOF == 2) // FP operators that are undefined on NaNs.
1335 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
1336 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
1337 return DAG.getConstant(UOF, VT);
1338 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
1339 // if it is not already.
1340 ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
1341 if (NewCond != Cond)
1342 return DAG.getSetCC(VT, N0, N1, NewCond);
1343 }
1344
1345 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
1346 MVT::isInteger(N0.getValueType())) {
1347 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
1348 N0.getOpcode() == ISD::XOR) {
1349 // Simplify (X+Y) == (X+Z) --> Y == Z
1350 if (N0.getOpcode() == N1.getOpcode()) {
1351 if (N0.getOperand(0) == N1.getOperand(0))
1352 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
1353 if (N0.getOperand(1) == N1.getOperand(1))
1354 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
1355 if (DAG.isCommutativeBinOp(N0.getOpcode())) {
1356 // If X op Y == Y op X, try other combinations.
1357 if (N0.getOperand(0) == N1.getOperand(1))
1358 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
1359 if (N0.getOperand(1) == N1.getOperand(0))
1360 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(1), Cond);
1361 }
1362 }
1363
1364 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
1365 if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1366 // Turn (X+C1) == C2 --> X == C2-C1
1367 if (N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse()) {
1368 return DAG.getSetCC(VT, N0.getOperand(0),
1369 DAG.getConstant(RHSC->getValue()-LHSR->getValue(),
1370 N0.getValueType()), Cond);
1371 }
1372
1373 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
1374 if (N0.getOpcode() == ISD::XOR)
1375 // If we know that all of the inverted bits are zero, don't bother
1376 // performing the inversion.
Dan Gohmanea859be2007-06-22 14:59:07 +00001377 if (DAG.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getValue()))
Evan Chengfa1eb272007-02-08 22:13:59 +00001378 return DAG.getSetCC(VT, N0.getOperand(0),
1379 DAG.getConstant(LHSR->getValue()^RHSC->getValue(),
1380 N0.getValueType()), Cond);
1381 }
1382
1383 // Turn (C1-X) == C2 --> X == C1-C2
1384 if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
1385 if (N0.getOpcode() == ISD::SUB && N0.Val->hasOneUse()) {
1386 return DAG.getSetCC(VT, N0.getOperand(1),
1387 DAG.getConstant(SUBC->getValue()-RHSC->getValue(),
1388 N0.getValueType()), Cond);
1389 }
1390 }
1391 }
1392
1393 // Simplify (X+Z) == X --> Z == 0
1394 if (N0.getOperand(0) == N1)
1395 return DAG.getSetCC(VT, N0.getOperand(1),
1396 DAG.getConstant(0, N0.getValueType()), Cond);
1397 if (N0.getOperand(1) == N1) {
1398 if (DAG.isCommutativeBinOp(N0.getOpcode()))
1399 return DAG.getSetCC(VT, N0.getOperand(0),
1400 DAG.getConstant(0, N0.getValueType()), Cond);
Chris Lattner2ad913b2007-05-19 00:43:44 +00001401 else if (N0.Val->hasOneUse()) {
Evan Chengfa1eb272007-02-08 22:13:59 +00001402 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
1403 // (Z-X) == X --> Z == X<<1
1404 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
1405 N1,
1406 DAG.getConstant(1, getShiftAmountTy()));
1407 if (!DCI.isCalledByLegalizer())
1408 DCI.AddToWorklist(SH.Val);
1409 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
1410 }
1411 }
1412 }
1413
1414 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
1415 N1.getOpcode() == ISD::XOR) {
1416 // Simplify X == (X+Z) --> Z == 0
1417 if (N1.getOperand(0) == N0) {
1418 return DAG.getSetCC(VT, N1.getOperand(1),
1419 DAG.getConstant(0, N1.getValueType()), Cond);
1420 } else if (N1.getOperand(1) == N0) {
1421 if (DAG.isCommutativeBinOp(N1.getOpcode())) {
1422 return DAG.getSetCC(VT, N1.getOperand(0),
1423 DAG.getConstant(0, N1.getValueType()), Cond);
Chris Lattner7667c0b2007-05-19 00:46:51 +00001424 } else if (N1.Val->hasOneUse()) {
Evan Chengfa1eb272007-02-08 22:13:59 +00001425 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
1426 // X == (Z-X) --> X<<1 == Z
1427 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
1428 DAG.getConstant(1, getShiftAmountTy()));
1429 if (!DCI.isCalledByLegalizer())
1430 DCI.AddToWorklist(SH.Val);
1431 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
1432 }
1433 }
1434 }
1435 }
1436
1437 // Fold away ALL boolean setcc's.
1438 SDOperand Temp;
1439 if (N0.getValueType() == MVT::i1 && foldBooleans) {
1440 switch (Cond) {
1441 default: assert(0 && "Unknown integer setcc!");
1442 case ISD::SETEQ: // X == Y -> (X^Y)^1
1443 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
1444 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
1445 if (!DCI.isCalledByLegalizer())
1446 DCI.AddToWorklist(Temp.Val);
1447 break;
1448 case ISD::SETNE: // X != Y --> (X^Y)
1449 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
1450 break;
1451 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
1452 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
1453 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
1454 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
1455 if (!DCI.isCalledByLegalizer())
1456 DCI.AddToWorklist(Temp.Val);
1457 break;
1458 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
1459 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
1460 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
1461 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
1462 if (!DCI.isCalledByLegalizer())
1463 DCI.AddToWorklist(Temp.Val);
1464 break;
1465 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
1466 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
1467 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
1468 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
1469 if (!DCI.isCalledByLegalizer())
1470 DCI.AddToWorklist(Temp.Val);
1471 break;
1472 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
1473 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
1474 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
1475 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
1476 break;
1477 }
1478 if (VT != MVT::i1) {
1479 if (!DCI.isCalledByLegalizer())
1480 DCI.AddToWorklist(N0.Val);
1481 // FIXME: If running after legalize, we probably can't do this.
1482 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
1483 }
1484 return N0;
1485 }
1486
1487 // Could not fold it.
1488 return SDOperand();
1489}
1490
Chris Lattner00ffed02006-03-01 04:52:55 +00001491SDOperand TargetLowering::
1492PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
1493 // Default implementation: no optimization.
1494 return SDOperand();
1495}
1496
Chris Lattnereb8146b2006-02-04 02:13:02 +00001497//===----------------------------------------------------------------------===//
1498// Inline Assembler Implementation Methods
1499//===----------------------------------------------------------------------===//
1500
1501TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00001502TargetLowering::getConstraintType(const std::string &Constraint) const {
Chris Lattnereb8146b2006-02-04 02:13:02 +00001503 // FIXME: lots more standard ones to handle.
Chris Lattner4234f572007-03-25 02:14:49 +00001504 if (Constraint.size() == 1) {
1505 switch (Constraint[0]) {
1506 default: break;
1507 case 'r': return C_RegisterClass;
1508 case 'm': // memory
1509 case 'o': // offsetable
1510 case 'V': // not offsetable
1511 return C_Memory;
1512 case 'i': // Simple Integer or Relocatable Constant
1513 case 'n': // Simple Integer
1514 case 's': // Relocatable Constant
Chris Lattnerc13dd1c2007-03-25 04:35:41 +00001515 case 'X': // Allow ANY value.
Chris Lattner4234f572007-03-25 02:14:49 +00001516 case 'I': // Target registers.
1517 case 'J':
1518 case 'K':
1519 case 'L':
1520 case 'M':
1521 case 'N':
1522 case 'O':
1523 case 'P':
1524 return C_Other;
1525 }
Chris Lattnereb8146b2006-02-04 02:13:02 +00001526 }
Chris Lattner065421f2007-03-25 02:18:14 +00001527
1528 if (Constraint.size() > 1 && Constraint[0] == '{' &&
1529 Constraint[Constraint.size()-1] == '}')
1530 return C_Register;
Chris Lattner4234f572007-03-25 02:14:49 +00001531 return C_Unknown;
Chris Lattnereb8146b2006-02-04 02:13:02 +00001532}
1533
Dale Johannesenba2a0b92008-01-29 02:21:21 +00001534/// LowerXConstraint - try to replace an X constraint, which matches anything,
1535/// with another that has more specific requirements based on the type of the
1536/// corresponding operand.
1537void TargetLowering::lowerXConstraint(MVT::ValueType ConstraintVT,
1538 std::string& s) const {
1539 if (MVT::isInteger(ConstraintVT))
1540 s = "r";
1541 else if (MVT::isFloatingPoint(ConstraintVT))
1542 s = "f"; // works for many targets
1543 else
1544 s = "";
1545}
1546
Chris Lattner48884cd2007-08-25 00:47:38 +00001547/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
1548/// vector. If it is invalid, don't add anything to Ops.
1549void TargetLowering::LowerAsmOperandForConstraint(SDOperand Op,
1550 char ConstraintLetter,
1551 std::vector<SDOperand> &Ops,
1552 SelectionDAG &DAG) {
Chris Lattnereb8146b2006-02-04 02:13:02 +00001553 switch (ConstraintLetter) {
Chris Lattner9ff6ee82007-02-17 06:00:35 +00001554 default: break;
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001555 case 'X': // Allows any operand; labels (basic block) use this.
1556 if (Op.getOpcode() == ISD::BasicBlock) {
1557 Ops.push_back(Op);
1558 return;
1559 }
1560 // fall through
Chris Lattnereb8146b2006-02-04 02:13:02 +00001561 case 'i': // Simple Integer or Relocatable Constant
1562 case 'n': // Simple Integer
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001563 case 's': { // Relocatable Constant
Chris Lattner75c7d2b2007-05-03 16:54:34 +00001564 // These operands are interested in values of the form (GV+C), where C may
1565 // be folded in as an offset of GV, or it may be explicitly added. Also, it
1566 // is possible and fine if either GV or C are missing.
1567 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
1568 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
1569
1570 // If we have "(add GV, C)", pull out GV/C
1571 if (Op.getOpcode() == ISD::ADD) {
1572 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
1573 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
1574 if (C == 0 || GA == 0) {
1575 C = dyn_cast<ConstantSDNode>(Op.getOperand(0));
1576 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(1));
1577 }
1578 if (C == 0 || GA == 0)
1579 C = 0, GA = 0;
1580 }
1581
1582 // If we find a valid operand, map to the TargetXXX version so that the
1583 // value itself doesn't get selected.
1584 if (GA) { // Either &GV or &GV+C
1585 if (ConstraintLetter != 'n') {
1586 int64_t Offs = GA->getOffset();
1587 if (C) Offs += C->getValue();
Chris Lattner48884cd2007-08-25 00:47:38 +00001588 Ops.push_back(DAG.getTargetGlobalAddress(GA->getGlobal(),
1589 Op.getValueType(), Offs));
1590 return;
Chris Lattner75c7d2b2007-05-03 16:54:34 +00001591 }
1592 }
1593 if (C) { // just C, no GV.
Chris Lattner9ff6ee82007-02-17 06:00:35 +00001594 // Simple constants are not allowed for 's'.
Chris Lattner48884cd2007-08-25 00:47:38 +00001595 if (ConstraintLetter != 's') {
1596 Ops.push_back(DAG.getTargetConstant(C->getValue(), Op.getValueType()));
1597 return;
1598 }
Chris Lattner9ff6ee82007-02-17 06:00:35 +00001599 }
Chris Lattner9ff6ee82007-02-17 06:00:35 +00001600 break;
Chris Lattnereb8146b2006-02-04 02:13:02 +00001601 }
Chris Lattner75c7d2b2007-05-03 16:54:34 +00001602 }
Chris Lattnereb8146b2006-02-04 02:13:02 +00001603}
1604
Chris Lattner4ccb0702006-01-26 20:37:03 +00001605std::vector<unsigned> TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00001606getRegClassForInlineAsmConstraint(const std::string &Constraint,
1607 MVT::ValueType VT) const {
1608 return std::vector<unsigned>();
1609}
1610
1611
1612std::pair<unsigned, const TargetRegisterClass*> TargetLowering::
Chris Lattner4217ca8dc2006-02-21 23:11:00 +00001613getRegForInlineAsmConstraint(const std::string &Constraint,
1614 MVT::ValueType VT) const {
Chris Lattner1efa40f2006-02-22 00:56:39 +00001615 if (Constraint[0] != '{')
1616 return std::pair<unsigned, const TargetRegisterClass*>(0, 0);
Chris Lattnera55079a2006-02-01 01:29:47 +00001617 assert(*(Constraint.end()-1) == '}' && "Not a brace enclosed constraint?");
1618
1619 // Remove the braces from around the name.
1620 std::string RegName(Constraint.begin()+1, Constraint.end()-1);
Chris Lattner1efa40f2006-02-22 00:56:39 +00001621
1622 // Figure out which register class contains this reg.
Dan Gohman6f0d0242008-02-10 18:45:23 +00001623 const TargetRegisterInfo *RI = TM.getRegisterInfo();
1624 for (TargetRegisterInfo::regclass_iterator RCI = RI->regclass_begin(),
Chris Lattner1efa40f2006-02-22 00:56:39 +00001625 E = RI->regclass_end(); RCI != E; ++RCI) {
1626 const TargetRegisterClass *RC = *RCI;
Chris Lattnerb3befd42006-02-22 23:00:51 +00001627
1628 // If none of the the value types for this register class are valid, we
1629 // can't use it. For example, 64-bit reg classes on 32-bit targets.
1630 bool isLegal = false;
1631 for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
1632 I != E; ++I) {
1633 if (isTypeLegal(*I)) {
1634 isLegal = true;
1635 break;
1636 }
1637 }
1638
1639 if (!isLegal) continue;
1640
Chris Lattner1efa40f2006-02-22 00:56:39 +00001641 for (TargetRegisterClass::iterator I = RC->begin(), E = RC->end();
1642 I != E; ++I) {
Chris Lattnerb3befd42006-02-22 23:00:51 +00001643 if (StringsEqualNoCase(RegName, RI->get(*I).Name))
Chris Lattner1efa40f2006-02-22 00:56:39 +00001644 return std::make_pair(*I, RC);
Chris Lattner1efa40f2006-02-22 00:56:39 +00001645 }
Chris Lattner4ccb0702006-01-26 20:37:03 +00001646 }
Chris Lattnera55079a2006-02-01 01:29:47 +00001647
Chris Lattner1efa40f2006-02-22 00:56:39 +00001648 return std::pair<unsigned, const TargetRegisterClass*>(0, 0);
Chris Lattner4ccb0702006-01-26 20:37:03 +00001649}
Evan Cheng30b37b52006-03-13 23:18:16 +00001650
1651//===----------------------------------------------------------------------===//
1652// Loop Strength Reduction hooks
1653//===----------------------------------------------------------------------===//
1654
Chris Lattner1436bb62007-03-30 23:14:50 +00001655/// isLegalAddressingMode - Return true if the addressing mode represented
1656/// by AM is legal for this target, for a load/store of the specified type.
1657bool TargetLowering::isLegalAddressingMode(const AddrMode &AM,
1658 const Type *Ty) const {
1659 // The default implementation of this implements a conservative RISCy, r+r and
1660 // r+i addr mode.
1661
1662 // Allows a sign-extended 16-bit immediate field.
1663 if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1)
1664 return false;
1665
1666 // No global is ever allowed as a base.
1667 if (AM.BaseGV)
1668 return false;
1669
1670 // Only support r+r,
1671 switch (AM.Scale) {
1672 case 0: // "r+i" or just "i", depending on HasBaseReg.
1673 break;
1674 case 1:
1675 if (AM.HasBaseReg && AM.BaseOffs) // "r+r+i" is not allowed.
1676 return false;
1677 // Otherwise we have r+r or r+i.
1678 break;
1679 case 2:
1680 if (AM.HasBaseReg || AM.BaseOffs) // 2*r+r or 2*r+i is not allowed.
1681 return false;
1682 // Allow 2*r as r+r.
1683 break;
1684 }
1685
1686 return true;
1687}
1688
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00001689// Magic for divide replacement
1690
1691struct ms {
1692 int64_t m; // magic number
1693 int64_t s; // shift amount
1694};
1695
1696struct mu {
1697 uint64_t m; // magic number
1698 int64_t a; // add indicator
1699 int64_t s; // shift amount
1700};
1701
1702/// magic - calculate the magic numbers required to codegen an integer sdiv as
1703/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
1704/// or -1.
1705static ms magic32(int32_t d) {
1706 int32_t p;
1707 uint32_t ad, anc, delta, q1, r1, q2, r2, t;
1708 const uint32_t two31 = 0x80000000U;
1709 struct ms mag;
1710
1711 ad = abs(d);
1712 t = two31 + ((uint32_t)d >> 31);
1713 anc = t - 1 - t%ad; // absolute value of nc
1714 p = 31; // initialize p
1715 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
1716 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
1717 q2 = two31/ad; // initialize q2 = 2p/abs(d)
1718 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
1719 do {
1720 p = p + 1;
1721 q1 = 2*q1; // update q1 = 2p/abs(nc)
1722 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
1723 if (r1 >= anc) { // must be unsigned comparison
1724 q1 = q1 + 1;
1725 r1 = r1 - anc;
1726 }
1727 q2 = 2*q2; // update q2 = 2p/abs(d)
1728 r2 = 2*r2; // update r2 = rem(2p/abs(d))
1729 if (r2 >= ad) { // must be unsigned comparison
1730 q2 = q2 + 1;
1731 r2 = r2 - ad;
1732 }
1733 delta = ad - r2;
1734 } while (q1 < delta || (q1 == delta && r1 == 0));
1735
1736 mag.m = (int32_t)(q2 + 1); // make sure to sign extend
1737 if (d < 0) mag.m = -mag.m; // resulting magic number
1738 mag.s = p - 32; // resulting shift
1739 return mag;
1740}
1741
1742/// magicu - calculate the magic numbers required to codegen an integer udiv as
1743/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
1744static mu magicu32(uint32_t d) {
1745 int32_t p;
1746 uint32_t nc, delta, q1, r1, q2, r2;
1747 struct mu magu;
1748 magu.a = 0; // initialize "add" indicator
1749 nc = - 1 - (-d)%d;
1750 p = 31; // initialize p
1751 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
1752 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
1753 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
1754 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
1755 do {
1756 p = p + 1;
1757 if (r1 >= nc - r1 ) {
1758 q1 = 2*q1 + 1; // update q1
1759 r1 = 2*r1 - nc; // update r1
1760 }
1761 else {
1762 q1 = 2*q1; // update q1
1763 r1 = 2*r1; // update r1
1764 }
1765 if (r2 + 1 >= d - r2) {
1766 if (q2 >= 0x7FFFFFFF) magu.a = 1;
1767 q2 = 2*q2 + 1; // update q2
1768 r2 = 2*r2 + 1 - d; // update r2
1769 }
1770 else {
1771 if (q2 >= 0x80000000) magu.a = 1;
1772 q2 = 2*q2; // update q2
1773 r2 = 2*r2 + 1; // update r2
1774 }
1775 delta = d - 1 - r2;
1776 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
1777 magu.m = q2 + 1; // resulting magic number
1778 magu.s = p - 32; // resulting shift
1779 return magu;
1780}
1781
1782/// magic - calculate the magic numbers required to codegen an integer sdiv as
1783/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
1784/// or -1.
1785static ms magic64(int64_t d) {
1786 int64_t p;
1787 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
1788 const uint64_t two63 = 9223372036854775808ULL; // 2^63
1789 struct ms mag;
1790
1791 ad = d >= 0 ? d : -d;
1792 t = two63 + ((uint64_t)d >> 63);
1793 anc = t - 1 - t%ad; // absolute value of nc
1794 p = 63; // initialize p
1795 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
1796 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
1797 q2 = two63/ad; // initialize q2 = 2p/abs(d)
1798 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
1799 do {
1800 p = p + 1;
1801 q1 = 2*q1; // update q1 = 2p/abs(nc)
1802 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
1803 if (r1 >= anc) { // must be unsigned comparison
1804 q1 = q1 + 1;
1805 r1 = r1 - anc;
1806 }
1807 q2 = 2*q2; // update q2 = 2p/abs(d)
1808 r2 = 2*r2; // update r2 = rem(2p/abs(d))
1809 if (r2 >= ad) { // must be unsigned comparison
1810 q2 = q2 + 1;
1811 r2 = r2 - ad;
1812 }
1813 delta = ad - r2;
1814 } while (q1 < delta || (q1 == delta && r1 == 0));
1815
1816 mag.m = q2 + 1;
1817 if (d < 0) mag.m = -mag.m; // resulting magic number
1818 mag.s = p - 64; // resulting shift
1819 return mag;
1820}
1821
1822/// magicu - calculate the magic numbers required to codegen an integer udiv as
1823/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
1824static mu magicu64(uint64_t d)
1825{
1826 int64_t p;
1827 uint64_t nc, delta, q1, r1, q2, r2;
1828 struct mu magu;
1829 magu.a = 0; // initialize "add" indicator
1830 nc = - 1 - (-d)%d;
1831 p = 63; // initialize p
1832 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
1833 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
1834 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
1835 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
1836 do {
1837 p = p + 1;
1838 if (r1 >= nc - r1 ) {
1839 q1 = 2*q1 + 1; // update q1
1840 r1 = 2*r1 - nc; // update r1
1841 }
1842 else {
1843 q1 = 2*q1; // update q1
1844 r1 = 2*r1; // update r1
1845 }
1846 if (r2 + 1 >= d - r2) {
1847 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
1848 q2 = 2*q2 + 1; // update q2
1849 r2 = 2*r2 + 1 - d; // update r2
1850 }
1851 else {
1852 if (q2 >= 0x8000000000000000ull) magu.a = 1;
1853 q2 = 2*q2; // update q2
1854 r2 = 2*r2 + 1; // update r2
1855 }
1856 delta = d - 1 - r2;
Andrew Lenharth3e348492006-05-16 17:45:23 +00001857 } while (p < 128 && (q1 < delta || (q1 == delta && r1 == 0)));
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00001858 magu.m = q2 + 1; // resulting magic number
1859 magu.s = p - 64; // resulting shift
1860 return magu;
1861}
1862
1863/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
1864/// return a DAG expression to select that will generate the same value by
1865/// multiplying by a magic number. See:
1866/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
1867SDOperand TargetLowering::BuildSDIV(SDNode *N, SelectionDAG &DAG,
Anton Korobeynikovbed29462007-04-16 18:10:23 +00001868 std::vector<SDNode*>* Created) const {
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00001869 MVT::ValueType VT = N->getValueType(0);
1870
1871 // Check to see if we can do this.
1872 if (!isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64))
1873 return SDOperand(); // BuildSDIV only operates on i32 or i64
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00001874
1875 int64_t d = cast<ConstantSDNode>(N->getOperand(1))->getSignExtended();
1876 ms magics = (VT == MVT::i32) ? magic32(d) : magic64(d);
1877
1878 // Multiply the numerator (operand 0) by the magic value
Dan Gohman525178c2007-10-08 18:33:35 +00001879 SDOperand Q;
1880 if (isOperationLegal(ISD::MULHS, VT))
1881 Q = DAG.getNode(ISD::MULHS, VT, N->getOperand(0),
1882 DAG.getConstant(magics.m, VT));
1883 else if (isOperationLegal(ISD::SMUL_LOHI, VT))
1884 Q = SDOperand(DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(VT, VT),
1885 N->getOperand(0),
1886 DAG.getConstant(magics.m, VT)).Val, 1);
1887 else
1888 return SDOperand(); // No mulhs or equvialent
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00001889 // If d > 0 and m < 0, add the numerator
1890 if (d > 0 && magics.m < 0) {
1891 Q = DAG.getNode(ISD::ADD, VT, Q, N->getOperand(0));
1892 if (Created)
1893 Created->push_back(Q.Val);
1894 }
1895 // If d < 0 and m > 0, subtract the numerator.
1896 if (d < 0 && magics.m > 0) {
1897 Q = DAG.getNode(ISD::SUB, VT, Q, N->getOperand(0));
1898 if (Created)
1899 Created->push_back(Q.Val);
1900 }
1901 // Shift right algebraic if shift value is nonzero
1902 if (magics.s > 0) {
1903 Q = DAG.getNode(ISD::SRA, VT, Q,
1904 DAG.getConstant(magics.s, getShiftAmountTy()));
1905 if (Created)
1906 Created->push_back(Q.Val);
1907 }
1908 // Extract the sign bit and add it to the quotient
1909 SDOperand T =
1910 DAG.getNode(ISD::SRL, VT, Q, DAG.getConstant(MVT::getSizeInBits(VT)-1,
1911 getShiftAmountTy()));
1912 if (Created)
1913 Created->push_back(T.Val);
1914 return DAG.getNode(ISD::ADD, VT, Q, T);
1915}
1916
1917/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
1918/// return a DAG expression to select that will generate the same value by
1919/// multiplying by a magic number. See:
1920/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
1921SDOperand TargetLowering::BuildUDIV(SDNode *N, SelectionDAG &DAG,
Anton Korobeynikovbed29462007-04-16 18:10:23 +00001922 std::vector<SDNode*>* Created) const {
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00001923 MVT::ValueType VT = N->getValueType(0);
1924
1925 // Check to see if we can do this.
1926 if (!isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64))
1927 return SDOperand(); // BuildUDIV only operates on i32 or i64
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00001928
1929 uint64_t d = cast<ConstantSDNode>(N->getOperand(1))->getValue();
1930 mu magics = (VT == MVT::i32) ? magicu32(d) : magicu64(d);
1931
1932 // Multiply the numerator (operand 0) by the magic value
Dan Gohman525178c2007-10-08 18:33:35 +00001933 SDOperand Q;
1934 if (isOperationLegal(ISD::MULHU, VT))
1935 Q = DAG.getNode(ISD::MULHU, VT, N->getOperand(0),
1936 DAG.getConstant(magics.m, VT));
1937 else if (isOperationLegal(ISD::UMUL_LOHI, VT))
1938 Q = SDOperand(DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(VT, VT),
1939 N->getOperand(0),
1940 DAG.getConstant(magics.m, VT)).Val, 1);
1941 else
1942 return SDOperand(); // No mulhu or equvialent
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00001943 if (Created)
1944 Created->push_back(Q.Val);
1945
1946 if (magics.a == 0) {
1947 return DAG.getNode(ISD::SRL, VT, Q,
1948 DAG.getConstant(magics.s, getShiftAmountTy()));
1949 } else {
1950 SDOperand NPQ = DAG.getNode(ISD::SUB, VT, N->getOperand(0), Q);
1951 if (Created)
1952 Created->push_back(NPQ.Val);
1953 NPQ = DAG.getNode(ISD::SRL, VT, NPQ,
1954 DAG.getConstant(1, getShiftAmountTy()));
1955 if (Created)
1956 Created->push_back(NPQ.Val);
1957 NPQ = DAG.getNode(ISD::ADD, VT, NPQ, Q);
1958 if (Created)
1959 Created->push_back(NPQ.Val);
1960 return DAG.getNode(ISD::SRL, VT, NPQ,
1961 DAG.getConstant(magics.s-1, getShiftAmountTy()));
1962 }
1963}