Bill Schmidt | 646cd79 | 2013-07-30 00:50:39 +0000 | [diff] [blame] | 1 | //===-- PPCFastISel.cpp - PowerPC FastISel implementation -----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the PowerPC-specific support for the FastISel class. Some |
| 11 | // of the target-specific code is generated by tablegen in the file |
| 12 | // PPCGenFastISel.inc, which is #included here. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #define DEBUG_TYPE "ppcfastisel" |
| 17 | #include "PPC.h" |
| 18 | #include "PPCISelLowering.h" |
| 19 | #include "PPCSubtarget.h" |
| 20 | #include "PPCTargetMachine.h" |
| 21 | #include "MCTargetDesc/PPCPredicates.h" |
| 22 | #include "llvm/ADT/Optional.h" |
| 23 | #include "llvm/CodeGen/CallingConvLower.h" |
| 24 | #include "llvm/CodeGen/FastISel.h" |
| 25 | #include "llvm/CodeGen/FunctionLoweringInfo.h" |
| 26 | #include "llvm/CodeGen/MachineConstantPool.h" |
| 27 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 28 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 29 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 30 | #include "llvm/IR/CallingConv.h" |
| 31 | #include "llvm/IR/GlobalAlias.h" |
| 32 | #include "llvm/IR/GlobalVariable.h" |
| 33 | #include "llvm/IR/IntrinsicInst.h" |
| 34 | #include "llvm/IR/Operator.h" |
| 35 | #include "llvm/Support/Debug.h" |
| 36 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
| 37 | #include "llvm/Target/TargetLowering.h" |
| 38 | #include "llvm/Target/TargetMachine.h" |
| 39 | |
| 40 | using namespace llvm; |
| 41 | |
| 42 | namespace { |
| 43 | |
| 44 | typedef struct Address { |
| 45 | enum { |
| 46 | RegBase, |
| 47 | FrameIndexBase |
| 48 | } BaseType; |
| 49 | |
| 50 | union { |
| 51 | unsigned Reg; |
| 52 | int FI; |
| 53 | } Base; |
| 54 | |
| 55 | int Offset; |
| 56 | |
| 57 | // Innocuous defaults for our address. |
| 58 | Address() |
| 59 | : BaseType(RegBase), Offset(0) { |
| 60 | Base.Reg = 0; |
| 61 | } |
| 62 | } Address; |
| 63 | |
| 64 | class PPCFastISel : public FastISel { |
| 65 | |
| 66 | const TargetMachine &TM; |
| 67 | const TargetInstrInfo &TII; |
| 68 | const TargetLowering &TLI; |
| 69 | const PPCSubtarget &PPCSubTarget; |
| 70 | LLVMContext *Context; |
| 71 | |
| 72 | public: |
| 73 | explicit PPCFastISel(FunctionLoweringInfo &FuncInfo, |
| 74 | const TargetLibraryInfo *LibInfo) |
| 75 | : FastISel(FuncInfo, LibInfo), |
| 76 | TM(FuncInfo.MF->getTarget()), |
| 77 | TII(*TM.getInstrInfo()), |
| 78 | TLI(*TM.getTargetLowering()), |
| 79 | PPCSubTarget( |
| 80 | *((static_cast<const PPCTargetMachine *>(&TM))->getSubtargetImpl()) |
| 81 | ), |
| 82 | Context(&FuncInfo.Fn->getContext()) { } |
| 83 | |
| 84 | // Backend specific FastISel code. |
| 85 | private: |
| 86 | virtual bool TargetSelectInstruction(const Instruction *I); |
| 87 | virtual unsigned TargetMaterializeConstant(const Constant *C); |
| 88 | virtual unsigned TargetMaterializeAlloca(const AllocaInst *AI); |
| 89 | virtual bool tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo, |
| 90 | const LoadInst *LI); |
| 91 | virtual bool FastLowerArguments(); |
Bill Schmidt | 3fad2bc | 2013-08-25 22:33:42 +0000 | [diff] [blame^] | 92 | virtual unsigned FastEmit_i(MVT Ty, MVT RetTy, unsigned Opc, uint64_t Imm); |
| 93 | |
| 94 | // Instruction selection routines. |
| 95 | private: |
| 96 | bool SelectBranch(const Instruction *I); |
| 97 | bool SelectIndirectBr(const Instruction *I); |
Bill Schmidt | 646cd79 | 2013-07-30 00:50:39 +0000 | [diff] [blame] | 98 | |
| 99 | // Utility routines. |
| 100 | private: |
Bill Schmidt | 3fad2bc | 2013-08-25 22:33:42 +0000 | [diff] [blame^] | 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); |
Bill Schmidt | 646cd79 | 2013-07-30 00:50:39 +0000 | [diff] [blame] | 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); |
| 111 | |
| 112 | private: |
| 113 | #include "PPCGenFastISel.inc" |
| 114 | |
| 115 | }; |
| 116 | |
| 117 | } // end anonymous namespace |
| 118 | |
Bill Schmidt | 3fad2bc | 2013-08-25 22:33:42 +0000 | [diff] [blame^] | 119 | static Optional<PPC::Predicate> getComparePred(CmpInst::Predicate Pred) { |
| 120 | switch (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: |
| 130 | default: |
| 131 | return Optional<PPC::Predicate>(); |
| 132 | |
| 133 | case CmpInst::FCMP_OEQ: |
| 134 | case CmpInst::ICMP_EQ: |
| 135 | return PPC::PRED_EQ; |
| 136 | |
| 137 | case CmpInst::FCMP_OGT: |
| 138 | case CmpInst::ICMP_UGT: |
| 139 | case CmpInst::ICMP_SGT: |
| 140 | return PPC::PRED_GT; |
| 141 | |
| 142 | case CmpInst::FCMP_OGE: |
| 143 | case CmpInst::ICMP_UGE: |
| 144 | case CmpInst::ICMP_SGE: |
| 145 | return PPC::PRED_GE; |
| 146 | |
| 147 | case CmpInst::FCMP_OLT: |
| 148 | case CmpInst::ICMP_ULT: |
| 149 | case CmpInst::ICMP_SLT: |
| 150 | return PPC::PRED_LT; |
| 151 | |
| 152 | case CmpInst::FCMP_OLE: |
| 153 | case CmpInst::ICMP_ULE: |
| 154 | case CmpInst::ICMP_SLE: |
| 155 | return PPC::PRED_LE; |
| 156 | |
| 157 | case CmpInst::FCMP_ONE: |
| 158 | case CmpInst::ICMP_NE: |
| 159 | return PPC::PRED_NE; |
| 160 | |
| 161 | case CmpInst::FCMP_ORD: |
| 162 | return PPC::PRED_NU; |
| 163 | |
| 164 | case CmpInst::FCMP_UNO: |
| 165 | return PPC::PRED_UN; |
| 166 | } |
| 167 | } |
| 168 | |
| 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)]; |
| 175 | |
| 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()); |
| 179 | if (!OptPPCPred) |
| 180 | return false; |
| 181 | |
| 182 | PPC::Predicate PPCPred = OptPPCPred.getValue(); |
| 183 | |
| 184 | // Take advantage of fall-through opportunities. |
| 185 | if (FuncInfo.MBB->isLayoutSuccessor(TBB)) { |
| 186 | std::swap(TBB, FBB); |
| 187 | PPCPred = PPC::InvertPredicate(PPCPred); |
| 188 | } |
| 189 | |
| 190 | unsigned CondReg = createResultReg(&PPC::CRRCRegClass); |
| 191 | |
| 192 | if (!PPCEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned(), |
| 193 | CondReg)) |
| 194 | return false; |
| 195 | |
| 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); |
| 200 | return true; |
| 201 | |
| 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); |
| 207 | return true; |
| 208 | } |
| 209 | |
| 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 |
| 215 | // to test here. |
| 216 | return false; |
| 217 | } |
| 218 | |
| 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()) |
| 226 | return false; |
| 227 | MVT SrcVT = SrcEVT.getSimpleVT(); |
| 228 | |
| 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. |
| 233 | long Imm = 0; |
| 234 | bool UseImm = false; |
| 235 | |
| 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))) |
| 244 | UseImm = true; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | unsigned CmpOpc; |
| 249 | bool NeedsExt = false; |
| 250 | switch (SrcVT.SimpleTy) { |
| 251 | default: return false; |
| 252 | case MVT::f32: |
| 253 | CmpOpc = PPC::FCMPUS; |
| 254 | break; |
| 255 | case MVT::f64: |
| 256 | CmpOpc = PPC::FCMPUD; |
| 257 | break; |
| 258 | case MVT::i1: |
| 259 | case MVT::i8: |
| 260 | case MVT::i16: |
| 261 | NeedsExt = true; |
| 262 | // Intentional fall-through. |
| 263 | case MVT::i32: |
| 264 | if (!UseImm) |
| 265 | CmpOpc = IsZExt ? PPC::CMPLW : PPC::CMPW; |
| 266 | else |
| 267 | CmpOpc = IsZExt ? PPC::CMPLWI : PPC::CMPWI; |
| 268 | break; |
| 269 | case MVT::i64: |
| 270 | if (!UseImm) |
| 271 | CmpOpc = IsZExt ? PPC::CMPLD : PPC::CMPD; |
| 272 | else |
| 273 | CmpOpc = IsZExt ? PPC::CMPLDI : PPC::CMPDI; |
| 274 | break; |
| 275 | } |
| 276 | |
| 277 | unsigned SrcReg1 = getRegForValue(SrcValue1); |
| 278 | if (SrcReg1 == 0) |
| 279 | return false; |
| 280 | |
| 281 | unsigned SrcReg2 = 0; |
| 282 | if (!UseImm) { |
| 283 | SrcReg2 = getRegForValue(SrcValue2); |
| 284 | if (SrcReg2 == 0) |
| 285 | return false; |
| 286 | } |
| 287 | |
| 288 | if (NeedsExt) { |
| 289 | unsigned ExtReg = createResultReg(&PPC::GPRCRegClass); |
| 290 | if (!PPCEmitIntExt(SrcVT, SrcReg1, MVT::i32, ExtReg, IsZExt)) |
| 291 | return false; |
| 292 | SrcReg1 = ExtReg; |
| 293 | |
| 294 | if (!UseImm) { |
| 295 | unsigned ExtReg = createResultReg(&PPC::GPRCRegClass); |
| 296 | if (!PPCEmitIntExt(SrcVT, SrcReg2, MVT::i32, ExtReg, IsZExt)) |
| 297 | return false; |
| 298 | SrcReg2 = ExtReg; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | if (!UseImm) |
| 303 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc), DestReg) |
| 304 | .addReg(SrcReg1).addReg(SrcReg2); |
| 305 | else |
| 306 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc), DestReg) |
| 307 | .addReg(SrcReg1).addImm(Imm); |
| 308 | |
| 309 | return true; |
| 310 | } |
| 311 | |
| 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 |
| 318 | && IsZExt && false); |
| 319 | } |
| 320 | |
| 321 | // Attempt to fast-select an indirect branch instruction. |
| 322 | bool PPCFastISel::SelectIndirectBr(const Instruction *I) { |
| 323 | unsigned AddrReg = getRegForValue(I->getOperand(0)); |
| 324 | if (AddrReg == 0) |
| 325 | return false; |
| 326 | |
| 327 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(PPC::MTCTR8)) |
| 328 | .addReg(AddrReg); |
| 329 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(PPC::BCTR8)); |
| 330 | |
| 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)]); |
| 334 | |
| 335 | return true; |
| 336 | } |
| 337 | |
Bill Schmidt | 646cd79 | 2013-07-30 00:50:39 +0000 | [diff] [blame] | 338 | // Attempt to fast-select an instruction that wasn't handled by |
Bill Schmidt | 3fad2bc | 2013-08-25 22:33:42 +0000 | [diff] [blame^] | 339 | // the table-generated machinery. |
Bill Schmidt | 646cd79 | 2013-07-30 00:50:39 +0000 | [diff] [blame] | 340 | bool PPCFastISel::TargetSelectInstruction(const Instruction *I) { |
Bill Schmidt | 3fad2bc | 2013-08-25 22:33:42 +0000 | [diff] [blame^] | 341 | |
| 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. |
| 350 | default: |
| 351 | break; |
| 352 | } |
| 353 | return false; |
Bill Schmidt | 646cd79 | 2013-07-30 00:50:39 +0000 | [diff] [blame] | 354 | } |
| 355 | |
| 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) |
| 361 | return 0; |
| 362 | |
| 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(); |
| 369 | |
| 370 | MachineMemOperand *MMO = |
| 371 | FuncInfo.MF->getMachineMemOperand( |
| 372 | MachinePointerInfo::getConstantPool(), MachineMemOperand::MOLoad, |
| 373 | (VT == MVT::f32) ? 4 : 8, Align); |
| 374 | |
Bill Schmidt | 3fad2bc | 2013-08-25 22:33:42 +0000 | [diff] [blame^] | 375 | unsigned Opc = (VT == MVT::f32) ? PPC::LFS : PPC::LFD; |
| 376 | unsigned TmpReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass); |
| 377 | |
| 378 | // For small code model, generate a LF[SD](0, LDtocCPT(Idx, X2)). |
| 379 | if (CModel == CodeModel::Small || CModel == CodeModel::JITDefault) { |
Bill Schmidt | 646cd79 | 2013-07-30 00:50:39 +0000 | [diff] [blame] | 380 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(PPC::LDtocCPT), |
Bill Schmidt | 3fad2bc | 2013-08-25 22:33:42 +0000 | [diff] [blame^] | 381 | TmpReg) |
| 382 | .addConstantPoolIndex(Idx).addReg(PPC::X2); |
| 383 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), DestReg) |
| 384 | .addImm(0).addReg(TmpReg).addMemOperand(MMO); |
| 385 | } else { |
Bill Schmidt | 646cd79 | 2013-07-30 00:50:39 +0000 | [diff] [blame] | 386 | // Otherwise we generate LF[SD](Idx[lo], ADDIStocHA(X2, Idx)). |
Bill Schmidt | 646cd79 | 2013-07-30 00:50:39 +0000 | [diff] [blame] | 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) |
| 391 | .addReg(TmpReg) |
| 392 | .addMemOperand(MMO); |
| 393 | } |
| 394 | |
| 395 | return DestReg; |
| 396 | } |
| 397 | |
| 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; |
| 404 | |
| 405 | unsigned ResultReg = createResultReg(RC); |
| 406 | bool IsGPRC = RC->hasSuperClassEq(&PPC::GPRCRegClass); |
| 407 | |
| 408 | if (isInt<16>(Imm)) |
| 409 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 410 | TII.get(IsGPRC ? PPC::LI : PPC::LI8), ResultReg) |
| 411 | .addImm(Imm); |
| 412 | else if (Lo) { |
| 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) |
| 417 | .addImm(Hi); |
| 418 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 419 | TII.get(IsGPRC ? PPC::ORI : PPC::ORI8), ResultReg) |
| 420 | .addReg(TmpReg).addImm(Lo); |
| 421 | } else |
| 422 | // Just Hi bits. |
| 423 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 424 | TII.get(IsGPRC ? PPC::LIS : PPC::LIS8), ResultReg) |
| 425 | .addImm(Hi); |
| 426 | |
| 427 | return ResultReg; |
| 428 | } |
| 429 | |
| 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; |
| 435 | unsigned Shift = 0; |
| 436 | |
| 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; |
| 442 | |
| 443 | if (isInt<32>(ImmSh)) |
| 444 | Imm = ImmSh; |
| 445 | else { |
| 446 | Remainder = Imm; |
| 447 | Shift = 32; |
| 448 | Imm >>= 32; |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | // Handle the high-order 32 bits (if shifted) or the whole 32 bits |
| 453 | // (if not shifted). |
| 454 | unsigned TmpReg1 = PPCMaterialize32BitInt(Imm, RC); |
| 455 | if (!Shift) |
| 456 | return TmpReg1; |
| 457 | |
| 458 | // If upper 32 bits were not zero, we've built them and need to shift |
| 459 | // them into place. |
| 460 | unsigned TmpReg2; |
| 461 | if (Imm) { |
| 462 | TmpReg2 = createResultReg(RC); |
| 463 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(PPC::RLDICR), |
| 464 | TmpReg2).addReg(TmpReg1).addImm(Shift).addImm(63 - Shift); |
| 465 | } else |
| 466 | TmpReg2 = TmpReg1; |
| 467 | |
| 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); |
| 473 | } else |
| 474 | TmpReg3 = TmpReg2; |
| 475 | |
| 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); |
| 480 | return ResultReg; |
| 481 | } |
| 482 | |
| 483 | return TmpReg3; |
| 484 | } |
| 485 | |
| 486 | |
| 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) { |
| 490 | |
| 491 | if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16 && |
| 492 | VT != MVT::i8 && VT != MVT::i1) |
| 493 | return 0; |
| 494 | |
| 495 | const TargetRegisterClass *RC = ((VT == MVT::i64) ? &PPC::G8RCRegClass : |
| 496 | &PPC::GPRCRegClass); |
| 497 | |
| 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()); |
| 505 | return ImmReg; |
| 506 | } |
| 507 | |
| 508 | // Construct the constant piecewise. |
| 509 | int64_t Imm = CI->getZExtValue(); |
| 510 | |
| 511 | if (VT == MVT::i64) |
| 512 | return PPCMaterialize64BitInt(Imm, RC); |
| 513 | else if (VT == MVT::i32) |
| 514 | return PPCMaterialize32BitInt(Imm, RC); |
| 515 | |
| 516 | return 0; |
| 517 | } |
| 518 | |
| 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); |
| 523 | |
| 524 | // Only handle simple types. |
| 525 | if (!CEVT.isSimple()) return 0; |
| 526 | MVT VT = CEVT.getSimpleVT(); |
| 527 | |
| 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. |
| 533 | |
| 534 | return 0; |
| 535 | } |
| 536 | |
| 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) { |
| 540 | return AI && 0; |
| 541 | } |
| 542 | |
| 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; |
| 547 | } |
| 548 | |
| 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 |
| 555 | // or vector args. |
| 556 | return false; |
| 557 | } |
| 558 | |
Bill Schmidt | 3fad2bc | 2013-08-25 22:33:42 +0000 | [diff] [blame^] | 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) { |
| 562 | |
| 563 | if (Opc != ISD::Constant) |
| 564 | return 0; |
| 565 | |
| 566 | if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16 && |
| 567 | VT != MVT::i8 && VT != MVT::i1) |
| 568 | return 0; |
| 569 | |
| 570 | const TargetRegisterClass *RC = ((VT == MVT::i64) ? &PPC::G8RCRegClass : |
| 571 | &PPC::GPRCRegClass); |
| 572 | if (VT == MVT::i64) |
| 573 | return PPCMaterialize64BitInt(Imm, RC); |
| 574 | else |
| 575 | return PPCMaterialize32BitInt(Imm, RC); |
| 576 | } |
| 577 | |
Bill Schmidt | 646cd79 | 2013-07-30 00:50:39 +0000 | [diff] [blame] | 578 | namespace llvm { |
| 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(); |
| 583 | |
| 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); |
| 588 | |
| 589 | return 0; |
| 590 | } |
| 591 | } |