blob: 823fef9b2571235fd4171d821afe7b2beaee3dfa [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
Nate Begeman23afcfb2005-03-29 22:48:55 +0000498 //Give up and do the stupid thing
Nate Begemana9795f82005-03-24 04:41:43 +0000499 unsigned Tmp1 = SelectExpr(CC);
Nate Begeman23afcfb2005-03-29 22:48:55 +0000500 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(0);
501 BuildMI(BB, PPC::BNE, 2).addReg(PPC::CR0).addMBB(Dest);
Nate Begemana9795f82005-03-24 04:41:43 +0000502 return;
503}
504
505unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
506{
507 unsigned Tmp1, Tmp2, Tmp3;
508 unsigned Opc = 0;
509 SDNode *Node = N.Val;
510 MVT::ValueType DestType = N.getValueType();
511 unsigned opcode = N.getOpcode();
512
513 switch (opcode) {
514 default:
515 Node->dump();
516 assert(0 && "Node not handled!\n");
517
Nate Begeman23afcfb2005-03-29 22:48:55 +0000518 case ISD::SELECT: {
519 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
520
521 // FIXME: generate FSEL here
522
523 // Create an iterator with which to insert the MBB for copying the false
524 // value and the MBB to hold the PHI instruction for this SetCC.
525 MachineBasicBlock *thisMBB = BB;
526 const BasicBlock *LLVM_BB = BB->getBasicBlock();
527 ilist<MachineBasicBlock>::iterator It = BB;
528 ++It;
529
530 // thisMBB:
531 // ...
532 // TrueVal = ...
533 // cmpTY cr0, r1, r2
534 // bCC copy1MBB
535 // fallthrough --> copy0MBB
536 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(0);
537 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
538 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
539 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
540 BuildMI(BB, PPC::BNE, 2).addReg(PPC::CR0).addMBB(sinkMBB);
541 MachineFunction *F = BB->getParent();
542 F->getBasicBlockList().insert(It, copy0MBB);
543 F->getBasicBlockList().insert(It, sinkMBB);
544 // Update machine-CFG edges
545 BB->addSuccessor(copy0MBB);
546 BB->addSuccessor(sinkMBB);
547
548 // copy0MBB:
549 // %FalseValue = ...
550 // # fallthrough to sinkMBB
551 BB = copy0MBB;
552 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
553 // Update machine-CFG edges
554 BB->addSuccessor(sinkMBB);
555
556 // sinkMBB:
557 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
558 // ...
559 BB = sinkMBB;
560 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
561 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
562 return Result;
563 }
Nate Begemana9795f82005-03-24 04:41:43 +0000564
565 case ISD::FP_ROUND:
566 assert (DestType == MVT::f32 &&
567 N.getOperand(0).getValueType() == MVT::f64 &&
568 "only f64 to f32 conversion supported here");
569 Tmp1 = SelectExpr(N.getOperand(0));
570 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
571 return Result;
572
573 case ISD::FP_EXTEND:
574 assert (DestType == MVT::f64 &&
575 N.getOperand(0).getValueType() == MVT::f32 &&
576 "only f32 to f64 conversion supported here");
577 Tmp1 = SelectExpr(N.getOperand(0));
578 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
579 return Result;
580
581 case ISD::CopyFromReg:
Nate Begemanf2622612005-03-26 02:17:46 +0000582 if (Result == 1)
583 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
584 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
585 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
586 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000587
588 case ISD::LOAD:
Nate Begeman9db505c2005-03-28 19:36:43 +0000589 case ISD::EXTLOAD: {
590 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
591 Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType();
592
593 // Make sure we generate both values.
594 if (Result != 1)
595 ExprMap[N.getValue(1)] = 1; // Generate the token
596 else
597 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
598
599 SDOperand Chain = N.getOperand(0);
600 SDOperand Address = N.getOperand(1);
601 Select(Chain);
602
603 switch (TypeBeingLoaded) {
604 default: assert(0 && "Cannot fp load this type!");
605 case MVT::f32: Opc = PPC::LFS; break;
606 case MVT::f64: Opc = PPC::LFD; break;
607 }
608
609 if(Address.getOpcode() == ISD::FrameIndex) {
610 BuildMI(BB, Opc, 2, Result)
611 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
612 .addReg(PPC::R1);
613 } else {
614 int offset;
615 SelectAddr(Address, Tmp1, offset);
616 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
617 }
618 return Result;
619 }
Nate Begemana9795f82005-03-24 04:41:43 +0000620
621 case ISD::ConstantFP:
Nate Begemanca12a2b2005-03-28 22:28:37 +0000622 assert(0 && "ISD::ConstantFP Unimplemented");
Nate Begemana9795f82005-03-24 04:41:43 +0000623 abort();
624
625 case ISD::MUL:
626 case ISD::ADD:
627 case ISD::SUB:
628 case ISD::SDIV:
629 switch( opcode ) {
630 case ISD::MUL: Opc = DestType == MVT::f64 ? PPC::FMUL : PPC::FMULS; break;
631 case ISD::ADD: Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS; break;
632 case ISD::SUB: Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS; break;
633 case ISD::SDIV: Opc = DestType == MVT::f64 ? PPC::FDIV : PPC::FDIVS; break;
634 };
Nate Begemana9795f82005-03-24 04:41:43 +0000635 Tmp1 = SelectExpr(N.getOperand(0));
636 Tmp2 = SelectExpr(N.getOperand(1));
637 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
638 return Result;
639
Nate Begemana9795f82005-03-24 04:41:43 +0000640 case ISD::UINT_TO_FP:
641 case ISD::SINT_TO_FP:
Nate Begemanf3d08f32005-03-29 00:03:27 +0000642 assert(0 && "ISD::U/SINT_TO_FP Unimplemented");
Nate Begemana9795f82005-03-24 04:41:43 +0000643 abort();
644 }
645 assert(0 && "should not get here");
646 return 0;
647}
648
649unsigned ISel::SelectExpr(SDOperand N) {
650 unsigned Result;
651 unsigned Tmp1, Tmp2, Tmp3;
652 unsigned Opc = 0;
653 unsigned opcode = N.getOpcode();
654
655 SDNode *Node = N.Val;
656 MVT::ValueType DestType = N.getValueType();
657
658 unsigned &Reg = ExprMap[N];
659 if (Reg) return Reg;
660
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000661 if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::ADD_PARTS &&
662 N.getOpcode() != ISD::SUB_PARTS)
Nate Begemana9795f82005-03-24 04:41:43 +0000663 Reg = Result = (N.getValueType() != MVT::Other) ?
664 MakeReg(N.getValueType()) : 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000665 else {
666 // If this is a call instruction, make sure to prepare ALL of the result
667 // values as well as the chain.
668 if (N.getOpcode() == ISD::CALL) {
669 if (Node->getNumValues() == 1)
670 Reg = Result = 1; // Void call, just a chain.
671 else {
672 Result = MakeReg(Node->getValueType(0));
673 ExprMap[N.getValue(0)] = Result;
674 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
675 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
676 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
677 }
678 } else {
679 Result = MakeReg(Node->getValueType(0));
680 ExprMap[N.getValue(0)] = Result;
681 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
682 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
683 }
684 }
685
686 if (DestType == MVT::f64 || DestType == MVT::f32)
687 return SelectExprFP(N, Result);
Nate Begemana9795f82005-03-24 04:41:43 +0000688
689 switch (opcode) {
690 default:
691 Node->dump();
692 assert(0 && "Node not handled!\n");
693
694 case ISD::DYNAMIC_STACKALLOC:
Nate Begeman5e966612005-03-24 06:28:42 +0000695 // Generate both result values. FIXME: Need a better commment here?
696 if (Result != 1)
697 ExprMap[N.getValue(1)] = 1;
698 else
699 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
700
701 // FIXME: We are currently ignoring the requested alignment for handling
702 // greater than the stack alignment. This will need to be revisited at some
703 // point. Align = N.getOperand(2);
704 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
705 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
706 std::cerr << "Cannot allocate stack object with greater alignment than"
707 << " the stack alignment yet!";
708 abort();
709 }
710 Select(N.getOperand(0));
711 Tmp1 = SelectExpr(N.getOperand(1));
712 // Subtract size from stack pointer, thereby allocating some space.
713 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
714 // Put a pointer to the space into the result register by copying the SP
715 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
716 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000717
718 case ISD::ConstantPool:
Nate Begemanca12a2b2005-03-28 22:28:37 +0000719 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
720 Tmp2 = MakeReg(MVT::i32);
721 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp2).addReg(getGlobalBaseReg())
722 .addConstantPoolIndex(Tmp1);
723 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
724 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000725
726 case ISD::FrameIndex:
Nate Begemanf3d08f32005-03-29 00:03:27 +0000727 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
728 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1);
729 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000730
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000731 case ISD::GlobalAddress: {
732 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanca12a2b2005-03-28 22:28:37 +0000733 Tmp1 = MakeReg(MVT::i32);
Nate Begemanc7b09f12005-03-25 08:34:25 +0000734 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
735 .addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000736 if (GV->hasWeakLinkage() || GV->isExternal()) {
737 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
738 } else {
739 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
740 }
741 return Result;
742 }
743
Nate Begeman5e966612005-03-24 06:28:42 +0000744 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +0000745 case ISD::EXTLOAD:
746 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000747 case ISD::SEXTLOAD: {
Nate Begeman9db505c2005-03-28 19:36:43 +0000748 bool sext = (ISD::SEXTLOAD == opcode);
749 bool byte = (MVT::i8 == Node->getValueType(0));
750 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
751 Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType();
752
Nate Begeman5e966612005-03-24 06:28:42 +0000753 // Make sure we generate both values.
754 if (Result != 1)
755 ExprMap[N.getValue(1)] = 1; // Generate the token
756 else
757 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
758
759 SDOperand Chain = N.getOperand(0);
760 SDOperand Address = N.getOperand(1);
761 Select(Chain);
762
Nate Begeman9db505c2005-03-28 19:36:43 +0000763 switch (TypeBeingLoaded) {
Nate Begeman5e966612005-03-24 06:28:42 +0000764 default: assert(0 && "Cannot load this type!");
Nate Begeman9db505c2005-03-28 19:36:43 +0000765 case MVT::i1: Opc = PPC::LBZ; break;
766 case MVT::i8: Opc = PPC::LBZ; break;
767 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
768 case MVT::i32: Opc = PPC::LWZ; break;
Nate Begeman5e966612005-03-24 06:28:42 +0000769 }
770
Nate Begeman9db505c2005-03-28 19:36:43 +0000771 // Since there's no load byte & sign extend instruction we have to split
772 // byte SEXTLOADs into lbz + extsb. This requires we make a temp register.
773 if (sext && byte) {
774 Tmp3 = Result;
775 Result = MakeReg(MVT::i32);
Chris Lattner848132d2005-03-29 15:13:27 +0000776 } else {
777 Tmp3 = 0; // Silence GCC warning.
Nate Begeman9db505c2005-03-28 19:36:43 +0000778 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000779 if(Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman5e966612005-03-24 06:28:42 +0000780 BuildMI(BB, Opc, 2, Result)
781 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
782 .addReg(PPC::R1);
783 } else {
784 int offset;
785 SelectAddr(Address, Tmp1, offset);
786 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
787 }
Nate Begeman9db505c2005-03-28 19:36:43 +0000788 if (sext && byte) {
789 BuildMI(BB, PPC::EXTSB, 1, Tmp3).addReg(Result);
790 Result = Tmp3;
791 }
Nate Begeman5e966612005-03-24 06:28:42 +0000792 return Result;
793 }
794
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000795 case ISD::CALL: {
796 // Lower the chain for this call.
797 Select(N.getOperand(0));
798 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
799
800 // get the virtual reg for each argument
801 std::vector<unsigned> VRegs;
802 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
803 VRegs.push_back(SelectExpr(N.getOperand(i)));
804
805 // The ABI specifies that the first 32 bytes of args may be passed in GPRs,
806 // and that 13 FPRs may also be used for passing any floating point args.
807 int GPR_remaining = 8, FPR_remaining = 13;
808 unsigned GPR_idx = 0, FPR_idx = 0;
809 static const unsigned GPR[] = {
810 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
811 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
812 };
813 static const unsigned FPR[] = {
814 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6,
815 PPC::F7, PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12,
816 PPC::F13
817 };
818
819 // move the vregs into the appropriate architected register or stack slot
820 for(int i = 0, e = VRegs.size(); i < e; ++i) {
821 unsigned OperandType = N.getOperand(i+2).getValueType();
822 switch(OperandType) {
823 default:
824 Node->dump();
825 N.getOperand(i).Val->dump();
826 std::cerr << "Type for " << i << " is: " <<
827 N.getOperand(i+2).getValueType() << "\n";
828 assert(0 && "Unknown value type for call");
829 case MVT::i1:
830 case MVT::i8:
831 case MVT::i16:
832 case MVT::i32:
833 if (GPR_remaining > 0)
834 BuildMI(BB, PPC::OR, 2, GPR[GPR_idx]).addReg(VRegs[i])
835 .addReg(VRegs[i]);
836 break;
837 case MVT::f32:
838 case MVT::f64:
839 if (FPR_remaining > 0) {
840 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(VRegs[i]);
Nate Begemanf2622612005-03-26 02:17:46 +0000841 ++FPR_idx;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000842 --FPR_remaining;
843 }
844 break;
845 }
846 // All arguments consume GPRs available for argument passing
Nate Begemanf2622612005-03-26 02:17:46 +0000847 if (GPR_remaining > 0) {
848 ++GPR_idx;
849 --GPR_remaining;
850 }
851 if (MVT::f64 == OperandType && GPR_remaining > 0) {
852 ++GPR_idx;
853 --GPR_remaining;
854 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000855 }
856
857 // Emit the correct call instruction based on the type of symbol called.
858 if (GlobalAddressSDNode *GASD =
859 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
860 BuildMI(BB, PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(), true);
861 } else if (ExternalSymbolSDNode *ESSDN =
862 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
863 BuildMI(BB, PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(), true);
864 } else {
865 Tmp1 = SelectExpr(N.getOperand(1));
866 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
867 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
868 BuildMI(BB, PPC::CALLindirect, 3).addImm(20).addImm(0).addReg(PPC::R12);
869 }
870
871 switch (Node->getValueType(0)) {
872 default: assert(0 && "Unknown value type for call result!");
873 case MVT::Other: return 1;
874 case MVT::i1:
875 case MVT::i8:
876 case MVT::i16:
877 case MVT::i32:
Nate Begemanc7b09f12005-03-25 08:34:25 +0000878 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000879 if (Node->getValueType(1) == MVT::i32)
Nate Begemanc7b09f12005-03-25 08:34:25 +0000880 BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R4).addReg(PPC::R4);
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000881 break;
882 case MVT::f32:
883 case MVT::f64:
884 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
885 break;
886 }
887 return Result+N.ResNo;
888 }
Nate Begemana9795f82005-03-24 04:41:43 +0000889
890 case ISD::SIGN_EXTEND:
891 case ISD::SIGN_EXTEND_INREG:
892 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman9db505c2005-03-28 19:36:43 +0000893 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
894 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
895 case MVT::i16:
896 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
897 break;
898 case MVT::i8:
899 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
900 break;
Nate Begeman74747862005-03-29 22:24:51 +0000901 case MVT::i1:
902 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
903 break;
Nate Begeman9db505c2005-03-28 19:36:43 +0000904 }
Nate Begemana9795f82005-03-24 04:41:43 +0000905 return Result;
906
907 case ISD::ZERO_EXTEND_INREG:
908 Tmp1 = SelectExpr(N.getOperand(0));
909 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
Nate Begeman9db505c2005-03-28 19:36:43 +0000910 default: Node->dump(); assert(0 && "Unhandled ZERO_EXTEND type"); break;
Nate Begemana9795f82005-03-24 04:41:43 +0000911 case MVT::i16: Tmp2 = 16; break;
912 case MVT::i8: Tmp2 = 24; break;
913 case MVT::i1: Tmp2 = 31; break;
914 }
Nate Begeman33162522005-03-29 21:54:38 +0000915 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(0).addImm(Tmp2)
916 .addImm(31);
Nate Begemana9795f82005-03-24 04:41:43 +0000917 return Result;
918
Nate Begemana9795f82005-03-24 04:41:43 +0000919 case ISD::CopyFromReg:
920 if (Result == 1)
921 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
922 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
923 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
924 return Result;
925
926 case ISD::SHL:
Nate Begeman5e966612005-03-24 06:28:42 +0000927 Tmp1 = SelectExpr(N.getOperand(0));
928 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
929 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +0000930 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
Nate Begeman5e966612005-03-24 06:28:42 +0000931 .addImm(31-Tmp2);
932 } else {
933 Tmp2 = SelectExpr(N.getOperand(1));
934 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
935 }
936 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000937
Nate Begeman5e966612005-03-24 06:28:42 +0000938 case ISD::SRL:
939 Tmp1 = SelectExpr(N.getOperand(0));
940 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
941 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +0000942 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2)
Nate Begeman5e966612005-03-24 06:28:42 +0000943 .addImm(Tmp2).addImm(31);
944 } else {
945 Tmp2 = SelectExpr(N.getOperand(1));
946 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
947 }
948 return Result;
949
950 case ISD::SRA:
951 Tmp1 = SelectExpr(N.getOperand(0));
952 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
953 Tmp2 = CN->getValue() & 0x1F;
954 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
955 } else {
956 Tmp2 = SelectExpr(N.getOperand(1));
957 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
958 }
959 return Result;
960
Nate Begemana9795f82005-03-24 04:41:43 +0000961 case ISD::ADD:
962 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
963 Tmp1 = SelectExpr(N.getOperand(0));
964 switch(canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
965 default: assert(0 && "unhandled result code");
966 case 0: // No immediate
967 Tmp2 = SelectExpr(N.getOperand(1));
968 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
969 break;
970 case 1: // Low immediate
971 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
972 break;
973 case 2: // Shifted immediate
974 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
975 break;
976 }
977 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000978
Nate Begemana9795f82005-03-24 04:41:43 +0000979 case ISD::AND:
980 case ISD::OR:
981 case ISD::XOR:
982 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
983 Tmp1 = SelectExpr(N.getOperand(0));
984 switch(canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
985 default: assert(0 && "unhandled result code");
986 case 0: // No immediate
987 Tmp2 = SelectExpr(N.getOperand(1));
988 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +0000989 case ISD::AND: Opc = PPC::AND; break;
990 case ISD::OR: Opc = PPC::OR; break;
991 case ISD::XOR: Opc = PPC::XOR; break;
Nate Begemana9795f82005-03-24 04:41:43 +0000992 }
Nate Begeman5e966612005-03-24 06:28:42 +0000993 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +0000994 break;
995 case 1: // Low immediate
996 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +0000997 case ISD::AND: Opc = PPC::ANDIo; break;
998 case ISD::OR: Opc = PPC::ORI; break;
999 case ISD::XOR: Opc = PPC::XORI; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001000 }
Nate Begeman5e966612005-03-24 06:28:42 +00001001 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001002 break;
1003 case 2: // Shifted immediate
1004 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +00001005 case ISD::AND: Opc = PPC::ANDISo; break;
1006 case ISD::OR: Opc = PPC::ORIS; break;
1007 case ISD::XOR: Opc = PPC::XORIS; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001008 }
Nate Begeman5e966612005-03-24 06:28:42 +00001009 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001010 break;
1011 }
1012 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001013
1014 case ISD::SUB:
1015 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1016 Tmp1 = SelectExpr(N.getOperand(0));
1017 Tmp2 = SelectExpr(N.getOperand(1));
1018 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
1019 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001020
Nate Begeman5e966612005-03-24 06:28:42 +00001021 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001022 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1023 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman307e7442005-03-26 01:28:53 +00001024 if (1 == canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
1025 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1026 else {
1027 Tmp2 = SelectExpr(N.getOperand(1));
1028 BuildMI(BB, PPC::MULLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1029 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001030 return Result;
1031
Nate Begemanf3d08f32005-03-29 00:03:27 +00001032 case ISD::SDIV:
1033 case ISD::UDIV:
1034 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1035 Tmp1 = SelectExpr(N.getOperand(0));
1036 Tmp2 = SelectExpr(N.getOperand(1));
1037 Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW;
1038 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1039 return Result;
1040
1041 case ISD::UREM:
1042 case ISD::SREM: {
1043 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1044 Tmp1 = SelectExpr(N.getOperand(0));
1045 Tmp2 = SelectExpr(N.getOperand(1));
1046 Tmp3 = MakeReg(MVT::i32);
1047 unsigned Tmp4 = MakeReg(MVT::i32);
1048 Opc = (ISD::UREM == opcode) ? PPC::DIVWU : PPC::DIVW;
1049 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1050 BuildMI(BB, PPC::MULLW, 2, Tmp4).addReg(Tmp3).addReg(Tmp2);
1051 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp4).addReg(Tmp1);
1052 return Result;
1053 }
1054
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001055 case ISD::ADD_PARTS:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001056 case ISD::SUB_PARTS: {
1057 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1058 "Not an i64 add/sub!");
1059 // Emit all of the operands.
1060 std::vector<unsigned> InVals;
1061 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
1062 InVals.push_back(SelectExpr(N.getOperand(i)));
1063 if (N.getOpcode() == ISD::ADD_PARTS) {
Nate Begemanf70b5762005-03-28 23:08:54 +00001064 BuildMI(BB, PPC::ADDC, 2, Result+1).addReg(InVals[0]).addReg(InVals[2]);
1065 BuildMI(BB, PPC::ADDE, 2, Result).addReg(InVals[1]).addReg(InVals[3]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001066 } else {
Nate Begemanf70b5762005-03-28 23:08:54 +00001067 BuildMI(BB, PPC::SUBFC, 2, Result+1).addReg(InVals[2]).addReg(InVals[0]);
1068 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(InVals[3]).addReg(InVals[1]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001069 }
1070 return Result+N.ResNo;
1071 }
1072
Nate Begemana9795f82005-03-24 04:41:43 +00001073 case ISD::FP_TO_UINT:
1074 case ISD::FP_TO_SINT:
1075 abort();
1076
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001077 case ISD::SETCC:
Nate Begeman33162522005-03-29 21:54:38 +00001078 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1079 bool U = false;
1080 bool IsInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
1081
1082 switch (SetCC->getCondition()) {
1083 default: Node->dump(); assert(0 && "Unknown comparison!");
1084 case ISD::SETEQ: Opc = PPC::BEQ; break;
1085 case ISD::SETNE: Opc = PPC::BNE; break;
1086 case ISD::SETULT: U = true;
1087 case ISD::SETLT: Opc = PPC::BLT; break;
1088 case ISD::SETULE: U = true;
1089 case ISD::SETLE: Opc = PPC::BLE; break;
1090 case ISD::SETUGT: U = true;
1091 case ISD::SETGT: Opc = PPC::BGT; break;
1092 case ISD::SETUGE: U = true;
1093 case ISD::SETGE: Opc = PPC::BGE; break;
1094 }
1095
1096 // FIXME: Is there a situation in which we would ever need to emit fcmpo?
1097 static const unsigned CompareOpcodes[] =
1098 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
1099 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
1100
1101 // Create an iterator with which to insert the MBB for copying the false
1102 // value and the MBB to hold the PHI instruction for this SetCC.
1103 MachineBasicBlock *thisMBB = BB;
1104 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1105 ilist<MachineBasicBlock>::iterator It = BB;
1106 ++It;
1107
1108 // thisMBB:
1109 // ...
1110 // cmpTY cr0, r1, r2
1111 // %TrueValue = li 1
1112 // bCC sinkMBB
1113 Tmp1 = SelectExpr(N.getOperand(0));
1114 Tmp2 = SelectExpr(N.getOperand(1));
1115 BuildMI(BB, CompareOpc, 2, PPC::CR0).addReg(Tmp1).addReg(Tmp2);
1116 unsigned TrueValue = MakeReg(MVT::i32);
1117 BuildMI(BB, PPC::LI, 1, TrueValue).addSImm(1);
1118 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1119 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1120 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
1121 MachineFunction *F = BB->getParent();
1122 F->getBasicBlockList().insert(It, copy0MBB);
1123 F->getBasicBlockList().insert(It, sinkMBB);
1124 // Update machine-CFG edges
1125 BB->addSuccessor(copy0MBB);
1126 BB->addSuccessor(sinkMBB);
1127
1128 // copy0MBB:
1129 // %FalseValue = li 0
1130 // fallthrough
1131 BB = copy0MBB;
1132 unsigned FalseValue = MakeReg(MVT::i32);
1133 BuildMI(BB, PPC::LI, 1, FalseValue).addSImm(0);
1134 // Update machine-CFG edges
1135 BB->addSuccessor(sinkMBB);
1136
1137 // sinkMBB:
1138 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1139 // ...
1140 BB = sinkMBB;
1141 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1142 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1143 return Result;
1144 }
1145 assert(0 && "Is this legal?");
1146 return 0;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001147
Nate Begeman74747862005-03-29 22:24:51 +00001148 case ISD::SELECT: {
1149 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1150
1151 // Create an iterator with which to insert the MBB for copying the false
1152 // value and the MBB to hold the PHI instruction for this SetCC.
1153 MachineBasicBlock *thisMBB = BB;
1154 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1155 ilist<MachineBasicBlock>::iterator It = BB;
1156 ++It;
1157
1158 // thisMBB:
1159 // ...
1160 // TrueVal = ...
1161 // cmpTY cr0, r1, r2
1162 // bCC copy1MBB
1163 // fallthrough --> copy0MBB
1164 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(0);
1165 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1166 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1167 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
1168 BuildMI(BB, PPC::BNE, 2).addReg(PPC::CR0).addMBB(sinkMBB);
1169 MachineFunction *F = BB->getParent();
1170 F->getBasicBlockList().insert(It, copy0MBB);
1171 F->getBasicBlockList().insert(It, sinkMBB);
1172 // Update machine-CFG edges
1173 BB->addSuccessor(copy0MBB);
1174 BB->addSuccessor(sinkMBB);
1175
1176 // copy0MBB:
1177 // %FalseValue = ...
1178 // # fallthrough to sinkMBB
1179 BB = copy0MBB;
1180 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
1181 // Update machine-CFG edges
1182 BB->addSuccessor(sinkMBB);
1183
1184 // sinkMBB:
1185 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1186 // ...
1187 BB = sinkMBB;
1188 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1189 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1190
1191 // FIXME: Select i64?
1192 return Result;
1193 }
Nate Begemana9795f82005-03-24 04:41:43 +00001194
1195 case ISD::Constant:
1196 switch (N.getValueType()) {
1197 default: assert(0 && "Cannot use constants of this type!");
1198 case MVT::i1:
1199 BuildMI(BB, PPC::LI, 1, Result)
1200 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
1201 break;
1202 case MVT::i32:
1203 {
1204 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
1205 if (v < 32768 && v >= -32768) {
1206 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
1207 } else {
Nate Begeman5e966612005-03-24 06:28:42 +00001208 Tmp1 = MakeReg(MVT::i32);
1209 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
1210 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
Nate Begemana9795f82005-03-24 04:41:43 +00001211 }
1212 }
1213 }
1214 return Result;
1215 }
1216
1217 return 0;
1218}
1219
1220void ISel::Select(SDOperand N) {
1221 unsigned Tmp1, Tmp2, Opc;
1222 unsigned opcode = N.getOpcode();
1223
1224 if (!ExprMap.insert(std::make_pair(N, 1)).second)
1225 return; // Already selected.
1226
1227 SDNode *Node = N.Val;
1228
1229 switch (Node->getOpcode()) {
1230 default:
1231 Node->dump(); std::cerr << "\n";
1232 assert(0 && "Node not handled yet!");
1233 case ISD::EntryToken: return; // Noop
1234 case ISD::TokenFactor:
1235 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1236 Select(Node->getOperand(i));
1237 return;
1238 case ISD::ADJCALLSTACKDOWN:
1239 case ISD::ADJCALLSTACKUP:
1240 Select(N.getOperand(0));
1241 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1242 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? PPC::ADJCALLSTACKDOWN :
1243 PPC::ADJCALLSTACKUP;
1244 BuildMI(BB, Opc, 1).addImm(Tmp1);
1245 return;
1246 case ISD::BR: {
1247 MachineBasicBlock *Dest =
1248 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
Nate Begemana9795f82005-03-24 04:41:43 +00001249 Select(N.getOperand(0));
1250 BuildMI(BB, PPC::B, 1).addMBB(Dest);
1251 return;
1252 }
1253 case ISD::BRCOND:
1254 SelectBranchCC(N);
1255 return;
1256 case ISD::CopyToReg:
1257 Select(N.getOperand(0));
1258 Tmp1 = SelectExpr(N.getOperand(1));
1259 Tmp2 = cast<RegSDNode>(N)->getReg();
1260
1261 if (Tmp1 != Tmp2) {
1262 if (N.getOperand(1).getValueType() == MVT::f64 ||
1263 N.getOperand(1).getValueType() == MVT::f32)
1264 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
1265 else
1266 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1267 }
1268 return;
1269 case ISD::ImplicitDef:
1270 Select(N.getOperand(0));
1271 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
1272 return;
1273 case ISD::RET:
1274 switch (N.getNumOperands()) {
1275 default:
1276 assert(0 && "Unknown return instruction!");
1277 case 3:
1278 assert(N.getOperand(1).getValueType() == MVT::i32 &&
1279 N.getOperand(2).getValueType() == MVT::i32 &&
1280 "Unknown two-register value!");
1281 Select(N.getOperand(0));
1282 Tmp1 = SelectExpr(N.getOperand(1));
1283 Tmp2 = SelectExpr(N.getOperand(2));
1284 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
1285 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp2).addReg(Tmp2);
1286 break;
1287 case 2:
1288 Select(N.getOperand(0));
1289 Tmp1 = SelectExpr(N.getOperand(1));
1290 switch (N.getOperand(1).getValueType()) {
1291 default:
1292 assert(0 && "Unknown return type!");
1293 case MVT::f64:
1294 case MVT::f32:
1295 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
1296 break;
1297 case MVT::i32:
1298 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
1299 break;
1300 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001301 case 1:
1302 Select(N.getOperand(0));
1303 break;
Nate Begemana9795f82005-03-24 04:41:43 +00001304 }
1305 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
1306 return;
Nate Begemana9795f82005-03-24 04:41:43 +00001307 case ISD::TRUNCSTORE:
1308 case ISD::STORE:
1309 {
1310 SDOperand Chain = N.getOperand(0);
1311 SDOperand Value = N.getOperand(1);
1312 SDOperand Address = N.getOperand(2);
1313 Select(Chain);
1314
1315 Tmp1 = SelectExpr(Value); //value
1316
1317 if (opcode == ISD::STORE) {
1318 switch(Value.getValueType()) {
1319 default: assert(0 && "unknown Type in store");
1320 case MVT::i32: Opc = PPC::STW; break;
1321 case MVT::f64: Opc = PPC::STFD; break;
1322 case MVT::f32: Opc = PPC::STFS; break;
1323 }
1324 } else { //ISD::TRUNCSTORE
1325 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1326 default: assert(0 && "unknown Type in store");
1327 case MVT::i1: //FIXME: DAG does not promote this load
1328 case MVT::i8: Opc = PPC::STB; break;
1329 case MVT::i16: Opc = PPC::STH; break;
1330 }
1331 }
1332
1333 if (Address.getOpcode() == ISD::GlobalAddress)
1334 {
1335 BuildMI(BB, Opc, 2).addReg(Tmp1)
1336 .addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1337 }
1338 else if(Address.getOpcode() == ISD::FrameIndex)
1339 {
1340 BuildMI(BB, Opc, 2).addReg(Tmp1)
1341 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex());
1342 }
1343 else
1344 {
1345 int offset;
1346 SelectAddr(Address, Tmp2, offset);
1347 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1348 }
1349 return;
1350 }
1351 case ISD::EXTLOAD:
1352 case ISD::SEXTLOAD:
1353 case ISD::ZEXTLOAD:
1354 case ISD::LOAD:
1355 case ISD::CopyFromReg:
1356 case ISD::CALL:
1357 case ISD::DYNAMIC_STACKALLOC:
1358 ExprMap.erase(N);
1359 SelectExpr(N);
1360 return;
1361 }
1362 assert(0 && "Should not be reached!");
1363}
1364
1365
1366/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
1367/// into a machine code representation using pattern matching and a machine
1368/// description file.
1369///
1370FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) {
1371 return new ISel(TM);
Chris Lattner246fa632005-03-24 06:16:18 +00001372}
1373