blob: 3b599c580ef04035395a61b345948f7be56e98f7 [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.
11//
12//===----------------------------------------------------------------------===//
13
14#include "PowerPC.h"
15#include "PowerPCInstrBuilder.h"
16#include "PowerPCInstrInfo.h"
17#include "PPC32RegisterInfo.h"
18#include "llvm/Constants.h" // FIXME: REMOVE
19#include "llvm/Function.h"
20#include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
21#include "llvm/CodeGen/MachineFunction.h"
22#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/SelectionDAG.h"
24#include "llvm/CodeGen/SelectionDAGISel.h"
25#include "llvm/CodeGen/SSARegMap.h"
26#include "llvm/Target/TargetData.h"
27#include "llvm/Target/TargetLowering.h"
Nate Begeman93075ec2005-04-04 23:40:36 +000028#include "llvm/Target/TargetOptions.h"
Nate Begemana9795f82005-03-24 04:41:43 +000029#include "llvm/Support/Debug.h"
30#include "llvm/Support/MathExtras.h"
31#include "llvm/ADT/Statistic.h"
32#include <set>
33#include <algorithm>
34using namespace llvm;
35
36//===----------------------------------------------------------------------===//
37// PPC32TargetLowering - PPC32 Implementation of the TargetLowering interface
38namespace {
39 class PPC32TargetLowering : public TargetLowering {
40 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
41 int ReturnAddrIndex; // FrameIndex for return slot.
42 public:
43 PPC32TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
Nate Begemana9795f82005-03-24 04:41:43 +000044 // Set up the register classes.
45 addRegisterClass(MVT::i32, PPC32::GPRCRegisterClass);
Nate Begeman7532e2f2005-03-26 08:25:22 +000046 addRegisterClass(MVT::f32, PPC32::FPRCRegisterClass);
Nate Begemana9795f82005-03-24 04:41:43 +000047 addRegisterClass(MVT::f64, PPC32::FPRCRegisterClass);
48
Nate Begeman74d73452005-03-31 00:15:26 +000049 // PowerPC has no intrinsics for these particular operations
Nate Begeman01d05262005-03-30 01:45:43 +000050 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
51 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
52 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
53
Nate Begeman74d73452005-03-31 00:15:26 +000054 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
55 setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
56 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
Chris Lattner43fdea02005-04-02 05:03:24 +000057
Nate Begeman27eeb002005-04-02 05:59:34 +000058 setShiftAmountFlavor(Extend); // shl X, 32 == 0
Nate Begeman3e897162005-03-31 23:55:40 +000059 addLegalFPImmediate(+0.0); // Necessary for FSEL
60 addLegalFPImmediate(-0.0); //
61
Nate Begemana9795f82005-03-24 04:41:43 +000062 computeRegisterProperties();
63 }
64
65 /// LowerArguments - This hook must be implemented to indicate how we should
66 /// lower the arguments for the specified function, into the specified DAG.
67 virtual std::vector<SDOperand>
68 LowerArguments(Function &F, SelectionDAG &DAG);
69
70 /// LowerCallTo - This hook lowers an abstract call to a function into an
71 /// actual call.
72 virtual std::pair<SDOperand, SDOperand>
Nate Begeman307e7442005-03-26 01:28:53 +000073 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
74 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Nate Begemana9795f82005-03-24 04:41:43 +000075
76 virtual std::pair<SDOperand, SDOperand>
77 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
78
79 virtual std::pair<SDOperand,SDOperand>
80 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
81 const Type *ArgTy, SelectionDAG &DAG);
82
83 virtual std::pair<SDOperand, SDOperand>
84 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
85 SelectionDAG &DAG);
86 };
87}
88
89
90std::vector<SDOperand>
91PPC32TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
92 //
93 // add beautiful description of PPC stack frame format, or at least some docs
94 //
95 MachineFunction &MF = DAG.getMachineFunction();
96 MachineFrameInfo *MFI = MF.getFrameInfo();
97 MachineBasicBlock& BB = MF.front();
98 std::vector<SDOperand> ArgValues;
99
100 // Due to the rather complicated nature of the PowerPC ABI, rather than a
101 // fixed size array of physical args, for the sake of simplicity let the STL
102 // handle tracking them for us.
103 std::vector<unsigned> argVR, argPR, argOp;
104 unsigned ArgOffset = 24;
105 unsigned GPR_remaining = 8;
106 unsigned FPR_remaining = 13;
107 unsigned GPR_idx = 0, FPR_idx = 0;
108 static const unsigned GPR[] = {
109 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
110 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
111 };
112 static const unsigned FPR[] = {
113 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
114 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
115 };
116
117 // Add DAG nodes to load the arguments... On entry to a function on PPC,
118 // the arguments start at offset 24, although they are likely to be passed
119 // in registers.
120 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
121 SDOperand newroot, argt;
122 unsigned ObjSize;
123 bool needsLoad = false;
124 MVT::ValueType ObjectVT = getValueType(I->getType());
125
126 switch (ObjectVT) {
127 default: assert(0 && "Unhandled argument type!");
128 case MVT::i1:
129 case MVT::i8:
130 case MVT::i16:
131 case MVT::i32:
132 ObjSize = 4;
133 if (GPR_remaining > 0) {
134 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
Nate Begemanf70b5762005-03-28 23:08:54 +0000135 argt = newroot = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32,
136 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000137 if (ObjectVT != MVT::i32)
138 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, newroot);
Nate Begemana9795f82005-03-24 04:41:43 +0000139 } else {
140 needsLoad = true;
141 }
142 break;
Nate Begemanf7e43382005-03-26 07:46:36 +0000143 case MVT::i64: ObjSize = 8;
144 // FIXME: can split 64b load between reg/mem if it is last arg in regs
Nate Begemana9795f82005-03-24 04:41:43 +0000145 if (GPR_remaining > 1) {
146 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
147 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx+1]);
Nate Begemanca12a2b2005-03-28 22:28:37 +0000148 // Copy the extracted halves into the virtual registers
Nate Begemanf70b5762005-03-28 23:08:54 +0000149 SDOperand argHi = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32,
150 DAG.getRoot());
151 SDOperand argLo = DAG.getCopyFromReg(GPR[GPR_idx+1], MVT::i32, argHi);
Nate Begemanca12a2b2005-03-28 22:28:37 +0000152 // Build the outgoing arg thingy
Nate Begemanf70b5762005-03-28 23:08:54 +0000153 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
154 newroot = argLo;
Nate Begemana9795f82005-03-24 04:41:43 +0000155 } else {
156 needsLoad = true;
157 }
158 break;
159 case MVT::f32: ObjSize = 4;
160 case MVT::f64: ObjSize = 8;
161 if (FPR_remaining > 0) {
162 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, FPR[FPR_idx]);
Nate Begemanf70b5762005-03-28 23:08:54 +0000163 argt = newroot = DAG.getCopyFromReg(FPR[FPR_idx], ObjectVT,
164 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000165 --FPR_remaining;
166 ++FPR_idx;
167 } else {
168 needsLoad = true;
169 }
170 break;
171 }
172
173 // We need to load the argument to a virtual register if we determined above
174 // that we ran out of physical registers of the appropriate type
175 if (needsLoad) {
Nate Begemane5846682005-04-04 06:52:38 +0000176 unsigned SubregOffset = 0;
Nate Begemanc3e2db42005-04-04 09:09:00 +0000177 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
Nate Begemane5846682005-04-04 06:52:38 +0000178 if (ObjectVT == MVT::i16) SubregOffset = 2;
Nate Begemana9795f82005-03-24 04:41:43 +0000179 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
180 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
Nate Begemane5846682005-04-04 06:52:38 +0000181 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
182 DAG.getConstant(SubregOffset, MVT::i32));
Nate Begemana9795f82005-03-24 04:41:43 +0000183 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN);
184 }
185
186 // Every 4 bytes of argument space consumes one of the GPRs available for
187 // argument passing.
188 if (GPR_remaining > 0) {
189 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
190 GPR_remaining -= delta;
191 GPR_idx += delta;
192 }
193 ArgOffset += ObjSize;
194
195 DAG.setRoot(newroot.getValue(1));
196 ArgValues.push_back(argt);
197 }
198
Nate Begemana9795f82005-03-24 04:41:43 +0000199 // If the function takes variable number of arguments, make a frame index for
200 // the start of the first vararg value... for expansion of llvm.va_start.
Nate Begemanfa554702005-04-03 22:13:27 +0000201 if (F.isVarArg()) {
Nate Begemana9795f82005-03-24 04:41:43 +0000202 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
Nate Begemanfa554702005-04-03 22:13:27 +0000203 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
Nate Begeman6644d4c2005-04-03 23:11:17 +0000204 // If this function is vararg, store any remaining integer argument regs
205 // to their spots on the stack so that they may be loaded by deferencing the
206 // result of va_next.
207 std::vector<SDOperand> MemOps;
208 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
209 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
210 SDOperand Val = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32, DAG.getRoot());
211 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
212 Val, FIN);
213 MemOps.push_back(Store);
214 // Increment the address by four for the next argument to store
215 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
216 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
217 }
218 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
Nate Begemanfa554702005-04-03 22:13:27 +0000219 }
Nate Begemana9795f82005-03-24 04:41:43 +0000220
221 return ArgValues;
222}
223
224std::pair<SDOperand, SDOperand>
225PPC32TargetLowering::LowerCallTo(SDOperand Chain,
Nate Begeman307e7442005-03-26 01:28:53 +0000226 const Type *RetTy, bool isVarArg,
227 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG) {
228 // args_to_use will accumulate outgoing args for the ISD::CALL case in
229 // SelectExpr to use to put the arguments in the appropriate registers.
Nate Begemana9795f82005-03-24 04:41:43 +0000230 std::vector<SDOperand> args_to_use;
Nate Begeman307e7442005-03-26 01:28:53 +0000231
232 // Count how many bytes are to be pushed on the stack, including the linkage
233 // area, and parameter passing area.
234 unsigned NumBytes = 24;
235
236 if (Args.empty()) {
Nate Begemana7e11a42005-04-01 05:57:17 +0000237 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
238 DAG.getConstant(NumBytes, getPointerTy()));
Nate Begeman307e7442005-03-26 01:28:53 +0000239 } else {
240 for (unsigned i = 0, e = Args.size(); i != e; ++i)
241 switch (getValueType(Args[i].second)) {
242 default: assert(0 && "Unknown value type!");
243 case MVT::i1:
244 case MVT::i8:
245 case MVT::i16:
246 case MVT::i32:
247 case MVT::f32:
248 NumBytes += 4;
249 break;
250 case MVT::i64:
251 case MVT::f64:
252 NumBytes += 8;
253 break;
254 }
255
256 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
257 // plus 32 bytes of argument space in case any called code gets funky on us.
258 if (NumBytes < 56) NumBytes = 56;
259
260 // Adjust the stack pointer for the new arguments...
261 // These operations are automatically eliminated by the prolog/epilog pass
262 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
263 DAG.getConstant(NumBytes, getPointerTy()));
264
265 // Set up a copy of the stack pointer for use loading and storing any
266 // arguments that may not fit in the registers available for argument
267 // passing.
268 SDOperand StackPtr = DAG.getCopyFromReg(PPC::R1, MVT::i32,
269 DAG.getEntryNode());
270
271 // Figure out which arguments are going to go in registers, and which in
272 // memory. Also, if this is a vararg function, floating point operations
273 // must be stored to our stack, and loaded into integer regs as well, if
274 // any integer regs are available for argument passing.
275 unsigned ArgOffset = 24;
276 unsigned GPR_remaining = 8;
277 unsigned FPR_remaining = 13;
Nate Begeman74d73452005-03-31 00:15:26 +0000278
279 std::vector<SDOperand> MemOps;
Nate Begeman307e7442005-03-26 01:28:53 +0000280 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
281 // PtrOff will be used to store the current argument to the stack if a
282 // register cannot be found for it.
283 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
284 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Nate Begemanf7e43382005-03-26 07:46:36 +0000285 MVT::ValueType ArgVT = getValueType(Args[i].second);
Nate Begeman307e7442005-03-26 01:28:53 +0000286
Nate Begemanf7e43382005-03-26 07:46:36 +0000287 switch (ArgVT) {
Nate Begeman307e7442005-03-26 01:28:53 +0000288 default: assert(0 && "Unexpected ValueType for argument!");
289 case MVT::i1:
290 case MVT::i8:
291 case MVT::i16:
292 // Promote the integer to 32 bits. If the input type is signed use a
293 // sign extend, otherwise use a zero extend.
294 if (Args[i].second->isSigned())
295 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
296 else
297 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
298 // FALL THROUGH
299 case MVT::i32:
300 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000301 args_to_use.push_back(Args[i].first);
Nate Begeman307e7442005-03-26 01:28:53 +0000302 --GPR_remaining;
303 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000304 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
305 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000306 }
307 ArgOffset += 4;
308 break;
309 case MVT::i64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000310 // If we have one free GPR left, we can place the upper half of the i64
311 // in it, and store the other half to the stack. If we have two or more
312 // free GPRs, then we can pass both halves of the i64 in registers.
313 if (GPR_remaining > 0) {
Nate Begemanf2622612005-03-26 02:17:46 +0000314 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
315 Args[i].first, DAG.getConstant(1, MVT::i32));
316 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
317 Args[i].first, DAG.getConstant(0, MVT::i32));
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000318 args_to_use.push_back(Hi);
Nate Begeman74d73452005-03-31 00:15:26 +0000319 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000320 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000321 args_to_use.push_back(Lo);
Nate Begeman74d73452005-03-31 00:15:26 +0000322 --GPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000323 } else {
324 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
325 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Nate Begeman74d73452005-03-31 00:15:26 +0000326 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
327 Lo, PtrOff));
Nate Begemanf7e43382005-03-26 07:46:36 +0000328 }
Nate Begeman307e7442005-03-26 01:28:53 +0000329 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000330 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
331 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000332 }
333 ArgOffset += 8;
334 break;
335 case MVT::f32:
Nate Begeman307e7442005-03-26 01:28:53 +0000336 case MVT::f64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000337 if (FPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000338 args_to_use.push_back(Args[i].first);
339 --FPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000340 if (isVarArg) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000341 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
342 Args[i].first, PtrOff);
343 MemOps.push_back(Store);
Nate Begeman74d73452005-03-31 00:15:26 +0000344 // Float varargs are always shadowed in available integer registers
345 if (GPR_remaining > 0) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000346 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff);
Nate Begeman74d73452005-03-31 00:15:26 +0000347 MemOps.push_back(Load);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000348 args_to_use.push_back(Load);
349 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000350 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000351 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Nate Begeman74d73452005-03-31 00:15:26 +0000352 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
353 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
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 } else {
360 // If we have any FPRs remaining, we may also have GPRs remaining.
361 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
362 // GPRs.
363 if (GPR_remaining > 0) {
364 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
365 --GPR_remaining;
366 }
367 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
368 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
369 --GPR_remaining;
370 }
Nate Begeman74d73452005-03-31 00:15:26 +0000371 }
Nate Begeman307e7442005-03-26 01:28:53 +0000372 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000373 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
374 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000375 }
Nate Begemanf7e43382005-03-26 07:46:36 +0000376 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
Nate Begeman307e7442005-03-26 01:28:53 +0000377 break;
378 }
Nate Begemana9795f82005-03-24 04:41:43 +0000379 }
Nate Begeman74d73452005-03-31 00:15:26 +0000380 if (!MemOps.empty())
381 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
Nate Begemana9795f82005-03-24 04:41:43 +0000382 }
383
384 std::vector<MVT::ValueType> RetVals;
385 MVT::ValueType RetTyVT = getValueType(RetTy);
386 if (RetTyVT != MVT::isVoid)
387 RetVals.push_back(RetTyVT);
388 RetVals.push_back(MVT::Other);
389
390 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
391 Chain, Callee, args_to_use), 0);
392 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
393 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
394 DAG.getConstant(NumBytes, getPointerTy()));
395 return std::make_pair(TheCall, Chain);
396}
397
398std::pair<SDOperand, SDOperand>
399PPC32TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
400 //vastart just returns the address of the VarArgsFrameIndex slot.
401 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
402}
403
404std::pair<SDOperand,SDOperand> PPC32TargetLowering::
405LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
406 const Type *ArgTy, SelectionDAG &DAG) {
Nate Begemanc7b09f12005-03-25 08:34:25 +0000407 MVT::ValueType ArgVT = getValueType(ArgTy);
408 SDOperand Result;
409 if (!isVANext) {
410 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList);
411 } else {
412 unsigned Amt;
413 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
414 Amt = 4;
415 else {
416 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
417 "Other types should have been promoted for varargs!");
418 Amt = 8;
419 }
420 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
421 DAG.getConstant(Amt, VAList.getValueType()));
422 }
423 return std::make_pair(Result, Chain);
Nate Begemana9795f82005-03-24 04:41:43 +0000424}
425
426
427std::pair<SDOperand, SDOperand> PPC32TargetLowering::
428LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
429 SelectionDAG &DAG) {
Nate Begeman01d05262005-03-30 01:45:43 +0000430 assert(0 && "LowerFrameReturnAddress unimplemented");
Nate Begemana9795f82005-03-24 04:41:43 +0000431 abort();
432}
433
434namespace {
Nate Begemanaa73a9f2005-04-03 11:20:20 +0000435Statistic<>NotLogic("ppc-codegen", "Number of inverted logical ops");
Nate Begeman93075ec2005-04-04 23:40:36 +0000436Statistic<>FusedFP("ppc-codegen", "Number of fused fp operations");
Nate Begemana9795f82005-03-24 04:41:43 +0000437//===--------------------------------------------------------------------===//
438/// ISel - PPC32 specific code to select PPC32 machine instructions for
439/// SelectionDAG operations.
440//===--------------------------------------------------------------------===//
441class ISel : public SelectionDAGISel {
442
443 /// Comment Here.
444 PPC32TargetLowering PPC32Lowering;
445
446 /// ExprMap - As shared expressions are codegen'd, we keep track of which
447 /// vreg the value is produced in, so we only emit one copy of each compiled
448 /// tree.
449 std::map<SDOperand, unsigned> ExprMap;
Nate Begemanc7b09f12005-03-25 08:34:25 +0000450
451 unsigned GlobalBaseReg;
452 bool GlobalBaseInitialized;
Nate Begemana9795f82005-03-24 04:41:43 +0000453
454public:
455 ISel(TargetMachine &TM) : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM)
456 {}
457
Nate Begemanc7b09f12005-03-25 08:34:25 +0000458 /// runOnFunction - Override this function in order to reset our per-function
459 /// variables.
460 virtual bool runOnFunction(Function &Fn) {
461 // Make sure we re-emit a set of the global base reg if necessary
462 GlobalBaseInitialized = false;
463 return SelectionDAGISel::runOnFunction(Fn);
464 }
465
Nate Begemana9795f82005-03-24 04:41:43 +0000466 /// InstructionSelectBasicBlock - This callback is invoked by
467 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
468 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
469 DEBUG(BB->dump());
470 // Codegen the basic block.
471 Select(DAG.getRoot());
472
473 // Clear state used for selection.
474 ExprMap.clear();
475 }
476
Nate Begemandffcfcc2005-04-01 00:32:34 +0000477 unsigned getGlobalBaseReg();
Nate Begeman6b559972005-04-01 02:59:27 +0000478 unsigned getConstDouble(double floatVal, unsigned Result);
Nate Begemandffcfcc2005-04-01 00:32:34 +0000479 unsigned SelectSetCR0(SDOperand CC);
Nate Begemana9795f82005-03-24 04:41:43 +0000480 unsigned SelectExpr(SDOperand N);
481 unsigned SelectExprFP(SDOperand N, unsigned Result);
482 void Select(SDOperand N);
483
Nate Begeman04730362005-04-01 04:45:11 +0000484 bool SelectAddr(SDOperand N, unsigned& Reg, int& offset);
Nate Begemana9795f82005-03-24 04:41:43 +0000485 void SelectBranchCC(SDOperand N);
486};
487
Nate Begeman80196b12005-04-05 00:15:08 +0000488/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
489/// returns zero when the input is not exactly a power of two.
490static unsigned ExactLog2(unsigned Val) {
491 if (Val == 0 || (Val & (Val-1))) return 0;
492 unsigned Count = 0;
493 while (Val != 1) {
494 Val >>= 1;
495 ++Count;
496 }
497 return Count;
498}
499
Nate Begemana9795f82005-03-24 04:41:43 +0000500/// canUseAsImmediateForOpcode - This method returns a value indicating whether
501/// the ConstantSDNode N can be used as an immediate to Opcode. The return
502/// values are either 0, 1 or 2. 0 indicates that either N is not a
503/// ConstantSDNode, or is not suitable for use by that opcode. A return value
504/// of 1 indicates that the constant may be used in normal immediate form. A
505/// return value of 2 indicates that the constant may be used in shifted
506/// immediate form. If the return value is nonzero, the constant value is
507/// placed in Imm.
508///
509static unsigned canUseAsImmediateForOpcode(SDOperand N, unsigned Opcode,
Nate Begeman3e897162005-03-31 23:55:40 +0000510 unsigned& Imm, bool U = false) {
Nate Begemana9795f82005-03-24 04:41:43 +0000511 if (N.getOpcode() != ISD::Constant) return 0;
512
513 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
514
515 switch(Opcode) {
516 default: return 0;
517 case ISD::ADD:
518 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
519 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
520 break;
521 case ISD::AND:
522 case ISD::XOR:
523 case ISD::OR:
524 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
525 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
526 break;
Nate Begeman307e7442005-03-26 01:28:53 +0000527 case ISD::MUL:
Nate Begeman27523a12005-04-02 00:42:16 +0000528 case ISD::SUB:
Nate Begeman307e7442005-03-26 01:28:53 +0000529 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
530 break;
Nate Begeman3e897162005-03-31 23:55:40 +0000531 case ISD::SETCC:
532 if (U && (v >= 0 && v <= 65535)) { Imm = v & 0xFFFF; return 1; }
533 if (!U && (v <= 32767 && v >= -32768)) { Imm = v & 0xFFFF; return 1; }
534 break;
Nate Begeman80196b12005-04-05 00:15:08 +0000535 case ISD::SDIV:
536 if (0 != ExactLog2(v)) { Imm = ExactLog2(v); return 1; }
537 break;
Nate Begemana9795f82005-03-24 04:41:43 +0000538 }
539 return 0;
540}
Nate Begeman3e897162005-03-31 23:55:40 +0000541
542/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
543/// to Condition. If the Condition is unordered or unsigned, the bool argument
544/// U is set to true, otherwise it is set to false.
545static unsigned getBCCForSetCC(unsigned Condition, bool& U) {
546 U = false;
547 switch (Condition) {
548 default: assert(0 && "Unknown condition!"); abort();
549 case ISD::SETEQ: return PPC::BEQ;
550 case ISD::SETNE: return PPC::BNE;
551 case ISD::SETULT: U = true;
552 case ISD::SETLT: return PPC::BLT;
553 case ISD::SETULE: U = true;
554 case ISD::SETLE: return PPC::BLE;
555 case ISD::SETUGT: U = true;
556 case ISD::SETGT: return PPC::BGT;
557 case ISD::SETUGE: U = true;
558 case ISD::SETGE: return PPC::BGE;
559 }
Nate Begeman04730362005-04-01 04:45:11 +0000560 return 0;
561}
562
563/// IndexedOpForOp - Return the indexed variant for each of the PowerPC load
564/// and store immediate instructions.
565static unsigned IndexedOpForOp(unsigned Opcode) {
566 switch(Opcode) {
567 default: assert(0 && "Unknown opcode!"); abort();
568 case PPC::LBZ: return PPC::LBZX; case PPC::STB: return PPC::STBX;
569 case PPC::LHZ: return PPC::LHZX; case PPC::STH: return PPC::STHX;
570 case PPC::LHA: return PPC::LHAX; case PPC::STW: return PPC::STWX;
571 case PPC::LWZ: return PPC::LWZX; case PPC::STFS: return PPC::STFSX;
572 case PPC::LFS: return PPC::LFSX; case PPC::STFD: return PPC::STFDX;
573 case PPC::LFD: return PPC::LFDX;
574 }
575 return 0;
Nate Begeman3e897162005-03-31 23:55:40 +0000576}
Nate Begemana9795f82005-03-24 04:41:43 +0000577}
578
Nate Begemanc7b09f12005-03-25 08:34:25 +0000579/// getGlobalBaseReg - Output the instructions required to put the
580/// base address to use for accessing globals into a register.
581///
582unsigned ISel::getGlobalBaseReg() {
583 if (!GlobalBaseInitialized) {
584 // Insert the set of GlobalBaseReg into the first MBB of the function
585 MachineBasicBlock &FirstMBB = BB->getParent()->front();
586 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
587 GlobalBaseReg = MakeReg(MVT::i32);
588 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
589 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR);
590 GlobalBaseInitialized = true;
591 }
592 return GlobalBaseReg;
593}
594
Nate Begeman6b559972005-04-01 02:59:27 +0000595/// getConstDouble - Loads a floating point value into a register, via the
596/// Constant Pool. Optionally takes a register in which to load the value.
597unsigned ISel::getConstDouble(double doubleVal, unsigned Result=0) {
598 unsigned Tmp1 = MakeReg(MVT::i32);
599 if (0 == Result) Result = MakeReg(MVT::f64);
600 MachineConstantPool *CP = BB->getParent()->getConstantPool();
601 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, doubleVal);
602 unsigned CPI = CP->getConstantPoolIndex(CFP);
603 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
604 .addConstantPoolIndex(CPI);
605 BuildMI(BB, PPC::LFD, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
606 return Result;
607}
608
Nate Begemandffcfcc2005-04-01 00:32:34 +0000609unsigned ISel::SelectSetCR0(SDOperand CC) {
610 unsigned Opc, Tmp1, Tmp2;
611 static const unsigned CompareOpcodes[] =
612 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
613
614 // If the first operand to the select is a SETCC node, then we can fold it
615 // into the branch that selects which value to return.
616 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
617 if (SetCC && CC.getOpcode() == ISD::SETCC) {
618 bool U;
619 Opc = getBCCForSetCC(SetCC->getCondition(), U);
620 Tmp1 = SelectExpr(SetCC->getOperand(0));
621
622 // Pass the optional argument U to canUseAsImmediateForOpcode for SETCC,
623 // so that it knows whether the SETCC immediate range is signed or not.
624 if (1 == canUseAsImmediateForOpcode(SetCC->getOperand(1), ISD::SETCC,
625 Tmp2, U)) {
626 if (U)
627 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(Tmp2);
628 else
629 BuildMI(BB, PPC::CMPWI, 2, PPC::CR0).addReg(Tmp1).addSImm(Tmp2);
630 } else {
631 bool IsInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
632 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
633 Tmp2 = SelectExpr(SetCC->getOperand(1));
634 BuildMI(BB, CompareOpc, 2, PPC::CR0).addReg(Tmp1).addReg(Tmp2);
635 }
636 } else {
637 Tmp1 = SelectExpr(CC);
638 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(0);
639 Opc = PPC::BNE;
640 }
641 return Opc;
642}
643
644/// Check to see if the load is a constant offset from a base register
Nate Begeman04730362005-04-01 04:45:11 +0000645bool ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
Nate Begemana9795f82005-03-24 04:41:43 +0000646{
Nate Begeman96fc6812005-03-31 02:05:53 +0000647 unsigned imm = 0, opcode = N.getOpcode();
Nate Begeman04730362005-04-01 04:45:11 +0000648 if (N.getOpcode() == ISD::ADD) {
649 Reg = SelectExpr(N.getOperand(0));
Nate Begeman96fc6812005-03-31 02:05:53 +0000650 if (1 == canUseAsImmediateForOpcode(N.getOperand(1), opcode, imm)) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000651 offset = imm;
Nate Begeman04730362005-04-01 04:45:11 +0000652 return false;
653 }
654 offset = SelectExpr(N.getOperand(1));
655 return true;
656 }
Nate Begemana9795f82005-03-24 04:41:43 +0000657 Reg = SelectExpr(N);
658 offset = 0;
Nate Begeman04730362005-04-01 04:45:11 +0000659 return false;
Nate Begemana9795f82005-03-24 04:41:43 +0000660}
661
662void ISel::SelectBranchCC(SDOperand N)
663{
664 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
665 MachineBasicBlock *Dest =
666 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Nate Begeman3e897162005-03-31 23:55:40 +0000667
Nate Begemana9795f82005-03-24 04:41:43 +0000668 Select(N.getOperand(0)); //chain
Nate Begemandffcfcc2005-04-01 00:32:34 +0000669 unsigned Opc = SelectSetCR0(N.getOperand(1));
Nate Begeman3e897162005-03-31 23:55:40 +0000670 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(Dest);
Nate Begemana9795f82005-03-24 04:41:43 +0000671 return;
672}
673
674unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
675{
676 unsigned Tmp1, Tmp2, Tmp3;
677 unsigned Opc = 0;
678 SDNode *Node = N.Val;
679 MVT::ValueType DestType = N.getValueType();
680 unsigned opcode = N.getOpcode();
681
682 switch (opcode) {
683 default:
684 Node->dump();
685 assert(0 && "Node not handled!\n");
686
Nate Begeman23afcfb2005-03-29 22:48:55 +0000687 case ISD::SELECT: {
Nate Begeman3e897162005-03-31 23:55:40 +0000688 // Attempt to generate FSEL. We can do this whenever we have an FP result,
689 // and an FP comparison in the SetCC node.
690 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val);
691 if (SetCC && N.getOperand(0).getOpcode() == ISD::SETCC &&
692 !MVT::isInteger(SetCC->getOperand(0).getValueType()) &&
693 SetCC->getCondition() != ISD::SETEQ &&
694 SetCC->getCondition() != ISD::SETNE) {
695 MVT::ValueType VT = SetCC->getOperand(0).getValueType();
696 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
697 unsigned TV = SelectExpr(N.getOperand(1)); // Use if TRUE
698 unsigned FV = SelectExpr(N.getOperand(2)); // Use if FALSE
699
700 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1));
701 if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) {
702 switch(SetCC->getCondition()) {
703 default: assert(0 && "Invalid FSEL condition"); abort();
704 case ISD::SETULT:
705 case ISD::SETLT:
706 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(FV).addReg(TV);
707 return Result;
708 case ISD::SETUGE:
709 case ISD::SETGE:
710 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV);
711 return Result;
712 case ISD::SETUGT:
713 case ISD::SETGT: {
714 Tmp2 = MakeReg(VT);
715 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
716 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(FV).addReg(TV);
717 return Result;
718 }
719 case ISD::SETULE:
720 case ISD::SETLE: {
721 Tmp2 = MakeReg(VT);
722 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
723 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV);
724 return Result;
725 }
726 }
727 } else {
728 Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS;
729 Tmp2 = SelectExpr(SetCC->getOperand(1));
730 Tmp3 = MakeReg(VT);
731 switch(SetCC->getCondition()) {
732 default: assert(0 && "Invalid FSEL condition"); abort();
733 case ISD::SETULT:
734 case ISD::SETLT:
735 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
736 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
737 return Result;
738 case ISD::SETUGE:
739 case ISD::SETGE:
740 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
741 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
742 return Result;
743 case ISD::SETUGT:
744 case ISD::SETGT:
745 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
746 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
747 return Result;
748 case ISD::SETULE:
749 case ISD::SETLE:
750 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
751 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
752 return Result;
753 }
754 }
755 assert(0 && "Should never get here");
756 return 0;
757 }
758
Nate Begeman31318e42005-04-01 07:21:30 +0000759 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
760 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman6cb2e1b2005-04-01 08:57:43 +0000761 Opc = SelectSetCR0(N.getOperand(0));
Nate Begeman31318e42005-04-01 07:21:30 +0000762
Nate Begeman23afcfb2005-03-29 22:48:55 +0000763 // Create an iterator with which to insert the MBB for copying the false
764 // value and the MBB to hold the PHI instruction for this SetCC.
765 MachineBasicBlock *thisMBB = BB;
766 const BasicBlock *LLVM_BB = BB->getBasicBlock();
767 ilist<MachineBasicBlock>::iterator It = BB;
768 ++It;
769
770 // thisMBB:
771 // ...
772 // TrueVal = ...
773 // cmpTY cr0, r1, r2
774 // bCC copy1MBB
775 // fallthrough --> copy0MBB
Nate Begeman23afcfb2005-03-29 22:48:55 +0000776 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
777 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman6cb2e1b2005-04-01 08:57:43 +0000778 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
Nate Begeman23afcfb2005-03-29 22:48:55 +0000779 MachineFunction *F = BB->getParent();
780 F->getBasicBlockList().insert(It, copy0MBB);
781 F->getBasicBlockList().insert(It, sinkMBB);
782 // Update machine-CFG edges
783 BB->addSuccessor(copy0MBB);
784 BB->addSuccessor(sinkMBB);
785
786 // copy0MBB:
787 // %FalseValue = ...
788 // # fallthrough to sinkMBB
789 BB = copy0MBB;
Nate Begeman23afcfb2005-03-29 22:48:55 +0000790 // Update machine-CFG edges
791 BB->addSuccessor(sinkMBB);
792
793 // sinkMBB:
794 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
795 // ...
796 BB = sinkMBB;
797 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
798 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
799 return Result;
800 }
Nate Begeman27eeb002005-04-02 05:59:34 +0000801
802 case ISD::FNEG:
Nate Begeman93075ec2005-04-04 23:40:36 +0000803 if (!NoExcessFPPrecision &&
804 ISD::ADD == N.getOperand(0).getOpcode() &&
805 N.getOperand(0).Val->hasOneUse() &&
806 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
807 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
Nate Begeman80196b12005-04-05 00:15:08 +0000808 ++FusedFP; // Statistic
Nate Begeman93075ec2005-04-04 23:40:36 +0000809 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
810 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
811 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
812 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
813 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
814 } else if (!NoExcessFPPrecision &&
815 ISD::SUB == N.getOperand(0).getOpcode() &&
816 N.getOperand(0).Val->hasOneUse() &&
817 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
818 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
Nate Begeman80196b12005-04-05 00:15:08 +0000819 ++FusedFP; // Statistic
Nate Begeman93075ec2005-04-04 23:40:36 +0000820 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
821 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
822 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
823 Opc = DestType == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS;
824 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
825 } else if (ISD::FABS == N.getOperand(0).getOpcode()) {
Nate Begeman27eeb002005-04-02 05:59:34 +0000826 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
827 BuildMI(BB, PPC::FNABS, 1, Result).addReg(Tmp1);
828 } else {
829 Tmp1 = SelectExpr(N.getOperand(0));
830 BuildMI(BB, PPC::FNEG, 1, Result).addReg(Tmp1);
831 }
832 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000833
Nate Begeman27eeb002005-04-02 05:59:34 +0000834 case ISD::FABS:
835 Tmp1 = SelectExpr(N.getOperand(0));
836 BuildMI(BB, PPC::FABS, 1, Result).addReg(Tmp1);
837 return Result;
838
Nate Begemana9795f82005-03-24 04:41:43 +0000839 case ISD::FP_ROUND:
840 assert (DestType == MVT::f32 &&
841 N.getOperand(0).getValueType() == MVT::f64 &&
842 "only f64 to f32 conversion supported here");
843 Tmp1 = SelectExpr(N.getOperand(0));
844 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
845 return Result;
846
847 case ISD::FP_EXTEND:
848 assert (DestType == MVT::f64 &&
849 N.getOperand(0).getValueType() == MVT::f32 &&
850 "only f32 to f64 conversion supported here");
851 Tmp1 = SelectExpr(N.getOperand(0));
852 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
853 return Result;
854
855 case ISD::CopyFromReg:
Nate Begemanf2622612005-03-26 02:17:46 +0000856 if (Result == 1)
857 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
858 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
859 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
860 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000861
Nate Begeman6d369cc2005-04-01 01:08:07 +0000862 case ISD::ConstantFP: {
Nate Begeman6d369cc2005-04-01 01:08:07 +0000863 ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
Nate Begeman6b559972005-04-01 02:59:27 +0000864 Result = getConstDouble(CN->getValue(), Result);
Nate Begeman6d369cc2005-04-01 01:08:07 +0000865 return Result;
866 }
Nate Begemana9795f82005-03-24 04:41:43 +0000867
Nate Begemana9795f82005-03-24 04:41:43 +0000868 case ISD::ADD:
Nate Begeman93075ec2005-04-04 23:40:36 +0000869 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
870 N.getOperand(0).Val->hasOneUse()) {
871 ++FusedFP; // Statistic
872 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
873 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
874 Tmp3 = SelectExpr(N.getOperand(1));
875 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
876 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
877 return Result;
878 }
879 Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS;
880 Tmp1 = SelectExpr(N.getOperand(0));
881 Tmp2 = SelectExpr(N.getOperand(1));
882 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
883 return Result;
884
Nate Begemana9795f82005-03-24 04:41:43 +0000885 case ISD::SUB:
Nate Begeman93075ec2005-04-04 23:40:36 +0000886 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
887 N.getOperand(0).Val->hasOneUse()) {
888 ++FusedFP; // Statistic
889 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
890 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
891 Tmp3 = SelectExpr(N.getOperand(1));
892 Opc = DestType == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS;
893 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
894 return Result;
895 }
896 Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS;
897 Tmp1 = SelectExpr(N.getOperand(0));
898 Tmp2 = SelectExpr(N.getOperand(1));
899 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
900 return Result;
901
902 case ISD::MUL:
Nate Begemana9795f82005-03-24 04:41:43 +0000903 case ISD::SDIV:
904 switch( opcode ) {
905 case ISD::MUL: Opc = DestType == MVT::f64 ? PPC::FMUL : PPC::FMULS; break;
Nate Begemana9795f82005-03-24 04:41:43 +0000906 case ISD::SDIV: Opc = DestType == MVT::f64 ? PPC::FDIV : PPC::FDIVS; break;
907 };
Nate Begemana9795f82005-03-24 04:41:43 +0000908 Tmp1 = SelectExpr(N.getOperand(0));
909 Tmp2 = SelectExpr(N.getOperand(1));
910 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
911 return Result;
912
Nate Begemana9795f82005-03-24 04:41:43 +0000913 case ISD::UINT_TO_FP:
Nate Begemanfdcf3412005-03-30 19:38:35 +0000914 case ISD::SINT_TO_FP: {
915 assert (N.getOperand(0).getValueType() == MVT::i32
916 && "int to float must operate on i32");
917 bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
918 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
919 Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into
920 Tmp3 = MakeReg(MVT::i32); // temp reg to hold the conversion constant
921 unsigned ConstF = MakeReg(MVT::f64); // temp reg to hold the fp constant
922
923 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
924 MachineConstantPool *CP = BB->getParent()->getConstantPool();
925
926 // FIXME: pull this FP constant generation stuff out into something like
927 // the simple ISel's getReg.
928 if (IsUnsigned) {
929 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000000p52);
930 unsigned CPI = CP->getConstantPoolIndex(CFP);
931 // Load constant fp value
932 unsigned Tmp4 = MakeReg(MVT::i32);
933 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg())
934 .addConstantPoolIndex(CPI);
935 BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
936 // Store the hi & low halves of the fp value, currently in int regs
937 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
938 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
939 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp1), FrameIdx, 4);
940 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
941 // Generate the return value with a subtract
942 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
943 } else {
944 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000008p52);
945 unsigned CPI = CP->getConstantPoolIndex(CFP);
946 // Load constant fp value
947 unsigned Tmp4 = MakeReg(MVT::i32);
948 unsigned TmpL = MakeReg(MVT::i32);
949 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg())
950 .addConstantPoolIndex(CPI);
951 BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
952 // Store the hi & low halves of the fp value, currently in int regs
953 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
954 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
955 BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
956 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
957 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
958 // Generate the return value with a subtract
959 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
960 }
961 return Result;
962 }
Nate Begemana9795f82005-03-24 04:41:43 +0000963 }
Nate Begeman6b559972005-04-01 02:59:27 +0000964 assert(0 && "Should never get here");
Nate Begemana9795f82005-03-24 04:41:43 +0000965 return 0;
966}
967
968unsigned ISel::SelectExpr(SDOperand N) {
969 unsigned Result;
970 unsigned Tmp1, Tmp2, Tmp3;
971 unsigned Opc = 0;
972 unsigned opcode = N.getOpcode();
973
974 SDNode *Node = N.Val;
975 MVT::ValueType DestType = N.getValueType();
976
977 unsigned &Reg = ExprMap[N];
978 if (Reg) return Reg;
979
Nate Begeman27eeb002005-04-02 05:59:34 +0000980 switch (N.getOpcode()) {
981 default:
Nate Begemana9795f82005-03-24 04:41:43 +0000982 Reg = Result = (N.getValueType() != MVT::Other) ?
Nate Begeman27eeb002005-04-02 05:59:34 +0000983 MakeReg(N.getValueType()) : 1;
984 break;
985 case ISD::CALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000986 // If this is a call instruction, make sure to prepare ALL of the result
987 // values as well as the chain.
Nate Begeman27eeb002005-04-02 05:59:34 +0000988 if (Node->getNumValues() == 1)
989 Reg = Result = 1; // Void call, just a chain.
990 else {
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000991 Result = MakeReg(Node->getValueType(0));
992 ExprMap[N.getValue(0)] = Result;
Nate Begeman27eeb002005-04-02 05:59:34 +0000993 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000994 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Nate Begeman27eeb002005-04-02 05:59:34 +0000995 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000996 }
Nate Begeman27eeb002005-04-02 05:59:34 +0000997 break;
998 case ISD::ADD_PARTS:
999 case ISD::SUB_PARTS:
1000 case ISD::SHL_PARTS:
1001 case ISD::SRL_PARTS:
1002 case ISD::SRA_PARTS:
1003 Result = MakeReg(Node->getValueType(0));
1004 ExprMap[N.getValue(0)] = Result;
1005 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
1006 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1007 break;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001008 }
1009
Nate Begemane5846682005-04-04 06:52:38 +00001010 if (ISD::CopyFromReg == opcode)
1011 DestType = N.getValue(0).getValueType();
1012
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001013 if (DestType == MVT::f64 || DestType == MVT::f32)
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001014 if (ISD::LOAD != opcode && ISD::EXTLOAD != opcode && ISD::UNDEF != opcode)
Nate Begeman74d73452005-03-31 00:15:26 +00001015 return SelectExprFP(N, Result);
Nate Begemana9795f82005-03-24 04:41:43 +00001016
1017 switch (opcode) {
1018 default:
1019 Node->dump();
1020 assert(0 && "Node not handled!\n");
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001021 case ISD::UNDEF:
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001022 BuildMI(BB, PPC::IMPLICIT_DEF, 0, Result);
1023 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001024 case ISD::DYNAMIC_STACKALLOC:
Nate Begeman5e966612005-03-24 06:28:42 +00001025 // Generate both result values. FIXME: Need a better commment here?
1026 if (Result != 1)
1027 ExprMap[N.getValue(1)] = 1;
1028 else
1029 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1030
1031 // FIXME: We are currently ignoring the requested alignment for handling
1032 // greater than the stack alignment. This will need to be revisited at some
1033 // point. Align = N.getOperand(2);
1034 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1035 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1036 std::cerr << "Cannot allocate stack object with greater alignment than"
1037 << " the stack alignment yet!";
1038 abort();
1039 }
1040 Select(N.getOperand(0));
1041 Tmp1 = SelectExpr(N.getOperand(1));
1042 // Subtract size from stack pointer, thereby allocating some space.
1043 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
1044 // Put a pointer to the space into the result register by copying the SP
1045 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
1046 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001047
1048 case ISD::ConstantPool:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001049 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1050 Tmp2 = MakeReg(MVT::i32);
1051 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp2).addReg(getGlobalBaseReg())
1052 .addConstantPoolIndex(Tmp1);
1053 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
1054 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001055
1056 case ISD::FrameIndex:
Nate Begemanf3d08f32005-03-29 00:03:27 +00001057 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Nate Begeman58f718c2005-03-30 02:23:08 +00001058 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
Nate Begemanf3d08f32005-03-29 00:03:27 +00001059 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001060
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001061 case ISD::GlobalAddress: {
1062 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanca12a2b2005-03-28 22:28:37 +00001063 Tmp1 = MakeReg(MVT::i32);
Nate Begemanc7b09f12005-03-25 08:34:25 +00001064 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1065 .addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001066 if (GV->hasWeakLinkage() || GV->isExternal()) {
1067 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
1068 } else {
1069 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
1070 }
1071 return Result;
1072 }
1073
Nate Begeman5e966612005-03-24 06:28:42 +00001074 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +00001075 case ISD::EXTLOAD:
1076 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001077 case ISD::SEXTLOAD: {
Nate Begeman9db505c2005-03-28 19:36:43 +00001078 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
1079 Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType();
Nate Begeman74d73452005-03-31 00:15:26 +00001080 bool sext = (ISD::SEXTLOAD == opcode);
1081 bool byte = (MVT::i8 == TypeBeingLoaded);
1082
Nate Begeman5e966612005-03-24 06:28:42 +00001083 // Make sure we generate both values.
1084 if (Result != 1)
1085 ExprMap[N.getValue(1)] = 1; // Generate the token
1086 else
1087 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1088
1089 SDOperand Chain = N.getOperand(0);
1090 SDOperand Address = N.getOperand(1);
1091 Select(Chain);
1092
Nate Begeman9db505c2005-03-28 19:36:43 +00001093 switch (TypeBeingLoaded) {
Nate Begeman74d73452005-03-31 00:15:26 +00001094 default: Node->dump(); assert(0 && "Cannot load this type!");
Nate Begeman9db505c2005-03-28 19:36:43 +00001095 case MVT::i1: Opc = PPC::LBZ; break;
1096 case MVT::i8: Opc = PPC::LBZ; break;
1097 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
1098 case MVT::i32: Opc = PPC::LWZ; break;
Nate Begeman74d73452005-03-31 00:15:26 +00001099 case MVT::f32: Opc = PPC::LFS; break;
1100 case MVT::f64: Opc = PPC::LFD; break;
Nate Begeman5e966612005-03-24 06:28:42 +00001101 }
1102
Nate Begeman74d73452005-03-31 00:15:26 +00001103 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1104 Tmp1 = MakeReg(MVT::i32);
1105 int CPI = CP->getIndex();
1106 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1107 .addConstantPoolIndex(CPI);
1108 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001109 }
Nate Begeman74d73452005-03-31 00:15:26 +00001110 else if(Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman58f718c2005-03-30 02:23:08 +00001111 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
1112 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
Nate Begeman5e966612005-03-24 06:28:42 +00001113 } else {
1114 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00001115 bool idx = SelectAddr(Address, Tmp1, offset);
1116 if (idx) {
1117 Opc = IndexedOpForOp(Opc);
1118 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset);
1119 } else {
1120 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
1121 }
Nate Begeman5e966612005-03-24 06:28:42 +00001122 }
1123 return Result;
1124 }
1125
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001126 case ISD::CALL: {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001127 unsigned GPR_idx = 0, FPR_idx = 0;
1128 static const unsigned GPR[] = {
1129 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1130 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1131 };
1132 static const unsigned FPR[] = {
1133 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1134 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1135 };
1136
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001137 // Lower the chain for this call.
1138 Select(N.getOperand(0));
1139 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
Nate Begeman74d73452005-03-31 00:15:26 +00001140
Nate Begemand860aa62005-04-04 22:17:48 +00001141 MachineInstr *CallMI;
1142 // Emit the correct call instruction based on the type of symbol called.
1143 if (GlobalAddressSDNode *GASD =
1144 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
1145 CallMI = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(),
1146 true);
1147 } else if (ExternalSymbolSDNode *ESSDN =
1148 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
1149 CallMI = BuildMI(PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(),
1150 true);
1151 } else {
1152 Tmp1 = SelectExpr(N.getOperand(1));
1153 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
1154 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
1155 CallMI = BuildMI(PPC::CALLindirect, 3).addImm(20).addImm(0)
1156 .addReg(PPC::R12);
1157 }
1158
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001159 // Load the register args to virtual regs
1160 std::vector<unsigned> ArgVR;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001161 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001162 ArgVR.push_back(SelectExpr(N.getOperand(i)));
1163
1164 // Copy the virtual registers into the appropriate argument register
1165 for(int i = 0, e = ArgVR.size(); i < e; ++i) {
1166 switch(N.getOperand(i+2).getValueType()) {
1167 default: Node->dump(); assert(0 && "Unknown value type for call");
1168 case MVT::i1:
1169 case MVT::i8:
1170 case MVT::i16:
1171 case MVT::i32:
1172 assert(GPR_idx < 8 && "Too many int args");
Nate Begemand860aa62005-04-04 22:17:48 +00001173 if (N.getOperand(i+2).getOpcode() != ISD::UNDEF) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001174 BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001175 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1176 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001177 ++GPR_idx;
1178 break;
1179 case MVT::f64:
1180 case MVT::f32:
1181 assert(FPR_idx < 13 && "Too many fp args");
1182 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001183 CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001184 ++FPR_idx;
1185 break;
1186 }
1187 }
Nate Begemand860aa62005-04-04 22:17:48 +00001188
1189 // Put the call instruction in the correct place in the MachineBasicBlock
1190 BB->push_back(CallMI);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001191
1192 switch (Node->getValueType(0)) {
1193 default: assert(0 && "Unknown value type for call result!");
1194 case MVT::Other: return 1;
1195 case MVT::i1:
1196 case MVT::i8:
1197 case MVT::i16:
1198 case MVT::i32:
Nate Begemane5846682005-04-04 06:52:38 +00001199 if (Node->getValueType(1) == MVT::i32) {
1200 BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R3).addReg(PPC::R3);
1201 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R4).addReg(PPC::R4);
1202 } else {
1203 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
1204 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001205 break;
1206 case MVT::f32:
1207 case MVT::f64:
1208 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1209 break;
1210 }
1211 return Result+N.ResNo;
1212 }
Nate Begemana9795f82005-03-24 04:41:43 +00001213
1214 case ISD::SIGN_EXTEND:
1215 case ISD::SIGN_EXTEND_INREG:
1216 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman9db505c2005-03-28 19:36:43 +00001217 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1218 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
1219 case MVT::i16:
1220 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
1221 break;
1222 case MVT::i8:
1223 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
1224 break;
Nate Begeman74747862005-03-29 22:24:51 +00001225 case MVT::i1:
1226 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1227 break;
Nate Begeman9db505c2005-03-28 19:36:43 +00001228 }
Nate Begemana9795f82005-03-24 04:41:43 +00001229 return Result;
1230
1231 case ISD::ZERO_EXTEND_INREG:
1232 Tmp1 = SelectExpr(N.getOperand(0));
1233 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
Nate Begeman9db505c2005-03-28 19:36:43 +00001234 default: Node->dump(); assert(0 && "Unhandled ZERO_EXTEND type"); break;
Nate Begemana9795f82005-03-24 04:41:43 +00001235 case MVT::i16: Tmp2 = 16; break;
1236 case MVT::i8: Tmp2 = 24; break;
1237 case MVT::i1: Tmp2 = 31; break;
1238 }
Nate Begeman33162522005-03-29 21:54:38 +00001239 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(0).addImm(Tmp2)
1240 .addImm(31);
Nate Begemana9795f82005-03-24 04:41:43 +00001241 return Result;
1242
Nate Begemana9795f82005-03-24 04:41:43 +00001243 case ISD::CopyFromReg:
1244 if (Result == 1)
1245 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1246 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1247 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1248 return Result;
1249
1250 case ISD::SHL:
Nate Begeman5e966612005-03-24 06:28:42 +00001251 Tmp1 = SelectExpr(N.getOperand(0));
1252 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1253 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001254 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
Nate Begeman5e966612005-03-24 06:28:42 +00001255 .addImm(31-Tmp2);
1256 } else {
1257 Tmp2 = SelectExpr(N.getOperand(1));
1258 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1259 }
1260 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001261
Nate Begeman5e966612005-03-24 06:28:42 +00001262 case ISD::SRL:
1263 Tmp1 = SelectExpr(N.getOperand(0));
1264 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1265 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001266 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2)
Nate Begeman5e966612005-03-24 06:28:42 +00001267 .addImm(Tmp2).addImm(31);
1268 } else {
1269 Tmp2 = SelectExpr(N.getOperand(1));
1270 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1271 }
1272 return Result;
1273
1274 case ISD::SRA:
1275 Tmp1 = SelectExpr(N.getOperand(0));
1276 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1277 Tmp2 = CN->getValue() & 0x1F;
1278 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1279 } else {
1280 Tmp2 = SelectExpr(N.getOperand(1));
1281 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1282 }
1283 return Result;
1284
Nate Begemana9795f82005-03-24 04:41:43 +00001285 case ISD::ADD:
1286 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1287 Tmp1 = SelectExpr(N.getOperand(0));
1288 switch(canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1289 default: assert(0 && "unhandled result code");
1290 case 0: // No immediate
1291 Tmp2 = SelectExpr(N.getOperand(1));
1292 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1293 break;
1294 case 1: // Low immediate
1295 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1296 break;
1297 case 2: // Shifted immediate
1298 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1299 break;
1300 }
1301 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001302
Nate Begemana9795f82005-03-24 04:41:43 +00001303 case ISD::AND:
1304 case ISD::OR:
Nate Begemana9795f82005-03-24 04:41:43 +00001305 Tmp1 = SelectExpr(N.getOperand(0));
1306 switch(canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1307 default: assert(0 && "unhandled result code");
1308 case 0: // No immediate
1309 Tmp2 = SelectExpr(N.getOperand(1));
1310 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +00001311 case ISD::AND: Opc = PPC::AND; break;
1312 case ISD::OR: Opc = PPC::OR; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001313 }
Nate Begeman5e966612005-03-24 06:28:42 +00001314 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001315 break;
1316 case 1: // Low immediate
1317 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +00001318 case ISD::AND: Opc = PPC::ANDIo; break;
1319 case ISD::OR: Opc = PPC::ORI; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001320 }
Nate Begeman5e966612005-03-24 06:28:42 +00001321 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001322 break;
1323 case 2: // Shifted immediate
1324 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +00001325 case ISD::AND: Opc = PPC::ANDISo; break;
1326 case ISD::OR: Opc = PPC::ORIS; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001327 }
Nate Begeman5e966612005-03-24 06:28:42 +00001328 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001329 break;
1330 }
1331 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001332
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001333 case ISD::XOR: {
1334 // Check for EQV: xor, (xor a, -1), b
1335 if (N.getOperand(0).getOpcode() == ISD::XOR &&
1336 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1337 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) {
1338 ++NotLogic;
1339 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1340 Tmp2 = SelectExpr(N.getOperand(1));
1341 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1342 return Result;
1343 }
1344 // Check for NOT, NOR, and NAND: xor (copy, or, and), -1
1345 if (N.getOperand(1).getOpcode() == ISD::Constant &&
1346 cast<ConstantSDNode>(N.getOperand(1))->isAllOnesValue()) {
1347 ++NotLogic;
1348 switch(N.getOperand(0).getOpcode()) {
1349 case ISD::OR:
1350 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1351 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1352 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1353 break;
1354 case ISD::AND:
1355 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1356 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1357 BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2);
1358 break;
1359 default:
1360 Tmp1 = SelectExpr(N.getOperand(0));
1361 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1362 break;
1363 }
1364 return Result;
1365 }
1366 Tmp1 = SelectExpr(N.getOperand(0));
1367 switch(canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1368 default: assert(0 && "unhandled result code");
1369 case 0: // No immediate
1370 Tmp2 = SelectExpr(N.getOperand(1));
1371 BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1372 break;
1373 case 1: // Low immediate
1374 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1375 break;
1376 case 2: // Shifted immediate
1377 BuildMI(BB, PPC::XORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
1378 break;
1379 }
1380 return Result;
1381 }
1382
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001383 case ISD::SUB:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001384 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman27523a12005-04-02 00:42:16 +00001385 if (1 == canUseAsImmediateForOpcode(N.getOperand(0), opcode, Tmp1))
1386 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1);
1387 else {
1388 Tmp1 = SelectExpr(N.getOperand(0));
1389 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
1390 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001391 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001392
Nate Begeman5e966612005-03-24 06:28:42 +00001393 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001394 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman307e7442005-03-26 01:28:53 +00001395 if (1 == canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
1396 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1397 else {
1398 Tmp2 = SelectExpr(N.getOperand(1));
1399 BuildMI(BB, PPC::MULLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1400 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001401 return Result;
1402
Nate Begemanf3d08f32005-03-29 00:03:27 +00001403 case ISD::SDIV:
1404 case ISD::UDIV:
Nate Begeman80196b12005-04-05 00:15:08 +00001405 if (1 == canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp3)) {
1406 Tmp1 = MakeReg(MVT::i32);
1407 Tmp2 = SelectExpr(N.getOperand(0));
1408 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
1409 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp1);
1410 return Result;
1411 }
Nate Begemanf3d08f32005-03-29 00:03:27 +00001412 Tmp1 = SelectExpr(N.getOperand(0));
1413 Tmp2 = SelectExpr(N.getOperand(1));
1414 Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW;
1415 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1416 return Result;
1417
1418 case ISD::UREM:
1419 case ISD::SREM: {
Nate Begemanf3d08f32005-03-29 00:03:27 +00001420 Tmp1 = SelectExpr(N.getOperand(0));
1421 Tmp2 = SelectExpr(N.getOperand(1));
1422 Tmp3 = MakeReg(MVT::i32);
1423 unsigned Tmp4 = MakeReg(MVT::i32);
1424 Opc = (ISD::UREM == opcode) ? PPC::DIVWU : PPC::DIVW;
1425 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1426 BuildMI(BB, PPC::MULLW, 2, Tmp4).addReg(Tmp3).addReg(Tmp2);
1427 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp4).addReg(Tmp1);
1428 return Result;
1429 }
1430
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001431 case ISD::ADD_PARTS:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001432 case ISD::SUB_PARTS: {
1433 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1434 "Not an i64 add/sub!");
1435 // Emit all of the operands.
1436 std::vector<unsigned> InVals;
1437 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
1438 InVals.push_back(SelectExpr(N.getOperand(i)));
1439 if (N.getOpcode() == ISD::ADD_PARTS) {
Nate Begeman27eeb002005-04-02 05:59:34 +00001440 BuildMI(BB, PPC::ADDC, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
1441 BuildMI(BB, PPC::ADDE, 2, Result+1).addReg(InVals[1]).addReg(InVals[3]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001442 } else {
Nate Begeman27eeb002005-04-02 05:59:34 +00001443 BuildMI(BB, PPC::SUBFC, 2, Result).addReg(InVals[2]).addReg(InVals[0]);
1444 BuildMI(BB, PPC::SUBFE, 2, Result+1).addReg(InVals[3]).addReg(InVals[1]);
1445 }
1446 return Result+N.ResNo;
1447 }
1448
1449 case ISD::SHL_PARTS:
1450 case ISD::SRA_PARTS:
1451 case ISD::SRL_PARTS: {
1452 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
1453 "Not an i64 shift!");
1454 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
1455 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
1456 unsigned SHReg = SelectExpr(N.getOperand(2));
1457 Tmp1 = MakeReg(MVT::i32);
1458 Tmp2 = MakeReg(MVT::i32);
1459 Tmp3 = MakeReg(MVT::i32);
1460 unsigned Tmp4 = MakeReg(MVT::i32);
1461 unsigned Tmp5 = MakeReg(MVT::i32);
1462 unsigned Tmp6 = MakeReg(MVT::i32);
1463 BuildMI(BB, PPC::SUBFIC, 2, Tmp1).addReg(SHReg).addSImm(32);
1464 if (ISD::SHL_PARTS == opcode) {
1465 BuildMI(BB, PPC::SLW, 2, Tmp2).addReg(ShiftOpHi).addReg(SHReg);
1466 BuildMI(BB, PPC::SRW, 2, Tmp3).addReg(ShiftOpLo).addReg(Tmp1);
1467 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1468 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
Nate Begemanfa554702005-04-03 22:13:27 +00001469 BuildMI(BB, PPC::SLW, 2, Tmp6).addReg(ShiftOpLo).addReg(Tmp5);
Nate Begeman27eeb002005-04-02 05:59:34 +00001470 BuildMI(BB, PPC::OR, 2, Result+1).addReg(Tmp4).addReg(Tmp6);
1471 BuildMI(BB, PPC::SLW, 2, Result).addReg(ShiftOpLo).addReg(SHReg);
1472 } else if (ISD::SRL_PARTS == opcode) {
1473 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1474 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1475 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1476 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
1477 BuildMI(BB, PPC::SRW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1478 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp4).addReg(Tmp6);
1479 BuildMI(BB, PPC::SRW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1480 } else {
1481 MachineBasicBlock *TmpMBB = new MachineBasicBlock(BB->getBasicBlock());
1482 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1483 MachineBasicBlock *OldMBB = BB;
1484 MachineFunction *F = BB->getParent();
1485 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1486 F->getBasicBlockList().insert(It, TmpMBB);
1487 F->getBasicBlockList().insert(It, PhiMBB);
1488 BB->addSuccessor(TmpMBB);
1489 BB->addSuccessor(PhiMBB);
1490 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1491 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1492 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1493 BuildMI(BB, PPC::ADDICo, 2, Tmp5).addReg(SHReg).addSImm(-32);
1494 BuildMI(BB, PPC::SRAW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1495 BuildMI(BB, PPC::SRAW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1496 BuildMI(BB, PPC::BLE, 2).addReg(PPC::CR0).addMBB(PhiMBB);
1497 // Select correct least significant half if the shift amount > 32
1498 BB = TmpMBB;
1499 unsigned Tmp7 = MakeReg(MVT::i32);
1500 BuildMI(BB, PPC::OR, 2, Tmp7).addReg(Tmp6).addReg(Tmp6);
1501 TmpMBB->addSuccessor(PhiMBB);
1502 BB = PhiMBB;
1503 BuildMI(BB, PPC::PHI, 4, Result).addReg(Tmp4).addMBB(OldMBB)
1504 .addReg(Tmp7).addMBB(TmpMBB);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001505 }
1506 return Result+N.ResNo;
1507 }
1508
Nate Begemana9795f82005-03-24 04:41:43 +00001509 case ISD::FP_TO_UINT:
Nate Begeman6b559972005-04-01 02:59:27 +00001510 case ISD::FP_TO_SINT: {
1511 bool U = (ISD::FP_TO_UINT == opcode);
1512 Tmp1 = SelectExpr(N.getOperand(0));
1513 if (!U) {
1514 Tmp2 = MakeReg(MVT::f64);
1515 BuildMI(BB, PPC::FCTIWZ, 1, Tmp2).addReg(Tmp1);
1516 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1517 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx);
1518 addFrameReference(BuildMI(BB, PPC::LWZ, 2, Result), FrameIdx, 4);
1519 return Result;
1520 } else {
1521 unsigned Zero = getConstDouble(0.0);
1522 unsigned MaxInt = getConstDouble((1LL << 32) - 1);
1523 unsigned Border = getConstDouble(1LL << 31);
1524 unsigned UseZero = MakeReg(MVT::f64);
1525 unsigned UseMaxInt = MakeReg(MVT::f64);
1526 unsigned UseChoice = MakeReg(MVT::f64);
1527 unsigned TmpReg = MakeReg(MVT::f64);
1528 unsigned TmpReg2 = MakeReg(MVT::f64);
1529 unsigned ConvReg = MakeReg(MVT::f64);
1530 unsigned IntTmp = MakeReg(MVT::i32);
1531 unsigned XorReg = MakeReg(MVT::i32);
1532 MachineFunction *F = BB->getParent();
1533 int FrameIdx = F->getFrameInfo()->CreateStackObject(8, 8);
1534 // Update machine-CFG edges
1535 MachineBasicBlock *XorMBB = new MachineBasicBlock(BB->getBasicBlock());
1536 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1537 MachineBasicBlock *OldMBB = BB;
1538 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1539 F->getBasicBlockList().insert(It, XorMBB);
1540 F->getBasicBlockList().insert(It, PhiMBB);
1541 BB->addSuccessor(XorMBB);
1542 BB->addSuccessor(PhiMBB);
1543 // Convert from floating point to unsigned 32-bit value
1544 // Use 0 if incoming value is < 0.0
1545 BuildMI(BB, PPC::FSEL, 3, UseZero).addReg(Tmp1).addReg(Tmp1).addReg(Zero);
1546 // Use 2**32 - 1 if incoming value is >= 2**32
1547 BuildMI(BB, PPC::FSUB, 2, UseMaxInt).addReg(MaxInt).addReg(Tmp1);
1548 BuildMI(BB, PPC::FSEL, 3, UseChoice).addReg(UseMaxInt).addReg(UseZero)
1549 .addReg(MaxInt);
1550 // Subtract 2**31
1551 BuildMI(BB, PPC::FSUB, 2, TmpReg).addReg(UseChoice).addReg(Border);
1552 // Use difference if >= 2**31
1553 BuildMI(BB, PPC::FCMPU, 2, PPC::CR0).addReg(UseChoice).addReg(Border);
1554 BuildMI(BB, PPC::FSEL, 3, TmpReg2).addReg(TmpReg).addReg(TmpReg)
1555 .addReg(UseChoice);
1556 // Convert to integer
1557 BuildMI(BB, PPC::FCTIWZ, 1, ConvReg).addReg(TmpReg2);
1558 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(ConvReg), FrameIdx);
1559 addFrameReference(BuildMI(BB, PPC::LWZ, 2, IntTmp), FrameIdx, 4);
1560 BuildMI(BB, PPC::BLT, 2).addReg(PPC::CR0).addMBB(PhiMBB);
1561 BuildMI(BB, PPC::B, 1).addMBB(XorMBB);
1562
1563 // XorMBB:
1564 // add 2**31 if input was >= 2**31
1565 BB = XorMBB;
1566 BuildMI(BB, PPC::XORIS, 2, XorReg).addReg(IntTmp).addImm(0x8000);
1567 XorMBB->addSuccessor(PhiMBB);
1568
1569 // PhiMBB:
1570 // DestReg = phi [ IntTmp, OldMBB ], [ XorReg, XorMBB ]
1571 BB = PhiMBB;
1572 BuildMI(BB, PPC::PHI, 4, Result).addReg(IntTmp).addMBB(OldMBB)
1573 .addReg(XorReg).addMBB(XorMBB);
1574 return Result;
1575 }
1576 assert(0 && "Should never get here");
1577 return 0;
1578 }
Nate Begemana9795f82005-03-24 04:41:43 +00001579
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001580 case ISD::SETCC:
Nate Begeman33162522005-03-29 21:54:38 +00001581 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
Nate Begemandffcfcc2005-04-01 00:32:34 +00001582 Opc = SelectSetCR0(N);
Nate Begeman33162522005-03-29 21:54:38 +00001583
Nate Begeman31318e42005-04-01 07:21:30 +00001584 unsigned TrueValue = MakeReg(MVT::i32);
1585 BuildMI(BB, PPC::LI, 1, TrueValue).addSImm(1);
1586 unsigned FalseValue = MakeReg(MVT::i32);
1587 BuildMI(BB, PPC::LI, 1, FalseValue).addSImm(0);
1588
Nate Begeman33162522005-03-29 21:54:38 +00001589 // Create an iterator with which to insert the MBB for copying the false
1590 // value and the MBB to hold the PHI instruction for this SetCC.
1591 MachineBasicBlock *thisMBB = BB;
1592 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1593 ilist<MachineBasicBlock>::iterator It = BB;
1594 ++It;
1595
1596 // thisMBB:
1597 // ...
1598 // cmpTY cr0, r1, r2
1599 // %TrueValue = li 1
1600 // bCC sinkMBB
Nate Begeman33162522005-03-29 21:54:38 +00001601 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1602 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1603 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
1604 MachineFunction *F = BB->getParent();
1605 F->getBasicBlockList().insert(It, copy0MBB);
1606 F->getBasicBlockList().insert(It, sinkMBB);
1607 // Update machine-CFG edges
1608 BB->addSuccessor(copy0MBB);
1609 BB->addSuccessor(sinkMBB);
1610
1611 // copy0MBB:
1612 // %FalseValue = li 0
1613 // fallthrough
1614 BB = copy0MBB;
Nate Begeman33162522005-03-29 21:54:38 +00001615 // Update machine-CFG edges
1616 BB->addSuccessor(sinkMBB);
1617
1618 // sinkMBB:
1619 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1620 // ...
1621 BB = sinkMBB;
1622 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1623 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1624 return Result;
1625 }
1626 assert(0 && "Is this legal?");
1627 return 0;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001628
Nate Begeman74747862005-03-29 22:24:51 +00001629 case ISD::SELECT: {
Chris Lattner30710192005-04-01 07:10:02 +00001630 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
1631 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman6cb2e1b2005-04-01 08:57:43 +00001632 Opc = SelectSetCR0(N.getOperand(0));
Chris Lattner30710192005-04-01 07:10:02 +00001633
Nate Begeman74747862005-03-29 22:24:51 +00001634 // Create an iterator with which to insert the MBB for copying the false
1635 // value and the MBB to hold the PHI instruction for this SetCC.
1636 MachineBasicBlock *thisMBB = BB;
1637 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1638 ilist<MachineBasicBlock>::iterator It = BB;
1639 ++It;
1640
1641 // thisMBB:
1642 // ...
1643 // TrueVal = ...
1644 // cmpTY cr0, r1, r2
1645 // bCC copy1MBB
1646 // fallthrough --> copy0MBB
Nate Begeman74747862005-03-29 22:24:51 +00001647 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1648 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman3e897162005-03-31 23:55:40 +00001649 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
Nate Begeman74747862005-03-29 22:24:51 +00001650 MachineFunction *F = BB->getParent();
1651 F->getBasicBlockList().insert(It, copy0MBB);
1652 F->getBasicBlockList().insert(It, sinkMBB);
1653 // Update machine-CFG edges
1654 BB->addSuccessor(copy0MBB);
1655 BB->addSuccessor(sinkMBB);
1656
1657 // copy0MBB:
1658 // %FalseValue = ...
1659 // # fallthrough to sinkMBB
1660 BB = copy0MBB;
Nate Begeman74747862005-03-29 22:24:51 +00001661 // Update machine-CFG edges
1662 BB->addSuccessor(sinkMBB);
1663
1664 // sinkMBB:
1665 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1666 // ...
1667 BB = sinkMBB;
1668 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1669 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1670
1671 // FIXME: Select i64?
1672 return Result;
1673 }
Nate Begemana9795f82005-03-24 04:41:43 +00001674
1675 case ISD::Constant:
1676 switch (N.getValueType()) {
1677 default: assert(0 && "Cannot use constants of this type!");
1678 case MVT::i1:
1679 BuildMI(BB, PPC::LI, 1, Result)
1680 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
1681 break;
1682 case MVT::i32:
1683 {
1684 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
1685 if (v < 32768 && v >= -32768) {
1686 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
1687 } else {
Nate Begeman5e966612005-03-24 06:28:42 +00001688 Tmp1 = MakeReg(MVT::i32);
1689 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
1690 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
Nate Begemana9795f82005-03-24 04:41:43 +00001691 }
1692 }
1693 }
1694 return Result;
1695 }
1696
1697 return 0;
1698}
1699
1700void ISel::Select(SDOperand N) {
1701 unsigned Tmp1, Tmp2, Opc;
1702 unsigned opcode = N.getOpcode();
1703
1704 if (!ExprMap.insert(std::make_pair(N, 1)).second)
1705 return; // Already selected.
1706
1707 SDNode *Node = N.Val;
1708
1709 switch (Node->getOpcode()) {
1710 default:
1711 Node->dump(); std::cerr << "\n";
1712 assert(0 && "Node not handled yet!");
1713 case ISD::EntryToken: return; // Noop
1714 case ISD::TokenFactor:
1715 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1716 Select(Node->getOperand(i));
1717 return;
1718 case ISD::ADJCALLSTACKDOWN:
1719 case ISD::ADJCALLSTACKUP:
1720 Select(N.getOperand(0));
1721 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1722 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? PPC::ADJCALLSTACKDOWN :
1723 PPC::ADJCALLSTACKUP;
1724 BuildMI(BB, Opc, 1).addImm(Tmp1);
1725 return;
1726 case ISD::BR: {
1727 MachineBasicBlock *Dest =
1728 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
Nate Begemana9795f82005-03-24 04:41:43 +00001729 Select(N.getOperand(0));
1730 BuildMI(BB, PPC::B, 1).addMBB(Dest);
1731 return;
1732 }
1733 case ISD::BRCOND:
1734 SelectBranchCC(N);
1735 return;
1736 case ISD::CopyToReg:
1737 Select(N.getOperand(0));
1738 Tmp1 = SelectExpr(N.getOperand(1));
1739 Tmp2 = cast<RegSDNode>(N)->getReg();
1740
1741 if (Tmp1 != Tmp2) {
1742 if (N.getOperand(1).getValueType() == MVT::f64 ||
1743 N.getOperand(1).getValueType() == MVT::f32)
1744 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
1745 else
1746 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1747 }
1748 return;
1749 case ISD::ImplicitDef:
1750 Select(N.getOperand(0));
1751 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
1752 return;
1753 case ISD::RET:
1754 switch (N.getNumOperands()) {
1755 default:
1756 assert(0 && "Unknown return instruction!");
1757 case 3:
1758 assert(N.getOperand(1).getValueType() == MVT::i32 &&
1759 N.getOperand(2).getValueType() == MVT::i32 &&
1760 "Unknown two-register value!");
1761 Select(N.getOperand(0));
1762 Tmp1 = SelectExpr(N.getOperand(1));
1763 Tmp2 = SelectExpr(N.getOperand(2));
Nate Begeman27523a12005-04-02 00:42:16 +00001764 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2);
1765 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00001766 break;
1767 case 2:
1768 Select(N.getOperand(0));
1769 Tmp1 = SelectExpr(N.getOperand(1));
1770 switch (N.getOperand(1).getValueType()) {
1771 default:
1772 assert(0 && "Unknown return type!");
1773 case MVT::f64:
1774 case MVT::f32:
1775 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
1776 break;
1777 case MVT::i32:
1778 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
1779 break;
1780 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001781 case 1:
1782 Select(N.getOperand(0));
1783 break;
Nate Begemana9795f82005-03-24 04:41:43 +00001784 }
1785 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
1786 return;
Nate Begemana9795f82005-03-24 04:41:43 +00001787 case ISD::TRUNCSTORE:
1788 case ISD::STORE:
1789 {
1790 SDOperand Chain = N.getOperand(0);
1791 SDOperand Value = N.getOperand(1);
1792 SDOperand Address = N.getOperand(2);
1793 Select(Chain);
1794
1795 Tmp1 = SelectExpr(Value); //value
1796
1797 if (opcode == ISD::STORE) {
1798 switch(Value.getValueType()) {
1799 default: assert(0 && "unknown Type in store");
1800 case MVT::i32: Opc = PPC::STW; break;
1801 case MVT::f64: Opc = PPC::STFD; break;
1802 case MVT::f32: Opc = PPC::STFS; break;
1803 }
1804 } else { //ISD::TRUNCSTORE
1805 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1806 default: assert(0 && "unknown Type in store");
1807 case MVT::i1: //FIXME: DAG does not promote this load
1808 case MVT::i8: Opc = PPC::STB; break;
1809 case MVT::i16: Opc = PPC::STH; break;
1810 }
1811 }
1812
Nate Begemana7e11a42005-04-01 05:57:17 +00001813 if(Address.getOpcode() == ISD::FrameIndex)
Nate Begemana9795f82005-03-24 04:41:43 +00001814 {
Nate Begeman58f718c2005-03-30 02:23:08 +00001815 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
1816 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001817 }
1818 else
1819 {
1820 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00001821 bool idx = SelectAddr(Address, Tmp2, offset);
1822 if (idx) {
1823 Opc = IndexedOpForOp(Opc);
1824 BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset);
1825 } else {
1826 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1827 }
Nate Begemana9795f82005-03-24 04:41:43 +00001828 }
1829 return;
1830 }
1831 case ISD::EXTLOAD:
1832 case ISD::SEXTLOAD:
1833 case ISD::ZEXTLOAD:
1834 case ISD::LOAD:
1835 case ISD::CopyFromReg:
1836 case ISD::CALL:
1837 case ISD::DYNAMIC_STACKALLOC:
1838 ExprMap.erase(N);
1839 SelectExpr(N);
1840 return;
1841 }
1842 assert(0 && "Should not be reached!");
1843}
1844
1845
1846/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
1847/// into a machine code representation using pattern matching and a machine
1848/// description file.
1849///
1850FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) {
1851 return new ISel(TM);
Chris Lattner246fa632005-03-24 06:16:18 +00001852}
1853