diff --git a/llvm/include/llvm/CodeGen/GlobalISel/MachineIRBuilder.h b/llvm/include/llvm/CodeGen/GlobalISel/MachineIRBuilder.h --- a/llvm/include/llvm/CodeGen/GlobalISel/MachineIRBuilder.h +++ b/llvm/include/llvm/CodeGen/GlobalISel/MachineIRBuilder.h @@ -379,7 +379,14 @@ /// /// \return a MachineInstrBuilder for the newly created instruction. MachineInstrBuilder buildDynStackAlloc(const DstOp &Res, const SrcOp &Size, - unsigned Align); + MaybeAlign Align); + + LLVM_ATTRIBUTE_DEPRECATED(inline MachineInstrBuilder buildDynStackAlloc( + const DstOp &Res, const SrcOp &Size, + unsigned Align), + "Use the version that takes MaybeAlign instead") { + return buildDynStackAlloc(Res, Size, MaybeAlign(Align)); + } /// Build and insert \p Res = G_FRAME_INDEX \p Idx /// diff --git a/llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp b/llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp --- a/llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp +++ b/llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp @@ -1858,12 +1858,7 @@ return false; // Now we're in the harder dynamic case. - Type *Ty = AI.getAllocatedType(); - unsigned Align = - std::max((unsigned)DL->getPrefTypeAlignment(Ty), AI.getAlignment()); - Register NumElts = getOrCreateVReg(*AI.getArraySize()); - Type *IntPtrIRTy = DL->getIntPtrType(AI.getType()); LLT IntPtrTy = getLLTForType(*IntPtrIRTy, *DL); if (MRI->getType(NumElts) != IntPtrTy) { @@ -1872,29 +1867,32 @@ NumElts = ExtElts; } + Type *Ty = AI.getAllocatedType(); + Register AllocSize = MRI->createGenericVirtualRegister(IntPtrTy); Register TySize = getOrCreateVReg(*ConstantInt::get(IntPtrIRTy, DL->getTypeAllocSize(Ty))); MIRBuilder.buildMul(AllocSize, NumElts, TySize); - unsigned StackAlign = - MF->getSubtarget().getFrameLowering()->getStackAlignment(); - if (Align <= StackAlign) - Align = 0; - // Round the size of the allocation up to the stack alignment size // by add SA-1 to the size. This doesn't overflow because we're computing // an address inside an alloca. - auto SAMinusOne = MIRBuilder.buildConstant(IntPtrTy, StackAlign - 1); + Align StackAlign = MF->getSubtarget().getFrameLowering()->getStackAlign(); + auto SAMinusOne = MIRBuilder.buildConstant(IntPtrTy, StackAlign.value() - 1); auto AllocAdd = MIRBuilder.buildAdd(IntPtrTy, AllocSize, SAMinusOne, MachineInstr::NoUWrap); auto AlignCst = - MIRBuilder.buildConstant(IntPtrTy, ~(uint64_t)(StackAlign - 1)); + MIRBuilder.buildConstant(IntPtrTy, ~(uint64_t)(StackAlign.value() - 1)); auto AlignedAlloc = MIRBuilder.buildAnd(IntPtrTy, AllocAdd, AlignCst); - MIRBuilder.buildDynStackAlloc(getOrCreateVReg(AI), AlignedAlloc, Align); + Align Alignment = max(AI.getAlign(), DL->getPrefTypeAlign(Ty)); + MaybeAlign DynStackAllocAlign = Alignment; + if (Alignment <= StackAlign) + DynStackAllocAlign = None; + MIRBuilder.buildDynStackAlloc(getOrCreateVReg(AI), AlignedAlloc, + DynStackAllocAlign); - MF->getFrameInfo().CreateVariableSizedObject(Align ? Align : 1, &AI); + MF->getFrameInfo().CreateVariableSizedObject(Alignment, &AI); assert(MF->getFrameInfo().hasVarSizedObjects()); return true; } diff --git a/llvm/lib/CodeGen/GlobalISel/MachineIRBuilder.cpp b/llvm/lib/CodeGen/GlobalISel/MachineIRBuilder.cpp --- a/llvm/lib/CodeGen/GlobalISel/MachineIRBuilder.cpp +++ b/llvm/lib/CodeGen/GlobalISel/MachineIRBuilder.cpp @@ -162,12 +162,12 @@ MachineInstrBuilder MachineIRBuilder::buildDynStackAlloc(const DstOp &Res, const SrcOp &Size, - unsigned Align) { + MaybeAlign Align) { assert(Res.getLLTTy(*getMRI()).isPointer() && "expected ptr dst type"); auto MIB = buildInstr(TargetOpcode::G_DYN_STACKALLOC); Res.addDefToMIB(*getMRI(), MIB); Size.addSrcToMIB(MIB); - MIB.addImm(Align); + MIB.addImm(Align ? Align->value() : 0); return MIB; }