blob: fac2806c1208d25429e7160e9d5a06a01ba0fa5e [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);
Nate Begeman37efe672006-04-22 18:53:45 +0000126 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000127
Nate Begemanee625572006-01-27 21:09:22 +0000128 // RET must be custom lowered, to meet ABI requirements
129 setOperationAction(ISD::RET , MVT::Other, Custom);
130
Nate Begemanacc398c2006-01-25 18:21:52 +0000131 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
132 setOperationAction(ISD::VASTART , MVT::Other, Custom);
133
Chris Lattnerb22c08b2006-01-15 09:02:48 +0000134 // Use the default implementation.
Nate Begemanacc398c2006-01-25 18:21:52 +0000135 setOperationAction(ISD::VAARG , MVT::Other, Expand);
136 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
137 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Chris Lattnerb22c08b2006-01-15 09:02:48 +0000138 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
139 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
140 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Chris Lattner860e8862005-11-17 07:30:41 +0000141
Chris Lattner6d92cad2006-03-26 10:06:40 +0000142 // We want to custom lower some of our intrinsics.
Chris Lattner48b61a72006-03-28 00:40:33 +0000143 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Chris Lattner6d92cad2006-03-26 10:06:40 +0000144
Nate Begemanc09eeec2005-09-06 22:03:27 +0000145 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000146 // They also have instructions for converting between i64 and fp.
Nate Begemanc09eeec2005-09-06 22:03:27 +0000147 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
148 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Chris Lattner7fbcef72006-03-24 07:53:47 +0000149
150 // FIXME: disable this lowered code. This generates 64-bit register values,
151 // and we don't model the fact that the top part is clobbered by calls. We
152 // need to flag these together so that the value isn't live across a call.
153 //setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
154
Nate Begemanae749a92005-10-25 23:48:36 +0000155 // To take advantage of the above i64 FP_TO_SINT, promote i32 FP_TO_UINT
156 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Promote);
157 } else {
Chris Lattner860e8862005-11-17 07:30:41 +0000158 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Nate Begemanae749a92005-10-25 23:48:36 +0000159 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begeman9d2b8172005-10-18 00:56:42 +0000160 }
161
162 if (TM.getSubtarget<PPCSubtarget>().has64BitRegs()) {
163 // 64 bit PowerPC implementations can support i64 types directly
164 addRegisterClass(MVT::i64, PPC::G8RCRegisterClass);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000165 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
166 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000167 } else {
168 // 32 bit PowerPC wants to expand i64 shifts itself.
169 setOperationAction(ISD::SHL, MVT::i64, Custom);
170 setOperationAction(ISD::SRL, MVT::i64, Custom);
171 setOperationAction(ISD::SRA, MVT::i64, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000172 }
Evan Chengd30bf012006-03-01 01:11:20 +0000173
Nate Begeman425a9692005-11-29 08:17:20 +0000174 if (TM.getSubtarget<PPCSubtarget>().hasAltivec()) {
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000175 // First set operation action for all vector types to expand. Then we
176 // will selectively turn on ones that can be effectively codegen'd.
177 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
178 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Chris Lattnerf3f69de2006-04-16 01:37:57 +0000179 // add/sub are legal for all supported vector VT's.
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000180 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Legal);
181 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Legal);
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000182
Chris Lattner7ff7e672006-04-04 17:25:31 +0000183 // We promote all shuffles to v16i8.
184 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Promote);
Chris Lattnerf3f69de2006-04-16 01:37:57 +0000185 AddPromotedToType (ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, MVT::v16i8);
186
187 // We promote all non-typed operations to v4i32.
188 setOperationAction(ISD::AND , (MVT::ValueType)VT, Promote);
189 AddPromotedToType (ISD::AND , (MVT::ValueType)VT, MVT::v4i32);
190 setOperationAction(ISD::OR , (MVT::ValueType)VT, Promote);
191 AddPromotedToType (ISD::OR , (MVT::ValueType)VT, MVT::v4i32);
192 setOperationAction(ISD::XOR , (MVT::ValueType)VT, Promote);
193 AddPromotedToType (ISD::XOR , (MVT::ValueType)VT, MVT::v4i32);
194 setOperationAction(ISD::LOAD , (MVT::ValueType)VT, Promote);
195 AddPromotedToType (ISD::LOAD , (MVT::ValueType)VT, MVT::v4i32);
196 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
197 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v4i32);
198 setOperationAction(ISD::STORE, (MVT::ValueType)VT, Promote);
199 AddPromotedToType (ISD::STORE, (MVT::ValueType)VT, MVT::v4i32);
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000200
Chris Lattnerf3f69de2006-04-16 01:37:57 +0000201 // No other operations are legal.
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000202 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
203 setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand);
204 setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand);
205 setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand);
206 setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand);
207 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
208 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
209 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Expand);
Chris Lattner01cae072006-04-03 23:55:43 +0000210
211 setOperationAction(ISD::SCALAR_TO_VECTOR, (MVT::ValueType)VT, Expand);
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000212 }
213
Chris Lattner7ff7e672006-04-04 17:25:31 +0000214 // We can custom expand all VECTOR_SHUFFLEs to VPERM, others we can handle
215 // with merges, splats, etc.
216 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i8, Custom);
217
Chris Lattnerf3f69de2006-04-16 01:37:57 +0000218 setOperationAction(ISD::AND , MVT::v4i32, Legal);
219 setOperationAction(ISD::OR , MVT::v4i32, Legal);
220 setOperationAction(ISD::XOR , MVT::v4i32, Legal);
221 setOperationAction(ISD::LOAD , MVT::v4i32, Legal);
222 setOperationAction(ISD::SELECT, MVT::v4i32, Expand);
223 setOperationAction(ISD::STORE , MVT::v4i32, Legal);
224
Nate Begeman425a9692005-11-29 08:17:20 +0000225 addRegisterClass(MVT::v4f32, PPC::VRRCRegisterClass);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000226 addRegisterClass(MVT::v4i32, PPC::VRRCRegisterClass);
Chris Lattner8d052bc2006-03-25 07:39:07 +0000227 addRegisterClass(MVT::v8i16, PPC::VRRCRegisterClass);
228 addRegisterClass(MVT::v16i8, PPC::VRRCRegisterClass);
Chris Lattnerec4a0c72006-01-29 06:32:58 +0000229
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000230 setOperationAction(ISD::MUL, MVT::v4f32, Legal);
Chris Lattnere7c768e2006-04-18 03:24:30 +0000231 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
Chris Lattner72dd9bd2006-04-18 03:43:48 +0000232 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
Chris Lattner19a81522006-04-18 03:57:35 +0000233 setOperationAction(ISD::MUL, MVT::v16i8, Custom);
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000234
Chris Lattnerb2177b92006-03-19 06:55:52 +0000235 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom);
236 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Custom);
Chris Lattner64b3a082006-03-24 07:48:08 +0000237
Chris Lattner541f91b2006-04-02 00:43:36 +0000238 setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Custom);
239 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Custom);
Chris Lattner64b3a082006-03-24 07:48:08 +0000240 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Custom);
241 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
Nate Begeman425a9692005-11-29 08:17:20 +0000242 }
243
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000244 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattnercadd7422006-01-13 17:52:03 +0000245 setStackPointerRegisterToSaveRestore(PPC::R1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000246
Chris Lattner8c13d0a2006-03-01 04:57:39 +0000247 // We have target-specific dag combine patterns for the following nodes:
248 setTargetDAGCombine(ISD::SINT_TO_FP);
Chris Lattner51269842006-03-01 05:50:56 +0000249 setTargetDAGCombine(ISD::STORE);
Chris Lattner90564f22006-04-18 17:59:36 +0000250 setTargetDAGCombine(ISD::BR_CC);
Chris Lattner8c13d0a2006-03-01 04:57:39 +0000251
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000252 computeRegisterProperties();
253}
254
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000255const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
256 switch (Opcode) {
257 default: return 0;
258 case PPCISD::FSEL: return "PPCISD::FSEL";
259 case PPCISD::FCFID: return "PPCISD::FCFID";
260 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
261 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
Chris Lattner51269842006-03-01 05:50:56 +0000262 case PPCISD::STFIWX: return "PPCISD::STFIWX";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000263 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
264 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000265 case PPCISD::VPERM: return "PPCISD::VPERM";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000266 case PPCISD::Hi: return "PPCISD::Hi";
267 case PPCISD::Lo: return "PPCISD::Lo";
268 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
269 case PPCISD::SRL: return "PPCISD::SRL";
270 case PPCISD::SRA: return "PPCISD::SRA";
271 case PPCISD::SHL: return "PPCISD::SHL";
Chris Lattnerecfe55e2006-03-22 05:30:33 +0000272 case PPCISD::EXTSW_32: return "PPCISD::EXTSW_32";
273 case PPCISD::STD_32: return "PPCISD::STD_32";
Chris Lattnere00ebf02006-01-28 07:33:03 +0000274 case PPCISD::CALL: return "PPCISD::CALL";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000275 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
Chris Lattner6d92cad2006-03-26 10:06:40 +0000276 case PPCISD::MFCR: return "PPCISD::MFCR";
Chris Lattnera17b1552006-03-31 05:13:27 +0000277 case PPCISD::VCMP: return "PPCISD::VCMP";
Chris Lattner6d92cad2006-03-26 10:06:40 +0000278 case PPCISD::VCMPo: return "PPCISD::VCMPo";
Chris Lattnerf70f8d92006-04-18 18:05:58 +0000279 case PPCISD::COND_BRANCH: return "PPCISD::COND_BRANCH";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000280 }
281}
282
Chris Lattner1a635d62006-04-14 06:01:58 +0000283//===----------------------------------------------------------------------===//
284// Node matching predicates, for use by the tblgen matching code.
285//===----------------------------------------------------------------------===//
286
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000287/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
288static bool isFloatingPointZero(SDOperand Op) {
289 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
290 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
291 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
292 // Maybe this has already been legalized into the constant pool?
293 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
294 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
295 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
296 }
297 return false;
298}
299
Chris Lattnerddb739e2006-04-06 17:23:16 +0000300/// isConstantOrUndef - Op is either an undef node or a ConstantSDNode. Return
301/// true if Op is undef or if it matches the specified value.
302static bool isConstantOrUndef(SDOperand Op, unsigned Val) {
303 return Op.getOpcode() == ISD::UNDEF ||
304 cast<ConstantSDNode>(Op)->getValue() == Val;
305}
306
307/// isVPKUHUMShuffleMask - Return true if this is the shuffle mask for a
308/// VPKUHUM instruction.
Chris Lattnerf24380e2006-04-06 22:28:36 +0000309bool PPC::isVPKUHUMShuffleMask(SDNode *N, bool isUnary) {
310 if (!isUnary) {
311 for (unsigned i = 0; i != 16; ++i)
312 if (!isConstantOrUndef(N->getOperand(i), i*2+1))
313 return false;
314 } else {
315 for (unsigned i = 0; i != 8; ++i)
316 if (!isConstantOrUndef(N->getOperand(i), i*2+1) ||
317 !isConstantOrUndef(N->getOperand(i+8), i*2+1))
318 return false;
319 }
Chris Lattnerd0608e12006-04-06 18:26:28 +0000320 return true;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000321}
322
323/// isVPKUWUMShuffleMask - Return true if this is the shuffle mask for a
324/// VPKUWUM instruction.
Chris Lattnerf24380e2006-04-06 22:28:36 +0000325bool PPC::isVPKUWUMShuffleMask(SDNode *N, bool isUnary) {
326 if (!isUnary) {
327 for (unsigned i = 0; i != 16; i += 2)
328 if (!isConstantOrUndef(N->getOperand(i ), i*2+2) ||
329 !isConstantOrUndef(N->getOperand(i+1), i*2+3))
330 return false;
331 } else {
332 for (unsigned i = 0; i != 8; i += 2)
333 if (!isConstantOrUndef(N->getOperand(i ), i*2+2) ||
334 !isConstantOrUndef(N->getOperand(i+1), i*2+3) ||
335 !isConstantOrUndef(N->getOperand(i+8), i*2+2) ||
336 !isConstantOrUndef(N->getOperand(i+9), i*2+3))
337 return false;
338 }
Chris Lattnerd0608e12006-04-06 18:26:28 +0000339 return true;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000340}
341
Chris Lattnercaad1632006-04-06 22:02:42 +0000342/// isVMerge - Common function, used to match vmrg* shuffles.
343///
344static bool isVMerge(SDNode *N, unsigned UnitSize,
345 unsigned LHSStart, unsigned RHSStart) {
Chris Lattner116cc482006-04-06 21:11:54 +0000346 assert(N->getOpcode() == ISD::BUILD_VECTOR &&
347 N->getNumOperands() == 16 && "PPC only supports shuffles by bytes!");
348 assert((UnitSize == 1 || UnitSize == 2 || UnitSize == 4) &&
349 "Unsupported merge size!");
350
351 for (unsigned i = 0; i != 8/UnitSize; ++i) // Step over units
352 for (unsigned j = 0; j != UnitSize; ++j) { // Step over bytes within unit
353 if (!isConstantOrUndef(N->getOperand(i*UnitSize*2+j),
Chris Lattnercaad1632006-04-06 22:02:42 +0000354 LHSStart+j+i*UnitSize) ||
Chris Lattner116cc482006-04-06 21:11:54 +0000355 !isConstantOrUndef(N->getOperand(i*UnitSize*2+UnitSize+j),
Chris Lattnercaad1632006-04-06 22:02:42 +0000356 RHSStart+j+i*UnitSize))
Chris Lattner116cc482006-04-06 21:11:54 +0000357 return false;
358 }
Chris Lattnercaad1632006-04-06 22:02:42 +0000359 return true;
360}
361
362/// isVMRGLShuffleMask - Return true if this is a shuffle mask suitable for
363/// a VRGL* instruction with the specified unit size (1,2 or 4 bytes).
364bool PPC::isVMRGLShuffleMask(SDNode *N, unsigned UnitSize, bool isUnary) {
365 if (!isUnary)
366 return isVMerge(N, UnitSize, 8, 24);
367 return isVMerge(N, UnitSize, 8, 8);
Chris Lattner116cc482006-04-06 21:11:54 +0000368}
369
370/// isVMRGHShuffleMask - Return true if this is a shuffle mask suitable for
371/// a VRGH* instruction with the specified unit size (1,2 or 4 bytes).
Chris Lattnercaad1632006-04-06 22:02:42 +0000372bool PPC::isVMRGHShuffleMask(SDNode *N, unsigned UnitSize, bool isUnary) {
373 if (!isUnary)
374 return isVMerge(N, UnitSize, 0, 16);
375 return isVMerge(N, UnitSize, 0, 0);
Chris Lattner116cc482006-04-06 21:11:54 +0000376}
377
378
Chris Lattnerd0608e12006-04-06 18:26:28 +0000379/// isVSLDOIShuffleMask - If this is a vsldoi shuffle mask, return the shift
380/// amount, otherwise return -1.
Chris Lattnerf24380e2006-04-06 22:28:36 +0000381int PPC::isVSLDOIShuffleMask(SDNode *N, bool isUnary) {
Chris Lattner116cc482006-04-06 21:11:54 +0000382 assert(N->getOpcode() == ISD::BUILD_VECTOR &&
383 N->getNumOperands() == 16 && "PPC only supports shuffles by bytes!");
Chris Lattnerd0608e12006-04-06 18:26:28 +0000384 // Find the first non-undef value in the shuffle mask.
385 unsigned i;
386 for (i = 0; i != 16 && N->getOperand(i).getOpcode() == ISD::UNDEF; ++i)
387 /*search*/;
388
389 if (i == 16) return -1; // all undef.
390
391 // Otherwise, check to see if the rest of the elements are consequtively
392 // numbered from this value.
393 unsigned ShiftAmt = cast<ConstantSDNode>(N->getOperand(i))->getValue();
394 if (ShiftAmt < i) return -1;
395 ShiftAmt -= i;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000396
Chris Lattnerf24380e2006-04-06 22:28:36 +0000397 if (!isUnary) {
398 // Check the rest of the elements to see if they are consequtive.
399 for (++i; i != 16; ++i)
400 if (!isConstantOrUndef(N->getOperand(i), ShiftAmt+i))
401 return -1;
402 } else {
403 // Check the rest of the elements to see if they are consequtive.
404 for (++i; i != 16; ++i)
405 if (!isConstantOrUndef(N->getOperand(i), (ShiftAmt+i) & 15))
406 return -1;
407 }
Chris Lattnerd0608e12006-04-06 18:26:28 +0000408
409 return ShiftAmt;
410}
Chris Lattneref819f82006-03-20 06:33:01 +0000411
412/// isSplatShuffleMask - Return true if the specified VECTOR_SHUFFLE operand
413/// specifies a splat of a single element that is suitable for input to
414/// VSPLTB/VSPLTH/VSPLTW.
Chris Lattner7ff7e672006-04-04 17:25:31 +0000415bool PPC::isSplatShuffleMask(SDNode *N, unsigned EltSize) {
416 assert(N->getOpcode() == ISD::BUILD_VECTOR &&
417 N->getNumOperands() == 16 &&
418 (EltSize == 1 || EltSize == 2 || EltSize == 4));
Chris Lattnerdd4d2d02006-03-20 06:51:10 +0000419
Chris Lattner88a99ef2006-03-20 06:37:44 +0000420 // This is a splat operation if each element of the permute is the same, and
421 // if the value doesn't reference the second vector.
Chris Lattner7ff7e672006-04-04 17:25:31 +0000422 unsigned ElementBase = 0;
Chris Lattner88a99ef2006-03-20 06:37:44 +0000423 SDOperand Elt = N->getOperand(0);
Chris Lattner7ff7e672006-04-04 17:25:31 +0000424 if (ConstantSDNode *EltV = dyn_cast<ConstantSDNode>(Elt))
425 ElementBase = EltV->getValue();
426 else
427 return false; // FIXME: Handle UNDEF elements too!
428
429 if (cast<ConstantSDNode>(Elt)->getValue() >= 16)
430 return false;
431
432 // Check that they are consequtive.
433 for (unsigned i = 1; i != EltSize; ++i) {
434 if (!isa<ConstantSDNode>(N->getOperand(i)) ||
435 cast<ConstantSDNode>(N->getOperand(i))->getValue() != i+ElementBase)
436 return false;
437 }
438
Chris Lattner88a99ef2006-03-20 06:37:44 +0000439 assert(isa<ConstantSDNode>(Elt) && "Invalid VECTOR_SHUFFLE mask!");
Chris Lattner7ff7e672006-04-04 17:25:31 +0000440 for (unsigned i = EltSize, e = 16; i != e; i += EltSize) {
Chris Lattnerb097aa92006-04-14 23:19:08 +0000441 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Chris Lattner88a99ef2006-03-20 06:37:44 +0000442 assert(isa<ConstantSDNode>(N->getOperand(i)) &&
443 "Invalid VECTOR_SHUFFLE mask!");
Chris Lattner7ff7e672006-04-04 17:25:31 +0000444 for (unsigned j = 0; j != EltSize; ++j)
445 if (N->getOperand(i+j) != N->getOperand(j))
446 return false;
Chris Lattner88a99ef2006-03-20 06:37:44 +0000447 }
448
Chris Lattner7ff7e672006-04-04 17:25:31 +0000449 return true;
Chris Lattneref819f82006-03-20 06:33:01 +0000450}
451
452/// getVSPLTImmediate - Return the appropriate VSPLT* immediate to splat the
453/// specified isSplatShuffleMask VECTOR_SHUFFLE mask.
Chris Lattner7ff7e672006-04-04 17:25:31 +0000454unsigned PPC::getVSPLTImmediate(SDNode *N, unsigned EltSize) {
455 assert(isSplatShuffleMask(N, EltSize));
456 return cast<ConstantSDNode>(N->getOperand(0))->getValue() / EltSize;
Chris Lattneref819f82006-03-20 06:33:01 +0000457}
458
Chris Lattnere87192a2006-04-12 17:37:20 +0000459/// get_VSPLTI_elt - If this is a build_vector of constants which can be formed
Chris Lattner140a58f2006-04-08 06:46:53 +0000460/// by using a vspltis[bhw] instruction of the specified element size, return
461/// the constant being splatted. The ByteSize field indicates the number of
462/// bytes of each element [124] -> [bhw].
Chris Lattnere87192a2006-04-12 17:37:20 +0000463SDOperand PPC::get_VSPLTI_elt(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000464 SDOperand OpVal(0, 0);
Chris Lattner79d9a882006-04-08 07:14:26 +0000465
466 // If ByteSize of the splat is bigger than the element size of the
467 // build_vector, then we have a case where we are checking for a splat where
468 // multiple elements of the buildvector are folded together into a single
469 // logical element of the splat (e.g. "vsplish 1" to splat {0,1}*8).
470 unsigned EltSize = 16/N->getNumOperands();
471 if (EltSize < ByteSize) {
472 unsigned Multiple = ByteSize/EltSize; // Number of BV entries per spltval.
473 SDOperand UniquedVals[4];
474 assert(Multiple > 1 && Multiple <= 4 && "How can this happen?");
475
476 // See if all of the elements in the buildvector agree across.
477 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
478 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
479 // If the element isn't a constant, bail fully out.
480 if (!isa<ConstantSDNode>(N->getOperand(i))) return SDOperand();
481
482
483 if (UniquedVals[i&(Multiple-1)].Val == 0)
484 UniquedVals[i&(Multiple-1)] = N->getOperand(i);
485 else if (UniquedVals[i&(Multiple-1)] != N->getOperand(i))
486 return SDOperand(); // no match.
487 }
488
489 // Okay, if we reached this point, UniquedVals[0..Multiple-1] contains
490 // either constant or undef values that are identical for each chunk. See
491 // if these chunks can form into a larger vspltis*.
492
493 // Check to see if all of the leading entries are either 0 or -1. If
494 // neither, then this won't fit into the immediate field.
495 bool LeadingZero = true;
496 bool LeadingOnes = true;
497 for (unsigned i = 0; i != Multiple-1; ++i) {
498 if (UniquedVals[i].Val == 0) continue; // Must have been undefs.
499
500 LeadingZero &= cast<ConstantSDNode>(UniquedVals[i])->isNullValue();
501 LeadingOnes &= cast<ConstantSDNode>(UniquedVals[i])->isAllOnesValue();
502 }
503 // Finally, check the least significant entry.
504 if (LeadingZero) {
505 if (UniquedVals[Multiple-1].Val == 0)
506 return DAG.getTargetConstant(0, MVT::i32); // 0,0,0,undef
507 int Val = cast<ConstantSDNode>(UniquedVals[Multiple-1])->getValue();
508 if (Val < 16)
509 return DAG.getTargetConstant(Val, MVT::i32); // 0,0,0,4 -> vspltisw(4)
510 }
511 if (LeadingOnes) {
512 if (UniquedVals[Multiple-1].Val == 0)
513 return DAG.getTargetConstant(~0U, MVT::i32); // -1,-1,-1,undef
514 int Val =cast<ConstantSDNode>(UniquedVals[Multiple-1])->getSignExtended();
515 if (Val >= -16) // -1,-1,-1,-2 -> vspltisw(-2)
516 return DAG.getTargetConstant(Val, MVT::i32);
517 }
518
519 return SDOperand();
520 }
521
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000522 // Check to see if this buildvec has a single non-undef value in its elements.
523 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
524 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
525 if (OpVal.Val == 0)
526 OpVal = N->getOperand(i);
527 else if (OpVal != N->getOperand(i))
Chris Lattner140a58f2006-04-08 06:46:53 +0000528 return SDOperand();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000529 }
530
Chris Lattner140a58f2006-04-08 06:46:53 +0000531 if (OpVal.Val == 0) return SDOperand(); // All UNDEF: use implicit def.
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000532
Nate Begeman98e70cc2006-03-28 04:15:58 +0000533 unsigned ValSizeInBytes = 0;
534 uint64_t Value = 0;
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000535 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
536 Value = CN->getValue();
537 ValSizeInBytes = MVT::getSizeInBits(CN->getValueType(0))/8;
538 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) {
539 assert(CN->getValueType(0) == MVT::f32 && "Only one legal FP vector type!");
540 Value = FloatToBits(CN->getValue());
541 ValSizeInBytes = 4;
542 }
543
544 // If the splat value is larger than the element value, then we can never do
545 // this splat. The only case that we could fit the replicated bits into our
546 // immediate field for would be zero, and we prefer to use vxor for it.
Chris Lattner140a58f2006-04-08 06:46:53 +0000547 if (ValSizeInBytes < ByteSize) return SDOperand();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000548
549 // If the element value is larger than the splat value, cut it in half and
550 // check to see if the two halves are equal. Continue doing this until we
551 // get to ByteSize. This allows us to handle 0x01010101 as 0x01.
552 while (ValSizeInBytes > ByteSize) {
553 ValSizeInBytes >>= 1;
554
555 // If the top half equals the bottom half, we're still ok.
Chris Lattner9b42bdd2006-04-05 17:39:25 +0000556 if (((Value >> (ValSizeInBytes*8)) & ((1 << (8*ValSizeInBytes))-1)) !=
557 (Value & ((1 << (8*ValSizeInBytes))-1)))
Chris Lattner140a58f2006-04-08 06:46:53 +0000558 return SDOperand();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000559 }
560
561 // Properly sign extend the value.
562 int ShAmt = (4-ByteSize)*8;
563 int MaskVal = ((int)Value << ShAmt) >> ShAmt;
564
Evan Cheng5b6a01b2006-03-26 09:52:32 +0000565 // If this is zero, don't match, zero matches ISD::isBuildVectorAllZeros.
Chris Lattner140a58f2006-04-08 06:46:53 +0000566 if (MaskVal == 0) return SDOperand();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000567
Chris Lattner140a58f2006-04-08 06:46:53 +0000568 // Finally, if this value fits in a 5 bit sext field, return it
569 if (((MaskVal << (32-5)) >> (32-5)) == MaskVal)
570 return DAG.getTargetConstant(MaskVal, MVT::i32);
571 return SDOperand();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000572}
573
Chris Lattner1a635d62006-04-14 06:01:58 +0000574//===----------------------------------------------------------------------===//
575// LowerOperation implementation
576//===----------------------------------------------------------------------===//
577
578static SDOperand LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
579 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
580 Constant *C = CP->get();
581 SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i32, CP->getAlignment());
582 SDOperand Zero = DAG.getConstant(0, MVT::i32);
583
584 const TargetMachine &TM = DAG.getTarget();
585
586 // If this is a non-darwin platform, we don't support non-static relo models
587 // yet.
588 if (TM.getRelocationModel() == Reloc::Static ||
589 !TM.getSubtarget<PPCSubtarget>().isDarwin()) {
590 // Generate non-pic code that has direct accesses to the constant pool.
591 // The address of the global is just (hi(&g)+lo(&g)).
592 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
593 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
594 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
595 }
596
597 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
598 if (TM.getRelocationModel() == Reloc::PIC) {
599 // With PIC, the first instruction is actually "GR+hi(&G)".
600 Hi = DAG.getNode(ISD::ADD, MVT::i32,
601 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
602 }
603
604 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
605 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
606 return Lo;
607}
608
Nate Begeman37efe672006-04-22 18:53:45 +0000609static SDOperand LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
610 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
611 SDOperand JTI = DAG.getTargetJumpTable(JT->getIndex(), MVT::i32);
612 SDOperand Zero = DAG.getConstant(0, MVT::i32);
613
614 const TargetMachine &TM = DAG.getTarget();
615
616 // If this is a non-darwin platform, we don't support non-static relo models
617 // yet.
618 if (TM.getRelocationModel() == Reloc::Static ||
619 !TM.getSubtarget<PPCSubtarget>().isDarwin()) {
620 // Generate non-pic code that has direct accesses to the constant pool.
621 // The address of the global is just (hi(&g)+lo(&g)).
622 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, JTI, Zero);
623 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, JTI, Zero);
624 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
625 }
626
627 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, JTI, Zero);
628 if (TM.getRelocationModel() == Reloc::PIC) {
629 // With PIC, the first instruction is actually "GR+hi(&G)".
630 Hi = DAG.getNode(ISD::ADD, MVT::i32,
631 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
632 }
633
634 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, JTI, Zero);
635 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
636 return Lo;
637}
638
Chris Lattner1a635d62006-04-14 06:01:58 +0000639static SDOperand LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
640 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
641 GlobalValue *GV = GSDN->getGlobal();
642 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32, GSDN->getOffset());
643 SDOperand Zero = DAG.getConstant(0, MVT::i32);
644
645 const TargetMachine &TM = DAG.getTarget();
646
647 // If this is a non-darwin platform, we don't support non-static relo models
648 // yet.
649 if (TM.getRelocationModel() == Reloc::Static ||
650 !TM.getSubtarget<PPCSubtarget>().isDarwin()) {
651 // Generate non-pic code that has direct accesses to globals.
652 // The address of the global is just (hi(&g)+lo(&g)).
653 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
654 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
655 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
656 }
657
658 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
659 if (TM.getRelocationModel() == Reloc::PIC) {
660 // With PIC, the first instruction is actually "GR+hi(&G)".
661 Hi = DAG.getNode(ISD::ADD, MVT::i32,
662 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
663 }
664
665 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
666 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
667
668 if (!GV->hasWeakLinkage() && !GV->hasLinkOnceLinkage() &&
669 (!GV->isExternal() || GV->hasNotBeenReadFromBytecode()))
670 return Lo;
671
672 // If the global is weak or external, we have to go through the lazy
673 // resolution stub.
674 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), Lo, DAG.getSrcValue(0));
675}
676
677static SDOperand LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
678 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
679
680 // If we're comparing for equality to zero, expose the fact that this is
681 // implented as a ctlz/srl pair on ppc, so that the dag combiner can
682 // fold the new nodes.
683 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
684 if (C->isNullValue() && CC == ISD::SETEQ) {
685 MVT::ValueType VT = Op.getOperand(0).getValueType();
686 SDOperand Zext = Op.getOperand(0);
687 if (VT < MVT::i32) {
688 VT = MVT::i32;
689 Zext = DAG.getNode(ISD::ZERO_EXTEND, VT, Op.getOperand(0));
690 }
691 unsigned Log2b = Log2_32(MVT::getSizeInBits(VT));
692 SDOperand Clz = DAG.getNode(ISD::CTLZ, VT, Zext);
693 SDOperand Scc = DAG.getNode(ISD::SRL, VT, Clz,
694 DAG.getConstant(Log2b, MVT::i32));
695 return DAG.getNode(ISD::TRUNCATE, MVT::i32, Scc);
696 }
697 // Leave comparisons against 0 and -1 alone for now, since they're usually
698 // optimized. FIXME: revisit this when we can custom lower all setcc
699 // optimizations.
700 if (C->isAllOnesValue() || C->isNullValue())
701 return SDOperand();
702 }
703
704 // If we have an integer seteq/setne, turn it into a compare against zero
705 // by subtracting the rhs from the lhs, which is faster than setting a
706 // condition register, reading it back out, and masking the correct bit.
707 MVT::ValueType LHSVT = Op.getOperand(0).getValueType();
708 if (MVT::isInteger(LHSVT) && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
709 MVT::ValueType VT = Op.getValueType();
710 SDOperand Sub = DAG.getNode(ISD::SUB, LHSVT, Op.getOperand(0),
711 Op.getOperand(1));
712 return DAG.getSetCC(VT, Sub, DAG.getConstant(0, LHSVT), CC);
713 }
714 return SDOperand();
715}
716
717static SDOperand LowerVASTART(SDOperand Op, SelectionDAG &DAG,
718 unsigned VarArgsFrameIndex) {
719 // vastart just stores the address of the VarArgsFrameIndex slot into the
720 // memory location argument.
721 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
722 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
723 Op.getOperand(1), Op.getOperand(2));
724}
725
726static SDOperand LowerRET(SDOperand Op, SelectionDAG &DAG) {
727 SDOperand Copy;
728 switch(Op.getNumOperands()) {
729 default:
730 assert(0 && "Do not know how to return this many arguments!");
731 abort();
732 case 1:
733 return SDOperand(); // ret void is legal
734 case 2: {
735 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
736 unsigned ArgReg;
737 if (MVT::isVector(ArgVT))
738 ArgReg = PPC::V2;
739 else if (MVT::isInteger(ArgVT))
740 ArgReg = PPC::R3;
741 else {
742 assert(MVT::isFloatingPoint(ArgVT));
743 ArgReg = PPC::F1;
744 }
745
746 Copy = DAG.getCopyToReg(Op.getOperand(0), ArgReg, Op.getOperand(1),
747 SDOperand());
748
749 // If we haven't noted the R3/F1 are live out, do so now.
750 if (DAG.getMachineFunction().liveout_empty())
751 DAG.getMachineFunction().addLiveOut(ArgReg);
752 break;
753 }
754 case 3:
755 Copy = DAG.getCopyToReg(Op.getOperand(0), PPC::R3, Op.getOperand(2),
756 SDOperand());
757 Copy = DAG.getCopyToReg(Copy, PPC::R4, Op.getOperand(1),Copy.getValue(1));
758 // If we haven't noted the R3+R4 are live out, do so now.
759 if (DAG.getMachineFunction().liveout_empty()) {
760 DAG.getMachineFunction().addLiveOut(PPC::R3);
761 DAG.getMachineFunction().addLiveOut(PPC::R4);
762 }
763 break;
764 }
765 return DAG.getNode(PPCISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
766}
767
768/// LowerSELECT_CC - Lower floating point select_cc's into fsel instruction when
769/// possible.
770static SDOperand LowerSELECT_CC(SDOperand Op, SelectionDAG &DAG) {
771 // Not FP? Not a fsel.
772 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
773 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
774 return SDOperand();
775
776 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
777
778 // Cannot handle SETEQ/SETNE.
779 if (CC == ISD::SETEQ || CC == ISD::SETNE) return SDOperand();
780
781 MVT::ValueType ResVT = Op.getValueType();
782 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
783 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
784 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
785
786 // If the RHS of the comparison is a 0.0, we don't need to do the
787 // subtraction at all.
788 if (isFloatingPointZero(RHS))
789 switch (CC) {
790 default: break; // SETUO etc aren't handled by fsel.
791 case ISD::SETULT:
792 case ISD::SETLT:
793 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
794 case ISD::SETUGE:
795 case ISD::SETGE:
796 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
797 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
798 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
799 case ISD::SETUGT:
800 case ISD::SETGT:
801 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
802 case ISD::SETULE:
803 case ISD::SETLE:
804 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
805 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
806 return DAG.getNode(PPCISD::FSEL, ResVT,
807 DAG.getNode(ISD::FNEG, MVT::f64, LHS), TV, FV);
808 }
809
810 SDOperand Cmp;
811 switch (CC) {
812 default: break; // SETUO etc aren't handled by fsel.
813 case ISD::SETULT:
814 case ISD::SETLT:
815 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
816 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
817 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
818 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
819 case ISD::SETUGE:
820 case ISD::SETGE:
821 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
822 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
823 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
824 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
825 case ISD::SETUGT:
826 case ISD::SETGT:
827 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
828 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
829 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
830 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
831 case ISD::SETULE:
832 case ISD::SETLE:
833 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
834 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
835 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
836 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
837 }
838 return SDOperand();
839}
840
841static SDOperand LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
842 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
843 SDOperand Src = Op.getOperand(0);
844 if (Src.getValueType() == MVT::f32)
845 Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src);
846
847 SDOperand Tmp;
848 switch (Op.getValueType()) {
849 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
850 case MVT::i32:
851 Tmp = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
852 break;
853 case MVT::i64:
854 Tmp = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
855 break;
856 }
857
858 // Convert the FP value to an int value through memory.
859 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Tmp);
860 if (Op.getValueType() == MVT::i32)
861 Bits = DAG.getNode(ISD::TRUNCATE, MVT::i32, Bits);
862 return Bits;
863}
864
865static SDOperand LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
866 if (Op.getOperand(0).getValueType() == MVT::i64) {
867 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, Op.getOperand(0));
868 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Bits);
869 if (Op.getValueType() == MVT::f32)
870 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
871 return FP;
872 }
873
874 assert(Op.getOperand(0).getValueType() == MVT::i32 &&
875 "Unhandled SINT_TO_FP type in custom expander!");
876 // Since we only generate this in 64-bit mode, we can take advantage of
877 // 64-bit registers. In particular, sign extend the input value into the
878 // 64-bit register with extsw, store the WHOLE 64-bit value into the stack
879 // then lfd it and fcfid it.
880 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
881 int FrameIdx = FrameInfo->CreateStackObject(8, 8);
882 SDOperand FIdx = DAG.getFrameIndex(FrameIdx, MVT::i32);
883
884 SDOperand Ext64 = DAG.getNode(PPCISD::EXTSW_32, MVT::i32,
885 Op.getOperand(0));
886
887 // STD the extended value into the stack slot.
888 SDOperand Store = DAG.getNode(PPCISD::STD_32, MVT::Other,
889 DAG.getEntryNode(), Ext64, FIdx,
890 DAG.getSrcValue(NULL));
891 // Load the value as a double.
892 SDOperand Ld = DAG.getLoad(MVT::f64, Store, FIdx, DAG.getSrcValue(NULL));
893
894 // FCFID it and return it.
895 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Ld);
896 if (Op.getValueType() == MVT::f32)
897 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
898 return FP;
899}
900
901static SDOperand LowerSHL(SDOperand Op, SelectionDAG &DAG) {
902 assert(Op.getValueType() == MVT::i64 &&
903 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
904 // The generic code does a fine job expanding shift by a constant.
905 if (isa<ConstantSDNode>(Op.getOperand(1))) return SDOperand();
906
907 // Otherwise, expand into a bunch of logical ops. Note that these ops
908 // depend on the PPC behavior for oversized shift amounts.
909 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
910 DAG.getConstant(0, MVT::i32));
911 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
912 DAG.getConstant(1, MVT::i32));
913 SDOperand Amt = Op.getOperand(1);
914
915 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
916 DAG.getConstant(32, MVT::i32), Amt);
917 SDOperand Tmp2 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Amt);
918 SDOperand Tmp3 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Tmp1);
919 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
920 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
921 DAG.getConstant(-32U, MVT::i32));
922 SDOperand Tmp6 = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Tmp5);
923 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
924 SDOperand OutLo = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Amt);
925 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
926}
927
928static SDOperand LowerSRL(SDOperand Op, SelectionDAG &DAG) {
929 assert(Op.getValueType() == MVT::i64 &&
930 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
931 // The generic code does a fine job expanding shift by a constant.
932 if (isa<ConstantSDNode>(Op.getOperand(1))) return SDOperand();
933
934 // Otherwise, expand into a bunch of logical ops. Note that these ops
935 // depend on the PPC behavior for oversized shift amounts.
936 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
937 DAG.getConstant(0, MVT::i32));
938 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
939 DAG.getConstant(1, MVT::i32));
940 SDOperand Amt = Op.getOperand(1);
941
942 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
943 DAG.getConstant(32, MVT::i32), Amt);
944 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
945 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
946 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
947 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
948 DAG.getConstant(-32U, MVT::i32));
949 SDOperand Tmp6 = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Tmp5);
950 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
951 SDOperand OutHi = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Amt);
952 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
953}
954
955static SDOperand LowerSRA(SDOperand Op, SelectionDAG &DAG) {
956 assert(Op.getValueType() == MVT::i64 &&
957 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
958 // The generic code does a fine job expanding shift by a constant.
959 if (isa<ConstantSDNode>(Op.getOperand(1))) return SDOperand();
960
961 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
962 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
963 DAG.getConstant(0, MVT::i32));
964 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
965 DAG.getConstant(1, MVT::i32));
966 SDOperand Amt = Op.getOperand(1);
967
968 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
969 DAG.getConstant(32, MVT::i32), Amt);
970 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
971 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
972 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
973 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
974 DAG.getConstant(-32U, MVT::i32));
975 SDOperand Tmp6 = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Tmp5);
976 SDOperand OutHi = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Amt);
977 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
978 Tmp4, Tmp6, ISD::SETLE);
979 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
980}
981
982//===----------------------------------------------------------------------===//
983// Vector related lowering.
984//
985
Chris Lattnerac225ca2006-04-12 19:07:14 +0000986// If this is a vector of constants or undefs, get the bits. A bit in
987// UndefBits is set if the corresponding element of the vector is an
988// ISD::UNDEF value. For undefs, the corresponding VectorBits values are
989// zero. Return true if this is not an array of constants, false if it is.
990//
Chris Lattnerac225ca2006-04-12 19:07:14 +0000991static bool GetConstantBuildVectorBits(SDNode *BV, uint64_t VectorBits[2],
992 uint64_t UndefBits[2]) {
993 // Start with zero'd results.
994 VectorBits[0] = VectorBits[1] = UndefBits[0] = UndefBits[1] = 0;
995
996 unsigned EltBitSize = MVT::getSizeInBits(BV->getOperand(0).getValueType());
997 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
998 SDOperand OpVal = BV->getOperand(i);
999
1000 unsigned PartNo = i >= e/2; // In the upper 128 bits?
Chris Lattnerb17f1672006-04-16 01:01:29 +00001001 unsigned SlotNo = e/2 - (i & (e/2-1))-1; // Which subpiece of the uint64_t.
Chris Lattnerac225ca2006-04-12 19:07:14 +00001002
1003 uint64_t EltBits = 0;
1004 if (OpVal.getOpcode() == ISD::UNDEF) {
1005 uint64_t EltUndefBits = ~0U >> (32-EltBitSize);
1006 UndefBits[PartNo] |= EltUndefBits << (SlotNo*EltBitSize);
1007 continue;
1008 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
1009 EltBits = CN->getValue() & (~0U >> (32-EltBitSize));
1010 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) {
1011 assert(CN->getValueType(0) == MVT::f32 &&
1012 "Only one legal FP vector type!");
1013 EltBits = FloatToBits(CN->getValue());
1014 } else {
1015 // Nonconstant element.
1016 return true;
1017 }
1018
1019 VectorBits[PartNo] |= EltBits << (SlotNo*EltBitSize);
1020 }
1021
1022 //printf("%llx %llx %llx %llx\n",
1023 // VectorBits[0], VectorBits[1], UndefBits[0], UndefBits[1]);
1024 return false;
1025}
Chris Lattneref819f82006-03-20 06:33:01 +00001026
Chris Lattnerb17f1672006-04-16 01:01:29 +00001027// If this is a splat (repetition) of a value across the whole vector, return
1028// the smallest size that splats it. For example, "0x01010101010101..." is a
1029// splat of 0x01, 0x0101, and 0x01010101. We return SplatBits = 0x01 and
1030// SplatSize = 1 byte.
1031static bool isConstantSplat(const uint64_t Bits128[2],
1032 const uint64_t Undef128[2],
1033 unsigned &SplatBits, unsigned &SplatUndef,
1034 unsigned &SplatSize) {
1035
1036 // Don't let undefs prevent splats from matching. See if the top 64-bits are
1037 // the same as the lower 64-bits, ignoring undefs.
1038 if ((Bits128[0] & ~Undef128[1]) != (Bits128[1] & ~Undef128[0]))
1039 return false; // Can't be a splat if two pieces don't match.
1040
1041 uint64_t Bits64 = Bits128[0] | Bits128[1];
1042 uint64_t Undef64 = Undef128[0] & Undef128[1];
1043
1044 // Check that the top 32-bits are the same as the lower 32-bits, ignoring
1045 // undefs.
1046 if ((Bits64 & (~Undef64 >> 32)) != ((Bits64 >> 32) & ~Undef64))
1047 return false; // Can't be a splat if two pieces don't match.
1048
1049 uint32_t Bits32 = uint32_t(Bits64) | uint32_t(Bits64 >> 32);
1050 uint32_t Undef32 = uint32_t(Undef64) & uint32_t(Undef64 >> 32);
1051
1052 // If the top 16-bits are different than the lower 16-bits, ignoring
1053 // undefs, we have an i32 splat.
1054 if ((Bits32 & (~Undef32 >> 16)) != ((Bits32 >> 16) & ~Undef32)) {
1055 SplatBits = Bits32;
1056 SplatUndef = Undef32;
1057 SplatSize = 4;
1058 return true;
1059 }
1060
1061 uint16_t Bits16 = uint16_t(Bits32) | uint16_t(Bits32 >> 16);
1062 uint16_t Undef16 = uint16_t(Undef32) & uint16_t(Undef32 >> 16);
1063
1064 // If the top 8-bits are different than the lower 8-bits, ignoring
1065 // undefs, we have an i16 splat.
1066 if ((Bits16 & (uint16_t(~Undef16) >> 8)) != ((Bits16 >> 8) & ~Undef16)) {
1067 SplatBits = Bits16;
1068 SplatUndef = Undef16;
1069 SplatSize = 2;
1070 return true;
1071 }
1072
1073 // Otherwise, we have an 8-bit splat.
1074 SplatBits = uint8_t(Bits16) | uint8_t(Bits16 >> 8);
1075 SplatUndef = uint8_t(Undef16) & uint8_t(Undef16 >> 8);
1076 SplatSize = 1;
1077 return true;
1078}
1079
Chris Lattner4a998b92006-04-17 06:00:21 +00001080/// BuildSplatI - Build a canonical splati of Val with an element size of
1081/// SplatSize. Cast the result to VT.
1082static SDOperand BuildSplatI(int Val, unsigned SplatSize, MVT::ValueType VT,
1083 SelectionDAG &DAG) {
1084 assert(Val >= -16 && Val <= 15 && "vsplti is out of range!");
Chris Lattner6876e662006-04-17 06:58:41 +00001085
1086 // Force vspltis[hw] -1 to vspltisb -1.
1087 if (Val == -1) SplatSize = 1;
1088
Chris Lattner4a998b92006-04-17 06:00:21 +00001089 static const MVT::ValueType VTys[] = { // canonical VT to use for each size.
1090 MVT::v16i8, MVT::v8i16, MVT::Other, MVT::v4i32
1091 };
1092 MVT::ValueType CanonicalVT = VTys[SplatSize-1];
1093
1094 // Build a canonical splat for this value.
1095 SDOperand Elt = DAG.getConstant(Val, MVT::getVectorBaseType(CanonicalVT));
1096 std::vector<SDOperand> Ops(MVT::getVectorNumElements(CanonicalVT), Elt);
1097 SDOperand Res = DAG.getNode(ISD::BUILD_VECTOR, CanonicalVT, Ops);
1098 return DAG.getNode(ISD::BIT_CONVERT, VT, Res);
1099}
1100
Chris Lattnere7c768e2006-04-18 03:24:30 +00001101/// BuildIntrinsicOp - Return a binary operator intrinsic node with the
Chris Lattner6876e662006-04-17 06:58:41 +00001102/// specified intrinsic ID.
Chris Lattnere7c768e2006-04-18 03:24:30 +00001103static SDOperand BuildIntrinsicOp(unsigned IID, SDOperand LHS, SDOperand RHS,
1104 SelectionDAG &DAG,
1105 MVT::ValueType DestVT = MVT::Other) {
1106 if (DestVT == MVT::Other) DestVT = LHS.getValueType();
1107 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DestVT,
Chris Lattner6876e662006-04-17 06:58:41 +00001108 DAG.getConstant(IID, MVT::i32), LHS, RHS);
1109}
1110
Chris Lattnere7c768e2006-04-18 03:24:30 +00001111/// BuildIntrinsicOp - Return a ternary operator intrinsic node with the
1112/// specified intrinsic ID.
1113static SDOperand BuildIntrinsicOp(unsigned IID, SDOperand Op0, SDOperand Op1,
1114 SDOperand Op2, SelectionDAG &DAG,
1115 MVT::ValueType DestVT = MVT::Other) {
1116 if (DestVT == MVT::Other) DestVT = Op0.getValueType();
1117 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DestVT,
1118 DAG.getConstant(IID, MVT::i32), Op0, Op1, Op2);
1119}
1120
1121
Chris Lattnerbdd558c2006-04-17 17:55:10 +00001122/// BuildVSLDOI - Return a VECTOR_SHUFFLE that is a vsldoi of the specified
1123/// amount. The result has the specified value type.
1124static SDOperand BuildVSLDOI(SDOperand LHS, SDOperand RHS, unsigned Amt,
1125 MVT::ValueType VT, SelectionDAG &DAG) {
1126 // Force LHS/RHS to be the right type.
1127 LHS = DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, LHS);
1128 RHS = DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, RHS);
1129
1130 std::vector<SDOperand> Ops;
1131 for (unsigned i = 0; i != 16; ++i)
1132 Ops.push_back(DAG.getConstant(i+Amt, MVT::i32));
1133 SDOperand T = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v16i8, LHS, RHS,
1134 DAG.getNode(ISD::BUILD_VECTOR, MVT::v16i8, Ops));
1135 return DAG.getNode(ISD::BIT_CONVERT, VT, T);
1136}
1137
Chris Lattnerf1b47082006-04-14 05:19:18 +00001138// If this is a case we can't handle, return null and let the default
1139// expansion code take care of it. If we CAN select this case, and if it
1140// selects to a single instruction, return Op. Otherwise, if we can codegen
1141// this case more efficiently than a constant pool load, lower it to the
1142// sequence of ops that should be used.
1143static SDOperand LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
1144 // If this is a vector of constants or undefs, get the bits. A bit in
1145 // UndefBits is set if the corresponding element of the vector is an
1146 // ISD::UNDEF value. For undefs, the corresponding VectorBits values are
1147 // zero.
1148 uint64_t VectorBits[2];
1149 uint64_t UndefBits[2];
1150 if (GetConstantBuildVectorBits(Op.Val, VectorBits, UndefBits))
1151 return SDOperand(); // Not a constant vector.
1152
Chris Lattnerb17f1672006-04-16 01:01:29 +00001153 // If this is a splat (repetition) of a value across the whole vector, return
1154 // the smallest size that splats it. For example, "0x01010101010101..." is a
1155 // splat of 0x01, 0x0101, and 0x01010101. We return SplatBits = 0x01 and
1156 // SplatSize = 1 byte.
1157 unsigned SplatBits, SplatUndef, SplatSize;
1158 if (isConstantSplat(VectorBits, UndefBits, SplatBits, SplatUndef, SplatSize)){
1159 bool HasAnyUndefs = (UndefBits[0] | UndefBits[1]) != 0;
1160
1161 // First, handle single instruction cases.
1162
1163 // All zeros?
1164 if (SplatBits == 0) {
1165 // Canonicalize all zero vectors to be v4i32.
1166 if (Op.getValueType() != MVT::v4i32 || HasAnyUndefs) {
1167 SDOperand Z = DAG.getConstant(0, MVT::i32);
1168 Z = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Z, Z, Z, Z);
1169 Op = DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Z);
1170 }
1171 return Op;
Chris Lattnerf1b47082006-04-14 05:19:18 +00001172 }
Chris Lattnerb17f1672006-04-16 01:01:29 +00001173
1174 // If the sign extended value is in the range [-16,15], use VSPLTI[bhw].
1175 int32_t SextVal= int32_t(SplatBits << (32-8*SplatSize)) >> (32-8*SplatSize);
Chris Lattner4a998b92006-04-17 06:00:21 +00001176 if (SextVal >= -16 && SextVal <= 15)
1177 return BuildSplatI(SextVal, SplatSize, Op.getValueType(), DAG);
Chris Lattnerb17f1672006-04-16 01:01:29 +00001178
Chris Lattnerdbce85d2006-04-17 18:09:22 +00001179
1180 // Two instruction sequences.
1181
Chris Lattner4a998b92006-04-17 06:00:21 +00001182 // If this value is in the range [-32,30] and is even, use:
1183 // tmp = VSPLTI[bhw], result = add tmp, tmp
1184 if (SextVal >= -32 && SextVal <= 30 && (SextVal & 1) == 0) {
1185 Op = BuildSplatI(SextVal >> 1, SplatSize, Op.getValueType(), DAG);
1186 return DAG.getNode(ISD::ADD, Op.getValueType(), Op, Op);
1187 }
Chris Lattner6876e662006-04-17 06:58:41 +00001188
1189 // If this is 0x8000_0000 x 4, turn into vspltisw + vslw. If it is
1190 // 0x7FFF_FFFF x 4, turn it into not(0x8000_0000). This is important
1191 // for fneg/fabs.
1192 if (SplatSize == 4 && SplatBits == (0x7FFFFFFF&~SplatUndef)) {
1193 // Make -1 and vspltisw -1:
1194 SDOperand OnesV = BuildSplatI(-1, 4, MVT::v4i32, DAG);
1195
1196 // Make the VSLW intrinsic, computing 0x8000_0000.
Chris Lattnere7c768e2006-04-18 03:24:30 +00001197 SDOperand Res = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, OnesV,
1198 OnesV, DAG);
Chris Lattner6876e662006-04-17 06:58:41 +00001199
1200 // xor by OnesV to invert it.
1201 Res = DAG.getNode(ISD::XOR, MVT::v4i32, Res, OnesV);
1202 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Res);
1203 }
1204
1205 // Check to see if this is a wide variety of vsplti*, binop self cases.
1206 unsigned SplatBitSize = SplatSize*8;
1207 static const char SplatCsts[] = {
1208 -1, 1, -2, 2, -3, 3, -4, 4, -5, 5, -6, 6, -7, 7,
Chris Lattnerdbce85d2006-04-17 18:09:22 +00001209 -8, 8, -9, 9, -10, 10, -11, 11, -12, 12, -13, 13, 14, -14, 15, -15, -16
Chris Lattner6876e662006-04-17 06:58:41 +00001210 };
1211 for (unsigned idx = 0; idx < sizeof(SplatCsts)/sizeof(SplatCsts[0]); ++idx){
1212 // Indirect through the SplatCsts array so that we favor 'vsplti -1' for
1213 // cases which are ambiguous (e.g. formation of 0x8000_0000). 'vsplti -1'
1214 int i = SplatCsts[idx];
1215
1216 // Figure out what shift amount will be used by altivec if shifted by i in
1217 // this splat size.
1218 unsigned TypeShiftAmt = i & (SplatBitSize-1);
1219
1220 // vsplti + shl self.
1221 if (SextVal == (i << (int)TypeShiftAmt)) {
1222 Op = BuildSplatI(i, SplatSize, Op.getValueType(), DAG);
1223 static const unsigned IIDs[] = { // Intrinsic to use for each size.
1224 Intrinsic::ppc_altivec_vslb, Intrinsic::ppc_altivec_vslh, 0,
1225 Intrinsic::ppc_altivec_vslw
1226 };
Chris Lattnere7c768e2006-04-18 03:24:30 +00001227 return BuildIntrinsicOp(IIDs[SplatSize-1], Op, Op, DAG);
Chris Lattner6876e662006-04-17 06:58:41 +00001228 }
1229
1230 // vsplti + srl self.
1231 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
1232 Op = BuildSplatI(i, SplatSize, Op.getValueType(), DAG);
1233 static const unsigned IIDs[] = { // Intrinsic to use for each size.
1234 Intrinsic::ppc_altivec_vsrb, Intrinsic::ppc_altivec_vsrh, 0,
1235 Intrinsic::ppc_altivec_vsrw
1236 };
Chris Lattnere7c768e2006-04-18 03:24:30 +00001237 return BuildIntrinsicOp(IIDs[SplatSize-1], Op, Op, DAG);
Chris Lattner6876e662006-04-17 06:58:41 +00001238 }
1239
1240 // vsplti + sra self.
1241 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
1242 Op = BuildSplatI(i, SplatSize, Op.getValueType(), DAG);
1243 static const unsigned IIDs[] = { // Intrinsic to use for each size.
1244 Intrinsic::ppc_altivec_vsrab, Intrinsic::ppc_altivec_vsrah, 0,
1245 Intrinsic::ppc_altivec_vsraw
1246 };
Chris Lattnere7c768e2006-04-18 03:24:30 +00001247 return BuildIntrinsicOp(IIDs[SplatSize-1], Op, Op, DAG);
Chris Lattner6876e662006-04-17 06:58:41 +00001248 }
1249
Chris Lattnerbdd558c2006-04-17 17:55:10 +00001250 // vsplti + rol self.
1251 if (SextVal == (int)(((unsigned)i << TypeShiftAmt) |
1252 ((unsigned)i >> (SplatBitSize-TypeShiftAmt)))) {
1253 Op = BuildSplatI(i, SplatSize, Op.getValueType(), DAG);
1254 static const unsigned IIDs[] = { // Intrinsic to use for each size.
1255 Intrinsic::ppc_altivec_vrlb, Intrinsic::ppc_altivec_vrlh, 0,
1256 Intrinsic::ppc_altivec_vrlw
1257 };
Chris Lattnere7c768e2006-04-18 03:24:30 +00001258 return BuildIntrinsicOp(IIDs[SplatSize-1], Op, Op, DAG);
Chris Lattnerbdd558c2006-04-17 17:55:10 +00001259 }
1260
1261 // t = vsplti c, result = vsldoi t, t, 1
1262 if (SextVal == ((i << 8) | (i >> (TypeShiftAmt-8)))) {
1263 SDOperand T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG);
1264 return BuildVSLDOI(T, T, 1, Op.getValueType(), DAG);
1265 }
1266 // t = vsplti c, result = vsldoi t, t, 2
1267 if (SextVal == ((i << 16) | (i >> (TypeShiftAmt-16)))) {
1268 SDOperand T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG);
1269 return BuildVSLDOI(T, T, 2, Op.getValueType(), DAG);
1270 }
1271 // t = vsplti c, result = vsldoi t, t, 3
1272 if (SextVal == ((i << 24) | (i >> (TypeShiftAmt-24)))) {
1273 SDOperand T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG);
1274 return BuildVSLDOI(T, T, 3, Op.getValueType(), DAG);
1275 }
Chris Lattner6876e662006-04-17 06:58:41 +00001276 }
1277
Chris Lattner6876e662006-04-17 06:58:41 +00001278 // Three instruction sequences.
1279
Chris Lattnerdbce85d2006-04-17 18:09:22 +00001280 // Odd, in range [17,31]: (vsplti C)-(vsplti -16).
1281 if (SextVal >= 0 && SextVal <= 31) {
1282 SDOperand LHS = BuildSplatI(SextVal-16, SplatSize, Op.getValueType(),DAG);
1283 SDOperand RHS = BuildSplatI(-16, SplatSize, Op.getValueType(), DAG);
1284 return DAG.getNode(ISD::SUB, Op.getValueType(), LHS, RHS);
1285 }
1286 // Odd, in range [-31,-17]: (vsplti C)+(vsplti -16).
1287 if (SextVal >= -31 && SextVal <= 0) {
1288 SDOperand LHS = BuildSplatI(SextVal+16, SplatSize, Op.getValueType(),DAG);
1289 SDOperand RHS = BuildSplatI(-16, SplatSize, Op.getValueType(), DAG);
Chris Lattnerc4083822006-04-17 06:07:44 +00001290 return DAG.getNode(ISD::ADD, Op.getValueType(), LHS, RHS);
Chris Lattnerf1b47082006-04-14 05:19:18 +00001291 }
1292 }
Chris Lattnerb17f1672006-04-16 01:01:29 +00001293
Chris Lattnerf1b47082006-04-14 05:19:18 +00001294 return SDOperand();
1295}
1296
Chris Lattner59138102006-04-17 05:28:54 +00001297/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
1298/// the specified operations to build the shuffle.
1299static SDOperand GeneratePerfectShuffle(unsigned PFEntry, SDOperand LHS,
1300 SDOperand RHS, SelectionDAG &DAG) {
1301 unsigned OpNum = (PFEntry >> 26) & 0x0F;
1302 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
1303 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
1304
1305 enum {
1306 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
1307 OP_VMRGHW,
1308 OP_VMRGLW,
1309 OP_VSPLTISW0,
1310 OP_VSPLTISW1,
1311 OP_VSPLTISW2,
1312 OP_VSPLTISW3,
1313 OP_VSLDOI4,
1314 OP_VSLDOI8,
1315 OP_VSLDOI12,
1316 };
1317
1318 if (OpNum == OP_COPY) {
1319 if (LHSID == (1*9+2)*9+3) return LHS;
1320 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
1321 return RHS;
1322 }
1323
Chris Lattnerbdd558c2006-04-17 17:55:10 +00001324 SDOperand OpLHS, OpRHS;
1325 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG);
1326 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG);
1327
Chris Lattner59138102006-04-17 05:28:54 +00001328 unsigned ShufIdxs[16];
1329 switch (OpNum) {
1330 default: assert(0 && "Unknown i32 permute!");
1331 case OP_VMRGHW:
1332 ShufIdxs[ 0] = 0; ShufIdxs[ 1] = 1; ShufIdxs[ 2] = 2; ShufIdxs[ 3] = 3;
1333 ShufIdxs[ 4] = 16; ShufIdxs[ 5] = 17; ShufIdxs[ 6] = 18; ShufIdxs[ 7] = 19;
1334 ShufIdxs[ 8] = 4; ShufIdxs[ 9] = 5; ShufIdxs[10] = 6; ShufIdxs[11] = 7;
1335 ShufIdxs[12] = 20; ShufIdxs[13] = 21; ShufIdxs[14] = 22; ShufIdxs[15] = 23;
1336 break;
1337 case OP_VMRGLW:
1338 ShufIdxs[ 0] = 8; ShufIdxs[ 1] = 9; ShufIdxs[ 2] = 10; ShufIdxs[ 3] = 11;
1339 ShufIdxs[ 4] = 24; ShufIdxs[ 5] = 25; ShufIdxs[ 6] = 26; ShufIdxs[ 7] = 27;
1340 ShufIdxs[ 8] = 12; ShufIdxs[ 9] = 13; ShufIdxs[10] = 14; ShufIdxs[11] = 15;
1341 ShufIdxs[12] = 28; ShufIdxs[13] = 29; ShufIdxs[14] = 30; ShufIdxs[15] = 31;
1342 break;
1343 case OP_VSPLTISW0:
1344 for (unsigned i = 0; i != 16; ++i)
1345 ShufIdxs[i] = (i&3)+0;
1346 break;
1347 case OP_VSPLTISW1:
1348 for (unsigned i = 0; i != 16; ++i)
1349 ShufIdxs[i] = (i&3)+4;
1350 break;
1351 case OP_VSPLTISW2:
1352 for (unsigned i = 0; i != 16; ++i)
1353 ShufIdxs[i] = (i&3)+8;
1354 break;
1355 case OP_VSPLTISW3:
1356 for (unsigned i = 0; i != 16; ++i)
1357 ShufIdxs[i] = (i&3)+12;
1358 break;
1359 case OP_VSLDOI4:
Chris Lattnerbdd558c2006-04-17 17:55:10 +00001360 return BuildVSLDOI(OpLHS, OpRHS, 4, OpLHS.getValueType(), DAG);
Chris Lattner59138102006-04-17 05:28:54 +00001361 case OP_VSLDOI8:
Chris Lattnerbdd558c2006-04-17 17:55:10 +00001362 return BuildVSLDOI(OpLHS, OpRHS, 8, OpLHS.getValueType(), DAG);
Chris Lattner59138102006-04-17 05:28:54 +00001363 case OP_VSLDOI12:
Chris Lattnerbdd558c2006-04-17 17:55:10 +00001364 return BuildVSLDOI(OpLHS, OpRHS, 12, OpLHS.getValueType(), DAG);
Chris Lattner59138102006-04-17 05:28:54 +00001365 }
1366 std::vector<SDOperand> Ops;
1367 for (unsigned i = 0; i != 16; ++i)
1368 Ops.push_back(DAG.getConstant(ShufIdxs[i], MVT::i32));
Chris Lattner59138102006-04-17 05:28:54 +00001369
1370 return DAG.getNode(ISD::VECTOR_SHUFFLE, OpLHS.getValueType(), OpLHS, OpRHS,
1371 DAG.getNode(ISD::BUILD_VECTOR, MVT::v16i8, Ops));
1372}
1373
Chris Lattnerf1b47082006-04-14 05:19:18 +00001374/// LowerVECTOR_SHUFFLE - Return the code we lower for VECTOR_SHUFFLE. If this
1375/// is a shuffle we can handle in a single instruction, return it. Otherwise,
1376/// return the code it can be lowered into. Worst case, it can always be
1377/// lowered into a vperm.
1378static SDOperand LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
1379 SDOperand V1 = Op.getOperand(0);
1380 SDOperand V2 = Op.getOperand(1);
1381 SDOperand PermMask = Op.getOperand(2);
1382
1383 // Cases that are handled by instructions that take permute immediates
1384 // (such as vsplt*) should be left as VECTOR_SHUFFLE nodes so they can be
1385 // selected by the instruction selector.
1386 if (V2.getOpcode() == ISD::UNDEF) {
1387 if (PPC::isSplatShuffleMask(PermMask.Val, 1) ||
1388 PPC::isSplatShuffleMask(PermMask.Val, 2) ||
1389 PPC::isSplatShuffleMask(PermMask.Val, 4) ||
1390 PPC::isVPKUWUMShuffleMask(PermMask.Val, true) ||
1391 PPC::isVPKUHUMShuffleMask(PermMask.Val, true) ||
1392 PPC::isVSLDOIShuffleMask(PermMask.Val, true) != -1 ||
1393 PPC::isVMRGLShuffleMask(PermMask.Val, 1, true) ||
1394 PPC::isVMRGLShuffleMask(PermMask.Val, 2, true) ||
1395 PPC::isVMRGLShuffleMask(PermMask.Val, 4, true) ||
1396 PPC::isVMRGHShuffleMask(PermMask.Val, 1, true) ||
1397 PPC::isVMRGHShuffleMask(PermMask.Val, 2, true) ||
1398 PPC::isVMRGHShuffleMask(PermMask.Val, 4, true)) {
1399 return Op;
1400 }
1401 }
1402
1403 // Altivec has a variety of "shuffle immediates" that take two vector inputs
1404 // and produce a fixed permutation. If any of these match, do not lower to
1405 // VPERM.
1406 if (PPC::isVPKUWUMShuffleMask(PermMask.Val, false) ||
1407 PPC::isVPKUHUMShuffleMask(PermMask.Val, false) ||
1408 PPC::isVSLDOIShuffleMask(PermMask.Val, false) != -1 ||
1409 PPC::isVMRGLShuffleMask(PermMask.Val, 1, false) ||
1410 PPC::isVMRGLShuffleMask(PermMask.Val, 2, false) ||
1411 PPC::isVMRGLShuffleMask(PermMask.Val, 4, false) ||
1412 PPC::isVMRGHShuffleMask(PermMask.Val, 1, false) ||
1413 PPC::isVMRGHShuffleMask(PermMask.Val, 2, false) ||
1414 PPC::isVMRGHShuffleMask(PermMask.Val, 4, false))
1415 return Op;
1416
Chris Lattner59138102006-04-17 05:28:54 +00001417 // Check to see if this is a shuffle of 4-byte values. If so, we can use our
1418 // perfect shuffle table to emit an optimal matching sequence.
1419 unsigned PFIndexes[4];
1420 bool isFourElementShuffle = true;
1421 for (unsigned i = 0; i != 4 && isFourElementShuffle; ++i) { // Element number
1422 unsigned EltNo = 8; // Start out undef.
1423 for (unsigned j = 0; j != 4; ++j) { // Intra-element byte.
1424 if (PermMask.getOperand(i*4+j).getOpcode() == ISD::UNDEF)
1425 continue; // Undef, ignore it.
1426
1427 unsigned ByteSource =
1428 cast<ConstantSDNode>(PermMask.getOperand(i*4+j))->getValue();
1429 if ((ByteSource & 3) != j) {
1430 isFourElementShuffle = false;
1431 break;
1432 }
1433
1434 if (EltNo == 8) {
1435 EltNo = ByteSource/4;
1436 } else if (EltNo != ByteSource/4) {
1437 isFourElementShuffle = false;
1438 break;
1439 }
1440 }
1441 PFIndexes[i] = EltNo;
1442 }
1443
1444 // If this shuffle can be expressed as a shuffle of 4-byte elements, use the
1445 // perfect shuffle vector to determine if it is cost effective to do this as
1446 // discrete instructions, or whether we should use a vperm.
1447 if (isFourElementShuffle) {
1448 // Compute the index in the perfect shuffle table.
1449 unsigned PFTableIndex =
1450 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
1451
1452 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
1453 unsigned Cost = (PFEntry >> 30);
1454
1455 // Determining when to avoid vperm is tricky. Many things affect the cost
1456 // of vperm, particularly how many times the perm mask needs to be computed.
1457 // For example, if the perm mask can be hoisted out of a loop or is already
1458 // used (perhaps because there are multiple permutes with the same shuffle
1459 // mask?) the vperm has a cost of 1. OTOH, hoisting the permute mask out of
1460 // the loop requires an extra register.
1461 //
1462 // As a compromise, we only emit discrete instructions if the shuffle can be
1463 // generated in 3 or fewer operations. When we have loop information
1464 // available, if this block is within a loop, we should avoid using vperm
1465 // for 3-operation perms and use a constant pool load instead.
1466 if (Cost < 3)
1467 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG);
1468 }
Chris Lattnerf1b47082006-04-14 05:19:18 +00001469
1470 // Lower this to a VPERM(V1, V2, V3) expression, where V3 is a constant
1471 // vector that will get spilled to the constant pool.
1472 if (V2.getOpcode() == ISD::UNDEF) V2 = V1;
1473
1474 // The SHUFFLE_VECTOR mask is almost exactly what we want for vperm, except
1475 // that it is in input element units, not in bytes. Convert now.
1476 MVT::ValueType EltVT = MVT::getVectorBaseType(V1.getValueType());
1477 unsigned BytesPerElement = MVT::getSizeInBits(EltVT)/8;
1478
1479 std::vector<SDOperand> ResultMask;
1480 for (unsigned i = 0, e = PermMask.getNumOperands(); i != e; ++i) {
Chris Lattner730b4562006-04-15 23:48:05 +00001481 unsigned SrcElt;
1482 if (PermMask.getOperand(i).getOpcode() == ISD::UNDEF)
1483 SrcElt = 0;
1484 else
1485 SrcElt = cast<ConstantSDNode>(PermMask.getOperand(i))->getValue();
Chris Lattnerf1b47082006-04-14 05:19:18 +00001486
1487 for (unsigned j = 0; j != BytesPerElement; ++j)
1488 ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement+j,
1489 MVT::i8));
1490 }
1491
1492 SDOperand VPermMask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v16i8, ResultMask);
1493 return DAG.getNode(PPCISD::VPERM, V1.getValueType(), V1, V2, VPermMask);
1494}
1495
Chris Lattner90564f22006-04-18 17:59:36 +00001496/// getAltivecCompareInfo - Given an intrinsic, return false if it is not an
1497/// altivec comparison. If it is, return true and fill in Opc/isDot with
1498/// information about the intrinsic.
1499static bool getAltivecCompareInfo(SDOperand Intrin, int &CompareOpc,
1500 bool &isDot) {
1501 unsigned IntrinsicID = cast<ConstantSDNode>(Intrin.getOperand(0))->getValue();
1502 CompareOpc = -1;
1503 isDot = false;
1504 switch (IntrinsicID) {
1505 default: return false;
1506 // Comparison predicates.
Chris Lattner1a635d62006-04-14 06:01:58 +00001507 case Intrinsic::ppc_altivec_vcmpbfp_p: CompareOpc = 966; isDot = 1; break;
1508 case Intrinsic::ppc_altivec_vcmpeqfp_p: CompareOpc = 198; isDot = 1; break;
1509 case Intrinsic::ppc_altivec_vcmpequb_p: CompareOpc = 6; isDot = 1; break;
1510 case Intrinsic::ppc_altivec_vcmpequh_p: CompareOpc = 70; isDot = 1; break;
1511 case Intrinsic::ppc_altivec_vcmpequw_p: CompareOpc = 134; isDot = 1; break;
1512 case Intrinsic::ppc_altivec_vcmpgefp_p: CompareOpc = 454; isDot = 1; break;
1513 case Intrinsic::ppc_altivec_vcmpgtfp_p: CompareOpc = 710; isDot = 1; break;
1514 case Intrinsic::ppc_altivec_vcmpgtsb_p: CompareOpc = 774; isDot = 1; break;
1515 case Intrinsic::ppc_altivec_vcmpgtsh_p: CompareOpc = 838; isDot = 1; break;
1516 case Intrinsic::ppc_altivec_vcmpgtsw_p: CompareOpc = 902; isDot = 1; break;
1517 case Intrinsic::ppc_altivec_vcmpgtub_p: CompareOpc = 518; isDot = 1; break;
1518 case Intrinsic::ppc_altivec_vcmpgtuh_p: CompareOpc = 582; isDot = 1; break;
1519 case Intrinsic::ppc_altivec_vcmpgtuw_p: CompareOpc = 646; isDot = 1; break;
1520
1521 // Normal Comparisons.
1522 case Intrinsic::ppc_altivec_vcmpbfp: CompareOpc = 966; isDot = 0; break;
1523 case Intrinsic::ppc_altivec_vcmpeqfp: CompareOpc = 198; isDot = 0; break;
1524 case Intrinsic::ppc_altivec_vcmpequb: CompareOpc = 6; isDot = 0; break;
1525 case Intrinsic::ppc_altivec_vcmpequh: CompareOpc = 70; isDot = 0; break;
1526 case Intrinsic::ppc_altivec_vcmpequw: CompareOpc = 134; isDot = 0; break;
1527 case Intrinsic::ppc_altivec_vcmpgefp: CompareOpc = 454; isDot = 0; break;
1528 case Intrinsic::ppc_altivec_vcmpgtfp: CompareOpc = 710; isDot = 0; break;
1529 case Intrinsic::ppc_altivec_vcmpgtsb: CompareOpc = 774; isDot = 0; break;
1530 case Intrinsic::ppc_altivec_vcmpgtsh: CompareOpc = 838; isDot = 0; break;
1531 case Intrinsic::ppc_altivec_vcmpgtsw: CompareOpc = 902; isDot = 0; break;
1532 case Intrinsic::ppc_altivec_vcmpgtub: CompareOpc = 518; isDot = 0; break;
1533 case Intrinsic::ppc_altivec_vcmpgtuh: CompareOpc = 582; isDot = 0; break;
1534 case Intrinsic::ppc_altivec_vcmpgtuw: CompareOpc = 646; isDot = 0; break;
1535 }
Chris Lattner90564f22006-04-18 17:59:36 +00001536 return true;
1537}
1538
1539/// LowerINTRINSIC_WO_CHAIN - If this is an intrinsic that we want to custom
1540/// lower, do it, otherwise return null.
1541static SDOperand LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
1542 // If this is a lowered altivec predicate compare, CompareOpc is set to the
1543 // opcode number of the comparison.
1544 int CompareOpc;
1545 bool isDot;
1546 if (!getAltivecCompareInfo(Op, CompareOpc, isDot))
1547 return SDOperand(); // Don't custom lower most intrinsics.
Chris Lattner1a635d62006-04-14 06:01:58 +00001548
Chris Lattner90564f22006-04-18 17:59:36 +00001549 // If this is a non-dot comparison, make the VCMP node and we are done.
Chris Lattner1a635d62006-04-14 06:01:58 +00001550 if (!isDot) {
1551 SDOperand Tmp = DAG.getNode(PPCISD::VCMP, Op.getOperand(2).getValueType(),
1552 Op.getOperand(1), Op.getOperand(2),
1553 DAG.getConstant(CompareOpc, MVT::i32));
1554 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Tmp);
1555 }
1556
1557 // Create the PPCISD altivec 'dot' comparison node.
1558 std::vector<SDOperand> Ops;
1559 std::vector<MVT::ValueType> VTs;
1560 Ops.push_back(Op.getOperand(2)); // LHS
1561 Ops.push_back(Op.getOperand(3)); // RHS
1562 Ops.push_back(DAG.getConstant(CompareOpc, MVT::i32));
1563 VTs.push_back(Op.getOperand(2).getValueType());
1564 VTs.push_back(MVT::Flag);
1565 SDOperand CompNode = DAG.getNode(PPCISD::VCMPo, VTs, Ops);
1566
1567 // Now that we have the comparison, emit a copy from the CR to a GPR.
1568 // This is flagged to the above dot comparison.
1569 SDOperand Flags = DAG.getNode(PPCISD::MFCR, MVT::i32,
1570 DAG.getRegister(PPC::CR6, MVT::i32),
1571 CompNode.getValue(1));
1572
1573 // Unpack the result based on how the target uses it.
1574 unsigned BitNo; // Bit # of CR6.
1575 bool InvertBit; // Invert result?
1576 switch (cast<ConstantSDNode>(Op.getOperand(1))->getValue()) {
1577 default: // Can't happen, don't crash on invalid number though.
1578 case 0: // Return the value of the EQ bit of CR6.
1579 BitNo = 0; InvertBit = false;
1580 break;
1581 case 1: // Return the inverted value of the EQ bit of CR6.
1582 BitNo = 0; InvertBit = true;
1583 break;
1584 case 2: // Return the value of the LT bit of CR6.
1585 BitNo = 2; InvertBit = false;
1586 break;
1587 case 3: // Return the inverted value of the LT bit of CR6.
1588 BitNo = 2; InvertBit = true;
1589 break;
1590 }
1591
1592 // Shift the bit into the low position.
1593 Flags = DAG.getNode(ISD::SRL, MVT::i32, Flags,
1594 DAG.getConstant(8-(3-BitNo), MVT::i32));
1595 // Isolate the bit.
1596 Flags = DAG.getNode(ISD::AND, MVT::i32, Flags,
1597 DAG.getConstant(1, MVT::i32));
1598
1599 // If we are supposed to, toggle the bit.
1600 if (InvertBit)
1601 Flags = DAG.getNode(ISD::XOR, MVT::i32, Flags,
1602 DAG.getConstant(1, MVT::i32));
1603 return Flags;
1604}
1605
1606static SDOperand LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
1607 // Create a stack slot that is 16-byte aligned.
1608 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
1609 int FrameIdx = FrameInfo->CreateStackObject(16, 16);
1610 SDOperand FIdx = DAG.getFrameIndex(FrameIdx, MVT::i32);
1611
1612 // Store the input value into Value#0 of the stack slot.
1613 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
1614 Op.getOperand(0), FIdx,DAG.getSrcValue(NULL));
1615 // Load it out.
1616 return DAG.getLoad(Op.getValueType(), Store, FIdx, DAG.getSrcValue(NULL));
1617}
1618
Chris Lattnere7c768e2006-04-18 03:24:30 +00001619static SDOperand LowerMUL(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner72dd9bd2006-04-18 03:43:48 +00001620 if (Op.getValueType() == MVT::v4i32) {
1621 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
1622
1623 SDOperand Zero = BuildSplatI( 0, 1, MVT::v4i32, DAG);
1624 SDOperand Neg16 = BuildSplatI(-16, 4, MVT::v4i32, DAG); // +16 as shift amt.
1625
1626 SDOperand RHSSwap = // = vrlw RHS, 16
1627 BuildIntrinsicOp(Intrinsic::ppc_altivec_vrlw, RHS, Neg16, DAG);
1628
1629 // Shrinkify inputs to v8i16.
1630 LHS = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, LHS);
1631 RHS = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, RHS);
1632 RHSSwap = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, RHSSwap);
1633
1634 // Low parts multiplied together, generating 32-bit results (we ignore the
1635 // top parts).
1636 SDOperand LoProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmulouh,
1637 LHS, RHS, DAG, MVT::v4i32);
1638
1639 SDOperand HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmsumuhm,
1640 LHS, RHSSwap, Zero, DAG, MVT::v4i32);
1641 // Shift the high parts up 16 bits.
1642 HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, HiProd, Neg16, DAG);
1643 return DAG.getNode(ISD::ADD, MVT::v4i32, LoProd, HiProd);
1644 } else if (Op.getValueType() == MVT::v8i16) {
1645 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
1646
Chris Lattnercea2aa72006-04-18 04:28:57 +00001647 SDOperand Zero = BuildSplatI(0, 1, MVT::v8i16, DAG);
Chris Lattner72dd9bd2006-04-18 03:43:48 +00001648
Chris Lattnercea2aa72006-04-18 04:28:57 +00001649 return BuildIntrinsicOp(Intrinsic::ppc_altivec_vmladduhm,
1650 LHS, RHS, Zero, DAG);
Chris Lattner19a81522006-04-18 03:57:35 +00001651 } else if (Op.getValueType() == MVT::v16i8) {
1652 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
1653
1654 // Multiply the even 8-bit parts, producing 16-bit sums.
1655 SDOperand EvenParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuleub,
1656 LHS, RHS, DAG, MVT::v8i16);
1657 EvenParts = DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, EvenParts);
1658
1659 // Multiply the odd 8-bit parts, producing 16-bit sums.
1660 SDOperand OddParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuloub,
1661 LHS, RHS, DAG, MVT::v8i16);
1662 OddParts = DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, OddParts);
1663
1664 // Merge the results together.
1665 std::vector<SDOperand> Ops;
1666 for (unsigned i = 0; i != 8; ++i) {
1667 Ops.push_back(DAG.getConstant(2*i+1, MVT::i8));
1668 Ops.push_back(DAG.getConstant(2*i+1+16, MVT::i8));
1669 }
1670
1671 return DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v16i8, EvenParts, OddParts,
1672 DAG.getNode(ISD::BUILD_VECTOR, MVT::v16i8, Ops));
Chris Lattner72dd9bd2006-04-18 03:43:48 +00001673 } else {
1674 assert(0 && "Unknown mul to lower!");
1675 abort();
1676 }
Chris Lattnere7c768e2006-04-18 03:24:30 +00001677}
1678
Chris Lattnere4bc9ea2005-08-26 00:52:45 +00001679/// LowerOperation - Provide custom lowering hooks for some operations.
1680///
Nate Begeman21e463b2005-10-16 05:39:50 +00001681SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +00001682 switch (Op.getOpcode()) {
1683 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattner1a635d62006-04-14 06:01:58 +00001684 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
1685 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Nate Begeman37efe672006-04-22 18:53:45 +00001686 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Chris Lattner1a635d62006-04-14 06:01:58 +00001687 case ISD::SETCC: return LowerSETCC(Op, DAG);
1688 case ISD::VASTART: return LowerVASTART(Op, DAG, VarArgsFrameIndex);
1689 case ISD::RET: return LowerRET(Op, DAG);
Chris Lattner7c0d6642005-10-02 06:37:13 +00001690
Chris Lattner1a635d62006-04-14 06:01:58 +00001691 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
1692 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
1693 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00001694
Chris Lattner1a635d62006-04-14 06:01:58 +00001695 // Lower 64-bit shifts.
1696 case ISD::SHL: return LowerSHL(Op, DAG);
1697 case ISD::SRL: return LowerSRL(Op, DAG);
1698 case ISD::SRA: return LowerSRA(Op, DAG);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00001699
Chris Lattner1a635d62006-04-14 06:01:58 +00001700 // Vector-related lowering.
1701 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
1702 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
1703 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
1704 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
Chris Lattnere7c768e2006-04-18 03:24:30 +00001705 case ISD::MUL: return LowerMUL(Op, DAG);
Chris Lattnerbc11c342005-08-31 20:23:54 +00001706 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +00001707 return SDOperand();
1708}
1709
Chris Lattner1a635d62006-04-14 06:01:58 +00001710//===----------------------------------------------------------------------===//
1711// Other Lowering Code
1712//===----------------------------------------------------------------------===//
1713
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001714std::vector<SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +00001715PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001716 //
1717 // add beautiful description of PPC stack frame format, or at least some docs
1718 //
1719 MachineFunction &MF = DAG.getMachineFunction();
1720 MachineFrameInfo *MFI = MF.getFrameInfo();
1721 MachineBasicBlock& BB = MF.front();
Chris Lattner7b738342005-09-13 19:33:40 +00001722 SSARegMap *RegMap = MF.getSSARegMap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001723 std::vector<SDOperand> ArgValues;
1724
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001725 unsigned ArgOffset = 24;
1726 unsigned GPR_remaining = 8;
1727 unsigned FPR_remaining = 13;
1728 unsigned GPR_idx = 0, FPR_idx = 0;
1729 static const unsigned GPR[] = {
1730 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1731 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1732 };
1733 static const unsigned FPR[] = {
1734 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1735 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1736 };
1737
1738 // Add DAG nodes to load the arguments... On entry to a function on PPC,
1739 // the arguments start at offset 24, although they are likely to be passed
1740 // in registers.
1741 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
1742 SDOperand newroot, argt;
1743 unsigned ObjSize;
1744 bool needsLoad = false;
1745 bool ArgLive = !I->use_empty();
1746 MVT::ValueType ObjectVT = getValueType(I->getType());
1747
1748 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +00001749 default: assert(0 && "Unhandled argument type!");
1750 case MVT::i1:
1751 case MVT::i8:
1752 case MVT::i16:
1753 case MVT::i32:
1754 ObjSize = 4;
1755 if (!ArgLive) break;
1756 if (GPR_remaining > 0) {
Nate Begeman1d9d7422005-10-18 00:28:58 +00001757 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +00001758 MF.addLiveIn(GPR[GPR_idx], VReg);
1759 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +00001760 if (ObjectVT != MVT::i32) {
1761 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
1762 : ISD::AssertZext;
1763 argt = DAG.getNode(AssertOp, MVT::i32, argt,
1764 DAG.getValueType(ObjectVT));
1765 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
1766 }
Chris Lattner915fb302005-08-30 00:19:00 +00001767 } else {
1768 needsLoad = true;
1769 }
1770 break;
Chris Lattner80720a92005-11-30 20:40:54 +00001771 case MVT::i64:
1772 ObjSize = 8;
Chris Lattner915fb302005-08-30 00:19:00 +00001773 if (!ArgLive) break;
1774 if (GPR_remaining > 0) {
1775 SDOperand argHi, argLo;
Nate Begeman1d9d7422005-10-18 00:28:58 +00001776 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +00001777 MF.addLiveIn(GPR[GPR_idx], VReg);
1778 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +00001779 // If we have two or more remaining argument registers, then both halves
1780 // of the i64 can be sourced from there. Otherwise, the lower half will
1781 // have to come off the stack. This can happen when an i64 is preceded
1782 // by 28 bytes of arguments.
1783 if (GPR_remaining > 1) {
Nate Begeman1d9d7422005-10-18 00:28:58 +00001784 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +00001785 MF.addLiveIn(GPR[GPR_idx+1], VReg);
1786 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +00001787 } else {
1788 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
1789 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
1790 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
1791 DAG.getSrcValue(NULL));
1792 }
1793 // Build the outgoing arg thingy
1794 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
1795 newroot = argLo;
1796 } else {
1797 needsLoad = true;
1798 }
1799 break;
1800 case MVT::f32:
1801 case MVT::f64:
1802 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
Chris Lattner413b9792006-01-11 18:21:25 +00001803 if (!ArgLive) {
1804 if (FPR_remaining > 0) {
1805 --FPR_remaining;
1806 ++FPR_idx;
1807 }
1808 break;
1809 }
Chris Lattner915fb302005-08-30 00:19:00 +00001810 if (FPR_remaining > 0) {
Chris Lattner919c0322005-10-01 01:35:02 +00001811 unsigned VReg;
1812 if (ObjectVT == MVT::f32)
Nate Begeman1d9d7422005-10-18 00:28:58 +00001813 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner919c0322005-10-01 01:35:02 +00001814 else
Nate Begeman1d9d7422005-10-18 00:28:58 +00001815 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +00001816 MF.addLiveIn(FPR[FPR_idx], VReg);
1817 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
Chris Lattner915fb302005-08-30 00:19:00 +00001818 --FPR_remaining;
1819 ++FPR_idx;
1820 } else {
1821 needsLoad = true;
1822 }
1823 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001824 }
1825
1826 // We need to load the argument to a virtual register if we determined above
1827 // that we ran out of physical registers of the appropriate type
1828 if (needsLoad) {
1829 unsigned SubregOffset = 0;
1830 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
1831 if (ObjectVT == MVT::i16) SubregOffset = 2;
1832 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
1833 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
1834 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
1835 DAG.getConstant(SubregOffset, MVT::i32));
1836 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
1837 DAG.getSrcValue(NULL));
1838 }
1839
1840 // Every 4 bytes of argument space consumes one of the GPRs available for
1841 // argument passing.
1842 if (GPR_remaining > 0) {
1843 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
1844 GPR_remaining -= delta;
1845 GPR_idx += delta;
1846 }
1847 ArgOffset += ObjSize;
1848 if (newroot.Val)
1849 DAG.setRoot(newroot.getValue(1));
1850
1851 ArgValues.push_back(argt);
1852 }
1853
1854 // If the function takes variable number of arguments, make a frame index for
1855 // the start of the first vararg value... for expansion of llvm.va_start.
1856 if (F.isVarArg()) {
1857 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
1858 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
1859 // If this function is vararg, store any remaining integer argument regs
1860 // to their spots on the stack so that they may be loaded by deferencing the
1861 // result of va_next.
1862 std::vector<SDOperand> MemOps;
1863 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begeman1d9d7422005-10-18 00:28:58 +00001864 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +00001865 MF.addLiveIn(GPR[GPR_idx], VReg);
1866 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001867 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
1868 Val, FIN, DAG.getSrcValue(NULL));
1869 MemOps.push_back(Store);
1870 // Increment the address by four for the next argument to store
1871 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
1872 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
1873 }
Chris Lattner80720a92005-11-30 20:40:54 +00001874 if (!MemOps.empty()) {
1875 MemOps.push_back(DAG.getRoot());
1876 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
1877 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001878 }
1879
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001880 return ArgValues;
1881}
1882
1883std::pair<SDOperand, SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +00001884PPCTargetLowering::LowerCallTo(SDOperand Chain,
1885 const Type *RetTy, bool isVarArg,
1886 unsigned CallingConv, bool isTailCall,
1887 SDOperand Callee, ArgListTy &Args,
1888 SelectionDAG &DAG) {
Chris Lattner281b55e2006-01-27 23:34:02 +00001889 // args_to_use will accumulate outgoing args for the PPCISD::CALL case in
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001890 // SelectExpr to use to put the arguments in the appropriate registers.
1891 std::vector<SDOperand> args_to_use;
1892
1893 // Count how many bytes are to be pushed on the stack, including the linkage
1894 // area, and parameter passing area.
1895 unsigned NumBytes = 24;
1896
1897 if (Args.empty()) {
Chris Lattner45b39762006-02-13 08:55:29 +00001898 Chain = DAG.getCALLSEQ_START(Chain,
1899 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001900 } else {
Chris Lattner915fb302005-08-30 00:19:00 +00001901 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001902 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +00001903 default: assert(0 && "Unknown value type!");
1904 case MVT::i1:
1905 case MVT::i8:
1906 case MVT::i16:
1907 case MVT::i32:
1908 case MVT::f32:
1909 NumBytes += 4;
1910 break;
1911 case MVT::i64:
1912 case MVT::f64:
1913 NumBytes += 8;
1914 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001915 }
Chris Lattner915fb302005-08-30 00:19:00 +00001916 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001917
Chris Lattner915fb302005-08-30 00:19:00 +00001918 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
1919 // plus 32 bytes of argument space in case any called code gets funky on us.
1920 // (Required by ABI to support var arg)
1921 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001922
1923 // Adjust the stack pointer for the new arguments...
1924 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattner45b39762006-02-13 08:55:29 +00001925 Chain = DAG.getCALLSEQ_START(Chain,
1926 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001927
1928 // Set up a copy of the stack pointer for use loading and storing any
1929 // arguments that may not fit in the registers available for argument
1930 // passing.
Chris Lattnera243db82006-01-11 19:55:07 +00001931 SDOperand StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001932
1933 // Figure out which arguments are going to go in registers, and which in
1934 // memory. Also, if this is a vararg function, floating point operations
1935 // must be stored to our stack, and loaded into integer regs as well, if
1936 // any integer regs are available for argument passing.
1937 unsigned ArgOffset = 24;
1938 unsigned GPR_remaining = 8;
1939 unsigned FPR_remaining = 13;
1940
1941 std::vector<SDOperand> MemOps;
1942 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
1943 // PtrOff will be used to store the current argument to the stack if a
1944 // register cannot be found for it.
1945 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1946 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
1947 MVT::ValueType ArgVT = getValueType(Args[i].second);
1948
1949 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +00001950 default: assert(0 && "Unexpected ValueType for argument!");
1951 case MVT::i1:
1952 case MVT::i8:
1953 case MVT::i16:
1954 // Promote the integer to 32 bits. If the input type is signed use a
1955 // sign extend, otherwise use a zero extend.
1956 if (Args[i].second->isSigned())
1957 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
1958 else
1959 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
1960 // FALL THROUGH
1961 case MVT::i32:
1962 if (GPR_remaining > 0) {
1963 args_to_use.push_back(Args[i].first);
1964 --GPR_remaining;
1965 } else {
1966 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1967 Args[i].first, PtrOff,
1968 DAG.getSrcValue(NULL)));
1969 }
1970 ArgOffset += 4;
1971 break;
1972 case MVT::i64:
1973 // If we have one free GPR left, we can place the upper half of the i64
1974 // in it, and store the other half to the stack. If we have two or more
1975 // free GPRs, then we can pass both halves of the i64 in registers.
1976 if (GPR_remaining > 0) {
1977 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
1978 Args[i].first, DAG.getConstant(1, MVT::i32));
1979 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
1980 Args[i].first, DAG.getConstant(0, MVT::i32));
1981 args_to_use.push_back(Hi);
1982 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001983 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +00001984 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001985 --GPR_remaining;
1986 } else {
Chris Lattner915fb302005-08-30 00:19:00 +00001987 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
1988 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001989 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +00001990 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001991 }
Chris Lattner915fb302005-08-30 00:19:00 +00001992 } else {
1993 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1994 Args[i].first, PtrOff,
1995 DAG.getSrcValue(NULL)));
1996 }
1997 ArgOffset += 8;
1998 break;
1999 case MVT::f32:
2000 case MVT::f64:
2001 if (FPR_remaining > 0) {
2002 args_to_use.push_back(Args[i].first);
2003 --FPR_remaining;
2004 if (isVarArg) {
2005 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
2006 Args[i].first, PtrOff,
2007 DAG.getSrcValue(NULL));
2008 MemOps.push_back(Store);
2009 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +00002010 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +00002011 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
2012 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +00002013 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +00002014 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00002015 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +00002016 }
2017 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +00002018 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
2019 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +00002020 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
2021 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +00002022 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +00002023 args_to_use.push_back(Load);
2024 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00002025 }
2026 } else {
Chris Lattner915fb302005-08-30 00:19:00 +00002027 // If we have any FPRs remaining, we may also have GPRs remaining.
2028 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
2029 // GPRs.
2030 if (GPR_remaining > 0) {
2031 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
2032 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00002033 }
Chris Lattner915fb302005-08-30 00:19:00 +00002034 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
2035 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
2036 --GPR_remaining;
2037 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +00002038 }
Chris Lattner915fb302005-08-30 00:19:00 +00002039 } else {
2040 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
2041 Args[i].first, PtrOff,
2042 DAG.getSrcValue(NULL)));
2043 }
2044 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
2045 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00002046 }
2047 }
2048 if (!MemOps.empty())
2049 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
2050 }
2051
2052 std::vector<MVT::ValueType> RetVals;
2053 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +00002054 MVT::ValueType ActualRetTyVT = RetTyVT;
2055 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
2056 ActualRetTyVT = MVT::i32; // Promote result to i32.
2057
Chris Lattnere00ebf02006-01-28 07:33:03 +00002058 if (RetTyVT == MVT::i64) {
2059 RetVals.push_back(MVT::i32);
2060 RetVals.push_back(MVT::i32);
2061 } else if (RetTyVT != MVT::isVoid) {
Chris Lattnerf5059492005-09-02 01:24:55 +00002062 RetVals.push_back(ActualRetTyVT);
Chris Lattnere00ebf02006-01-28 07:33:03 +00002063 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +00002064 RetVals.push_back(MVT::Other);
2065
Chris Lattner2823b3e2005-11-17 05:56:14 +00002066 // If the callee is a GlobalAddress node (quite common, every direct call is)
2067 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
2068 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
2069 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
2070
Chris Lattner281b55e2006-01-27 23:34:02 +00002071 std::vector<SDOperand> Ops;
2072 Ops.push_back(Chain);
2073 Ops.push_back(Callee);
2074 Ops.insert(Ops.end(), args_to_use.begin(), args_to_use.end());
2075 SDOperand TheCall = DAG.getNode(PPCISD::CALL, RetVals, Ops);
Chris Lattnere00ebf02006-01-28 07:33:03 +00002076 Chain = TheCall.getValue(TheCall.Val->getNumValues()-1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00002077 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
2078 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +00002079 SDOperand RetVal = TheCall;
2080
2081 // If the result is a small value, add a note so that we keep track of the
2082 // information about whether it is sign or zero extended.
2083 if (RetTyVT != ActualRetTyVT) {
2084 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
2085 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
2086 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
Chris Lattnere00ebf02006-01-28 07:33:03 +00002087 } else if (RetTyVT == MVT::i64) {
2088 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, RetVal, RetVal.getValue(1));
Chris Lattnerf5059492005-09-02 01:24:55 +00002089 }
2090
2091 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00002092}
2093
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00002094MachineBasicBlock *
Nate Begeman21e463b2005-10-16 05:39:50 +00002095PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
2096 MachineBasicBlock *BB) {
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00002097 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
Chris Lattner919c0322005-10-01 01:35:02 +00002098 MI->getOpcode() == PPC::SELECT_CC_F4 ||
Chris Lattner710ff322006-04-08 22:45:08 +00002099 MI->getOpcode() == PPC::SELECT_CC_F8 ||
2100 MI->getOpcode() == PPC::SELECT_CC_VRRC) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00002101 "Unexpected instr type to insert");
2102
2103 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
2104 // control-flow pattern. The incoming instruction knows the destination vreg
2105 // to set, the condition code register to branch on, the true/false values to
2106 // select between, and a branch opcode to use.
2107 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2108 ilist<MachineBasicBlock>::iterator It = BB;
2109 ++It;
2110
2111 // thisMBB:
2112 // ...
2113 // TrueVal = ...
2114 // cmpTY ccX, r1, r2
2115 // bCC copy1MBB
2116 // fallthrough --> copy0MBB
2117 MachineBasicBlock *thisMBB = BB;
2118 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
2119 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
2120 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
2121 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
2122 MachineFunction *F = BB->getParent();
2123 F->getBasicBlockList().insert(It, copy0MBB);
2124 F->getBasicBlockList().insert(It, sinkMBB);
Nate Begemanf15485a2006-03-27 01:32:24 +00002125 // Update machine-CFG edges by first adding all successors of the current
2126 // block to the new block which will contain the Phi node for the select.
2127 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
2128 e = BB->succ_end(); i != e; ++i)
2129 sinkMBB->addSuccessor(*i);
2130 // Next, remove all successors of the current block, and add the true
2131 // and fallthrough blocks as its successors.
2132 while(!BB->succ_empty())
2133 BB->removeSuccessor(BB->succ_begin());
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00002134 BB->addSuccessor(copy0MBB);
2135 BB->addSuccessor(sinkMBB);
2136
2137 // copy0MBB:
2138 // %FalseValue = ...
2139 // # fallthrough to sinkMBB
2140 BB = copy0MBB;
2141
2142 // Update machine-CFG edges
2143 BB->addSuccessor(sinkMBB);
2144
2145 // sinkMBB:
2146 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2147 // ...
2148 BB = sinkMBB;
2149 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
2150 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
2151 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
2152
2153 delete MI; // The pseudo instruction is gone now.
2154 return BB;
2155}
2156
Chris Lattner1a635d62006-04-14 06:01:58 +00002157//===----------------------------------------------------------------------===//
2158// Target Optimization Hooks
2159//===----------------------------------------------------------------------===//
2160
Chris Lattner8c13d0a2006-03-01 04:57:39 +00002161SDOperand PPCTargetLowering::PerformDAGCombine(SDNode *N,
2162 DAGCombinerInfo &DCI) const {
2163 TargetMachine &TM = getTargetMachine();
2164 SelectionDAG &DAG = DCI.DAG;
2165 switch (N->getOpcode()) {
2166 default: break;
2167 case ISD::SINT_TO_FP:
2168 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
Chris Lattnerecfe55e2006-03-22 05:30:33 +00002169 if (N->getOperand(0).getOpcode() == ISD::FP_TO_SINT) {
2170 // Turn (sint_to_fp (fp_to_sint X)) -> fctidz/fcfid without load/stores.
2171 // We allow the src/dst to be either f32/f64, but the intermediate
2172 // type must be i64.
2173 if (N->getOperand(0).getValueType() == MVT::i64) {
2174 SDOperand Val = N->getOperand(0).getOperand(0);
2175 if (Val.getValueType() == MVT::f32) {
2176 Val = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Val);
2177 DCI.AddToWorklist(Val.Val);
2178 }
2179
2180 Val = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Val);
Chris Lattner8c13d0a2006-03-01 04:57:39 +00002181 DCI.AddToWorklist(Val.Val);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00002182 Val = DAG.getNode(PPCISD::FCFID, MVT::f64, Val);
Chris Lattner8c13d0a2006-03-01 04:57:39 +00002183 DCI.AddToWorklist(Val.Val);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00002184 if (N->getValueType(0) == MVT::f32) {
2185 Val = DAG.getNode(ISD::FP_ROUND, MVT::f32, Val);
2186 DCI.AddToWorklist(Val.Val);
2187 }
2188 return Val;
2189 } else if (N->getOperand(0).getValueType() == MVT::i32) {
2190 // If the intermediate type is i32, we can avoid the load/store here
2191 // too.
Chris Lattner8c13d0a2006-03-01 04:57:39 +00002192 }
Chris Lattner8c13d0a2006-03-01 04:57:39 +00002193 }
2194 }
2195 break;
Chris Lattner51269842006-03-01 05:50:56 +00002196 case ISD::STORE:
2197 // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)).
2198 if (TM.getSubtarget<PPCSubtarget>().hasSTFIWX() &&
2199 N->getOperand(1).getOpcode() == ISD::FP_TO_SINT &&
2200 N->getOperand(1).getValueType() == MVT::i32) {
2201 SDOperand Val = N->getOperand(1).getOperand(0);
2202 if (Val.getValueType() == MVT::f32) {
2203 Val = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Val);
2204 DCI.AddToWorklist(Val.Val);
2205 }
2206 Val = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Val);
2207 DCI.AddToWorklist(Val.Val);
2208
2209 Val = DAG.getNode(PPCISD::STFIWX, MVT::Other, N->getOperand(0), Val,
2210 N->getOperand(2), N->getOperand(3));
2211 DCI.AddToWorklist(Val.Val);
2212 return Val;
2213 }
2214 break;
Chris Lattner4468c222006-03-31 06:02:07 +00002215 case PPCISD::VCMP: {
2216 // If a VCMPo node already exists with exactly the same operands as this
2217 // node, use its result instead of this node (VCMPo computes both a CR6 and
2218 // a normal output).
2219 //
2220 if (!N->getOperand(0).hasOneUse() &&
2221 !N->getOperand(1).hasOneUse() &&
2222 !N->getOperand(2).hasOneUse()) {
2223
2224 // Scan all of the users of the LHS, looking for VCMPo's that match.
2225 SDNode *VCMPoNode = 0;
2226
2227 SDNode *LHSN = N->getOperand(0).Val;
2228 for (SDNode::use_iterator UI = LHSN->use_begin(), E = LHSN->use_end();
2229 UI != E; ++UI)
2230 if ((*UI)->getOpcode() == PPCISD::VCMPo &&
2231 (*UI)->getOperand(1) == N->getOperand(1) &&
2232 (*UI)->getOperand(2) == N->getOperand(2) &&
2233 (*UI)->getOperand(0) == N->getOperand(0)) {
2234 VCMPoNode = *UI;
2235 break;
2236 }
2237
Chris Lattner00901202006-04-18 18:28:22 +00002238 // If there is no VCMPo node, or if the flag value has a single use, don't
2239 // transform this.
2240 if (!VCMPoNode || VCMPoNode->hasNUsesOfValue(0, 1))
2241 break;
2242
2243 // Look at the (necessarily single) use of the flag value. If it has a
2244 // chain, this transformation is more complex. Note that multiple things
2245 // could use the value result, which we should ignore.
2246 SDNode *FlagUser = 0;
2247 for (SDNode::use_iterator UI = VCMPoNode->use_begin();
2248 FlagUser == 0; ++UI) {
2249 assert(UI != VCMPoNode->use_end() && "Didn't find user!");
2250 SDNode *User = *UI;
2251 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
2252 if (User->getOperand(i) == SDOperand(VCMPoNode, 1)) {
2253 FlagUser = User;
2254 break;
2255 }
2256 }
2257 }
2258
2259 // If the user is a MFCR instruction, we know this is safe. Otherwise we
2260 // give up for right now.
2261 if (FlagUser->getOpcode() == PPCISD::MFCR)
Chris Lattner4468c222006-03-31 06:02:07 +00002262 return SDOperand(VCMPoNode, 0);
2263 }
2264 break;
2265 }
Chris Lattner90564f22006-04-18 17:59:36 +00002266 case ISD::BR_CC: {
2267 // If this is a branch on an altivec predicate comparison, lower this so
2268 // that we don't have to do a MFCR: instead, branch directly on CR6. This
2269 // lowering is done pre-legalize, because the legalizer lowers the predicate
2270 // compare down to code that is difficult to reassemble.
2271 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
2272 SDOperand LHS = N->getOperand(2), RHS = N->getOperand(3);
2273 int CompareOpc;
2274 bool isDot;
2275
2276 if (LHS.getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
2277 isa<ConstantSDNode>(RHS) && (CC == ISD::SETEQ || CC == ISD::SETNE) &&
2278 getAltivecCompareInfo(LHS, CompareOpc, isDot)) {
2279 assert(isDot && "Can't compare against a vector result!");
2280
2281 // If this is a comparison against something other than 0/1, then we know
2282 // that the condition is never/always true.
2283 unsigned Val = cast<ConstantSDNode>(RHS)->getValue();
2284 if (Val != 0 && Val != 1) {
2285 if (CC == ISD::SETEQ) // Cond never true, remove branch.
2286 return N->getOperand(0);
2287 // Always !=, turn it into an unconditional branch.
2288 return DAG.getNode(ISD::BR, MVT::Other,
2289 N->getOperand(0), N->getOperand(4));
2290 }
2291
2292 bool BranchOnWhenPredTrue = (CC == ISD::SETEQ) ^ (Val == 0);
2293
2294 // Create the PPCISD altivec 'dot' comparison node.
2295 std::vector<SDOperand> Ops;
2296 std::vector<MVT::ValueType> VTs;
2297 Ops.push_back(LHS.getOperand(2)); // LHS of compare
2298 Ops.push_back(LHS.getOperand(3)); // RHS of compare
2299 Ops.push_back(DAG.getConstant(CompareOpc, MVT::i32));
2300 VTs.push_back(LHS.getOperand(2).getValueType());
2301 VTs.push_back(MVT::Flag);
2302 SDOperand CompNode = DAG.getNode(PPCISD::VCMPo, VTs, Ops);
2303
2304 // Unpack the result based on how the target uses it.
2305 unsigned CompOpc;
2306 switch (cast<ConstantSDNode>(LHS.getOperand(1))->getValue()) {
2307 default: // Can't happen, don't crash on invalid number though.
2308 case 0: // Branch on the value of the EQ bit of CR6.
2309 CompOpc = BranchOnWhenPredTrue ? PPC::BEQ : PPC::BNE;
2310 break;
2311 case 1: // Branch on the inverted value of the EQ bit of CR6.
2312 CompOpc = BranchOnWhenPredTrue ? PPC::BNE : PPC::BEQ;
2313 break;
2314 case 2: // Branch on the value of the LT bit of CR6.
2315 CompOpc = BranchOnWhenPredTrue ? PPC::BLT : PPC::BGE;
2316 break;
2317 case 3: // Branch on the inverted value of the LT bit of CR6.
2318 CompOpc = BranchOnWhenPredTrue ? PPC::BGE : PPC::BLT;
2319 break;
2320 }
2321
2322 return DAG.getNode(PPCISD::COND_BRANCH, MVT::Other, N->getOperand(0),
2323 DAG.getRegister(PPC::CR6, MVT::i32),
2324 DAG.getConstant(CompOpc, MVT::i32),
2325 N->getOperand(4), CompNode.getValue(1));
2326 }
2327 break;
2328 }
Chris Lattner8c13d0a2006-03-01 04:57:39 +00002329 }
2330
2331 return SDOperand();
2332}
2333
Chris Lattner1a635d62006-04-14 06:01:58 +00002334//===----------------------------------------------------------------------===//
2335// Inline Assembly Support
2336//===----------------------------------------------------------------------===//
2337
Chris Lattnerbbe77de2006-04-02 06:26:07 +00002338void PPCTargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
2339 uint64_t Mask,
2340 uint64_t &KnownZero,
2341 uint64_t &KnownOne,
2342 unsigned Depth) const {
2343 KnownZero = 0;
2344 KnownOne = 0;
2345 switch (Op.getOpcode()) {
2346 default: break;
2347 case ISD::INTRINSIC_WO_CHAIN: {
2348 switch (cast<ConstantSDNode>(Op.getOperand(0))->getValue()) {
2349 default: break;
2350 case Intrinsic::ppc_altivec_vcmpbfp_p:
2351 case Intrinsic::ppc_altivec_vcmpeqfp_p:
2352 case Intrinsic::ppc_altivec_vcmpequb_p:
2353 case Intrinsic::ppc_altivec_vcmpequh_p:
2354 case Intrinsic::ppc_altivec_vcmpequw_p:
2355 case Intrinsic::ppc_altivec_vcmpgefp_p:
2356 case Intrinsic::ppc_altivec_vcmpgtfp_p:
2357 case Intrinsic::ppc_altivec_vcmpgtsb_p:
2358 case Intrinsic::ppc_altivec_vcmpgtsh_p:
2359 case Intrinsic::ppc_altivec_vcmpgtsw_p:
2360 case Intrinsic::ppc_altivec_vcmpgtub_p:
2361 case Intrinsic::ppc_altivec_vcmpgtuh_p:
2362 case Intrinsic::ppc_altivec_vcmpgtuw_p:
2363 KnownZero = ~1U; // All bits but the low one are known to be zero.
2364 break;
2365 }
2366 }
2367 }
2368}
2369
2370
Chris Lattnerad3bc8d2006-02-07 20:16:30 +00002371/// getConstraintType - Given a constraint letter, return the type of
2372/// constraint it is for this target.
2373PPCTargetLowering::ConstraintType
2374PPCTargetLowering::getConstraintType(char ConstraintLetter) const {
2375 switch (ConstraintLetter) {
2376 default: break;
2377 case 'b':
2378 case 'r':
2379 case 'f':
2380 case 'v':
2381 case 'y':
2382 return C_RegisterClass;
2383 }
2384 return TargetLowering::getConstraintType(ConstraintLetter);
2385}
2386
2387
Chris Lattnerddc787d2006-01-31 19:20:21 +00002388std::vector<unsigned> PPCTargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00002389getRegClassForInlineAsmConstraint(const std::string &Constraint,
2390 MVT::ValueType VT) const {
Chris Lattnerddc787d2006-01-31 19:20:21 +00002391 if (Constraint.size() == 1) {
2392 switch (Constraint[0]) { // GCC RS6000 Constraint Letters
2393 default: break; // Unknown constriant letter
2394 case 'b':
2395 return make_vector<unsigned>(/*no R0*/ PPC::R1 , PPC::R2 , PPC::R3 ,
2396 PPC::R4 , PPC::R5 , PPC::R6 , PPC::R7 ,
2397 PPC::R8 , PPC::R9 , PPC::R10, PPC::R11,
2398 PPC::R12, PPC::R13, PPC::R14, PPC::R15,
2399 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
2400 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
2401 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
2402 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
2403 0);
2404 case 'r':
2405 return make_vector<unsigned>(PPC::R0 , PPC::R1 , PPC::R2 , PPC::R3 ,
2406 PPC::R4 , PPC::R5 , PPC::R6 , PPC::R7 ,
2407 PPC::R8 , PPC::R9 , PPC::R10, PPC::R11,
2408 PPC::R12, PPC::R13, PPC::R14, PPC::R15,
2409 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
2410 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
2411 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
2412 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
2413 0);
2414 case 'f':
2415 return make_vector<unsigned>(PPC::F0 , PPC::F1 , PPC::F2 , PPC::F3 ,
2416 PPC::F4 , PPC::F5 , PPC::F6 , PPC::F7 ,
2417 PPC::F8 , PPC::F9 , PPC::F10, PPC::F11,
2418 PPC::F12, PPC::F13, PPC::F14, PPC::F15,
2419 PPC::F16, PPC::F17, PPC::F18, PPC::F19,
2420 PPC::F20, PPC::F21, PPC::F22, PPC::F23,
2421 PPC::F24, PPC::F25, PPC::F26, PPC::F27,
2422 PPC::F28, PPC::F29, PPC::F30, PPC::F31,
2423 0);
2424 case 'v':
2425 return make_vector<unsigned>(PPC::V0 , PPC::V1 , PPC::V2 , PPC::V3 ,
2426 PPC::V4 , PPC::V5 , PPC::V6 , PPC::V7 ,
2427 PPC::V8 , PPC::V9 , PPC::V10, PPC::V11,
2428 PPC::V12, PPC::V13, PPC::V14, PPC::V15,
2429 PPC::V16, PPC::V17, PPC::V18, PPC::V19,
2430 PPC::V20, PPC::V21, PPC::V22, PPC::V23,
2431 PPC::V24, PPC::V25, PPC::V26, PPC::V27,
2432 PPC::V28, PPC::V29, PPC::V30, PPC::V31,
2433 0);
2434 case 'y':
2435 return make_vector<unsigned>(PPC::CR0, PPC::CR1, PPC::CR2, PPC::CR3,
2436 PPC::CR4, PPC::CR5, PPC::CR6, PPC::CR7,
2437 0);
2438 }
2439 }
2440
Chris Lattner1efa40f2006-02-22 00:56:39 +00002441 return std::vector<unsigned>();
Chris Lattnerddc787d2006-01-31 19:20:21 +00002442}
Chris Lattner763317d2006-02-07 00:47:13 +00002443
2444// isOperandValidForConstraint
2445bool PPCTargetLowering::
2446isOperandValidForConstraint(SDOperand Op, char Letter) {
2447 switch (Letter) {
2448 default: break;
2449 case 'I':
2450 case 'J':
2451 case 'K':
2452 case 'L':
2453 case 'M':
2454 case 'N':
2455 case 'O':
2456 case 'P': {
2457 if (!isa<ConstantSDNode>(Op)) return false; // Must be an immediate.
2458 unsigned Value = cast<ConstantSDNode>(Op)->getValue();
2459 switch (Letter) {
2460 default: assert(0 && "Unknown constraint letter!");
2461 case 'I': // "I" is a signed 16-bit constant.
2462 return (short)Value == (int)Value;
2463 case 'J': // "J" is a constant with only the high-order 16 bits nonzero.
2464 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits.
2465 return (short)Value == 0;
2466 case 'K': // "K" is a constant with only the low-order 16 bits nonzero.
2467 return (Value >> 16) == 0;
2468 case 'M': // "M" is a constant that is greater than 31.
2469 return Value > 31;
2470 case 'N': // "N" is a positive constant that is an exact power of two.
2471 return (int)Value > 0 && isPowerOf2_32(Value);
2472 case 'O': // "O" is the constant zero.
2473 return Value == 0;
2474 case 'P': // "P" is a constant whose negation is a signed 16-bit constant.
2475 return (short)-Value == (int)-Value;
2476 }
2477 break;
2478 }
2479 }
2480
2481 // Handle standard constraint letters.
2482 return TargetLowering::isOperandValidForConstraint(Op, Letter);
2483}
Evan Chengc4c62572006-03-13 23:20:37 +00002484
2485/// isLegalAddressImmediate - Return true if the integer value can be used
2486/// as the offset of the target addressing mode.
2487bool PPCTargetLowering::isLegalAddressImmediate(int64_t V) const {
2488 // PPC allows a sign-extended 16-bit immediate field.
2489 return (V > -(1 << 16) && V < (1 << 16)-1);
2490}