blob: bd04a806253750aa5189192ae0ada860fe35977f [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
Chris Lattner331d1bc2006-11-02 01:44:04 +000031PPCTargetLowering::PPCTargetLowering(PPCTargetMachine &TM)
32 : TargetLowering(TM), PPCSubTarget(*TM.getSubtargetImpl()) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +000033
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
Evan Chengc5484282006-10-04 00:56:09 +000046 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
47 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Expand);
48 setLoadXAction(ISD::SEXTLOAD, MVT::i8, Expand);
49
Evan Cheng8b2794a2006-10-13 21:14:26 +000050 // PowerPC does not have truncstore for i1.
51 setStoreXAction(MVT::i1, Promote);
52
Chris Lattnera54aa942006-01-29 06:26:08 +000053 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
54 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
55
Chris Lattner7c5a3d32005-08-16 17:14:42 +000056 // PowerPC has no intrinsics for these particular operations
57 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
58 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
59 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
60
Chris Lattner7c5a3d32005-08-16 17:14:42 +000061 // PowerPC has no SREM/UREM instructions
62 setOperationAction(ISD::SREM, MVT::i32, Expand);
63 setOperationAction(ISD::UREM, MVT::i32, Expand);
Chris Lattner563ecfb2006-06-27 18:18:41 +000064 setOperationAction(ISD::SREM, MVT::i64, Expand);
65 setOperationAction(ISD::UREM, MVT::i64, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000066
67 // We don't support sin/cos/sqrt/fmod
68 setOperationAction(ISD::FSIN , MVT::f64, Expand);
69 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattner615c2d02005-09-28 22:29:58 +000070 setOperationAction(ISD::FREM , MVT::f64, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000071 setOperationAction(ISD::FSIN , MVT::f32, Expand);
72 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattner615c2d02005-09-28 22:29:58 +000073 setOperationAction(ISD::FREM , MVT::f32, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000074
75 // If we're enabling GP optimizations, use hardware square root
Chris Lattner1e9de3e2005-09-02 18:33:05 +000076 if (!TM.getSubtarget<PPCSubtarget>().hasFSQRT()) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +000077 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
78 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
79 }
80
Chris Lattner9601a862006-03-05 05:08:37 +000081 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
82 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
83
Nate Begemand88fc032006-01-14 03:14:10 +000084 // PowerPC does not have BSWAP, CTPOP or CTTZ
85 setOperationAction(ISD::BSWAP, MVT::i32 , Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000086 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
87 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
Chris Lattnerf89437d2006-06-27 20:14:52 +000088 setOperationAction(ISD::BSWAP, MVT::i64 , Expand);
89 setOperationAction(ISD::CTPOP, MVT::i64 , Expand);
90 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000091
Nate Begeman35ef9132006-01-11 21:21:00 +000092 // PowerPC does not have ROTR
93 setOperationAction(ISD::ROTR, MVT::i32 , Expand);
94
Chris Lattner7c5a3d32005-08-16 17:14:42 +000095 // PowerPC does not have Select
96 setOperationAction(ISD::SELECT, MVT::i32, Expand);
Chris Lattnerf89437d2006-06-27 20:14:52 +000097 setOperationAction(ISD::SELECT, MVT::i64, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000098 setOperationAction(ISD::SELECT, MVT::f32, Expand);
99 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000100
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000101 // PowerPC wants to turn select_cc of FP into fsel when possible.
102 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
103 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Nate Begeman44775902006-01-31 08:17:29 +0000104
Nate Begeman750ac1b2006-02-01 07:19:44 +0000105 // PowerPC wants to optimize integer setcc a bit
Nate Begeman44775902006-01-31 08:17:29 +0000106 setOperationAction(ISD::SETCC, MVT::i32, Custom);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000107
Nate Begeman81e80972006-03-17 01:40:33 +0000108 // PowerPC does not have BRCOND which requires SetCC
109 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
Evan Chengc35497f2006-10-30 08:02:39 +0000110
111 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000112
Chris Lattnerf7605322005-08-31 21:09:52 +0000113 // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
114 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000115
Jim Laskeyad23c9d2005-08-17 00:40:22 +0000116 // PowerPC does not have [U|S]INT_TO_FP
117 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
118 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
119
Chris Lattner53e88452005-12-23 05:13:35 +0000120 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
121 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
Chris Lattner5f9faea2006-06-27 18:40:08 +0000122 setOperationAction(ISD::BIT_CONVERT, MVT::i64, Expand);
123 setOperationAction(ISD::BIT_CONVERT, MVT::f64, Expand);
Chris Lattner53e88452005-12-23 05:13:35 +0000124
Chris Lattner25b8b8c2006-04-28 21:56:10 +0000125 // We cannot sextinreg(i1). Expand to shifts.
126 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
127
128
Jim Laskeyabf6d172006-01-05 01:25:28 +0000129 // Support label based line numbers.
Chris Lattnerf73bae12005-11-29 06:16:21 +0000130 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000131 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000132 // FIXME - use subtarget debug flags
Jim Laskeye0bce712006-01-05 01:47:43 +0000133 if (!TM.getSubtarget<PPCSubtarget>().isDarwin())
Jim Laskeyabf6d172006-01-05 01:25:28 +0000134 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattnere6ec9f22005-09-10 00:21:06 +0000135
Nate Begeman28a6b022005-12-10 02:36:00 +0000136 // We want to legalize GlobalAddress and ConstantPool nodes into the
137 // appropriate instructions to materialize the address.
Chris Lattner3eef4e32005-11-17 18:26:56 +0000138 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Nate Begeman28a6b022005-12-10 02:36:00 +0000139 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Nate Begeman37efe672006-04-22 18:53:45 +0000140 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
Chris Lattner059ca0f2006-06-16 21:01:35 +0000141 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
142 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
143 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
144
Nate Begemanee625572006-01-27 21:09:22 +0000145 // RET must be custom lowered, to meet ABI requirements
146 setOperationAction(ISD::RET , MVT::Other, Custom);
147
Nate Begemanacc398c2006-01-25 18:21:52 +0000148 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
149 setOperationAction(ISD::VASTART , MVT::Other, Custom);
150
Chris Lattnerb22c08b2006-01-15 09:02:48 +0000151 // Use the default implementation.
Nate Begemanacc398c2006-01-25 18:21:52 +0000152 setOperationAction(ISD::VAARG , MVT::Other, Expand);
153 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
154 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Chris Lattnerb22c08b2006-01-15 09:02:48 +0000155 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
156 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
157 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Chris Lattner56a752e2006-10-18 01:18:48 +0000158 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64 , Expand);
159
Chris Lattner6d92cad2006-03-26 10:06:40 +0000160 // We want to custom lower some of our intrinsics.
Chris Lattner48b61a72006-03-28 00:40:33 +0000161 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Chris Lattner6d92cad2006-03-26 10:06:40 +0000162
Chris Lattnera7a58542006-06-16 17:34:12 +0000163 if (TM.getSubtarget<PPCSubtarget>().has64BitSupport()) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000164 // They also have instructions for converting between i64 and fp.
Nate Begemanc09eeec2005-09-06 22:03:27 +0000165 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
166 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Chris Lattner7fbcef72006-03-24 07:53:47 +0000167
168 // FIXME: disable this lowered code. This generates 64-bit register values,
169 // and we don't model the fact that the top part is clobbered by calls. We
170 // need to flag these together so that the value isn't live across a call.
171 //setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
172
Nate Begemanae749a92005-10-25 23:48:36 +0000173 // To take advantage of the above i64 FP_TO_SINT, promote i32 FP_TO_UINT
174 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Promote);
175 } else {
Chris Lattner860e8862005-11-17 07:30:41 +0000176 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Nate Begemanae749a92005-10-25 23:48:36 +0000177 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begeman9d2b8172005-10-18 00:56:42 +0000178 }
179
Chris Lattnera7a58542006-06-16 17:34:12 +0000180 if (TM.getSubtarget<PPCSubtarget>().use64BitRegs()) {
Nate Begeman9d2b8172005-10-18 00:56:42 +0000181 // 64 bit PowerPC implementations can support i64 types directly
182 addRegisterClass(MVT::i64, PPC::G8RCRegisterClass);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000183 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
184 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000185 } else {
186 // 32 bit PowerPC wants to expand i64 shifts itself.
Chris Lattner3fe6c1d2006-09-20 03:47:40 +0000187 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
188 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
189 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000190 }
Evan Chengd30bf012006-03-01 01:11:20 +0000191
Nate Begeman425a9692005-11-29 08:17:20 +0000192 if (TM.getSubtarget<PPCSubtarget>().hasAltivec()) {
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000193 // First set operation action for all vector types to expand. Then we
194 // will selectively turn on ones that can be effectively codegen'd.
195 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
196 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Chris Lattnerf3f69de2006-04-16 01:37:57 +0000197 // add/sub are legal for all supported vector VT's.
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000198 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Legal);
199 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Legal);
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000200
Chris Lattner7ff7e672006-04-04 17:25:31 +0000201 // We promote all shuffles to v16i8.
202 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Promote);
Chris Lattnerf3f69de2006-04-16 01:37:57 +0000203 AddPromotedToType (ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, MVT::v16i8);
204
205 // We promote all non-typed operations to v4i32.
206 setOperationAction(ISD::AND , (MVT::ValueType)VT, Promote);
207 AddPromotedToType (ISD::AND , (MVT::ValueType)VT, MVT::v4i32);
208 setOperationAction(ISD::OR , (MVT::ValueType)VT, Promote);
209 AddPromotedToType (ISD::OR , (MVT::ValueType)VT, MVT::v4i32);
210 setOperationAction(ISD::XOR , (MVT::ValueType)VT, Promote);
211 AddPromotedToType (ISD::XOR , (MVT::ValueType)VT, MVT::v4i32);
212 setOperationAction(ISD::LOAD , (MVT::ValueType)VT, Promote);
213 AddPromotedToType (ISD::LOAD , (MVT::ValueType)VT, MVT::v4i32);
214 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
215 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v4i32);
216 setOperationAction(ISD::STORE, (MVT::ValueType)VT, Promote);
217 AddPromotedToType (ISD::STORE, (MVT::ValueType)VT, MVT::v4i32);
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000218
Chris Lattnerf3f69de2006-04-16 01:37:57 +0000219 // No other operations are legal.
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000220 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
221 setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand);
222 setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand);
223 setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand);
224 setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand);
Chris Lattner2ef5e892006-05-24 00:15:25 +0000225 setOperationAction(ISD::FDIV, (MVT::ValueType)VT, Expand);
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000226 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
227 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
228 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Expand);
Chris Lattner01cae072006-04-03 23:55:43 +0000229
230 setOperationAction(ISD::SCALAR_TO_VECTOR, (MVT::ValueType)VT, Expand);
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000231 }
232
Chris Lattner7ff7e672006-04-04 17:25:31 +0000233 // We can custom expand all VECTOR_SHUFFLEs to VPERM, others we can handle
234 // with merges, splats, etc.
235 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i8, Custom);
236
Chris Lattnerf3f69de2006-04-16 01:37:57 +0000237 setOperationAction(ISD::AND , MVT::v4i32, Legal);
238 setOperationAction(ISD::OR , MVT::v4i32, Legal);
239 setOperationAction(ISD::XOR , MVT::v4i32, Legal);
240 setOperationAction(ISD::LOAD , MVT::v4i32, Legal);
241 setOperationAction(ISD::SELECT, MVT::v4i32, Expand);
242 setOperationAction(ISD::STORE , MVT::v4i32, Legal);
243
Nate Begeman425a9692005-11-29 08:17:20 +0000244 addRegisterClass(MVT::v4f32, PPC::VRRCRegisterClass);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000245 addRegisterClass(MVT::v4i32, PPC::VRRCRegisterClass);
Chris Lattner8d052bc2006-03-25 07:39:07 +0000246 addRegisterClass(MVT::v8i16, PPC::VRRCRegisterClass);
247 addRegisterClass(MVT::v16i8, PPC::VRRCRegisterClass);
Chris Lattnerec4a0c72006-01-29 06:32:58 +0000248
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000249 setOperationAction(ISD::MUL, MVT::v4f32, Legal);
Chris Lattnere7c768e2006-04-18 03:24:30 +0000250 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
Chris Lattner72dd9bd2006-04-18 03:43:48 +0000251 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
Chris Lattner19a81522006-04-18 03:57:35 +0000252 setOperationAction(ISD::MUL, MVT::v16i8, Custom);
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000253
Chris Lattnerb2177b92006-03-19 06:55:52 +0000254 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom);
255 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Custom);
Chris Lattner64b3a082006-03-24 07:48:08 +0000256
Chris Lattner541f91b2006-04-02 00:43:36 +0000257 setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Custom);
258 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Custom);
Chris Lattner64b3a082006-03-24 07:48:08 +0000259 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Custom);
260 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
Nate Begeman425a9692005-11-29 08:17:20 +0000261 }
262
Chris Lattnerc08f9022006-06-27 00:04:13 +0000263 setSetCCResultType(MVT::i32);
Chris Lattner7b0c58c2006-06-27 17:34:57 +0000264 setShiftAmountType(MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000265 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattner10da9572006-10-18 01:20:43 +0000266
267 if (TM.getSubtarget<PPCSubtarget>().isPPC64())
268 setStackPointerRegisterToSaveRestore(PPC::X1);
269 else
270 setStackPointerRegisterToSaveRestore(PPC::R1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000271
Chris Lattner8c13d0a2006-03-01 04:57:39 +0000272 // We have target-specific dag combine patterns for the following nodes:
273 setTargetDAGCombine(ISD::SINT_TO_FP);
Chris Lattner51269842006-03-01 05:50:56 +0000274 setTargetDAGCombine(ISD::STORE);
Chris Lattner90564f22006-04-18 17:59:36 +0000275 setTargetDAGCombine(ISD::BR_CC);
Chris Lattnerd9989382006-07-10 20:56:58 +0000276 setTargetDAGCombine(ISD::BSWAP);
Chris Lattner8c13d0a2006-03-01 04:57:39 +0000277
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000278 computeRegisterProperties();
279}
280
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000281const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
282 switch (Opcode) {
283 default: return 0;
284 case PPCISD::FSEL: return "PPCISD::FSEL";
285 case PPCISD::FCFID: return "PPCISD::FCFID";
286 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
287 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
Chris Lattner51269842006-03-01 05:50:56 +0000288 case PPCISD::STFIWX: return "PPCISD::STFIWX";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000289 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
290 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000291 case PPCISD::VPERM: return "PPCISD::VPERM";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000292 case PPCISD::Hi: return "PPCISD::Hi";
293 case PPCISD::Lo: return "PPCISD::Lo";
294 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
295 case PPCISD::SRL: return "PPCISD::SRL";
296 case PPCISD::SRA: return "PPCISD::SRA";
297 case PPCISD::SHL: return "PPCISD::SHL";
Chris Lattnerecfe55e2006-03-22 05:30:33 +0000298 case PPCISD::EXTSW_32: return "PPCISD::EXTSW_32";
299 case PPCISD::STD_32: return "PPCISD::STD_32";
Chris Lattnere00ebf02006-01-28 07:33:03 +0000300 case PPCISD::CALL: return "PPCISD::CALL";
Chris Lattnerc703a8f2006-05-17 19:00:46 +0000301 case PPCISD::MTCTR: return "PPCISD::MTCTR";
302 case PPCISD::BCTRL: return "PPCISD::BCTRL";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000303 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
Chris Lattner6d92cad2006-03-26 10:06:40 +0000304 case PPCISD::MFCR: return "PPCISD::MFCR";
Chris Lattnera17b1552006-03-31 05:13:27 +0000305 case PPCISD::VCMP: return "PPCISD::VCMP";
Chris Lattner6d92cad2006-03-26 10:06:40 +0000306 case PPCISD::VCMPo: return "PPCISD::VCMPo";
Chris Lattnerd9989382006-07-10 20:56:58 +0000307 case PPCISD::LBRX: return "PPCISD::LBRX";
308 case PPCISD::STBRX: return "PPCISD::STBRX";
Chris Lattnerf70f8d92006-04-18 18:05:58 +0000309 case PPCISD::COND_BRANCH: return "PPCISD::COND_BRANCH";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000310 }
311}
312
Chris Lattner1a635d62006-04-14 06:01:58 +0000313//===----------------------------------------------------------------------===//
314// Node matching predicates, for use by the tblgen matching code.
315//===----------------------------------------------------------------------===//
316
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000317/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
318static bool isFloatingPointZero(SDOperand Op) {
319 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
320 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
Evan Cheng466685d2006-10-09 20:57:25 +0000321 else if (ISD::isEXTLoad(Op.Val) || ISD::isNON_EXTLoad(Op.Val)) {
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000322 // Maybe this has already been legalized into the constant pool?
323 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
Evan Chengc356a572006-09-12 21:04:05 +0000324 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000325 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
326 }
327 return false;
328}
329
Chris Lattnerddb739e2006-04-06 17:23:16 +0000330/// isConstantOrUndef - Op is either an undef node or a ConstantSDNode. Return
331/// true if Op is undef or if it matches the specified value.
332static bool isConstantOrUndef(SDOperand Op, unsigned Val) {
333 return Op.getOpcode() == ISD::UNDEF ||
334 cast<ConstantSDNode>(Op)->getValue() == Val;
335}
336
337/// isVPKUHUMShuffleMask - Return true if this is the shuffle mask for a
338/// VPKUHUM instruction.
Chris Lattnerf24380e2006-04-06 22:28:36 +0000339bool PPC::isVPKUHUMShuffleMask(SDNode *N, bool isUnary) {
340 if (!isUnary) {
341 for (unsigned i = 0; i != 16; ++i)
342 if (!isConstantOrUndef(N->getOperand(i), i*2+1))
343 return false;
344 } else {
345 for (unsigned i = 0; i != 8; ++i)
346 if (!isConstantOrUndef(N->getOperand(i), i*2+1) ||
347 !isConstantOrUndef(N->getOperand(i+8), i*2+1))
348 return false;
349 }
Chris Lattnerd0608e12006-04-06 18:26:28 +0000350 return true;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000351}
352
353/// isVPKUWUMShuffleMask - Return true if this is the shuffle mask for a
354/// VPKUWUM instruction.
Chris Lattnerf24380e2006-04-06 22:28:36 +0000355bool PPC::isVPKUWUMShuffleMask(SDNode *N, bool isUnary) {
356 if (!isUnary) {
357 for (unsigned i = 0; i != 16; i += 2)
358 if (!isConstantOrUndef(N->getOperand(i ), i*2+2) ||
359 !isConstantOrUndef(N->getOperand(i+1), i*2+3))
360 return false;
361 } else {
362 for (unsigned i = 0; i != 8; i += 2)
363 if (!isConstantOrUndef(N->getOperand(i ), i*2+2) ||
364 !isConstantOrUndef(N->getOperand(i+1), i*2+3) ||
365 !isConstantOrUndef(N->getOperand(i+8), i*2+2) ||
366 !isConstantOrUndef(N->getOperand(i+9), i*2+3))
367 return false;
368 }
Chris Lattnerd0608e12006-04-06 18:26:28 +0000369 return true;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000370}
371
Chris Lattnercaad1632006-04-06 22:02:42 +0000372/// isVMerge - Common function, used to match vmrg* shuffles.
373///
374static bool isVMerge(SDNode *N, unsigned UnitSize,
375 unsigned LHSStart, unsigned RHSStart) {
Chris Lattner116cc482006-04-06 21:11:54 +0000376 assert(N->getOpcode() == ISD::BUILD_VECTOR &&
377 N->getNumOperands() == 16 && "PPC only supports shuffles by bytes!");
378 assert((UnitSize == 1 || UnitSize == 2 || UnitSize == 4) &&
379 "Unsupported merge size!");
380
381 for (unsigned i = 0; i != 8/UnitSize; ++i) // Step over units
382 for (unsigned j = 0; j != UnitSize; ++j) { // Step over bytes within unit
383 if (!isConstantOrUndef(N->getOperand(i*UnitSize*2+j),
Chris Lattnercaad1632006-04-06 22:02:42 +0000384 LHSStart+j+i*UnitSize) ||
Chris Lattner116cc482006-04-06 21:11:54 +0000385 !isConstantOrUndef(N->getOperand(i*UnitSize*2+UnitSize+j),
Chris Lattnercaad1632006-04-06 22:02:42 +0000386 RHSStart+j+i*UnitSize))
Chris Lattner116cc482006-04-06 21:11:54 +0000387 return false;
388 }
Chris Lattnercaad1632006-04-06 22:02:42 +0000389 return true;
390}
391
392/// isVMRGLShuffleMask - Return true if this is a shuffle mask suitable for
393/// a VRGL* instruction with the specified unit size (1,2 or 4 bytes).
394bool PPC::isVMRGLShuffleMask(SDNode *N, unsigned UnitSize, bool isUnary) {
395 if (!isUnary)
396 return isVMerge(N, UnitSize, 8, 24);
397 return isVMerge(N, UnitSize, 8, 8);
Chris Lattner116cc482006-04-06 21:11:54 +0000398}
399
400/// isVMRGHShuffleMask - Return true if this is a shuffle mask suitable for
401/// a VRGH* instruction with the specified unit size (1,2 or 4 bytes).
Chris Lattnercaad1632006-04-06 22:02:42 +0000402bool PPC::isVMRGHShuffleMask(SDNode *N, unsigned UnitSize, bool isUnary) {
403 if (!isUnary)
404 return isVMerge(N, UnitSize, 0, 16);
405 return isVMerge(N, UnitSize, 0, 0);
Chris Lattner116cc482006-04-06 21:11:54 +0000406}
407
408
Chris Lattnerd0608e12006-04-06 18:26:28 +0000409/// isVSLDOIShuffleMask - If this is a vsldoi shuffle mask, return the shift
410/// amount, otherwise return -1.
Chris Lattnerf24380e2006-04-06 22:28:36 +0000411int PPC::isVSLDOIShuffleMask(SDNode *N, bool isUnary) {
Chris Lattner116cc482006-04-06 21:11:54 +0000412 assert(N->getOpcode() == ISD::BUILD_VECTOR &&
413 N->getNumOperands() == 16 && "PPC only supports shuffles by bytes!");
Chris Lattnerd0608e12006-04-06 18:26:28 +0000414 // Find the first non-undef value in the shuffle mask.
415 unsigned i;
416 for (i = 0; i != 16 && N->getOperand(i).getOpcode() == ISD::UNDEF; ++i)
417 /*search*/;
418
419 if (i == 16) return -1; // all undef.
420
421 // Otherwise, check to see if the rest of the elements are consequtively
422 // numbered from this value.
423 unsigned ShiftAmt = cast<ConstantSDNode>(N->getOperand(i))->getValue();
424 if (ShiftAmt < i) return -1;
425 ShiftAmt -= i;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000426
Chris Lattnerf24380e2006-04-06 22:28:36 +0000427 if (!isUnary) {
428 // Check the rest of the elements to see if they are consequtive.
429 for (++i; i != 16; ++i)
430 if (!isConstantOrUndef(N->getOperand(i), ShiftAmt+i))
431 return -1;
432 } else {
433 // Check the rest of the elements to see if they are consequtive.
434 for (++i; i != 16; ++i)
435 if (!isConstantOrUndef(N->getOperand(i), (ShiftAmt+i) & 15))
436 return -1;
437 }
Chris Lattnerd0608e12006-04-06 18:26:28 +0000438
439 return ShiftAmt;
440}
Chris Lattneref819f82006-03-20 06:33:01 +0000441
442/// isSplatShuffleMask - Return true if the specified VECTOR_SHUFFLE operand
443/// specifies a splat of a single element that is suitable for input to
444/// VSPLTB/VSPLTH/VSPLTW.
Chris Lattner7ff7e672006-04-04 17:25:31 +0000445bool PPC::isSplatShuffleMask(SDNode *N, unsigned EltSize) {
446 assert(N->getOpcode() == ISD::BUILD_VECTOR &&
447 N->getNumOperands() == 16 &&
448 (EltSize == 1 || EltSize == 2 || EltSize == 4));
Chris Lattnerdd4d2d02006-03-20 06:51:10 +0000449
Chris Lattner88a99ef2006-03-20 06:37:44 +0000450 // This is a splat operation if each element of the permute is the same, and
451 // if the value doesn't reference the second vector.
Chris Lattner7ff7e672006-04-04 17:25:31 +0000452 unsigned ElementBase = 0;
Chris Lattner88a99ef2006-03-20 06:37:44 +0000453 SDOperand Elt = N->getOperand(0);
Chris Lattner7ff7e672006-04-04 17:25:31 +0000454 if (ConstantSDNode *EltV = dyn_cast<ConstantSDNode>(Elt))
455 ElementBase = EltV->getValue();
456 else
457 return false; // FIXME: Handle UNDEF elements too!
458
459 if (cast<ConstantSDNode>(Elt)->getValue() >= 16)
460 return false;
461
462 // Check that they are consequtive.
463 for (unsigned i = 1; i != EltSize; ++i) {
464 if (!isa<ConstantSDNode>(N->getOperand(i)) ||
465 cast<ConstantSDNode>(N->getOperand(i))->getValue() != i+ElementBase)
466 return false;
467 }
468
Chris Lattner88a99ef2006-03-20 06:37:44 +0000469 assert(isa<ConstantSDNode>(Elt) && "Invalid VECTOR_SHUFFLE mask!");
Chris Lattner7ff7e672006-04-04 17:25:31 +0000470 for (unsigned i = EltSize, e = 16; i != e; i += EltSize) {
Chris Lattnerb097aa92006-04-14 23:19:08 +0000471 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Chris Lattner88a99ef2006-03-20 06:37:44 +0000472 assert(isa<ConstantSDNode>(N->getOperand(i)) &&
473 "Invalid VECTOR_SHUFFLE mask!");
Chris Lattner7ff7e672006-04-04 17:25:31 +0000474 for (unsigned j = 0; j != EltSize; ++j)
475 if (N->getOperand(i+j) != N->getOperand(j))
476 return false;
Chris Lattner88a99ef2006-03-20 06:37:44 +0000477 }
478
Chris Lattner7ff7e672006-04-04 17:25:31 +0000479 return true;
Chris Lattneref819f82006-03-20 06:33:01 +0000480}
481
482/// getVSPLTImmediate - Return the appropriate VSPLT* immediate to splat the
483/// specified isSplatShuffleMask VECTOR_SHUFFLE mask.
Chris Lattner7ff7e672006-04-04 17:25:31 +0000484unsigned PPC::getVSPLTImmediate(SDNode *N, unsigned EltSize) {
485 assert(isSplatShuffleMask(N, EltSize));
486 return cast<ConstantSDNode>(N->getOperand(0))->getValue() / EltSize;
Chris Lattneref819f82006-03-20 06:33:01 +0000487}
488
Chris Lattnere87192a2006-04-12 17:37:20 +0000489/// get_VSPLTI_elt - If this is a build_vector of constants which can be formed
Chris Lattner140a58f2006-04-08 06:46:53 +0000490/// by using a vspltis[bhw] instruction of the specified element size, return
491/// the constant being splatted. The ByteSize field indicates the number of
492/// bytes of each element [124] -> [bhw].
Chris Lattnere87192a2006-04-12 17:37:20 +0000493SDOperand PPC::get_VSPLTI_elt(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000494 SDOperand OpVal(0, 0);
Chris Lattner79d9a882006-04-08 07:14:26 +0000495
496 // If ByteSize of the splat is bigger than the element size of the
497 // build_vector, then we have a case where we are checking for a splat where
498 // multiple elements of the buildvector are folded together into a single
499 // logical element of the splat (e.g. "vsplish 1" to splat {0,1}*8).
500 unsigned EltSize = 16/N->getNumOperands();
501 if (EltSize < ByteSize) {
502 unsigned Multiple = ByteSize/EltSize; // Number of BV entries per spltval.
503 SDOperand UniquedVals[4];
504 assert(Multiple > 1 && Multiple <= 4 && "How can this happen?");
505
506 // See if all of the elements in the buildvector agree across.
507 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
508 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
509 // If the element isn't a constant, bail fully out.
510 if (!isa<ConstantSDNode>(N->getOperand(i))) return SDOperand();
511
512
513 if (UniquedVals[i&(Multiple-1)].Val == 0)
514 UniquedVals[i&(Multiple-1)] = N->getOperand(i);
515 else if (UniquedVals[i&(Multiple-1)] != N->getOperand(i))
516 return SDOperand(); // no match.
517 }
518
519 // Okay, if we reached this point, UniquedVals[0..Multiple-1] contains
520 // either constant or undef values that are identical for each chunk. See
521 // if these chunks can form into a larger vspltis*.
522
523 // Check to see if all of the leading entries are either 0 or -1. If
524 // neither, then this won't fit into the immediate field.
525 bool LeadingZero = true;
526 bool LeadingOnes = true;
527 for (unsigned i = 0; i != Multiple-1; ++i) {
528 if (UniquedVals[i].Val == 0) continue; // Must have been undefs.
529
530 LeadingZero &= cast<ConstantSDNode>(UniquedVals[i])->isNullValue();
531 LeadingOnes &= cast<ConstantSDNode>(UniquedVals[i])->isAllOnesValue();
532 }
533 // Finally, check the least significant entry.
534 if (LeadingZero) {
535 if (UniquedVals[Multiple-1].Val == 0)
536 return DAG.getTargetConstant(0, MVT::i32); // 0,0,0,undef
537 int Val = cast<ConstantSDNode>(UniquedVals[Multiple-1])->getValue();
538 if (Val < 16)
539 return DAG.getTargetConstant(Val, MVT::i32); // 0,0,0,4 -> vspltisw(4)
540 }
541 if (LeadingOnes) {
542 if (UniquedVals[Multiple-1].Val == 0)
543 return DAG.getTargetConstant(~0U, MVT::i32); // -1,-1,-1,undef
544 int Val =cast<ConstantSDNode>(UniquedVals[Multiple-1])->getSignExtended();
545 if (Val >= -16) // -1,-1,-1,-2 -> vspltisw(-2)
546 return DAG.getTargetConstant(Val, MVT::i32);
547 }
548
549 return SDOperand();
550 }
551
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000552 // Check to see if this buildvec has a single non-undef value in its elements.
553 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
554 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
555 if (OpVal.Val == 0)
556 OpVal = N->getOperand(i);
557 else if (OpVal != N->getOperand(i))
Chris Lattner140a58f2006-04-08 06:46:53 +0000558 return SDOperand();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000559 }
560
Chris Lattner140a58f2006-04-08 06:46:53 +0000561 if (OpVal.Val == 0) return SDOperand(); // All UNDEF: use implicit def.
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000562
Nate Begeman98e70cc2006-03-28 04:15:58 +0000563 unsigned ValSizeInBytes = 0;
564 uint64_t Value = 0;
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000565 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
566 Value = CN->getValue();
567 ValSizeInBytes = MVT::getSizeInBits(CN->getValueType(0))/8;
568 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) {
569 assert(CN->getValueType(0) == MVT::f32 && "Only one legal FP vector type!");
570 Value = FloatToBits(CN->getValue());
571 ValSizeInBytes = 4;
572 }
573
574 // If the splat value is larger than the element value, then we can never do
575 // this splat. The only case that we could fit the replicated bits into our
576 // immediate field for would be zero, and we prefer to use vxor for it.
Chris Lattner140a58f2006-04-08 06:46:53 +0000577 if (ValSizeInBytes < ByteSize) return SDOperand();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000578
579 // If the element value is larger than the splat value, cut it in half and
580 // check to see if the two halves are equal. Continue doing this until we
581 // get to ByteSize. This allows us to handle 0x01010101 as 0x01.
582 while (ValSizeInBytes > ByteSize) {
583 ValSizeInBytes >>= 1;
584
585 // If the top half equals the bottom half, we're still ok.
Chris Lattner9b42bdd2006-04-05 17:39:25 +0000586 if (((Value >> (ValSizeInBytes*8)) & ((1 << (8*ValSizeInBytes))-1)) !=
587 (Value & ((1 << (8*ValSizeInBytes))-1)))
Chris Lattner140a58f2006-04-08 06:46:53 +0000588 return SDOperand();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000589 }
590
591 // Properly sign extend the value.
592 int ShAmt = (4-ByteSize)*8;
593 int MaskVal = ((int)Value << ShAmt) >> ShAmt;
594
Evan Cheng5b6a01b2006-03-26 09:52:32 +0000595 // If this is zero, don't match, zero matches ISD::isBuildVectorAllZeros.
Chris Lattner140a58f2006-04-08 06:46:53 +0000596 if (MaskVal == 0) return SDOperand();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000597
Chris Lattner140a58f2006-04-08 06:46:53 +0000598 // Finally, if this value fits in a 5 bit sext field, return it
599 if (((MaskVal << (32-5)) >> (32-5)) == MaskVal)
600 return DAG.getTargetConstant(MaskVal, MVT::i32);
601 return SDOperand();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000602}
603
Chris Lattner1a635d62006-04-14 06:01:58 +0000604//===----------------------------------------------------------------------===//
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000605// Addressing Mode Selection
606//===----------------------------------------------------------------------===//
607
608/// isIntS16Immediate - This method tests to see if the node is either a 32-bit
609/// or 64-bit immediate, and if the value can be accurately represented as a
610/// sign extension from a 16-bit value. If so, this returns true and the
611/// immediate.
612static bool isIntS16Immediate(SDNode *N, short &Imm) {
613 if (N->getOpcode() != ISD::Constant)
614 return false;
615
616 Imm = (short)cast<ConstantSDNode>(N)->getValue();
617 if (N->getValueType(0) == MVT::i32)
618 return Imm == (int32_t)cast<ConstantSDNode>(N)->getValue();
619 else
620 return Imm == (int64_t)cast<ConstantSDNode>(N)->getValue();
621}
622static bool isIntS16Immediate(SDOperand Op, short &Imm) {
623 return isIntS16Immediate(Op.Val, Imm);
624}
625
626
627/// SelectAddressRegReg - Given the specified addressed, check to see if it
628/// can be represented as an indexed [r+r] operation. Returns false if it
629/// can be more efficiently represented with [r+imm].
630bool PPCTargetLowering::SelectAddressRegReg(SDOperand N, SDOperand &Base,
631 SDOperand &Index,
632 SelectionDAG &DAG) {
633 short imm = 0;
634 if (N.getOpcode() == ISD::ADD) {
635 if (isIntS16Immediate(N.getOperand(1), imm))
636 return false; // r+i
637 if (N.getOperand(1).getOpcode() == PPCISD::Lo)
638 return false; // r+i
639
640 Base = N.getOperand(0);
641 Index = N.getOperand(1);
642 return true;
643 } else if (N.getOpcode() == ISD::OR) {
644 if (isIntS16Immediate(N.getOperand(1), imm))
645 return false; // r+i can fold it if we can.
646
647 // If this is an or of disjoint bitfields, we can codegen this as an add
648 // (for better address arithmetic) if the LHS and RHS of the OR are provably
649 // disjoint.
650 uint64_t LHSKnownZero, LHSKnownOne;
651 uint64_t RHSKnownZero, RHSKnownOne;
652 ComputeMaskedBits(N.getOperand(0), ~0U, LHSKnownZero, LHSKnownOne);
653
654 if (LHSKnownZero) {
655 ComputeMaskedBits(N.getOperand(1), ~0U, RHSKnownZero, RHSKnownOne);
656 // If all of the bits are known zero on the LHS or RHS, the add won't
657 // carry.
658 if ((LHSKnownZero | RHSKnownZero) == ~0U) {
659 Base = N.getOperand(0);
660 Index = N.getOperand(1);
661 return true;
662 }
663 }
664 }
665
666 return false;
667}
668
669/// Returns true if the address N can be represented by a base register plus
670/// a signed 16-bit displacement [r+imm], and if it is not better
671/// represented as reg+reg.
672bool PPCTargetLowering::SelectAddressRegImm(SDOperand N, SDOperand &Disp,
673 SDOperand &Base, SelectionDAG &DAG){
674 // If this can be more profitably realized as r+r, fail.
675 if (SelectAddressRegReg(N, Disp, Base, DAG))
676 return false;
677
678 if (N.getOpcode() == ISD::ADD) {
679 short imm = 0;
680 if (isIntS16Immediate(N.getOperand(1), imm)) {
681 Disp = DAG.getTargetConstant((int)imm & 0xFFFF, MVT::i32);
682 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
683 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
684 } else {
685 Base = N.getOperand(0);
686 }
687 return true; // [r+i]
688 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
689 // Match LOAD (ADD (X, Lo(G))).
690 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getValue()
691 && "Cannot handle constant offsets yet!");
692 Disp = N.getOperand(1).getOperand(0); // The global address.
693 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
694 Disp.getOpcode() == ISD::TargetConstantPool ||
695 Disp.getOpcode() == ISD::TargetJumpTable);
696 Base = N.getOperand(0);
697 return true; // [&g+r]
698 }
699 } else if (N.getOpcode() == ISD::OR) {
700 short imm = 0;
701 if (isIntS16Immediate(N.getOperand(1), imm)) {
702 // If this is an or of disjoint bitfields, we can codegen this as an add
703 // (for better address arithmetic) if the LHS and RHS of the OR are
704 // provably disjoint.
705 uint64_t LHSKnownZero, LHSKnownOne;
706 ComputeMaskedBits(N.getOperand(0), ~0U, LHSKnownZero, LHSKnownOne);
707 if ((LHSKnownZero|~(unsigned)imm) == ~0U) {
708 // If all of the bits are known zero on the LHS or RHS, the add won't
709 // carry.
710 Base = N.getOperand(0);
711 Disp = DAG.getTargetConstant((int)imm & 0xFFFF, MVT::i32);
712 return true;
713 }
714 }
715 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
716 // Loading from a constant address.
717
718 // If this address fits entirely in a 16-bit sext immediate field, codegen
719 // this as "d, 0"
720 short Imm;
721 if (isIntS16Immediate(CN, Imm)) {
722 Disp = DAG.getTargetConstant(Imm, CN->getValueType(0));
723 Base = DAG.getRegister(PPC::R0, CN->getValueType(0));
724 return true;
725 }
726
727 // FIXME: Handle small sext constant offsets in PPC64 mode also!
728 if (CN->getValueType(0) == MVT::i32) {
729 int Addr = (int)CN->getValue();
730
731 // Otherwise, break this down into an LIS + disp.
732 Disp = DAG.getTargetConstant((short)Addr, MVT::i32);
733 Base = DAG.getConstant(Addr - (signed short)Addr, MVT::i32);
734 return true;
735 }
736 }
737
738 Disp = DAG.getTargetConstant(0, getPointerTy());
739 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
740 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
741 else
742 Base = N;
743 return true; // [r+0]
744}
745
746/// SelectAddressRegRegOnly - Given the specified addressed, force it to be
747/// represented as an indexed [r+r] operation.
748bool PPCTargetLowering::SelectAddressRegRegOnly(SDOperand N, SDOperand &Base,
749 SDOperand &Index,
750 SelectionDAG &DAG) {
751 // Check to see if we can easily represent this as an [r+r] address. This
752 // will fail if it thinks that the address is more profitably represented as
753 // reg+imm, e.g. where imm = 0.
754 if (SelectAddressRegReg(N, Base, Index, DAG))
755 return true;
756
757 // If the operand is an addition, always emit this as [r+r], since this is
758 // better (for code size, and execution, as the memop does the add for free)
759 // than emitting an explicit add.
760 if (N.getOpcode() == ISD::ADD) {
761 Base = N.getOperand(0);
762 Index = N.getOperand(1);
763 return true;
764 }
765
766 // Otherwise, do it the hard way, using R0 as the base register.
767 Base = DAG.getRegister(PPC::R0, N.getValueType());
768 Index = N;
769 return true;
770}
771
772/// SelectAddressRegImmShift - Returns true if the address N can be
773/// represented by a base register plus a signed 14-bit displacement
774/// [r+imm*4]. Suitable for use by STD and friends.
775bool PPCTargetLowering::SelectAddressRegImmShift(SDOperand N, SDOperand &Disp,
776 SDOperand &Base,
777 SelectionDAG &DAG) {
778 // If this can be more profitably realized as r+r, fail.
779 if (SelectAddressRegReg(N, Disp, Base, DAG))
780 return false;
781
782 if (N.getOpcode() == ISD::ADD) {
783 short imm = 0;
784 if (isIntS16Immediate(N.getOperand(1), imm) && (imm & 3) == 0) {
785 Disp = DAG.getTargetConstant(((int)imm & 0xFFFF) >> 2, MVT::i32);
786 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
787 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
788 } else {
789 Base = N.getOperand(0);
790 }
791 return true; // [r+i]
792 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
793 // Match LOAD (ADD (X, Lo(G))).
794 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getValue()
795 && "Cannot handle constant offsets yet!");
796 Disp = N.getOperand(1).getOperand(0); // The global address.
797 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
798 Disp.getOpcode() == ISD::TargetConstantPool ||
799 Disp.getOpcode() == ISD::TargetJumpTable);
800 Base = N.getOperand(0);
801 return true; // [&g+r]
802 }
803 } else if (N.getOpcode() == ISD::OR) {
804 short imm = 0;
805 if (isIntS16Immediate(N.getOperand(1), imm) && (imm & 3) == 0) {
806 // If this is an or of disjoint bitfields, we can codegen this as an add
807 // (for better address arithmetic) if the LHS and RHS of the OR are
808 // provably disjoint.
809 uint64_t LHSKnownZero, LHSKnownOne;
810 ComputeMaskedBits(N.getOperand(0), ~0U, LHSKnownZero, LHSKnownOne);
811 if ((LHSKnownZero|~(unsigned)imm) == ~0U) {
812 // If all of the bits are known zero on the LHS or RHS, the add won't
813 // carry.
814 Base = N.getOperand(0);
815 Disp = DAG.getTargetConstant(((int)imm & 0xFFFF) >> 2, MVT::i32);
816 return true;
817 }
818 }
819 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
820 // Loading from a constant address.
821
822 // If this address fits entirely in a 14-bit sext immediate field, codegen
823 // this as "d, 0"
824 short Imm;
825 if (isIntS16Immediate(CN, Imm)) {
826 Disp = DAG.getTargetConstant((unsigned short)Imm >> 2, getPointerTy());
827 Base = DAG.getRegister(PPC::R0, CN->getValueType(0));
828 return true;
829 }
830
831 // FIXME: Handle small sext constant offsets in PPC64 mode also!
832 if (CN->getValueType(0) == MVT::i32) {
833 int Addr = (int)CN->getValue();
834
835 // Otherwise, break this down into an LIS + disp.
836 Disp = DAG.getTargetConstant((short)Addr >> 2, MVT::i32);
837 Base = DAG.getConstant(Addr - (signed short)Addr, MVT::i32);
838 return true;
839 }
840 }
841
842 Disp = DAG.getTargetConstant(0, getPointerTy());
843 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
844 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
845 else
846 Base = N;
847 return true; // [r+0]
848}
849
850
851/// getPreIndexedAddressParts - returns true by value, base pointer and
852/// offset pointer and addressing mode by reference if the node's address
853/// can be legally represented as pre-indexed load / store address.
854bool PPCTargetLowering::getPreIndexedAddressParts(SDNode *N, SDOperand &Base,
855 SDOperand &Offset,
856 ISD::MemOpAddrMode &AM,
857 SelectionDAG &DAG) {
858 return false;
859
860#if 0
861 MVT::ValueType VT;
862 SDOperand Ptr;
863 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
864 Ptr = LD->getBasePtr();
865 VT = LD->getLoadedVT();
866
867 // TODO: handle other cases.
868 if (VT != MVT::i32) return false;
869 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
870 Ptr = ST->getBasePtr();
871 VT = ST->getStoredVT();
872 // TODO: handle other cases.
873 return false;
874 } else
875 return false;
876
877
878
879 return false;
880#endif
881}
882
883//===----------------------------------------------------------------------===//
Chris Lattner1a635d62006-04-14 06:01:58 +0000884// LowerOperation implementation
885//===----------------------------------------------------------------------===//
886
887static SDOperand LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner059ca0f2006-06-16 21:01:35 +0000888 MVT::ValueType PtrVT = Op.getValueType();
Chris Lattner1a635d62006-04-14 06:01:58 +0000889 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Chengc356a572006-09-12 21:04:05 +0000890 Constant *C = CP->getConstVal();
Chris Lattner059ca0f2006-06-16 21:01:35 +0000891 SDOperand CPI = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment());
892 SDOperand Zero = DAG.getConstant(0, PtrVT);
Chris Lattner1a635d62006-04-14 06:01:58 +0000893
894 const TargetMachine &TM = DAG.getTarget();
895
Chris Lattner059ca0f2006-06-16 21:01:35 +0000896 SDOperand Hi = DAG.getNode(PPCISD::Hi, PtrVT, CPI, Zero);
897 SDOperand Lo = DAG.getNode(PPCISD::Lo, PtrVT, CPI, Zero);
898
Chris Lattner1a635d62006-04-14 06:01:58 +0000899 // If this is a non-darwin platform, we don't support non-static relo models
900 // yet.
901 if (TM.getRelocationModel() == Reloc::Static ||
902 !TM.getSubtarget<PPCSubtarget>().isDarwin()) {
903 // Generate non-pic code that has direct accesses to the constant pool.
904 // The address of the global is just (hi(&g)+lo(&g)).
Chris Lattner059ca0f2006-06-16 21:01:35 +0000905 return DAG.getNode(ISD::ADD, PtrVT, Hi, Lo);
Chris Lattner1a635d62006-04-14 06:01:58 +0000906 }
907
Chris Lattner35d86fe2006-07-26 21:12:04 +0000908 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattner1a635d62006-04-14 06:01:58 +0000909 // With PIC, the first instruction is actually "GR+hi(&G)".
Chris Lattner059ca0f2006-06-16 21:01:35 +0000910 Hi = DAG.getNode(ISD::ADD, PtrVT,
911 DAG.getNode(PPCISD::GlobalBaseReg, PtrVT), Hi);
Chris Lattner1a635d62006-04-14 06:01:58 +0000912 }
913
Chris Lattner059ca0f2006-06-16 21:01:35 +0000914 Lo = DAG.getNode(ISD::ADD, PtrVT, Hi, Lo);
Chris Lattner1a635d62006-04-14 06:01:58 +0000915 return Lo;
916}
917
Nate Begeman37efe672006-04-22 18:53:45 +0000918static SDOperand LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner059ca0f2006-06-16 21:01:35 +0000919 MVT::ValueType PtrVT = Op.getValueType();
Nate Begeman37efe672006-04-22 18:53:45 +0000920 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Chris Lattner059ca0f2006-06-16 21:01:35 +0000921 SDOperand JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
922 SDOperand Zero = DAG.getConstant(0, PtrVT);
Nate Begeman37efe672006-04-22 18:53:45 +0000923
924 const TargetMachine &TM = DAG.getTarget();
Chris Lattner059ca0f2006-06-16 21:01:35 +0000925
926 SDOperand Hi = DAG.getNode(PPCISD::Hi, PtrVT, JTI, Zero);
927 SDOperand Lo = DAG.getNode(PPCISD::Lo, PtrVT, JTI, Zero);
928
Nate Begeman37efe672006-04-22 18:53:45 +0000929 // If this is a non-darwin platform, we don't support non-static relo models
930 // yet.
931 if (TM.getRelocationModel() == Reloc::Static ||
932 !TM.getSubtarget<PPCSubtarget>().isDarwin()) {
933 // Generate non-pic code that has direct accesses to the constant pool.
934 // The address of the global is just (hi(&g)+lo(&g)).
Chris Lattner059ca0f2006-06-16 21:01:35 +0000935 return DAG.getNode(ISD::ADD, PtrVT, Hi, Lo);
Nate Begeman37efe672006-04-22 18:53:45 +0000936 }
937
Chris Lattner35d86fe2006-07-26 21:12:04 +0000938 if (TM.getRelocationModel() == Reloc::PIC_) {
Nate Begeman37efe672006-04-22 18:53:45 +0000939 // With PIC, the first instruction is actually "GR+hi(&G)".
Chris Lattner059ca0f2006-06-16 21:01:35 +0000940 Hi = DAG.getNode(ISD::ADD, PtrVT,
Chris Lattner0d72a202006-07-28 16:45:47 +0000941 DAG.getNode(PPCISD::GlobalBaseReg, PtrVT), Hi);
Nate Begeman37efe672006-04-22 18:53:45 +0000942 }
943
Chris Lattner059ca0f2006-06-16 21:01:35 +0000944 Lo = DAG.getNode(ISD::ADD, PtrVT, Hi, Lo);
Nate Begeman37efe672006-04-22 18:53:45 +0000945 return Lo;
946}
947
Chris Lattner1a635d62006-04-14 06:01:58 +0000948static SDOperand LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner059ca0f2006-06-16 21:01:35 +0000949 MVT::ValueType PtrVT = Op.getValueType();
Chris Lattner1a635d62006-04-14 06:01:58 +0000950 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
951 GlobalValue *GV = GSDN->getGlobal();
Chris Lattner059ca0f2006-06-16 21:01:35 +0000952 SDOperand GA = DAG.getTargetGlobalAddress(GV, PtrVT, GSDN->getOffset());
953 SDOperand Zero = DAG.getConstant(0, PtrVT);
Chris Lattner1a635d62006-04-14 06:01:58 +0000954
955 const TargetMachine &TM = DAG.getTarget();
956
Chris Lattner059ca0f2006-06-16 21:01:35 +0000957 SDOperand Hi = DAG.getNode(PPCISD::Hi, PtrVT, GA, Zero);
958 SDOperand Lo = DAG.getNode(PPCISD::Lo, PtrVT, GA, Zero);
959
Chris Lattner1a635d62006-04-14 06:01:58 +0000960 // If this is a non-darwin platform, we don't support non-static relo models
961 // yet.
962 if (TM.getRelocationModel() == Reloc::Static ||
963 !TM.getSubtarget<PPCSubtarget>().isDarwin()) {
964 // Generate non-pic code that has direct accesses to globals.
965 // The address of the global is just (hi(&g)+lo(&g)).
Chris Lattner059ca0f2006-06-16 21:01:35 +0000966 return DAG.getNode(ISD::ADD, PtrVT, Hi, Lo);
Chris Lattner1a635d62006-04-14 06:01:58 +0000967 }
968
Chris Lattner35d86fe2006-07-26 21:12:04 +0000969 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattner1a635d62006-04-14 06:01:58 +0000970 // With PIC, the first instruction is actually "GR+hi(&G)".
Chris Lattner059ca0f2006-06-16 21:01:35 +0000971 Hi = DAG.getNode(ISD::ADD, PtrVT,
972 DAG.getNode(PPCISD::GlobalBaseReg, PtrVT), Hi);
Chris Lattner1a635d62006-04-14 06:01:58 +0000973 }
974
Chris Lattner059ca0f2006-06-16 21:01:35 +0000975 Lo = DAG.getNode(ISD::ADD, PtrVT, Hi, Lo);
Chris Lattner1a635d62006-04-14 06:01:58 +0000976
977 if (!GV->hasWeakLinkage() && !GV->hasLinkOnceLinkage() &&
978 (!GV->isExternal() || GV->hasNotBeenReadFromBytecode()))
979 return Lo;
980
981 // If the global is weak or external, we have to go through the lazy
982 // resolution stub.
Evan Cheng466685d2006-10-09 20:57:25 +0000983 return DAG.getLoad(PtrVT, DAG.getEntryNode(), Lo, NULL, 0);
Chris Lattner1a635d62006-04-14 06:01:58 +0000984}
985
986static SDOperand LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
987 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
988
989 // If we're comparing for equality to zero, expose the fact that this is
990 // implented as a ctlz/srl pair on ppc, so that the dag combiner can
991 // fold the new nodes.
992 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
993 if (C->isNullValue() && CC == ISD::SETEQ) {
994 MVT::ValueType VT = Op.getOperand(0).getValueType();
995 SDOperand Zext = Op.getOperand(0);
996 if (VT < MVT::i32) {
997 VT = MVT::i32;
998 Zext = DAG.getNode(ISD::ZERO_EXTEND, VT, Op.getOperand(0));
999 }
1000 unsigned Log2b = Log2_32(MVT::getSizeInBits(VT));
1001 SDOperand Clz = DAG.getNode(ISD::CTLZ, VT, Zext);
1002 SDOperand Scc = DAG.getNode(ISD::SRL, VT, Clz,
1003 DAG.getConstant(Log2b, MVT::i32));
1004 return DAG.getNode(ISD::TRUNCATE, MVT::i32, Scc);
1005 }
1006 // Leave comparisons against 0 and -1 alone for now, since they're usually
1007 // optimized. FIXME: revisit this when we can custom lower all setcc
1008 // optimizations.
1009 if (C->isAllOnesValue() || C->isNullValue())
1010 return SDOperand();
1011 }
1012
1013 // If we have an integer seteq/setne, turn it into a compare against zero
1014 // by subtracting the rhs from the lhs, which is faster than setting a
1015 // condition register, reading it back out, and masking the correct bit.
1016 MVT::ValueType LHSVT = Op.getOperand(0).getValueType();
1017 if (MVT::isInteger(LHSVT) && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
1018 MVT::ValueType VT = Op.getValueType();
1019 SDOperand Sub = DAG.getNode(ISD::SUB, LHSVT, Op.getOperand(0),
1020 Op.getOperand(1));
1021 return DAG.getSetCC(VT, Sub, DAG.getConstant(0, LHSVT), CC);
1022 }
1023 return SDOperand();
1024}
1025
1026static SDOperand LowerVASTART(SDOperand Op, SelectionDAG &DAG,
1027 unsigned VarArgsFrameIndex) {
1028 // vastart just stores the address of the VarArgsFrameIndex slot into the
1029 // memory location argument.
Chris Lattner0d72a202006-07-28 16:45:47 +00001030 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1031 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001032 SrcValueSDNode *SV = cast<SrcValueSDNode>(Op.getOperand(2));
1033 return DAG.getStore(Op.getOperand(0), FR, Op.getOperand(1), SV->getValue(),
1034 SV->getOffset());
Chris Lattner1a635d62006-04-14 06:01:58 +00001035}
1036
Chris Lattnerc91a4752006-06-26 22:48:35 +00001037static SDOperand LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG,
1038 int &VarArgsFrameIndex) {
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001039 // TODO: add description of PPC stack frame format, or at least some docs.
1040 //
1041 MachineFunction &MF = DAG.getMachineFunction();
1042 MachineFrameInfo *MFI = MF.getFrameInfo();
1043 SSARegMap *RegMap = MF.getSSARegMap();
Chris Lattner79e490a2006-08-11 17:18:05 +00001044 SmallVector<SDOperand, 8> ArgValues;
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001045 SDOperand Root = Op.getOperand(0);
1046
1047 unsigned ArgOffset = 24;
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00001048 const unsigned Num_GPR_Regs = 8;
1049 const unsigned Num_FPR_Regs = 13;
1050 const unsigned Num_VR_Regs = 12;
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001051 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Chris Lattnerc91a4752006-06-26 22:48:35 +00001052
1053 static const unsigned GPR_32[] = { // 32-bit registers.
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001054 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1055 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1056 };
Chris Lattnerc91a4752006-06-26 22:48:35 +00001057 static const unsigned GPR_64[] = { // 64-bit registers.
1058 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
1059 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
1060 };
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001061 static const unsigned FPR[] = {
1062 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1063 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1064 };
1065 static const unsigned VR[] = {
1066 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
1067 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
1068 };
Chris Lattnerc91a4752006-06-26 22:48:35 +00001069
1070 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1071 bool isPPC64 = PtrVT == MVT::i64;
1072 const unsigned *GPR = isPPC64 ? GPR_64 : GPR_32;
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001073
1074 // Add DAG nodes to load the arguments or copy them out of registers. On
1075 // entry to a function on PPC, the arguments start at offset 24, although the
1076 // first ones are often in registers.
1077 for (unsigned ArgNo = 0, e = Op.Val->getNumValues()-1; ArgNo != e; ++ArgNo) {
1078 SDOperand ArgVal;
1079 bool needsLoad = false;
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001080 MVT::ValueType ObjectVT = Op.getValue(ArgNo).getValueType();
1081 unsigned ObjSize = MVT::getSizeInBits(ObjectVT)/8;
1082
Chris Lattnerbe4849a2006-05-16 18:51:52 +00001083 unsigned CurArgOffset = ArgOffset;
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001084 switch (ObjectVT) {
1085 default: assert(0 && "Unhandled argument type!");
1086 case MVT::i32:
Chris Lattnerbe4849a2006-05-16 18:51:52 +00001087 // All int arguments reserve stack space.
Chris Lattnerc91a4752006-06-26 22:48:35 +00001088 ArgOffset += isPPC64 ? 8 : 4;
Chris Lattnerbe4849a2006-05-16 18:51:52 +00001089
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00001090 if (GPR_idx != Num_GPR_Regs) {
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001091 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
1092 MF.addLiveIn(GPR[GPR_idx], VReg);
1093 ArgVal = DAG.getCopyFromReg(Root, VReg, MVT::i32);
Chris Lattnerbe4849a2006-05-16 18:51:52 +00001094 ++GPR_idx;
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001095 } else {
1096 needsLoad = true;
1097 }
1098 break;
Chris Lattnerc91a4752006-06-26 22:48:35 +00001099 case MVT::i64: // PPC64
1100 // All int arguments reserve stack space.
1101 ArgOffset += 8;
1102
1103 if (GPR_idx != Num_GPR_Regs) {
1104 unsigned VReg = RegMap->createVirtualRegister(&PPC::G8RCRegClass);
1105 MF.addLiveIn(GPR[GPR_idx], VReg);
1106 ArgVal = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1107 ++GPR_idx;
1108 } else {
1109 needsLoad = true;
1110 }
1111 break;
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001112 case MVT::f32:
1113 case MVT::f64:
Chris Lattnerbe4849a2006-05-16 18:51:52 +00001114 // All FP arguments reserve stack space.
1115 ArgOffset += ObjSize;
1116
1117 // Every 4 bytes of argument space consumes one of the GPRs available for
1118 // argument passing.
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00001119 if (GPR_idx != Num_GPR_Regs) {
1120 ++GPR_idx;
1121 if (ObjSize == 8 && GPR_idx != Num_GPR_Regs)
1122 ++GPR_idx;
Chris Lattnerbe4849a2006-05-16 18:51:52 +00001123 }
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00001124 if (FPR_idx != Num_FPR_Regs) {
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001125 unsigned VReg;
1126 if (ObjectVT == MVT::f32)
1127 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
1128 else
1129 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
1130 MF.addLiveIn(FPR[FPR_idx], VReg);
1131 ArgVal = DAG.getCopyFromReg(Root, VReg, ObjectVT);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001132 ++FPR_idx;
1133 } else {
1134 needsLoad = true;
1135 }
1136 break;
1137 case MVT::v4f32:
1138 case MVT::v4i32:
1139 case MVT::v8i16:
1140 case MVT::v16i8:
Chris Lattnerbe4849a2006-05-16 18:51:52 +00001141 // Note that vector arguments in registers don't reserve stack space.
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00001142 if (VR_idx != Num_VR_Regs) {
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001143 unsigned VReg = RegMap->createVirtualRegister(&PPC::VRRCRegClass);
1144 MF.addLiveIn(VR[VR_idx], VReg);
1145 ArgVal = DAG.getCopyFromReg(Root, VReg, ObjectVT);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001146 ++VR_idx;
1147 } else {
1148 // This should be simple, but requires getting 16-byte aligned stack
1149 // values.
1150 assert(0 && "Loading VR argument not implemented yet!");
1151 needsLoad = true;
1152 }
1153 break;
1154 }
1155
1156 // We need to load the argument to a virtual register if we determined above
1157 // that we ran out of physical registers of the appropriate type
1158 if (needsLoad) {
Chris Lattnerb375b5e2006-05-16 18:54:32 +00001159 // If the argument is actually used, emit a load from the right stack
1160 // slot.
1161 if (!Op.Val->hasNUsesOfValue(0, ArgNo)) {
1162 int FI = MFI->CreateFixedObject(ObjSize, CurArgOffset);
Chris Lattnerc91a4752006-06-26 22:48:35 +00001163 SDOperand FIN = DAG.getFrameIndex(FI, PtrVT);
Evan Cheng466685d2006-10-09 20:57:25 +00001164 ArgVal = DAG.getLoad(ObjectVT, Root, FIN, NULL, 0);
Chris Lattnerb375b5e2006-05-16 18:54:32 +00001165 } else {
1166 // Don't emit a dead load.
1167 ArgVal = DAG.getNode(ISD::UNDEF, ObjectVT);
1168 }
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001169 }
1170
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001171 ArgValues.push_back(ArgVal);
1172 }
1173
1174 // If the function takes variable number of arguments, make a frame index for
1175 // the start of the first vararg value... for expansion of llvm.va_start.
1176 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1177 if (isVarArg) {
Chris Lattnerc91a4752006-06-26 22:48:35 +00001178 VarArgsFrameIndex = MFI->CreateFixedObject(MVT::getSizeInBits(PtrVT)/8,
1179 ArgOffset);
1180 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001181 // If this function is vararg, store any remaining integer argument regs
1182 // to their spots on the stack so that they may be loaded by deferencing the
1183 // result of va_next.
Chris Lattnere2199452006-08-11 17:38:39 +00001184 SmallVector<SDOperand, 8> MemOps;
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00001185 for (; GPR_idx != Num_GPR_Regs; ++GPR_idx) {
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001186 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
1187 MF.addLiveIn(GPR[GPR_idx], VReg);
Chris Lattnerc91a4752006-06-26 22:48:35 +00001188 SDOperand Val = DAG.getCopyFromReg(Root, VReg, PtrVT);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001189 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001190 MemOps.push_back(Store);
1191 // Increment the address by four for the next argument to store
Chris Lattnerc91a4752006-06-26 22:48:35 +00001192 SDOperand PtrOff = DAG.getConstant(MVT::getSizeInBits(PtrVT)/8, PtrVT);
1193 FIN = DAG.getNode(ISD::ADD, PtrOff.getValueType(), FIN, PtrOff);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001194 }
1195 if (!MemOps.empty())
Chris Lattnere2199452006-08-11 17:38:39 +00001196 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,&MemOps[0],MemOps.size());
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001197 }
1198
1199 ArgValues.push_back(Root);
1200
1201 // Return the new list of results.
1202 std::vector<MVT::ValueType> RetVT(Op.Val->value_begin(),
1203 Op.Val->value_end());
Chris Lattner79e490a2006-08-11 17:18:05 +00001204 return DAG.getNode(ISD::MERGE_VALUES, RetVT, &ArgValues[0], ArgValues.size());
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001205}
1206
Chris Lattnerc703a8f2006-05-17 19:00:46 +00001207/// isCallCompatibleAddress - Return the immediate to use if the specified
1208/// 32-bit value is representable in the immediate field of a BxA instruction.
1209static SDNode *isBLACompatibleAddress(SDOperand Op, SelectionDAG &DAG) {
1210 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
1211 if (!C) return 0;
1212
1213 int Addr = C->getValue();
1214 if ((Addr & 3) != 0 || // Low 2 bits are implicitly zero.
1215 (Addr << 6 >> 6) != Addr)
1216 return 0; // Top 6 bits have to be sext of immediate.
1217
1218 return DAG.getConstant((int)C->getValue() >> 2, MVT::i32).Val;
1219}
1220
1221
Chris Lattnerabde4602006-05-16 22:56:08 +00001222static SDOperand LowerCALL(SDOperand Op, SelectionDAG &DAG) {
1223 SDOperand Chain = Op.getOperand(0);
Chris Lattnerabde4602006-05-16 22:56:08 +00001224 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Chris Lattnerabde4602006-05-16 22:56:08 +00001225 SDOperand Callee = Op.getOperand(4);
Evan Cheng4360bdc2006-05-25 00:57:32 +00001226 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
1227
Chris Lattnerc91a4752006-06-26 22:48:35 +00001228 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1229 bool isPPC64 = PtrVT == MVT::i64;
1230 unsigned PtrByteSize = isPPC64 ? 8 : 4;
1231
1232
Chris Lattnerabde4602006-05-16 22:56:08 +00001233 // args_to_use will accumulate outgoing args for the PPCISD::CALL case in
1234 // SelectExpr to use to put the arguments in the appropriate registers.
1235 std::vector<SDOperand> args_to_use;
1236
1237 // Count how many bytes are to be pushed on the stack, including the linkage
Chris Lattnerc91a4752006-06-26 22:48:35 +00001238 // area, and parameter passing area. We start with 24/48 bytes, which is
Chris Lattnerc8b682c2006-05-17 00:15:40 +00001239 // prereserved space for [SP][CR][LR][3 x unused].
Chris Lattnerc91a4752006-06-26 22:48:35 +00001240 unsigned NumBytes = 6*PtrByteSize;
Chris Lattnerabde4602006-05-16 22:56:08 +00001241
Chris Lattnerc8b682c2006-05-17 00:15:40 +00001242 // Add up all the space actually used.
Evan Cheng4360bdc2006-05-25 00:57:32 +00001243 for (unsigned i = 0; i != NumOps; ++i)
1244 NumBytes += MVT::getSizeInBits(Op.getOperand(5+2*i).getValueType())/8;
Chris Lattnerc04ba7a2006-05-16 23:54:25 +00001245
Chris Lattner7b053502006-05-30 21:21:04 +00001246 // The prolog code of the callee may store up to 8 GPR argument registers to
1247 // the stack, allowing va_start to index over them in memory if its varargs.
1248 // Because we cannot tell if this is needed on the caller side, we have to
1249 // conservatively assume that it is needed. As such, make sure we have at
1250 // least enough stack space for the caller to store the 8 GPRs.
Chris Lattnerc91a4752006-06-26 22:48:35 +00001251 if (NumBytes < 6*PtrByteSize+8*PtrByteSize)
1252 NumBytes = 6*PtrByteSize+8*PtrByteSize;
Chris Lattnerc8b682c2006-05-17 00:15:40 +00001253
1254 // Adjust the stack pointer for the new arguments...
1255 // These operations are automatically eliminated by the prolog/epilog pass
1256 Chain = DAG.getCALLSEQ_START(Chain,
Chris Lattnerc91a4752006-06-26 22:48:35 +00001257 DAG.getConstant(NumBytes, PtrVT));
Chris Lattnerc8b682c2006-05-17 00:15:40 +00001258
1259 // Set up a copy of the stack pointer for use loading and storing any
1260 // arguments that may not fit in the registers available for argument
1261 // passing.
Chris Lattnerc91a4752006-06-26 22:48:35 +00001262 SDOperand StackPtr;
1263 if (isPPC64)
1264 StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
1265 else
1266 StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Chris Lattnerc8b682c2006-05-17 00:15:40 +00001267
1268 // Figure out which arguments are going to go in registers, and which in
1269 // memory. Also, if this is a vararg function, floating point operations
1270 // must be stored to our stack, and loaded into integer regs as well, if
1271 // any integer regs are available for argument passing.
Chris Lattnerc91a4752006-06-26 22:48:35 +00001272 unsigned ArgOffset = 6*PtrByteSize;
Chris Lattner9a2a4972006-05-17 06:01:33 +00001273 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Chris Lattnerc91a4752006-06-26 22:48:35 +00001274 static const unsigned GPR_32[] = { // 32-bit registers.
Chris Lattner9a2a4972006-05-17 06:01:33 +00001275 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1276 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1277 };
Chris Lattnerc91a4752006-06-26 22:48:35 +00001278 static const unsigned GPR_64[] = { // 64-bit registers.
1279 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
1280 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
1281 };
Chris Lattner9a2a4972006-05-17 06:01:33 +00001282 static const unsigned FPR[] = {
1283 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1284 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1285 };
1286 static const unsigned VR[] = {
1287 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
1288 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
1289 };
Chris Lattnerc91a4752006-06-26 22:48:35 +00001290 const unsigned NumGPRs = sizeof(GPR_32)/sizeof(GPR_32[0]);
Chris Lattner9a2a4972006-05-17 06:01:33 +00001291 const unsigned NumFPRs = sizeof(FPR)/sizeof(FPR[0]);
1292 const unsigned NumVRs = sizeof( VR)/sizeof( VR[0]);
1293
Chris Lattnerc91a4752006-06-26 22:48:35 +00001294 const unsigned *GPR = isPPC64 ? GPR_64 : GPR_32;
1295
Chris Lattner9a2a4972006-05-17 06:01:33 +00001296 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
Chris Lattnere2199452006-08-11 17:38:39 +00001297 SmallVector<SDOperand, 8> MemOpChains;
Evan Cheng4360bdc2006-05-25 00:57:32 +00001298 for (unsigned i = 0; i != NumOps; ++i) {
1299 SDOperand Arg = Op.getOperand(5+2*i);
Chris Lattnerc8b682c2006-05-17 00:15:40 +00001300
1301 // PtrOff will be used to store the current argument to the stack if a
1302 // register cannot be found for it.
1303 SDOperand PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
Chris Lattnerc91a4752006-06-26 22:48:35 +00001304 PtrOff = DAG.getNode(ISD::ADD, PtrVT, StackPtr, PtrOff);
1305
1306 // On PPC64, promote integers to 64-bit values.
1307 if (isPPC64 && Arg.getValueType() == MVT::i32) {
1308 unsigned ExtOp = ISD::ZERO_EXTEND;
1309 if (cast<ConstantSDNode>(Op.getOperand(5+2*i+1))->getValue())
1310 ExtOp = ISD::SIGN_EXTEND;
1311 Arg = DAG.getNode(ExtOp, MVT::i64, Arg);
1312 }
1313
Chris Lattnerc8b682c2006-05-17 00:15:40 +00001314 switch (Arg.getValueType()) {
1315 default: assert(0 && "Unexpected ValueType for argument!");
1316 case MVT::i32:
Chris Lattnerc91a4752006-06-26 22:48:35 +00001317 case MVT::i64:
Chris Lattner9a2a4972006-05-17 06:01:33 +00001318 if (GPR_idx != NumGPRs) {
1319 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg));
Chris Lattnerc8b682c2006-05-17 00:15:40 +00001320 } else {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001321 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
Chris Lattnerc8b682c2006-05-17 00:15:40 +00001322 }
Chris Lattnerc91a4752006-06-26 22:48:35 +00001323 ArgOffset += PtrByteSize;
Chris Lattnerc8b682c2006-05-17 00:15:40 +00001324 break;
1325 case MVT::f32:
1326 case MVT::f64:
Chris Lattner9a2a4972006-05-17 06:01:33 +00001327 if (FPR_idx != NumFPRs) {
1328 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg));
1329
Chris Lattnerc8b682c2006-05-17 00:15:40 +00001330 if (isVarArg) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001331 SDOperand Store = DAG.getStore(Chain, Arg, PtrOff, NULL, 0);
Chris Lattner9a2a4972006-05-17 06:01:33 +00001332 MemOpChains.push_back(Store);
1333
Chris Lattnerc8b682c2006-05-17 00:15:40 +00001334 // Float varargs are always shadowed in available integer registers
Chris Lattner9a2a4972006-05-17 06:01:33 +00001335 if (GPR_idx != NumGPRs) {
Evan Cheng466685d2006-10-09 20:57:25 +00001336 SDOperand Load = DAG.getLoad(PtrVT, Store, PtrOff, NULL, 0);
Chris Lattner9a2a4972006-05-17 06:01:33 +00001337 MemOpChains.push_back(Load.getValue(1));
1338 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Chris Lattnerc8b682c2006-05-17 00:15:40 +00001339 }
Chris Lattner9a2a4972006-05-17 06:01:33 +00001340 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64) {
Chris Lattnerc8b682c2006-05-17 00:15:40 +00001341 SDOperand ConstFour = DAG.getConstant(4, PtrOff.getValueType());
Chris Lattnerc91a4752006-06-26 22:48:35 +00001342 PtrOff = DAG.getNode(ISD::ADD, PtrVT, PtrOff, ConstFour);
Evan Cheng466685d2006-10-09 20:57:25 +00001343 SDOperand Load = DAG.getLoad(PtrVT, Store, PtrOff, NULL, 0);
Chris Lattner9a2a4972006-05-17 06:01:33 +00001344 MemOpChains.push_back(Load.getValue(1));
1345 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Chris Lattnerabde4602006-05-16 22:56:08 +00001346 }
1347 } else {
Chris Lattnerc8b682c2006-05-17 00:15:40 +00001348 // If we have any FPRs remaining, we may also have GPRs remaining.
1349 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
1350 // GPRs.
Chris Lattner9a2a4972006-05-17 06:01:33 +00001351 if (GPR_idx != NumGPRs)
1352 ++GPR_idx;
Chris Lattnerc91a4752006-06-26 22:48:35 +00001353 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 && !isPPC64)
Chris Lattner9a2a4972006-05-17 06:01:33 +00001354 ++GPR_idx;
Chris Lattnerabde4602006-05-16 22:56:08 +00001355 }
Chris Lattnerc8b682c2006-05-17 00:15:40 +00001356 } else {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001357 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
Chris Lattnerabde4602006-05-16 22:56:08 +00001358 }
Chris Lattnerc91a4752006-06-26 22:48:35 +00001359 if (isPPC64)
1360 ArgOffset += 8;
1361 else
1362 ArgOffset += Arg.getValueType() == MVT::f32 ? 4 : 8;
Chris Lattnerc8b682c2006-05-17 00:15:40 +00001363 break;
1364 case MVT::v4f32:
1365 case MVT::v4i32:
1366 case MVT::v8i16:
1367 case MVT::v16i8:
1368 assert(!isVarArg && "Don't support passing vectors to varargs yet!");
Chris Lattner9a2a4972006-05-17 06:01:33 +00001369 assert(VR_idx != NumVRs &&
Chris Lattnerc8b682c2006-05-17 00:15:40 +00001370 "Don't support passing more than 12 vector args yet!");
Chris Lattner9a2a4972006-05-17 06:01:33 +00001371 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Arg));
Chris Lattnerc8b682c2006-05-17 00:15:40 +00001372 break;
Chris Lattnerabde4602006-05-16 22:56:08 +00001373 }
Chris Lattnerabde4602006-05-16 22:56:08 +00001374 }
Chris Lattner9a2a4972006-05-17 06:01:33 +00001375 if (!MemOpChains.empty())
Chris Lattnere2199452006-08-11 17:38:39 +00001376 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1377 &MemOpChains[0], MemOpChains.size());
Chris Lattnerabde4602006-05-16 22:56:08 +00001378
Chris Lattner9a2a4972006-05-17 06:01:33 +00001379 // Build a sequence of copy-to-reg nodes chained together with token chain
1380 // and flag operands which copy the outgoing args into the appropriate regs.
1381 SDOperand InFlag;
1382 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1383 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1384 InFlag);
1385 InFlag = Chain.getValue(1);
1386 }
Chris Lattnerabde4602006-05-16 22:56:08 +00001387
Chris Lattnerc703a8f2006-05-17 19:00:46 +00001388 std::vector<MVT::ValueType> NodeTys;
Chris Lattner4a45abf2006-06-10 01:14:28 +00001389 NodeTys.push_back(MVT::Other); // Returns a chain
1390 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1391
Chris Lattner79e490a2006-08-11 17:18:05 +00001392 SmallVector<SDOperand, 8> Ops;
Chris Lattner4a45abf2006-06-10 01:14:28 +00001393 unsigned CallOpc = PPCISD::CALL;
Chris Lattnerc703a8f2006-05-17 19:00:46 +00001394
1395 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1396 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1397 // node so that legalize doesn't hack it.
Chris Lattnerabde4602006-05-16 22:56:08 +00001398 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Chris Lattner9a2a4972006-05-17 06:01:33 +00001399 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), Callee.getValueType());
Chris Lattnerc703a8f2006-05-17 19:00:46 +00001400 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1401 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), Callee.getValueType());
1402 else if (SDNode *Dest = isBLACompatibleAddress(Callee, DAG))
1403 // If this is an absolute destination address, use the munged value.
1404 Callee = SDOperand(Dest, 0);
1405 else {
1406 // Otherwise, this is an indirect call. We have to use a MTCTR/BCTRL pair
1407 // to do the call, we can't use PPCISD::CALL.
Chris Lattner79e490a2006-08-11 17:18:05 +00001408 SDOperand MTCTROps[] = {Chain, Callee, InFlag};
1409 Chain = DAG.getNode(PPCISD::MTCTR, NodeTys, MTCTROps, 2+(InFlag.Val!=0));
Chris Lattnerc703a8f2006-05-17 19:00:46 +00001410 InFlag = Chain.getValue(1);
1411
1412 // Copy the callee address into R12 on darwin.
1413 Chain = DAG.getCopyToReg(Chain, PPC::R12, Callee, InFlag);
1414 InFlag = Chain.getValue(1);
1415
1416 NodeTys.clear();
1417 NodeTys.push_back(MVT::Other);
1418 NodeTys.push_back(MVT::Flag);
Chris Lattnerc703a8f2006-05-17 19:00:46 +00001419 Ops.push_back(Chain);
Chris Lattner4a45abf2006-06-10 01:14:28 +00001420 CallOpc = PPCISD::BCTRL;
Chris Lattnerc703a8f2006-05-17 19:00:46 +00001421 Callee.Val = 0;
1422 }
Chris Lattner9a2a4972006-05-17 06:01:33 +00001423
Chris Lattner4a45abf2006-06-10 01:14:28 +00001424 // If this is a direct call, pass the chain and the callee.
Chris Lattnerc703a8f2006-05-17 19:00:46 +00001425 if (Callee.Val) {
Chris Lattnerc703a8f2006-05-17 19:00:46 +00001426 Ops.push_back(Chain);
1427 Ops.push_back(Callee);
Chris Lattnerc703a8f2006-05-17 19:00:46 +00001428 }
Chris Lattnerabde4602006-05-16 22:56:08 +00001429
Chris Lattner4a45abf2006-06-10 01:14:28 +00001430 // Add argument registers to the end of the list so that they are known live
1431 // into the call.
1432 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1433 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1434 RegsToPass[i].second.getValueType()));
1435
1436 if (InFlag.Val)
1437 Ops.push_back(InFlag);
Chris Lattner79e490a2006-08-11 17:18:05 +00001438 Chain = DAG.getNode(CallOpc, NodeTys, &Ops[0], Ops.size());
Chris Lattner4a45abf2006-06-10 01:14:28 +00001439 InFlag = Chain.getValue(1);
1440
Chris Lattner79e490a2006-08-11 17:18:05 +00001441 SDOperand ResultVals[3];
1442 unsigned NumResults = 0;
Chris Lattner9a2a4972006-05-17 06:01:33 +00001443 NodeTys.clear();
1444
1445 // If the call has results, copy the values out of the ret val registers.
1446 switch (Op.Val->getValueType(0)) {
1447 default: assert(0 && "Unexpected ret value!");
1448 case MVT::Other: break;
1449 case MVT::i32:
1450 if (Op.Val->getValueType(1) == MVT::i32) {
1451 Chain = DAG.getCopyFromReg(Chain, PPC::R4, MVT::i32, InFlag).getValue(1);
Chris Lattner79e490a2006-08-11 17:18:05 +00001452 ResultVals[0] = Chain.getValue(0);
Chris Lattner9a2a4972006-05-17 06:01:33 +00001453 Chain = DAG.getCopyFromReg(Chain, PPC::R3, MVT::i32,
1454 Chain.getValue(2)).getValue(1);
Chris Lattner79e490a2006-08-11 17:18:05 +00001455 ResultVals[1] = Chain.getValue(0);
1456 NumResults = 2;
Chris Lattner9a2a4972006-05-17 06:01:33 +00001457 NodeTys.push_back(MVT::i32);
1458 } else {
1459 Chain = DAG.getCopyFromReg(Chain, PPC::R3, MVT::i32, InFlag).getValue(1);
Chris Lattner79e490a2006-08-11 17:18:05 +00001460 ResultVals[0] = Chain.getValue(0);
1461 NumResults = 1;
Chris Lattner9a2a4972006-05-17 06:01:33 +00001462 }
1463 NodeTys.push_back(MVT::i32);
1464 break;
Chris Lattnerc91a4752006-06-26 22:48:35 +00001465 case MVT::i64:
1466 Chain = DAG.getCopyFromReg(Chain, PPC::X3, MVT::i64, InFlag).getValue(1);
Chris Lattner79e490a2006-08-11 17:18:05 +00001467 ResultVals[0] = Chain.getValue(0);
1468 NumResults = 1;
Chris Lattnerc91a4752006-06-26 22:48:35 +00001469 NodeTys.push_back(MVT::i64);
1470 break;
Chris Lattner9a2a4972006-05-17 06:01:33 +00001471 case MVT::f32:
1472 case MVT::f64:
1473 Chain = DAG.getCopyFromReg(Chain, PPC::F1, Op.Val->getValueType(0),
1474 InFlag).getValue(1);
Chris Lattner79e490a2006-08-11 17:18:05 +00001475 ResultVals[0] = Chain.getValue(0);
1476 NumResults = 1;
Chris Lattner9a2a4972006-05-17 06:01:33 +00001477 NodeTys.push_back(Op.Val->getValueType(0));
1478 break;
1479 case MVT::v4f32:
1480 case MVT::v4i32:
1481 case MVT::v8i16:
1482 case MVT::v16i8:
1483 Chain = DAG.getCopyFromReg(Chain, PPC::V2, Op.Val->getValueType(0),
1484 InFlag).getValue(1);
Chris Lattner79e490a2006-08-11 17:18:05 +00001485 ResultVals[0] = Chain.getValue(0);
1486 NumResults = 1;
Chris Lattner9a2a4972006-05-17 06:01:33 +00001487 NodeTys.push_back(Op.Val->getValueType(0));
1488 break;
1489 }
1490
Chris Lattnerabde4602006-05-16 22:56:08 +00001491 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
Chris Lattnerc91a4752006-06-26 22:48:35 +00001492 DAG.getConstant(NumBytes, PtrVT));
Chris Lattner9a2a4972006-05-17 06:01:33 +00001493 NodeTys.push_back(MVT::Other);
Chris Lattnerabde4602006-05-16 22:56:08 +00001494
Chris Lattnerc703a8f2006-05-17 19:00:46 +00001495 // If the function returns void, just return the chain.
Chris Lattnerf6e190f2006-08-12 07:20:05 +00001496 if (NumResults == 0)
Chris Lattnerc703a8f2006-05-17 19:00:46 +00001497 return Chain;
1498
1499 // Otherwise, merge everything together with a MERGE_VALUES node.
Chris Lattner79e490a2006-08-11 17:18:05 +00001500 ResultVals[NumResults++] = Chain;
1501 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys,
1502 ResultVals, NumResults);
Chris Lattnerabde4602006-05-16 22:56:08 +00001503 return Res.getValue(Op.ResNo);
1504}
1505
Chris Lattner1a635d62006-04-14 06:01:58 +00001506static SDOperand LowerRET(SDOperand Op, SelectionDAG &DAG) {
1507 SDOperand Copy;
1508 switch(Op.getNumOperands()) {
1509 default:
1510 assert(0 && "Do not know how to return this many arguments!");
1511 abort();
1512 case 1:
1513 return SDOperand(); // ret void is legal
Evan Cheng6848be12006-05-26 23:10:12 +00001514 case 3: {
Chris Lattner1a635d62006-04-14 06:01:58 +00001515 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
1516 unsigned ArgReg;
Chris Lattneref957102006-06-21 00:34:03 +00001517 if (ArgVT == MVT::i32) {
Chris Lattner1a635d62006-04-14 06:01:58 +00001518 ArgReg = PPC::R3;
Chris Lattneref957102006-06-21 00:34:03 +00001519 } else if (ArgVT == MVT::i64) {
1520 ArgReg = PPC::X3;
Chris Lattner325f0a12006-08-11 16:47:32 +00001521 } else if (MVT::isVector(ArgVT)) {
Chris Lattneref957102006-06-21 00:34:03 +00001522 ArgReg = PPC::V2;
Chris Lattner325f0a12006-08-11 16:47:32 +00001523 } else {
1524 assert(MVT::isFloatingPoint(ArgVT));
1525 ArgReg = PPC::F1;
Chris Lattner1a635d62006-04-14 06:01:58 +00001526 }
1527
1528 Copy = DAG.getCopyToReg(Op.getOperand(0), ArgReg, Op.getOperand(1),
1529 SDOperand());
1530
1531 // If we haven't noted the R3/F1 are live out, do so now.
1532 if (DAG.getMachineFunction().liveout_empty())
1533 DAG.getMachineFunction().addLiveOut(ArgReg);
1534 break;
1535 }
Evan Cheng6848be12006-05-26 23:10:12 +00001536 case 5:
1537 Copy = DAG.getCopyToReg(Op.getOperand(0), PPC::R3, Op.getOperand(3),
Chris Lattner1a635d62006-04-14 06:01:58 +00001538 SDOperand());
1539 Copy = DAG.getCopyToReg(Copy, PPC::R4, Op.getOperand(1),Copy.getValue(1));
1540 // If we haven't noted the R3+R4 are live out, do so now.
1541 if (DAG.getMachineFunction().liveout_empty()) {
1542 DAG.getMachineFunction().addLiveOut(PPC::R3);
1543 DAG.getMachineFunction().addLiveOut(PPC::R4);
1544 }
1545 break;
1546 }
1547 return DAG.getNode(PPCISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
1548}
1549
1550/// LowerSELECT_CC - Lower floating point select_cc's into fsel instruction when
1551/// possible.
1552static SDOperand LowerSELECT_CC(SDOperand Op, SelectionDAG &DAG) {
1553 // Not FP? Not a fsel.
1554 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
1555 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
1556 return SDOperand();
1557
1558 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
1559
1560 // Cannot handle SETEQ/SETNE.
1561 if (CC == ISD::SETEQ || CC == ISD::SETNE) return SDOperand();
1562
1563 MVT::ValueType ResVT = Op.getValueType();
1564 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
1565 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
1566 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
1567
1568 // If the RHS of the comparison is a 0.0, we don't need to do the
1569 // subtraction at all.
1570 if (isFloatingPointZero(RHS))
1571 switch (CC) {
1572 default: break; // SETUO etc aren't handled by fsel.
1573 case ISD::SETULT:
Chris Lattner57340122006-05-24 00:06:44 +00001574 case ISD::SETOLT:
Chris Lattner1a635d62006-04-14 06:01:58 +00001575 case ISD::SETLT:
1576 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
1577 case ISD::SETUGE:
Chris Lattner57340122006-05-24 00:06:44 +00001578 case ISD::SETOGE:
Chris Lattner1a635d62006-04-14 06:01:58 +00001579 case ISD::SETGE:
1580 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
1581 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
1582 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
1583 case ISD::SETUGT:
Chris Lattner57340122006-05-24 00:06:44 +00001584 case ISD::SETOGT:
Chris Lattner1a635d62006-04-14 06:01:58 +00001585 case ISD::SETGT:
1586 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
1587 case ISD::SETULE:
Chris Lattner57340122006-05-24 00:06:44 +00001588 case ISD::SETOLE:
Chris Lattner1a635d62006-04-14 06:01:58 +00001589 case ISD::SETLE:
1590 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
1591 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
1592 return DAG.getNode(PPCISD::FSEL, ResVT,
1593 DAG.getNode(ISD::FNEG, MVT::f64, LHS), TV, FV);
1594 }
1595
1596 SDOperand Cmp;
1597 switch (CC) {
1598 default: break; // SETUO etc aren't handled by fsel.
1599 case ISD::SETULT:
Chris Lattner57340122006-05-24 00:06:44 +00001600 case ISD::SETOLT:
Chris Lattner1a635d62006-04-14 06:01:58 +00001601 case ISD::SETLT:
1602 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
1603 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
1604 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
1605 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
1606 case ISD::SETUGE:
Chris Lattner57340122006-05-24 00:06:44 +00001607 case ISD::SETOGE:
Chris Lattner1a635d62006-04-14 06:01:58 +00001608 case ISD::SETGE:
1609 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
1610 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
1611 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
1612 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
1613 case ISD::SETUGT:
Chris Lattner57340122006-05-24 00:06:44 +00001614 case ISD::SETOGT:
Chris Lattner1a635d62006-04-14 06:01:58 +00001615 case ISD::SETGT:
1616 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
1617 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
1618 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
1619 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
1620 case ISD::SETULE:
Chris Lattner57340122006-05-24 00:06:44 +00001621 case ISD::SETOLE:
Chris Lattner1a635d62006-04-14 06:01:58 +00001622 case ISD::SETLE:
1623 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
1624 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
1625 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
1626 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
1627 }
1628 return SDOperand();
1629}
1630
1631static SDOperand LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
1632 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
1633 SDOperand Src = Op.getOperand(0);
1634 if (Src.getValueType() == MVT::f32)
1635 Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src);
1636
1637 SDOperand Tmp;
1638 switch (Op.getValueType()) {
1639 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
1640 case MVT::i32:
1641 Tmp = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
1642 break;
1643 case MVT::i64:
1644 Tmp = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
1645 break;
1646 }
1647
1648 // Convert the FP value to an int value through memory.
1649 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Tmp);
1650 if (Op.getValueType() == MVT::i32)
1651 Bits = DAG.getNode(ISD::TRUNCATE, MVT::i32, Bits);
1652 return Bits;
1653}
1654
1655static SDOperand LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
1656 if (Op.getOperand(0).getValueType() == MVT::i64) {
1657 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, Op.getOperand(0));
1658 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Bits);
1659 if (Op.getValueType() == MVT::f32)
1660 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
1661 return FP;
1662 }
1663
1664 assert(Op.getOperand(0).getValueType() == MVT::i32 &&
1665 "Unhandled SINT_TO_FP type in custom expander!");
1666 // Since we only generate this in 64-bit mode, we can take advantage of
1667 // 64-bit registers. In particular, sign extend the input value into the
1668 // 64-bit register with extsw, store the WHOLE 64-bit value into the stack
1669 // then lfd it and fcfid it.
1670 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
1671 int FrameIdx = FrameInfo->CreateStackObject(8, 8);
Chris Lattner0d72a202006-07-28 16:45:47 +00001672 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1673 SDOperand FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Chris Lattner1a635d62006-04-14 06:01:58 +00001674
1675 SDOperand Ext64 = DAG.getNode(PPCISD::EXTSW_32, MVT::i32,
1676 Op.getOperand(0));
1677
1678 // STD the extended value into the stack slot.
1679 SDOperand Store = DAG.getNode(PPCISD::STD_32, MVT::Other,
1680 DAG.getEntryNode(), Ext64, FIdx,
1681 DAG.getSrcValue(NULL));
1682 // Load the value as a double.
Evan Cheng466685d2006-10-09 20:57:25 +00001683 SDOperand Ld = DAG.getLoad(MVT::f64, Store, FIdx, NULL, 0);
Chris Lattner1a635d62006-04-14 06:01:58 +00001684
1685 // FCFID it and return it.
1686 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Ld);
1687 if (Op.getValueType() == MVT::f32)
1688 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
1689 return FP;
1690}
1691
Chris Lattner3fe6c1d2006-09-20 03:47:40 +00001692static SDOperand LowerSHL_PARTS(SDOperand Op, SelectionDAG &DAG) {
1693 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
Chris Lattner1a635d62006-04-14 06:01:58 +00001694 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
Chris Lattner1a635d62006-04-14 06:01:58 +00001695
Chris Lattner3fe6c1d2006-09-20 03:47:40 +00001696 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner1a635d62006-04-14 06:01:58 +00001697 // depend on the PPC behavior for oversized shift amounts.
Chris Lattner3fe6c1d2006-09-20 03:47:40 +00001698 SDOperand Lo = Op.getOperand(0);
1699 SDOperand Hi = Op.getOperand(1);
1700 SDOperand Amt = Op.getOperand(2);
Chris Lattner1a635d62006-04-14 06:01:58 +00001701
1702 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
1703 DAG.getConstant(32, MVT::i32), Amt);
1704 SDOperand Tmp2 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Amt);
1705 SDOperand Tmp3 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Tmp1);
1706 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
1707 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
1708 DAG.getConstant(-32U, MVT::i32));
1709 SDOperand Tmp6 = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Tmp5);
1710 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
1711 SDOperand OutLo = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Amt);
Chris Lattner3fe6c1d2006-09-20 03:47:40 +00001712 SDOperand OutOps[] = { OutLo, OutHi };
1713 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(MVT::i32, MVT::i32),
1714 OutOps, 2);
Chris Lattner1a635d62006-04-14 06:01:58 +00001715}
1716
Chris Lattner3fe6c1d2006-09-20 03:47:40 +00001717static SDOperand LowerSRL_PARTS(SDOperand Op, SelectionDAG &DAG) {
1718 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
1719 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRL!");
Chris Lattner1a635d62006-04-14 06:01:58 +00001720
1721 // Otherwise, expand into a bunch of logical ops. Note that these ops
1722 // depend on the PPC behavior for oversized shift amounts.
Chris Lattner3fe6c1d2006-09-20 03:47:40 +00001723 SDOperand Lo = Op.getOperand(0);
1724 SDOperand Hi = Op.getOperand(1);
1725 SDOperand Amt = Op.getOperand(2);
Chris Lattner1a635d62006-04-14 06:01:58 +00001726
1727 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
1728 DAG.getConstant(32, MVT::i32), Amt);
1729 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
1730 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
1731 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
1732 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
1733 DAG.getConstant(-32U, MVT::i32));
1734 SDOperand Tmp6 = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Tmp5);
1735 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
1736 SDOperand OutHi = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Amt);
Chris Lattner3fe6c1d2006-09-20 03:47:40 +00001737 SDOperand OutOps[] = { OutLo, OutHi };
1738 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(MVT::i32, MVT::i32),
1739 OutOps, 2);
Chris Lattner1a635d62006-04-14 06:01:58 +00001740}
1741
Chris Lattner3fe6c1d2006-09-20 03:47:40 +00001742static SDOperand LowerSRA_PARTS(SDOperand Op, SelectionDAG &DAG) {
1743 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
Chris Lattner1a635d62006-04-14 06:01:58 +00001744 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
Chris Lattner1a635d62006-04-14 06:01:58 +00001745
1746 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
Chris Lattner3fe6c1d2006-09-20 03:47:40 +00001747 SDOperand Lo = Op.getOperand(0);
1748 SDOperand Hi = Op.getOperand(1);
1749 SDOperand Amt = Op.getOperand(2);
Chris Lattner1a635d62006-04-14 06:01:58 +00001750
1751 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
1752 DAG.getConstant(32, MVT::i32), Amt);
1753 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
1754 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
1755 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
1756 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
1757 DAG.getConstant(-32U, MVT::i32));
1758 SDOperand Tmp6 = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Tmp5);
1759 SDOperand OutHi = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Amt);
1760 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
1761 Tmp4, Tmp6, ISD::SETLE);
Chris Lattner3fe6c1d2006-09-20 03:47:40 +00001762 SDOperand OutOps[] = { OutLo, OutHi };
1763 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(MVT::i32, MVT::i32),
1764 OutOps, 2);
Chris Lattner1a635d62006-04-14 06:01:58 +00001765}
1766
1767//===----------------------------------------------------------------------===//
1768// Vector related lowering.
1769//
1770
Chris Lattnerac225ca2006-04-12 19:07:14 +00001771// If this is a vector of constants or undefs, get the bits. A bit in
1772// UndefBits is set if the corresponding element of the vector is an
1773// ISD::UNDEF value. For undefs, the corresponding VectorBits values are
1774// zero. Return true if this is not an array of constants, false if it is.
1775//
Chris Lattnerac225ca2006-04-12 19:07:14 +00001776static bool GetConstantBuildVectorBits(SDNode *BV, uint64_t VectorBits[2],
1777 uint64_t UndefBits[2]) {
1778 // Start with zero'd results.
1779 VectorBits[0] = VectorBits[1] = UndefBits[0] = UndefBits[1] = 0;
1780
1781 unsigned EltBitSize = MVT::getSizeInBits(BV->getOperand(0).getValueType());
1782 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
1783 SDOperand OpVal = BV->getOperand(i);
1784
1785 unsigned PartNo = i >= e/2; // In the upper 128 bits?
Chris Lattnerb17f1672006-04-16 01:01:29 +00001786 unsigned SlotNo = e/2 - (i & (e/2-1))-1; // Which subpiece of the uint64_t.
Chris Lattnerac225ca2006-04-12 19:07:14 +00001787
1788 uint64_t EltBits = 0;
1789 if (OpVal.getOpcode() == ISD::UNDEF) {
1790 uint64_t EltUndefBits = ~0U >> (32-EltBitSize);
1791 UndefBits[PartNo] |= EltUndefBits << (SlotNo*EltBitSize);
1792 continue;
1793 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
1794 EltBits = CN->getValue() & (~0U >> (32-EltBitSize));
1795 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) {
1796 assert(CN->getValueType(0) == MVT::f32 &&
1797 "Only one legal FP vector type!");
1798 EltBits = FloatToBits(CN->getValue());
1799 } else {
1800 // Nonconstant element.
1801 return true;
1802 }
1803
1804 VectorBits[PartNo] |= EltBits << (SlotNo*EltBitSize);
1805 }
1806
1807 //printf("%llx %llx %llx %llx\n",
1808 // VectorBits[0], VectorBits[1], UndefBits[0], UndefBits[1]);
1809 return false;
1810}
Chris Lattneref819f82006-03-20 06:33:01 +00001811
Chris Lattnerb17f1672006-04-16 01:01:29 +00001812// If this is a splat (repetition) of a value across the whole vector, return
1813// the smallest size that splats it. For example, "0x01010101010101..." is a
1814// splat of 0x01, 0x0101, and 0x01010101. We return SplatBits = 0x01 and
1815// SplatSize = 1 byte.
1816static bool isConstantSplat(const uint64_t Bits128[2],
1817 const uint64_t Undef128[2],
1818 unsigned &SplatBits, unsigned &SplatUndef,
1819 unsigned &SplatSize) {
1820
1821 // Don't let undefs prevent splats from matching. See if the top 64-bits are
1822 // the same as the lower 64-bits, ignoring undefs.
1823 if ((Bits128[0] & ~Undef128[1]) != (Bits128[1] & ~Undef128[0]))
1824 return false; // Can't be a splat if two pieces don't match.
1825
1826 uint64_t Bits64 = Bits128[0] | Bits128[1];
1827 uint64_t Undef64 = Undef128[0] & Undef128[1];
1828
1829 // Check that the top 32-bits are the same as the lower 32-bits, ignoring
1830 // undefs.
1831 if ((Bits64 & (~Undef64 >> 32)) != ((Bits64 >> 32) & ~Undef64))
1832 return false; // Can't be a splat if two pieces don't match.
1833
1834 uint32_t Bits32 = uint32_t(Bits64) | uint32_t(Bits64 >> 32);
1835 uint32_t Undef32 = uint32_t(Undef64) & uint32_t(Undef64 >> 32);
1836
1837 // If the top 16-bits are different than the lower 16-bits, ignoring
1838 // undefs, we have an i32 splat.
1839 if ((Bits32 & (~Undef32 >> 16)) != ((Bits32 >> 16) & ~Undef32)) {
1840 SplatBits = Bits32;
1841 SplatUndef = Undef32;
1842 SplatSize = 4;
1843 return true;
1844 }
1845
1846 uint16_t Bits16 = uint16_t(Bits32) | uint16_t(Bits32 >> 16);
1847 uint16_t Undef16 = uint16_t(Undef32) & uint16_t(Undef32 >> 16);
1848
1849 // If the top 8-bits are different than the lower 8-bits, ignoring
1850 // undefs, we have an i16 splat.
1851 if ((Bits16 & (uint16_t(~Undef16) >> 8)) != ((Bits16 >> 8) & ~Undef16)) {
1852 SplatBits = Bits16;
1853 SplatUndef = Undef16;
1854 SplatSize = 2;
1855 return true;
1856 }
1857
1858 // Otherwise, we have an 8-bit splat.
1859 SplatBits = uint8_t(Bits16) | uint8_t(Bits16 >> 8);
1860 SplatUndef = uint8_t(Undef16) & uint8_t(Undef16 >> 8);
1861 SplatSize = 1;
1862 return true;
1863}
1864
Chris Lattner4a998b92006-04-17 06:00:21 +00001865/// BuildSplatI - Build a canonical splati of Val with an element size of
1866/// SplatSize. Cast the result to VT.
1867static SDOperand BuildSplatI(int Val, unsigned SplatSize, MVT::ValueType VT,
1868 SelectionDAG &DAG) {
1869 assert(Val >= -16 && Val <= 15 && "vsplti is out of range!");
Chris Lattner6876e662006-04-17 06:58:41 +00001870
1871 // Force vspltis[hw] -1 to vspltisb -1.
1872 if (Val == -1) SplatSize = 1;
1873
Chris Lattner4a998b92006-04-17 06:00:21 +00001874 static const MVT::ValueType VTys[] = { // canonical VT to use for each size.
1875 MVT::v16i8, MVT::v8i16, MVT::Other, MVT::v4i32
1876 };
1877 MVT::ValueType CanonicalVT = VTys[SplatSize-1];
1878
1879 // Build a canonical splat for this value.
1880 SDOperand Elt = DAG.getConstant(Val, MVT::getVectorBaseType(CanonicalVT));
Chris Lattnere2199452006-08-11 17:38:39 +00001881 SmallVector<SDOperand, 8> Ops;
1882 Ops.assign(MVT::getVectorNumElements(CanonicalVT), Elt);
1883 SDOperand Res = DAG.getNode(ISD::BUILD_VECTOR, CanonicalVT,
1884 &Ops[0], Ops.size());
Chris Lattner4a998b92006-04-17 06:00:21 +00001885 return DAG.getNode(ISD::BIT_CONVERT, VT, Res);
1886}
1887
Chris Lattnere7c768e2006-04-18 03:24:30 +00001888/// BuildIntrinsicOp - Return a binary operator intrinsic node with the
Chris Lattner6876e662006-04-17 06:58:41 +00001889/// specified intrinsic ID.
Chris Lattnere7c768e2006-04-18 03:24:30 +00001890static SDOperand BuildIntrinsicOp(unsigned IID, SDOperand LHS, SDOperand RHS,
1891 SelectionDAG &DAG,
1892 MVT::ValueType DestVT = MVT::Other) {
1893 if (DestVT == MVT::Other) DestVT = LHS.getValueType();
1894 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DestVT,
Chris Lattner6876e662006-04-17 06:58:41 +00001895 DAG.getConstant(IID, MVT::i32), LHS, RHS);
1896}
1897
Chris Lattnere7c768e2006-04-18 03:24:30 +00001898/// BuildIntrinsicOp - Return a ternary operator intrinsic node with the
1899/// specified intrinsic ID.
1900static SDOperand BuildIntrinsicOp(unsigned IID, SDOperand Op0, SDOperand Op1,
1901 SDOperand Op2, SelectionDAG &DAG,
1902 MVT::ValueType DestVT = MVT::Other) {
1903 if (DestVT == MVT::Other) DestVT = Op0.getValueType();
1904 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DestVT,
1905 DAG.getConstant(IID, MVT::i32), Op0, Op1, Op2);
1906}
1907
1908
Chris Lattnerbdd558c2006-04-17 17:55:10 +00001909/// BuildVSLDOI - Return a VECTOR_SHUFFLE that is a vsldoi of the specified
1910/// amount. The result has the specified value type.
1911static SDOperand BuildVSLDOI(SDOperand LHS, SDOperand RHS, unsigned Amt,
1912 MVT::ValueType VT, SelectionDAG &DAG) {
1913 // Force LHS/RHS to be the right type.
1914 LHS = DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, LHS);
1915 RHS = DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, RHS);
1916
Chris Lattnere2199452006-08-11 17:38:39 +00001917 SDOperand Ops[16];
Chris Lattnerbdd558c2006-04-17 17:55:10 +00001918 for (unsigned i = 0; i != 16; ++i)
Chris Lattnere2199452006-08-11 17:38:39 +00001919 Ops[i] = DAG.getConstant(i+Amt, MVT::i32);
Chris Lattnerbdd558c2006-04-17 17:55:10 +00001920 SDOperand T = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v16i8, LHS, RHS,
Chris Lattnere2199452006-08-11 17:38:39 +00001921 DAG.getNode(ISD::BUILD_VECTOR, MVT::v16i8, Ops,16));
Chris Lattnerbdd558c2006-04-17 17:55:10 +00001922 return DAG.getNode(ISD::BIT_CONVERT, VT, T);
1923}
1924
Chris Lattnerf1b47082006-04-14 05:19:18 +00001925// If this is a case we can't handle, return null and let the default
1926// expansion code take care of it. If we CAN select this case, and if it
1927// selects to a single instruction, return Op. Otherwise, if we can codegen
1928// this case more efficiently than a constant pool load, lower it to the
1929// sequence of ops that should be used.
1930static SDOperand LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
1931 // If this is a vector of constants or undefs, get the bits. A bit in
1932 // UndefBits is set if the corresponding element of the vector is an
1933 // ISD::UNDEF value. For undefs, the corresponding VectorBits values are
1934 // zero.
1935 uint64_t VectorBits[2];
1936 uint64_t UndefBits[2];
1937 if (GetConstantBuildVectorBits(Op.Val, VectorBits, UndefBits))
1938 return SDOperand(); // Not a constant vector.
1939
Chris Lattnerb17f1672006-04-16 01:01:29 +00001940 // If this is a splat (repetition) of a value across the whole vector, return
1941 // the smallest size that splats it. For example, "0x01010101010101..." is a
1942 // splat of 0x01, 0x0101, and 0x01010101. We return SplatBits = 0x01 and
1943 // SplatSize = 1 byte.
1944 unsigned SplatBits, SplatUndef, SplatSize;
1945 if (isConstantSplat(VectorBits, UndefBits, SplatBits, SplatUndef, SplatSize)){
1946 bool HasAnyUndefs = (UndefBits[0] | UndefBits[1]) != 0;
1947
1948 // First, handle single instruction cases.
1949
1950 // All zeros?
1951 if (SplatBits == 0) {
1952 // Canonicalize all zero vectors to be v4i32.
1953 if (Op.getValueType() != MVT::v4i32 || HasAnyUndefs) {
1954 SDOperand Z = DAG.getConstant(0, MVT::i32);
1955 Z = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Z, Z, Z, Z);
1956 Op = DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Z);
1957 }
1958 return Op;
Chris Lattnerf1b47082006-04-14 05:19:18 +00001959 }
Chris Lattnerb17f1672006-04-16 01:01:29 +00001960
1961 // If the sign extended value is in the range [-16,15], use VSPLTI[bhw].
1962 int32_t SextVal= int32_t(SplatBits << (32-8*SplatSize)) >> (32-8*SplatSize);
Chris Lattner4a998b92006-04-17 06:00:21 +00001963 if (SextVal >= -16 && SextVal <= 15)
1964 return BuildSplatI(SextVal, SplatSize, Op.getValueType(), DAG);
Chris Lattnerb17f1672006-04-16 01:01:29 +00001965
Chris Lattnerdbce85d2006-04-17 18:09:22 +00001966
1967 // Two instruction sequences.
1968
Chris Lattner4a998b92006-04-17 06:00:21 +00001969 // If this value is in the range [-32,30] and is even, use:
1970 // tmp = VSPLTI[bhw], result = add tmp, tmp
1971 if (SextVal >= -32 && SextVal <= 30 && (SextVal & 1) == 0) {
1972 Op = BuildSplatI(SextVal >> 1, SplatSize, Op.getValueType(), DAG);
1973 return DAG.getNode(ISD::ADD, Op.getValueType(), Op, Op);
1974 }
Chris Lattner6876e662006-04-17 06:58:41 +00001975
1976 // If this is 0x8000_0000 x 4, turn into vspltisw + vslw. If it is
1977 // 0x7FFF_FFFF x 4, turn it into not(0x8000_0000). This is important
1978 // for fneg/fabs.
1979 if (SplatSize == 4 && SplatBits == (0x7FFFFFFF&~SplatUndef)) {
1980 // Make -1 and vspltisw -1:
1981 SDOperand OnesV = BuildSplatI(-1, 4, MVT::v4i32, DAG);
1982
1983 // Make the VSLW intrinsic, computing 0x8000_0000.
Chris Lattnere7c768e2006-04-18 03:24:30 +00001984 SDOperand Res = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, OnesV,
1985 OnesV, DAG);
Chris Lattner6876e662006-04-17 06:58:41 +00001986
1987 // xor by OnesV to invert it.
1988 Res = DAG.getNode(ISD::XOR, MVT::v4i32, Res, OnesV);
1989 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Res);
1990 }
1991
1992 // Check to see if this is a wide variety of vsplti*, binop self cases.
1993 unsigned SplatBitSize = SplatSize*8;
1994 static const char SplatCsts[] = {
1995 -1, 1, -2, 2, -3, 3, -4, 4, -5, 5, -6, 6, -7, 7,
Chris Lattnerdbce85d2006-04-17 18:09:22 +00001996 -8, 8, -9, 9, -10, 10, -11, 11, -12, 12, -13, 13, 14, -14, 15, -15, -16
Chris Lattner6876e662006-04-17 06:58:41 +00001997 };
1998 for (unsigned idx = 0; idx < sizeof(SplatCsts)/sizeof(SplatCsts[0]); ++idx){
1999 // Indirect through the SplatCsts array so that we favor 'vsplti -1' for
2000 // cases which are ambiguous (e.g. formation of 0x8000_0000). 'vsplti -1'
2001 int i = SplatCsts[idx];
2002
2003 // Figure out what shift amount will be used by altivec if shifted by i in
2004 // this splat size.
2005 unsigned TypeShiftAmt = i & (SplatBitSize-1);
2006
2007 // vsplti + shl self.
2008 if (SextVal == (i << (int)TypeShiftAmt)) {
2009 Op = BuildSplatI(i, SplatSize, Op.getValueType(), DAG);
2010 static const unsigned IIDs[] = { // Intrinsic to use for each size.
2011 Intrinsic::ppc_altivec_vslb, Intrinsic::ppc_altivec_vslh, 0,
2012 Intrinsic::ppc_altivec_vslw
2013 };
Chris Lattnere7c768e2006-04-18 03:24:30 +00002014 return BuildIntrinsicOp(IIDs[SplatSize-1], Op, Op, DAG);
Chris Lattner6876e662006-04-17 06:58:41 +00002015 }
2016
2017 // vsplti + srl self.
2018 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
2019 Op = BuildSplatI(i, SplatSize, Op.getValueType(), DAG);
2020 static const unsigned IIDs[] = { // Intrinsic to use for each size.
2021 Intrinsic::ppc_altivec_vsrb, Intrinsic::ppc_altivec_vsrh, 0,
2022 Intrinsic::ppc_altivec_vsrw
2023 };
Chris Lattnere7c768e2006-04-18 03:24:30 +00002024 return BuildIntrinsicOp(IIDs[SplatSize-1], Op, Op, DAG);
Chris Lattner6876e662006-04-17 06:58:41 +00002025 }
2026
2027 // vsplti + sra self.
2028 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
2029 Op = BuildSplatI(i, SplatSize, Op.getValueType(), DAG);
2030 static const unsigned IIDs[] = { // Intrinsic to use for each size.
2031 Intrinsic::ppc_altivec_vsrab, Intrinsic::ppc_altivec_vsrah, 0,
2032 Intrinsic::ppc_altivec_vsraw
2033 };
Chris Lattnere7c768e2006-04-18 03:24:30 +00002034 return BuildIntrinsicOp(IIDs[SplatSize-1], Op, Op, DAG);
Chris Lattner6876e662006-04-17 06:58:41 +00002035 }
2036
Chris Lattnerbdd558c2006-04-17 17:55:10 +00002037 // vsplti + rol self.
2038 if (SextVal == (int)(((unsigned)i << TypeShiftAmt) |
2039 ((unsigned)i >> (SplatBitSize-TypeShiftAmt)))) {
2040 Op = BuildSplatI(i, SplatSize, Op.getValueType(), DAG);
2041 static const unsigned IIDs[] = { // Intrinsic to use for each size.
2042 Intrinsic::ppc_altivec_vrlb, Intrinsic::ppc_altivec_vrlh, 0,
2043 Intrinsic::ppc_altivec_vrlw
2044 };
Chris Lattnere7c768e2006-04-18 03:24:30 +00002045 return BuildIntrinsicOp(IIDs[SplatSize-1], Op, Op, DAG);
Chris Lattnerbdd558c2006-04-17 17:55:10 +00002046 }
2047
2048 // t = vsplti c, result = vsldoi t, t, 1
2049 if (SextVal == ((i << 8) | (i >> (TypeShiftAmt-8)))) {
2050 SDOperand T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG);
2051 return BuildVSLDOI(T, T, 1, Op.getValueType(), DAG);
2052 }
2053 // t = vsplti c, result = vsldoi t, t, 2
2054 if (SextVal == ((i << 16) | (i >> (TypeShiftAmt-16)))) {
2055 SDOperand T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG);
2056 return BuildVSLDOI(T, T, 2, Op.getValueType(), DAG);
2057 }
2058 // t = vsplti c, result = vsldoi t, t, 3
2059 if (SextVal == ((i << 24) | (i >> (TypeShiftAmt-24)))) {
2060 SDOperand T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG);
2061 return BuildVSLDOI(T, T, 3, Op.getValueType(), DAG);
2062 }
Chris Lattner6876e662006-04-17 06:58:41 +00002063 }
2064
Chris Lattner6876e662006-04-17 06:58:41 +00002065 // Three instruction sequences.
2066
Chris Lattnerdbce85d2006-04-17 18:09:22 +00002067 // Odd, in range [17,31]: (vsplti C)-(vsplti -16).
2068 if (SextVal >= 0 && SextVal <= 31) {
2069 SDOperand LHS = BuildSplatI(SextVal-16, SplatSize, Op.getValueType(),DAG);
2070 SDOperand RHS = BuildSplatI(-16, SplatSize, Op.getValueType(), DAG);
2071 return DAG.getNode(ISD::SUB, Op.getValueType(), LHS, RHS);
2072 }
2073 // Odd, in range [-31,-17]: (vsplti C)+(vsplti -16).
2074 if (SextVal >= -31 && SextVal <= 0) {
2075 SDOperand LHS = BuildSplatI(SextVal+16, SplatSize, Op.getValueType(),DAG);
2076 SDOperand RHS = BuildSplatI(-16, SplatSize, Op.getValueType(), DAG);
Chris Lattnerc4083822006-04-17 06:07:44 +00002077 return DAG.getNode(ISD::ADD, Op.getValueType(), LHS, RHS);
Chris Lattnerf1b47082006-04-14 05:19:18 +00002078 }
2079 }
Chris Lattnerb17f1672006-04-16 01:01:29 +00002080
Chris Lattnerf1b47082006-04-14 05:19:18 +00002081 return SDOperand();
2082}
2083
Chris Lattner59138102006-04-17 05:28:54 +00002084/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
2085/// the specified operations to build the shuffle.
2086static SDOperand GeneratePerfectShuffle(unsigned PFEntry, SDOperand LHS,
2087 SDOperand RHS, SelectionDAG &DAG) {
2088 unsigned OpNum = (PFEntry >> 26) & 0x0F;
2089 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
2090 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
2091
2092 enum {
Chris Lattner00402c72006-05-16 04:20:24 +00002093 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
Chris Lattner59138102006-04-17 05:28:54 +00002094 OP_VMRGHW,
2095 OP_VMRGLW,
2096 OP_VSPLTISW0,
2097 OP_VSPLTISW1,
2098 OP_VSPLTISW2,
2099 OP_VSPLTISW3,
2100 OP_VSLDOI4,
2101 OP_VSLDOI8,
Chris Lattnerd74ea2b2006-05-24 17:04:05 +00002102 OP_VSLDOI12
Chris Lattner59138102006-04-17 05:28:54 +00002103 };
2104
2105 if (OpNum == OP_COPY) {
2106 if (LHSID == (1*9+2)*9+3) return LHS;
2107 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
2108 return RHS;
2109 }
2110
Chris Lattnerbdd558c2006-04-17 17:55:10 +00002111 SDOperand OpLHS, OpRHS;
2112 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG);
2113 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG);
2114
Chris Lattner59138102006-04-17 05:28:54 +00002115 unsigned ShufIdxs[16];
2116 switch (OpNum) {
2117 default: assert(0 && "Unknown i32 permute!");
2118 case OP_VMRGHW:
2119 ShufIdxs[ 0] = 0; ShufIdxs[ 1] = 1; ShufIdxs[ 2] = 2; ShufIdxs[ 3] = 3;
2120 ShufIdxs[ 4] = 16; ShufIdxs[ 5] = 17; ShufIdxs[ 6] = 18; ShufIdxs[ 7] = 19;
2121 ShufIdxs[ 8] = 4; ShufIdxs[ 9] = 5; ShufIdxs[10] = 6; ShufIdxs[11] = 7;
2122 ShufIdxs[12] = 20; ShufIdxs[13] = 21; ShufIdxs[14] = 22; ShufIdxs[15] = 23;
2123 break;
2124 case OP_VMRGLW:
2125 ShufIdxs[ 0] = 8; ShufIdxs[ 1] = 9; ShufIdxs[ 2] = 10; ShufIdxs[ 3] = 11;
2126 ShufIdxs[ 4] = 24; ShufIdxs[ 5] = 25; ShufIdxs[ 6] = 26; ShufIdxs[ 7] = 27;
2127 ShufIdxs[ 8] = 12; ShufIdxs[ 9] = 13; ShufIdxs[10] = 14; ShufIdxs[11] = 15;
2128 ShufIdxs[12] = 28; ShufIdxs[13] = 29; ShufIdxs[14] = 30; ShufIdxs[15] = 31;
2129 break;
2130 case OP_VSPLTISW0:
2131 for (unsigned i = 0; i != 16; ++i)
2132 ShufIdxs[i] = (i&3)+0;
2133 break;
2134 case OP_VSPLTISW1:
2135 for (unsigned i = 0; i != 16; ++i)
2136 ShufIdxs[i] = (i&3)+4;
2137 break;
2138 case OP_VSPLTISW2:
2139 for (unsigned i = 0; i != 16; ++i)
2140 ShufIdxs[i] = (i&3)+8;
2141 break;
2142 case OP_VSPLTISW3:
2143 for (unsigned i = 0; i != 16; ++i)
2144 ShufIdxs[i] = (i&3)+12;
2145 break;
2146 case OP_VSLDOI4:
Chris Lattnerbdd558c2006-04-17 17:55:10 +00002147 return BuildVSLDOI(OpLHS, OpRHS, 4, OpLHS.getValueType(), DAG);
Chris Lattner59138102006-04-17 05:28:54 +00002148 case OP_VSLDOI8:
Chris Lattnerbdd558c2006-04-17 17:55:10 +00002149 return BuildVSLDOI(OpLHS, OpRHS, 8, OpLHS.getValueType(), DAG);
Chris Lattner59138102006-04-17 05:28:54 +00002150 case OP_VSLDOI12:
Chris Lattnerbdd558c2006-04-17 17:55:10 +00002151 return BuildVSLDOI(OpLHS, OpRHS, 12, OpLHS.getValueType(), DAG);
Chris Lattner59138102006-04-17 05:28:54 +00002152 }
Chris Lattnere2199452006-08-11 17:38:39 +00002153 SDOperand Ops[16];
Chris Lattner59138102006-04-17 05:28:54 +00002154 for (unsigned i = 0; i != 16; ++i)
Chris Lattnere2199452006-08-11 17:38:39 +00002155 Ops[i] = DAG.getConstant(ShufIdxs[i], MVT::i32);
Chris Lattner59138102006-04-17 05:28:54 +00002156
2157 return DAG.getNode(ISD::VECTOR_SHUFFLE, OpLHS.getValueType(), OpLHS, OpRHS,
Chris Lattnere2199452006-08-11 17:38:39 +00002158 DAG.getNode(ISD::BUILD_VECTOR, MVT::v16i8, Ops, 16));
Chris Lattner59138102006-04-17 05:28:54 +00002159}
2160
Chris Lattnerf1b47082006-04-14 05:19:18 +00002161/// LowerVECTOR_SHUFFLE - Return the code we lower for VECTOR_SHUFFLE. If this
2162/// is a shuffle we can handle in a single instruction, return it. Otherwise,
2163/// return the code it can be lowered into. Worst case, it can always be
2164/// lowered into a vperm.
2165static SDOperand LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
2166 SDOperand V1 = Op.getOperand(0);
2167 SDOperand V2 = Op.getOperand(1);
2168 SDOperand PermMask = Op.getOperand(2);
2169
2170 // Cases that are handled by instructions that take permute immediates
2171 // (such as vsplt*) should be left as VECTOR_SHUFFLE nodes so they can be
2172 // selected by the instruction selector.
2173 if (V2.getOpcode() == ISD::UNDEF) {
2174 if (PPC::isSplatShuffleMask(PermMask.Val, 1) ||
2175 PPC::isSplatShuffleMask(PermMask.Val, 2) ||
2176 PPC::isSplatShuffleMask(PermMask.Val, 4) ||
2177 PPC::isVPKUWUMShuffleMask(PermMask.Val, true) ||
2178 PPC::isVPKUHUMShuffleMask(PermMask.Val, true) ||
2179 PPC::isVSLDOIShuffleMask(PermMask.Val, true) != -1 ||
2180 PPC::isVMRGLShuffleMask(PermMask.Val, 1, true) ||
2181 PPC::isVMRGLShuffleMask(PermMask.Val, 2, true) ||
2182 PPC::isVMRGLShuffleMask(PermMask.Val, 4, true) ||
2183 PPC::isVMRGHShuffleMask(PermMask.Val, 1, true) ||
2184 PPC::isVMRGHShuffleMask(PermMask.Val, 2, true) ||
2185 PPC::isVMRGHShuffleMask(PermMask.Val, 4, true)) {
2186 return Op;
2187 }
2188 }
2189
2190 // Altivec has a variety of "shuffle immediates" that take two vector inputs
2191 // and produce a fixed permutation. If any of these match, do not lower to
2192 // VPERM.
2193 if (PPC::isVPKUWUMShuffleMask(PermMask.Val, false) ||
2194 PPC::isVPKUHUMShuffleMask(PermMask.Val, false) ||
2195 PPC::isVSLDOIShuffleMask(PermMask.Val, false) != -1 ||
2196 PPC::isVMRGLShuffleMask(PermMask.Val, 1, false) ||
2197 PPC::isVMRGLShuffleMask(PermMask.Val, 2, false) ||
2198 PPC::isVMRGLShuffleMask(PermMask.Val, 4, false) ||
2199 PPC::isVMRGHShuffleMask(PermMask.Val, 1, false) ||
2200 PPC::isVMRGHShuffleMask(PermMask.Val, 2, false) ||
2201 PPC::isVMRGHShuffleMask(PermMask.Val, 4, false))
2202 return Op;
2203
Chris Lattner59138102006-04-17 05:28:54 +00002204 // Check to see if this is a shuffle of 4-byte values. If so, we can use our
2205 // perfect shuffle table to emit an optimal matching sequence.
2206 unsigned PFIndexes[4];
2207 bool isFourElementShuffle = true;
2208 for (unsigned i = 0; i != 4 && isFourElementShuffle; ++i) { // Element number
2209 unsigned EltNo = 8; // Start out undef.
2210 for (unsigned j = 0; j != 4; ++j) { // Intra-element byte.
2211 if (PermMask.getOperand(i*4+j).getOpcode() == ISD::UNDEF)
2212 continue; // Undef, ignore it.
2213
2214 unsigned ByteSource =
2215 cast<ConstantSDNode>(PermMask.getOperand(i*4+j))->getValue();
2216 if ((ByteSource & 3) != j) {
2217 isFourElementShuffle = false;
2218 break;
2219 }
2220
2221 if (EltNo == 8) {
2222 EltNo = ByteSource/4;
2223 } else if (EltNo != ByteSource/4) {
2224 isFourElementShuffle = false;
2225 break;
2226 }
2227 }
2228 PFIndexes[i] = EltNo;
2229 }
2230
2231 // If this shuffle can be expressed as a shuffle of 4-byte elements, use the
2232 // perfect shuffle vector to determine if it is cost effective to do this as
2233 // discrete instructions, or whether we should use a vperm.
2234 if (isFourElementShuffle) {
2235 // Compute the index in the perfect shuffle table.
2236 unsigned PFTableIndex =
2237 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
2238
2239 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
2240 unsigned Cost = (PFEntry >> 30);
2241
2242 // Determining when to avoid vperm is tricky. Many things affect the cost
2243 // of vperm, particularly how many times the perm mask needs to be computed.
2244 // For example, if the perm mask can be hoisted out of a loop or is already
2245 // used (perhaps because there are multiple permutes with the same shuffle
2246 // mask?) the vperm has a cost of 1. OTOH, hoisting the permute mask out of
2247 // the loop requires an extra register.
2248 //
2249 // As a compromise, we only emit discrete instructions if the shuffle can be
2250 // generated in 3 or fewer operations. When we have loop information
2251 // available, if this block is within a loop, we should avoid using vperm
2252 // for 3-operation perms and use a constant pool load instead.
2253 if (Cost < 3)
2254 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG);
2255 }
Chris Lattnerf1b47082006-04-14 05:19:18 +00002256
2257 // Lower this to a VPERM(V1, V2, V3) expression, where V3 is a constant
2258 // vector that will get spilled to the constant pool.
2259 if (V2.getOpcode() == ISD::UNDEF) V2 = V1;
2260
2261 // The SHUFFLE_VECTOR mask is almost exactly what we want for vperm, except
2262 // that it is in input element units, not in bytes. Convert now.
2263 MVT::ValueType EltVT = MVT::getVectorBaseType(V1.getValueType());
2264 unsigned BytesPerElement = MVT::getSizeInBits(EltVT)/8;
2265
Chris Lattnere2199452006-08-11 17:38:39 +00002266 SmallVector<SDOperand, 16> ResultMask;
Chris Lattnerf1b47082006-04-14 05:19:18 +00002267 for (unsigned i = 0, e = PermMask.getNumOperands(); i != e; ++i) {
Chris Lattner730b4562006-04-15 23:48:05 +00002268 unsigned SrcElt;
2269 if (PermMask.getOperand(i).getOpcode() == ISD::UNDEF)
2270 SrcElt = 0;
2271 else
2272 SrcElt = cast<ConstantSDNode>(PermMask.getOperand(i))->getValue();
Chris Lattnerf1b47082006-04-14 05:19:18 +00002273
2274 for (unsigned j = 0; j != BytesPerElement; ++j)
2275 ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement+j,
2276 MVT::i8));
2277 }
2278
Chris Lattnere2199452006-08-11 17:38:39 +00002279 SDOperand VPermMask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v16i8,
2280 &ResultMask[0], ResultMask.size());
Chris Lattnerf1b47082006-04-14 05:19:18 +00002281 return DAG.getNode(PPCISD::VPERM, V1.getValueType(), V1, V2, VPermMask);
2282}
2283
Chris Lattner90564f22006-04-18 17:59:36 +00002284/// getAltivecCompareInfo - Given an intrinsic, return false if it is not an
2285/// altivec comparison. If it is, return true and fill in Opc/isDot with
2286/// information about the intrinsic.
2287static bool getAltivecCompareInfo(SDOperand Intrin, int &CompareOpc,
2288 bool &isDot) {
2289 unsigned IntrinsicID = cast<ConstantSDNode>(Intrin.getOperand(0))->getValue();
2290 CompareOpc = -1;
2291 isDot = false;
2292 switch (IntrinsicID) {
2293 default: return false;
2294 // Comparison predicates.
Chris Lattner1a635d62006-04-14 06:01:58 +00002295 case Intrinsic::ppc_altivec_vcmpbfp_p: CompareOpc = 966; isDot = 1; break;
2296 case Intrinsic::ppc_altivec_vcmpeqfp_p: CompareOpc = 198; isDot = 1; break;
2297 case Intrinsic::ppc_altivec_vcmpequb_p: CompareOpc = 6; isDot = 1; break;
2298 case Intrinsic::ppc_altivec_vcmpequh_p: CompareOpc = 70; isDot = 1; break;
2299 case Intrinsic::ppc_altivec_vcmpequw_p: CompareOpc = 134; isDot = 1; break;
2300 case Intrinsic::ppc_altivec_vcmpgefp_p: CompareOpc = 454; isDot = 1; break;
2301 case Intrinsic::ppc_altivec_vcmpgtfp_p: CompareOpc = 710; isDot = 1; break;
2302 case Intrinsic::ppc_altivec_vcmpgtsb_p: CompareOpc = 774; isDot = 1; break;
2303 case Intrinsic::ppc_altivec_vcmpgtsh_p: CompareOpc = 838; isDot = 1; break;
2304 case Intrinsic::ppc_altivec_vcmpgtsw_p: CompareOpc = 902; isDot = 1; break;
2305 case Intrinsic::ppc_altivec_vcmpgtub_p: CompareOpc = 518; isDot = 1; break;
2306 case Intrinsic::ppc_altivec_vcmpgtuh_p: CompareOpc = 582; isDot = 1; break;
2307 case Intrinsic::ppc_altivec_vcmpgtuw_p: CompareOpc = 646; isDot = 1; break;
2308
2309 // Normal Comparisons.
2310 case Intrinsic::ppc_altivec_vcmpbfp: CompareOpc = 966; isDot = 0; break;
2311 case Intrinsic::ppc_altivec_vcmpeqfp: CompareOpc = 198; isDot = 0; break;
2312 case Intrinsic::ppc_altivec_vcmpequb: CompareOpc = 6; isDot = 0; break;
2313 case Intrinsic::ppc_altivec_vcmpequh: CompareOpc = 70; isDot = 0; break;
2314 case Intrinsic::ppc_altivec_vcmpequw: CompareOpc = 134; isDot = 0; break;
2315 case Intrinsic::ppc_altivec_vcmpgefp: CompareOpc = 454; isDot = 0; break;
2316 case Intrinsic::ppc_altivec_vcmpgtfp: CompareOpc = 710; isDot = 0; break;
2317 case Intrinsic::ppc_altivec_vcmpgtsb: CompareOpc = 774; isDot = 0; break;
2318 case Intrinsic::ppc_altivec_vcmpgtsh: CompareOpc = 838; isDot = 0; break;
2319 case Intrinsic::ppc_altivec_vcmpgtsw: CompareOpc = 902; isDot = 0; break;
2320 case Intrinsic::ppc_altivec_vcmpgtub: CompareOpc = 518; isDot = 0; break;
2321 case Intrinsic::ppc_altivec_vcmpgtuh: CompareOpc = 582; isDot = 0; break;
2322 case Intrinsic::ppc_altivec_vcmpgtuw: CompareOpc = 646; isDot = 0; break;
2323 }
Chris Lattner90564f22006-04-18 17:59:36 +00002324 return true;
2325}
2326
2327/// LowerINTRINSIC_WO_CHAIN - If this is an intrinsic that we want to custom
2328/// lower, do it, otherwise return null.
2329static SDOperand LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
2330 // If this is a lowered altivec predicate compare, CompareOpc is set to the
2331 // opcode number of the comparison.
2332 int CompareOpc;
2333 bool isDot;
2334 if (!getAltivecCompareInfo(Op, CompareOpc, isDot))
2335 return SDOperand(); // Don't custom lower most intrinsics.
Chris Lattner1a635d62006-04-14 06:01:58 +00002336
Chris Lattner90564f22006-04-18 17:59:36 +00002337 // If this is a non-dot comparison, make the VCMP node and we are done.
Chris Lattner1a635d62006-04-14 06:01:58 +00002338 if (!isDot) {
2339 SDOperand Tmp = DAG.getNode(PPCISD::VCMP, Op.getOperand(2).getValueType(),
2340 Op.getOperand(1), Op.getOperand(2),
2341 DAG.getConstant(CompareOpc, MVT::i32));
2342 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Tmp);
2343 }
2344
2345 // Create the PPCISD altivec 'dot' comparison node.
Chris Lattner79e490a2006-08-11 17:18:05 +00002346 SDOperand Ops[] = {
2347 Op.getOperand(2), // LHS
2348 Op.getOperand(3), // RHS
2349 DAG.getConstant(CompareOpc, MVT::i32)
2350 };
Chris Lattner1a635d62006-04-14 06:01:58 +00002351 std::vector<MVT::ValueType> VTs;
Chris Lattner1a635d62006-04-14 06:01:58 +00002352 VTs.push_back(Op.getOperand(2).getValueType());
2353 VTs.push_back(MVT::Flag);
Chris Lattner79e490a2006-08-11 17:18:05 +00002354 SDOperand CompNode = DAG.getNode(PPCISD::VCMPo, VTs, Ops, 3);
Chris Lattner1a635d62006-04-14 06:01:58 +00002355
2356 // Now that we have the comparison, emit a copy from the CR to a GPR.
2357 // This is flagged to the above dot comparison.
2358 SDOperand Flags = DAG.getNode(PPCISD::MFCR, MVT::i32,
2359 DAG.getRegister(PPC::CR6, MVT::i32),
2360 CompNode.getValue(1));
2361
2362 // Unpack the result based on how the target uses it.
2363 unsigned BitNo; // Bit # of CR6.
2364 bool InvertBit; // Invert result?
2365 switch (cast<ConstantSDNode>(Op.getOperand(1))->getValue()) {
2366 default: // Can't happen, don't crash on invalid number though.
2367 case 0: // Return the value of the EQ bit of CR6.
2368 BitNo = 0; InvertBit = false;
2369 break;
2370 case 1: // Return the inverted value of the EQ bit of CR6.
2371 BitNo = 0; InvertBit = true;
2372 break;
2373 case 2: // Return the value of the LT bit of CR6.
2374 BitNo = 2; InvertBit = false;
2375 break;
2376 case 3: // Return the inverted value of the LT bit of CR6.
2377 BitNo = 2; InvertBit = true;
2378 break;
2379 }
2380
2381 // Shift the bit into the low position.
2382 Flags = DAG.getNode(ISD::SRL, MVT::i32, Flags,
2383 DAG.getConstant(8-(3-BitNo), MVT::i32));
2384 // Isolate the bit.
2385 Flags = DAG.getNode(ISD::AND, MVT::i32, Flags,
2386 DAG.getConstant(1, MVT::i32));
2387
2388 // If we are supposed to, toggle the bit.
2389 if (InvertBit)
2390 Flags = DAG.getNode(ISD::XOR, MVT::i32, Flags,
2391 DAG.getConstant(1, MVT::i32));
2392 return Flags;
2393}
2394
2395static SDOperand LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
2396 // Create a stack slot that is 16-byte aligned.
2397 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
2398 int FrameIdx = FrameInfo->CreateStackObject(16, 16);
Chris Lattner0d72a202006-07-28 16:45:47 +00002399 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
2400 SDOperand FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Chris Lattner1a635d62006-04-14 06:01:58 +00002401
2402 // Store the input value into Value#0 of the stack slot.
Evan Cheng786225a2006-10-05 23:01:46 +00002403 SDOperand Store = DAG.getStore(DAG.getEntryNode(),
Evan Cheng8b2794a2006-10-13 21:14:26 +00002404 Op.getOperand(0), FIdx, NULL, 0);
Chris Lattner1a635d62006-04-14 06:01:58 +00002405 // Load it out.
Evan Cheng466685d2006-10-09 20:57:25 +00002406 return DAG.getLoad(Op.getValueType(), Store, FIdx, NULL, 0);
Chris Lattner1a635d62006-04-14 06:01:58 +00002407}
2408
Chris Lattnere7c768e2006-04-18 03:24:30 +00002409static SDOperand LowerMUL(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner72dd9bd2006-04-18 03:43:48 +00002410 if (Op.getValueType() == MVT::v4i32) {
2411 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
2412
2413 SDOperand Zero = BuildSplatI( 0, 1, MVT::v4i32, DAG);
2414 SDOperand Neg16 = BuildSplatI(-16, 4, MVT::v4i32, DAG); // +16 as shift amt.
2415
2416 SDOperand RHSSwap = // = vrlw RHS, 16
2417 BuildIntrinsicOp(Intrinsic::ppc_altivec_vrlw, RHS, Neg16, DAG);
2418
2419 // Shrinkify inputs to v8i16.
2420 LHS = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, LHS);
2421 RHS = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, RHS);
2422 RHSSwap = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, RHSSwap);
2423
2424 // Low parts multiplied together, generating 32-bit results (we ignore the
2425 // top parts).
2426 SDOperand LoProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmulouh,
2427 LHS, RHS, DAG, MVT::v4i32);
2428
2429 SDOperand HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmsumuhm,
2430 LHS, RHSSwap, Zero, DAG, MVT::v4i32);
2431 // Shift the high parts up 16 bits.
2432 HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, HiProd, Neg16, DAG);
2433 return DAG.getNode(ISD::ADD, MVT::v4i32, LoProd, HiProd);
2434 } else if (Op.getValueType() == MVT::v8i16) {
2435 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
2436
Chris Lattnercea2aa72006-04-18 04:28:57 +00002437 SDOperand Zero = BuildSplatI(0, 1, MVT::v8i16, DAG);
Chris Lattner72dd9bd2006-04-18 03:43:48 +00002438
Chris Lattnercea2aa72006-04-18 04:28:57 +00002439 return BuildIntrinsicOp(Intrinsic::ppc_altivec_vmladduhm,
2440 LHS, RHS, Zero, DAG);
Chris Lattner19a81522006-04-18 03:57:35 +00002441 } else if (Op.getValueType() == MVT::v16i8) {
2442 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
2443
2444 // Multiply the even 8-bit parts, producing 16-bit sums.
2445 SDOperand EvenParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuleub,
2446 LHS, RHS, DAG, MVT::v8i16);
2447 EvenParts = DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, EvenParts);
2448
2449 // Multiply the odd 8-bit parts, producing 16-bit sums.
2450 SDOperand OddParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuloub,
2451 LHS, RHS, DAG, MVT::v8i16);
2452 OddParts = DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, OddParts);
2453
2454 // Merge the results together.
Chris Lattnere2199452006-08-11 17:38:39 +00002455 SDOperand Ops[16];
Chris Lattner19a81522006-04-18 03:57:35 +00002456 for (unsigned i = 0; i != 8; ++i) {
Chris Lattnere2199452006-08-11 17:38:39 +00002457 Ops[i*2 ] = DAG.getConstant(2*i+1, MVT::i8);
2458 Ops[i*2+1] = DAG.getConstant(2*i+1+16, MVT::i8);
Chris Lattner19a81522006-04-18 03:57:35 +00002459 }
Chris Lattner19a81522006-04-18 03:57:35 +00002460 return DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v16i8, EvenParts, OddParts,
Chris Lattnere2199452006-08-11 17:38:39 +00002461 DAG.getNode(ISD::BUILD_VECTOR, MVT::v16i8, Ops, 16));
Chris Lattner72dd9bd2006-04-18 03:43:48 +00002462 } else {
2463 assert(0 && "Unknown mul to lower!");
2464 abort();
2465 }
Chris Lattnere7c768e2006-04-18 03:24:30 +00002466}
2467
Chris Lattnere4bc9ea2005-08-26 00:52:45 +00002468/// LowerOperation - Provide custom lowering hooks for some operations.
2469///
Nate Begeman21e463b2005-10-16 05:39:50 +00002470SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +00002471 switch (Op.getOpcode()) {
2472 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattner1a635d62006-04-14 06:01:58 +00002473 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
2474 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Nate Begeman37efe672006-04-22 18:53:45 +00002475 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Chris Lattner1a635d62006-04-14 06:01:58 +00002476 case ISD::SETCC: return LowerSETCC(Op, DAG);
2477 case ISD::VASTART: return LowerVASTART(Op, DAG, VarArgsFrameIndex);
Chris Lattneref957102006-06-21 00:34:03 +00002478 case ISD::FORMAL_ARGUMENTS:
Chris Lattnerc91a4752006-06-26 22:48:35 +00002479 return LowerFORMAL_ARGUMENTS(Op, DAG, VarArgsFrameIndex);
Chris Lattnerabde4602006-05-16 22:56:08 +00002480 case ISD::CALL: return LowerCALL(Op, DAG);
Chris Lattner1a635d62006-04-14 06:01:58 +00002481 case ISD::RET: return LowerRET(Op, DAG);
Chris Lattner7c0d6642005-10-02 06:37:13 +00002482
Chris Lattner1a635d62006-04-14 06:01:58 +00002483 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
2484 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
2485 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00002486
Chris Lattner1a635d62006-04-14 06:01:58 +00002487 // Lower 64-bit shifts.
Chris Lattner3fe6c1d2006-09-20 03:47:40 +00002488 case ISD::SHL_PARTS: return LowerSHL_PARTS(Op, DAG);
2489 case ISD::SRL_PARTS: return LowerSRL_PARTS(Op, DAG);
2490 case ISD::SRA_PARTS: return LowerSRA_PARTS(Op, DAG);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00002491
Chris Lattner1a635d62006-04-14 06:01:58 +00002492 // Vector-related lowering.
2493 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
2494 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
2495 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
2496 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
Chris Lattnere7c768e2006-04-18 03:24:30 +00002497 case ISD::MUL: return LowerMUL(Op, DAG);
Chris Lattnerbc11c342005-08-31 20:23:54 +00002498 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +00002499 return SDOperand();
2500}
2501
Chris Lattner1a635d62006-04-14 06:01:58 +00002502//===----------------------------------------------------------------------===//
2503// Other Lowering Code
2504//===----------------------------------------------------------------------===//
2505
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00002506MachineBasicBlock *
Nate Begeman21e463b2005-10-16 05:39:50 +00002507PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
2508 MachineBasicBlock *BB) {
Chris Lattnerc08f9022006-06-27 00:04:13 +00002509 assert((MI->getOpcode() == PPC::SELECT_CC_I4 ||
2510 MI->getOpcode() == PPC::SELECT_CC_I8 ||
Chris Lattner919c0322005-10-01 01:35:02 +00002511 MI->getOpcode() == PPC::SELECT_CC_F4 ||
Chris Lattner710ff322006-04-08 22:45:08 +00002512 MI->getOpcode() == PPC::SELECT_CC_F8 ||
2513 MI->getOpcode() == PPC::SELECT_CC_VRRC) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00002514 "Unexpected instr type to insert");
2515
2516 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
2517 // control-flow pattern. The incoming instruction knows the destination vreg
2518 // to set, the condition code register to branch on, the true/false values to
2519 // select between, and a branch opcode to use.
2520 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2521 ilist<MachineBasicBlock>::iterator It = BB;
2522 ++It;
2523
2524 // thisMBB:
2525 // ...
2526 // TrueVal = ...
2527 // cmpTY ccX, r1, r2
2528 // bCC copy1MBB
2529 // fallthrough --> copy0MBB
2530 MachineBasicBlock *thisMBB = BB;
2531 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
2532 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
2533 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
2534 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
2535 MachineFunction *F = BB->getParent();
2536 F->getBasicBlockList().insert(It, copy0MBB);
2537 F->getBasicBlockList().insert(It, sinkMBB);
Nate Begemanf15485a2006-03-27 01:32:24 +00002538 // Update machine-CFG edges by first adding all successors of the current
2539 // block to the new block which will contain the Phi node for the select.
2540 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
2541 e = BB->succ_end(); i != e; ++i)
2542 sinkMBB->addSuccessor(*i);
2543 // Next, remove all successors of the current block, and add the true
2544 // and fallthrough blocks as its successors.
2545 while(!BB->succ_empty())
2546 BB->removeSuccessor(BB->succ_begin());
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00002547 BB->addSuccessor(copy0MBB);
2548 BB->addSuccessor(sinkMBB);
2549
2550 // copy0MBB:
2551 // %FalseValue = ...
2552 // # fallthrough to sinkMBB
2553 BB = copy0MBB;
2554
2555 // Update machine-CFG edges
2556 BB->addSuccessor(sinkMBB);
2557
2558 // sinkMBB:
2559 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2560 // ...
2561 BB = sinkMBB;
2562 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
2563 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
2564 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
2565
2566 delete MI; // The pseudo instruction is gone now.
2567 return BB;
2568}
2569
Chris Lattner1a635d62006-04-14 06:01:58 +00002570//===----------------------------------------------------------------------===//
2571// Target Optimization Hooks
2572//===----------------------------------------------------------------------===//
2573
Chris Lattner8c13d0a2006-03-01 04:57:39 +00002574SDOperand PPCTargetLowering::PerformDAGCombine(SDNode *N,
2575 DAGCombinerInfo &DCI) const {
2576 TargetMachine &TM = getTargetMachine();
2577 SelectionDAG &DAG = DCI.DAG;
2578 switch (N->getOpcode()) {
2579 default: break;
Chris Lattnercf9d0ac2006-09-19 05:22:59 +00002580 case PPCISD::SHL:
2581 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
2582 if (C->getValue() == 0) // 0 << V -> 0.
2583 return N->getOperand(0);
2584 }
2585 break;
2586 case PPCISD::SRL:
2587 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
2588 if (C->getValue() == 0) // 0 >>u V -> 0.
2589 return N->getOperand(0);
2590 }
2591 break;
2592 case PPCISD::SRA:
2593 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
2594 if (C->getValue() == 0 || // 0 >>s V -> 0.
2595 C->isAllOnesValue()) // -1 >>s V -> -1.
2596 return N->getOperand(0);
2597 }
2598 break;
2599
Chris Lattner8c13d0a2006-03-01 04:57:39 +00002600 case ISD::SINT_TO_FP:
Chris Lattnera7a58542006-06-16 17:34:12 +00002601 if (TM.getSubtarget<PPCSubtarget>().has64BitSupport()) {
Chris Lattnerecfe55e2006-03-22 05:30:33 +00002602 if (N->getOperand(0).getOpcode() == ISD::FP_TO_SINT) {
2603 // Turn (sint_to_fp (fp_to_sint X)) -> fctidz/fcfid without load/stores.
2604 // We allow the src/dst to be either f32/f64, but the intermediate
2605 // type must be i64.
2606 if (N->getOperand(0).getValueType() == MVT::i64) {
2607 SDOperand Val = N->getOperand(0).getOperand(0);
2608 if (Val.getValueType() == MVT::f32) {
2609 Val = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Val);
2610 DCI.AddToWorklist(Val.Val);
2611 }
2612
2613 Val = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Val);
Chris Lattner8c13d0a2006-03-01 04:57:39 +00002614 DCI.AddToWorklist(Val.Val);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00002615 Val = DAG.getNode(PPCISD::FCFID, MVT::f64, Val);
Chris Lattner8c13d0a2006-03-01 04:57:39 +00002616 DCI.AddToWorklist(Val.Val);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00002617 if (N->getValueType(0) == MVT::f32) {
2618 Val = DAG.getNode(ISD::FP_ROUND, MVT::f32, Val);
2619 DCI.AddToWorklist(Val.Val);
2620 }
2621 return Val;
2622 } else if (N->getOperand(0).getValueType() == MVT::i32) {
2623 // If the intermediate type is i32, we can avoid the load/store here
2624 // too.
Chris Lattner8c13d0a2006-03-01 04:57:39 +00002625 }
Chris Lattner8c13d0a2006-03-01 04:57:39 +00002626 }
2627 }
2628 break;
Chris Lattner51269842006-03-01 05:50:56 +00002629 case ISD::STORE:
2630 // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)).
2631 if (TM.getSubtarget<PPCSubtarget>().hasSTFIWX() &&
2632 N->getOperand(1).getOpcode() == ISD::FP_TO_SINT &&
2633 N->getOperand(1).getValueType() == MVT::i32) {
2634 SDOperand Val = N->getOperand(1).getOperand(0);
2635 if (Val.getValueType() == MVT::f32) {
2636 Val = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Val);
2637 DCI.AddToWorklist(Val.Val);
2638 }
2639 Val = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Val);
2640 DCI.AddToWorklist(Val.Val);
2641
2642 Val = DAG.getNode(PPCISD::STFIWX, MVT::Other, N->getOperand(0), Val,
2643 N->getOperand(2), N->getOperand(3));
2644 DCI.AddToWorklist(Val.Val);
2645 return Val;
2646 }
Chris Lattnerd9989382006-07-10 20:56:58 +00002647
2648 // Turn STORE (BSWAP) -> sthbrx/stwbrx.
2649 if (N->getOperand(1).getOpcode() == ISD::BSWAP &&
2650 N->getOperand(1).Val->hasOneUse() &&
2651 (N->getOperand(1).getValueType() == MVT::i32 ||
2652 N->getOperand(1).getValueType() == MVT::i16)) {
2653 SDOperand BSwapOp = N->getOperand(1).getOperand(0);
2654 // Do an any-extend to 32-bits if this is a half-word input.
2655 if (BSwapOp.getValueType() == MVT::i16)
2656 BSwapOp = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, BSwapOp);
2657
2658 return DAG.getNode(PPCISD::STBRX, MVT::Other, N->getOperand(0), BSwapOp,
2659 N->getOperand(2), N->getOperand(3),
2660 DAG.getValueType(N->getOperand(1).getValueType()));
2661 }
2662 break;
2663 case ISD::BSWAP:
2664 // Turn BSWAP (LOAD) -> lhbrx/lwbrx.
Evan Cheng466685d2006-10-09 20:57:25 +00002665 if (ISD::isNON_EXTLoad(N->getOperand(0).Val) &&
Chris Lattnerd9989382006-07-10 20:56:58 +00002666 N->getOperand(0).hasOneUse() &&
2667 (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i16)) {
2668 SDOperand Load = N->getOperand(0);
Evan Cheng466685d2006-10-09 20:57:25 +00002669 LoadSDNode *LD = cast<LoadSDNode>(Load);
Chris Lattnerd9989382006-07-10 20:56:58 +00002670 // Create the byte-swapping load.
2671 std::vector<MVT::ValueType> VTs;
2672 VTs.push_back(MVT::i32);
2673 VTs.push_back(MVT::Other);
Evan Cheng466685d2006-10-09 20:57:25 +00002674 SDOperand SV = DAG.getSrcValue(LD->getSrcValue(), LD->getSrcValueOffset());
Chris Lattner79e490a2006-08-11 17:18:05 +00002675 SDOperand Ops[] = {
Evan Cheng466685d2006-10-09 20:57:25 +00002676 LD->getChain(), // Chain
2677 LD->getBasePtr(), // Ptr
2678 SV, // SrcValue
Chris Lattner79e490a2006-08-11 17:18:05 +00002679 DAG.getValueType(N->getValueType(0)) // VT
2680 };
2681 SDOperand BSLoad = DAG.getNode(PPCISD::LBRX, VTs, Ops, 4);
Chris Lattnerd9989382006-07-10 20:56:58 +00002682
2683 // If this is an i16 load, insert the truncate.
2684 SDOperand ResVal = BSLoad;
2685 if (N->getValueType(0) == MVT::i16)
2686 ResVal = DAG.getNode(ISD::TRUNCATE, MVT::i16, BSLoad);
2687
2688 // First, combine the bswap away. This makes the value produced by the
2689 // load dead.
2690 DCI.CombineTo(N, ResVal);
2691
2692 // Next, combine the load away, we give it a bogus result value but a real
2693 // chain result. The result value is dead because the bswap is dead.
2694 DCI.CombineTo(Load.Val, ResVal, BSLoad.getValue(1));
2695
2696 // Return N so it doesn't get rechecked!
2697 return SDOperand(N, 0);
2698 }
2699
Chris Lattner51269842006-03-01 05:50:56 +00002700 break;
Chris Lattner4468c222006-03-31 06:02:07 +00002701 case PPCISD::VCMP: {
2702 // If a VCMPo node already exists with exactly the same operands as this
2703 // node, use its result instead of this node (VCMPo computes both a CR6 and
2704 // a normal output).
2705 //
2706 if (!N->getOperand(0).hasOneUse() &&
2707 !N->getOperand(1).hasOneUse() &&
2708 !N->getOperand(2).hasOneUse()) {
2709
2710 // Scan all of the users of the LHS, looking for VCMPo's that match.
2711 SDNode *VCMPoNode = 0;
2712
2713 SDNode *LHSN = N->getOperand(0).Val;
2714 for (SDNode::use_iterator UI = LHSN->use_begin(), E = LHSN->use_end();
2715 UI != E; ++UI)
2716 if ((*UI)->getOpcode() == PPCISD::VCMPo &&
2717 (*UI)->getOperand(1) == N->getOperand(1) &&
2718 (*UI)->getOperand(2) == N->getOperand(2) &&
2719 (*UI)->getOperand(0) == N->getOperand(0)) {
2720 VCMPoNode = *UI;
2721 break;
2722 }
2723
Chris Lattner00901202006-04-18 18:28:22 +00002724 // If there is no VCMPo node, or if the flag value has a single use, don't
2725 // transform this.
2726 if (!VCMPoNode || VCMPoNode->hasNUsesOfValue(0, 1))
2727 break;
2728
2729 // Look at the (necessarily single) use of the flag value. If it has a
2730 // chain, this transformation is more complex. Note that multiple things
2731 // could use the value result, which we should ignore.
2732 SDNode *FlagUser = 0;
2733 for (SDNode::use_iterator UI = VCMPoNode->use_begin();
2734 FlagUser == 0; ++UI) {
2735 assert(UI != VCMPoNode->use_end() && "Didn't find user!");
2736 SDNode *User = *UI;
2737 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
2738 if (User->getOperand(i) == SDOperand(VCMPoNode, 1)) {
2739 FlagUser = User;
2740 break;
2741 }
2742 }
2743 }
2744
2745 // If the user is a MFCR instruction, we know this is safe. Otherwise we
2746 // give up for right now.
2747 if (FlagUser->getOpcode() == PPCISD::MFCR)
Chris Lattner4468c222006-03-31 06:02:07 +00002748 return SDOperand(VCMPoNode, 0);
2749 }
2750 break;
2751 }
Chris Lattner90564f22006-04-18 17:59:36 +00002752 case ISD::BR_CC: {
2753 // If this is a branch on an altivec predicate comparison, lower this so
2754 // that we don't have to do a MFCR: instead, branch directly on CR6. This
2755 // lowering is done pre-legalize, because the legalizer lowers the predicate
2756 // compare down to code that is difficult to reassemble.
2757 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
2758 SDOperand LHS = N->getOperand(2), RHS = N->getOperand(3);
2759 int CompareOpc;
2760 bool isDot;
2761
2762 if (LHS.getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
2763 isa<ConstantSDNode>(RHS) && (CC == ISD::SETEQ || CC == ISD::SETNE) &&
2764 getAltivecCompareInfo(LHS, CompareOpc, isDot)) {
2765 assert(isDot && "Can't compare against a vector result!");
2766
2767 // If this is a comparison against something other than 0/1, then we know
2768 // that the condition is never/always true.
2769 unsigned Val = cast<ConstantSDNode>(RHS)->getValue();
2770 if (Val != 0 && Val != 1) {
2771 if (CC == ISD::SETEQ) // Cond never true, remove branch.
2772 return N->getOperand(0);
2773 // Always !=, turn it into an unconditional branch.
2774 return DAG.getNode(ISD::BR, MVT::Other,
2775 N->getOperand(0), N->getOperand(4));
2776 }
2777
2778 bool BranchOnWhenPredTrue = (CC == ISD::SETEQ) ^ (Val == 0);
2779
2780 // Create the PPCISD altivec 'dot' comparison node.
Chris Lattner90564f22006-04-18 17:59:36 +00002781 std::vector<MVT::ValueType> VTs;
Chris Lattner79e490a2006-08-11 17:18:05 +00002782 SDOperand Ops[] = {
2783 LHS.getOperand(2), // LHS of compare
2784 LHS.getOperand(3), // RHS of compare
2785 DAG.getConstant(CompareOpc, MVT::i32)
2786 };
Chris Lattner90564f22006-04-18 17:59:36 +00002787 VTs.push_back(LHS.getOperand(2).getValueType());
2788 VTs.push_back(MVT::Flag);
Chris Lattner79e490a2006-08-11 17:18:05 +00002789 SDOperand CompNode = DAG.getNode(PPCISD::VCMPo, VTs, Ops, 3);
Chris Lattner90564f22006-04-18 17:59:36 +00002790
2791 // Unpack the result based on how the target uses it.
2792 unsigned CompOpc;
2793 switch (cast<ConstantSDNode>(LHS.getOperand(1))->getValue()) {
2794 default: // Can't happen, don't crash on invalid number though.
2795 case 0: // Branch on the value of the EQ bit of CR6.
2796 CompOpc = BranchOnWhenPredTrue ? PPC::BEQ : PPC::BNE;
2797 break;
2798 case 1: // Branch on the inverted value of the EQ bit of CR6.
2799 CompOpc = BranchOnWhenPredTrue ? PPC::BNE : PPC::BEQ;
2800 break;
2801 case 2: // Branch on the value of the LT bit of CR6.
2802 CompOpc = BranchOnWhenPredTrue ? PPC::BLT : PPC::BGE;
2803 break;
2804 case 3: // Branch on the inverted value of the LT bit of CR6.
2805 CompOpc = BranchOnWhenPredTrue ? PPC::BGE : PPC::BLT;
2806 break;
2807 }
2808
2809 return DAG.getNode(PPCISD::COND_BRANCH, MVT::Other, N->getOperand(0),
2810 DAG.getRegister(PPC::CR6, MVT::i32),
2811 DAG.getConstant(CompOpc, MVT::i32),
2812 N->getOperand(4), CompNode.getValue(1));
2813 }
2814 break;
2815 }
Chris Lattner8c13d0a2006-03-01 04:57:39 +00002816 }
2817
2818 return SDOperand();
2819}
2820
Chris Lattner1a635d62006-04-14 06:01:58 +00002821//===----------------------------------------------------------------------===//
2822// Inline Assembly Support
2823//===----------------------------------------------------------------------===//
2824
Chris Lattnerbbe77de2006-04-02 06:26:07 +00002825void PPCTargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
2826 uint64_t Mask,
2827 uint64_t &KnownZero,
2828 uint64_t &KnownOne,
2829 unsigned Depth) const {
2830 KnownZero = 0;
2831 KnownOne = 0;
2832 switch (Op.getOpcode()) {
2833 default: break;
Chris Lattnerd9989382006-07-10 20:56:58 +00002834 case PPCISD::LBRX: {
2835 // lhbrx is known to have the top bits cleared out.
2836 if (cast<VTSDNode>(Op.getOperand(3))->getVT() == MVT::i16)
2837 KnownZero = 0xFFFF0000;
2838 break;
2839 }
Chris Lattnerbbe77de2006-04-02 06:26:07 +00002840 case ISD::INTRINSIC_WO_CHAIN: {
2841 switch (cast<ConstantSDNode>(Op.getOperand(0))->getValue()) {
2842 default: break;
2843 case Intrinsic::ppc_altivec_vcmpbfp_p:
2844 case Intrinsic::ppc_altivec_vcmpeqfp_p:
2845 case Intrinsic::ppc_altivec_vcmpequb_p:
2846 case Intrinsic::ppc_altivec_vcmpequh_p:
2847 case Intrinsic::ppc_altivec_vcmpequw_p:
2848 case Intrinsic::ppc_altivec_vcmpgefp_p:
2849 case Intrinsic::ppc_altivec_vcmpgtfp_p:
2850 case Intrinsic::ppc_altivec_vcmpgtsb_p:
2851 case Intrinsic::ppc_altivec_vcmpgtsh_p:
2852 case Intrinsic::ppc_altivec_vcmpgtsw_p:
2853 case Intrinsic::ppc_altivec_vcmpgtub_p:
2854 case Intrinsic::ppc_altivec_vcmpgtuh_p:
2855 case Intrinsic::ppc_altivec_vcmpgtuw_p:
2856 KnownZero = ~1U; // All bits but the low one are known to be zero.
2857 break;
2858 }
2859 }
2860 }
2861}
2862
2863
Chris Lattnerad3bc8d2006-02-07 20:16:30 +00002864/// getConstraintType - Given a constraint letter, return the type of
2865/// constraint it is for this target.
2866PPCTargetLowering::ConstraintType
2867PPCTargetLowering::getConstraintType(char ConstraintLetter) const {
2868 switch (ConstraintLetter) {
2869 default: break;
2870 case 'b':
2871 case 'r':
2872 case 'f':
2873 case 'v':
2874 case 'y':
2875 return C_RegisterClass;
2876 }
2877 return TargetLowering::getConstraintType(ConstraintLetter);
2878}
2879
Chris Lattner331d1bc2006-11-02 01:44:04 +00002880std::pair<unsigned, const TargetRegisterClass*>
2881PPCTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
2882 MVT::ValueType VT) const {
Chris Lattnerddc787d2006-01-31 19:20:21 +00002883 if (Constraint.size() == 1) {
Chris Lattner331d1bc2006-11-02 01:44:04 +00002884 // GCC RS6000 Constraint Letters
2885 switch (Constraint[0]) {
2886 case 'b': // R1-R31
2887 case 'r': // R0-R31
2888 if (VT == MVT::i64 && PPCSubTarget.isPPC64())
2889 return std::make_pair(0U, PPC::G8RCRegisterClass);
2890 return std::make_pair(0U, PPC::GPRCRegisterClass);
2891 case 'f':
2892 if (VT == MVT::f32)
2893 return std::make_pair(0U, PPC::F4RCRegisterClass);
2894 else if (VT == MVT::f64)
2895 return std::make_pair(0U, PPC::F8RCRegisterClass);
2896 break;
Chris Lattnerddc787d2006-01-31 19:20:21 +00002897 case 'v':
Chris Lattner331d1bc2006-11-02 01:44:04 +00002898 return std::make_pair(0U, PPC::VRRCRegisterClass);
2899 case 'y': // crrc
2900 return std::make_pair(0U, PPC::CRRCRegisterClass);
Chris Lattnerddc787d2006-01-31 19:20:21 +00002901 }
2902 }
2903
Chris Lattner331d1bc2006-11-02 01:44:04 +00002904 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattnerddc787d2006-01-31 19:20:21 +00002905}
Chris Lattner763317d2006-02-07 00:47:13 +00002906
Chris Lattner331d1bc2006-11-02 01:44:04 +00002907
Chris Lattner763317d2006-02-07 00:47:13 +00002908// isOperandValidForConstraint
Chris Lattnerdba1aee2006-10-31 19:40:43 +00002909SDOperand PPCTargetLowering::
2910isOperandValidForConstraint(SDOperand Op, char Letter, SelectionDAG &DAG) {
Chris Lattner763317d2006-02-07 00:47:13 +00002911 switch (Letter) {
2912 default: break;
2913 case 'I':
2914 case 'J':
2915 case 'K':
2916 case 'L':
2917 case 'M':
2918 case 'N':
2919 case 'O':
2920 case 'P': {
Chris Lattnerdba1aee2006-10-31 19:40:43 +00002921 if (!isa<ConstantSDNode>(Op)) return SDOperand(0,0);// Must be an immediate.
Chris Lattner763317d2006-02-07 00:47:13 +00002922 unsigned Value = cast<ConstantSDNode>(Op)->getValue();
2923 switch (Letter) {
2924 default: assert(0 && "Unknown constraint letter!");
2925 case 'I': // "I" is a signed 16-bit constant.
Chris Lattnerdba1aee2006-10-31 19:40:43 +00002926 if ((short)Value == (int)Value) return Op;
2927 break;
Chris Lattner763317d2006-02-07 00:47:13 +00002928 case 'J': // "J" is a constant with only the high-order 16 bits nonzero.
2929 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits.
Chris Lattnerdba1aee2006-10-31 19:40:43 +00002930 if ((short)Value == 0) return Op;
2931 break;
Chris Lattner763317d2006-02-07 00:47:13 +00002932 case 'K': // "K" is a constant with only the low-order 16 bits nonzero.
Chris Lattnerdba1aee2006-10-31 19:40:43 +00002933 if ((Value >> 16) == 0) return Op;
2934 break;
Chris Lattner763317d2006-02-07 00:47:13 +00002935 case 'M': // "M" is a constant that is greater than 31.
Chris Lattnerdba1aee2006-10-31 19:40:43 +00002936 if (Value > 31) return Op;
2937 break;
Chris Lattner763317d2006-02-07 00:47:13 +00002938 case 'N': // "N" is a positive constant that is an exact power of two.
Chris Lattnerdba1aee2006-10-31 19:40:43 +00002939 if ((int)Value > 0 && isPowerOf2_32(Value)) return Op;
2940 break;
Chris Lattner763317d2006-02-07 00:47:13 +00002941 case 'O': // "O" is the constant zero.
Chris Lattnerdba1aee2006-10-31 19:40:43 +00002942 if (Value == 0) return Op;
2943 break;
Chris Lattner763317d2006-02-07 00:47:13 +00002944 case 'P': // "P" is a constant whose negation is a signed 16-bit constant.
Chris Lattnerdba1aee2006-10-31 19:40:43 +00002945 if ((short)-Value == (int)-Value) return Op;
2946 break;
Chris Lattner763317d2006-02-07 00:47:13 +00002947 }
2948 break;
2949 }
2950 }
2951
2952 // Handle standard constraint letters.
Chris Lattnerdba1aee2006-10-31 19:40:43 +00002953 return TargetLowering::isOperandValidForConstraint(Op, Letter, DAG);
Chris Lattner763317d2006-02-07 00:47:13 +00002954}
Evan Chengc4c62572006-03-13 23:20:37 +00002955
2956/// isLegalAddressImmediate - Return true if the integer value can be used
2957/// as the offset of the target addressing mode.
2958bool PPCTargetLowering::isLegalAddressImmediate(int64_t V) const {
2959 // PPC allows a sign-extended 16-bit immediate field.
2960 return (V > -(1 << 16) && V < (1 << 16)-1);
2961}
Reid Spencer3a9ec242006-08-28 01:02:49 +00002962
2963bool PPCTargetLowering::isLegalAddressImmediate(llvm::GlobalValue* GV) const {
2964 return TargetLowering::isLegalAddressImmediate(GV);
2965}