Eugene Zelenko | 076468c | 2017-09-20 21:35:51 +0000 | [diff] [blame] | 1 | //===- llvm/lib/Target/ARM/ARMCallLowering.cpp - Call lowering ------------===// |
Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 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 |
Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
Eugene Zelenko | 076468c | 2017-09-20 21:35:51 +0000 | [diff] [blame] | 8 | // |
Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 9 | /// \file |
| 10 | /// This file implements the lowering of LLVM calls to machine code calls for |
| 11 | /// GlobalISel. |
Eugene Zelenko | 076468c | 2017-09-20 21:35:51 +0000 | [diff] [blame] | 12 | // |
Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "ARMCallLowering.h" |
Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 16 | #include "ARMBaseInstrInfo.h" |
| 17 | #include "ARMISelLowering.h" |
Diana Picus | 1d8eaf4 | 2017-01-25 07:08:53 +0000 | [diff] [blame] | 18 | #include "ARMSubtarget.h" |
Eugene Zelenko | 076468c | 2017-09-20 21:35:51 +0000 | [diff] [blame] | 19 | #include "Utils/ARMBaseInfo.h" |
| 20 | #include "llvm/ADT/SmallVector.h" |
Diana Picus | 32cd9b4 | 2017-02-02 14:01:00 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/Analysis.h" |
Eugene Zelenko | 076468c | 2017-09-20 21:35:51 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/CallingConvLower.h" |
Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h" |
Diana Picus | 0091cc3 | 2017-06-05 12:54:53 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/GlobalISel/Utils.h" |
Eugene Zelenko | 076468c | 2017-09-20 21:35:51 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/LowLevelType.h" |
| 26 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 27 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 28 | #include "llvm/CodeGen/MachineFunction.h" |
| 29 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 30 | #include "llvm/CodeGen/MachineMemOperand.h" |
| 31 | #include "llvm/CodeGen/MachineOperand.h" |
Diana Picus | 1437f6d | 2016-12-19 11:55:41 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
| 34 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
Craig Topper | 2fa1436 | 2018-03-29 17:21:10 +0000 | [diff] [blame] | 35 | #include "llvm/CodeGen/ValueTypes.h" |
Eugene Zelenko | 076468c | 2017-09-20 21:35:51 +0000 | [diff] [blame] | 36 | #include "llvm/IR/Attributes.h" |
| 37 | #include "llvm/IR/DataLayout.h" |
| 38 | #include "llvm/IR/DerivedTypes.h" |
| 39 | #include "llvm/IR/Function.h" |
| 40 | #include "llvm/IR/Type.h" |
| 41 | #include "llvm/IR/Value.h" |
| 42 | #include "llvm/Support/Casting.h" |
| 43 | #include "llvm/Support/LowLevelTypeImpl.h" |
David Blaikie | 13e77db | 2018-03-23 23:58:25 +0000 | [diff] [blame] | 44 | #include "llvm/Support/MachineValueType.h" |
Eugene Zelenko | 076468c | 2017-09-20 21:35:51 +0000 | [diff] [blame] | 45 | #include <algorithm> |
| 46 | #include <cassert> |
| 47 | #include <cstdint> |
| 48 | #include <utility> |
Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 49 | |
| 50 | using namespace llvm; |
| 51 | |
Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 52 | ARMCallLowering::ARMCallLowering(const ARMTargetLowering &TLI) |
| 53 | : CallLowering(&TLI) {} |
| 54 | |
Benjamin Kramer | 061f4a5 | 2017-01-13 14:39:03 +0000 | [diff] [blame] | 55 | static bool isSupportedType(const DataLayout &DL, const ARMTargetLowering &TLI, |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 56 | Type *T) { |
Diana Picus | 8fd1601 | 2017-06-15 09:42:02 +0000 | [diff] [blame] | 57 | if (T->isArrayTy()) |
Diana Picus | 1e88ac2 | 2019-04-30 09:05:25 +0000 | [diff] [blame] | 58 | return isSupportedType(DL, TLI, T->getArrayElementType()); |
Diana Picus | 8cca8cb | 2017-05-29 07:01:52 +0000 | [diff] [blame] | 59 | |
Diana Picus | 8fd1601 | 2017-06-15 09:42:02 +0000 | [diff] [blame] | 60 | if (T->isStructTy()) { |
| 61 | // For now we only allow homogeneous structs that we can manipulate with |
| 62 | // G_MERGE_VALUES and G_UNMERGE_VALUES |
| 63 | auto StructT = cast<StructType>(T); |
| 64 | for (unsigned i = 1, e = StructT->getNumElements(); i != e; ++i) |
| 65 | if (StructT->getElementType(i) != StructT->getElementType(0)) |
| 66 | return false; |
Diana Picus | 1e88ac2 | 2019-04-30 09:05:25 +0000 | [diff] [blame] | 67 | return isSupportedType(DL, TLI, StructT->getElementType(0)); |
Diana Picus | 8fd1601 | 2017-06-15 09:42:02 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Diana Picus | 0c11c7b | 2017-02-02 14:00:54 +0000 | [diff] [blame] | 70 | EVT VT = TLI.getValueType(DL, T, true); |
Diana Picus | f941ec0 | 2017-04-21 11:53:01 +0000 | [diff] [blame] | 71 | if (!VT.isSimple() || VT.isVector() || |
| 72 | !(VT.isInteger() || VT.isFloatingPoint())) |
Diana Picus | 97ae95c | 2016-12-19 14:08:02 +0000 | [diff] [blame] | 73 | return false; |
| 74 | |
| 75 | unsigned VTSize = VT.getSimpleVT().getSizeInBits(); |
Diana Picus | ca6a890 | 2017-02-16 07:53:07 +0000 | [diff] [blame] | 76 | |
| 77 | if (VTSize == 64) |
| 78 | // FIXME: Support i64 too |
| 79 | return VT.isFloatingPoint(); |
| 80 | |
Diana Picus | d83df5d | 2017-01-25 08:47:40 +0000 | [diff] [blame] | 81 | return VTSize == 1 || VTSize == 8 || VTSize == 16 || VTSize == 32; |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | namespace { |
Eugene Zelenko | 076468c | 2017-09-20 21:35:51 +0000 | [diff] [blame] | 85 | |
Diana Picus | a606713 | 2017-02-23 13:25:43 +0000 | [diff] [blame] | 86 | /// Helper class for values going out through an ABI boundary (used for handling |
| 87 | /// function return values and call parameters). |
| 88 | struct OutgoingValueHandler : public CallLowering::ValueHandler { |
| 89 | OutgoingValueHandler(MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI, |
| 90 | MachineInstrBuilder &MIB, CCAssignFn *AssignFn) |
Eugene Zelenko | 076468c | 2017-09-20 21:35:51 +0000 | [diff] [blame] | 91 | : ValueHandler(MIRBuilder, MRI, AssignFn), MIB(MIB) {} |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 92 | |
Matt Arsenault | faeaedf | 2019-06-24 16:16:12 +0000 | [diff] [blame] | 93 | Register getStackAddress(uint64_t Size, int64_t Offset, |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 94 | MachinePointerInfo &MPO) override { |
Diana Picus | 3841522 | 2017-03-01 15:54:21 +0000 | [diff] [blame] | 95 | assert((Size == 1 || Size == 2 || Size == 4 || Size == 8) && |
| 96 | "Unsupported size"); |
Diana Picus | 1ffca2a | 2017-02-28 14:17:53 +0000 | [diff] [blame] | 97 | |
| 98 | LLT p0 = LLT::pointer(0, 32); |
| 99 | LLT s32 = LLT::scalar(32); |
Matt Arsenault | faeaedf | 2019-06-24 16:16:12 +0000 | [diff] [blame] | 100 | Register SPReg = MRI.createGenericVirtualRegister(p0); |
| 101 | MIRBuilder.buildCopy(SPReg, Register(ARM::SP)); |
Diana Picus | 1ffca2a | 2017-02-28 14:17:53 +0000 | [diff] [blame] | 102 | |
Matt Arsenault | faeaedf | 2019-06-24 16:16:12 +0000 | [diff] [blame] | 103 | Register OffsetReg = MRI.createGenericVirtualRegister(s32); |
Diana Picus | 1ffca2a | 2017-02-28 14:17:53 +0000 | [diff] [blame] | 104 | MIRBuilder.buildConstant(OffsetReg, Offset); |
| 105 | |
Matt Arsenault | faeaedf | 2019-06-24 16:16:12 +0000 | [diff] [blame] | 106 | Register AddrReg = MRI.createGenericVirtualRegister(p0); |
Diana Picus | 1ffca2a | 2017-02-28 14:17:53 +0000 | [diff] [blame] | 107 | MIRBuilder.buildGEP(AddrReg, SPReg, OffsetReg); |
| 108 | |
| 109 | MPO = MachinePointerInfo::getStack(MIRBuilder.getMF(), Offset); |
Diana Picus | 1ffca2a | 2017-02-28 14:17:53 +0000 | [diff] [blame] | 110 | return AddrReg; |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Matt Arsenault | faeaedf | 2019-06-24 16:16:12 +0000 | [diff] [blame] | 113 | void assignValueToReg(Register ValVReg, Register PhysReg, |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 114 | CCValAssign &VA) override { |
| 115 | assert(VA.isRegLoc() && "Value shouldn't be assigned to reg"); |
| 116 | assert(VA.getLocReg() == PhysReg && "Assigning to the wrong reg?"); |
| 117 | |
Diana Picus | ca6a890 | 2017-02-16 07:53:07 +0000 | [diff] [blame] | 118 | assert(VA.getValVT().getSizeInBits() <= 64 && "Unsupported value size"); |
| 119 | assert(VA.getLocVT().getSizeInBits() <= 64 && "Unsupported location size"); |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 120 | |
Matt Arsenault | faeaedf | 2019-06-24 16:16:12 +0000 | [diff] [blame] | 121 | Register ExtReg = extendRegister(ValVReg, VA); |
Diana Picus | 8b6c6be | 2017-01-25 08:10:40 +0000 | [diff] [blame] | 122 | MIRBuilder.buildCopy(PhysReg, ExtReg); |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 123 | MIB.addUse(PhysReg, RegState::Implicit); |
| 124 | } |
| 125 | |
Matt Arsenault | faeaedf | 2019-06-24 16:16:12 +0000 | [diff] [blame] | 126 | void assignValueToAddress(Register ValVReg, Register Addr, uint64_t Size, |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 127 | MachinePointerInfo &MPO, CCValAssign &VA) override { |
Diana Picus | 9c52309 | 2017-03-01 15:35:14 +0000 | [diff] [blame] | 128 | assert((Size == 1 || Size == 2 || Size == 4 || Size == 8) && |
| 129 | "Unsupported size"); |
Diana Picus | 1ffca2a | 2017-02-28 14:17:53 +0000 | [diff] [blame] | 130 | |
Matt Arsenault | faeaedf | 2019-06-24 16:16:12 +0000 | [diff] [blame] | 131 | Register ExtReg = extendRegister(ValVReg, VA); |
Diana Picus | 1ffca2a | 2017-02-28 14:17:53 +0000 | [diff] [blame] | 132 | auto MMO = MIRBuilder.getMF().getMachineMemOperand( |
Diana Picus | 9c52309 | 2017-03-01 15:35:14 +0000 | [diff] [blame] | 133 | MPO, MachineMemOperand::MOStore, VA.getLocVT().getStoreSize(), |
Matt Arsenault | 2a64598 | 2019-01-31 01:38:47 +0000 | [diff] [blame] | 134 | /* Alignment */ 1); |
Diana Picus | 9c52309 | 2017-03-01 15:35:14 +0000 | [diff] [blame] | 135 | MIRBuilder.buildStore(ExtReg, Addr, *MMO); |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Diana Picus | ca6a890 | 2017-02-16 07:53:07 +0000 | [diff] [blame] | 138 | unsigned assignCustomValue(const CallLowering::ArgInfo &Arg, |
| 139 | ArrayRef<CCValAssign> VAs) override { |
Diana Picus | 69ce1c13 | 2019-06-27 08:50:53 +0000 | [diff] [blame^] | 140 | assert(Arg.Regs.size() == 1 && "Can't handle multple regs yet"); |
| 141 | |
Diana Picus | ca6a890 | 2017-02-16 07:53:07 +0000 | [diff] [blame] | 142 | CCValAssign VA = VAs[0]; |
| 143 | assert(VA.needsCustom() && "Value doesn't need custom handling"); |
| 144 | assert(VA.getValVT() == MVT::f64 && "Unsupported type"); |
| 145 | |
| 146 | CCValAssign NextVA = VAs[1]; |
| 147 | assert(NextVA.needsCustom() && "Value doesn't need custom handling"); |
| 148 | assert(NextVA.getValVT() == MVT::f64 && "Unsupported type"); |
| 149 | |
| 150 | assert(VA.getValNo() == NextVA.getValNo() && |
| 151 | "Values belong to different arguments"); |
| 152 | |
| 153 | assert(VA.isRegLoc() && "Value should be in reg"); |
| 154 | assert(NextVA.isRegLoc() && "Value should be in reg"); |
| 155 | |
Matt Arsenault | e3a676e | 2019-06-24 15:50:29 +0000 | [diff] [blame] | 156 | Register NewRegs[] = {MRI.createGenericVirtualRegister(LLT::scalar(32)), |
Diana Picus | ca6a890 | 2017-02-16 07:53:07 +0000 | [diff] [blame] | 157 | MRI.createGenericVirtualRegister(LLT::scalar(32))}; |
Diana Picus | 69ce1c13 | 2019-06-27 08:50:53 +0000 | [diff] [blame^] | 158 | MIRBuilder.buildUnmerge(NewRegs, Arg.Regs[0]); |
Diana Picus | ca6a890 | 2017-02-16 07:53:07 +0000 | [diff] [blame] | 159 | |
| 160 | bool IsLittle = MIRBuilder.getMF().getSubtarget<ARMSubtarget>().isLittle(); |
| 161 | if (!IsLittle) |
| 162 | std::swap(NewRegs[0], NewRegs[1]); |
| 163 | |
| 164 | assignValueToReg(NewRegs[0], VA.getLocReg(), VA); |
| 165 | assignValueToReg(NewRegs[1], NextVA.getLocReg(), NextVA); |
| 166 | |
| 167 | return 1; |
| 168 | } |
| 169 | |
Diana Picus | 9c52309 | 2017-03-01 15:35:14 +0000 | [diff] [blame] | 170 | bool assignArg(unsigned ValNo, MVT ValVT, MVT LocVT, |
Diana Picus | 3841522 | 2017-03-01 15:54:21 +0000 | [diff] [blame] | 171 | CCValAssign::LocInfo LocInfo, |
| 172 | const CallLowering::ArgInfo &Info, CCState &State) override { |
Diana Picus | 9c52309 | 2017-03-01 15:35:14 +0000 | [diff] [blame] | 173 | if (AssignFn(ValNo, ValVT, LocVT, LocInfo, Info.Flags, State)) |
| 174 | return true; |
| 175 | |
Diana Picus | 3841522 | 2017-03-01 15:54:21 +0000 | [diff] [blame] | 176 | StackSize = |
| 177 | std::max(StackSize, static_cast<uint64_t>(State.getNextStackOffset())); |
Diana Picus | 9c52309 | 2017-03-01 15:35:14 +0000 | [diff] [blame] | 178 | return false; |
| 179 | } |
| 180 | |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 181 | MachineInstrBuilder &MIB; |
Eugene Zelenko | 076468c | 2017-09-20 21:35:51 +0000 | [diff] [blame] | 182 | uint64_t StackSize = 0; |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 183 | }; |
Eugene Zelenko | 076468c | 2017-09-20 21:35:51 +0000 | [diff] [blame] | 184 | |
| 185 | } // end anonymous namespace |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 186 | |
Diana Picus | 8cca8cb | 2017-05-29 07:01:52 +0000 | [diff] [blame] | 187 | void ARMCallLowering::splitToValueTypes( |
| 188 | const ArgInfo &OrigArg, SmallVectorImpl<ArgInfo> &SplitArgs, |
| 189 | MachineFunction &MF, const SplitArgTy &PerformArgSplit) const { |
Diana Picus | 32cd9b4 | 2017-02-02 14:01:00 +0000 | [diff] [blame] | 190 | const ARMTargetLowering &TLI = *getTLI<ARMTargetLowering>(); |
| 191 | LLVMContext &Ctx = OrigArg.Ty->getContext(); |
Diana Picus | 8cca8cb | 2017-05-29 07:01:52 +0000 | [diff] [blame] | 192 | const DataLayout &DL = MF.getDataLayout(); |
| 193 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 194 | const Function &F = MF.getFunction(); |
Diana Picus | 32cd9b4 | 2017-02-02 14:01:00 +0000 | [diff] [blame] | 195 | |
| 196 | SmallVector<EVT, 4> SplitVTs; |
Diana Picus | 68b20c5 | 2019-05-27 10:30:33 +0000 | [diff] [blame] | 197 | ComputeValueVTs(TLI, DL, OrigArg.Ty, SplitVTs, nullptr, nullptr, 0); |
Diana Picus | 69ce1c13 | 2019-06-27 08:50:53 +0000 | [diff] [blame^] | 198 | assert(OrigArg.Regs.size() == 1 && "Can't handle multple regs yet"); |
Diana Picus | 32cd9b4 | 2017-02-02 14:01:00 +0000 | [diff] [blame] | 199 | |
Diana Picus | 8cca8cb | 2017-05-29 07:01:52 +0000 | [diff] [blame] | 200 | if (SplitVTs.size() == 1) { |
| 201 | // Even if there is no splitting to do, we still want to replace the |
| 202 | // original type (e.g. pointer type -> integer). |
Diana Picus | e7aa909 | 2017-06-02 10:16:48 +0000 | [diff] [blame] | 203 | auto Flags = OrigArg.Flags; |
| 204 | unsigned OriginalAlignment = DL.getABITypeAlignment(OrigArg.Ty); |
| 205 | Flags.setOrigAlign(OriginalAlignment); |
Diana Picus | 69ce1c13 | 2019-06-27 08:50:53 +0000 | [diff] [blame^] | 206 | SplitArgs.emplace_back(OrigArg.Regs[0], SplitVTs[0].getTypeForEVT(Ctx), |
| 207 | Flags, OrigArg.IsFixed); |
Diana Picus | 8cca8cb | 2017-05-29 07:01:52 +0000 | [diff] [blame] | 208 | return; |
| 209 | } |
Diana Picus | 32cd9b4 | 2017-02-02 14:01:00 +0000 | [diff] [blame] | 210 | |
Diana Picus | 8cca8cb | 2017-05-29 07:01:52 +0000 | [diff] [blame] | 211 | for (unsigned i = 0, e = SplitVTs.size(); i != e; ++i) { |
| 212 | EVT SplitVT = SplitVTs[i]; |
| 213 | Type *SplitTy = SplitVT.getTypeForEVT(Ctx); |
| 214 | auto Flags = OrigArg.Flags; |
Diana Picus | e7aa909 | 2017-06-02 10:16:48 +0000 | [diff] [blame] | 215 | |
| 216 | unsigned OriginalAlignment = DL.getABITypeAlignment(SplitTy); |
| 217 | Flags.setOrigAlign(OriginalAlignment); |
| 218 | |
Diana Picus | 8cca8cb | 2017-05-29 07:01:52 +0000 | [diff] [blame] | 219 | bool NeedsConsecutiveRegisters = |
| 220 | TLI.functionArgumentNeedsConsecutiveRegisters( |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 221 | SplitTy, F.getCallingConv(), F.isVarArg()); |
Diana Picus | 8cca8cb | 2017-05-29 07:01:52 +0000 | [diff] [blame] | 222 | if (NeedsConsecutiveRegisters) { |
| 223 | Flags.setInConsecutiveRegs(); |
| 224 | if (i == e - 1) |
| 225 | Flags.setInConsecutiveRegsLast(); |
| 226 | } |
Diana Picus | e7aa909 | 2017-06-02 10:16:48 +0000 | [diff] [blame] | 227 | |
Diana Picus | 69ce1c13 | 2019-06-27 08:50:53 +0000 | [diff] [blame^] | 228 | Register PartReg = |
Diana Picus | 68b20c5 | 2019-05-27 10:30:33 +0000 | [diff] [blame] | 229 | MRI.createGenericVirtualRegister(getLLTForType(*SplitTy, DL)); |
| 230 | SplitArgs.push_back(ArgInfo{PartReg, SplitTy, Flags, OrigArg.IsFixed}); |
| 231 | PerformArgSplit(PartReg); |
Diana Picus | 8cca8cb | 2017-05-29 07:01:52 +0000 | [diff] [blame] | 232 | } |
Diana Picus | 32cd9b4 | 2017-02-02 14:01:00 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 235 | /// Lower the return value for the already existing \p Ret. This assumes that |
| 236 | /// \p MIRBuilder's insertion point is correct. |
| 237 | bool ARMCallLowering::lowerReturnVal(MachineIRBuilder &MIRBuilder, |
Matt Arsenault | e3a676e | 2019-06-24 15:50:29 +0000 | [diff] [blame] | 238 | const Value *Val, ArrayRef<Register> VRegs, |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 239 | MachineInstrBuilder &Ret) const { |
| 240 | if (!Val) |
| 241 | // Nothing to do here. |
| 242 | return true; |
| 243 | |
| 244 | auto &MF = MIRBuilder.getMF(); |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 245 | const auto &F = MF.getFunction(); |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 246 | |
| 247 | auto DL = MF.getDataLayout(); |
| 248 | auto &TLI = *getTLI<ARMTargetLowering>(); |
| 249 | if (!isSupportedType(DL, TLI, Val->getType())) |
Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 250 | return false; |
| 251 | |
Alexander Ivchenko | 49168f6 | 2018-08-02 08:33:31 +0000 | [diff] [blame] | 252 | SmallVector<EVT, 4> SplitEVTs; |
| 253 | ComputeValueVTs(TLI, DL, Val->getType(), SplitEVTs); |
| 254 | assert(VRegs.size() == SplitEVTs.size() && |
| 255 | "For each split Type there should be exactly one VReg."); |
Diana Picus | 32cd9b4 | 2017-02-02 14:01:00 +0000 | [diff] [blame] | 256 | |
Alexander Ivchenko | 49168f6 | 2018-08-02 08:33:31 +0000 | [diff] [blame] | 257 | SmallVector<ArgInfo, 4> SplitVTs; |
| 258 | LLVMContext &Ctx = Val->getType()->getContext(); |
| 259 | for (unsigned i = 0; i < SplitEVTs.size(); ++i) { |
| 260 | ArgInfo CurArgInfo(VRegs[i], SplitEVTs[i].getTypeForEVT(Ctx)); |
| 261 | setArgFlags(CurArgInfo, AttributeList::ReturnIndex, DL, F); |
| 262 | |
Matt Arsenault | e3a676e | 2019-06-24 15:50:29 +0000 | [diff] [blame] | 263 | SmallVector<Register, 4> Regs; |
Diana Picus | 68b20c5 | 2019-05-27 10:30:33 +0000 | [diff] [blame] | 264 | splitToValueTypes(CurArgInfo, SplitVTs, MF, |
Matt Arsenault | e3a676e | 2019-06-24 15:50:29 +0000 | [diff] [blame] | 265 | [&](Register Reg) { Regs.push_back(Reg); }); |
Alexander Ivchenko | 49168f6 | 2018-08-02 08:33:31 +0000 | [diff] [blame] | 266 | if (Regs.size() > 1) |
| 267 | MIRBuilder.buildUnmerge(Regs, VRegs[i]); |
| 268 | } |
Diana Picus | 8fd1601 | 2017-06-15 09:42:02 +0000 | [diff] [blame] | 269 | |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 270 | CCAssignFn *AssignFn = |
| 271 | TLI.CCAssignFnForReturn(F.getCallingConv(), F.isVarArg()); |
Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 272 | |
Diana Picus | a606713 | 2017-02-23 13:25:43 +0000 | [diff] [blame] | 273 | OutgoingValueHandler RetHandler(MIRBuilder, MF.getRegInfo(), Ret, AssignFn); |
Diana Picus | 32cd9b4 | 2017-02-02 14:01:00 +0000 | [diff] [blame] | 274 | return handleAssignments(MIRBuilder, SplitVTs, RetHandler); |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | bool ARMCallLowering::lowerReturn(MachineIRBuilder &MIRBuilder, |
Alexander Ivchenko | 49168f6 | 2018-08-02 08:33:31 +0000 | [diff] [blame] | 278 | const Value *Val, |
Matt Arsenault | e3a676e | 2019-06-24 15:50:29 +0000 | [diff] [blame] | 279 | ArrayRef<Register> VRegs) const { |
Alexander Ivchenko | 49168f6 | 2018-08-02 08:33:31 +0000 | [diff] [blame] | 280 | assert(!Val == VRegs.empty() && "Return value without a vreg"); |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 281 | |
Joerg Sonnenberger | 0f76a35 | 2017-08-28 20:20:47 +0000 | [diff] [blame] | 282 | auto const &ST = MIRBuilder.getMF().getSubtarget<ARMSubtarget>(); |
| 283 | unsigned Opcode = ST.getReturnOpcode(); |
| 284 | auto Ret = MIRBuilder.buildInstrNoInsert(Opcode).add(predOps(ARMCC::AL)); |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 285 | |
Alexander Ivchenko | 49168f6 | 2018-08-02 08:33:31 +0000 | [diff] [blame] | 286 | if (!lowerReturnVal(MIRBuilder, Val, VRegs, Ret)) |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 287 | return false; |
| 288 | |
| 289 | MIRBuilder.insertInstr(Ret); |
Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 290 | return true; |
| 291 | } |
| 292 | |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 293 | namespace { |
Eugene Zelenko | 076468c | 2017-09-20 21:35:51 +0000 | [diff] [blame] | 294 | |
Diana Picus | a8cb0cd | 2017-02-23 14:18:41 +0000 | [diff] [blame] | 295 | /// Helper class for values coming in through an ABI boundary (used for handling |
| 296 | /// formal arguments and call return values). |
| 297 | struct IncomingValueHandler : public CallLowering::ValueHandler { |
| 298 | IncomingValueHandler(MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI, |
| 299 | CCAssignFn AssignFn) |
Tim Northover | d943354 | 2017-01-17 22:30:10 +0000 | [diff] [blame] | 300 | : ValueHandler(MIRBuilder, MRI, AssignFn) {} |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 301 | |
Amara Emerson | 2b523f8 | 2019-04-09 21:22:33 +0000 | [diff] [blame] | 302 | bool isArgumentHandler() const override { return true; } |
| 303 | |
Matt Arsenault | faeaedf | 2019-06-24 16:16:12 +0000 | [diff] [blame] | 304 | Register getStackAddress(uint64_t Size, int64_t Offset, |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 305 | MachinePointerInfo &MPO) override { |
Diana Picus | ca6a890 | 2017-02-16 07:53:07 +0000 | [diff] [blame] | 306 | assert((Size == 1 || Size == 2 || Size == 4 || Size == 8) && |
| 307 | "Unsupported size"); |
Diana Picus | 1437f6d | 2016-12-19 11:55:41 +0000 | [diff] [blame] | 308 | |
| 309 | auto &MFI = MIRBuilder.getMF().getFrameInfo(); |
| 310 | |
| 311 | int FI = MFI.CreateFixedObject(Size, Offset, true); |
| 312 | MPO = MachinePointerInfo::getFixedStack(MIRBuilder.getMF(), FI); |
| 313 | |
| 314 | unsigned AddrReg = |
| 315 | MRI.createGenericVirtualRegister(LLT::pointer(MPO.getAddrSpace(), 32)); |
| 316 | MIRBuilder.buildFrameIndex(AddrReg, FI); |
| 317 | |
| 318 | return AddrReg; |
| 319 | } |
| 320 | |
Matt Arsenault | faeaedf | 2019-06-24 16:16:12 +0000 | [diff] [blame] | 321 | void assignValueToAddress(Register ValVReg, Register Addr, uint64_t Size, |
Diana Picus | 1437f6d | 2016-12-19 11:55:41 +0000 | [diff] [blame] | 322 | MachinePointerInfo &MPO, CCValAssign &VA) override { |
Diana Picus | ca6a890 | 2017-02-16 07:53:07 +0000 | [diff] [blame] | 323 | assert((Size == 1 || Size == 2 || Size == 4 || Size == 8) && |
| 324 | "Unsupported size"); |
Diana Picus | 278c722 | 2017-01-26 09:20:47 +0000 | [diff] [blame] | 325 | |
| 326 | if (VA.getLocInfo() == CCValAssign::SExt || |
| 327 | VA.getLocInfo() == CCValAssign::ZExt) { |
Diana Picus | a8cb0cd | 2017-02-23 14:18:41 +0000 | [diff] [blame] | 328 | // If the value is zero- or sign-extended, its size becomes 4 bytes, so |
| 329 | // that's what we should load. |
Diana Picus | 278c722 | 2017-01-26 09:20:47 +0000 | [diff] [blame] | 330 | Size = 4; |
| 331 | assert(MRI.getType(ValVReg).isScalar() && "Only scalars supported atm"); |
Diana Picus | 1437f6d | 2016-12-19 11:55:41 +0000 | [diff] [blame] | 332 | |
Diana Picus | 4f46be3 | 2017-04-27 10:23:30 +0000 | [diff] [blame] | 333 | auto LoadVReg = MRI.createGenericVirtualRegister(LLT::scalar(32)); |
Matt Arsenault | 2a64598 | 2019-01-31 01:38:47 +0000 | [diff] [blame] | 334 | buildLoad(LoadVReg, Addr, Size, /* Alignment */ 1, MPO); |
Diana Picus | 4f46be3 | 2017-04-27 10:23:30 +0000 | [diff] [blame] | 335 | MIRBuilder.buildTrunc(ValVReg, LoadVReg); |
| 336 | } else { |
| 337 | // If the value is not extended, a simple load will suffice. |
Matt Arsenault | 2a64598 | 2019-01-31 01:38:47 +0000 | [diff] [blame] | 338 | buildLoad(ValVReg, Addr, Size, /* Alignment */ 1, MPO); |
Diana Picus | 4f46be3 | 2017-04-27 10:23:30 +0000 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | |
Matt Arsenault | faeaedf | 2019-06-24 16:16:12 +0000 | [diff] [blame] | 342 | void buildLoad(Register Val, Register Addr, uint64_t Size, unsigned Alignment, |
Diana Picus | 4f46be3 | 2017-04-27 10:23:30 +0000 | [diff] [blame] | 343 | MachinePointerInfo &MPO) { |
Diana Picus | 1437f6d | 2016-12-19 11:55:41 +0000 | [diff] [blame] | 344 | auto MMO = MIRBuilder.getMF().getMachineMemOperand( |
Diana Picus | 4f46be3 | 2017-04-27 10:23:30 +0000 | [diff] [blame] | 345 | MPO, MachineMemOperand::MOLoad, Size, Alignment); |
| 346 | MIRBuilder.buildLoad(Val, Addr, *MMO); |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Matt Arsenault | faeaedf | 2019-06-24 16:16:12 +0000 | [diff] [blame] | 349 | void assignValueToReg(Register ValVReg, Register PhysReg, |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 350 | CCValAssign &VA) override { |
| 351 | assert(VA.isRegLoc() && "Value shouldn't be assigned to reg"); |
| 352 | assert(VA.getLocReg() == PhysReg && "Assigning to the wrong reg?"); |
| 353 | |
Aditya Nandakumar | c3bfc81 | 2017-10-09 20:07:43 +0000 | [diff] [blame] | 354 | auto ValSize = VA.getValVT().getSizeInBits(); |
| 355 | auto LocSize = VA.getLocVT().getSizeInBits(); |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 356 | |
Aditya Nandakumar | c3bfc81 | 2017-10-09 20:07:43 +0000 | [diff] [blame] | 357 | assert(ValSize <= 64 && "Unsupported value size"); |
| 358 | assert(LocSize <= 64 && "Unsupported location size"); |
| 359 | |
Diana Picus | a8cb0cd | 2017-02-23 14:18:41 +0000 | [diff] [blame] | 360 | markPhysRegUsed(PhysReg); |
Aditya Nandakumar | c3bfc81 | 2017-10-09 20:07:43 +0000 | [diff] [blame] | 361 | if (ValSize == LocSize) { |
| 362 | MIRBuilder.buildCopy(ValVReg, PhysReg); |
| 363 | } else { |
| 364 | assert(ValSize < LocSize && "Extensions not supported"); |
| 365 | |
| 366 | // We cannot create a truncating copy, nor a trunc of a physical register. |
| 367 | // Therefore, we need to copy the content of the physical register into a |
| 368 | // virtual one and then truncate that. |
| 369 | auto PhysRegToVReg = |
| 370 | MRI.createGenericVirtualRegister(LLT::scalar(LocSize)); |
| 371 | MIRBuilder.buildCopy(PhysRegToVReg, PhysReg); |
| 372 | MIRBuilder.buildTrunc(ValVReg, PhysRegToVReg); |
| 373 | } |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 374 | } |
Diana Picus | ca6a890 | 2017-02-16 07:53:07 +0000 | [diff] [blame] | 375 | |
Diana Picus | a606713 | 2017-02-23 13:25:43 +0000 | [diff] [blame] | 376 | unsigned assignCustomValue(const ARMCallLowering::ArgInfo &Arg, |
Diana Picus | ca6a890 | 2017-02-16 07:53:07 +0000 | [diff] [blame] | 377 | ArrayRef<CCValAssign> VAs) override { |
Diana Picus | 69ce1c13 | 2019-06-27 08:50:53 +0000 | [diff] [blame^] | 378 | assert(Arg.Regs.size() == 1 && "Can't handle multple regs yet"); |
| 379 | |
Diana Picus | ca6a890 | 2017-02-16 07:53:07 +0000 | [diff] [blame] | 380 | CCValAssign VA = VAs[0]; |
| 381 | assert(VA.needsCustom() && "Value doesn't need custom handling"); |
| 382 | assert(VA.getValVT() == MVT::f64 && "Unsupported type"); |
| 383 | |
| 384 | CCValAssign NextVA = VAs[1]; |
| 385 | assert(NextVA.needsCustom() && "Value doesn't need custom handling"); |
| 386 | assert(NextVA.getValVT() == MVT::f64 && "Unsupported type"); |
| 387 | |
| 388 | assert(VA.getValNo() == NextVA.getValNo() && |
| 389 | "Values belong to different arguments"); |
| 390 | |
| 391 | assert(VA.isRegLoc() && "Value should be in reg"); |
| 392 | assert(NextVA.isRegLoc() && "Value should be in reg"); |
| 393 | |
Matt Arsenault | e3a676e | 2019-06-24 15:50:29 +0000 | [diff] [blame] | 394 | Register NewRegs[] = {MRI.createGenericVirtualRegister(LLT::scalar(32)), |
Diana Picus | ca6a890 | 2017-02-16 07:53:07 +0000 | [diff] [blame] | 395 | MRI.createGenericVirtualRegister(LLT::scalar(32))}; |
| 396 | |
| 397 | assignValueToReg(NewRegs[0], VA.getLocReg(), VA); |
| 398 | assignValueToReg(NewRegs[1], NextVA.getLocReg(), NextVA); |
| 399 | |
| 400 | bool IsLittle = MIRBuilder.getMF().getSubtarget<ARMSubtarget>().isLittle(); |
| 401 | if (!IsLittle) |
| 402 | std::swap(NewRegs[0], NewRegs[1]); |
| 403 | |
Diana Picus | 69ce1c13 | 2019-06-27 08:50:53 +0000 | [diff] [blame^] | 404 | MIRBuilder.buildMerge(Arg.Regs[0], NewRegs); |
Diana Picus | ca6a890 | 2017-02-16 07:53:07 +0000 | [diff] [blame] | 405 | |
| 406 | return 1; |
| 407 | } |
Diana Picus | a8cb0cd | 2017-02-23 14:18:41 +0000 | [diff] [blame] | 408 | |
| 409 | /// Marking a physical register as used is different between formal |
| 410 | /// parameters, where it's a basic block live-in, and call returns, where it's |
| 411 | /// an implicit-def of the call instruction. |
| 412 | virtual void markPhysRegUsed(unsigned PhysReg) = 0; |
| 413 | }; |
| 414 | |
| 415 | struct FormalArgHandler : public IncomingValueHandler { |
| 416 | FormalArgHandler(MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI, |
| 417 | CCAssignFn AssignFn) |
| 418 | : IncomingValueHandler(MIRBuilder, MRI, AssignFn) {} |
| 419 | |
| 420 | void markPhysRegUsed(unsigned PhysReg) override { |
| 421 | MIRBuilder.getMBB().addLiveIn(PhysReg); |
| 422 | } |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 423 | }; |
Eugene Zelenko | 076468c | 2017-09-20 21:35:51 +0000 | [diff] [blame] | 424 | |
| 425 | } // end anonymous namespace |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 426 | |
Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 427 | bool ARMCallLowering::lowerFormalArguments(MachineIRBuilder &MIRBuilder, |
| 428 | const Function &F, |
Matt Arsenault | e3a676e | 2019-06-24 15:50:29 +0000 | [diff] [blame] | 429 | ArrayRef<Register> VRegs) const { |
Diana Picus | acf4bf2 | 2017-11-03 10:30:12 +0000 | [diff] [blame] | 430 | auto &TLI = *getTLI<ARMTargetLowering>(); |
| 431 | auto Subtarget = TLI.getSubtarget(); |
| 432 | |
Diana Picus | 8a1b4f5 | 2018-12-05 10:35:28 +0000 | [diff] [blame] | 433 | if (Subtarget->isThumb1Only()) |
Diana Picus | acf4bf2 | 2017-11-03 10:30:12 +0000 | [diff] [blame] | 434 | return false; |
| 435 | |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 436 | // Quick exit if there aren't any args |
| 437 | if (F.arg_empty()) |
| 438 | return true; |
| 439 | |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 440 | if (F.isVarArg()) |
| 441 | return false; |
| 442 | |
Diana Picus | 32cd9b4 | 2017-02-02 14:01:00 +0000 | [diff] [blame] | 443 | auto &MF = MIRBuilder.getMF(); |
Diana Picus | 8cca8cb | 2017-05-29 07:01:52 +0000 | [diff] [blame] | 444 | auto &MBB = MIRBuilder.getMBB(); |
Diana Picus | 32cd9b4 | 2017-02-02 14:01:00 +0000 | [diff] [blame] | 445 | auto DL = MF.getDataLayout(); |
Diana Picus | 7232af3 | 2017-02-09 13:09:59 +0000 | [diff] [blame] | 446 | |
Diana Picus | f003d9f | 2017-11-30 12:23:44 +0000 | [diff] [blame] | 447 | for (auto &Arg : F.args()) { |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 448 | if (!isSupportedType(DL, TLI, Arg.getType())) |
| 449 | return false; |
Diana Picus | f003d9f | 2017-11-30 12:23:44 +0000 | [diff] [blame] | 450 | if (Arg.hasByValOrInAllocaAttr()) |
| 451 | return false; |
| 452 | } |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 453 | |
| 454 | CCAssignFn *AssignFn = |
| 455 | TLI.CCAssignFnForCall(F.getCallingConv(), F.isVarArg()); |
| 456 | |
Diana Picus | 0c05cce | 2017-05-29 09:09:54 +0000 | [diff] [blame] | 457 | FormalArgHandler ArgHandler(MIRBuilder, MIRBuilder.getMF().getRegInfo(), |
| 458 | AssignFn); |
| 459 | |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 460 | SmallVector<ArgInfo, 8> ArgInfos; |
Matt Arsenault | e3a676e | 2019-06-24 15:50:29 +0000 | [diff] [blame] | 461 | SmallVector<Register, 4> SplitRegs; |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 462 | unsigned Idx = 0; |
Reid Kleckner | 45707d4 | 2017-03-16 22:59:15 +0000 | [diff] [blame] | 463 | for (auto &Arg : F.args()) { |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 464 | ArgInfo AInfo(VRegs[Idx], Arg.getType()); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 465 | setArgFlags(AInfo, Idx + AttributeList::FirstArgIndex, DL, F); |
Diana Picus | 8cca8cb | 2017-05-29 07:01:52 +0000 | [diff] [blame] | 466 | |
Diana Picus | 0c05cce | 2017-05-29 09:09:54 +0000 | [diff] [blame] | 467 | SplitRegs.clear(); |
Diana Picus | 0c05cce | 2017-05-29 09:09:54 +0000 | [diff] [blame] | 468 | |
Diana Picus | 68b20c5 | 2019-05-27 10:30:33 +0000 | [diff] [blame] | 469 | splitToValueTypes(AInfo, ArgInfos, MF, |
Matt Arsenault | e3a676e | 2019-06-24 15:50:29 +0000 | [diff] [blame] | 470 | [&](Register Reg) { SplitRegs.push_back(Reg); }); |
Diana Picus | 0c05cce | 2017-05-29 09:09:54 +0000 | [diff] [blame] | 471 | |
| 472 | if (!SplitRegs.empty()) |
Diana Picus | 8fd1601 | 2017-06-15 09:42:02 +0000 | [diff] [blame] | 473 | MIRBuilder.buildMerge(VRegs[Idx], SplitRegs); |
Diana Picus | 8cca8cb | 2017-05-29 07:01:52 +0000 | [diff] [blame] | 474 | |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 475 | Idx++; |
| 476 | } |
| 477 | |
Diana Picus | 8cca8cb | 2017-05-29 07:01:52 +0000 | [diff] [blame] | 478 | if (!MBB.empty()) |
| 479 | MIRBuilder.setInstr(*MBB.begin()); |
| 480 | |
Amara Emerson | 0d6a26d | 2018-05-16 10:32:02 +0000 | [diff] [blame] | 481 | if (!handleAssignments(MIRBuilder, ArgInfos, ArgHandler)) |
| 482 | return false; |
| 483 | |
| 484 | // Move back to the end of the basic block. |
| 485 | MIRBuilder.setMBB(MBB); |
| 486 | return true; |
Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 487 | } |
Diana Picus | 613b656 | 2017-02-21 11:33:59 +0000 | [diff] [blame] | 488 | |
Diana Picus | a8cb0cd | 2017-02-23 14:18:41 +0000 | [diff] [blame] | 489 | namespace { |
Eugene Zelenko | 076468c | 2017-09-20 21:35:51 +0000 | [diff] [blame] | 490 | |
Diana Picus | a8cb0cd | 2017-02-23 14:18:41 +0000 | [diff] [blame] | 491 | struct CallReturnHandler : public IncomingValueHandler { |
| 492 | CallReturnHandler(MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI, |
| 493 | MachineInstrBuilder MIB, CCAssignFn *AssignFn) |
| 494 | : IncomingValueHandler(MIRBuilder, MRI, AssignFn), MIB(MIB) {} |
| 495 | |
| 496 | void markPhysRegUsed(unsigned PhysReg) override { |
| 497 | MIB.addDef(PhysReg, RegState::Implicit); |
| 498 | } |
| 499 | |
| 500 | MachineInstrBuilder MIB; |
| 501 | }; |
Eugene Zelenko | 076468c | 2017-09-20 21:35:51 +0000 | [diff] [blame] | 502 | |
Diana Picus | 8a1b4f5 | 2018-12-05 10:35:28 +0000 | [diff] [blame] | 503 | // FIXME: This should move to the ARMSubtarget when it supports all the opcodes. |
| 504 | unsigned getCallOpcode(const ARMSubtarget &STI, bool isDirect) { |
| 505 | if (isDirect) |
| 506 | return STI.isThumb() ? ARM::tBL : ARM::BL; |
| 507 | |
| 508 | if (STI.isThumb()) |
| 509 | return ARM::tBLXr; |
| 510 | |
| 511 | if (STI.hasV5TOps()) |
| 512 | return ARM::BLX; |
| 513 | |
| 514 | if (STI.hasV4TOps()) |
| 515 | return ARM::BX_CALL; |
| 516 | |
| 517 | return ARM::BMOVPCRX_CALL; |
| 518 | } |
Eugene Zelenko | 076468c | 2017-09-20 21:35:51 +0000 | [diff] [blame] | 519 | } // end anonymous namespace |
Diana Picus | a8cb0cd | 2017-02-23 14:18:41 +0000 | [diff] [blame] | 520 | |
Diana Picus | 613b656 | 2017-02-21 11:33:59 +0000 | [diff] [blame] | 521 | bool ARMCallLowering::lowerCall(MachineIRBuilder &MIRBuilder, |
Diana Picus | d79253a | 2017-03-20 14:40:18 +0000 | [diff] [blame] | 522 | CallingConv::ID CallConv, |
Diana Picus | 613b656 | 2017-02-21 11:33:59 +0000 | [diff] [blame] | 523 | const MachineOperand &Callee, |
| 524 | const ArgInfo &OrigRet, |
| 525 | ArrayRef<ArgInfo> OrigArgs) const { |
Diana Picus | a606713 | 2017-02-23 13:25:43 +0000 | [diff] [blame] | 526 | MachineFunction &MF = MIRBuilder.getMF(); |
| 527 | const auto &TLI = *getTLI<ARMTargetLowering>(); |
| 528 | const auto &DL = MF.getDataLayout(); |
Diana Picus | b350221 | 2017-10-25 11:42:40 +0000 | [diff] [blame] | 529 | const auto &STI = MF.getSubtarget<ARMSubtarget>(); |
Diana Picus | 0091cc3 | 2017-06-05 12:54:53 +0000 | [diff] [blame] | 530 | const TargetRegisterInfo *TRI = STI.getRegisterInfo(); |
Diana Picus | a606713 | 2017-02-23 13:25:43 +0000 | [diff] [blame] | 531 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
Diana Picus | 613b656 | 2017-02-21 11:33:59 +0000 | [diff] [blame] | 532 | |
Diana Picus | b350221 | 2017-10-25 11:42:40 +0000 | [diff] [blame] | 533 | if (STI.genLongCalls()) |
Diana Picus | 613b656 | 2017-02-21 11:33:59 +0000 | [diff] [blame] | 534 | return false; |
| 535 | |
Diana Picus | 8a1b4f5 | 2018-12-05 10:35:28 +0000 | [diff] [blame] | 536 | if (STI.isThumb1Only()) |
| 537 | return false; |
| 538 | |
Diana Picus | 1ffca2a | 2017-02-28 14:17:53 +0000 | [diff] [blame] | 539 | auto CallSeqStart = MIRBuilder.buildInstr(ARM::ADJCALLSTACKDOWN); |
Diana Picus | 613b656 | 2017-02-21 11:33:59 +0000 | [diff] [blame] | 540 | |
Diana Picus | a606713 | 2017-02-23 13:25:43 +0000 | [diff] [blame] | 541 | // Create the call instruction so we can add the implicit uses of arg |
| 542 | // registers, but don't insert it yet. |
Diana Picus | 639e066 | 2019-01-17 10:11:59 +0000 | [diff] [blame] | 543 | bool IsDirect = !Callee.isReg(); |
| 544 | auto CallOpcode = getCallOpcode(STI, IsDirect); |
Diana Picus | 8a1b4f5 | 2018-12-05 10:35:28 +0000 | [diff] [blame] | 545 | auto MIB = MIRBuilder.buildInstrNoInsert(CallOpcode); |
| 546 | |
Diana Picus | 639e066 | 2019-01-17 10:11:59 +0000 | [diff] [blame] | 547 | bool IsThumb = STI.isThumb(); |
| 548 | if (IsThumb) |
Diana Picus | 8a1b4f5 | 2018-12-05 10:35:28 +0000 | [diff] [blame] | 549 | MIB.add(predOps(ARMCC::AL)); |
| 550 | |
| 551 | MIB.add(Callee); |
Diana Picus | 639e066 | 2019-01-17 10:11:59 +0000 | [diff] [blame] | 552 | if (!IsDirect) { |
Diana Picus | 0091cc3 | 2017-06-05 12:54:53 +0000 | [diff] [blame] | 553 | auto CalleeReg = Callee.getReg(); |
Diana Picus | 8a1b4f5 | 2018-12-05 10:35:28 +0000 | [diff] [blame] | 554 | if (CalleeReg && !TRI->isPhysicalRegister(CalleeReg)) { |
Diana Picus | 639e066 | 2019-01-17 10:11:59 +0000 | [diff] [blame] | 555 | unsigned CalleeIdx = IsThumb ? 2 : 0; |
Diana Picus | 8a1b4f5 | 2018-12-05 10:35:28 +0000 | [diff] [blame] | 556 | MIB->getOperand(CalleeIdx).setReg(constrainOperandRegClass( |
Diana Picus | 0091cc3 | 2017-06-05 12:54:53 +0000 | [diff] [blame] | 557 | MF, *TRI, MRI, *STI.getInstrInfo(), *STI.getRegBankInfo(), |
Diana Picus | 8a1b4f5 | 2018-12-05 10:35:28 +0000 | [diff] [blame] | 558 | *MIB.getInstr(), MIB->getDesc(), Callee, CalleeIdx)); |
| 559 | } |
Diana Picus | 0091cc3 | 2017-06-05 12:54:53 +0000 | [diff] [blame] | 560 | } |
Diana Picus | a606713 | 2017-02-23 13:25:43 +0000 | [diff] [blame] | 561 | |
Diana Picus | 8a1b4f5 | 2018-12-05 10:35:28 +0000 | [diff] [blame] | 562 | MIB.addRegMask(TRI->getCallPreservedMask(MF, CallConv)); |
| 563 | |
Diana Picus | d5c2499 | 2019-01-17 10:11:55 +0000 | [diff] [blame] | 564 | bool IsVarArg = false; |
Diana Picus | a606713 | 2017-02-23 13:25:43 +0000 | [diff] [blame] | 565 | SmallVector<ArgInfo, 8> ArgInfos; |
| 566 | for (auto Arg : OrigArgs) { |
| 567 | if (!isSupportedType(DL, TLI, Arg.Ty)) |
| 568 | return false; |
| 569 | |
| 570 | if (!Arg.IsFixed) |
Diana Picus | d5c2499 | 2019-01-17 10:11:55 +0000 | [diff] [blame] | 571 | IsVarArg = true; |
Diana Picus | a606713 | 2017-02-23 13:25:43 +0000 | [diff] [blame] | 572 | |
Diana Picus | f003d9f | 2017-11-30 12:23:44 +0000 | [diff] [blame] | 573 | if (Arg.Flags.isByVal()) |
| 574 | return false; |
| 575 | |
Diana Picus | 69ce1c13 | 2019-06-27 08:50:53 +0000 | [diff] [blame^] | 576 | assert(Arg.Regs.size() == 1 && "Can't handle multple regs yet"); |
| 577 | |
Matt Arsenault | e3a676e | 2019-06-24 15:50:29 +0000 | [diff] [blame] | 578 | SmallVector<Register, 8> Regs; |
Diana Picus | 68b20c5 | 2019-05-27 10:30:33 +0000 | [diff] [blame] | 579 | splitToValueTypes(Arg, ArgInfos, MF, |
| 580 | [&](unsigned Reg) { Regs.push_back(Reg); }); |
Diana Picus | 8fd1601 | 2017-06-15 09:42:02 +0000 | [diff] [blame] | 581 | |
| 582 | if (Regs.size() > 1) |
Diana Picus | 69ce1c13 | 2019-06-27 08:50:53 +0000 | [diff] [blame^] | 583 | MIRBuilder.buildUnmerge(Regs, Arg.Regs[0]); |
Diana Picus | a606713 | 2017-02-23 13:25:43 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Diana Picus | d5c2499 | 2019-01-17 10:11:55 +0000 | [diff] [blame] | 586 | auto ArgAssignFn = TLI.CCAssignFnForCall(CallConv, IsVarArg); |
Diana Picus | a606713 | 2017-02-23 13:25:43 +0000 | [diff] [blame] | 587 | OutgoingValueHandler ArgHandler(MIRBuilder, MRI, MIB, ArgAssignFn); |
| 588 | if (!handleAssignments(MIRBuilder, ArgInfos, ArgHandler)) |
| 589 | return false; |
| 590 | |
| 591 | // Now we can add the actual call instruction to the correct basic block. |
| 592 | MIRBuilder.insertInstr(MIB); |
Diana Picus | 613b656 | 2017-02-21 11:33:59 +0000 | [diff] [blame] | 593 | |
Diana Picus | a8cb0cd | 2017-02-23 14:18:41 +0000 | [diff] [blame] | 594 | if (!OrigRet.Ty->isVoidTy()) { |
| 595 | if (!isSupportedType(DL, TLI, OrigRet.Ty)) |
| 596 | return false; |
| 597 | |
| 598 | ArgInfos.clear(); |
Matt Arsenault | e3a676e | 2019-06-24 15:50:29 +0000 | [diff] [blame] | 599 | SmallVector<Register, 8> SplitRegs; |
Diana Picus | 8cca8cb | 2017-05-29 07:01:52 +0000 | [diff] [blame] | 600 | splitToValueTypes(OrigRet, ArgInfos, MF, |
Matt Arsenault | e3a676e | 2019-06-24 15:50:29 +0000 | [diff] [blame] | 601 | [&](Register Reg) { SplitRegs.push_back(Reg); }); |
Diana Picus | a8cb0cd | 2017-02-23 14:18:41 +0000 | [diff] [blame] | 602 | |
Diana Picus | d5c2499 | 2019-01-17 10:11:55 +0000 | [diff] [blame] | 603 | auto RetAssignFn = TLI.CCAssignFnForReturn(CallConv, IsVarArg); |
Diana Picus | a8cb0cd | 2017-02-23 14:18:41 +0000 | [diff] [blame] | 604 | CallReturnHandler RetHandler(MIRBuilder, MRI, MIB, RetAssignFn); |
| 605 | if (!handleAssignments(MIRBuilder, ArgInfos, RetHandler)) |
| 606 | return false; |
Diana Picus | bf4aed2 | 2017-05-29 08:19:19 +0000 | [diff] [blame] | 607 | |
Diana Picus | 8fd1601 | 2017-06-15 09:42:02 +0000 | [diff] [blame] | 608 | if (!SplitRegs.empty()) { |
Diana Picus | bf4aed2 | 2017-05-29 08:19:19 +0000 | [diff] [blame] | 609 | // We have split the value and allocated each individual piece, now build |
| 610 | // it up again. |
Diana Picus | 69ce1c13 | 2019-06-27 08:50:53 +0000 | [diff] [blame^] | 611 | assert(OrigRet.Regs.size() == 1 && "Can't handle multple regs yet"); |
| 612 | MIRBuilder.buildMerge(OrigRet.Regs[0], SplitRegs); |
Diana Picus | bf4aed2 | 2017-05-29 08:19:19 +0000 | [diff] [blame] | 613 | } |
Diana Picus | a8cb0cd | 2017-02-23 14:18:41 +0000 | [diff] [blame] | 614 | } |
| 615 | |
Diana Picus | 1ffca2a | 2017-02-28 14:17:53 +0000 | [diff] [blame] | 616 | // We now know the size of the stack - update the ADJCALLSTACKDOWN |
| 617 | // accordingly. |
Serge Pavlov | d526b13 | 2017-05-09 13:35:13 +0000 | [diff] [blame] | 618 | CallSeqStart.addImm(ArgHandler.StackSize).addImm(0).add(predOps(ARMCC::AL)); |
Diana Picus | 1ffca2a | 2017-02-28 14:17:53 +0000 | [diff] [blame] | 619 | |
Diana Picus | 613b656 | 2017-02-21 11:33:59 +0000 | [diff] [blame] | 620 | MIRBuilder.buildInstr(ARM::ADJCALLSTACKUP) |
Diana Picus | 1ffca2a | 2017-02-28 14:17:53 +0000 | [diff] [blame] | 621 | .addImm(ArgHandler.StackSize) |
Diana Picus | 613b656 | 2017-02-21 11:33:59 +0000 | [diff] [blame] | 622 | .addImm(0) |
| 623 | .add(predOps(ARMCC::AL)); |
| 624 | |
| 625 | return true; |
| 626 | } |