16bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines//===---- CGLoopInfo.cpp - LLVM CodeGen for loop metadata -*- C++ -*-------===//
26bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines//
36bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines//                     The LLVM Compiler Infrastructure
46bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines//
56bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines// This file is distributed under the University of Illinois Open Source
66bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines// License. See LICENSE.TXT for details.
76bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines//
86bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines//===----------------------------------------------------------------------===//
96bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
106bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#include "CGLoopInfo.h"
116bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#include "llvm/IR/BasicBlock.h"
126bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#include "llvm/IR/Constants.h"
136bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#include "llvm/IR/InstrTypes.h"
146bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#include "llvm/IR/Instructions.h"
156bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#include "llvm/IR/Metadata.h"
166bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesusing namespace clang;
176bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesusing namespace CodeGen;
186bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesusing namespace llvm;
196bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
206bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesstatic MDNode *createMetadata(LLVMContext &Ctx, const LoopAttributes &Attrs) {
216bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
226bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  if (!Attrs.IsParallel && Attrs.VectorizerWidth == 0 &&
236bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      Attrs.VectorizerUnroll == 0 &&
246bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      Attrs.VectorizerEnable == LoopAttributes::VecUnspecified)
256bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
266bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
270e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  SmallVector<Metadata *, 4> Args;
286bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  // Reserve operand 0 for loop id self reference.
290e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  auto TempNode = MDNode::getTemporary(Ctx, None);
300e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  Args.push_back(TempNode.get());
316bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
326bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  // Setting vectorizer.width
336bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  if (Attrs.VectorizerWidth > 0) {
340e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    Metadata *Vals[] = {MDString::get(Ctx, "llvm.loop.vectorize.width"),
350e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                        ConstantAsMetadata::get(ConstantInt::get(
360e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                            Type::getInt32Ty(Ctx), Attrs.VectorizerWidth))};
376bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    Args.push_back(MDNode::get(Ctx, Vals));
386bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  }
396bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
406bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  // Setting vectorizer.unroll
416bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  if (Attrs.VectorizerUnroll > 0) {
420e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    Metadata *Vals[] = {MDString::get(Ctx, "llvm.loop.interleave.count"),
430e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                        ConstantAsMetadata::get(ConstantInt::get(
440e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                            Type::getInt32Ty(Ctx), Attrs.VectorizerUnroll))};
456bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    Args.push_back(MDNode::get(Ctx, Vals));
466bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  }
476bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
486bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  // Setting vectorizer.enable
496bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  if (Attrs.VectorizerEnable != LoopAttributes::VecUnspecified) {
500e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    Metadata *Vals[] = {
510e2c34f92f00628d48968dfea096d36381f494cbStephen Hines        MDString::get(Ctx, "llvm.loop.vectorize.enable"),
520e2c34f92f00628d48968dfea096d36381f494cbStephen Hines        ConstantAsMetadata::get(ConstantInt::get(
530e2c34f92f00628d48968dfea096d36381f494cbStephen Hines            Type::getInt1Ty(Ctx),
540e2c34f92f00628d48968dfea096d36381f494cbStephen Hines            (Attrs.VectorizerEnable == LoopAttributes::VecEnable)))};
556bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    Args.push_back(MDNode::get(Ctx, Vals));
566bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  }
576bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
586bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  // Set the first operand to itself.
590e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  MDNode *LoopID = MDNode::get(Ctx, Args);
606bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  LoopID->replaceOperandWith(0, LoopID);
616bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  return LoopID;
626bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines}
636bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
646bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen HinesLoopAttributes::LoopAttributes(bool IsParallel)
656bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    : IsParallel(IsParallel), VectorizerEnable(LoopAttributes::VecUnspecified),
666bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      VectorizerWidth(0), VectorizerUnroll(0) {}
676bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
686bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesvoid LoopAttributes::clear() {
696bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  IsParallel = false;
706bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  VectorizerWidth = 0;
716bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  VectorizerUnroll = 0;
726bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  VectorizerEnable = LoopAttributes::VecUnspecified;
736bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines}
746bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
756bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen HinesLoopInfo::LoopInfo(BasicBlock *Header, const LoopAttributes &Attrs)
766bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    : LoopID(nullptr), Header(Header), Attrs(Attrs) {
776bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  LoopID = createMetadata(Header->getContext(), Attrs);
786bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines}
796bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
806bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesvoid LoopInfoStack::push(BasicBlock *Header) {
816bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  Active.push_back(LoopInfo(Header, StagedAttrs));
826bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  // Clear the attributes so nested loops do not inherit them.
836bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  StagedAttrs.clear();
846bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines}
856bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
866bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesvoid LoopInfoStack::pop() {
876bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  assert(!Active.empty() && "No active loops to pop");
886bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  Active.pop_back();
896bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines}
906bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
916bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesvoid LoopInfoStack::InsertHelper(Instruction *I) const {
926bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  if (!hasInfo())
936bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return;
946bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
956bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  const LoopInfo &L = getInfo();
966bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  if (!L.getLoopID())
976bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return;
986bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
996bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  if (TerminatorInst *TI = dyn_cast<TerminatorInst>(I)) {
1006bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    for (unsigned i = 0, ie = TI->getNumSuccessors(); i < ie; ++i)
1016bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      if (TI->getSuccessor(i) == L.getHeader()) {
1026bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        TI->setMetadata("llvm.loop", L.getLoopID());
1036bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        break;
1046bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      }
1056bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return;
1066bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  }
1076bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
1086bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  if (L.getAttributes().IsParallel && I->mayReadOrWriteMemory())
1096bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    I->setMetadata("llvm.mem.parallel_loop_access", L.getLoopID());
1106bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines}
111