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 | 8cca8cb | 2017-05-29 07:01:52 +0000 | [diff] [blame] | 58 | return true; |
| 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; |
| 67 | return true; |
| 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 | |
| 93 | unsigned getStackAddress(uint64_t Size, int64_t Offset, |
| 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); |
| 100 | unsigned SPReg = MRI.createGenericVirtualRegister(p0); |
| 101 | MIRBuilder.buildCopy(SPReg, ARM::SP); |
| 102 | |
| 103 | unsigned OffsetReg = MRI.createGenericVirtualRegister(s32); |
| 104 | MIRBuilder.buildConstant(OffsetReg, Offset); |
| 105 | |
| 106 | unsigned AddrReg = MRI.createGenericVirtualRegister(p0); |
| 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 | |
| 113 | void assignValueToReg(unsigned ValVReg, unsigned PhysReg, |
| 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 | |
Diana Picus | 8b6c6be | 2017-01-25 08:10:40 +0000 | [diff] [blame] | 121 | unsigned ExtReg = extendRegister(ValVReg, VA); |
| 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 | |
| 126 | void assignValueToAddress(unsigned ValVReg, unsigned Addr, uint64_t Size, |
| 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 | |
Diana Picus | 9c52309 | 2017-03-01 15:35:14 +0000 | [diff] [blame] | 131 | unsigned 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 { |
| 140 | CCValAssign VA = VAs[0]; |
| 141 | assert(VA.needsCustom() && "Value doesn't need custom handling"); |
| 142 | assert(VA.getValVT() == MVT::f64 && "Unsupported type"); |
| 143 | |
| 144 | CCValAssign NextVA = VAs[1]; |
| 145 | assert(NextVA.needsCustom() && "Value doesn't need custom handling"); |
| 146 | assert(NextVA.getValVT() == MVT::f64 && "Unsupported type"); |
| 147 | |
| 148 | assert(VA.getValNo() == NextVA.getValNo() && |
| 149 | "Values belong to different arguments"); |
| 150 | |
| 151 | assert(VA.isRegLoc() && "Value should be in reg"); |
| 152 | assert(NextVA.isRegLoc() && "Value should be in reg"); |
| 153 | |
| 154 | unsigned NewRegs[] = {MRI.createGenericVirtualRegister(LLT::scalar(32)), |
| 155 | MRI.createGenericVirtualRegister(LLT::scalar(32))}; |
Diana Picus | 0b4190a | 2017-06-07 12:35:05 +0000 | [diff] [blame] | 156 | MIRBuilder.buildUnmerge(NewRegs, Arg.Reg); |
Diana Picus | ca6a890 | 2017-02-16 07:53:07 +0000 | [diff] [blame] | 157 | |
| 158 | bool IsLittle = MIRBuilder.getMF().getSubtarget<ARMSubtarget>().isLittle(); |
| 159 | if (!IsLittle) |
| 160 | std::swap(NewRegs[0], NewRegs[1]); |
| 161 | |
| 162 | assignValueToReg(NewRegs[0], VA.getLocReg(), VA); |
| 163 | assignValueToReg(NewRegs[1], NextVA.getLocReg(), NextVA); |
| 164 | |
| 165 | return 1; |
| 166 | } |
| 167 | |
Diana Picus | 9c52309 | 2017-03-01 15:35:14 +0000 | [diff] [blame] | 168 | bool assignArg(unsigned ValNo, MVT ValVT, MVT LocVT, |
Diana Picus | 3841522 | 2017-03-01 15:54:21 +0000 | [diff] [blame] | 169 | CCValAssign::LocInfo LocInfo, |
| 170 | const CallLowering::ArgInfo &Info, CCState &State) override { |
Diana Picus | 9c52309 | 2017-03-01 15:35:14 +0000 | [diff] [blame] | 171 | if (AssignFn(ValNo, ValVT, LocVT, LocInfo, Info.Flags, State)) |
| 172 | return true; |
| 173 | |
Diana Picus | 3841522 | 2017-03-01 15:54:21 +0000 | [diff] [blame] | 174 | StackSize = |
| 175 | std::max(StackSize, static_cast<uint64_t>(State.getNextStackOffset())); |
Diana Picus | 9c52309 | 2017-03-01 15:35:14 +0000 | [diff] [blame] | 176 | return false; |
| 177 | } |
| 178 | |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 179 | MachineInstrBuilder &MIB; |
Eugene Zelenko | 076468c | 2017-09-20 21:35:51 +0000 | [diff] [blame] | 180 | uint64_t StackSize = 0; |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 181 | }; |
Eugene Zelenko | 076468c | 2017-09-20 21:35:51 +0000 | [diff] [blame] | 182 | |
| 183 | } // end anonymous namespace |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 184 | |
Diana Picus | 8cca8cb | 2017-05-29 07:01:52 +0000 | [diff] [blame] | 185 | void ARMCallLowering::splitToValueTypes( |
| 186 | const ArgInfo &OrigArg, SmallVectorImpl<ArgInfo> &SplitArgs, |
| 187 | MachineFunction &MF, const SplitArgTy &PerformArgSplit) const { |
Diana Picus | 32cd9b4 | 2017-02-02 14:01:00 +0000 | [diff] [blame] | 188 | const ARMTargetLowering &TLI = *getTLI<ARMTargetLowering>(); |
| 189 | LLVMContext &Ctx = OrigArg.Ty->getContext(); |
Diana Picus | 8cca8cb | 2017-05-29 07:01:52 +0000 | [diff] [blame] | 190 | const DataLayout &DL = MF.getDataLayout(); |
| 191 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 192 | const Function &F = MF.getFunction(); |
Diana Picus | 32cd9b4 | 2017-02-02 14:01:00 +0000 | [diff] [blame] | 193 | |
| 194 | SmallVector<EVT, 4> SplitVTs; |
| 195 | SmallVector<uint64_t, 4> Offsets; |
| 196 | ComputeValueVTs(TLI, DL, OrigArg.Ty, SplitVTs, &Offsets, 0); |
| 197 | |
Diana Picus | 8cca8cb | 2017-05-29 07:01:52 +0000 | [diff] [blame] | 198 | if (SplitVTs.size() == 1) { |
| 199 | // Even if there is no splitting to do, we still want to replace the |
| 200 | // original type (e.g. pointer type -> integer). |
Diana Picus | e7aa909 | 2017-06-02 10:16:48 +0000 | [diff] [blame] | 201 | auto Flags = OrigArg.Flags; |
| 202 | unsigned OriginalAlignment = DL.getABITypeAlignment(OrigArg.Ty); |
| 203 | Flags.setOrigAlign(OriginalAlignment); |
| 204 | SplitArgs.emplace_back(OrigArg.Reg, SplitVTs[0].getTypeForEVT(Ctx), Flags, |
| 205 | OrigArg.IsFixed); |
Diana Picus | 8cca8cb | 2017-05-29 07:01:52 +0000 | [diff] [blame] | 206 | return; |
| 207 | } |
Diana Picus | 32cd9b4 | 2017-02-02 14:01:00 +0000 | [diff] [blame] | 208 | |
Diana Picus | 8cca8cb | 2017-05-29 07:01:52 +0000 | [diff] [blame] | 209 | unsigned FirstRegIdx = SplitArgs.size(); |
| 210 | for (unsigned i = 0, e = SplitVTs.size(); i != e; ++i) { |
| 211 | EVT SplitVT = SplitVTs[i]; |
| 212 | Type *SplitTy = SplitVT.getTypeForEVT(Ctx); |
| 213 | auto Flags = OrigArg.Flags; |
Diana Picus | e7aa909 | 2017-06-02 10:16:48 +0000 | [diff] [blame] | 214 | |
| 215 | unsigned OriginalAlignment = DL.getABITypeAlignment(SplitTy); |
| 216 | Flags.setOrigAlign(OriginalAlignment); |
| 217 | |
Diana Picus | 8cca8cb | 2017-05-29 07:01:52 +0000 | [diff] [blame] | 218 | bool NeedsConsecutiveRegisters = |
| 219 | TLI.functionArgumentNeedsConsecutiveRegisters( |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 220 | SplitTy, F.getCallingConv(), F.isVarArg()); |
Diana Picus | 8cca8cb | 2017-05-29 07:01:52 +0000 | [diff] [blame] | 221 | if (NeedsConsecutiveRegisters) { |
| 222 | Flags.setInConsecutiveRegs(); |
| 223 | if (i == e - 1) |
| 224 | Flags.setInConsecutiveRegsLast(); |
| 225 | } |
Diana Picus | e7aa909 | 2017-06-02 10:16:48 +0000 | [diff] [blame] | 226 | |
Diana Picus | 8cca8cb | 2017-05-29 07:01:52 +0000 | [diff] [blame] | 227 | SplitArgs.push_back( |
| 228 | ArgInfo{MRI.createGenericVirtualRegister(getLLTForType(*SplitTy, DL)), |
| 229 | SplitTy, Flags, OrigArg.IsFixed}); |
| 230 | } |
| 231 | |
| 232 | for (unsigned i = 0; i < Offsets.size(); ++i) |
| 233 | PerformArgSplit(SplitArgs[FirstRegIdx + i].Reg, Offsets[i] * 8); |
Diana Picus | 32cd9b4 | 2017-02-02 14:01:00 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 236 | /// Lower the return value for the already existing \p Ret. This assumes that |
| 237 | /// \p MIRBuilder's insertion point is correct. |
| 238 | bool ARMCallLowering::lowerReturnVal(MachineIRBuilder &MIRBuilder, |
Alexander Ivchenko | 49168f6 | 2018-08-02 08:33:31 +0000 | [diff] [blame] | 239 | const Value *Val, ArrayRef<unsigned> VRegs, |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 240 | MachineInstrBuilder &Ret) const { |
| 241 | if (!Val) |
| 242 | // Nothing to do here. |
| 243 | return true; |
| 244 | |
| 245 | auto &MF = MIRBuilder.getMF(); |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 246 | const auto &F = MF.getFunction(); |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 247 | |
| 248 | auto DL = MF.getDataLayout(); |
| 249 | auto &TLI = *getTLI<ARMTargetLowering>(); |
| 250 | if (!isSupportedType(DL, TLI, Val->getType())) |
Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 251 | return false; |
| 252 | |
Alexander Ivchenko | 49168f6 | 2018-08-02 08:33:31 +0000 | [diff] [blame] | 253 | SmallVector<EVT, 4> SplitEVTs; |
| 254 | ComputeValueVTs(TLI, DL, Val->getType(), SplitEVTs); |
| 255 | assert(VRegs.size() == SplitEVTs.size() && |
| 256 | "For each split Type there should be exactly one VReg."); |
Diana Picus | 32cd9b4 | 2017-02-02 14:01:00 +0000 | [diff] [blame] | 257 | |
Alexander Ivchenko | 49168f6 | 2018-08-02 08:33:31 +0000 | [diff] [blame] | 258 | SmallVector<ArgInfo, 4> SplitVTs; |
| 259 | LLVMContext &Ctx = Val->getType()->getContext(); |
| 260 | for (unsigned i = 0; i < SplitEVTs.size(); ++i) { |
| 261 | ArgInfo CurArgInfo(VRegs[i], SplitEVTs[i].getTypeForEVT(Ctx)); |
| 262 | setArgFlags(CurArgInfo, AttributeList::ReturnIndex, DL, F); |
| 263 | |
| 264 | SmallVector<unsigned, 4> Regs; |
| 265 | splitToValueTypes( |
| 266 | CurArgInfo, SplitVTs, MF, |
| 267 | [&](unsigned Reg, uint64_t Offset) { Regs.push_back(Reg); }); |
| 268 | if (Regs.size() > 1) |
| 269 | MIRBuilder.buildUnmerge(Regs, VRegs[i]); |
| 270 | } |
Diana Picus | 8fd1601 | 2017-06-15 09:42:02 +0000 | [diff] [blame] | 271 | |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 272 | CCAssignFn *AssignFn = |
| 273 | TLI.CCAssignFnForReturn(F.getCallingConv(), F.isVarArg()); |
Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 274 | |
Diana Picus | a606713 | 2017-02-23 13:25:43 +0000 | [diff] [blame] | 275 | OutgoingValueHandler RetHandler(MIRBuilder, MF.getRegInfo(), Ret, AssignFn); |
Diana Picus | 32cd9b4 | 2017-02-02 14:01:00 +0000 | [diff] [blame] | 276 | return handleAssignments(MIRBuilder, SplitVTs, RetHandler); |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | bool ARMCallLowering::lowerReturn(MachineIRBuilder &MIRBuilder, |
Alexander Ivchenko | 49168f6 | 2018-08-02 08:33:31 +0000 | [diff] [blame] | 280 | const Value *Val, |
| 281 | ArrayRef<unsigned> VRegs) const { |
| 282 | assert(!Val == VRegs.empty() && "Return value without a vreg"); |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 283 | |
Joerg Sonnenberger | 0f76a35 | 2017-08-28 20:20:47 +0000 | [diff] [blame] | 284 | auto const &ST = MIRBuilder.getMF().getSubtarget<ARMSubtarget>(); |
| 285 | unsigned Opcode = ST.getReturnOpcode(); |
| 286 | auto Ret = MIRBuilder.buildInstrNoInsert(Opcode).add(predOps(ARMCC::AL)); |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 287 | |
Alexander Ivchenko | 49168f6 | 2018-08-02 08:33:31 +0000 | [diff] [blame] | 288 | if (!lowerReturnVal(MIRBuilder, Val, VRegs, Ret)) |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 289 | return false; |
| 290 | |
| 291 | MIRBuilder.insertInstr(Ret); |
Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 292 | return true; |
| 293 | } |
| 294 | |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 295 | namespace { |
Eugene Zelenko | 076468c | 2017-09-20 21:35:51 +0000 | [diff] [blame] | 296 | |
Diana Picus | a8cb0cd | 2017-02-23 14:18:41 +0000 | [diff] [blame] | 297 | /// Helper class for values coming in through an ABI boundary (used for handling |
| 298 | /// formal arguments and call return values). |
| 299 | struct IncomingValueHandler : public CallLowering::ValueHandler { |
| 300 | IncomingValueHandler(MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI, |
| 301 | CCAssignFn AssignFn) |
Tim Northover | d943354 | 2017-01-17 22:30:10 +0000 | [diff] [blame] | 302 | : ValueHandler(MIRBuilder, MRI, AssignFn) {} |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 303 | |
Amara Emerson | 2b523f8 | 2019-04-09 21:22:33 +0000 | [diff] [blame] | 304 | bool isArgumentHandler() const override { return true; } |
| 305 | |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 306 | unsigned getStackAddress(uint64_t Size, int64_t Offset, |
| 307 | MachinePointerInfo &MPO) override { |
Diana Picus | ca6a890 | 2017-02-16 07:53:07 +0000 | [diff] [blame] | 308 | assert((Size == 1 || Size == 2 || Size == 4 || Size == 8) && |
| 309 | "Unsupported size"); |
Diana Picus | 1437f6d | 2016-12-19 11:55:41 +0000 | [diff] [blame] | 310 | |
| 311 | auto &MFI = MIRBuilder.getMF().getFrameInfo(); |
| 312 | |
| 313 | int FI = MFI.CreateFixedObject(Size, Offset, true); |
| 314 | MPO = MachinePointerInfo::getFixedStack(MIRBuilder.getMF(), FI); |
| 315 | |
| 316 | unsigned AddrReg = |
| 317 | MRI.createGenericVirtualRegister(LLT::pointer(MPO.getAddrSpace(), 32)); |
| 318 | MIRBuilder.buildFrameIndex(AddrReg, FI); |
| 319 | |
| 320 | return AddrReg; |
| 321 | } |
| 322 | |
| 323 | void assignValueToAddress(unsigned ValVReg, unsigned Addr, uint64_t Size, |
| 324 | MachinePointerInfo &MPO, CCValAssign &VA) override { |
Diana Picus | ca6a890 | 2017-02-16 07:53:07 +0000 | [diff] [blame] | 325 | assert((Size == 1 || Size == 2 || Size == 4 || Size == 8) && |
| 326 | "Unsupported size"); |
Diana Picus | 278c722 | 2017-01-26 09:20:47 +0000 | [diff] [blame] | 327 | |
| 328 | if (VA.getLocInfo() == CCValAssign::SExt || |
| 329 | VA.getLocInfo() == CCValAssign::ZExt) { |
Diana Picus | a8cb0cd | 2017-02-23 14:18:41 +0000 | [diff] [blame] | 330 | // If the value is zero- or sign-extended, its size becomes 4 bytes, so |
| 331 | // that's what we should load. |
Diana Picus | 278c722 | 2017-01-26 09:20:47 +0000 | [diff] [blame] | 332 | Size = 4; |
| 333 | assert(MRI.getType(ValVReg).isScalar() && "Only scalars supported atm"); |
Diana Picus | 1437f6d | 2016-12-19 11:55:41 +0000 | [diff] [blame] | 334 | |
Diana Picus | 4f46be3 | 2017-04-27 10:23:30 +0000 | [diff] [blame] | 335 | auto LoadVReg = MRI.createGenericVirtualRegister(LLT::scalar(32)); |
Matt Arsenault | 2a64598 | 2019-01-31 01:38:47 +0000 | [diff] [blame] | 336 | buildLoad(LoadVReg, Addr, Size, /* Alignment */ 1, MPO); |
Diana Picus | 4f46be3 | 2017-04-27 10:23:30 +0000 | [diff] [blame] | 337 | MIRBuilder.buildTrunc(ValVReg, LoadVReg); |
| 338 | } else { |
| 339 | // If the value is not extended, a simple load will suffice. |
Matt Arsenault | 2a64598 | 2019-01-31 01:38:47 +0000 | [diff] [blame] | 340 | buildLoad(ValVReg, Addr, Size, /* Alignment */ 1, MPO); |
Diana Picus | 4f46be3 | 2017-04-27 10:23:30 +0000 | [diff] [blame] | 341 | } |
| 342 | } |
| 343 | |
| 344 | void buildLoad(unsigned Val, unsigned Addr, uint64_t Size, unsigned Alignment, |
| 345 | MachinePointerInfo &MPO) { |
Diana Picus | 1437f6d | 2016-12-19 11:55:41 +0000 | [diff] [blame] | 346 | auto MMO = MIRBuilder.getMF().getMachineMemOperand( |
Diana Picus | 4f46be3 | 2017-04-27 10:23:30 +0000 | [diff] [blame] | 347 | MPO, MachineMemOperand::MOLoad, Size, Alignment); |
| 348 | MIRBuilder.buildLoad(Val, Addr, *MMO); |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | void assignValueToReg(unsigned ValVReg, unsigned PhysReg, |
| 352 | CCValAssign &VA) override { |
| 353 | assert(VA.isRegLoc() && "Value shouldn't be assigned to reg"); |
| 354 | assert(VA.getLocReg() == PhysReg && "Assigning to the wrong reg?"); |
| 355 | |
Aditya Nandakumar | c3bfc81 | 2017-10-09 20:07:43 +0000 | [diff] [blame] | 356 | auto ValSize = VA.getValVT().getSizeInBits(); |
| 357 | auto LocSize = VA.getLocVT().getSizeInBits(); |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 358 | |
Aditya Nandakumar | c3bfc81 | 2017-10-09 20:07:43 +0000 | [diff] [blame] | 359 | assert(ValSize <= 64 && "Unsupported value size"); |
| 360 | assert(LocSize <= 64 && "Unsupported location size"); |
| 361 | |
Diana Picus | a8cb0cd | 2017-02-23 14:18:41 +0000 | [diff] [blame] | 362 | markPhysRegUsed(PhysReg); |
Aditya Nandakumar | c3bfc81 | 2017-10-09 20:07:43 +0000 | [diff] [blame] | 363 | if (ValSize == LocSize) { |
| 364 | MIRBuilder.buildCopy(ValVReg, PhysReg); |
| 365 | } else { |
| 366 | assert(ValSize < LocSize && "Extensions not supported"); |
| 367 | |
| 368 | // We cannot create a truncating copy, nor a trunc of a physical register. |
| 369 | // Therefore, we need to copy the content of the physical register into a |
| 370 | // virtual one and then truncate that. |
| 371 | auto PhysRegToVReg = |
| 372 | MRI.createGenericVirtualRegister(LLT::scalar(LocSize)); |
| 373 | MIRBuilder.buildCopy(PhysRegToVReg, PhysReg); |
| 374 | MIRBuilder.buildTrunc(ValVReg, PhysRegToVReg); |
| 375 | } |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 376 | } |
Diana Picus | ca6a890 | 2017-02-16 07:53:07 +0000 | [diff] [blame] | 377 | |
Diana Picus | a606713 | 2017-02-23 13:25:43 +0000 | [diff] [blame] | 378 | unsigned assignCustomValue(const ARMCallLowering::ArgInfo &Arg, |
Diana Picus | ca6a890 | 2017-02-16 07:53:07 +0000 | [diff] [blame] | 379 | ArrayRef<CCValAssign> VAs) override { |
| 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 | |
| 394 | unsigned NewRegs[] = {MRI.createGenericVirtualRegister(LLT::scalar(32)), |
| 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 | 0b4190a | 2017-06-07 12:35:05 +0000 | [diff] [blame] | 404 | MIRBuilder.buildMerge(Arg.Reg, 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, |
| 429 | ArrayRef<unsigned> 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; |
Diana Picus | 0c05cce | 2017-05-29 09:09:54 +0000 | [diff] [blame] | 461 | SmallVector<unsigned, 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 | 8cca8cb | 2017-05-29 07:01:52 +0000 | [diff] [blame] | 469 | splitToValueTypes(AInfo, ArgInfos, MF, [&](unsigned Reg, uint64_t Offset) { |
Diana Picus | 0c05cce | 2017-05-29 09:09:54 +0000 | [diff] [blame] | 470 | SplitRegs.push_back(Reg); |
Diana Picus | 8cca8cb | 2017-05-29 07:01:52 +0000 | [diff] [blame] | 471 | }); |
Diana Picus | 0c05cce | 2017-05-29 09:09:54 +0000 | [diff] [blame] | 472 | |
| 473 | if (!SplitRegs.empty()) |
Diana Picus | 8fd1601 | 2017-06-15 09:42:02 +0000 | [diff] [blame] | 474 | MIRBuilder.buildMerge(VRegs[Idx], SplitRegs); |
Diana Picus | 8cca8cb | 2017-05-29 07:01:52 +0000 | [diff] [blame] | 475 | |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 476 | Idx++; |
| 477 | } |
| 478 | |
Diana Picus | 8cca8cb | 2017-05-29 07:01:52 +0000 | [diff] [blame] | 479 | if (!MBB.empty()) |
| 480 | MIRBuilder.setInstr(*MBB.begin()); |
| 481 | |
Amara Emerson | 0d6a26d | 2018-05-16 10:32:02 +0000 | [diff] [blame] | 482 | if (!handleAssignments(MIRBuilder, ArgInfos, ArgHandler)) |
| 483 | return false; |
| 484 | |
| 485 | // Move back to the end of the basic block. |
| 486 | MIRBuilder.setMBB(MBB); |
| 487 | return true; |
Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 488 | } |
Diana Picus | 613b656 | 2017-02-21 11:33:59 +0000 | [diff] [blame] | 489 | |
Diana Picus | a8cb0cd | 2017-02-23 14:18:41 +0000 | [diff] [blame] | 490 | namespace { |
Eugene Zelenko | 076468c | 2017-09-20 21:35:51 +0000 | [diff] [blame] | 491 | |
Diana Picus | a8cb0cd | 2017-02-23 14:18:41 +0000 | [diff] [blame] | 492 | struct CallReturnHandler : public IncomingValueHandler { |
| 493 | CallReturnHandler(MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI, |
| 494 | MachineInstrBuilder MIB, CCAssignFn *AssignFn) |
| 495 | : IncomingValueHandler(MIRBuilder, MRI, AssignFn), MIB(MIB) {} |
| 496 | |
| 497 | void markPhysRegUsed(unsigned PhysReg) override { |
| 498 | MIB.addDef(PhysReg, RegState::Implicit); |
| 499 | } |
| 500 | |
| 501 | MachineInstrBuilder MIB; |
| 502 | }; |
Eugene Zelenko | 076468c | 2017-09-20 21:35:51 +0000 | [diff] [blame] | 503 | |
Diana Picus | 8a1b4f5 | 2018-12-05 10:35:28 +0000 | [diff] [blame] | 504 | // FIXME: This should move to the ARMSubtarget when it supports all the opcodes. |
| 505 | unsigned getCallOpcode(const ARMSubtarget &STI, bool isDirect) { |
| 506 | if (isDirect) |
| 507 | return STI.isThumb() ? ARM::tBL : ARM::BL; |
| 508 | |
| 509 | if (STI.isThumb()) |
| 510 | return ARM::tBLXr; |
| 511 | |
| 512 | if (STI.hasV5TOps()) |
| 513 | return ARM::BLX; |
| 514 | |
| 515 | if (STI.hasV4TOps()) |
| 516 | return ARM::BX_CALL; |
| 517 | |
| 518 | return ARM::BMOVPCRX_CALL; |
| 519 | } |
Eugene Zelenko | 076468c | 2017-09-20 21:35:51 +0000 | [diff] [blame] | 520 | } // end anonymous namespace |
Diana Picus | a8cb0cd | 2017-02-23 14:18:41 +0000 | [diff] [blame] | 521 | |
Diana Picus | 613b656 | 2017-02-21 11:33:59 +0000 | [diff] [blame] | 522 | bool ARMCallLowering::lowerCall(MachineIRBuilder &MIRBuilder, |
Diana Picus | d79253a | 2017-03-20 14:40:18 +0000 | [diff] [blame] | 523 | CallingConv::ID CallConv, |
Diana Picus | 613b656 | 2017-02-21 11:33:59 +0000 | [diff] [blame] | 524 | const MachineOperand &Callee, |
| 525 | const ArgInfo &OrigRet, |
| 526 | ArrayRef<ArgInfo> OrigArgs) const { |
Diana Picus | a606713 | 2017-02-23 13:25:43 +0000 | [diff] [blame] | 527 | MachineFunction &MF = MIRBuilder.getMF(); |
| 528 | const auto &TLI = *getTLI<ARMTargetLowering>(); |
| 529 | const auto &DL = MF.getDataLayout(); |
Diana Picus | b350221 | 2017-10-25 11:42:40 +0000 | [diff] [blame] | 530 | const auto &STI = MF.getSubtarget<ARMSubtarget>(); |
Diana Picus | 0091cc3 | 2017-06-05 12:54:53 +0000 | [diff] [blame] | 531 | const TargetRegisterInfo *TRI = STI.getRegisterInfo(); |
Diana Picus | a606713 | 2017-02-23 13:25:43 +0000 | [diff] [blame] | 532 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
Diana Picus | 613b656 | 2017-02-21 11:33:59 +0000 | [diff] [blame] | 533 | |
Diana Picus | b350221 | 2017-10-25 11:42:40 +0000 | [diff] [blame] | 534 | if (STI.genLongCalls()) |
Diana Picus | 613b656 | 2017-02-21 11:33:59 +0000 | [diff] [blame] | 535 | return false; |
| 536 | |
Diana Picus | 8a1b4f5 | 2018-12-05 10:35:28 +0000 | [diff] [blame] | 537 | if (STI.isThumb1Only()) |
| 538 | return false; |
| 539 | |
Diana Picus | 1ffca2a | 2017-02-28 14:17:53 +0000 | [diff] [blame] | 540 | auto CallSeqStart = MIRBuilder.buildInstr(ARM::ADJCALLSTACKDOWN); |
Diana Picus | 613b656 | 2017-02-21 11:33:59 +0000 | [diff] [blame] | 541 | |
Diana Picus | a606713 | 2017-02-23 13:25:43 +0000 | [diff] [blame] | 542 | // Create the call instruction so we can add the implicit uses of arg |
| 543 | // registers, but don't insert it yet. |
Diana Picus | 639e066 | 2019-01-17 10:11:59 +0000 | [diff] [blame] | 544 | bool IsDirect = !Callee.isReg(); |
| 545 | auto CallOpcode = getCallOpcode(STI, IsDirect); |
Diana Picus | 8a1b4f5 | 2018-12-05 10:35:28 +0000 | [diff] [blame] | 546 | auto MIB = MIRBuilder.buildInstrNoInsert(CallOpcode); |
| 547 | |
Diana Picus | 639e066 | 2019-01-17 10:11:59 +0000 | [diff] [blame] | 548 | bool IsThumb = STI.isThumb(); |
| 549 | if (IsThumb) |
Diana Picus | 8a1b4f5 | 2018-12-05 10:35:28 +0000 | [diff] [blame] | 550 | MIB.add(predOps(ARMCC::AL)); |
| 551 | |
| 552 | MIB.add(Callee); |
Diana Picus | 639e066 | 2019-01-17 10:11:59 +0000 | [diff] [blame] | 553 | if (!IsDirect) { |
Diana Picus | 0091cc3 | 2017-06-05 12:54:53 +0000 | [diff] [blame] | 554 | auto CalleeReg = Callee.getReg(); |
Diana Picus | 8a1b4f5 | 2018-12-05 10:35:28 +0000 | [diff] [blame] | 555 | if (CalleeReg && !TRI->isPhysicalRegister(CalleeReg)) { |
Diana Picus | 639e066 | 2019-01-17 10:11:59 +0000 | [diff] [blame] | 556 | unsigned CalleeIdx = IsThumb ? 2 : 0; |
Diana Picus | 8a1b4f5 | 2018-12-05 10:35:28 +0000 | [diff] [blame] | 557 | MIB->getOperand(CalleeIdx).setReg(constrainOperandRegClass( |
Diana Picus | 0091cc3 | 2017-06-05 12:54:53 +0000 | [diff] [blame] | 558 | MF, *TRI, MRI, *STI.getInstrInfo(), *STI.getRegBankInfo(), |
Diana Picus | 8a1b4f5 | 2018-12-05 10:35:28 +0000 | [diff] [blame] | 559 | *MIB.getInstr(), MIB->getDesc(), Callee, CalleeIdx)); |
| 560 | } |
Diana Picus | 0091cc3 | 2017-06-05 12:54:53 +0000 | [diff] [blame] | 561 | } |
Diana Picus | a606713 | 2017-02-23 13:25:43 +0000 | [diff] [blame] | 562 | |
Diana Picus | 8a1b4f5 | 2018-12-05 10:35:28 +0000 | [diff] [blame] | 563 | MIB.addRegMask(TRI->getCallPreservedMask(MF, CallConv)); |
| 564 | |
Diana Picus | d5c2499 | 2019-01-17 10:11:55 +0000 | [diff] [blame] | 565 | bool IsVarArg = false; |
Diana Picus | a606713 | 2017-02-23 13:25:43 +0000 | [diff] [blame] | 566 | SmallVector<ArgInfo, 8> ArgInfos; |
| 567 | for (auto Arg : OrigArgs) { |
| 568 | if (!isSupportedType(DL, TLI, Arg.Ty)) |
| 569 | return false; |
| 570 | |
| 571 | if (!Arg.IsFixed) |
Diana Picus | d5c2499 | 2019-01-17 10:11:55 +0000 | [diff] [blame] | 572 | IsVarArg = true; |
Diana Picus | a606713 | 2017-02-23 13:25:43 +0000 | [diff] [blame] | 573 | |
Diana Picus | f003d9f | 2017-11-30 12:23:44 +0000 | [diff] [blame] | 574 | if (Arg.Flags.isByVal()) |
| 575 | return false; |
| 576 | |
Diana Picus | 8fd1601 | 2017-06-15 09:42:02 +0000 | [diff] [blame] | 577 | SmallVector<unsigned, 8> Regs; |
Diana Picus | 8cca8cb | 2017-05-29 07:01:52 +0000 | [diff] [blame] | 578 | splitToValueTypes(Arg, ArgInfos, MF, [&](unsigned Reg, uint64_t Offset) { |
Diana Picus | 8fd1601 | 2017-06-15 09:42:02 +0000 | [diff] [blame] | 579 | Regs.push_back(Reg); |
Diana Picus | 8cca8cb | 2017-05-29 07:01:52 +0000 | [diff] [blame] | 580 | }); |
Diana Picus | 8fd1601 | 2017-06-15 09:42:02 +0000 | [diff] [blame] | 581 | |
| 582 | if (Regs.size() > 1) |
| 583 | MIRBuilder.buildUnmerge(Regs, Arg.Reg); |
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(); |
Diana Picus | bf4aed2 | 2017-05-29 08:19:19 +0000 | [diff] [blame] | 599 | SmallVector<unsigned, 8> SplitRegs; |
Diana Picus | 8cca8cb | 2017-05-29 07:01:52 +0000 | [diff] [blame] | 600 | splitToValueTypes(OrigRet, ArgInfos, MF, |
Diana Picus | bf4aed2 | 2017-05-29 08:19:19 +0000 | [diff] [blame] | 601 | [&](unsigned Reg, uint64_t Offset) { |
Diana Picus | bf4aed2 | 2017-05-29 08:19:19 +0000 | [diff] [blame] | 602 | SplitRegs.push_back(Reg); |
| 603 | }); |
Diana Picus | a8cb0cd | 2017-02-23 14:18:41 +0000 | [diff] [blame] | 604 | |
Diana Picus | d5c2499 | 2019-01-17 10:11:55 +0000 | [diff] [blame] | 605 | auto RetAssignFn = TLI.CCAssignFnForReturn(CallConv, IsVarArg); |
Diana Picus | a8cb0cd | 2017-02-23 14:18:41 +0000 | [diff] [blame] | 606 | CallReturnHandler RetHandler(MIRBuilder, MRI, MIB, RetAssignFn); |
| 607 | if (!handleAssignments(MIRBuilder, ArgInfos, RetHandler)) |
| 608 | return false; |
Diana Picus | bf4aed2 | 2017-05-29 08:19:19 +0000 | [diff] [blame] | 609 | |
Diana Picus | 8fd1601 | 2017-06-15 09:42:02 +0000 | [diff] [blame] | 610 | if (!SplitRegs.empty()) { |
Diana Picus | bf4aed2 | 2017-05-29 08:19:19 +0000 | [diff] [blame] | 611 | // We have split the value and allocated each individual piece, now build |
| 612 | // it up again. |
Diana Picus | 8fd1601 | 2017-06-15 09:42:02 +0000 | [diff] [blame] | 613 | MIRBuilder.buildMerge(OrigRet.Reg, SplitRegs); |
Diana Picus | bf4aed2 | 2017-05-29 08:19:19 +0000 | [diff] [blame] | 614 | } |
Diana Picus | a8cb0cd | 2017-02-23 14:18:41 +0000 | [diff] [blame] | 615 | } |
| 616 | |
Diana Picus | 1ffca2a | 2017-02-28 14:17:53 +0000 | [diff] [blame] | 617 | // We now know the size of the stack - update the ADJCALLSTACKDOWN |
| 618 | // accordingly. |
Serge Pavlov | d526b13 | 2017-05-09 13:35:13 +0000 | [diff] [blame] | 619 | CallSeqStart.addImm(ArgHandler.StackSize).addImm(0).add(predOps(ARMCC::AL)); |
Diana Picus | 1ffca2a | 2017-02-28 14:17:53 +0000 | [diff] [blame] | 620 | |
Diana Picus | 613b656 | 2017-02-21 11:33:59 +0000 | [diff] [blame] | 621 | MIRBuilder.buildInstr(ARM::ADJCALLSTACKUP) |
Diana Picus | 1ffca2a | 2017-02-28 14:17:53 +0000 | [diff] [blame] | 622 | .addImm(ArgHandler.StackSize) |
Diana Picus | 613b656 | 2017-02-21 11:33:59 +0000 | [diff] [blame] | 623 | .addImm(0) |
| 624 | .add(predOps(ARMCC::AL)); |
| 625 | |
| 626 | return true; |
| 627 | } |