Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 1 | //===-- llvm/lib/Target/ARM/ARMCallLowering.cpp - Call lowering -----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | /// |
| 10 | /// \file |
| 11 | /// This file implements the lowering of LLVM calls to machine code calls for |
| 12 | /// GlobalISel. |
| 13 | /// |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "ARMCallLowering.h" |
| 17 | |
| 18 | #include "ARMBaseInstrInfo.h" |
| 19 | #include "ARMISelLowering.h" |
Diana Picus | 1d8eaf4 | 2017-01-25 07:08:53 +0000 | [diff] [blame] | 20 | #include "ARMSubtarget.h" |
Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 21 | |
Diana Picus | 32cd9b4 | 2017-02-02 14:01:00 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/Analysis.h" |
Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h" |
Diana Picus | 1437f6d | 2016-12-19 11:55:41 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace llvm; |
| 27 | |
| 28 | #ifndef LLVM_BUILD_GLOBAL_ISEL |
| 29 | #error "This shouldn't be built without GISel" |
| 30 | #endif |
| 31 | |
| 32 | ARMCallLowering::ARMCallLowering(const ARMTargetLowering &TLI) |
| 33 | : CallLowering(&TLI) {} |
| 34 | |
Benjamin Kramer | 061f4a5 | 2017-01-13 14:39:03 +0000 | [diff] [blame] | 35 | static bool isSupportedType(const DataLayout &DL, const ARMTargetLowering &TLI, |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 36 | Type *T) { |
Diana Picus | 0c11c7b | 2017-02-02 14:00:54 +0000 | [diff] [blame] | 37 | EVT VT = TLI.getValueType(DL, T, true); |
Diana Picus | f941ec0 | 2017-04-21 11:53:01 +0000 | [diff] [blame^] | 38 | if (!VT.isSimple() || VT.isVector() || |
| 39 | !(VT.isInteger() || VT.isFloatingPoint())) |
Diana Picus | 97ae95c | 2016-12-19 14:08:02 +0000 | [diff] [blame] | 40 | return false; |
| 41 | |
| 42 | unsigned VTSize = VT.getSimpleVT().getSizeInBits(); |
Diana Picus | ca6a890 | 2017-02-16 07:53:07 +0000 | [diff] [blame] | 43 | |
| 44 | if (VTSize == 64) |
| 45 | // FIXME: Support i64 too |
| 46 | return VT.isFloatingPoint(); |
| 47 | |
Diana Picus | d83df5d | 2017-01-25 08:47:40 +0000 | [diff] [blame] | 48 | return VTSize == 1 || VTSize == 8 || VTSize == 16 || VTSize == 32; |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | namespace { |
Diana Picus | a606713 | 2017-02-23 13:25:43 +0000 | [diff] [blame] | 52 | /// Helper class for values going out through an ABI boundary (used for handling |
| 53 | /// function return values and call parameters). |
| 54 | struct OutgoingValueHandler : public CallLowering::ValueHandler { |
| 55 | OutgoingValueHandler(MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI, |
| 56 | MachineInstrBuilder &MIB, CCAssignFn *AssignFn) |
Diana Picus | 1ffca2a | 2017-02-28 14:17:53 +0000 | [diff] [blame] | 57 | : ValueHandler(MIRBuilder, MRI, AssignFn), MIB(MIB), StackSize(0) {} |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 58 | |
| 59 | unsigned getStackAddress(uint64_t Size, int64_t Offset, |
| 60 | MachinePointerInfo &MPO) override { |
Diana Picus | 3841522 | 2017-03-01 15:54:21 +0000 | [diff] [blame] | 61 | assert((Size == 1 || Size == 2 || Size == 4 || Size == 8) && |
| 62 | "Unsupported size"); |
Diana Picus | 1ffca2a | 2017-02-28 14:17:53 +0000 | [diff] [blame] | 63 | |
| 64 | LLT p0 = LLT::pointer(0, 32); |
| 65 | LLT s32 = LLT::scalar(32); |
| 66 | unsigned SPReg = MRI.createGenericVirtualRegister(p0); |
| 67 | MIRBuilder.buildCopy(SPReg, ARM::SP); |
| 68 | |
| 69 | unsigned OffsetReg = MRI.createGenericVirtualRegister(s32); |
| 70 | MIRBuilder.buildConstant(OffsetReg, Offset); |
| 71 | |
| 72 | unsigned AddrReg = MRI.createGenericVirtualRegister(p0); |
| 73 | MIRBuilder.buildGEP(AddrReg, SPReg, OffsetReg); |
| 74 | |
| 75 | MPO = MachinePointerInfo::getStack(MIRBuilder.getMF(), Offset); |
Diana Picus | 1ffca2a | 2017-02-28 14:17:53 +0000 | [diff] [blame] | 76 | return AddrReg; |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void assignValueToReg(unsigned ValVReg, unsigned PhysReg, |
| 80 | CCValAssign &VA) override { |
| 81 | assert(VA.isRegLoc() && "Value shouldn't be assigned to reg"); |
| 82 | assert(VA.getLocReg() == PhysReg && "Assigning to the wrong reg?"); |
| 83 | |
Diana Picus | ca6a890 | 2017-02-16 07:53:07 +0000 | [diff] [blame] | 84 | assert(VA.getValVT().getSizeInBits() <= 64 && "Unsupported value size"); |
| 85 | assert(VA.getLocVT().getSizeInBits() <= 64 && "Unsupported location size"); |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 86 | |
Diana Picus | 8b6c6be | 2017-01-25 08:10:40 +0000 | [diff] [blame] | 87 | unsigned ExtReg = extendRegister(ValVReg, VA); |
| 88 | MIRBuilder.buildCopy(PhysReg, ExtReg); |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 89 | MIB.addUse(PhysReg, RegState::Implicit); |
| 90 | } |
| 91 | |
| 92 | void assignValueToAddress(unsigned ValVReg, unsigned Addr, uint64_t Size, |
| 93 | MachinePointerInfo &MPO, CCValAssign &VA) override { |
Diana Picus | 9c52309 | 2017-03-01 15:35:14 +0000 | [diff] [blame] | 94 | assert((Size == 1 || Size == 2 || Size == 4 || Size == 8) && |
| 95 | "Unsupported size"); |
Diana Picus | 1ffca2a | 2017-02-28 14:17:53 +0000 | [diff] [blame] | 96 | |
Diana Picus | 9c52309 | 2017-03-01 15:35:14 +0000 | [diff] [blame] | 97 | unsigned ExtReg = extendRegister(ValVReg, VA); |
Diana Picus | 1ffca2a | 2017-02-28 14:17:53 +0000 | [diff] [blame] | 98 | auto MMO = MIRBuilder.getMF().getMachineMemOperand( |
Diana Picus | 9c52309 | 2017-03-01 15:35:14 +0000 | [diff] [blame] | 99 | MPO, MachineMemOperand::MOStore, VA.getLocVT().getStoreSize(), |
| 100 | /* Alignment */ 0); |
| 101 | MIRBuilder.buildStore(ExtReg, Addr, *MMO); |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Diana Picus | ca6a890 | 2017-02-16 07:53:07 +0000 | [diff] [blame] | 104 | unsigned assignCustomValue(const CallLowering::ArgInfo &Arg, |
| 105 | ArrayRef<CCValAssign> VAs) override { |
| 106 | CCValAssign VA = VAs[0]; |
| 107 | assert(VA.needsCustom() && "Value doesn't need custom handling"); |
| 108 | assert(VA.getValVT() == MVT::f64 && "Unsupported type"); |
| 109 | |
| 110 | CCValAssign NextVA = VAs[1]; |
| 111 | assert(NextVA.needsCustom() && "Value doesn't need custom handling"); |
| 112 | assert(NextVA.getValVT() == MVT::f64 && "Unsupported type"); |
| 113 | |
| 114 | assert(VA.getValNo() == NextVA.getValNo() && |
| 115 | "Values belong to different arguments"); |
| 116 | |
| 117 | assert(VA.isRegLoc() && "Value should be in reg"); |
| 118 | assert(NextVA.isRegLoc() && "Value should be in reg"); |
| 119 | |
| 120 | unsigned NewRegs[] = {MRI.createGenericVirtualRegister(LLT::scalar(32)), |
| 121 | MRI.createGenericVirtualRegister(LLT::scalar(32))}; |
Tim Northover | c2c545b | 2017-03-06 23:50:28 +0000 | [diff] [blame] | 122 | MIRBuilder.buildExtract(NewRegs[0], Arg.Reg, 0); |
| 123 | MIRBuilder.buildExtract(NewRegs[1], Arg.Reg, 32); |
Diana Picus | ca6a890 | 2017-02-16 07:53:07 +0000 | [diff] [blame] | 124 | |
| 125 | bool IsLittle = MIRBuilder.getMF().getSubtarget<ARMSubtarget>().isLittle(); |
| 126 | if (!IsLittle) |
| 127 | std::swap(NewRegs[0], NewRegs[1]); |
| 128 | |
| 129 | assignValueToReg(NewRegs[0], VA.getLocReg(), VA); |
| 130 | assignValueToReg(NewRegs[1], NextVA.getLocReg(), NextVA); |
| 131 | |
| 132 | return 1; |
| 133 | } |
| 134 | |
Diana Picus | 9c52309 | 2017-03-01 15:35:14 +0000 | [diff] [blame] | 135 | bool assignArg(unsigned ValNo, MVT ValVT, MVT LocVT, |
Diana Picus | 3841522 | 2017-03-01 15:54:21 +0000 | [diff] [blame] | 136 | CCValAssign::LocInfo LocInfo, |
| 137 | const CallLowering::ArgInfo &Info, CCState &State) override { |
Diana Picus | 9c52309 | 2017-03-01 15:35:14 +0000 | [diff] [blame] | 138 | if (AssignFn(ValNo, ValVT, LocVT, LocInfo, Info.Flags, State)) |
| 139 | return true; |
| 140 | |
Diana Picus | 3841522 | 2017-03-01 15:54:21 +0000 | [diff] [blame] | 141 | StackSize = |
| 142 | std::max(StackSize, static_cast<uint64_t>(State.getNextStackOffset())); |
Diana Picus | 9c52309 | 2017-03-01 15:35:14 +0000 | [diff] [blame] | 143 | return false; |
| 144 | } |
| 145 | |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 146 | MachineInstrBuilder &MIB; |
Diana Picus | 1ffca2a | 2017-02-28 14:17:53 +0000 | [diff] [blame] | 147 | uint64_t StackSize; |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 148 | }; |
| 149 | } // End anonymous namespace. |
| 150 | |
Diana Picus | 32cd9b4 | 2017-02-02 14:01:00 +0000 | [diff] [blame] | 151 | void ARMCallLowering::splitToValueTypes(const ArgInfo &OrigArg, |
| 152 | SmallVectorImpl<ArgInfo> &SplitArgs, |
| 153 | const DataLayout &DL, |
| 154 | MachineRegisterInfo &MRI) const { |
| 155 | const ARMTargetLowering &TLI = *getTLI<ARMTargetLowering>(); |
| 156 | LLVMContext &Ctx = OrigArg.Ty->getContext(); |
| 157 | |
| 158 | SmallVector<EVT, 4> SplitVTs; |
| 159 | SmallVector<uint64_t, 4> Offsets; |
| 160 | ComputeValueVTs(TLI, DL, OrigArg.Ty, SplitVTs, &Offsets, 0); |
| 161 | |
| 162 | assert(SplitVTs.size() == 1 && "Unsupported type"); |
| 163 | |
| 164 | // Even if there is no splitting to do, we still want to replace the original |
| 165 | // type (e.g. pointer type -> integer). |
| 166 | SplitArgs.emplace_back(OrigArg.Reg, SplitVTs[0].getTypeForEVT(Ctx), |
| 167 | OrigArg.Flags, OrigArg.IsFixed); |
| 168 | } |
| 169 | |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 170 | /// Lower the return value for the already existing \p Ret. This assumes that |
| 171 | /// \p MIRBuilder's insertion point is correct. |
| 172 | bool ARMCallLowering::lowerReturnVal(MachineIRBuilder &MIRBuilder, |
| 173 | const Value *Val, unsigned VReg, |
| 174 | MachineInstrBuilder &Ret) const { |
| 175 | if (!Val) |
| 176 | // Nothing to do here. |
| 177 | return true; |
| 178 | |
| 179 | auto &MF = MIRBuilder.getMF(); |
| 180 | const auto &F = *MF.getFunction(); |
| 181 | |
| 182 | auto DL = MF.getDataLayout(); |
| 183 | auto &TLI = *getTLI<ARMTargetLowering>(); |
| 184 | if (!isSupportedType(DL, TLI, Val->getType())) |
Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 185 | return false; |
| 186 | |
Diana Picus | 32cd9b4 | 2017-02-02 14:01:00 +0000 | [diff] [blame] | 187 | SmallVector<ArgInfo, 4> SplitVTs; |
| 188 | ArgInfo RetInfo(VReg, Val->getType()); |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 189 | setArgFlags(RetInfo, AttributeList::ReturnIndex, DL, F); |
Diana Picus | 32cd9b4 | 2017-02-02 14:01:00 +0000 | [diff] [blame] | 190 | splitToValueTypes(RetInfo, SplitVTs, DL, MF.getRegInfo()); |
| 191 | |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 192 | CCAssignFn *AssignFn = |
| 193 | TLI.CCAssignFnForReturn(F.getCallingConv(), F.isVarArg()); |
Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 194 | |
Diana Picus | a606713 | 2017-02-23 13:25:43 +0000 | [diff] [blame] | 195 | OutgoingValueHandler RetHandler(MIRBuilder, MF.getRegInfo(), Ret, AssignFn); |
Diana Picus | 32cd9b4 | 2017-02-02 14:01:00 +0000 | [diff] [blame] | 196 | return handleAssignments(MIRBuilder, SplitVTs, RetHandler); |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | bool ARMCallLowering::lowerReturn(MachineIRBuilder &MIRBuilder, |
| 200 | const Value *Val, unsigned VReg) const { |
| 201 | assert(!Val == !VReg && "Return value without a vreg"); |
| 202 | |
Diana Picus | 4f8c3e1 | 2017-01-13 09:37:56 +0000 | [diff] [blame] | 203 | auto Ret = MIRBuilder.buildInstrNoInsert(ARM::BX_RET).add(predOps(ARMCC::AL)); |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 204 | |
| 205 | if (!lowerReturnVal(MIRBuilder, Val, VReg, Ret)) |
| 206 | return false; |
| 207 | |
| 208 | MIRBuilder.insertInstr(Ret); |
Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 209 | return true; |
| 210 | } |
| 211 | |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 212 | namespace { |
Diana Picus | a8cb0cd | 2017-02-23 14:18:41 +0000 | [diff] [blame] | 213 | /// Helper class for values coming in through an ABI boundary (used for handling |
| 214 | /// formal arguments and call return values). |
| 215 | struct IncomingValueHandler : public CallLowering::ValueHandler { |
| 216 | IncomingValueHandler(MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI, |
| 217 | CCAssignFn AssignFn) |
Tim Northover | d943354 | 2017-01-17 22:30:10 +0000 | [diff] [blame] | 218 | : ValueHandler(MIRBuilder, MRI, AssignFn) {} |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 219 | |
| 220 | unsigned getStackAddress(uint64_t Size, int64_t Offset, |
| 221 | MachinePointerInfo &MPO) override { |
Diana Picus | ca6a890 | 2017-02-16 07:53:07 +0000 | [diff] [blame] | 222 | assert((Size == 1 || Size == 2 || Size == 4 || Size == 8) && |
| 223 | "Unsupported size"); |
Diana Picus | 1437f6d | 2016-12-19 11:55:41 +0000 | [diff] [blame] | 224 | |
| 225 | auto &MFI = MIRBuilder.getMF().getFrameInfo(); |
| 226 | |
| 227 | int FI = MFI.CreateFixedObject(Size, Offset, true); |
| 228 | MPO = MachinePointerInfo::getFixedStack(MIRBuilder.getMF(), FI); |
| 229 | |
| 230 | unsigned AddrReg = |
| 231 | MRI.createGenericVirtualRegister(LLT::pointer(MPO.getAddrSpace(), 32)); |
| 232 | MIRBuilder.buildFrameIndex(AddrReg, FI); |
| 233 | |
| 234 | return AddrReg; |
| 235 | } |
| 236 | |
| 237 | void assignValueToAddress(unsigned ValVReg, unsigned Addr, uint64_t Size, |
| 238 | MachinePointerInfo &MPO, CCValAssign &VA) override { |
Diana Picus | ca6a890 | 2017-02-16 07:53:07 +0000 | [diff] [blame] | 239 | assert((Size == 1 || Size == 2 || Size == 4 || Size == 8) && |
| 240 | "Unsupported size"); |
Diana Picus | 278c722 | 2017-01-26 09:20:47 +0000 | [diff] [blame] | 241 | |
| 242 | if (VA.getLocInfo() == CCValAssign::SExt || |
| 243 | VA.getLocInfo() == CCValAssign::ZExt) { |
Diana Picus | a8cb0cd | 2017-02-23 14:18:41 +0000 | [diff] [blame] | 244 | // If the value is zero- or sign-extended, its size becomes 4 bytes, so |
| 245 | // that's what we should load. |
Diana Picus | 278c722 | 2017-01-26 09:20:47 +0000 | [diff] [blame] | 246 | Size = 4; |
| 247 | assert(MRI.getType(ValVReg).isScalar() && "Only scalars supported atm"); |
| 248 | MRI.setType(ValVReg, LLT::scalar(32)); |
| 249 | } |
Diana Picus | 1437f6d | 2016-12-19 11:55:41 +0000 | [diff] [blame] | 250 | |
| 251 | auto MMO = MIRBuilder.getMF().getMachineMemOperand( |
| 252 | MPO, MachineMemOperand::MOLoad, Size, /* Alignment */ 0); |
| 253 | MIRBuilder.buildLoad(ValVReg, Addr, *MMO); |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | void assignValueToReg(unsigned ValVReg, unsigned PhysReg, |
| 257 | CCValAssign &VA) override { |
| 258 | assert(VA.isRegLoc() && "Value shouldn't be assigned to reg"); |
| 259 | assert(VA.getLocReg() == PhysReg && "Assigning to the wrong reg?"); |
| 260 | |
Diana Picus | ca6a890 | 2017-02-16 07:53:07 +0000 | [diff] [blame] | 261 | assert(VA.getValVT().getSizeInBits() <= 64 && "Unsupported value size"); |
| 262 | assert(VA.getLocVT().getSizeInBits() <= 64 && "Unsupported location size"); |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 263 | |
Diana Picus | a8cb0cd | 2017-02-23 14:18:41 +0000 | [diff] [blame] | 264 | // The necesary extensions are handled on the other side of the ABI |
| 265 | // boundary. |
| 266 | markPhysRegUsed(PhysReg); |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 267 | MIRBuilder.buildCopy(ValVReg, PhysReg); |
| 268 | } |
Diana Picus | ca6a890 | 2017-02-16 07:53:07 +0000 | [diff] [blame] | 269 | |
Diana Picus | a606713 | 2017-02-23 13:25:43 +0000 | [diff] [blame] | 270 | unsigned assignCustomValue(const ARMCallLowering::ArgInfo &Arg, |
Diana Picus | ca6a890 | 2017-02-16 07:53:07 +0000 | [diff] [blame] | 271 | ArrayRef<CCValAssign> VAs) override { |
| 272 | CCValAssign VA = VAs[0]; |
| 273 | assert(VA.needsCustom() && "Value doesn't need custom handling"); |
| 274 | assert(VA.getValVT() == MVT::f64 && "Unsupported type"); |
| 275 | |
| 276 | CCValAssign NextVA = VAs[1]; |
| 277 | assert(NextVA.needsCustom() && "Value doesn't need custom handling"); |
| 278 | assert(NextVA.getValVT() == MVT::f64 && "Unsupported type"); |
| 279 | |
| 280 | assert(VA.getValNo() == NextVA.getValNo() && |
| 281 | "Values belong to different arguments"); |
| 282 | |
| 283 | assert(VA.isRegLoc() && "Value should be in reg"); |
| 284 | assert(NextVA.isRegLoc() && "Value should be in reg"); |
| 285 | |
| 286 | unsigned NewRegs[] = {MRI.createGenericVirtualRegister(LLT::scalar(32)), |
| 287 | MRI.createGenericVirtualRegister(LLT::scalar(32))}; |
| 288 | |
| 289 | assignValueToReg(NewRegs[0], VA.getLocReg(), VA); |
| 290 | assignValueToReg(NewRegs[1], NextVA.getLocReg(), NextVA); |
| 291 | |
| 292 | bool IsLittle = MIRBuilder.getMF().getSubtarget<ARMSubtarget>().isLittle(); |
| 293 | if (!IsLittle) |
| 294 | std::swap(NewRegs[0], NewRegs[1]); |
| 295 | |
| 296 | MIRBuilder.buildSequence(Arg.Reg, NewRegs, {0, 32}); |
| 297 | |
| 298 | return 1; |
| 299 | } |
Diana Picus | a8cb0cd | 2017-02-23 14:18:41 +0000 | [diff] [blame] | 300 | |
| 301 | /// Marking a physical register as used is different between formal |
| 302 | /// parameters, where it's a basic block live-in, and call returns, where it's |
| 303 | /// an implicit-def of the call instruction. |
| 304 | virtual void markPhysRegUsed(unsigned PhysReg) = 0; |
| 305 | }; |
| 306 | |
| 307 | struct FormalArgHandler : public IncomingValueHandler { |
| 308 | FormalArgHandler(MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI, |
| 309 | CCAssignFn AssignFn) |
| 310 | : IncomingValueHandler(MIRBuilder, MRI, AssignFn) {} |
| 311 | |
| 312 | void markPhysRegUsed(unsigned PhysReg) override { |
| 313 | MIRBuilder.getMBB().addLiveIn(PhysReg); |
| 314 | } |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 315 | }; |
| 316 | } // End anonymous namespace |
| 317 | |
Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 318 | bool ARMCallLowering::lowerFormalArguments(MachineIRBuilder &MIRBuilder, |
| 319 | const Function &F, |
| 320 | ArrayRef<unsigned> VRegs) const { |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 321 | // Quick exit if there aren't any args |
| 322 | if (F.arg_empty()) |
| 323 | return true; |
| 324 | |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 325 | if (F.isVarArg()) |
| 326 | return false; |
| 327 | |
Diana Picus | 32cd9b4 | 2017-02-02 14:01:00 +0000 | [diff] [blame] | 328 | auto &MF = MIRBuilder.getMF(); |
| 329 | auto DL = MF.getDataLayout(); |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 330 | auto &TLI = *getTLI<ARMTargetLowering>(); |
| 331 | |
Diana Picus | 7232af3 | 2017-02-09 13:09:59 +0000 | [diff] [blame] | 332 | auto Subtarget = TLI.getSubtarget(); |
| 333 | |
| 334 | if (Subtarget->isThumb()) |
| 335 | return false; |
| 336 | |
Reid Kleckner | 45707d4 | 2017-03-16 22:59:15 +0000 | [diff] [blame] | 337 | for (auto &Arg : F.args()) |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 338 | if (!isSupportedType(DL, TLI, Arg.getType())) |
| 339 | return false; |
| 340 | |
| 341 | CCAssignFn *AssignFn = |
| 342 | TLI.CCAssignFnForCall(F.getCallingConv(), F.isVarArg()); |
| 343 | |
| 344 | SmallVector<ArgInfo, 8> ArgInfos; |
| 345 | unsigned Idx = 0; |
Reid Kleckner | 45707d4 | 2017-03-16 22:59:15 +0000 | [diff] [blame] | 346 | for (auto &Arg : F.args()) { |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 347 | ArgInfo AInfo(VRegs[Idx], Arg.getType()); |
| 348 | setArgFlags(AInfo, Idx + 1, DL, F); |
Diana Picus | 32cd9b4 | 2017-02-02 14:01:00 +0000 | [diff] [blame] | 349 | splitToValueTypes(AInfo, ArgInfos, DL, MF.getRegInfo()); |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 350 | Idx++; |
| 351 | } |
| 352 | |
Tim Northover | d943354 | 2017-01-17 22:30:10 +0000 | [diff] [blame] | 353 | FormalArgHandler ArgHandler(MIRBuilder, MIRBuilder.getMF().getRegInfo(), |
| 354 | AssignFn); |
| 355 | return handleAssignments(MIRBuilder, ArgInfos, ArgHandler); |
Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 356 | } |
Diana Picus | 613b656 | 2017-02-21 11:33:59 +0000 | [diff] [blame] | 357 | |
Diana Picus | a8cb0cd | 2017-02-23 14:18:41 +0000 | [diff] [blame] | 358 | namespace { |
| 359 | struct CallReturnHandler : public IncomingValueHandler { |
| 360 | CallReturnHandler(MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI, |
| 361 | MachineInstrBuilder MIB, CCAssignFn *AssignFn) |
| 362 | : IncomingValueHandler(MIRBuilder, MRI, AssignFn), MIB(MIB) {} |
| 363 | |
| 364 | void markPhysRegUsed(unsigned PhysReg) override { |
| 365 | MIB.addDef(PhysReg, RegState::Implicit); |
| 366 | } |
| 367 | |
| 368 | MachineInstrBuilder MIB; |
| 369 | }; |
| 370 | } // End anonymous namespace. |
| 371 | |
Diana Picus | 613b656 | 2017-02-21 11:33:59 +0000 | [diff] [blame] | 372 | bool ARMCallLowering::lowerCall(MachineIRBuilder &MIRBuilder, |
Diana Picus | d79253a | 2017-03-20 14:40:18 +0000 | [diff] [blame] | 373 | CallingConv::ID CallConv, |
Diana Picus | 613b656 | 2017-02-21 11:33:59 +0000 | [diff] [blame] | 374 | const MachineOperand &Callee, |
| 375 | const ArgInfo &OrigRet, |
| 376 | ArrayRef<ArgInfo> OrigArgs) const { |
Diana Picus | a606713 | 2017-02-23 13:25:43 +0000 | [diff] [blame] | 377 | MachineFunction &MF = MIRBuilder.getMF(); |
| 378 | const auto &TLI = *getTLI<ARMTargetLowering>(); |
| 379 | const auto &DL = MF.getDataLayout(); |
Diana Picus | 613b656 | 2017-02-21 11:33:59 +0000 | [diff] [blame] | 380 | const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); |
Diana Picus | a606713 | 2017-02-23 13:25:43 +0000 | [diff] [blame] | 381 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
Diana Picus | 613b656 | 2017-02-21 11:33:59 +0000 | [diff] [blame] | 382 | |
| 383 | if (MF.getSubtarget<ARMSubtarget>().genLongCalls()) |
| 384 | return false; |
| 385 | |
Diana Picus | 1ffca2a | 2017-02-28 14:17:53 +0000 | [diff] [blame] | 386 | auto CallSeqStart = MIRBuilder.buildInstr(ARM::ADJCALLSTACKDOWN); |
Diana Picus | 613b656 | 2017-02-21 11:33:59 +0000 | [diff] [blame] | 387 | |
Diana Picus | a606713 | 2017-02-23 13:25:43 +0000 | [diff] [blame] | 388 | // Create the call instruction so we can add the implicit uses of arg |
| 389 | // registers, but don't insert it yet. |
| 390 | auto MIB = MIRBuilder.buildInstrNoInsert(ARM::BLX).add(Callee).addRegMask( |
| 391 | TRI->getCallPreservedMask(MF, CallConv)); |
| 392 | |
| 393 | SmallVector<ArgInfo, 8> ArgInfos; |
| 394 | for (auto Arg : OrigArgs) { |
| 395 | if (!isSupportedType(DL, TLI, Arg.Ty)) |
| 396 | return false; |
| 397 | |
| 398 | if (!Arg.IsFixed) |
| 399 | return false; |
| 400 | |
| 401 | splitToValueTypes(Arg, ArgInfos, DL, MRI); |
| 402 | } |
| 403 | |
| 404 | auto ArgAssignFn = TLI.CCAssignFnForCall(CallConv, /*IsVarArg=*/false); |
| 405 | OutgoingValueHandler ArgHandler(MIRBuilder, MRI, MIB, ArgAssignFn); |
| 406 | if (!handleAssignments(MIRBuilder, ArgInfos, ArgHandler)) |
| 407 | return false; |
| 408 | |
| 409 | // Now we can add the actual call instruction to the correct basic block. |
| 410 | MIRBuilder.insertInstr(MIB); |
Diana Picus | 613b656 | 2017-02-21 11:33:59 +0000 | [diff] [blame] | 411 | |
Diana Picus | a8cb0cd | 2017-02-23 14:18:41 +0000 | [diff] [blame] | 412 | if (!OrigRet.Ty->isVoidTy()) { |
| 413 | if (!isSupportedType(DL, TLI, OrigRet.Ty)) |
| 414 | return false; |
| 415 | |
| 416 | ArgInfos.clear(); |
| 417 | splitToValueTypes(OrigRet, ArgInfos, DL, MRI); |
| 418 | |
| 419 | auto RetAssignFn = TLI.CCAssignFnForReturn(CallConv, /*IsVarArg=*/false); |
| 420 | CallReturnHandler RetHandler(MIRBuilder, MRI, MIB, RetAssignFn); |
| 421 | if (!handleAssignments(MIRBuilder, ArgInfos, RetHandler)) |
| 422 | return false; |
| 423 | } |
| 424 | |
Diana Picus | 1ffca2a | 2017-02-28 14:17:53 +0000 | [diff] [blame] | 425 | // We now know the size of the stack - update the ADJCALLSTACKDOWN |
| 426 | // accordingly. |
| 427 | CallSeqStart.addImm(ArgHandler.StackSize).add(predOps(ARMCC::AL)); |
| 428 | |
Diana Picus | 613b656 | 2017-02-21 11:33:59 +0000 | [diff] [blame] | 429 | MIRBuilder.buildInstr(ARM::ADJCALLSTACKUP) |
Diana Picus | 1ffca2a | 2017-02-28 14:17:53 +0000 | [diff] [blame] | 430 | .addImm(ArgHandler.StackSize) |
Diana Picus | 613b656 | 2017-02-21 11:33:59 +0000 | [diff] [blame] | 431 | .addImm(0) |
| 432 | .add(predOps(ARMCC::AL)); |
| 433 | |
| 434 | return true; |
| 435 | } |