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