blob: 2e524d604789853449a1d1816d31632d156157e6 [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)
95 : FastISel(FuncInfo, LibInfo),
96 TM(FuncInfo.MF->getTarget()),
97 TII(*TM.getInstrInfo()),
98 TLI(*TM.getTargetLowering()),
Eric Christopher1b8e7632014-05-22 01:07:24 +000099 PPCSubTarget(&TM.getSubtarget<PPCSubtarget>()),
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000100 Context(&FuncInfo.Fn->getContext()) { }
101
102 // Backend specific FastISel code.
103 private:
Craig Topper0d3fa922014-04-29 07:57:37 +0000104 bool TargetSelectInstruction(const Instruction *I) override;
105 unsigned TargetMaterializeConstant(const Constant *C) override;
106 unsigned TargetMaterializeAlloca(const AllocaInst *AI) override;
107 bool tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
108 const LoadInst *LI) override;
109 bool FastLowerArguments() override;
110 unsigned FastEmit_i(MVT Ty, MVT RetTy, unsigned Opc, uint64_t Imm) override;
111 unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
112 const TargetRegisterClass *RC,
113 unsigned Op0, bool Op0IsKill,
114 uint64_t Imm);
115 unsigned FastEmitInst_r(unsigned MachineInstOpcode,
116 const TargetRegisterClass *RC,
117 unsigned Op0, bool Op0IsKill);
118 unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
119 const TargetRegisterClass *RC,
120 unsigned Op0, bool Op0IsKill,
121 unsigned Op1, bool Op1IsKill);
Bill Schmidt03008132013-08-25 22:33:42 +0000122
123 // Instruction selection routines.
124 private:
Bill Schmidtccecf262013-08-30 02:29:45 +0000125 bool SelectLoad(const Instruction *I);
126 bool SelectStore(const Instruction *I);
Bill Schmidt03008132013-08-25 22:33:42 +0000127 bool SelectBranch(const Instruction *I);
128 bool SelectIndirectBr(const Instruction *I);
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000129 bool SelectFPExt(const Instruction *I);
130 bool SelectFPTrunc(const Instruction *I);
131 bool SelectIToFP(const Instruction *I, bool IsSigned);
132 bool SelectFPToI(const Instruction *I, bool IsSigned);
Bill Schmidtccecf262013-08-30 02:29:45 +0000133 bool SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode);
Bill Schmidt8470b0f2013-08-30 22:18:55 +0000134 bool SelectCall(const Instruction *I);
Bill Schmidtd89f6782013-08-26 19:42:51 +0000135 bool SelectRet(const Instruction *I);
Bill Schmidt9d9510d2013-08-30 23:31:33 +0000136 bool SelectTrunc(const Instruction *I);
Bill Schmidtd89f6782013-08-26 19:42:51 +0000137 bool SelectIntExt(const Instruction *I);
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000138
139 // Utility routines.
140 private:
Bill Schmidtccecf262013-08-30 02:29:45 +0000141 bool isTypeLegal(Type *Ty, MVT &VT);
142 bool isLoadTypeLegal(Type *Ty, MVT &VT);
Bill Schmidt03008132013-08-25 22:33:42 +0000143 bool PPCEmitCmp(const Value *Src1Value, const Value *Src2Value,
144 bool isZExt, unsigned DestReg);
Bill Schmidtccecf262013-08-30 02:29:45 +0000145 bool PPCEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
146 const TargetRegisterClass *RC, bool IsZExt = true,
147 unsigned FP64LoadOpc = PPC::LFD);
148 bool PPCEmitStore(MVT VT, unsigned SrcReg, Address &Addr);
149 bool PPCComputeAddress(const Value *Obj, Address &Addr);
150 void PPCSimplifyAddress(Address &Addr, MVT VT, bool &UseOffset,
151 unsigned &IndexReg);
Bill Schmidt03008132013-08-25 22:33:42 +0000152 bool PPCEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
153 unsigned DestReg, bool IsZExt);
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000154 unsigned PPCMaterializeFP(const ConstantFP *CFP, MVT VT);
Bill Schmidtccecf262013-08-30 02:29:45 +0000155 unsigned PPCMaterializeGV(const GlobalValue *GV, MVT VT);
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000156 unsigned PPCMaterializeInt(const Constant *C, MVT VT);
157 unsigned PPCMaterialize32BitInt(int64_t Imm,
158 const TargetRegisterClass *RC);
159 unsigned PPCMaterialize64BitInt(int64_t Imm,
160 const TargetRegisterClass *RC);
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000161 unsigned PPCMoveToIntReg(const Instruction *I, MVT VT,
162 unsigned SrcReg, bool IsSigned);
163 unsigned PPCMoveToFPReg(MVT VT, unsigned SrcReg, bool IsSigned);
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000164
Bill Schmidtd89f6782013-08-26 19:42:51 +0000165 // Call handling routines.
166 private:
Bill Schmidt8470b0f2013-08-30 22:18:55 +0000167 bool processCallArgs(SmallVectorImpl<Value*> &Args,
168 SmallVectorImpl<unsigned> &ArgRegs,
169 SmallVectorImpl<MVT> &ArgVTs,
170 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
171 SmallVectorImpl<unsigned> &RegArgs,
172 CallingConv::ID CC,
173 unsigned &NumBytes,
174 bool IsVarArg);
175 void finishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
176 const Instruction *I, CallingConv::ID CC,
177 unsigned &NumBytes, bool IsVarArg);
Bill Schmidtd89f6782013-08-26 19:42:51 +0000178 CCAssignFn *usePPC32CCs(unsigned Flag);
179
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000180 private:
181 #include "PPCGenFastISel.inc"
182
183};
184
185} // end anonymous namespace
186
Bill Schmidtd89f6782013-08-26 19:42:51 +0000187#include "PPCGenCallingConv.inc"
188
189// Function whose sole purpose is to kill compiler warnings
190// stemming from unused functions included from PPCGenCallingConv.inc.
191CCAssignFn *PPCFastISel::usePPC32CCs(unsigned Flag) {
192 if (Flag == 1)
193 return CC_PPC32_SVR4;
194 else if (Flag == 2)
195 return CC_PPC32_SVR4_ByVal;
196 else if (Flag == 3)
197 return CC_PPC32_SVR4_VarArg;
198 else
199 return RetCC_PPC;
200}
201
Bill Schmidt03008132013-08-25 22:33:42 +0000202static Optional<PPC::Predicate> getComparePred(CmpInst::Predicate Pred) {
203 switch (Pred) {
204 // These are not representable with any single compare.
205 case CmpInst::FCMP_FALSE:
206 case CmpInst::FCMP_UEQ:
207 case CmpInst::FCMP_UGT:
208 case CmpInst::FCMP_UGE:
209 case CmpInst::FCMP_ULT:
210 case CmpInst::FCMP_ULE:
211 case CmpInst::FCMP_UNE:
212 case CmpInst::FCMP_TRUE:
213 default:
214 return Optional<PPC::Predicate>();
215
216 case CmpInst::FCMP_OEQ:
217 case CmpInst::ICMP_EQ:
218 return PPC::PRED_EQ;
219
220 case CmpInst::FCMP_OGT:
221 case CmpInst::ICMP_UGT:
222 case CmpInst::ICMP_SGT:
223 return PPC::PRED_GT;
224
225 case CmpInst::FCMP_OGE:
226 case CmpInst::ICMP_UGE:
227 case CmpInst::ICMP_SGE:
228 return PPC::PRED_GE;
229
230 case CmpInst::FCMP_OLT:
231 case CmpInst::ICMP_ULT:
232 case CmpInst::ICMP_SLT:
233 return PPC::PRED_LT;
234
235 case CmpInst::FCMP_OLE:
236 case CmpInst::ICMP_ULE:
237 case CmpInst::ICMP_SLE:
238 return PPC::PRED_LE;
239
240 case CmpInst::FCMP_ONE:
241 case CmpInst::ICMP_NE:
242 return PPC::PRED_NE;
243
244 case CmpInst::FCMP_ORD:
245 return PPC::PRED_NU;
246
247 case CmpInst::FCMP_UNO:
248 return PPC::PRED_UN;
249 }
250}
251
Bill Schmidtccecf262013-08-30 02:29:45 +0000252// Determine whether the type Ty is simple enough to be handled by
253// fast-isel, and return its equivalent machine type in VT.
254// FIXME: Copied directly from ARM -- factor into base class?
255bool PPCFastISel::isTypeLegal(Type *Ty, MVT &VT) {
256 EVT Evt = TLI.getValueType(Ty, true);
257
258 // Only handle simple types.
259 if (Evt == MVT::Other || !Evt.isSimple()) return false;
260 VT = Evt.getSimpleVT();
261
262 // Handle all legal types, i.e. a register that will directly hold this
263 // value.
264 return TLI.isTypeLegal(VT);
265}
266
267// Determine whether the type Ty is simple enough to be handled by
268// fast-isel as a load target, and return its equivalent machine type in VT.
269bool PPCFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
270 if (isTypeLegal(Ty, VT)) return true;
271
272 // If this is a type than can be sign or zero-extended to a basic operation
273 // go ahead and accept it now.
274 if (VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32) {
275 return true;
276 }
277
278 return false;
279}
280
281// Given a value Obj, create an Address object Addr that represents its
282// address. Return false if we can't handle it.
283bool PPCFastISel::PPCComputeAddress(const Value *Obj, Address &Addr) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000284 const User *U = nullptr;
Bill Schmidtccecf262013-08-30 02:29:45 +0000285 unsigned Opcode = Instruction::UserOp1;
286 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
287 // Don't walk into other basic blocks unless the object is an alloca from
288 // another block, otherwise it may not have a virtual register assigned.
289 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
290 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
291 Opcode = I->getOpcode();
292 U = I;
293 }
294 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
295 Opcode = C->getOpcode();
296 U = C;
297 }
298
299 switch (Opcode) {
300 default:
301 break;
302 case Instruction::BitCast:
303 // Look through bitcasts.
304 return PPCComputeAddress(U->getOperand(0), Addr);
305 case Instruction::IntToPtr:
306 // Look past no-op inttoptrs.
307 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
308 return PPCComputeAddress(U->getOperand(0), Addr);
309 break;
310 case Instruction::PtrToInt:
311 // Look past no-op ptrtoints.
312 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
313 return PPCComputeAddress(U->getOperand(0), Addr);
314 break;
315 case Instruction::GetElementPtr: {
316 Address SavedAddr = Addr;
317 long TmpOffset = Addr.Offset;
318
319 // Iterate through the GEP folding the constants into offsets where
320 // we can.
321 gep_type_iterator GTI = gep_type_begin(U);
322 for (User::const_op_iterator II = U->op_begin() + 1, IE = U->op_end();
323 II != IE; ++II, ++GTI) {
324 const Value *Op = *II;
325 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000326 const StructLayout *SL = DL.getStructLayout(STy);
Bill Schmidtccecf262013-08-30 02:29:45 +0000327 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
328 TmpOffset += SL->getElementOffset(Idx);
329 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000330 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
Bill Schmidtccecf262013-08-30 02:29:45 +0000331 for (;;) {
332 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
333 // Constant-offset addressing.
334 TmpOffset += CI->getSExtValue() * S;
335 break;
336 }
Bob Wilson9f3e6b22013-11-15 19:09:27 +0000337 if (canFoldAddIntoGEP(U, Op)) {
338 // A compatible add with a constant operand. Fold the constant.
Bill Schmidtccecf262013-08-30 02:29:45 +0000339 ConstantInt *CI =
340 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
341 TmpOffset += CI->getSExtValue() * S;
342 // Iterate on the other operand.
343 Op = cast<AddOperator>(Op)->getOperand(0);
344 continue;
345 }
346 // Unsupported
347 goto unsupported_gep;
348 }
349 }
350 }
351
352 // Try to grab the base operand now.
353 Addr.Offset = TmpOffset;
354 if (PPCComputeAddress(U->getOperand(0), Addr)) return true;
355
356 // We failed, restore everything and try the other options.
357 Addr = SavedAddr;
358
359 unsupported_gep:
360 break;
361 }
362 case Instruction::Alloca: {
363 const AllocaInst *AI = cast<AllocaInst>(Obj);
364 DenseMap<const AllocaInst*, int>::iterator SI =
365 FuncInfo.StaticAllocaMap.find(AI);
366 if (SI != FuncInfo.StaticAllocaMap.end()) {
367 Addr.BaseType = Address::FrameIndexBase;
368 Addr.Base.FI = SI->second;
369 return true;
370 }
371 break;
372 }
373 }
374
375 // FIXME: References to parameters fall through to the behavior
376 // below. They should be able to reference a frame index since
377 // they are stored to the stack, so we can get "ld rx, offset(r1)"
378 // instead of "addi ry, r1, offset / ld rx, 0(ry)". Obj will
379 // just contain the parameter. Try to handle this with a FI.
380
381 // Try to get this in a register if nothing else has worked.
382 if (Addr.Base.Reg == 0)
383 Addr.Base.Reg = getRegForValue(Obj);
384
385 // Prevent assignment of base register to X0, which is inappropriate
386 // for loads and stores alike.
387 if (Addr.Base.Reg != 0)
388 MRI.setRegClass(Addr.Base.Reg, &PPC::G8RC_and_G8RC_NOX0RegClass);
389
390 return Addr.Base.Reg != 0;
391}
392
393// Fix up some addresses that can't be used directly. For example, if
394// an offset won't fit in an instruction field, we may need to move it
395// into an index register.
396void PPCFastISel::PPCSimplifyAddress(Address &Addr, MVT VT, bool &UseOffset,
397 unsigned &IndexReg) {
398
399 // Check whether the offset fits in the instruction field.
400 if (!isInt<16>(Addr.Offset))
401 UseOffset = false;
402
403 // If this is a stack pointer and the offset needs to be simplified then
404 // put the alloca address into a register, set the base type back to
405 // register and continue. This should almost never happen.
406 if (!UseOffset && Addr.BaseType == Address::FrameIndexBase) {
407 unsigned ResultReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000408 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDI8),
Bill Schmidtccecf262013-08-30 02:29:45 +0000409 ResultReg).addFrameIndex(Addr.Base.FI).addImm(0);
410 Addr.Base.Reg = ResultReg;
411 Addr.BaseType = Address::RegBase;
412 }
413
414 if (!UseOffset) {
415 IntegerType *OffsetTy = ((VT == MVT::i32) ? Type::getInt32Ty(*Context)
416 : Type::getInt64Ty(*Context));
417 const ConstantInt *Offset =
418 ConstantInt::getSigned(OffsetTy, (int64_t)(Addr.Offset));
419 IndexReg = PPCMaterializeInt(Offset, MVT::i64);
420 assert(IndexReg && "Unexpected error in PPCMaterializeInt!");
421 }
422}
423
424// Emit a load instruction if possible, returning true if we succeeded,
425// otherwise false. See commentary below for how the register class of
426// the load is determined.
427bool PPCFastISel::PPCEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
428 const TargetRegisterClass *RC,
429 bool IsZExt, unsigned FP64LoadOpc) {
430 unsigned Opc;
431 bool UseOffset = true;
432
433 // If ResultReg is given, it determines the register class of the load.
434 // Otherwise, RC is the register class to use. If the result of the
435 // load isn't anticipated in this block, both may be zero, in which
436 // case we must make a conservative guess. In particular, don't assign
437 // R0 or X0 to the result register, as the result may be used in a load,
438 // store, add-immediate, or isel that won't permit this. (Though
439 // perhaps the spill and reload of live-exit values would handle this?)
440 const TargetRegisterClass *UseRC =
441 (ResultReg ? MRI.getRegClass(ResultReg) :
442 (RC ? RC :
443 (VT == MVT::f64 ? &PPC::F8RCRegClass :
444 (VT == MVT::f32 ? &PPC::F4RCRegClass :
445 (VT == MVT::i64 ? &PPC::G8RC_and_G8RC_NOX0RegClass :
446 &PPC::GPRC_and_GPRC_NOR0RegClass)))));
447
448 bool Is32BitInt = UseRC->hasSuperClassEq(&PPC::GPRCRegClass);
449
450 switch (VT.SimpleTy) {
451 default: // e.g., vector types not handled
452 return false;
453 case MVT::i8:
454 Opc = Is32BitInt ? PPC::LBZ : PPC::LBZ8;
455 break;
456 case MVT::i16:
457 Opc = (IsZExt ?
458 (Is32BitInt ? PPC::LHZ : PPC::LHZ8) :
459 (Is32BitInt ? PPC::LHA : PPC::LHA8));
460 break;
461 case MVT::i32:
462 Opc = (IsZExt ?
463 (Is32BitInt ? PPC::LWZ : PPC::LWZ8) :
464 (Is32BitInt ? PPC::LWA_32 : PPC::LWA));
465 if ((Opc == PPC::LWA || Opc == PPC::LWA_32) && ((Addr.Offset & 3) != 0))
466 UseOffset = false;
467 break;
468 case MVT::i64:
469 Opc = PPC::LD;
470 assert(UseRC->hasSuperClassEq(&PPC::G8RCRegClass) &&
471 "64-bit load with 32-bit target??");
472 UseOffset = ((Addr.Offset & 3) == 0);
473 break;
474 case MVT::f32:
475 Opc = PPC::LFS;
476 break;
477 case MVT::f64:
478 Opc = FP64LoadOpc;
479 break;
480 }
481
482 // If necessary, materialize the offset into a register and use
483 // the indexed form. Also handle stack pointers with special needs.
484 unsigned IndexReg = 0;
485 PPCSimplifyAddress(Addr, VT, UseOffset, IndexReg);
486 if (ResultReg == 0)
487 ResultReg = createResultReg(UseRC);
488
489 // Note: If we still have a frame index here, we know the offset is
490 // in range, as otherwise PPCSimplifyAddress would have converted it
491 // into a RegBase.
492 if (Addr.BaseType == Address::FrameIndexBase) {
493
494 MachineMemOperand *MMO =
495 FuncInfo.MF->getMachineMemOperand(
496 MachinePointerInfo::getFixedStack(Addr.Base.FI, Addr.Offset),
497 MachineMemOperand::MOLoad, MFI.getObjectSize(Addr.Base.FI),
498 MFI.getObjectAlignment(Addr.Base.FI));
499
Rafael Espindolaea09c592014-02-18 22:05:46 +0000500 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Bill Schmidtccecf262013-08-30 02:29:45 +0000501 .addImm(Addr.Offset).addFrameIndex(Addr.Base.FI).addMemOperand(MMO);
502
503 // Base reg with offset in range.
504 } else if (UseOffset) {
505
Rafael Espindolaea09c592014-02-18 22:05:46 +0000506 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Bill Schmidtccecf262013-08-30 02:29:45 +0000507 .addImm(Addr.Offset).addReg(Addr.Base.Reg);
508
509 // Indexed form.
510 } else {
511 // Get the RR opcode corresponding to the RI one. FIXME: It would be
512 // preferable to use the ImmToIdxMap from PPCRegisterInfo.cpp, but it
513 // is hard to get at.
514 switch (Opc) {
515 default: llvm_unreachable("Unexpected opcode!");
516 case PPC::LBZ: Opc = PPC::LBZX; break;
517 case PPC::LBZ8: Opc = PPC::LBZX8; break;
518 case PPC::LHZ: Opc = PPC::LHZX; break;
519 case PPC::LHZ8: Opc = PPC::LHZX8; break;
520 case PPC::LHA: Opc = PPC::LHAX; break;
521 case PPC::LHA8: Opc = PPC::LHAX8; break;
522 case PPC::LWZ: Opc = PPC::LWZX; break;
523 case PPC::LWZ8: Opc = PPC::LWZX8; break;
524 case PPC::LWA: Opc = PPC::LWAX; break;
525 case PPC::LWA_32: Opc = PPC::LWAX_32; break;
526 case PPC::LD: Opc = PPC::LDX; break;
527 case PPC::LFS: Opc = PPC::LFSX; break;
528 case PPC::LFD: Opc = PPC::LFDX; break;
529 }
Rafael Espindolaea09c592014-02-18 22:05:46 +0000530 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Bill Schmidtccecf262013-08-30 02:29:45 +0000531 .addReg(Addr.Base.Reg).addReg(IndexReg);
532 }
533
534 return true;
535}
536
537// Attempt to fast-select a load instruction.
538bool PPCFastISel::SelectLoad(const Instruction *I) {
539 // FIXME: No atomic loads are supported.
540 if (cast<LoadInst>(I)->isAtomic())
541 return false;
542
543 // Verify we have a legal type before going any further.
544 MVT VT;
545 if (!isLoadTypeLegal(I->getType(), VT))
546 return false;
547
548 // See if we can handle this address.
549 Address Addr;
550 if (!PPCComputeAddress(I->getOperand(0), Addr))
551 return false;
552
553 // Look at the currently assigned register for this instruction
554 // to determine the required register class. This is necessary
555 // to constrain RA from using R0/X0 when this is not legal.
556 unsigned AssignedReg = FuncInfo.ValueMap[I];
557 const TargetRegisterClass *RC =
Craig Topper062a2ba2014-04-25 05:30:21 +0000558 AssignedReg ? MRI.getRegClass(AssignedReg) : nullptr;
Bill Schmidtccecf262013-08-30 02:29:45 +0000559
560 unsigned ResultReg = 0;
561 if (!PPCEmitLoad(VT, ResultReg, Addr, RC))
562 return false;
563 UpdateValueMap(I, ResultReg);
564 return true;
565}
566
567// Emit a store instruction to store SrcReg at Addr.
568bool PPCFastISel::PPCEmitStore(MVT VT, unsigned SrcReg, Address &Addr) {
569 assert(SrcReg && "Nothing to store!");
570 unsigned Opc;
571 bool UseOffset = true;
572
573 const TargetRegisterClass *RC = MRI.getRegClass(SrcReg);
574 bool Is32BitInt = RC->hasSuperClassEq(&PPC::GPRCRegClass);
575
576 switch (VT.SimpleTy) {
577 default: // e.g., vector types not handled
578 return false;
579 case MVT::i8:
580 Opc = Is32BitInt ? PPC::STB : PPC::STB8;
581 break;
582 case MVT::i16:
583 Opc = Is32BitInt ? PPC::STH : PPC::STH8;
584 break;
585 case MVT::i32:
586 assert(Is32BitInt && "Not GPRC for i32??");
587 Opc = PPC::STW;
588 break;
589 case MVT::i64:
590 Opc = PPC::STD;
591 UseOffset = ((Addr.Offset & 3) == 0);
592 break;
593 case MVT::f32:
594 Opc = PPC::STFS;
595 break;
596 case MVT::f64:
597 Opc = PPC::STFD;
598 break;
599 }
600
601 // If necessary, materialize the offset into a register and use
602 // the indexed form. Also handle stack pointers with special needs.
603 unsigned IndexReg = 0;
604 PPCSimplifyAddress(Addr, VT, UseOffset, IndexReg);
605
606 // Note: If we still have a frame index here, we know the offset is
607 // in range, as otherwise PPCSimplifyAddress would have converted it
608 // into a RegBase.
609 if (Addr.BaseType == Address::FrameIndexBase) {
610 MachineMemOperand *MMO =
611 FuncInfo.MF->getMachineMemOperand(
612 MachinePointerInfo::getFixedStack(Addr.Base.FI, Addr.Offset),
613 MachineMemOperand::MOStore, MFI.getObjectSize(Addr.Base.FI),
614 MFI.getObjectAlignment(Addr.Base.FI));
615
Rafael Espindolaea09c592014-02-18 22:05:46 +0000616 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
617 .addReg(SrcReg)
618 .addImm(Addr.Offset)
619 .addFrameIndex(Addr.Base.FI)
620 .addMemOperand(MMO);
Bill Schmidtccecf262013-08-30 02:29:45 +0000621
622 // Base reg with offset in range.
Bill Schmidt72e3d55a2013-08-30 03:07:11 +0000623 } else if (UseOffset)
Rafael Espindolaea09c592014-02-18 22:05:46 +0000624 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
Bill Schmidtccecf262013-08-30 02:29:45 +0000625 .addReg(SrcReg).addImm(Addr.Offset).addReg(Addr.Base.Reg);
626
627 // Indexed form.
Bill Schmidt72e3d55a2013-08-30 03:07:11 +0000628 else {
Bill Schmidtccecf262013-08-30 02:29:45 +0000629 // Get the RR opcode corresponding to the RI one. FIXME: It would be
630 // preferable to use the ImmToIdxMap from PPCRegisterInfo.cpp, but it
631 // is hard to get at.
632 switch (Opc) {
633 default: llvm_unreachable("Unexpected opcode!");
634 case PPC::STB: Opc = PPC::STBX; break;
635 case PPC::STH : Opc = PPC::STHX; break;
636 case PPC::STW : Opc = PPC::STWX; break;
637 case PPC::STB8: Opc = PPC::STBX8; break;
638 case PPC::STH8: Opc = PPC::STHX8; break;
639 case PPC::STW8: Opc = PPC::STWX8; break;
640 case PPC::STD: Opc = PPC::STDX; break;
641 case PPC::STFS: Opc = PPC::STFSX; break;
642 case PPC::STFD: Opc = PPC::STFDX; break;
643 }
Rafael Espindolaea09c592014-02-18 22:05:46 +0000644 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
Bill Schmidtccecf262013-08-30 02:29:45 +0000645 .addReg(SrcReg).addReg(Addr.Base.Reg).addReg(IndexReg);
646 }
647
648 return true;
649}
650
651// Attempt to fast-select a store instruction.
652bool PPCFastISel::SelectStore(const Instruction *I) {
653 Value *Op0 = I->getOperand(0);
654 unsigned SrcReg = 0;
655
656 // FIXME: No atomics loads are supported.
657 if (cast<StoreInst>(I)->isAtomic())
658 return false;
659
660 // Verify we have a legal type before going any further.
661 MVT VT;
662 if (!isLoadTypeLegal(Op0->getType(), VT))
663 return false;
664
665 // Get the value to be stored into a register.
666 SrcReg = getRegForValue(Op0);
667 if (SrcReg == 0)
668 return false;
669
670 // See if we can handle this address.
671 Address Addr;
672 if (!PPCComputeAddress(I->getOperand(1), Addr))
673 return false;
674
675 if (!PPCEmitStore(VT, SrcReg, Addr))
676 return false;
677
678 return true;
679}
680
Bill Schmidt03008132013-08-25 22:33:42 +0000681// Attempt to fast-select a branch instruction.
682bool PPCFastISel::SelectBranch(const Instruction *I) {
683 const BranchInst *BI = cast<BranchInst>(I);
684 MachineBasicBlock *BrBB = FuncInfo.MBB;
685 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
686 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
687
688 // For now, just try the simplest case where it's fed by a compare.
689 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
690 Optional<PPC::Predicate> OptPPCPred = getComparePred(CI->getPredicate());
691 if (!OptPPCPred)
692 return false;
693
694 PPC::Predicate PPCPred = OptPPCPred.getValue();
695
696 // Take advantage of fall-through opportunities.
697 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
698 std::swap(TBB, FBB);
699 PPCPred = PPC::InvertPredicate(PPCPred);
700 }
701
702 unsigned CondReg = createResultReg(&PPC::CRRCRegClass);
703
704 if (!PPCEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned(),
705 CondReg))
706 return false;
707
Rafael Espindolaea09c592014-02-18 22:05:46 +0000708 BuildMI(*BrBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::BCC))
Bill Schmidt03008132013-08-25 22:33:42 +0000709 .addImm(PPCPred).addReg(CondReg).addMBB(TBB);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000710 FastEmitBranch(FBB, DbgLoc);
Bill Schmidt03008132013-08-25 22:33:42 +0000711 FuncInfo.MBB->addSuccessor(TBB);
712 return true;
713
714 } else if (const ConstantInt *CI =
715 dyn_cast<ConstantInt>(BI->getCondition())) {
716 uint64_t Imm = CI->getZExtValue();
717 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
Rafael Espindolaea09c592014-02-18 22:05:46 +0000718 FastEmitBranch(Target, DbgLoc);
Bill Schmidt03008132013-08-25 22:33:42 +0000719 return true;
720 }
721
722 // FIXME: ARM looks for a case where the block containing the compare
723 // has been split from the block containing the branch. If this happens,
724 // there is a vreg available containing the result of the compare. I'm
725 // not sure we can do much, as we've lost the predicate information with
726 // the compare instruction -- we have a 4-bit CR but don't know which bit
727 // to test here.
728 return false;
729}
730
731// Attempt to emit a compare of the two source values. Signed and unsigned
732// comparisons are supported. Return false if we can't handle it.
733bool PPCFastISel::PPCEmitCmp(const Value *SrcValue1, const Value *SrcValue2,
734 bool IsZExt, unsigned DestReg) {
735 Type *Ty = SrcValue1->getType();
736 EVT SrcEVT = TLI.getValueType(Ty, true);
737 if (!SrcEVT.isSimple())
738 return false;
739 MVT SrcVT = SrcEVT.getSimpleVT();
740
Eric Christopher1b8e7632014-05-22 01:07:24 +0000741 if (SrcVT == MVT::i1 && PPCSubTarget->useCRBits())
Hal Finkel940ab932014-02-28 00:27:01 +0000742 return false;
743
Bill Schmidt03008132013-08-25 22:33:42 +0000744 // See if operand 2 is an immediate encodeable in the compare.
745 // FIXME: Operands are not in canonical order at -O0, so an immediate
746 // operand in position 1 is a lost opportunity for now. We are
747 // similar to ARM in this regard.
748 long Imm = 0;
749 bool UseImm = false;
750
751 // Only 16-bit integer constants can be represented in compares for
752 // PowerPC. Others will be materialized into a register.
753 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(SrcValue2)) {
754 if (SrcVT == MVT::i64 || SrcVT == MVT::i32 || SrcVT == MVT::i16 ||
755 SrcVT == MVT::i8 || SrcVT == MVT::i1) {
756 const APInt &CIVal = ConstInt->getValue();
757 Imm = (IsZExt) ? (long)CIVal.getZExtValue() : (long)CIVal.getSExtValue();
758 if ((IsZExt && isUInt<16>(Imm)) || (!IsZExt && isInt<16>(Imm)))
759 UseImm = true;
760 }
761 }
762
763 unsigned CmpOpc;
764 bool NeedsExt = false;
765 switch (SrcVT.SimpleTy) {
766 default: return false;
767 case MVT::f32:
768 CmpOpc = PPC::FCMPUS;
769 break;
770 case MVT::f64:
771 CmpOpc = PPC::FCMPUD;
772 break;
773 case MVT::i1:
774 case MVT::i8:
775 case MVT::i16:
776 NeedsExt = true;
777 // Intentional fall-through.
778 case MVT::i32:
779 if (!UseImm)
780 CmpOpc = IsZExt ? PPC::CMPLW : PPC::CMPW;
781 else
782 CmpOpc = IsZExt ? PPC::CMPLWI : PPC::CMPWI;
783 break;
784 case MVT::i64:
785 if (!UseImm)
786 CmpOpc = IsZExt ? PPC::CMPLD : PPC::CMPD;
787 else
788 CmpOpc = IsZExt ? PPC::CMPLDI : PPC::CMPDI;
789 break;
790 }
791
792 unsigned SrcReg1 = getRegForValue(SrcValue1);
793 if (SrcReg1 == 0)
794 return false;
795
796 unsigned SrcReg2 = 0;
797 if (!UseImm) {
798 SrcReg2 = getRegForValue(SrcValue2);
799 if (SrcReg2 == 0)
800 return false;
801 }
802
803 if (NeedsExt) {
804 unsigned ExtReg = createResultReg(&PPC::GPRCRegClass);
805 if (!PPCEmitIntExt(SrcVT, SrcReg1, MVT::i32, ExtReg, IsZExt))
806 return false;
807 SrcReg1 = ExtReg;
808
809 if (!UseImm) {
810 unsigned ExtReg = createResultReg(&PPC::GPRCRegClass);
811 if (!PPCEmitIntExt(SrcVT, SrcReg2, MVT::i32, ExtReg, IsZExt))
812 return false;
813 SrcReg2 = ExtReg;
814 }
815 }
816
817 if (!UseImm)
Rafael Espindolaea09c592014-02-18 22:05:46 +0000818 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc), DestReg)
Bill Schmidt03008132013-08-25 22:33:42 +0000819 .addReg(SrcReg1).addReg(SrcReg2);
820 else
Rafael Espindolaea09c592014-02-18 22:05:46 +0000821 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc), DestReg)
Bill Schmidt03008132013-08-25 22:33:42 +0000822 .addReg(SrcReg1).addImm(Imm);
823
824 return true;
825}
826
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000827// Attempt to fast-select a floating-point extend instruction.
828bool PPCFastISel::SelectFPExt(const Instruction *I) {
829 Value *Src = I->getOperand(0);
830 EVT SrcVT = TLI.getValueType(Src->getType(), true);
831 EVT DestVT = TLI.getValueType(I->getType(), true);
832
833 if (SrcVT != MVT::f32 || DestVT != MVT::f64)
834 return false;
835
836 unsigned SrcReg = getRegForValue(Src);
837 if (!SrcReg)
838 return false;
839
840 // No code is generated for a FP extend.
841 UpdateValueMap(I, SrcReg);
842 return true;
843}
844
845// Attempt to fast-select a floating-point truncate instruction.
846bool PPCFastISel::SelectFPTrunc(const Instruction *I) {
847 Value *Src = I->getOperand(0);
848 EVT SrcVT = TLI.getValueType(Src->getType(), true);
849 EVT DestVT = TLI.getValueType(I->getType(), true);
850
851 if (SrcVT != MVT::f64 || DestVT != MVT::f32)
852 return false;
853
854 unsigned SrcReg = getRegForValue(Src);
855 if (!SrcReg)
856 return false;
857
858 // Round the result to single precision.
859 unsigned DestReg = createResultReg(&PPC::F4RCRegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000860 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::FRSP), DestReg)
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000861 .addReg(SrcReg);
862
863 UpdateValueMap(I, DestReg);
864 return true;
865}
866
867// Move an i32 or i64 value in a GPR to an f64 value in an FPR.
868// FIXME: When direct register moves are implemented (see PowerISA 2.08),
869// those should be used instead of moving via a stack slot when the
870// subtarget permits.
871// FIXME: The code here is sloppy for the 4-byte case. Can use a 4-byte
872// stack slot and 4-byte store/load sequence. Or just sext the 4-byte
873// case to 8 bytes which produces tighter code but wastes stack space.
874unsigned PPCFastISel::PPCMoveToFPReg(MVT SrcVT, unsigned SrcReg,
875 bool IsSigned) {
876
877 // If necessary, extend 32-bit int to 64-bit.
878 if (SrcVT == MVT::i32) {
879 unsigned TmpReg = createResultReg(&PPC::G8RCRegClass);
880 if (!PPCEmitIntExt(MVT::i32, SrcReg, MVT::i64, TmpReg, !IsSigned))
881 return 0;
882 SrcReg = TmpReg;
883 }
884
885 // Get a stack slot 8 bytes wide, aligned on an 8-byte boundary.
886 Address Addr;
887 Addr.BaseType = Address::FrameIndexBase;
888 Addr.Base.FI = MFI.CreateStackObject(8, 8, false);
889
890 // Store the value from the GPR.
891 if (!PPCEmitStore(MVT::i64, SrcReg, Addr))
892 return 0;
893
894 // Load the integer value into an FPR. The kind of load used depends
895 // on a number of conditions.
896 unsigned LoadOpc = PPC::LFD;
897
898 if (SrcVT == MVT::i32) {
Bill Schmidtff9622e2014-03-18 14:32:50 +0000899 if (!IsSigned) {
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000900 LoadOpc = PPC::LFIWZX;
Bill Schmidtff9622e2014-03-18 14:32:50 +0000901 Addr.Offset = 4;
Eric Christopher1b8e7632014-05-22 01:07:24 +0000902 } else if (PPCSubTarget->hasLFIWAX()) {
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000903 LoadOpc = PPC::LFIWAX;
Bill Schmidtff9622e2014-03-18 14:32:50 +0000904 Addr.Offset = 4;
905 }
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000906 }
907
908 const TargetRegisterClass *RC = &PPC::F8RCRegClass;
909 unsigned ResultReg = 0;
910 if (!PPCEmitLoad(MVT::f64, ResultReg, Addr, RC, !IsSigned, LoadOpc))
911 return 0;
912
913 return ResultReg;
914}
915
916// Attempt to fast-select an integer-to-floating-point conversion.
917bool PPCFastISel::SelectIToFP(const Instruction *I, bool IsSigned) {
918 MVT DstVT;
919 Type *DstTy = I->getType();
920 if (!isTypeLegal(DstTy, DstVT))
921 return false;
922
923 if (DstVT != MVT::f32 && DstVT != MVT::f64)
924 return false;
925
926 Value *Src = I->getOperand(0);
927 EVT SrcEVT = TLI.getValueType(Src->getType(), true);
928 if (!SrcEVT.isSimple())
929 return false;
930
931 MVT SrcVT = SrcEVT.getSimpleVT();
932
933 if (SrcVT != MVT::i8 && SrcVT != MVT::i16 &&
934 SrcVT != MVT::i32 && SrcVT != MVT::i64)
935 return false;
936
937 unsigned SrcReg = getRegForValue(Src);
938 if (SrcReg == 0)
939 return false;
940
941 // We can only lower an unsigned convert if we have the newer
942 // floating-point conversion operations.
Eric Christopher1b8e7632014-05-22 01:07:24 +0000943 if (!IsSigned && !PPCSubTarget->hasFPCVT())
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000944 return false;
945
946 // FIXME: For now we require the newer floating-point conversion operations
947 // (which are present only on P7 and A2 server models) when converting
948 // to single-precision float. Otherwise we have to generate a lot of
949 // fiddly code to avoid double rounding. If necessary, the fiddly code
950 // can be found in PPCTargetLowering::LowerINT_TO_FP().
Eric Christopher1b8e7632014-05-22 01:07:24 +0000951 if (DstVT == MVT::f32 && !PPCSubTarget->hasFPCVT())
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000952 return false;
953
954 // Extend the input if necessary.
955 if (SrcVT == MVT::i8 || SrcVT == MVT::i16) {
956 unsigned TmpReg = createResultReg(&PPC::G8RCRegClass);
957 if (!PPCEmitIntExt(SrcVT, SrcReg, MVT::i64, TmpReg, !IsSigned))
958 return false;
959 SrcVT = MVT::i64;
960 SrcReg = TmpReg;
961 }
962
963 // Move the integer value to an FPR.
964 unsigned FPReg = PPCMoveToFPReg(SrcVT, SrcReg, IsSigned);
965 if (FPReg == 0)
966 return false;
967
968 // Determine the opcode for the conversion.
969 const TargetRegisterClass *RC = &PPC::F8RCRegClass;
970 unsigned DestReg = createResultReg(RC);
971 unsigned Opc;
972
973 if (DstVT == MVT::f32)
974 Opc = IsSigned ? PPC::FCFIDS : PPC::FCFIDUS;
975 else
976 Opc = IsSigned ? PPC::FCFID : PPC::FCFIDU;
977
978 // Generate the convert.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000979 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000980 .addReg(FPReg);
981
982 UpdateValueMap(I, DestReg);
983 return true;
984}
985
986// Move the floating-point value in SrcReg into an integer destination
987// register, and return the register (or zero if we can't handle it).
988// FIXME: When direct register moves are implemented (see PowerISA 2.08),
989// those should be used instead of moving via a stack slot when the
990// subtarget permits.
991unsigned PPCFastISel::PPCMoveToIntReg(const Instruction *I, MVT VT,
992 unsigned SrcReg, bool IsSigned) {
993 // Get a stack slot 8 bytes wide, aligned on an 8-byte boundary.
994 // Note that if have STFIWX available, we could use a 4-byte stack
995 // slot for i32, but this being fast-isel we'll just go with the
996 // easiest code gen possible.
997 Address Addr;
998 Addr.BaseType = Address::FrameIndexBase;
999 Addr.Base.FI = MFI.CreateStackObject(8, 8, false);
1000
1001 // Store the value from the FPR.
1002 if (!PPCEmitStore(MVT::f64, SrcReg, Addr))
1003 return 0;
1004
1005 // Reload it into a GPR. If we want an i32, modify the address
1006 // to have a 4-byte offset so we load from the right place.
1007 if (VT == MVT::i32)
1008 Addr.Offset = 4;
1009
1010 // Look at the currently assigned register for this instruction
1011 // to determine the required register class.
1012 unsigned AssignedReg = FuncInfo.ValueMap[I];
1013 const TargetRegisterClass *RC =
Craig Topper062a2ba2014-04-25 05:30:21 +00001014 AssignedReg ? MRI.getRegClass(AssignedReg) : nullptr;
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001015
1016 unsigned ResultReg = 0;
1017 if (!PPCEmitLoad(VT, ResultReg, Addr, RC, !IsSigned))
1018 return 0;
1019
1020 return ResultReg;
1021}
1022
1023// Attempt to fast-select a floating-point-to-integer conversion.
1024bool PPCFastISel::SelectFPToI(const Instruction *I, bool IsSigned) {
1025 MVT DstVT, SrcVT;
1026 Type *DstTy = I->getType();
1027 if (!isTypeLegal(DstTy, DstVT))
1028 return false;
1029
1030 if (DstVT != MVT::i32 && DstVT != MVT::i64)
1031 return false;
1032
Bill Schmidt83973ef2014-06-24 20:05:18 +00001033 // If we don't have FCTIDUZ and we need it, punt to SelectionDAG.
1034 if (DstVT == MVT::i64 && !IsSigned && !PPCSubTarget->hasFPCVT())
1035 return false;
1036
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001037 Value *Src = I->getOperand(0);
1038 Type *SrcTy = Src->getType();
1039 if (!isTypeLegal(SrcTy, SrcVT))
1040 return false;
1041
1042 if (SrcVT != MVT::f32 && SrcVT != MVT::f64)
1043 return false;
1044
1045 unsigned SrcReg = getRegForValue(Src);
1046 if (SrcReg == 0)
1047 return false;
1048
1049 // Convert f32 to f64 if necessary. This is just a meaningless copy
1050 // to get the register class right. COPY_TO_REGCLASS is needed since
1051 // a COPY from F4RC to F8RC is converted to a F4RC-F4RC copy downstream.
1052 const TargetRegisterClass *InRC = MRI.getRegClass(SrcReg);
1053 if (InRC == &PPC::F4RCRegClass) {
1054 unsigned TmpReg = createResultReg(&PPC::F8RCRegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001055 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001056 TII.get(TargetOpcode::COPY_TO_REGCLASS), TmpReg)
1057 .addReg(SrcReg).addImm(PPC::F8RCRegClassID);
1058 SrcReg = TmpReg;
1059 }
1060
1061 // Determine the opcode for the conversion, which takes place
1062 // entirely within FPRs.
1063 unsigned DestReg = createResultReg(&PPC::F8RCRegClass);
1064 unsigned Opc;
1065
1066 if (DstVT == MVT::i32)
1067 if (IsSigned)
1068 Opc = PPC::FCTIWZ;
1069 else
Eric Christopher1b8e7632014-05-22 01:07:24 +00001070 Opc = PPCSubTarget->hasFPCVT() ? PPC::FCTIWUZ : PPC::FCTIDZ;
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001071 else
1072 Opc = IsSigned ? PPC::FCTIDZ : PPC::FCTIDUZ;
1073
1074 // Generate the convert.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001075 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001076 .addReg(SrcReg);
1077
1078 // Now move the integer value from a float register to an integer register.
1079 unsigned IntReg = PPCMoveToIntReg(I, DstVT, DestReg, IsSigned);
1080 if (IntReg == 0)
1081 return false;
1082
1083 UpdateValueMap(I, IntReg);
1084 return true;
1085}
1086
Bill Schmidtccecf262013-08-30 02:29:45 +00001087// Attempt to fast-select a binary integer operation that isn't already
1088// handled automatically.
1089bool PPCFastISel::SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode) {
1090 EVT DestVT = TLI.getValueType(I->getType(), true);
1091
1092 // We can get here in the case when we have a binary operation on a non-legal
1093 // type and the target independent selector doesn't know how to handle it.
1094 if (DestVT != MVT::i16 && DestVT != MVT::i8)
1095 return false;
1096
1097 // Look at the currently assigned register for this instruction
1098 // to determine the required register class. If there is no register,
1099 // make a conservative choice (don't assign R0).
1100 unsigned AssignedReg = FuncInfo.ValueMap[I];
1101 const TargetRegisterClass *RC =
1102 (AssignedReg ? MRI.getRegClass(AssignedReg) :
1103 &PPC::GPRC_and_GPRC_NOR0RegClass);
1104 bool IsGPRC = RC->hasSuperClassEq(&PPC::GPRCRegClass);
1105
1106 unsigned Opc;
1107 switch (ISDOpcode) {
1108 default: return false;
1109 case ISD::ADD:
1110 Opc = IsGPRC ? PPC::ADD4 : PPC::ADD8;
1111 break;
1112 case ISD::OR:
1113 Opc = IsGPRC ? PPC::OR : PPC::OR8;
1114 break;
1115 case ISD::SUB:
1116 Opc = IsGPRC ? PPC::SUBF : PPC::SUBF8;
1117 break;
1118 }
1119
1120 unsigned ResultReg = createResultReg(RC ? RC : &PPC::G8RCRegClass);
1121 unsigned SrcReg1 = getRegForValue(I->getOperand(0));
1122 if (SrcReg1 == 0) return false;
1123
1124 // Handle case of small immediate operand.
1125 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(I->getOperand(1))) {
1126 const APInt &CIVal = ConstInt->getValue();
1127 int Imm = (int)CIVal.getSExtValue();
1128 bool UseImm = true;
1129 if (isInt<16>(Imm)) {
1130 switch (Opc) {
1131 default:
1132 llvm_unreachable("Missing case!");
1133 case PPC::ADD4:
1134 Opc = PPC::ADDI;
1135 MRI.setRegClass(SrcReg1, &PPC::GPRC_and_GPRC_NOR0RegClass);
1136 break;
1137 case PPC::ADD8:
1138 Opc = PPC::ADDI8;
1139 MRI.setRegClass(SrcReg1, &PPC::G8RC_and_G8RC_NOX0RegClass);
1140 break;
1141 case PPC::OR:
1142 Opc = PPC::ORI;
1143 break;
1144 case PPC::OR8:
1145 Opc = PPC::ORI8;
1146 break;
1147 case PPC::SUBF:
1148 if (Imm == -32768)
1149 UseImm = false;
1150 else {
1151 Opc = PPC::ADDI;
1152 MRI.setRegClass(SrcReg1, &PPC::GPRC_and_GPRC_NOR0RegClass);
1153 Imm = -Imm;
1154 }
1155 break;
1156 case PPC::SUBF8:
1157 if (Imm == -32768)
1158 UseImm = false;
1159 else {
1160 Opc = PPC::ADDI8;
1161 MRI.setRegClass(SrcReg1, &PPC::G8RC_and_G8RC_NOX0RegClass);
1162 Imm = -Imm;
1163 }
1164 break;
1165 }
1166
1167 if (UseImm) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001168 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
1169 ResultReg)
1170 .addReg(SrcReg1)
1171 .addImm(Imm);
Bill Schmidtccecf262013-08-30 02:29:45 +00001172 UpdateValueMap(I, ResultReg);
1173 return true;
1174 }
1175 }
1176 }
1177
1178 // Reg-reg case.
1179 unsigned SrcReg2 = getRegForValue(I->getOperand(1));
1180 if (SrcReg2 == 0) return false;
1181
1182 // Reverse operands for subtract-from.
1183 if (ISDOpcode == ISD::SUB)
1184 std::swap(SrcReg1, SrcReg2);
1185
Rafael Espindolaea09c592014-02-18 22:05:46 +00001186 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Bill Schmidtccecf262013-08-30 02:29:45 +00001187 .addReg(SrcReg1).addReg(SrcReg2);
1188 UpdateValueMap(I, ResultReg);
1189 return true;
1190}
1191
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001192// Handle arguments to a call that we're attempting to fast-select.
1193// Return false if the arguments are too complex for us at the moment.
1194bool PPCFastISel::processCallArgs(SmallVectorImpl<Value*> &Args,
1195 SmallVectorImpl<unsigned> &ArgRegs,
1196 SmallVectorImpl<MVT> &ArgVTs,
1197 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1198 SmallVectorImpl<unsigned> &RegArgs,
1199 CallingConv::ID CC,
1200 unsigned &NumBytes,
1201 bool IsVarArg) {
1202 SmallVector<CCValAssign, 16> ArgLocs;
1203 CCState CCInfo(CC, IsVarArg, *FuncInfo.MF, TM, ArgLocs, *Context);
Ulrich Weigandf316e1d2014-06-23 13:47:52 +00001204
1205 // Reserve space for the linkage area on the stack.
Ulrich Weigand8658f172014-07-20 23:43:15 +00001206 bool isELFv2ABI = PPCSubTarget->isELFv2ABI();
1207 unsigned LinkageSize = PPCFrameLowering::getLinkageSize(true, false,
1208 isELFv2ABI);
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00001209 CCInfo.AllocateStack(LinkageSize, 8);
Ulrich Weigandf316e1d2014-06-23 13:47:52 +00001210
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001211 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_PPC64_ELF_FIS);
1212
1213 // Bail out if we can't handle any of the arguments.
1214 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1215 CCValAssign &VA = ArgLocs[I];
1216 MVT ArgVT = ArgVTs[VA.getValNo()];
1217
1218 // Skip vector arguments for now, as well as long double and
1219 // uint128_t, and anything that isn't passed in a register.
Hal Finkel940ab932014-02-28 00:27:01 +00001220 if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64 || ArgVT == MVT::i1 ||
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001221 !VA.isRegLoc() || VA.needsCustom())
1222 return false;
1223
1224 // Skip bit-converted arguments for now.
1225 if (VA.getLocInfo() == CCValAssign::BCvt)
1226 return false;
1227 }
1228
1229 // Get a count of how many bytes are to be pushed onto the stack.
1230 NumBytes = CCInfo.getNextStackOffset();
1231
Ulrich Weigandf316e1d2014-06-23 13:47:52 +00001232 // The prolog code of the callee may store up to 8 GPR argument registers to
1233 // the stack, allowing va_start to index over them in memory if its varargs.
1234 // Because we cannot tell if this is needed on the caller side, we have to
1235 // conservatively assume that it is needed. As such, make sure we have at
1236 // least enough stack space for the caller to store the 8 GPRs.
Ulrich Weigand8658f172014-07-20 23:43:15 +00001237 // FIXME: On ELFv2, it may be unnecessary to allocate the parameter area.
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00001238 NumBytes = std::max(NumBytes, LinkageSize + 64);
Ulrich Weigandf316e1d2014-06-23 13:47:52 +00001239
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001240 // Issue CALLSEQ_START.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001241 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001242 TII.get(TII.getCallFrameSetupOpcode()))
1243 .addImm(NumBytes);
1244
1245 // Prepare to assign register arguments. Every argument uses up a
1246 // GPR protocol register even if it's passed in a floating-point
1247 // register.
1248 unsigned NextGPR = PPC::X3;
1249 unsigned NextFPR = PPC::F1;
1250
1251 // Process arguments.
1252 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1253 CCValAssign &VA = ArgLocs[I];
1254 unsigned Arg = ArgRegs[VA.getValNo()];
1255 MVT ArgVT = ArgVTs[VA.getValNo()];
1256
1257 // Handle argument promotion and bitcasts.
1258 switch (VA.getLocInfo()) {
1259 default:
1260 llvm_unreachable("Unknown loc info!");
1261 case CCValAssign::Full:
1262 break;
1263 case CCValAssign::SExt: {
1264 MVT DestVT = VA.getLocVT();
1265 const TargetRegisterClass *RC =
1266 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1267 unsigned TmpReg = createResultReg(RC);
1268 if (!PPCEmitIntExt(ArgVT, Arg, DestVT, TmpReg, /*IsZExt*/false))
1269 llvm_unreachable("Failed to emit a sext!");
1270 ArgVT = DestVT;
1271 Arg = TmpReg;
1272 break;
1273 }
1274 case CCValAssign::AExt:
1275 case CCValAssign::ZExt: {
1276 MVT DestVT = VA.getLocVT();
1277 const TargetRegisterClass *RC =
1278 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1279 unsigned TmpReg = createResultReg(RC);
1280 if (!PPCEmitIntExt(ArgVT, Arg, DestVT, TmpReg, /*IsZExt*/true))
1281 llvm_unreachable("Failed to emit a zext!");
1282 ArgVT = DestVT;
1283 Arg = TmpReg;
1284 break;
1285 }
1286 case CCValAssign::BCvt: {
1287 // FIXME: Not yet handled.
1288 llvm_unreachable("Should have bailed before getting here!");
1289 break;
1290 }
1291 }
1292
1293 // Copy this argument to the appropriate register.
1294 unsigned ArgReg;
1295 if (ArgVT == MVT::f32 || ArgVT == MVT::f64) {
1296 ArgReg = NextFPR++;
1297 ++NextGPR;
1298 } else
1299 ArgReg = NextGPR++;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001300
1301 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1302 TII.get(TargetOpcode::COPY), ArgReg).addReg(Arg);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001303 RegArgs.push_back(ArgReg);
1304 }
1305
1306 return true;
1307}
1308
1309// For a call that we've determined we can fast-select, finish the
1310// call sequence and generate a copy to obtain the return value (if any).
1311void PPCFastISel::finishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
1312 const Instruction *I, CallingConv::ID CC,
1313 unsigned &NumBytes, bool IsVarArg) {
1314 // Issue CallSEQ_END.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001315 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001316 TII.get(TII.getCallFrameDestroyOpcode()))
1317 .addImm(NumBytes).addImm(0);
1318
1319 // Next, generate a copy to obtain the return value.
1320 // FIXME: No multi-register return values yet, though I don't foresee
1321 // any real difficulties there.
1322 if (RetVT != MVT::isVoid) {
1323 SmallVector<CCValAssign, 16> RVLocs;
1324 CCState CCInfo(CC, IsVarArg, *FuncInfo.MF, TM, RVLocs, *Context);
1325 CCInfo.AnalyzeCallResult(RetVT, RetCC_PPC64_ELF_FIS);
1326 CCValAssign &VA = RVLocs[0];
1327 assert(RVLocs.size() == 1 && "No support for multi-reg return values!");
1328 assert(VA.isRegLoc() && "Can only return in registers!");
1329
1330 MVT DestVT = VA.getValVT();
1331 MVT CopyVT = DestVT;
1332
1333 // Ints smaller than a register still arrive in a full 64-bit
1334 // register, so make sure we recognize this.
1335 if (RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32)
1336 CopyVT = MVT::i64;
1337
1338 unsigned SourcePhysReg = VA.getLocReg();
Bill Schmidt0954ea12013-08-30 23:25:30 +00001339 unsigned ResultReg = 0;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001340
1341 if (RetVT == CopyVT) {
1342 const TargetRegisterClass *CpyRC = TLI.getRegClassFor(CopyVT);
1343 ResultReg = createResultReg(CpyRC);
1344
Rafael Espindolaea09c592014-02-18 22:05:46 +00001345 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001346 TII.get(TargetOpcode::COPY), ResultReg)
1347 .addReg(SourcePhysReg);
1348
1349 // If necessary, round the floating result to single precision.
1350 } else if (CopyVT == MVT::f64) {
1351 ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
Rafael Espindolaea09c592014-02-18 22:05:46 +00001352 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::FRSP),
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001353 ResultReg).addReg(SourcePhysReg);
1354
1355 // If only the low half of a general register is needed, generate
1356 // a GPRC copy instead of a G8RC copy. (EXTRACT_SUBREG can't be
1357 // used along the fast-isel path (not lowered), and downstream logic
1358 // also doesn't like a direct subreg copy on a physical reg.)
1359 } else if (RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32) {
1360 ResultReg = createResultReg(&PPC::GPRCRegClass);
1361 // Convert physical register from G8RC to GPRC.
1362 SourcePhysReg -= PPC::X0 - PPC::R0;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001363 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001364 TII.get(TargetOpcode::COPY), ResultReg)
1365 .addReg(SourcePhysReg);
1366 }
1367
Bill Schmidt0954ea12013-08-30 23:25:30 +00001368 assert(ResultReg && "ResultReg unset!");
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001369 UsedRegs.push_back(SourcePhysReg);
1370 UpdateValueMap(I, ResultReg);
1371 }
1372}
1373
1374// Attempt to fast-select a call instruction.
1375bool PPCFastISel::SelectCall(const Instruction *I) {
1376 const CallInst *CI = cast<CallInst>(I);
1377 const Value *Callee = CI->getCalledValue();
1378
1379 // Can't handle inline asm.
1380 if (isa<InlineAsm>(Callee))
1381 return false;
1382
1383 // Allow SelectionDAG isel to handle tail calls.
1384 if (CI->isTailCall())
1385 return false;
1386
1387 // Obtain calling convention.
1388 ImmutableCallSite CS(CI);
1389 CallingConv::ID CC = CS.getCallingConv();
1390
1391 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1392 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1393 bool IsVarArg = FTy->isVarArg();
1394
1395 // Not ready for varargs yet.
1396 if (IsVarArg)
1397 return false;
1398
1399 // Handle simple calls for now, with legal return types and
1400 // those that can be extended.
1401 Type *RetTy = I->getType();
1402 MVT RetVT;
1403 if (RetTy->isVoidTy())
1404 RetVT = MVT::isVoid;
1405 else if (!isTypeLegal(RetTy, RetVT) && RetVT != MVT::i16 &&
1406 RetVT != MVT::i8)
1407 return false;
1408
1409 // FIXME: No multi-register return values yet.
1410 if (RetVT != MVT::isVoid && RetVT != MVT::i8 && RetVT != MVT::i16 &&
1411 RetVT != MVT::i32 && RetVT != MVT::i64 && RetVT != MVT::f32 &&
1412 RetVT != MVT::f64) {
1413 SmallVector<CCValAssign, 16> RVLocs;
1414 CCState CCInfo(CC, IsVarArg, *FuncInfo.MF, TM, RVLocs, *Context);
1415 CCInfo.AnalyzeCallResult(RetVT, RetCC_PPC64_ELF_FIS);
1416 if (RVLocs.size() > 1)
1417 return false;
1418 }
1419
1420 // Bail early if more than 8 arguments, as we only currently
1421 // handle arguments passed in registers.
1422 unsigned NumArgs = CS.arg_size();
1423 if (NumArgs > 8)
1424 return false;
1425
1426 // Set up the argument vectors.
1427 SmallVector<Value*, 8> Args;
1428 SmallVector<unsigned, 8> ArgRegs;
1429 SmallVector<MVT, 8> ArgVTs;
1430 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1431
1432 Args.reserve(NumArgs);
1433 ArgRegs.reserve(NumArgs);
1434 ArgVTs.reserve(NumArgs);
1435 ArgFlags.reserve(NumArgs);
1436
1437 for (ImmutableCallSite::arg_iterator II = CS.arg_begin(), IE = CS.arg_end();
1438 II != IE; ++II) {
1439 // FIXME: ARM does something for intrinsic calls here, check into that.
1440
1441 unsigned AttrIdx = II - CS.arg_begin() + 1;
1442
1443 // Only handle easy calls for now. It would be reasonably easy
1444 // to handle <= 8-byte structures passed ByVal in registers, but we
1445 // have to ensure they are right-justified in the register.
1446 if (CS.paramHasAttr(AttrIdx, Attribute::InReg) ||
1447 CS.paramHasAttr(AttrIdx, Attribute::StructRet) ||
1448 CS.paramHasAttr(AttrIdx, Attribute::Nest) ||
1449 CS.paramHasAttr(AttrIdx, Attribute::ByVal))
1450 return false;
1451
1452 ISD::ArgFlagsTy Flags;
1453 if (CS.paramHasAttr(AttrIdx, Attribute::SExt))
1454 Flags.setSExt();
1455 if (CS.paramHasAttr(AttrIdx, Attribute::ZExt))
1456 Flags.setZExt();
1457
1458 Type *ArgTy = (*II)->getType();
1459 MVT ArgVT;
1460 if (!isTypeLegal(ArgTy, ArgVT) && ArgVT != MVT::i16 && ArgVT != MVT::i8)
1461 return false;
1462
1463 if (ArgVT.isVector())
1464 return false;
1465
1466 unsigned Arg = getRegForValue(*II);
1467 if (Arg == 0)
1468 return false;
1469
Rafael Espindolaea09c592014-02-18 22:05:46 +00001470 unsigned OriginalAlignment = DL.getABITypeAlignment(ArgTy);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001471 Flags.setOrigAlign(OriginalAlignment);
1472
1473 Args.push_back(*II);
1474 ArgRegs.push_back(Arg);
1475 ArgVTs.push_back(ArgVT);
1476 ArgFlags.push_back(Flags);
1477 }
1478
1479 // Process the arguments.
1480 SmallVector<unsigned, 8> RegArgs;
1481 unsigned NumBytes;
1482
1483 if (!processCallArgs(Args, ArgRegs, ArgVTs, ArgFlags,
1484 RegArgs, CC, NumBytes, IsVarArg))
1485 return false;
1486
1487 // FIXME: No handling for function pointers yet. This requires
1488 // implementing the function descriptor (OPD) setup.
1489 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
1490 if (!GV)
1491 return false;
1492
1493 // Build direct call with NOP for TOC restore.
1494 // FIXME: We can and should optimize away the NOP for local calls.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001495 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001496 TII.get(PPC::BL8_NOP));
1497 // Add callee.
1498 MIB.addGlobalAddress(GV);
1499
1500 // Add implicit physical register uses to the call.
1501 for (unsigned II = 0, IE = RegArgs.size(); II != IE; ++II)
1502 MIB.addReg(RegArgs[II], RegState::Implicit);
1503
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00001504 // Direct calls in the ELFv2 ABI need the TOC register live into the call.
1505 if (PPCSubTarget->isELFv2ABI())
1506 MIB.addReg(PPC::X2, RegState::Implicit);
1507
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001508 // Add a register mask with the call-preserved registers. Proper
1509 // defs for return values will be added by setPhysRegsDeadExcept().
1510 MIB.addRegMask(TRI.getCallPreservedMask(CC));
1511
1512 // Finish off the call including any return values.
1513 SmallVector<unsigned, 4> UsedRegs;
1514 finishCall(RetVT, UsedRegs, I, CC, NumBytes, IsVarArg);
1515
1516 // Set all unused physregs defs as dead.
1517 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
1518
1519 return true;
1520}
1521
Bill Schmidtd89f6782013-08-26 19:42:51 +00001522// Attempt to fast-select a return instruction.
1523bool PPCFastISel::SelectRet(const Instruction *I) {
1524
1525 if (!FuncInfo.CanLowerReturn)
1526 return false;
1527
1528 const ReturnInst *Ret = cast<ReturnInst>(I);
1529 const Function &F = *I->getParent()->getParent();
1530
1531 // Build a list of return value registers.
1532 SmallVector<unsigned, 4> RetRegs;
1533 CallingConv::ID CC = F.getCallingConv();
1534
1535 if (Ret->getNumOperands() > 0) {
1536 SmallVector<ISD::OutputArg, 4> Outs;
1537 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI);
1538
1539 // Analyze operands of the call, assigning locations to each operand.
1540 SmallVector<CCValAssign, 16> ValLocs;
1541 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, TM, ValLocs, *Context);
1542 CCInfo.AnalyzeReturn(Outs, RetCC_PPC64_ELF_FIS);
1543 const Value *RV = Ret->getOperand(0);
1544
1545 // FIXME: Only one output register for now.
1546 if (ValLocs.size() > 1)
1547 return false;
1548
1549 // Special case for returning a constant integer of any size.
1550 // Materialize the constant as an i64 and copy it to the return
1551 // register. This avoids an unnecessary extend or truncate.
1552 if (isa<ConstantInt>(*RV)) {
1553 const Constant *C = cast<Constant>(RV);
1554 unsigned SrcReg = PPCMaterializeInt(C, MVT::i64);
1555 unsigned RetReg = ValLocs[0].getLocReg();
Rafael Espindolaea09c592014-02-18 22:05:46 +00001556 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1557 TII.get(TargetOpcode::COPY), RetReg).addReg(SrcReg);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001558 RetRegs.push_back(RetReg);
1559
1560 } else {
1561 unsigned Reg = getRegForValue(RV);
1562
1563 if (Reg == 0)
1564 return false;
1565
1566 // Copy the result values into the output registers.
1567 for (unsigned i = 0; i < ValLocs.size(); ++i) {
1568
1569 CCValAssign &VA = ValLocs[i];
1570 assert(VA.isRegLoc() && "Can only return in registers!");
1571 RetRegs.push_back(VA.getLocReg());
1572 unsigned SrcReg = Reg + VA.getValNo();
1573
1574 EVT RVEVT = TLI.getValueType(RV->getType());
1575 if (!RVEVT.isSimple())
1576 return false;
1577 MVT RVVT = RVEVT.getSimpleVT();
1578 MVT DestVT = VA.getLocVT();
1579
1580 if (RVVT != DestVT && RVVT != MVT::i8 &&
1581 RVVT != MVT::i16 && RVVT != MVT::i32)
1582 return false;
1583
1584 if (RVVT != DestVT) {
1585 switch (VA.getLocInfo()) {
1586 default:
1587 llvm_unreachable("Unknown loc info!");
1588 case CCValAssign::Full:
1589 llvm_unreachable("Full value assign but types don't match?");
1590 case CCValAssign::AExt:
1591 case CCValAssign::ZExt: {
1592 const TargetRegisterClass *RC =
1593 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1594 unsigned TmpReg = createResultReg(RC);
1595 if (!PPCEmitIntExt(RVVT, SrcReg, DestVT, TmpReg, true))
1596 return false;
1597 SrcReg = TmpReg;
1598 break;
1599 }
1600 case CCValAssign::SExt: {
1601 const TargetRegisterClass *RC =
1602 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1603 unsigned TmpReg = createResultReg(RC);
1604 if (!PPCEmitIntExt(RVVT, SrcReg, DestVT, TmpReg, false))
1605 return false;
1606 SrcReg = TmpReg;
1607 break;
1608 }
1609 }
1610 }
1611
Rafael Espindolaea09c592014-02-18 22:05:46 +00001612 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidtd89f6782013-08-26 19:42:51 +00001613 TII.get(TargetOpcode::COPY), RetRegs[i])
1614 .addReg(SrcReg);
1615 }
1616 }
1617 }
1618
Rafael Espindolaea09c592014-02-18 22:05:46 +00001619 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidtd89f6782013-08-26 19:42:51 +00001620 TII.get(PPC::BLR));
1621
1622 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
1623 MIB.addReg(RetRegs[i], RegState::Implicit);
1624
1625 return true;
1626}
1627
Bill Schmidt03008132013-08-25 22:33:42 +00001628// Attempt to emit an integer extend of SrcReg into DestReg. Both
1629// signed and zero extensions are supported. Return false if we
Bill Schmidtd89f6782013-08-26 19:42:51 +00001630// can't handle it.
Bill Schmidt03008132013-08-25 22:33:42 +00001631bool PPCFastISel::PPCEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1632 unsigned DestReg, bool IsZExt) {
Bill Schmidtd89f6782013-08-26 19:42:51 +00001633 if (DestVT != MVT::i32 && DestVT != MVT::i64)
1634 return false;
1635 if (SrcVT != MVT::i8 && SrcVT != MVT::i16 && SrcVT != MVT::i32)
1636 return false;
1637
1638 // Signed extensions use EXTSB, EXTSH, EXTSW.
1639 if (!IsZExt) {
1640 unsigned Opc;
1641 if (SrcVT == MVT::i8)
1642 Opc = (DestVT == MVT::i32) ? PPC::EXTSB : PPC::EXTSB8_32_64;
1643 else if (SrcVT == MVT::i16)
1644 Opc = (DestVT == MVT::i32) ? PPC::EXTSH : PPC::EXTSH8_32_64;
1645 else {
1646 assert(DestVT == MVT::i64 && "Signed extend from i32 to i32??");
1647 Opc = PPC::EXTSW_32_64;
1648 }
Rafael Espindolaea09c592014-02-18 22:05:46 +00001649 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidtd89f6782013-08-26 19:42:51 +00001650 .addReg(SrcReg);
1651
1652 // Unsigned 32-bit extensions use RLWINM.
1653 } else if (DestVT == MVT::i32) {
1654 unsigned MB;
1655 if (SrcVT == MVT::i8)
1656 MB = 24;
1657 else {
1658 assert(SrcVT == MVT::i16 && "Unsigned extend from i32 to i32??");
1659 MB = 16;
1660 }
Rafael Espindolaea09c592014-02-18 22:05:46 +00001661 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::RLWINM),
Bill Schmidtd89f6782013-08-26 19:42:51 +00001662 DestReg)
1663 .addReg(SrcReg).addImm(/*SH=*/0).addImm(MB).addImm(/*ME=*/31);
1664
1665 // Unsigned 64-bit extensions use RLDICL (with a 32-bit source).
1666 } else {
1667 unsigned MB;
1668 if (SrcVT == MVT::i8)
1669 MB = 56;
1670 else if (SrcVT == MVT::i16)
1671 MB = 48;
1672 else
1673 MB = 32;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001674 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidtd89f6782013-08-26 19:42:51 +00001675 TII.get(PPC::RLDICL_32_64), DestReg)
1676 .addReg(SrcReg).addImm(/*SH=*/0).addImm(MB);
1677 }
1678
1679 return true;
Bill Schmidt03008132013-08-25 22:33:42 +00001680}
1681
1682// Attempt to fast-select an indirect branch instruction.
1683bool PPCFastISel::SelectIndirectBr(const Instruction *I) {
1684 unsigned AddrReg = getRegForValue(I->getOperand(0));
1685 if (AddrReg == 0)
1686 return false;
1687
Rafael Espindolaea09c592014-02-18 22:05:46 +00001688 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::MTCTR8))
Bill Schmidt03008132013-08-25 22:33:42 +00001689 .addReg(AddrReg);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001690 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::BCTR8));
Bill Schmidt03008132013-08-25 22:33:42 +00001691
1692 const IndirectBrInst *IB = cast<IndirectBrInst>(I);
1693 for (unsigned i = 0, e = IB->getNumSuccessors(); i != e; ++i)
1694 FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[IB->getSuccessor(i)]);
1695
1696 return true;
1697}
1698
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001699// Attempt to fast-select an integer truncate instruction.
1700bool PPCFastISel::SelectTrunc(const Instruction *I) {
1701 Value *Src = I->getOperand(0);
1702 EVT SrcVT = TLI.getValueType(Src->getType(), true);
1703 EVT DestVT = TLI.getValueType(I->getType(), true);
1704
1705 if (SrcVT != MVT::i64 && SrcVT != MVT::i32 && SrcVT != MVT::i16)
1706 return false;
1707
1708 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8)
1709 return false;
1710
1711 unsigned SrcReg = getRegForValue(Src);
1712 if (!SrcReg)
1713 return false;
1714
1715 // The only interesting case is when we need to switch register classes.
1716 if (SrcVT == MVT::i64) {
1717 unsigned ResultReg = createResultReg(&PPC::GPRCRegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001718 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1719 TII.get(TargetOpcode::COPY),
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001720 ResultReg).addReg(SrcReg, 0, PPC::sub_32);
1721 SrcReg = ResultReg;
1722 }
1723
1724 UpdateValueMap(I, SrcReg);
1725 return true;
1726}
1727
Bill Schmidtd89f6782013-08-26 19:42:51 +00001728// Attempt to fast-select an integer extend instruction.
1729bool PPCFastISel::SelectIntExt(const Instruction *I) {
1730 Type *DestTy = I->getType();
1731 Value *Src = I->getOperand(0);
1732 Type *SrcTy = Src->getType();
1733
1734 bool IsZExt = isa<ZExtInst>(I);
1735 unsigned SrcReg = getRegForValue(Src);
1736 if (!SrcReg) return false;
1737
1738 EVT SrcEVT, DestEVT;
1739 SrcEVT = TLI.getValueType(SrcTy, true);
1740 DestEVT = TLI.getValueType(DestTy, true);
1741 if (!SrcEVT.isSimple())
1742 return false;
1743 if (!DestEVT.isSimple())
1744 return false;
1745
1746 MVT SrcVT = SrcEVT.getSimpleVT();
1747 MVT DestVT = DestEVT.getSimpleVT();
1748
1749 // If we know the register class needed for the result of this
1750 // instruction, use it. Otherwise pick the register class of the
1751 // correct size that does not contain X0/R0, since we don't know
1752 // whether downstream uses permit that assignment.
1753 unsigned AssignedReg = FuncInfo.ValueMap[I];
1754 const TargetRegisterClass *RC =
1755 (AssignedReg ? MRI.getRegClass(AssignedReg) :
1756 (DestVT == MVT::i64 ? &PPC::G8RC_and_G8RC_NOX0RegClass :
1757 &PPC::GPRC_and_GPRC_NOR0RegClass));
1758 unsigned ResultReg = createResultReg(RC);
1759
1760 if (!PPCEmitIntExt(SrcVT, SrcReg, DestVT, ResultReg, IsZExt))
1761 return false;
1762
1763 UpdateValueMap(I, ResultReg);
1764 return true;
1765}
1766
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001767// Attempt to fast-select an instruction that wasn't handled by
Bill Schmidt03008132013-08-25 22:33:42 +00001768// the table-generated machinery.
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001769bool PPCFastISel::TargetSelectInstruction(const Instruction *I) {
Bill Schmidt03008132013-08-25 22:33:42 +00001770
1771 switch (I->getOpcode()) {
Bill Schmidtccecf262013-08-30 02:29:45 +00001772 case Instruction::Load:
1773 return SelectLoad(I);
1774 case Instruction::Store:
1775 return SelectStore(I);
Bill Schmidt03008132013-08-25 22:33:42 +00001776 case Instruction::Br:
1777 return SelectBranch(I);
1778 case Instruction::IndirectBr:
1779 return SelectIndirectBr(I);
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001780 case Instruction::FPExt:
1781 return SelectFPExt(I);
1782 case Instruction::FPTrunc:
1783 return SelectFPTrunc(I);
1784 case Instruction::SIToFP:
1785 return SelectIToFP(I, /*IsSigned*/ true);
1786 case Instruction::UIToFP:
1787 return SelectIToFP(I, /*IsSigned*/ false);
1788 case Instruction::FPToSI:
1789 return SelectFPToI(I, /*IsSigned*/ true);
1790 case Instruction::FPToUI:
1791 return SelectFPToI(I, /*IsSigned*/ false);
Bill Schmidtccecf262013-08-30 02:29:45 +00001792 case Instruction::Add:
1793 return SelectBinaryIntOp(I, ISD::ADD);
1794 case Instruction::Or:
1795 return SelectBinaryIntOp(I, ISD::OR);
1796 case Instruction::Sub:
1797 return SelectBinaryIntOp(I, ISD::SUB);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001798 case Instruction::Call:
1799 if (dyn_cast<IntrinsicInst>(I))
1800 return false;
1801 return SelectCall(I);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001802 case Instruction::Ret:
1803 return SelectRet(I);
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001804 case Instruction::Trunc:
1805 return SelectTrunc(I);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001806 case Instruction::ZExt:
1807 case Instruction::SExt:
1808 return SelectIntExt(I);
Bill Schmidt03008132013-08-25 22:33:42 +00001809 // Here add other flavors of Instruction::XXX that automated
1810 // cases don't catch. For example, switches are terminators
1811 // that aren't yet handled.
1812 default:
1813 break;
1814 }
1815 return false;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001816}
1817
1818// Materialize a floating-point constant into a register, and return
1819// the register number (or zero if we failed to handle it).
1820unsigned PPCFastISel::PPCMaterializeFP(const ConstantFP *CFP, MVT VT) {
1821 // No plans to handle long double here.
1822 if (VT != MVT::f32 && VT != MVT::f64)
1823 return 0;
1824
1825 // All FP constants are loaded from the constant pool.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001826 unsigned Align = DL.getPrefTypeAlignment(CFP->getType());
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001827 assert(Align > 0 && "Unexpectedly missing alignment information!");
1828 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
1829 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
1830 CodeModel::Model CModel = TM.getCodeModel();
1831
1832 MachineMemOperand *MMO =
1833 FuncInfo.MF->getMachineMemOperand(
1834 MachinePointerInfo::getConstantPool(), MachineMemOperand::MOLoad,
1835 (VT == MVT::f32) ? 4 : 8, Align);
1836
Bill Schmidt03008132013-08-25 22:33:42 +00001837 unsigned Opc = (VT == MVT::f32) ? PPC::LFS : PPC::LFD;
1838 unsigned TmpReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
1839
1840 // For small code model, generate a LF[SD](0, LDtocCPT(Idx, X2)).
1841 if (CModel == CodeModel::Small || CModel == CodeModel::JITDefault) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001842 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocCPT),
Bill Schmidt03008132013-08-25 22:33:42 +00001843 TmpReg)
1844 .addConstantPoolIndex(Idx).addReg(PPC::X2);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001845 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidt03008132013-08-25 22:33:42 +00001846 .addImm(0).addReg(TmpReg).addMemOperand(MMO);
1847 } else {
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001848 // Otherwise we generate LF[SD](Idx[lo], ADDIStocHA(X2, Idx)).
Rafael Espindolaea09c592014-02-18 22:05:46 +00001849 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDIStocHA),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001850 TmpReg).addReg(PPC::X2).addConstantPoolIndex(Idx);
Bill Schmidtbb381d72013-09-17 20:03:25 +00001851 // But for large code model, we must generate a LDtocL followed
1852 // by the LF[SD].
1853 if (CModel == CodeModel::Large) {
1854 unsigned TmpReg2 = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001855 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocL),
Bill Schmidtbb381d72013-09-17 20:03:25 +00001856 TmpReg2).addConstantPoolIndex(Idx).addReg(TmpReg);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001857 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidtbb381d72013-09-17 20:03:25 +00001858 .addImm(0).addReg(TmpReg2);
1859 } else
Rafael Espindolaea09c592014-02-18 22:05:46 +00001860 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidtbb381d72013-09-17 20:03:25 +00001861 .addConstantPoolIndex(Idx, 0, PPCII::MO_TOC_LO)
1862 .addReg(TmpReg)
1863 .addMemOperand(MMO);
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001864 }
1865
1866 return DestReg;
1867}
1868
Bill Schmidtccecf262013-08-30 02:29:45 +00001869// Materialize the address of a global value into a register, and return
1870// the register number (or zero if we failed to handle it).
1871unsigned PPCFastISel::PPCMaterializeGV(const GlobalValue *GV, MVT VT) {
1872 assert(VT == MVT::i64 && "Non-address!");
1873 const TargetRegisterClass *RC = &PPC::G8RC_and_G8RC_NOX0RegClass;
1874 unsigned DestReg = createResultReg(RC);
1875
1876 // Global values may be plain old object addresses, TLS object
1877 // addresses, constant pool entries, or jump tables. How we generate
1878 // code for these may depend on small, medium, or large code model.
1879 CodeModel::Model CModel = TM.getCodeModel();
1880
1881 // FIXME: Jump tables are not yet required because fast-isel doesn't
1882 // handle switches; if that changes, we need them as well. For now,
1883 // what follows assumes everything's a generic (or TLS) global address.
Bill Schmidtccecf262013-08-30 02:29:45 +00001884
1885 // FIXME: We don't yet handle the complexity of TLS.
Rafael Espindola59f7eba2014-05-28 18:15:43 +00001886 if (GV->isThreadLocal())
Bill Schmidtccecf262013-08-30 02:29:45 +00001887 return 0;
1888
1889 // For small code model, generate a simple TOC load.
1890 if (CModel == CodeModel::Small || CModel == CodeModel::JITDefault)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001891 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtoc),
1892 DestReg)
1893 .addGlobalAddress(GV)
1894 .addReg(PPC::X2);
Bill Schmidtccecf262013-08-30 02:29:45 +00001895 else {
Bill Schmidt5d82f092014-06-16 21:36:02 +00001896 // If the address is an externally defined symbol, a symbol with common
1897 // or externally available linkage, a non-local function address, or a
Bill Schmidtccecf262013-08-30 02:29:45 +00001898 // jump table address (not yet needed), or if we are generating code
1899 // for large code model, we generate:
1900 // LDtocL(GV, ADDIStocHA(%X2, GV))
1901 // Otherwise we generate:
1902 // ADDItocL(ADDIStocHA(%X2, GV), GV)
1903 // Either way, start with the ADDIStocHA:
1904 unsigned HighPartReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001905 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDIStocHA),
Bill Schmidtccecf262013-08-30 02:29:45 +00001906 HighPartReg).addReg(PPC::X2).addGlobalAddress(GV);
1907
Bill Schmidtccecf262013-08-30 02:29:45 +00001908 // If/when switches are implemented, jump tables should be handled
1909 // on the "if" path here.
Bill Schmidt5d82f092014-06-16 21:36:02 +00001910 if (CModel == CodeModel::Large ||
1911 (GV->getType()->getElementType()->isFunctionTy() &&
1912 (GV->isDeclaration() || GV->isWeakForLinker())) ||
1913 GV->isDeclaration() || GV->hasCommonLinkage() ||
1914 GV->hasAvailableExternallyLinkage())
Rafael Espindolaea09c592014-02-18 22:05:46 +00001915 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocL),
Bill Schmidtccecf262013-08-30 02:29:45 +00001916 DestReg).addGlobalAddress(GV).addReg(HighPartReg);
1917 else
1918 // Otherwise generate the ADDItocL.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001919 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDItocL),
Bill Schmidtccecf262013-08-30 02:29:45 +00001920 DestReg).addReg(HighPartReg).addGlobalAddress(GV);
1921 }
1922
1923 return DestReg;
1924}
1925
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001926// Materialize a 32-bit integer constant into a register, and return
1927// the register number (or zero if we failed to handle it).
1928unsigned PPCFastISel::PPCMaterialize32BitInt(int64_t Imm,
1929 const TargetRegisterClass *RC) {
1930 unsigned Lo = Imm & 0xFFFF;
1931 unsigned Hi = (Imm >> 16) & 0xFFFF;
1932
1933 unsigned ResultReg = createResultReg(RC);
1934 bool IsGPRC = RC->hasSuperClassEq(&PPC::GPRCRegClass);
1935
1936 if (isInt<16>(Imm))
Rafael Espindolaea09c592014-02-18 22:05:46 +00001937 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001938 TII.get(IsGPRC ? PPC::LI : PPC::LI8), ResultReg)
1939 .addImm(Imm);
1940 else if (Lo) {
1941 // Both Lo and Hi have nonzero bits.
1942 unsigned TmpReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001943 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001944 TII.get(IsGPRC ? PPC::LIS : PPC::LIS8), TmpReg)
1945 .addImm(Hi);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001946 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001947 TII.get(IsGPRC ? PPC::ORI : PPC::ORI8), ResultReg)
1948 .addReg(TmpReg).addImm(Lo);
1949 } else
1950 // Just Hi bits.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001951 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001952 TII.get(IsGPRC ? PPC::LIS : PPC::LIS8), ResultReg)
1953 .addImm(Hi);
1954
1955 return ResultReg;
1956}
1957
1958// Materialize a 64-bit integer constant into a register, and return
1959// the register number (or zero if we failed to handle it).
1960unsigned PPCFastISel::PPCMaterialize64BitInt(int64_t Imm,
1961 const TargetRegisterClass *RC) {
1962 unsigned Remainder = 0;
1963 unsigned Shift = 0;
1964
1965 // If the value doesn't fit in 32 bits, see if we can shift it
1966 // so that it fits in 32 bits.
1967 if (!isInt<32>(Imm)) {
1968 Shift = countTrailingZeros<uint64_t>(Imm);
1969 int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
1970
1971 if (isInt<32>(ImmSh))
1972 Imm = ImmSh;
1973 else {
1974 Remainder = Imm;
1975 Shift = 32;
1976 Imm >>= 32;
1977 }
1978 }
1979
1980 // Handle the high-order 32 bits (if shifted) or the whole 32 bits
1981 // (if not shifted).
1982 unsigned TmpReg1 = PPCMaterialize32BitInt(Imm, RC);
1983 if (!Shift)
1984 return TmpReg1;
1985
1986 // If upper 32 bits were not zero, we've built them and need to shift
1987 // them into place.
1988 unsigned TmpReg2;
1989 if (Imm) {
1990 TmpReg2 = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001991 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::RLDICR),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001992 TmpReg2).addReg(TmpReg1).addImm(Shift).addImm(63 - Shift);
1993 } else
1994 TmpReg2 = TmpReg1;
1995
1996 unsigned TmpReg3, Hi, Lo;
1997 if ((Hi = (Remainder >> 16) & 0xFFFF)) {
1998 TmpReg3 = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001999 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ORIS8),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002000 TmpReg3).addReg(TmpReg2).addImm(Hi);
2001 } else
2002 TmpReg3 = TmpReg2;
2003
2004 if ((Lo = Remainder & 0xFFFF)) {
2005 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002006 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ORI8),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002007 ResultReg).addReg(TmpReg3).addImm(Lo);
2008 return ResultReg;
2009 }
2010
2011 return TmpReg3;
2012}
2013
2014
2015// Materialize an integer constant into a register, and return
2016// the register number (or zero if we failed to handle it).
2017unsigned PPCFastISel::PPCMaterializeInt(const Constant *C, MVT VT) {
Hal Finkel940ab932014-02-28 00:27:01 +00002018 // If we're using CR bit registers for i1 values, handle that as a special
2019 // case first.
Eric Christopher1b8e7632014-05-22 01:07:24 +00002020 if (VT == MVT::i1 && PPCSubTarget->useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +00002021 const ConstantInt *CI = cast<ConstantInt>(C);
2022 unsigned ImmReg = createResultReg(&PPC::CRBITRCRegClass);
2023 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2024 TII.get(CI->isZero() ? PPC::CRUNSET : PPC::CRSET), ImmReg);
2025 return ImmReg;
2026 }
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002027
2028 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16 &&
2029 VT != MVT::i8 && VT != MVT::i1)
2030 return 0;
2031
2032 const TargetRegisterClass *RC = ((VT == MVT::i64) ? &PPC::G8RCRegClass :
2033 &PPC::GPRCRegClass);
2034
2035 // If the constant is in range, use a load-immediate.
2036 const ConstantInt *CI = cast<ConstantInt>(C);
2037 if (isInt<16>(CI->getSExtValue())) {
2038 unsigned Opc = (VT == MVT::i64) ? PPC::LI8 : PPC::LI;
2039 unsigned ImmReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002040 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ImmReg)
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002041 .addImm(CI->getSExtValue());
2042 return ImmReg;
2043 }
2044
2045 // Construct the constant piecewise.
2046 int64_t Imm = CI->getZExtValue();
2047
2048 if (VT == MVT::i64)
2049 return PPCMaterialize64BitInt(Imm, RC);
2050 else if (VT == MVT::i32)
2051 return PPCMaterialize32BitInt(Imm, RC);
2052
2053 return 0;
2054}
2055
2056// Materialize a constant into a register, and return the register
2057// number (or zero if we failed to handle it).
2058unsigned PPCFastISel::TargetMaterializeConstant(const Constant *C) {
2059 EVT CEVT = TLI.getValueType(C->getType(), true);
2060
2061 // Only handle simple types.
2062 if (!CEVT.isSimple()) return 0;
2063 MVT VT = CEVT.getSimpleVT();
2064
2065 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
2066 return PPCMaterializeFP(CFP, VT);
Bill Schmidtccecf262013-08-30 02:29:45 +00002067 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
2068 return PPCMaterializeGV(GV, VT);
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002069 else if (isa<ConstantInt>(C))
2070 return PPCMaterializeInt(C, VT);
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002071
2072 return 0;
2073}
2074
2075// Materialize the address created by an alloca into a register, and
Bill Schmidteb8d6f72013-08-31 02:33:40 +00002076// return the register number (or zero if we failed to handle it).
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002077unsigned PPCFastISel::TargetMaterializeAlloca(const AllocaInst *AI) {
Bill Schmidteb8d6f72013-08-31 02:33:40 +00002078 // Don't handle dynamic allocas.
2079 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
2080
2081 MVT VT;
2082 if (!isLoadTypeLegal(AI->getType(), VT)) return 0;
2083
2084 DenseMap<const AllocaInst*, int>::iterator SI =
2085 FuncInfo.StaticAllocaMap.find(AI);
2086
2087 if (SI != FuncInfo.StaticAllocaMap.end()) {
2088 unsigned ResultReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002089 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDI8),
Bill Schmidteb8d6f72013-08-31 02:33:40 +00002090 ResultReg).addFrameIndex(SI->second).addImm(0);
2091 return ResultReg;
2092 }
2093
2094 return 0;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002095}
2096
Bill Schmidtccecf262013-08-30 02:29:45 +00002097// Fold loads into extends when possible.
2098// FIXME: We can have multiple redundant extend/trunc instructions
2099// following a load. The folding only picks up one. Extend this
2100// to check subsequent instructions for the same pattern and remove
2101// them. Thus ResultReg should be the def reg for the last redundant
2102// instruction in a chain, and all intervening instructions can be
2103// removed from parent. Change test/CodeGen/PowerPC/fast-isel-fold.ll
2104// to add ELF64-NOT: rldicl to the appropriate tests when this works.
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002105bool PPCFastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
2106 const LoadInst *LI) {
Bill Schmidtccecf262013-08-30 02:29:45 +00002107 // Verify we have a legal type before going any further.
2108 MVT VT;
2109 if (!isLoadTypeLegal(LI->getType(), VT))
2110 return false;
2111
2112 // Combine load followed by zero- or sign-extend.
2113 bool IsZExt = false;
2114 switch(MI->getOpcode()) {
2115 default:
2116 return false;
2117
2118 case PPC::RLDICL:
2119 case PPC::RLDICL_32_64: {
2120 IsZExt = true;
2121 unsigned MB = MI->getOperand(3).getImm();
2122 if ((VT == MVT::i8 && MB <= 56) ||
2123 (VT == MVT::i16 && MB <= 48) ||
2124 (VT == MVT::i32 && MB <= 32))
2125 break;
2126 return false;
2127 }
2128
2129 case PPC::RLWINM:
2130 case PPC::RLWINM8: {
2131 IsZExt = true;
2132 unsigned MB = MI->getOperand(3).getImm();
2133 if ((VT == MVT::i8 && MB <= 24) ||
2134 (VT == MVT::i16 && MB <= 16))
2135 break;
2136 return false;
2137 }
2138
2139 case PPC::EXTSB:
2140 case PPC::EXTSB8:
2141 case PPC::EXTSB8_32_64:
2142 /* There is no sign-extending load-byte instruction. */
2143 return false;
2144
2145 case PPC::EXTSH:
2146 case PPC::EXTSH8:
2147 case PPC::EXTSH8_32_64: {
2148 if (VT != MVT::i16 && VT != MVT::i8)
2149 return false;
2150 break;
2151 }
2152
2153 case PPC::EXTSW:
2154 case PPC::EXTSW_32_64: {
2155 if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8)
2156 return false;
2157 break;
2158 }
2159 }
2160
2161 // See if we can handle this address.
2162 Address Addr;
2163 if (!PPCComputeAddress(LI->getOperand(0), Addr))
2164 return false;
2165
2166 unsigned ResultReg = MI->getOperand(0).getReg();
2167
Craig Topper062a2ba2014-04-25 05:30:21 +00002168 if (!PPCEmitLoad(VT, ResultReg, Addr, nullptr, IsZExt))
Bill Schmidtccecf262013-08-30 02:29:45 +00002169 return false;
2170
2171 MI->eraseFromParent();
2172 return true;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002173}
2174
2175// Attempt to lower call arguments in a faster way than done by
2176// the selection DAG code.
2177bool PPCFastISel::FastLowerArguments() {
2178 // Defer to normal argument lowering for now. It's reasonably
2179 // efficient. Consider doing something like ARM to handle the
2180 // case where all args fit in registers, no varargs, no float
2181 // or vector args.
2182 return false;
2183}
2184
Bill Schmidt03008132013-08-25 22:33:42 +00002185// Handle materializing integer constants into a register. This is not
2186// automatically generated for PowerPC, so must be explicitly created here.
2187unsigned PPCFastISel::FastEmit_i(MVT Ty, MVT VT, unsigned Opc, uint64_t Imm) {
2188
2189 if (Opc != ISD::Constant)
2190 return 0;
2191
Hal Finkel940ab932014-02-28 00:27:01 +00002192 // If we're using CR bit registers for i1 values, handle that as a special
2193 // case first.
Eric Christopher1b8e7632014-05-22 01:07:24 +00002194 if (VT == MVT::i1 && PPCSubTarget->useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +00002195 unsigned ImmReg = createResultReg(&PPC::CRBITRCRegClass);
2196 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2197 TII.get(Imm == 0 ? PPC::CRUNSET : PPC::CRSET), ImmReg);
2198 return ImmReg;
2199 }
2200
Bill Schmidt03008132013-08-25 22:33:42 +00002201 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16 &&
2202 VT != MVT::i8 && VT != MVT::i1)
2203 return 0;
2204
2205 const TargetRegisterClass *RC = ((VT == MVT::i64) ? &PPC::G8RCRegClass :
2206 &PPC::GPRCRegClass);
2207 if (VT == MVT::i64)
2208 return PPCMaterialize64BitInt(Imm, RC);
2209 else
2210 return PPCMaterialize32BitInt(Imm, RC);
2211}
2212
Bill Schmidtccecf262013-08-30 02:29:45 +00002213// Override for ADDI and ADDI8 to set the correct register class
2214// on RHS operand 0. The automatic infrastructure naively assumes
2215// GPRC for i32 and G8RC for i64; the concept of "no R0" is lost
2216// for these cases. At the moment, none of the other automatically
2217// generated RI instructions require special treatment. However, once
2218// SelectSelect is implemented, "isel" requires similar handling.
2219//
2220// Also be conservative about the output register class. Avoid
2221// assigning R0 or X0 to the output register for GPRC and G8RC
2222// register classes, as any such result could be used in ADDI, etc.,
2223// where those regs have another meaning.
2224unsigned PPCFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
2225 const TargetRegisterClass *RC,
2226 unsigned Op0, bool Op0IsKill,
2227 uint64_t Imm) {
2228 if (MachineInstOpcode == PPC::ADDI)
2229 MRI.setRegClass(Op0, &PPC::GPRC_and_GPRC_NOR0RegClass);
2230 else if (MachineInstOpcode == PPC::ADDI8)
2231 MRI.setRegClass(Op0, &PPC::G8RC_and_G8RC_NOX0RegClass);
2232
2233 const TargetRegisterClass *UseRC =
2234 (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
2235 (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
2236
2237 return FastISel::FastEmitInst_ri(MachineInstOpcode, UseRC,
2238 Op0, Op0IsKill, Imm);
2239}
2240
2241// Override for instructions with one register operand to avoid use of
2242// R0/X0. The automatic infrastructure isn't aware of the context so
2243// we must be conservative.
2244unsigned PPCFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
2245 const TargetRegisterClass* RC,
2246 unsigned Op0, bool Op0IsKill) {
2247 const TargetRegisterClass *UseRC =
2248 (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
2249 (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
2250
2251 return FastISel::FastEmitInst_r(MachineInstOpcode, UseRC, Op0, Op0IsKill);
2252}
2253
2254// Override for instructions with two register operands to avoid use
2255// of R0/X0. The automatic infrastructure isn't aware of the context
2256// so we must be conservative.
2257unsigned PPCFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
2258 const TargetRegisterClass* RC,
2259 unsigned Op0, bool Op0IsKill,
2260 unsigned Op1, bool Op1IsKill) {
2261 const TargetRegisterClass *UseRC =
2262 (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
2263 (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
2264
2265 return FastISel::FastEmitInst_rr(MachineInstOpcode, UseRC, Op0, Op0IsKill,
2266 Op1, Op1IsKill);
2267}
2268
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002269namespace llvm {
2270 // Create the fast instruction selector for PowerPC64 ELF.
2271 FastISel *PPC::createFastISel(FunctionLoweringInfo &FuncInfo,
2272 const TargetLibraryInfo *LibInfo) {
2273 const TargetMachine &TM = FuncInfo.MF->getTarget();
2274
2275 // Only available on 64-bit ELF for now.
2276 const PPCSubtarget *Subtarget = &TM.getSubtarget<PPCSubtarget>();
2277 if (Subtarget->isPPC64() && Subtarget->isSVR4ABI())
2278 return new PPCFastISel(FuncInfo, LibInfo);
2279
Craig Topper062a2ba2014-04-25 05:30:21 +00002280 return nullptr;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002281 }
2282}