blob: c778021749b035575d6a2634cce198f30a2ff918 [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 };
Nate Begemana9795f82005-03-24 04:41:43 +0000590 Tmp1 = SelectExpr(N.getOperand(0));
591 Tmp2 = SelectExpr(N.getOperand(1));
592 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
593 return Result;
594
Nate Begemana9795f82005-03-24 04:41:43 +0000595 case ISD::UINT_TO_FP:
596 case ISD::SINT_TO_FP:
Nate Begemanf3d08f32005-03-29 00:03:27 +0000597 assert(0 && "ISD::U/SINT_TO_FP Unimplemented");
Nate Begemana9795f82005-03-24 04:41:43 +0000598 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 Begemanf3d08f32005-03-29 00:03:27 +0000682 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
683 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1);
684 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000685
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000686 case ISD::GlobalAddress: {
687 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanca12a2b2005-03-28 22:28:37 +0000688 Tmp1 = MakeReg(MVT::i32);
Nate Begemanc7b09f12005-03-25 08:34:25 +0000689 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
690 .addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000691 if (GV->hasWeakLinkage() || GV->isExternal()) {
692 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
693 } else {
694 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
695 }
696 return Result;
697 }
698
Nate Begeman5e966612005-03-24 06:28:42 +0000699 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +0000700 case ISD::EXTLOAD:
701 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000702 case ISD::SEXTLOAD: {
Nate Begeman9db505c2005-03-28 19:36:43 +0000703 bool sext = (ISD::SEXTLOAD == opcode);
704 bool byte = (MVT::i8 == Node->getValueType(0));
705 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
706 Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType();
707
Nate Begeman5e966612005-03-24 06:28:42 +0000708 // Make sure we generate both values.
709 if (Result != 1)
710 ExprMap[N.getValue(1)] = 1; // Generate the token
711 else
712 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
713
714 SDOperand Chain = N.getOperand(0);
715 SDOperand Address = N.getOperand(1);
716 Select(Chain);
717
Nate Begeman9db505c2005-03-28 19:36:43 +0000718 switch (TypeBeingLoaded) {
Nate Begeman5e966612005-03-24 06:28:42 +0000719 default: assert(0 && "Cannot load this type!");
Nate Begeman9db505c2005-03-28 19:36:43 +0000720 case MVT::i1: Opc = PPC::LBZ; break;
721 case MVT::i8: Opc = PPC::LBZ; break;
722 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
723 case MVT::i32: Opc = PPC::LWZ; break;
Nate Begeman5e966612005-03-24 06:28:42 +0000724 }
725
Nate Begeman9db505c2005-03-28 19:36:43 +0000726 // Since there's no load byte & sign extend instruction we have to split
727 // byte SEXTLOADs into lbz + extsb. This requires we make a temp register.
728 if (sext && byte) {
729 Tmp3 = Result;
730 Result = MakeReg(MVT::i32);
Chris Lattner848132d2005-03-29 15:13:27 +0000731 } else {
732 Tmp3 = 0; // Silence GCC warning.
Nate Begeman9db505c2005-03-28 19:36:43 +0000733 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000734 if(Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman5e966612005-03-24 06:28:42 +0000735 BuildMI(BB, Opc, 2, Result)
736 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
737 .addReg(PPC::R1);
738 } else {
739 int offset;
740 SelectAddr(Address, Tmp1, offset);
741 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
742 }
Nate Begeman9db505c2005-03-28 19:36:43 +0000743 if (sext && byte) {
744 BuildMI(BB, PPC::EXTSB, 1, Tmp3).addReg(Result);
745 Result = Tmp3;
746 }
Nate Begeman5e966612005-03-24 06:28:42 +0000747 return Result;
748 }
749
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000750 case ISD::CALL: {
751 // Lower the chain for this call.
752 Select(N.getOperand(0));
753 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
754
755 // get the virtual reg for each argument
756 std::vector<unsigned> VRegs;
757 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
758 VRegs.push_back(SelectExpr(N.getOperand(i)));
759
760 // The ABI specifies that the first 32 bytes of args may be passed in GPRs,
761 // and that 13 FPRs may also be used for passing any floating point args.
762 int GPR_remaining = 8, FPR_remaining = 13;
763 unsigned GPR_idx = 0, FPR_idx = 0;
764 static const unsigned GPR[] = {
765 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
766 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
767 };
768 static const unsigned FPR[] = {
769 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6,
770 PPC::F7, PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12,
771 PPC::F13
772 };
773
774 // move the vregs into the appropriate architected register or stack slot
775 for(int i = 0, e = VRegs.size(); i < e; ++i) {
776 unsigned OperandType = N.getOperand(i+2).getValueType();
777 switch(OperandType) {
778 default:
779 Node->dump();
780 N.getOperand(i).Val->dump();
781 std::cerr << "Type for " << i << " is: " <<
782 N.getOperand(i+2).getValueType() << "\n";
783 assert(0 && "Unknown value type for call");
784 case MVT::i1:
785 case MVT::i8:
786 case MVT::i16:
787 case MVT::i32:
788 if (GPR_remaining > 0)
789 BuildMI(BB, PPC::OR, 2, GPR[GPR_idx]).addReg(VRegs[i])
790 .addReg(VRegs[i]);
791 break;
792 case MVT::f32:
793 case MVT::f64:
794 if (FPR_remaining > 0) {
795 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(VRegs[i]);
Nate Begemanf2622612005-03-26 02:17:46 +0000796 ++FPR_idx;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000797 --FPR_remaining;
798 }
799 break;
800 }
801 // All arguments consume GPRs available for argument passing
Nate Begemanf2622612005-03-26 02:17:46 +0000802 if (GPR_remaining > 0) {
803 ++GPR_idx;
804 --GPR_remaining;
805 }
806 if (MVT::f64 == OperandType && GPR_remaining > 0) {
807 ++GPR_idx;
808 --GPR_remaining;
809 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000810 }
811
812 // Emit the correct call instruction based on the type of symbol called.
813 if (GlobalAddressSDNode *GASD =
814 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
815 BuildMI(BB, PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(), true);
816 } else if (ExternalSymbolSDNode *ESSDN =
817 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
818 BuildMI(BB, PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(), true);
819 } else {
820 Tmp1 = SelectExpr(N.getOperand(1));
821 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
822 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
823 BuildMI(BB, PPC::CALLindirect, 3).addImm(20).addImm(0).addReg(PPC::R12);
824 }
825
826 switch (Node->getValueType(0)) {
827 default: assert(0 && "Unknown value type for call result!");
828 case MVT::Other: return 1;
829 case MVT::i1:
830 case MVT::i8:
831 case MVT::i16:
832 case MVT::i32:
Nate Begemanc7b09f12005-03-25 08:34:25 +0000833 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000834 if (Node->getValueType(1) == MVT::i32)
Nate Begemanc7b09f12005-03-25 08:34:25 +0000835 BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R4).addReg(PPC::R4);
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000836 break;
837 case MVT::f32:
838 case MVT::f64:
839 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
840 break;
841 }
842 return Result+N.ResNo;
843 }
Nate Begemana9795f82005-03-24 04:41:43 +0000844
845 case ISD::SIGN_EXTEND:
846 case ISD::SIGN_EXTEND_INREG:
847 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman9db505c2005-03-28 19:36:43 +0000848 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
849 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
850 case MVT::i16:
851 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
852 break;
853 case MVT::i8:
854 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
855 break;
Nate Begeman74747862005-03-29 22:24:51 +0000856 case MVT::i1:
857 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
858 break;
Nate Begeman9db505c2005-03-28 19:36:43 +0000859 }
Nate Begemana9795f82005-03-24 04:41:43 +0000860 return Result;
861
862 case ISD::ZERO_EXTEND_INREG:
863 Tmp1 = SelectExpr(N.getOperand(0));
864 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
Nate Begeman9db505c2005-03-28 19:36:43 +0000865 default: Node->dump(); assert(0 && "Unhandled ZERO_EXTEND type"); break;
Nate Begemana9795f82005-03-24 04:41:43 +0000866 case MVT::i16: Tmp2 = 16; break;
867 case MVT::i8: Tmp2 = 24; break;
868 case MVT::i1: Tmp2 = 31; break;
869 }
Nate Begeman33162522005-03-29 21:54:38 +0000870 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(0).addImm(Tmp2)
871 .addImm(31);
Nate Begemana9795f82005-03-24 04:41:43 +0000872 return Result;
873
Nate Begemana9795f82005-03-24 04:41:43 +0000874 case ISD::CopyFromReg:
875 if (Result == 1)
876 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
877 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
878 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
879 return Result;
880
881 case ISD::SHL:
Nate Begeman5e966612005-03-24 06:28:42 +0000882 Tmp1 = SelectExpr(N.getOperand(0));
883 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
884 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +0000885 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
Nate Begeman5e966612005-03-24 06:28:42 +0000886 .addImm(31-Tmp2);
887 } else {
888 Tmp2 = SelectExpr(N.getOperand(1));
889 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
890 }
891 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000892
Nate Begeman5e966612005-03-24 06:28:42 +0000893 case ISD::SRL:
894 Tmp1 = SelectExpr(N.getOperand(0));
895 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
896 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +0000897 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2)
Nate Begeman5e966612005-03-24 06:28:42 +0000898 .addImm(Tmp2).addImm(31);
899 } else {
900 Tmp2 = SelectExpr(N.getOperand(1));
901 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
902 }
903 return Result;
904
905 case ISD::SRA:
906 Tmp1 = SelectExpr(N.getOperand(0));
907 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
908 Tmp2 = CN->getValue() & 0x1F;
909 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
910 } else {
911 Tmp2 = SelectExpr(N.getOperand(1));
912 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
913 }
914 return Result;
915
Nate Begemana9795f82005-03-24 04:41:43 +0000916 case ISD::ADD:
917 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
918 Tmp1 = SelectExpr(N.getOperand(0));
919 switch(canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
920 default: assert(0 && "unhandled result code");
921 case 0: // No immediate
922 Tmp2 = SelectExpr(N.getOperand(1));
923 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
924 break;
925 case 1: // Low immediate
926 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
927 break;
928 case 2: // Shifted immediate
929 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
930 break;
931 }
932 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000933
Nate Begemana9795f82005-03-24 04:41:43 +0000934 case ISD::AND:
935 case ISD::OR:
936 case ISD::XOR:
937 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
938 Tmp1 = SelectExpr(N.getOperand(0));
939 switch(canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
940 default: assert(0 && "unhandled result code");
941 case 0: // No immediate
942 Tmp2 = SelectExpr(N.getOperand(1));
943 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +0000944 case ISD::AND: Opc = PPC::AND; break;
945 case ISD::OR: Opc = PPC::OR; break;
946 case ISD::XOR: Opc = PPC::XOR; break;
Nate Begemana9795f82005-03-24 04:41:43 +0000947 }
Nate Begeman5e966612005-03-24 06:28:42 +0000948 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +0000949 break;
950 case 1: // Low immediate
951 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +0000952 case ISD::AND: Opc = PPC::ANDIo; break;
953 case ISD::OR: Opc = PPC::ORI; break;
954 case ISD::XOR: Opc = PPC::XORI; break;
Nate Begemana9795f82005-03-24 04:41:43 +0000955 }
Nate Begeman5e966612005-03-24 06:28:42 +0000956 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +0000957 break;
958 case 2: // Shifted immediate
959 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +0000960 case ISD::AND: Opc = PPC::ANDISo; break;
961 case ISD::OR: Opc = PPC::ORIS; break;
962 case ISD::XOR: Opc = PPC::XORIS; break;
Nate Begemana9795f82005-03-24 04:41:43 +0000963 }
Nate Begeman5e966612005-03-24 06:28:42 +0000964 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +0000965 break;
966 }
967 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000968
969 case ISD::SUB:
970 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
971 Tmp1 = SelectExpr(N.getOperand(0));
972 Tmp2 = SelectExpr(N.getOperand(1));
973 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
974 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000975
Nate Begeman5e966612005-03-24 06:28:42 +0000976 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000977 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
978 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman307e7442005-03-26 01:28:53 +0000979 if (1 == canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
980 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
981 else {
982 Tmp2 = SelectExpr(N.getOperand(1));
983 BuildMI(BB, PPC::MULLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
984 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000985 return Result;
986
Nate Begemanf3d08f32005-03-29 00:03:27 +0000987 case ISD::SDIV:
988 case ISD::UDIV:
989 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
990 Tmp1 = SelectExpr(N.getOperand(0));
991 Tmp2 = SelectExpr(N.getOperand(1));
992 Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW;
993 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
994 return Result;
995
996 case ISD::UREM:
997 case ISD::SREM: {
998 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
999 Tmp1 = SelectExpr(N.getOperand(0));
1000 Tmp2 = SelectExpr(N.getOperand(1));
1001 Tmp3 = MakeReg(MVT::i32);
1002 unsigned Tmp4 = MakeReg(MVT::i32);
1003 Opc = (ISD::UREM == opcode) ? PPC::DIVWU : PPC::DIVW;
1004 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1005 BuildMI(BB, PPC::MULLW, 2, Tmp4).addReg(Tmp3).addReg(Tmp2);
1006 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp4).addReg(Tmp1);
1007 return Result;
1008 }
1009
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001010 case ISD::ADD_PARTS:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001011 case ISD::SUB_PARTS: {
1012 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1013 "Not an i64 add/sub!");
1014 // Emit all of the operands.
1015 std::vector<unsigned> InVals;
1016 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
1017 InVals.push_back(SelectExpr(N.getOperand(i)));
1018 if (N.getOpcode() == ISD::ADD_PARTS) {
Nate Begemanf70b5762005-03-28 23:08:54 +00001019 BuildMI(BB, PPC::ADDC, 2, Result+1).addReg(InVals[0]).addReg(InVals[2]);
1020 BuildMI(BB, PPC::ADDE, 2, Result).addReg(InVals[1]).addReg(InVals[3]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001021 } else {
Nate Begemanf70b5762005-03-28 23:08:54 +00001022 BuildMI(BB, PPC::SUBFC, 2, Result+1).addReg(InVals[2]).addReg(InVals[0]);
1023 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(InVals[3]).addReg(InVals[1]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001024 }
1025 return Result+N.ResNo;
1026 }
1027
Nate Begemana9795f82005-03-24 04:41:43 +00001028 case ISD::FP_TO_UINT:
1029 case ISD::FP_TO_SINT:
1030 abort();
1031
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001032 case ISD::SETCC:
Nate Begeman33162522005-03-29 21:54:38 +00001033 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1034 bool U = false;
1035 bool IsInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
1036
1037 switch (SetCC->getCondition()) {
1038 default: Node->dump(); assert(0 && "Unknown comparison!");
1039 case ISD::SETEQ: Opc = PPC::BEQ; break;
1040 case ISD::SETNE: Opc = PPC::BNE; break;
1041 case ISD::SETULT: U = true;
1042 case ISD::SETLT: Opc = PPC::BLT; break;
1043 case ISD::SETULE: U = true;
1044 case ISD::SETLE: Opc = PPC::BLE; break;
1045 case ISD::SETUGT: U = true;
1046 case ISD::SETGT: Opc = PPC::BGT; break;
1047 case ISD::SETUGE: U = true;
1048 case ISD::SETGE: Opc = PPC::BGE; break;
1049 }
1050
1051 // FIXME: Is there a situation in which we would ever need to emit fcmpo?
1052 static const unsigned CompareOpcodes[] =
1053 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
1054 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
1055
1056 // Create an iterator with which to insert the MBB for copying the false
1057 // value and the MBB to hold the PHI instruction for this SetCC.
1058 MachineBasicBlock *thisMBB = BB;
1059 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1060 ilist<MachineBasicBlock>::iterator It = BB;
1061 ++It;
1062
1063 // thisMBB:
1064 // ...
1065 // cmpTY cr0, r1, r2
1066 // %TrueValue = li 1
1067 // bCC sinkMBB
1068 Tmp1 = SelectExpr(N.getOperand(0));
1069 Tmp2 = SelectExpr(N.getOperand(1));
1070 BuildMI(BB, CompareOpc, 2, PPC::CR0).addReg(Tmp1).addReg(Tmp2);
1071 unsigned TrueValue = MakeReg(MVT::i32);
1072 BuildMI(BB, PPC::LI, 1, TrueValue).addSImm(1);
1073 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1074 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1075 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
1076 MachineFunction *F = BB->getParent();
1077 F->getBasicBlockList().insert(It, copy0MBB);
1078 F->getBasicBlockList().insert(It, sinkMBB);
1079 // Update machine-CFG edges
1080 BB->addSuccessor(copy0MBB);
1081 BB->addSuccessor(sinkMBB);
1082
1083 // copy0MBB:
1084 // %FalseValue = li 0
1085 // fallthrough
1086 BB = copy0MBB;
1087 unsigned FalseValue = MakeReg(MVT::i32);
1088 BuildMI(BB, PPC::LI, 1, FalseValue).addSImm(0);
1089 // Update machine-CFG edges
1090 BB->addSuccessor(sinkMBB);
1091
1092 // sinkMBB:
1093 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1094 // ...
1095 BB = sinkMBB;
1096 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1097 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1098 return Result;
1099 }
1100 assert(0 && "Is this legal?");
1101 return 0;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001102
Nate Begeman74747862005-03-29 22:24:51 +00001103 case ISD::SELECT: {
1104 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1105
1106 // Create an iterator with which to insert the MBB for copying the false
1107 // value and the MBB to hold the PHI instruction for this SetCC.
1108 MachineBasicBlock *thisMBB = BB;
1109 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1110 ilist<MachineBasicBlock>::iterator It = BB;
1111 ++It;
1112
1113 // thisMBB:
1114 // ...
1115 // TrueVal = ...
1116 // cmpTY cr0, r1, r2
1117 // bCC copy1MBB
1118 // fallthrough --> copy0MBB
1119 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(0);
1120 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1121 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1122 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
1123 BuildMI(BB, PPC::BNE, 2).addReg(PPC::CR0).addMBB(sinkMBB);
1124 MachineFunction *F = BB->getParent();
1125 F->getBasicBlockList().insert(It, copy0MBB);
1126 F->getBasicBlockList().insert(It, sinkMBB);
1127 // Update machine-CFG edges
1128 BB->addSuccessor(copy0MBB);
1129 BB->addSuccessor(sinkMBB);
1130
1131 // copy0MBB:
1132 // %FalseValue = ...
1133 // # fallthrough to sinkMBB
1134 BB = copy0MBB;
1135 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
1136 // Update machine-CFG edges
1137 BB->addSuccessor(sinkMBB);
1138
1139 // sinkMBB:
1140 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1141 // ...
1142 BB = sinkMBB;
1143 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1144 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1145
1146 // FIXME: Select i64?
1147 return Result;
1148 }
Nate Begemana9795f82005-03-24 04:41:43 +00001149
1150 case ISD::Constant:
1151 switch (N.getValueType()) {
1152 default: assert(0 && "Cannot use constants of this type!");
1153 case MVT::i1:
1154 BuildMI(BB, PPC::LI, 1, Result)
1155 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
1156 break;
1157 case MVT::i32:
1158 {
1159 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
1160 if (v < 32768 && v >= -32768) {
1161 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
1162 } else {
Nate Begeman5e966612005-03-24 06:28:42 +00001163 Tmp1 = MakeReg(MVT::i32);
1164 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
1165 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
Nate Begemana9795f82005-03-24 04:41:43 +00001166 }
1167 }
1168 }
1169 return Result;
1170 }
1171
1172 return 0;
1173}
1174
1175void ISel::Select(SDOperand N) {
1176 unsigned Tmp1, Tmp2, Opc;
1177 unsigned opcode = N.getOpcode();
1178
1179 if (!ExprMap.insert(std::make_pair(N, 1)).second)
1180 return; // Already selected.
1181
1182 SDNode *Node = N.Val;
1183
1184 switch (Node->getOpcode()) {
1185 default:
1186 Node->dump(); std::cerr << "\n";
1187 assert(0 && "Node not handled yet!");
1188 case ISD::EntryToken: return; // Noop
1189 case ISD::TokenFactor:
1190 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1191 Select(Node->getOperand(i));
1192 return;
1193 case ISD::ADJCALLSTACKDOWN:
1194 case ISD::ADJCALLSTACKUP:
1195 Select(N.getOperand(0));
1196 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1197 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? PPC::ADJCALLSTACKDOWN :
1198 PPC::ADJCALLSTACKUP;
1199 BuildMI(BB, Opc, 1).addImm(Tmp1);
1200 return;
1201 case ISD::BR: {
1202 MachineBasicBlock *Dest =
1203 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
Nate Begemana9795f82005-03-24 04:41:43 +00001204 Select(N.getOperand(0));
1205 BuildMI(BB, PPC::B, 1).addMBB(Dest);
1206 return;
1207 }
1208 case ISD::BRCOND:
1209 SelectBranchCC(N);
1210 return;
1211 case ISD::CopyToReg:
1212 Select(N.getOperand(0));
1213 Tmp1 = SelectExpr(N.getOperand(1));
1214 Tmp2 = cast<RegSDNode>(N)->getReg();
1215
1216 if (Tmp1 != Tmp2) {
1217 if (N.getOperand(1).getValueType() == MVT::f64 ||
1218 N.getOperand(1).getValueType() == MVT::f32)
1219 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
1220 else
1221 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1222 }
1223 return;
1224 case ISD::ImplicitDef:
1225 Select(N.getOperand(0));
1226 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
1227 return;
1228 case ISD::RET:
1229 switch (N.getNumOperands()) {
1230 default:
1231 assert(0 && "Unknown return instruction!");
1232 case 3:
1233 assert(N.getOperand(1).getValueType() == MVT::i32 &&
1234 N.getOperand(2).getValueType() == MVT::i32 &&
1235 "Unknown two-register value!");
1236 Select(N.getOperand(0));
1237 Tmp1 = SelectExpr(N.getOperand(1));
1238 Tmp2 = SelectExpr(N.getOperand(2));
1239 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
1240 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp2).addReg(Tmp2);
1241 break;
1242 case 2:
1243 Select(N.getOperand(0));
1244 Tmp1 = SelectExpr(N.getOperand(1));
1245 switch (N.getOperand(1).getValueType()) {
1246 default:
1247 assert(0 && "Unknown return type!");
1248 case MVT::f64:
1249 case MVT::f32:
1250 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
1251 break;
1252 case MVT::i32:
1253 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
1254 break;
1255 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001256 case 1:
1257 Select(N.getOperand(0));
1258 break;
Nate Begemana9795f82005-03-24 04:41:43 +00001259 }
1260 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
1261 return;
Nate Begemana9795f82005-03-24 04:41:43 +00001262 case ISD::TRUNCSTORE:
1263 case ISD::STORE:
1264 {
1265 SDOperand Chain = N.getOperand(0);
1266 SDOperand Value = N.getOperand(1);
1267 SDOperand Address = N.getOperand(2);
1268 Select(Chain);
1269
1270 Tmp1 = SelectExpr(Value); //value
1271
1272 if (opcode == ISD::STORE) {
1273 switch(Value.getValueType()) {
1274 default: assert(0 && "unknown Type in store");
1275 case MVT::i32: Opc = PPC::STW; break;
1276 case MVT::f64: Opc = PPC::STFD; break;
1277 case MVT::f32: Opc = PPC::STFS; break;
1278 }
1279 } else { //ISD::TRUNCSTORE
1280 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1281 default: assert(0 && "unknown Type in store");
1282 case MVT::i1: //FIXME: DAG does not promote this load
1283 case MVT::i8: Opc = PPC::STB; break;
1284 case MVT::i16: Opc = PPC::STH; break;
1285 }
1286 }
1287
1288 if (Address.getOpcode() == ISD::GlobalAddress)
1289 {
1290 BuildMI(BB, Opc, 2).addReg(Tmp1)
1291 .addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1292 }
1293 else if(Address.getOpcode() == ISD::FrameIndex)
1294 {
1295 BuildMI(BB, Opc, 2).addReg(Tmp1)
1296 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex());
1297 }
1298 else
1299 {
1300 int offset;
1301 SelectAddr(Address, Tmp2, offset);
1302 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1303 }
1304 return;
1305 }
1306 case ISD::EXTLOAD:
1307 case ISD::SEXTLOAD:
1308 case ISD::ZEXTLOAD:
1309 case ISD::LOAD:
1310 case ISD::CopyFromReg:
1311 case ISD::CALL:
1312 case ISD::DYNAMIC_STACKALLOC:
1313 ExprMap.erase(N);
1314 SelectExpr(N);
1315 return;
1316 }
1317 assert(0 && "Should not be reached!");
1318}
1319
1320
1321/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
1322/// into a machine code representation using pattern matching and a machine
1323/// description file.
1324///
1325FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) {
1326 return new ISel(TM);
Chris Lattner246fa632005-03-24 06:16:18 +00001327}
1328