blob: a397e8f2c39b8eb78d9046dad36da011648879b0 [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
Bill Schmidt0cf702f2013-07-30 00:50:39 +000016#include "PPC.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000017#include "MCTargetDesc/PPCPredicates.h"
Bill Schmidt0cf702f2013-07-30 00:50:39 +000018#include "PPCISelLowering.h"
19#include "PPCSubtarget.h"
20#include "PPCTargetMachine.h"
Bill Schmidt0cf702f2013-07-30 00:50:39 +000021#include "llvm/ADT/Optional.h"
22#include "llvm/CodeGen/CallingConvLower.h"
23#include "llvm/CodeGen/FastISel.h"
24#include "llvm/CodeGen/FunctionLoweringInfo.h"
25#include "llvm/CodeGen/MachineConstantPool.h"
26#include "llvm/CodeGen/MachineFrameInfo.h"
27#include "llvm/CodeGen/MachineInstrBuilder.h"
28#include "llvm/CodeGen/MachineRegisterInfo.h"
29#include "llvm/IR/CallingConv.h"
Chandler Carruth03eb0de2014-03-04 10:40:04 +000030#include "llvm/IR/GetElementPtrTypeIterator.h"
Bill Schmidt0cf702f2013-07-30 00:50:39 +000031#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"
Bill Schmidt0cf702f2013-07-30 00:50:39 +000036#include "llvm/Target/TargetLowering.h"
37#include "llvm/Target/TargetMachine.h"
38
Bill Schmidteb8d6f72013-08-31 02:33:40 +000039//===----------------------------------------------------------------------===//
40//
41// TBD:
42// FastLowerArguments: Handle simple cases.
43// PPCMaterializeGV: Handle TLS.
44// SelectCall: Handle function pointers.
45// SelectCall: Handle multi-register return values.
46// SelectCall: Optimize away nops for local calls.
47// processCallArgs: Handle bit-converted arguments.
48// finishCall: Handle multi-register return values.
49// PPCComputeAddress: Handle parameter references as FrameIndex's.
50// PPCEmitCmp: Handle immediate as operand 1.
51// SelectCall: Handle small byval arguments.
52// SelectIntrinsicCall: Implement.
53// SelectSelect: Implement.
54// Consider factoring isTypeLegal into the base class.
55// Implement switches and jump tables.
56//
57//===----------------------------------------------------------------------===//
Bill Schmidt0cf702f2013-07-30 00:50:39 +000058using namespace llvm;
59
Chandler Carruth84e68b22014-04-22 02:41:26 +000060#define DEBUG_TYPE "ppcfastisel"
61
Bill Schmidt0cf702f2013-07-30 00:50:39 +000062namespace {
63
64typedef struct Address {
65 enum {
66 RegBase,
67 FrameIndexBase
68 } BaseType;
69
70 union {
71 unsigned Reg;
72 int FI;
73 } Base;
74
Bill Schmidtccecf262013-08-30 02:29:45 +000075 long Offset;
Bill Schmidt0cf702f2013-07-30 00:50:39 +000076
77 // Innocuous defaults for our address.
78 Address()
79 : BaseType(RegBase), Offset(0) {
80 Base.Reg = 0;
81 }
82} Address;
83
Craig Topper26696312014-03-18 07:27:13 +000084class PPCFastISel final : public FastISel {
Bill Schmidt0cf702f2013-07-30 00:50:39 +000085
86 const TargetMachine &TM;
87 const TargetInstrInfo &TII;
88 const TargetLowering &TLI;
Eric Christopher1b8e7632014-05-22 01:07:24 +000089 const PPCSubtarget *PPCSubTarget;
Bill Schmidt0cf702f2013-07-30 00:50:39 +000090 LLVMContext *Context;
91
92 public:
93 explicit PPCFastISel(FunctionLoweringInfo &FuncInfo,
94 const TargetLibraryInfo *LibInfo)
Eric Christopherd9134482014-08-04 21:25:23 +000095 : FastISel(FuncInfo, LibInfo), TM(FuncInfo.MF->getTarget()),
96 TII(*TM.getSubtargetImpl()->getInstrInfo()),
97 TLI(*TM.getSubtargetImpl()->getTargetLowering()),
98 PPCSubTarget(&TM.getSubtarget<PPCSubtarget>()),
99 Context(&FuncInfo.Fn->getContext()) {}
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000100
101 // Backend specific FastISel code.
102 private:
Craig Topper0d3fa922014-04-29 07:57:37 +0000103 bool TargetSelectInstruction(const Instruction *I) override;
104 unsigned TargetMaterializeConstant(const Constant *C) override;
105 unsigned TargetMaterializeAlloca(const AllocaInst *AI) override;
106 bool tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
107 const LoadInst *LI) override;
108 bool FastLowerArguments() override;
109 unsigned FastEmit_i(MVT Ty, MVT RetTy, unsigned Opc, uint64_t Imm) override;
110 unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
111 const TargetRegisterClass *RC,
112 unsigned Op0, bool Op0IsKill,
113 uint64_t Imm);
114 unsigned FastEmitInst_r(unsigned MachineInstOpcode,
115 const TargetRegisterClass *RC,
116 unsigned Op0, bool Op0IsKill);
117 unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
118 const TargetRegisterClass *RC,
119 unsigned Op0, bool Op0IsKill,
120 unsigned Op1, bool Op1IsKill);
Bill Schmidt03008132013-08-25 22:33:42 +0000121
122 // Instruction selection routines.
123 private:
Bill Schmidtccecf262013-08-30 02:29:45 +0000124 bool SelectLoad(const Instruction *I);
125 bool SelectStore(const Instruction *I);
Bill Schmidt03008132013-08-25 22:33:42 +0000126 bool SelectBranch(const Instruction *I);
127 bool SelectIndirectBr(const Instruction *I);
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000128 bool SelectFPExt(const Instruction *I);
129 bool SelectFPTrunc(const Instruction *I);
130 bool SelectIToFP(const Instruction *I, bool IsSigned);
131 bool SelectFPToI(const Instruction *I, bool IsSigned);
Bill Schmidtccecf262013-08-30 02:29:45 +0000132 bool SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode);
Bill Schmidt8470b0f2013-08-30 22:18:55 +0000133 bool SelectCall(const Instruction *I);
Bill Schmidtd89f6782013-08-26 19:42:51 +0000134 bool SelectRet(const Instruction *I);
Bill Schmidt9d9510d2013-08-30 23:31:33 +0000135 bool SelectTrunc(const Instruction *I);
Bill Schmidtd89f6782013-08-26 19:42:51 +0000136 bool SelectIntExt(const Instruction *I);
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000137
138 // Utility routines.
139 private:
Bill Schmidtccecf262013-08-30 02:29:45 +0000140 bool isTypeLegal(Type *Ty, MVT &VT);
141 bool isLoadTypeLegal(Type *Ty, MVT &VT);
Bill Schmidt03008132013-08-25 22:33:42 +0000142 bool PPCEmitCmp(const Value *Src1Value, const Value *Src2Value,
143 bool isZExt, unsigned DestReg);
Bill Schmidtccecf262013-08-30 02:29:45 +0000144 bool PPCEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
145 const TargetRegisterClass *RC, bool IsZExt = true,
146 unsigned FP64LoadOpc = PPC::LFD);
147 bool PPCEmitStore(MVT VT, unsigned SrcReg, Address &Addr);
148 bool PPCComputeAddress(const Value *Obj, Address &Addr);
149 void PPCSimplifyAddress(Address &Addr, MVT VT, bool &UseOffset,
150 unsigned &IndexReg);
Bill Schmidt03008132013-08-25 22:33:42 +0000151 bool PPCEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
152 unsigned DestReg, bool IsZExt);
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000153 unsigned PPCMaterializeFP(const ConstantFP *CFP, MVT VT);
Bill Schmidtccecf262013-08-30 02:29:45 +0000154 unsigned PPCMaterializeGV(const GlobalValue *GV, MVT VT);
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000155 unsigned PPCMaterializeInt(const Constant *C, MVT VT);
156 unsigned PPCMaterialize32BitInt(int64_t Imm,
157 const TargetRegisterClass *RC);
158 unsigned PPCMaterialize64BitInt(int64_t Imm,
159 const TargetRegisterClass *RC);
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000160 unsigned PPCMoveToIntReg(const Instruction *I, MVT VT,
161 unsigned SrcReg, bool IsSigned);
162 unsigned PPCMoveToFPReg(MVT VT, unsigned SrcReg, bool IsSigned);
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000163
Bill Schmidtd89f6782013-08-26 19:42:51 +0000164 // Call handling routines.
165 private:
Bill Schmidt8470b0f2013-08-30 22:18:55 +0000166 bool processCallArgs(SmallVectorImpl<Value*> &Args,
167 SmallVectorImpl<unsigned> &ArgRegs,
168 SmallVectorImpl<MVT> &ArgVTs,
169 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
170 SmallVectorImpl<unsigned> &RegArgs,
171 CallingConv::ID CC,
172 unsigned &NumBytes,
173 bool IsVarArg);
174 void finishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
175 const Instruction *I, CallingConv::ID CC,
176 unsigned &NumBytes, bool IsVarArg);
Bill Schmidtd89f6782013-08-26 19:42:51 +0000177 CCAssignFn *usePPC32CCs(unsigned Flag);
178
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000179 private:
180 #include "PPCGenFastISel.inc"
181
182};
183
184} // end anonymous namespace
185
Bill Schmidtd89f6782013-08-26 19:42:51 +0000186#include "PPCGenCallingConv.inc"
187
188// Function whose sole purpose is to kill compiler warnings
189// stemming from unused functions included from PPCGenCallingConv.inc.
190CCAssignFn *PPCFastISel::usePPC32CCs(unsigned Flag) {
191 if (Flag == 1)
192 return CC_PPC32_SVR4;
193 else if (Flag == 2)
194 return CC_PPC32_SVR4_ByVal;
195 else if (Flag == 3)
196 return CC_PPC32_SVR4_VarArg;
197 else
198 return RetCC_PPC;
199}
200
Bill Schmidt03008132013-08-25 22:33:42 +0000201static Optional<PPC::Predicate> getComparePred(CmpInst::Predicate Pred) {
202 switch (Pred) {
203 // These are not representable with any single compare.
204 case CmpInst::FCMP_FALSE:
205 case CmpInst::FCMP_UEQ:
206 case CmpInst::FCMP_UGT:
207 case CmpInst::FCMP_UGE:
208 case CmpInst::FCMP_ULT:
209 case CmpInst::FCMP_ULE:
210 case CmpInst::FCMP_UNE:
211 case CmpInst::FCMP_TRUE:
212 default:
213 return Optional<PPC::Predicate>();
214
215 case CmpInst::FCMP_OEQ:
216 case CmpInst::ICMP_EQ:
217 return PPC::PRED_EQ;
218
219 case CmpInst::FCMP_OGT:
220 case CmpInst::ICMP_UGT:
221 case CmpInst::ICMP_SGT:
222 return PPC::PRED_GT;
223
224 case CmpInst::FCMP_OGE:
225 case CmpInst::ICMP_UGE:
226 case CmpInst::ICMP_SGE:
227 return PPC::PRED_GE;
228
229 case CmpInst::FCMP_OLT:
230 case CmpInst::ICMP_ULT:
231 case CmpInst::ICMP_SLT:
232 return PPC::PRED_LT;
233
234 case CmpInst::FCMP_OLE:
235 case CmpInst::ICMP_ULE:
236 case CmpInst::ICMP_SLE:
237 return PPC::PRED_LE;
238
239 case CmpInst::FCMP_ONE:
240 case CmpInst::ICMP_NE:
241 return PPC::PRED_NE;
242
243 case CmpInst::FCMP_ORD:
244 return PPC::PRED_NU;
245
246 case CmpInst::FCMP_UNO:
247 return PPC::PRED_UN;
248 }
249}
250
Bill Schmidtccecf262013-08-30 02:29:45 +0000251// Determine whether the type Ty is simple enough to be handled by
252// fast-isel, and return its equivalent machine type in VT.
253// FIXME: Copied directly from ARM -- factor into base class?
254bool PPCFastISel::isTypeLegal(Type *Ty, MVT &VT) {
255 EVT Evt = TLI.getValueType(Ty, true);
256
257 // Only handle simple types.
258 if (Evt == MVT::Other || !Evt.isSimple()) return false;
259 VT = Evt.getSimpleVT();
260
261 // Handle all legal types, i.e. a register that will directly hold this
262 // value.
263 return TLI.isTypeLegal(VT);
264}
265
266// Determine whether the type Ty is simple enough to be handled by
267// fast-isel as a load target, and return its equivalent machine type in VT.
268bool PPCFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
269 if (isTypeLegal(Ty, VT)) return true;
270
271 // If this is a type than can be sign or zero-extended to a basic operation
272 // go ahead and accept it now.
273 if (VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32) {
274 return true;
275 }
276
277 return false;
278}
279
280// Given a value Obj, create an Address object Addr that represents its
281// address. Return false if we can't handle it.
282bool PPCFastISel::PPCComputeAddress(const Value *Obj, Address &Addr) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000283 const User *U = nullptr;
Bill Schmidtccecf262013-08-30 02:29:45 +0000284 unsigned Opcode = Instruction::UserOp1;
285 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
286 // Don't walk into other basic blocks unless the object is an alloca from
287 // another block, otherwise it may not have a virtual register assigned.
288 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
289 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
290 Opcode = I->getOpcode();
291 U = I;
292 }
293 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
294 Opcode = C->getOpcode();
295 U = C;
296 }
297
298 switch (Opcode) {
299 default:
300 break;
301 case Instruction::BitCast:
302 // Look through bitcasts.
303 return PPCComputeAddress(U->getOperand(0), Addr);
304 case Instruction::IntToPtr:
305 // Look past no-op inttoptrs.
306 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
307 return PPCComputeAddress(U->getOperand(0), Addr);
308 break;
309 case Instruction::PtrToInt:
310 // Look past no-op ptrtoints.
311 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
312 return PPCComputeAddress(U->getOperand(0), Addr);
313 break;
314 case Instruction::GetElementPtr: {
315 Address SavedAddr = Addr;
316 long TmpOffset = Addr.Offset;
317
318 // Iterate through the GEP folding the constants into offsets where
319 // we can.
320 gep_type_iterator GTI = gep_type_begin(U);
321 for (User::const_op_iterator II = U->op_begin() + 1, IE = U->op_end();
322 II != IE; ++II, ++GTI) {
323 const Value *Op = *II;
324 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000325 const StructLayout *SL = DL.getStructLayout(STy);
Bill Schmidtccecf262013-08-30 02:29:45 +0000326 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
327 TmpOffset += SL->getElementOffset(Idx);
328 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000329 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
Bill Schmidtccecf262013-08-30 02:29:45 +0000330 for (;;) {
331 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
332 // Constant-offset addressing.
333 TmpOffset += CI->getSExtValue() * S;
334 break;
335 }
Bob Wilson9f3e6b22013-11-15 19:09:27 +0000336 if (canFoldAddIntoGEP(U, Op)) {
337 // A compatible add with a constant operand. Fold the constant.
Bill Schmidtccecf262013-08-30 02:29:45 +0000338 ConstantInt *CI =
339 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
340 TmpOffset += CI->getSExtValue() * S;
341 // Iterate on the other operand.
342 Op = cast<AddOperator>(Op)->getOperand(0);
343 continue;
344 }
345 // Unsupported
346 goto unsupported_gep;
347 }
348 }
349 }
350
351 // Try to grab the base operand now.
352 Addr.Offset = TmpOffset;
353 if (PPCComputeAddress(U->getOperand(0), Addr)) return true;
354
355 // We failed, restore everything and try the other options.
356 Addr = SavedAddr;
357
358 unsupported_gep:
359 break;
360 }
361 case Instruction::Alloca: {
362 const AllocaInst *AI = cast<AllocaInst>(Obj);
363 DenseMap<const AllocaInst*, int>::iterator SI =
364 FuncInfo.StaticAllocaMap.find(AI);
365 if (SI != FuncInfo.StaticAllocaMap.end()) {
366 Addr.BaseType = Address::FrameIndexBase;
367 Addr.Base.FI = SI->second;
368 return true;
369 }
370 break;
371 }
372 }
373
374 // FIXME: References to parameters fall through to the behavior
375 // below. They should be able to reference a frame index since
376 // they are stored to the stack, so we can get "ld rx, offset(r1)"
377 // instead of "addi ry, r1, offset / ld rx, 0(ry)". Obj will
378 // just contain the parameter. Try to handle this with a FI.
379
380 // Try to get this in a register if nothing else has worked.
381 if (Addr.Base.Reg == 0)
382 Addr.Base.Reg = getRegForValue(Obj);
383
384 // Prevent assignment of base register to X0, which is inappropriate
385 // for loads and stores alike.
386 if (Addr.Base.Reg != 0)
387 MRI.setRegClass(Addr.Base.Reg, &PPC::G8RC_and_G8RC_NOX0RegClass);
388
389 return Addr.Base.Reg != 0;
390}
391
392// Fix up some addresses that can't be used directly. For example, if
393// an offset won't fit in an instruction field, we may need to move it
394// into an index register.
395void PPCFastISel::PPCSimplifyAddress(Address &Addr, MVT VT, bool &UseOffset,
396 unsigned &IndexReg) {
397
398 // Check whether the offset fits in the instruction field.
399 if (!isInt<16>(Addr.Offset))
400 UseOffset = false;
401
402 // If this is a stack pointer and the offset needs to be simplified then
403 // put the alloca address into a register, set the base type back to
404 // register and continue. This should almost never happen.
405 if (!UseOffset && Addr.BaseType == Address::FrameIndexBase) {
406 unsigned ResultReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000407 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDI8),
Bill Schmidtccecf262013-08-30 02:29:45 +0000408 ResultReg).addFrameIndex(Addr.Base.FI).addImm(0);
409 Addr.Base.Reg = ResultReg;
410 Addr.BaseType = Address::RegBase;
411 }
412
413 if (!UseOffset) {
414 IntegerType *OffsetTy = ((VT == MVT::i32) ? Type::getInt32Ty(*Context)
415 : Type::getInt64Ty(*Context));
416 const ConstantInt *Offset =
417 ConstantInt::getSigned(OffsetTy, (int64_t)(Addr.Offset));
418 IndexReg = PPCMaterializeInt(Offset, MVT::i64);
419 assert(IndexReg && "Unexpected error in PPCMaterializeInt!");
420 }
421}
422
423// Emit a load instruction if possible, returning true if we succeeded,
424// otherwise false. See commentary below for how the register class of
425// the load is determined.
426bool PPCFastISel::PPCEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
427 const TargetRegisterClass *RC,
428 bool IsZExt, unsigned FP64LoadOpc) {
429 unsigned Opc;
430 bool UseOffset = true;
431
432 // If ResultReg is given, it determines the register class of the load.
433 // Otherwise, RC is the register class to use. If the result of the
434 // load isn't anticipated in this block, both may be zero, in which
435 // case we must make a conservative guess. In particular, don't assign
436 // R0 or X0 to the result register, as the result may be used in a load,
437 // store, add-immediate, or isel that won't permit this. (Though
438 // perhaps the spill and reload of live-exit values would handle this?)
439 const TargetRegisterClass *UseRC =
440 (ResultReg ? MRI.getRegClass(ResultReg) :
441 (RC ? RC :
442 (VT == MVT::f64 ? &PPC::F8RCRegClass :
443 (VT == MVT::f32 ? &PPC::F4RCRegClass :
444 (VT == MVT::i64 ? &PPC::G8RC_and_G8RC_NOX0RegClass :
445 &PPC::GPRC_and_GPRC_NOR0RegClass)))));
446
447 bool Is32BitInt = UseRC->hasSuperClassEq(&PPC::GPRCRegClass);
448
449 switch (VT.SimpleTy) {
450 default: // e.g., vector types not handled
451 return false;
452 case MVT::i8:
453 Opc = Is32BitInt ? PPC::LBZ : PPC::LBZ8;
454 break;
455 case MVT::i16:
456 Opc = (IsZExt ?
457 (Is32BitInt ? PPC::LHZ : PPC::LHZ8) :
458 (Is32BitInt ? PPC::LHA : PPC::LHA8));
459 break;
460 case MVT::i32:
461 Opc = (IsZExt ?
462 (Is32BitInt ? PPC::LWZ : PPC::LWZ8) :
463 (Is32BitInt ? PPC::LWA_32 : PPC::LWA));
464 if ((Opc == PPC::LWA || Opc == PPC::LWA_32) && ((Addr.Offset & 3) != 0))
465 UseOffset = false;
466 break;
467 case MVT::i64:
468 Opc = PPC::LD;
469 assert(UseRC->hasSuperClassEq(&PPC::G8RCRegClass) &&
470 "64-bit load with 32-bit target??");
471 UseOffset = ((Addr.Offset & 3) == 0);
472 break;
473 case MVT::f32:
474 Opc = PPC::LFS;
475 break;
476 case MVT::f64:
477 Opc = FP64LoadOpc;
478 break;
479 }
480
481 // If necessary, materialize the offset into a register and use
482 // the indexed form. Also handle stack pointers with special needs.
483 unsigned IndexReg = 0;
484 PPCSimplifyAddress(Addr, VT, UseOffset, IndexReg);
485 if (ResultReg == 0)
486 ResultReg = createResultReg(UseRC);
487
488 // Note: If we still have a frame index here, we know the offset is
489 // in range, as otherwise PPCSimplifyAddress would have converted it
490 // into a RegBase.
491 if (Addr.BaseType == Address::FrameIndexBase) {
492
493 MachineMemOperand *MMO =
494 FuncInfo.MF->getMachineMemOperand(
495 MachinePointerInfo::getFixedStack(Addr.Base.FI, Addr.Offset),
496 MachineMemOperand::MOLoad, MFI.getObjectSize(Addr.Base.FI),
497 MFI.getObjectAlignment(Addr.Base.FI));
498
Rafael Espindolaea09c592014-02-18 22:05:46 +0000499 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Bill Schmidtccecf262013-08-30 02:29:45 +0000500 .addImm(Addr.Offset).addFrameIndex(Addr.Base.FI).addMemOperand(MMO);
501
502 // Base reg with offset in range.
503 } else if (UseOffset) {
504
Rafael Espindolaea09c592014-02-18 22:05:46 +0000505 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Bill Schmidtccecf262013-08-30 02:29:45 +0000506 .addImm(Addr.Offset).addReg(Addr.Base.Reg);
507
508 // Indexed form.
509 } else {
510 // Get the RR opcode corresponding to the RI one. FIXME: It would be
511 // preferable to use the ImmToIdxMap from PPCRegisterInfo.cpp, but it
512 // is hard to get at.
513 switch (Opc) {
514 default: llvm_unreachable("Unexpected opcode!");
515 case PPC::LBZ: Opc = PPC::LBZX; break;
516 case PPC::LBZ8: Opc = PPC::LBZX8; break;
517 case PPC::LHZ: Opc = PPC::LHZX; break;
518 case PPC::LHZ8: Opc = PPC::LHZX8; break;
519 case PPC::LHA: Opc = PPC::LHAX; break;
520 case PPC::LHA8: Opc = PPC::LHAX8; break;
521 case PPC::LWZ: Opc = PPC::LWZX; break;
522 case PPC::LWZ8: Opc = PPC::LWZX8; break;
523 case PPC::LWA: Opc = PPC::LWAX; break;
524 case PPC::LWA_32: Opc = PPC::LWAX_32; break;
525 case PPC::LD: Opc = PPC::LDX; break;
526 case PPC::LFS: Opc = PPC::LFSX; break;
527 case PPC::LFD: Opc = PPC::LFDX; break;
528 }
Rafael Espindolaea09c592014-02-18 22:05:46 +0000529 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Bill Schmidtccecf262013-08-30 02:29:45 +0000530 .addReg(Addr.Base.Reg).addReg(IndexReg);
531 }
532
533 return true;
534}
535
536// Attempt to fast-select a load instruction.
537bool PPCFastISel::SelectLoad(const Instruction *I) {
538 // FIXME: No atomic loads are supported.
539 if (cast<LoadInst>(I)->isAtomic())
540 return false;
541
542 // Verify we have a legal type before going any further.
543 MVT VT;
544 if (!isLoadTypeLegal(I->getType(), VT))
545 return false;
546
547 // See if we can handle this address.
548 Address Addr;
549 if (!PPCComputeAddress(I->getOperand(0), Addr))
550 return false;
551
552 // Look at the currently assigned register for this instruction
553 // to determine the required register class. This is necessary
554 // to constrain RA from using R0/X0 when this is not legal.
555 unsigned AssignedReg = FuncInfo.ValueMap[I];
556 const TargetRegisterClass *RC =
Craig Topper062a2ba2014-04-25 05:30:21 +0000557 AssignedReg ? MRI.getRegClass(AssignedReg) : nullptr;
Bill Schmidtccecf262013-08-30 02:29:45 +0000558
559 unsigned ResultReg = 0;
560 if (!PPCEmitLoad(VT, ResultReg, Addr, RC))
561 return false;
562 UpdateValueMap(I, ResultReg);
563 return true;
564}
565
566// Emit a store instruction to store SrcReg at Addr.
567bool PPCFastISel::PPCEmitStore(MVT VT, unsigned SrcReg, Address &Addr) {
568 assert(SrcReg && "Nothing to store!");
569 unsigned Opc;
570 bool UseOffset = true;
571
572 const TargetRegisterClass *RC = MRI.getRegClass(SrcReg);
573 bool Is32BitInt = RC->hasSuperClassEq(&PPC::GPRCRegClass);
574
575 switch (VT.SimpleTy) {
576 default: // e.g., vector types not handled
577 return false;
578 case MVT::i8:
579 Opc = Is32BitInt ? PPC::STB : PPC::STB8;
580 break;
581 case MVT::i16:
582 Opc = Is32BitInt ? PPC::STH : PPC::STH8;
583 break;
584 case MVT::i32:
585 assert(Is32BitInt && "Not GPRC for i32??");
586 Opc = PPC::STW;
587 break;
588 case MVT::i64:
589 Opc = PPC::STD;
590 UseOffset = ((Addr.Offset & 3) == 0);
591 break;
592 case MVT::f32:
593 Opc = PPC::STFS;
594 break;
595 case MVT::f64:
596 Opc = PPC::STFD;
597 break;
598 }
599
600 // If necessary, materialize the offset into a register and use
601 // the indexed form. Also handle stack pointers with special needs.
602 unsigned IndexReg = 0;
603 PPCSimplifyAddress(Addr, VT, UseOffset, IndexReg);
604
605 // Note: If we still have a frame index here, we know the offset is
606 // in range, as otherwise PPCSimplifyAddress would have converted it
607 // into a RegBase.
608 if (Addr.BaseType == Address::FrameIndexBase) {
609 MachineMemOperand *MMO =
610 FuncInfo.MF->getMachineMemOperand(
611 MachinePointerInfo::getFixedStack(Addr.Base.FI, Addr.Offset),
612 MachineMemOperand::MOStore, MFI.getObjectSize(Addr.Base.FI),
613 MFI.getObjectAlignment(Addr.Base.FI));
614
Rafael Espindolaea09c592014-02-18 22:05:46 +0000615 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
616 .addReg(SrcReg)
617 .addImm(Addr.Offset)
618 .addFrameIndex(Addr.Base.FI)
619 .addMemOperand(MMO);
Bill Schmidtccecf262013-08-30 02:29:45 +0000620
621 // Base reg with offset in range.
Bill Schmidt72e3d55a2013-08-30 03:07:11 +0000622 } else if (UseOffset)
Rafael Espindolaea09c592014-02-18 22:05:46 +0000623 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
Bill Schmidtccecf262013-08-30 02:29:45 +0000624 .addReg(SrcReg).addImm(Addr.Offset).addReg(Addr.Base.Reg);
625
626 // Indexed form.
Bill Schmidt72e3d55a2013-08-30 03:07:11 +0000627 else {
Bill Schmidtccecf262013-08-30 02:29:45 +0000628 // Get the RR opcode corresponding to the RI one. FIXME: It would be
629 // preferable to use the ImmToIdxMap from PPCRegisterInfo.cpp, but it
630 // is hard to get at.
631 switch (Opc) {
632 default: llvm_unreachable("Unexpected opcode!");
633 case PPC::STB: Opc = PPC::STBX; break;
634 case PPC::STH : Opc = PPC::STHX; break;
635 case PPC::STW : Opc = PPC::STWX; break;
636 case PPC::STB8: Opc = PPC::STBX8; break;
637 case PPC::STH8: Opc = PPC::STHX8; break;
638 case PPC::STW8: Opc = PPC::STWX8; break;
639 case PPC::STD: Opc = PPC::STDX; break;
640 case PPC::STFS: Opc = PPC::STFSX; break;
641 case PPC::STFD: Opc = PPC::STFDX; break;
642 }
Rafael Espindolaea09c592014-02-18 22:05:46 +0000643 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
Bill Schmidtccecf262013-08-30 02:29:45 +0000644 .addReg(SrcReg).addReg(Addr.Base.Reg).addReg(IndexReg);
645 }
646
647 return true;
648}
649
650// Attempt to fast-select a store instruction.
651bool PPCFastISel::SelectStore(const Instruction *I) {
652 Value *Op0 = I->getOperand(0);
653 unsigned SrcReg = 0;
654
655 // FIXME: No atomics loads are supported.
656 if (cast<StoreInst>(I)->isAtomic())
657 return false;
658
659 // Verify we have a legal type before going any further.
660 MVT VT;
661 if (!isLoadTypeLegal(Op0->getType(), VT))
662 return false;
663
664 // Get the value to be stored into a register.
665 SrcReg = getRegForValue(Op0);
666 if (SrcReg == 0)
667 return false;
668
669 // See if we can handle this address.
670 Address Addr;
671 if (!PPCComputeAddress(I->getOperand(1), Addr))
672 return false;
673
674 if (!PPCEmitStore(VT, SrcReg, Addr))
675 return false;
676
677 return true;
678}
679
Bill Schmidt03008132013-08-25 22:33:42 +0000680// Attempt to fast-select a branch instruction.
681bool PPCFastISel::SelectBranch(const Instruction *I) {
682 const BranchInst *BI = cast<BranchInst>(I);
683 MachineBasicBlock *BrBB = FuncInfo.MBB;
684 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
685 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
686
687 // For now, just try the simplest case where it's fed by a compare.
688 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
689 Optional<PPC::Predicate> OptPPCPred = getComparePred(CI->getPredicate());
690 if (!OptPPCPred)
691 return false;
692
693 PPC::Predicate PPCPred = OptPPCPred.getValue();
694
695 // Take advantage of fall-through opportunities.
696 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
697 std::swap(TBB, FBB);
698 PPCPred = PPC::InvertPredicate(PPCPred);
699 }
700
701 unsigned CondReg = createResultReg(&PPC::CRRCRegClass);
702
703 if (!PPCEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned(),
704 CondReg))
705 return false;
706
Rafael Espindolaea09c592014-02-18 22:05:46 +0000707 BuildMI(*BrBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::BCC))
Bill Schmidt03008132013-08-25 22:33:42 +0000708 .addImm(PPCPred).addReg(CondReg).addMBB(TBB);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000709 FastEmitBranch(FBB, DbgLoc);
Bill Schmidt03008132013-08-25 22:33:42 +0000710 FuncInfo.MBB->addSuccessor(TBB);
711 return true;
712
713 } else if (const ConstantInt *CI =
714 dyn_cast<ConstantInt>(BI->getCondition())) {
715 uint64_t Imm = CI->getZExtValue();
716 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
Rafael Espindolaea09c592014-02-18 22:05:46 +0000717 FastEmitBranch(Target, DbgLoc);
Bill Schmidt03008132013-08-25 22:33:42 +0000718 return true;
719 }
720
721 // FIXME: ARM looks for a case where the block containing the compare
722 // has been split from the block containing the branch. If this happens,
723 // there is a vreg available containing the result of the compare. I'm
724 // not sure we can do much, as we've lost the predicate information with
725 // the compare instruction -- we have a 4-bit CR but don't know which bit
726 // to test here.
727 return false;
728}
729
730// Attempt to emit a compare of the two source values. Signed and unsigned
731// comparisons are supported. Return false if we can't handle it.
732bool PPCFastISel::PPCEmitCmp(const Value *SrcValue1, const Value *SrcValue2,
733 bool IsZExt, unsigned DestReg) {
734 Type *Ty = SrcValue1->getType();
735 EVT SrcEVT = TLI.getValueType(Ty, true);
736 if (!SrcEVT.isSimple())
737 return false;
738 MVT SrcVT = SrcEVT.getSimpleVT();
739
Eric Christopher1b8e7632014-05-22 01:07:24 +0000740 if (SrcVT == MVT::i1 && PPCSubTarget->useCRBits())
Hal Finkel940ab932014-02-28 00:27:01 +0000741 return false;
742
Bill Schmidt03008132013-08-25 22:33:42 +0000743 // 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) {
Bill Schmidtff9622e2014-03-18 14:32:50 +0000898 if (!IsSigned) {
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000899 LoadOpc = PPC::LFIWZX;
Bill Schmidtff9622e2014-03-18 14:32:50 +0000900 Addr.Offset = 4;
Eric Christopher1b8e7632014-05-22 01:07:24 +0000901 } else if (PPCSubTarget->hasLFIWAX()) {
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000902 LoadOpc = PPC::LFIWAX;
Bill Schmidtff9622e2014-03-18 14:32:50 +0000903 Addr.Offset = 4;
904 }
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000905 }
906
907 const TargetRegisterClass *RC = &PPC::F8RCRegClass;
908 unsigned ResultReg = 0;
909 if (!PPCEmitLoad(MVT::f64, ResultReg, Addr, RC, !IsSigned, LoadOpc))
910 return 0;
911
912 return ResultReg;
913}
914
915// Attempt to fast-select an integer-to-floating-point conversion.
916bool PPCFastISel::SelectIToFP(const Instruction *I, bool IsSigned) {
917 MVT DstVT;
918 Type *DstTy = I->getType();
919 if (!isTypeLegal(DstTy, DstVT))
920 return false;
921
922 if (DstVT != MVT::f32 && DstVT != MVT::f64)
923 return false;
924
925 Value *Src = I->getOperand(0);
926 EVT SrcEVT = TLI.getValueType(Src->getType(), true);
927 if (!SrcEVT.isSimple())
928 return false;
929
930 MVT SrcVT = SrcEVT.getSimpleVT();
931
932 if (SrcVT != MVT::i8 && SrcVT != MVT::i16 &&
933 SrcVT != MVT::i32 && SrcVT != MVT::i64)
934 return false;
935
936 unsigned SrcReg = getRegForValue(Src);
937 if (SrcReg == 0)
938 return false;
939
940 // We can only lower an unsigned convert if we have the newer
941 // floating-point conversion operations.
Eric Christopher1b8e7632014-05-22 01:07:24 +0000942 if (!IsSigned && !PPCSubTarget->hasFPCVT())
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000943 return false;
944
945 // FIXME: For now we require the newer floating-point conversion operations
946 // (which are present only on P7 and A2 server models) when converting
947 // to single-precision float. Otherwise we have to generate a lot of
948 // fiddly code to avoid double rounding. If necessary, the fiddly code
949 // can be found in PPCTargetLowering::LowerINT_TO_FP().
Eric Christopher1b8e7632014-05-22 01:07:24 +0000950 if (DstVT == MVT::f32 && !PPCSubTarget->hasFPCVT())
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000951 return false;
952
953 // Extend the input if necessary.
954 if (SrcVT == MVT::i8 || SrcVT == MVT::i16) {
955 unsigned TmpReg = createResultReg(&PPC::G8RCRegClass);
956 if (!PPCEmitIntExt(SrcVT, SrcReg, MVT::i64, TmpReg, !IsSigned))
957 return false;
958 SrcVT = MVT::i64;
959 SrcReg = TmpReg;
960 }
961
962 // Move the integer value to an FPR.
963 unsigned FPReg = PPCMoveToFPReg(SrcVT, SrcReg, IsSigned);
964 if (FPReg == 0)
965 return false;
966
967 // Determine the opcode for the conversion.
968 const TargetRegisterClass *RC = &PPC::F8RCRegClass;
969 unsigned DestReg = createResultReg(RC);
970 unsigned Opc;
971
972 if (DstVT == MVT::f32)
973 Opc = IsSigned ? PPC::FCFIDS : PPC::FCFIDUS;
974 else
975 Opc = IsSigned ? PPC::FCFID : PPC::FCFIDU;
976
977 // Generate the convert.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000978 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000979 .addReg(FPReg);
980
981 UpdateValueMap(I, DestReg);
982 return true;
983}
984
985// Move the floating-point value in SrcReg into an integer destination
986// register, and return the register (or zero if we can't handle it).
987// FIXME: When direct register moves are implemented (see PowerISA 2.08),
988// those should be used instead of moving via a stack slot when the
989// subtarget permits.
990unsigned PPCFastISel::PPCMoveToIntReg(const Instruction *I, MVT VT,
991 unsigned SrcReg, bool IsSigned) {
992 // Get a stack slot 8 bytes wide, aligned on an 8-byte boundary.
993 // Note that if have STFIWX available, we could use a 4-byte stack
994 // slot for i32, but this being fast-isel we'll just go with the
995 // easiest code gen possible.
996 Address Addr;
997 Addr.BaseType = Address::FrameIndexBase;
998 Addr.Base.FI = MFI.CreateStackObject(8, 8, false);
999
1000 // Store the value from the FPR.
1001 if (!PPCEmitStore(MVT::f64, SrcReg, Addr))
1002 return 0;
1003
1004 // Reload it into a GPR. If we want an i32, modify the address
1005 // to have a 4-byte offset so we load from the right place.
1006 if (VT == MVT::i32)
1007 Addr.Offset = 4;
1008
1009 // Look at the currently assigned register for this instruction
1010 // to determine the required register class.
1011 unsigned AssignedReg = FuncInfo.ValueMap[I];
1012 const TargetRegisterClass *RC =
Craig Topper062a2ba2014-04-25 05:30:21 +00001013 AssignedReg ? MRI.getRegClass(AssignedReg) : nullptr;
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001014
1015 unsigned ResultReg = 0;
1016 if (!PPCEmitLoad(VT, ResultReg, Addr, RC, !IsSigned))
1017 return 0;
1018
1019 return ResultReg;
1020}
1021
1022// Attempt to fast-select a floating-point-to-integer conversion.
1023bool PPCFastISel::SelectFPToI(const Instruction *I, bool IsSigned) {
1024 MVT DstVT, SrcVT;
1025 Type *DstTy = I->getType();
1026 if (!isTypeLegal(DstTy, DstVT))
1027 return false;
1028
1029 if (DstVT != MVT::i32 && DstVT != MVT::i64)
1030 return false;
1031
Bill Schmidt83973ef2014-06-24 20:05:18 +00001032 // If we don't have FCTIDUZ and we need it, punt to SelectionDAG.
1033 if (DstVT == MVT::i64 && !IsSigned && !PPCSubTarget->hasFPCVT())
1034 return false;
1035
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001036 Value *Src = I->getOperand(0);
1037 Type *SrcTy = Src->getType();
1038 if (!isTypeLegal(SrcTy, SrcVT))
1039 return false;
1040
1041 if (SrcVT != MVT::f32 && SrcVT != MVT::f64)
1042 return false;
1043
1044 unsigned SrcReg = getRegForValue(Src);
1045 if (SrcReg == 0)
1046 return false;
1047
1048 // Convert f32 to f64 if necessary. This is just a meaningless copy
1049 // to get the register class right. COPY_TO_REGCLASS is needed since
1050 // a COPY from F4RC to F8RC is converted to a F4RC-F4RC copy downstream.
1051 const TargetRegisterClass *InRC = MRI.getRegClass(SrcReg);
1052 if (InRC == &PPC::F4RCRegClass) {
1053 unsigned TmpReg = createResultReg(&PPC::F8RCRegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001054 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001055 TII.get(TargetOpcode::COPY_TO_REGCLASS), TmpReg)
1056 .addReg(SrcReg).addImm(PPC::F8RCRegClassID);
1057 SrcReg = TmpReg;
1058 }
1059
1060 // Determine the opcode for the conversion, which takes place
1061 // entirely within FPRs.
1062 unsigned DestReg = createResultReg(&PPC::F8RCRegClass);
1063 unsigned Opc;
1064
1065 if (DstVT == MVT::i32)
1066 if (IsSigned)
1067 Opc = PPC::FCTIWZ;
1068 else
Eric Christopher1b8e7632014-05-22 01:07:24 +00001069 Opc = PPCSubTarget->hasFPCVT() ? PPC::FCTIWUZ : PPC::FCTIDZ;
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001070 else
1071 Opc = IsSigned ? PPC::FCTIDZ : PPC::FCTIDUZ;
1072
1073 // Generate the convert.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001074 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001075 .addReg(SrcReg);
1076
1077 // Now move the integer value from a float register to an integer register.
1078 unsigned IntReg = PPCMoveToIntReg(I, DstVT, DestReg, IsSigned);
1079 if (IntReg == 0)
1080 return false;
1081
1082 UpdateValueMap(I, IntReg);
1083 return true;
1084}
1085
Bill Schmidtccecf262013-08-30 02:29:45 +00001086// Attempt to fast-select a binary integer operation that isn't already
1087// handled automatically.
1088bool PPCFastISel::SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode) {
1089 EVT DestVT = TLI.getValueType(I->getType(), true);
1090
1091 // We can get here in the case when we have a binary operation on a non-legal
1092 // type and the target independent selector doesn't know how to handle it.
1093 if (DestVT != MVT::i16 && DestVT != MVT::i8)
1094 return false;
1095
1096 // Look at the currently assigned register for this instruction
1097 // to determine the required register class. If there is no register,
1098 // make a conservative choice (don't assign R0).
1099 unsigned AssignedReg = FuncInfo.ValueMap[I];
1100 const TargetRegisterClass *RC =
1101 (AssignedReg ? MRI.getRegClass(AssignedReg) :
1102 &PPC::GPRC_and_GPRC_NOR0RegClass);
1103 bool IsGPRC = RC->hasSuperClassEq(&PPC::GPRCRegClass);
1104
1105 unsigned Opc;
1106 switch (ISDOpcode) {
1107 default: return false;
1108 case ISD::ADD:
1109 Opc = IsGPRC ? PPC::ADD4 : PPC::ADD8;
1110 break;
1111 case ISD::OR:
1112 Opc = IsGPRC ? PPC::OR : PPC::OR8;
1113 break;
1114 case ISD::SUB:
1115 Opc = IsGPRC ? PPC::SUBF : PPC::SUBF8;
1116 break;
1117 }
1118
1119 unsigned ResultReg = createResultReg(RC ? RC : &PPC::G8RCRegClass);
1120 unsigned SrcReg1 = getRegForValue(I->getOperand(0));
1121 if (SrcReg1 == 0) return false;
1122
1123 // Handle case of small immediate operand.
1124 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(I->getOperand(1))) {
1125 const APInt &CIVal = ConstInt->getValue();
1126 int Imm = (int)CIVal.getSExtValue();
1127 bool UseImm = true;
1128 if (isInt<16>(Imm)) {
1129 switch (Opc) {
1130 default:
1131 llvm_unreachable("Missing case!");
1132 case PPC::ADD4:
1133 Opc = PPC::ADDI;
1134 MRI.setRegClass(SrcReg1, &PPC::GPRC_and_GPRC_NOR0RegClass);
1135 break;
1136 case PPC::ADD8:
1137 Opc = PPC::ADDI8;
1138 MRI.setRegClass(SrcReg1, &PPC::G8RC_and_G8RC_NOX0RegClass);
1139 break;
1140 case PPC::OR:
1141 Opc = PPC::ORI;
1142 break;
1143 case PPC::OR8:
1144 Opc = PPC::ORI8;
1145 break;
1146 case PPC::SUBF:
1147 if (Imm == -32768)
1148 UseImm = false;
1149 else {
1150 Opc = PPC::ADDI;
1151 MRI.setRegClass(SrcReg1, &PPC::GPRC_and_GPRC_NOR0RegClass);
1152 Imm = -Imm;
1153 }
1154 break;
1155 case PPC::SUBF8:
1156 if (Imm == -32768)
1157 UseImm = false;
1158 else {
1159 Opc = PPC::ADDI8;
1160 MRI.setRegClass(SrcReg1, &PPC::G8RC_and_G8RC_NOX0RegClass);
1161 Imm = -Imm;
1162 }
1163 break;
1164 }
1165
1166 if (UseImm) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001167 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
1168 ResultReg)
1169 .addReg(SrcReg1)
1170 .addImm(Imm);
Bill Schmidtccecf262013-08-30 02:29:45 +00001171 UpdateValueMap(I, ResultReg);
1172 return true;
1173 }
1174 }
1175 }
1176
1177 // Reg-reg case.
1178 unsigned SrcReg2 = getRegForValue(I->getOperand(1));
1179 if (SrcReg2 == 0) return false;
1180
1181 // Reverse operands for subtract-from.
1182 if (ISDOpcode == ISD::SUB)
1183 std::swap(SrcReg1, SrcReg2);
1184
Rafael Espindolaea09c592014-02-18 22:05:46 +00001185 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Bill Schmidtccecf262013-08-30 02:29:45 +00001186 .addReg(SrcReg1).addReg(SrcReg2);
1187 UpdateValueMap(I, ResultReg);
1188 return true;
1189}
1190
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001191// Handle arguments to a call that we're attempting to fast-select.
1192// Return false if the arguments are too complex for us at the moment.
1193bool PPCFastISel::processCallArgs(SmallVectorImpl<Value*> &Args,
1194 SmallVectorImpl<unsigned> &ArgRegs,
1195 SmallVectorImpl<MVT> &ArgVTs,
1196 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1197 SmallVectorImpl<unsigned> &RegArgs,
1198 CallingConv::ID CC,
1199 unsigned &NumBytes,
1200 bool IsVarArg) {
1201 SmallVector<CCValAssign, 16> ArgLocs;
1202 CCState CCInfo(CC, IsVarArg, *FuncInfo.MF, TM, ArgLocs, *Context);
Ulrich Weigandf316e1d2014-06-23 13:47:52 +00001203
1204 // Reserve space for the linkage area on the stack.
Ulrich Weigand8658f172014-07-20 23:43:15 +00001205 bool isELFv2ABI = PPCSubTarget->isELFv2ABI();
1206 unsigned LinkageSize = PPCFrameLowering::getLinkageSize(true, false,
1207 isELFv2ABI);
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00001208 CCInfo.AllocateStack(LinkageSize, 8);
Ulrich Weigandf316e1d2014-06-23 13:47:52 +00001209
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001210 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_PPC64_ELF_FIS);
1211
1212 // Bail out if we can't handle any of the arguments.
1213 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1214 CCValAssign &VA = ArgLocs[I];
1215 MVT ArgVT = ArgVTs[VA.getValNo()];
1216
1217 // Skip vector arguments for now, as well as long double and
1218 // uint128_t, and anything that isn't passed in a register.
Hal Finkel940ab932014-02-28 00:27:01 +00001219 if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64 || ArgVT == MVT::i1 ||
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001220 !VA.isRegLoc() || VA.needsCustom())
1221 return false;
1222
1223 // Skip bit-converted arguments for now.
1224 if (VA.getLocInfo() == CCValAssign::BCvt)
1225 return false;
1226 }
1227
1228 // Get a count of how many bytes are to be pushed onto the stack.
1229 NumBytes = CCInfo.getNextStackOffset();
1230
Ulrich Weigandf316e1d2014-06-23 13:47:52 +00001231 // The prolog code of the callee may store up to 8 GPR argument registers to
1232 // the stack, allowing va_start to index over them in memory if its varargs.
1233 // Because we cannot tell if this is needed on the caller side, we have to
1234 // conservatively assume that it is needed. As such, make sure we have at
1235 // least enough stack space for the caller to store the 8 GPRs.
Ulrich Weigand8658f172014-07-20 23:43:15 +00001236 // FIXME: On ELFv2, it may be unnecessary to allocate the parameter area.
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00001237 NumBytes = std::max(NumBytes, LinkageSize + 64);
Ulrich Weigandf316e1d2014-06-23 13:47:52 +00001238
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001239 // Issue CALLSEQ_START.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001240 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001241 TII.get(TII.getCallFrameSetupOpcode()))
1242 .addImm(NumBytes);
1243
1244 // Prepare to assign register arguments. Every argument uses up a
1245 // GPR protocol register even if it's passed in a floating-point
1246 // register.
1247 unsigned NextGPR = PPC::X3;
1248 unsigned NextFPR = PPC::F1;
1249
1250 // Process arguments.
1251 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1252 CCValAssign &VA = ArgLocs[I];
1253 unsigned Arg = ArgRegs[VA.getValNo()];
1254 MVT ArgVT = ArgVTs[VA.getValNo()];
1255
1256 // Handle argument promotion and bitcasts.
1257 switch (VA.getLocInfo()) {
1258 default:
1259 llvm_unreachable("Unknown loc info!");
1260 case CCValAssign::Full:
1261 break;
1262 case CCValAssign::SExt: {
1263 MVT DestVT = VA.getLocVT();
1264 const TargetRegisterClass *RC =
1265 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1266 unsigned TmpReg = createResultReg(RC);
1267 if (!PPCEmitIntExt(ArgVT, Arg, DestVT, TmpReg, /*IsZExt*/false))
1268 llvm_unreachable("Failed to emit a sext!");
1269 ArgVT = DestVT;
1270 Arg = TmpReg;
1271 break;
1272 }
1273 case CCValAssign::AExt:
1274 case CCValAssign::ZExt: {
1275 MVT DestVT = VA.getLocVT();
1276 const TargetRegisterClass *RC =
1277 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1278 unsigned TmpReg = createResultReg(RC);
1279 if (!PPCEmitIntExt(ArgVT, Arg, DestVT, TmpReg, /*IsZExt*/true))
1280 llvm_unreachable("Failed to emit a zext!");
1281 ArgVT = DestVT;
1282 Arg = TmpReg;
1283 break;
1284 }
1285 case CCValAssign::BCvt: {
1286 // FIXME: Not yet handled.
1287 llvm_unreachable("Should have bailed before getting here!");
1288 break;
1289 }
1290 }
1291
1292 // Copy this argument to the appropriate register.
1293 unsigned ArgReg;
1294 if (ArgVT == MVT::f32 || ArgVT == MVT::f64) {
1295 ArgReg = NextFPR++;
1296 ++NextGPR;
1297 } else
1298 ArgReg = NextGPR++;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001299
1300 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1301 TII.get(TargetOpcode::COPY), ArgReg).addReg(Arg);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001302 RegArgs.push_back(ArgReg);
1303 }
1304
1305 return true;
1306}
1307
1308// For a call that we've determined we can fast-select, finish the
1309// call sequence and generate a copy to obtain the return value (if any).
1310void PPCFastISel::finishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
1311 const Instruction *I, CallingConv::ID CC,
1312 unsigned &NumBytes, bool IsVarArg) {
1313 // Issue CallSEQ_END.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001314 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001315 TII.get(TII.getCallFrameDestroyOpcode()))
1316 .addImm(NumBytes).addImm(0);
1317
1318 // Next, generate a copy to obtain the return value.
1319 // FIXME: No multi-register return values yet, though I don't foresee
1320 // any real difficulties there.
1321 if (RetVT != MVT::isVoid) {
1322 SmallVector<CCValAssign, 16> RVLocs;
1323 CCState CCInfo(CC, IsVarArg, *FuncInfo.MF, TM, RVLocs, *Context);
1324 CCInfo.AnalyzeCallResult(RetVT, RetCC_PPC64_ELF_FIS);
1325 CCValAssign &VA = RVLocs[0];
1326 assert(RVLocs.size() == 1 && "No support for multi-reg return values!");
1327 assert(VA.isRegLoc() && "Can only return in registers!");
1328
1329 MVT DestVT = VA.getValVT();
1330 MVT CopyVT = DestVT;
1331
1332 // Ints smaller than a register still arrive in a full 64-bit
1333 // register, so make sure we recognize this.
1334 if (RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32)
1335 CopyVT = MVT::i64;
1336
1337 unsigned SourcePhysReg = VA.getLocReg();
Bill Schmidt0954ea12013-08-30 23:25:30 +00001338 unsigned ResultReg = 0;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001339
1340 if (RetVT == CopyVT) {
1341 const TargetRegisterClass *CpyRC = TLI.getRegClassFor(CopyVT);
1342 ResultReg = createResultReg(CpyRC);
1343
Rafael Espindolaea09c592014-02-18 22:05:46 +00001344 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001345 TII.get(TargetOpcode::COPY), ResultReg)
1346 .addReg(SourcePhysReg);
1347
1348 // If necessary, round the floating result to single precision.
1349 } else if (CopyVT == MVT::f64) {
1350 ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
Rafael Espindolaea09c592014-02-18 22:05:46 +00001351 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::FRSP),
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001352 ResultReg).addReg(SourcePhysReg);
1353
1354 // If only the low half of a general register is needed, generate
1355 // a GPRC copy instead of a G8RC copy. (EXTRACT_SUBREG can't be
1356 // used along the fast-isel path (not lowered), and downstream logic
1357 // also doesn't like a direct subreg copy on a physical reg.)
1358 } else if (RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32) {
1359 ResultReg = createResultReg(&PPC::GPRCRegClass);
1360 // Convert physical register from G8RC to GPRC.
1361 SourcePhysReg -= PPC::X0 - PPC::R0;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001362 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001363 TII.get(TargetOpcode::COPY), ResultReg)
1364 .addReg(SourcePhysReg);
1365 }
1366
Bill Schmidt0954ea12013-08-30 23:25:30 +00001367 assert(ResultReg && "ResultReg unset!");
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001368 UsedRegs.push_back(SourcePhysReg);
1369 UpdateValueMap(I, ResultReg);
1370 }
1371}
1372
1373// Attempt to fast-select a call instruction.
1374bool PPCFastISel::SelectCall(const Instruction *I) {
1375 const CallInst *CI = cast<CallInst>(I);
1376 const Value *Callee = CI->getCalledValue();
1377
1378 // Can't handle inline asm.
1379 if (isa<InlineAsm>(Callee))
1380 return false;
1381
1382 // Allow SelectionDAG isel to handle tail calls.
1383 if (CI->isTailCall())
1384 return false;
1385
1386 // Obtain calling convention.
1387 ImmutableCallSite CS(CI);
1388 CallingConv::ID CC = CS.getCallingConv();
1389
1390 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1391 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1392 bool IsVarArg = FTy->isVarArg();
1393
1394 // Not ready for varargs yet.
1395 if (IsVarArg)
1396 return false;
1397
1398 // Handle simple calls for now, with legal return types and
1399 // those that can be extended.
1400 Type *RetTy = I->getType();
1401 MVT RetVT;
1402 if (RetTy->isVoidTy())
1403 RetVT = MVT::isVoid;
1404 else if (!isTypeLegal(RetTy, RetVT) && RetVT != MVT::i16 &&
1405 RetVT != MVT::i8)
1406 return false;
1407
1408 // FIXME: No multi-register return values yet.
1409 if (RetVT != MVT::isVoid && RetVT != MVT::i8 && RetVT != MVT::i16 &&
1410 RetVT != MVT::i32 && RetVT != MVT::i64 && RetVT != MVT::f32 &&
1411 RetVT != MVT::f64) {
1412 SmallVector<CCValAssign, 16> RVLocs;
1413 CCState CCInfo(CC, IsVarArg, *FuncInfo.MF, TM, RVLocs, *Context);
1414 CCInfo.AnalyzeCallResult(RetVT, RetCC_PPC64_ELF_FIS);
1415 if (RVLocs.size() > 1)
1416 return false;
1417 }
1418
1419 // Bail early if more than 8 arguments, as we only currently
1420 // handle arguments passed in registers.
1421 unsigned NumArgs = CS.arg_size();
1422 if (NumArgs > 8)
1423 return false;
1424
1425 // Set up the argument vectors.
1426 SmallVector<Value*, 8> Args;
1427 SmallVector<unsigned, 8> ArgRegs;
1428 SmallVector<MVT, 8> ArgVTs;
1429 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1430
1431 Args.reserve(NumArgs);
1432 ArgRegs.reserve(NumArgs);
1433 ArgVTs.reserve(NumArgs);
1434 ArgFlags.reserve(NumArgs);
1435
1436 for (ImmutableCallSite::arg_iterator II = CS.arg_begin(), IE = CS.arg_end();
1437 II != IE; ++II) {
1438 // FIXME: ARM does something for intrinsic calls here, check into that.
1439
1440 unsigned AttrIdx = II - CS.arg_begin() + 1;
1441
1442 // Only handle easy calls for now. It would be reasonably easy
1443 // to handle <= 8-byte structures passed ByVal in registers, but we
1444 // have to ensure they are right-justified in the register.
1445 if (CS.paramHasAttr(AttrIdx, Attribute::InReg) ||
1446 CS.paramHasAttr(AttrIdx, Attribute::StructRet) ||
1447 CS.paramHasAttr(AttrIdx, Attribute::Nest) ||
1448 CS.paramHasAttr(AttrIdx, Attribute::ByVal))
1449 return false;
1450
1451 ISD::ArgFlagsTy Flags;
1452 if (CS.paramHasAttr(AttrIdx, Attribute::SExt))
1453 Flags.setSExt();
1454 if (CS.paramHasAttr(AttrIdx, Attribute::ZExt))
1455 Flags.setZExt();
1456
1457 Type *ArgTy = (*II)->getType();
1458 MVT ArgVT;
1459 if (!isTypeLegal(ArgTy, ArgVT) && ArgVT != MVT::i16 && ArgVT != MVT::i8)
1460 return false;
1461
1462 if (ArgVT.isVector())
1463 return false;
1464
1465 unsigned Arg = getRegForValue(*II);
1466 if (Arg == 0)
1467 return false;
1468
Rafael Espindolaea09c592014-02-18 22:05:46 +00001469 unsigned OriginalAlignment = DL.getABITypeAlignment(ArgTy);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001470 Flags.setOrigAlign(OriginalAlignment);
1471
1472 Args.push_back(*II);
1473 ArgRegs.push_back(Arg);
1474 ArgVTs.push_back(ArgVT);
1475 ArgFlags.push_back(Flags);
1476 }
1477
1478 // Process the arguments.
1479 SmallVector<unsigned, 8> RegArgs;
1480 unsigned NumBytes;
1481
1482 if (!processCallArgs(Args, ArgRegs, ArgVTs, ArgFlags,
1483 RegArgs, CC, NumBytes, IsVarArg))
1484 return false;
1485
1486 // FIXME: No handling for function pointers yet. This requires
1487 // implementing the function descriptor (OPD) setup.
1488 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
1489 if (!GV)
1490 return false;
1491
1492 // Build direct call with NOP for TOC restore.
1493 // FIXME: We can and should optimize away the NOP for local calls.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001494 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001495 TII.get(PPC::BL8_NOP));
1496 // Add callee.
1497 MIB.addGlobalAddress(GV);
1498
1499 // Add implicit physical register uses to the call.
1500 for (unsigned II = 0, IE = RegArgs.size(); II != IE; ++II)
1501 MIB.addReg(RegArgs[II], RegState::Implicit);
1502
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00001503 // Direct calls in the ELFv2 ABI need the TOC register live into the call.
1504 if (PPCSubTarget->isELFv2ABI())
1505 MIB.addReg(PPC::X2, RegState::Implicit);
1506
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001507 // Add a register mask with the call-preserved registers. Proper
1508 // defs for return values will be added by setPhysRegsDeadExcept().
1509 MIB.addRegMask(TRI.getCallPreservedMask(CC));
1510
1511 // Finish off the call including any return values.
1512 SmallVector<unsigned, 4> UsedRegs;
1513 finishCall(RetVT, UsedRegs, I, CC, NumBytes, IsVarArg);
1514
1515 // Set all unused physregs defs as dead.
1516 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
1517
1518 return true;
1519}
1520
Bill Schmidtd89f6782013-08-26 19:42:51 +00001521// Attempt to fast-select a return instruction.
1522bool PPCFastISel::SelectRet(const Instruction *I) {
1523
1524 if (!FuncInfo.CanLowerReturn)
1525 return false;
1526
1527 const ReturnInst *Ret = cast<ReturnInst>(I);
1528 const Function &F = *I->getParent()->getParent();
1529
1530 // Build a list of return value registers.
1531 SmallVector<unsigned, 4> RetRegs;
1532 CallingConv::ID CC = F.getCallingConv();
1533
1534 if (Ret->getNumOperands() > 0) {
1535 SmallVector<ISD::OutputArg, 4> Outs;
1536 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI);
1537
1538 // Analyze operands of the call, assigning locations to each operand.
1539 SmallVector<CCValAssign, 16> ValLocs;
1540 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, TM, ValLocs, *Context);
1541 CCInfo.AnalyzeReturn(Outs, RetCC_PPC64_ELF_FIS);
1542 const Value *RV = Ret->getOperand(0);
1543
1544 // FIXME: Only one output register for now.
1545 if (ValLocs.size() > 1)
1546 return false;
1547
1548 // Special case for returning a constant integer of any size.
1549 // Materialize the constant as an i64 and copy it to the return
1550 // register. This avoids an unnecessary extend or truncate.
1551 if (isa<ConstantInt>(*RV)) {
1552 const Constant *C = cast<Constant>(RV);
1553 unsigned SrcReg = PPCMaterializeInt(C, MVT::i64);
1554 unsigned RetReg = ValLocs[0].getLocReg();
Rafael Espindolaea09c592014-02-18 22:05:46 +00001555 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1556 TII.get(TargetOpcode::COPY), RetReg).addReg(SrcReg);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001557 RetRegs.push_back(RetReg);
1558
1559 } else {
1560 unsigned Reg = getRegForValue(RV);
1561
1562 if (Reg == 0)
1563 return false;
1564
1565 // Copy the result values into the output registers.
1566 for (unsigned i = 0; i < ValLocs.size(); ++i) {
1567
1568 CCValAssign &VA = ValLocs[i];
1569 assert(VA.isRegLoc() && "Can only return in registers!");
1570 RetRegs.push_back(VA.getLocReg());
1571 unsigned SrcReg = Reg + VA.getValNo();
1572
1573 EVT RVEVT = TLI.getValueType(RV->getType());
1574 if (!RVEVT.isSimple())
1575 return false;
1576 MVT RVVT = RVEVT.getSimpleVT();
1577 MVT DestVT = VA.getLocVT();
1578
1579 if (RVVT != DestVT && RVVT != MVT::i8 &&
1580 RVVT != MVT::i16 && RVVT != MVT::i32)
1581 return false;
1582
1583 if (RVVT != DestVT) {
1584 switch (VA.getLocInfo()) {
1585 default:
1586 llvm_unreachable("Unknown loc info!");
1587 case CCValAssign::Full:
1588 llvm_unreachable("Full value assign but types don't match?");
1589 case CCValAssign::AExt:
1590 case CCValAssign::ZExt: {
1591 const TargetRegisterClass *RC =
1592 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1593 unsigned TmpReg = createResultReg(RC);
1594 if (!PPCEmitIntExt(RVVT, SrcReg, DestVT, TmpReg, true))
1595 return false;
1596 SrcReg = TmpReg;
1597 break;
1598 }
1599 case CCValAssign::SExt: {
1600 const TargetRegisterClass *RC =
1601 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1602 unsigned TmpReg = createResultReg(RC);
1603 if (!PPCEmitIntExt(RVVT, SrcReg, DestVT, TmpReg, false))
1604 return false;
1605 SrcReg = TmpReg;
1606 break;
1607 }
1608 }
1609 }
1610
Rafael Espindolaea09c592014-02-18 22:05:46 +00001611 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidtd89f6782013-08-26 19:42:51 +00001612 TII.get(TargetOpcode::COPY), RetRegs[i])
1613 .addReg(SrcReg);
1614 }
1615 }
1616 }
1617
Rafael Espindolaea09c592014-02-18 22:05:46 +00001618 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidtd89f6782013-08-26 19:42:51 +00001619 TII.get(PPC::BLR));
1620
1621 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
1622 MIB.addReg(RetRegs[i], RegState::Implicit);
1623
1624 return true;
1625}
1626
Bill Schmidt03008132013-08-25 22:33:42 +00001627// Attempt to emit an integer extend of SrcReg into DestReg. Both
1628// signed and zero extensions are supported. Return false if we
Bill Schmidtd89f6782013-08-26 19:42:51 +00001629// can't handle it.
Bill Schmidt03008132013-08-25 22:33:42 +00001630bool PPCFastISel::PPCEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1631 unsigned DestReg, bool IsZExt) {
Bill Schmidtd89f6782013-08-26 19:42:51 +00001632 if (DestVT != MVT::i32 && DestVT != MVT::i64)
1633 return false;
1634 if (SrcVT != MVT::i8 && SrcVT != MVT::i16 && SrcVT != MVT::i32)
1635 return false;
1636
1637 // Signed extensions use EXTSB, EXTSH, EXTSW.
1638 if (!IsZExt) {
1639 unsigned Opc;
1640 if (SrcVT == MVT::i8)
1641 Opc = (DestVT == MVT::i32) ? PPC::EXTSB : PPC::EXTSB8_32_64;
1642 else if (SrcVT == MVT::i16)
1643 Opc = (DestVT == MVT::i32) ? PPC::EXTSH : PPC::EXTSH8_32_64;
1644 else {
1645 assert(DestVT == MVT::i64 && "Signed extend from i32 to i32??");
1646 Opc = PPC::EXTSW_32_64;
1647 }
Rafael Espindolaea09c592014-02-18 22:05:46 +00001648 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidtd89f6782013-08-26 19:42:51 +00001649 .addReg(SrcReg);
1650
1651 // Unsigned 32-bit extensions use RLWINM.
1652 } else if (DestVT == MVT::i32) {
1653 unsigned MB;
1654 if (SrcVT == MVT::i8)
1655 MB = 24;
1656 else {
1657 assert(SrcVT == MVT::i16 && "Unsigned extend from i32 to i32??");
1658 MB = 16;
1659 }
Rafael Espindolaea09c592014-02-18 22:05:46 +00001660 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::RLWINM),
Bill Schmidtd89f6782013-08-26 19:42:51 +00001661 DestReg)
1662 .addReg(SrcReg).addImm(/*SH=*/0).addImm(MB).addImm(/*ME=*/31);
1663
1664 // Unsigned 64-bit extensions use RLDICL (with a 32-bit source).
1665 } else {
1666 unsigned MB;
1667 if (SrcVT == MVT::i8)
1668 MB = 56;
1669 else if (SrcVT == MVT::i16)
1670 MB = 48;
1671 else
1672 MB = 32;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001673 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidtd89f6782013-08-26 19:42:51 +00001674 TII.get(PPC::RLDICL_32_64), DestReg)
1675 .addReg(SrcReg).addImm(/*SH=*/0).addImm(MB);
1676 }
1677
1678 return true;
Bill Schmidt03008132013-08-25 22:33:42 +00001679}
1680
1681// Attempt to fast-select an indirect branch instruction.
1682bool PPCFastISel::SelectIndirectBr(const Instruction *I) {
1683 unsigned AddrReg = getRegForValue(I->getOperand(0));
1684 if (AddrReg == 0)
1685 return false;
1686
Rafael Espindolaea09c592014-02-18 22:05:46 +00001687 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::MTCTR8))
Bill Schmidt03008132013-08-25 22:33:42 +00001688 .addReg(AddrReg);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001689 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::BCTR8));
Bill Schmidt03008132013-08-25 22:33:42 +00001690
1691 const IndirectBrInst *IB = cast<IndirectBrInst>(I);
1692 for (unsigned i = 0, e = IB->getNumSuccessors(); i != e; ++i)
1693 FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[IB->getSuccessor(i)]);
1694
1695 return true;
1696}
1697
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001698// Attempt to fast-select an integer truncate instruction.
1699bool PPCFastISel::SelectTrunc(const Instruction *I) {
1700 Value *Src = I->getOperand(0);
1701 EVT SrcVT = TLI.getValueType(Src->getType(), true);
1702 EVT DestVT = TLI.getValueType(I->getType(), true);
1703
1704 if (SrcVT != MVT::i64 && SrcVT != MVT::i32 && SrcVT != MVT::i16)
1705 return false;
1706
1707 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8)
1708 return false;
1709
1710 unsigned SrcReg = getRegForValue(Src);
1711 if (!SrcReg)
1712 return false;
1713
1714 // The only interesting case is when we need to switch register classes.
1715 if (SrcVT == MVT::i64) {
1716 unsigned ResultReg = createResultReg(&PPC::GPRCRegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001717 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1718 TII.get(TargetOpcode::COPY),
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001719 ResultReg).addReg(SrcReg, 0, PPC::sub_32);
1720 SrcReg = ResultReg;
1721 }
1722
1723 UpdateValueMap(I, SrcReg);
1724 return true;
1725}
1726
Bill Schmidtd89f6782013-08-26 19:42:51 +00001727// Attempt to fast-select an integer extend instruction.
1728bool PPCFastISel::SelectIntExt(const Instruction *I) {
1729 Type *DestTy = I->getType();
1730 Value *Src = I->getOperand(0);
1731 Type *SrcTy = Src->getType();
1732
1733 bool IsZExt = isa<ZExtInst>(I);
1734 unsigned SrcReg = getRegForValue(Src);
1735 if (!SrcReg) return false;
1736
1737 EVT SrcEVT, DestEVT;
1738 SrcEVT = TLI.getValueType(SrcTy, true);
1739 DestEVT = TLI.getValueType(DestTy, true);
1740 if (!SrcEVT.isSimple())
1741 return false;
1742 if (!DestEVT.isSimple())
1743 return false;
1744
1745 MVT SrcVT = SrcEVT.getSimpleVT();
1746 MVT DestVT = DestEVT.getSimpleVT();
1747
1748 // If we know the register class needed for the result of this
1749 // instruction, use it. Otherwise pick the register class of the
1750 // correct size that does not contain X0/R0, since we don't know
1751 // whether downstream uses permit that assignment.
1752 unsigned AssignedReg = FuncInfo.ValueMap[I];
1753 const TargetRegisterClass *RC =
1754 (AssignedReg ? MRI.getRegClass(AssignedReg) :
1755 (DestVT == MVT::i64 ? &PPC::G8RC_and_G8RC_NOX0RegClass :
1756 &PPC::GPRC_and_GPRC_NOR0RegClass));
1757 unsigned ResultReg = createResultReg(RC);
1758
1759 if (!PPCEmitIntExt(SrcVT, SrcReg, DestVT, ResultReg, IsZExt))
1760 return false;
1761
1762 UpdateValueMap(I, ResultReg);
1763 return true;
1764}
1765
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001766// Attempt to fast-select an instruction that wasn't handled by
Bill Schmidt03008132013-08-25 22:33:42 +00001767// the table-generated machinery.
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001768bool PPCFastISel::TargetSelectInstruction(const Instruction *I) {
Bill Schmidt03008132013-08-25 22:33:42 +00001769
1770 switch (I->getOpcode()) {
Bill Schmidtccecf262013-08-30 02:29:45 +00001771 case Instruction::Load:
1772 return SelectLoad(I);
1773 case Instruction::Store:
1774 return SelectStore(I);
Bill Schmidt03008132013-08-25 22:33:42 +00001775 case Instruction::Br:
1776 return SelectBranch(I);
1777 case Instruction::IndirectBr:
1778 return SelectIndirectBr(I);
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001779 case Instruction::FPExt:
1780 return SelectFPExt(I);
1781 case Instruction::FPTrunc:
1782 return SelectFPTrunc(I);
1783 case Instruction::SIToFP:
1784 return SelectIToFP(I, /*IsSigned*/ true);
1785 case Instruction::UIToFP:
1786 return SelectIToFP(I, /*IsSigned*/ false);
1787 case Instruction::FPToSI:
1788 return SelectFPToI(I, /*IsSigned*/ true);
1789 case Instruction::FPToUI:
1790 return SelectFPToI(I, /*IsSigned*/ false);
Bill Schmidtccecf262013-08-30 02:29:45 +00001791 case Instruction::Add:
1792 return SelectBinaryIntOp(I, ISD::ADD);
1793 case Instruction::Or:
1794 return SelectBinaryIntOp(I, ISD::OR);
1795 case Instruction::Sub:
1796 return SelectBinaryIntOp(I, ISD::SUB);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001797 case Instruction::Call:
1798 if (dyn_cast<IntrinsicInst>(I))
1799 return false;
1800 return SelectCall(I);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001801 case Instruction::Ret:
1802 return SelectRet(I);
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001803 case Instruction::Trunc:
1804 return SelectTrunc(I);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001805 case Instruction::ZExt:
1806 case Instruction::SExt:
1807 return SelectIntExt(I);
Bill Schmidt03008132013-08-25 22:33:42 +00001808 // Here add other flavors of Instruction::XXX that automated
1809 // cases don't catch. For example, switches are terminators
1810 // that aren't yet handled.
1811 default:
1812 break;
1813 }
1814 return false;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001815}
1816
1817// Materialize a floating-point constant into a register, and return
1818// the register number (or zero if we failed to handle it).
1819unsigned PPCFastISel::PPCMaterializeFP(const ConstantFP *CFP, MVT VT) {
1820 // No plans to handle long double here.
1821 if (VT != MVT::f32 && VT != MVT::f64)
1822 return 0;
1823
1824 // All FP constants are loaded from the constant pool.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001825 unsigned Align = DL.getPrefTypeAlignment(CFP->getType());
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001826 assert(Align > 0 && "Unexpectedly missing alignment information!");
1827 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
1828 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
1829 CodeModel::Model CModel = TM.getCodeModel();
1830
1831 MachineMemOperand *MMO =
1832 FuncInfo.MF->getMachineMemOperand(
1833 MachinePointerInfo::getConstantPool(), MachineMemOperand::MOLoad,
1834 (VT == MVT::f32) ? 4 : 8, Align);
1835
Bill Schmidt03008132013-08-25 22:33:42 +00001836 unsigned Opc = (VT == MVT::f32) ? PPC::LFS : PPC::LFD;
1837 unsigned TmpReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
1838
1839 // For small code model, generate a LF[SD](0, LDtocCPT(Idx, X2)).
1840 if (CModel == CodeModel::Small || CModel == CodeModel::JITDefault) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001841 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocCPT),
Bill Schmidt03008132013-08-25 22:33:42 +00001842 TmpReg)
1843 .addConstantPoolIndex(Idx).addReg(PPC::X2);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001844 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidt03008132013-08-25 22:33:42 +00001845 .addImm(0).addReg(TmpReg).addMemOperand(MMO);
1846 } else {
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001847 // Otherwise we generate LF[SD](Idx[lo], ADDIStocHA(X2, Idx)).
Rafael Espindolaea09c592014-02-18 22:05:46 +00001848 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDIStocHA),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001849 TmpReg).addReg(PPC::X2).addConstantPoolIndex(Idx);
Bill Schmidtbb381d72013-09-17 20:03:25 +00001850 // But for large code model, we must generate a LDtocL followed
1851 // by the LF[SD].
1852 if (CModel == CodeModel::Large) {
1853 unsigned TmpReg2 = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001854 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocL),
Bill Schmidtbb381d72013-09-17 20:03:25 +00001855 TmpReg2).addConstantPoolIndex(Idx).addReg(TmpReg);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001856 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidtbb381d72013-09-17 20:03:25 +00001857 .addImm(0).addReg(TmpReg2);
1858 } else
Rafael Espindolaea09c592014-02-18 22:05:46 +00001859 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidtbb381d72013-09-17 20:03:25 +00001860 .addConstantPoolIndex(Idx, 0, PPCII::MO_TOC_LO)
1861 .addReg(TmpReg)
1862 .addMemOperand(MMO);
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001863 }
1864
1865 return DestReg;
1866}
1867
Bill Schmidtccecf262013-08-30 02:29:45 +00001868// Materialize the address of a global value into a register, and return
1869// the register number (or zero if we failed to handle it).
1870unsigned PPCFastISel::PPCMaterializeGV(const GlobalValue *GV, MVT VT) {
1871 assert(VT == MVT::i64 && "Non-address!");
1872 const TargetRegisterClass *RC = &PPC::G8RC_and_G8RC_NOX0RegClass;
1873 unsigned DestReg = createResultReg(RC);
1874
1875 // Global values may be plain old object addresses, TLS object
1876 // addresses, constant pool entries, or jump tables. How we generate
1877 // code for these may depend on small, medium, or large code model.
1878 CodeModel::Model CModel = TM.getCodeModel();
1879
1880 // FIXME: Jump tables are not yet required because fast-isel doesn't
1881 // handle switches; if that changes, we need them as well. For now,
1882 // what follows assumes everything's a generic (or TLS) global address.
Bill Schmidtccecf262013-08-30 02:29:45 +00001883
1884 // FIXME: We don't yet handle the complexity of TLS.
Rafael Espindola59f7eba2014-05-28 18:15:43 +00001885 if (GV->isThreadLocal())
Bill Schmidtccecf262013-08-30 02:29:45 +00001886 return 0;
1887
1888 // For small code model, generate a simple TOC load.
1889 if (CModel == CodeModel::Small || CModel == CodeModel::JITDefault)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001890 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtoc),
1891 DestReg)
1892 .addGlobalAddress(GV)
1893 .addReg(PPC::X2);
Bill Schmidtccecf262013-08-30 02:29:45 +00001894 else {
Bill Schmidt5d82f092014-06-16 21:36:02 +00001895 // If the address is an externally defined symbol, a symbol with common
1896 // or externally available linkage, a non-local function address, or a
Bill Schmidtccecf262013-08-30 02:29:45 +00001897 // jump table address (not yet needed), or if we are generating code
1898 // for large code model, we generate:
1899 // LDtocL(GV, ADDIStocHA(%X2, GV))
1900 // Otherwise we generate:
1901 // ADDItocL(ADDIStocHA(%X2, GV), GV)
1902 // Either way, start with the ADDIStocHA:
1903 unsigned HighPartReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001904 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDIStocHA),
Bill Schmidtccecf262013-08-30 02:29:45 +00001905 HighPartReg).addReg(PPC::X2).addGlobalAddress(GV);
1906
Bill Schmidtccecf262013-08-30 02:29:45 +00001907 // If/when switches are implemented, jump tables should be handled
1908 // on the "if" path here.
Bill Schmidt5d82f092014-06-16 21:36:02 +00001909 if (CModel == CodeModel::Large ||
1910 (GV->getType()->getElementType()->isFunctionTy() &&
1911 (GV->isDeclaration() || GV->isWeakForLinker())) ||
1912 GV->isDeclaration() || GV->hasCommonLinkage() ||
1913 GV->hasAvailableExternallyLinkage())
Rafael Espindolaea09c592014-02-18 22:05:46 +00001914 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocL),
Bill Schmidtccecf262013-08-30 02:29:45 +00001915 DestReg).addGlobalAddress(GV).addReg(HighPartReg);
1916 else
1917 // Otherwise generate the ADDItocL.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001918 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDItocL),
Bill Schmidtccecf262013-08-30 02:29:45 +00001919 DestReg).addReg(HighPartReg).addGlobalAddress(GV);
1920 }
1921
1922 return DestReg;
1923}
1924
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001925// Materialize a 32-bit integer constant into a register, and return
1926// the register number (or zero if we failed to handle it).
1927unsigned PPCFastISel::PPCMaterialize32BitInt(int64_t Imm,
1928 const TargetRegisterClass *RC) {
1929 unsigned Lo = Imm & 0xFFFF;
1930 unsigned Hi = (Imm >> 16) & 0xFFFF;
1931
1932 unsigned ResultReg = createResultReg(RC);
1933 bool IsGPRC = RC->hasSuperClassEq(&PPC::GPRCRegClass);
1934
1935 if (isInt<16>(Imm))
Rafael Espindolaea09c592014-02-18 22:05:46 +00001936 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001937 TII.get(IsGPRC ? PPC::LI : PPC::LI8), ResultReg)
1938 .addImm(Imm);
1939 else if (Lo) {
1940 // Both Lo and Hi have nonzero bits.
1941 unsigned TmpReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001942 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001943 TII.get(IsGPRC ? PPC::LIS : PPC::LIS8), TmpReg)
1944 .addImm(Hi);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001945 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001946 TII.get(IsGPRC ? PPC::ORI : PPC::ORI8), ResultReg)
1947 .addReg(TmpReg).addImm(Lo);
1948 } else
1949 // Just Hi bits.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001950 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001951 TII.get(IsGPRC ? PPC::LIS : PPC::LIS8), ResultReg)
1952 .addImm(Hi);
1953
1954 return ResultReg;
1955}
1956
1957// Materialize a 64-bit integer constant into a register, and return
1958// the register number (or zero if we failed to handle it).
1959unsigned PPCFastISel::PPCMaterialize64BitInt(int64_t Imm,
1960 const TargetRegisterClass *RC) {
1961 unsigned Remainder = 0;
1962 unsigned Shift = 0;
1963
1964 // If the value doesn't fit in 32 bits, see if we can shift it
1965 // so that it fits in 32 bits.
1966 if (!isInt<32>(Imm)) {
1967 Shift = countTrailingZeros<uint64_t>(Imm);
1968 int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
1969
1970 if (isInt<32>(ImmSh))
1971 Imm = ImmSh;
1972 else {
1973 Remainder = Imm;
1974 Shift = 32;
1975 Imm >>= 32;
1976 }
1977 }
1978
1979 // Handle the high-order 32 bits (if shifted) or the whole 32 bits
1980 // (if not shifted).
1981 unsigned TmpReg1 = PPCMaterialize32BitInt(Imm, RC);
1982 if (!Shift)
1983 return TmpReg1;
1984
1985 // If upper 32 bits were not zero, we've built them and need to shift
1986 // them into place.
1987 unsigned TmpReg2;
1988 if (Imm) {
1989 TmpReg2 = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001990 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::RLDICR),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001991 TmpReg2).addReg(TmpReg1).addImm(Shift).addImm(63 - Shift);
1992 } else
1993 TmpReg2 = TmpReg1;
1994
1995 unsigned TmpReg3, Hi, Lo;
1996 if ((Hi = (Remainder >> 16) & 0xFFFF)) {
1997 TmpReg3 = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001998 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ORIS8),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001999 TmpReg3).addReg(TmpReg2).addImm(Hi);
2000 } else
2001 TmpReg3 = TmpReg2;
2002
2003 if ((Lo = Remainder & 0xFFFF)) {
2004 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002005 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ORI8),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002006 ResultReg).addReg(TmpReg3).addImm(Lo);
2007 return ResultReg;
2008 }
2009
2010 return TmpReg3;
2011}
2012
2013
2014// Materialize an integer constant into a register, and return
2015// the register number (or zero if we failed to handle it).
2016unsigned PPCFastISel::PPCMaterializeInt(const Constant *C, MVT VT) {
Hal Finkel940ab932014-02-28 00:27:01 +00002017 // If we're using CR bit registers for i1 values, handle that as a special
2018 // case first.
Eric Christopher1b8e7632014-05-22 01:07:24 +00002019 if (VT == MVT::i1 && PPCSubTarget->useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +00002020 const ConstantInt *CI = cast<ConstantInt>(C);
2021 unsigned ImmReg = createResultReg(&PPC::CRBITRCRegClass);
2022 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2023 TII.get(CI->isZero() ? PPC::CRUNSET : PPC::CRSET), ImmReg);
2024 return ImmReg;
2025 }
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002026
2027 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16 &&
2028 VT != MVT::i8 && VT != MVT::i1)
2029 return 0;
2030
2031 const TargetRegisterClass *RC = ((VT == MVT::i64) ? &PPC::G8RCRegClass :
2032 &PPC::GPRCRegClass);
2033
2034 // If the constant is in range, use a load-immediate.
2035 const ConstantInt *CI = cast<ConstantInt>(C);
2036 if (isInt<16>(CI->getSExtValue())) {
2037 unsigned Opc = (VT == MVT::i64) ? PPC::LI8 : PPC::LI;
2038 unsigned ImmReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002039 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ImmReg)
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002040 .addImm(CI->getSExtValue());
2041 return ImmReg;
2042 }
2043
2044 // Construct the constant piecewise.
2045 int64_t Imm = CI->getZExtValue();
2046
2047 if (VT == MVT::i64)
2048 return PPCMaterialize64BitInt(Imm, RC);
2049 else if (VT == MVT::i32)
2050 return PPCMaterialize32BitInt(Imm, RC);
2051
2052 return 0;
2053}
2054
2055// Materialize a constant into a register, and return the register
2056// number (or zero if we failed to handle it).
2057unsigned PPCFastISel::TargetMaterializeConstant(const Constant *C) {
2058 EVT CEVT = TLI.getValueType(C->getType(), true);
2059
2060 // Only handle simple types.
2061 if (!CEVT.isSimple()) return 0;
2062 MVT VT = CEVT.getSimpleVT();
2063
2064 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
2065 return PPCMaterializeFP(CFP, VT);
Bill Schmidtccecf262013-08-30 02:29:45 +00002066 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
2067 return PPCMaterializeGV(GV, VT);
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002068 else if (isa<ConstantInt>(C))
2069 return PPCMaterializeInt(C, VT);
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002070
2071 return 0;
2072}
2073
2074// Materialize the address created by an alloca into a register, and
Bill Schmidteb8d6f72013-08-31 02:33:40 +00002075// return the register number (or zero if we failed to handle it).
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002076unsigned PPCFastISel::TargetMaterializeAlloca(const AllocaInst *AI) {
Bill Schmidteb8d6f72013-08-31 02:33:40 +00002077 // Don't handle dynamic allocas.
2078 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
2079
2080 MVT VT;
2081 if (!isLoadTypeLegal(AI->getType(), VT)) return 0;
2082
2083 DenseMap<const AllocaInst*, int>::iterator SI =
2084 FuncInfo.StaticAllocaMap.find(AI);
2085
2086 if (SI != FuncInfo.StaticAllocaMap.end()) {
2087 unsigned ResultReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002088 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDI8),
Bill Schmidteb8d6f72013-08-31 02:33:40 +00002089 ResultReg).addFrameIndex(SI->second).addImm(0);
2090 return ResultReg;
2091 }
2092
2093 return 0;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002094}
2095
Bill Schmidtccecf262013-08-30 02:29:45 +00002096// Fold loads into extends when possible.
2097// FIXME: We can have multiple redundant extend/trunc instructions
2098// following a load. The folding only picks up one. Extend this
2099// to check subsequent instructions for the same pattern and remove
2100// them. Thus ResultReg should be the def reg for the last redundant
2101// instruction in a chain, and all intervening instructions can be
2102// removed from parent. Change test/CodeGen/PowerPC/fast-isel-fold.ll
2103// to add ELF64-NOT: rldicl to the appropriate tests when this works.
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002104bool PPCFastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
2105 const LoadInst *LI) {
Bill Schmidtccecf262013-08-30 02:29:45 +00002106 // Verify we have a legal type before going any further.
2107 MVT VT;
2108 if (!isLoadTypeLegal(LI->getType(), VT))
2109 return false;
2110
2111 // Combine load followed by zero- or sign-extend.
2112 bool IsZExt = false;
2113 switch(MI->getOpcode()) {
2114 default:
2115 return false;
2116
2117 case PPC::RLDICL:
2118 case PPC::RLDICL_32_64: {
2119 IsZExt = true;
2120 unsigned MB = MI->getOperand(3).getImm();
2121 if ((VT == MVT::i8 && MB <= 56) ||
2122 (VT == MVT::i16 && MB <= 48) ||
2123 (VT == MVT::i32 && MB <= 32))
2124 break;
2125 return false;
2126 }
2127
2128 case PPC::RLWINM:
2129 case PPC::RLWINM8: {
2130 IsZExt = true;
2131 unsigned MB = MI->getOperand(3).getImm();
2132 if ((VT == MVT::i8 && MB <= 24) ||
2133 (VT == MVT::i16 && MB <= 16))
2134 break;
2135 return false;
2136 }
2137
2138 case PPC::EXTSB:
2139 case PPC::EXTSB8:
2140 case PPC::EXTSB8_32_64:
2141 /* There is no sign-extending load-byte instruction. */
2142 return false;
2143
2144 case PPC::EXTSH:
2145 case PPC::EXTSH8:
2146 case PPC::EXTSH8_32_64: {
2147 if (VT != MVT::i16 && VT != MVT::i8)
2148 return false;
2149 break;
2150 }
2151
2152 case PPC::EXTSW:
2153 case PPC::EXTSW_32_64: {
2154 if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8)
2155 return false;
2156 break;
2157 }
2158 }
2159
2160 // See if we can handle this address.
2161 Address Addr;
2162 if (!PPCComputeAddress(LI->getOperand(0), Addr))
2163 return false;
2164
2165 unsigned ResultReg = MI->getOperand(0).getReg();
2166
Craig Topper062a2ba2014-04-25 05:30:21 +00002167 if (!PPCEmitLoad(VT, ResultReg, Addr, nullptr, IsZExt))
Bill Schmidtccecf262013-08-30 02:29:45 +00002168 return false;
2169
2170 MI->eraseFromParent();
2171 return true;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002172}
2173
2174// Attempt to lower call arguments in a faster way than done by
2175// the selection DAG code.
2176bool PPCFastISel::FastLowerArguments() {
2177 // Defer to normal argument lowering for now. It's reasonably
2178 // efficient. Consider doing something like ARM to handle the
2179 // case where all args fit in registers, no varargs, no float
2180 // or vector args.
2181 return false;
2182}
2183
Bill Schmidt03008132013-08-25 22:33:42 +00002184// Handle materializing integer constants into a register. This is not
2185// automatically generated for PowerPC, so must be explicitly created here.
2186unsigned PPCFastISel::FastEmit_i(MVT Ty, MVT VT, unsigned Opc, uint64_t Imm) {
2187
2188 if (Opc != ISD::Constant)
2189 return 0;
2190
Hal Finkel940ab932014-02-28 00:27:01 +00002191 // If we're using CR bit registers for i1 values, handle that as a special
2192 // case first.
Eric Christopher1b8e7632014-05-22 01:07:24 +00002193 if (VT == MVT::i1 && PPCSubTarget->useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +00002194 unsigned ImmReg = createResultReg(&PPC::CRBITRCRegClass);
2195 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2196 TII.get(Imm == 0 ? PPC::CRUNSET : PPC::CRSET), ImmReg);
2197 return ImmReg;
2198 }
2199
Bill Schmidt03008132013-08-25 22:33:42 +00002200 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16 &&
2201 VT != MVT::i8 && VT != MVT::i1)
2202 return 0;
2203
2204 const TargetRegisterClass *RC = ((VT == MVT::i64) ? &PPC::G8RCRegClass :
2205 &PPC::GPRCRegClass);
2206 if (VT == MVT::i64)
2207 return PPCMaterialize64BitInt(Imm, RC);
2208 else
2209 return PPCMaterialize32BitInt(Imm, RC);
2210}
2211
Bill Schmidtccecf262013-08-30 02:29:45 +00002212// Override for ADDI and ADDI8 to set the correct register class
2213// on RHS operand 0. The automatic infrastructure naively assumes
2214// GPRC for i32 and G8RC for i64; the concept of "no R0" is lost
2215// for these cases. At the moment, none of the other automatically
2216// generated RI instructions require special treatment. However, once
2217// SelectSelect is implemented, "isel" requires similar handling.
2218//
2219// Also be conservative about the output register class. Avoid
2220// assigning R0 or X0 to the output register for GPRC and G8RC
2221// register classes, as any such result could be used in ADDI, etc.,
2222// where those regs have another meaning.
2223unsigned PPCFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
2224 const TargetRegisterClass *RC,
2225 unsigned Op0, bool Op0IsKill,
2226 uint64_t Imm) {
2227 if (MachineInstOpcode == PPC::ADDI)
2228 MRI.setRegClass(Op0, &PPC::GPRC_and_GPRC_NOR0RegClass);
2229 else if (MachineInstOpcode == PPC::ADDI8)
2230 MRI.setRegClass(Op0, &PPC::G8RC_and_G8RC_NOX0RegClass);
2231
2232 const TargetRegisterClass *UseRC =
2233 (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
2234 (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
2235
2236 return FastISel::FastEmitInst_ri(MachineInstOpcode, UseRC,
2237 Op0, Op0IsKill, Imm);
2238}
2239
2240// Override for instructions with one register operand to avoid use of
2241// R0/X0. The automatic infrastructure isn't aware of the context so
2242// we must be conservative.
2243unsigned PPCFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
2244 const TargetRegisterClass* RC,
2245 unsigned Op0, bool Op0IsKill) {
2246 const TargetRegisterClass *UseRC =
2247 (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
2248 (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
2249
2250 return FastISel::FastEmitInst_r(MachineInstOpcode, UseRC, Op0, Op0IsKill);
2251}
2252
2253// Override for instructions with two register operands to avoid use
2254// of R0/X0. The automatic infrastructure isn't aware of the context
2255// so we must be conservative.
2256unsigned PPCFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
2257 const TargetRegisterClass* RC,
2258 unsigned Op0, bool Op0IsKill,
2259 unsigned Op1, bool Op1IsKill) {
2260 const TargetRegisterClass *UseRC =
2261 (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
2262 (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
2263
2264 return FastISel::FastEmitInst_rr(MachineInstOpcode, UseRC, Op0, Op0IsKill,
2265 Op1, Op1IsKill);
2266}
2267
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002268namespace llvm {
2269 // Create the fast instruction selector for PowerPC64 ELF.
2270 FastISel *PPC::createFastISel(FunctionLoweringInfo &FuncInfo,
2271 const TargetLibraryInfo *LibInfo) {
2272 const TargetMachine &TM = FuncInfo.MF->getTarget();
2273
2274 // Only available on 64-bit ELF for now.
2275 const PPCSubtarget *Subtarget = &TM.getSubtarget<PPCSubtarget>();
2276 if (Subtarget->isPPC64() && Subtarget->isSVR4ABI())
2277 return new PPCFastISel(FuncInfo, LibInfo);
2278
Craig Topper062a2ba2014-04-25 05:30:21 +00002279 return nullptr;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002280 }
2281}