LLVM: lib/SandboxIR/Region.cpp Source File (original) (raw)

1

2

3

4

5

6

7

8

11

13

17 return TTI.getInstructionCost(LLVMI, Operands, CostKind);

18}

19

21 auto Cost = getCost(I);

22 if (Rgn.contains(I))

23

24 AfterCost -= Cost;

25 else

26

27

28 BeforeCost += Cost;

29}

30

31#ifndef NDEBUG

33#endif

34

36 : Ctx(Ctx), Scoreboard(*this, TTI) {

38 auto *RegionStrMD = MDString::get(LLVMCtx, RegionStr);

40

41 CreateInstCB = Ctx.registerCreateInstrCallback(

42 [this](Instruction *NewInst) { add(NewInst); });

43 EraseInstCB = Ctx.registerEraseInstrCallback([this](Instruction *ErasedInst) {

44 remove(ErasedInst);

45 removeFromAux(ErasedInst);

46 });

47}

48

50 Ctx.unregisterCreateInstrCallback(CreateInstCB);

51 Ctx.unregisterEraseInstrCallback(EraseInstCB);

52}

53

54void Region::addImpl(Instruction *I, bool IgnoreCost) {

56

58 if (!IgnoreCost)

59

60 Scoreboard.add(I);

61}

62

65 auto &LLVMCtx = Ctx.LLVMCtx;

66 for (auto [Idx, I] : enumerate(Aux)) {

70 "Instruction already in Aux!");

73

74 addImpl(I, true);

75 }

76}

77

78void Region::setAux(unsigned Idx, Instruction *I) {

79 assert((Idx >= Aux.size() || Aux[Idx] == nullptr) &&

80 "There is already an Instruction at Idx in Aux!");

81 unsigned ExpectedSz = Idx + 1;

82 if (Aux.size() < ExpectedSz) {

83 auto SzBefore = Aux.size();

84 Aux.resize(ExpectedSz);

85

86 for (unsigned Idx = SzBefore; Idx + 1 < ExpectedSz; ++Idx)

87 Aux[Idx] = nullptr;

88 }

89 Aux[Idx] = I;

90

91 addImpl(I, true);

92}

93

96 LLVMI->setMetadata(AuxMDKind, nullptr);

97}

98

100 auto It = find(Aux, I);

101 if (It == Aux.end())

102 return;

103 dropAuxMetadata(I);

104 Aux.erase(It);

105}

106

108 for (unsigned Idx : seq(0, Aux.size()))

109 dropAuxMetadata(Aux[Idx]);

110 Aux.clear();

111}

112

114

115

117

120}

121

122#ifndef NDEBUG

124 if (Insts.size() != Other.Insts.size())

125 return false;

126 if (!std::is_permutation(Insts.begin(), Insts.end(), Other.Insts.begin()))

127 return false;

128 return true;

129}

130

132 for (auto *I : Insts)

133 OS << *I << "\n";

134 if (!Aux.empty()) {

135 OS << "\nAux:\n";

136 for (auto *I : Aux) {

137 if (I == nullptr)

138 OS << "NULL\n";

139 else

140 OS << *I << "\n";

141 }

142 }

143}

144

149#endif

150

155 auto &Ctx = F.getContext();

160 if (auto *MDN = LLVMI->getMetadata(MDKind)) {

161 auto [It, Inserted] = MDNToRegion.try_emplace(MDN);

162 if (Inserted) {

163 Regions.push_back(std::make_unique(Ctx, TTI));

164 R = Regions.back().get();

165 It->second = R;

166 } else {

167 R = It->second;

168 }

169 R->addImpl(&Inst, true);

170 }

171 if (auto *AuxMDN = LLVMI->getMetadata(AuxMDKind)) {

175 if (R == nullptr) {

176 errs() << "No region specified for Aux: '" << *LLVMI << "'\n";

177 exit(1);

178 }

179 R->setAux(Idx, &Inst);

180 }

181 }

182 }

183#ifndef NDEBUG

184

185 for (auto &RPtr : Regions)

186 for (auto *I : RPtr->getAux())

187 assert(I != nullptr && "Gap in Aux!");

188#endif

189 return Regions;

190}

191

192}

assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")

ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...

This is the shared class of boolean and integer constants.

This is an important base class in LLVM.

std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)

This is an important class for using LLVM in a threaded context.

static MDTuple * getDistinct(LLVMContext &Context, ArrayRef< Metadata * > MDs)

static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)

static LLVM_ABI MDString * get(LLVMContext &Context, StringRef Str)

bool remove(const value_type &X)

Remove an item from the set vector.

bool insert(const value_type &X)

Insert a new element into the SetVector.

void push_back(const T &Elt)

This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.

This pass provides access to the codegen interfaces that are needed for IR-level transformations.

static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)

This class implements an extremely fast bulk output stream that can only output to a stream.

A sandboxir::User with operands, opcode and linked with previous/next instructions in an instruction ...

void dump() const

Definition Region.cpp:145

LLVM_ABI_FOR_TEST bool operator==(const Region &Other) const

This is an expensive check, meant for testing.

Definition Region.cpp:123

LLVM_ABI Region(Context &Ctx, TargetTransformInfo &TTI)

Definition Region.cpp:35

static LLVM_ABI SmallVector< std::unique_ptr< Region > > createRegionsFromMD(Function &F, TargetTransformInfo &TTI)

Definition Region.cpp:152

LLVM_ABI void clearAux()

Clears all auxiliary data.

Definition Region.cpp:107

LLVM_ABI ~Region()

Definition Region.cpp:49

LLVM_ABI void remove(Instruction *I)

Mark I as a deleted instruction from the region.

Definition Region.cpp:20

LLVM_DUMP_METHOD void dump() const

Definition Region.cpp:32

void add(Instruction *I)

Mark I as a newly added instruction to the region.

BasicBlock(llvm::BasicBlock *BB, Context &SBCtx)

friend class Instruction

Iterator for Instructions in a `BasicBlock.

auto find(R &&Range, const T &Val)

Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.

auto enumerate(FirstRange &&First, RestRanges &&...Rest)

Given two or more input ranges, returns a new range whose values are tuples (A, B,...

decltype(auto) dyn_cast(const From &Val)

dyn_cast - Return the argument parameter cast to the specified type.

LLVM_ABI raw_ostream & dbgs()

dbgs() - This returns a reference to a raw_ostream for debugging messages.

class LLVM_GSL_OWNER SmallVector

Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...

LLVM_ABI raw_fd_ostream & errs()

This returns a reference to a raw_ostream for standard error.

decltype(auto) cast(const From &Val)

cast - Return the argument parameter cast to the specified type.

auto seq(T Begin, T End)

Iterate over an integral type from Begin up to - but not including - End.