blob: c8a0cc914030575e1f740d8f860167dc68a24ab1 [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"
Chris Lattner59138102006-04-17 05:28:54 +000016#include "PPCPerfectShuffle.h"
Nate Begeman750ac1b2006-02-01 07:19:44 +000017#include "llvm/ADT/VectorExtras.h"
Evan Chengc4c62572006-03-13 23:20:37 +000018#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000019#include "llvm/CodeGen/MachineFrameInfo.h"
20#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner8a2d3ca2005-08-26 21:23:58 +000021#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000022#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner7b738342005-09-13 19:33:40 +000023#include "llvm/CodeGen/SSARegMap.h"
Chris Lattner0b1e4e52005-08-26 17:36:52 +000024#include "llvm/Constants.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000025#include "llvm/Function.h"
Chris Lattner6d92cad2006-03-26 10:06:40 +000026#include "llvm/Intrinsics.h"
Nate Begeman750ac1b2006-02-01 07:19:44 +000027#include "llvm/Support/MathExtras.h"
Evan Chengd2ee2182006-02-18 00:08:58 +000028#include "llvm/Target/TargetOptions.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000029using namespace llvm;
30
Nate Begeman21e463b2005-10-16 05:39:50 +000031PPCTargetLowering::PPCTargetLowering(TargetMachine &TM)
Chris Lattner7c5a3d32005-08-16 17:14:42 +000032 : TargetLowering(TM) {
33
34 // Fold away setcc operations if possible.
35 setSetCCIsExpensive();
Nate Begeman405e3ec2005-10-21 00:02:42 +000036 setPow2DivIsCheap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +000037
Chris Lattnerd145a612005-09-27 22:18:25 +000038 // Use _setjmp/_longjmp instead of setjmp/longjmp.
39 setUseUnderscoreSetJmpLongJmp(true);
40
Chris Lattner7c5a3d32005-08-16 17:14:42 +000041 // Set up the register classes.
Nate Begeman1d9d7422005-10-18 00:28:58 +000042 addRegisterClass(MVT::i32, PPC::GPRCRegisterClass);
43 addRegisterClass(MVT::f32, PPC::F4RCRegisterClass);
44 addRegisterClass(MVT::f64, PPC::F8RCRegisterClass);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000045
Chris Lattnera54aa942006-01-29 06:26:08 +000046 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
47 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
48
Chris Lattner7c5a3d32005-08-16 17:14:42 +000049 // PowerPC has no intrinsics for these particular operations
50 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
51 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
52 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
53
54 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
55 setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
56 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
57
58 // PowerPC has no SREM/UREM instructions
59 setOperationAction(ISD::SREM, MVT::i32, Expand);
60 setOperationAction(ISD::UREM, MVT::i32, Expand);
61
62 // We don't support sin/cos/sqrt/fmod
63 setOperationAction(ISD::FSIN , MVT::f64, Expand);
64 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattner615c2d02005-09-28 22:29:58 +000065 setOperationAction(ISD::FREM , MVT::f64, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000066 setOperationAction(ISD::FSIN , MVT::f32, Expand);
67 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattner615c2d02005-09-28 22:29:58 +000068 setOperationAction(ISD::FREM , MVT::f32, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000069
70 // If we're enabling GP optimizations, use hardware square root
Chris Lattner1e9de3e2005-09-02 18:33:05 +000071 if (!TM.getSubtarget<PPCSubtarget>().hasFSQRT()) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +000072 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
73 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
74 }
75
Chris Lattner9601a862006-03-05 05:08:37 +000076 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
77 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
78
Nate Begemand88fc032006-01-14 03:14:10 +000079 // PowerPC does not have BSWAP, CTPOP or CTTZ
80 setOperationAction(ISD::BSWAP, MVT::i32 , Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000081 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
82 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
83
Nate Begeman35ef9132006-01-11 21:21:00 +000084 // PowerPC does not have ROTR
85 setOperationAction(ISD::ROTR, MVT::i32 , Expand);
86
Chris Lattner7c5a3d32005-08-16 17:14:42 +000087 // PowerPC does not have Select
88 setOperationAction(ISD::SELECT, MVT::i32, Expand);
89 setOperationAction(ISD::SELECT, MVT::f32, Expand);
90 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +000091
Chris Lattner0b1e4e52005-08-26 17:36:52 +000092 // PowerPC wants to turn select_cc of FP into fsel when possible.
93 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
94 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Nate Begeman44775902006-01-31 08:17:29 +000095
Nate Begeman750ac1b2006-02-01 07:19:44 +000096 // PowerPC wants to optimize integer setcc a bit
Nate Begeman44775902006-01-31 08:17:29 +000097 setOperationAction(ISD::SETCC, MVT::i32, Custom);
Chris Lattnereb9b62e2005-08-31 19:09:57 +000098
Nate Begeman81e80972006-03-17 01:40:33 +000099 // PowerPC does not have BRCOND which requires SetCC
100 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000101
Chris Lattnerf7605322005-08-31 21:09:52 +0000102 // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
103 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000104
Jim Laskeyad23c9d2005-08-17 00:40:22 +0000105 // PowerPC does not have [U|S]INT_TO_FP
106 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
107 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
108
Chris Lattner53e88452005-12-23 05:13:35 +0000109 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
110 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
111
Chris Lattnere6ec9f22005-09-10 00:21:06 +0000112 // PowerPC does not have truncstore for i1.
113 setOperationAction(ISD::TRUNCSTORE, MVT::i1, Promote);
Chris Lattnerf73bae12005-11-29 06:16:21 +0000114
Jim Laskeyabf6d172006-01-05 01:25:28 +0000115 // Support label based line numbers.
Chris Lattnerf73bae12005-11-29 06:16:21 +0000116 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000117 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000118 // FIXME - use subtarget debug flags
Jim Laskeye0bce712006-01-05 01:47:43 +0000119 if (!TM.getSubtarget<PPCSubtarget>().isDarwin())
Jim Laskeyabf6d172006-01-05 01:25:28 +0000120 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattnere6ec9f22005-09-10 00:21:06 +0000121
Nate Begeman28a6b022005-12-10 02:36:00 +0000122 // We want to legalize GlobalAddress and ConstantPool nodes into the
123 // appropriate instructions to materialize the address.
Chris Lattner3eef4e32005-11-17 18:26:56 +0000124 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Nate Begeman28a6b022005-12-10 02:36:00 +0000125 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000126
Nate Begemanee625572006-01-27 21:09:22 +0000127 // RET must be custom lowered, to meet ABI requirements
128 setOperationAction(ISD::RET , MVT::Other, Custom);
129
Nate Begemanacc398c2006-01-25 18:21:52 +0000130 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
131 setOperationAction(ISD::VASTART , MVT::Other, Custom);
132
Chris Lattnerb22c08b2006-01-15 09:02:48 +0000133 // Use the default implementation.
Nate Begemanacc398c2006-01-25 18:21:52 +0000134 setOperationAction(ISD::VAARG , MVT::Other, Expand);
135 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
136 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Chris Lattnerb22c08b2006-01-15 09:02:48 +0000137 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
138 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
139 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Chris Lattner860e8862005-11-17 07:30:41 +0000140
Chris Lattner6d92cad2006-03-26 10:06:40 +0000141 // We want to custom lower some of our intrinsics.
Chris Lattner48b61a72006-03-28 00:40:33 +0000142 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Chris Lattner6d92cad2006-03-26 10:06:40 +0000143
Nate Begemanc09eeec2005-09-06 22:03:27 +0000144 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000145 // They also have instructions for converting between i64 and fp.
Nate Begemanc09eeec2005-09-06 22:03:27 +0000146 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
147 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Chris Lattner7fbcef72006-03-24 07:53:47 +0000148
149 // FIXME: disable this lowered code. This generates 64-bit register values,
150 // and we don't model the fact that the top part is clobbered by calls. We
151 // need to flag these together so that the value isn't live across a call.
152 //setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
153
Nate Begemanae749a92005-10-25 23:48:36 +0000154 // To take advantage of the above i64 FP_TO_SINT, promote i32 FP_TO_UINT
155 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Promote);
156 } else {
Chris Lattner860e8862005-11-17 07:30:41 +0000157 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Nate Begemanae749a92005-10-25 23:48:36 +0000158 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begeman9d2b8172005-10-18 00:56:42 +0000159 }
160
161 if (TM.getSubtarget<PPCSubtarget>().has64BitRegs()) {
162 // 64 bit PowerPC implementations can support i64 types directly
163 addRegisterClass(MVT::i64, PPC::G8RCRegisterClass);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000164 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
165 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000166 } else {
167 // 32 bit PowerPC wants to expand i64 shifts itself.
168 setOperationAction(ISD::SHL, MVT::i64, Custom);
169 setOperationAction(ISD::SRL, MVT::i64, Custom);
170 setOperationAction(ISD::SRA, MVT::i64, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000171 }
Evan Chengd30bf012006-03-01 01:11:20 +0000172
Nate Begeman425a9692005-11-29 08:17:20 +0000173 if (TM.getSubtarget<PPCSubtarget>().hasAltivec()) {
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000174 // First set operation action for all vector types to expand. Then we
175 // will selectively turn on ones that can be effectively codegen'd.
176 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
177 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Chris Lattnerf3f69de2006-04-16 01:37:57 +0000178 // add/sub are legal for all supported vector VT's.
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000179 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Legal);
180 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Legal);
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000181
Chris Lattner7ff7e672006-04-04 17:25:31 +0000182 // We promote all shuffles to v16i8.
183 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Promote);
Chris Lattnerf3f69de2006-04-16 01:37:57 +0000184 AddPromotedToType (ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, MVT::v16i8);
185
186 // We promote all non-typed operations to v4i32.
187 setOperationAction(ISD::AND , (MVT::ValueType)VT, Promote);
188 AddPromotedToType (ISD::AND , (MVT::ValueType)VT, MVT::v4i32);
189 setOperationAction(ISD::OR , (MVT::ValueType)VT, Promote);
190 AddPromotedToType (ISD::OR , (MVT::ValueType)VT, MVT::v4i32);
191 setOperationAction(ISD::XOR , (MVT::ValueType)VT, Promote);
192 AddPromotedToType (ISD::XOR , (MVT::ValueType)VT, MVT::v4i32);
193 setOperationAction(ISD::LOAD , (MVT::ValueType)VT, Promote);
194 AddPromotedToType (ISD::LOAD , (MVT::ValueType)VT, MVT::v4i32);
195 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
196 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v4i32);
197 setOperationAction(ISD::STORE, (MVT::ValueType)VT, Promote);
198 AddPromotedToType (ISD::STORE, (MVT::ValueType)VT, MVT::v4i32);
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000199
Chris Lattnerf3f69de2006-04-16 01:37:57 +0000200 // No other operations are legal.
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000201 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
202 setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand);
203 setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand);
204 setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand);
205 setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand);
206 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
207 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
208 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Expand);
Chris Lattner01cae072006-04-03 23:55:43 +0000209
210 setOperationAction(ISD::SCALAR_TO_VECTOR, (MVT::ValueType)VT, Expand);
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000211 }
212
Chris Lattner7ff7e672006-04-04 17:25:31 +0000213 // We can custom expand all VECTOR_SHUFFLEs to VPERM, others we can handle
214 // with merges, splats, etc.
215 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i8, Custom);
216
Chris Lattnerf3f69de2006-04-16 01:37:57 +0000217 setOperationAction(ISD::AND , MVT::v4i32, Legal);
218 setOperationAction(ISD::OR , MVT::v4i32, Legal);
219 setOperationAction(ISD::XOR , MVT::v4i32, Legal);
220 setOperationAction(ISD::LOAD , MVT::v4i32, Legal);
221 setOperationAction(ISD::SELECT, MVT::v4i32, Expand);
222 setOperationAction(ISD::STORE , MVT::v4i32, Legal);
223
Nate Begeman425a9692005-11-29 08:17:20 +0000224 addRegisterClass(MVT::v4f32, PPC::VRRCRegisterClass);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000225 addRegisterClass(MVT::v4i32, PPC::VRRCRegisterClass);
Chris Lattner8d052bc2006-03-25 07:39:07 +0000226 addRegisterClass(MVT::v8i16, PPC::VRRCRegisterClass);
227 addRegisterClass(MVT::v16i8, PPC::VRRCRegisterClass);
Chris Lattnerec4a0c72006-01-29 06:32:58 +0000228
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000229 setOperationAction(ISD::MUL, MVT::v4f32, Legal);
Chris Lattnere7c768e2006-04-18 03:24:30 +0000230 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
Chris Lattner72dd9bd2006-04-18 03:43:48 +0000231 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000232
Chris Lattnerb2177b92006-03-19 06:55:52 +0000233 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom);
234 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Custom);
Chris Lattner64b3a082006-03-24 07:48:08 +0000235
Chris Lattner541f91b2006-04-02 00:43:36 +0000236 setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Custom);
237 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Custom);
Chris Lattner64b3a082006-03-24 07:48:08 +0000238 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Custom);
239 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
Nate Begeman425a9692005-11-29 08:17:20 +0000240 }
241
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000242 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattnercadd7422006-01-13 17:52:03 +0000243 setStackPointerRegisterToSaveRestore(PPC::R1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000244
Chris Lattner8c13d0a2006-03-01 04:57:39 +0000245 // We have target-specific dag combine patterns for the following nodes:
246 setTargetDAGCombine(ISD::SINT_TO_FP);
Chris Lattner51269842006-03-01 05:50:56 +0000247 setTargetDAGCombine(ISD::STORE);
Chris Lattner8c13d0a2006-03-01 04:57:39 +0000248
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000249 computeRegisterProperties();
250}
251
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000252const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
253 switch (Opcode) {
254 default: return 0;
255 case PPCISD::FSEL: return "PPCISD::FSEL";
256 case PPCISD::FCFID: return "PPCISD::FCFID";
257 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
258 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
Chris Lattner51269842006-03-01 05:50:56 +0000259 case PPCISD::STFIWX: return "PPCISD::STFIWX";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000260 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
261 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000262 case PPCISD::VPERM: return "PPCISD::VPERM";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000263 case PPCISD::Hi: return "PPCISD::Hi";
264 case PPCISD::Lo: return "PPCISD::Lo";
265 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
266 case PPCISD::SRL: return "PPCISD::SRL";
267 case PPCISD::SRA: return "PPCISD::SRA";
268 case PPCISD::SHL: return "PPCISD::SHL";
Chris Lattnerecfe55e2006-03-22 05:30:33 +0000269 case PPCISD::EXTSW_32: return "PPCISD::EXTSW_32";
270 case PPCISD::STD_32: return "PPCISD::STD_32";
Chris Lattnere00ebf02006-01-28 07:33:03 +0000271 case PPCISD::CALL: return "PPCISD::CALL";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000272 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
Chris Lattner6d92cad2006-03-26 10:06:40 +0000273 case PPCISD::MFCR: return "PPCISD::MFCR";
Chris Lattnera17b1552006-03-31 05:13:27 +0000274 case PPCISD::VCMP: return "PPCISD::VCMP";
Chris Lattner6d92cad2006-03-26 10:06:40 +0000275 case PPCISD::VCMPo: return "PPCISD::VCMPo";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000276 }
277}
278
Chris Lattner1a635d62006-04-14 06:01:58 +0000279//===----------------------------------------------------------------------===//
280// Node matching predicates, for use by the tblgen matching code.
281//===----------------------------------------------------------------------===//
282
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000283/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
284static bool isFloatingPointZero(SDOperand Op) {
285 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
286 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
287 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
288 // Maybe this has already been legalized into the constant pool?
289 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
290 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
291 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
292 }
293 return false;
294}
295
Chris Lattnerddb739e2006-04-06 17:23:16 +0000296/// isConstantOrUndef - Op is either an undef node or a ConstantSDNode. Return
297/// true if Op is undef or if it matches the specified value.
298static bool isConstantOrUndef(SDOperand Op, unsigned Val) {
299 return Op.getOpcode() == ISD::UNDEF ||
300 cast<ConstantSDNode>(Op)->getValue() == Val;
301}
302
303/// isVPKUHUMShuffleMask - Return true if this is the shuffle mask for a
304/// VPKUHUM instruction.
Chris Lattnerf24380e2006-04-06 22:28:36 +0000305bool PPC::isVPKUHUMShuffleMask(SDNode *N, bool isUnary) {
306 if (!isUnary) {
307 for (unsigned i = 0; i != 16; ++i)
308 if (!isConstantOrUndef(N->getOperand(i), i*2+1))
309 return false;
310 } else {
311 for (unsigned i = 0; i != 8; ++i)
312 if (!isConstantOrUndef(N->getOperand(i), i*2+1) ||
313 !isConstantOrUndef(N->getOperand(i+8), i*2+1))
314 return false;
315 }
Chris Lattnerd0608e12006-04-06 18:26:28 +0000316 return true;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000317}
318
319/// isVPKUWUMShuffleMask - Return true if this is the shuffle mask for a
320/// VPKUWUM instruction.
Chris Lattnerf24380e2006-04-06 22:28:36 +0000321bool PPC::isVPKUWUMShuffleMask(SDNode *N, bool isUnary) {
322 if (!isUnary) {
323 for (unsigned i = 0; i != 16; i += 2)
324 if (!isConstantOrUndef(N->getOperand(i ), i*2+2) ||
325 !isConstantOrUndef(N->getOperand(i+1), i*2+3))
326 return false;
327 } else {
328 for (unsigned i = 0; i != 8; i += 2)
329 if (!isConstantOrUndef(N->getOperand(i ), i*2+2) ||
330 !isConstantOrUndef(N->getOperand(i+1), i*2+3) ||
331 !isConstantOrUndef(N->getOperand(i+8), i*2+2) ||
332 !isConstantOrUndef(N->getOperand(i+9), i*2+3))
333 return false;
334 }
Chris Lattnerd0608e12006-04-06 18:26:28 +0000335 return true;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000336}
337
Chris Lattnercaad1632006-04-06 22:02:42 +0000338/// isVMerge - Common function, used to match vmrg* shuffles.
339///
340static bool isVMerge(SDNode *N, unsigned UnitSize,
341 unsigned LHSStart, unsigned RHSStart) {
Chris Lattner116cc482006-04-06 21:11:54 +0000342 assert(N->getOpcode() == ISD::BUILD_VECTOR &&
343 N->getNumOperands() == 16 && "PPC only supports shuffles by bytes!");
344 assert((UnitSize == 1 || UnitSize == 2 || UnitSize == 4) &&
345 "Unsupported merge size!");
346
347 for (unsigned i = 0; i != 8/UnitSize; ++i) // Step over units
348 for (unsigned j = 0; j != UnitSize; ++j) { // Step over bytes within unit
349 if (!isConstantOrUndef(N->getOperand(i*UnitSize*2+j),
Chris Lattnercaad1632006-04-06 22:02:42 +0000350 LHSStart+j+i*UnitSize) ||
Chris Lattner116cc482006-04-06 21:11:54 +0000351 !isConstantOrUndef(N->getOperand(i*UnitSize*2+UnitSize+j),
Chris Lattnercaad1632006-04-06 22:02:42 +0000352 RHSStart+j+i*UnitSize))
Chris Lattner116cc482006-04-06 21:11:54 +0000353 return false;
354 }
Chris Lattnercaad1632006-04-06 22:02:42 +0000355 return true;
356}
357
358/// isVMRGLShuffleMask - Return true if this is a shuffle mask suitable for
359/// a VRGL* instruction with the specified unit size (1,2 or 4 bytes).
360bool PPC::isVMRGLShuffleMask(SDNode *N, unsigned UnitSize, bool isUnary) {
361 if (!isUnary)
362 return isVMerge(N, UnitSize, 8, 24);
363 return isVMerge(N, UnitSize, 8, 8);
Chris Lattner116cc482006-04-06 21:11:54 +0000364}
365
366/// isVMRGHShuffleMask - Return true if this is a shuffle mask suitable for
367/// a VRGH* instruction with the specified unit size (1,2 or 4 bytes).
Chris Lattnercaad1632006-04-06 22:02:42 +0000368bool PPC::isVMRGHShuffleMask(SDNode *N, unsigned UnitSize, bool isUnary) {
369 if (!isUnary)
370 return isVMerge(N, UnitSize, 0, 16);
371 return isVMerge(N, UnitSize, 0, 0);
Chris Lattner116cc482006-04-06 21:11:54 +0000372}
373
374
Chris Lattnerd0608e12006-04-06 18:26:28 +0000375/// isVSLDOIShuffleMask - If this is a vsldoi shuffle mask, return the shift
376/// amount, otherwise return -1.
Chris Lattnerf24380e2006-04-06 22:28:36 +0000377int PPC::isVSLDOIShuffleMask(SDNode *N, bool isUnary) {
Chris Lattner116cc482006-04-06 21:11:54 +0000378 assert(N->getOpcode() == ISD::BUILD_VECTOR &&
379 N->getNumOperands() == 16 && "PPC only supports shuffles by bytes!");
Chris Lattnerd0608e12006-04-06 18:26:28 +0000380 // Find the first non-undef value in the shuffle mask.
381 unsigned i;
382 for (i = 0; i != 16 && N->getOperand(i).getOpcode() == ISD::UNDEF; ++i)
383 /*search*/;
384
385 if (i == 16) return -1; // all undef.
386
387 // Otherwise, check to see if the rest of the elements are consequtively
388 // numbered from this value.
389 unsigned ShiftAmt = cast<ConstantSDNode>(N->getOperand(i))->getValue();
390 if (ShiftAmt < i) return -1;
391 ShiftAmt -= i;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000392
Chris Lattnerf24380e2006-04-06 22:28:36 +0000393 if (!isUnary) {
394 // Check the rest of the elements to see if they are consequtive.
395 for (++i; i != 16; ++i)
396 if (!isConstantOrUndef(N->getOperand(i), ShiftAmt+i))
397 return -1;
398 } else {
399 // Check the rest of the elements to see if they are consequtive.
400 for (++i; i != 16; ++i)
401 if (!isConstantOrUndef(N->getOperand(i), (ShiftAmt+i) & 15))
402 return -1;
403 }
Chris Lattnerd0608e12006-04-06 18:26:28 +0000404
405 return ShiftAmt;
406}
Chris Lattneref819f82006-03-20 06:33:01 +0000407
408/// isSplatShuffleMask - Return true if the specified VECTOR_SHUFFLE operand
409/// specifies a splat of a single element that is suitable for input to
410/// VSPLTB/VSPLTH/VSPLTW.
Chris Lattner7ff7e672006-04-04 17:25:31 +0000411bool PPC::isSplatShuffleMask(SDNode *N, unsigned EltSize) {
412 assert(N->getOpcode() == ISD::BUILD_VECTOR &&
413 N->getNumOperands() == 16 &&
414 (EltSize == 1 || EltSize == 2 || EltSize == 4));
Chris Lattnerdd4d2d02006-03-20 06:51:10 +0000415
Chris Lattner88a99ef2006-03-20 06:37:44 +0000416 // This is a splat operation if each element of the permute is the same, and
417 // if the value doesn't reference the second vector.
Chris Lattner7ff7e672006-04-04 17:25:31 +0000418 unsigned ElementBase = 0;
Chris Lattner88a99ef2006-03-20 06:37:44 +0000419 SDOperand Elt = N->getOperand(0);
Chris Lattner7ff7e672006-04-04 17:25:31 +0000420 if (ConstantSDNode *EltV = dyn_cast<ConstantSDNode>(Elt))
421 ElementBase = EltV->getValue();
422 else
423 return false; // FIXME: Handle UNDEF elements too!
424
425 if (cast<ConstantSDNode>(Elt)->getValue() >= 16)
426 return false;
427
428 // Check that they are consequtive.
429 for (unsigned i = 1; i != EltSize; ++i) {
430 if (!isa<ConstantSDNode>(N->getOperand(i)) ||
431 cast<ConstantSDNode>(N->getOperand(i))->getValue() != i+ElementBase)
432 return false;
433 }
434
Chris Lattner88a99ef2006-03-20 06:37:44 +0000435 assert(isa<ConstantSDNode>(Elt) && "Invalid VECTOR_SHUFFLE mask!");
Chris Lattner7ff7e672006-04-04 17:25:31 +0000436 for (unsigned i = EltSize, e = 16; i != e; i += EltSize) {
Chris Lattnerb097aa92006-04-14 23:19:08 +0000437 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Chris Lattner88a99ef2006-03-20 06:37:44 +0000438 assert(isa<ConstantSDNode>(N->getOperand(i)) &&
439 "Invalid VECTOR_SHUFFLE mask!");
Chris Lattner7ff7e672006-04-04 17:25:31 +0000440 for (unsigned j = 0; j != EltSize; ++j)
441 if (N->getOperand(i+j) != N->getOperand(j))
442 return false;
Chris Lattner88a99ef2006-03-20 06:37:44 +0000443 }
444
Chris Lattner7ff7e672006-04-04 17:25:31 +0000445 return true;
Chris Lattneref819f82006-03-20 06:33:01 +0000446}
447
448/// getVSPLTImmediate - Return the appropriate VSPLT* immediate to splat the
449/// specified isSplatShuffleMask VECTOR_SHUFFLE mask.
Chris Lattner7ff7e672006-04-04 17:25:31 +0000450unsigned PPC::getVSPLTImmediate(SDNode *N, unsigned EltSize) {
451 assert(isSplatShuffleMask(N, EltSize));
452 return cast<ConstantSDNode>(N->getOperand(0))->getValue() / EltSize;
Chris Lattneref819f82006-03-20 06:33:01 +0000453}
454
Chris Lattnere87192a2006-04-12 17:37:20 +0000455/// get_VSPLTI_elt - If this is a build_vector of constants which can be formed
Chris Lattner140a58f2006-04-08 06:46:53 +0000456/// by using a vspltis[bhw] instruction of the specified element size, return
457/// the constant being splatted. The ByteSize field indicates the number of
458/// bytes of each element [124] -> [bhw].
Chris Lattnere87192a2006-04-12 17:37:20 +0000459SDOperand PPC::get_VSPLTI_elt(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000460 SDOperand OpVal(0, 0);
Chris Lattner79d9a882006-04-08 07:14:26 +0000461
462 // If ByteSize of the splat is bigger than the element size of the
463 // build_vector, then we have a case where we are checking for a splat where
464 // multiple elements of the buildvector are folded together into a single
465 // logical element of the splat (e.g. "vsplish 1" to splat {0,1}*8).
466 unsigned EltSize = 16/N->getNumOperands();
467 if (EltSize < ByteSize) {
468 unsigned Multiple = ByteSize/EltSize; // Number of BV entries per spltval.
469 SDOperand UniquedVals[4];
470 assert(Multiple > 1 && Multiple <= 4 && "How can this happen?");
471
472 // See if all of the elements in the buildvector agree across.
473 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
474 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
475 // If the element isn't a constant, bail fully out.
476 if (!isa<ConstantSDNode>(N->getOperand(i))) return SDOperand();
477
478
479 if (UniquedVals[i&(Multiple-1)].Val == 0)
480 UniquedVals[i&(Multiple-1)] = N->getOperand(i);
481 else if (UniquedVals[i&(Multiple-1)] != N->getOperand(i))
482 return SDOperand(); // no match.
483 }
484
485 // Okay, if we reached this point, UniquedVals[0..Multiple-1] contains
486 // either constant or undef values that are identical for each chunk. See
487 // if these chunks can form into a larger vspltis*.
488
489 // Check to see if all of the leading entries are either 0 or -1. If
490 // neither, then this won't fit into the immediate field.
491 bool LeadingZero = true;
492 bool LeadingOnes = true;
493 for (unsigned i = 0; i != Multiple-1; ++i) {
494 if (UniquedVals[i].Val == 0) continue; // Must have been undefs.
495
496 LeadingZero &= cast<ConstantSDNode>(UniquedVals[i])->isNullValue();
497 LeadingOnes &= cast<ConstantSDNode>(UniquedVals[i])->isAllOnesValue();
498 }
499 // Finally, check the least significant entry.
500 if (LeadingZero) {
501 if (UniquedVals[Multiple-1].Val == 0)
502 return DAG.getTargetConstant(0, MVT::i32); // 0,0,0,undef
503 int Val = cast<ConstantSDNode>(UniquedVals[Multiple-1])->getValue();
504 if (Val < 16)
505 return DAG.getTargetConstant(Val, MVT::i32); // 0,0,0,4 -> vspltisw(4)
506 }
507 if (LeadingOnes) {
508 if (UniquedVals[Multiple-1].Val == 0)
509 return DAG.getTargetConstant(~0U, MVT::i32); // -1,-1,-1,undef
510 int Val =cast<ConstantSDNode>(UniquedVals[Multiple-1])->getSignExtended();
511 if (Val >= -16) // -1,-1,-1,-2 -> vspltisw(-2)
512 return DAG.getTargetConstant(Val, MVT::i32);
513 }
514
515 return SDOperand();
516 }
517
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000518 // Check to see if this buildvec has a single non-undef value in its elements.
519 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
520 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
521 if (OpVal.Val == 0)
522 OpVal = N->getOperand(i);
523 else if (OpVal != N->getOperand(i))
Chris Lattner140a58f2006-04-08 06:46:53 +0000524 return SDOperand();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000525 }
526
Chris Lattner140a58f2006-04-08 06:46:53 +0000527 if (OpVal.Val == 0) return SDOperand(); // All UNDEF: use implicit def.
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000528
Nate Begeman98e70cc2006-03-28 04:15:58 +0000529 unsigned ValSizeInBytes = 0;
530 uint64_t Value = 0;
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000531 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
532 Value = CN->getValue();
533 ValSizeInBytes = MVT::getSizeInBits(CN->getValueType(0))/8;
534 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) {
535 assert(CN->getValueType(0) == MVT::f32 && "Only one legal FP vector type!");
536 Value = FloatToBits(CN->getValue());
537 ValSizeInBytes = 4;
538 }
539
540 // If the splat value is larger than the element value, then we can never do
541 // this splat. The only case that we could fit the replicated bits into our
542 // immediate field for would be zero, and we prefer to use vxor for it.
Chris Lattner140a58f2006-04-08 06:46:53 +0000543 if (ValSizeInBytes < ByteSize) return SDOperand();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000544
545 // If the element value is larger than the splat value, cut it in half and
546 // check to see if the two halves are equal. Continue doing this until we
547 // get to ByteSize. This allows us to handle 0x01010101 as 0x01.
548 while (ValSizeInBytes > ByteSize) {
549 ValSizeInBytes >>= 1;
550
551 // If the top half equals the bottom half, we're still ok.
Chris Lattner9b42bdd2006-04-05 17:39:25 +0000552 if (((Value >> (ValSizeInBytes*8)) & ((1 << (8*ValSizeInBytes))-1)) !=
553 (Value & ((1 << (8*ValSizeInBytes))-1)))
Chris Lattner140a58f2006-04-08 06:46:53 +0000554 return SDOperand();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000555 }
556
557 // Properly sign extend the value.
558 int ShAmt = (4-ByteSize)*8;
559 int MaskVal = ((int)Value << ShAmt) >> ShAmt;
560
Evan Cheng5b6a01b2006-03-26 09:52:32 +0000561 // If this is zero, don't match, zero matches ISD::isBuildVectorAllZeros.
Chris Lattner140a58f2006-04-08 06:46:53 +0000562 if (MaskVal == 0) return SDOperand();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000563
Chris Lattner140a58f2006-04-08 06:46:53 +0000564 // Finally, if this value fits in a 5 bit sext field, return it
565 if (((MaskVal << (32-5)) >> (32-5)) == MaskVal)
566 return DAG.getTargetConstant(MaskVal, MVT::i32);
567 return SDOperand();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000568}
569
Chris Lattner1a635d62006-04-14 06:01:58 +0000570//===----------------------------------------------------------------------===//
571// LowerOperation implementation
572//===----------------------------------------------------------------------===//
573
574static SDOperand LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
575 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
576 Constant *C = CP->get();
577 SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i32, CP->getAlignment());
578 SDOperand Zero = DAG.getConstant(0, MVT::i32);
579
580 const TargetMachine &TM = DAG.getTarget();
581
582 // If this is a non-darwin platform, we don't support non-static relo models
583 // yet.
584 if (TM.getRelocationModel() == Reloc::Static ||
585 !TM.getSubtarget<PPCSubtarget>().isDarwin()) {
586 // Generate non-pic code that has direct accesses to the constant pool.
587 // The address of the global is just (hi(&g)+lo(&g)).
588 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
589 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
590 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
591 }
592
593 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
594 if (TM.getRelocationModel() == Reloc::PIC) {
595 // With PIC, the first instruction is actually "GR+hi(&G)".
596 Hi = DAG.getNode(ISD::ADD, MVT::i32,
597 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
598 }
599
600 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
601 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
602 return Lo;
603}
604
605static SDOperand LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
606 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
607 GlobalValue *GV = GSDN->getGlobal();
608 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32, GSDN->getOffset());
609 SDOperand Zero = DAG.getConstant(0, MVT::i32);
610
611 const TargetMachine &TM = DAG.getTarget();
612
613 // If this is a non-darwin platform, we don't support non-static relo models
614 // yet.
615 if (TM.getRelocationModel() == Reloc::Static ||
616 !TM.getSubtarget<PPCSubtarget>().isDarwin()) {
617 // Generate non-pic code that has direct accesses to globals.
618 // The address of the global is just (hi(&g)+lo(&g)).
619 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
620 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
621 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
622 }
623
624 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
625 if (TM.getRelocationModel() == Reloc::PIC) {
626 // With PIC, the first instruction is actually "GR+hi(&G)".
627 Hi = DAG.getNode(ISD::ADD, MVT::i32,
628 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
629 }
630
631 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
632 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
633
634 if (!GV->hasWeakLinkage() && !GV->hasLinkOnceLinkage() &&
635 (!GV->isExternal() || GV->hasNotBeenReadFromBytecode()))
636 return Lo;
637
638 // If the global is weak or external, we have to go through the lazy
639 // resolution stub.
640 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), Lo, DAG.getSrcValue(0));
641}
642
643static SDOperand LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
644 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
645
646 // If we're comparing for equality to zero, expose the fact that this is
647 // implented as a ctlz/srl pair on ppc, so that the dag combiner can
648 // fold the new nodes.
649 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
650 if (C->isNullValue() && CC == ISD::SETEQ) {
651 MVT::ValueType VT = Op.getOperand(0).getValueType();
652 SDOperand Zext = Op.getOperand(0);
653 if (VT < MVT::i32) {
654 VT = MVT::i32;
655 Zext = DAG.getNode(ISD::ZERO_EXTEND, VT, Op.getOperand(0));
656 }
657 unsigned Log2b = Log2_32(MVT::getSizeInBits(VT));
658 SDOperand Clz = DAG.getNode(ISD::CTLZ, VT, Zext);
659 SDOperand Scc = DAG.getNode(ISD::SRL, VT, Clz,
660 DAG.getConstant(Log2b, MVT::i32));
661 return DAG.getNode(ISD::TRUNCATE, MVT::i32, Scc);
662 }
663 // Leave comparisons against 0 and -1 alone for now, since they're usually
664 // optimized. FIXME: revisit this when we can custom lower all setcc
665 // optimizations.
666 if (C->isAllOnesValue() || C->isNullValue())
667 return SDOperand();
668 }
669
670 // If we have an integer seteq/setne, turn it into a compare against zero
671 // by subtracting the rhs from the lhs, which is faster than setting a
672 // condition register, reading it back out, and masking the correct bit.
673 MVT::ValueType LHSVT = Op.getOperand(0).getValueType();
674 if (MVT::isInteger(LHSVT) && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
675 MVT::ValueType VT = Op.getValueType();
676 SDOperand Sub = DAG.getNode(ISD::SUB, LHSVT, Op.getOperand(0),
677 Op.getOperand(1));
678 return DAG.getSetCC(VT, Sub, DAG.getConstant(0, LHSVT), CC);
679 }
680 return SDOperand();
681}
682
683static SDOperand LowerVASTART(SDOperand Op, SelectionDAG &DAG,
684 unsigned VarArgsFrameIndex) {
685 // vastart just stores the address of the VarArgsFrameIndex slot into the
686 // memory location argument.
687 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
688 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
689 Op.getOperand(1), Op.getOperand(2));
690}
691
692static SDOperand LowerRET(SDOperand Op, SelectionDAG &DAG) {
693 SDOperand Copy;
694 switch(Op.getNumOperands()) {
695 default:
696 assert(0 && "Do not know how to return this many arguments!");
697 abort();
698 case 1:
699 return SDOperand(); // ret void is legal
700 case 2: {
701 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
702 unsigned ArgReg;
703 if (MVT::isVector(ArgVT))
704 ArgReg = PPC::V2;
705 else if (MVT::isInteger(ArgVT))
706 ArgReg = PPC::R3;
707 else {
708 assert(MVT::isFloatingPoint(ArgVT));
709 ArgReg = PPC::F1;
710 }
711
712 Copy = DAG.getCopyToReg(Op.getOperand(0), ArgReg, Op.getOperand(1),
713 SDOperand());
714
715 // If we haven't noted the R3/F1 are live out, do so now.
716 if (DAG.getMachineFunction().liveout_empty())
717 DAG.getMachineFunction().addLiveOut(ArgReg);
718 break;
719 }
720 case 3:
721 Copy = DAG.getCopyToReg(Op.getOperand(0), PPC::R3, Op.getOperand(2),
722 SDOperand());
723 Copy = DAG.getCopyToReg(Copy, PPC::R4, Op.getOperand(1),Copy.getValue(1));
724 // If we haven't noted the R3+R4 are live out, do so now.
725 if (DAG.getMachineFunction().liveout_empty()) {
726 DAG.getMachineFunction().addLiveOut(PPC::R3);
727 DAG.getMachineFunction().addLiveOut(PPC::R4);
728 }
729 break;
730 }
731 return DAG.getNode(PPCISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
732}
733
734/// LowerSELECT_CC - Lower floating point select_cc's into fsel instruction when
735/// possible.
736static SDOperand LowerSELECT_CC(SDOperand Op, SelectionDAG &DAG) {
737 // Not FP? Not a fsel.
738 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
739 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
740 return SDOperand();
741
742 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
743
744 // Cannot handle SETEQ/SETNE.
745 if (CC == ISD::SETEQ || CC == ISD::SETNE) return SDOperand();
746
747 MVT::ValueType ResVT = Op.getValueType();
748 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
749 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
750 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
751
752 // If the RHS of the comparison is a 0.0, we don't need to do the
753 // subtraction at all.
754 if (isFloatingPointZero(RHS))
755 switch (CC) {
756 default: break; // SETUO etc aren't handled by fsel.
757 case ISD::SETULT:
758 case ISD::SETLT:
759 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
760 case ISD::SETUGE:
761 case ISD::SETGE:
762 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
763 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
764 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
765 case ISD::SETUGT:
766 case ISD::SETGT:
767 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
768 case ISD::SETULE:
769 case ISD::SETLE:
770 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
771 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
772 return DAG.getNode(PPCISD::FSEL, ResVT,
773 DAG.getNode(ISD::FNEG, MVT::f64, LHS), TV, FV);
774 }
775
776 SDOperand Cmp;
777 switch (CC) {
778 default: break; // SETUO etc aren't handled by fsel.
779 case ISD::SETULT:
780 case ISD::SETLT:
781 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
782 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
783 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
784 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
785 case ISD::SETUGE:
786 case ISD::SETGE:
787 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
788 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
789 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
790 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
791 case ISD::SETUGT:
792 case ISD::SETGT:
793 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
794 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
795 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
796 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
797 case ISD::SETULE:
798 case ISD::SETLE:
799 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
800 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
801 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
802 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
803 }
804 return SDOperand();
805}
806
807static SDOperand LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
808 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
809 SDOperand Src = Op.getOperand(0);
810 if (Src.getValueType() == MVT::f32)
811 Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src);
812
813 SDOperand Tmp;
814 switch (Op.getValueType()) {
815 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
816 case MVT::i32:
817 Tmp = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
818 break;
819 case MVT::i64:
820 Tmp = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
821 break;
822 }
823
824 // Convert the FP value to an int value through memory.
825 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Tmp);
826 if (Op.getValueType() == MVT::i32)
827 Bits = DAG.getNode(ISD::TRUNCATE, MVT::i32, Bits);
828 return Bits;
829}
830
831static SDOperand LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
832 if (Op.getOperand(0).getValueType() == MVT::i64) {
833 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, Op.getOperand(0));
834 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Bits);
835 if (Op.getValueType() == MVT::f32)
836 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
837 return FP;
838 }
839
840 assert(Op.getOperand(0).getValueType() == MVT::i32 &&
841 "Unhandled SINT_TO_FP type in custom expander!");
842 // Since we only generate this in 64-bit mode, we can take advantage of
843 // 64-bit registers. In particular, sign extend the input value into the
844 // 64-bit register with extsw, store the WHOLE 64-bit value into the stack
845 // then lfd it and fcfid it.
846 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
847 int FrameIdx = FrameInfo->CreateStackObject(8, 8);
848 SDOperand FIdx = DAG.getFrameIndex(FrameIdx, MVT::i32);
849
850 SDOperand Ext64 = DAG.getNode(PPCISD::EXTSW_32, MVT::i32,
851 Op.getOperand(0));
852
853 // STD the extended value into the stack slot.
854 SDOperand Store = DAG.getNode(PPCISD::STD_32, MVT::Other,
855 DAG.getEntryNode(), Ext64, FIdx,
856 DAG.getSrcValue(NULL));
857 // Load the value as a double.
858 SDOperand Ld = DAG.getLoad(MVT::f64, Store, FIdx, DAG.getSrcValue(NULL));
859
860 // FCFID it and return it.
861 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Ld);
862 if (Op.getValueType() == MVT::f32)
863 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
864 return FP;
865}
866
867static SDOperand LowerSHL(SDOperand Op, SelectionDAG &DAG) {
868 assert(Op.getValueType() == MVT::i64 &&
869 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
870 // The generic code does a fine job expanding shift by a constant.
871 if (isa<ConstantSDNode>(Op.getOperand(1))) return SDOperand();
872
873 // Otherwise, expand into a bunch of logical ops. Note that these ops
874 // depend on the PPC behavior for oversized shift amounts.
875 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
876 DAG.getConstant(0, MVT::i32));
877 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
878 DAG.getConstant(1, MVT::i32));
879 SDOperand Amt = Op.getOperand(1);
880
881 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
882 DAG.getConstant(32, MVT::i32), Amt);
883 SDOperand Tmp2 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Amt);
884 SDOperand Tmp3 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Tmp1);
885 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
886 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
887 DAG.getConstant(-32U, MVT::i32));
888 SDOperand Tmp6 = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Tmp5);
889 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
890 SDOperand OutLo = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Amt);
891 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
892}
893
894static SDOperand LowerSRL(SDOperand Op, SelectionDAG &DAG) {
895 assert(Op.getValueType() == MVT::i64 &&
896 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
897 // The generic code does a fine job expanding shift by a constant.
898 if (isa<ConstantSDNode>(Op.getOperand(1))) return SDOperand();
899
900 // Otherwise, expand into a bunch of logical ops. Note that these ops
901 // depend on the PPC behavior for oversized shift amounts.
902 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
903 DAG.getConstant(0, MVT::i32));
904 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
905 DAG.getConstant(1, MVT::i32));
906 SDOperand Amt = Op.getOperand(1);
907
908 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
909 DAG.getConstant(32, MVT::i32), Amt);
910 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
911 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
912 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
913 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
914 DAG.getConstant(-32U, MVT::i32));
915 SDOperand Tmp6 = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Tmp5);
916 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
917 SDOperand OutHi = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Amt);
918 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
919}
920
921static SDOperand LowerSRA(SDOperand Op, SelectionDAG &DAG) {
922 assert(Op.getValueType() == MVT::i64 &&
923 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
924 // The generic code does a fine job expanding shift by a constant.
925 if (isa<ConstantSDNode>(Op.getOperand(1))) return SDOperand();
926
927 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
928 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
929 DAG.getConstant(0, MVT::i32));
930 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
931 DAG.getConstant(1, MVT::i32));
932 SDOperand Amt = Op.getOperand(1);
933
934 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
935 DAG.getConstant(32, MVT::i32), Amt);
936 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
937 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
938 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
939 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
940 DAG.getConstant(-32U, MVT::i32));
941 SDOperand Tmp6 = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Tmp5);
942 SDOperand OutHi = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Amt);
943 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
944 Tmp4, Tmp6, ISD::SETLE);
945 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
946}
947
948//===----------------------------------------------------------------------===//
949// Vector related lowering.
950//
951
Chris Lattnerac225ca2006-04-12 19:07:14 +0000952// If this is a vector of constants or undefs, get the bits. A bit in
953// UndefBits is set if the corresponding element of the vector is an
954// ISD::UNDEF value. For undefs, the corresponding VectorBits values are
955// zero. Return true if this is not an array of constants, false if it is.
956//
Chris Lattnerac225ca2006-04-12 19:07:14 +0000957static bool GetConstantBuildVectorBits(SDNode *BV, uint64_t VectorBits[2],
958 uint64_t UndefBits[2]) {
959 // Start with zero'd results.
960 VectorBits[0] = VectorBits[1] = UndefBits[0] = UndefBits[1] = 0;
961
962 unsigned EltBitSize = MVT::getSizeInBits(BV->getOperand(0).getValueType());
963 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
964 SDOperand OpVal = BV->getOperand(i);
965
966 unsigned PartNo = i >= e/2; // In the upper 128 bits?
Chris Lattnerb17f1672006-04-16 01:01:29 +0000967 unsigned SlotNo = e/2 - (i & (e/2-1))-1; // Which subpiece of the uint64_t.
Chris Lattnerac225ca2006-04-12 19:07:14 +0000968
969 uint64_t EltBits = 0;
970 if (OpVal.getOpcode() == ISD::UNDEF) {
971 uint64_t EltUndefBits = ~0U >> (32-EltBitSize);
972 UndefBits[PartNo] |= EltUndefBits << (SlotNo*EltBitSize);
973 continue;
974 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
975 EltBits = CN->getValue() & (~0U >> (32-EltBitSize));
976 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) {
977 assert(CN->getValueType(0) == MVT::f32 &&
978 "Only one legal FP vector type!");
979 EltBits = FloatToBits(CN->getValue());
980 } else {
981 // Nonconstant element.
982 return true;
983 }
984
985 VectorBits[PartNo] |= EltBits << (SlotNo*EltBitSize);
986 }
987
988 //printf("%llx %llx %llx %llx\n",
989 // VectorBits[0], VectorBits[1], UndefBits[0], UndefBits[1]);
990 return false;
991}
Chris Lattneref819f82006-03-20 06:33:01 +0000992
Chris Lattnerb17f1672006-04-16 01:01:29 +0000993// If this is a splat (repetition) of a value across the whole vector, return
994// the smallest size that splats it. For example, "0x01010101010101..." is a
995// splat of 0x01, 0x0101, and 0x01010101. We return SplatBits = 0x01 and
996// SplatSize = 1 byte.
997static bool isConstantSplat(const uint64_t Bits128[2],
998 const uint64_t Undef128[2],
999 unsigned &SplatBits, unsigned &SplatUndef,
1000 unsigned &SplatSize) {
1001
1002 // Don't let undefs prevent splats from matching. See if the top 64-bits are
1003 // the same as the lower 64-bits, ignoring undefs.
1004 if ((Bits128[0] & ~Undef128[1]) != (Bits128[1] & ~Undef128[0]))
1005 return false; // Can't be a splat if two pieces don't match.
1006
1007 uint64_t Bits64 = Bits128[0] | Bits128[1];
1008 uint64_t Undef64 = Undef128[0] & Undef128[1];
1009
1010 // Check that the top 32-bits are the same as the lower 32-bits, ignoring
1011 // undefs.
1012 if ((Bits64 & (~Undef64 >> 32)) != ((Bits64 >> 32) & ~Undef64))
1013 return false; // Can't be a splat if two pieces don't match.
1014
1015 uint32_t Bits32 = uint32_t(Bits64) | uint32_t(Bits64 >> 32);
1016 uint32_t Undef32 = uint32_t(Undef64) & uint32_t(Undef64 >> 32);
1017
1018 // If the top 16-bits are different than the lower 16-bits, ignoring
1019 // undefs, we have an i32 splat.
1020 if ((Bits32 & (~Undef32 >> 16)) != ((Bits32 >> 16) & ~Undef32)) {
1021 SplatBits = Bits32;
1022 SplatUndef = Undef32;
1023 SplatSize = 4;
1024 return true;
1025 }
1026
1027 uint16_t Bits16 = uint16_t(Bits32) | uint16_t(Bits32 >> 16);
1028 uint16_t Undef16 = uint16_t(Undef32) & uint16_t(Undef32 >> 16);
1029
1030 // If the top 8-bits are different than the lower 8-bits, ignoring
1031 // undefs, we have an i16 splat.
1032 if ((Bits16 & (uint16_t(~Undef16) >> 8)) != ((Bits16 >> 8) & ~Undef16)) {
1033 SplatBits = Bits16;
1034 SplatUndef = Undef16;
1035 SplatSize = 2;
1036 return true;
1037 }
1038
1039 // Otherwise, we have an 8-bit splat.
1040 SplatBits = uint8_t(Bits16) | uint8_t(Bits16 >> 8);
1041 SplatUndef = uint8_t(Undef16) & uint8_t(Undef16 >> 8);
1042 SplatSize = 1;
1043 return true;
1044}
1045
Chris Lattner4a998b92006-04-17 06:00:21 +00001046/// BuildSplatI - Build a canonical splati of Val with an element size of
1047/// SplatSize. Cast the result to VT.
1048static SDOperand BuildSplatI(int Val, unsigned SplatSize, MVT::ValueType VT,
1049 SelectionDAG &DAG) {
1050 assert(Val >= -16 && Val <= 15 && "vsplti is out of range!");
Chris Lattner6876e662006-04-17 06:58:41 +00001051
1052 // Force vspltis[hw] -1 to vspltisb -1.
1053 if (Val == -1) SplatSize = 1;
1054
Chris Lattner4a998b92006-04-17 06:00:21 +00001055 static const MVT::ValueType VTys[] = { // canonical VT to use for each size.
1056 MVT::v16i8, MVT::v8i16, MVT::Other, MVT::v4i32
1057 };
1058 MVT::ValueType CanonicalVT = VTys[SplatSize-1];
1059
1060 // Build a canonical splat for this value.
1061 SDOperand Elt = DAG.getConstant(Val, MVT::getVectorBaseType(CanonicalVT));
1062 std::vector<SDOperand> Ops(MVT::getVectorNumElements(CanonicalVT), Elt);
1063 SDOperand Res = DAG.getNode(ISD::BUILD_VECTOR, CanonicalVT, Ops);
1064 return DAG.getNode(ISD::BIT_CONVERT, VT, Res);
1065}
1066
Chris Lattnere7c768e2006-04-18 03:24:30 +00001067/// BuildIntrinsicOp - Return a binary operator intrinsic node with the
Chris Lattner6876e662006-04-17 06:58:41 +00001068/// specified intrinsic ID.
Chris Lattnere7c768e2006-04-18 03:24:30 +00001069static SDOperand BuildIntrinsicOp(unsigned IID, SDOperand LHS, SDOperand RHS,
1070 SelectionDAG &DAG,
1071 MVT::ValueType DestVT = MVT::Other) {
1072 if (DestVT == MVT::Other) DestVT = LHS.getValueType();
1073 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DestVT,
Chris Lattner6876e662006-04-17 06:58:41 +00001074 DAG.getConstant(IID, MVT::i32), LHS, RHS);
1075}
1076
Chris Lattnere7c768e2006-04-18 03:24:30 +00001077/// BuildIntrinsicOp - Return a ternary operator intrinsic node with the
1078/// specified intrinsic ID.
1079static SDOperand BuildIntrinsicOp(unsigned IID, SDOperand Op0, SDOperand Op1,
1080 SDOperand Op2, SelectionDAG &DAG,
1081 MVT::ValueType DestVT = MVT::Other) {
1082 if (DestVT == MVT::Other) DestVT = Op0.getValueType();
1083 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DestVT,
1084 DAG.getConstant(IID, MVT::i32), Op0, Op1, Op2);
1085}
1086
1087
Chris Lattnerbdd558c2006-04-17 17:55:10 +00001088/// BuildVSLDOI - Return a VECTOR_SHUFFLE that is a vsldoi of the specified
1089/// amount. The result has the specified value type.
1090static SDOperand BuildVSLDOI(SDOperand LHS, SDOperand RHS, unsigned Amt,
1091 MVT::ValueType VT, SelectionDAG &DAG) {
1092 // Force LHS/RHS to be the right type.
1093 LHS = DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, LHS);
1094 RHS = DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, RHS);
1095
1096 std::vector<SDOperand> Ops;
1097 for (unsigned i = 0; i != 16; ++i)
1098 Ops.push_back(DAG.getConstant(i+Amt, MVT::i32));
1099 SDOperand T = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v16i8, LHS, RHS,
1100 DAG.getNode(ISD::BUILD_VECTOR, MVT::v16i8, Ops));
1101 return DAG.getNode(ISD::BIT_CONVERT, VT, T);
1102}
1103
Chris Lattnerf1b47082006-04-14 05:19:18 +00001104// If this is a case we can't handle, return null and let the default
1105// expansion code take care of it. If we CAN select this case, and if it
1106// selects to a single instruction, return Op. Otherwise, if we can codegen
1107// this case more efficiently than a constant pool load, lower it to the
1108// sequence of ops that should be used.
1109static SDOperand LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
1110 // If this is a vector of constants or undefs, get the bits. A bit in
1111 // UndefBits is set if the corresponding element of the vector is an
1112 // ISD::UNDEF value. For undefs, the corresponding VectorBits values are
1113 // zero.
1114 uint64_t VectorBits[2];
1115 uint64_t UndefBits[2];
1116 if (GetConstantBuildVectorBits(Op.Val, VectorBits, UndefBits))
1117 return SDOperand(); // Not a constant vector.
1118
Chris Lattnerb17f1672006-04-16 01:01:29 +00001119 // If this is a splat (repetition) of a value across the whole vector, return
1120 // the smallest size that splats it. For example, "0x01010101010101..." is a
1121 // splat of 0x01, 0x0101, and 0x01010101. We return SplatBits = 0x01 and
1122 // SplatSize = 1 byte.
1123 unsigned SplatBits, SplatUndef, SplatSize;
1124 if (isConstantSplat(VectorBits, UndefBits, SplatBits, SplatUndef, SplatSize)){
1125 bool HasAnyUndefs = (UndefBits[0] | UndefBits[1]) != 0;
1126
1127 // First, handle single instruction cases.
1128
1129 // All zeros?
1130 if (SplatBits == 0) {
1131 // Canonicalize all zero vectors to be v4i32.
1132 if (Op.getValueType() != MVT::v4i32 || HasAnyUndefs) {
1133 SDOperand Z = DAG.getConstant(0, MVT::i32);
1134 Z = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Z, Z, Z, Z);
1135 Op = DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Z);
1136 }
1137 return Op;
Chris Lattnerf1b47082006-04-14 05:19:18 +00001138 }
Chris Lattnerb17f1672006-04-16 01:01:29 +00001139
1140 // If the sign extended value is in the range [-16,15], use VSPLTI[bhw].
1141 int32_t SextVal= int32_t(SplatBits << (32-8*SplatSize)) >> (32-8*SplatSize);
Chris Lattner4a998b92006-04-17 06:00:21 +00001142 if (SextVal >= -16 && SextVal <= 15)
1143 return BuildSplatI(SextVal, SplatSize, Op.getValueType(), DAG);
Chris Lattnerb17f1672006-04-16 01:01:29 +00001144
Chris Lattnerdbce85d2006-04-17 18:09:22 +00001145
1146 // Two instruction sequences.
1147
Chris Lattner4a998b92006-04-17 06:00:21 +00001148 // If this value is in the range [-32,30] and is even, use:
1149 // tmp = VSPLTI[bhw], result = add tmp, tmp
1150 if (SextVal >= -32 && SextVal <= 30 && (SextVal & 1) == 0) {
1151 Op = BuildSplatI(SextVal >> 1, SplatSize, Op.getValueType(), DAG);
1152 return DAG.getNode(ISD::ADD, Op.getValueType(), Op, Op);
1153 }
Chris Lattner6876e662006-04-17 06:58:41 +00001154
1155 // If this is 0x8000_0000 x 4, turn into vspltisw + vslw. If it is
1156 // 0x7FFF_FFFF x 4, turn it into not(0x8000_0000). This is important
1157 // for fneg/fabs.
1158 if (SplatSize == 4 && SplatBits == (0x7FFFFFFF&~SplatUndef)) {
1159 // Make -1 and vspltisw -1:
1160 SDOperand OnesV = BuildSplatI(-1, 4, MVT::v4i32, DAG);
1161
1162 // Make the VSLW intrinsic, computing 0x8000_0000.
Chris Lattnere7c768e2006-04-18 03:24:30 +00001163 SDOperand Res = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, OnesV,
1164 OnesV, DAG);
Chris Lattner6876e662006-04-17 06:58:41 +00001165
1166 // xor by OnesV to invert it.
1167 Res = DAG.getNode(ISD::XOR, MVT::v4i32, Res, OnesV);
1168 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Res);
1169 }
1170
1171 // Check to see if this is a wide variety of vsplti*, binop self cases.
1172 unsigned SplatBitSize = SplatSize*8;
1173 static const char SplatCsts[] = {
1174 -1, 1, -2, 2, -3, 3, -4, 4, -5, 5, -6, 6, -7, 7,
Chris Lattnerdbce85d2006-04-17 18:09:22 +00001175 -8, 8, -9, 9, -10, 10, -11, 11, -12, 12, -13, 13, 14, -14, 15, -15, -16
Chris Lattner6876e662006-04-17 06:58:41 +00001176 };
1177 for (unsigned idx = 0; idx < sizeof(SplatCsts)/sizeof(SplatCsts[0]); ++idx){
1178 // Indirect through the SplatCsts array so that we favor 'vsplti -1' for
1179 // cases which are ambiguous (e.g. formation of 0x8000_0000). 'vsplti -1'
1180 int i = SplatCsts[idx];
1181
1182 // Figure out what shift amount will be used by altivec if shifted by i in
1183 // this splat size.
1184 unsigned TypeShiftAmt = i & (SplatBitSize-1);
1185
1186 // vsplti + shl self.
1187 if (SextVal == (i << (int)TypeShiftAmt)) {
1188 Op = BuildSplatI(i, SplatSize, Op.getValueType(), DAG);
1189 static const unsigned IIDs[] = { // Intrinsic to use for each size.
1190 Intrinsic::ppc_altivec_vslb, Intrinsic::ppc_altivec_vslh, 0,
1191 Intrinsic::ppc_altivec_vslw
1192 };
Chris Lattnere7c768e2006-04-18 03:24:30 +00001193 return BuildIntrinsicOp(IIDs[SplatSize-1], Op, Op, DAG);
Chris Lattner6876e662006-04-17 06:58:41 +00001194 }
1195
1196 // vsplti + srl self.
1197 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
1198 Op = BuildSplatI(i, SplatSize, Op.getValueType(), DAG);
1199 static const unsigned IIDs[] = { // Intrinsic to use for each size.
1200 Intrinsic::ppc_altivec_vsrb, Intrinsic::ppc_altivec_vsrh, 0,
1201 Intrinsic::ppc_altivec_vsrw
1202 };
Chris Lattnere7c768e2006-04-18 03:24:30 +00001203 return BuildIntrinsicOp(IIDs[SplatSize-1], Op, Op, DAG);
Chris Lattner6876e662006-04-17 06:58:41 +00001204 }
1205
1206 // vsplti + sra self.
1207 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
1208 Op = BuildSplatI(i, SplatSize, Op.getValueType(), DAG);
1209 static const unsigned IIDs[] = { // Intrinsic to use for each size.
1210 Intrinsic::ppc_altivec_vsrab, Intrinsic::ppc_altivec_vsrah, 0,
1211 Intrinsic::ppc_altivec_vsraw
1212 };
Chris Lattnere7c768e2006-04-18 03:24:30 +00001213 return BuildIntrinsicOp(IIDs[SplatSize-1], Op, Op, DAG);
Chris Lattner6876e662006-04-17 06:58:41 +00001214 }
1215
Chris Lattnerbdd558c2006-04-17 17:55:10 +00001216 // vsplti + rol self.
1217 if (SextVal == (int)(((unsigned)i << TypeShiftAmt) |
1218 ((unsigned)i >> (SplatBitSize-TypeShiftAmt)))) {
1219 Op = BuildSplatI(i, SplatSize, Op.getValueType(), DAG);
1220 static const unsigned IIDs[] = { // Intrinsic to use for each size.
1221 Intrinsic::ppc_altivec_vrlb, Intrinsic::ppc_altivec_vrlh, 0,
1222 Intrinsic::ppc_altivec_vrlw
1223 };
Chris Lattnere7c768e2006-04-18 03:24:30 +00001224 return BuildIntrinsicOp(IIDs[SplatSize-1], Op, Op, DAG);
Chris Lattnerbdd558c2006-04-17 17:55:10 +00001225 }
1226
1227 // t = vsplti c, result = vsldoi t, t, 1
1228 if (SextVal == ((i << 8) | (i >> (TypeShiftAmt-8)))) {
1229 SDOperand T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG);
1230 return BuildVSLDOI(T, T, 1, Op.getValueType(), DAG);
1231 }
1232 // t = vsplti c, result = vsldoi t, t, 2
1233 if (SextVal == ((i << 16) | (i >> (TypeShiftAmt-16)))) {
1234 SDOperand T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG);
1235 return BuildVSLDOI(T, T, 2, Op.getValueType(), DAG);
1236 }
1237 // t = vsplti c, result = vsldoi t, t, 3
1238 if (SextVal == ((i << 24) | (i >> (TypeShiftAmt-24)))) {
1239 SDOperand T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG);
1240 return BuildVSLDOI(T, T, 3, Op.getValueType(), DAG);
1241 }
Chris Lattner6876e662006-04-17 06:58:41 +00001242 }
1243
Chris Lattner6876e662006-04-17 06:58:41 +00001244 // Three instruction sequences.
1245
Chris Lattnerdbce85d2006-04-17 18:09:22 +00001246 // Odd, in range [17,31]: (vsplti C)-(vsplti -16).
1247 if (SextVal >= 0 && SextVal <= 31) {
1248 SDOperand LHS = BuildSplatI(SextVal-16, SplatSize, Op.getValueType(),DAG);
1249 SDOperand RHS = BuildSplatI(-16, SplatSize, Op.getValueType(), DAG);
1250 return DAG.getNode(ISD::SUB, Op.getValueType(), LHS, RHS);
1251 }
1252 // Odd, in range [-31,-17]: (vsplti C)+(vsplti -16).
1253 if (SextVal >= -31 && SextVal <= 0) {
1254 SDOperand LHS = BuildSplatI(SextVal+16, SplatSize, Op.getValueType(),DAG);
1255 SDOperand RHS = BuildSplatI(-16, SplatSize, Op.getValueType(), DAG);
Chris Lattnerc4083822006-04-17 06:07:44 +00001256 return DAG.getNode(ISD::ADD, Op.getValueType(), LHS, RHS);
Chris Lattnerf1b47082006-04-14 05:19:18 +00001257 }
1258 }
Chris Lattnerb17f1672006-04-16 01:01:29 +00001259
Chris Lattnerf1b47082006-04-14 05:19:18 +00001260 return SDOperand();
1261}
1262
Chris Lattner59138102006-04-17 05:28:54 +00001263/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
1264/// the specified operations to build the shuffle.
1265static SDOperand GeneratePerfectShuffle(unsigned PFEntry, SDOperand LHS,
1266 SDOperand RHS, SelectionDAG &DAG) {
1267 unsigned OpNum = (PFEntry >> 26) & 0x0F;
1268 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
1269 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
1270
1271 enum {
1272 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
1273 OP_VMRGHW,
1274 OP_VMRGLW,
1275 OP_VSPLTISW0,
1276 OP_VSPLTISW1,
1277 OP_VSPLTISW2,
1278 OP_VSPLTISW3,
1279 OP_VSLDOI4,
1280 OP_VSLDOI8,
1281 OP_VSLDOI12,
1282 };
1283
1284 if (OpNum == OP_COPY) {
1285 if (LHSID == (1*9+2)*9+3) return LHS;
1286 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
1287 return RHS;
1288 }
1289
Chris Lattnerbdd558c2006-04-17 17:55:10 +00001290 SDOperand OpLHS, OpRHS;
1291 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG);
1292 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG);
1293
Chris Lattner59138102006-04-17 05:28:54 +00001294 unsigned ShufIdxs[16];
1295 switch (OpNum) {
1296 default: assert(0 && "Unknown i32 permute!");
1297 case OP_VMRGHW:
1298 ShufIdxs[ 0] = 0; ShufIdxs[ 1] = 1; ShufIdxs[ 2] = 2; ShufIdxs[ 3] = 3;
1299 ShufIdxs[ 4] = 16; ShufIdxs[ 5] = 17; ShufIdxs[ 6] = 18; ShufIdxs[ 7] = 19;
1300 ShufIdxs[ 8] = 4; ShufIdxs[ 9] = 5; ShufIdxs[10] = 6; ShufIdxs[11] = 7;
1301 ShufIdxs[12] = 20; ShufIdxs[13] = 21; ShufIdxs[14] = 22; ShufIdxs[15] = 23;
1302 break;
1303 case OP_VMRGLW:
1304 ShufIdxs[ 0] = 8; ShufIdxs[ 1] = 9; ShufIdxs[ 2] = 10; ShufIdxs[ 3] = 11;
1305 ShufIdxs[ 4] = 24; ShufIdxs[ 5] = 25; ShufIdxs[ 6] = 26; ShufIdxs[ 7] = 27;
1306 ShufIdxs[ 8] = 12; ShufIdxs[ 9] = 13; ShufIdxs[10] = 14; ShufIdxs[11] = 15;
1307 ShufIdxs[12] = 28; ShufIdxs[13] = 29; ShufIdxs[14] = 30; ShufIdxs[15] = 31;
1308 break;
1309 case OP_VSPLTISW0:
1310 for (unsigned i = 0; i != 16; ++i)
1311 ShufIdxs[i] = (i&3)+0;
1312 break;
1313 case OP_VSPLTISW1:
1314 for (unsigned i = 0; i != 16; ++i)
1315 ShufIdxs[i] = (i&3)+4;
1316 break;
1317 case OP_VSPLTISW2:
1318 for (unsigned i = 0; i != 16; ++i)
1319 ShufIdxs[i] = (i&3)+8;
1320 break;
1321 case OP_VSPLTISW3:
1322 for (unsigned i = 0; i != 16; ++i)
1323 ShufIdxs[i] = (i&3)+12;
1324 break;
1325 case OP_VSLDOI4:
Chris Lattnerbdd558c2006-04-17 17:55:10 +00001326 return BuildVSLDOI(OpLHS, OpRHS, 4, OpLHS.getValueType(), DAG);
Chris Lattner59138102006-04-17 05:28:54 +00001327 case OP_VSLDOI8:
Chris Lattnerbdd558c2006-04-17 17:55:10 +00001328 return BuildVSLDOI(OpLHS, OpRHS, 8, OpLHS.getValueType(), DAG);
Chris Lattner59138102006-04-17 05:28:54 +00001329 case OP_VSLDOI12:
Chris Lattnerbdd558c2006-04-17 17:55:10 +00001330 return BuildVSLDOI(OpLHS, OpRHS, 12, OpLHS.getValueType(), DAG);
Chris Lattner59138102006-04-17 05:28:54 +00001331 }
1332 std::vector<SDOperand> Ops;
1333 for (unsigned i = 0; i != 16; ++i)
1334 Ops.push_back(DAG.getConstant(ShufIdxs[i], MVT::i32));
Chris Lattner59138102006-04-17 05:28:54 +00001335
1336 return DAG.getNode(ISD::VECTOR_SHUFFLE, OpLHS.getValueType(), OpLHS, OpRHS,
1337 DAG.getNode(ISD::BUILD_VECTOR, MVT::v16i8, Ops));
1338}
1339
Chris Lattnerf1b47082006-04-14 05:19:18 +00001340/// LowerVECTOR_SHUFFLE - Return the code we lower for VECTOR_SHUFFLE. If this
1341/// is a shuffle we can handle in a single instruction, return it. Otherwise,
1342/// return the code it can be lowered into. Worst case, it can always be
1343/// lowered into a vperm.
1344static SDOperand LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
1345 SDOperand V1 = Op.getOperand(0);
1346 SDOperand V2 = Op.getOperand(1);
1347 SDOperand PermMask = Op.getOperand(2);
1348
1349 // Cases that are handled by instructions that take permute immediates
1350 // (such as vsplt*) should be left as VECTOR_SHUFFLE nodes so they can be
1351 // selected by the instruction selector.
1352 if (V2.getOpcode() == ISD::UNDEF) {
1353 if (PPC::isSplatShuffleMask(PermMask.Val, 1) ||
1354 PPC::isSplatShuffleMask(PermMask.Val, 2) ||
1355 PPC::isSplatShuffleMask(PermMask.Val, 4) ||
1356 PPC::isVPKUWUMShuffleMask(PermMask.Val, true) ||
1357 PPC::isVPKUHUMShuffleMask(PermMask.Val, true) ||
1358 PPC::isVSLDOIShuffleMask(PermMask.Val, true) != -1 ||
1359 PPC::isVMRGLShuffleMask(PermMask.Val, 1, true) ||
1360 PPC::isVMRGLShuffleMask(PermMask.Val, 2, true) ||
1361 PPC::isVMRGLShuffleMask(PermMask.Val, 4, true) ||
1362 PPC::isVMRGHShuffleMask(PermMask.Val, 1, true) ||
1363 PPC::isVMRGHShuffleMask(PermMask.Val, 2, true) ||
1364 PPC::isVMRGHShuffleMask(PermMask.Val, 4, true)) {
1365 return Op;
1366 }
1367 }
1368
1369 // Altivec has a variety of "shuffle immediates" that take two vector inputs
1370 // and produce a fixed permutation. If any of these match, do not lower to
1371 // VPERM.
1372 if (PPC::isVPKUWUMShuffleMask(PermMask.Val, false) ||
1373 PPC::isVPKUHUMShuffleMask(PermMask.Val, false) ||
1374 PPC::isVSLDOIShuffleMask(PermMask.Val, false) != -1 ||
1375 PPC::isVMRGLShuffleMask(PermMask.Val, 1, false) ||
1376 PPC::isVMRGLShuffleMask(PermMask.Val, 2, false) ||
1377 PPC::isVMRGLShuffleMask(PermMask.Val, 4, false) ||
1378 PPC::isVMRGHShuffleMask(PermMask.Val, 1, false) ||
1379 PPC::isVMRGHShuffleMask(PermMask.Val, 2, false) ||
1380 PPC::isVMRGHShuffleMask(PermMask.Val, 4, false))
1381 return Op;
1382
Chris Lattner59138102006-04-17 05:28:54 +00001383 // Check to see if this is a shuffle of 4-byte values. If so, we can use our
1384 // perfect shuffle table to emit an optimal matching sequence.
1385 unsigned PFIndexes[4];
1386 bool isFourElementShuffle = true;
1387 for (unsigned i = 0; i != 4 && isFourElementShuffle; ++i) { // Element number
1388 unsigned EltNo = 8; // Start out undef.
1389 for (unsigned j = 0; j != 4; ++j) { // Intra-element byte.
1390 if (PermMask.getOperand(i*4+j).getOpcode() == ISD::UNDEF)
1391 continue; // Undef, ignore it.
1392
1393 unsigned ByteSource =
1394 cast<ConstantSDNode>(PermMask.getOperand(i*4+j))->getValue();
1395 if ((ByteSource & 3) != j) {
1396 isFourElementShuffle = false;
1397 break;
1398 }
1399
1400 if (EltNo == 8) {
1401 EltNo = ByteSource/4;
1402 } else if (EltNo != ByteSource/4) {
1403 isFourElementShuffle = false;
1404 break;
1405 }
1406 }
1407 PFIndexes[i] = EltNo;
1408 }
1409
1410 // If this shuffle can be expressed as a shuffle of 4-byte elements, use the
1411 // perfect shuffle vector to determine if it is cost effective to do this as
1412 // discrete instructions, or whether we should use a vperm.
1413 if (isFourElementShuffle) {
1414 // Compute the index in the perfect shuffle table.
1415 unsigned PFTableIndex =
1416 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
1417
1418 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
1419 unsigned Cost = (PFEntry >> 30);
1420
1421 // Determining when to avoid vperm is tricky. Many things affect the cost
1422 // of vperm, particularly how many times the perm mask needs to be computed.
1423 // For example, if the perm mask can be hoisted out of a loop or is already
1424 // used (perhaps because there are multiple permutes with the same shuffle
1425 // mask?) the vperm has a cost of 1. OTOH, hoisting the permute mask out of
1426 // the loop requires an extra register.
1427 //
1428 // As a compromise, we only emit discrete instructions if the shuffle can be
1429 // generated in 3 or fewer operations. When we have loop information
1430 // available, if this block is within a loop, we should avoid using vperm
1431 // for 3-operation perms and use a constant pool load instead.
1432 if (Cost < 3)
1433 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG);
1434 }
Chris Lattnerf1b47082006-04-14 05:19:18 +00001435
1436 // Lower this to a VPERM(V1, V2, V3) expression, where V3 is a constant
1437 // vector that will get spilled to the constant pool.
1438 if (V2.getOpcode() == ISD::UNDEF) V2 = V1;
1439
1440 // The SHUFFLE_VECTOR mask is almost exactly what we want for vperm, except
1441 // that it is in input element units, not in bytes. Convert now.
1442 MVT::ValueType EltVT = MVT::getVectorBaseType(V1.getValueType());
1443 unsigned BytesPerElement = MVT::getSizeInBits(EltVT)/8;
1444
1445 std::vector<SDOperand> ResultMask;
1446 for (unsigned i = 0, e = PermMask.getNumOperands(); i != e; ++i) {
Chris Lattner730b4562006-04-15 23:48:05 +00001447 unsigned SrcElt;
1448 if (PermMask.getOperand(i).getOpcode() == ISD::UNDEF)
1449 SrcElt = 0;
1450 else
1451 SrcElt = cast<ConstantSDNode>(PermMask.getOperand(i))->getValue();
Chris Lattnerf1b47082006-04-14 05:19:18 +00001452
1453 for (unsigned j = 0; j != BytesPerElement; ++j)
1454 ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement+j,
1455 MVT::i8));
1456 }
1457
1458 SDOperand VPermMask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v16i8, ResultMask);
1459 return DAG.getNode(PPCISD::VPERM, V1.getValueType(), V1, V2, VPermMask);
1460}
1461
Chris Lattner1a635d62006-04-14 06:01:58 +00001462/// LowerINTRINSIC_WO_CHAIN - If this is an intrinsic that we want to custom
1463/// lower, do it, otherwise return null.
1464static SDOperand LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
1465 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
1466
1467 // If this is a lowered altivec predicate compare, CompareOpc is set to the
1468 // opcode number of the comparison.
1469 int CompareOpc = -1;
1470 bool isDot = false;
1471 switch (IntNo) {
1472 default: return SDOperand(); // Don't custom lower most intrinsics.
1473 // Comparison predicates.
1474 case Intrinsic::ppc_altivec_vcmpbfp_p: CompareOpc = 966; isDot = 1; break;
1475 case Intrinsic::ppc_altivec_vcmpeqfp_p: CompareOpc = 198; isDot = 1; break;
1476 case Intrinsic::ppc_altivec_vcmpequb_p: CompareOpc = 6; isDot = 1; break;
1477 case Intrinsic::ppc_altivec_vcmpequh_p: CompareOpc = 70; isDot = 1; break;
1478 case Intrinsic::ppc_altivec_vcmpequw_p: CompareOpc = 134; isDot = 1; break;
1479 case Intrinsic::ppc_altivec_vcmpgefp_p: CompareOpc = 454; isDot = 1; break;
1480 case Intrinsic::ppc_altivec_vcmpgtfp_p: CompareOpc = 710; isDot = 1; break;
1481 case Intrinsic::ppc_altivec_vcmpgtsb_p: CompareOpc = 774; isDot = 1; break;
1482 case Intrinsic::ppc_altivec_vcmpgtsh_p: CompareOpc = 838; isDot = 1; break;
1483 case Intrinsic::ppc_altivec_vcmpgtsw_p: CompareOpc = 902; isDot = 1; break;
1484 case Intrinsic::ppc_altivec_vcmpgtub_p: CompareOpc = 518; isDot = 1; break;
1485 case Intrinsic::ppc_altivec_vcmpgtuh_p: CompareOpc = 582; isDot = 1; break;
1486 case Intrinsic::ppc_altivec_vcmpgtuw_p: CompareOpc = 646; isDot = 1; break;
1487
1488 // Normal Comparisons.
1489 case Intrinsic::ppc_altivec_vcmpbfp: CompareOpc = 966; isDot = 0; break;
1490 case Intrinsic::ppc_altivec_vcmpeqfp: CompareOpc = 198; isDot = 0; break;
1491 case Intrinsic::ppc_altivec_vcmpequb: CompareOpc = 6; isDot = 0; break;
1492 case Intrinsic::ppc_altivec_vcmpequh: CompareOpc = 70; isDot = 0; break;
1493 case Intrinsic::ppc_altivec_vcmpequw: CompareOpc = 134; isDot = 0; break;
1494 case Intrinsic::ppc_altivec_vcmpgefp: CompareOpc = 454; isDot = 0; break;
1495 case Intrinsic::ppc_altivec_vcmpgtfp: CompareOpc = 710; isDot = 0; break;
1496 case Intrinsic::ppc_altivec_vcmpgtsb: CompareOpc = 774; isDot = 0; break;
1497 case Intrinsic::ppc_altivec_vcmpgtsh: CompareOpc = 838; isDot = 0; break;
1498 case Intrinsic::ppc_altivec_vcmpgtsw: CompareOpc = 902; isDot = 0; break;
1499 case Intrinsic::ppc_altivec_vcmpgtub: CompareOpc = 518; isDot = 0; break;
1500 case Intrinsic::ppc_altivec_vcmpgtuh: CompareOpc = 582; isDot = 0; break;
1501 case Intrinsic::ppc_altivec_vcmpgtuw: CompareOpc = 646; isDot = 0; break;
1502 }
1503
1504 assert(CompareOpc>0 && "We only lower altivec predicate compares so far!");
1505
1506 // If this is a non-dot comparison, make the VCMP node.
1507 if (!isDot) {
1508 SDOperand Tmp = DAG.getNode(PPCISD::VCMP, Op.getOperand(2).getValueType(),
1509 Op.getOperand(1), Op.getOperand(2),
1510 DAG.getConstant(CompareOpc, MVT::i32));
1511 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Tmp);
1512 }
1513
1514 // Create the PPCISD altivec 'dot' comparison node.
1515 std::vector<SDOperand> Ops;
1516 std::vector<MVT::ValueType> VTs;
1517 Ops.push_back(Op.getOperand(2)); // LHS
1518 Ops.push_back(Op.getOperand(3)); // RHS
1519 Ops.push_back(DAG.getConstant(CompareOpc, MVT::i32));
1520 VTs.push_back(Op.getOperand(2).getValueType());
1521 VTs.push_back(MVT::Flag);
1522 SDOperand CompNode = DAG.getNode(PPCISD::VCMPo, VTs, Ops);
1523
1524 // Now that we have the comparison, emit a copy from the CR to a GPR.
1525 // This is flagged to the above dot comparison.
1526 SDOperand Flags = DAG.getNode(PPCISD::MFCR, MVT::i32,
1527 DAG.getRegister(PPC::CR6, MVT::i32),
1528 CompNode.getValue(1));
1529
1530 // Unpack the result based on how the target uses it.
1531 unsigned BitNo; // Bit # of CR6.
1532 bool InvertBit; // Invert result?
1533 switch (cast<ConstantSDNode>(Op.getOperand(1))->getValue()) {
1534 default: // Can't happen, don't crash on invalid number though.
1535 case 0: // Return the value of the EQ bit of CR6.
1536 BitNo = 0; InvertBit = false;
1537 break;
1538 case 1: // Return the inverted value of the EQ bit of CR6.
1539 BitNo = 0; InvertBit = true;
1540 break;
1541 case 2: // Return the value of the LT bit of CR6.
1542 BitNo = 2; InvertBit = false;
1543 break;
1544 case 3: // Return the inverted value of the LT bit of CR6.
1545 BitNo = 2; InvertBit = true;
1546 break;
1547 }
1548
1549 // Shift the bit into the low position.
1550 Flags = DAG.getNode(ISD::SRL, MVT::i32, Flags,
1551 DAG.getConstant(8-(3-BitNo), MVT::i32));
1552 // Isolate the bit.
1553 Flags = DAG.getNode(ISD::AND, MVT::i32, Flags,
1554 DAG.getConstant(1, MVT::i32));
1555
1556 // If we are supposed to, toggle the bit.
1557 if (InvertBit)
1558 Flags = DAG.getNode(ISD::XOR, MVT::i32, Flags,
1559 DAG.getConstant(1, MVT::i32));
1560 return Flags;
1561}
1562
1563static SDOperand LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
1564 // Create a stack slot that is 16-byte aligned.
1565 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
1566 int FrameIdx = FrameInfo->CreateStackObject(16, 16);
1567 SDOperand FIdx = DAG.getFrameIndex(FrameIdx, MVT::i32);
1568
1569 // Store the input value into Value#0 of the stack slot.
1570 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
1571 Op.getOperand(0), FIdx,DAG.getSrcValue(NULL));
1572 // Load it out.
1573 return DAG.getLoad(Op.getValueType(), Store, FIdx, DAG.getSrcValue(NULL));
1574}
1575
Chris Lattnere7c768e2006-04-18 03:24:30 +00001576static SDOperand LowerMUL(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner72dd9bd2006-04-18 03:43:48 +00001577 if (Op.getValueType() == MVT::v4i32) {
1578 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
1579
1580 SDOperand Zero = BuildSplatI( 0, 1, MVT::v4i32, DAG);
1581 SDOperand Neg16 = BuildSplatI(-16, 4, MVT::v4i32, DAG); // +16 as shift amt.
1582
1583 SDOperand RHSSwap = // = vrlw RHS, 16
1584 BuildIntrinsicOp(Intrinsic::ppc_altivec_vrlw, RHS, Neg16, DAG);
1585
1586 // Shrinkify inputs to v8i16.
1587 LHS = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, LHS);
1588 RHS = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, RHS);
1589 RHSSwap = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, RHSSwap);
1590
1591 // Low parts multiplied together, generating 32-bit results (we ignore the
1592 // top parts).
1593 SDOperand LoProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmulouh,
1594 LHS, RHS, DAG, MVT::v4i32);
1595
1596 SDOperand HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmsumuhm,
1597 LHS, RHSSwap, Zero, DAG, MVT::v4i32);
1598 // Shift the high parts up 16 bits.
1599 HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, HiProd, Neg16, DAG);
1600 return DAG.getNode(ISD::ADD, MVT::v4i32, LoProd, HiProd);
1601 } else if (Op.getValueType() == MVT::v8i16) {
1602 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
1603
1604 // Multiply the even 16-parts, producing 32-bit sums.
1605 SDOperand EvenParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuleuh,
1606 LHS, RHS, DAG, MVT::v4i32);
1607 EvenParts = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, EvenParts);
1608
1609 // Multiply the odd 16-parts, producing 32-bit sums.
1610 SDOperand OddParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmulouh,
1611 LHS, RHS, DAG, MVT::v4i32);
1612 OddParts = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, OddParts);
1613
1614 // Merge the results together.
1615 std::vector<SDOperand> Ops;
1616 for (unsigned i = 0; i != 4; ++i) {
1617 Ops.push_back(DAG.getConstant(2*i+1, MVT::i16));
1618 Ops.push_back(DAG.getConstant(2*i+1+8, MVT::i16));
1619 }
1620
1621 return DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, EvenParts, OddParts,
1622 DAG.getNode(ISD::BUILD_VECTOR, MVT::v8i16, Ops));
1623 } else {
1624 assert(0 && "Unknown mul to lower!");
1625 abort();
1626 }
Chris Lattnere7c768e2006-04-18 03:24:30 +00001627}
1628
Chris Lattnere4bc9ea2005-08-26 00:52:45 +00001629/// LowerOperation - Provide custom lowering hooks for some operations.
1630///
Nate Begeman21e463b2005-10-16 05:39:50 +00001631SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +00001632 switch (Op.getOpcode()) {
1633 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattner1a635d62006-04-14 06:01:58 +00001634 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
1635 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
1636 case ISD::SETCC: return LowerSETCC(Op, DAG);
1637 case ISD::VASTART: return LowerVASTART(Op, DAG, VarArgsFrameIndex);
1638 case ISD::RET: return LowerRET(Op, DAG);
Chris Lattner7c0d6642005-10-02 06:37:13 +00001639
Chris Lattner1a635d62006-04-14 06:01:58 +00001640 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
1641 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
1642 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00001643
Chris Lattner1a635d62006-04-14 06:01:58 +00001644 // Lower 64-bit shifts.
1645 case ISD::SHL: return LowerSHL(Op, DAG);
1646 case ISD::SRL: return LowerSRL(Op, DAG);
1647 case ISD::SRA: return LowerSRA(Op, DAG);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00001648
Chris Lattner1a635d62006-04-14 06:01:58 +00001649 // Vector-related lowering.
1650 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
1651 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
1652 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
1653 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
Chris Lattnere7c768e2006-04-18 03:24:30 +00001654 case ISD::MUL: return LowerMUL(Op, DAG);
Chris Lattnerbc11c342005-08-31 20:23:54 +00001655 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +00001656 return SDOperand();
1657}
1658
Chris Lattner1a635d62006-04-14 06:01:58 +00001659//===----------------------------------------------------------------------===//
1660// Other Lowering Code
1661//===----------------------------------------------------------------------===//
1662
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001663std::vector<SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +00001664PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001665 //
1666 // add beautiful description of PPC stack frame format, or at least some docs
1667 //
1668 MachineFunction &MF = DAG.getMachineFunction();
1669 MachineFrameInfo *MFI = MF.getFrameInfo();
1670 MachineBasicBlock& BB = MF.front();
Chris Lattner7b738342005-09-13 19:33:40 +00001671 SSARegMap *RegMap = MF.getSSARegMap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001672 std::vector<SDOperand> ArgValues;
1673
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001674 unsigned ArgOffset = 24;
1675 unsigned GPR_remaining = 8;
1676 unsigned FPR_remaining = 13;
1677 unsigned GPR_idx = 0, FPR_idx = 0;
1678 static const unsigned GPR[] = {
1679 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1680 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1681 };
1682 static const unsigned FPR[] = {
1683 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1684 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1685 };
1686
1687 // Add DAG nodes to load the arguments... On entry to a function on PPC,
1688 // the arguments start at offset 24, although they are likely to be passed
1689 // in registers.
1690 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
1691 SDOperand newroot, argt;
1692 unsigned ObjSize;
1693 bool needsLoad = false;
1694 bool ArgLive = !I->use_empty();
1695 MVT::ValueType ObjectVT = getValueType(I->getType());
1696
1697 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +00001698 default: assert(0 && "Unhandled argument type!");
1699 case MVT::i1:
1700 case MVT::i8:
1701 case MVT::i16:
1702 case MVT::i32:
1703 ObjSize = 4;
1704 if (!ArgLive) break;
1705 if (GPR_remaining > 0) {
Nate Begeman1d9d7422005-10-18 00:28:58 +00001706 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +00001707 MF.addLiveIn(GPR[GPR_idx], VReg);
1708 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +00001709 if (ObjectVT != MVT::i32) {
1710 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
1711 : ISD::AssertZext;
1712 argt = DAG.getNode(AssertOp, MVT::i32, argt,
1713 DAG.getValueType(ObjectVT));
1714 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
1715 }
Chris Lattner915fb302005-08-30 00:19:00 +00001716 } else {
1717 needsLoad = true;
1718 }
1719 break;
Chris Lattner80720a92005-11-30 20:40:54 +00001720 case MVT::i64:
1721 ObjSize = 8;
Chris Lattner915fb302005-08-30 00:19:00 +00001722 if (!ArgLive) break;
1723 if (GPR_remaining > 0) {
1724 SDOperand argHi, argLo;
Nate Begeman1d9d7422005-10-18 00:28:58 +00001725 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +00001726 MF.addLiveIn(GPR[GPR_idx], VReg);
1727 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +00001728 // If we have two or more remaining argument registers, then both halves
1729 // of the i64 can be sourced from there. Otherwise, the lower half will
1730 // have to come off the stack. This can happen when an i64 is preceded
1731 // by 28 bytes of arguments.
1732 if (GPR_remaining > 1) {
Nate Begeman1d9d7422005-10-18 00:28:58 +00001733 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +00001734 MF.addLiveIn(GPR[GPR_idx+1], VReg);
1735 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +00001736 } else {
1737 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
1738 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
1739 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
1740 DAG.getSrcValue(NULL));
1741 }
1742 // Build the outgoing arg thingy
1743 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
1744 newroot = argLo;
1745 } else {
1746 needsLoad = true;
1747 }
1748 break;
1749 case MVT::f32:
1750 case MVT::f64:
1751 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
Chris Lattner413b9792006-01-11 18:21:25 +00001752 if (!ArgLive) {
1753 if (FPR_remaining > 0) {
1754 --FPR_remaining;
1755 ++FPR_idx;
1756 }
1757 break;
1758 }
Chris Lattner915fb302005-08-30 00:19:00 +00001759 if (FPR_remaining > 0) {
Chris Lattner919c0322005-10-01 01:35:02 +00001760 unsigned VReg;
1761 if (ObjectVT == MVT::f32)
Nate Begeman1d9d7422005-10-18 00:28:58 +00001762 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner919c0322005-10-01 01:35:02 +00001763 else
Nate Begeman1d9d7422005-10-18 00:28:58 +00001764 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +00001765 MF.addLiveIn(FPR[FPR_idx], VReg);
1766 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
Chris Lattner915fb302005-08-30 00:19:00 +00001767 --FPR_remaining;
1768 ++FPR_idx;
1769 } else {
1770 needsLoad = true;
1771 }
1772 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001773 }
1774
1775 // We need to load the argument to a virtual register if we determined above
1776 // that we ran out of physical registers of the appropriate type
1777 if (needsLoad) {
1778 unsigned SubregOffset = 0;
1779 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
1780 if (ObjectVT == MVT::i16) SubregOffset = 2;
1781 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
1782 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
1783 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
1784 DAG.getConstant(SubregOffset, MVT::i32));
1785 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
1786 DAG.getSrcValue(NULL));
1787 }
1788
1789 // Every 4 bytes of argument space consumes one of the GPRs available for
1790 // argument passing.
1791 if (GPR_remaining > 0) {
1792 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
1793 GPR_remaining -= delta;
1794 GPR_idx += delta;
1795 }
1796 ArgOffset += ObjSize;
1797 if (newroot.Val)
1798 DAG.setRoot(newroot.getValue(1));
1799
1800 ArgValues.push_back(argt);
1801 }
1802
1803 // If the function takes variable number of arguments, make a frame index for
1804 // the start of the first vararg value... for expansion of llvm.va_start.
1805 if (F.isVarArg()) {
1806 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
1807 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
1808 // If this function is vararg, store any remaining integer argument regs
1809 // to their spots on the stack so that they may be loaded by deferencing the
1810 // result of va_next.
1811 std::vector<SDOperand> MemOps;
1812 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begeman1d9d7422005-10-18 00:28:58 +00001813 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +00001814 MF.addLiveIn(GPR[GPR_idx], VReg);
1815 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001816 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
1817 Val, FIN, DAG.getSrcValue(NULL));
1818 MemOps.push_back(Store);
1819 // Increment the address by four for the next argument to store
1820 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
1821 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
1822 }
Chris Lattner80720a92005-11-30 20:40:54 +00001823 if (!MemOps.empty()) {
1824 MemOps.push_back(DAG.getRoot());
1825 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
1826 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001827 }
1828
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001829 return ArgValues;
1830}
1831
1832std::pair<SDOperand, SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +00001833PPCTargetLowering::LowerCallTo(SDOperand Chain,
1834 const Type *RetTy, bool isVarArg,
1835 unsigned CallingConv, bool isTailCall,
1836 SDOperand Callee, ArgListTy &Args,
1837 SelectionDAG &DAG) {
Chris Lattner281b55e2006-01-27 23:34:02 +00001838 // args_to_use will accumulate outgoing args for the PPCISD::CALL case in
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001839 // SelectExpr to use to put the arguments in the appropriate registers.
1840 std::vector<SDOperand> args_to_use;
1841
1842 // Count how many bytes are to be pushed on the stack, including the linkage
1843 // area, and parameter passing area.
1844 unsigned NumBytes = 24;
1845
1846 if (Args.empty()) {
Chris Lattner45b39762006-02-13 08:55:29 +00001847 Chain = DAG.getCALLSEQ_START(Chain,
1848 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001849 } else {
Chris Lattner915fb302005-08-30 00:19:00 +00001850 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001851 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +00001852 default: assert(0 && "Unknown value type!");
1853 case MVT::i1:
1854 case MVT::i8:
1855 case MVT::i16:
1856 case MVT::i32:
1857 case MVT::f32:
1858 NumBytes += 4;
1859 break;
1860 case MVT::i64:
1861 case MVT::f64:
1862 NumBytes += 8;
1863 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001864 }
Chris Lattner915fb302005-08-30 00:19:00 +00001865 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001866
Chris Lattner915fb302005-08-30 00:19:00 +00001867 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
1868 // plus 32 bytes of argument space in case any called code gets funky on us.
1869 // (Required by ABI to support var arg)
1870 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001871
1872 // Adjust the stack pointer for the new arguments...
1873 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattner45b39762006-02-13 08:55:29 +00001874 Chain = DAG.getCALLSEQ_START(Chain,
1875 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001876
1877 // Set up a copy of the stack pointer for use loading and storing any
1878 // arguments that may not fit in the registers available for argument
1879 // passing.
Chris Lattnera243db82006-01-11 19:55:07 +00001880 SDOperand StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001881
1882 // Figure out which arguments are going to go in registers, and which in
1883 // memory. Also, if this is a vararg function, floating point operations
1884 // must be stored to our stack, and loaded into integer regs as well, if
1885 // any integer regs are available for argument passing.
1886 unsigned ArgOffset = 24;
1887 unsigned GPR_remaining = 8;
1888 unsigned FPR_remaining = 13;
1889
1890 std::vector<SDOperand> MemOps;
1891 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
1892 // PtrOff will be used to store the current argument to the stack if a
1893 // register cannot be found for it.
1894 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1895 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
1896 MVT::ValueType ArgVT = getValueType(Args[i].second);
1897
1898 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +00001899 default: assert(0 && "Unexpected ValueType for argument!");
1900 case MVT::i1:
1901 case MVT::i8:
1902 case MVT::i16:
1903 // Promote the integer to 32 bits. If the input type is signed use a
1904 // sign extend, otherwise use a zero extend.
1905 if (Args[i].second->isSigned())
1906 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
1907 else
1908 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
1909 // FALL THROUGH
1910 case MVT::i32:
1911 if (GPR_remaining > 0) {
1912 args_to_use.push_back(Args[i].first);
1913 --GPR_remaining;
1914 } else {
1915 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1916 Args[i].first, PtrOff,
1917 DAG.getSrcValue(NULL)));
1918 }
1919 ArgOffset += 4;
1920 break;
1921 case MVT::i64:
1922 // If we have one free GPR left, we can place the upper half of the i64
1923 // in it, and store the other half to the stack. If we have two or more
1924 // free GPRs, then we can pass both halves of the i64 in registers.
1925 if (GPR_remaining > 0) {
1926 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
1927 Args[i].first, DAG.getConstant(1, MVT::i32));
1928 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
1929 Args[i].first, DAG.getConstant(0, MVT::i32));
1930 args_to_use.push_back(Hi);
1931 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001932 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +00001933 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001934 --GPR_remaining;
1935 } else {
Chris Lattner915fb302005-08-30 00:19:00 +00001936 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
1937 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001938 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +00001939 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001940 }
Chris Lattner915fb302005-08-30 00:19:00 +00001941 } else {
1942 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1943 Args[i].first, PtrOff,
1944 DAG.getSrcValue(NULL)));
1945 }
1946 ArgOffset += 8;
1947 break;
1948 case MVT::f32:
1949 case MVT::f64:
1950 if (FPR_remaining > 0) {
1951 args_to_use.push_back(Args[i].first);
1952 --FPR_remaining;
1953 if (isVarArg) {
1954 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
1955 Args[i].first, PtrOff,
1956 DAG.getSrcValue(NULL));
1957 MemOps.push_back(Store);
1958 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001959 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +00001960 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
1961 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +00001962 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +00001963 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001964 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +00001965 }
1966 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001967 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
1968 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +00001969 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
1970 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +00001971 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +00001972 args_to_use.push_back(Load);
1973 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001974 }
1975 } else {
Chris Lattner915fb302005-08-30 00:19:00 +00001976 // If we have any FPRs remaining, we may also have GPRs remaining.
1977 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
1978 // GPRs.
1979 if (GPR_remaining > 0) {
1980 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
1981 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001982 }
Chris Lattner915fb302005-08-30 00:19:00 +00001983 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
1984 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
1985 --GPR_remaining;
1986 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001987 }
Chris Lattner915fb302005-08-30 00:19:00 +00001988 } else {
1989 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1990 Args[i].first, PtrOff,
1991 DAG.getSrcValue(NULL)));
1992 }
1993 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
1994 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001995 }
1996 }
1997 if (!MemOps.empty())
1998 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
1999 }
2000
2001 std::vector<MVT::ValueType> RetVals;
2002 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +00002003 MVT::ValueType ActualRetTyVT = RetTyVT;
2004 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
2005 ActualRetTyVT = MVT::i32; // Promote result to i32.
2006
Chris Lattnere00ebf02006-01-28 07:33:03 +00002007 if (RetTyVT == MVT::i64) {
2008 RetVals.push_back(MVT::i32);
2009 RetVals.push_back(MVT::i32);
2010 } else if (RetTyVT != MVT::isVoid) {
Chris Lattnerf5059492005-09-02 01:24:55 +00002011 RetVals.push_back(ActualRetTyVT);
Chris Lattnere00ebf02006-01-28 07:33:03 +00002012 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +00002013 RetVals.push_back(MVT::Other);
2014
Chris Lattner2823b3e2005-11-17 05:56:14 +00002015 // If the callee is a GlobalAddress node (quite common, every direct call is)
2016 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
2017 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
2018 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
2019
Chris Lattner281b55e2006-01-27 23:34:02 +00002020 std::vector<SDOperand> Ops;
2021 Ops.push_back(Chain);
2022 Ops.push_back(Callee);
2023 Ops.insert(Ops.end(), args_to_use.begin(), args_to_use.end());
2024 SDOperand TheCall = DAG.getNode(PPCISD::CALL, RetVals, Ops);
Chris Lattnere00ebf02006-01-28 07:33:03 +00002025 Chain = TheCall.getValue(TheCall.Val->getNumValues()-1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00002026 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
2027 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +00002028 SDOperand RetVal = TheCall;
2029
2030 // If the result is a small value, add a note so that we keep track of the
2031 // information about whether it is sign or zero extended.
2032 if (RetTyVT != ActualRetTyVT) {
2033 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
2034 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
2035 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
Chris Lattnere00ebf02006-01-28 07:33:03 +00002036 } else if (RetTyVT == MVT::i64) {
2037 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, RetVal, RetVal.getValue(1));
Chris Lattnerf5059492005-09-02 01:24:55 +00002038 }
2039
2040 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00002041}
2042
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00002043MachineBasicBlock *
Nate Begeman21e463b2005-10-16 05:39:50 +00002044PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
2045 MachineBasicBlock *BB) {
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00002046 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
Chris Lattner919c0322005-10-01 01:35:02 +00002047 MI->getOpcode() == PPC::SELECT_CC_F4 ||
Chris Lattner710ff322006-04-08 22:45:08 +00002048 MI->getOpcode() == PPC::SELECT_CC_F8 ||
2049 MI->getOpcode() == PPC::SELECT_CC_VRRC) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00002050 "Unexpected instr type to insert");
2051
2052 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
2053 // control-flow pattern. The incoming instruction knows the destination vreg
2054 // to set, the condition code register to branch on, the true/false values to
2055 // select between, and a branch opcode to use.
2056 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2057 ilist<MachineBasicBlock>::iterator It = BB;
2058 ++It;
2059
2060 // thisMBB:
2061 // ...
2062 // TrueVal = ...
2063 // cmpTY ccX, r1, r2
2064 // bCC copy1MBB
2065 // fallthrough --> copy0MBB
2066 MachineBasicBlock *thisMBB = BB;
2067 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
2068 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
2069 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
2070 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
2071 MachineFunction *F = BB->getParent();
2072 F->getBasicBlockList().insert(It, copy0MBB);
2073 F->getBasicBlockList().insert(It, sinkMBB);
Nate Begemanf15485a2006-03-27 01:32:24 +00002074 // Update machine-CFG edges by first adding all successors of the current
2075 // block to the new block which will contain the Phi node for the select.
2076 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
2077 e = BB->succ_end(); i != e; ++i)
2078 sinkMBB->addSuccessor(*i);
2079 // Next, remove all successors of the current block, and add the true
2080 // and fallthrough blocks as its successors.
2081 while(!BB->succ_empty())
2082 BB->removeSuccessor(BB->succ_begin());
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00002083 BB->addSuccessor(copy0MBB);
2084 BB->addSuccessor(sinkMBB);
2085
2086 // copy0MBB:
2087 // %FalseValue = ...
2088 // # fallthrough to sinkMBB
2089 BB = copy0MBB;
2090
2091 // Update machine-CFG edges
2092 BB->addSuccessor(sinkMBB);
2093
2094 // sinkMBB:
2095 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2096 // ...
2097 BB = sinkMBB;
2098 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
2099 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
2100 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
2101
2102 delete MI; // The pseudo instruction is gone now.
2103 return BB;
2104}
2105
Chris Lattner1a635d62006-04-14 06:01:58 +00002106//===----------------------------------------------------------------------===//
2107// Target Optimization Hooks
2108//===----------------------------------------------------------------------===//
2109
Chris Lattner8c13d0a2006-03-01 04:57:39 +00002110SDOperand PPCTargetLowering::PerformDAGCombine(SDNode *N,
2111 DAGCombinerInfo &DCI) const {
2112 TargetMachine &TM = getTargetMachine();
2113 SelectionDAG &DAG = DCI.DAG;
2114 switch (N->getOpcode()) {
2115 default: break;
2116 case ISD::SINT_TO_FP:
2117 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
Chris Lattnerecfe55e2006-03-22 05:30:33 +00002118 if (N->getOperand(0).getOpcode() == ISD::FP_TO_SINT) {
2119 // Turn (sint_to_fp (fp_to_sint X)) -> fctidz/fcfid without load/stores.
2120 // We allow the src/dst to be either f32/f64, but the intermediate
2121 // type must be i64.
2122 if (N->getOperand(0).getValueType() == MVT::i64) {
2123 SDOperand Val = N->getOperand(0).getOperand(0);
2124 if (Val.getValueType() == MVT::f32) {
2125 Val = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Val);
2126 DCI.AddToWorklist(Val.Val);
2127 }
2128
2129 Val = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Val);
Chris Lattner8c13d0a2006-03-01 04:57:39 +00002130 DCI.AddToWorklist(Val.Val);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00002131 Val = DAG.getNode(PPCISD::FCFID, MVT::f64, Val);
Chris Lattner8c13d0a2006-03-01 04:57:39 +00002132 DCI.AddToWorklist(Val.Val);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00002133 if (N->getValueType(0) == MVT::f32) {
2134 Val = DAG.getNode(ISD::FP_ROUND, MVT::f32, Val);
2135 DCI.AddToWorklist(Val.Val);
2136 }
2137 return Val;
2138 } else if (N->getOperand(0).getValueType() == MVT::i32) {
2139 // If the intermediate type is i32, we can avoid the load/store here
2140 // too.
Chris Lattner8c13d0a2006-03-01 04:57:39 +00002141 }
Chris Lattner8c13d0a2006-03-01 04:57:39 +00002142 }
2143 }
2144 break;
Chris Lattner51269842006-03-01 05:50:56 +00002145 case ISD::STORE:
2146 // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)).
2147 if (TM.getSubtarget<PPCSubtarget>().hasSTFIWX() &&
2148 N->getOperand(1).getOpcode() == ISD::FP_TO_SINT &&
2149 N->getOperand(1).getValueType() == MVT::i32) {
2150 SDOperand Val = N->getOperand(1).getOperand(0);
2151 if (Val.getValueType() == MVT::f32) {
2152 Val = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Val);
2153 DCI.AddToWorklist(Val.Val);
2154 }
2155 Val = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Val);
2156 DCI.AddToWorklist(Val.Val);
2157
2158 Val = DAG.getNode(PPCISD::STFIWX, MVT::Other, N->getOperand(0), Val,
2159 N->getOperand(2), N->getOperand(3));
2160 DCI.AddToWorklist(Val.Val);
2161 return Val;
2162 }
2163 break;
Chris Lattner4468c222006-03-31 06:02:07 +00002164 case PPCISD::VCMP: {
2165 // If a VCMPo node already exists with exactly the same operands as this
2166 // node, use its result instead of this node (VCMPo computes both a CR6 and
2167 // a normal output).
2168 //
2169 if (!N->getOperand(0).hasOneUse() &&
2170 !N->getOperand(1).hasOneUse() &&
2171 !N->getOperand(2).hasOneUse()) {
2172
2173 // Scan all of the users of the LHS, looking for VCMPo's that match.
2174 SDNode *VCMPoNode = 0;
2175
2176 SDNode *LHSN = N->getOperand(0).Val;
2177 for (SDNode::use_iterator UI = LHSN->use_begin(), E = LHSN->use_end();
2178 UI != E; ++UI)
2179 if ((*UI)->getOpcode() == PPCISD::VCMPo &&
2180 (*UI)->getOperand(1) == N->getOperand(1) &&
2181 (*UI)->getOperand(2) == N->getOperand(2) &&
2182 (*UI)->getOperand(0) == N->getOperand(0)) {
2183 VCMPoNode = *UI;
2184 break;
2185 }
2186
2187 // If there are non-zero uses of the flag value, use the VCMPo node!
Chris Lattner33497cc2006-03-31 06:04:53 +00002188 if (VCMPoNode && !VCMPoNode->hasNUsesOfValue(0, 1))
Chris Lattner4468c222006-03-31 06:02:07 +00002189 return SDOperand(VCMPoNode, 0);
2190 }
2191 break;
2192 }
Chris Lattner8c13d0a2006-03-01 04:57:39 +00002193 }
2194
2195 return SDOperand();
2196}
2197
Chris Lattner1a635d62006-04-14 06:01:58 +00002198//===----------------------------------------------------------------------===//
2199// Inline Assembly Support
2200//===----------------------------------------------------------------------===//
2201
Chris Lattnerbbe77de2006-04-02 06:26:07 +00002202void PPCTargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
2203 uint64_t Mask,
2204 uint64_t &KnownZero,
2205 uint64_t &KnownOne,
2206 unsigned Depth) const {
2207 KnownZero = 0;
2208 KnownOne = 0;
2209 switch (Op.getOpcode()) {
2210 default: break;
2211 case ISD::INTRINSIC_WO_CHAIN: {
2212 switch (cast<ConstantSDNode>(Op.getOperand(0))->getValue()) {
2213 default: break;
2214 case Intrinsic::ppc_altivec_vcmpbfp_p:
2215 case Intrinsic::ppc_altivec_vcmpeqfp_p:
2216 case Intrinsic::ppc_altivec_vcmpequb_p:
2217 case Intrinsic::ppc_altivec_vcmpequh_p:
2218 case Intrinsic::ppc_altivec_vcmpequw_p:
2219 case Intrinsic::ppc_altivec_vcmpgefp_p:
2220 case Intrinsic::ppc_altivec_vcmpgtfp_p:
2221 case Intrinsic::ppc_altivec_vcmpgtsb_p:
2222 case Intrinsic::ppc_altivec_vcmpgtsh_p:
2223 case Intrinsic::ppc_altivec_vcmpgtsw_p:
2224 case Intrinsic::ppc_altivec_vcmpgtub_p:
2225 case Intrinsic::ppc_altivec_vcmpgtuh_p:
2226 case Intrinsic::ppc_altivec_vcmpgtuw_p:
2227 KnownZero = ~1U; // All bits but the low one are known to be zero.
2228 break;
2229 }
2230 }
2231 }
2232}
2233
2234
Chris Lattnerad3bc8d2006-02-07 20:16:30 +00002235/// getConstraintType - Given a constraint letter, return the type of
2236/// constraint it is for this target.
2237PPCTargetLowering::ConstraintType
2238PPCTargetLowering::getConstraintType(char ConstraintLetter) const {
2239 switch (ConstraintLetter) {
2240 default: break;
2241 case 'b':
2242 case 'r':
2243 case 'f':
2244 case 'v':
2245 case 'y':
2246 return C_RegisterClass;
2247 }
2248 return TargetLowering::getConstraintType(ConstraintLetter);
2249}
2250
2251
Chris Lattnerddc787d2006-01-31 19:20:21 +00002252std::vector<unsigned> PPCTargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00002253getRegClassForInlineAsmConstraint(const std::string &Constraint,
2254 MVT::ValueType VT) const {
Chris Lattnerddc787d2006-01-31 19:20:21 +00002255 if (Constraint.size() == 1) {
2256 switch (Constraint[0]) { // GCC RS6000 Constraint Letters
2257 default: break; // Unknown constriant letter
2258 case 'b':
2259 return make_vector<unsigned>(/*no R0*/ PPC::R1 , PPC::R2 , PPC::R3 ,
2260 PPC::R4 , PPC::R5 , PPC::R6 , PPC::R7 ,
2261 PPC::R8 , PPC::R9 , PPC::R10, PPC::R11,
2262 PPC::R12, PPC::R13, PPC::R14, PPC::R15,
2263 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
2264 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
2265 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
2266 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
2267 0);
2268 case 'r':
2269 return make_vector<unsigned>(PPC::R0 , PPC::R1 , PPC::R2 , PPC::R3 ,
2270 PPC::R4 , PPC::R5 , PPC::R6 , PPC::R7 ,
2271 PPC::R8 , PPC::R9 , PPC::R10, PPC::R11,
2272 PPC::R12, PPC::R13, PPC::R14, PPC::R15,
2273 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
2274 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
2275 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
2276 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
2277 0);
2278 case 'f':
2279 return make_vector<unsigned>(PPC::F0 , PPC::F1 , PPC::F2 , PPC::F3 ,
2280 PPC::F4 , PPC::F5 , PPC::F6 , PPC::F7 ,
2281 PPC::F8 , PPC::F9 , PPC::F10, PPC::F11,
2282 PPC::F12, PPC::F13, PPC::F14, PPC::F15,
2283 PPC::F16, PPC::F17, PPC::F18, PPC::F19,
2284 PPC::F20, PPC::F21, PPC::F22, PPC::F23,
2285 PPC::F24, PPC::F25, PPC::F26, PPC::F27,
2286 PPC::F28, PPC::F29, PPC::F30, PPC::F31,
2287 0);
2288 case 'v':
2289 return make_vector<unsigned>(PPC::V0 , PPC::V1 , PPC::V2 , PPC::V3 ,
2290 PPC::V4 , PPC::V5 , PPC::V6 , PPC::V7 ,
2291 PPC::V8 , PPC::V9 , PPC::V10, PPC::V11,
2292 PPC::V12, PPC::V13, PPC::V14, PPC::V15,
2293 PPC::V16, PPC::V17, PPC::V18, PPC::V19,
2294 PPC::V20, PPC::V21, PPC::V22, PPC::V23,
2295 PPC::V24, PPC::V25, PPC::V26, PPC::V27,
2296 PPC::V28, PPC::V29, PPC::V30, PPC::V31,
2297 0);
2298 case 'y':
2299 return make_vector<unsigned>(PPC::CR0, PPC::CR1, PPC::CR2, PPC::CR3,
2300 PPC::CR4, PPC::CR5, PPC::CR6, PPC::CR7,
2301 0);
2302 }
2303 }
2304
Chris Lattner1efa40f2006-02-22 00:56:39 +00002305 return std::vector<unsigned>();
Chris Lattnerddc787d2006-01-31 19:20:21 +00002306}
Chris Lattner763317d2006-02-07 00:47:13 +00002307
2308// isOperandValidForConstraint
2309bool PPCTargetLowering::
2310isOperandValidForConstraint(SDOperand Op, char Letter) {
2311 switch (Letter) {
2312 default: break;
2313 case 'I':
2314 case 'J':
2315 case 'K':
2316 case 'L':
2317 case 'M':
2318 case 'N':
2319 case 'O':
2320 case 'P': {
2321 if (!isa<ConstantSDNode>(Op)) return false; // Must be an immediate.
2322 unsigned Value = cast<ConstantSDNode>(Op)->getValue();
2323 switch (Letter) {
2324 default: assert(0 && "Unknown constraint letter!");
2325 case 'I': // "I" is a signed 16-bit constant.
2326 return (short)Value == (int)Value;
2327 case 'J': // "J" is a constant with only the high-order 16 bits nonzero.
2328 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits.
2329 return (short)Value == 0;
2330 case 'K': // "K" is a constant with only the low-order 16 bits nonzero.
2331 return (Value >> 16) == 0;
2332 case 'M': // "M" is a constant that is greater than 31.
2333 return Value > 31;
2334 case 'N': // "N" is a positive constant that is an exact power of two.
2335 return (int)Value > 0 && isPowerOf2_32(Value);
2336 case 'O': // "O" is the constant zero.
2337 return Value == 0;
2338 case 'P': // "P" is a constant whose negation is a signed 16-bit constant.
2339 return (short)-Value == (int)-Value;
2340 }
2341 break;
2342 }
2343 }
2344
2345 // Handle standard constraint letters.
2346 return TargetLowering::isOperandValidForConstraint(Op, Letter);
2347}
Evan Chengc4c62572006-03-13 23:20:37 +00002348
2349/// isLegalAddressImmediate - Return true if the integer value can be used
2350/// as the offset of the target addressing mode.
2351bool PPCTargetLowering::isLegalAddressImmediate(int64_t V) const {
2352 // PPC allows a sign-extended 16-bit immediate field.
2353 return (V > -(1 << 16) && V < (1 << 16)-1);
2354}