blob: bfb03275eb8ed9b0af4e0f4fe3688438f1c8ac0d [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 Begeman96fc6812005-03-31 02:05:53 +0000324 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
325 Args[i].first, PtrOff);
326 MemOps.push_back(Store);
Nate Begeman74d73452005-03-31 00:15:26 +0000327 // Float varargs are always shadowed in available integer registers
328 if (GPR_remaining > 0) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000329 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff);
Nate Begeman74d73452005-03-31 00:15:26 +0000330 MemOps.push_back(Load);
Nate Begeman96fc6812005-03-31 02:05:53 +0000331 args_to_use.push_back(DAG.getCopyToReg(Load, Load,
Nate Begeman74d73452005-03-31 00:15:26 +0000332 GPR[GPR_idx]));
333 }
334 if (GPR_remaining > 1 && MVT::f64 == ArgVT) {
335 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
336 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Nate Begeman96fc6812005-03-31 02:05:53 +0000337 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff);
Nate Begeman74d73452005-03-31 00:15:26 +0000338 MemOps.push_back(Load);
Nate Begeman96fc6812005-03-31 02:05:53 +0000339 args_to_use.push_back(DAG.getCopyToReg(Load, Load,
Nate Begeman74d73452005-03-31 00:15:26 +0000340 GPR[GPR_idx+1]));
341 }
Nate Begemanf7e43382005-03-26 07:46:36 +0000342 }
Nate Begeman74d73452005-03-31 00:15:26 +0000343 args_to_use.push_back(DAG.getCopyToReg(Chain, Args[i].first,
344 FPR[FPR_idx]));
Nate Begeman307e7442005-03-26 01:28:53 +0000345 --FPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000346 ++FPR_idx;
Nate Begemanf7e43382005-03-26 07:46:36 +0000347 // If we have any FPRs remaining, we may also have GPRs remaining.
348 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
349 // GPRs.
Nate Begeman74d73452005-03-31 00:15:26 +0000350 if (GPR_remaining > 0) {
351 --GPR_remaining;
352 ++GPR_idx;
353 }
354 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
355 --GPR_remaining;
356 ++GPR_idx;
357 }
Nate Begeman307e7442005-03-26 01:28:53 +0000358 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000359 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
360 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000361 }
Nate Begemanf7e43382005-03-26 07:46:36 +0000362 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
Nate Begeman307e7442005-03-26 01:28:53 +0000363 break;
364 }
Nate Begemana9795f82005-03-24 04:41:43 +0000365 }
Nate Begeman74d73452005-03-31 00:15:26 +0000366 if (!MemOps.empty())
367 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
Nate Begemana9795f82005-03-24 04:41:43 +0000368 }
369
370 std::vector<MVT::ValueType> RetVals;
371 MVT::ValueType RetTyVT = getValueType(RetTy);
372 if (RetTyVT != MVT::isVoid)
373 RetVals.push_back(RetTyVT);
374 RetVals.push_back(MVT::Other);
375
376 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
377 Chain, Callee, args_to_use), 0);
378 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
379 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
380 DAG.getConstant(NumBytes, getPointerTy()));
381 return std::make_pair(TheCall, Chain);
382}
383
384std::pair<SDOperand, SDOperand>
385PPC32TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
386 //vastart just returns the address of the VarArgsFrameIndex slot.
387 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
388}
389
390std::pair<SDOperand,SDOperand> PPC32TargetLowering::
391LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
392 const Type *ArgTy, SelectionDAG &DAG) {
Nate Begemanc7b09f12005-03-25 08:34:25 +0000393 MVT::ValueType ArgVT = getValueType(ArgTy);
394 SDOperand Result;
395 if (!isVANext) {
396 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList);
397 } else {
398 unsigned Amt;
399 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
400 Amt = 4;
401 else {
402 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
403 "Other types should have been promoted for varargs!");
404 Amt = 8;
405 }
406 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
407 DAG.getConstant(Amt, VAList.getValueType()));
408 }
409 return std::make_pair(Result, Chain);
Nate Begemana9795f82005-03-24 04:41:43 +0000410}
411
412
413std::pair<SDOperand, SDOperand> PPC32TargetLowering::
414LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
415 SelectionDAG &DAG) {
Nate Begeman01d05262005-03-30 01:45:43 +0000416 assert(0 && "LowerFrameReturnAddress unimplemented");
Nate Begemana9795f82005-03-24 04:41:43 +0000417 abort();
418}
419
420namespace {
421
422//===--------------------------------------------------------------------===//
423/// ISel - PPC32 specific code to select PPC32 machine instructions for
424/// SelectionDAG operations.
425//===--------------------------------------------------------------------===//
426class ISel : public SelectionDAGISel {
427
428 /// Comment Here.
429 PPC32TargetLowering PPC32Lowering;
430
431 /// ExprMap - As shared expressions are codegen'd, we keep track of which
432 /// vreg the value is produced in, so we only emit one copy of each compiled
433 /// tree.
434 std::map<SDOperand, unsigned> ExprMap;
Nate Begemanc7b09f12005-03-25 08:34:25 +0000435
436 unsigned GlobalBaseReg;
437 bool GlobalBaseInitialized;
Nate Begemana9795f82005-03-24 04:41:43 +0000438
439public:
440 ISel(TargetMachine &TM) : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM)
441 {}
442
Nate Begemanc7b09f12005-03-25 08:34:25 +0000443 /// runOnFunction - Override this function in order to reset our per-function
444 /// variables.
445 virtual bool runOnFunction(Function &Fn) {
446 // Make sure we re-emit a set of the global base reg if necessary
447 GlobalBaseInitialized = false;
448 return SelectionDAGISel::runOnFunction(Fn);
449 }
450
Nate Begemana9795f82005-03-24 04:41:43 +0000451 /// InstructionSelectBasicBlock - This callback is invoked by
452 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
453 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
454 DEBUG(BB->dump());
455 // Codegen the basic block.
456 Select(DAG.getRoot());
457
458 // Clear state used for selection.
459 ExprMap.clear();
460 }
461
Nate Begemanc7b09f12005-03-25 08:34:25 +0000462 unsigned ISel::getGlobalBaseReg();
Nate Begemana9795f82005-03-24 04:41:43 +0000463 unsigned SelectExpr(SDOperand N);
464 unsigned SelectExprFP(SDOperand N, unsigned Result);
465 void Select(SDOperand N);
466
467 void SelectAddr(SDOperand N, unsigned& Reg, int& offset);
468 void SelectBranchCC(SDOperand N);
469};
470
471/// canUseAsImmediateForOpcode - This method returns a value indicating whether
472/// the ConstantSDNode N can be used as an immediate to Opcode. The return
473/// values are either 0, 1 or 2. 0 indicates that either N is not a
474/// ConstantSDNode, or is not suitable for use by that opcode. A return value
475/// of 1 indicates that the constant may be used in normal immediate form. A
476/// return value of 2 indicates that the constant may be used in shifted
477/// immediate form. If the return value is nonzero, the constant value is
478/// placed in Imm.
479///
480static unsigned canUseAsImmediateForOpcode(SDOperand N, unsigned Opcode,
481 unsigned& Imm) {
482 if (N.getOpcode() != ISD::Constant) return 0;
483
484 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
485
486 switch(Opcode) {
487 default: return 0;
488 case ISD::ADD:
489 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
490 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
491 break;
492 case ISD::AND:
493 case ISD::XOR:
494 case ISD::OR:
495 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
496 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
497 break;
Nate Begeman307e7442005-03-26 01:28:53 +0000498 case ISD::MUL:
499 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
500 break;
Nate Begemana9795f82005-03-24 04:41:43 +0000501 }
502 return 0;
503}
504}
505
Nate Begemanc7b09f12005-03-25 08:34:25 +0000506/// getGlobalBaseReg - Output the instructions required to put the
507/// base address to use for accessing globals into a register.
508///
509unsigned ISel::getGlobalBaseReg() {
510 if (!GlobalBaseInitialized) {
511 // Insert the set of GlobalBaseReg into the first MBB of the function
512 MachineBasicBlock &FirstMBB = BB->getParent()->front();
513 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
514 GlobalBaseReg = MakeReg(MVT::i32);
515 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
516 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR);
517 GlobalBaseInitialized = true;
518 }
519 return GlobalBaseReg;
520}
521
Nate Begemana9795f82005-03-24 04:41:43 +0000522//Check to see if the load is a constant offset from a base register
523void ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
524{
Nate Begeman96fc6812005-03-31 02:05:53 +0000525 unsigned imm = 0, opcode = N.getOpcode();
526 if (N.getOpcode() == ISD::ADD)
527 if (1 == canUseAsImmediateForOpcode(N.getOperand(1), opcode, imm)) {
528 Reg = SelectExpr(N.getOperand(0));
529 offset = imm;
530 return;
531 }
Nate Begemana9795f82005-03-24 04:41:43 +0000532 Reg = SelectExpr(N);
533 offset = 0;
534 return;
535}
536
537void ISel::SelectBranchCC(SDOperand N)
538{
539 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
540 MachineBasicBlock *Dest =
541 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
542 unsigned Opc;
543
544 Select(N.getOperand(0)); //chain
545 SDOperand CC = N.getOperand(1);
546
Nate Begeman23afcfb2005-03-29 22:48:55 +0000547 //Give up and do the stupid thing
Nate Begemana9795f82005-03-24 04:41:43 +0000548 unsigned Tmp1 = SelectExpr(CC);
Nate Begeman23afcfb2005-03-29 22:48:55 +0000549 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(0);
550 BuildMI(BB, PPC::BNE, 2).addReg(PPC::CR0).addMBB(Dest);
Nate Begemana9795f82005-03-24 04:41:43 +0000551 return;
552}
553
554unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
555{
556 unsigned Tmp1, Tmp2, Tmp3;
557 unsigned Opc = 0;
558 SDNode *Node = N.Val;
559 MVT::ValueType DestType = N.getValueType();
560 unsigned opcode = N.getOpcode();
561
562 switch (opcode) {
563 default:
564 Node->dump();
565 assert(0 && "Node not handled!\n");
566
Nate Begeman23afcfb2005-03-29 22:48:55 +0000567 case ISD::SELECT: {
568 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
569
570 // FIXME: generate FSEL here
571
572 // Create an iterator with which to insert the MBB for copying the false
573 // value and the MBB to hold the PHI instruction for this SetCC.
574 MachineBasicBlock *thisMBB = BB;
575 const BasicBlock *LLVM_BB = BB->getBasicBlock();
576 ilist<MachineBasicBlock>::iterator It = BB;
577 ++It;
578
579 // thisMBB:
580 // ...
581 // TrueVal = ...
582 // cmpTY cr0, r1, r2
583 // bCC copy1MBB
584 // fallthrough --> copy0MBB
585 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(0);
586 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
587 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
588 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
589 BuildMI(BB, PPC::BNE, 2).addReg(PPC::CR0).addMBB(sinkMBB);
590 MachineFunction *F = BB->getParent();
591 F->getBasicBlockList().insert(It, copy0MBB);
592 F->getBasicBlockList().insert(It, sinkMBB);
593 // Update machine-CFG edges
594 BB->addSuccessor(copy0MBB);
595 BB->addSuccessor(sinkMBB);
596
597 // copy0MBB:
598 // %FalseValue = ...
599 // # fallthrough to sinkMBB
600 BB = copy0MBB;
601 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
602 // Update machine-CFG edges
603 BB->addSuccessor(sinkMBB);
604
605 // sinkMBB:
606 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
607 // ...
608 BB = sinkMBB;
609 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
610 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
611 return Result;
612 }
Nate Begemana9795f82005-03-24 04:41:43 +0000613
614 case ISD::FP_ROUND:
615 assert (DestType == MVT::f32 &&
616 N.getOperand(0).getValueType() == MVT::f64 &&
617 "only f64 to f32 conversion supported here");
618 Tmp1 = SelectExpr(N.getOperand(0));
619 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
620 return Result;
621
622 case ISD::FP_EXTEND:
623 assert (DestType == MVT::f64 &&
624 N.getOperand(0).getValueType() == MVT::f32 &&
625 "only f32 to f64 conversion supported here");
626 Tmp1 = SelectExpr(N.getOperand(0));
627 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
628 return Result;
629
630 case ISD::CopyFromReg:
Nate Begemanf2622612005-03-26 02:17:46 +0000631 if (Result == 1)
632 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
633 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
634 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
635 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000636
Nate Begemana9795f82005-03-24 04:41:43 +0000637 case ISD::ConstantFP:
Nate Begemanca12a2b2005-03-28 22:28:37 +0000638 assert(0 && "ISD::ConstantFP Unimplemented");
Nate Begemana9795f82005-03-24 04:41:43 +0000639 abort();
640
641 case ISD::MUL:
642 case ISD::ADD:
643 case ISD::SUB:
644 case ISD::SDIV:
645 switch( opcode ) {
646 case ISD::MUL: Opc = DestType == MVT::f64 ? PPC::FMUL : PPC::FMULS; break;
647 case ISD::ADD: Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS; break;
648 case ISD::SUB: Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS; break;
649 case ISD::SDIV: Opc = DestType == MVT::f64 ? PPC::FDIV : PPC::FDIVS; break;
650 };
Nate Begemana9795f82005-03-24 04:41:43 +0000651 Tmp1 = SelectExpr(N.getOperand(0));
652 Tmp2 = SelectExpr(N.getOperand(1));
653 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
654 return Result;
655
Nate Begemana9795f82005-03-24 04:41:43 +0000656 case ISD::UINT_TO_FP:
Nate Begemanfdcf3412005-03-30 19:38:35 +0000657 case ISD::SINT_TO_FP: {
658 assert (N.getOperand(0).getValueType() == MVT::i32
659 && "int to float must operate on i32");
660 bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
661 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
662 Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into
663 Tmp3 = MakeReg(MVT::i32); // temp reg to hold the conversion constant
664 unsigned ConstF = MakeReg(MVT::f64); // temp reg to hold the fp constant
665
666 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
667 MachineConstantPool *CP = BB->getParent()->getConstantPool();
668
669 // FIXME: pull this FP constant generation stuff out into something like
670 // the simple ISel's getReg.
671 if (IsUnsigned) {
672 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000000p52);
673 unsigned CPI = CP->getConstantPoolIndex(CFP);
674 // Load constant fp value
675 unsigned Tmp4 = MakeReg(MVT::i32);
676 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg())
677 .addConstantPoolIndex(CPI);
678 BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
679 // Store the hi & low halves of the fp value, currently in int regs
680 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
681 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
682 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp1), FrameIdx, 4);
683 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
684 // Generate the return value with a subtract
685 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
686 } else {
687 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000008p52);
688 unsigned CPI = CP->getConstantPoolIndex(CFP);
689 // Load constant fp value
690 unsigned Tmp4 = MakeReg(MVT::i32);
691 unsigned TmpL = MakeReg(MVT::i32);
692 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg())
693 .addConstantPoolIndex(CPI);
694 BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
695 // Store the hi & low halves of the fp value, currently in int regs
696 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
697 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
698 BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
699 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
700 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
701 // Generate the return value with a subtract
702 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
703 }
704 return Result;
705 }
Nate Begemana9795f82005-03-24 04:41:43 +0000706 }
707 assert(0 && "should not get here");
708 return 0;
709}
710
711unsigned ISel::SelectExpr(SDOperand N) {
712 unsigned Result;
713 unsigned Tmp1, Tmp2, Tmp3;
714 unsigned Opc = 0;
715 unsigned opcode = N.getOpcode();
716
717 SDNode *Node = N.Val;
718 MVT::ValueType DestType = N.getValueType();
719
720 unsigned &Reg = ExprMap[N];
721 if (Reg) return Reg;
722
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000723 if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::ADD_PARTS &&
724 N.getOpcode() != ISD::SUB_PARTS)
Nate Begemana9795f82005-03-24 04:41:43 +0000725 Reg = Result = (N.getValueType() != MVT::Other) ?
726 MakeReg(N.getValueType()) : 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000727 else {
728 // If this is a call instruction, make sure to prepare ALL of the result
729 // values as well as the chain.
730 if (N.getOpcode() == ISD::CALL) {
731 if (Node->getNumValues() == 1)
732 Reg = Result = 1; // Void call, just a chain.
733 else {
734 Result = MakeReg(Node->getValueType(0));
735 ExprMap[N.getValue(0)] = Result;
736 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
737 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
738 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
739 }
740 } else {
741 Result = MakeReg(Node->getValueType(0));
742 ExprMap[N.getValue(0)] = Result;
743 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
744 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
745 }
746 }
747
748 if (DestType == MVT::f64 || DestType == MVT::f32)
Nate Begeman74d73452005-03-31 00:15:26 +0000749 if (ISD::LOAD != opcode && ISD::EXTLOAD != opcode)
750 return SelectExprFP(N, Result);
Nate Begemana9795f82005-03-24 04:41:43 +0000751
752 switch (opcode) {
753 default:
754 Node->dump();
755 assert(0 && "Node not handled!\n");
756
757 case ISD::DYNAMIC_STACKALLOC:
Nate Begeman5e966612005-03-24 06:28:42 +0000758 // Generate both result values. FIXME: Need a better commment here?
759 if (Result != 1)
760 ExprMap[N.getValue(1)] = 1;
761 else
762 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
763
764 // FIXME: We are currently ignoring the requested alignment for handling
765 // greater than the stack alignment. This will need to be revisited at some
766 // point. Align = N.getOperand(2);
767 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
768 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
769 std::cerr << "Cannot allocate stack object with greater alignment than"
770 << " the stack alignment yet!";
771 abort();
772 }
773 Select(N.getOperand(0));
774 Tmp1 = SelectExpr(N.getOperand(1));
775 // Subtract size from stack pointer, thereby allocating some space.
776 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
777 // Put a pointer to the space into the result register by copying the SP
778 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
779 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000780
781 case ISD::ConstantPool:
Nate Begemanca12a2b2005-03-28 22:28:37 +0000782 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
783 Tmp2 = MakeReg(MVT::i32);
784 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp2).addReg(getGlobalBaseReg())
785 .addConstantPoolIndex(Tmp1);
786 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
787 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000788
789 case ISD::FrameIndex:
Nate Begemanf3d08f32005-03-29 00:03:27 +0000790 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Nate Begeman58f718c2005-03-30 02:23:08 +0000791 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
Nate Begemanf3d08f32005-03-29 00:03:27 +0000792 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000793
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000794 case ISD::GlobalAddress: {
795 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanca12a2b2005-03-28 22:28:37 +0000796 Tmp1 = MakeReg(MVT::i32);
Nate Begemanc7b09f12005-03-25 08:34:25 +0000797 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
798 .addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000799 if (GV->hasWeakLinkage() || GV->isExternal()) {
800 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
801 } else {
802 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
803 }
804 return Result;
805 }
806
Nate Begeman5e966612005-03-24 06:28:42 +0000807 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +0000808 case ISD::EXTLOAD:
809 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000810 case ISD::SEXTLOAD: {
Nate Begeman9db505c2005-03-28 19:36:43 +0000811 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
812 Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType();
Nate Begeman74d73452005-03-31 00:15:26 +0000813 bool sext = (ISD::SEXTLOAD == opcode);
814 bool byte = (MVT::i8 == TypeBeingLoaded);
815
Nate Begeman5e966612005-03-24 06:28:42 +0000816 // Make sure we generate both values.
817 if (Result != 1)
818 ExprMap[N.getValue(1)] = 1; // Generate the token
819 else
820 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
821
822 SDOperand Chain = N.getOperand(0);
823 SDOperand Address = N.getOperand(1);
824 Select(Chain);
825
Nate Begeman9db505c2005-03-28 19:36:43 +0000826 switch (TypeBeingLoaded) {
Nate Begeman74d73452005-03-31 00:15:26 +0000827 default: Node->dump(); assert(0 && "Cannot load this type!");
Nate Begeman9db505c2005-03-28 19:36:43 +0000828 case MVT::i1: Opc = PPC::LBZ; break;
829 case MVT::i8: Opc = PPC::LBZ; break;
830 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
831 case MVT::i32: Opc = PPC::LWZ; break;
Nate Begeman74d73452005-03-31 00:15:26 +0000832 case MVT::f32: Opc = PPC::LFS; break;
833 case MVT::f64: Opc = PPC::LFD; break;
Nate Begeman5e966612005-03-24 06:28:42 +0000834 }
835
Nate Begeman74d73452005-03-31 00:15:26 +0000836 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
837 Tmp1 = MakeReg(MVT::i32);
838 int CPI = CP->getIndex();
839 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
840 .addConstantPoolIndex(CPI);
841 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +0000842 }
Nate Begeman74d73452005-03-31 00:15:26 +0000843 else if(Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman58f718c2005-03-30 02:23:08 +0000844 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
845 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
Nate Begeman5e966612005-03-24 06:28:42 +0000846 } else {
847 int offset;
848 SelectAddr(Address, Tmp1, offset);
849 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
850 }
851 return Result;
852 }
853
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000854 case ISD::CALL: {
855 // Lower the chain for this call.
856 Select(N.getOperand(0));
857 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
Nate Begeman74d73452005-03-31 00:15:26 +0000858
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000859 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Nate Begeman74d73452005-03-31 00:15:26 +0000860 Select(N.getOperand(i));
861
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000862 // Emit the correct call instruction based on the type of symbol called.
863 if (GlobalAddressSDNode *GASD =
864 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
865 BuildMI(BB, PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(), true);
866 } else if (ExternalSymbolSDNode *ESSDN =
867 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
868 BuildMI(BB, PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(), true);
869 } else {
870 Tmp1 = SelectExpr(N.getOperand(1));
871 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
872 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
873 BuildMI(BB, PPC::CALLindirect, 3).addImm(20).addImm(0).addReg(PPC::R12);
874 }
875
876 switch (Node->getValueType(0)) {
877 default: assert(0 && "Unknown value type for call result!");
878 case MVT::Other: return 1;
879 case MVT::i1:
880 case MVT::i8:
881 case MVT::i16:
882 case MVT::i32:
Nate Begemanc7b09f12005-03-25 08:34:25 +0000883 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000884 if (Node->getValueType(1) == MVT::i32)
Nate Begemanc7b09f12005-03-25 08:34:25 +0000885 BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R4).addReg(PPC::R4);
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000886 break;
887 case MVT::f32:
888 case MVT::f64:
889 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
890 break;
891 }
892 return Result+N.ResNo;
893 }
Nate Begemana9795f82005-03-24 04:41:43 +0000894
895 case ISD::SIGN_EXTEND:
896 case ISD::SIGN_EXTEND_INREG:
897 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman9db505c2005-03-28 19:36:43 +0000898 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
899 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
900 case MVT::i16:
901 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
902 break;
903 case MVT::i8:
904 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
905 break;
Nate Begeman74747862005-03-29 22:24:51 +0000906 case MVT::i1:
907 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
908 break;
Nate Begeman9db505c2005-03-28 19:36:43 +0000909 }
Nate Begemana9795f82005-03-24 04:41:43 +0000910 return Result;
911
912 case ISD::ZERO_EXTEND_INREG:
913 Tmp1 = SelectExpr(N.getOperand(0));
914 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
Nate Begeman9db505c2005-03-28 19:36:43 +0000915 default: Node->dump(); assert(0 && "Unhandled ZERO_EXTEND type"); break;
Nate Begemana9795f82005-03-24 04:41:43 +0000916 case MVT::i16: Tmp2 = 16; break;
917 case MVT::i8: Tmp2 = 24; break;
918 case MVT::i1: Tmp2 = 31; break;
919 }
Nate Begeman33162522005-03-29 21:54:38 +0000920 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(0).addImm(Tmp2)
921 .addImm(31);
Nate Begemana9795f82005-03-24 04:41:43 +0000922 return Result;
923
Nate Begemana9795f82005-03-24 04:41:43 +0000924 case ISD::CopyFromReg:
925 if (Result == 1)
926 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
927 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
928 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
929 return Result;
930
931 case ISD::SHL:
Nate Begeman5e966612005-03-24 06:28:42 +0000932 Tmp1 = SelectExpr(N.getOperand(0));
933 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
934 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +0000935 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
Nate Begeman5e966612005-03-24 06:28:42 +0000936 .addImm(31-Tmp2);
937 } else {
938 Tmp2 = SelectExpr(N.getOperand(1));
939 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
940 }
941 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000942
Nate Begeman5e966612005-03-24 06:28:42 +0000943 case ISD::SRL:
944 Tmp1 = SelectExpr(N.getOperand(0));
945 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
946 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +0000947 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2)
Nate Begeman5e966612005-03-24 06:28:42 +0000948 .addImm(Tmp2).addImm(31);
949 } else {
950 Tmp2 = SelectExpr(N.getOperand(1));
951 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
952 }
953 return Result;
954
955 case ISD::SRA:
956 Tmp1 = SelectExpr(N.getOperand(0));
957 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
958 Tmp2 = CN->getValue() & 0x1F;
959 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
960 } else {
961 Tmp2 = SelectExpr(N.getOperand(1));
962 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
963 }
964 return Result;
965
Nate Begemana9795f82005-03-24 04:41:43 +0000966 case ISD::ADD:
967 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
968 Tmp1 = SelectExpr(N.getOperand(0));
969 switch(canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
970 default: assert(0 && "unhandled result code");
971 case 0: // No immediate
972 Tmp2 = SelectExpr(N.getOperand(1));
973 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
974 break;
975 case 1: // Low immediate
976 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
977 break;
978 case 2: // Shifted immediate
979 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
980 break;
981 }
982 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000983
Nate Begemana9795f82005-03-24 04:41:43 +0000984 case ISD::AND:
985 case ISD::OR:
986 case ISD::XOR:
987 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
988 Tmp1 = SelectExpr(N.getOperand(0));
989 switch(canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
990 default: assert(0 && "unhandled result code");
991 case 0: // No immediate
992 Tmp2 = SelectExpr(N.getOperand(1));
993 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +0000994 case ISD::AND: Opc = PPC::AND; break;
995 case ISD::OR: Opc = PPC::OR; break;
996 case ISD::XOR: Opc = PPC::XOR; break;
Nate Begemana9795f82005-03-24 04:41:43 +0000997 }
Nate Begeman5e966612005-03-24 06:28:42 +0000998 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +0000999 break;
1000 case 1: // Low immediate
1001 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +00001002 case ISD::AND: Opc = PPC::ANDIo; break;
1003 case ISD::OR: Opc = PPC::ORI; break;
1004 case ISD::XOR: Opc = PPC::XORI; 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 case 2: // Shifted immediate
1009 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +00001010 case ISD::AND: Opc = PPC::ANDISo; break;
1011 case ISD::OR: Opc = PPC::ORIS; break;
1012 case ISD::XOR: Opc = PPC::XORIS; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001013 }
Nate Begeman5e966612005-03-24 06:28:42 +00001014 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001015 break;
1016 }
1017 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001018
1019 case ISD::SUB:
1020 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1021 Tmp1 = SelectExpr(N.getOperand(0));
1022 Tmp2 = SelectExpr(N.getOperand(1));
1023 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
1024 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001025
Nate Begeman5e966612005-03-24 06:28:42 +00001026 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001027 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1028 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman307e7442005-03-26 01:28:53 +00001029 if (1 == canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
1030 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1031 else {
1032 Tmp2 = SelectExpr(N.getOperand(1));
1033 BuildMI(BB, PPC::MULLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1034 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001035 return Result;
1036
Nate Begemanf3d08f32005-03-29 00:03:27 +00001037 case ISD::SDIV:
1038 case ISD::UDIV:
1039 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1040 Tmp1 = SelectExpr(N.getOperand(0));
1041 Tmp2 = SelectExpr(N.getOperand(1));
1042 Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW;
1043 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1044 return Result;
1045
1046 case ISD::UREM:
1047 case ISD::SREM: {
1048 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1049 Tmp1 = SelectExpr(N.getOperand(0));
1050 Tmp2 = SelectExpr(N.getOperand(1));
1051 Tmp3 = MakeReg(MVT::i32);
1052 unsigned Tmp4 = MakeReg(MVT::i32);
1053 Opc = (ISD::UREM == opcode) ? PPC::DIVWU : PPC::DIVW;
1054 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1055 BuildMI(BB, PPC::MULLW, 2, Tmp4).addReg(Tmp3).addReg(Tmp2);
1056 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp4).addReg(Tmp1);
1057 return Result;
1058 }
1059
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001060 case ISD::ADD_PARTS:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001061 case ISD::SUB_PARTS: {
1062 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1063 "Not an i64 add/sub!");
1064 // Emit all of the operands.
1065 std::vector<unsigned> InVals;
1066 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
1067 InVals.push_back(SelectExpr(N.getOperand(i)));
1068 if (N.getOpcode() == ISD::ADD_PARTS) {
Nate Begemanf70b5762005-03-28 23:08:54 +00001069 BuildMI(BB, PPC::ADDC, 2, Result+1).addReg(InVals[0]).addReg(InVals[2]);
1070 BuildMI(BB, PPC::ADDE, 2, Result).addReg(InVals[1]).addReg(InVals[3]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001071 } else {
Nate Begemanf70b5762005-03-28 23:08:54 +00001072 BuildMI(BB, PPC::SUBFC, 2, Result+1).addReg(InVals[2]).addReg(InVals[0]);
1073 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(InVals[3]).addReg(InVals[1]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001074 }
1075 return Result+N.ResNo;
1076 }
1077
Nate Begemana9795f82005-03-24 04:41:43 +00001078 case ISD::FP_TO_UINT:
1079 case ISD::FP_TO_SINT:
Nate Begeman01d05262005-03-30 01:45:43 +00001080 assert(0 && "FP_TO_S/UINT unimplemented");
Nate Begemana9795f82005-03-24 04:41:43 +00001081 abort();
1082
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001083 case ISD::SETCC:
Nate Begeman33162522005-03-29 21:54:38 +00001084 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1085 bool U = false;
1086 bool IsInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
1087
1088 switch (SetCC->getCondition()) {
1089 default: Node->dump(); assert(0 && "Unknown comparison!");
1090 case ISD::SETEQ: Opc = PPC::BEQ; break;
1091 case ISD::SETNE: Opc = PPC::BNE; break;
1092 case ISD::SETULT: U = true;
1093 case ISD::SETLT: Opc = PPC::BLT; break;
1094 case ISD::SETULE: U = true;
1095 case ISD::SETLE: Opc = PPC::BLE; break;
1096 case ISD::SETUGT: U = true;
1097 case ISD::SETGT: Opc = PPC::BGT; break;
1098 case ISD::SETUGE: U = true;
1099 case ISD::SETGE: Opc = PPC::BGE; break;
1100 }
1101
1102 // FIXME: Is there a situation in which we would ever need to emit fcmpo?
1103 static const unsigned CompareOpcodes[] =
1104 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
1105 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
1106
1107 // Create an iterator with which to insert the MBB for copying the false
1108 // value and the MBB to hold the PHI instruction for this SetCC.
1109 MachineBasicBlock *thisMBB = BB;
1110 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1111 ilist<MachineBasicBlock>::iterator It = BB;
1112 ++It;
1113
1114 // thisMBB:
1115 // ...
1116 // cmpTY cr0, r1, r2
1117 // %TrueValue = li 1
1118 // bCC sinkMBB
1119 Tmp1 = SelectExpr(N.getOperand(0));
1120 Tmp2 = SelectExpr(N.getOperand(1));
1121 BuildMI(BB, CompareOpc, 2, PPC::CR0).addReg(Tmp1).addReg(Tmp2);
1122 unsigned TrueValue = MakeReg(MVT::i32);
1123 BuildMI(BB, PPC::LI, 1, TrueValue).addSImm(1);
1124 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1125 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1126 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
1127 MachineFunction *F = BB->getParent();
1128 F->getBasicBlockList().insert(It, copy0MBB);
1129 F->getBasicBlockList().insert(It, sinkMBB);
1130 // Update machine-CFG edges
1131 BB->addSuccessor(copy0MBB);
1132 BB->addSuccessor(sinkMBB);
1133
1134 // copy0MBB:
1135 // %FalseValue = li 0
1136 // fallthrough
1137 BB = copy0MBB;
1138 unsigned FalseValue = MakeReg(MVT::i32);
1139 BuildMI(BB, PPC::LI, 1, FalseValue).addSImm(0);
1140 // Update machine-CFG edges
1141 BB->addSuccessor(sinkMBB);
1142
1143 // sinkMBB:
1144 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1145 // ...
1146 BB = sinkMBB;
1147 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1148 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1149 return Result;
1150 }
1151 assert(0 && "Is this legal?");
1152 return 0;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001153
Nate Begeman74747862005-03-29 22:24:51 +00001154 case ISD::SELECT: {
1155 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1156
1157 // Create an iterator with which to insert the MBB for copying the false
1158 // value and the MBB to hold the PHI instruction for this SetCC.
1159 MachineBasicBlock *thisMBB = BB;
1160 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1161 ilist<MachineBasicBlock>::iterator It = BB;
1162 ++It;
1163
1164 // thisMBB:
1165 // ...
1166 // TrueVal = ...
1167 // cmpTY cr0, r1, r2
1168 // bCC copy1MBB
1169 // fallthrough --> copy0MBB
1170 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(0);
1171 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1172 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1173 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
1174 BuildMI(BB, PPC::BNE, 2).addReg(PPC::CR0).addMBB(sinkMBB);
1175 MachineFunction *F = BB->getParent();
1176 F->getBasicBlockList().insert(It, copy0MBB);
1177 F->getBasicBlockList().insert(It, sinkMBB);
1178 // Update machine-CFG edges
1179 BB->addSuccessor(copy0MBB);
1180 BB->addSuccessor(sinkMBB);
1181
1182 // copy0MBB:
1183 // %FalseValue = ...
1184 // # fallthrough to sinkMBB
1185 BB = copy0MBB;
1186 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
1187 // Update machine-CFG edges
1188 BB->addSuccessor(sinkMBB);
1189
1190 // sinkMBB:
1191 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1192 // ...
1193 BB = sinkMBB;
1194 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1195 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1196
1197 // FIXME: Select i64?
1198 return Result;
1199 }
Nate Begemana9795f82005-03-24 04:41:43 +00001200
1201 case ISD::Constant:
1202 switch (N.getValueType()) {
1203 default: assert(0 && "Cannot use constants of this type!");
1204 case MVT::i1:
1205 BuildMI(BB, PPC::LI, 1, Result)
1206 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
1207 break;
1208 case MVT::i32:
1209 {
1210 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
1211 if (v < 32768 && v >= -32768) {
1212 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
1213 } else {
Nate Begeman5e966612005-03-24 06:28:42 +00001214 Tmp1 = MakeReg(MVT::i32);
1215 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
1216 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
Nate Begemana9795f82005-03-24 04:41:43 +00001217 }
1218 }
1219 }
1220 return Result;
1221 }
1222
1223 return 0;
1224}
1225
1226void ISel::Select(SDOperand N) {
1227 unsigned Tmp1, Tmp2, Opc;
1228 unsigned opcode = N.getOpcode();
1229
1230 if (!ExprMap.insert(std::make_pair(N, 1)).second)
1231 return; // Already selected.
1232
1233 SDNode *Node = N.Val;
1234
1235 switch (Node->getOpcode()) {
1236 default:
1237 Node->dump(); std::cerr << "\n";
1238 assert(0 && "Node not handled yet!");
1239 case ISD::EntryToken: return; // Noop
1240 case ISD::TokenFactor:
1241 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1242 Select(Node->getOperand(i));
1243 return;
1244 case ISD::ADJCALLSTACKDOWN:
1245 case ISD::ADJCALLSTACKUP:
1246 Select(N.getOperand(0));
1247 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1248 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? PPC::ADJCALLSTACKDOWN :
1249 PPC::ADJCALLSTACKUP;
1250 BuildMI(BB, Opc, 1).addImm(Tmp1);
1251 return;
1252 case ISD::BR: {
1253 MachineBasicBlock *Dest =
1254 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
Nate Begemana9795f82005-03-24 04:41:43 +00001255 Select(N.getOperand(0));
1256 BuildMI(BB, PPC::B, 1).addMBB(Dest);
1257 return;
1258 }
1259 case ISD::BRCOND:
1260 SelectBranchCC(N);
1261 return;
1262 case ISD::CopyToReg:
1263 Select(N.getOperand(0));
1264 Tmp1 = SelectExpr(N.getOperand(1));
1265 Tmp2 = cast<RegSDNode>(N)->getReg();
1266
1267 if (Tmp1 != Tmp2) {
1268 if (N.getOperand(1).getValueType() == MVT::f64 ||
1269 N.getOperand(1).getValueType() == MVT::f32)
1270 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
1271 else
1272 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1273 }
1274 return;
1275 case ISD::ImplicitDef:
1276 Select(N.getOperand(0));
1277 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
1278 return;
1279 case ISD::RET:
1280 switch (N.getNumOperands()) {
1281 default:
1282 assert(0 && "Unknown return instruction!");
1283 case 3:
1284 assert(N.getOperand(1).getValueType() == MVT::i32 &&
1285 N.getOperand(2).getValueType() == MVT::i32 &&
1286 "Unknown two-register value!");
1287 Select(N.getOperand(0));
1288 Tmp1 = SelectExpr(N.getOperand(1));
1289 Tmp2 = SelectExpr(N.getOperand(2));
1290 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
1291 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp2).addReg(Tmp2);
1292 break;
1293 case 2:
1294 Select(N.getOperand(0));
1295 Tmp1 = SelectExpr(N.getOperand(1));
1296 switch (N.getOperand(1).getValueType()) {
1297 default:
1298 assert(0 && "Unknown return type!");
1299 case MVT::f64:
1300 case MVT::f32:
1301 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
1302 break;
1303 case MVT::i32:
1304 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
1305 break;
1306 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001307 case 1:
1308 Select(N.getOperand(0));
1309 break;
Nate Begemana9795f82005-03-24 04:41:43 +00001310 }
1311 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
1312 return;
Nate Begemana9795f82005-03-24 04:41:43 +00001313 case ISD::TRUNCSTORE:
1314 case ISD::STORE:
1315 {
1316 SDOperand Chain = N.getOperand(0);
1317 SDOperand Value = N.getOperand(1);
1318 SDOperand Address = N.getOperand(2);
1319 Select(Chain);
1320
1321 Tmp1 = SelectExpr(Value); //value
1322
1323 if (opcode == ISD::STORE) {
1324 switch(Value.getValueType()) {
1325 default: assert(0 && "unknown Type in store");
1326 case MVT::i32: Opc = PPC::STW; break;
1327 case MVT::f64: Opc = PPC::STFD; break;
1328 case MVT::f32: Opc = PPC::STFS; break;
1329 }
1330 } else { //ISD::TRUNCSTORE
1331 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1332 default: assert(0 && "unknown Type in store");
1333 case MVT::i1: //FIXME: DAG does not promote this load
1334 case MVT::i8: Opc = PPC::STB; break;
1335 case MVT::i16: Opc = PPC::STH; break;
1336 }
1337 }
1338
1339 if (Address.getOpcode() == ISD::GlobalAddress)
1340 {
1341 BuildMI(BB, Opc, 2).addReg(Tmp1)
1342 .addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1343 }
1344 else if(Address.getOpcode() == ISD::FrameIndex)
1345 {
Nate Begeman58f718c2005-03-30 02:23:08 +00001346 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
1347 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001348 }
1349 else
1350 {
1351 int offset;
1352 SelectAddr(Address, Tmp2, offset);
1353 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1354 }
1355 return;
1356 }
1357 case ISD::EXTLOAD:
1358 case ISD::SEXTLOAD:
1359 case ISD::ZEXTLOAD:
1360 case ISD::LOAD:
1361 case ISD::CopyFromReg:
1362 case ISD::CALL:
1363 case ISD::DYNAMIC_STACKALLOC:
1364 ExprMap.erase(N);
1365 SelectExpr(N);
1366 return;
1367 }
1368 assert(0 && "Should not be reached!");
1369}
1370
1371
1372/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
1373/// into a machine code representation using pattern matching and a machine
1374/// description file.
1375///
1376FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) {
1377 return new ISel(TM);
Chris Lattner246fa632005-03-24 06:16:18 +00001378}
1379