blob: 258de780eb6ef9a474bd2b65eacc4b2cd8b27192 [file] [log] [blame]
Nate Begeman1d9d7422005-10-18 00:28:58 +00001//===-- PPCISelLowering.cpp - PPC DAG Lowering Implementation -------------===//
Chris Lattner7c5a3d32005-08-16 17:14:42 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Nate Begeman21e463b2005-10-16 05:39:50 +000010// This file implements the PPCISelLowering class.
Chris Lattner7c5a3d32005-08-16 17:14:42 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner16e71f22005-10-14 23:59:06 +000014#include "PPCISelLowering.h"
15#include "PPCTargetMachine.h"
Nate Begeman750ac1b2006-02-01 07:19:44 +000016#include "llvm/ADT/VectorExtras.h"
Evan Chengc4c62572006-03-13 23:20:37 +000017#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000018#include "llvm/CodeGen/MachineFrameInfo.h"
19#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner8a2d3ca2005-08-26 21:23:58 +000020#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000021#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner7b738342005-09-13 19:33:40 +000022#include "llvm/CodeGen/SSARegMap.h"
Chris Lattner0b1e4e52005-08-26 17:36:52 +000023#include "llvm/Constants.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000024#include "llvm/Function.h"
Nate Begeman750ac1b2006-02-01 07:19:44 +000025#include "llvm/Support/MathExtras.h"
Evan Chengd2ee2182006-02-18 00:08:58 +000026#include "llvm/Target/TargetOptions.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000027using namespace llvm;
28
Nate Begeman21e463b2005-10-16 05:39:50 +000029PPCTargetLowering::PPCTargetLowering(TargetMachine &TM)
Chris Lattner7c5a3d32005-08-16 17:14:42 +000030 : TargetLowering(TM) {
31
32 // Fold away setcc operations if possible.
33 setSetCCIsExpensive();
Nate Begeman405e3ec2005-10-21 00:02:42 +000034 setPow2DivIsCheap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +000035
Chris Lattnerd145a612005-09-27 22:18:25 +000036 // Use _setjmp/_longjmp instead of setjmp/longjmp.
37 setUseUnderscoreSetJmpLongJmp(true);
38
Chris Lattner7c5a3d32005-08-16 17:14:42 +000039 // Set up the register classes.
Nate Begeman1d9d7422005-10-18 00:28:58 +000040 addRegisterClass(MVT::i32, PPC::GPRCRegisterClass);
41 addRegisterClass(MVT::f32, PPC::F4RCRegisterClass);
42 addRegisterClass(MVT::f64, PPC::F8RCRegisterClass);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000043
Chris Lattnera54aa942006-01-29 06:26:08 +000044 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
45 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
46
Chris Lattner7c5a3d32005-08-16 17:14:42 +000047 // PowerPC has no intrinsics for these particular operations
48 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
49 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
50 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
51
52 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
53 setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
54 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
55
56 // PowerPC has no SREM/UREM instructions
57 setOperationAction(ISD::SREM, MVT::i32, Expand);
58 setOperationAction(ISD::UREM, MVT::i32, Expand);
59
60 // We don't support sin/cos/sqrt/fmod
61 setOperationAction(ISD::FSIN , MVT::f64, Expand);
62 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattner615c2d02005-09-28 22:29:58 +000063 setOperationAction(ISD::FREM , MVT::f64, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000064 setOperationAction(ISD::FSIN , MVT::f32, Expand);
65 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattner615c2d02005-09-28 22:29:58 +000066 setOperationAction(ISD::FREM , MVT::f32, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000067
68 // If we're enabling GP optimizations, use hardware square root
Chris Lattner1e9de3e2005-09-02 18:33:05 +000069 if (!TM.getSubtarget<PPCSubtarget>().hasFSQRT()) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +000070 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
71 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
72 }
73
Chris Lattner9601a862006-03-05 05:08:37 +000074 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
75 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
76
Nate Begemand88fc032006-01-14 03:14:10 +000077 // PowerPC does not have BSWAP, CTPOP or CTTZ
78 setOperationAction(ISD::BSWAP, MVT::i32 , Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000079 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
80 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
81
Nate Begeman35ef9132006-01-11 21:21:00 +000082 // PowerPC does not have ROTR
83 setOperationAction(ISD::ROTR, MVT::i32 , Expand);
84
Chris Lattner7c5a3d32005-08-16 17:14:42 +000085 // PowerPC does not have Select
86 setOperationAction(ISD::SELECT, MVT::i32, Expand);
87 setOperationAction(ISD::SELECT, MVT::f32, Expand);
88 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +000089
Chris Lattner0b1e4e52005-08-26 17:36:52 +000090 // PowerPC wants to turn select_cc of FP into fsel when possible.
91 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
92 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Nate Begeman44775902006-01-31 08:17:29 +000093
Nate Begeman750ac1b2006-02-01 07:19:44 +000094 // PowerPC wants to optimize integer setcc a bit
Nate Begeman44775902006-01-31 08:17:29 +000095 setOperationAction(ISD::SETCC, MVT::i32, Custom);
Chris Lattnereb9b62e2005-08-31 19:09:57 +000096
Nate Begeman81e80972006-03-17 01:40:33 +000097 // PowerPC does not have BRCOND which requires SetCC
98 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000099
Chris Lattnerf7605322005-08-31 21:09:52 +0000100 // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
101 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000102
Jim Laskeyad23c9d2005-08-17 00:40:22 +0000103 // PowerPC does not have [U|S]INT_TO_FP
104 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
105 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
106
Chris Lattner53e88452005-12-23 05:13:35 +0000107 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
108 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
109
Chris Lattnere6ec9f22005-09-10 00:21:06 +0000110 // PowerPC does not have truncstore for i1.
111 setOperationAction(ISD::TRUNCSTORE, MVT::i1, Promote);
Chris Lattnerf73bae12005-11-29 06:16:21 +0000112
Jim Laskeyabf6d172006-01-05 01:25:28 +0000113 // Support label based line numbers.
Chris Lattnerf73bae12005-11-29 06:16:21 +0000114 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000115 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000116 // FIXME - use subtarget debug flags
Jim Laskeye0bce712006-01-05 01:47:43 +0000117 if (!TM.getSubtarget<PPCSubtarget>().isDarwin())
Jim Laskeyabf6d172006-01-05 01:25:28 +0000118 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattnere6ec9f22005-09-10 00:21:06 +0000119
Nate Begeman28a6b022005-12-10 02:36:00 +0000120 // We want to legalize GlobalAddress and ConstantPool nodes into the
121 // appropriate instructions to materialize the address.
Chris Lattner3eef4e32005-11-17 18:26:56 +0000122 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Nate Begeman28a6b022005-12-10 02:36:00 +0000123 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000124
Nate Begemanee625572006-01-27 21:09:22 +0000125 // RET must be custom lowered, to meet ABI requirements
126 setOperationAction(ISD::RET , MVT::Other, Custom);
127
Nate Begemanacc398c2006-01-25 18:21:52 +0000128 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
129 setOperationAction(ISD::VASTART , MVT::Other, Custom);
130
Chris Lattnerb22c08b2006-01-15 09:02:48 +0000131 // Use the default implementation.
Nate Begemanacc398c2006-01-25 18:21:52 +0000132 setOperationAction(ISD::VAARG , MVT::Other, Expand);
133 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
134 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Chris Lattnerb22c08b2006-01-15 09:02:48 +0000135 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
136 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
137 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Chris Lattner860e8862005-11-17 07:30:41 +0000138
Nate Begemanc09eeec2005-09-06 22:03:27 +0000139 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000140 // They also have instructions for converting between i64 and fp.
Nate Begemanc09eeec2005-09-06 22:03:27 +0000141 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
142 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Nate Begemanae749a92005-10-25 23:48:36 +0000143 // To take advantage of the above i64 FP_TO_SINT, promote i32 FP_TO_UINT
144 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Promote);
145 } else {
Chris Lattner860e8862005-11-17 07:30:41 +0000146 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Nate Begemanae749a92005-10-25 23:48:36 +0000147 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begeman9d2b8172005-10-18 00:56:42 +0000148 }
149
150 if (TM.getSubtarget<PPCSubtarget>().has64BitRegs()) {
151 // 64 bit PowerPC implementations can support i64 types directly
152 addRegisterClass(MVT::i64, PPC::G8RCRegisterClass);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000153 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
154 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000155 } else {
156 // 32 bit PowerPC wants to expand i64 shifts itself.
157 setOperationAction(ISD::SHL, MVT::i64, Custom);
158 setOperationAction(ISD::SRL, MVT::i64, Custom);
159 setOperationAction(ISD::SRA, MVT::i64, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000160 }
161
Evan Chengd30bf012006-03-01 01:11:20 +0000162 // First set operation action for all vector types to expand. Then we
163 // will selectively turn on ones that can be effectively codegen'd.
164 for (unsigned VT = (unsigned)MVT::Vector + 1;
165 VT != (unsigned)MVT::LAST_VALUETYPE; VT++) {
166 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
167 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
168 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
169 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000170 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
171
172 // FIXME: We don't support any BUILD_VECTOR's yet. We should custom expand
173 // the ones we do, like splat(0.0) and splat(-0.0).
174 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000175 }
176
Nate Begeman425a9692005-11-29 08:17:20 +0000177 if (TM.getSubtarget<PPCSubtarget>().hasAltivec()) {
Nate Begeman425a9692005-11-29 08:17:20 +0000178 addRegisterClass(MVT::v4f32, PPC::VRRCRegisterClass);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000179 addRegisterClass(MVT::v4i32, PPC::VRRCRegisterClass);
Chris Lattnerec4a0c72006-01-29 06:32:58 +0000180
Evan Chengd30bf012006-03-01 01:11:20 +0000181 setOperationAction(ISD::ADD , MVT::v4f32, Legal);
182 setOperationAction(ISD::SUB , MVT::v4f32, Legal);
183 setOperationAction(ISD::MUL , MVT::v4f32, Legal);
184 setOperationAction(ISD::LOAD , MVT::v4f32, Legal);
185 setOperationAction(ISD::ADD , MVT::v4i32, Legal);
186 setOperationAction(ISD::LOAD , MVT::v4i32, Legal);
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000187 setOperationAction(ISD::LOAD , MVT::v16i8, Legal);
188
189 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i32, Custom);
190 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
191
Chris Lattnerb2177b92006-03-19 06:55:52 +0000192 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom);
193 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Custom);
Nate Begeman425a9692005-11-29 08:17:20 +0000194 }
195
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000196 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattnercadd7422006-01-13 17:52:03 +0000197 setStackPointerRegisterToSaveRestore(PPC::R1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000198
Chris Lattner8c13d0a2006-03-01 04:57:39 +0000199 // We have target-specific dag combine patterns for the following nodes:
200 setTargetDAGCombine(ISD::SINT_TO_FP);
Chris Lattner51269842006-03-01 05:50:56 +0000201 setTargetDAGCombine(ISD::STORE);
Chris Lattner8c13d0a2006-03-01 04:57:39 +0000202
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000203 computeRegisterProperties();
204}
205
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000206const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
207 switch (Opcode) {
208 default: return 0;
209 case PPCISD::FSEL: return "PPCISD::FSEL";
210 case PPCISD::FCFID: return "PPCISD::FCFID";
211 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
212 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
Chris Lattner51269842006-03-01 05:50:56 +0000213 case PPCISD::STFIWX: return "PPCISD::STFIWX";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000214 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
215 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
Chris Lattnerb2177b92006-03-19 06:55:52 +0000216 case PPCISD::LVE_X: return "PPCISD::LVE_X";
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000217 case PPCISD::VPERM: return "PPCISD::VPERM";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000218 case PPCISD::Hi: return "PPCISD::Hi";
219 case PPCISD::Lo: return "PPCISD::Lo";
220 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
221 case PPCISD::SRL: return "PPCISD::SRL";
222 case PPCISD::SRA: return "PPCISD::SRA";
223 case PPCISD::SHL: return "PPCISD::SHL";
Chris Lattnere00ebf02006-01-28 07:33:03 +0000224 case PPCISD::CALL: return "PPCISD::CALL";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000225 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
226 }
227}
228
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000229/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
230static bool isFloatingPointZero(SDOperand Op) {
231 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
232 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
233 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
234 // Maybe this has already been legalized into the constant pool?
235 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
236 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
237 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
238 }
239 return false;
240}
241
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000242/// LowerOperation - Provide custom lowering hooks for some operations.
243///
Nate Begeman21e463b2005-10-16 05:39:50 +0000244SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000245 switch (Op.getOpcode()) {
246 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattnerf7605322005-08-31 21:09:52 +0000247 case ISD::FP_TO_SINT: {
Nate Begemanc09eeec2005-09-06 22:03:27 +0000248 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
Chris Lattner7c0d6642005-10-02 06:37:13 +0000249 SDOperand Src = Op.getOperand(0);
250 if (Src.getValueType() == MVT::f32)
251 Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src);
252
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000253 SDOperand Tmp;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000254 switch (Op.getValueType()) {
255 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
256 case MVT::i32:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000257 Tmp = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000258 break;
259 case MVT::i64:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000260 Tmp = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000261 break;
262 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000263
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000264 // Convert the FP value to an int value through memory.
265 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Tmp);
266 if (Op.getValueType() == MVT::i32)
267 Bits = DAG.getNode(ISD::TRUNCATE, MVT::i32, Bits);
268 return Bits;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000269 }
270 case ISD::SINT_TO_FP: {
271 assert(MVT::i64 == Op.getOperand(0).getValueType() &&
272 "Unhandled SINT_TO_FP type in custom expander!");
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000273 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, Op.getOperand(0));
274 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Bits);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000275 if (MVT::f32 == Op.getValueType())
276 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
277 return FP;
Chris Lattnerf7605322005-08-31 21:09:52 +0000278 }
279 case ISD::SELECT_CC: {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000280 // Turn FP only select_cc's into fsel instructions.
Chris Lattnerf7605322005-08-31 21:09:52 +0000281 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
282 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
283 break;
284
285 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
286
287 // Cannot handle SETEQ/SETNE.
288 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
289
290 MVT::ValueType ResVT = Op.getValueType();
291 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
292 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
293 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000294
Chris Lattnerf7605322005-08-31 21:09:52 +0000295 // If the RHS of the comparison is a 0.0, we don't need to do the
296 // subtraction at all.
297 if (isFloatingPointZero(RHS))
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000298 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000299 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000300 case ISD::SETULT:
301 case ISD::SETLT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000302 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000303 case ISD::SETUGE:
304 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000305 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
306 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattnerf7605322005-08-31 21:09:52 +0000307 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000308 case ISD::SETUGT:
309 case ISD::SETGT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000310 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000311 case ISD::SETULE:
312 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000313 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
314 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattner0bbea952005-08-26 20:25:03 +0000315 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattner85fd97d2005-10-26 18:01:11 +0000316 DAG.getNode(ISD::FNEG, MVT::f64, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000317 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000318
Chris Lattnereb255f22005-10-25 20:54:57 +0000319 SDOperand Cmp;
Chris Lattnerf7605322005-08-31 21:09:52 +0000320 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000321 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnerf7605322005-08-31 21:09:52 +0000322 case ISD::SETULT:
323 case ISD::SETLT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000324 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
325 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
326 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
327 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000328 case ISD::SETUGE:
329 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000330 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
331 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
332 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
333 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000334 case ISD::SETUGT:
335 case ISD::SETGT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000336 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
337 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
338 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
339 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000340 case ISD::SETULE:
341 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000342 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
343 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
344 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
345 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000346 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000347 break;
348 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000349 case ISD::SHL: {
350 assert(Op.getValueType() == MVT::i64 &&
351 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
352 // The generic code does a fine job expanding shift by a constant.
353 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
354
355 // Otherwise, expand into a bunch of logical ops. Note that these ops
356 // depend on the PPC behavior for oversized shift amounts.
357 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
358 DAG.getConstant(0, MVT::i32));
359 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
360 DAG.getConstant(1, MVT::i32));
361 SDOperand Amt = Op.getOperand(1);
362
363 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
364 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000365 SDOperand Tmp2 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Amt);
366 SDOperand Tmp3 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000367 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
368 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
369 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000370 SDOperand Tmp6 = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000371 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000372 SDOperand OutLo = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000373 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
374 }
375 case ISD::SRL: {
376 assert(Op.getValueType() == MVT::i64 &&
377 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
378 // The generic code does a fine job expanding shift by a constant.
379 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
380
381 // Otherwise, expand into a bunch of logical ops. Note that these ops
382 // depend on the PPC behavior for oversized shift amounts.
383 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
384 DAG.getConstant(0, MVT::i32));
385 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
386 DAG.getConstant(1, MVT::i32));
387 SDOperand Amt = Op.getOperand(1);
388
389 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
390 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000391 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
392 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000393 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
394 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
395 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000396 SDOperand Tmp6 = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000397 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000398 SDOperand OutHi = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000399 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
400 }
401 case ISD::SRA: {
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000402 assert(Op.getValueType() == MVT::i64 &&
403 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
404 // The generic code does a fine job expanding shift by a constant.
405 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
406
407 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
408 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
409 DAG.getConstant(0, MVT::i32));
410 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
411 DAG.getConstant(1, MVT::i32));
412 SDOperand Amt = Op.getOperand(1);
413
414 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
415 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000416 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
417 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000418 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
419 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
420 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000421 SDOperand Tmp6 = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Tmp5);
422 SDOperand OutHi = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Amt);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000423 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
424 Tmp4, Tmp6, ISD::SETLE);
425 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000426 }
Nate Begeman28a6b022005-12-10 02:36:00 +0000427 case ISD::ConstantPool: {
Evan Chengb8973bd2006-01-31 22:23:14 +0000428 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
429 Constant *C = CP->get();
430 SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i32, CP->getAlignment());
Nate Begeman28a6b022005-12-10 02:36:00 +0000431 SDOperand Zero = DAG.getConstant(0, MVT::i32);
432
Evan Cheng4c1aa862006-02-22 20:19:42 +0000433 if (getTargetMachine().getRelocationModel() == Reloc::Static) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000434 // Generate non-pic code that has direct accesses to the constant pool.
435 // The address of the global is just (hi(&g)+lo(&g)).
436 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
437 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
438 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
439 }
440
441 // Only lower ConstantPool on Darwin.
442 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
443 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
Evan Cheng4c1aa862006-02-22 20:19:42 +0000444 if (getTargetMachine().getRelocationModel() == Reloc::PIC) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000445 // With PIC, the first instruction is actually "GR+hi(&G)".
446 Hi = DAG.getNode(ISD::ADD, MVT::i32,
447 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
448 }
449
450 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
451 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
452 return Lo;
453 }
Chris Lattner860e8862005-11-17 07:30:41 +0000454 case ISD::GlobalAddress: {
Nate Begeman50fb3c42005-12-24 01:00:15 +0000455 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
456 GlobalValue *GV = GSDN->getGlobal();
457 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32, GSDN->getOffset());
Chris Lattner860e8862005-11-17 07:30:41 +0000458 SDOperand Zero = DAG.getConstant(0, MVT::i32);
Chris Lattner1d05cb42005-11-17 18:55:48 +0000459
Evan Cheng4c1aa862006-02-22 20:19:42 +0000460 if (getTargetMachine().getRelocationModel() == Reloc::Static) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000461 // Generate non-pic code that has direct accesses to globals.
462 // The address of the global is just (hi(&g)+lo(&g)).
Chris Lattner1d05cb42005-11-17 18:55:48 +0000463 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
464 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
465 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
466 }
Chris Lattner860e8862005-11-17 07:30:41 +0000467
Chris Lattner1d05cb42005-11-17 18:55:48 +0000468 // Only lower GlobalAddress on Darwin.
469 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
Chris Lattnera35ef632006-01-06 01:04:03 +0000470
Chris Lattner860e8862005-11-17 07:30:41 +0000471 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
Evan Cheng4c1aa862006-02-22 20:19:42 +0000472 if (getTargetMachine().getRelocationModel() == Reloc::PIC) {
Chris Lattner860e8862005-11-17 07:30:41 +0000473 // With PIC, the first instruction is actually "GR+hi(&G)".
474 Hi = DAG.getNode(ISD::ADD, MVT::i32,
Chris Lattner15666132005-11-17 17:51:38 +0000475 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
Chris Lattner860e8862005-11-17 07:30:41 +0000476 }
477
478 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
479 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
480
Chris Lattner37dd6f12006-01-29 20:49:17 +0000481 if (!GV->hasWeakLinkage() && !GV->hasLinkOnceLinkage() &&
482 (!GV->isExternal() || GV->hasNotBeenReadFromBytecode()))
Chris Lattner860e8862005-11-17 07:30:41 +0000483 return Lo;
484
485 // If the global is weak or external, we have to go through the lazy
486 // resolution stub.
487 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), Lo, DAG.getSrcValue(0));
488 }
Nate Begeman44775902006-01-31 08:17:29 +0000489 case ISD::SETCC: {
490 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Nate Begeman750ac1b2006-02-01 07:19:44 +0000491
492 // If we're comparing for equality to zero, expose the fact that this is
493 // implented as a ctlz/srl pair on ppc, so that the dag combiner can
494 // fold the new nodes.
495 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
496 if (C->isNullValue() && CC == ISD::SETEQ) {
497 MVT::ValueType VT = Op.getOperand(0).getValueType();
498 SDOperand Zext = Op.getOperand(0);
499 if (VT < MVT::i32) {
500 VT = MVT::i32;
501 Zext = DAG.getNode(ISD::ZERO_EXTEND, VT, Op.getOperand(0));
502 }
503 unsigned Log2b = Log2_32(MVT::getSizeInBits(VT));
504 SDOperand Clz = DAG.getNode(ISD::CTLZ, VT, Zext);
505 SDOperand Scc = DAG.getNode(ISD::SRL, VT, Clz,
506 DAG.getConstant(Log2b, getShiftAmountTy()));
507 return DAG.getNode(ISD::TRUNCATE, getSetCCResultTy(), Scc);
508 }
509 // Leave comparisons against 0 and -1 alone for now, since they're usually
510 // optimized. FIXME: revisit this when we can custom lower all setcc
511 // optimizations.
512 if (C->isAllOnesValue() || C->isNullValue())
513 break;
514 }
515
516 // If we have an integer seteq/setne, turn it into a compare against zero
517 // by subtracting the rhs from the lhs, which is faster than setting a
518 // condition register, reading it back out, and masking the correct bit.
519 MVT::ValueType LHSVT = Op.getOperand(0).getValueType();
520 if (MVT::isInteger(LHSVT) && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
521 MVT::ValueType VT = Op.getValueType();
522 SDOperand Sub = DAG.getNode(ISD::SUB, LHSVT, Op.getOperand(0),
523 Op.getOperand(1));
524 return DAG.getSetCC(VT, Sub, DAG.getConstant(0, LHSVT), CC);
525 }
Nate Begeman44775902006-01-31 08:17:29 +0000526 break;
527 }
Nate Begemanacc398c2006-01-25 18:21:52 +0000528 case ISD::VASTART: {
529 // vastart just stores the address of the VarArgsFrameIndex slot into the
530 // memory location argument.
531 // FIXME: Replace MVT::i32 with PointerTy
532 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
533 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
534 Op.getOperand(1), Op.getOperand(2));
535 }
Nate Begemanee625572006-01-27 21:09:22 +0000536 case ISD::RET: {
537 SDOperand Copy;
538
539 switch(Op.getNumOperands()) {
540 default:
541 assert(0 && "Do not know how to return this many arguments!");
542 abort();
543 case 1:
544 return SDOperand(); // ret void is legal
545 case 2: {
546 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
547 unsigned ArgReg = MVT::isInteger(ArgVT) ? PPC::R3 : PPC::F1;
548 Copy = DAG.getCopyToReg(Op.getOperand(0), ArgReg, Op.getOperand(1),
549 SDOperand());
550 break;
551 }
552 case 3:
553 Copy = DAG.getCopyToReg(Op.getOperand(0), PPC::R3, Op.getOperand(2),
554 SDOperand());
555 Copy = DAG.getCopyToReg(Copy, PPC::R4, Op.getOperand(1),Copy.getValue(1));
556 break;
557 }
558 return DAG.getNode(PPCISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
559 }
Chris Lattnerb2177b92006-03-19 06:55:52 +0000560 case ISD::SCALAR_TO_VECTOR: {
561 // Create a stack slot that is 16-byte aligned.
562 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
563 int FrameIdx = FrameInfo->CreateStackObject(16, 16);
564 SDOperand FIdx = DAG.getFrameIndex(FrameIdx, MVT::i32);
565
566 // Store the input value into Value#0 of the stack slot.
567 unsigned InSize = MVT::getSizeInBits(Op.getOperand(0).getValueType())/8;
568 FIdx = DAG.getNode(ISD::ADD, MVT::i32, FIdx,
569 DAG.getConstant(16-InSize, MVT::i32));
570 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
571 Op.getOperand(0), FIdx,DAG.getSrcValue(NULL));
572 return DAG.getNode(PPCISD::LVE_X, Op.getValueType(), Store, FIdx,
573 DAG.getSrcValue(NULL));
574 }
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000575 case ISD::VECTOR_SHUFFLE: {
576 // FIXME: Cases that are handled by instructions that take permute
577 // immediates (such as vsplt*) shouldn't be lowered here! Also handle cases
578 // that are cheaper to do as multiple such instructions than as a constant
579 // pool load/vperm pair.
580
581 // Lower this to a VPERM(V1, V2, V3) expression, where V3 is a constant
582 // vector that will get spilled to the constant pool.
583 SDOperand V1 = Op.getOperand(0);
584 SDOperand V2 = Op.getOperand(1);
585 if (V2.getOpcode() == ISD::UNDEF) V2 = V1;
586 SDOperand PermMask = Op.getOperand(2);
587
588 // The SHUFFLE_VECTOR mask is almost exactly what we want for vperm, except
589 // that it is in input element units, not in bytes. Convert now.
590 MVT::ValueType EltVT = MVT::getVectorBaseType(V1.getValueType());
591 unsigned BytesPerElement = MVT::getSizeInBits(EltVT)/8;
592
593 std::vector<SDOperand> ResultMask;
594 for (unsigned i = 0, e = PermMask.getNumOperands(); i != e; ++i) {
595 unsigned SrcElt =cast<ConstantSDNode>(PermMask.getOperand(i))->getValue();
596
597 for (unsigned j = 0; j != BytesPerElement; ++j)
598 ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement+j,
599 MVT::i8));
600 }
601
602 SDOperand VPermMask =DAG.getNode(ISD::BUILD_VECTOR, MVT::v16i8, ResultMask);
603 return DAG.getNode(PPCISD::VPERM, V1.getValueType(), V1, V2, VPermMask);
604 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000605 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000606 return SDOperand();
607}
608
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000609std::vector<SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000610PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000611 //
612 // add beautiful description of PPC stack frame format, or at least some docs
613 //
614 MachineFunction &MF = DAG.getMachineFunction();
615 MachineFrameInfo *MFI = MF.getFrameInfo();
616 MachineBasicBlock& BB = MF.front();
Chris Lattner7b738342005-09-13 19:33:40 +0000617 SSARegMap *RegMap = MF.getSSARegMap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000618 std::vector<SDOperand> ArgValues;
619
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000620 unsigned ArgOffset = 24;
621 unsigned GPR_remaining = 8;
622 unsigned FPR_remaining = 13;
623 unsigned GPR_idx = 0, FPR_idx = 0;
624 static const unsigned GPR[] = {
625 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
626 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
627 };
628 static const unsigned FPR[] = {
629 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
630 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
631 };
632
633 // Add DAG nodes to load the arguments... On entry to a function on PPC,
634 // the arguments start at offset 24, although they are likely to be passed
635 // in registers.
636 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
637 SDOperand newroot, argt;
638 unsigned ObjSize;
639 bool needsLoad = false;
640 bool ArgLive = !I->use_empty();
641 MVT::ValueType ObjectVT = getValueType(I->getType());
642
643 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000644 default: assert(0 && "Unhandled argument type!");
645 case MVT::i1:
646 case MVT::i8:
647 case MVT::i16:
648 case MVT::i32:
649 ObjSize = 4;
650 if (!ArgLive) break;
651 if (GPR_remaining > 0) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000652 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000653 MF.addLiveIn(GPR[GPR_idx], VReg);
654 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +0000655 if (ObjectVT != MVT::i32) {
656 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
657 : ISD::AssertZext;
658 argt = DAG.getNode(AssertOp, MVT::i32, argt,
659 DAG.getValueType(ObjectVT));
660 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
661 }
Chris Lattner915fb302005-08-30 00:19:00 +0000662 } else {
663 needsLoad = true;
664 }
665 break;
Chris Lattner80720a92005-11-30 20:40:54 +0000666 case MVT::i64:
667 ObjSize = 8;
Chris Lattner915fb302005-08-30 00:19:00 +0000668 if (!ArgLive) break;
669 if (GPR_remaining > 0) {
670 SDOperand argHi, argLo;
Nate Begeman1d9d7422005-10-18 00:28:58 +0000671 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000672 MF.addLiveIn(GPR[GPR_idx], VReg);
673 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000674 // If we have two or more remaining argument registers, then both halves
675 // of the i64 can be sourced from there. Otherwise, the lower half will
676 // have to come off the stack. This can happen when an i64 is preceded
677 // by 28 bytes of arguments.
678 if (GPR_remaining > 1) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000679 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000680 MF.addLiveIn(GPR[GPR_idx+1], VReg);
681 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000682 } else {
683 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
684 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
685 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
686 DAG.getSrcValue(NULL));
687 }
688 // Build the outgoing arg thingy
689 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
690 newroot = argLo;
691 } else {
692 needsLoad = true;
693 }
694 break;
695 case MVT::f32:
696 case MVT::f64:
697 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
Chris Lattner413b9792006-01-11 18:21:25 +0000698 if (!ArgLive) {
699 if (FPR_remaining > 0) {
700 --FPR_remaining;
701 ++FPR_idx;
702 }
703 break;
704 }
Chris Lattner915fb302005-08-30 00:19:00 +0000705 if (FPR_remaining > 0) {
Chris Lattner919c0322005-10-01 01:35:02 +0000706 unsigned VReg;
707 if (ObjectVT == MVT::f32)
Nate Begeman1d9d7422005-10-18 00:28:58 +0000708 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner919c0322005-10-01 01:35:02 +0000709 else
Nate Begeman1d9d7422005-10-18 00:28:58 +0000710 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000711 MF.addLiveIn(FPR[FPR_idx], VReg);
712 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
Chris Lattner915fb302005-08-30 00:19:00 +0000713 --FPR_remaining;
714 ++FPR_idx;
715 } else {
716 needsLoad = true;
717 }
718 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000719 }
720
721 // We need to load the argument to a virtual register if we determined above
722 // that we ran out of physical registers of the appropriate type
723 if (needsLoad) {
724 unsigned SubregOffset = 0;
725 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
726 if (ObjectVT == MVT::i16) SubregOffset = 2;
727 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
728 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
729 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
730 DAG.getConstant(SubregOffset, MVT::i32));
731 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
732 DAG.getSrcValue(NULL));
733 }
734
735 // Every 4 bytes of argument space consumes one of the GPRs available for
736 // argument passing.
737 if (GPR_remaining > 0) {
738 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
739 GPR_remaining -= delta;
740 GPR_idx += delta;
741 }
742 ArgOffset += ObjSize;
743 if (newroot.Val)
744 DAG.setRoot(newroot.getValue(1));
745
746 ArgValues.push_back(argt);
747 }
748
749 // If the function takes variable number of arguments, make a frame index for
750 // the start of the first vararg value... for expansion of llvm.va_start.
751 if (F.isVarArg()) {
752 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
753 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
754 // If this function is vararg, store any remaining integer argument regs
755 // to their spots on the stack so that they may be loaded by deferencing the
756 // result of va_next.
757 std::vector<SDOperand> MemOps;
758 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000759 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000760 MF.addLiveIn(GPR[GPR_idx], VReg);
761 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000762 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
763 Val, FIN, DAG.getSrcValue(NULL));
764 MemOps.push_back(Store);
765 // Increment the address by four for the next argument to store
766 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
767 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
768 }
Chris Lattner80720a92005-11-30 20:40:54 +0000769 if (!MemOps.empty()) {
770 MemOps.push_back(DAG.getRoot());
771 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
772 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000773 }
774
775 // Finally, inform the code generator which regs we return values in.
776 switch (getValueType(F.getReturnType())) {
777 default: assert(0 && "Unknown type!");
778 case MVT::isVoid: break;
779 case MVT::i1:
780 case MVT::i8:
781 case MVT::i16:
782 case MVT::i32:
783 MF.addLiveOut(PPC::R3);
784 break;
785 case MVT::i64:
786 MF.addLiveOut(PPC::R3);
787 MF.addLiveOut(PPC::R4);
788 break;
789 case MVT::f32:
790 case MVT::f64:
791 MF.addLiveOut(PPC::F1);
792 break;
793 }
794
795 return ArgValues;
796}
797
798std::pair<SDOperand, SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000799PPCTargetLowering::LowerCallTo(SDOperand Chain,
800 const Type *RetTy, bool isVarArg,
801 unsigned CallingConv, bool isTailCall,
802 SDOperand Callee, ArgListTy &Args,
803 SelectionDAG &DAG) {
Chris Lattner281b55e2006-01-27 23:34:02 +0000804 // args_to_use will accumulate outgoing args for the PPCISD::CALL case in
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000805 // SelectExpr to use to put the arguments in the appropriate registers.
806 std::vector<SDOperand> args_to_use;
807
808 // Count how many bytes are to be pushed on the stack, including the linkage
809 // area, and parameter passing area.
810 unsigned NumBytes = 24;
811
812 if (Args.empty()) {
Chris Lattner45b39762006-02-13 08:55:29 +0000813 Chain = DAG.getCALLSEQ_START(Chain,
814 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000815 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000816 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000817 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +0000818 default: assert(0 && "Unknown value type!");
819 case MVT::i1:
820 case MVT::i8:
821 case MVT::i16:
822 case MVT::i32:
823 case MVT::f32:
824 NumBytes += 4;
825 break;
826 case MVT::i64:
827 case MVT::f64:
828 NumBytes += 8;
829 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000830 }
Chris Lattner915fb302005-08-30 00:19:00 +0000831 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000832
Chris Lattner915fb302005-08-30 00:19:00 +0000833 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
834 // plus 32 bytes of argument space in case any called code gets funky on us.
835 // (Required by ABI to support var arg)
836 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000837
838 // Adjust the stack pointer for the new arguments...
839 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattner45b39762006-02-13 08:55:29 +0000840 Chain = DAG.getCALLSEQ_START(Chain,
841 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000842
843 // Set up a copy of the stack pointer for use loading and storing any
844 // arguments that may not fit in the registers available for argument
845 // passing.
Chris Lattnera243db82006-01-11 19:55:07 +0000846 SDOperand StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000847
848 // Figure out which arguments are going to go in registers, and which in
849 // memory. Also, if this is a vararg function, floating point operations
850 // must be stored to our stack, and loaded into integer regs as well, if
851 // any integer regs are available for argument passing.
852 unsigned ArgOffset = 24;
853 unsigned GPR_remaining = 8;
854 unsigned FPR_remaining = 13;
855
856 std::vector<SDOperand> MemOps;
857 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
858 // PtrOff will be used to store the current argument to the stack if a
859 // register cannot be found for it.
860 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
861 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
862 MVT::ValueType ArgVT = getValueType(Args[i].second);
863
864 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000865 default: assert(0 && "Unexpected ValueType for argument!");
866 case MVT::i1:
867 case MVT::i8:
868 case MVT::i16:
869 // Promote the integer to 32 bits. If the input type is signed use a
870 // sign extend, otherwise use a zero extend.
871 if (Args[i].second->isSigned())
872 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
873 else
874 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
875 // FALL THROUGH
876 case MVT::i32:
877 if (GPR_remaining > 0) {
878 args_to_use.push_back(Args[i].first);
879 --GPR_remaining;
880 } else {
881 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
882 Args[i].first, PtrOff,
883 DAG.getSrcValue(NULL)));
884 }
885 ArgOffset += 4;
886 break;
887 case MVT::i64:
888 // If we have one free GPR left, we can place the upper half of the i64
889 // in it, and store the other half to the stack. If we have two or more
890 // free GPRs, then we can pass both halves of the i64 in registers.
891 if (GPR_remaining > 0) {
892 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
893 Args[i].first, DAG.getConstant(1, MVT::i32));
894 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
895 Args[i].first, DAG.getConstant(0, MVT::i32));
896 args_to_use.push_back(Hi);
897 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000898 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000899 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000900 --GPR_remaining;
901 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000902 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
903 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000904 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +0000905 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000906 }
Chris Lattner915fb302005-08-30 00:19:00 +0000907 } else {
908 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
909 Args[i].first, PtrOff,
910 DAG.getSrcValue(NULL)));
911 }
912 ArgOffset += 8;
913 break;
914 case MVT::f32:
915 case MVT::f64:
916 if (FPR_remaining > 0) {
917 args_to_use.push_back(Args[i].first);
918 --FPR_remaining;
919 if (isVarArg) {
920 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
921 Args[i].first, PtrOff,
922 DAG.getSrcValue(NULL));
923 MemOps.push_back(Store);
924 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000925 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000926 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
927 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000928 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000929 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000930 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +0000931 }
932 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000933 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
934 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +0000935 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
936 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000937 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000938 args_to_use.push_back(Load);
939 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000940 }
941 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000942 // If we have any FPRs remaining, we may also have GPRs remaining.
943 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
944 // GPRs.
945 if (GPR_remaining > 0) {
946 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
947 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000948 }
Chris Lattner915fb302005-08-30 00:19:00 +0000949 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
950 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
951 --GPR_remaining;
952 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000953 }
Chris Lattner915fb302005-08-30 00:19:00 +0000954 } else {
955 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
956 Args[i].first, PtrOff,
957 DAG.getSrcValue(NULL)));
958 }
959 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
960 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000961 }
962 }
963 if (!MemOps.empty())
964 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
965 }
966
967 std::vector<MVT::ValueType> RetVals;
968 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +0000969 MVT::ValueType ActualRetTyVT = RetTyVT;
970 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
971 ActualRetTyVT = MVT::i32; // Promote result to i32.
972
Chris Lattnere00ebf02006-01-28 07:33:03 +0000973 if (RetTyVT == MVT::i64) {
974 RetVals.push_back(MVT::i32);
975 RetVals.push_back(MVT::i32);
976 } else if (RetTyVT != MVT::isVoid) {
Chris Lattnerf5059492005-09-02 01:24:55 +0000977 RetVals.push_back(ActualRetTyVT);
Chris Lattnere00ebf02006-01-28 07:33:03 +0000978 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000979 RetVals.push_back(MVT::Other);
980
Chris Lattner2823b3e2005-11-17 05:56:14 +0000981 // If the callee is a GlobalAddress node (quite common, every direct call is)
982 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
983 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
984 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
985
Chris Lattner281b55e2006-01-27 23:34:02 +0000986 std::vector<SDOperand> Ops;
987 Ops.push_back(Chain);
988 Ops.push_back(Callee);
989 Ops.insert(Ops.end(), args_to_use.begin(), args_to_use.end());
990 SDOperand TheCall = DAG.getNode(PPCISD::CALL, RetVals, Ops);
Chris Lattnere00ebf02006-01-28 07:33:03 +0000991 Chain = TheCall.getValue(TheCall.Val->getNumValues()-1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000992 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
993 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +0000994 SDOperand RetVal = TheCall;
995
996 // If the result is a small value, add a note so that we keep track of the
997 // information about whether it is sign or zero extended.
998 if (RetTyVT != ActualRetTyVT) {
999 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
1000 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
1001 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
Chris Lattnere00ebf02006-01-28 07:33:03 +00001002 } else if (RetTyVT == MVT::i64) {
1003 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, RetVal, RetVal.getValue(1));
Chris Lattnerf5059492005-09-02 01:24:55 +00001004 }
1005
1006 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001007}
1008
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001009MachineBasicBlock *
Nate Begeman21e463b2005-10-16 05:39:50 +00001010PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1011 MachineBasicBlock *BB) {
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001012 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
Chris Lattner919c0322005-10-01 01:35:02 +00001013 MI->getOpcode() == PPC::SELECT_CC_F4 ||
1014 MI->getOpcode() == PPC::SELECT_CC_F8) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001015 "Unexpected instr type to insert");
1016
1017 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
1018 // control-flow pattern. The incoming instruction knows the destination vreg
1019 // to set, the condition code register to branch on, the true/false values to
1020 // select between, and a branch opcode to use.
1021 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1022 ilist<MachineBasicBlock>::iterator It = BB;
1023 ++It;
1024
1025 // thisMBB:
1026 // ...
1027 // TrueVal = ...
1028 // cmpTY ccX, r1, r2
1029 // bCC copy1MBB
1030 // fallthrough --> copy0MBB
1031 MachineBasicBlock *thisMBB = BB;
1032 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1033 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1034 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
1035 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
1036 MachineFunction *F = BB->getParent();
1037 F->getBasicBlockList().insert(It, copy0MBB);
1038 F->getBasicBlockList().insert(It, sinkMBB);
1039 // Update machine-CFG edges
1040 BB->addSuccessor(copy0MBB);
1041 BB->addSuccessor(sinkMBB);
1042
1043 // copy0MBB:
1044 // %FalseValue = ...
1045 // # fallthrough to sinkMBB
1046 BB = copy0MBB;
1047
1048 // Update machine-CFG edges
1049 BB->addSuccessor(sinkMBB);
1050
1051 // sinkMBB:
1052 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1053 // ...
1054 BB = sinkMBB;
1055 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
1056 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
1057 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
1058
1059 delete MI; // The pseudo instruction is gone now.
1060 return BB;
1061}
1062
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001063SDOperand PPCTargetLowering::PerformDAGCombine(SDNode *N,
1064 DAGCombinerInfo &DCI) const {
1065 TargetMachine &TM = getTargetMachine();
1066 SelectionDAG &DAG = DCI.DAG;
1067 switch (N->getOpcode()) {
1068 default: break;
1069 case ISD::SINT_TO_FP:
1070 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
1071 // Turn (sint_to_fp (fp_to_sint X)) -> fctidz/fcfid without load/stores.
1072 // We allow the src/dst to be either f32/f64, but force the intermediate
1073 // type to be i64.
1074 if (N->getOperand(0).getOpcode() == ISD::FP_TO_SINT &&
1075 N->getOperand(0).getValueType() == MVT::i64) {
1076
1077 SDOperand Val = N->getOperand(0).getOperand(0);
1078 if (Val.getValueType() == MVT::f32) {
1079 Val = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Val);
1080 DCI.AddToWorklist(Val.Val);
1081 }
1082
1083 Val = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Val);
1084 DCI.AddToWorklist(Val.Val);
1085 Val = DAG.getNode(PPCISD::FCFID, MVT::f64, Val);
1086 DCI.AddToWorklist(Val.Val);
1087 if (N->getValueType(0) == MVT::f32) {
1088 Val = DAG.getNode(ISD::FP_ROUND, MVT::f32, Val);
1089 DCI.AddToWorklist(Val.Val);
1090 }
1091 return Val;
1092 }
1093 }
1094 break;
Chris Lattner51269842006-03-01 05:50:56 +00001095 case ISD::STORE:
1096 // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)).
1097 if (TM.getSubtarget<PPCSubtarget>().hasSTFIWX() &&
1098 N->getOperand(1).getOpcode() == ISD::FP_TO_SINT &&
1099 N->getOperand(1).getValueType() == MVT::i32) {
1100 SDOperand Val = N->getOperand(1).getOperand(0);
1101 if (Val.getValueType() == MVT::f32) {
1102 Val = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Val);
1103 DCI.AddToWorklist(Val.Val);
1104 }
1105 Val = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Val);
1106 DCI.AddToWorklist(Val.Val);
1107
1108 Val = DAG.getNode(PPCISD::STFIWX, MVT::Other, N->getOperand(0), Val,
1109 N->getOperand(2), N->getOperand(3));
1110 DCI.AddToWorklist(Val.Val);
1111 return Val;
1112 }
1113 break;
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001114 }
1115
1116 return SDOperand();
1117}
1118
Chris Lattnerad3bc8d2006-02-07 20:16:30 +00001119/// getConstraintType - Given a constraint letter, return the type of
1120/// constraint it is for this target.
1121PPCTargetLowering::ConstraintType
1122PPCTargetLowering::getConstraintType(char ConstraintLetter) const {
1123 switch (ConstraintLetter) {
1124 default: break;
1125 case 'b':
1126 case 'r':
1127 case 'f':
1128 case 'v':
1129 case 'y':
1130 return C_RegisterClass;
1131 }
1132 return TargetLowering::getConstraintType(ConstraintLetter);
1133}
1134
1135
Chris Lattnerddc787d2006-01-31 19:20:21 +00001136std::vector<unsigned> PPCTargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00001137getRegClassForInlineAsmConstraint(const std::string &Constraint,
1138 MVT::ValueType VT) const {
Chris Lattnerddc787d2006-01-31 19:20:21 +00001139 if (Constraint.size() == 1) {
1140 switch (Constraint[0]) { // GCC RS6000 Constraint Letters
1141 default: break; // Unknown constriant letter
1142 case 'b':
1143 return make_vector<unsigned>(/*no R0*/ PPC::R1 , PPC::R2 , PPC::R3 ,
1144 PPC::R4 , PPC::R5 , PPC::R6 , PPC::R7 ,
1145 PPC::R8 , PPC::R9 , PPC::R10, PPC::R11,
1146 PPC::R12, PPC::R13, PPC::R14, PPC::R15,
1147 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
1148 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
1149 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
1150 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
1151 0);
1152 case 'r':
1153 return make_vector<unsigned>(PPC::R0 , PPC::R1 , PPC::R2 , PPC::R3 ,
1154 PPC::R4 , PPC::R5 , PPC::R6 , PPC::R7 ,
1155 PPC::R8 , PPC::R9 , PPC::R10, PPC::R11,
1156 PPC::R12, PPC::R13, PPC::R14, PPC::R15,
1157 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
1158 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
1159 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
1160 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
1161 0);
1162 case 'f':
1163 return make_vector<unsigned>(PPC::F0 , PPC::F1 , PPC::F2 , PPC::F3 ,
1164 PPC::F4 , PPC::F5 , PPC::F6 , PPC::F7 ,
1165 PPC::F8 , PPC::F9 , PPC::F10, PPC::F11,
1166 PPC::F12, PPC::F13, PPC::F14, PPC::F15,
1167 PPC::F16, PPC::F17, PPC::F18, PPC::F19,
1168 PPC::F20, PPC::F21, PPC::F22, PPC::F23,
1169 PPC::F24, PPC::F25, PPC::F26, PPC::F27,
1170 PPC::F28, PPC::F29, PPC::F30, PPC::F31,
1171 0);
1172 case 'v':
1173 return make_vector<unsigned>(PPC::V0 , PPC::V1 , PPC::V2 , PPC::V3 ,
1174 PPC::V4 , PPC::V5 , PPC::V6 , PPC::V7 ,
1175 PPC::V8 , PPC::V9 , PPC::V10, PPC::V11,
1176 PPC::V12, PPC::V13, PPC::V14, PPC::V15,
1177 PPC::V16, PPC::V17, PPC::V18, PPC::V19,
1178 PPC::V20, PPC::V21, PPC::V22, PPC::V23,
1179 PPC::V24, PPC::V25, PPC::V26, PPC::V27,
1180 PPC::V28, PPC::V29, PPC::V30, PPC::V31,
1181 0);
1182 case 'y':
1183 return make_vector<unsigned>(PPC::CR0, PPC::CR1, PPC::CR2, PPC::CR3,
1184 PPC::CR4, PPC::CR5, PPC::CR6, PPC::CR7,
1185 0);
1186 }
1187 }
1188
Chris Lattner1efa40f2006-02-22 00:56:39 +00001189 return std::vector<unsigned>();
Chris Lattnerddc787d2006-01-31 19:20:21 +00001190}
Chris Lattner763317d2006-02-07 00:47:13 +00001191
1192// isOperandValidForConstraint
1193bool PPCTargetLowering::
1194isOperandValidForConstraint(SDOperand Op, char Letter) {
1195 switch (Letter) {
1196 default: break;
1197 case 'I':
1198 case 'J':
1199 case 'K':
1200 case 'L':
1201 case 'M':
1202 case 'N':
1203 case 'O':
1204 case 'P': {
1205 if (!isa<ConstantSDNode>(Op)) return false; // Must be an immediate.
1206 unsigned Value = cast<ConstantSDNode>(Op)->getValue();
1207 switch (Letter) {
1208 default: assert(0 && "Unknown constraint letter!");
1209 case 'I': // "I" is a signed 16-bit constant.
1210 return (short)Value == (int)Value;
1211 case 'J': // "J" is a constant with only the high-order 16 bits nonzero.
1212 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits.
1213 return (short)Value == 0;
1214 case 'K': // "K" is a constant with only the low-order 16 bits nonzero.
1215 return (Value >> 16) == 0;
1216 case 'M': // "M" is a constant that is greater than 31.
1217 return Value > 31;
1218 case 'N': // "N" is a positive constant that is an exact power of two.
1219 return (int)Value > 0 && isPowerOf2_32(Value);
1220 case 'O': // "O" is the constant zero.
1221 return Value == 0;
1222 case 'P': // "P" is a constant whose negation is a signed 16-bit constant.
1223 return (short)-Value == (int)-Value;
1224 }
1225 break;
1226 }
1227 }
1228
1229 // Handle standard constraint letters.
1230 return TargetLowering::isOperandValidForConstraint(Op, Letter);
1231}
Evan Chengc4c62572006-03-13 23:20:37 +00001232
1233/// isLegalAddressImmediate - Return true if the integer value can be used
1234/// as the offset of the target addressing mode.
1235bool PPCTargetLowering::isLegalAddressImmediate(int64_t V) const {
1236 // PPC allows a sign-extended 16-bit immediate field.
1237 return (V > -(1 << 16) && V < (1 << 16)-1);
1238}