LLVM: lib/Transforms/Scalar/AlignmentFromAssumptions.cpp Source File (original) (raw)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

33

34#define DEBUG_TYPE "alignment-from-assumptions"

35using namespace llvm;

36

38 "Number of loads changed by alignment assumptions");

40 "Number of stores changed by alignment assumptions");

42 "Number of memory intrinsics changed by alignment assumptions");

43

44

45

46

47

48

49

51 const SCEV *AlignSCEV,

53

54 const SCEV *DiffUnitsSCEV = SE->getURemExpr(DiffSCEV, AlignSCEV);

55

56 LLVM_DEBUG(dbgs() << "\talignment relative to " << *AlignSCEV << " is "

57 << *DiffUnitsSCEV << " (diff: " << *DiffSCEV << ")\n");

58

61 int64_t DiffUnits = ConstDUSCEV->getValue()->getSExtValue();

62

63

64

65

66 if (!DiffUnits)

68

69

70

71 uint64_t DiffUnitsAbs = std::abs(DiffUnits);

73 return Align(DiffUnitsAbs);

74 }

75

76 return std::nullopt;

77}

78

79

80

81

86

90

91

92

94

95

96

97 DiffSCEV = SE->getAddExpr(DiffSCEV, OffSCEV);

98

99 LLVM_DEBUG(dbgs() << "AFI: alignment of " << *Ptr << " relative to "

100 << *AlignSCEV << " and offset " << *OffSCEV

101 << " using diff " << *DiffSCEV << "\n");

102

105 return *NewAlignment;

106 }

107

109

110

111

112

113

114

115

116 const SCEV *DiffStartSCEV = DiffARSCEV->getStart();

117 const SCEV *DiffIncSCEV = DiffARSCEV->getStepRecurrence(*SE);

118

119 LLVM_DEBUG(dbgs() << "\ttrying start/inc alignment using start "

120 << *DiffStartSCEV << " and inc " << *DiffIncSCEV << "\n");

121

122

123

124

125

129

131 << "\n");

133 << "\n");

134

135 if (!NewAlignment || !NewIncAlignment)

137

138 const Align NewAlign = *NewAlignment;

139 const Align NewIncAlign = *NewIncAlignment;

140 if (NewAlign > NewIncAlign) {

142 << DebugStr(NewIncAlign) << "\n");

143 return NewIncAlign;

144 }

145 if (NewIncAlign > NewAlign) {

147 << "\n");

148 return NewAlign;

149 }

150 assert(NewIncAlign == NewAlign);

152 << "\n");

153 return NewAlign;

154 }

155

157}

158

160 unsigned Idx,

162 const SCEV *&AlignSCEV,

163 const SCEV *&OffSCEV) {

167 return false;

169 AAPtr = AlignOB.Inputs[0].get();

170

172 AlignSCEV = SE->getSCEV(AlignOB.Inputs[1].get());

173 AlignSCEV = SE->getTruncateOrZeroExtend(AlignSCEV, Int64Ty);

175

176

177 return false;

179

180 return false;

181 if (AlignOB.Inputs.size() == 3)

182 OffSCEV = SE->getSCEV(AlignOB.Inputs[2].get());

183 else

184 OffSCEV = SE->getZero(Int64Ty);

185 OffSCEV = SE->getTruncateOrZeroExtend(OffSCEV, Int64Ty);

186 return true;

187}

188

190 unsigned Idx) {

192 const SCEV *AlignSCEV, *OffSCEV;

194 return false;

195

196

197

199 return false;

200

201 const SCEV *AASCEV = SE->getSCEV(AAPtr);

202

203

207 if (J == ACall)

208 continue;

209

211 if (K->getFunction() == ACall->getFunction())

213 }

214

215 while (!WorkList.empty()) {

219 continue;

221 LI->getPointerOperand(), SE);

222 if (NewAlignment > LI->getAlign()) {

223 LI->setAlignment(NewAlignment);

224 ++NumLoadAlignChanged;

225 }

228 continue;

230 SI->getPointerOperand(), SE);

231 if (NewAlignment > SI->getAlign()) {

232 SI->setAlignment(NewAlignment);

233 ++NumStoreAlignChanged;

234 }

237 continue;

238 Align NewDestAlignment =

240

242 << "\n";);

243 if (NewDestAlignment > *MI->getDestAlign()) {

244 MI->setDestAlignment(NewDestAlignment);

245 ++NumMemIntAlignChanged;

246 }

247

248

249

251 Align NewSrcAlignment =

252 getNewAlignment(AASCEV, AlignSCEV, OffSCEV, MTI->getSource(), SE);

253

255 << "\n";);

256

257 if (NewSrcAlignment > *MTI->getSourceAlign()) {

258 MTI->setSourceAlignment(NewSrcAlignment);

259 ++NumMemIntAlignChanged;

260 }

261 }

262 }

263

264

265

268 for (auto &U : J->uses()) {

269 if (U->getType()->isPointerTy()) {

272 if (SI && SI->getPointerOperandIndex() != U.getOperandNo())

273 continue;

274 if (!Visited.count(K))

276 }

277 }

278 }

279

280 return true;

281}

282

286 SE = SE_;

287 DT = DT_;

288

291 if (AssumeVH) {

293 for (unsigned Idx = 0; Idx < Call->getNumOperandBundles(); Idx++)

295 }

296

298}

299

302

308

312 return PA;

313}

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

static MaybeAlign getNewAlignmentDiff(const SCEV *DiffSCEV, const SCEV *AlignSCEV, ScalarEvolution *SE)

Definition AlignmentFromAssumptions.cpp:50

static Align getNewAlignment(const SCEV *AASCEV, const SCEV *AlignSCEV, const SCEV *OffSCEV, Value *Ptr, ScalarEvolution *SE)

Definition AlignmentFromAssumptions.cpp:82

This is the interface for a simple mod/ref and alias analysis over globals.

This file defines the SmallPtrSet class.

This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...

#define STATISTIC(VARNAME, DESC)

PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)

Get the result of an analysis pass for a given IR unit.

A function analysis which provides an AssumptionCache.

A cache of @llvm.assume calls within a function.

MutableArrayRef< WeakVH > assumptions()

Access the list of assumption handles currently tracked for this function.

Represents analyses that only rely on functions' control flow.

This class represents a function call, abstracting a target machine's calling convention.

Analysis pass which computes a DominatorTree.

Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.

LLVM_ABI const Function * getFunction() const

Return the function this instruction belongs to.

An instruction for reading from memory.

This is the common base class for memset/memcpy/memmove.

This class wraps the llvm.memcpy/memmove intrinsics.

A set of analyses that are preserved following a run of a transformation pass.

static PreservedAnalyses all()

Construct a special preserved set that preserves all passes.

PreservedAnalyses & preserveSet()

Mark an analysis set as preserved.

PreservedAnalyses & preserve()

Mark an analysis as preserved.

This node represents a polynomial recurrence on the trip count of the specified loop.

This class represents a constant integer value.

This class represents an analyzed expression in the program.

LLVM_ABI Type * getType() const

Return the LLVM type of this SCEV expression.

Analysis pass that exposes the ScalarEvolution for a function.

The main scalar evolution driver.

LLVM_ABI const SCEV * getURemExpr(const SCEV *LHS, const SCEV *RHS)

Represents an unsigned remainder expression based on unsigned division.

LLVM_ABI const SCEV * getSCEV(Value *V)

Return a SCEV expression for the full generality of the specified expression.

LLVM_ABI const SCEV * getNoopOrSignExtend(const SCEV *V, Type *Ty)

Return a SCEV corresponding to a conversion of the input value to the specified type.

LLVM_ABI const SCEV * getMinusSCEV(const SCEV *LHS, const SCEV *RHS, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)

Return LHS-RHS.

LLVM_ABI const SCEV * getAddExpr(SmallVectorImpl< const SCEV * > &Ops, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)

Get a canonical add expression, or something simpler if possible.

size_type count(ConstPtrType Ptr) const

count - Return 1 if the specified pointer is in the set, 0 otherwise.

std::pair< iterator, bool > insert(PtrType Ptr)

Inserts Ptr if and only if there is no element in the container equal to Ptr.

SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.

void push_back(const T &Elt)

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

An instruction for storing to memory.

The instances of the Type class are immutable: once they are created, they are never changed.

static LLVM_ABI IntegerType * getInt64Ty(LLVMContext &C)

LLVM Value Representation.

iterator_range< user_iterator > users()

LLVM_ABI const Value * stripPointerCastsSameRepresentation() const

Strip off pointer casts, all-zero GEPs and address space casts but ensures the representation of the ...

iterator_range< use_iterator > uses()

This is an optimization pass for GlobalISel generic memory operations.

LLVM_ABI bool isValidAssumeForContext(const Instruction *I, const Instruction *CxtI, const DominatorTree *DT=nullptr, bool AllowEphemerals=false)

Return true if it is valid to use the assumptions provided by an assume intrinsic,...

decltype(auto) dyn_cast(const From &Val)

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

constexpr bool isPowerOf2_64(uint64_t Value)

Return true if the argument is a power of two > 0 (64 bit edition.)

LLVM_ABI raw_ostream & dbgs()

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

bool isa(const From &Val)

isa - Return true if the parameter to the template is an instance of one of the template type argu...

decltype(auto) cast(const From &Val)

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

AnalysisManager< Function > FunctionAnalysisManager

Convenience typedef for the Function analysis manager.

This struct is a compact representation of a valid (non-zero power of two) alignment.

bool extractAlignmentInfo(CallInst *I, unsigned Idx, Value *&AAPtr, const SCEV *&AlignSCEV, const SCEV *&OffSCEV)

Definition AlignmentFromAssumptions.cpp:159

bool processAssumption(CallInst *I, unsigned Idx)

Definition AlignmentFromAssumptions.cpp:189

bool runImpl(Function &F, AssumptionCache &AC, ScalarEvolution *SE_, DominatorTree *DT_)

Definition AlignmentFromAssumptions.cpp:283

PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)

Definition AlignmentFromAssumptions.cpp:301

This struct is a compact representation of a valid (power of two) or undefined (0) alignment.

A lightweight accessor for an operand bundle meant to be passed around by value.

uint32_t getTagID() const

Return the tag of this operand bundle as an integer.