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