blob: 003d16c26b11605552704cd88d009a16f4c23bbe [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"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000017#include "llvm/CodeGen/MachineFrameInfo.h"
18#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner8a2d3ca2005-08-26 21:23:58 +000019#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000020#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner7b738342005-09-13 19:33:40 +000021#include "llvm/CodeGen/SSARegMap.h"
Chris Lattner0b1e4e52005-08-26 17:36:52 +000022#include "llvm/Constants.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000023#include "llvm/Function.h"
Nate Begeman750ac1b2006-02-01 07:19:44 +000024#include "llvm/Support/MathExtras.h"
Evan Chengd2ee2182006-02-18 00:08:58 +000025#include "llvm/Target/TargetOptions.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000026using namespace llvm;
27
Nate Begeman21e463b2005-10-16 05:39:50 +000028PPCTargetLowering::PPCTargetLowering(TargetMachine &TM)
Chris Lattner7c5a3d32005-08-16 17:14:42 +000029 : TargetLowering(TM) {
30
31 // Fold away setcc operations if possible.
32 setSetCCIsExpensive();
Nate Begeman405e3ec2005-10-21 00:02:42 +000033 setPow2DivIsCheap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +000034
Chris Lattnerd145a612005-09-27 22:18:25 +000035 // Use _setjmp/_longjmp instead of setjmp/longjmp.
36 setUseUnderscoreSetJmpLongJmp(true);
37
Chris Lattner7c5a3d32005-08-16 17:14:42 +000038 // Set up the register classes.
Nate Begeman1d9d7422005-10-18 00:28:58 +000039 addRegisterClass(MVT::i32, PPC::GPRCRegisterClass);
40 addRegisterClass(MVT::f32, PPC::F4RCRegisterClass);
41 addRegisterClass(MVT::f64, PPC::F8RCRegisterClass);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000042
Chris Lattnera54aa942006-01-29 06:26:08 +000043 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
44 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
45
Chris Lattner7c5a3d32005-08-16 17:14:42 +000046 // PowerPC has no intrinsics for these particular operations
47 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
48 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
49 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
50
51 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
52 setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
53 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
54
55 // PowerPC has no SREM/UREM instructions
56 setOperationAction(ISD::SREM, MVT::i32, Expand);
57 setOperationAction(ISD::UREM, MVT::i32, Expand);
58
59 // We don't support sin/cos/sqrt/fmod
60 setOperationAction(ISD::FSIN , MVT::f64, Expand);
61 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattner615c2d02005-09-28 22:29:58 +000062 setOperationAction(ISD::FREM , MVT::f64, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000063 setOperationAction(ISD::FSIN , MVT::f32, Expand);
64 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattner615c2d02005-09-28 22:29:58 +000065 setOperationAction(ISD::FREM , MVT::f32, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000066
67 // If we're enabling GP optimizations, use hardware square root
Chris Lattner1e9de3e2005-09-02 18:33:05 +000068 if (!TM.getSubtarget<PPCSubtarget>().hasFSQRT()) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +000069 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
70 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
71 }
72
Nate Begemand88fc032006-01-14 03:14:10 +000073 // PowerPC does not have BSWAP, CTPOP or CTTZ
74 setOperationAction(ISD::BSWAP, MVT::i32 , Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000075 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
76 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
77
Nate Begeman35ef9132006-01-11 21:21:00 +000078 // PowerPC does not have ROTR
79 setOperationAction(ISD::ROTR, MVT::i32 , Expand);
80
Chris Lattner7c5a3d32005-08-16 17:14:42 +000081 // PowerPC does not have Select
82 setOperationAction(ISD::SELECT, MVT::i32, Expand);
83 setOperationAction(ISD::SELECT, MVT::f32, Expand);
84 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +000085
Chris Lattner0b1e4e52005-08-26 17:36:52 +000086 // PowerPC wants to turn select_cc of FP into fsel when possible.
87 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
88 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Nate Begeman44775902006-01-31 08:17:29 +000089
Nate Begeman750ac1b2006-02-01 07:19:44 +000090 // PowerPC wants to optimize integer setcc a bit
Nate Begeman44775902006-01-31 08:17:29 +000091 setOperationAction(ISD::SETCC, MVT::i32, Custom);
Chris Lattnereb9b62e2005-08-31 19:09:57 +000092
Nate Begeman7cbd5252005-08-16 19:49:35 +000093 // PowerPC does not have BRCOND* which requires SetCC
94 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
95 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000096
Chris Lattnerf7605322005-08-31 21:09:52 +000097 // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
98 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +000099
Jim Laskeyad23c9d2005-08-17 00:40:22 +0000100 // PowerPC does not have [U|S]INT_TO_FP
101 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
102 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
103
Chris Lattner53e88452005-12-23 05:13:35 +0000104 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
105 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
106
Chris Lattnere6ec9f22005-09-10 00:21:06 +0000107 // PowerPC does not have truncstore for i1.
108 setOperationAction(ISD::TRUNCSTORE, MVT::i1, Promote);
Chris Lattnerf73bae12005-11-29 06:16:21 +0000109
Jim Laskeyabf6d172006-01-05 01:25:28 +0000110 // Support label based line numbers.
Chris Lattnerf73bae12005-11-29 06:16:21 +0000111 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000112 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000113 // FIXME - use subtarget debug flags
Jim Laskeye0bce712006-01-05 01:47:43 +0000114 if (!TM.getSubtarget<PPCSubtarget>().isDarwin())
Jim Laskeyabf6d172006-01-05 01:25:28 +0000115 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattnere6ec9f22005-09-10 00:21:06 +0000116
Nate Begeman28a6b022005-12-10 02:36:00 +0000117 // We want to legalize GlobalAddress and ConstantPool nodes into the
118 // appropriate instructions to materialize the address.
Chris Lattner3eef4e32005-11-17 18:26:56 +0000119 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Nate Begeman28a6b022005-12-10 02:36:00 +0000120 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000121
Nate Begemanee625572006-01-27 21:09:22 +0000122 // RET must be custom lowered, to meet ABI requirements
123 setOperationAction(ISD::RET , MVT::Other, Custom);
124
Nate Begemanacc398c2006-01-25 18:21:52 +0000125 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
126 setOperationAction(ISD::VASTART , MVT::Other, Custom);
127
Chris Lattnerb22c08b2006-01-15 09:02:48 +0000128 // Use the default implementation.
Nate Begemanacc398c2006-01-25 18:21:52 +0000129 setOperationAction(ISD::VAARG , MVT::Other, Expand);
130 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
131 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Chris Lattnerb22c08b2006-01-15 09:02:48 +0000132 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
133 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
134 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Chris Lattner860e8862005-11-17 07:30:41 +0000135
Nate Begemanc09eeec2005-09-06 22:03:27 +0000136 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000137 // They also have instructions for converting between i64 and fp.
Nate Begemanc09eeec2005-09-06 22:03:27 +0000138 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
139 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Nate Begemanae749a92005-10-25 23:48:36 +0000140 // To take advantage of the above i64 FP_TO_SINT, promote i32 FP_TO_UINT
141 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Promote);
142 } else {
Chris Lattner860e8862005-11-17 07:30:41 +0000143 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Nate Begemanae749a92005-10-25 23:48:36 +0000144 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begeman9d2b8172005-10-18 00:56:42 +0000145 }
146
147 if (TM.getSubtarget<PPCSubtarget>().has64BitRegs()) {
148 // 64 bit PowerPC implementations can support i64 types directly
149 addRegisterClass(MVT::i64, PPC::G8RCRegisterClass);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000150 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
151 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000152 } else {
153 // 32 bit PowerPC wants to expand i64 shifts itself.
154 setOperationAction(ISD::SHL, MVT::i64, Custom);
155 setOperationAction(ISD::SRL, MVT::i64, Custom);
156 setOperationAction(ISD::SRA, MVT::i64, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000157 }
158
Nate Begeman425a9692005-11-29 08:17:20 +0000159 if (TM.getSubtarget<PPCSubtarget>().hasAltivec()) {
Nate Begeman425a9692005-11-29 08:17:20 +0000160 addRegisterClass(MVT::v4f32, PPC::VRRCRegisterClass);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000161 addRegisterClass(MVT::v4i32, PPC::VRRCRegisterClass);
Chris Lattnerec4a0c72006-01-29 06:32:58 +0000162
163 // FIXME: We don't support any ConstantVec's yet. We should custom expand
164 // the ones we do!
Chris Lattnerd9b55dd2006-01-29 08:41:37 +0000165 setOperationAction(ISD::ConstantVec, MVT::v4f32, Expand);
166 setOperationAction(ISD::ConstantVec, MVT::v4i32, Expand);
Nate Begeman425a9692005-11-29 08:17:20 +0000167 }
168
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000169 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattnercadd7422006-01-13 17:52:03 +0000170 setStackPointerRegisterToSaveRestore(PPC::R1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000171
172 computeRegisterProperties();
173}
174
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000175const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
176 switch (Opcode) {
177 default: return 0;
178 case PPCISD::FSEL: return "PPCISD::FSEL";
179 case PPCISD::FCFID: return "PPCISD::FCFID";
180 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
181 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
182 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
183 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
184 case PPCISD::Hi: return "PPCISD::Hi";
185 case PPCISD::Lo: return "PPCISD::Lo";
186 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
187 case PPCISD::SRL: return "PPCISD::SRL";
188 case PPCISD::SRA: return "PPCISD::SRA";
189 case PPCISD::SHL: return "PPCISD::SHL";
Chris Lattnere00ebf02006-01-28 07:33:03 +0000190 case PPCISD::CALL: return "PPCISD::CALL";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000191 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
192 }
193}
194
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000195/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
196static bool isFloatingPointZero(SDOperand Op) {
197 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
198 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
199 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
200 // Maybe this has already been legalized into the constant pool?
201 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
202 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
203 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
204 }
205 return false;
206}
207
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000208/// LowerOperation - Provide custom lowering hooks for some operations.
209///
Nate Begeman21e463b2005-10-16 05:39:50 +0000210SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000211 switch (Op.getOpcode()) {
212 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattnerf7605322005-08-31 21:09:52 +0000213 case ISD::FP_TO_SINT: {
Nate Begemanc09eeec2005-09-06 22:03:27 +0000214 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
Chris Lattner7c0d6642005-10-02 06:37:13 +0000215 SDOperand Src = Op.getOperand(0);
216 if (Src.getValueType() == MVT::f32)
217 Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src);
218
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000219 SDOperand Tmp;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000220 switch (Op.getValueType()) {
221 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
222 case MVT::i32:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000223 Tmp = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000224 break;
225 case MVT::i64:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000226 Tmp = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000227 break;
228 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000229
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000230 // Convert the FP value to an int value through memory.
231 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Tmp);
232 if (Op.getValueType() == MVT::i32)
233 Bits = DAG.getNode(ISD::TRUNCATE, MVT::i32, Bits);
234 return Bits;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000235 }
236 case ISD::SINT_TO_FP: {
237 assert(MVT::i64 == Op.getOperand(0).getValueType() &&
238 "Unhandled SINT_TO_FP type in custom expander!");
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000239 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, Op.getOperand(0));
240 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Bits);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000241 if (MVT::f32 == Op.getValueType())
242 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
243 return FP;
Chris Lattnerf7605322005-08-31 21:09:52 +0000244 }
245 case ISD::SELECT_CC: {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000246 // Turn FP only select_cc's into fsel instructions.
Chris Lattnerf7605322005-08-31 21:09:52 +0000247 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
248 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
249 break;
250
251 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
252
253 // Cannot handle SETEQ/SETNE.
254 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
255
256 MVT::ValueType ResVT = Op.getValueType();
257 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
258 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
259 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000260
Chris Lattnerf7605322005-08-31 21:09:52 +0000261 // If the RHS of the comparison is a 0.0, we don't need to do the
262 // subtraction at all.
263 if (isFloatingPointZero(RHS))
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000264 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000265 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000266 case ISD::SETULT:
267 case ISD::SETLT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000268 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000269 case ISD::SETUGE:
270 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000271 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
272 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattnerf7605322005-08-31 21:09:52 +0000273 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000274 case ISD::SETUGT:
275 case ISD::SETGT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000276 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000277 case ISD::SETULE:
278 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000279 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
280 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattner0bbea952005-08-26 20:25:03 +0000281 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattner85fd97d2005-10-26 18:01:11 +0000282 DAG.getNode(ISD::FNEG, MVT::f64, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000283 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000284
Chris Lattnereb255f22005-10-25 20:54:57 +0000285 SDOperand Cmp;
Chris Lattnerf7605322005-08-31 21:09:52 +0000286 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000287 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnerf7605322005-08-31 21:09:52 +0000288 case ISD::SETULT:
289 case ISD::SETLT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000290 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
291 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
292 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
293 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000294 case ISD::SETUGE:
295 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000296 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
297 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
298 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
299 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000300 case ISD::SETUGT:
301 case ISD::SETGT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000302 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
303 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
304 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
305 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000306 case ISD::SETULE:
307 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000308 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
309 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
310 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
311 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000312 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000313 break;
314 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000315 case ISD::SHL: {
316 assert(Op.getValueType() == MVT::i64 &&
317 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
318 // The generic code does a fine job expanding shift by a constant.
319 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
320
321 // Otherwise, expand into a bunch of logical ops. Note that these ops
322 // depend on the PPC behavior for oversized shift amounts.
323 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
324 DAG.getConstant(0, MVT::i32));
325 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
326 DAG.getConstant(1, MVT::i32));
327 SDOperand Amt = Op.getOperand(1);
328
329 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
330 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000331 SDOperand Tmp2 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Amt);
332 SDOperand Tmp3 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000333 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
334 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
335 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000336 SDOperand Tmp6 = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000337 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000338 SDOperand OutLo = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000339 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
340 }
341 case ISD::SRL: {
342 assert(Op.getValueType() == MVT::i64 &&
343 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
344 // The generic code does a fine job expanding shift by a constant.
345 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
346
347 // Otherwise, expand into a bunch of logical ops. Note that these ops
348 // depend on the PPC behavior for oversized shift amounts.
349 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
350 DAG.getConstant(0, MVT::i32));
351 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
352 DAG.getConstant(1, MVT::i32));
353 SDOperand Amt = Op.getOperand(1);
354
355 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
356 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000357 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
358 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000359 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
360 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
361 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000362 SDOperand Tmp6 = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000363 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000364 SDOperand OutHi = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000365 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
366 }
367 case ISD::SRA: {
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000368 assert(Op.getValueType() == MVT::i64 &&
369 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
370 // The generic code does a fine job expanding shift by a constant.
371 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
372
373 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
374 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
375 DAG.getConstant(0, MVT::i32));
376 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
377 DAG.getConstant(1, MVT::i32));
378 SDOperand Amt = Op.getOperand(1);
379
380 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
381 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000382 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
383 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000384 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
385 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
386 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000387 SDOperand Tmp6 = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Tmp5);
388 SDOperand OutHi = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Amt);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000389 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
390 Tmp4, Tmp6, ISD::SETLE);
391 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000392 }
Nate Begeman28a6b022005-12-10 02:36:00 +0000393 case ISD::ConstantPool: {
Evan Chengb8973bd2006-01-31 22:23:14 +0000394 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
395 Constant *C = CP->get();
396 SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i32, CP->getAlignment());
Nate Begeman28a6b022005-12-10 02:36:00 +0000397 SDOperand Zero = DAG.getConstant(0, MVT::i32);
398
399 if (PPCGenerateStaticCode) {
400 // Generate non-pic code that has direct accesses to the constant pool.
401 // The address of the global is just (hi(&g)+lo(&g)).
402 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
403 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
404 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
405 }
406
407 // Only lower ConstantPool on Darwin.
408 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
409 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
410 if (PICEnabled) {
411 // With PIC, the first instruction is actually "GR+hi(&G)".
412 Hi = DAG.getNode(ISD::ADD, MVT::i32,
413 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
414 }
415
416 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
417 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
418 return Lo;
419 }
Chris Lattner860e8862005-11-17 07:30:41 +0000420 case ISD::GlobalAddress: {
Nate Begeman50fb3c42005-12-24 01:00:15 +0000421 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
422 GlobalValue *GV = GSDN->getGlobal();
423 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32, GSDN->getOffset());
Chris Lattner860e8862005-11-17 07:30:41 +0000424 SDOperand Zero = DAG.getConstant(0, MVT::i32);
Chris Lattner1d05cb42005-11-17 18:55:48 +0000425
426 if (PPCGenerateStaticCode) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000427 // Generate non-pic code that has direct accesses to globals.
428 // The address of the global is just (hi(&g)+lo(&g)).
Chris Lattner1d05cb42005-11-17 18:55:48 +0000429 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
430 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
431 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
432 }
Chris Lattner860e8862005-11-17 07:30:41 +0000433
Chris Lattner1d05cb42005-11-17 18:55:48 +0000434 // Only lower GlobalAddress on Darwin.
435 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
Chris Lattnera35ef632006-01-06 01:04:03 +0000436
Chris Lattner860e8862005-11-17 07:30:41 +0000437 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
438 if (PICEnabled) {
439 // With PIC, the first instruction is actually "GR+hi(&G)".
440 Hi = DAG.getNode(ISD::ADD, MVT::i32,
Chris Lattner15666132005-11-17 17:51:38 +0000441 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
Chris Lattner860e8862005-11-17 07:30:41 +0000442 }
443
444 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
445 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
446
Chris Lattner37dd6f12006-01-29 20:49:17 +0000447 if (!GV->hasWeakLinkage() && !GV->hasLinkOnceLinkage() &&
448 (!GV->isExternal() || GV->hasNotBeenReadFromBytecode()))
Chris Lattner860e8862005-11-17 07:30:41 +0000449 return Lo;
450
451 // If the global is weak or external, we have to go through the lazy
452 // resolution stub.
453 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), Lo, DAG.getSrcValue(0));
454 }
Nate Begeman44775902006-01-31 08:17:29 +0000455 case ISD::SETCC: {
456 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Nate Begeman750ac1b2006-02-01 07:19:44 +0000457
458 // If we're comparing for equality to zero, expose the fact that this is
459 // implented as a ctlz/srl pair on ppc, so that the dag combiner can
460 // fold the new nodes.
461 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
462 if (C->isNullValue() && CC == ISD::SETEQ) {
463 MVT::ValueType VT = Op.getOperand(0).getValueType();
464 SDOperand Zext = Op.getOperand(0);
465 if (VT < MVT::i32) {
466 VT = MVT::i32;
467 Zext = DAG.getNode(ISD::ZERO_EXTEND, VT, Op.getOperand(0));
468 }
469 unsigned Log2b = Log2_32(MVT::getSizeInBits(VT));
470 SDOperand Clz = DAG.getNode(ISD::CTLZ, VT, Zext);
471 SDOperand Scc = DAG.getNode(ISD::SRL, VT, Clz,
472 DAG.getConstant(Log2b, getShiftAmountTy()));
473 return DAG.getNode(ISD::TRUNCATE, getSetCCResultTy(), Scc);
474 }
475 // Leave comparisons against 0 and -1 alone for now, since they're usually
476 // optimized. FIXME: revisit this when we can custom lower all setcc
477 // optimizations.
478 if (C->isAllOnesValue() || C->isNullValue())
479 break;
480 }
481
482 // If we have an integer seteq/setne, turn it into a compare against zero
483 // by subtracting the rhs from the lhs, which is faster than setting a
484 // condition register, reading it back out, and masking the correct bit.
485 MVT::ValueType LHSVT = Op.getOperand(0).getValueType();
486 if (MVT::isInteger(LHSVT) && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
487 MVT::ValueType VT = Op.getValueType();
488 SDOperand Sub = DAG.getNode(ISD::SUB, LHSVT, Op.getOperand(0),
489 Op.getOperand(1));
490 return DAG.getSetCC(VT, Sub, DAG.getConstant(0, LHSVT), CC);
491 }
Nate Begeman44775902006-01-31 08:17:29 +0000492 break;
493 }
Nate Begemanacc398c2006-01-25 18:21:52 +0000494 case ISD::VASTART: {
495 // vastart just stores the address of the VarArgsFrameIndex slot into the
496 // memory location argument.
497 // FIXME: Replace MVT::i32 with PointerTy
498 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
499 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
500 Op.getOperand(1), Op.getOperand(2));
501 }
Nate Begemanee625572006-01-27 21:09:22 +0000502 case ISD::RET: {
503 SDOperand Copy;
504
505 switch(Op.getNumOperands()) {
506 default:
507 assert(0 && "Do not know how to return this many arguments!");
508 abort();
509 case 1:
510 return SDOperand(); // ret void is legal
511 case 2: {
512 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
513 unsigned ArgReg = MVT::isInteger(ArgVT) ? PPC::R3 : PPC::F1;
514 Copy = DAG.getCopyToReg(Op.getOperand(0), ArgReg, Op.getOperand(1),
515 SDOperand());
516 break;
517 }
518 case 3:
519 Copy = DAG.getCopyToReg(Op.getOperand(0), PPC::R3, Op.getOperand(2),
520 SDOperand());
521 Copy = DAG.getCopyToReg(Copy, PPC::R4, Op.getOperand(1),Copy.getValue(1));
522 break;
523 }
524 return DAG.getNode(PPCISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
525 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000526 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000527 return SDOperand();
528}
529
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000530std::vector<SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000531PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000532 //
533 // add beautiful description of PPC stack frame format, or at least some docs
534 //
535 MachineFunction &MF = DAG.getMachineFunction();
536 MachineFrameInfo *MFI = MF.getFrameInfo();
537 MachineBasicBlock& BB = MF.front();
Chris Lattner7b738342005-09-13 19:33:40 +0000538 SSARegMap *RegMap = MF.getSSARegMap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000539 std::vector<SDOperand> ArgValues;
540
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000541 unsigned ArgOffset = 24;
542 unsigned GPR_remaining = 8;
543 unsigned FPR_remaining = 13;
544 unsigned GPR_idx = 0, FPR_idx = 0;
545 static const unsigned GPR[] = {
546 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
547 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
548 };
549 static const unsigned FPR[] = {
550 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
551 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
552 };
553
554 // Add DAG nodes to load the arguments... On entry to a function on PPC,
555 // the arguments start at offset 24, although they are likely to be passed
556 // in registers.
557 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
558 SDOperand newroot, argt;
559 unsigned ObjSize;
560 bool needsLoad = false;
561 bool ArgLive = !I->use_empty();
562 MVT::ValueType ObjectVT = getValueType(I->getType());
563
564 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000565 default: assert(0 && "Unhandled argument type!");
566 case MVT::i1:
567 case MVT::i8:
568 case MVT::i16:
569 case MVT::i32:
570 ObjSize = 4;
571 if (!ArgLive) break;
572 if (GPR_remaining > 0) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000573 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000574 MF.addLiveIn(GPR[GPR_idx], VReg);
575 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +0000576 if (ObjectVT != MVT::i32) {
577 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
578 : ISD::AssertZext;
579 argt = DAG.getNode(AssertOp, MVT::i32, argt,
580 DAG.getValueType(ObjectVT));
581 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
582 }
Chris Lattner915fb302005-08-30 00:19:00 +0000583 } else {
584 needsLoad = true;
585 }
586 break;
Chris Lattner80720a92005-11-30 20:40:54 +0000587 case MVT::i64:
588 ObjSize = 8;
Chris Lattner915fb302005-08-30 00:19:00 +0000589 if (!ArgLive) break;
590 if (GPR_remaining > 0) {
591 SDOperand argHi, argLo;
Nate Begeman1d9d7422005-10-18 00:28:58 +0000592 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000593 MF.addLiveIn(GPR[GPR_idx], VReg);
594 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000595 // If we have two or more remaining argument registers, then both halves
596 // of the i64 can be sourced from there. Otherwise, the lower half will
597 // have to come off the stack. This can happen when an i64 is preceded
598 // by 28 bytes of arguments.
599 if (GPR_remaining > 1) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000600 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000601 MF.addLiveIn(GPR[GPR_idx+1], VReg);
602 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000603 } else {
604 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
605 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
606 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
607 DAG.getSrcValue(NULL));
608 }
609 // Build the outgoing arg thingy
610 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
611 newroot = argLo;
612 } else {
613 needsLoad = true;
614 }
615 break;
616 case MVT::f32:
617 case MVT::f64:
618 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
Chris Lattner413b9792006-01-11 18:21:25 +0000619 if (!ArgLive) {
620 if (FPR_remaining > 0) {
621 --FPR_remaining;
622 ++FPR_idx;
623 }
624 break;
625 }
Chris Lattner915fb302005-08-30 00:19:00 +0000626 if (FPR_remaining > 0) {
Chris Lattner919c0322005-10-01 01:35:02 +0000627 unsigned VReg;
628 if (ObjectVT == MVT::f32)
Nate Begeman1d9d7422005-10-18 00:28:58 +0000629 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner919c0322005-10-01 01:35:02 +0000630 else
Nate Begeman1d9d7422005-10-18 00:28:58 +0000631 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000632 MF.addLiveIn(FPR[FPR_idx], VReg);
633 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
Chris Lattner915fb302005-08-30 00:19:00 +0000634 --FPR_remaining;
635 ++FPR_idx;
636 } else {
637 needsLoad = true;
638 }
639 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000640 }
641
642 // We need to load the argument to a virtual register if we determined above
643 // that we ran out of physical registers of the appropriate type
644 if (needsLoad) {
645 unsigned SubregOffset = 0;
646 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
647 if (ObjectVT == MVT::i16) SubregOffset = 2;
648 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
649 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
650 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
651 DAG.getConstant(SubregOffset, MVT::i32));
652 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
653 DAG.getSrcValue(NULL));
654 }
655
656 // Every 4 bytes of argument space consumes one of the GPRs available for
657 // argument passing.
658 if (GPR_remaining > 0) {
659 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
660 GPR_remaining -= delta;
661 GPR_idx += delta;
662 }
663 ArgOffset += ObjSize;
664 if (newroot.Val)
665 DAG.setRoot(newroot.getValue(1));
666
667 ArgValues.push_back(argt);
668 }
669
670 // If the function takes variable number of arguments, make a frame index for
671 // the start of the first vararg value... for expansion of llvm.va_start.
672 if (F.isVarArg()) {
673 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
674 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
675 // If this function is vararg, store any remaining integer argument regs
676 // to their spots on the stack so that they may be loaded by deferencing the
677 // result of va_next.
678 std::vector<SDOperand> MemOps;
679 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000680 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000681 MF.addLiveIn(GPR[GPR_idx], VReg);
682 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000683 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
684 Val, FIN, DAG.getSrcValue(NULL));
685 MemOps.push_back(Store);
686 // Increment the address by four for the next argument to store
687 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
688 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
689 }
Chris Lattner80720a92005-11-30 20:40:54 +0000690 if (!MemOps.empty()) {
691 MemOps.push_back(DAG.getRoot());
692 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
693 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000694 }
695
696 // Finally, inform the code generator which regs we return values in.
697 switch (getValueType(F.getReturnType())) {
698 default: assert(0 && "Unknown type!");
699 case MVT::isVoid: break;
700 case MVT::i1:
701 case MVT::i8:
702 case MVT::i16:
703 case MVT::i32:
704 MF.addLiveOut(PPC::R3);
705 break;
706 case MVT::i64:
707 MF.addLiveOut(PPC::R3);
708 MF.addLiveOut(PPC::R4);
709 break;
710 case MVT::f32:
711 case MVT::f64:
712 MF.addLiveOut(PPC::F1);
713 break;
714 }
715
716 return ArgValues;
717}
718
719std::pair<SDOperand, SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000720PPCTargetLowering::LowerCallTo(SDOperand Chain,
721 const Type *RetTy, bool isVarArg,
722 unsigned CallingConv, bool isTailCall,
723 SDOperand Callee, ArgListTy &Args,
724 SelectionDAG &DAG) {
Chris Lattner281b55e2006-01-27 23:34:02 +0000725 // args_to_use will accumulate outgoing args for the PPCISD::CALL case in
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000726 // SelectExpr to use to put the arguments in the appropriate registers.
727 std::vector<SDOperand> args_to_use;
728
729 // Count how many bytes are to be pushed on the stack, including the linkage
730 // area, and parameter passing area.
731 unsigned NumBytes = 24;
732
733 if (Args.empty()) {
Chris Lattner45b39762006-02-13 08:55:29 +0000734 Chain = DAG.getCALLSEQ_START(Chain,
735 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000736 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000737 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000738 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +0000739 default: assert(0 && "Unknown value type!");
740 case MVT::i1:
741 case MVT::i8:
742 case MVT::i16:
743 case MVT::i32:
744 case MVT::f32:
745 NumBytes += 4;
746 break;
747 case MVT::i64:
748 case MVT::f64:
749 NumBytes += 8;
750 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000751 }
Chris Lattner915fb302005-08-30 00:19:00 +0000752 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000753
Chris Lattner915fb302005-08-30 00:19:00 +0000754 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
755 // plus 32 bytes of argument space in case any called code gets funky on us.
756 // (Required by ABI to support var arg)
757 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000758
759 // Adjust the stack pointer for the new arguments...
760 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattner45b39762006-02-13 08:55:29 +0000761 Chain = DAG.getCALLSEQ_START(Chain,
762 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000763
764 // Set up a copy of the stack pointer for use loading and storing any
765 // arguments that may not fit in the registers available for argument
766 // passing.
Chris Lattnera243db82006-01-11 19:55:07 +0000767 SDOperand StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000768
769 // Figure out which arguments are going to go in registers, and which in
770 // memory. Also, if this is a vararg function, floating point operations
771 // must be stored to our stack, and loaded into integer regs as well, if
772 // any integer regs are available for argument passing.
773 unsigned ArgOffset = 24;
774 unsigned GPR_remaining = 8;
775 unsigned FPR_remaining = 13;
776
777 std::vector<SDOperand> MemOps;
778 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
779 // PtrOff will be used to store the current argument to the stack if a
780 // register cannot be found for it.
781 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
782 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
783 MVT::ValueType ArgVT = getValueType(Args[i].second);
784
785 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000786 default: assert(0 && "Unexpected ValueType for argument!");
787 case MVT::i1:
788 case MVT::i8:
789 case MVT::i16:
790 // Promote the integer to 32 bits. If the input type is signed use a
791 // sign extend, otherwise use a zero extend.
792 if (Args[i].second->isSigned())
793 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
794 else
795 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
796 // FALL THROUGH
797 case MVT::i32:
798 if (GPR_remaining > 0) {
799 args_to_use.push_back(Args[i].first);
800 --GPR_remaining;
801 } else {
802 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
803 Args[i].first, PtrOff,
804 DAG.getSrcValue(NULL)));
805 }
806 ArgOffset += 4;
807 break;
808 case MVT::i64:
809 // If we have one free GPR left, we can place the upper half of the i64
810 // in it, and store the other half to the stack. If we have two or more
811 // free GPRs, then we can pass both halves of the i64 in registers.
812 if (GPR_remaining > 0) {
813 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
814 Args[i].first, DAG.getConstant(1, MVT::i32));
815 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
816 Args[i].first, DAG.getConstant(0, MVT::i32));
817 args_to_use.push_back(Hi);
818 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000819 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000820 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000821 --GPR_remaining;
822 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000823 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
824 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000825 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +0000826 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000827 }
Chris Lattner915fb302005-08-30 00:19:00 +0000828 } else {
829 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
830 Args[i].first, PtrOff,
831 DAG.getSrcValue(NULL)));
832 }
833 ArgOffset += 8;
834 break;
835 case MVT::f32:
836 case MVT::f64:
837 if (FPR_remaining > 0) {
838 args_to_use.push_back(Args[i].first);
839 --FPR_remaining;
840 if (isVarArg) {
841 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
842 Args[i].first, PtrOff,
843 DAG.getSrcValue(NULL));
844 MemOps.push_back(Store);
845 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000846 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000847 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
848 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000849 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000850 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000851 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +0000852 }
853 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000854 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
855 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +0000856 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
857 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000858 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000859 args_to_use.push_back(Load);
860 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000861 }
862 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000863 // If we have any FPRs remaining, we may also have GPRs remaining.
864 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
865 // GPRs.
866 if (GPR_remaining > 0) {
867 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
868 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000869 }
Chris Lattner915fb302005-08-30 00:19:00 +0000870 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
871 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
872 --GPR_remaining;
873 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000874 }
Chris Lattner915fb302005-08-30 00:19:00 +0000875 } else {
876 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
877 Args[i].first, PtrOff,
878 DAG.getSrcValue(NULL)));
879 }
880 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
881 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000882 }
883 }
884 if (!MemOps.empty())
885 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
886 }
887
888 std::vector<MVT::ValueType> RetVals;
889 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +0000890 MVT::ValueType ActualRetTyVT = RetTyVT;
891 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
892 ActualRetTyVT = MVT::i32; // Promote result to i32.
893
Chris Lattnere00ebf02006-01-28 07:33:03 +0000894 if (RetTyVT == MVT::i64) {
895 RetVals.push_back(MVT::i32);
896 RetVals.push_back(MVT::i32);
897 } else if (RetTyVT != MVT::isVoid) {
Chris Lattnerf5059492005-09-02 01:24:55 +0000898 RetVals.push_back(ActualRetTyVT);
Chris Lattnere00ebf02006-01-28 07:33:03 +0000899 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000900 RetVals.push_back(MVT::Other);
901
Chris Lattner2823b3e2005-11-17 05:56:14 +0000902 // If the callee is a GlobalAddress node (quite common, every direct call is)
903 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
904 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
905 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
906
Chris Lattner281b55e2006-01-27 23:34:02 +0000907 std::vector<SDOperand> Ops;
908 Ops.push_back(Chain);
909 Ops.push_back(Callee);
910 Ops.insert(Ops.end(), args_to_use.begin(), args_to_use.end());
911 SDOperand TheCall = DAG.getNode(PPCISD::CALL, RetVals, Ops);
Chris Lattnere00ebf02006-01-28 07:33:03 +0000912 Chain = TheCall.getValue(TheCall.Val->getNumValues()-1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000913 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
914 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +0000915 SDOperand RetVal = TheCall;
916
917 // If the result is a small value, add a note so that we keep track of the
918 // information about whether it is sign or zero extended.
919 if (RetTyVT != ActualRetTyVT) {
920 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
921 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
922 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
Chris Lattnere00ebf02006-01-28 07:33:03 +0000923 } else if (RetTyVT == MVT::i64) {
924 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, RetVal, RetVal.getValue(1));
Chris Lattnerf5059492005-09-02 01:24:55 +0000925 }
926
927 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000928}
929
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000930MachineBasicBlock *
Nate Begeman21e463b2005-10-16 05:39:50 +0000931PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
932 MachineBasicBlock *BB) {
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000933 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
Chris Lattner919c0322005-10-01 01:35:02 +0000934 MI->getOpcode() == PPC::SELECT_CC_F4 ||
935 MI->getOpcode() == PPC::SELECT_CC_F8) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000936 "Unexpected instr type to insert");
937
938 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
939 // control-flow pattern. The incoming instruction knows the destination vreg
940 // to set, the condition code register to branch on, the true/false values to
941 // select between, and a branch opcode to use.
942 const BasicBlock *LLVM_BB = BB->getBasicBlock();
943 ilist<MachineBasicBlock>::iterator It = BB;
944 ++It;
945
946 // thisMBB:
947 // ...
948 // TrueVal = ...
949 // cmpTY ccX, r1, r2
950 // bCC copy1MBB
951 // fallthrough --> copy0MBB
952 MachineBasicBlock *thisMBB = BB;
953 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
954 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
955 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
956 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
957 MachineFunction *F = BB->getParent();
958 F->getBasicBlockList().insert(It, copy0MBB);
959 F->getBasicBlockList().insert(It, sinkMBB);
960 // Update machine-CFG edges
961 BB->addSuccessor(copy0MBB);
962 BB->addSuccessor(sinkMBB);
963
964 // copy0MBB:
965 // %FalseValue = ...
966 // # fallthrough to sinkMBB
967 BB = copy0MBB;
968
969 // Update machine-CFG edges
970 BB->addSuccessor(sinkMBB);
971
972 // sinkMBB:
973 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
974 // ...
975 BB = sinkMBB;
976 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
977 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
978 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
979
980 delete MI; // The pseudo instruction is gone now.
981 return BB;
982}
983
Chris Lattnerad3bc8d2006-02-07 20:16:30 +0000984/// getConstraintType - Given a constraint letter, return the type of
985/// constraint it is for this target.
986PPCTargetLowering::ConstraintType
987PPCTargetLowering::getConstraintType(char ConstraintLetter) const {
988 switch (ConstraintLetter) {
989 default: break;
990 case 'b':
991 case 'r':
992 case 'f':
993 case 'v':
994 case 'y':
995 return C_RegisterClass;
996 }
997 return TargetLowering::getConstraintType(ConstraintLetter);
998}
999
1000
Chris Lattnerddc787d2006-01-31 19:20:21 +00001001std::vector<unsigned> PPCTargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00001002getRegClassForInlineAsmConstraint(const std::string &Constraint,
1003 MVT::ValueType VT) const {
Chris Lattnerddc787d2006-01-31 19:20:21 +00001004 if (Constraint.size() == 1) {
1005 switch (Constraint[0]) { // GCC RS6000 Constraint Letters
1006 default: break; // Unknown constriant letter
1007 case 'b':
1008 return make_vector<unsigned>(/*no R0*/ PPC::R1 , PPC::R2 , PPC::R3 ,
1009 PPC::R4 , PPC::R5 , PPC::R6 , PPC::R7 ,
1010 PPC::R8 , PPC::R9 , PPC::R10, PPC::R11,
1011 PPC::R12, PPC::R13, PPC::R14, PPC::R15,
1012 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
1013 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
1014 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
1015 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
1016 0);
1017 case 'r':
1018 return make_vector<unsigned>(PPC::R0 , PPC::R1 , PPC::R2 , PPC::R3 ,
1019 PPC::R4 , PPC::R5 , PPC::R6 , PPC::R7 ,
1020 PPC::R8 , PPC::R9 , PPC::R10, PPC::R11,
1021 PPC::R12, PPC::R13, PPC::R14, PPC::R15,
1022 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
1023 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
1024 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
1025 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
1026 0);
1027 case 'f':
1028 return make_vector<unsigned>(PPC::F0 , PPC::F1 , PPC::F2 , PPC::F3 ,
1029 PPC::F4 , PPC::F5 , PPC::F6 , PPC::F7 ,
1030 PPC::F8 , PPC::F9 , PPC::F10, PPC::F11,
1031 PPC::F12, PPC::F13, PPC::F14, PPC::F15,
1032 PPC::F16, PPC::F17, PPC::F18, PPC::F19,
1033 PPC::F20, PPC::F21, PPC::F22, PPC::F23,
1034 PPC::F24, PPC::F25, PPC::F26, PPC::F27,
1035 PPC::F28, PPC::F29, PPC::F30, PPC::F31,
1036 0);
1037 case 'v':
1038 return make_vector<unsigned>(PPC::V0 , PPC::V1 , PPC::V2 , PPC::V3 ,
1039 PPC::V4 , PPC::V5 , PPC::V6 , PPC::V7 ,
1040 PPC::V8 , PPC::V9 , PPC::V10, PPC::V11,
1041 PPC::V12, PPC::V13, PPC::V14, PPC::V15,
1042 PPC::V16, PPC::V17, PPC::V18, PPC::V19,
1043 PPC::V20, PPC::V21, PPC::V22, PPC::V23,
1044 PPC::V24, PPC::V25, PPC::V26, PPC::V27,
1045 PPC::V28, PPC::V29, PPC::V30, PPC::V31,
1046 0);
1047 case 'y':
1048 return make_vector<unsigned>(PPC::CR0, PPC::CR1, PPC::CR2, PPC::CR3,
1049 PPC::CR4, PPC::CR5, PPC::CR6, PPC::CR7,
1050 0);
1051 }
1052 }
1053
Chris Lattner1efa40f2006-02-22 00:56:39 +00001054 return std::vector<unsigned>();
Chris Lattnerddc787d2006-01-31 19:20:21 +00001055}
Chris Lattner763317d2006-02-07 00:47:13 +00001056
1057// isOperandValidForConstraint
1058bool PPCTargetLowering::
1059isOperandValidForConstraint(SDOperand Op, char Letter) {
1060 switch (Letter) {
1061 default: break;
1062 case 'I':
1063 case 'J':
1064 case 'K':
1065 case 'L':
1066 case 'M':
1067 case 'N':
1068 case 'O':
1069 case 'P': {
1070 if (!isa<ConstantSDNode>(Op)) return false; // Must be an immediate.
1071 unsigned Value = cast<ConstantSDNode>(Op)->getValue();
1072 switch (Letter) {
1073 default: assert(0 && "Unknown constraint letter!");
1074 case 'I': // "I" is a signed 16-bit constant.
1075 return (short)Value == (int)Value;
1076 case 'J': // "J" is a constant with only the high-order 16 bits nonzero.
1077 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits.
1078 return (short)Value == 0;
1079 case 'K': // "K" is a constant with only the low-order 16 bits nonzero.
1080 return (Value >> 16) == 0;
1081 case 'M': // "M" is a constant that is greater than 31.
1082 return Value > 31;
1083 case 'N': // "N" is a positive constant that is an exact power of two.
1084 return (int)Value > 0 && isPowerOf2_32(Value);
1085 case 'O': // "O" is the constant zero.
1086 return Value == 0;
1087 case 'P': // "P" is a constant whose negation is a signed 16-bit constant.
1088 return (short)-Value == (int)-Value;
1089 }
1090 break;
1091 }
1092 }
1093
1094 // Handle standard constraint letters.
1095 return TargetLowering::isOperandValidForConstraint(Op, Letter);
1096}