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