blob: dd1a4f331bf2fa916d239cc034e798010c522389 [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.
Nate Begemanc8c5c8f2005-04-05 04:32:16 +0000670 //ilist<MachineBasicBlock>::iterator It = BB;
671 //MachineBasicBlock *Fallthrough = ++It;
Nate Begeman439b4442005-04-05 04:22:58 +0000672
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 Begemanc8c5c8f2005-04-05 04:32:16 +0000675 // FIXME: Use this once we have something approximating two-way branches
676 // We cannot currently use this in case the ISel hands us something like
677 // BRcc MBBx
678 // BR MBBy
679 // since the fallthrough basic block for the conditional branch does not start
680 // with the unconditional branch (it is skipped over).
681 //BuildMI(BB, PPC::COND_BRANCH, 4).addReg(PPC::CR0).addImm(Opc)
682 // .addMBB(Dest).addMBB(Fallthrough);
683 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(Dest);
Nate Begemana9795f82005-03-24 04:41:43 +0000684 return;
685}
686
687unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
688{
689 unsigned Tmp1, Tmp2, Tmp3;
690 unsigned Opc = 0;
691 SDNode *Node = N.Val;
692 MVT::ValueType DestType = N.getValueType();
693 unsigned opcode = N.getOpcode();
694
695 switch (opcode) {
696 default:
697 Node->dump();
698 assert(0 && "Node not handled!\n");
699
Nate Begeman23afcfb2005-03-29 22:48:55 +0000700 case ISD::SELECT: {
Nate Begeman3e897162005-03-31 23:55:40 +0000701 // Attempt to generate FSEL. We can do this whenever we have an FP result,
702 // and an FP comparison in the SetCC node.
703 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val);
704 if (SetCC && N.getOperand(0).getOpcode() == ISD::SETCC &&
705 !MVT::isInteger(SetCC->getOperand(0).getValueType()) &&
706 SetCC->getCondition() != ISD::SETEQ &&
707 SetCC->getCondition() != ISD::SETNE) {
708 MVT::ValueType VT = SetCC->getOperand(0).getValueType();
709 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
710 unsigned TV = SelectExpr(N.getOperand(1)); // Use if TRUE
711 unsigned FV = SelectExpr(N.getOperand(2)); // Use if FALSE
712
713 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1));
714 if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) {
715 switch(SetCC->getCondition()) {
716 default: assert(0 && "Invalid FSEL condition"); abort();
717 case ISD::SETULT:
718 case ISD::SETLT:
719 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(FV).addReg(TV);
720 return Result;
721 case ISD::SETUGE:
722 case ISD::SETGE:
723 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV);
724 return Result;
725 case ISD::SETUGT:
726 case ISD::SETGT: {
727 Tmp2 = MakeReg(VT);
728 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
729 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(FV).addReg(TV);
730 return Result;
731 }
732 case ISD::SETULE:
733 case ISD::SETLE: {
734 Tmp2 = MakeReg(VT);
735 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
736 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV);
737 return Result;
738 }
739 }
740 } else {
741 Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS;
742 Tmp2 = SelectExpr(SetCC->getOperand(1));
743 Tmp3 = MakeReg(VT);
744 switch(SetCC->getCondition()) {
745 default: assert(0 && "Invalid FSEL condition"); abort();
746 case ISD::SETULT:
747 case ISD::SETLT:
748 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
749 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
750 return Result;
751 case ISD::SETUGE:
752 case ISD::SETGE:
753 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
754 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
755 return Result;
756 case ISD::SETUGT:
757 case ISD::SETGT:
758 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
759 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
760 return Result;
761 case ISD::SETULE:
762 case ISD::SETLE:
763 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
764 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
765 return Result;
766 }
767 }
768 assert(0 && "Should never get here");
769 return 0;
770 }
771
Nate Begeman31318e42005-04-01 07:21:30 +0000772 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
773 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman6cb2e1b2005-04-01 08:57:43 +0000774 Opc = SelectSetCR0(N.getOperand(0));
Nate Begeman31318e42005-04-01 07:21:30 +0000775
Nate Begeman23afcfb2005-03-29 22:48:55 +0000776 // Create an iterator with which to insert the MBB for copying the false
777 // value and the MBB to hold the PHI instruction for this SetCC.
778 MachineBasicBlock *thisMBB = BB;
779 const BasicBlock *LLVM_BB = BB->getBasicBlock();
780 ilist<MachineBasicBlock>::iterator It = BB;
781 ++It;
782
783 // thisMBB:
784 // ...
785 // TrueVal = ...
786 // cmpTY cr0, r1, r2
787 // bCC copy1MBB
788 // fallthrough --> copy0MBB
Nate Begeman23afcfb2005-03-29 22:48:55 +0000789 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
790 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman6cb2e1b2005-04-01 08:57:43 +0000791 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
Nate Begeman23afcfb2005-03-29 22:48:55 +0000792 MachineFunction *F = BB->getParent();
793 F->getBasicBlockList().insert(It, copy0MBB);
794 F->getBasicBlockList().insert(It, sinkMBB);
795 // Update machine-CFG edges
796 BB->addSuccessor(copy0MBB);
797 BB->addSuccessor(sinkMBB);
798
799 // copy0MBB:
800 // %FalseValue = ...
801 // # fallthrough to sinkMBB
802 BB = copy0MBB;
Nate Begeman23afcfb2005-03-29 22:48:55 +0000803 // Update machine-CFG edges
804 BB->addSuccessor(sinkMBB);
805
806 // sinkMBB:
807 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
808 // ...
809 BB = sinkMBB;
810 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
811 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
812 return Result;
813 }
Nate Begeman27eeb002005-04-02 05:59:34 +0000814
815 case ISD::FNEG:
Nate Begeman93075ec2005-04-04 23:40:36 +0000816 if (!NoExcessFPPrecision &&
817 ISD::ADD == N.getOperand(0).getOpcode() &&
818 N.getOperand(0).Val->hasOneUse() &&
819 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
820 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
Nate Begeman80196b12005-04-05 00:15:08 +0000821 ++FusedFP; // Statistic
Nate Begeman93075ec2005-04-04 23:40:36 +0000822 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
823 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
824 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
825 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
826 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
827 } else if (!NoExcessFPPrecision &&
828 ISD::SUB == N.getOperand(0).getOpcode() &&
829 N.getOperand(0).Val->hasOneUse() &&
830 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
831 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
Nate Begeman80196b12005-04-05 00:15:08 +0000832 ++FusedFP; // Statistic
Nate Begeman93075ec2005-04-04 23:40:36 +0000833 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
834 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
835 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
836 Opc = DestType == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS;
837 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
838 } else if (ISD::FABS == N.getOperand(0).getOpcode()) {
Nate Begeman27eeb002005-04-02 05:59:34 +0000839 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
840 BuildMI(BB, PPC::FNABS, 1, Result).addReg(Tmp1);
841 } else {
842 Tmp1 = SelectExpr(N.getOperand(0));
843 BuildMI(BB, PPC::FNEG, 1, Result).addReg(Tmp1);
844 }
845 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000846
Nate Begeman27eeb002005-04-02 05:59:34 +0000847 case ISD::FABS:
848 Tmp1 = SelectExpr(N.getOperand(0));
849 BuildMI(BB, PPC::FABS, 1, Result).addReg(Tmp1);
850 return Result;
851
Nate Begemana9795f82005-03-24 04:41:43 +0000852 case ISD::FP_ROUND:
853 assert (DestType == MVT::f32 &&
854 N.getOperand(0).getValueType() == MVT::f64 &&
855 "only f64 to f32 conversion supported here");
856 Tmp1 = SelectExpr(N.getOperand(0));
857 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
858 return Result;
859
860 case ISD::FP_EXTEND:
861 assert (DestType == MVT::f64 &&
862 N.getOperand(0).getValueType() == MVT::f32 &&
863 "only f32 to f64 conversion supported here");
864 Tmp1 = SelectExpr(N.getOperand(0));
865 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
866 return Result;
867
868 case ISD::CopyFromReg:
Nate Begemanf2622612005-03-26 02:17:46 +0000869 if (Result == 1)
870 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
871 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
872 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
873 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000874
Nate Begeman6d369cc2005-04-01 01:08:07 +0000875 case ISD::ConstantFP: {
Nate Begeman6d369cc2005-04-01 01:08:07 +0000876 ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
Nate Begeman6b559972005-04-01 02:59:27 +0000877 Result = getConstDouble(CN->getValue(), Result);
Nate Begeman6d369cc2005-04-01 01:08:07 +0000878 return Result;
879 }
Nate Begemana9795f82005-03-24 04:41:43 +0000880
Nate Begemana9795f82005-03-24 04:41:43 +0000881 case ISD::ADD:
Nate Begeman93075ec2005-04-04 23:40:36 +0000882 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
883 N.getOperand(0).Val->hasOneUse()) {
884 ++FusedFP; // Statistic
885 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
886 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
887 Tmp3 = SelectExpr(N.getOperand(1));
888 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
889 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
890 return Result;
891 }
892 Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS;
893 Tmp1 = SelectExpr(N.getOperand(0));
894 Tmp2 = SelectExpr(N.getOperand(1));
895 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
896 return Result;
897
Nate Begemana9795f82005-03-24 04:41:43 +0000898 case ISD::SUB:
Nate Begeman93075ec2005-04-04 23:40:36 +0000899 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
900 N.getOperand(0).Val->hasOneUse()) {
901 ++FusedFP; // Statistic
902 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
903 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
904 Tmp3 = SelectExpr(N.getOperand(1));
905 Opc = DestType == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS;
906 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
907 return Result;
908 }
909 Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS;
910 Tmp1 = SelectExpr(N.getOperand(0));
911 Tmp2 = SelectExpr(N.getOperand(1));
912 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
913 return Result;
914
915 case ISD::MUL:
Nate Begemana9795f82005-03-24 04:41:43 +0000916 case ISD::SDIV:
917 switch( opcode ) {
918 case ISD::MUL: Opc = DestType == MVT::f64 ? PPC::FMUL : PPC::FMULS; break;
Nate Begemana9795f82005-03-24 04:41:43 +0000919 case ISD::SDIV: Opc = DestType == MVT::f64 ? PPC::FDIV : PPC::FDIVS; break;
920 };
Nate Begemana9795f82005-03-24 04:41:43 +0000921 Tmp1 = SelectExpr(N.getOperand(0));
922 Tmp2 = SelectExpr(N.getOperand(1));
923 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
924 return Result;
925
Nate Begemana9795f82005-03-24 04:41:43 +0000926 case ISD::UINT_TO_FP:
Nate Begemanfdcf3412005-03-30 19:38:35 +0000927 case ISD::SINT_TO_FP: {
928 assert (N.getOperand(0).getValueType() == MVT::i32
929 && "int to float must operate on i32");
930 bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
931 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
932 Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into
933 Tmp3 = MakeReg(MVT::i32); // temp reg to hold the conversion constant
934 unsigned ConstF = MakeReg(MVT::f64); // temp reg to hold the fp constant
935
936 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
937 MachineConstantPool *CP = BB->getParent()->getConstantPool();
938
939 // FIXME: pull this FP constant generation stuff out into something like
940 // the simple ISel's getReg.
941 if (IsUnsigned) {
942 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000000p52);
943 unsigned CPI = CP->getConstantPoolIndex(CFP);
944 // Load constant fp value
945 unsigned Tmp4 = MakeReg(MVT::i32);
946 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg())
947 .addConstantPoolIndex(CPI);
948 BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
949 // Store the hi & low halves of the fp value, currently in int regs
950 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
951 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
952 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp1), FrameIdx, 4);
953 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
954 // Generate the return value with a subtract
955 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
956 } else {
957 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000008p52);
958 unsigned CPI = CP->getConstantPoolIndex(CFP);
959 // Load constant fp value
960 unsigned Tmp4 = MakeReg(MVT::i32);
961 unsigned TmpL = MakeReg(MVT::i32);
962 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg())
963 .addConstantPoolIndex(CPI);
964 BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
965 // Store the hi & low halves of the fp value, currently in int regs
966 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
967 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
968 BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
969 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
970 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
971 // Generate the return value with a subtract
972 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
973 }
974 return Result;
975 }
Nate Begemana9795f82005-03-24 04:41:43 +0000976 }
Nate Begeman6b559972005-04-01 02:59:27 +0000977 assert(0 && "Should never get here");
Nate Begemana9795f82005-03-24 04:41:43 +0000978 return 0;
979}
980
981unsigned ISel::SelectExpr(SDOperand N) {
982 unsigned Result;
983 unsigned Tmp1, Tmp2, Tmp3;
984 unsigned Opc = 0;
985 unsigned opcode = N.getOpcode();
986
987 SDNode *Node = N.Val;
988 MVT::ValueType DestType = N.getValueType();
989
990 unsigned &Reg = ExprMap[N];
991 if (Reg) return Reg;
992
Nate Begeman27eeb002005-04-02 05:59:34 +0000993 switch (N.getOpcode()) {
994 default:
Nate Begemana9795f82005-03-24 04:41:43 +0000995 Reg = Result = (N.getValueType() != MVT::Other) ?
Nate Begeman27eeb002005-04-02 05:59:34 +0000996 MakeReg(N.getValueType()) : 1;
997 break;
998 case ISD::CALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000999 // If this is a call instruction, make sure to prepare ALL of the result
1000 // values as well as the chain.
Nate Begeman27eeb002005-04-02 05:59:34 +00001001 if (Node->getNumValues() == 1)
1002 Reg = Result = 1; // Void call, just a chain.
1003 else {
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001004 Result = MakeReg(Node->getValueType(0));
1005 ExprMap[N.getValue(0)] = Result;
Nate Begeman27eeb002005-04-02 05:59:34 +00001006 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001007 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Nate Begeman27eeb002005-04-02 05:59:34 +00001008 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001009 }
Nate Begeman27eeb002005-04-02 05:59:34 +00001010 break;
1011 case ISD::ADD_PARTS:
1012 case ISD::SUB_PARTS:
1013 case ISD::SHL_PARTS:
1014 case ISD::SRL_PARTS:
1015 case ISD::SRA_PARTS:
1016 Result = MakeReg(Node->getValueType(0));
1017 ExprMap[N.getValue(0)] = Result;
1018 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
1019 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1020 break;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001021 }
1022
Nate Begemane5846682005-04-04 06:52:38 +00001023 if (ISD::CopyFromReg == opcode)
1024 DestType = N.getValue(0).getValueType();
1025
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001026 if (DestType == MVT::f64 || DestType == MVT::f32)
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001027 if (ISD::LOAD != opcode && ISD::EXTLOAD != opcode && ISD::UNDEF != opcode)
Nate Begeman74d73452005-03-31 00:15:26 +00001028 return SelectExprFP(N, Result);
Nate Begemana9795f82005-03-24 04:41:43 +00001029
1030 switch (opcode) {
1031 default:
1032 Node->dump();
1033 assert(0 && "Node not handled!\n");
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001034 case ISD::UNDEF:
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001035 BuildMI(BB, PPC::IMPLICIT_DEF, 0, Result);
1036 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001037 case ISD::DYNAMIC_STACKALLOC:
Nate Begeman5e966612005-03-24 06:28:42 +00001038 // Generate both result values. FIXME: Need a better commment here?
1039 if (Result != 1)
1040 ExprMap[N.getValue(1)] = 1;
1041 else
1042 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1043
1044 // FIXME: We are currently ignoring the requested alignment for handling
1045 // greater than the stack alignment. This will need to be revisited at some
1046 // point. Align = N.getOperand(2);
1047 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1048 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1049 std::cerr << "Cannot allocate stack object with greater alignment than"
1050 << " the stack alignment yet!";
1051 abort();
1052 }
1053 Select(N.getOperand(0));
1054 Tmp1 = SelectExpr(N.getOperand(1));
1055 // Subtract size from stack pointer, thereby allocating some space.
1056 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
1057 // Put a pointer to the space into the result register by copying the SP
1058 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
1059 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001060
1061 case ISD::ConstantPool:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001062 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1063 Tmp2 = MakeReg(MVT::i32);
1064 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp2).addReg(getGlobalBaseReg())
1065 .addConstantPoolIndex(Tmp1);
1066 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
1067 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001068
1069 case ISD::FrameIndex:
Nate Begemanf3d08f32005-03-29 00:03:27 +00001070 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Nate Begeman58f718c2005-03-30 02:23:08 +00001071 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
Nate Begemanf3d08f32005-03-29 00:03:27 +00001072 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001073
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001074 case ISD::GlobalAddress: {
1075 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanca12a2b2005-03-28 22:28:37 +00001076 Tmp1 = MakeReg(MVT::i32);
Nate Begemanc7b09f12005-03-25 08:34:25 +00001077 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1078 .addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001079 if (GV->hasWeakLinkage() || GV->isExternal()) {
1080 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
1081 } else {
1082 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
1083 }
1084 return Result;
1085 }
1086
Nate Begeman5e966612005-03-24 06:28:42 +00001087 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +00001088 case ISD::EXTLOAD:
1089 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001090 case ISD::SEXTLOAD: {
Nate Begeman9db505c2005-03-28 19:36:43 +00001091 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
1092 Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType();
Nate Begeman74d73452005-03-31 00:15:26 +00001093 bool sext = (ISD::SEXTLOAD == opcode);
1094 bool byte = (MVT::i8 == TypeBeingLoaded);
1095
Nate Begeman5e966612005-03-24 06:28:42 +00001096 // Make sure we generate both values.
1097 if (Result != 1)
1098 ExprMap[N.getValue(1)] = 1; // Generate the token
1099 else
1100 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1101
1102 SDOperand Chain = N.getOperand(0);
1103 SDOperand Address = N.getOperand(1);
1104 Select(Chain);
1105
Nate Begeman9db505c2005-03-28 19:36:43 +00001106 switch (TypeBeingLoaded) {
Nate Begeman74d73452005-03-31 00:15:26 +00001107 default: Node->dump(); assert(0 && "Cannot load this type!");
Nate Begeman9db505c2005-03-28 19:36:43 +00001108 case MVT::i1: Opc = PPC::LBZ; break;
1109 case MVT::i8: Opc = PPC::LBZ; break;
1110 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
1111 case MVT::i32: Opc = PPC::LWZ; break;
Nate Begeman74d73452005-03-31 00:15:26 +00001112 case MVT::f32: Opc = PPC::LFS; break;
1113 case MVT::f64: Opc = PPC::LFD; break;
Nate Begeman5e966612005-03-24 06:28:42 +00001114 }
1115
Nate Begeman74d73452005-03-31 00:15:26 +00001116 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1117 Tmp1 = MakeReg(MVT::i32);
1118 int CPI = CP->getIndex();
1119 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1120 .addConstantPoolIndex(CPI);
1121 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001122 }
Nate Begeman74d73452005-03-31 00:15:26 +00001123 else if(Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman58f718c2005-03-30 02:23:08 +00001124 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
1125 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
Nate Begeman5e966612005-03-24 06:28:42 +00001126 } else {
1127 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00001128 bool idx = SelectAddr(Address, Tmp1, offset);
1129 if (idx) {
1130 Opc = IndexedOpForOp(Opc);
1131 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset);
1132 } else {
1133 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
1134 }
Nate Begeman5e966612005-03-24 06:28:42 +00001135 }
1136 return Result;
1137 }
1138
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001139 case ISD::CALL: {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001140 unsigned GPR_idx = 0, FPR_idx = 0;
1141 static const unsigned GPR[] = {
1142 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1143 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1144 };
1145 static const unsigned FPR[] = {
1146 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1147 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1148 };
1149
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001150 // Lower the chain for this call.
1151 Select(N.getOperand(0));
1152 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
Nate Begeman74d73452005-03-31 00:15:26 +00001153
Nate Begemand860aa62005-04-04 22:17:48 +00001154 MachineInstr *CallMI;
1155 // Emit the correct call instruction based on the type of symbol called.
1156 if (GlobalAddressSDNode *GASD =
1157 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
1158 CallMI = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(),
1159 true);
1160 } else if (ExternalSymbolSDNode *ESSDN =
1161 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
1162 CallMI = BuildMI(PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(),
1163 true);
1164 } else {
1165 Tmp1 = SelectExpr(N.getOperand(1));
1166 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
1167 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
1168 CallMI = BuildMI(PPC::CALLindirect, 3).addImm(20).addImm(0)
1169 .addReg(PPC::R12);
1170 }
1171
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001172 // Load the register args to virtual regs
1173 std::vector<unsigned> ArgVR;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001174 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001175 ArgVR.push_back(SelectExpr(N.getOperand(i)));
1176
1177 // Copy the virtual registers into the appropriate argument register
1178 for(int i = 0, e = ArgVR.size(); i < e; ++i) {
1179 switch(N.getOperand(i+2).getValueType()) {
1180 default: Node->dump(); assert(0 && "Unknown value type for call");
1181 case MVT::i1:
1182 case MVT::i8:
1183 case MVT::i16:
1184 case MVT::i32:
1185 assert(GPR_idx < 8 && "Too many int args");
Nate Begemand860aa62005-04-04 22:17:48 +00001186 if (N.getOperand(i+2).getOpcode() != ISD::UNDEF) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001187 BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001188 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1189 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001190 ++GPR_idx;
1191 break;
1192 case MVT::f64:
1193 case MVT::f32:
1194 assert(FPR_idx < 13 && "Too many fp args");
1195 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001196 CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001197 ++FPR_idx;
1198 break;
1199 }
1200 }
Nate Begemand860aa62005-04-04 22:17:48 +00001201
1202 // Put the call instruction in the correct place in the MachineBasicBlock
1203 BB->push_back(CallMI);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001204
1205 switch (Node->getValueType(0)) {
1206 default: assert(0 && "Unknown value type for call result!");
1207 case MVT::Other: return 1;
1208 case MVT::i1:
1209 case MVT::i8:
1210 case MVT::i16:
1211 case MVT::i32:
Nate Begemane5846682005-04-04 06:52:38 +00001212 if (Node->getValueType(1) == MVT::i32) {
1213 BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R3).addReg(PPC::R3);
1214 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R4).addReg(PPC::R4);
1215 } else {
1216 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
1217 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001218 break;
1219 case MVT::f32:
1220 case MVT::f64:
1221 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1222 break;
1223 }
1224 return Result+N.ResNo;
1225 }
Nate Begemana9795f82005-03-24 04:41:43 +00001226
1227 case ISD::SIGN_EXTEND:
1228 case ISD::SIGN_EXTEND_INREG:
1229 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman9db505c2005-03-28 19:36:43 +00001230 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1231 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
1232 case MVT::i16:
1233 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
1234 break;
1235 case MVT::i8:
1236 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
1237 break;
Nate Begeman74747862005-03-29 22:24:51 +00001238 case MVT::i1:
1239 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1240 break;
Nate Begeman9db505c2005-03-28 19:36:43 +00001241 }
Nate Begemana9795f82005-03-24 04:41:43 +00001242 return Result;
1243
1244 case ISD::ZERO_EXTEND_INREG:
1245 Tmp1 = SelectExpr(N.getOperand(0));
1246 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
Nate Begeman9db505c2005-03-28 19:36:43 +00001247 default: Node->dump(); assert(0 && "Unhandled ZERO_EXTEND type"); break;
Nate Begemana9795f82005-03-24 04:41:43 +00001248 case MVT::i16: Tmp2 = 16; break;
1249 case MVT::i8: Tmp2 = 24; break;
1250 case MVT::i1: Tmp2 = 31; break;
1251 }
Nate Begeman33162522005-03-29 21:54:38 +00001252 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(0).addImm(Tmp2)
1253 .addImm(31);
Nate Begemana9795f82005-03-24 04:41:43 +00001254 return Result;
1255
Nate Begemana9795f82005-03-24 04:41:43 +00001256 case ISD::CopyFromReg:
1257 if (Result == 1)
1258 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1259 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1260 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1261 return Result;
1262
1263 case ISD::SHL:
Nate Begeman5e966612005-03-24 06:28:42 +00001264 Tmp1 = SelectExpr(N.getOperand(0));
1265 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1266 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001267 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
Nate Begeman5e966612005-03-24 06:28:42 +00001268 .addImm(31-Tmp2);
1269 } else {
1270 Tmp2 = SelectExpr(N.getOperand(1));
1271 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1272 }
1273 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001274
Nate Begeman5e966612005-03-24 06:28:42 +00001275 case ISD::SRL:
1276 Tmp1 = SelectExpr(N.getOperand(0));
1277 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1278 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001279 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2)
Nate Begeman5e966612005-03-24 06:28:42 +00001280 .addImm(Tmp2).addImm(31);
1281 } else {
1282 Tmp2 = SelectExpr(N.getOperand(1));
1283 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1284 }
1285 return Result;
1286
1287 case ISD::SRA:
1288 Tmp1 = SelectExpr(N.getOperand(0));
1289 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1290 Tmp2 = CN->getValue() & 0x1F;
1291 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1292 } else {
1293 Tmp2 = SelectExpr(N.getOperand(1));
1294 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1295 }
1296 return Result;
1297
Nate Begemana9795f82005-03-24 04:41:43 +00001298 case ISD::ADD:
1299 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1300 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001301 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemana9795f82005-03-24 04:41:43 +00001302 default: assert(0 && "unhandled result code");
1303 case 0: // No immediate
1304 Tmp2 = SelectExpr(N.getOperand(1));
1305 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1306 break;
1307 case 1: // Low immediate
1308 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1309 break;
1310 case 2: // Shifted immediate
1311 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1312 break;
1313 }
1314 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001315
Nate Begemana9795f82005-03-24 04:41:43 +00001316 case ISD::AND:
1317 case ISD::OR:
Nate Begemana9795f82005-03-24 04:41:43 +00001318 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001319 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemana9795f82005-03-24 04:41:43 +00001320 default: assert(0 && "unhandled result code");
1321 case 0: // No immediate
1322 Tmp2 = SelectExpr(N.getOperand(1));
1323 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +00001324 case ISD::AND: Opc = PPC::AND; break;
1325 case ISD::OR: Opc = PPC::OR; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001326 }
Nate Begeman5e966612005-03-24 06:28:42 +00001327 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001328 break;
1329 case 1: // Low immediate
1330 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +00001331 case ISD::AND: Opc = PPC::ANDIo; break;
1332 case ISD::OR: Opc = PPC::ORI; 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 case 2: // Shifted immediate
1337 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +00001338 case ISD::AND: Opc = PPC::ANDISo; break;
1339 case ISD::OR: Opc = PPC::ORIS; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001340 }
Nate Begeman5e966612005-03-24 06:28:42 +00001341 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001342 break;
1343 }
1344 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001345
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001346 case ISD::XOR: {
1347 // Check for EQV: xor, (xor a, -1), b
1348 if (N.getOperand(0).getOpcode() == ISD::XOR &&
1349 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1350 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) {
1351 ++NotLogic;
1352 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1353 Tmp2 = SelectExpr(N.getOperand(1));
1354 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1355 return Result;
1356 }
1357 // Check for NOT, NOR, and NAND: xor (copy, or, and), -1
1358 if (N.getOperand(1).getOpcode() == ISD::Constant &&
1359 cast<ConstantSDNode>(N.getOperand(1))->isAllOnesValue()) {
1360 ++NotLogic;
1361 switch(N.getOperand(0).getOpcode()) {
1362 case ISD::OR:
1363 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1364 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1365 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1366 break;
1367 case ISD::AND:
1368 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1369 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1370 BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2);
1371 break;
1372 default:
1373 Tmp1 = SelectExpr(N.getOperand(0));
1374 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1375 break;
1376 }
1377 return Result;
1378 }
1379 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001380 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001381 default: assert(0 && "unhandled result code");
1382 case 0: // No immediate
1383 Tmp2 = SelectExpr(N.getOperand(1));
1384 BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1385 break;
1386 case 1: // Low immediate
1387 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1388 break;
1389 case 2: // Shifted immediate
1390 BuildMI(BB, PPC::XORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
1391 break;
1392 }
1393 return Result;
1394 }
1395
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001396 case ISD::SUB:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001397 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman439b4442005-04-05 04:22:58 +00001398 if (1 == getImmediateForOpcode(N.getOperand(0), opcode, Tmp1))
Nate Begeman27523a12005-04-02 00:42:16 +00001399 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1);
1400 else {
1401 Tmp1 = SelectExpr(N.getOperand(0));
1402 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
1403 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001404 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001405
Nate Begeman5e966612005-03-24 06:28:42 +00001406 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001407 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001408 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
Nate Begeman307e7442005-03-26 01:28:53 +00001409 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1410 else {
1411 Tmp2 = SelectExpr(N.getOperand(1));
1412 BuildMI(BB, PPC::MULLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1413 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001414 return Result;
1415
Nate Begemanf3d08f32005-03-29 00:03:27 +00001416 case ISD::SDIV:
1417 case ISD::UDIV:
Nate Begeman439b4442005-04-05 04:22:58 +00001418 if (3 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp3)) {
Nate Begeman80196b12005-04-05 00:15:08 +00001419 Tmp1 = MakeReg(MVT::i32);
1420 Tmp2 = SelectExpr(N.getOperand(0));
1421 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
1422 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp1);
1423 return Result;
1424 }
Nate Begemanf3d08f32005-03-29 00:03:27 +00001425 Tmp1 = SelectExpr(N.getOperand(0));
1426 Tmp2 = SelectExpr(N.getOperand(1));
1427 Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW;
1428 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1429 return Result;
1430
1431 case ISD::UREM:
1432 case ISD::SREM: {
Nate Begemanf3d08f32005-03-29 00:03:27 +00001433 Tmp1 = SelectExpr(N.getOperand(0));
1434 Tmp2 = SelectExpr(N.getOperand(1));
1435 Tmp3 = MakeReg(MVT::i32);
1436 unsigned Tmp4 = MakeReg(MVT::i32);
1437 Opc = (ISD::UREM == opcode) ? PPC::DIVWU : PPC::DIVW;
1438 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1439 BuildMI(BB, PPC::MULLW, 2, Tmp4).addReg(Tmp3).addReg(Tmp2);
1440 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp4).addReg(Tmp1);
1441 return Result;
1442 }
1443
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001444 case ISD::ADD_PARTS:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001445 case ISD::SUB_PARTS: {
1446 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1447 "Not an i64 add/sub!");
1448 // Emit all of the operands.
1449 std::vector<unsigned> InVals;
1450 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
1451 InVals.push_back(SelectExpr(N.getOperand(i)));
1452 if (N.getOpcode() == ISD::ADD_PARTS) {
Nate Begeman27eeb002005-04-02 05:59:34 +00001453 BuildMI(BB, PPC::ADDC, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
1454 BuildMI(BB, PPC::ADDE, 2, Result+1).addReg(InVals[1]).addReg(InVals[3]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001455 } else {
Nate Begeman27eeb002005-04-02 05:59:34 +00001456 BuildMI(BB, PPC::SUBFC, 2, Result).addReg(InVals[2]).addReg(InVals[0]);
1457 BuildMI(BB, PPC::SUBFE, 2, Result+1).addReg(InVals[3]).addReg(InVals[1]);
1458 }
1459 return Result+N.ResNo;
1460 }
1461
1462 case ISD::SHL_PARTS:
1463 case ISD::SRA_PARTS:
1464 case ISD::SRL_PARTS: {
1465 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
1466 "Not an i64 shift!");
1467 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
1468 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
1469 unsigned SHReg = SelectExpr(N.getOperand(2));
1470 Tmp1 = MakeReg(MVT::i32);
1471 Tmp2 = MakeReg(MVT::i32);
1472 Tmp3 = MakeReg(MVT::i32);
1473 unsigned Tmp4 = MakeReg(MVT::i32);
1474 unsigned Tmp5 = MakeReg(MVT::i32);
1475 unsigned Tmp6 = MakeReg(MVT::i32);
1476 BuildMI(BB, PPC::SUBFIC, 2, Tmp1).addReg(SHReg).addSImm(32);
1477 if (ISD::SHL_PARTS == opcode) {
1478 BuildMI(BB, PPC::SLW, 2, Tmp2).addReg(ShiftOpHi).addReg(SHReg);
1479 BuildMI(BB, PPC::SRW, 2, Tmp3).addReg(ShiftOpLo).addReg(Tmp1);
1480 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1481 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
Nate Begemanfa554702005-04-03 22:13:27 +00001482 BuildMI(BB, PPC::SLW, 2, Tmp6).addReg(ShiftOpLo).addReg(Tmp5);
Nate Begeman27eeb002005-04-02 05:59:34 +00001483 BuildMI(BB, PPC::OR, 2, Result+1).addReg(Tmp4).addReg(Tmp6);
1484 BuildMI(BB, PPC::SLW, 2, Result).addReg(ShiftOpLo).addReg(SHReg);
1485 } else if (ISD::SRL_PARTS == opcode) {
1486 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1487 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1488 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1489 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
1490 BuildMI(BB, PPC::SRW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1491 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp4).addReg(Tmp6);
1492 BuildMI(BB, PPC::SRW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1493 } else {
1494 MachineBasicBlock *TmpMBB = new MachineBasicBlock(BB->getBasicBlock());
1495 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1496 MachineBasicBlock *OldMBB = BB;
1497 MachineFunction *F = BB->getParent();
1498 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1499 F->getBasicBlockList().insert(It, TmpMBB);
1500 F->getBasicBlockList().insert(It, PhiMBB);
1501 BB->addSuccessor(TmpMBB);
1502 BB->addSuccessor(PhiMBB);
1503 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1504 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1505 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1506 BuildMI(BB, PPC::ADDICo, 2, Tmp5).addReg(SHReg).addSImm(-32);
1507 BuildMI(BB, PPC::SRAW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1508 BuildMI(BB, PPC::SRAW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1509 BuildMI(BB, PPC::BLE, 2).addReg(PPC::CR0).addMBB(PhiMBB);
1510 // Select correct least significant half if the shift amount > 32
1511 BB = TmpMBB;
1512 unsigned Tmp7 = MakeReg(MVT::i32);
1513 BuildMI(BB, PPC::OR, 2, Tmp7).addReg(Tmp6).addReg(Tmp6);
1514 TmpMBB->addSuccessor(PhiMBB);
1515 BB = PhiMBB;
1516 BuildMI(BB, PPC::PHI, 4, Result).addReg(Tmp4).addMBB(OldMBB)
1517 .addReg(Tmp7).addMBB(TmpMBB);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001518 }
1519 return Result+N.ResNo;
1520 }
1521
Nate Begemana9795f82005-03-24 04:41:43 +00001522 case ISD::FP_TO_UINT:
Nate Begeman6b559972005-04-01 02:59:27 +00001523 case ISD::FP_TO_SINT: {
1524 bool U = (ISD::FP_TO_UINT == opcode);
1525 Tmp1 = SelectExpr(N.getOperand(0));
1526 if (!U) {
1527 Tmp2 = MakeReg(MVT::f64);
1528 BuildMI(BB, PPC::FCTIWZ, 1, Tmp2).addReg(Tmp1);
1529 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1530 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx);
1531 addFrameReference(BuildMI(BB, PPC::LWZ, 2, Result), FrameIdx, 4);
1532 return Result;
1533 } else {
1534 unsigned Zero = getConstDouble(0.0);
1535 unsigned MaxInt = getConstDouble((1LL << 32) - 1);
1536 unsigned Border = getConstDouble(1LL << 31);
1537 unsigned UseZero = MakeReg(MVT::f64);
1538 unsigned UseMaxInt = MakeReg(MVT::f64);
1539 unsigned UseChoice = MakeReg(MVT::f64);
1540 unsigned TmpReg = MakeReg(MVT::f64);
1541 unsigned TmpReg2 = MakeReg(MVT::f64);
1542 unsigned ConvReg = MakeReg(MVT::f64);
1543 unsigned IntTmp = MakeReg(MVT::i32);
1544 unsigned XorReg = MakeReg(MVT::i32);
1545 MachineFunction *F = BB->getParent();
1546 int FrameIdx = F->getFrameInfo()->CreateStackObject(8, 8);
1547 // Update machine-CFG edges
1548 MachineBasicBlock *XorMBB = new MachineBasicBlock(BB->getBasicBlock());
1549 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1550 MachineBasicBlock *OldMBB = BB;
1551 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1552 F->getBasicBlockList().insert(It, XorMBB);
1553 F->getBasicBlockList().insert(It, PhiMBB);
1554 BB->addSuccessor(XorMBB);
1555 BB->addSuccessor(PhiMBB);
1556 // Convert from floating point to unsigned 32-bit value
1557 // Use 0 if incoming value is < 0.0
1558 BuildMI(BB, PPC::FSEL, 3, UseZero).addReg(Tmp1).addReg(Tmp1).addReg(Zero);
1559 // Use 2**32 - 1 if incoming value is >= 2**32
1560 BuildMI(BB, PPC::FSUB, 2, UseMaxInt).addReg(MaxInt).addReg(Tmp1);
1561 BuildMI(BB, PPC::FSEL, 3, UseChoice).addReg(UseMaxInt).addReg(UseZero)
1562 .addReg(MaxInt);
1563 // Subtract 2**31
1564 BuildMI(BB, PPC::FSUB, 2, TmpReg).addReg(UseChoice).addReg(Border);
1565 // Use difference if >= 2**31
1566 BuildMI(BB, PPC::FCMPU, 2, PPC::CR0).addReg(UseChoice).addReg(Border);
1567 BuildMI(BB, PPC::FSEL, 3, TmpReg2).addReg(TmpReg).addReg(TmpReg)
1568 .addReg(UseChoice);
1569 // Convert to integer
1570 BuildMI(BB, PPC::FCTIWZ, 1, ConvReg).addReg(TmpReg2);
1571 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(ConvReg), FrameIdx);
1572 addFrameReference(BuildMI(BB, PPC::LWZ, 2, IntTmp), FrameIdx, 4);
1573 BuildMI(BB, PPC::BLT, 2).addReg(PPC::CR0).addMBB(PhiMBB);
1574 BuildMI(BB, PPC::B, 1).addMBB(XorMBB);
1575
1576 // XorMBB:
1577 // add 2**31 if input was >= 2**31
1578 BB = XorMBB;
1579 BuildMI(BB, PPC::XORIS, 2, XorReg).addReg(IntTmp).addImm(0x8000);
1580 XorMBB->addSuccessor(PhiMBB);
1581
1582 // PhiMBB:
1583 // DestReg = phi [ IntTmp, OldMBB ], [ XorReg, XorMBB ]
1584 BB = PhiMBB;
1585 BuildMI(BB, PPC::PHI, 4, Result).addReg(IntTmp).addMBB(OldMBB)
1586 .addReg(XorReg).addMBB(XorMBB);
1587 return Result;
1588 }
1589 assert(0 && "Should never get here");
1590 return 0;
1591 }
Nate Begemana9795f82005-03-24 04:41:43 +00001592
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001593 case ISD::SETCC:
Nate Begeman33162522005-03-29 21:54:38 +00001594 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
Nate Begemandffcfcc2005-04-01 00:32:34 +00001595 Opc = SelectSetCR0(N);
Nate Begeman33162522005-03-29 21:54:38 +00001596
Nate Begeman31318e42005-04-01 07:21:30 +00001597 unsigned TrueValue = MakeReg(MVT::i32);
1598 BuildMI(BB, PPC::LI, 1, TrueValue).addSImm(1);
1599 unsigned FalseValue = MakeReg(MVT::i32);
1600 BuildMI(BB, PPC::LI, 1, FalseValue).addSImm(0);
1601
Nate Begeman33162522005-03-29 21:54:38 +00001602 // Create an iterator with which to insert the MBB for copying the false
1603 // value and the MBB to hold the PHI instruction for this SetCC.
1604 MachineBasicBlock *thisMBB = BB;
1605 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1606 ilist<MachineBasicBlock>::iterator It = BB;
1607 ++It;
1608
1609 // thisMBB:
1610 // ...
1611 // cmpTY cr0, r1, r2
1612 // %TrueValue = li 1
1613 // bCC sinkMBB
Nate Begeman33162522005-03-29 21:54:38 +00001614 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1615 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1616 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
1617 MachineFunction *F = BB->getParent();
1618 F->getBasicBlockList().insert(It, copy0MBB);
1619 F->getBasicBlockList().insert(It, sinkMBB);
1620 // Update machine-CFG edges
1621 BB->addSuccessor(copy0MBB);
1622 BB->addSuccessor(sinkMBB);
1623
1624 // copy0MBB:
1625 // %FalseValue = li 0
1626 // fallthrough
1627 BB = copy0MBB;
Nate Begeman33162522005-03-29 21:54:38 +00001628 // Update machine-CFG edges
1629 BB->addSuccessor(sinkMBB);
1630
1631 // sinkMBB:
1632 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1633 // ...
1634 BB = sinkMBB;
1635 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1636 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1637 return Result;
1638 }
1639 assert(0 && "Is this legal?");
1640 return 0;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001641
Nate Begeman74747862005-03-29 22:24:51 +00001642 case ISD::SELECT: {
Chris Lattner30710192005-04-01 07:10:02 +00001643 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
1644 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman6cb2e1b2005-04-01 08:57:43 +00001645 Opc = SelectSetCR0(N.getOperand(0));
Chris Lattner30710192005-04-01 07:10:02 +00001646
Nate Begeman74747862005-03-29 22:24:51 +00001647 // Create an iterator with which to insert the MBB for copying the false
1648 // value and the MBB to hold the PHI instruction for this SetCC.
1649 MachineBasicBlock *thisMBB = BB;
1650 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1651 ilist<MachineBasicBlock>::iterator It = BB;
1652 ++It;
1653
1654 // thisMBB:
1655 // ...
1656 // TrueVal = ...
1657 // cmpTY cr0, r1, r2
1658 // bCC copy1MBB
1659 // fallthrough --> copy0MBB
Nate Begeman74747862005-03-29 22:24:51 +00001660 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1661 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman3e897162005-03-31 23:55:40 +00001662 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
Nate Begeman74747862005-03-29 22:24:51 +00001663 MachineFunction *F = BB->getParent();
1664 F->getBasicBlockList().insert(It, copy0MBB);
1665 F->getBasicBlockList().insert(It, sinkMBB);
1666 // Update machine-CFG edges
1667 BB->addSuccessor(copy0MBB);
1668 BB->addSuccessor(sinkMBB);
1669
1670 // copy0MBB:
1671 // %FalseValue = ...
1672 // # fallthrough to sinkMBB
1673 BB = copy0MBB;
Nate Begeman74747862005-03-29 22:24:51 +00001674 // Update machine-CFG edges
1675 BB->addSuccessor(sinkMBB);
1676
1677 // sinkMBB:
1678 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1679 // ...
1680 BB = sinkMBB;
1681 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1682 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1683
1684 // FIXME: Select i64?
1685 return Result;
1686 }
Nate Begemana9795f82005-03-24 04:41:43 +00001687
1688 case ISD::Constant:
1689 switch (N.getValueType()) {
1690 default: assert(0 && "Cannot use constants of this type!");
1691 case MVT::i1:
1692 BuildMI(BB, PPC::LI, 1, Result)
1693 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
1694 break;
1695 case MVT::i32:
1696 {
1697 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
1698 if (v < 32768 && v >= -32768) {
1699 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
1700 } else {
Nate Begeman5e966612005-03-24 06:28:42 +00001701 Tmp1 = MakeReg(MVT::i32);
1702 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
1703 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
Nate Begemana9795f82005-03-24 04:41:43 +00001704 }
1705 }
1706 }
1707 return Result;
1708 }
1709
1710 return 0;
1711}
1712
1713void ISel::Select(SDOperand N) {
1714 unsigned Tmp1, Tmp2, Opc;
1715 unsigned opcode = N.getOpcode();
1716
1717 if (!ExprMap.insert(std::make_pair(N, 1)).second)
1718 return; // Already selected.
1719
1720 SDNode *Node = N.Val;
1721
1722 switch (Node->getOpcode()) {
1723 default:
1724 Node->dump(); std::cerr << "\n";
1725 assert(0 && "Node not handled yet!");
1726 case ISD::EntryToken: return; // Noop
1727 case ISD::TokenFactor:
1728 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1729 Select(Node->getOperand(i));
1730 return;
1731 case ISD::ADJCALLSTACKDOWN:
1732 case ISD::ADJCALLSTACKUP:
1733 Select(N.getOperand(0));
1734 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1735 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? PPC::ADJCALLSTACKDOWN :
1736 PPC::ADJCALLSTACKUP;
1737 BuildMI(BB, Opc, 1).addImm(Tmp1);
1738 return;
1739 case ISD::BR: {
1740 MachineBasicBlock *Dest =
1741 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
Nate Begemana9795f82005-03-24 04:41:43 +00001742 Select(N.getOperand(0));
1743 BuildMI(BB, PPC::B, 1).addMBB(Dest);
1744 return;
1745 }
1746 case ISD::BRCOND:
1747 SelectBranchCC(N);
1748 return;
1749 case ISD::CopyToReg:
1750 Select(N.getOperand(0));
1751 Tmp1 = SelectExpr(N.getOperand(1));
1752 Tmp2 = cast<RegSDNode>(N)->getReg();
1753
1754 if (Tmp1 != Tmp2) {
1755 if (N.getOperand(1).getValueType() == MVT::f64 ||
1756 N.getOperand(1).getValueType() == MVT::f32)
1757 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
1758 else
1759 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1760 }
1761 return;
1762 case ISD::ImplicitDef:
1763 Select(N.getOperand(0));
1764 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
1765 return;
1766 case ISD::RET:
1767 switch (N.getNumOperands()) {
1768 default:
1769 assert(0 && "Unknown return instruction!");
1770 case 3:
1771 assert(N.getOperand(1).getValueType() == MVT::i32 &&
1772 N.getOperand(2).getValueType() == MVT::i32 &&
1773 "Unknown two-register value!");
1774 Select(N.getOperand(0));
1775 Tmp1 = SelectExpr(N.getOperand(1));
1776 Tmp2 = SelectExpr(N.getOperand(2));
Nate Begeman27523a12005-04-02 00:42:16 +00001777 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2);
1778 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00001779 break;
1780 case 2:
1781 Select(N.getOperand(0));
1782 Tmp1 = SelectExpr(N.getOperand(1));
1783 switch (N.getOperand(1).getValueType()) {
1784 default:
1785 assert(0 && "Unknown return type!");
1786 case MVT::f64:
1787 case MVT::f32:
1788 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
1789 break;
1790 case MVT::i32:
1791 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
1792 break;
1793 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001794 case 1:
1795 Select(N.getOperand(0));
1796 break;
Nate Begemana9795f82005-03-24 04:41:43 +00001797 }
1798 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
1799 return;
Nate Begemana9795f82005-03-24 04:41:43 +00001800 case ISD::TRUNCSTORE:
1801 case ISD::STORE:
1802 {
1803 SDOperand Chain = N.getOperand(0);
1804 SDOperand Value = N.getOperand(1);
1805 SDOperand Address = N.getOperand(2);
1806 Select(Chain);
1807
1808 Tmp1 = SelectExpr(Value); //value
1809
1810 if (opcode == ISD::STORE) {
1811 switch(Value.getValueType()) {
1812 default: assert(0 && "unknown Type in store");
1813 case MVT::i32: Opc = PPC::STW; break;
1814 case MVT::f64: Opc = PPC::STFD; break;
1815 case MVT::f32: Opc = PPC::STFS; break;
1816 }
1817 } else { //ISD::TRUNCSTORE
1818 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1819 default: assert(0 && "unknown Type in store");
1820 case MVT::i1: //FIXME: DAG does not promote this load
1821 case MVT::i8: Opc = PPC::STB; break;
1822 case MVT::i16: Opc = PPC::STH; break;
1823 }
1824 }
1825
Nate Begemana7e11a42005-04-01 05:57:17 +00001826 if(Address.getOpcode() == ISD::FrameIndex)
Nate Begemana9795f82005-03-24 04:41:43 +00001827 {
Nate Begeman58f718c2005-03-30 02:23:08 +00001828 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
1829 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001830 }
1831 else
1832 {
1833 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00001834 bool idx = SelectAddr(Address, Tmp2, offset);
1835 if (idx) {
1836 Opc = IndexedOpForOp(Opc);
1837 BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset);
1838 } else {
1839 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1840 }
Nate Begemana9795f82005-03-24 04:41:43 +00001841 }
1842 return;
1843 }
1844 case ISD::EXTLOAD:
1845 case ISD::SEXTLOAD:
1846 case ISD::ZEXTLOAD:
1847 case ISD::LOAD:
1848 case ISD::CopyFromReg:
1849 case ISD::CALL:
1850 case ISD::DYNAMIC_STACKALLOC:
1851 ExprMap.erase(N);
1852 SelectExpr(N);
1853 return;
1854 }
1855 assert(0 && "Should not be reached!");
1856}
1857
1858
1859/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
1860/// into a machine code representation using pattern matching and a machine
1861/// description file.
1862///
1863FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) {
1864 return new ISel(TM);
Chris Lattner246fa632005-03-24 06:16:18 +00001865}
1866