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