blob: 019d401db5a16c8bca3283b7aded76cc2e59050d [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) {
43 // Set up the TargetLowering object.
44
45 // Set up the register classes.
46 addRegisterClass(MVT::i32, PPC32::GPRCRegisterClass);
Nate Begeman7532e2f2005-03-26 08:25:22 +000047 addRegisterClass(MVT::f32, PPC32::FPRCRegisterClass);
Nate Begemana9795f82005-03-24 04:41:43 +000048 addRegisterClass(MVT::f64, PPC32::FPRCRegisterClass);
49
50 computeRegisterProperties();
51 }
52
53 /// LowerArguments - This hook must be implemented to indicate how we should
54 /// lower the arguments for the specified function, into the specified DAG.
55 virtual std::vector<SDOperand>
56 LowerArguments(Function &F, SelectionDAG &DAG);
57
58 /// LowerCallTo - This hook lowers an abstract call to a function into an
59 /// actual call.
60 virtual std::pair<SDOperand, SDOperand>
Nate Begeman307e7442005-03-26 01:28:53 +000061 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
62 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Nate Begemana9795f82005-03-24 04:41:43 +000063
64 virtual std::pair<SDOperand, SDOperand>
65 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
66
67 virtual std::pair<SDOperand,SDOperand>
68 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
69 const Type *ArgTy, SelectionDAG &DAG);
70
71 virtual std::pair<SDOperand, SDOperand>
72 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
73 SelectionDAG &DAG);
74 };
75}
76
77
78std::vector<SDOperand>
79PPC32TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
80 //
81 // add beautiful description of PPC stack frame format, or at least some docs
82 //
83 MachineFunction &MF = DAG.getMachineFunction();
84 MachineFrameInfo *MFI = MF.getFrameInfo();
85 MachineBasicBlock& BB = MF.front();
86 std::vector<SDOperand> ArgValues;
87
88 // Due to the rather complicated nature of the PowerPC ABI, rather than a
89 // fixed size array of physical args, for the sake of simplicity let the STL
90 // handle tracking them for us.
91 std::vector<unsigned> argVR, argPR, argOp;
92 unsigned ArgOffset = 24;
93 unsigned GPR_remaining = 8;
94 unsigned FPR_remaining = 13;
95 unsigned GPR_idx = 0, FPR_idx = 0;
96 static const unsigned GPR[] = {
97 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
98 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
99 };
100 static const unsigned FPR[] = {
101 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
102 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
103 };
104
105 // Add DAG nodes to load the arguments... On entry to a function on PPC,
106 // the arguments start at offset 24, although they are likely to be passed
107 // in registers.
108 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
109 SDOperand newroot, argt;
110 unsigned ObjSize;
111 bool needsLoad = false;
112 MVT::ValueType ObjectVT = getValueType(I->getType());
113
114 switch (ObjectVT) {
115 default: assert(0 && "Unhandled argument type!");
116 case MVT::i1:
117 case MVT::i8:
118 case MVT::i16:
119 case MVT::i32:
120 ObjSize = 4;
121 if (GPR_remaining > 0) {
122 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
123 unsigned virtReg =
124 MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i32));
125 argt = newroot = DAG.getCopyFromReg(virtReg, MVT::i32, DAG.getRoot());
126 if (ObjectVT != MVT::i32)
127 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, newroot);
128 argVR.push_back(virtReg);
129 argPR.push_back(GPR[GPR_idx]);
130 argOp.push_back(PPC::OR);
131 } else {
132 needsLoad = true;
133 }
134 break;
Nate Begemanf7e43382005-03-26 07:46:36 +0000135 case MVT::i64: ObjSize = 8;
136 // FIXME: can split 64b load between reg/mem if it is last arg in regs
Nate Begemana9795f82005-03-24 04:41:43 +0000137 if (GPR_remaining > 1) {
138 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
139 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx+1]);
140 MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i32));
141 unsigned virtReg =
142 MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i32))-1;
143 // FIXME: is this correct?
144 argt = newroot = DAG.getCopyFromReg(virtReg, MVT::i32, DAG.getRoot());
145 argt = DAG.getCopyFromReg(virtReg+1, MVT::i32, newroot);
146 // Push the arguments for emitting into BB later
147 argVR.push_back(virtReg); argVR.push_back(virtReg+1);
148 argPR.push_back(GPR[GPR_idx]); argPR.push_back(GPR[GPR_idx+1]);
149 argOp.push_back(PPC::OR); argOp.push_back(PPC::OR);
150 } 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]);
158 unsigned virtReg =
159 MF.getSSARegMap()->createVirtualRegister(getRegClassFor(ObjectVT));
160 argt = newroot = DAG.getCopyFromReg(virtReg, ObjectVT, DAG.getRoot());
161 argVR.push_back(virtReg);
162 argPR.push_back(FPR[FPR_idx]);
163 argOp.push_back(PPC::FMR);
164 --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
193 for (int i = 0, count = argVR.size(); i < count; ++i) {
194 if (argOp[i] == PPC::FMR)
195 BuildMI(&BB, argOp[i], 1, argVR[i]).addReg(argPR[i]);
196 else
197 BuildMI(&BB, argOp[i], 2, argVR[i]).addReg(argPR[i]).addReg(argPR[i]);
198 }
199
200 // If the function takes variable number of arguments, make a frame index for
201 // the start of the first vararg value... for expansion of llvm.va_start.
202 if (F.isVarArg())
203 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
204
205 return ArgValues;
206}
207
208std::pair<SDOperand, SDOperand>
209PPC32TargetLowering::LowerCallTo(SDOperand Chain,
Nate Begeman307e7442005-03-26 01:28:53 +0000210 const Type *RetTy, bool isVarArg,
211 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG) {
212 // args_to_use will accumulate outgoing args for the ISD::CALL case in
213 // SelectExpr to use to put the arguments in the appropriate registers.
Nate Begemana9795f82005-03-24 04:41:43 +0000214 std::vector<SDOperand> args_to_use;
Nate Begeman307e7442005-03-26 01:28:53 +0000215
216 // Count how many bytes are to be pushed on the stack, including the linkage
217 // area, and parameter passing area.
218 unsigned NumBytes = 24;
219
220 if (Args.empty()) {
221 NumBytes = 0; // Save zero bytes.
222 } else {
223 for (unsigned i = 0, e = Args.size(); i != e; ++i)
224 switch (getValueType(Args[i].second)) {
225 default: assert(0 && "Unknown value type!");
226 case MVT::i1:
227 case MVT::i8:
228 case MVT::i16:
229 case MVT::i32:
230 case MVT::f32:
231 NumBytes += 4;
232 break;
233 case MVT::i64:
234 case MVT::f64:
235 NumBytes += 8;
236 break;
237 }
238
239 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
240 // plus 32 bytes of argument space in case any called code gets funky on us.
241 if (NumBytes < 56) NumBytes = 56;
242
243 // Adjust the stack pointer for the new arguments...
244 // These operations are automatically eliminated by the prolog/epilog pass
245 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
246 DAG.getConstant(NumBytes, getPointerTy()));
247
248 // Set up a copy of the stack pointer for use loading and storing any
249 // arguments that may not fit in the registers available for argument
250 // passing.
251 SDOperand StackPtr = DAG.getCopyFromReg(PPC::R1, MVT::i32,
252 DAG.getEntryNode());
253
254 // Figure out which arguments are going to go in registers, and which in
255 // memory. Also, if this is a vararg function, floating point operations
256 // must be stored to our stack, and loaded into integer regs as well, if
257 // any integer regs are available for argument passing.
258 unsigned ArgOffset = 24;
259 unsigned GPR_remaining = 8;
260 unsigned FPR_remaining = 13;
261 std::vector<SDOperand> Stores;
262 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
263 // PtrOff will be used to store the current argument to the stack if a
264 // register cannot be found for it.
265 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
266 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Nate Begemanf7e43382005-03-26 07:46:36 +0000267 MVT::ValueType ArgVT = getValueType(Args[i].second);
Nate Begeman307e7442005-03-26 01:28:53 +0000268
Nate Begemanf7e43382005-03-26 07:46:36 +0000269 switch (ArgVT) {
Nate Begeman307e7442005-03-26 01:28:53 +0000270 default: assert(0 && "Unexpected ValueType for argument!");
271 case MVT::i1:
272 case MVT::i8:
273 case MVT::i16:
274 // Promote the integer to 32 bits. If the input type is signed use a
275 // sign extend, otherwise use a zero extend.
276 if (Args[i].second->isSigned())
277 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
278 else
279 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
280 // FALL THROUGH
281 case MVT::i32:
282 if (GPR_remaining > 0) {
283 args_to_use.push_back(Args[i].first);
284 --GPR_remaining;
285 } else {
286 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
287 Args[i].first, PtrOff));
288 }
289 ArgOffset += 4;
290 break;
291 case MVT::i64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000292 // If we have one free GPR left, we can place the upper half of the i64
293 // in it, and store the other half to the stack. If we have two or more
294 // free GPRs, then we can pass both halves of the i64 in registers.
295 if (GPR_remaining > 0) {
Nate Begemanf2622612005-03-26 02:17:46 +0000296 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
297 Args[i].first, DAG.getConstant(1, MVT::i32));
298 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
299 Args[i].first, DAG.getConstant(0, MVT::i32));
Nate Begemanf7e43382005-03-26 07:46:36 +0000300 args_to_use.push_back(Hi);
301 if (GPR_remaining > 1) {
302 args_to_use.push_back(Lo);
303 GPR_remaining -= 2;
304 } else {
305 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
306 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
307 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
308 Lo, PtrOff));
309 --GPR_remaining;
310 }
Nate Begeman307e7442005-03-26 01:28:53 +0000311 } else {
312 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
313 Args[i].first, PtrOff));
314 }
315 ArgOffset += 8;
316 break;
317 case MVT::f32:
Nate Begeman307e7442005-03-26 01:28:53 +0000318 case MVT::f64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000319 if (FPR_remaining > 0) {
320 if (isVarArg) {
321 // FIXME: Need FunctionType information so we can conditionally
322 // store only the non-fixed arguments in a vararg function.
323 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
324 Args[i].first, PtrOff));
Nate Begeman7532e2f2005-03-26 08:25:22 +0000325 // FIXME: Need a way to communicate to the ISD::CALL select code
326 // that a particular argument is non-fixed so that we can load them
327 // into the correct GPR to shadow the FPR
Nate Begemanf7e43382005-03-26 07:46:36 +0000328 }
Nate Begemanf2622612005-03-26 02:17:46 +0000329 args_to_use.push_back(Args[i].first);
Nate Begeman307e7442005-03-26 01:28:53 +0000330 --FPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000331 // If we have any FPRs remaining, we may also have GPRs remaining.
332 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
333 // GPRs.
Nate Begeman307e7442005-03-26 01:28:53 +0000334 if (GPR_remaining > 0) --GPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000335 if (GPR_remaining > 0 && MVT::f64 == ArgVT) --GPR_remaining;
Nate Begeman307e7442005-03-26 01:28:53 +0000336 } else {
337 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
338 Args[i].first, PtrOff));
339 }
Nate Begemanf7e43382005-03-26 07:46:36 +0000340 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
Nate Begeman307e7442005-03-26 01:28:53 +0000341 break;
342 }
Nate Begemana9795f82005-03-24 04:41:43 +0000343 }
Nate Begeman307e7442005-03-26 01:28:53 +0000344 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
Nate Begemana9795f82005-03-24 04:41:43 +0000345 }
346
347 std::vector<MVT::ValueType> RetVals;
348 MVT::ValueType RetTyVT = getValueType(RetTy);
349 if (RetTyVT != MVT::isVoid)
350 RetVals.push_back(RetTyVT);
351 RetVals.push_back(MVT::Other);
352
353 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
354 Chain, Callee, args_to_use), 0);
355 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
356 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
357 DAG.getConstant(NumBytes, getPointerTy()));
358 return std::make_pair(TheCall, Chain);
359}
360
361std::pair<SDOperand, SDOperand>
362PPC32TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
363 //vastart just returns the address of the VarArgsFrameIndex slot.
364 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
365}
366
367std::pair<SDOperand,SDOperand> PPC32TargetLowering::
368LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
369 const Type *ArgTy, SelectionDAG &DAG) {
Nate Begemanc7b09f12005-03-25 08:34:25 +0000370 MVT::ValueType ArgVT = getValueType(ArgTy);
371 SDOperand Result;
372 if (!isVANext) {
373 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList);
374 } else {
375 unsigned Amt;
376 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
377 Amt = 4;
378 else {
379 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
380 "Other types should have been promoted for varargs!");
381 Amt = 8;
382 }
383 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
384 DAG.getConstant(Amt, VAList.getValueType()));
385 }
386 return std::make_pair(Result, Chain);
Nate Begemana9795f82005-03-24 04:41:43 +0000387}
388
389
390std::pair<SDOperand, SDOperand> PPC32TargetLowering::
391LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
392 SelectionDAG &DAG) {
393 abort();
394}
395
396namespace {
397
398//===--------------------------------------------------------------------===//
399/// ISel - PPC32 specific code to select PPC32 machine instructions for
400/// SelectionDAG operations.
401//===--------------------------------------------------------------------===//
402class ISel : public SelectionDAGISel {
403
404 /// Comment Here.
405 PPC32TargetLowering PPC32Lowering;
406
407 /// ExprMap - As shared expressions are codegen'd, we keep track of which
408 /// vreg the value is produced in, so we only emit one copy of each compiled
409 /// tree.
410 std::map<SDOperand, unsigned> ExprMap;
Nate Begemanc7b09f12005-03-25 08:34:25 +0000411
412 unsigned GlobalBaseReg;
413 bool GlobalBaseInitialized;
Nate Begemana9795f82005-03-24 04:41:43 +0000414
415public:
416 ISel(TargetMachine &TM) : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM)
417 {}
418
Nate Begemanc7b09f12005-03-25 08:34:25 +0000419 /// runOnFunction - Override this function in order to reset our per-function
420 /// variables.
421 virtual bool runOnFunction(Function &Fn) {
422 // Make sure we re-emit a set of the global base reg if necessary
423 GlobalBaseInitialized = false;
424 return SelectionDAGISel::runOnFunction(Fn);
425 }
426
Nate Begemana9795f82005-03-24 04:41:43 +0000427 /// InstructionSelectBasicBlock - This callback is invoked by
428 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
429 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
430 DEBUG(BB->dump());
431 // Codegen the basic block.
432 Select(DAG.getRoot());
433
434 // Clear state used for selection.
435 ExprMap.clear();
436 }
437
Nate Begemanc7b09f12005-03-25 08:34:25 +0000438 unsigned ISel::getGlobalBaseReg();
Nate Begemana9795f82005-03-24 04:41:43 +0000439 unsigned SelectExpr(SDOperand N);
440 unsigned SelectExprFP(SDOperand N, unsigned Result);
441 void Select(SDOperand N);
442
443 void SelectAddr(SDOperand N, unsigned& Reg, int& offset);
444 void SelectBranchCC(SDOperand N);
445};
446
447/// canUseAsImmediateForOpcode - This method returns a value indicating whether
448/// the ConstantSDNode N can be used as an immediate to Opcode. The return
449/// values are either 0, 1 or 2. 0 indicates that either N is not a
450/// ConstantSDNode, or is not suitable for use by that opcode. A return value
451/// of 1 indicates that the constant may be used in normal immediate form. A
452/// return value of 2 indicates that the constant may be used in shifted
453/// immediate form. If the return value is nonzero, the constant value is
454/// placed in Imm.
455///
456static unsigned canUseAsImmediateForOpcode(SDOperand N, unsigned Opcode,
457 unsigned& Imm) {
458 if (N.getOpcode() != ISD::Constant) return 0;
459
460 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
461
462 switch(Opcode) {
463 default: return 0;
464 case ISD::ADD:
465 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
466 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
467 break;
468 case ISD::AND:
469 case ISD::XOR:
470 case ISD::OR:
471 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
472 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
473 break;
Nate Begeman307e7442005-03-26 01:28:53 +0000474 case ISD::MUL:
475 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
476 break;
Nate Begemana9795f82005-03-24 04:41:43 +0000477 }
478 return 0;
479}
480}
481
Nate Begemanc7b09f12005-03-25 08:34:25 +0000482/// getGlobalBaseReg - Output the instructions required to put the
483/// base address to use for accessing globals into a register.
484///
485unsigned ISel::getGlobalBaseReg() {
486 if (!GlobalBaseInitialized) {
487 // Insert the set of GlobalBaseReg into the first MBB of the function
488 MachineBasicBlock &FirstMBB = BB->getParent()->front();
489 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
490 GlobalBaseReg = MakeReg(MVT::i32);
491 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
492 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR);
493 GlobalBaseInitialized = true;
494 }
495 return GlobalBaseReg;
496}
497
Nate Begemana9795f82005-03-24 04:41:43 +0000498//Check to see if the load is a constant offset from a base register
499void ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
500{
501 Reg = SelectExpr(N);
502 offset = 0;
503 return;
504}
505
506void ISel::SelectBranchCC(SDOperand N)
507{
508 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
509 MachineBasicBlock *Dest =
510 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
511 unsigned Opc;
512
513 Select(N.getOperand(0)); //chain
514 SDOperand CC = N.getOperand(1);
515
516 //Giveup and do the stupid thing
517 unsigned Tmp1 = SelectExpr(CC);
518 BuildMI(BB, PPC::BNE, 2).addReg(Tmp1).addMBB(Dest);
519 return;
520}
521
522unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
523{
524 unsigned Tmp1, Tmp2, Tmp3;
525 unsigned Opc = 0;
526 SDNode *Node = N.Val;
527 MVT::ValueType DestType = N.getValueType();
528 unsigned opcode = N.getOpcode();
529
530 switch (opcode) {
531 default:
532 Node->dump();
533 assert(0 && "Node not handled!\n");
534
535 case ISD::SELECT:
536 abort();
537
538 case ISD::FP_ROUND:
539 assert (DestType == MVT::f32 &&
540 N.getOperand(0).getValueType() == MVT::f64 &&
541 "only f64 to f32 conversion supported here");
542 Tmp1 = SelectExpr(N.getOperand(0));
543 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
544 return Result;
545
546 case ISD::FP_EXTEND:
547 assert (DestType == MVT::f64 &&
548 N.getOperand(0).getValueType() == MVT::f32 &&
549 "only f32 to f64 conversion supported here");
550 Tmp1 = SelectExpr(N.getOperand(0));
551 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
552 return Result;
553
554 case ISD::CopyFromReg:
Nate Begemanf2622612005-03-26 02:17:46 +0000555 if (Result == 1)
556 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
557 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
558 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
559 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000560
561 case ISD::LOAD:
Nate Begeman9db505c2005-03-28 19:36:43 +0000562 case ISD::EXTLOAD: {
563 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
564 Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType();
565
566 // Make sure we generate both values.
567 if (Result != 1)
568 ExprMap[N.getValue(1)] = 1; // Generate the token
569 else
570 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
571
572 SDOperand Chain = N.getOperand(0);
573 SDOperand Address = N.getOperand(1);
574 Select(Chain);
575
576 switch (TypeBeingLoaded) {
577 default: assert(0 && "Cannot fp load this type!");
578 case MVT::f32: Opc = PPC::LFS; break;
579 case MVT::f64: Opc = PPC::LFD; break;
580 }
581
582 if(Address.getOpcode() == ISD::FrameIndex) {
583 BuildMI(BB, Opc, 2, Result)
584 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
585 .addReg(PPC::R1);
586 } else {
587 int offset;
588 SelectAddr(Address, Tmp1, offset);
589 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
590 }
591 return Result;
592 }
Nate Begemana9795f82005-03-24 04:41:43 +0000593
594 case ISD::ConstantFP:
595 abort();
596
597 case ISD::MUL:
598 case ISD::ADD:
599 case ISD::SUB:
600 case ISD::SDIV:
601 switch( opcode ) {
602 case ISD::MUL: Opc = DestType == MVT::f64 ? PPC::FMUL : PPC::FMULS; break;
603 case ISD::ADD: Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS; break;
604 case ISD::SUB: Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS; break;
605 case ISD::SDIV: Opc = DestType == MVT::f64 ? PPC::FDIV : PPC::FDIVS; break;
606 };
607
608 Tmp1 = SelectExpr(N.getOperand(0));
609 Tmp2 = SelectExpr(N.getOperand(1));
610 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
611 return Result;
612
Nate Begemana9795f82005-03-24 04:41:43 +0000613 case ISD::UINT_TO_FP:
614 case ISD::SINT_TO_FP:
615 abort();
616 }
617 assert(0 && "should not get here");
618 return 0;
619}
620
621unsigned ISel::SelectExpr(SDOperand N) {
622 unsigned Result;
623 unsigned Tmp1, Tmp2, Tmp3;
624 unsigned Opc = 0;
625 unsigned opcode = N.getOpcode();
626
627 SDNode *Node = N.Val;
628 MVT::ValueType DestType = N.getValueType();
629
630 unsigned &Reg = ExprMap[N];
631 if (Reg) return Reg;
632
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000633 if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::ADD_PARTS &&
634 N.getOpcode() != ISD::SUB_PARTS)
Nate Begemana9795f82005-03-24 04:41:43 +0000635 Reg = Result = (N.getValueType() != MVT::Other) ?
636 MakeReg(N.getValueType()) : 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000637 else {
638 // If this is a call instruction, make sure to prepare ALL of the result
639 // values as well as the chain.
640 if (N.getOpcode() == ISD::CALL) {
641 if (Node->getNumValues() == 1)
642 Reg = Result = 1; // Void call, just a chain.
643 else {
644 Result = MakeReg(Node->getValueType(0));
645 ExprMap[N.getValue(0)] = Result;
646 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
647 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
648 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
649 }
650 } else {
651 Result = MakeReg(Node->getValueType(0));
652 ExprMap[N.getValue(0)] = Result;
653 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
654 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
655 }
656 }
657
658 if (DestType == MVT::f64 || DestType == MVT::f32)
659 return SelectExprFP(N, Result);
Nate Begemana9795f82005-03-24 04:41:43 +0000660
661 switch (opcode) {
662 default:
663 Node->dump();
664 assert(0 && "Node not handled!\n");
665
666 case ISD::DYNAMIC_STACKALLOC:
Nate Begeman5e966612005-03-24 06:28:42 +0000667 // Generate both result values. FIXME: Need a better commment here?
668 if (Result != 1)
669 ExprMap[N.getValue(1)] = 1;
670 else
671 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
672
673 // FIXME: We are currently ignoring the requested alignment for handling
674 // greater than the stack alignment. This will need to be revisited at some
675 // point. Align = N.getOperand(2);
676 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
677 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
678 std::cerr << "Cannot allocate stack object with greater alignment than"
679 << " the stack alignment yet!";
680 abort();
681 }
682 Select(N.getOperand(0));
683 Tmp1 = SelectExpr(N.getOperand(1));
684 // Subtract size from stack pointer, thereby allocating some space.
685 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
686 // Put a pointer to the space into the result register by copying the SP
687 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
688 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000689
690 case ISD::ConstantPool:
691 abort();
692
693 case ISD::FrameIndex:
694 abort();
695
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000696 case ISD::GlobalAddress: {
697 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
698 unsigned Tmp1 = MakeReg(MVT::i32);
Nate Begemanc7b09f12005-03-25 08:34:25 +0000699 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
700 .addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000701 if (GV->hasWeakLinkage() || GV->isExternal()) {
702 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
703 } else {
704 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
705 }
706 return Result;
707 }
708
Nate Begeman5e966612005-03-24 06:28:42 +0000709 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +0000710 case ISD::EXTLOAD:
711 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000712 case ISD::SEXTLOAD: {
Nate Begeman9db505c2005-03-28 19:36:43 +0000713 bool sext = (ISD::SEXTLOAD == opcode);
714 bool byte = (MVT::i8 == Node->getValueType(0));
715 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
716 Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType();
717
Nate Begeman5e966612005-03-24 06:28:42 +0000718 // Make sure we generate both values.
719 if (Result != 1)
720 ExprMap[N.getValue(1)] = 1; // Generate the token
721 else
722 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
723
724 SDOperand Chain = N.getOperand(0);
725 SDOperand Address = N.getOperand(1);
726 Select(Chain);
727
Nate Begeman9db505c2005-03-28 19:36:43 +0000728 switch (TypeBeingLoaded) {
Nate Begeman5e966612005-03-24 06:28:42 +0000729 default: assert(0 && "Cannot load this type!");
Nate Begeman9db505c2005-03-28 19:36:43 +0000730 case MVT::i1: Opc = PPC::LBZ; break;
731 case MVT::i8: Opc = PPC::LBZ; break;
732 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
733 case MVT::i32: Opc = PPC::LWZ; break;
Nate Begeman5e966612005-03-24 06:28:42 +0000734 }
735
Nate Begeman9db505c2005-03-28 19:36:43 +0000736 // Since there's no load byte & sign extend instruction we have to split
737 // byte SEXTLOADs into lbz + extsb. This requires we make a temp register.
738 if (sext && byte) {
739 Tmp3 = Result;
740 Result = MakeReg(MVT::i32);
741 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000742 if(Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman5e966612005-03-24 06:28:42 +0000743 BuildMI(BB, Opc, 2, Result)
744 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
745 .addReg(PPC::R1);
746 } else {
747 int offset;
748 SelectAddr(Address, Tmp1, offset);
749 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
750 }
Nate Begeman9db505c2005-03-28 19:36:43 +0000751 if (sext && byte) {
752 BuildMI(BB, PPC::EXTSB, 1, Tmp3).addReg(Result);
753 Result = Tmp3;
754 }
Nate Begeman5e966612005-03-24 06:28:42 +0000755 return Result;
756 }
757
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000758 case ISD::CALL: {
759 // Lower the chain for this call.
760 Select(N.getOperand(0));
761 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
762
763 // get the virtual reg for each argument
764 std::vector<unsigned> VRegs;
765 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
766 VRegs.push_back(SelectExpr(N.getOperand(i)));
767
768 // The ABI specifies that the first 32 bytes of args may be passed in GPRs,
769 // and that 13 FPRs may also be used for passing any floating point args.
770 int GPR_remaining = 8, FPR_remaining = 13;
771 unsigned GPR_idx = 0, FPR_idx = 0;
772 static const unsigned GPR[] = {
773 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
774 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
775 };
776 static const unsigned FPR[] = {
777 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6,
778 PPC::F7, PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12,
779 PPC::F13
780 };
781
782 // move the vregs into the appropriate architected register or stack slot
783 for(int i = 0, e = VRegs.size(); i < e; ++i) {
784 unsigned OperandType = N.getOperand(i+2).getValueType();
785 switch(OperandType) {
786 default:
787 Node->dump();
788 N.getOperand(i).Val->dump();
789 std::cerr << "Type for " << i << " is: " <<
790 N.getOperand(i+2).getValueType() << "\n";
791 assert(0 && "Unknown value type for call");
792 case MVT::i1:
793 case MVT::i8:
794 case MVT::i16:
795 case MVT::i32:
796 if (GPR_remaining > 0)
797 BuildMI(BB, PPC::OR, 2, GPR[GPR_idx]).addReg(VRegs[i])
798 .addReg(VRegs[i]);
799 break;
800 case MVT::f32:
801 case MVT::f64:
802 if (FPR_remaining > 0) {
803 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(VRegs[i]);
Nate Begemanf2622612005-03-26 02:17:46 +0000804 ++FPR_idx;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000805 --FPR_remaining;
806 }
807 break;
808 }
809 // All arguments consume GPRs available for argument passing
Nate Begemanf2622612005-03-26 02:17:46 +0000810 if (GPR_remaining > 0) {
811 ++GPR_idx;
812 --GPR_remaining;
813 }
814 if (MVT::f64 == OperandType && GPR_remaining > 0) {
815 ++GPR_idx;
816 --GPR_remaining;
817 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000818 }
819
820 // Emit the correct call instruction based on the type of symbol called.
821 if (GlobalAddressSDNode *GASD =
822 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
823 BuildMI(BB, PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(), true);
824 } else if (ExternalSymbolSDNode *ESSDN =
825 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
826 BuildMI(BB, PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(), true);
827 } else {
828 Tmp1 = SelectExpr(N.getOperand(1));
829 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
830 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
831 BuildMI(BB, PPC::CALLindirect, 3).addImm(20).addImm(0).addReg(PPC::R12);
832 }
833
834 switch (Node->getValueType(0)) {
835 default: assert(0 && "Unknown value type for call result!");
836 case MVT::Other: return 1;
837 case MVT::i1:
838 case MVT::i8:
839 case MVT::i16:
840 case MVT::i32:
Nate Begemanc7b09f12005-03-25 08:34:25 +0000841 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000842 if (Node->getValueType(1) == MVT::i32)
Nate Begemanc7b09f12005-03-25 08:34:25 +0000843 BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R4).addReg(PPC::R4);
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000844 break;
845 case MVT::f32:
846 case MVT::f64:
847 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
848 break;
849 }
850 return Result+N.ResNo;
851 }
Nate Begemana9795f82005-03-24 04:41:43 +0000852
853 case ISD::SIGN_EXTEND:
854 case ISD::SIGN_EXTEND_INREG:
855 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman9db505c2005-03-28 19:36:43 +0000856 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
857 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
858 case MVT::i16:
859 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
860 break;
861 case MVT::i8:
862 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
863 break;
864 }
Nate Begemana9795f82005-03-24 04:41:43 +0000865 return Result;
866
867 case ISD::ZERO_EXTEND_INREG:
868 Tmp1 = SelectExpr(N.getOperand(0));
869 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
Nate Begeman9db505c2005-03-28 19:36:43 +0000870 default: Node->dump(); assert(0 && "Unhandled ZERO_EXTEND type"); break;
Nate Begemana9795f82005-03-24 04:41:43 +0000871 case MVT::i16: Tmp2 = 16; break;
872 case MVT::i8: Tmp2 = 24; break;
873 case MVT::i1: Tmp2 = 31; break;
874 }
875 BuildMI(BB, PPC::RLWINM, 5, Result).addReg(Tmp1).addImm(0).addImm(0)
876 .addImm(Tmp2).addImm(31);
877 return Result;
878
Nate Begemana9795f82005-03-24 04:41:43 +0000879 case ISD::CopyFromReg:
880 if (Result == 1)
881 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
882 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
883 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
884 return Result;
885
886 case ISD::SHL:
Nate Begeman5e966612005-03-24 06:28:42 +0000887 Tmp1 = SelectExpr(N.getOperand(0));
888 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
889 Tmp2 = CN->getValue() & 0x1F;
890 BuildMI(BB, PPC::RLWINM, 5, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
891 .addImm(31-Tmp2);
892 } else {
893 Tmp2 = SelectExpr(N.getOperand(1));
894 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
895 }
896 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000897
Nate Begeman5e966612005-03-24 06:28:42 +0000898 case ISD::SRL:
899 Tmp1 = SelectExpr(N.getOperand(0));
900 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
901 Tmp2 = CN->getValue() & 0x1F;
902 BuildMI(BB, PPC::RLWINM, 5, Result).addReg(Tmp1).addImm(32-Tmp2)
903 .addImm(Tmp2).addImm(31);
904 } else {
905 Tmp2 = SelectExpr(N.getOperand(1));
906 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
907 }
908 return Result;
909
910 case ISD::SRA:
911 Tmp1 = SelectExpr(N.getOperand(0));
912 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
913 Tmp2 = CN->getValue() & 0x1F;
914 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
915 } else {
916 Tmp2 = SelectExpr(N.getOperand(1));
917 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
918 }
919 return Result;
920
Nate Begemana9795f82005-03-24 04:41:43 +0000921 case ISD::ADD:
922 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
923 Tmp1 = SelectExpr(N.getOperand(0));
924 switch(canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
925 default: assert(0 && "unhandled result code");
926 case 0: // No immediate
927 Tmp2 = SelectExpr(N.getOperand(1));
928 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
929 break;
930 case 1: // Low immediate
931 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
932 break;
933 case 2: // Shifted immediate
934 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
935 break;
936 }
937 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000938
Nate Begemana9795f82005-03-24 04:41:43 +0000939 case ISD::AND:
940 case ISD::OR:
941 case ISD::XOR:
942 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
943 Tmp1 = SelectExpr(N.getOperand(0));
944 switch(canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
945 default: assert(0 && "unhandled result code");
946 case 0: // No immediate
947 Tmp2 = SelectExpr(N.getOperand(1));
948 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +0000949 case ISD::AND: Opc = PPC::AND; break;
950 case ISD::OR: Opc = PPC::OR; break;
951 case ISD::XOR: Opc = PPC::XOR; break;
Nate Begemana9795f82005-03-24 04:41:43 +0000952 }
Nate Begeman5e966612005-03-24 06:28:42 +0000953 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +0000954 break;
955 case 1: // Low immediate
956 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +0000957 case ISD::AND: Opc = PPC::ANDIo; break;
958 case ISD::OR: Opc = PPC::ORI; break;
959 case ISD::XOR: Opc = PPC::XORI; break;
Nate Begemana9795f82005-03-24 04:41:43 +0000960 }
Nate Begeman5e966612005-03-24 06:28:42 +0000961 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +0000962 break;
963 case 2: // Shifted immediate
964 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +0000965 case ISD::AND: Opc = PPC::ANDISo; break;
966 case ISD::OR: Opc = PPC::ORIS; break;
967 case ISD::XOR: Opc = PPC::XORIS; break;
Nate Begemana9795f82005-03-24 04:41:43 +0000968 }
Nate Begeman5e966612005-03-24 06:28:42 +0000969 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +0000970 break;
971 }
972 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000973
974 case ISD::SUB:
975 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
976 Tmp1 = SelectExpr(N.getOperand(0));
977 Tmp2 = SelectExpr(N.getOperand(1));
978 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
979 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000980
Nate Begeman5e966612005-03-24 06:28:42 +0000981 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000982 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
983 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman307e7442005-03-26 01:28:53 +0000984 if (1 == canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
985 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
986 else {
987 Tmp2 = SelectExpr(N.getOperand(1));
988 BuildMI(BB, PPC::MULLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
989 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000990 return Result;
991
992 case ISD::ADD_PARTS:
993 case ISD::SUB_PARTS:
Nate Begemana9795f82005-03-24 04:41:43 +0000994 case ISD::UREM:
995 case ISD::SREM:
996 case ISD::SDIV:
997 case ISD::UDIV:
998 abort();
999
1000 case ISD::FP_TO_UINT:
1001 case ISD::FP_TO_SINT:
1002 abort();
1003
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001004 case ISD::SETCC:
1005 abort();
1006
Nate Begemana9795f82005-03-24 04:41:43 +00001007 case ISD::SELECT:
1008 abort();
1009
1010 case ISD::Constant:
1011 switch (N.getValueType()) {
1012 default: assert(0 && "Cannot use constants of this type!");
1013 case MVT::i1:
1014 BuildMI(BB, PPC::LI, 1, Result)
1015 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
1016 break;
1017 case MVT::i32:
1018 {
1019 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
1020 if (v < 32768 && v >= -32768) {
1021 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
1022 } else {
Nate Begeman5e966612005-03-24 06:28:42 +00001023 Tmp1 = MakeReg(MVT::i32);
1024 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
1025 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
Nate Begemana9795f82005-03-24 04:41:43 +00001026 }
1027 }
1028 }
1029 return Result;
1030 }
1031
1032 return 0;
1033}
1034
1035void ISel::Select(SDOperand N) {
1036 unsigned Tmp1, Tmp2, Opc;
1037 unsigned opcode = N.getOpcode();
1038
1039 if (!ExprMap.insert(std::make_pair(N, 1)).second)
1040 return; // Already selected.
1041
1042 SDNode *Node = N.Val;
1043
1044 switch (Node->getOpcode()) {
1045 default:
1046 Node->dump(); std::cerr << "\n";
1047 assert(0 && "Node not handled yet!");
1048 case ISD::EntryToken: return; // Noop
1049 case ISD::TokenFactor:
1050 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1051 Select(Node->getOperand(i));
1052 return;
1053 case ISD::ADJCALLSTACKDOWN:
1054 case ISD::ADJCALLSTACKUP:
1055 Select(N.getOperand(0));
1056 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1057 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? PPC::ADJCALLSTACKDOWN :
1058 PPC::ADJCALLSTACKUP;
1059 BuildMI(BB, Opc, 1).addImm(Tmp1);
1060 return;
1061 case ISD::BR: {
1062 MachineBasicBlock *Dest =
1063 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
Nate Begemana9795f82005-03-24 04:41:43 +00001064 Select(N.getOperand(0));
1065 BuildMI(BB, PPC::B, 1).addMBB(Dest);
1066 return;
1067 }
1068 case ISD::BRCOND:
1069 SelectBranchCC(N);
1070 return;
1071 case ISD::CopyToReg:
1072 Select(N.getOperand(0));
1073 Tmp1 = SelectExpr(N.getOperand(1));
1074 Tmp2 = cast<RegSDNode>(N)->getReg();
1075
1076 if (Tmp1 != Tmp2) {
1077 if (N.getOperand(1).getValueType() == MVT::f64 ||
1078 N.getOperand(1).getValueType() == MVT::f32)
1079 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
1080 else
1081 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1082 }
1083 return;
1084 case ISD::ImplicitDef:
1085 Select(N.getOperand(0));
1086 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
1087 return;
1088 case ISD::RET:
1089 switch (N.getNumOperands()) {
1090 default:
1091 assert(0 && "Unknown return instruction!");
1092 case 3:
1093 assert(N.getOperand(1).getValueType() == MVT::i32 &&
1094 N.getOperand(2).getValueType() == MVT::i32 &&
1095 "Unknown two-register value!");
1096 Select(N.getOperand(0));
1097 Tmp1 = SelectExpr(N.getOperand(1));
1098 Tmp2 = SelectExpr(N.getOperand(2));
1099 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
1100 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp2).addReg(Tmp2);
1101 break;
1102 case 2:
1103 Select(N.getOperand(0));
1104 Tmp1 = SelectExpr(N.getOperand(1));
1105 switch (N.getOperand(1).getValueType()) {
1106 default:
1107 assert(0 && "Unknown return type!");
1108 case MVT::f64:
1109 case MVT::f32:
1110 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
1111 break;
1112 case MVT::i32:
1113 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
1114 break;
1115 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001116 case 1:
1117 Select(N.getOperand(0));
1118 break;
Nate Begemana9795f82005-03-24 04:41:43 +00001119 }
1120 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
1121 return;
Nate Begemana9795f82005-03-24 04:41:43 +00001122 case ISD::TRUNCSTORE:
1123 case ISD::STORE:
1124 {
1125 SDOperand Chain = N.getOperand(0);
1126 SDOperand Value = N.getOperand(1);
1127 SDOperand Address = N.getOperand(2);
1128 Select(Chain);
1129
1130 Tmp1 = SelectExpr(Value); //value
1131
1132 if (opcode == ISD::STORE) {
1133 switch(Value.getValueType()) {
1134 default: assert(0 && "unknown Type in store");
1135 case MVT::i32: Opc = PPC::STW; break;
1136 case MVT::f64: Opc = PPC::STFD; break;
1137 case MVT::f32: Opc = PPC::STFS; break;
1138 }
1139 } else { //ISD::TRUNCSTORE
1140 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1141 default: assert(0 && "unknown Type in store");
1142 case MVT::i1: //FIXME: DAG does not promote this load
1143 case MVT::i8: Opc = PPC::STB; break;
1144 case MVT::i16: Opc = PPC::STH; break;
1145 }
1146 }
1147
1148 if (Address.getOpcode() == ISD::GlobalAddress)
1149 {
1150 BuildMI(BB, Opc, 2).addReg(Tmp1)
1151 .addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1152 }
1153 else if(Address.getOpcode() == ISD::FrameIndex)
1154 {
1155 BuildMI(BB, Opc, 2).addReg(Tmp1)
1156 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex());
1157 }
1158 else
1159 {
1160 int offset;
1161 SelectAddr(Address, Tmp2, offset);
1162 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1163 }
1164 return;
1165 }
1166 case ISD::EXTLOAD:
1167 case ISD::SEXTLOAD:
1168 case ISD::ZEXTLOAD:
1169 case ISD::LOAD:
1170 case ISD::CopyFromReg:
1171 case ISD::CALL:
1172 case ISD::DYNAMIC_STACKALLOC:
1173 ExprMap.erase(N);
1174 SelectExpr(N);
1175 return;
1176 }
1177 assert(0 && "Should not be reached!");
1178}
1179
1180
1181/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
1182/// into a machine code representation using pattern matching and a machine
1183/// description file.
1184///
1185FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) {
1186 return new ISel(TM);
Chris Lattner246fa632005-03-24 06:16:18 +00001187}
1188