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