blob: 5f5ba5c0ce60aedf8a62ff35d0288bca51ac5ad2 [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);
1072 bool byte = (MVT::i8 == TypeBeingLoaded);
1073
1074 // Make sure we generate both values.
1075 if (Result != 1)
1076 ExprMap[N.getValue(1)] = 1; // Generate the token
1077 else
1078 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1079
1080 SDOperand Chain = N.getOperand(0);
1081 SDOperand Address = N.getOperand(1);
1082 Select(Chain);
1083
1084 switch (TypeBeingLoaded) {
1085 default: Node->dump(); assert(0 && "Cannot load this type!");
1086 case MVT::i1: Opc = PPC::LBZ; break;
1087 case MVT::i8: Opc = PPC::LBZ; break;
1088 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
1089 case MVT::i32: Opc = sext ? PPC::LWA : PPC::LWZ; break;
1090 case MVT::i64: Opc = PPC::LD; break;
1091 case MVT::f32: Opc = PPC::LFS; break;
1092 case MVT::f64: Opc = PPC::LFD; break;
1093 }
1094
1095 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1096 Tmp1 = MakeReg(MVT::i64);
1097 int CPI = CP->getIndex();
1098 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1099 .addConstantPoolIndex(CPI);
1100 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
1101 }
1102 else if(Address.getOpcode() == ISD::FrameIndex) {
1103 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
1104 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
1105 } else {
1106 int offset;
1107 bool idx = SelectAddr(Address, Tmp1, offset);
1108 if (idx) {
1109 Opc = IndexedOpForOp(Opc);
1110 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset);
1111 } else {
1112 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
1113 }
1114 }
1115 return Result;
1116 }
1117
1118 case ISD::CALL: {
1119 unsigned GPR_idx = 0, FPR_idx = 0;
1120 static const unsigned GPR[] = {
1121 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1122 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1123 };
1124 static const unsigned FPR[] = {
1125 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1126 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1127 };
1128
1129 // Lower the chain for this call.
1130 Select(N.getOperand(0));
1131 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
1132
1133 MachineInstr *CallMI;
1134 // Emit the correct call instruction based on the type of symbol called.
1135 if (GlobalAddressSDNode *GASD =
1136 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
1137 CallMI = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(),
1138 true);
1139 } else if (ExternalSymbolSDNode *ESSDN =
1140 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
1141 CallMI = BuildMI(PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(),
1142 true);
1143 } else {
1144 Tmp1 = SelectExpr(N.getOperand(1));
1145 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
1146 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
1147 CallMI = BuildMI(PPC::CALLindirect, 3).addImm(20).addImm(0)
1148 .addReg(PPC::R12);
1149 }
1150
1151 // Load the register args to virtual regs
1152 std::vector<unsigned> ArgVR;
1153 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
1154 ArgVR.push_back(SelectExpr(N.getOperand(i)));
1155
1156 // Copy the virtual registers into the appropriate argument register
1157 for(int i = 0, e = ArgVR.size(); i < e; ++i) {
1158 switch(N.getOperand(i+2).getValueType()) {
1159 default: Node->dump(); assert(0 && "Unknown value type for call");
1160 case MVT::i1:
1161 case MVT::i8:
1162 case MVT::i16:
1163 case MVT::i32:
1164 case MVT::i64:
1165 assert(GPR_idx < 8 && "Too many int args");
1166 if (N.getOperand(i+2).getOpcode() != ISD::UNDEF) {
1167 BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]);
1168 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1169 }
1170 ++GPR_idx;
1171 break;
1172 case MVT::f64:
1173 case MVT::f32:
1174 assert(FPR_idx < 13 && "Too many fp args");
1175 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]);
1176 CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use);
1177 ++FPR_idx;
1178 break;
1179 }
1180 }
1181
1182 // Put the call instruction in the correct place in the MachineBasicBlock
1183 BB->push_back(CallMI);
1184
1185 switch (Node->getValueType(0)) {
1186 default: assert(0 && "Unknown value type for call result!");
1187 case MVT::Other: return 1;
1188 case MVT::i1:
1189 case MVT::i8:
1190 case MVT::i16:
1191 case MVT::i32:
1192 case MVT::i64:
1193 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
1194 break;
1195 case MVT::f32:
1196 case MVT::f64:
1197 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1198 break;
1199 }
1200 return Result+N.ResNo;
1201 }
1202
1203 case ISD::SIGN_EXTEND:
1204 case ISD::SIGN_EXTEND_INREG:
1205 Tmp1 = SelectExpr(N.getOperand(0));
1206 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1207 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
1208 case MVT::i32:
1209 BuildMI(BB, PPC::EXTSW, 1, Result).addReg(Tmp1);
1210 break;
1211 case MVT::i16:
1212 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
1213 break;
1214 case MVT::i8:
1215 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
1216 break;
1217 case MVT::i1:
1218 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1219 break;
1220 }
1221 return Result;
1222
1223 case ISD::ZERO_EXTEND_INREG:
1224 Tmp1 = SelectExpr(N.getOperand(0));
1225 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1226 default: Node->dump(); assert(0 && "Unhandled ZERO_EXTEND type"); break;
1227 case MVT::i16: Tmp2 = 16; break;
1228 case MVT::i8: Tmp2 = 24; break;
1229 case MVT::i1: Tmp2 = 31; break;
1230 }
1231 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(0).addImm(Tmp2)
1232 .addImm(31);
1233 return Result;
1234
1235 case ISD::CopyFromReg:
1236 if (Result == 1)
1237 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1238 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1239 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1240 return Result;
1241
1242 case ISD::SHL:
1243 Tmp1 = SelectExpr(N.getOperand(0));
1244 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1245 Tmp2 = CN->getValue() & 0x1F;
1246 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
1247 .addImm(31-Tmp2);
1248 } else {
1249 Tmp2 = SelectExpr(N.getOperand(1));
1250 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1251 }
1252 return Result;
1253
1254 case ISD::SRL:
1255 Tmp1 = SelectExpr(N.getOperand(0));
1256 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1257 Tmp2 = CN->getValue() & 0x1F;
1258 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2)
1259 .addImm(Tmp2).addImm(31);
1260 } else {
1261 Tmp2 = SelectExpr(N.getOperand(1));
1262 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1263 }
1264 return Result;
1265
1266 case ISD::SRA:
1267 Tmp1 = SelectExpr(N.getOperand(0));
1268 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1269 Tmp2 = CN->getValue() & 0x1F;
1270 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1271 } else {
1272 Tmp2 = SelectExpr(N.getOperand(1));
1273 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1274 }
1275 return Result;
1276
1277 case ISD::ADD:
1278 Tmp1 = SelectExpr(N.getOperand(0));
1279 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1280 default: assert(0 && "unhandled result code");
1281 case 0: // No immediate
1282 Tmp2 = SelectExpr(N.getOperand(1));
1283 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1284 break;
1285 case 1: // Low immediate
1286 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1287 break;
1288 case 2: // Shifted immediate
1289 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1290 break;
1291 }
1292 return Result;
1293
1294 case ISD::AND:
1295 case ISD::OR:
1296 Tmp1 = SelectExpr(N.getOperand(0));
1297 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1298 default: assert(0 && "unhandled result code");
1299 case 0: // No immediate
1300 Tmp2 = SelectExpr(N.getOperand(1));
1301 switch (opcode) {
1302 case ISD::AND: Opc = PPC::AND; break;
1303 case ISD::OR: Opc = PPC::OR; break;
1304 }
1305 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1306 break;
1307 case 1: // Low immediate
1308 switch (opcode) {
1309 case ISD::AND: Opc = PPC::ANDIo; break;
1310 case ISD::OR: Opc = PPC::ORI; break;
1311 }
1312 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1313 break;
1314 case 2: // Shifted immediate
1315 switch (opcode) {
1316 case ISD::AND: Opc = PPC::ANDISo; break;
1317 case ISD::OR: Opc = PPC::ORIS; break;
1318 }
1319 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1320 break;
1321 }
1322 return Result;
1323
1324 case ISD::XOR: {
1325 // Check for EQV: xor, (xor a, -1), b
1326 if (N.getOperand(0).getOpcode() == ISD::XOR &&
1327 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1328 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) {
1329 ++NotLogic;
1330 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1331 Tmp2 = SelectExpr(N.getOperand(1));
1332 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1333 return Result;
1334 }
1335 // Check for NOT, NOR, and NAND: xor (copy, or, and), -1
1336 if (N.getOperand(1).getOpcode() == ISD::Constant &&
1337 cast<ConstantSDNode>(N.getOperand(1))->isAllOnesValue()) {
1338 ++NotLogic;
1339 switch(N.getOperand(0).getOpcode()) {
1340 case ISD::OR:
1341 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1342 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1343 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1344 break;
1345 case ISD::AND:
1346 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1347 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1348 BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2);
1349 break;
1350 default:
1351 Tmp1 = SelectExpr(N.getOperand(0));
1352 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1353 break;
1354 }
1355 return Result;
1356 }
1357 Tmp1 = SelectExpr(N.getOperand(0));
1358 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1359 default: assert(0 && "unhandled result code");
1360 case 0: // No immediate
1361 Tmp2 = SelectExpr(N.getOperand(1));
1362 BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1363 break;
1364 case 1: // Low immediate
1365 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1366 break;
1367 case 2: // Shifted immediate
1368 BuildMI(BB, PPC::XORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
1369 break;
1370 }
1371 return Result;
1372 }
1373
1374 case ISD::SUB:
1375 Tmp2 = SelectExpr(N.getOperand(1));
1376 if (1 == getImmediateForOpcode(N.getOperand(0), opcode, Tmp1))
1377 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1);
1378 else {
1379 Tmp1 = SelectExpr(N.getOperand(0));
1380 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
1381 }
1382 return Result;
1383
1384 case ISD::MUL:
1385 Tmp1 = SelectExpr(N.getOperand(0));
1386 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
1387 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1388 else {
1389 Tmp2 = SelectExpr(N.getOperand(1));
1390 BuildMI(BB, PPC::MULLD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1391 }
1392 return Result;
1393
1394 case ISD::SDIV:
1395 case ISD::UDIV:
1396 if (3 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp3)) {
1397 Tmp1 = MakeReg(MVT::i64);
1398 Tmp2 = SelectExpr(N.getOperand(0));
1399 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
1400 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp1);
1401 return Result;
1402 }
1403 Tmp1 = SelectExpr(N.getOperand(0));
1404 Tmp2 = SelectExpr(N.getOperand(1));
1405 Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW;
1406 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1407 return Result;
1408
1409 case ISD::UREM:
1410 case ISD::SREM: {
1411 Tmp1 = SelectExpr(N.getOperand(0));
1412 Tmp2 = SelectExpr(N.getOperand(1));
1413 Tmp3 = MakeReg(MVT::i64);
1414 unsigned Tmp4 = MakeReg(MVT::i64);
1415 Opc = (ISD::UREM == opcode) ? PPC::DIVDU : PPC::DIVD;
1416 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1417 BuildMI(BB, PPC::MULLD, 2, Tmp4).addReg(Tmp3).addReg(Tmp2);
1418 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp4).addReg(Tmp1);
1419 return Result;
1420 }
1421
1422 case ISD::FP_TO_UINT:
1423 case ISD::FP_TO_SINT: {
Nate Begemand3e6b942005-04-05 08:51:15 +00001424 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begemana3829d52005-04-05 17:32:30 +00001425 Tmp2 = MakeReg(MVT::f64);
1426 BuildMI(BB, PPC::FCTIDZ, 1, Tmp2).addReg(Tmp1);
1427 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1428 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx);
1429 addFrameReference(BuildMI(BB, PPC::LD, 2, Result), FrameIdx);
1430 return Result;
Nate Begemand3e6b942005-04-05 08:51:15 +00001431 }
1432
1433 case ISD::SETCC:
1434 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1435 Opc = SelectSetCR0(N);
1436
1437 unsigned TrueValue = MakeReg(MVT::i32);
1438 BuildMI(BB, PPC::LI, 1, TrueValue).addSImm(1);
1439 unsigned FalseValue = MakeReg(MVT::i32);
1440 BuildMI(BB, PPC::LI, 1, FalseValue).addSImm(0);
1441
1442 // Create an iterator with which to insert the MBB for copying the false
1443 // value and the MBB to hold the PHI instruction for this SetCC.
1444 MachineBasicBlock *thisMBB = BB;
1445 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1446 ilist<MachineBasicBlock>::iterator It = BB;
1447 ++It;
1448
1449 // thisMBB:
1450 // ...
1451 // cmpTY cr0, r1, r2
1452 // %TrueValue = li 1
1453 // bCC sinkMBB
1454 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1455 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1456 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
1457 MachineFunction *F = BB->getParent();
1458 F->getBasicBlockList().insert(It, copy0MBB);
1459 F->getBasicBlockList().insert(It, sinkMBB);
1460 // Update machine-CFG edges
1461 BB->addSuccessor(copy0MBB);
1462 BB->addSuccessor(sinkMBB);
1463
1464 // copy0MBB:
1465 // %FalseValue = li 0
1466 // fallthrough
1467 BB = copy0MBB;
1468 // Update machine-CFG edges
1469 BB->addSuccessor(sinkMBB);
1470
1471 // sinkMBB:
1472 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1473 // ...
1474 BB = sinkMBB;
1475 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1476 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1477 return Result;
1478 }
1479 assert(0 && "Is this legal?");
1480 return 0;
1481
1482 case ISD::SELECT: {
1483 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
1484 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
1485 Opc = SelectSetCR0(N.getOperand(0));
1486
1487 // Create an iterator with which to insert the MBB for copying the false
1488 // value and the MBB to hold the PHI instruction for this SetCC.
1489 MachineBasicBlock *thisMBB = BB;
1490 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1491 ilist<MachineBasicBlock>::iterator It = BB;
1492 ++It;
1493
1494 // thisMBB:
1495 // ...
1496 // TrueVal = ...
1497 // cmpTY cr0, r1, r2
1498 // bCC copy1MBB
1499 // fallthrough --> copy0MBB
1500 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1501 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1502 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
1503 MachineFunction *F = BB->getParent();
1504 F->getBasicBlockList().insert(It, copy0MBB);
1505 F->getBasicBlockList().insert(It, sinkMBB);
1506 // Update machine-CFG edges
1507 BB->addSuccessor(copy0MBB);
1508 BB->addSuccessor(sinkMBB);
1509
1510 // copy0MBB:
1511 // %FalseValue = ...
1512 // # fallthrough to sinkMBB
1513 BB = copy0MBB;
1514 // Update machine-CFG edges
1515 BB->addSuccessor(sinkMBB);
1516
1517 // sinkMBB:
1518 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1519 // ...
1520 BB = sinkMBB;
1521 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1522 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1523
1524 // FIXME: Select i64?
1525 return Result;
1526 }
1527
1528 case ISD::Constant:
1529 switch (N.getValueType()) {
1530 default: assert(0 && "Cannot use constants of this type!");
1531 case MVT::i1:
1532 BuildMI(BB, PPC::LI, 1, Result)
1533 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
1534 break;
1535 case MVT::i32:
1536 {
1537 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
1538 if (v < 32768 && v >= -32768) {
1539 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
1540 } else {
1541 Tmp1 = MakeReg(MVT::i32);
1542 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
1543 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
1544 }
1545 }
1546 }
1547 return Result;
1548 }
1549
1550 return 0;
1551}
1552
1553void ISel::Select(SDOperand N) {
1554 unsigned Tmp1, Tmp2, Opc;
1555 unsigned opcode = N.getOpcode();
1556
1557 if (!ExprMap.insert(std::make_pair(N, 1)).second)
1558 return; // Already selected.
1559
1560 SDNode *Node = N.Val;
1561
1562 switch (Node->getOpcode()) {
1563 default:
1564 Node->dump(); std::cerr << "\n";
1565 assert(0 && "Node not handled yet!");
1566 case ISD::EntryToken: return; // Noop
1567 case ISD::TokenFactor:
1568 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1569 Select(Node->getOperand(i));
1570 return;
1571 case ISD::ADJCALLSTACKDOWN:
1572 case ISD::ADJCALLSTACKUP:
1573 Select(N.getOperand(0));
1574 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1575 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? PPC::ADJCALLSTACKDOWN :
1576 PPC::ADJCALLSTACKUP;
1577 BuildMI(BB, Opc, 1).addImm(Tmp1);
1578 return;
1579 case ISD::BR: {
1580 MachineBasicBlock *Dest =
1581 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1582 Select(N.getOperand(0));
1583 BuildMI(BB, PPC::B, 1).addMBB(Dest);
1584 return;
1585 }
1586 case ISD::BRCOND:
1587 SelectBranchCC(N);
1588 return;
1589 case ISD::CopyToReg:
1590 Select(N.getOperand(0));
1591 Tmp1 = SelectExpr(N.getOperand(1));
1592 Tmp2 = cast<RegSDNode>(N)->getReg();
1593
1594 if (Tmp1 != Tmp2) {
1595 if (N.getOperand(1).getValueType() == MVT::f64 ||
1596 N.getOperand(1).getValueType() == MVT::f32)
1597 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
1598 else
1599 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1600 }
1601 return;
1602 case ISD::ImplicitDef:
1603 Select(N.getOperand(0));
1604 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
1605 return;
1606 case ISD::RET:
1607 switch (N.getNumOperands()) {
1608 default:
1609 assert(0 && "Unknown return instruction!");
1610 case 3:
1611 assert(N.getOperand(1).getValueType() == MVT::i32 &&
1612 N.getOperand(2).getValueType() == MVT::i32 &&
1613 "Unknown two-register value!");
1614 Select(N.getOperand(0));
1615 Tmp1 = SelectExpr(N.getOperand(1));
1616 Tmp2 = SelectExpr(N.getOperand(2));
1617 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2);
1618 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1);
1619 break;
1620 case 2:
1621 Select(N.getOperand(0));
1622 Tmp1 = SelectExpr(N.getOperand(1));
1623 switch (N.getOperand(1).getValueType()) {
1624 default:
1625 assert(0 && "Unknown return type!");
1626 case MVT::f64:
1627 case MVT::f32:
1628 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
1629 break;
1630 case MVT::i32:
1631 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
1632 break;
1633 }
1634 case 1:
1635 Select(N.getOperand(0));
1636 break;
1637 }
1638 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
1639 return;
1640 case ISD::TRUNCSTORE:
1641 case ISD::STORE:
1642 {
1643 SDOperand Chain = N.getOperand(0);
1644 SDOperand Value = N.getOperand(1);
1645 SDOperand Address = N.getOperand(2);
1646 Select(Chain);
1647
1648 Tmp1 = SelectExpr(Value); //value
1649
1650 if (opcode == ISD::STORE) {
1651 switch(Value.getValueType()) {
1652 default: assert(0 && "unknown Type in store");
1653 case MVT::i64: Opc = PPC::STD; break;
1654 case MVT::f64: Opc = PPC::STFD; break;
1655 case MVT::f32: Opc = PPC::STFS; break;
1656 }
1657 } else { //ISD::TRUNCSTORE
1658 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1659 default: assert(0 && "unknown Type in store");
1660 case MVT::i1: //FIXME: DAG does not promote this load
1661 case MVT::i8: Opc= PPC::STB; break;
1662 case MVT::i16: Opc = PPC::STH; break;
1663 case MVT::i32: Opc = PPC::STW; break;
1664 }
1665 }
1666
1667 if(Address.getOpcode() == ISD::FrameIndex)
1668 {
1669 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
1670 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
1671 }
1672 else
1673 {
1674 int offset;
1675 bool idx = SelectAddr(Address, Tmp2, offset);
1676 if (idx) {
1677 Opc = IndexedOpForOp(Opc);
1678 BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset);
1679 } else {
1680 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1681 }
1682 }
1683 return;
1684 }
1685 case ISD::EXTLOAD:
1686 case ISD::SEXTLOAD:
1687 case ISD::ZEXTLOAD:
1688 case ISD::LOAD:
1689 case ISD::CopyFromReg:
1690 case ISD::CALL:
1691 case ISD::DYNAMIC_STACKALLOC:
1692 ExprMap.erase(N);
1693 SelectExpr(N);
1694 return;
1695 }
1696 assert(0 && "Should not be reached!");
1697}
1698
1699
1700/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
1701/// into a machine code representation using pattern matching and a machine
1702/// description file.
1703///
1704FunctionPass *llvm::createPPC64ISelPattern(TargetMachine &TM) {
1705 return new ISel(TM);
1706}
1707