blob: cbff20f866d427dadcb53747dc1ac4117ab03bf9 [file] [log] [blame]
Nate Begemana3829d52005-04-05 17:32:30 +00001//===-- PPC64ISelPattern.cpp - A pattern matching inst selector for PPC64 -===//
Nate Begemand3e6b942005-04-05 08:51:15 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Nate Begeman and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Nate Begemana3829d52005-04-05 17:32:30 +000010// This file defines a pattern matching instruction selector for 64 bit PowerPC.
Nate Begemand3e6b942005-04-05 08:51:15 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "PowerPC.h"
15#include "PowerPCInstrBuilder.h"
16#include "PowerPCInstrInfo.h"
17#include "PPC64RegisterInfo.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/Target/TargetOptions.h"
29#include "llvm/Support/Debug.h"
30#include "llvm/Support/MathExtras.h"
31#include "llvm/ADT/Statistic.h"
32#include <set>
33#include <algorithm>
34using namespace llvm;
35
36//===----------------------------------------------------------------------===//
37// PPC32TargetLowering - PPC32 Implementation of the TargetLowering interface
38namespace {
39 class PPC64TargetLowering : public TargetLowering {
40 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
41 int ReturnAddrIndex; // FrameIndex for return slot.
42 public:
43 PPC64TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
44 // Set up the register classes.
45 addRegisterClass(MVT::i64, PPC64::GPRCRegisterClass);
46 addRegisterClass(MVT::f32, PPC64::FPRCRegisterClass);
47 addRegisterClass(MVT::f64, PPC64::FPRCRegisterClass);
48
49 // PowerPC has no intrinsics for these particular operations
50 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
51 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
52 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
53
54 // PPC 64 has i16 and i32 but no i8 (or i1) SEXTLOAD
55 setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
56 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
57
58 setShiftAmountFlavor(Extend); // shl X, 32 == 0
59 addLegalFPImmediate(+0.0); // Necessary for FSEL
60 addLegalFPImmediate(-0.0); //
61
62 computeRegisterProperties();
63 }
64
65 /// LowerArguments - This hook must be implemented to indicate how we should
66 /// lower the arguments for the specified function, into the specified DAG.
67 virtual std::vector<SDOperand>
68 LowerArguments(Function &F, SelectionDAG &DAG);
69
70 /// LowerCallTo - This hook lowers an abstract call to a function into an
71 /// actual call.
72 virtual std::pair<SDOperand, SDOperand>
73 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
74 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
75
76 virtual std::pair<SDOperand, SDOperand>
77 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
78
79 virtual std::pair<SDOperand,SDOperand>
80 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
81 const Type *ArgTy, SelectionDAG &DAG);
82
83 virtual std::pair<SDOperand, SDOperand>
84 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
85 SelectionDAG &DAG);
86 };
87}
88
89
90std::vector<SDOperand>
91PPC64TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
92 //
93 // add beautiful description of PPC stack frame format, or at least some docs
94 //
95 MachineFunction &MF = DAG.getMachineFunction();
96 MachineFrameInfo *MFI = MF.getFrameInfo();
97 MachineBasicBlock& BB = MF.front();
98 std::vector<SDOperand> ArgValues;
99
100 // Due to the rather complicated nature of the PowerPC ABI, rather than a
101 // fixed size array of physical args, for the sake of simplicity let the STL
102 // handle tracking them for us.
103 std::vector<unsigned> argVR, argPR, argOp;
104 unsigned ArgOffset = 24;
105 unsigned GPR_remaining = 8;
106 unsigned FPR_remaining = 13;
107 unsigned GPR_idx = 0, FPR_idx = 0;
108 static const unsigned GPR[] = {
109 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
110 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
111 };
112 static const unsigned FPR[] = {
113 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
114 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
115 };
116
117 // Add DAG nodes to load the arguments... On entry to a function on PPC,
118 // the arguments start at offset 24, although they are likely to be passed
119 // in registers.
120 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
121 SDOperand newroot, argt;
122 unsigned ObjSize;
123 bool needsLoad = false;
124 MVT::ValueType ObjectVT = getValueType(I->getType());
125
126 switch (ObjectVT) {
127 default: assert(0 && "Unhandled argument type!");
128 case MVT::i1:
129 case MVT::i8:
130 case MVT::i16:
131 case MVT::i32:
132 ObjSize = 4;
133 if (GPR_remaining > 0) {
134 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
135 argt = newroot = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32,
136 DAG.getRoot());
137 if (ObjectVT != MVT::i32)
138 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, newroot);
139 } else {
140 needsLoad = true;
141 }
142 break;
143 case MVT::i64: ObjSize = 8;
144 // FIXME: can split 64b load between reg/mem if it is last arg in regs
145 if (GPR_remaining > 1) {
146 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
147 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx+1]);
148 // Copy the extracted halves into the virtual registers
149 SDOperand argHi = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32,
150 DAG.getRoot());
151 SDOperand argLo = DAG.getCopyFromReg(GPR[GPR_idx+1], MVT::i32, argHi);
152 // Build the outgoing arg thingy
153 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
154 newroot = argLo;
155 } else {
156 needsLoad = true;
157 }
158 break;
159 case MVT::f32: ObjSize = 4;
160 case MVT::f64: ObjSize = 8;
161 if (FPR_remaining > 0) {
162 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, FPR[FPR_idx]);
163 argt = newroot = DAG.getCopyFromReg(FPR[FPR_idx], ObjectVT,
164 DAG.getRoot());
165 --FPR_remaining;
166 ++FPR_idx;
167 } else {
168 needsLoad = true;
169 }
170 break;
171 }
172
173 // We need to load the argument to a virtual register if we determined above
174 // that we ran out of physical registers of the appropriate type
175 if (needsLoad) {
176 unsigned SubregOffset = 0;
177 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
178 if (ObjectVT == MVT::i16) SubregOffset = 2;
179 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
180 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
181 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
182 DAG.getConstant(SubregOffset, MVT::i32));
183 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN);
184 }
185
186 // Every 4 bytes of argument space consumes one of the GPRs available for
187 // argument passing.
188 if (GPR_remaining > 0) {
189 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
190 GPR_remaining -= delta;
191 GPR_idx += delta;
192 }
193 ArgOffset += ObjSize;
194
195 DAG.setRoot(newroot.getValue(1));
196 ArgValues.push_back(argt);
197 }
198
199 // If the function takes variable number of arguments, make a frame index for
200 // the start of the first vararg value... for expansion of llvm.va_start.
201 if (F.isVarArg()) {
202 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
203 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
204 // If this function is vararg, store any remaining integer argument regs
205 // to their spots on the stack so that they may be loaded by deferencing the
206 // result of va_next.
207 std::vector<SDOperand> MemOps;
208 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
209 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
210 SDOperand Val = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32, DAG.getRoot());
211 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
212 Val, FIN);
213 MemOps.push_back(Store);
214 // Increment the address by four for the next argument to store
215 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
216 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
217 }
218 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
219 }
220
221 return ArgValues;
222}
223
224std::pair<SDOperand, SDOperand>
225PPC64TargetLowering::LowerCallTo(SDOperand Chain,
226 const Type *RetTy, bool isVarArg,
227 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG) {
228 // args_to_use will accumulate outgoing args for the ISD::CALL case in
229 // SelectExpr to use to put the arguments in the appropriate registers.
230 std::vector<SDOperand> args_to_use;
231
232 // Count how many bytes are to be pushed on the stack, including the linkage
233 // area, and parameter passing area.
234 unsigned NumBytes = 24;
235
236 if (Args.empty()) {
237 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
238 DAG.getConstant(NumBytes, getPointerTy()));
239 } else {
240 for (unsigned i = 0, e = Args.size(); i != e; ++i)
241 switch (getValueType(Args[i].second)) {
242 default: assert(0 && "Unknown value type!");
243 case MVT::i1:
244 case MVT::i8:
245 case MVT::i16:
246 case MVT::i32:
247 case MVT::f32:
248 NumBytes += 4;
249 break;
250 case MVT::i64:
251 case MVT::f64:
252 NumBytes += 8;
253 break;
254 }
255
256 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
257 // plus 32 bytes of argument space in case any called code gets funky on us.
258 if (NumBytes < 56) NumBytes = 56;
259
260 // Adjust the stack pointer for the new arguments...
261 // These operations are automatically eliminated by the prolog/epilog pass
262 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
263 DAG.getConstant(NumBytes, getPointerTy()));
264
265 // Set up a copy of the stack pointer for use loading and storing any
266 // arguments that may not fit in the registers available for argument
267 // passing.
268 SDOperand StackPtr = DAG.getCopyFromReg(PPC::R1, MVT::i32,
269 DAG.getEntryNode());
270
271 // Figure out which arguments are going to go in registers, and which in
272 // memory. Also, if this is a vararg function, floating point operations
273 // must be stored to our stack, and loaded into integer regs as well, if
274 // any integer regs are available for argument passing.
275 unsigned ArgOffset = 24;
276 unsigned GPR_remaining = 8;
277 unsigned FPR_remaining = 13;
278
279 std::vector<SDOperand> MemOps;
280 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
281 // PtrOff will be used to store the current argument to the stack if a
282 // register cannot be found for it.
283 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
284 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
285 MVT::ValueType ArgVT = getValueType(Args[i].second);
286
287 switch (ArgVT) {
288 default: assert(0 && "Unexpected ValueType for argument!");
289 case MVT::i1:
290 case MVT::i8:
291 case MVT::i16:
292 // Promote the integer to 32 bits. If the input type is signed use a
293 // sign extend, otherwise use a zero extend.
294 if (Args[i].second->isSigned())
295 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
296 else
297 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
298 // FALL THROUGH
299 case MVT::i32:
300 if (GPR_remaining > 0) {
301 args_to_use.push_back(Args[i].first);
302 --GPR_remaining;
303 } else {
304 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
305 Args[i].first, PtrOff));
306 }
307 ArgOffset += 4;
308 break;
309 case MVT::i64:
310 // If we have one free GPR left, we can place the upper half of the i64
311 // in it, and store the other half to the stack. If we have two or more
312 // free GPRs, then we can pass both halves of the i64 in registers.
313 if (GPR_remaining > 0) {
314 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
315 Args[i].first, DAG.getConstant(1, MVT::i32));
316 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
317 Args[i].first, DAG.getConstant(0, MVT::i32));
318 args_to_use.push_back(Hi);
319 --GPR_remaining;
320 if (GPR_remaining > 0) {
321 args_to_use.push_back(Lo);
322 --GPR_remaining;
323 } else {
324 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
325 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
326 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
327 Lo, PtrOff));
328 }
329 } else {
330 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
331 Args[i].first, PtrOff));
332 }
333 ArgOffset += 8;
334 break;
335 case MVT::f32:
336 case MVT::f64:
337 if (FPR_remaining > 0) {
338 args_to_use.push_back(Args[i].first);
339 --FPR_remaining;
340 if (isVarArg) {
341 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
342 Args[i].first, PtrOff);
343 MemOps.push_back(Store);
344 // Float varargs are always shadowed in available integer registers
345 if (GPR_remaining > 0) {
346 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff);
347 MemOps.push_back(Load);
348 args_to_use.push_back(Load);
349 --GPR_remaining;
350 }
351 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
352 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
353 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
354 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff);
355 MemOps.push_back(Load);
356 args_to_use.push_back(Load);
357 --GPR_remaining;
358 }
359 } else {
360 // If we have any FPRs remaining, we may also have GPRs remaining.
361 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
362 // GPRs.
363 if (GPR_remaining > 0) {
364 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
365 --GPR_remaining;
366 }
367 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
368 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
369 --GPR_remaining;
370 }
371 }
372 } else {
373 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
374 Args[i].first, PtrOff));
375 }
376 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
377 break;
378 }
379 }
380 if (!MemOps.empty())
381 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
382 }
383
384 std::vector<MVT::ValueType> RetVals;
385 MVT::ValueType RetTyVT = getValueType(RetTy);
386 if (RetTyVT != MVT::isVoid)
387 RetVals.push_back(RetTyVT);
388 RetVals.push_back(MVT::Other);
389
390 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
391 Chain, Callee, args_to_use), 0);
392 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
393 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
394 DAG.getConstant(NumBytes, getPointerTy()));
395 return std::make_pair(TheCall, Chain);
396}
397
398std::pair<SDOperand, SDOperand>
399PPC64TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
400 //vastart just returns the address of the VarArgsFrameIndex slot.
401 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
402}
403
404std::pair<SDOperand,SDOperand> PPC64TargetLowering::
405LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
406 const Type *ArgTy, SelectionDAG &DAG) {
407 MVT::ValueType ArgVT = getValueType(ArgTy);
408 SDOperand Result;
409 if (!isVANext) {
410 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList);
411 } else {
412 unsigned Amt;
413 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
414 Amt = 4;
415 else {
416 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
417 "Other types should have been promoted for varargs!");
418 Amt = 8;
419 }
420 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
421 DAG.getConstant(Amt, VAList.getValueType()));
422 }
423 return std::make_pair(Result, Chain);
424}
425
426
427std::pair<SDOperand, SDOperand> PPC64TargetLowering::
428LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
429 SelectionDAG &DAG) {
430 assert(0 && "LowerFrameReturnAddress unimplemented");
431 abort();
432}
433
434namespace {
435Statistic<>NotLogic("ppc-codegen", "Number of inverted logical ops");
436Statistic<>FusedFP("ppc-codegen", "Number of fused fp operations");
437//===--------------------------------------------------------------------===//
438/// ISel - PPC32 specific code to select PPC32 machine instructions for
439/// SelectionDAG operations.
440//===--------------------------------------------------------------------===//
441class ISel : public SelectionDAGISel {
442
443 /// Comment Here.
444 PPC64TargetLowering PPC64Lowering;
445
446 /// ExprMap - As shared expressions are codegen'd, we keep track of which
447 /// vreg the value is produced in, so we only emit one copy of each compiled
448 /// tree.
449 std::map<SDOperand, unsigned> ExprMap;
450
451 unsigned GlobalBaseReg;
452 bool GlobalBaseInitialized;
453
454public:
455 ISel(TargetMachine &TM) : SelectionDAGISel(PPC64Lowering), PPC64Lowering(TM)
456 {}
457
458 /// runOnFunction - Override this function in order to reset our per-function
459 /// variables.
460 virtual bool runOnFunction(Function &Fn) {
461 // Make sure we re-emit a set of the global base reg if necessary
462 GlobalBaseInitialized = false;
463 return SelectionDAGISel::runOnFunction(Fn);
464 }
465
466 /// InstructionSelectBasicBlock - This callback is invoked by
467 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
468 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
469 DEBUG(BB->dump());
470 // Codegen the basic block.
471 Select(DAG.getRoot());
472
473 // Clear state used for selection.
474 ExprMap.clear();
475 }
476
477 unsigned getGlobalBaseReg();
478 unsigned getConstDouble(double floatVal, unsigned Result);
479 unsigned SelectSetCR0(SDOperand CC);
480 unsigned SelectExpr(SDOperand N);
481 unsigned SelectExprFP(SDOperand N, unsigned Result);
482 void Select(SDOperand N);
483
484 bool SelectAddr(SDOperand N, unsigned& Reg, int& offset);
485 void SelectBranchCC(SDOperand N);
486};
487
488/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
489/// returns zero when the input is not exactly a power of two.
490static unsigned ExactLog2(unsigned Val) {
491 if (Val == 0 || (Val & (Val-1))) return 0;
492 unsigned Count = 0;
493 while (Val != 1) {
494 Val >>= 1;
495 ++Count;
496 }
497 return Count;
498}
499
500/// getImmediateForOpcode - This method returns a value indicating whether
501/// the ConstantSDNode N can be used as an immediate to Opcode. The return
502/// values are either 0, 1 or 2. 0 indicates that either N is not a
503/// ConstantSDNode, or is not suitable for use by that opcode. A return value
504/// of 1 indicates that the constant may be used in normal immediate form. A
505/// return value of 2 indicates that the constant may be used in shifted
506/// immediate form. A return value of 3 indicates that log base 2 of the
507/// constant may be used.
508///
509static unsigned getImmediateForOpcode(SDOperand N, unsigned Opcode,
510 unsigned& Imm, bool U = false) {
511 if (N.getOpcode() != ISD::Constant) return 0;
512
513 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
514
515 switch(Opcode) {
516 default: return 0;
517 case ISD::ADD:
518 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
519 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
520 break;
521 case ISD::AND:
522 case ISD::XOR:
523 case ISD::OR:
524 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
525 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
526 break;
527 case ISD::MUL:
528 case ISD::SUB:
529 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
530 break;
531 case ISD::SETCC:
532 if (U && (v >= 0 && v <= 65535)) { Imm = v & 0xFFFF; return 1; }
533 if (!U && (v <= 32767 && v >= -32768)) { Imm = v & 0xFFFF; return 1; }
534 break;
535 case ISD::SDIV:
536 if ((Imm = ExactLog2(v))) { return 3; }
537 break;
538 }
539 return 0;
540}
541
542/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
543/// to Condition. If the Condition is unordered or unsigned, the bool argument
544/// U is set to true, otherwise it is set to false.
545static unsigned getBCCForSetCC(unsigned Condition, bool& U) {
546 U = false;
547 switch (Condition) {
548 default: assert(0 && "Unknown condition!"); abort();
549 case ISD::SETEQ: return PPC::BEQ;
550 case ISD::SETNE: return PPC::BNE;
551 case ISD::SETULT: U = true;
552 case ISD::SETLT: return PPC::BLT;
553 case ISD::SETULE: U = true;
554 case ISD::SETLE: return PPC::BLE;
555 case ISD::SETUGT: U = true;
556 case ISD::SETGT: return PPC::BGT;
557 case ISD::SETUGE: U = true;
558 case ISD::SETGE: return PPC::BGE;
559 }
560 return 0;
561}
562
563/// IndexedOpForOp - Return the indexed variant for each of the PowerPC load
564/// and store immediate instructions.
565static unsigned IndexedOpForOp(unsigned Opcode) {
566 switch(Opcode) {
567 default: assert(0 && "Unknown opcode!"); abort();
568 case PPC::LBZ: return PPC::LBZX; case PPC::STB: return PPC::STBX;
569 case PPC::LHZ: return PPC::LHZX; case PPC::STH: return PPC::STHX;
570 case PPC::LHA: return PPC::LHAX; case PPC::STW: return PPC::STWX;
571 case PPC::LWZ: return PPC::LWZX; case PPC::STD: return PPC::STDX;
572 case PPC::LD: return PPC::LDX; case PPC::STFS: return PPC::STFSX;
573 case PPC::LFS: return PPC::LFSX; case PPC::STFD: return PPC::STFDX;
574 case PPC::LFD: return PPC::LFDX;
575 }
576 return 0;
577}
578}
579
580/// getGlobalBaseReg - Output the instructions required to put the
581/// base address to use for accessing globals into a register.
582///
583unsigned ISel::getGlobalBaseReg() {
584 if (!GlobalBaseInitialized) {
585 // Insert the set of GlobalBaseReg into the first MBB of the function
586 MachineBasicBlock &FirstMBB = BB->getParent()->front();
587 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
588 GlobalBaseReg = MakeReg(MVT::i64);
589 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
590 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR);
591 GlobalBaseInitialized = true;
592 }
593 return GlobalBaseReg;
594}
595
596/// getConstDouble - Loads a floating point value into a register, via the
597/// Constant Pool. Optionally takes a register in which to load the value.
598unsigned ISel::getConstDouble(double doubleVal, unsigned Result=0) {
599 unsigned Tmp1 = MakeReg(MVT::i32);
600 if (0 == Result) Result = MakeReg(MVT::f64);
601 MachineConstantPool *CP = BB->getParent()->getConstantPool();
602 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, doubleVal);
603 unsigned CPI = CP->getConstantPoolIndex(CFP);
604 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
605 .addConstantPoolIndex(CPI);
606 BuildMI(BB, PPC::LFD, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
607 return Result;
608}
609
610unsigned ISel::SelectSetCR0(SDOperand CC) {
611 unsigned Opc, Tmp1, Tmp2;
612 static const unsigned CompareOpcodes[] =
613 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
614
615 // If the first operand to the select is a SETCC node, then we can fold it
616 // into the branch that selects which value to return.
617 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
618 if (SetCC && CC.getOpcode() == ISD::SETCC) {
619 bool U;
620 Opc = getBCCForSetCC(SetCC->getCondition(), U);
621 Tmp1 = SelectExpr(SetCC->getOperand(0));
622
623 // Pass the optional argument U to getImmediateForOpcode for SETCC,
624 // so that it knows whether the SETCC immediate range is signed or not.
625 if (1 == getImmediateForOpcode(SetCC->getOperand(1), ISD::SETCC,
626 Tmp2, U)) {
627 if (U)
628 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(Tmp2);
629 else
630 BuildMI(BB, PPC::CMPWI, 2, PPC::CR0).addReg(Tmp1).addSImm(Tmp2);
631 } else {
632 bool IsInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
633 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
634 Tmp2 = SelectExpr(SetCC->getOperand(1));
635 BuildMI(BB, CompareOpc, 2, PPC::CR0).addReg(Tmp1).addReg(Tmp2);
636 }
637 } else {
638 Tmp1 = SelectExpr(CC);
639 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(0);
640 Opc = PPC::BNE;
641 }
642 return Opc;
643}
644
645/// Check to see if the load is a constant offset from a base register
646bool ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
647{
648 unsigned imm = 0, opcode = N.getOpcode();
649 if (N.getOpcode() == ISD::ADD) {
650 Reg = SelectExpr(N.getOperand(0));
651 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, imm)) {
652 offset = imm;
653 return false;
654 }
655 offset = SelectExpr(N.getOperand(1));
656 return true;
657 }
658 Reg = SelectExpr(N);
659 offset = 0;
660 return false;
661}
662
663void ISel::SelectBranchCC(SDOperand N)
664{
665 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
666 MachineBasicBlock *Dest =
667 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
668
669 // Get the MBB we will fall through to so that we can hand it off to the
670 // branch selection pass as an argument to the PPC::COND_BRANCH pseudo op.
671 //ilist<MachineBasicBlock>::iterator It = BB;
672 //MachineBasicBlock *Fallthrough = ++It;
673
674 Select(N.getOperand(0)); //chain
675 unsigned Opc = SelectSetCR0(N.getOperand(1));
676 // FIXME: Use this once we have something approximating two-way branches
677 // We cannot currently use this in case the ISel hands us something like
678 // BRcc MBBx
679 // BR MBBy
680 // since the fallthrough basic block for the conditional branch does not start
681 // with the unconditional branch (it is skipped over).
682 //BuildMI(BB, PPC::COND_BRANCH, 4).addReg(PPC::CR0).addImm(Opc)
683 // .addMBB(Dest).addMBB(Fallthrough);
684 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(Dest);
685 return;
686}
687
688unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
689{
690 unsigned Tmp1, Tmp2, Tmp3;
691 unsigned Opc = 0;
692 SDNode *Node = N.Val;
693 MVT::ValueType DestType = N.getValueType();
694 unsigned opcode = N.getOpcode();
695
696 switch (opcode) {
697 default:
698 Node->dump();
699 assert(0 && "Node not handled!\n");
700
701 case ISD::SELECT: {
702 // Attempt to generate FSEL. We can do this whenever we have an FP result,
703 // and an FP comparison in the SetCC node.
704 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val);
705 if (SetCC && N.getOperand(0).getOpcode() == ISD::SETCC &&
706 !MVT::isInteger(SetCC->getOperand(0).getValueType()) &&
707 SetCC->getCondition() != ISD::SETEQ &&
708 SetCC->getCondition() != ISD::SETNE) {
709 MVT::ValueType VT = SetCC->getOperand(0).getValueType();
710 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
711 unsigned TV = SelectExpr(N.getOperand(1)); // Use if TRUE
712 unsigned FV = SelectExpr(N.getOperand(2)); // Use if FALSE
713
714 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1));
715 if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) {
716 switch(SetCC->getCondition()) {
717 default: assert(0 && "Invalid FSEL condition"); abort();
718 case ISD::SETULT:
719 case ISD::SETLT:
720 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(FV).addReg(TV);
721 return Result;
722 case ISD::SETUGE:
723 case ISD::SETGE:
724 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV);
725 return Result;
726 case ISD::SETUGT:
727 case ISD::SETGT: {
728 Tmp2 = MakeReg(VT);
729 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
730 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(FV).addReg(TV);
731 return Result;
732 }
733 case ISD::SETULE:
734 case ISD::SETLE: {
735 Tmp2 = MakeReg(VT);
736 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
737 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV);
738 return Result;
739 }
740 }
741 } else {
742 Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS;
743 Tmp2 = SelectExpr(SetCC->getOperand(1));
744 Tmp3 = MakeReg(VT);
745 switch(SetCC->getCondition()) {
746 default: assert(0 && "Invalid FSEL condition"); abort();
747 case ISD::SETULT:
748 case ISD::SETLT:
749 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
750 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
751 return Result;
752 case ISD::SETUGE:
753 case ISD::SETGE:
754 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
755 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
756 return Result;
757 case ISD::SETUGT:
758 case ISD::SETGT:
759 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
760 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
761 return Result;
762 case ISD::SETULE:
763 case ISD::SETLE:
764 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
765 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
766 return Result;
767 }
768 }
769 assert(0 && "Should never get here");
770 return 0;
771 }
772
773 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
774 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
775 Opc = SelectSetCR0(N.getOperand(0));
776
777 // Create an iterator with which to insert the MBB for copying the false
778 // value and the MBB to hold the PHI instruction for this SetCC.
779 MachineBasicBlock *thisMBB = BB;
780 const BasicBlock *LLVM_BB = BB->getBasicBlock();
781 ilist<MachineBasicBlock>::iterator It = BB;
782 ++It;
783
784 // thisMBB:
785 // ...
786 // TrueVal = ...
787 // cmpTY cr0, r1, r2
788 // bCC copy1MBB
789 // fallthrough --> copy0MBB
790 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
791 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
792 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
793 MachineFunction *F = BB->getParent();
794 F->getBasicBlockList().insert(It, copy0MBB);
795 F->getBasicBlockList().insert(It, sinkMBB);
796 // Update machine-CFG edges
797 BB->addSuccessor(copy0MBB);
798 BB->addSuccessor(sinkMBB);
799
800 // copy0MBB:
801 // %FalseValue = ...
802 // # fallthrough to sinkMBB
803 BB = copy0MBB;
804 // Update machine-CFG edges
805 BB->addSuccessor(sinkMBB);
806
807 // sinkMBB:
808 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
809 // ...
810 BB = sinkMBB;
811 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
812 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
813 return Result;
814 }
815
816 case ISD::FNEG:
817 if (!NoExcessFPPrecision &&
818 ISD::ADD == N.getOperand(0).getOpcode() &&
819 N.getOperand(0).Val->hasOneUse() &&
820 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
821 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
822 ++FusedFP; // Statistic
823 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
824 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
825 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
826 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
827 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
828 } else if (!NoExcessFPPrecision &&
829 ISD::SUB == N.getOperand(0).getOpcode() &&
830 N.getOperand(0).Val->hasOneUse() &&
831 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
832 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
833 ++FusedFP; // Statistic
834 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
835 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
836 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
837 Opc = DestType == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS;
838 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
839 } else if (ISD::FABS == N.getOperand(0).getOpcode()) {
840 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
841 BuildMI(BB, PPC::FNABS, 1, Result).addReg(Tmp1);
842 } else {
843 Tmp1 = SelectExpr(N.getOperand(0));
844 BuildMI(BB, PPC::FNEG, 1, Result).addReg(Tmp1);
845 }
846 return Result;
847
848 case ISD::FABS:
849 Tmp1 = SelectExpr(N.getOperand(0));
850 BuildMI(BB, PPC::FABS, 1, Result).addReg(Tmp1);
851 return Result;
852
853 case ISD::FP_ROUND:
854 assert (DestType == MVT::f32 &&
855 N.getOperand(0).getValueType() == MVT::f64 &&
856 "only f64 to f32 conversion supported here");
857 Tmp1 = SelectExpr(N.getOperand(0));
858 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
859 return Result;
860
861 case ISD::FP_EXTEND:
862 assert (DestType == MVT::f64 &&
863 N.getOperand(0).getValueType() == MVT::f32 &&
864 "only f32 to f64 conversion supported here");
865 Tmp1 = SelectExpr(N.getOperand(0));
866 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
867 return Result;
868
869 case ISD::CopyFromReg:
870 if (Result == 1)
871 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
872 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
873 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
874 return Result;
875
876 case ISD::ConstantFP: {
877 ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
878 Result = getConstDouble(CN->getValue(), Result);
879 return Result;
880 }
881
882 case ISD::ADD:
883 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
884 N.getOperand(0).Val->hasOneUse()) {
885 ++FusedFP; // Statistic
886 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
887 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
888 Tmp3 = SelectExpr(N.getOperand(1));
889 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
890 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
891 return Result;
892 }
893 Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS;
894 Tmp1 = SelectExpr(N.getOperand(0));
895 Tmp2 = SelectExpr(N.getOperand(1));
896 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
897 return Result;
898
899 case ISD::SUB:
900 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
901 N.getOperand(0).Val->hasOneUse()) {
902 ++FusedFP; // Statistic
903 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
904 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
905 Tmp3 = SelectExpr(N.getOperand(1));
906 Opc = DestType == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS;
907 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
908 return Result;
909 }
910 Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS;
911 Tmp1 = SelectExpr(N.getOperand(0));
912 Tmp2 = SelectExpr(N.getOperand(1));
913 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
914 return Result;
915
916 case ISD::MUL:
917 case ISD::SDIV:
918 switch( opcode ) {
919 case ISD::MUL: Opc = DestType == MVT::f64 ? PPC::FMUL : PPC::FMULS; break;
920 case ISD::SDIV: Opc = DestType == MVT::f64 ? PPC::FDIV : PPC::FDIVS; break;
921 };
922 Tmp1 = SelectExpr(N.getOperand(0));
923 Tmp2 = SelectExpr(N.getOperand(1));
924 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
925 return Result;
926
927 case ISD::UINT_TO_FP:
928 case ISD::SINT_TO_FP: {
929 bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
930 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
931 Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into
932 Tmp3 = MakeReg(MVT::i64); // temp reg to hold the conversion constant
933 unsigned ConstF = MakeReg(MVT::f64); // temp reg to hold the fp constant
934
935 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
936 MachineConstantPool *CP = BB->getParent()->getConstantPool();
937
938 // FIXME: pull this FP constant generation stuff out into something like
939 // the simple ISel's getReg.
940 if (IsUnsigned) {
941 addFrameReference(BuildMI(BB, PPC::STD, 3).addReg(Tmp1), FrameIdx);
942 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
943 BuildMI(BB, PPC::FCFID, 1, Result).addReg(Tmp2);
944 } else {
945 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000008p52);
946 unsigned CPI = CP->getConstantPoolIndex(CFP);
947 // Load constant fp value
948 unsigned Tmp4 = MakeReg(MVT::i32);
949 unsigned TmpL = MakeReg(MVT::i32);
950 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg())
951 .addConstantPoolIndex(CPI);
952 BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
953 // Store the hi & low halves of the fp value, currently in int regs
954 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
955 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
956 BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
957 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
958 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
959 // Generate the return value with a subtract
960 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
961 }
962 return Result;
963 }
964 }
965 assert(0 && "Should never get here");
966 return 0;
967}
968
969unsigned ISel::SelectExpr(SDOperand N) {
970 unsigned Result;
971 unsigned Tmp1, Tmp2, Tmp3;
972 unsigned Opc = 0;
973 unsigned opcode = N.getOpcode();
974
975 SDNode *Node = N.Val;
976 MVT::ValueType DestType = N.getValueType();
977
978 unsigned &Reg = ExprMap[N];
979 if (Reg) return Reg;
980
981 switch (N.getOpcode()) {
982 default:
983 Reg = Result = (N.getValueType() != MVT::Other) ?
984 MakeReg(N.getValueType()) : 1;
985 break;
986 case ISD::CALL:
987 // If this is a call instruction, make sure to prepare ALL of the result
988 // values as well as the chain.
989 if (Node->getNumValues() == 1)
990 Reg = Result = 1; // Void call, just a chain.
991 else {
992 Result = MakeReg(Node->getValueType(0));
993 ExprMap[N.getValue(0)] = Result;
994 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
995 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
996 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
997 }
998 break;
999 }
1000
1001 if (ISD::CopyFromReg == opcode)
1002 DestType = N.getValue(0).getValueType();
1003
1004 if (DestType == MVT::f64 || DestType == MVT::f32)
1005 if (ISD::LOAD != opcode && ISD::EXTLOAD != opcode && ISD::UNDEF != opcode)
1006 return SelectExprFP(N, Result);
1007
1008 switch (opcode) {
1009 default:
1010 Node->dump();
1011 assert(0 && "Node not handled!\n");
1012 case ISD::UNDEF:
1013 BuildMI(BB, PPC::IMPLICIT_DEF, 0, Result);
1014 return Result;
1015 case ISD::DYNAMIC_STACKALLOC:
1016 // Generate both result values. FIXME: Need a better commment here?
1017 if (Result != 1)
1018 ExprMap[N.getValue(1)] = 1;
1019 else
1020 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1021
1022 // FIXME: We are currently ignoring the requested alignment for handling
1023 // greater than the stack alignment. This will need to be revisited at some
1024 // point. Align = N.getOperand(2);
1025 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1026 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1027 std::cerr << "Cannot allocate stack object with greater alignment than"
1028 << " the stack alignment yet!";
1029 abort();
1030 }
1031 Select(N.getOperand(0));
1032 Tmp1 = SelectExpr(N.getOperand(1));
1033 // Subtract size from stack pointer, thereby allocating some space.
1034 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
1035 // Put a pointer to the space into the result register by copying the SP
1036 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
1037 return Result;
1038
1039 case ISD::ConstantPool:
1040 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1041 Tmp2 = MakeReg(MVT::i64);
1042 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp2).addReg(getGlobalBaseReg())
1043 .addConstantPoolIndex(Tmp1);
1044 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
1045 return Result;
1046
1047 case ISD::FrameIndex:
1048 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
1049 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
1050 return Result;
1051
1052 case ISD::GlobalAddress: {
1053 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1054 Tmp1 = MakeReg(MVT::i64);
1055 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1056 .addGlobalAddress(GV);
1057 if (GV->hasWeakLinkage() || GV->isExternal()) {
1058 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
1059 } else {
1060 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
1061 }
1062 return Result;
1063 }
1064
1065 case ISD::LOAD:
1066 case ISD::EXTLOAD:
1067 case ISD::ZEXTLOAD:
1068 case ISD::SEXTLOAD: {
1069 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
1070 Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType();
1071 bool sext = (ISD::SEXTLOAD == opcode);
Nate Begemand3e6b942005-04-05 08:51:15 +00001072
1073 // Make sure we generate both values.
1074 if (Result != 1)
1075 ExprMap[N.getValue(1)] = 1; // Generate the token
1076 else
1077 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1078
1079 SDOperand Chain = N.getOperand(0);
1080 SDOperand Address = N.getOperand(1);
1081 Select(Chain);
1082
1083 switch (TypeBeingLoaded) {
1084 default: Node->dump(); assert(0 && "Cannot load this type!");
1085 case MVT::i1: Opc = PPC::LBZ; break;
1086 case MVT::i8: Opc = PPC::LBZ; break;
1087 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
1088 case MVT::i32: Opc = sext ? PPC::LWA : PPC::LWZ; break;
1089 case MVT::i64: Opc = PPC::LD; break;
1090 case MVT::f32: Opc = PPC::LFS; break;
1091 case MVT::f64: Opc = PPC::LFD; break;
1092 }
1093
1094 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1095 Tmp1 = MakeReg(MVT::i64);
1096 int CPI = CP->getIndex();
1097 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1098 .addConstantPoolIndex(CPI);
1099 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
1100 }
1101 else if(Address.getOpcode() == ISD::FrameIndex) {
1102 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
1103 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
1104 } else {
1105 int offset;
1106 bool idx = SelectAddr(Address, Tmp1, offset);
1107 if (idx) {
1108 Opc = IndexedOpForOp(Opc);
1109 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset);
1110 } else {
1111 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
1112 }
1113 }
1114 return Result;
1115 }
1116
1117 case ISD::CALL: {
1118 unsigned GPR_idx = 0, FPR_idx = 0;
1119 static const unsigned GPR[] = {
1120 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1121 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1122 };
1123 static const unsigned FPR[] = {
1124 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1125 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1126 };
1127
1128 // Lower the chain for this call.
1129 Select(N.getOperand(0));
1130 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
1131
1132 MachineInstr *CallMI;
1133 // Emit the correct call instruction based on the type of symbol called.
1134 if (GlobalAddressSDNode *GASD =
1135 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
1136 CallMI = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(),
1137 true);
1138 } else if (ExternalSymbolSDNode *ESSDN =
1139 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
1140 CallMI = BuildMI(PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(),
1141 true);
1142 } else {
1143 Tmp1 = SelectExpr(N.getOperand(1));
1144 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
1145 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
1146 CallMI = BuildMI(PPC::CALLindirect, 3).addImm(20).addImm(0)
1147 .addReg(PPC::R12);
1148 }
1149
1150 // Load the register args to virtual regs
1151 std::vector<unsigned> ArgVR;
1152 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
1153 ArgVR.push_back(SelectExpr(N.getOperand(i)));
1154
1155 // Copy the virtual registers into the appropriate argument register
1156 for(int i = 0, e = ArgVR.size(); i < e; ++i) {
1157 switch(N.getOperand(i+2).getValueType()) {
1158 default: Node->dump(); assert(0 && "Unknown value type for call");
1159 case MVT::i1:
1160 case MVT::i8:
1161 case MVT::i16:
1162 case MVT::i32:
1163 case MVT::i64:
1164 assert(GPR_idx < 8 && "Too many int args");
1165 if (N.getOperand(i+2).getOpcode() != ISD::UNDEF) {
1166 BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]);
1167 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1168 }
1169 ++GPR_idx;
1170 break;
1171 case MVT::f64:
1172 case MVT::f32:
1173 assert(FPR_idx < 13 && "Too many fp args");
1174 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]);
1175 CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use);
1176 ++FPR_idx;
1177 break;
1178 }
1179 }
1180
1181 // Put the call instruction in the correct place in the MachineBasicBlock
1182 BB->push_back(CallMI);
1183
1184 switch (Node->getValueType(0)) {
1185 default: assert(0 && "Unknown value type for call result!");
1186 case MVT::Other: return 1;
1187 case MVT::i1:
1188 case MVT::i8:
1189 case MVT::i16:
1190 case MVT::i32:
1191 case MVT::i64:
1192 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
1193 break;
1194 case MVT::f32:
1195 case MVT::f64:
1196 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1197 break;
1198 }
1199 return Result+N.ResNo;
1200 }
1201
1202 case ISD::SIGN_EXTEND:
1203 case ISD::SIGN_EXTEND_INREG:
1204 Tmp1 = SelectExpr(N.getOperand(0));
1205 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1206 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
1207 case MVT::i32:
1208 BuildMI(BB, PPC::EXTSW, 1, Result).addReg(Tmp1);
1209 break;
1210 case MVT::i16:
1211 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
1212 break;
1213 case MVT::i8:
1214 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
1215 break;
1216 case MVT::i1:
1217 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1218 break;
1219 }
1220 return Result;
1221
1222 case ISD::ZERO_EXTEND_INREG:
1223 Tmp1 = SelectExpr(N.getOperand(0));
1224 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1225 default: Node->dump(); assert(0 && "Unhandled ZERO_EXTEND type"); break;
1226 case MVT::i16: Tmp2 = 16; break;
1227 case MVT::i8: Tmp2 = 24; break;
1228 case MVT::i1: Tmp2 = 31; break;
1229 }
1230 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(0).addImm(Tmp2)
1231 .addImm(31);
1232 return Result;
1233
1234 case ISD::CopyFromReg:
1235 if (Result == 1)
1236 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1237 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1238 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1239 return Result;
1240
1241 case ISD::SHL:
1242 Tmp1 = SelectExpr(N.getOperand(0));
1243 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1244 Tmp2 = CN->getValue() & 0x1F;
1245 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
1246 .addImm(31-Tmp2);
1247 } else {
1248 Tmp2 = SelectExpr(N.getOperand(1));
1249 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1250 }
1251 return Result;
1252
1253 case ISD::SRL:
1254 Tmp1 = SelectExpr(N.getOperand(0));
1255 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1256 Tmp2 = CN->getValue() & 0x1F;
1257 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2)
1258 .addImm(Tmp2).addImm(31);
1259 } else {
1260 Tmp2 = SelectExpr(N.getOperand(1));
1261 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1262 }
1263 return Result;
1264
1265 case ISD::SRA:
1266 Tmp1 = SelectExpr(N.getOperand(0));
1267 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1268 Tmp2 = CN->getValue() & 0x1F;
1269 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1270 } else {
1271 Tmp2 = SelectExpr(N.getOperand(1));
1272 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1273 }
1274 return Result;
1275
1276 case ISD::ADD:
1277 Tmp1 = SelectExpr(N.getOperand(0));
1278 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1279 default: assert(0 && "unhandled result code");
1280 case 0: // No immediate
1281 Tmp2 = SelectExpr(N.getOperand(1));
1282 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1283 break;
1284 case 1: // Low immediate
1285 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1286 break;
1287 case 2: // Shifted immediate
1288 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1289 break;
1290 }
1291 return Result;
1292
1293 case ISD::AND:
1294 case ISD::OR:
1295 Tmp1 = SelectExpr(N.getOperand(0));
1296 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1297 default: assert(0 && "unhandled result code");
1298 case 0: // No immediate
1299 Tmp2 = SelectExpr(N.getOperand(1));
1300 switch (opcode) {
1301 case ISD::AND: Opc = PPC::AND; break;
1302 case ISD::OR: Opc = PPC::OR; break;
1303 }
1304 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1305 break;
1306 case 1: // Low immediate
1307 switch (opcode) {
1308 case ISD::AND: Opc = PPC::ANDIo; break;
1309 case ISD::OR: Opc = PPC::ORI; break;
1310 }
1311 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1312 break;
1313 case 2: // Shifted immediate
1314 switch (opcode) {
1315 case ISD::AND: Opc = PPC::ANDISo; break;
1316 case ISD::OR: Opc = PPC::ORIS; break;
1317 }
1318 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1319 break;
1320 }
1321 return Result;
1322
1323 case ISD::XOR: {
1324 // Check for EQV: xor, (xor a, -1), b
1325 if (N.getOperand(0).getOpcode() == ISD::XOR &&
1326 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1327 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) {
1328 ++NotLogic;
1329 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1330 Tmp2 = SelectExpr(N.getOperand(1));
1331 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1332 return Result;
1333 }
1334 // Check for NOT, NOR, and NAND: xor (copy, or, and), -1
1335 if (N.getOperand(1).getOpcode() == ISD::Constant &&
1336 cast<ConstantSDNode>(N.getOperand(1))->isAllOnesValue()) {
1337 ++NotLogic;
1338 switch(N.getOperand(0).getOpcode()) {
1339 case ISD::OR:
1340 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1341 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1342 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1343 break;
1344 case ISD::AND:
1345 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1346 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1347 BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2);
1348 break;
1349 default:
1350 Tmp1 = SelectExpr(N.getOperand(0));
1351 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1352 break;
1353 }
1354 return Result;
1355 }
1356 Tmp1 = SelectExpr(N.getOperand(0));
1357 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1358 default: assert(0 && "unhandled result code");
1359 case 0: // No immediate
1360 Tmp2 = SelectExpr(N.getOperand(1));
1361 BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1362 break;
1363 case 1: // Low immediate
1364 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1365 break;
1366 case 2: // Shifted immediate
1367 BuildMI(BB, PPC::XORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
1368 break;
1369 }
1370 return Result;
1371 }
1372
1373 case ISD::SUB:
1374 Tmp2 = SelectExpr(N.getOperand(1));
1375 if (1 == getImmediateForOpcode(N.getOperand(0), opcode, Tmp1))
1376 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1);
1377 else {
1378 Tmp1 = SelectExpr(N.getOperand(0));
1379 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
1380 }
1381 return Result;
1382
1383 case ISD::MUL:
1384 Tmp1 = SelectExpr(N.getOperand(0));
1385 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
1386 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1387 else {
1388 Tmp2 = SelectExpr(N.getOperand(1));
1389 BuildMI(BB, PPC::MULLD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1390 }
1391 return Result;
1392
1393 case ISD::SDIV:
1394 case ISD::UDIV:
1395 if (3 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp3)) {
1396 Tmp1 = MakeReg(MVT::i64);
1397 Tmp2 = SelectExpr(N.getOperand(0));
1398 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
1399 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp1);
1400 return Result;
1401 }
1402 Tmp1 = SelectExpr(N.getOperand(0));
1403 Tmp2 = SelectExpr(N.getOperand(1));
1404 Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW;
1405 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1406 return Result;
1407
1408 case ISD::UREM:
1409 case ISD::SREM: {
1410 Tmp1 = SelectExpr(N.getOperand(0));
1411 Tmp2 = SelectExpr(N.getOperand(1));
1412 Tmp3 = MakeReg(MVT::i64);
1413 unsigned Tmp4 = MakeReg(MVT::i64);
1414 Opc = (ISD::UREM == opcode) ? PPC::DIVDU : PPC::DIVD;
1415 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1416 BuildMI(BB, PPC::MULLD, 2, Tmp4).addReg(Tmp3).addReg(Tmp2);
1417 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp4).addReg(Tmp1);
1418 return Result;
1419 }
1420
1421 case ISD::FP_TO_UINT:
1422 case ISD::FP_TO_SINT: {
Nate Begemand3e6b942005-04-05 08:51:15 +00001423 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begemana3829d52005-04-05 17:32:30 +00001424 Tmp2 = MakeReg(MVT::f64);
1425 BuildMI(BB, PPC::FCTIDZ, 1, Tmp2).addReg(Tmp1);
1426 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1427 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx);
1428 addFrameReference(BuildMI(BB, PPC::LD, 2, Result), FrameIdx);
1429 return Result;
Nate Begemand3e6b942005-04-05 08:51:15 +00001430 }
1431
1432 case ISD::SETCC:
1433 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1434 Opc = SelectSetCR0(N);
1435
1436 unsigned TrueValue = MakeReg(MVT::i32);
1437 BuildMI(BB, PPC::LI, 1, TrueValue).addSImm(1);
1438 unsigned FalseValue = MakeReg(MVT::i32);
1439 BuildMI(BB, PPC::LI, 1, FalseValue).addSImm(0);
1440
1441 // Create an iterator with which to insert the MBB for copying the false
1442 // value and the MBB to hold the PHI instruction for this SetCC.
1443 MachineBasicBlock *thisMBB = BB;
1444 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1445 ilist<MachineBasicBlock>::iterator It = BB;
1446 ++It;
1447
1448 // thisMBB:
1449 // ...
1450 // cmpTY cr0, r1, r2
1451 // %TrueValue = li 1
1452 // bCC sinkMBB
1453 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1454 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1455 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
1456 MachineFunction *F = BB->getParent();
1457 F->getBasicBlockList().insert(It, copy0MBB);
1458 F->getBasicBlockList().insert(It, sinkMBB);
1459 // Update machine-CFG edges
1460 BB->addSuccessor(copy0MBB);
1461 BB->addSuccessor(sinkMBB);
1462
1463 // copy0MBB:
1464 // %FalseValue = li 0
1465 // fallthrough
1466 BB = copy0MBB;
1467 // Update machine-CFG edges
1468 BB->addSuccessor(sinkMBB);
1469
1470 // sinkMBB:
1471 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1472 // ...
1473 BB = sinkMBB;
1474 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1475 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1476 return Result;
1477 }
1478 assert(0 && "Is this legal?");
1479 return 0;
1480
1481 case ISD::SELECT: {
1482 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
1483 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
1484 Opc = SelectSetCR0(N.getOperand(0));
1485
1486 // Create an iterator with which to insert the MBB for copying the false
1487 // value and the MBB to hold the PHI instruction for this SetCC.
1488 MachineBasicBlock *thisMBB = BB;
1489 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1490 ilist<MachineBasicBlock>::iterator It = BB;
1491 ++It;
1492
1493 // thisMBB:
1494 // ...
1495 // TrueVal = ...
1496 // cmpTY cr0, r1, r2
1497 // bCC copy1MBB
1498 // fallthrough --> copy0MBB
1499 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1500 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1501 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
1502 MachineFunction *F = BB->getParent();
1503 F->getBasicBlockList().insert(It, copy0MBB);
1504 F->getBasicBlockList().insert(It, sinkMBB);
1505 // Update machine-CFG edges
1506 BB->addSuccessor(copy0MBB);
1507 BB->addSuccessor(sinkMBB);
1508
1509 // copy0MBB:
1510 // %FalseValue = ...
1511 // # fallthrough to sinkMBB
1512 BB = copy0MBB;
1513 // Update machine-CFG edges
1514 BB->addSuccessor(sinkMBB);
1515
1516 // sinkMBB:
1517 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1518 // ...
1519 BB = sinkMBB;
1520 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1521 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1522
1523 // FIXME: Select i64?
1524 return Result;
1525 }
1526
1527 case ISD::Constant:
1528 switch (N.getValueType()) {
1529 default: assert(0 && "Cannot use constants of this type!");
1530 case MVT::i1:
1531 BuildMI(BB, PPC::LI, 1, Result)
1532 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
1533 break;
1534 case MVT::i32:
1535 {
1536 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
1537 if (v < 32768 && v >= -32768) {
1538 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
1539 } else {
1540 Tmp1 = MakeReg(MVT::i32);
1541 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
1542 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
1543 }
1544 }
1545 }
1546 return Result;
1547 }
1548
1549 return 0;
1550}
1551
1552void ISel::Select(SDOperand N) {
1553 unsigned Tmp1, Tmp2, Opc;
1554 unsigned opcode = N.getOpcode();
1555
1556 if (!ExprMap.insert(std::make_pair(N, 1)).second)
1557 return; // Already selected.
1558
1559 SDNode *Node = N.Val;
1560
1561 switch (Node->getOpcode()) {
1562 default:
1563 Node->dump(); std::cerr << "\n";
1564 assert(0 && "Node not handled yet!");
1565 case ISD::EntryToken: return; // Noop
1566 case ISD::TokenFactor:
1567 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1568 Select(Node->getOperand(i));
1569 return;
1570 case ISD::ADJCALLSTACKDOWN:
1571 case ISD::ADJCALLSTACKUP:
1572 Select(N.getOperand(0));
1573 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1574 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? PPC::ADJCALLSTACKDOWN :
1575 PPC::ADJCALLSTACKUP;
1576 BuildMI(BB, Opc, 1).addImm(Tmp1);
1577 return;
1578 case ISD::BR: {
1579 MachineBasicBlock *Dest =
1580 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1581 Select(N.getOperand(0));
1582 BuildMI(BB, PPC::B, 1).addMBB(Dest);
1583 return;
1584 }
1585 case ISD::BRCOND:
1586 SelectBranchCC(N);
1587 return;
1588 case ISD::CopyToReg:
1589 Select(N.getOperand(0));
1590 Tmp1 = SelectExpr(N.getOperand(1));
1591 Tmp2 = cast<RegSDNode>(N)->getReg();
1592
1593 if (Tmp1 != Tmp2) {
1594 if (N.getOperand(1).getValueType() == MVT::f64 ||
1595 N.getOperand(1).getValueType() == MVT::f32)
1596 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
1597 else
1598 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1599 }
1600 return;
1601 case ISD::ImplicitDef:
1602 Select(N.getOperand(0));
1603 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
1604 return;
1605 case ISD::RET:
1606 switch (N.getNumOperands()) {
1607 default:
1608 assert(0 && "Unknown return instruction!");
1609 case 3:
1610 assert(N.getOperand(1).getValueType() == MVT::i32 &&
1611 N.getOperand(2).getValueType() == MVT::i32 &&
1612 "Unknown two-register value!");
1613 Select(N.getOperand(0));
1614 Tmp1 = SelectExpr(N.getOperand(1));
1615 Tmp2 = SelectExpr(N.getOperand(2));
1616 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2);
1617 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1);
1618 break;
1619 case 2:
1620 Select(N.getOperand(0));
1621 Tmp1 = SelectExpr(N.getOperand(1));
1622 switch (N.getOperand(1).getValueType()) {
1623 default:
1624 assert(0 && "Unknown return type!");
1625 case MVT::f64:
1626 case MVT::f32:
1627 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
1628 break;
1629 case MVT::i32:
1630 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
1631 break;
1632 }
1633 case 1:
1634 Select(N.getOperand(0));
1635 break;
1636 }
1637 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
1638 return;
1639 case ISD::TRUNCSTORE:
1640 case ISD::STORE:
1641 {
1642 SDOperand Chain = N.getOperand(0);
1643 SDOperand Value = N.getOperand(1);
1644 SDOperand Address = N.getOperand(2);
1645 Select(Chain);
1646
1647 Tmp1 = SelectExpr(Value); //value
1648
1649 if (opcode == ISD::STORE) {
1650 switch(Value.getValueType()) {
1651 default: assert(0 && "unknown Type in store");
1652 case MVT::i64: Opc = PPC::STD; break;
1653 case MVT::f64: Opc = PPC::STFD; break;
1654 case MVT::f32: Opc = PPC::STFS; break;
1655 }
1656 } else { //ISD::TRUNCSTORE
1657 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1658 default: assert(0 && "unknown Type in store");
1659 case MVT::i1: //FIXME: DAG does not promote this load
1660 case MVT::i8: Opc= PPC::STB; break;
1661 case MVT::i16: Opc = PPC::STH; break;
1662 case MVT::i32: Opc = PPC::STW; break;
1663 }
1664 }
1665
1666 if(Address.getOpcode() == ISD::FrameIndex)
1667 {
1668 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
1669 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
1670 }
1671 else
1672 {
1673 int offset;
1674 bool idx = SelectAddr(Address, Tmp2, offset);
1675 if (idx) {
1676 Opc = IndexedOpForOp(Opc);
1677 BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset);
1678 } else {
1679 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1680 }
1681 }
1682 return;
1683 }
1684 case ISD::EXTLOAD:
1685 case ISD::SEXTLOAD:
1686 case ISD::ZEXTLOAD:
1687 case ISD::LOAD:
1688 case ISD::CopyFromReg:
1689 case ISD::CALL:
1690 case ISD::DYNAMIC_STACKALLOC:
1691 ExprMap.erase(N);
1692 SelectExpr(N);
1693 return;
1694 }
1695 assert(0 && "Should not be reached!");
1696}
1697
1698
1699/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
1700/// into a machine code representation using pattern matching and a machine
1701/// description file.
1702///
1703FunctionPass *llvm::createPPC64ISelPattern(TargetMachine &TM) {
1704 return new ISel(TM);
1705}
1706