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