blob: 07e3b64f7a837997226eeafe5dd7e07b6dea69c4 [file] [log] [blame]
Bill Schmidt0cf702f2013-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"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000018#include "MCTargetDesc/PPCPredicates.h"
Bill Schmidt0cf702f2013-07-30 00:50:39 +000019#include "PPCISelLowering.h"
20#include "PPCSubtarget.h"
21#include "PPCTargetMachine.h"
Bill Schmidt0cf702f2013-07-30 00:50:39 +000022#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
Bill Schmidteb8d6f72013-08-31 02:33:40 +000040//===----------------------------------------------------------------------===//
41//
42// TBD:
43// FastLowerArguments: Handle simple cases.
44// PPCMaterializeGV: Handle TLS.
45// SelectCall: Handle function pointers.
46// SelectCall: Handle multi-register return values.
47// SelectCall: Optimize away nops for local calls.
48// processCallArgs: Handle bit-converted arguments.
49// finishCall: Handle multi-register return values.
50// PPCComputeAddress: Handle parameter references as FrameIndex's.
51// PPCEmitCmp: Handle immediate as operand 1.
52// SelectCall: Handle small byval arguments.
53// SelectIntrinsicCall: Implement.
54// SelectSelect: Implement.
55// Consider factoring isTypeLegal into the base class.
56// Implement switches and jump tables.
57//
58//===----------------------------------------------------------------------===//
Bill Schmidt0cf702f2013-07-30 00:50:39 +000059using namespace llvm;
60
61namespace {
62
63typedef struct Address {
64 enum {
65 RegBase,
66 FrameIndexBase
67 } BaseType;
68
69 union {
70 unsigned Reg;
71 int FI;
72 } Base;
73
Bill Schmidtccecf262013-08-30 02:29:45 +000074 long Offset;
Bill Schmidt0cf702f2013-07-30 00:50:39 +000075
76 // Innocuous defaults for our address.
77 Address()
78 : BaseType(RegBase), Offset(0) {
79 Base.Reg = 0;
80 }
81} Address;
82
83class PPCFastISel : public FastISel {
84
85 const TargetMachine &TM;
86 const TargetInstrInfo &TII;
87 const TargetLowering &TLI;
88 const PPCSubtarget &PPCSubTarget;
89 LLVMContext *Context;
90
91 public:
92 explicit PPCFastISel(FunctionLoweringInfo &FuncInfo,
93 const TargetLibraryInfo *LibInfo)
94 : FastISel(FuncInfo, LibInfo),
95 TM(FuncInfo.MF->getTarget()),
96 TII(*TM.getInstrInfo()),
97 TLI(*TM.getTargetLowering()),
98 PPCSubTarget(
99 *((static_cast<const PPCTargetMachine *>(&TM))->getSubtargetImpl())
100 ),
101 Context(&FuncInfo.Fn->getContext()) { }
102
103 // Backend specific FastISel code.
104 private:
105 virtual bool TargetSelectInstruction(const Instruction *I);
106 virtual unsigned TargetMaterializeConstant(const Constant *C);
107 virtual unsigned TargetMaterializeAlloca(const AllocaInst *AI);
108 virtual bool tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
109 const LoadInst *LI);
110 virtual bool FastLowerArguments();
Bill Schmidt03008132013-08-25 22:33:42 +0000111 virtual unsigned FastEmit_i(MVT Ty, MVT RetTy, unsigned Opc, uint64_t Imm);
Bill Schmidtccecf262013-08-30 02:29:45 +0000112 virtual unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
113 const TargetRegisterClass *RC,
114 unsigned Op0, bool Op0IsKill,
115 uint64_t Imm);
116 virtual unsigned FastEmitInst_r(unsigned MachineInstOpcode,
117 const TargetRegisterClass *RC,
118 unsigned Op0, bool Op0IsKill);
119 virtual unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
120 const TargetRegisterClass *RC,
121 unsigned Op0, bool Op0IsKill,
122 unsigned Op1, bool Op1IsKill);
Bill Schmidt03008132013-08-25 22:33:42 +0000123
124 // Instruction selection routines.
125 private:
Bill Schmidtccecf262013-08-30 02:29:45 +0000126 bool SelectLoad(const Instruction *I);
127 bool SelectStore(const Instruction *I);
Bill Schmidt03008132013-08-25 22:33:42 +0000128 bool SelectBranch(const Instruction *I);
129 bool SelectIndirectBr(const Instruction *I);
Bill Schmidt057b04f2013-08-30 03:16:48 +0000130 bool SelectCmp(const Instruction *I);
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000131 bool SelectFPExt(const Instruction *I);
132 bool SelectFPTrunc(const Instruction *I);
133 bool SelectIToFP(const Instruction *I, bool IsSigned);
134 bool SelectFPToI(const Instruction *I, bool IsSigned);
Bill Schmidtccecf262013-08-30 02:29:45 +0000135 bool SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode);
Bill Schmidt8470b0f2013-08-30 22:18:55 +0000136 bool SelectCall(const Instruction *I);
Bill Schmidtd89f6782013-08-26 19:42:51 +0000137 bool SelectRet(const Instruction *I);
Bill Schmidt9d9510d2013-08-30 23:31:33 +0000138 bool SelectTrunc(const Instruction *I);
Bill Schmidtd89f6782013-08-26 19:42:51 +0000139 bool SelectIntExt(const Instruction *I);
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000140
141 // Utility routines.
142 private:
Bill Schmidtccecf262013-08-30 02:29:45 +0000143 bool isTypeLegal(Type *Ty, MVT &VT);
144 bool isLoadTypeLegal(Type *Ty, MVT &VT);
Bill Schmidt03008132013-08-25 22:33:42 +0000145 bool PPCEmitCmp(const Value *Src1Value, const Value *Src2Value,
146 bool isZExt, unsigned DestReg);
Bill Schmidtccecf262013-08-30 02:29:45 +0000147 bool PPCEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
148 const TargetRegisterClass *RC, bool IsZExt = true,
149 unsigned FP64LoadOpc = PPC::LFD);
150 bool PPCEmitStore(MVT VT, unsigned SrcReg, Address &Addr);
151 bool PPCComputeAddress(const Value *Obj, Address &Addr);
152 void PPCSimplifyAddress(Address &Addr, MVT VT, bool &UseOffset,
153 unsigned &IndexReg);
Bill Schmidt03008132013-08-25 22:33:42 +0000154 bool PPCEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
155 unsigned DestReg, bool IsZExt);
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000156 unsigned PPCMaterializeFP(const ConstantFP *CFP, MVT VT);
Bill Schmidtccecf262013-08-30 02:29:45 +0000157 unsigned PPCMaterializeGV(const GlobalValue *GV, MVT VT);
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000158 unsigned PPCMaterializeInt(const Constant *C, MVT VT);
159 unsigned PPCMaterialize32BitInt(int64_t Imm,
160 const TargetRegisterClass *RC);
161 unsigned PPCMaterialize64BitInt(int64_t Imm,
162 const TargetRegisterClass *RC);
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000163 unsigned PPCMoveToIntReg(const Instruction *I, MVT VT,
164 unsigned SrcReg, bool IsSigned);
165 unsigned PPCMoveToFPReg(MVT VT, unsigned SrcReg, bool IsSigned);
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000166
Bill Schmidtd89f6782013-08-26 19:42:51 +0000167 // Call handling routines.
168 private:
Bill Schmidt8470b0f2013-08-30 22:18:55 +0000169 bool processCallArgs(SmallVectorImpl<Value*> &Args,
170 SmallVectorImpl<unsigned> &ArgRegs,
171 SmallVectorImpl<MVT> &ArgVTs,
172 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
173 SmallVectorImpl<unsigned> &RegArgs,
174 CallingConv::ID CC,
175 unsigned &NumBytes,
176 bool IsVarArg);
177 void finishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
178 const Instruction *I, CallingConv::ID CC,
179 unsigned &NumBytes, bool IsVarArg);
Bill Schmidtd89f6782013-08-26 19:42:51 +0000180 CCAssignFn *usePPC32CCs(unsigned Flag);
181
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000182 private:
183 #include "PPCGenFastISel.inc"
184
185};
186
187} // end anonymous namespace
188
Bill Schmidtd89f6782013-08-26 19:42:51 +0000189#include "PPCGenCallingConv.inc"
190
191// Function whose sole purpose is to kill compiler warnings
192// stemming from unused functions included from PPCGenCallingConv.inc.
193CCAssignFn *PPCFastISel::usePPC32CCs(unsigned Flag) {
194 if (Flag == 1)
195 return CC_PPC32_SVR4;
196 else if (Flag == 2)
197 return CC_PPC32_SVR4_ByVal;
198 else if (Flag == 3)
199 return CC_PPC32_SVR4_VarArg;
200 else
201 return RetCC_PPC;
202}
203
Bill Schmidt03008132013-08-25 22:33:42 +0000204static Optional<PPC::Predicate> getComparePred(CmpInst::Predicate Pred) {
205 switch (Pred) {
206 // These are not representable with any single compare.
207 case CmpInst::FCMP_FALSE:
208 case CmpInst::FCMP_UEQ:
209 case CmpInst::FCMP_UGT:
210 case CmpInst::FCMP_UGE:
211 case CmpInst::FCMP_ULT:
212 case CmpInst::FCMP_ULE:
213 case CmpInst::FCMP_UNE:
214 case CmpInst::FCMP_TRUE:
215 default:
216 return Optional<PPC::Predicate>();
217
218 case CmpInst::FCMP_OEQ:
219 case CmpInst::ICMP_EQ:
220 return PPC::PRED_EQ;
221
222 case CmpInst::FCMP_OGT:
223 case CmpInst::ICMP_UGT:
224 case CmpInst::ICMP_SGT:
225 return PPC::PRED_GT;
226
227 case CmpInst::FCMP_OGE:
228 case CmpInst::ICMP_UGE:
229 case CmpInst::ICMP_SGE:
230 return PPC::PRED_GE;
231
232 case CmpInst::FCMP_OLT:
233 case CmpInst::ICMP_ULT:
234 case CmpInst::ICMP_SLT:
235 return PPC::PRED_LT;
236
237 case CmpInst::FCMP_OLE:
238 case CmpInst::ICMP_ULE:
239 case CmpInst::ICMP_SLE:
240 return PPC::PRED_LE;
241
242 case CmpInst::FCMP_ONE:
243 case CmpInst::ICMP_NE:
244 return PPC::PRED_NE;
245
246 case CmpInst::FCMP_ORD:
247 return PPC::PRED_NU;
248
249 case CmpInst::FCMP_UNO:
250 return PPC::PRED_UN;
251 }
252}
253
Bill Schmidtccecf262013-08-30 02:29:45 +0000254// Determine whether the type Ty is simple enough to be handled by
255// fast-isel, and return its equivalent machine type in VT.
256// FIXME: Copied directly from ARM -- factor into base class?
257bool PPCFastISel::isTypeLegal(Type *Ty, MVT &VT) {
258 EVT Evt = TLI.getValueType(Ty, true);
259
260 // Only handle simple types.
261 if (Evt == MVT::Other || !Evt.isSimple()) return false;
262 VT = Evt.getSimpleVT();
263
264 // Handle all legal types, i.e. a register that will directly hold this
265 // value.
266 return TLI.isTypeLegal(VT);
267}
268
269// Determine whether the type Ty is simple enough to be handled by
270// fast-isel as a load target, and return its equivalent machine type in VT.
271bool PPCFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
272 if (isTypeLegal(Ty, VT)) return true;
273
274 // If this is a type than can be sign or zero-extended to a basic operation
275 // go ahead and accept it now.
276 if (VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32) {
277 return true;
278 }
279
280 return false;
281}
282
283// Given a value Obj, create an Address object Addr that represents its
284// address. Return false if we can't handle it.
285bool PPCFastISel::PPCComputeAddress(const Value *Obj, Address &Addr) {
286 const User *U = NULL;
287 unsigned Opcode = Instruction::UserOp1;
288 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
289 // Don't walk into other basic blocks unless the object is an alloca from
290 // another block, otherwise it may not have a virtual register assigned.
291 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
292 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
293 Opcode = I->getOpcode();
294 U = I;
295 }
296 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
297 Opcode = C->getOpcode();
298 U = C;
299 }
300
301 switch (Opcode) {
302 default:
303 break;
304 case Instruction::BitCast:
305 // Look through bitcasts.
306 return PPCComputeAddress(U->getOperand(0), Addr);
307 case Instruction::IntToPtr:
308 // Look past no-op inttoptrs.
309 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
310 return PPCComputeAddress(U->getOperand(0), Addr);
311 break;
312 case Instruction::PtrToInt:
313 // Look past no-op ptrtoints.
314 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
315 return PPCComputeAddress(U->getOperand(0), Addr);
316 break;
317 case Instruction::GetElementPtr: {
318 Address SavedAddr = Addr;
319 long TmpOffset = Addr.Offset;
320
321 // Iterate through the GEP folding the constants into offsets where
322 // we can.
323 gep_type_iterator GTI = gep_type_begin(U);
324 for (User::const_op_iterator II = U->op_begin() + 1, IE = U->op_end();
325 II != IE; ++II, ++GTI) {
326 const Value *Op = *II;
327 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000328 const StructLayout *SL = DL.getStructLayout(STy);
Bill Schmidtccecf262013-08-30 02:29:45 +0000329 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
330 TmpOffset += SL->getElementOffset(Idx);
331 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000332 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
Bill Schmidtccecf262013-08-30 02:29:45 +0000333 for (;;) {
334 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
335 // Constant-offset addressing.
336 TmpOffset += CI->getSExtValue() * S;
337 break;
338 }
Bob Wilson9f3e6b22013-11-15 19:09:27 +0000339 if (canFoldAddIntoGEP(U, Op)) {
340 // A compatible add with a constant operand. Fold the constant.
Bill Schmidtccecf262013-08-30 02:29:45 +0000341 ConstantInt *CI =
342 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
343 TmpOffset += CI->getSExtValue() * S;
344 // Iterate on the other operand.
345 Op = cast<AddOperator>(Op)->getOperand(0);
346 continue;
347 }
348 // Unsupported
349 goto unsupported_gep;
350 }
351 }
352 }
353
354 // Try to grab the base operand now.
355 Addr.Offset = TmpOffset;
356 if (PPCComputeAddress(U->getOperand(0), Addr)) return true;
357
358 // We failed, restore everything and try the other options.
359 Addr = SavedAddr;
360
361 unsupported_gep:
362 break;
363 }
364 case Instruction::Alloca: {
365 const AllocaInst *AI = cast<AllocaInst>(Obj);
366 DenseMap<const AllocaInst*, int>::iterator SI =
367 FuncInfo.StaticAllocaMap.find(AI);
368 if (SI != FuncInfo.StaticAllocaMap.end()) {
369 Addr.BaseType = Address::FrameIndexBase;
370 Addr.Base.FI = SI->second;
371 return true;
372 }
373 break;
374 }
375 }
376
377 // FIXME: References to parameters fall through to the behavior
378 // below. They should be able to reference a frame index since
379 // they are stored to the stack, so we can get "ld rx, offset(r1)"
380 // instead of "addi ry, r1, offset / ld rx, 0(ry)". Obj will
381 // just contain the parameter. Try to handle this with a FI.
382
383 // Try to get this in a register if nothing else has worked.
384 if (Addr.Base.Reg == 0)
385 Addr.Base.Reg = getRegForValue(Obj);
386
387 // Prevent assignment of base register to X0, which is inappropriate
388 // for loads and stores alike.
389 if (Addr.Base.Reg != 0)
390 MRI.setRegClass(Addr.Base.Reg, &PPC::G8RC_and_G8RC_NOX0RegClass);
391
392 return Addr.Base.Reg != 0;
393}
394
395// Fix up some addresses that can't be used directly. For example, if
396// an offset won't fit in an instruction field, we may need to move it
397// into an index register.
398void PPCFastISel::PPCSimplifyAddress(Address &Addr, MVT VT, bool &UseOffset,
399 unsigned &IndexReg) {
400
401 // Check whether the offset fits in the instruction field.
402 if (!isInt<16>(Addr.Offset))
403 UseOffset = false;
404
405 // If this is a stack pointer and the offset needs to be simplified then
406 // put the alloca address into a register, set the base type back to
407 // register and continue. This should almost never happen.
408 if (!UseOffset && Addr.BaseType == Address::FrameIndexBase) {
409 unsigned ResultReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000410 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDI8),
Bill Schmidtccecf262013-08-30 02:29:45 +0000411 ResultReg).addFrameIndex(Addr.Base.FI).addImm(0);
412 Addr.Base.Reg = ResultReg;
413 Addr.BaseType = Address::RegBase;
414 }
415
416 if (!UseOffset) {
417 IntegerType *OffsetTy = ((VT == MVT::i32) ? Type::getInt32Ty(*Context)
418 : Type::getInt64Ty(*Context));
419 const ConstantInt *Offset =
420 ConstantInt::getSigned(OffsetTy, (int64_t)(Addr.Offset));
421 IndexReg = PPCMaterializeInt(Offset, MVT::i64);
422 assert(IndexReg && "Unexpected error in PPCMaterializeInt!");
423 }
424}
425
426// Emit a load instruction if possible, returning true if we succeeded,
427// otherwise false. See commentary below for how the register class of
428// the load is determined.
429bool PPCFastISel::PPCEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
430 const TargetRegisterClass *RC,
431 bool IsZExt, unsigned FP64LoadOpc) {
432 unsigned Opc;
433 bool UseOffset = true;
434
435 // If ResultReg is given, it determines the register class of the load.
436 // Otherwise, RC is the register class to use. If the result of the
437 // load isn't anticipated in this block, both may be zero, in which
438 // case we must make a conservative guess. In particular, don't assign
439 // R0 or X0 to the result register, as the result may be used in a load,
440 // store, add-immediate, or isel that won't permit this. (Though
441 // perhaps the spill and reload of live-exit values would handle this?)
442 const TargetRegisterClass *UseRC =
443 (ResultReg ? MRI.getRegClass(ResultReg) :
444 (RC ? RC :
445 (VT == MVT::f64 ? &PPC::F8RCRegClass :
446 (VT == MVT::f32 ? &PPC::F4RCRegClass :
447 (VT == MVT::i64 ? &PPC::G8RC_and_G8RC_NOX0RegClass :
448 &PPC::GPRC_and_GPRC_NOR0RegClass)))));
449
450 bool Is32BitInt = UseRC->hasSuperClassEq(&PPC::GPRCRegClass);
451
452 switch (VT.SimpleTy) {
453 default: // e.g., vector types not handled
454 return false;
455 case MVT::i8:
456 Opc = Is32BitInt ? PPC::LBZ : PPC::LBZ8;
457 break;
458 case MVT::i16:
459 Opc = (IsZExt ?
460 (Is32BitInt ? PPC::LHZ : PPC::LHZ8) :
461 (Is32BitInt ? PPC::LHA : PPC::LHA8));
462 break;
463 case MVT::i32:
464 Opc = (IsZExt ?
465 (Is32BitInt ? PPC::LWZ : PPC::LWZ8) :
466 (Is32BitInt ? PPC::LWA_32 : PPC::LWA));
467 if ((Opc == PPC::LWA || Opc == PPC::LWA_32) && ((Addr.Offset & 3) != 0))
468 UseOffset = false;
469 break;
470 case MVT::i64:
471 Opc = PPC::LD;
472 assert(UseRC->hasSuperClassEq(&PPC::G8RCRegClass) &&
473 "64-bit load with 32-bit target??");
474 UseOffset = ((Addr.Offset & 3) == 0);
475 break;
476 case MVT::f32:
477 Opc = PPC::LFS;
478 break;
479 case MVT::f64:
480 Opc = FP64LoadOpc;
481 break;
482 }
483
484 // If necessary, materialize the offset into a register and use
485 // the indexed form. Also handle stack pointers with special needs.
486 unsigned IndexReg = 0;
487 PPCSimplifyAddress(Addr, VT, UseOffset, IndexReg);
488 if (ResultReg == 0)
489 ResultReg = createResultReg(UseRC);
490
491 // Note: If we still have a frame index here, we know the offset is
492 // in range, as otherwise PPCSimplifyAddress would have converted it
493 // into a RegBase.
494 if (Addr.BaseType == Address::FrameIndexBase) {
495
496 MachineMemOperand *MMO =
497 FuncInfo.MF->getMachineMemOperand(
498 MachinePointerInfo::getFixedStack(Addr.Base.FI, Addr.Offset),
499 MachineMemOperand::MOLoad, MFI.getObjectSize(Addr.Base.FI),
500 MFI.getObjectAlignment(Addr.Base.FI));
501
Rafael Espindolaea09c592014-02-18 22:05:46 +0000502 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Bill Schmidtccecf262013-08-30 02:29:45 +0000503 .addImm(Addr.Offset).addFrameIndex(Addr.Base.FI).addMemOperand(MMO);
504
505 // Base reg with offset in range.
506 } else if (UseOffset) {
507
Rafael Espindolaea09c592014-02-18 22:05:46 +0000508 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Bill Schmidtccecf262013-08-30 02:29:45 +0000509 .addImm(Addr.Offset).addReg(Addr.Base.Reg);
510
511 // Indexed form.
512 } else {
513 // Get the RR opcode corresponding to the RI one. FIXME: It would be
514 // preferable to use the ImmToIdxMap from PPCRegisterInfo.cpp, but it
515 // is hard to get at.
516 switch (Opc) {
517 default: llvm_unreachable("Unexpected opcode!");
518 case PPC::LBZ: Opc = PPC::LBZX; break;
519 case PPC::LBZ8: Opc = PPC::LBZX8; break;
520 case PPC::LHZ: Opc = PPC::LHZX; break;
521 case PPC::LHZ8: Opc = PPC::LHZX8; break;
522 case PPC::LHA: Opc = PPC::LHAX; break;
523 case PPC::LHA8: Opc = PPC::LHAX8; break;
524 case PPC::LWZ: Opc = PPC::LWZX; break;
525 case PPC::LWZ8: Opc = PPC::LWZX8; break;
526 case PPC::LWA: Opc = PPC::LWAX; break;
527 case PPC::LWA_32: Opc = PPC::LWAX_32; break;
528 case PPC::LD: Opc = PPC::LDX; break;
529 case PPC::LFS: Opc = PPC::LFSX; break;
530 case PPC::LFD: Opc = PPC::LFDX; break;
531 }
Rafael Espindolaea09c592014-02-18 22:05:46 +0000532 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Bill Schmidtccecf262013-08-30 02:29:45 +0000533 .addReg(Addr.Base.Reg).addReg(IndexReg);
534 }
535
536 return true;
537}
538
539// Attempt to fast-select a load instruction.
540bool PPCFastISel::SelectLoad(const Instruction *I) {
541 // FIXME: No atomic loads are supported.
542 if (cast<LoadInst>(I)->isAtomic())
543 return false;
544
545 // Verify we have a legal type before going any further.
546 MVT VT;
547 if (!isLoadTypeLegal(I->getType(), VT))
548 return false;
549
550 // See if we can handle this address.
551 Address Addr;
552 if (!PPCComputeAddress(I->getOperand(0), Addr))
553 return false;
554
555 // Look at the currently assigned register for this instruction
556 // to determine the required register class. This is necessary
557 // to constrain RA from using R0/X0 when this is not legal.
558 unsigned AssignedReg = FuncInfo.ValueMap[I];
559 const TargetRegisterClass *RC =
560 AssignedReg ? MRI.getRegClass(AssignedReg) : 0;
561
562 unsigned ResultReg = 0;
563 if (!PPCEmitLoad(VT, ResultReg, Addr, RC))
564 return false;
565 UpdateValueMap(I, ResultReg);
566 return true;
567}
568
569// Emit a store instruction to store SrcReg at Addr.
570bool PPCFastISel::PPCEmitStore(MVT VT, unsigned SrcReg, Address &Addr) {
571 assert(SrcReg && "Nothing to store!");
572 unsigned Opc;
573 bool UseOffset = true;
574
575 const TargetRegisterClass *RC = MRI.getRegClass(SrcReg);
576 bool Is32BitInt = RC->hasSuperClassEq(&PPC::GPRCRegClass);
577
578 switch (VT.SimpleTy) {
579 default: // e.g., vector types not handled
580 return false;
581 case MVT::i8:
582 Opc = Is32BitInt ? PPC::STB : PPC::STB8;
583 break;
584 case MVT::i16:
585 Opc = Is32BitInt ? PPC::STH : PPC::STH8;
586 break;
587 case MVT::i32:
588 assert(Is32BitInt && "Not GPRC for i32??");
589 Opc = PPC::STW;
590 break;
591 case MVT::i64:
592 Opc = PPC::STD;
593 UseOffset = ((Addr.Offset & 3) == 0);
594 break;
595 case MVT::f32:
596 Opc = PPC::STFS;
597 break;
598 case MVT::f64:
599 Opc = PPC::STFD;
600 break;
601 }
602
603 // If necessary, materialize the offset into a register and use
604 // the indexed form. Also handle stack pointers with special needs.
605 unsigned IndexReg = 0;
606 PPCSimplifyAddress(Addr, VT, UseOffset, IndexReg);
607
608 // Note: If we still have a frame index here, we know the offset is
609 // in range, as otherwise PPCSimplifyAddress would have converted it
610 // into a RegBase.
611 if (Addr.BaseType == Address::FrameIndexBase) {
612 MachineMemOperand *MMO =
613 FuncInfo.MF->getMachineMemOperand(
614 MachinePointerInfo::getFixedStack(Addr.Base.FI, Addr.Offset),
615 MachineMemOperand::MOStore, MFI.getObjectSize(Addr.Base.FI),
616 MFI.getObjectAlignment(Addr.Base.FI));
617
Rafael Espindolaea09c592014-02-18 22:05:46 +0000618 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
619 .addReg(SrcReg)
620 .addImm(Addr.Offset)
621 .addFrameIndex(Addr.Base.FI)
622 .addMemOperand(MMO);
Bill Schmidtccecf262013-08-30 02:29:45 +0000623
624 // Base reg with offset in range.
Bill Schmidt72e3d55a2013-08-30 03:07:11 +0000625 } else if (UseOffset)
Rafael Espindolaea09c592014-02-18 22:05:46 +0000626 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
Bill Schmidtccecf262013-08-30 02:29:45 +0000627 .addReg(SrcReg).addImm(Addr.Offset).addReg(Addr.Base.Reg);
628
629 // Indexed form.
Bill Schmidt72e3d55a2013-08-30 03:07:11 +0000630 else {
Bill Schmidtccecf262013-08-30 02:29:45 +0000631 // Get the RR opcode corresponding to the RI one. FIXME: It would be
632 // preferable to use the ImmToIdxMap from PPCRegisterInfo.cpp, but it
633 // is hard to get at.
634 switch (Opc) {
635 default: llvm_unreachable("Unexpected opcode!");
636 case PPC::STB: Opc = PPC::STBX; break;
637 case PPC::STH : Opc = PPC::STHX; break;
638 case PPC::STW : Opc = PPC::STWX; break;
639 case PPC::STB8: Opc = PPC::STBX8; break;
640 case PPC::STH8: Opc = PPC::STHX8; break;
641 case PPC::STW8: Opc = PPC::STWX8; break;
642 case PPC::STD: Opc = PPC::STDX; break;
643 case PPC::STFS: Opc = PPC::STFSX; break;
644 case PPC::STFD: Opc = PPC::STFDX; break;
645 }
Rafael Espindolaea09c592014-02-18 22:05:46 +0000646 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
Bill Schmidtccecf262013-08-30 02:29:45 +0000647 .addReg(SrcReg).addReg(Addr.Base.Reg).addReg(IndexReg);
648 }
649
650 return true;
651}
652
653// Attempt to fast-select a store instruction.
654bool PPCFastISel::SelectStore(const Instruction *I) {
655 Value *Op0 = I->getOperand(0);
656 unsigned SrcReg = 0;
657
658 // FIXME: No atomics loads are supported.
659 if (cast<StoreInst>(I)->isAtomic())
660 return false;
661
662 // Verify we have a legal type before going any further.
663 MVT VT;
664 if (!isLoadTypeLegal(Op0->getType(), VT))
665 return false;
666
667 // Get the value to be stored into a register.
668 SrcReg = getRegForValue(Op0);
669 if (SrcReg == 0)
670 return false;
671
672 // See if we can handle this address.
673 Address Addr;
674 if (!PPCComputeAddress(I->getOperand(1), Addr))
675 return false;
676
677 if (!PPCEmitStore(VT, SrcReg, Addr))
678 return false;
679
680 return true;
681}
682
Bill Schmidt03008132013-08-25 22:33:42 +0000683// Attempt to fast-select a branch instruction.
684bool PPCFastISel::SelectBranch(const Instruction *I) {
685 const BranchInst *BI = cast<BranchInst>(I);
686 MachineBasicBlock *BrBB = FuncInfo.MBB;
687 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
688 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
689
690 // For now, just try the simplest case where it's fed by a compare.
691 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
692 Optional<PPC::Predicate> OptPPCPred = getComparePred(CI->getPredicate());
693 if (!OptPPCPred)
694 return false;
695
696 PPC::Predicate PPCPred = OptPPCPred.getValue();
697
698 // Take advantage of fall-through opportunities.
699 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
700 std::swap(TBB, FBB);
701 PPCPred = PPC::InvertPredicate(PPCPred);
702 }
703
704 unsigned CondReg = createResultReg(&PPC::CRRCRegClass);
705
706 if (!PPCEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned(),
707 CondReg))
708 return false;
709
Rafael Espindolaea09c592014-02-18 22:05:46 +0000710 BuildMI(*BrBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::BCC))
Bill Schmidt03008132013-08-25 22:33:42 +0000711 .addImm(PPCPred).addReg(CondReg).addMBB(TBB);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000712 FastEmitBranch(FBB, DbgLoc);
Bill Schmidt03008132013-08-25 22:33:42 +0000713 FuncInfo.MBB->addSuccessor(TBB);
714 return true;
715
716 } else if (const ConstantInt *CI =
717 dyn_cast<ConstantInt>(BI->getCondition())) {
718 uint64_t Imm = CI->getZExtValue();
719 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
Rafael Espindolaea09c592014-02-18 22:05:46 +0000720 FastEmitBranch(Target, DbgLoc);
Bill Schmidt03008132013-08-25 22:33:42 +0000721 return true;
722 }
723
724 // FIXME: ARM looks for a case where the block containing the compare
725 // has been split from the block containing the branch. If this happens,
726 // there is a vreg available containing the result of the compare. I'm
727 // not sure we can do much, as we've lost the predicate information with
728 // the compare instruction -- we have a 4-bit CR but don't know which bit
729 // to test here.
730 return false;
731}
732
733// Attempt to emit a compare of the two source values. Signed and unsigned
734// comparisons are supported. Return false if we can't handle it.
735bool PPCFastISel::PPCEmitCmp(const Value *SrcValue1, const Value *SrcValue2,
736 bool IsZExt, unsigned DestReg) {
737 Type *Ty = SrcValue1->getType();
738 EVT SrcEVT = TLI.getValueType(Ty, true);
739 if (!SrcEVT.isSimple())
740 return false;
741 MVT SrcVT = SrcEVT.getSimpleVT();
742
743 // See if operand 2 is an immediate encodeable in the compare.
744 // FIXME: Operands are not in canonical order at -O0, so an immediate
745 // operand in position 1 is a lost opportunity for now. We are
746 // similar to ARM in this regard.
747 long Imm = 0;
748 bool UseImm = false;
749
750 // Only 16-bit integer constants can be represented in compares for
751 // PowerPC. Others will be materialized into a register.
752 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(SrcValue2)) {
753 if (SrcVT == MVT::i64 || SrcVT == MVT::i32 || SrcVT == MVT::i16 ||
754 SrcVT == MVT::i8 || SrcVT == MVT::i1) {
755 const APInt &CIVal = ConstInt->getValue();
756 Imm = (IsZExt) ? (long)CIVal.getZExtValue() : (long)CIVal.getSExtValue();
757 if ((IsZExt && isUInt<16>(Imm)) || (!IsZExt && isInt<16>(Imm)))
758 UseImm = true;
759 }
760 }
761
762 unsigned CmpOpc;
763 bool NeedsExt = false;
764 switch (SrcVT.SimpleTy) {
765 default: return false;
766 case MVT::f32:
767 CmpOpc = PPC::FCMPUS;
768 break;
769 case MVT::f64:
770 CmpOpc = PPC::FCMPUD;
771 break;
772 case MVT::i1:
773 case MVT::i8:
774 case MVT::i16:
775 NeedsExt = true;
776 // Intentional fall-through.
777 case MVT::i32:
778 if (!UseImm)
779 CmpOpc = IsZExt ? PPC::CMPLW : PPC::CMPW;
780 else
781 CmpOpc = IsZExt ? PPC::CMPLWI : PPC::CMPWI;
782 break;
783 case MVT::i64:
784 if (!UseImm)
785 CmpOpc = IsZExt ? PPC::CMPLD : PPC::CMPD;
786 else
787 CmpOpc = IsZExt ? PPC::CMPLDI : PPC::CMPDI;
788 break;
789 }
790
791 unsigned SrcReg1 = getRegForValue(SrcValue1);
792 if (SrcReg1 == 0)
793 return false;
794
795 unsigned SrcReg2 = 0;
796 if (!UseImm) {
797 SrcReg2 = getRegForValue(SrcValue2);
798 if (SrcReg2 == 0)
799 return false;
800 }
801
802 if (NeedsExt) {
803 unsigned ExtReg = createResultReg(&PPC::GPRCRegClass);
804 if (!PPCEmitIntExt(SrcVT, SrcReg1, MVT::i32, ExtReg, IsZExt))
805 return false;
806 SrcReg1 = ExtReg;
807
808 if (!UseImm) {
809 unsigned ExtReg = createResultReg(&PPC::GPRCRegClass);
810 if (!PPCEmitIntExt(SrcVT, SrcReg2, MVT::i32, ExtReg, IsZExt))
811 return false;
812 SrcReg2 = ExtReg;
813 }
814 }
815
816 if (!UseImm)
Rafael Espindolaea09c592014-02-18 22:05:46 +0000817 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc), DestReg)
Bill Schmidt03008132013-08-25 22:33:42 +0000818 .addReg(SrcReg1).addReg(SrcReg2);
819 else
Rafael Espindolaea09c592014-02-18 22:05:46 +0000820 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc), DestReg)
Bill Schmidt03008132013-08-25 22:33:42 +0000821 .addReg(SrcReg1).addImm(Imm);
822
823 return true;
824}
825
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000826// Attempt to fast-select a floating-point extend instruction.
827bool PPCFastISel::SelectFPExt(const Instruction *I) {
828 Value *Src = I->getOperand(0);
829 EVT SrcVT = TLI.getValueType(Src->getType(), true);
830 EVT DestVT = TLI.getValueType(I->getType(), true);
831
832 if (SrcVT != MVT::f32 || DestVT != MVT::f64)
833 return false;
834
835 unsigned SrcReg = getRegForValue(Src);
836 if (!SrcReg)
837 return false;
838
839 // No code is generated for a FP extend.
840 UpdateValueMap(I, SrcReg);
841 return true;
842}
843
844// Attempt to fast-select a floating-point truncate instruction.
845bool PPCFastISel::SelectFPTrunc(const Instruction *I) {
846 Value *Src = I->getOperand(0);
847 EVT SrcVT = TLI.getValueType(Src->getType(), true);
848 EVT DestVT = TLI.getValueType(I->getType(), true);
849
850 if (SrcVT != MVT::f64 || DestVT != MVT::f32)
851 return false;
852
853 unsigned SrcReg = getRegForValue(Src);
854 if (!SrcReg)
855 return false;
856
857 // Round the result to single precision.
858 unsigned DestReg = createResultReg(&PPC::F4RCRegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000859 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::FRSP), DestReg)
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000860 .addReg(SrcReg);
861
862 UpdateValueMap(I, DestReg);
863 return true;
864}
865
866// Move an i32 or i64 value in a GPR to an f64 value in an FPR.
867// FIXME: When direct register moves are implemented (see PowerISA 2.08),
868// those should be used instead of moving via a stack slot when the
869// subtarget permits.
870// FIXME: The code here is sloppy for the 4-byte case. Can use a 4-byte
871// stack slot and 4-byte store/load sequence. Or just sext the 4-byte
872// case to 8 bytes which produces tighter code but wastes stack space.
873unsigned PPCFastISel::PPCMoveToFPReg(MVT SrcVT, unsigned SrcReg,
874 bool IsSigned) {
875
876 // If necessary, extend 32-bit int to 64-bit.
877 if (SrcVT == MVT::i32) {
878 unsigned TmpReg = createResultReg(&PPC::G8RCRegClass);
879 if (!PPCEmitIntExt(MVT::i32, SrcReg, MVT::i64, TmpReg, !IsSigned))
880 return 0;
881 SrcReg = TmpReg;
882 }
883
884 // Get a stack slot 8 bytes wide, aligned on an 8-byte boundary.
885 Address Addr;
886 Addr.BaseType = Address::FrameIndexBase;
887 Addr.Base.FI = MFI.CreateStackObject(8, 8, false);
888
889 // Store the value from the GPR.
890 if (!PPCEmitStore(MVT::i64, SrcReg, Addr))
891 return 0;
892
893 // Load the integer value into an FPR. The kind of load used depends
894 // on a number of conditions.
895 unsigned LoadOpc = PPC::LFD;
896
897 if (SrcVT == MVT::i32) {
898 Addr.Offset = 4;
899 if (!IsSigned)
900 LoadOpc = PPC::LFIWZX;
901 else if (PPCSubTarget.hasLFIWAX())
902 LoadOpc = PPC::LFIWAX;
903 }
904
905 const TargetRegisterClass *RC = &PPC::F8RCRegClass;
906 unsigned ResultReg = 0;
907 if (!PPCEmitLoad(MVT::f64, ResultReg, Addr, RC, !IsSigned, LoadOpc))
908 return 0;
909
910 return ResultReg;
911}
912
913// Attempt to fast-select an integer-to-floating-point conversion.
914bool PPCFastISel::SelectIToFP(const Instruction *I, bool IsSigned) {
915 MVT DstVT;
916 Type *DstTy = I->getType();
917 if (!isTypeLegal(DstTy, DstVT))
918 return false;
919
920 if (DstVT != MVT::f32 && DstVT != MVT::f64)
921 return false;
922
923 Value *Src = I->getOperand(0);
924 EVT SrcEVT = TLI.getValueType(Src->getType(), true);
925 if (!SrcEVT.isSimple())
926 return false;
927
928 MVT SrcVT = SrcEVT.getSimpleVT();
929
930 if (SrcVT != MVT::i8 && SrcVT != MVT::i16 &&
931 SrcVT != MVT::i32 && SrcVT != MVT::i64)
932 return false;
933
934 unsigned SrcReg = getRegForValue(Src);
935 if (SrcReg == 0)
936 return false;
937
938 // We can only lower an unsigned convert if we have the newer
939 // floating-point conversion operations.
940 if (!IsSigned && !PPCSubTarget.hasFPCVT())
941 return false;
942
943 // FIXME: For now we require the newer floating-point conversion operations
944 // (which are present only on P7 and A2 server models) when converting
945 // to single-precision float. Otherwise we have to generate a lot of
946 // fiddly code to avoid double rounding. If necessary, the fiddly code
947 // can be found in PPCTargetLowering::LowerINT_TO_FP().
948 if (DstVT == MVT::f32 && !PPCSubTarget.hasFPCVT())
949 return false;
950
951 // Extend the input if necessary.
952 if (SrcVT == MVT::i8 || SrcVT == MVT::i16) {
953 unsigned TmpReg = createResultReg(&PPC::G8RCRegClass);
954 if (!PPCEmitIntExt(SrcVT, SrcReg, MVT::i64, TmpReg, !IsSigned))
955 return false;
956 SrcVT = MVT::i64;
957 SrcReg = TmpReg;
958 }
959
960 // Move the integer value to an FPR.
961 unsigned FPReg = PPCMoveToFPReg(SrcVT, SrcReg, IsSigned);
962 if (FPReg == 0)
963 return false;
964
965 // Determine the opcode for the conversion.
966 const TargetRegisterClass *RC = &PPC::F8RCRegClass;
967 unsigned DestReg = createResultReg(RC);
968 unsigned Opc;
969
970 if (DstVT == MVT::f32)
971 Opc = IsSigned ? PPC::FCFIDS : PPC::FCFIDUS;
972 else
973 Opc = IsSigned ? PPC::FCFID : PPC::FCFIDU;
974
975 // Generate the convert.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000976 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000977 .addReg(FPReg);
978
979 UpdateValueMap(I, DestReg);
980 return true;
981}
982
983// Move the floating-point value in SrcReg into an integer destination
984// register, and return the register (or zero if we can't handle it).
985// FIXME: When direct register moves are implemented (see PowerISA 2.08),
986// those should be used instead of moving via a stack slot when the
987// subtarget permits.
988unsigned PPCFastISel::PPCMoveToIntReg(const Instruction *I, MVT VT,
989 unsigned SrcReg, bool IsSigned) {
990 // Get a stack slot 8 bytes wide, aligned on an 8-byte boundary.
991 // Note that if have STFIWX available, we could use a 4-byte stack
992 // slot for i32, but this being fast-isel we'll just go with the
993 // easiest code gen possible.
994 Address Addr;
995 Addr.BaseType = Address::FrameIndexBase;
996 Addr.Base.FI = MFI.CreateStackObject(8, 8, false);
997
998 // Store the value from the FPR.
999 if (!PPCEmitStore(MVT::f64, SrcReg, Addr))
1000 return 0;
1001
1002 // Reload it into a GPR. If we want an i32, modify the address
1003 // to have a 4-byte offset so we load from the right place.
1004 if (VT == MVT::i32)
1005 Addr.Offset = 4;
1006
1007 // Look at the currently assigned register for this instruction
1008 // to determine the required register class.
1009 unsigned AssignedReg = FuncInfo.ValueMap[I];
1010 const TargetRegisterClass *RC =
1011 AssignedReg ? MRI.getRegClass(AssignedReg) : 0;
1012
1013 unsigned ResultReg = 0;
1014 if (!PPCEmitLoad(VT, ResultReg, Addr, RC, !IsSigned))
1015 return 0;
1016
1017 return ResultReg;
1018}
1019
1020// Attempt to fast-select a floating-point-to-integer conversion.
1021bool PPCFastISel::SelectFPToI(const Instruction *I, bool IsSigned) {
1022 MVT DstVT, SrcVT;
1023 Type *DstTy = I->getType();
1024 if (!isTypeLegal(DstTy, DstVT))
1025 return false;
1026
1027 if (DstVT != MVT::i32 && DstVT != MVT::i64)
1028 return false;
1029
1030 Value *Src = I->getOperand(0);
1031 Type *SrcTy = Src->getType();
1032 if (!isTypeLegal(SrcTy, SrcVT))
1033 return false;
1034
1035 if (SrcVT != MVT::f32 && SrcVT != MVT::f64)
1036 return false;
1037
1038 unsigned SrcReg = getRegForValue(Src);
1039 if (SrcReg == 0)
1040 return false;
1041
1042 // Convert f32 to f64 if necessary. This is just a meaningless copy
1043 // to get the register class right. COPY_TO_REGCLASS is needed since
1044 // a COPY from F4RC to F8RC is converted to a F4RC-F4RC copy downstream.
1045 const TargetRegisterClass *InRC = MRI.getRegClass(SrcReg);
1046 if (InRC == &PPC::F4RCRegClass) {
1047 unsigned TmpReg = createResultReg(&PPC::F8RCRegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001048 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001049 TII.get(TargetOpcode::COPY_TO_REGCLASS), TmpReg)
1050 .addReg(SrcReg).addImm(PPC::F8RCRegClassID);
1051 SrcReg = TmpReg;
1052 }
1053
1054 // Determine the opcode for the conversion, which takes place
1055 // entirely within FPRs.
1056 unsigned DestReg = createResultReg(&PPC::F8RCRegClass);
1057 unsigned Opc;
1058
1059 if (DstVT == MVT::i32)
1060 if (IsSigned)
1061 Opc = PPC::FCTIWZ;
1062 else
1063 Opc = PPCSubTarget.hasFPCVT() ? PPC::FCTIWUZ : PPC::FCTIDZ;
1064 else
1065 Opc = IsSigned ? PPC::FCTIDZ : PPC::FCTIDUZ;
1066
1067 // Generate the convert.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001068 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001069 .addReg(SrcReg);
1070
1071 // Now move the integer value from a float register to an integer register.
1072 unsigned IntReg = PPCMoveToIntReg(I, DstVT, DestReg, IsSigned);
1073 if (IntReg == 0)
1074 return false;
1075
1076 UpdateValueMap(I, IntReg);
1077 return true;
1078}
1079
Bill Schmidtccecf262013-08-30 02:29:45 +00001080// Attempt to fast-select a binary integer operation that isn't already
1081// handled automatically.
1082bool PPCFastISel::SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode) {
1083 EVT DestVT = TLI.getValueType(I->getType(), true);
1084
1085 // We can get here in the case when we have a binary operation on a non-legal
1086 // type and the target independent selector doesn't know how to handle it.
1087 if (DestVT != MVT::i16 && DestVT != MVT::i8)
1088 return false;
1089
1090 // Look at the currently assigned register for this instruction
1091 // to determine the required register class. If there is no register,
1092 // make a conservative choice (don't assign R0).
1093 unsigned AssignedReg = FuncInfo.ValueMap[I];
1094 const TargetRegisterClass *RC =
1095 (AssignedReg ? MRI.getRegClass(AssignedReg) :
1096 &PPC::GPRC_and_GPRC_NOR0RegClass);
1097 bool IsGPRC = RC->hasSuperClassEq(&PPC::GPRCRegClass);
1098
1099 unsigned Opc;
1100 switch (ISDOpcode) {
1101 default: return false;
1102 case ISD::ADD:
1103 Opc = IsGPRC ? PPC::ADD4 : PPC::ADD8;
1104 break;
1105 case ISD::OR:
1106 Opc = IsGPRC ? PPC::OR : PPC::OR8;
1107 break;
1108 case ISD::SUB:
1109 Opc = IsGPRC ? PPC::SUBF : PPC::SUBF8;
1110 break;
1111 }
1112
1113 unsigned ResultReg = createResultReg(RC ? RC : &PPC::G8RCRegClass);
1114 unsigned SrcReg1 = getRegForValue(I->getOperand(0));
1115 if (SrcReg1 == 0) return false;
1116
1117 // Handle case of small immediate operand.
1118 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(I->getOperand(1))) {
1119 const APInt &CIVal = ConstInt->getValue();
1120 int Imm = (int)CIVal.getSExtValue();
1121 bool UseImm = true;
1122 if (isInt<16>(Imm)) {
1123 switch (Opc) {
1124 default:
1125 llvm_unreachable("Missing case!");
1126 case PPC::ADD4:
1127 Opc = PPC::ADDI;
1128 MRI.setRegClass(SrcReg1, &PPC::GPRC_and_GPRC_NOR0RegClass);
1129 break;
1130 case PPC::ADD8:
1131 Opc = PPC::ADDI8;
1132 MRI.setRegClass(SrcReg1, &PPC::G8RC_and_G8RC_NOX0RegClass);
1133 break;
1134 case PPC::OR:
1135 Opc = PPC::ORI;
1136 break;
1137 case PPC::OR8:
1138 Opc = PPC::ORI8;
1139 break;
1140 case PPC::SUBF:
1141 if (Imm == -32768)
1142 UseImm = false;
1143 else {
1144 Opc = PPC::ADDI;
1145 MRI.setRegClass(SrcReg1, &PPC::GPRC_and_GPRC_NOR0RegClass);
1146 Imm = -Imm;
1147 }
1148 break;
1149 case PPC::SUBF8:
1150 if (Imm == -32768)
1151 UseImm = false;
1152 else {
1153 Opc = PPC::ADDI8;
1154 MRI.setRegClass(SrcReg1, &PPC::G8RC_and_G8RC_NOX0RegClass);
1155 Imm = -Imm;
1156 }
1157 break;
1158 }
1159
1160 if (UseImm) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001161 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
1162 ResultReg)
1163 .addReg(SrcReg1)
1164 .addImm(Imm);
Bill Schmidtccecf262013-08-30 02:29:45 +00001165 UpdateValueMap(I, ResultReg);
1166 return true;
1167 }
1168 }
1169 }
1170
1171 // Reg-reg case.
1172 unsigned SrcReg2 = getRegForValue(I->getOperand(1));
1173 if (SrcReg2 == 0) return false;
1174
1175 // Reverse operands for subtract-from.
1176 if (ISDOpcode == ISD::SUB)
1177 std::swap(SrcReg1, SrcReg2);
1178
Rafael Espindolaea09c592014-02-18 22:05:46 +00001179 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Bill Schmidtccecf262013-08-30 02:29:45 +00001180 .addReg(SrcReg1).addReg(SrcReg2);
1181 UpdateValueMap(I, ResultReg);
1182 return true;
1183}
1184
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001185// Handle arguments to a call that we're attempting to fast-select.
1186// Return false if the arguments are too complex for us at the moment.
1187bool PPCFastISel::processCallArgs(SmallVectorImpl<Value*> &Args,
1188 SmallVectorImpl<unsigned> &ArgRegs,
1189 SmallVectorImpl<MVT> &ArgVTs,
1190 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1191 SmallVectorImpl<unsigned> &RegArgs,
1192 CallingConv::ID CC,
1193 unsigned &NumBytes,
1194 bool IsVarArg) {
1195 SmallVector<CCValAssign, 16> ArgLocs;
1196 CCState CCInfo(CC, IsVarArg, *FuncInfo.MF, TM, ArgLocs, *Context);
1197 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_PPC64_ELF_FIS);
1198
1199 // Bail out if we can't handle any of the arguments.
1200 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1201 CCValAssign &VA = ArgLocs[I];
1202 MVT ArgVT = ArgVTs[VA.getValNo()];
1203
1204 // Skip vector arguments for now, as well as long double and
1205 // uint128_t, and anything that isn't passed in a register.
1206 if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64 ||
1207 !VA.isRegLoc() || VA.needsCustom())
1208 return false;
1209
1210 // Skip bit-converted arguments for now.
1211 if (VA.getLocInfo() == CCValAssign::BCvt)
1212 return false;
1213 }
1214
1215 // Get a count of how many bytes are to be pushed onto the stack.
1216 NumBytes = CCInfo.getNextStackOffset();
1217
1218 // Issue CALLSEQ_START.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001219 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001220 TII.get(TII.getCallFrameSetupOpcode()))
1221 .addImm(NumBytes);
1222
1223 // Prepare to assign register arguments. Every argument uses up a
1224 // GPR protocol register even if it's passed in a floating-point
1225 // register.
1226 unsigned NextGPR = PPC::X3;
1227 unsigned NextFPR = PPC::F1;
1228
1229 // Process arguments.
1230 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1231 CCValAssign &VA = ArgLocs[I];
1232 unsigned Arg = ArgRegs[VA.getValNo()];
1233 MVT ArgVT = ArgVTs[VA.getValNo()];
1234
1235 // Handle argument promotion and bitcasts.
1236 switch (VA.getLocInfo()) {
1237 default:
1238 llvm_unreachable("Unknown loc info!");
1239 case CCValAssign::Full:
1240 break;
1241 case CCValAssign::SExt: {
1242 MVT DestVT = VA.getLocVT();
1243 const TargetRegisterClass *RC =
1244 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1245 unsigned TmpReg = createResultReg(RC);
1246 if (!PPCEmitIntExt(ArgVT, Arg, DestVT, TmpReg, /*IsZExt*/false))
1247 llvm_unreachable("Failed to emit a sext!");
1248 ArgVT = DestVT;
1249 Arg = TmpReg;
1250 break;
1251 }
1252 case CCValAssign::AExt:
1253 case CCValAssign::ZExt: {
1254 MVT DestVT = VA.getLocVT();
1255 const TargetRegisterClass *RC =
1256 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1257 unsigned TmpReg = createResultReg(RC);
1258 if (!PPCEmitIntExt(ArgVT, Arg, DestVT, TmpReg, /*IsZExt*/true))
1259 llvm_unreachable("Failed to emit a zext!");
1260 ArgVT = DestVT;
1261 Arg = TmpReg;
1262 break;
1263 }
1264 case CCValAssign::BCvt: {
1265 // FIXME: Not yet handled.
1266 llvm_unreachable("Should have bailed before getting here!");
1267 break;
1268 }
1269 }
1270
1271 // Copy this argument to the appropriate register.
1272 unsigned ArgReg;
1273 if (ArgVT == MVT::f32 || ArgVT == MVT::f64) {
1274 ArgReg = NextFPR++;
1275 ++NextGPR;
1276 } else
1277 ArgReg = NextGPR++;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001278
1279 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1280 TII.get(TargetOpcode::COPY), ArgReg).addReg(Arg);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001281 RegArgs.push_back(ArgReg);
1282 }
1283
1284 return true;
1285}
1286
1287// For a call that we've determined we can fast-select, finish the
1288// call sequence and generate a copy to obtain the return value (if any).
1289void PPCFastISel::finishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
1290 const Instruction *I, CallingConv::ID CC,
1291 unsigned &NumBytes, bool IsVarArg) {
1292 // Issue CallSEQ_END.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001293 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001294 TII.get(TII.getCallFrameDestroyOpcode()))
1295 .addImm(NumBytes).addImm(0);
1296
1297 // Next, generate a copy to obtain the return value.
1298 // FIXME: No multi-register return values yet, though I don't foresee
1299 // any real difficulties there.
1300 if (RetVT != MVT::isVoid) {
1301 SmallVector<CCValAssign, 16> RVLocs;
1302 CCState CCInfo(CC, IsVarArg, *FuncInfo.MF, TM, RVLocs, *Context);
1303 CCInfo.AnalyzeCallResult(RetVT, RetCC_PPC64_ELF_FIS);
1304 CCValAssign &VA = RVLocs[0];
1305 assert(RVLocs.size() == 1 && "No support for multi-reg return values!");
1306 assert(VA.isRegLoc() && "Can only return in registers!");
1307
1308 MVT DestVT = VA.getValVT();
1309 MVT CopyVT = DestVT;
1310
1311 // Ints smaller than a register still arrive in a full 64-bit
1312 // register, so make sure we recognize this.
1313 if (RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32)
1314 CopyVT = MVT::i64;
1315
1316 unsigned SourcePhysReg = VA.getLocReg();
Bill Schmidt0954ea12013-08-30 23:25:30 +00001317 unsigned ResultReg = 0;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001318
1319 if (RetVT == CopyVT) {
1320 const TargetRegisterClass *CpyRC = TLI.getRegClassFor(CopyVT);
1321 ResultReg = createResultReg(CpyRC);
1322
Rafael Espindolaea09c592014-02-18 22:05:46 +00001323 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001324 TII.get(TargetOpcode::COPY), ResultReg)
1325 .addReg(SourcePhysReg);
1326
1327 // If necessary, round the floating result to single precision.
1328 } else if (CopyVT == MVT::f64) {
1329 ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
Rafael Espindolaea09c592014-02-18 22:05:46 +00001330 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::FRSP),
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001331 ResultReg).addReg(SourcePhysReg);
1332
1333 // If only the low half of a general register is needed, generate
1334 // a GPRC copy instead of a G8RC copy. (EXTRACT_SUBREG can't be
1335 // used along the fast-isel path (not lowered), and downstream logic
1336 // also doesn't like a direct subreg copy on a physical reg.)
1337 } else if (RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32) {
1338 ResultReg = createResultReg(&PPC::GPRCRegClass);
1339 // Convert physical register from G8RC to GPRC.
1340 SourcePhysReg -= PPC::X0 - PPC::R0;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001341 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001342 TII.get(TargetOpcode::COPY), ResultReg)
1343 .addReg(SourcePhysReg);
1344 }
1345
Bill Schmidt0954ea12013-08-30 23:25:30 +00001346 assert(ResultReg && "ResultReg unset!");
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001347 UsedRegs.push_back(SourcePhysReg);
1348 UpdateValueMap(I, ResultReg);
1349 }
1350}
1351
1352// Attempt to fast-select a call instruction.
1353bool PPCFastISel::SelectCall(const Instruction *I) {
1354 const CallInst *CI = cast<CallInst>(I);
1355 const Value *Callee = CI->getCalledValue();
1356
1357 // Can't handle inline asm.
1358 if (isa<InlineAsm>(Callee))
1359 return false;
1360
1361 // Allow SelectionDAG isel to handle tail calls.
1362 if (CI->isTailCall())
1363 return false;
1364
1365 // Obtain calling convention.
1366 ImmutableCallSite CS(CI);
1367 CallingConv::ID CC = CS.getCallingConv();
1368
1369 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1370 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1371 bool IsVarArg = FTy->isVarArg();
1372
1373 // Not ready for varargs yet.
1374 if (IsVarArg)
1375 return false;
1376
1377 // Handle simple calls for now, with legal return types and
1378 // those that can be extended.
1379 Type *RetTy = I->getType();
1380 MVT RetVT;
1381 if (RetTy->isVoidTy())
1382 RetVT = MVT::isVoid;
1383 else if (!isTypeLegal(RetTy, RetVT) && RetVT != MVT::i16 &&
1384 RetVT != MVT::i8)
1385 return false;
1386
1387 // FIXME: No multi-register return values yet.
1388 if (RetVT != MVT::isVoid && RetVT != MVT::i8 && RetVT != MVT::i16 &&
1389 RetVT != MVT::i32 && RetVT != MVT::i64 && RetVT != MVT::f32 &&
1390 RetVT != MVT::f64) {
1391 SmallVector<CCValAssign, 16> RVLocs;
1392 CCState CCInfo(CC, IsVarArg, *FuncInfo.MF, TM, RVLocs, *Context);
1393 CCInfo.AnalyzeCallResult(RetVT, RetCC_PPC64_ELF_FIS);
1394 if (RVLocs.size() > 1)
1395 return false;
1396 }
1397
1398 // Bail early if more than 8 arguments, as we only currently
1399 // handle arguments passed in registers.
1400 unsigned NumArgs = CS.arg_size();
1401 if (NumArgs > 8)
1402 return false;
1403
1404 // Set up the argument vectors.
1405 SmallVector<Value*, 8> Args;
1406 SmallVector<unsigned, 8> ArgRegs;
1407 SmallVector<MVT, 8> ArgVTs;
1408 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1409
1410 Args.reserve(NumArgs);
1411 ArgRegs.reserve(NumArgs);
1412 ArgVTs.reserve(NumArgs);
1413 ArgFlags.reserve(NumArgs);
1414
1415 for (ImmutableCallSite::arg_iterator II = CS.arg_begin(), IE = CS.arg_end();
1416 II != IE; ++II) {
1417 // FIXME: ARM does something for intrinsic calls here, check into that.
1418
1419 unsigned AttrIdx = II - CS.arg_begin() + 1;
1420
1421 // Only handle easy calls for now. It would be reasonably easy
1422 // to handle <= 8-byte structures passed ByVal in registers, but we
1423 // have to ensure they are right-justified in the register.
1424 if (CS.paramHasAttr(AttrIdx, Attribute::InReg) ||
1425 CS.paramHasAttr(AttrIdx, Attribute::StructRet) ||
1426 CS.paramHasAttr(AttrIdx, Attribute::Nest) ||
1427 CS.paramHasAttr(AttrIdx, Attribute::ByVal))
1428 return false;
1429
1430 ISD::ArgFlagsTy Flags;
1431 if (CS.paramHasAttr(AttrIdx, Attribute::SExt))
1432 Flags.setSExt();
1433 if (CS.paramHasAttr(AttrIdx, Attribute::ZExt))
1434 Flags.setZExt();
1435
1436 Type *ArgTy = (*II)->getType();
1437 MVT ArgVT;
1438 if (!isTypeLegal(ArgTy, ArgVT) && ArgVT != MVT::i16 && ArgVT != MVT::i8)
1439 return false;
1440
1441 if (ArgVT.isVector())
1442 return false;
1443
1444 unsigned Arg = getRegForValue(*II);
1445 if (Arg == 0)
1446 return false;
1447
Rafael Espindolaea09c592014-02-18 22:05:46 +00001448 unsigned OriginalAlignment = DL.getABITypeAlignment(ArgTy);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001449 Flags.setOrigAlign(OriginalAlignment);
1450
1451 Args.push_back(*II);
1452 ArgRegs.push_back(Arg);
1453 ArgVTs.push_back(ArgVT);
1454 ArgFlags.push_back(Flags);
1455 }
1456
1457 // Process the arguments.
1458 SmallVector<unsigned, 8> RegArgs;
1459 unsigned NumBytes;
1460
1461 if (!processCallArgs(Args, ArgRegs, ArgVTs, ArgFlags,
1462 RegArgs, CC, NumBytes, IsVarArg))
1463 return false;
1464
1465 // FIXME: No handling for function pointers yet. This requires
1466 // implementing the function descriptor (OPD) setup.
1467 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
1468 if (!GV)
1469 return false;
1470
1471 // Build direct call with NOP for TOC restore.
1472 // FIXME: We can and should optimize away the NOP for local calls.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001473 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001474 TII.get(PPC::BL8_NOP));
1475 // Add callee.
1476 MIB.addGlobalAddress(GV);
1477
1478 // Add implicit physical register uses to the call.
1479 for (unsigned II = 0, IE = RegArgs.size(); II != IE; ++II)
1480 MIB.addReg(RegArgs[II], RegState::Implicit);
1481
1482 // Add a register mask with the call-preserved registers. Proper
1483 // defs for return values will be added by setPhysRegsDeadExcept().
1484 MIB.addRegMask(TRI.getCallPreservedMask(CC));
1485
1486 // Finish off the call including any return values.
1487 SmallVector<unsigned, 4> UsedRegs;
1488 finishCall(RetVT, UsedRegs, I, CC, NumBytes, IsVarArg);
1489
1490 // Set all unused physregs defs as dead.
1491 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
1492
1493 return true;
1494}
1495
Bill Schmidtd89f6782013-08-26 19:42:51 +00001496// Attempt to fast-select a return instruction.
1497bool PPCFastISel::SelectRet(const Instruction *I) {
1498
1499 if (!FuncInfo.CanLowerReturn)
1500 return false;
1501
1502 const ReturnInst *Ret = cast<ReturnInst>(I);
1503 const Function &F = *I->getParent()->getParent();
1504
1505 // Build a list of return value registers.
1506 SmallVector<unsigned, 4> RetRegs;
1507 CallingConv::ID CC = F.getCallingConv();
1508
1509 if (Ret->getNumOperands() > 0) {
1510 SmallVector<ISD::OutputArg, 4> Outs;
1511 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI);
1512
1513 // Analyze operands of the call, assigning locations to each operand.
1514 SmallVector<CCValAssign, 16> ValLocs;
1515 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, TM, ValLocs, *Context);
1516 CCInfo.AnalyzeReturn(Outs, RetCC_PPC64_ELF_FIS);
1517 const Value *RV = Ret->getOperand(0);
1518
1519 // FIXME: Only one output register for now.
1520 if (ValLocs.size() > 1)
1521 return false;
1522
1523 // Special case for returning a constant integer of any size.
1524 // Materialize the constant as an i64 and copy it to the return
1525 // register. This avoids an unnecessary extend or truncate.
1526 if (isa<ConstantInt>(*RV)) {
1527 const Constant *C = cast<Constant>(RV);
1528 unsigned SrcReg = PPCMaterializeInt(C, MVT::i64);
1529 unsigned RetReg = ValLocs[0].getLocReg();
Rafael Espindolaea09c592014-02-18 22:05:46 +00001530 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1531 TII.get(TargetOpcode::COPY), RetReg).addReg(SrcReg);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001532 RetRegs.push_back(RetReg);
1533
1534 } else {
1535 unsigned Reg = getRegForValue(RV);
1536
1537 if (Reg == 0)
1538 return false;
1539
1540 // Copy the result values into the output registers.
1541 for (unsigned i = 0; i < ValLocs.size(); ++i) {
1542
1543 CCValAssign &VA = ValLocs[i];
1544 assert(VA.isRegLoc() && "Can only return in registers!");
1545 RetRegs.push_back(VA.getLocReg());
1546 unsigned SrcReg = Reg + VA.getValNo();
1547
1548 EVT RVEVT = TLI.getValueType(RV->getType());
1549 if (!RVEVT.isSimple())
1550 return false;
1551 MVT RVVT = RVEVT.getSimpleVT();
1552 MVT DestVT = VA.getLocVT();
1553
1554 if (RVVT != DestVT && RVVT != MVT::i8 &&
1555 RVVT != MVT::i16 && RVVT != MVT::i32)
1556 return false;
1557
1558 if (RVVT != DestVT) {
1559 switch (VA.getLocInfo()) {
1560 default:
1561 llvm_unreachable("Unknown loc info!");
1562 case CCValAssign::Full:
1563 llvm_unreachable("Full value assign but types don't match?");
1564 case CCValAssign::AExt:
1565 case CCValAssign::ZExt: {
1566 const TargetRegisterClass *RC =
1567 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1568 unsigned TmpReg = createResultReg(RC);
1569 if (!PPCEmitIntExt(RVVT, SrcReg, DestVT, TmpReg, true))
1570 return false;
1571 SrcReg = TmpReg;
1572 break;
1573 }
1574 case CCValAssign::SExt: {
1575 const TargetRegisterClass *RC =
1576 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1577 unsigned TmpReg = createResultReg(RC);
1578 if (!PPCEmitIntExt(RVVT, SrcReg, DestVT, TmpReg, false))
1579 return false;
1580 SrcReg = TmpReg;
1581 break;
1582 }
1583 }
1584 }
1585
Rafael Espindolaea09c592014-02-18 22:05:46 +00001586 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidtd89f6782013-08-26 19:42:51 +00001587 TII.get(TargetOpcode::COPY), RetRegs[i])
1588 .addReg(SrcReg);
1589 }
1590 }
1591 }
1592
Rafael Espindolaea09c592014-02-18 22:05:46 +00001593 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidtd89f6782013-08-26 19:42:51 +00001594 TII.get(PPC::BLR));
1595
1596 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
1597 MIB.addReg(RetRegs[i], RegState::Implicit);
1598
1599 return true;
1600}
1601
Bill Schmidt03008132013-08-25 22:33:42 +00001602// Attempt to emit an integer extend of SrcReg into DestReg. Both
1603// signed and zero extensions are supported. Return false if we
Bill Schmidtd89f6782013-08-26 19:42:51 +00001604// can't handle it.
Bill Schmidt03008132013-08-25 22:33:42 +00001605bool PPCFastISel::PPCEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1606 unsigned DestReg, bool IsZExt) {
Bill Schmidtd89f6782013-08-26 19:42:51 +00001607 if (DestVT != MVT::i32 && DestVT != MVT::i64)
1608 return false;
1609 if (SrcVT != MVT::i8 && SrcVT != MVT::i16 && SrcVT != MVT::i32)
1610 return false;
1611
1612 // Signed extensions use EXTSB, EXTSH, EXTSW.
1613 if (!IsZExt) {
1614 unsigned Opc;
1615 if (SrcVT == MVT::i8)
1616 Opc = (DestVT == MVT::i32) ? PPC::EXTSB : PPC::EXTSB8_32_64;
1617 else if (SrcVT == MVT::i16)
1618 Opc = (DestVT == MVT::i32) ? PPC::EXTSH : PPC::EXTSH8_32_64;
1619 else {
1620 assert(DestVT == MVT::i64 && "Signed extend from i32 to i32??");
1621 Opc = PPC::EXTSW_32_64;
1622 }
Rafael Espindolaea09c592014-02-18 22:05:46 +00001623 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidtd89f6782013-08-26 19:42:51 +00001624 .addReg(SrcReg);
1625
1626 // Unsigned 32-bit extensions use RLWINM.
1627 } else if (DestVT == MVT::i32) {
1628 unsigned MB;
1629 if (SrcVT == MVT::i8)
1630 MB = 24;
1631 else {
1632 assert(SrcVT == MVT::i16 && "Unsigned extend from i32 to i32??");
1633 MB = 16;
1634 }
Rafael Espindolaea09c592014-02-18 22:05:46 +00001635 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::RLWINM),
Bill Schmidtd89f6782013-08-26 19:42:51 +00001636 DestReg)
1637 .addReg(SrcReg).addImm(/*SH=*/0).addImm(MB).addImm(/*ME=*/31);
1638
1639 // Unsigned 64-bit extensions use RLDICL (with a 32-bit source).
1640 } else {
1641 unsigned MB;
1642 if (SrcVT == MVT::i8)
1643 MB = 56;
1644 else if (SrcVT == MVT::i16)
1645 MB = 48;
1646 else
1647 MB = 32;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001648 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidtd89f6782013-08-26 19:42:51 +00001649 TII.get(PPC::RLDICL_32_64), DestReg)
1650 .addReg(SrcReg).addImm(/*SH=*/0).addImm(MB);
1651 }
1652
1653 return true;
Bill Schmidt03008132013-08-25 22:33:42 +00001654}
1655
1656// Attempt to fast-select an indirect branch instruction.
1657bool PPCFastISel::SelectIndirectBr(const Instruction *I) {
1658 unsigned AddrReg = getRegForValue(I->getOperand(0));
1659 if (AddrReg == 0)
1660 return false;
1661
Rafael Espindolaea09c592014-02-18 22:05:46 +00001662 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::MTCTR8))
Bill Schmidt03008132013-08-25 22:33:42 +00001663 .addReg(AddrReg);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001664 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::BCTR8));
Bill Schmidt03008132013-08-25 22:33:42 +00001665
1666 const IndirectBrInst *IB = cast<IndirectBrInst>(I);
1667 for (unsigned i = 0, e = IB->getNumSuccessors(); i != e; ++i)
1668 FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[IB->getSuccessor(i)]);
1669
1670 return true;
1671}
1672
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001673// Attempt to fast-select an integer truncate instruction.
1674bool PPCFastISel::SelectTrunc(const Instruction *I) {
1675 Value *Src = I->getOperand(0);
1676 EVT SrcVT = TLI.getValueType(Src->getType(), true);
1677 EVT DestVT = TLI.getValueType(I->getType(), true);
1678
1679 if (SrcVT != MVT::i64 && SrcVT != MVT::i32 && SrcVT != MVT::i16)
1680 return false;
1681
1682 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8)
1683 return false;
1684
1685 unsigned SrcReg = getRegForValue(Src);
1686 if (!SrcReg)
1687 return false;
1688
1689 // The only interesting case is when we need to switch register classes.
1690 if (SrcVT == MVT::i64) {
1691 unsigned ResultReg = createResultReg(&PPC::GPRCRegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001692 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1693 TII.get(TargetOpcode::COPY),
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001694 ResultReg).addReg(SrcReg, 0, PPC::sub_32);
1695 SrcReg = ResultReg;
1696 }
1697
1698 UpdateValueMap(I, SrcReg);
1699 return true;
1700}
1701
Bill Schmidtd89f6782013-08-26 19:42:51 +00001702// Attempt to fast-select an integer extend instruction.
1703bool PPCFastISel::SelectIntExt(const Instruction *I) {
1704 Type *DestTy = I->getType();
1705 Value *Src = I->getOperand(0);
1706 Type *SrcTy = Src->getType();
1707
1708 bool IsZExt = isa<ZExtInst>(I);
1709 unsigned SrcReg = getRegForValue(Src);
1710 if (!SrcReg) return false;
1711
1712 EVT SrcEVT, DestEVT;
1713 SrcEVT = TLI.getValueType(SrcTy, true);
1714 DestEVT = TLI.getValueType(DestTy, true);
1715 if (!SrcEVT.isSimple())
1716 return false;
1717 if (!DestEVT.isSimple())
1718 return false;
1719
1720 MVT SrcVT = SrcEVT.getSimpleVT();
1721 MVT DestVT = DestEVT.getSimpleVT();
1722
1723 // If we know the register class needed for the result of this
1724 // instruction, use it. Otherwise pick the register class of the
1725 // correct size that does not contain X0/R0, since we don't know
1726 // whether downstream uses permit that assignment.
1727 unsigned AssignedReg = FuncInfo.ValueMap[I];
1728 const TargetRegisterClass *RC =
1729 (AssignedReg ? MRI.getRegClass(AssignedReg) :
1730 (DestVT == MVT::i64 ? &PPC::G8RC_and_G8RC_NOX0RegClass :
1731 &PPC::GPRC_and_GPRC_NOR0RegClass));
1732 unsigned ResultReg = createResultReg(RC);
1733
1734 if (!PPCEmitIntExt(SrcVT, SrcReg, DestVT, ResultReg, IsZExt))
1735 return false;
1736
1737 UpdateValueMap(I, ResultReg);
1738 return true;
1739}
1740
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001741// Attempt to fast-select an instruction that wasn't handled by
Bill Schmidt03008132013-08-25 22:33:42 +00001742// the table-generated machinery.
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001743bool PPCFastISel::TargetSelectInstruction(const Instruction *I) {
Bill Schmidt03008132013-08-25 22:33:42 +00001744
1745 switch (I->getOpcode()) {
Bill Schmidtccecf262013-08-30 02:29:45 +00001746 case Instruction::Load:
1747 return SelectLoad(I);
1748 case Instruction::Store:
1749 return SelectStore(I);
Bill Schmidt03008132013-08-25 22:33:42 +00001750 case Instruction::Br:
1751 return SelectBranch(I);
1752 case Instruction::IndirectBr:
1753 return SelectIndirectBr(I);
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001754 case Instruction::FPExt:
1755 return SelectFPExt(I);
1756 case Instruction::FPTrunc:
1757 return SelectFPTrunc(I);
1758 case Instruction::SIToFP:
1759 return SelectIToFP(I, /*IsSigned*/ true);
1760 case Instruction::UIToFP:
1761 return SelectIToFP(I, /*IsSigned*/ false);
1762 case Instruction::FPToSI:
1763 return SelectFPToI(I, /*IsSigned*/ true);
1764 case Instruction::FPToUI:
1765 return SelectFPToI(I, /*IsSigned*/ false);
Bill Schmidtccecf262013-08-30 02:29:45 +00001766 case Instruction::Add:
1767 return SelectBinaryIntOp(I, ISD::ADD);
1768 case Instruction::Or:
1769 return SelectBinaryIntOp(I, ISD::OR);
1770 case Instruction::Sub:
1771 return SelectBinaryIntOp(I, ISD::SUB);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001772 case Instruction::Call:
1773 if (dyn_cast<IntrinsicInst>(I))
1774 return false;
1775 return SelectCall(I);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001776 case Instruction::Ret:
1777 return SelectRet(I);
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001778 case Instruction::Trunc:
1779 return SelectTrunc(I);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001780 case Instruction::ZExt:
1781 case Instruction::SExt:
1782 return SelectIntExt(I);
Bill Schmidt03008132013-08-25 22:33:42 +00001783 // Here add other flavors of Instruction::XXX that automated
1784 // cases don't catch. For example, switches are terminators
1785 // that aren't yet handled.
1786 default:
1787 break;
1788 }
1789 return false;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001790}
1791
1792// Materialize a floating-point constant into a register, and return
1793// the register number (or zero if we failed to handle it).
1794unsigned PPCFastISel::PPCMaterializeFP(const ConstantFP *CFP, MVT VT) {
1795 // No plans to handle long double here.
1796 if (VT != MVT::f32 && VT != MVT::f64)
1797 return 0;
1798
1799 // All FP constants are loaded from the constant pool.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001800 unsigned Align = DL.getPrefTypeAlignment(CFP->getType());
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001801 assert(Align > 0 && "Unexpectedly missing alignment information!");
1802 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
1803 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
1804 CodeModel::Model CModel = TM.getCodeModel();
1805
1806 MachineMemOperand *MMO =
1807 FuncInfo.MF->getMachineMemOperand(
1808 MachinePointerInfo::getConstantPool(), MachineMemOperand::MOLoad,
1809 (VT == MVT::f32) ? 4 : 8, Align);
1810
Bill Schmidt03008132013-08-25 22:33:42 +00001811 unsigned Opc = (VT == MVT::f32) ? PPC::LFS : PPC::LFD;
1812 unsigned TmpReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
1813
1814 // For small code model, generate a LF[SD](0, LDtocCPT(Idx, X2)).
1815 if (CModel == CodeModel::Small || CModel == CodeModel::JITDefault) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001816 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocCPT),
Bill Schmidt03008132013-08-25 22:33:42 +00001817 TmpReg)
1818 .addConstantPoolIndex(Idx).addReg(PPC::X2);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001819 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidt03008132013-08-25 22:33:42 +00001820 .addImm(0).addReg(TmpReg).addMemOperand(MMO);
1821 } else {
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001822 // Otherwise we generate LF[SD](Idx[lo], ADDIStocHA(X2, Idx)).
Rafael Espindolaea09c592014-02-18 22:05:46 +00001823 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDIStocHA),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001824 TmpReg).addReg(PPC::X2).addConstantPoolIndex(Idx);
Bill Schmidtbb381d72013-09-17 20:03:25 +00001825 // But for large code model, we must generate a LDtocL followed
1826 // by the LF[SD].
1827 if (CModel == CodeModel::Large) {
1828 unsigned TmpReg2 = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001829 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocL),
Bill Schmidtbb381d72013-09-17 20:03:25 +00001830 TmpReg2).addConstantPoolIndex(Idx).addReg(TmpReg);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001831 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidtbb381d72013-09-17 20:03:25 +00001832 .addImm(0).addReg(TmpReg2);
1833 } else
Rafael Espindolaea09c592014-02-18 22:05:46 +00001834 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidtbb381d72013-09-17 20:03:25 +00001835 .addConstantPoolIndex(Idx, 0, PPCII::MO_TOC_LO)
1836 .addReg(TmpReg)
1837 .addMemOperand(MMO);
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001838 }
1839
1840 return DestReg;
1841}
1842
Bill Schmidtccecf262013-08-30 02:29:45 +00001843// Materialize the address of a global value into a register, and return
1844// the register number (or zero if we failed to handle it).
1845unsigned PPCFastISel::PPCMaterializeGV(const GlobalValue *GV, MVT VT) {
1846 assert(VT == MVT::i64 && "Non-address!");
1847 const TargetRegisterClass *RC = &PPC::G8RC_and_G8RC_NOX0RegClass;
1848 unsigned DestReg = createResultReg(RC);
1849
1850 // Global values may be plain old object addresses, TLS object
1851 // addresses, constant pool entries, or jump tables. How we generate
1852 // code for these may depend on small, medium, or large code model.
1853 CodeModel::Model CModel = TM.getCodeModel();
1854
1855 // FIXME: Jump tables are not yet required because fast-isel doesn't
1856 // handle switches; if that changes, we need them as well. For now,
1857 // what follows assumes everything's a generic (or TLS) global address.
1858 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
1859 if (!GVar) {
1860 // If GV is an alias, use the aliasee for determining thread-locality.
1861 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
1862 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false));
Bill Schmidtccecf262013-08-30 02:29:45 +00001863 }
1864
1865 // FIXME: We don't yet handle the complexity of TLS.
1866 bool IsTLS = GVar && GVar->isThreadLocal();
1867 if (IsTLS)
1868 return 0;
1869
1870 // For small code model, generate a simple TOC load.
1871 if (CModel == CodeModel::Small || CModel == CodeModel::JITDefault)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001872 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtoc),
1873 DestReg)
1874 .addGlobalAddress(GV)
1875 .addReg(PPC::X2);
Bill Schmidtccecf262013-08-30 02:29:45 +00001876 else {
1877 // If the address is an externally defined symbol, a symbol with
1878 // common or externally available linkage, a function address, or a
1879 // jump table address (not yet needed), or if we are generating code
1880 // for large code model, we generate:
1881 // LDtocL(GV, ADDIStocHA(%X2, GV))
1882 // Otherwise we generate:
1883 // ADDItocL(ADDIStocHA(%X2, GV), GV)
1884 // Either way, start with the ADDIStocHA:
1885 unsigned HighPartReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001886 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDIStocHA),
Bill Schmidtccecf262013-08-30 02:29:45 +00001887 HighPartReg).addReg(PPC::X2).addGlobalAddress(GV);
1888
1889 // !GVar implies a function address. An external variable is one
1890 // without an initializer.
1891 // If/when switches are implemented, jump tables should be handled
1892 // on the "if" path here.
1893 if (CModel == CodeModel::Large || !GVar || !GVar->hasInitializer() ||
1894 GVar->hasCommonLinkage() || GVar->hasAvailableExternallyLinkage())
Rafael Espindolaea09c592014-02-18 22:05:46 +00001895 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocL),
Bill Schmidtccecf262013-08-30 02:29:45 +00001896 DestReg).addGlobalAddress(GV).addReg(HighPartReg);
1897 else
1898 // Otherwise generate the ADDItocL.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001899 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDItocL),
Bill Schmidtccecf262013-08-30 02:29:45 +00001900 DestReg).addReg(HighPartReg).addGlobalAddress(GV);
1901 }
1902
1903 return DestReg;
1904}
1905
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001906// Materialize a 32-bit integer constant into a register, and return
1907// the register number (or zero if we failed to handle it).
1908unsigned PPCFastISel::PPCMaterialize32BitInt(int64_t Imm,
1909 const TargetRegisterClass *RC) {
1910 unsigned Lo = Imm & 0xFFFF;
1911 unsigned Hi = (Imm >> 16) & 0xFFFF;
1912
1913 unsigned ResultReg = createResultReg(RC);
1914 bool IsGPRC = RC->hasSuperClassEq(&PPC::GPRCRegClass);
1915
1916 if (isInt<16>(Imm))
Rafael Espindolaea09c592014-02-18 22:05:46 +00001917 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001918 TII.get(IsGPRC ? PPC::LI : PPC::LI8), ResultReg)
1919 .addImm(Imm);
1920 else if (Lo) {
1921 // Both Lo and Hi have nonzero bits.
1922 unsigned TmpReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001923 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001924 TII.get(IsGPRC ? PPC::LIS : PPC::LIS8), TmpReg)
1925 .addImm(Hi);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001926 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001927 TII.get(IsGPRC ? PPC::ORI : PPC::ORI8), ResultReg)
1928 .addReg(TmpReg).addImm(Lo);
1929 } else
1930 // Just Hi bits.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001931 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001932 TII.get(IsGPRC ? PPC::LIS : PPC::LIS8), ResultReg)
1933 .addImm(Hi);
1934
1935 return ResultReg;
1936}
1937
1938// Materialize a 64-bit integer constant into a register, and return
1939// the register number (or zero if we failed to handle it).
1940unsigned PPCFastISel::PPCMaterialize64BitInt(int64_t Imm,
1941 const TargetRegisterClass *RC) {
1942 unsigned Remainder = 0;
1943 unsigned Shift = 0;
1944
1945 // If the value doesn't fit in 32 bits, see if we can shift it
1946 // so that it fits in 32 bits.
1947 if (!isInt<32>(Imm)) {
1948 Shift = countTrailingZeros<uint64_t>(Imm);
1949 int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
1950
1951 if (isInt<32>(ImmSh))
1952 Imm = ImmSh;
1953 else {
1954 Remainder = Imm;
1955 Shift = 32;
1956 Imm >>= 32;
1957 }
1958 }
1959
1960 // Handle the high-order 32 bits (if shifted) or the whole 32 bits
1961 // (if not shifted).
1962 unsigned TmpReg1 = PPCMaterialize32BitInt(Imm, RC);
1963 if (!Shift)
1964 return TmpReg1;
1965
1966 // If upper 32 bits were not zero, we've built them and need to shift
1967 // them into place.
1968 unsigned TmpReg2;
1969 if (Imm) {
1970 TmpReg2 = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001971 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::RLDICR),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001972 TmpReg2).addReg(TmpReg1).addImm(Shift).addImm(63 - Shift);
1973 } else
1974 TmpReg2 = TmpReg1;
1975
1976 unsigned TmpReg3, Hi, Lo;
1977 if ((Hi = (Remainder >> 16) & 0xFFFF)) {
1978 TmpReg3 = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001979 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ORIS8),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001980 TmpReg3).addReg(TmpReg2).addImm(Hi);
1981 } else
1982 TmpReg3 = TmpReg2;
1983
1984 if ((Lo = Remainder & 0xFFFF)) {
1985 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001986 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ORI8),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001987 ResultReg).addReg(TmpReg3).addImm(Lo);
1988 return ResultReg;
1989 }
1990
1991 return TmpReg3;
1992}
1993
1994
1995// Materialize an integer constant into a register, and return
1996// the register number (or zero if we failed to handle it).
1997unsigned PPCFastISel::PPCMaterializeInt(const Constant *C, MVT VT) {
1998
1999 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16 &&
2000 VT != MVT::i8 && VT != MVT::i1)
2001 return 0;
2002
2003 const TargetRegisterClass *RC = ((VT == MVT::i64) ? &PPC::G8RCRegClass :
2004 &PPC::GPRCRegClass);
2005
2006 // If the constant is in range, use a load-immediate.
2007 const ConstantInt *CI = cast<ConstantInt>(C);
2008 if (isInt<16>(CI->getSExtValue())) {
2009 unsigned Opc = (VT == MVT::i64) ? PPC::LI8 : PPC::LI;
2010 unsigned ImmReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002011 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ImmReg)
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002012 .addImm(CI->getSExtValue());
2013 return ImmReg;
2014 }
2015
2016 // Construct the constant piecewise.
2017 int64_t Imm = CI->getZExtValue();
2018
2019 if (VT == MVT::i64)
2020 return PPCMaterialize64BitInt(Imm, RC);
2021 else if (VT == MVT::i32)
2022 return PPCMaterialize32BitInt(Imm, RC);
2023
2024 return 0;
2025}
2026
2027// Materialize a constant into a register, and return the register
2028// number (or zero if we failed to handle it).
2029unsigned PPCFastISel::TargetMaterializeConstant(const Constant *C) {
2030 EVT CEVT = TLI.getValueType(C->getType(), true);
2031
2032 // Only handle simple types.
2033 if (!CEVT.isSimple()) return 0;
2034 MVT VT = CEVT.getSimpleVT();
2035
2036 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
2037 return PPCMaterializeFP(CFP, VT);
Bill Schmidtccecf262013-08-30 02:29:45 +00002038 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
2039 return PPCMaterializeGV(GV, VT);
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002040 else if (isa<ConstantInt>(C))
2041 return PPCMaterializeInt(C, VT);
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002042
2043 return 0;
2044}
2045
2046// Materialize the address created by an alloca into a register, and
Bill Schmidteb8d6f72013-08-31 02:33:40 +00002047// return the register number (or zero if we failed to handle it).
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002048unsigned PPCFastISel::TargetMaterializeAlloca(const AllocaInst *AI) {
Bill Schmidteb8d6f72013-08-31 02:33:40 +00002049 // Don't handle dynamic allocas.
2050 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
2051
2052 MVT VT;
2053 if (!isLoadTypeLegal(AI->getType(), VT)) return 0;
2054
2055 DenseMap<const AllocaInst*, int>::iterator SI =
2056 FuncInfo.StaticAllocaMap.find(AI);
2057
2058 if (SI != FuncInfo.StaticAllocaMap.end()) {
2059 unsigned ResultReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002060 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDI8),
Bill Schmidteb8d6f72013-08-31 02:33:40 +00002061 ResultReg).addFrameIndex(SI->second).addImm(0);
2062 return ResultReg;
2063 }
2064
2065 return 0;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002066}
2067
Bill Schmidtccecf262013-08-30 02:29:45 +00002068// Fold loads into extends when possible.
2069// FIXME: We can have multiple redundant extend/trunc instructions
2070// following a load. The folding only picks up one. Extend this
2071// to check subsequent instructions for the same pattern and remove
2072// them. Thus ResultReg should be the def reg for the last redundant
2073// instruction in a chain, and all intervening instructions can be
2074// removed from parent. Change test/CodeGen/PowerPC/fast-isel-fold.ll
2075// to add ELF64-NOT: rldicl to the appropriate tests when this works.
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002076bool PPCFastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
2077 const LoadInst *LI) {
Bill Schmidtccecf262013-08-30 02:29:45 +00002078 // Verify we have a legal type before going any further.
2079 MVT VT;
2080 if (!isLoadTypeLegal(LI->getType(), VT))
2081 return false;
2082
2083 // Combine load followed by zero- or sign-extend.
2084 bool IsZExt = false;
2085 switch(MI->getOpcode()) {
2086 default:
2087 return false;
2088
2089 case PPC::RLDICL:
2090 case PPC::RLDICL_32_64: {
2091 IsZExt = true;
2092 unsigned MB = MI->getOperand(3).getImm();
2093 if ((VT == MVT::i8 && MB <= 56) ||
2094 (VT == MVT::i16 && MB <= 48) ||
2095 (VT == MVT::i32 && MB <= 32))
2096 break;
2097 return false;
2098 }
2099
2100 case PPC::RLWINM:
2101 case PPC::RLWINM8: {
2102 IsZExt = true;
2103 unsigned MB = MI->getOperand(3).getImm();
2104 if ((VT == MVT::i8 && MB <= 24) ||
2105 (VT == MVT::i16 && MB <= 16))
2106 break;
2107 return false;
2108 }
2109
2110 case PPC::EXTSB:
2111 case PPC::EXTSB8:
2112 case PPC::EXTSB8_32_64:
2113 /* There is no sign-extending load-byte instruction. */
2114 return false;
2115
2116 case PPC::EXTSH:
2117 case PPC::EXTSH8:
2118 case PPC::EXTSH8_32_64: {
2119 if (VT != MVT::i16 && VT != MVT::i8)
2120 return false;
2121 break;
2122 }
2123
2124 case PPC::EXTSW:
2125 case PPC::EXTSW_32_64: {
2126 if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8)
2127 return false;
2128 break;
2129 }
2130 }
2131
2132 // See if we can handle this address.
2133 Address Addr;
2134 if (!PPCComputeAddress(LI->getOperand(0), Addr))
2135 return false;
2136
2137 unsigned ResultReg = MI->getOperand(0).getReg();
2138
2139 if (!PPCEmitLoad(VT, ResultReg, Addr, 0, IsZExt))
2140 return false;
2141
2142 MI->eraseFromParent();
2143 return true;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002144}
2145
2146// Attempt to lower call arguments in a faster way than done by
2147// the selection DAG code.
2148bool PPCFastISel::FastLowerArguments() {
2149 // Defer to normal argument lowering for now. It's reasonably
2150 // efficient. Consider doing something like ARM to handle the
2151 // case where all args fit in registers, no varargs, no float
2152 // or vector args.
2153 return false;
2154}
2155
Bill Schmidt03008132013-08-25 22:33:42 +00002156// Handle materializing integer constants into a register. This is not
2157// automatically generated for PowerPC, so must be explicitly created here.
2158unsigned PPCFastISel::FastEmit_i(MVT Ty, MVT VT, unsigned Opc, uint64_t Imm) {
2159
2160 if (Opc != ISD::Constant)
2161 return 0;
2162
2163 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16 &&
2164 VT != MVT::i8 && VT != MVT::i1)
2165 return 0;
2166
2167 const TargetRegisterClass *RC = ((VT == MVT::i64) ? &PPC::G8RCRegClass :
2168 &PPC::GPRCRegClass);
2169 if (VT == MVT::i64)
2170 return PPCMaterialize64BitInt(Imm, RC);
2171 else
2172 return PPCMaterialize32BitInt(Imm, RC);
2173}
2174
Bill Schmidtccecf262013-08-30 02:29:45 +00002175// Override for ADDI and ADDI8 to set the correct register class
2176// on RHS operand 0. The automatic infrastructure naively assumes
2177// GPRC for i32 and G8RC for i64; the concept of "no R0" is lost
2178// for these cases. At the moment, none of the other automatically
2179// generated RI instructions require special treatment. However, once
2180// SelectSelect is implemented, "isel" requires similar handling.
2181//
2182// Also be conservative about the output register class. Avoid
2183// assigning R0 or X0 to the output register for GPRC and G8RC
2184// register classes, as any such result could be used in ADDI, etc.,
2185// where those regs have another meaning.
2186unsigned PPCFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
2187 const TargetRegisterClass *RC,
2188 unsigned Op0, bool Op0IsKill,
2189 uint64_t Imm) {
2190 if (MachineInstOpcode == PPC::ADDI)
2191 MRI.setRegClass(Op0, &PPC::GPRC_and_GPRC_NOR0RegClass);
2192 else if (MachineInstOpcode == PPC::ADDI8)
2193 MRI.setRegClass(Op0, &PPC::G8RC_and_G8RC_NOX0RegClass);
2194
2195 const TargetRegisterClass *UseRC =
2196 (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
2197 (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
2198
2199 return FastISel::FastEmitInst_ri(MachineInstOpcode, UseRC,
2200 Op0, Op0IsKill, Imm);
2201}
2202
2203// Override for instructions with one register operand to avoid use of
2204// R0/X0. The automatic infrastructure isn't aware of the context so
2205// we must be conservative.
2206unsigned PPCFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
2207 const TargetRegisterClass* RC,
2208 unsigned Op0, bool Op0IsKill) {
2209 const TargetRegisterClass *UseRC =
2210 (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
2211 (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
2212
2213 return FastISel::FastEmitInst_r(MachineInstOpcode, UseRC, Op0, Op0IsKill);
2214}
2215
2216// Override for instructions with two register operands to avoid use
2217// of R0/X0. The automatic infrastructure isn't aware of the context
2218// so we must be conservative.
2219unsigned PPCFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
2220 const TargetRegisterClass* RC,
2221 unsigned Op0, bool Op0IsKill,
2222 unsigned Op1, bool Op1IsKill) {
2223 const TargetRegisterClass *UseRC =
2224 (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
2225 (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
2226
2227 return FastISel::FastEmitInst_rr(MachineInstOpcode, UseRC, Op0, Op0IsKill,
2228 Op1, Op1IsKill);
2229}
2230
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002231namespace llvm {
2232 // Create the fast instruction selector for PowerPC64 ELF.
2233 FastISel *PPC::createFastISel(FunctionLoweringInfo &FuncInfo,
2234 const TargetLibraryInfo *LibInfo) {
2235 const TargetMachine &TM = FuncInfo.MF->getTarget();
2236
2237 // Only available on 64-bit ELF for now.
2238 const PPCSubtarget *Subtarget = &TM.getSubtarget<PPCSubtarget>();
2239 if (Subtarget->isPPC64() && Subtarget->isSVR4ABI())
2240 return new PPCFastISel(FuncInfo, LibInfo);
2241
2242 return 0;
2243 }
2244}