blob: 97f40f34450b9cb5c6e04870b84685ee5389d436 [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);
Chris Lattner43fdea02005-04-02 05:03:24 +000056
Nate Begeman27eeb002005-04-02 05:59:34 +000057 setShiftAmountFlavor(Extend); // shl X, 32 == 0
Nate Begeman3e897162005-03-31 23:55:40 +000058 addLegalFPImmediate(+0.0); // Necessary for FSEL
59 addLegalFPImmediate(-0.0); //
60
Nate Begemana9795f82005-03-24 04:41:43 +000061 computeRegisterProperties();
62 }
63
64 /// LowerArguments - This hook must be implemented to indicate how we should
65 /// lower the arguments for the specified function, into the specified DAG.
66 virtual std::vector<SDOperand>
67 LowerArguments(Function &F, SelectionDAG &DAG);
68
69 /// LowerCallTo - This hook lowers an abstract call to a function into an
70 /// actual call.
71 virtual std::pair<SDOperand, SDOperand>
Nate Begeman307e7442005-03-26 01:28:53 +000072 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
73 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Nate Begemana9795f82005-03-24 04:41:43 +000074
75 virtual std::pair<SDOperand, SDOperand>
76 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
77
78 virtual std::pair<SDOperand,SDOperand>
79 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
80 const Type *ArgTy, SelectionDAG &DAG);
81
82 virtual std::pair<SDOperand, SDOperand>
83 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
84 SelectionDAG &DAG);
85 };
86}
87
88
89std::vector<SDOperand>
90PPC32TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
91 //
92 // add beautiful description of PPC stack frame format, or at least some docs
93 //
94 MachineFunction &MF = DAG.getMachineFunction();
95 MachineFrameInfo *MFI = MF.getFrameInfo();
96 MachineBasicBlock& BB = MF.front();
97 std::vector<SDOperand> ArgValues;
98
99 // Due to the rather complicated nature of the PowerPC ABI, rather than a
100 // fixed size array of physical args, for the sake of simplicity let the STL
101 // handle tracking them for us.
102 std::vector<unsigned> argVR, argPR, argOp;
103 unsigned ArgOffset = 24;
104 unsigned GPR_remaining = 8;
105 unsigned FPR_remaining = 13;
106 unsigned GPR_idx = 0, FPR_idx = 0;
107 static const unsigned GPR[] = {
108 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
109 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
110 };
111 static const unsigned FPR[] = {
112 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
113 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
114 };
115
116 // Add DAG nodes to load the arguments... On entry to a function on PPC,
117 // the arguments start at offset 24, although they are likely to be passed
118 // in registers.
119 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
120 SDOperand newroot, argt;
121 unsigned ObjSize;
122 bool needsLoad = false;
123 MVT::ValueType ObjectVT = getValueType(I->getType());
124
125 switch (ObjectVT) {
126 default: assert(0 && "Unhandled argument type!");
127 case MVT::i1:
128 case MVT::i8:
129 case MVT::i16:
130 case MVT::i32:
131 ObjSize = 4;
132 if (GPR_remaining > 0) {
133 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
Nate Begemanf70b5762005-03-28 23:08:54 +0000134 argt = newroot = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32,
135 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000136 if (ObjectVT != MVT::i32)
137 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, newroot);
Nate Begemana9795f82005-03-24 04:41:43 +0000138 } else {
139 needsLoad = true;
140 }
141 break;
Nate Begemanf7e43382005-03-26 07:46:36 +0000142 case MVT::i64: ObjSize = 8;
143 // FIXME: can split 64b load between reg/mem if it is last arg in regs
Nate Begemana9795f82005-03-24 04:41:43 +0000144 if (GPR_remaining > 1) {
145 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
146 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx+1]);
Nate Begemanca12a2b2005-03-28 22:28:37 +0000147 // Copy the extracted halves into the virtual registers
Nate Begemanf70b5762005-03-28 23:08:54 +0000148 SDOperand argHi = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32,
149 DAG.getRoot());
150 SDOperand argLo = DAG.getCopyFromReg(GPR[GPR_idx+1], MVT::i32, argHi);
Nate Begemanca12a2b2005-03-28 22:28:37 +0000151 // Build the outgoing arg thingy
Nate Begemanf70b5762005-03-28 23:08:54 +0000152 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
153 newroot = argLo;
Nate Begemana9795f82005-03-24 04:41:43 +0000154 } else {
155 needsLoad = true;
156 }
157 break;
158 case MVT::f32: ObjSize = 4;
159 case MVT::f64: ObjSize = 8;
160 if (FPR_remaining > 0) {
161 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, FPR[FPR_idx]);
Nate Begemanf70b5762005-03-28 23:08:54 +0000162 argt = newroot = DAG.getCopyFromReg(FPR[FPR_idx], ObjectVT,
163 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000164 --FPR_remaining;
165 ++FPR_idx;
166 } else {
167 needsLoad = true;
168 }
169 break;
170 }
171
172 // We need to load the argument to a virtual register if we determined above
173 // that we ran out of physical registers of the appropriate type
174 if (needsLoad) {
175 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
176 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
177 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN);
178 }
179
180 // Every 4 bytes of argument space consumes one of the GPRs available for
181 // argument passing.
182 if (GPR_remaining > 0) {
183 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
184 GPR_remaining -= delta;
185 GPR_idx += delta;
186 }
187 ArgOffset += ObjSize;
188
189 DAG.setRoot(newroot.getValue(1));
190 ArgValues.push_back(argt);
191 }
192
Nate Begemana9795f82005-03-24 04:41:43 +0000193 // If the function takes variable number of arguments, make a frame index for
194 // the start of the first vararg value... for expansion of llvm.va_start.
195 if (F.isVarArg())
196 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
197
198 return ArgValues;
199}
200
201std::pair<SDOperand, SDOperand>
202PPC32TargetLowering::LowerCallTo(SDOperand Chain,
Nate Begeman307e7442005-03-26 01:28:53 +0000203 const Type *RetTy, bool isVarArg,
204 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG) {
205 // args_to_use will accumulate outgoing args for the ISD::CALL case in
206 // SelectExpr to use to put the arguments in the appropriate registers.
Nate Begemana9795f82005-03-24 04:41:43 +0000207 std::vector<SDOperand> args_to_use;
Nate Begeman307e7442005-03-26 01:28:53 +0000208
209 // Count how many bytes are to be pushed on the stack, including the linkage
210 // area, and parameter passing area.
211 unsigned NumBytes = 24;
212
213 if (Args.empty()) {
Nate Begemana7e11a42005-04-01 05:57:17 +0000214 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
215 DAG.getConstant(NumBytes, getPointerTy()));
Nate Begeman307e7442005-03-26 01:28:53 +0000216 } else {
217 for (unsigned i = 0, e = Args.size(); i != e; ++i)
218 switch (getValueType(Args[i].second)) {
219 default: assert(0 && "Unknown value type!");
220 case MVT::i1:
221 case MVT::i8:
222 case MVT::i16:
223 case MVT::i32:
224 case MVT::f32:
225 NumBytes += 4;
226 break;
227 case MVT::i64:
228 case MVT::f64:
229 NumBytes += 8;
230 break;
231 }
232
233 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
234 // plus 32 bytes of argument space in case any called code gets funky on us.
235 if (NumBytes < 56) NumBytes = 56;
236
237 // Adjust the stack pointer for the new arguments...
238 // These operations are automatically eliminated by the prolog/epilog pass
239 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
240 DAG.getConstant(NumBytes, getPointerTy()));
241
242 // Set up a copy of the stack pointer for use loading and storing any
243 // arguments that may not fit in the registers available for argument
244 // passing.
245 SDOperand StackPtr = DAG.getCopyFromReg(PPC::R1, MVT::i32,
246 DAG.getEntryNode());
247
248 // Figure out which arguments are going to go in registers, and which in
249 // memory. Also, if this is a vararg function, floating point operations
250 // must be stored to our stack, and loaded into integer regs as well, if
251 // any integer regs are available for argument passing.
252 unsigned ArgOffset = 24;
253 unsigned GPR_remaining = 8;
254 unsigned FPR_remaining = 13;
Nate Begeman74d73452005-03-31 00:15:26 +0000255
256 std::vector<SDOperand> MemOps;
Nate Begeman307e7442005-03-26 01:28:53 +0000257 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
258 // PtrOff will be used to store the current argument to the stack if a
259 // register cannot be found for it.
260 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
261 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Nate Begemanf7e43382005-03-26 07:46:36 +0000262 MVT::ValueType ArgVT = getValueType(Args[i].second);
Nate Begeman307e7442005-03-26 01:28:53 +0000263
Nate Begemanf7e43382005-03-26 07:46:36 +0000264 switch (ArgVT) {
Nate Begeman307e7442005-03-26 01:28:53 +0000265 default: assert(0 && "Unexpected ValueType for argument!");
266 case MVT::i1:
267 case MVT::i8:
268 case MVT::i16:
269 // Promote the integer to 32 bits. If the input type is signed use a
270 // sign extend, otherwise use a zero extend.
271 if (Args[i].second->isSigned())
272 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
273 else
274 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
275 // FALL THROUGH
276 case MVT::i32:
277 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000278 args_to_use.push_back(Args[i].first);
Nate Begeman307e7442005-03-26 01:28:53 +0000279 --GPR_remaining;
280 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000281 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
282 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000283 }
284 ArgOffset += 4;
285 break;
286 case MVT::i64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000287 // If we have one free GPR left, we can place the upper half of the i64
288 // in it, and store the other half to the stack. If we have two or more
289 // free GPRs, then we can pass both halves of the i64 in registers.
290 if (GPR_remaining > 0) {
Nate Begemanf2622612005-03-26 02:17:46 +0000291 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
292 Args[i].first, DAG.getConstant(1, MVT::i32));
293 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
294 Args[i].first, DAG.getConstant(0, MVT::i32));
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000295 args_to_use.push_back(Hi);
Nate Begeman74d73452005-03-31 00:15:26 +0000296 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000297 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000298 args_to_use.push_back(Lo);
Nate Begeman74d73452005-03-31 00:15:26 +0000299 --GPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000300 } else {
301 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
302 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Nate Begeman74d73452005-03-31 00:15:26 +0000303 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
304 Lo, PtrOff));
Nate Begemanf7e43382005-03-26 07:46:36 +0000305 }
Nate Begeman307e7442005-03-26 01:28:53 +0000306 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000307 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
308 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000309 }
310 ArgOffset += 8;
311 break;
312 case MVT::f32:
Nate Begeman307e7442005-03-26 01:28:53 +0000313 case MVT::f64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000314 if (FPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000315 args_to_use.push_back(Args[i].first);
316 --FPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000317 if (isVarArg) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000318 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
319 Args[i].first, PtrOff);
320 MemOps.push_back(Store);
Nate Begeman74d73452005-03-31 00:15:26 +0000321 // Float varargs are always shadowed in available integer registers
322 if (GPR_remaining > 0) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000323 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff);
Nate Begeman74d73452005-03-31 00:15:26 +0000324 MemOps.push_back(Load);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000325 args_to_use.push_back(Load);
326 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000327 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000328 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Nate Begeman74d73452005-03-31 00:15:26 +0000329 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
330 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Nate Begeman96fc6812005-03-31 02:05:53 +0000331 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff);
Nate Begeman74d73452005-03-31 00:15:26 +0000332 MemOps.push_back(Load);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000333 args_to_use.push_back(Load);
334 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000335 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000336 } else {
337 // If we have any FPRs remaining, we may also have GPRs remaining.
338 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
339 // GPRs.
340 if (GPR_remaining > 0) {
341 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
342 --GPR_remaining;
343 }
344 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
345 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
346 --GPR_remaining;
347 }
Nate Begeman74d73452005-03-31 00:15:26 +0000348 }
Nate Begeman307e7442005-03-26 01:28:53 +0000349 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000350 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
351 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000352 }
Nate Begemanf7e43382005-03-26 07:46:36 +0000353 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
Nate Begeman307e7442005-03-26 01:28:53 +0000354 break;
355 }
Nate Begemana9795f82005-03-24 04:41:43 +0000356 }
Nate Begeman74d73452005-03-31 00:15:26 +0000357 if (!MemOps.empty())
358 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
Nate Begemana9795f82005-03-24 04:41:43 +0000359 }
360
361 std::vector<MVT::ValueType> RetVals;
362 MVT::ValueType RetTyVT = getValueType(RetTy);
363 if (RetTyVT != MVT::isVoid)
364 RetVals.push_back(RetTyVT);
365 RetVals.push_back(MVT::Other);
366
367 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
368 Chain, Callee, args_to_use), 0);
369 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
370 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
371 DAG.getConstant(NumBytes, getPointerTy()));
372 return std::make_pair(TheCall, Chain);
373}
374
375std::pair<SDOperand, SDOperand>
376PPC32TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
377 //vastart just returns the address of the VarArgsFrameIndex slot.
378 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
379}
380
381std::pair<SDOperand,SDOperand> PPC32TargetLowering::
382LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
383 const Type *ArgTy, SelectionDAG &DAG) {
Nate Begemanc7b09f12005-03-25 08:34:25 +0000384 MVT::ValueType ArgVT = getValueType(ArgTy);
385 SDOperand Result;
386 if (!isVANext) {
387 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList);
388 } else {
389 unsigned Amt;
390 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
391 Amt = 4;
392 else {
393 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
394 "Other types should have been promoted for varargs!");
395 Amt = 8;
396 }
397 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
398 DAG.getConstant(Amt, VAList.getValueType()));
399 }
400 return std::make_pair(Result, Chain);
Nate Begemana9795f82005-03-24 04:41:43 +0000401}
402
403
404std::pair<SDOperand, SDOperand> PPC32TargetLowering::
405LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
406 SelectionDAG &DAG) {
Nate Begeman01d05262005-03-30 01:45:43 +0000407 assert(0 && "LowerFrameReturnAddress unimplemented");
Nate Begemana9795f82005-03-24 04:41:43 +0000408 abort();
409}
410
411namespace {
412
413//===--------------------------------------------------------------------===//
414/// ISel - PPC32 specific code to select PPC32 machine instructions for
415/// SelectionDAG operations.
416//===--------------------------------------------------------------------===//
417class ISel : public SelectionDAGISel {
418
419 /// Comment Here.
420 PPC32TargetLowering PPC32Lowering;
421
422 /// ExprMap - As shared expressions are codegen'd, we keep track of which
423 /// vreg the value is produced in, so we only emit one copy of each compiled
424 /// tree.
425 std::map<SDOperand, unsigned> ExprMap;
Nate Begemanc7b09f12005-03-25 08:34:25 +0000426
427 unsigned GlobalBaseReg;
428 bool GlobalBaseInitialized;
Nate Begemana9795f82005-03-24 04:41:43 +0000429
430public:
431 ISel(TargetMachine &TM) : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM)
432 {}
433
Nate Begemanc7b09f12005-03-25 08:34:25 +0000434 /// runOnFunction - Override this function in order to reset our per-function
435 /// variables.
436 virtual bool runOnFunction(Function &Fn) {
437 // Make sure we re-emit a set of the global base reg if necessary
438 GlobalBaseInitialized = false;
439 return SelectionDAGISel::runOnFunction(Fn);
440 }
441
Nate Begemana9795f82005-03-24 04:41:43 +0000442 /// InstructionSelectBasicBlock - This callback is invoked by
443 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
444 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
445 DEBUG(BB->dump());
446 // Codegen the basic block.
447 Select(DAG.getRoot());
448
449 // Clear state used for selection.
450 ExprMap.clear();
451 }
452
Nate Begemandffcfcc2005-04-01 00:32:34 +0000453 unsigned getGlobalBaseReg();
Nate Begeman6b559972005-04-01 02:59:27 +0000454 unsigned getConstDouble(double floatVal, unsigned Result);
Nate Begemandffcfcc2005-04-01 00:32:34 +0000455 unsigned SelectSetCR0(SDOperand CC);
Nate Begemana9795f82005-03-24 04:41:43 +0000456 unsigned SelectExpr(SDOperand N);
457 unsigned SelectExprFP(SDOperand N, unsigned Result);
458 void Select(SDOperand N);
459
Nate Begeman04730362005-04-01 04:45:11 +0000460 bool SelectAddr(SDOperand N, unsigned& Reg, int& offset);
Nate Begemana9795f82005-03-24 04:41:43 +0000461 void SelectBranchCC(SDOperand N);
462};
463
464/// canUseAsImmediateForOpcode - This method returns a value indicating whether
465/// the ConstantSDNode N can be used as an immediate to Opcode. The return
466/// values are either 0, 1 or 2. 0 indicates that either N is not a
467/// ConstantSDNode, or is not suitable for use by that opcode. A return value
468/// of 1 indicates that the constant may be used in normal immediate form. A
469/// return value of 2 indicates that the constant may be used in shifted
470/// immediate form. If the return value is nonzero, the constant value is
471/// placed in Imm.
472///
473static unsigned canUseAsImmediateForOpcode(SDOperand N, unsigned Opcode,
Nate Begeman3e897162005-03-31 23:55:40 +0000474 unsigned& Imm, bool U = false) {
Nate Begemana9795f82005-03-24 04:41:43 +0000475 if (N.getOpcode() != ISD::Constant) return 0;
476
477 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
478
479 switch(Opcode) {
480 default: return 0;
481 case ISD::ADD:
482 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
483 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
484 break;
485 case ISD::AND:
486 case ISD::XOR:
487 case ISD::OR:
488 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
489 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
490 break;
Nate Begeman307e7442005-03-26 01:28:53 +0000491 case ISD::MUL:
Nate Begeman27523a12005-04-02 00:42:16 +0000492 case ISD::SUB:
Nate Begeman307e7442005-03-26 01:28:53 +0000493 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
494 break;
Nate Begeman3e897162005-03-31 23:55:40 +0000495 case ISD::SETCC:
496 if (U && (v >= 0 && v <= 65535)) { Imm = v & 0xFFFF; return 1; }
497 if (!U && (v <= 32767 && v >= -32768)) { Imm = v & 0xFFFF; return 1; }
498 break;
Nate Begemana9795f82005-03-24 04:41:43 +0000499 }
500 return 0;
501}
Nate Begeman3e897162005-03-31 23:55:40 +0000502
503/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
504/// to Condition. If the Condition is unordered or unsigned, the bool argument
505/// U is set to true, otherwise it is set to false.
506static unsigned getBCCForSetCC(unsigned Condition, bool& U) {
507 U = false;
508 switch (Condition) {
509 default: assert(0 && "Unknown condition!"); abort();
510 case ISD::SETEQ: return PPC::BEQ;
511 case ISD::SETNE: return PPC::BNE;
512 case ISD::SETULT: U = true;
513 case ISD::SETLT: return PPC::BLT;
514 case ISD::SETULE: U = true;
515 case ISD::SETLE: return PPC::BLE;
516 case ISD::SETUGT: U = true;
517 case ISD::SETGT: return PPC::BGT;
518 case ISD::SETUGE: U = true;
519 case ISD::SETGE: return PPC::BGE;
520 }
Nate Begeman04730362005-04-01 04:45:11 +0000521 return 0;
522}
523
524/// IndexedOpForOp - Return the indexed variant for each of the PowerPC load
525/// and store immediate instructions.
526static unsigned IndexedOpForOp(unsigned Opcode) {
527 switch(Opcode) {
528 default: assert(0 && "Unknown opcode!"); abort();
529 case PPC::LBZ: return PPC::LBZX; case PPC::STB: return PPC::STBX;
530 case PPC::LHZ: return PPC::LHZX; case PPC::STH: return PPC::STHX;
531 case PPC::LHA: return PPC::LHAX; case PPC::STW: return PPC::STWX;
532 case PPC::LWZ: return PPC::LWZX; case PPC::STFS: return PPC::STFSX;
533 case PPC::LFS: return PPC::LFSX; case PPC::STFD: return PPC::STFDX;
534 case PPC::LFD: return PPC::LFDX;
535 }
536 return 0;
Nate Begeman3e897162005-03-31 23:55:40 +0000537}
Nate Begemana9795f82005-03-24 04:41:43 +0000538}
539
Nate Begemanc7b09f12005-03-25 08:34:25 +0000540/// getGlobalBaseReg - Output the instructions required to put the
541/// base address to use for accessing globals into a register.
542///
543unsigned ISel::getGlobalBaseReg() {
544 if (!GlobalBaseInitialized) {
545 // Insert the set of GlobalBaseReg into the first MBB of the function
546 MachineBasicBlock &FirstMBB = BB->getParent()->front();
547 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
548 GlobalBaseReg = MakeReg(MVT::i32);
549 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
550 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR);
551 GlobalBaseInitialized = true;
552 }
553 return GlobalBaseReg;
554}
555
Nate Begeman6b559972005-04-01 02:59:27 +0000556/// getConstDouble - Loads a floating point value into a register, via the
557/// Constant Pool. Optionally takes a register in which to load the value.
558unsigned ISel::getConstDouble(double doubleVal, unsigned Result=0) {
559 unsigned Tmp1 = MakeReg(MVT::i32);
560 if (0 == Result) Result = MakeReg(MVT::f64);
561 MachineConstantPool *CP = BB->getParent()->getConstantPool();
562 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, doubleVal);
563 unsigned CPI = CP->getConstantPoolIndex(CFP);
564 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
565 .addConstantPoolIndex(CPI);
566 BuildMI(BB, PPC::LFD, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
567 return Result;
568}
569
Nate Begemandffcfcc2005-04-01 00:32:34 +0000570unsigned ISel::SelectSetCR0(SDOperand CC) {
571 unsigned Opc, Tmp1, Tmp2;
572 static const unsigned CompareOpcodes[] =
573 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
574
575 // If the first operand to the select is a SETCC node, then we can fold it
576 // into the branch that selects which value to return.
577 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
578 if (SetCC && CC.getOpcode() == ISD::SETCC) {
579 bool U;
580 Opc = getBCCForSetCC(SetCC->getCondition(), U);
581 Tmp1 = SelectExpr(SetCC->getOperand(0));
582
583 // Pass the optional argument U to canUseAsImmediateForOpcode for SETCC,
584 // so that it knows whether the SETCC immediate range is signed or not.
585 if (1 == canUseAsImmediateForOpcode(SetCC->getOperand(1), ISD::SETCC,
586 Tmp2, U)) {
587 if (U)
588 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(Tmp2);
589 else
590 BuildMI(BB, PPC::CMPWI, 2, PPC::CR0).addReg(Tmp1).addSImm(Tmp2);
591 } else {
592 bool IsInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
593 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
594 Tmp2 = SelectExpr(SetCC->getOperand(1));
595 BuildMI(BB, CompareOpc, 2, PPC::CR0).addReg(Tmp1).addReg(Tmp2);
596 }
597 } else {
598 Tmp1 = SelectExpr(CC);
599 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(0);
600 Opc = PPC::BNE;
601 }
602 return Opc;
603}
604
605/// Check to see if the load is a constant offset from a base register
Nate Begeman04730362005-04-01 04:45:11 +0000606bool ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
Nate Begemana9795f82005-03-24 04:41:43 +0000607{
Nate Begeman96fc6812005-03-31 02:05:53 +0000608 unsigned imm = 0, opcode = N.getOpcode();
Nate Begeman04730362005-04-01 04:45:11 +0000609 if (N.getOpcode() == ISD::ADD) {
610 Reg = SelectExpr(N.getOperand(0));
Nate Begeman96fc6812005-03-31 02:05:53 +0000611 if (1 == canUseAsImmediateForOpcode(N.getOperand(1), opcode, imm)) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000612 offset = imm;
Nate Begeman04730362005-04-01 04:45:11 +0000613 return false;
614 }
615 offset = SelectExpr(N.getOperand(1));
616 return true;
617 }
Nate Begemana9795f82005-03-24 04:41:43 +0000618 Reg = SelectExpr(N);
619 offset = 0;
Nate Begeman04730362005-04-01 04:45:11 +0000620 return false;
Nate Begemana9795f82005-03-24 04:41:43 +0000621}
622
623void ISel::SelectBranchCC(SDOperand N)
624{
625 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
626 MachineBasicBlock *Dest =
627 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Nate Begeman3e897162005-03-31 23:55:40 +0000628
Nate Begemana9795f82005-03-24 04:41:43 +0000629 Select(N.getOperand(0)); //chain
Nate Begemandffcfcc2005-04-01 00:32:34 +0000630 unsigned Opc = SelectSetCR0(N.getOperand(1));
Nate Begeman3e897162005-03-31 23:55:40 +0000631 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(Dest);
Nate Begemana9795f82005-03-24 04:41:43 +0000632 return;
633}
634
635unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
636{
637 unsigned Tmp1, Tmp2, Tmp3;
638 unsigned Opc = 0;
639 SDNode *Node = N.Val;
640 MVT::ValueType DestType = N.getValueType();
641 unsigned opcode = N.getOpcode();
642
643 switch (opcode) {
644 default:
645 Node->dump();
646 assert(0 && "Node not handled!\n");
647
Nate Begeman23afcfb2005-03-29 22:48:55 +0000648 case ISD::SELECT: {
Nate Begeman3e897162005-03-31 23:55:40 +0000649 // Attempt to generate FSEL. We can do this whenever we have an FP result,
650 // and an FP comparison in the SetCC node.
651 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val);
652 if (SetCC && N.getOperand(0).getOpcode() == ISD::SETCC &&
653 !MVT::isInteger(SetCC->getOperand(0).getValueType()) &&
654 SetCC->getCondition() != ISD::SETEQ &&
655 SetCC->getCondition() != ISD::SETNE) {
656 MVT::ValueType VT = SetCC->getOperand(0).getValueType();
657 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
658 unsigned TV = SelectExpr(N.getOperand(1)); // Use if TRUE
659 unsigned FV = SelectExpr(N.getOperand(2)); // Use if FALSE
660
661 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1));
662 if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) {
663 switch(SetCC->getCondition()) {
664 default: assert(0 && "Invalid FSEL condition"); abort();
665 case ISD::SETULT:
666 case ISD::SETLT:
667 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(FV).addReg(TV);
668 return Result;
669 case ISD::SETUGE:
670 case ISD::SETGE:
671 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV);
672 return Result;
673 case ISD::SETUGT:
674 case ISD::SETGT: {
675 Tmp2 = MakeReg(VT);
676 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
677 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(FV).addReg(TV);
678 return Result;
679 }
680 case ISD::SETULE:
681 case ISD::SETLE: {
682 Tmp2 = MakeReg(VT);
683 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
684 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV);
685 return Result;
686 }
687 }
688 } else {
689 Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS;
690 Tmp2 = SelectExpr(SetCC->getOperand(1));
691 Tmp3 = MakeReg(VT);
692 switch(SetCC->getCondition()) {
693 default: assert(0 && "Invalid FSEL condition"); abort();
694 case ISD::SETULT:
695 case ISD::SETLT:
696 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
697 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
698 return Result;
699 case ISD::SETUGE:
700 case ISD::SETGE:
701 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
702 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
703 return Result;
704 case ISD::SETUGT:
705 case ISD::SETGT:
706 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
707 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
708 return Result;
709 case ISD::SETULE:
710 case ISD::SETLE:
711 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
712 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
713 return Result;
714 }
715 }
716 assert(0 && "Should never get here");
717 return 0;
718 }
719
Nate Begeman31318e42005-04-01 07:21:30 +0000720 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
721 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman6cb2e1b2005-04-01 08:57:43 +0000722 Opc = SelectSetCR0(N.getOperand(0));
Nate Begeman31318e42005-04-01 07:21:30 +0000723
Nate Begeman23afcfb2005-03-29 22:48:55 +0000724 // Create an iterator with which to insert the MBB for copying the false
725 // value and the MBB to hold the PHI instruction for this SetCC.
726 MachineBasicBlock *thisMBB = BB;
727 const BasicBlock *LLVM_BB = BB->getBasicBlock();
728 ilist<MachineBasicBlock>::iterator It = BB;
729 ++It;
730
731 // thisMBB:
732 // ...
733 // TrueVal = ...
734 // cmpTY cr0, r1, r2
735 // bCC copy1MBB
736 // fallthrough --> copy0MBB
Nate Begeman23afcfb2005-03-29 22:48:55 +0000737 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
738 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman6cb2e1b2005-04-01 08:57:43 +0000739 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
Nate Begeman23afcfb2005-03-29 22:48:55 +0000740 MachineFunction *F = BB->getParent();
741 F->getBasicBlockList().insert(It, copy0MBB);
742 F->getBasicBlockList().insert(It, sinkMBB);
743 // Update machine-CFG edges
744 BB->addSuccessor(copy0MBB);
745 BB->addSuccessor(sinkMBB);
746
747 // copy0MBB:
748 // %FalseValue = ...
749 // # fallthrough to sinkMBB
750 BB = copy0MBB;
Nate Begeman23afcfb2005-03-29 22:48:55 +0000751 // Update machine-CFG edges
752 BB->addSuccessor(sinkMBB);
753
754 // sinkMBB:
755 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
756 // ...
757 BB = sinkMBB;
758 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
759 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
760 return Result;
761 }
Nate Begeman27eeb002005-04-02 05:59:34 +0000762
763 case ISD::FNEG:
764 if (ISD::FABS == N.getOperand(0).getOpcode()) {
765 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
766 BuildMI(BB, PPC::FNABS, 1, Result).addReg(Tmp1);
767 } else {
768 Tmp1 = SelectExpr(N.getOperand(0));
769 BuildMI(BB, PPC::FNEG, 1, Result).addReg(Tmp1);
770 }
771 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000772
Nate Begeman27eeb002005-04-02 05:59:34 +0000773 case ISD::FABS:
774 Tmp1 = SelectExpr(N.getOperand(0));
775 BuildMI(BB, PPC::FABS, 1, Result).addReg(Tmp1);
776 return Result;
777
Nate Begemana9795f82005-03-24 04:41:43 +0000778 case ISD::FP_ROUND:
779 assert (DestType == MVT::f32 &&
780 N.getOperand(0).getValueType() == MVT::f64 &&
781 "only f64 to f32 conversion supported here");
782 Tmp1 = SelectExpr(N.getOperand(0));
783 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
784 return Result;
785
786 case ISD::FP_EXTEND:
787 assert (DestType == MVT::f64 &&
788 N.getOperand(0).getValueType() == MVT::f32 &&
789 "only f32 to f64 conversion supported here");
790 Tmp1 = SelectExpr(N.getOperand(0));
791 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
792 return Result;
793
794 case ISD::CopyFromReg:
Nate Begemanf2622612005-03-26 02:17:46 +0000795 if (Result == 1)
796 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
797 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
798 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
799 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000800
Nate Begeman6d369cc2005-04-01 01:08:07 +0000801 case ISD::ConstantFP: {
Nate Begeman6d369cc2005-04-01 01:08:07 +0000802 ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
Nate Begeman6b559972005-04-01 02:59:27 +0000803 Result = getConstDouble(CN->getValue(), Result);
Nate Begeman6d369cc2005-04-01 01:08:07 +0000804 return Result;
805 }
Nate Begemana9795f82005-03-24 04:41:43 +0000806
807 case ISD::MUL:
808 case ISD::ADD:
809 case ISD::SUB:
810 case ISD::SDIV:
811 switch( opcode ) {
812 case ISD::MUL: Opc = DestType == MVT::f64 ? PPC::FMUL : PPC::FMULS; break;
813 case ISD::ADD: Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS; break;
814 case ISD::SUB: Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS; break;
815 case ISD::SDIV: Opc = DestType == MVT::f64 ? PPC::FDIV : PPC::FDIVS; break;
816 };
Nate Begemana9795f82005-03-24 04:41:43 +0000817 Tmp1 = SelectExpr(N.getOperand(0));
818 Tmp2 = SelectExpr(N.getOperand(1));
819 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
820 return Result;
821
Nate Begemana9795f82005-03-24 04:41:43 +0000822 case ISD::UINT_TO_FP:
Nate Begemanfdcf3412005-03-30 19:38:35 +0000823 case ISD::SINT_TO_FP: {
824 assert (N.getOperand(0).getValueType() == MVT::i32
825 && "int to float must operate on i32");
826 bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
827 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
828 Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into
829 Tmp3 = MakeReg(MVT::i32); // temp reg to hold the conversion constant
830 unsigned ConstF = MakeReg(MVT::f64); // temp reg to hold the fp constant
831
832 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
833 MachineConstantPool *CP = BB->getParent()->getConstantPool();
834
835 // FIXME: pull this FP constant generation stuff out into something like
836 // the simple ISel's getReg.
837 if (IsUnsigned) {
838 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000000p52);
839 unsigned CPI = CP->getConstantPoolIndex(CFP);
840 // Load constant fp value
841 unsigned Tmp4 = MakeReg(MVT::i32);
842 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg())
843 .addConstantPoolIndex(CPI);
844 BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
845 // Store the hi & low halves of the fp value, currently in int regs
846 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
847 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
848 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp1), FrameIdx, 4);
849 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
850 // Generate the return value with a subtract
851 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
852 } else {
853 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000008p52);
854 unsigned CPI = CP->getConstantPoolIndex(CFP);
855 // Load constant fp value
856 unsigned Tmp4 = MakeReg(MVT::i32);
857 unsigned TmpL = MakeReg(MVT::i32);
858 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg())
859 .addConstantPoolIndex(CPI);
860 BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
861 // Store the hi & low halves of the fp value, currently in int regs
862 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
863 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
864 BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
865 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
866 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
867 // Generate the return value with a subtract
868 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
869 }
870 return Result;
871 }
Nate Begemana9795f82005-03-24 04:41:43 +0000872 }
Nate Begeman6b559972005-04-01 02:59:27 +0000873 assert(0 && "Should never get here");
Nate Begemana9795f82005-03-24 04:41:43 +0000874 return 0;
875}
876
877unsigned ISel::SelectExpr(SDOperand N) {
878 unsigned Result;
879 unsigned Tmp1, Tmp2, Tmp3;
880 unsigned Opc = 0;
881 unsigned opcode = N.getOpcode();
882
883 SDNode *Node = N.Val;
884 MVT::ValueType DestType = N.getValueType();
885
886 unsigned &Reg = ExprMap[N];
887 if (Reg) return Reg;
888
Nate Begeman27eeb002005-04-02 05:59:34 +0000889 switch (N.getOpcode()) {
890 default:
Nate Begemana9795f82005-03-24 04:41:43 +0000891 Reg = Result = (N.getValueType() != MVT::Other) ?
Nate Begeman27eeb002005-04-02 05:59:34 +0000892 MakeReg(N.getValueType()) : 1;
893 break;
894 case ISD::CALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000895 // If this is a call instruction, make sure to prepare ALL of the result
896 // values as well as the chain.
Nate Begeman27eeb002005-04-02 05:59:34 +0000897 if (Node->getNumValues() == 1)
898 Reg = Result = 1; // Void call, just a chain.
899 else {
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000900 Result = MakeReg(Node->getValueType(0));
901 ExprMap[N.getValue(0)] = Result;
Nate Begeman27eeb002005-04-02 05:59:34 +0000902 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000903 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Nate Begeman27eeb002005-04-02 05:59:34 +0000904 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000905 }
Nate Begeman27eeb002005-04-02 05:59:34 +0000906 break;
907 case ISD::ADD_PARTS:
908 case ISD::SUB_PARTS:
909 case ISD::SHL_PARTS:
910 case ISD::SRL_PARTS:
911 case ISD::SRA_PARTS:
912 Result = MakeReg(Node->getValueType(0));
913 ExprMap[N.getValue(0)] = Result;
914 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
915 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
916 break;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000917 }
918
919 if (DestType == MVT::f64 || DestType == MVT::f32)
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000920 if (ISD::LOAD != opcode && ISD::EXTLOAD != opcode && ISD::UNDEF != opcode)
Nate Begeman74d73452005-03-31 00:15:26 +0000921 return SelectExprFP(N, Result);
Nate Begemana9795f82005-03-24 04:41:43 +0000922
923 switch (opcode) {
924 default:
925 Node->dump();
926 assert(0 && "Node not handled!\n");
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000927 case ISD::UNDEF:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000928 BuildMI(BB, PPC::IMPLICIT_DEF, 0, Result);
929 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000930 case ISD::DYNAMIC_STACKALLOC:
Nate Begeman5e966612005-03-24 06:28:42 +0000931 // Generate both result values. FIXME: Need a better commment here?
932 if (Result != 1)
933 ExprMap[N.getValue(1)] = 1;
934 else
935 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
936
937 // FIXME: We are currently ignoring the requested alignment for handling
938 // greater than the stack alignment. This will need to be revisited at some
939 // point. Align = N.getOperand(2);
940 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
941 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
942 std::cerr << "Cannot allocate stack object with greater alignment than"
943 << " the stack alignment yet!";
944 abort();
945 }
946 Select(N.getOperand(0));
947 Tmp1 = SelectExpr(N.getOperand(1));
948 // Subtract size from stack pointer, thereby allocating some space.
949 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
950 // Put a pointer to the space into the result register by copying the SP
951 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
952 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000953
954 case ISD::ConstantPool:
Nate Begemanca12a2b2005-03-28 22:28:37 +0000955 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
956 Tmp2 = MakeReg(MVT::i32);
957 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp2).addReg(getGlobalBaseReg())
958 .addConstantPoolIndex(Tmp1);
959 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
960 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000961
962 case ISD::FrameIndex:
Nate Begemanf3d08f32005-03-29 00:03:27 +0000963 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Nate Begeman58f718c2005-03-30 02:23:08 +0000964 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
Nate Begemanf3d08f32005-03-29 00:03:27 +0000965 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000966
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000967 case ISD::GlobalAddress: {
968 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanca12a2b2005-03-28 22:28:37 +0000969 Tmp1 = MakeReg(MVT::i32);
Nate Begemanc7b09f12005-03-25 08:34:25 +0000970 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
971 .addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000972 if (GV->hasWeakLinkage() || GV->isExternal()) {
973 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
974 } else {
975 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
976 }
977 return Result;
978 }
979
Nate Begeman5e966612005-03-24 06:28:42 +0000980 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +0000981 case ISD::EXTLOAD:
982 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000983 case ISD::SEXTLOAD: {
Nate Begeman9db505c2005-03-28 19:36:43 +0000984 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
985 Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType();
Nate Begeman74d73452005-03-31 00:15:26 +0000986 bool sext = (ISD::SEXTLOAD == opcode);
987 bool byte = (MVT::i8 == TypeBeingLoaded);
988
Nate Begeman5e966612005-03-24 06:28:42 +0000989 // Make sure we generate both values.
990 if (Result != 1)
991 ExprMap[N.getValue(1)] = 1; // Generate the token
992 else
993 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
994
995 SDOperand Chain = N.getOperand(0);
996 SDOperand Address = N.getOperand(1);
997 Select(Chain);
998
Nate Begeman9db505c2005-03-28 19:36:43 +0000999 switch (TypeBeingLoaded) {
Nate Begeman74d73452005-03-31 00:15:26 +00001000 default: Node->dump(); assert(0 && "Cannot load this type!");
Nate Begeman9db505c2005-03-28 19:36:43 +00001001 case MVT::i1: Opc = PPC::LBZ; break;
1002 case MVT::i8: Opc = PPC::LBZ; break;
1003 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
1004 case MVT::i32: Opc = PPC::LWZ; break;
Nate Begeman74d73452005-03-31 00:15:26 +00001005 case MVT::f32: Opc = PPC::LFS; break;
1006 case MVT::f64: Opc = PPC::LFD; break;
Nate Begeman5e966612005-03-24 06:28:42 +00001007 }
1008
Nate Begeman74d73452005-03-31 00:15:26 +00001009 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1010 Tmp1 = MakeReg(MVT::i32);
1011 int CPI = CP->getIndex();
1012 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1013 .addConstantPoolIndex(CPI);
1014 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001015 }
Nate Begeman74d73452005-03-31 00:15:26 +00001016 else if(Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman58f718c2005-03-30 02:23:08 +00001017 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
1018 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
Nate Begeman5e966612005-03-24 06:28:42 +00001019 } else {
1020 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00001021 bool idx = SelectAddr(Address, Tmp1, offset);
1022 if (idx) {
1023 Opc = IndexedOpForOp(Opc);
1024 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset);
1025 } else {
1026 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
1027 }
Nate Begeman5e966612005-03-24 06:28:42 +00001028 }
1029 return Result;
1030 }
1031
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001032 case ISD::CALL: {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001033 unsigned GPR_idx = 0, FPR_idx = 0;
1034 static const unsigned GPR[] = {
1035 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1036 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1037 };
1038 static const unsigned FPR[] = {
1039 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1040 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1041 };
1042
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001043 // Lower the chain for this call.
1044 Select(N.getOperand(0));
1045 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
Nate Begeman74d73452005-03-31 00:15:26 +00001046
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001047 // Load the register args to virtual regs
1048 std::vector<unsigned> ArgVR;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001049 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001050 ArgVR.push_back(SelectExpr(N.getOperand(i)));
1051
1052 // Copy the virtual registers into the appropriate argument register
1053 for(int i = 0, e = ArgVR.size(); i < e; ++i) {
1054 switch(N.getOperand(i+2).getValueType()) {
1055 default: Node->dump(); assert(0 && "Unknown value type for call");
1056 case MVT::i1:
1057 case MVT::i8:
1058 case MVT::i16:
1059 case MVT::i32:
1060 assert(GPR_idx < 8 && "Too many int args");
1061 if (N.getOperand(i+2).getOpcode() != ISD::UNDEF)
1062 BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]);
1063 ++GPR_idx;
1064 break;
1065 case MVT::f64:
1066 case MVT::f32:
1067 assert(FPR_idx < 13 && "Too many fp args");
1068 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]);
1069 ++FPR_idx;
1070 break;
1071 }
1072 }
1073
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001074 // Emit the correct call instruction based on the type of symbol called.
1075 if (GlobalAddressSDNode *GASD =
1076 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
1077 BuildMI(BB, PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(), true);
1078 } else if (ExternalSymbolSDNode *ESSDN =
1079 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
1080 BuildMI(BB, PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(), true);
1081 } else {
1082 Tmp1 = SelectExpr(N.getOperand(1));
1083 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
1084 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
1085 BuildMI(BB, PPC::CALLindirect, 3).addImm(20).addImm(0).addReg(PPC::R12);
1086 }
1087
1088 switch (Node->getValueType(0)) {
1089 default: assert(0 && "Unknown value type for call result!");
1090 case MVT::Other: return 1;
1091 case MVT::i1:
1092 case MVT::i8:
1093 case MVT::i16:
1094 case MVT::i32:
Nate Begemanc7b09f12005-03-25 08:34:25 +00001095 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001096 if (Node->getValueType(1) == MVT::i32)
Nate Begemanc7b09f12005-03-25 08:34:25 +00001097 BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R4).addReg(PPC::R4);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001098 break;
1099 case MVT::f32:
1100 case MVT::f64:
1101 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1102 break;
1103 }
1104 return Result+N.ResNo;
1105 }
Nate Begemana9795f82005-03-24 04:41:43 +00001106
1107 case ISD::SIGN_EXTEND:
1108 case ISD::SIGN_EXTEND_INREG:
1109 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman9db505c2005-03-28 19:36:43 +00001110 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1111 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
1112 case MVT::i16:
1113 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
1114 break;
1115 case MVT::i8:
1116 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
1117 break;
Nate Begeman74747862005-03-29 22:24:51 +00001118 case MVT::i1:
1119 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1120 break;
Nate Begeman9db505c2005-03-28 19:36:43 +00001121 }
Nate Begemana9795f82005-03-24 04:41:43 +00001122 return Result;
1123
1124 case ISD::ZERO_EXTEND_INREG:
1125 Tmp1 = SelectExpr(N.getOperand(0));
1126 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
Nate Begeman9db505c2005-03-28 19:36:43 +00001127 default: Node->dump(); assert(0 && "Unhandled ZERO_EXTEND type"); break;
Nate Begemana9795f82005-03-24 04:41:43 +00001128 case MVT::i16: Tmp2 = 16; break;
1129 case MVT::i8: Tmp2 = 24; break;
1130 case MVT::i1: Tmp2 = 31; break;
1131 }
Nate Begeman33162522005-03-29 21:54:38 +00001132 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(0).addImm(Tmp2)
1133 .addImm(31);
Nate Begemana9795f82005-03-24 04:41:43 +00001134 return Result;
1135
Nate Begemana9795f82005-03-24 04:41:43 +00001136 case ISD::CopyFromReg:
1137 if (Result == 1)
1138 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1139 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1140 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1141 return Result;
1142
1143 case ISD::SHL:
Nate Begeman5e966612005-03-24 06:28:42 +00001144 Tmp1 = SelectExpr(N.getOperand(0));
1145 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1146 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001147 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
Nate Begeman5e966612005-03-24 06:28:42 +00001148 .addImm(31-Tmp2);
1149 } else {
1150 Tmp2 = SelectExpr(N.getOperand(1));
1151 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1152 }
1153 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001154
Nate Begeman5e966612005-03-24 06:28:42 +00001155 case ISD::SRL:
1156 Tmp1 = SelectExpr(N.getOperand(0));
1157 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1158 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001159 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2)
Nate Begeman5e966612005-03-24 06:28:42 +00001160 .addImm(Tmp2).addImm(31);
1161 } else {
1162 Tmp2 = SelectExpr(N.getOperand(1));
1163 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1164 }
1165 return Result;
1166
1167 case ISD::SRA:
1168 Tmp1 = SelectExpr(N.getOperand(0));
1169 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1170 Tmp2 = CN->getValue() & 0x1F;
1171 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1172 } else {
1173 Tmp2 = SelectExpr(N.getOperand(1));
1174 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1175 }
1176 return Result;
1177
Nate Begemana9795f82005-03-24 04:41:43 +00001178 case ISD::ADD:
1179 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1180 Tmp1 = SelectExpr(N.getOperand(0));
1181 switch(canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1182 default: assert(0 && "unhandled result code");
1183 case 0: // No immediate
1184 Tmp2 = SelectExpr(N.getOperand(1));
1185 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1186 break;
1187 case 1: // Low immediate
1188 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1189 break;
1190 case 2: // Shifted immediate
1191 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1192 break;
1193 }
1194 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001195
Nate Begemana9795f82005-03-24 04:41:43 +00001196 case ISD::AND:
1197 case ISD::OR:
1198 case ISD::XOR:
1199 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1200 Tmp1 = SelectExpr(N.getOperand(0));
1201 switch(canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1202 default: assert(0 && "unhandled result code");
1203 case 0: // No immediate
1204 Tmp2 = SelectExpr(N.getOperand(1));
1205 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +00001206 case ISD::AND: Opc = PPC::AND; break;
1207 case ISD::OR: Opc = PPC::OR; break;
1208 case ISD::XOR: Opc = PPC::XOR; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001209 }
Nate Begeman5e966612005-03-24 06:28:42 +00001210 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001211 break;
1212 case 1: // Low immediate
1213 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +00001214 case ISD::AND: Opc = PPC::ANDIo; break;
1215 case ISD::OR: Opc = PPC::ORI; break;
1216 case ISD::XOR: Opc = PPC::XORI; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001217 }
Nate Begeman5e966612005-03-24 06:28:42 +00001218 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001219 break;
1220 case 2: // Shifted immediate
1221 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +00001222 case ISD::AND: Opc = PPC::ANDISo; break;
1223 case ISD::OR: Opc = PPC::ORIS; break;
1224 case ISD::XOR: Opc = PPC::XORIS; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001225 }
Nate Begeman5e966612005-03-24 06:28:42 +00001226 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001227 break;
1228 }
1229 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001230
1231 case ISD::SUB:
1232 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001233 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman27523a12005-04-02 00:42:16 +00001234 if (1 == canUseAsImmediateForOpcode(N.getOperand(0), opcode, Tmp1))
1235 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1);
1236 else {
1237 Tmp1 = SelectExpr(N.getOperand(0));
1238 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
1239 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001240 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001241
Nate Begeman5e966612005-03-24 06:28:42 +00001242 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001243 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1244 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman307e7442005-03-26 01:28:53 +00001245 if (1 == canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
1246 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1247 else {
1248 Tmp2 = SelectExpr(N.getOperand(1));
1249 BuildMI(BB, PPC::MULLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1250 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001251 return Result;
1252
Nate Begemanf3d08f32005-03-29 00:03:27 +00001253 case ISD::SDIV:
1254 case ISD::UDIV:
1255 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1256 Tmp1 = SelectExpr(N.getOperand(0));
1257 Tmp2 = SelectExpr(N.getOperand(1));
1258 Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW;
1259 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1260 return Result;
1261
1262 case ISD::UREM:
1263 case ISD::SREM: {
1264 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1265 Tmp1 = SelectExpr(N.getOperand(0));
1266 Tmp2 = SelectExpr(N.getOperand(1));
1267 Tmp3 = MakeReg(MVT::i32);
1268 unsigned Tmp4 = MakeReg(MVT::i32);
1269 Opc = (ISD::UREM == opcode) ? PPC::DIVWU : PPC::DIVW;
1270 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1271 BuildMI(BB, PPC::MULLW, 2, Tmp4).addReg(Tmp3).addReg(Tmp2);
1272 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp4).addReg(Tmp1);
1273 return Result;
1274 }
1275
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001276 case ISD::ADD_PARTS:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001277 case ISD::SUB_PARTS: {
1278 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1279 "Not an i64 add/sub!");
1280 // Emit all of the operands.
1281 std::vector<unsigned> InVals;
1282 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
1283 InVals.push_back(SelectExpr(N.getOperand(i)));
1284 if (N.getOpcode() == ISD::ADD_PARTS) {
Nate Begeman27eeb002005-04-02 05:59:34 +00001285 BuildMI(BB, PPC::ADDC, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
1286 BuildMI(BB, PPC::ADDE, 2, Result+1).addReg(InVals[1]).addReg(InVals[3]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001287 } else {
Nate Begeman27eeb002005-04-02 05:59:34 +00001288 BuildMI(BB, PPC::SUBFC, 2, Result).addReg(InVals[2]).addReg(InVals[0]);
1289 BuildMI(BB, PPC::SUBFE, 2, Result+1).addReg(InVals[3]).addReg(InVals[1]);
1290 }
1291 return Result+N.ResNo;
1292 }
1293
1294 case ISD::SHL_PARTS:
1295 case ISD::SRA_PARTS:
1296 case ISD::SRL_PARTS: {
1297 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
1298 "Not an i64 shift!");
1299 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
1300 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
1301 unsigned SHReg = SelectExpr(N.getOperand(2));
1302 Tmp1 = MakeReg(MVT::i32);
1303 Tmp2 = MakeReg(MVT::i32);
1304 Tmp3 = MakeReg(MVT::i32);
1305 unsigned Tmp4 = MakeReg(MVT::i32);
1306 unsigned Tmp5 = MakeReg(MVT::i32);
1307 unsigned Tmp6 = MakeReg(MVT::i32);
1308 BuildMI(BB, PPC::SUBFIC, 2, Tmp1).addReg(SHReg).addSImm(32);
1309 if (ISD::SHL_PARTS == opcode) {
1310 BuildMI(BB, PPC::SLW, 2, Tmp2).addReg(ShiftOpHi).addReg(SHReg);
1311 BuildMI(BB, PPC::SRW, 2, Tmp3).addReg(ShiftOpLo).addReg(Tmp1);
1312 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1313 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
1314 BuildMI(BB, PPC::SLW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1315 BuildMI(BB, PPC::OR, 2, Result+1).addReg(Tmp4).addReg(Tmp6);
1316 BuildMI(BB, PPC::SLW, 2, Result).addReg(ShiftOpLo).addReg(SHReg);
1317 } else if (ISD::SRL_PARTS == opcode) {
1318 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1319 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1320 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1321 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
1322 BuildMI(BB, PPC::SRW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1323 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp4).addReg(Tmp6);
1324 BuildMI(BB, PPC::SRW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1325 } else {
1326 MachineBasicBlock *TmpMBB = new MachineBasicBlock(BB->getBasicBlock());
1327 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1328 MachineBasicBlock *OldMBB = BB;
1329 MachineFunction *F = BB->getParent();
1330 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1331 F->getBasicBlockList().insert(It, TmpMBB);
1332 F->getBasicBlockList().insert(It, PhiMBB);
1333 BB->addSuccessor(TmpMBB);
1334 BB->addSuccessor(PhiMBB);
1335 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1336 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1337 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1338 BuildMI(BB, PPC::ADDICo, 2, Tmp5).addReg(SHReg).addSImm(-32);
1339 BuildMI(BB, PPC::SRAW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1340 BuildMI(BB, PPC::SRAW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1341 BuildMI(BB, PPC::BLE, 2).addReg(PPC::CR0).addMBB(PhiMBB);
1342 // Select correct least significant half if the shift amount > 32
1343 BB = TmpMBB;
1344 unsigned Tmp7 = MakeReg(MVT::i32);
1345 BuildMI(BB, PPC::OR, 2, Tmp7).addReg(Tmp6).addReg(Tmp6);
1346 TmpMBB->addSuccessor(PhiMBB);
1347 BB = PhiMBB;
1348 BuildMI(BB, PPC::PHI, 4, Result).addReg(Tmp4).addMBB(OldMBB)
1349 .addReg(Tmp7).addMBB(TmpMBB);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001350 }
1351 return Result+N.ResNo;
1352 }
1353
Nate Begemana9795f82005-03-24 04:41:43 +00001354 case ISD::FP_TO_UINT:
Nate Begeman6b559972005-04-01 02:59:27 +00001355 case ISD::FP_TO_SINT: {
1356 bool U = (ISD::FP_TO_UINT == opcode);
1357 Tmp1 = SelectExpr(N.getOperand(0));
1358 if (!U) {
1359 Tmp2 = MakeReg(MVT::f64);
1360 BuildMI(BB, PPC::FCTIWZ, 1, Tmp2).addReg(Tmp1);
1361 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1362 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx);
1363 addFrameReference(BuildMI(BB, PPC::LWZ, 2, Result), FrameIdx, 4);
1364 return Result;
1365 } else {
1366 unsigned Zero = getConstDouble(0.0);
1367 unsigned MaxInt = getConstDouble((1LL << 32) - 1);
1368 unsigned Border = getConstDouble(1LL << 31);
1369 unsigned UseZero = MakeReg(MVT::f64);
1370 unsigned UseMaxInt = MakeReg(MVT::f64);
1371 unsigned UseChoice = MakeReg(MVT::f64);
1372 unsigned TmpReg = MakeReg(MVT::f64);
1373 unsigned TmpReg2 = MakeReg(MVT::f64);
1374 unsigned ConvReg = MakeReg(MVT::f64);
1375 unsigned IntTmp = MakeReg(MVT::i32);
1376 unsigned XorReg = MakeReg(MVT::i32);
1377 MachineFunction *F = BB->getParent();
1378 int FrameIdx = F->getFrameInfo()->CreateStackObject(8, 8);
1379 // Update machine-CFG edges
1380 MachineBasicBlock *XorMBB = new MachineBasicBlock(BB->getBasicBlock());
1381 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1382 MachineBasicBlock *OldMBB = BB;
1383 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1384 F->getBasicBlockList().insert(It, XorMBB);
1385 F->getBasicBlockList().insert(It, PhiMBB);
1386 BB->addSuccessor(XorMBB);
1387 BB->addSuccessor(PhiMBB);
1388 // Convert from floating point to unsigned 32-bit value
1389 // Use 0 if incoming value is < 0.0
1390 BuildMI(BB, PPC::FSEL, 3, UseZero).addReg(Tmp1).addReg(Tmp1).addReg(Zero);
1391 // Use 2**32 - 1 if incoming value is >= 2**32
1392 BuildMI(BB, PPC::FSUB, 2, UseMaxInt).addReg(MaxInt).addReg(Tmp1);
1393 BuildMI(BB, PPC::FSEL, 3, UseChoice).addReg(UseMaxInt).addReg(UseZero)
1394 .addReg(MaxInt);
1395 // Subtract 2**31
1396 BuildMI(BB, PPC::FSUB, 2, TmpReg).addReg(UseChoice).addReg(Border);
1397 // Use difference if >= 2**31
1398 BuildMI(BB, PPC::FCMPU, 2, PPC::CR0).addReg(UseChoice).addReg(Border);
1399 BuildMI(BB, PPC::FSEL, 3, TmpReg2).addReg(TmpReg).addReg(TmpReg)
1400 .addReg(UseChoice);
1401 // Convert to integer
1402 BuildMI(BB, PPC::FCTIWZ, 1, ConvReg).addReg(TmpReg2);
1403 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(ConvReg), FrameIdx);
1404 addFrameReference(BuildMI(BB, PPC::LWZ, 2, IntTmp), FrameIdx, 4);
1405 BuildMI(BB, PPC::BLT, 2).addReg(PPC::CR0).addMBB(PhiMBB);
1406 BuildMI(BB, PPC::B, 1).addMBB(XorMBB);
1407
1408 // XorMBB:
1409 // add 2**31 if input was >= 2**31
1410 BB = XorMBB;
1411 BuildMI(BB, PPC::XORIS, 2, XorReg).addReg(IntTmp).addImm(0x8000);
1412 XorMBB->addSuccessor(PhiMBB);
1413
1414 // PhiMBB:
1415 // DestReg = phi [ IntTmp, OldMBB ], [ XorReg, XorMBB ]
1416 BB = PhiMBB;
1417 BuildMI(BB, PPC::PHI, 4, Result).addReg(IntTmp).addMBB(OldMBB)
1418 .addReg(XorReg).addMBB(XorMBB);
1419 return Result;
1420 }
1421 assert(0 && "Should never get here");
1422 return 0;
1423 }
Nate Begemana9795f82005-03-24 04:41:43 +00001424
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001425 case ISD::SETCC:
Nate Begeman33162522005-03-29 21:54:38 +00001426 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
Nate Begemandffcfcc2005-04-01 00:32:34 +00001427 Opc = SelectSetCR0(N);
Nate Begeman33162522005-03-29 21:54:38 +00001428
Nate Begeman31318e42005-04-01 07:21:30 +00001429 unsigned TrueValue = MakeReg(MVT::i32);
1430 BuildMI(BB, PPC::LI, 1, TrueValue).addSImm(1);
1431 unsigned FalseValue = MakeReg(MVT::i32);
1432 BuildMI(BB, PPC::LI, 1, FalseValue).addSImm(0);
1433
Nate Begeman33162522005-03-29 21:54:38 +00001434 // Create an iterator with which to insert the MBB for copying the false
1435 // value and the MBB to hold the PHI instruction for this SetCC.
1436 MachineBasicBlock *thisMBB = BB;
1437 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1438 ilist<MachineBasicBlock>::iterator It = BB;
1439 ++It;
1440
1441 // thisMBB:
1442 // ...
1443 // cmpTY cr0, r1, r2
1444 // %TrueValue = li 1
1445 // bCC sinkMBB
Nate Begeman33162522005-03-29 21:54:38 +00001446 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1447 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1448 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
1449 MachineFunction *F = BB->getParent();
1450 F->getBasicBlockList().insert(It, copy0MBB);
1451 F->getBasicBlockList().insert(It, sinkMBB);
1452 // Update machine-CFG edges
1453 BB->addSuccessor(copy0MBB);
1454 BB->addSuccessor(sinkMBB);
1455
1456 // copy0MBB:
1457 // %FalseValue = li 0
1458 // fallthrough
1459 BB = copy0MBB;
Nate Begeman33162522005-03-29 21:54:38 +00001460 // Update machine-CFG edges
1461 BB->addSuccessor(sinkMBB);
1462
1463 // sinkMBB:
1464 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1465 // ...
1466 BB = sinkMBB;
1467 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1468 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1469 return Result;
1470 }
1471 assert(0 && "Is this legal?");
1472 return 0;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001473
Nate Begeman74747862005-03-29 22:24:51 +00001474 case ISD::SELECT: {
Chris Lattner30710192005-04-01 07:10:02 +00001475 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
1476 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman6cb2e1b2005-04-01 08:57:43 +00001477 Opc = SelectSetCR0(N.getOperand(0));
Chris Lattner30710192005-04-01 07:10:02 +00001478
Nate Begeman74747862005-03-29 22:24:51 +00001479 // Create an iterator with which to insert the MBB for copying the false
1480 // value and the MBB to hold the PHI instruction for this SetCC.
1481 MachineBasicBlock *thisMBB = BB;
1482 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1483 ilist<MachineBasicBlock>::iterator It = BB;
1484 ++It;
1485
1486 // thisMBB:
1487 // ...
1488 // TrueVal = ...
1489 // cmpTY cr0, r1, r2
1490 // bCC copy1MBB
1491 // fallthrough --> copy0MBB
Nate Begeman74747862005-03-29 22:24:51 +00001492 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1493 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman3e897162005-03-31 23:55:40 +00001494 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
Nate Begeman74747862005-03-29 22:24:51 +00001495 MachineFunction *F = BB->getParent();
1496 F->getBasicBlockList().insert(It, copy0MBB);
1497 F->getBasicBlockList().insert(It, sinkMBB);
1498 // Update machine-CFG edges
1499 BB->addSuccessor(copy0MBB);
1500 BB->addSuccessor(sinkMBB);
1501
1502 // copy0MBB:
1503 // %FalseValue = ...
1504 // # fallthrough to sinkMBB
1505 BB = copy0MBB;
Nate Begeman74747862005-03-29 22:24:51 +00001506 // Update machine-CFG edges
1507 BB->addSuccessor(sinkMBB);
1508
1509 // sinkMBB:
1510 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1511 // ...
1512 BB = sinkMBB;
1513 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1514 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1515
1516 // FIXME: Select i64?
1517 return Result;
1518 }
Nate Begemana9795f82005-03-24 04:41:43 +00001519
1520 case ISD::Constant:
1521 switch (N.getValueType()) {
1522 default: assert(0 && "Cannot use constants of this type!");
1523 case MVT::i1:
1524 BuildMI(BB, PPC::LI, 1, Result)
1525 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
1526 break;
1527 case MVT::i32:
1528 {
1529 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
1530 if (v < 32768 && v >= -32768) {
1531 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
1532 } else {
Nate Begeman5e966612005-03-24 06:28:42 +00001533 Tmp1 = MakeReg(MVT::i32);
1534 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
1535 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
Nate Begemana9795f82005-03-24 04:41:43 +00001536 }
1537 }
1538 }
1539 return Result;
1540 }
1541
1542 return 0;
1543}
1544
1545void ISel::Select(SDOperand N) {
1546 unsigned Tmp1, Tmp2, Opc;
1547 unsigned opcode = N.getOpcode();
1548
1549 if (!ExprMap.insert(std::make_pair(N, 1)).second)
1550 return; // Already selected.
1551
1552 SDNode *Node = N.Val;
1553
1554 switch (Node->getOpcode()) {
1555 default:
1556 Node->dump(); std::cerr << "\n";
1557 assert(0 && "Node not handled yet!");
1558 case ISD::EntryToken: return; // Noop
1559 case ISD::TokenFactor:
1560 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1561 Select(Node->getOperand(i));
1562 return;
1563 case ISD::ADJCALLSTACKDOWN:
1564 case ISD::ADJCALLSTACKUP:
1565 Select(N.getOperand(0));
1566 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1567 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? PPC::ADJCALLSTACKDOWN :
1568 PPC::ADJCALLSTACKUP;
1569 BuildMI(BB, Opc, 1).addImm(Tmp1);
1570 return;
1571 case ISD::BR: {
1572 MachineBasicBlock *Dest =
1573 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
Nate Begemana9795f82005-03-24 04:41:43 +00001574 Select(N.getOperand(0));
1575 BuildMI(BB, PPC::B, 1).addMBB(Dest);
1576 return;
1577 }
1578 case ISD::BRCOND:
1579 SelectBranchCC(N);
1580 return;
1581 case ISD::CopyToReg:
1582 Select(N.getOperand(0));
1583 Tmp1 = SelectExpr(N.getOperand(1));
1584 Tmp2 = cast<RegSDNode>(N)->getReg();
1585
1586 if (Tmp1 != Tmp2) {
1587 if (N.getOperand(1).getValueType() == MVT::f64 ||
1588 N.getOperand(1).getValueType() == MVT::f32)
1589 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
1590 else
1591 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1592 }
1593 return;
1594 case ISD::ImplicitDef:
1595 Select(N.getOperand(0));
1596 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
1597 return;
1598 case ISD::RET:
1599 switch (N.getNumOperands()) {
1600 default:
1601 assert(0 && "Unknown return instruction!");
1602 case 3:
1603 assert(N.getOperand(1).getValueType() == MVT::i32 &&
1604 N.getOperand(2).getValueType() == MVT::i32 &&
1605 "Unknown two-register value!");
1606 Select(N.getOperand(0));
1607 Tmp1 = SelectExpr(N.getOperand(1));
1608 Tmp2 = SelectExpr(N.getOperand(2));
Nate Begeman27523a12005-04-02 00:42:16 +00001609 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2);
1610 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00001611 break;
1612 case 2:
1613 Select(N.getOperand(0));
1614 Tmp1 = SelectExpr(N.getOperand(1));
1615 switch (N.getOperand(1).getValueType()) {
1616 default:
1617 assert(0 && "Unknown return type!");
1618 case MVT::f64:
1619 case MVT::f32:
1620 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
1621 break;
1622 case MVT::i32:
1623 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
1624 break;
1625 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001626 case 1:
1627 Select(N.getOperand(0));
1628 break;
Nate Begemana9795f82005-03-24 04:41:43 +00001629 }
1630 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
1631 return;
Nate Begemana9795f82005-03-24 04:41:43 +00001632 case ISD::TRUNCSTORE:
1633 case ISD::STORE:
1634 {
1635 SDOperand Chain = N.getOperand(0);
1636 SDOperand Value = N.getOperand(1);
1637 SDOperand Address = N.getOperand(2);
1638 Select(Chain);
1639
1640 Tmp1 = SelectExpr(Value); //value
1641
1642 if (opcode == ISD::STORE) {
1643 switch(Value.getValueType()) {
1644 default: assert(0 && "unknown Type in store");
1645 case MVT::i32: Opc = PPC::STW; break;
1646 case MVT::f64: Opc = PPC::STFD; break;
1647 case MVT::f32: Opc = PPC::STFS; break;
1648 }
1649 } else { //ISD::TRUNCSTORE
1650 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1651 default: assert(0 && "unknown Type in store");
1652 case MVT::i1: //FIXME: DAG does not promote this load
1653 case MVT::i8: Opc = PPC::STB; break;
1654 case MVT::i16: Opc = PPC::STH; break;
1655 }
1656 }
1657
Nate Begemana7e11a42005-04-01 05:57:17 +00001658 if(Address.getOpcode() == ISD::FrameIndex)
Nate Begemana9795f82005-03-24 04:41:43 +00001659 {
Nate Begeman58f718c2005-03-30 02:23:08 +00001660 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
1661 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001662 }
1663 else
1664 {
1665 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00001666 bool idx = SelectAddr(Address, Tmp2, offset);
1667 if (idx) {
1668 Opc = IndexedOpForOp(Opc);
1669 BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset);
1670 } else {
1671 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1672 }
Nate Begemana9795f82005-03-24 04:41:43 +00001673 }
1674 return;
1675 }
1676 case ISD::EXTLOAD:
1677 case ISD::SEXTLOAD:
1678 case ISD::ZEXTLOAD:
1679 case ISD::LOAD:
1680 case ISD::CopyFromReg:
1681 case ISD::CALL:
1682 case ISD::DYNAMIC_STACKALLOC:
1683 ExprMap.erase(N);
1684 SelectExpr(N);
1685 return;
1686 }
1687 assert(0 && "Should not be reached!");
1688}
1689
1690
1691/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
1692/// into a machine code representation using pattern matching and a machine
1693/// description file.
1694///
1695FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) {
1696 return new ISel(TM);
Chris Lattner246fa632005-03-24 06:16:18 +00001697}
1698