blob: eab96a4f47ee665d80654de5872491f1318a97c2 [file] [log] [blame]
Nate Begeman1d9d7422005-10-18 00:28:58 +00001//===-- PPCISelLowering.cpp - PPC DAG Lowering Implementation -------------===//
Chris Lattner7c5a3d32005-08-16 17:14:42 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Nate Begeman21e463b2005-10-16 05:39:50 +000010// This file implements the PPCISelLowering class.
Chris Lattner7c5a3d32005-08-16 17:14:42 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner16e71f22005-10-14 23:59:06 +000014#include "PPCISelLowering.h"
15#include "PPCTargetMachine.h"
Nate Begeman750ac1b2006-02-01 07:19:44 +000016#include "llvm/ADT/VectorExtras.h"
Evan Chengc4c62572006-03-13 23:20:37 +000017#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000018#include "llvm/CodeGen/MachineFrameInfo.h"
19#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner8a2d3ca2005-08-26 21:23:58 +000020#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000021#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner7b738342005-09-13 19:33:40 +000022#include "llvm/CodeGen/SSARegMap.h"
Chris Lattner0b1e4e52005-08-26 17:36:52 +000023#include "llvm/Constants.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000024#include "llvm/Function.h"
Chris Lattner6d92cad2006-03-26 10:06:40 +000025#include "llvm/Intrinsics.h"
Nate Begeman750ac1b2006-02-01 07:19:44 +000026#include "llvm/Support/MathExtras.h"
Evan Chengd2ee2182006-02-18 00:08:58 +000027#include "llvm/Target/TargetOptions.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000028using namespace llvm;
29
Nate Begeman21e463b2005-10-16 05:39:50 +000030PPCTargetLowering::PPCTargetLowering(TargetMachine &TM)
Chris Lattner7c5a3d32005-08-16 17:14:42 +000031 : TargetLowering(TM) {
32
33 // Fold away setcc operations if possible.
34 setSetCCIsExpensive();
Nate Begeman405e3ec2005-10-21 00:02:42 +000035 setPow2DivIsCheap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +000036
Chris Lattnerd145a612005-09-27 22:18:25 +000037 // Use _setjmp/_longjmp instead of setjmp/longjmp.
38 setUseUnderscoreSetJmpLongJmp(true);
39
Chris Lattner7c5a3d32005-08-16 17:14:42 +000040 // Set up the register classes.
Nate Begeman1d9d7422005-10-18 00:28:58 +000041 addRegisterClass(MVT::i32, PPC::GPRCRegisterClass);
42 addRegisterClass(MVT::f32, PPC::F4RCRegisterClass);
43 addRegisterClass(MVT::f64, PPC::F8RCRegisterClass);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000044
Chris Lattnera54aa942006-01-29 06:26:08 +000045 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
46 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
47
Chris Lattner7c5a3d32005-08-16 17:14:42 +000048 // PowerPC has no intrinsics for these particular operations
49 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
50 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
51 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
52
53 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
54 setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
55 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
56
57 // PowerPC has no SREM/UREM instructions
58 setOperationAction(ISD::SREM, MVT::i32, Expand);
59 setOperationAction(ISD::UREM, MVT::i32, Expand);
60
61 // We don't support sin/cos/sqrt/fmod
62 setOperationAction(ISD::FSIN , MVT::f64, Expand);
63 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattner615c2d02005-09-28 22:29:58 +000064 setOperationAction(ISD::FREM , MVT::f64, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000065 setOperationAction(ISD::FSIN , MVT::f32, Expand);
66 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattner615c2d02005-09-28 22:29:58 +000067 setOperationAction(ISD::FREM , MVT::f32, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000068
69 // If we're enabling GP optimizations, use hardware square root
Chris Lattner1e9de3e2005-09-02 18:33:05 +000070 if (!TM.getSubtarget<PPCSubtarget>().hasFSQRT()) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +000071 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
72 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
73 }
74
Chris Lattner9601a862006-03-05 05:08:37 +000075 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
76 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
77
Nate Begemand88fc032006-01-14 03:14:10 +000078 // PowerPC does not have BSWAP, CTPOP or CTTZ
79 setOperationAction(ISD::BSWAP, MVT::i32 , Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000080 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
81 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
82
Nate Begeman35ef9132006-01-11 21:21:00 +000083 // PowerPC does not have ROTR
84 setOperationAction(ISD::ROTR, MVT::i32 , Expand);
85
Chris Lattner7c5a3d32005-08-16 17:14:42 +000086 // PowerPC does not have Select
87 setOperationAction(ISD::SELECT, MVT::i32, Expand);
88 setOperationAction(ISD::SELECT, MVT::f32, Expand);
89 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +000090
Chris Lattner0b1e4e52005-08-26 17:36:52 +000091 // PowerPC wants to turn select_cc of FP into fsel when possible.
92 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
93 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Nate Begeman44775902006-01-31 08:17:29 +000094
Nate Begeman750ac1b2006-02-01 07:19:44 +000095 // PowerPC wants to optimize integer setcc a bit
Nate Begeman44775902006-01-31 08:17:29 +000096 setOperationAction(ISD::SETCC, MVT::i32, Custom);
Chris Lattnereb9b62e2005-08-31 19:09:57 +000097
Nate Begeman81e80972006-03-17 01:40:33 +000098 // PowerPC does not have BRCOND which requires SetCC
99 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000100
Chris Lattnerf7605322005-08-31 21:09:52 +0000101 // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
102 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000103
Jim Laskeyad23c9d2005-08-17 00:40:22 +0000104 // PowerPC does not have [U|S]INT_TO_FP
105 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
106 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
107
Chris Lattner53e88452005-12-23 05:13:35 +0000108 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
109 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
110
Chris Lattnere6ec9f22005-09-10 00:21:06 +0000111 // PowerPC does not have truncstore for i1.
112 setOperationAction(ISD::TRUNCSTORE, MVT::i1, Promote);
Chris Lattnerf73bae12005-11-29 06:16:21 +0000113
Jim Laskeyabf6d172006-01-05 01:25:28 +0000114 // Support label based line numbers.
Chris Lattnerf73bae12005-11-29 06:16:21 +0000115 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000116 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000117 // FIXME - use subtarget debug flags
Jim Laskeye0bce712006-01-05 01:47:43 +0000118 if (!TM.getSubtarget<PPCSubtarget>().isDarwin())
Jim Laskeyabf6d172006-01-05 01:25:28 +0000119 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattnere6ec9f22005-09-10 00:21:06 +0000120
Nate Begeman28a6b022005-12-10 02:36:00 +0000121 // We want to legalize GlobalAddress and ConstantPool nodes into the
122 // appropriate instructions to materialize the address.
Chris Lattner3eef4e32005-11-17 18:26:56 +0000123 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Nate Begeman28a6b022005-12-10 02:36:00 +0000124 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000125
Nate Begemanee625572006-01-27 21:09:22 +0000126 // RET must be custom lowered, to meet ABI requirements
127 setOperationAction(ISD::RET , MVT::Other, Custom);
128
Nate Begemanacc398c2006-01-25 18:21:52 +0000129 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
130 setOperationAction(ISD::VASTART , MVT::Other, Custom);
131
Chris Lattnerb22c08b2006-01-15 09:02:48 +0000132 // Use the default implementation.
Nate Begemanacc398c2006-01-25 18:21:52 +0000133 setOperationAction(ISD::VAARG , MVT::Other, Expand);
134 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
135 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Chris Lattnerb22c08b2006-01-15 09:02:48 +0000136 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
137 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
138 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Chris Lattner860e8862005-11-17 07:30:41 +0000139
Chris Lattner6d92cad2006-03-26 10:06:40 +0000140 // We want to custom lower some of our intrinsics.
Chris Lattner48b61a72006-03-28 00:40:33 +0000141 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Chris Lattner6d92cad2006-03-26 10:06:40 +0000142
Nate Begemanc09eeec2005-09-06 22:03:27 +0000143 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000144 // They also have instructions for converting between i64 and fp.
Nate Begemanc09eeec2005-09-06 22:03:27 +0000145 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
146 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Chris Lattner7fbcef72006-03-24 07:53:47 +0000147
148 // FIXME: disable this lowered code. This generates 64-bit register values,
149 // and we don't model the fact that the top part is clobbered by calls. We
150 // need to flag these together so that the value isn't live across a call.
151 //setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
152
Nate Begemanae749a92005-10-25 23:48:36 +0000153 // To take advantage of the above i64 FP_TO_SINT, promote i32 FP_TO_UINT
154 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Promote);
155 } else {
Chris Lattner860e8862005-11-17 07:30:41 +0000156 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Nate Begemanae749a92005-10-25 23:48:36 +0000157 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begeman9d2b8172005-10-18 00:56:42 +0000158 }
159
160 if (TM.getSubtarget<PPCSubtarget>().has64BitRegs()) {
161 // 64 bit PowerPC implementations can support i64 types directly
162 addRegisterClass(MVT::i64, PPC::G8RCRegisterClass);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000163 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
164 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000165 } else {
166 // 32 bit PowerPC wants to expand i64 shifts itself.
167 setOperationAction(ISD::SHL, MVT::i64, Custom);
168 setOperationAction(ISD::SRL, MVT::i64, Custom);
169 setOperationAction(ISD::SRA, MVT::i64, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000170 }
Evan Chengd30bf012006-03-01 01:11:20 +0000171
Nate Begeman425a9692005-11-29 08:17:20 +0000172 if (TM.getSubtarget<PPCSubtarget>().hasAltivec()) {
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000173 // First set operation action for all vector types to expand. Then we
174 // will selectively turn on ones that can be effectively codegen'd.
175 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
176 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Chris Lattnerf3f69de2006-04-16 01:37:57 +0000177 // add/sub are legal for all supported vector VT's.
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000178 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Legal);
179 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Legal);
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000180
Chris Lattner7ff7e672006-04-04 17:25:31 +0000181 // We promote all shuffles to v16i8.
182 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Promote);
Chris Lattnerf3f69de2006-04-16 01:37:57 +0000183 AddPromotedToType (ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, MVT::v16i8);
184
185 // We promote all non-typed operations to v4i32.
186 setOperationAction(ISD::AND , (MVT::ValueType)VT, Promote);
187 AddPromotedToType (ISD::AND , (MVT::ValueType)VT, MVT::v4i32);
188 setOperationAction(ISD::OR , (MVT::ValueType)VT, Promote);
189 AddPromotedToType (ISD::OR , (MVT::ValueType)VT, MVT::v4i32);
190 setOperationAction(ISD::XOR , (MVT::ValueType)VT, Promote);
191 AddPromotedToType (ISD::XOR , (MVT::ValueType)VT, MVT::v4i32);
192 setOperationAction(ISD::LOAD , (MVT::ValueType)VT, Promote);
193 AddPromotedToType (ISD::LOAD , (MVT::ValueType)VT, MVT::v4i32);
194 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
195 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v4i32);
196 setOperationAction(ISD::STORE, (MVT::ValueType)VT, Promote);
197 AddPromotedToType (ISD::STORE, (MVT::ValueType)VT, MVT::v4i32);
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000198
Chris Lattnerf3f69de2006-04-16 01:37:57 +0000199 // No other operations are legal.
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000200 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
201 setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand);
202 setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand);
203 setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand);
204 setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand);
205 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
206 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
207 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Expand);
Chris Lattner01cae072006-04-03 23:55:43 +0000208
209 setOperationAction(ISD::SCALAR_TO_VECTOR, (MVT::ValueType)VT, Expand);
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000210 }
211
Chris Lattner7ff7e672006-04-04 17:25:31 +0000212 // We can custom expand all VECTOR_SHUFFLEs to VPERM, others we can handle
213 // with merges, splats, etc.
214 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i8, Custom);
215
Chris Lattnerf3f69de2006-04-16 01:37:57 +0000216 setOperationAction(ISD::AND , MVT::v4i32, Legal);
217 setOperationAction(ISD::OR , MVT::v4i32, Legal);
218 setOperationAction(ISD::XOR , MVT::v4i32, Legal);
219 setOperationAction(ISD::LOAD , MVT::v4i32, Legal);
220 setOperationAction(ISD::SELECT, MVT::v4i32, Expand);
221 setOperationAction(ISD::STORE , MVT::v4i32, Legal);
222
Nate Begeman425a9692005-11-29 08:17:20 +0000223 addRegisterClass(MVT::v4f32, PPC::VRRCRegisterClass);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000224 addRegisterClass(MVT::v4i32, PPC::VRRCRegisterClass);
Chris Lattner8d052bc2006-03-25 07:39:07 +0000225 addRegisterClass(MVT::v8i16, PPC::VRRCRegisterClass);
226 addRegisterClass(MVT::v16i8, PPC::VRRCRegisterClass);
Chris Lattnerec4a0c72006-01-29 06:32:58 +0000227
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000228 setOperationAction(ISD::MUL, MVT::v4f32, Legal);
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000229
Chris Lattnerb2177b92006-03-19 06:55:52 +0000230 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom);
231 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Custom);
Chris Lattner64b3a082006-03-24 07:48:08 +0000232
Chris Lattner541f91b2006-04-02 00:43:36 +0000233 setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Custom);
234 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Custom);
Chris Lattner64b3a082006-03-24 07:48:08 +0000235 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Custom);
236 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
Nate Begeman425a9692005-11-29 08:17:20 +0000237 }
238
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000239 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattnercadd7422006-01-13 17:52:03 +0000240 setStackPointerRegisterToSaveRestore(PPC::R1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000241
Chris Lattner8c13d0a2006-03-01 04:57:39 +0000242 // We have target-specific dag combine patterns for the following nodes:
243 setTargetDAGCombine(ISD::SINT_TO_FP);
Chris Lattner51269842006-03-01 05:50:56 +0000244 setTargetDAGCombine(ISD::STORE);
Chris Lattner8c13d0a2006-03-01 04:57:39 +0000245
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000246 computeRegisterProperties();
247}
248
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000249const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
250 switch (Opcode) {
251 default: return 0;
252 case PPCISD::FSEL: return "PPCISD::FSEL";
253 case PPCISD::FCFID: return "PPCISD::FCFID";
254 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
255 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
Chris Lattner51269842006-03-01 05:50:56 +0000256 case PPCISD::STFIWX: return "PPCISD::STFIWX";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000257 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
258 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000259 case PPCISD::VPERM: return "PPCISD::VPERM";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000260 case PPCISD::Hi: return "PPCISD::Hi";
261 case PPCISD::Lo: return "PPCISD::Lo";
262 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
263 case PPCISD::SRL: return "PPCISD::SRL";
264 case PPCISD::SRA: return "PPCISD::SRA";
265 case PPCISD::SHL: return "PPCISD::SHL";
Chris Lattnerecfe55e2006-03-22 05:30:33 +0000266 case PPCISD::EXTSW_32: return "PPCISD::EXTSW_32";
267 case PPCISD::STD_32: return "PPCISD::STD_32";
Chris Lattnere00ebf02006-01-28 07:33:03 +0000268 case PPCISD::CALL: return "PPCISD::CALL";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000269 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
Chris Lattner6d92cad2006-03-26 10:06:40 +0000270 case PPCISD::MFCR: return "PPCISD::MFCR";
Chris Lattnera17b1552006-03-31 05:13:27 +0000271 case PPCISD::VCMP: return "PPCISD::VCMP";
Chris Lattner6d92cad2006-03-26 10:06:40 +0000272 case PPCISD::VCMPo: return "PPCISD::VCMPo";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000273 }
274}
275
Chris Lattner1a635d62006-04-14 06:01:58 +0000276//===----------------------------------------------------------------------===//
277// Node matching predicates, for use by the tblgen matching code.
278//===----------------------------------------------------------------------===//
279
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000280/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
281static bool isFloatingPointZero(SDOperand Op) {
282 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
283 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
284 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
285 // Maybe this has already been legalized into the constant pool?
286 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
287 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
288 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
289 }
290 return false;
291}
292
Chris Lattnerddb739e2006-04-06 17:23:16 +0000293/// isConstantOrUndef - Op is either an undef node or a ConstantSDNode. Return
294/// true if Op is undef or if it matches the specified value.
295static bool isConstantOrUndef(SDOperand Op, unsigned Val) {
296 return Op.getOpcode() == ISD::UNDEF ||
297 cast<ConstantSDNode>(Op)->getValue() == Val;
298}
299
300/// isVPKUHUMShuffleMask - Return true if this is the shuffle mask for a
301/// VPKUHUM instruction.
Chris Lattnerf24380e2006-04-06 22:28:36 +0000302bool PPC::isVPKUHUMShuffleMask(SDNode *N, bool isUnary) {
303 if (!isUnary) {
304 for (unsigned i = 0; i != 16; ++i)
305 if (!isConstantOrUndef(N->getOperand(i), i*2+1))
306 return false;
307 } else {
308 for (unsigned i = 0; i != 8; ++i)
309 if (!isConstantOrUndef(N->getOperand(i), i*2+1) ||
310 !isConstantOrUndef(N->getOperand(i+8), i*2+1))
311 return false;
312 }
Chris Lattnerd0608e12006-04-06 18:26:28 +0000313 return true;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000314}
315
316/// isVPKUWUMShuffleMask - Return true if this is the shuffle mask for a
317/// VPKUWUM instruction.
Chris Lattnerf24380e2006-04-06 22:28:36 +0000318bool PPC::isVPKUWUMShuffleMask(SDNode *N, bool isUnary) {
319 if (!isUnary) {
320 for (unsigned i = 0; i != 16; i += 2)
321 if (!isConstantOrUndef(N->getOperand(i ), i*2+2) ||
322 !isConstantOrUndef(N->getOperand(i+1), i*2+3))
323 return false;
324 } else {
325 for (unsigned i = 0; i != 8; i += 2)
326 if (!isConstantOrUndef(N->getOperand(i ), i*2+2) ||
327 !isConstantOrUndef(N->getOperand(i+1), i*2+3) ||
328 !isConstantOrUndef(N->getOperand(i+8), i*2+2) ||
329 !isConstantOrUndef(N->getOperand(i+9), i*2+3))
330 return false;
331 }
Chris Lattnerd0608e12006-04-06 18:26:28 +0000332 return true;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000333}
334
Chris Lattnercaad1632006-04-06 22:02:42 +0000335/// isVMerge - Common function, used to match vmrg* shuffles.
336///
337static bool isVMerge(SDNode *N, unsigned UnitSize,
338 unsigned LHSStart, unsigned RHSStart) {
Chris Lattner116cc482006-04-06 21:11:54 +0000339 assert(N->getOpcode() == ISD::BUILD_VECTOR &&
340 N->getNumOperands() == 16 && "PPC only supports shuffles by bytes!");
341 assert((UnitSize == 1 || UnitSize == 2 || UnitSize == 4) &&
342 "Unsupported merge size!");
343
344 for (unsigned i = 0; i != 8/UnitSize; ++i) // Step over units
345 for (unsigned j = 0; j != UnitSize; ++j) { // Step over bytes within unit
346 if (!isConstantOrUndef(N->getOperand(i*UnitSize*2+j),
Chris Lattnercaad1632006-04-06 22:02:42 +0000347 LHSStart+j+i*UnitSize) ||
Chris Lattner116cc482006-04-06 21:11:54 +0000348 !isConstantOrUndef(N->getOperand(i*UnitSize*2+UnitSize+j),
Chris Lattnercaad1632006-04-06 22:02:42 +0000349 RHSStart+j+i*UnitSize))
Chris Lattner116cc482006-04-06 21:11:54 +0000350 return false;
351 }
Chris Lattnercaad1632006-04-06 22:02:42 +0000352 return true;
353}
354
355/// isVMRGLShuffleMask - Return true if this is a shuffle mask suitable for
356/// a VRGL* instruction with the specified unit size (1,2 or 4 bytes).
357bool PPC::isVMRGLShuffleMask(SDNode *N, unsigned UnitSize, bool isUnary) {
358 if (!isUnary)
359 return isVMerge(N, UnitSize, 8, 24);
360 return isVMerge(N, UnitSize, 8, 8);
Chris Lattner116cc482006-04-06 21:11:54 +0000361}
362
363/// isVMRGHShuffleMask - Return true if this is a shuffle mask suitable for
364/// a VRGH* instruction with the specified unit size (1,2 or 4 bytes).
Chris Lattnercaad1632006-04-06 22:02:42 +0000365bool PPC::isVMRGHShuffleMask(SDNode *N, unsigned UnitSize, bool isUnary) {
366 if (!isUnary)
367 return isVMerge(N, UnitSize, 0, 16);
368 return isVMerge(N, UnitSize, 0, 0);
Chris Lattner116cc482006-04-06 21:11:54 +0000369}
370
371
Chris Lattnerd0608e12006-04-06 18:26:28 +0000372/// isVSLDOIShuffleMask - If this is a vsldoi shuffle mask, return the shift
373/// amount, otherwise return -1.
Chris Lattnerf24380e2006-04-06 22:28:36 +0000374int PPC::isVSLDOIShuffleMask(SDNode *N, bool isUnary) {
Chris Lattner116cc482006-04-06 21:11:54 +0000375 assert(N->getOpcode() == ISD::BUILD_VECTOR &&
376 N->getNumOperands() == 16 && "PPC only supports shuffles by bytes!");
Chris Lattnerd0608e12006-04-06 18:26:28 +0000377 // Find the first non-undef value in the shuffle mask.
378 unsigned i;
379 for (i = 0; i != 16 && N->getOperand(i).getOpcode() == ISD::UNDEF; ++i)
380 /*search*/;
381
382 if (i == 16) return -1; // all undef.
383
384 // Otherwise, check to see if the rest of the elements are consequtively
385 // numbered from this value.
386 unsigned ShiftAmt = cast<ConstantSDNode>(N->getOperand(i))->getValue();
387 if (ShiftAmt < i) return -1;
388 ShiftAmt -= i;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000389
Chris Lattnerf24380e2006-04-06 22:28:36 +0000390 if (!isUnary) {
391 // Check the rest of the elements to see if they are consequtive.
392 for (++i; i != 16; ++i)
393 if (!isConstantOrUndef(N->getOperand(i), ShiftAmt+i))
394 return -1;
395 } else {
396 // Check the rest of the elements to see if they are consequtive.
397 for (++i; i != 16; ++i)
398 if (!isConstantOrUndef(N->getOperand(i), (ShiftAmt+i) & 15))
399 return -1;
400 }
Chris Lattnerd0608e12006-04-06 18:26:28 +0000401
402 return ShiftAmt;
403}
Chris Lattneref819f82006-03-20 06:33:01 +0000404
405/// isSplatShuffleMask - Return true if the specified VECTOR_SHUFFLE operand
406/// specifies a splat of a single element that is suitable for input to
407/// VSPLTB/VSPLTH/VSPLTW.
Chris Lattner7ff7e672006-04-04 17:25:31 +0000408bool PPC::isSplatShuffleMask(SDNode *N, unsigned EltSize) {
409 assert(N->getOpcode() == ISD::BUILD_VECTOR &&
410 N->getNumOperands() == 16 &&
411 (EltSize == 1 || EltSize == 2 || EltSize == 4));
Chris Lattnerdd4d2d02006-03-20 06:51:10 +0000412
Chris Lattner88a99ef2006-03-20 06:37:44 +0000413 // This is a splat operation if each element of the permute is the same, and
414 // if the value doesn't reference the second vector.
Chris Lattner7ff7e672006-04-04 17:25:31 +0000415 unsigned ElementBase = 0;
Chris Lattner88a99ef2006-03-20 06:37:44 +0000416 SDOperand Elt = N->getOperand(0);
Chris Lattner7ff7e672006-04-04 17:25:31 +0000417 if (ConstantSDNode *EltV = dyn_cast<ConstantSDNode>(Elt))
418 ElementBase = EltV->getValue();
419 else
420 return false; // FIXME: Handle UNDEF elements too!
421
422 if (cast<ConstantSDNode>(Elt)->getValue() >= 16)
423 return false;
424
425 // Check that they are consequtive.
426 for (unsigned i = 1; i != EltSize; ++i) {
427 if (!isa<ConstantSDNode>(N->getOperand(i)) ||
428 cast<ConstantSDNode>(N->getOperand(i))->getValue() != i+ElementBase)
429 return false;
430 }
431
Chris Lattner88a99ef2006-03-20 06:37:44 +0000432 assert(isa<ConstantSDNode>(Elt) && "Invalid VECTOR_SHUFFLE mask!");
Chris Lattner7ff7e672006-04-04 17:25:31 +0000433 for (unsigned i = EltSize, e = 16; i != e; i += EltSize) {
Chris Lattnerb097aa92006-04-14 23:19:08 +0000434 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Chris Lattner88a99ef2006-03-20 06:37:44 +0000435 assert(isa<ConstantSDNode>(N->getOperand(i)) &&
436 "Invalid VECTOR_SHUFFLE mask!");
Chris Lattner7ff7e672006-04-04 17:25:31 +0000437 for (unsigned j = 0; j != EltSize; ++j)
438 if (N->getOperand(i+j) != N->getOperand(j))
439 return false;
Chris Lattner88a99ef2006-03-20 06:37:44 +0000440 }
441
Chris Lattner7ff7e672006-04-04 17:25:31 +0000442 return true;
Chris Lattneref819f82006-03-20 06:33:01 +0000443}
444
445/// getVSPLTImmediate - Return the appropriate VSPLT* immediate to splat the
446/// specified isSplatShuffleMask VECTOR_SHUFFLE mask.
Chris Lattner7ff7e672006-04-04 17:25:31 +0000447unsigned PPC::getVSPLTImmediate(SDNode *N, unsigned EltSize) {
448 assert(isSplatShuffleMask(N, EltSize));
449 return cast<ConstantSDNode>(N->getOperand(0))->getValue() / EltSize;
Chris Lattneref819f82006-03-20 06:33:01 +0000450}
451
Chris Lattnere87192a2006-04-12 17:37:20 +0000452/// get_VSPLTI_elt - If this is a build_vector of constants which can be formed
Chris Lattner140a58f2006-04-08 06:46:53 +0000453/// by using a vspltis[bhw] instruction of the specified element size, return
454/// the constant being splatted. The ByteSize field indicates the number of
455/// bytes of each element [124] -> [bhw].
Chris Lattnere87192a2006-04-12 17:37:20 +0000456SDOperand PPC::get_VSPLTI_elt(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000457 SDOperand OpVal(0, 0);
Chris Lattner79d9a882006-04-08 07:14:26 +0000458
459 // If ByteSize of the splat is bigger than the element size of the
460 // build_vector, then we have a case where we are checking for a splat where
461 // multiple elements of the buildvector are folded together into a single
462 // logical element of the splat (e.g. "vsplish 1" to splat {0,1}*8).
463 unsigned EltSize = 16/N->getNumOperands();
464 if (EltSize < ByteSize) {
465 unsigned Multiple = ByteSize/EltSize; // Number of BV entries per spltval.
466 SDOperand UniquedVals[4];
467 assert(Multiple > 1 && Multiple <= 4 && "How can this happen?");
468
469 // See if all of the elements in the buildvector agree across.
470 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
471 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
472 // If the element isn't a constant, bail fully out.
473 if (!isa<ConstantSDNode>(N->getOperand(i))) return SDOperand();
474
475
476 if (UniquedVals[i&(Multiple-1)].Val == 0)
477 UniquedVals[i&(Multiple-1)] = N->getOperand(i);
478 else if (UniquedVals[i&(Multiple-1)] != N->getOperand(i))
479 return SDOperand(); // no match.
480 }
481
482 // Okay, if we reached this point, UniquedVals[0..Multiple-1] contains
483 // either constant or undef values that are identical for each chunk. See
484 // if these chunks can form into a larger vspltis*.
485
486 // Check to see if all of the leading entries are either 0 or -1. If
487 // neither, then this won't fit into the immediate field.
488 bool LeadingZero = true;
489 bool LeadingOnes = true;
490 for (unsigned i = 0; i != Multiple-1; ++i) {
491 if (UniquedVals[i].Val == 0) continue; // Must have been undefs.
492
493 LeadingZero &= cast<ConstantSDNode>(UniquedVals[i])->isNullValue();
494 LeadingOnes &= cast<ConstantSDNode>(UniquedVals[i])->isAllOnesValue();
495 }
496 // Finally, check the least significant entry.
497 if (LeadingZero) {
498 if (UniquedVals[Multiple-1].Val == 0)
499 return DAG.getTargetConstant(0, MVT::i32); // 0,0,0,undef
500 int Val = cast<ConstantSDNode>(UniquedVals[Multiple-1])->getValue();
501 if (Val < 16)
502 return DAG.getTargetConstant(Val, MVT::i32); // 0,0,0,4 -> vspltisw(4)
503 }
504 if (LeadingOnes) {
505 if (UniquedVals[Multiple-1].Val == 0)
506 return DAG.getTargetConstant(~0U, MVT::i32); // -1,-1,-1,undef
507 int Val =cast<ConstantSDNode>(UniquedVals[Multiple-1])->getSignExtended();
508 if (Val >= -16) // -1,-1,-1,-2 -> vspltisw(-2)
509 return DAG.getTargetConstant(Val, MVT::i32);
510 }
511
512 return SDOperand();
513 }
514
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000515 // Check to see if this buildvec has a single non-undef value in its elements.
516 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
517 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
518 if (OpVal.Val == 0)
519 OpVal = N->getOperand(i);
520 else if (OpVal != N->getOperand(i))
Chris Lattner140a58f2006-04-08 06:46:53 +0000521 return SDOperand();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000522 }
523
Chris Lattner140a58f2006-04-08 06:46:53 +0000524 if (OpVal.Val == 0) return SDOperand(); // All UNDEF: use implicit def.
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000525
Nate Begeman98e70cc2006-03-28 04:15:58 +0000526 unsigned ValSizeInBytes = 0;
527 uint64_t Value = 0;
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000528 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
529 Value = CN->getValue();
530 ValSizeInBytes = MVT::getSizeInBits(CN->getValueType(0))/8;
531 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) {
532 assert(CN->getValueType(0) == MVT::f32 && "Only one legal FP vector type!");
533 Value = FloatToBits(CN->getValue());
534 ValSizeInBytes = 4;
535 }
536
537 // If the splat value is larger than the element value, then we can never do
538 // this splat. The only case that we could fit the replicated bits into our
539 // immediate field for would be zero, and we prefer to use vxor for it.
Chris Lattner140a58f2006-04-08 06:46:53 +0000540 if (ValSizeInBytes < ByteSize) return SDOperand();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000541
542 // If the element value is larger than the splat value, cut it in half and
543 // check to see if the two halves are equal. Continue doing this until we
544 // get to ByteSize. This allows us to handle 0x01010101 as 0x01.
545 while (ValSizeInBytes > ByteSize) {
546 ValSizeInBytes >>= 1;
547
548 // If the top half equals the bottom half, we're still ok.
Chris Lattner9b42bdd2006-04-05 17:39:25 +0000549 if (((Value >> (ValSizeInBytes*8)) & ((1 << (8*ValSizeInBytes))-1)) !=
550 (Value & ((1 << (8*ValSizeInBytes))-1)))
Chris Lattner140a58f2006-04-08 06:46:53 +0000551 return SDOperand();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000552 }
553
554 // Properly sign extend the value.
555 int ShAmt = (4-ByteSize)*8;
556 int MaskVal = ((int)Value << ShAmt) >> ShAmt;
557
Evan Cheng5b6a01b2006-03-26 09:52:32 +0000558 // If this is zero, don't match, zero matches ISD::isBuildVectorAllZeros.
Chris Lattner140a58f2006-04-08 06:46:53 +0000559 if (MaskVal == 0) return SDOperand();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000560
Chris Lattner140a58f2006-04-08 06:46:53 +0000561 // Finally, if this value fits in a 5 bit sext field, return it
562 if (((MaskVal << (32-5)) >> (32-5)) == MaskVal)
563 return DAG.getTargetConstant(MaskVal, MVT::i32);
564 return SDOperand();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000565}
566
Chris Lattner1a635d62006-04-14 06:01:58 +0000567//===----------------------------------------------------------------------===//
568// LowerOperation implementation
569//===----------------------------------------------------------------------===//
570
571static SDOperand LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
572 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
573 Constant *C = CP->get();
574 SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i32, CP->getAlignment());
575 SDOperand Zero = DAG.getConstant(0, MVT::i32);
576
577 const TargetMachine &TM = DAG.getTarget();
578
579 // If this is a non-darwin platform, we don't support non-static relo models
580 // yet.
581 if (TM.getRelocationModel() == Reloc::Static ||
582 !TM.getSubtarget<PPCSubtarget>().isDarwin()) {
583 // Generate non-pic code that has direct accesses to the constant pool.
584 // The address of the global is just (hi(&g)+lo(&g)).
585 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
586 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
587 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
588 }
589
590 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
591 if (TM.getRelocationModel() == Reloc::PIC) {
592 // With PIC, the first instruction is actually "GR+hi(&G)".
593 Hi = DAG.getNode(ISD::ADD, MVT::i32,
594 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
595 }
596
597 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
598 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
599 return Lo;
600}
601
602static SDOperand LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
603 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
604 GlobalValue *GV = GSDN->getGlobal();
605 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32, GSDN->getOffset());
606 SDOperand Zero = DAG.getConstant(0, MVT::i32);
607
608 const TargetMachine &TM = DAG.getTarget();
609
610 // If this is a non-darwin platform, we don't support non-static relo models
611 // yet.
612 if (TM.getRelocationModel() == Reloc::Static ||
613 !TM.getSubtarget<PPCSubtarget>().isDarwin()) {
614 // Generate non-pic code that has direct accesses to globals.
615 // The address of the global is just (hi(&g)+lo(&g)).
616 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
617 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
618 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
619 }
620
621 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
622 if (TM.getRelocationModel() == Reloc::PIC) {
623 // With PIC, the first instruction is actually "GR+hi(&G)".
624 Hi = DAG.getNode(ISD::ADD, MVT::i32,
625 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
626 }
627
628 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
629 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
630
631 if (!GV->hasWeakLinkage() && !GV->hasLinkOnceLinkage() &&
632 (!GV->isExternal() || GV->hasNotBeenReadFromBytecode()))
633 return Lo;
634
635 // If the global is weak or external, we have to go through the lazy
636 // resolution stub.
637 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), Lo, DAG.getSrcValue(0));
638}
639
640static SDOperand LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
641 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
642
643 // If we're comparing for equality to zero, expose the fact that this is
644 // implented as a ctlz/srl pair on ppc, so that the dag combiner can
645 // fold the new nodes.
646 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
647 if (C->isNullValue() && CC == ISD::SETEQ) {
648 MVT::ValueType VT = Op.getOperand(0).getValueType();
649 SDOperand Zext = Op.getOperand(0);
650 if (VT < MVT::i32) {
651 VT = MVT::i32;
652 Zext = DAG.getNode(ISD::ZERO_EXTEND, VT, Op.getOperand(0));
653 }
654 unsigned Log2b = Log2_32(MVT::getSizeInBits(VT));
655 SDOperand Clz = DAG.getNode(ISD::CTLZ, VT, Zext);
656 SDOperand Scc = DAG.getNode(ISD::SRL, VT, Clz,
657 DAG.getConstant(Log2b, MVT::i32));
658 return DAG.getNode(ISD::TRUNCATE, MVT::i32, Scc);
659 }
660 // Leave comparisons against 0 and -1 alone for now, since they're usually
661 // optimized. FIXME: revisit this when we can custom lower all setcc
662 // optimizations.
663 if (C->isAllOnesValue() || C->isNullValue())
664 return SDOperand();
665 }
666
667 // If we have an integer seteq/setne, turn it into a compare against zero
668 // by subtracting the rhs from the lhs, which is faster than setting a
669 // condition register, reading it back out, and masking the correct bit.
670 MVT::ValueType LHSVT = Op.getOperand(0).getValueType();
671 if (MVT::isInteger(LHSVT) && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
672 MVT::ValueType VT = Op.getValueType();
673 SDOperand Sub = DAG.getNode(ISD::SUB, LHSVT, Op.getOperand(0),
674 Op.getOperand(1));
675 return DAG.getSetCC(VT, Sub, DAG.getConstant(0, LHSVT), CC);
676 }
677 return SDOperand();
678}
679
680static SDOperand LowerVASTART(SDOperand Op, SelectionDAG &DAG,
681 unsigned VarArgsFrameIndex) {
682 // vastart just stores the address of the VarArgsFrameIndex slot into the
683 // memory location argument.
684 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
685 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
686 Op.getOperand(1), Op.getOperand(2));
687}
688
689static SDOperand LowerRET(SDOperand Op, SelectionDAG &DAG) {
690 SDOperand Copy;
691 switch(Op.getNumOperands()) {
692 default:
693 assert(0 && "Do not know how to return this many arguments!");
694 abort();
695 case 1:
696 return SDOperand(); // ret void is legal
697 case 2: {
698 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
699 unsigned ArgReg;
700 if (MVT::isVector(ArgVT))
701 ArgReg = PPC::V2;
702 else if (MVT::isInteger(ArgVT))
703 ArgReg = PPC::R3;
704 else {
705 assert(MVT::isFloatingPoint(ArgVT));
706 ArgReg = PPC::F1;
707 }
708
709 Copy = DAG.getCopyToReg(Op.getOperand(0), ArgReg, Op.getOperand(1),
710 SDOperand());
711
712 // If we haven't noted the R3/F1 are live out, do so now.
713 if (DAG.getMachineFunction().liveout_empty())
714 DAG.getMachineFunction().addLiveOut(ArgReg);
715 break;
716 }
717 case 3:
718 Copy = DAG.getCopyToReg(Op.getOperand(0), PPC::R3, Op.getOperand(2),
719 SDOperand());
720 Copy = DAG.getCopyToReg(Copy, PPC::R4, Op.getOperand(1),Copy.getValue(1));
721 // If we haven't noted the R3+R4 are live out, do so now.
722 if (DAG.getMachineFunction().liveout_empty()) {
723 DAG.getMachineFunction().addLiveOut(PPC::R3);
724 DAG.getMachineFunction().addLiveOut(PPC::R4);
725 }
726 break;
727 }
728 return DAG.getNode(PPCISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
729}
730
731/// LowerSELECT_CC - Lower floating point select_cc's into fsel instruction when
732/// possible.
733static SDOperand LowerSELECT_CC(SDOperand Op, SelectionDAG &DAG) {
734 // Not FP? Not a fsel.
735 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
736 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
737 return SDOperand();
738
739 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
740
741 // Cannot handle SETEQ/SETNE.
742 if (CC == ISD::SETEQ || CC == ISD::SETNE) return SDOperand();
743
744 MVT::ValueType ResVT = Op.getValueType();
745 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
746 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
747 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
748
749 // If the RHS of the comparison is a 0.0, we don't need to do the
750 // subtraction at all.
751 if (isFloatingPointZero(RHS))
752 switch (CC) {
753 default: break; // SETUO etc aren't handled by fsel.
754 case ISD::SETULT:
755 case ISD::SETLT:
756 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
757 case ISD::SETUGE:
758 case ISD::SETGE:
759 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
760 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
761 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
762 case ISD::SETUGT:
763 case ISD::SETGT:
764 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
765 case ISD::SETULE:
766 case ISD::SETLE:
767 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
768 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
769 return DAG.getNode(PPCISD::FSEL, ResVT,
770 DAG.getNode(ISD::FNEG, MVT::f64, LHS), TV, FV);
771 }
772
773 SDOperand Cmp;
774 switch (CC) {
775 default: break; // SETUO etc aren't handled by fsel.
776 case ISD::SETULT:
777 case ISD::SETLT:
778 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
779 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
780 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
781 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
782 case ISD::SETUGE:
783 case ISD::SETGE:
784 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
785 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
786 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
787 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
788 case ISD::SETUGT:
789 case ISD::SETGT:
790 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
791 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
792 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
793 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
794 case ISD::SETULE:
795 case ISD::SETLE:
796 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
797 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
798 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
799 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
800 }
801 return SDOperand();
802}
803
804static SDOperand LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
805 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
806 SDOperand Src = Op.getOperand(0);
807 if (Src.getValueType() == MVT::f32)
808 Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src);
809
810 SDOperand Tmp;
811 switch (Op.getValueType()) {
812 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
813 case MVT::i32:
814 Tmp = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
815 break;
816 case MVT::i64:
817 Tmp = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
818 break;
819 }
820
821 // Convert the FP value to an int value through memory.
822 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Tmp);
823 if (Op.getValueType() == MVT::i32)
824 Bits = DAG.getNode(ISD::TRUNCATE, MVT::i32, Bits);
825 return Bits;
826}
827
828static SDOperand LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
829 if (Op.getOperand(0).getValueType() == MVT::i64) {
830 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, Op.getOperand(0));
831 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Bits);
832 if (Op.getValueType() == MVT::f32)
833 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
834 return FP;
835 }
836
837 assert(Op.getOperand(0).getValueType() == MVT::i32 &&
838 "Unhandled SINT_TO_FP type in custom expander!");
839 // Since we only generate this in 64-bit mode, we can take advantage of
840 // 64-bit registers. In particular, sign extend the input value into the
841 // 64-bit register with extsw, store the WHOLE 64-bit value into the stack
842 // then lfd it and fcfid it.
843 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
844 int FrameIdx = FrameInfo->CreateStackObject(8, 8);
845 SDOperand FIdx = DAG.getFrameIndex(FrameIdx, MVT::i32);
846
847 SDOperand Ext64 = DAG.getNode(PPCISD::EXTSW_32, MVT::i32,
848 Op.getOperand(0));
849
850 // STD the extended value into the stack slot.
851 SDOperand Store = DAG.getNode(PPCISD::STD_32, MVT::Other,
852 DAG.getEntryNode(), Ext64, FIdx,
853 DAG.getSrcValue(NULL));
854 // Load the value as a double.
855 SDOperand Ld = DAG.getLoad(MVT::f64, Store, FIdx, DAG.getSrcValue(NULL));
856
857 // FCFID it and return it.
858 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Ld);
859 if (Op.getValueType() == MVT::f32)
860 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
861 return FP;
862}
863
864static SDOperand LowerSHL(SDOperand Op, SelectionDAG &DAG) {
865 assert(Op.getValueType() == MVT::i64 &&
866 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
867 // The generic code does a fine job expanding shift by a constant.
868 if (isa<ConstantSDNode>(Op.getOperand(1))) return SDOperand();
869
870 // Otherwise, expand into a bunch of logical ops. Note that these ops
871 // depend on the PPC behavior for oversized shift amounts.
872 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
873 DAG.getConstant(0, MVT::i32));
874 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
875 DAG.getConstant(1, MVT::i32));
876 SDOperand Amt = Op.getOperand(1);
877
878 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
879 DAG.getConstant(32, MVT::i32), Amt);
880 SDOperand Tmp2 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Amt);
881 SDOperand Tmp3 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Tmp1);
882 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
883 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
884 DAG.getConstant(-32U, MVT::i32));
885 SDOperand Tmp6 = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Tmp5);
886 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
887 SDOperand OutLo = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Amt);
888 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
889}
890
891static SDOperand LowerSRL(SDOperand Op, SelectionDAG &DAG) {
892 assert(Op.getValueType() == MVT::i64 &&
893 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
894 // The generic code does a fine job expanding shift by a constant.
895 if (isa<ConstantSDNode>(Op.getOperand(1))) return SDOperand();
896
897 // Otherwise, expand into a bunch of logical ops. Note that these ops
898 // depend on the PPC behavior for oversized shift amounts.
899 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
900 DAG.getConstant(0, MVT::i32));
901 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
902 DAG.getConstant(1, MVT::i32));
903 SDOperand Amt = Op.getOperand(1);
904
905 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
906 DAG.getConstant(32, MVT::i32), Amt);
907 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
908 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
909 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
910 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
911 DAG.getConstant(-32U, MVT::i32));
912 SDOperand Tmp6 = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Tmp5);
913 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
914 SDOperand OutHi = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Amt);
915 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
916}
917
918static SDOperand LowerSRA(SDOperand Op, SelectionDAG &DAG) {
919 assert(Op.getValueType() == MVT::i64 &&
920 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
921 // The generic code does a fine job expanding shift by a constant.
922 if (isa<ConstantSDNode>(Op.getOperand(1))) return SDOperand();
923
924 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
925 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
926 DAG.getConstant(0, MVT::i32));
927 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
928 DAG.getConstant(1, MVT::i32));
929 SDOperand Amt = Op.getOperand(1);
930
931 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
932 DAG.getConstant(32, MVT::i32), Amt);
933 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
934 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
935 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
936 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
937 DAG.getConstant(-32U, MVT::i32));
938 SDOperand Tmp6 = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Tmp5);
939 SDOperand OutHi = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Amt);
940 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
941 Tmp4, Tmp6, ISD::SETLE);
942 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
943}
944
945//===----------------------------------------------------------------------===//
946// Vector related lowering.
947//
948
Chris Lattnerac225ca2006-04-12 19:07:14 +0000949// If this is a vector of constants or undefs, get the bits. A bit in
950// UndefBits is set if the corresponding element of the vector is an
951// ISD::UNDEF value. For undefs, the corresponding VectorBits values are
952// zero. Return true if this is not an array of constants, false if it is.
953//
Chris Lattnerac225ca2006-04-12 19:07:14 +0000954static bool GetConstantBuildVectorBits(SDNode *BV, uint64_t VectorBits[2],
955 uint64_t UndefBits[2]) {
956 // Start with zero'd results.
957 VectorBits[0] = VectorBits[1] = UndefBits[0] = UndefBits[1] = 0;
958
959 unsigned EltBitSize = MVT::getSizeInBits(BV->getOperand(0).getValueType());
960 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
961 SDOperand OpVal = BV->getOperand(i);
962
963 unsigned PartNo = i >= e/2; // In the upper 128 bits?
Chris Lattnerb17f1672006-04-16 01:01:29 +0000964 unsigned SlotNo = e/2 - (i & (e/2-1))-1; // Which subpiece of the uint64_t.
Chris Lattnerac225ca2006-04-12 19:07:14 +0000965
966 uint64_t EltBits = 0;
967 if (OpVal.getOpcode() == ISD::UNDEF) {
968 uint64_t EltUndefBits = ~0U >> (32-EltBitSize);
969 UndefBits[PartNo] |= EltUndefBits << (SlotNo*EltBitSize);
970 continue;
971 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
972 EltBits = CN->getValue() & (~0U >> (32-EltBitSize));
973 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) {
974 assert(CN->getValueType(0) == MVT::f32 &&
975 "Only one legal FP vector type!");
976 EltBits = FloatToBits(CN->getValue());
977 } else {
978 // Nonconstant element.
979 return true;
980 }
981
982 VectorBits[PartNo] |= EltBits << (SlotNo*EltBitSize);
983 }
984
985 //printf("%llx %llx %llx %llx\n",
986 // VectorBits[0], VectorBits[1], UndefBits[0], UndefBits[1]);
987 return false;
988}
Chris Lattneref819f82006-03-20 06:33:01 +0000989
Chris Lattnerb17f1672006-04-16 01:01:29 +0000990// If this is a splat (repetition) of a value across the whole vector, return
991// the smallest size that splats it. For example, "0x01010101010101..." is a
992// splat of 0x01, 0x0101, and 0x01010101. We return SplatBits = 0x01 and
993// SplatSize = 1 byte.
994static bool isConstantSplat(const uint64_t Bits128[2],
995 const uint64_t Undef128[2],
996 unsigned &SplatBits, unsigned &SplatUndef,
997 unsigned &SplatSize) {
998
999 // Don't let undefs prevent splats from matching. See if the top 64-bits are
1000 // the same as the lower 64-bits, ignoring undefs.
1001 if ((Bits128[0] & ~Undef128[1]) != (Bits128[1] & ~Undef128[0]))
1002 return false; // Can't be a splat if two pieces don't match.
1003
1004 uint64_t Bits64 = Bits128[0] | Bits128[1];
1005 uint64_t Undef64 = Undef128[0] & Undef128[1];
1006
1007 // Check that the top 32-bits are the same as the lower 32-bits, ignoring
1008 // undefs.
1009 if ((Bits64 & (~Undef64 >> 32)) != ((Bits64 >> 32) & ~Undef64))
1010 return false; // Can't be a splat if two pieces don't match.
1011
1012 uint32_t Bits32 = uint32_t(Bits64) | uint32_t(Bits64 >> 32);
1013 uint32_t Undef32 = uint32_t(Undef64) & uint32_t(Undef64 >> 32);
1014
1015 // If the top 16-bits are different than the lower 16-bits, ignoring
1016 // undefs, we have an i32 splat.
1017 if ((Bits32 & (~Undef32 >> 16)) != ((Bits32 >> 16) & ~Undef32)) {
1018 SplatBits = Bits32;
1019 SplatUndef = Undef32;
1020 SplatSize = 4;
1021 return true;
1022 }
1023
1024 uint16_t Bits16 = uint16_t(Bits32) | uint16_t(Bits32 >> 16);
1025 uint16_t Undef16 = uint16_t(Undef32) & uint16_t(Undef32 >> 16);
1026
1027 // If the top 8-bits are different than the lower 8-bits, ignoring
1028 // undefs, we have an i16 splat.
1029 if ((Bits16 & (uint16_t(~Undef16) >> 8)) != ((Bits16 >> 8) & ~Undef16)) {
1030 SplatBits = Bits16;
1031 SplatUndef = Undef16;
1032 SplatSize = 2;
1033 return true;
1034 }
1035
1036 // Otherwise, we have an 8-bit splat.
1037 SplatBits = uint8_t(Bits16) | uint8_t(Bits16 >> 8);
1038 SplatUndef = uint8_t(Undef16) & uint8_t(Undef16 >> 8);
1039 SplatSize = 1;
1040 return true;
1041}
1042
Chris Lattnerf1b47082006-04-14 05:19:18 +00001043// If this is a case we can't handle, return null and let the default
1044// expansion code take care of it. If we CAN select this case, and if it
1045// selects to a single instruction, return Op. Otherwise, if we can codegen
1046// this case more efficiently than a constant pool load, lower it to the
1047// sequence of ops that should be used.
1048static SDOperand LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
1049 // If this is a vector of constants or undefs, get the bits. A bit in
1050 // UndefBits is set if the corresponding element of the vector is an
1051 // ISD::UNDEF value. For undefs, the corresponding VectorBits values are
1052 // zero.
1053 uint64_t VectorBits[2];
1054 uint64_t UndefBits[2];
1055 if (GetConstantBuildVectorBits(Op.Val, VectorBits, UndefBits))
1056 return SDOperand(); // Not a constant vector.
1057
Chris Lattnerb17f1672006-04-16 01:01:29 +00001058 // If this is a splat (repetition) of a value across the whole vector, return
1059 // the smallest size that splats it. For example, "0x01010101010101..." is a
1060 // splat of 0x01, 0x0101, and 0x01010101. We return SplatBits = 0x01 and
1061 // SplatSize = 1 byte.
1062 unsigned SplatBits, SplatUndef, SplatSize;
1063 if (isConstantSplat(VectorBits, UndefBits, SplatBits, SplatUndef, SplatSize)){
1064 bool HasAnyUndefs = (UndefBits[0] | UndefBits[1]) != 0;
1065
1066 // First, handle single instruction cases.
1067
1068 // All zeros?
1069 if (SplatBits == 0) {
1070 // Canonicalize all zero vectors to be v4i32.
1071 if (Op.getValueType() != MVT::v4i32 || HasAnyUndefs) {
1072 SDOperand Z = DAG.getConstant(0, MVT::i32);
1073 Z = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Z, Z, Z, Z);
1074 Op = DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Z);
1075 }
1076 return Op;
Chris Lattnerf1b47082006-04-14 05:19:18 +00001077 }
Chris Lattnerb17f1672006-04-16 01:01:29 +00001078
1079 // If the sign extended value is in the range [-16,15], use VSPLTI[bhw].
1080 int32_t SextVal= int32_t(SplatBits << (32-8*SplatSize)) >> (32-8*SplatSize);
1081 if (SextVal >= -16 && SextVal <= 15) {
1082 const MVT::ValueType VTys[] = { // canonical VT to use for each size.
1083 MVT::v16i8, MVT::v8i16, MVT::Other, MVT::v4i32
1084 };
1085 MVT::ValueType CanonicalVT = VTys[SplatSize-1];
1086
1087 // If this is a non-canonical splat for this value,
1088 if (Op.getValueType() != CanonicalVT || HasAnyUndefs) {
1089 SDOperand Elt = DAG.getConstant(SplatBits,
1090 MVT::getVectorBaseType(CanonicalVT));
1091 std::vector<SDOperand> Ops(MVT::getVectorNumElements(CanonicalVT), Elt);
1092 SDOperand Res = DAG.getNode(ISD::BUILD_VECTOR, CanonicalVT, Ops);
1093 Op = DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Res);
1094 }
1095 return Op;
Chris Lattnerf1b47082006-04-14 05:19:18 +00001096 }
Chris Lattnerb17f1672006-04-16 01:01:29 +00001097
Chris Lattnerf1b47082006-04-14 05:19:18 +00001098
1099 // If this is 0x8000_0000 x 4, turn into vspltisw + vslw. If it is
1100 // 0x7FFF_FFFF x 4, turn it into not(0x8000_0000). These are important
1101 // for fneg/fabs.
Chris Lattnerb17f1672006-04-16 01:01:29 +00001102 if (SplatSize == 4 &&
1103 SplatBits == 0x80000000 || SplatBits == (0x7FFFFFFF&~SplatUndef)) {
Chris Lattnerf1b47082006-04-14 05:19:18 +00001104 // Make -1 and vspltisw -1:
1105 SDOperand OnesI = DAG.getConstant(~0U, MVT::i32);
1106 SDOperand OnesV = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32,
1107 OnesI, OnesI, OnesI, OnesI);
1108
1109 // Make the VSLW intrinsic, computing 0x8000_0000.
1110 SDOperand Res
1111 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, MVT::v4i32,
1112 DAG.getConstant(Intrinsic::ppc_altivec_vslw, MVT::i32),
1113 OnesV, OnesV);
1114
1115 // If this is 0x7FFF_FFFF, xor by OnesV to invert it.
Chris Lattnerb17f1672006-04-16 01:01:29 +00001116 if (SplatBits == 0x80000000)
Chris Lattnerf1b47082006-04-14 05:19:18 +00001117 Res = DAG.getNode(ISD::XOR, MVT::v4i32, Res, OnesV);
1118
1119 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Res);
1120 }
1121 }
Chris Lattnerb17f1672006-04-16 01:01:29 +00001122
Chris Lattnerf1b47082006-04-14 05:19:18 +00001123 return SDOperand();
1124}
1125
1126/// LowerVECTOR_SHUFFLE - Return the code we lower for VECTOR_SHUFFLE. If this
1127/// is a shuffle we can handle in a single instruction, return it. Otherwise,
1128/// return the code it can be lowered into. Worst case, it can always be
1129/// lowered into a vperm.
1130static SDOperand LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
1131 SDOperand V1 = Op.getOperand(0);
1132 SDOperand V2 = Op.getOperand(1);
1133 SDOperand PermMask = Op.getOperand(2);
1134
1135 // Cases that are handled by instructions that take permute immediates
1136 // (such as vsplt*) should be left as VECTOR_SHUFFLE nodes so they can be
1137 // selected by the instruction selector.
1138 if (V2.getOpcode() == ISD::UNDEF) {
1139 if (PPC::isSplatShuffleMask(PermMask.Val, 1) ||
1140 PPC::isSplatShuffleMask(PermMask.Val, 2) ||
1141 PPC::isSplatShuffleMask(PermMask.Val, 4) ||
1142 PPC::isVPKUWUMShuffleMask(PermMask.Val, true) ||
1143 PPC::isVPKUHUMShuffleMask(PermMask.Val, true) ||
1144 PPC::isVSLDOIShuffleMask(PermMask.Val, true) != -1 ||
1145 PPC::isVMRGLShuffleMask(PermMask.Val, 1, true) ||
1146 PPC::isVMRGLShuffleMask(PermMask.Val, 2, true) ||
1147 PPC::isVMRGLShuffleMask(PermMask.Val, 4, true) ||
1148 PPC::isVMRGHShuffleMask(PermMask.Val, 1, true) ||
1149 PPC::isVMRGHShuffleMask(PermMask.Val, 2, true) ||
1150 PPC::isVMRGHShuffleMask(PermMask.Val, 4, true)) {
1151 return Op;
1152 }
1153 }
1154
1155 // Altivec has a variety of "shuffle immediates" that take two vector inputs
1156 // and produce a fixed permutation. If any of these match, do not lower to
1157 // VPERM.
1158 if (PPC::isVPKUWUMShuffleMask(PermMask.Val, false) ||
1159 PPC::isVPKUHUMShuffleMask(PermMask.Val, false) ||
1160 PPC::isVSLDOIShuffleMask(PermMask.Val, false) != -1 ||
1161 PPC::isVMRGLShuffleMask(PermMask.Val, 1, false) ||
1162 PPC::isVMRGLShuffleMask(PermMask.Val, 2, false) ||
1163 PPC::isVMRGLShuffleMask(PermMask.Val, 4, false) ||
1164 PPC::isVMRGHShuffleMask(PermMask.Val, 1, false) ||
1165 PPC::isVMRGHShuffleMask(PermMask.Val, 2, false) ||
1166 PPC::isVMRGHShuffleMask(PermMask.Val, 4, false))
1167 return Op;
1168
1169 // TODO: Handle more cases, and also handle cases that are cheaper to do as
1170 // multiple such instructions than as a constant pool load/vperm pair.
1171
1172 // Lower this to a VPERM(V1, V2, V3) expression, where V3 is a constant
1173 // vector that will get spilled to the constant pool.
1174 if (V2.getOpcode() == ISD::UNDEF) V2 = V1;
1175
1176 // The SHUFFLE_VECTOR mask is almost exactly what we want for vperm, except
1177 // that it is in input element units, not in bytes. Convert now.
1178 MVT::ValueType EltVT = MVT::getVectorBaseType(V1.getValueType());
1179 unsigned BytesPerElement = MVT::getSizeInBits(EltVT)/8;
1180
1181 std::vector<SDOperand> ResultMask;
1182 for (unsigned i = 0, e = PermMask.getNumOperands(); i != e; ++i) {
Chris Lattner730b4562006-04-15 23:48:05 +00001183 unsigned SrcElt;
1184 if (PermMask.getOperand(i).getOpcode() == ISD::UNDEF)
1185 SrcElt = 0;
1186 else
1187 SrcElt = cast<ConstantSDNode>(PermMask.getOperand(i))->getValue();
Chris Lattnerf1b47082006-04-14 05:19:18 +00001188
1189 for (unsigned j = 0; j != BytesPerElement; ++j)
1190 ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement+j,
1191 MVT::i8));
1192 }
1193
1194 SDOperand VPermMask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v16i8, ResultMask);
1195 return DAG.getNode(PPCISD::VPERM, V1.getValueType(), V1, V2, VPermMask);
1196}
1197
Chris Lattner1a635d62006-04-14 06:01:58 +00001198/// LowerINTRINSIC_WO_CHAIN - If this is an intrinsic that we want to custom
1199/// lower, do it, otherwise return null.
1200static SDOperand LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
1201 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
1202
1203 // If this is a lowered altivec predicate compare, CompareOpc is set to the
1204 // opcode number of the comparison.
1205 int CompareOpc = -1;
1206 bool isDot = false;
1207 switch (IntNo) {
1208 default: return SDOperand(); // Don't custom lower most intrinsics.
1209 // Comparison predicates.
1210 case Intrinsic::ppc_altivec_vcmpbfp_p: CompareOpc = 966; isDot = 1; break;
1211 case Intrinsic::ppc_altivec_vcmpeqfp_p: CompareOpc = 198; isDot = 1; break;
1212 case Intrinsic::ppc_altivec_vcmpequb_p: CompareOpc = 6; isDot = 1; break;
1213 case Intrinsic::ppc_altivec_vcmpequh_p: CompareOpc = 70; isDot = 1; break;
1214 case Intrinsic::ppc_altivec_vcmpequw_p: CompareOpc = 134; isDot = 1; break;
1215 case Intrinsic::ppc_altivec_vcmpgefp_p: CompareOpc = 454; isDot = 1; break;
1216 case Intrinsic::ppc_altivec_vcmpgtfp_p: CompareOpc = 710; isDot = 1; break;
1217 case Intrinsic::ppc_altivec_vcmpgtsb_p: CompareOpc = 774; isDot = 1; break;
1218 case Intrinsic::ppc_altivec_vcmpgtsh_p: CompareOpc = 838; isDot = 1; break;
1219 case Intrinsic::ppc_altivec_vcmpgtsw_p: CompareOpc = 902; isDot = 1; break;
1220 case Intrinsic::ppc_altivec_vcmpgtub_p: CompareOpc = 518; isDot = 1; break;
1221 case Intrinsic::ppc_altivec_vcmpgtuh_p: CompareOpc = 582; isDot = 1; break;
1222 case Intrinsic::ppc_altivec_vcmpgtuw_p: CompareOpc = 646; isDot = 1; break;
1223
1224 // Normal Comparisons.
1225 case Intrinsic::ppc_altivec_vcmpbfp: CompareOpc = 966; isDot = 0; break;
1226 case Intrinsic::ppc_altivec_vcmpeqfp: CompareOpc = 198; isDot = 0; break;
1227 case Intrinsic::ppc_altivec_vcmpequb: CompareOpc = 6; isDot = 0; break;
1228 case Intrinsic::ppc_altivec_vcmpequh: CompareOpc = 70; isDot = 0; break;
1229 case Intrinsic::ppc_altivec_vcmpequw: CompareOpc = 134; isDot = 0; break;
1230 case Intrinsic::ppc_altivec_vcmpgefp: CompareOpc = 454; isDot = 0; break;
1231 case Intrinsic::ppc_altivec_vcmpgtfp: CompareOpc = 710; isDot = 0; break;
1232 case Intrinsic::ppc_altivec_vcmpgtsb: CompareOpc = 774; isDot = 0; break;
1233 case Intrinsic::ppc_altivec_vcmpgtsh: CompareOpc = 838; isDot = 0; break;
1234 case Intrinsic::ppc_altivec_vcmpgtsw: CompareOpc = 902; isDot = 0; break;
1235 case Intrinsic::ppc_altivec_vcmpgtub: CompareOpc = 518; isDot = 0; break;
1236 case Intrinsic::ppc_altivec_vcmpgtuh: CompareOpc = 582; isDot = 0; break;
1237 case Intrinsic::ppc_altivec_vcmpgtuw: CompareOpc = 646; isDot = 0; break;
1238 }
1239
1240 assert(CompareOpc>0 && "We only lower altivec predicate compares so far!");
1241
1242 // If this is a non-dot comparison, make the VCMP node.
1243 if (!isDot) {
1244 SDOperand Tmp = DAG.getNode(PPCISD::VCMP, Op.getOperand(2).getValueType(),
1245 Op.getOperand(1), Op.getOperand(2),
1246 DAG.getConstant(CompareOpc, MVT::i32));
1247 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Tmp);
1248 }
1249
1250 // Create the PPCISD altivec 'dot' comparison node.
1251 std::vector<SDOperand> Ops;
1252 std::vector<MVT::ValueType> VTs;
1253 Ops.push_back(Op.getOperand(2)); // LHS
1254 Ops.push_back(Op.getOperand(3)); // RHS
1255 Ops.push_back(DAG.getConstant(CompareOpc, MVT::i32));
1256 VTs.push_back(Op.getOperand(2).getValueType());
1257 VTs.push_back(MVT::Flag);
1258 SDOperand CompNode = DAG.getNode(PPCISD::VCMPo, VTs, Ops);
1259
1260 // Now that we have the comparison, emit a copy from the CR to a GPR.
1261 // This is flagged to the above dot comparison.
1262 SDOperand Flags = DAG.getNode(PPCISD::MFCR, MVT::i32,
1263 DAG.getRegister(PPC::CR6, MVT::i32),
1264 CompNode.getValue(1));
1265
1266 // Unpack the result based on how the target uses it.
1267 unsigned BitNo; // Bit # of CR6.
1268 bool InvertBit; // Invert result?
1269 switch (cast<ConstantSDNode>(Op.getOperand(1))->getValue()) {
1270 default: // Can't happen, don't crash on invalid number though.
1271 case 0: // Return the value of the EQ bit of CR6.
1272 BitNo = 0; InvertBit = false;
1273 break;
1274 case 1: // Return the inverted value of the EQ bit of CR6.
1275 BitNo = 0; InvertBit = true;
1276 break;
1277 case 2: // Return the value of the LT bit of CR6.
1278 BitNo = 2; InvertBit = false;
1279 break;
1280 case 3: // Return the inverted value of the LT bit of CR6.
1281 BitNo = 2; InvertBit = true;
1282 break;
1283 }
1284
1285 // Shift the bit into the low position.
1286 Flags = DAG.getNode(ISD::SRL, MVT::i32, Flags,
1287 DAG.getConstant(8-(3-BitNo), MVT::i32));
1288 // Isolate the bit.
1289 Flags = DAG.getNode(ISD::AND, MVT::i32, Flags,
1290 DAG.getConstant(1, MVT::i32));
1291
1292 // If we are supposed to, toggle the bit.
1293 if (InvertBit)
1294 Flags = DAG.getNode(ISD::XOR, MVT::i32, Flags,
1295 DAG.getConstant(1, MVT::i32));
1296 return Flags;
1297}
1298
1299static SDOperand LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
1300 // Create a stack slot that is 16-byte aligned.
1301 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
1302 int FrameIdx = FrameInfo->CreateStackObject(16, 16);
1303 SDOperand FIdx = DAG.getFrameIndex(FrameIdx, MVT::i32);
1304
1305 // Store the input value into Value#0 of the stack slot.
1306 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
1307 Op.getOperand(0), FIdx,DAG.getSrcValue(NULL));
1308 // Load it out.
1309 return DAG.getLoad(Op.getValueType(), Store, FIdx, DAG.getSrcValue(NULL));
1310}
1311
Chris Lattnere4bc9ea2005-08-26 00:52:45 +00001312/// LowerOperation - Provide custom lowering hooks for some operations.
1313///
Nate Begeman21e463b2005-10-16 05:39:50 +00001314SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +00001315 switch (Op.getOpcode()) {
1316 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattner1a635d62006-04-14 06:01:58 +00001317 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
1318 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
1319 case ISD::SETCC: return LowerSETCC(Op, DAG);
1320 case ISD::VASTART: return LowerVASTART(Op, DAG, VarArgsFrameIndex);
1321 case ISD::RET: return LowerRET(Op, DAG);
Chris Lattner7c0d6642005-10-02 06:37:13 +00001322
Chris Lattner1a635d62006-04-14 06:01:58 +00001323 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
1324 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
1325 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00001326
Chris Lattner1a635d62006-04-14 06:01:58 +00001327 // Lower 64-bit shifts.
1328 case ISD::SHL: return LowerSHL(Op, DAG);
1329 case ISD::SRL: return LowerSRL(Op, DAG);
1330 case ISD::SRA: return LowerSRA(Op, DAG);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00001331
Chris Lattner1a635d62006-04-14 06:01:58 +00001332 // Vector-related lowering.
1333 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
1334 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
1335 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
1336 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
Chris Lattnerbc11c342005-08-31 20:23:54 +00001337 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +00001338 return SDOperand();
1339}
1340
Chris Lattner1a635d62006-04-14 06:01:58 +00001341//===----------------------------------------------------------------------===//
1342// Other Lowering Code
1343//===----------------------------------------------------------------------===//
1344
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001345std::vector<SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +00001346PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001347 //
1348 // add beautiful description of PPC stack frame format, or at least some docs
1349 //
1350 MachineFunction &MF = DAG.getMachineFunction();
1351 MachineFrameInfo *MFI = MF.getFrameInfo();
1352 MachineBasicBlock& BB = MF.front();
Chris Lattner7b738342005-09-13 19:33:40 +00001353 SSARegMap *RegMap = MF.getSSARegMap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001354 std::vector<SDOperand> ArgValues;
1355
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001356 unsigned ArgOffset = 24;
1357 unsigned GPR_remaining = 8;
1358 unsigned FPR_remaining = 13;
1359 unsigned GPR_idx = 0, FPR_idx = 0;
1360 static const unsigned GPR[] = {
1361 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1362 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1363 };
1364 static const unsigned FPR[] = {
1365 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1366 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1367 };
1368
1369 // Add DAG nodes to load the arguments... On entry to a function on PPC,
1370 // the arguments start at offset 24, although they are likely to be passed
1371 // in registers.
1372 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
1373 SDOperand newroot, argt;
1374 unsigned ObjSize;
1375 bool needsLoad = false;
1376 bool ArgLive = !I->use_empty();
1377 MVT::ValueType ObjectVT = getValueType(I->getType());
1378
1379 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +00001380 default: assert(0 && "Unhandled argument type!");
1381 case MVT::i1:
1382 case MVT::i8:
1383 case MVT::i16:
1384 case MVT::i32:
1385 ObjSize = 4;
1386 if (!ArgLive) break;
1387 if (GPR_remaining > 0) {
Nate Begeman1d9d7422005-10-18 00:28:58 +00001388 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +00001389 MF.addLiveIn(GPR[GPR_idx], VReg);
1390 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +00001391 if (ObjectVT != MVT::i32) {
1392 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
1393 : ISD::AssertZext;
1394 argt = DAG.getNode(AssertOp, MVT::i32, argt,
1395 DAG.getValueType(ObjectVT));
1396 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
1397 }
Chris Lattner915fb302005-08-30 00:19:00 +00001398 } else {
1399 needsLoad = true;
1400 }
1401 break;
Chris Lattner80720a92005-11-30 20:40:54 +00001402 case MVT::i64:
1403 ObjSize = 8;
Chris Lattner915fb302005-08-30 00:19:00 +00001404 if (!ArgLive) break;
1405 if (GPR_remaining > 0) {
1406 SDOperand argHi, argLo;
Nate Begeman1d9d7422005-10-18 00:28:58 +00001407 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +00001408 MF.addLiveIn(GPR[GPR_idx], VReg);
1409 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +00001410 // If we have two or more remaining argument registers, then both halves
1411 // of the i64 can be sourced from there. Otherwise, the lower half will
1412 // have to come off the stack. This can happen when an i64 is preceded
1413 // by 28 bytes of arguments.
1414 if (GPR_remaining > 1) {
Nate Begeman1d9d7422005-10-18 00:28:58 +00001415 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +00001416 MF.addLiveIn(GPR[GPR_idx+1], VReg);
1417 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +00001418 } else {
1419 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
1420 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
1421 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
1422 DAG.getSrcValue(NULL));
1423 }
1424 // Build the outgoing arg thingy
1425 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
1426 newroot = argLo;
1427 } else {
1428 needsLoad = true;
1429 }
1430 break;
1431 case MVT::f32:
1432 case MVT::f64:
1433 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
Chris Lattner413b9792006-01-11 18:21:25 +00001434 if (!ArgLive) {
1435 if (FPR_remaining > 0) {
1436 --FPR_remaining;
1437 ++FPR_idx;
1438 }
1439 break;
1440 }
Chris Lattner915fb302005-08-30 00:19:00 +00001441 if (FPR_remaining > 0) {
Chris Lattner919c0322005-10-01 01:35:02 +00001442 unsigned VReg;
1443 if (ObjectVT == MVT::f32)
Nate Begeman1d9d7422005-10-18 00:28:58 +00001444 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner919c0322005-10-01 01:35:02 +00001445 else
Nate Begeman1d9d7422005-10-18 00:28:58 +00001446 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +00001447 MF.addLiveIn(FPR[FPR_idx], VReg);
1448 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
Chris Lattner915fb302005-08-30 00:19:00 +00001449 --FPR_remaining;
1450 ++FPR_idx;
1451 } else {
1452 needsLoad = true;
1453 }
1454 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001455 }
1456
1457 // We need to load the argument to a virtual register if we determined above
1458 // that we ran out of physical registers of the appropriate type
1459 if (needsLoad) {
1460 unsigned SubregOffset = 0;
1461 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
1462 if (ObjectVT == MVT::i16) SubregOffset = 2;
1463 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
1464 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
1465 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
1466 DAG.getConstant(SubregOffset, MVT::i32));
1467 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
1468 DAG.getSrcValue(NULL));
1469 }
1470
1471 // Every 4 bytes of argument space consumes one of the GPRs available for
1472 // argument passing.
1473 if (GPR_remaining > 0) {
1474 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
1475 GPR_remaining -= delta;
1476 GPR_idx += delta;
1477 }
1478 ArgOffset += ObjSize;
1479 if (newroot.Val)
1480 DAG.setRoot(newroot.getValue(1));
1481
1482 ArgValues.push_back(argt);
1483 }
1484
1485 // If the function takes variable number of arguments, make a frame index for
1486 // the start of the first vararg value... for expansion of llvm.va_start.
1487 if (F.isVarArg()) {
1488 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
1489 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
1490 // If this function is vararg, store any remaining integer argument regs
1491 // to their spots on the stack so that they may be loaded by deferencing the
1492 // result of va_next.
1493 std::vector<SDOperand> MemOps;
1494 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begeman1d9d7422005-10-18 00:28:58 +00001495 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +00001496 MF.addLiveIn(GPR[GPR_idx], VReg);
1497 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001498 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
1499 Val, FIN, DAG.getSrcValue(NULL));
1500 MemOps.push_back(Store);
1501 // Increment the address by four for the next argument to store
1502 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
1503 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
1504 }
Chris Lattner80720a92005-11-30 20:40:54 +00001505 if (!MemOps.empty()) {
1506 MemOps.push_back(DAG.getRoot());
1507 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
1508 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001509 }
1510
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001511 return ArgValues;
1512}
1513
1514std::pair<SDOperand, SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +00001515PPCTargetLowering::LowerCallTo(SDOperand Chain,
1516 const Type *RetTy, bool isVarArg,
1517 unsigned CallingConv, bool isTailCall,
1518 SDOperand Callee, ArgListTy &Args,
1519 SelectionDAG &DAG) {
Chris Lattner281b55e2006-01-27 23:34:02 +00001520 // args_to_use will accumulate outgoing args for the PPCISD::CALL case in
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001521 // SelectExpr to use to put the arguments in the appropriate registers.
1522 std::vector<SDOperand> args_to_use;
1523
1524 // Count how many bytes are to be pushed on the stack, including the linkage
1525 // area, and parameter passing area.
1526 unsigned NumBytes = 24;
1527
1528 if (Args.empty()) {
Chris Lattner45b39762006-02-13 08:55:29 +00001529 Chain = DAG.getCALLSEQ_START(Chain,
1530 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001531 } else {
Chris Lattner915fb302005-08-30 00:19:00 +00001532 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001533 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +00001534 default: assert(0 && "Unknown value type!");
1535 case MVT::i1:
1536 case MVT::i8:
1537 case MVT::i16:
1538 case MVT::i32:
1539 case MVT::f32:
1540 NumBytes += 4;
1541 break;
1542 case MVT::i64:
1543 case MVT::f64:
1544 NumBytes += 8;
1545 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001546 }
Chris Lattner915fb302005-08-30 00:19:00 +00001547 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001548
Chris Lattner915fb302005-08-30 00:19:00 +00001549 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
1550 // plus 32 bytes of argument space in case any called code gets funky on us.
1551 // (Required by ABI to support var arg)
1552 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001553
1554 // Adjust the stack pointer for the new arguments...
1555 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattner45b39762006-02-13 08:55:29 +00001556 Chain = DAG.getCALLSEQ_START(Chain,
1557 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001558
1559 // Set up a copy of the stack pointer for use loading and storing any
1560 // arguments that may not fit in the registers available for argument
1561 // passing.
Chris Lattnera243db82006-01-11 19:55:07 +00001562 SDOperand StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001563
1564 // Figure out which arguments are going to go in registers, and which in
1565 // memory. Also, if this is a vararg function, floating point operations
1566 // must be stored to our stack, and loaded into integer regs as well, if
1567 // any integer regs are available for argument passing.
1568 unsigned ArgOffset = 24;
1569 unsigned GPR_remaining = 8;
1570 unsigned FPR_remaining = 13;
1571
1572 std::vector<SDOperand> MemOps;
1573 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
1574 // PtrOff will be used to store the current argument to the stack if a
1575 // register cannot be found for it.
1576 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1577 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
1578 MVT::ValueType ArgVT = getValueType(Args[i].second);
1579
1580 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +00001581 default: assert(0 && "Unexpected ValueType for argument!");
1582 case MVT::i1:
1583 case MVT::i8:
1584 case MVT::i16:
1585 // Promote the integer to 32 bits. If the input type is signed use a
1586 // sign extend, otherwise use a zero extend.
1587 if (Args[i].second->isSigned())
1588 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
1589 else
1590 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
1591 // FALL THROUGH
1592 case MVT::i32:
1593 if (GPR_remaining > 0) {
1594 args_to_use.push_back(Args[i].first);
1595 --GPR_remaining;
1596 } else {
1597 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1598 Args[i].first, PtrOff,
1599 DAG.getSrcValue(NULL)));
1600 }
1601 ArgOffset += 4;
1602 break;
1603 case MVT::i64:
1604 // If we have one free GPR left, we can place the upper half of the i64
1605 // in it, and store the other half to the stack. If we have two or more
1606 // free GPRs, then we can pass both halves of the i64 in registers.
1607 if (GPR_remaining > 0) {
1608 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
1609 Args[i].first, DAG.getConstant(1, MVT::i32));
1610 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
1611 Args[i].first, DAG.getConstant(0, MVT::i32));
1612 args_to_use.push_back(Hi);
1613 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001614 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +00001615 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001616 --GPR_remaining;
1617 } else {
Chris Lattner915fb302005-08-30 00:19:00 +00001618 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
1619 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001620 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +00001621 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001622 }
Chris Lattner915fb302005-08-30 00:19:00 +00001623 } else {
1624 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1625 Args[i].first, PtrOff,
1626 DAG.getSrcValue(NULL)));
1627 }
1628 ArgOffset += 8;
1629 break;
1630 case MVT::f32:
1631 case MVT::f64:
1632 if (FPR_remaining > 0) {
1633 args_to_use.push_back(Args[i].first);
1634 --FPR_remaining;
1635 if (isVarArg) {
1636 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
1637 Args[i].first, PtrOff,
1638 DAG.getSrcValue(NULL));
1639 MemOps.push_back(Store);
1640 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001641 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +00001642 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
1643 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +00001644 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +00001645 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001646 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +00001647 }
1648 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001649 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
1650 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +00001651 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
1652 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +00001653 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +00001654 args_to_use.push_back(Load);
1655 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001656 }
1657 } else {
Chris Lattner915fb302005-08-30 00:19:00 +00001658 // If we have any FPRs remaining, we may also have GPRs remaining.
1659 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
1660 // GPRs.
1661 if (GPR_remaining > 0) {
1662 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
1663 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001664 }
Chris Lattner915fb302005-08-30 00:19:00 +00001665 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
1666 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
1667 --GPR_remaining;
1668 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001669 }
Chris Lattner915fb302005-08-30 00:19:00 +00001670 } else {
1671 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1672 Args[i].first, PtrOff,
1673 DAG.getSrcValue(NULL)));
1674 }
1675 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
1676 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001677 }
1678 }
1679 if (!MemOps.empty())
1680 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
1681 }
1682
1683 std::vector<MVT::ValueType> RetVals;
1684 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +00001685 MVT::ValueType ActualRetTyVT = RetTyVT;
1686 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
1687 ActualRetTyVT = MVT::i32; // Promote result to i32.
1688
Chris Lattnere00ebf02006-01-28 07:33:03 +00001689 if (RetTyVT == MVT::i64) {
1690 RetVals.push_back(MVT::i32);
1691 RetVals.push_back(MVT::i32);
1692 } else if (RetTyVT != MVT::isVoid) {
Chris Lattnerf5059492005-09-02 01:24:55 +00001693 RetVals.push_back(ActualRetTyVT);
Chris Lattnere00ebf02006-01-28 07:33:03 +00001694 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001695 RetVals.push_back(MVT::Other);
1696
Chris Lattner2823b3e2005-11-17 05:56:14 +00001697 // If the callee is a GlobalAddress node (quite common, every direct call is)
1698 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1699 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1700 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
1701
Chris Lattner281b55e2006-01-27 23:34:02 +00001702 std::vector<SDOperand> Ops;
1703 Ops.push_back(Chain);
1704 Ops.push_back(Callee);
1705 Ops.insert(Ops.end(), args_to_use.begin(), args_to_use.end());
1706 SDOperand TheCall = DAG.getNode(PPCISD::CALL, RetVals, Ops);
Chris Lattnere00ebf02006-01-28 07:33:03 +00001707 Chain = TheCall.getValue(TheCall.Val->getNumValues()-1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001708 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
1709 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +00001710 SDOperand RetVal = TheCall;
1711
1712 // If the result is a small value, add a note so that we keep track of the
1713 // information about whether it is sign or zero extended.
1714 if (RetTyVT != ActualRetTyVT) {
1715 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
1716 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
1717 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
Chris Lattnere00ebf02006-01-28 07:33:03 +00001718 } else if (RetTyVT == MVT::i64) {
1719 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, RetVal, RetVal.getValue(1));
Chris Lattnerf5059492005-09-02 01:24:55 +00001720 }
1721
1722 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001723}
1724
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001725MachineBasicBlock *
Nate Begeman21e463b2005-10-16 05:39:50 +00001726PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1727 MachineBasicBlock *BB) {
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001728 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
Chris Lattner919c0322005-10-01 01:35:02 +00001729 MI->getOpcode() == PPC::SELECT_CC_F4 ||
Chris Lattner710ff322006-04-08 22:45:08 +00001730 MI->getOpcode() == PPC::SELECT_CC_F8 ||
1731 MI->getOpcode() == PPC::SELECT_CC_VRRC) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001732 "Unexpected instr type to insert");
1733
1734 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
1735 // control-flow pattern. The incoming instruction knows the destination vreg
1736 // to set, the condition code register to branch on, the true/false values to
1737 // select between, and a branch opcode to use.
1738 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1739 ilist<MachineBasicBlock>::iterator It = BB;
1740 ++It;
1741
1742 // thisMBB:
1743 // ...
1744 // TrueVal = ...
1745 // cmpTY ccX, r1, r2
1746 // bCC copy1MBB
1747 // fallthrough --> copy0MBB
1748 MachineBasicBlock *thisMBB = BB;
1749 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1750 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1751 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
1752 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
1753 MachineFunction *F = BB->getParent();
1754 F->getBasicBlockList().insert(It, copy0MBB);
1755 F->getBasicBlockList().insert(It, sinkMBB);
Nate Begemanf15485a2006-03-27 01:32:24 +00001756 // Update machine-CFG edges by first adding all successors of the current
1757 // block to the new block which will contain the Phi node for the select.
1758 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
1759 e = BB->succ_end(); i != e; ++i)
1760 sinkMBB->addSuccessor(*i);
1761 // Next, remove all successors of the current block, and add the true
1762 // and fallthrough blocks as its successors.
1763 while(!BB->succ_empty())
1764 BB->removeSuccessor(BB->succ_begin());
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001765 BB->addSuccessor(copy0MBB);
1766 BB->addSuccessor(sinkMBB);
1767
1768 // copy0MBB:
1769 // %FalseValue = ...
1770 // # fallthrough to sinkMBB
1771 BB = copy0MBB;
1772
1773 // Update machine-CFG edges
1774 BB->addSuccessor(sinkMBB);
1775
1776 // sinkMBB:
1777 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1778 // ...
1779 BB = sinkMBB;
1780 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
1781 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
1782 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
1783
1784 delete MI; // The pseudo instruction is gone now.
1785 return BB;
1786}
1787
Chris Lattner1a635d62006-04-14 06:01:58 +00001788//===----------------------------------------------------------------------===//
1789// Target Optimization Hooks
1790//===----------------------------------------------------------------------===//
1791
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001792SDOperand PPCTargetLowering::PerformDAGCombine(SDNode *N,
1793 DAGCombinerInfo &DCI) const {
1794 TargetMachine &TM = getTargetMachine();
1795 SelectionDAG &DAG = DCI.DAG;
1796 switch (N->getOpcode()) {
1797 default: break;
1798 case ISD::SINT_TO_FP:
1799 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
Chris Lattnerecfe55e2006-03-22 05:30:33 +00001800 if (N->getOperand(0).getOpcode() == ISD::FP_TO_SINT) {
1801 // Turn (sint_to_fp (fp_to_sint X)) -> fctidz/fcfid without load/stores.
1802 // We allow the src/dst to be either f32/f64, but the intermediate
1803 // type must be i64.
1804 if (N->getOperand(0).getValueType() == MVT::i64) {
1805 SDOperand Val = N->getOperand(0).getOperand(0);
1806 if (Val.getValueType() == MVT::f32) {
1807 Val = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Val);
1808 DCI.AddToWorklist(Val.Val);
1809 }
1810
1811 Val = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Val);
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001812 DCI.AddToWorklist(Val.Val);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00001813 Val = DAG.getNode(PPCISD::FCFID, MVT::f64, Val);
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001814 DCI.AddToWorklist(Val.Val);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00001815 if (N->getValueType(0) == MVT::f32) {
1816 Val = DAG.getNode(ISD::FP_ROUND, MVT::f32, Val);
1817 DCI.AddToWorklist(Val.Val);
1818 }
1819 return Val;
1820 } else if (N->getOperand(0).getValueType() == MVT::i32) {
1821 // If the intermediate type is i32, we can avoid the load/store here
1822 // too.
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001823 }
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001824 }
1825 }
1826 break;
Chris Lattner51269842006-03-01 05:50:56 +00001827 case ISD::STORE:
1828 // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)).
1829 if (TM.getSubtarget<PPCSubtarget>().hasSTFIWX() &&
1830 N->getOperand(1).getOpcode() == ISD::FP_TO_SINT &&
1831 N->getOperand(1).getValueType() == MVT::i32) {
1832 SDOperand Val = N->getOperand(1).getOperand(0);
1833 if (Val.getValueType() == MVT::f32) {
1834 Val = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Val);
1835 DCI.AddToWorklist(Val.Val);
1836 }
1837 Val = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Val);
1838 DCI.AddToWorklist(Val.Val);
1839
1840 Val = DAG.getNode(PPCISD::STFIWX, MVT::Other, N->getOperand(0), Val,
1841 N->getOperand(2), N->getOperand(3));
1842 DCI.AddToWorklist(Val.Val);
1843 return Val;
1844 }
1845 break;
Chris Lattner4468c222006-03-31 06:02:07 +00001846 case PPCISD::VCMP: {
1847 // If a VCMPo node already exists with exactly the same operands as this
1848 // node, use its result instead of this node (VCMPo computes both a CR6 and
1849 // a normal output).
1850 //
1851 if (!N->getOperand(0).hasOneUse() &&
1852 !N->getOperand(1).hasOneUse() &&
1853 !N->getOperand(2).hasOneUse()) {
1854
1855 // Scan all of the users of the LHS, looking for VCMPo's that match.
1856 SDNode *VCMPoNode = 0;
1857
1858 SDNode *LHSN = N->getOperand(0).Val;
1859 for (SDNode::use_iterator UI = LHSN->use_begin(), E = LHSN->use_end();
1860 UI != E; ++UI)
1861 if ((*UI)->getOpcode() == PPCISD::VCMPo &&
1862 (*UI)->getOperand(1) == N->getOperand(1) &&
1863 (*UI)->getOperand(2) == N->getOperand(2) &&
1864 (*UI)->getOperand(0) == N->getOperand(0)) {
1865 VCMPoNode = *UI;
1866 break;
1867 }
1868
1869 // If there are non-zero uses of the flag value, use the VCMPo node!
Chris Lattner33497cc2006-03-31 06:04:53 +00001870 if (VCMPoNode && !VCMPoNode->hasNUsesOfValue(0, 1))
Chris Lattner4468c222006-03-31 06:02:07 +00001871 return SDOperand(VCMPoNode, 0);
1872 }
1873 break;
1874 }
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001875 }
1876
1877 return SDOperand();
1878}
1879
Chris Lattner1a635d62006-04-14 06:01:58 +00001880//===----------------------------------------------------------------------===//
1881// Inline Assembly Support
1882//===----------------------------------------------------------------------===//
1883
Chris Lattnerbbe77de2006-04-02 06:26:07 +00001884void PPCTargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
1885 uint64_t Mask,
1886 uint64_t &KnownZero,
1887 uint64_t &KnownOne,
1888 unsigned Depth) const {
1889 KnownZero = 0;
1890 KnownOne = 0;
1891 switch (Op.getOpcode()) {
1892 default: break;
1893 case ISD::INTRINSIC_WO_CHAIN: {
1894 switch (cast<ConstantSDNode>(Op.getOperand(0))->getValue()) {
1895 default: break;
1896 case Intrinsic::ppc_altivec_vcmpbfp_p:
1897 case Intrinsic::ppc_altivec_vcmpeqfp_p:
1898 case Intrinsic::ppc_altivec_vcmpequb_p:
1899 case Intrinsic::ppc_altivec_vcmpequh_p:
1900 case Intrinsic::ppc_altivec_vcmpequw_p:
1901 case Intrinsic::ppc_altivec_vcmpgefp_p:
1902 case Intrinsic::ppc_altivec_vcmpgtfp_p:
1903 case Intrinsic::ppc_altivec_vcmpgtsb_p:
1904 case Intrinsic::ppc_altivec_vcmpgtsh_p:
1905 case Intrinsic::ppc_altivec_vcmpgtsw_p:
1906 case Intrinsic::ppc_altivec_vcmpgtub_p:
1907 case Intrinsic::ppc_altivec_vcmpgtuh_p:
1908 case Intrinsic::ppc_altivec_vcmpgtuw_p:
1909 KnownZero = ~1U; // All bits but the low one are known to be zero.
1910 break;
1911 }
1912 }
1913 }
1914}
1915
1916
Chris Lattnerad3bc8d2006-02-07 20:16:30 +00001917/// getConstraintType - Given a constraint letter, return the type of
1918/// constraint it is for this target.
1919PPCTargetLowering::ConstraintType
1920PPCTargetLowering::getConstraintType(char ConstraintLetter) const {
1921 switch (ConstraintLetter) {
1922 default: break;
1923 case 'b':
1924 case 'r':
1925 case 'f':
1926 case 'v':
1927 case 'y':
1928 return C_RegisterClass;
1929 }
1930 return TargetLowering::getConstraintType(ConstraintLetter);
1931}
1932
1933
Chris Lattnerddc787d2006-01-31 19:20:21 +00001934std::vector<unsigned> PPCTargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00001935getRegClassForInlineAsmConstraint(const std::string &Constraint,
1936 MVT::ValueType VT) const {
Chris Lattnerddc787d2006-01-31 19:20:21 +00001937 if (Constraint.size() == 1) {
1938 switch (Constraint[0]) { // GCC RS6000 Constraint Letters
1939 default: break; // Unknown constriant letter
1940 case 'b':
1941 return make_vector<unsigned>(/*no R0*/ PPC::R1 , PPC::R2 , PPC::R3 ,
1942 PPC::R4 , PPC::R5 , PPC::R6 , PPC::R7 ,
1943 PPC::R8 , PPC::R9 , PPC::R10, PPC::R11,
1944 PPC::R12, PPC::R13, PPC::R14, PPC::R15,
1945 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
1946 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
1947 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
1948 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
1949 0);
1950 case 'r':
1951 return make_vector<unsigned>(PPC::R0 , PPC::R1 , PPC::R2 , PPC::R3 ,
1952 PPC::R4 , PPC::R5 , PPC::R6 , PPC::R7 ,
1953 PPC::R8 , PPC::R9 , PPC::R10, PPC::R11,
1954 PPC::R12, PPC::R13, PPC::R14, PPC::R15,
1955 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
1956 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
1957 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
1958 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
1959 0);
1960 case 'f':
1961 return make_vector<unsigned>(PPC::F0 , PPC::F1 , PPC::F2 , PPC::F3 ,
1962 PPC::F4 , PPC::F5 , PPC::F6 , PPC::F7 ,
1963 PPC::F8 , PPC::F9 , PPC::F10, PPC::F11,
1964 PPC::F12, PPC::F13, PPC::F14, PPC::F15,
1965 PPC::F16, PPC::F17, PPC::F18, PPC::F19,
1966 PPC::F20, PPC::F21, PPC::F22, PPC::F23,
1967 PPC::F24, PPC::F25, PPC::F26, PPC::F27,
1968 PPC::F28, PPC::F29, PPC::F30, PPC::F31,
1969 0);
1970 case 'v':
1971 return make_vector<unsigned>(PPC::V0 , PPC::V1 , PPC::V2 , PPC::V3 ,
1972 PPC::V4 , PPC::V5 , PPC::V6 , PPC::V7 ,
1973 PPC::V8 , PPC::V9 , PPC::V10, PPC::V11,
1974 PPC::V12, PPC::V13, PPC::V14, PPC::V15,
1975 PPC::V16, PPC::V17, PPC::V18, PPC::V19,
1976 PPC::V20, PPC::V21, PPC::V22, PPC::V23,
1977 PPC::V24, PPC::V25, PPC::V26, PPC::V27,
1978 PPC::V28, PPC::V29, PPC::V30, PPC::V31,
1979 0);
1980 case 'y':
1981 return make_vector<unsigned>(PPC::CR0, PPC::CR1, PPC::CR2, PPC::CR3,
1982 PPC::CR4, PPC::CR5, PPC::CR6, PPC::CR7,
1983 0);
1984 }
1985 }
1986
Chris Lattner1efa40f2006-02-22 00:56:39 +00001987 return std::vector<unsigned>();
Chris Lattnerddc787d2006-01-31 19:20:21 +00001988}
Chris Lattner763317d2006-02-07 00:47:13 +00001989
1990// isOperandValidForConstraint
1991bool PPCTargetLowering::
1992isOperandValidForConstraint(SDOperand Op, char Letter) {
1993 switch (Letter) {
1994 default: break;
1995 case 'I':
1996 case 'J':
1997 case 'K':
1998 case 'L':
1999 case 'M':
2000 case 'N':
2001 case 'O':
2002 case 'P': {
2003 if (!isa<ConstantSDNode>(Op)) return false; // Must be an immediate.
2004 unsigned Value = cast<ConstantSDNode>(Op)->getValue();
2005 switch (Letter) {
2006 default: assert(0 && "Unknown constraint letter!");
2007 case 'I': // "I" is a signed 16-bit constant.
2008 return (short)Value == (int)Value;
2009 case 'J': // "J" is a constant with only the high-order 16 bits nonzero.
2010 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits.
2011 return (short)Value == 0;
2012 case 'K': // "K" is a constant with only the low-order 16 bits nonzero.
2013 return (Value >> 16) == 0;
2014 case 'M': // "M" is a constant that is greater than 31.
2015 return Value > 31;
2016 case 'N': // "N" is a positive constant that is an exact power of two.
2017 return (int)Value > 0 && isPowerOf2_32(Value);
2018 case 'O': // "O" is the constant zero.
2019 return Value == 0;
2020 case 'P': // "P" is a constant whose negation is a signed 16-bit constant.
2021 return (short)-Value == (int)-Value;
2022 }
2023 break;
2024 }
2025 }
2026
2027 // Handle standard constraint letters.
2028 return TargetLowering::isOperandValidForConstraint(Op, Letter);
2029}
Evan Chengc4c62572006-03-13 23:20:37 +00002030
2031/// isLegalAddressImmediate - Return true if the integer value can be used
2032/// as the offset of the target addressing mode.
2033bool PPCTargetLowering::isLegalAddressImmediate(int64_t V) const {
2034 // PPC allows a sign-extended 16-bit immediate field.
2035 return (V > -(1 << 16) && V < (1 << 16)-1);
2036}