blob: 9b667637c1fade208d6ca548b3d53975b47edf71 [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:
598 abort();
599 }
600 assert(0 && "should not get here");
601 return 0;
602}
603
604unsigned ISel::SelectExpr(SDOperand N) {
605 unsigned Result;
606 unsigned Tmp1, Tmp2, Tmp3;
607 unsigned Opc = 0;
608 unsigned opcode = N.getOpcode();
609
610 SDNode *Node = N.Val;
611 MVT::ValueType DestType = N.getValueType();
612
613 unsigned &Reg = ExprMap[N];
614 if (Reg) return Reg;
615
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000616 if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::ADD_PARTS &&
617 N.getOpcode() != ISD::SUB_PARTS)
Nate Begemana9795f82005-03-24 04:41:43 +0000618 Reg = Result = (N.getValueType() != MVT::Other) ?
619 MakeReg(N.getValueType()) : 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000620 else {
621 // If this is a call instruction, make sure to prepare ALL of the result
622 // values as well as the chain.
623 if (N.getOpcode() == ISD::CALL) {
624 if (Node->getNumValues() == 1)
625 Reg = Result = 1; // Void call, just a chain.
626 else {
627 Result = MakeReg(Node->getValueType(0));
628 ExprMap[N.getValue(0)] = Result;
629 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
630 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
631 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
632 }
633 } else {
634 Result = MakeReg(Node->getValueType(0));
635 ExprMap[N.getValue(0)] = Result;
636 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
637 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
638 }
639 }
640
641 if (DestType == MVT::f64 || DestType == MVT::f32)
642 return SelectExprFP(N, Result);
Nate Begemana9795f82005-03-24 04:41:43 +0000643
644 switch (opcode) {
645 default:
646 Node->dump();
647 assert(0 && "Node not handled!\n");
648
649 case ISD::DYNAMIC_STACKALLOC:
Nate Begeman5e966612005-03-24 06:28:42 +0000650 // Generate both result values. FIXME: Need a better commment here?
651 if (Result != 1)
652 ExprMap[N.getValue(1)] = 1;
653 else
654 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
655
656 // FIXME: We are currently ignoring the requested alignment for handling
657 // greater than the stack alignment. This will need to be revisited at some
658 // point. Align = N.getOperand(2);
659 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
660 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
661 std::cerr << "Cannot allocate stack object with greater alignment than"
662 << " the stack alignment yet!";
663 abort();
664 }
665 Select(N.getOperand(0));
666 Tmp1 = SelectExpr(N.getOperand(1));
667 // Subtract size from stack pointer, thereby allocating some space.
668 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
669 // Put a pointer to the space into the result register by copying the SP
670 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
671 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000672
673 case ISD::ConstantPool:
Nate Begemanca12a2b2005-03-28 22:28:37 +0000674 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
675 Tmp2 = MakeReg(MVT::i32);
676 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp2).addReg(getGlobalBaseReg())
677 .addConstantPoolIndex(Tmp1);
678 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
679 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000680
681 case ISD::FrameIndex:
Nate Begemanca12a2b2005-03-28 22:28:37 +0000682 assert(0 && "ISD::FrameIndex Unimplemented");
Nate Begemana9795f82005-03-24 04:41:43 +0000683 abort();
684
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000685 case ISD::GlobalAddress: {
686 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanca12a2b2005-03-28 22:28:37 +0000687 Tmp1 = MakeReg(MVT::i32);
Nate Begemanc7b09f12005-03-25 08:34:25 +0000688 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
689 .addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000690 if (GV->hasWeakLinkage() || GV->isExternal()) {
691 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
692 } else {
693 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
694 }
695 return Result;
696 }
697
Nate Begeman5e966612005-03-24 06:28:42 +0000698 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +0000699 case ISD::EXTLOAD:
700 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000701 case ISD::SEXTLOAD: {
Nate Begeman9db505c2005-03-28 19:36:43 +0000702 bool sext = (ISD::SEXTLOAD == opcode);
703 bool byte = (MVT::i8 == Node->getValueType(0));
704 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
705 Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType();
706
Nate Begeman5e966612005-03-24 06:28:42 +0000707 // Make sure we generate both values.
708 if (Result != 1)
709 ExprMap[N.getValue(1)] = 1; // Generate the token
710 else
711 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
712
713 SDOperand Chain = N.getOperand(0);
714 SDOperand Address = N.getOperand(1);
715 Select(Chain);
716
Nate Begeman9db505c2005-03-28 19:36:43 +0000717 switch (TypeBeingLoaded) {
Nate Begeman5e966612005-03-24 06:28:42 +0000718 default: assert(0 && "Cannot load this type!");
Nate Begeman9db505c2005-03-28 19:36:43 +0000719 case MVT::i1: Opc = PPC::LBZ; break;
720 case MVT::i8: Opc = PPC::LBZ; break;
721 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
722 case MVT::i32: Opc = PPC::LWZ; break;
Nate Begeman5e966612005-03-24 06:28:42 +0000723 }
724
Nate Begeman9db505c2005-03-28 19:36:43 +0000725 // Since there's no load byte & sign extend instruction we have to split
726 // byte SEXTLOADs into lbz + extsb. This requires we make a temp register.
727 if (sext && byte) {
728 Tmp3 = Result;
729 Result = MakeReg(MVT::i32);
730 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000731 if(Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman5e966612005-03-24 06:28:42 +0000732 BuildMI(BB, Opc, 2, Result)
733 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
734 .addReg(PPC::R1);
735 } else {
736 int offset;
737 SelectAddr(Address, Tmp1, offset);
738 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
739 }
Nate Begeman9db505c2005-03-28 19:36:43 +0000740 if (sext && byte) {
741 BuildMI(BB, PPC::EXTSB, 1, Tmp3).addReg(Result);
742 Result = Tmp3;
743 }
Nate Begeman5e966612005-03-24 06:28:42 +0000744 return Result;
745 }
746
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000747 case ISD::CALL: {
748 // Lower the chain for this call.
749 Select(N.getOperand(0));
750 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
751
752 // get the virtual reg for each argument
753 std::vector<unsigned> VRegs;
754 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
755 VRegs.push_back(SelectExpr(N.getOperand(i)));
756
757 // The ABI specifies that the first 32 bytes of args may be passed in GPRs,
758 // and that 13 FPRs may also be used for passing any floating point args.
759 int GPR_remaining = 8, FPR_remaining = 13;
760 unsigned GPR_idx = 0, FPR_idx = 0;
761 static const unsigned GPR[] = {
762 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
763 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
764 };
765 static const unsigned FPR[] = {
766 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6,
767 PPC::F7, PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12,
768 PPC::F13
769 };
770
771 // move the vregs into the appropriate architected register or stack slot
772 for(int i = 0, e = VRegs.size(); i < e; ++i) {
773 unsigned OperandType = N.getOperand(i+2).getValueType();
774 switch(OperandType) {
775 default:
776 Node->dump();
777 N.getOperand(i).Val->dump();
778 std::cerr << "Type for " << i << " is: " <<
779 N.getOperand(i+2).getValueType() << "\n";
780 assert(0 && "Unknown value type for call");
781 case MVT::i1:
782 case MVT::i8:
783 case MVT::i16:
784 case MVT::i32:
785 if (GPR_remaining > 0)
786 BuildMI(BB, PPC::OR, 2, GPR[GPR_idx]).addReg(VRegs[i])
787 .addReg(VRegs[i]);
788 break;
789 case MVT::f32:
790 case MVT::f64:
791 if (FPR_remaining > 0) {
792 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(VRegs[i]);
Nate Begemanf2622612005-03-26 02:17:46 +0000793 ++FPR_idx;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000794 --FPR_remaining;
795 }
796 break;
797 }
798 // All arguments consume GPRs available for argument passing
Nate Begemanf2622612005-03-26 02:17:46 +0000799 if (GPR_remaining > 0) {
800 ++GPR_idx;
801 --GPR_remaining;
802 }
803 if (MVT::f64 == OperandType && GPR_remaining > 0) {
804 ++GPR_idx;
805 --GPR_remaining;
806 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000807 }
808
809 // Emit the correct call instruction based on the type of symbol called.
810 if (GlobalAddressSDNode *GASD =
811 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
812 BuildMI(BB, PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(), true);
813 } else if (ExternalSymbolSDNode *ESSDN =
814 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
815 BuildMI(BB, PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(), true);
816 } else {
817 Tmp1 = SelectExpr(N.getOperand(1));
818 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
819 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
820 BuildMI(BB, PPC::CALLindirect, 3).addImm(20).addImm(0).addReg(PPC::R12);
821 }
822
823 switch (Node->getValueType(0)) {
824 default: assert(0 && "Unknown value type for call result!");
825 case MVT::Other: return 1;
826 case MVT::i1:
827 case MVT::i8:
828 case MVT::i16:
829 case MVT::i32:
Nate Begemanc7b09f12005-03-25 08:34:25 +0000830 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000831 if (Node->getValueType(1) == MVT::i32)
Nate Begemanc7b09f12005-03-25 08:34:25 +0000832 BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R4).addReg(PPC::R4);
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000833 break;
834 case MVT::f32:
835 case MVT::f64:
836 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
837 break;
838 }
839 return Result+N.ResNo;
840 }
Nate Begemana9795f82005-03-24 04:41:43 +0000841
842 case ISD::SIGN_EXTEND:
843 case ISD::SIGN_EXTEND_INREG:
844 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman9db505c2005-03-28 19:36:43 +0000845 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
846 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
847 case MVT::i16:
848 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
849 break;
850 case MVT::i8:
851 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
852 break;
853 }
Nate Begemana9795f82005-03-24 04:41:43 +0000854 return Result;
855
856 case ISD::ZERO_EXTEND_INREG:
857 Tmp1 = SelectExpr(N.getOperand(0));
858 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
Nate Begeman9db505c2005-03-28 19:36:43 +0000859 default: Node->dump(); assert(0 && "Unhandled ZERO_EXTEND type"); break;
Nate Begemana9795f82005-03-24 04:41:43 +0000860 case MVT::i16: Tmp2 = 16; break;
861 case MVT::i8: Tmp2 = 24; break;
862 case MVT::i1: Tmp2 = 31; break;
863 }
864 BuildMI(BB, PPC::RLWINM, 5, Result).addReg(Tmp1).addImm(0).addImm(0)
865 .addImm(Tmp2).addImm(31);
866 return Result;
867
Nate Begemana9795f82005-03-24 04:41:43 +0000868 case ISD::CopyFromReg:
869 if (Result == 1)
870 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
871 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
872 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
873 return Result;
874
875 case ISD::SHL:
Nate Begeman5e966612005-03-24 06:28:42 +0000876 Tmp1 = SelectExpr(N.getOperand(0));
877 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
878 Tmp2 = CN->getValue() & 0x1F;
879 BuildMI(BB, PPC::RLWINM, 5, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
880 .addImm(31-Tmp2);
881 } else {
882 Tmp2 = SelectExpr(N.getOperand(1));
883 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
884 }
885 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000886
Nate Begeman5e966612005-03-24 06:28:42 +0000887 case ISD::SRL:
888 Tmp1 = SelectExpr(N.getOperand(0));
889 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
890 Tmp2 = CN->getValue() & 0x1F;
891 BuildMI(BB, PPC::RLWINM, 5, Result).addReg(Tmp1).addImm(32-Tmp2)
892 .addImm(Tmp2).addImm(31);
893 } else {
894 Tmp2 = SelectExpr(N.getOperand(1));
895 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
896 }
897 return Result;
898
899 case ISD::SRA:
900 Tmp1 = SelectExpr(N.getOperand(0));
901 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
902 Tmp2 = CN->getValue() & 0x1F;
903 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
904 } else {
905 Tmp2 = SelectExpr(N.getOperand(1));
906 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
907 }
908 return Result;
909
Nate Begemana9795f82005-03-24 04:41:43 +0000910 case ISD::ADD:
911 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
912 Tmp1 = SelectExpr(N.getOperand(0));
913 switch(canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
914 default: assert(0 && "unhandled result code");
915 case 0: // No immediate
916 Tmp2 = SelectExpr(N.getOperand(1));
917 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
918 break;
919 case 1: // Low immediate
920 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
921 break;
922 case 2: // Shifted immediate
923 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
924 break;
925 }
926 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000927
Nate Begemana9795f82005-03-24 04:41:43 +0000928 case ISD::AND:
929 case ISD::OR:
930 case ISD::XOR:
931 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
932 Tmp1 = SelectExpr(N.getOperand(0));
933 switch(canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
934 default: assert(0 && "unhandled result code");
935 case 0: // No immediate
936 Tmp2 = SelectExpr(N.getOperand(1));
937 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +0000938 case ISD::AND: Opc = PPC::AND; break;
939 case ISD::OR: Opc = PPC::OR; break;
940 case ISD::XOR: Opc = PPC::XOR; break;
Nate Begemana9795f82005-03-24 04:41:43 +0000941 }
Nate Begeman5e966612005-03-24 06:28:42 +0000942 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +0000943 break;
944 case 1: // Low immediate
945 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +0000946 case ISD::AND: Opc = PPC::ANDIo; break;
947 case ISD::OR: Opc = PPC::ORI; break;
948 case ISD::XOR: Opc = PPC::XORI; break;
Nate Begemana9795f82005-03-24 04:41:43 +0000949 }
Nate Begeman5e966612005-03-24 06:28:42 +0000950 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +0000951 break;
952 case 2: // Shifted immediate
953 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +0000954 case ISD::AND: Opc = PPC::ANDISo; break;
955 case ISD::OR: Opc = PPC::ORIS; break;
956 case ISD::XOR: Opc = PPC::XORIS; break;
Nate Begemana9795f82005-03-24 04:41:43 +0000957 }
Nate Begeman5e966612005-03-24 06:28:42 +0000958 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +0000959 break;
960 }
961 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000962
963 case ISD::SUB:
964 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
965 Tmp1 = SelectExpr(N.getOperand(0));
966 Tmp2 = SelectExpr(N.getOperand(1));
967 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
968 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000969
Nate Begeman5e966612005-03-24 06:28:42 +0000970 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000971 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
972 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman307e7442005-03-26 01:28:53 +0000973 if (1 == canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
974 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
975 else {
976 Tmp2 = SelectExpr(N.getOperand(1));
977 BuildMI(BB, PPC::MULLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
978 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000979 return Result;
980
981 case ISD::ADD_PARTS:
Nate Begemanca12a2b2005-03-28 22:28:37 +0000982 case ISD::SUB_PARTS: {
983 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
984 "Not an i64 add/sub!");
985 // Emit all of the operands.
986 std::vector<unsigned> InVals;
987 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
988 InVals.push_back(SelectExpr(N.getOperand(i)));
989 if (N.getOpcode() == ISD::ADD_PARTS) {
Nate Begemanf70b5762005-03-28 23:08:54 +0000990 BuildMI(BB, PPC::ADDC, 2, Result+1).addReg(InVals[0]).addReg(InVals[2]);
991 BuildMI(BB, PPC::ADDE, 2, Result).addReg(InVals[1]).addReg(InVals[3]);
Nate Begemanca12a2b2005-03-28 22:28:37 +0000992 } else {
Nate Begemanf70b5762005-03-28 23:08:54 +0000993 BuildMI(BB, PPC::SUBFC, 2, Result+1).addReg(InVals[2]).addReg(InVals[0]);
994 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(InVals[3]).addReg(InVals[1]);
Nate Begemanca12a2b2005-03-28 22:28:37 +0000995 }
996 return Result+N.ResNo;
997 }
998
Nate Begemana9795f82005-03-24 04:41:43 +0000999 case ISD::UREM:
1000 case ISD::SREM:
1001 case ISD::SDIV:
1002 case ISD::UDIV:
1003 abort();
1004
1005 case ISD::FP_TO_UINT:
1006 case ISD::FP_TO_SINT:
1007 abort();
1008
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001009 case ISD::SETCC:
1010 abort();
1011
Nate Begemana9795f82005-03-24 04:41:43 +00001012 case ISD::SELECT:
1013 abort();
1014
1015 case ISD::Constant:
1016 switch (N.getValueType()) {
1017 default: assert(0 && "Cannot use constants of this type!");
1018 case MVT::i1:
1019 BuildMI(BB, PPC::LI, 1, Result)
1020 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
1021 break;
1022 case MVT::i32:
1023 {
1024 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
1025 if (v < 32768 && v >= -32768) {
1026 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
1027 } else {
Nate Begeman5e966612005-03-24 06:28:42 +00001028 Tmp1 = MakeReg(MVT::i32);
1029 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
1030 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
Nate Begemana9795f82005-03-24 04:41:43 +00001031 }
1032 }
1033 }
1034 return Result;
1035 }
1036
1037 return 0;
1038}
1039
1040void ISel::Select(SDOperand N) {
1041 unsigned Tmp1, Tmp2, Opc;
1042 unsigned opcode = N.getOpcode();
1043
1044 if (!ExprMap.insert(std::make_pair(N, 1)).second)
1045 return; // Already selected.
1046
1047 SDNode *Node = N.Val;
1048
1049 switch (Node->getOpcode()) {
1050 default:
1051 Node->dump(); std::cerr << "\n";
1052 assert(0 && "Node not handled yet!");
1053 case ISD::EntryToken: return; // Noop
1054 case ISD::TokenFactor:
1055 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1056 Select(Node->getOperand(i));
1057 return;
1058 case ISD::ADJCALLSTACKDOWN:
1059 case ISD::ADJCALLSTACKUP:
1060 Select(N.getOperand(0));
1061 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1062 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? PPC::ADJCALLSTACKDOWN :
1063 PPC::ADJCALLSTACKUP;
1064 BuildMI(BB, Opc, 1).addImm(Tmp1);
1065 return;
1066 case ISD::BR: {
1067 MachineBasicBlock *Dest =
1068 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
Nate Begemana9795f82005-03-24 04:41:43 +00001069 Select(N.getOperand(0));
1070 BuildMI(BB, PPC::B, 1).addMBB(Dest);
1071 return;
1072 }
1073 case ISD::BRCOND:
1074 SelectBranchCC(N);
1075 return;
1076 case ISD::CopyToReg:
1077 Select(N.getOperand(0));
1078 Tmp1 = SelectExpr(N.getOperand(1));
1079 Tmp2 = cast<RegSDNode>(N)->getReg();
1080
1081 if (Tmp1 != Tmp2) {
1082 if (N.getOperand(1).getValueType() == MVT::f64 ||
1083 N.getOperand(1).getValueType() == MVT::f32)
1084 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
1085 else
1086 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1087 }
1088 return;
1089 case ISD::ImplicitDef:
1090 Select(N.getOperand(0));
1091 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
1092 return;
1093 case ISD::RET:
1094 switch (N.getNumOperands()) {
1095 default:
1096 assert(0 && "Unknown return instruction!");
1097 case 3:
1098 assert(N.getOperand(1).getValueType() == MVT::i32 &&
1099 N.getOperand(2).getValueType() == MVT::i32 &&
1100 "Unknown two-register value!");
1101 Select(N.getOperand(0));
1102 Tmp1 = SelectExpr(N.getOperand(1));
1103 Tmp2 = SelectExpr(N.getOperand(2));
1104 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
1105 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp2).addReg(Tmp2);
1106 break;
1107 case 2:
1108 Select(N.getOperand(0));
1109 Tmp1 = SelectExpr(N.getOperand(1));
1110 switch (N.getOperand(1).getValueType()) {
1111 default:
1112 assert(0 && "Unknown return type!");
1113 case MVT::f64:
1114 case MVT::f32:
1115 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
1116 break;
1117 case MVT::i32:
1118 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
1119 break;
1120 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001121 case 1:
1122 Select(N.getOperand(0));
1123 break;
Nate Begemana9795f82005-03-24 04:41:43 +00001124 }
1125 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
1126 return;
Nate Begemana9795f82005-03-24 04:41:43 +00001127 case ISD::TRUNCSTORE:
1128 case ISD::STORE:
1129 {
1130 SDOperand Chain = N.getOperand(0);
1131 SDOperand Value = N.getOperand(1);
1132 SDOperand Address = N.getOperand(2);
1133 Select(Chain);
1134
1135 Tmp1 = SelectExpr(Value); //value
1136
1137 if (opcode == ISD::STORE) {
1138 switch(Value.getValueType()) {
1139 default: assert(0 && "unknown Type in store");
1140 case MVT::i32: Opc = PPC::STW; break;
1141 case MVT::f64: Opc = PPC::STFD; break;
1142 case MVT::f32: Opc = PPC::STFS; break;
1143 }
1144 } else { //ISD::TRUNCSTORE
1145 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1146 default: assert(0 && "unknown Type in store");
1147 case MVT::i1: //FIXME: DAG does not promote this load
1148 case MVT::i8: Opc = PPC::STB; break;
1149 case MVT::i16: Opc = PPC::STH; break;
1150 }
1151 }
1152
1153 if (Address.getOpcode() == ISD::GlobalAddress)
1154 {
1155 BuildMI(BB, Opc, 2).addReg(Tmp1)
1156 .addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1157 }
1158 else if(Address.getOpcode() == ISD::FrameIndex)
1159 {
1160 BuildMI(BB, Opc, 2).addReg(Tmp1)
1161 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex());
1162 }
1163 else
1164 {
1165 int offset;
1166 SelectAddr(Address, Tmp2, offset);
1167 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1168 }
1169 return;
1170 }
1171 case ISD::EXTLOAD:
1172 case ISD::SEXTLOAD:
1173 case ISD::ZEXTLOAD:
1174 case ISD::LOAD:
1175 case ISD::CopyFromReg:
1176 case ISD::CALL:
1177 case ISD::DYNAMIC_STACKALLOC:
1178 ExprMap.erase(N);
1179 SelectExpr(N);
1180 return;
1181 }
1182 assert(0 && "Should not be reached!");
1183}
1184
1185
1186/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
1187/// into a machine code representation using pattern matching and a machine
1188/// description file.
1189///
1190FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) {
1191 return new ISel(TM);
Chris Lattner246fa632005-03-24 06:16:18 +00001192}
1193