Skip to content

Commit 1c18a9c

Browse files
committed
[LLVM][Alignment] Introduce Alignment In MachineFrameInfo
Summary: This is patch is part of a serie to introduce an Alignment type. See this thread for context: http://lists.llvm.org/pipermail/llvm-dev/2019-July/133851.html See this patch for the introduction of the type: https://reviews.llvm.org/D64790 Reviewers: jfb Subscribers: hiraditya, dexonsmith, llvm-commits, courbet Tags: #llvm Differential Revision: https://reviews.llvm.org/D65800 llvm-svn: 369531
1 parent 717717b commit 1c18a9c

File tree

7 files changed

+62
-38
lines changed

7 files changed

+62
-38
lines changed

llvm/include/llvm/CodeGen/MachineFrameInfo.h

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#define LLVM_CODEGEN_MACHINEFRAMEINFO_H
1515

1616
#include "llvm/ADT/SmallVector.h"
17+
#include "llvm/Support/Alignment.h"
1718
#include "llvm/Support/DataTypes.h"
1819
#include <cassert>
1920
#include <vector>
@@ -129,7 +130,7 @@ class MachineFrameInfo {
129130
uint64_t Size;
130131

131132
// The required alignment of this stack slot.
132-
unsigned Alignment;
133+
Align Alignment;
133134

134135
// If true, the value of the stack object is set before
135136
// entering the function and is not modified inside the function. By
@@ -180,17 +181,16 @@ class MachineFrameInfo {
180181

181182
uint8_t SSPLayout;
182183

183-
StackObject(uint64_t Size, unsigned Alignment, int64_t SPOffset,
184+
StackObject(uint64_t Size, llvm::Align Alignment, int64_t SPOffset,
184185
bool IsImmutable, bool IsSpillSlot, const AllocaInst *Alloca,
185186
bool IsAliased, uint8_t StackID = 0)
186-
: SPOffset(SPOffset), Size(Size), Alignment(Alignment),
187-
isImmutable(IsImmutable), isSpillSlot(IsSpillSlot),
188-
StackID(StackID), Alloca(Alloca), isAliased(IsAliased),
189-
SSPLayout(SSPLK_None) {}
187+
: SPOffset(SPOffset), Size(Size), Alignment(Alignment),
188+
isImmutable(IsImmutable), isSpillSlot(IsSpillSlot), StackID(StackID),
189+
Alloca(Alloca), isAliased(IsAliased), SSPLayout(SSPLK_None) {}
190190
};
191191

192192
/// The alignment of the stack.
193-
unsigned StackAlignment;
193+
Align StackAlignment;
194194

195195
/// Can the stack be realigned. This can be false if the target does not
196196
/// support stack realignment, or if the user asks us not to realign the
@@ -260,7 +260,7 @@ class MachineFrameInfo {
260260
/// native alignment maintained by the compiler, dynamic alignment code will
261261
/// be needed.
262262
///
263-
unsigned MaxAlignment = 0;
263+
Align MaxAlignment;
264264

265265
/// Set to true if this function adjusts the stack -- e.g.,
266266
/// when calling another function. This is only valid during and after
@@ -304,7 +304,7 @@ class MachineFrameInfo {
304304

305305
/// Required alignment of the local object blob, which is the strictest
306306
/// alignment of any object in it.
307-
unsigned LocalFrameMaxAlign = 0;
307+
Align LocalFrameMaxAlign;
308308

309309
/// Whether the local object blob needs to be allocated together. If not,
310310
/// PEI should ignore the isPreAllocated flags on the stack objects and
@@ -338,8 +338,8 @@ class MachineFrameInfo {
338338
public:
339339
explicit MachineFrameInfo(unsigned StackAlignment, bool StackRealignable,
340340
bool ForcedRealign)
341-
: StackAlignment(StackAlignment), StackRealignable(StackRealignable),
342-
ForcedRealign(ForcedRealign) {}
341+
: StackAlignment(assumeAligned(StackAlignment)),
342+
StackRealignable(StackRealignable), ForcedRealign(ForcedRealign) {}
343343

344344
/// Return true if there are any stack objects in this function.
345345
bool hasStackObjects() const { return !Objects.empty(); }
@@ -419,10 +419,10 @@ class MachineFrameInfo {
419419

420420
/// Required alignment of the local object blob,
421421
/// which is the strictest alignment of any object in it.
422-
void setLocalFrameMaxAlign(unsigned Align) { LocalFrameMaxAlign = Align; }
422+
void setLocalFrameMaxAlign(Align Align) { LocalFrameMaxAlign = Align; }
423423

424424
/// Return the required alignment of the local object blob.
425-
unsigned getLocalFrameMaxAlign() const { return LocalFrameMaxAlign; }
425+
Align getLocalFrameMaxAlign() const { return LocalFrameMaxAlign; }
426426

427427
/// Get whether the local allocation blob should be allocated together or
428428
/// let PEI allocate the locals in it directly.
@@ -462,14 +462,14 @@ class MachineFrameInfo {
462462
unsigned getObjectAlignment(int ObjectIdx) const {
463463
assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
464464
"Invalid Object Idx!");
465-
return Objects[ObjectIdx+NumFixedObjects].Alignment;
465+
return Objects[ObjectIdx + NumFixedObjects].Alignment.value();
466466
}
467467

468468
/// setObjectAlignment - Change the alignment of the specified stack object.
469469
void setObjectAlignment(int ObjectIdx, unsigned Align) {
470470
assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
471471
"Invalid Object Idx!");
472-
Objects[ObjectIdx+NumFixedObjects].Alignment = Align;
472+
Objects[ObjectIdx + NumFixedObjects].Alignment = assumeAligned(Align);
473473

474474
// Only ensure max alignment for the default stack.
475475
if (getStackID(ObjectIdx) == 0)
@@ -561,10 +561,14 @@ class MachineFrameInfo {
561561

562562
/// Return the alignment in bytes that this function must be aligned to,
563563
/// which is greater than the default stack alignment provided by the target.
564-
unsigned getMaxAlignment() const { return MaxAlignment; }
564+
unsigned getMaxAlignment() const { return MaxAlignment.value(); }
565565

566566
/// Make sure the function is at least Align bytes aligned.
567-
void ensureMaxAlignment(unsigned Align);
567+
void ensureMaxAlignment(llvm::Align Align);
568+
/// FIXME: Remove this once transition to Align is over.
569+
inline void ensureMaxAlignment(unsigned Align) {
570+
ensureMaxAlignment(assumeAligned(Align));
571+
}
568572

569573
/// Return true if this function adjusts the stack -- e.g.,
570574
/// when calling another function. This is only valid during and after
@@ -728,12 +732,24 @@ class MachineFrameInfo {
728732

729733
/// Create a new statically sized stack object, returning
730734
/// a nonnegative identifier to represent it.
731-
int CreateStackObject(uint64_t Size, unsigned Alignment, bool isSpillSlot,
735+
int CreateStackObject(uint64_t Size, llvm::Align Alignment, bool isSpillSlot,
732736
const AllocaInst *Alloca = nullptr, uint8_t ID = 0);
737+
/// FIXME: Remove this function when transition to llvm::Align is over.
738+
inline int CreateStackObject(uint64_t Size, unsigned Alignment,
739+
bool isSpillSlot,
740+
const AllocaInst *Alloca = nullptr,
741+
uint8_t ID = 0) {
742+
return CreateStackObject(Size, assumeAligned(Alignment), isSpillSlot,
743+
Alloca, ID);
744+
}
733745

734746
/// Create a new statically sized stack object that represents a spill slot,
735747
/// returning a nonnegative identifier to represent it.
736-
int CreateSpillStackObject(uint64_t Size, unsigned Alignment);
748+
int CreateSpillStackObject(uint64_t Size, llvm::Align Alignment);
749+
/// FIXME: Remove this function when transition to llvm::Align is over.
750+
inline int CreateSpillStackObject(uint64_t Size, unsigned Alignment) {
751+
return CreateSpillStackObject(Size, assumeAligned(Alignment));
752+
}
737753

738754
/// Remove or mark dead a statically sized stack object.
739755
void RemoveStackObject(int ObjectIdx) {
@@ -744,7 +760,12 @@ class MachineFrameInfo {
744760
/// Notify the MachineFrameInfo object that a variable sized object has been
745761
/// created. This must be created whenever a variable sized object is
746762
/// created, whether or not the index returned is actually used.
747-
int CreateVariableSizedObject(unsigned Alignment, const AllocaInst *Alloca);
763+
int CreateVariableSizedObject(llvm::Align Alignment,
764+
const AllocaInst *Alloca);
765+
/// FIXME: Remove this function when transition to llvm::Align is over.
766+
int CreateVariableSizedObject(unsigned Alignment, const AllocaInst *Alloca) {
767+
return CreateVariableSizedObject(assumeAligned(Alignment), Alloca);
768+
}
748769

749770
/// Returns a reference to call saved info vector for the current function.
750771
const std::vector<CalleeSavedInfo> &getCalleeSavedInfo() const {

llvm/lib/Analysis/MemoryBuiltins.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ STATISTIC(ObjectVisitorLoad,
523523

524524
APInt ObjectSizeOffsetVisitor::align(APInt Size, uint64_t Align) {
525525
if (Options.RoundToAlign && Align)
526-
return APInt(IntTyBits, alignTo(Size.getZExtValue(), Align));
526+
return APInt(IntTyBits, alignTo(Size.getZExtValue(), llvm::Align(Align)));
527527
return Size;
528528
}
529529

llvm/lib/CodeGen/LocalStackSlotAllocation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ void LocalStackSlotPass::calculateFrameObjectOffsets(MachineFunction &Fn) {
261261

262262
// Remember how big this blob of stack space is
263263
MFI.setLocalFrameSize(Offset);
264-
MFI.setLocalFrameMaxAlign(MaxAlign);
264+
MFI.setLocalFrameMaxAlign(assumeAligned(MaxAlign));
265265
}
266266

267267
static inline bool

llvm/lib/CodeGen/MachineFrameInfo.cpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,26 @@
2828

2929
using namespace llvm;
3030

31-
void MachineFrameInfo::ensureMaxAlignment(unsigned Align) {
31+
void MachineFrameInfo::ensureMaxAlignment(llvm::Align Align) {
3232
if (!StackRealignable)
3333
assert(Align <= StackAlignment &&
3434
"For targets without stack realignment, Align is out of limit!");
3535
if (MaxAlignment < Align) MaxAlignment = Align;
3636
}
3737

3838
/// Clamp the alignment if requested and emit a warning.
39-
static inline unsigned clampStackAlignment(bool ShouldClamp, unsigned Align,
40-
unsigned StackAlign) {
39+
static inline llvm::Align clampStackAlignment(bool ShouldClamp,
40+
llvm::Align Align,
41+
llvm::Align StackAlign) {
4142
if (!ShouldClamp || Align <= StackAlign)
4243
return Align;
43-
LLVM_DEBUG(dbgs() << "Warning: requested alignment " << Align
44-
<< " exceeds the stack alignment " << StackAlign
44+
LLVM_DEBUG(dbgs() << "Warning: requested alignment " << Align.value()
45+
<< " exceeds the stack alignment " << StackAlign.value()
4546
<< " when stack realignment is off" << '\n');
4647
return StackAlign;
4748
}
4849

49-
int MachineFrameInfo::CreateStackObject(uint64_t Size, unsigned Alignment,
50+
int MachineFrameInfo::CreateStackObject(uint64_t Size, llvm::Align Alignment,
5051
bool IsSpillSlot,
5152
const AllocaInst *Alloca,
5253
uint8_t StackID) {
@@ -62,15 +63,15 @@ int MachineFrameInfo::CreateStackObject(uint64_t Size, unsigned Alignment,
6263
}
6364

6465
int MachineFrameInfo::CreateSpillStackObject(uint64_t Size,
65-
unsigned Alignment) {
66+
llvm::Align Alignment) {
6667
Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment);
6768
CreateStackObject(Size, Alignment, true);
6869
int Index = (int)Objects.size() - NumFixedObjects - 1;
6970
ensureMaxAlignment(Alignment);
7071
return Index;
7172
}
7273

73-
int MachineFrameInfo::CreateVariableSizedObject(unsigned Alignment,
74+
int MachineFrameInfo::CreateVariableSizedObject(llvm::Align Alignment,
7475
const AllocaInst *Alloca) {
7576
HasVarSizedObjects = true;
7677
Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment);
@@ -88,7 +89,8 @@ int MachineFrameInfo::CreateFixedObject(uint64_t Size, int64_t SPOffset,
8889
// object is 16-byte aligned. Note that unlike the non-fixed case, if the
8990
// stack needs realignment, we can't assume that the stack will in fact be
9091
// aligned.
91-
unsigned Alignment = MinAlign(SPOffset, ForcedRealign ? 1 : StackAlignment);
92+
llvm::Align Alignment =
93+
commonAlignment(ForcedRealign ? llvm::Align() : StackAlignment, SPOffset);
9294
Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment);
9395
Objects.insert(Objects.begin(),
9496
StackObject(Size, Alignment, SPOffset, IsImmutable,
@@ -100,7 +102,8 @@ int MachineFrameInfo::CreateFixedObject(uint64_t Size, int64_t SPOffset,
100102
int MachineFrameInfo::CreateFixedSpillStackObject(uint64_t Size,
101103
int64_t SPOffset,
102104
bool IsImmutable) {
103-
unsigned Alignment = MinAlign(SPOffset, ForcedRealign ? 1 : StackAlignment);
105+
llvm::Align Alignment =
106+
commonAlignment(ForcedRealign ? llvm::Align() : StackAlignment, SPOffset);
104107
Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment);
105108
Objects.insert(Objects.begin(),
106109
StackObject(Size, Alignment, SPOffset, IsImmutable,
@@ -232,7 +235,7 @@ void MachineFrameInfo::print(const MachineFunction &MF, raw_ostream &OS) const{
232235
OS << "variable sized";
233236
else
234237
OS << "size=" << SO.Size;
235-
OS << ", align=" << SO.Alignment;
238+
OS << ", align=" << SO.Alignment.value();
236239

237240
if (i < NumFixedObjects)
238241
OS << ", fixed";

llvm/lib/CodeGen/PrologEpilogInserter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,7 @@ void PEI::calculateFrameObjectOffsets(MachineFunction &MF) {
898898
// frame index registers. Functions which don't want/need this optimization
899899
// will continue to use the existing code path.
900900
if (MFI.getUseLocalStackAllocationBlock()) {
901-
unsigned Align = MFI.getLocalFrameMaxAlign();
901+
unsigned Align = MFI.getLocalFrameMaxAlign().value();
902902

903903
// Adjust to alignment boundary.
904904
Offset = alignTo(Offset, Align, Skew);

llvm/lib/Target/Hexagon/HexagonFrameLowering.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,10 +1377,10 @@ void HexagonFrameLowering::processFunctionBeforeFrameFinalized(
13771377
}
13781378

13791379
MFI.setLocalFrameSize(LFS);
1380-
unsigned A = MFI.getLocalFrameMaxAlign();
1380+
Align A = MFI.getLocalFrameMaxAlign();
13811381
assert(A <= 8 && "Unexpected local frame alignment");
1382-
if (A == 0)
1383-
MFI.setLocalFrameMaxAlign(8);
1382+
if (A == 1)
1383+
MFI.setLocalFrameMaxAlign(llvm::Align(8));
13841384
MFI.setUseLocalStackAllocationBlock(true);
13851385

13861386
// Set the physical aligned-stack base address register.

llvm/lib/Target/NVPTX/NVPTXPrologEpilogPass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ NVPTXPrologEpilogPass::calculateFrameObjectOffsets(MachineFunction &Fn) {
178178
// frame index registers. Functions which don't want/need this optimization
179179
// will continue to use the existing code path.
180180
if (MFI.getUseLocalStackAllocationBlock()) {
181-
unsigned Align = MFI.getLocalFrameMaxAlign();
181+
unsigned Align = MFI.getLocalFrameMaxAlign().value();
182182

183183
// Adjust to alignment boundary.
184184
Offset = (Offset + Align - 1) / Align * Align;

0 commit comments

Comments
 (0)