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;
32 auto TempNode = MDNode::getTemporary(Ctx, None);
33 Args.push_back(TempNode.get());
37 Metadata *Vals[] = {MDString::get(Ctx,
"llvm.loop.vectorize.width"),
38 ConstantAsMetadata::get(ConstantInt::get(
40 Args.push_back(MDNode::get(Ctx, Vals));
45 Metadata *Vals[] = {MDString::get(Ctx,
"llvm.loop.interleave.count"),
46 ConstantAsMetadata::get(ConstantInt::get(
48 Args.push_back(MDNode::get(Ctx, Vals));
53 Metadata *Vals[] = {MDString::get(Ctx,
"llvm.loop.unroll.count"),
54 ConstantAsMetadata::get(ConstantInt::get(
56 Args.push_back(MDNode::get(Ctx, Vals));
61 Metadata *Vals[] = {MDString::get(Ctx,
"llvm.loop.vectorize.enable"),
62 ConstantAsMetadata::get(ConstantInt::get(
65 Args.push_back(MDNode::get(Ctx, Vals));
72 Name =
"llvm.loop.unroll.enable";
74 Name =
"llvm.loop.unroll.full";
76 Name =
"llvm.loop.unroll.disable";
77 Metadata *Vals[] = {MDString::get(Ctx, Name)};
78 Args.push_back(MDNode::get(Ctx, Vals));
82 MDNode *LoopID = MDNode::get(Ctx, Args);
83 LoopID->replaceOperandWith(0, LoopID);
90 InterleaveCount(0), UnrollCount(0) {}
102 : LoopID(nullptr), Header(Header), Attrs(Attrs) {
107 Active.push_back(
LoopInfo(Header, StagedAttrs));
116 for (
const auto *
Attr : Attrs) {
117 const LoopHintAttr *LH = dyn_cast<LoopHintAttr>(
Attr);
123 auto *ValueExpr = LH->getValue();
124 unsigned ValueInt = 1;
126 llvm::APSInt ValueAPS = ValueExpr->EvaluateKnownConstInt(Ctx);
127 ValueInt = ValueAPS.getSExtValue();
130 LoopHintAttr::OptionType Option = LH->getOption();
131 LoopHintAttr::LoopHintState
State = LH->getState();
133 case LoopHintAttr::Disable:
135 case LoopHintAttr::Vectorize:
139 case LoopHintAttr::Interleave:
143 case LoopHintAttr::Unroll:
146 case LoopHintAttr::UnrollCount:
147 case LoopHintAttr::VectorizeWidth:
148 case LoopHintAttr::InterleaveCount:
149 llvm_unreachable(
"Options cannot be disabled.");
153 case LoopHintAttr::Enable:
155 case LoopHintAttr::Vectorize:
156 case LoopHintAttr::Interleave:
159 case LoopHintAttr::Unroll:
162 case LoopHintAttr::UnrollCount:
163 case LoopHintAttr::VectorizeWidth:
164 case LoopHintAttr::InterleaveCount:
165 llvm_unreachable(
"Options cannot enabled.");
169 case LoopHintAttr::AssumeSafety:
171 case LoopHintAttr::Vectorize:
172 case LoopHintAttr::Interleave:
177 case LoopHintAttr::Unroll:
178 case LoopHintAttr::UnrollCount:
179 case LoopHintAttr::VectorizeWidth:
180 case LoopHintAttr::InterleaveCount:
181 llvm_unreachable(
"Options cannot be used to assume mem safety.");
185 case LoopHintAttr::Full:
187 case LoopHintAttr::Unroll:
190 case LoopHintAttr::Vectorize:
191 case LoopHintAttr::Interleave:
192 case LoopHintAttr::UnrollCount:
193 case LoopHintAttr::VectorizeWidth:
194 case LoopHintAttr::InterleaveCount:
195 llvm_unreachable(
"Options cannot be used with 'full' hint.");
199 case LoopHintAttr::Numeric:
201 case LoopHintAttr::VectorizeWidth:
204 case LoopHintAttr::InterleaveCount:
207 case LoopHintAttr::UnrollCount:
210 case LoopHintAttr::Unroll:
211 case LoopHintAttr::Vectorize:
212 case LoopHintAttr::Interleave:
213 llvm_unreachable(
"Options cannot be assigned a value.");
225 assert(!Active.empty() &&
"No active loops to pop");
237 if (TerminatorInst *TI = dyn_cast<TerminatorInst>(I)) {
238 for (
unsigned i = 0, ie = TI->getNumSuccessors(); i < ie; ++i)
239 if (TI->getSuccessor(i) == L.
getHeader()) {
240 TI->setMetadata(
"llvm.loop", L.
getLoopID());
247 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.
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.
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.
void push(llvm::BasicBlock *Header)
Begin a new structured loop.
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.
static MDNode * createMetadata(LLVMContext &Ctx, const LoopAttributes &Attrs)
LoopInfo(llvm::BasicBlock *Header, const LoopAttributes &Attrs)
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.