14 #include "llvm/IR/BasicBlock.h"
15 #include "llvm/IR/Constants.h"
16 #include "llvm/IR/InstrTypes.h"
17 #include "llvm/IR/Instructions.h"
18 #include "llvm/IR/Metadata.h"
19 using namespace clang::CodeGen;
23 llvm::DebugLoc Location) {
35 auto TempNode = MDNode::getTemporary(Ctx, None);
36 Args.push_back(TempNode.get());
40 Args.push_back(Location.getAsMDNode());
44 Metadata *Vals[] = {MDString::get(Ctx,
"llvm.loop.vectorize.width"),
45 ConstantAsMetadata::get(ConstantInt::get(
47 Args.push_back(MDNode::get(Ctx, Vals));
52 Metadata *Vals[] = {MDString::get(Ctx,
"llvm.loop.interleave.count"),
53 ConstantAsMetadata::get(ConstantInt::get(
55 Args.push_back(MDNode::get(Ctx, Vals));
60 Metadata *Vals[] = {MDString::get(Ctx,
"llvm.loop.unroll.count"),
61 ConstantAsMetadata::get(ConstantInt::get(
63 Args.push_back(MDNode::get(Ctx, Vals));
68 Metadata *Vals[] = {MDString::get(Ctx,
"llvm.loop.vectorize.enable"),
69 ConstantAsMetadata::get(ConstantInt::get(
72 Args.push_back(MDNode::get(Ctx, Vals));
79 Name =
"llvm.loop.unroll.enable";
81 Name =
"llvm.loop.unroll.full";
83 Name =
"llvm.loop.unroll.disable";
84 Metadata *Vals[] = {MDString::get(Ctx, Name)};
85 Args.push_back(MDNode::get(Ctx, Vals));
89 Metadata *Vals[] = {MDString::get(Ctx,
"llvm.loop.distribute.enable"),
90 ConstantAsMetadata::get(ConstantInt::get(
93 Args.push_back(MDNode::get(Ctx, Vals));
97 MDNode *LoopID = MDNode::get(Ctx, Args);
98 LoopID->replaceOperandWith(0, LoopID);
105 InterleaveCount(0), UnrollCount(0),
118 llvm::DebugLoc Location)
119 : LoopID(nullptr), Header(Header), Attrs(Attrs) {
124 Active.push_back(
LoopInfo(Header, StagedAttrs, Location));
131 llvm::DebugLoc Location) {
134 for (
const auto *
Attr : Attrs) {
135 const LoopHintAttr *LH = dyn_cast<LoopHintAttr>(
Attr);
136 const OpenCLUnrollHintAttr *OpenCLHint =
137 dyn_cast<OpenCLUnrollHintAttr>(
Attr);
140 if (!LH && !OpenCLHint) {
144 LoopHintAttr::OptionType Option = LoopHintAttr::Unroll;
145 LoopHintAttr::LoopHintState
State = LoopHintAttr::Disable;
146 unsigned ValueInt = 1;
154 ValueInt = OpenCLHint->getUnrollHint();
156 State = LoopHintAttr::Full;
157 }
else if (ValueInt != 1) {
158 Option = LoopHintAttr::UnrollCount;
159 State = LoopHintAttr::Numeric;
162 auto *ValueExpr = LH->getValue();
164 llvm::APSInt ValueAPS = ValueExpr->EvaluateKnownConstInt(Ctx);
165 ValueInt = ValueAPS.getSExtValue();
168 Option = LH->getOption();
169 State = LH->getState();
172 case LoopHintAttr::Disable:
174 case LoopHintAttr::Vectorize:
178 case LoopHintAttr::Interleave:
182 case LoopHintAttr::Unroll:
185 case LoopHintAttr::Distribute:
188 case LoopHintAttr::UnrollCount:
189 case LoopHintAttr::VectorizeWidth:
190 case LoopHintAttr::InterleaveCount:
191 llvm_unreachable(
"Options cannot be disabled.");
195 case LoopHintAttr::Enable:
197 case LoopHintAttr::Vectorize:
198 case LoopHintAttr::Interleave:
201 case LoopHintAttr::Unroll:
204 case LoopHintAttr::Distribute:
207 case LoopHintAttr::UnrollCount:
208 case LoopHintAttr::VectorizeWidth:
209 case LoopHintAttr::InterleaveCount:
210 llvm_unreachable(
"Options cannot enabled.");
214 case LoopHintAttr::AssumeSafety:
216 case LoopHintAttr::Vectorize:
217 case LoopHintAttr::Interleave:
222 case LoopHintAttr::Unroll:
223 case LoopHintAttr::UnrollCount:
224 case LoopHintAttr::VectorizeWidth:
225 case LoopHintAttr::InterleaveCount:
226 case LoopHintAttr::Distribute:
227 llvm_unreachable(
"Options cannot be used to assume mem safety.");
231 case LoopHintAttr::Full:
233 case LoopHintAttr::Unroll:
236 case LoopHintAttr::Vectorize:
237 case LoopHintAttr::Interleave:
238 case LoopHintAttr::UnrollCount:
239 case LoopHintAttr::VectorizeWidth:
240 case LoopHintAttr::InterleaveCount:
241 case LoopHintAttr::Distribute:
242 llvm_unreachable(
"Options cannot be used with 'full' hint.");
246 case LoopHintAttr::Numeric:
248 case LoopHintAttr::VectorizeWidth:
251 case LoopHintAttr::InterleaveCount:
254 case LoopHintAttr::UnrollCount:
257 case LoopHintAttr::Unroll:
258 case LoopHintAttr::Vectorize:
259 case LoopHintAttr::Interleave:
260 case LoopHintAttr::Distribute:
261 llvm_unreachable(
"Options cannot be assigned a value.");
269 push(Header, Location);
273 assert(!Active.empty() &&
"No active loops to pop");
285 if (TerminatorInst *TI = dyn_cast<TerminatorInst>(I)) {
286 for (
unsigned i = 0, ie = TI->getNumSuccessors(); i < ie; ++i)
287 if (TI->getSuccessor(i) == L.
getHeader()) {
288 TI->setMetadata(llvm::LLVMContext::MD_loop, L.
getLoopID());
295 I->setMetadata(
"llvm.mem.parallel_loop_access", L.
getLoopID());
Defines the clang::ASTContext interface.
void setUnrollCount(unsigned C)
Set the unroll count for the next loop pushed.
void push(llvm::BasicBlock *Header, llvm::DebugLoc Location=llvm::DebugLoc())
Begin a new structured loop.
Attributes that may be specified on loops.
Information used when generating a structured loop.
LoopAttributes(bool IsParallel=false)
LVEnableState UnrollEnable
Value for llvm.loop.unroll.* metadata (enable, disable, or full).
class LLVM_ALIGNAS(8) DependentTemplateSpecializationType const IdentifierInfo * Name
Represents a template specialization type whose template cannot be resolved, e.g. ...
llvm::BasicBlock * getHeader() const
Get the header block of this loop.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
unsigned InterleaveCount
Value for llvm.loop.interleave.count metadata.
static MDNode * createMetadata(LLVMContext &Ctx, const LoopAttributes &Attrs, llvm::DebugLoc Location)
LVEnableState VectorizeEnable
Value for llvm.loop.vectorize.enable metadata.
void pop()
End the current loop.
Whether values of this type can be null is (explicitly) unspecified.
void setVectorizeWidth(unsigned W)
Set the vectorize width for the next loop pushed.
detail::InMemoryDirectory::const_iterator I
llvm::MDNode * getLoopID() const
Get the loop id metadata for this loop.
bool IsParallel
Generate llvm.loop.parallel metadata for loads and stores.
void setInterleaveCount(unsigned C)
Set the interleave count for the next loop pushed.
unsigned UnrollCount
llvm.unroll.
void setParallel(bool Enable=true)
Set the next pushed loop as parallel.
LVEnableState DistributeEnable
Value for llvm.loop.distribute.enable metadata.
void setDistributeState(bool Enable=true)
Set the next pushed loop as a distribution candidate.
LoopInfo(llvm::BasicBlock *Header, const LoopAttributes &Attrs, llvm::DebugLoc Location)
Construct a new LoopInfo for the loop with entry Header.
void InsertHelper(llvm::Instruction *I) const
Function called by the CodeGenFunction when an instruction is created.
const LoopAttributes & getAttributes() const
Get the set of attributes active for this loop.
void setUnrollState(const LoopAttributes::LVEnableState &State)
Set the next pushed loop unroll state.
void setVectorizeEnable(bool Enable=true)
Set the next pushed loop 'vectorize.enable'.
unsigned VectorizeWidth
Value for llvm.loop.vectorize.width metadata.
Attr - This represents one attribute.