blob: 09779bef057e20c217bd79164f3880adf212e21d [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)
Evan Cheng54fc97d2008-04-19 01:30:48 +000042 : TargetLowering(TM), PPCSubTarget(*TM.getSubtargetImpl()),
43 PPCAtomicLabelIndex(0) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +000044
Nate Begeman405e3ec2005-10-21 00:02:42 +000045 setPow2DivIsCheap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +000046
Chris Lattnerd145a612005-09-27 22:18:25 +000047 // Use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000048 setUseUnderscoreSetJmp(true);
49 setUseUnderscoreLongJmp(true);
Chris Lattnerd145a612005-09-27 22:18:25 +000050
Chris Lattner7c5a3d32005-08-16 17:14:42 +000051 // Set up the register classes.
Nate Begeman1d9d7422005-10-18 00:28:58 +000052 addRegisterClass(MVT::i32, PPC::GPRCRegisterClass);
53 addRegisterClass(MVT::f32, PPC::F4RCRegisterClass);
54 addRegisterClass(MVT::f64, PPC::F8RCRegisterClass);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000055
Evan Chengc5484282006-10-04 00:56:09 +000056 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
Duncan Sandsf9c98e62008-01-23 20:39:46 +000057 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +000058 setLoadXAction(ISD::SEXTLOAD, MVT::i8, Expand);
Duncan Sandsf9c98e62008-01-23 20:39:46 +000059
Chris Lattnerddf89562008-01-17 19:59:44 +000060 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
61
Chris Lattner94e509c2006-11-10 23:58:45 +000062 // PowerPC has pre-inc load and store's.
63 setIndexedLoadAction(ISD::PRE_INC, MVT::i1, Legal);
64 setIndexedLoadAction(ISD::PRE_INC, MVT::i8, Legal);
65 setIndexedLoadAction(ISD::PRE_INC, MVT::i16, Legal);
Evan Chengcd633192006-11-09 19:11:50 +000066 setIndexedLoadAction(ISD::PRE_INC, MVT::i32, Legal);
67 setIndexedLoadAction(ISD::PRE_INC, MVT::i64, Legal);
Chris Lattner94e509c2006-11-10 23:58:45 +000068 setIndexedStoreAction(ISD::PRE_INC, MVT::i1, Legal);
69 setIndexedStoreAction(ISD::PRE_INC, MVT::i8, Legal);
70 setIndexedStoreAction(ISD::PRE_INC, MVT::i16, Legal);
Evan Chengcd633192006-11-09 19:11:50 +000071 setIndexedStoreAction(ISD::PRE_INC, MVT::i32, Legal);
72 setIndexedStoreAction(ISD::PRE_INC, MVT::i64, Legal);
73
Dale Johannesen638ccd52007-10-06 01:24:11 +000074 // Shortening conversions involving ppcf128 get expanded (2 regs -> 1 reg)
75 setConvertAction(MVT::ppcf128, MVT::f64, Expand);
76 setConvertAction(MVT::ppcf128, MVT::f32, Expand);
Dale Johannesen6eaeff22007-10-10 01:01:31 +000077 // This is used in the ppcf128->int sequence. Note it has different semantics
78 // from FP_ROUND: that rounds to nearest, this rounds to zero.
79 setOperationAction(ISD::FP_ROUND_INREG, MVT::ppcf128, Custom);
Dale Johannesen638ccd52007-10-06 01:24:11 +000080
Chris Lattner7c5a3d32005-08-16 17:14:42 +000081 // PowerPC has no intrinsics for these particular operations
Andrew Lenharthd497d9f2008-02-16 14:46:26 +000082 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
83
Chris Lattner7c5a3d32005-08-16 17:14:42 +000084 // PowerPC has no SREM/UREM instructions
85 setOperationAction(ISD::SREM, MVT::i32, Expand);
86 setOperationAction(ISD::UREM, MVT::i32, Expand);
Chris Lattner563ecfb2006-06-27 18:18:41 +000087 setOperationAction(ISD::SREM, MVT::i64, Expand);
88 setOperationAction(ISD::UREM, MVT::i64, Expand);
Dan Gohman3ce990d2007-10-08 17:28:24 +000089
90 // Don't use SMUL_LOHI/UMUL_LOHI or SDIVREM/UDIVREM to lower SREM/UREM.
91 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
92 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
93 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
94 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
95 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
96 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
97 setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
98 setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000099
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000100 // We don't support sin/cos/sqrt/fmod/pow
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000101 setOperationAction(ISD::FSIN , MVT::f64, Expand);
102 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattner615c2d02005-09-28 22:29:58 +0000103 setOperationAction(ISD::FREM , MVT::f64, Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000104 setOperationAction(ISD::FPOW , MVT::f64, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000105 setOperationAction(ISD::FSIN , MVT::f32, Expand);
106 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattner615c2d02005-09-28 22:29:58 +0000107 setOperationAction(ISD::FREM , MVT::f32, Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000108 setOperationAction(ISD::FPOW , MVT::f32, Expand);
Dale Johannesen5c5eb802008-01-18 19:55:37 +0000109
Dan Gohman1a024862008-01-31 00:41:03 +0000110 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000111
112 // If we're enabling GP optimizations, use hardware square root
Chris Lattner1e9de3e2005-09-02 18:33:05 +0000113 if (!TM.getSubtarget<PPCSubtarget>().hasFSQRT()) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000114 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
115 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
116 }
117
Chris Lattner9601a862006-03-05 05:08:37 +0000118 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
119 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
120
Nate Begemand88fc032006-01-14 03:14:10 +0000121 // PowerPC does not have BSWAP, CTPOP or CTTZ
122 setOperationAction(ISD::BSWAP, MVT::i32 , Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000123 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
124 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
Chris Lattnerf89437d2006-06-27 20:14:52 +0000125 setOperationAction(ISD::BSWAP, MVT::i64 , Expand);
126 setOperationAction(ISD::CTPOP, MVT::i64 , Expand);
127 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000128
Nate Begeman35ef9132006-01-11 21:21:00 +0000129 // PowerPC does not have ROTR
130 setOperationAction(ISD::ROTR, MVT::i32 , Expand);
131
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000132 // PowerPC does not have Select
133 setOperationAction(ISD::SELECT, MVT::i32, Expand);
Chris Lattnerf89437d2006-06-27 20:14:52 +0000134 setOperationAction(ISD::SELECT, MVT::i64, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000135 setOperationAction(ISD::SELECT, MVT::f32, Expand);
136 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000137
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000138 // PowerPC wants to turn select_cc of FP into fsel when possible.
139 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
140 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Nate Begeman44775902006-01-31 08:17:29 +0000141
Nate Begeman750ac1b2006-02-01 07:19:44 +0000142 // PowerPC wants to optimize integer setcc a bit
Nate Begeman44775902006-01-31 08:17:29 +0000143 setOperationAction(ISD::SETCC, MVT::i32, Custom);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000144
Nate Begeman81e80972006-03-17 01:40:33 +0000145 // PowerPC does not have BRCOND which requires SetCC
146 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
Evan Chengc35497f2006-10-30 08:02:39 +0000147
148 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000149
Chris Lattnerf7605322005-08-31 21:09:52 +0000150 // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
151 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000152
Jim Laskeyad23c9d2005-08-17 00:40:22 +0000153 // PowerPC does not have [U|S]INT_TO_FP
154 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
155 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
156
Chris Lattner53e88452005-12-23 05:13:35 +0000157 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
158 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
Chris Lattner5f9faea2006-06-27 18:40:08 +0000159 setOperationAction(ISD::BIT_CONVERT, MVT::i64, Expand);
160 setOperationAction(ISD::BIT_CONVERT, MVT::f64, Expand);
Chris Lattner53e88452005-12-23 05:13:35 +0000161
Chris Lattner25b8b8c2006-04-28 21:56:10 +0000162 // We cannot sextinreg(i1). Expand to shifts.
163 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Jim Laskey2ad9f172007-02-22 14:56:36 +0000164
Jim Laskeyabf6d172006-01-05 01:25:28 +0000165 // Support label based line numbers.
Chris Lattnerf73bae12005-11-29 06:16:21 +0000166 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000167 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Nicolas Geoffray616585b2007-12-21 12:19:44 +0000168
169 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
170 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
171 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
172 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
173
Chris Lattnere6ec9f22005-09-10 00:21:06 +0000174
Nate Begeman28a6b022005-12-10 02:36:00 +0000175 // We want to legalize GlobalAddress and ConstantPool nodes into the
176 // appropriate instructions to materialize the address.
Chris Lattner3eef4e32005-11-17 18:26:56 +0000177 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +0000178 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Nate Begeman28a6b022005-12-10 02:36:00 +0000179 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Nate Begeman37efe672006-04-22 18:53:45 +0000180 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
Chris Lattner059ca0f2006-06-16 21:01:35 +0000181 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +0000182 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
Chris Lattner059ca0f2006-06-16 21:01:35 +0000183 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
184 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
185
Nate Begemanee625572006-01-27 21:09:22 +0000186 // RET must be custom lowered, to meet ABI requirements
187 setOperationAction(ISD::RET , MVT::Other, Custom);
Duncan Sands36397f52007-07-27 12:58:54 +0000188
Nate Begemanacc398c2006-01-25 18:21:52 +0000189 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
190 setOperationAction(ISD::VASTART , MVT::Other, Custom);
191
Nicolas Geoffray01119992007-04-03 13:59:52 +0000192 // VAARG is custom lowered with ELF 32 ABI
193 if (TM.getSubtarget<PPCSubtarget>().isELF32_ABI())
194 setOperationAction(ISD::VAARG, MVT::Other, Custom);
195 else
196 setOperationAction(ISD::VAARG, MVT::Other, Expand);
197
Chris Lattnerb22c08b2006-01-15 09:02:48 +0000198 // Use the default implementation.
Nate Begemanacc398c2006-01-25 18:21:52 +0000199 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
200 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Chris Lattnerb22c08b2006-01-15 09:02:48 +0000201 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
Jim Laskeyefc7e522006-12-04 22:04:42 +0000202 setOperationAction(ISD::STACKRESTORE , MVT::Other, Custom);
Jim Laskey2f616bf2006-11-16 22:43:37 +0000203 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
204 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64 , Custom);
Chris Lattner56a752e2006-10-18 01:18:48 +0000205
Evan Cheng54fc97d2008-04-19 01:30:48 +0000206 setOperationAction(ISD::ATOMIC_LAS , MVT::i32 , Custom);
207 setOperationAction(ISD::ATOMIC_LCS , MVT::i32 , Custom);
208 setOperationAction(ISD::ATOMIC_SWAP , MVT::i32 , Custom);
Evan Cheng8608f2e2008-04-19 02:30:38 +0000209 if (TM.getSubtarget<PPCSubtarget>().has64BitSupport()) {
210 setOperationAction(ISD::ATOMIC_LAS , MVT::i64 , Custom);
211 setOperationAction(ISD::ATOMIC_LCS , MVT::i64 , Custom);
212 setOperationAction(ISD::ATOMIC_SWAP , MVT::i64 , Custom);
213 }
Evan Cheng54fc97d2008-04-19 01:30:48 +0000214
Chris Lattner6d92cad2006-03-26 10:06:40 +0000215 // We want to custom lower some of our intrinsics.
Chris Lattner48b61a72006-03-28 00:40:33 +0000216 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Chris Lattner6d92cad2006-03-26 10:06:40 +0000217
Chris Lattnera7a58542006-06-16 17:34:12 +0000218 if (TM.getSubtarget<PPCSubtarget>().has64BitSupport()) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000219 // They also have instructions for converting between i64 and fp.
Nate Begemanc09eeec2005-09-06 22:03:27 +0000220 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
Jim Laskeyca367b42006-12-15 14:32:57 +0000221 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000222 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Chris Lattner85c671b2006-12-07 01:24:16 +0000223 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Jim Laskeyca367b42006-12-15 14:32:57 +0000224 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
225
Chris Lattner7fbcef72006-03-24 07:53:47 +0000226 // FIXME: disable this lowered code. This generates 64-bit register values,
227 // and we don't model the fact that the top part is clobbered by calls. We
228 // need to flag these together so that the value isn't live across a call.
229 //setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
230
Nate Begemanae749a92005-10-25 23:48:36 +0000231 // To take advantage of the above i64 FP_TO_SINT, promote i32 FP_TO_UINT
232 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Promote);
233 } else {
Chris Lattner860e8862005-11-17 07:30:41 +0000234 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Nate Begemanae749a92005-10-25 23:48:36 +0000235 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begeman9d2b8172005-10-18 00:56:42 +0000236 }
237
Chris Lattnera7a58542006-06-16 17:34:12 +0000238 if (TM.getSubtarget<PPCSubtarget>().use64BitRegs()) {
Chris Lattner26cb2862007-10-19 04:08:28 +0000239 // 64-bit PowerPC implementations can support i64 types directly
Nate Begeman9d2b8172005-10-18 00:56:42 +0000240 addRegisterClass(MVT::i64, PPC::G8RCRegisterClass);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000241 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
242 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Dan Gohman9ed06db2008-03-07 20:36:53 +0000243 // 64-bit PowerPC wants to expand i128 shifts itself.
244 setOperationAction(ISD::SHL_PARTS, MVT::i64, Custom);
245 setOperationAction(ISD::SRA_PARTS, MVT::i64, Custom);
246 setOperationAction(ISD::SRL_PARTS, MVT::i64, Custom);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000247 } else {
Chris Lattner26cb2862007-10-19 04:08:28 +0000248 // 32-bit PowerPC wants to expand i64 shifts itself.
Chris Lattner3fe6c1d2006-09-20 03:47:40 +0000249 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
250 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
251 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000252 }
Evan Chengd30bf012006-03-01 01:11:20 +0000253
Nate Begeman425a9692005-11-29 08:17:20 +0000254 if (TM.getSubtarget<PPCSubtarget>().hasAltivec()) {
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000255 // First set operation action for all vector types to expand. Then we
256 // will selectively turn on ones that can be effectively codegen'd.
257 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
Dan Gohmanf5135be2007-05-18 23:21:46 +0000258 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Chris Lattnerf3f69de2006-04-16 01:37:57 +0000259 // add/sub are legal for all supported vector VT's.
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000260 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Legal);
261 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Legal);
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000262
Chris Lattner7ff7e672006-04-04 17:25:31 +0000263 // We promote all shuffles to v16i8.
264 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Promote);
Chris Lattnerf3f69de2006-04-16 01:37:57 +0000265 AddPromotedToType (ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, MVT::v16i8);
266
267 // We promote all non-typed operations to v4i32.
268 setOperationAction(ISD::AND , (MVT::ValueType)VT, Promote);
269 AddPromotedToType (ISD::AND , (MVT::ValueType)VT, MVT::v4i32);
270 setOperationAction(ISD::OR , (MVT::ValueType)VT, Promote);
271 AddPromotedToType (ISD::OR , (MVT::ValueType)VT, MVT::v4i32);
272 setOperationAction(ISD::XOR , (MVT::ValueType)VT, Promote);
273 AddPromotedToType (ISD::XOR , (MVT::ValueType)VT, MVT::v4i32);
274 setOperationAction(ISD::LOAD , (MVT::ValueType)VT, Promote);
275 AddPromotedToType (ISD::LOAD , (MVT::ValueType)VT, MVT::v4i32);
276 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
277 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v4i32);
278 setOperationAction(ISD::STORE, (MVT::ValueType)VT, Promote);
279 AddPromotedToType (ISD::STORE, (MVT::ValueType)VT, MVT::v4i32);
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000280
Chris Lattnerf3f69de2006-04-16 01:37:57 +0000281 // No other operations are legal.
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000282 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
283 setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand);
284 setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand);
285 setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand);
286 setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand);
Chris Lattner2ef5e892006-05-24 00:15:25 +0000287 setOperationAction(ISD::FDIV, (MVT::ValueType)VT, Expand);
Evan Cheng66ffe6b2007-07-30 07:51:22 +0000288 setOperationAction(ISD::FNEG, (MVT::ValueType)VT, Expand);
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000289 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
290 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
291 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Expand);
Dan Gohman3ce990d2007-10-08 17:28:24 +0000292 setOperationAction(ISD::UMUL_LOHI, (MVT::ValueType)VT, Expand);
293 setOperationAction(ISD::SMUL_LOHI, (MVT::ValueType)VT, Expand);
294 setOperationAction(ISD::UDIVREM, (MVT::ValueType)VT, Expand);
295 setOperationAction(ISD::SDIVREM, (MVT::ValueType)VT, Expand);
Chris Lattner01cae072006-04-03 23:55:43 +0000296 setOperationAction(ISD::SCALAR_TO_VECTOR, (MVT::ValueType)VT, Expand);
Dan Gohmana3f269f2007-10-12 14:08:57 +0000297 setOperationAction(ISD::FPOW, (MVT::ValueType)VT, Expand);
298 setOperationAction(ISD::CTPOP, (MVT::ValueType)VT, Expand);
299 setOperationAction(ISD::CTLZ, (MVT::ValueType)VT, Expand);
300 setOperationAction(ISD::CTTZ, (MVT::ValueType)VT, Expand);
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000301 }
302
Chris Lattner7ff7e672006-04-04 17:25:31 +0000303 // We can custom expand all VECTOR_SHUFFLEs to VPERM, others we can handle
304 // with merges, splats, etc.
305 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i8, Custom);
306
Chris Lattnerf3f69de2006-04-16 01:37:57 +0000307 setOperationAction(ISD::AND , MVT::v4i32, Legal);
308 setOperationAction(ISD::OR , MVT::v4i32, Legal);
309 setOperationAction(ISD::XOR , MVT::v4i32, Legal);
310 setOperationAction(ISD::LOAD , MVT::v4i32, Legal);
311 setOperationAction(ISD::SELECT, MVT::v4i32, Expand);
312 setOperationAction(ISD::STORE , MVT::v4i32, Legal);
313
Nate Begeman425a9692005-11-29 08:17:20 +0000314 addRegisterClass(MVT::v4f32, PPC::VRRCRegisterClass);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000315 addRegisterClass(MVT::v4i32, PPC::VRRCRegisterClass);
Chris Lattner8d052bc2006-03-25 07:39:07 +0000316 addRegisterClass(MVT::v8i16, PPC::VRRCRegisterClass);
317 addRegisterClass(MVT::v16i8, PPC::VRRCRegisterClass);
Chris Lattnerec4a0c72006-01-29 06:32:58 +0000318
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000319 setOperationAction(ISD::MUL, MVT::v4f32, Legal);
Chris Lattnere7c768e2006-04-18 03:24:30 +0000320 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
Chris Lattner72dd9bd2006-04-18 03:43:48 +0000321 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
Chris Lattner19a81522006-04-18 03:57:35 +0000322 setOperationAction(ISD::MUL, MVT::v16i8, Custom);
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000323
Chris Lattnerb2177b92006-03-19 06:55:52 +0000324 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom);
325 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Custom);
Chris Lattner64b3a082006-03-24 07:48:08 +0000326
Chris Lattner541f91b2006-04-02 00:43:36 +0000327 setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Custom);
328 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Custom);
Chris Lattner64b3a082006-03-24 07:48:08 +0000329 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Custom);
330 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
Nate Begeman425a9692005-11-29 08:17:20 +0000331 }
332
Chris Lattner7b0c58c2006-06-27 17:34:57 +0000333 setShiftAmountType(MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000334 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattner10da9572006-10-18 01:20:43 +0000335
Jim Laskey2ad9f172007-02-22 14:56:36 +0000336 if (TM.getSubtarget<PPCSubtarget>().isPPC64()) {
Chris Lattner10da9572006-10-18 01:20:43 +0000337 setStackPointerRegisterToSaveRestore(PPC::X1);
Jim Laskey2ad9f172007-02-22 14:56:36 +0000338 setExceptionPointerRegister(PPC::X3);
339 setExceptionSelectorRegister(PPC::X4);
340 } else {
Chris Lattner10da9572006-10-18 01:20:43 +0000341 setStackPointerRegisterToSaveRestore(PPC::R1);
Jim Laskey2ad9f172007-02-22 14:56:36 +0000342 setExceptionPointerRegister(PPC::R3);
343 setExceptionSelectorRegister(PPC::R4);
344 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000345
Chris Lattner8c13d0a2006-03-01 04:57:39 +0000346 // We have target-specific dag combine patterns for the following nodes:
347 setTargetDAGCombine(ISD::SINT_TO_FP);
Chris Lattner51269842006-03-01 05:50:56 +0000348 setTargetDAGCombine(ISD::STORE);
Chris Lattner90564f22006-04-18 17:59:36 +0000349 setTargetDAGCombine(ISD::BR_CC);
Chris Lattnerd9989382006-07-10 20:56:58 +0000350 setTargetDAGCombine(ISD::BSWAP);
Chris Lattner8c13d0a2006-03-01 04:57:39 +0000351
Dale Johannesenfabd32d2007-10-19 00:59:18 +0000352 // Darwin long double math library functions have $LDBL128 appended.
353 if (TM.getSubtarget<PPCSubtarget>().isDarwin()) {
Duncan Sands007f9842008-01-10 10:28:30 +0000354 setLibcallName(RTLIB::COS_PPCF128, "cosl$LDBL128");
Dale Johannesenfabd32d2007-10-19 00:59:18 +0000355 setLibcallName(RTLIB::POW_PPCF128, "powl$LDBL128");
356 setLibcallName(RTLIB::REM_PPCF128, "fmodl$LDBL128");
Duncan Sands007f9842008-01-10 10:28:30 +0000357 setLibcallName(RTLIB::SIN_PPCF128, "sinl$LDBL128");
358 setLibcallName(RTLIB::SQRT_PPCF128, "sqrtl$LDBL128");
Dale Johannesenfabd32d2007-10-19 00:59:18 +0000359 }
360
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000361 computeRegisterProperties();
362}
363
Dale Johannesen28d08fd2008-02-28 22:31:51 +0000364/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
365/// function arguments in the caller parameter area.
366unsigned PPCTargetLowering::getByValTypeAlignment(const Type *Ty) const {
367 TargetMachine &TM = getTargetMachine();
368 // Darwin passes everything on 4 byte boundary.
369 if (TM.getSubtarget<PPCSubtarget>().isDarwin())
370 return 4;
371 // FIXME Elf TBD
372 return 4;
373}
374
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000375const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
376 switch (Opcode) {
377 default: return 0;
378 case PPCISD::FSEL: return "PPCISD::FSEL";
379 case PPCISD::FCFID: return "PPCISD::FCFID";
380 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
381 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
Chris Lattner51269842006-03-01 05:50:56 +0000382 case PPCISD::STFIWX: return "PPCISD::STFIWX";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000383 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
384 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000385 case PPCISD::VPERM: return "PPCISD::VPERM";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000386 case PPCISD::Hi: return "PPCISD::Hi";
387 case PPCISD::Lo: return "PPCISD::Lo";
Jim Laskey2060a822006-12-11 18:45:56 +0000388 case PPCISD::DYNALLOC: return "PPCISD::DYNALLOC";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000389 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
390 case PPCISD::SRL: return "PPCISD::SRL";
391 case PPCISD::SRA: return "PPCISD::SRA";
392 case PPCISD::SHL: return "PPCISD::SHL";
Chris Lattnerecfe55e2006-03-22 05:30:33 +0000393 case PPCISD::EXTSW_32: return "PPCISD::EXTSW_32";
394 case PPCISD::STD_32: return "PPCISD::STD_32";
Nicolas Geoffray63f8fb12007-02-27 13:01:19 +0000395 case PPCISD::CALL_ELF: return "PPCISD::CALL_ELF";
396 case PPCISD::CALL_Macho: return "PPCISD::CALL_Macho";
Chris Lattnerc703a8f2006-05-17 19:00:46 +0000397 case PPCISD::MTCTR: return "PPCISD::MTCTR";
Chris Lattner9f0bc652007-02-25 05:34:32 +0000398 case PPCISD::BCTRL_Macho: return "PPCISD::BCTRL_Macho";
399 case PPCISD::BCTRL_ELF: return "PPCISD::BCTRL_ELF";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000400 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
Chris Lattner6d92cad2006-03-26 10:06:40 +0000401 case PPCISD::MFCR: return "PPCISD::MFCR";
Chris Lattnera17b1552006-03-31 05:13:27 +0000402 case PPCISD::VCMP: return "PPCISD::VCMP";
Chris Lattner6d92cad2006-03-26 10:06:40 +0000403 case PPCISD::VCMPo: return "PPCISD::VCMPo";
Chris Lattnerd9989382006-07-10 20:56:58 +0000404 case PPCISD::LBRX: return "PPCISD::LBRX";
405 case PPCISD::STBRX: return "PPCISD::STBRX";
Evan Cheng8608f2e2008-04-19 02:30:38 +0000406 case PPCISD::LARX: return "PPCISD::LARX";
407 case PPCISD::STCX: return "PPCISD::STCX";
Evan Cheng54fc97d2008-04-19 01:30:48 +0000408 case PPCISD::CMP_UNRESERVE: return "PPCISD::CMP_UNRESERVE";
Chris Lattnerf70f8d92006-04-18 18:05:58 +0000409 case PPCISD::COND_BRANCH: return "PPCISD::COND_BRANCH";
Chris Lattneref97c672008-01-18 18:51:16 +0000410 case PPCISD::MFFS: return "PPCISD::MFFS";
411 case PPCISD::MTFSB0: return "PPCISD::MTFSB0";
412 case PPCISD::MTFSB1: return "PPCISD::MTFSB1";
413 case PPCISD::FADDRTZ: return "PPCISD::FADDRTZ";
414 case PPCISD::MTFSF: return "PPCISD::MTFSF";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000415 }
416}
417
Scott Michel5b8f82e2008-03-10 15:42:14 +0000418
419MVT::ValueType
420PPCTargetLowering::getSetCCResultType(const SDOperand &) const {
421 return MVT::i32;
422}
423
424
Chris Lattner1a635d62006-04-14 06:01:58 +0000425//===----------------------------------------------------------------------===//
426// Node matching predicates, for use by the tblgen matching code.
427//===----------------------------------------------------------------------===//
428
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000429/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
430static bool isFloatingPointZero(SDOperand Op) {
431 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +0000432 return CFP->getValueAPF().isZero();
Evan Cheng466685d2006-10-09 20:57:25 +0000433 else if (ISD::isEXTLoad(Op.Val) || ISD::isNON_EXTLoad(Op.Val)) {
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000434 // Maybe this has already been legalized into the constant pool?
435 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
Evan Chengc356a572006-09-12 21:04:05 +0000436 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +0000437 return CFP->getValueAPF().isZero();
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000438 }
439 return false;
440}
441
Chris Lattnerddb739e2006-04-06 17:23:16 +0000442/// isConstantOrUndef - Op is either an undef node or a ConstantSDNode. Return
443/// true if Op is undef or if it matches the specified value.
444static bool isConstantOrUndef(SDOperand Op, unsigned Val) {
445 return Op.getOpcode() == ISD::UNDEF ||
446 cast<ConstantSDNode>(Op)->getValue() == Val;
447}
448
449/// isVPKUHUMShuffleMask - Return true if this is the shuffle mask for a
450/// VPKUHUM instruction.
Chris Lattnerf24380e2006-04-06 22:28:36 +0000451bool PPC::isVPKUHUMShuffleMask(SDNode *N, bool isUnary) {
452 if (!isUnary) {
453 for (unsigned i = 0; i != 16; ++i)
454 if (!isConstantOrUndef(N->getOperand(i), i*2+1))
455 return false;
456 } else {
457 for (unsigned i = 0; i != 8; ++i)
458 if (!isConstantOrUndef(N->getOperand(i), i*2+1) ||
459 !isConstantOrUndef(N->getOperand(i+8), i*2+1))
460 return false;
461 }
Chris Lattnerd0608e12006-04-06 18:26:28 +0000462 return true;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000463}
464
465/// isVPKUWUMShuffleMask - Return true if this is the shuffle mask for a
466/// VPKUWUM instruction.
Chris Lattnerf24380e2006-04-06 22:28:36 +0000467bool PPC::isVPKUWUMShuffleMask(SDNode *N, bool isUnary) {
468 if (!isUnary) {
469 for (unsigned i = 0; i != 16; i += 2)
470 if (!isConstantOrUndef(N->getOperand(i ), i*2+2) ||
471 !isConstantOrUndef(N->getOperand(i+1), i*2+3))
472 return false;
473 } else {
474 for (unsigned i = 0; i != 8; i += 2)
475 if (!isConstantOrUndef(N->getOperand(i ), i*2+2) ||
476 !isConstantOrUndef(N->getOperand(i+1), i*2+3) ||
477 !isConstantOrUndef(N->getOperand(i+8), i*2+2) ||
478 !isConstantOrUndef(N->getOperand(i+9), i*2+3))
479 return false;
480 }
Chris Lattnerd0608e12006-04-06 18:26:28 +0000481 return true;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000482}
483
Chris Lattnercaad1632006-04-06 22:02:42 +0000484/// isVMerge - Common function, used to match vmrg* shuffles.
485///
486static bool isVMerge(SDNode *N, unsigned UnitSize,
487 unsigned LHSStart, unsigned RHSStart) {
Chris Lattner116cc482006-04-06 21:11:54 +0000488 assert(N->getOpcode() == ISD::BUILD_VECTOR &&
489 N->getNumOperands() == 16 && "PPC only supports shuffles by bytes!");
490 assert((UnitSize == 1 || UnitSize == 2 || UnitSize == 4) &&
491 "Unsupported merge size!");
492
493 for (unsigned i = 0; i != 8/UnitSize; ++i) // Step over units
494 for (unsigned j = 0; j != UnitSize; ++j) { // Step over bytes within unit
495 if (!isConstantOrUndef(N->getOperand(i*UnitSize*2+j),
Chris Lattnercaad1632006-04-06 22:02:42 +0000496 LHSStart+j+i*UnitSize) ||
Chris Lattner116cc482006-04-06 21:11:54 +0000497 !isConstantOrUndef(N->getOperand(i*UnitSize*2+UnitSize+j),
Chris Lattnercaad1632006-04-06 22:02:42 +0000498 RHSStart+j+i*UnitSize))
Chris Lattner116cc482006-04-06 21:11:54 +0000499 return false;
500 }
Chris Lattnercaad1632006-04-06 22:02:42 +0000501 return true;
502}
503
504/// isVMRGLShuffleMask - Return true if this is a shuffle mask suitable for
505/// a VRGL* instruction with the specified unit size (1,2 or 4 bytes).
506bool PPC::isVMRGLShuffleMask(SDNode *N, unsigned UnitSize, bool isUnary) {
507 if (!isUnary)
508 return isVMerge(N, UnitSize, 8, 24);
509 return isVMerge(N, UnitSize, 8, 8);
Chris Lattner116cc482006-04-06 21:11:54 +0000510}
511
512/// isVMRGHShuffleMask - Return true if this is a shuffle mask suitable for
513/// a VRGH* instruction with the specified unit size (1,2 or 4 bytes).
Chris Lattnercaad1632006-04-06 22:02:42 +0000514bool PPC::isVMRGHShuffleMask(SDNode *N, unsigned UnitSize, bool isUnary) {
515 if (!isUnary)
516 return isVMerge(N, UnitSize, 0, 16);
517 return isVMerge(N, UnitSize, 0, 0);
Chris Lattner116cc482006-04-06 21:11:54 +0000518}
519
520
Chris Lattnerd0608e12006-04-06 18:26:28 +0000521/// isVSLDOIShuffleMask - If this is a vsldoi shuffle mask, return the shift
522/// amount, otherwise return -1.
Chris Lattnerf24380e2006-04-06 22:28:36 +0000523int PPC::isVSLDOIShuffleMask(SDNode *N, bool isUnary) {
Chris Lattner116cc482006-04-06 21:11:54 +0000524 assert(N->getOpcode() == ISD::BUILD_VECTOR &&
525 N->getNumOperands() == 16 && "PPC only supports shuffles by bytes!");
Chris Lattnerd0608e12006-04-06 18:26:28 +0000526 // Find the first non-undef value in the shuffle mask.
527 unsigned i;
528 for (i = 0; i != 16 && N->getOperand(i).getOpcode() == ISD::UNDEF; ++i)
529 /*search*/;
530
531 if (i == 16) return -1; // all undef.
532
533 // Otherwise, check to see if the rest of the elements are consequtively
534 // numbered from this value.
535 unsigned ShiftAmt = cast<ConstantSDNode>(N->getOperand(i))->getValue();
536 if (ShiftAmt < i) return -1;
537 ShiftAmt -= i;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000538
Chris Lattnerf24380e2006-04-06 22:28:36 +0000539 if (!isUnary) {
540 // Check the rest of the elements to see if they are consequtive.
541 for (++i; i != 16; ++i)
542 if (!isConstantOrUndef(N->getOperand(i), ShiftAmt+i))
543 return -1;
544 } else {
545 // Check the rest of the elements to see if they are consequtive.
546 for (++i; i != 16; ++i)
547 if (!isConstantOrUndef(N->getOperand(i), (ShiftAmt+i) & 15))
548 return -1;
549 }
Chris Lattnerd0608e12006-04-06 18:26:28 +0000550
551 return ShiftAmt;
552}
Chris Lattneref819f82006-03-20 06:33:01 +0000553
554/// isSplatShuffleMask - Return true if the specified VECTOR_SHUFFLE operand
555/// specifies a splat of a single element that is suitable for input to
556/// VSPLTB/VSPLTH/VSPLTW.
Chris Lattner7ff7e672006-04-04 17:25:31 +0000557bool PPC::isSplatShuffleMask(SDNode *N, unsigned EltSize) {
558 assert(N->getOpcode() == ISD::BUILD_VECTOR &&
559 N->getNumOperands() == 16 &&
560 (EltSize == 1 || EltSize == 2 || EltSize == 4));
Chris Lattnerdd4d2d02006-03-20 06:51:10 +0000561
Chris Lattner88a99ef2006-03-20 06:37:44 +0000562 // This is a splat operation if each element of the permute is the same, and
563 // if the value doesn't reference the second vector.
Chris Lattner7ff7e672006-04-04 17:25:31 +0000564 unsigned ElementBase = 0;
Chris Lattner88a99ef2006-03-20 06:37:44 +0000565 SDOperand Elt = N->getOperand(0);
Chris Lattner7ff7e672006-04-04 17:25:31 +0000566 if (ConstantSDNode *EltV = dyn_cast<ConstantSDNode>(Elt))
567 ElementBase = EltV->getValue();
568 else
569 return false; // FIXME: Handle UNDEF elements too!
570
571 if (cast<ConstantSDNode>(Elt)->getValue() >= 16)
572 return false;
573
574 // Check that they are consequtive.
575 for (unsigned i = 1; i != EltSize; ++i) {
576 if (!isa<ConstantSDNode>(N->getOperand(i)) ||
577 cast<ConstantSDNode>(N->getOperand(i))->getValue() != i+ElementBase)
578 return false;
579 }
580
Chris Lattner88a99ef2006-03-20 06:37:44 +0000581 assert(isa<ConstantSDNode>(Elt) && "Invalid VECTOR_SHUFFLE mask!");
Chris Lattner7ff7e672006-04-04 17:25:31 +0000582 for (unsigned i = EltSize, e = 16; i != e; i += EltSize) {
Chris Lattnerb097aa92006-04-14 23:19:08 +0000583 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Chris Lattner88a99ef2006-03-20 06:37:44 +0000584 assert(isa<ConstantSDNode>(N->getOperand(i)) &&
585 "Invalid VECTOR_SHUFFLE mask!");
Chris Lattner7ff7e672006-04-04 17:25:31 +0000586 for (unsigned j = 0; j != EltSize; ++j)
587 if (N->getOperand(i+j) != N->getOperand(j))
588 return false;
Chris Lattner88a99ef2006-03-20 06:37:44 +0000589 }
590
Chris Lattner7ff7e672006-04-04 17:25:31 +0000591 return true;
Chris Lattneref819f82006-03-20 06:33:01 +0000592}
593
Evan Cheng66ffe6b2007-07-30 07:51:22 +0000594/// isAllNegativeZeroVector - Returns true if all elements of build_vector
595/// are -0.0.
596bool PPC::isAllNegativeZeroVector(SDNode *N) {
597 assert(N->getOpcode() == ISD::BUILD_VECTOR);
598 if (PPC::isSplatShuffleMask(N, N->getNumOperands()))
599 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N))
Dale Johanneseneaf08942007-08-31 04:03:46 +0000600 return CFP->getValueAPF().isNegZero();
Evan Cheng66ffe6b2007-07-30 07:51:22 +0000601 return false;
602}
603
Chris Lattneref819f82006-03-20 06:33:01 +0000604/// getVSPLTImmediate - Return the appropriate VSPLT* immediate to splat the
605/// specified isSplatShuffleMask VECTOR_SHUFFLE mask.
Chris Lattner7ff7e672006-04-04 17:25:31 +0000606unsigned PPC::getVSPLTImmediate(SDNode *N, unsigned EltSize) {
607 assert(isSplatShuffleMask(N, EltSize));
608 return cast<ConstantSDNode>(N->getOperand(0))->getValue() / EltSize;
Chris Lattneref819f82006-03-20 06:33:01 +0000609}
610
Chris Lattnere87192a2006-04-12 17:37:20 +0000611/// get_VSPLTI_elt - If this is a build_vector of constants which can be formed
Chris Lattner140a58f2006-04-08 06:46:53 +0000612/// by using a vspltis[bhw] instruction of the specified element size, return
613/// the constant being splatted. The ByteSize field indicates the number of
614/// bytes of each element [124] -> [bhw].
Chris Lattnere87192a2006-04-12 17:37:20 +0000615SDOperand PPC::get_VSPLTI_elt(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000616 SDOperand OpVal(0, 0);
Chris Lattner79d9a882006-04-08 07:14:26 +0000617
618 // If ByteSize of the splat is bigger than the element size of the
619 // build_vector, then we have a case where we are checking for a splat where
620 // multiple elements of the buildvector are folded together into a single
621 // logical element of the splat (e.g. "vsplish 1" to splat {0,1}*8).
622 unsigned EltSize = 16/N->getNumOperands();
623 if (EltSize < ByteSize) {
624 unsigned Multiple = ByteSize/EltSize; // Number of BV entries per spltval.
625 SDOperand UniquedVals[4];
626 assert(Multiple > 1 && Multiple <= 4 && "How can this happen?");
627
628 // See if all of the elements in the buildvector agree across.
629 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
630 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
631 // If the element isn't a constant, bail fully out.
632 if (!isa<ConstantSDNode>(N->getOperand(i))) return SDOperand();
633
634
635 if (UniquedVals[i&(Multiple-1)].Val == 0)
636 UniquedVals[i&(Multiple-1)] = N->getOperand(i);
637 else if (UniquedVals[i&(Multiple-1)] != N->getOperand(i))
638 return SDOperand(); // no match.
639 }
640
641 // Okay, if we reached this point, UniquedVals[0..Multiple-1] contains
642 // either constant or undef values that are identical for each chunk. See
643 // if these chunks can form into a larger vspltis*.
644
645 // Check to see if all of the leading entries are either 0 or -1. If
646 // neither, then this won't fit into the immediate field.
647 bool LeadingZero = true;
648 bool LeadingOnes = true;
649 for (unsigned i = 0; i != Multiple-1; ++i) {
650 if (UniquedVals[i].Val == 0) continue; // Must have been undefs.
651
652 LeadingZero &= cast<ConstantSDNode>(UniquedVals[i])->isNullValue();
653 LeadingOnes &= cast<ConstantSDNode>(UniquedVals[i])->isAllOnesValue();
654 }
655 // Finally, check the least significant entry.
656 if (LeadingZero) {
657 if (UniquedVals[Multiple-1].Val == 0)
658 return DAG.getTargetConstant(0, MVT::i32); // 0,0,0,undef
659 int Val = cast<ConstantSDNode>(UniquedVals[Multiple-1])->getValue();
660 if (Val < 16)
661 return DAG.getTargetConstant(Val, MVT::i32); // 0,0,0,4 -> vspltisw(4)
662 }
663 if (LeadingOnes) {
664 if (UniquedVals[Multiple-1].Val == 0)
665 return DAG.getTargetConstant(~0U, MVT::i32); // -1,-1,-1,undef
666 int Val =cast<ConstantSDNode>(UniquedVals[Multiple-1])->getSignExtended();
667 if (Val >= -16) // -1,-1,-1,-2 -> vspltisw(-2)
668 return DAG.getTargetConstant(Val, MVT::i32);
669 }
670
671 return SDOperand();
672 }
673
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000674 // Check to see if this buildvec has a single non-undef value in its elements.
675 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
676 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
677 if (OpVal.Val == 0)
678 OpVal = N->getOperand(i);
679 else if (OpVal != N->getOperand(i))
Chris Lattner140a58f2006-04-08 06:46:53 +0000680 return SDOperand();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000681 }
682
Chris Lattner140a58f2006-04-08 06:46:53 +0000683 if (OpVal.Val == 0) return SDOperand(); // All UNDEF: use implicit def.
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000684
Nate Begeman98e70cc2006-03-28 04:15:58 +0000685 unsigned ValSizeInBytes = 0;
686 uint64_t Value = 0;
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000687 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
688 Value = CN->getValue();
689 ValSizeInBytes = MVT::getSizeInBits(CN->getValueType(0))/8;
690 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) {
691 assert(CN->getValueType(0) == MVT::f32 && "Only one legal FP vector type!");
Dale Johanneseneaf08942007-08-31 04:03:46 +0000692 Value = FloatToBits(CN->getValueAPF().convertToFloat());
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000693 ValSizeInBytes = 4;
694 }
695
696 // If the splat value is larger than the element value, then we can never do
697 // this splat. The only case that we could fit the replicated bits into our
698 // immediate field for would be zero, and we prefer to use vxor for it.
Chris Lattner140a58f2006-04-08 06:46:53 +0000699 if (ValSizeInBytes < ByteSize) return SDOperand();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000700
701 // If the element value is larger than the splat value, cut it in half and
702 // check to see if the two halves are equal. Continue doing this until we
703 // get to ByteSize. This allows us to handle 0x01010101 as 0x01.
704 while (ValSizeInBytes > ByteSize) {
705 ValSizeInBytes >>= 1;
706
707 // If the top half equals the bottom half, we're still ok.
Chris Lattner9b42bdd2006-04-05 17:39:25 +0000708 if (((Value >> (ValSizeInBytes*8)) & ((1 << (8*ValSizeInBytes))-1)) !=
709 (Value & ((1 << (8*ValSizeInBytes))-1)))
Chris Lattner140a58f2006-04-08 06:46:53 +0000710 return SDOperand();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000711 }
712
713 // Properly sign extend the value.
714 int ShAmt = (4-ByteSize)*8;
715 int MaskVal = ((int)Value << ShAmt) >> ShAmt;
716
Evan Cheng5b6a01b2006-03-26 09:52:32 +0000717 // If this is zero, don't match, zero matches ISD::isBuildVectorAllZeros.
Chris Lattner140a58f2006-04-08 06:46:53 +0000718 if (MaskVal == 0) return SDOperand();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000719
Chris Lattner140a58f2006-04-08 06:46:53 +0000720 // Finally, if this value fits in a 5 bit sext field, return it
721 if (((MaskVal << (32-5)) >> (32-5)) == MaskVal)
722 return DAG.getTargetConstant(MaskVal, MVT::i32);
723 return SDOperand();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000724}
725
Chris Lattner1a635d62006-04-14 06:01:58 +0000726//===----------------------------------------------------------------------===//
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000727// Addressing Mode Selection
728//===----------------------------------------------------------------------===//
729
730/// isIntS16Immediate - This method tests to see if the node is either a 32-bit
731/// or 64-bit immediate, and if the value can be accurately represented as a
732/// sign extension from a 16-bit value. If so, this returns true and the
733/// immediate.
734static bool isIntS16Immediate(SDNode *N, short &Imm) {
735 if (N->getOpcode() != ISD::Constant)
736 return false;
737
738 Imm = (short)cast<ConstantSDNode>(N)->getValue();
739 if (N->getValueType(0) == MVT::i32)
740 return Imm == (int32_t)cast<ConstantSDNode>(N)->getValue();
741 else
742 return Imm == (int64_t)cast<ConstantSDNode>(N)->getValue();
743}
744static bool isIntS16Immediate(SDOperand Op, short &Imm) {
745 return isIntS16Immediate(Op.Val, Imm);
746}
747
748
749/// SelectAddressRegReg - Given the specified addressed, check to see if it
750/// can be represented as an indexed [r+r] operation. Returns false if it
751/// can be more efficiently represented with [r+imm].
752bool PPCTargetLowering::SelectAddressRegReg(SDOperand N, SDOperand &Base,
753 SDOperand &Index,
754 SelectionDAG &DAG) {
755 short imm = 0;
756 if (N.getOpcode() == ISD::ADD) {
757 if (isIntS16Immediate(N.getOperand(1), imm))
758 return false; // r+i
759 if (N.getOperand(1).getOpcode() == PPCISD::Lo)
760 return false; // r+i
761
762 Base = N.getOperand(0);
763 Index = N.getOperand(1);
764 return true;
765 } else if (N.getOpcode() == ISD::OR) {
766 if (isIntS16Immediate(N.getOperand(1), imm))
767 return false; // r+i can fold it if we can.
768
769 // If this is an or of disjoint bitfields, we can codegen this as an add
770 // (for better address arithmetic) if the LHS and RHS of the OR are provably
771 // disjoint.
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000772 APInt LHSKnownZero, LHSKnownOne;
773 APInt RHSKnownZero, RHSKnownOne;
774 DAG.ComputeMaskedBits(N.getOperand(0),
Dan Gohmanec59b952008-02-27 21:12:32 +0000775 APInt::getAllOnesValue(N.getOperand(0)
776 .getValueSizeInBits()),
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000777 LHSKnownZero, LHSKnownOne);
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000778
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000779 if (LHSKnownZero.getBoolValue()) {
780 DAG.ComputeMaskedBits(N.getOperand(1),
Dan Gohmanec59b952008-02-27 21:12:32 +0000781 APInt::getAllOnesValue(N.getOperand(1)
782 .getValueSizeInBits()),
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000783 RHSKnownZero, RHSKnownOne);
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000784 // If all of the bits are known zero on the LHS or RHS, the add won't
785 // carry.
Dan Gohmanec59b952008-02-27 21:12:32 +0000786 if (~(LHSKnownZero | RHSKnownZero) == 0) {
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000787 Base = N.getOperand(0);
788 Index = N.getOperand(1);
789 return true;
790 }
791 }
792 }
793
794 return false;
795}
796
797/// Returns true if the address N can be represented by a base register plus
798/// a signed 16-bit displacement [r+imm], and if it is not better
799/// represented as reg+reg.
800bool PPCTargetLowering::SelectAddressRegImm(SDOperand N, SDOperand &Disp,
801 SDOperand &Base, SelectionDAG &DAG){
802 // If this can be more profitably realized as r+r, fail.
803 if (SelectAddressRegReg(N, Disp, Base, DAG))
804 return false;
805
806 if (N.getOpcode() == ISD::ADD) {
807 short imm = 0;
808 if (isIntS16Immediate(N.getOperand(1), imm)) {
809 Disp = DAG.getTargetConstant((int)imm & 0xFFFF, MVT::i32);
810 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
811 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
812 } else {
813 Base = N.getOperand(0);
814 }
815 return true; // [r+i]
816 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
817 // Match LOAD (ADD (X, Lo(G))).
818 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getValue()
819 && "Cannot handle constant offsets yet!");
820 Disp = N.getOperand(1).getOperand(0); // The global address.
821 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
822 Disp.getOpcode() == ISD::TargetConstantPool ||
823 Disp.getOpcode() == ISD::TargetJumpTable);
824 Base = N.getOperand(0);
825 return true; // [&g+r]
826 }
827 } else if (N.getOpcode() == ISD::OR) {
828 short imm = 0;
829 if (isIntS16Immediate(N.getOperand(1), imm)) {
830 // If this is an or of disjoint bitfields, we can codegen this as an add
831 // (for better address arithmetic) if the LHS and RHS of the OR are
832 // provably disjoint.
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000833 APInt LHSKnownZero, LHSKnownOne;
834 DAG.ComputeMaskedBits(N.getOperand(0),
Bill Wendling3e98c302008-03-24 23:16:37 +0000835 APInt::getAllOnesValue(N.getOperand(0)
836 .getValueSizeInBits()),
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000837 LHSKnownZero, LHSKnownOne);
Bill Wendling3e98c302008-03-24 23:16:37 +0000838
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000839 if ((LHSKnownZero.getZExtValue()|~(uint64_t)imm) == ~0ULL) {
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000840 // If all of the bits are known zero on the LHS or RHS, the add won't
841 // carry.
842 Base = N.getOperand(0);
843 Disp = DAG.getTargetConstant((int)imm & 0xFFFF, MVT::i32);
844 return true;
845 }
846 }
847 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
848 // Loading from a constant address.
849
850 // If this address fits entirely in a 16-bit sext immediate field, codegen
851 // this as "d, 0"
852 short Imm;
853 if (isIntS16Immediate(CN, Imm)) {
854 Disp = DAG.getTargetConstant(Imm, CN->getValueType(0));
855 Base = DAG.getRegister(PPC::R0, CN->getValueType(0));
856 return true;
857 }
Chris Lattnerbc681d62007-02-17 06:44:03 +0000858
859 // Handle 32-bit sext immediates with LIS + addr mode.
860 if (CN->getValueType(0) == MVT::i32 ||
861 (int64_t)CN->getValue() == (int)CN->getValue()) {
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000862 int Addr = (int)CN->getValue();
863
864 // Otherwise, break this down into an LIS + disp.
Chris Lattnerbc681d62007-02-17 06:44:03 +0000865 Disp = DAG.getTargetConstant((short)Addr, MVT::i32);
866
867 Base = DAG.getTargetConstant((Addr - (signed short)Addr) >> 16, MVT::i32);
868 unsigned Opc = CN->getValueType(0) == MVT::i32 ? PPC::LIS : PPC::LIS8;
869 Base = SDOperand(DAG.getTargetNode(Opc, CN->getValueType(0), Base), 0);
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000870 return true;
871 }
872 }
873
874 Disp = DAG.getTargetConstant(0, getPointerTy());
875 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
876 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
877 else
878 Base = N;
879 return true; // [r+0]
880}
881
882/// SelectAddressRegRegOnly - Given the specified addressed, force it to be
883/// represented as an indexed [r+r] operation.
884bool PPCTargetLowering::SelectAddressRegRegOnly(SDOperand N, SDOperand &Base,
885 SDOperand &Index,
886 SelectionDAG &DAG) {
887 // Check to see if we can easily represent this as an [r+r] address. This
888 // will fail if it thinks that the address is more profitably represented as
889 // reg+imm, e.g. where imm = 0.
890 if (SelectAddressRegReg(N, Base, Index, DAG))
891 return true;
892
893 // If the operand is an addition, always emit this as [r+r], since this is
894 // better (for code size, and execution, as the memop does the add for free)
895 // than emitting an explicit add.
896 if (N.getOpcode() == ISD::ADD) {
897 Base = N.getOperand(0);
898 Index = N.getOperand(1);
899 return true;
900 }
901
902 // Otherwise, do it the hard way, using R0 as the base register.
903 Base = DAG.getRegister(PPC::R0, N.getValueType());
904 Index = N;
905 return true;
906}
907
908/// SelectAddressRegImmShift - Returns true if the address N can be
909/// represented by a base register plus a signed 14-bit displacement
910/// [r+imm*4]. Suitable for use by STD and friends.
911bool PPCTargetLowering::SelectAddressRegImmShift(SDOperand N, SDOperand &Disp,
912 SDOperand &Base,
913 SelectionDAG &DAG) {
914 // If this can be more profitably realized as r+r, fail.
915 if (SelectAddressRegReg(N, Disp, Base, DAG))
916 return false;
917
918 if (N.getOpcode() == ISD::ADD) {
919 short imm = 0;
920 if (isIntS16Immediate(N.getOperand(1), imm) && (imm & 3) == 0) {
921 Disp = DAG.getTargetConstant(((int)imm & 0xFFFF) >> 2, MVT::i32);
922 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
923 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
924 } else {
925 Base = N.getOperand(0);
926 }
927 return true; // [r+i]
928 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
929 // Match LOAD (ADD (X, Lo(G))).
930 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getValue()
931 && "Cannot handle constant offsets yet!");
932 Disp = N.getOperand(1).getOperand(0); // The global address.
933 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
934 Disp.getOpcode() == ISD::TargetConstantPool ||
935 Disp.getOpcode() == ISD::TargetJumpTable);
936 Base = N.getOperand(0);
937 return true; // [&g+r]
938 }
939 } else if (N.getOpcode() == ISD::OR) {
940 short imm = 0;
941 if (isIntS16Immediate(N.getOperand(1), imm) && (imm & 3) == 0) {
942 // If this is an or of disjoint bitfields, we can codegen this as an add
943 // (for better address arithmetic) if the LHS and RHS of the OR are
944 // provably disjoint.
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000945 APInt LHSKnownZero, LHSKnownOne;
946 DAG.ComputeMaskedBits(N.getOperand(0),
Bill Wendling3e98c302008-03-24 23:16:37 +0000947 APInt::getAllOnesValue(N.getOperand(0)
948 .getValueSizeInBits()),
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000949 LHSKnownZero, LHSKnownOne);
950 if ((LHSKnownZero.getZExtValue()|~(uint64_t)imm) == ~0ULL) {
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000951 // If all of the bits are known zero on the LHS or RHS, the add won't
952 // carry.
953 Base = N.getOperand(0);
954 Disp = DAG.getTargetConstant(((int)imm & 0xFFFF) >> 2, MVT::i32);
955 return true;
956 }
957 }
958 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
Chris Lattnerdee5a5a2007-02-17 06:57:26 +0000959 // Loading from a constant address. Verify low two bits are clear.
960 if ((CN->getValue() & 3) == 0) {
961 // If this address fits entirely in a 14-bit sext immediate field, codegen
962 // this as "d, 0"
963 short Imm;
964 if (isIntS16Immediate(CN, Imm)) {
965 Disp = DAG.getTargetConstant((unsigned short)Imm >> 2, getPointerTy());
966 Base = DAG.getRegister(PPC::R0, CN->getValueType(0));
967 return true;
968 }
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000969
Chris Lattnerdee5a5a2007-02-17 06:57:26 +0000970 // Fold the low-part of 32-bit absolute addresses into addr mode.
971 if (CN->getValueType(0) == MVT::i32 ||
972 (int64_t)CN->getValue() == (int)CN->getValue()) {
973 int Addr = (int)CN->getValue();
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000974
Chris Lattnerdee5a5a2007-02-17 06:57:26 +0000975 // Otherwise, break this down into an LIS + disp.
976 Disp = DAG.getTargetConstant((short)Addr >> 2, MVT::i32);
977
978 Base = DAG.getTargetConstant((Addr-(signed short)Addr) >> 16, MVT::i32);
979 unsigned Opc = CN->getValueType(0) == MVT::i32 ? PPC::LIS : PPC::LIS8;
980 Base = SDOperand(DAG.getTargetNode(Opc, CN->getValueType(0), Base), 0);
981 return true;
982 }
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000983 }
984 }
985
986 Disp = DAG.getTargetConstant(0, getPointerTy());
987 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
988 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
989 else
990 Base = N;
991 return true; // [r+0]
992}
993
994
995/// getPreIndexedAddressParts - returns true by value, base pointer and
996/// offset pointer and addressing mode by reference if the node's address
997/// can be legally represented as pre-indexed load / store address.
998bool PPCTargetLowering::getPreIndexedAddressParts(SDNode *N, SDOperand &Base,
999 SDOperand &Offset,
Evan Cheng144d8f02006-11-09 17:55:04 +00001000 ISD::MemIndexedMode &AM,
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001001 SelectionDAG &DAG) {
Chris Lattner4eab7142006-11-10 02:08:47 +00001002 // Disabled by default for now.
1003 if (!EnablePPCPreinc) return false;
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001004
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001005 SDOperand Ptr;
Chris Lattner2fe4bf42006-11-14 01:38:31 +00001006 MVT::ValueType VT;
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001007 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
1008 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001009 VT = LD->getMemoryVT();
Chris Lattner0851b4f2006-11-15 19:55:13 +00001010
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001011 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattner4eab7142006-11-10 02:08:47 +00001012 ST = ST;
Chris Lattner2fe4bf42006-11-14 01:38:31 +00001013 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001014 VT = ST->getMemoryVT();
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001015 } else
1016 return false;
1017
Chris Lattner2fe4bf42006-11-14 01:38:31 +00001018 // PowerPC doesn't have preinc load/store instructions for vectors.
1019 if (MVT::isVector(VT))
1020 return false;
1021
Chris Lattner0851b4f2006-11-15 19:55:13 +00001022 // TODO: Check reg+reg first.
1023
1024 // LDU/STU use reg+imm*4, others use reg+imm.
1025 if (VT != MVT::i64) {
1026 // reg + imm
1027 if (!SelectAddressRegImm(Ptr, Offset, Base, DAG))
1028 return false;
1029 } else {
1030 // reg + imm * 4.
1031 if (!SelectAddressRegImmShift(Ptr, Offset, Base, DAG))
1032 return false;
1033 }
Chris Lattnerf6edf4d2006-11-11 00:08:42 +00001034
Chris Lattnerf6edf4d2006-11-11 00:08:42 +00001035 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattner0851b4f2006-11-15 19:55:13 +00001036 // PPC64 doesn't have lwau, but it does have lwaux. Reject preinc load of
1037 // sext i32 to i64 when addr mode is r+i.
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001038 if (LD->getValueType(0) == MVT::i64 && LD->getMemoryVT() == MVT::i32 &&
Chris Lattnerf6edf4d2006-11-11 00:08:42 +00001039 LD->getExtensionType() == ISD::SEXTLOAD &&
1040 isa<ConstantSDNode>(Offset))
1041 return false;
Chris Lattner0851b4f2006-11-15 19:55:13 +00001042 }
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001043
Chris Lattner4eab7142006-11-10 02:08:47 +00001044 AM = ISD::PRE_INC;
1045 return true;
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001046}
1047
1048//===----------------------------------------------------------------------===//
Chris Lattner1a635d62006-04-14 06:01:58 +00001049// LowerOperation implementation
1050//===----------------------------------------------------------------------===//
1051
Dale Johannesen5b3b6952008-03-04 23:17:14 +00001052SDOperand PPCTargetLowering::LowerConstantPool(SDOperand Op,
1053 SelectionDAG &DAG) {
Chris Lattner059ca0f2006-06-16 21:01:35 +00001054 MVT::ValueType PtrVT = Op.getValueType();
Chris Lattner1a635d62006-04-14 06:01:58 +00001055 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Chengc356a572006-09-12 21:04:05 +00001056 Constant *C = CP->getConstVal();
Chris Lattner059ca0f2006-06-16 21:01:35 +00001057 SDOperand CPI = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment());
1058 SDOperand Zero = DAG.getConstant(0, PtrVT);
Chris Lattner1a635d62006-04-14 06:01:58 +00001059
1060 const TargetMachine &TM = DAG.getTarget();
1061
Chris Lattner059ca0f2006-06-16 21:01:35 +00001062 SDOperand Hi = DAG.getNode(PPCISD::Hi, PtrVT, CPI, Zero);
1063 SDOperand Lo = DAG.getNode(PPCISD::Lo, PtrVT, CPI, Zero);
1064
Chris Lattner1a635d62006-04-14 06:01:58 +00001065 // If this is a non-darwin platform, we don't support non-static relo models
1066 // yet.
1067 if (TM.getRelocationModel() == Reloc::Static ||
1068 !TM.getSubtarget<PPCSubtarget>().isDarwin()) {
1069 // Generate non-pic code that has direct accesses to the constant pool.
1070 // The address of the global is just (hi(&g)+lo(&g)).
Chris Lattner059ca0f2006-06-16 21:01:35 +00001071 return DAG.getNode(ISD::ADD, PtrVT, Hi, Lo);
Chris Lattner1a635d62006-04-14 06:01:58 +00001072 }
1073
Chris Lattner35d86fe2006-07-26 21:12:04 +00001074 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattner1a635d62006-04-14 06:01:58 +00001075 // With PIC, the first instruction is actually "GR+hi(&G)".
Chris Lattner059ca0f2006-06-16 21:01:35 +00001076 Hi = DAG.getNode(ISD::ADD, PtrVT,
1077 DAG.getNode(PPCISD::GlobalBaseReg, PtrVT), Hi);
Chris Lattner1a635d62006-04-14 06:01:58 +00001078 }
1079
Chris Lattner059ca0f2006-06-16 21:01:35 +00001080 Lo = DAG.getNode(ISD::ADD, PtrVT, Hi, Lo);
Chris Lattner1a635d62006-04-14 06:01:58 +00001081 return Lo;
1082}
1083
Dale Johannesen5b3b6952008-03-04 23:17:14 +00001084SDOperand PPCTargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner059ca0f2006-06-16 21:01:35 +00001085 MVT::ValueType PtrVT = Op.getValueType();
Nate Begeman37efe672006-04-22 18:53:45 +00001086 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Chris Lattner059ca0f2006-06-16 21:01:35 +00001087 SDOperand JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
1088 SDOperand Zero = DAG.getConstant(0, PtrVT);
Nate Begeman37efe672006-04-22 18:53:45 +00001089
1090 const TargetMachine &TM = DAG.getTarget();
Chris Lattner059ca0f2006-06-16 21:01:35 +00001091
1092 SDOperand Hi = DAG.getNode(PPCISD::Hi, PtrVT, JTI, Zero);
1093 SDOperand Lo = DAG.getNode(PPCISD::Lo, PtrVT, JTI, Zero);
1094
Nate Begeman37efe672006-04-22 18:53:45 +00001095 // If this is a non-darwin platform, we don't support non-static relo models
1096 // yet.
1097 if (TM.getRelocationModel() == Reloc::Static ||
1098 !TM.getSubtarget<PPCSubtarget>().isDarwin()) {
1099 // Generate non-pic code that has direct accesses to the constant pool.
1100 // The address of the global is just (hi(&g)+lo(&g)).
Chris Lattner059ca0f2006-06-16 21:01:35 +00001101 return DAG.getNode(ISD::ADD, PtrVT, Hi, Lo);
Nate Begeman37efe672006-04-22 18:53:45 +00001102 }
1103
Chris Lattner35d86fe2006-07-26 21:12:04 +00001104 if (TM.getRelocationModel() == Reloc::PIC_) {
Nate Begeman37efe672006-04-22 18:53:45 +00001105 // With PIC, the first instruction is actually "GR+hi(&G)".
Chris Lattner059ca0f2006-06-16 21:01:35 +00001106 Hi = DAG.getNode(ISD::ADD, PtrVT,
Chris Lattner0d72a202006-07-28 16:45:47 +00001107 DAG.getNode(PPCISD::GlobalBaseReg, PtrVT), Hi);
Nate Begeman37efe672006-04-22 18:53:45 +00001108 }
1109
Chris Lattner059ca0f2006-06-16 21:01:35 +00001110 Lo = DAG.getNode(ISD::ADD, PtrVT, Hi, Lo);
Nate Begeman37efe672006-04-22 18:53:45 +00001111 return Lo;
1112}
1113
Dale Johannesen5b3b6952008-03-04 23:17:14 +00001114SDOperand PPCTargetLowering::LowerGlobalTLSAddress(SDOperand Op,
1115 SelectionDAG &DAG) {
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +00001116 assert(0 && "TLS not implemented for PPC.");
Chris Lattnerd27c9912008-03-30 18:22:13 +00001117 return SDOperand(); // Not reached
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +00001118}
1119
Dale Johannesen5b3b6952008-03-04 23:17:14 +00001120SDOperand PPCTargetLowering::LowerGlobalAddress(SDOperand Op,
1121 SelectionDAG &DAG) {
Chris Lattner059ca0f2006-06-16 21:01:35 +00001122 MVT::ValueType PtrVT = Op.getValueType();
Chris Lattner1a635d62006-04-14 06:01:58 +00001123 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
1124 GlobalValue *GV = GSDN->getGlobal();
Chris Lattner059ca0f2006-06-16 21:01:35 +00001125 SDOperand GA = DAG.getTargetGlobalAddress(GV, PtrVT, GSDN->getOffset());
Evan Chengfcf5d4f2008-02-02 05:06:29 +00001126 // If it's a debug information descriptor, don't mess with it.
1127 if (DAG.isVerifiedDebugInfoDesc(Op))
1128 return GA;
Chris Lattner059ca0f2006-06-16 21:01:35 +00001129 SDOperand Zero = DAG.getConstant(0, PtrVT);
Chris Lattner1a635d62006-04-14 06:01:58 +00001130
1131 const TargetMachine &TM = DAG.getTarget();
1132
Chris Lattner059ca0f2006-06-16 21:01:35 +00001133 SDOperand Hi = DAG.getNode(PPCISD::Hi, PtrVT, GA, Zero);
1134 SDOperand Lo = DAG.getNode(PPCISD::Lo, PtrVT, GA, Zero);
1135
Chris Lattner1a635d62006-04-14 06:01:58 +00001136 // If this is a non-darwin platform, we don't support non-static relo models
1137 // yet.
1138 if (TM.getRelocationModel() == Reloc::Static ||
1139 !TM.getSubtarget<PPCSubtarget>().isDarwin()) {
1140 // Generate non-pic code that has direct accesses to globals.
1141 // The address of the global is just (hi(&g)+lo(&g)).
Chris Lattner059ca0f2006-06-16 21:01:35 +00001142 return DAG.getNode(ISD::ADD, PtrVT, Hi, Lo);
Chris Lattner1a635d62006-04-14 06:01:58 +00001143 }
1144
Chris Lattner35d86fe2006-07-26 21:12:04 +00001145 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattner1a635d62006-04-14 06:01:58 +00001146 // With PIC, the first instruction is actually "GR+hi(&G)".
Chris Lattner059ca0f2006-06-16 21:01:35 +00001147 Hi = DAG.getNode(ISD::ADD, PtrVT,
1148 DAG.getNode(PPCISD::GlobalBaseReg, PtrVT), Hi);
Chris Lattner1a635d62006-04-14 06:01:58 +00001149 }
1150
Chris Lattner059ca0f2006-06-16 21:01:35 +00001151 Lo = DAG.getNode(ISD::ADD, PtrVT, Hi, Lo);
Chris Lattner1a635d62006-04-14 06:01:58 +00001152
Chris Lattner57fc62c2006-12-11 23:22:45 +00001153 if (!TM.getSubtarget<PPCSubtarget>().hasLazyResolverStub(GV))
Chris Lattner1a635d62006-04-14 06:01:58 +00001154 return Lo;
1155
1156 // If the global is weak or external, we have to go through the lazy
1157 // resolution stub.
Evan Cheng466685d2006-10-09 20:57:25 +00001158 return DAG.getLoad(PtrVT, DAG.getEntryNode(), Lo, NULL, 0);
Chris Lattner1a635d62006-04-14 06:01:58 +00001159}
1160
Dale Johannesen5b3b6952008-03-04 23:17:14 +00001161SDOperand PPCTargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner1a635d62006-04-14 06:01:58 +00001162 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
1163
1164 // If we're comparing for equality to zero, expose the fact that this is
1165 // implented as a ctlz/srl pair on ppc, so that the dag combiner can
1166 // fold the new nodes.
1167 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1168 if (C->isNullValue() && CC == ISD::SETEQ) {
1169 MVT::ValueType VT = Op.getOperand(0).getValueType();
1170 SDOperand Zext = Op.getOperand(0);
1171 if (VT < MVT::i32) {
1172 VT = MVT::i32;
1173 Zext = DAG.getNode(ISD::ZERO_EXTEND, VT, Op.getOperand(0));
1174 }
1175 unsigned Log2b = Log2_32(MVT::getSizeInBits(VT));
1176 SDOperand Clz = DAG.getNode(ISD::CTLZ, VT, Zext);
1177 SDOperand Scc = DAG.getNode(ISD::SRL, VT, Clz,
1178 DAG.getConstant(Log2b, MVT::i32));
1179 return DAG.getNode(ISD::TRUNCATE, MVT::i32, Scc);
1180 }
1181 // Leave comparisons against 0 and -1 alone for now, since they're usually
1182 // optimized. FIXME: revisit this when we can custom lower all setcc
1183 // optimizations.
1184 if (C->isAllOnesValue() || C->isNullValue())
1185 return SDOperand();
1186 }
1187
1188 // If we have an integer seteq/setne, turn it into a compare against zero
Chris Lattnerac011bc2006-11-14 05:28:08 +00001189 // by xor'ing the rhs with the lhs, which is faster than setting a
1190 // condition register, reading it back out, and masking the correct bit. The
1191 // normal approach here uses sub to do this instead of xor. Using xor exposes
1192 // the result to other bit-twiddling opportunities.
Chris Lattner1a635d62006-04-14 06:01:58 +00001193 MVT::ValueType LHSVT = Op.getOperand(0).getValueType();
1194 if (MVT::isInteger(LHSVT) && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
1195 MVT::ValueType VT = Op.getValueType();
Chris Lattnerac011bc2006-11-14 05:28:08 +00001196 SDOperand Sub = DAG.getNode(ISD::XOR, LHSVT, Op.getOperand(0),
Chris Lattner1a635d62006-04-14 06:01:58 +00001197 Op.getOperand(1));
1198 return DAG.getSetCC(VT, Sub, DAG.getConstant(0, LHSVT), CC);
1199 }
1200 return SDOperand();
1201}
1202
Dale Johannesen5b3b6952008-03-04 23:17:14 +00001203SDOperand PPCTargetLowering::LowerVAARG(SDOperand Op, SelectionDAG &DAG,
Nicolas Geoffray01119992007-04-03 13:59:52 +00001204 int VarArgsFrameIndex,
1205 int VarArgsStackOffset,
1206 unsigned VarArgsNumGPR,
1207 unsigned VarArgsNumFPR,
1208 const PPCSubtarget &Subtarget) {
1209
1210 assert(0 && "VAARG in ELF32 ABI not implemented yet!");
Chris Lattnerd27c9912008-03-30 18:22:13 +00001211 return SDOperand(); // Not reached
Nicolas Geoffray01119992007-04-03 13:59:52 +00001212}
1213
Dale Johannesen5b3b6952008-03-04 23:17:14 +00001214SDOperand PPCTargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG,
Nicolas Geoffray01119992007-04-03 13:59:52 +00001215 int VarArgsFrameIndex,
1216 int VarArgsStackOffset,
1217 unsigned VarArgsNumGPR,
1218 unsigned VarArgsNumFPR,
1219 const PPCSubtarget &Subtarget) {
1220
1221 if (Subtarget.isMachoABI()) {
1222 // vastart just stores the address of the VarArgsFrameIndex slot into the
1223 // memory location argument.
1224 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1225 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001226 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
1227 return DAG.getStore(Op.getOperand(0), FR, Op.getOperand(1), SV, 0);
Nicolas Geoffray01119992007-04-03 13:59:52 +00001228 }
1229
1230 // For ELF 32 ABI we follow the layout of the va_list struct.
1231 // We suppose the given va_list is already allocated.
1232 //
1233 // typedef struct {
1234 // char gpr; /* index into the array of 8 GPRs
1235 // * stored in the register save area
1236 // * gpr=0 corresponds to r3,
1237 // * gpr=1 to r4, etc.
1238 // */
1239 // char fpr; /* index into the array of 8 FPRs
1240 // * stored in the register save area
1241 // * fpr=0 corresponds to f1,
1242 // * fpr=1 to f2, etc.
1243 // */
1244 // char *overflow_arg_area;
1245 // /* location on stack that holds
1246 // * the next overflow argument
1247 // */
1248 // char *reg_save_area;
1249 // /* where r3:r10 and f1:f8 (if saved)
1250 // * are stored
1251 // */
1252 // } va_list[1];
1253
1254
1255 SDOperand ArgGPR = DAG.getConstant(VarArgsNumGPR, MVT::i8);
1256 SDOperand ArgFPR = DAG.getConstant(VarArgsNumFPR, MVT::i8);
1257
1258
Chris Lattner0d72a202006-07-28 16:45:47 +00001259 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Nicolas Geoffray01119992007-04-03 13:59:52 +00001260
Dan Gohman69de1932008-02-06 22:27:42 +00001261 SDOperand StackOffsetFI = DAG.getFrameIndex(VarArgsStackOffset, PtrVT);
Chris Lattner0d72a202006-07-28 16:45:47 +00001262 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
Nicolas Geoffray01119992007-04-03 13:59:52 +00001263
Dan Gohman69de1932008-02-06 22:27:42 +00001264 uint64_t FrameOffset = MVT::getSizeInBits(PtrVT)/8;
1265 SDOperand ConstFrameOffset = DAG.getConstant(FrameOffset, PtrVT);
1266
1267 uint64_t StackOffset = MVT::getSizeInBits(PtrVT)/8 - 1;
1268 SDOperand ConstStackOffset = DAG.getConstant(StackOffset, PtrVT);
1269
1270 uint64_t FPROffset = 1;
1271 SDOperand ConstFPROffset = DAG.getConstant(FPROffset, PtrVT);
Nicolas Geoffray01119992007-04-03 13:59:52 +00001272
Dan Gohman69de1932008-02-06 22:27:42 +00001273 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Nicolas Geoffray01119992007-04-03 13:59:52 +00001274
1275 // Store first byte : number of int regs
1276 SDOperand firstStore = DAG.getStore(Op.getOperand(0), ArgGPR,
Dan Gohman69de1932008-02-06 22:27:42 +00001277 Op.getOperand(1), SV, 0);
1278 uint64_t nextOffset = FPROffset;
Nicolas Geoffray01119992007-04-03 13:59:52 +00001279 SDOperand nextPtr = DAG.getNode(ISD::ADD, PtrVT, Op.getOperand(1),
1280 ConstFPROffset);
1281
1282 // Store second byte : number of float regs
Dan Gohman69de1932008-02-06 22:27:42 +00001283 SDOperand secondStore =
1284 DAG.getStore(firstStore, ArgFPR, nextPtr, SV, nextOffset);
1285 nextOffset += StackOffset;
Nicolas Geoffray01119992007-04-03 13:59:52 +00001286 nextPtr = DAG.getNode(ISD::ADD, PtrVT, nextPtr, ConstStackOffset);
1287
1288 // Store second word : arguments given on stack
Dan Gohman69de1932008-02-06 22:27:42 +00001289 SDOperand thirdStore =
1290 DAG.getStore(secondStore, StackOffsetFI, nextPtr, SV, nextOffset);
1291 nextOffset += FrameOffset;
Nicolas Geoffray01119992007-04-03 13:59:52 +00001292 nextPtr = DAG.getNode(ISD::ADD, PtrVT, nextPtr, ConstFrameOffset);
1293
1294 // Store third word : arguments given in registers
Dan Gohman69de1932008-02-06 22:27:42 +00001295 return DAG.getStore(thirdStore, FR, nextPtr, SV, nextOffset);
Nicolas Geoffray01119992007-04-03 13:59:52 +00001296
Chris Lattner1a635d62006-04-14 06:01:58 +00001297}
1298
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00001299#include "PPCGenCallingConv.inc"
1300
Chris Lattner9f0bc652007-02-25 05:34:32 +00001301/// GetFPR - Get the set of FP registers that should be allocated for arguments,
1302/// depending on which subtarget is selected.
1303static const unsigned *GetFPR(const PPCSubtarget &Subtarget) {
1304 if (Subtarget.isMachoABI()) {
1305 static const unsigned FPR[] = {
1306 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1307 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1308 };
1309 return FPR;
1310 }
1311
1312
1313 static const unsigned FPR[] = {
1314 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
Nicolas Geoffrayef3c0302007-04-03 10:27:07 +00001315 PPC::F8
Chris Lattner9f0bc652007-02-25 05:34:32 +00001316 };
1317 return FPR;
1318}
1319
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00001320SDOperand
1321PPCTargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op,
1322 SelectionDAG &DAG,
1323 int &VarArgsFrameIndex,
1324 int &VarArgsStackOffset,
1325 unsigned &VarArgsNumGPR,
1326 unsigned &VarArgsNumFPR,
1327 const PPCSubtarget &Subtarget) {
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001328 // TODO: add description of PPC stack frame format, or at least some docs.
1329 //
1330 MachineFunction &MF = DAG.getMachineFunction();
1331 MachineFrameInfo *MFI = MF.getFrameInfo();
Chris Lattner84bc5422007-12-31 04:13:23 +00001332 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Chris Lattner79e490a2006-08-11 17:18:05 +00001333 SmallVector<SDOperand, 8> ArgValues;
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001334 SDOperand Root = Op.getOperand(0);
Dale Johannesen75092de2008-03-12 00:22:17 +00001335 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001336
Jim Laskey2f616bf2006-11-16 22:43:37 +00001337 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1338 bool isPPC64 = PtrVT == MVT::i64;
Chris Lattner9f0bc652007-02-25 05:34:32 +00001339 bool isMachoABI = Subtarget.isMachoABI();
Nicolas Geoffrayec58d9f2007-04-03 12:35:28 +00001340 bool isELF32_ABI = Subtarget.isELF32_ABI();
Jim Laskeye9bd7b22006-11-28 14:53:52 +00001341 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Jim Laskey2f616bf2006-11-16 22:43:37 +00001342
Chris Lattner9f0bc652007-02-25 05:34:32 +00001343 unsigned ArgOffset = PPCFrameInfo::getLinkageSize(isPPC64, isMachoABI);
Chris Lattnerc91a4752006-06-26 22:48:35 +00001344
1345 static const unsigned GPR_32[] = { // 32-bit registers.
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001346 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1347 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1348 };
Chris Lattnerc91a4752006-06-26 22:48:35 +00001349 static const unsigned GPR_64[] = { // 64-bit registers.
1350 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
1351 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
1352 };
Chris Lattner9f0bc652007-02-25 05:34:32 +00001353
1354 static const unsigned *FPR = GetFPR(Subtarget);
1355
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001356 static const unsigned VR[] = {
1357 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
1358 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
1359 };
Chris Lattnerc91a4752006-06-26 22:48:35 +00001360
Owen Anderson718cb662007-09-07 04:06:50 +00001361 const unsigned Num_GPR_Regs = array_lengthof(GPR_32);
Nicolas Geoffrayef3c0302007-04-03 10:27:07 +00001362 const unsigned Num_FPR_Regs = isMachoABI ? 13 : 8;
Owen Anderson718cb662007-09-07 04:06:50 +00001363 const unsigned Num_VR_Regs = array_lengthof( VR);
Jim Laskey2f616bf2006-11-16 22:43:37 +00001364
1365 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
1366
Chris Lattnerc91a4752006-06-26 22:48:35 +00001367 const unsigned *GPR = isPPC64 ? GPR_64 : GPR_32;
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001368
Dale Johannesen8f5422c2008-03-14 17:41:26 +00001369 // In 32-bit non-varargs functions, the stack space for vectors is after the
1370 // stack space for non-vectors. We do not use this space unless we have
1371 // too many vectors to fit in registers, something that only occurs in
1372 // constructed examples:), but we have to walk the arglist to figure
1373 // that out...for the pathological case, compute VecArgOffset as the
1374 // start of the vector parameter area. Computing VecArgOffset is the
1375 // entire point of the following loop.
1376 // Altivec is not mentioned in the ppc32 Elf Supplement, so I'm not trying
1377 // to handle Elf here.
1378 unsigned VecArgOffset = ArgOffset;
1379 if (!isVarArg && !isPPC64) {
1380 for (unsigned ArgNo = 0, e = Op.Val->getNumValues()-1; ArgNo != e;
1381 ++ArgNo) {
1382 MVT::ValueType ObjectVT = Op.getValue(ArgNo).getValueType();
1383 unsigned ObjSize = MVT::getSizeInBits(ObjectVT)/8;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001384 ISD::ArgFlagsTy Flags =
1385 cast<ARG_FLAGSSDNode>(Op.getOperand(ArgNo+3))->getArgFlags();
Dale Johannesen8f5422c2008-03-14 17:41:26 +00001386
Duncan Sands276dcbd2008-03-21 09:14:45 +00001387 if (Flags.isByVal()) {
Dale Johannesen8f5422c2008-03-14 17:41:26 +00001388 // ObjSize is the true size, ArgSize rounded up to multiple of regs.
Duncan Sands276dcbd2008-03-21 09:14:45 +00001389 ObjSize = Flags.getByValSize();
Dale Johannesen8f5422c2008-03-14 17:41:26 +00001390 unsigned ArgSize =
1391 ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
1392 VecArgOffset += ArgSize;
1393 continue;
1394 }
1395
1396 switch(ObjectVT) {
1397 default: assert(0 && "Unhandled argument type!");
1398 case MVT::i32:
1399 case MVT::f32:
1400 VecArgOffset += isPPC64 ? 8 : 4;
1401 break;
1402 case MVT::i64: // PPC64
1403 case MVT::f64:
1404 VecArgOffset += 8;
1405 break;
1406 case MVT::v4f32:
1407 case MVT::v4i32:
1408 case MVT::v8i16:
1409 case MVT::v16i8:
1410 // Nothing to do, we're only looking at Nonvector args here.
1411 break;
1412 }
1413 }
1414 }
1415 // We've found where the vector parameter area in memory is. Skip the
1416 // first 12 parameters; these don't use that memory.
1417 VecArgOffset = ((VecArgOffset+15)/16)*16;
1418 VecArgOffset += 12*16;
1419
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001420 // Add DAG nodes to load the arguments or copy them out of registers. On
Jim Laskey2f616bf2006-11-16 22:43:37 +00001421 // entry to a function on PPC, the arguments start after the linkage area,
1422 // although the first ones are often in registers.
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00001423 //
Nicolas Geoffrayec58d9f2007-04-03 12:35:28 +00001424 // In the ELF 32 ABI, GPRs and stack are double word align: an argument
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00001425 // represented with two words (long long or double) must be copied to an
Nicolas Geoffrayc0cb28f2008-04-13 13:40:22 +00001426 // even GPR_idx value or to an even ArgOffset value.
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00001427
Dale Johannesen8419dd62008-03-07 20:27:40 +00001428 SmallVector<SDOperand, 8> MemOps;
1429
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001430 for (unsigned ArgNo = 0, e = Op.Val->getNumValues()-1; ArgNo != e; ++ArgNo) {
1431 SDOperand ArgVal;
1432 bool needsLoad = false;
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001433 MVT::ValueType ObjectVT = Op.getValue(ArgNo).getValueType();
1434 unsigned ObjSize = MVT::getSizeInBits(ObjectVT)/8;
Jim Laskey619965d2006-11-29 13:37:09 +00001435 unsigned ArgSize = ObjSize;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001436 ISD::ArgFlagsTy Flags =
1437 cast<ARG_FLAGSSDNode>(Op.getOperand(ArgNo+3))->getArgFlags();
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00001438 // See if next argument requires stack alignment in ELF
Nicolas Geoffray6ccbbd82008-04-15 08:08:50 +00001439 bool Align = Flags.isSplit();
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001440
Chris Lattnerbe4849a2006-05-16 18:51:52 +00001441 unsigned CurArgOffset = ArgOffset;
Dale Johannesen8419dd62008-03-07 20:27:40 +00001442
1443 // FIXME alignment for ELF may not be right
1444 // FIXME the codegen can be much improved in some cases.
1445 // We do not have to keep everything in memory.
Duncan Sands276dcbd2008-03-21 09:14:45 +00001446 if (Flags.isByVal()) {
Dale Johannesen8419dd62008-03-07 20:27:40 +00001447 // ObjSize is the true size, ArgSize rounded up to multiple of registers.
Duncan Sands276dcbd2008-03-21 09:14:45 +00001448 ObjSize = Flags.getByValSize();
Dale Johannesen8419dd62008-03-07 20:27:40 +00001449 ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Dale Johannesen7f96f392008-03-08 01:41:42 +00001450 // Double word align in ELF
Nicolas Geoffrayc0cb28f2008-04-13 13:40:22 +00001451 if (Align && isELF32_ABI) GPR_idx += (GPR_idx % 2);
Dale Johannesen7f96f392008-03-08 01:41:42 +00001452 // Objects of size 1 and 2 are right justified, everything else is
1453 // left justified. This means the memory address is adjusted forwards.
1454 if (ObjSize==1 || ObjSize==2) {
1455 CurArgOffset = CurArgOffset + (4 - ObjSize);
1456 }
Dale Johannesen8419dd62008-03-07 20:27:40 +00001457 // The value of the object is its address.
1458 int FI = MFI->CreateFixedObject(ObjSize, CurArgOffset);
1459 SDOperand FIN = DAG.getFrameIndex(FI, PtrVT);
1460 ArgValues.push_back(FIN);
Dale Johannesen7f96f392008-03-08 01:41:42 +00001461 if (ObjSize==1 || ObjSize==2) {
1462 if (GPR_idx != Num_GPR_Regs) {
1463 unsigned VReg = RegInfo.createVirtualRegister(&PPC::GPRCRegClass);
1464 RegInfo.addLiveIn(GPR[GPR_idx], VReg);
1465 SDOperand Val = DAG.getCopyFromReg(Root, VReg, PtrVT);
1466 SDOperand Store = DAG.getTruncStore(Val.getValue(1), Val, FIN,
1467 NULL, 0, ObjSize==1 ? MVT::i8 : MVT::i16 );
1468 MemOps.push_back(Store);
1469 ++GPR_idx;
1470 if (isMachoABI) ArgOffset += PtrByteSize;
1471 } else {
1472 ArgOffset += PtrByteSize;
1473 }
1474 continue;
1475 }
Dale Johannesen8419dd62008-03-07 20:27:40 +00001476 for (unsigned j = 0; j < ArgSize; j += PtrByteSize) {
1477 // Store whatever pieces of the object are in registers
1478 // to memory. ArgVal will be address of the beginning of
1479 // the object.
1480 if (GPR_idx != Num_GPR_Regs) {
1481 unsigned VReg = RegInfo.createVirtualRegister(&PPC::GPRCRegClass);
1482 RegInfo.addLiveIn(GPR[GPR_idx], VReg);
1483 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset);
1484 SDOperand FIN = DAG.getFrameIndex(FI, PtrVT);
1485 SDOperand Val = DAG.getCopyFromReg(Root, VReg, PtrVT);
1486 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1487 MemOps.push_back(Store);
1488 ++GPR_idx;
1489 if (isMachoABI) ArgOffset += PtrByteSize;
1490 } else {
1491 ArgOffset += ArgSize - (ArgOffset-CurArgOffset);
1492 break;
1493 }
1494 }
1495 continue;
1496 }
1497
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001498 switch (ObjectVT) {
1499 default: assert(0 && "Unhandled argument type!");
1500 case MVT::i32:
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00001501 if (!isPPC64) {
1502 // Double word align in ELF
Nicolas Geoffrayc0cb28f2008-04-13 13:40:22 +00001503 if (Align && isELF32_ABI) GPR_idx += (GPR_idx % 2);
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00001504
1505 if (GPR_idx != Num_GPR_Regs) {
1506 unsigned VReg = RegInfo.createVirtualRegister(&PPC::GPRCRegClass);
1507 RegInfo.addLiveIn(GPR[GPR_idx], VReg);
1508 ArgVal = DAG.getCopyFromReg(Root, VReg, MVT::i32);
1509 ++GPR_idx;
1510 } else {
1511 needsLoad = true;
1512 ArgSize = PtrByteSize;
1513 }
1514 // Stack align in ELF
Nicolas Geoffrayc0cb28f2008-04-13 13:40:22 +00001515 if (needsLoad && Align && isELF32_ABI)
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00001516 ArgOffset += ((ArgOffset/4) % 2) * PtrByteSize;
1517 // All int arguments reserve stack space in Macho ABI.
1518 if (isMachoABI || needsLoad) ArgOffset += PtrByteSize;
1519 break;
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001520 }
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00001521 // FALLTHROUGH
Chris Lattner9f0bc652007-02-25 05:34:32 +00001522 case MVT::i64: // PPC64
Chris Lattnerc91a4752006-06-26 22:48:35 +00001523 if (GPR_idx != Num_GPR_Regs) {
Chris Lattner84bc5422007-12-31 04:13:23 +00001524 unsigned VReg = RegInfo.createVirtualRegister(&PPC::G8RCRegClass);
1525 RegInfo.addLiveIn(GPR[GPR_idx], VReg);
Chris Lattnerc91a4752006-06-26 22:48:35 +00001526 ArgVal = DAG.getCopyFromReg(Root, VReg, MVT::i64);
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00001527
1528 if (ObjectVT == MVT::i32) {
1529 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote
1530 // value to MVT::i64 and then truncate to the correct register size.
Duncan Sands276dcbd2008-03-21 09:14:45 +00001531 if (Flags.isSExt())
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00001532 ArgVal = DAG.getNode(ISD::AssertSext, MVT::i64, ArgVal,
1533 DAG.getValueType(ObjectVT));
Duncan Sands276dcbd2008-03-21 09:14:45 +00001534 else if (Flags.isZExt())
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00001535 ArgVal = DAG.getNode(ISD::AssertZext, MVT::i64, ArgVal,
1536 DAG.getValueType(ObjectVT));
1537
1538 ArgVal = DAG.getNode(ISD::TRUNCATE, MVT::i32, ArgVal);
1539 }
1540
Chris Lattnerc91a4752006-06-26 22:48:35 +00001541 ++GPR_idx;
1542 } else {
1543 needsLoad = true;
1544 }
Chris Lattner9f0bc652007-02-25 05:34:32 +00001545 // All int arguments reserve stack space in Macho ABI.
1546 if (isMachoABI || needsLoad) ArgOffset += 8;
Chris Lattnerc91a4752006-06-26 22:48:35 +00001547 break;
Chris Lattner9f0bc652007-02-25 05:34:32 +00001548
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001549 case MVT::f32:
1550 case MVT::f64:
Chris Lattnerbe4849a2006-05-16 18:51:52 +00001551 // Every 4 bytes of argument space consumes one of the GPRs available for
1552 // argument passing.
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00001553 if (GPR_idx != Num_GPR_Regs && isMachoABI) {
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00001554 ++GPR_idx;
Chris Lattnerb1eb9872006-11-18 01:57:19 +00001555 if (ObjSize == 8 && GPR_idx != Num_GPR_Regs && !isPPC64)
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00001556 ++GPR_idx;
Chris Lattnerbe4849a2006-05-16 18:51:52 +00001557 }
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00001558 if (FPR_idx != Num_FPR_Regs) {
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001559 unsigned VReg;
1560 if (ObjectVT == MVT::f32)
Chris Lattner84bc5422007-12-31 04:13:23 +00001561 VReg = RegInfo.createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001562 else
Chris Lattner84bc5422007-12-31 04:13:23 +00001563 VReg = RegInfo.createVirtualRegister(&PPC::F8RCRegClass);
1564 RegInfo.addLiveIn(FPR[FPR_idx], VReg);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001565 ArgVal = DAG.getCopyFromReg(Root, VReg, ObjectVT);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001566 ++FPR_idx;
1567 } else {
1568 needsLoad = true;
1569 }
Chris Lattner9f0bc652007-02-25 05:34:32 +00001570
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00001571 // Stack align in ELF
Nicolas Geoffrayc0cb28f2008-04-13 13:40:22 +00001572 if (needsLoad && Align && isELF32_ABI)
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00001573 ArgOffset += ((ArgOffset/4) % 2) * PtrByteSize;
Chris Lattner9f0bc652007-02-25 05:34:32 +00001574 // All FP arguments reserve stack space in Macho ABI.
1575 if (isMachoABI || needsLoad) ArgOffset += isPPC64 ? 8 : ObjSize;
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001576 break;
1577 case MVT::v4f32:
1578 case MVT::v4i32:
1579 case MVT::v8i16:
1580 case MVT::v16i8:
Dale Johannesen75092de2008-03-12 00:22:17 +00001581 // Note that vector arguments in registers don't reserve stack space,
1582 // except in varargs functions.
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00001583 if (VR_idx != Num_VR_Regs) {
Chris Lattner84bc5422007-12-31 04:13:23 +00001584 unsigned VReg = RegInfo.createVirtualRegister(&PPC::VRRCRegClass);
1585 RegInfo.addLiveIn(VR[VR_idx], VReg);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001586 ArgVal = DAG.getCopyFromReg(Root, VReg, ObjectVT);
Dale Johannesen75092de2008-03-12 00:22:17 +00001587 if (isVarArg) {
1588 while ((ArgOffset % 16) != 0) {
1589 ArgOffset += PtrByteSize;
1590 if (GPR_idx != Num_GPR_Regs)
1591 GPR_idx++;
1592 }
1593 ArgOffset += 16;
1594 GPR_idx = std::min(GPR_idx+4, Num_GPR_Regs);
1595 }
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001596 ++VR_idx;
1597 } else {
Dale Johannesen8f5422c2008-03-14 17:41:26 +00001598 if (!isVarArg && !isPPC64) {
1599 // Vectors go after all the nonvectors.
1600 CurArgOffset = VecArgOffset;
1601 VecArgOffset += 16;
1602 } else {
1603 // Vectors are aligned.
1604 ArgOffset = ((ArgOffset+15)/16)*16;
1605 CurArgOffset = ArgOffset;
1606 ArgOffset += 16;
Dale Johannesen404d9902008-03-12 00:49:20 +00001607 }
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001608 needsLoad = true;
1609 }
1610 break;
1611 }
1612
1613 // We need to load the argument to a virtual register if we determined above
Chris Lattner9f72d1a2008-02-13 07:35:30 +00001614 // that we ran out of physical registers of the appropriate type.
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001615 if (needsLoad) {
Chris Lattner9f72d1a2008-02-13 07:35:30 +00001616 int FI = MFI->CreateFixedObject(ObjSize,
1617 CurArgOffset + (ArgSize - ObjSize));
1618 SDOperand FIN = DAG.getFrameIndex(FI, PtrVT);
1619 ArgVal = DAG.getLoad(ObjectVT, Root, FIN, NULL, 0);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001620 }
1621
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001622 ArgValues.push_back(ArgVal);
1623 }
Dale Johannesen8419dd62008-03-07 20:27:40 +00001624
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001625 // If the function takes variable number of arguments, make a frame index for
1626 // the start of the first vararg value... for expansion of llvm.va_start.
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001627 if (isVarArg) {
Nicolas Geoffray01119992007-04-03 13:59:52 +00001628
1629 int depth;
1630 if (isELF32_ABI) {
1631 VarArgsNumGPR = GPR_idx;
1632 VarArgsNumFPR = FPR_idx;
1633
1634 // Make room for Num_GPR_Regs, Num_FPR_Regs and for a possible frame
1635 // pointer.
1636 depth = -(Num_GPR_Regs * MVT::getSizeInBits(PtrVT)/8 +
1637 Num_FPR_Regs * MVT::getSizeInBits(MVT::f64)/8 +
1638 MVT::getSizeInBits(PtrVT)/8);
1639
1640 VarArgsStackOffset = MFI->CreateFixedObject(MVT::getSizeInBits(PtrVT)/8,
1641 ArgOffset);
1642
1643 }
1644 else
1645 depth = ArgOffset;
1646
Chris Lattnerc91a4752006-06-26 22:48:35 +00001647 VarArgsFrameIndex = MFI->CreateFixedObject(MVT::getSizeInBits(PtrVT)/8,
Nicolas Geoffray01119992007-04-03 13:59:52 +00001648 depth);
Chris Lattnerc91a4752006-06-26 22:48:35 +00001649 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
Nicolas Geoffray01119992007-04-03 13:59:52 +00001650
Nicolas Geoffray01119992007-04-03 13:59:52 +00001651 // In ELF 32 ABI, the fixed integer arguments of a variadic function are
1652 // stored to the VarArgsFrameIndex on the stack.
1653 if (isELF32_ABI) {
1654 for (GPR_idx = 0; GPR_idx != VarArgsNumGPR; ++GPR_idx) {
1655 SDOperand Val = DAG.getRegister(GPR[GPR_idx], PtrVT);
1656 SDOperand Store = DAG.getStore(Root, Val, FIN, NULL, 0);
1657 MemOps.push_back(Store);
1658 // Increment the address by four for the next argument to store
1659 SDOperand PtrOff = DAG.getConstant(MVT::getSizeInBits(PtrVT)/8, PtrVT);
1660 FIN = DAG.getNode(ISD::ADD, PtrOff.getValueType(), FIN, PtrOff);
1661 }
1662 }
1663
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001664 // If this function is vararg, store any remaining integer argument regs
1665 // to their spots on the stack so that they may be loaded by deferencing the
1666 // result of va_next.
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00001667 for (; GPR_idx != Num_GPR_Regs; ++GPR_idx) {
Chris Lattnerb1eb9872006-11-18 01:57:19 +00001668 unsigned VReg;
1669 if (isPPC64)
Chris Lattner84bc5422007-12-31 04:13:23 +00001670 VReg = RegInfo.createVirtualRegister(&PPC::G8RCRegClass);
Chris Lattnerb1eb9872006-11-18 01:57:19 +00001671 else
Chris Lattner84bc5422007-12-31 04:13:23 +00001672 VReg = RegInfo.createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattnerb1eb9872006-11-18 01:57:19 +00001673
Chris Lattner84bc5422007-12-31 04:13:23 +00001674 RegInfo.addLiveIn(GPR[GPR_idx], VReg);
Chris Lattnerc91a4752006-06-26 22:48:35 +00001675 SDOperand Val = DAG.getCopyFromReg(Root, VReg, PtrVT);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001676 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001677 MemOps.push_back(Store);
1678 // Increment the address by four for the next argument to store
Chris Lattnerc91a4752006-06-26 22:48:35 +00001679 SDOperand PtrOff = DAG.getConstant(MVT::getSizeInBits(PtrVT)/8, PtrVT);
1680 FIN = DAG.getNode(ISD::ADD, PtrOff.getValueType(), FIN, PtrOff);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001681 }
Nicolas Geoffray01119992007-04-03 13:59:52 +00001682
1683 // In ELF 32 ABI, the double arguments are stored to the VarArgsFrameIndex
1684 // on the stack.
1685 if (isELF32_ABI) {
1686 for (FPR_idx = 0; FPR_idx != VarArgsNumFPR; ++FPR_idx) {
1687 SDOperand Val = DAG.getRegister(FPR[FPR_idx], MVT::f64);
1688 SDOperand Store = DAG.getStore(Root, Val, FIN, NULL, 0);
1689 MemOps.push_back(Store);
1690 // Increment the address by eight for the next argument to store
1691 SDOperand PtrOff = DAG.getConstant(MVT::getSizeInBits(MVT::f64)/8,
1692 PtrVT);
1693 FIN = DAG.getNode(ISD::ADD, PtrOff.getValueType(), FIN, PtrOff);
1694 }
1695
1696 for (; FPR_idx != Num_FPR_Regs; ++FPR_idx) {
1697 unsigned VReg;
Chris Lattner84bc5422007-12-31 04:13:23 +00001698 VReg = RegInfo.createVirtualRegister(&PPC::F8RCRegClass);
Nicolas Geoffray01119992007-04-03 13:59:52 +00001699
Chris Lattner84bc5422007-12-31 04:13:23 +00001700 RegInfo.addLiveIn(FPR[FPR_idx], VReg);
Nicolas Geoffray01119992007-04-03 13:59:52 +00001701 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::f64);
1702 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1703 MemOps.push_back(Store);
1704 // Increment the address by eight for the next argument to store
1705 SDOperand PtrOff = DAG.getConstant(MVT::getSizeInBits(MVT::f64)/8,
1706 PtrVT);
1707 FIN = DAG.getNode(ISD::ADD, PtrOff.getValueType(), FIN, PtrOff);
1708 }
1709 }
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001710 }
1711
Dale Johannesen8419dd62008-03-07 20:27:40 +00001712 if (!MemOps.empty())
1713 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,&MemOps[0],MemOps.size());
1714
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001715 ArgValues.push_back(Root);
1716
1717 // Return the new list of results.
1718 std::vector<MVT::ValueType> RetVT(Op.Val->value_begin(),
1719 Op.Val->value_end());
Chris Lattner79e490a2006-08-11 17:18:05 +00001720 return DAG.getNode(ISD::MERGE_VALUES, RetVT, &ArgValues[0], ArgValues.size());
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001721}
1722
Chris Lattnerc703a8f2006-05-17 19:00:46 +00001723/// isCallCompatibleAddress - Return the immediate to use if the specified
1724/// 32-bit value is representable in the immediate field of a BxA instruction.
1725static SDNode *isBLACompatibleAddress(SDOperand Op, SelectionDAG &DAG) {
1726 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
1727 if (!C) return 0;
1728
1729 int Addr = C->getValue();
1730 if ((Addr & 3) != 0 || // Low 2 bits are implicitly zero.
1731 (Addr << 6 >> 6) != Addr)
1732 return 0; // Top 6 bits have to be sext of immediate.
1733
Evan Cheng33118762007-10-22 19:46:19 +00001734 return DAG.getConstant((int)C->getValue() >> 2,
1735 DAG.getTargetLoweringInfo().getPointerTy()).Val;
Chris Lattnerc703a8f2006-05-17 19:00:46 +00001736}
1737
Dale Johannesen5b3b6952008-03-04 23:17:14 +00001738/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1739/// by "Src" to address "Dst" of size "Size". Alignment information is
1740/// specified by the specific parameter attribute. The copy will be passed as
1741/// a byval function parameter.
1742/// Sometimes what we are copying is the end of a larger object, the part that
1743/// does not fit in registers.
1744static SDOperand
1745CreateCopyOfByValArgument(SDOperand Src, SDOperand Dst, SDOperand Chain,
Duncan Sands276dcbd2008-03-21 09:14:45 +00001746 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1747 unsigned Size) {
Dan Gohman707e0182008-04-12 04:36:06 +00001748 SDOperand SizeNode = DAG.getConstant(Size, MVT::i32);
1749 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, Flags.getByValAlign(), false,
1750 NULL, 0, NULL, 0);
Dale Johannesen5b3b6952008-03-04 23:17:14 +00001751}
Chris Lattner9f0bc652007-02-25 05:34:32 +00001752
Dale Johannesen5b3b6952008-03-04 23:17:14 +00001753SDOperand PPCTargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG,
Dan Gohman7925ed02008-03-19 21:39:28 +00001754 const PPCSubtarget &Subtarget,
1755 TargetMachine &TM) {
Chris Lattner9f0bc652007-02-25 05:34:32 +00001756 SDOperand Chain = Op.getOperand(0);
1757 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1758 SDOperand Callee = Op.getOperand(4);
1759 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
1760
1761 bool isMachoABI = Subtarget.isMachoABI();
Nicolas Geoffrayec58d9f2007-04-03 12:35:28 +00001762 bool isELF32_ABI = Subtarget.isELF32_ABI();
Evan Cheng4360bdc2006-05-25 00:57:32 +00001763
Chris Lattnerc91a4752006-06-26 22:48:35 +00001764 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1765 bool isPPC64 = PtrVT == MVT::i64;
1766 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Chris Lattnerc91a4752006-06-26 22:48:35 +00001767
Chris Lattnerabde4602006-05-16 22:56:08 +00001768 // args_to_use will accumulate outgoing args for the PPCISD::CALL case in
1769 // SelectExpr to use to put the arguments in the appropriate registers.
1770 std::vector<SDOperand> args_to_use;
1771
1772 // Count how many bytes are to be pushed on the stack, including the linkage
Chris Lattnerc91a4752006-06-26 22:48:35 +00001773 // area, and parameter passing area. We start with 24/48 bytes, which is
Chris Lattnerc8b682c2006-05-17 00:15:40 +00001774 // prereserved space for [SP][CR][LR][3 x unused].
Chris Lattner9f0bc652007-02-25 05:34:32 +00001775 unsigned NumBytes = PPCFrameInfo::getLinkageSize(isPPC64, isMachoABI);
Dale Johannesen75092de2008-03-12 00:22:17 +00001776
Chris Lattnerc8b682c2006-05-17 00:15:40 +00001777 // Add up all the space actually used.
Dale Johannesen8f5422c2008-03-14 17:41:26 +00001778 // In 32-bit non-varargs calls, Altivec parameters all go at the end; usually
1779 // they all go in registers, but we must reserve stack space for them for
1780 // possible use by the caller. In varargs or 64-bit calls, parameters are
1781 // assigned stack space in order, with padding so Altivec parameters are
1782 // 16-byte aligned.
1783 unsigned nAltivecParamsAtEnd = 0;
Jim Laskeye9bd7b22006-11-28 14:53:52 +00001784 for (unsigned i = 0; i != NumOps; ++i) {
Dale Johannesen75092de2008-03-12 00:22:17 +00001785 SDOperand Arg = Op.getOperand(5+2*i);
1786 MVT::ValueType ArgVT = Arg.getValueType();
Dale Johannesen8f5422c2008-03-14 17:41:26 +00001787 if (ArgVT==MVT::v4f32 || ArgVT==MVT::v4i32 ||
1788 ArgVT==MVT::v8i16 || ArgVT==MVT::v16i8) {
1789 if (!isVarArg && !isPPC64) {
1790 // Non-varargs Altivec parameters go after all the non-Altivec parameters;
1791 // do those last so we know how much padding we need.
1792 nAltivecParamsAtEnd++;
1793 continue;
1794 } else {
1795 // Varargs and 64-bit Altivec parameters are padded to 16 byte boundary.
1796 NumBytes = ((NumBytes+15)/16)*16;
1797 }
1798 }
Duncan Sands276dcbd2008-03-21 09:14:45 +00001799 ISD::ArgFlagsTy Flags =
1800 cast<ARG_FLAGSSDNode>(Op.getOperand(5+2*i+1))->getArgFlags();
Jim Laskeye9bd7b22006-11-28 14:53:52 +00001801 unsigned ArgSize =MVT::getSizeInBits(Op.getOperand(5+2*i).getValueType())/8;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001802 if (Flags.isByVal())
1803 ArgSize = Flags.getByValSize();
Dale Johannesen7f96f392008-03-08 01:41:42 +00001804 ArgSize = ((ArgSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Jim Laskeye9bd7b22006-11-28 14:53:52 +00001805 NumBytes += ArgSize;
1806 }
Dale Johannesen8f5422c2008-03-14 17:41:26 +00001807 // Allow for Altivec parameters at the end, if needed.
1808 if (nAltivecParamsAtEnd) {
1809 NumBytes = ((NumBytes+15)/16)*16;
1810 NumBytes += 16*nAltivecParamsAtEnd;
1811 }
Chris Lattnerc04ba7a2006-05-16 23:54:25 +00001812
Chris Lattner7b053502006-05-30 21:21:04 +00001813 // The prolog code of the callee may store up to 8 GPR argument registers to
1814 // the stack, allowing va_start to index over them in memory if its varargs.
1815 // Because we cannot tell if this is needed on the caller side, we have to
1816 // conservatively assume that it is needed. As such, make sure we have at
1817 // least enough stack space for the caller to store the 8 GPRs.
Chris Lattner9f0bc652007-02-25 05:34:32 +00001818 NumBytes = std::max(NumBytes,
1819 PPCFrameInfo::getMinCallFrameSize(isPPC64, isMachoABI));
Chris Lattnerc8b682c2006-05-17 00:15:40 +00001820
1821 // Adjust the stack pointer for the new arguments...
1822 // These operations are automatically eliminated by the prolog/epilog pass
1823 Chain = DAG.getCALLSEQ_START(Chain,
Chris Lattnerc91a4752006-06-26 22:48:35 +00001824 DAG.getConstant(NumBytes, PtrVT));
Dale Johannesen1f797a32008-03-05 23:31:27 +00001825 SDOperand CallSeqStart = Chain;
Chris Lattnerc8b682c2006-05-17 00:15:40 +00001826
1827 // Set up a copy of the stack pointer for use loading and storing any
1828 // arguments that may not fit in the registers available for argument
1829 // passing.
Chris Lattnerc91a4752006-06-26 22:48:35 +00001830 SDOperand StackPtr;
1831 if (isPPC64)
1832 StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
1833 else
1834 StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Chris Lattnerc8b682c2006-05-17 00:15:40 +00001835
1836 // Figure out which arguments are going to go in registers, and which in
1837 // memory. Also, if this is a vararg function, floating point operations
1838 // must be stored to our stack, and loaded into integer regs as well, if
1839 // any integer regs are available for argument passing.
Chris Lattner9f0bc652007-02-25 05:34:32 +00001840 unsigned ArgOffset = PPCFrameInfo::getLinkageSize(isPPC64, isMachoABI);
Chris Lattner9a2a4972006-05-17 06:01:33 +00001841 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Jim Laskey2f616bf2006-11-16 22:43:37 +00001842
Chris Lattnerc91a4752006-06-26 22:48:35 +00001843 static const unsigned GPR_32[] = { // 32-bit registers.
Chris Lattner9a2a4972006-05-17 06:01:33 +00001844 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1845 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1846 };
Chris Lattnerc91a4752006-06-26 22:48:35 +00001847 static const unsigned GPR_64[] = { // 64-bit registers.
1848 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
1849 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
1850 };
Chris Lattner9f0bc652007-02-25 05:34:32 +00001851 static const unsigned *FPR = GetFPR(Subtarget);
1852
Chris Lattner9a2a4972006-05-17 06:01:33 +00001853 static const unsigned VR[] = {
1854 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
1855 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
1856 };
Owen Anderson718cb662007-09-07 04:06:50 +00001857 const unsigned NumGPRs = array_lengthof(GPR_32);
Nicolas Geoffrayef3c0302007-04-03 10:27:07 +00001858 const unsigned NumFPRs = isMachoABI ? 13 : 8;
Owen Anderson718cb662007-09-07 04:06:50 +00001859 const unsigned NumVRs = array_lengthof( VR);
Chris Lattner9a2a4972006-05-17 06:01:33 +00001860
Chris Lattnerc91a4752006-06-26 22:48:35 +00001861 const unsigned *GPR = isPPC64 ? GPR_64 : GPR_32;
1862
Chris Lattner9a2a4972006-05-17 06:01:33 +00001863 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
Chris Lattnere2199452006-08-11 17:38:39 +00001864 SmallVector<SDOperand, 8> MemOpChains;
Evan Cheng4360bdc2006-05-25 00:57:32 +00001865 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattner9f0bc652007-02-25 05:34:32 +00001866 bool inMem = false;
Evan Cheng4360bdc2006-05-25 00:57:32 +00001867 SDOperand Arg = Op.getOperand(5+2*i);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001868 ISD::ArgFlagsTy Flags =
1869 cast<ARG_FLAGSSDNode>(Op.getOperand(5+2*i+1))->getArgFlags();
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00001870 // See if next argument requires stack alignment in ELF
Nicolas Geoffray6ccbbd82008-04-15 08:08:50 +00001871 bool Align = Flags.isSplit();
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00001872
Chris Lattnerc8b682c2006-05-17 00:15:40 +00001873 // PtrOff will be used to store the current argument to the stack if a
1874 // register cannot be found for it.
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00001875 SDOperand PtrOff;
1876
Nicolas Geoffrayec58d9f2007-04-03 12:35:28 +00001877 // Stack align in ELF 32
Nicolas Geoffrayc0cb28f2008-04-13 13:40:22 +00001878 if (isELF32_ABI && Align)
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00001879 PtrOff = DAG.getConstant(ArgOffset + ((ArgOffset/4) % 2) * PtrByteSize,
1880 StackPtr.getValueType());
1881 else
1882 PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
1883
Chris Lattnerc91a4752006-06-26 22:48:35 +00001884 PtrOff = DAG.getNode(ISD::ADD, PtrVT, StackPtr, PtrOff);
1885
1886 // On PPC64, promote integers to 64-bit values.
1887 if (isPPC64 && Arg.getValueType() == MVT::i32) {
Duncan Sands276dcbd2008-03-21 09:14:45 +00001888 // FIXME: Should this use ANY_EXTEND if neither sext nor zext?
1889 unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Chris Lattnerc91a4752006-06-26 22:48:35 +00001890 Arg = DAG.getNode(ExtOp, MVT::i64, Arg);
1891 }
Dale Johannesen5b3b6952008-03-04 23:17:14 +00001892
1893 // FIXME Elf untested, what are alignment rules?
Dale Johannesen8419dd62008-03-07 20:27:40 +00001894 // FIXME memcpy is used way more than necessary. Correctness first.
Duncan Sands276dcbd2008-03-21 09:14:45 +00001895 if (Flags.isByVal()) {
1896 unsigned Size = Flags.getByValSize();
Nicolas Geoffrayc0cb28f2008-04-13 13:40:22 +00001897 if (isELF32_ABI && Align) GPR_idx += (GPR_idx % 2);
Dale Johannesen8419dd62008-03-07 20:27:40 +00001898 if (Size==1 || Size==2) {
1899 // Very small objects are passed right-justified.
1900 // Everything else is passed left-justified.
1901 MVT::ValueType VT = (Size==1) ? MVT::i8 : MVT::i16;
1902 if (GPR_idx != NumGPRs) {
1903 SDOperand Load = DAG.getExtLoad(ISD::EXTLOAD, PtrVT, Chain, Arg,
1904 NULL, 0, VT);
1905 MemOpChains.push_back(Load.getValue(1));
1906 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
1907 if (isMachoABI)
1908 ArgOffset += PtrByteSize;
1909 } else {
1910 SDOperand Const = DAG.getConstant(4 - Size, PtrOff.getValueType());
1911 SDOperand AddPtr = DAG.getNode(ISD::ADD, PtrVT, PtrOff, Const);
1912 SDOperand MemcpyCall = CreateCopyOfByValArgument(Arg, AddPtr,
1913 CallSeqStart.Val->getOperand(0),
1914 Flags, DAG, Size);
1915 // This must go outside the CALLSEQ_START..END.
1916 SDOperand NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
1917 CallSeqStart.Val->getOperand(1));
1918 DAG.ReplaceAllUsesWith(CallSeqStart.Val, NewCallSeqStart.Val);
1919 Chain = CallSeqStart = NewCallSeqStart;
1920 ArgOffset += PtrByteSize;
1921 }
1922 continue;
1923 }
Dale Johannesenfdd3ade2008-03-17 02:13:43 +00001924 // Copy entire object into memory. There are cases where gcc-generated
1925 // code assumes it is there, even if it could be put entirely into
1926 // registers. (This is not what the doc says.)
1927 SDOperand MemcpyCall = CreateCopyOfByValArgument(Arg, PtrOff,
1928 CallSeqStart.Val->getOperand(0),
1929 Flags, DAG, Size);
1930 // This must go outside the CALLSEQ_START..END.
1931 SDOperand NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
1932 CallSeqStart.Val->getOperand(1));
1933 DAG.ReplaceAllUsesWith(CallSeqStart.Val, NewCallSeqStart.Val);
1934 Chain = CallSeqStart = NewCallSeqStart;
1935 // And copy the pieces of it that fit into registers.
Dale Johannesen5b3b6952008-03-04 23:17:14 +00001936 for (unsigned j=0; j<Size; j+=PtrByteSize) {
1937 SDOperand Const = DAG.getConstant(j, PtrOff.getValueType());
1938 SDOperand AddArg = DAG.getNode(ISD::ADD, PtrVT, Arg, Const);
1939 if (GPR_idx != NumGPRs) {
1940 SDOperand Load = DAG.getLoad(PtrVT, Chain, AddArg, NULL, 0);
Dale Johannesen1f797a32008-03-05 23:31:27 +00001941 MemOpChains.push_back(Load.getValue(1));
Dale Johannesen5b3b6952008-03-04 23:17:14 +00001942 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
1943 if (isMachoABI)
1944 ArgOffset += PtrByteSize;
1945 } else {
Dale Johannesenfdd3ade2008-03-17 02:13:43 +00001946 ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize;
Dale Johannesen8419dd62008-03-07 20:27:40 +00001947 break;
Dale Johannesen5b3b6952008-03-04 23:17:14 +00001948 }
1949 }
1950 continue;
1951 }
1952
Chris Lattnerc8b682c2006-05-17 00:15:40 +00001953 switch (Arg.getValueType()) {
1954 default: assert(0 && "Unexpected ValueType for argument!");
1955 case MVT::i32:
Chris Lattnerc91a4752006-06-26 22:48:35 +00001956 case MVT::i64:
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00001957 // Double word align in ELF
Nicolas Geoffrayc0cb28f2008-04-13 13:40:22 +00001958 if (isELF32_ABI && Align) GPR_idx += (GPR_idx % 2);
Chris Lattner9a2a4972006-05-17 06:01:33 +00001959 if (GPR_idx != NumGPRs) {
1960 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg));
Chris Lattnerc8b682c2006-05-17 00:15:40 +00001961 } else {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001962 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
Chris Lattner9f0bc652007-02-25 05:34:32 +00001963 inMem = true;
Chris Lattnerc8b682c2006-05-17 00:15:40 +00001964 }
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00001965 if (inMem || isMachoABI) {
1966 // Stack align in ELF
Nicolas Geoffrayc0cb28f2008-04-13 13:40:22 +00001967 if (isELF32_ABI && Align)
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00001968 ArgOffset += ((ArgOffset/4) % 2) * PtrByteSize;
1969
1970 ArgOffset += PtrByteSize;
1971 }
Chris Lattnerc8b682c2006-05-17 00:15:40 +00001972 break;
1973 case MVT::f32:
1974 case MVT::f64:
Chris Lattner9a2a4972006-05-17 06:01:33 +00001975 if (FPR_idx != NumFPRs) {
1976 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg));
1977
Chris Lattnerc8b682c2006-05-17 00:15:40 +00001978 if (isVarArg) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001979 SDOperand Store = DAG.getStore(Chain, Arg, PtrOff, NULL, 0);
Chris Lattner9a2a4972006-05-17 06:01:33 +00001980 MemOpChains.push_back(Store);
1981
Chris Lattnerc8b682c2006-05-17 00:15:40 +00001982 // Float varargs are always shadowed in available integer registers
Chris Lattner9a2a4972006-05-17 06:01:33 +00001983 if (GPR_idx != NumGPRs) {
Evan Cheng466685d2006-10-09 20:57:25 +00001984 SDOperand Load = DAG.getLoad(PtrVT, Store, PtrOff, NULL, 0);
Chris Lattner9a2a4972006-05-17 06:01:33 +00001985 MemOpChains.push_back(Load.getValue(1));
Chris Lattner9f0bc652007-02-25 05:34:32 +00001986 if (isMachoABI) RegsToPass.push_back(std::make_pair(GPR[GPR_idx++],
1987 Load));
Chris Lattnerc8b682c2006-05-17 00:15:40 +00001988 }
Jim Laskeyfbb74e62006-12-01 16:30:47 +00001989 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 && !isPPC64){
Chris Lattnerc8b682c2006-05-17 00:15:40 +00001990 SDOperand ConstFour = DAG.getConstant(4, PtrOff.getValueType());
Chris Lattnerc91a4752006-06-26 22:48:35 +00001991 PtrOff = DAG.getNode(ISD::ADD, PtrVT, PtrOff, ConstFour);
Evan Cheng466685d2006-10-09 20:57:25 +00001992 SDOperand Load = DAG.getLoad(PtrVT, Store, PtrOff, NULL, 0);
Chris Lattner9a2a4972006-05-17 06:01:33 +00001993 MemOpChains.push_back(Load.getValue(1));
Chris Lattner9f0bc652007-02-25 05:34:32 +00001994 if (isMachoABI) RegsToPass.push_back(std::make_pair(GPR[GPR_idx++],
1995 Load));
Chris Lattnerabde4602006-05-16 22:56:08 +00001996 }
1997 } else {
Chris Lattnerc8b682c2006-05-17 00:15:40 +00001998 // If we have any FPRs remaining, we may also have GPRs remaining.
1999 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
2000 // GPRs.
Chris Lattner9f0bc652007-02-25 05:34:32 +00002001 if (isMachoABI) {
2002 if (GPR_idx != NumGPRs)
2003 ++GPR_idx;
2004 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 &&
2005 !isPPC64) // PPC64 has 64-bit GPR's obviously :)
2006 ++GPR_idx;
2007 }
Chris Lattnerabde4602006-05-16 22:56:08 +00002008 }
Chris Lattnerc8b682c2006-05-17 00:15:40 +00002009 } else {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002010 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
Chris Lattner9f0bc652007-02-25 05:34:32 +00002011 inMem = true;
Chris Lattnerabde4602006-05-16 22:56:08 +00002012 }
Chris Lattner9f0bc652007-02-25 05:34:32 +00002013 if (inMem || isMachoABI) {
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00002014 // Stack align in ELF
Nicolas Geoffrayc0cb28f2008-04-13 13:40:22 +00002015 if (isELF32_ABI && Align)
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00002016 ArgOffset += ((ArgOffset/4) % 2) * PtrByteSize;
Chris Lattner9f0bc652007-02-25 05:34:32 +00002017 if (isPPC64)
2018 ArgOffset += 8;
2019 else
2020 ArgOffset += Arg.getValueType() == MVT::f32 ? 4 : 8;
2021 }
Chris Lattnerc8b682c2006-05-17 00:15:40 +00002022 break;
2023 case MVT::v4f32:
2024 case MVT::v4i32:
2025 case MVT::v8i16:
2026 case MVT::v16i8:
Dale Johannesen75092de2008-03-12 00:22:17 +00002027 if (isVarArg) {
2028 // These go aligned on the stack, or in the corresponding R registers
2029 // when within range. The Darwin PPC ABI doc claims they also go in
2030 // V registers; in fact gcc does this only for arguments that are
2031 // prototyped, not for those that match the ... We do it for all
2032 // arguments, seems to work.
2033 while (ArgOffset % 16 !=0) {
2034 ArgOffset += PtrByteSize;
2035 if (GPR_idx != NumGPRs)
2036 GPR_idx++;
2037 }
2038 // We could elide this store in the case where the object fits
2039 // entirely in R registers. Maybe later.
2040 PtrOff = DAG.getNode(ISD::ADD, PtrVT, StackPtr,
2041 DAG.getConstant(ArgOffset, PtrVT));
2042 SDOperand Store = DAG.getStore(Chain, Arg, PtrOff, NULL, 0);
2043 MemOpChains.push_back(Store);
2044 if (VR_idx != NumVRs) {
2045 SDOperand Load = DAG.getLoad(MVT::v4f32, Store, PtrOff, NULL, 0);
2046 MemOpChains.push_back(Load.getValue(1));
2047 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Load));
2048 }
2049 ArgOffset += 16;
2050 for (unsigned i=0; i<16; i+=PtrByteSize) {
2051 if (GPR_idx == NumGPRs)
2052 break;
2053 SDOperand Ix = DAG.getNode(ISD::ADD, PtrVT, PtrOff,
2054 DAG.getConstant(i, PtrVT));
2055 SDOperand Load = DAG.getLoad(PtrVT, Store, Ix, NULL, 0);
2056 MemOpChains.push_back(Load.getValue(1));
2057 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
2058 }
2059 break;
2060 }
Dale Johannesen8f5422c2008-03-14 17:41:26 +00002061 // Non-varargs Altivec params generally go in registers, but have
2062 // stack space allocated at the end.
2063 if (VR_idx != NumVRs) {
2064 // Doesn't have GPR space allocated.
2065 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Arg));
2066 } else if (nAltivecParamsAtEnd==0) {
2067 // We are emitting Altivec params in order.
Dale Johannesen75092de2008-03-12 00:22:17 +00002068 PtrOff = DAG.getNode(ISD::ADD, PtrVT, StackPtr,
2069 DAG.getConstant(ArgOffset, PtrVT));
2070 SDOperand Store = DAG.getStore(Chain, Arg, PtrOff, NULL, 0);
2071 MemOpChains.push_back(Store);
2072 ArgOffset += 16;
Dale Johannesen75092de2008-03-12 00:22:17 +00002073 }
Chris Lattnerc8b682c2006-05-17 00:15:40 +00002074 break;
Chris Lattnerabde4602006-05-16 22:56:08 +00002075 }
Chris Lattnerabde4602006-05-16 22:56:08 +00002076 }
Dale Johannesen8f5422c2008-03-14 17:41:26 +00002077 // If all Altivec parameters fit in registers, as they usually do,
2078 // they get stack space following the non-Altivec parameters. We
2079 // don't track this here because nobody below needs it.
2080 // If there are more Altivec parameters than fit in registers emit
2081 // the stores here.
2082 if (!isVarArg && nAltivecParamsAtEnd > NumVRs) {
2083 unsigned j = 0;
2084 // Offset is aligned; skip 1st 12 params which go in V registers.
2085 ArgOffset = ((ArgOffset+15)/16)*16;
2086 ArgOffset += 12*16;
2087 for (unsigned i = 0; i != NumOps; ++i) {
2088 SDOperand Arg = Op.getOperand(5+2*i);
2089 MVT::ValueType ArgType = Arg.getValueType();
2090 if (ArgType==MVT::v4f32 || ArgType==MVT::v4i32 ||
2091 ArgType==MVT::v8i16 || ArgType==MVT::v16i8) {
2092 if (++j > NumVRs) {
2093 SDOperand PtrOff = DAG.getNode(ISD::ADD, PtrVT, StackPtr,
2094 DAG.getConstant(ArgOffset, PtrVT));
2095 SDOperand Store = DAG.getStore(Chain, Arg, PtrOff, NULL, 0);
2096 MemOpChains.push_back(Store);
2097 ArgOffset += 16;
2098 }
2099 }
2100 }
2101 }
2102
Chris Lattner9a2a4972006-05-17 06:01:33 +00002103 if (!MemOpChains.empty())
Chris Lattnere2199452006-08-11 17:38:39 +00002104 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
2105 &MemOpChains[0], MemOpChains.size());
Chris Lattnerabde4602006-05-16 22:56:08 +00002106
Chris Lattner9a2a4972006-05-17 06:01:33 +00002107 // Build a sequence of copy-to-reg nodes chained together with token chain
2108 // and flag operands which copy the outgoing args into the appropriate regs.
2109 SDOperand InFlag;
2110 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2111 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
2112 InFlag);
2113 InFlag = Chain.getValue(1);
2114 }
Chris Lattner9f0bc652007-02-25 05:34:32 +00002115
Nicolas Geoffrayec58d9f2007-04-03 12:35:28 +00002116 // With the ELF 32 ABI, set CR6 to true if this is a vararg call.
2117 if (isVarArg && isELF32_ABI) {
Nicolas Geoffray0404cd92008-03-10 14:12:10 +00002118 SDOperand SetCR(DAG.getTargetNode(PPC::CRSET, MVT::i32), 0);
2119 Chain = DAG.getCopyToReg(Chain, PPC::CR1EQ, SetCR, InFlag);
Chris Lattner9f0bc652007-02-25 05:34:32 +00002120 InFlag = Chain.getValue(1);
2121 }
2122
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002123 std::vector<MVT::ValueType> NodeTys;
Chris Lattner4a45abf2006-06-10 01:14:28 +00002124 NodeTys.push_back(MVT::Other); // Returns a chain
2125 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
2126
Chris Lattner79e490a2006-08-11 17:18:05 +00002127 SmallVector<SDOperand, 8> Ops;
Nicolas Geoffray63f8fb12007-02-27 13:01:19 +00002128 unsigned CallOpc = isMachoABI? PPCISD::CALL_Macho : PPCISD::CALL_ELF;
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002129
2130 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
2131 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2132 // node so that legalize doesn't hack it.
Nicolas Geoffray5a6c91a2007-12-21 12:22:29 +00002133 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
2134 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), Callee.getValueType());
2135 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002136 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), Callee.getValueType());
2137 else if (SDNode *Dest = isBLACompatibleAddress(Callee, DAG))
2138 // If this is an absolute destination address, use the munged value.
2139 Callee = SDOperand(Dest, 0);
2140 else {
2141 // Otherwise, this is an indirect call. We have to use a MTCTR/BCTRL pair
2142 // to do the call, we can't use PPCISD::CALL.
Chris Lattner79e490a2006-08-11 17:18:05 +00002143 SDOperand MTCTROps[] = {Chain, Callee, InFlag};
2144 Chain = DAG.getNode(PPCISD::MTCTR, NodeTys, MTCTROps, 2+(InFlag.Val!=0));
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002145 InFlag = Chain.getValue(1);
2146
Chris Lattnerdc9971a2008-03-09 20:49:33 +00002147 // Copy the callee address into R12/X12 on darwin.
Chris Lattner9f0bc652007-02-25 05:34:32 +00002148 if (isMachoABI) {
Chris Lattnerdc9971a2008-03-09 20:49:33 +00002149 unsigned Reg = Callee.getValueType() == MVT::i32 ? PPC::R12 : PPC::X12;
2150 Chain = DAG.getCopyToReg(Chain, Reg, Callee, InFlag);
Chris Lattner9f0bc652007-02-25 05:34:32 +00002151 InFlag = Chain.getValue(1);
2152 }
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002153
2154 NodeTys.clear();
2155 NodeTys.push_back(MVT::Other);
2156 NodeTys.push_back(MVT::Flag);
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002157 Ops.push_back(Chain);
Chris Lattner9f0bc652007-02-25 05:34:32 +00002158 CallOpc = isMachoABI ? PPCISD::BCTRL_Macho : PPCISD::BCTRL_ELF;
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002159 Callee.Val = 0;
2160 }
Chris Lattner9a2a4972006-05-17 06:01:33 +00002161
Chris Lattner4a45abf2006-06-10 01:14:28 +00002162 // If this is a direct call, pass the chain and the callee.
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002163 if (Callee.Val) {
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002164 Ops.push_back(Chain);
2165 Ops.push_back(Callee);
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002166 }
Chris Lattnerabde4602006-05-16 22:56:08 +00002167
Chris Lattner4a45abf2006-06-10 01:14:28 +00002168 // Add argument registers to the end of the list so that they are known live
2169 // into the call.
2170 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2171 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2172 RegsToPass[i].second.getValueType()));
2173
2174 if (InFlag.Val)
2175 Ops.push_back(InFlag);
Chris Lattner79e490a2006-08-11 17:18:05 +00002176 Chain = DAG.getNode(CallOpc, NodeTys, &Ops[0], Ops.size());
Chris Lattner4a45abf2006-06-10 01:14:28 +00002177 InFlag = Chain.getValue(1);
2178
Bill Wendling0f8d9c02007-11-13 00:44:25 +00002179 Chain = DAG.getCALLSEQ_END(Chain,
2180 DAG.getConstant(NumBytes, PtrVT),
2181 DAG.getConstant(0, PtrVT),
2182 InFlag);
2183 if (Op.Val->getValueType(0) != MVT::Other)
2184 InFlag = Chain.getValue(1);
2185
Dan Gohman7925ed02008-03-19 21:39:28 +00002186 SmallVector<SDOperand, 16> ResultVals;
2187 SmallVector<CCValAssign, 16> RVLocs;
2188 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
2189 CCState CCInfo(CC, isVarArg, TM, RVLocs);
2190 CCInfo.AnalyzeCallResult(Op.Val, RetCC_PPC);
Chris Lattner9a2a4972006-05-17 06:01:33 +00002191
Dan Gohman7925ed02008-03-19 21:39:28 +00002192 // Copy all of the result registers out of their specified physreg.
2193 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
2194 CCValAssign &VA = RVLocs[i];
2195 MVT::ValueType VT = VA.getValVT();
2196 assert(VA.isRegLoc() && "Can only return in registers!");
2197 Chain = DAG.getCopyFromReg(Chain, VA.getLocReg(), VT, InFlag).getValue(1);
2198 ResultVals.push_back(Chain.getValue(0));
2199 InFlag = Chain.getValue(2);
Chris Lattner9a2a4972006-05-17 06:01:33 +00002200 }
Dan Gohman7925ed02008-03-19 21:39:28 +00002201
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002202 // If the function returns void, just return the chain.
Dan Gohman7925ed02008-03-19 21:39:28 +00002203 if (RVLocs.empty())
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002204 return Chain;
2205
2206 // Otherwise, merge everything together with a MERGE_VALUES node.
Dan Gohman7925ed02008-03-19 21:39:28 +00002207 ResultVals.push_back(Chain);
2208 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
2209 &ResultVals[0], ResultVals.size());
Chris Lattnerabde4602006-05-16 22:56:08 +00002210 return Res.getValue(Op.ResNo);
2211}
2212
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002213SDOperand PPCTargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG,
2214 TargetMachine &TM) {
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00002215 SmallVector<CCValAssign, 16> RVLocs;
2216 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
Chris Lattner52387be2007-06-19 00:13:10 +00002217 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
2218 CCState CCInfo(CC, isVarArg, TM, RVLocs);
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00002219 CCInfo.AnalyzeReturn(Op.Val, RetCC_PPC);
2220
2221 // If this is the first return lowered for this function, add the regs to the
2222 // liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00002223 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00002224 for (unsigned i = 0; i != RVLocs.size(); ++i)
Chris Lattner84bc5422007-12-31 04:13:23 +00002225 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00002226 }
2227
Chris Lattnercaddd442007-02-26 19:44:02 +00002228 SDOperand Chain = Op.getOperand(0);
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00002229 SDOperand Flag;
2230
2231 // Copy the result values into the output registers.
2232 for (unsigned i = 0; i != RVLocs.size(); ++i) {
2233 CCValAssign &VA = RVLocs[i];
2234 assert(VA.isRegLoc() && "Can only return in registers!");
2235 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1), Flag);
2236 Flag = Chain.getValue(1);
2237 }
2238
2239 if (Flag.Val)
2240 return DAG.getNode(PPCISD::RET_FLAG, MVT::Other, Chain, Flag);
2241 else
Chris Lattnercaddd442007-02-26 19:44:02 +00002242 return DAG.getNode(PPCISD::RET_FLAG, MVT::Other, Chain);
Chris Lattner1a635d62006-04-14 06:01:58 +00002243}
2244
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002245SDOperand PPCTargetLowering::LowerSTACKRESTORE(SDOperand Op, SelectionDAG &DAG,
Jim Laskeyefc7e522006-12-04 22:04:42 +00002246 const PPCSubtarget &Subtarget) {
2247 // When we pop the dynamic allocation we need to restore the SP link.
2248
2249 // Get the corect type for pointers.
2250 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
2251
2252 // Construct the stack pointer operand.
2253 bool IsPPC64 = Subtarget.isPPC64();
2254 unsigned SP = IsPPC64 ? PPC::X1 : PPC::R1;
2255 SDOperand StackPtr = DAG.getRegister(SP, PtrVT);
2256
2257 // Get the operands for the STACKRESTORE.
2258 SDOperand Chain = Op.getOperand(0);
2259 SDOperand SaveSP = Op.getOperand(1);
2260
2261 // Load the old link SP.
2262 SDOperand LoadLinkSP = DAG.getLoad(PtrVT, Chain, StackPtr, NULL, 0);
2263
2264 // Restore the stack pointer.
2265 Chain = DAG.getCopyToReg(LoadLinkSP.getValue(1), SP, SaveSP);
2266
2267 // Store the old link SP.
2268 return DAG.getStore(Chain, LoadLinkSP, StackPtr, NULL, 0);
2269}
2270
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002271SDOperand PPCTargetLowering::LowerDYNAMIC_STACKALLOC(SDOperand Op,
2272 SelectionDAG &DAG,
Jim Laskey2f616bf2006-11-16 22:43:37 +00002273 const PPCSubtarget &Subtarget) {
2274 MachineFunction &MF = DAG.getMachineFunction();
2275 bool IsPPC64 = Subtarget.isPPC64();
Chris Lattner9f0bc652007-02-25 05:34:32 +00002276 bool isMachoABI = Subtarget.isMachoABI();
Jim Laskey2f616bf2006-11-16 22:43:37 +00002277
2278 // Get current frame pointer save index. The users of this index will be
2279 // primarily DYNALLOC instructions.
2280 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
2281 int FPSI = FI->getFramePointerSaveIndex();
Chris Lattner9f0bc652007-02-25 05:34:32 +00002282
Jim Laskey2f616bf2006-11-16 22:43:37 +00002283 // If the frame pointer save index hasn't been defined yet.
2284 if (!FPSI) {
2285 // Find out what the fix offset of the frame pointer save area.
Chris Lattner9f0bc652007-02-25 05:34:32 +00002286 int FPOffset = PPCFrameInfo::getFramePointerSaveOffset(IsPPC64, isMachoABI);
2287
Jim Laskey2f616bf2006-11-16 22:43:37 +00002288 // Allocate the frame index for frame pointer save area.
Chris Lattner9f0bc652007-02-25 05:34:32 +00002289 FPSI = MF.getFrameInfo()->CreateFixedObject(IsPPC64? 8 : 4, FPOffset);
Jim Laskey2f616bf2006-11-16 22:43:37 +00002290 // Save the result.
2291 FI->setFramePointerSaveIndex(FPSI);
2292 }
2293
2294 // Get the inputs.
2295 SDOperand Chain = Op.getOperand(0);
2296 SDOperand Size = Op.getOperand(1);
2297
2298 // Get the corect type for pointers.
2299 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
2300 // Negate the size.
2301 SDOperand NegSize = DAG.getNode(ISD::SUB, PtrVT,
2302 DAG.getConstant(0, PtrVT), Size);
2303 // Construct a node for the frame pointer save index.
2304 SDOperand FPSIdx = DAG.getFrameIndex(FPSI, PtrVT);
2305 // Build a DYNALLOC node.
2306 SDOperand Ops[3] = { Chain, NegSize, FPSIdx };
2307 SDVTList VTs = DAG.getVTList(PtrVT, MVT::Other);
2308 return DAG.getNode(PPCISD::DYNALLOC, VTs, Ops, 3);
2309}
2310
Evan Cheng54fc97d2008-04-19 01:30:48 +00002311SDOperand PPCTargetLowering::LowerAtomicLAS(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng8608f2e2008-04-19 02:30:38 +00002312 MVT::ValueType VT = Op.Val->getValueType(0);
Evan Cheng54fc97d2008-04-19 01:30:48 +00002313 SDOperand Chain = Op.getOperand(0);
2314 SDOperand Ptr = Op.getOperand(1);
2315 SDOperand Incr = Op.getOperand(2);
2316
2317 // Issue a "load and reserve".
2318 std::vector<MVT::ValueType> VTs;
2319 VTs.push_back(VT);
2320 VTs.push_back(MVT::Other);
2321
2322 SDOperand Label = DAG.getConstant(PPCAtomicLabelIndex++, MVT::i32);
2323 SDOperand Ops[] = {
Evan Cheng8608f2e2008-04-19 02:30:38 +00002324 Chain, // Chain
2325 Ptr, // Ptr
2326 Label, // Label
Evan Cheng54fc97d2008-04-19 01:30:48 +00002327 };
Evan Cheng8608f2e2008-04-19 02:30:38 +00002328 SDOperand Load = DAG.getNode(PPCISD::LARX, VTs, Ops, 3);
Evan Cheng54fc97d2008-04-19 01:30:48 +00002329 Chain = Load.getValue(1);
2330
2331 // Compute new value.
2332 SDOperand NewVal = DAG.getNode(ISD::ADD, VT, Load, Incr);
2333
2334 // Issue a "store and check".
2335 SDOperand Ops2[] = {
Evan Cheng8608f2e2008-04-19 02:30:38 +00002336 Chain, // Chain
2337 NewVal, // Value
2338 Ptr, // Ptr
2339 Label, // Label
Evan Cheng54fc97d2008-04-19 01:30:48 +00002340 };
Evan Cheng8608f2e2008-04-19 02:30:38 +00002341 SDOperand Store = DAG.getNode(PPCISD::STCX, MVT::Other, Ops2, 4);
Evan Cheng54fc97d2008-04-19 01:30:48 +00002342 SDOperand OutOps[] = { Load, Store };
2343 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
2344 OutOps, 2);
2345}
2346
2347SDOperand PPCTargetLowering::LowerAtomicLCS(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng8608f2e2008-04-19 02:30:38 +00002348 MVT::ValueType VT = Op.Val->getValueType(0);
Evan Cheng54fc97d2008-04-19 01:30:48 +00002349 SDOperand Chain = Op.getOperand(0);
2350 SDOperand Ptr = Op.getOperand(1);
2351 SDOperand NewVal = Op.getOperand(2);
2352 SDOperand OldVal = Op.getOperand(3);
2353
2354 // Issue a "load and reserve".
2355 std::vector<MVT::ValueType> VTs;
2356 VTs.push_back(VT);
2357 VTs.push_back(MVT::Other);
2358
2359 SDOperand Label = DAG.getConstant(PPCAtomicLabelIndex++, MVT::i32);
2360 SDOperand Ops[] = {
Evan Cheng8608f2e2008-04-19 02:30:38 +00002361 Chain, // Chain
2362 Ptr, // Ptr
2363 Label, // Label
Evan Cheng54fc97d2008-04-19 01:30:48 +00002364 };
Evan Cheng8608f2e2008-04-19 02:30:38 +00002365 SDOperand Load = DAG.getNode(PPCISD::LARX, VTs, Ops, 3);
Evan Cheng54fc97d2008-04-19 01:30:48 +00002366 Chain = Load.getValue(1);
2367
2368 // Compare and unreserve if not equal.
2369 SDOperand Ops2[] = {
Evan Cheng8608f2e2008-04-19 02:30:38 +00002370 Chain, // Chain
2371 OldVal, // Old value
2372 Load, // Value in memory
2373 Label, // Label
Evan Cheng54fc97d2008-04-19 01:30:48 +00002374 };
2375 Chain = DAG.getNode(PPCISD::CMP_UNRESERVE, MVT::Other, Ops2, 4);
2376
2377 // Issue a "store and check".
2378 SDOperand Ops3[] = {
Evan Cheng8608f2e2008-04-19 02:30:38 +00002379 Chain, // Chain
2380 NewVal, // Value
2381 Ptr, // Ptr
2382 Label, // Label
Evan Cheng54fc97d2008-04-19 01:30:48 +00002383 };
Evan Cheng8608f2e2008-04-19 02:30:38 +00002384 SDOperand Store = DAG.getNode(PPCISD::STCX, MVT::Other, Ops3, 4);
Evan Cheng54fc97d2008-04-19 01:30:48 +00002385 SDOperand OutOps[] = { Load, Store };
2386 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
2387 OutOps, 2);
2388}
2389
2390SDOperand PPCTargetLowering::LowerAtomicSWAP(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng8608f2e2008-04-19 02:30:38 +00002391 MVT::ValueType VT = Op.Val->getValueType(0);
Evan Cheng54fc97d2008-04-19 01:30:48 +00002392 SDOperand Chain = Op.getOperand(0);
2393 SDOperand Ptr = Op.getOperand(1);
2394 SDOperand NewVal = Op.getOperand(2);
2395
2396 // Issue a "load and reserve".
2397 std::vector<MVT::ValueType> VTs;
2398 VTs.push_back(VT);
2399 VTs.push_back(MVT::Other);
2400
2401 SDOperand Label = DAG.getConstant(PPCAtomicLabelIndex++, MVT::i32);
2402 SDOperand Ops[] = {
Evan Cheng8608f2e2008-04-19 02:30:38 +00002403 Chain, // Chain
2404 Ptr, // Ptr
2405 Label, // Label
Evan Cheng54fc97d2008-04-19 01:30:48 +00002406 };
Evan Cheng8608f2e2008-04-19 02:30:38 +00002407 SDOperand Load = DAG.getNode(PPCISD::LARX, VTs, Ops, 3);
Evan Cheng54fc97d2008-04-19 01:30:48 +00002408 Chain = Load.getValue(1);
2409
2410 // Issue a "store and check".
2411 SDOperand Ops2[] = {
Evan Cheng8608f2e2008-04-19 02:30:38 +00002412 Chain, // Chain
2413 NewVal, // Value
2414 Ptr, // Ptr
2415 Label, // Label
Evan Cheng54fc97d2008-04-19 01:30:48 +00002416 };
Evan Cheng8608f2e2008-04-19 02:30:38 +00002417 SDOperand Store = DAG.getNode(PPCISD::STCX, MVT::Other, Ops2, 4);
Evan Cheng54fc97d2008-04-19 01:30:48 +00002418 SDOperand OutOps[] = { Load, Store };
2419 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
2420 OutOps, 2);
2421}
Jim Laskey2f616bf2006-11-16 22:43:37 +00002422
Chris Lattner1a635d62006-04-14 06:01:58 +00002423/// LowerSELECT_CC - Lower floating point select_cc's into fsel instruction when
2424/// possible.
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002425SDOperand PPCTargetLowering::LowerSELECT_CC(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner1a635d62006-04-14 06:01:58 +00002426 // Not FP? Not a fsel.
2427 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
2428 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
2429 return SDOperand();
2430
2431 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
2432
2433 // Cannot handle SETEQ/SETNE.
2434 if (CC == ISD::SETEQ || CC == ISD::SETNE) return SDOperand();
2435
2436 MVT::ValueType ResVT = Op.getValueType();
2437 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
2438 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
2439 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
2440
2441 // If the RHS of the comparison is a 0.0, we don't need to do the
2442 // subtraction at all.
2443 if (isFloatingPointZero(RHS))
2444 switch (CC) {
2445 default: break; // SETUO etc aren't handled by fsel.
2446 case ISD::SETULT:
Chris Lattner57340122006-05-24 00:06:44 +00002447 case ISD::SETOLT:
Chris Lattner1a635d62006-04-14 06:01:58 +00002448 case ISD::SETLT:
2449 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
2450 case ISD::SETUGE:
Chris Lattner57340122006-05-24 00:06:44 +00002451 case ISD::SETOGE:
Chris Lattner1a635d62006-04-14 06:01:58 +00002452 case ISD::SETGE:
2453 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
2454 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
2455 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
2456 case ISD::SETUGT:
Chris Lattner57340122006-05-24 00:06:44 +00002457 case ISD::SETOGT:
Chris Lattner1a635d62006-04-14 06:01:58 +00002458 case ISD::SETGT:
2459 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
2460 case ISD::SETULE:
Chris Lattner57340122006-05-24 00:06:44 +00002461 case ISD::SETOLE:
Chris Lattner1a635d62006-04-14 06:01:58 +00002462 case ISD::SETLE:
2463 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
2464 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
2465 return DAG.getNode(PPCISD::FSEL, ResVT,
2466 DAG.getNode(ISD::FNEG, MVT::f64, LHS), TV, FV);
2467 }
2468
Chris Lattner1de7c1d2007-10-15 20:14:52 +00002469 SDOperand Cmp;
Chris Lattner1a635d62006-04-14 06:01:58 +00002470 switch (CC) {
2471 default: break; // SETUO etc aren't handled by fsel.
2472 case ISD::SETULT:
Chris Lattner57340122006-05-24 00:06:44 +00002473 case ISD::SETOLT:
Chris Lattner1a635d62006-04-14 06:01:58 +00002474 case ISD::SETLT:
2475 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
2476 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
2477 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
2478 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
2479 case ISD::SETUGE:
Chris Lattner57340122006-05-24 00:06:44 +00002480 case ISD::SETOGE:
Chris Lattner1a635d62006-04-14 06:01:58 +00002481 case ISD::SETGE:
2482 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
2483 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
2484 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
2485 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
2486 case ISD::SETUGT:
Chris Lattner57340122006-05-24 00:06:44 +00002487 case ISD::SETOGT:
Chris Lattner1a635d62006-04-14 06:01:58 +00002488 case ISD::SETGT:
2489 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
2490 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
2491 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
2492 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
2493 case ISD::SETULE:
Chris Lattner57340122006-05-24 00:06:44 +00002494 case ISD::SETOLE:
Chris Lattner1a635d62006-04-14 06:01:58 +00002495 case ISD::SETLE:
2496 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
2497 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
2498 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
2499 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
2500 }
2501 return SDOperand();
2502}
2503
Chris Lattner1f873002007-11-28 18:44:47 +00002504// FIXME: Split this code up when LegalizeDAGTypes lands.
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002505SDOperand PPCTargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner1a635d62006-04-14 06:01:58 +00002506 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
2507 SDOperand Src = Op.getOperand(0);
2508 if (Src.getValueType() == MVT::f32)
2509 Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src);
2510
2511 SDOperand Tmp;
2512 switch (Op.getValueType()) {
2513 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
2514 case MVT::i32:
2515 Tmp = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
2516 break;
2517 case MVT::i64:
2518 Tmp = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
2519 break;
2520 }
2521
2522 // Convert the FP value to an int value through memory.
Chris Lattner1de7c1d2007-10-15 20:14:52 +00002523 SDOperand FIPtr = DAG.CreateStackTemporary(MVT::f64);
2524
2525 // Emit a store to the stack slot.
2526 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Tmp, FIPtr, NULL, 0);
2527
2528 // Result is a load from the stack slot. If loading 4 bytes, make sure to
2529 // add in a bias.
Chris Lattner1a635d62006-04-14 06:01:58 +00002530 if (Op.getValueType() == MVT::i32)
Chris Lattner1de7c1d2007-10-15 20:14:52 +00002531 FIPtr = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr,
2532 DAG.getConstant(4, FIPtr.getValueType()));
2533 return DAG.getLoad(Op.getValueType(), Chain, FIPtr, NULL, 0);
Chris Lattner1a635d62006-04-14 06:01:58 +00002534}
2535
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002536SDOperand PPCTargetLowering::LowerFP_ROUND_INREG(SDOperand Op,
2537 SelectionDAG &DAG) {
Dale Johannesen6eaeff22007-10-10 01:01:31 +00002538 assert(Op.getValueType() == MVT::ppcf128);
2539 SDNode *Node = Op.Val;
2540 assert(Node->getOperand(0).getValueType() == MVT::ppcf128);
Chris Lattner26cb2862007-10-19 04:08:28 +00002541 assert(Node->getOperand(0).Val->getOpcode() == ISD::BUILD_PAIR);
Dale Johannesen6eaeff22007-10-10 01:01:31 +00002542 SDOperand Lo = Node->getOperand(0).Val->getOperand(0);
2543 SDOperand Hi = Node->getOperand(0).Val->getOperand(1);
2544
2545 // This sequence changes FPSCR to do round-to-zero, adds the two halves
2546 // of the long double, and puts FPSCR back the way it was. We do not
2547 // actually model FPSCR.
2548 std::vector<MVT::ValueType> NodeTys;
2549 SDOperand Ops[4], Result, MFFSreg, InFlag, FPreg;
2550
2551 NodeTys.push_back(MVT::f64); // Return register
2552 NodeTys.push_back(MVT::Flag); // Returns a flag for later insns
2553 Result = DAG.getNode(PPCISD::MFFS, NodeTys, &InFlag, 0);
2554 MFFSreg = Result.getValue(0);
2555 InFlag = Result.getValue(1);
2556
2557 NodeTys.clear();
2558 NodeTys.push_back(MVT::Flag); // Returns a flag
2559 Ops[0] = DAG.getConstant(31, MVT::i32);
2560 Ops[1] = InFlag;
2561 Result = DAG.getNode(PPCISD::MTFSB1, NodeTys, Ops, 2);
2562 InFlag = Result.getValue(0);
2563
2564 NodeTys.clear();
2565 NodeTys.push_back(MVT::Flag); // Returns a flag
2566 Ops[0] = DAG.getConstant(30, MVT::i32);
2567 Ops[1] = InFlag;
2568 Result = DAG.getNode(PPCISD::MTFSB0, NodeTys, Ops, 2);
2569 InFlag = Result.getValue(0);
2570
2571 NodeTys.clear();
2572 NodeTys.push_back(MVT::f64); // result of add
2573 NodeTys.push_back(MVT::Flag); // Returns a flag
2574 Ops[0] = Lo;
2575 Ops[1] = Hi;
2576 Ops[2] = InFlag;
2577 Result = DAG.getNode(PPCISD::FADDRTZ, NodeTys, Ops, 3);
2578 FPreg = Result.getValue(0);
2579 InFlag = Result.getValue(1);
2580
2581 NodeTys.clear();
2582 NodeTys.push_back(MVT::f64);
2583 Ops[0] = DAG.getConstant(1, MVT::i32);
2584 Ops[1] = MFFSreg;
2585 Ops[2] = FPreg;
2586 Ops[3] = InFlag;
2587 Result = DAG.getNode(PPCISD::MTFSF, NodeTys, Ops, 4);
2588 FPreg = Result.getValue(0);
2589
2590 // We know the low half is about to be thrown away, so just use something
2591 // convenient.
2592 return DAG.getNode(ISD::BUILD_PAIR, Lo.getValueType(), FPreg, FPreg);
2593}
2594
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002595SDOperand PPCTargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
Dan Gohman034f60e2008-03-11 01:59:03 +00002596 // Don't handle ppc_fp128 here; let it be lowered to a libcall.
2597 if (Op.getValueType() != MVT::f32 && Op.getValueType() != MVT::f64)
2598 return SDOperand();
2599
Chris Lattner1a635d62006-04-14 06:01:58 +00002600 if (Op.getOperand(0).getValueType() == MVT::i64) {
2601 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, Op.getOperand(0));
2602 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Bits);
2603 if (Op.getValueType() == MVT::f32)
Chris Lattner0bd48932008-01-17 07:00:52 +00002604 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP, DAG.getIntPtrConstant(0));
Chris Lattner1a635d62006-04-14 06:01:58 +00002605 return FP;
2606 }
2607
2608 assert(Op.getOperand(0).getValueType() == MVT::i32 &&
2609 "Unhandled SINT_TO_FP type in custom expander!");
2610 // Since we only generate this in 64-bit mode, we can take advantage of
2611 // 64-bit registers. In particular, sign extend the input value into the
2612 // 64-bit register with extsw, store the WHOLE 64-bit value into the stack
2613 // then lfd it and fcfid it.
2614 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
2615 int FrameIdx = FrameInfo->CreateStackObject(8, 8);
Chris Lattner0d72a202006-07-28 16:45:47 +00002616 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
2617 SDOperand FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Chris Lattner1a635d62006-04-14 06:01:58 +00002618
2619 SDOperand Ext64 = DAG.getNode(PPCISD::EXTSW_32, MVT::i32,
2620 Op.getOperand(0));
2621
2622 // STD the extended value into the stack slot.
Dan Gohman36b5c132008-04-07 19:35:22 +00002623 MachineMemOperand MO(PseudoSourceValue::getFixedStack(),
2624 MachineMemOperand::MOStore, FrameIdx, 8, 8);
Chris Lattner1a635d62006-04-14 06:01:58 +00002625 SDOperand Store = DAG.getNode(PPCISD::STD_32, MVT::Other,
2626 DAG.getEntryNode(), Ext64, FIdx,
Dan Gohman69de1932008-02-06 22:27:42 +00002627 DAG.getMemOperand(MO));
Chris Lattner1a635d62006-04-14 06:01:58 +00002628 // Load the value as a double.
Evan Cheng466685d2006-10-09 20:57:25 +00002629 SDOperand Ld = DAG.getLoad(MVT::f64, Store, FIdx, NULL, 0);
Chris Lattner1a635d62006-04-14 06:01:58 +00002630
2631 // FCFID it and return it.
2632 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Ld);
2633 if (Op.getValueType() == MVT::f32)
Chris Lattner0bd48932008-01-17 07:00:52 +00002634 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP, DAG.getIntPtrConstant(0));
Chris Lattner1a635d62006-04-14 06:01:58 +00002635 return FP;
2636}
2637
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002638SDOperand PPCTargetLowering::LowerFLT_ROUNDS_(SDOperand Op, SelectionDAG &DAG) {
Dale Johannesen5c5eb802008-01-18 19:55:37 +00002639 /*
2640 The rounding mode is in bits 30:31 of FPSR, and has the following
2641 settings:
2642 00 Round to nearest
2643 01 Round to 0
2644 10 Round to +inf
2645 11 Round to -inf
2646
2647 FLT_ROUNDS, on the other hand, expects the following:
2648 -1 Undefined
2649 0 Round to 0
2650 1 Round to nearest
2651 2 Round to +inf
2652 3 Round to -inf
2653
2654 To perform the conversion, we do:
2655 ((FPSCR & 0x3) ^ ((~FPSCR & 0x3) >> 1))
2656 */
2657
2658 MachineFunction &MF = DAG.getMachineFunction();
2659 MVT::ValueType VT = Op.getValueType();
2660 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
2661 std::vector<MVT::ValueType> NodeTys;
2662 SDOperand MFFSreg, InFlag;
2663
2664 // Save FP Control Word to register
2665 NodeTys.push_back(MVT::f64); // return register
2666 NodeTys.push_back(MVT::Flag); // unused in this context
2667 SDOperand Chain = DAG.getNode(PPCISD::MFFS, NodeTys, &InFlag, 0);
2668
2669 // Save FP register to stack slot
2670 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
2671 SDOperand StackSlot = DAG.getFrameIndex(SSFI, PtrVT);
2672 SDOperand Store = DAG.getStore(DAG.getEntryNode(), Chain,
2673 StackSlot, NULL, 0);
2674
2675 // Load FP Control Word from low 32 bits of stack slot.
2676 SDOperand Four = DAG.getConstant(4, PtrVT);
2677 SDOperand Addr = DAG.getNode(ISD::ADD, PtrVT, StackSlot, Four);
2678 SDOperand CWD = DAG.getLoad(MVT::i32, Store, Addr, NULL, 0);
2679
2680 // Transform as necessary
2681 SDOperand CWD1 =
2682 DAG.getNode(ISD::AND, MVT::i32,
2683 CWD, DAG.getConstant(3, MVT::i32));
2684 SDOperand CWD2 =
2685 DAG.getNode(ISD::SRL, MVT::i32,
2686 DAG.getNode(ISD::AND, MVT::i32,
2687 DAG.getNode(ISD::XOR, MVT::i32,
2688 CWD, DAG.getConstant(3, MVT::i32)),
2689 DAG.getConstant(3, MVT::i32)),
2690 DAG.getConstant(1, MVT::i8));
2691
2692 SDOperand RetVal =
2693 DAG.getNode(ISD::XOR, MVT::i32, CWD1, CWD2);
2694
2695 return DAG.getNode((MVT::getSizeInBits(VT) < 16 ?
2696 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
2697}
2698
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002699SDOperand PPCTargetLowering::LowerSHL_PARTS(SDOperand Op, SelectionDAG &DAG) {
Dan Gohman9ed06db2008-03-07 20:36:53 +00002700 MVT::ValueType VT = Op.getValueType();
2701 unsigned BitWidth = MVT::getSizeInBits(VT);
2702 assert(Op.getNumOperands() == 3 &&
2703 VT == Op.getOperand(1).getValueType() &&
2704 "Unexpected SHL!");
Chris Lattner1a635d62006-04-14 06:01:58 +00002705
Chris Lattner3fe6c1d2006-09-20 03:47:40 +00002706 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner1a635d62006-04-14 06:01:58 +00002707 // depend on the PPC behavior for oversized shift amounts.
Chris Lattner3fe6c1d2006-09-20 03:47:40 +00002708 SDOperand Lo = Op.getOperand(0);
2709 SDOperand Hi = Op.getOperand(1);
2710 SDOperand Amt = Op.getOperand(2);
Dan Gohman9ed06db2008-03-07 20:36:53 +00002711 MVT::ValueType AmtVT = Amt.getValueType();
Chris Lattner1a635d62006-04-14 06:01:58 +00002712
Dan Gohman9ed06db2008-03-07 20:36:53 +00002713 SDOperand Tmp1 = DAG.getNode(ISD::SUB, AmtVT,
2714 DAG.getConstant(BitWidth, AmtVT), Amt);
2715 SDOperand Tmp2 = DAG.getNode(PPCISD::SHL, VT, Hi, Amt);
2716 SDOperand Tmp3 = DAG.getNode(PPCISD::SRL, VT, Lo, Tmp1);
2717 SDOperand Tmp4 = DAG.getNode(ISD::OR , VT, Tmp2, Tmp3);
2718 SDOperand Tmp5 = DAG.getNode(ISD::ADD, AmtVT, Amt,
2719 DAG.getConstant(-BitWidth, AmtVT));
2720 SDOperand Tmp6 = DAG.getNode(PPCISD::SHL, VT, Lo, Tmp5);
2721 SDOperand OutHi = DAG.getNode(ISD::OR, VT, Tmp4, Tmp6);
2722 SDOperand OutLo = DAG.getNode(PPCISD::SHL, VT, Lo, Amt);
Chris Lattner3fe6c1d2006-09-20 03:47:40 +00002723 SDOperand OutOps[] = { OutLo, OutHi };
Dan Gohman9ed06db2008-03-07 20:36:53 +00002724 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, VT),
Chris Lattner3fe6c1d2006-09-20 03:47:40 +00002725 OutOps, 2);
Chris Lattner1a635d62006-04-14 06:01:58 +00002726}
2727
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002728SDOperand PPCTargetLowering::LowerSRL_PARTS(SDOperand Op, SelectionDAG &DAG) {
Dan Gohman9ed06db2008-03-07 20:36:53 +00002729 MVT::ValueType VT = Op.getValueType();
2730 unsigned BitWidth = MVT::getSizeInBits(VT);
2731 assert(Op.getNumOperands() == 3 &&
2732 VT == Op.getOperand(1).getValueType() &&
2733 "Unexpected SRL!");
Chris Lattner1a635d62006-04-14 06:01:58 +00002734
Dan Gohman9ed06db2008-03-07 20:36:53 +00002735 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner1a635d62006-04-14 06:01:58 +00002736 // depend on the PPC behavior for oversized shift amounts.
Chris Lattner3fe6c1d2006-09-20 03:47:40 +00002737 SDOperand Lo = Op.getOperand(0);
2738 SDOperand Hi = Op.getOperand(1);
2739 SDOperand Amt = Op.getOperand(2);
Dan Gohman9ed06db2008-03-07 20:36:53 +00002740 MVT::ValueType AmtVT = Amt.getValueType();
Chris Lattner1a635d62006-04-14 06:01:58 +00002741
Dan Gohman9ed06db2008-03-07 20:36:53 +00002742 SDOperand Tmp1 = DAG.getNode(ISD::SUB, AmtVT,
2743 DAG.getConstant(BitWidth, AmtVT), Amt);
2744 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, VT, Lo, Amt);
2745 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, VT, Hi, Tmp1);
2746 SDOperand Tmp4 = DAG.getNode(ISD::OR , VT, Tmp2, Tmp3);
2747 SDOperand Tmp5 = DAG.getNode(ISD::ADD, AmtVT, Amt,
2748 DAG.getConstant(-BitWidth, AmtVT));
2749 SDOperand Tmp6 = DAG.getNode(PPCISD::SRL, VT, Hi, Tmp5);
2750 SDOperand OutLo = DAG.getNode(ISD::OR, VT, Tmp4, Tmp6);
2751 SDOperand OutHi = DAG.getNode(PPCISD::SRL, VT, Hi, Amt);
Chris Lattner3fe6c1d2006-09-20 03:47:40 +00002752 SDOperand OutOps[] = { OutLo, OutHi };
Dan Gohman9ed06db2008-03-07 20:36:53 +00002753 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, VT),
Chris Lattner3fe6c1d2006-09-20 03:47:40 +00002754 OutOps, 2);
Chris Lattner1a635d62006-04-14 06:01:58 +00002755}
2756
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002757SDOperand PPCTargetLowering::LowerSRA_PARTS(SDOperand Op, SelectionDAG &DAG) {
Dan Gohman9ed06db2008-03-07 20:36:53 +00002758 MVT::ValueType VT = Op.getValueType();
2759 unsigned BitWidth = MVT::getSizeInBits(VT);
2760 assert(Op.getNumOperands() == 3 &&
2761 VT == Op.getOperand(1).getValueType() &&
2762 "Unexpected SRA!");
Chris Lattner1a635d62006-04-14 06:01:58 +00002763
Dan Gohman9ed06db2008-03-07 20:36:53 +00002764 // Expand into a bunch of logical ops, followed by a select_cc.
Chris Lattner3fe6c1d2006-09-20 03:47:40 +00002765 SDOperand Lo = Op.getOperand(0);
2766 SDOperand Hi = Op.getOperand(1);
2767 SDOperand Amt = Op.getOperand(2);
Dan Gohman9ed06db2008-03-07 20:36:53 +00002768 MVT::ValueType AmtVT = Amt.getValueType();
Chris Lattner1a635d62006-04-14 06:01:58 +00002769
Dan Gohman9ed06db2008-03-07 20:36:53 +00002770 SDOperand Tmp1 = DAG.getNode(ISD::SUB, AmtVT,
2771 DAG.getConstant(BitWidth, AmtVT), Amt);
2772 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, VT, Lo, Amt);
2773 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, VT, Hi, Tmp1);
2774 SDOperand Tmp4 = DAG.getNode(ISD::OR , VT, Tmp2, Tmp3);
2775 SDOperand Tmp5 = DAG.getNode(ISD::ADD, AmtVT, Amt,
2776 DAG.getConstant(-BitWidth, AmtVT));
2777 SDOperand Tmp6 = DAG.getNode(PPCISD::SRA, VT, Hi, Tmp5);
2778 SDOperand OutHi = DAG.getNode(PPCISD::SRA, VT, Hi, Amt);
2779 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, AmtVT),
Chris Lattner1a635d62006-04-14 06:01:58 +00002780 Tmp4, Tmp6, ISD::SETLE);
Chris Lattner3fe6c1d2006-09-20 03:47:40 +00002781 SDOperand OutOps[] = { OutLo, OutHi };
Dan Gohman9ed06db2008-03-07 20:36:53 +00002782 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, VT),
Chris Lattner3fe6c1d2006-09-20 03:47:40 +00002783 OutOps, 2);
Chris Lattner1a635d62006-04-14 06:01:58 +00002784}
2785
2786//===----------------------------------------------------------------------===//
2787// Vector related lowering.
2788//
2789
Chris Lattnerac225ca2006-04-12 19:07:14 +00002790// If this is a vector of constants or undefs, get the bits. A bit in
2791// UndefBits is set if the corresponding element of the vector is an
2792// ISD::UNDEF value. For undefs, the corresponding VectorBits values are
2793// zero. Return true if this is not an array of constants, false if it is.
2794//
Chris Lattnerac225ca2006-04-12 19:07:14 +00002795static bool GetConstantBuildVectorBits(SDNode *BV, uint64_t VectorBits[2],
2796 uint64_t UndefBits[2]) {
2797 // Start with zero'd results.
2798 VectorBits[0] = VectorBits[1] = UndefBits[0] = UndefBits[1] = 0;
2799
2800 unsigned EltBitSize = MVT::getSizeInBits(BV->getOperand(0).getValueType());
2801 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
2802 SDOperand OpVal = BV->getOperand(i);
2803
2804 unsigned PartNo = i >= e/2; // In the upper 128 bits?
Chris Lattnerb17f1672006-04-16 01:01:29 +00002805 unsigned SlotNo = e/2 - (i & (e/2-1))-1; // Which subpiece of the uint64_t.
Chris Lattnerac225ca2006-04-12 19:07:14 +00002806
2807 uint64_t EltBits = 0;
2808 if (OpVal.getOpcode() == ISD::UNDEF) {
2809 uint64_t EltUndefBits = ~0U >> (32-EltBitSize);
2810 UndefBits[PartNo] |= EltUndefBits << (SlotNo*EltBitSize);
2811 continue;
2812 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
2813 EltBits = CN->getValue() & (~0U >> (32-EltBitSize));
2814 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) {
2815 assert(CN->getValueType(0) == MVT::f32 &&
2816 "Only one legal FP vector type!");
Dale Johanneseneaf08942007-08-31 04:03:46 +00002817 EltBits = FloatToBits(CN->getValueAPF().convertToFloat());
Chris Lattnerac225ca2006-04-12 19:07:14 +00002818 } else {
2819 // Nonconstant element.
2820 return true;
2821 }
2822
2823 VectorBits[PartNo] |= EltBits << (SlotNo*EltBitSize);
2824 }
2825
2826 //printf("%llx %llx %llx %llx\n",
2827 // VectorBits[0], VectorBits[1], UndefBits[0], UndefBits[1]);
2828 return false;
2829}
Chris Lattneref819f82006-03-20 06:33:01 +00002830
Chris Lattnerb17f1672006-04-16 01:01:29 +00002831// If this is a splat (repetition) of a value across the whole vector, return
2832// the smallest size that splats it. For example, "0x01010101010101..." is a
2833// splat of 0x01, 0x0101, and 0x01010101. We return SplatBits = 0x01 and
2834// SplatSize = 1 byte.
2835static bool isConstantSplat(const uint64_t Bits128[2],
2836 const uint64_t Undef128[2],
2837 unsigned &SplatBits, unsigned &SplatUndef,
2838 unsigned &SplatSize) {
2839
2840 // Don't let undefs prevent splats from matching. See if the top 64-bits are
2841 // the same as the lower 64-bits, ignoring undefs.
2842 if ((Bits128[0] & ~Undef128[1]) != (Bits128[1] & ~Undef128[0]))
2843 return false; // Can't be a splat if two pieces don't match.
2844
2845 uint64_t Bits64 = Bits128[0] | Bits128[1];
2846 uint64_t Undef64 = Undef128[0] & Undef128[1];
2847
2848 // Check that the top 32-bits are the same as the lower 32-bits, ignoring
2849 // undefs.
2850 if ((Bits64 & (~Undef64 >> 32)) != ((Bits64 >> 32) & ~Undef64))
2851 return false; // Can't be a splat if two pieces don't match.
2852
2853 uint32_t Bits32 = uint32_t(Bits64) | uint32_t(Bits64 >> 32);
2854 uint32_t Undef32 = uint32_t(Undef64) & uint32_t(Undef64 >> 32);
2855
2856 // If the top 16-bits are different than the lower 16-bits, ignoring
2857 // undefs, we have an i32 splat.
2858 if ((Bits32 & (~Undef32 >> 16)) != ((Bits32 >> 16) & ~Undef32)) {
2859 SplatBits = Bits32;
2860 SplatUndef = Undef32;
2861 SplatSize = 4;
2862 return true;
2863 }
2864
2865 uint16_t Bits16 = uint16_t(Bits32) | uint16_t(Bits32 >> 16);
2866 uint16_t Undef16 = uint16_t(Undef32) & uint16_t(Undef32 >> 16);
2867
2868 // If the top 8-bits are different than the lower 8-bits, ignoring
2869 // undefs, we have an i16 splat.
2870 if ((Bits16 & (uint16_t(~Undef16) >> 8)) != ((Bits16 >> 8) & ~Undef16)) {
2871 SplatBits = Bits16;
2872 SplatUndef = Undef16;
2873 SplatSize = 2;
2874 return true;
2875 }
2876
2877 // Otherwise, we have an 8-bit splat.
2878 SplatBits = uint8_t(Bits16) | uint8_t(Bits16 >> 8);
2879 SplatUndef = uint8_t(Undef16) & uint8_t(Undef16 >> 8);
2880 SplatSize = 1;
2881 return true;
2882}
2883
Chris Lattner4a998b92006-04-17 06:00:21 +00002884/// BuildSplatI - Build a canonical splati of Val with an element size of
2885/// SplatSize. Cast the result to VT.
2886static SDOperand BuildSplatI(int Val, unsigned SplatSize, MVT::ValueType VT,
2887 SelectionDAG &DAG) {
2888 assert(Val >= -16 && Val <= 15 && "vsplti is out of range!");
Chris Lattner70fa4932006-12-01 01:45:39 +00002889
Chris Lattner4a998b92006-04-17 06:00:21 +00002890 static const MVT::ValueType VTys[] = { // canonical VT to use for each size.
2891 MVT::v16i8, MVT::v8i16, MVT::Other, MVT::v4i32
2892 };
Chris Lattner70fa4932006-12-01 01:45:39 +00002893
2894 MVT::ValueType ReqVT = VT != MVT::Other ? VT : VTys[SplatSize-1];
2895
2896 // Force vspltis[hw] -1 to vspltisb -1 to canonicalize.
2897 if (Val == -1)
2898 SplatSize = 1;
2899
Chris Lattner4a998b92006-04-17 06:00:21 +00002900 MVT::ValueType CanonicalVT = VTys[SplatSize-1];
2901
2902 // Build a canonical splat for this value.
Dan Gohman51eaa862007-06-14 22:58:02 +00002903 SDOperand Elt = DAG.getConstant(Val, MVT::getVectorElementType(CanonicalVT));
Chris Lattnere2199452006-08-11 17:38:39 +00002904 SmallVector<SDOperand, 8> Ops;
2905 Ops.assign(MVT::getVectorNumElements(CanonicalVT), Elt);
2906 SDOperand Res = DAG.getNode(ISD::BUILD_VECTOR, CanonicalVT,
2907 &Ops[0], Ops.size());
Chris Lattner70fa4932006-12-01 01:45:39 +00002908 return DAG.getNode(ISD::BIT_CONVERT, ReqVT, Res);
Chris Lattner4a998b92006-04-17 06:00:21 +00002909}
2910
Chris Lattnere7c768e2006-04-18 03:24:30 +00002911/// BuildIntrinsicOp - Return a binary operator intrinsic node with the
Chris Lattner6876e662006-04-17 06:58:41 +00002912/// specified intrinsic ID.
Chris Lattnere7c768e2006-04-18 03:24:30 +00002913static SDOperand BuildIntrinsicOp(unsigned IID, SDOperand LHS, SDOperand RHS,
2914 SelectionDAG &DAG,
2915 MVT::ValueType DestVT = MVT::Other) {
2916 if (DestVT == MVT::Other) DestVT = LHS.getValueType();
2917 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DestVT,
Chris Lattner6876e662006-04-17 06:58:41 +00002918 DAG.getConstant(IID, MVT::i32), LHS, RHS);
2919}
2920
Chris Lattnere7c768e2006-04-18 03:24:30 +00002921/// BuildIntrinsicOp - Return a ternary operator intrinsic node with the
2922/// specified intrinsic ID.
2923static SDOperand BuildIntrinsicOp(unsigned IID, SDOperand Op0, SDOperand Op1,
2924 SDOperand Op2, SelectionDAG &DAG,
2925 MVT::ValueType DestVT = MVT::Other) {
2926 if (DestVT == MVT::Other) DestVT = Op0.getValueType();
2927 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DestVT,
2928 DAG.getConstant(IID, MVT::i32), Op0, Op1, Op2);
2929}
2930
2931
Chris Lattnerbdd558c2006-04-17 17:55:10 +00002932/// BuildVSLDOI - Return a VECTOR_SHUFFLE that is a vsldoi of the specified
2933/// amount. The result has the specified value type.
2934static SDOperand BuildVSLDOI(SDOperand LHS, SDOperand RHS, unsigned Amt,
2935 MVT::ValueType VT, SelectionDAG &DAG) {
2936 // Force LHS/RHS to be the right type.
2937 LHS = DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, LHS);
2938 RHS = DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, RHS);
2939
Chris Lattnere2199452006-08-11 17:38:39 +00002940 SDOperand Ops[16];
Chris Lattnerbdd558c2006-04-17 17:55:10 +00002941 for (unsigned i = 0; i != 16; ++i)
Chris Lattnere2199452006-08-11 17:38:39 +00002942 Ops[i] = DAG.getConstant(i+Amt, MVT::i32);
Chris Lattnerbdd558c2006-04-17 17:55:10 +00002943 SDOperand T = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v16i8, LHS, RHS,
Chris Lattnere2199452006-08-11 17:38:39 +00002944 DAG.getNode(ISD::BUILD_VECTOR, MVT::v16i8, Ops,16));
Chris Lattnerbdd558c2006-04-17 17:55:10 +00002945 return DAG.getNode(ISD::BIT_CONVERT, VT, T);
2946}
2947
Chris Lattnerf1b47082006-04-14 05:19:18 +00002948// If this is a case we can't handle, return null and let the default
2949// expansion code take care of it. If we CAN select this case, and if it
2950// selects to a single instruction, return Op. Otherwise, if we can codegen
2951// this case more efficiently than a constant pool load, lower it to the
2952// sequence of ops that should be used.
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002953SDOperand PPCTargetLowering::LowerBUILD_VECTOR(SDOperand Op,
2954 SelectionDAG &DAG) {
Chris Lattnerf1b47082006-04-14 05:19:18 +00002955 // If this is a vector of constants or undefs, get the bits. A bit in
2956 // UndefBits is set if the corresponding element of the vector is an
2957 // ISD::UNDEF value. For undefs, the corresponding VectorBits values are
2958 // zero.
2959 uint64_t VectorBits[2];
2960 uint64_t UndefBits[2];
2961 if (GetConstantBuildVectorBits(Op.Val, VectorBits, UndefBits))
2962 return SDOperand(); // Not a constant vector.
2963
Chris Lattnerb17f1672006-04-16 01:01:29 +00002964 // If this is a splat (repetition) of a value across the whole vector, return
2965 // the smallest size that splats it. For example, "0x01010101010101..." is a
2966 // splat of 0x01, 0x0101, and 0x01010101. We return SplatBits = 0x01 and
2967 // SplatSize = 1 byte.
2968 unsigned SplatBits, SplatUndef, SplatSize;
2969 if (isConstantSplat(VectorBits, UndefBits, SplatBits, SplatUndef, SplatSize)){
2970 bool HasAnyUndefs = (UndefBits[0] | UndefBits[1]) != 0;
2971
2972 // First, handle single instruction cases.
2973
2974 // All zeros?
2975 if (SplatBits == 0) {
2976 // Canonicalize all zero vectors to be v4i32.
2977 if (Op.getValueType() != MVT::v4i32 || HasAnyUndefs) {
2978 SDOperand Z = DAG.getConstant(0, MVT::i32);
2979 Z = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Z, Z, Z, Z);
2980 Op = DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Z);
2981 }
2982 return Op;
Chris Lattnerf1b47082006-04-14 05:19:18 +00002983 }
Chris Lattnerb17f1672006-04-16 01:01:29 +00002984
2985 // If the sign extended value is in the range [-16,15], use VSPLTI[bhw].
2986 int32_t SextVal= int32_t(SplatBits << (32-8*SplatSize)) >> (32-8*SplatSize);
Chris Lattner4a998b92006-04-17 06:00:21 +00002987 if (SextVal >= -16 && SextVal <= 15)
2988 return BuildSplatI(SextVal, SplatSize, Op.getValueType(), DAG);
Chris Lattnerb17f1672006-04-16 01:01:29 +00002989
Chris Lattnerdbce85d2006-04-17 18:09:22 +00002990
2991 // Two instruction sequences.
2992
Chris Lattner4a998b92006-04-17 06:00:21 +00002993 // If this value is in the range [-32,30] and is even, use:
2994 // tmp = VSPLTI[bhw], result = add tmp, tmp
2995 if (SextVal >= -32 && SextVal <= 30 && (SextVal & 1) == 0) {
2996 Op = BuildSplatI(SextVal >> 1, SplatSize, Op.getValueType(), DAG);
2997 return DAG.getNode(ISD::ADD, Op.getValueType(), Op, Op);
2998 }
Chris Lattner6876e662006-04-17 06:58:41 +00002999
3000 // If this is 0x8000_0000 x 4, turn into vspltisw + vslw. If it is
3001 // 0x7FFF_FFFF x 4, turn it into not(0x8000_0000). This is important
3002 // for fneg/fabs.
3003 if (SplatSize == 4 && SplatBits == (0x7FFFFFFF&~SplatUndef)) {
3004 // Make -1 and vspltisw -1:
3005 SDOperand OnesV = BuildSplatI(-1, 4, MVT::v4i32, DAG);
3006
3007 // Make the VSLW intrinsic, computing 0x8000_0000.
Chris Lattnere7c768e2006-04-18 03:24:30 +00003008 SDOperand Res = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, OnesV,
3009 OnesV, DAG);
Chris Lattner6876e662006-04-17 06:58:41 +00003010
3011 // xor by OnesV to invert it.
3012 Res = DAG.getNode(ISD::XOR, MVT::v4i32, Res, OnesV);
3013 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Res);
3014 }
3015
3016 // Check to see if this is a wide variety of vsplti*, binop self cases.
3017 unsigned SplatBitSize = SplatSize*8;
Lauro Ramos Venancio1baa1972007-03-27 16:33:08 +00003018 static const signed char SplatCsts[] = {
Chris Lattner6876e662006-04-17 06:58:41 +00003019 -1, 1, -2, 2, -3, 3, -4, 4, -5, 5, -6, 6, -7, 7,
Chris Lattnerdbce85d2006-04-17 18:09:22 +00003020 -8, 8, -9, 9, -10, 10, -11, 11, -12, 12, -13, 13, 14, -14, 15, -15, -16
Chris Lattner6876e662006-04-17 06:58:41 +00003021 };
Chris Lattner15eb3292006-11-29 19:58:49 +00003022
Owen Anderson718cb662007-09-07 04:06:50 +00003023 for (unsigned idx = 0; idx < array_lengthof(SplatCsts); ++idx) {
Chris Lattner6876e662006-04-17 06:58:41 +00003024 // Indirect through the SplatCsts array so that we favor 'vsplti -1' for
3025 // cases which are ambiguous (e.g. formation of 0x8000_0000). 'vsplti -1'
3026 int i = SplatCsts[idx];
3027
3028 // Figure out what shift amount will be used by altivec if shifted by i in
3029 // this splat size.
3030 unsigned TypeShiftAmt = i & (SplatBitSize-1);
3031
3032 // vsplti + shl self.
3033 if (SextVal == (i << (int)TypeShiftAmt)) {
Chris Lattner15eb3292006-11-29 19:58:49 +00003034 SDOperand Res = BuildSplatI(i, SplatSize, MVT::Other, DAG);
Chris Lattner6876e662006-04-17 06:58:41 +00003035 static const unsigned IIDs[] = { // Intrinsic to use for each size.
3036 Intrinsic::ppc_altivec_vslb, Intrinsic::ppc_altivec_vslh, 0,
3037 Intrinsic::ppc_altivec_vslw
3038 };
Chris Lattner15eb3292006-11-29 19:58:49 +00003039 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG);
3040 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Res);
Chris Lattner6876e662006-04-17 06:58:41 +00003041 }
3042
3043 // vsplti + srl self.
3044 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
Chris Lattner15eb3292006-11-29 19:58:49 +00003045 SDOperand Res = BuildSplatI(i, SplatSize, MVT::Other, DAG);
Chris Lattner6876e662006-04-17 06:58:41 +00003046 static const unsigned IIDs[] = { // Intrinsic to use for each size.
3047 Intrinsic::ppc_altivec_vsrb, Intrinsic::ppc_altivec_vsrh, 0,
3048 Intrinsic::ppc_altivec_vsrw
3049 };
Chris Lattner15eb3292006-11-29 19:58:49 +00003050 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG);
3051 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Res);
Chris Lattner6876e662006-04-17 06:58:41 +00003052 }
3053
3054 // vsplti + sra self.
3055 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
Chris Lattner15eb3292006-11-29 19:58:49 +00003056 SDOperand Res = BuildSplatI(i, SplatSize, MVT::Other, DAG);
Chris Lattner6876e662006-04-17 06:58:41 +00003057 static const unsigned IIDs[] = { // Intrinsic to use for each size.
3058 Intrinsic::ppc_altivec_vsrab, Intrinsic::ppc_altivec_vsrah, 0,
3059 Intrinsic::ppc_altivec_vsraw
3060 };
Chris Lattner15eb3292006-11-29 19:58:49 +00003061 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG);
3062 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Res);
Chris Lattner6876e662006-04-17 06:58:41 +00003063 }
3064
Chris Lattnerbdd558c2006-04-17 17:55:10 +00003065 // vsplti + rol self.
3066 if (SextVal == (int)(((unsigned)i << TypeShiftAmt) |
3067 ((unsigned)i >> (SplatBitSize-TypeShiftAmt)))) {
Chris Lattner15eb3292006-11-29 19:58:49 +00003068 SDOperand Res = BuildSplatI(i, SplatSize, MVT::Other, DAG);
Chris Lattnerbdd558c2006-04-17 17:55:10 +00003069 static const unsigned IIDs[] = { // Intrinsic to use for each size.
3070 Intrinsic::ppc_altivec_vrlb, Intrinsic::ppc_altivec_vrlh, 0,
3071 Intrinsic::ppc_altivec_vrlw
3072 };
Chris Lattner15eb3292006-11-29 19:58:49 +00003073 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG);
3074 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Res);
Chris Lattnerbdd558c2006-04-17 17:55:10 +00003075 }
3076
3077 // t = vsplti c, result = vsldoi t, t, 1
3078 if (SextVal == ((i << 8) | (i >> (TypeShiftAmt-8)))) {
3079 SDOperand T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG);
3080 return BuildVSLDOI(T, T, 1, Op.getValueType(), DAG);
3081 }
3082 // t = vsplti c, result = vsldoi t, t, 2
3083 if (SextVal == ((i << 16) | (i >> (TypeShiftAmt-16)))) {
3084 SDOperand T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG);
3085 return BuildVSLDOI(T, T, 2, Op.getValueType(), DAG);
3086 }
3087 // t = vsplti c, result = vsldoi t, t, 3
3088 if (SextVal == ((i << 24) | (i >> (TypeShiftAmt-24)))) {
3089 SDOperand T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG);
3090 return BuildVSLDOI(T, T, 3, Op.getValueType(), DAG);
3091 }
Chris Lattner6876e662006-04-17 06:58:41 +00003092 }
3093
Chris Lattner6876e662006-04-17 06:58:41 +00003094 // Three instruction sequences.
3095
Chris Lattnerdbce85d2006-04-17 18:09:22 +00003096 // Odd, in range [17,31]: (vsplti C)-(vsplti -16).
3097 if (SextVal >= 0 && SextVal <= 31) {
Chris Lattner15eb3292006-11-29 19:58:49 +00003098 SDOperand LHS = BuildSplatI(SextVal-16, SplatSize, MVT::Other, DAG);
3099 SDOperand RHS = BuildSplatI(-16, SplatSize, MVT::Other, DAG);
Dale Johannesen296c1762007-10-14 01:58:32 +00003100 LHS = DAG.getNode(ISD::SUB, LHS.getValueType(), LHS, RHS);
Chris Lattner15eb3292006-11-29 19:58:49 +00003101 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), LHS);
Chris Lattnerdbce85d2006-04-17 18:09:22 +00003102 }
3103 // Odd, in range [-31,-17]: (vsplti C)+(vsplti -16).
3104 if (SextVal >= -31 && SextVal <= 0) {
Chris Lattner15eb3292006-11-29 19:58:49 +00003105 SDOperand LHS = BuildSplatI(SextVal+16, SplatSize, MVT::Other, DAG);
3106 SDOperand RHS = BuildSplatI(-16, SplatSize, MVT::Other, DAG);
Dale Johannesen296c1762007-10-14 01:58:32 +00003107 LHS = DAG.getNode(ISD::ADD, LHS.getValueType(), LHS, RHS);
Chris Lattner15eb3292006-11-29 19:58:49 +00003108 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), LHS);
Chris Lattnerf1b47082006-04-14 05:19:18 +00003109 }
3110 }
Chris Lattnerb17f1672006-04-16 01:01:29 +00003111
Chris Lattnerf1b47082006-04-14 05:19:18 +00003112 return SDOperand();
3113}
3114
Chris Lattner59138102006-04-17 05:28:54 +00003115/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
3116/// the specified operations to build the shuffle.
3117static SDOperand GeneratePerfectShuffle(unsigned PFEntry, SDOperand LHS,
3118 SDOperand RHS, SelectionDAG &DAG) {
3119 unsigned OpNum = (PFEntry >> 26) & 0x0F;
3120 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
3121 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
3122
3123 enum {
Chris Lattner00402c72006-05-16 04:20:24 +00003124 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 +00003125 OP_VMRGHW,
3126 OP_VMRGLW,
3127 OP_VSPLTISW0,
3128 OP_VSPLTISW1,
3129 OP_VSPLTISW2,
3130 OP_VSPLTISW3,
3131 OP_VSLDOI4,
3132 OP_VSLDOI8,
Chris Lattnerd74ea2b2006-05-24 17:04:05 +00003133 OP_VSLDOI12
Chris Lattner59138102006-04-17 05:28:54 +00003134 };
3135
3136 if (OpNum == OP_COPY) {
3137 if (LHSID == (1*9+2)*9+3) return LHS;
3138 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
3139 return RHS;
3140 }
3141
Chris Lattnerbdd558c2006-04-17 17:55:10 +00003142 SDOperand OpLHS, OpRHS;
3143 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG);
3144 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG);
3145
Chris Lattner59138102006-04-17 05:28:54 +00003146 unsigned ShufIdxs[16];
3147 switch (OpNum) {
3148 default: assert(0 && "Unknown i32 permute!");
3149 case OP_VMRGHW:
3150 ShufIdxs[ 0] = 0; ShufIdxs[ 1] = 1; ShufIdxs[ 2] = 2; ShufIdxs[ 3] = 3;
3151 ShufIdxs[ 4] = 16; ShufIdxs[ 5] = 17; ShufIdxs[ 6] = 18; ShufIdxs[ 7] = 19;
3152 ShufIdxs[ 8] = 4; ShufIdxs[ 9] = 5; ShufIdxs[10] = 6; ShufIdxs[11] = 7;
3153 ShufIdxs[12] = 20; ShufIdxs[13] = 21; ShufIdxs[14] = 22; ShufIdxs[15] = 23;
3154 break;
3155 case OP_VMRGLW:
3156 ShufIdxs[ 0] = 8; ShufIdxs[ 1] = 9; ShufIdxs[ 2] = 10; ShufIdxs[ 3] = 11;
3157 ShufIdxs[ 4] = 24; ShufIdxs[ 5] = 25; ShufIdxs[ 6] = 26; ShufIdxs[ 7] = 27;
3158 ShufIdxs[ 8] = 12; ShufIdxs[ 9] = 13; ShufIdxs[10] = 14; ShufIdxs[11] = 15;
3159 ShufIdxs[12] = 28; ShufIdxs[13] = 29; ShufIdxs[14] = 30; ShufIdxs[15] = 31;
3160 break;
3161 case OP_VSPLTISW0:
3162 for (unsigned i = 0; i != 16; ++i)
3163 ShufIdxs[i] = (i&3)+0;
3164 break;
3165 case OP_VSPLTISW1:
3166 for (unsigned i = 0; i != 16; ++i)
3167 ShufIdxs[i] = (i&3)+4;
3168 break;
3169 case OP_VSPLTISW2:
3170 for (unsigned i = 0; i != 16; ++i)
3171 ShufIdxs[i] = (i&3)+8;
3172 break;
3173 case OP_VSPLTISW3:
3174 for (unsigned i = 0; i != 16; ++i)
3175 ShufIdxs[i] = (i&3)+12;
3176 break;
3177 case OP_VSLDOI4:
Chris Lattnerbdd558c2006-04-17 17:55:10 +00003178 return BuildVSLDOI(OpLHS, OpRHS, 4, OpLHS.getValueType(), DAG);
Chris Lattner59138102006-04-17 05:28:54 +00003179 case OP_VSLDOI8:
Chris Lattnerbdd558c2006-04-17 17:55:10 +00003180 return BuildVSLDOI(OpLHS, OpRHS, 8, OpLHS.getValueType(), DAG);
Chris Lattner59138102006-04-17 05:28:54 +00003181 case OP_VSLDOI12:
Chris Lattnerbdd558c2006-04-17 17:55:10 +00003182 return BuildVSLDOI(OpLHS, OpRHS, 12, OpLHS.getValueType(), DAG);
Chris Lattner59138102006-04-17 05:28:54 +00003183 }
Chris Lattnere2199452006-08-11 17:38:39 +00003184 SDOperand Ops[16];
Chris Lattner59138102006-04-17 05:28:54 +00003185 for (unsigned i = 0; i != 16; ++i)
Chris Lattnere2199452006-08-11 17:38:39 +00003186 Ops[i] = DAG.getConstant(ShufIdxs[i], MVT::i32);
Chris Lattner59138102006-04-17 05:28:54 +00003187
3188 return DAG.getNode(ISD::VECTOR_SHUFFLE, OpLHS.getValueType(), OpLHS, OpRHS,
Chris Lattnere2199452006-08-11 17:38:39 +00003189 DAG.getNode(ISD::BUILD_VECTOR, MVT::v16i8, Ops, 16));
Chris Lattner59138102006-04-17 05:28:54 +00003190}
3191
Chris Lattnerf1b47082006-04-14 05:19:18 +00003192/// LowerVECTOR_SHUFFLE - Return the code we lower for VECTOR_SHUFFLE. If this
3193/// is a shuffle we can handle in a single instruction, return it. Otherwise,
3194/// return the code it can be lowered into. Worst case, it can always be
3195/// lowered into a vperm.
Dale Johannesen5b3b6952008-03-04 23:17:14 +00003196SDOperand PPCTargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op,
3197 SelectionDAG &DAG) {
Chris Lattnerf1b47082006-04-14 05:19:18 +00003198 SDOperand V1 = Op.getOperand(0);
3199 SDOperand V2 = Op.getOperand(1);
3200 SDOperand PermMask = Op.getOperand(2);
3201
3202 // Cases that are handled by instructions that take permute immediates
3203 // (such as vsplt*) should be left as VECTOR_SHUFFLE nodes so they can be
3204 // selected by the instruction selector.
3205 if (V2.getOpcode() == ISD::UNDEF) {
3206 if (PPC::isSplatShuffleMask(PermMask.Val, 1) ||
3207 PPC::isSplatShuffleMask(PermMask.Val, 2) ||
3208 PPC::isSplatShuffleMask(PermMask.Val, 4) ||
3209 PPC::isVPKUWUMShuffleMask(PermMask.Val, true) ||
3210 PPC::isVPKUHUMShuffleMask(PermMask.Val, true) ||
3211 PPC::isVSLDOIShuffleMask(PermMask.Val, true) != -1 ||
3212 PPC::isVMRGLShuffleMask(PermMask.Val, 1, true) ||
3213 PPC::isVMRGLShuffleMask(PermMask.Val, 2, true) ||
3214 PPC::isVMRGLShuffleMask(PermMask.Val, 4, true) ||
3215 PPC::isVMRGHShuffleMask(PermMask.Val, 1, true) ||
3216 PPC::isVMRGHShuffleMask(PermMask.Val, 2, true) ||
3217 PPC::isVMRGHShuffleMask(PermMask.Val, 4, true)) {
3218 return Op;
3219 }
3220 }
3221
3222 // Altivec has a variety of "shuffle immediates" that take two vector inputs
3223 // and produce a fixed permutation. If any of these match, do not lower to
3224 // VPERM.
3225 if (PPC::isVPKUWUMShuffleMask(PermMask.Val, false) ||
3226 PPC::isVPKUHUMShuffleMask(PermMask.Val, false) ||
3227 PPC::isVSLDOIShuffleMask(PermMask.Val, false) != -1 ||
3228 PPC::isVMRGLShuffleMask(PermMask.Val, 1, false) ||
3229 PPC::isVMRGLShuffleMask(PermMask.Val, 2, false) ||
3230 PPC::isVMRGLShuffleMask(PermMask.Val, 4, false) ||
3231 PPC::isVMRGHShuffleMask(PermMask.Val, 1, false) ||
3232 PPC::isVMRGHShuffleMask(PermMask.Val, 2, false) ||
3233 PPC::isVMRGHShuffleMask(PermMask.Val, 4, false))
3234 return Op;
3235
Chris Lattner59138102006-04-17 05:28:54 +00003236 // Check to see if this is a shuffle of 4-byte values. If so, we can use our
3237 // perfect shuffle table to emit an optimal matching sequence.
3238 unsigned PFIndexes[4];
3239 bool isFourElementShuffle = true;
3240 for (unsigned i = 0; i != 4 && isFourElementShuffle; ++i) { // Element number
3241 unsigned EltNo = 8; // Start out undef.
3242 for (unsigned j = 0; j != 4; ++j) { // Intra-element byte.
3243 if (PermMask.getOperand(i*4+j).getOpcode() == ISD::UNDEF)
3244 continue; // Undef, ignore it.
3245
3246 unsigned ByteSource =
3247 cast<ConstantSDNode>(PermMask.getOperand(i*4+j))->getValue();
3248 if ((ByteSource & 3) != j) {
3249 isFourElementShuffle = false;
3250 break;
3251 }
3252
3253 if (EltNo == 8) {
3254 EltNo = ByteSource/4;
3255 } else if (EltNo != ByteSource/4) {
3256 isFourElementShuffle = false;
3257 break;
3258 }
3259 }
3260 PFIndexes[i] = EltNo;
3261 }
3262
3263 // If this shuffle can be expressed as a shuffle of 4-byte elements, use the
3264 // perfect shuffle vector to determine if it is cost effective to do this as
3265 // discrete instructions, or whether we should use a vperm.
3266 if (isFourElementShuffle) {
3267 // Compute the index in the perfect shuffle table.
3268 unsigned PFTableIndex =
3269 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
3270
3271 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
3272 unsigned Cost = (PFEntry >> 30);
3273
3274 // Determining when to avoid vperm is tricky. Many things affect the cost
3275 // of vperm, particularly how many times the perm mask needs to be computed.
3276 // For example, if the perm mask can be hoisted out of a loop or is already
3277 // used (perhaps because there are multiple permutes with the same shuffle
3278 // mask?) the vperm has a cost of 1. OTOH, hoisting the permute mask out of
3279 // the loop requires an extra register.
3280 //
3281 // As a compromise, we only emit discrete instructions if the shuffle can be
3282 // generated in 3 or fewer operations. When we have loop information
3283 // available, if this block is within a loop, we should avoid using vperm
3284 // for 3-operation perms and use a constant pool load instead.
3285 if (Cost < 3)
3286 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG);
3287 }
Chris Lattnerf1b47082006-04-14 05:19:18 +00003288
3289 // Lower this to a VPERM(V1, V2, V3) expression, where V3 is a constant
3290 // vector that will get spilled to the constant pool.
3291 if (V2.getOpcode() == ISD::UNDEF) V2 = V1;
3292
3293 // The SHUFFLE_VECTOR mask is almost exactly what we want for vperm, except
3294 // that it is in input element units, not in bytes. Convert now.
Dan Gohman51eaa862007-06-14 22:58:02 +00003295 MVT::ValueType EltVT = MVT::getVectorElementType(V1.getValueType());
Chris Lattnerf1b47082006-04-14 05:19:18 +00003296 unsigned BytesPerElement = MVT::getSizeInBits(EltVT)/8;
3297
Chris Lattnere2199452006-08-11 17:38:39 +00003298 SmallVector<SDOperand, 16> ResultMask;
Chris Lattnerf1b47082006-04-14 05:19:18 +00003299 for (unsigned i = 0, e = PermMask.getNumOperands(); i != e; ++i) {
Chris Lattner730b4562006-04-15 23:48:05 +00003300 unsigned SrcElt;
3301 if (PermMask.getOperand(i).getOpcode() == ISD::UNDEF)
3302 SrcElt = 0;
3303 else
3304 SrcElt = cast<ConstantSDNode>(PermMask.getOperand(i))->getValue();
Chris Lattnerf1b47082006-04-14 05:19:18 +00003305
3306 for (unsigned j = 0; j != BytesPerElement; ++j)
3307 ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement+j,
3308 MVT::i8));
3309 }
3310
Chris Lattnere2199452006-08-11 17:38:39 +00003311 SDOperand VPermMask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v16i8,
3312 &ResultMask[0], ResultMask.size());
Chris Lattnerf1b47082006-04-14 05:19:18 +00003313 return DAG.getNode(PPCISD::VPERM, V1.getValueType(), V1, V2, VPermMask);
3314}
3315
Chris Lattner90564f22006-04-18 17:59:36 +00003316/// getAltivecCompareInfo - Given an intrinsic, return false if it is not an
3317/// altivec comparison. If it is, return true and fill in Opc/isDot with
3318/// information about the intrinsic.
3319static bool getAltivecCompareInfo(SDOperand Intrin, int &CompareOpc,
3320 bool &isDot) {
3321 unsigned IntrinsicID = cast<ConstantSDNode>(Intrin.getOperand(0))->getValue();
3322 CompareOpc = -1;
3323 isDot = false;
3324 switch (IntrinsicID) {
3325 default: return false;
3326 // Comparison predicates.
Chris Lattner1a635d62006-04-14 06:01:58 +00003327 case Intrinsic::ppc_altivec_vcmpbfp_p: CompareOpc = 966; isDot = 1; break;
3328 case Intrinsic::ppc_altivec_vcmpeqfp_p: CompareOpc = 198; isDot = 1; break;
3329 case Intrinsic::ppc_altivec_vcmpequb_p: CompareOpc = 6; isDot = 1; break;
3330 case Intrinsic::ppc_altivec_vcmpequh_p: CompareOpc = 70; isDot = 1; break;
3331 case Intrinsic::ppc_altivec_vcmpequw_p: CompareOpc = 134; isDot = 1; break;
3332 case Intrinsic::ppc_altivec_vcmpgefp_p: CompareOpc = 454; isDot = 1; break;
3333 case Intrinsic::ppc_altivec_vcmpgtfp_p: CompareOpc = 710; isDot = 1; break;
3334 case Intrinsic::ppc_altivec_vcmpgtsb_p: CompareOpc = 774; isDot = 1; break;
3335 case Intrinsic::ppc_altivec_vcmpgtsh_p: CompareOpc = 838; isDot = 1; break;
3336 case Intrinsic::ppc_altivec_vcmpgtsw_p: CompareOpc = 902; isDot = 1; break;
3337 case Intrinsic::ppc_altivec_vcmpgtub_p: CompareOpc = 518; isDot = 1; break;
3338 case Intrinsic::ppc_altivec_vcmpgtuh_p: CompareOpc = 582; isDot = 1; break;
3339 case Intrinsic::ppc_altivec_vcmpgtuw_p: CompareOpc = 646; isDot = 1; break;
3340
3341 // Normal Comparisons.
3342 case Intrinsic::ppc_altivec_vcmpbfp: CompareOpc = 966; isDot = 0; break;
3343 case Intrinsic::ppc_altivec_vcmpeqfp: CompareOpc = 198; isDot = 0; break;
3344 case Intrinsic::ppc_altivec_vcmpequb: CompareOpc = 6; isDot = 0; break;
3345 case Intrinsic::ppc_altivec_vcmpequh: CompareOpc = 70; isDot = 0; break;
3346 case Intrinsic::ppc_altivec_vcmpequw: CompareOpc = 134; isDot = 0; break;
3347 case Intrinsic::ppc_altivec_vcmpgefp: CompareOpc = 454; isDot = 0; break;
3348 case Intrinsic::ppc_altivec_vcmpgtfp: CompareOpc = 710; isDot = 0; break;
3349 case Intrinsic::ppc_altivec_vcmpgtsb: CompareOpc = 774; isDot = 0; break;
3350 case Intrinsic::ppc_altivec_vcmpgtsh: CompareOpc = 838; isDot = 0; break;
3351 case Intrinsic::ppc_altivec_vcmpgtsw: CompareOpc = 902; isDot = 0; break;
3352 case Intrinsic::ppc_altivec_vcmpgtub: CompareOpc = 518; isDot = 0; break;
3353 case Intrinsic::ppc_altivec_vcmpgtuh: CompareOpc = 582; isDot = 0; break;
3354 case Intrinsic::ppc_altivec_vcmpgtuw: CompareOpc = 646; isDot = 0; break;
3355 }
Chris Lattner90564f22006-04-18 17:59:36 +00003356 return true;
3357}
3358
3359/// LowerINTRINSIC_WO_CHAIN - If this is an intrinsic that we want to custom
3360/// lower, do it, otherwise return null.
Dale Johannesen5b3b6952008-03-04 23:17:14 +00003361SDOperand PPCTargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op,
3362 SelectionDAG &DAG) {
Chris Lattner90564f22006-04-18 17:59:36 +00003363 // If this is a lowered altivec predicate compare, CompareOpc is set to the
3364 // opcode number of the comparison.
3365 int CompareOpc;
3366 bool isDot;
3367 if (!getAltivecCompareInfo(Op, CompareOpc, isDot))
3368 return SDOperand(); // Don't custom lower most intrinsics.
Chris Lattner1a635d62006-04-14 06:01:58 +00003369
Chris Lattner90564f22006-04-18 17:59:36 +00003370 // If this is a non-dot comparison, make the VCMP node and we are done.
Chris Lattner1a635d62006-04-14 06:01:58 +00003371 if (!isDot) {
3372 SDOperand Tmp = DAG.getNode(PPCISD::VCMP, Op.getOperand(2).getValueType(),
3373 Op.getOperand(1), Op.getOperand(2),
3374 DAG.getConstant(CompareOpc, MVT::i32));
3375 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Tmp);
3376 }
3377
3378 // Create the PPCISD altivec 'dot' comparison node.
Chris Lattner79e490a2006-08-11 17:18:05 +00003379 SDOperand Ops[] = {
3380 Op.getOperand(2), // LHS
3381 Op.getOperand(3), // RHS
3382 DAG.getConstant(CompareOpc, MVT::i32)
3383 };
Chris Lattner1a635d62006-04-14 06:01:58 +00003384 std::vector<MVT::ValueType> VTs;
Chris Lattner1a635d62006-04-14 06:01:58 +00003385 VTs.push_back(Op.getOperand(2).getValueType());
3386 VTs.push_back(MVT::Flag);
Chris Lattner79e490a2006-08-11 17:18:05 +00003387 SDOperand CompNode = DAG.getNode(PPCISD::VCMPo, VTs, Ops, 3);
Chris Lattner1a635d62006-04-14 06:01:58 +00003388
3389 // Now that we have the comparison, emit a copy from the CR to a GPR.
3390 // This is flagged to the above dot comparison.
3391 SDOperand Flags = DAG.getNode(PPCISD::MFCR, MVT::i32,
3392 DAG.getRegister(PPC::CR6, MVT::i32),
3393 CompNode.getValue(1));
3394
3395 // Unpack the result based on how the target uses it.
3396 unsigned BitNo; // Bit # of CR6.
3397 bool InvertBit; // Invert result?
3398 switch (cast<ConstantSDNode>(Op.getOperand(1))->getValue()) {
3399 default: // Can't happen, don't crash on invalid number though.
3400 case 0: // Return the value of the EQ bit of CR6.
3401 BitNo = 0; InvertBit = false;
3402 break;
3403 case 1: // Return the inverted value of the EQ bit of CR6.
3404 BitNo = 0; InvertBit = true;
3405 break;
3406 case 2: // Return the value of the LT bit of CR6.
3407 BitNo = 2; InvertBit = false;
3408 break;
3409 case 3: // Return the inverted value of the LT bit of CR6.
3410 BitNo = 2; InvertBit = true;
3411 break;
3412 }
3413
3414 // Shift the bit into the low position.
3415 Flags = DAG.getNode(ISD::SRL, MVT::i32, Flags,
3416 DAG.getConstant(8-(3-BitNo), MVT::i32));
3417 // Isolate the bit.
3418 Flags = DAG.getNode(ISD::AND, MVT::i32, Flags,
3419 DAG.getConstant(1, MVT::i32));
3420
3421 // If we are supposed to, toggle the bit.
3422 if (InvertBit)
3423 Flags = DAG.getNode(ISD::XOR, MVT::i32, Flags,
3424 DAG.getConstant(1, MVT::i32));
3425 return Flags;
3426}
3427
Dale Johannesen5b3b6952008-03-04 23:17:14 +00003428SDOperand PPCTargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op,
3429 SelectionDAG &DAG) {
Chris Lattner1a635d62006-04-14 06:01:58 +00003430 // Create a stack slot that is 16-byte aligned.
3431 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
3432 int FrameIdx = FrameInfo->CreateStackObject(16, 16);
Chris Lattner0d72a202006-07-28 16:45:47 +00003433 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
3434 SDOperand FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Chris Lattner1a635d62006-04-14 06:01:58 +00003435
3436 // Store the input value into Value#0 of the stack slot.
Evan Cheng786225a2006-10-05 23:01:46 +00003437 SDOperand Store = DAG.getStore(DAG.getEntryNode(),
Evan Cheng8b2794a2006-10-13 21:14:26 +00003438 Op.getOperand(0), FIdx, NULL, 0);
Chris Lattner1a635d62006-04-14 06:01:58 +00003439 // Load it out.
Evan Cheng466685d2006-10-09 20:57:25 +00003440 return DAG.getLoad(Op.getValueType(), Store, FIdx, NULL, 0);
Chris Lattner1a635d62006-04-14 06:01:58 +00003441}
3442
Dale Johannesen5b3b6952008-03-04 23:17:14 +00003443SDOperand PPCTargetLowering::LowerMUL(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner72dd9bd2006-04-18 03:43:48 +00003444 if (Op.getValueType() == MVT::v4i32) {
3445 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
3446
3447 SDOperand Zero = BuildSplatI( 0, 1, MVT::v4i32, DAG);
3448 SDOperand Neg16 = BuildSplatI(-16, 4, MVT::v4i32, DAG); // +16 as shift amt.
3449
3450 SDOperand RHSSwap = // = vrlw RHS, 16
3451 BuildIntrinsicOp(Intrinsic::ppc_altivec_vrlw, RHS, Neg16, DAG);
3452
3453 // Shrinkify inputs to v8i16.
3454 LHS = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, LHS);
3455 RHS = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, RHS);
3456 RHSSwap = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, RHSSwap);
3457
3458 // Low parts multiplied together, generating 32-bit results (we ignore the
3459 // top parts).
3460 SDOperand LoProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmulouh,
3461 LHS, RHS, DAG, MVT::v4i32);
3462
3463 SDOperand HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmsumuhm,
3464 LHS, RHSSwap, Zero, DAG, MVT::v4i32);
3465 // Shift the high parts up 16 bits.
3466 HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, HiProd, Neg16, DAG);
3467 return DAG.getNode(ISD::ADD, MVT::v4i32, LoProd, HiProd);
3468 } else if (Op.getValueType() == MVT::v8i16) {
3469 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
3470
Chris Lattnercea2aa72006-04-18 04:28:57 +00003471 SDOperand Zero = BuildSplatI(0, 1, MVT::v8i16, DAG);
Chris Lattner72dd9bd2006-04-18 03:43:48 +00003472
Chris Lattnercea2aa72006-04-18 04:28:57 +00003473 return BuildIntrinsicOp(Intrinsic::ppc_altivec_vmladduhm,
3474 LHS, RHS, Zero, DAG);
Chris Lattner19a81522006-04-18 03:57:35 +00003475 } else if (Op.getValueType() == MVT::v16i8) {
3476 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
3477
3478 // Multiply the even 8-bit parts, producing 16-bit sums.
3479 SDOperand EvenParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuleub,
3480 LHS, RHS, DAG, MVT::v8i16);
3481 EvenParts = DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, EvenParts);
3482
3483 // Multiply the odd 8-bit parts, producing 16-bit sums.
3484 SDOperand OddParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuloub,
3485 LHS, RHS, DAG, MVT::v8i16);
3486 OddParts = DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, OddParts);
3487
3488 // Merge the results together.
Chris Lattnere2199452006-08-11 17:38:39 +00003489 SDOperand Ops[16];
Chris Lattner19a81522006-04-18 03:57:35 +00003490 for (unsigned i = 0; i != 8; ++i) {
Chris Lattnere2199452006-08-11 17:38:39 +00003491 Ops[i*2 ] = DAG.getConstant(2*i+1, MVT::i8);
3492 Ops[i*2+1] = DAG.getConstant(2*i+1+16, MVT::i8);
Chris Lattner19a81522006-04-18 03:57:35 +00003493 }
Chris Lattner19a81522006-04-18 03:57:35 +00003494 return DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v16i8, EvenParts, OddParts,
Chris Lattnere2199452006-08-11 17:38:39 +00003495 DAG.getNode(ISD::BUILD_VECTOR, MVT::v16i8, Ops, 16));
Chris Lattner72dd9bd2006-04-18 03:43:48 +00003496 } else {
3497 assert(0 && "Unknown mul to lower!");
3498 abort();
3499 }
Chris Lattnere7c768e2006-04-18 03:24:30 +00003500}
3501
Chris Lattnere4bc9ea2005-08-26 00:52:45 +00003502/// LowerOperation - Provide custom lowering hooks for some operations.
3503///
Nate Begeman21e463b2005-10-16 05:39:50 +00003504SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +00003505 switch (Op.getOpcode()) {
3506 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattner1a635d62006-04-14 06:01:58 +00003507 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
3508 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +00003509 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Nate Begeman37efe672006-04-22 18:53:45 +00003510 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Chris Lattner1a635d62006-04-14 06:01:58 +00003511 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nicolas Geoffray01119992007-04-03 13:59:52 +00003512 case ISD::VASTART:
3513 return LowerVASTART(Op, DAG, VarArgsFrameIndex, VarArgsStackOffset,
3514 VarArgsNumGPR, VarArgsNumFPR, PPCSubTarget);
3515
3516 case ISD::VAARG:
3517 return LowerVAARG(Op, DAG, VarArgsFrameIndex, VarArgsStackOffset,
3518 VarArgsNumGPR, VarArgsNumFPR, PPCSubTarget);
3519
Chris Lattneref957102006-06-21 00:34:03 +00003520 case ISD::FORMAL_ARGUMENTS:
Nicolas Geoffray01119992007-04-03 13:59:52 +00003521 return LowerFORMAL_ARGUMENTS(Op, DAG, VarArgsFrameIndex,
3522 VarArgsStackOffset, VarArgsNumGPR,
3523 VarArgsNumFPR, PPCSubTarget);
3524
Dan Gohman7925ed02008-03-19 21:39:28 +00003525 case ISD::CALL: return LowerCALL(Op, DAG, PPCSubTarget,
3526 getTargetMachine());
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00003527 case ISD::RET: return LowerRET(Op, DAG, getTargetMachine());
Jim Laskeyefc7e522006-12-04 22:04:42 +00003528 case ISD::STACKRESTORE: return LowerSTACKRESTORE(Op, DAG, PPCSubTarget);
Chris Lattner9f0bc652007-02-25 05:34:32 +00003529 case ISD::DYNAMIC_STACKALLOC:
3530 return LowerDYNAMIC_STACKALLOC(Op, DAG, PPCSubTarget);
Evan Cheng54fc97d2008-04-19 01:30:48 +00003531
3532 case ISD::ATOMIC_LAS: return LowerAtomicLAS(Op, DAG);
3533 case ISD::ATOMIC_LCS: return LowerAtomicLCS(Op, DAG);
3534 case ISD::ATOMIC_SWAP: return LowerAtomicSWAP(Op, DAG);
Chris Lattner7c0d6642005-10-02 06:37:13 +00003535
Chris Lattner1a635d62006-04-14 06:01:58 +00003536 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
3537 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
3538 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003539 case ISD::FP_ROUND_INREG: return LowerFP_ROUND_INREG(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00003540 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00003541
Chris Lattner1a635d62006-04-14 06:01:58 +00003542 // Lower 64-bit shifts.
Chris Lattner3fe6c1d2006-09-20 03:47:40 +00003543 case ISD::SHL_PARTS: return LowerSHL_PARTS(Op, DAG);
3544 case ISD::SRL_PARTS: return LowerSRL_PARTS(Op, DAG);
3545 case ISD::SRA_PARTS: return LowerSRA_PARTS(Op, DAG);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00003546
Chris Lattner1a635d62006-04-14 06:01:58 +00003547 // Vector-related lowering.
3548 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
3549 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
3550 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
3551 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
Chris Lattnere7c768e2006-04-18 03:24:30 +00003552 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00003553
Chris Lattner3fc027d2007-12-08 06:59:59 +00003554 // Frame & Return address.
3555 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Nicolas Geoffray43c6e7c2007-03-01 13:11:38 +00003556 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Chris Lattnerbc11c342005-08-31 20:23:54 +00003557 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +00003558 return SDOperand();
3559}
3560
Chris Lattner1f873002007-11-28 18:44:47 +00003561SDNode *PPCTargetLowering::ExpandOperationResult(SDNode *N, SelectionDAG &DAG) {
3562 switch (N->getOpcode()) {
3563 default: assert(0 && "Wasn't expecting to be able to lower this!");
3564 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(SDOperand(N, 0), DAG).Val;
3565 }
3566}
3567
3568
Chris Lattner1a635d62006-04-14 06:01:58 +00003569//===----------------------------------------------------------------------===//
3570// Other Lowering Code
3571//===----------------------------------------------------------------------===//
3572
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00003573MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00003574PPCTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
3575 MachineBasicBlock *BB) {
Evan Chengc0f64ff2006-11-27 23:37:22 +00003576 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Chris Lattnerc08f9022006-06-27 00:04:13 +00003577 assert((MI->getOpcode() == PPC::SELECT_CC_I4 ||
3578 MI->getOpcode() == PPC::SELECT_CC_I8 ||
Chris Lattner919c0322005-10-01 01:35:02 +00003579 MI->getOpcode() == PPC::SELECT_CC_F4 ||
Chris Lattner710ff322006-04-08 22:45:08 +00003580 MI->getOpcode() == PPC::SELECT_CC_F8 ||
3581 MI->getOpcode() == PPC::SELECT_CC_VRRC) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00003582 "Unexpected instr type to insert");
3583
3584 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
3585 // control-flow pattern. The incoming instruction knows the destination vreg
3586 // to set, the condition code register to branch on, the true/false values to
3587 // select between, and a branch opcode to use.
3588 const BasicBlock *LLVM_BB = BB->getBasicBlock();
3589 ilist<MachineBasicBlock>::iterator It = BB;
3590 ++It;
3591
3592 // thisMBB:
3593 // ...
3594 // TrueVal = ...
3595 // cmpTY ccX, r1, r2
3596 // bCC copy1MBB
3597 // fallthrough --> copy0MBB
3598 MachineBasicBlock *thisMBB = BB;
3599 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
3600 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Chris Lattnerdf4ed632006-11-17 22:10:59 +00003601 unsigned SelectPred = MI->getOperand(4).getImm();
Evan Chengc0f64ff2006-11-27 23:37:22 +00003602 BuildMI(BB, TII->get(PPC::BCC))
Chris Lattner18258c62006-11-17 22:37:34 +00003603 .addImm(SelectPred).addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00003604 MachineFunction *F = BB->getParent();
3605 F->getBasicBlockList().insert(It, copy0MBB);
3606 F->getBasicBlockList().insert(It, sinkMBB);
Nate Begemanf15485a2006-03-27 01:32:24 +00003607 // Update machine-CFG edges by first adding all successors of the current
3608 // block to the new block which will contain the Phi node for the select.
3609 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
3610 e = BB->succ_end(); i != e; ++i)
3611 sinkMBB->addSuccessor(*i);
3612 // Next, remove all successors of the current block, and add the true
3613 // and fallthrough blocks as its successors.
3614 while(!BB->succ_empty())
3615 BB->removeSuccessor(BB->succ_begin());
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00003616 BB->addSuccessor(copy0MBB);
3617 BB->addSuccessor(sinkMBB);
3618
3619 // copy0MBB:
3620 // %FalseValue = ...
3621 // # fallthrough to sinkMBB
3622 BB = copy0MBB;
3623
3624 // Update machine-CFG edges
3625 BB->addSuccessor(sinkMBB);
3626
3627 // sinkMBB:
3628 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
3629 // ...
3630 BB = sinkMBB;
Evan Chengc0f64ff2006-11-27 23:37:22 +00003631 BuildMI(BB, TII->get(PPC::PHI), MI->getOperand(0).getReg())
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00003632 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
3633 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
3634
3635 delete MI; // The pseudo instruction is gone now.
3636 return BB;
3637}
3638
Chris Lattner1a635d62006-04-14 06:01:58 +00003639//===----------------------------------------------------------------------===//
3640// Target Optimization Hooks
3641//===----------------------------------------------------------------------===//
3642
Chris Lattner8c13d0a2006-03-01 04:57:39 +00003643SDOperand PPCTargetLowering::PerformDAGCombine(SDNode *N,
3644 DAGCombinerInfo &DCI) const {
3645 TargetMachine &TM = getTargetMachine();
3646 SelectionDAG &DAG = DCI.DAG;
3647 switch (N->getOpcode()) {
3648 default: break;
Chris Lattnercf9d0ac2006-09-19 05:22:59 +00003649 case PPCISD::SHL:
3650 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
3651 if (C->getValue() == 0) // 0 << V -> 0.
3652 return N->getOperand(0);
3653 }
3654 break;
3655 case PPCISD::SRL:
3656 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
3657 if (C->getValue() == 0) // 0 >>u V -> 0.
3658 return N->getOperand(0);
3659 }
3660 break;
3661 case PPCISD::SRA:
3662 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
3663 if (C->getValue() == 0 || // 0 >>s V -> 0.
3664 C->isAllOnesValue()) // -1 >>s V -> -1.
3665 return N->getOperand(0);
3666 }
3667 break;
3668
Chris Lattner8c13d0a2006-03-01 04:57:39 +00003669 case ISD::SINT_TO_FP:
Chris Lattnera7a58542006-06-16 17:34:12 +00003670 if (TM.getSubtarget<PPCSubtarget>().has64BitSupport()) {
Chris Lattnerecfe55e2006-03-22 05:30:33 +00003671 if (N->getOperand(0).getOpcode() == ISD::FP_TO_SINT) {
3672 // Turn (sint_to_fp (fp_to_sint X)) -> fctidz/fcfid without load/stores.
3673 // We allow the src/dst to be either f32/f64, but the intermediate
3674 // type must be i64.
Dale Johannesen79217062007-10-23 23:20:14 +00003675 if (N->getOperand(0).getValueType() == MVT::i64 &&
3676 N->getOperand(0).getOperand(0).getValueType() != MVT::ppcf128) {
Chris Lattnerecfe55e2006-03-22 05:30:33 +00003677 SDOperand Val = N->getOperand(0).getOperand(0);
3678 if (Val.getValueType() == MVT::f32) {
3679 Val = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Val);
3680 DCI.AddToWorklist(Val.Val);
3681 }
3682
3683 Val = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Val);
Chris Lattner8c13d0a2006-03-01 04:57:39 +00003684 DCI.AddToWorklist(Val.Val);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00003685 Val = DAG.getNode(PPCISD::FCFID, MVT::f64, Val);
Chris Lattner8c13d0a2006-03-01 04:57:39 +00003686 DCI.AddToWorklist(Val.Val);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00003687 if (N->getValueType(0) == MVT::f32) {
Chris Lattner0bd48932008-01-17 07:00:52 +00003688 Val = DAG.getNode(ISD::FP_ROUND, MVT::f32, Val,
3689 DAG.getIntPtrConstant(0));
Chris Lattnerecfe55e2006-03-22 05:30:33 +00003690 DCI.AddToWorklist(Val.Val);
3691 }
3692 return Val;
3693 } else if (N->getOperand(0).getValueType() == MVT::i32) {
3694 // If the intermediate type is i32, we can avoid the load/store here
3695 // too.
Chris Lattner8c13d0a2006-03-01 04:57:39 +00003696 }
Chris Lattner8c13d0a2006-03-01 04:57:39 +00003697 }
3698 }
3699 break;
Chris Lattner51269842006-03-01 05:50:56 +00003700 case ISD::STORE:
3701 // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)).
3702 if (TM.getSubtarget<PPCSubtarget>().hasSTFIWX() &&
Chris Lattnera7a02fb2008-01-18 16:54:56 +00003703 !cast<StoreSDNode>(N)->isTruncatingStore() &&
Chris Lattner51269842006-03-01 05:50:56 +00003704 N->getOperand(1).getOpcode() == ISD::FP_TO_SINT &&
Dale Johannesen79217062007-10-23 23:20:14 +00003705 N->getOperand(1).getValueType() == MVT::i32 &&
3706 N->getOperand(1).getOperand(0).getValueType() != MVT::ppcf128) {
Chris Lattner51269842006-03-01 05:50:56 +00003707 SDOperand Val = N->getOperand(1).getOperand(0);
3708 if (Val.getValueType() == MVT::f32) {
3709 Val = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Val);
3710 DCI.AddToWorklist(Val.Val);
3711 }
3712 Val = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Val);
3713 DCI.AddToWorklist(Val.Val);
3714
3715 Val = DAG.getNode(PPCISD::STFIWX, MVT::Other, N->getOperand(0), Val,
3716 N->getOperand(2), N->getOperand(3));
3717 DCI.AddToWorklist(Val.Val);
3718 return Val;
3719 }
Chris Lattnerd9989382006-07-10 20:56:58 +00003720
3721 // Turn STORE (BSWAP) -> sthbrx/stwbrx.
3722 if (N->getOperand(1).getOpcode() == ISD::BSWAP &&
3723 N->getOperand(1).Val->hasOneUse() &&
3724 (N->getOperand(1).getValueType() == MVT::i32 ||
3725 N->getOperand(1).getValueType() == MVT::i16)) {
3726 SDOperand BSwapOp = N->getOperand(1).getOperand(0);
3727 // Do an any-extend to 32-bits if this is a half-word input.
3728 if (BSwapOp.getValueType() == MVT::i16)
3729 BSwapOp = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, BSwapOp);
3730
3731 return DAG.getNode(PPCISD::STBRX, MVT::Other, N->getOperand(0), BSwapOp,
3732 N->getOperand(2), N->getOperand(3),
3733 DAG.getValueType(N->getOperand(1).getValueType()));
3734 }
3735 break;
3736 case ISD::BSWAP:
3737 // Turn BSWAP (LOAD) -> lhbrx/lwbrx.
Evan Cheng466685d2006-10-09 20:57:25 +00003738 if (ISD::isNON_EXTLoad(N->getOperand(0).Val) &&
Chris Lattnerd9989382006-07-10 20:56:58 +00003739 N->getOperand(0).hasOneUse() &&
3740 (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i16)) {
3741 SDOperand Load = N->getOperand(0);
Evan Cheng466685d2006-10-09 20:57:25 +00003742 LoadSDNode *LD = cast<LoadSDNode>(Load);
Chris Lattnerd9989382006-07-10 20:56:58 +00003743 // Create the byte-swapping load.
3744 std::vector<MVT::ValueType> VTs;
3745 VTs.push_back(MVT::i32);
3746 VTs.push_back(MVT::Other);
Dan Gohman69de1932008-02-06 22:27:42 +00003747 SDOperand MO = DAG.getMemOperand(LD->getMemOperand());
Chris Lattner79e490a2006-08-11 17:18:05 +00003748 SDOperand Ops[] = {
Evan Cheng466685d2006-10-09 20:57:25 +00003749 LD->getChain(), // Chain
3750 LD->getBasePtr(), // Ptr
Dan Gohman69de1932008-02-06 22:27:42 +00003751 MO, // MemOperand
Chris Lattner79e490a2006-08-11 17:18:05 +00003752 DAG.getValueType(N->getValueType(0)) // VT
3753 };
3754 SDOperand BSLoad = DAG.getNode(PPCISD::LBRX, VTs, Ops, 4);
Chris Lattnerd9989382006-07-10 20:56:58 +00003755
3756 // If this is an i16 load, insert the truncate.
3757 SDOperand ResVal = BSLoad;
3758 if (N->getValueType(0) == MVT::i16)
3759 ResVal = DAG.getNode(ISD::TRUNCATE, MVT::i16, BSLoad);
3760
3761 // First, combine the bswap away. This makes the value produced by the
3762 // load dead.
3763 DCI.CombineTo(N, ResVal);
3764
3765 // Next, combine the load away, we give it a bogus result value but a real
3766 // chain result. The result value is dead because the bswap is dead.
3767 DCI.CombineTo(Load.Val, ResVal, BSLoad.getValue(1));
3768
3769 // Return N so it doesn't get rechecked!
3770 return SDOperand(N, 0);
3771 }
3772
Chris Lattner51269842006-03-01 05:50:56 +00003773 break;
Chris Lattner4468c222006-03-31 06:02:07 +00003774 case PPCISD::VCMP: {
3775 // If a VCMPo node already exists with exactly the same operands as this
3776 // node, use its result instead of this node (VCMPo computes both a CR6 and
3777 // a normal output).
3778 //
3779 if (!N->getOperand(0).hasOneUse() &&
3780 !N->getOperand(1).hasOneUse() &&
3781 !N->getOperand(2).hasOneUse()) {
3782
3783 // Scan all of the users of the LHS, looking for VCMPo's that match.
3784 SDNode *VCMPoNode = 0;
3785
3786 SDNode *LHSN = N->getOperand(0).Val;
3787 for (SDNode::use_iterator UI = LHSN->use_begin(), E = LHSN->use_end();
3788 UI != E; ++UI)
Roman Levensteindc1adac2008-04-07 10:06:32 +00003789 if ((*UI).getUser()->getOpcode() == PPCISD::VCMPo &&
3790 (*UI).getUser()->getOperand(1) == N->getOperand(1) &&
3791 (*UI).getUser()->getOperand(2) == N->getOperand(2) &&
3792 (*UI).getUser()->getOperand(0) == N->getOperand(0)) {
3793 VCMPoNode = UI->getUser();
Chris Lattner4468c222006-03-31 06:02:07 +00003794 break;
3795 }
3796
Chris Lattner00901202006-04-18 18:28:22 +00003797 // If there is no VCMPo node, or if the flag value has a single use, don't
3798 // transform this.
3799 if (!VCMPoNode || VCMPoNode->hasNUsesOfValue(0, 1))
3800 break;
3801
3802 // Look at the (necessarily single) use of the flag value. If it has a
3803 // chain, this transformation is more complex. Note that multiple things
3804 // could use the value result, which we should ignore.
3805 SDNode *FlagUser = 0;
3806 for (SDNode::use_iterator UI = VCMPoNode->use_begin();
3807 FlagUser == 0; ++UI) {
3808 assert(UI != VCMPoNode->use_end() && "Didn't find user!");
Roman Levensteindc1adac2008-04-07 10:06:32 +00003809 SDNode *User = UI->getUser();
Chris Lattner00901202006-04-18 18:28:22 +00003810 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
3811 if (User->getOperand(i) == SDOperand(VCMPoNode, 1)) {
3812 FlagUser = User;
3813 break;
3814 }
3815 }
3816 }
3817
3818 // If the user is a MFCR instruction, we know this is safe. Otherwise we
3819 // give up for right now.
3820 if (FlagUser->getOpcode() == PPCISD::MFCR)
Chris Lattner4468c222006-03-31 06:02:07 +00003821 return SDOperand(VCMPoNode, 0);
3822 }
3823 break;
3824 }
Chris Lattner90564f22006-04-18 17:59:36 +00003825 case ISD::BR_CC: {
3826 // If this is a branch on an altivec predicate comparison, lower this so
3827 // that we don't have to do a MFCR: instead, branch directly on CR6. This
3828 // lowering is done pre-legalize, because the legalizer lowers the predicate
3829 // compare down to code that is difficult to reassemble.
3830 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
3831 SDOperand LHS = N->getOperand(2), RHS = N->getOperand(3);
3832 int CompareOpc;
3833 bool isDot;
3834
3835 if (LHS.getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
3836 isa<ConstantSDNode>(RHS) && (CC == ISD::SETEQ || CC == ISD::SETNE) &&
3837 getAltivecCompareInfo(LHS, CompareOpc, isDot)) {
3838 assert(isDot && "Can't compare against a vector result!");
3839
3840 // If this is a comparison against something other than 0/1, then we know
3841 // that the condition is never/always true.
3842 unsigned Val = cast<ConstantSDNode>(RHS)->getValue();
3843 if (Val != 0 && Val != 1) {
3844 if (CC == ISD::SETEQ) // Cond never true, remove branch.
3845 return N->getOperand(0);
3846 // Always !=, turn it into an unconditional branch.
3847 return DAG.getNode(ISD::BR, MVT::Other,
3848 N->getOperand(0), N->getOperand(4));
3849 }
3850
3851 bool BranchOnWhenPredTrue = (CC == ISD::SETEQ) ^ (Val == 0);
3852
3853 // Create the PPCISD altivec 'dot' comparison node.
Chris Lattner90564f22006-04-18 17:59:36 +00003854 std::vector<MVT::ValueType> VTs;
Chris Lattner79e490a2006-08-11 17:18:05 +00003855 SDOperand Ops[] = {
3856 LHS.getOperand(2), // LHS of compare
3857 LHS.getOperand(3), // RHS of compare
3858 DAG.getConstant(CompareOpc, MVT::i32)
3859 };
Chris Lattner90564f22006-04-18 17:59:36 +00003860 VTs.push_back(LHS.getOperand(2).getValueType());
3861 VTs.push_back(MVT::Flag);
Chris Lattner79e490a2006-08-11 17:18:05 +00003862 SDOperand CompNode = DAG.getNode(PPCISD::VCMPo, VTs, Ops, 3);
Chris Lattner90564f22006-04-18 17:59:36 +00003863
3864 // Unpack the result based on how the target uses it.
Chris Lattnerdf4ed632006-11-17 22:10:59 +00003865 PPC::Predicate CompOpc;
Chris Lattner90564f22006-04-18 17:59:36 +00003866 switch (cast<ConstantSDNode>(LHS.getOperand(1))->getValue()) {
3867 default: // Can't happen, don't crash on invalid number though.
3868 case 0: // Branch on the value of the EQ bit of CR6.
Chris Lattnerdf4ed632006-11-17 22:10:59 +00003869 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_EQ : PPC::PRED_NE;
Chris Lattner90564f22006-04-18 17:59:36 +00003870 break;
3871 case 1: // Branch on the inverted value of the EQ bit of CR6.
Chris Lattnerdf4ed632006-11-17 22:10:59 +00003872 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_NE : PPC::PRED_EQ;
Chris Lattner90564f22006-04-18 17:59:36 +00003873 break;
3874 case 2: // Branch on the value of the LT bit of CR6.
Chris Lattnerdf4ed632006-11-17 22:10:59 +00003875 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_LT : PPC::PRED_GE;
Chris Lattner90564f22006-04-18 17:59:36 +00003876 break;
3877 case 3: // Branch on the inverted value of the LT bit of CR6.
Chris Lattnerdf4ed632006-11-17 22:10:59 +00003878 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_GE : PPC::PRED_LT;
Chris Lattner90564f22006-04-18 17:59:36 +00003879 break;
3880 }
3881
3882 return DAG.getNode(PPCISD::COND_BRANCH, MVT::Other, N->getOperand(0),
Chris Lattner90564f22006-04-18 17:59:36 +00003883 DAG.getConstant(CompOpc, MVT::i32),
Chris Lattner18258c62006-11-17 22:37:34 +00003884 DAG.getRegister(PPC::CR6, MVT::i32),
Chris Lattner90564f22006-04-18 17:59:36 +00003885 N->getOperand(4), CompNode.getValue(1));
3886 }
3887 break;
3888 }
Chris Lattner8c13d0a2006-03-01 04:57:39 +00003889 }
3890
3891 return SDOperand();
3892}
3893
Chris Lattner1a635d62006-04-14 06:01:58 +00003894//===----------------------------------------------------------------------===//
3895// Inline Assembly Support
3896//===----------------------------------------------------------------------===//
3897
Chris Lattnerbbe77de2006-04-02 06:26:07 +00003898void PPCTargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00003899 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00003900 APInt &KnownZero,
3901 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00003902 const SelectionDAG &DAG,
Chris Lattnerbbe77de2006-04-02 06:26:07 +00003903 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00003904 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Chris Lattnerbbe77de2006-04-02 06:26:07 +00003905 switch (Op.getOpcode()) {
3906 default: break;
Chris Lattnerd9989382006-07-10 20:56:58 +00003907 case PPCISD::LBRX: {
3908 // lhbrx is known to have the top bits cleared out.
3909 if (cast<VTSDNode>(Op.getOperand(3))->getVT() == MVT::i16)
3910 KnownZero = 0xFFFF0000;
3911 break;
3912 }
Chris Lattnerbbe77de2006-04-02 06:26:07 +00003913 case ISD::INTRINSIC_WO_CHAIN: {
3914 switch (cast<ConstantSDNode>(Op.getOperand(0))->getValue()) {
3915 default: break;
3916 case Intrinsic::ppc_altivec_vcmpbfp_p:
3917 case Intrinsic::ppc_altivec_vcmpeqfp_p:
3918 case Intrinsic::ppc_altivec_vcmpequb_p:
3919 case Intrinsic::ppc_altivec_vcmpequh_p:
3920 case Intrinsic::ppc_altivec_vcmpequw_p:
3921 case Intrinsic::ppc_altivec_vcmpgefp_p:
3922 case Intrinsic::ppc_altivec_vcmpgtfp_p:
3923 case Intrinsic::ppc_altivec_vcmpgtsb_p:
3924 case Intrinsic::ppc_altivec_vcmpgtsh_p:
3925 case Intrinsic::ppc_altivec_vcmpgtsw_p:
3926 case Intrinsic::ppc_altivec_vcmpgtub_p:
3927 case Intrinsic::ppc_altivec_vcmpgtuh_p:
3928 case Intrinsic::ppc_altivec_vcmpgtuw_p:
3929 KnownZero = ~1U; // All bits but the low one are known to be zero.
3930 break;
3931 }
3932 }
3933 }
3934}
3935
3936
Chris Lattner4234f572007-03-25 02:14:49 +00003937/// getConstraintType - Given a constraint, return the type of
Chris Lattnerad3bc8d2006-02-07 20:16:30 +00003938/// constraint it is for this target.
3939PPCTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00003940PPCTargetLowering::getConstraintType(const std::string &Constraint) const {
3941 if (Constraint.size() == 1) {
3942 switch (Constraint[0]) {
3943 default: break;
3944 case 'b':
3945 case 'r':
3946 case 'f':
3947 case 'v':
3948 case 'y':
3949 return C_RegisterClass;
3950 }
3951 }
3952 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerad3bc8d2006-02-07 20:16:30 +00003953}
3954
Chris Lattner331d1bc2006-11-02 01:44:04 +00003955std::pair<unsigned, const TargetRegisterClass*>
3956PPCTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
3957 MVT::ValueType VT) const {
Chris Lattnerddc787d2006-01-31 19:20:21 +00003958 if (Constraint.size() == 1) {
Chris Lattner331d1bc2006-11-02 01:44:04 +00003959 // GCC RS6000 Constraint Letters
3960 switch (Constraint[0]) {
3961 case 'b': // R1-R31
3962 case 'r': // R0-R31
3963 if (VT == MVT::i64 && PPCSubTarget.isPPC64())
3964 return std::make_pair(0U, PPC::G8RCRegisterClass);
3965 return std::make_pair(0U, PPC::GPRCRegisterClass);
3966 case 'f':
3967 if (VT == MVT::f32)
3968 return std::make_pair(0U, PPC::F4RCRegisterClass);
3969 else if (VT == MVT::f64)
3970 return std::make_pair(0U, PPC::F8RCRegisterClass);
3971 break;
Chris Lattnerddc787d2006-01-31 19:20:21 +00003972 case 'v':
Chris Lattner331d1bc2006-11-02 01:44:04 +00003973 return std::make_pair(0U, PPC::VRRCRegisterClass);
3974 case 'y': // crrc
3975 return std::make_pair(0U, PPC::CRRCRegisterClass);
Chris Lattnerddc787d2006-01-31 19:20:21 +00003976 }
3977 }
3978
Chris Lattner331d1bc2006-11-02 01:44:04 +00003979 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattnerddc787d2006-01-31 19:20:21 +00003980}
Chris Lattner763317d2006-02-07 00:47:13 +00003981
Chris Lattner331d1bc2006-11-02 01:44:04 +00003982
Chris Lattner48884cd2007-08-25 00:47:38 +00003983/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3984/// vector. If it is invalid, don't add anything to Ops.
3985void PPCTargetLowering::LowerAsmOperandForConstraint(SDOperand Op, char Letter,
3986 std::vector<SDOperand>&Ops,
3987 SelectionDAG &DAG) {
3988 SDOperand Result(0,0);
Chris Lattner763317d2006-02-07 00:47:13 +00003989 switch (Letter) {
3990 default: break;
3991 case 'I':
3992 case 'J':
3993 case 'K':
3994 case 'L':
3995 case 'M':
3996 case 'N':
3997 case 'O':
3998 case 'P': {
Chris Lattner9f5d5782007-05-15 01:31:05 +00003999 ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op);
Chris Lattner48884cd2007-08-25 00:47:38 +00004000 if (!CST) return; // Must be an immediate to match.
Chris Lattner9f5d5782007-05-15 01:31:05 +00004001 unsigned Value = CST->getValue();
Chris Lattner763317d2006-02-07 00:47:13 +00004002 switch (Letter) {
4003 default: assert(0 && "Unknown constraint letter!");
4004 case 'I': // "I" is a signed 16-bit constant.
Chris Lattner9f5d5782007-05-15 01:31:05 +00004005 if ((short)Value == (int)Value)
Chris Lattner48884cd2007-08-25 00:47:38 +00004006 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00004007 break;
Chris Lattner763317d2006-02-07 00:47:13 +00004008 case 'J': // "J" is a constant with only the high-order 16 bits nonzero.
4009 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits.
Chris Lattner9f5d5782007-05-15 01:31:05 +00004010 if ((short)Value == 0)
Chris Lattner48884cd2007-08-25 00:47:38 +00004011 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00004012 break;
Chris Lattner763317d2006-02-07 00:47:13 +00004013 case 'K': // "K" is a constant with only the low-order 16 bits nonzero.
Chris Lattner9f5d5782007-05-15 01:31:05 +00004014 if ((Value >> 16) == 0)
Chris Lattner48884cd2007-08-25 00:47:38 +00004015 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00004016 break;
Chris Lattner763317d2006-02-07 00:47:13 +00004017 case 'M': // "M" is a constant that is greater than 31.
Chris Lattner9f5d5782007-05-15 01:31:05 +00004018 if (Value > 31)
Chris Lattner48884cd2007-08-25 00:47:38 +00004019 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00004020 break;
Chris Lattner763317d2006-02-07 00:47:13 +00004021 case 'N': // "N" is a positive constant that is an exact power of two.
Chris Lattner9f5d5782007-05-15 01:31:05 +00004022 if ((int)Value > 0 && isPowerOf2_32(Value))
Chris Lattner48884cd2007-08-25 00:47:38 +00004023 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00004024 break;
Chris Lattner763317d2006-02-07 00:47:13 +00004025 case 'O': // "O" is the constant zero.
Chris Lattner9f5d5782007-05-15 01:31:05 +00004026 if (Value == 0)
Chris Lattner48884cd2007-08-25 00:47:38 +00004027 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00004028 break;
Chris Lattner763317d2006-02-07 00:47:13 +00004029 case 'P': // "P" is a constant whose negation is a signed 16-bit constant.
Chris Lattner9f5d5782007-05-15 01:31:05 +00004030 if ((short)-Value == (int)-Value)
Chris Lattner48884cd2007-08-25 00:47:38 +00004031 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00004032 break;
Chris Lattner763317d2006-02-07 00:47:13 +00004033 }
4034 break;
4035 }
4036 }
4037
Chris Lattner48884cd2007-08-25 00:47:38 +00004038 if (Result.Val) {
4039 Ops.push_back(Result);
4040 return;
4041 }
4042
Chris Lattner763317d2006-02-07 00:47:13 +00004043 // Handle standard constraint letters.
Chris Lattner48884cd2007-08-25 00:47:38 +00004044 TargetLowering::LowerAsmOperandForConstraint(Op, Letter, Ops, DAG);
Chris Lattner763317d2006-02-07 00:47:13 +00004045}
Evan Chengc4c62572006-03-13 23:20:37 +00004046
Chris Lattnerc9addb72007-03-30 23:15:24 +00004047// isLegalAddressingMode - Return true if the addressing mode represented
4048// by AM is legal for this target, for a load/store of the specified type.
4049bool PPCTargetLowering::isLegalAddressingMode(const AddrMode &AM,
4050 const Type *Ty) const {
4051 // FIXME: PPC does not allow r+i addressing modes for vectors!
4052
4053 // PPC allows a sign-extended 16-bit immediate field.
4054 if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1)
4055 return false;
4056
4057 // No global is ever allowed as a base.
4058 if (AM.BaseGV)
4059 return false;
4060
4061 // PPC only support r+r,
4062 switch (AM.Scale) {
4063 case 0: // "r+i" or just "i", depending on HasBaseReg.
4064 break;
4065 case 1:
4066 if (AM.HasBaseReg && AM.BaseOffs) // "r+r+i" is not allowed.
4067 return false;
4068 // Otherwise we have r+r or r+i.
4069 break;
4070 case 2:
4071 if (AM.HasBaseReg || AM.BaseOffs) // 2*r+r or 2*r+i is not allowed.
4072 return false;
4073 // Allow 2*r as r+r.
4074 break;
Chris Lattner7c7ba9d2007-04-09 22:10:05 +00004075 default:
4076 // No other scales are supported.
4077 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00004078 }
4079
4080 return true;
4081}
4082
Evan Chengc4c62572006-03-13 23:20:37 +00004083/// isLegalAddressImmediate - Return true if the integer value can be used
Evan Cheng86193912007-03-12 23:29:01 +00004084/// as the offset of the target addressing mode for load / store of the
4085/// given type.
4086bool PPCTargetLowering::isLegalAddressImmediate(int64_t V,const Type *Ty) const{
Evan Chengc4c62572006-03-13 23:20:37 +00004087 // PPC allows a sign-extended 16-bit immediate field.
4088 return (V > -(1 << 16) && V < (1 << 16)-1);
4089}
Reid Spencer3a9ec242006-08-28 01:02:49 +00004090
4091bool PPCTargetLowering::isLegalAddressImmediate(llvm::GlobalValue* GV) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +00004092 return false;
Reid Spencer3a9ec242006-08-28 01:02:49 +00004093}
Nicolas Geoffray43c6e7c2007-03-01 13:11:38 +00004094
Chris Lattner3fc027d2007-12-08 06:59:59 +00004095SDOperand PPCTargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
4096 // Depths > 0 not supported yet!
4097 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
4098 return SDOperand();
4099
4100 MachineFunction &MF = DAG.getMachineFunction();
4101 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
4102 int RAIdx = FuncInfo->getReturnAddrSaveIndex();
4103 if (RAIdx == 0) {
4104 bool isPPC64 = PPCSubTarget.isPPC64();
4105 int Offset =
4106 PPCFrameInfo::getReturnSaveOffset(isPPC64, PPCSubTarget.isMachoABI());
4107
4108 // Set up a frame object for the return address.
4109 RAIdx = MF.getFrameInfo()->CreateFixedObject(isPPC64 ? 8 : 4, Offset);
4110
4111 // Remember it for next time.
4112 FuncInfo->setReturnAddrSaveIndex(RAIdx);
4113
4114 // Make sure the function really does not optimize away the store of the RA
4115 // to the stack.
4116 FuncInfo->setLRStoreRequired();
4117 }
4118
4119 // Just load the return address off the stack.
4120 SDOperand RetAddrFI = DAG.getFrameIndex(RAIdx, getPointerTy());
4121 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
4122}
4123
4124SDOperand PPCTargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
Nicolas Geoffray43c6e7c2007-03-01 13:11:38 +00004125 // Depths > 0 not supported yet!
4126 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
4127 return SDOperand();
4128
4129 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
4130 bool isPPC64 = PtrVT == MVT::i64;
4131
4132 MachineFunction &MF = DAG.getMachineFunction();
4133 MachineFrameInfo *MFI = MF.getFrameInfo();
4134 bool is31 = (NoFramePointerElim || MFI->hasVarSizedObjects())
4135 && MFI->getStackSize();
4136
4137 if (isPPC64)
4138 return DAG.getCopyFromReg(DAG.getEntryNode(), is31 ? PPC::X31 : PPC::X1,
Bill Wendlingb8a80f02007-08-30 00:59:19 +00004139 MVT::i64);
Nicolas Geoffray43c6e7c2007-03-01 13:11:38 +00004140 else
4141 return DAG.getCopyFromReg(DAG.getEntryNode(), is31 ? PPC::R31 : PPC::R1,
4142 MVT::i32);
4143}