1//===- Hexagon.cpp --------------------------------------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
9#include "ABIInfoImpl.h"
10#include "TargetInfo.h"
11#include "llvm/ADT/StringExtras.h"
12
13using namespace clang;
14using namespace clang::CodeGen;
15
16//===----------------------------------------------------------------------===//
17// Hexagon ABI Implementation
18//===----------------------------------------------------------------------===//
19
20namespace {
21
22class HexagonABIInfo : public DefaultABIInfo {
23public:
24 HexagonABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
25
26private:
27 ABIArgInfo classifyReturnType(QualType RetTy) const;
28 ABIArgInfo classifyArgumentType(QualType RetTy, unsigned *RegsLeft) const;
29
30 void computeInfo(CGFunctionInfo &FI) const override;
31
32 RValue EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, QualType Ty,
33 AggValueSlot Slot) const override;
34 Address EmitVAArgFromMemory(CodeGenFunction &CFG, Address VAListAddr,
35 QualType Ty) const;
36 Address EmitVAArgForHexagon(CodeGenFunction &CFG, Address VAListAddr,
37 QualType Ty) const;
38 Address EmitVAArgForHexagonLinux(CodeGenFunction &CFG, Address VAListAddr,
39 QualType Ty) const;
40};
41
42class HexagonTargetCodeGenInfo : public TargetCodeGenInfo {
43public:
44 HexagonTargetCodeGenInfo(CodeGenTypes &CGT)
45 : TargetCodeGenInfo(std::make_unique<HexagonABIInfo>(args&: CGT)) {}
46
47 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
48 return 29;
49 }
50
51 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
52 CodeGen::CodeGenModule &GCM) const override {
53 auto *Fn = dyn_cast<llvm::Function>(Val: GV);
54 if (!Fn)
55 return;
56 // Declarations are not skipped: functions without a FunctionDecl, such as
57 // global initializers, get here before their body is emitted.
58 const auto *FD = dyn_cast_or_null<FunctionDecl>(Val: D);
59 unsigned Limit = GCM.getCodeGenOpts().HLXVectorStackLimit;
60 if (const auto *Attr =
61 FD ? FD->getAttr<HexagonHLXVectorStackLimitAttr>() : nullptr)
62 Fn->addFnAttr(Kind: "hlx-vector-stack-limit", Val: llvm::utostr(X: Attr->getLimit()));
63 else if (Limit != UINT_MAX)
64 Fn->addFnAttr(Kind: "hlx-vector-stack-limit", Val: llvm::utostr(X: Limit));
65 }
66};
67
68} // namespace
69
70void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const {
71 unsigned RegsLeft = 6;
72 if (!getCXXABI().classifyReturnType(FI))
73 FI.getReturnInfo() = classifyReturnType(RetTy: FI.getReturnType());
74 for (auto &I : FI.arguments())
75 I.info = classifyArgumentType(RetTy: I.type, RegsLeft: &RegsLeft);
76}
77
78static bool HexagonAdjustRegsLeft(uint64_t Size, unsigned *RegsLeft) {
79 assert(Size <= 64 && "Not expecting to pass arguments larger than 64 bits"
80 " through registers");
81
82 if (*RegsLeft == 0)
83 return false;
84
85 if (Size <= 32) {
86 (*RegsLeft)--;
87 return true;
88 }
89
90 if (2 <= (*RegsLeft & (~1U))) {
91 *RegsLeft = (*RegsLeft & (~1U)) - 2;
92 return true;
93 }
94
95 // Next available register was r5 but candidate was greater than 32-bits so it
96 // has to go on the stack. However we still consume r5
97 if (*RegsLeft == 1)
98 *RegsLeft = 0;
99
100 return false;
101}
102
103ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty,
104 unsigned *RegsLeft) const {
105 if (!isAggregateTypeForABI(T: Ty)) {
106 // Treat an enum type as its underlying type.
107 if (const auto *ED = Ty->getAsEnumDecl())
108 Ty = ED->getIntegerType();
109
110 uint64_t Size = getContext().getTypeSize(T: Ty);
111 if (Size <= 64)
112 HexagonAdjustRegsLeft(Size, RegsLeft);
113
114 if (Size > 64 && Ty->isBitIntType())
115 return getNaturalAlignIndirect(Ty, AddrSpace: getDataLayout().getAllocaAddrSpace(),
116 /*ByVal=*/true);
117
118 return isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
119 : ABIArgInfo::getDirect();
120 }
121
122 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(T: Ty, CXXABI&: getCXXABI()))
123 return getNaturalAlignIndirect(Ty, AddrSpace: getDataLayout().getAllocaAddrSpace(),
124 ByVal: RAA == CGCXXABI::RAA_DirectInMemory);
125
126 // Ignore empty records.
127 if (isEmptyRecord(Context&: getContext(), T: Ty, AllowArrays: true))
128 return ABIArgInfo::getIgnore();
129
130 uint64_t Size = getContext().getTypeSize(T: Ty);
131 unsigned Align = getContext().getTypeAlign(T: Ty);
132
133 if (Size > 64)
134 return getNaturalAlignIndirect(Ty, AddrSpace: getDataLayout().getAllocaAddrSpace(),
135 /*ByVal=*/true);
136
137 if (HexagonAdjustRegsLeft(Size, RegsLeft))
138 Align = Size <= 32 ? 32 : 64;
139 if (Size <= Align) {
140 // Pass in the smallest viable integer type.
141 Size = llvm::bit_ceil(Value: Size);
142 return ABIArgInfo::getDirect(T: llvm::Type::getIntNTy(C&: getVMContext(), N: Size));
143 }
144 return DefaultABIInfo::classifyArgumentType(RetTy: Ty);
145}
146
147ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const {
148 if (RetTy->isVoidType())
149 return ABIArgInfo::getIgnore();
150
151 const TargetInfo &T = CGT.getTarget();
152 uint64_t Size = getContext().getTypeSize(T: RetTy);
153
154 if (RetTy->getAs<VectorType>()) {
155 // HVX vectors are returned in vector registers or register pairs.
156 if (T.hasFeature(Feature: "hvx")) {
157 assert(T.hasFeature("hvx-length64b") || T.hasFeature("hvx-length128b"));
158 uint64_t VecSize = T.hasFeature(Feature: "hvx-length64b") ? 64*8 : 128*8;
159 if (Size == VecSize || Size == 2*VecSize)
160 return ABIArgInfo::getDirectInReg();
161 }
162 // HLX vectors are returned in HLX registers or register pairs.
163 if (T.hasFeature(Feature: "hlx")) {
164 uint64_t VecSize = 2048 * 8;
165 if (Size == VecSize || Size == 2 * VecSize)
166 return ABIArgInfo::getDirectInReg();
167 }
168 // Large vector types should be returned via memory.
169 if (Size > 64)
170 return getNaturalAlignIndirect(Ty: RetTy,
171 AddrSpace: getDataLayout().getAllocaAddrSpace());
172 }
173
174 if (!isAggregateTypeForABI(T: RetTy)) {
175 // Treat an enum type as its underlying type.
176 if (const auto *ED = RetTy->getAsEnumDecl())
177 RetTy = ED->getIntegerType();
178
179 if (Size > 64 && RetTy->isBitIntType())
180 return getNaturalAlignIndirect(
181 Ty: RetTy, AddrSpace: getDataLayout().getAllocaAddrSpace(), /*ByVal=*/false);
182
183 return isPromotableIntegerTypeForABI(Ty: RetTy) ? ABIArgInfo::getExtend(Ty: RetTy)
184 : ABIArgInfo::getDirect();
185 }
186
187 if (isEmptyRecord(Context&: getContext(), T: RetTy, AllowArrays: true))
188 return ABIArgInfo::getIgnore();
189
190 // Aggregates <= 8 bytes are returned in registers, other aggregates
191 // are returned indirectly.
192 if (Size <= 64) {
193 // Return in the smallest viable integer type.
194 Size = llvm::bit_ceil(Value: Size);
195 return ABIArgInfo::getDirect(T: llvm::Type::getIntNTy(C&: getVMContext(), N: Size));
196 }
197 return getNaturalAlignIndirect(Ty: RetTy, AddrSpace: getDataLayout().getAllocaAddrSpace(),
198 /*ByVal=*/true);
199}
200
201Address HexagonABIInfo::EmitVAArgFromMemory(CodeGenFunction &CGF,
202 Address VAListAddr,
203 QualType Ty) const {
204 // Load the overflow area pointer.
205 Address __overflow_area_pointer_p =
206 CGF.Builder.CreateStructGEP(Addr: VAListAddr, Index: 2, Name: "__overflow_area_pointer_p");
207 llvm::Value *__overflow_area_pointer = CGF.Builder.CreateLoad(
208 Addr: __overflow_area_pointer_p, Name: "__overflow_area_pointer");
209
210 uint64_t Align = CGF.getContext().getTypeAlign(T: Ty) / 8;
211 if (Align > 4) {
212 // Alignment should be a power of 2.
213 assert((Align & (Align - 1)) == 0 && "Alignment is not power of 2!");
214
215 // overflow_arg_area = (overflow_arg_area + align - 1) & -align;
216 llvm::Value *Offset = llvm::ConstantInt::get(Ty: CGF.Int64Ty, V: Align - 1);
217
218 // Add offset to the current pointer to access the argument.
219 __overflow_area_pointer =
220 CGF.Builder.CreateGEP(Ty: CGF.Int8Ty, Ptr: __overflow_area_pointer, IdxList: Offset);
221 llvm::Value *AsInt =
222 CGF.Builder.CreatePtrToInt(V: __overflow_area_pointer, DestTy: CGF.Int32Ty);
223
224 // Create a mask which should be "AND"ed
225 // with (overflow_arg_area + align - 1)
226 llvm::Value *Mask = llvm::ConstantInt::getSigned(Ty: CGF.Int32Ty, V: -(int)Align);
227 __overflow_area_pointer = CGF.Builder.CreateIntToPtr(
228 V: CGF.Builder.CreateAnd(LHS: AsInt, RHS: Mask), DestTy: __overflow_area_pointer->getType(),
229 Name: "__overflow_area_pointer.align");
230 }
231
232 // Get the type of the argument from memory and bitcast
233 // overflow area pointer to the argument type.
234 llvm::Type *PTy = CGF.ConvertTypeForMem(T: Ty);
235 Address AddrTyped =
236 Address(__overflow_area_pointer, PTy, CharUnits::fromQuantity(Quantity: Align));
237
238 // Round up to the minimum stack alignment for varargs which is 4 bytes.
239 uint64_t Offset = llvm::alignTo(Value: CGF.getContext().getTypeSize(T: Ty) / 8, Align: 4);
240
241 __overflow_area_pointer = CGF.Builder.CreateGEP(
242 Ty: CGF.Int8Ty, Ptr: __overflow_area_pointer,
243 IdxList: llvm::ConstantInt::get(Ty: CGF.Int32Ty, V: Offset),
244 Name: "__overflow_area_pointer.next");
245 CGF.Builder.CreateStore(Val: __overflow_area_pointer, Addr: __overflow_area_pointer_p);
246
247 return AddrTyped;
248}
249
250Address HexagonABIInfo::EmitVAArgForHexagon(CodeGenFunction &CGF,
251 Address VAListAddr,
252 QualType Ty) const {
253 // FIXME: Need to handle alignment
254 llvm::Type *BP = CGF.Int8PtrTy;
255 CGBuilderTy &Builder = CGF.Builder;
256 Address VAListAddrAsBPP = VAListAddr.withElementType(ElemTy: BP);
257 llvm::Value *Addr = Builder.CreateLoad(Addr: VAListAddrAsBPP, Name: "ap.cur");
258 // Handle address alignment for type alignment > 32 bits
259 uint64_t TyAlign = CGF.getContext().getTypeAlign(T: Ty) / 8;
260 if (TyAlign > 4) {
261 assert((TyAlign & (TyAlign - 1)) == 0 && "Alignment is not power of 2!");
262 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(V: Addr, DestTy: CGF.Int32Ty);
263 AddrAsInt = Builder.CreateAdd(LHS: AddrAsInt, RHS: Builder.getInt32(C: TyAlign - 1));
264 AddrAsInt = Builder.CreateAnd(LHS: AddrAsInt, RHS: Builder.getInt32(C: ~(TyAlign - 1)));
265 Addr = Builder.CreateIntToPtr(V: AddrAsInt, DestTy: BP);
266 }
267 Address AddrTyped =
268 Address(Addr, CGF.ConvertType(T: Ty), CharUnits::fromQuantity(Quantity: TyAlign));
269
270 uint64_t Offset = llvm::alignTo(Value: CGF.getContext().getTypeSize(T: Ty) / 8, Align: 4);
271 llvm::Value *NextAddr = Builder.CreateGEP(
272 Ty: CGF.Int8Ty, Ptr: Addr, IdxList: llvm::ConstantInt::get(Ty: CGF.Int32Ty, V: Offset), Name: "ap.next");
273 Builder.CreateStore(Val: NextAddr, Addr: VAListAddrAsBPP);
274
275 return AddrTyped;
276}
277
278Address HexagonABIInfo::EmitVAArgForHexagonLinux(CodeGenFunction &CGF,
279 Address VAListAddr,
280 QualType Ty) const {
281 int ArgSize = CGF.getContext().getTypeSize(T: Ty) / 8;
282
283 if (ArgSize > 8)
284 return EmitVAArgFromMemory(CGF, VAListAddr, Ty);
285
286 // Here we have check if the argument is in register area or
287 // in overflow area.
288 // If the saved register area pointer + argsize rounded up to alignment >
289 // saved register area end pointer, argument is in overflow area.
290 unsigned RegsLeft = 6;
291 Ty = CGF.getContext().getCanonicalType(T: Ty);
292 (void)classifyArgumentType(Ty, RegsLeft: &RegsLeft);
293
294 llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock(name: "vaarg.maybe_reg");
295 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock(name: "vaarg.in_reg");
296 llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock(name: "vaarg.on_stack");
297 llvm::BasicBlock *ContBlock = CGF.createBasicBlock(name: "vaarg.end");
298
299 // Get rounded size of the argument.GCC does not allow vararg of
300 // size < 4 bytes. We follow the same logic here.
301 ArgSize = (CGF.getContext().getTypeSize(T: Ty) <= 32) ? 4 : 8;
302 int ArgAlign = (CGF.getContext().getTypeSize(T: Ty) <= 32) ? 4 : 8;
303
304 // Argument may be in saved register area
305 CGF.EmitBlock(BB: MaybeRegBlock);
306
307 // Load the current saved register area pointer.
308 Address __current_saved_reg_area_pointer_p = CGF.Builder.CreateStructGEP(
309 Addr: VAListAddr, Index: 0, Name: "__current_saved_reg_area_pointer_p");
310 llvm::Value *__current_saved_reg_area_pointer = CGF.Builder.CreateLoad(
311 Addr: __current_saved_reg_area_pointer_p, Name: "__current_saved_reg_area_pointer");
312
313 // Load the saved register area end pointer.
314 Address __saved_reg_area_end_pointer_p = CGF.Builder.CreateStructGEP(
315 Addr: VAListAddr, Index: 1, Name: "__saved_reg_area_end_pointer_p");
316 llvm::Value *__saved_reg_area_end_pointer = CGF.Builder.CreateLoad(
317 Addr: __saved_reg_area_end_pointer_p, Name: "__saved_reg_area_end_pointer");
318
319 // If the size of argument is > 4 bytes, check if the stack
320 // location is aligned to 8 bytes
321 if (ArgAlign > 4) {
322
323 llvm::Value *__current_saved_reg_area_pointer_int =
324 CGF.Builder.CreatePtrToInt(V: __current_saved_reg_area_pointer,
325 DestTy: CGF.Int32Ty);
326
327 __current_saved_reg_area_pointer_int = CGF.Builder.CreateAdd(
328 LHS: __current_saved_reg_area_pointer_int,
329 RHS: llvm::ConstantInt::get(Ty: CGF.Int32Ty, V: (ArgAlign - 1)),
330 Name: "align_current_saved_reg_area_pointer");
331
332 __current_saved_reg_area_pointer_int = CGF.Builder.CreateAnd(
333 LHS: __current_saved_reg_area_pointer_int,
334 RHS: llvm::ConstantInt::getSigned(Ty: CGF.Int32Ty, V: -ArgAlign),
335 Name: "align_current_saved_reg_area_pointer");
336
337 __current_saved_reg_area_pointer =
338 CGF.Builder.CreateIntToPtr(V: __current_saved_reg_area_pointer_int,
339 DestTy: __current_saved_reg_area_pointer->getType(),
340 Name: "align_current_saved_reg_area_pointer");
341 }
342
343 llvm::Value *__new_saved_reg_area_pointer =
344 CGF.Builder.CreateGEP(Ty: CGF.Int8Ty, Ptr: __current_saved_reg_area_pointer,
345 IdxList: llvm::ConstantInt::get(Ty: CGF.Int32Ty, V: ArgSize),
346 Name: "__new_saved_reg_area_pointer");
347
348 llvm::Value *UsingStack = nullptr;
349 UsingStack = CGF.Builder.CreateICmpSGT(LHS: __new_saved_reg_area_pointer,
350 RHS: __saved_reg_area_end_pointer);
351
352 CGF.Builder.CreateCondBr(Cond: UsingStack, True: OnStackBlock, False: InRegBlock);
353
354 // Argument in saved register area
355 // Implement the block where argument is in register saved area
356 CGF.EmitBlock(BB: InRegBlock);
357
358 CGF.Builder.CreateStore(Val: __new_saved_reg_area_pointer,
359 Addr: __current_saved_reg_area_pointer_p);
360
361 CGF.EmitBranch(Block: ContBlock);
362
363 // Argument in overflow area
364 // Implement the block where the argument is in overflow area.
365 CGF.EmitBlock(BB: OnStackBlock);
366
367 // Load the overflow area pointer
368 Address __overflow_area_pointer_p =
369 CGF.Builder.CreateStructGEP(Addr: VAListAddr, Index: 2, Name: "__overflow_area_pointer_p");
370 llvm::Value *__overflow_area_pointer = CGF.Builder.CreateLoad(
371 Addr: __overflow_area_pointer_p, Name: "__overflow_area_pointer");
372
373 // Align the overflow area pointer according to the alignment of the argument
374 if (ArgAlign > 4) {
375 llvm::Value *__overflow_area_pointer_int =
376 CGF.Builder.CreatePtrToInt(V: __overflow_area_pointer, DestTy: CGF.Int32Ty);
377
378 __overflow_area_pointer_int =
379 CGF.Builder.CreateAdd(LHS: __overflow_area_pointer_int,
380 RHS: llvm::ConstantInt::get(Ty: CGF.Int32Ty, V: ArgAlign - 1),
381 Name: "align_overflow_area_pointer");
382
383 __overflow_area_pointer_int = CGF.Builder.CreateAnd(
384 LHS: __overflow_area_pointer_int,
385 RHS: llvm::ConstantInt::getSigned(Ty: CGF.Int32Ty, V: -ArgAlign),
386 Name: "align_overflow_area_pointer");
387
388 __overflow_area_pointer = CGF.Builder.CreateIntToPtr(
389 V: __overflow_area_pointer_int, DestTy: __overflow_area_pointer->getType(),
390 Name: "align_overflow_area_pointer");
391 }
392
393 // Get the pointer for next argument in overflow area and store it
394 // to overflow area pointer.
395 llvm::Value *__new_overflow_area_pointer = CGF.Builder.CreateGEP(
396 Ty: CGF.Int8Ty, Ptr: __overflow_area_pointer,
397 IdxList: llvm::ConstantInt::get(Ty: CGF.Int32Ty, V: ArgSize),
398 Name: "__overflow_area_pointer.next");
399
400 CGF.Builder.CreateStore(Val: __new_overflow_area_pointer,
401 Addr: __overflow_area_pointer_p);
402
403 CGF.Builder.CreateStore(Val: __new_overflow_area_pointer,
404 Addr: __current_saved_reg_area_pointer_p);
405
406 CGF.EmitBranch(Block: ContBlock);
407 // Get the correct pointer to load the variable argument
408 // Implement the ContBlock
409 CGF.EmitBlock(BB: ContBlock);
410
411 llvm::Type *MemTy = CGF.ConvertTypeForMem(T: Ty);
412 llvm::PHINode *ArgAddr = CGF.Builder.CreatePHI(
413 Ty: llvm::PointerType::getUnqual(C&: MemTy->getContext()), NumReservedValues: 2, Name: "vaarg.addr");
414 ArgAddr->addIncoming(V: __current_saved_reg_area_pointer, BB: InRegBlock);
415 ArgAddr->addIncoming(V: __overflow_area_pointer, BB: OnStackBlock);
416
417 return Address(ArgAddr, MemTy, CharUnits::fromQuantity(Quantity: ArgAlign));
418}
419
420RValue HexagonABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
421 QualType Ty, AggValueSlot Slot) const {
422
423 if (getTarget().getTriple().isMusl())
424 return CGF.EmitLoadOfAnyValue(
425 V: CGF.MakeAddrLValue(Addr: EmitVAArgForHexagonLinux(CGF, VAListAddr, Ty), T: Ty),
426 Slot);
427
428 return CGF.EmitLoadOfAnyValue(
429 V: CGF.MakeAddrLValue(Addr: EmitVAArgForHexagon(CGF, VAListAddr, Ty), T: Ty), Slot);
430}
431
432std::unique_ptr<TargetCodeGenInfo>
433CodeGen::createHexagonTargetCodeGenInfo(CodeGenModule &CGM) {
434 return std::make_unique<HexagonTargetCodeGenInfo>(args&: CGM.getTypes());
435}
436