blob: ee72012133a4bc5138585f71dfffb003f5a4cc4a [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";
34 Names[RTLIB::SRL_I32] = "__lshrsi3";
35 Names[RTLIB::SRL_I64] = "__lshrdi3";
36 Names[RTLIB::SRA_I32] = "__ashrsi3";
37 Names[RTLIB::SRA_I64] = "__ashrdi3";
38 Names[RTLIB::MUL_I32] = "__mulsi3";
39 Names[RTLIB::MUL_I64] = "__muldi3";
Duncan Sands5ac319a2008-07-10 15:35:05 +000040 Names[RTLIB::MUL_I128] = "__multi3";
Evan Cheng56966222007-01-12 02:11:51 +000041 Names[RTLIB::SDIV_I32] = "__divsi3";
42 Names[RTLIB::SDIV_I64] = "__divdi3";
Duncan Sands5ac319a2008-07-10 15:35:05 +000043 Names[RTLIB::SDIV_I128] = "__divti3";
Evan Cheng56966222007-01-12 02:11:51 +000044 Names[RTLIB::UDIV_I32] = "__udivsi3";
45 Names[RTLIB::UDIV_I64] = "__udivdi3";
Duncan Sands5ac319a2008-07-10 15:35:05 +000046 Names[RTLIB::UDIV_I128] = "__udivti3";
Evan Cheng56966222007-01-12 02:11:51 +000047 Names[RTLIB::SREM_I32] = "__modsi3";
48 Names[RTLIB::SREM_I64] = "__moddi3";
Duncan Sands5ac319a2008-07-10 15:35:05 +000049 Names[RTLIB::SREM_I128] = "__modti3";
Evan Cheng56966222007-01-12 02:11:51 +000050 Names[RTLIB::UREM_I32] = "__umodsi3";
51 Names[RTLIB::UREM_I64] = "__umoddi3";
Duncan Sands5ac319a2008-07-10 15:35:05 +000052 Names[RTLIB::UREM_I128] = "__umodti3";
Evan Cheng56966222007-01-12 02:11:51 +000053 Names[RTLIB::NEG_I32] = "__negsi2";
54 Names[RTLIB::NEG_I64] = "__negdi2";
55 Names[RTLIB::ADD_F32] = "__addsf3";
56 Names[RTLIB::ADD_F64] = "__adddf3";
Duncan Sands007f9842008-01-10 10:28:30 +000057 Names[RTLIB::ADD_F80] = "__addxf3";
Dale Johannesen161e8972007-10-05 20:04:43 +000058 Names[RTLIB::ADD_PPCF128] = "__gcc_qadd";
Evan Cheng56966222007-01-12 02:11:51 +000059 Names[RTLIB::SUB_F32] = "__subsf3";
60 Names[RTLIB::SUB_F64] = "__subdf3";
Duncan Sands007f9842008-01-10 10:28:30 +000061 Names[RTLIB::SUB_F80] = "__subxf3";
Dale Johannesen161e8972007-10-05 20:04:43 +000062 Names[RTLIB::SUB_PPCF128] = "__gcc_qsub";
Evan Cheng56966222007-01-12 02:11:51 +000063 Names[RTLIB::MUL_F32] = "__mulsf3";
64 Names[RTLIB::MUL_F64] = "__muldf3";
Duncan Sands007f9842008-01-10 10:28:30 +000065 Names[RTLIB::MUL_F80] = "__mulxf3";
Dale Johannesen161e8972007-10-05 20:04:43 +000066 Names[RTLIB::MUL_PPCF128] = "__gcc_qmul";
Evan Cheng56966222007-01-12 02:11:51 +000067 Names[RTLIB::DIV_F32] = "__divsf3";
68 Names[RTLIB::DIV_F64] = "__divdf3";
Duncan Sands007f9842008-01-10 10:28:30 +000069 Names[RTLIB::DIV_F80] = "__divxf3";
Dale Johannesen161e8972007-10-05 20:04:43 +000070 Names[RTLIB::DIV_PPCF128] = "__gcc_qdiv";
Evan Cheng56966222007-01-12 02:11:51 +000071 Names[RTLIB::REM_F32] = "fmodf";
72 Names[RTLIB::REM_F64] = "fmod";
Duncan Sands007f9842008-01-10 10:28:30 +000073 Names[RTLIB::REM_F80] = "fmodl";
Dale Johannesen161e8972007-10-05 20:04:43 +000074 Names[RTLIB::REM_PPCF128] = "fmodl";
Evan Cheng56966222007-01-12 02:11:51 +000075 Names[RTLIB::POWI_F32] = "__powisf2";
76 Names[RTLIB::POWI_F64] = "__powidf2";
Dale Johannesen161e8972007-10-05 20:04:43 +000077 Names[RTLIB::POWI_F80] = "__powixf2";
78 Names[RTLIB::POWI_PPCF128] = "__powitf2";
Evan Cheng56966222007-01-12 02:11:51 +000079 Names[RTLIB::SQRT_F32] = "sqrtf";
80 Names[RTLIB::SQRT_F64] = "sqrt";
Dale Johannesen161e8972007-10-05 20:04:43 +000081 Names[RTLIB::SQRT_F80] = "sqrtl";
82 Names[RTLIB::SQRT_PPCF128] = "sqrtl";
Evan Cheng56966222007-01-12 02:11:51 +000083 Names[RTLIB::SIN_F32] = "sinf";
84 Names[RTLIB::SIN_F64] = "sin";
Duncan Sands007f9842008-01-10 10:28:30 +000085 Names[RTLIB::SIN_F80] = "sinl";
86 Names[RTLIB::SIN_PPCF128] = "sinl";
Evan Cheng56966222007-01-12 02:11:51 +000087 Names[RTLIB::COS_F32] = "cosf";
88 Names[RTLIB::COS_F64] = "cos";
Duncan Sands007f9842008-01-10 10:28:30 +000089 Names[RTLIB::COS_F80] = "cosl";
90 Names[RTLIB::COS_PPCF128] = "cosl";
Dan Gohmane54be102007-10-11 23:09:10 +000091 Names[RTLIB::POW_F32] = "powf";
92 Names[RTLIB::POW_F64] = "pow";
93 Names[RTLIB::POW_F80] = "powl";
94 Names[RTLIB::POW_PPCF128] = "powl";
Evan Cheng56966222007-01-12 02:11:51 +000095 Names[RTLIB::FPEXT_F32_F64] = "__extendsfdf2";
96 Names[RTLIB::FPROUND_F64_F32] = "__truncdfsf2";
97 Names[RTLIB::FPTOSINT_F32_I32] = "__fixsfsi";
98 Names[RTLIB::FPTOSINT_F32_I64] = "__fixsfdi";
Dan Gohmana2e94852008-03-10 23:03:31 +000099 Names[RTLIB::FPTOSINT_F32_I128] = "__fixsfti";
Evan Cheng56966222007-01-12 02:11:51 +0000100 Names[RTLIB::FPTOSINT_F64_I32] = "__fixdfsi";
101 Names[RTLIB::FPTOSINT_F64_I64] = "__fixdfdi";
Dan Gohmana2e94852008-03-10 23:03:31 +0000102 Names[RTLIB::FPTOSINT_F64_I128] = "__fixdfti";
Duncan Sandsbe1ad4d2008-07-10 15:33:02 +0000103 Names[RTLIB::FPTOSINT_F80_I32] = "__fixxfsi";
Dale Johannesen161e8972007-10-05 20:04:43 +0000104 Names[RTLIB::FPTOSINT_F80_I64] = "__fixxfdi";
Dan Gohmana2e94852008-03-10 23:03:31 +0000105 Names[RTLIB::FPTOSINT_F80_I128] = "__fixxfti";
Duncan Sands041cde22008-06-25 20:24:48 +0000106 Names[RTLIB::FPTOSINT_PPCF128_I32] = "__fixtfsi";
Dale Johannesen161e8972007-10-05 20:04:43 +0000107 Names[RTLIB::FPTOSINT_PPCF128_I64] = "__fixtfdi";
Dan Gohmana2e94852008-03-10 23:03:31 +0000108 Names[RTLIB::FPTOSINT_PPCF128_I128] = "__fixtfti";
Evan Cheng56966222007-01-12 02:11:51 +0000109 Names[RTLIB::FPTOUINT_F32_I32] = "__fixunssfsi";
110 Names[RTLIB::FPTOUINT_F32_I64] = "__fixunssfdi";
Dan Gohmana2e94852008-03-10 23:03:31 +0000111 Names[RTLIB::FPTOUINT_F32_I128] = "__fixunssfti";
Evan Cheng56966222007-01-12 02:11:51 +0000112 Names[RTLIB::FPTOUINT_F64_I32] = "__fixunsdfsi";
113 Names[RTLIB::FPTOUINT_F64_I64] = "__fixunsdfdi";
Dan Gohmana2e94852008-03-10 23:03:31 +0000114 Names[RTLIB::FPTOUINT_F64_I128] = "__fixunsdfti";
Dale Johannesen161e8972007-10-05 20:04:43 +0000115 Names[RTLIB::FPTOUINT_F80_I32] = "__fixunsxfsi";
116 Names[RTLIB::FPTOUINT_F80_I64] = "__fixunsxfdi";
Dan Gohmana2e94852008-03-10 23:03:31 +0000117 Names[RTLIB::FPTOUINT_F80_I128] = "__fixunsxfti";
Duncan Sands041cde22008-06-25 20:24:48 +0000118 Names[RTLIB::FPTOUINT_PPCF128_I32] = "__fixunstfsi";
Dale Johannesen161e8972007-10-05 20:04:43 +0000119 Names[RTLIB::FPTOUINT_PPCF128_I64] = "__fixunstfdi";
Dan Gohmana2e94852008-03-10 23:03:31 +0000120 Names[RTLIB::FPTOUINT_PPCF128_I128] = "__fixunstfti";
Evan Cheng56966222007-01-12 02:11:51 +0000121 Names[RTLIB::SINTTOFP_I32_F32] = "__floatsisf";
122 Names[RTLIB::SINTTOFP_I32_F64] = "__floatsidf";
123 Names[RTLIB::SINTTOFP_I64_F32] = "__floatdisf";
124 Names[RTLIB::SINTTOFP_I64_F64] = "__floatdidf";
Dale Johannesen161e8972007-10-05 20:04:43 +0000125 Names[RTLIB::SINTTOFP_I64_F80] = "__floatdixf";
126 Names[RTLIB::SINTTOFP_I64_PPCF128] = "__floatditf";
Dan Gohmand91446d2008-03-05 01:08:17 +0000127 Names[RTLIB::SINTTOFP_I128_F32] = "__floattisf";
128 Names[RTLIB::SINTTOFP_I128_F64] = "__floattidf";
129 Names[RTLIB::SINTTOFP_I128_F80] = "__floattixf";
130 Names[RTLIB::SINTTOFP_I128_PPCF128] = "__floattitf";
Evan Cheng56966222007-01-12 02:11:51 +0000131 Names[RTLIB::UINTTOFP_I32_F32] = "__floatunsisf";
132 Names[RTLIB::UINTTOFP_I32_F64] = "__floatunsidf";
133 Names[RTLIB::UINTTOFP_I64_F32] = "__floatundisf";
134 Names[RTLIB::UINTTOFP_I64_F64] = "__floatundidf";
135 Names[RTLIB::OEQ_F32] = "__eqsf2";
136 Names[RTLIB::OEQ_F64] = "__eqdf2";
137 Names[RTLIB::UNE_F32] = "__nesf2";
138 Names[RTLIB::UNE_F64] = "__nedf2";
139 Names[RTLIB::OGE_F32] = "__gesf2";
140 Names[RTLIB::OGE_F64] = "__gedf2";
141 Names[RTLIB::OLT_F32] = "__ltsf2";
142 Names[RTLIB::OLT_F64] = "__ltdf2";
143 Names[RTLIB::OLE_F32] = "__lesf2";
144 Names[RTLIB::OLE_F64] = "__ledf2";
145 Names[RTLIB::OGT_F32] = "__gtsf2";
146 Names[RTLIB::OGT_F64] = "__gtdf2";
147 Names[RTLIB::UO_F32] = "__unordsf2";
148 Names[RTLIB::UO_F64] = "__unorddf2";
Evan Chengd385fd62007-01-31 09:29:11 +0000149 Names[RTLIB::O_F32] = "__unordsf2";
150 Names[RTLIB::O_F64] = "__unorddf2";
151}
152
153/// InitCmpLibcallCCs - Set default comparison libcall CC.
154///
155static void InitCmpLibcallCCs(ISD::CondCode *CCs) {
156 memset(CCs, ISD::SETCC_INVALID, sizeof(ISD::CondCode)*RTLIB::UNKNOWN_LIBCALL);
157 CCs[RTLIB::OEQ_F32] = ISD::SETEQ;
158 CCs[RTLIB::OEQ_F64] = ISD::SETEQ;
159 CCs[RTLIB::UNE_F32] = ISD::SETNE;
160 CCs[RTLIB::UNE_F64] = ISD::SETNE;
161 CCs[RTLIB::OGE_F32] = ISD::SETGE;
162 CCs[RTLIB::OGE_F64] = ISD::SETGE;
163 CCs[RTLIB::OLT_F32] = ISD::SETLT;
164 CCs[RTLIB::OLT_F64] = ISD::SETLT;
165 CCs[RTLIB::OLE_F32] = ISD::SETLE;
166 CCs[RTLIB::OLE_F64] = ISD::SETLE;
167 CCs[RTLIB::OGT_F32] = ISD::SETGT;
168 CCs[RTLIB::OGT_F64] = ISD::SETGT;
169 CCs[RTLIB::UO_F32] = ISD::SETNE;
170 CCs[RTLIB::UO_F64] = ISD::SETNE;
171 CCs[RTLIB::O_F32] = ISD::SETEQ;
172 CCs[RTLIB::O_F64] = ISD::SETEQ;
Evan Cheng56966222007-01-12 02:11:51 +0000173}
174
Chris Lattner310968c2005-01-07 07:44:53 +0000175TargetLowering::TargetLowering(TargetMachine &tm)
Chris Lattner3e6e8cc2006-01-29 08:41:12 +0000176 : TM(tm), TD(TM.getTargetData()) {
Mon P Wang63307c32008-05-05 19:05:59 +0000177 assert(ISD::BUILTIN_OP_END <= OpActionsCapacity &&
Chris Lattner310968c2005-01-07 07:44:53 +0000178 "Fixed size array in TargetLowering is not large enough!");
Chris Lattnercba82f92005-01-16 07:28:11 +0000179 // All operations default to being supported.
180 memset(OpActions, 0, sizeof(OpActions));
Evan Chengc5484282006-10-04 00:56:09 +0000181 memset(LoadXActions, 0, sizeof(LoadXActions));
Chris Lattnerddf89562008-01-17 19:59:44 +0000182 memset(TruncStoreActions, 0, sizeof(TruncStoreActions));
Chris Lattnerc9133f92008-01-18 19:36:20 +0000183 memset(IndexedModeActions, 0, sizeof(IndexedModeActions));
184 memset(ConvertActions, 0, sizeof(ConvertActions));
Dan Gohman93f81e22007-07-09 20:49:44 +0000185
Chris Lattner1a3048b2007-12-22 20:47:56 +0000186 // Set default actions for various operations.
Evan Cheng5ff839f2006-11-09 18:56:43 +0000187 for (unsigned VT = 0; VT != (unsigned)MVT::LAST_VALUETYPE; ++VT) {
Chris Lattner1a3048b2007-12-22 20:47:56 +0000188 // Default all indexed load / store to expand.
Evan Cheng5ff839f2006-11-09 18:56:43 +0000189 for (unsigned IM = (unsigned)ISD::PRE_INC;
190 IM != (unsigned)ISD::LAST_INDEXED_MODE; ++IM) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000191 setIndexedLoadAction(IM, (MVT::SimpleValueType)VT, Expand);
192 setIndexedStoreAction(IM, (MVT::SimpleValueType)VT, Expand);
Evan Cheng5ff839f2006-11-09 18:56:43 +0000193 }
Chris Lattner1a3048b2007-12-22 20:47:56 +0000194
195 // These operations default to expand.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000196 setOperationAction(ISD::FGETSIGN, (MVT::SimpleValueType)VT, Expand);
Evan Cheng5ff839f2006-11-09 18:56:43 +0000197 }
Evan Chengd2cde682008-03-10 19:38:10 +0000198
199 // Most targets ignore the @llvm.prefetch intrinsic.
200 setOperationAction(ISD::PREFETCH, MVT::Other, Expand);
Nate Begemane1795842008-02-14 08:57:00 +0000201
202 // ConstantFP nodes default to expand. Targets can either change this to
203 // Legal, in which case all fp constants are legal, or use addLegalFPImmediate
204 // to optimize expansions for certain constants.
205 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
206 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
207 setOperationAction(ISD::ConstantFP, MVT::f80, Expand);
Chris Lattner310968c2005-01-07 07:44:53 +0000208
Chris Lattner41bab0b2008-01-15 21:58:08 +0000209 // Default ISD::TRAP to expand (which turns it into abort).
210 setOperationAction(ISD::TRAP, MVT::Other, Expand);
211
Owen Andersona69571c2006-05-03 01:29:57 +0000212 IsLittleEndian = TD->isLittleEndian();
Chris Lattnercf9668f2006-10-06 22:52:08 +0000213 UsesGlobalOffsetTable = false;
Scott Michel5b8f82e2008-03-10 15:42:14 +0000214 ShiftAmountTy = PointerTy = getValueType(TD->getIntPtrType());
Chris Lattnerd6e49672005-01-19 03:36:14 +0000215 ShiftAmtHandling = Undefined;
Chris Lattner310968c2005-01-07 07:44:53 +0000216 memset(RegClassForVT, 0,MVT::LAST_VALUETYPE*sizeof(TargetRegisterClass*));
Owen Anderson718cb662007-09-07 04:06:50 +0000217 memset(TargetDAGCombineArray, 0, array_lengthof(TargetDAGCombineArray));
Evan Chenga03a5dc2006-02-14 08:38:30 +0000218 maxStoresPerMemset = maxStoresPerMemcpy = maxStoresPerMemmove = 8;
Reid Spencer0f9beca2005-08-27 19:09:02 +0000219 allowUnalignedMemoryAccesses = false;
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000220 UseUnderscoreSetJmp = false;
221 UseUnderscoreLongJmp = false;
Chris Lattner66180392007-02-25 01:28:05 +0000222 SelectIsExpensive = false;
Nate Begeman405e3ec2005-10-21 00:02:42 +0000223 IntDivIsCheap = false;
224 Pow2DivIsCheap = false;
Chris Lattneree4a7652006-01-25 18:57:15 +0000225 StackPointerRegisterToSaveRestore = 0;
Jim Laskey9bb3c932007-02-22 18:04:49 +0000226 ExceptionPointerRegister = 0;
227 ExceptionSelectorRegister = 0;
Chris Lattnerdfe89342007-09-21 17:06:39 +0000228 SetCCResultContents = UndefinedSetCCResult;
Evan Cheng0577a222006-01-25 18:52:42 +0000229 SchedPreferenceInfo = SchedulingForLatency;
Chris Lattner7acf5f32006-09-05 17:39:15 +0000230 JumpBufSize = 0;
Duraid Madina0c9e0ff2006-09-04 07:44:11 +0000231 JumpBufAlignment = 0;
Evan Chengd60483e2007-05-16 23:45:53 +0000232 IfCvtBlockSizeLimit = 2;
Evan Chengfb8075d2008-02-28 00:43:03 +0000233 IfCvtDupBlockSizeLimit = 0;
234 PrefLoopAlignment = 0;
Evan Cheng56966222007-01-12 02:11:51 +0000235
236 InitLibcallNames(LibcallRoutineNames);
Evan Chengd385fd62007-01-31 09:29:11 +0000237 InitCmpLibcallCCs(CmpLibcallCCs);
Dan Gohmanc3b0b5c2007-09-25 15:10:49 +0000238
239 // Tell Legalize whether the assembler supports DEBUG_LOC.
240 if (!TM.getTargetAsmInfo()->hasDotLocAndDotFile())
241 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Chris Lattner310968c2005-01-07 07:44:53 +0000242}
243
Chris Lattnercba82f92005-01-16 07:28:11 +0000244TargetLowering::~TargetLowering() {}
245
Chris Lattner310968c2005-01-07 07:44:53 +0000246/// computeRegisterProperties - Once all of the register classes are added,
247/// this allows us to compute derived properties we expose.
248void TargetLowering::computeRegisterProperties() {
Nate Begeman6a648612005-11-29 05:45:29 +0000249 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattnerbb97d812005-01-16 01:10:58 +0000250 "Too many value types for ValueTypeActions to hold!");
251
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000252 // Everything defaults to needing one register.
253 for (unsigned i = 0; i != MVT::LAST_VALUETYPE; ++i) {
Dan Gohmanb9f10192007-06-21 14:42:22 +0000254 NumRegistersForVT[i] = 1;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000255 RegisterTypeForVT[i] = TransformToType[i] = (MVT::SimpleValueType)i;
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000256 }
257 // ...except isVoid, which doesn't need any registers.
258 NumRegistersForVT[MVT::isVoid] = 0;
Misha Brukmanf976c852005-04-21 22:55:34 +0000259
Chris Lattner310968c2005-01-07 07:44:53 +0000260 // Find the largest integer register class.
Duncan Sands89307632008-06-09 15:48:25 +0000261 unsigned LargestIntReg = MVT::LAST_INTEGER_VALUETYPE;
Chris Lattner310968c2005-01-07 07:44:53 +0000262 for (; RegClassForVT[LargestIntReg] == 0; --LargestIntReg)
263 assert(LargestIntReg != MVT::i1 && "No integer registers defined!");
264
265 // Every integer value type larger than this largest register takes twice as
266 // many registers to represent as the previous ValueType.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000267 for (unsigned ExpandedReg = LargestIntReg + 1; ; ++ExpandedReg) {
268 MVT EVT = (MVT::SimpleValueType)ExpandedReg;
269 if (!EVT.isInteger())
270 break;
Dan Gohmanb9f10192007-06-21 14:42:22 +0000271 NumRegistersForVT[ExpandedReg] = 2*NumRegistersForVT[ExpandedReg-1];
Duncan Sands83ec4b62008-06-06 12:08:01 +0000272 RegisterTypeForVT[ExpandedReg] = (MVT::SimpleValueType)LargestIntReg;
273 TransformToType[ExpandedReg] = (MVT::SimpleValueType)(ExpandedReg - 1);
274 ValueTypeActions.setTypeAction(EVT, Expand);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +0000275 }
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000276
277 // Inspect all of the ValueType's smaller than the largest integer
278 // register to see which ones need promotion.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000279 unsigned LegalIntReg = LargestIntReg;
280 for (unsigned IntReg = LargestIntReg - 1;
281 IntReg >= (unsigned)MVT::i1; --IntReg) {
282 MVT IVT = (MVT::SimpleValueType)IntReg;
283 if (isTypeLegal(IVT)) {
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000284 LegalIntReg = IntReg;
285 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000286 RegisterTypeForVT[IntReg] = TransformToType[IntReg] =
287 (MVT::SimpleValueType)LegalIntReg;
288 ValueTypeActions.setTypeAction(IVT, Promote);
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000289 }
290 }
291
Dale Johannesen161e8972007-10-05 20:04:43 +0000292 // ppcf128 type is really two f64's.
293 if (!isTypeLegal(MVT::ppcf128)) {
294 NumRegistersForVT[MVT::ppcf128] = 2*NumRegistersForVT[MVT::f64];
295 RegisterTypeForVT[MVT::ppcf128] = MVT::f64;
296 TransformToType[MVT::ppcf128] = MVT::f64;
297 ValueTypeActions.setTypeAction(MVT::ppcf128, Expand);
298 }
299
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000300 // Decide how to handle f64. If the target does not have native f64 support,
301 // expand it to i64 and we will be generating soft float library calls.
302 if (!isTypeLegal(MVT::f64)) {
303 NumRegistersForVT[MVT::f64] = NumRegistersForVT[MVT::i64];
304 RegisterTypeForVT[MVT::f64] = RegisterTypeForVT[MVT::i64];
305 TransformToType[MVT::f64] = MVT::i64;
306 ValueTypeActions.setTypeAction(MVT::f64, Expand);
307 }
308
309 // Decide how to handle f32. If the target does not have native support for
310 // f32, promote it to f64 if it is legal. Otherwise, expand it to i32.
311 if (!isTypeLegal(MVT::f32)) {
312 if (isTypeLegal(MVT::f64)) {
313 NumRegistersForVT[MVT::f32] = NumRegistersForVT[MVT::f64];
314 RegisterTypeForVT[MVT::f32] = RegisterTypeForVT[MVT::f64];
315 TransformToType[MVT::f32] = MVT::f64;
316 ValueTypeActions.setTypeAction(MVT::f32, Promote);
317 } else {
318 NumRegistersForVT[MVT::f32] = NumRegistersForVT[MVT::i32];
319 RegisterTypeForVT[MVT::f32] = RegisterTypeForVT[MVT::i32];
320 TransformToType[MVT::f32] = MVT::i32;
321 ValueTypeActions.setTypeAction(MVT::f32, Expand);
322 }
Evan Cheng1a8f1fe2006-12-09 02:42:38 +0000323 }
Nate Begeman4ef3b812005-11-22 01:29:36 +0000324
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000325 // Loop over all of the vector value types to see which need transformations.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000326 for (unsigned i = MVT::FIRST_VECTOR_VALUETYPE;
327 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
328 MVT VT = (MVT::SimpleValueType)i;
329 if (!isTypeLegal(VT)) {
330 MVT IntermediateVT, RegisterVT;
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000331 unsigned NumIntermediates;
332 NumRegistersForVT[i] =
Duncan Sands83ec4b62008-06-06 12:08:01 +0000333 getVectorTypeBreakdown(VT,
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000334 IntermediateVT, NumIntermediates,
335 RegisterVT);
336 RegisterTypeForVT[i] = RegisterVT;
337 TransformToType[i] = MVT::Other; // this isn't actually used
Duncan Sands83ec4b62008-06-06 12:08:01 +0000338 ValueTypeActions.setTypeAction(VT, Expand);
Dan Gohman7f321562007-06-25 16:23:39 +0000339 }
Chris Lattner3a5935842006-03-16 19:50:01 +0000340 }
Chris Lattnerbb97d812005-01-16 01:10:58 +0000341}
Chris Lattnercba82f92005-01-16 07:28:11 +0000342
Evan Cheng72261582005-12-20 06:22:03 +0000343const char *TargetLowering::getTargetNodeName(unsigned Opcode) const {
344 return NULL;
345}
Evan Cheng3a03ebb2005-12-21 23:05:39 +0000346
Scott Michel5b8f82e2008-03-10 15:42:14 +0000347
Duncan Sands83ec4b62008-06-06 12:08:01 +0000348MVT TargetLowering::getSetCCResultType(const SDOperand &) const {
Scott Michel5b8f82e2008-03-10 15:42:14 +0000349 return getValueType(TD->getIntPtrType());
350}
351
352
Dan Gohman7f321562007-06-25 16:23:39 +0000353/// getVectorTypeBreakdown - Vector types are broken down into some number of
354/// legal first class types. For example, MVT::v8f32 maps to 2 MVT::v4f32
Chris Lattnerdc879292006-03-31 00:28:56 +0000355/// with Altivec or SSE1, or 8 promoted MVT::f64 values with the X86 FP stack.
Dan Gohman7f321562007-06-25 16:23:39 +0000356/// Similarly, MVT::v2i64 turns into 4 MVT::i32 values with both PPC and X86.
Chris Lattnerdc879292006-03-31 00:28:56 +0000357///
Dan Gohman7f321562007-06-25 16:23:39 +0000358/// This method returns the number of registers needed, and the VT for each
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000359/// register. It also returns the VT and quantity of the intermediate values
360/// before they are promoted/expanded.
Chris Lattnerdc879292006-03-31 00:28:56 +0000361///
Duncan Sands83ec4b62008-06-06 12:08:01 +0000362unsigned TargetLowering::getVectorTypeBreakdown(MVT VT,
363 MVT &IntermediateVT,
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000364 unsigned &NumIntermediates,
Duncan Sands83ec4b62008-06-06 12:08:01 +0000365 MVT &RegisterVT) const {
Chris Lattnerdc879292006-03-31 00:28:56 +0000366 // Figure out the right, legal destination reg to copy into.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000367 unsigned NumElts = VT.getVectorNumElements();
368 MVT EltTy = VT.getVectorElementType();
Chris Lattnerdc879292006-03-31 00:28:56 +0000369
370 unsigned NumVectorRegs = 1;
371
Nate Begemand73ab882007-11-27 19:28:48 +0000372 // FIXME: We don't support non-power-of-2-sized vectors for now. Ideally we
373 // could break down into LHS/RHS like LegalizeDAG does.
374 if (!isPowerOf2_32(NumElts)) {
375 NumVectorRegs = NumElts;
376 NumElts = 1;
377 }
378
Chris Lattnerdc879292006-03-31 00:28:56 +0000379 // Divide the input until we get to a supported size. This will always
380 // end with a scalar if the target doesn't support vectors.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000381 while (NumElts > 1 && !isTypeLegal(MVT::getVectorVT(EltTy, NumElts))) {
Chris Lattnerdc879292006-03-31 00:28:56 +0000382 NumElts >>= 1;
383 NumVectorRegs <<= 1;
384 }
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000385
386 NumIntermediates = NumVectorRegs;
Chris Lattnerdc879292006-03-31 00:28:56 +0000387
Duncan Sands83ec4b62008-06-06 12:08:01 +0000388 MVT NewVT = MVT::getVectorVT(EltTy, NumElts);
Dan Gohman7f321562007-06-25 16:23:39 +0000389 if (!isTypeLegal(NewVT))
390 NewVT = EltTy;
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000391 IntermediateVT = NewVT;
Chris Lattnerdc879292006-03-31 00:28:56 +0000392
Duncan Sands83ec4b62008-06-06 12:08:01 +0000393 MVT DestVT = getTypeToTransformTo(NewVT);
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000394 RegisterVT = DestVT;
Duncan Sands8e4eb092008-06-08 20:54:56 +0000395 if (DestVT.bitsLT(NewVT)) {
Chris Lattnerdc879292006-03-31 00:28:56 +0000396 // Value is expanded, e.g. i64 -> i16.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000397 return NumVectorRegs*(NewVT.getSizeInBits()/DestVT.getSizeInBits());
Chris Lattnerdc879292006-03-31 00:28:56 +0000398 } else {
399 // Otherwise, promotion or legal types use the same number of registers as
400 // the vector decimated to the appropriate level.
Chris Lattner79227e22006-03-31 00:46:36 +0000401 return NumVectorRegs;
Chris Lattnerdc879292006-03-31 00:28:56 +0000402 }
403
Evan Chenge9b3da12006-05-17 18:10:06 +0000404 return 1;
Chris Lattnerdc879292006-03-31 00:28:56 +0000405}
406
Evan Cheng3ae05432008-01-24 00:22:01 +0000407/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
Dale Johannesen28d08fd2008-02-28 22:31:51 +0000408/// function arguments in the caller parameter area. This is the actual
409/// alignment, not its logarithm.
Evan Cheng3ae05432008-01-24 00:22:01 +0000410unsigned TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Dale Johannesen28d08fd2008-02-28 22:31:51 +0000411 return TD->getCallFrameTypeAlignment(Ty);
Evan Cheng3ae05432008-01-24 00:22:01 +0000412}
413
Evan Chengcc415862007-11-09 01:32:10 +0000414SDOperand TargetLowering::getPICJumpTableRelocBase(SDOperand Table,
415 SelectionDAG &DAG) const {
416 if (usesGlobalOffsetTable())
417 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
418 return Table;
419}
420
Chris Lattnereb8146b2006-02-04 02:13:02 +0000421//===----------------------------------------------------------------------===//
422// Optimization Methods
423//===----------------------------------------------------------------------===//
424
Nate Begeman368e18d2006-02-16 21:11:51 +0000425/// ShrinkDemandedConstant - Check to see if the specified operand of the
426/// specified instruction is a constant integer. If so, check to see if there
427/// are any bits set in the constant that are not demanded. If so, shrink the
428/// constant and return true.
429bool TargetLowering::TargetLoweringOpt::ShrinkDemandedConstant(SDOperand Op,
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000430 const APInt &Demanded) {
Chris Lattnerec665152006-02-26 23:36:02 +0000431 // FIXME: ISD::SELECT, ISD::SELECT_CC
Nate Begeman368e18d2006-02-16 21:11:51 +0000432 switch(Op.getOpcode()) {
433 default: break;
Nate Begemande996292006-02-03 22:24:05 +0000434 case ISD::AND:
Nate Begeman368e18d2006-02-16 21:11:51 +0000435 case ISD::OR:
436 case ISD::XOR:
437 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000438 if (C->getAPIntValue().intersects(~Demanded)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000439 MVT VT = Op.getValueType();
Nate Begeman368e18d2006-02-16 21:11:51 +0000440 SDOperand New = DAG.getNode(Op.getOpcode(), VT, Op.getOperand(0),
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000441 DAG.getConstant(Demanded &
442 C->getAPIntValue(),
Nate Begeman368e18d2006-02-16 21:11:51 +0000443 VT));
444 return CombineTo(Op, New);
Nate Begemande996292006-02-03 22:24:05 +0000445 }
Nate Begemande996292006-02-03 22:24:05 +0000446 break;
447 }
448 return false;
449}
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000450
Nate Begeman368e18d2006-02-16 21:11:51 +0000451/// SimplifyDemandedBits - Look at Op. At this point, we know that only the
452/// DemandedMask bits of the result of Op are ever used downstream. If we can
453/// use this information to simplify Op, create a new simplified DAG node and
454/// return true, returning the original and new nodes in Old and New. Otherwise,
455/// analyze the expression and return a mask of KnownOne and KnownZero bits for
456/// the expression (used to simplify the caller). The KnownZero/One bits may
457/// only be accurate for those bits in the DemandedMask.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000458bool TargetLowering::SimplifyDemandedBits(SDOperand Op,
459 const APInt &DemandedMask,
460 APInt &KnownZero,
461 APInt &KnownOne,
Nate Begeman368e18d2006-02-16 21:11:51 +0000462 TargetLoweringOpt &TLO,
463 unsigned Depth) const {
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000464 unsigned BitWidth = DemandedMask.getBitWidth();
465 assert(Op.getValueSizeInBits() == BitWidth &&
466 "Mask size mismatches value type size!");
467 APInt NewMask = DemandedMask;
Chris Lattner3fc5b012007-05-17 18:19:23 +0000468
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000469 // Don't know anything.
470 KnownZero = KnownOne = APInt(BitWidth, 0);
471
Nate Begeman368e18d2006-02-16 21:11:51 +0000472 // Other users may use these bits.
473 if (!Op.Val->hasOneUse()) {
474 if (Depth != 0) {
475 // If not at the root, Just compute the KnownZero/KnownOne bits to
476 // simplify things downstream.
Dan Gohmanea859be2007-06-22 14:59:07 +0000477 TLO.DAG.ComputeMaskedBits(Op, DemandedMask, KnownZero, KnownOne, Depth);
Nate Begeman368e18d2006-02-16 21:11:51 +0000478 return false;
479 }
480 // If this is the root being simplified, allow it to have multiple uses,
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000481 // just set the NewMask to all bits.
482 NewMask = APInt::getAllOnesValue(BitWidth);
Nate Begeman368e18d2006-02-16 21:11:51 +0000483 } else if (DemandedMask == 0) {
484 // Not demanding any bits from Op.
485 if (Op.getOpcode() != ISD::UNDEF)
486 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::UNDEF, Op.getValueType()));
487 return false;
488 } else if (Depth == 6) { // Limit search depth.
489 return false;
490 }
491
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000492 APInt KnownZero2, KnownOne2, KnownZeroOut, KnownOneOut;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000493 switch (Op.getOpcode()) {
494 case ISD::Constant:
Nate Begeman368e18d2006-02-16 21:11:51 +0000495 // We know all of the bits for a constant!
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000496 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & NewMask;
497 KnownZero = ~KnownOne & NewMask;
Chris Lattnerec665152006-02-26 23:36:02 +0000498 return false; // Don't fall through, will infinitely loop.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000499 case ISD::AND:
Chris Lattner81cd3552006-02-27 00:36:27 +0000500 // If the RHS is a constant, check to see if the LHS would be zero without
501 // using the bits from the RHS. Below, we use knowledge about the RHS to
502 // simplify the LHS, here we're using information from the LHS to simplify
503 // the RHS.
504 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000505 APInt LHSZero, LHSOne;
506 TLO.DAG.ComputeMaskedBits(Op.getOperand(0), NewMask,
Dan Gohmanea859be2007-06-22 14:59:07 +0000507 LHSZero, LHSOne, Depth+1);
Chris Lattner81cd3552006-02-27 00:36:27 +0000508 // If the LHS already has zeros where RHSC does, this and is dead.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000509 if ((LHSZero & NewMask) == (~RHSC->getAPIntValue() & NewMask))
Chris Lattner81cd3552006-02-27 00:36:27 +0000510 return TLO.CombineTo(Op, Op.getOperand(0));
511 // If any of the set bits in the RHS are known zero on the LHS, shrink
512 // the constant.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000513 if (TLO.ShrinkDemandedConstant(Op, ~LHSZero & NewMask))
Chris Lattner81cd3552006-02-27 00:36:27 +0000514 return true;
515 }
516
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000517 if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero,
Nate Begeman368e18d2006-02-16 21:11:51 +0000518 KnownOne, TLO, Depth+1))
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000519 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000520 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000521 if (SimplifyDemandedBits(Op.getOperand(0), ~KnownZero & NewMask,
Nate Begeman368e18d2006-02-16 21:11:51 +0000522 KnownZero2, KnownOne2, TLO, Depth+1))
523 return true;
524 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
525
526 // If all of the demanded bits are known one on one side, return the other.
527 // These bits cannot contribute to the result of the 'and'.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000528 if ((NewMask & ~KnownZero2 & KnownOne) == (~KnownZero2 & NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +0000529 return TLO.CombineTo(Op, Op.getOperand(0));
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000530 if ((NewMask & ~KnownZero & KnownOne2) == (~KnownZero & NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +0000531 return TLO.CombineTo(Op, Op.getOperand(1));
532 // If all of the demanded bits in the inputs are known zeros, return zero.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000533 if ((NewMask & (KnownZero|KnownZero2)) == NewMask)
Nate Begeman368e18d2006-02-16 21:11:51 +0000534 return TLO.CombineTo(Op, TLO.DAG.getConstant(0, Op.getValueType()));
535 // If the RHS is a constant, see if we can simplify it.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000536 if (TLO.ShrinkDemandedConstant(Op, ~KnownZero2 & NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +0000537 return true;
Chris Lattner5f0c6582006-02-27 00:22:28 +0000538
Nate Begeman368e18d2006-02-16 21:11:51 +0000539 // Output known-1 bits are only known if set in both the LHS & RHS.
540 KnownOne &= KnownOne2;
541 // Output known-0 are known to be clear if zero in either the LHS | RHS.
542 KnownZero |= KnownZero2;
543 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000544 case ISD::OR:
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000545 if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero,
Nate Begeman368e18d2006-02-16 21:11:51 +0000546 KnownOne, TLO, Depth+1))
547 return true;
548 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000549 if (SimplifyDemandedBits(Op.getOperand(0), ~KnownOne & NewMask,
Nate Begeman368e18d2006-02-16 21:11:51 +0000550 KnownZero2, KnownOne2, TLO, Depth+1))
551 return true;
552 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
553
554 // If all of the demanded bits are known zero on one side, return the other.
555 // These bits cannot contribute to the result of the 'or'.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000556 if ((NewMask & ~KnownOne2 & KnownZero) == (~KnownOne2 & NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +0000557 return TLO.CombineTo(Op, Op.getOperand(0));
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000558 if ((NewMask & ~KnownOne & KnownZero2) == (~KnownOne & NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +0000559 return TLO.CombineTo(Op, Op.getOperand(1));
560 // If all of the potentially set bits on one side are known to be set on
561 // the other side, just use the 'other' side.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000562 if ((NewMask & ~KnownZero & KnownOne2) == (~KnownZero & NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +0000563 return TLO.CombineTo(Op, Op.getOperand(0));
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000564 if ((NewMask & ~KnownZero2 & KnownOne) == (~KnownZero2 & NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +0000565 return TLO.CombineTo(Op, Op.getOperand(1));
566 // If the RHS is a constant, see if we can simplify it.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000567 if (TLO.ShrinkDemandedConstant(Op, NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +0000568 return true;
569
570 // Output known-0 bits are only known if clear in both the LHS & RHS.
571 KnownZero &= KnownZero2;
572 // Output known-1 are known to be set if set in either the LHS | RHS.
573 KnownOne |= KnownOne2;
574 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000575 case ISD::XOR:
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000576 if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero,
Nate Begeman368e18d2006-02-16 21:11:51 +0000577 KnownOne, TLO, Depth+1))
578 return true;
579 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000580 if (SimplifyDemandedBits(Op.getOperand(0), NewMask, KnownZero2,
Nate Begeman368e18d2006-02-16 21:11:51 +0000581 KnownOne2, TLO, Depth+1))
582 return true;
583 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
584
585 // If all of the demanded bits are known zero on one side, return the other.
586 // These bits cannot contribute to the result of the 'xor'.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000587 if ((KnownZero & NewMask) == NewMask)
Nate Begeman368e18d2006-02-16 21:11:51 +0000588 return TLO.CombineTo(Op, Op.getOperand(0));
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000589 if ((KnownZero2 & NewMask) == NewMask)
Nate Begeman368e18d2006-02-16 21:11:51 +0000590 return TLO.CombineTo(Op, Op.getOperand(1));
Chris Lattner3687c1a2006-11-27 21:50:02 +0000591
592 // If all of the unknown bits are known to be zero on one side or the other
593 // (but not both) turn this into an *inclusive* or.
594 // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000595 if ((NewMask & ~KnownZero & ~KnownZero2) == 0)
Chris Lattner3687c1a2006-11-27 21:50:02 +0000596 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::OR, Op.getValueType(),
597 Op.getOperand(0),
598 Op.getOperand(1)));
Nate Begeman368e18d2006-02-16 21:11:51 +0000599
600 // Output known-0 bits are known if clear or set in both the LHS & RHS.
601 KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
602 // Output known-1 are known to be set if set in only one of the LHS, RHS.
603 KnownOneOut = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
604
Nate Begeman368e18d2006-02-16 21:11:51 +0000605 // If all of the demanded bits on one side are known, and all of the set
606 // bits on that side are also known to be set on the other side, turn this
607 // into an AND, as we know the bits will be cleared.
608 // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000609 if ((NewMask & (KnownZero|KnownOne)) == NewMask) { // all known
Nate Begeman368e18d2006-02-16 21:11:51 +0000610 if ((KnownOne & KnownOne2) == KnownOne) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000611 MVT VT = Op.getValueType();
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000612 SDOperand ANDC = TLO.DAG.getConstant(~KnownOne & NewMask, VT);
Nate Begeman368e18d2006-02-16 21:11:51 +0000613 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::AND, VT, Op.getOperand(0),
614 ANDC));
615 }
616 }
617
618 // If the RHS is a constant, see if we can simplify it.
Torok Edwin4fea2e92008-04-06 21:23:02 +0000619 // for XOR, we prefer to force bits to 1 if they will make a -1.
620 // if we can't force bits, try to shrink constant
621 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
622 APInt Expanded = C->getAPIntValue() | (~NewMask);
623 // if we can expand it to have all bits set, do it
624 if (Expanded.isAllOnesValue()) {
625 if (Expanded != C->getAPIntValue()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000626 MVT VT = Op.getValueType();
Torok Edwin4fea2e92008-04-06 21:23:02 +0000627 SDOperand New = TLO.DAG.getNode(Op.getOpcode(), VT, Op.getOperand(0),
628 TLO.DAG.getConstant(Expanded, VT));
629 return TLO.CombineTo(Op, New);
630 }
631 // if it already has all the bits set, nothing to change
632 // but don't shrink either!
633 } else if (TLO.ShrinkDemandedConstant(Op, NewMask)) {
634 return true;
635 }
636 }
637
Nate Begeman368e18d2006-02-16 21:11:51 +0000638 KnownZero = KnownZeroOut;
639 KnownOne = KnownOneOut;
640 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000641 case ISD::SELECT:
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000642 if (SimplifyDemandedBits(Op.getOperand(2), NewMask, KnownZero,
Nate Begeman368e18d2006-02-16 21:11:51 +0000643 KnownOne, TLO, Depth+1))
644 return true;
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000645 if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero2,
Nate Begeman368e18d2006-02-16 21:11:51 +0000646 KnownOne2, TLO, Depth+1))
647 return true;
648 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
649 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
650
651 // If the operands are constants, see if we can simplify them.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000652 if (TLO.ShrinkDemandedConstant(Op, NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +0000653 return true;
654
655 // Only known if known in both the LHS and RHS.
656 KnownOne &= KnownOne2;
657 KnownZero &= KnownZero2;
658 break;
Chris Lattnerec665152006-02-26 23:36:02 +0000659 case ISD::SELECT_CC:
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000660 if (SimplifyDemandedBits(Op.getOperand(3), NewMask, KnownZero,
Chris Lattnerec665152006-02-26 23:36:02 +0000661 KnownOne, TLO, Depth+1))
662 return true;
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000663 if (SimplifyDemandedBits(Op.getOperand(2), NewMask, KnownZero2,
Chris Lattnerec665152006-02-26 23:36:02 +0000664 KnownOne2, TLO, Depth+1))
665 return true;
666 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
667 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
668
669 // If the operands are constants, see if we can simplify them.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000670 if (TLO.ShrinkDemandedConstant(Op, NewMask))
Chris Lattnerec665152006-02-26 23:36:02 +0000671 return true;
672
673 // Only known if known in both the LHS and RHS.
674 KnownOne &= KnownOne2;
675 KnownZero &= KnownZero2;
676 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000677 case ISD::SHL:
Nate Begeman368e18d2006-02-16 21:11:51 +0000678 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner895c4ab2007-04-17 21:14:16 +0000679 unsigned ShAmt = SA->getValue();
680 SDOperand InOp = Op.getOperand(0);
681
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000682 // If the shift count is an invalid immediate, don't do anything.
683 if (ShAmt >= BitWidth)
684 break;
685
Chris Lattner895c4ab2007-04-17 21:14:16 +0000686 // If this is ((X >>u C1) << ShAmt), see if we can simplify this into a
687 // single shift. We can do this if the bottom bits (which are shifted
688 // out) are never demanded.
689 if (InOp.getOpcode() == ISD::SRL &&
690 isa<ConstantSDNode>(InOp.getOperand(1))) {
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000691 if (ShAmt && (NewMask & APInt::getLowBitsSet(BitWidth, ShAmt)) == 0) {
Chris Lattner895c4ab2007-04-17 21:14:16 +0000692 unsigned C1 = cast<ConstantSDNode>(InOp.getOperand(1))->getValue();
693 unsigned Opc = ISD::SHL;
694 int Diff = ShAmt-C1;
695 if (Diff < 0) {
696 Diff = -Diff;
697 Opc = ISD::SRL;
698 }
699
700 SDOperand NewSA =
Chris Lattner4e7e6cd2007-05-30 16:30:06 +0000701 TLO.DAG.getConstant(Diff, Op.getOperand(1).getValueType());
Duncan Sands83ec4b62008-06-06 12:08:01 +0000702 MVT VT = Op.getValueType();
Chris Lattner0a16a1f2007-04-18 03:01:40 +0000703 return TLO.CombineTo(Op, TLO.DAG.getNode(Opc, VT,
Chris Lattner895c4ab2007-04-17 21:14:16 +0000704 InOp.getOperand(0), NewSA));
705 }
706 }
707
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000708 if (SimplifyDemandedBits(Op.getOperand(0), NewMask.lshr(ShAmt),
Nate Begeman368e18d2006-02-16 21:11:51 +0000709 KnownZero, KnownOne, TLO, Depth+1))
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000710 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000711 KnownZero <<= SA->getValue();
712 KnownOne <<= SA->getValue();
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000713 // low bits known zero.
714 KnownZero |= APInt::getLowBitsSet(BitWidth, SA->getValue());
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000715 }
716 break;
Nate Begeman368e18d2006-02-16 21:11:51 +0000717 case ISD::SRL:
718 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000719 MVT VT = Op.getValueType();
Nate Begeman368e18d2006-02-16 21:11:51 +0000720 unsigned ShAmt = SA->getValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000721 unsigned VTSize = VT.getSizeInBits();
Chris Lattner895c4ab2007-04-17 21:14:16 +0000722 SDOperand InOp = Op.getOperand(0);
723
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000724 // If the shift count is an invalid immediate, don't do anything.
725 if (ShAmt >= BitWidth)
726 break;
727
Chris Lattner895c4ab2007-04-17 21:14:16 +0000728 // If this is ((X << C1) >>u ShAmt), see if we can simplify this into a
729 // single shift. We can do this if the top bits (which are shifted out)
730 // are never demanded.
731 if (InOp.getOpcode() == ISD::SHL &&
732 isa<ConstantSDNode>(InOp.getOperand(1))) {
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000733 if (ShAmt && (NewMask & APInt::getHighBitsSet(VTSize, ShAmt)) == 0) {
Chris Lattner895c4ab2007-04-17 21:14:16 +0000734 unsigned C1 = cast<ConstantSDNode>(InOp.getOperand(1))->getValue();
735 unsigned Opc = ISD::SRL;
736 int Diff = ShAmt-C1;
737 if (Diff < 0) {
738 Diff = -Diff;
739 Opc = ISD::SHL;
740 }
741
742 SDOperand NewSA =
Chris Lattner8c7d2d52007-04-17 22:53:02 +0000743 TLO.DAG.getConstant(Diff, Op.getOperand(1).getValueType());
Chris Lattner895c4ab2007-04-17 21:14:16 +0000744 return TLO.CombineTo(Op, TLO.DAG.getNode(Opc, VT,
745 InOp.getOperand(0), NewSA));
746 }
747 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000748
749 // Compute the new bits that are at the top now.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000750 if (SimplifyDemandedBits(InOp, (NewMask << ShAmt),
Nate Begeman368e18d2006-02-16 21:11:51 +0000751 KnownZero, KnownOne, TLO, Depth+1))
752 return true;
753 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000754 KnownZero = KnownZero.lshr(ShAmt);
755 KnownOne = KnownOne.lshr(ShAmt);
Chris Lattnerc4fa6032006-06-13 16:52:37 +0000756
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000757 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Chris Lattnerc4fa6032006-06-13 16:52:37 +0000758 KnownZero |= HighBits; // High bits known zero.
Nate Begeman368e18d2006-02-16 21:11:51 +0000759 }
760 break;
761 case ISD::SRA:
762 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000763 MVT VT = Op.getValueType();
Nate Begeman368e18d2006-02-16 21:11:51 +0000764 unsigned ShAmt = SA->getValue();
765
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000766 // If the shift count is an invalid immediate, don't do anything.
767 if (ShAmt >= BitWidth)
768 break;
769
770 APInt InDemandedMask = (NewMask << ShAmt);
Chris Lattner1b737132006-05-08 17:22:53 +0000771
772 // If any of the demanded bits are produced by the sign extension, we also
773 // demand the input sign bit.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000774 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
775 if (HighBits.intersects(NewMask))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000776 InDemandedMask |= APInt::getSignBit(VT.getSizeInBits());
Chris Lattner1b737132006-05-08 17:22:53 +0000777
778 if (SimplifyDemandedBits(Op.getOperand(0), InDemandedMask,
Nate Begeman368e18d2006-02-16 21:11:51 +0000779 KnownZero, KnownOne, TLO, Depth+1))
780 return true;
781 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000782 KnownZero = KnownZero.lshr(ShAmt);
783 KnownOne = KnownOne.lshr(ShAmt);
Nate Begeman368e18d2006-02-16 21:11:51 +0000784
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000785 // Handle the sign bit, adjusted to where it is now in the mask.
786 APInt SignBit = APInt::getSignBit(BitWidth).lshr(ShAmt);
Nate Begeman368e18d2006-02-16 21:11:51 +0000787
788 // If the input sign bit is known to be zero, or if none of the top bits
789 // are demanded, turn this into an unsigned shift right.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000790 if (KnownZero.intersects(SignBit) || (HighBits & ~NewMask) == HighBits) {
Nate Begeman368e18d2006-02-16 21:11:51 +0000791 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, VT, Op.getOperand(0),
792 Op.getOperand(1)));
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000793 } else if (KnownOne.intersects(SignBit)) { // New bits are known one.
Nate Begeman368e18d2006-02-16 21:11:51 +0000794 KnownOne |= HighBits;
795 }
796 }
797 break;
798 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000799 MVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Nate Begeman368e18d2006-02-16 21:11:51 +0000800
Chris Lattnerec665152006-02-26 23:36:02 +0000801 // Sign extension. Compute the demanded bits in the result that are not
Nate Begeman368e18d2006-02-16 21:11:51 +0000802 // present in the input.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000803 APInt NewBits = APInt::getHighBitsSet(BitWidth,
Duncan Sands83ec4b62008-06-06 12:08:01 +0000804 BitWidth - EVT.getSizeInBits()) &
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000805 NewMask;
Nate Begeman368e18d2006-02-16 21:11:51 +0000806
Chris Lattnerec665152006-02-26 23:36:02 +0000807 // If none of the extended bits are demanded, eliminate the sextinreg.
808 if (NewBits == 0)
809 return TLO.CombineTo(Op, Op.getOperand(0));
810
Duncan Sands83ec4b62008-06-06 12:08:01 +0000811 APInt InSignBit = APInt::getSignBit(EVT.getSizeInBits());
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000812 InSignBit.zext(BitWidth);
813 APInt InputDemandedBits = APInt::getLowBitsSet(BitWidth,
Duncan Sands83ec4b62008-06-06 12:08:01 +0000814 EVT.getSizeInBits()) &
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000815 NewMask;
Nate Begeman368e18d2006-02-16 21:11:51 +0000816
Chris Lattnerec665152006-02-26 23:36:02 +0000817 // Since the sign extended bits are demanded, we know that the sign
Nate Begeman368e18d2006-02-16 21:11:51 +0000818 // bit is demanded.
Chris Lattnerec665152006-02-26 23:36:02 +0000819 InputDemandedBits |= InSignBit;
Nate Begeman368e18d2006-02-16 21:11:51 +0000820
821 if (SimplifyDemandedBits(Op.getOperand(0), InputDemandedBits,
822 KnownZero, KnownOne, TLO, Depth+1))
823 return true;
824 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
825
826 // If the sign bit of the input is known set or clear, then we know the
827 // top bits of the result.
828
Chris Lattnerec665152006-02-26 23:36:02 +0000829 // If the input sign bit is known zero, convert this into a zero extension.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000830 if (KnownZero.intersects(InSignBit))
Chris Lattnerec665152006-02-26 23:36:02 +0000831 return TLO.CombineTo(Op,
832 TLO.DAG.getZeroExtendInReg(Op.getOperand(0), EVT));
833
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000834 if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Nate Begeman368e18d2006-02-16 21:11:51 +0000835 KnownOne |= NewBits;
836 KnownZero &= ~NewBits;
Chris Lattnerec665152006-02-26 23:36:02 +0000837 } else { // Input sign bit unknown
Nate Begeman368e18d2006-02-16 21:11:51 +0000838 KnownZero &= ~NewBits;
839 KnownOne &= ~NewBits;
840 }
841 break;
842 }
Chris Lattnerec665152006-02-26 23:36:02 +0000843 case ISD::ZERO_EXTEND: {
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000844 unsigned OperandBitWidth = Op.getOperand(0).getValueSizeInBits();
845 APInt InMask = NewMask;
846 InMask.trunc(OperandBitWidth);
Chris Lattnerec665152006-02-26 23:36:02 +0000847
848 // If none of the top bits are demanded, convert this into an any_extend.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000849 APInt NewBits =
850 APInt::getHighBitsSet(BitWidth, BitWidth - OperandBitWidth) & NewMask;
851 if (!NewBits.intersects(NewMask))
Chris Lattnerec665152006-02-26 23:36:02 +0000852 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ANY_EXTEND,
853 Op.getValueType(),
854 Op.getOperand(0)));
855
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000856 if (SimplifyDemandedBits(Op.getOperand(0), InMask,
Chris Lattnerec665152006-02-26 23:36:02 +0000857 KnownZero, KnownOne, TLO, Depth+1))
858 return true;
859 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000860 KnownZero.zext(BitWidth);
861 KnownOne.zext(BitWidth);
Chris Lattnerec665152006-02-26 23:36:02 +0000862 KnownZero |= NewBits;
863 break;
864 }
865 case ISD::SIGN_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000866 MVT InVT = Op.getOperand(0).getValueType();
867 unsigned InBits = InVT.getSizeInBits();
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000868 APInt InMask = APInt::getLowBitsSet(BitWidth, InBits);
Dan Gohman97360282008-03-11 21:29:43 +0000869 APInt InSignBit = APInt::getBitsSet(BitWidth, InBits - 1, InBits);
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000870 APInt NewBits = ~InMask & NewMask;
Chris Lattnerec665152006-02-26 23:36:02 +0000871
872 // If none of the top bits are demanded, convert this into an any_extend.
873 if (NewBits == 0)
Chris Lattnerfea997a2007-02-01 04:55:59 +0000874 return TLO.CombineTo(Op,TLO.DAG.getNode(ISD::ANY_EXTEND,Op.getValueType(),
Chris Lattnerec665152006-02-26 23:36:02 +0000875 Op.getOperand(0)));
876
877 // Since some of the sign extended bits are demanded, we know that the sign
878 // bit is demanded.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000879 APInt InDemandedBits = InMask & NewMask;
Chris Lattnerec665152006-02-26 23:36:02 +0000880 InDemandedBits |= InSignBit;
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000881 InDemandedBits.trunc(InBits);
Chris Lattnerec665152006-02-26 23:36:02 +0000882
883 if (SimplifyDemandedBits(Op.getOperand(0), InDemandedBits, KnownZero,
884 KnownOne, TLO, Depth+1))
885 return true;
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000886 KnownZero.zext(BitWidth);
887 KnownOne.zext(BitWidth);
Chris Lattnerec665152006-02-26 23:36:02 +0000888
889 // If the sign bit is known zero, convert this to a zero extend.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000890 if (KnownZero.intersects(InSignBit))
Chris Lattnerec665152006-02-26 23:36:02 +0000891 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ZERO_EXTEND,
892 Op.getValueType(),
893 Op.getOperand(0)));
894
895 // If the sign bit is known one, the top bits match.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000896 if (KnownOne.intersects(InSignBit)) {
Chris Lattnerec665152006-02-26 23:36:02 +0000897 KnownOne |= NewBits;
898 KnownZero &= ~NewBits;
899 } else { // Otherwise, top bits aren't known.
900 KnownOne &= ~NewBits;
901 KnownZero &= ~NewBits;
902 }
903 break;
904 }
905 case ISD::ANY_EXTEND: {
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000906 unsigned OperandBitWidth = Op.getOperand(0).getValueSizeInBits();
907 APInt InMask = NewMask;
908 InMask.trunc(OperandBitWidth);
909 if (SimplifyDemandedBits(Op.getOperand(0), InMask,
Chris Lattnerec665152006-02-26 23:36:02 +0000910 KnownZero, KnownOne, TLO, Depth+1))
911 return true;
912 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000913 KnownZero.zext(BitWidth);
914 KnownOne.zext(BitWidth);
Chris Lattnerec665152006-02-26 23:36:02 +0000915 break;
916 }
Chris Lattnerfe8babf2006-05-05 22:32:12 +0000917 case ISD::TRUNCATE: {
Chris Lattnerc93dfda2006-05-06 00:11:52 +0000918 // Simplify the input, using demanded bit information, and compute the known
919 // zero/one bits live out.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000920 APInt TruncMask = NewMask;
921 TruncMask.zext(Op.getOperand(0).getValueSizeInBits());
922 if (SimplifyDemandedBits(Op.getOperand(0), TruncMask,
Chris Lattnerfe8babf2006-05-05 22:32:12 +0000923 KnownZero, KnownOne, TLO, Depth+1))
924 return true;
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000925 KnownZero.trunc(BitWidth);
926 KnownOne.trunc(BitWidth);
Chris Lattnerc93dfda2006-05-06 00:11:52 +0000927
928 // If the input is only used by this truncate, see if we can shrink it based
929 // on the known demanded bits.
930 if (Op.getOperand(0).Val->hasOneUse()) {
931 SDOperand In = Op.getOperand(0);
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000932 unsigned InBitWidth = In.getValueSizeInBits();
Chris Lattnerc93dfda2006-05-06 00:11:52 +0000933 switch (In.getOpcode()) {
934 default: break;
935 case ISD::SRL:
936 // Shrink SRL by a constant if none of the high bits shifted in are
937 // demanded.
938 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(In.getOperand(1))){
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000939 APInt HighBits = APInt::getHighBitsSet(InBitWidth,
940 InBitWidth - BitWidth);
941 HighBits = HighBits.lshr(ShAmt->getValue());
942 HighBits.trunc(BitWidth);
Chris Lattnerc93dfda2006-05-06 00:11:52 +0000943
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000944 if (ShAmt->getValue() < BitWidth && !(HighBits & NewMask)) {
Chris Lattnerc93dfda2006-05-06 00:11:52 +0000945 // None of the shifted in bits are needed. Add a truncate of the
946 // shift input, then shift it.
947 SDOperand NewTrunc = TLO.DAG.getNode(ISD::TRUNCATE,
948 Op.getValueType(),
949 In.getOperand(0));
950 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL,Op.getValueType(),
951 NewTrunc, In.getOperand(1)));
952 }
953 }
954 break;
955 }
956 }
957
Chris Lattnerfe8babf2006-05-05 22:32:12 +0000958 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Chris Lattnerfe8babf2006-05-05 22:32:12 +0000959 break;
960 }
Chris Lattnerec665152006-02-26 23:36:02 +0000961 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000962 MVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000963 APInt InMask = APInt::getLowBitsSet(BitWidth,
Duncan Sands83ec4b62008-06-06 12:08:01 +0000964 VT.getSizeInBits());
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000965 if (SimplifyDemandedBits(Op.getOperand(0), InMask & NewMask,
Chris Lattnerec665152006-02-26 23:36:02 +0000966 KnownZero, KnownOne, TLO, Depth+1))
967 return true;
968 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000969 KnownZero |= ~InMask & NewMask;
Chris Lattnerec665152006-02-26 23:36:02 +0000970 break;
971 }
Chris Lattner2ceb2cf2007-12-22 21:35:38 +0000972 case ISD::BIT_CONVERT:
973#if 0
974 // If this is an FP->Int bitcast and if the sign bit is the only thing that
975 // is demanded, turn this into a FGETSIGN.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000976 if (NewMask == MVT::getIntegerVTSignBit(Op.getValueType()) &&
Chris Lattner2ceb2cf2007-12-22 21:35:38 +0000977 MVT::isFloatingPoint(Op.getOperand(0).getValueType()) &&
978 !MVT::isVector(Op.getOperand(0).getValueType())) {
979 // Only do this xform if FGETSIGN is valid or if before legalize.
980 if (!TLO.AfterLegalize ||
981 isOperationLegal(ISD::FGETSIGN, Op.getValueType())) {
982 // Make a FGETSIGN + SHL to move the sign bit into the appropriate
983 // place. We expect the SHL to be eliminated by other optimizations.
984 SDOperand Sign = TLO.DAG.getNode(ISD::FGETSIGN, Op.getValueType(),
985 Op.getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +0000986 unsigned ShVal = Op.getValueType().getSizeInBits()-1;
Chris Lattner2ceb2cf2007-12-22 21:35:38 +0000987 SDOperand ShAmt = TLO.DAG.getConstant(ShVal, getShiftAmountTy());
988 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SHL, Op.getValueType(),
989 Sign, ShAmt));
990 }
991 }
992#endif
993 break;
Dan Gohman54eed372008-05-06 00:53:29 +0000994 default:
Chris Lattner1482b5f2006-04-02 06:15:09 +0000995 // Just use ComputeMaskedBits to compute output bits.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000996 TLO.DAG.ComputeMaskedBits(Op, NewMask, KnownZero, KnownOne, Depth);
Chris Lattnera6bc5a42006-02-27 01:00:42 +0000997 break;
Nate Begeman368e18d2006-02-16 21:11:51 +0000998 }
Chris Lattnerec665152006-02-26 23:36:02 +0000999
1000 // If we know the value of all of the demanded bits, return this as a
1001 // constant.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001002 if ((NewMask & (KnownZero|KnownOne)) == NewMask)
Chris Lattnerec665152006-02-26 23:36:02 +00001003 return TLO.CombineTo(Op, TLO.DAG.getConstant(KnownOne, Op.getValueType()));
1004
Nate Begeman368e18d2006-02-16 21:11:51 +00001005 return false;
1006}
1007
Nate Begeman368e18d2006-02-16 21:11:51 +00001008/// computeMaskedBitsForTargetNode - Determine which of the bits specified
1009/// in Mask are known to be either zero or one and return them in the
1010/// KnownZero/KnownOne bitsets.
1011void TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00001012 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001013 APInt &KnownZero,
1014 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00001015 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00001016 unsigned Depth) const {
Chris Lattner1b5232a2006-04-02 06:19:46 +00001017 assert((Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1018 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1019 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1020 Op.getOpcode() == ISD::INTRINSIC_VOID) &&
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001021 "Should use MaskedValueIsZero if you don't know whether Op"
1022 " is a target node!");
Dan Gohman977a76f2008-02-13 22:28:48 +00001023 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Cheng3a03ebb2005-12-21 23:05:39 +00001024}
Chris Lattner4ccb0702006-01-26 20:37:03 +00001025
Chris Lattner5c3e21d2006-05-06 09:27:13 +00001026/// ComputeNumSignBitsForTargetNode - This method can be implemented by
1027/// targets that want to expose additional information about sign bits to the
1028/// DAG Combiner.
1029unsigned TargetLowering::ComputeNumSignBitsForTargetNode(SDOperand Op,
1030 unsigned Depth) const {
1031 assert((Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1032 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1033 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1034 Op.getOpcode() == ISD::INTRINSIC_VOID) &&
1035 "Should use ComputeNumSignBits if you don't know whether Op"
1036 " is a target node!");
1037 return 1;
1038}
1039
1040
Evan Chengfa1eb272007-02-08 22:13:59 +00001041/// SimplifySetCC - Try to simplify a setcc built with the specified operands
1042/// and cc. If it is unable to simplify it, return a null SDOperand.
1043SDOperand
Duncan Sands83ec4b62008-06-06 12:08:01 +00001044TargetLowering::SimplifySetCC(MVT VT, SDOperand N0, SDOperand N1,
Evan Chengfa1eb272007-02-08 22:13:59 +00001045 ISD::CondCode Cond, bool foldBooleans,
1046 DAGCombinerInfo &DCI) const {
1047 SelectionDAG &DAG = DCI.DAG;
1048
1049 // These setcc operations always fold.
1050 switch (Cond) {
1051 default: break;
1052 case ISD::SETFALSE:
1053 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
1054 case ISD::SETTRUE:
1055 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
1056 }
1057
1058 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
Dan Gohman6c6cd1c2008-03-03 22:22:56 +00001059 const APInt &C1 = N1C->getAPIntValue();
Evan Chengfa1eb272007-02-08 22:13:59 +00001060 if (isa<ConstantSDNode>(N0.Val)) {
1061 return DAG.FoldSetCC(VT, N0, N1, Cond);
1062 } else {
1063 // If the LHS is '(srl (ctlz x), 5)', the RHS is 0/1, and this is an
1064 // equality comparison, then we're just comparing whether X itself is
1065 // zero.
1066 if (N0.getOpcode() == ISD::SRL && (C1 == 0 || C1 == 1) &&
1067 N0.getOperand(0).getOpcode() == ISD::CTLZ &&
1068 N0.getOperand(1).getOpcode() == ISD::Constant) {
1069 unsigned ShAmt = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1070 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00001071 ShAmt == Log2_32(N0.getValueType().getSizeInBits())) {
Evan Chengfa1eb272007-02-08 22:13:59 +00001072 if ((C1 == 0) == (Cond == ISD::SETEQ)) {
1073 // (srl (ctlz x), 5) == 0 -> X != 0
1074 // (srl (ctlz x), 5) != 1 -> X != 0
1075 Cond = ISD::SETNE;
1076 } else {
1077 // (srl (ctlz x), 5) != 0 -> X == 0
1078 // (srl (ctlz x), 5) == 1 -> X == 0
1079 Cond = ISD::SETEQ;
1080 }
1081 SDOperand Zero = DAG.getConstant(0, N0.getValueType());
1082 return DAG.getSetCC(VT, N0.getOperand(0).getOperand(0),
1083 Zero, Cond);
1084 }
1085 }
1086
1087 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
1088 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001089 unsigned InSize = N0.getOperand(0).getValueType().getSizeInBits();
Evan Chengfa1eb272007-02-08 22:13:59 +00001090
1091 // If the comparison constant has bits in the upper part, the
1092 // zero-extended value could never match.
Dan Gohman6c6cd1c2008-03-03 22:22:56 +00001093 if (C1.intersects(APInt::getHighBitsSet(C1.getBitWidth(),
1094 C1.getBitWidth() - InSize))) {
Evan Chengfa1eb272007-02-08 22:13:59 +00001095 switch (Cond) {
1096 case ISD::SETUGT:
1097 case ISD::SETUGE:
1098 case ISD::SETEQ: return DAG.getConstant(0, VT);
1099 case ISD::SETULT:
1100 case ISD::SETULE:
1101 case ISD::SETNE: return DAG.getConstant(1, VT);
1102 case ISD::SETGT:
1103 case ISD::SETGE:
1104 // True if the sign bit of C1 is set.
Dan Gohman6c6cd1c2008-03-03 22:22:56 +00001105 return DAG.getConstant(C1.isNegative(), VT);
Evan Chengfa1eb272007-02-08 22:13:59 +00001106 case ISD::SETLT:
1107 case ISD::SETLE:
1108 // True if the sign bit of C1 isn't set.
Dan Gohman6c6cd1c2008-03-03 22:22:56 +00001109 return DAG.getConstant(C1.isNonNegative(), VT);
Evan Chengfa1eb272007-02-08 22:13:59 +00001110 default:
1111 break;
1112 }
1113 }
1114
1115 // Otherwise, we can perform the comparison with the low bits.
1116 switch (Cond) {
1117 case ISD::SETEQ:
1118 case ISD::SETNE:
1119 case ISD::SETUGT:
1120 case ISD::SETUGE:
1121 case ISD::SETULT:
1122 case ISD::SETULE:
1123 return DAG.getSetCC(VT, N0.getOperand(0),
Dan Gohman6c6cd1c2008-03-03 22:22:56 +00001124 DAG.getConstant(APInt(C1).trunc(InSize),
1125 N0.getOperand(0).getValueType()),
Evan Chengfa1eb272007-02-08 22:13:59 +00001126 Cond);
1127 default:
1128 break; // todo, be more careful with signed comparisons
1129 }
1130 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1131 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001132 MVT ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
1133 unsigned ExtSrcTyBits = ExtSrcTy.getSizeInBits();
1134 MVT ExtDstTy = N0.getValueType();
1135 unsigned ExtDstTyBits = ExtDstTy.getSizeInBits();
Evan Chengfa1eb272007-02-08 22:13:59 +00001136
1137 // If the extended part has any inconsistent bits, it cannot ever
1138 // compare equal. In other words, they have to be all ones or all
1139 // zeros.
Dan Gohman6c6cd1c2008-03-03 22:22:56 +00001140 APInt ExtBits =
1141 APInt::getHighBitsSet(ExtDstTyBits, ExtDstTyBits - ExtSrcTyBits);
Evan Chengfa1eb272007-02-08 22:13:59 +00001142 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
1143 return DAG.getConstant(Cond == ISD::SETNE, VT);
1144
1145 SDOperand ZextOp;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001146 MVT Op0Ty = N0.getOperand(0).getValueType();
Evan Chengfa1eb272007-02-08 22:13:59 +00001147 if (Op0Ty == ExtSrcTy) {
1148 ZextOp = N0.getOperand(0);
1149 } else {
Dan Gohman3370dd72008-03-03 22:37:52 +00001150 APInt Imm = APInt::getLowBitsSet(ExtDstTyBits, ExtSrcTyBits);
Evan Chengfa1eb272007-02-08 22:13:59 +00001151 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
1152 DAG.getConstant(Imm, Op0Ty));
1153 }
1154 if (!DCI.isCalledByLegalizer())
1155 DCI.AddToWorklist(ZextOp.Val);
1156 // Otherwise, make this a use of a zext.
1157 return DAG.getSetCC(VT, ZextOp,
Dan Gohman6c6cd1c2008-03-03 22:22:56 +00001158 DAG.getConstant(C1 & APInt::getLowBitsSet(
1159 ExtDstTyBits,
1160 ExtSrcTyBits),
Evan Chengfa1eb272007-02-08 22:13:59 +00001161 ExtDstTy),
1162 Cond);
Dan Gohman6c6cd1c2008-03-03 22:22:56 +00001163 } else if ((N1C->isNullValue() || N1C->getAPIntValue() == 1) &&
Evan Chengfa1eb272007-02-08 22:13:59 +00001164 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
1165
1166 // SETCC (SETCC), [0|1], [EQ|NE] -> SETCC
1167 if (N0.getOpcode() == ISD::SETCC) {
1168 bool TrueWhenTrue = (Cond == ISD::SETEQ) ^ (N1C->getValue() != 1);
1169 if (TrueWhenTrue)
1170 return N0;
1171
1172 // Invert the condition.
1173 ISD::CondCode CC = cast<CondCodeSDNode>(N0.getOperand(2))->get();
1174 CC = ISD::getSetCCInverse(CC,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001175 N0.getOperand(0).getValueType().isInteger());
Evan Chengfa1eb272007-02-08 22:13:59 +00001176 return DAG.getSetCC(VT, N0.getOperand(0), N0.getOperand(1), CC);
1177 }
1178
1179 if ((N0.getOpcode() == ISD::XOR ||
1180 (N0.getOpcode() == ISD::AND &&
1181 N0.getOperand(0).getOpcode() == ISD::XOR &&
1182 N0.getOperand(1) == N0.getOperand(0).getOperand(1))) &&
1183 isa<ConstantSDNode>(N0.getOperand(1)) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00001184 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue() == 1) {
Evan Chengfa1eb272007-02-08 22:13:59 +00001185 // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We
1186 // can only do this if the top bits are known zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001187 unsigned BitWidth = N0.getValueSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001188 if (DAG.MaskedValueIsZero(N0,
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001189 APInt::getHighBitsSet(BitWidth,
1190 BitWidth-1))) {
Evan Chengfa1eb272007-02-08 22:13:59 +00001191 // Okay, get the un-inverted input value.
1192 SDOperand Val;
1193 if (N0.getOpcode() == ISD::XOR)
1194 Val = N0.getOperand(0);
1195 else {
1196 assert(N0.getOpcode() == ISD::AND &&
1197 N0.getOperand(0).getOpcode() == ISD::XOR);
1198 // ((X^1)&1)^1 -> X & 1
1199 Val = DAG.getNode(ISD::AND, N0.getValueType(),
1200 N0.getOperand(0).getOperand(0),
1201 N0.getOperand(1));
1202 }
1203 return DAG.getSetCC(VT, Val, N1,
1204 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
1205 }
1206 }
1207 }
1208
Dan Gohman3370dd72008-03-03 22:37:52 +00001209 APInt MinVal, MaxVal;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001210 unsigned OperandBitSize = N1C->getValueType(0).getSizeInBits();
Evan Chengfa1eb272007-02-08 22:13:59 +00001211 if (ISD::isSignedIntSetCC(Cond)) {
Dan Gohman3370dd72008-03-03 22:37:52 +00001212 MinVal = APInt::getSignedMinValue(OperandBitSize);
1213 MaxVal = APInt::getSignedMaxValue(OperandBitSize);
Evan Chengfa1eb272007-02-08 22:13:59 +00001214 } else {
Dan Gohman3370dd72008-03-03 22:37:52 +00001215 MinVal = APInt::getMinValue(OperandBitSize);
1216 MaxVal = APInt::getMaxValue(OperandBitSize);
Evan Chengfa1eb272007-02-08 22:13:59 +00001217 }
1218
1219 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
1220 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
1221 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
Dan Gohman6c6cd1c2008-03-03 22:22:56 +00001222 // X >= C0 --> X > (C0-1)
1223 return DAG.getSetCC(VT, N0, DAG.getConstant(C1-1, N1.getValueType()),
Evan Chengfa1eb272007-02-08 22:13:59 +00001224 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
1225 }
1226
1227 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
1228 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
Dan Gohman6c6cd1c2008-03-03 22:22:56 +00001229 // X <= C0 --> X < (C0+1)
1230 return DAG.getSetCC(VT, N0, DAG.getConstant(C1+1, N1.getValueType()),
Evan Chengfa1eb272007-02-08 22:13:59 +00001231 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
1232 }
1233
1234 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
1235 return DAG.getConstant(0, VT); // X < MIN --> false
1236 if ((Cond == ISD::SETGE || Cond == ISD::SETUGE) && C1 == MinVal)
1237 return DAG.getConstant(1, VT); // X >= MIN --> true
1238 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal)
1239 return DAG.getConstant(0, VT); // X > MAX --> false
1240 if ((Cond == ISD::SETLE || Cond == ISD::SETULE) && C1 == MaxVal)
1241 return DAG.getConstant(1, VT); // X <= MAX --> true
1242
1243 // Canonicalize setgt X, Min --> setne X, Min
1244 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
1245 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
1246 // Canonicalize setlt X, Max --> setne X, Max
1247 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
1248 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
1249
1250 // If we have setult X, 1, turn it into seteq X, 0
1251 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
1252 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
1253 ISD::SETEQ);
1254 // If we have setugt X, Max-1, turn it into seteq X, Max
1255 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
1256 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
1257 ISD::SETEQ);
1258
1259 // If we have "setcc X, C0", check to see if we can shrink the immediate
1260 // by changing cc.
1261
1262 // SETUGT X, SINTMAX -> SETLT X, 0
1263 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
1264 C1 == (~0ULL >> (65-OperandBitSize)))
1265 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
1266 ISD::SETLT);
1267
1268 // FIXME: Implement the rest of these.
1269
1270 // Fold bit comparisons when we can.
1271 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
1272 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
1273 if (ConstantSDNode *AndRHS =
1274 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1275 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
1276 // Perform the xform if the AND RHS is a single bit.
1277 if (isPowerOf2_64(AndRHS->getValue())) {
1278 return DAG.getNode(ISD::SRL, VT, N0,
1279 DAG.getConstant(Log2_64(AndRHS->getValue()),
1280 getShiftAmountTy()));
1281 }
1282 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
1283 // (X & 8) == 8 --> (X & 8) >> 3
1284 // Perform the xform if C1 is a single bit.
Dan Gohman6c6cd1c2008-03-03 22:22:56 +00001285 if (C1.isPowerOf2()) {
Evan Chengfa1eb272007-02-08 22:13:59 +00001286 return DAG.getNode(ISD::SRL, VT, N0,
Dan Gohman6c6cd1c2008-03-03 22:22:56 +00001287 DAG.getConstant(C1.logBase2(), getShiftAmountTy()));
Evan Chengfa1eb272007-02-08 22:13:59 +00001288 }
1289 }
1290 }
1291 }
1292 } else if (isa<ConstantSDNode>(N0.Val)) {
1293 // Ensure that the constant occurs on the RHS.
1294 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
1295 }
1296
1297 if (isa<ConstantFPSDNode>(N0.Val)) {
1298 // Constant fold or commute setcc.
1299 SDOperand O = DAG.FoldSetCC(VT, N0, N1, Cond);
1300 if (O.Val) return O;
Chris Lattner63079f02007-12-29 08:37:08 +00001301 } else if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1.Val)) {
1302 // If the RHS of an FP comparison is a constant, simplify it away in
1303 // some cases.
1304 if (CFP->getValueAPF().isNaN()) {
1305 // If an operand is known to be a nan, we can fold it.
1306 switch (ISD::getUnorderedFlavor(Cond)) {
1307 default: assert(0 && "Unknown flavor!");
1308 case 0: // Known false.
1309 return DAG.getConstant(0, VT);
1310 case 1: // Known true.
1311 return DAG.getConstant(1, VT);
Chris Lattner1c3e1e22007-12-30 21:21:10 +00001312 case 2: // Undefined.
Chris Lattner63079f02007-12-29 08:37:08 +00001313 return DAG.getNode(ISD::UNDEF, VT);
1314 }
1315 }
1316
1317 // Otherwise, we know the RHS is not a NaN. Simplify the node to drop the
1318 // constant if knowing that the operand is non-nan is enough. We prefer to
1319 // have SETO(x,x) instead of SETO(x, 0.0) because this avoids having to
1320 // materialize 0.0.
1321 if (Cond == ISD::SETO || Cond == ISD::SETUO)
1322 return DAG.getSetCC(VT, N0, N0, Cond);
Evan Chengfa1eb272007-02-08 22:13:59 +00001323 }
1324
1325 if (N0 == N1) {
1326 // We can always fold X == X for integer setcc's.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001327 if (N0.getValueType().isInteger())
Evan Chengfa1eb272007-02-08 22:13:59 +00001328 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
1329 unsigned UOF = ISD::getUnorderedFlavor(Cond);
1330 if (UOF == 2) // FP operators that are undefined on NaNs.
1331 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
1332 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
1333 return DAG.getConstant(UOF, VT);
1334 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
1335 // if it is not already.
1336 ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
1337 if (NewCond != Cond)
1338 return DAG.getSetCC(VT, N0, N1, NewCond);
1339 }
1340
1341 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00001342 N0.getValueType().isInteger()) {
Evan Chengfa1eb272007-02-08 22:13:59 +00001343 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
1344 N0.getOpcode() == ISD::XOR) {
1345 // Simplify (X+Y) == (X+Z) --> Y == Z
1346 if (N0.getOpcode() == N1.getOpcode()) {
1347 if (N0.getOperand(0) == N1.getOperand(0))
1348 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
1349 if (N0.getOperand(1) == N1.getOperand(1))
1350 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
1351 if (DAG.isCommutativeBinOp(N0.getOpcode())) {
1352 // If X op Y == Y op X, try other combinations.
1353 if (N0.getOperand(0) == N1.getOperand(1))
1354 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
1355 if (N0.getOperand(1) == N1.getOperand(0))
1356 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(1), Cond);
1357 }
1358 }
1359
1360 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
1361 if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1362 // Turn (X+C1) == C2 --> X == C2-C1
1363 if (N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse()) {
1364 return DAG.getSetCC(VT, N0.getOperand(0),
1365 DAG.getConstant(RHSC->getValue()-LHSR->getValue(),
1366 N0.getValueType()), Cond);
1367 }
1368
1369 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
1370 if (N0.getOpcode() == ISD::XOR)
1371 // If we know that all of the inverted bits are zero, don't bother
1372 // performing the inversion.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001373 if (DAG.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getAPIntValue()))
1374 return
1375 DAG.getSetCC(VT, N0.getOperand(0),
1376 DAG.getConstant(LHSR->getAPIntValue() ^
1377 RHSC->getAPIntValue(),
1378 N0.getValueType()),
1379 Cond);
Evan Chengfa1eb272007-02-08 22:13:59 +00001380 }
1381
1382 // Turn (C1-X) == C2 --> X == C1-C2
1383 if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
1384 if (N0.getOpcode() == ISD::SUB && N0.Val->hasOneUse()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001385 return
1386 DAG.getSetCC(VT, N0.getOperand(1),
1387 DAG.getConstant(SUBC->getAPIntValue() -
1388 RHSC->getAPIntValue(),
1389 N0.getValueType()),
1390 Cond);
Evan Chengfa1eb272007-02-08 22:13:59 +00001391 }
1392 }
1393 }
1394
1395 // Simplify (X+Z) == X --> Z == 0
1396 if (N0.getOperand(0) == N1)
1397 return DAG.getSetCC(VT, N0.getOperand(1),
1398 DAG.getConstant(0, N0.getValueType()), Cond);
1399 if (N0.getOperand(1) == N1) {
1400 if (DAG.isCommutativeBinOp(N0.getOpcode()))
1401 return DAG.getSetCC(VT, N0.getOperand(0),
1402 DAG.getConstant(0, N0.getValueType()), Cond);
Chris Lattner2ad913b2007-05-19 00:43:44 +00001403 else if (N0.Val->hasOneUse()) {
Evan Chengfa1eb272007-02-08 22:13:59 +00001404 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
1405 // (Z-X) == X --> Z == X<<1
1406 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
1407 N1,
1408 DAG.getConstant(1, getShiftAmountTy()));
1409 if (!DCI.isCalledByLegalizer())
1410 DCI.AddToWorklist(SH.Val);
1411 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
1412 }
1413 }
1414 }
1415
1416 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
1417 N1.getOpcode() == ISD::XOR) {
1418 // Simplify X == (X+Z) --> Z == 0
1419 if (N1.getOperand(0) == N0) {
1420 return DAG.getSetCC(VT, N1.getOperand(1),
1421 DAG.getConstant(0, N1.getValueType()), Cond);
1422 } else if (N1.getOperand(1) == N0) {
1423 if (DAG.isCommutativeBinOp(N1.getOpcode())) {
1424 return DAG.getSetCC(VT, N1.getOperand(0),
1425 DAG.getConstant(0, N1.getValueType()), Cond);
Chris Lattner7667c0b2007-05-19 00:46:51 +00001426 } else if (N1.Val->hasOneUse()) {
Evan Chengfa1eb272007-02-08 22:13:59 +00001427 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
1428 // X == (Z-X) --> X<<1 == Z
1429 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
1430 DAG.getConstant(1, getShiftAmountTy()));
1431 if (!DCI.isCalledByLegalizer())
1432 DCI.AddToWorklist(SH.Val);
1433 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
1434 }
1435 }
1436 }
1437 }
1438
1439 // Fold away ALL boolean setcc's.
1440 SDOperand Temp;
1441 if (N0.getValueType() == MVT::i1 && foldBooleans) {
1442 switch (Cond) {
1443 default: assert(0 && "Unknown integer setcc!");
1444 case ISD::SETEQ: // X == Y -> (X^Y)^1
1445 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
1446 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
1447 if (!DCI.isCalledByLegalizer())
1448 DCI.AddToWorklist(Temp.Val);
1449 break;
1450 case ISD::SETNE: // X != Y --> (X^Y)
1451 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
1452 break;
1453 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
1454 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
1455 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
1456 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
1457 if (!DCI.isCalledByLegalizer())
1458 DCI.AddToWorklist(Temp.Val);
1459 break;
1460 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
1461 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
1462 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
1463 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
1464 if (!DCI.isCalledByLegalizer())
1465 DCI.AddToWorklist(Temp.Val);
1466 break;
1467 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
1468 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
1469 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
1470 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
1471 if (!DCI.isCalledByLegalizer())
1472 DCI.AddToWorklist(Temp.Val);
1473 break;
1474 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
1475 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
1476 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
1477 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
1478 break;
1479 }
1480 if (VT != MVT::i1) {
1481 if (!DCI.isCalledByLegalizer())
1482 DCI.AddToWorklist(N0.Val);
1483 // FIXME: If running after legalize, we probably can't do this.
1484 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
1485 }
1486 return N0;
1487 }
1488
1489 // Could not fold it.
1490 return SDOperand();
1491}
1492
Evan Chengad4196b2008-05-12 19:56:52 +00001493/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
1494/// node is a GlobalAddress + offset.
1495bool TargetLowering::isGAPlusOffset(SDNode *N, GlobalValue* &GA,
1496 int64_t &Offset) const {
1497 if (isa<GlobalAddressSDNode>(N)) {
Dan Gohman9ea3f562008-06-09 22:05:52 +00001498 GlobalAddressSDNode *GASD = cast<GlobalAddressSDNode>(N);
1499 GA = GASD->getGlobal();
1500 Offset += GASD->getOffset();
Evan Chengad4196b2008-05-12 19:56:52 +00001501 return true;
1502 }
1503
1504 if (N->getOpcode() == ISD::ADD) {
1505 SDOperand N1 = N->getOperand(0);
1506 SDOperand N2 = N->getOperand(1);
1507 if (isGAPlusOffset(N1.Val, GA, Offset)) {
1508 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
1509 if (V) {
1510 Offset += V->getSignExtended();
1511 return true;
1512 }
1513 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
1514 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
1515 if (V) {
1516 Offset += V->getSignExtended();
1517 return true;
1518 }
1519 }
1520 }
1521 return false;
1522}
1523
1524
1525/// isConsecutiveLoad - Return true if LD (which must be a LoadSDNode) is
1526/// loading 'Bytes' bytes from a location that is 'Dist' units away from the
1527/// location that the 'Base' load is loading from.
1528bool TargetLowering::isConsecutiveLoad(SDNode *LD, SDNode *Base,
1529 unsigned Bytes, int Dist,
Evan Cheng9bfa03c2008-05-12 23:04:07 +00001530 const MachineFrameInfo *MFI) const {
Evan Chengad4196b2008-05-12 19:56:52 +00001531 if (LD->getOperand(0).Val != Base->getOperand(0).Val)
1532 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001533 MVT VT = LD->getValueType(0);
1534 if (VT.getSizeInBits() / 8 != Bytes)
Evan Chengad4196b2008-05-12 19:56:52 +00001535 return false;
1536
1537 SDOperand Loc = LD->getOperand(1);
1538 SDOperand BaseLoc = Base->getOperand(1);
1539 if (Loc.getOpcode() == ISD::FrameIndex) {
1540 if (BaseLoc.getOpcode() != ISD::FrameIndex)
1541 return false;
1542 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
1543 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
1544 int FS = MFI->getObjectSize(FI);
1545 int BFS = MFI->getObjectSize(BFI);
1546 if (FS != BFS || FS != (int)Bytes) return false;
1547 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
1548 }
1549
1550 GlobalValue *GV1 = NULL;
1551 GlobalValue *GV2 = NULL;
1552 int64_t Offset1 = 0;
1553 int64_t Offset2 = 0;
1554 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
1555 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
1556 if (isGA1 && isGA2 && GV1 == GV2)
1557 return Offset1 == (Offset2 + Dist*Bytes);
1558 return false;
1559}
1560
1561
Chris Lattner00ffed02006-03-01 04:52:55 +00001562SDOperand TargetLowering::
1563PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
1564 // Default implementation: no optimization.
1565 return SDOperand();
1566}
1567
Chris Lattnereb8146b2006-02-04 02:13:02 +00001568//===----------------------------------------------------------------------===//
1569// Inline Assembler Implementation Methods
1570//===----------------------------------------------------------------------===//
1571
Chris Lattner4376fea2008-04-27 00:09:47 +00001572
Chris Lattnereb8146b2006-02-04 02:13:02 +00001573TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00001574TargetLowering::getConstraintType(const std::string &Constraint) const {
Chris Lattnereb8146b2006-02-04 02:13:02 +00001575 // FIXME: lots more standard ones to handle.
Chris Lattner4234f572007-03-25 02:14:49 +00001576 if (Constraint.size() == 1) {
1577 switch (Constraint[0]) {
1578 default: break;
1579 case 'r': return C_RegisterClass;
1580 case 'm': // memory
1581 case 'o': // offsetable
1582 case 'V': // not offsetable
1583 return C_Memory;
1584 case 'i': // Simple Integer or Relocatable Constant
1585 case 'n': // Simple Integer
1586 case 's': // Relocatable Constant
Chris Lattnerc13dd1c2007-03-25 04:35:41 +00001587 case 'X': // Allow ANY value.
Chris Lattner4234f572007-03-25 02:14:49 +00001588 case 'I': // Target registers.
1589 case 'J':
1590 case 'K':
1591 case 'L':
1592 case 'M':
1593 case 'N':
1594 case 'O':
1595 case 'P':
1596 return C_Other;
1597 }
Chris Lattnereb8146b2006-02-04 02:13:02 +00001598 }
Chris Lattner065421f2007-03-25 02:18:14 +00001599
1600 if (Constraint.size() > 1 && Constraint[0] == '{' &&
1601 Constraint[Constraint.size()-1] == '}')
1602 return C_Register;
Chris Lattner4234f572007-03-25 02:14:49 +00001603 return C_Unknown;
Chris Lattnereb8146b2006-02-04 02:13:02 +00001604}
1605
Dale Johannesenba2a0b92008-01-29 02:21:21 +00001606/// LowerXConstraint - try to replace an X constraint, which matches anything,
1607/// with another that has more specific requirements based on the type of the
1608/// corresponding operand.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001609const char *TargetLowering::LowerXConstraint(MVT ConstraintVT) const{
1610 if (ConstraintVT.isInteger())
Chris Lattner5e764232008-04-26 23:02:14 +00001611 return "r";
Duncan Sands83ec4b62008-06-06 12:08:01 +00001612 if (ConstraintVT.isFloatingPoint())
Chris Lattner5e764232008-04-26 23:02:14 +00001613 return "f"; // works for many targets
1614 return 0;
Dale Johannesenba2a0b92008-01-29 02:21:21 +00001615}
1616
Chris Lattner48884cd2007-08-25 00:47:38 +00001617/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
1618/// vector. If it is invalid, don't add anything to Ops.
1619void TargetLowering::LowerAsmOperandForConstraint(SDOperand Op,
1620 char ConstraintLetter,
1621 std::vector<SDOperand> &Ops,
Chris Lattner5e764232008-04-26 23:02:14 +00001622 SelectionDAG &DAG) const {
Chris Lattnereb8146b2006-02-04 02:13:02 +00001623 switch (ConstraintLetter) {
Chris Lattner9ff6ee82007-02-17 06:00:35 +00001624 default: break;
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001625 case 'X': // Allows any operand; labels (basic block) use this.
1626 if (Op.getOpcode() == ISD::BasicBlock) {
1627 Ops.push_back(Op);
1628 return;
1629 }
1630 // fall through
Chris Lattnereb8146b2006-02-04 02:13:02 +00001631 case 'i': // Simple Integer or Relocatable Constant
1632 case 'n': // Simple Integer
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001633 case 's': { // Relocatable Constant
Chris Lattner75c7d2b2007-05-03 16:54:34 +00001634 // These operands are interested in values of the form (GV+C), where C may
1635 // be folded in as an offset of GV, or it may be explicitly added. Also, it
1636 // is possible and fine if either GV or C are missing.
1637 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
1638 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
1639
1640 // If we have "(add GV, C)", pull out GV/C
1641 if (Op.getOpcode() == ISD::ADD) {
1642 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
1643 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
1644 if (C == 0 || GA == 0) {
1645 C = dyn_cast<ConstantSDNode>(Op.getOperand(0));
1646 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(1));
1647 }
1648 if (C == 0 || GA == 0)
1649 C = 0, GA = 0;
1650 }
1651
1652 // If we find a valid operand, map to the TargetXXX version so that the
1653 // value itself doesn't get selected.
1654 if (GA) { // Either &GV or &GV+C
1655 if (ConstraintLetter != 'n') {
1656 int64_t Offs = GA->getOffset();
1657 if (C) Offs += C->getValue();
Chris Lattner48884cd2007-08-25 00:47:38 +00001658 Ops.push_back(DAG.getTargetGlobalAddress(GA->getGlobal(),
1659 Op.getValueType(), Offs));
1660 return;
Chris Lattner75c7d2b2007-05-03 16:54:34 +00001661 }
1662 }
1663 if (C) { // just C, no GV.
Chris Lattner9ff6ee82007-02-17 06:00:35 +00001664 // Simple constants are not allowed for 's'.
Chris Lattner48884cd2007-08-25 00:47:38 +00001665 if (ConstraintLetter != 's') {
1666 Ops.push_back(DAG.getTargetConstant(C->getValue(), Op.getValueType()));
1667 return;
1668 }
Chris Lattner9ff6ee82007-02-17 06:00:35 +00001669 }
Chris Lattner9ff6ee82007-02-17 06:00:35 +00001670 break;
Chris Lattnereb8146b2006-02-04 02:13:02 +00001671 }
Chris Lattner75c7d2b2007-05-03 16:54:34 +00001672 }
Chris Lattnereb8146b2006-02-04 02:13:02 +00001673}
1674
Chris Lattner4ccb0702006-01-26 20:37:03 +00001675std::vector<unsigned> TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00001676getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001677 MVT VT) const {
Chris Lattner1efa40f2006-02-22 00:56:39 +00001678 return std::vector<unsigned>();
1679}
1680
1681
1682std::pair<unsigned, const TargetRegisterClass*> TargetLowering::
Chris Lattner4217ca8dc2006-02-21 23:11:00 +00001683getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001684 MVT VT) const {
Chris Lattner1efa40f2006-02-22 00:56:39 +00001685 if (Constraint[0] != '{')
1686 return std::pair<unsigned, const TargetRegisterClass*>(0, 0);
Chris Lattnera55079a2006-02-01 01:29:47 +00001687 assert(*(Constraint.end()-1) == '}' && "Not a brace enclosed constraint?");
1688
1689 // Remove the braces from around the name.
1690 std::string RegName(Constraint.begin()+1, Constraint.end()-1);
Chris Lattner1efa40f2006-02-22 00:56:39 +00001691
1692 // Figure out which register class contains this reg.
Dan Gohman6f0d0242008-02-10 18:45:23 +00001693 const TargetRegisterInfo *RI = TM.getRegisterInfo();
1694 for (TargetRegisterInfo::regclass_iterator RCI = RI->regclass_begin(),
Chris Lattner1efa40f2006-02-22 00:56:39 +00001695 E = RI->regclass_end(); RCI != E; ++RCI) {
1696 const TargetRegisterClass *RC = *RCI;
Chris Lattnerb3befd42006-02-22 23:00:51 +00001697
1698 // If none of the the value types for this register class are valid, we
1699 // can't use it. For example, 64-bit reg classes on 32-bit targets.
1700 bool isLegal = false;
1701 for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
1702 I != E; ++I) {
1703 if (isTypeLegal(*I)) {
1704 isLegal = true;
1705 break;
1706 }
1707 }
1708
1709 if (!isLegal) continue;
1710
Chris Lattner1efa40f2006-02-22 00:56:39 +00001711 for (TargetRegisterClass::iterator I = RC->begin(), E = RC->end();
1712 I != E; ++I) {
Bill Wendling74ab84c2008-02-26 21:11:01 +00001713 if (StringsEqualNoCase(RegName, RI->get(*I).AsmName))
Chris Lattner1efa40f2006-02-22 00:56:39 +00001714 return std::make_pair(*I, RC);
Chris Lattner1efa40f2006-02-22 00:56:39 +00001715 }
Chris Lattner4ccb0702006-01-26 20:37:03 +00001716 }
Chris Lattnera55079a2006-02-01 01:29:47 +00001717
Chris Lattner1efa40f2006-02-22 00:56:39 +00001718 return std::pair<unsigned, const TargetRegisterClass*>(0, 0);
Chris Lattner4ccb0702006-01-26 20:37:03 +00001719}
Evan Cheng30b37b52006-03-13 23:18:16 +00001720
1721//===----------------------------------------------------------------------===//
Chris Lattner4376fea2008-04-27 00:09:47 +00001722// Constraint Selection.
1723
1724/// getConstraintGenerality - Return an integer indicating how general CT
1725/// is.
1726static unsigned getConstraintGenerality(TargetLowering::ConstraintType CT) {
1727 switch (CT) {
1728 default: assert(0 && "Unknown constraint type!");
1729 case TargetLowering::C_Other:
1730 case TargetLowering::C_Unknown:
1731 return 0;
1732 case TargetLowering::C_Register:
1733 return 1;
1734 case TargetLowering::C_RegisterClass:
1735 return 2;
1736 case TargetLowering::C_Memory:
1737 return 3;
1738 }
1739}
1740
1741/// ChooseConstraint - If there are multiple different constraints that we
1742/// could pick for this operand (e.g. "imr") try to pick the 'best' one.
Chris Lattner24e1a9d2008-04-27 01:49:46 +00001743/// This is somewhat tricky: constraints fall into four classes:
Chris Lattner4376fea2008-04-27 00:09:47 +00001744/// Other -> immediates and magic values
1745/// Register -> one specific register
1746/// RegisterClass -> a group of regs
1747/// Memory -> memory
1748/// Ideally, we would pick the most specific constraint possible: if we have
1749/// something that fits into a register, we would pick it. The problem here
1750/// is that if we have something that could either be in a register or in
1751/// memory that use of the register could cause selection of *other*
1752/// operands to fail: they might only succeed if we pick memory. Because of
1753/// this the heuristic we use is:
1754///
1755/// 1) If there is an 'other' constraint, and if the operand is valid for
1756/// that constraint, use it. This makes us take advantage of 'i'
1757/// constraints when available.
1758/// 2) Otherwise, pick the most general constraint present. This prefers
1759/// 'm' over 'r', for example.
1760///
1761static void ChooseConstraint(TargetLowering::AsmOperandInfo &OpInfo,
Chris Lattner5a096902008-04-27 00:37:18 +00001762 const TargetLowering &TLI,
1763 SDOperand Op, SelectionDAG *DAG) {
Chris Lattner4376fea2008-04-27 00:09:47 +00001764 assert(OpInfo.Codes.size() > 1 && "Doesn't have multiple constraint options");
1765 unsigned BestIdx = 0;
1766 TargetLowering::ConstraintType BestType = TargetLowering::C_Unknown;
1767 int BestGenerality = -1;
1768
1769 // Loop over the options, keeping track of the most general one.
1770 for (unsigned i = 0, e = OpInfo.Codes.size(); i != e; ++i) {
1771 TargetLowering::ConstraintType CType =
1772 TLI.getConstraintType(OpInfo.Codes[i]);
1773
Chris Lattner5a096902008-04-27 00:37:18 +00001774 // If this is an 'other' constraint, see if the operand is valid for it.
1775 // For example, on X86 we might have an 'rI' constraint. If the operand
1776 // is an integer in the range [0..31] we want to use I (saving a load
1777 // of a register), otherwise we must use 'r'.
1778 if (CType == TargetLowering::C_Other && Op.Val) {
1779 assert(OpInfo.Codes[i].size() == 1 &&
1780 "Unhandled multi-letter 'other' constraint");
1781 std::vector<SDOperand> ResultOps;
1782 TLI.LowerAsmOperandForConstraint(Op, OpInfo.Codes[i][0],
1783 ResultOps, *DAG);
1784 if (!ResultOps.empty()) {
1785 BestType = CType;
1786 BestIdx = i;
1787 break;
1788 }
1789 }
1790
Chris Lattner4376fea2008-04-27 00:09:47 +00001791 // This constraint letter is more general than the previous one, use it.
1792 int Generality = getConstraintGenerality(CType);
1793 if (Generality > BestGenerality) {
1794 BestType = CType;
1795 BestIdx = i;
1796 BestGenerality = Generality;
1797 }
1798 }
1799
1800 OpInfo.ConstraintCode = OpInfo.Codes[BestIdx];
1801 OpInfo.ConstraintType = BestType;
1802}
1803
1804/// ComputeConstraintToUse - Determines the constraint code and constraint
1805/// type to use for the specific AsmOperandInfo, setting
1806/// OpInfo.ConstraintCode and OpInfo.ConstraintType.
Chris Lattner5a096902008-04-27 00:37:18 +00001807void TargetLowering::ComputeConstraintToUse(AsmOperandInfo &OpInfo,
1808 SDOperand Op,
1809 SelectionDAG *DAG) const {
Chris Lattner4376fea2008-04-27 00:09:47 +00001810 assert(!OpInfo.Codes.empty() && "Must have at least one constraint");
1811
1812 // Single-letter constraints ('r') are very common.
1813 if (OpInfo.Codes.size() == 1) {
1814 OpInfo.ConstraintCode = OpInfo.Codes[0];
1815 OpInfo.ConstraintType = getConstraintType(OpInfo.ConstraintCode);
1816 } else {
Chris Lattner5a096902008-04-27 00:37:18 +00001817 ChooseConstraint(OpInfo, *this, Op, DAG);
Chris Lattner4376fea2008-04-27 00:09:47 +00001818 }
1819
1820 // 'X' matches anything.
1821 if (OpInfo.ConstraintCode == "X" && OpInfo.CallOperandVal) {
1822 // Labels and constants are handled elsewhere ('X' is the only thing
1823 // that matches labels).
1824 if (isa<BasicBlock>(OpInfo.CallOperandVal) ||
1825 isa<ConstantInt>(OpInfo.CallOperandVal))
1826 return;
1827
1828 // Otherwise, try to resolve it to something we know about by looking at
1829 // the actual operand type.
1830 if (const char *Repl = LowerXConstraint(OpInfo.ConstraintVT)) {
1831 OpInfo.ConstraintCode = Repl;
1832 OpInfo.ConstraintType = getConstraintType(OpInfo.ConstraintCode);
1833 }
1834 }
1835}
1836
1837//===----------------------------------------------------------------------===//
Evan Cheng30b37b52006-03-13 23:18:16 +00001838// Loop Strength Reduction hooks
1839//===----------------------------------------------------------------------===//
1840
Chris Lattner1436bb62007-03-30 23:14:50 +00001841/// isLegalAddressingMode - Return true if the addressing mode represented
1842/// by AM is legal for this target, for a load/store of the specified type.
1843bool TargetLowering::isLegalAddressingMode(const AddrMode &AM,
1844 const Type *Ty) const {
1845 // The default implementation of this implements a conservative RISCy, r+r and
1846 // r+i addr mode.
1847
1848 // Allows a sign-extended 16-bit immediate field.
1849 if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1)
1850 return false;
1851
1852 // No global is ever allowed as a base.
1853 if (AM.BaseGV)
1854 return false;
1855
1856 // Only support r+r,
1857 switch (AM.Scale) {
1858 case 0: // "r+i" or just "i", depending on HasBaseReg.
1859 break;
1860 case 1:
1861 if (AM.HasBaseReg && AM.BaseOffs) // "r+r+i" is not allowed.
1862 return false;
1863 // Otherwise we have r+r or r+i.
1864 break;
1865 case 2:
1866 if (AM.HasBaseReg || AM.BaseOffs) // 2*r+r or 2*r+i is not allowed.
1867 return false;
1868 // Allow 2*r as r+r.
1869 break;
1870 }
1871
1872 return true;
1873}
1874
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00001875// Magic for divide replacement
1876
1877struct ms {
1878 int64_t m; // magic number
1879 int64_t s; // shift amount
1880};
1881
1882struct mu {
1883 uint64_t m; // magic number
1884 int64_t a; // add indicator
1885 int64_t s; // shift amount
1886};
1887
1888/// magic - calculate the magic numbers required to codegen an integer sdiv as
1889/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
1890/// or -1.
1891static ms magic32(int32_t d) {
1892 int32_t p;
1893 uint32_t ad, anc, delta, q1, r1, q2, r2, t;
1894 const uint32_t two31 = 0x80000000U;
1895 struct ms mag;
1896
1897 ad = abs(d);
1898 t = two31 + ((uint32_t)d >> 31);
1899 anc = t - 1 - t%ad; // absolute value of nc
1900 p = 31; // initialize p
1901 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
1902 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
1903 q2 = two31/ad; // initialize q2 = 2p/abs(d)
1904 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
1905 do {
1906 p = p + 1;
1907 q1 = 2*q1; // update q1 = 2p/abs(nc)
1908 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
1909 if (r1 >= anc) { // must be unsigned comparison
1910 q1 = q1 + 1;
1911 r1 = r1 - anc;
1912 }
1913 q2 = 2*q2; // update q2 = 2p/abs(d)
1914 r2 = 2*r2; // update r2 = rem(2p/abs(d))
1915 if (r2 >= ad) { // must be unsigned comparison
1916 q2 = q2 + 1;
1917 r2 = r2 - ad;
1918 }
1919 delta = ad - r2;
1920 } while (q1 < delta || (q1 == delta && r1 == 0));
1921
1922 mag.m = (int32_t)(q2 + 1); // make sure to sign extend
1923 if (d < 0) mag.m = -mag.m; // resulting magic number
1924 mag.s = p - 32; // resulting shift
1925 return mag;
1926}
1927
1928/// magicu - calculate the magic numbers required to codegen an integer udiv as
1929/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
1930static mu magicu32(uint32_t d) {
1931 int32_t p;
1932 uint32_t nc, delta, q1, r1, q2, r2;
1933 struct mu magu;
1934 magu.a = 0; // initialize "add" indicator
1935 nc = - 1 - (-d)%d;
1936 p = 31; // initialize p
1937 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
1938 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
1939 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
1940 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
1941 do {
1942 p = p + 1;
1943 if (r1 >= nc - r1 ) {
1944 q1 = 2*q1 + 1; // update q1
1945 r1 = 2*r1 - nc; // update r1
1946 }
1947 else {
1948 q1 = 2*q1; // update q1
1949 r1 = 2*r1; // update r1
1950 }
1951 if (r2 + 1 >= d - r2) {
1952 if (q2 >= 0x7FFFFFFF) magu.a = 1;
1953 q2 = 2*q2 + 1; // update q2
1954 r2 = 2*r2 + 1 - d; // update r2
1955 }
1956 else {
1957 if (q2 >= 0x80000000) magu.a = 1;
1958 q2 = 2*q2; // update q2
1959 r2 = 2*r2 + 1; // update r2
1960 }
1961 delta = d - 1 - r2;
1962 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
1963 magu.m = q2 + 1; // resulting magic number
1964 magu.s = p - 32; // resulting shift
1965 return magu;
1966}
1967
1968/// magic - calculate the magic numbers required to codegen an integer sdiv as
1969/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
1970/// or -1.
1971static ms magic64(int64_t d) {
1972 int64_t p;
1973 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
1974 const uint64_t two63 = 9223372036854775808ULL; // 2^63
1975 struct ms mag;
1976
1977 ad = d >= 0 ? d : -d;
1978 t = two63 + ((uint64_t)d >> 63);
1979 anc = t - 1 - t%ad; // absolute value of nc
1980 p = 63; // initialize p
1981 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
1982 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
1983 q2 = two63/ad; // initialize q2 = 2p/abs(d)
1984 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
1985 do {
1986 p = p + 1;
1987 q1 = 2*q1; // update q1 = 2p/abs(nc)
1988 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
1989 if (r1 >= anc) { // must be unsigned comparison
1990 q1 = q1 + 1;
1991 r1 = r1 - anc;
1992 }
1993 q2 = 2*q2; // update q2 = 2p/abs(d)
1994 r2 = 2*r2; // update r2 = rem(2p/abs(d))
1995 if (r2 >= ad) { // must be unsigned comparison
1996 q2 = q2 + 1;
1997 r2 = r2 - ad;
1998 }
1999 delta = ad - r2;
2000 } while (q1 < delta || (q1 == delta && r1 == 0));
2001
2002 mag.m = q2 + 1;
2003 if (d < 0) mag.m = -mag.m; // resulting magic number
2004 mag.s = p - 64; // resulting shift
2005 return mag;
2006}
2007
2008/// magicu - calculate the magic numbers required to codegen an integer udiv as
2009/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
2010static mu magicu64(uint64_t d)
2011{
2012 int64_t p;
2013 uint64_t nc, delta, q1, r1, q2, r2;
2014 struct mu magu;
2015 magu.a = 0; // initialize "add" indicator
2016 nc = - 1 - (-d)%d;
2017 p = 63; // initialize p
2018 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
2019 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
2020 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
2021 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
2022 do {
2023 p = p + 1;
2024 if (r1 >= nc - r1 ) {
2025 q1 = 2*q1 + 1; // update q1
2026 r1 = 2*r1 - nc; // update r1
2027 }
2028 else {
2029 q1 = 2*q1; // update q1
2030 r1 = 2*r1; // update r1
2031 }
2032 if (r2 + 1 >= d - r2) {
2033 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
2034 q2 = 2*q2 + 1; // update q2
2035 r2 = 2*r2 + 1 - d; // update r2
2036 }
2037 else {
2038 if (q2 >= 0x8000000000000000ull) magu.a = 1;
2039 q2 = 2*q2; // update q2
2040 r2 = 2*r2 + 1; // update r2
2041 }
2042 delta = d - 1 - r2;
Andrew Lenharth3e348492006-05-16 17:45:23 +00002043 } while (p < 128 && (q1 < delta || (q1 == delta && r1 == 0)));
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002044 magu.m = q2 + 1; // resulting magic number
2045 magu.s = p - 64; // resulting shift
2046 return magu;
2047}
2048
2049/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
2050/// return a DAG expression to select that will generate the same value by
2051/// multiplying by a magic number. See:
2052/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
2053SDOperand TargetLowering::BuildSDIV(SDNode *N, SelectionDAG &DAG,
Anton Korobeynikovbed29462007-04-16 18:10:23 +00002054 std::vector<SDNode*>* Created) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002055 MVT VT = N->getValueType(0);
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002056
2057 // Check to see if we can do this.
2058 if (!isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64))
2059 return SDOperand(); // BuildSDIV only operates on i32 or i64
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002060
2061 int64_t d = cast<ConstantSDNode>(N->getOperand(1))->getSignExtended();
2062 ms magics = (VT == MVT::i32) ? magic32(d) : magic64(d);
2063
2064 // Multiply the numerator (operand 0) by the magic value
Dan Gohman525178c2007-10-08 18:33:35 +00002065 SDOperand Q;
2066 if (isOperationLegal(ISD::MULHS, VT))
2067 Q = DAG.getNode(ISD::MULHS, VT, N->getOperand(0),
2068 DAG.getConstant(magics.m, VT));
2069 else if (isOperationLegal(ISD::SMUL_LOHI, VT))
2070 Q = SDOperand(DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(VT, VT),
2071 N->getOperand(0),
2072 DAG.getConstant(magics.m, VT)).Val, 1);
2073 else
2074 return SDOperand(); // No mulhs or equvialent
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002075 // If d > 0 and m < 0, add the numerator
2076 if (d > 0 && magics.m < 0) {
2077 Q = DAG.getNode(ISD::ADD, VT, Q, N->getOperand(0));
2078 if (Created)
2079 Created->push_back(Q.Val);
2080 }
2081 // If d < 0 and m > 0, subtract the numerator.
2082 if (d < 0 && magics.m > 0) {
2083 Q = DAG.getNode(ISD::SUB, VT, Q, N->getOperand(0));
2084 if (Created)
2085 Created->push_back(Q.Val);
2086 }
2087 // Shift right algebraic if shift value is nonzero
2088 if (magics.s > 0) {
2089 Q = DAG.getNode(ISD::SRA, VT, Q,
2090 DAG.getConstant(magics.s, getShiftAmountTy()));
2091 if (Created)
2092 Created->push_back(Q.Val);
2093 }
2094 // Extract the sign bit and add it to the quotient
2095 SDOperand T =
Duncan Sands83ec4b62008-06-06 12:08:01 +00002096 DAG.getNode(ISD::SRL, VT, Q, DAG.getConstant(VT.getSizeInBits()-1,
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002097 getShiftAmountTy()));
2098 if (Created)
2099 Created->push_back(T.Val);
2100 return DAG.getNode(ISD::ADD, VT, Q, T);
2101}
2102
2103/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
2104/// return a DAG expression to select that will generate the same value by
2105/// multiplying by a magic number. See:
2106/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
2107SDOperand TargetLowering::BuildUDIV(SDNode *N, SelectionDAG &DAG,
Anton Korobeynikovbed29462007-04-16 18:10:23 +00002108 std::vector<SDNode*>* Created) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002109 MVT VT = N->getValueType(0);
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002110
2111 // Check to see if we can do this.
2112 if (!isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64))
2113 return SDOperand(); // BuildUDIV only operates on i32 or i64
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002114
2115 uint64_t d = cast<ConstantSDNode>(N->getOperand(1))->getValue();
2116 mu magics = (VT == MVT::i32) ? magicu32(d) : magicu64(d);
2117
2118 // Multiply the numerator (operand 0) by the magic value
Dan Gohman525178c2007-10-08 18:33:35 +00002119 SDOperand Q;
2120 if (isOperationLegal(ISD::MULHU, VT))
2121 Q = DAG.getNode(ISD::MULHU, VT, N->getOperand(0),
2122 DAG.getConstant(magics.m, VT));
2123 else if (isOperationLegal(ISD::UMUL_LOHI, VT))
2124 Q = SDOperand(DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(VT, VT),
2125 N->getOperand(0),
2126 DAG.getConstant(magics.m, VT)).Val, 1);
2127 else
2128 return SDOperand(); // No mulhu or equvialent
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002129 if (Created)
2130 Created->push_back(Q.Val);
2131
2132 if (magics.a == 0) {
2133 return DAG.getNode(ISD::SRL, VT, Q,
2134 DAG.getConstant(magics.s, getShiftAmountTy()));
2135 } else {
2136 SDOperand NPQ = DAG.getNode(ISD::SUB, VT, N->getOperand(0), Q);
2137 if (Created)
2138 Created->push_back(NPQ.Val);
2139 NPQ = DAG.getNode(ISD::SRL, VT, NPQ,
2140 DAG.getConstant(1, getShiftAmountTy()));
2141 if (Created)
2142 Created->push_back(NPQ.Val);
2143 NPQ = DAG.getNode(ISD::ADD, VT, NPQ, Q);
2144 if (Created)
2145 Created->push_back(NPQ.Val);
2146 return DAG.getNode(ISD::SRL, VT, NPQ,
2147 DAG.getConstant(magics.s-1, getShiftAmountTy()));
2148 }
2149}