Skip to content

Commit e8a0a09

Browse files
committed
[Alignment][NFC] Convert AllocaInst to MaybeAlign
Summary: This is patch is part of a series 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: courbet Reviewed By: courbet Subscribers: hiraditya, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D69301
1 parent 03de2f8 commit e8a0a09

File tree

8 files changed

+39
-39
lines changed

8 files changed

+39
-39
lines changed

llvm/include/llvm/IR/Instructions.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ class AllocaInst : public UnaryInstruction {
7878
AllocaInst(Type *Ty, unsigned AddrSpace,
7979
const Twine &Name, BasicBlock *InsertAtEnd);
8080

81-
AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, unsigned Align,
81+
AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, MaybeAlign Align,
8282
const Twine &Name = "", Instruction *InsertBefore = nullptr);
83-
AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, unsigned Align,
83+
AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, MaybeAlign Align,
8484
const Twine &Name, BasicBlock *InsertAtEnd);
8585

8686
/// Return true if there is an allocation size parameter to the allocation

llvm/lib/AsmParser/LLParser.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6917,8 +6917,7 @@ int LLParser::ParseAlloc(Instruction *&Inst, PerFunctionState &PFS) {
69176917
if (Size && !Size->getType()->isIntegerTy())
69186918
return Error(SizeLoc, "element count must have integer type");
69196919

6920-
AllocaInst *AI =
6921-
new AllocaInst(Ty, AddrSpace, Size, Alignment ? Alignment->value() : 0);
6920+
AllocaInst *AI = new AllocaInst(Ty, AddrSpace, Size, Alignment);
69226921
AI->setUsedWithInAlloca(IsInAlloca);
69236922
AI->setSwiftError(IsSwiftError);
69246923
Inst = AI;

llvm/lib/Bitcode/Reader/BitcodeReader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4761,7 +4761,7 @@ Error BitcodeReader::parseFunctionBody(Function *F) {
47614761
const DataLayout &DL = TheModule->getDataLayout();
47624762
unsigned AS = DL.getAllocaAddrSpace();
47634763

4764-
AllocaInst *AI = new AllocaInst(Ty, AS, Size, Align ? Align->value() : 0);
4764+
AllocaInst *AI = new AllocaInst(Ty, AS, Size, Align);
47654765
AI->setUsedWithInAlloca(InAlloca);
47664766
AI->setSwiftError(SwiftError);
47674767
I = AI;

llvm/lib/CodeGen/SjLjEHPrepare.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,9 @@ Value *SjLjEHPrepare::setupFunctionContext(Function &F,
175175
// that needs to be restored on all exits from the function. This is an alloca
176176
// because the value needs to be added to the global context list.
177177
auto &DL = F.getParent()->getDataLayout();
178-
unsigned Align = DL.getPrefTypeAlignment(FunctionContextTy);
179-
FuncCtx = new AllocaInst(FunctionContextTy, DL.getAllocaAddrSpace(),
180-
nullptr, Align, "fn_context", &EntryBB->front());
178+
const Align Alignment(DL.getPrefTypeAlignment(FunctionContextTy));
179+
FuncCtx = new AllocaInst(FunctionContextTy, DL.getAllocaAddrSpace(), nullptr,
180+
Alignment, "fn_context", &EntryBB->front());
181181

182182
// Fill in the function context structure.
183183
for (LandingPadInst *LPI : LPads) {

llvm/lib/IR/Instructions.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,30 +1217,31 @@ AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name,
12171217

12181218
AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
12191219
const Twine &Name, Instruction *InsertBefore)
1220-
: AllocaInst(Ty, AddrSpace, ArraySize, /*Align=*/0, Name, InsertBefore) {}
1220+
: AllocaInst(Ty, AddrSpace, ArraySize, /*Align=*/None, Name, InsertBefore) {
1221+
}
12211222

12221223
AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
12231224
const Twine &Name, BasicBlock *InsertAtEnd)
1224-
: AllocaInst(Ty, AddrSpace, ArraySize, /*Align=*/0, Name, InsertAtEnd) {}
1225+
: AllocaInst(Ty, AddrSpace, ArraySize, /*Align=*/None, Name, InsertAtEnd) {}
12251226

12261227
AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
1227-
unsigned Align, const Twine &Name,
1228+
MaybeAlign Align, const Twine &Name,
12281229
Instruction *InsertBefore)
1229-
: UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca,
1230-
getAISize(Ty->getContext(), ArraySize), InsertBefore),
1231-
AllocatedType(Ty) {
1230+
: UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca,
1231+
getAISize(Ty->getContext(), ArraySize), InsertBefore),
1232+
AllocatedType(Ty) {
12321233
setAlignment(MaybeAlign(Align));
12331234
assert(!Ty->isVoidTy() && "Cannot allocate void!");
12341235
setName(Name);
12351236
}
12361237

12371238
AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
1238-
unsigned Align, const Twine &Name,
1239+
MaybeAlign Align, const Twine &Name,
12391240
BasicBlock *InsertAtEnd)
1240-
: UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca,
1241-
getAISize(Ty->getContext(), ArraySize), InsertAtEnd),
1241+
: UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca,
1242+
getAISize(Ty->getContext(), ArraySize), InsertAtEnd),
12421243
AllocatedType(Ty) {
1243-
setAlignment(MaybeAlign(Align));
1244+
setAlignment(Align);
12441245
assert(!Ty->isVoidTy() && "Cannot allocate void!");
12451246
setName(Name);
12461247
}
@@ -4126,9 +4127,9 @@ InsertValueInst *InsertValueInst::cloneImpl() const {
41264127
}
41274128

41284129
AllocaInst *AllocaInst::cloneImpl() const {
4129-
AllocaInst *Result = new AllocaInst(getAllocatedType(),
4130-
getType()->getAddressSpace(),
4131-
(Value *)getOperand(0), getAlignment());
4130+
AllocaInst *Result =
4131+
new AllocaInst(getAllocatedType(), getType()->getAddressSpace(),
4132+
(Value *)getOperand(0), MaybeAlign(getAlignment()));
41324133
Result->setUsedWithInAlloca(isUsedWithInAlloca());
41334134
Result->setSwiftError(isSwiftError());
41344135
return Result;

llvm/lib/Transforms/IPO/ArgumentPromotion.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,9 @@ doPromotion(Function *F, SmallPtrSetImpl<Argument *> &ArgsToPromote,
386386

387387
// Just add all the struct element types.
388388
Type *AgTy = cast<PointerType>(I->getType())->getElementType();
389-
Value *TheAlloca = new AllocaInst(AgTy, DL.getAllocaAddrSpace(), nullptr,
390-
I->getParamAlignment(), "", InsertPt);
389+
Value *TheAlloca =
390+
new AllocaInst(AgTy, DL.getAllocaAddrSpace(), nullptr,
391+
MaybeAlign(I->getParamAlignment()), "", InsertPt);
391392
StructType *STy = cast<StructType>(AgTy);
392393
Value *Idxs[2] = {ConstantInt::get(Type::getInt32Ty(F->getContext()), 0),
393394
nullptr};

llvm/lib/Transforms/Scalar/SROA.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4152,20 +4152,19 @@ AllocaInst *SROA::rewritePartition(AllocaInst &AI, AllocaSlices &AS,
41524152
// FIXME: We might want to defer PHI speculation until after here.
41534153
// FIXME: return nullptr;
41544154
} else {
4155-
unsigned Alignment = AI.getAlignment();
4156-
if (!Alignment) {
4157-
// The minimum alignment which users can rely on when the explicit
4158-
// alignment is omitted or zero is that required by the ABI for this
4159-
// type.
4160-
Alignment = DL.getABITypeAlignment(AI.getAllocatedType());
4161-
}
4162-
Alignment = MinAlign(Alignment, P.beginOffset());
4155+
// If alignment is unspecified we fallback on the one required by the ABI
4156+
// for this type. We also make sure the alignment is compatible with
4157+
// P.beginOffset().
4158+
const Align Alignment = commonAlignment(
4159+
DL.getValueOrABITypeAlignment(MaybeAlign(AI.getAlignment()),
4160+
AI.getAllocatedType()),
4161+
P.beginOffset());
41634162
// If we will get at least this much alignment from the type alone, leave
41644163
// the alloca's alignment unconstrained.
4165-
if (Alignment <= DL.getABITypeAlignment(SliceTy))
4166-
Alignment = 0;
4164+
const bool IsUnconstrained = Alignment <= DL.getABITypeAlignment(SliceTy);
41674165
NewAI = new AllocaInst(
4168-
SliceTy, AI.getType()->getAddressSpace(), nullptr, Alignment,
4166+
SliceTy, AI.getType()->getAddressSpace(), nullptr,
4167+
IsUnconstrained ? MaybeAlign() : Alignment,
41694168
AI.getName() + ".sroa." + Twine(P.begin() - AS.begin()), &AI);
41704169
// Copy the old AI debug location over to the new one.
41714170
NewAI->setDebugLoc(AI.getDebugLoc());

llvm/lib/Transforms/Utils/InlineFunction.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,16 +1293,16 @@ static Value *HandleByValArgument(Value *Arg, Instruction *TheCall,
12931293
}
12941294

12951295
// Create the alloca. If we have DataLayout, use nice alignment.
1296-
unsigned Align = DL.getPrefTypeAlignment(AggTy);
1296+
Align Alignment(DL.getPrefTypeAlignment(AggTy));
12971297

12981298
// If the byval had an alignment specified, we *must* use at least that
12991299
// alignment, as it is required by the byval argument (and uses of the
13001300
// pointer inside the callee).
1301-
Align = std::max(Align, ByValAlignment);
1301+
Alignment = max(Alignment, MaybeAlign(ByValAlignment));
13021302

1303-
Value *NewAlloca = new AllocaInst(AggTy, DL.getAllocaAddrSpace(),
1304-
nullptr, Align, Arg->getName(),
1305-
&*Caller->begin()->begin());
1303+
Value *NewAlloca =
1304+
new AllocaInst(AggTy, DL.getAllocaAddrSpace(), nullptr, Alignment,
1305+
Arg->getName(), &*Caller->begin()->begin());
13061306
IFI.StaticAllocas.push_back(cast<AllocaInst>(NewAlloca));
13071307

13081308
// Uses of the argument in the function should use our new alloca

0 commit comments

Comments
 (0)