Skip to content

Commit 1c207e8

Browse files
committed
[NFC][clang] Split clang/lib/CodeGen/CGBuiltin.cpp into target-specific files
clang/lib/CodeGen/CGBuiltin.cpp is over 1MB in size, and can take minutes to recompile (depending on compiler and host system) when modified. Splitting it has been discussed in this thread: https://discourse.llvm.org/t/splitting-clang-s-cgbuiltin-cpp-over-23k-lines-long-takes-1min-to-compile/ and the idea has received a number of +1 votes, hence this change.
1 parent 9b1f905 commit 1c207e8

File tree

13 files changed

+17944
-17657
lines changed

13 files changed

+17944
-17657
lines changed

clang/lib/CodeGen/BuiltinTargets/AArch64.cpp

Lines changed: 8040 additions & 0 deletions
Large diffs are not rendered by default.

clang/lib/CodeGen/BuiltinTargets/AMDGPU.cpp

Lines changed: 1884 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 267 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
1+
//===------ Hexagon.cpp - Emit LLVM Code for builtins ---------------------===//
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+
// This contains code to emit Builtin calls as LLVM code.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "ABIInfo.h"
14+
#include "CodeGenFunction.h"
15+
#include "TargetInfo.h"
16+
#include "clang/Basic/TargetBuiltins.h"
17+
#include "llvm/IR/InlineAsm.h"
18+
#include "llvm/IR/Intrinsics.h"
19+
#include "llvm/IR/IntrinsicsHexagon.h"
20+
21+
using namespace clang;
22+
using namespace CodeGen;
23+
using namespace llvm;
24+
25+
static std::pair<Intrinsic::ID, unsigned>
26+
getIntrinsicForHexagonNonClangBuiltin(unsigned BuiltinID) {
27+
struct Info {
28+
unsigned BuiltinID;
29+
Intrinsic::ID IntrinsicID;
30+
unsigned VecLen;
31+
};
32+
static Info Infos[] = {
33+
#define CUSTOM_BUILTIN_MAPPING(x,s) \
34+
{ Hexagon::BI__builtin_HEXAGON_##x, Intrinsic::hexagon_##x, s },
35+
CUSTOM_BUILTIN_MAPPING(L2_loadrub_pci, 0)
36+
CUSTOM_BUILTIN_MAPPING(L2_loadrb_pci, 0)
37+
CUSTOM_BUILTIN_MAPPING(L2_loadruh_pci, 0)
38+
CUSTOM_BUILTIN_MAPPING(L2_loadrh_pci, 0)
39+
CUSTOM_BUILTIN_MAPPING(L2_loadri_pci, 0)
40+
CUSTOM_BUILTIN_MAPPING(L2_loadrd_pci, 0)
41+
CUSTOM_BUILTIN_MAPPING(L2_loadrub_pcr, 0)
42+
CUSTOM_BUILTIN_MAPPING(L2_loadrb_pcr, 0)
43+
CUSTOM_BUILTIN_MAPPING(L2_loadruh_pcr, 0)
44+
CUSTOM_BUILTIN_MAPPING(L2_loadrh_pcr, 0)
45+
CUSTOM_BUILTIN_MAPPING(L2_loadri_pcr, 0)
46+
CUSTOM_BUILTIN_MAPPING(L2_loadrd_pcr, 0)
47+
CUSTOM_BUILTIN_MAPPING(S2_storerb_pci, 0)
48+
CUSTOM_BUILTIN_MAPPING(S2_storerh_pci, 0)
49+
CUSTOM_BUILTIN_MAPPING(S2_storerf_pci, 0)
50+
CUSTOM_BUILTIN_MAPPING(S2_storeri_pci, 0)
51+
CUSTOM_BUILTIN_MAPPING(S2_storerd_pci, 0)
52+
CUSTOM_BUILTIN_MAPPING(S2_storerb_pcr, 0)
53+
CUSTOM_BUILTIN_MAPPING(S2_storerh_pcr, 0)
54+
CUSTOM_BUILTIN_MAPPING(S2_storerf_pcr, 0)
55+
CUSTOM_BUILTIN_MAPPING(S2_storeri_pcr, 0)
56+
CUSTOM_BUILTIN_MAPPING(S2_storerd_pcr, 0)
57+
// Legacy builtins that take a vector in place of a vector predicate.
58+
CUSTOM_BUILTIN_MAPPING(V6_vmaskedstoreq, 64)
59+
CUSTOM_BUILTIN_MAPPING(V6_vmaskedstorenq, 64)
60+
CUSTOM_BUILTIN_MAPPING(V6_vmaskedstorentq, 64)
61+
CUSTOM_BUILTIN_MAPPING(V6_vmaskedstorentnq, 64)
62+
CUSTOM_BUILTIN_MAPPING(V6_vmaskedstoreq_128B, 128)
63+
CUSTOM_BUILTIN_MAPPING(V6_vmaskedstorenq_128B, 128)
64+
CUSTOM_BUILTIN_MAPPING(V6_vmaskedstorentq_128B, 128)
65+
CUSTOM_BUILTIN_MAPPING(V6_vmaskedstorentnq_128B, 128)
66+
#include "clang/Basic/BuiltinsHexagonMapCustomDep.def"
67+
#undef CUSTOM_BUILTIN_MAPPING
68+
};
69+
70+
auto CmpInfo = [] (Info A, Info B) { return A.BuiltinID < B.BuiltinID; };
71+
static const bool SortOnce = (llvm::sort(Infos, CmpInfo), true);
72+
(void)SortOnce;
73+
74+
const Info *F = llvm::lower_bound(Infos, Info{BuiltinID, 0, 0}, CmpInfo);
75+
if (F == std::end(Infos) || F->BuiltinID != BuiltinID)
76+
return {Intrinsic::not_intrinsic, 0};
77+
78+
return {F->IntrinsicID, F->VecLen};
79+
}
80+
81+
Value *CodeGenFunction::EmitHexagonBuiltinExpr(unsigned BuiltinID,
82+
const CallExpr *E) {
83+
Intrinsic::ID ID;
84+
unsigned VecLen;
85+
std::tie(ID, VecLen) = getIntrinsicForHexagonNonClangBuiltin(BuiltinID);
86+
87+
auto MakeCircOp = [this, E](unsigned IntID, bool IsLoad) {
88+
// The base pointer is passed by address, so it needs to be loaded.
89+
Address A = EmitPointerWithAlignment(E->getArg(0));
90+
Address BP = Address(A.emitRawPointer(*this), Int8PtrTy, A.getAlignment());
91+
llvm::Value *Base = Builder.CreateLoad(BP);
92+
// The treatment of both loads and stores is the same: the arguments for
93+
// the builtin are the same as the arguments for the intrinsic.
94+
// Load:
95+
// builtin(Base, Inc, Mod, Start) -> intr(Base, Inc, Mod, Start)
96+
// builtin(Base, Mod, Start) -> intr(Base, Mod, Start)
97+
// Store:
98+
// builtin(Base, Inc, Mod, Val, Start) -> intr(Base, Inc, Mod, Val, Start)
99+
// builtin(Base, Mod, Val, Start) -> intr(Base, Mod, Val, Start)
100+
SmallVector<llvm::Value*,5> Ops = { Base };
101+
for (unsigned i = 1, e = E->getNumArgs(); i != e; ++i)
102+
Ops.push_back(EmitScalarExpr(E->getArg(i)));
103+
104+
llvm::Value *Result = Builder.CreateCall(CGM.getIntrinsic(IntID), Ops);
105+
// The load intrinsics generate two results (Value, NewBase), stores
106+
// generate one (NewBase). The new base address needs to be stored.
107+
llvm::Value *NewBase = IsLoad ? Builder.CreateExtractValue(Result, 1)
108+
: Result;
109+
llvm::Value *LV = EmitScalarExpr(E->getArg(0));
110+
Address Dest = EmitPointerWithAlignment(E->getArg(0));
111+
llvm::Value *RetVal =
112+
Builder.CreateAlignedStore(NewBase, LV, Dest.getAlignment());
113+
if (IsLoad)
114+
RetVal = Builder.CreateExtractValue(Result, 0);
115+
return RetVal;
116+
};
117+
118+
// Handle the conversion of bit-reverse load intrinsics to bit code.
119+
// The intrinsic call after this function only reads from memory and the
120+
// write to memory is dealt by the store instruction.
121+
auto MakeBrevLd = [this, E](unsigned IntID, llvm::Type *DestTy) {
122+
// The intrinsic generates one result, which is the new value for the base
123+
// pointer. It needs to be returned. The result of the load instruction is
124+
// passed to intrinsic by address, so the value needs to be stored.
125+
llvm::Value *BaseAddress = EmitScalarExpr(E->getArg(0));
126+
127+
// Expressions like &(*pt++) will be incremented per evaluation.
128+
// EmitPointerWithAlignment and EmitScalarExpr evaluates the expression
129+
// per call.
130+
Address DestAddr = EmitPointerWithAlignment(E->getArg(1));
131+
DestAddr = DestAddr.withElementType(Int8Ty);
132+
llvm::Value *DestAddress = DestAddr.emitRawPointer(*this);
133+
134+
// Operands are Base, Dest, Modifier.
135+
// The intrinsic format in LLVM IR is defined as
136+
// { ValueType, i8* } (i8*, i32).
137+
llvm::Value *Result = Builder.CreateCall(
138+
CGM.getIntrinsic(IntID), {BaseAddress, EmitScalarExpr(E->getArg(2))});
139+
140+
// The value needs to be stored as the variable is passed by reference.
141+
llvm::Value *DestVal = Builder.CreateExtractValue(Result, 0);
142+
143+
// The store needs to be truncated to fit the destination type.
144+
// While i32 and i64 are natively supported on Hexagon, i8 and i16 needs
145+
// to be handled with stores of respective destination type.
146+
DestVal = Builder.CreateTrunc(DestVal, DestTy);
147+
148+
Builder.CreateAlignedStore(DestVal, DestAddress, DestAddr.getAlignment());
149+
// The updated value of the base pointer is returned.
150+
return Builder.CreateExtractValue(Result, 1);
151+
};
152+
153+
auto V2Q = [this, VecLen] (llvm::Value *Vec) {
154+
Intrinsic::ID ID = VecLen == 128 ? Intrinsic::hexagon_V6_vandvrt_128B
155+
: Intrinsic::hexagon_V6_vandvrt;
156+
return Builder.CreateCall(CGM.getIntrinsic(ID),
157+
{Vec, Builder.getInt32(-1)});
158+
};
159+
auto Q2V = [this, VecLen] (llvm::Value *Pred) {
160+
Intrinsic::ID ID = VecLen == 128 ? Intrinsic::hexagon_V6_vandqrt_128B
161+
: Intrinsic::hexagon_V6_vandqrt;
162+
return Builder.CreateCall(CGM.getIntrinsic(ID),
163+
{Pred, Builder.getInt32(-1)});
164+
};
165+
166+
switch (BuiltinID) {
167+
// These intrinsics return a tuple {Vector, VectorPred} in LLVM IR,
168+
// and the corresponding C/C++ builtins use loads/stores to update
169+
// the predicate.
170+
case Hexagon::BI__builtin_HEXAGON_V6_vaddcarry:
171+
case Hexagon::BI__builtin_HEXAGON_V6_vaddcarry_128B:
172+
case Hexagon::BI__builtin_HEXAGON_V6_vsubcarry:
173+
case Hexagon::BI__builtin_HEXAGON_V6_vsubcarry_128B: {
174+
// Get the type from the 0-th argument.
175+
llvm::Type *VecType = ConvertType(E->getArg(0)->getType());
176+
Address PredAddr =
177+
EmitPointerWithAlignment(E->getArg(2)).withElementType(VecType);
178+
llvm::Value *PredIn = V2Q(Builder.CreateLoad(PredAddr));
179+
llvm::Value *Result = Builder.CreateCall(CGM.getIntrinsic(ID),
180+
{EmitScalarExpr(E->getArg(0)), EmitScalarExpr(E->getArg(1)), PredIn});
181+
182+
llvm::Value *PredOut = Builder.CreateExtractValue(Result, 1);
183+
Builder.CreateAlignedStore(Q2V(PredOut), PredAddr.emitRawPointer(*this),
184+
PredAddr.getAlignment());
185+
return Builder.CreateExtractValue(Result, 0);
186+
}
187+
// These are identical to the builtins above, except they don't consume
188+
// input carry, only generate carry-out. Since they still produce two
189+
// outputs, generate the store of the predicate, but no load.
190+
case Hexagon::BI__builtin_HEXAGON_V6_vaddcarryo:
191+
case Hexagon::BI__builtin_HEXAGON_V6_vaddcarryo_128B:
192+
case Hexagon::BI__builtin_HEXAGON_V6_vsubcarryo:
193+
case Hexagon::BI__builtin_HEXAGON_V6_vsubcarryo_128B: {
194+
// Get the type from the 0-th argument.
195+
llvm::Type *VecType = ConvertType(E->getArg(0)->getType());
196+
Address PredAddr =
197+
EmitPointerWithAlignment(E->getArg(2)).withElementType(VecType);
198+
llvm::Value *Result = Builder.CreateCall(CGM.getIntrinsic(ID),
199+
{EmitScalarExpr(E->getArg(0)), EmitScalarExpr(E->getArg(1))});
200+
201+
llvm::Value *PredOut = Builder.CreateExtractValue(Result, 1);
202+
Builder.CreateAlignedStore(Q2V(PredOut), PredAddr.emitRawPointer(*this),
203+
PredAddr.getAlignment());
204+
return Builder.CreateExtractValue(Result, 0);
205+
}
206+
207+
case Hexagon::BI__builtin_HEXAGON_V6_vmaskedstoreq:
208+
case Hexagon::BI__builtin_HEXAGON_V6_vmaskedstorenq:
209+
case Hexagon::BI__builtin_HEXAGON_V6_vmaskedstorentq:
210+
case Hexagon::BI__builtin_HEXAGON_V6_vmaskedstorentnq:
211+
case Hexagon::BI__builtin_HEXAGON_V6_vmaskedstoreq_128B:
212+
case Hexagon::BI__builtin_HEXAGON_V6_vmaskedstorenq_128B:
213+
case Hexagon::BI__builtin_HEXAGON_V6_vmaskedstorentq_128B:
214+
case Hexagon::BI__builtin_HEXAGON_V6_vmaskedstorentnq_128B: {
215+
SmallVector<llvm::Value*,4> Ops;
216+
const Expr *PredOp = E->getArg(0);
217+
// There will be an implicit cast to a boolean vector. Strip it.
218+
if (auto *Cast = dyn_cast<ImplicitCastExpr>(PredOp)) {
219+
if (Cast->getCastKind() == CK_BitCast)
220+
PredOp = Cast->getSubExpr();
221+
Ops.push_back(V2Q(EmitScalarExpr(PredOp)));
222+
}
223+
for (int i = 1, e = E->getNumArgs(); i != e; ++i)
224+
Ops.push_back(EmitScalarExpr(E->getArg(i)));
225+
return Builder.CreateCall(CGM.getIntrinsic(ID), Ops);
226+
}
227+
228+
case Hexagon::BI__builtin_HEXAGON_L2_loadrub_pci:
229+
case Hexagon::BI__builtin_HEXAGON_L2_loadrb_pci:
230+
case Hexagon::BI__builtin_HEXAGON_L2_loadruh_pci:
231+
case Hexagon::BI__builtin_HEXAGON_L2_loadrh_pci:
232+
case Hexagon::BI__builtin_HEXAGON_L2_loadri_pci:
233+
case Hexagon::BI__builtin_HEXAGON_L2_loadrd_pci:
234+
case Hexagon::BI__builtin_HEXAGON_L2_loadrub_pcr:
235+
case Hexagon::BI__builtin_HEXAGON_L2_loadrb_pcr:
236+
case Hexagon::BI__builtin_HEXAGON_L2_loadruh_pcr:
237+
case Hexagon::BI__builtin_HEXAGON_L2_loadrh_pcr:
238+
case Hexagon::BI__builtin_HEXAGON_L2_loadri_pcr:
239+
case Hexagon::BI__builtin_HEXAGON_L2_loadrd_pcr:
240+
return MakeCircOp(ID, /*IsLoad=*/true);
241+
case Hexagon::BI__builtin_HEXAGON_S2_storerb_pci:
242+
case Hexagon::BI__builtin_HEXAGON_S2_storerh_pci:
243+
case Hexagon::BI__builtin_HEXAGON_S2_storerf_pci:
244+
case Hexagon::BI__builtin_HEXAGON_S2_storeri_pci:
245+
case Hexagon::BI__builtin_HEXAGON_S2_storerd_pci:
246+
case Hexagon::BI__builtin_HEXAGON_S2_storerb_pcr:
247+
case Hexagon::BI__builtin_HEXAGON_S2_storerh_pcr:
248+
case Hexagon::BI__builtin_HEXAGON_S2_storerf_pcr:
249+
case Hexagon::BI__builtin_HEXAGON_S2_storeri_pcr:
250+
case Hexagon::BI__builtin_HEXAGON_S2_storerd_pcr:
251+
return MakeCircOp(ID, /*IsLoad=*/false);
252+
case Hexagon::BI__builtin_brev_ldub:
253+
return MakeBrevLd(Intrinsic::hexagon_L2_loadrub_pbr, Int8Ty);
254+
case Hexagon::BI__builtin_brev_ldb:
255+
return MakeBrevLd(Intrinsic::hexagon_L2_loadrb_pbr, Int8Ty);
256+
case Hexagon::BI__builtin_brev_lduh:
257+
return MakeBrevLd(Intrinsic::hexagon_L2_loadruh_pbr, Int16Ty);
258+
case Hexagon::BI__builtin_brev_ldh:
259+
return MakeBrevLd(Intrinsic::hexagon_L2_loadrh_pbr, Int16Ty);
260+
case Hexagon::BI__builtin_brev_ldw:
261+
return MakeBrevLd(Intrinsic::hexagon_L2_loadri_pbr, Int32Ty);
262+
case Hexagon::BI__builtin_brev_ldd:
263+
return MakeBrevLd(Intrinsic::hexagon_L2_loadrd_pbr, Int64Ty);
264+
} // switch
265+
266+
return nullptr;
267+
}

0 commit comments

Comments
 (0)