blob: 83bea7e11c2aafef666ca409ae37963c7f52fa3a [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//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner7c5a3d32005-08-16 17:14:42 +00007//
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"
Jim Laskey2f616bf2006-11-16 22:43:37 +000015#include "PPCMachineFunctionInfo.h"
Chris Lattnerdf4ed632006-11-17 22:10:59 +000016#include "PPCPredicates.h"
Chris Lattner16e71f22005-10-14 23:59:06 +000017#include "PPCTargetMachine.h"
Chris Lattner59138102006-04-17 05:28:54 +000018#include "PPCPerfectShuffle.h"
Owen Anderson718cb662007-09-07 04:06:50 +000019#include "llvm/ADT/STLExtras.h"
Nate Begeman750ac1b2006-02-01 07:19:44 +000020#include "llvm/ADT/VectorExtras.h"
Evan Chengc4c62572006-03-13 23:20:37 +000021#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Chris Lattnerb9a7bea2007-03-06 00:59:59 +000022#include "llvm/CodeGen/CallingConvLower.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000023#include "llvm/CodeGen/MachineFrameInfo.h"
24#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner8a2d3ca2005-08-26 21:23:58 +000025#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000026#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000027#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000028#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner0b1e4e52005-08-26 17:36:52 +000029#include "llvm/Constants.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000030#include "llvm/Function.h"
Chris Lattner6d92cad2006-03-26 10:06:40 +000031#include "llvm/Intrinsics.h"
Nate Begeman750ac1b2006-02-01 07:19:44 +000032#include "llvm/Support/MathExtras.h"
Evan Chengd2ee2182006-02-18 00:08:58 +000033#include "llvm/Target/TargetOptions.h"
Chris Lattner4eab7142006-11-10 02:08:47 +000034#include "llvm/Support/CommandLine.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000035using namespace llvm;
36
Chris Lattner3ee77402007-06-19 05:46:06 +000037static cl::opt<bool> EnablePPCPreinc("enable-ppc-preinc",
38cl::desc("enable preincrement load/store generation on PPC (experimental)"),
39 cl::Hidden);
Chris Lattner4eab7142006-11-10 02:08:47 +000040
Chris Lattner331d1bc2006-11-02 01:44:04 +000041PPCTargetLowering::PPCTargetLowering(PPCTargetMachine &TM)
42 : TargetLowering(TM), PPCSubTarget(*TM.getSubtargetImpl()) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +000043
Nate Begeman405e3ec2005-10-21 00:02:42 +000044 setPow2DivIsCheap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +000045
Chris Lattnerd145a612005-09-27 22:18:25 +000046 // Use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000047 setUseUnderscoreSetJmp(true);
48 setUseUnderscoreLongJmp(true);
Chris Lattnerd145a612005-09-27 22:18:25 +000049
Chris Lattner7c5a3d32005-08-16 17:14:42 +000050 // Set up the register classes.
Nate Begeman1d9d7422005-10-18 00:28:58 +000051 addRegisterClass(MVT::i32, PPC::GPRCRegisterClass);
52 addRegisterClass(MVT::f32, PPC::F4RCRegisterClass);
53 addRegisterClass(MVT::f64, PPC::F8RCRegisterClass);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000054
Evan Chengc5484282006-10-04 00:56:09 +000055 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
Duncan Sandsf9c98e62008-01-23 20:39:46 +000056 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +000057 setLoadXAction(ISD::SEXTLOAD, MVT::i8, Expand);
Duncan Sandsf9c98e62008-01-23 20:39:46 +000058
Chris Lattnerddf89562008-01-17 19:59:44 +000059 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
60
Chris Lattner94e509c2006-11-10 23:58:45 +000061 // PowerPC has pre-inc load and store's.
62 setIndexedLoadAction(ISD::PRE_INC, MVT::i1, Legal);
63 setIndexedLoadAction(ISD::PRE_INC, MVT::i8, Legal);
64 setIndexedLoadAction(ISD::PRE_INC, MVT::i16, Legal);
Evan Chengcd633192006-11-09 19:11:50 +000065 setIndexedLoadAction(ISD::PRE_INC, MVT::i32, Legal);
66 setIndexedLoadAction(ISD::PRE_INC, MVT::i64, Legal);
Chris Lattner94e509c2006-11-10 23:58:45 +000067 setIndexedStoreAction(ISD::PRE_INC, MVT::i1, Legal);
68 setIndexedStoreAction(ISD::PRE_INC, MVT::i8, Legal);
69 setIndexedStoreAction(ISD::PRE_INC, MVT::i16, Legal);
Evan Chengcd633192006-11-09 19:11:50 +000070 setIndexedStoreAction(ISD::PRE_INC, MVT::i32, Legal);
71 setIndexedStoreAction(ISD::PRE_INC, MVT::i64, Legal);
72
Dale Johannesen638ccd52007-10-06 01:24:11 +000073 // Shortening conversions involving ppcf128 get expanded (2 regs -> 1 reg)
74 setConvertAction(MVT::ppcf128, MVT::f64, Expand);
75 setConvertAction(MVT::ppcf128, MVT::f32, Expand);
Dale Johannesen6eaeff22007-10-10 01:01:31 +000076 // This is used in the ppcf128->int sequence. Note it has different semantics
77 // from FP_ROUND: that rounds to nearest, this rounds to zero.
78 setOperationAction(ISD::FP_ROUND_INREG, MVT::ppcf128, Custom);
Dale Johannesen638ccd52007-10-06 01:24:11 +000079
Chris Lattner7c5a3d32005-08-16 17:14:42 +000080 // PowerPC has no intrinsics for these particular operations
Andrew Lenharthd497d9f2008-02-16 14:46:26 +000081 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
82
Chris Lattner7c5a3d32005-08-16 17:14:42 +000083 // PowerPC has no SREM/UREM instructions
84 setOperationAction(ISD::SREM, MVT::i32, Expand);
85 setOperationAction(ISD::UREM, MVT::i32, Expand);
Chris Lattner563ecfb2006-06-27 18:18:41 +000086 setOperationAction(ISD::SREM, MVT::i64, Expand);
87 setOperationAction(ISD::UREM, MVT::i64, Expand);
Dan Gohman3ce990d2007-10-08 17:28:24 +000088
89 // Don't use SMUL_LOHI/UMUL_LOHI or SDIVREM/UDIVREM to lower SREM/UREM.
90 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
91 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
92 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
93 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
94 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
95 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
96 setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
97 setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000098
Dan Gohmanf96e4de2007-10-11 23:21:31 +000099 // We don't support sin/cos/sqrt/fmod/pow
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000100 setOperationAction(ISD::FSIN , MVT::f64, Expand);
101 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattner615c2d02005-09-28 22:29:58 +0000102 setOperationAction(ISD::FREM , MVT::f64, Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000103 setOperationAction(ISD::FPOW , MVT::f64, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000104 setOperationAction(ISD::FSIN , MVT::f32, Expand);
105 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattner615c2d02005-09-28 22:29:58 +0000106 setOperationAction(ISD::FREM , MVT::f32, Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000107 setOperationAction(ISD::FPOW , MVT::f32, Expand);
Dale Johannesen5c5eb802008-01-18 19:55:37 +0000108
Dan Gohman1a024862008-01-31 00:41:03 +0000109 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000110
111 // If we're enabling GP optimizations, use hardware square root
Chris Lattner1e9de3e2005-09-02 18:33:05 +0000112 if (!TM.getSubtarget<PPCSubtarget>().hasFSQRT()) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000113 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
114 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
115 }
116
Chris Lattner9601a862006-03-05 05:08:37 +0000117 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
118 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
119
Nate Begemand88fc032006-01-14 03:14:10 +0000120 // PowerPC does not have BSWAP, CTPOP or CTTZ
121 setOperationAction(ISD::BSWAP, MVT::i32 , Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000122 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
123 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
Chris Lattnerf89437d2006-06-27 20:14:52 +0000124 setOperationAction(ISD::BSWAP, MVT::i64 , Expand);
125 setOperationAction(ISD::CTPOP, MVT::i64 , Expand);
126 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000127
Nate Begeman35ef9132006-01-11 21:21:00 +0000128 // PowerPC does not have ROTR
129 setOperationAction(ISD::ROTR, MVT::i32 , Expand);
130
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000131 // PowerPC does not have Select
132 setOperationAction(ISD::SELECT, MVT::i32, Expand);
Chris Lattnerf89437d2006-06-27 20:14:52 +0000133 setOperationAction(ISD::SELECT, MVT::i64, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000134 setOperationAction(ISD::SELECT, MVT::f32, Expand);
135 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000136
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000137 // PowerPC wants to turn select_cc of FP into fsel when possible.
138 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
139 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Nate Begeman44775902006-01-31 08:17:29 +0000140
Nate Begeman750ac1b2006-02-01 07:19:44 +0000141 // PowerPC wants to optimize integer setcc a bit
Nate Begeman44775902006-01-31 08:17:29 +0000142 setOperationAction(ISD::SETCC, MVT::i32, Custom);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000143
Nate Begeman81e80972006-03-17 01:40:33 +0000144 // PowerPC does not have BRCOND which requires SetCC
145 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
Evan Chengc35497f2006-10-30 08:02:39 +0000146
147 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000148
Chris Lattnerf7605322005-08-31 21:09:52 +0000149 // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
150 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000151
Jim Laskeyad23c9d2005-08-17 00:40:22 +0000152 // PowerPC does not have [U|S]INT_TO_FP
153 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
154 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
155
Chris Lattner53e88452005-12-23 05:13:35 +0000156 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
157 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
Chris Lattner5f9faea2006-06-27 18:40:08 +0000158 setOperationAction(ISD::BIT_CONVERT, MVT::i64, Expand);
159 setOperationAction(ISD::BIT_CONVERT, MVT::f64, Expand);
Chris Lattner53e88452005-12-23 05:13:35 +0000160
Chris Lattner25b8b8c2006-04-28 21:56:10 +0000161 // We cannot sextinreg(i1). Expand to shifts.
162 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Jim Laskey2ad9f172007-02-22 14:56:36 +0000163
Jim Laskeyabf6d172006-01-05 01:25:28 +0000164 // Support label based line numbers.
Chris Lattnerf73bae12005-11-29 06:16:21 +0000165 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000166 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Nicolas Geoffray616585b2007-12-21 12:19:44 +0000167
168 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
169 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
170 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
171 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
172
Chris Lattnere6ec9f22005-09-10 00:21:06 +0000173
Nate Begeman28a6b022005-12-10 02:36:00 +0000174 // We want to legalize GlobalAddress and ConstantPool nodes into the
175 // appropriate instructions to materialize the address.
Chris Lattner3eef4e32005-11-17 18:26:56 +0000176 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +0000177 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Nate Begeman28a6b022005-12-10 02:36:00 +0000178 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Nate Begeman37efe672006-04-22 18:53:45 +0000179 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
Chris Lattner059ca0f2006-06-16 21:01:35 +0000180 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +0000181 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
Chris Lattner059ca0f2006-06-16 21:01:35 +0000182 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
183 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
184
Nate Begemanee625572006-01-27 21:09:22 +0000185 // RET must be custom lowered, to meet ABI requirements
186 setOperationAction(ISD::RET , MVT::Other, Custom);
Duncan Sands36397f52007-07-27 12:58:54 +0000187
Nate Begemanacc398c2006-01-25 18:21:52 +0000188 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
189 setOperationAction(ISD::VASTART , MVT::Other, Custom);
190
Nicolas Geoffray01119992007-04-03 13:59:52 +0000191 // VAARG is custom lowered with ELF 32 ABI
192 if (TM.getSubtarget<PPCSubtarget>().isELF32_ABI())
193 setOperationAction(ISD::VAARG, MVT::Other, Custom);
194 else
195 setOperationAction(ISD::VAARG, MVT::Other, Expand);
196
Chris Lattnerb22c08b2006-01-15 09:02:48 +0000197 // Use the default implementation.
Nate Begemanacc398c2006-01-25 18:21:52 +0000198 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
199 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Chris Lattnerb22c08b2006-01-15 09:02:48 +0000200 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
Jim Laskeyefc7e522006-12-04 22:04:42 +0000201 setOperationAction(ISD::STACKRESTORE , MVT::Other, Custom);
Jim Laskey2f616bf2006-11-16 22:43:37 +0000202 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
203 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64 , Custom);
Chris Lattner56a752e2006-10-18 01:18:48 +0000204
Chris Lattner6d92cad2006-03-26 10:06:40 +0000205 // We want to custom lower some of our intrinsics.
Chris Lattner48b61a72006-03-28 00:40:33 +0000206 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Chris Lattner6d92cad2006-03-26 10:06:40 +0000207
Chris Lattnera7a58542006-06-16 17:34:12 +0000208 if (TM.getSubtarget<PPCSubtarget>().has64BitSupport()) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000209 // They also have instructions for converting between i64 and fp.
Nate Begemanc09eeec2005-09-06 22:03:27 +0000210 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
Jim Laskeyca367b42006-12-15 14:32:57 +0000211 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000212 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Chris Lattner85c671b2006-12-07 01:24:16 +0000213 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Jim Laskeyca367b42006-12-15 14:32:57 +0000214 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
215
Chris Lattner7fbcef72006-03-24 07:53:47 +0000216 // FIXME: disable this lowered code. This generates 64-bit register values,
217 // and we don't model the fact that the top part is clobbered by calls. We
218 // need to flag these together so that the value isn't live across a call.
219 //setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
220
Nate Begemanae749a92005-10-25 23:48:36 +0000221 // To take advantage of the above i64 FP_TO_SINT, promote i32 FP_TO_UINT
222 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Promote);
223 } else {
Chris Lattner860e8862005-11-17 07:30:41 +0000224 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Nate Begemanae749a92005-10-25 23:48:36 +0000225 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begeman9d2b8172005-10-18 00:56:42 +0000226 }
227
Chris Lattnera7a58542006-06-16 17:34:12 +0000228 if (TM.getSubtarget<PPCSubtarget>().use64BitRegs()) {
Chris Lattner26cb2862007-10-19 04:08:28 +0000229 // 64-bit PowerPC implementations can support i64 types directly
Nate Begeman9d2b8172005-10-18 00:56:42 +0000230 addRegisterClass(MVT::i64, PPC::G8RCRegisterClass);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000231 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
232 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Dan Gohman9ed06db2008-03-07 20:36:53 +0000233 // 64-bit PowerPC wants to expand i128 shifts itself.
234 setOperationAction(ISD::SHL_PARTS, MVT::i64, Custom);
235 setOperationAction(ISD::SRA_PARTS, MVT::i64, Custom);
236 setOperationAction(ISD::SRL_PARTS, MVT::i64, Custom);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000237 } else {
Chris Lattner26cb2862007-10-19 04:08:28 +0000238 // 32-bit PowerPC wants to expand i64 shifts itself.
Chris Lattner3fe6c1d2006-09-20 03:47:40 +0000239 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
240 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
241 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000242 }
Evan Chengd30bf012006-03-01 01:11:20 +0000243
Nate Begeman425a9692005-11-29 08:17:20 +0000244 if (TM.getSubtarget<PPCSubtarget>().hasAltivec()) {
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000245 // First set operation action for all vector types to expand. Then we
246 // will selectively turn on ones that can be effectively codegen'd.
247 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
Dan Gohmanf5135be2007-05-18 23:21:46 +0000248 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Chris Lattnerf3f69de2006-04-16 01:37:57 +0000249 // add/sub are legal for all supported vector VT's.
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000250 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Legal);
251 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Legal);
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000252
Chris Lattner7ff7e672006-04-04 17:25:31 +0000253 // We promote all shuffles to v16i8.
254 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Promote);
Chris Lattnerf3f69de2006-04-16 01:37:57 +0000255 AddPromotedToType (ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, MVT::v16i8);
256
257 // We promote all non-typed operations to v4i32.
258 setOperationAction(ISD::AND , (MVT::ValueType)VT, Promote);
259 AddPromotedToType (ISD::AND , (MVT::ValueType)VT, MVT::v4i32);
260 setOperationAction(ISD::OR , (MVT::ValueType)VT, Promote);
261 AddPromotedToType (ISD::OR , (MVT::ValueType)VT, MVT::v4i32);
262 setOperationAction(ISD::XOR , (MVT::ValueType)VT, Promote);
263 AddPromotedToType (ISD::XOR , (MVT::ValueType)VT, MVT::v4i32);
264 setOperationAction(ISD::LOAD , (MVT::ValueType)VT, Promote);
265 AddPromotedToType (ISD::LOAD , (MVT::ValueType)VT, MVT::v4i32);
266 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
267 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v4i32);
268 setOperationAction(ISD::STORE, (MVT::ValueType)VT, Promote);
269 AddPromotedToType (ISD::STORE, (MVT::ValueType)VT, MVT::v4i32);
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000270
Chris Lattnerf3f69de2006-04-16 01:37:57 +0000271 // No other operations are legal.
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000272 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
273 setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand);
274 setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand);
275 setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand);
276 setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand);
Chris Lattner2ef5e892006-05-24 00:15:25 +0000277 setOperationAction(ISD::FDIV, (MVT::ValueType)VT, Expand);
Evan Cheng66ffe6b2007-07-30 07:51:22 +0000278 setOperationAction(ISD::FNEG, (MVT::ValueType)VT, Expand);
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000279 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
280 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
281 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Expand);
Dan Gohman3ce990d2007-10-08 17:28:24 +0000282 setOperationAction(ISD::UMUL_LOHI, (MVT::ValueType)VT, Expand);
283 setOperationAction(ISD::SMUL_LOHI, (MVT::ValueType)VT, Expand);
284 setOperationAction(ISD::UDIVREM, (MVT::ValueType)VT, Expand);
285 setOperationAction(ISD::SDIVREM, (MVT::ValueType)VT, Expand);
Chris Lattner01cae072006-04-03 23:55:43 +0000286 setOperationAction(ISD::SCALAR_TO_VECTOR, (MVT::ValueType)VT, Expand);
Dan Gohmana3f269f2007-10-12 14:08:57 +0000287 setOperationAction(ISD::FPOW, (MVT::ValueType)VT, Expand);
288 setOperationAction(ISD::CTPOP, (MVT::ValueType)VT, Expand);
289 setOperationAction(ISD::CTLZ, (MVT::ValueType)VT, Expand);
290 setOperationAction(ISD::CTTZ, (MVT::ValueType)VT, Expand);
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000291 }
292
Chris Lattner7ff7e672006-04-04 17:25:31 +0000293 // We can custom expand all VECTOR_SHUFFLEs to VPERM, others we can handle
294 // with merges, splats, etc.
295 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i8, Custom);
296
Chris Lattnerf3f69de2006-04-16 01:37:57 +0000297 setOperationAction(ISD::AND , MVT::v4i32, Legal);
298 setOperationAction(ISD::OR , MVT::v4i32, Legal);
299 setOperationAction(ISD::XOR , MVT::v4i32, Legal);
300 setOperationAction(ISD::LOAD , MVT::v4i32, Legal);
301 setOperationAction(ISD::SELECT, MVT::v4i32, Expand);
302 setOperationAction(ISD::STORE , MVT::v4i32, Legal);
303
Nate Begeman425a9692005-11-29 08:17:20 +0000304 addRegisterClass(MVT::v4f32, PPC::VRRCRegisterClass);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000305 addRegisterClass(MVT::v4i32, PPC::VRRCRegisterClass);
Chris Lattner8d052bc2006-03-25 07:39:07 +0000306 addRegisterClass(MVT::v8i16, PPC::VRRCRegisterClass);
307 addRegisterClass(MVT::v16i8, PPC::VRRCRegisterClass);
Chris Lattnerec4a0c72006-01-29 06:32:58 +0000308
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000309 setOperationAction(ISD::MUL, MVT::v4f32, Legal);
Chris Lattnere7c768e2006-04-18 03:24:30 +0000310 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
Chris Lattner72dd9bd2006-04-18 03:43:48 +0000311 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
Chris Lattner19a81522006-04-18 03:57:35 +0000312 setOperationAction(ISD::MUL, MVT::v16i8, Custom);
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000313
Chris Lattnerb2177b92006-03-19 06:55:52 +0000314 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom);
315 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Custom);
Chris Lattner64b3a082006-03-24 07:48:08 +0000316
Chris Lattner541f91b2006-04-02 00:43:36 +0000317 setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Custom);
318 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Custom);
Chris Lattner64b3a082006-03-24 07:48:08 +0000319 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Custom);
320 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
Nate Begeman425a9692005-11-29 08:17:20 +0000321 }
322
Chris Lattner7b0c58c2006-06-27 17:34:57 +0000323 setShiftAmountType(MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000324 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattner10da9572006-10-18 01:20:43 +0000325
Jim Laskey2ad9f172007-02-22 14:56:36 +0000326 if (TM.getSubtarget<PPCSubtarget>().isPPC64()) {
Chris Lattner10da9572006-10-18 01:20:43 +0000327 setStackPointerRegisterToSaveRestore(PPC::X1);
Jim Laskey2ad9f172007-02-22 14:56:36 +0000328 setExceptionPointerRegister(PPC::X3);
329 setExceptionSelectorRegister(PPC::X4);
330 } else {
Chris Lattner10da9572006-10-18 01:20:43 +0000331 setStackPointerRegisterToSaveRestore(PPC::R1);
Jim Laskey2ad9f172007-02-22 14:56:36 +0000332 setExceptionPointerRegister(PPC::R3);
333 setExceptionSelectorRegister(PPC::R4);
334 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000335
Chris Lattner8c13d0a2006-03-01 04:57:39 +0000336 // We have target-specific dag combine patterns for the following nodes:
337 setTargetDAGCombine(ISD::SINT_TO_FP);
Chris Lattner51269842006-03-01 05:50:56 +0000338 setTargetDAGCombine(ISD::STORE);
Chris Lattner90564f22006-04-18 17:59:36 +0000339 setTargetDAGCombine(ISD::BR_CC);
Chris Lattnerd9989382006-07-10 20:56:58 +0000340 setTargetDAGCombine(ISD::BSWAP);
Chris Lattner8c13d0a2006-03-01 04:57:39 +0000341
Dale Johannesenfabd32d2007-10-19 00:59:18 +0000342 // Darwin long double math library functions have $LDBL128 appended.
343 if (TM.getSubtarget<PPCSubtarget>().isDarwin()) {
Duncan Sands007f9842008-01-10 10:28:30 +0000344 setLibcallName(RTLIB::COS_PPCF128, "cosl$LDBL128");
Dale Johannesenfabd32d2007-10-19 00:59:18 +0000345 setLibcallName(RTLIB::POW_PPCF128, "powl$LDBL128");
346 setLibcallName(RTLIB::REM_PPCF128, "fmodl$LDBL128");
Duncan Sands007f9842008-01-10 10:28:30 +0000347 setLibcallName(RTLIB::SIN_PPCF128, "sinl$LDBL128");
348 setLibcallName(RTLIB::SQRT_PPCF128, "sqrtl$LDBL128");
Dale Johannesenfabd32d2007-10-19 00:59:18 +0000349 }
350
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000351 computeRegisterProperties();
352}
353
Dale Johannesen28d08fd2008-02-28 22:31:51 +0000354/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
355/// function arguments in the caller parameter area.
356unsigned PPCTargetLowering::getByValTypeAlignment(const Type *Ty) const {
357 TargetMachine &TM = getTargetMachine();
358 // Darwin passes everything on 4 byte boundary.
359 if (TM.getSubtarget<PPCSubtarget>().isDarwin())
360 return 4;
361 // FIXME Elf TBD
362 return 4;
363}
364
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000365const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
366 switch (Opcode) {
367 default: return 0;
368 case PPCISD::FSEL: return "PPCISD::FSEL";
369 case PPCISD::FCFID: return "PPCISD::FCFID";
370 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
371 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
Chris Lattner51269842006-03-01 05:50:56 +0000372 case PPCISD::STFIWX: return "PPCISD::STFIWX";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000373 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
374 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000375 case PPCISD::VPERM: return "PPCISD::VPERM";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000376 case PPCISD::Hi: return "PPCISD::Hi";
377 case PPCISD::Lo: return "PPCISD::Lo";
Jim Laskey2060a822006-12-11 18:45:56 +0000378 case PPCISD::DYNALLOC: return "PPCISD::DYNALLOC";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000379 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
380 case PPCISD::SRL: return "PPCISD::SRL";
381 case PPCISD::SRA: return "PPCISD::SRA";
382 case PPCISD::SHL: return "PPCISD::SHL";
Chris Lattnerecfe55e2006-03-22 05:30:33 +0000383 case PPCISD::EXTSW_32: return "PPCISD::EXTSW_32";
384 case PPCISD::STD_32: return "PPCISD::STD_32";
Nicolas Geoffray63f8fb12007-02-27 13:01:19 +0000385 case PPCISD::CALL_ELF: return "PPCISD::CALL_ELF";
386 case PPCISD::CALL_Macho: return "PPCISD::CALL_Macho";
Chris Lattnerc703a8f2006-05-17 19:00:46 +0000387 case PPCISD::MTCTR: return "PPCISD::MTCTR";
Chris Lattner9f0bc652007-02-25 05:34:32 +0000388 case PPCISD::BCTRL_Macho: return "PPCISD::BCTRL_Macho";
389 case PPCISD::BCTRL_ELF: return "PPCISD::BCTRL_ELF";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000390 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
Chris Lattner6d92cad2006-03-26 10:06:40 +0000391 case PPCISD::MFCR: return "PPCISD::MFCR";
Chris Lattnera17b1552006-03-31 05:13:27 +0000392 case PPCISD::VCMP: return "PPCISD::VCMP";
Chris Lattner6d92cad2006-03-26 10:06:40 +0000393 case PPCISD::VCMPo: return "PPCISD::VCMPo";
Chris Lattnerd9989382006-07-10 20:56:58 +0000394 case PPCISD::LBRX: return "PPCISD::LBRX";
395 case PPCISD::STBRX: return "PPCISD::STBRX";
Chris Lattnerf70f8d92006-04-18 18:05:58 +0000396 case PPCISD::COND_BRANCH: return "PPCISD::COND_BRANCH";
Chris Lattneref97c672008-01-18 18:51:16 +0000397 case PPCISD::MFFS: return "PPCISD::MFFS";
398 case PPCISD::MTFSB0: return "PPCISD::MTFSB0";
399 case PPCISD::MTFSB1: return "PPCISD::MTFSB1";
400 case PPCISD::FADDRTZ: return "PPCISD::FADDRTZ";
401 case PPCISD::MTFSF: return "PPCISD::MTFSF";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000402 }
403}
404
Scott Michel5b8f82e2008-03-10 15:42:14 +0000405
406MVT::ValueType
407PPCTargetLowering::getSetCCResultType(const SDOperand &) const {
408 return MVT::i32;
409}
410
411
Chris Lattner1a635d62006-04-14 06:01:58 +0000412//===----------------------------------------------------------------------===//
413// Node matching predicates, for use by the tblgen matching code.
414//===----------------------------------------------------------------------===//
415
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000416/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
417static bool isFloatingPointZero(SDOperand Op) {
418 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +0000419 return CFP->getValueAPF().isZero();
Evan Cheng466685d2006-10-09 20:57:25 +0000420 else if (ISD::isEXTLoad(Op.Val) || ISD::isNON_EXTLoad(Op.Val)) {
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000421 // Maybe this has already been legalized into the constant pool?
422 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
Evan Chengc356a572006-09-12 21:04:05 +0000423 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +0000424 return CFP->getValueAPF().isZero();
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000425 }
426 return false;
427}
428
Chris Lattnerddb739e2006-04-06 17:23:16 +0000429/// isConstantOrUndef - Op is either an undef node or a ConstantSDNode. Return
430/// true if Op is undef or if it matches the specified value.
431static bool isConstantOrUndef(SDOperand Op, unsigned Val) {
432 return Op.getOpcode() == ISD::UNDEF ||
433 cast<ConstantSDNode>(Op)->getValue() == Val;
434}
435
436/// isVPKUHUMShuffleMask - Return true if this is the shuffle mask for a
437/// VPKUHUM instruction.
Chris Lattnerf24380e2006-04-06 22:28:36 +0000438bool PPC::isVPKUHUMShuffleMask(SDNode *N, bool isUnary) {
439 if (!isUnary) {
440 for (unsigned i = 0; i != 16; ++i)
441 if (!isConstantOrUndef(N->getOperand(i), i*2+1))
442 return false;
443 } else {
444 for (unsigned i = 0; i != 8; ++i)
445 if (!isConstantOrUndef(N->getOperand(i), i*2+1) ||
446 !isConstantOrUndef(N->getOperand(i+8), i*2+1))
447 return false;
448 }
Chris Lattnerd0608e12006-04-06 18:26:28 +0000449 return true;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000450}
451
452/// isVPKUWUMShuffleMask - Return true if this is the shuffle mask for a
453/// VPKUWUM instruction.
Chris Lattnerf24380e2006-04-06 22:28:36 +0000454bool PPC::isVPKUWUMShuffleMask(SDNode *N, bool isUnary) {
455 if (!isUnary) {
456 for (unsigned i = 0; i != 16; i += 2)
457 if (!isConstantOrUndef(N->getOperand(i ), i*2+2) ||
458 !isConstantOrUndef(N->getOperand(i+1), i*2+3))
459 return false;
460 } else {
461 for (unsigned i = 0; i != 8; i += 2)
462 if (!isConstantOrUndef(N->getOperand(i ), i*2+2) ||
463 !isConstantOrUndef(N->getOperand(i+1), i*2+3) ||
464 !isConstantOrUndef(N->getOperand(i+8), i*2+2) ||
465 !isConstantOrUndef(N->getOperand(i+9), i*2+3))
466 return false;
467 }
Chris Lattnerd0608e12006-04-06 18:26:28 +0000468 return true;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000469}
470
Chris Lattnercaad1632006-04-06 22:02:42 +0000471/// isVMerge - Common function, used to match vmrg* shuffles.
472///
473static bool isVMerge(SDNode *N, unsigned UnitSize,
474 unsigned LHSStart, unsigned RHSStart) {
Chris Lattner116cc482006-04-06 21:11:54 +0000475 assert(N->getOpcode() == ISD::BUILD_VECTOR &&
476 N->getNumOperands() == 16 && "PPC only supports shuffles by bytes!");
477 assert((UnitSize == 1 || UnitSize == 2 || UnitSize == 4) &&
478 "Unsupported merge size!");
479
480 for (unsigned i = 0; i != 8/UnitSize; ++i) // Step over units
481 for (unsigned j = 0; j != UnitSize; ++j) { // Step over bytes within unit
482 if (!isConstantOrUndef(N->getOperand(i*UnitSize*2+j),
Chris Lattnercaad1632006-04-06 22:02:42 +0000483 LHSStart+j+i*UnitSize) ||
Chris Lattner116cc482006-04-06 21:11:54 +0000484 !isConstantOrUndef(N->getOperand(i*UnitSize*2+UnitSize+j),
Chris Lattnercaad1632006-04-06 22:02:42 +0000485 RHSStart+j+i*UnitSize))
Chris Lattner116cc482006-04-06 21:11:54 +0000486 return false;
487 }
Chris Lattnercaad1632006-04-06 22:02:42 +0000488 return true;
489}
490
491/// isVMRGLShuffleMask - Return true if this is a shuffle mask suitable for
492/// a VRGL* instruction with the specified unit size (1,2 or 4 bytes).
493bool PPC::isVMRGLShuffleMask(SDNode *N, unsigned UnitSize, bool isUnary) {
494 if (!isUnary)
495 return isVMerge(N, UnitSize, 8, 24);
496 return isVMerge(N, UnitSize, 8, 8);
Chris Lattner116cc482006-04-06 21:11:54 +0000497}
498
499/// isVMRGHShuffleMask - Return true if this is a shuffle mask suitable for
500/// a VRGH* instruction with the specified unit size (1,2 or 4 bytes).
Chris Lattnercaad1632006-04-06 22:02:42 +0000501bool PPC::isVMRGHShuffleMask(SDNode *N, unsigned UnitSize, bool isUnary) {
502 if (!isUnary)
503 return isVMerge(N, UnitSize, 0, 16);
504 return isVMerge(N, UnitSize, 0, 0);
Chris Lattner116cc482006-04-06 21:11:54 +0000505}
506
507
Chris Lattnerd0608e12006-04-06 18:26:28 +0000508/// isVSLDOIShuffleMask - If this is a vsldoi shuffle mask, return the shift
509/// amount, otherwise return -1.
Chris Lattnerf24380e2006-04-06 22:28:36 +0000510int PPC::isVSLDOIShuffleMask(SDNode *N, bool isUnary) {
Chris Lattner116cc482006-04-06 21:11:54 +0000511 assert(N->getOpcode() == ISD::BUILD_VECTOR &&
512 N->getNumOperands() == 16 && "PPC only supports shuffles by bytes!");
Chris Lattnerd0608e12006-04-06 18:26:28 +0000513 // Find the first non-undef value in the shuffle mask.
514 unsigned i;
515 for (i = 0; i != 16 && N->getOperand(i).getOpcode() == ISD::UNDEF; ++i)
516 /*search*/;
517
518 if (i == 16) return -1; // all undef.
519
520 // Otherwise, check to see if the rest of the elements are consequtively
521 // numbered from this value.
522 unsigned ShiftAmt = cast<ConstantSDNode>(N->getOperand(i))->getValue();
523 if (ShiftAmt < i) return -1;
524 ShiftAmt -= i;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000525
Chris Lattnerf24380e2006-04-06 22:28:36 +0000526 if (!isUnary) {
527 // Check the rest of the elements to see if they are consequtive.
528 for (++i; i != 16; ++i)
529 if (!isConstantOrUndef(N->getOperand(i), ShiftAmt+i))
530 return -1;
531 } else {
532 // Check the rest of the elements to see if they are consequtive.
533 for (++i; i != 16; ++i)
534 if (!isConstantOrUndef(N->getOperand(i), (ShiftAmt+i) & 15))
535 return -1;
536 }
Chris Lattnerd0608e12006-04-06 18:26:28 +0000537
538 return ShiftAmt;
539}
Chris Lattneref819f82006-03-20 06:33:01 +0000540
541/// isSplatShuffleMask - Return true if the specified VECTOR_SHUFFLE operand
542/// specifies a splat of a single element that is suitable for input to
543/// VSPLTB/VSPLTH/VSPLTW.
Chris Lattner7ff7e672006-04-04 17:25:31 +0000544bool PPC::isSplatShuffleMask(SDNode *N, unsigned EltSize) {
545 assert(N->getOpcode() == ISD::BUILD_VECTOR &&
546 N->getNumOperands() == 16 &&
547 (EltSize == 1 || EltSize == 2 || EltSize == 4));
Chris Lattnerdd4d2d02006-03-20 06:51:10 +0000548
Chris Lattner88a99ef2006-03-20 06:37:44 +0000549 // This is a splat operation if each element of the permute is the same, and
550 // if the value doesn't reference the second vector.
Chris Lattner7ff7e672006-04-04 17:25:31 +0000551 unsigned ElementBase = 0;
Chris Lattner88a99ef2006-03-20 06:37:44 +0000552 SDOperand Elt = N->getOperand(0);
Chris Lattner7ff7e672006-04-04 17:25:31 +0000553 if (ConstantSDNode *EltV = dyn_cast<ConstantSDNode>(Elt))
554 ElementBase = EltV->getValue();
555 else
556 return false; // FIXME: Handle UNDEF elements too!
557
558 if (cast<ConstantSDNode>(Elt)->getValue() >= 16)
559 return false;
560
561 // Check that they are consequtive.
562 for (unsigned i = 1; i != EltSize; ++i) {
563 if (!isa<ConstantSDNode>(N->getOperand(i)) ||
564 cast<ConstantSDNode>(N->getOperand(i))->getValue() != i+ElementBase)
565 return false;
566 }
567
Chris Lattner88a99ef2006-03-20 06:37:44 +0000568 assert(isa<ConstantSDNode>(Elt) && "Invalid VECTOR_SHUFFLE mask!");
Chris Lattner7ff7e672006-04-04 17:25:31 +0000569 for (unsigned i = EltSize, e = 16; i != e; i += EltSize) {
Chris Lattnerb097aa92006-04-14 23:19:08 +0000570 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Chris Lattner88a99ef2006-03-20 06:37:44 +0000571 assert(isa<ConstantSDNode>(N->getOperand(i)) &&
572 "Invalid VECTOR_SHUFFLE mask!");
Chris Lattner7ff7e672006-04-04 17:25:31 +0000573 for (unsigned j = 0; j != EltSize; ++j)
574 if (N->getOperand(i+j) != N->getOperand(j))
575 return false;
Chris Lattner88a99ef2006-03-20 06:37:44 +0000576 }
577
Chris Lattner7ff7e672006-04-04 17:25:31 +0000578 return true;
Chris Lattneref819f82006-03-20 06:33:01 +0000579}
580
Evan Cheng66ffe6b2007-07-30 07:51:22 +0000581/// isAllNegativeZeroVector - Returns true if all elements of build_vector
582/// are -0.0.
583bool PPC::isAllNegativeZeroVector(SDNode *N) {
584 assert(N->getOpcode() == ISD::BUILD_VECTOR);
585 if (PPC::isSplatShuffleMask(N, N->getNumOperands()))
586 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N))
Dale Johanneseneaf08942007-08-31 04:03:46 +0000587 return CFP->getValueAPF().isNegZero();
Evan Cheng66ffe6b2007-07-30 07:51:22 +0000588 return false;
589}
590
Chris Lattneref819f82006-03-20 06:33:01 +0000591/// getVSPLTImmediate - Return the appropriate VSPLT* immediate to splat the
592/// specified isSplatShuffleMask VECTOR_SHUFFLE mask.
Chris Lattner7ff7e672006-04-04 17:25:31 +0000593unsigned PPC::getVSPLTImmediate(SDNode *N, unsigned EltSize) {
594 assert(isSplatShuffleMask(N, EltSize));
595 return cast<ConstantSDNode>(N->getOperand(0))->getValue() / EltSize;
Chris Lattneref819f82006-03-20 06:33:01 +0000596}
597
Chris Lattnere87192a2006-04-12 17:37:20 +0000598/// get_VSPLTI_elt - If this is a build_vector of constants which can be formed
Chris Lattner140a58f2006-04-08 06:46:53 +0000599/// by using a vspltis[bhw] instruction of the specified element size, return
600/// the constant being splatted. The ByteSize field indicates the number of
601/// bytes of each element [124] -> [bhw].
Chris Lattnere87192a2006-04-12 17:37:20 +0000602SDOperand PPC::get_VSPLTI_elt(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000603 SDOperand OpVal(0, 0);
Chris Lattner79d9a882006-04-08 07:14:26 +0000604
605 // If ByteSize of the splat is bigger than the element size of the
606 // build_vector, then we have a case where we are checking for a splat where
607 // multiple elements of the buildvector are folded together into a single
608 // logical element of the splat (e.g. "vsplish 1" to splat {0,1}*8).
609 unsigned EltSize = 16/N->getNumOperands();
610 if (EltSize < ByteSize) {
611 unsigned Multiple = ByteSize/EltSize; // Number of BV entries per spltval.
612 SDOperand UniquedVals[4];
613 assert(Multiple > 1 && Multiple <= 4 && "How can this happen?");
614
615 // See if all of the elements in the buildvector agree across.
616 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
617 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
618 // If the element isn't a constant, bail fully out.
619 if (!isa<ConstantSDNode>(N->getOperand(i))) return SDOperand();
620
621
622 if (UniquedVals[i&(Multiple-1)].Val == 0)
623 UniquedVals[i&(Multiple-1)] = N->getOperand(i);
624 else if (UniquedVals[i&(Multiple-1)] != N->getOperand(i))
625 return SDOperand(); // no match.
626 }
627
628 // Okay, if we reached this point, UniquedVals[0..Multiple-1] contains
629 // either constant or undef values that are identical for each chunk. See
630 // if these chunks can form into a larger vspltis*.
631
632 // Check to see if all of the leading entries are either 0 or -1. If
633 // neither, then this won't fit into the immediate field.
634 bool LeadingZero = true;
635 bool LeadingOnes = true;
636 for (unsigned i = 0; i != Multiple-1; ++i) {
637 if (UniquedVals[i].Val == 0) continue; // Must have been undefs.
638
639 LeadingZero &= cast<ConstantSDNode>(UniquedVals[i])->isNullValue();
640 LeadingOnes &= cast<ConstantSDNode>(UniquedVals[i])->isAllOnesValue();
641 }
642 // Finally, check the least significant entry.
643 if (LeadingZero) {
644 if (UniquedVals[Multiple-1].Val == 0)
645 return DAG.getTargetConstant(0, MVT::i32); // 0,0,0,undef
646 int Val = cast<ConstantSDNode>(UniquedVals[Multiple-1])->getValue();
647 if (Val < 16)
648 return DAG.getTargetConstant(Val, MVT::i32); // 0,0,0,4 -> vspltisw(4)
649 }
650 if (LeadingOnes) {
651 if (UniquedVals[Multiple-1].Val == 0)
652 return DAG.getTargetConstant(~0U, MVT::i32); // -1,-1,-1,undef
653 int Val =cast<ConstantSDNode>(UniquedVals[Multiple-1])->getSignExtended();
654 if (Val >= -16) // -1,-1,-1,-2 -> vspltisw(-2)
655 return DAG.getTargetConstant(Val, MVT::i32);
656 }
657
658 return SDOperand();
659 }
660
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000661 // Check to see if this buildvec has a single non-undef value in its elements.
662 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
663 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
664 if (OpVal.Val == 0)
665 OpVal = N->getOperand(i);
666 else if (OpVal != N->getOperand(i))
Chris Lattner140a58f2006-04-08 06:46:53 +0000667 return SDOperand();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000668 }
669
Chris Lattner140a58f2006-04-08 06:46:53 +0000670 if (OpVal.Val == 0) return SDOperand(); // All UNDEF: use implicit def.
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000671
Nate Begeman98e70cc2006-03-28 04:15:58 +0000672 unsigned ValSizeInBytes = 0;
673 uint64_t Value = 0;
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000674 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
675 Value = CN->getValue();
676 ValSizeInBytes = MVT::getSizeInBits(CN->getValueType(0))/8;
677 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) {
678 assert(CN->getValueType(0) == MVT::f32 && "Only one legal FP vector type!");
Dale Johanneseneaf08942007-08-31 04:03:46 +0000679 Value = FloatToBits(CN->getValueAPF().convertToFloat());
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000680 ValSizeInBytes = 4;
681 }
682
683 // If the splat value is larger than the element value, then we can never do
684 // this splat. The only case that we could fit the replicated bits into our
685 // immediate field for would be zero, and we prefer to use vxor for it.
Chris Lattner140a58f2006-04-08 06:46:53 +0000686 if (ValSizeInBytes < ByteSize) return SDOperand();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000687
688 // If the element value is larger than the splat value, cut it in half and
689 // check to see if the two halves are equal. Continue doing this until we
690 // get to ByteSize. This allows us to handle 0x01010101 as 0x01.
691 while (ValSizeInBytes > ByteSize) {
692 ValSizeInBytes >>= 1;
693
694 // If the top half equals the bottom half, we're still ok.
Chris Lattner9b42bdd2006-04-05 17:39:25 +0000695 if (((Value >> (ValSizeInBytes*8)) & ((1 << (8*ValSizeInBytes))-1)) !=
696 (Value & ((1 << (8*ValSizeInBytes))-1)))
Chris Lattner140a58f2006-04-08 06:46:53 +0000697 return SDOperand();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000698 }
699
700 // Properly sign extend the value.
701 int ShAmt = (4-ByteSize)*8;
702 int MaskVal = ((int)Value << ShAmt) >> ShAmt;
703
Evan Cheng5b6a01b2006-03-26 09:52:32 +0000704 // If this is zero, don't match, zero matches ISD::isBuildVectorAllZeros.
Chris Lattner140a58f2006-04-08 06:46:53 +0000705 if (MaskVal == 0) return SDOperand();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000706
Chris Lattner140a58f2006-04-08 06:46:53 +0000707 // Finally, if this value fits in a 5 bit sext field, return it
708 if (((MaskVal << (32-5)) >> (32-5)) == MaskVal)
709 return DAG.getTargetConstant(MaskVal, MVT::i32);
710 return SDOperand();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000711}
712
Chris Lattner1a635d62006-04-14 06:01:58 +0000713//===----------------------------------------------------------------------===//
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000714// Addressing Mode Selection
715//===----------------------------------------------------------------------===//
716
717/// isIntS16Immediate - This method tests to see if the node is either a 32-bit
718/// or 64-bit immediate, and if the value can be accurately represented as a
719/// sign extension from a 16-bit value. If so, this returns true and the
720/// immediate.
721static bool isIntS16Immediate(SDNode *N, short &Imm) {
722 if (N->getOpcode() != ISD::Constant)
723 return false;
724
725 Imm = (short)cast<ConstantSDNode>(N)->getValue();
726 if (N->getValueType(0) == MVT::i32)
727 return Imm == (int32_t)cast<ConstantSDNode>(N)->getValue();
728 else
729 return Imm == (int64_t)cast<ConstantSDNode>(N)->getValue();
730}
731static bool isIntS16Immediate(SDOperand Op, short &Imm) {
732 return isIntS16Immediate(Op.Val, Imm);
733}
734
735
736/// SelectAddressRegReg - Given the specified addressed, check to see if it
737/// can be represented as an indexed [r+r] operation. Returns false if it
738/// can be more efficiently represented with [r+imm].
739bool PPCTargetLowering::SelectAddressRegReg(SDOperand N, SDOperand &Base,
740 SDOperand &Index,
741 SelectionDAG &DAG) {
742 short imm = 0;
743 if (N.getOpcode() == ISD::ADD) {
744 if (isIntS16Immediate(N.getOperand(1), imm))
745 return false; // r+i
746 if (N.getOperand(1).getOpcode() == PPCISD::Lo)
747 return false; // r+i
748
749 Base = N.getOperand(0);
750 Index = N.getOperand(1);
751 return true;
752 } else if (N.getOpcode() == ISD::OR) {
753 if (isIntS16Immediate(N.getOperand(1), imm))
754 return false; // r+i can fold it if we can.
755
756 // If this is an or of disjoint bitfields, we can codegen this as an add
757 // (for better address arithmetic) if the LHS and RHS of the OR are provably
758 // disjoint.
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000759 APInt LHSKnownZero, LHSKnownOne;
760 APInt RHSKnownZero, RHSKnownOne;
761 DAG.ComputeMaskedBits(N.getOperand(0),
Dan Gohmanec59b952008-02-27 21:12:32 +0000762 APInt::getAllOnesValue(N.getOperand(0)
763 .getValueSizeInBits()),
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000764 LHSKnownZero, LHSKnownOne);
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000765
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000766 if (LHSKnownZero.getBoolValue()) {
767 DAG.ComputeMaskedBits(N.getOperand(1),
Dan Gohmanec59b952008-02-27 21:12:32 +0000768 APInt::getAllOnesValue(N.getOperand(1)
769 .getValueSizeInBits()),
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000770 RHSKnownZero, RHSKnownOne);
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000771 // If all of the bits are known zero on the LHS or RHS, the add won't
772 // carry.
Dan Gohmanec59b952008-02-27 21:12:32 +0000773 if (~(LHSKnownZero | RHSKnownZero) == 0) {
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000774 Base = N.getOperand(0);
775 Index = N.getOperand(1);
776 return true;
777 }
778 }
779 }
780
781 return false;
782}
783
784/// Returns true if the address N can be represented by a base register plus
785/// a signed 16-bit displacement [r+imm], and if it is not better
786/// represented as reg+reg.
787bool PPCTargetLowering::SelectAddressRegImm(SDOperand N, SDOperand &Disp,
788 SDOperand &Base, SelectionDAG &DAG){
789 // If this can be more profitably realized as r+r, fail.
790 if (SelectAddressRegReg(N, Disp, Base, DAG))
791 return false;
792
793 if (N.getOpcode() == ISD::ADD) {
794 short imm = 0;
795 if (isIntS16Immediate(N.getOperand(1), imm)) {
796 Disp = DAG.getTargetConstant((int)imm & 0xFFFF, MVT::i32);
797 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
798 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
799 } else {
800 Base = N.getOperand(0);
801 }
802 return true; // [r+i]
803 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
804 // Match LOAD (ADD (X, Lo(G))).
805 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getValue()
806 && "Cannot handle constant offsets yet!");
807 Disp = N.getOperand(1).getOperand(0); // The global address.
808 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
809 Disp.getOpcode() == ISD::TargetConstantPool ||
810 Disp.getOpcode() == ISD::TargetJumpTable);
811 Base = N.getOperand(0);
812 return true; // [&g+r]
813 }
814 } else if (N.getOpcode() == ISD::OR) {
815 short imm = 0;
816 if (isIntS16Immediate(N.getOperand(1), imm)) {
817 // If this is an or of disjoint bitfields, we can codegen this as an add
818 // (for better address arithmetic) if the LHS and RHS of the OR are
819 // provably disjoint.
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000820 APInt LHSKnownZero, LHSKnownOne;
821 DAG.ComputeMaskedBits(N.getOperand(0),
Bill Wendling3e98c302008-03-24 23:16:37 +0000822 APInt::getAllOnesValue(N.getOperand(0)
823 .getValueSizeInBits()),
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000824 LHSKnownZero, LHSKnownOne);
Bill Wendling3e98c302008-03-24 23:16:37 +0000825
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000826 if ((LHSKnownZero.getZExtValue()|~(uint64_t)imm) == ~0ULL) {
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000827 // If all of the bits are known zero on the LHS or RHS, the add won't
828 // carry.
829 Base = N.getOperand(0);
830 Disp = DAG.getTargetConstant((int)imm & 0xFFFF, MVT::i32);
831 return true;
832 }
833 }
834 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
835 // Loading from a constant address.
836
837 // If this address fits entirely in a 16-bit sext immediate field, codegen
838 // this as "d, 0"
839 short Imm;
840 if (isIntS16Immediate(CN, Imm)) {
841 Disp = DAG.getTargetConstant(Imm, CN->getValueType(0));
842 Base = DAG.getRegister(PPC::R0, CN->getValueType(0));
843 return true;
844 }
Chris Lattnerbc681d62007-02-17 06:44:03 +0000845
846 // Handle 32-bit sext immediates with LIS + addr mode.
847 if (CN->getValueType(0) == MVT::i32 ||
848 (int64_t)CN->getValue() == (int)CN->getValue()) {
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000849 int Addr = (int)CN->getValue();
850
851 // Otherwise, break this down into an LIS + disp.
Chris Lattnerbc681d62007-02-17 06:44:03 +0000852 Disp = DAG.getTargetConstant((short)Addr, MVT::i32);
853
854 Base = DAG.getTargetConstant((Addr - (signed short)Addr) >> 16, MVT::i32);
855 unsigned Opc = CN->getValueType(0) == MVT::i32 ? PPC::LIS : PPC::LIS8;
856 Base = SDOperand(DAG.getTargetNode(Opc, CN->getValueType(0), Base), 0);
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000857 return true;
858 }
859 }
860
861 Disp = DAG.getTargetConstant(0, getPointerTy());
862 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
863 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
864 else
865 Base = N;
866 return true; // [r+0]
867}
868
869/// SelectAddressRegRegOnly - Given the specified addressed, force it to be
870/// represented as an indexed [r+r] operation.
871bool PPCTargetLowering::SelectAddressRegRegOnly(SDOperand N, SDOperand &Base,
872 SDOperand &Index,
873 SelectionDAG &DAG) {
874 // Check to see if we can easily represent this as an [r+r] address. This
875 // will fail if it thinks that the address is more profitably represented as
876 // reg+imm, e.g. where imm = 0.
877 if (SelectAddressRegReg(N, Base, Index, DAG))
878 return true;
879
880 // If the operand is an addition, always emit this as [r+r], since this is
881 // better (for code size, and execution, as the memop does the add for free)
882 // than emitting an explicit add.
883 if (N.getOpcode() == ISD::ADD) {
884 Base = N.getOperand(0);
885 Index = N.getOperand(1);
886 return true;
887 }
888
889 // Otherwise, do it the hard way, using R0 as the base register.
890 Base = DAG.getRegister(PPC::R0, N.getValueType());
891 Index = N;
892 return true;
893}
894
895/// SelectAddressRegImmShift - Returns true if the address N can be
896/// represented by a base register plus a signed 14-bit displacement
897/// [r+imm*4]. Suitable for use by STD and friends.
898bool PPCTargetLowering::SelectAddressRegImmShift(SDOperand N, SDOperand &Disp,
899 SDOperand &Base,
900 SelectionDAG &DAG) {
901 // If this can be more profitably realized as r+r, fail.
902 if (SelectAddressRegReg(N, Disp, Base, DAG))
903 return false;
904
905 if (N.getOpcode() == ISD::ADD) {
906 short imm = 0;
907 if (isIntS16Immediate(N.getOperand(1), imm) && (imm & 3) == 0) {
908 Disp = DAG.getTargetConstant(((int)imm & 0xFFFF) >> 2, MVT::i32);
909 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
910 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
911 } else {
912 Base = N.getOperand(0);
913 }
914 return true; // [r+i]
915 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
916 // Match LOAD (ADD (X, Lo(G))).
917 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getValue()
918 && "Cannot handle constant offsets yet!");
919 Disp = N.getOperand(1).getOperand(0); // The global address.
920 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
921 Disp.getOpcode() == ISD::TargetConstantPool ||
922 Disp.getOpcode() == ISD::TargetJumpTable);
923 Base = N.getOperand(0);
924 return true; // [&g+r]
925 }
926 } else if (N.getOpcode() == ISD::OR) {
927 short imm = 0;
928 if (isIntS16Immediate(N.getOperand(1), imm) && (imm & 3) == 0) {
929 // If this is an or of disjoint bitfields, we can codegen this as an add
930 // (for better address arithmetic) if the LHS and RHS of the OR are
931 // provably disjoint.
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000932 APInt LHSKnownZero, LHSKnownOne;
933 DAG.ComputeMaskedBits(N.getOperand(0),
Bill Wendling3e98c302008-03-24 23:16:37 +0000934 APInt::getAllOnesValue(N.getOperand(0)
935 .getValueSizeInBits()),
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000936 LHSKnownZero, LHSKnownOne);
937 if ((LHSKnownZero.getZExtValue()|~(uint64_t)imm) == ~0ULL) {
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000938 // If all of the bits are known zero on the LHS or RHS, the add won't
939 // carry.
940 Base = N.getOperand(0);
941 Disp = DAG.getTargetConstant(((int)imm & 0xFFFF) >> 2, MVT::i32);
942 return true;
943 }
944 }
945 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
Chris Lattnerdee5a5a2007-02-17 06:57:26 +0000946 // Loading from a constant address. Verify low two bits are clear.
947 if ((CN->getValue() & 3) == 0) {
948 // If this address fits entirely in a 14-bit sext immediate field, codegen
949 // this as "d, 0"
950 short Imm;
951 if (isIntS16Immediate(CN, Imm)) {
952 Disp = DAG.getTargetConstant((unsigned short)Imm >> 2, getPointerTy());
953 Base = DAG.getRegister(PPC::R0, CN->getValueType(0));
954 return true;
955 }
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000956
Chris Lattnerdee5a5a2007-02-17 06:57:26 +0000957 // Fold the low-part of 32-bit absolute addresses into addr mode.
958 if (CN->getValueType(0) == MVT::i32 ||
959 (int64_t)CN->getValue() == (int)CN->getValue()) {
960 int Addr = (int)CN->getValue();
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000961
Chris Lattnerdee5a5a2007-02-17 06:57:26 +0000962 // Otherwise, break this down into an LIS + disp.
963 Disp = DAG.getTargetConstant((short)Addr >> 2, MVT::i32);
964
965 Base = DAG.getTargetConstant((Addr-(signed short)Addr) >> 16, MVT::i32);
966 unsigned Opc = CN->getValueType(0) == MVT::i32 ? PPC::LIS : PPC::LIS8;
967 Base = SDOperand(DAG.getTargetNode(Opc, CN->getValueType(0), Base), 0);
968 return true;
969 }
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000970 }
971 }
972
973 Disp = DAG.getTargetConstant(0, getPointerTy());
974 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
975 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
976 else
977 Base = N;
978 return true; // [r+0]
979}
980
981
982/// getPreIndexedAddressParts - returns true by value, base pointer and
983/// offset pointer and addressing mode by reference if the node's address
984/// can be legally represented as pre-indexed load / store address.
985bool PPCTargetLowering::getPreIndexedAddressParts(SDNode *N, SDOperand &Base,
986 SDOperand &Offset,
Evan Cheng144d8f02006-11-09 17:55:04 +0000987 ISD::MemIndexedMode &AM,
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000988 SelectionDAG &DAG) {
Chris Lattner4eab7142006-11-10 02:08:47 +0000989 // Disabled by default for now.
990 if (!EnablePPCPreinc) return false;
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000991
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000992 SDOperand Ptr;
Chris Lattner2fe4bf42006-11-14 01:38:31 +0000993 MVT::ValueType VT;
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000994 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
995 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000996 VT = LD->getMemoryVT();
Chris Lattner0851b4f2006-11-15 19:55:13 +0000997
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000998 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattner4eab7142006-11-10 02:08:47 +0000999 ST = ST;
Chris Lattner2fe4bf42006-11-14 01:38:31 +00001000 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001001 VT = ST->getMemoryVT();
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001002 } else
1003 return false;
1004
Chris Lattner2fe4bf42006-11-14 01:38:31 +00001005 // PowerPC doesn't have preinc load/store instructions for vectors.
1006 if (MVT::isVector(VT))
1007 return false;
1008
Chris Lattner0851b4f2006-11-15 19:55:13 +00001009 // TODO: Check reg+reg first.
1010
1011 // LDU/STU use reg+imm*4, others use reg+imm.
1012 if (VT != MVT::i64) {
1013 // reg + imm
1014 if (!SelectAddressRegImm(Ptr, Offset, Base, DAG))
1015 return false;
1016 } else {
1017 // reg + imm * 4.
1018 if (!SelectAddressRegImmShift(Ptr, Offset, Base, DAG))
1019 return false;
1020 }
Chris Lattnerf6edf4d2006-11-11 00:08:42 +00001021
Chris Lattnerf6edf4d2006-11-11 00:08:42 +00001022 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattner0851b4f2006-11-15 19:55:13 +00001023 // PPC64 doesn't have lwau, but it does have lwaux. Reject preinc load of
1024 // sext i32 to i64 when addr mode is r+i.
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001025 if (LD->getValueType(0) == MVT::i64 && LD->getMemoryVT() == MVT::i32 &&
Chris Lattnerf6edf4d2006-11-11 00:08:42 +00001026 LD->getExtensionType() == ISD::SEXTLOAD &&
1027 isa<ConstantSDNode>(Offset))
1028 return false;
Chris Lattner0851b4f2006-11-15 19:55:13 +00001029 }
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001030
Chris Lattner4eab7142006-11-10 02:08:47 +00001031 AM = ISD::PRE_INC;
1032 return true;
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001033}
1034
1035//===----------------------------------------------------------------------===//
Chris Lattner1a635d62006-04-14 06:01:58 +00001036// LowerOperation implementation
1037//===----------------------------------------------------------------------===//
1038
Dale Johannesen5b3b6952008-03-04 23:17:14 +00001039SDOperand PPCTargetLowering::LowerConstantPool(SDOperand Op,
1040 SelectionDAG &DAG) {
Chris Lattner059ca0f2006-06-16 21:01:35 +00001041 MVT::ValueType PtrVT = Op.getValueType();
Chris Lattner1a635d62006-04-14 06:01:58 +00001042 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Chengc356a572006-09-12 21:04:05 +00001043 Constant *C = CP->getConstVal();
Chris Lattner059ca0f2006-06-16 21:01:35 +00001044 SDOperand CPI = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment());
1045 SDOperand Zero = DAG.getConstant(0, PtrVT);
Chris Lattner1a635d62006-04-14 06:01:58 +00001046
1047 const TargetMachine &TM = DAG.getTarget();
1048
Chris Lattner059ca0f2006-06-16 21:01:35 +00001049 SDOperand Hi = DAG.getNode(PPCISD::Hi, PtrVT, CPI, Zero);
1050 SDOperand Lo = DAG.getNode(PPCISD::Lo, PtrVT, CPI, Zero);
1051
Chris Lattner1a635d62006-04-14 06:01:58 +00001052 // If this is a non-darwin platform, we don't support non-static relo models
1053 // yet.
1054 if (TM.getRelocationModel() == Reloc::Static ||
1055 !TM.getSubtarget<PPCSubtarget>().isDarwin()) {
1056 // Generate non-pic code that has direct accesses to the constant pool.
1057 // The address of the global is just (hi(&g)+lo(&g)).
Chris Lattner059ca0f2006-06-16 21:01:35 +00001058 return DAG.getNode(ISD::ADD, PtrVT, Hi, Lo);
Chris Lattner1a635d62006-04-14 06:01:58 +00001059 }
1060
Chris Lattner35d86fe2006-07-26 21:12:04 +00001061 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattner1a635d62006-04-14 06:01:58 +00001062 // With PIC, the first instruction is actually "GR+hi(&G)".
Chris Lattner059ca0f2006-06-16 21:01:35 +00001063 Hi = DAG.getNode(ISD::ADD, PtrVT,
1064 DAG.getNode(PPCISD::GlobalBaseReg, PtrVT), Hi);
Chris Lattner1a635d62006-04-14 06:01:58 +00001065 }
1066
Chris Lattner059ca0f2006-06-16 21:01:35 +00001067 Lo = DAG.getNode(ISD::ADD, PtrVT, Hi, Lo);
Chris Lattner1a635d62006-04-14 06:01:58 +00001068 return Lo;
1069}
1070
Dale Johannesen5b3b6952008-03-04 23:17:14 +00001071SDOperand PPCTargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner059ca0f2006-06-16 21:01:35 +00001072 MVT::ValueType PtrVT = Op.getValueType();
Nate Begeman37efe672006-04-22 18:53:45 +00001073 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Chris Lattner059ca0f2006-06-16 21:01:35 +00001074 SDOperand JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
1075 SDOperand Zero = DAG.getConstant(0, PtrVT);
Nate Begeman37efe672006-04-22 18:53:45 +00001076
1077 const TargetMachine &TM = DAG.getTarget();
Chris Lattner059ca0f2006-06-16 21:01:35 +00001078
1079 SDOperand Hi = DAG.getNode(PPCISD::Hi, PtrVT, JTI, Zero);
1080 SDOperand Lo = DAG.getNode(PPCISD::Lo, PtrVT, JTI, Zero);
1081
Nate Begeman37efe672006-04-22 18:53:45 +00001082 // If this is a non-darwin platform, we don't support non-static relo models
1083 // yet.
1084 if (TM.getRelocationModel() == Reloc::Static ||
1085 !TM.getSubtarget<PPCSubtarget>().isDarwin()) {
1086 // Generate non-pic code that has direct accesses to the constant pool.
1087 // The address of the global is just (hi(&g)+lo(&g)).
Chris Lattner059ca0f2006-06-16 21:01:35 +00001088 return DAG.getNode(ISD::ADD, PtrVT, Hi, Lo);
Nate Begeman37efe672006-04-22 18:53:45 +00001089 }
1090
Chris Lattner35d86fe2006-07-26 21:12:04 +00001091 if (TM.getRelocationModel() == Reloc::PIC_) {
Nate Begeman37efe672006-04-22 18:53:45 +00001092 // With PIC, the first instruction is actually "GR+hi(&G)".
Chris Lattner059ca0f2006-06-16 21:01:35 +00001093 Hi = DAG.getNode(ISD::ADD, PtrVT,
Chris Lattner0d72a202006-07-28 16:45:47 +00001094 DAG.getNode(PPCISD::GlobalBaseReg, PtrVT), Hi);
Nate Begeman37efe672006-04-22 18:53:45 +00001095 }
1096
Chris Lattner059ca0f2006-06-16 21:01:35 +00001097 Lo = DAG.getNode(ISD::ADD, PtrVT, Hi, Lo);
Nate Begeman37efe672006-04-22 18:53:45 +00001098 return Lo;
1099}
1100
Dale Johannesen5b3b6952008-03-04 23:17:14 +00001101SDOperand PPCTargetLowering::LowerGlobalTLSAddress(SDOperand Op,
1102 SelectionDAG &DAG) {
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +00001103 assert(0 && "TLS not implemented for PPC.");
Chris Lattnerd27c9912008-03-30 18:22:13 +00001104 return SDOperand(); // Not reached
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +00001105}
1106
Dale Johannesen5b3b6952008-03-04 23:17:14 +00001107SDOperand PPCTargetLowering::LowerGlobalAddress(SDOperand Op,
1108 SelectionDAG &DAG) {
Chris Lattner059ca0f2006-06-16 21:01:35 +00001109 MVT::ValueType PtrVT = Op.getValueType();
Chris Lattner1a635d62006-04-14 06:01:58 +00001110 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
1111 GlobalValue *GV = GSDN->getGlobal();
Chris Lattner059ca0f2006-06-16 21:01:35 +00001112 SDOperand GA = DAG.getTargetGlobalAddress(GV, PtrVT, GSDN->getOffset());
Evan Chengfcf5d4f2008-02-02 05:06:29 +00001113 // If it's a debug information descriptor, don't mess with it.
1114 if (DAG.isVerifiedDebugInfoDesc(Op))
1115 return GA;
Chris Lattner059ca0f2006-06-16 21:01:35 +00001116 SDOperand Zero = DAG.getConstant(0, PtrVT);
Chris Lattner1a635d62006-04-14 06:01:58 +00001117
1118 const TargetMachine &TM = DAG.getTarget();
1119
Chris Lattner059ca0f2006-06-16 21:01:35 +00001120 SDOperand Hi = DAG.getNode(PPCISD::Hi, PtrVT, GA, Zero);
1121 SDOperand Lo = DAG.getNode(PPCISD::Lo, PtrVT, GA, Zero);
1122
Chris Lattner1a635d62006-04-14 06:01:58 +00001123 // If this is a non-darwin platform, we don't support non-static relo models
1124 // yet.
1125 if (TM.getRelocationModel() == Reloc::Static ||
1126 !TM.getSubtarget<PPCSubtarget>().isDarwin()) {
1127 // Generate non-pic code that has direct accesses to globals.
1128 // The address of the global is just (hi(&g)+lo(&g)).
Chris Lattner059ca0f2006-06-16 21:01:35 +00001129 return DAG.getNode(ISD::ADD, PtrVT, Hi, Lo);
Chris Lattner1a635d62006-04-14 06:01:58 +00001130 }
1131
Chris Lattner35d86fe2006-07-26 21:12:04 +00001132 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattner1a635d62006-04-14 06:01:58 +00001133 // With PIC, the first instruction is actually "GR+hi(&G)".
Chris Lattner059ca0f2006-06-16 21:01:35 +00001134 Hi = DAG.getNode(ISD::ADD, PtrVT,
1135 DAG.getNode(PPCISD::GlobalBaseReg, PtrVT), Hi);
Chris Lattner1a635d62006-04-14 06:01:58 +00001136 }
1137
Chris Lattner059ca0f2006-06-16 21:01:35 +00001138 Lo = DAG.getNode(ISD::ADD, PtrVT, Hi, Lo);
Chris Lattner1a635d62006-04-14 06:01:58 +00001139
Chris Lattner57fc62c2006-12-11 23:22:45 +00001140 if (!TM.getSubtarget<PPCSubtarget>().hasLazyResolverStub(GV))
Chris Lattner1a635d62006-04-14 06:01:58 +00001141 return Lo;
1142
1143 // If the global is weak or external, we have to go through the lazy
1144 // resolution stub.
Evan Cheng466685d2006-10-09 20:57:25 +00001145 return DAG.getLoad(PtrVT, DAG.getEntryNode(), Lo, NULL, 0);
Chris Lattner1a635d62006-04-14 06:01:58 +00001146}
1147
Dale Johannesen5b3b6952008-03-04 23:17:14 +00001148SDOperand PPCTargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner1a635d62006-04-14 06:01:58 +00001149 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
1150
1151 // If we're comparing for equality to zero, expose the fact that this is
1152 // implented as a ctlz/srl pair on ppc, so that the dag combiner can
1153 // fold the new nodes.
1154 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1155 if (C->isNullValue() && CC == ISD::SETEQ) {
1156 MVT::ValueType VT = Op.getOperand(0).getValueType();
1157 SDOperand Zext = Op.getOperand(0);
1158 if (VT < MVT::i32) {
1159 VT = MVT::i32;
1160 Zext = DAG.getNode(ISD::ZERO_EXTEND, VT, Op.getOperand(0));
1161 }
1162 unsigned Log2b = Log2_32(MVT::getSizeInBits(VT));
1163 SDOperand Clz = DAG.getNode(ISD::CTLZ, VT, Zext);
1164 SDOperand Scc = DAG.getNode(ISD::SRL, VT, Clz,
1165 DAG.getConstant(Log2b, MVT::i32));
1166 return DAG.getNode(ISD::TRUNCATE, MVT::i32, Scc);
1167 }
1168 // Leave comparisons against 0 and -1 alone for now, since they're usually
1169 // optimized. FIXME: revisit this when we can custom lower all setcc
1170 // optimizations.
1171 if (C->isAllOnesValue() || C->isNullValue())
1172 return SDOperand();
1173 }
1174
1175 // If we have an integer seteq/setne, turn it into a compare against zero
Chris Lattnerac011bc2006-11-14 05:28:08 +00001176 // by xor'ing the rhs with the lhs, which is faster than setting a
1177 // condition register, reading it back out, and masking the correct bit. The
1178 // normal approach here uses sub to do this instead of xor. Using xor exposes
1179 // the result to other bit-twiddling opportunities.
Chris Lattner1a635d62006-04-14 06:01:58 +00001180 MVT::ValueType LHSVT = Op.getOperand(0).getValueType();
1181 if (MVT::isInteger(LHSVT) && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
1182 MVT::ValueType VT = Op.getValueType();
Chris Lattnerac011bc2006-11-14 05:28:08 +00001183 SDOperand Sub = DAG.getNode(ISD::XOR, LHSVT, Op.getOperand(0),
Chris Lattner1a635d62006-04-14 06:01:58 +00001184 Op.getOperand(1));
1185 return DAG.getSetCC(VT, Sub, DAG.getConstant(0, LHSVT), CC);
1186 }
1187 return SDOperand();
1188}
1189
Dale Johannesen5b3b6952008-03-04 23:17:14 +00001190SDOperand PPCTargetLowering::LowerVAARG(SDOperand Op, SelectionDAG &DAG,
Nicolas Geoffray01119992007-04-03 13:59:52 +00001191 int VarArgsFrameIndex,
1192 int VarArgsStackOffset,
1193 unsigned VarArgsNumGPR,
1194 unsigned VarArgsNumFPR,
1195 const PPCSubtarget &Subtarget) {
1196
1197 assert(0 && "VAARG in ELF32 ABI not implemented yet!");
Chris Lattnerd27c9912008-03-30 18:22:13 +00001198 return SDOperand(); // Not reached
Nicolas Geoffray01119992007-04-03 13:59:52 +00001199}
1200
Dale Johannesen5b3b6952008-03-04 23:17:14 +00001201SDOperand PPCTargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG,
Nicolas Geoffray01119992007-04-03 13:59:52 +00001202 int VarArgsFrameIndex,
1203 int VarArgsStackOffset,
1204 unsigned VarArgsNumGPR,
1205 unsigned VarArgsNumFPR,
1206 const PPCSubtarget &Subtarget) {
1207
1208 if (Subtarget.isMachoABI()) {
1209 // vastart just stores the address of the VarArgsFrameIndex slot into the
1210 // memory location argument.
1211 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1212 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001213 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
1214 return DAG.getStore(Op.getOperand(0), FR, Op.getOperand(1), SV, 0);
Nicolas Geoffray01119992007-04-03 13:59:52 +00001215 }
1216
1217 // For ELF 32 ABI we follow the layout of the va_list struct.
1218 // We suppose the given va_list is already allocated.
1219 //
1220 // typedef struct {
1221 // char gpr; /* index into the array of 8 GPRs
1222 // * stored in the register save area
1223 // * gpr=0 corresponds to r3,
1224 // * gpr=1 to r4, etc.
1225 // */
1226 // char fpr; /* index into the array of 8 FPRs
1227 // * stored in the register save area
1228 // * fpr=0 corresponds to f1,
1229 // * fpr=1 to f2, etc.
1230 // */
1231 // char *overflow_arg_area;
1232 // /* location on stack that holds
1233 // * the next overflow argument
1234 // */
1235 // char *reg_save_area;
1236 // /* where r3:r10 and f1:f8 (if saved)
1237 // * are stored
1238 // */
1239 // } va_list[1];
1240
1241
1242 SDOperand ArgGPR = DAG.getConstant(VarArgsNumGPR, MVT::i8);
1243 SDOperand ArgFPR = DAG.getConstant(VarArgsNumFPR, MVT::i8);
1244
1245
Chris Lattner0d72a202006-07-28 16:45:47 +00001246 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Nicolas Geoffray01119992007-04-03 13:59:52 +00001247
Dan Gohman69de1932008-02-06 22:27:42 +00001248 SDOperand StackOffsetFI = DAG.getFrameIndex(VarArgsStackOffset, PtrVT);
Chris Lattner0d72a202006-07-28 16:45:47 +00001249 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
Nicolas Geoffray01119992007-04-03 13:59:52 +00001250
Dan Gohman69de1932008-02-06 22:27:42 +00001251 uint64_t FrameOffset = MVT::getSizeInBits(PtrVT)/8;
1252 SDOperand ConstFrameOffset = DAG.getConstant(FrameOffset, PtrVT);
1253
1254 uint64_t StackOffset = MVT::getSizeInBits(PtrVT)/8 - 1;
1255 SDOperand ConstStackOffset = DAG.getConstant(StackOffset, PtrVT);
1256
1257 uint64_t FPROffset = 1;
1258 SDOperand ConstFPROffset = DAG.getConstant(FPROffset, PtrVT);
Nicolas Geoffray01119992007-04-03 13:59:52 +00001259
Dan Gohman69de1932008-02-06 22:27:42 +00001260 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Nicolas Geoffray01119992007-04-03 13:59:52 +00001261
1262 // Store first byte : number of int regs
1263 SDOperand firstStore = DAG.getStore(Op.getOperand(0), ArgGPR,
Dan Gohman69de1932008-02-06 22:27:42 +00001264 Op.getOperand(1), SV, 0);
1265 uint64_t nextOffset = FPROffset;
Nicolas Geoffray01119992007-04-03 13:59:52 +00001266 SDOperand nextPtr = DAG.getNode(ISD::ADD, PtrVT, Op.getOperand(1),
1267 ConstFPROffset);
1268
1269 // Store second byte : number of float regs
Dan Gohman69de1932008-02-06 22:27:42 +00001270 SDOperand secondStore =
1271 DAG.getStore(firstStore, ArgFPR, nextPtr, SV, nextOffset);
1272 nextOffset += StackOffset;
Nicolas Geoffray01119992007-04-03 13:59:52 +00001273 nextPtr = DAG.getNode(ISD::ADD, PtrVT, nextPtr, ConstStackOffset);
1274
1275 // Store second word : arguments given on stack
Dan Gohman69de1932008-02-06 22:27:42 +00001276 SDOperand thirdStore =
1277 DAG.getStore(secondStore, StackOffsetFI, nextPtr, SV, nextOffset);
1278 nextOffset += FrameOffset;
Nicolas Geoffray01119992007-04-03 13:59:52 +00001279 nextPtr = DAG.getNode(ISD::ADD, PtrVT, nextPtr, ConstFrameOffset);
1280
1281 // Store third word : arguments given in registers
Dan Gohman69de1932008-02-06 22:27:42 +00001282 return DAG.getStore(thirdStore, FR, nextPtr, SV, nextOffset);
Nicolas Geoffray01119992007-04-03 13:59:52 +00001283
Chris Lattner1a635d62006-04-14 06:01:58 +00001284}
1285
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00001286#include "PPCGenCallingConv.inc"
1287
Chris Lattner9f0bc652007-02-25 05:34:32 +00001288/// GetFPR - Get the set of FP registers that should be allocated for arguments,
1289/// depending on which subtarget is selected.
1290static const unsigned *GetFPR(const PPCSubtarget &Subtarget) {
1291 if (Subtarget.isMachoABI()) {
1292 static const unsigned FPR[] = {
1293 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1294 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1295 };
1296 return FPR;
1297 }
1298
1299
1300 static const unsigned FPR[] = {
1301 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
Nicolas Geoffrayef3c0302007-04-03 10:27:07 +00001302 PPC::F8
Chris Lattner9f0bc652007-02-25 05:34:32 +00001303 };
1304 return FPR;
1305}
1306
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00001307SDOperand
1308PPCTargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op,
1309 SelectionDAG &DAG,
1310 int &VarArgsFrameIndex,
1311 int &VarArgsStackOffset,
1312 unsigned &VarArgsNumGPR,
1313 unsigned &VarArgsNumFPR,
1314 const PPCSubtarget &Subtarget) {
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001315 // TODO: add description of PPC stack frame format, or at least some docs.
1316 //
1317 MachineFunction &MF = DAG.getMachineFunction();
1318 MachineFrameInfo *MFI = MF.getFrameInfo();
Chris Lattner84bc5422007-12-31 04:13:23 +00001319 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Chris Lattner79e490a2006-08-11 17:18:05 +00001320 SmallVector<SDOperand, 8> ArgValues;
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001321 SDOperand Root = Op.getOperand(0);
Dale Johannesen75092de2008-03-12 00:22:17 +00001322 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001323
Jim Laskey2f616bf2006-11-16 22:43:37 +00001324 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1325 bool isPPC64 = PtrVT == MVT::i64;
Chris Lattner9f0bc652007-02-25 05:34:32 +00001326 bool isMachoABI = Subtarget.isMachoABI();
Nicolas Geoffrayec58d9f2007-04-03 12:35:28 +00001327 bool isELF32_ABI = Subtarget.isELF32_ABI();
Jim Laskeye9bd7b22006-11-28 14:53:52 +00001328 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Jim Laskey2f616bf2006-11-16 22:43:37 +00001329
Chris Lattner9f0bc652007-02-25 05:34:32 +00001330 unsigned ArgOffset = PPCFrameInfo::getLinkageSize(isPPC64, isMachoABI);
Chris Lattnerc91a4752006-06-26 22:48:35 +00001331
1332 static const unsigned GPR_32[] = { // 32-bit registers.
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001333 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1334 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1335 };
Chris Lattnerc91a4752006-06-26 22:48:35 +00001336 static const unsigned GPR_64[] = { // 64-bit registers.
1337 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
1338 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
1339 };
Chris Lattner9f0bc652007-02-25 05:34:32 +00001340
1341 static const unsigned *FPR = GetFPR(Subtarget);
1342
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001343 static const unsigned VR[] = {
1344 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
1345 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
1346 };
Chris Lattnerc91a4752006-06-26 22:48:35 +00001347
Owen Anderson718cb662007-09-07 04:06:50 +00001348 const unsigned Num_GPR_Regs = array_lengthof(GPR_32);
Nicolas Geoffrayef3c0302007-04-03 10:27:07 +00001349 const unsigned Num_FPR_Regs = isMachoABI ? 13 : 8;
Owen Anderson718cb662007-09-07 04:06:50 +00001350 const unsigned Num_VR_Regs = array_lengthof( VR);
Jim Laskey2f616bf2006-11-16 22:43:37 +00001351
1352 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
1353
Chris Lattnerc91a4752006-06-26 22:48:35 +00001354 const unsigned *GPR = isPPC64 ? GPR_64 : GPR_32;
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001355
Dale Johannesen8f5422c2008-03-14 17:41:26 +00001356 // In 32-bit non-varargs functions, the stack space for vectors is after the
1357 // stack space for non-vectors. We do not use this space unless we have
1358 // too many vectors to fit in registers, something that only occurs in
1359 // constructed examples:), but we have to walk the arglist to figure
1360 // that out...for the pathological case, compute VecArgOffset as the
1361 // start of the vector parameter area. Computing VecArgOffset is the
1362 // entire point of the following loop.
1363 // Altivec is not mentioned in the ppc32 Elf Supplement, so I'm not trying
1364 // to handle Elf here.
1365 unsigned VecArgOffset = ArgOffset;
1366 if (!isVarArg && !isPPC64) {
1367 for (unsigned ArgNo = 0, e = Op.Val->getNumValues()-1; ArgNo != e;
1368 ++ArgNo) {
1369 MVT::ValueType ObjectVT = Op.getValue(ArgNo).getValueType();
1370 unsigned ObjSize = MVT::getSizeInBits(ObjectVT)/8;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001371 ISD::ArgFlagsTy Flags =
1372 cast<ARG_FLAGSSDNode>(Op.getOperand(ArgNo+3))->getArgFlags();
Dale Johannesen8f5422c2008-03-14 17:41:26 +00001373
Duncan Sands276dcbd2008-03-21 09:14:45 +00001374 if (Flags.isByVal()) {
Dale Johannesen8f5422c2008-03-14 17:41:26 +00001375 // ObjSize is the true size, ArgSize rounded up to multiple of regs.
Duncan Sands276dcbd2008-03-21 09:14:45 +00001376 ObjSize = Flags.getByValSize();
Dale Johannesen8f5422c2008-03-14 17:41:26 +00001377 unsigned ArgSize =
1378 ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
1379 VecArgOffset += ArgSize;
1380 continue;
1381 }
1382
1383 switch(ObjectVT) {
1384 default: assert(0 && "Unhandled argument type!");
1385 case MVT::i32:
1386 case MVT::f32:
1387 VecArgOffset += isPPC64 ? 8 : 4;
1388 break;
1389 case MVT::i64: // PPC64
1390 case MVT::f64:
1391 VecArgOffset += 8;
1392 break;
1393 case MVT::v4f32:
1394 case MVT::v4i32:
1395 case MVT::v8i16:
1396 case MVT::v16i8:
1397 // Nothing to do, we're only looking at Nonvector args here.
1398 break;
1399 }
1400 }
1401 }
1402 // We've found where the vector parameter area in memory is. Skip the
1403 // first 12 parameters; these don't use that memory.
1404 VecArgOffset = ((VecArgOffset+15)/16)*16;
1405 VecArgOffset += 12*16;
1406
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001407 // Add DAG nodes to load the arguments or copy them out of registers. On
Jim Laskey2f616bf2006-11-16 22:43:37 +00001408 // entry to a function on PPC, the arguments start after the linkage area,
1409 // although the first ones are often in registers.
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00001410 //
Nicolas Geoffrayec58d9f2007-04-03 12:35:28 +00001411 // In the ELF 32 ABI, GPRs and stack are double word align: an argument
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00001412 // represented with two words (long long or double) must be copied to an
Nicolas Geoffrayc0cb28f2008-04-13 13:40:22 +00001413 // even GPR_idx value or to an even ArgOffset value.
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00001414
Dale Johannesen8419dd62008-03-07 20:27:40 +00001415 SmallVector<SDOperand, 8> MemOps;
1416
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001417 for (unsigned ArgNo = 0, e = Op.Val->getNumValues()-1; ArgNo != e; ++ArgNo) {
1418 SDOperand ArgVal;
1419 bool needsLoad = false;
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001420 MVT::ValueType ObjectVT = Op.getValue(ArgNo).getValueType();
1421 unsigned ObjSize = MVT::getSizeInBits(ObjectVT)/8;
Jim Laskey619965d2006-11-29 13:37:09 +00001422 unsigned ArgSize = ObjSize;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001423 ISD::ArgFlagsTy Flags =
1424 cast<ARG_FLAGSSDNode>(Op.getOperand(ArgNo+3))->getArgFlags();
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00001425 // See if next argument requires stack alignment in ELF
Nicolas Geoffray6ccbbd82008-04-15 08:08:50 +00001426 bool Align = Flags.isSplit();
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001427
Chris Lattnerbe4849a2006-05-16 18:51:52 +00001428 unsigned CurArgOffset = ArgOffset;
Dale Johannesen8419dd62008-03-07 20:27:40 +00001429
1430 // FIXME alignment for ELF may not be right
1431 // FIXME the codegen can be much improved in some cases.
1432 // We do not have to keep everything in memory.
Duncan Sands276dcbd2008-03-21 09:14:45 +00001433 if (Flags.isByVal()) {
Dale Johannesen8419dd62008-03-07 20:27:40 +00001434 // ObjSize is the true size, ArgSize rounded up to multiple of registers.
Duncan Sands276dcbd2008-03-21 09:14:45 +00001435 ObjSize = Flags.getByValSize();
Dale Johannesen8419dd62008-03-07 20:27:40 +00001436 ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Dale Johannesen7f96f392008-03-08 01:41:42 +00001437 // Double word align in ELF
Nicolas Geoffrayc0cb28f2008-04-13 13:40:22 +00001438 if (Align && isELF32_ABI) GPR_idx += (GPR_idx % 2);
Dale Johannesen7f96f392008-03-08 01:41:42 +00001439 // Objects of size 1 and 2 are right justified, everything else is
1440 // left justified. This means the memory address is adjusted forwards.
1441 if (ObjSize==1 || ObjSize==2) {
1442 CurArgOffset = CurArgOffset + (4 - ObjSize);
1443 }
Dale Johannesen8419dd62008-03-07 20:27:40 +00001444 // The value of the object is its address.
1445 int FI = MFI->CreateFixedObject(ObjSize, CurArgOffset);
1446 SDOperand FIN = DAG.getFrameIndex(FI, PtrVT);
1447 ArgValues.push_back(FIN);
Dale Johannesen7f96f392008-03-08 01:41:42 +00001448 if (ObjSize==1 || ObjSize==2) {
1449 if (GPR_idx != Num_GPR_Regs) {
1450 unsigned VReg = RegInfo.createVirtualRegister(&PPC::GPRCRegClass);
1451 RegInfo.addLiveIn(GPR[GPR_idx], VReg);
1452 SDOperand Val = DAG.getCopyFromReg(Root, VReg, PtrVT);
1453 SDOperand Store = DAG.getTruncStore(Val.getValue(1), Val, FIN,
1454 NULL, 0, ObjSize==1 ? MVT::i8 : MVT::i16 );
1455 MemOps.push_back(Store);
1456 ++GPR_idx;
1457 if (isMachoABI) ArgOffset += PtrByteSize;
1458 } else {
1459 ArgOffset += PtrByteSize;
1460 }
1461 continue;
1462 }
Dale Johannesen8419dd62008-03-07 20:27:40 +00001463 for (unsigned j = 0; j < ArgSize; j += PtrByteSize) {
1464 // Store whatever pieces of the object are in registers
1465 // to memory. ArgVal will be address of the beginning of
1466 // the object.
1467 if (GPR_idx != Num_GPR_Regs) {
1468 unsigned VReg = RegInfo.createVirtualRegister(&PPC::GPRCRegClass);
1469 RegInfo.addLiveIn(GPR[GPR_idx], VReg);
1470 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset);
1471 SDOperand FIN = DAG.getFrameIndex(FI, PtrVT);
1472 SDOperand Val = DAG.getCopyFromReg(Root, VReg, PtrVT);
1473 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1474 MemOps.push_back(Store);
1475 ++GPR_idx;
1476 if (isMachoABI) ArgOffset += PtrByteSize;
1477 } else {
1478 ArgOffset += ArgSize - (ArgOffset-CurArgOffset);
1479 break;
1480 }
1481 }
1482 continue;
1483 }
1484
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001485 switch (ObjectVT) {
1486 default: assert(0 && "Unhandled argument type!");
1487 case MVT::i32:
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00001488 if (!isPPC64) {
1489 // Double word align in ELF
Nicolas Geoffrayc0cb28f2008-04-13 13:40:22 +00001490 if (Align && isELF32_ABI) GPR_idx += (GPR_idx % 2);
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00001491
1492 if (GPR_idx != Num_GPR_Regs) {
1493 unsigned VReg = RegInfo.createVirtualRegister(&PPC::GPRCRegClass);
1494 RegInfo.addLiveIn(GPR[GPR_idx], VReg);
1495 ArgVal = DAG.getCopyFromReg(Root, VReg, MVT::i32);
1496 ++GPR_idx;
1497 } else {
1498 needsLoad = true;
1499 ArgSize = PtrByteSize;
1500 }
1501 // Stack align in ELF
Nicolas Geoffrayc0cb28f2008-04-13 13:40:22 +00001502 if (needsLoad && Align && isELF32_ABI)
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00001503 ArgOffset += ((ArgOffset/4) % 2) * PtrByteSize;
1504 // All int arguments reserve stack space in Macho ABI.
1505 if (isMachoABI || needsLoad) ArgOffset += PtrByteSize;
1506 break;
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001507 }
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00001508 // FALLTHROUGH
Chris Lattner9f0bc652007-02-25 05:34:32 +00001509 case MVT::i64: // PPC64
Chris Lattnerc91a4752006-06-26 22:48:35 +00001510 if (GPR_idx != Num_GPR_Regs) {
Chris Lattner84bc5422007-12-31 04:13:23 +00001511 unsigned VReg = RegInfo.createVirtualRegister(&PPC::G8RCRegClass);
1512 RegInfo.addLiveIn(GPR[GPR_idx], VReg);
Chris Lattnerc91a4752006-06-26 22:48:35 +00001513 ArgVal = DAG.getCopyFromReg(Root, VReg, MVT::i64);
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00001514
1515 if (ObjectVT == MVT::i32) {
1516 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote
1517 // value to MVT::i64 and then truncate to the correct register size.
Duncan Sands276dcbd2008-03-21 09:14:45 +00001518 if (Flags.isSExt())
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00001519 ArgVal = DAG.getNode(ISD::AssertSext, MVT::i64, ArgVal,
1520 DAG.getValueType(ObjectVT));
Duncan Sands276dcbd2008-03-21 09:14:45 +00001521 else if (Flags.isZExt())
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00001522 ArgVal = DAG.getNode(ISD::AssertZext, MVT::i64, ArgVal,
1523 DAG.getValueType(ObjectVT));
1524
1525 ArgVal = DAG.getNode(ISD::TRUNCATE, MVT::i32, ArgVal);
1526 }
1527
Chris Lattnerc91a4752006-06-26 22:48:35 +00001528 ++GPR_idx;
1529 } else {
1530 needsLoad = true;
1531 }
Chris Lattner9f0bc652007-02-25 05:34:32 +00001532 // All int arguments reserve stack space in Macho ABI.
1533 if (isMachoABI || needsLoad) ArgOffset += 8;
Chris Lattnerc91a4752006-06-26 22:48:35 +00001534 break;
Chris Lattner9f0bc652007-02-25 05:34:32 +00001535
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001536 case MVT::f32:
1537 case MVT::f64:
Chris Lattnerbe4849a2006-05-16 18:51:52 +00001538 // Every 4 bytes of argument space consumes one of the GPRs available for
1539 // argument passing.
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00001540 if (GPR_idx != Num_GPR_Regs && isMachoABI) {
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00001541 ++GPR_idx;
Chris Lattnerb1eb9872006-11-18 01:57:19 +00001542 if (ObjSize == 8 && GPR_idx != Num_GPR_Regs && !isPPC64)
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00001543 ++GPR_idx;
Chris Lattnerbe4849a2006-05-16 18:51:52 +00001544 }
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00001545 if (FPR_idx != Num_FPR_Regs) {
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001546 unsigned VReg;
1547 if (ObjectVT == MVT::f32)
Chris Lattner84bc5422007-12-31 04:13:23 +00001548 VReg = RegInfo.createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001549 else
Chris Lattner84bc5422007-12-31 04:13:23 +00001550 VReg = RegInfo.createVirtualRegister(&PPC::F8RCRegClass);
1551 RegInfo.addLiveIn(FPR[FPR_idx], VReg);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001552 ArgVal = DAG.getCopyFromReg(Root, VReg, ObjectVT);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001553 ++FPR_idx;
1554 } else {
1555 needsLoad = true;
1556 }
Chris Lattner9f0bc652007-02-25 05:34:32 +00001557
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00001558 // Stack align in ELF
Nicolas Geoffrayc0cb28f2008-04-13 13:40:22 +00001559 if (needsLoad && Align && isELF32_ABI)
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00001560 ArgOffset += ((ArgOffset/4) % 2) * PtrByteSize;
Chris Lattner9f0bc652007-02-25 05:34:32 +00001561 // All FP arguments reserve stack space in Macho ABI.
1562 if (isMachoABI || needsLoad) ArgOffset += isPPC64 ? 8 : ObjSize;
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001563 break;
1564 case MVT::v4f32:
1565 case MVT::v4i32:
1566 case MVT::v8i16:
1567 case MVT::v16i8:
Dale Johannesen75092de2008-03-12 00:22:17 +00001568 // Note that vector arguments in registers don't reserve stack space,
1569 // except in varargs functions.
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00001570 if (VR_idx != Num_VR_Regs) {
Chris Lattner84bc5422007-12-31 04:13:23 +00001571 unsigned VReg = RegInfo.createVirtualRegister(&PPC::VRRCRegClass);
1572 RegInfo.addLiveIn(VR[VR_idx], VReg);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001573 ArgVal = DAG.getCopyFromReg(Root, VReg, ObjectVT);
Dale Johannesen75092de2008-03-12 00:22:17 +00001574 if (isVarArg) {
1575 while ((ArgOffset % 16) != 0) {
1576 ArgOffset += PtrByteSize;
1577 if (GPR_idx != Num_GPR_Regs)
1578 GPR_idx++;
1579 }
1580 ArgOffset += 16;
1581 GPR_idx = std::min(GPR_idx+4, Num_GPR_Regs);
1582 }
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001583 ++VR_idx;
1584 } else {
Dale Johannesen8f5422c2008-03-14 17:41:26 +00001585 if (!isVarArg && !isPPC64) {
1586 // Vectors go after all the nonvectors.
1587 CurArgOffset = VecArgOffset;
1588 VecArgOffset += 16;
1589 } else {
1590 // Vectors are aligned.
1591 ArgOffset = ((ArgOffset+15)/16)*16;
1592 CurArgOffset = ArgOffset;
1593 ArgOffset += 16;
Dale Johannesen404d9902008-03-12 00:49:20 +00001594 }
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001595 needsLoad = true;
1596 }
1597 break;
1598 }
1599
1600 // We need to load the argument to a virtual register if we determined above
Chris Lattner9f72d1a2008-02-13 07:35:30 +00001601 // that we ran out of physical registers of the appropriate type.
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001602 if (needsLoad) {
Chris Lattner9f72d1a2008-02-13 07:35:30 +00001603 int FI = MFI->CreateFixedObject(ObjSize,
1604 CurArgOffset + (ArgSize - ObjSize));
1605 SDOperand FIN = DAG.getFrameIndex(FI, PtrVT);
1606 ArgVal = DAG.getLoad(ObjectVT, Root, FIN, NULL, 0);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001607 }
1608
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001609 ArgValues.push_back(ArgVal);
1610 }
Dale Johannesen8419dd62008-03-07 20:27:40 +00001611
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001612 // If the function takes variable number of arguments, make a frame index for
1613 // the start of the first vararg value... for expansion of llvm.va_start.
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001614 if (isVarArg) {
Nicolas Geoffray01119992007-04-03 13:59:52 +00001615
1616 int depth;
1617 if (isELF32_ABI) {
1618 VarArgsNumGPR = GPR_idx;
1619 VarArgsNumFPR = FPR_idx;
1620
1621 // Make room for Num_GPR_Regs, Num_FPR_Regs and for a possible frame
1622 // pointer.
1623 depth = -(Num_GPR_Regs * MVT::getSizeInBits(PtrVT)/8 +
1624 Num_FPR_Regs * MVT::getSizeInBits(MVT::f64)/8 +
1625 MVT::getSizeInBits(PtrVT)/8);
1626
1627 VarArgsStackOffset = MFI->CreateFixedObject(MVT::getSizeInBits(PtrVT)/8,
1628 ArgOffset);
1629
1630 }
1631 else
1632 depth = ArgOffset;
1633
Chris Lattnerc91a4752006-06-26 22:48:35 +00001634 VarArgsFrameIndex = MFI->CreateFixedObject(MVT::getSizeInBits(PtrVT)/8,
Nicolas Geoffray01119992007-04-03 13:59:52 +00001635 depth);
Chris Lattnerc91a4752006-06-26 22:48:35 +00001636 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
Nicolas Geoffray01119992007-04-03 13:59:52 +00001637
Nicolas Geoffray01119992007-04-03 13:59:52 +00001638 // In ELF 32 ABI, the fixed integer arguments of a variadic function are
1639 // stored to the VarArgsFrameIndex on the stack.
1640 if (isELF32_ABI) {
1641 for (GPR_idx = 0; GPR_idx != VarArgsNumGPR; ++GPR_idx) {
1642 SDOperand Val = DAG.getRegister(GPR[GPR_idx], PtrVT);
1643 SDOperand Store = DAG.getStore(Root, Val, FIN, NULL, 0);
1644 MemOps.push_back(Store);
1645 // Increment the address by four for the next argument to store
1646 SDOperand PtrOff = DAG.getConstant(MVT::getSizeInBits(PtrVT)/8, PtrVT);
1647 FIN = DAG.getNode(ISD::ADD, PtrOff.getValueType(), FIN, PtrOff);
1648 }
1649 }
1650
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001651 // If this function is vararg, store any remaining integer argument regs
1652 // to their spots on the stack so that they may be loaded by deferencing the
1653 // result of va_next.
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00001654 for (; GPR_idx != Num_GPR_Regs; ++GPR_idx) {
Chris Lattnerb1eb9872006-11-18 01:57:19 +00001655 unsigned VReg;
1656 if (isPPC64)
Chris Lattner84bc5422007-12-31 04:13:23 +00001657 VReg = RegInfo.createVirtualRegister(&PPC::G8RCRegClass);
Chris Lattnerb1eb9872006-11-18 01:57:19 +00001658 else
Chris Lattner84bc5422007-12-31 04:13:23 +00001659 VReg = RegInfo.createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattnerb1eb9872006-11-18 01:57:19 +00001660
Chris Lattner84bc5422007-12-31 04:13:23 +00001661 RegInfo.addLiveIn(GPR[GPR_idx], VReg);
Chris Lattnerc91a4752006-06-26 22:48:35 +00001662 SDOperand Val = DAG.getCopyFromReg(Root, VReg, PtrVT);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001663 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001664 MemOps.push_back(Store);
1665 // Increment the address by four for the next argument to store
Chris Lattnerc91a4752006-06-26 22:48:35 +00001666 SDOperand PtrOff = DAG.getConstant(MVT::getSizeInBits(PtrVT)/8, PtrVT);
1667 FIN = DAG.getNode(ISD::ADD, PtrOff.getValueType(), FIN, PtrOff);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001668 }
Nicolas Geoffray01119992007-04-03 13:59:52 +00001669
1670 // In ELF 32 ABI, the double arguments are stored to the VarArgsFrameIndex
1671 // on the stack.
1672 if (isELF32_ABI) {
1673 for (FPR_idx = 0; FPR_idx != VarArgsNumFPR; ++FPR_idx) {
1674 SDOperand Val = DAG.getRegister(FPR[FPR_idx], MVT::f64);
1675 SDOperand Store = DAG.getStore(Root, Val, FIN, NULL, 0);
1676 MemOps.push_back(Store);
1677 // Increment the address by eight for the next argument to store
1678 SDOperand PtrOff = DAG.getConstant(MVT::getSizeInBits(MVT::f64)/8,
1679 PtrVT);
1680 FIN = DAG.getNode(ISD::ADD, PtrOff.getValueType(), FIN, PtrOff);
1681 }
1682
1683 for (; FPR_idx != Num_FPR_Regs; ++FPR_idx) {
1684 unsigned VReg;
Chris Lattner84bc5422007-12-31 04:13:23 +00001685 VReg = RegInfo.createVirtualRegister(&PPC::F8RCRegClass);
Nicolas Geoffray01119992007-04-03 13:59:52 +00001686
Chris Lattner84bc5422007-12-31 04:13:23 +00001687 RegInfo.addLiveIn(FPR[FPR_idx], VReg);
Nicolas Geoffray01119992007-04-03 13:59:52 +00001688 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::f64);
1689 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1690 MemOps.push_back(Store);
1691 // Increment the address by eight for the next argument to store
1692 SDOperand PtrOff = DAG.getConstant(MVT::getSizeInBits(MVT::f64)/8,
1693 PtrVT);
1694 FIN = DAG.getNode(ISD::ADD, PtrOff.getValueType(), FIN, PtrOff);
1695 }
1696 }
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001697 }
1698
Dale Johannesen8419dd62008-03-07 20:27:40 +00001699 if (!MemOps.empty())
1700 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,&MemOps[0],MemOps.size());
1701
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001702 ArgValues.push_back(Root);
1703
1704 // Return the new list of results.
1705 std::vector<MVT::ValueType> RetVT(Op.Val->value_begin(),
1706 Op.Val->value_end());
Chris Lattner79e490a2006-08-11 17:18:05 +00001707 return DAG.getNode(ISD::MERGE_VALUES, RetVT, &ArgValues[0], ArgValues.size());
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001708}
1709
Chris Lattnerc703a8f2006-05-17 19:00:46 +00001710/// isCallCompatibleAddress - Return the immediate to use if the specified
1711/// 32-bit value is representable in the immediate field of a BxA instruction.
1712static SDNode *isBLACompatibleAddress(SDOperand Op, SelectionDAG &DAG) {
1713 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
1714 if (!C) return 0;
1715
1716 int Addr = C->getValue();
1717 if ((Addr & 3) != 0 || // Low 2 bits are implicitly zero.
1718 (Addr << 6 >> 6) != Addr)
1719 return 0; // Top 6 bits have to be sext of immediate.
1720
Evan Cheng33118762007-10-22 19:46:19 +00001721 return DAG.getConstant((int)C->getValue() >> 2,
1722 DAG.getTargetLoweringInfo().getPointerTy()).Val;
Chris Lattnerc703a8f2006-05-17 19:00:46 +00001723}
1724
Dale Johannesen5b3b6952008-03-04 23:17:14 +00001725/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1726/// by "Src" to address "Dst" of size "Size". Alignment information is
1727/// specified by the specific parameter attribute. The copy will be passed as
1728/// a byval function parameter.
1729/// Sometimes what we are copying is the end of a larger object, the part that
1730/// does not fit in registers.
1731static SDOperand
1732CreateCopyOfByValArgument(SDOperand Src, SDOperand Dst, SDOperand Chain,
Duncan Sands276dcbd2008-03-21 09:14:45 +00001733 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1734 unsigned Size) {
Dan Gohman707e0182008-04-12 04:36:06 +00001735 SDOperand SizeNode = DAG.getConstant(Size, MVT::i32);
1736 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, Flags.getByValAlign(), false,
1737 NULL, 0, NULL, 0);
Dale Johannesen5b3b6952008-03-04 23:17:14 +00001738}
Chris Lattner9f0bc652007-02-25 05:34:32 +00001739
Dale Johannesen5b3b6952008-03-04 23:17:14 +00001740SDOperand PPCTargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG,
Dan Gohman7925ed02008-03-19 21:39:28 +00001741 const PPCSubtarget &Subtarget,
1742 TargetMachine &TM) {
Chris Lattner9f0bc652007-02-25 05:34:32 +00001743 SDOperand Chain = Op.getOperand(0);
1744 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1745 SDOperand Callee = Op.getOperand(4);
1746 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
1747
1748 bool isMachoABI = Subtarget.isMachoABI();
Nicolas Geoffrayec58d9f2007-04-03 12:35:28 +00001749 bool isELF32_ABI = Subtarget.isELF32_ABI();
Evan Cheng4360bdc2006-05-25 00:57:32 +00001750
Chris Lattnerc91a4752006-06-26 22:48:35 +00001751 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1752 bool isPPC64 = PtrVT == MVT::i64;
1753 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Chris Lattnerc91a4752006-06-26 22:48:35 +00001754
Chris Lattnerabde4602006-05-16 22:56:08 +00001755 // args_to_use will accumulate outgoing args for the PPCISD::CALL case in
1756 // SelectExpr to use to put the arguments in the appropriate registers.
1757 std::vector<SDOperand> args_to_use;
1758
1759 // Count how many bytes are to be pushed on the stack, including the linkage
Chris Lattnerc91a4752006-06-26 22:48:35 +00001760 // area, and parameter passing area. We start with 24/48 bytes, which is
Chris Lattnerc8b682c2006-05-17 00:15:40 +00001761 // prereserved space for [SP][CR][LR][3 x unused].
Chris Lattner9f0bc652007-02-25 05:34:32 +00001762 unsigned NumBytes = PPCFrameInfo::getLinkageSize(isPPC64, isMachoABI);
Dale Johannesen75092de2008-03-12 00:22:17 +00001763
Chris Lattnerc8b682c2006-05-17 00:15:40 +00001764 // Add up all the space actually used.
Dale Johannesen8f5422c2008-03-14 17:41:26 +00001765 // In 32-bit non-varargs calls, Altivec parameters all go at the end; usually
1766 // they all go in registers, but we must reserve stack space for them for
1767 // possible use by the caller. In varargs or 64-bit calls, parameters are
1768 // assigned stack space in order, with padding so Altivec parameters are
1769 // 16-byte aligned.
1770 unsigned nAltivecParamsAtEnd = 0;
Jim Laskeye9bd7b22006-11-28 14:53:52 +00001771 for (unsigned i = 0; i != NumOps; ++i) {
Dale Johannesen75092de2008-03-12 00:22:17 +00001772 SDOperand Arg = Op.getOperand(5+2*i);
1773 MVT::ValueType ArgVT = Arg.getValueType();
Dale Johannesen8f5422c2008-03-14 17:41:26 +00001774 if (ArgVT==MVT::v4f32 || ArgVT==MVT::v4i32 ||
1775 ArgVT==MVT::v8i16 || ArgVT==MVT::v16i8) {
1776 if (!isVarArg && !isPPC64) {
1777 // Non-varargs Altivec parameters go after all the non-Altivec parameters;
1778 // do those last so we know how much padding we need.
1779 nAltivecParamsAtEnd++;
1780 continue;
1781 } else {
1782 // Varargs and 64-bit Altivec parameters are padded to 16 byte boundary.
1783 NumBytes = ((NumBytes+15)/16)*16;
1784 }
1785 }
Duncan Sands276dcbd2008-03-21 09:14:45 +00001786 ISD::ArgFlagsTy Flags =
1787 cast<ARG_FLAGSSDNode>(Op.getOperand(5+2*i+1))->getArgFlags();
Jim Laskeye9bd7b22006-11-28 14:53:52 +00001788 unsigned ArgSize =MVT::getSizeInBits(Op.getOperand(5+2*i).getValueType())/8;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001789 if (Flags.isByVal())
1790 ArgSize = Flags.getByValSize();
Dale Johannesen7f96f392008-03-08 01:41:42 +00001791 ArgSize = ((ArgSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Jim Laskeye9bd7b22006-11-28 14:53:52 +00001792 NumBytes += ArgSize;
1793 }
Dale Johannesen8f5422c2008-03-14 17:41:26 +00001794 // Allow for Altivec parameters at the end, if needed.
1795 if (nAltivecParamsAtEnd) {
1796 NumBytes = ((NumBytes+15)/16)*16;
1797 NumBytes += 16*nAltivecParamsAtEnd;
1798 }
Chris Lattnerc04ba7a2006-05-16 23:54:25 +00001799
Chris Lattner7b053502006-05-30 21:21:04 +00001800 // The prolog code of the callee may store up to 8 GPR argument registers to
1801 // the stack, allowing va_start to index over them in memory if its varargs.
1802 // Because we cannot tell if this is needed on the caller side, we have to
1803 // conservatively assume that it is needed. As such, make sure we have at
1804 // least enough stack space for the caller to store the 8 GPRs.
Chris Lattner9f0bc652007-02-25 05:34:32 +00001805 NumBytes = std::max(NumBytes,
1806 PPCFrameInfo::getMinCallFrameSize(isPPC64, isMachoABI));
Chris Lattnerc8b682c2006-05-17 00:15:40 +00001807
1808 // Adjust the stack pointer for the new arguments...
1809 // These operations are automatically eliminated by the prolog/epilog pass
1810 Chain = DAG.getCALLSEQ_START(Chain,
Chris Lattnerc91a4752006-06-26 22:48:35 +00001811 DAG.getConstant(NumBytes, PtrVT));
Dale Johannesen1f797a32008-03-05 23:31:27 +00001812 SDOperand CallSeqStart = Chain;
Chris Lattnerc8b682c2006-05-17 00:15:40 +00001813
1814 // Set up a copy of the stack pointer for use loading and storing any
1815 // arguments that may not fit in the registers available for argument
1816 // passing.
Chris Lattnerc91a4752006-06-26 22:48:35 +00001817 SDOperand StackPtr;
1818 if (isPPC64)
1819 StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
1820 else
1821 StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Chris Lattnerc8b682c2006-05-17 00:15:40 +00001822
1823 // Figure out which arguments are going to go in registers, and which in
1824 // memory. Also, if this is a vararg function, floating point operations
1825 // must be stored to our stack, and loaded into integer regs as well, if
1826 // any integer regs are available for argument passing.
Chris Lattner9f0bc652007-02-25 05:34:32 +00001827 unsigned ArgOffset = PPCFrameInfo::getLinkageSize(isPPC64, isMachoABI);
Chris Lattner9a2a4972006-05-17 06:01:33 +00001828 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Jim Laskey2f616bf2006-11-16 22:43:37 +00001829
Chris Lattnerc91a4752006-06-26 22:48:35 +00001830 static const unsigned GPR_32[] = { // 32-bit registers.
Chris Lattner9a2a4972006-05-17 06:01:33 +00001831 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1832 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1833 };
Chris Lattnerc91a4752006-06-26 22:48:35 +00001834 static const unsigned GPR_64[] = { // 64-bit registers.
1835 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
1836 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
1837 };
Chris Lattner9f0bc652007-02-25 05:34:32 +00001838 static const unsigned *FPR = GetFPR(Subtarget);
1839
Chris Lattner9a2a4972006-05-17 06:01:33 +00001840 static const unsigned VR[] = {
1841 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
1842 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
1843 };
Owen Anderson718cb662007-09-07 04:06:50 +00001844 const unsigned NumGPRs = array_lengthof(GPR_32);
Nicolas Geoffrayef3c0302007-04-03 10:27:07 +00001845 const unsigned NumFPRs = isMachoABI ? 13 : 8;
Owen Anderson718cb662007-09-07 04:06:50 +00001846 const unsigned NumVRs = array_lengthof( VR);
Chris Lattner9a2a4972006-05-17 06:01:33 +00001847
Chris Lattnerc91a4752006-06-26 22:48:35 +00001848 const unsigned *GPR = isPPC64 ? GPR_64 : GPR_32;
1849
Chris Lattner9a2a4972006-05-17 06:01:33 +00001850 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
Chris Lattnere2199452006-08-11 17:38:39 +00001851 SmallVector<SDOperand, 8> MemOpChains;
Evan Cheng4360bdc2006-05-25 00:57:32 +00001852 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattner9f0bc652007-02-25 05:34:32 +00001853 bool inMem = false;
Evan Cheng4360bdc2006-05-25 00:57:32 +00001854 SDOperand Arg = Op.getOperand(5+2*i);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001855 ISD::ArgFlagsTy Flags =
1856 cast<ARG_FLAGSSDNode>(Op.getOperand(5+2*i+1))->getArgFlags();
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00001857 // See if next argument requires stack alignment in ELF
Nicolas Geoffray6ccbbd82008-04-15 08:08:50 +00001858 bool Align = Flags.isSplit();
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00001859
Chris Lattnerc8b682c2006-05-17 00:15:40 +00001860 // PtrOff will be used to store the current argument to the stack if a
1861 // register cannot be found for it.
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00001862 SDOperand PtrOff;
1863
Nicolas Geoffrayec58d9f2007-04-03 12:35:28 +00001864 // Stack align in ELF 32
Nicolas Geoffrayc0cb28f2008-04-13 13:40:22 +00001865 if (isELF32_ABI && Align)
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00001866 PtrOff = DAG.getConstant(ArgOffset + ((ArgOffset/4) % 2) * PtrByteSize,
1867 StackPtr.getValueType());
1868 else
1869 PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
1870
Chris Lattnerc91a4752006-06-26 22:48:35 +00001871 PtrOff = DAG.getNode(ISD::ADD, PtrVT, StackPtr, PtrOff);
1872
1873 // On PPC64, promote integers to 64-bit values.
1874 if (isPPC64 && Arg.getValueType() == MVT::i32) {
Duncan Sands276dcbd2008-03-21 09:14:45 +00001875 // FIXME: Should this use ANY_EXTEND if neither sext nor zext?
1876 unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Chris Lattnerc91a4752006-06-26 22:48:35 +00001877 Arg = DAG.getNode(ExtOp, MVT::i64, Arg);
1878 }
Dale Johannesen5b3b6952008-03-04 23:17:14 +00001879
1880 // FIXME Elf untested, what are alignment rules?
Dale Johannesen8419dd62008-03-07 20:27:40 +00001881 // FIXME memcpy is used way more than necessary. Correctness first.
Duncan Sands276dcbd2008-03-21 09:14:45 +00001882 if (Flags.isByVal()) {
1883 unsigned Size = Flags.getByValSize();
Nicolas Geoffrayc0cb28f2008-04-13 13:40:22 +00001884 if (isELF32_ABI && Align) GPR_idx += (GPR_idx % 2);
Dale Johannesen8419dd62008-03-07 20:27:40 +00001885 if (Size==1 || Size==2) {
1886 // Very small objects are passed right-justified.
1887 // Everything else is passed left-justified.
1888 MVT::ValueType VT = (Size==1) ? MVT::i8 : MVT::i16;
1889 if (GPR_idx != NumGPRs) {
1890 SDOperand Load = DAG.getExtLoad(ISD::EXTLOAD, PtrVT, Chain, Arg,
1891 NULL, 0, VT);
1892 MemOpChains.push_back(Load.getValue(1));
1893 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
1894 if (isMachoABI)
1895 ArgOffset += PtrByteSize;
1896 } else {
1897 SDOperand Const = DAG.getConstant(4 - Size, PtrOff.getValueType());
1898 SDOperand AddPtr = DAG.getNode(ISD::ADD, PtrVT, PtrOff, Const);
1899 SDOperand MemcpyCall = CreateCopyOfByValArgument(Arg, AddPtr,
1900 CallSeqStart.Val->getOperand(0),
1901 Flags, DAG, Size);
1902 // This must go outside the CALLSEQ_START..END.
1903 SDOperand NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
1904 CallSeqStart.Val->getOperand(1));
1905 DAG.ReplaceAllUsesWith(CallSeqStart.Val, NewCallSeqStart.Val);
1906 Chain = CallSeqStart = NewCallSeqStart;
1907 ArgOffset += PtrByteSize;
1908 }
1909 continue;
1910 }
Dale Johannesenfdd3ade2008-03-17 02:13:43 +00001911 // Copy entire object into memory. There are cases where gcc-generated
1912 // code assumes it is there, even if it could be put entirely into
1913 // registers. (This is not what the doc says.)
1914 SDOperand MemcpyCall = CreateCopyOfByValArgument(Arg, PtrOff,
1915 CallSeqStart.Val->getOperand(0),
1916 Flags, DAG, Size);
1917 // This must go outside the CALLSEQ_START..END.
1918 SDOperand NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
1919 CallSeqStart.Val->getOperand(1));
1920 DAG.ReplaceAllUsesWith(CallSeqStart.Val, NewCallSeqStart.Val);
1921 Chain = CallSeqStart = NewCallSeqStart;
1922 // And copy the pieces of it that fit into registers.
Dale Johannesen5b3b6952008-03-04 23:17:14 +00001923 for (unsigned j=0; j<Size; j+=PtrByteSize) {
1924 SDOperand Const = DAG.getConstant(j, PtrOff.getValueType());
1925 SDOperand AddArg = DAG.getNode(ISD::ADD, PtrVT, Arg, Const);
1926 if (GPR_idx != NumGPRs) {
1927 SDOperand Load = DAG.getLoad(PtrVT, Chain, AddArg, NULL, 0);
Dale Johannesen1f797a32008-03-05 23:31:27 +00001928 MemOpChains.push_back(Load.getValue(1));
Dale Johannesen5b3b6952008-03-04 23:17:14 +00001929 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
1930 if (isMachoABI)
1931 ArgOffset += PtrByteSize;
1932 } else {
Dale Johannesenfdd3ade2008-03-17 02:13:43 +00001933 ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize;
Dale Johannesen8419dd62008-03-07 20:27:40 +00001934 break;
Dale Johannesen5b3b6952008-03-04 23:17:14 +00001935 }
1936 }
1937 continue;
1938 }
1939
Chris Lattnerc8b682c2006-05-17 00:15:40 +00001940 switch (Arg.getValueType()) {
1941 default: assert(0 && "Unexpected ValueType for argument!");
1942 case MVT::i32:
Chris Lattnerc91a4752006-06-26 22:48:35 +00001943 case MVT::i64:
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00001944 // Double word align in ELF
Nicolas Geoffrayc0cb28f2008-04-13 13:40:22 +00001945 if (isELF32_ABI && Align) GPR_idx += (GPR_idx % 2);
Chris Lattner9a2a4972006-05-17 06:01:33 +00001946 if (GPR_idx != NumGPRs) {
1947 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg));
Chris Lattnerc8b682c2006-05-17 00:15:40 +00001948 } else {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001949 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
Chris Lattner9f0bc652007-02-25 05:34:32 +00001950 inMem = true;
Chris Lattnerc8b682c2006-05-17 00:15:40 +00001951 }
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00001952 if (inMem || isMachoABI) {
1953 // Stack align in ELF
Nicolas Geoffrayc0cb28f2008-04-13 13:40:22 +00001954 if (isELF32_ABI && Align)
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00001955 ArgOffset += ((ArgOffset/4) % 2) * PtrByteSize;
1956
1957 ArgOffset += PtrByteSize;
1958 }
Chris Lattnerc8b682c2006-05-17 00:15:40 +00001959 break;
1960 case MVT::f32:
1961 case MVT::f64:
Chris Lattner9a2a4972006-05-17 06:01:33 +00001962 if (FPR_idx != NumFPRs) {
1963 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg));
1964
Chris Lattnerc8b682c2006-05-17 00:15:40 +00001965 if (isVarArg) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001966 SDOperand Store = DAG.getStore(Chain, Arg, PtrOff, NULL, 0);
Chris Lattner9a2a4972006-05-17 06:01:33 +00001967 MemOpChains.push_back(Store);
1968
Chris Lattnerc8b682c2006-05-17 00:15:40 +00001969 // Float varargs are always shadowed in available integer registers
Chris Lattner9a2a4972006-05-17 06:01:33 +00001970 if (GPR_idx != NumGPRs) {
Evan Cheng466685d2006-10-09 20:57:25 +00001971 SDOperand Load = DAG.getLoad(PtrVT, Store, PtrOff, NULL, 0);
Chris Lattner9a2a4972006-05-17 06:01:33 +00001972 MemOpChains.push_back(Load.getValue(1));
Chris Lattner9f0bc652007-02-25 05:34:32 +00001973 if (isMachoABI) RegsToPass.push_back(std::make_pair(GPR[GPR_idx++],
1974 Load));
Chris Lattnerc8b682c2006-05-17 00:15:40 +00001975 }
Jim Laskeyfbb74e62006-12-01 16:30:47 +00001976 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 && !isPPC64){
Chris Lattnerc8b682c2006-05-17 00:15:40 +00001977 SDOperand ConstFour = DAG.getConstant(4, PtrOff.getValueType());
Chris Lattnerc91a4752006-06-26 22:48:35 +00001978 PtrOff = DAG.getNode(ISD::ADD, PtrVT, PtrOff, ConstFour);
Evan Cheng466685d2006-10-09 20:57:25 +00001979 SDOperand Load = DAG.getLoad(PtrVT, Store, PtrOff, NULL, 0);
Chris Lattner9a2a4972006-05-17 06:01:33 +00001980 MemOpChains.push_back(Load.getValue(1));
Chris Lattner9f0bc652007-02-25 05:34:32 +00001981 if (isMachoABI) RegsToPass.push_back(std::make_pair(GPR[GPR_idx++],
1982 Load));
Chris Lattnerabde4602006-05-16 22:56:08 +00001983 }
1984 } else {
Chris Lattnerc8b682c2006-05-17 00:15:40 +00001985 // If we have any FPRs remaining, we may also have GPRs remaining.
1986 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
1987 // GPRs.
Chris Lattner9f0bc652007-02-25 05:34:32 +00001988 if (isMachoABI) {
1989 if (GPR_idx != NumGPRs)
1990 ++GPR_idx;
1991 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 &&
1992 !isPPC64) // PPC64 has 64-bit GPR's obviously :)
1993 ++GPR_idx;
1994 }
Chris Lattnerabde4602006-05-16 22:56:08 +00001995 }
Chris Lattnerc8b682c2006-05-17 00:15:40 +00001996 } else {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001997 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
Chris Lattner9f0bc652007-02-25 05:34:32 +00001998 inMem = true;
Chris Lattnerabde4602006-05-16 22:56:08 +00001999 }
Chris Lattner9f0bc652007-02-25 05:34:32 +00002000 if (inMem || isMachoABI) {
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00002001 // Stack align in ELF
Nicolas Geoffrayc0cb28f2008-04-13 13:40:22 +00002002 if (isELF32_ABI && Align)
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00002003 ArgOffset += ((ArgOffset/4) % 2) * PtrByteSize;
Chris Lattner9f0bc652007-02-25 05:34:32 +00002004 if (isPPC64)
2005 ArgOffset += 8;
2006 else
2007 ArgOffset += Arg.getValueType() == MVT::f32 ? 4 : 8;
2008 }
Chris Lattnerc8b682c2006-05-17 00:15:40 +00002009 break;
2010 case MVT::v4f32:
2011 case MVT::v4i32:
2012 case MVT::v8i16:
2013 case MVT::v16i8:
Dale Johannesen75092de2008-03-12 00:22:17 +00002014 if (isVarArg) {
2015 // These go aligned on the stack, or in the corresponding R registers
2016 // when within range. The Darwin PPC ABI doc claims they also go in
2017 // V registers; in fact gcc does this only for arguments that are
2018 // prototyped, not for those that match the ... We do it for all
2019 // arguments, seems to work.
2020 while (ArgOffset % 16 !=0) {
2021 ArgOffset += PtrByteSize;
2022 if (GPR_idx != NumGPRs)
2023 GPR_idx++;
2024 }
2025 // We could elide this store in the case where the object fits
2026 // entirely in R registers. Maybe later.
2027 PtrOff = DAG.getNode(ISD::ADD, PtrVT, StackPtr,
2028 DAG.getConstant(ArgOffset, PtrVT));
2029 SDOperand Store = DAG.getStore(Chain, Arg, PtrOff, NULL, 0);
2030 MemOpChains.push_back(Store);
2031 if (VR_idx != NumVRs) {
2032 SDOperand Load = DAG.getLoad(MVT::v4f32, Store, PtrOff, NULL, 0);
2033 MemOpChains.push_back(Load.getValue(1));
2034 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Load));
2035 }
2036 ArgOffset += 16;
2037 for (unsigned i=0; i<16; i+=PtrByteSize) {
2038 if (GPR_idx == NumGPRs)
2039 break;
2040 SDOperand Ix = DAG.getNode(ISD::ADD, PtrVT, PtrOff,
2041 DAG.getConstant(i, PtrVT));
2042 SDOperand Load = DAG.getLoad(PtrVT, Store, Ix, NULL, 0);
2043 MemOpChains.push_back(Load.getValue(1));
2044 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
2045 }
2046 break;
2047 }
Dale Johannesen8f5422c2008-03-14 17:41:26 +00002048 // Non-varargs Altivec params generally go in registers, but have
2049 // stack space allocated at the end.
2050 if (VR_idx != NumVRs) {
2051 // Doesn't have GPR space allocated.
2052 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Arg));
2053 } else if (nAltivecParamsAtEnd==0) {
2054 // We are emitting Altivec params in order.
Dale Johannesen75092de2008-03-12 00:22:17 +00002055 PtrOff = DAG.getNode(ISD::ADD, PtrVT, StackPtr,
2056 DAG.getConstant(ArgOffset, PtrVT));
2057 SDOperand Store = DAG.getStore(Chain, Arg, PtrOff, NULL, 0);
2058 MemOpChains.push_back(Store);
2059 ArgOffset += 16;
Dale Johannesen75092de2008-03-12 00:22:17 +00002060 }
Chris Lattnerc8b682c2006-05-17 00:15:40 +00002061 break;
Chris Lattnerabde4602006-05-16 22:56:08 +00002062 }
Chris Lattnerabde4602006-05-16 22:56:08 +00002063 }
Dale Johannesen8f5422c2008-03-14 17:41:26 +00002064 // If all Altivec parameters fit in registers, as they usually do,
2065 // they get stack space following the non-Altivec parameters. We
2066 // don't track this here because nobody below needs it.
2067 // If there are more Altivec parameters than fit in registers emit
2068 // the stores here.
2069 if (!isVarArg && nAltivecParamsAtEnd > NumVRs) {
2070 unsigned j = 0;
2071 // Offset is aligned; skip 1st 12 params which go in V registers.
2072 ArgOffset = ((ArgOffset+15)/16)*16;
2073 ArgOffset += 12*16;
2074 for (unsigned i = 0; i != NumOps; ++i) {
2075 SDOperand Arg = Op.getOperand(5+2*i);
2076 MVT::ValueType ArgType = Arg.getValueType();
2077 if (ArgType==MVT::v4f32 || ArgType==MVT::v4i32 ||
2078 ArgType==MVT::v8i16 || ArgType==MVT::v16i8) {
2079 if (++j > NumVRs) {
2080 SDOperand PtrOff = DAG.getNode(ISD::ADD, PtrVT, StackPtr,
2081 DAG.getConstant(ArgOffset, PtrVT));
2082 SDOperand Store = DAG.getStore(Chain, Arg, PtrOff, NULL, 0);
2083 MemOpChains.push_back(Store);
2084 ArgOffset += 16;
2085 }
2086 }
2087 }
2088 }
2089
Chris Lattner9a2a4972006-05-17 06:01:33 +00002090 if (!MemOpChains.empty())
Chris Lattnere2199452006-08-11 17:38:39 +00002091 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
2092 &MemOpChains[0], MemOpChains.size());
Chris Lattnerabde4602006-05-16 22:56:08 +00002093
Chris Lattner9a2a4972006-05-17 06:01:33 +00002094 // Build a sequence of copy-to-reg nodes chained together with token chain
2095 // and flag operands which copy the outgoing args into the appropriate regs.
2096 SDOperand InFlag;
2097 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2098 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
2099 InFlag);
2100 InFlag = Chain.getValue(1);
2101 }
Chris Lattner9f0bc652007-02-25 05:34:32 +00002102
Nicolas Geoffrayec58d9f2007-04-03 12:35:28 +00002103 // With the ELF 32 ABI, set CR6 to true if this is a vararg call.
2104 if (isVarArg && isELF32_ABI) {
Nicolas Geoffray0404cd92008-03-10 14:12:10 +00002105 SDOperand SetCR(DAG.getTargetNode(PPC::CRSET, MVT::i32), 0);
2106 Chain = DAG.getCopyToReg(Chain, PPC::CR1EQ, SetCR, InFlag);
Chris Lattner9f0bc652007-02-25 05:34:32 +00002107 InFlag = Chain.getValue(1);
2108 }
2109
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002110 std::vector<MVT::ValueType> NodeTys;
Chris Lattner4a45abf2006-06-10 01:14:28 +00002111 NodeTys.push_back(MVT::Other); // Returns a chain
2112 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
2113
Chris Lattner79e490a2006-08-11 17:18:05 +00002114 SmallVector<SDOperand, 8> Ops;
Nicolas Geoffray63f8fb12007-02-27 13:01:19 +00002115 unsigned CallOpc = isMachoABI? PPCISD::CALL_Macho : PPCISD::CALL_ELF;
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002116
2117 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
2118 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2119 // node so that legalize doesn't hack it.
Nicolas Geoffray5a6c91a2007-12-21 12:22:29 +00002120 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
2121 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), Callee.getValueType());
2122 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002123 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), Callee.getValueType());
2124 else if (SDNode *Dest = isBLACompatibleAddress(Callee, DAG))
2125 // If this is an absolute destination address, use the munged value.
2126 Callee = SDOperand(Dest, 0);
2127 else {
2128 // Otherwise, this is an indirect call. We have to use a MTCTR/BCTRL pair
2129 // to do the call, we can't use PPCISD::CALL.
Chris Lattner79e490a2006-08-11 17:18:05 +00002130 SDOperand MTCTROps[] = {Chain, Callee, InFlag};
2131 Chain = DAG.getNode(PPCISD::MTCTR, NodeTys, MTCTROps, 2+(InFlag.Val!=0));
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002132 InFlag = Chain.getValue(1);
2133
Chris Lattnerdc9971a2008-03-09 20:49:33 +00002134 // Copy the callee address into R12/X12 on darwin.
Chris Lattner9f0bc652007-02-25 05:34:32 +00002135 if (isMachoABI) {
Chris Lattnerdc9971a2008-03-09 20:49:33 +00002136 unsigned Reg = Callee.getValueType() == MVT::i32 ? PPC::R12 : PPC::X12;
2137 Chain = DAG.getCopyToReg(Chain, Reg, Callee, InFlag);
Chris Lattner9f0bc652007-02-25 05:34:32 +00002138 InFlag = Chain.getValue(1);
2139 }
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002140
2141 NodeTys.clear();
2142 NodeTys.push_back(MVT::Other);
2143 NodeTys.push_back(MVT::Flag);
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002144 Ops.push_back(Chain);
Chris Lattner9f0bc652007-02-25 05:34:32 +00002145 CallOpc = isMachoABI ? PPCISD::BCTRL_Macho : PPCISD::BCTRL_ELF;
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002146 Callee.Val = 0;
2147 }
Chris Lattner9a2a4972006-05-17 06:01:33 +00002148
Chris Lattner4a45abf2006-06-10 01:14:28 +00002149 // If this is a direct call, pass the chain and the callee.
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002150 if (Callee.Val) {
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002151 Ops.push_back(Chain);
2152 Ops.push_back(Callee);
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002153 }
Chris Lattnerabde4602006-05-16 22:56:08 +00002154
Chris Lattner4a45abf2006-06-10 01:14:28 +00002155 // Add argument registers to the end of the list so that they are known live
2156 // into the call.
2157 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2158 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2159 RegsToPass[i].second.getValueType()));
2160
2161 if (InFlag.Val)
2162 Ops.push_back(InFlag);
Chris Lattner79e490a2006-08-11 17:18:05 +00002163 Chain = DAG.getNode(CallOpc, NodeTys, &Ops[0], Ops.size());
Chris Lattner4a45abf2006-06-10 01:14:28 +00002164 InFlag = Chain.getValue(1);
2165
Bill Wendling0f8d9c02007-11-13 00:44:25 +00002166 Chain = DAG.getCALLSEQ_END(Chain,
2167 DAG.getConstant(NumBytes, PtrVT),
2168 DAG.getConstant(0, PtrVT),
2169 InFlag);
2170 if (Op.Val->getValueType(0) != MVT::Other)
2171 InFlag = Chain.getValue(1);
2172
Dan Gohman7925ed02008-03-19 21:39:28 +00002173 SmallVector<SDOperand, 16> ResultVals;
2174 SmallVector<CCValAssign, 16> RVLocs;
2175 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
2176 CCState CCInfo(CC, isVarArg, TM, RVLocs);
2177 CCInfo.AnalyzeCallResult(Op.Val, RetCC_PPC);
Chris Lattner9a2a4972006-05-17 06:01:33 +00002178
Dan Gohman7925ed02008-03-19 21:39:28 +00002179 // Copy all of the result registers out of their specified physreg.
2180 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
2181 CCValAssign &VA = RVLocs[i];
2182 MVT::ValueType VT = VA.getValVT();
2183 assert(VA.isRegLoc() && "Can only return in registers!");
2184 Chain = DAG.getCopyFromReg(Chain, VA.getLocReg(), VT, InFlag).getValue(1);
2185 ResultVals.push_back(Chain.getValue(0));
2186 InFlag = Chain.getValue(2);
Chris Lattner9a2a4972006-05-17 06:01:33 +00002187 }
Dan Gohman7925ed02008-03-19 21:39:28 +00002188
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002189 // If the function returns void, just return the chain.
Dan Gohman7925ed02008-03-19 21:39:28 +00002190 if (RVLocs.empty())
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002191 return Chain;
2192
2193 // Otherwise, merge everything together with a MERGE_VALUES node.
Dan Gohman7925ed02008-03-19 21:39:28 +00002194 ResultVals.push_back(Chain);
2195 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
2196 &ResultVals[0], ResultVals.size());
Chris Lattnerabde4602006-05-16 22:56:08 +00002197 return Res.getValue(Op.ResNo);
2198}
2199
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002200SDOperand PPCTargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG,
2201 TargetMachine &TM) {
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00002202 SmallVector<CCValAssign, 16> RVLocs;
2203 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
Chris Lattner52387be2007-06-19 00:13:10 +00002204 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
2205 CCState CCInfo(CC, isVarArg, TM, RVLocs);
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00002206 CCInfo.AnalyzeReturn(Op.Val, RetCC_PPC);
2207
2208 // If this is the first return lowered for this function, add the regs to the
2209 // liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00002210 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00002211 for (unsigned i = 0; i != RVLocs.size(); ++i)
Chris Lattner84bc5422007-12-31 04:13:23 +00002212 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00002213 }
2214
Chris Lattnercaddd442007-02-26 19:44:02 +00002215 SDOperand Chain = Op.getOperand(0);
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00002216 SDOperand Flag;
2217
2218 // Copy the result values into the output registers.
2219 for (unsigned i = 0; i != RVLocs.size(); ++i) {
2220 CCValAssign &VA = RVLocs[i];
2221 assert(VA.isRegLoc() && "Can only return in registers!");
2222 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1), Flag);
2223 Flag = Chain.getValue(1);
2224 }
2225
2226 if (Flag.Val)
2227 return DAG.getNode(PPCISD::RET_FLAG, MVT::Other, Chain, Flag);
2228 else
Chris Lattnercaddd442007-02-26 19:44:02 +00002229 return DAG.getNode(PPCISD::RET_FLAG, MVT::Other, Chain);
Chris Lattner1a635d62006-04-14 06:01:58 +00002230}
2231
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002232SDOperand PPCTargetLowering::LowerSTACKRESTORE(SDOperand Op, SelectionDAG &DAG,
Jim Laskeyefc7e522006-12-04 22:04:42 +00002233 const PPCSubtarget &Subtarget) {
2234 // When we pop the dynamic allocation we need to restore the SP link.
2235
2236 // Get the corect type for pointers.
2237 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
2238
2239 // Construct the stack pointer operand.
2240 bool IsPPC64 = Subtarget.isPPC64();
2241 unsigned SP = IsPPC64 ? PPC::X1 : PPC::R1;
2242 SDOperand StackPtr = DAG.getRegister(SP, PtrVT);
2243
2244 // Get the operands for the STACKRESTORE.
2245 SDOperand Chain = Op.getOperand(0);
2246 SDOperand SaveSP = Op.getOperand(1);
2247
2248 // Load the old link SP.
2249 SDOperand LoadLinkSP = DAG.getLoad(PtrVT, Chain, StackPtr, NULL, 0);
2250
2251 // Restore the stack pointer.
2252 Chain = DAG.getCopyToReg(LoadLinkSP.getValue(1), SP, SaveSP);
2253
2254 // Store the old link SP.
2255 return DAG.getStore(Chain, LoadLinkSP, StackPtr, NULL, 0);
2256}
2257
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002258SDOperand PPCTargetLowering::LowerDYNAMIC_STACKALLOC(SDOperand Op,
2259 SelectionDAG &DAG,
Jim Laskey2f616bf2006-11-16 22:43:37 +00002260 const PPCSubtarget &Subtarget) {
2261 MachineFunction &MF = DAG.getMachineFunction();
2262 bool IsPPC64 = Subtarget.isPPC64();
Chris Lattner9f0bc652007-02-25 05:34:32 +00002263 bool isMachoABI = Subtarget.isMachoABI();
Jim Laskey2f616bf2006-11-16 22:43:37 +00002264
2265 // Get current frame pointer save index. The users of this index will be
2266 // primarily DYNALLOC instructions.
2267 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
2268 int FPSI = FI->getFramePointerSaveIndex();
Chris Lattner9f0bc652007-02-25 05:34:32 +00002269
Jim Laskey2f616bf2006-11-16 22:43:37 +00002270 // If the frame pointer save index hasn't been defined yet.
2271 if (!FPSI) {
2272 // Find out what the fix offset of the frame pointer save area.
Chris Lattner9f0bc652007-02-25 05:34:32 +00002273 int FPOffset = PPCFrameInfo::getFramePointerSaveOffset(IsPPC64, isMachoABI);
2274
Jim Laskey2f616bf2006-11-16 22:43:37 +00002275 // Allocate the frame index for frame pointer save area.
Chris Lattner9f0bc652007-02-25 05:34:32 +00002276 FPSI = MF.getFrameInfo()->CreateFixedObject(IsPPC64? 8 : 4, FPOffset);
Jim Laskey2f616bf2006-11-16 22:43:37 +00002277 // Save the result.
2278 FI->setFramePointerSaveIndex(FPSI);
2279 }
2280
2281 // Get the inputs.
2282 SDOperand Chain = Op.getOperand(0);
2283 SDOperand Size = Op.getOperand(1);
2284
2285 // Get the corect type for pointers.
2286 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
2287 // Negate the size.
2288 SDOperand NegSize = DAG.getNode(ISD::SUB, PtrVT,
2289 DAG.getConstant(0, PtrVT), Size);
2290 // Construct a node for the frame pointer save index.
2291 SDOperand FPSIdx = DAG.getFrameIndex(FPSI, PtrVT);
2292 // Build a DYNALLOC node.
2293 SDOperand Ops[3] = { Chain, NegSize, FPSIdx };
2294 SDVTList VTs = DAG.getVTList(PtrVT, MVT::Other);
2295 return DAG.getNode(PPCISD::DYNALLOC, VTs, Ops, 3);
2296}
2297
2298
Chris Lattner1a635d62006-04-14 06:01:58 +00002299/// LowerSELECT_CC - Lower floating point select_cc's into fsel instruction when
2300/// possible.
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002301SDOperand PPCTargetLowering::LowerSELECT_CC(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner1a635d62006-04-14 06:01:58 +00002302 // Not FP? Not a fsel.
2303 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
2304 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
2305 return SDOperand();
2306
2307 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
2308
2309 // Cannot handle SETEQ/SETNE.
2310 if (CC == ISD::SETEQ || CC == ISD::SETNE) return SDOperand();
2311
2312 MVT::ValueType ResVT = Op.getValueType();
2313 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
2314 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
2315 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
2316
2317 // If the RHS of the comparison is a 0.0, we don't need to do the
2318 // subtraction at all.
2319 if (isFloatingPointZero(RHS))
2320 switch (CC) {
2321 default: break; // SETUO etc aren't handled by fsel.
2322 case ISD::SETULT:
Chris Lattner57340122006-05-24 00:06:44 +00002323 case ISD::SETOLT:
Chris Lattner1a635d62006-04-14 06:01:58 +00002324 case ISD::SETLT:
2325 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
2326 case ISD::SETUGE:
Chris Lattner57340122006-05-24 00:06:44 +00002327 case ISD::SETOGE:
Chris Lattner1a635d62006-04-14 06:01:58 +00002328 case ISD::SETGE:
2329 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
2330 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
2331 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
2332 case ISD::SETUGT:
Chris Lattner57340122006-05-24 00:06:44 +00002333 case ISD::SETOGT:
Chris Lattner1a635d62006-04-14 06:01:58 +00002334 case ISD::SETGT:
2335 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
2336 case ISD::SETULE:
Chris Lattner57340122006-05-24 00:06:44 +00002337 case ISD::SETOLE:
Chris Lattner1a635d62006-04-14 06:01:58 +00002338 case ISD::SETLE:
2339 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
2340 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
2341 return DAG.getNode(PPCISD::FSEL, ResVT,
2342 DAG.getNode(ISD::FNEG, MVT::f64, LHS), TV, FV);
2343 }
2344
Chris Lattner1de7c1d2007-10-15 20:14:52 +00002345 SDOperand Cmp;
Chris Lattner1a635d62006-04-14 06:01:58 +00002346 switch (CC) {
2347 default: break; // SETUO etc aren't handled by fsel.
2348 case ISD::SETULT:
Chris Lattner57340122006-05-24 00:06:44 +00002349 case ISD::SETOLT:
Chris Lattner1a635d62006-04-14 06:01:58 +00002350 case ISD::SETLT:
2351 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
2352 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
2353 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
2354 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
2355 case ISD::SETUGE:
Chris Lattner57340122006-05-24 00:06:44 +00002356 case ISD::SETOGE:
Chris Lattner1a635d62006-04-14 06:01:58 +00002357 case ISD::SETGE:
2358 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
2359 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
2360 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
2361 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
2362 case ISD::SETUGT:
Chris Lattner57340122006-05-24 00:06:44 +00002363 case ISD::SETOGT:
Chris Lattner1a635d62006-04-14 06:01:58 +00002364 case ISD::SETGT:
2365 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
2366 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
2367 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
2368 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
2369 case ISD::SETULE:
Chris Lattner57340122006-05-24 00:06:44 +00002370 case ISD::SETOLE:
Chris Lattner1a635d62006-04-14 06:01:58 +00002371 case ISD::SETLE:
2372 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
2373 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
2374 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
2375 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
2376 }
2377 return SDOperand();
2378}
2379
Chris Lattner1f873002007-11-28 18:44:47 +00002380// FIXME: Split this code up when LegalizeDAGTypes lands.
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002381SDOperand PPCTargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner1a635d62006-04-14 06:01:58 +00002382 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
2383 SDOperand Src = Op.getOperand(0);
2384 if (Src.getValueType() == MVT::f32)
2385 Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src);
2386
2387 SDOperand Tmp;
2388 switch (Op.getValueType()) {
2389 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
2390 case MVT::i32:
2391 Tmp = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
2392 break;
2393 case MVT::i64:
2394 Tmp = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
2395 break;
2396 }
2397
2398 // Convert the FP value to an int value through memory.
Chris Lattner1de7c1d2007-10-15 20:14:52 +00002399 SDOperand FIPtr = DAG.CreateStackTemporary(MVT::f64);
2400
2401 // Emit a store to the stack slot.
2402 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Tmp, FIPtr, NULL, 0);
2403
2404 // Result is a load from the stack slot. If loading 4 bytes, make sure to
2405 // add in a bias.
Chris Lattner1a635d62006-04-14 06:01:58 +00002406 if (Op.getValueType() == MVT::i32)
Chris Lattner1de7c1d2007-10-15 20:14:52 +00002407 FIPtr = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr,
2408 DAG.getConstant(4, FIPtr.getValueType()));
2409 return DAG.getLoad(Op.getValueType(), Chain, FIPtr, NULL, 0);
Chris Lattner1a635d62006-04-14 06:01:58 +00002410}
2411
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002412SDOperand PPCTargetLowering::LowerFP_ROUND_INREG(SDOperand Op,
2413 SelectionDAG &DAG) {
Dale Johannesen6eaeff22007-10-10 01:01:31 +00002414 assert(Op.getValueType() == MVT::ppcf128);
2415 SDNode *Node = Op.Val;
2416 assert(Node->getOperand(0).getValueType() == MVT::ppcf128);
Chris Lattner26cb2862007-10-19 04:08:28 +00002417 assert(Node->getOperand(0).Val->getOpcode() == ISD::BUILD_PAIR);
Dale Johannesen6eaeff22007-10-10 01:01:31 +00002418 SDOperand Lo = Node->getOperand(0).Val->getOperand(0);
2419 SDOperand Hi = Node->getOperand(0).Val->getOperand(1);
2420
2421 // This sequence changes FPSCR to do round-to-zero, adds the two halves
2422 // of the long double, and puts FPSCR back the way it was. We do not
2423 // actually model FPSCR.
2424 std::vector<MVT::ValueType> NodeTys;
2425 SDOperand Ops[4], Result, MFFSreg, InFlag, FPreg;
2426
2427 NodeTys.push_back(MVT::f64); // Return register
2428 NodeTys.push_back(MVT::Flag); // Returns a flag for later insns
2429 Result = DAG.getNode(PPCISD::MFFS, NodeTys, &InFlag, 0);
2430 MFFSreg = Result.getValue(0);
2431 InFlag = Result.getValue(1);
2432
2433 NodeTys.clear();
2434 NodeTys.push_back(MVT::Flag); // Returns a flag
2435 Ops[0] = DAG.getConstant(31, MVT::i32);
2436 Ops[1] = InFlag;
2437 Result = DAG.getNode(PPCISD::MTFSB1, NodeTys, Ops, 2);
2438 InFlag = Result.getValue(0);
2439
2440 NodeTys.clear();
2441 NodeTys.push_back(MVT::Flag); // Returns a flag
2442 Ops[0] = DAG.getConstant(30, MVT::i32);
2443 Ops[1] = InFlag;
2444 Result = DAG.getNode(PPCISD::MTFSB0, NodeTys, Ops, 2);
2445 InFlag = Result.getValue(0);
2446
2447 NodeTys.clear();
2448 NodeTys.push_back(MVT::f64); // result of add
2449 NodeTys.push_back(MVT::Flag); // Returns a flag
2450 Ops[0] = Lo;
2451 Ops[1] = Hi;
2452 Ops[2] = InFlag;
2453 Result = DAG.getNode(PPCISD::FADDRTZ, NodeTys, Ops, 3);
2454 FPreg = Result.getValue(0);
2455 InFlag = Result.getValue(1);
2456
2457 NodeTys.clear();
2458 NodeTys.push_back(MVT::f64);
2459 Ops[0] = DAG.getConstant(1, MVT::i32);
2460 Ops[1] = MFFSreg;
2461 Ops[2] = FPreg;
2462 Ops[3] = InFlag;
2463 Result = DAG.getNode(PPCISD::MTFSF, NodeTys, Ops, 4);
2464 FPreg = Result.getValue(0);
2465
2466 // We know the low half is about to be thrown away, so just use something
2467 // convenient.
2468 return DAG.getNode(ISD::BUILD_PAIR, Lo.getValueType(), FPreg, FPreg);
2469}
2470
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002471SDOperand PPCTargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
Dan Gohman034f60e2008-03-11 01:59:03 +00002472 // Don't handle ppc_fp128 here; let it be lowered to a libcall.
2473 if (Op.getValueType() != MVT::f32 && Op.getValueType() != MVT::f64)
2474 return SDOperand();
2475
Chris Lattner1a635d62006-04-14 06:01:58 +00002476 if (Op.getOperand(0).getValueType() == MVT::i64) {
2477 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, Op.getOperand(0));
2478 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Bits);
2479 if (Op.getValueType() == MVT::f32)
Chris Lattner0bd48932008-01-17 07:00:52 +00002480 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP, DAG.getIntPtrConstant(0));
Chris Lattner1a635d62006-04-14 06:01:58 +00002481 return FP;
2482 }
2483
2484 assert(Op.getOperand(0).getValueType() == MVT::i32 &&
2485 "Unhandled SINT_TO_FP type in custom expander!");
2486 // Since we only generate this in 64-bit mode, we can take advantage of
2487 // 64-bit registers. In particular, sign extend the input value into the
2488 // 64-bit register with extsw, store the WHOLE 64-bit value into the stack
2489 // then lfd it and fcfid it.
2490 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
2491 int FrameIdx = FrameInfo->CreateStackObject(8, 8);
Chris Lattner0d72a202006-07-28 16:45:47 +00002492 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
2493 SDOperand FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Chris Lattner1a635d62006-04-14 06:01:58 +00002494
2495 SDOperand Ext64 = DAG.getNode(PPCISD::EXTSW_32, MVT::i32,
2496 Op.getOperand(0));
2497
2498 // STD the extended value into the stack slot.
Dan Gohman36b5c132008-04-07 19:35:22 +00002499 MachineMemOperand MO(PseudoSourceValue::getFixedStack(),
2500 MachineMemOperand::MOStore, FrameIdx, 8, 8);
Chris Lattner1a635d62006-04-14 06:01:58 +00002501 SDOperand Store = DAG.getNode(PPCISD::STD_32, MVT::Other,
2502 DAG.getEntryNode(), Ext64, FIdx,
Dan Gohman69de1932008-02-06 22:27:42 +00002503 DAG.getMemOperand(MO));
Chris Lattner1a635d62006-04-14 06:01:58 +00002504 // Load the value as a double.
Evan Cheng466685d2006-10-09 20:57:25 +00002505 SDOperand Ld = DAG.getLoad(MVT::f64, Store, FIdx, NULL, 0);
Chris Lattner1a635d62006-04-14 06:01:58 +00002506
2507 // FCFID it and return it.
2508 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Ld);
2509 if (Op.getValueType() == MVT::f32)
Chris Lattner0bd48932008-01-17 07:00:52 +00002510 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP, DAG.getIntPtrConstant(0));
Chris Lattner1a635d62006-04-14 06:01:58 +00002511 return FP;
2512}
2513
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002514SDOperand PPCTargetLowering::LowerFLT_ROUNDS_(SDOperand Op, SelectionDAG &DAG) {
Dale Johannesen5c5eb802008-01-18 19:55:37 +00002515 /*
2516 The rounding mode is in bits 30:31 of FPSR, and has the following
2517 settings:
2518 00 Round to nearest
2519 01 Round to 0
2520 10 Round to +inf
2521 11 Round to -inf
2522
2523 FLT_ROUNDS, on the other hand, expects the following:
2524 -1 Undefined
2525 0 Round to 0
2526 1 Round to nearest
2527 2 Round to +inf
2528 3 Round to -inf
2529
2530 To perform the conversion, we do:
2531 ((FPSCR & 0x3) ^ ((~FPSCR & 0x3) >> 1))
2532 */
2533
2534 MachineFunction &MF = DAG.getMachineFunction();
2535 MVT::ValueType VT = Op.getValueType();
2536 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
2537 std::vector<MVT::ValueType> NodeTys;
2538 SDOperand MFFSreg, InFlag;
2539
2540 // Save FP Control Word to register
2541 NodeTys.push_back(MVT::f64); // return register
2542 NodeTys.push_back(MVT::Flag); // unused in this context
2543 SDOperand Chain = DAG.getNode(PPCISD::MFFS, NodeTys, &InFlag, 0);
2544
2545 // Save FP register to stack slot
2546 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
2547 SDOperand StackSlot = DAG.getFrameIndex(SSFI, PtrVT);
2548 SDOperand Store = DAG.getStore(DAG.getEntryNode(), Chain,
2549 StackSlot, NULL, 0);
2550
2551 // Load FP Control Word from low 32 bits of stack slot.
2552 SDOperand Four = DAG.getConstant(4, PtrVT);
2553 SDOperand Addr = DAG.getNode(ISD::ADD, PtrVT, StackSlot, Four);
2554 SDOperand CWD = DAG.getLoad(MVT::i32, Store, Addr, NULL, 0);
2555
2556 // Transform as necessary
2557 SDOperand CWD1 =
2558 DAG.getNode(ISD::AND, MVT::i32,
2559 CWD, DAG.getConstant(3, MVT::i32));
2560 SDOperand CWD2 =
2561 DAG.getNode(ISD::SRL, MVT::i32,
2562 DAG.getNode(ISD::AND, MVT::i32,
2563 DAG.getNode(ISD::XOR, MVT::i32,
2564 CWD, DAG.getConstant(3, MVT::i32)),
2565 DAG.getConstant(3, MVT::i32)),
2566 DAG.getConstant(1, MVT::i8));
2567
2568 SDOperand RetVal =
2569 DAG.getNode(ISD::XOR, MVT::i32, CWD1, CWD2);
2570
2571 return DAG.getNode((MVT::getSizeInBits(VT) < 16 ?
2572 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
2573}
2574
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002575SDOperand PPCTargetLowering::LowerSHL_PARTS(SDOperand Op, SelectionDAG &DAG) {
Dan Gohman9ed06db2008-03-07 20:36:53 +00002576 MVT::ValueType VT = Op.getValueType();
2577 unsigned BitWidth = MVT::getSizeInBits(VT);
2578 assert(Op.getNumOperands() == 3 &&
2579 VT == Op.getOperand(1).getValueType() &&
2580 "Unexpected SHL!");
Chris Lattner1a635d62006-04-14 06:01:58 +00002581
Chris Lattner3fe6c1d2006-09-20 03:47:40 +00002582 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner1a635d62006-04-14 06:01:58 +00002583 // depend on the PPC behavior for oversized shift amounts.
Chris Lattner3fe6c1d2006-09-20 03:47:40 +00002584 SDOperand Lo = Op.getOperand(0);
2585 SDOperand Hi = Op.getOperand(1);
2586 SDOperand Amt = Op.getOperand(2);
Dan Gohman9ed06db2008-03-07 20:36:53 +00002587 MVT::ValueType AmtVT = Amt.getValueType();
Chris Lattner1a635d62006-04-14 06:01:58 +00002588
Dan Gohman9ed06db2008-03-07 20:36:53 +00002589 SDOperand Tmp1 = DAG.getNode(ISD::SUB, AmtVT,
2590 DAG.getConstant(BitWidth, AmtVT), Amt);
2591 SDOperand Tmp2 = DAG.getNode(PPCISD::SHL, VT, Hi, Amt);
2592 SDOperand Tmp3 = DAG.getNode(PPCISD::SRL, VT, Lo, Tmp1);
2593 SDOperand Tmp4 = DAG.getNode(ISD::OR , VT, Tmp2, Tmp3);
2594 SDOperand Tmp5 = DAG.getNode(ISD::ADD, AmtVT, Amt,
2595 DAG.getConstant(-BitWidth, AmtVT));
2596 SDOperand Tmp6 = DAG.getNode(PPCISD::SHL, VT, Lo, Tmp5);
2597 SDOperand OutHi = DAG.getNode(ISD::OR, VT, Tmp4, Tmp6);
2598 SDOperand OutLo = DAG.getNode(PPCISD::SHL, VT, Lo, Amt);
Chris Lattner3fe6c1d2006-09-20 03:47:40 +00002599 SDOperand OutOps[] = { OutLo, OutHi };
Dan Gohman9ed06db2008-03-07 20:36:53 +00002600 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, VT),
Chris Lattner3fe6c1d2006-09-20 03:47:40 +00002601 OutOps, 2);
Chris Lattner1a635d62006-04-14 06:01:58 +00002602}
2603
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002604SDOperand PPCTargetLowering::LowerSRL_PARTS(SDOperand Op, SelectionDAG &DAG) {
Dan Gohman9ed06db2008-03-07 20:36:53 +00002605 MVT::ValueType VT = Op.getValueType();
2606 unsigned BitWidth = MVT::getSizeInBits(VT);
2607 assert(Op.getNumOperands() == 3 &&
2608 VT == Op.getOperand(1).getValueType() &&
2609 "Unexpected SRL!");
Chris Lattner1a635d62006-04-14 06:01:58 +00002610
Dan Gohman9ed06db2008-03-07 20:36:53 +00002611 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner1a635d62006-04-14 06:01:58 +00002612 // depend on the PPC behavior for oversized shift amounts.
Chris Lattner3fe6c1d2006-09-20 03:47:40 +00002613 SDOperand Lo = Op.getOperand(0);
2614 SDOperand Hi = Op.getOperand(1);
2615 SDOperand Amt = Op.getOperand(2);
Dan Gohman9ed06db2008-03-07 20:36:53 +00002616 MVT::ValueType AmtVT = Amt.getValueType();
Chris Lattner1a635d62006-04-14 06:01:58 +00002617
Dan Gohman9ed06db2008-03-07 20:36:53 +00002618 SDOperand Tmp1 = DAG.getNode(ISD::SUB, AmtVT,
2619 DAG.getConstant(BitWidth, AmtVT), Amt);
2620 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, VT, Lo, Amt);
2621 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, VT, Hi, Tmp1);
2622 SDOperand Tmp4 = DAG.getNode(ISD::OR , VT, Tmp2, Tmp3);
2623 SDOperand Tmp5 = DAG.getNode(ISD::ADD, AmtVT, Amt,
2624 DAG.getConstant(-BitWidth, AmtVT));
2625 SDOperand Tmp6 = DAG.getNode(PPCISD::SRL, VT, Hi, Tmp5);
2626 SDOperand OutLo = DAG.getNode(ISD::OR, VT, Tmp4, Tmp6);
2627 SDOperand OutHi = DAG.getNode(PPCISD::SRL, VT, Hi, Amt);
Chris Lattner3fe6c1d2006-09-20 03:47:40 +00002628 SDOperand OutOps[] = { OutLo, OutHi };
Dan Gohman9ed06db2008-03-07 20:36:53 +00002629 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, VT),
Chris Lattner3fe6c1d2006-09-20 03:47:40 +00002630 OutOps, 2);
Chris Lattner1a635d62006-04-14 06:01:58 +00002631}
2632
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002633SDOperand PPCTargetLowering::LowerSRA_PARTS(SDOperand Op, SelectionDAG &DAG) {
Dan Gohman9ed06db2008-03-07 20:36:53 +00002634 MVT::ValueType VT = Op.getValueType();
2635 unsigned BitWidth = MVT::getSizeInBits(VT);
2636 assert(Op.getNumOperands() == 3 &&
2637 VT == Op.getOperand(1).getValueType() &&
2638 "Unexpected SRA!");
Chris Lattner1a635d62006-04-14 06:01:58 +00002639
Dan Gohman9ed06db2008-03-07 20:36:53 +00002640 // Expand into a bunch of logical ops, followed by a select_cc.
Chris Lattner3fe6c1d2006-09-20 03:47:40 +00002641 SDOperand Lo = Op.getOperand(0);
2642 SDOperand Hi = Op.getOperand(1);
2643 SDOperand Amt = Op.getOperand(2);
Dan Gohman9ed06db2008-03-07 20:36:53 +00002644 MVT::ValueType AmtVT = Amt.getValueType();
Chris Lattner1a635d62006-04-14 06:01:58 +00002645
Dan Gohman9ed06db2008-03-07 20:36:53 +00002646 SDOperand Tmp1 = DAG.getNode(ISD::SUB, AmtVT,
2647 DAG.getConstant(BitWidth, AmtVT), Amt);
2648 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, VT, Lo, Amt);
2649 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, VT, Hi, Tmp1);
2650 SDOperand Tmp4 = DAG.getNode(ISD::OR , VT, Tmp2, Tmp3);
2651 SDOperand Tmp5 = DAG.getNode(ISD::ADD, AmtVT, Amt,
2652 DAG.getConstant(-BitWidth, AmtVT));
2653 SDOperand Tmp6 = DAG.getNode(PPCISD::SRA, VT, Hi, Tmp5);
2654 SDOperand OutHi = DAG.getNode(PPCISD::SRA, VT, Hi, Amt);
2655 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, AmtVT),
Chris Lattner1a635d62006-04-14 06:01:58 +00002656 Tmp4, Tmp6, ISD::SETLE);
Chris Lattner3fe6c1d2006-09-20 03:47:40 +00002657 SDOperand OutOps[] = { OutLo, OutHi };
Dan Gohman9ed06db2008-03-07 20:36:53 +00002658 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, VT),
Chris Lattner3fe6c1d2006-09-20 03:47:40 +00002659 OutOps, 2);
Chris Lattner1a635d62006-04-14 06:01:58 +00002660}
2661
2662//===----------------------------------------------------------------------===//
2663// Vector related lowering.
2664//
2665
Chris Lattnerac225ca2006-04-12 19:07:14 +00002666// If this is a vector of constants or undefs, get the bits. A bit in
2667// UndefBits is set if the corresponding element of the vector is an
2668// ISD::UNDEF value. For undefs, the corresponding VectorBits values are
2669// zero. Return true if this is not an array of constants, false if it is.
2670//
Chris Lattnerac225ca2006-04-12 19:07:14 +00002671static bool GetConstantBuildVectorBits(SDNode *BV, uint64_t VectorBits[2],
2672 uint64_t UndefBits[2]) {
2673 // Start with zero'd results.
2674 VectorBits[0] = VectorBits[1] = UndefBits[0] = UndefBits[1] = 0;
2675
2676 unsigned EltBitSize = MVT::getSizeInBits(BV->getOperand(0).getValueType());
2677 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
2678 SDOperand OpVal = BV->getOperand(i);
2679
2680 unsigned PartNo = i >= e/2; // In the upper 128 bits?
Chris Lattnerb17f1672006-04-16 01:01:29 +00002681 unsigned SlotNo = e/2 - (i & (e/2-1))-1; // Which subpiece of the uint64_t.
Chris Lattnerac225ca2006-04-12 19:07:14 +00002682
2683 uint64_t EltBits = 0;
2684 if (OpVal.getOpcode() == ISD::UNDEF) {
2685 uint64_t EltUndefBits = ~0U >> (32-EltBitSize);
2686 UndefBits[PartNo] |= EltUndefBits << (SlotNo*EltBitSize);
2687 continue;
2688 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
2689 EltBits = CN->getValue() & (~0U >> (32-EltBitSize));
2690 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) {
2691 assert(CN->getValueType(0) == MVT::f32 &&
2692 "Only one legal FP vector type!");
Dale Johanneseneaf08942007-08-31 04:03:46 +00002693 EltBits = FloatToBits(CN->getValueAPF().convertToFloat());
Chris Lattnerac225ca2006-04-12 19:07:14 +00002694 } else {
2695 // Nonconstant element.
2696 return true;
2697 }
2698
2699 VectorBits[PartNo] |= EltBits << (SlotNo*EltBitSize);
2700 }
2701
2702 //printf("%llx %llx %llx %llx\n",
2703 // VectorBits[0], VectorBits[1], UndefBits[0], UndefBits[1]);
2704 return false;
2705}
Chris Lattneref819f82006-03-20 06:33:01 +00002706
Chris Lattnerb17f1672006-04-16 01:01:29 +00002707// If this is a splat (repetition) of a value across the whole vector, return
2708// the smallest size that splats it. For example, "0x01010101010101..." is a
2709// splat of 0x01, 0x0101, and 0x01010101. We return SplatBits = 0x01 and
2710// SplatSize = 1 byte.
2711static bool isConstantSplat(const uint64_t Bits128[2],
2712 const uint64_t Undef128[2],
2713 unsigned &SplatBits, unsigned &SplatUndef,
2714 unsigned &SplatSize) {
2715
2716 // Don't let undefs prevent splats from matching. See if the top 64-bits are
2717 // the same as the lower 64-bits, ignoring undefs.
2718 if ((Bits128[0] & ~Undef128[1]) != (Bits128[1] & ~Undef128[0]))
2719 return false; // Can't be a splat if two pieces don't match.
2720
2721 uint64_t Bits64 = Bits128[0] | Bits128[1];
2722 uint64_t Undef64 = Undef128[0] & Undef128[1];
2723
2724 // Check that the top 32-bits are the same as the lower 32-bits, ignoring
2725 // undefs.
2726 if ((Bits64 & (~Undef64 >> 32)) != ((Bits64 >> 32) & ~Undef64))
2727 return false; // Can't be a splat if two pieces don't match.
2728
2729 uint32_t Bits32 = uint32_t(Bits64) | uint32_t(Bits64 >> 32);
2730 uint32_t Undef32 = uint32_t(Undef64) & uint32_t(Undef64 >> 32);
2731
2732 // If the top 16-bits are different than the lower 16-bits, ignoring
2733 // undefs, we have an i32 splat.
2734 if ((Bits32 & (~Undef32 >> 16)) != ((Bits32 >> 16) & ~Undef32)) {
2735 SplatBits = Bits32;
2736 SplatUndef = Undef32;
2737 SplatSize = 4;
2738 return true;
2739 }
2740
2741 uint16_t Bits16 = uint16_t(Bits32) | uint16_t(Bits32 >> 16);
2742 uint16_t Undef16 = uint16_t(Undef32) & uint16_t(Undef32 >> 16);
2743
2744 // If the top 8-bits are different than the lower 8-bits, ignoring
2745 // undefs, we have an i16 splat.
2746 if ((Bits16 & (uint16_t(~Undef16) >> 8)) != ((Bits16 >> 8) & ~Undef16)) {
2747 SplatBits = Bits16;
2748 SplatUndef = Undef16;
2749 SplatSize = 2;
2750 return true;
2751 }
2752
2753 // Otherwise, we have an 8-bit splat.
2754 SplatBits = uint8_t(Bits16) | uint8_t(Bits16 >> 8);
2755 SplatUndef = uint8_t(Undef16) & uint8_t(Undef16 >> 8);
2756 SplatSize = 1;
2757 return true;
2758}
2759
Chris Lattner4a998b92006-04-17 06:00:21 +00002760/// BuildSplatI - Build a canonical splati of Val with an element size of
2761/// SplatSize. Cast the result to VT.
2762static SDOperand BuildSplatI(int Val, unsigned SplatSize, MVT::ValueType VT,
2763 SelectionDAG &DAG) {
2764 assert(Val >= -16 && Val <= 15 && "vsplti is out of range!");
Chris Lattner70fa4932006-12-01 01:45:39 +00002765
Chris Lattner4a998b92006-04-17 06:00:21 +00002766 static const MVT::ValueType VTys[] = { // canonical VT to use for each size.
2767 MVT::v16i8, MVT::v8i16, MVT::Other, MVT::v4i32
2768 };
Chris Lattner70fa4932006-12-01 01:45:39 +00002769
2770 MVT::ValueType ReqVT = VT != MVT::Other ? VT : VTys[SplatSize-1];
2771
2772 // Force vspltis[hw] -1 to vspltisb -1 to canonicalize.
2773 if (Val == -1)
2774 SplatSize = 1;
2775
Chris Lattner4a998b92006-04-17 06:00:21 +00002776 MVT::ValueType CanonicalVT = VTys[SplatSize-1];
2777
2778 // Build a canonical splat for this value.
Dan Gohman51eaa862007-06-14 22:58:02 +00002779 SDOperand Elt = DAG.getConstant(Val, MVT::getVectorElementType(CanonicalVT));
Chris Lattnere2199452006-08-11 17:38:39 +00002780 SmallVector<SDOperand, 8> Ops;
2781 Ops.assign(MVT::getVectorNumElements(CanonicalVT), Elt);
2782 SDOperand Res = DAG.getNode(ISD::BUILD_VECTOR, CanonicalVT,
2783 &Ops[0], Ops.size());
Chris Lattner70fa4932006-12-01 01:45:39 +00002784 return DAG.getNode(ISD::BIT_CONVERT, ReqVT, Res);
Chris Lattner4a998b92006-04-17 06:00:21 +00002785}
2786
Chris Lattnere7c768e2006-04-18 03:24:30 +00002787/// BuildIntrinsicOp - Return a binary operator intrinsic node with the
Chris Lattner6876e662006-04-17 06:58:41 +00002788/// specified intrinsic ID.
Chris Lattnere7c768e2006-04-18 03:24:30 +00002789static SDOperand BuildIntrinsicOp(unsigned IID, SDOperand LHS, SDOperand RHS,
2790 SelectionDAG &DAG,
2791 MVT::ValueType DestVT = MVT::Other) {
2792 if (DestVT == MVT::Other) DestVT = LHS.getValueType();
2793 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DestVT,
Chris Lattner6876e662006-04-17 06:58:41 +00002794 DAG.getConstant(IID, MVT::i32), LHS, RHS);
2795}
2796
Chris Lattnere7c768e2006-04-18 03:24:30 +00002797/// BuildIntrinsicOp - Return a ternary operator intrinsic node with the
2798/// specified intrinsic ID.
2799static SDOperand BuildIntrinsicOp(unsigned IID, SDOperand Op0, SDOperand Op1,
2800 SDOperand Op2, SelectionDAG &DAG,
2801 MVT::ValueType DestVT = MVT::Other) {
2802 if (DestVT == MVT::Other) DestVT = Op0.getValueType();
2803 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DestVT,
2804 DAG.getConstant(IID, MVT::i32), Op0, Op1, Op2);
2805}
2806
2807
Chris Lattnerbdd558c2006-04-17 17:55:10 +00002808/// BuildVSLDOI - Return a VECTOR_SHUFFLE that is a vsldoi of the specified
2809/// amount. The result has the specified value type.
2810static SDOperand BuildVSLDOI(SDOperand LHS, SDOperand RHS, unsigned Amt,
2811 MVT::ValueType VT, SelectionDAG &DAG) {
2812 // Force LHS/RHS to be the right type.
2813 LHS = DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, LHS);
2814 RHS = DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, RHS);
2815
Chris Lattnere2199452006-08-11 17:38:39 +00002816 SDOperand Ops[16];
Chris Lattnerbdd558c2006-04-17 17:55:10 +00002817 for (unsigned i = 0; i != 16; ++i)
Chris Lattnere2199452006-08-11 17:38:39 +00002818 Ops[i] = DAG.getConstant(i+Amt, MVT::i32);
Chris Lattnerbdd558c2006-04-17 17:55:10 +00002819 SDOperand T = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v16i8, LHS, RHS,
Chris Lattnere2199452006-08-11 17:38:39 +00002820 DAG.getNode(ISD::BUILD_VECTOR, MVT::v16i8, Ops,16));
Chris Lattnerbdd558c2006-04-17 17:55:10 +00002821 return DAG.getNode(ISD::BIT_CONVERT, VT, T);
2822}
2823
Chris Lattnerf1b47082006-04-14 05:19:18 +00002824// If this is a case we can't handle, return null and let the default
2825// expansion code take care of it. If we CAN select this case, and if it
2826// selects to a single instruction, return Op. Otherwise, if we can codegen
2827// this case more efficiently than a constant pool load, lower it to the
2828// sequence of ops that should be used.
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002829SDOperand PPCTargetLowering::LowerBUILD_VECTOR(SDOperand Op,
2830 SelectionDAG &DAG) {
Chris Lattnerf1b47082006-04-14 05:19:18 +00002831 // If this is a vector of constants or undefs, get the bits. A bit in
2832 // UndefBits is set if the corresponding element of the vector is an
2833 // ISD::UNDEF value. For undefs, the corresponding VectorBits values are
2834 // zero.
2835 uint64_t VectorBits[2];
2836 uint64_t UndefBits[2];
2837 if (GetConstantBuildVectorBits(Op.Val, VectorBits, UndefBits))
2838 return SDOperand(); // Not a constant vector.
2839
Chris Lattnerb17f1672006-04-16 01:01:29 +00002840 // If this is a splat (repetition) of a value across the whole vector, return
2841 // the smallest size that splats it. For example, "0x01010101010101..." is a
2842 // splat of 0x01, 0x0101, and 0x01010101. We return SplatBits = 0x01 and
2843 // SplatSize = 1 byte.
2844 unsigned SplatBits, SplatUndef, SplatSize;
2845 if (isConstantSplat(VectorBits, UndefBits, SplatBits, SplatUndef, SplatSize)){
2846 bool HasAnyUndefs = (UndefBits[0] | UndefBits[1]) != 0;
2847
2848 // First, handle single instruction cases.
2849
2850 // All zeros?
2851 if (SplatBits == 0) {
2852 // Canonicalize all zero vectors to be v4i32.
2853 if (Op.getValueType() != MVT::v4i32 || HasAnyUndefs) {
2854 SDOperand Z = DAG.getConstant(0, MVT::i32);
2855 Z = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Z, Z, Z, Z);
2856 Op = DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Z);
2857 }
2858 return Op;
Chris Lattnerf1b47082006-04-14 05:19:18 +00002859 }
Chris Lattnerb17f1672006-04-16 01:01:29 +00002860
2861 // If the sign extended value is in the range [-16,15], use VSPLTI[bhw].
2862 int32_t SextVal= int32_t(SplatBits << (32-8*SplatSize)) >> (32-8*SplatSize);
Chris Lattner4a998b92006-04-17 06:00:21 +00002863 if (SextVal >= -16 && SextVal <= 15)
2864 return BuildSplatI(SextVal, SplatSize, Op.getValueType(), DAG);
Chris Lattnerb17f1672006-04-16 01:01:29 +00002865
Chris Lattnerdbce85d2006-04-17 18:09:22 +00002866
2867 // Two instruction sequences.
2868
Chris Lattner4a998b92006-04-17 06:00:21 +00002869 // If this value is in the range [-32,30] and is even, use:
2870 // tmp = VSPLTI[bhw], result = add tmp, tmp
2871 if (SextVal >= -32 && SextVal <= 30 && (SextVal & 1) == 0) {
2872 Op = BuildSplatI(SextVal >> 1, SplatSize, Op.getValueType(), DAG);
2873 return DAG.getNode(ISD::ADD, Op.getValueType(), Op, Op);
2874 }
Chris Lattner6876e662006-04-17 06:58:41 +00002875
2876 // If this is 0x8000_0000 x 4, turn into vspltisw + vslw. If it is
2877 // 0x7FFF_FFFF x 4, turn it into not(0x8000_0000). This is important
2878 // for fneg/fabs.
2879 if (SplatSize == 4 && SplatBits == (0x7FFFFFFF&~SplatUndef)) {
2880 // Make -1 and vspltisw -1:
2881 SDOperand OnesV = BuildSplatI(-1, 4, MVT::v4i32, DAG);
2882
2883 // Make the VSLW intrinsic, computing 0x8000_0000.
Chris Lattnere7c768e2006-04-18 03:24:30 +00002884 SDOperand Res = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, OnesV,
2885 OnesV, DAG);
Chris Lattner6876e662006-04-17 06:58:41 +00002886
2887 // xor by OnesV to invert it.
2888 Res = DAG.getNode(ISD::XOR, MVT::v4i32, Res, OnesV);
2889 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Res);
2890 }
2891
2892 // Check to see if this is a wide variety of vsplti*, binop self cases.
2893 unsigned SplatBitSize = SplatSize*8;
Lauro Ramos Venancio1baa1972007-03-27 16:33:08 +00002894 static const signed char SplatCsts[] = {
Chris Lattner6876e662006-04-17 06:58:41 +00002895 -1, 1, -2, 2, -3, 3, -4, 4, -5, 5, -6, 6, -7, 7,
Chris Lattnerdbce85d2006-04-17 18:09:22 +00002896 -8, 8, -9, 9, -10, 10, -11, 11, -12, 12, -13, 13, 14, -14, 15, -15, -16
Chris Lattner6876e662006-04-17 06:58:41 +00002897 };
Chris Lattner15eb3292006-11-29 19:58:49 +00002898
Owen Anderson718cb662007-09-07 04:06:50 +00002899 for (unsigned idx = 0; idx < array_lengthof(SplatCsts); ++idx) {
Chris Lattner6876e662006-04-17 06:58:41 +00002900 // Indirect through the SplatCsts array so that we favor 'vsplti -1' for
2901 // cases which are ambiguous (e.g. formation of 0x8000_0000). 'vsplti -1'
2902 int i = SplatCsts[idx];
2903
2904 // Figure out what shift amount will be used by altivec if shifted by i in
2905 // this splat size.
2906 unsigned TypeShiftAmt = i & (SplatBitSize-1);
2907
2908 // vsplti + shl self.
2909 if (SextVal == (i << (int)TypeShiftAmt)) {
Chris Lattner15eb3292006-11-29 19:58:49 +00002910 SDOperand Res = BuildSplatI(i, SplatSize, MVT::Other, DAG);
Chris Lattner6876e662006-04-17 06:58:41 +00002911 static const unsigned IIDs[] = { // Intrinsic to use for each size.
2912 Intrinsic::ppc_altivec_vslb, Intrinsic::ppc_altivec_vslh, 0,
2913 Intrinsic::ppc_altivec_vslw
2914 };
Chris Lattner15eb3292006-11-29 19:58:49 +00002915 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG);
2916 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Res);
Chris Lattner6876e662006-04-17 06:58:41 +00002917 }
2918
2919 // vsplti + srl self.
2920 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
Chris Lattner15eb3292006-11-29 19:58:49 +00002921 SDOperand Res = BuildSplatI(i, SplatSize, MVT::Other, DAG);
Chris Lattner6876e662006-04-17 06:58:41 +00002922 static const unsigned IIDs[] = { // Intrinsic to use for each size.
2923 Intrinsic::ppc_altivec_vsrb, Intrinsic::ppc_altivec_vsrh, 0,
2924 Intrinsic::ppc_altivec_vsrw
2925 };
Chris Lattner15eb3292006-11-29 19:58:49 +00002926 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG);
2927 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Res);
Chris Lattner6876e662006-04-17 06:58:41 +00002928 }
2929
2930 // vsplti + sra self.
2931 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
Chris Lattner15eb3292006-11-29 19:58:49 +00002932 SDOperand Res = BuildSplatI(i, SplatSize, MVT::Other, DAG);
Chris Lattner6876e662006-04-17 06:58:41 +00002933 static const unsigned IIDs[] = { // Intrinsic to use for each size.
2934 Intrinsic::ppc_altivec_vsrab, Intrinsic::ppc_altivec_vsrah, 0,
2935 Intrinsic::ppc_altivec_vsraw
2936 };
Chris Lattner15eb3292006-11-29 19:58:49 +00002937 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG);
2938 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Res);
Chris Lattner6876e662006-04-17 06:58:41 +00002939 }
2940
Chris Lattnerbdd558c2006-04-17 17:55:10 +00002941 // vsplti + rol self.
2942 if (SextVal == (int)(((unsigned)i << TypeShiftAmt) |
2943 ((unsigned)i >> (SplatBitSize-TypeShiftAmt)))) {
Chris Lattner15eb3292006-11-29 19:58:49 +00002944 SDOperand Res = BuildSplatI(i, SplatSize, MVT::Other, DAG);
Chris Lattnerbdd558c2006-04-17 17:55:10 +00002945 static const unsigned IIDs[] = { // Intrinsic to use for each size.
2946 Intrinsic::ppc_altivec_vrlb, Intrinsic::ppc_altivec_vrlh, 0,
2947 Intrinsic::ppc_altivec_vrlw
2948 };
Chris Lattner15eb3292006-11-29 19:58:49 +00002949 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG);
2950 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Res);
Chris Lattnerbdd558c2006-04-17 17:55:10 +00002951 }
2952
2953 // t = vsplti c, result = vsldoi t, t, 1
2954 if (SextVal == ((i << 8) | (i >> (TypeShiftAmt-8)))) {
2955 SDOperand T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG);
2956 return BuildVSLDOI(T, T, 1, Op.getValueType(), DAG);
2957 }
2958 // t = vsplti c, result = vsldoi t, t, 2
2959 if (SextVal == ((i << 16) | (i >> (TypeShiftAmt-16)))) {
2960 SDOperand T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG);
2961 return BuildVSLDOI(T, T, 2, Op.getValueType(), DAG);
2962 }
2963 // t = vsplti c, result = vsldoi t, t, 3
2964 if (SextVal == ((i << 24) | (i >> (TypeShiftAmt-24)))) {
2965 SDOperand T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG);
2966 return BuildVSLDOI(T, T, 3, Op.getValueType(), DAG);
2967 }
Chris Lattner6876e662006-04-17 06:58:41 +00002968 }
2969
Chris Lattner6876e662006-04-17 06:58:41 +00002970 // Three instruction sequences.
2971
Chris Lattnerdbce85d2006-04-17 18:09:22 +00002972 // Odd, in range [17,31]: (vsplti C)-(vsplti -16).
2973 if (SextVal >= 0 && SextVal <= 31) {
Chris Lattner15eb3292006-11-29 19:58:49 +00002974 SDOperand LHS = BuildSplatI(SextVal-16, SplatSize, MVT::Other, DAG);
2975 SDOperand RHS = BuildSplatI(-16, SplatSize, MVT::Other, DAG);
Dale Johannesen296c1762007-10-14 01:58:32 +00002976 LHS = DAG.getNode(ISD::SUB, LHS.getValueType(), LHS, RHS);
Chris Lattner15eb3292006-11-29 19:58:49 +00002977 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), LHS);
Chris Lattnerdbce85d2006-04-17 18:09:22 +00002978 }
2979 // Odd, in range [-31,-17]: (vsplti C)+(vsplti -16).
2980 if (SextVal >= -31 && SextVal <= 0) {
Chris Lattner15eb3292006-11-29 19:58:49 +00002981 SDOperand LHS = BuildSplatI(SextVal+16, SplatSize, MVT::Other, DAG);
2982 SDOperand RHS = BuildSplatI(-16, SplatSize, MVT::Other, DAG);
Dale Johannesen296c1762007-10-14 01:58:32 +00002983 LHS = DAG.getNode(ISD::ADD, LHS.getValueType(), LHS, RHS);
Chris Lattner15eb3292006-11-29 19:58:49 +00002984 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), LHS);
Chris Lattnerf1b47082006-04-14 05:19:18 +00002985 }
2986 }
Chris Lattnerb17f1672006-04-16 01:01:29 +00002987
Chris Lattnerf1b47082006-04-14 05:19:18 +00002988 return SDOperand();
2989}
2990
Chris Lattner59138102006-04-17 05:28:54 +00002991/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
2992/// the specified operations to build the shuffle.
2993static SDOperand GeneratePerfectShuffle(unsigned PFEntry, SDOperand LHS,
2994 SDOperand RHS, SelectionDAG &DAG) {
2995 unsigned OpNum = (PFEntry >> 26) & 0x0F;
2996 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
2997 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
2998
2999 enum {
Chris Lattner00402c72006-05-16 04:20:24 +00003000 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 +00003001 OP_VMRGHW,
3002 OP_VMRGLW,
3003 OP_VSPLTISW0,
3004 OP_VSPLTISW1,
3005 OP_VSPLTISW2,
3006 OP_VSPLTISW3,
3007 OP_VSLDOI4,
3008 OP_VSLDOI8,
Chris Lattnerd74ea2b2006-05-24 17:04:05 +00003009 OP_VSLDOI12
Chris Lattner59138102006-04-17 05:28:54 +00003010 };
3011
3012 if (OpNum == OP_COPY) {
3013 if (LHSID == (1*9+2)*9+3) return LHS;
3014 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
3015 return RHS;
3016 }
3017
Chris Lattnerbdd558c2006-04-17 17:55:10 +00003018 SDOperand OpLHS, OpRHS;
3019 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG);
3020 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG);
3021
Chris Lattner59138102006-04-17 05:28:54 +00003022 unsigned ShufIdxs[16];
3023 switch (OpNum) {
3024 default: assert(0 && "Unknown i32 permute!");
3025 case OP_VMRGHW:
3026 ShufIdxs[ 0] = 0; ShufIdxs[ 1] = 1; ShufIdxs[ 2] = 2; ShufIdxs[ 3] = 3;
3027 ShufIdxs[ 4] = 16; ShufIdxs[ 5] = 17; ShufIdxs[ 6] = 18; ShufIdxs[ 7] = 19;
3028 ShufIdxs[ 8] = 4; ShufIdxs[ 9] = 5; ShufIdxs[10] = 6; ShufIdxs[11] = 7;
3029 ShufIdxs[12] = 20; ShufIdxs[13] = 21; ShufIdxs[14] = 22; ShufIdxs[15] = 23;
3030 break;
3031 case OP_VMRGLW:
3032 ShufIdxs[ 0] = 8; ShufIdxs[ 1] = 9; ShufIdxs[ 2] = 10; ShufIdxs[ 3] = 11;
3033 ShufIdxs[ 4] = 24; ShufIdxs[ 5] = 25; ShufIdxs[ 6] = 26; ShufIdxs[ 7] = 27;
3034 ShufIdxs[ 8] = 12; ShufIdxs[ 9] = 13; ShufIdxs[10] = 14; ShufIdxs[11] = 15;
3035 ShufIdxs[12] = 28; ShufIdxs[13] = 29; ShufIdxs[14] = 30; ShufIdxs[15] = 31;
3036 break;
3037 case OP_VSPLTISW0:
3038 for (unsigned i = 0; i != 16; ++i)
3039 ShufIdxs[i] = (i&3)+0;
3040 break;
3041 case OP_VSPLTISW1:
3042 for (unsigned i = 0; i != 16; ++i)
3043 ShufIdxs[i] = (i&3)+4;
3044 break;
3045 case OP_VSPLTISW2:
3046 for (unsigned i = 0; i != 16; ++i)
3047 ShufIdxs[i] = (i&3)+8;
3048 break;
3049 case OP_VSPLTISW3:
3050 for (unsigned i = 0; i != 16; ++i)
3051 ShufIdxs[i] = (i&3)+12;
3052 break;
3053 case OP_VSLDOI4:
Chris Lattnerbdd558c2006-04-17 17:55:10 +00003054 return BuildVSLDOI(OpLHS, OpRHS, 4, OpLHS.getValueType(), DAG);
Chris Lattner59138102006-04-17 05:28:54 +00003055 case OP_VSLDOI8:
Chris Lattnerbdd558c2006-04-17 17:55:10 +00003056 return BuildVSLDOI(OpLHS, OpRHS, 8, OpLHS.getValueType(), DAG);
Chris Lattner59138102006-04-17 05:28:54 +00003057 case OP_VSLDOI12:
Chris Lattnerbdd558c2006-04-17 17:55:10 +00003058 return BuildVSLDOI(OpLHS, OpRHS, 12, OpLHS.getValueType(), DAG);
Chris Lattner59138102006-04-17 05:28:54 +00003059 }
Chris Lattnere2199452006-08-11 17:38:39 +00003060 SDOperand Ops[16];
Chris Lattner59138102006-04-17 05:28:54 +00003061 for (unsigned i = 0; i != 16; ++i)
Chris Lattnere2199452006-08-11 17:38:39 +00003062 Ops[i] = DAG.getConstant(ShufIdxs[i], MVT::i32);
Chris Lattner59138102006-04-17 05:28:54 +00003063
3064 return DAG.getNode(ISD::VECTOR_SHUFFLE, OpLHS.getValueType(), OpLHS, OpRHS,
Chris Lattnere2199452006-08-11 17:38:39 +00003065 DAG.getNode(ISD::BUILD_VECTOR, MVT::v16i8, Ops, 16));
Chris Lattner59138102006-04-17 05:28:54 +00003066}
3067
Chris Lattnerf1b47082006-04-14 05:19:18 +00003068/// LowerVECTOR_SHUFFLE - Return the code we lower for VECTOR_SHUFFLE. If this
3069/// is a shuffle we can handle in a single instruction, return it. Otherwise,
3070/// return the code it can be lowered into. Worst case, it can always be
3071/// lowered into a vperm.
Dale Johannesen5b3b6952008-03-04 23:17:14 +00003072SDOperand PPCTargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op,
3073 SelectionDAG &DAG) {
Chris Lattnerf1b47082006-04-14 05:19:18 +00003074 SDOperand V1 = Op.getOperand(0);
3075 SDOperand V2 = Op.getOperand(1);
3076 SDOperand PermMask = Op.getOperand(2);
3077
3078 // Cases that are handled by instructions that take permute immediates
3079 // (such as vsplt*) should be left as VECTOR_SHUFFLE nodes so they can be
3080 // selected by the instruction selector.
3081 if (V2.getOpcode() == ISD::UNDEF) {
3082 if (PPC::isSplatShuffleMask(PermMask.Val, 1) ||
3083 PPC::isSplatShuffleMask(PermMask.Val, 2) ||
3084 PPC::isSplatShuffleMask(PermMask.Val, 4) ||
3085 PPC::isVPKUWUMShuffleMask(PermMask.Val, true) ||
3086 PPC::isVPKUHUMShuffleMask(PermMask.Val, true) ||
3087 PPC::isVSLDOIShuffleMask(PermMask.Val, true) != -1 ||
3088 PPC::isVMRGLShuffleMask(PermMask.Val, 1, true) ||
3089 PPC::isVMRGLShuffleMask(PermMask.Val, 2, true) ||
3090 PPC::isVMRGLShuffleMask(PermMask.Val, 4, true) ||
3091 PPC::isVMRGHShuffleMask(PermMask.Val, 1, true) ||
3092 PPC::isVMRGHShuffleMask(PermMask.Val, 2, true) ||
3093 PPC::isVMRGHShuffleMask(PermMask.Val, 4, true)) {
3094 return Op;
3095 }
3096 }
3097
3098 // Altivec has a variety of "shuffle immediates" that take two vector inputs
3099 // and produce a fixed permutation. If any of these match, do not lower to
3100 // VPERM.
3101 if (PPC::isVPKUWUMShuffleMask(PermMask.Val, false) ||
3102 PPC::isVPKUHUMShuffleMask(PermMask.Val, false) ||
3103 PPC::isVSLDOIShuffleMask(PermMask.Val, false) != -1 ||
3104 PPC::isVMRGLShuffleMask(PermMask.Val, 1, false) ||
3105 PPC::isVMRGLShuffleMask(PermMask.Val, 2, false) ||
3106 PPC::isVMRGLShuffleMask(PermMask.Val, 4, false) ||
3107 PPC::isVMRGHShuffleMask(PermMask.Val, 1, false) ||
3108 PPC::isVMRGHShuffleMask(PermMask.Val, 2, false) ||
3109 PPC::isVMRGHShuffleMask(PermMask.Val, 4, false))
3110 return Op;
3111
Chris Lattner59138102006-04-17 05:28:54 +00003112 // Check to see if this is a shuffle of 4-byte values. If so, we can use our
3113 // perfect shuffle table to emit an optimal matching sequence.
3114 unsigned PFIndexes[4];
3115 bool isFourElementShuffle = true;
3116 for (unsigned i = 0; i != 4 && isFourElementShuffle; ++i) { // Element number
3117 unsigned EltNo = 8; // Start out undef.
3118 for (unsigned j = 0; j != 4; ++j) { // Intra-element byte.
3119 if (PermMask.getOperand(i*4+j).getOpcode() == ISD::UNDEF)
3120 continue; // Undef, ignore it.
3121
3122 unsigned ByteSource =
3123 cast<ConstantSDNode>(PermMask.getOperand(i*4+j))->getValue();
3124 if ((ByteSource & 3) != j) {
3125 isFourElementShuffle = false;
3126 break;
3127 }
3128
3129 if (EltNo == 8) {
3130 EltNo = ByteSource/4;
3131 } else if (EltNo != ByteSource/4) {
3132 isFourElementShuffle = false;
3133 break;
3134 }
3135 }
3136 PFIndexes[i] = EltNo;
3137 }
3138
3139 // If this shuffle can be expressed as a shuffle of 4-byte elements, use the
3140 // perfect shuffle vector to determine if it is cost effective to do this as
3141 // discrete instructions, or whether we should use a vperm.
3142 if (isFourElementShuffle) {
3143 // Compute the index in the perfect shuffle table.
3144 unsigned PFTableIndex =
3145 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
3146
3147 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
3148 unsigned Cost = (PFEntry >> 30);
3149
3150 // Determining when to avoid vperm is tricky. Many things affect the cost
3151 // of vperm, particularly how many times the perm mask needs to be computed.
3152 // For example, if the perm mask can be hoisted out of a loop or is already
3153 // used (perhaps because there are multiple permutes with the same shuffle
3154 // mask?) the vperm has a cost of 1. OTOH, hoisting the permute mask out of
3155 // the loop requires an extra register.
3156 //
3157 // As a compromise, we only emit discrete instructions if the shuffle can be
3158 // generated in 3 or fewer operations. When we have loop information
3159 // available, if this block is within a loop, we should avoid using vperm
3160 // for 3-operation perms and use a constant pool load instead.
3161 if (Cost < 3)
3162 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG);
3163 }
Chris Lattnerf1b47082006-04-14 05:19:18 +00003164
3165 // Lower this to a VPERM(V1, V2, V3) expression, where V3 is a constant
3166 // vector that will get spilled to the constant pool.
3167 if (V2.getOpcode() == ISD::UNDEF) V2 = V1;
3168
3169 // The SHUFFLE_VECTOR mask is almost exactly what we want for vperm, except
3170 // that it is in input element units, not in bytes. Convert now.
Dan Gohman51eaa862007-06-14 22:58:02 +00003171 MVT::ValueType EltVT = MVT::getVectorElementType(V1.getValueType());
Chris Lattnerf1b47082006-04-14 05:19:18 +00003172 unsigned BytesPerElement = MVT::getSizeInBits(EltVT)/8;
3173
Chris Lattnere2199452006-08-11 17:38:39 +00003174 SmallVector<SDOperand, 16> ResultMask;
Chris Lattnerf1b47082006-04-14 05:19:18 +00003175 for (unsigned i = 0, e = PermMask.getNumOperands(); i != e; ++i) {
Chris Lattner730b4562006-04-15 23:48:05 +00003176 unsigned SrcElt;
3177 if (PermMask.getOperand(i).getOpcode() == ISD::UNDEF)
3178 SrcElt = 0;
3179 else
3180 SrcElt = cast<ConstantSDNode>(PermMask.getOperand(i))->getValue();
Chris Lattnerf1b47082006-04-14 05:19:18 +00003181
3182 for (unsigned j = 0; j != BytesPerElement; ++j)
3183 ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement+j,
3184 MVT::i8));
3185 }
3186
Chris Lattnere2199452006-08-11 17:38:39 +00003187 SDOperand VPermMask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v16i8,
3188 &ResultMask[0], ResultMask.size());
Chris Lattnerf1b47082006-04-14 05:19:18 +00003189 return DAG.getNode(PPCISD::VPERM, V1.getValueType(), V1, V2, VPermMask);
3190}
3191
Chris Lattner90564f22006-04-18 17:59:36 +00003192/// getAltivecCompareInfo - Given an intrinsic, return false if it is not an
3193/// altivec comparison. If it is, return true and fill in Opc/isDot with
3194/// information about the intrinsic.
3195static bool getAltivecCompareInfo(SDOperand Intrin, int &CompareOpc,
3196 bool &isDot) {
3197 unsigned IntrinsicID = cast<ConstantSDNode>(Intrin.getOperand(0))->getValue();
3198 CompareOpc = -1;
3199 isDot = false;
3200 switch (IntrinsicID) {
3201 default: return false;
3202 // Comparison predicates.
Chris Lattner1a635d62006-04-14 06:01:58 +00003203 case Intrinsic::ppc_altivec_vcmpbfp_p: CompareOpc = 966; isDot = 1; break;
3204 case Intrinsic::ppc_altivec_vcmpeqfp_p: CompareOpc = 198; isDot = 1; break;
3205 case Intrinsic::ppc_altivec_vcmpequb_p: CompareOpc = 6; isDot = 1; break;
3206 case Intrinsic::ppc_altivec_vcmpequh_p: CompareOpc = 70; isDot = 1; break;
3207 case Intrinsic::ppc_altivec_vcmpequw_p: CompareOpc = 134; isDot = 1; break;
3208 case Intrinsic::ppc_altivec_vcmpgefp_p: CompareOpc = 454; isDot = 1; break;
3209 case Intrinsic::ppc_altivec_vcmpgtfp_p: CompareOpc = 710; isDot = 1; break;
3210 case Intrinsic::ppc_altivec_vcmpgtsb_p: CompareOpc = 774; isDot = 1; break;
3211 case Intrinsic::ppc_altivec_vcmpgtsh_p: CompareOpc = 838; isDot = 1; break;
3212 case Intrinsic::ppc_altivec_vcmpgtsw_p: CompareOpc = 902; isDot = 1; break;
3213 case Intrinsic::ppc_altivec_vcmpgtub_p: CompareOpc = 518; isDot = 1; break;
3214 case Intrinsic::ppc_altivec_vcmpgtuh_p: CompareOpc = 582; isDot = 1; break;
3215 case Intrinsic::ppc_altivec_vcmpgtuw_p: CompareOpc = 646; isDot = 1; break;
3216
3217 // Normal Comparisons.
3218 case Intrinsic::ppc_altivec_vcmpbfp: CompareOpc = 966; isDot = 0; break;
3219 case Intrinsic::ppc_altivec_vcmpeqfp: CompareOpc = 198; isDot = 0; break;
3220 case Intrinsic::ppc_altivec_vcmpequb: CompareOpc = 6; isDot = 0; break;
3221 case Intrinsic::ppc_altivec_vcmpequh: CompareOpc = 70; isDot = 0; break;
3222 case Intrinsic::ppc_altivec_vcmpequw: CompareOpc = 134; isDot = 0; break;
3223 case Intrinsic::ppc_altivec_vcmpgefp: CompareOpc = 454; isDot = 0; break;
3224 case Intrinsic::ppc_altivec_vcmpgtfp: CompareOpc = 710; isDot = 0; break;
3225 case Intrinsic::ppc_altivec_vcmpgtsb: CompareOpc = 774; isDot = 0; break;
3226 case Intrinsic::ppc_altivec_vcmpgtsh: CompareOpc = 838; isDot = 0; break;
3227 case Intrinsic::ppc_altivec_vcmpgtsw: CompareOpc = 902; isDot = 0; break;
3228 case Intrinsic::ppc_altivec_vcmpgtub: CompareOpc = 518; isDot = 0; break;
3229 case Intrinsic::ppc_altivec_vcmpgtuh: CompareOpc = 582; isDot = 0; break;
3230 case Intrinsic::ppc_altivec_vcmpgtuw: CompareOpc = 646; isDot = 0; break;
3231 }
Chris Lattner90564f22006-04-18 17:59:36 +00003232 return true;
3233}
3234
3235/// LowerINTRINSIC_WO_CHAIN - If this is an intrinsic that we want to custom
3236/// lower, do it, otherwise return null.
Dale Johannesen5b3b6952008-03-04 23:17:14 +00003237SDOperand PPCTargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op,
3238 SelectionDAG &DAG) {
Chris Lattner90564f22006-04-18 17:59:36 +00003239 // If this is a lowered altivec predicate compare, CompareOpc is set to the
3240 // opcode number of the comparison.
3241 int CompareOpc;
3242 bool isDot;
3243 if (!getAltivecCompareInfo(Op, CompareOpc, isDot))
3244 return SDOperand(); // Don't custom lower most intrinsics.
Chris Lattner1a635d62006-04-14 06:01:58 +00003245
Chris Lattner90564f22006-04-18 17:59:36 +00003246 // If this is a non-dot comparison, make the VCMP node and we are done.
Chris Lattner1a635d62006-04-14 06:01:58 +00003247 if (!isDot) {
3248 SDOperand Tmp = DAG.getNode(PPCISD::VCMP, Op.getOperand(2).getValueType(),
3249 Op.getOperand(1), Op.getOperand(2),
3250 DAG.getConstant(CompareOpc, MVT::i32));
3251 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Tmp);
3252 }
3253
3254 // Create the PPCISD altivec 'dot' comparison node.
Chris Lattner79e490a2006-08-11 17:18:05 +00003255 SDOperand Ops[] = {
3256 Op.getOperand(2), // LHS
3257 Op.getOperand(3), // RHS
3258 DAG.getConstant(CompareOpc, MVT::i32)
3259 };
Chris Lattner1a635d62006-04-14 06:01:58 +00003260 std::vector<MVT::ValueType> VTs;
Chris Lattner1a635d62006-04-14 06:01:58 +00003261 VTs.push_back(Op.getOperand(2).getValueType());
3262 VTs.push_back(MVT::Flag);
Chris Lattner79e490a2006-08-11 17:18:05 +00003263 SDOperand CompNode = DAG.getNode(PPCISD::VCMPo, VTs, Ops, 3);
Chris Lattner1a635d62006-04-14 06:01:58 +00003264
3265 // Now that we have the comparison, emit a copy from the CR to a GPR.
3266 // This is flagged to the above dot comparison.
3267 SDOperand Flags = DAG.getNode(PPCISD::MFCR, MVT::i32,
3268 DAG.getRegister(PPC::CR6, MVT::i32),
3269 CompNode.getValue(1));
3270
3271 // Unpack the result based on how the target uses it.
3272 unsigned BitNo; // Bit # of CR6.
3273 bool InvertBit; // Invert result?
3274 switch (cast<ConstantSDNode>(Op.getOperand(1))->getValue()) {
3275 default: // Can't happen, don't crash on invalid number though.
3276 case 0: // Return the value of the EQ bit of CR6.
3277 BitNo = 0; InvertBit = false;
3278 break;
3279 case 1: // Return the inverted value of the EQ bit of CR6.
3280 BitNo = 0; InvertBit = true;
3281 break;
3282 case 2: // Return the value of the LT bit of CR6.
3283 BitNo = 2; InvertBit = false;
3284 break;
3285 case 3: // Return the inverted value of the LT bit of CR6.
3286 BitNo = 2; InvertBit = true;
3287 break;
3288 }
3289
3290 // Shift the bit into the low position.
3291 Flags = DAG.getNode(ISD::SRL, MVT::i32, Flags,
3292 DAG.getConstant(8-(3-BitNo), MVT::i32));
3293 // Isolate the bit.
3294 Flags = DAG.getNode(ISD::AND, MVT::i32, Flags,
3295 DAG.getConstant(1, MVT::i32));
3296
3297 // If we are supposed to, toggle the bit.
3298 if (InvertBit)
3299 Flags = DAG.getNode(ISD::XOR, MVT::i32, Flags,
3300 DAG.getConstant(1, MVT::i32));
3301 return Flags;
3302}
3303
Dale Johannesen5b3b6952008-03-04 23:17:14 +00003304SDOperand PPCTargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op,
3305 SelectionDAG &DAG) {
Chris Lattner1a635d62006-04-14 06:01:58 +00003306 // Create a stack slot that is 16-byte aligned.
3307 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
3308 int FrameIdx = FrameInfo->CreateStackObject(16, 16);
Chris Lattner0d72a202006-07-28 16:45:47 +00003309 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
3310 SDOperand FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Chris Lattner1a635d62006-04-14 06:01:58 +00003311
3312 // Store the input value into Value#0 of the stack slot.
Evan Cheng786225a2006-10-05 23:01:46 +00003313 SDOperand Store = DAG.getStore(DAG.getEntryNode(),
Evan Cheng8b2794a2006-10-13 21:14:26 +00003314 Op.getOperand(0), FIdx, NULL, 0);
Chris Lattner1a635d62006-04-14 06:01:58 +00003315 // Load it out.
Evan Cheng466685d2006-10-09 20:57:25 +00003316 return DAG.getLoad(Op.getValueType(), Store, FIdx, NULL, 0);
Chris Lattner1a635d62006-04-14 06:01:58 +00003317}
3318
Dale Johannesen5b3b6952008-03-04 23:17:14 +00003319SDOperand PPCTargetLowering::LowerMUL(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner72dd9bd2006-04-18 03:43:48 +00003320 if (Op.getValueType() == MVT::v4i32) {
3321 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
3322
3323 SDOperand Zero = BuildSplatI( 0, 1, MVT::v4i32, DAG);
3324 SDOperand Neg16 = BuildSplatI(-16, 4, MVT::v4i32, DAG); // +16 as shift amt.
3325
3326 SDOperand RHSSwap = // = vrlw RHS, 16
3327 BuildIntrinsicOp(Intrinsic::ppc_altivec_vrlw, RHS, Neg16, DAG);
3328
3329 // Shrinkify inputs to v8i16.
3330 LHS = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, LHS);
3331 RHS = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, RHS);
3332 RHSSwap = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, RHSSwap);
3333
3334 // Low parts multiplied together, generating 32-bit results (we ignore the
3335 // top parts).
3336 SDOperand LoProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmulouh,
3337 LHS, RHS, DAG, MVT::v4i32);
3338
3339 SDOperand HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmsumuhm,
3340 LHS, RHSSwap, Zero, DAG, MVT::v4i32);
3341 // Shift the high parts up 16 bits.
3342 HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, HiProd, Neg16, DAG);
3343 return DAG.getNode(ISD::ADD, MVT::v4i32, LoProd, HiProd);
3344 } else if (Op.getValueType() == MVT::v8i16) {
3345 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
3346
Chris Lattnercea2aa72006-04-18 04:28:57 +00003347 SDOperand Zero = BuildSplatI(0, 1, MVT::v8i16, DAG);
Chris Lattner72dd9bd2006-04-18 03:43:48 +00003348
Chris Lattnercea2aa72006-04-18 04:28:57 +00003349 return BuildIntrinsicOp(Intrinsic::ppc_altivec_vmladduhm,
3350 LHS, RHS, Zero, DAG);
Chris Lattner19a81522006-04-18 03:57:35 +00003351 } else if (Op.getValueType() == MVT::v16i8) {
3352 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
3353
3354 // Multiply the even 8-bit parts, producing 16-bit sums.
3355 SDOperand EvenParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuleub,
3356 LHS, RHS, DAG, MVT::v8i16);
3357 EvenParts = DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, EvenParts);
3358
3359 // Multiply the odd 8-bit parts, producing 16-bit sums.
3360 SDOperand OddParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuloub,
3361 LHS, RHS, DAG, MVT::v8i16);
3362 OddParts = DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, OddParts);
3363
3364 // Merge the results together.
Chris Lattnere2199452006-08-11 17:38:39 +00003365 SDOperand Ops[16];
Chris Lattner19a81522006-04-18 03:57:35 +00003366 for (unsigned i = 0; i != 8; ++i) {
Chris Lattnere2199452006-08-11 17:38:39 +00003367 Ops[i*2 ] = DAG.getConstant(2*i+1, MVT::i8);
3368 Ops[i*2+1] = DAG.getConstant(2*i+1+16, MVT::i8);
Chris Lattner19a81522006-04-18 03:57:35 +00003369 }
Chris Lattner19a81522006-04-18 03:57:35 +00003370 return DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v16i8, EvenParts, OddParts,
Chris Lattnere2199452006-08-11 17:38:39 +00003371 DAG.getNode(ISD::BUILD_VECTOR, MVT::v16i8, Ops, 16));
Chris Lattner72dd9bd2006-04-18 03:43:48 +00003372 } else {
3373 assert(0 && "Unknown mul to lower!");
3374 abort();
3375 }
Chris Lattnere7c768e2006-04-18 03:24:30 +00003376}
3377
Chris Lattnere4bc9ea2005-08-26 00:52:45 +00003378/// LowerOperation - Provide custom lowering hooks for some operations.
3379///
Nate Begeman21e463b2005-10-16 05:39:50 +00003380SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +00003381 switch (Op.getOpcode()) {
3382 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattner1a635d62006-04-14 06:01:58 +00003383 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
3384 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +00003385 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Nate Begeman37efe672006-04-22 18:53:45 +00003386 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Chris Lattner1a635d62006-04-14 06:01:58 +00003387 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nicolas Geoffray01119992007-04-03 13:59:52 +00003388 case ISD::VASTART:
3389 return LowerVASTART(Op, DAG, VarArgsFrameIndex, VarArgsStackOffset,
3390 VarArgsNumGPR, VarArgsNumFPR, PPCSubTarget);
3391
3392 case ISD::VAARG:
3393 return LowerVAARG(Op, DAG, VarArgsFrameIndex, VarArgsStackOffset,
3394 VarArgsNumGPR, VarArgsNumFPR, PPCSubTarget);
3395
Chris Lattneref957102006-06-21 00:34:03 +00003396 case ISD::FORMAL_ARGUMENTS:
Nicolas Geoffray01119992007-04-03 13:59:52 +00003397 return LowerFORMAL_ARGUMENTS(Op, DAG, VarArgsFrameIndex,
3398 VarArgsStackOffset, VarArgsNumGPR,
3399 VarArgsNumFPR, PPCSubTarget);
3400
Dan Gohman7925ed02008-03-19 21:39:28 +00003401 case ISD::CALL: return LowerCALL(Op, DAG, PPCSubTarget,
3402 getTargetMachine());
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00003403 case ISD::RET: return LowerRET(Op, DAG, getTargetMachine());
Jim Laskeyefc7e522006-12-04 22:04:42 +00003404 case ISD::STACKRESTORE: return LowerSTACKRESTORE(Op, DAG, PPCSubTarget);
Chris Lattner9f0bc652007-02-25 05:34:32 +00003405 case ISD::DYNAMIC_STACKALLOC:
3406 return LowerDYNAMIC_STACKALLOC(Op, DAG, PPCSubTarget);
Chris Lattner7c0d6642005-10-02 06:37:13 +00003407
Chris Lattner1a635d62006-04-14 06:01:58 +00003408 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
3409 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
3410 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003411 case ISD::FP_ROUND_INREG: return LowerFP_ROUND_INREG(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00003412 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00003413
Chris Lattner1a635d62006-04-14 06:01:58 +00003414 // Lower 64-bit shifts.
Chris Lattner3fe6c1d2006-09-20 03:47:40 +00003415 case ISD::SHL_PARTS: return LowerSHL_PARTS(Op, DAG);
3416 case ISD::SRL_PARTS: return LowerSRL_PARTS(Op, DAG);
3417 case ISD::SRA_PARTS: return LowerSRA_PARTS(Op, DAG);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00003418
Chris Lattner1a635d62006-04-14 06:01:58 +00003419 // Vector-related lowering.
3420 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
3421 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
3422 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
3423 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
Chris Lattnere7c768e2006-04-18 03:24:30 +00003424 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00003425
Chris Lattner3fc027d2007-12-08 06:59:59 +00003426 // Frame & Return address.
3427 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Nicolas Geoffray43c6e7c2007-03-01 13:11:38 +00003428 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Chris Lattnerbc11c342005-08-31 20:23:54 +00003429 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +00003430 return SDOperand();
3431}
3432
Chris Lattner1f873002007-11-28 18:44:47 +00003433SDNode *PPCTargetLowering::ExpandOperationResult(SDNode *N, SelectionDAG &DAG) {
3434 switch (N->getOpcode()) {
3435 default: assert(0 && "Wasn't expecting to be able to lower this!");
3436 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(SDOperand(N, 0), DAG).Val;
3437 }
3438}
3439
3440
Chris Lattner1a635d62006-04-14 06:01:58 +00003441//===----------------------------------------------------------------------===//
3442// Other Lowering Code
3443//===----------------------------------------------------------------------===//
3444
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00003445MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00003446PPCTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
3447 MachineBasicBlock *BB) {
Evan Chengc0f64ff2006-11-27 23:37:22 +00003448 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Chris Lattnerc08f9022006-06-27 00:04:13 +00003449 assert((MI->getOpcode() == PPC::SELECT_CC_I4 ||
3450 MI->getOpcode() == PPC::SELECT_CC_I8 ||
Chris Lattner919c0322005-10-01 01:35:02 +00003451 MI->getOpcode() == PPC::SELECT_CC_F4 ||
Chris Lattner710ff322006-04-08 22:45:08 +00003452 MI->getOpcode() == PPC::SELECT_CC_F8 ||
3453 MI->getOpcode() == PPC::SELECT_CC_VRRC) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00003454 "Unexpected instr type to insert");
3455
3456 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
3457 // control-flow pattern. The incoming instruction knows the destination vreg
3458 // to set, the condition code register to branch on, the true/false values to
3459 // select between, and a branch opcode to use.
3460 const BasicBlock *LLVM_BB = BB->getBasicBlock();
3461 ilist<MachineBasicBlock>::iterator It = BB;
3462 ++It;
3463
3464 // thisMBB:
3465 // ...
3466 // TrueVal = ...
3467 // cmpTY ccX, r1, r2
3468 // bCC copy1MBB
3469 // fallthrough --> copy0MBB
3470 MachineBasicBlock *thisMBB = BB;
3471 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
3472 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Chris Lattnerdf4ed632006-11-17 22:10:59 +00003473 unsigned SelectPred = MI->getOperand(4).getImm();
Evan Chengc0f64ff2006-11-27 23:37:22 +00003474 BuildMI(BB, TII->get(PPC::BCC))
Chris Lattner18258c62006-11-17 22:37:34 +00003475 .addImm(SelectPred).addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00003476 MachineFunction *F = BB->getParent();
3477 F->getBasicBlockList().insert(It, copy0MBB);
3478 F->getBasicBlockList().insert(It, sinkMBB);
Nate Begemanf15485a2006-03-27 01:32:24 +00003479 // Update machine-CFG edges by first adding all successors of the current
3480 // block to the new block which will contain the Phi node for the select.
3481 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
3482 e = BB->succ_end(); i != e; ++i)
3483 sinkMBB->addSuccessor(*i);
3484 // Next, remove all successors of the current block, and add the true
3485 // and fallthrough blocks as its successors.
3486 while(!BB->succ_empty())
3487 BB->removeSuccessor(BB->succ_begin());
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00003488 BB->addSuccessor(copy0MBB);
3489 BB->addSuccessor(sinkMBB);
3490
3491 // copy0MBB:
3492 // %FalseValue = ...
3493 // # fallthrough to sinkMBB
3494 BB = copy0MBB;
3495
3496 // Update machine-CFG edges
3497 BB->addSuccessor(sinkMBB);
3498
3499 // sinkMBB:
3500 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
3501 // ...
3502 BB = sinkMBB;
Evan Chengc0f64ff2006-11-27 23:37:22 +00003503 BuildMI(BB, TII->get(PPC::PHI), MI->getOperand(0).getReg())
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00003504 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
3505 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
3506
3507 delete MI; // The pseudo instruction is gone now.
3508 return BB;
3509}
3510
Chris Lattner1a635d62006-04-14 06:01:58 +00003511//===----------------------------------------------------------------------===//
3512// Target Optimization Hooks
3513//===----------------------------------------------------------------------===//
3514
Chris Lattner8c13d0a2006-03-01 04:57:39 +00003515SDOperand PPCTargetLowering::PerformDAGCombine(SDNode *N,
3516 DAGCombinerInfo &DCI) const {
3517 TargetMachine &TM = getTargetMachine();
3518 SelectionDAG &DAG = DCI.DAG;
3519 switch (N->getOpcode()) {
3520 default: break;
Chris Lattnercf9d0ac2006-09-19 05:22:59 +00003521 case PPCISD::SHL:
3522 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
3523 if (C->getValue() == 0) // 0 << V -> 0.
3524 return N->getOperand(0);
3525 }
3526 break;
3527 case PPCISD::SRL:
3528 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
3529 if (C->getValue() == 0) // 0 >>u V -> 0.
3530 return N->getOperand(0);
3531 }
3532 break;
3533 case PPCISD::SRA:
3534 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
3535 if (C->getValue() == 0 || // 0 >>s V -> 0.
3536 C->isAllOnesValue()) // -1 >>s V -> -1.
3537 return N->getOperand(0);
3538 }
3539 break;
3540
Chris Lattner8c13d0a2006-03-01 04:57:39 +00003541 case ISD::SINT_TO_FP:
Chris Lattnera7a58542006-06-16 17:34:12 +00003542 if (TM.getSubtarget<PPCSubtarget>().has64BitSupport()) {
Chris Lattnerecfe55e2006-03-22 05:30:33 +00003543 if (N->getOperand(0).getOpcode() == ISD::FP_TO_SINT) {
3544 // Turn (sint_to_fp (fp_to_sint X)) -> fctidz/fcfid without load/stores.
3545 // We allow the src/dst to be either f32/f64, but the intermediate
3546 // type must be i64.
Dale Johannesen79217062007-10-23 23:20:14 +00003547 if (N->getOperand(0).getValueType() == MVT::i64 &&
3548 N->getOperand(0).getOperand(0).getValueType() != MVT::ppcf128) {
Chris Lattnerecfe55e2006-03-22 05:30:33 +00003549 SDOperand Val = N->getOperand(0).getOperand(0);
3550 if (Val.getValueType() == MVT::f32) {
3551 Val = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Val);
3552 DCI.AddToWorklist(Val.Val);
3553 }
3554
3555 Val = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Val);
Chris Lattner8c13d0a2006-03-01 04:57:39 +00003556 DCI.AddToWorklist(Val.Val);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00003557 Val = DAG.getNode(PPCISD::FCFID, MVT::f64, Val);
Chris Lattner8c13d0a2006-03-01 04:57:39 +00003558 DCI.AddToWorklist(Val.Val);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00003559 if (N->getValueType(0) == MVT::f32) {
Chris Lattner0bd48932008-01-17 07:00:52 +00003560 Val = DAG.getNode(ISD::FP_ROUND, MVT::f32, Val,
3561 DAG.getIntPtrConstant(0));
Chris Lattnerecfe55e2006-03-22 05:30:33 +00003562 DCI.AddToWorklist(Val.Val);
3563 }
3564 return Val;
3565 } else if (N->getOperand(0).getValueType() == MVT::i32) {
3566 // If the intermediate type is i32, we can avoid the load/store here
3567 // too.
Chris Lattner8c13d0a2006-03-01 04:57:39 +00003568 }
Chris Lattner8c13d0a2006-03-01 04:57:39 +00003569 }
3570 }
3571 break;
Chris Lattner51269842006-03-01 05:50:56 +00003572 case ISD::STORE:
3573 // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)).
3574 if (TM.getSubtarget<PPCSubtarget>().hasSTFIWX() &&
Chris Lattnera7a02fb2008-01-18 16:54:56 +00003575 !cast<StoreSDNode>(N)->isTruncatingStore() &&
Chris Lattner51269842006-03-01 05:50:56 +00003576 N->getOperand(1).getOpcode() == ISD::FP_TO_SINT &&
Dale Johannesen79217062007-10-23 23:20:14 +00003577 N->getOperand(1).getValueType() == MVT::i32 &&
3578 N->getOperand(1).getOperand(0).getValueType() != MVT::ppcf128) {
Chris Lattner51269842006-03-01 05:50:56 +00003579 SDOperand Val = N->getOperand(1).getOperand(0);
3580 if (Val.getValueType() == MVT::f32) {
3581 Val = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Val);
3582 DCI.AddToWorklist(Val.Val);
3583 }
3584 Val = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Val);
3585 DCI.AddToWorklist(Val.Val);
3586
3587 Val = DAG.getNode(PPCISD::STFIWX, MVT::Other, N->getOperand(0), Val,
3588 N->getOperand(2), N->getOperand(3));
3589 DCI.AddToWorklist(Val.Val);
3590 return Val;
3591 }
Chris Lattnerd9989382006-07-10 20:56:58 +00003592
3593 // Turn STORE (BSWAP) -> sthbrx/stwbrx.
3594 if (N->getOperand(1).getOpcode() == ISD::BSWAP &&
3595 N->getOperand(1).Val->hasOneUse() &&
3596 (N->getOperand(1).getValueType() == MVT::i32 ||
3597 N->getOperand(1).getValueType() == MVT::i16)) {
3598 SDOperand BSwapOp = N->getOperand(1).getOperand(0);
3599 // Do an any-extend to 32-bits if this is a half-word input.
3600 if (BSwapOp.getValueType() == MVT::i16)
3601 BSwapOp = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, BSwapOp);
3602
3603 return DAG.getNode(PPCISD::STBRX, MVT::Other, N->getOperand(0), BSwapOp,
3604 N->getOperand(2), N->getOperand(3),
3605 DAG.getValueType(N->getOperand(1).getValueType()));
3606 }
3607 break;
3608 case ISD::BSWAP:
3609 // Turn BSWAP (LOAD) -> lhbrx/lwbrx.
Evan Cheng466685d2006-10-09 20:57:25 +00003610 if (ISD::isNON_EXTLoad(N->getOperand(0).Val) &&
Chris Lattnerd9989382006-07-10 20:56:58 +00003611 N->getOperand(0).hasOneUse() &&
3612 (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i16)) {
3613 SDOperand Load = N->getOperand(0);
Evan Cheng466685d2006-10-09 20:57:25 +00003614 LoadSDNode *LD = cast<LoadSDNode>(Load);
Chris Lattnerd9989382006-07-10 20:56:58 +00003615 // Create the byte-swapping load.
3616 std::vector<MVT::ValueType> VTs;
3617 VTs.push_back(MVT::i32);
3618 VTs.push_back(MVT::Other);
Dan Gohman69de1932008-02-06 22:27:42 +00003619 SDOperand MO = DAG.getMemOperand(LD->getMemOperand());
Chris Lattner79e490a2006-08-11 17:18:05 +00003620 SDOperand Ops[] = {
Evan Cheng466685d2006-10-09 20:57:25 +00003621 LD->getChain(), // Chain
3622 LD->getBasePtr(), // Ptr
Dan Gohman69de1932008-02-06 22:27:42 +00003623 MO, // MemOperand
Chris Lattner79e490a2006-08-11 17:18:05 +00003624 DAG.getValueType(N->getValueType(0)) // VT
3625 };
3626 SDOperand BSLoad = DAG.getNode(PPCISD::LBRX, VTs, Ops, 4);
Chris Lattnerd9989382006-07-10 20:56:58 +00003627
3628 // If this is an i16 load, insert the truncate.
3629 SDOperand ResVal = BSLoad;
3630 if (N->getValueType(0) == MVT::i16)
3631 ResVal = DAG.getNode(ISD::TRUNCATE, MVT::i16, BSLoad);
3632
3633 // First, combine the bswap away. This makes the value produced by the
3634 // load dead.
3635 DCI.CombineTo(N, ResVal);
3636
3637 // Next, combine the load away, we give it a bogus result value but a real
3638 // chain result. The result value is dead because the bswap is dead.
3639 DCI.CombineTo(Load.Val, ResVal, BSLoad.getValue(1));
3640
3641 // Return N so it doesn't get rechecked!
3642 return SDOperand(N, 0);
3643 }
3644
Chris Lattner51269842006-03-01 05:50:56 +00003645 break;
Chris Lattner4468c222006-03-31 06:02:07 +00003646 case PPCISD::VCMP: {
3647 // If a VCMPo node already exists with exactly the same operands as this
3648 // node, use its result instead of this node (VCMPo computes both a CR6 and
3649 // a normal output).
3650 //
3651 if (!N->getOperand(0).hasOneUse() &&
3652 !N->getOperand(1).hasOneUse() &&
3653 !N->getOperand(2).hasOneUse()) {
3654
3655 // Scan all of the users of the LHS, looking for VCMPo's that match.
3656 SDNode *VCMPoNode = 0;
3657
3658 SDNode *LHSN = N->getOperand(0).Val;
3659 for (SDNode::use_iterator UI = LHSN->use_begin(), E = LHSN->use_end();
3660 UI != E; ++UI)
Roman Levensteindc1adac2008-04-07 10:06:32 +00003661 if ((*UI).getUser()->getOpcode() == PPCISD::VCMPo &&
3662 (*UI).getUser()->getOperand(1) == N->getOperand(1) &&
3663 (*UI).getUser()->getOperand(2) == N->getOperand(2) &&
3664 (*UI).getUser()->getOperand(0) == N->getOperand(0)) {
3665 VCMPoNode = UI->getUser();
Chris Lattner4468c222006-03-31 06:02:07 +00003666 break;
3667 }
3668
Chris Lattner00901202006-04-18 18:28:22 +00003669 // If there is no VCMPo node, or if the flag value has a single use, don't
3670 // transform this.
3671 if (!VCMPoNode || VCMPoNode->hasNUsesOfValue(0, 1))
3672 break;
3673
3674 // Look at the (necessarily single) use of the flag value. If it has a
3675 // chain, this transformation is more complex. Note that multiple things
3676 // could use the value result, which we should ignore.
3677 SDNode *FlagUser = 0;
3678 for (SDNode::use_iterator UI = VCMPoNode->use_begin();
3679 FlagUser == 0; ++UI) {
3680 assert(UI != VCMPoNode->use_end() && "Didn't find user!");
Roman Levensteindc1adac2008-04-07 10:06:32 +00003681 SDNode *User = UI->getUser();
Chris Lattner00901202006-04-18 18:28:22 +00003682 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
3683 if (User->getOperand(i) == SDOperand(VCMPoNode, 1)) {
3684 FlagUser = User;
3685 break;
3686 }
3687 }
3688 }
3689
3690 // If the user is a MFCR instruction, we know this is safe. Otherwise we
3691 // give up for right now.
3692 if (FlagUser->getOpcode() == PPCISD::MFCR)
Chris Lattner4468c222006-03-31 06:02:07 +00003693 return SDOperand(VCMPoNode, 0);
3694 }
3695 break;
3696 }
Chris Lattner90564f22006-04-18 17:59:36 +00003697 case ISD::BR_CC: {
3698 // If this is a branch on an altivec predicate comparison, lower this so
3699 // that we don't have to do a MFCR: instead, branch directly on CR6. This
3700 // lowering is done pre-legalize, because the legalizer lowers the predicate
3701 // compare down to code that is difficult to reassemble.
3702 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
3703 SDOperand LHS = N->getOperand(2), RHS = N->getOperand(3);
3704 int CompareOpc;
3705 bool isDot;
3706
3707 if (LHS.getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
3708 isa<ConstantSDNode>(RHS) && (CC == ISD::SETEQ || CC == ISD::SETNE) &&
3709 getAltivecCompareInfo(LHS, CompareOpc, isDot)) {
3710 assert(isDot && "Can't compare against a vector result!");
3711
3712 // If this is a comparison against something other than 0/1, then we know
3713 // that the condition is never/always true.
3714 unsigned Val = cast<ConstantSDNode>(RHS)->getValue();
3715 if (Val != 0 && Val != 1) {
3716 if (CC == ISD::SETEQ) // Cond never true, remove branch.
3717 return N->getOperand(0);
3718 // Always !=, turn it into an unconditional branch.
3719 return DAG.getNode(ISD::BR, MVT::Other,
3720 N->getOperand(0), N->getOperand(4));
3721 }
3722
3723 bool BranchOnWhenPredTrue = (CC == ISD::SETEQ) ^ (Val == 0);
3724
3725 // Create the PPCISD altivec 'dot' comparison node.
Chris Lattner90564f22006-04-18 17:59:36 +00003726 std::vector<MVT::ValueType> VTs;
Chris Lattner79e490a2006-08-11 17:18:05 +00003727 SDOperand Ops[] = {
3728 LHS.getOperand(2), // LHS of compare
3729 LHS.getOperand(3), // RHS of compare
3730 DAG.getConstant(CompareOpc, MVT::i32)
3731 };
Chris Lattner90564f22006-04-18 17:59:36 +00003732 VTs.push_back(LHS.getOperand(2).getValueType());
3733 VTs.push_back(MVT::Flag);
Chris Lattner79e490a2006-08-11 17:18:05 +00003734 SDOperand CompNode = DAG.getNode(PPCISD::VCMPo, VTs, Ops, 3);
Chris Lattner90564f22006-04-18 17:59:36 +00003735
3736 // Unpack the result based on how the target uses it.
Chris Lattnerdf4ed632006-11-17 22:10:59 +00003737 PPC::Predicate CompOpc;
Chris Lattner90564f22006-04-18 17:59:36 +00003738 switch (cast<ConstantSDNode>(LHS.getOperand(1))->getValue()) {
3739 default: // Can't happen, don't crash on invalid number though.
3740 case 0: // Branch on the value of the EQ bit of CR6.
Chris Lattnerdf4ed632006-11-17 22:10:59 +00003741 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_EQ : PPC::PRED_NE;
Chris Lattner90564f22006-04-18 17:59:36 +00003742 break;
3743 case 1: // Branch on the inverted value of the EQ bit of CR6.
Chris Lattnerdf4ed632006-11-17 22:10:59 +00003744 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_NE : PPC::PRED_EQ;
Chris Lattner90564f22006-04-18 17:59:36 +00003745 break;
3746 case 2: // Branch on the value of the LT bit of CR6.
Chris Lattnerdf4ed632006-11-17 22:10:59 +00003747 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_LT : PPC::PRED_GE;
Chris Lattner90564f22006-04-18 17:59:36 +00003748 break;
3749 case 3: // Branch on the inverted value of the LT bit of CR6.
Chris Lattnerdf4ed632006-11-17 22:10:59 +00003750 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_GE : PPC::PRED_LT;
Chris Lattner90564f22006-04-18 17:59:36 +00003751 break;
3752 }
3753
3754 return DAG.getNode(PPCISD::COND_BRANCH, MVT::Other, N->getOperand(0),
Chris Lattner90564f22006-04-18 17:59:36 +00003755 DAG.getConstant(CompOpc, MVT::i32),
Chris Lattner18258c62006-11-17 22:37:34 +00003756 DAG.getRegister(PPC::CR6, MVT::i32),
Chris Lattner90564f22006-04-18 17:59:36 +00003757 N->getOperand(4), CompNode.getValue(1));
3758 }
3759 break;
3760 }
Chris Lattner8c13d0a2006-03-01 04:57:39 +00003761 }
3762
3763 return SDOperand();
3764}
3765
Chris Lattner1a635d62006-04-14 06:01:58 +00003766//===----------------------------------------------------------------------===//
3767// Inline Assembly Support
3768//===----------------------------------------------------------------------===//
3769
Chris Lattnerbbe77de2006-04-02 06:26:07 +00003770void PPCTargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00003771 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00003772 APInt &KnownZero,
3773 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00003774 const SelectionDAG &DAG,
Chris Lattnerbbe77de2006-04-02 06:26:07 +00003775 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00003776 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Chris Lattnerbbe77de2006-04-02 06:26:07 +00003777 switch (Op.getOpcode()) {
3778 default: break;
Chris Lattnerd9989382006-07-10 20:56:58 +00003779 case PPCISD::LBRX: {
3780 // lhbrx is known to have the top bits cleared out.
3781 if (cast<VTSDNode>(Op.getOperand(3))->getVT() == MVT::i16)
3782 KnownZero = 0xFFFF0000;
3783 break;
3784 }
Chris Lattnerbbe77de2006-04-02 06:26:07 +00003785 case ISD::INTRINSIC_WO_CHAIN: {
3786 switch (cast<ConstantSDNode>(Op.getOperand(0))->getValue()) {
3787 default: break;
3788 case Intrinsic::ppc_altivec_vcmpbfp_p:
3789 case Intrinsic::ppc_altivec_vcmpeqfp_p:
3790 case Intrinsic::ppc_altivec_vcmpequb_p:
3791 case Intrinsic::ppc_altivec_vcmpequh_p:
3792 case Intrinsic::ppc_altivec_vcmpequw_p:
3793 case Intrinsic::ppc_altivec_vcmpgefp_p:
3794 case Intrinsic::ppc_altivec_vcmpgtfp_p:
3795 case Intrinsic::ppc_altivec_vcmpgtsb_p:
3796 case Intrinsic::ppc_altivec_vcmpgtsh_p:
3797 case Intrinsic::ppc_altivec_vcmpgtsw_p:
3798 case Intrinsic::ppc_altivec_vcmpgtub_p:
3799 case Intrinsic::ppc_altivec_vcmpgtuh_p:
3800 case Intrinsic::ppc_altivec_vcmpgtuw_p:
3801 KnownZero = ~1U; // All bits but the low one are known to be zero.
3802 break;
3803 }
3804 }
3805 }
3806}
3807
3808
Chris Lattner4234f572007-03-25 02:14:49 +00003809/// getConstraintType - Given a constraint, return the type of
Chris Lattnerad3bc8d2006-02-07 20:16:30 +00003810/// constraint it is for this target.
3811PPCTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00003812PPCTargetLowering::getConstraintType(const std::string &Constraint) const {
3813 if (Constraint.size() == 1) {
3814 switch (Constraint[0]) {
3815 default: break;
3816 case 'b':
3817 case 'r':
3818 case 'f':
3819 case 'v':
3820 case 'y':
3821 return C_RegisterClass;
3822 }
3823 }
3824 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerad3bc8d2006-02-07 20:16:30 +00003825}
3826
Chris Lattner331d1bc2006-11-02 01:44:04 +00003827std::pair<unsigned, const TargetRegisterClass*>
3828PPCTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
3829 MVT::ValueType VT) const {
Chris Lattnerddc787d2006-01-31 19:20:21 +00003830 if (Constraint.size() == 1) {
Chris Lattner331d1bc2006-11-02 01:44:04 +00003831 // GCC RS6000 Constraint Letters
3832 switch (Constraint[0]) {
3833 case 'b': // R1-R31
3834 case 'r': // R0-R31
3835 if (VT == MVT::i64 && PPCSubTarget.isPPC64())
3836 return std::make_pair(0U, PPC::G8RCRegisterClass);
3837 return std::make_pair(0U, PPC::GPRCRegisterClass);
3838 case 'f':
3839 if (VT == MVT::f32)
3840 return std::make_pair(0U, PPC::F4RCRegisterClass);
3841 else if (VT == MVT::f64)
3842 return std::make_pair(0U, PPC::F8RCRegisterClass);
3843 break;
Chris Lattnerddc787d2006-01-31 19:20:21 +00003844 case 'v':
Chris Lattner331d1bc2006-11-02 01:44:04 +00003845 return std::make_pair(0U, PPC::VRRCRegisterClass);
3846 case 'y': // crrc
3847 return std::make_pair(0U, PPC::CRRCRegisterClass);
Chris Lattnerddc787d2006-01-31 19:20:21 +00003848 }
3849 }
3850
Chris Lattner331d1bc2006-11-02 01:44:04 +00003851 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattnerddc787d2006-01-31 19:20:21 +00003852}
Chris Lattner763317d2006-02-07 00:47:13 +00003853
Chris Lattner331d1bc2006-11-02 01:44:04 +00003854
Chris Lattner48884cd2007-08-25 00:47:38 +00003855/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3856/// vector. If it is invalid, don't add anything to Ops.
3857void PPCTargetLowering::LowerAsmOperandForConstraint(SDOperand Op, char Letter,
3858 std::vector<SDOperand>&Ops,
3859 SelectionDAG &DAG) {
3860 SDOperand Result(0,0);
Chris Lattner763317d2006-02-07 00:47:13 +00003861 switch (Letter) {
3862 default: break;
3863 case 'I':
3864 case 'J':
3865 case 'K':
3866 case 'L':
3867 case 'M':
3868 case 'N':
3869 case 'O':
3870 case 'P': {
Chris Lattner9f5d5782007-05-15 01:31:05 +00003871 ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op);
Chris Lattner48884cd2007-08-25 00:47:38 +00003872 if (!CST) return; // Must be an immediate to match.
Chris Lattner9f5d5782007-05-15 01:31:05 +00003873 unsigned Value = CST->getValue();
Chris Lattner763317d2006-02-07 00:47:13 +00003874 switch (Letter) {
3875 default: assert(0 && "Unknown constraint letter!");
3876 case 'I': // "I" is a signed 16-bit constant.
Chris Lattner9f5d5782007-05-15 01:31:05 +00003877 if ((short)Value == (int)Value)
Chris Lattner48884cd2007-08-25 00:47:38 +00003878 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00003879 break;
Chris Lattner763317d2006-02-07 00:47:13 +00003880 case 'J': // "J" is a constant with only the high-order 16 bits nonzero.
3881 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits.
Chris Lattner9f5d5782007-05-15 01:31:05 +00003882 if ((short)Value == 0)
Chris Lattner48884cd2007-08-25 00:47:38 +00003883 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00003884 break;
Chris Lattner763317d2006-02-07 00:47:13 +00003885 case 'K': // "K" is a constant with only the low-order 16 bits nonzero.
Chris Lattner9f5d5782007-05-15 01:31:05 +00003886 if ((Value >> 16) == 0)
Chris Lattner48884cd2007-08-25 00:47:38 +00003887 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00003888 break;
Chris Lattner763317d2006-02-07 00:47:13 +00003889 case 'M': // "M" is a constant that is greater than 31.
Chris Lattner9f5d5782007-05-15 01:31:05 +00003890 if (Value > 31)
Chris Lattner48884cd2007-08-25 00:47:38 +00003891 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00003892 break;
Chris Lattner763317d2006-02-07 00:47:13 +00003893 case 'N': // "N" is a positive constant that is an exact power of two.
Chris Lattner9f5d5782007-05-15 01:31:05 +00003894 if ((int)Value > 0 && isPowerOf2_32(Value))
Chris Lattner48884cd2007-08-25 00:47:38 +00003895 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00003896 break;
Chris Lattner763317d2006-02-07 00:47:13 +00003897 case 'O': // "O" is the constant zero.
Chris Lattner9f5d5782007-05-15 01:31:05 +00003898 if (Value == 0)
Chris Lattner48884cd2007-08-25 00:47:38 +00003899 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00003900 break;
Chris Lattner763317d2006-02-07 00:47:13 +00003901 case 'P': // "P" is a constant whose negation is a signed 16-bit constant.
Chris Lattner9f5d5782007-05-15 01:31:05 +00003902 if ((short)-Value == (int)-Value)
Chris Lattner48884cd2007-08-25 00:47:38 +00003903 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00003904 break;
Chris Lattner763317d2006-02-07 00:47:13 +00003905 }
3906 break;
3907 }
3908 }
3909
Chris Lattner48884cd2007-08-25 00:47:38 +00003910 if (Result.Val) {
3911 Ops.push_back(Result);
3912 return;
3913 }
3914
Chris Lattner763317d2006-02-07 00:47:13 +00003915 // Handle standard constraint letters.
Chris Lattner48884cd2007-08-25 00:47:38 +00003916 TargetLowering::LowerAsmOperandForConstraint(Op, Letter, Ops, DAG);
Chris Lattner763317d2006-02-07 00:47:13 +00003917}
Evan Chengc4c62572006-03-13 23:20:37 +00003918
Chris Lattnerc9addb72007-03-30 23:15:24 +00003919// isLegalAddressingMode - Return true if the addressing mode represented
3920// by AM is legal for this target, for a load/store of the specified type.
3921bool PPCTargetLowering::isLegalAddressingMode(const AddrMode &AM,
3922 const Type *Ty) const {
3923 // FIXME: PPC does not allow r+i addressing modes for vectors!
3924
3925 // PPC allows a sign-extended 16-bit immediate field.
3926 if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1)
3927 return false;
3928
3929 // No global is ever allowed as a base.
3930 if (AM.BaseGV)
3931 return false;
3932
3933 // PPC only support r+r,
3934 switch (AM.Scale) {
3935 case 0: // "r+i" or just "i", depending on HasBaseReg.
3936 break;
3937 case 1:
3938 if (AM.HasBaseReg && AM.BaseOffs) // "r+r+i" is not allowed.
3939 return false;
3940 // Otherwise we have r+r or r+i.
3941 break;
3942 case 2:
3943 if (AM.HasBaseReg || AM.BaseOffs) // 2*r+r or 2*r+i is not allowed.
3944 return false;
3945 // Allow 2*r as r+r.
3946 break;
Chris Lattner7c7ba9d2007-04-09 22:10:05 +00003947 default:
3948 // No other scales are supported.
3949 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00003950 }
3951
3952 return true;
3953}
3954
Evan Chengc4c62572006-03-13 23:20:37 +00003955/// isLegalAddressImmediate - Return true if the integer value can be used
Evan Cheng86193912007-03-12 23:29:01 +00003956/// as the offset of the target addressing mode for load / store of the
3957/// given type.
3958bool PPCTargetLowering::isLegalAddressImmediate(int64_t V,const Type *Ty) const{
Evan Chengc4c62572006-03-13 23:20:37 +00003959 // PPC allows a sign-extended 16-bit immediate field.
3960 return (V > -(1 << 16) && V < (1 << 16)-1);
3961}
Reid Spencer3a9ec242006-08-28 01:02:49 +00003962
3963bool PPCTargetLowering::isLegalAddressImmediate(llvm::GlobalValue* GV) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +00003964 return false;
Reid Spencer3a9ec242006-08-28 01:02:49 +00003965}
Nicolas Geoffray43c6e7c2007-03-01 13:11:38 +00003966
Chris Lattner3fc027d2007-12-08 06:59:59 +00003967SDOperand PPCTargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
3968 // Depths > 0 not supported yet!
3969 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
3970 return SDOperand();
3971
3972 MachineFunction &MF = DAG.getMachineFunction();
3973 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
3974 int RAIdx = FuncInfo->getReturnAddrSaveIndex();
3975 if (RAIdx == 0) {
3976 bool isPPC64 = PPCSubTarget.isPPC64();
3977 int Offset =
3978 PPCFrameInfo::getReturnSaveOffset(isPPC64, PPCSubTarget.isMachoABI());
3979
3980 // Set up a frame object for the return address.
3981 RAIdx = MF.getFrameInfo()->CreateFixedObject(isPPC64 ? 8 : 4, Offset);
3982
3983 // Remember it for next time.
3984 FuncInfo->setReturnAddrSaveIndex(RAIdx);
3985
3986 // Make sure the function really does not optimize away the store of the RA
3987 // to the stack.
3988 FuncInfo->setLRStoreRequired();
3989 }
3990
3991 // Just load the return address off the stack.
3992 SDOperand RetAddrFI = DAG.getFrameIndex(RAIdx, getPointerTy());
3993 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
3994}
3995
3996SDOperand PPCTargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
Nicolas Geoffray43c6e7c2007-03-01 13:11:38 +00003997 // Depths > 0 not supported yet!
3998 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
3999 return SDOperand();
4000
4001 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
4002 bool isPPC64 = PtrVT == MVT::i64;
4003
4004 MachineFunction &MF = DAG.getMachineFunction();
4005 MachineFrameInfo *MFI = MF.getFrameInfo();
4006 bool is31 = (NoFramePointerElim || MFI->hasVarSizedObjects())
4007 && MFI->getStackSize();
4008
4009 if (isPPC64)
4010 return DAG.getCopyFromReg(DAG.getEntryNode(), is31 ? PPC::X31 : PPC::X1,
Bill Wendlingb8a80f02007-08-30 00:59:19 +00004011 MVT::i64);
Nicolas Geoffray43c6e7c2007-03-01 13:11:38 +00004012 else
4013 return DAG.getCopyFromReg(DAG.getEntryNode(), is31 ? PPC::R31 : PPC::R1,
4014 MVT::i32);
4015}