1 //===-- PPCFastISel.cpp - PowerPC FastISel implementation -----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
16 #define DEBUG_TYPE "ppcfastisel"
18 #include "PPCISelLowering.h"
19 #include "PPCSubtarget.h"
20 #include "PPCTargetMachine.h"
21 #include "MCTargetDesc/PPCPredicates.h"
22 #include "llvm/ADT/Optional.h"
23 #include "llvm/CodeGen/CallingConvLower.h"
24 #include "llvm/CodeGen/FastISel.h"
25 #include "llvm/CodeGen/FunctionLoweringInfo.h"
26 #include "llvm/CodeGen/MachineConstantPool.h"
27 #include "llvm/CodeGen/MachineFrameInfo.h"
28 #include "llvm/CodeGen/MachineInstrBuilder.h"
29 #include "llvm/CodeGen/MachineRegisterInfo.h"
30 #include "llvm/IR/CallingConv.h"
31 #include "llvm/IR/GlobalAlias.h"
32 #include "llvm/IR/GlobalVariable.h"
33 #include "llvm/IR/IntrinsicInst.h"
34 #include "llvm/IR/Operator.h"
35 #include "llvm/Support/Debug.h"
36 #include "llvm/Support/GetElementPtrTypeIterator.h"
37 #include "llvm/Target/TargetLowering.h"
38 #include "llvm/Target/TargetMachine.h"
44 typedef struct Address {
57 // Innocuous defaults for our address.
59 : BaseType(RegBase), Offset(0) {
64 class PPCFastISel : public FastISel {
66 const TargetMachine &TM;
67 const TargetInstrInfo &TII;
68 const TargetLowering &TLI;
69 const PPCSubtarget &PPCSubTarget;
73 explicit PPCFastISel(FunctionLoweringInfo &FuncInfo,
74 const TargetLibraryInfo *LibInfo)
75 : FastISel(FuncInfo, LibInfo),
76 TM(FuncInfo.MF->getTarget()),
77 TII(*TM.getInstrInfo()),
78 TLI(*TM.getTargetLowering()),
80 *((static_cast<const PPCTargetMachine *>(&TM))->getSubtargetImpl())
82 Context(&FuncInfo.Fn->getContext()) { }
84 // Backend specific FastISel code.
86 virtual bool TargetSelectInstruction(const Instruction *I);
87 virtual unsigned TargetMaterializeConstant(const Constant *C);
88 virtual unsigned TargetMaterializeAlloca(const AllocaInst *AI);
89 virtual bool tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
91 virtual bool FastLowerArguments();
92 virtual unsigned FastEmit_i(MVT Ty, MVT RetTy, unsigned Opc, uint64_t Imm);
94 // Instruction selection routines.
96 bool SelectBranch(const Instruction *I);
97 bool SelectIndirectBr(const Instruction *I);
101 bool PPCEmitCmp(const Value *Src1Value, const Value *Src2Value,
102 bool isZExt, unsigned DestReg);
103 bool PPCEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
104 unsigned DestReg, bool IsZExt);
105 unsigned PPCMaterializeFP(const ConstantFP *CFP, MVT VT);
106 unsigned PPCMaterializeInt(const Constant *C, MVT VT);
107 unsigned PPCMaterialize32BitInt(int64_t Imm,
108 const TargetRegisterClass *RC);
109 unsigned PPCMaterialize64BitInt(int64_t Imm,
110 const TargetRegisterClass *RC);
113 #include "PPCGenFastISel.inc"
117 } // end anonymous namespace
119 static Optional<PPC::Predicate> getComparePred(CmpInst::Predicate Pred) {
121 // These are not representable with any single compare.
122 case CmpInst::FCMP_FALSE:
123 case CmpInst::FCMP_UEQ:
124 case CmpInst::FCMP_UGT:
125 case CmpInst::FCMP_UGE:
126 case CmpInst::FCMP_ULT:
127 case CmpInst::FCMP_ULE:
128 case CmpInst::FCMP_UNE:
129 case CmpInst::FCMP_TRUE:
131 return Optional<PPC::Predicate>();
133 case CmpInst::FCMP_OEQ:
134 case CmpInst::ICMP_EQ:
137 case CmpInst::FCMP_OGT:
138 case CmpInst::ICMP_UGT:
139 case CmpInst::ICMP_SGT:
142 case CmpInst::FCMP_OGE:
143 case CmpInst::ICMP_UGE:
144 case CmpInst::ICMP_SGE:
147 case CmpInst::FCMP_OLT:
148 case CmpInst::ICMP_ULT:
149 case CmpInst::ICMP_SLT:
152 case CmpInst::FCMP_OLE:
153 case CmpInst::ICMP_ULE:
154 case CmpInst::ICMP_SLE:
157 case CmpInst::FCMP_ONE:
158 case CmpInst::ICMP_NE:
161 case CmpInst::FCMP_ORD:
164 case CmpInst::FCMP_UNO:
169 // Attempt to fast-select a branch instruction.
170 bool PPCFastISel::SelectBranch(const Instruction *I) {
171 const BranchInst *BI = cast<BranchInst>(I);
172 MachineBasicBlock *BrBB = FuncInfo.MBB;
173 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
174 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
176 // For now, just try the simplest case where it's fed by a compare.
177 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
178 Optional<PPC::Predicate> OptPPCPred = getComparePred(CI->getPredicate());
182 PPC::Predicate PPCPred = OptPPCPred.getValue();
184 // Take advantage of fall-through opportunities.
185 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
187 PPCPred = PPC::InvertPredicate(PPCPred);
190 unsigned CondReg = createResultReg(&PPC::CRRCRegClass);
192 if (!PPCEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned(),
196 BuildMI(*BrBB, FuncInfo.InsertPt, DL, TII.get(PPC::BCC))
197 .addImm(PPCPred).addReg(CondReg).addMBB(TBB);
198 FastEmitBranch(FBB, DL);
199 FuncInfo.MBB->addSuccessor(TBB);
202 } else if (const ConstantInt *CI =
203 dyn_cast<ConstantInt>(BI->getCondition())) {
204 uint64_t Imm = CI->getZExtValue();
205 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
206 FastEmitBranch(Target, DL);
210 // FIXME: ARM looks for a case where the block containing the compare
211 // has been split from the block containing the branch. If this happens,
212 // there is a vreg available containing the result of the compare. I'm
213 // not sure we can do much, as we've lost the predicate information with
214 // the compare instruction -- we have a 4-bit CR but don't know which bit
219 // Attempt to emit a compare of the two source values. Signed and unsigned
220 // comparisons are supported. Return false if we can't handle it.
221 bool PPCFastISel::PPCEmitCmp(const Value *SrcValue1, const Value *SrcValue2,
222 bool IsZExt, unsigned DestReg) {
223 Type *Ty = SrcValue1->getType();
224 EVT SrcEVT = TLI.getValueType(Ty, true);
225 if (!SrcEVT.isSimple())
227 MVT SrcVT = SrcEVT.getSimpleVT();
229 // See if operand 2 is an immediate encodeable in the compare.
230 // FIXME: Operands are not in canonical order at -O0, so an immediate
231 // operand in position 1 is a lost opportunity for now. We are
232 // similar to ARM in this regard.
236 // Only 16-bit integer constants can be represented in compares for
237 // PowerPC. Others will be materialized into a register.
238 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(SrcValue2)) {
239 if (SrcVT == MVT::i64 || SrcVT == MVT::i32 || SrcVT == MVT::i16 ||
240 SrcVT == MVT::i8 || SrcVT == MVT::i1) {
241 const APInt &CIVal = ConstInt->getValue();
242 Imm = (IsZExt) ? (long)CIVal.getZExtValue() : (long)CIVal.getSExtValue();
243 if ((IsZExt && isUInt<16>(Imm)) || (!IsZExt && isInt<16>(Imm)))
249 bool NeedsExt = false;
250 switch (SrcVT.SimpleTy) {
251 default: return false;
253 CmpOpc = PPC::FCMPUS;
256 CmpOpc = PPC::FCMPUD;
262 // Intentional fall-through.
265 CmpOpc = IsZExt ? PPC::CMPLW : PPC::CMPW;
267 CmpOpc = IsZExt ? PPC::CMPLWI : PPC::CMPWI;
271 CmpOpc = IsZExt ? PPC::CMPLD : PPC::CMPD;
273 CmpOpc = IsZExt ? PPC::CMPLDI : PPC::CMPDI;
277 unsigned SrcReg1 = getRegForValue(SrcValue1);
281 unsigned SrcReg2 = 0;
283 SrcReg2 = getRegForValue(SrcValue2);
289 unsigned ExtReg = createResultReg(&PPC::GPRCRegClass);
290 if (!PPCEmitIntExt(SrcVT, SrcReg1, MVT::i32, ExtReg, IsZExt))
295 unsigned ExtReg = createResultReg(&PPC::GPRCRegClass);
296 if (!PPCEmitIntExt(SrcVT, SrcReg2, MVT::i32, ExtReg, IsZExt))
303 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc), DestReg)
304 .addReg(SrcReg1).addReg(SrcReg2);
306 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc), DestReg)
307 .addReg(SrcReg1).addImm(Imm);
312 // Attempt to emit an integer extend of SrcReg into DestReg. Both
313 // signed and zero extensions are supported. Return false if we
314 // can't handle it. Not yet implemented.
315 bool PPCFastISel::PPCEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
316 unsigned DestReg, bool IsZExt) {
317 return (SrcVT == MVT::i8 && SrcReg && DestVT == MVT::i8 && DestReg
321 // Attempt to fast-select an indirect branch instruction.
322 bool PPCFastISel::SelectIndirectBr(const Instruction *I) {
323 unsigned AddrReg = getRegForValue(I->getOperand(0));
327 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(PPC::MTCTR8))
329 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(PPC::BCTR8));
331 const IndirectBrInst *IB = cast<IndirectBrInst>(I);
332 for (unsigned i = 0, e = IB->getNumSuccessors(); i != e; ++i)
333 FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[IB->getSuccessor(i)]);
338 // Attempt to fast-select an instruction that wasn't handled by
339 // the table-generated machinery.
340 bool PPCFastISel::TargetSelectInstruction(const Instruction *I) {
342 switch (I->getOpcode()) {
343 case Instruction::Br:
344 return SelectBranch(I);
345 case Instruction::IndirectBr:
346 return SelectIndirectBr(I);
347 // Here add other flavors of Instruction::XXX that automated
348 // cases don't catch. For example, switches are terminators
349 // that aren't yet handled.
356 // Materialize a floating-point constant into a register, and return
357 // the register number (or zero if we failed to handle it).
358 unsigned PPCFastISel::PPCMaterializeFP(const ConstantFP *CFP, MVT VT) {
359 // No plans to handle long double here.
360 if (VT != MVT::f32 && VT != MVT::f64)
363 // All FP constants are loaded from the constant pool.
364 unsigned Align = TD.getPrefTypeAlignment(CFP->getType());
365 assert(Align > 0 && "Unexpectedly missing alignment information!");
366 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
367 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
368 CodeModel::Model CModel = TM.getCodeModel();
370 MachineMemOperand *MMO =
371 FuncInfo.MF->getMachineMemOperand(
372 MachinePointerInfo::getConstantPool(), MachineMemOperand::MOLoad,
373 (VT == MVT::f32) ? 4 : 8, Align);
375 unsigned Opc = (VT == MVT::f32) ? PPC::LFS : PPC::LFD;
376 unsigned TmpReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
378 // For small code model, generate a LF[SD](0, LDtocCPT(Idx, X2)).
379 if (CModel == CodeModel::Small || CModel == CodeModel::JITDefault) {
380 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(PPC::LDtocCPT),
382 .addConstantPoolIndex(Idx).addReg(PPC::X2);
383 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), DestReg)
384 .addImm(0).addReg(TmpReg).addMemOperand(MMO);
386 // Otherwise we generate LF[SD](Idx[lo], ADDIStocHA(X2, Idx)).
387 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(PPC::ADDIStocHA),
388 TmpReg).addReg(PPC::X2).addConstantPoolIndex(Idx);
389 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), DestReg)
390 .addConstantPoolIndex(Idx, 0, PPCII::MO_TOC_LO)
398 // Materialize a 32-bit integer constant into a register, and return
399 // the register number (or zero if we failed to handle it).
400 unsigned PPCFastISel::PPCMaterialize32BitInt(int64_t Imm,
401 const TargetRegisterClass *RC) {
402 unsigned Lo = Imm & 0xFFFF;
403 unsigned Hi = (Imm >> 16) & 0xFFFF;
405 unsigned ResultReg = createResultReg(RC);
406 bool IsGPRC = RC->hasSuperClassEq(&PPC::GPRCRegClass);
409 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
410 TII.get(IsGPRC ? PPC::LI : PPC::LI8), ResultReg)
413 // Both Lo and Hi have nonzero bits.
414 unsigned TmpReg = createResultReg(RC);
415 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
416 TII.get(IsGPRC ? PPC::LIS : PPC::LIS8), TmpReg)
418 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
419 TII.get(IsGPRC ? PPC::ORI : PPC::ORI8), ResultReg)
420 .addReg(TmpReg).addImm(Lo);
423 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
424 TII.get(IsGPRC ? PPC::LIS : PPC::LIS8), ResultReg)
430 // Materialize a 64-bit integer constant into a register, and return
431 // the register number (or zero if we failed to handle it).
432 unsigned PPCFastISel::PPCMaterialize64BitInt(int64_t Imm,
433 const TargetRegisterClass *RC) {
434 unsigned Remainder = 0;
437 // If the value doesn't fit in 32 bits, see if we can shift it
438 // so that it fits in 32 bits.
439 if (!isInt<32>(Imm)) {
440 Shift = countTrailingZeros<uint64_t>(Imm);
441 int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
443 if (isInt<32>(ImmSh))
452 // Handle the high-order 32 bits (if shifted) or the whole 32 bits
454 unsigned TmpReg1 = PPCMaterialize32BitInt(Imm, RC);
458 // If upper 32 bits were not zero, we've built them and need to shift
462 TmpReg2 = createResultReg(RC);
463 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(PPC::RLDICR),
464 TmpReg2).addReg(TmpReg1).addImm(Shift).addImm(63 - Shift);
468 unsigned TmpReg3, Hi, Lo;
469 if ((Hi = (Remainder >> 16) & 0xFFFF)) {
470 TmpReg3 = createResultReg(RC);
471 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(PPC::ORIS8),
472 TmpReg3).addReg(TmpReg2).addImm(Hi);
476 if ((Lo = Remainder & 0xFFFF)) {
477 unsigned ResultReg = createResultReg(RC);
478 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(PPC::ORI8),
479 ResultReg).addReg(TmpReg3).addImm(Lo);
487 // Materialize an integer constant into a register, and return
488 // the register number (or zero if we failed to handle it).
489 unsigned PPCFastISel::PPCMaterializeInt(const Constant *C, MVT VT) {
491 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16 &&
492 VT != MVT::i8 && VT != MVT::i1)
495 const TargetRegisterClass *RC = ((VT == MVT::i64) ? &PPC::G8RCRegClass :
498 // If the constant is in range, use a load-immediate.
499 const ConstantInt *CI = cast<ConstantInt>(C);
500 if (isInt<16>(CI->getSExtValue())) {
501 unsigned Opc = (VT == MVT::i64) ? PPC::LI8 : PPC::LI;
502 unsigned ImmReg = createResultReg(RC);
503 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ImmReg)
504 .addImm(CI->getSExtValue());
508 // Construct the constant piecewise.
509 int64_t Imm = CI->getZExtValue();
512 return PPCMaterialize64BitInt(Imm, RC);
513 else if (VT == MVT::i32)
514 return PPCMaterialize32BitInt(Imm, RC);
519 // Materialize a constant into a register, and return the register
520 // number (or zero if we failed to handle it).
521 unsigned PPCFastISel::TargetMaterializeConstant(const Constant *C) {
522 EVT CEVT = TLI.getValueType(C->getType(), true);
524 // Only handle simple types.
525 if (!CEVT.isSimple()) return 0;
526 MVT VT = CEVT.getSimpleVT();
528 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
529 return PPCMaterializeFP(CFP, VT);
530 else if (isa<ConstantInt>(C))
531 return PPCMaterializeInt(C, VT);
532 // TBD: Global values.
537 // Materialize the address created by an alloca into a register, and
538 // return the register number (or zero if we failed to handle it). TBD.
539 unsigned PPCFastISel::TargetMaterializeAlloca(const AllocaInst *AI) {
543 // Fold loads into extends when possible. TBD.
544 bool PPCFastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
545 const LoadInst *LI) {
546 return MI && OpNo && LI && false;
549 // Attempt to lower call arguments in a faster way than done by
550 // the selection DAG code.
551 bool PPCFastISel::FastLowerArguments() {
552 // Defer to normal argument lowering for now. It's reasonably
553 // efficient. Consider doing something like ARM to handle the
554 // case where all args fit in registers, no varargs, no float
559 // Handle materializing integer constants into a register. This is not
560 // automatically generated for PowerPC, so must be explicitly created here.
561 unsigned PPCFastISel::FastEmit_i(MVT Ty, MVT VT, unsigned Opc, uint64_t Imm) {
563 if (Opc != ISD::Constant)
566 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16 &&
567 VT != MVT::i8 && VT != MVT::i1)
570 const TargetRegisterClass *RC = ((VT == MVT::i64) ? &PPC::G8RCRegClass :
573 return PPCMaterialize64BitInt(Imm, RC);
575 return PPCMaterialize32BitInt(Imm, RC);
579 // Create the fast instruction selector for PowerPC64 ELF.
580 FastISel *PPC::createFastISel(FunctionLoweringInfo &FuncInfo,
581 const TargetLibraryInfo *LibInfo) {
582 const TargetMachine &TM = FuncInfo.MF->getTarget();
584 // Only available on 64-bit ELF for now.
585 const PPCSubtarget *Subtarget = &TM.getSubtarget<PPCSubtarget>();
586 if (Subtarget->isPPC64() && Subtarget->isSVR4ABI())
587 return new PPCFastISel(FuncInfo, LibInfo);