blob: 7ad633a39d825fe66fe62cb81979022d21465c02 [file] [log] [blame]
Nate Begemana9795f82005-03-24 04:41:43 +00001//===-- PPC32ISelPattern.cpp - A pattern matching inst selector for PPC32 -===//
2//
3// The LLVM Compiler Infrastructure
4//
Nate Begeman5e966612005-03-24 06:28:42 +00005// This file was developed by Nate Begeman and is distributed under
Nate Begemana9795f82005-03-24 04:41:43 +00006// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for 32 bit PowerPC.
Nate Begeman815d6da2005-04-06 00:25:27 +000011// Magic number generation for integer divide from the PowerPC Compiler Writer's
12// Guide, section 3.2.3.5
Nate Begemana9795f82005-03-24 04:41:43 +000013//
14//===----------------------------------------------------------------------===//
15
16#include "PowerPC.h"
17#include "PowerPCInstrBuilder.h"
18#include "PowerPCInstrInfo.h"
19#include "PPC32RegisterInfo.h"
20#include "llvm/Constants.h" // FIXME: REMOVE
21#include "llvm/Function.h"
22#include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
23#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
25#include "llvm/CodeGen/SelectionDAG.h"
26#include "llvm/CodeGen/SelectionDAGISel.h"
27#include "llvm/CodeGen/SSARegMap.h"
28#include "llvm/Target/TargetData.h"
29#include "llvm/Target/TargetLowering.h"
Nate Begeman93075ec2005-04-04 23:40:36 +000030#include "llvm/Target/TargetOptions.h"
Nate Begemana9795f82005-03-24 04:41:43 +000031#include "llvm/Support/Debug.h"
32#include "llvm/Support/MathExtras.h"
33#include "llvm/ADT/Statistic.h"
34#include <set>
35#include <algorithm>
36using namespace llvm;
37
38//===----------------------------------------------------------------------===//
39// PPC32TargetLowering - PPC32 Implementation of the TargetLowering interface
40namespace {
41 class PPC32TargetLowering : public TargetLowering {
42 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
43 int ReturnAddrIndex; // FrameIndex for return slot.
44 public:
45 PPC32TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
Nate Begemana9795f82005-03-24 04:41:43 +000046 // Set up the register classes.
47 addRegisterClass(MVT::i32, PPC32::GPRCRegisterClass);
Nate Begeman7532e2f2005-03-26 08:25:22 +000048 addRegisterClass(MVT::f32, PPC32::FPRCRegisterClass);
Nate Begemana9795f82005-03-24 04:41:43 +000049 addRegisterClass(MVT::f64, PPC32::FPRCRegisterClass);
50
Nate Begeman74d73452005-03-31 00:15:26 +000051 // PowerPC has no intrinsics for these particular operations
Chris Lattner644db4e2005-04-09 03:22:30 +000052 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
Nate Begeman01d05262005-03-30 01:45:43 +000053 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
54 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
55 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
56
Nate Begeman74d73452005-03-31 00:15:26 +000057 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
58 setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
59 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
Nate Begeman815d6da2005-04-06 00:25:27 +000060
61 // PowerPC has no SREM/UREM instructions
62 setOperationAction(ISD::SREM, MVT::i32, Expand);
63 setOperationAction(ISD::UREM, MVT::i32, Expand);
Chris Lattner43fdea02005-04-02 05:03:24 +000064
Nate Begeman27eeb002005-04-02 05:59:34 +000065 setShiftAmountFlavor(Extend); // shl X, 32 == 0
Chris Lattnercbd06fc2005-04-07 19:41:49 +000066 setSetCCResultContents(ZeroOrOneSetCCResult);
Nate Begeman3e897162005-03-31 23:55:40 +000067 addLegalFPImmediate(+0.0); // Necessary for FSEL
68 addLegalFPImmediate(-0.0); //
69
Nate Begemana9795f82005-03-24 04:41:43 +000070 computeRegisterProperties();
71 }
72
73 /// LowerArguments - This hook must be implemented to indicate how we should
74 /// lower the arguments for the specified function, into the specified DAG.
75 virtual std::vector<SDOperand>
76 LowerArguments(Function &F, SelectionDAG &DAG);
77
78 /// LowerCallTo - This hook lowers an abstract call to a function into an
79 /// actual call.
80 virtual std::pair<SDOperand, SDOperand>
Nate Begeman307e7442005-03-26 01:28:53 +000081 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
82 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Nate Begemana9795f82005-03-24 04:41:43 +000083
84 virtual std::pair<SDOperand, SDOperand>
85 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
86
87 virtual std::pair<SDOperand,SDOperand>
88 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
89 const Type *ArgTy, SelectionDAG &DAG);
90
91 virtual std::pair<SDOperand, SDOperand>
92 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
93 SelectionDAG &DAG);
94 };
95}
96
97
98std::vector<SDOperand>
99PPC32TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
100 //
101 // add beautiful description of PPC stack frame format, or at least some docs
102 //
103 MachineFunction &MF = DAG.getMachineFunction();
104 MachineFrameInfo *MFI = MF.getFrameInfo();
105 MachineBasicBlock& BB = MF.front();
106 std::vector<SDOperand> ArgValues;
107
108 // Due to the rather complicated nature of the PowerPC ABI, rather than a
109 // fixed size array of physical args, for the sake of simplicity let the STL
110 // handle tracking them for us.
111 std::vector<unsigned> argVR, argPR, argOp;
112 unsigned ArgOffset = 24;
113 unsigned GPR_remaining = 8;
114 unsigned FPR_remaining = 13;
115 unsigned GPR_idx = 0, FPR_idx = 0;
116 static const unsigned GPR[] = {
117 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
118 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
119 };
120 static const unsigned FPR[] = {
121 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
122 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
123 };
124
125 // Add DAG nodes to load the arguments... On entry to a function on PPC,
126 // the arguments start at offset 24, although they are likely to be passed
127 // in registers.
128 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
129 SDOperand newroot, argt;
130 unsigned ObjSize;
131 bool needsLoad = false;
132 MVT::ValueType ObjectVT = getValueType(I->getType());
133
134 switch (ObjectVT) {
135 default: assert(0 && "Unhandled argument type!");
136 case MVT::i1:
137 case MVT::i8:
138 case MVT::i16:
139 case MVT::i32:
140 ObjSize = 4;
141 if (GPR_remaining > 0) {
142 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
Nate Begemanf70b5762005-03-28 23:08:54 +0000143 argt = newroot = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32,
144 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000145 if (ObjectVT != MVT::i32)
146 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, newroot);
Nate Begemana9795f82005-03-24 04:41:43 +0000147 } else {
148 needsLoad = true;
149 }
150 break;
Nate Begemanf7e43382005-03-26 07:46:36 +0000151 case MVT::i64: ObjSize = 8;
152 // FIXME: can split 64b load between reg/mem if it is last arg in regs
Nate Begemana9795f82005-03-24 04:41:43 +0000153 if (GPR_remaining > 1) {
154 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
155 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx+1]);
Nate Begemanca12a2b2005-03-28 22:28:37 +0000156 // Copy the extracted halves into the virtual registers
Nate Begemanf70b5762005-03-28 23:08:54 +0000157 SDOperand argHi = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32,
158 DAG.getRoot());
159 SDOperand argLo = DAG.getCopyFromReg(GPR[GPR_idx+1], MVT::i32, argHi);
Nate Begemanca12a2b2005-03-28 22:28:37 +0000160 // Build the outgoing arg thingy
Nate Begemanf70b5762005-03-28 23:08:54 +0000161 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
162 newroot = argLo;
Nate Begemana9795f82005-03-24 04:41:43 +0000163 } else {
164 needsLoad = true;
165 }
166 break;
167 case MVT::f32: ObjSize = 4;
168 case MVT::f64: ObjSize = 8;
169 if (FPR_remaining > 0) {
170 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, FPR[FPR_idx]);
Nate Begemanf70b5762005-03-28 23:08:54 +0000171 argt = newroot = DAG.getCopyFromReg(FPR[FPR_idx], ObjectVT,
172 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000173 --FPR_remaining;
174 ++FPR_idx;
175 } else {
176 needsLoad = true;
177 }
178 break;
179 }
180
181 // We need to load the argument to a virtual register if we determined above
182 // that we ran out of physical registers of the appropriate type
183 if (needsLoad) {
Nate Begemane5846682005-04-04 06:52:38 +0000184 unsigned SubregOffset = 0;
Nate Begemanc3e2db42005-04-04 09:09:00 +0000185 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
Nate Begemane5846682005-04-04 06:52:38 +0000186 if (ObjectVT == MVT::i16) SubregOffset = 2;
Nate Begemana9795f82005-03-24 04:41:43 +0000187 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
188 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
Nate Begemane5846682005-04-04 06:52:38 +0000189 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
190 DAG.getConstant(SubregOffset, MVT::i32));
Nate Begemana9795f82005-03-24 04:41:43 +0000191 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN);
192 }
193
194 // Every 4 bytes of argument space consumes one of the GPRs available for
195 // argument passing.
196 if (GPR_remaining > 0) {
197 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
198 GPR_remaining -= delta;
199 GPR_idx += delta;
200 }
201 ArgOffset += ObjSize;
202
203 DAG.setRoot(newroot.getValue(1));
204 ArgValues.push_back(argt);
205 }
206
Nate Begemana9795f82005-03-24 04:41:43 +0000207 // If the function takes variable number of arguments, make a frame index for
208 // the start of the first vararg value... for expansion of llvm.va_start.
Nate Begemanfa554702005-04-03 22:13:27 +0000209 if (F.isVarArg()) {
Nate Begemana9795f82005-03-24 04:41:43 +0000210 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
Nate Begemanfa554702005-04-03 22:13:27 +0000211 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
Nate Begeman6644d4c2005-04-03 23:11:17 +0000212 // If this function is vararg, store any remaining integer argument regs
213 // to their spots on the stack so that they may be loaded by deferencing the
214 // result of va_next.
215 std::vector<SDOperand> MemOps;
216 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
217 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
218 SDOperand Val = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32, DAG.getRoot());
219 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
220 Val, FIN);
221 MemOps.push_back(Store);
222 // Increment the address by four for the next argument to store
223 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
224 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
225 }
226 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
Nate Begemanfa554702005-04-03 22:13:27 +0000227 }
Nate Begemana9795f82005-03-24 04:41:43 +0000228
229 return ArgValues;
230}
231
232std::pair<SDOperand, SDOperand>
233PPC32TargetLowering::LowerCallTo(SDOperand Chain,
Nate Begeman307e7442005-03-26 01:28:53 +0000234 const Type *RetTy, bool isVarArg,
235 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG) {
236 // args_to_use will accumulate outgoing args for the ISD::CALL case in
237 // SelectExpr to use to put the arguments in the appropriate registers.
Nate Begemana9795f82005-03-24 04:41:43 +0000238 std::vector<SDOperand> args_to_use;
Nate Begeman307e7442005-03-26 01:28:53 +0000239
240 // Count how many bytes are to be pushed on the stack, including the linkage
241 // area, and parameter passing area.
242 unsigned NumBytes = 24;
243
244 if (Args.empty()) {
Nate Begemana7e11a42005-04-01 05:57:17 +0000245 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
246 DAG.getConstant(NumBytes, getPointerTy()));
Nate Begeman307e7442005-03-26 01:28:53 +0000247 } else {
248 for (unsigned i = 0, e = Args.size(); i != e; ++i)
249 switch (getValueType(Args[i].second)) {
250 default: assert(0 && "Unknown value type!");
251 case MVT::i1:
252 case MVT::i8:
253 case MVT::i16:
254 case MVT::i32:
255 case MVT::f32:
256 NumBytes += 4;
257 break;
258 case MVT::i64:
259 case MVT::f64:
260 NumBytes += 8;
261 break;
262 }
263
264 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
265 // plus 32 bytes of argument space in case any called code gets funky on us.
266 if (NumBytes < 56) NumBytes = 56;
267
268 // Adjust the stack pointer for the new arguments...
269 // These operations are automatically eliminated by the prolog/epilog pass
270 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
271 DAG.getConstant(NumBytes, getPointerTy()));
272
273 // Set up a copy of the stack pointer for use loading and storing any
274 // arguments that may not fit in the registers available for argument
275 // passing.
276 SDOperand StackPtr = DAG.getCopyFromReg(PPC::R1, MVT::i32,
277 DAG.getEntryNode());
278
279 // Figure out which arguments are going to go in registers, and which in
280 // memory. Also, if this is a vararg function, floating point operations
281 // must be stored to our stack, and loaded into integer regs as well, if
282 // any integer regs are available for argument passing.
283 unsigned ArgOffset = 24;
284 unsigned GPR_remaining = 8;
285 unsigned FPR_remaining = 13;
Nate Begeman74d73452005-03-31 00:15:26 +0000286
287 std::vector<SDOperand> MemOps;
Nate Begeman307e7442005-03-26 01:28:53 +0000288 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
289 // PtrOff will be used to store the current argument to the stack if a
290 // register cannot be found for it.
291 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
292 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Nate Begemanf7e43382005-03-26 07:46:36 +0000293 MVT::ValueType ArgVT = getValueType(Args[i].second);
Nate Begeman307e7442005-03-26 01:28:53 +0000294
Nate Begemanf7e43382005-03-26 07:46:36 +0000295 switch (ArgVT) {
Nate Begeman307e7442005-03-26 01:28:53 +0000296 default: assert(0 && "Unexpected ValueType for argument!");
297 case MVT::i1:
298 case MVT::i8:
299 case MVT::i16:
300 // Promote the integer to 32 bits. If the input type is signed use a
301 // sign extend, otherwise use a zero extend.
302 if (Args[i].second->isSigned())
303 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
304 else
305 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
306 // FALL THROUGH
307 case MVT::i32:
308 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000309 args_to_use.push_back(Args[i].first);
Nate Begeman307e7442005-03-26 01:28:53 +0000310 --GPR_remaining;
311 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000312 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
313 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000314 }
315 ArgOffset += 4;
316 break;
317 case MVT::i64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000318 // If we have one free GPR left, we can place the upper half of the i64
319 // in it, and store the other half to the stack. If we have two or more
320 // free GPRs, then we can pass both halves of the i64 in registers.
321 if (GPR_remaining > 0) {
Nate Begemanf2622612005-03-26 02:17:46 +0000322 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
323 Args[i].first, DAG.getConstant(1, MVT::i32));
324 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
325 Args[i].first, DAG.getConstant(0, MVT::i32));
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000326 args_to_use.push_back(Hi);
Nate Begeman74d73452005-03-31 00:15:26 +0000327 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000328 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000329 args_to_use.push_back(Lo);
Nate Begeman74d73452005-03-31 00:15:26 +0000330 --GPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000331 } else {
332 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
333 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Nate Begeman74d73452005-03-31 00:15:26 +0000334 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
335 Lo, PtrOff));
Nate Begemanf7e43382005-03-26 07:46:36 +0000336 }
Nate Begeman307e7442005-03-26 01:28:53 +0000337 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000338 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
339 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000340 }
341 ArgOffset += 8;
342 break;
343 case MVT::f32:
Nate Begeman307e7442005-03-26 01:28:53 +0000344 case MVT::f64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000345 if (FPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000346 args_to_use.push_back(Args[i].first);
347 --FPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000348 if (isVarArg) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000349 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
350 Args[i].first, PtrOff);
351 MemOps.push_back(Store);
Nate Begeman74d73452005-03-31 00:15:26 +0000352 // Float varargs are always shadowed in available integer registers
353 if (GPR_remaining > 0) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000354 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff);
Nate Begeman74d73452005-03-31 00:15:26 +0000355 MemOps.push_back(Load);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000356 args_to_use.push_back(Load);
357 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000358 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000359 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Nate Begeman74d73452005-03-31 00:15:26 +0000360 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
361 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Nate Begeman96fc6812005-03-31 02:05:53 +0000362 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff);
Nate Begeman74d73452005-03-31 00:15:26 +0000363 MemOps.push_back(Load);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000364 args_to_use.push_back(Load);
365 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000366 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000367 } else {
368 // If we have any FPRs remaining, we may also have GPRs remaining.
369 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
370 // GPRs.
371 if (GPR_remaining > 0) {
372 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
373 --GPR_remaining;
374 }
375 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
376 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
377 --GPR_remaining;
378 }
Nate Begeman74d73452005-03-31 00:15:26 +0000379 }
Nate Begeman307e7442005-03-26 01:28:53 +0000380 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000381 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
382 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000383 }
Nate Begemanf7e43382005-03-26 07:46:36 +0000384 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
Nate Begeman307e7442005-03-26 01:28:53 +0000385 break;
386 }
Nate Begemana9795f82005-03-24 04:41:43 +0000387 }
Nate Begeman74d73452005-03-31 00:15:26 +0000388 if (!MemOps.empty())
389 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
Nate Begemana9795f82005-03-24 04:41:43 +0000390 }
391
392 std::vector<MVT::ValueType> RetVals;
393 MVT::ValueType RetTyVT = getValueType(RetTy);
394 if (RetTyVT != MVT::isVoid)
395 RetVals.push_back(RetTyVT);
396 RetVals.push_back(MVT::Other);
397
398 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
399 Chain, Callee, args_to_use), 0);
400 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
401 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
402 DAG.getConstant(NumBytes, getPointerTy()));
403 return std::make_pair(TheCall, Chain);
404}
405
406std::pair<SDOperand, SDOperand>
407PPC32TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
408 //vastart just returns the address of the VarArgsFrameIndex slot.
409 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
410}
411
412std::pair<SDOperand,SDOperand> PPC32TargetLowering::
413LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
414 const Type *ArgTy, SelectionDAG &DAG) {
Nate Begemanc7b09f12005-03-25 08:34:25 +0000415 MVT::ValueType ArgVT = getValueType(ArgTy);
416 SDOperand Result;
417 if (!isVANext) {
418 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList);
419 } else {
420 unsigned Amt;
421 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
422 Amt = 4;
423 else {
424 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
425 "Other types should have been promoted for varargs!");
426 Amt = 8;
427 }
428 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
429 DAG.getConstant(Amt, VAList.getValueType()));
430 }
431 return std::make_pair(Result, Chain);
Nate Begemana9795f82005-03-24 04:41:43 +0000432}
433
434
435std::pair<SDOperand, SDOperand> PPC32TargetLowering::
436LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
437 SelectionDAG &DAG) {
Nate Begeman01d05262005-03-30 01:45:43 +0000438 assert(0 && "LowerFrameReturnAddress unimplemented");
Nate Begemana9795f82005-03-24 04:41:43 +0000439 abort();
440}
441
442namespace {
Nate Begemanaa73a9f2005-04-03 11:20:20 +0000443Statistic<>NotLogic("ppc-codegen", "Number of inverted logical ops");
Nate Begeman93075ec2005-04-04 23:40:36 +0000444Statistic<>FusedFP("ppc-codegen", "Number of fused fp operations");
Nate Begemana9795f82005-03-24 04:41:43 +0000445//===--------------------------------------------------------------------===//
446/// ISel - PPC32 specific code to select PPC32 machine instructions for
447/// SelectionDAG operations.
448//===--------------------------------------------------------------------===//
449class ISel : public SelectionDAGISel {
Nate Begemana9795f82005-03-24 04:41:43 +0000450 PPC32TargetLowering PPC32Lowering;
Nate Begeman815d6da2005-04-06 00:25:27 +0000451 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
452 // for sdiv and udiv until it is put into the future
453 // dag combiner.
Nate Begemana9795f82005-03-24 04:41:43 +0000454
455 /// ExprMap - As shared expressions are codegen'd, we keep track of which
456 /// vreg the value is produced in, so we only emit one copy of each compiled
457 /// tree.
458 std::map<SDOperand, unsigned> ExprMap;
Nate Begemanc7b09f12005-03-25 08:34:25 +0000459
460 unsigned GlobalBaseReg;
461 bool GlobalBaseInitialized;
Nate Begemana9795f82005-03-24 04:41:43 +0000462
463public:
Nate Begeman815d6da2005-04-06 00:25:27 +0000464 ISel(TargetMachine &TM) : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM),
465 ISelDAG(0) {}
Nate Begemana9795f82005-03-24 04:41:43 +0000466
Nate Begemanc7b09f12005-03-25 08:34:25 +0000467 /// runOnFunction - Override this function in order to reset our per-function
468 /// variables.
469 virtual bool runOnFunction(Function &Fn) {
470 // Make sure we re-emit a set of the global base reg if necessary
471 GlobalBaseInitialized = false;
472 return SelectionDAGISel::runOnFunction(Fn);
473 }
474
Nate Begemana9795f82005-03-24 04:41:43 +0000475 /// InstructionSelectBasicBlock - This callback is invoked by
476 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
477 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
478 DEBUG(BB->dump());
479 // Codegen the basic block.
Nate Begeman815d6da2005-04-06 00:25:27 +0000480 ISelDAG = &DAG;
Nate Begemana9795f82005-03-24 04:41:43 +0000481 Select(DAG.getRoot());
482
483 // Clear state used for selection.
484 ExprMap.clear();
Nate Begeman815d6da2005-04-06 00:25:27 +0000485 ISelDAG = 0;
Nate Begemana9795f82005-03-24 04:41:43 +0000486 }
Nate Begeman815d6da2005-04-06 00:25:27 +0000487
488 // dag -> dag expanders for integer divide by constant
489 SDOperand BuildSDIVSequence(SDOperand N);
490 SDOperand BuildUDIVSequence(SDOperand N);
Nate Begemana9795f82005-03-24 04:41:43 +0000491
Nate Begemandffcfcc2005-04-01 00:32:34 +0000492 unsigned getGlobalBaseReg();
Nate Begeman6b559972005-04-01 02:59:27 +0000493 unsigned getConstDouble(double floatVal, unsigned Result);
Nate Begeman7ddecb42005-04-06 23:51:40 +0000494 bool SelectBitfieldInsert(SDOperand OR, unsigned Result);
Nate Begemandffcfcc2005-04-01 00:32:34 +0000495 unsigned SelectSetCR0(SDOperand CC);
Nate Begemana9795f82005-03-24 04:41:43 +0000496 unsigned SelectExpr(SDOperand N);
497 unsigned SelectExprFP(SDOperand N, unsigned Result);
498 void Select(SDOperand N);
499
Nate Begeman04730362005-04-01 04:45:11 +0000500 bool SelectAddr(SDOperand N, unsigned& Reg, int& offset);
Nate Begemana9795f82005-03-24 04:41:43 +0000501 void SelectBranchCC(SDOperand N);
502};
503
Nate Begeman80196b12005-04-05 00:15:08 +0000504/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
505/// returns zero when the input is not exactly a power of two.
506static unsigned ExactLog2(unsigned Val) {
507 if (Val == 0 || (Val & (Val-1))) return 0;
508 unsigned Count = 0;
509 while (Val != 1) {
510 Val >>= 1;
511 ++Count;
512 }
513 return Count;
514}
515
Nate Begeman7ddecb42005-04-06 23:51:40 +0000516// IsRunOfOnes - returns true if Val consists of one contiguous run of 1's with
517// any number of 0's on either side. the 1's are allowed to wrap from LSB to
518// MSB. so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
519// not, since all 1's are not contiguous.
520static bool IsRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
521 bool isRun = true;
522 MB = 0;
523 ME = 0;
524
525 // look for first set bit
526 int i = 0;
527 for (; i < 32; i++) {
528 if ((Val & (1 << (31 - i))) != 0) {
529 MB = i;
530 ME = i;
531 break;
532 }
533 }
534
535 // look for last set bit
536 for (; i < 32; i++) {
537 if ((Val & (1 << (31 - i))) == 0)
538 break;
539 ME = i;
540 }
541
542 // look for next set bit
543 for (; i < 32; i++) {
544 if ((Val & (1 << (31 - i))) != 0)
545 break;
546 }
547
548 // if we exhausted all the bits, we found a match at this point for 0*1*0*
549 if (i == 32)
550 return true;
551
552 // since we just encountered more 1's, if it doesn't wrap around to the
553 // most significant bit of the word, then we did not find a match to 1*0*1* so
554 // exit.
555 if (MB != 0)
556 return false;
557
558 // look for last set bit
559 for (MB = i; i < 32; i++) {
560 if ((Val & (1 << (31 - i))) == 0)
561 break;
562 }
563
564 // if we exhausted all the bits, then we found a match for 1*0*1*, otherwise,
565 // the value is not a run of ones.
566 if (i == 32)
567 return true;
568 return false;
569}
570
Nate Begeman439b4442005-04-05 04:22:58 +0000571/// getImmediateForOpcode - This method returns a value indicating whether
Nate Begemana9795f82005-03-24 04:41:43 +0000572/// the ConstantSDNode N can be used as an immediate to Opcode. The return
573/// values are either 0, 1 or 2. 0 indicates that either N is not a
574/// ConstantSDNode, or is not suitable for use by that opcode. A return value
575/// of 1 indicates that the constant may be used in normal immediate form. A
576/// return value of 2 indicates that the constant may be used in shifted
Nate Begeman439b4442005-04-05 04:22:58 +0000577/// immediate form. A return value of 3 indicates that log base 2 of the
Nate Begeman815d6da2005-04-06 00:25:27 +0000578/// constant may be used. A return value of 4 indicates that the constant is
579/// suitable for conversion into a magic number for integer division.
Nate Begemana9795f82005-03-24 04:41:43 +0000580///
Nate Begeman439b4442005-04-05 04:22:58 +0000581static unsigned getImmediateForOpcode(SDOperand N, unsigned Opcode,
582 unsigned& Imm, bool U = false) {
Nate Begemana9795f82005-03-24 04:41:43 +0000583 if (N.getOpcode() != ISD::Constant) return 0;
584
585 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
586
587 switch(Opcode) {
588 default: return 0;
589 case ISD::ADD:
590 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
591 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
592 break;
593 case ISD::AND:
594 case ISD::XOR:
595 case ISD::OR:
596 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
597 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
598 break;
Nate Begeman307e7442005-03-26 01:28:53 +0000599 case ISD::MUL:
Nate Begeman27523a12005-04-02 00:42:16 +0000600 case ISD::SUB:
Nate Begeman307e7442005-03-26 01:28:53 +0000601 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
602 break;
Nate Begeman3e897162005-03-31 23:55:40 +0000603 case ISD::SETCC:
604 if (U && (v >= 0 && v <= 65535)) { Imm = v & 0xFFFF; return 1; }
605 if (!U && (v <= 32767 && v >= -32768)) { Imm = v & 0xFFFF; return 1; }
606 break;
Nate Begeman80196b12005-04-05 00:15:08 +0000607 case ISD::SDIV:
Nate Begeman439b4442005-04-05 04:22:58 +0000608 if ((Imm = ExactLog2(v))) { return 3; }
Nate Begeman815d6da2005-04-06 00:25:27 +0000609 if (v <= -2 || v >= 2) { return 4; }
610 break;
611 case ISD::UDIV:
Nate Begeman27b4c232005-04-06 06:44:57 +0000612 if (v > 1) { return 4; }
Nate Begeman80196b12005-04-05 00:15:08 +0000613 break;
Nate Begemana9795f82005-03-24 04:41:43 +0000614 }
615 return 0;
616}
Nate Begeman3e897162005-03-31 23:55:40 +0000617
618/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
619/// to Condition. If the Condition is unordered or unsigned, the bool argument
620/// U is set to true, otherwise it is set to false.
621static unsigned getBCCForSetCC(unsigned Condition, bool& U) {
622 U = false;
623 switch (Condition) {
624 default: assert(0 && "Unknown condition!"); abort();
625 case ISD::SETEQ: return PPC::BEQ;
626 case ISD::SETNE: return PPC::BNE;
627 case ISD::SETULT: U = true;
628 case ISD::SETLT: return PPC::BLT;
629 case ISD::SETULE: U = true;
630 case ISD::SETLE: return PPC::BLE;
631 case ISD::SETUGT: U = true;
632 case ISD::SETGT: return PPC::BGT;
633 case ISD::SETUGE: U = true;
634 case ISD::SETGE: return PPC::BGE;
635 }
Nate Begeman04730362005-04-01 04:45:11 +0000636 return 0;
637}
638
639/// IndexedOpForOp - Return the indexed variant for each of the PowerPC load
640/// and store immediate instructions.
641static unsigned IndexedOpForOp(unsigned Opcode) {
642 switch(Opcode) {
643 default: assert(0 && "Unknown opcode!"); abort();
644 case PPC::LBZ: return PPC::LBZX; case PPC::STB: return PPC::STBX;
645 case PPC::LHZ: return PPC::LHZX; case PPC::STH: return PPC::STHX;
646 case PPC::LHA: return PPC::LHAX; case PPC::STW: return PPC::STWX;
647 case PPC::LWZ: return PPC::LWZX; case PPC::STFS: return PPC::STFSX;
648 case PPC::LFS: return PPC::LFSX; case PPC::STFD: return PPC::STFDX;
649 case PPC::LFD: return PPC::LFDX;
650 }
651 return 0;
Nate Begeman3e897162005-03-31 23:55:40 +0000652}
Nate Begeman815d6da2005-04-06 00:25:27 +0000653
654// Structure used to return the necessary information to codegen an SDIV as
655// a multiply.
656struct ms {
657 int m; // magic number
658 int s; // shift amount
659};
660
661struct mu {
662 unsigned int m; // magic number
663 int a; // add indicator
664 int s; // shift amount
665};
666
667/// magic - calculate the magic numbers required to codegen an integer sdiv as
668/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
669/// or -1.
670static struct ms magic(int d) {
671 int p;
672 unsigned int ad, anc, delta, q1, r1, q2, r2, t;
673 const unsigned int two31 = 2147483648U; // 2^31
674 struct ms mag;
675
676 ad = abs(d);
677 t = two31 + ((unsigned int)d >> 31);
678 anc = t - 1 - t%ad; // absolute value of nc
679 p = 31; // initialize p
680 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
681 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
682 q2 = two31/ad; // initialize q2 = 2p/abs(d)
683 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
684 do {
685 p = p + 1;
686 q1 = 2*q1; // update q1 = 2p/abs(nc)
687 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
688 if (r1 >= anc) { // must be unsigned comparison
689 q1 = q1 + 1;
690 r1 = r1 - anc;
691 }
692 q2 = 2*q2; // update q2 = 2p/abs(d)
693 r2 = 2*r2; // update r2 = rem(2p/abs(d))
694 if (r2 >= ad) { // must be unsigned comparison
695 q2 = q2 + 1;
696 r2 = r2 - ad;
697 }
698 delta = ad - r2;
699 } while (q1 < delta || (q1 == delta && r1 == 0));
700
701 mag.m = q2 + 1;
702 if (d < 0) mag.m = -mag.m; // resulting magic number
703 mag.s = p - 32; // resulting shift
704 return mag;
705}
706
707/// magicu - calculate the magic numbers required to codegen an integer udiv as
708/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
709static struct mu magicu(unsigned d)
710{
711 int p;
712 unsigned int nc, delta, q1, r1, q2, r2;
713 struct mu magu;
714 magu.a = 0; // initialize "add" indicator
715 nc = - 1 - (-d)%d;
716 p = 31; // initialize p
717 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
718 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
719 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
720 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
721 do {
722 p = p + 1;
723 if (r1 >= nc - r1 ) {
724 q1 = 2*q1 + 1; // update q1
725 r1 = 2*r1 - nc; // update r1
726 }
727 else {
728 q1 = 2*q1; // update q1
729 r1 = 2*r1; // update r1
730 }
731 if (r2 + 1 >= d - r2) {
732 if (q2 >= 0x7FFFFFFF) magu.a = 1;
733 q2 = 2*q2 + 1; // update q2
734 r2 = 2*r2 + 1 - d; // update r2
735 }
736 else {
737 if (q2 >= 0x80000000) magu.a = 1;
738 q2 = 2*q2; // update q2
739 r2 = 2*r2 + 1; // update r2
740 }
741 delta = d - 1 - r2;
742 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
743 magu.m = q2 + 1; // resulting magic number
744 magu.s = p - 32; // resulting shift
745 return magu;
746}
747}
748
749/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
750/// return a DAG expression to select that will generate the same value by
751/// multiplying by a magic number. See:
752/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
753SDOperand ISel::BuildSDIVSequence(SDOperand N) {
754 int d = (int)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
755 ms magics = magic(d);
756 // Multiply the numerator (operand 0) by the magic value
757 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i32, N.getOperand(0),
758 ISelDAG->getConstant(magics.m, MVT::i32));
759 // If d > 0 and m < 0, add the numerator
760 if (d > 0 && magics.m < 0)
761 Q = ISelDAG->getNode(ISD::ADD, MVT::i32, Q, N.getOperand(0));
762 // If d < 0 and m > 0, subtract the numerator.
763 if (d < 0 && magics.m > 0)
764 Q = ISelDAG->getNode(ISD::SUB, MVT::i32, Q, N.getOperand(0));
765 // Shift right algebraic if shift value is nonzero
766 if (magics.s > 0)
767 Q = ISelDAG->getNode(ISD::SRA, MVT::i32, Q,
768 ISelDAG->getConstant(magics.s, MVT::i32));
769 // Extract the sign bit and add it to the quotient
770 SDOperand T =
771 ISelDAG->getNode(ISD::SRL, MVT::i32, Q, ISelDAG->getConstant(31, MVT::i32));
Nate Begeman27b4c232005-04-06 06:44:57 +0000772 return ISelDAG->getNode(ISD::ADD, MVT::i32, Q, T);
Nate Begeman815d6da2005-04-06 00:25:27 +0000773}
774
775/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
776/// return a DAG expression to select that will generate the same value by
777/// multiplying by a magic number. See:
778/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
779SDOperand ISel::BuildUDIVSequence(SDOperand N) {
780 unsigned d =
781 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
782 mu magics = magicu(d);
783 // Multiply the numerator (operand 0) by the magic value
784 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i32, N.getOperand(0),
785 ISelDAG->getConstant(magics.m, MVT::i32));
786 if (magics.a == 0) {
787 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, Q,
788 ISelDAG->getConstant(magics.s, MVT::i32));
789 } else {
790 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i32, N.getOperand(0), Q);
791 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
792 ISelDAG->getConstant(1, MVT::i32));
793 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i32, NPQ, Q);
794 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
795 ISelDAG->getConstant(magics.s-1, MVT::i32));
796 }
Nate Begeman27b4c232005-04-06 06:44:57 +0000797 return Q;
Nate Begemana9795f82005-03-24 04:41:43 +0000798}
799
Nate Begemanc7b09f12005-03-25 08:34:25 +0000800/// getGlobalBaseReg - Output the instructions required to put the
801/// base address to use for accessing globals into a register.
802///
803unsigned ISel::getGlobalBaseReg() {
804 if (!GlobalBaseInitialized) {
805 // Insert the set of GlobalBaseReg into the first MBB of the function
806 MachineBasicBlock &FirstMBB = BB->getParent()->front();
807 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
808 GlobalBaseReg = MakeReg(MVT::i32);
809 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
810 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR);
811 GlobalBaseInitialized = true;
812 }
813 return GlobalBaseReg;
814}
815
Nate Begeman6b559972005-04-01 02:59:27 +0000816/// getConstDouble - Loads a floating point value into a register, via the
817/// Constant Pool. Optionally takes a register in which to load the value.
818unsigned ISel::getConstDouble(double doubleVal, unsigned Result=0) {
819 unsigned Tmp1 = MakeReg(MVT::i32);
820 if (0 == Result) Result = MakeReg(MVT::f64);
821 MachineConstantPool *CP = BB->getParent()->getConstantPool();
822 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, doubleVal);
823 unsigned CPI = CP->getConstantPoolIndex(CFP);
824 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
825 .addConstantPoolIndex(CPI);
826 BuildMI(BB, PPC::LFD, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
827 return Result;
828}
829
Nate Begeman7ddecb42005-04-06 23:51:40 +0000830/// SelectBitfieldInsert - turn an or of two masked values into
831/// the rotate left word immediate then mask insert (rlwimi) instruction.
832/// Returns true on success, false if the caller still needs to select OR.
833///
834/// Patterns matched:
835/// 1. or shl, and 5. or and, and
836/// 2. or and, shl 6. or shl, shr
837/// 3. or shr, and 7. or shr, shl
838/// 4. or and, shr
839bool ISel::SelectBitfieldInsert(SDOperand OR, unsigned Result) {
840 unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, Amount = 0;
841 unsigned Op0Opc = OR.getOperand(0).getOpcode();
842 unsigned Op1Opc = OR.getOperand(1).getOpcode();
843
844 // Verify that we have the correct opcodes
845 if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc)
846 return false;
847 if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc)
848 return false;
849
850 // Generate Mask value for Target
851 if (ConstantSDNode *CN =
852 dyn_cast<ConstantSDNode>(OR.getOperand(0).getOperand(1).Val)) {
853 switch(Op0Opc) {
854 case ISD::SHL: TgtMask <<= (unsigned)CN->getValue(); break;
855 case ISD::SRL: TgtMask >>= (unsigned)CN->getValue(); break;
856 case ISD::AND: TgtMask &= (unsigned)CN->getValue(); break;
857 }
858 } else {
859 return false;
860 }
861
862 // Generate Mask value for Insert
863 if (ConstantSDNode *CN =
864 dyn_cast<ConstantSDNode>(OR.getOperand(1).getOperand(1).Val)) {
865 switch(Op1Opc) {
866 case ISD::SHL:
867 Amount = CN->getValue();
868 InsMask <<= Amount;
869 break;
870 case ISD::SRL:
871 Amount = CN->getValue();
872 InsMask >>= Amount;
873 Amount = 32-Amount;
874 break;
875 case ISD::AND:
876 InsMask &= (unsigned)CN->getValue();
877 break;
878 }
879 } else {
880 return false;
881 }
882
883 // Verify that the Target mask and Insert mask together form a full word mask
884 // and that the Insert mask is a run of set bits (which implies both are runs
885 // of set bits). Given that, Select the arguments and generate the rlwimi
886 // instruction.
887 unsigned MB, ME;
888 if (((TgtMask ^ InsMask) == 0xFFFFFFFF) && IsRunOfOnes(InsMask, MB, ME)) {
889 unsigned Tmp1, Tmp2;
890 if (Op0Opc == ISD::AND)
891 Tmp1 = SelectExpr(OR.getOperand(0).getOperand(0));
892 else
893 Tmp1 = SelectExpr(OR.getOperand(0));
894 Tmp2 = SelectExpr(OR.getOperand(1).getOperand(0));
895 BuildMI(BB, PPC::RLWIMI, 5, Result).addReg(Tmp1).addReg(Tmp2)
896 .addImm(Amount).addImm(MB).addImm(ME);
897 return true;
898 }
899 return false;
900}
901
Nate Begemandffcfcc2005-04-01 00:32:34 +0000902unsigned ISel::SelectSetCR0(SDOperand CC) {
903 unsigned Opc, Tmp1, Tmp2;
904 static const unsigned CompareOpcodes[] =
905 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
906
907 // If the first operand to the select is a SETCC node, then we can fold it
908 // into the branch that selects which value to return.
909 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
910 if (SetCC && CC.getOpcode() == ISD::SETCC) {
911 bool U;
912 Opc = getBCCForSetCC(SetCC->getCondition(), U);
913 Tmp1 = SelectExpr(SetCC->getOperand(0));
914
Nate Begeman439b4442005-04-05 04:22:58 +0000915 // Pass the optional argument U to getImmediateForOpcode for SETCC,
Nate Begemandffcfcc2005-04-01 00:32:34 +0000916 // so that it knows whether the SETCC immediate range is signed or not.
Nate Begeman439b4442005-04-05 04:22:58 +0000917 if (1 == getImmediateForOpcode(SetCC->getOperand(1), ISD::SETCC,
918 Tmp2, U)) {
Nate Begemandffcfcc2005-04-01 00:32:34 +0000919 if (U)
920 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(Tmp2);
921 else
922 BuildMI(BB, PPC::CMPWI, 2, PPC::CR0).addReg(Tmp1).addSImm(Tmp2);
923 } else {
924 bool IsInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
925 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
926 Tmp2 = SelectExpr(SetCC->getOperand(1));
927 BuildMI(BB, CompareOpc, 2, PPC::CR0).addReg(Tmp1).addReg(Tmp2);
928 }
929 } else {
930 Tmp1 = SelectExpr(CC);
931 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(0);
932 Opc = PPC::BNE;
933 }
934 return Opc;
935}
936
937/// Check to see if the load is a constant offset from a base register
Nate Begeman04730362005-04-01 04:45:11 +0000938bool ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
Nate Begemana9795f82005-03-24 04:41:43 +0000939{
Nate Begeman96fc6812005-03-31 02:05:53 +0000940 unsigned imm = 0, opcode = N.getOpcode();
Nate Begeman04730362005-04-01 04:45:11 +0000941 if (N.getOpcode() == ISD::ADD) {
942 Reg = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +0000943 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, imm)) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000944 offset = imm;
Nate Begeman04730362005-04-01 04:45:11 +0000945 return false;
946 }
947 offset = SelectExpr(N.getOperand(1));
948 return true;
949 }
Nate Begemana9795f82005-03-24 04:41:43 +0000950 Reg = SelectExpr(N);
951 offset = 0;
Nate Begeman04730362005-04-01 04:45:11 +0000952 return false;
Nate Begemana9795f82005-03-24 04:41:43 +0000953}
954
955void ISel::SelectBranchCC(SDOperand N)
956{
957 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
958 MachineBasicBlock *Dest =
959 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Nate Begeman3e897162005-03-31 23:55:40 +0000960
Nate Begeman439b4442005-04-05 04:22:58 +0000961 // Get the MBB we will fall through to so that we can hand it off to the
962 // branch selection pass as an argument to the PPC::COND_BRANCH pseudo op.
Nate Begemanc8c5c8f2005-04-05 04:32:16 +0000963 //ilist<MachineBasicBlock>::iterator It = BB;
964 //MachineBasicBlock *Fallthrough = ++It;
Nate Begeman439b4442005-04-05 04:22:58 +0000965
Nate Begemana9795f82005-03-24 04:41:43 +0000966 Select(N.getOperand(0)); //chain
Nate Begemandffcfcc2005-04-01 00:32:34 +0000967 unsigned Opc = SelectSetCR0(N.getOperand(1));
Nate Begemanc8c5c8f2005-04-05 04:32:16 +0000968 // FIXME: Use this once we have something approximating two-way branches
969 // We cannot currently use this in case the ISel hands us something like
970 // BRcc MBBx
971 // BR MBBy
972 // since the fallthrough basic block for the conditional branch does not start
973 // with the unconditional branch (it is skipped over).
974 //BuildMI(BB, PPC::COND_BRANCH, 4).addReg(PPC::CR0).addImm(Opc)
975 // .addMBB(Dest).addMBB(Fallthrough);
976 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(Dest);
Nate Begemana9795f82005-03-24 04:41:43 +0000977 return;
978}
979
980unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
981{
982 unsigned Tmp1, Tmp2, Tmp3;
983 unsigned Opc = 0;
984 SDNode *Node = N.Val;
985 MVT::ValueType DestType = N.getValueType();
986 unsigned opcode = N.getOpcode();
987
988 switch (opcode) {
989 default:
990 Node->dump();
991 assert(0 && "Node not handled!\n");
992
Nate Begeman23afcfb2005-03-29 22:48:55 +0000993 case ISD::SELECT: {
Nate Begeman3e897162005-03-31 23:55:40 +0000994 // Attempt to generate FSEL. We can do this whenever we have an FP result,
995 // and an FP comparison in the SetCC node.
996 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val);
997 if (SetCC && N.getOperand(0).getOpcode() == ISD::SETCC &&
998 !MVT::isInteger(SetCC->getOperand(0).getValueType()) &&
999 SetCC->getCondition() != ISD::SETEQ &&
1000 SetCC->getCondition() != ISD::SETNE) {
1001 MVT::ValueType VT = SetCC->getOperand(0).getValueType();
1002 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
1003 unsigned TV = SelectExpr(N.getOperand(1)); // Use if TRUE
1004 unsigned FV = SelectExpr(N.getOperand(2)); // Use if FALSE
1005
1006 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1));
1007 if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) {
1008 switch(SetCC->getCondition()) {
1009 default: assert(0 && "Invalid FSEL condition"); abort();
1010 case ISD::SETULT:
1011 case ISD::SETLT:
1012 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(FV).addReg(TV);
1013 return Result;
1014 case ISD::SETUGE:
1015 case ISD::SETGE:
1016 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV);
1017 return Result;
1018 case ISD::SETUGT:
1019 case ISD::SETGT: {
1020 Tmp2 = MakeReg(VT);
1021 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
1022 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(FV).addReg(TV);
1023 return Result;
1024 }
1025 case ISD::SETULE:
1026 case ISD::SETLE: {
1027 Tmp2 = MakeReg(VT);
1028 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
1029 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV);
1030 return Result;
1031 }
1032 }
1033 } else {
1034 Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS;
1035 Tmp2 = SelectExpr(SetCC->getOperand(1));
1036 Tmp3 = MakeReg(VT);
1037 switch(SetCC->getCondition()) {
1038 default: assert(0 && "Invalid FSEL condition"); abort();
1039 case ISD::SETULT:
1040 case ISD::SETLT:
1041 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1042 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
1043 return Result;
1044 case ISD::SETUGE:
1045 case ISD::SETGE:
1046 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1047 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
1048 return Result;
1049 case ISD::SETUGT:
1050 case ISD::SETGT:
1051 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1052 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
1053 return Result;
1054 case ISD::SETULE:
1055 case ISD::SETLE:
1056 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1057 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
1058 return Result;
1059 }
1060 }
1061 assert(0 && "Should never get here");
1062 return 0;
1063 }
1064
Nate Begeman31318e42005-04-01 07:21:30 +00001065 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
1066 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman6cb2e1b2005-04-01 08:57:43 +00001067 Opc = SelectSetCR0(N.getOperand(0));
Nate Begeman31318e42005-04-01 07:21:30 +00001068
Nate Begeman23afcfb2005-03-29 22:48:55 +00001069 // Create an iterator with which to insert the MBB for copying the false
1070 // value and the MBB to hold the PHI instruction for this SetCC.
1071 MachineBasicBlock *thisMBB = BB;
1072 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1073 ilist<MachineBasicBlock>::iterator It = BB;
1074 ++It;
1075
1076 // thisMBB:
1077 // ...
1078 // TrueVal = ...
1079 // cmpTY cr0, r1, r2
1080 // bCC copy1MBB
1081 // fallthrough --> copy0MBB
Nate Begeman23afcfb2005-03-29 22:48:55 +00001082 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1083 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman6cb2e1b2005-04-01 08:57:43 +00001084 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
Nate Begeman23afcfb2005-03-29 22:48:55 +00001085 MachineFunction *F = BB->getParent();
1086 F->getBasicBlockList().insert(It, copy0MBB);
1087 F->getBasicBlockList().insert(It, sinkMBB);
1088 // Update machine-CFG edges
1089 BB->addSuccessor(copy0MBB);
1090 BB->addSuccessor(sinkMBB);
1091
1092 // copy0MBB:
1093 // %FalseValue = ...
1094 // # fallthrough to sinkMBB
1095 BB = copy0MBB;
Nate Begeman23afcfb2005-03-29 22:48:55 +00001096 // Update machine-CFG edges
1097 BB->addSuccessor(sinkMBB);
1098
1099 // sinkMBB:
1100 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1101 // ...
1102 BB = sinkMBB;
1103 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1104 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1105 return Result;
1106 }
Nate Begeman27eeb002005-04-02 05:59:34 +00001107
1108 case ISD::FNEG:
Nate Begeman93075ec2005-04-04 23:40:36 +00001109 if (!NoExcessFPPrecision &&
1110 ISD::ADD == N.getOperand(0).getOpcode() &&
1111 N.getOperand(0).Val->hasOneUse() &&
1112 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
1113 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
Nate Begeman80196b12005-04-05 00:15:08 +00001114 ++FusedFP; // Statistic
Nate Begeman93075ec2005-04-04 23:40:36 +00001115 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1116 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
1117 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
1118 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
1119 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1120 } else if (!NoExcessFPPrecision &&
Nate Begemane88aa5b2005-04-09 03:05:51 +00001121 ISD::ADD == N.getOperand(0).getOpcode() &&
Nate Begeman93075ec2005-04-04 23:40:36 +00001122 N.getOperand(0).Val->hasOneUse() &&
Nate Begemane88aa5b2005-04-09 03:05:51 +00001123 ISD::MUL == N.getOperand(0).getOperand(1).getOpcode() &&
1124 N.getOperand(0).getOperand(1).Val->hasOneUse()) {
Nate Begeman80196b12005-04-05 00:15:08 +00001125 ++FusedFP; // Statistic
Nate Begemane88aa5b2005-04-09 03:05:51 +00001126 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1127 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(1));
1128 Tmp3 = SelectExpr(N.getOperand(0).getOperand(0));
1129 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
Nate Begeman93075ec2005-04-04 23:40:36 +00001130 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1131 } else if (ISD::FABS == N.getOperand(0).getOpcode()) {
Nate Begeman27eeb002005-04-02 05:59:34 +00001132 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1133 BuildMI(BB, PPC::FNABS, 1, Result).addReg(Tmp1);
1134 } else {
1135 Tmp1 = SelectExpr(N.getOperand(0));
1136 BuildMI(BB, PPC::FNEG, 1, Result).addReg(Tmp1);
1137 }
1138 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001139
Nate Begeman27eeb002005-04-02 05:59:34 +00001140 case ISD::FABS:
1141 Tmp1 = SelectExpr(N.getOperand(0));
1142 BuildMI(BB, PPC::FABS, 1, Result).addReg(Tmp1);
1143 return Result;
1144
Nate Begemana9795f82005-03-24 04:41:43 +00001145 case ISD::FP_ROUND:
1146 assert (DestType == MVT::f32 &&
1147 N.getOperand(0).getValueType() == MVT::f64 &&
1148 "only f64 to f32 conversion supported here");
1149 Tmp1 = SelectExpr(N.getOperand(0));
1150 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
1151 return Result;
1152
1153 case ISD::FP_EXTEND:
1154 assert (DestType == MVT::f64 &&
1155 N.getOperand(0).getValueType() == MVT::f32 &&
1156 "only f32 to f64 conversion supported here");
1157 Tmp1 = SelectExpr(N.getOperand(0));
1158 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
1159 return Result;
1160
1161 case ISD::CopyFromReg:
Nate Begemanf2622612005-03-26 02:17:46 +00001162 if (Result == 1)
1163 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1164 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1165 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
1166 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001167
Nate Begeman6d369cc2005-04-01 01:08:07 +00001168 case ISD::ConstantFP: {
Nate Begeman6d369cc2005-04-01 01:08:07 +00001169 ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
Nate Begeman6b559972005-04-01 02:59:27 +00001170 Result = getConstDouble(CN->getValue(), Result);
Nate Begeman6d369cc2005-04-01 01:08:07 +00001171 return Result;
1172 }
Nate Begemana9795f82005-03-24 04:41:43 +00001173
Nate Begemana9795f82005-03-24 04:41:43 +00001174 case ISD::ADD:
Nate Begeman93075ec2005-04-04 23:40:36 +00001175 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1176 N.getOperand(0).Val->hasOneUse()) {
1177 ++FusedFP; // Statistic
1178 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1179 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1180 Tmp3 = SelectExpr(N.getOperand(1));
1181 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1182 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1183 return Result;
1184 }
Nate Begemane88aa5b2005-04-09 03:05:51 +00001185 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1186 N.getOperand(1).Val->hasOneUse()) {
1187 ++FusedFP; // Statistic
1188 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1189 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1190 Tmp3 = SelectExpr(N.getOperand(0));
1191 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1192 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1193 return Result;
1194 }
Nate Begeman93075ec2005-04-04 23:40:36 +00001195 Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS;
1196 Tmp1 = SelectExpr(N.getOperand(0));
1197 Tmp2 = SelectExpr(N.getOperand(1));
1198 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1199 return Result;
1200
Nate Begemana9795f82005-03-24 04:41:43 +00001201 case ISD::SUB:
Nate Begeman93075ec2005-04-04 23:40:36 +00001202 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1203 N.getOperand(0).Val->hasOneUse()) {
1204 ++FusedFP; // Statistic
1205 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1206 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1207 Tmp3 = SelectExpr(N.getOperand(1));
1208 Opc = DestType == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS;
1209 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1210 return Result;
1211 }
Nate Begemane88aa5b2005-04-09 03:05:51 +00001212 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1213 N.getOperand(1).Val->hasOneUse()) {
1214 ++FusedFP; // Statistic
1215 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1216 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1217 Tmp3 = SelectExpr(N.getOperand(0));
1218 Opc = DestType == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS;
1219 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1220 return Result;
1221 }
Nate Begeman93075ec2005-04-04 23:40:36 +00001222 Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS;
1223 Tmp1 = SelectExpr(N.getOperand(0));
1224 Tmp2 = SelectExpr(N.getOperand(1));
1225 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1226 return Result;
1227
1228 case ISD::MUL:
Nate Begemana9795f82005-03-24 04:41:43 +00001229 case ISD::SDIV:
1230 switch( opcode ) {
1231 case ISD::MUL: Opc = DestType == MVT::f64 ? PPC::FMUL : PPC::FMULS; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001232 case ISD::SDIV: Opc = DestType == MVT::f64 ? PPC::FDIV : PPC::FDIVS; break;
1233 };
Nate Begemana9795f82005-03-24 04:41:43 +00001234 Tmp1 = SelectExpr(N.getOperand(0));
1235 Tmp2 = SelectExpr(N.getOperand(1));
1236 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1237 return Result;
1238
Nate Begemana9795f82005-03-24 04:41:43 +00001239 case ISD::UINT_TO_FP:
Nate Begemanfdcf3412005-03-30 19:38:35 +00001240 case ISD::SINT_TO_FP: {
1241 assert (N.getOperand(0).getValueType() == MVT::i32
1242 && "int to float must operate on i32");
1243 bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
1244 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1245 Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into
1246 Tmp3 = MakeReg(MVT::i32); // temp reg to hold the conversion constant
1247 unsigned ConstF = MakeReg(MVT::f64); // temp reg to hold the fp constant
1248
1249 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1250 MachineConstantPool *CP = BB->getParent()->getConstantPool();
1251
1252 // FIXME: pull this FP constant generation stuff out into something like
1253 // the simple ISel's getReg.
1254 if (IsUnsigned) {
1255 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000000p52);
1256 unsigned CPI = CP->getConstantPoolIndex(CFP);
1257 // Load constant fp value
1258 unsigned Tmp4 = MakeReg(MVT::i32);
1259 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg())
1260 .addConstantPoolIndex(CPI);
1261 BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
1262 // Store the hi & low halves of the fp value, currently in int regs
1263 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
1264 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
1265 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp1), FrameIdx, 4);
1266 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
1267 // Generate the return value with a subtract
1268 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
1269 } else {
1270 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000008p52);
1271 unsigned CPI = CP->getConstantPoolIndex(CFP);
1272 // Load constant fp value
1273 unsigned Tmp4 = MakeReg(MVT::i32);
1274 unsigned TmpL = MakeReg(MVT::i32);
1275 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg())
1276 .addConstantPoolIndex(CPI);
1277 BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
1278 // Store the hi & low halves of the fp value, currently in int regs
1279 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
1280 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
1281 BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
1282 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
1283 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
1284 // Generate the return value with a subtract
1285 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
1286 }
1287 return Result;
1288 }
Nate Begemana9795f82005-03-24 04:41:43 +00001289 }
Nate Begeman6b559972005-04-01 02:59:27 +00001290 assert(0 && "Should never get here");
Nate Begemana9795f82005-03-24 04:41:43 +00001291 return 0;
1292}
1293
1294unsigned ISel::SelectExpr(SDOperand N) {
1295 unsigned Result;
1296 unsigned Tmp1, Tmp2, Tmp3;
1297 unsigned Opc = 0;
1298 unsigned opcode = N.getOpcode();
1299
1300 SDNode *Node = N.Val;
1301 MVT::ValueType DestType = N.getValueType();
1302
1303 unsigned &Reg = ExprMap[N];
1304 if (Reg) return Reg;
1305
Nate Begeman27eeb002005-04-02 05:59:34 +00001306 switch (N.getOpcode()) {
1307 default:
Nate Begemana9795f82005-03-24 04:41:43 +00001308 Reg = Result = (N.getValueType() != MVT::Other) ?
Nate Begeman27eeb002005-04-02 05:59:34 +00001309 MakeReg(N.getValueType()) : 1;
1310 break;
1311 case ISD::CALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001312 // If this is a call instruction, make sure to prepare ALL of the result
1313 // values as well as the chain.
Nate Begeman27eeb002005-04-02 05:59:34 +00001314 if (Node->getNumValues() == 1)
1315 Reg = Result = 1; // Void call, just a chain.
1316 else {
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001317 Result = MakeReg(Node->getValueType(0));
1318 ExprMap[N.getValue(0)] = Result;
Nate Begeman27eeb002005-04-02 05:59:34 +00001319 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001320 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Nate Begeman27eeb002005-04-02 05:59:34 +00001321 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001322 }
Nate Begeman27eeb002005-04-02 05:59:34 +00001323 break;
1324 case ISD::ADD_PARTS:
1325 case ISD::SUB_PARTS:
1326 case ISD::SHL_PARTS:
1327 case ISD::SRL_PARTS:
1328 case ISD::SRA_PARTS:
1329 Result = MakeReg(Node->getValueType(0));
1330 ExprMap[N.getValue(0)] = Result;
1331 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
1332 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1333 break;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001334 }
1335
Nate Begemane5846682005-04-04 06:52:38 +00001336 if (ISD::CopyFromReg == opcode)
1337 DestType = N.getValue(0).getValueType();
1338
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001339 if (DestType == MVT::f64 || DestType == MVT::f32)
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001340 if (ISD::LOAD != opcode && ISD::EXTLOAD != opcode && ISD::UNDEF != opcode)
Nate Begeman74d73452005-03-31 00:15:26 +00001341 return SelectExprFP(N, Result);
Nate Begemana9795f82005-03-24 04:41:43 +00001342
1343 switch (opcode) {
1344 default:
1345 Node->dump();
1346 assert(0 && "Node not handled!\n");
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001347 case ISD::UNDEF:
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001348 BuildMI(BB, PPC::IMPLICIT_DEF, 0, Result);
1349 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001350 case ISD::DYNAMIC_STACKALLOC:
Nate Begeman5e966612005-03-24 06:28:42 +00001351 // Generate both result values. FIXME: Need a better commment here?
1352 if (Result != 1)
1353 ExprMap[N.getValue(1)] = 1;
1354 else
1355 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1356
1357 // FIXME: We are currently ignoring the requested alignment for handling
1358 // greater than the stack alignment. This will need to be revisited at some
1359 // point. Align = N.getOperand(2);
1360 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1361 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1362 std::cerr << "Cannot allocate stack object with greater alignment than"
1363 << " the stack alignment yet!";
1364 abort();
1365 }
1366 Select(N.getOperand(0));
1367 Tmp1 = SelectExpr(N.getOperand(1));
1368 // Subtract size from stack pointer, thereby allocating some space.
1369 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
1370 // Put a pointer to the space into the result register by copying the SP
1371 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
1372 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001373
1374 case ISD::ConstantPool:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001375 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1376 Tmp2 = MakeReg(MVT::i32);
1377 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp2).addReg(getGlobalBaseReg())
1378 .addConstantPoolIndex(Tmp1);
1379 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
1380 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001381
1382 case ISD::FrameIndex:
Nate Begemanf3d08f32005-03-29 00:03:27 +00001383 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Nate Begeman58f718c2005-03-30 02:23:08 +00001384 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
Nate Begemanf3d08f32005-03-29 00:03:27 +00001385 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001386
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001387 case ISD::GlobalAddress: {
1388 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanca12a2b2005-03-28 22:28:37 +00001389 Tmp1 = MakeReg(MVT::i32);
Nate Begemanc7b09f12005-03-25 08:34:25 +00001390 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1391 .addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001392 if (GV->hasWeakLinkage() || GV->isExternal()) {
1393 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
1394 } else {
1395 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
1396 }
1397 return Result;
1398 }
1399
Nate Begeman5e966612005-03-24 06:28:42 +00001400 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +00001401 case ISD::EXTLOAD:
1402 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001403 case ISD::SEXTLOAD: {
Nate Begeman9db505c2005-03-28 19:36:43 +00001404 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
1405 Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType();
Nate Begeman74d73452005-03-31 00:15:26 +00001406 bool sext = (ISD::SEXTLOAD == opcode);
Nate Begeman74d73452005-03-31 00:15:26 +00001407
Nate Begeman5e966612005-03-24 06:28:42 +00001408 // Make sure we generate both values.
1409 if (Result != 1)
1410 ExprMap[N.getValue(1)] = 1; // Generate the token
1411 else
1412 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1413
1414 SDOperand Chain = N.getOperand(0);
1415 SDOperand Address = N.getOperand(1);
1416 Select(Chain);
1417
Nate Begeman9db505c2005-03-28 19:36:43 +00001418 switch (TypeBeingLoaded) {
Nate Begeman74d73452005-03-31 00:15:26 +00001419 default: Node->dump(); assert(0 && "Cannot load this type!");
Nate Begeman9db505c2005-03-28 19:36:43 +00001420 case MVT::i1: Opc = PPC::LBZ; break;
1421 case MVT::i8: Opc = PPC::LBZ; break;
1422 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
1423 case MVT::i32: Opc = PPC::LWZ; break;
Nate Begeman74d73452005-03-31 00:15:26 +00001424 case MVT::f32: Opc = PPC::LFS; break;
1425 case MVT::f64: Opc = PPC::LFD; break;
Nate Begeman5e966612005-03-24 06:28:42 +00001426 }
1427
Nate Begeman74d73452005-03-31 00:15:26 +00001428 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1429 Tmp1 = MakeReg(MVT::i32);
1430 int CPI = CP->getIndex();
1431 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1432 .addConstantPoolIndex(CPI);
1433 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001434 }
Nate Begeman74d73452005-03-31 00:15:26 +00001435 else if(Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman58f718c2005-03-30 02:23:08 +00001436 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
1437 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
Nate Begeman5e966612005-03-24 06:28:42 +00001438 } else {
1439 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00001440 bool idx = SelectAddr(Address, Tmp1, offset);
1441 if (idx) {
1442 Opc = IndexedOpForOp(Opc);
1443 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset);
1444 } else {
1445 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
1446 }
Nate Begeman5e966612005-03-24 06:28:42 +00001447 }
1448 return Result;
1449 }
1450
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001451 case ISD::CALL: {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001452 unsigned GPR_idx = 0, FPR_idx = 0;
1453 static const unsigned GPR[] = {
1454 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1455 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1456 };
1457 static const unsigned FPR[] = {
1458 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1459 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1460 };
1461
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001462 // Lower the chain for this call.
1463 Select(N.getOperand(0));
1464 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
Nate Begeman74d73452005-03-31 00:15:26 +00001465
Nate Begemand860aa62005-04-04 22:17:48 +00001466 MachineInstr *CallMI;
1467 // Emit the correct call instruction based on the type of symbol called.
1468 if (GlobalAddressSDNode *GASD =
1469 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
1470 CallMI = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(),
1471 true);
1472 } else if (ExternalSymbolSDNode *ESSDN =
1473 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
1474 CallMI = BuildMI(PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(),
1475 true);
1476 } else {
1477 Tmp1 = SelectExpr(N.getOperand(1));
1478 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
1479 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
1480 CallMI = BuildMI(PPC::CALLindirect, 3).addImm(20).addImm(0)
1481 .addReg(PPC::R12);
1482 }
1483
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001484 // Load the register args to virtual regs
1485 std::vector<unsigned> ArgVR;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001486 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001487 ArgVR.push_back(SelectExpr(N.getOperand(i)));
1488
1489 // Copy the virtual registers into the appropriate argument register
1490 for(int i = 0, e = ArgVR.size(); i < e; ++i) {
1491 switch(N.getOperand(i+2).getValueType()) {
1492 default: Node->dump(); assert(0 && "Unknown value type for call");
1493 case MVT::i1:
1494 case MVT::i8:
1495 case MVT::i16:
1496 case MVT::i32:
1497 assert(GPR_idx < 8 && "Too many int args");
Nate Begemand860aa62005-04-04 22:17:48 +00001498 if (N.getOperand(i+2).getOpcode() != ISD::UNDEF) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001499 BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001500 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1501 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001502 ++GPR_idx;
1503 break;
1504 case MVT::f64:
1505 case MVT::f32:
1506 assert(FPR_idx < 13 && "Too many fp args");
1507 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001508 CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001509 ++FPR_idx;
1510 break;
1511 }
1512 }
Nate Begemand860aa62005-04-04 22:17:48 +00001513
1514 // Put the call instruction in the correct place in the MachineBasicBlock
1515 BB->push_back(CallMI);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001516
1517 switch (Node->getValueType(0)) {
1518 default: assert(0 && "Unknown value type for call result!");
1519 case MVT::Other: return 1;
1520 case MVT::i1:
1521 case MVT::i8:
1522 case MVT::i16:
1523 case MVT::i32:
Nate Begemane5846682005-04-04 06:52:38 +00001524 if (Node->getValueType(1) == MVT::i32) {
1525 BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R3).addReg(PPC::R3);
1526 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R4).addReg(PPC::R4);
1527 } else {
1528 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
1529 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001530 break;
1531 case MVT::f32:
1532 case MVT::f64:
1533 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1534 break;
1535 }
1536 return Result+N.ResNo;
1537 }
Nate Begemana9795f82005-03-24 04:41:43 +00001538
1539 case ISD::SIGN_EXTEND:
1540 case ISD::SIGN_EXTEND_INREG:
1541 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman9db505c2005-03-28 19:36:43 +00001542 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1543 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
1544 case MVT::i16:
1545 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
1546 break;
1547 case MVT::i8:
1548 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
1549 break;
Nate Begeman74747862005-03-29 22:24:51 +00001550 case MVT::i1:
1551 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1552 break;
Nate Begeman9db505c2005-03-28 19:36:43 +00001553 }
Nate Begemana9795f82005-03-24 04:41:43 +00001554 return Result;
1555
1556 case ISD::ZERO_EXTEND_INREG:
1557 Tmp1 = SelectExpr(N.getOperand(0));
1558 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
Nate Begeman9db505c2005-03-28 19:36:43 +00001559 default: Node->dump(); assert(0 && "Unhandled ZERO_EXTEND type"); break;
Nate Begemana9795f82005-03-24 04:41:43 +00001560 case MVT::i16: Tmp2 = 16; break;
1561 case MVT::i8: Tmp2 = 24; break;
1562 case MVT::i1: Tmp2 = 31; break;
1563 }
Nate Begeman33162522005-03-29 21:54:38 +00001564 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(0).addImm(Tmp2)
1565 .addImm(31);
Nate Begemana9795f82005-03-24 04:41:43 +00001566 return Result;
1567
Nate Begemana9795f82005-03-24 04:41:43 +00001568 case ISD::CopyFromReg:
1569 if (Result == 1)
1570 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1571 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1572 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1573 return Result;
1574
1575 case ISD::SHL:
Nate Begeman5e966612005-03-24 06:28:42 +00001576 Tmp1 = SelectExpr(N.getOperand(0));
1577 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1578 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001579 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
Nate Begeman5e966612005-03-24 06:28:42 +00001580 .addImm(31-Tmp2);
1581 } else {
1582 Tmp2 = SelectExpr(N.getOperand(1));
1583 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1584 }
1585 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001586
Nate Begeman5e966612005-03-24 06:28:42 +00001587 case ISD::SRL:
1588 Tmp1 = SelectExpr(N.getOperand(0));
1589 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1590 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001591 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2)
Nate Begeman5e966612005-03-24 06:28:42 +00001592 .addImm(Tmp2).addImm(31);
1593 } else {
1594 Tmp2 = SelectExpr(N.getOperand(1));
1595 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1596 }
1597 return Result;
1598
1599 case ISD::SRA:
1600 Tmp1 = SelectExpr(N.getOperand(0));
1601 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1602 Tmp2 = CN->getValue() & 0x1F;
1603 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1604 } else {
1605 Tmp2 = SelectExpr(N.getOperand(1));
1606 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1607 }
1608 return Result;
1609
Nate Begemana9795f82005-03-24 04:41:43 +00001610 case ISD::ADD:
1611 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1612 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001613 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemana9795f82005-03-24 04:41:43 +00001614 default: assert(0 && "unhandled result code");
1615 case 0: // No immediate
1616 Tmp2 = SelectExpr(N.getOperand(1));
1617 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1618 break;
1619 case 1: // Low immediate
1620 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1621 break;
1622 case 2: // Shifted immediate
1623 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1624 break;
1625 }
1626 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001627
Nate Begemana9795f82005-03-24 04:41:43 +00001628 case ISD::AND:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001629 Tmp1 = SelectExpr(N.getOperand(0));
1630 // FIXME: should add check in getImmediateForOpcode to return a value
1631 // indicating the immediate is a run of set bits so we can emit a bitfield
1632 // clear with RLWINM instead.
1633 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1634 default: assert(0 && "unhandled result code");
1635 case 0: // No immediate
1636 Tmp2 = SelectExpr(N.getOperand(1));
1637 BuildMI(BB, PPC::AND, 2, Result).addReg(Tmp1).addReg(Tmp2);
1638 break;
1639 case 1: // Low immediate
1640 BuildMI(BB, PPC::ANDIo, 2, Result).addReg(Tmp1).addImm(Tmp2);
1641 break;
1642 case 2: // Shifted immediate
1643 BuildMI(BB, PPC::ANDISo, 2, Result).addReg(Tmp1).addImm(Tmp2);
1644 break;
1645 }
1646 return Result;
1647
Nate Begemana9795f82005-03-24 04:41:43 +00001648 case ISD::OR:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001649 if (SelectBitfieldInsert(N, Result))
1650 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001651 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001652 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemana9795f82005-03-24 04:41:43 +00001653 default: assert(0 && "unhandled result code");
1654 case 0: // No immediate
1655 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001656 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001657 break;
1658 case 1: // Low immediate
Nate Begeman7ddecb42005-04-06 23:51:40 +00001659 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001660 break;
1661 case 2: // Shifted immediate
Nate Begeman7ddecb42005-04-06 23:51:40 +00001662 BuildMI(BB, PPC::ORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001663 break;
1664 }
1665 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001666
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001667 case ISD::XOR: {
1668 // Check for EQV: xor, (xor a, -1), b
1669 if (N.getOperand(0).getOpcode() == ISD::XOR &&
1670 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1671 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) {
1672 ++NotLogic;
1673 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1674 Tmp2 = SelectExpr(N.getOperand(1));
1675 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1676 return Result;
1677 }
1678 // Check for NOT, NOR, and NAND: xor (copy, or, and), -1
1679 if (N.getOperand(1).getOpcode() == ISD::Constant &&
1680 cast<ConstantSDNode>(N.getOperand(1))->isAllOnesValue()) {
1681 ++NotLogic;
1682 switch(N.getOperand(0).getOpcode()) {
1683 case ISD::OR:
1684 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1685 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1686 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1687 break;
1688 case ISD::AND:
1689 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1690 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1691 BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2);
1692 break;
1693 default:
1694 Tmp1 = SelectExpr(N.getOperand(0));
1695 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1696 break;
1697 }
1698 return Result;
1699 }
1700 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001701 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001702 default: assert(0 && "unhandled result code");
1703 case 0: // No immediate
1704 Tmp2 = SelectExpr(N.getOperand(1));
1705 BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1706 break;
1707 case 1: // Low immediate
1708 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1709 break;
1710 case 2: // Shifted immediate
1711 BuildMI(BB, PPC::XORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
1712 break;
1713 }
1714 return Result;
1715 }
1716
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001717 case ISD::SUB:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001718 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman439b4442005-04-05 04:22:58 +00001719 if (1 == getImmediateForOpcode(N.getOperand(0), opcode, Tmp1))
Nate Begeman27523a12005-04-02 00:42:16 +00001720 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1);
1721 else {
1722 Tmp1 = SelectExpr(N.getOperand(0));
1723 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
1724 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001725 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001726
Nate Begeman5e966612005-03-24 06:28:42 +00001727 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001728 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001729 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
Nate Begeman307e7442005-03-26 01:28:53 +00001730 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1731 else {
1732 Tmp2 = SelectExpr(N.getOperand(1));
1733 BuildMI(BB, PPC::MULLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1734 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001735 return Result;
1736
Nate Begeman815d6da2005-04-06 00:25:27 +00001737 case ISD::MULHS:
1738 case ISD::MULHU:
1739 Tmp1 = SelectExpr(N.getOperand(0));
1740 Tmp2 = SelectExpr(N.getOperand(1));
1741 Opc = (ISD::MULHU == opcode) ? PPC::MULHWU : PPC::MULHW;
1742 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1743 return Result;
1744
Nate Begemanf3d08f32005-03-29 00:03:27 +00001745 case ISD::SDIV:
1746 case ISD::UDIV:
Nate Begeman815d6da2005-04-06 00:25:27 +00001747 switch (getImmediateForOpcode(N.getOperand(1), opcode, Tmp3)) {
1748 default: break;
1749 // If this is an sdiv by a power of two, we can use an srawi/addze pair.
1750 case 3:
Nate Begeman80196b12005-04-05 00:15:08 +00001751 Tmp1 = MakeReg(MVT::i32);
1752 Tmp2 = SelectExpr(N.getOperand(0));
1753 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
1754 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp1);
1755 return Result;
Nate Begeman815d6da2005-04-06 00:25:27 +00001756 // If this is a divide by constant, we can emit code using some magic
1757 // constants to implement it as a multiply instead.
Nate Begeman27b4c232005-04-06 06:44:57 +00001758 case 4:
1759 ExprMap.erase(N);
1760 if (opcode == ISD::SDIV)
1761 return SelectExpr(BuildSDIVSequence(N));
1762 else
1763 return SelectExpr(BuildUDIVSequence(N));
Nate Begeman80196b12005-04-05 00:15:08 +00001764 }
Nate Begemanf3d08f32005-03-29 00:03:27 +00001765 Tmp1 = SelectExpr(N.getOperand(0));
1766 Tmp2 = SelectExpr(N.getOperand(1));
1767 Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW;
1768 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1769 return Result;
1770
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001771 case ISD::ADD_PARTS:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001772 case ISD::SUB_PARTS: {
1773 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1774 "Not an i64 add/sub!");
1775 // Emit all of the operands.
1776 std::vector<unsigned> InVals;
1777 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
1778 InVals.push_back(SelectExpr(N.getOperand(i)));
1779 if (N.getOpcode() == ISD::ADD_PARTS) {
Nate Begeman27eeb002005-04-02 05:59:34 +00001780 BuildMI(BB, PPC::ADDC, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
1781 BuildMI(BB, PPC::ADDE, 2, Result+1).addReg(InVals[1]).addReg(InVals[3]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001782 } else {
Nate Begeman27eeb002005-04-02 05:59:34 +00001783 BuildMI(BB, PPC::SUBFC, 2, Result).addReg(InVals[2]).addReg(InVals[0]);
1784 BuildMI(BB, PPC::SUBFE, 2, Result+1).addReg(InVals[3]).addReg(InVals[1]);
1785 }
1786 return Result+N.ResNo;
1787 }
1788
1789 case ISD::SHL_PARTS:
1790 case ISD::SRA_PARTS:
1791 case ISD::SRL_PARTS: {
1792 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
1793 "Not an i64 shift!");
1794 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
1795 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
1796 unsigned SHReg = SelectExpr(N.getOperand(2));
1797 Tmp1 = MakeReg(MVT::i32);
1798 Tmp2 = MakeReg(MVT::i32);
1799 Tmp3 = MakeReg(MVT::i32);
1800 unsigned Tmp4 = MakeReg(MVT::i32);
1801 unsigned Tmp5 = MakeReg(MVT::i32);
1802 unsigned Tmp6 = MakeReg(MVT::i32);
1803 BuildMI(BB, PPC::SUBFIC, 2, Tmp1).addReg(SHReg).addSImm(32);
1804 if (ISD::SHL_PARTS == opcode) {
1805 BuildMI(BB, PPC::SLW, 2, Tmp2).addReg(ShiftOpHi).addReg(SHReg);
1806 BuildMI(BB, PPC::SRW, 2, Tmp3).addReg(ShiftOpLo).addReg(Tmp1);
1807 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1808 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
Nate Begemanfa554702005-04-03 22:13:27 +00001809 BuildMI(BB, PPC::SLW, 2, Tmp6).addReg(ShiftOpLo).addReg(Tmp5);
Nate Begeman27eeb002005-04-02 05:59:34 +00001810 BuildMI(BB, PPC::OR, 2, Result+1).addReg(Tmp4).addReg(Tmp6);
1811 BuildMI(BB, PPC::SLW, 2, Result).addReg(ShiftOpLo).addReg(SHReg);
1812 } else if (ISD::SRL_PARTS == opcode) {
1813 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1814 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1815 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1816 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
1817 BuildMI(BB, PPC::SRW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1818 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp4).addReg(Tmp6);
1819 BuildMI(BB, PPC::SRW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1820 } else {
1821 MachineBasicBlock *TmpMBB = new MachineBasicBlock(BB->getBasicBlock());
1822 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1823 MachineBasicBlock *OldMBB = BB;
1824 MachineFunction *F = BB->getParent();
1825 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1826 F->getBasicBlockList().insert(It, TmpMBB);
1827 F->getBasicBlockList().insert(It, PhiMBB);
1828 BB->addSuccessor(TmpMBB);
1829 BB->addSuccessor(PhiMBB);
1830 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1831 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1832 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1833 BuildMI(BB, PPC::ADDICo, 2, Tmp5).addReg(SHReg).addSImm(-32);
1834 BuildMI(BB, PPC::SRAW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1835 BuildMI(BB, PPC::SRAW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1836 BuildMI(BB, PPC::BLE, 2).addReg(PPC::CR0).addMBB(PhiMBB);
1837 // Select correct least significant half if the shift amount > 32
1838 BB = TmpMBB;
1839 unsigned Tmp7 = MakeReg(MVT::i32);
1840 BuildMI(BB, PPC::OR, 2, Tmp7).addReg(Tmp6).addReg(Tmp6);
1841 TmpMBB->addSuccessor(PhiMBB);
1842 BB = PhiMBB;
1843 BuildMI(BB, PPC::PHI, 4, Result).addReg(Tmp4).addMBB(OldMBB)
1844 .addReg(Tmp7).addMBB(TmpMBB);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001845 }
1846 return Result+N.ResNo;
1847 }
1848
Nate Begemana9795f82005-03-24 04:41:43 +00001849 case ISD::FP_TO_UINT:
Nate Begeman6b559972005-04-01 02:59:27 +00001850 case ISD::FP_TO_SINT: {
1851 bool U = (ISD::FP_TO_UINT == opcode);
1852 Tmp1 = SelectExpr(N.getOperand(0));
1853 if (!U) {
1854 Tmp2 = MakeReg(MVT::f64);
1855 BuildMI(BB, PPC::FCTIWZ, 1, Tmp2).addReg(Tmp1);
1856 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1857 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx);
1858 addFrameReference(BuildMI(BB, PPC::LWZ, 2, Result), FrameIdx, 4);
1859 return Result;
1860 } else {
1861 unsigned Zero = getConstDouble(0.0);
1862 unsigned MaxInt = getConstDouble((1LL << 32) - 1);
1863 unsigned Border = getConstDouble(1LL << 31);
1864 unsigned UseZero = MakeReg(MVT::f64);
1865 unsigned UseMaxInt = MakeReg(MVT::f64);
1866 unsigned UseChoice = MakeReg(MVT::f64);
1867 unsigned TmpReg = MakeReg(MVT::f64);
1868 unsigned TmpReg2 = MakeReg(MVT::f64);
1869 unsigned ConvReg = MakeReg(MVT::f64);
1870 unsigned IntTmp = MakeReg(MVT::i32);
1871 unsigned XorReg = MakeReg(MVT::i32);
1872 MachineFunction *F = BB->getParent();
1873 int FrameIdx = F->getFrameInfo()->CreateStackObject(8, 8);
1874 // Update machine-CFG edges
1875 MachineBasicBlock *XorMBB = new MachineBasicBlock(BB->getBasicBlock());
1876 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1877 MachineBasicBlock *OldMBB = BB;
1878 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1879 F->getBasicBlockList().insert(It, XorMBB);
1880 F->getBasicBlockList().insert(It, PhiMBB);
1881 BB->addSuccessor(XorMBB);
1882 BB->addSuccessor(PhiMBB);
1883 // Convert from floating point to unsigned 32-bit value
1884 // Use 0 if incoming value is < 0.0
1885 BuildMI(BB, PPC::FSEL, 3, UseZero).addReg(Tmp1).addReg(Tmp1).addReg(Zero);
1886 // Use 2**32 - 1 if incoming value is >= 2**32
1887 BuildMI(BB, PPC::FSUB, 2, UseMaxInt).addReg(MaxInt).addReg(Tmp1);
1888 BuildMI(BB, PPC::FSEL, 3, UseChoice).addReg(UseMaxInt).addReg(UseZero)
1889 .addReg(MaxInt);
1890 // Subtract 2**31
1891 BuildMI(BB, PPC::FSUB, 2, TmpReg).addReg(UseChoice).addReg(Border);
1892 // Use difference if >= 2**31
1893 BuildMI(BB, PPC::FCMPU, 2, PPC::CR0).addReg(UseChoice).addReg(Border);
1894 BuildMI(BB, PPC::FSEL, 3, TmpReg2).addReg(TmpReg).addReg(TmpReg)
1895 .addReg(UseChoice);
1896 // Convert to integer
1897 BuildMI(BB, PPC::FCTIWZ, 1, ConvReg).addReg(TmpReg2);
1898 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(ConvReg), FrameIdx);
1899 addFrameReference(BuildMI(BB, PPC::LWZ, 2, IntTmp), FrameIdx, 4);
1900 BuildMI(BB, PPC::BLT, 2).addReg(PPC::CR0).addMBB(PhiMBB);
1901 BuildMI(BB, PPC::B, 1).addMBB(XorMBB);
1902
1903 // XorMBB:
1904 // add 2**31 if input was >= 2**31
1905 BB = XorMBB;
1906 BuildMI(BB, PPC::XORIS, 2, XorReg).addReg(IntTmp).addImm(0x8000);
1907 XorMBB->addSuccessor(PhiMBB);
1908
1909 // PhiMBB:
1910 // DestReg = phi [ IntTmp, OldMBB ], [ XorReg, XorMBB ]
1911 BB = PhiMBB;
1912 BuildMI(BB, PPC::PHI, 4, Result).addReg(IntTmp).addMBB(OldMBB)
1913 .addReg(XorReg).addMBB(XorMBB);
1914 return Result;
1915 }
1916 assert(0 && "Should never get here");
1917 return 0;
1918 }
Nate Begemana9795f82005-03-24 04:41:43 +00001919
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001920 case ISD::SETCC:
Nate Begeman33162522005-03-29 21:54:38 +00001921 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
Nate Begeman7e7fadd2005-04-07 20:30:01 +00001922 // We can codegen setcc op, 0 very efficiently compared to a conditional
1923 // branch. Check for that here.
1924 if (ConstantSDNode *CN =
1925 dyn_cast<ConstantSDNode>(SetCC->getOperand(1).Val)) {
1926 if (CN->getValue() == 0) {
1927 Tmp1 = SelectExpr(SetCC->getOperand(0));
1928 switch (SetCC->getCondition()) {
1929 default: assert(0 && "Unhandled SetCC condition"); abort();
1930 case ISD::SETEQ:
1931 case ISD::SETULE:
1932 Tmp2 = MakeReg(MVT::i32);
1933 BuildMI(BB, PPC::CNTLZW, 1, Tmp2).addReg(Tmp1);
1934 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp2).addImm(27)
1935 .addImm(5).addImm(31);
1936 break;
1937 case ISD::SETNE:
1938 case ISD::SETUGT:
1939 Tmp2 = MakeReg(MVT::i32);
1940 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(-1);
1941 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp2).addReg(Tmp1);
1942 break;
1943 case ISD::SETULT:
1944 BuildMI(BB, PPC::LI, 1, Result).addSImm(0);
1945 break;
1946 case ISD::SETLT:
1947 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(1)
1948 .addImm(31).addImm(31);
1949 break;
1950 case ISD::SETLE:
1951 Tmp2 = MakeReg(MVT::i32);
1952 Tmp3 = MakeReg(MVT::i32);
1953 BuildMI(BB, PPC::NEG, 2, Tmp2).addReg(Tmp1);
1954 BuildMI(BB, PPC::ORC, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1955 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
1956 .addImm(31).addImm(31);
1957 break;
1958 case ISD::SETGT:
1959 Tmp2 = MakeReg(MVT::i32);
1960 Tmp3 = MakeReg(MVT::i32);
1961 BuildMI(BB, PPC::NEG, 2, Tmp2).addReg(Tmp1);
1962 BuildMI(BB, PPC::ANDC, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1963 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
1964 .addImm(31).addImm(31);
1965 break;
1966 case ISD::SETUGE:
1967 BuildMI(BB, PPC::LI, 1, Result).addSImm(1);
1968 break;
1969 case ISD::SETGE:
1970 BuildMI(BB, PPC::RLWINM, 4, Tmp2).addReg(Tmp1).addImm(1)
1971 .addImm(31).addImm(31);
1972 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp2).addImm(1);
1973 break;
1974 }
1975 return Result;
1976 }
1977 }
1978
Nate Begemandffcfcc2005-04-01 00:32:34 +00001979 Opc = SelectSetCR0(N);
Nate Begeman31318e42005-04-01 07:21:30 +00001980 unsigned TrueValue = MakeReg(MVT::i32);
1981 BuildMI(BB, PPC::LI, 1, TrueValue).addSImm(1);
1982 unsigned FalseValue = MakeReg(MVT::i32);
1983 BuildMI(BB, PPC::LI, 1, FalseValue).addSImm(0);
1984
Nate Begeman33162522005-03-29 21:54:38 +00001985 // Create an iterator with which to insert the MBB for copying the false
1986 // value and the MBB to hold the PHI instruction for this SetCC.
1987 MachineBasicBlock *thisMBB = BB;
1988 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1989 ilist<MachineBasicBlock>::iterator It = BB;
1990 ++It;
1991
1992 // thisMBB:
1993 // ...
1994 // cmpTY cr0, r1, r2
1995 // %TrueValue = li 1
1996 // bCC sinkMBB
Nate Begeman33162522005-03-29 21:54:38 +00001997 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1998 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1999 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
2000 MachineFunction *F = BB->getParent();
2001 F->getBasicBlockList().insert(It, copy0MBB);
2002 F->getBasicBlockList().insert(It, sinkMBB);
2003 // Update machine-CFG edges
2004 BB->addSuccessor(copy0MBB);
2005 BB->addSuccessor(sinkMBB);
2006
2007 // copy0MBB:
2008 // %FalseValue = li 0
2009 // fallthrough
2010 BB = copy0MBB;
Nate Begeman33162522005-03-29 21:54:38 +00002011 // Update machine-CFG edges
2012 BB->addSuccessor(sinkMBB);
2013
2014 // sinkMBB:
2015 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2016 // ...
2017 BB = sinkMBB;
2018 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
2019 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
2020 return Result;
2021 }
2022 assert(0 && "Is this legal?");
2023 return 0;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002024
Nate Begeman74747862005-03-29 22:24:51 +00002025 case ISD::SELECT: {
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002026 // We can codegen select (a < 0) ? b : 0 very efficiently compared to a
2027 // conditional branch. Check for that here.
2028 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val)) {
2029 if (ConstantSDNode *CN =
2030 dyn_cast<ConstantSDNode>(SetCC->getOperand(1).Val)) {
2031 if (ConstantSDNode *CNF =
2032 dyn_cast<ConstantSDNode>(N.getOperand(2).Val)) {
2033 if (CN->getValue() == 0 && CNF->getValue() == 0 &&
2034 SetCC->getCondition() == ISD::SETLT) {
2035 Tmp1 = SelectExpr(N.getOperand(1)); // TRUE value
2036 Tmp2 = SelectExpr(SetCC->getOperand(0));
2037 Tmp3 = MakeReg(MVT::i32);
2038 BuildMI(BB, PPC::SRAWI, 2, Tmp3).addReg(Tmp2).addImm(31);
2039 BuildMI(BB, PPC::AND, 2, Result).addReg(Tmp1).addReg(Tmp3);
2040 return Result;
2041 }
2042 }
2043 }
2044 }
Chris Lattner30710192005-04-01 07:10:02 +00002045 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
2046 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman6cb2e1b2005-04-01 08:57:43 +00002047 Opc = SelectSetCR0(N.getOperand(0));
Chris Lattner30710192005-04-01 07:10:02 +00002048
Nate Begeman74747862005-03-29 22:24:51 +00002049 // Create an iterator with which to insert the MBB for copying the false
2050 // value and the MBB to hold the PHI instruction for this SetCC.
2051 MachineBasicBlock *thisMBB = BB;
2052 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2053 ilist<MachineBasicBlock>::iterator It = BB;
2054 ++It;
2055
2056 // thisMBB:
2057 // ...
2058 // TrueVal = ...
2059 // cmpTY cr0, r1, r2
2060 // bCC copy1MBB
2061 // fallthrough --> copy0MBB
Nate Begeman74747862005-03-29 22:24:51 +00002062 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
2063 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman3e897162005-03-31 23:55:40 +00002064 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
Nate Begeman74747862005-03-29 22:24:51 +00002065 MachineFunction *F = BB->getParent();
2066 F->getBasicBlockList().insert(It, copy0MBB);
2067 F->getBasicBlockList().insert(It, sinkMBB);
2068 // Update machine-CFG edges
2069 BB->addSuccessor(copy0MBB);
2070 BB->addSuccessor(sinkMBB);
2071
2072 // copy0MBB:
2073 // %FalseValue = ...
2074 // # fallthrough to sinkMBB
2075 BB = copy0MBB;
Nate Begeman74747862005-03-29 22:24:51 +00002076 // Update machine-CFG edges
2077 BB->addSuccessor(sinkMBB);
2078
2079 // sinkMBB:
2080 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2081 // ...
2082 BB = sinkMBB;
2083 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
2084 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
Nate Begeman74747862005-03-29 22:24:51 +00002085 return Result;
2086 }
Nate Begemana9795f82005-03-24 04:41:43 +00002087
2088 case ISD::Constant:
2089 switch (N.getValueType()) {
2090 default: assert(0 && "Cannot use constants of this type!");
2091 case MVT::i1:
2092 BuildMI(BB, PPC::LI, 1, Result)
2093 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
2094 break;
2095 case MVT::i32:
2096 {
2097 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
2098 if (v < 32768 && v >= -32768) {
2099 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
2100 } else {
Nate Begeman5e966612005-03-24 06:28:42 +00002101 Tmp1 = MakeReg(MVT::i32);
2102 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
2103 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
Nate Begemana9795f82005-03-24 04:41:43 +00002104 }
2105 }
2106 }
2107 return Result;
2108 }
2109
2110 return 0;
2111}
2112
2113void ISel::Select(SDOperand N) {
2114 unsigned Tmp1, Tmp2, Opc;
2115 unsigned opcode = N.getOpcode();
2116
2117 if (!ExprMap.insert(std::make_pair(N, 1)).second)
2118 return; // Already selected.
2119
2120 SDNode *Node = N.Val;
2121
2122 switch (Node->getOpcode()) {
2123 default:
2124 Node->dump(); std::cerr << "\n";
2125 assert(0 && "Node not handled yet!");
2126 case ISD::EntryToken: return; // Noop
2127 case ISD::TokenFactor:
2128 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2129 Select(Node->getOperand(i));
2130 return;
2131 case ISD::ADJCALLSTACKDOWN:
2132 case ISD::ADJCALLSTACKUP:
2133 Select(N.getOperand(0));
2134 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
2135 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? PPC::ADJCALLSTACKDOWN :
2136 PPC::ADJCALLSTACKUP;
2137 BuildMI(BB, Opc, 1).addImm(Tmp1);
2138 return;
2139 case ISD::BR: {
2140 MachineBasicBlock *Dest =
2141 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
Nate Begemana9795f82005-03-24 04:41:43 +00002142 Select(N.getOperand(0));
2143 BuildMI(BB, PPC::B, 1).addMBB(Dest);
2144 return;
2145 }
2146 case ISD::BRCOND:
2147 SelectBranchCC(N);
2148 return;
2149 case ISD::CopyToReg:
2150 Select(N.getOperand(0));
2151 Tmp1 = SelectExpr(N.getOperand(1));
2152 Tmp2 = cast<RegSDNode>(N)->getReg();
2153
2154 if (Tmp1 != Tmp2) {
2155 if (N.getOperand(1).getValueType() == MVT::f64 ||
2156 N.getOperand(1).getValueType() == MVT::f32)
2157 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
2158 else
2159 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2160 }
2161 return;
2162 case ISD::ImplicitDef:
2163 Select(N.getOperand(0));
2164 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
2165 return;
2166 case ISD::RET:
2167 switch (N.getNumOperands()) {
2168 default:
2169 assert(0 && "Unknown return instruction!");
2170 case 3:
2171 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2172 N.getOperand(2).getValueType() == MVT::i32 &&
2173 "Unknown two-register value!");
2174 Select(N.getOperand(0));
2175 Tmp1 = SelectExpr(N.getOperand(1));
2176 Tmp2 = SelectExpr(N.getOperand(2));
Nate Begeman27523a12005-04-02 00:42:16 +00002177 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2);
2178 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00002179 break;
2180 case 2:
2181 Select(N.getOperand(0));
2182 Tmp1 = SelectExpr(N.getOperand(1));
2183 switch (N.getOperand(1).getValueType()) {
2184 default:
2185 assert(0 && "Unknown return type!");
2186 case MVT::f64:
2187 case MVT::f32:
2188 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
2189 break;
2190 case MVT::i32:
2191 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
2192 break;
2193 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002194 case 1:
2195 Select(N.getOperand(0));
2196 break;
Nate Begemana9795f82005-03-24 04:41:43 +00002197 }
2198 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
2199 return;
Nate Begemana9795f82005-03-24 04:41:43 +00002200 case ISD::TRUNCSTORE:
2201 case ISD::STORE:
2202 {
2203 SDOperand Chain = N.getOperand(0);
2204 SDOperand Value = N.getOperand(1);
2205 SDOperand Address = N.getOperand(2);
2206 Select(Chain);
2207
2208 Tmp1 = SelectExpr(Value); //value
2209
2210 if (opcode == ISD::STORE) {
2211 switch(Value.getValueType()) {
2212 default: assert(0 && "unknown Type in store");
2213 case MVT::i32: Opc = PPC::STW; break;
2214 case MVT::f64: Opc = PPC::STFD; break;
2215 case MVT::f32: Opc = PPC::STFS; break;
2216 }
2217 } else { //ISD::TRUNCSTORE
2218 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
2219 default: assert(0 && "unknown Type in store");
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002220 case MVT::i1:
Nate Begemana9795f82005-03-24 04:41:43 +00002221 case MVT::i8: Opc = PPC::STB; break;
2222 case MVT::i16: Opc = PPC::STH; break;
2223 }
2224 }
2225
Nate Begemana7e11a42005-04-01 05:57:17 +00002226 if(Address.getOpcode() == ISD::FrameIndex)
Nate Begemana9795f82005-03-24 04:41:43 +00002227 {
Nate Begeman58f718c2005-03-30 02:23:08 +00002228 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
2229 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00002230 }
2231 else
2232 {
2233 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00002234 bool idx = SelectAddr(Address, Tmp2, offset);
2235 if (idx) {
2236 Opc = IndexedOpForOp(Opc);
2237 BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset);
2238 } else {
2239 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2240 }
Nate Begemana9795f82005-03-24 04:41:43 +00002241 }
2242 return;
2243 }
2244 case ISD::EXTLOAD:
2245 case ISD::SEXTLOAD:
2246 case ISD::ZEXTLOAD:
2247 case ISD::LOAD:
2248 case ISD::CopyFromReg:
2249 case ISD::CALL:
2250 case ISD::DYNAMIC_STACKALLOC:
2251 ExprMap.erase(N);
2252 SelectExpr(N);
2253 return;
2254 }
2255 assert(0 && "Should not be reached!");
2256}
2257
2258
2259/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
2260/// into a machine code representation using pattern matching and a machine
2261/// description file.
2262///
2263FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) {
2264 return new ISel(TM);
Chris Lattner246fa632005-03-24 06:16:18 +00002265}
2266