blob: 3bceed418a11f9c5178736367915a6614deb58a3 [file] [log] [blame]
Bill Schmidt646cd792013-07-30 00:50:39 +00001//===-- PPCFastISel.cpp - PowerPC FastISel implementation -----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the PowerPC-specific support for the FastISel class. Some
11// of the target-specific code is generated by tablegen in the file
12// PPCGenFastISel.inc, which is #included here.
13//
14//===----------------------------------------------------------------------===//
15
16#define DEBUG_TYPE "ppcfastisel"
17#include "PPC.h"
18#include "PPCISelLowering.h"
19#include "PPCSubtarget.h"
20#include "PPCTargetMachine.h"
21#include "MCTargetDesc/PPCPredicates.h"
22#include "llvm/ADT/Optional.h"
23#include "llvm/CodeGen/CallingConvLower.h"
24#include "llvm/CodeGen/FastISel.h"
25#include "llvm/CodeGen/FunctionLoweringInfo.h"
26#include "llvm/CodeGen/MachineConstantPool.h"
27#include "llvm/CodeGen/MachineFrameInfo.h"
28#include "llvm/CodeGen/MachineInstrBuilder.h"
29#include "llvm/CodeGen/MachineRegisterInfo.h"
30#include "llvm/IR/CallingConv.h"
31#include "llvm/IR/GlobalAlias.h"
32#include "llvm/IR/GlobalVariable.h"
33#include "llvm/IR/IntrinsicInst.h"
34#include "llvm/IR/Operator.h"
35#include "llvm/Support/Debug.h"
36#include "llvm/Support/GetElementPtrTypeIterator.h"
37#include "llvm/Target/TargetLowering.h"
38#include "llvm/Target/TargetMachine.h"
39
40using namespace llvm;
41
42namespace {
43
44typedef struct Address {
45 enum {
46 RegBase,
47 FrameIndexBase
48 } BaseType;
49
50 union {
51 unsigned Reg;
52 int FI;
53 } Base;
54
Bill Schmidt72489682013-08-30 02:29:45 +000055 long Offset;
Bill Schmidt646cd792013-07-30 00:50:39 +000056
57 // Innocuous defaults for our address.
58 Address()
59 : BaseType(RegBase), Offset(0) {
60 Base.Reg = 0;
61 }
62} Address;
63
64class PPCFastISel : public FastISel {
65
66 const TargetMachine &TM;
67 const TargetInstrInfo &TII;
68 const TargetLowering &TLI;
69 const PPCSubtarget &PPCSubTarget;
70 LLVMContext *Context;
71
72 public:
73 explicit PPCFastISel(FunctionLoweringInfo &FuncInfo,
74 const TargetLibraryInfo *LibInfo)
75 : FastISel(FuncInfo, LibInfo),
76 TM(FuncInfo.MF->getTarget()),
77 TII(*TM.getInstrInfo()),
78 TLI(*TM.getTargetLowering()),
79 PPCSubTarget(
80 *((static_cast<const PPCTargetMachine *>(&TM))->getSubtargetImpl())
81 ),
82 Context(&FuncInfo.Fn->getContext()) { }
83
84 // Backend specific FastISel code.
85 private:
86 virtual bool TargetSelectInstruction(const Instruction *I);
87 virtual unsigned TargetMaterializeConstant(const Constant *C);
88 virtual unsigned TargetMaterializeAlloca(const AllocaInst *AI);
89 virtual bool tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
90 const LoadInst *LI);
91 virtual bool FastLowerArguments();
Bill Schmidt3fad2bc2013-08-25 22:33:42 +000092 virtual unsigned FastEmit_i(MVT Ty, MVT RetTy, unsigned Opc, uint64_t Imm);
Bill Schmidt72489682013-08-30 02:29:45 +000093 virtual unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
94 const TargetRegisterClass *RC,
95 unsigned Op0, bool Op0IsKill,
96 uint64_t Imm);
97 virtual unsigned FastEmitInst_r(unsigned MachineInstOpcode,
98 const TargetRegisterClass *RC,
99 unsigned Op0, bool Op0IsKill);
100 virtual unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
101 const TargetRegisterClass *RC,
102 unsigned Op0, bool Op0IsKill,
103 unsigned Op1, bool Op1IsKill);
Bill Schmidt3fad2bc2013-08-25 22:33:42 +0000104
105 // Instruction selection routines.
106 private:
Bill Schmidt72489682013-08-30 02:29:45 +0000107 bool SelectLoad(const Instruction *I);
108 bool SelectStore(const Instruction *I);
Bill Schmidt3fad2bc2013-08-25 22:33:42 +0000109 bool SelectBranch(const Instruction *I);
110 bool SelectIndirectBr(const Instruction *I);
Bill Schmidte206efd32013-08-30 03:16:48 +0000111 bool SelectCmp(const Instruction *I);
Bill Schmidt9bc94272013-08-30 15:18:11 +0000112 bool SelectFPExt(const Instruction *I);
113 bool SelectFPTrunc(const Instruction *I);
114 bool SelectIToFP(const Instruction *I, bool IsSigned);
115 bool SelectFPToI(const Instruction *I, bool IsSigned);
Bill Schmidt72489682013-08-30 02:29:45 +0000116 bool SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode);
Bill Schmidt055d2072013-08-26 19:42:51 +0000117 bool SelectRet(const Instruction *I);
118 bool SelectIntExt(const Instruction *I);
Bill Schmidt646cd792013-07-30 00:50:39 +0000119
120 // Utility routines.
121 private:
Bill Schmidt72489682013-08-30 02:29:45 +0000122 bool isTypeLegal(Type *Ty, MVT &VT);
123 bool isLoadTypeLegal(Type *Ty, MVT &VT);
Bill Schmidt3fad2bc2013-08-25 22:33:42 +0000124 bool PPCEmitCmp(const Value *Src1Value, const Value *Src2Value,
125 bool isZExt, unsigned DestReg);
Bill Schmidt72489682013-08-30 02:29:45 +0000126 bool PPCEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
127 const TargetRegisterClass *RC, bool IsZExt = true,
128 unsigned FP64LoadOpc = PPC::LFD);
129 bool PPCEmitStore(MVT VT, unsigned SrcReg, Address &Addr);
130 bool PPCComputeAddress(const Value *Obj, Address &Addr);
131 void PPCSimplifyAddress(Address &Addr, MVT VT, bool &UseOffset,
132 unsigned &IndexReg);
Bill Schmidt3fad2bc2013-08-25 22:33:42 +0000133 bool PPCEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
134 unsigned DestReg, bool IsZExt);
Bill Schmidt646cd792013-07-30 00:50:39 +0000135 unsigned PPCMaterializeFP(const ConstantFP *CFP, MVT VT);
Bill Schmidt72489682013-08-30 02:29:45 +0000136 unsigned PPCMaterializeGV(const GlobalValue *GV, MVT VT);
Bill Schmidt646cd792013-07-30 00:50:39 +0000137 unsigned PPCMaterializeInt(const Constant *C, MVT VT);
138 unsigned PPCMaterialize32BitInt(int64_t Imm,
139 const TargetRegisterClass *RC);
140 unsigned PPCMaterialize64BitInt(int64_t Imm,
141 const TargetRegisterClass *RC);
Bill Schmidt9bc94272013-08-30 15:18:11 +0000142 unsigned PPCMoveToIntReg(const Instruction *I, MVT VT,
143 unsigned SrcReg, bool IsSigned);
144 unsigned PPCMoveToFPReg(MVT VT, unsigned SrcReg, bool IsSigned);
Bill Schmidt646cd792013-07-30 00:50:39 +0000145
Bill Schmidt055d2072013-08-26 19:42:51 +0000146 // Call handling routines.
147 private:
148 CCAssignFn *usePPC32CCs(unsigned Flag);
149
Bill Schmidt646cd792013-07-30 00:50:39 +0000150 private:
151 #include "PPCGenFastISel.inc"
152
153};
154
155} // end anonymous namespace
156
Bill Schmidt055d2072013-08-26 19:42:51 +0000157#include "PPCGenCallingConv.inc"
158
159// Function whose sole purpose is to kill compiler warnings
160// stemming from unused functions included from PPCGenCallingConv.inc.
161CCAssignFn *PPCFastISel::usePPC32CCs(unsigned Flag) {
162 if (Flag == 1)
163 return CC_PPC32_SVR4;
164 else if (Flag == 2)
165 return CC_PPC32_SVR4_ByVal;
166 else if (Flag == 3)
167 return CC_PPC32_SVR4_VarArg;
168 else
169 return RetCC_PPC;
170}
171
Bill Schmidt3fad2bc2013-08-25 22:33:42 +0000172static Optional<PPC::Predicate> getComparePred(CmpInst::Predicate Pred) {
173 switch (Pred) {
174 // These are not representable with any single compare.
175 case CmpInst::FCMP_FALSE:
176 case CmpInst::FCMP_UEQ:
177 case CmpInst::FCMP_UGT:
178 case CmpInst::FCMP_UGE:
179 case CmpInst::FCMP_ULT:
180 case CmpInst::FCMP_ULE:
181 case CmpInst::FCMP_UNE:
182 case CmpInst::FCMP_TRUE:
183 default:
184 return Optional<PPC::Predicate>();
185
186 case CmpInst::FCMP_OEQ:
187 case CmpInst::ICMP_EQ:
188 return PPC::PRED_EQ;
189
190 case CmpInst::FCMP_OGT:
191 case CmpInst::ICMP_UGT:
192 case CmpInst::ICMP_SGT:
193 return PPC::PRED_GT;
194
195 case CmpInst::FCMP_OGE:
196 case CmpInst::ICMP_UGE:
197 case CmpInst::ICMP_SGE:
198 return PPC::PRED_GE;
199
200 case CmpInst::FCMP_OLT:
201 case CmpInst::ICMP_ULT:
202 case CmpInst::ICMP_SLT:
203 return PPC::PRED_LT;
204
205 case CmpInst::FCMP_OLE:
206 case CmpInst::ICMP_ULE:
207 case CmpInst::ICMP_SLE:
208 return PPC::PRED_LE;
209
210 case CmpInst::FCMP_ONE:
211 case CmpInst::ICMP_NE:
212 return PPC::PRED_NE;
213
214 case CmpInst::FCMP_ORD:
215 return PPC::PRED_NU;
216
217 case CmpInst::FCMP_UNO:
218 return PPC::PRED_UN;
219 }
220}
221
Bill Schmidt72489682013-08-30 02:29:45 +0000222// Determine whether the type Ty is simple enough to be handled by
223// fast-isel, and return its equivalent machine type in VT.
224// FIXME: Copied directly from ARM -- factor into base class?
225bool PPCFastISel::isTypeLegal(Type *Ty, MVT &VT) {
226 EVT Evt = TLI.getValueType(Ty, true);
227
228 // Only handle simple types.
229 if (Evt == MVT::Other || !Evt.isSimple()) return false;
230 VT = Evt.getSimpleVT();
231
232 // Handle all legal types, i.e. a register that will directly hold this
233 // value.
234 return TLI.isTypeLegal(VT);
235}
236
237// Determine whether the type Ty is simple enough to be handled by
238// fast-isel as a load target, and return its equivalent machine type in VT.
239bool PPCFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
240 if (isTypeLegal(Ty, VT)) return true;
241
242 // If this is a type than can be sign or zero-extended to a basic operation
243 // go ahead and accept it now.
244 if (VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32) {
245 return true;
246 }
247
248 return false;
249}
250
251// Given a value Obj, create an Address object Addr that represents its
252// address. Return false if we can't handle it.
253bool PPCFastISel::PPCComputeAddress(const Value *Obj, Address &Addr) {
254 const User *U = NULL;
255 unsigned Opcode = Instruction::UserOp1;
256 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
257 // Don't walk into other basic blocks unless the object is an alloca from
258 // another block, otherwise it may not have a virtual register assigned.
259 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
260 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
261 Opcode = I->getOpcode();
262 U = I;
263 }
264 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
265 Opcode = C->getOpcode();
266 U = C;
267 }
268
269 switch (Opcode) {
270 default:
271 break;
272 case Instruction::BitCast:
273 // Look through bitcasts.
274 return PPCComputeAddress(U->getOperand(0), Addr);
275 case Instruction::IntToPtr:
276 // Look past no-op inttoptrs.
277 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
278 return PPCComputeAddress(U->getOperand(0), Addr);
279 break;
280 case Instruction::PtrToInt:
281 // Look past no-op ptrtoints.
282 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
283 return PPCComputeAddress(U->getOperand(0), Addr);
284 break;
285 case Instruction::GetElementPtr: {
286 Address SavedAddr = Addr;
287 long TmpOffset = Addr.Offset;
288
289 // Iterate through the GEP folding the constants into offsets where
290 // we can.
291 gep_type_iterator GTI = gep_type_begin(U);
292 for (User::const_op_iterator II = U->op_begin() + 1, IE = U->op_end();
293 II != IE; ++II, ++GTI) {
294 const Value *Op = *II;
295 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
296 const StructLayout *SL = TD.getStructLayout(STy);
297 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
298 TmpOffset += SL->getElementOffset(Idx);
299 } else {
300 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
301 for (;;) {
302 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
303 // Constant-offset addressing.
304 TmpOffset += CI->getSExtValue() * S;
305 break;
306 }
307 if (isa<AddOperator>(Op) &&
308 (!isa<Instruction>(Op) ||
309 FuncInfo.MBBMap[cast<Instruction>(Op)->getParent()]
310 == FuncInfo.MBB) &&
311 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
312 // An add (in the same block) with a constant operand. Fold the
313 // constant.
314 ConstantInt *CI =
315 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
316 TmpOffset += CI->getSExtValue() * S;
317 // Iterate on the other operand.
318 Op = cast<AddOperator>(Op)->getOperand(0);
319 continue;
320 }
321 // Unsupported
322 goto unsupported_gep;
323 }
324 }
325 }
326
327 // Try to grab the base operand now.
328 Addr.Offset = TmpOffset;
329 if (PPCComputeAddress(U->getOperand(0), Addr)) return true;
330
331 // We failed, restore everything and try the other options.
332 Addr = SavedAddr;
333
334 unsupported_gep:
335 break;
336 }
337 case Instruction::Alloca: {
338 const AllocaInst *AI = cast<AllocaInst>(Obj);
339 DenseMap<const AllocaInst*, int>::iterator SI =
340 FuncInfo.StaticAllocaMap.find(AI);
341 if (SI != FuncInfo.StaticAllocaMap.end()) {
342 Addr.BaseType = Address::FrameIndexBase;
343 Addr.Base.FI = SI->second;
344 return true;
345 }
346 break;
347 }
348 }
349
350 // FIXME: References to parameters fall through to the behavior
351 // below. They should be able to reference a frame index since
352 // they are stored to the stack, so we can get "ld rx, offset(r1)"
353 // instead of "addi ry, r1, offset / ld rx, 0(ry)". Obj will
354 // just contain the parameter. Try to handle this with a FI.
355
356 // Try to get this in a register if nothing else has worked.
357 if (Addr.Base.Reg == 0)
358 Addr.Base.Reg = getRegForValue(Obj);
359
360 // Prevent assignment of base register to X0, which is inappropriate
361 // for loads and stores alike.
362 if (Addr.Base.Reg != 0)
363 MRI.setRegClass(Addr.Base.Reg, &PPC::G8RC_and_G8RC_NOX0RegClass);
364
365 return Addr.Base.Reg != 0;
366}
367
368// Fix up some addresses that can't be used directly. For example, if
369// an offset won't fit in an instruction field, we may need to move it
370// into an index register.
371void PPCFastISel::PPCSimplifyAddress(Address &Addr, MVT VT, bool &UseOffset,
372 unsigned &IndexReg) {
373
374 // Check whether the offset fits in the instruction field.
375 if (!isInt<16>(Addr.Offset))
376 UseOffset = false;
377
378 // If this is a stack pointer and the offset needs to be simplified then
379 // put the alloca address into a register, set the base type back to
380 // register and continue. This should almost never happen.
381 if (!UseOffset && Addr.BaseType == Address::FrameIndexBase) {
382 unsigned ResultReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
383 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(PPC::ADDI8),
384 ResultReg).addFrameIndex(Addr.Base.FI).addImm(0);
385 Addr.Base.Reg = ResultReg;
386 Addr.BaseType = Address::RegBase;
387 }
388
389 if (!UseOffset) {
390 IntegerType *OffsetTy = ((VT == MVT::i32) ? Type::getInt32Ty(*Context)
391 : Type::getInt64Ty(*Context));
392 const ConstantInt *Offset =
393 ConstantInt::getSigned(OffsetTy, (int64_t)(Addr.Offset));
394 IndexReg = PPCMaterializeInt(Offset, MVT::i64);
395 assert(IndexReg && "Unexpected error in PPCMaterializeInt!");
396 }
397}
398
399// Emit a load instruction if possible, returning true if we succeeded,
400// otherwise false. See commentary below for how the register class of
401// the load is determined.
402bool PPCFastISel::PPCEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
403 const TargetRegisterClass *RC,
404 bool IsZExt, unsigned FP64LoadOpc) {
405 unsigned Opc;
406 bool UseOffset = true;
407
408 // If ResultReg is given, it determines the register class of the load.
409 // Otherwise, RC is the register class to use. If the result of the
410 // load isn't anticipated in this block, both may be zero, in which
411 // case we must make a conservative guess. In particular, don't assign
412 // R0 or X0 to the result register, as the result may be used in a load,
413 // store, add-immediate, or isel that won't permit this. (Though
414 // perhaps the spill and reload of live-exit values would handle this?)
415 const TargetRegisterClass *UseRC =
416 (ResultReg ? MRI.getRegClass(ResultReg) :
417 (RC ? RC :
418 (VT == MVT::f64 ? &PPC::F8RCRegClass :
419 (VT == MVT::f32 ? &PPC::F4RCRegClass :
420 (VT == MVT::i64 ? &PPC::G8RC_and_G8RC_NOX0RegClass :
421 &PPC::GPRC_and_GPRC_NOR0RegClass)))));
422
423 bool Is32BitInt = UseRC->hasSuperClassEq(&PPC::GPRCRegClass);
424
425 switch (VT.SimpleTy) {
426 default: // e.g., vector types not handled
427 return false;
428 case MVT::i8:
429 Opc = Is32BitInt ? PPC::LBZ : PPC::LBZ8;
430 break;
431 case MVT::i16:
432 Opc = (IsZExt ?
433 (Is32BitInt ? PPC::LHZ : PPC::LHZ8) :
434 (Is32BitInt ? PPC::LHA : PPC::LHA8));
435 break;
436 case MVT::i32:
437 Opc = (IsZExt ?
438 (Is32BitInt ? PPC::LWZ : PPC::LWZ8) :
439 (Is32BitInt ? PPC::LWA_32 : PPC::LWA));
440 if ((Opc == PPC::LWA || Opc == PPC::LWA_32) && ((Addr.Offset & 3) != 0))
441 UseOffset = false;
442 break;
443 case MVT::i64:
444 Opc = PPC::LD;
445 assert(UseRC->hasSuperClassEq(&PPC::G8RCRegClass) &&
446 "64-bit load with 32-bit target??");
447 UseOffset = ((Addr.Offset & 3) == 0);
448 break;
449 case MVT::f32:
450 Opc = PPC::LFS;
451 break;
452 case MVT::f64:
453 Opc = FP64LoadOpc;
454 break;
455 }
456
457 // If necessary, materialize the offset into a register and use
458 // the indexed form. Also handle stack pointers with special needs.
459 unsigned IndexReg = 0;
460 PPCSimplifyAddress(Addr, VT, UseOffset, IndexReg);
461 if (ResultReg == 0)
462 ResultReg = createResultReg(UseRC);
463
464 // Note: If we still have a frame index here, we know the offset is
465 // in range, as otherwise PPCSimplifyAddress would have converted it
466 // into a RegBase.
467 if (Addr.BaseType == Address::FrameIndexBase) {
468
469 MachineMemOperand *MMO =
470 FuncInfo.MF->getMachineMemOperand(
471 MachinePointerInfo::getFixedStack(Addr.Base.FI, Addr.Offset),
472 MachineMemOperand::MOLoad, MFI.getObjectSize(Addr.Base.FI),
473 MFI.getObjectAlignment(Addr.Base.FI));
474
475 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg)
476 .addImm(Addr.Offset).addFrameIndex(Addr.Base.FI).addMemOperand(MMO);
477
478 // Base reg with offset in range.
479 } else if (UseOffset) {
480
481 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg)
482 .addImm(Addr.Offset).addReg(Addr.Base.Reg);
483
484 // Indexed form.
485 } else {
486 // Get the RR opcode corresponding to the RI one. FIXME: It would be
487 // preferable to use the ImmToIdxMap from PPCRegisterInfo.cpp, but it
488 // is hard to get at.
489 switch (Opc) {
490 default: llvm_unreachable("Unexpected opcode!");
491 case PPC::LBZ: Opc = PPC::LBZX; break;
492 case PPC::LBZ8: Opc = PPC::LBZX8; break;
493 case PPC::LHZ: Opc = PPC::LHZX; break;
494 case PPC::LHZ8: Opc = PPC::LHZX8; break;
495 case PPC::LHA: Opc = PPC::LHAX; break;
496 case PPC::LHA8: Opc = PPC::LHAX8; break;
497 case PPC::LWZ: Opc = PPC::LWZX; break;
498 case PPC::LWZ8: Opc = PPC::LWZX8; break;
499 case PPC::LWA: Opc = PPC::LWAX; break;
500 case PPC::LWA_32: Opc = PPC::LWAX_32; break;
501 case PPC::LD: Opc = PPC::LDX; break;
502 case PPC::LFS: Opc = PPC::LFSX; break;
503 case PPC::LFD: Opc = PPC::LFDX; break;
504 }
505 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg)
506 .addReg(Addr.Base.Reg).addReg(IndexReg);
507 }
508
509 return true;
510}
511
512// Attempt to fast-select a load instruction.
513bool PPCFastISel::SelectLoad(const Instruction *I) {
514 // FIXME: No atomic loads are supported.
515 if (cast<LoadInst>(I)->isAtomic())
516 return false;
517
518 // Verify we have a legal type before going any further.
519 MVT VT;
520 if (!isLoadTypeLegal(I->getType(), VT))
521 return false;
522
523 // See if we can handle this address.
524 Address Addr;
525 if (!PPCComputeAddress(I->getOperand(0), Addr))
526 return false;
527
528 // Look at the currently assigned register for this instruction
529 // to determine the required register class. This is necessary
530 // to constrain RA from using R0/X0 when this is not legal.
531 unsigned AssignedReg = FuncInfo.ValueMap[I];
532 const TargetRegisterClass *RC =
533 AssignedReg ? MRI.getRegClass(AssignedReg) : 0;
534
535 unsigned ResultReg = 0;
536 if (!PPCEmitLoad(VT, ResultReg, Addr, RC))
537 return false;
538 UpdateValueMap(I, ResultReg);
539 return true;
540}
541
542// Emit a store instruction to store SrcReg at Addr.
543bool PPCFastISel::PPCEmitStore(MVT VT, unsigned SrcReg, Address &Addr) {
544 assert(SrcReg && "Nothing to store!");
545 unsigned Opc;
546 bool UseOffset = true;
547
548 const TargetRegisterClass *RC = MRI.getRegClass(SrcReg);
549 bool Is32BitInt = RC->hasSuperClassEq(&PPC::GPRCRegClass);
550
551 switch (VT.SimpleTy) {
552 default: // e.g., vector types not handled
553 return false;
554 case MVT::i8:
555 Opc = Is32BitInt ? PPC::STB : PPC::STB8;
556 break;
557 case MVT::i16:
558 Opc = Is32BitInt ? PPC::STH : PPC::STH8;
559 break;
560 case MVT::i32:
561 assert(Is32BitInt && "Not GPRC for i32??");
562 Opc = PPC::STW;
563 break;
564 case MVT::i64:
565 Opc = PPC::STD;
566 UseOffset = ((Addr.Offset & 3) == 0);
567 break;
568 case MVT::f32:
569 Opc = PPC::STFS;
570 break;
571 case MVT::f64:
572 Opc = PPC::STFD;
573 break;
574 }
575
576 // If necessary, materialize the offset into a register and use
577 // the indexed form. Also handle stack pointers with special needs.
578 unsigned IndexReg = 0;
579 PPCSimplifyAddress(Addr, VT, UseOffset, IndexReg);
580
581 // Note: If we still have a frame index here, we know the offset is
582 // in range, as otherwise PPCSimplifyAddress would have converted it
583 // into a RegBase.
584 if (Addr.BaseType == Address::FrameIndexBase) {
585 MachineMemOperand *MMO =
586 FuncInfo.MF->getMachineMemOperand(
587 MachinePointerInfo::getFixedStack(Addr.Base.FI, Addr.Offset),
588 MachineMemOperand::MOStore, MFI.getObjectSize(Addr.Base.FI),
589 MFI.getObjectAlignment(Addr.Base.FI));
590
591 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc)).addReg(SrcReg)
592 .addImm(Addr.Offset).addFrameIndex(Addr.Base.FI).addMemOperand(MMO);
593
594 // Base reg with offset in range.
Bill Schmidt40433e52013-08-30 03:07:11 +0000595 } else if (UseOffset)
Bill Schmidt72489682013-08-30 02:29:45 +0000596 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc))
597 .addReg(SrcReg).addImm(Addr.Offset).addReg(Addr.Base.Reg);
598
599 // Indexed form.
Bill Schmidt40433e52013-08-30 03:07:11 +0000600 else {
Bill Schmidt72489682013-08-30 02:29:45 +0000601 // Get the RR opcode corresponding to the RI one. FIXME: It would be
602 // preferable to use the ImmToIdxMap from PPCRegisterInfo.cpp, but it
603 // is hard to get at.
604 switch (Opc) {
605 default: llvm_unreachable("Unexpected opcode!");
606 case PPC::STB: Opc = PPC::STBX; break;
607 case PPC::STH : Opc = PPC::STHX; break;
608 case PPC::STW : Opc = PPC::STWX; break;
609 case PPC::STB8: Opc = PPC::STBX8; break;
610 case PPC::STH8: Opc = PPC::STHX8; break;
611 case PPC::STW8: Opc = PPC::STWX8; break;
612 case PPC::STD: Opc = PPC::STDX; break;
613 case PPC::STFS: Opc = PPC::STFSX; break;
614 case PPC::STFD: Opc = PPC::STFDX; break;
615 }
616 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc))
617 .addReg(SrcReg).addReg(Addr.Base.Reg).addReg(IndexReg);
618 }
619
620 return true;
621}
622
623// Attempt to fast-select a store instruction.
624bool PPCFastISel::SelectStore(const Instruction *I) {
625 Value *Op0 = I->getOperand(0);
626 unsigned SrcReg = 0;
627
628 // FIXME: No atomics loads are supported.
629 if (cast<StoreInst>(I)->isAtomic())
630 return false;
631
632 // Verify we have a legal type before going any further.
633 MVT VT;
634 if (!isLoadTypeLegal(Op0->getType(), VT))
635 return false;
636
637 // Get the value to be stored into a register.
638 SrcReg = getRegForValue(Op0);
639 if (SrcReg == 0)
640 return false;
641
642 // See if we can handle this address.
643 Address Addr;
644 if (!PPCComputeAddress(I->getOperand(1), Addr))
645 return false;
646
647 if (!PPCEmitStore(VT, SrcReg, Addr))
648 return false;
649
650 return true;
651}
652
Bill Schmidt3fad2bc2013-08-25 22:33:42 +0000653// Attempt to fast-select a branch instruction.
654bool PPCFastISel::SelectBranch(const Instruction *I) {
655 const BranchInst *BI = cast<BranchInst>(I);
656 MachineBasicBlock *BrBB = FuncInfo.MBB;
657 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
658 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
659
660 // For now, just try the simplest case where it's fed by a compare.
661 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
662 Optional<PPC::Predicate> OptPPCPred = getComparePred(CI->getPredicate());
663 if (!OptPPCPred)
664 return false;
665
666 PPC::Predicate PPCPred = OptPPCPred.getValue();
667
668 // Take advantage of fall-through opportunities.
669 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
670 std::swap(TBB, FBB);
671 PPCPred = PPC::InvertPredicate(PPCPred);
672 }
673
674 unsigned CondReg = createResultReg(&PPC::CRRCRegClass);
675
676 if (!PPCEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned(),
677 CondReg))
678 return false;
679
680 BuildMI(*BrBB, FuncInfo.InsertPt, DL, TII.get(PPC::BCC))
681 .addImm(PPCPred).addReg(CondReg).addMBB(TBB);
682 FastEmitBranch(FBB, DL);
683 FuncInfo.MBB->addSuccessor(TBB);
684 return true;
685
686 } else if (const ConstantInt *CI =
687 dyn_cast<ConstantInt>(BI->getCondition())) {
688 uint64_t Imm = CI->getZExtValue();
689 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
690 FastEmitBranch(Target, DL);
691 return true;
692 }
693
694 // FIXME: ARM looks for a case where the block containing the compare
695 // has been split from the block containing the branch. If this happens,
696 // there is a vreg available containing the result of the compare. I'm
697 // not sure we can do much, as we've lost the predicate information with
698 // the compare instruction -- we have a 4-bit CR but don't know which bit
699 // to test here.
700 return false;
701}
702
703// Attempt to emit a compare of the two source values. Signed and unsigned
704// comparisons are supported. Return false if we can't handle it.
705bool PPCFastISel::PPCEmitCmp(const Value *SrcValue1, const Value *SrcValue2,
706 bool IsZExt, unsigned DestReg) {
707 Type *Ty = SrcValue1->getType();
708 EVT SrcEVT = TLI.getValueType(Ty, true);
709 if (!SrcEVT.isSimple())
710 return false;
711 MVT SrcVT = SrcEVT.getSimpleVT();
712
713 // See if operand 2 is an immediate encodeable in the compare.
714 // FIXME: Operands are not in canonical order at -O0, so an immediate
715 // operand in position 1 is a lost opportunity for now. We are
716 // similar to ARM in this regard.
717 long Imm = 0;
718 bool UseImm = false;
719
720 // Only 16-bit integer constants can be represented in compares for
721 // PowerPC. Others will be materialized into a register.
722 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(SrcValue2)) {
723 if (SrcVT == MVT::i64 || SrcVT == MVT::i32 || SrcVT == MVT::i16 ||
724 SrcVT == MVT::i8 || SrcVT == MVT::i1) {
725 const APInt &CIVal = ConstInt->getValue();
726 Imm = (IsZExt) ? (long)CIVal.getZExtValue() : (long)CIVal.getSExtValue();
727 if ((IsZExt && isUInt<16>(Imm)) || (!IsZExt && isInt<16>(Imm)))
728 UseImm = true;
729 }
730 }
731
732 unsigned CmpOpc;
733 bool NeedsExt = false;
734 switch (SrcVT.SimpleTy) {
735 default: return false;
736 case MVT::f32:
737 CmpOpc = PPC::FCMPUS;
738 break;
739 case MVT::f64:
740 CmpOpc = PPC::FCMPUD;
741 break;
742 case MVT::i1:
743 case MVT::i8:
744 case MVT::i16:
745 NeedsExt = true;
746 // Intentional fall-through.
747 case MVT::i32:
748 if (!UseImm)
749 CmpOpc = IsZExt ? PPC::CMPLW : PPC::CMPW;
750 else
751 CmpOpc = IsZExt ? PPC::CMPLWI : PPC::CMPWI;
752 break;
753 case MVT::i64:
754 if (!UseImm)
755 CmpOpc = IsZExt ? PPC::CMPLD : PPC::CMPD;
756 else
757 CmpOpc = IsZExt ? PPC::CMPLDI : PPC::CMPDI;
758 break;
759 }
760
761 unsigned SrcReg1 = getRegForValue(SrcValue1);
762 if (SrcReg1 == 0)
763 return false;
764
765 unsigned SrcReg2 = 0;
766 if (!UseImm) {
767 SrcReg2 = getRegForValue(SrcValue2);
768 if (SrcReg2 == 0)
769 return false;
770 }
771
772 if (NeedsExt) {
773 unsigned ExtReg = createResultReg(&PPC::GPRCRegClass);
774 if (!PPCEmitIntExt(SrcVT, SrcReg1, MVT::i32, ExtReg, IsZExt))
775 return false;
776 SrcReg1 = ExtReg;
777
778 if (!UseImm) {
779 unsigned ExtReg = createResultReg(&PPC::GPRCRegClass);
780 if (!PPCEmitIntExt(SrcVT, SrcReg2, MVT::i32, ExtReg, IsZExt))
781 return false;
782 SrcReg2 = ExtReg;
783 }
784 }
785
786 if (!UseImm)
787 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc), DestReg)
788 .addReg(SrcReg1).addReg(SrcReg2);
789 else
790 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc), DestReg)
791 .addReg(SrcReg1).addImm(Imm);
792
793 return true;
794}
795
Bill Schmidt9bc94272013-08-30 15:18:11 +0000796// Attempt to fast-select a floating-point extend instruction.
797bool PPCFastISel::SelectFPExt(const Instruction *I) {
798 Value *Src = I->getOperand(0);
799 EVT SrcVT = TLI.getValueType(Src->getType(), true);
800 EVT DestVT = TLI.getValueType(I->getType(), true);
801
802 if (SrcVT != MVT::f32 || DestVT != MVT::f64)
803 return false;
804
805 unsigned SrcReg = getRegForValue(Src);
806 if (!SrcReg)
807 return false;
808
809 // No code is generated for a FP extend.
810 UpdateValueMap(I, SrcReg);
811 return true;
812}
813
814// Attempt to fast-select a floating-point truncate instruction.
815bool PPCFastISel::SelectFPTrunc(const Instruction *I) {
816 Value *Src = I->getOperand(0);
817 EVT SrcVT = TLI.getValueType(Src->getType(), true);
818 EVT DestVT = TLI.getValueType(I->getType(), true);
819
820 if (SrcVT != MVT::f64 || DestVT != MVT::f32)
821 return false;
822
823 unsigned SrcReg = getRegForValue(Src);
824 if (!SrcReg)
825 return false;
826
827 // Round the result to single precision.
828 unsigned DestReg = createResultReg(&PPC::F4RCRegClass);
829 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(PPC::FRSP), DestReg)
830 .addReg(SrcReg);
831
832 UpdateValueMap(I, DestReg);
833 return true;
834}
835
836// Move an i32 or i64 value in a GPR to an f64 value in an FPR.
837// FIXME: When direct register moves are implemented (see PowerISA 2.08),
838// those should be used instead of moving via a stack slot when the
839// subtarget permits.
840// FIXME: The code here is sloppy for the 4-byte case. Can use a 4-byte
841// stack slot and 4-byte store/load sequence. Or just sext the 4-byte
842// case to 8 bytes which produces tighter code but wastes stack space.
843unsigned PPCFastISel::PPCMoveToFPReg(MVT SrcVT, unsigned SrcReg,
844 bool IsSigned) {
845
846 // If necessary, extend 32-bit int to 64-bit.
847 if (SrcVT == MVT::i32) {
848 unsigned TmpReg = createResultReg(&PPC::G8RCRegClass);
849 if (!PPCEmitIntExt(MVT::i32, SrcReg, MVT::i64, TmpReg, !IsSigned))
850 return 0;
851 SrcReg = TmpReg;
852 }
853
854 // Get a stack slot 8 bytes wide, aligned on an 8-byte boundary.
855 Address Addr;
856 Addr.BaseType = Address::FrameIndexBase;
857 Addr.Base.FI = MFI.CreateStackObject(8, 8, false);
858
859 // Store the value from the GPR.
860 if (!PPCEmitStore(MVT::i64, SrcReg, Addr))
861 return 0;
862
863 // Load the integer value into an FPR. The kind of load used depends
864 // on a number of conditions.
865 unsigned LoadOpc = PPC::LFD;
866
867 if (SrcVT == MVT::i32) {
868 Addr.Offset = 4;
869 if (!IsSigned)
870 LoadOpc = PPC::LFIWZX;
871 else if (PPCSubTarget.hasLFIWAX())
872 LoadOpc = PPC::LFIWAX;
873 }
874
875 const TargetRegisterClass *RC = &PPC::F8RCRegClass;
876 unsigned ResultReg = 0;
877 if (!PPCEmitLoad(MVT::f64, ResultReg, Addr, RC, !IsSigned, LoadOpc))
878 return 0;
879
880 return ResultReg;
881}
882
883// Attempt to fast-select an integer-to-floating-point conversion.
884bool PPCFastISel::SelectIToFP(const Instruction *I, bool IsSigned) {
885 MVT DstVT;
886 Type *DstTy = I->getType();
887 if (!isTypeLegal(DstTy, DstVT))
888 return false;
889
890 if (DstVT != MVT::f32 && DstVT != MVT::f64)
891 return false;
892
893 Value *Src = I->getOperand(0);
894 EVT SrcEVT = TLI.getValueType(Src->getType(), true);
895 if (!SrcEVT.isSimple())
896 return false;
897
898 MVT SrcVT = SrcEVT.getSimpleVT();
899
900 if (SrcVT != MVT::i8 && SrcVT != MVT::i16 &&
901 SrcVT != MVT::i32 && SrcVT != MVT::i64)
902 return false;
903
904 unsigned SrcReg = getRegForValue(Src);
905 if (SrcReg == 0)
906 return false;
907
908 // We can only lower an unsigned convert if we have the newer
909 // floating-point conversion operations.
910 if (!IsSigned && !PPCSubTarget.hasFPCVT())
911 return false;
912
913 // FIXME: For now we require the newer floating-point conversion operations
914 // (which are present only on P7 and A2 server models) when converting
915 // to single-precision float. Otherwise we have to generate a lot of
916 // fiddly code to avoid double rounding. If necessary, the fiddly code
917 // can be found in PPCTargetLowering::LowerINT_TO_FP().
918 if (DstVT == MVT::f32 && !PPCSubTarget.hasFPCVT())
919 return false;
920
921 // Extend the input if necessary.
922 if (SrcVT == MVT::i8 || SrcVT == MVT::i16) {
923 unsigned TmpReg = createResultReg(&PPC::G8RCRegClass);
924 if (!PPCEmitIntExt(SrcVT, SrcReg, MVT::i64, TmpReg, !IsSigned))
925 return false;
926 SrcVT = MVT::i64;
927 SrcReg = TmpReg;
928 }
929
930 // Move the integer value to an FPR.
931 unsigned FPReg = PPCMoveToFPReg(SrcVT, SrcReg, IsSigned);
932 if (FPReg == 0)
933 return false;
934
935 // Determine the opcode for the conversion.
936 const TargetRegisterClass *RC = &PPC::F8RCRegClass;
937 unsigned DestReg = createResultReg(RC);
938 unsigned Opc;
939
940 if (DstVT == MVT::f32)
941 Opc = IsSigned ? PPC::FCFIDS : PPC::FCFIDUS;
942 else
943 Opc = IsSigned ? PPC::FCFID : PPC::FCFIDU;
944
945 // Generate the convert.
946 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), DestReg)
947 .addReg(FPReg);
948
949 UpdateValueMap(I, DestReg);
950 return true;
951}
952
953// Move the floating-point value in SrcReg into an integer destination
954// register, and return the register (or zero if we can't handle it).
955// FIXME: When direct register moves are implemented (see PowerISA 2.08),
956// those should be used instead of moving via a stack slot when the
957// subtarget permits.
958unsigned PPCFastISel::PPCMoveToIntReg(const Instruction *I, MVT VT,
959 unsigned SrcReg, bool IsSigned) {
960 // Get a stack slot 8 bytes wide, aligned on an 8-byte boundary.
961 // Note that if have STFIWX available, we could use a 4-byte stack
962 // slot for i32, but this being fast-isel we'll just go with the
963 // easiest code gen possible.
964 Address Addr;
965 Addr.BaseType = Address::FrameIndexBase;
966 Addr.Base.FI = MFI.CreateStackObject(8, 8, false);
967
968 // Store the value from the FPR.
969 if (!PPCEmitStore(MVT::f64, SrcReg, Addr))
970 return 0;
971
972 // Reload it into a GPR. If we want an i32, modify the address
973 // to have a 4-byte offset so we load from the right place.
974 if (VT == MVT::i32)
975 Addr.Offset = 4;
976
977 // Look at the currently assigned register for this instruction
978 // to determine the required register class.
979 unsigned AssignedReg = FuncInfo.ValueMap[I];
980 const TargetRegisterClass *RC =
981 AssignedReg ? MRI.getRegClass(AssignedReg) : 0;
982
983 unsigned ResultReg = 0;
984 if (!PPCEmitLoad(VT, ResultReg, Addr, RC, !IsSigned))
985 return 0;
986
987 return ResultReg;
988}
989
990// Attempt to fast-select a floating-point-to-integer conversion.
991bool PPCFastISel::SelectFPToI(const Instruction *I, bool IsSigned) {
992 MVT DstVT, SrcVT;
993 Type *DstTy = I->getType();
994 if (!isTypeLegal(DstTy, DstVT))
995 return false;
996
997 if (DstVT != MVT::i32 && DstVT != MVT::i64)
998 return false;
999
1000 Value *Src = I->getOperand(0);
1001 Type *SrcTy = Src->getType();
1002 if (!isTypeLegal(SrcTy, SrcVT))
1003 return false;
1004
1005 if (SrcVT != MVT::f32 && SrcVT != MVT::f64)
1006 return false;
1007
1008 unsigned SrcReg = getRegForValue(Src);
1009 if (SrcReg == 0)
1010 return false;
1011
1012 // Convert f32 to f64 if necessary. This is just a meaningless copy
1013 // to get the register class right. COPY_TO_REGCLASS is needed since
1014 // a COPY from F4RC to F8RC is converted to a F4RC-F4RC copy downstream.
1015 const TargetRegisterClass *InRC = MRI.getRegClass(SrcReg);
1016 if (InRC == &PPC::F4RCRegClass) {
1017 unsigned TmpReg = createResultReg(&PPC::F8RCRegClass);
1018 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1019 TII.get(TargetOpcode::COPY_TO_REGCLASS), TmpReg)
1020 .addReg(SrcReg).addImm(PPC::F8RCRegClassID);
1021 SrcReg = TmpReg;
1022 }
1023
1024 // Determine the opcode for the conversion, which takes place
1025 // entirely within FPRs.
1026 unsigned DestReg = createResultReg(&PPC::F8RCRegClass);
1027 unsigned Opc;
1028
1029 if (DstVT == MVT::i32)
1030 if (IsSigned)
1031 Opc = PPC::FCTIWZ;
1032 else
1033 Opc = PPCSubTarget.hasFPCVT() ? PPC::FCTIWUZ : PPC::FCTIDZ;
1034 else
1035 Opc = IsSigned ? PPC::FCTIDZ : PPC::FCTIDUZ;
1036
1037 // Generate the convert.
1038 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), DestReg)
1039 .addReg(SrcReg);
1040
1041 // Now move the integer value from a float register to an integer register.
1042 unsigned IntReg = PPCMoveToIntReg(I, DstVT, DestReg, IsSigned);
1043 if (IntReg == 0)
1044 return false;
1045
1046 UpdateValueMap(I, IntReg);
1047 return true;
1048}
1049
Bill Schmidt72489682013-08-30 02:29:45 +00001050// Attempt to fast-select a binary integer operation that isn't already
1051// handled automatically.
1052bool PPCFastISel::SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode) {
1053 EVT DestVT = TLI.getValueType(I->getType(), true);
1054
1055 // We can get here in the case when we have a binary operation on a non-legal
1056 // type and the target independent selector doesn't know how to handle it.
1057 if (DestVT != MVT::i16 && DestVT != MVT::i8)
1058 return false;
1059
1060 // Look at the currently assigned register for this instruction
1061 // to determine the required register class. If there is no register,
1062 // make a conservative choice (don't assign R0).
1063 unsigned AssignedReg = FuncInfo.ValueMap[I];
1064 const TargetRegisterClass *RC =
1065 (AssignedReg ? MRI.getRegClass(AssignedReg) :
1066 &PPC::GPRC_and_GPRC_NOR0RegClass);
1067 bool IsGPRC = RC->hasSuperClassEq(&PPC::GPRCRegClass);
1068
1069 unsigned Opc;
1070 switch (ISDOpcode) {
1071 default: return false;
1072 case ISD::ADD:
1073 Opc = IsGPRC ? PPC::ADD4 : PPC::ADD8;
1074 break;
1075 case ISD::OR:
1076 Opc = IsGPRC ? PPC::OR : PPC::OR8;
1077 break;
1078 case ISD::SUB:
1079 Opc = IsGPRC ? PPC::SUBF : PPC::SUBF8;
1080 break;
1081 }
1082
1083 unsigned ResultReg = createResultReg(RC ? RC : &PPC::G8RCRegClass);
1084 unsigned SrcReg1 = getRegForValue(I->getOperand(0));
1085 if (SrcReg1 == 0) return false;
1086
1087 // Handle case of small immediate operand.
1088 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(I->getOperand(1))) {
1089 const APInt &CIVal = ConstInt->getValue();
1090 int Imm = (int)CIVal.getSExtValue();
1091 bool UseImm = true;
1092 if (isInt<16>(Imm)) {
1093 switch (Opc) {
1094 default:
1095 llvm_unreachable("Missing case!");
1096 case PPC::ADD4:
1097 Opc = PPC::ADDI;
1098 MRI.setRegClass(SrcReg1, &PPC::GPRC_and_GPRC_NOR0RegClass);
1099 break;
1100 case PPC::ADD8:
1101 Opc = PPC::ADDI8;
1102 MRI.setRegClass(SrcReg1, &PPC::G8RC_and_G8RC_NOX0RegClass);
1103 break;
1104 case PPC::OR:
1105 Opc = PPC::ORI;
1106 break;
1107 case PPC::OR8:
1108 Opc = PPC::ORI8;
1109 break;
1110 case PPC::SUBF:
1111 if (Imm == -32768)
1112 UseImm = false;
1113 else {
1114 Opc = PPC::ADDI;
1115 MRI.setRegClass(SrcReg1, &PPC::GPRC_and_GPRC_NOR0RegClass);
1116 Imm = -Imm;
1117 }
1118 break;
1119 case PPC::SUBF8:
1120 if (Imm == -32768)
1121 UseImm = false;
1122 else {
1123 Opc = PPC::ADDI8;
1124 MRI.setRegClass(SrcReg1, &PPC::G8RC_and_G8RC_NOX0RegClass);
1125 Imm = -Imm;
1126 }
1127 break;
1128 }
1129
1130 if (UseImm) {
1131 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg)
1132 .addReg(SrcReg1).addImm(Imm);
1133 UpdateValueMap(I, ResultReg);
1134 return true;
1135 }
1136 }
1137 }
1138
1139 // Reg-reg case.
1140 unsigned SrcReg2 = getRegForValue(I->getOperand(1));
1141 if (SrcReg2 == 0) return false;
1142
1143 // Reverse operands for subtract-from.
1144 if (ISDOpcode == ISD::SUB)
1145 std::swap(SrcReg1, SrcReg2);
1146
1147 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg)
1148 .addReg(SrcReg1).addReg(SrcReg2);
1149 UpdateValueMap(I, ResultReg);
1150 return true;
1151}
1152
Bill Schmidt055d2072013-08-26 19:42:51 +00001153// Attempt to fast-select a return instruction.
1154bool PPCFastISel::SelectRet(const Instruction *I) {
1155
1156 if (!FuncInfo.CanLowerReturn)
1157 return false;
1158
1159 const ReturnInst *Ret = cast<ReturnInst>(I);
1160 const Function &F = *I->getParent()->getParent();
1161
1162 // Build a list of return value registers.
1163 SmallVector<unsigned, 4> RetRegs;
1164 CallingConv::ID CC = F.getCallingConv();
1165
1166 if (Ret->getNumOperands() > 0) {
1167 SmallVector<ISD::OutputArg, 4> Outs;
1168 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI);
1169
1170 // Analyze operands of the call, assigning locations to each operand.
1171 SmallVector<CCValAssign, 16> ValLocs;
1172 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, TM, ValLocs, *Context);
1173 CCInfo.AnalyzeReturn(Outs, RetCC_PPC64_ELF_FIS);
1174 const Value *RV = Ret->getOperand(0);
1175
1176 // FIXME: Only one output register for now.
1177 if (ValLocs.size() > 1)
1178 return false;
1179
1180 // Special case for returning a constant integer of any size.
1181 // Materialize the constant as an i64 and copy it to the return
1182 // register. This avoids an unnecessary extend or truncate.
1183 if (isa<ConstantInt>(*RV)) {
1184 const Constant *C = cast<Constant>(RV);
1185 unsigned SrcReg = PPCMaterializeInt(C, MVT::i64);
1186 unsigned RetReg = ValLocs[0].getLocReg();
1187 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1188 RetReg).addReg(SrcReg);
1189 RetRegs.push_back(RetReg);
1190
1191 } else {
1192 unsigned Reg = getRegForValue(RV);
1193
1194 if (Reg == 0)
1195 return false;
1196
1197 // Copy the result values into the output registers.
1198 for (unsigned i = 0; i < ValLocs.size(); ++i) {
1199
1200 CCValAssign &VA = ValLocs[i];
1201 assert(VA.isRegLoc() && "Can only return in registers!");
1202 RetRegs.push_back(VA.getLocReg());
1203 unsigned SrcReg = Reg + VA.getValNo();
1204
1205 EVT RVEVT = TLI.getValueType(RV->getType());
1206 if (!RVEVT.isSimple())
1207 return false;
1208 MVT RVVT = RVEVT.getSimpleVT();
1209 MVT DestVT = VA.getLocVT();
1210
1211 if (RVVT != DestVT && RVVT != MVT::i8 &&
1212 RVVT != MVT::i16 && RVVT != MVT::i32)
1213 return false;
1214
1215 if (RVVT != DestVT) {
1216 switch (VA.getLocInfo()) {
1217 default:
1218 llvm_unreachable("Unknown loc info!");
1219 case CCValAssign::Full:
1220 llvm_unreachable("Full value assign but types don't match?");
1221 case CCValAssign::AExt:
1222 case CCValAssign::ZExt: {
1223 const TargetRegisterClass *RC =
1224 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1225 unsigned TmpReg = createResultReg(RC);
1226 if (!PPCEmitIntExt(RVVT, SrcReg, DestVT, TmpReg, true))
1227 return false;
1228 SrcReg = TmpReg;
1229 break;
1230 }
1231 case CCValAssign::SExt: {
1232 const TargetRegisterClass *RC =
1233 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1234 unsigned TmpReg = createResultReg(RC);
1235 if (!PPCEmitIntExt(RVVT, SrcReg, DestVT, TmpReg, false))
1236 return false;
1237 SrcReg = TmpReg;
1238 break;
1239 }
1240 }
1241 }
1242
1243 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1244 TII.get(TargetOpcode::COPY), RetRegs[i])
1245 .addReg(SrcReg);
1246 }
1247 }
1248 }
1249
1250 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1251 TII.get(PPC::BLR));
1252
1253 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
1254 MIB.addReg(RetRegs[i], RegState::Implicit);
1255
1256 return true;
1257}
1258
Bill Schmidt3fad2bc2013-08-25 22:33:42 +00001259// Attempt to emit an integer extend of SrcReg into DestReg. Both
1260// signed and zero extensions are supported. Return false if we
Bill Schmidt055d2072013-08-26 19:42:51 +00001261// can't handle it.
Bill Schmidt3fad2bc2013-08-25 22:33:42 +00001262bool PPCFastISel::PPCEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1263 unsigned DestReg, bool IsZExt) {
Bill Schmidt055d2072013-08-26 19:42:51 +00001264 if (DestVT != MVT::i32 && DestVT != MVT::i64)
1265 return false;
1266 if (SrcVT != MVT::i8 && SrcVT != MVT::i16 && SrcVT != MVT::i32)
1267 return false;
1268
1269 // Signed extensions use EXTSB, EXTSH, EXTSW.
1270 if (!IsZExt) {
1271 unsigned Opc;
1272 if (SrcVT == MVT::i8)
1273 Opc = (DestVT == MVT::i32) ? PPC::EXTSB : PPC::EXTSB8_32_64;
1274 else if (SrcVT == MVT::i16)
1275 Opc = (DestVT == MVT::i32) ? PPC::EXTSH : PPC::EXTSH8_32_64;
1276 else {
1277 assert(DestVT == MVT::i64 && "Signed extend from i32 to i32??");
1278 Opc = PPC::EXTSW_32_64;
1279 }
1280 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), DestReg)
1281 .addReg(SrcReg);
1282
1283 // Unsigned 32-bit extensions use RLWINM.
1284 } else if (DestVT == MVT::i32) {
1285 unsigned MB;
1286 if (SrcVT == MVT::i8)
1287 MB = 24;
1288 else {
1289 assert(SrcVT == MVT::i16 && "Unsigned extend from i32 to i32??");
1290 MB = 16;
1291 }
1292 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(PPC::RLWINM),
1293 DestReg)
1294 .addReg(SrcReg).addImm(/*SH=*/0).addImm(MB).addImm(/*ME=*/31);
1295
1296 // Unsigned 64-bit extensions use RLDICL (with a 32-bit source).
1297 } else {
1298 unsigned MB;
1299 if (SrcVT == MVT::i8)
1300 MB = 56;
1301 else if (SrcVT == MVT::i16)
1302 MB = 48;
1303 else
1304 MB = 32;
1305 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1306 TII.get(PPC::RLDICL_32_64), DestReg)
1307 .addReg(SrcReg).addImm(/*SH=*/0).addImm(MB);
1308 }
1309
1310 return true;
Bill Schmidt3fad2bc2013-08-25 22:33:42 +00001311}
1312
1313// Attempt to fast-select an indirect branch instruction.
1314bool PPCFastISel::SelectIndirectBr(const Instruction *I) {
1315 unsigned AddrReg = getRegForValue(I->getOperand(0));
1316 if (AddrReg == 0)
1317 return false;
1318
1319 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(PPC::MTCTR8))
1320 .addReg(AddrReg);
1321 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(PPC::BCTR8));
1322
1323 const IndirectBrInst *IB = cast<IndirectBrInst>(I);
1324 for (unsigned i = 0, e = IB->getNumSuccessors(); i != e; ++i)
1325 FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[IB->getSuccessor(i)]);
1326
1327 return true;
1328}
1329
Bill Schmidte206efd32013-08-30 03:16:48 +00001330// Attempt to fast-select a compare instruction.
1331bool PPCFastISel::SelectCmp(const Instruction *I) {
1332 const CmpInst *CI = cast<CmpInst>(I);
1333 Optional<PPC::Predicate> OptPPCPred = getComparePred(CI->getPredicate());
1334 if (!OptPPCPred)
1335 return false;
1336
1337 unsigned CondReg = createResultReg(&PPC::CRRCRegClass);
1338
1339 if (!PPCEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned(),
1340 CondReg))
1341 return false;
1342
1343 UpdateValueMap(I, CondReg);
1344 return true;
1345}
1346
Bill Schmidt055d2072013-08-26 19:42:51 +00001347// Attempt to fast-select an integer extend instruction.
1348bool PPCFastISel::SelectIntExt(const Instruction *I) {
1349 Type *DestTy = I->getType();
1350 Value *Src = I->getOperand(0);
1351 Type *SrcTy = Src->getType();
1352
1353 bool IsZExt = isa<ZExtInst>(I);
1354 unsigned SrcReg = getRegForValue(Src);
1355 if (!SrcReg) return false;
1356
1357 EVT SrcEVT, DestEVT;
1358 SrcEVT = TLI.getValueType(SrcTy, true);
1359 DestEVT = TLI.getValueType(DestTy, true);
1360 if (!SrcEVT.isSimple())
1361 return false;
1362 if (!DestEVT.isSimple())
1363 return false;
1364
1365 MVT SrcVT = SrcEVT.getSimpleVT();
1366 MVT DestVT = DestEVT.getSimpleVT();
1367
1368 // If we know the register class needed for the result of this
1369 // instruction, use it. Otherwise pick the register class of the
1370 // correct size that does not contain X0/R0, since we don't know
1371 // whether downstream uses permit that assignment.
1372 unsigned AssignedReg = FuncInfo.ValueMap[I];
1373 const TargetRegisterClass *RC =
1374 (AssignedReg ? MRI.getRegClass(AssignedReg) :
1375 (DestVT == MVT::i64 ? &PPC::G8RC_and_G8RC_NOX0RegClass :
1376 &PPC::GPRC_and_GPRC_NOR0RegClass));
1377 unsigned ResultReg = createResultReg(RC);
1378
1379 if (!PPCEmitIntExt(SrcVT, SrcReg, DestVT, ResultReg, IsZExt))
1380 return false;
1381
1382 UpdateValueMap(I, ResultReg);
1383 return true;
1384}
1385
Bill Schmidt646cd792013-07-30 00:50:39 +00001386// Attempt to fast-select an instruction that wasn't handled by
Bill Schmidt3fad2bc2013-08-25 22:33:42 +00001387// the table-generated machinery.
Bill Schmidt646cd792013-07-30 00:50:39 +00001388bool PPCFastISel::TargetSelectInstruction(const Instruction *I) {
Bill Schmidt3fad2bc2013-08-25 22:33:42 +00001389
1390 switch (I->getOpcode()) {
Bill Schmidt72489682013-08-30 02:29:45 +00001391 case Instruction::Load:
1392 return SelectLoad(I);
1393 case Instruction::Store:
1394 return SelectStore(I);
Bill Schmidt3fad2bc2013-08-25 22:33:42 +00001395 case Instruction::Br:
1396 return SelectBranch(I);
1397 case Instruction::IndirectBr:
1398 return SelectIndirectBr(I);
Bill Schmidt9bc94272013-08-30 15:18:11 +00001399 case Instruction::FPExt:
1400 return SelectFPExt(I);
1401 case Instruction::FPTrunc:
1402 return SelectFPTrunc(I);
1403 case Instruction::SIToFP:
1404 return SelectIToFP(I, /*IsSigned*/ true);
1405 case Instruction::UIToFP:
1406 return SelectIToFP(I, /*IsSigned*/ false);
1407 case Instruction::FPToSI:
1408 return SelectFPToI(I, /*IsSigned*/ true);
1409 case Instruction::FPToUI:
1410 return SelectFPToI(I, /*IsSigned*/ false);
Bill Schmidt72489682013-08-30 02:29:45 +00001411 case Instruction::Add:
1412 return SelectBinaryIntOp(I, ISD::ADD);
1413 case Instruction::Or:
1414 return SelectBinaryIntOp(I, ISD::OR);
1415 case Instruction::Sub:
1416 return SelectBinaryIntOp(I, ISD::SUB);
Bill Schmidt055d2072013-08-26 19:42:51 +00001417 case Instruction::Ret:
1418 return SelectRet(I);
1419 case Instruction::ZExt:
1420 case Instruction::SExt:
1421 return SelectIntExt(I);
Bill Schmidt3fad2bc2013-08-25 22:33:42 +00001422 // Here add other flavors of Instruction::XXX that automated
1423 // cases don't catch. For example, switches are terminators
1424 // that aren't yet handled.
1425 default:
1426 break;
1427 }
1428 return false;
Bill Schmidt646cd792013-07-30 00:50:39 +00001429}
1430
1431// Materialize a floating-point constant into a register, and return
1432// the register number (or zero if we failed to handle it).
1433unsigned PPCFastISel::PPCMaterializeFP(const ConstantFP *CFP, MVT VT) {
1434 // No plans to handle long double here.
1435 if (VT != MVT::f32 && VT != MVT::f64)
1436 return 0;
1437
1438 // All FP constants are loaded from the constant pool.
1439 unsigned Align = TD.getPrefTypeAlignment(CFP->getType());
1440 assert(Align > 0 && "Unexpectedly missing alignment information!");
1441 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
1442 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
1443 CodeModel::Model CModel = TM.getCodeModel();
1444
1445 MachineMemOperand *MMO =
1446 FuncInfo.MF->getMachineMemOperand(
1447 MachinePointerInfo::getConstantPool(), MachineMemOperand::MOLoad,
1448 (VT == MVT::f32) ? 4 : 8, Align);
1449
Bill Schmidt3fad2bc2013-08-25 22:33:42 +00001450 unsigned Opc = (VT == MVT::f32) ? PPC::LFS : PPC::LFD;
1451 unsigned TmpReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
1452
1453 // For small code model, generate a LF[SD](0, LDtocCPT(Idx, X2)).
1454 if (CModel == CodeModel::Small || CModel == CodeModel::JITDefault) {
Bill Schmidt646cd792013-07-30 00:50:39 +00001455 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(PPC::LDtocCPT),
Bill Schmidt3fad2bc2013-08-25 22:33:42 +00001456 TmpReg)
1457 .addConstantPoolIndex(Idx).addReg(PPC::X2);
1458 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), DestReg)
1459 .addImm(0).addReg(TmpReg).addMemOperand(MMO);
1460 } else {
Bill Schmidt646cd792013-07-30 00:50:39 +00001461 // Otherwise we generate LF[SD](Idx[lo], ADDIStocHA(X2, Idx)).
Bill Schmidt646cd792013-07-30 00:50:39 +00001462 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(PPC::ADDIStocHA),
1463 TmpReg).addReg(PPC::X2).addConstantPoolIndex(Idx);
1464 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), DestReg)
1465 .addConstantPoolIndex(Idx, 0, PPCII::MO_TOC_LO)
1466 .addReg(TmpReg)
1467 .addMemOperand(MMO);
1468 }
1469
1470 return DestReg;
1471}
1472
Bill Schmidt72489682013-08-30 02:29:45 +00001473// Materialize the address of a global value into a register, and return
1474// the register number (or zero if we failed to handle it).
1475unsigned PPCFastISel::PPCMaterializeGV(const GlobalValue *GV, MVT VT) {
1476 assert(VT == MVT::i64 && "Non-address!");
1477 const TargetRegisterClass *RC = &PPC::G8RC_and_G8RC_NOX0RegClass;
1478 unsigned DestReg = createResultReg(RC);
1479
1480 // Global values may be plain old object addresses, TLS object
1481 // addresses, constant pool entries, or jump tables. How we generate
1482 // code for these may depend on small, medium, or large code model.
1483 CodeModel::Model CModel = TM.getCodeModel();
1484
1485 // FIXME: Jump tables are not yet required because fast-isel doesn't
1486 // handle switches; if that changes, we need them as well. For now,
1487 // what follows assumes everything's a generic (or TLS) global address.
1488 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
1489 if (!GVar) {
1490 // If GV is an alias, use the aliasee for determining thread-locality.
1491 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
1492 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false));
1493 assert((GVar || isa<Function>(GV)) && "Unexpected GV subclass!");
1494 }
1495
1496 // FIXME: We don't yet handle the complexity of TLS.
1497 bool IsTLS = GVar && GVar->isThreadLocal();
1498 if (IsTLS)
1499 return 0;
1500
1501 // For small code model, generate a simple TOC load.
1502 if (CModel == CodeModel::Small || CModel == CodeModel::JITDefault)
1503 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(PPC::LDtoc), DestReg)
1504 .addGlobalAddress(GV).addReg(PPC::X2);
1505 else {
1506 // If the address is an externally defined symbol, a symbol with
1507 // common or externally available linkage, a function address, or a
1508 // jump table address (not yet needed), or if we are generating code
1509 // for large code model, we generate:
1510 // LDtocL(GV, ADDIStocHA(%X2, GV))
1511 // Otherwise we generate:
1512 // ADDItocL(ADDIStocHA(%X2, GV), GV)
1513 // Either way, start with the ADDIStocHA:
1514 unsigned HighPartReg = createResultReg(RC);
1515 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(PPC::ADDIStocHA),
1516 HighPartReg).addReg(PPC::X2).addGlobalAddress(GV);
1517
1518 // !GVar implies a function address. An external variable is one
1519 // without an initializer.
1520 // If/when switches are implemented, jump tables should be handled
1521 // on the "if" path here.
1522 if (CModel == CodeModel::Large || !GVar || !GVar->hasInitializer() ||
1523 GVar->hasCommonLinkage() || GVar->hasAvailableExternallyLinkage())
1524 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(PPC::LDtocL),
1525 DestReg).addGlobalAddress(GV).addReg(HighPartReg);
1526 else
1527 // Otherwise generate the ADDItocL.
1528 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(PPC::ADDItocL),
1529 DestReg).addReg(HighPartReg).addGlobalAddress(GV);
1530 }
1531
1532 return DestReg;
1533}
1534
Bill Schmidt646cd792013-07-30 00:50:39 +00001535// Materialize a 32-bit integer constant into a register, and return
1536// the register number (or zero if we failed to handle it).
1537unsigned PPCFastISel::PPCMaterialize32BitInt(int64_t Imm,
1538 const TargetRegisterClass *RC) {
1539 unsigned Lo = Imm & 0xFFFF;
1540 unsigned Hi = (Imm >> 16) & 0xFFFF;
1541
1542 unsigned ResultReg = createResultReg(RC);
1543 bool IsGPRC = RC->hasSuperClassEq(&PPC::GPRCRegClass);
1544
1545 if (isInt<16>(Imm))
1546 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1547 TII.get(IsGPRC ? PPC::LI : PPC::LI8), ResultReg)
1548 .addImm(Imm);
1549 else if (Lo) {
1550 // Both Lo and Hi have nonzero bits.
1551 unsigned TmpReg = createResultReg(RC);
1552 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1553 TII.get(IsGPRC ? PPC::LIS : PPC::LIS8), TmpReg)
1554 .addImm(Hi);
1555 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1556 TII.get(IsGPRC ? PPC::ORI : PPC::ORI8), ResultReg)
1557 .addReg(TmpReg).addImm(Lo);
1558 } else
1559 // Just Hi bits.
1560 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1561 TII.get(IsGPRC ? PPC::LIS : PPC::LIS8), ResultReg)
1562 .addImm(Hi);
1563
1564 return ResultReg;
1565}
1566
1567// Materialize a 64-bit integer constant into a register, and return
1568// the register number (or zero if we failed to handle it).
1569unsigned PPCFastISel::PPCMaterialize64BitInt(int64_t Imm,
1570 const TargetRegisterClass *RC) {
1571 unsigned Remainder = 0;
1572 unsigned Shift = 0;
1573
1574 // If the value doesn't fit in 32 bits, see if we can shift it
1575 // so that it fits in 32 bits.
1576 if (!isInt<32>(Imm)) {
1577 Shift = countTrailingZeros<uint64_t>(Imm);
1578 int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
1579
1580 if (isInt<32>(ImmSh))
1581 Imm = ImmSh;
1582 else {
1583 Remainder = Imm;
1584 Shift = 32;
1585 Imm >>= 32;
1586 }
1587 }
1588
1589 // Handle the high-order 32 bits (if shifted) or the whole 32 bits
1590 // (if not shifted).
1591 unsigned TmpReg1 = PPCMaterialize32BitInt(Imm, RC);
1592 if (!Shift)
1593 return TmpReg1;
1594
1595 // If upper 32 bits were not zero, we've built them and need to shift
1596 // them into place.
1597 unsigned TmpReg2;
1598 if (Imm) {
1599 TmpReg2 = createResultReg(RC);
1600 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(PPC::RLDICR),
1601 TmpReg2).addReg(TmpReg1).addImm(Shift).addImm(63 - Shift);
1602 } else
1603 TmpReg2 = TmpReg1;
1604
1605 unsigned TmpReg3, Hi, Lo;
1606 if ((Hi = (Remainder >> 16) & 0xFFFF)) {
1607 TmpReg3 = createResultReg(RC);
1608 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(PPC::ORIS8),
1609 TmpReg3).addReg(TmpReg2).addImm(Hi);
1610 } else
1611 TmpReg3 = TmpReg2;
1612
1613 if ((Lo = Remainder & 0xFFFF)) {
1614 unsigned ResultReg = createResultReg(RC);
1615 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(PPC::ORI8),
1616 ResultReg).addReg(TmpReg3).addImm(Lo);
1617 return ResultReg;
1618 }
1619
1620 return TmpReg3;
1621}
1622
1623
1624// Materialize an integer constant into a register, and return
1625// the register number (or zero if we failed to handle it).
1626unsigned PPCFastISel::PPCMaterializeInt(const Constant *C, MVT VT) {
1627
1628 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16 &&
1629 VT != MVT::i8 && VT != MVT::i1)
1630 return 0;
1631
1632 const TargetRegisterClass *RC = ((VT == MVT::i64) ? &PPC::G8RCRegClass :
1633 &PPC::GPRCRegClass);
1634
1635 // If the constant is in range, use a load-immediate.
1636 const ConstantInt *CI = cast<ConstantInt>(C);
1637 if (isInt<16>(CI->getSExtValue())) {
1638 unsigned Opc = (VT == MVT::i64) ? PPC::LI8 : PPC::LI;
1639 unsigned ImmReg = createResultReg(RC);
1640 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ImmReg)
1641 .addImm(CI->getSExtValue());
1642 return ImmReg;
1643 }
1644
1645 // Construct the constant piecewise.
1646 int64_t Imm = CI->getZExtValue();
1647
1648 if (VT == MVT::i64)
1649 return PPCMaterialize64BitInt(Imm, RC);
1650 else if (VT == MVT::i32)
1651 return PPCMaterialize32BitInt(Imm, RC);
1652
1653 return 0;
1654}
1655
1656// Materialize a constant into a register, and return the register
1657// number (or zero if we failed to handle it).
1658unsigned PPCFastISel::TargetMaterializeConstant(const Constant *C) {
1659 EVT CEVT = TLI.getValueType(C->getType(), true);
1660
1661 // Only handle simple types.
1662 if (!CEVT.isSimple()) return 0;
1663 MVT VT = CEVT.getSimpleVT();
1664
1665 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
1666 return PPCMaterializeFP(CFP, VT);
Bill Schmidt72489682013-08-30 02:29:45 +00001667 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
1668 return PPCMaterializeGV(GV, VT);
Bill Schmidt646cd792013-07-30 00:50:39 +00001669 else if (isa<ConstantInt>(C))
1670 return PPCMaterializeInt(C, VT);
1671 // TBD: Global values.
1672
1673 return 0;
1674}
1675
1676// Materialize the address created by an alloca into a register, and
1677// return the register number (or zero if we failed to handle it). TBD.
1678unsigned PPCFastISel::TargetMaterializeAlloca(const AllocaInst *AI) {
1679 return AI && 0;
1680}
1681
Bill Schmidt72489682013-08-30 02:29:45 +00001682// Fold loads into extends when possible.
1683// FIXME: We can have multiple redundant extend/trunc instructions
1684// following a load. The folding only picks up one. Extend this
1685// to check subsequent instructions for the same pattern and remove
1686// them. Thus ResultReg should be the def reg for the last redundant
1687// instruction in a chain, and all intervening instructions can be
1688// removed from parent. Change test/CodeGen/PowerPC/fast-isel-fold.ll
1689// to add ELF64-NOT: rldicl to the appropriate tests when this works.
Bill Schmidt646cd792013-07-30 00:50:39 +00001690bool PPCFastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
1691 const LoadInst *LI) {
Bill Schmidt72489682013-08-30 02:29:45 +00001692 // Verify we have a legal type before going any further.
1693 MVT VT;
1694 if (!isLoadTypeLegal(LI->getType(), VT))
1695 return false;
1696
1697 // Combine load followed by zero- or sign-extend.
1698 bool IsZExt = false;
1699 switch(MI->getOpcode()) {
1700 default:
1701 return false;
1702
1703 case PPC::RLDICL:
1704 case PPC::RLDICL_32_64: {
1705 IsZExt = true;
1706 unsigned MB = MI->getOperand(3).getImm();
1707 if ((VT == MVT::i8 && MB <= 56) ||
1708 (VT == MVT::i16 && MB <= 48) ||
1709 (VT == MVT::i32 && MB <= 32))
1710 break;
1711 return false;
1712 }
1713
1714 case PPC::RLWINM:
1715 case PPC::RLWINM8: {
1716 IsZExt = true;
1717 unsigned MB = MI->getOperand(3).getImm();
1718 if ((VT == MVT::i8 && MB <= 24) ||
1719 (VT == MVT::i16 && MB <= 16))
1720 break;
1721 return false;
1722 }
1723
1724 case PPC::EXTSB:
1725 case PPC::EXTSB8:
1726 case PPC::EXTSB8_32_64:
1727 /* There is no sign-extending load-byte instruction. */
1728 return false;
1729
1730 case PPC::EXTSH:
1731 case PPC::EXTSH8:
1732 case PPC::EXTSH8_32_64: {
1733 if (VT != MVT::i16 && VT != MVT::i8)
1734 return false;
1735 break;
1736 }
1737
1738 case PPC::EXTSW:
1739 case PPC::EXTSW_32_64: {
1740 if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8)
1741 return false;
1742 break;
1743 }
1744 }
1745
1746 // See if we can handle this address.
1747 Address Addr;
1748 if (!PPCComputeAddress(LI->getOperand(0), Addr))
1749 return false;
1750
1751 unsigned ResultReg = MI->getOperand(0).getReg();
1752
1753 if (!PPCEmitLoad(VT, ResultReg, Addr, 0, IsZExt))
1754 return false;
1755
1756 MI->eraseFromParent();
1757 return true;
Bill Schmidt646cd792013-07-30 00:50:39 +00001758}
1759
1760// Attempt to lower call arguments in a faster way than done by
1761// the selection DAG code.
1762bool PPCFastISel::FastLowerArguments() {
1763 // Defer to normal argument lowering for now. It's reasonably
1764 // efficient. Consider doing something like ARM to handle the
1765 // case where all args fit in registers, no varargs, no float
1766 // or vector args.
1767 return false;
1768}
1769
Bill Schmidt3fad2bc2013-08-25 22:33:42 +00001770// Handle materializing integer constants into a register. This is not
1771// automatically generated for PowerPC, so must be explicitly created here.
1772unsigned PPCFastISel::FastEmit_i(MVT Ty, MVT VT, unsigned Opc, uint64_t Imm) {
1773
1774 if (Opc != ISD::Constant)
1775 return 0;
1776
1777 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16 &&
1778 VT != MVT::i8 && VT != MVT::i1)
1779 return 0;
1780
1781 const TargetRegisterClass *RC = ((VT == MVT::i64) ? &PPC::G8RCRegClass :
1782 &PPC::GPRCRegClass);
1783 if (VT == MVT::i64)
1784 return PPCMaterialize64BitInt(Imm, RC);
1785 else
1786 return PPCMaterialize32BitInt(Imm, RC);
1787}
1788
Bill Schmidt72489682013-08-30 02:29:45 +00001789// Override for ADDI and ADDI8 to set the correct register class
1790// on RHS operand 0. The automatic infrastructure naively assumes
1791// GPRC for i32 and G8RC for i64; the concept of "no R0" is lost
1792// for these cases. At the moment, none of the other automatically
1793// generated RI instructions require special treatment. However, once
1794// SelectSelect is implemented, "isel" requires similar handling.
1795//
1796// Also be conservative about the output register class. Avoid
1797// assigning R0 or X0 to the output register for GPRC and G8RC
1798// register classes, as any such result could be used in ADDI, etc.,
1799// where those regs have another meaning.
1800unsigned PPCFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
1801 const TargetRegisterClass *RC,
1802 unsigned Op0, bool Op0IsKill,
1803 uint64_t Imm) {
1804 if (MachineInstOpcode == PPC::ADDI)
1805 MRI.setRegClass(Op0, &PPC::GPRC_and_GPRC_NOR0RegClass);
1806 else if (MachineInstOpcode == PPC::ADDI8)
1807 MRI.setRegClass(Op0, &PPC::G8RC_and_G8RC_NOX0RegClass);
1808
1809 const TargetRegisterClass *UseRC =
1810 (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
1811 (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
1812
1813 return FastISel::FastEmitInst_ri(MachineInstOpcode, UseRC,
1814 Op0, Op0IsKill, Imm);
1815}
1816
1817// Override for instructions with one register operand to avoid use of
1818// R0/X0. The automatic infrastructure isn't aware of the context so
1819// we must be conservative.
1820unsigned PPCFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
1821 const TargetRegisterClass* RC,
1822 unsigned Op0, bool Op0IsKill) {
1823 const TargetRegisterClass *UseRC =
1824 (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
1825 (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
1826
1827 return FastISel::FastEmitInst_r(MachineInstOpcode, UseRC, Op0, Op0IsKill);
1828}
1829
1830// Override for instructions with two register operands to avoid use
1831// of R0/X0. The automatic infrastructure isn't aware of the context
1832// so we must be conservative.
1833unsigned PPCFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
1834 const TargetRegisterClass* RC,
1835 unsigned Op0, bool Op0IsKill,
1836 unsigned Op1, bool Op1IsKill) {
1837 const TargetRegisterClass *UseRC =
1838 (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
1839 (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
1840
1841 return FastISel::FastEmitInst_rr(MachineInstOpcode, UseRC, Op0, Op0IsKill,
1842 Op1, Op1IsKill);
1843}
1844
Bill Schmidt646cd792013-07-30 00:50:39 +00001845namespace llvm {
1846 // Create the fast instruction selector for PowerPC64 ELF.
1847 FastISel *PPC::createFastISel(FunctionLoweringInfo &FuncInfo,
1848 const TargetLibraryInfo *LibInfo) {
1849 const TargetMachine &TM = FuncInfo.MF->getTarget();
1850
1851 // Only available on 64-bit ELF for now.
1852 const PPCSubtarget *Subtarget = &TM.getSubtarget<PPCSubtarget>();
1853 if (Subtarget->isPPC64() && Subtarget->isSVR4ABI())
1854 return new PPCFastISel(FuncInfo, LibInfo);
1855
1856 return 0;
1857 }
1858}