blob: fcd8f438f109e632dd7651230d5ca720c1ec97dc [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]);
Nate Begemanf70b5762005-03-28 23:08:54 +0000123 argt = newroot = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32,
124 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000125 if (ObjectVT != MVT::i32)
126 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, newroot);
Nate Begemana9795f82005-03-24 04:41:43 +0000127 } else {
128 needsLoad = true;
129 }
130 break;
Nate Begemanf7e43382005-03-26 07:46:36 +0000131 case MVT::i64: ObjSize = 8;
132 // FIXME: can split 64b load between reg/mem if it is last arg in regs
Nate Begemana9795f82005-03-24 04:41:43 +0000133 if (GPR_remaining > 1) {
134 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
135 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx+1]);
Nate Begemanca12a2b2005-03-28 22:28:37 +0000136 // Copy the extracted halves into the virtual registers
Nate Begemanf70b5762005-03-28 23:08:54 +0000137 SDOperand argHi = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32,
138 DAG.getRoot());
139 SDOperand argLo = DAG.getCopyFromReg(GPR[GPR_idx+1], MVT::i32, argHi);
Nate Begemanca12a2b2005-03-28 22:28:37 +0000140 // Build the outgoing arg thingy
Nate Begemanf70b5762005-03-28 23:08:54 +0000141 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
142 newroot = argLo;
Nate Begemana9795f82005-03-24 04:41:43 +0000143 } else {
144 needsLoad = true;
145 }
146 break;
147 case MVT::f32: ObjSize = 4;
148 case MVT::f64: ObjSize = 8;
149 if (FPR_remaining > 0) {
150 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, FPR[FPR_idx]);
Nate Begemanf70b5762005-03-28 23:08:54 +0000151 argt = newroot = DAG.getCopyFromReg(FPR[FPR_idx], ObjectVT,
152 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000153 --FPR_remaining;
154 ++FPR_idx;
155 } else {
156 needsLoad = true;
157 }
158 break;
159 }
160
161 // We need to load the argument to a virtual register if we determined above
162 // that we ran out of physical registers of the appropriate type
163 if (needsLoad) {
164 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
165 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
166 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN);
167 }
168
169 // Every 4 bytes of argument space consumes one of the GPRs available for
170 // argument passing.
171 if (GPR_remaining > 0) {
172 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
173 GPR_remaining -= delta;
174 GPR_idx += delta;
175 }
176 ArgOffset += ObjSize;
177
178 DAG.setRoot(newroot.getValue(1));
179 ArgValues.push_back(argt);
180 }
181
Nate Begemana9795f82005-03-24 04:41:43 +0000182 // If the function takes variable number of arguments, make a frame index for
183 // the start of the first vararg value... for expansion of llvm.va_start.
184 if (F.isVarArg())
185 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
186
187 return ArgValues;
188}
189
190std::pair<SDOperand, SDOperand>
191PPC32TargetLowering::LowerCallTo(SDOperand Chain,
Nate Begeman307e7442005-03-26 01:28:53 +0000192 const Type *RetTy, bool isVarArg,
193 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG) {
194 // args_to_use will accumulate outgoing args for the ISD::CALL case in
195 // SelectExpr to use to put the arguments in the appropriate registers.
Nate Begemana9795f82005-03-24 04:41:43 +0000196 std::vector<SDOperand> args_to_use;
Nate Begeman307e7442005-03-26 01:28:53 +0000197
198 // Count how many bytes are to be pushed on the stack, including the linkage
199 // area, and parameter passing area.
200 unsigned NumBytes = 24;
201
202 if (Args.empty()) {
203 NumBytes = 0; // Save zero bytes.
204 } else {
205 for (unsigned i = 0, e = Args.size(); i != e; ++i)
206 switch (getValueType(Args[i].second)) {
207 default: assert(0 && "Unknown value type!");
208 case MVT::i1:
209 case MVT::i8:
210 case MVT::i16:
211 case MVT::i32:
212 case MVT::f32:
213 NumBytes += 4;
214 break;
215 case MVT::i64:
216 case MVT::f64:
217 NumBytes += 8;
218 break;
219 }
220
221 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
222 // plus 32 bytes of argument space in case any called code gets funky on us.
223 if (NumBytes < 56) NumBytes = 56;
224
225 // Adjust the stack pointer for the new arguments...
226 // These operations are automatically eliminated by the prolog/epilog pass
227 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
228 DAG.getConstant(NumBytes, getPointerTy()));
229
230 // Set up a copy of the stack pointer for use loading and storing any
231 // arguments that may not fit in the registers available for argument
232 // passing.
233 SDOperand StackPtr = DAG.getCopyFromReg(PPC::R1, MVT::i32,
234 DAG.getEntryNode());
235
236 // Figure out which arguments are going to go in registers, and which in
237 // memory. Also, if this is a vararg function, floating point operations
238 // must be stored to our stack, and loaded into integer regs as well, if
239 // any integer regs are available for argument passing.
240 unsigned ArgOffset = 24;
241 unsigned GPR_remaining = 8;
242 unsigned FPR_remaining = 13;
243 std::vector<SDOperand> Stores;
244 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
245 // PtrOff will be used to store the current argument to the stack if a
246 // register cannot be found for it.
247 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
248 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Nate Begemanf7e43382005-03-26 07:46:36 +0000249 MVT::ValueType ArgVT = getValueType(Args[i].second);
Nate Begeman307e7442005-03-26 01:28:53 +0000250
Nate Begemanf7e43382005-03-26 07:46:36 +0000251 switch (ArgVT) {
Nate Begeman307e7442005-03-26 01:28:53 +0000252 default: assert(0 && "Unexpected ValueType for argument!");
253 case MVT::i1:
254 case MVT::i8:
255 case MVT::i16:
256 // Promote the integer to 32 bits. If the input type is signed use a
257 // sign extend, otherwise use a zero extend.
258 if (Args[i].second->isSigned())
259 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
260 else
261 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
262 // FALL THROUGH
263 case MVT::i32:
264 if (GPR_remaining > 0) {
265 args_to_use.push_back(Args[i].first);
266 --GPR_remaining;
267 } else {
268 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
269 Args[i].first, PtrOff));
270 }
271 ArgOffset += 4;
272 break;
273 case MVT::i64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000274 // If we have one free GPR left, we can place the upper half of the i64
275 // in it, and store the other half to the stack. If we have two or more
276 // free GPRs, then we can pass both halves of the i64 in registers.
277 if (GPR_remaining > 0) {
Nate Begemanf2622612005-03-26 02:17:46 +0000278 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
279 Args[i].first, DAG.getConstant(1, MVT::i32));
280 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
281 Args[i].first, DAG.getConstant(0, MVT::i32));
Nate Begemanf7e43382005-03-26 07:46:36 +0000282 args_to_use.push_back(Hi);
283 if (GPR_remaining > 1) {
284 args_to_use.push_back(Lo);
285 GPR_remaining -= 2;
286 } else {
287 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
288 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
289 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
290 Lo, PtrOff));
291 --GPR_remaining;
292 }
Nate Begeman307e7442005-03-26 01:28:53 +0000293 } else {
294 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
295 Args[i].first, PtrOff));
296 }
297 ArgOffset += 8;
298 break;
299 case MVT::f32:
Nate Begeman307e7442005-03-26 01:28:53 +0000300 case MVT::f64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000301 if (FPR_remaining > 0) {
302 if (isVarArg) {
303 // FIXME: Need FunctionType information so we can conditionally
304 // store only the non-fixed arguments in a vararg function.
305 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
306 Args[i].first, PtrOff));
Nate Begeman7532e2f2005-03-26 08:25:22 +0000307 // FIXME: Need a way to communicate to the ISD::CALL select code
308 // that a particular argument is non-fixed so that we can load them
309 // into the correct GPR to shadow the FPR
Nate Begemanf7e43382005-03-26 07:46:36 +0000310 }
Nate Begemanf2622612005-03-26 02:17:46 +0000311 args_to_use.push_back(Args[i].first);
Nate Begeman307e7442005-03-26 01:28:53 +0000312 --FPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000313 // If we have any FPRs remaining, we may also have GPRs remaining.
314 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
315 // GPRs.
Nate Begeman307e7442005-03-26 01:28:53 +0000316 if (GPR_remaining > 0) --GPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000317 if (GPR_remaining > 0 && MVT::f64 == ArgVT) --GPR_remaining;
Nate Begeman307e7442005-03-26 01:28:53 +0000318 } else {
319 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
320 Args[i].first, PtrOff));
321 }
Nate Begemanf7e43382005-03-26 07:46:36 +0000322 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
Nate Begeman307e7442005-03-26 01:28:53 +0000323 break;
324 }
Nate Begemana9795f82005-03-24 04:41:43 +0000325 }
Nate Begeman307e7442005-03-26 01:28:53 +0000326 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
Nate Begemana9795f82005-03-24 04:41:43 +0000327 }
328
329 std::vector<MVT::ValueType> RetVals;
330 MVT::ValueType RetTyVT = getValueType(RetTy);
331 if (RetTyVT != MVT::isVoid)
332 RetVals.push_back(RetTyVT);
333 RetVals.push_back(MVT::Other);
334
335 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
336 Chain, Callee, args_to_use), 0);
337 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
338 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
339 DAG.getConstant(NumBytes, getPointerTy()));
340 return std::make_pair(TheCall, Chain);
341}
342
343std::pair<SDOperand, SDOperand>
344PPC32TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
345 //vastart just returns the address of the VarArgsFrameIndex slot.
346 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
347}
348
349std::pair<SDOperand,SDOperand> PPC32TargetLowering::
350LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
351 const Type *ArgTy, SelectionDAG &DAG) {
Nate Begemanc7b09f12005-03-25 08:34:25 +0000352 MVT::ValueType ArgVT = getValueType(ArgTy);
353 SDOperand Result;
354 if (!isVANext) {
355 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList);
356 } else {
357 unsigned Amt;
358 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
359 Amt = 4;
360 else {
361 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
362 "Other types should have been promoted for varargs!");
363 Amt = 8;
364 }
365 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
366 DAG.getConstant(Amt, VAList.getValueType()));
367 }
368 return std::make_pair(Result, Chain);
Nate Begemana9795f82005-03-24 04:41:43 +0000369}
370
371
372std::pair<SDOperand, SDOperand> PPC32TargetLowering::
373LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
374 SelectionDAG &DAG) {
375 abort();
376}
377
378namespace {
379
380//===--------------------------------------------------------------------===//
381/// ISel - PPC32 specific code to select PPC32 machine instructions for
382/// SelectionDAG operations.
383//===--------------------------------------------------------------------===//
384class ISel : public SelectionDAGISel {
385
386 /// Comment Here.
387 PPC32TargetLowering PPC32Lowering;
388
389 /// ExprMap - As shared expressions are codegen'd, we keep track of which
390 /// vreg the value is produced in, so we only emit one copy of each compiled
391 /// tree.
392 std::map<SDOperand, unsigned> ExprMap;
Nate Begemanc7b09f12005-03-25 08:34:25 +0000393
394 unsigned GlobalBaseReg;
395 bool GlobalBaseInitialized;
Nate Begemana9795f82005-03-24 04:41:43 +0000396
397public:
398 ISel(TargetMachine &TM) : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM)
399 {}
400
Nate Begemanc7b09f12005-03-25 08:34:25 +0000401 /// runOnFunction - Override this function in order to reset our per-function
402 /// variables.
403 virtual bool runOnFunction(Function &Fn) {
404 // Make sure we re-emit a set of the global base reg if necessary
405 GlobalBaseInitialized = false;
406 return SelectionDAGISel::runOnFunction(Fn);
407 }
408
Nate Begemana9795f82005-03-24 04:41:43 +0000409 /// InstructionSelectBasicBlock - This callback is invoked by
410 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
411 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
412 DEBUG(BB->dump());
413 // Codegen the basic block.
414 Select(DAG.getRoot());
415
416 // Clear state used for selection.
417 ExprMap.clear();
418 }
419
Nate Begemanc7b09f12005-03-25 08:34:25 +0000420 unsigned ISel::getGlobalBaseReg();
Nate Begemana9795f82005-03-24 04:41:43 +0000421 unsigned SelectExpr(SDOperand N);
422 unsigned SelectExprFP(SDOperand N, unsigned Result);
423 void Select(SDOperand N);
424
425 void SelectAddr(SDOperand N, unsigned& Reg, int& offset);
426 void SelectBranchCC(SDOperand N);
427};
428
429/// canUseAsImmediateForOpcode - This method returns a value indicating whether
430/// the ConstantSDNode N can be used as an immediate to Opcode. The return
431/// values are either 0, 1 or 2. 0 indicates that either N is not a
432/// ConstantSDNode, or is not suitable for use by that opcode. A return value
433/// of 1 indicates that the constant may be used in normal immediate form. A
434/// return value of 2 indicates that the constant may be used in shifted
435/// immediate form. If the return value is nonzero, the constant value is
436/// placed in Imm.
437///
438static unsigned canUseAsImmediateForOpcode(SDOperand N, unsigned Opcode,
439 unsigned& Imm) {
440 if (N.getOpcode() != ISD::Constant) return 0;
441
442 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
443
444 switch(Opcode) {
445 default: return 0;
446 case ISD::ADD:
447 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
448 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
449 break;
450 case ISD::AND:
451 case ISD::XOR:
452 case ISD::OR:
453 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
454 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
455 break;
Nate Begeman307e7442005-03-26 01:28:53 +0000456 case ISD::MUL:
457 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
458 break;
Nate Begemana9795f82005-03-24 04:41:43 +0000459 }
460 return 0;
461}
462}
463
Nate Begemanc7b09f12005-03-25 08:34:25 +0000464/// getGlobalBaseReg - Output the instructions required to put the
465/// base address to use for accessing globals into a register.
466///
467unsigned ISel::getGlobalBaseReg() {
468 if (!GlobalBaseInitialized) {
469 // Insert the set of GlobalBaseReg into the first MBB of the function
470 MachineBasicBlock &FirstMBB = BB->getParent()->front();
471 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
472 GlobalBaseReg = MakeReg(MVT::i32);
473 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
474 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR);
475 GlobalBaseInitialized = true;
476 }
477 return GlobalBaseReg;
478}
479
Nate Begemana9795f82005-03-24 04:41:43 +0000480//Check to see if the load is a constant offset from a base register
481void ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
482{
483 Reg = SelectExpr(N);
484 offset = 0;
485 return;
486}
487
488void ISel::SelectBranchCC(SDOperand N)
489{
490 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
491 MachineBasicBlock *Dest =
492 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
493 unsigned Opc;
494
495 Select(N.getOperand(0)); //chain
496 SDOperand CC = N.getOperand(1);
497
498 //Giveup and do the stupid thing
499 unsigned Tmp1 = SelectExpr(CC);
500 BuildMI(BB, PPC::BNE, 2).addReg(Tmp1).addMBB(Dest);
501 return;
502}
503
504unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
505{
506 unsigned Tmp1, Tmp2, Tmp3;
507 unsigned Opc = 0;
508 SDNode *Node = N.Val;
509 MVT::ValueType DestType = N.getValueType();
510 unsigned opcode = N.getOpcode();
511
512 switch (opcode) {
513 default:
514 Node->dump();
515 assert(0 && "Node not handled!\n");
516
517 case ISD::SELECT:
518 abort();
519
520 case ISD::FP_ROUND:
521 assert (DestType == MVT::f32 &&
522 N.getOperand(0).getValueType() == MVT::f64 &&
523 "only f64 to f32 conversion supported here");
524 Tmp1 = SelectExpr(N.getOperand(0));
525 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
526 return Result;
527
528 case ISD::FP_EXTEND:
529 assert (DestType == MVT::f64 &&
530 N.getOperand(0).getValueType() == MVT::f32 &&
531 "only f32 to f64 conversion supported here");
532 Tmp1 = SelectExpr(N.getOperand(0));
533 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
534 return Result;
535
536 case ISD::CopyFromReg:
Nate Begemanf2622612005-03-26 02:17:46 +0000537 if (Result == 1)
538 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
539 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
540 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
541 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000542
543 case ISD::LOAD:
Nate Begeman9db505c2005-03-28 19:36:43 +0000544 case ISD::EXTLOAD: {
545 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
546 Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType();
547
548 // Make sure we generate both values.
549 if (Result != 1)
550 ExprMap[N.getValue(1)] = 1; // Generate the token
551 else
552 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
553
554 SDOperand Chain = N.getOperand(0);
555 SDOperand Address = N.getOperand(1);
556 Select(Chain);
557
558 switch (TypeBeingLoaded) {
559 default: assert(0 && "Cannot fp load this type!");
560 case MVT::f32: Opc = PPC::LFS; break;
561 case MVT::f64: Opc = PPC::LFD; break;
562 }
563
564 if(Address.getOpcode() == ISD::FrameIndex) {
565 BuildMI(BB, Opc, 2, Result)
566 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
567 .addReg(PPC::R1);
568 } else {
569 int offset;
570 SelectAddr(Address, Tmp1, offset);
571 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
572 }
573 return Result;
574 }
Nate Begemana9795f82005-03-24 04:41:43 +0000575
576 case ISD::ConstantFP:
Nate Begemanca12a2b2005-03-28 22:28:37 +0000577 assert(0 && "ISD::ConstantFP Unimplemented");
Nate Begemana9795f82005-03-24 04:41:43 +0000578 abort();
579
580 case ISD::MUL:
581 case ISD::ADD:
582 case ISD::SUB:
583 case ISD::SDIV:
584 switch( opcode ) {
585 case ISD::MUL: Opc = DestType == MVT::f64 ? PPC::FMUL : PPC::FMULS; break;
586 case ISD::ADD: Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS; break;
587 case ISD::SUB: Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS; break;
588 case ISD::SDIV: Opc = DestType == MVT::f64 ? PPC::FDIV : PPC::FDIVS; break;
589 };
590
591 Tmp1 = SelectExpr(N.getOperand(0));
592 Tmp2 = SelectExpr(N.getOperand(1));
593 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
594 return Result;
595
Nate Begemana9795f82005-03-24 04:41:43 +0000596 case ISD::UINT_TO_FP:
597 case ISD::SINT_TO_FP:
Nate Begemanf3d08f32005-03-29 00:03:27 +0000598 assert(0 && "ISD::U/SINT_TO_FP Unimplemented");
Nate Begemana9795f82005-03-24 04:41:43 +0000599 abort();
600 }
601 assert(0 && "should not get here");
602 return 0;
603}
604
605unsigned ISel::SelectExpr(SDOperand N) {
606 unsigned Result;
607 unsigned Tmp1, Tmp2, Tmp3;
608 unsigned Opc = 0;
609 unsigned opcode = N.getOpcode();
610
611 SDNode *Node = N.Val;
612 MVT::ValueType DestType = N.getValueType();
613
614 unsigned &Reg = ExprMap[N];
615 if (Reg) return Reg;
616
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000617 if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::ADD_PARTS &&
618 N.getOpcode() != ISD::SUB_PARTS)
Nate Begemana9795f82005-03-24 04:41:43 +0000619 Reg = Result = (N.getValueType() != MVT::Other) ?
620 MakeReg(N.getValueType()) : 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000621 else {
622 // If this is a call instruction, make sure to prepare ALL of the result
623 // values as well as the chain.
624 if (N.getOpcode() == ISD::CALL) {
625 if (Node->getNumValues() == 1)
626 Reg = Result = 1; // Void call, just a chain.
627 else {
628 Result = MakeReg(Node->getValueType(0));
629 ExprMap[N.getValue(0)] = Result;
630 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
631 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
632 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
633 }
634 } else {
635 Result = MakeReg(Node->getValueType(0));
636 ExprMap[N.getValue(0)] = Result;
637 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
638 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
639 }
640 }
641
642 if (DestType == MVT::f64 || DestType == MVT::f32)
643 return SelectExprFP(N, Result);
Nate Begemana9795f82005-03-24 04:41:43 +0000644
645 switch (opcode) {
646 default:
647 Node->dump();
648 assert(0 && "Node not handled!\n");
649
650 case ISD::DYNAMIC_STACKALLOC:
Nate Begeman5e966612005-03-24 06:28:42 +0000651 // Generate both result values. FIXME: Need a better commment here?
652 if (Result != 1)
653 ExprMap[N.getValue(1)] = 1;
654 else
655 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
656
657 // FIXME: We are currently ignoring the requested alignment for handling
658 // greater than the stack alignment. This will need to be revisited at some
659 // point. Align = N.getOperand(2);
660 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
661 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
662 std::cerr << "Cannot allocate stack object with greater alignment than"
663 << " the stack alignment yet!";
664 abort();
665 }
666 Select(N.getOperand(0));
667 Tmp1 = SelectExpr(N.getOperand(1));
668 // Subtract size from stack pointer, thereby allocating some space.
669 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
670 // Put a pointer to the space into the result register by copying the SP
671 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
672 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000673
674 case ISD::ConstantPool:
Nate Begemanca12a2b2005-03-28 22:28:37 +0000675 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
676 Tmp2 = MakeReg(MVT::i32);
677 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp2).addReg(getGlobalBaseReg())
678 .addConstantPoolIndex(Tmp1);
679 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
680 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000681
682 case ISD::FrameIndex:
Nate Begemanf3d08f32005-03-29 00:03:27 +0000683 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
684 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1);
685 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000686
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000687 case ISD::GlobalAddress: {
688 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanca12a2b2005-03-28 22:28:37 +0000689 Tmp1 = MakeReg(MVT::i32);
Nate Begemanc7b09f12005-03-25 08:34:25 +0000690 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
691 .addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000692 if (GV->hasWeakLinkage() || GV->isExternal()) {
693 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
694 } else {
695 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
696 }
697 return Result;
698 }
699
Nate Begeman5e966612005-03-24 06:28:42 +0000700 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +0000701 case ISD::EXTLOAD:
702 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000703 case ISD::SEXTLOAD: {
Nate Begeman9db505c2005-03-28 19:36:43 +0000704 bool sext = (ISD::SEXTLOAD == opcode);
705 bool byte = (MVT::i8 == Node->getValueType(0));
706 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
707 Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType();
708
Nate Begeman5e966612005-03-24 06:28:42 +0000709 // Make sure we generate both values.
710 if (Result != 1)
711 ExprMap[N.getValue(1)] = 1; // Generate the token
712 else
713 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
714
715 SDOperand Chain = N.getOperand(0);
716 SDOperand Address = N.getOperand(1);
717 Select(Chain);
718
Nate Begeman9db505c2005-03-28 19:36:43 +0000719 switch (TypeBeingLoaded) {
Nate Begeman5e966612005-03-24 06:28:42 +0000720 default: assert(0 && "Cannot load this type!");
Nate Begeman9db505c2005-03-28 19:36:43 +0000721 case MVT::i1: Opc = PPC::LBZ; break;
722 case MVT::i8: Opc = PPC::LBZ; break;
723 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
724 case MVT::i32: Opc = PPC::LWZ; break;
Nate Begeman5e966612005-03-24 06:28:42 +0000725 }
726
Nate Begeman9db505c2005-03-28 19:36:43 +0000727 // Since there's no load byte & sign extend instruction we have to split
728 // byte SEXTLOADs into lbz + extsb. This requires we make a temp register.
729 if (sext && byte) {
730 Tmp3 = Result;
731 Result = MakeReg(MVT::i32);
732 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000733 if(Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman5e966612005-03-24 06:28:42 +0000734 BuildMI(BB, Opc, 2, Result)
735 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
736 .addReg(PPC::R1);
737 } else {
738 int offset;
739 SelectAddr(Address, Tmp1, offset);
740 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
741 }
Nate Begeman9db505c2005-03-28 19:36:43 +0000742 if (sext && byte) {
743 BuildMI(BB, PPC::EXTSB, 1, Tmp3).addReg(Result);
744 Result = Tmp3;
745 }
Nate Begeman5e966612005-03-24 06:28:42 +0000746 return Result;
747 }
748
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000749 case ISD::CALL: {
750 // Lower the chain for this call.
751 Select(N.getOperand(0));
752 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
753
754 // get the virtual reg for each argument
755 std::vector<unsigned> VRegs;
756 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
757 VRegs.push_back(SelectExpr(N.getOperand(i)));
758
759 // The ABI specifies that the first 32 bytes of args may be passed in GPRs,
760 // and that 13 FPRs may also be used for passing any floating point args.
761 int GPR_remaining = 8, FPR_remaining = 13;
762 unsigned GPR_idx = 0, FPR_idx = 0;
763 static const unsigned GPR[] = {
764 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
765 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
766 };
767 static const unsigned FPR[] = {
768 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6,
769 PPC::F7, PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12,
770 PPC::F13
771 };
772
773 // move the vregs into the appropriate architected register or stack slot
774 for(int i = 0, e = VRegs.size(); i < e; ++i) {
775 unsigned OperandType = N.getOperand(i+2).getValueType();
776 switch(OperandType) {
777 default:
778 Node->dump();
779 N.getOperand(i).Val->dump();
780 std::cerr << "Type for " << i << " is: " <<
781 N.getOperand(i+2).getValueType() << "\n";
782 assert(0 && "Unknown value type for call");
783 case MVT::i1:
784 case MVT::i8:
785 case MVT::i16:
786 case MVT::i32:
787 if (GPR_remaining > 0)
788 BuildMI(BB, PPC::OR, 2, GPR[GPR_idx]).addReg(VRegs[i])
789 .addReg(VRegs[i]);
790 break;
791 case MVT::f32:
792 case MVT::f64:
793 if (FPR_remaining > 0) {
794 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(VRegs[i]);
Nate Begemanf2622612005-03-26 02:17:46 +0000795 ++FPR_idx;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000796 --FPR_remaining;
797 }
798 break;
799 }
800 // All arguments consume GPRs available for argument passing
Nate Begemanf2622612005-03-26 02:17:46 +0000801 if (GPR_remaining > 0) {
802 ++GPR_idx;
803 --GPR_remaining;
804 }
805 if (MVT::f64 == OperandType && GPR_remaining > 0) {
806 ++GPR_idx;
807 --GPR_remaining;
808 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000809 }
810
811 // Emit the correct call instruction based on the type of symbol called.
812 if (GlobalAddressSDNode *GASD =
813 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
814 BuildMI(BB, PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(), true);
815 } else if (ExternalSymbolSDNode *ESSDN =
816 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
817 BuildMI(BB, PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(), true);
818 } else {
819 Tmp1 = SelectExpr(N.getOperand(1));
820 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
821 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
822 BuildMI(BB, PPC::CALLindirect, 3).addImm(20).addImm(0).addReg(PPC::R12);
823 }
824
825 switch (Node->getValueType(0)) {
826 default: assert(0 && "Unknown value type for call result!");
827 case MVT::Other: return 1;
828 case MVT::i1:
829 case MVT::i8:
830 case MVT::i16:
831 case MVT::i32:
Nate Begemanc7b09f12005-03-25 08:34:25 +0000832 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000833 if (Node->getValueType(1) == MVT::i32)
Nate Begemanc7b09f12005-03-25 08:34:25 +0000834 BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R4).addReg(PPC::R4);
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000835 break;
836 case MVT::f32:
837 case MVT::f64:
838 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
839 break;
840 }
841 return Result+N.ResNo;
842 }
Nate Begemana9795f82005-03-24 04:41:43 +0000843
844 case ISD::SIGN_EXTEND:
845 case ISD::SIGN_EXTEND_INREG:
846 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman9db505c2005-03-28 19:36:43 +0000847 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
848 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
849 case MVT::i16:
850 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
851 break;
852 case MVT::i8:
853 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
854 break;
855 }
Nate Begemana9795f82005-03-24 04:41:43 +0000856 return Result;
857
858 case ISD::ZERO_EXTEND_INREG:
859 Tmp1 = SelectExpr(N.getOperand(0));
860 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
Nate Begeman9db505c2005-03-28 19:36:43 +0000861 default: Node->dump(); assert(0 && "Unhandled ZERO_EXTEND type"); break;
Nate Begemana9795f82005-03-24 04:41:43 +0000862 case MVT::i16: Tmp2 = 16; break;
863 case MVT::i8: Tmp2 = 24; break;
864 case MVT::i1: Tmp2 = 31; break;
865 }
866 BuildMI(BB, PPC::RLWINM, 5, Result).addReg(Tmp1).addImm(0).addImm(0)
867 .addImm(Tmp2).addImm(31);
868 return Result;
869
Nate Begemana9795f82005-03-24 04:41:43 +0000870 case ISD::CopyFromReg:
871 if (Result == 1)
872 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
873 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
874 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
875 return Result;
876
877 case ISD::SHL:
Nate Begeman5e966612005-03-24 06:28:42 +0000878 Tmp1 = SelectExpr(N.getOperand(0));
879 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
880 Tmp2 = CN->getValue() & 0x1F;
881 BuildMI(BB, PPC::RLWINM, 5, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
882 .addImm(31-Tmp2);
883 } else {
884 Tmp2 = SelectExpr(N.getOperand(1));
885 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
886 }
887 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000888
Nate Begeman5e966612005-03-24 06:28:42 +0000889 case ISD::SRL:
890 Tmp1 = SelectExpr(N.getOperand(0));
891 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
892 Tmp2 = CN->getValue() & 0x1F;
893 BuildMI(BB, PPC::RLWINM, 5, Result).addReg(Tmp1).addImm(32-Tmp2)
894 .addImm(Tmp2).addImm(31);
895 } else {
896 Tmp2 = SelectExpr(N.getOperand(1));
897 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
898 }
899 return Result;
900
901 case ISD::SRA:
902 Tmp1 = SelectExpr(N.getOperand(0));
903 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
904 Tmp2 = CN->getValue() & 0x1F;
905 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
906 } else {
907 Tmp2 = SelectExpr(N.getOperand(1));
908 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
909 }
910 return Result;
911
Nate Begemana9795f82005-03-24 04:41:43 +0000912 case ISD::ADD:
913 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
914 Tmp1 = SelectExpr(N.getOperand(0));
915 switch(canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
916 default: assert(0 && "unhandled result code");
917 case 0: // No immediate
918 Tmp2 = SelectExpr(N.getOperand(1));
919 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
920 break;
921 case 1: // Low immediate
922 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
923 break;
924 case 2: // Shifted immediate
925 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
926 break;
927 }
928 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000929
Nate Begemana9795f82005-03-24 04:41:43 +0000930 case ISD::AND:
931 case ISD::OR:
932 case ISD::XOR:
933 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
934 Tmp1 = SelectExpr(N.getOperand(0));
935 switch(canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
936 default: assert(0 && "unhandled result code");
937 case 0: // No immediate
938 Tmp2 = SelectExpr(N.getOperand(1));
939 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +0000940 case ISD::AND: Opc = PPC::AND; break;
941 case ISD::OR: Opc = PPC::OR; break;
942 case ISD::XOR: Opc = PPC::XOR; break;
Nate Begemana9795f82005-03-24 04:41:43 +0000943 }
Nate Begeman5e966612005-03-24 06:28:42 +0000944 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +0000945 break;
946 case 1: // Low immediate
947 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +0000948 case ISD::AND: Opc = PPC::ANDIo; break;
949 case ISD::OR: Opc = PPC::ORI; break;
950 case ISD::XOR: Opc = PPC::XORI; break;
Nate Begemana9795f82005-03-24 04:41:43 +0000951 }
Nate Begeman5e966612005-03-24 06:28:42 +0000952 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +0000953 break;
954 case 2: // Shifted immediate
955 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +0000956 case ISD::AND: Opc = PPC::ANDISo; break;
957 case ISD::OR: Opc = PPC::ORIS; break;
958 case ISD::XOR: Opc = PPC::XORIS; break;
Nate Begemana9795f82005-03-24 04:41:43 +0000959 }
Nate Begeman5e966612005-03-24 06:28:42 +0000960 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +0000961 break;
962 }
963 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000964
965 case ISD::SUB:
966 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
967 Tmp1 = SelectExpr(N.getOperand(0));
968 Tmp2 = SelectExpr(N.getOperand(1));
969 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
970 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000971
Nate Begeman5e966612005-03-24 06:28:42 +0000972 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000973 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
974 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman307e7442005-03-26 01:28:53 +0000975 if (1 == canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
976 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
977 else {
978 Tmp2 = SelectExpr(N.getOperand(1));
979 BuildMI(BB, PPC::MULLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
980 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000981 return Result;
982
Nate Begemanf3d08f32005-03-29 00:03:27 +0000983 case ISD::SDIV:
984 case ISD::UDIV:
985 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
986 Tmp1 = SelectExpr(N.getOperand(0));
987 Tmp2 = SelectExpr(N.getOperand(1));
988 Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW;
989 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
990 return Result;
991
992 case ISD::UREM:
993 case ISD::SREM: {
994 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
995 Tmp1 = SelectExpr(N.getOperand(0));
996 Tmp2 = SelectExpr(N.getOperand(1));
997 Tmp3 = MakeReg(MVT::i32);
998 unsigned Tmp4 = MakeReg(MVT::i32);
999 Opc = (ISD::UREM == opcode) ? PPC::DIVWU : PPC::DIVW;
1000 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1001 BuildMI(BB, PPC::MULLW, 2, Tmp4).addReg(Tmp3).addReg(Tmp2);
1002 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp4).addReg(Tmp1);
1003 return Result;
1004 }
1005
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001006 case ISD::ADD_PARTS:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001007 case ISD::SUB_PARTS: {
1008 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1009 "Not an i64 add/sub!");
1010 // Emit all of the operands.
1011 std::vector<unsigned> InVals;
1012 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
1013 InVals.push_back(SelectExpr(N.getOperand(i)));
1014 if (N.getOpcode() == ISD::ADD_PARTS) {
Nate Begemanf70b5762005-03-28 23:08:54 +00001015 BuildMI(BB, PPC::ADDC, 2, Result+1).addReg(InVals[0]).addReg(InVals[2]);
1016 BuildMI(BB, PPC::ADDE, 2, Result).addReg(InVals[1]).addReg(InVals[3]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001017 } else {
Nate Begemanf70b5762005-03-28 23:08:54 +00001018 BuildMI(BB, PPC::SUBFC, 2, Result+1).addReg(InVals[2]).addReg(InVals[0]);
1019 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(InVals[3]).addReg(InVals[1]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001020 }
1021 return Result+N.ResNo;
1022 }
1023
Nate Begemana9795f82005-03-24 04:41:43 +00001024 case ISD::FP_TO_UINT:
1025 case ISD::FP_TO_SINT:
1026 abort();
1027
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001028 case ISD::SETCC:
1029 abort();
1030
Nate Begemana9795f82005-03-24 04:41:43 +00001031 case ISD::SELECT:
1032 abort();
1033
1034 case ISD::Constant:
1035 switch (N.getValueType()) {
1036 default: assert(0 && "Cannot use constants of this type!");
1037 case MVT::i1:
1038 BuildMI(BB, PPC::LI, 1, Result)
1039 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
1040 break;
1041 case MVT::i32:
1042 {
1043 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
1044 if (v < 32768 && v >= -32768) {
1045 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
1046 } else {
Nate Begeman5e966612005-03-24 06:28:42 +00001047 Tmp1 = MakeReg(MVT::i32);
1048 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
1049 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
Nate Begemana9795f82005-03-24 04:41:43 +00001050 }
1051 }
1052 }
1053 return Result;
1054 }
1055
1056 return 0;
1057}
1058
1059void ISel::Select(SDOperand N) {
1060 unsigned Tmp1, Tmp2, Opc;
1061 unsigned opcode = N.getOpcode();
1062
1063 if (!ExprMap.insert(std::make_pair(N, 1)).second)
1064 return; // Already selected.
1065
1066 SDNode *Node = N.Val;
1067
1068 switch (Node->getOpcode()) {
1069 default:
1070 Node->dump(); std::cerr << "\n";
1071 assert(0 && "Node not handled yet!");
1072 case ISD::EntryToken: return; // Noop
1073 case ISD::TokenFactor:
1074 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1075 Select(Node->getOperand(i));
1076 return;
1077 case ISD::ADJCALLSTACKDOWN:
1078 case ISD::ADJCALLSTACKUP:
1079 Select(N.getOperand(0));
1080 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1081 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? PPC::ADJCALLSTACKDOWN :
1082 PPC::ADJCALLSTACKUP;
1083 BuildMI(BB, Opc, 1).addImm(Tmp1);
1084 return;
1085 case ISD::BR: {
1086 MachineBasicBlock *Dest =
1087 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
Nate Begemana9795f82005-03-24 04:41:43 +00001088 Select(N.getOperand(0));
1089 BuildMI(BB, PPC::B, 1).addMBB(Dest);
1090 return;
1091 }
1092 case ISD::BRCOND:
1093 SelectBranchCC(N);
1094 return;
1095 case ISD::CopyToReg:
1096 Select(N.getOperand(0));
1097 Tmp1 = SelectExpr(N.getOperand(1));
1098 Tmp2 = cast<RegSDNode>(N)->getReg();
1099
1100 if (Tmp1 != Tmp2) {
1101 if (N.getOperand(1).getValueType() == MVT::f64 ||
1102 N.getOperand(1).getValueType() == MVT::f32)
1103 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
1104 else
1105 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1106 }
1107 return;
1108 case ISD::ImplicitDef:
1109 Select(N.getOperand(0));
1110 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
1111 return;
1112 case ISD::RET:
1113 switch (N.getNumOperands()) {
1114 default:
1115 assert(0 && "Unknown return instruction!");
1116 case 3:
1117 assert(N.getOperand(1).getValueType() == MVT::i32 &&
1118 N.getOperand(2).getValueType() == MVT::i32 &&
1119 "Unknown two-register value!");
1120 Select(N.getOperand(0));
1121 Tmp1 = SelectExpr(N.getOperand(1));
1122 Tmp2 = SelectExpr(N.getOperand(2));
1123 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
1124 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp2).addReg(Tmp2);
1125 break;
1126 case 2:
1127 Select(N.getOperand(0));
1128 Tmp1 = SelectExpr(N.getOperand(1));
1129 switch (N.getOperand(1).getValueType()) {
1130 default:
1131 assert(0 && "Unknown return type!");
1132 case MVT::f64:
1133 case MVT::f32:
1134 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
1135 break;
1136 case MVT::i32:
1137 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
1138 break;
1139 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001140 case 1:
1141 Select(N.getOperand(0));
1142 break;
Nate Begemana9795f82005-03-24 04:41:43 +00001143 }
1144 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
1145 return;
Nate Begemana9795f82005-03-24 04:41:43 +00001146 case ISD::TRUNCSTORE:
1147 case ISD::STORE:
1148 {
1149 SDOperand Chain = N.getOperand(0);
1150 SDOperand Value = N.getOperand(1);
1151 SDOperand Address = N.getOperand(2);
1152 Select(Chain);
1153
1154 Tmp1 = SelectExpr(Value); //value
1155
1156 if (opcode == ISD::STORE) {
1157 switch(Value.getValueType()) {
1158 default: assert(0 && "unknown Type in store");
1159 case MVT::i32: Opc = PPC::STW; break;
1160 case MVT::f64: Opc = PPC::STFD; break;
1161 case MVT::f32: Opc = PPC::STFS; break;
1162 }
1163 } else { //ISD::TRUNCSTORE
1164 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1165 default: assert(0 && "unknown Type in store");
1166 case MVT::i1: //FIXME: DAG does not promote this load
1167 case MVT::i8: Opc = PPC::STB; break;
1168 case MVT::i16: Opc = PPC::STH; break;
1169 }
1170 }
1171
1172 if (Address.getOpcode() == ISD::GlobalAddress)
1173 {
1174 BuildMI(BB, Opc, 2).addReg(Tmp1)
1175 .addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1176 }
1177 else if(Address.getOpcode() == ISD::FrameIndex)
1178 {
1179 BuildMI(BB, Opc, 2).addReg(Tmp1)
1180 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex());
1181 }
1182 else
1183 {
1184 int offset;
1185 SelectAddr(Address, Tmp2, offset);
1186 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1187 }
1188 return;
1189 }
1190 case ISD::EXTLOAD:
1191 case ISD::SEXTLOAD:
1192 case ISD::ZEXTLOAD:
1193 case ISD::LOAD:
1194 case ISD::CopyFromReg:
1195 case ISD::CALL:
1196 case ISD::DYNAMIC_STACKALLOC:
1197 ExprMap.erase(N);
1198 SelectExpr(N);
1199 return;
1200 }
1201 assert(0 && "Should not be reached!");
1202}
1203
1204
1205/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
1206/// into a machine code representation using pattern matching and a machine
1207/// description file.
1208///
1209FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) {
1210 return new ISel(TM);
Chris Lattner246fa632005-03-24 06:16:18 +00001211}
1212