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