blob: f480ed21eb66d0bc6eae12ce0bba69165da8210d [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 Begeman439b4442005-04-05 04:22:58 +0000500/// getImmediateForOpcode - This method returns a value indicating whether
Nate Begemana9795f82005-03-24 04:41:43 +0000501/// 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
Nate Begeman439b4442005-04-05 04:22:58 +0000506/// immediate form. A return value of 3 indicates that log base 2 of the
507/// constant may be used.
Nate Begemana9795f82005-03-24 04:41:43 +0000508///
Nate Begeman439b4442005-04-05 04:22:58 +0000509static unsigned getImmediateForOpcode(SDOperand N, unsigned Opcode,
510 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:
Nate Begeman439b4442005-04-05 04:22:58 +0000536 if ((Imm = ExactLog2(v))) { return 3; }
Nate Begeman80196b12005-04-05 00:15:08 +0000537 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
Nate Begeman439b4442005-04-05 04:22:58 +0000622 // Pass the optional argument U to getImmediateForOpcode for SETCC,
Nate Begemandffcfcc2005-04-01 00:32:34 +0000623 // so that it knows whether the SETCC immediate range is signed or not.
Nate Begeman439b4442005-04-05 04:22:58 +0000624 if (1 == getImmediateForOpcode(SetCC->getOperand(1), ISD::SETCC,
625 Tmp2, U)) {
Nate Begemandffcfcc2005-04-01 00:32:34 +0000626 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 Begeman439b4442005-04-05 04:22:58 +0000650 if (1 == getImmediateForOpcode(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 Begeman439b4442005-04-05 04:22:58 +0000668 // Get the MBB we will fall through to so that we can hand it off to the
669 // branch selection pass as an argument to the PPC::COND_BRANCH pseudo op.
670 ilist<MachineBasicBlock>::iterator It = BB;
671 MachineBasicBlock *Fallthrough = ++It;
672
Nate Begemana9795f82005-03-24 04:41:43 +0000673 Select(N.getOperand(0)); //chain
Nate Begemandffcfcc2005-04-01 00:32:34 +0000674 unsigned Opc = SelectSetCR0(N.getOperand(1));
Nate Begeman439b4442005-04-05 04:22:58 +0000675 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(PPC::CR0).addImm(Opc)
676 .addMBB(Dest).addMBB(Fallthrough);
Nate Begemana9795f82005-03-24 04:41:43 +0000677 return;
678}
679
680unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
681{
682 unsigned Tmp1, Tmp2, Tmp3;
683 unsigned Opc = 0;
684 SDNode *Node = N.Val;
685 MVT::ValueType DestType = N.getValueType();
686 unsigned opcode = N.getOpcode();
687
688 switch (opcode) {
689 default:
690 Node->dump();
691 assert(0 && "Node not handled!\n");
692
Nate Begeman23afcfb2005-03-29 22:48:55 +0000693 case ISD::SELECT: {
Nate Begeman3e897162005-03-31 23:55:40 +0000694 // Attempt to generate FSEL. We can do this whenever we have an FP result,
695 // and an FP comparison in the SetCC node.
696 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val);
697 if (SetCC && N.getOperand(0).getOpcode() == ISD::SETCC &&
698 !MVT::isInteger(SetCC->getOperand(0).getValueType()) &&
699 SetCC->getCondition() != ISD::SETEQ &&
700 SetCC->getCondition() != ISD::SETNE) {
701 MVT::ValueType VT = SetCC->getOperand(0).getValueType();
702 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
703 unsigned TV = SelectExpr(N.getOperand(1)); // Use if TRUE
704 unsigned FV = SelectExpr(N.getOperand(2)); // Use if FALSE
705
706 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1));
707 if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) {
708 switch(SetCC->getCondition()) {
709 default: assert(0 && "Invalid FSEL condition"); abort();
710 case ISD::SETULT:
711 case ISD::SETLT:
712 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(FV).addReg(TV);
713 return Result;
714 case ISD::SETUGE:
715 case ISD::SETGE:
716 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV);
717 return Result;
718 case ISD::SETUGT:
719 case ISD::SETGT: {
720 Tmp2 = MakeReg(VT);
721 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
722 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(FV).addReg(TV);
723 return Result;
724 }
725 case ISD::SETULE:
726 case ISD::SETLE: {
727 Tmp2 = MakeReg(VT);
728 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
729 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV);
730 return Result;
731 }
732 }
733 } else {
734 Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS;
735 Tmp2 = SelectExpr(SetCC->getOperand(1));
736 Tmp3 = MakeReg(VT);
737 switch(SetCC->getCondition()) {
738 default: assert(0 && "Invalid FSEL condition"); abort();
739 case ISD::SETULT:
740 case ISD::SETLT:
741 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
742 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
743 return Result;
744 case ISD::SETUGE:
745 case ISD::SETGE:
746 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
747 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
748 return Result;
749 case ISD::SETUGT:
750 case ISD::SETGT:
751 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
752 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
753 return Result;
754 case ISD::SETULE:
755 case ISD::SETLE:
756 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
757 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
758 return Result;
759 }
760 }
761 assert(0 && "Should never get here");
762 return 0;
763 }
764
Nate Begeman31318e42005-04-01 07:21:30 +0000765 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
766 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman6cb2e1b2005-04-01 08:57:43 +0000767 Opc = SelectSetCR0(N.getOperand(0));
Nate Begeman31318e42005-04-01 07:21:30 +0000768
Nate Begeman23afcfb2005-03-29 22:48:55 +0000769 // Create an iterator with which to insert the MBB for copying the false
770 // value and the MBB to hold the PHI instruction for this SetCC.
771 MachineBasicBlock *thisMBB = BB;
772 const BasicBlock *LLVM_BB = BB->getBasicBlock();
773 ilist<MachineBasicBlock>::iterator It = BB;
774 ++It;
775
776 // thisMBB:
777 // ...
778 // TrueVal = ...
779 // cmpTY cr0, r1, r2
780 // bCC copy1MBB
781 // fallthrough --> copy0MBB
Nate Begeman23afcfb2005-03-29 22:48:55 +0000782 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
783 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman6cb2e1b2005-04-01 08:57:43 +0000784 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
Nate Begeman23afcfb2005-03-29 22:48:55 +0000785 MachineFunction *F = BB->getParent();
786 F->getBasicBlockList().insert(It, copy0MBB);
787 F->getBasicBlockList().insert(It, sinkMBB);
788 // Update machine-CFG edges
789 BB->addSuccessor(copy0MBB);
790 BB->addSuccessor(sinkMBB);
791
792 // copy0MBB:
793 // %FalseValue = ...
794 // # fallthrough to sinkMBB
795 BB = copy0MBB;
Nate Begeman23afcfb2005-03-29 22:48:55 +0000796 // Update machine-CFG edges
797 BB->addSuccessor(sinkMBB);
798
799 // sinkMBB:
800 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
801 // ...
802 BB = sinkMBB;
803 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
804 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
805 return Result;
806 }
Nate Begeman27eeb002005-04-02 05:59:34 +0000807
808 case ISD::FNEG:
Nate Begeman93075ec2005-04-04 23:40:36 +0000809 if (!NoExcessFPPrecision &&
810 ISD::ADD == N.getOperand(0).getOpcode() &&
811 N.getOperand(0).Val->hasOneUse() &&
812 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
813 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
Nate Begeman80196b12005-04-05 00:15:08 +0000814 ++FusedFP; // Statistic
Nate Begeman93075ec2005-04-04 23:40:36 +0000815 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
816 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
817 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
818 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
819 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
820 } else if (!NoExcessFPPrecision &&
821 ISD::SUB == N.getOperand(0).getOpcode() &&
822 N.getOperand(0).Val->hasOneUse() &&
823 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
824 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
Nate Begeman80196b12005-04-05 00:15:08 +0000825 ++FusedFP; // Statistic
Nate Begeman93075ec2005-04-04 23:40:36 +0000826 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
827 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
828 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
829 Opc = DestType == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS;
830 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
831 } else if (ISD::FABS == N.getOperand(0).getOpcode()) {
Nate Begeman27eeb002005-04-02 05:59:34 +0000832 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
833 BuildMI(BB, PPC::FNABS, 1, Result).addReg(Tmp1);
834 } else {
835 Tmp1 = SelectExpr(N.getOperand(0));
836 BuildMI(BB, PPC::FNEG, 1, Result).addReg(Tmp1);
837 }
838 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000839
Nate Begeman27eeb002005-04-02 05:59:34 +0000840 case ISD::FABS:
841 Tmp1 = SelectExpr(N.getOperand(0));
842 BuildMI(BB, PPC::FABS, 1, Result).addReg(Tmp1);
843 return Result;
844
Nate Begemana9795f82005-03-24 04:41:43 +0000845 case ISD::FP_ROUND:
846 assert (DestType == MVT::f32 &&
847 N.getOperand(0).getValueType() == MVT::f64 &&
848 "only f64 to f32 conversion supported here");
849 Tmp1 = SelectExpr(N.getOperand(0));
850 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
851 return Result;
852
853 case ISD::FP_EXTEND:
854 assert (DestType == MVT::f64 &&
855 N.getOperand(0).getValueType() == MVT::f32 &&
856 "only f32 to f64 conversion supported here");
857 Tmp1 = SelectExpr(N.getOperand(0));
858 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
859 return Result;
860
861 case ISD::CopyFromReg:
Nate Begemanf2622612005-03-26 02:17:46 +0000862 if (Result == 1)
863 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
864 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
865 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
866 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000867
Nate Begeman6d369cc2005-04-01 01:08:07 +0000868 case ISD::ConstantFP: {
Nate Begeman6d369cc2005-04-01 01:08:07 +0000869 ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
Nate Begeman6b559972005-04-01 02:59:27 +0000870 Result = getConstDouble(CN->getValue(), Result);
Nate Begeman6d369cc2005-04-01 01:08:07 +0000871 return Result;
872 }
Nate Begemana9795f82005-03-24 04:41:43 +0000873
Nate Begemana9795f82005-03-24 04:41:43 +0000874 case ISD::ADD:
Nate Begeman93075ec2005-04-04 23:40:36 +0000875 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
876 N.getOperand(0).Val->hasOneUse()) {
877 ++FusedFP; // Statistic
878 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
879 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
880 Tmp3 = SelectExpr(N.getOperand(1));
881 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
882 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
883 return Result;
884 }
885 Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS;
886 Tmp1 = SelectExpr(N.getOperand(0));
887 Tmp2 = SelectExpr(N.getOperand(1));
888 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
889 return Result;
890
Nate Begemana9795f82005-03-24 04:41:43 +0000891 case ISD::SUB:
Nate Begeman93075ec2005-04-04 23:40:36 +0000892 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
893 N.getOperand(0).Val->hasOneUse()) {
894 ++FusedFP; // Statistic
895 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
896 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
897 Tmp3 = SelectExpr(N.getOperand(1));
898 Opc = DestType == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS;
899 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
900 return Result;
901 }
902 Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS;
903 Tmp1 = SelectExpr(N.getOperand(0));
904 Tmp2 = SelectExpr(N.getOperand(1));
905 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
906 return Result;
907
908 case ISD::MUL:
Nate Begemana9795f82005-03-24 04:41:43 +0000909 case ISD::SDIV:
910 switch( opcode ) {
911 case ISD::MUL: Opc = DestType == MVT::f64 ? PPC::FMUL : PPC::FMULS; break;
Nate Begemana9795f82005-03-24 04:41:43 +0000912 case ISD::SDIV: Opc = DestType == MVT::f64 ? PPC::FDIV : PPC::FDIVS; break;
913 };
Nate Begemana9795f82005-03-24 04:41:43 +0000914 Tmp1 = SelectExpr(N.getOperand(0));
915 Tmp2 = SelectExpr(N.getOperand(1));
916 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
917 return Result;
918
Nate Begemana9795f82005-03-24 04:41:43 +0000919 case ISD::UINT_TO_FP:
Nate Begemanfdcf3412005-03-30 19:38:35 +0000920 case ISD::SINT_TO_FP: {
921 assert (N.getOperand(0).getValueType() == MVT::i32
922 && "int to float must operate on i32");
923 bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
924 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
925 Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into
926 Tmp3 = MakeReg(MVT::i32); // temp reg to hold the conversion constant
927 unsigned ConstF = MakeReg(MVT::f64); // temp reg to hold the fp constant
928
929 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
930 MachineConstantPool *CP = BB->getParent()->getConstantPool();
931
932 // FIXME: pull this FP constant generation stuff out into something like
933 // the simple ISel's getReg.
934 if (IsUnsigned) {
935 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000000p52);
936 unsigned CPI = CP->getConstantPoolIndex(CFP);
937 // Load constant fp value
938 unsigned Tmp4 = MakeReg(MVT::i32);
939 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg())
940 .addConstantPoolIndex(CPI);
941 BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
942 // Store the hi & low halves of the fp value, currently in int regs
943 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
944 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
945 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp1), FrameIdx, 4);
946 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
947 // Generate the return value with a subtract
948 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
949 } else {
950 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000008p52);
951 unsigned CPI = CP->getConstantPoolIndex(CFP);
952 // Load constant fp value
953 unsigned Tmp4 = MakeReg(MVT::i32);
954 unsigned TmpL = MakeReg(MVT::i32);
955 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg())
956 .addConstantPoolIndex(CPI);
957 BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
958 // Store the hi & low halves of the fp value, currently in int regs
959 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
960 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
961 BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
962 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
963 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
964 // Generate the return value with a subtract
965 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
966 }
967 return Result;
968 }
Nate Begemana9795f82005-03-24 04:41:43 +0000969 }
Nate Begeman6b559972005-04-01 02:59:27 +0000970 assert(0 && "Should never get here");
Nate Begemana9795f82005-03-24 04:41:43 +0000971 return 0;
972}
973
974unsigned ISel::SelectExpr(SDOperand N) {
975 unsigned Result;
976 unsigned Tmp1, Tmp2, Tmp3;
977 unsigned Opc = 0;
978 unsigned opcode = N.getOpcode();
979
980 SDNode *Node = N.Val;
981 MVT::ValueType DestType = N.getValueType();
982
983 unsigned &Reg = ExprMap[N];
984 if (Reg) return Reg;
985
Nate Begeman27eeb002005-04-02 05:59:34 +0000986 switch (N.getOpcode()) {
987 default:
Nate Begemana9795f82005-03-24 04:41:43 +0000988 Reg = Result = (N.getValueType() != MVT::Other) ?
Nate Begeman27eeb002005-04-02 05:59:34 +0000989 MakeReg(N.getValueType()) : 1;
990 break;
991 case ISD::CALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000992 // If this is a call instruction, make sure to prepare ALL of the result
993 // values as well as the chain.
Nate Begeman27eeb002005-04-02 05:59:34 +0000994 if (Node->getNumValues() == 1)
995 Reg = Result = 1; // Void call, just a chain.
996 else {
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000997 Result = MakeReg(Node->getValueType(0));
998 ExprMap[N.getValue(0)] = Result;
Nate Begeman27eeb002005-04-02 05:59:34 +0000999 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001000 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Nate Begeman27eeb002005-04-02 05:59:34 +00001001 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001002 }
Nate Begeman27eeb002005-04-02 05:59:34 +00001003 break;
1004 case ISD::ADD_PARTS:
1005 case ISD::SUB_PARTS:
1006 case ISD::SHL_PARTS:
1007 case ISD::SRL_PARTS:
1008 case ISD::SRA_PARTS:
1009 Result = MakeReg(Node->getValueType(0));
1010 ExprMap[N.getValue(0)] = Result;
1011 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
1012 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1013 break;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001014 }
1015
Nate Begemane5846682005-04-04 06:52:38 +00001016 if (ISD::CopyFromReg == opcode)
1017 DestType = N.getValue(0).getValueType();
1018
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001019 if (DestType == MVT::f64 || DestType == MVT::f32)
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001020 if (ISD::LOAD != opcode && ISD::EXTLOAD != opcode && ISD::UNDEF != opcode)
Nate Begeman74d73452005-03-31 00:15:26 +00001021 return SelectExprFP(N, Result);
Nate Begemana9795f82005-03-24 04:41:43 +00001022
1023 switch (opcode) {
1024 default:
1025 Node->dump();
1026 assert(0 && "Node not handled!\n");
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001027 case ISD::UNDEF:
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001028 BuildMI(BB, PPC::IMPLICIT_DEF, 0, Result);
1029 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001030 case ISD::DYNAMIC_STACKALLOC:
Nate Begeman5e966612005-03-24 06:28:42 +00001031 // Generate both result values. FIXME: Need a better commment here?
1032 if (Result != 1)
1033 ExprMap[N.getValue(1)] = 1;
1034 else
1035 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1036
1037 // FIXME: We are currently ignoring the requested alignment for handling
1038 // greater than the stack alignment. This will need to be revisited at some
1039 // point. Align = N.getOperand(2);
1040 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1041 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1042 std::cerr << "Cannot allocate stack object with greater alignment than"
1043 << " the stack alignment yet!";
1044 abort();
1045 }
1046 Select(N.getOperand(0));
1047 Tmp1 = SelectExpr(N.getOperand(1));
1048 // Subtract size from stack pointer, thereby allocating some space.
1049 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
1050 // Put a pointer to the space into the result register by copying the SP
1051 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
1052 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001053
1054 case ISD::ConstantPool:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001055 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1056 Tmp2 = MakeReg(MVT::i32);
1057 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp2).addReg(getGlobalBaseReg())
1058 .addConstantPoolIndex(Tmp1);
1059 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
1060 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001061
1062 case ISD::FrameIndex:
Nate Begemanf3d08f32005-03-29 00:03:27 +00001063 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Nate Begeman58f718c2005-03-30 02:23:08 +00001064 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
Nate Begemanf3d08f32005-03-29 00:03:27 +00001065 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001066
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001067 case ISD::GlobalAddress: {
1068 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanca12a2b2005-03-28 22:28:37 +00001069 Tmp1 = MakeReg(MVT::i32);
Nate Begemanc7b09f12005-03-25 08:34:25 +00001070 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1071 .addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001072 if (GV->hasWeakLinkage() || GV->isExternal()) {
1073 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
1074 } else {
1075 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
1076 }
1077 return Result;
1078 }
1079
Nate Begeman5e966612005-03-24 06:28:42 +00001080 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +00001081 case ISD::EXTLOAD:
1082 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001083 case ISD::SEXTLOAD: {
Nate Begeman9db505c2005-03-28 19:36:43 +00001084 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
1085 Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType();
Nate Begeman74d73452005-03-31 00:15:26 +00001086 bool sext = (ISD::SEXTLOAD == opcode);
1087 bool byte = (MVT::i8 == TypeBeingLoaded);
1088
Nate Begeman5e966612005-03-24 06:28:42 +00001089 // Make sure we generate both values.
1090 if (Result != 1)
1091 ExprMap[N.getValue(1)] = 1; // Generate the token
1092 else
1093 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1094
1095 SDOperand Chain = N.getOperand(0);
1096 SDOperand Address = N.getOperand(1);
1097 Select(Chain);
1098
Nate Begeman9db505c2005-03-28 19:36:43 +00001099 switch (TypeBeingLoaded) {
Nate Begeman74d73452005-03-31 00:15:26 +00001100 default: Node->dump(); assert(0 && "Cannot load this type!");
Nate Begeman9db505c2005-03-28 19:36:43 +00001101 case MVT::i1: Opc = PPC::LBZ; break;
1102 case MVT::i8: Opc = PPC::LBZ; break;
1103 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
1104 case MVT::i32: Opc = PPC::LWZ; break;
Nate Begeman74d73452005-03-31 00:15:26 +00001105 case MVT::f32: Opc = PPC::LFS; break;
1106 case MVT::f64: Opc = PPC::LFD; break;
Nate Begeman5e966612005-03-24 06:28:42 +00001107 }
1108
Nate Begeman74d73452005-03-31 00:15:26 +00001109 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1110 Tmp1 = MakeReg(MVT::i32);
1111 int CPI = CP->getIndex();
1112 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1113 .addConstantPoolIndex(CPI);
1114 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001115 }
Nate Begeman74d73452005-03-31 00:15:26 +00001116 else if(Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman58f718c2005-03-30 02:23:08 +00001117 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
1118 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
Nate Begeman5e966612005-03-24 06:28:42 +00001119 } else {
1120 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00001121 bool idx = SelectAddr(Address, Tmp1, offset);
1122 if (idx) {
1123 Opc = IndexedOpForOp(Opc);
1124 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset);
1125 } else {
1126 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
1127 }
Nate Begeman5e966612005-03-24 06:28:42 +00001128 }
1129 return Result;
1130 }
1131
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001132 case ISD::CALL: {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001133 unsigned GPR_idx = 0, FPR_idx = 0;
1134 static const unsigned GPR[] = {
1135 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1136 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1137 };
1138 static const unsigned FPR[] = {
1139 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1140 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1141 };
1142
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001143 // Lower the chain for this call.
1144 Select(N.getOperand(0));
1145 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
Nate Begeman74d73452005-03-31 00:15:26 +00001146
Nate Begemand860aa62005-04-04 22:17:48 +00001147 MachineInstr *CallMI;
1148 // Emit the correct call instruction based on the type of symbol called.
1149 if (GlobalAddressSDNode *GASD =
1150 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
1151 CallMI = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(),
1152 true);
1153 } else if (ExternalSymbolSDNode *ESSDN =
1154 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
1155 CallMI = BuildMI(PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(),
1156 true);
1157 } else {
1158 Tmp1 = SelectExpr(N.getOperand(1));
1159 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
1160 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
1161 CallMI = BuildMI(PPC::CALLindirect, 3).addImm(20).addImm(0)
1162 .addReg(PPC::R12);
1163 }
1164
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001165 // Load the register args to virtual regs
1166 std::vector<unsigned> ArgVR;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001167 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001168 ArgVR.push_back(SelectExpr(N.getOperand(i)));
1169
1170 // Copy the virtual registers into the appropriate argument register
1171 for(int i = 0, e = ArgVR.size(); i < e; ++i) {
1172 switch(N.getOperand(i+2).getValueType()) {
1173 default: Node->dump(); assert(0 && "Unknown value type for call");
1174 case MVT::i1:
1175 case MVT::i8:
1176 case MVT::i16:
1177 case MVT::i32:
1178 assert(GPR_idx < 8 && "Too many int args");
Nate Begemand860aa62005-04-04 22:17:48 +00001179 if (N.getOperand(i+2).getOpcode() != ISD::UNDEF) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001180 BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001181 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1182 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001183 ++GPR_idx;
1184 break;
1185 case MVT::f64:
1186 case MVT::f32:
1187 assert(FPR_idx < 13 && "Too many fp args");
1188 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001189 CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001190 ++FPR_idx;
1191 break;
1192 }
1193 }
Nate Begemand860aa62005-04-04 22:17:48 +00001194
1195 // Put the call instruction in the correct place in the MachineBasicBlock
1196 BB->push_back(CallMI);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001197
1198 switch (Node->getValueType(0)) {
1199 default: assert(0 && "Unknown value type for call result!");
1200 case MVT::Other: return 1;
1201 case MVT::i1:
1202 case MVT::i8:
1203 case MVT::i16:
1204 case MVT::i32:
Nate Begemane5846682005-04-04 06:52:38 +00001205 if (Node->getValueType(1) == MVT::i32) {
1206 BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R3).addReg(PPC::R3);
1207 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R4).addReg(PPC::R4);
1208 } else {
1209 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
1210 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001211 break;
1212 case MVT::f32:
1213 case MVT::f64:
1214 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1215 break;
1216 }
1217 return Result+N.ResNo;
1218 }
Nate Begemana9795f82005-03-24 04:41:43 +00001219
1220 case ISD::SIGN_EXTEND:
1221 case ISD::SIGN_EXTEND_INREG:
1222 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman9db505c2005-03-28 19:36:43 +00001223 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1224 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
1225 case MVT::i16:
1226 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
1227 break;
1228 case MVT::i8:
1229 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
1230 break;
Nate Begeman74747862005-03-29 22:24:51 +00001231 case MVT::i1:
1232 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1233 break;
Nate Begeman9db505c2005-03-28 19:36:43 +00001234 }
Nate Begemana9795f82005-03-24 04:41:43 +00001235 return Result;
1236
1237 case ISD::ZERO_EXTEND_INREG:
1238 Tmp1 = SelectExpr(N.getOperand(0));
1239 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
Nate Begeman9db505c2005-03-28 19:36:43 +00001240 default: Node->dump(); assert(0 && "Unhandled ZERO_EXTEND type"); break;
Nate Begemana9795f82005-03-24 04:41:43 +00001241 case MVT::i16: Tmp2 = 16; break;
1242 case MVT::i8: Tmp2 = 24; break;
1243 case MVT::i1: Tmp2 = 31; break;
1244 }
Nate Begeman33162522005-03-29 21:54:38 +00001245 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(0).addImm(Tmp2)
1246 .addImm(31);
Nate Begemana9795f82005-03-24 04:41:43 +00001247 return Result;
1248
Nate Begemana9795f82005-03-24 04:41:43 +00001249 case ISD::CopyFromReg:
1250 if (Result == 1)
1251 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1252 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1253 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1254 return Result;
1255
1256 case ISD::SHL:
Nate Begeman5e966612005-03-24 06:28:42 +00001257 Tmp1 = SelectExpr(N.getOperand(0));
1258 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1259 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001260 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
Nate Begeman5e966612005-03-24 06:28:42 +00001261 .addImm(31-Tmp2);
1262 } else {
1263 Tmp2 = SelectExpr(N.getOperand(1));
1264 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1265 }
1266 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001267
Nate Begeman5e966612005-03-24 06:28:42 +00001268 case ISD::SRL:
1269 Tmp1 = SelectExpr(N.getOperand(0));
1270 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1271 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001272 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2)
Nate Begeman5e966612005-03-24 06:28:42 +00001273 .addImm(Tmp2).addImm(31);
1274 } else {
1275 Tmp2 = SelectExpr(N.getOperand(1));
1276 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1277 }
1278 return Result;
1279
1280 case ISD::SRA:
1281 Tmp1 = SelectExpr(N.getOperand(0));
1282 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1283 Tmp2 = CN->getValue() & 0x1F;
1284 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1285 } else {
1286 Tmp2 = SelectExpr(N.getOperand(1));
1287 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1288 }
1289 return Result;
1290
Nate Begemana9795f82005-03-24 04:41:43 +00001291 case ISD::ADD:
1292 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1293 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001294 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemana9795f82005-03-24 04:41:43 +00001295 default: assert(0 && "unhandled result code");
1296 case 0: // No immediate
1297 Tmp2 = SelectExpr(N.getOperand(1));
1298 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1299 break;
1300 case 1: // Low immediate
1301 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1302 break;
1303 case 2: // Shifted immediate
1304 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1305 break;
1306 }
1307 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001308
Nate Begemana9795f82005-03-24 04:41:43 +00001309 case ISD::AND:
1310 case ISD::OR:
Nate Begemana9795f82005-03-24 04:41:43 +00001311 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001312 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemana9795f82005-03-24 04:41:43 +00001313 default: assert(0 && "unhandled result code");
1314 case 0: // No immediate
1315 Tmp2 = SelectExpr(N.getOperand(1));
1316 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +00001317 case ISD::AND: Opc = PPC::AND; break;
1318 case ISD::OR: Opc = PPC::OR; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001319 }
Nate Begeman5e966612005-03-24 06:28:42 +00001320 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001321 break;
1322 case 1: // Low immediate
1323 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +00001324 case ISD::AND: Opc = PPC::ANDIo; break;
1325 case ISD::OR: Opc = PPC::ORI; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001326 }
Nate Begeman5e966612005-03-24 06:28:42 +00001327 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001328 break;
1329 case 2: // Shifted immediate
1330 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +00001331 case ISD::AND: Opc = PPC::ANDISo; break;
1332 case ISD::OR: Opc = PPC::ORIS; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001333 }
Nate Begeman5e966612005-03-24 06:28:42 +00001334 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001335 break;
1336 }
1337 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001338
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001339 case ISD::XOR: {
1340 // Check for EQV: xor, (xor a, -1), b
1341 if (N.getOperand(0).getOpcode() == ISD::XOR &&
1342 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1343 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) {
1344 ++NotLogic;
1345 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1346 Tmp2 = SelectExpr(N.getOperand(1));
1347 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1348 return Result;
1349 }
1350 // Check for NOT, NOR, and NAND: xor (copy, or, and), -1
1351 if (N.getOperand(1).getOpcode() == ISD::Constant &&
1352 cast<ConstantSDNode>(N.getOperand(1))->isAllOnesValue()) {
1353 ++NotLogic;
1354 switch(N.getOperand(0).getOpcode()) {
1355 case ISD::OR:
1356 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1357 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1358 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1359 break;
1360 case ISD::AND:
1361 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1362 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1363 BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2);
1364 break;
1365 default:
1366 Tmp1 = SelectExpr(N.getOperand(0));
1367 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1368 break;
1369 }
1370 return Result;
1371 }
1372 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001373 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001374 default: assert(0 && "unhandled result code");
1375 case 0: // No immediate
1376 Tmp2 = SelectExpr(N.getOperand(1));
1377 BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1378 break;
1379 case 1: // Low immediate
1380 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1381 break;
1382 case 2: // Shifted immediate
1383 BuildMI(BB, PPC::XORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
1384 break;
1385 }
1386 return Result;
1387 }
1388
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001389 case ISD::SUB:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001390 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman439b4442005-04-05 04:22:58 +00001391 if (1 == getImmediateForOpcode(N.getOperand(0), opcode, Tmp1))
Nate Begeman27523a12005-04-02 00:42:16 +00001392 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1);
1393 else {
1394 Tmp1 = SelectExpr(N.getOperand(0));
1395 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
1396 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001397 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001398
Nate Begeman5e966612005-03-24 06:28:42 +00001399 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001400 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001401 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
Nate Begeman307e7442005-03-26 01:28:53 +00001402 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1403 else {
1404 Tmp2 = SelectExpr(N.getOperand(1));
1405 BuildMI(BB, PPC::MULLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1406 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001407 return Result;
1408
Nate Begemanf3d08f32005-03-29 00:03:27 +00001409 case ISD::SDIV:
1410 case ISD::UDIV:
Nate Begeman439b4442005-04-05 04:22:58 +00001411 if (3 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp3)) {
Nate Begeman80196b12005-04-05 00:15:08 +00001412 Tmp1 = MakeReg(MVT::i32);
1413 Tmp2 = SelectExpr(N.getOperand(0));
1414 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
1415 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp1);
1416 return Result;
1417 }
Nate Begemanf3d08f32005-03-29 00:03:27 +00001418 Tmp1 = SelectExpr(N.getOperand(0));
1419 Tmp2 = SelectExpr(N.getOperand(1));
1420 Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW;
1421 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1422 return Result;
1423
1424 case ISD::UREM:
1425 case ISD::SREM: {
Nate Begemanf3d08f32005-03-29 00:03:27 +00001426 Tmp1 = SelectExpr(N.getOperand(0));
1427 Tmp2 = SelectExpr(N.getOperand(1));
1428 Tmp3 = MakeReg(MVT::i32);
1429 unsigned Tmp4 = MakeReg(MVT::i32);
1430 Opc = (ISD::UREM == opcode) ? PPC::DIVWU : PPC::DIVW;
1431 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1432 BuildMI(BB, PPC::MULLW, 2, Tmp4).addReg(Tmp3).addReg(Tmp2);
1433 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp4).addReg(Tmp1);
1434 return Result;
1435 }
1436
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001437 case ISD::ADD_PARTS:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001438 case ISD::SUB_PARTS: {
1439 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1440 "Not an i64 add/sub!");
1441 // Emit all of the operands.
1442 std::vector<unsigned> InVals;
1443 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
1444 InVals.push_back(SelectExpr(N.getOperand(i)));
1445 if (N.getOpcode() == ISD::ADD_PARTS) {
Nate Begeman27eeb002005-04-02 05:59:34 +00001446 BuildMI(BB, PPC::ADDC, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
1447 BuildMI(BB, PPC::ADDE, 2, Result+1).addReg(InVals[1]).addReg(InVals[3]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001448 } else {
Nate Begeman27eeb002005-04-02 05:59:34 +00001449 BuildMI(BB, PPC::SUBFC, 2, Result).addReg(InVals[2]).addReg(InVals[0]);
1450 BuildMI(BB, PPC::SUBFE, 2, Result+1).addReg(InVals[3]).addReg(InVals[1]);
1451 }
1452 return Result+N.ResNo;
1453 }
1454
1455 case ISD::SHL_PARTS:
1456 case ISD::SRA_PARTS:
1457 case ISD::SRL_PARTS: {
1458 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
1459 "Not an i64 shift!");
1460 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
1461 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
1462 unsigned SHReg = SelectExpr(N.getOperand(2));
1463 Tmp1 = MakeReg(MVT::i32);
1464 Tmp2 = MakeReg(MVT::i32);
1465 Tmp3 = MakeReg(MVT::i32);
1466 unsigned Tmp4 = MakeReg(MVT::i32);
1467 unsigned Tmp5 = MakeReg(MVT::i32);
1468 unsigned Tmp6 = MakeReg(MVT::i32);
1469 BuildMI(BB, PPC::SUBFIC, 2, Tmp1).addReg(SHReg).addSImm(32);
1470 if (ISD::SHL_PARTS == opcode) {
1471 BuildMI(BB, PPC::SLW, 2, Tmp2).addReg(ShiftOpHi).addReg(SHReg);
1472 BuildMI(BB, PPC::SRW, 2, Tmp3).addReg(ShiftOpLo).addReg(Tmp1);
1473 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1474 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
Nate Begemanfa554702005-04-03 22:13:27 +00001475 BuildMI(BB, PPC::SLW, 2, Tmp6).addReg(ShiftOpLo).addReg(Tmp5);
Nate Begeman27eeb002005-04-02 05:59:34 +00001476 BuildMI(BB, PPC::OR, 2, Result+1).addReg(Tmp4).addReg(Tmp6);
1477 BuildMI(BB, PPC::SLW, 2, Result).addReg(ShiftOpLo).addReg(SHReg);
1478 } else if (ISD::SRL_PARTS == opcode) {
1479 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1480 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1481 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1482 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
1483 BuildMI(BB, PPC::SRW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1484 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp4).addReg(Tmp6);
1485 BuildMI(BB, PPC::SRW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1486 } else {
1487 MachineBasicBlock *TmpMBB = new MachineBasicBlock(BB->getBasicBlock());
1488 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1489 MachineBasicBlock *OldMBB = BB;
1490 MachineFunction *F = BB->getParent();
1491 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1492 F->getBasicBlockList().insert(It, TmpMBB);
1493 F->getBasicBlockList().insert(It, PhiMBB);
1494 BB->addSuccessor(TmpMBB);
1495 BB->addSuccessor(PhiMBB);
1496 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1497 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1498 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1499 BuildMI(BB, PPC::ADDICo, 2, Tmp5).addReg(SHReg).addSImm(-32);
1500 BuildMI(BB, PPC::SRAW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1501 BuildMI(BB, PPC::SRAW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1502 BuildMI(BB, PPC::BLE, 2).addReg(PPC::CR0).addMBB(PhiMBB);
1503 // Select correct least significant half if the shift amount > 32
1504 BB = TmpMBB;
1505 unsigned Tmp7 = MakeReg(MVT::i32);
1506 BuildMI(BB, PPC::OR, 2, Tmp7).addReg(Tmp6).addReg(Tmp6);
1507 TmpMBB->addSuccessor(PhiMBB);
1508 BB = PhiMBB;
1509 BuildMI(BB, PPC::PHI, 4, Result).addReg(Tmp4).addMBB(OldMBB)
1510 .addReg(Tmp7).addMBB(TmpMBB);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001511 }
1512 return Result+N.ResNo;
1513 }
1514
Nate Begemana9795f82005-03-24 04:41:43 +00001515 case ISD::FP_TO_UINT:
Nate Begeman6b559972005-04-01 02:59:27 +00001516 case ISD::FP_TO_SINT: {
1517 bool U = (ISD::FP_TO_UINT == opcode);
1518 Tmp1 = SelectExpr(N.getOperand(0));
1519 if (!U) {
1520 Tmp2 = MakeReg(MVT::f64);
1521 BuildMI(BB, PPC::FCTIWZ, 1, Tmp2).addReg(Tmp1);
1522 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1523 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx);
1524 addFrameReference(BuildMI(BB, PPC::LWZ, 2, Result), FrameIdx, 4);
1525 return Result;
1526 } else {
1527 unsigned Zero = getConstDouble(0.0);
1528 unsigned MaxInt = getConstDouble((1LL << 32) - 1);
1529 unsigned Border = getConstDouble(1LL << 31);
1530 unsigned UseZero = MakeReg(MVT::f64);
1531 unsigned UseMaxInt = MakeReg(MVT::f64);
1532 unsigned UseChoice = MakeReg(MVT::f64);
1533 unsigned TmpReg = MakeReg(MVT::f64);
1534 unsigned TmpReg2 = MakeReg(MVT::f64);
1535 unsigned ConvReg = MakeReg(MVT::f64);
1536 unsigned IntTmp = MakeReg(MVT::i32);
1537 unsigned XorReg = MakeReg(MVT::i32);
1538 MachineFunction *F = BB->getParent();
1539 int FrameIdx = F->getFrameInfo()->CreateStackObject(8, 8);
1540 // Update machine-CFG edges
1541 MachineBasicBlock *XorMBB = new MachineBasicBlock(BB->getBasicBlock());
1542 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1543 MachineBasicBlock *OldMBB = BB;
1544 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1545 F->getBasicBlockList().insert(It, XorMBB);
1546 F->getBasicBlockList().insert(It, PhiMBB);
1547 BB->addSuccessor(XorMBB);
1548 BB->addSuccessor(PhiMBB);
1549 // Convert from floating point to unsigned 32-bit value
1550 // Use 0 if incoming value is < 0.0
1551 BuildMI(BB, PPC::FSEL, 3, UseZero).addReg(Tmp1).addReg(Tmp1).addReg(Zero);
1552 // Use 2**32 - 1 if incoming value is >= 2**32
1553 BuildMI(BB, PPC::FSUB, 2, UseMaxInt).addReg(MaxInt).addReg(Tmp1);
1554 BuildMI(BB, PPC::FSEL, 3, UseChoice).addReg(UseMaxInt).addReg(UseZero)
1555 .addReg(MaxInt);
1556 // Subtract 2**31
1557 BuildMI(BB, PPC::FSUB, 2, TmpReg).addReg(UseChoice).addReg(Border);
1558 // Use difference if >= 2**31
1559 BuildMI(BB, PPC::FCMPU, 2, PPC::CR0).addReg(UseChoice).addReg(Border);
1560 BuildMI(BB, PPC::FSEL, 3, TmpReg2).addReg(TmpReg).addReg(TmpReg)
1561 .addReg(UseChoice);
1562 // Convert to integer
1563 BuildMI(BB, PPC::FCTIWZ, 1, ConvReg).addReg(TmpReg2);
1564 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(ConvReg), FrameIdx);
1565 addFrameReference(BuildMI(BB, PPC::LWZ, 2, IntTmp), FrameIdx, 4);
1566 BuildMI(BB, PPC::BLT, 2).addReg(PPC::CR0).addMBB(PhiMBB);
1567 BuildMI(BB, PPC::B, 1).addMBB(XorMBB);
1568
1569 // XorMBB:
1570 // add 2**31 if input was >= 2**31
1571 BB = XorMBB;
1572 BuildMI(BB, PPC::XORIS, 2, XorReg).addReg(IntTmp).addImm(0x8000);
1573 XorMBB->addSuccessor(PhiMBB);
1574
1575 // PhiMBB:
1576 // DestReg = phi [ IntTmp, OldMBB ], [ XorReg, XorMBB ]
1577 BB = PhiMBB;
1578 BuildMI(BB, PPC::PHI, 4, Result).addReg(IntTmp).addMBB(OldMBB)
1579 .addReg(XorReg).addMBB(XorMBB);
1580 return Result;
1581 }
1582 assert(0 && "Should never get here");
1583 return 0;
1584 }
Nate Begemana9795f82005-03-24 04:41:43 +00001585
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001586 case ISD::SETCC:
Nate Begeman33162522005-03-29 21:54:38 +00001587 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
Nate Begemandffcfcc2005-04-01 00:32:34 +00001588 Opc = SelectSetCR0(N);
Nate Begeman33162522005-03-29 21:54:38 +00001589
Nate Begeman31318e42005-04-01 07:21:30 +00001590 unsigned TrueValue = MakeReg(MVT::i32);
1591 BuildMI(BB, PPC::LI, 1, TrueValue).addSImm(1);
1592 unsigned FalseValue = MakeReg(MVT::i32);
1593 BuildMI(BB, PPC::LI, 1, FalseValue).addSImm(0);
1594
Nate Begeman33162522005-03-29 21:54:38 +00001595 // Create an iterator with which to insert the MBB for copying the false
1596 // value and the MBB to hold the PHI instruction for this SetCC.
1597 MachineBasicBlock *thisMBB = BB;
1598 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1599 ilist<MachineBasicBlock>::iterator It = BB;
1600 ++It;
1601
1602 // thisMBB:
1603 // ...
1604 // cmpTY cr0, r1, r2
1605 // %TrueValue = li 1
1606 // bCC sinkMBB
Nate Begeman33162522005-03-29 21:54:38 +00001607 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1608 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1609 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
1610 MachineFunction *F = BB->getParent();
1611 F->getBasicBlockList().insert(It, copy0MBB);
1612 F->getBasicBlockList().insert(It, sinkMBB);
1613 // Update machine-CFG edges
1614 BB->addSuccessor(copy0MBB);
1615 BB->addSuccessor(sinkMBB);
1616
1617 // copy0MBB:
1618 // %FalseValue = li 0
1619 // fallthrough
1620 BB = copy0MBB;
Nate Begeman33162522005-03-29 21:54:38 +00001621 // Update machine-CFG edges
1622 BB->addSuccessor(sinkMBB);
1623
1624 // sinkMBB:
1625 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1626 // ...
1627 BB = sinkMBB;
1628 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1629 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1630 return Result;
1631 }
1632 assert(0 && "Is this legal?");
1633 return 0;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001634
Nate Begeman74747862005-03-29 22:24:51 +00001635 case ISD::SELECT: {
Chris Lattner30710192005-04-01 07:10:02 +00001636 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
1637 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman6cb2e1b2005-04-01 08:57:43 +00001638 Opc = SelectSetCR0(N.getOperand(0));
Chris Lattner30710192005-04-01 07:10:02 +00001639
Nate Begeman74747862005-03-29 22:24:51 +00001640 // Create an iterator with which to insert the MBB for copying the false
1641 // value and the MBB to hold the PHI instruction for this SetCC.
1642 MachineBasicBlock *thisMBB = BB;
1643 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1644 ilist<MachineBasicBlock>::iterator It = BB;
1645 ++It;
1646
1647 // thisMBB:
1648 // ...
1649 // TrueVal = ...
1650 // cmpTY cr0, r1, r2
1651 // bCC copy1MBB
1652 // fallthrough --> copy0MBB
Nate Begeman74747862005-03-29 22:24:51 +00001653 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1654 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman3e897162005-03-31 23:55:40 +00001655 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
Nate Begeman74747862005-03-29 22:24:51 +00001656 MachineFunction *F = BB->getParent();
1657 F->getBasicBlockList().insert(It, copy0MBB);
1658 F->getBasicBlockList().insert(It, sinkMBB);
1659 // Update machine-CFG edges
1660 BB->addSuccessor(copy0MBB);
1661 BB->addSuccessor(sinkMBB);
1662
1663 // copy0MBB:
1664 // %FalseValue = ...
1665 // # fallthrough to sinkMBB
1666 BB = copy0MBB;
Nate Begeman74747862005-03-29 22:24:51 +00001667 // Update machine-CFG edges
1668 BB->addSuccessor(sinkMBB);
1669
1670 // sinkMBB:
1671 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1672 // ...
1673 BB = sinkMBB;
1674 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1675 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1676
1677 // FIXME: Select i64?
1678 return Result;
1679 }
Nate Begemana9795f82005-03-24 04:41:43 +00001680
1681 case ISD::Constant:
1682 switch (N.getValueType()) {
1683 default: assert(0 && "Cannot use constants of this type!");
1684 case MVT::i1:
1685 BuildMI(BB, PPC::LI, 1, Result)
1686 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
1687 break;
1688 case MVT::i32:
1689 {
1690 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
1691 if (v < 32768 && v >= -32768) {
1692 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
1693 } else {
Nate Begeman5e966612005-03-24 06:28:42 +00001694 Tmp1 = MakeReg(MVT::i32);
1695 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
1696 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
Nate Begemana9795f82005-03-24 04:41:43 +00001697 }
1698 }
1699 }
1700 return Result;
1701 }
1702
1703 return 0;
1704}
1705
1706void ISel::Select(SDOperand N) {
1707 unsigned Tmp1, Tmp2, Opc;
1708 unsigned opcode = N.getOpcode();
1709
1710 if (!ExprMap.insert(std::make_pair(N, 1)).second)
1711 return; // Already selected.
1712
1713 SDNode *Node = N.Val;
1714
1715 switch (Node->getOpcode()) {
1716 default:
1717 Node->dump(); std::cerr << "\n";
1718 assert(0 && "Node not handled yet!");
1719 case ISD::EntryToken: return; // Noop
1720 case ISD::TokenFactor:
1721 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1722 Select(Node->getOperand(i));
1723 return;
1724 case ISD::ADJCALLSTACKDOWN:
1725 case ISD::ADJCALLSTACKUP:
1726 Select(N.getOperand(0));
1727 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1728 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? PPC::ADJCALLSTACKDOWN :
1729 PPC::ADJCALLSTACKUP;
1730 BuildMI(BB, Opc, 1).addImm(Tmp1);
1731 return;
1732 case ISD::BR: {
1733 MachineBasicBlock *Dest =
1734 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
Nate Begemana9795f82005-03-24 04:41:43 +00001735 Select(N.getOperand(0));
1736 BuildMI(BB, PPC::B, 1).addMBB(Dest);
1737 return;
1738 }
1739 case ISD::BRCOND:
1740 SelectBranchCC(N);
1741 return;
1742 case ISD::CopyToReg:
1743 Select(N.getOperand(0));
1744 Tmp1 = SelectExpr(N.getOperand(1));
1745 Tmp2 = cast<RegSDNode>(N)->getReg();
1746
1747 if (Tmp1 != Tmp2) {
1748 if (N.getOperand(1).getValueType() == MVT::f64 ||
1749 N.getOperand(1).getValueType() == MVT::f32)
1750 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
1751 else
1752 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1753 }
1754 return;
1755 case ISD::ImplicitDef:
1756 Select(N.getOperand(0));
1757 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
1758 return;
1759 case ISD::RET:
1760 switch (N.getNumOperands()) {
1761 default:
1762 assert(0 && "Unknown return instruction!");
1763 case 3:
1764 assert(N.getOperand(1).getValueType() == MVT::i32 &&
1765 N.getOperand(2).getValueType() == MVT::i32 &&
1766 "Unknown two-register value!");
1767 Select(N.getOperand(0));
1768 Tmp1 = SelectExpr(N.getOperand(1));
1769 Tmp2 = SelectExpr(N.getOperand(2));
Nate Begeman27523a12005-04-02 00:42:16 +00001770 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2);
1771 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00001772 break;
1773 case 2:
1774 Select(N.getOperand(0));
1775 Tmp1 = SelectExpr(N.getOperand(1));
1776 switch (N.getOperand(1).getValueType()) {
1777 default:
1778 assert(0 && "Unknown return type!");
1779 case MVT::f64:
1780 case MVT::f32:
1781 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
1782 break;
1783 case MVT::i32:
1784 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
1785 break;
1786 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001787 case 1:
1788 Select(N.getOperand(0));
1789 break;
Nate Begemana9795f82005-03-24 04:41:43 +00001790 }
1791 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
1792 return;
Nate Begemana9795f82005-03-24 04:41:43 +00001793 case ISD::TRUNCSTORE:
1794 case ISD::STORE:
1795 {
1796 SDOperand Chain = N.getOperand(0);
1797 SDOperand Value = N.getOperand(1);
1798 SDOperand Address = N.getOperand(2);
1799 Select(Chain);
1800
1801 Tmp1 = SelectExpr(Value); //value
1802
1803 if (opcode == ISD::STORE) {
1804 switch(Value.getValueType()) {
1805 default: assert(0 && "unknown Type in store");
1806 case MVT::i32: Opc = PPC::STW; break;
1807 case MVT::f64: Opc = PPC::STFD; break;
1808 case MVT::f32: Opc = PPC::STFS; break;
1809 }
1810 } else { //ISD::TRUNCSTORE
1811 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1812 default: assert(0 && "unknown Type in store");
1813 case MVT::i1: //FIXME: DAG does not promote this load
1814 case MVT::i8: Opc = PPC::STB; break;
1815 case MVT::i16: Opc = PPC::STH; break;
1816 }
1817 }
1818
Nate Begemana7e11a42005-04-01 05:57:17 +00001819 if(Address.getOpcode() == ISD::FrameIndex)
Nate Begemana9795f82005-03-24 04:41:43 +00001820 {
Nate Begeman58f718c2005-03-30 02:23:08 +00001821 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
1822 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001823 }
1824 else
1825 {
1826 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00001827 bool idx = SelectAddr(Address, Tmp2, offset);
1828 if (idx) {
1829 Opc = IndexedOpForOp(Opc);
1830 BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset);
1831 } else {
1832 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1833 }
Nate Begemana9795f82005-03-24 04:41:43 +00001834 }
1835 return;
1836 }
1837 case ISD::EXTLOAD:
1838 case ISD::SEXTLOAD:
1839 case ISD::ZEXTLOAD:
1840 case ISD::LOAD:
1841 case ISD::CopyFromReg:
1842 case ISD::CALL:
1843 case ISD::DYNAMIC_STACKALLOC:
1844 ExprMap.erase(N);
1845 SelectExpr(N);
1846 return;
1847 }
1848 assert(0 && "Should not be reached!");
1849}
1850
1851
1852/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
1853/// into a machine code representation using pattern matching and a machine
1854/// description file.
1855///
1856FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) {
1857 return new ISel(TM);
Chris Lattner246fa632005-03-24 06:16:18 +00001858}
1859