blob: 4a2a0c039021ba3963ffee3ca0b99c803187f5f7 [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"
Dan Gohman707e0182008-04-12 04:36:06 +000020#include "llvm/GlobalVariable.h"
Chris Lattnerdc879292006-03-31 00:28:56 +000021#include "llvm/DerivedTypes.h"
Evan Chengad4196b2008-05-12 19:56:52 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner310968c2005-01-07 07:44:53 +000023#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner4ccb0702006-01-26 20:37:03 +000024#include "llvm/ADT/StringExtras.h"
Owen Anderson718cb662007-09-07 04:06:50 +000025#include "llvm/ADT/STLExtras.h"
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +000026#include "llvm/Support/MathExtras.h"
Chris Lattner310968c2005-01-07 07:44:53 +000027using namespace llvm;
28
Evan Cheng56966222007-01-12 02:11:51 +000029/// InitLibcallNames - Set default libcall names.
30///
Evan Cheng79cca502007-01-12 22:51:10 +000031static void InitLibcallNames(const char **Names) {
Evan Cheng56966222007-01-12 02:11:51 +000032 Names[RTLIB::SHL_I32] = "__ashlsi3";
33 Names[RTLIB::SHL_I64] = "__ashldi3";
Duncan Sandsdddc6292008-07-11 16:52:29 +000034 Names[RTLIB::SHL_I128] = "__ashlti3";
Evan Cheng56966222007-01-12 02:11:51 +000035 Names[RTLIB::SRL_I32] = "__lshrsi3";
36 Names[RTLIB::SRL_I64] = "__lshrdi3";
Duncan Sandsdddc6292008-07-11 16:52:29 +000037 Names[RTLIB::SRL_I128] = "__lshrti3";
Evan Cheng56966222007-01-12 02:11:51 +000038 Names[RTLIB::SRA_I32] = "__ashrsi3";
39 Names[RTLIB::SRA_I64] = "__ashrdi3";
Duncan Sandsdddc6292008-07-11 16:52:29 +000040 Names[RTLIB::SRA_I128] = "__ashrti3";
Evan Cheng56966222007-01-12 02:11:51 +000041 Names[RTLIB::MUL_I32] = "__mulsi3";
42 Names[RTLIB::MUL_I64] = "__muldi3";
Duncan Sands5ac319a2008-07-10 15:35:05 +000043 Names[RTLIB::MUL_I128] = "__multi3";
Evan Cheng56966222007-01-12 02:11:51 +000044 Names[RTLIB::SDIV_I32] = "__divsi3";
45 Names[RTLIB::SDIV_I64] = "__divdi3";
Duncan Sands5ac319a2008-07-10 15:35:05 +000046 Names[RTLIB::SDIV_I128] = "__divti3";
Evan Cheng56966222007-01-12 02:11:51 +000047 Names[RTLIB::UDIV_I32] = "__udivsi3";
48 Names[RTLIB::UDIV_I64] = "__udivdi3";
Duncan Sands5ac319a2008-07-10 15:35:05 +000049 Names[RTLIB::UDIV_I128] = "__udivti3";
Evan Cheng56966222007-01-12 02:11:51 +000050 Names[RTLIB::SREM_I32] = "__modsi3";
51 Names[RTLIB::SREM_I64] = "__moddi3";
Duncan Sands5ac319a2008-07-10 15:35:05 +000052 Names[RTLIB::SREM_I128] = "__modti3";
Evan Cheng56966222007-01-12 02:11:51 +000053 Names[RTLIB::UREM_I32] = "__umodsi3";
54 Names[RTLIB::UREM_I64] = "__umoddi3";
Duncan Sands5ac319a2008-07-10 15:35:05 +000055 Names[RTLIB::UREM_I128] = "__umodti3";
Evan Cheng56966222007-01-12 02:11:51 +000056 Names[RTLIB::NEG_I32] = "__negsi2";
57 Names[RTLIB::NEG_I64] = "__negdi2";
58 Names[RTLIB::ADD_F32] = "__addsf3";
59 Names[RTLIB::ADD_F64] = "__adddf3";
Duncan Sands007f9842008-01-10 10:28:30 +000060 Names[RTLIB::ADD_F80] = "__addxf3";
Dale Johannesen161e8972007-10-05 20:04:43 +000061 Names[RTLIB::ADD_PPCF128] = "__gcc_qadd";
Evan Cheng56966222007-01-12 02:11:51 +000062 Names[RTLIB::SUB_F32] = "__subsf3";
63 Names[RTLIB::SUB_F64] = "__subdf3";
Duncan Sands007f9842008-01-10 10:28:30 +000064 Names[RTLIB::SUB_F80] = "__subxf3";
Dale Johannesen161e8972007-10-05 20:04:43 +000065 Names[RTLIB::SUB_PPCF128] = "__gcc_qsub";
Evan Cheng56966222007-01-12 02:11:51 +000066 Names[RTLIB::MUL_F32] = "__mulsf3";
67 Names[RTLIB::MUL_F64] = "__muldf3";
Duncan Sands007f9842008-01-10 10:28:30 +000068 Names[RTLIB::MUL_F80] = "__mulxf3";
Dale Johannesen161e8972007-10-05 20:04:43 +000069 Names[RTLIB::MUL_PPCF128] = "__gcc_qmul";
Evan Cheng56966222007-01-12 02:11:51 +000070 Names[RTLIB::DIV_F32] = "__divsf3";
71 Names[RTLIB::DIV_F64] = "__divdf3";
Duncan Sands007f9842008-01-10 10:28:30 +000072 Names[RTLIB::DIV_F80] = "__divxf3";
Dale Johannesen161e8972007-10-05 20:04:43 +000073 Names[RTLIB::DIV_PPCF128] = "__gcc_qdiv";
Evan Cheng56966222007-01-12 02:11:51 +000074 Names[RTLIB::REM_F32] = "fmodf";
75 Names[RTLIB::REM_F64] = "fmod";
Duncan Sands007f9842008-01-10 10:28:30 +000076 Names[RTLIB::REM_F80] = "fmodl";
Dale Johannesen161e8972007-10-05 20:04:43 +000077 Names[RTLIB::REM_PPCF128] = "fmodl";
Evan Cheng56966222007-01-12 02:11:51 +000078 Names[RTLIB::POWI_F32] = "__powisf2";
79 Names[RTLIB::POWI_F64] = "__powidf2";
Dale Johannesen161e8972007-10-05 20:04:43 +000080 Names[RTLIB::POWI_F80] = "__powixf2";
81 Names[RTLIB::POWI_PPCF128] = "__powitf2";
Evan Cheng56966222007-01-12 02:11:51 +000082 Names[RTLIB::SQRT_F32] = "sqrtf";
83 Names[RTLIB::SQRT_F64] = "sqrt";
Dale Johannesen161e8972007-10-05 20:04:43 +000084 Names[RTLIB::SQRT_F80] = "sqrtl";
85 Names[RTLIB::SQRT_PPCF128] = "sqrtl";
Evan Cheng56966222007-01-12 02:11:51 +000086 Names[RTLIB::SIN_F32] = "sinf";
87 Names[RTLIB::SIN_F64] = "sin";
Duncan Sands007f9842008-01-10 10:28:30 +000088 Names[RTLIB::SIN_F80] = "sinl";
89 Names[RTLIB::SIN_PPCF128] = "sinl";
Evan Cheng56966222007-01-12 02:11:51 +000090 Names[RTLIB::COS_F32] = "cosf";
91 Names[RTLIB::COS_F64] = "cos";
Duncan Sands007f9842008-01-10 10:28:30 +000092 Names[RTLIB::COS_F80] = "cosl";
93 Names[RTLIB::COS_PPCF128] = "cosl";
Dan Gohmane54be102007-10-11 23:09:10 +000094 Names[RTLIB::POW_F32] = "powf";
95 Names[RTLIB::POW_F64] = "pow";
96 Names[RTLIB::POW_F80] = "powl";
97 Names[RTLIB::POW_PPCF128] = "powl";
Evan Cheng56966222007-01-12 02:11:51 +000098 Names[RTLIB::FPEXT_F32_F64] = "__extendsfdf2";
99 Names[RTLIB::FPROUND_F64_F32] = "__truncdfsf2";
100 Names[RTLIB::FPTOSINT_F32_I32] = "__fixsfsi";
101 Names[RTLIB::FPTOSINT_F32_I64] = "__fixsfdi";
Dan Gohmana2e94852008-03-10 23:03:31 +0000102 Names[RTLIB::FPTOSINT_F32_I128] = "__fixsfti";
Evan Cheng56966222007-01-12 02:11:51 +0000103 Names[RTLIB::FPTOSINT_F64_I32] = "__fixdfsi";
104 Names[RTLIB::FPTOSINT_F64_I64] = "__fixdfdi";
Dan Gohmana2e94852008-03-10 23:03:31 +0000105 Names[RTLIB::FPTOSINT_F64_I128] = "__fixdfti";
Duncan Sandsbe1ad4d2008-07-10 15:33:02 +0000106 Names[RTLIB::FPTOSINT_F80_I32] = "__fixxfsi";
Dale Johannesen161e8972007-10-05 20:04:43 +0000107 Names[RTLIB::FPTOSINT_F80_I64] = "__fixxfdi";
Dan Gohmana2e94852008-03-10 23:03:31 +0000108 Names[RTLIB::FPTOSINT_F80_I128] = "__fixxfti";
Duncan Sands041cde22008-06-25 20:24:48 +0000109 Names[RTLIB::FPTOSINT_PPCF128_I32] = "__fixtfsi";
Dale Johannesen161e8972007-10-05 20:04:43 +0000110 Names[RTLIB::FPTOSINT_PPCF128_I64] = "__fixtfdi";
Dan Gohmana2e94852008-03-10 23:03:31 +0000111 Names[RTLIB::FPTOSINT_PPCF128_I128] = "__fixtfti";
Evan Cheng56966222007-01-12 02:11:51 +0000112 Names[RTLIB::FPTOUINT_F32_I32] = "__fixunssfsi";
113 Names[RTLIB::FPTOUINT_F32_I64] = "__fixunssfdi";
Dan Gohmana2e94852008-03-10 23:03:31 +0000114 Names[RTLIB::FPTOUINT_F32_I128] = "__fixunssfti";
Evan Cheng56966222007-01-12 02:11:51 +0000115 Names[RTLIB::FPTOUINT_F64_I32] = "__fixunsdfsi";
116 Names[RTLIB::FPTOUINT_F64_I64] = "__fixunsdfdi";
Dan Gohmana2e94852008-03-10 23:03:31 +0000117 Names[RTLIB::FPTOUINT_F64_I128] = "__fixunsdfti";
Dale Johannesen161e8972007-10-05 20:04:43 +0000118 Names[RTLIB::FPTOUINT_F80_I32] = "__fixunsxfsi";
119 Names[RTLIB::FPTOUINT_F80_I64] = "__fixunsxfdi";
Dan Gohmana2e94852008-03-10 23:03:31 +0000120 Names[RTLIB::FPTOUINT_F80_I128] = "__fixunsxfti";
Duncan Sands041cde22008-06-25 20:24:48 +0000121 Names[RTLIB::FPTOUINT_PPCF128_I32] = "__fixunstfsi";
Dale Johannesen161e8972007-10-05 20:04:43 +0000122 Names[RTLIB::FPTOUINT_PPCF128_I64] = "__fixunstfdi";
Dan Gohmana2e94852008-03-10 23:03:31 +0000123 Names[RTLIB::FPTOUINT_PPCF128_I128] = "__fixunstfti";
Evan Cheng56966222007-01-12 02:11:51 +0000124 Names[RTLIB::SINTTOFP_I32_F32] = "__floatsisf";
125 Names[RTLIB::SINTTOFP_I32_F64] = "__floatsidf";
126 Names[RTLIB::SINTTOFP_I64_F32] = "__floatdisf";
127 Names[RTLIB::SINTTOFP_I64_F64] = "__floatdidf";
Dale Johannesen161e8972007-10-05 20:04:43 +0000128 Names[RTLIB::SINTTOFP_I64_F80] = "__floatdixf";
129 Names[RTLIB::SINTTOFP_I64_PPCF128] = "__floatditf";
Dan Gohmand91446d2008-03-05 01:08:17 +0000130 Names[RTLIB::SINTTOFP_I128_F32] = "__floattisf";
131 Names[RTLIB::SINTTOFP_I128_F64] = "__floattidf";
132 Names[RTLIB::SINTTOFP_I128_F80] = "__floattixf";
133 Names[RTLIB::SINTTOFP_I128_PPCF128] = "__floattitf";
Evan Cheng56966222007-01-12 02:11:51 +0000134 Names[RTLIB::UINTTOFP_I32_F32] = "__floatunsisf";
135 Names[RTLIB::UINTTOFP_I32_F64] = "__floatunsidf";
136 Names[RTLIB::UINTTOFP_I64_F32] = "__floatundisf";
137 Names[RTLIB::UINTTOFP_I64_F64] = "__floatundidf";
138 Names[RTLIB::OEQ_F32] = "__eqsf2";
139 Names[RTLIB::OEQ_F64] = "__eqdf2";
140 Names[RTLIB::UNE_F32] = "__nesf2";
141 Names[RTLIB::UNE_F64] = "__nedf2";
142 Names[RTLIB::OGE_F32] = "__gesf2";
143 Names[RTLIB::OGE_F64] = "__gedf2";
144 Names[RTLIB::OLT_F32] = "__ltsf2";
145 Names[RTLIB::OLT_F64] = "__ltdf2";
146 Names[RTLIB::OLE_F32] = "__lesf2";
147 Names[RTLIB::OLE_F64] = "__ledf2";
148 Names[RTLIB::OGT_F32] = "__gtsf2";
149 Names[RTLIB::OGT_F64] = "__gtdf2";
150 Names[RTLIB::UO_F32] = "__unordsf2";
151 Names[RTLIB::UO_F64] = "__unorddf2";
Evan Chengd385fd62007-01-31 09:29:11 +0000152 Names[RTLIB::O_F32] = "__unordsf2";
153 Names[RTLIB::O_F64] = "__unorddf2";
154}
155
156/// InitCmpLibcallCCs - Set default comparison libcall CC.
157///
158static void InitCmpLibcallCCs(ISD::CondCode *CCs) {
159 memset(CCs, ISD::SETCC_INVALID, sizeof(ISD::CondCode)*RTLIB::UNKNOWN_LIBCALL);
160 CCs[RTLIB::OEQ_F32] = ISD::SETEQ;
161 CCs[RTLIB::OEQ_F64] = ISD::SETEQ;
162 CCs[RTLIB::UNE_F32] = ISD::SETNE;
163 CCs[RTLIB::UNE_F64] = ISD::SETNE;
164 CCs[RTLIB::OGE_F32] = ISD::SETGE;
165 CCs[RTLIB::OGE_F64] = ISD::SETGE;
166 CCs[RTLIB::OLT_F32] = ISD::SETLT;
167 CCs[RTLIB::OLT_F64] = ISD::SETLT;
168 CCs[RTLIB::OLE_F32] = ISD::SETLE;
169 CCs[RTLIB::OLE_F64] = ISD::SETLE;
170 CCs[RTLIB::OGT_F32] = ISD::SETGT;
171 CCs[RTLIB::OGT_F64] = ISD::SETGT;
172 CCs[RTLIB::UO_F32] = ISD::SETNE;
173 CCs[RTLIB::UO_F64] = ISD::SETNE;
174 CCs[RTLIB::O_F32] = ISD::SETEQ;
175 CCs[RTLIB::O_F64] = ISD::SETEQ;
Evan Cheng56966222007-01-12 02:11:51 +0000176}
177
Chris Lattner310968c2005-01-07 07:44:53 +0000178TargetLowering::TargetLowering(TargetMachine &tm)
Chris Lattner3e6e8cc2006-01-29 08:41:12 +0000179 : TM(tm), TD(TM.getTargetData()) {
Mon P Wang63307c32008-05-05 19:05:59 +0000180 assert(ISD::BUILTIN_OP_END <= OpActionsCapacity &&
Chris Lattner310968c2005-01-07 07:44:53 +0000181 "Fixed size array in TargetLowering is not large enough!");
Chris Lattnercba82f92005-01-16 07:28:11 +0000182 // All operations default to being supported.
183 memset(OpActions, 0, sizeof(OpActions));
Evan Chengc5484282006-10-04 00:56:09 +0000184 memset(LoadXActions, 0, sizeof(LoadXActions));
Chris Lattnerddf89562008-01-17 19:59:44 +0000185 memset(TruncStoreActions, 0, sizeof(TruncStoreActions));
Chris Lattnerc9133f92008-01-18 19:36:20 +0000186 memset(IndexedModeActions, 0, sizeof(IndexedModeActions));
187 memset(ConvertActions, 0, sizeof(ConvertActions));
Dan Gohman93f81e22007-07-09 20:49:44 +0000188
Chris Lattner1a3048b2007-12-22 20:47:56 +0000189 // Set default actions for various operations.
Evan Cheng5ff839f2006-11-09 18:56:43 +0000190 for (unsigned VT = 0; VT != (unsigned)MVT::LAST_VALUETYPE; ++VT) {
Chris Lattner1a3048b2007-12-22 20:47:56 +0000191 // Default all indexed load / store to expand.
Evan Cheng5ff839f2006-11-09 18:56:43 +0000192 for (unsigned IM = (unsigned)ISD::PRE_INC;
193 IM != (unsigned)ISD::LAST_INDEXED_MODE; ++IM) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000194 setIndexedLoadAction(IM, (MVT::SimpleValueType)VT, Expand);
195 setIndexedStoreAction(IM, (MVT::SimpleValueType)VT, Expand);
Evan Cheng5ff839f2006-11-09 18:56:43 +0000196 }
Chris Lattner1a3048b2007-12-22 20:47:56 +0000197
198 // These operations default to expand.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000199 setOperationAction(ISD::FGETSIGN, (MVT::SimpleValueType)VT, Expand);
Evan Cheng5ff839f2006-11-09 18:56:43 +0000200 }
Evan Chengd2cde682008-03-10 19:38:10 +0000201
202 // Most targets ignore the @llvm.prefetch intrinsic.
203 setOperationAction(ISD::PREFETCH, MVT::Other, Expand);
Nate Begemane1795842008-02-14 08:57:00 +0000204
205 // ConstantFP nodes default to expand. Targets can either change this to
206 // Legal, in which case all fp constants are legal, or use addLegalFPImmediate
207 // to optimize expansions for certain constants.
208 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
209 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
210 setOperationAction(ISD::ConstantFP, MVT::f80, Expand);
Chris Lattner310968c2005-01-07 07:44:53 +0000211
Chris Lattner41bab0b2008-01-15 21:58:08 +0000212 // Default ISD::TRAP to expand (which turns it into abort).
213 setOperationAction(ISD::TRAP, MVT::Other, Expand);
214
Owen Andersona69571c2006-05-03 01:29:57 +0000215 IsLittleEndian = TD->isLittleEndian();
Chris Lattnercf9668f2006-10-06 22:52:08 +0000216 UsesGlobalOffsetTable = false;
Scott Michel5b8f82e2008-03-10 15:42:14 +0000217 ShiftAmountTy = PointerTy = getValueType(TD->getIntPtrType());
Chris Lattnerd6e49672005-01-19 03:36:14 +0000218 ShiftAmtHandling = Undefined;
Chris Lattner310968c2005-01-07 07:44:53 +0000219 memset(RegClassForVT, 0,MVT::LAST_VALUETYPE*sizeof(TargetRegisterClass*));
Owen Anderson718cb662007-09-07 04:06:50 +0000220 memset(TargetDAGCombineArray, 0, array_lengthof(TargetDAGCombineArray));
Evan Chenga03a5dc2006-02-14 08:38:30 +0000221 maxStoresPerMemset = maxStoresPerMemcpy = maxStoresPerMemmove = 8;
Reid Spencer0f9beca2005-08-27 19:09:02 +0000222 allowUnalignedMemoryAccesses = false;
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000223 UseUnderscoreSetJmp = false;
224 UseUnderscoreLongJmp = false;
Chris Lattner66180392007-02-25 01:28:05 +0000225 SelectIsExpensive = false;
Nate Begeman405e3ec2005-10-21 00:02:42 +0000226 IntDivIsCheap = false;
227 Pow2DivIsCheap = false;
Chris Lattneree4a7652006-01-25 18:57:15 +0000228 StackPointerRegisterToSaveRestore = 0;
Jim Laskey9bb3c932007-02-22 18:04:49 +0000229 ExceptionPointerRegister = 0;
230 ExceptionSelectorRegister = 0;
Chris Lattnerdfe89342007-09-21 17:06:39 +0000231 SetCCResultContents = UndefinedSetCCResult;
Evan Cheng0577a222006-01-25 18:52:42 +0000232 SchedPreferenceInfo = SchedulingForLatency;
Chris Lattner7acf5f32006-09-05 17:39:15 +0000233 JumpBufSize = 0;
Duraid Madina0c9e0ff2006-09-04 07:44:11 +0000234 JumpBufAlignment = 0;
Evan Chengd60483e2007-05-16 23:45:53 +0000235 IfCvtBlockSizeLimit = 2;
Evan Chengfb8075d2008-02-28 00:43:03 +0000236 IfCvtDupBlockSizeLimit = 0;
237 PrefLoopAlignment = 0;
Evan Cheng56966222007-01-12 02:11:51 +0000238
239 InitLibcallNames(LibcallRoutineNames);
Evan Chengd385fd62007-01-31 09:29:11 +0000240 InitCmpLibcallCCs(CmpLibcallCCs);
Dan Gohmanc3b0b5c2007-09-25 15:10:49 +0000241
242 // Tell Legalize whether the assembler supports DEBUG_LOC.
243 if (!TM.getTargetAsmInfo()->hasDotLocAndDotFile())
244 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Chris Lattner310968c2005-01-07 07:44:53 +0000245}
246
Chris Lattnercba82f92005-01-16 07:28:11 +0000247TargetLowering::~TargetLowering() {}
248
Chris Lattner310968c2005-01-07 07:44:53 +0000249/// computeRegisterProperties - Once all of the register classes are added,
250/// this allows us to compute derived properties we expose.
251void TargetLowering::computeRegisterProperties() {
Nate Begeman6a648612005-11-29 05:45:29 +0000252 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattnerbb97d812005-01-16 01:10:58 +0000253 "Too many value types for ValueTypeActions to hold!");
254
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000255 // Everything defaults to needing one register.
256 for (unsigned i = 0; i != MVT::LAST_VALUETYPE; ++i) {
Dan Gohmanb9f10192007-06-21 14:42:22 +0000257 NumRegistersForVT[i] = 1;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000258 RegisterTypeForVT[i] = TransformToType[i] = (MVT::SimpleValueType)i;
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000259 }
260 // ...except isVoid, which doesn't need any registers.
261 NumRegistersForVT[MVT::isVoid] = 0;
Misha Brukmanf976c852005-04-21 22:55:34 +0000262
Chris Lattner310968c2005-01-07 07:44:53 +0000263 // Find the largest integer register class.
Duncan Sands89307632008-06-09 15:48:25 +0000264 unsigned LargestIntReg = MVT::LAST_INTEGER_VALUETYPE;
Chris Lattner310968c2005-01-07 07:44:53 +0000265 for (; RegClassForVT[LargestIntReg] == 0; --LargestIntReg)
266 assert(LargestIntReg != MVT::i1 && "No integer registers defined!");
267
268 // Every integer value type larger than this largest register takes twice as
269 // many registers to represent as the previous ValueType.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000270 for (unsigned ExpandedReg = LargestIntReg + 1; ; ++ExpandedReg) {
271 MVT EVT = (MVT::SimpleValueType)ExpandedReg;
272 if (!EVT.isInteger())
273 break;
Dan Gohmanb9f10192007-06-21 14:42:22 +0000274 NumRegistersForVT[ExpandedReg] = 2*NumRegistersForVT[ExpandedReg-1];
Duncan Sands83ec4b62008-06-06 12:08:01 +0000275 RegisterTypeForVT[ExpandedReg] = (MVT::SimpleValueType)LargestIntReg;
276 TransformToType[ExpandedReg] = (MVT::SimpleValueType)(ExpandedReg - 1);
277 ValueTypeActions.setTypeAction(EVT, Expand);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +0000278 }
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000279
280 // Inspect all of the ValueType's smaller than the largest integer
281 // register to see which ones need promotion.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000282 unsigned LegalIntReg = LargestIntReg;
283 for (unsigned IntReg = LargestIntReg - 1;
284 IntReg >= (unsigned)MVT::i1; --IntReg) {
285 MVT IVT = (MVT::SimpleValueType)IntReg;
286 if (isTypeLegal(IVT)) {
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000287 LegalIntReg = IntReg;
288 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000289 RegisterTypeForVT[IntReg] = TransformToType[IntReg] =
290 (MVT::SimpleValueType)LegalIntReg;
291 ValueTypeActions.setTypeAction(IVT, Promote);
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000292 }
293 }
294
Dale Johannesen161e8972007-10-05 20:04:43 +0000295 // ppcf128 type is really two f64's.
296 if (!isTypeLegal(MVT::ppcf128)) {
297 NumRegistersForVT[MVT::ppcf128] = 2*NumRegistersForVT[MVT::f64];
298 RegisterTypeForVT[MVT::ppcf128] = MVT::f64;
299 TransformToType[MVT::ppcf128] = MVT::f64;
300 ValueTypeActions.setTypeAction(MVT::ppcf128, Expand);
301 }
302
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000303 // Decide how to handle f64. If the target does not have native f64 support,
304 // expand it to i64 and we will be generating soft float library calls.
305 if (!isTypeLegal(MVT::f64)) {
306 NumRegistersForVT[MVT::f64] = NumRegistersForVT[MVT::i64];
307 RegisterTypeForVT[MVT::f64] = RegisterTypeForVT[MVT::i64];
308 TransformToType[MVT::f64] = MVT::i64;
309 ValueTypeActions.setTypeAction(MVT::f64, Expand);
310 }
311
312 // Decide how to handle f32. If the target does not have native support for
313 // f32, promote it to f64 if it is legal. Otherwise, expand it to i32.
314 if (!isTypeLegal(MVT::f32)) {
315 if (isTypeLegal(MVT::f64)) {
316 NumRegistersForVT[MVT::f32] = NumRegistersForVT[MVT::f64];
317 RegisterTypeForVT[MVT::f32] = RegisterTypeForVT[MVT::f64];
318 TransformToType[MVT::f32] = MVT::f64;
319 ValueTypeActions.setTypeAction(MVT::f32, Promote);
320 } else {
321 NumRegistersForVT[MVT::f32] = NumRegistersForVT[MVT::i32];
322 RegisterTypeForVT[MVT::f32] = RegisterTypeForVT[MVT::i32];
323 TransformToType[MVT::f32] = MVT::i32;
324 ValueTypeActions.setTypeAction(MVT::f32, Expand);
325 }
Evan Cheng1a8f1fe2006-12-09 02:42:38 +0000326 }
Nate Begeman4ef3b812005-11-22 01:29:36 +0000327
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000328 // Loop over all of the vector value types to see which need transformations.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000329 for (unsigned i = MVT::FIRST_VECTOR_VALUETYPE;
330 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
331 MVT VT = (MVT::SimpleValueType)i;
332 if (!isTypeLegal(VT)) {
333 MVT IntermediateVT, RegisterVT;
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000334 unsigned NumIntermediates;
335 NumRegistersForVT[i] =
Duncan Sands83ec4b62008-06-06 12:08:01 +0000336 getVectorTypeBreakdown(VT,
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000337 IntermediateVT, NumIntermediates,
338 RegisterVT);
339 RegisterTypeForVT[i] = RegisterVT;
340 TransformToType[i] = MVT::Other; // this isn't actually used
Duncan Sands83ec4b62008-06-06 12:08:01 +0000341 ValueTypeActions.setTypeAction(VT, Expand);
Dan Gohman7f321562007-06-25 16:23:39 +0000342 }
Chris Lattner3a5935842006-03-16 19:50:01 +0000343 }
Chris Lattnerbb97d812005-01-16 01:10:58 +0000344}
Chris Lattnercba82f92005-01-16 07:28:11 +0000345
Evan Cheng72261582005-12-20 06:22:03 +0000346const char *TargetLowering::getTargetNodeName(unsigned Opcode) const {
347 return NULL;
348}
Evan Cheng3a03ebb2005-12-21 23:05:39 +0000349
Scott Michel5b8f82e2008-03-10 15:42:14 +0000350
Duncan Sands83ec4b62008-06-06 12:08:01 +0000351MVT TargetLowering::getSetCCResultType(const SDOperand &) const {
Scott Michel5b8f82e2008-03-10 15:42:14 +0000352 return getValueType(TD->getIntPtrType());
353}
354
355
Dan Gohman7f321562007-06-25 16:23:39 +0000356/// getVectorTypeBreakdown - Vector types are broken down into some number of
357/// legal first class types. For example, MVT::v8f32 maps to 2 MVT::v4f32
Chris Lattnerdc879292006-03-31 00:28:56 +0000358/// with Altivec or SSE1, or 8 promoted MVT::f64 values with the X86 FP stack.
Dan Gohman7f321562007-06-25 16:23:39 +0000359/// Similarly, MVT::v2i64 turns into 4 MVT::i32 values with both PPC and X86.
Chris Lattnerdc879292006-03-31 00:28:56 +0000360///
Dan Gohman7f321562007-06-25 16:23:39 +0000361/// This method returns the number of registers needed, and the VT for each
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000362/// register. It also returns the VT and quantity of the intermediate values
363/// before they are promoted/expanded.
Chris Lattnerdc879292006-03-31 00:28:56 +0000364///
Duncan Sands83ec4b62008-06-06 12:08:01 +0000365unsigned TargetLowering::getVectorTypeBreakdown(MVT VT,
366 MVT &IntermediateVT,
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000367 unsigned &NumIntermediates,
Duncan Sands83ec4b62008-06-06 12:08:01 +0000368 MVT &RegisterVT) const {
Chris Lattnerdc879292006-03-31 00:28:56 +0000369 // Figure out the right, legal destination reg to copy into.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000370 unsigned NumElts = VT.getVectorNumElements();
371 MVT EltTy = VT.getVectorElementType();
Chris Lattnerdc879292006-03-31 00:28:56 +0000372
373 unsigned NumVectorRegs = 1;
374
Nate Begemand73ab882007-11-27 19:28:48 +0000375 // FIXME: We don't support non-power-of-2-sized vectors for now. Ideally we
376 // could break down into LHS/RHS like LegalizeDAG does.
377 if (!isPowerOf2_32(NumElts)) {
378 NumVectorRegs = NumElts;
379 NumElts = 1;
380 }
381
Chris Lattnerdc879292006-03-31 00:28:56 +0000382 // Divide the input until we get to a supported size. This will always
383 // end with a scalar if the target doesn't support vectors.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000384 while (NumElts > 1 && !isTypeLegal(MVT::getVectorVT(EltTy, NumElts))) {
Chris Lattnerdc879292006-03-31 00:28:56 +0000385 NumElts >>= 1;
386 NumVectorRegs <<= 1;
387 }
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000388
389 NumIntermediates = NumVectorRegs;
Chris Lattnerdc879292006-03-31 00:28:56 +0000390
Duncan Sands83ec4b62008-06-06 12:08:01 +0000391 MVT NewVT = MVT::getVectorVT(EltTy, NumElts);
Dan Gohman7f321562007-06-25 16:23:39 +0000392 if (!isTypeLegal(NewVT))
393 NewVT = EltTy;
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000394 IntermediateVT = NewVT;
Chris Lattnerdc879292006-03-31 00:28:56 +0000395
Duncan Sands83ec4b62008-06-06 12:08:01 +0000396 MVT DestVT = getTypeToTransformTo(NewVT);
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000397 RegisterVT = DestVT;
Duncan Sands8e4eb092008-06-08 20:54:56 +0000398 if (DestVT.bitsLT(NewVT)) {
Chris Lattnerdc879292006-03-31 00:28:56 +0000399 // Value is expanded, e.g. i64 -> i16.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000400 return NumVectorRegs*(NewVT.getSizeInBits()/DestVT.getSizeInBits());
Chris Lattnerdc879292006-03-31 00:28:56 +0000401 } else {
402 // Otherwise, promotion or legal types use the same number of registers as
403 // the vector decimated to the appropriate level.
Chris Lattner79227e22006-03-31 00:46:36 +0000404 return NumVectorRegs;
Chris Lattnerdc879292006-03-31 00:28:56 +0000405 }
406
Evan Chenge9b3da12006-05-17 18:10:06 +0000407 return 1;
Chris Lattnerdc879292006-03-31 00:28:56 +0000408}
409
Evan Cheng3ae05432008-01-24 00:22:01 +0000410/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
Dale Johannesen28d08fd2008-02-28 22:31:51 +0000411/// function arguments in the caller parameter area. This is the actual
412/// alignment, not its logarithm.
Evan Cheng3ae05432008-01-24 00:22:01 +0000413unsigned TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Dale Johannesen28d08fd2008-02-28 22:31:51 +0000414 return TD->getCallFrameTypeAlignment(Ty);
Evan Cheng3ae05432008-01-24 00:22:01 +0000415}
416
Evan Chengcc415862007-11-09 01:32:10 +0000417SDOperand TargetLowering::getPICJumpTableRelocBase(SDOperand Table,
418 SelectionDAG &DAG) const {
419 if (usesGlobalOffsetTable())
420 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
421 return Table;
422}
423
Chris Lattnereb8146b2006-02-04 02:13:02 +0000424//===----------------------------------------------------------------------===//
425// Optimization Methods
426//===----------------------------------------------------------------------===//
427
Nate Begeman368e18d2006-02-16 21:11:51 +0000428/// ShrinkDemandedConstant - Check to see if the specified operand of the
429/// specified instruction is a constant integer. If so, check to see if there
430/// are any bits set in the constant that are not demanded. If so, shrink the
431/// constant and return true.
432bool TargetLowering::TargetLoweringOpt::ShrinkDemandedConstant(SDOperand Op,
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000433 const APInt &Demanded) {
Chris Lattnerec665152006-02-26 23:36:02 +0000434 // FIXME: ISD::SELECT, ISD::SELECT_CC
Nate Begeman368e18d2006-02-16 21:11:51 +0000435 switch(Op.getOpcode()) {
436 default: break;
Nate Begemande996292006-02-03 22:24:05 +0000437 case ISD::AND:
Nate Begeman368e18d2006-02-16 21:11:51 +0000438 case ISD::OR:
439 case ISD::XOR:
440 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000441 if (C->getAPIntValue().intersects(~Demanded)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000442 MVT VT = Op.getValueType();
Nate Begeman368e18d2006-02-16 21:11:51 +0000443 SDOperand New = DAG.getNode(Op.getOpcode(), VT, Op.getOperand(0),
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000444 DAG.getConstant(Demanded &
445 C->getAPIntValue(),
Nate Begeman368e18d2006-02-16 21:11:51 +0000446 VT));
447 return CombineTo(Op, New);
Nate Begemande996292006-02-03 22:24:05 +0000448 }
Nate Begemande996292006-02-03 22:24:05 +0000449 break;
450 }
451 return false;
452}
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000453
Nate Begeman368e18d2006-02-16 21:11:51 +0000454/// SimplifyDemandedBits - Look at Op. At this point, we know that only the
455/// DemandedMask bits of the result of Op are ever used downstream. If we can
456/// use this information to simplify Op, create a new simplified DAG node and
457/// return true, returning the original and new nodes in Old and New. Otherwise,
458/// analyze the expression and return a mask of KnownOne and KnownZero bits for
459/// the expression (used to simplify the caller). The KnownZero/One bits may
460/// only be accurate for those bits in the DemandedMask.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000461bool TargetLowering::SimplifyDemandedBits(SDOperand Op,
462 const APInt &DemandedMask,
463 APInt &KnownZero,
464 APInt &KnownOne,
Nate Begeman368e18d2006-02-16 21:11:51 +0000465 TargetLoweringOpt &TLO,
466 unsigned Depth) const {
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000467 unsigned BitWidth = DemandedMask.getBitWidth();
468 assert(Op.getValueSizeInBits() == BitWidth &&
469 "Mask size mismatches value type size!");
470 APInt NewMask = DemandedMask;
Chris Lattner3fc5b012007-05-17 18:19:23 +0000471
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000472 // Don't know anything.
473 KnownZero = KnownOne = APInt(BitWidth, 0);
474
Nate Begeman368e18d2006-02-16 21:11:51 +0000475 // Other users may use these bits.
476 if (!Op.Val->hasOneUse()) {
477 if (Depth != 0) {
478 // If not at the root, Just compute the KnownZero/KnownOne bits to
479 // simplify things downstream.
Dan Gohmanea859be2007-06-22 14:59:07 +0000480 TLO.DAG.ComputeMaskedBits(Op, DemandedMask, KnownZero, KnownOne, Depth);
Nate Begeman368e18d2006-02-16 21:11:51 +0000481 return false;
482 }
483 // If this is the root being simplified, allow it to have multiple uses,
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000484 // just set the NewMask to all bits.
485 NewMask = APInt::getAllOnesValue(BitWidth);
Nate Begeman368e18d2006-02-16 21:11:51 +0000486 } else if (DemandedMask == 0) {
487 // Not demanding any bits from Op.
488 if (Op.getOpcode() != ISD::UNDEF)
489 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::UNDEF, Op.getValueType()));
490 return false;
491 } else if (Depth == 6) { // Limit search depth.
492 return false;
493 }
494
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000495 APInt KnownZero2, KnownOne2, KnownZeroOut, KnownOneOut;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000496 switch (Op.getOpcode()) {
497 case ISD::Constant:
Nate Begeman368e18d2006-02-16 21:11:51 +0000498 // We know all of the bits for a constant!
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000499 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & NewMask;
500 KnownZero = ~KnownOne & NewMask;
Chris Lattnerec665152006-02-26 23:36:02 +0000501 return false; // Don't fall through, will infinitely loop.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000502 case ISD::AND:
Chris Lattner81cd3552006-02-27 00:36:27 +0000503 // If the RHS is a constant, check to see if the LHS would be zero without
504 // using the bits from the RHS. Below, we use knowledge about the RHS to
505 // simplify the LHS, here we're using information from the LHS to simplify
506 // the RHS.
507 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000508 APInt LHSZero, LHSOne;
509 TLO.DAG.ComputeMaskedBits(Op.getOperand(0), NewMask,
Dan Gohmanea859be2007-06-22 14:59:07 +0000510 LHSZero, LHSOne, Depth+1);
Chris Lattner81cd3552006-02-27 00:36:27 +0000511 // If the LHS already has zeros where RHSC does, this and is dead.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000512 if ((LHSZero & NewMask) == (~RHSC->getAPIntValue() & NewMask))
Chris Lattner81cd3552006-02-27 00:36:27 +0000513 return TLO.CombineTo(Op, Op.getOperand(0));
514 // If any of the set bits in the RHS are known zero on the LHS, shrink
515 // the constant.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000516 if (TLO.ShrinkDemandedConstant(Op, ~LHSZero & NewMask))
Chris Lattner81cd3552006-02-27 00:36:27 +0000517 return true;
518 }
519
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000520 if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero,
Nate Begeman368e18d2006-02-16 21:11:51 +0000521 KnownOne, TLO, Depth+1))
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000522 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000523 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000524 if (SimplifyDemandedBits(Op.getOperand(0), ~KnownZero & NewMask,
Nate Begeman368e18d2006-02-16 21:11:51 +0000525 KnownZero2, KnownOne2, TLO, Depth+1))
526 return true;
527 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
528
529 // If all of the demanded bits are known one on one side, return the other.
530 // These bits cannot contribute to the result of the 'and'.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000531 if ((NewMask & ~KnownZero2 & KnownOne) == (~KnownZero2 & NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +0000532 return TLO.CombineTo(Op, Op.getOperand(0));
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000533 if ((NewMask & ~KnownZero & KnownOne2) == (~KnownZero & NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +0000534 return TLO.CombineTo(Op, Op.getOperand(1));
535 // If all of the demanded bits in the inputs are known zeros, return zero.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000536 if ((NewMask & (KnownZero|KnownZero2)) == NewMask)
Nate Begeman368e18d2006-02-16 21:11:51 +0000537 return TLO.CombineTo(Op, TLO.DAG.getConstant(0, Op.getValueType()));
538 // If the RHS is a constant, see if we can simplify it.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000539 if (TLO.ShrinkDemandedConstant(Op, ~KnownZero2 & NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +0000540 return true;
Chris Lattner5f0c6582006-02-27 00:22:28 +0000541
Nate Begeman368e18d2006-02-16 21:11:51 +0000542 // Output known-1 bits are only known if set in both the LHS & RHS.
543 KnownOne &= KnownOne2;
544 // Output known-0 are known to be clear if zero in either the LHS | RHS.
545 KnownZero |= KnownZero2;
546 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000547 case ISD::OR:
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000548 if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero,
Nate Begeman368e18d2006-02-16 21:11:51 +0000549 KnownOne, TLO, Depth+1))
550 return true;
551 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000552 if (SimplifyDemandedBits(Op.getOperand(0), ~KnownOne & NewMask,
Nate Begeman368e18d2006-02-16 21:11:51 +0000553 KnownZero2, KnownOne2, TLO, Depth+1))
554 return true;
555 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
556
557 // If all of the demanded bits are known zero on one side, return the other.
558 // These bits cannot contribute to the result of the 'or'.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000559 if ((NewMask & ~KnownOne2 & KnownZero) == (~KnownOne2 & NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +0000560 return TLO.CombineTo(Op, Op.getOperand(0));
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000561 if ((NewMask & ~KnownOne & KnownZero2) == (~KnownOne & NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +0000562 return TLO.CombineTo(Op, Op.getOperand(1));
563 // If all of the potentially set bits on one side are known to be set on
564 // the other side, just use the 'other' side.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000565 if ((NewMask & ~KnownZero & KnownOne2) == (~KnownZero & NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +0000566 return TLO.CombineTo(Op, Op.getOperand(0));
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000567 if ((NewMask & ~KnownZero2 & KnownOne) == (~KnownZero2 & NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +0000568 return TLO.CombineTo(Op, Op.getOperand(1));
569 // If the RHS is a constant, see if we can simplify it.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000570 if (TLO.ShrinkDemandedConstant(Op, NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +0000571 return true;
572
573 // Output known-0 bits are only known if clear in both the LHS & RHS.
574 KnownZero &= KnownZero2;
575 // Output known-1 are known to be set if set in either the LHS | RHS.
576 KnownOne |= KnownOne2;
577 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000578 case ISD::XOR:
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000579 if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero,
Nate Begeman368e18d2006-02-16 21:11:51 +0000580 KnownOne, TLO, Depth+1))
581 return true;
582 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000583 if (SimplifyDemandedBits(Op.getOperand(0), NewMask, KnownZero2,
Nate Begeman368e18d2006-02-16 21:11:51 +0000584 KnownOne2, TLO, Depth+1))
585 return true;
586 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
587
588 // If all of the demanded bits are known zero on one side, return the other.
589 // These bits cannot contribute to the result of the 'xor'.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000590 if ((KnownZero & NewMask) == NewMask)
Nate Begeman368e18d2006-02-16 21:11:51 +0000591 return TLO.CombineTo(Op, Op.getOperand(0));
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000592 if ((KnownZero2 & NewMask) == NewMask)
Nate Begeman368e18d2006-02-16 21:11:51 +0000593 return TLO.CombineTo(Op, Op.getOperand(1));
Chris Lattner3687c1a2006-11-27 21:50:02 +0000594
595 // If all of the unknown bits are known to be zero on one side or the other
596 // (but not both) turn this into an *inclusive* or.
597 // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000598 if ((NewMask & ~KnownZero & ~KnownZero2) == 0)
Chris Lattner3687c1a2006-11-27 21:50:02 +0000599 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::OR, Op.getValueType(),
600 Op.getOperand(0),
601 Op.getOperand(1)));
Nate Begeman368e18d2006-02-16 21:11:51 +0000602
603 // Output known-0 bits are known if clear or set in both the LHS & RHS.
604 KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
605 // Output known-1 are known to be set if set in only one of the LHS, RHS.
606 KnownOneOut = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
607
Nate Begeman368e18d2006-02-16 21:11:51 +0000608 // If all of the demanded bits on one side are known, and all of the set
609 // bits on that side are also known to be set on the other side, turn this
610 // into an AND, as we know the bits will be cleared.
611 // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000612 if ((NewMask & (KnownZero|KnownOne)) == NewMask) { // all known
Nate Begeman368e18d2006-02-16 21:11:51 +0000613 if ((KnownOne & KnownOne2) == KnownOne) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000614 MVT VT = Op.getValueType();
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000615 SDOperand ANDC = TLO.DAG.getConstant(~KnownOne & NewMask, VT);
Nate Begeman368e18d2006-02-16 21:11:51 +0000616 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::AND, VT, Op.getOperand(0),
617 ANDC));
618 }
619 }
620
621 // If the RHS is a constant, see if we can simplify it.
Torok Edwin4fea2e92008-04-06 21:23:02 +0000622 // for XOR, we prefer to force bits to 1 if they will make a -1.
623 // if we can't force bits, try to shrink constant
624 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
625 APInt Expanded = C->getAPIntValue() | (~NewMask);
626 // if we can expand it to have all bits set, do it
627 if (Expanded.isAllOnesValue()) {
628 if (Expanded != C->getAPIntValue()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000629 MVT VT = Op.getValueType();
Torok Edwin4fea2e92008-04-06 21:23:02 +0000630 SDOperand New = TLO.DAG.getNode(Op.getOpcode(), VT, Op.getOperand(0),
631 TLO.DAG.getConstant(Expanded, VT));
632 return TLO.CombineTo(Op, New);
633 }
634 // if it already has all the bits set, nothing to change
635 // but don't shrink either!
636 } else if (TLO.ShrinkDemandedConstant(Op, NewMask)) {
637 return true;
638 }
639 }
640
Nate Begeman368e18d2006-02-16 21:11:51 +0000641 KnownZero = KnownZeroOut;
642 KnownOne = KnownOneOut;
643 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000644 case ISD::SELECT:
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000645 if (SimplifyDemandedBits(Op.getOperand(2), NewMask, KnownZero,
Nate Begeman368e18d2006-02-16 21:11:51 +0000646 KnownOne, TLO, Depth+1))
647 return true;
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000648 if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero2,
Nate Begeman368e18d2006-02-16 21:11:51 +0000649 KnownOne2, TLO, Depth+1))
650 return true;
651 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
652 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
653
654 // If the operands are constants, see if we can simplify them.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000655 if (TLO.ShrinkDemandedConstant(Op, NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +0000656 return true;
657
658 // Only known if known in both the LHS and RHS.
659 KnownOne &= KnownOne2;
660 KnownZero &= KnownZero2;
661 break;
Chris Lattnerec665152006-02-26 23:36:02 +0000662 case ISD::SELECT_CC:
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000663 if (SimplifyDemandedBits(Op.getOperand(3), NewMask, KnownZero,
Chris Lattnerec665152006-02-26 23:36:02 +0000664 KnownOne, TLO, Depth+1))
665 return true;
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000666 if (SimplifyDemandedBits(Op.getOperand(2), NewMask, KnownZero2,
Chris Lattnerec665152006-02-26 23:36:02 +0000667 KnownOne2, TLO, Depth+1))
668 return true;
669 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
670 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
671
672 // If the operands are constants, see if we can simplify them.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000673 if (TLO.ShrinkDemandedConstant(Op, NewMask))
Chris Lattnerec665152006-02-26 23:36:02 +0000674 return true;
675
676 // Only known if known in both the LHS and RHS.
677 KnownOne &= KnownOne2;
678 KnownZero &= KnownZero2;
679 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000680 case ISD::SHL:
Nate Begeman368e18d2006-02-16 21:11:51 +0000681 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner895c4ab2007-04-17 21:14:16 +0000682 unsigned ShAmt = SA->getValue();
683 SDOperand InOp = Op.getOperand(0);
684
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000685 // If the shift count is an invalid immediate, don't do anything.
686 if (ShAmt >= BitWidth)
687 break;
688
Chris Lattner895c4ab2007-04-17 21:14:16 +0000689 // If this is ((X >>u C1) << ShAmt), see if we can simplify this into a
690 // single shift. We can do this if the bottom bits (which are shifted
691 // out) are never demanded.
692 if (InOp.getOpcode() == ISD::SRL &&
693 isa<ConstantSDNode>(InOp.getOperand(1))) {
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000694 if (ShAmt && (NewMask & APInt::getLowBitsSet(BitWidth, ShAmt)) == 0) {
Chris Lattner895c4ab2007-04-17 21:14:16 +0000695 unsigned C1 = cast<ConstantSDNode>(InOp.getOperand(1))->getValue();
696 unsigned Opc = ISD::SHL;
697 int Diff = ShAmt-C1;
698 if (Diff < 0) {
699 Diff = -Diff;
700 Opc = ISD::SRL;
701 }
702
703 SDOperand NewSA =
Chris Lattner4e7e6cd2007-05-30 16:30:06 +0000704 TLO.DAG.getConstant(Diff, Op.getOperand(1).getValueType());
Duncan Sands83ec4b62008-06-06 12:08:01 +0000705 MVT VT = Op.getValueType();
Chris Lattner0a16a1f2007-04-18 03:01:40 +0000706 return TLO.CombineTo(Op, TLO.DAG.getNode(Opc, VT,
Chris Lattner895c4ab2007-04-17 21:14:16 +0000707 InOp.getOperand(0), NewSA));
708 }
709 }
710
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000711 if (SimplifyDemandedBits(Op.getOperand(0), NewMask.lshr(ShAmt),
Nate Begeman368e18d2006-02-16 21:11:51 +0000712 KnownZero, KnownOne, TLO, Depth+1))
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000713 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000714 KnownZero <<= SA->getValue();
715 KnownOne <<= SA->getValue();
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000716 // low bits known zero.
717 KnownZero |= APInt::getLowBitsSet(BitWidth, SA->getValue());
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000718 }
719 break;
Nate Begeman368e18d2006-02-16 21:11:51 +0000720 case ISD::SRL:
721 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000722 MVT VT = Op.getValueType();
Nate Begeman368e18d2006-02-16 21:11:51 +0000723 unsigned ShAmt = SA->getValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000724 unsigned VTSize = VT.getSizeInBits();
Chris Lattner895c4ab2007-04-17 21:14:16 +0000725 SDOperand InOp = Op.getOperand(0);
726
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000727 // If the shift count is an invalid immediate, don't do anything.
728 if (ShAmt >= BitWidth)
729 break;
730
Chris Lattner895c4ab2007-04-17 21:14:16 +0000731 // If this is ((X << C1) >>u ShAmt), see if we can simplify this into a
732 // single shift. We can do this if the top bits (which are shifted out)
733 // are never demanded.
734 if (InOp.getOpcode() == ISD::SHL &&
735 isa<ConstantSDNode>(InOp.getOperand(1))) {
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000736 if (ShAmt && (NewMask & APInt::getHighBitsSet(VTSize, ShAmt)) == 0) {
Chris Lattner895c4ab2007-04-17 21:14:16 +0000737 unsigned C1 = cast<ConstantSDNode>(InOp.getOperand(1))->getValue();
738 unsigned Opc = ISD::SRL;
739 int Diff = ShAmt-C1;
740 if (Diff < 0) {
741 Diff = -Diff;
742 Opc = ISD::SHL;
743 }
744
745 SDOperand NewSA =
Chris Lattner8c7d2d52007-04-17 22:53:02 +0000746 TLO.DAG.getConstant(Diff, Op.getOperand(1).getValueType());
Chris Lattner895c4ab2007-04-17 21:14:16 +0000747 return TLO.CombineTo(Op, TLO.DAG.getNode(Opc, VT,
748 InOp.getOperand(0), NewSA));
749 }
750 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000751
752 // Compute the new bits that are at the top now.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000753 if (SimplifyDemandedBits(InOp, (NewMask << ShAmt),
Nate Begeman368e18d2006-02-16 21:11:51 +0000754 KnownZero, KnownOne, TLO, Depth+1))
755 return true;
756 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000757 KnownZero = KnownZero.lshr(ShAmt);
758 KnownOne = KnownOne.lshr(ShAmt);
Chris Lattnerc4fa6032006-06-13 16:52:37 +0000759
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000760 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Chris Lattnerc4fa6032006-06-13 16:52:37 +0000761 KnownZero |= HighBits; // High bits known zero.
Nate Begeman368e18d2006-02-16 21:11:51 +0000762 }
763 break;
764 case ISD::SRA:
765 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000766 MVT VT = Op.getValueType();
Nate Begeman368e18d2006-02-16 21:11:51 +0000767 unsigned ShAmt = SA->getValue();
768
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000769 // If the shift count is an invalid immediate, don't do anything.
770 if (ShAmt >= BitWidth)
771 break;
772
773 APInt InDemandedMask = (NewMask << ShAmt);
Chris Lattner1b737132006-05-08 17:22:53 +0000774
775 // If any of the demanded bits are produced by the sign extension, we also
776 // demand the input sign bit.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000777 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
778 if (HighBits.intersects(NewMask))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000779 InDemandedMask |= APInt::getSignBit(VT.getSizeInBits());
Chris Lattner1b737132006-05-08 17:22:53 +0000780
781 if (SimplifyDemandedBits(Op.getOperand(0), InDemandedMask,
Nate Begeman368e18d2006-02-16 21:11:51 +0000782 KnownZero, KnownOne, TLO, Depth+1))
783 return true;
784 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000785 KnownZero = KnownZero.lshr(ShAmt);
786 KnownOne = KnownOne.lshr(ShAmt);
Nate Begeman368e18d2006-02-16 21:11:51 +0000787
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000788 // Handle the sign bit, adjusted to where it is now in the mask.
789 APInt SignBit = APInt::getSignBit(BitWidth).lshr(ShAmt);
Nate Begeman368e18d2006-02-16 21:11:51 +0000790
791 // If the input sign bit is known to be zero, or if none of the top bits
792 // are demanded, turn this into an unsigned shift right.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000793 if (KnownZero.intersects(SignBit) || (HighBits & ~NewMask) == HighBits) {
Nate Begeman368e18d2006-02-16 21:11:51 +0000794 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, VT, Op.getOperand(0),
795 Op.getOperand(1)));
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000796 } else if (KnownOne.intersects(SignBit)) { // New bits are known one.
Nate Begeman368e18d2006-02-16 21:11:51 +0000797 KnownOne |= HighBits;
798 }
799 }
800 break;
801 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000802 MVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Nate Begeman368e18d2006-02-16 21:11:51 +0000803
Chris Lattnerec665152006-02-26 23:36:02 +0000804 // Sign extension. Compute the demanded bits in the result that are not
Nate Begeman368e18d2006-02-16 21:11:51 +0000805 // present in the input.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000806 APInt NewBits = APInt::getHighBitsSet(BitWidth,
Duncan Sands83ec4b62008-06-06 12:08:01 +0000807 BitWidth - EVT.getSizeInBits()) &
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000808 NewMask;
Nate Begeman368e18d2006-02-16 21:11:51 +0000809
Chris Lattnerec665152006-02-26 23:36:02 +0000810 // If none of the extended bits are demanded, eliminate the sextinreg.
811 if (NewBits == 0)
812 return TLO.CombineTo(Op, Op.getOperand(0));
813
Duncan Sands83ec4b62008-06-06 12:08:01 +0000814 APInt InSignBit = APInt::getSignBit(EVT.getSizeInBits());
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000815 InSignBit.zext(BitWidth);
816 APInt InputDemandedBits = APInt::getLowBitsSet(BitWidth,
Duncan Sands83ec4b62008-06-06 12:08:01 +0000817 EVT.getSizeInBits()) &
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000818 NewMask;
Nate Begeman368e18d2006-02-16 21:11:51 +0000819
Chris Lattnerec665152006-02-26 23:36:02 +0000820 // Since the sign extended bits are demanded, we know that the sign
Nate Begeman368e18d2006-02-16 21:11:51 +0000821 // bit is demanded.
Chris Lattnerec665152006-02-26 23:36:02 +0000822 InputDemandedBits |= InSignBit;
Nate Begeman368e18d2006-02-16 21:11:51 +0000823
824 if (SimplifyDemandedBits(Op.getOperand(0), InputDemandedBits,
825 KnownZero, KnownOne, TLO, Depth+1))
826 return true;
827 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
828
829 // If the sign bit of the input is known set or clear, then we know the
830 // top bits of the result.
831
Chris Lattnerec665152006-02-26 23:36:02 +0000832 // If the input sign bit is known zero, convert this into a zero extension.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000833 if (KnownZero.intersects(InSignBit))
Chris Lattnerec665152006-02-26 23:36:02 +0000834 return TLO.CombineTo(Op,
835 TLO.DAG.getZeroExtendInReg(Op.getOperand(0), EVT));
836
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000837 if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Nate Begeman368e18d2006-02-16 21:11:51 +0000838 KnownOne |= NewBits;
839 KnownZero &= ~NewBits;
Chris Lattnerec665152006-02-26 23:36:02 +0000840 } else { // Input sign bit unknown
Nate Begeman368e18d2006-02-16 21:11:51 +0000841 KnownZero &= ~NewBits;
842 KnownOne &= ~NewBits;
843 }
844 break;
845 }
Chris Lattnerec665152006-02-26 23:36:02 +0000846 case ISD::ZERO_EXTEND: {
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000847 unsigned OperandBitWidth = Op.getOperand(0).getValueSizeInBits();
848 APInt InMask = NewMask;
849 InMask.trunc(OperandBitWidth);
Chris Lattnerec665152006-02-26 23:36:02 +0000850
851 // If none of the top bits are demanded, convert this into an any_extend.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000852 APInt NewBits =
853 APInt::getHighBitsSet(BitWidth, BitWidth - OperandBitWidth) & NewMask;
854 if (!NewBits.intersects(NewMask))
Chris Lattnerec665152006-02-26 23:36:02 +0000855 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ANY_EXTEND,
856 Op.getValueType(),
857 Op.getOperand(0)));
858
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000859 if (SimplifyDemandedBits(Op.getOperand(0), InMask,
Chris Lattnerec665152006-02-26 23:36:02 +0000860 KnownZero, KnownOne, TLO, Depth+1))
861 return true;
862 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000863 KnownZero.zext(BitWidth);
864 KnownOne.zext(BitWidth);
Chris Lattnerec665152006-02-26 23:36:02 +0000865 KnownZero |= NewBits;
866 break;
867 }
868 case ISD::SIGN_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000869 MVT InVT = Op.getOperand(0).getValueType();
870 unsigned InBits = InVT.getSizeInBits();
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000871 APInt InMask = APInt::getLowBitsSet(BitWidth, InBits);
Dan Gohman97360282008-03-11 21:29:43 +0000872 APInt InSignBit = APInt::getBitsSet(BitWidth, InBits - 1, InBits);
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000873 APInt NewBits = ~InMask & NewMask;
Chris Lattnerec665152006-02-26 23:36:02 +0000874
875 // If none of the top bits are demanded, convert this into an any_extend.
876 if (NewBits == 0)
Chris Lattnerfea997a2007-02-01 04:55:59 +0000877 return TLO.CombineTo(Op,TLO.DAG.getNode(ISD::ANY_EXTEND,Op.getValueType(),
Chris Lattnerec665152006-02-26 23:36:02 +0000878 Op.getOperand(0)));
879
880 // Since some of the sign extended bits are demanded, we know that the sign
881 // bit is demanded.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000882 APInt InDemandedBits = InMask & NewMask;
Chris Lattnerec665152006-02-26 23:36:02 +0000883 InDemandedBits |= InSignBit;
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000884 InDemandedBits.trunc(InBits);
Chris Lattnerec665152006-02-26 23:36:02 +0000885
886 if (SimplifyDemandedBits(Op.getOperand(0), InDemandedBits, KnownZero,
887 KnownOne, TLO, Depth+1))
888 return true;
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000889 KnownZero.zext(BitWidth);
890 KnownOne.zext(BitWidth);
Chris Lattnerec665152006-02-26 23:36:02 +0000891
892 // If the sign bit is known zero, convert this to a zero extend.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000893 if (KnownZero.intersects(InSignBit))
Chris Lattnerec665152006-02-26 23:36:02 +0000894 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ZERO_EXTEND,
895 Op.getValueType(),
896 Op.getOperand(0)));
897
898 // If the sign bit is known one, the top bits match.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000899 if (KnownOne.intersects(InSignBit)) {
Chris Lattnerec665152006-02-26 23:36:02 +0000900 KnownOne |= NewBits;
901 KnownZero &= ~NewBits;
902 } else { // Otherwise, top bits aren't known.
903 KnownOne &= ~NewBits;
904 KnownZero &= ~NewBits;
905 }
906 break;
907 }
908 case ISD::ANY_EXTEND: {
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000909 unsigned OperandBitWidth = Op.getOperand(0).getValueSizeInBits();
910 APInt InMask = NewMask;
911 InMask.trunc(OperandBitWidth);
912 if (SimplifyDemandedBits(Op.getOperand(0), InMask,
Chris Lattnerec665152006-02-26 23:36:02 +0000913 KnownZero, KnownOne, TLO, Depth+1))
914 return true;
915 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000916 KnownZero.zext(BitWidth);
917 KnownOne.zext(BitWidth);
Chris Lattnerec665152006-02-26 23:36:02 +0000918 break;
919 }
Chris Lattnerfe8babf2006-05-05 22:32:12 +0000920 case ISD::TRUNCATE: {
Chris Lattnerc93dfda2006-05-06 00:11:52 +0000921 // Simplify the input, using demanded bit information, and compute the known
922 // zero/one bits live out.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000923 APInt TruncMask = NewMask;
924 TruncMask.zext(Op.getOperand(0).getValueSizeInBits());
925 if (SimplifyDemandedBits(Op.getOperand(0), TruncMask,
Chris Lattnerfe8babf2006-05-05 22:32:12 +0000926 KnownZero, KnownOne, TLO, Depth+1))
927 return true;
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000928 KnownZero.trunc(BitWidth);
929 KnownOne.trunc(BitWidth);
Chris Lattnerc93dfda2006-05-06 00:11:52 +0000930
931 // If the input is only used by this truncate, see if we can shrink it based
932 // on the known demanded bits.
933 if (Op.getOperand(0).Val->hasOneUse()) {
934 SDOperand In = Op.getOperand(0);
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000935 unsigned InBitWidth = In.getValueSizeInBits();
Chris Lattnerc93dfda2006-05-06 00:11:52 +0000936 switch (In.getOpcode()) {
937 default: break;
938 case ISD::SRL:
939 // Shrink SRL by a constant if none of the high bits shifted in are
940 // demanded.
941 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(In.getOperand(1))){
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000942 APInt HighBits = APInt::getHighBitsSet(InBitWidth,
943 InBitWidth - BitWidth);
944 HighBits = HighBits.lshr(ShAmt->getValue());
945 HighBits.trunc(BitWidth);
Chris Lattnerc93dfda2006-05-06 00:11:52 +0000946
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000947 if (ShAmt->getValue() < BitWidth && !(HighBits & NewMask)) {
Chris Lattnerc93dfda2006-05-06 00:11:52 +0000948 // None of the shifted in bits are needed. Add a truncate of the
949 // shift input, then shift it.
950 SDOperand NewTrunc = TLO.DAG.getNode(ISD::TRUNCATE,
951 Op.getValueType(),
952 In.getOperand(0));
953 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL,Op.getValueType(),
954 NewTrunc, In.getOperand(1)));
955 }
956 }
957 break;
958 }
959 }
960
Chris Lattnerfe8babf2006-05-05 22:32:12 +0000961 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Chris Lattnerfe8babf2006-05-05 22:32:12 +0000962 break;
963 }
Chris Lattnerec665152006-02-26 23:36:02 +0000964 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000965 MVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000966 APInt InMask = APInt::getLowBitsSet(BitWidth,
Duncan Sands83ec4b62008-06-06 12:08:01 +0000967 VT.getSizeInBits());
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000968 if (SimplifyDemandedBits(Op.getOperand(0), InMask & NewMask,
Chris Lattnerec665152006-02-26 23:36:02 +0000969 KnownZero, KnownOne, TLO, Depth+1))
970 return true;
971 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000972 KnownZero |= ~InMask & NewMask;
Chris Lattnerec665152006-02-26 23:36:02 +0000973 break;
974 }
Chris Lattner2ceb2cf2007-12-22 21:35:38 +0000975 case ISD::BIT_CONVERT:
976#if 0
977 // If this is an FP->Int bitcast and if the sign bit is the only thing that
978 // is demanded, turn this into a FGETSIGN.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000979 if (NewMask == MVT::getIntegerVTSignBit(Op.getValueType()) &&
Chris Lattner2ceb2cf2007-12-22 21:35:38 +0000980 MVT::isFloatingPoint(Op.getOperand(0).getValueType()) &&
981 !MVT::isVector(Op.getOperand(0).getValueType())) {
982 // Only do this xform if FGETSIGN is valid or if before legalize.
983 if (!TLO.AfterLegalize ||
984 isOperationLegal(ISD::FGETSIGN, Op.getValueType())) {
985 // Make a FGETSIGN + SHL to move the sign bit into the appropriate
986 // place. We expect the SHL to be eliminated by other optimizations.
987 SDOperand Sign = TLO.DAG.getNode(ISD::FGETSIGN, Op.getValueType(),
988 Op.getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +0000989 unsigned ShVal = Op.getValueType().getSizeInBits()-1;
Chris Lattner2ceb2cf2007-12-22 21:35:38 +0000990 SDOperand ShAmt = TLO.DAG.getConstant(ShVal, getShiftAmountTy());
991 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SHL, Op.getValueType(),
992 Sign, ShAmt));
993 }
994 }
995#endif
996 break;
Dan Gohman54eed372008-05-06 00:53:29 +0000997 default:
Chris Lattner1482b5f2006-04-02 06:15:09 +0000998 // Just use ComputeMaskedBits to compute output bits.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000999 TLO.DAG.ComputeMaskedBits(Op, NewMask, KnownZero, KnownOne, Depth);
Chris Lattnera6bc5a42006-02-27 01:00:42 +00001000 break;
Nate Begeman368e18d2006-02-16 21:11:51 +00001001 }
Chris Lattnerec665152006-02-26 23:36:02 +00001002
1003 // If we know the value of all of the demanded bits, return this as a
1004 // constant.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001005 if ((NewMask & (KnownZero|KnownOne)) == NewMask)
Chris Lattnerec665152006-02-26 23:36:02 +00001006 return TLO.CombineTo(Op, TLO.DAG.getConstant(KnownOne, Op.getValueType()));
1007
Nate Begeman368e18d2006-02-16 21:11:51 +00001008 return false;
1009}
1010
Nate Begeman368e18d2006-02-16 21:11:51 +00001011/// computeMaskedBitsForTargetNode - Determine which of the bits specified
1012/// in Mask are known to be either zero or one and return them in the
1013/// KnownZero/KnownOne bitsets.
1014void TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00001015 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001016 APInt &KnownZero,
1017 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00001018 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00001019 unsigned Depth) const {
Chris Lattner1b5232a2006-04-02 06:19:46 +00001020 assert((Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1021 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1022 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1023 Op.getOpcode() == ISD::INTRINSIC_VOID) &&
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001024 "Should use MaskedValueIsZero if you don't know whether Op"
1025 " is a target node!");
Dan Gohman977a76f2008-02-13 22:28:48 +00001026 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Cheng3a03ebb2005-12-21 23:05:39 +00001027}
Chris Lattner4ccb0702006-01-26 20:37:03 +00001028
Chris Lattner5c3e21d2006-05-06 09:27:13 +00001029/// ComputeNumSignBitsForTargetNode - This method can be implemented by
1030/// targets that want to expose additional information about sign bits to the
1031/// DAG Combiner.
1032unsigned TargetLowering::ComputeNumSignBitsForTargetNode(SDOperand Op,
1033 unsigned Depth) const {
1034 assert((Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1035 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1036 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1037 Op.getOpcode() == ISD::INTRINSIC_VOID) &&
1038 "Should use ComputeNumSignBits if you don't know whether Op"
1039 " is a target node!");
1040 return 1;
1041}
1042
1043
Evan Chengfa1eb272007-02-08 22:13:59 +00001044/// SimplifySetCC - Try to simplify a setcc built with the specified operands
1045/// and cc. If it is unable to simplify it, return a null SDOperand.
1046SDOperand
Duncan Sands83ec4b62008-06-06 12:08:01 +00001047TargetLowering::SimplifySetCC(MVT VT, SDOperand N0, SDOperand N1,
Evan Chengfa1eb272007-02-08 22:13:59 +00001048 ISD::CondCode Cond, bool foldBooleans,
1049 DAGCombinerInfo &DCI) const {
1050 SelectionDAG &DAG = DCI.DAG;
1051
1052 // These setcc operations always fold.
1053 switch (Cond) {
1054 default: break;
1055 case ISD::SETFALSE:
1056 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
1057 case ISD::SETTRUE:
1058 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
1059 }
1060
1061 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
Dan Gohman6c6cd1c2008-03-03 22:22:56 +00001062 const APInt &C1 = N1C->getAPIntValue();
Evan Chengfa1eb272007-02-08 22:13:59 +00001063 if (isa<ConstantSDNode>(N0.Val)) {
1064 return DAG.FoldSetCC(VT, N0, N1, Cond);
1065 } else {
1066 // If the LHS is '(srl (ctlz x), 5)', the RHS is 0/1, and this is an
1067 // equality comparison, then we're just comparing whether X itself is
1068 // zero.
1069 if (N0.getOpcode() == ISD::SRL && (C1 == 0 || C1 == 1) &&
1070 N0.getOperand(0).getOpcode() == ISD::CTLZ &&
1071 N0.getOperand(1).getOpcode() == ISD::Constant) {
1072 unsigned ShAmt = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1073 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00001074 ShAmt == Log2_32(N0.getValueType().getSizeInBits())) {
Evan Chengfa1eb272007-02-08 22:13:59 +00001075 if ((C1 == 0) == (Cond == ISD::SETEQ)) {
1076 // (srl (ctlz x), 5) == 0 -> X != 0
1077 // (srl (ctlz x), 5) != 1 -> X != 0
1078 Cond = ISD::SETNE;
1079 } else {
1080 // (srl (ctlz x), 5) != 0 -> X == 0
1081 // (srl (ctlz x), 5) == 1 -> X == 0
1082 Cond = ISD::SETEQ;
1083 }
1084 SDOperand Zero = DAG.getConstant(0, N0.getValueType());
1085 return DAG.getSetCC(VT, N0.getOperand(0).getOperand(0),
1086 Zero, Cond);
1087 }
1088 }
1089
1090 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
1091 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001092 unsigned InSize = N0.getOperand(0).getValueType().getSizeInBits();
Evan Chengfa1eb272007-02-08 22:13:59 +00001093
1094 // If the comparison constant has bits in the upper part, the
1095 // zero-extended value could never match.
Dan Gohman6c6cd1c2008-03-03 22:22:56 +00001096 if (C1.intersects(APInt::getHighBitsSet(C1.getBitWidth(),
1097 C1.getBitWidth() - InSize))) {
Evan Chengfa1eb272007-02-08 22:13:59 +00001098 switch (Cond) {
1099 case ISD::SETUGT:
1100 case ISD::SETUGE:
1101 case ISD::SETEQ: return DAG.getConstant(0, VT);
1102 case ISD::SETULT:
1103 case ISD::SETULE:
1104 case ISD::SETNE: return DAG.getConstant(1, VT);
1105 case ISD::SETGT:
1106 case ISD::SETGE:
1107 // True if the sign bit of C1 is set.
Dan Gohman6c6cd1c2008-03-03 22:22:56 +00001108 return DAG.getConstant(C1.isNegative(), VT);
Evan Chengfa1eb272007-02-08 22:13:59 +00001109 case ISD::SETLT:
1110 case ISD::SETLE:
1111 // True if the sign bit of C1 isn't set.
Dan Gohman6c6cd1c2008-03-03 22:22:56 +00001112 return DAG.getConstant(C1.isNonNegative(), VT);
Evan Chengfa1eb272007-02-08 22:13:59 +00001113 default:
1114 break;
1115 }
1116 }
1117
1118 // Otherwise, we can perform the comparison with the low bits.
1119 switch (Cond) {
1120 case ISD::SETEQ:
1121 case ISD::SETNE:
1122 case ISD::SETUGT:
1123 case ISD::SETUGE:
1124 case ISD::SETULT:
1125 case ISD::SETULE:
1126 return DAG.getSetCC(VT, N0.getOperand(0),
Dan Gohman6c6cd1c2008-03-03 22:22:56 +00001127 DAG.getConstant(APInt(C1).trunc(InSize),
1128 N0.getOperand(0).getValueType()),
Evan Chengfa1eb272007-02-08 22:13:59 +00001129 Cond);
1130 default:
1131 break; // todo, be more careful with signed comparisons
1132 }
1133 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1134 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001135 MVT ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
1136 unsigned ExtSrcTyBits = ExtSrcTy.getSizeInBits();
1137 MVT ExtDstTy = N0.getValueType();
1138 unsigned ExtDstTyBits = ExtDstTy.getSizeInBits();
Evan Chengfa1eb272007-02-08 22:13:59 +00001139
1140 // If the extended part has any inconsistent bits, it cannot ever
1141 // compare equal. In other words, they have to be all ones or all
1142 // zeros.
Dan Gohman6c6cd1c2008-03-03 22:22:56 +00001143 APInt ExtBits =
1144 APInt::getHighBitsSet(ExtDstTyBits, ExtDstTyBits - ExtSrcTyBits);
Evan Chengfa1eb272007-02-08 22:13:59 +00001145 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
1146 return DAG.getConstant(Cond == ISD::SETNE, VT);
1147
1148 SDOperand ZextOp;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001149 MVT Op0Ty = N0.getOperand(0).getValueType();
Evan Chengfa1eb272007-02-08 22:13:59 +00001150 if (Op0Ty == ExtSrcTy) {
1151 ZextOp = N0.getOperand(0);
1152 } else {
Dan Gohman3370dd72008-03-03 22:37:52 +00001153 APInt Imm = APInt::getLowBitsSet(ExtDstTyBits, ExtSrcTyBits);
Evan Chengfa1eb272007-02-08 22:13:59 +00001154 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
1155 DAG.getConstant(Imm, Op0Ty));
1156 }
1157 if (!DCI.isCalledByLegalizer())
1158 DCI.AddToWorklist(ZextOp.Val);
1159 // Otherwise, make this a use of a zext.
1160 return DAG.getSetCC(VT, ZextOp,
Dan Gohman6c6cd1c2008-03-03 22:22:56 +00001161 DAG.getConstant(C1 & APInt::getLowBitsSet(
1162 ExtDstTyBits,
1163 ExtSrcTyBits),
Evan Chengfa1eb272007-02-08 22:13:59 +00001164 ExtDstTy),
1165 Cond);
Dan Gohman6c6cd1c2008-03-03 22:22:56 +00001166 } else if ((N1C->isNullValue() || N1C->getAPIntValue() == 1) &&
Evan Chengfa1eb272007-02-08 22:13:59 +00001167 (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,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001178 N0.getOperand(0).getValueType().isInteger());
Evan Chengfa1eb272007-02-08 22:13:59 +00001179 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)) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00001187 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue() == 1) {
Evan Chengfa1eb272007-02-08 22:13:59 +00001188 // 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 Gohman2e68b6f2008-02-25 21:11:39 +00001190 unsigned BitWidth = N0.getValueSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001191 if (DAG.MaskedValueIsZero(N0,
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001192 APInt::getHighBitsSet(BitWidth,
1193 BitWidth-1))) {
Evan Chengfa1eb272007-02-08 22:13:59 +00001194 // Okay, get the un-inverted input value.
1195 SDOperand Val;
1196 if (N0.getOpcode() == ISD::XOR)
1197 Val = N0.getOperand(0);
1198 else {
1199 assert(N0.getOpcode() == ISD::AND &&
1200 N0.getOperand(0).getOpcode() == ISD::XOR);
1201 // ((X^1)&1)^1 -> X & 1
1202 Val = DAG.getNode(ISD::AND, N0.getValueType(),
1203 N0.getOperand(0).getOperand(0),
1204 N0.getOperand(1));
1205 }
1206 return DAG.getSetCC(VT, Val, N1,
1207 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
1208 }
1209 }
1210 }
1211
Dan Gohman3370dd72008-03-03 22:37:52 +00001212 APInt MinVal, MaxVal;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001213 unsigned OperandBitSize = N1C->getValueType(0).getSizeInBits();
Evan Chengfa1eb272007-02-08 22:13:59 +00001214 if (ISD::isSignedIntSetCC(Cond)) {
Dan Gohman3370dd72008-03-03 22:37:52 +00001215 MinVal = APInt::getSignedMinValue(OperandBitSize);
1216 MaxVal = APInt::getSignedMaxValue(OperandBitSize);
Evan Chengfa1eb272007-02-08 22:13:59 +00001217 } else {
Dan Gohman3370dd72008-03-03 22:37:52 +00001218 MinVal = APInt::getMinValue(OperandBitSize);
1219 MaxVal = APInt::getMaxValue(OperandBitSize);
Evan Chengfa1eb272007-02-08 22:13:59 +00001220 }
1221
1222 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
1223 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
1224 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
Dan Gohman6c6cd1c2008-03-03 22:22:56 +00001225 // X >= C0 --> X > (C0-1)
1226 return DAG.getSetCC(VT, N0, DAG.getConstant(C1-1, N1.getValueType()),
Evan Chengfa1eb272007-02-08 22:13:59 +00001227 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
1228 }
1229
1230 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
1231 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
Dan Gohman6c6cd1c2008-03-03 22:22:56 +00001232 // X <= C0 --> X < (C0+1)
1233 return DAG.getSetCC(VT, N0, DAG.getConstant(C1+1, N1.getValueType()),
Evan Chengfa1eb272007-02-08 22:13:59 +00001234 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
1235 }
1236
1237 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
1238 return DAG.getConstant(0, VT); // X < MIN --> false
1239 if ((Cond == ISD::SETGE || Cond == ISD::SETUGE) && C1 == MinVal)
1240 return DAG.getConstant(1, VT); // X >= MIN --> true
1241 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal)
1242 return DAG.getConstant(0, VT); // X > MAX --> false
1243 if ((Cond == ISD::SETLE || Cond == ISD::SETULE) && C1 == MaxVal)
1244 return DAG.getConstant(1, VT); // X <= MAX --> true
1245
1246 // Canonicalize setgt X, Min --> setne X, Min
1247 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
1248 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
1249 // Canonicalize setlt X, Max --> setne X, Max
1250 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
1251 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
1252
1253 // If we have setult X, 1, turn it into seteq X, 0
1254 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
1255 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
1256 ISD::SETEQ);
1257 // If we have setugt X, Max-1, turn it into seteq X, Max
1258 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
1259 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
1260 ISD::SETEQ);
1261
1262 // If we have "setcc X, C0", check to see if we can shrink the immediate
1263 // by changing cc.
1264
1265 // SETUGT X, SINTMAX -> SETLT X, 0
1266 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
1267 C1 == (~0ULL >> (65-OperandBitSize)))
1268 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
1269 ISD::SETLT);
1270
1271 // FIXME: Implement the rest of these.
1272
1273 // Fold bit comparisons when we can.
1274 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
1275 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
1276 if (ConstantSDNode *AndRHS =
1277 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1278 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
1279 // Perform the xform if the AND RHS is a single bit.
1280 if (isPowerOf2_64(AndRHS->getValue())) {
1281 return DAG.getNode(ISD::SRL, VT, N0,
1282 DAG.getConstant(Log2_64(AndRHS->getValue()),
1283 getShiftAmountTy()));
1284 }
1285 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
1286 // (X & 8) == 8 --> (X & 8) >> 3
1287 // Perform the xform if C1 is a single bit.
Dan Gohman6c6cd1c2008-03-03 22:22:56 +00001288 if (C1.isPowerOf2()) {
Evan Chengfa1eb272007-02-08 22:13:59 +00001289 return DAG.getNode(ISD::SRL, VT, N0,
Dan Gohman6c6cd1c2008-03-03 22:22:56 +00001290 DAG.getConstant(C1.logBase2(), getShiftAmountTy()));
Evan Chengfa1eb272007-02-08 22:13:59 +00001291 }
1292 }
1293 }
1294 }
1295 } else if (isa<ConstantSDNode>(N0.Val)) {
1296 // Ensure that the constant occurs on the RHS.
1297 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
1298 }
1299
1300 if (isa<ConstantFPSDNode>(N0.Val)) {
1301 // Constant fold or commute setcc.
1302 SDOperand O = DAG.FoldSetCC(VT, N0, N1, Cond);
1303 if (O.Val) return O;
Chris Lattner63079f02007-12-29 08:37:08 +00001304 } else if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1.Val)) {
1305 // If the RHS of an FP comparison is a constant, simplify it away in
1306 // some cases.
1307 if (CFP->getValueAPF().isNaN()) {
1308 // If an operand is known to be a nan, we can fold it.
1309 switch (ISD::getUnorderedFlavor(Cond)) {
1310 default: assert(0 && "Unknown flavor!");
1311 case 0: // Known false.
1312 return DAG.getConstant(0, VT);
1313 case 1: // Known true.
1314 return DAG.getConstant(1, VT);
Chris Lattner1c3e1e22007-12-30 21:21:10 +00001315 case 2: // Undefined.
Chris Lattner63079f02007-12-29 08:37:08 +00001316 return DAG.getNode(ISD::UNDEF, VT);
1317 }
1318 }
1319
1320 // Otherwise, we know the RHS is not a NaN. Simplify the node to drop the
1321 // constant if knowing that the operand is non-nan is enough. We prefer to
1322 // have SETO(x,x) instead of SETO(x, 0.0) because this avoids having to
1323 // materialize 0.0.
1324 if (Cond == ISD::SETO || Cond == ISD::SETUO)
1325 return DAG.getSetCC(VT, N0, N0, Cond);
Evan Chengfa1eb272007-02-08 22:13:59 +00001326 }
1327
1328 if (N0 == N1) {
1329 // We can always fold X == X for integer setcc's.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001330 if (N0.getValueType().isInteger())
Evan Chengfa1eb272007-02-08 22:13:59 +00001331 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
1332 unsigned UOF = ISD::getUnorderedFlavor(Cond);
1333 if (UOF == 2) // FP operators that are undefined on NaNs.
1334 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
1335 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
1336 return DAG.getConstant(UOF, VT);
1337 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
1338 // if it is not already.
1339 ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
1340 if (NewCond != Cond)
1341 return DAG.getSetCC(VT, N0, N1, NewCond);
1342 }
1343
1344 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00001345 N0.getValueType().isInteger()) {
Evan Chengfa1eb272007-02-08 22:13:59 +00001346 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
1347 N0.getOpcode() == ISD::XOR) {
1348 // Simplify (X+Y) == (X+Z) --> Y == Z
1349 if (N0.getOpcode() == N1.getOpcode()) {
1350 if (N0.getOperand(0) == N1.getOperand(0))
1351 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
1352 if (N0.getOperand(1) == N1.getOperand(1))
1353 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
1354 if (DAG.isCommutativeBinOp(N0.getOpcode())) {
1355 // If X op Y == Y op X, try other combinations.
1356 if (N0.getOperand(0) == N1.getOperand(1))
1357 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
1358 if (N0.getOperand(1) == N1.getOperand(0))
1359 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(1), Cond);
1360 }
1361 }
1362
1363 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
1364 if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1365 // Turn (X+C1) == C2 --> X == C2-C1
1366 if (N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse()) {
1367 return DAG.getSetCC(VT, N0.getOperand(0),
1368 DAG.getConstant(RHSC->getValue()-LHSR->getValue(),
1369 N0.getValueType()), Cond);
1370 }
1371
1372 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
1373 if (N0.getOpcode() == ISD::XOR)
1374 // If we know that all of the inverted bits are zero, don't bother
1375 // performing the inversion.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001376 if (DAG.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getAPIntValue()))
1377 return
1378 DAG.getSetCC(VT, N0.getOperand(0),
1379 DAG.getConstant(LHSR->getAPIntValue() ^
1380 RHSC->getAPIntValue(),
1381 N0.getValueType()),
1382 Cond);
Evan Chengfa1eb272007-02-08 22:13:59 +00001383 }
1384
1385 // Turn (C1-X) == C2 --> X == C1-C2
1386 if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
1387 if (N0.getOpcode() == ISD::SUB && N0.Val->hasOneUse()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001388 return
1389 DAG.getSetCC(VT, N0.getOperand(1),
1390 DAG.getConstant(SUBC->getAPIntValue() -
1391 RHSC->getAPIntValue(),
1392 N0.getValueType()),
1393 Cond);
Evan Chengfa1eb272007-02-08 22:13:59 +00001394 }
1395 }
1396 }
1397
1398 // Simplify (X+Z) == X --> Z == 0
1399 if (N0.getOperand(0) == N1)
1400 return DAG.getSetCC(VT, N0.getOperand(1),
1401 DAG.getConstant(0, N0.getValueType()), Cond);
1402 if (N0.getOperand(1) == N1) {
1403 if (DAG.isCommutativeBinOp(N0.getOpcode()))
1404 return DAG.getSetCC(VT, N0.getOperand(0),
1405 DAG.getConstant(0, N0.getValueType()), Cond);
Chris Lattner2ad913b2007-05-19 00:43:44 +00001406 else if (N0.Val->hasOneUse()) {
Evan Chengfa1eb272007-02-08 22:13:59 +00001407 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
1408 // (Z-X) == X --> Z == X<<1
1409 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
1410 N1,
1411 DAG.getConstant(1, getShiftAmountTy()));
1412 if (!DCI.isCalledByLegalizer())
1413 DCI.AddToWorklist(SH.Val);
1414 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
1415 }
1416 }
1417 }
1418
1419 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
1420 N1.getOpcode() == ISD::XOR) {
1421 // Simplify X == (X+Z) --> Z == 0
1422 if (N1.getOperand(0) == N0) {
1423 return DAG.getSetCC(VT, N1.getOperand(1),
1424 DAG.getConstant(0, N1.getValueType()), Cond);
1425 } else if (N1.getOperand(1) == N0) {
1426 if (DAG.isCommutativeBinOp(N1.getOpcode())) {
1427 return DAG.getSetCC(VT, N1.getOperand(0),
1428 DAG.getConstant(0, N1.getValueType()), Cond);
Chris Lattner7667c0b2007-05-19 00:46:51 +00001429 } else if (N1.Val->hasOneUse()) {
Evan Chengfa1eb272007-02-08 22:13:59 +00001430 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
1431 // X == (Z-X) --> X<<1 == Z
1432 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
1433 DAG.getConstant(1, getShiftAmountTy()));
1434 if (!DCI.isCalledByLegalizer())
1435 DCI.AddToWorklist(SH.Val);
1436 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
1437 }
1438 }
1439 }
1440 }
1441
1442 // Fold away ALL boolean setcc's.
1443 SDOperand Temp;
1444 if (N0.getValueType() == MVT::i1 && foldBooleans) {
1445 switch (Cond) {
1446 default: assert(0 && "Unknown integer setcc!");
1447 case ISD::SETEQ: // X == Y -> (X^Y)^1
1448 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
1449 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
1450 if (!DCI.isCalledByLegalizer())
1451 DCI.AddToWorklist(Temp.Val);
1452 break;
1453 case ISD::SETNE: // X != Y --> (X^Y)
1454 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
1455 break;
1456 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
1457 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
1458 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
1459 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
1460 if (!DCI.isCalledByLegalizer())
1461 DCI.AddToWorklist(Temp.Val);
1462 break;
1463 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
1464 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
1465 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
1466 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
1467 if (!DCI.isCalledByLegalizer())
1468 DCI.AddToWorklist(Temp.Val);
1469 break;
1470 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
1471 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
1472 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
1473 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
1474 if (!DCI.isCalledByLegalizer())
1475 DCI.AddToWorklist(Temp.Val);
1476 break;
1477 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
1478 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
1479 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
1480 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
1481 break;
1482 }
1483 if (VT != MVT::i1) {
1484 if (!DCI.isCalledByLegalizer())
1485 DCI.AddToWorklist(N0.Val);
1486 // FIXME: If running after legalize, we probably can't do this.
1487 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
1488 }
1489 return N0;
1490 }
1491
1492 // Could not fold it.
1493 return SDOperand();
1494}
1495
Evan Chengad4196b2008-05-12 19:56:52 +00001496/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
1497/// node is a GlobalAddress + offset.
1498bool TargetLowering::isGAPlusOffset(SDNode *N, GlobalValue* &GA,
1499 int64_t &Offset) const {
1500 if (isa<GlobalAddressSDNode>(N)) {
Dan Gohman9ea3f562008-06-09 22:05:52 +00001501 GlobalAddressSDNode *GASD = cast<GlobalAddressSDNode>(N);
1502 GA = GASD->getGlobal();
1503 Offset += GASD->getOffset();
Evan Chengad4196b2008-05-12 19:56:52 +00001504 return true;
1505 }
1506
1507 if (N->getOpcode() == ISD::ADD) {
1508 SDOperand N1 = N->getOperand(0);
1509 SDOperand N2 = N->getOperand(1);
1510 if (isGAPlusOffset(N1.Val, GA, Offset)) {
1511 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
1512 if (V) {
1513 Offset += V->getSignExtended();
1514 return true;
1515 }
1516 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
1517 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
1518 if (V) {
1519 Offset += V->getSignExtended();
1520 return true;
1521 }
1522 }
1523 }
1524 return false;
1525}
1526
1527
1528/// isConsecutiveLoad - Return true if LD (which must be a LoadSDNode) is
1529/// loading 'Bytes' bytes from a location that is 'Dist' units away from the
1530/// location that the 'Base' load is loading from.
1531bool TargetLowering::isConsecutiveLoad(SDNode *LD, SDNode *Base,
1532 unsigned Bytes, int Dist,
Evan Cheng9bfa03c2008-05-12 23:04:07 +00001533 const MachineFrameInfo *MFI) const {
Evan Chengad4196b2008-05-12 19:56:52 +00001534 if (LD->getOperand(0).Val != Base->getOperand(0).Val)
1535 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001536 MVT VT = LD->getValueType(0);
1537 if (VT.getSizeInBits() / 8 != Bytes)
Evan Chengad4196b2008-05-12 19:56:52 +00001538 return false;
1539
1540 SDOperand Loc = LD->getOperand(1);
1541 SDOperand BaseLoc = Base->getOperand(1);
1542 if (Loc.getOpcode() == ISD::FrameIndex) {
1543 if (BaseLoc.getOpcode() != ISD::FrameIndex)
1544 return false;
1545 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
1546 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
1547 int FS = MFI->getObjectSize(FI);
1548 int BFS = MFI->getObjectSize(BFI);
1549 if (FS != BFS || FS != (int)Bytes) return false;
1550 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
1551 }
1552
1553 GlobalValue *GV1 = NULL;
1554 GlobalValue *GV2 = NULL;
1555 int64_t Offset1 = 0;
1556 int64_t Offset2 = 0;
1557 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
1558 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
1559 if (isGA1 && isGA2 && GV1 == GV2)
1560 return Offset1 == (Offset2 + Dist*Bytes);
1561 return false;
1562}
1563
1564
Chris Lattner00ffed02006-03-01 04:52:55 +00001565SDOperand TargetLowering::
1566PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
1567 // Default implementation: no optimization.
1568 return SDOperand();
1569}
1570
Chris Lattnereb8146b2006-02-04 02:13:02 +00001571//===----------------------------------------------------------------------===//
1572// Inline Assembler Implementation Methods
1573//===----------------------------------------------------------------------===//
1574
Chris Lattner4376fea2008-04-27 00:09:47 +00001575
Chris Lattnereb8146b2006-02-04 02:13:02 +00001576TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00001577TargetLowering::getConstraintType(const std::string &Constraint) const {
Chris Lattnereb8146b2006-02-04 02:13:02 +00001578 // FIXME: lots more standard ones to handle.
Chris Lattner4234f572007-03-25 02:14:49 +00001579 if (Constraint.size() == 1) {
1580 switch (Constraint[0]) {
1581 default: break;
1582 case 'r': return C_RegisterClass;
1583 case 'm': // memory
1584 case 'o': // offsetable
1585 case 'V': // not offsetable
1586 return C_Memory;
1587 case 'i': // Simple Integer or Relocatable Constant
1588 case 'n': // Simple Integer
1589 case 's': // Relocatable Constant
Chris Lattnerc13dd1c2007-03-25 04:35:41 +00001590 case 'X': // Allow ANY value.
Chris Lattner4234f572007-03-25 02:14:49 +00001591 case 'I': // Target registers.
1592 case 'J':
1593 case 'K':
1594 case 'L':
1595 case 'M':
1596 case 'N':
1597 case 'O':
1598 case 'P':
1599 return C_Other;
1600 }
Chris Lattnereb8146b2006-02-04 02:13:02 +00001601 }
Chris Lattner065421f2007-03-25 02:18:14 +00001602
1603 if (Constraint.size() > 1 && Constraint[0] == '{' &&
1604 Constraint[Constraint.size()-1] == '}')
1605 return C_Register;
Chris Lattner4234f572007-03-25 02:14:49 +00001606 return C_Unknown;
Chris Lattnereb8146b2006-02-04 02:13:02 +00001607}
1608
Dale Johannesenba2a0b92008-01-29 02:21:21 +00001609/// LowerXConstraint - try to replace an X constraint, which matches anything,
1610/// with another that has more specific requirements based on the type of the
1611/// corresponding operand.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001612const char *TargetLowering::LowerXConstraint(MVT ConstraintVT) const{
1613 if (ConstraintVT.isInteger())
Chris Lattner5e764232008-04-26 23:02:14 +00001614 return "r";
Duncan Sands83ec4b62008-06-06 12:08:01 +00001615 if (ConstraintVT.isFloatingPoint())
Chris Lattner5e764232008-04-26 23:02:14 +00001616 return "f"; // works for many targets
1617 return 0;
Dale Johannesenba2a0b92008-01-29 02:21:21 +00001618}
1619
Chris Lattner48884cd2007-08-25 00:47:38 +00001620/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
1621/// vector. If it is invalid, don't add anything to Ops.
1622void TargetLowering::LowerAsmOperandForConstraint(SDOperand Op,
1623 char ConstraintLetter,
1624 std::vector<SDOperand> &Ops,
Chris Lattner5e764232008-04-26 23:02:14 +00001625 SelectionDAG &DAG) const {
Chris Lattnereb8146b2006-02-04 02:13:02 +00001626 switch (ConstraintLetter) {
Chris Lattner9ff6ee82007-02-17 06:00:35 +00001627 default: break;
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001628 case 'X': // Allows any operand; labels (basic block) use this.
1629 if (Op.getOpcode() == ISD::BasicBlock) {
1630 Ops.push_back(Op);
1631 return;
1632 }
1633 // fall through
Chris Lattnereb8146b2006-02-04 02:13:02 +00001634 case 'i': // Simple Integer or Relocatable Constant
1635 case 'n': // Simple Integer
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001636 case 's': { // Relocatable Constant
Chris Lattner75c7d2b2007-05-03 16:54:34 +00001637 // These operands are interested in values of the form (GV+C), where C may
1638 // be folded in as an offset of GV, or it may be explicitly added. Also, it
1639 // is possible and fine if either GV or C are missing.
1640 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
1641 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
1642
1643 // If we have "(add GV, C)", pull out GV/C
1644 if (Op.getOpcode() == ISD::ADD) {
1645 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
1646 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
1647 if (C == 0 || GA == 0) {
1648 C = dyn_cast<ConstantSDNode>(Op.getOperand(0));
1649 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(1));
1650 }
1651 if (C == 0 || GA == 0)
1652 C = 0, GA = 0;
1653 }
1654
1655 // If we find a valid operand, map to the TargetXXX version so that the
1656 // value itself doesn't get selected.
1657 if (GA) { // Either &GV or &GV+C
1658 if (ConstraintLetter != 'n') {
1659 int64_t Offs = GA->getOffset();
1660 if (C) Offs += C->getValue();
Chris Lattner48884cd2007-08-25 00:47:38 +00001661 Ops.push_back(DAG.getTargetGlobalAddress(GA->getGlobal(),
1662 Op.getValueType(), Offs));
1663 return;
Chris Lattner75c7d2b2007-05-03 16:54:34 +00001664 }
1665 }
1666 if (C) { // just C, no GV.
Chris Lattner9ff6ee82007-02-17 06:00:35 +00001667 // Simple constants are not allowed for 's'.
Chris Lattner48884cd2007-08-25 00:47:38 +00001668 if (ConstraintLetter != 's') {
1669 Ops.push_back(DAG.getTargetConstant(C->getValue(), Op.getValueType()));
1670 return;
1671 }
Chris Lattner9ff6ee82007-02-17 06:00:35 +00001672 }
Chris Lattner9ff6ee82007-02-17 06:00:35 +00001673 break;
Chris Lattnereb8146b2006-02-04 02:13:02 +00001674 }
Chris Lattner75c7d2b2007-05-03 16:54:34 +00001675 }
Chris Lattnereb8146b2006-02-04 02:13:02 +00001676}
1677
Chris Lattner4ccb0702006-01-26 20:37:03 +00001678std::vector<unsigned> TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00001679getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001680 MVT VT) const {
Chris Lattner1efa40f2006-02-22 00:56:39 +00001681 return std::vector<unsigned>();
1682}
1683
1684
1685std::pair<unsigned, const TargetRegisterClass*> TargetLowering::
Chris Lattner4217ca8dc2006-02-21 23:11:00 +00001686getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001687 MVT VT) const {
Chris Lattner1efa40f2006-02-22 00:56:39 +00001688 if (Constraint[0] != '{')
1689 return std::pair<unsigned, const TargetRegisterClass*>(0, 0);
Chris Lattnera55079a2006-02-01 01:29:47 +00001690 assert(*(Constraint.end()-1) == '}' && "Not a brace enclosed constraint?");
1691
1692 // Remove the braces from around the name.
1693 std::string RegName(Constraint.begin()+1, Constraint.end()-1);
Chris Lattner1efa40f2006-02-22 00:56:39 +00001694
1695 // Figure out which register class contains this reg.
Dan Gohman6f0d0242008-02-10 18:45:23 +00001696 const TargetRegisterInfo *RI = TM.getRegisterInfo();
1697 for (TargetRegisterInfo::regclass_iterator RCI = RI->regclass_begin(),
Chris Lattner1efa40f2006-02-22 00:56:39 +00001698 E = RI->regclass_end(); RCI != E; ++RCI) {
1699 const TargetRegisterClass *RC = *RCI;
Chris Lattnerb3befd42006-02-22 23:00:51 +00001700
1701 // If none of the the value types for this register class are valid, we
1702 // can't use it. For example, 64-bit reg classes on 32-bit targets.
1703 bool isLegal = false;
1704 for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
1705 I != E; ++I) {
1706 if (isTypeLegal(*I)) {
1707 isLegal = true;
1708 break;
1709 }
1710 }
1711
1712 if (!isLegal) continue;
1713
Chris Lattner1efa40f2006-02-22 00:56:39 +00001714 for (TargetRegisterClass::iterator I = RC->begin(), E = RC->end();
1715 I != E; ++I) {
Bill Wendling74ab84c2008-02-26 21:11:01 +00001716 if (StringsEqualNoCase(RegName, RI->get(*I).AsmName))
Chris Lattner1efa40f2006-02-22 00:56:39 +00001717 return std::make_pair(*I, RC);
Chris Lattner1efa40f2006-02-22 00:56:39 +00001718 }
Chris Lattner4ccb0702006-01-26 20:37:03 +00001719 }
Chris Lattnera55079a2006-02-01 01:29:47 +00001720
Chris Lattner1efa40f2006-02-22 00:56:39 +00001721 return std::pair<unsigned, const TargetRegisterClass*>(0, 0);
Chris Lattner4ccb0702006-01-26 20:37:03 +00001722}
Evan Cheng30b37b52006-03-13 23:18:16 +00001723
1724//===----------------------------------------------------------------------===//
Chris Lattner4376fea2008-04-27 00:09:47 +00001725// Constraint Selection.
1726
1727/// getConstraintGenerality - Return an integer indicating how general CT
1728/// is.
1729static unsigned getConstraintGenerality(TargetLowering::ConstraintType CT) {
1730 switch (CT) {
1731 default: assert(0 && "Unknown constraint type!");
1732 case TargetLowering::C_Other:
1733 case TargetLowering::C_Unknown:
1734 return 0;
1735 case TargetLowering::C_Register:
1736 return 1;
1737 case TargetLowering::C_RegisterClass:
1738 return 2;
1739 case TargetLowering::C_Memory:
1740 return 3;
1741 }
1742}
1743
1744/// ChooseConstraint - If there are multiple different constraints that we
1745/// could pick for this operand (e.g. "imr") try to pick the 'best' one.
Chris Lattner24e1a9d2008-04-27 01:49:46 +00001746/// This is somewhat tricky: constraints fall into four classes:
Chris Lattner4376fea2008-04-27 00:09:47 +00001747/// Other -> immediates and magic values
1748/// Register -> one specific register
1749/// RegisterClass -> a group of regs
1750/// Memory -> memory
1751/// Ideally, we would pick the most specific constraint possible: if we have
1752/// something that fits into a register, we would pick it. The problem here
1753/// is that if we have something that could either be in a register or in
1754/// memory that use of the register could cause selection of *other*
1755/// operands to fail: they might only succeed if we pick memory. Because of
1756/// this the heuristic we use is:
1757///
1758/// 1) If there is an 'other' constraint, and if the operand is valid for
1759/// that constraint, use it. This makes us take advantage of 'i'
1760/// constraints when available.
1761/// 2) Otherwise, pick the most general constraint present. This prefers
1762/// 'm' over 'r', for example.
1763///
1764static void ChooseConstraint(TargetLowering::AsmOperandInfo &OpInfo,
Chris Lattner5a096902008-04-27 00:37:18 +00001765 const TargetLowering &TLI,
1766 SDOperand Op, SelectionDAG *DAG) {
Chris Lattner4376fea2008-04-27 00:09:47 +00001767 assert(OpInfo.Codes.size() > 1 && "Doesn't have multiple constraint options");
1768 unsigned BestIdx = 0;
1769 TargetLowering::ConstraintType BestType = TargetLowering::C_Unknown;
1770 int BestGenerality = -1;
1771
1772 // Loop over the options, keeping track of the most general one.
1773 for (unsigned i = 0, e = OpInfo.Codes.size(); i != e; ++i) {
1774 TargetLowering::ConstraintType CType =
1775 TLI.getConstraintType(OpInfo.Codes[i]);
1776
Chris Lattner5a096902008-04-27 00:37:18 +00001777 // If this is an 'other' constraint, see if the operand is valid for it.
1778 // For example, on X86 we might have an 'rI' constraint. If the operand
1779 // is an integer in the range [0..31] we want to use I (saving a load
1780 // of a register), otherwise we must use 'r'.
1781 if (CType == TargetLowering::C_Other && Op.Val) {
1782 assert(OpInfo.Codes[i].size() == 1 &&
1783 "Unhandled multi-letter 'other' constraint");
1784 std::vector<SDOperand> ResultOps;
1785 TLI.LowerAsmOperandForConstraint(Op, OpInfo.Codes[i][0],
1786 ResultOps, *DAG);
1787 if (!ResultOps.empty()) {
1788 BestType = CType;
1789 BestIdx = i;
1790 break;
1791 }
1792 }
1793
Chris Lattner4376fea2008-04-27 00:09:47 +00001794 // This constraint letter is more general than the previous one, use it.
1795 int Generality = getConstraintGenerality(CType);
1796 if (Generality > BestGenerality) {
1797 BestType = CType;
1798 BestIdx = i;
1799 BestGenerality = Generality;
1800 }
1801 }
1802
1803 OpInfo.ConstraintCode = OpInfo.Codes[BestIdx];
1804 OpInfo.ConstraintType = BestType;
1805}
1806
1807/// ComputeConstraintToUse - Determines the constraint code and constraint
1808/// type to use for the specific AsmOperandInfo, setting
1809/// OpInfo.ConstraintCode and OpInfo.ConstraintType.
Chris Lattner5a096902008-04-27 00:37:18 +00001810void TargetLowering::ComputeConstraintToUse(AsmOperandInfo &OpInfo,
1811 SDOperand Op,
1812 SelectionDAG *DAG) const {
Chris Lattner4376fea2008-04-27 00:09:47 +00001813 assert(!OpInfo.Codes.empty() && "Must have at least one constraint");
1814
1815 // Single-letter constraints ('r') are very common.
1816 if (OpInfo.Codes.size() == 1) {
1817 OpInfo.ConstraintCode = OpInfo.Codes[0];
1818 OpInfo.ConstraintType = getConstraintType(OpInfo.ConstraintCode);
1819 } else {
Chris Lattner5a096902008-04-27 00:37:18 +00001820 ChooseConstraint(OpInfo, *this, Op, DAG);
Chris Lattner4376fea2008-04-27 00:09:47 +00001821 }
1822
1823 // 'X' matches anything.
1824 if (OpInfo.ConstraintCode == "X" && OpInfo.CallOperandVal) {
1825 // Labels and constants are handled elsewhere ('X' is the only thing
1826 // that matches labels).
1827 if (isa<BasicBlock>(OpInfo.CallOperandVal) ||
1828 isa<ConstantInt>(OpInfo.CallOperandVal))
1829 return;
1830
1831 // Otherwise, try to resolve it to something we know about by looking at
1832 // the actual operand type.
1833 if (const char *Repl = LowerXConstraint(OpInfo.ConstraintVT)) {
1834 OpInfo.ConstraintCode = Repl;
1835 OpInfo.ConstraintType = getConstraintType(OpInfo.ConstraintCode);
1836 }
1837 }
1838}
1839
1840//===----------------------------------------------------------------------===//
Evan Cheng30b37b52006-03-13 23:18:16 +00001841// Loop Strength Reduction hooks
1842//===----------------------------------------------------------------------===//
1843
Chris Lattner1436bb62007-03-30 23:14:50 +00001844/// isLegalAddressingMode - Return true if the addressing mode represented
1845/// by AM is legal for this target, for a load/store of the specified type.
1846bool TargetLowering::isLegalAddressingMode(const AddrMode &AM,
1847 const Type *Ty) const {
1848 // The default implementation of this implements a conservative RISCy, r+r and
1849 // r+i addr mode.
1850
1851 // Allows a sign-extended 16-bit immediate field.
1852 if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1)
1853 return false;
1854
1855 // No global is ever allowed as a base.
1856 if (AM.BaseGV)
1857 return false;
1858
1859 // Only support r+r,
1860 switch (AM.Scale) {
1861 case 0: // "r+i" or just "i", depending on HasBaseReg.
1862 break;
1863 case 1:
1864 if (AM.HasBaseReg && AM.BaseOffs) // "r+r+i" is not allowed.
1865 return false;
1866 // Otherwise we have r+r or r+i.
1867 break;
1868 case 2:
1869 if (AM.HasBaseReg || AM.BaseOffs) // 2*r+r or 2*r+i is not allowed.
1870 return false;
1871 // Allow 2*r as r+r.
1872 break;
1873 }
1874
1875 return true;
1876}
1877
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00001878// Magic for divide replacement
1879
1880struct ms {
1881 int64_t m; // magic number
1882 int64_t s; // shift amount
1883};
1884
1885struct mu {
1886 uint64_t m; // magic number
1887 int64_t a; // add indicator
1888 int64_t s; // shift amount
1889};
1890
1891/// magic - calculate the magic numbers required to codegen an integer sdiv as
1892/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
1893/// or -1.
1894static ms magic32(int32_t d) {
1895 int32_t p;
1896 uint32_t ad, anc, delta, q1, r1, q2, r2, t;
1897 const uint32_t two31 = 0x80000000U;
1898 struct ms mag;
1899
1900 ad = abs(d);
1901 t = two31 + ((uint32_t)d >> 31);
1902 anc = t - 1 - t%ad; // absolute value of nc
1903 p = 31; // initialize p
1904 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
1905 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
1906 q2 = two31/ad; // initialize q2 = 2p/abs(d)
1907 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
1908 do {
1909 p = p + 1;
1910 q1 = 2*q1; // update q1 = 2p/abs(nc)
1911 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
1912 if (r1 >= anc) { // must be unsigned comparison
1913 q1 = q1 + 1;
1914 r1 = r1 - anc;
1915 }
1916 q2 = 2*q2; // update q2 = 2p/abs(d)
1917 r2 = 2*r2; // update r2 = rem(2p/abs(d))
1918 if (r2 >= ad) { // must be unsigned comparison
1919 q2 = q2 + 1;
1920 r2 = r2 - ad;
1921 }
1922 delta = ad - r2;
1923 } while (q1 < delta || (q1 == delta && r1 == 0));
1924
1925 mag.m = (int32_t)(q2 + 1); // make sure to sign extend
1926 if (d < 0) mag.m = -mag.m; // resulting magic number
1927 mag.s = p - 32; // resulting shift
1928 return mag;
1929}
1930
1931/// magicu - calculate the magic numbers required to codegen an integer udiv as
1932/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
1933static mu magicu32(uint32_t d) {
1934 int32_t p;
1935 uint32_t nc, delta, q1, r1, q2, r2;
1936 struct mu magu;
1937 magu.a = 0; // initialize "add" indicator
1938 nc = - 1 - (-d)%d;
1939 p = 31; // initialize p
1940 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
1941 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
1942 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
1943 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
1944 do {
1945 p = p + 1;
1946 if (r1 >= nc - r1 ) {
1947 q1 = 2*q1 + 1; // update q1
1948 r1 = 2*r1 - nc; // update r1
1949 }
1950 else {
1951 q1 = 2*q1; // update q1
1952 r1 = 2*r1; // update r1
1953 }
1954 if (r2 + 1 >= d - r2) {
1955 if (q2 >= 0x7FFFFFFF) magu.a = 1;
1956 q2 = 2*q2 + 1; // update q2
1957 r2 = 2*r2 + 1 - d; // update r2
1958 }
1959 else {
1960 if (q2 >= 0x80000000) magu.a = 1;
1961 q2 = 2*q2; // update q2
1962 r2 = 2*r2 + 1; // update r2
1963 }
1964 delta = d - 1 - r2;
1965 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
1966 magu.m = q2 + 1; // resulting magic number
1967 magu.s = p - 32; // resulting shift
1968 return magu;
1969}
1970
1971/// magic - calculate the magic numbers required to codegen an integer sdiv as
1972/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
1973/// or -1.
1974static ms magic64(int64_t d) {
1975 int64_t p;
1976 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
1977 const uint64_t two63 = 9223372036854775808ULL; // 2^63
1978 struct ms mag;
1979
1980 ad = d >= 0 ? d : -d;
1981 t = two63 + ((uint64_t)d >> 63);
1982 anc = t - 1 - t%ad; // absolute value of nc
1983 p = 63; // initialize p
1984 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
1985 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
1986 q2 = two63/ad; // initialize q2 = 2p/abs(d)
1987 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
1988 do {
1989 p = p + 1;
1990 q1 = 2*q1; // update q1 = 2p/abs(nc)
1991 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
1992 if (r1 >= anc) { // must be unsigned comparison
1993 q1 = q1 + 1;
1994 r1 = r1 - anc;
1995 }
1996 q2 = 2*q2; // update q2 = 2p/abs(d)
1997 r2 = 2*r2; // update r2 = rem(2p/abs(d))
1998 if (r2 >= ad) { // must be unsigned comparison
1999 q2 = q2 + 1;
2000 r2 = r2 - ad;
2001 }
2002 delta = ad - r2;
2003 } while (q1 < delta || (q1 == delta && r1 == 0));
2004
2005 mag.m = q2 + 1;
2006 if (d < 0) mag.m = -mag.m; // resulting magic number
2007 mag.s = p - 64; // resulting shift
2008 return mag;
2009}
2010
2011/// magicu - calculate the magic numbers required to codegen an integer udiv as
2012/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
2013static mu magicu64(uint64_t d)
2014{
2015 int64_t p;
2016 uint64_t nc, delta, q1, r1, q2, r2;
2017 struct mu magu;
2018 magu.a = 0; // initialize "add" indicator
2019 nc = - 1 - (-d)%d;
2020 p = 63; // initialize p
2021 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
2022 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
2023 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
2024 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
2025 do {
2026 p = p + 1;
2027 if (r1 >= nc - r1 ) {
2028 q1 = 2*q1 + 1; // update q1
2029 r1 = 2*r1 - nc; // update r1
2030 }
2031 else {
2032 q1 = 2*q1; // update q1
2033 r1 = 2*r1; // update r1
2034 }
2035 if (r2 + 1 >= d - r2) {
2036 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
2037 q2 = 2*q2 + 1; // update q2
2038 r2 = 2*r2 + 1 - d; // update r2
2039 }
2040 else {
2041 if (q2 >= 0x8000000000000000ull) magu.a = 1;
2042 q2 = 2*q2; // update q2
2043 r2 = 2*r2 + 1; // update r2
2044 }
2045 delta = d - 1 - r2;
Andrew Lenharth3e348492006-05-16 17:45:23 +00002046 } while (p < 128 && (q1 < delta || (q1 == delta && r1 == 0)));
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002047 magu.m = q2 + 1; // resulting magic number
2048 magu.s = p - 64; // resulting shift
2049 return magu;
2050}
2051
2052/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
2053/// return a DAG expression to select that will generate the same value by
2054/// multiplying by a magic number. See:
2055/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
2056SDOperand TargetLowering::BuildSDIV(SDNode *N, SelectionDAG &DAG,
Anton Korobeynikovbed29462007-04-16 18:10:23 +00002057 std::vector<SDNode*>* Created) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002058 MVT VT = N->getValueType(0);
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002059
2060 // Check to see if we can do this.
2061 if (!isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64))
2062 return SDOperand(); // BuildSDIV only operates on i32 or i64
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002063
2064 int64_t d = cast<ConstantSDNode>(N->getOperand(1))->getSignExtended();
2065 ms magics = (VT == MVT::i32) ? magic32(d) : magic64(d);
2066
2067 // Multiply the numerator (operand 0) by the magic value
Dan Gohman525178c2007-10-08 18:33:35 +00002068 SDOperand Q;
2069 if (isOperationLegal(ISD::MULHS, VT))
2070 Q = DAG.getNode(ISD::MULHS, VT, N->getOperand(0),
2071 DAG.getConstant(magics.m, VT));
2072 else if (isOperationLegal(ISD::SMUL_LOHI, VT))
2073 Q = SDOperand(DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(VT, VT),
2074 N->getOperand(0),
2075 DAG.getConstant(magics.m, VT)).Val, 1);
2076 else
2077 return SDOperand(); // No mulhs or equvialent
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002078 // If d > 0 and m < 0, add the numerator
2079 if (d > 0 && magics.m < 0) {
2080 Q = DAG.getNode(ISD::ADD, VT, Q, N->getOperand(0));
2081 if (Created)
2082 Created->push_back(Q.Val);
2083 }
2084 // If d < 0 and m > 0, subtract the numerator.
2085 if (d < 0 && magics.m > 0) {
2086 Q = DAG.getNode(ISD::SUB, VT, Q, N->getOperand(0));
2087 if (Created)
2088 Created->push_back(Q.Val);
2089 }
2090 // Shift right algebraic if shift value is nonzero
2091 if (magics.s > 0) {
2092 Q = DAG.getNode(ISD::SRA, VT, Q,
2093 DAG.getConstant(magics.s, getShiftAmountTy()));
2094 if (Created)
2095 Created->push_back(Q.Val);
2096 }
2097 // Extract the sign bit and add it to the quotient
2098 SDOperand T =
Duncan Sands83ec4b62008-06-06 12:08:01 +00002099 DAG.getNode(ISD::SRL, VT, Q, DAG.getConstant(VT.getSizeInBits()-1,
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002100 getShiftAmountTy()));
2101 if (Created)
2102 Created->push_back(T.Val);
2103 return DAG.getNode(ISD::ADD, VT, Q, T);
2104}
2105
2106/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
2107/// return a DAG expression to select that will generate the same value by
2108/// multiplying by a magic number. See:
2109/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
2110SDOperand TargetLowering::BuildUDIV(SDNode *N, SelectionDAG &DAG,
Anton Korobeynikovbed29462007-04-16 18:10:23 +00002111 std::vector<SDNode*>* Created) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002112 MVT VT = N->getValueType(0);
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002113
2114 // Check to see if we can do this.
2115 if (!isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64))
2116 return SDOperand(); // BuildUDIV only operates on i32 or i64
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002117
2118 uint64_t d = cast<ConstantSDNode>(N->getOperand(1))->getValue();
2119 mu magics = (VT == MVT::i32) ? magicu32(d) : magicu64(d);
2120
2121 // Multiply the numerator (operand 0) by the magic value
Dan Gohman525178c2007-10-08 18:33:35 +00002122 SDOperand Q;
2123 if (isOperationLegal(ISD::MULHU, VT))
2124 Q = DAG.getNode(ISD::MULHU, VT, N->getOperand(0),
2125 DAG.getConstant(magics.m, VT));
2126 else if (isOperationLegal(ISD::UMUL_LOHI, VT))
2127 Q = SDOperand(DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(VT, VT),
2128 N->getOperand(0),
2129 DAG.getConstant(magics.m, VT)).Val, 1);
2130 else
2131 return SDOperand(); // No mulhu or equvialent
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002132 if (Created)
2133 Created->push_back(Q.Val);
2134
2135 if (magics.a == 0) {
2136 return DAG.getNode(ISD::SRL, VT, Q,
2137 DAG.getConstant(magics.s, getShiftAmountTy()));
2138 } else {
2139 SDOperand NPQ = DAG.getNode(ISD::SUB, VT, N->getOperand(0), Q);
2140 if (Created)
2141 Created->push_back(NPQ.Val);
2142 NPQ = DAG.getNode(ISD::SRL, VT, NPQ,
2143 DAG.getConstant(1, getShiftAmountTy()));
2144 if (Created)
2145 Created->push_back(NPQ.Val);
2146 NPQ = DAG.getNode(ISD::ADD, VT, NPQ, Q);
2147 if (Created)
2148 Created->push_back(NPQ.Val);
2149 return DAG.getNode(ISD::SRL, VT, NPQ,
2150 DAG.getConstant(magics.s-1, getShiftAmountTy()));
2151 }
2152}