Skip to content

Commit 5ff51ba

Browse files
nikicAlexisPerry
authored andcommitted
[IR] Add getDataLayout() helpers to BasicBlock and Instruction (llvm#96902)
This is a helper to avoid writing `getModule()->getDataLayout()`. I regularly try to use this method only to remember it doesn't exist... `getModule()->getDataLayout()` is also a common (the most common?) reason why code has to include the Module.h header.
1 parent 02ce4ee commit 5ff51ba

File tree

100 files changed

+244
-223
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+244
-223
lines changed

llvm/include/llvm/Analysis/MemorySSA.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1269,7 +1269,7 @@ class upward_defs_iterator
12691269
if (WalkingPhi && Location.Ptr) {
12701270
PHITransAddr Translator(
12711271
const_cast<Value *>(Location.Ptr),
1272-
OriginalAccess->getBlock()->getModule()->getDataLayout(), nullptr);
1272+
OriginalAccess->getBlock()->getDataLayout(), nullptr);
12731273

12741274
if (Value *Addr =
12751275
Translator.translateValue(OriginalAccess->getBlock(),

llvm/include/llvm/IR/BasicBlock.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ namespace llvm {
3232

3333
class AssemblyAnnotationWriter;
3434
class CallInst;
35+
class DataLayout;
3536
class Function;
3637
class LandingPadInst;
3738
class LLVMContext;
@@ -218,6 +219,11 @@ class BasicBlock final : public Value, // Basic blocks are data objects also
218219
static_cast<const BasicBlock *>(this)->getModule());
219220
}
220221

222+
/// Get the data layout of the module this basic block belongs to.
223+
///
224+
/// Requires the basic block to have a parent module.
225+
const DataLayout &getDataLayout() const;
226+
221227
/// Returns the terminator instruction if the block is well formed or null
222228
/// if the block is not well formed.
223229
const Instruction *getTerminator() const LLVM_READONLY {

llvm/include/llvm/IR/IRBuilder.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,7 +1049,7 @@ class IRBuilderBase {
10491049

10501050
/// Create a call to llvm.stacksave
10511051
CallInst *CreateStackSave(const Twine &Name = "") {
1052-
const DataLayout &DL = BB->getModule()->getDataLayout();
1052+
const DataLayout &DL = BB->getDataLayout();
10531053
return CreateIntrinsic(Intrinsic::stacksave, {DL.getAllocaPtrType(Context)},
10541054
{}, nullptr, Name);
10551055
}
@@ -1770,14 +1770,14 @@ class IRBuilderBase {
17701770

17711771
AllocaInst *CreateAlloca(Type *Ty, unsigned AddrSpace,
17721772
Value *ArraySize = nullptr, const Twine &Name = "") {
1773-
const DataLayout &DL = BB->getModule()->getDataLayout();
1773+
const DataLayout &DL = BB->getDataLayout();
17741774
Align AllocaAlign = DL.getPrefTypeAlign(Ty);
17751775
return Insert(new AllocaInst(Ty, AddrSpace, ArraySize, AllocaAlign), Name);
17761776
}
17771777

17781778
AllocaInst *CreateAlloca(Type *Ty, Value *ArraySize = nullptr,
17791779
const Twine &Name = "") {
1780-
const DataLayout &DL = BB->getModule()->getDataLayout();
1780+
const DataLayout &DL = BB->getDataLayout();
17811781
Align AllocaAlign = DL.getPrefTypeAlign(Ty);
17821782
unsigned AddrSpace = DL.getAllocaAddrSpace();
17831783
return Insert(new AllocaInst(Ty, AddrSpace, ArraySize, AllocaAlign), Name);
@@ -1815,7 +1815,7 @@ class IRBuilderBase {
18151815
LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, MaybeAlign Align,
18161816
bool isVolatile, const Twine &Name = "") {
18171817
if (!Align) {
1818-
const DataLayout &DL = BB->getModule()->getDataLayout();
1818+
const DataLayout &DL = BB->getDataLayout();
18191819
Align = DL.getABITypeAlign(Ty);
18201820
}
18211821
return Insert(new LoadInst(Ty, Ptr, Twine(), isVolatile, *Align), Name);
@@ -1824,7 +1824,7 @@ class IRBuilderBase {
18241824
StoreInst *CreateAlignedStore(Value *Val, Value *Ptr, MaybeAlign Align,
18251825
bool isVolatile = false) {
18261826
if (!Align) {
1827-
const DataLayout &DL = BB->getModule()->getDataLayout();
1827+
const DataLayout &DL = BB->getDataLayout();
18281828
Align = DL.getABITypeAlign(Val->getType());
18291829
}
18301830
return Insert(new StoreInst(Val, Ptr, isVolatile, *Align));
@@ -1841,7 +1841,7 @@ class IRBuilderBase {
18411841
AtomicOrdering FailureOrdering,
18421842
SyncScope::ID SSID = SyncScope::System) {
18431843
if (!Align) {
1844-
const DataLayout &DL = BB->getModule()->getDataLayout();
1844+
const DataLayout &DL = BB->getDataLayout();
18451845
Align = llvm::Align(DL.getTypeStoreSize(New->getType()));
18461846
}
18471847

@@ -1854,7 +1854,7 @@ class IRBuilderBase {
18541854
AtomicOrdering Ordering,
18551855
SyncScope::ID SSID = SyncScope::System) {
18561856
if (!Align) {
1857-
const DataLayout &DL = BB->getModule()->getDataLayout();
1857+
const DataLayout &DL = BB->getDataLayout();
18581858
Align = llvm::Align(DL.getTypeStoreSize(Val->getType()));
18591859
}
18601860

llvm/include/llvm/IR/Instruction.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
namespace llvm {
3030

3131
class BasicBlock;
32+
class DataLayout;
3233
class DbgMarker;
3334
class FastMathFlags;
3435
class MDNode;
@@ -189,6 +190,11 @@ class Instruction : public User,
189190
static_cast<const Instruction *>(this)->getFunction());
190191
}
191192

193+
/// Get the data layout of the module this instruction belongs to.
194+
///
195+
/// Requires the instruction to have a parent module.
196+
const DataLayout &getDataLayout() const;
197+
192198
/// This method unlinks 'this' from the containing basic block, but does not
193199
/// delete it.
194200
void removeFromParent();

llvm/include/llvm/Transforms/Instrumentation/AddressSanitizerCommon.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class InterestingMemoryOperand {
4343
Value *MaybeStride = nullptr)
4444
: IsWrite(IsWrite), OpType(OpType), Alignment(Alignment),
4545
MaybeMask(MaybeMask), MaybeEVL(MaybeEVL), MaybeStride(MaybeStride) {
46-
const DataLayout &DL = I->getModule()->getDataLayout();
46+
const DataLayout &DL = I->getDataLayout();
4747
TypeStoreSize = DL.getTypeStoreSizeInBits(OpType);
4848
PtrUse = &I->getOperandUse(OperandNo);
4949
}

llvm/lib/Analysis/BranchProbabilityInfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ computeUnlikelySuccessors(const BasicBlock *BB, Loop *L,
620620
if (!CmpLHSConst || !llvm::is_contained(successors(BB), B))
621621
continue;
622622
// First collapse InstChain
623-
const DataLayout &DL = BB->getModule()->getDataLayout();
623+
const DataLayout &DL = BB->getDataLayout();
624624
for (Instruction *I : llvm::reverse(InstChain)) {
625625
CmpLHSConst = ConstantFoldBinaryOpOperands(
626626
I->getOpcode(), CmpLHSConst, cast<Constant>(I->getOperand(1)), DL);

llvm/lib/Analysis/CaptureTracking.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ UseCaptureKind llvm::DetermineUseCaptureKind(
388388
// Comparing a dereferenceable_or_null pointer against null cannot
389389
// lead to pointer escapes, because if it is not null it must be a
390390
// valid (in-bounds) pointer.
391-
const DataLayout &DL = I->getModule()->getDataLayout();
391+
const DataLayout &DL = I->getDataLayout();
392392
if (IsDereferenceableOrNull && IsDereferenceableOrNull(O, DL))
393393
return UseCaptureKind::NO_CAPTURE;
394394
}

llvm/lib/Analysis/DemandedBits.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ void DemandedBits::determineLiveOperandBits(
6969
return;
7070
KnownBitsComputed = true;
7171

72-
const DataLayout &DL = UserI->getModule()->getDataLayout();
72+
const DataLayout &DL = UserI->getDataLayout();
7373
Known = KnownBits(BitWidth);
7474
computeKnownBits(V1, Known, DL, 0, &AC, UserI, &DT);
7575

@@ -404,14 +404,14 @@ APInt DemandedBits::getDemandedBits(Instruction *I) {
404404
if (Found != AliveBits.end())
405405
return Found->second;
406406

407-
const DataLayout &DL = I->getModule()->getDataLayout();
407+
const DataLayout &DL = I->getDataLayout();
408408
return APInt::getAllOnes(DL.getTypeSizeInBits(I->getType()->getScalarType()));
409409
}
410410

411411
APInt DemandedBits::getDemandedBits(Use *U) {
412412
Type *T = (*U)->getType();
413413
auto *UserI = cast<Instruction>(U->getUser());
414-
const DataLayout &DL = UserI->getModule()->getDataLayout();
414+
const DataLayout &DL = UserI->getDataLayout();
415415
unsigned BitWidth = DL.getTypeSizeInBits(T->getScalarType());
416416

417417
// We only track integer uses, everything else produces a mask with all bits

llvm/lib/Analysis/IVDescriptors.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ static std::pair<Type *, bool> computeRecurrenceType(Instruction *Exit,
9595
AssumptionCache *AC,
9696
DominatorTree *DT) {
9797
bool IsSigned = false;
98-
const DataLayout &DL = Exit->getModule()->getDataLayout();
98+
const DataLayout &DL = Exit->getDataLayout();
9999
uint64_t MaxBitWidth = DL.getTypeSizeInBits(Exit->getType());
100100

101101
if (DB) {

llvm/lib/Analysis/IVUsers.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ static bool IVUseShouldUsePostIncValue(Instruction *User, Value *Operand,
134134
/// add its users to the IVUsesByStride set and return true. Otherwise, return
135135
/// false.
136136
bool IVUsers::AddUsersIfInteresting(Instruction *I) {
137-
const DataLayout &DL = I->getModule()->getDataLayout();
137+
const DataLayout &DL = I->getDataLayout();
138138

139139
// Add this IV user to the Processed set before returning false to ensure that
140140
// all IV users are members of the set. See IVUsers::isIVUserOrOperand.

0 commit comments

Comments
 (0)