blob: e3803b9f1418197b27d781d1c49796553e325c4b [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"
28#include "llvm/Support/Debug.h"
29#include "llvm/Support/MathExtras.h"
30#include "llvm/ADT/Statistic.h"
31#include <set>
32#include <algorithm>
33using namespace llvm;
34
35//===----------------------------------------------------------------------===//
36// PPC32TargetLowering - PPC32 Implementation of the TargetLowering interface
37namespace {
38 class PPC32TargetLowering : public TargetLowering {
39 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
40 int ReturnAddrIndex; // FrameIndex for return slot.
41 public:
42 PPC32TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
Nate Begemana9795f82005-03-24 04:41:43 +000043 // Set up the register classes.
44 addRegisterClass(MVT::i32, PPC32::GPRCRegisterClass);
Nate Begeman7532e2f2005-03-26 08:25:22 +000045 addRegisterClass(MVT::f32, PPC32::FPRCRegisterClass);
Nate Begemana9795f82005-03-24 04:41:43 +000046 addRegisterClass(MVT::f64, PPC32::FPRCRegisterClass);
47
Nate Begeman74d73452005-03-31 00:15:26 +000048 // PowerPC has no intrinsics for these particular operations
Nate Begeman01d05262005-03-30 01:45:43 +000049 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
50 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
51 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
52
Nate Begeman74d73452005-03-31 00:15:26 +000053 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
54 setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
55 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
56
Nate Begemana9795f82005-03-24 04:41:43 +000057 computeRegisterProperties();
58 }
59
60 /// LowerArguments - This hook must be implemented to indicate how we should
61 /// lower the arguments for the specified function, into the specified DAG.
62 virtual std::vector<SDOperand>
63 LowerArguments(Function &F, SelectionDAG &DAG);
64
65 /// LowerCallTo - This hook lowers an abstract call to a function into an
66 /// actual call.
67 virtual std::pair<SDOperand, SDOperand>
Nate Begeman307e7442005-03-26 01:28:53 +000068 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
69 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Nate Begemana9795f82005-03-24 04:41:43 +000070
71 virtual std::pair<SDOperand, SDOperand>
72 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
73
74 virtual std::pair<SDOperand,SDOperand>
75 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
76 const Type *ArgTy, SelectionDAG &DAG);
77
78 virtual std::pair<SDOperand, SDOperand>
79 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
80 SelectionDAG &DAG);
81 };
82}
83
84
85std::vector<SDOperand>
86PPC32TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
87 //
88 // add beautiful description of PPC stack frame format, or at least some docs
89 //
90 MachineFunction &MF = DAG.getMachineFunction();
91 MachineFrameInfo *MFI = MF.getFrameInfo();
92 MachineBasicBlock& BB = MF.front();
93 std::vector<SDOperand> ArgValues;
94
95 // Due to the rather complicated nature of the PowerPC ABI, rather than a
96 // fixed size array of physical args, for the sake of simplicity let the STL
97 // handle tracking them for us.
98 std::vector<unsigned> argVR, argPR, argOp;
99 unsigned ArgOffset = 24;
100 unsigned GPR_remaining = 8;
101 unsigned FPR_remaining = 13;
102 unsigned GPR_idx = 0, FPR_idx = 0;
103 static const unsigned GPR[] = {
104 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
105 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
106 };
107 static const unsigned FPR[] = {
108 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
109 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
110 };
111
112 // Add DAG nodes to load the arguments... On entry to a function on PPC,
113 // the arguments start at offset 24, although they are likely to be passed
114 // in registers.
115 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
116 SDOperand newroot, argt;
117 unsigned ObjSize;
118 bool needsLoad = false;
119 MVT::ValueType ObjectVT = getValueType(I->getType());
120
121 switch (ObjectVT) {
122 default: assert(0 && "Unhandled argument type!");
123 case MVT::i1:
124 case MVT::i8:
125 case MVT::i16:
126 case MVT::i32:
127 ObjSize = 4;
128 if (GPR_remaining > 0) {
129 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
Nate Begemanf70b5762005-03-28 23:08:54 +0000130 argt = newroot = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32,
131 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000132 if (ObjectVT != MVT::i32)
133 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, newroot);
Nate Begemana9795f82005-03-24 04:41:43 +0000134 } else {
135 needsLoad = true;
136 }
137 break;
Nate Begemanf7e43382005-03-26 07:46:36 +0000138 case MVT::i64: ObjSize = 8;
139 // FIXME: can split 64b load between reg/mem if it is last arg in regs
Nate Begemana9795f82005-03-24 04:41:43 +0000140 if (GPR_remaining > 1) {
141 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
142 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx+1]);
Nate Begemanca12a2b2005-03-28 22:28:37 +0000143 // Copy the extracted halves into the virtual registers
Nate Begemanf70b5762005-03-28 23:08:54 +0000144 SDOperand argHi = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32,
145 DAG.getRoot());
146 SDOperand argLo = DAG.getCopyFromReg(GPR[GPR_idx+1], MVT::i32, argHi);
Nate Begemanca12a2b2005-03-28 22:28:37 +0000147 // Build the outgoing arg thingy
Nate Begemanf70b5762005-03-28 23:08:54 +0000148 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
149 newroot = argLo;
Nate Begemana9795f82005-03-24 04:41:43 +0000150 } else {
151 needsLoad = true;
152 }
153 break;
154 case MVT::f32: ObjSize = 4;
155 case MVT::f64: ObjSize = 8;
156 if (FPR_remaining > 0) {
157 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, FPR[FPR_idx]);
Nate Begemanf70b5762005-03-28 23:08:54 +0000158 argt = newroot = DAG.getCopyFromReg(FPR[FPR_idx], ObjectVT,
159 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000160 --FPR_remaining;
161 ++FPR_idx;
162 } else {
163 needsLoad = true;
164 }
165 break;
166 }
167
168 // We need to load the argument to a virtual register if we determined above
169 // that we ran out of physical registers of the appropriate type
170 if (needsLoad) {
171 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
172 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
173 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN);
174 }
175
176 // Every 4 bytes of argument space consumes one of the GPRs available for
177 // argument passing.
178 if (GPR_remaining > 0) {
179 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
180 GPR_remaining -= delta;
181 GPR_idx += delta;
182 }
183 ArgOffset += ObjSize;
184
185 DAG.setRoot(newroot.getValue(1));
186 ArgValues.push_back(argt);
187 }
188
Nate Begemana9795f82005-03-24 04:41:43 +0000189 // If the function takes variable number of arguments, make a frame index for
190 // the start of the first vararg value... for expansion of llvm.va_start.
191 if (F.isVarArg())
192 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
193
194 return ArgValues;
195}
196
197std::pair<SDOperand, SDOperand>
198PPC32TargetLowering::LowerCallTo(SDOperand Chain,
Nate Begeman307e7442005-03-26 01:28:53 +0000199 const Type *RetTy, bool isVarArg,
200 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG) {
201 // args_to_use will accumulate outgoing args for the ISD::CALL case in
202 // SelectExpr to use to put the arguments in the appropriate registers.
Nate Begemana9795f82005-03-24 04:41:43 +0000203 std::vector<SDOperand> args_to_use;
Nate Begeman307e7442005-03-26 01:28:53 +0000204
205 // Count how many bytes are to be pushed on the stack, including the linkage
206 // area, and parameter passing area.
207 unsigned NumBytes = 24;
208
209 if (Args.empty()) {
210 NumBytes = 0; // Save zero bytes.
211 } else {
212 for (unsigned i = 0, e = Args.size(); i != e; ++i)
213 switch (getValueType(Args[i].second)) {
214 default: assert(0 && "Unknown value type!");
215 case MVT::i1:
216 case MVT::i8:
217 case MVT::i16:
218 case MVT::i32:
219 case MVT::f32:
220 NumBytes += 4;
221 break;
222 case MVT::i64:
223 case MVT::f64:
224 NumBytes += 8;
225 break;
226 }
227
228 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
229 // plus 32 bytes of argument space in case any called code gets funky on us.
230 if (NumBytes < 56) NumBytes = 56;
231
232 // Adjust the stack pointer for the new arguments...
233 // These operations are automatically eliminated by the prolog/epilog pass
234 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
235 DAG.getConstant(NumBytes, getPointerTy()));
236
237 // Set up a copy of the stack pointer for use loading and storing any
238 // arguments that may not fit in the registers available for argument
239 // passing.
240 SDOperand StackPtr = DAG.getCopyFromReg(PPC::R1, MVT::i32,
241 DAG.getEntryNode());
242
243 // Figure out which arguments are going to go in registers, and which in
244 // memory. Also, if this is a vararg function, floating point operations
245 // must be stored to our stack, and loaded into integer regs as well, if
246 // any integer regs are available for argument passing.
247 unsigned ArgOffset = 24;
248 unsigned GPR_remaining = 8;
249 unsigned FPR_remaining = 13;
Nate Begeman74d73452005-03-31 00:15:26 +0000250 unsigned GPR_idx = 0, FPR_idx = 0;
251 static const unsigned GPR[] = {
252 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
253 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
254 };
255 static const unsigned FPR[] = {
256 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
257 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
258 };
259
260 std::vector<SDOperand> MemOps;
Nate Begeman307e7442005-03-26 01:28:53 +0000261 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
262 // PtrOff will be used to store the current argument to the stack if a
263 // register cannot be found for it.
264 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
265 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Nate Begemanf7e43382005-03-26 07:46:36 +0000266 MVT::ValueType ArgVT = getValueType(Args[i].second);
Nate Begeman307e7442005-03-26 01:28:53 +0000267
Nate Begemanf7e43382005-03-26 07:46:36 +0000268 switch (ArgVT) {
Nate Begeman307e7442005-03-26 01:28:53 +0000269 default: assert(0 && "Unexpected ValueType for argument!");
270 case MVT::i1:
271 case MVT::i8:
272 case MVT::i16:
273 // Promote the integer to 32 bits. If the input type is signed use a
274 // sign extend, otherwise use a zero extend.
275 if (Args[i].second->isSigned())
276 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
277 else
278 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
279 // FALL THROUGH
280 case MVT::i32:
281 if (GPR_remaining > 0) {
Nate Begeman74d73452005-03-31 00:15:26 +0000282 args_to_use.push_back(DAG.getCopyToReg(Chain, Args[i].first,
283 GPR[GPR_idx]));
Nate Begeman307e7442005-03-26 01:28:53 +0000284 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000285 ++GPR_idx;
Nate Begeman307e7442005-03-26 01:28:53 +0000286 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000287 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
288 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000289 }
290 ArgOffset += 4;
291 break;
292 case MVT::i64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000293 // If we have one free GPR left, we can place the upper half of the i64
294 // in it, and store the other half to the stack. If we have two or more
295 // free GPRs, then we can pass both halves of the i64 in registers.
296 if (GPR_remaining > 0) {
Nate Begemanf2622612005-03-26 02:17:46 +0000297 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
298 Args[i].first, DAG.getConstant(1, MVT::i32));
299 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
300 Args[i].first, DAG.getConstant(0, MVT::i32));
Nate Begeman74d73452005-03-31 00:15:26 +0000301 args_to_use.push_back(DAG.getCopyToReg(Chain, Hi, GPR[GPR_idx]));
302 --GPR_remaining;
303 ++GPR_idx;
304 if (GPR_remaining > 0) {
305 args_to_use.push_back(DAG.getCopyToReg(Chain, Lo, GPR[GPR_idx]));
306 --GPR_remaining;
307 ++GPR_idx;
Nate Begemanf7e43382005-03-26 07:46:36 +0000308 } else {
309 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
310 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Nate Begeman74d73452005-03-31 00:15:26 +0000311 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
312 Lo, PtrOff));
Nate Begemanf7e43382005-03-26 07:46:36 +0000313 }
Nate Begeman307e7442005-03-26 01:28:53 +0000314 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000315 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
316 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000317 }
318 ArgOffset += 8;
319 break;
320 case MVT::f32:
Nate Begeman307e7442005-03-26 01:28:53 +0000321 case MVT::f64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000322 if (FPR_remaining > 0) {
323 if (isVarArg) {
Nate Begeman74d73452005-03-31 00:15:26 +0000324 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
325 Args[i].first, PtrOff));
326 // Float varargs are always shadowed in available integer registers
327 if (GPR_remaining > 0) {
328 SDOperand Load = DAG.getLoad(MVT::i32, Chain, PtrOff);
329 MemOps.push_back(Load);
330 args_to_use.push_back(DAG.getCopyToReg(Chain, Load,
331 GPR[GPR_idx]));
332 }
333 if (GPR_remaining > 1 && MVT::f64 == ArgVT) {
334 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
335 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
336 SDOperand Load = DAG.getLoad(MVT::i32, Chain, PtrOff);
337 MemOps.push_back(Load);
338 args_to_use.push_back(DAG.getCopyToReg(Chain, Load,
339 GPR[GPR_idx+1]));
340 }
Nate Begemanf7e43382005-03-26 07:46:36 +0000341 }
Nate Begeman74d73452005-03-31 00:15:26 +0000342 args_to_use.push_back(DAG.getCopyToReg(Chain, Args[i].first,
343 FPR[FPR_idx]));
Nate Begeman307e7442005-03-26 01:28:53 +0000344 --FPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000345 ++FPR_idx;
Nate Begemanf7e43382005-03-26 07:46:36 +0000346 // If we have any FPRs remaining, we may also have GPRs remaining.
347 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
348 // GPRs.
Nate Begeman74d73452005-03-31 00:15:26 +0000349 if (GPR_remaining > 0) {
350 --GPR_remaining;
351 ++GPR_idx;
352 }
353 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
354 --GPR_remaining;
355 ++GPR_idx;
356 }
Nate Begeman307e7442005-03-26 01:28:53 +0000357 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000358 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
359 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000360 }
Nate Begemanf7e43382005-03-26 07:46:36 +0000361 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
Nate Begeman307e7442005-03-26 01:28:53 +0000362 break;
363 }
Nate Begemana9795f82005-03-24 04:41:43 +0000364 }
Nate Begeman74d73452005-03-31 00:15:26 +0000365 if (!MemOps.empty())
366 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
Nate Begemana9795f82005-03-24 04:41:43 +0000367 }
368
369 std::vector<MVT::ValueType> RetVals;
370 MVT::ValueType RetTyVT = getValueType(RetTy);
371 if (RetTyVT != MVT::isVoid)
372 RetVals.push_back(RetTyVT);
373 RetVals.push_back(MVT::Other);
374
375 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
376 Chain, Callee, args_to_use), 0);
377 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
378 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
379 DAG.getConstant(NumBytes, getPointerTy()));
380 return std::make_pair(TheCall, Chain);
381}
382
383std::pair<SDOperand, SDOperand>
384PPC32TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
385 //vastart just returns the address of the VarArgsFrameIndex slot.
386 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
387}
388
389std::pair<SDOperand,SDOperand> PPC32TargetLowering::
390LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
391 const Type *ArgTy, SelectionDAG &DAG) {
Nate Begemanc7b09f12005-03-25 08:34:25 +0000392 MVT::ValueType ArgVT = getValueType(ArgTy);
393 SDOperand Result;
394 if (!isVANext) {
395 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList);
396 } else {
397 unsigned Amt;
398 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
399 Amt = 4;
400 else {
401 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
402 "Other types should have been promoted for varargs!");
403 Amt = 8;
404 }
405 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
406 DAG.getConstant(Amt, VAList.getValueType()));
407 }
408 return std::make_pair(Result, Chain);
Nate Begemana9795f82005-03-24 04:41:43 +0000409}
410
411
412std::pair<SDOperand, SDOperand> PPC32TargetLowering::
413LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
414 SelectionDAG &DAG) {
Nate Begeman01d05262005-03-30 01:45:43 +0000415 assert(0 && "LowerFrameReturnAddress unimplemented");
Nate Begemana9795f82005-03-24 04:41:43 +0000416 abort();
417}
418
419namespace {
420
421//===--------------------------------------------------------------------===//
422/// ISel - PPC32 specific code to select PPC32 machine instructions for
423/// SelectionDAG operations.
424//===--------------------------------------------------------------------===//
425class ISel : public SelectionDAGISel {
426
427 /// Comment Here.
428 PPC32TargetLowering PPC32Lowering;
429
430 /// ExprMap - As shared expressions are codegen'd, we keep track of which
431 /// vreg the value is produced in, so we only emit one copy of each compiled
432 /// tree.
433 std::map<SDOperand, unsigned> ExprMap;
Nate Begemanc7b09f12005-03-25 08:34:25 +0000434
435 unsigned GlobalBaseReg;
436 bool GlobalBaseInitialized;
Nate Begemana9795f82005-03-24 04:41:43 +0000437
438public:
439 ISel(TargetMachine &TM) : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM)
440 {}
441
Nate Begemanc7b09f12005-03-25 08:34:25 +0000442 /// runOnFunction - Override this function in order to reset our per-function
443 /// variables.
444 virtual bool runOnFunction(Function &Fn) {
445 // Make sure we re-emit a set of the global base reg if necessary
446 GlobalBaseInitialized = false;
447 return SelectionDAGISel::runOnFunction(Fn);
448 }
449
Nate Begemana9795f82005-03-24 04:41:43 +0000450 /// InstructionSelectBasicBlock - This callback is invoked by
451 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
452 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
453 DEBUG(BB->dump());
454 // Codegen the basic block.
455 Select(DAG.getRoot());
456
457 // Clear state used for selection.
458 ExprMap.clear();
459 }
460
Nate Begemanc7b09f12005-03-25 08:34:25 +0000461 unsigned ISel::getGlobalBaseReg();
Nate Begemana9795f82005-03-24 04:41:43 +0000462 unsigned SelectExpr(SDOperand N);
463 unsigned SelectExprFP(SDOperand N, unsigned Result);
464 void Select(SDOperand N);
465
466 void SelectAddr(SDOperand N, unsigned& Reg, int& offset);
467 void SelectBranchCC(SDOperand N);
468};
469
470/// canUseAsImmediateForOpcode - This method returns a value indicating whether
471/// the ConstantSDNode N can be used as an immediate to Opcode. The return
472/// values are either 0, 1 or 2. 0 indicates that either N is not a
473/// ConstantSDNode, or is not suitable for use by that opcode. A return value
474/// of 1 indicates that the constant may be used in normal immediate form. A
475/// return value of 2 indicates that the constant may be used in shifted
476/// immediate form. If the return value is nonzero, the constant value is
477/// placed in Imm.
478///
479static unsigned canUseAsImmediateForOpcode(SDOperand N, unsigned Opcode,
480 unsigned& Imm) {
481 if (N.getOpcode() != ISD::Constant) return 0;
482
483 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
484
485 switch(Opcode) {
486 default: return 0;
487 case ISD::ADD:
488 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
489 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
490 break;
491 case ISD::AND:
492 case ISD::XOR:
493 case ISD::OR:
494 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
495 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
496 break;
Nate Begeman307e7442005-03-26 01:28:53 +0000497 case ISD::MUL:
498 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
499 break;
Nate Begemana9795f82005-03-24 04:41:43 +0000500 }
501 return 0;
502}
503}
504
Nate Begemanc7b09f12005-03-25 08:34:25 +0000505/// getGlobalBaseReg - Output the instructions required to put the
506/// base address to use for accessing globals into a register.
507///
508unsigned ISel::getGlobalBaseReg() {
509 if (!GlobalBaseInitialized) {
510 // Insert the set of GlobalBaseReg into the first MBB of the function
511 MachineBasicBlock &FirstMBB = BB->getParent()->front();
512 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
513 GlobalBaseReg = MakeReg(MVT::i32);
514 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
515 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR);
516 GlobalBaseInitialized = true;
517 }
518 return GlobalBaseReg;
519}
520
Nate Begemana9795f82005-03-24 04:41:43 +0000521//Check to see if the load is a constant offset from a base register
522void ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
523{
524 Reg = SelectExpr(N);
525 offset = 0;
526 return;
527}
528
529void ISel::SelectBranchCC(SDOperand N)
530{
531 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
532 MachineBasicBlock *Dest =
533 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
534 unsigned Opc;
535
536 Select(N.getOperand(0)); //chain
537 SDOperand CC = N.getOperand(1);
538
Nate Begeman23afcfb2005-03-29 22:48:55 +0000539 //Give up and do the stupid thing
Nate Begemana9795f82005-03-24 04:41:43 +0000540 unsigned Tmp1 = SelectExpr(CC);
Nate Begeman23afcfb2005-03-29 22:48:55 +0000541 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(0);
542 BuildMI(BB, PPC::BNE, 2).addReg(PPC::CR0).addMBB(Dest);
Nate Begemana9795f82005-03-24 04:41:43 +0000543 return;
544}
545
546unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
547{
548 unsigned Tmp1, Tmp2, Tmp3;
549 unsigned Opc = 0;
550 SDNode *Node = N.Val;
551 MVT::ValueType DestType = N.getValueType();
552 unsigned opcode = N.getOpcode();
553
554 switch (opcode) {
555 default:
556 Node->dump();
557 assert(0 && "Node not handled!\n");
558
Nate Begeman23afcfb2005-03-29 22:48:55 +0000559 case ISD::SELECT: {
560 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
561
562 // FIXME: generate FSEL here
563
564 // Create an iterator with which to insert the MBB for copying the false
565 // value and the MBB to hold the PHI instruction for this SetCC.
566 MachineBasicBlock *thisMBB = BB;
567 const BasicBlock *LLVM_BB = BB->getBasicBlock();
568 ilist<MachineBasicBlock>::iterator It = BB;
569 ++It;
570
571 // thisMBB:
572 // ...
573 // TrueVal = ...
574 // cmpTY cr0, r1, r2
575 // bCC copy1MBB
576 // fallthrough --> copy0MBB
577 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(0);
578 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
579 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
580 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
581 BuildMI(BB, PPC::BNE, 2).addReg(PPC::CR0).addMBB(sinkMBB);
582 MachineFunction *F = BB->getParent();
583 F->getBasicBlockList().insert(It, copy0MBB);
584 F->getBasicBlockList().insert(It, sinkMBB);
585 // Update machine-CFG edges
586 BB->addSuccessor(copy0MBB);
587 BB->addSuccessor(sinkMBB);
588
589 // copy0MBB:
590 // %FalseValue = ...
591 // # fallthrough to sinkMBB
592 BB = copy0MBB;
593 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
594 // Update machine-CFG edges
595 BB->addSuccessor(sinkMBB);
596
597 // sinkMBB:
598 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
599 // ...
600 BB = sinkMBB;
601 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
602 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
603 return Result;
604 }
Nate Begemana9795f82005-03-24 04:41:43 +0000605
606 case ISD::FP_ROUND:
607 assert (DestType == MVT::f32 &&
608 N.getOperand(0).getValueType() == MVT::f64 &&
609 "only f64 to f32 conversion supported here");
610 Tmp1 = SelectExpr(N.getOperand(0));
611 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
612 return Result;
613
614 case ISD::FP_EXTEND:
615 assert (DestType == MVT::f64 &&
616 N.getOperand(0).getValueType() == MVT::f32 &&
617 "only f32 to f64 conversion supported here");
618 Tmp1 = SelectExpr(N.getOperand(0));
619 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
620 return Result;
621
622 case ISD::CopyFromReg:
Nate Begemanf2622612005-03-26 02:17:46 +0000623 if (Result == 1)
624 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
625 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
626 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
627 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000628
Nate Begemana9795f82005-03-24 04:41:43 +0000629 case ISD::ConstantFP:
Nate Begemanca12a2b2005-03-28 22:28:37 +0000630 assert(0 && "ISD::ConstantFP Unimplemented");
Nate Begemana9795f82005-03-24 04:41:43 +0000631 abort();
632
633 case ISD::MUL:
634 case ISD::ADD:
635 case ISD::SUB:
636 case ISD::SDIV:
637 switch( opcode ) {
638 case ISD::MUL: Opc = DestType == MVT::f64 ? PPC::FMUL : PPC::FMULS; break;
639 case ISD::ADD: Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS; break;
640 case ISD::SUB: Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS; break;
641 case ISD::SDIV: Opc = DestType == MVT::f64 ? PPC::FDIV : PPC::FDIVS; break;
642 };
Nate Begemana9795f82005-03-24 04:41:43 +0000643 Tmp1 = SelectExpr(N.getOperand(0));
644 Tmp2 = SelectExpr(N.getOperand(1));
645 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
646 return Result;
647
Nate Begemana9795f82005-03-24 04:41:43 +0000648 case ISD::UINT_TO_FP:
Nate Begemanfdcf3412005-03-30 19:38:35 +0000649 case ISD::SINT_TO_FP: {
650 assert (N.getOperand(0).getValueType() == MVT::i32
651 && "int to float must operate on i32");
652 bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
653 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
654 Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into
655 Tmp3 = MakeReg(MVT::i32); // temp reg to hold the conversion constant
656 unsigned ConstF = MakeReg(MVT::f64); // temp reg to hold the fp constant
657
658 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
659 MachineConstantPool *CP = BB->getParent()->getConstantPool();
660
661 // FIXME: pull this FP constant generation stuff out into something like
662 // the simple ISel's getReg.
663 if (IsUnsigned) {
664 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000000p52);
665 unsigned CPI = CP->getConstantPoolIndex(CFP);
666 // Load constant fp value
667 unsigned Tmp4 = MakeReg(MVT::i32);
668 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg())
669 .addConstantPoolIndex(CPI);
670 BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
671 // Store the hi & low halves of the fp value, currently in int regs
672 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
673 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
674 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp1), FrameIdx, 4);
675 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
676 // Generate the return value with a subtract
677 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
678 } else {
679 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000008p52);
680 unsigned CPI = CP->getConstantPoolIndex(CFP);
681 // Load constant fp value
682 unsigned Tmp4 = MakeReg(MVT::i32);
683 unsigned TmpL = MakeReg(MVT::i32);
684 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg())
685 .addConstantPoolIndex(CPI);
686 BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
687 // Store the hi & low halves of the fp value, currently in int regs
688 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
689 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
690 BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
691 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
692 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
693 // Generate the return value with a subtract
694 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
695 }
696 return Result;
697 }
Nate Begemana9795f82005-03-24 04:41:43 +0000698 }
699 assert(0 && "should not get here");
700 return 0;
701}
702
703unsigned ISel::SelectExpr(SDOperand N) {
704 unsigned Result;
705 unsigned Tmp1, Tmp2, Tmp3;
706 unsigned Opc = 0;
707 unsigned opcode = N.getOpcode();
708
709 SDNode *Node = N.Val;
710 MVT::ValueType DestType = N.getValueType();
711
712 unsigned &Reg = ExprMap[N];
713 if (Reg) return Reg;
714
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000715 if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::ADD_PARTS &&
716 N.getOpcode() != ISD::SUB_PARTS)
Nate Begemana9795f82005-03-24 04:41:43 +0000717 Reg = Result = (N.getValueType() != MVT::Other) ?
718 MakeReg(N.getValueType()) : 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000719 else {
720 // If this is a call instruction, make sure to prepare ALL of the result
721 // values as well as the chain.
722 if (N.getOpcode() == ISD::CALL) {
723 if (Node->getNumValues() == 1)
724 Reg = Result = 1; // Void call, just a chain.
725 else {
726 Result = MakeReg(Node->getValueType(0));
727 ExprMap[N.getValue(0)] = Result;
728 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
729 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
730 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
731 }
732 } else {
733 Result = MakeReg(Node->getValueType(0));
734 ExprMap[N.getValue(0)] = Result;
735 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
736 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
737 }
738 }
739
740 if (DestType == MVT::f64 || DestType == MVT::f32)
Nate Begeman74d73452005-03-31 00:15:26 +0000741 if (ISD::LOAD != opcode && ISD::EXTLOAD != opcode)
742 return SelectExprFP(N, Result);
Nate Begemana9795f82005-03-24 04:41:43 +0000743
744 switch (opcode) {
745 default:
746 Node->dump();
747 assert(0 && "Node not handled!\n");
748
749 case ISD::DYNAMIC_STACKALLOC:
Nate Begeman5e966612005-03-24 06:28:42 +0000750 // Generate both result values. FIXME: Need a better commment here?
751 if (Result != 1)
752 ExprMap[N.getValue(1)] = 1;
753 else
754 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
755
756 // FIXME: We are currently ignoring the requested alignment for handling
757 // greater than the stack alignment. This will need to be revisited at some
758 // point. Align = N.getOperand(2);
759 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
760 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
761 std::cerr << "Cannot allocate stack object with greater alignment than"
762 << " the stack alignment yet!";
763 abort();
764 }
765 Select(N.getOperand(0));
766 Tmp1 = SelectExpr(N.getOperand(1));
767 // Subtract size from stack pointer, thereby allocating some space.
768 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
769 // Put a pointer to the space into the result register by copying the SP
770 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
771 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000772
773 case ISD::ConstantPool:
Nate Begemanca12a2b2005-03-28 22:28:37 +0000774 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
775 Tmp2 = MakeReg(MVT::i32);
776 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp2).addReg(getGlobalBaseReg())
777 .addConstantPoolIndex(Tmp1);
778 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
779 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000780
781 case ISD::FrameIndex:
Nate Begemanf3d08f32005-03-29 00:03:27 +0000782 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Nate Begeman58f718c2005-03-30 02:23:08 +0000783 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
Nate Begemanf3d08f32005-03-29 00:03:27 +0000784 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000785
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000786 case ISD::GlobalAddress: {
787 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanca12a2b2005-03-28 22:28:37 +0000788 Tmp1 = MakeReg(MVT::i32);
Nate Begemanc7b09f12005-03-25 08:34:25 +0000789 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
790 .addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000791 if (GV->hasWeakLinkage() || GV->isExternal()) {
792 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
793 } else {
794 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
795 }
796 return Result;
797 }
798
Nate Begeman5e966612005-03-24 06:28:42 +0000799 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +0000800 case ISD::EXTLOAD:
801 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000802 case ISD::SEXTLOAD: {
Nate Begeman9db505c2005-03-28 19:36:43 +0000803 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
804 Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType();
Nate Begeman74d73452005-03-31 00:15:26 +0000805 bool sext = (ISD::SEXTLOAD == opcode);
806 bool byte = (MVT::i8 == TypeBeingLoaded);
807
Nate Begeman5e966612005-03-24 06:28:42 +0000808 // Make sure we generate both values.
809 if (Result != 1)
810 ExprMap[N.getValue(1)] = 1; // Generate the token
811 else
812 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
813
814 SDOperand Chain = N.getOperand(0);
815 SDOperand Address = N.getOperand(1);
816 Select(Chain);
817
Nate Begeman9db505c2005-03-28 19:36:43 +0000818 switch (TypeBeingLoaded) {
Nate Begeman74d73452005-03-31 00:15:26 +0000819 default: Node->dump(); assert(0 && "Cannot load this type!");
Nate Begeman9db505c2005-03-28 19:36:43 +0000820 case MVT::i1: Opc = PPC::LBZ; break;
821 case MVT::i8: Opc = PPC::LBZ; break;
822 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
823 case MVT::i32: Opc = PPC::LWZ; break;
Nate Begeman74d73452005-03-31 00:15:26 +0000824 case MVT::f32: Opc = PPC::LFS; break;
825 case MVT::f64: Opc = PPC::LFD; break;
Nate Begeman5e966612005-03-24 06:28:42 +0000826 }
827
Nate Begeman74d73452005-03-31 00:15:26 +0000828 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
829 Tmp1 = MakeReg(MVT::i32);
830 int CPI = CP->getIndex();
831 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
832 .addConstantPoolIndex(CPI);
833 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +0000834 }
Nate Begeman74d73452005-03-31 00:15:26 +0000835 else if(Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman58f718c2005-03-30 02:23:08 +0000836 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
837 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
Nate Begeman5e966612005-03-24 06:28:42 +0000838 } else {
839 int offset;
840 SelectAddr(Address, Tmp1, offset);
841 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
842 }
843 return Result;
844 }
845
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000846 case ISD::CALL: {
847 // Lower the chain for this call.
848 Select(N.getOperand(0));
849 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
Nate Begeman74d73452005-03-31 00:15:26 +0000850
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000851 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Nate Begeman74d73452005-03-31 00:15:26 +0000852 Select(N.getOperand(i));
853
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000854 // Emit the correct call instruction based on the type of symbol called.
855 if (GlobalAddressSDNode *GASD =
856 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
857 BuildMI(BB, PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(), true);
858 } else if (ExternalSymbolSDNode *ESSDN =
859 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
860 BuildMI(BB, PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(), true);
861 } else {
862 Tmp1 = SelectExpr(N.getOperand(1));
863 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
864 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
865 BuildMI(BB, PPC::CALLindirect, 3).addImm(20).addImm(0).addReg(PPC::R12);
866 }
867
868 switch (Node->getValueType(0)) {
869 default: assert(0 && "Unknown value type for call result!");
870 case MVT::Other: return 1;
871 case MVT::i1:
872 case MVT::i8:
873 case MVT::i16:
874 case MVT::i32:
Nate Begemanc7b09f12005-03-25 08:34:25 +0000875 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000876 if (Node->getValueType(1) == MVT::i32)
Nate Begemanc7b09f12005-03-25 08:34:25 +0000877 BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R4).addReg(PPC::R4);
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000878 break;
879 case MVT::f32:
880 case MVT::f64:
881 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
882 break;
883 }
884 return Result+N.ResNo;
885 }
Nate Begemana9795f82005-03-24 04:41:43 +0000886
887 case ISD::SIGN_EXTEND:
888 case ISD::SIGN_EXTEND_INREG:
889 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman9db505c2005-03-28 19:36:43 +0000890 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
891 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
892 case MVT::i16:
893 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
894 break;
895 case MVT::i8:
896 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
897 break;
Nate Begeman74747862005-03-29 22:24:51 +0000898 case MVT::i1:
899 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
900 break;
Nate Begeman9db505c2005-03-28 19:36:43 +0000901 }
Nate Begemana9795f82005-03-24 04:41:43 +0000902 return Result;
903
904 case ISD::ZERO_EXTEND_INREG:
905 Tmp1 = SelectExpr(N.getOperand(0));
906 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
Nate Begeman9db505c2005-03-28 19:36:43 +0000907 default: Node->dump(); assert(0 && "Unhandled ZERO_EXTEND type"); break;
Nate Begemana9795f82005-03-24 04:41:43 +0000908 case MVT::i16: Tmp2 = 16; break;
909 case MVT::i8: Tmp2 = 24; break;
910 case MVT::i1: Tmp2 = 31; break;
911 }
Nate Begeman33162522005-03-29 21:54:38 +0000912 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(0).addImm(Tmp2)
913 .addImm(31);
Nate Begemana9795f82005-03-24 04:41:43 +0000914 return Result;
915
Nate Begemana9795f82005-03-24 04:41:43 +0000916 case ISD::CopyFromReg:
917 if (Result == 1)
918 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
919 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
920 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
921 return Result;
922
923 case ISD::SHL:
Nate Begeman5e966612005-03-24 06:28:42 +0000924 Tmp1 = SelectExpr(N.getOperand(0));
925 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
926 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +0000927 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
Nate Begeman5e966612005-03-24 06:28:42 +0000928 .addImm(31-Tmp2);
929 } else {
930 Tmp2 = SelectExpr(N.getOperand(1));
931 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
932 }
933 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000934
Nate Begeman5e966612005-03-24 06:28:42 +0000935 case ISD::SRL:
936 Tmp1 = SelectExpr(N.getOperand(0));
937 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
938 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +0000939 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2)
Nate Begeman5e966612005-03-24 06:28:42 +0000940 .addImm(Tmp2).addImm(31);
941 } else {
942 Tmp2 = SelectExpr(N.getOperand(1));
943 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
944 }
945 return Result;
946
947 case ISD::SRA:
948 Tmp1 = SelectExpr(N.getOperand(0));
949 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
950 Tmp2 = CN->getValue() & 0x1F;
951 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
952 } else {
953 Tmp2 = SelectExpr(N.getOperand(1));
954 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
955 }
956 return Result;
957
Nate Begemana9795f82005-03-24 04:41:43 +0000958 case ISD::ADD:
959 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
960 Tmp1 = SelectExpr(N.getOperand(0));
961 switch(canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
962 default: assert(0 && "unhandled result code");
963 case 0: // No immediate
964 Tmp2 = SelectExpr(N.getOperand(1));
965 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
966 break;
967 case 1: // Low immediate
968 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
969 break;
970 case 2: // Shifted immediate
971 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
972 break;
973 }
974 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000975
Nate Begemana9795f82005-03-24 04:41:43 +0000976 case ISD::AND:
977 case ISD::OR:
978 case ISD::XOR:
979 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
980 Tmp1 = SelectExpr(N.getOperand(0));
981 switch(canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
982 default: assert(0 && "unhandled result code");
983 case 0: // No immediate
984 Tmp2 = SelectExpr(N.getOperand(1));
985 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +0000986 case ISD::AND: Opc = PPC::AND; break;
987 case ISD::OR: Opc = PPC::OR; break;
988 case ISD::XOR: Opc = PPC::XOR; break;
Nate Begemana9795f82005-03-24 04:41:43 +0000989 }
Nate Begeman5e966612005-03-24 06:28:42 +0000990 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +0000991 break;
992 case 1: // Low immediate
993 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +0000994 case ISD::AND: Opc = PPC::ANDIo; break;
995 case ISD::OR: Opc = PPC::ORI; break;
996 case ISD::XOR: Opc = PPC::XORI; break;
Nate Begemana9795f82005-03-24 04:41:43 +0000997 }
Nate Begeman5e966612005-03-24 06:28:42 +0000998 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +0000999 break;
1000 case 2: // Shifted immediate
1001 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +00001002 case ISD::AND: Opc = PPC::ANDISo; break;
1003 case ISD::OR: Opc = PPC::ORIS; break;
1004 case ISD::XOR: Opc = PPC::XORIS; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001005 }
Nate Begeman5e966612005-03-24 06:28:42 +00001006 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001007 break;
1008 }
1009 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001010
1011 case ISD::SUB:
1012 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1013 Tmp1 = SelectExpr(N.getOperand(0));
1014 Tmp2 = SelectExpr(N.getOperand(1));
1015 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
1016 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001017
Nate Begeman5e966612005-03-24 06:28:42 +00001018 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001019 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1020 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman307e7442005-03-26 01:28:53 +00001021 if (1 == canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
1022 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1023 else {
1024 Tmp2 = SelectExpr(N.getOperand(1));
1025 BuildMI(BB, PPC::MULLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1026 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001027 return Result;
1028
Nate Begemanf3d08f32005-03-29 00:03:27 +00001029 case ISD::SDIV:
1030 case ISD::UDIV:
1031 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1032 Tmp1 = SelectExpr(N.getOperand(0));
1033 Tmp2 = SelectExpr(N.getOperand(1));
1034 Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW;
1035 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1036 return Result;
1037
1038 case ISD::UREM:
1039 case ISD::SREM: {
1040 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1041 Tmp1 = SelectExpr(N.getOperand(0));
1042 Tmp2 = SelectExpr(N.getOperand(1));
1043 Tmp3 = MakeReg(MVT::i32);
1044 unsigned Tmp4 = MakeReg(MVT::i32);
1045 Opc = (ISD::UREM == opcode) ? PPC::DIVWU : PPC::DIVW;
1046 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1047 BuildMI(BB, PPC::MULLW, 2, Tmp4).addReg(Tmp3).addReg(Tmp2);
1048 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp4).addReg(Tmp1);
1049 return Result;
1050 }
1051
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001052 case ISD::ADD_PARTS:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001053 case ISD::SUB_PARTS: {
1054 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1055 "Not an i64 add/sub!");
1056 // Emit all of the operands.
1057 std::vector<unsigned> InVals;
1058 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
1059 InVals.push_back(SelectExpr(N.getOperand(i)));
1060 if (N.getOpcode() == ISD::ADD_PARTS) {
Nate Begemanf70b5762005-03-28 23:08:54 +00001061 BuildMI(BB, PPC::ADDC, 2, Result+1).addReg(InVals[0]).addReg(InVals[2]);
1062 BuildMI(BB, PPC::ADDE, 2, Result).addReg(InVals[1]).addReg(InVals[3]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001063 } else {
Nate Begemanf70b5762005-03-28 23:08:54 +00001064 BuildMI(BB, PPC::SUBFC, 2, Result+1).addReg(InVals[2]).addReg(InVals[0]);
1065 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(InVals[3]).addReg(InVals[1]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001066 }
1067 return Result+N.ResNo;
1068 }
1069
Nate Begemana9795f82005-03-24 04:41:43 +00001070 case ISD::FP_TO_UINT:
1071 case ISD::FP_TO_SINT:
Nate Begeman01d05262005-03-30 01:45:43 +00001072 assert(0 && "FP_TO_S/UINT unimplemented");
Nate Begemana9795f82005-03-24 04:41:43 +00001073 abort();
1074
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001075 case ISD::SETCC:
Nate Begeman33162522005-03-29 21:54:38 +00001076 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1077 bool U = false;
1078 bool IsInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
1079
1080 switch (SetCC->getCondition()) {
1081 default: Node->dump(); assert(0 && "Unknown comparison!");
1082 case ISD::SETEQ: Opc = PPC::BEQ; break;
1083 case ISD::SETNE: Opc = PPC::BNE; break;
1084 case ISD::SETULT: U = true;
1085 case ISD::SETLT: Opc = PPC::BLT; break;
1086 case ISD::SETULE: U = true;
1087 case ISD::SETLE: Opc = PPC::BLE; break;
1088 case ISD::SETUGT: U = true;
1089 case ISD::SETGT: Opc = PPC::BGT; break;
1090 case ISD::SETUGE: U = true;
1091 case ISD::SETGE: Opc = PPC::BGE; break;
1092 }
1093
1094 // FIXME: Is there a situation in which we would ever need to emit fcmpo?
1095 static const unsigned CompareOpcodes[] =
1096 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
1097 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
1098
1099 // Create an iterator with which to insert the MBB for copying the false
1100 // value and the MBB to hold the PHI instruction for this SetCC.
1101 MachineBasicBlock *thisMBB = BB;
1102 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1103 ilist<MachineBasicBlock>::iterator It = BB;
1104 ++It;
1105
1106 // thisMBB:
1107 // ...
1108 // cmpTY cr0, r1, r2
1109 // %TrueValue = li 1
1110 // bCC sinkMBB
1111 Tmp1 = SelectExpr(N.getOperand(0));
1112 Tmp2 = SelectExpr(N.getOperand(1));
1113 BuildMI(BB, CompareOpc, 2, PPC::CR0).addReg(Tmp1).addReg(Tmp2);
1114 unsigned TrueValue = MakeReg(MVT::i32);
1115 BuildMI(BB, PPC::LI, 1, TrueValue).addSImm(1);
1116 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1117 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1118 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
1119 MachineFunction *F = BB->getParent();
1120 F->getBasicBlockList().insert(It, copy0MBB);
1121 F->getBasicBlockList().insert(It, sinkMBB);
1122 // Update machine-CFG edges
1123 BB->addSuccessor(copy0MBB);
1124 BB->addSuccessor(sinkMBB);
1125
1126 // copy0MBB:
1127 // %FalseValue = li 0
1128 // fallthrough
1129 BB = copy0MBB;
1130 unsigned FalseValue = MakeReg(MVT::i32);
1131 BuildMI(BB, PPC::LI, 1, FalseValue).addSImm(0);
1132 // Update machine-CFG edges
1133 BB->addSuccessor(sinkMBB);
1134
1135 // sinkMBB:
1136 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1137 // ...
1138 BB = sinkMBB;
1139 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1140 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1141 return Result;
1142 }
1143 assert(0 && "Is this legal?");
1144 return 0;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001145
Nate Begeman74747862005-03-29 22:24:51 +00001146 case ISD::SELECT: {
1147 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1148
1149 // Create an iterator with which to insert the MBB for copying the false
1150 // value and the MBB to hold the PHI instruction for this SetCC.
1151 MachineBasicBlock *thisMBB = BB;
1152 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1153 ilist<MachineBasicBlock>::iterator It = BB;
1154 ++It;
1155
1156 // thisMBB:
1157 // ...
1158 // TrueVal = ...
1159 // cmpTY cr0, r1, r2
1160 // bCC copy1MBB
1161 // fallthrough --> copy0MBB
1162 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(0);
1163 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1164 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1165 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
1166 BuildMI(BB, PPC::BNE, 2).addReg(PPC::CR0).addMBB(sinkMBB);
1167 MachineFunction *F = BB->getParent();
1168 F->getBasicBlockList().insert(It, copy0MBB);
1169 F->getBasicBlockList().insert(It, sinkMBB);
1170 // Update machine-CFG edges
1171 BB->addSuccessor(copy0MBB);
1172 BB->addSuccessor(sinkMBB);
1173
1174 // copy0MBB:
1175 // %FalseValue = ...
1176 // # fallthrough to sinkMBB
1177 BB = copy0MBB;
1178 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
1179 // Update machine-CFG edges
1180 BB->addSuccessor(sinkMBB);
1181
1182 // sinkMBB:
1183 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1184 // ...
1185 BB = sinkMBB;
1186 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1187 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1188
1189 // FIXME: Select i64?
1190 return Result;
1191 }
Nate Begemana9795f82005-03-24 04:41:43 +00001192
1193 case ISD::Constant:
1194 switch (N.getValueType()) {
1195 default: assert(0 && "Cannot use constants of this type!");
1196 case MVT::i1:
1197 BuildMI(BB, PPC::LI, 1, Result)
1198 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
1199 break;
1200 case MVT::i32:
1201 {
1202 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
1203 if (v < 32768 && v >= -32768) {
1204 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
1205 } else {
Nate Begeman5e966612005-03-24 06:28:42 +00001206 Tmp1 = MakeReg(MVT::i32);
1207 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
1208 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
Nate Begemana9795f82005-03-24 04:41:43 +00001209 }
1210 }
1211 }
1212 return Result;
1213 }
1214
1215 return 0;
1216}
1217
1218void ISel::Select(SDOperand N) {
1219 unsigned Tmp1, Tmp2, Opc;
1220 unsigned opcode = N.getOpcode();
1221
1222 if (!ExprMap.insert(std::make_pair(N, 1)).second)
1223 return; // Already selected.
1224
1225 SDNode *Node = N.Val;
1226
1227 switch (Node->getOpcode()) {
1228 default:
1229 Node->dump(); std::cerr << "\n";
1230 assert(0 && "Node not handled yet!");
1231 case ISD::EntryToken: return; // Noop
1232 case ISD::TokenFactor:
1233 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1234 Select(Node->getOperand(i));
1235 return;
1236 case ISD::ADJCALLSTACKDOWN:
1237 case ISD::ADJCALLSTACKUP:
1238 Select(N.getOperand(0));
1239 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1240 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? PPC::ADJCALLSTACKDOWN :
1241 PPC::ADJCALLSTACKUP;
1242 BuildMI(BB, Opc, 1).addImm(Tmp1);
1243 return;
1244 case ISD::BR: {
1245 MachineBasicBlock *Dest =
1246 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
Nate Begemana9795f82005-03-24 04:41:43 +00001247 Select(N.getOperand(0));
1248 BuildMI(BB, PPC::B, 1).addMBB(Dest);
1249 return;
1250 }
1251 case ISD::BRCOND:
1252 SelectBranchCC(N);
1253 return;
1254 case ISD::CopyToReg:
1255 Select(N.getOperand(0));
1256 Tmp1 = SelectExpr(N.getOperand(1));
1257 Tmp2 = cast<RegSDNode>(N)->getReg();
1258
1259 if (Tmp1 != Tmp2) {
1260 if (N.getOperand(1).getValueType() == MVT::f64 ||
1261 N.getOperand(1).getValueType() == MVT::f32)
1262 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
1263 else
1264 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1265 }
1266 return;
1267 case ISD::ImplicitDef:
1268 Select(N.getOperand(0));
1269 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
1270 return;
1271 case ISD::RET:
1272 switch (N.getNumOperands()) {
1273 default:
1274 assert(0 && "Unknown return instruction!");
1275 case 3:
1276 assert(N.getOperand(1).getValueType() == MVT::i32 &&
1277 N.getOperand(2).getValueType() == MVT::i32 &&
1278 "Unknown two-register value!");
1279 Select(N.getOperand(0));
1280 Tmp1 = SelectExpr(N.getOperand(1));
1281 Tmp2 = SelectExpr(N.getOperand(2));
1282 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
1283 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp2).addReg(Tmp2);
1284 break;
1285 case 2:
1286 Select(N.getOperand(0));
1287 Tmp1 = SelectExpr(N.getOperand(1));
1288 switch (N.getOperand(1).getValueType()) {
1289 default:
1290 assert(0 && "Unknown return type!");
1291 case MVT::f64:
1292 case MVT::f32:
1293 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
1294 break;
1295 case MVT::i32:
1296 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
1297 break;
1298 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001299 case 1:
1300 Select(N.getOperand(0));
1301 break;
Nate Begemana9795f82005-03-24 04:41:43 +00001302 }
1303 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
1304 return;
Nate Begemana9795f82005-03-24 04:41:43 +00001305 case ISD::TRUNCSTORE:
1306 case ISD::STORE:
1307 {
1308 SDOperand Chain = N.getOperand(0);
1309 SDOperand Value = N.getOperand(1);
1310 SDOperand Address = N.getOperand(2);
1311 Select(Chain);
1312
1313 Tmp1 = SelectExpr(Value); //value
1314
1315 if (opcode == ISD::STORE) {
1316 switch(Value.getValueType()) {
1317 default: assert(0 && "unknown Type in store");
1318 case MVT::i32: Opc = PPC::STW; break;
1319 case MVT::f64: Opc = PPC::STFD; break;
1320 case MVT::f32: Opc = PPC::STFS; break;
1321 }
1322 } else { //ISD::TRUNCSTORE
1323 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1324 default: assert(0 && "unknown Type in store");
1325 case MVT::i1: //FIXME: DAG does not promote this load
1326 case MVT::i8: Opc = PPC::STB; break;
1327 case MVT::i16: Opc = PPC::STH; break;
1328 }
1329 }
1330
1331 if (Address.getOpcode() == ISD::GlobalAddress)
1332 {
1333 BuildMI(BB, Opc, 2).addReg(Tmp1)
1334 .addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1335 }
1336 else if(Address.getOpcode() == ISD::FrameIndex)
1337 {
Nate Begeman58f718c2005-03-30 02:23:08 +00001338 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
1339 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001340 }
1341 else
1342 {
1343 int offset;
1344 SelectAddr(Address, Tmp2, offset);
1345 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1346 }
1347 return;
1348 }
1349 case ISD::EXTLOAD:
1350 case ISD::SEXTLOAD:
1351 case ISD::ZEXTLOAD:
1352 case ISD::LOAD:
1353 case ISD::CopyFromReg:
1354 case ISD::CALL:
1355 case ISD::DYNAMIC_STACKALLOC:
1356 ExprMap.erase(N);
1357 SelectExpr(N);
1358 return;
1359 }
1360 assert(0 && "Should not be reached!");
1361}
1362
1363
1364/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
1365/// into a machine code representation using pattern matching and a machine
1366/// description file.
1367///
1368FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) {
1369 return new ISel(TM);
Chris Lattner246fa632005-03-24 06:16:18 +00001370}
1371