Author: Ömer Sinan Ağacan Date: 2026-10-11T15:20:10+02:00 New Revision: cd9c8d090c1a87ebc619e7aba9a036f06466be5e
URL: https://github.com/llvm/llvm-project/commit/cd9c8d090c1a87ebc619e7aba9a036f06466be5e DIFF: https://github.com/llvm/llvm-project/commit/cd9c8d090c1a87ebc619e7aba9a036f06466be5e.diff LOG: [Alignment] Add a static function to construct an `Align` from power-of-2 values (#230920) Many call sites of the `Align` constructor uses it as `Align(1ULL << n)`, and the constructors takes the `Log2` of the value. Add a new static function to allow `Align::fromLog2(n)` and update old call sites. This avoids the unnecessary `Log2(1ULL << n)` computations when constructing `Align`s. Added: Modified: clang/lib/CodeGen/CodeGenModule.cpp lld/MachO/SyntheticSections.cpp lld/wasm/InputChunks.h llvm/include/llvm/IR/Instructions.h llvm/include/llvm/Support/Alignment.h llvm/lib/CodeGen/MachineBlockPlacement.cpp llvm/lib/CodeGen/MachineFunction.cpp llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp llvm/lib/MC/MCParser/AsmParser.cpp llvm/lib/MC/MCParser/DarwinAsmParser.cpp llvm/lib/MC/MCParser/MasmParser.cpp llvm/lib/ObjCopy/MachO/MachOLayoutBuilder.cpp llvm/lib/Target/ARM/ARMISelLowering.cpp llvm/lib/Target/BPF/BPFPreserveStaticOffset.cpp llvm/lib/Target/Hexagon/HexagonISelLowering.cpp llvm/lib/Target/Mips/MipsSEISelDAGToDAG.cpp llvm/lib/Target/X86/X86ISelLowering.cpp llvm/lib/Target/X86/X86InstrInfo.cpp llvm/lib/Transforms/IPO/AttributorAttributes.cpp llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp llvm/lib/Transforms/Instrumentation/TypeSanitizer.cpp llvm/lib/Transforms/Scalar/InferAlignment.cpp llvm/lib/Transforms/Utils/Local.cpp llvm/unittests/CodeGen/GlobalISel/KnownBitsTest.cpp llvm/unittests/Support/AlignmentTest.cpp llvm/utils/TableGen/X86FoldTablesEmitter.cpp Removed: ################################################################################ diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index 62ef11a697b1c..f8268d0593806 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -3306,7 +3306,7 @@ void CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D, if (unsigned alignment = D->getMaxAlignment() / Context.getCharWidth()) ExplicitAlignment = llvm::Align(alignment); else if (LangOpts.FunctionAlignment) - ExplicitAlignment = llvm::Align(1ull << LangOpts.FunctionAlignment); + ExplicitAlignment = llvm::Align::fromLog2(LangOpts.FunctionAlignment); if (ExplicitAlignment) { F->setAlignment(ExplicitAlignment); diff --git a/lld/MachO/SyntheticSections.cpp b/lld/MachO/SyntheticSections.cpp index 808f32fb2b094..cc7aaa2c7ee9f 100644 --- a/lld/MachO/SyntheticSections.cpp +++ b/lld/MachO/SyntheticSections.cpp @@ -1731,7 +1731,7 @@ void CStringSection::writeTo(uint8_t *buf) const { // `16 % A`. static Align getStringPieceAlignment(const CStringInputSection &isec, const StringPiece &piece) { - return llvm::Align(1ULL << llvm::countr_zero(isec.align | piece.inSecOff)); + return llvm::Align::fromLog2(llvm::countr_zero(isec.align | piece.inSecOff)); } void CStringSection::finalizeContents() { diff --git a/lld/wasm/InputChunks.h b/lld/wasm/InputChunks.h index 0e4b8882c90a3..9c519a9c3dbe9 100644 --- a/lld/wasm/InputChunks.h +++ b/lld/wasm/InputChunks.h @@ -232,8 +232,8 @@ class SyntheticMergedChunk : public InputChunk { public: SyntheticMergedChunk(StringRef name, uint32_t alignment, uint32_t flags) : InputChunk(nullptr, InputChunk::MergedChunk, name, alignment, flags), - builder(llvm::StringTableBuilder::RAW, llvm::Align(1ULL << alignment)) { - } + builder(llvm::StringTableBuilder::RAW, + llvm::Align::fromLog2(alignment)) {} static bool classof(const InputChunk *c) { return c->kind() == InputChunk::MergedChunk; diff --git a/llvm/include/llvm/IR/Instructions.h b/llvm/include/llvm/IR/Instructions.h index 47e9a79436e45..5d56e130eec5a 100644 --- a/llvm/include/llvm/IR/Instructions.h +++ b/llvm/include/llvm/IR/Instructions.h @@ -134,7 +134,7 @@ class AllocaInst : public UnaryInstruction { /// Return the alignment of the memory that is being allocated by the /// instruction. Align getAlign() const { - return Align(1ULL << getSubclassData<AlignmentField>()); + return Align::fromLog2(getSubclassData<AlignmentField>()); } void setAlignment(Align Align) { @@ -240,7 +240,7 @@ class LoadInst : public UnaryInstruction { /// Return the alignment of the access that is being performed. Align getAlign() const { - return Align(1ULL << (getSubclassData<AlignmentField>())); + return Align::fromLog2(getSubclassData<AlignmentField>()); } void setAlignment(Align Align) { @@ -388,7 +388,7 @@ class StoreInst : public Instruction { DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); Align getAlign() const { - return Align(1ULL << (getSubclassData<AlignmentField>())); + return Align::fromLog2(getSubclassData<AlignmentField>()); } void setAlignment(Align Align) { @@ -612,7 +612,7 @@ class AtomicCmpXchgInst : public Instruction { /// Return the alignment of the memory that is being allocated by the /// instruction. Align getAlign() const { - return Align(1ULL << getSubclassData<AlignmentField>()); + return Align::fromLog2(getSubclassData<AlignmentField>()); } void setAlignment(Align Align) { @@ -918,7 +918,7 @@ class AtomicRMWInst : public Instruction { /// Return the alignment of the memory that is being allocated by the /// instruction. Align getAlign() const { - return Align(1ULL << getSubclassData<AlignmentField>()); + return Align::fromLog2(getSubclassData<AlignmentField>()); } void setAlignment(Align Align) { diff --git a/llvm/include/llvm/Support/Alignment.h b/llvm/include/llvm/Support/Alignment.h index 2345dbe62c346..a1d1384607880 100644 --- a/llvm/include/llvm/Support/Alignment.h +++ b/llvm/include/llvm/Support/Alignment.h @@ -52,9 +52,6 @@ struct Align { friend unsigned encode(struct MaybeAlign A); friend struct MaybeAlign decodeMaybeAlign(unsigned Value); - struct FromShiftValue {}; - constexpr Align(FromShiftValue, uint8_t Shift) : ShiftValue(Shift) {} - public: /// Default is byte-aligned. constexpr Align() = default; @@ -72,6 +69,14 @@ struct Align { assert(ShiftValue < 64 && "Broken invariant"); } + /// Returns an alignment of `1 << Log2Value` bytes. + constexpr static Align fromLog2(unsigned Log2Value) { + assert(Log2Value < 64 && "Alignment exponent is too large"); + Align Out; + Out.ShiftValue = Log2Value; + return Out; + } + /// This is a hole in the type system and should not be abused. /// Needed to interact with C for instance. constexpr uint64_t value() const { return uint64_t(1) << ShiftValue; } @@ -86,7 +91,7 @@ struct Align { /// Allow constructions of constexpr Align. template <size_t kValue> constexpr static Align Constant() { - return Align(FromShiftValue{}, ConstantLog2<kValue>()); + return fromLog2(ConstantLog2<kValue>()); } /// Allow constructions of constexpr Align from types. diff --git a/llvm/lib/CodeGen/MachineBlockPlacement.cpp b/llvm/lib/CodeGen/MachineBlockPlacement.cpp index 9c9c482276999..276e1bc67f797 100644 --- a/llvm/lib/CodeGen/MachineBlockPlacement.cpp +++ b/llvm/lib/CodeGen/MachineBlockPlacement.cpp @@ -3118,10 +3118,10 @@ void MachineBlockPlacement::alignBlocks() { // Align all of the blocks in the function to a specific alignment. for (MachineBasicBlock &MBB : *F) { if (HasMaxBytesOverride) - MBB.setAlignment(Align(1ULL << AlignAllBlock), + MBB.setAlignment(Align::fromLog2(AlignAllBlock), MaxBytesForAlignmentOverride); else - MBB.setAlignment(Align(1ULL << AlignAllBlock)); + MBB.setAlignment(Align::fromLog2(AlignAllBlock)); } else if (AlignAllNonFallThruBlocks) { // Align all of the blocks that have no fall-through predecessors to a @@ -3130,10 +3130,10 @@ void MachineBlockPlacement::alignBlocks() { auto LayoutPred = std::prev(MBI); if (!LayoutPred->isSuccessor(&*MBI)) { if (HasMaxBytesOverride) - MBI->setAlignment(Align(1ULL << AlignAllNonFallThruBlocks), + MBI->setAlignment(Align::fromLog2(AlignAllNonFallThruBlocks), MaxBytesForAlignmentOverride); else - MBI->setAlignment(Align(1ULL << AlignAllNonFallThruBlocks)); + MBI->setAlignment(Align::fromLog2(AlignAllNonFallThruBlocks)); } } } diff --git a/llvm/lib/CodeGen/MachineFunction.cpp b/llvm/lib/CodeGen/MachineFunction.cpp index 282cce8667deb..4be10bc8b4202 100644 --- a/llvm/lib/CodeGen/MachineFunction.cpp +++ b/llvm/lib/CodeGen/MachineFunction.cpp @@ -237,7 +237,7 @@ void MachineFunction::init() { Alignment = std::max(Alignment, Align(4)); if (AlignAllFunctions) - Alignment = Align(1ULL << AlignAllFunctions); + Alignment = Align::fromLog2(AlignAllFunctions); JumpTableInfo = nullptr; diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp index 05398cb82f22a..7bd01594d394b 100644 --- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp @@ -14519,7 +14519,8 @@ MaybeAlign SelectionDAG::InferPtrAlign(SDValue Ptr) const { llvm::computeKnownBits(GV, Known, getDataLayout()); unsigned AlignBits = Known.countMinTrailingZeros(); if (AlignBits) - return commonAlignment(Align(1ull << std::min(31U, AlignBits)), GVOffset); + return commonAlignment(Align::fromLog2(std::min(31U, AlignBits)), + GVOffset); } // If this is a direct reference to a stack slot, use information about the diff --git a/llvm/lib/MC/MCParser/AsmParser.cpp b/llvm/lib/MC/MCParser/AsmParser.cpp index 588cc8e3ffde3..b50f27cca1be9 100644 --- a/llvm/lib/MC/MCParser/AsmParser.cpp +++ b/llvm/lib/MC/MCParser/AsmParser.cpp @@ -3550,7 +3550,7 @@ bool AsmParser::parseDirectivePrefAlign() { getStreamer().getCurrentSectionOnly()->getName() + "'"); - getStreamer().emitPrefAlign(Align(1ULL << Log2Alignment), *End, EmitNops, + getStreamer().emitPrefAlign(Align::fromLog2(Log2Alignment), *End, EmitNops, Fill, getTargetParser().getSTI()); return false; } @@ -5200,11 +5200,11 @@ bool AsmParser::parseDirectiveComm(bool IsLocal) { // Create the Symbol as a common or local common with Size and Pow2Alignment if (IsLocal) { getStreamer().emitLocalCommonSymbol(Sym, Size, - Align(1ULL << Pow2Alignment)); + Align::fromLog2(Pow2Alignment)); return false; } - getStreamer().emitCommonSymbol(Sym, Size, Align(1ULL << Pow2Alignment)); + getStreamer().emitCommonSymbol(Sym, Size, Align::fromLog2(Pow2Alignment)); return false; } @@ -6016,7 +6016,7 @@ bool AsmParser::parseDirectiveBundleAlignMode() { "invalid bundle alignment size (expected between 1 and 30)")) return true; - getStreamer().emitBundleAlignMode(Align(1ULL << AlignSizePow2)); + getStreamer().emitBundleAlignMode(Align::fromLog2(AlignSizePow2)); return false; } diff --git a/llvm/lib/MC/MCParser/DarwinAsmParser.cpp b/llvm/lib/MC/MCParser/DarwinAsmParser.cpp index ba6813c142266..de394168abe10 100644 --- a/llvm/lib/MC/MCParser/DarwinAsmParser.cpp +++ b/llvm/lib/MC/MCParser/DarwinAsmParser.cpp @@ -862,7 +862,7 @@ bool DarwinAsmParser::parseDirectiveTBSS(StringRef, SMLoc) { getContext().getMachOSection("__DATA", "__thread_bss", MachO::S_THREAD_LOCAL_ZEROFILL, 0, SectionKind::getThreadBSS()), - Sym, Size, Align(1ULL << Pow2Alignment)); + Sym, Size, Align::fromLog2(Pow2Alignment)); return false; } @@ -948,7 +948,7 @@ bool DarwinAsmParser::parseDirectiveZerofill(StringRef, SMLoc) { getStreamer().emitZerofill( getContext().getMachOSection(Segment, Section, MachO::S_ZEROFILL, 0, SectionKind::getBSS()), - Sym, Size, Align(1ULL << Pow2Alignment), SectionLoc); + Sym, Size, Align::fromLog2(Pow2Alignment), SectionLoc); return false; } diff --git a/llvm/lib/MC/MCParser/MasmParser.cpp b/llvm/lib/MC/MCParser/MasmParser.cpp index 6b97f16aa6fcc..ac525f175fd54 100644 --- a/llvm/lib/MC/MCParser/MasmParser.cpp +++ b/llvm/lib/MC/MCParser/MasmParser.cpp @@ -4629,11 +4629,11 @@ bool MasmParser::parseDirectiveComm(bool IsLocal) { // Create the Symbol as a common or local common with Size and Pow2Alignment. if (IsLocal) { getStreamer().emitLocalCommonSymbol(Sym, Size, - Align(1ULL << Pow2Alignment)); + Align::fromLog2(Pow2Alignment)); return false; } - getStreamer().emitCommonSymbol(Sym, Size, Align(1ULL << Pow2Alignment)); + getStreamer().emitCommonSymbol(Sym, Size, Align::fromLog2(Pow2Alignment)); return false; } diff --git a/llvm/lib/ObjCopy/MachO/MachOLayoutBuilder.cpp b/llvm/lib/ObjCopy/MachO/MachOLayoutBuilder.cpp index 8cd418276f851..af29762580600 100644 --- a/llvm/lib/ObjCopy/MachO/MachOLayoutBuilder.cpp +++ b/llvm/lib/ObjCopy/MachO/MachOLayoutBuilder.cpp @@ -159,7 +159,7 @@ uint64_t MachOLayoutBuilder::layoutSegments() { Sec->Offset = 0; } else { uint64_t PaddingSize = - offsetToAlignment(SegFileSize, Align(1ull << Sec->Align)); + offsetToAlignment(SegFileSize, Align::fromLog2(Sec->Align)); Sec->Offset = SegOffset + SegFileSize + PaddingSize; Sec->Size = Sec->Content.size(); SegFileSize += PaddingSize + Sec->Size; diff --git a/llvm/lib/Target/ARM/ARMISelLowering.cpp b/llvm/lib/Target/ARM/ARMISelLowering.cpp index 0652c231bc70c..1f94fec4a3f79 100644 --- a/llvm/lib/Target/ARM/ARMISelLowering.cpp +++ b/llvm/lib/Target/ARM/ARMISelLowering.cpp @@ -1440,9 +1440,10 @@ ARMTargetLowering::ARMTargetLowering(const TargetMachine &TM_, // Prefer likely predicted branches to selects on out-of-order cores. PredictableSelectIsExpensive = Subtarget->getSchedModel().isOutOfOrder(); - setPrefLoopAlignment(Align(1ULL << Subtarget->getPreferBranchLogAlignment())); + setPrefLoopAlignment( + Align::fromLog2(Subtarget->getPreferBranchLogAlignment())); setPrefFunctionAlignment( - Align(1ULL << Subtarget->getPreferBranchLogAlignment())); + Align::fromLog2(Subtarget->getPreferBranchLogAlignment())); setMinFunctionAlignment(Subtarget->isThumb() ? Align(2) : Align(4)); diff --git a/llvm/lib/Target/BPF/BPFPreserveStaticOffset.cpp b/llvm/lib/Target/BPF/BPFPreserveStaticOffset.cpp index ea9155c60d381..21fabe1d07e38 100644 --- a/llvm/lib/Target/BPF/BPFPreserveStaticOffset.cpp +++ b/llvm/lib/Target/BPF/BPFPreserveStaticOffset.cpp @@ -291,7 +291,7 @@ static void reconstructCommon(CallInst *Call, GetElementPtrInst *GEP, T *Insn, Insn->setOrdering((AtomicOrdering)getOperandAsUnsigned(Call, 2 + Delta)); Insn->setSyncScopeID(getOperandAsUnsigned(Call, 3 + Delta)); unsigned AlignShiftValue = getOperandAsUnsigned(Call, 4 + Delta); - Insn->setAlignment(Align(1ULL << AlignShiftValue)); + Insn->setAlignment(Align::fromLog2(AlignShiftValue)); GEP->setDebugLoc(Call->getDebugLoc()); Insn->setDebugLoc(Call->getDebugLoc()); Insn->setAAMetadata(Call->getAAMetadata()); diff --git a/llvm/lib/Target/Hexagon/HexagonISelLowering.cpp b/llvm/lib/Target/Hexagon/HexagonISelLowering.cpp index 41a6afa2f59ca..b6e16893bc134 100644 --- a/llvm/lib/Target/Hexagon/HexagonISelLowering.cpp +++ b/llvm/lib/Target/Hexagon/HexagonISelLowering.cpp @@ -1939,7 +1939,7 @@ HexagonTargetLowering::validateConstPtrAlignment(SDValue Ptr, Align NeedAlign, return true; unsigned Addr = CA->getZExtValue(); Align HaveAlign = - Addr != 0 ? Align(1ull << llvm::countr_zero(Addr)) : NeedAlign; + Addr != 0 ? Align::fromLog2(llvm::countr_zero(Addr)) : NeedAlign; if (HaveAlign >= NeedAlign) return true; diff --git a/llvm/lib/Target/Mips/MipsSEISelDAGToDAG.cpp b/llvm/lib/Target/Mips/MipsSEISelDAGToDAG.cpp index 71490f3b7a37c..54a2260b8311c 100644 --- a/llvm/lib/Target/Mips/MipsSEISelDAGToDAG.cpp +++ b/llvm/lib/Target/Mips/MipsSEISelDAGToDAG.cpp @@ -289,7 +289,7 @@ bool MipsSEDAGToDAGISel::selectAddrFrameIndexOffset( Base = Addr.getOperand(0); // If base is a FI, additional offset calculation is done in // eliminateFrameIndex, otherwise we need to check the alignment - const Align Alignment(1ULL << ShiftAmount); + const Align Alignment = Align::fromLog2(ShiftAmount); if (!isAligned(Alignment, CN->getZExtValue())) return false; } diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp index a265c163cf276..6304509e365a5 100644 --- a/llvm/lib/Target/X86/X86ISelLowering.cpp +++ b/llvm/lib/Target/X86/X86ISelLowering.cpp @@ -65673,6 +65673,6 @@ Align X86TargetLowering::getPrefLoopAlignment( std::optional<int> InnermostAlign = Subtarget.getCLOpts().experimental_pref_innermost_loop_alignment; if (ML && ML->isInnermost() && InnermostAlign) - return Align(1ULL << *InnermostAlign); + return Align::fromLog2(*InnermostAlign); return TargetLowering::getPrefLoopAlignment(); } diff --git a/llvm/lib/Target/X86/X86InstrInfo.cpp b/llvm/lib/Target/X86/X86InstrInfo.cpp index 6dfebe1cc15cc..2567c569899de 100644 --- a/llvm/lib/Target/X86/X86InstrInfo.cpp +++ b/llvm/lib/Target/X86/X86InstrInfo.cpp @@ -7781,7 +7781,7 @@ MachineInstr *X86InstrInfo::foldMemoryOperandImpl( if (I) { unsigned Opcode = I->DstOp; if (Alignment < - Align(1ULL << ((I->Flags & TB_ALIGN_MASK) >> TB_ALIGN_SHIFT))) + Align::fromLog2((I->Flags & TB_ALIGN_MASK) >> TB_ALIGN_SHIFT)) return nullptr; bool NarrowToMOV32rm = false; if (Size) { diff --git a/llvm/lib/Transforms/IPO/AttributorAttributes.cpp b/llvm/lib/Transforms/IPO/AttributorAttributes.cpp index 9af51dedab174..baf2567d3e957 100644 --- a/llvm/lib/Transforms/IPO/AttributorAttributes.cpp +++ b/llvm/lib/Transforms/IPO/AttributorAttributes.cpp @@ -5270,7 +5270,7 @@ static unsigned getKnownAlignForUse(Attributor &A, AAAlign &QueryingAA, if (ConstVals && ConstVals->isValidState() && ConstVals->isAtFixpoint()) { unsigned ShiftValue = std::min(ConstVals->getAssumedMinTrailingZeros(), Value::MaxAlignmentExponent); - Align ConstAlign(UINT64_C(1) << ShiftValue); + Align ConstAlign = Align::fromLog2(ShiftValue); if (ConstAlign >= AlignAA->getKnownAlign()) return Align(1).value(); } @@ -5601,7 +5601,7 @@ struct AAAlignCallSiteReturned final unsigned ShiftValue = std::min(ConstVals->getAssumedMinTrailingZeros(), Value::MaxAlignmentExponent); - Alignment = Align(UINT64_C(1) << ShiftValue); + Alignment = Align::fromLog2(ShiftValue); Valid = true; } diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp index 8c18d67db72db..6fb5f50244a46 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp @@ -2884,7 +2884,7 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) { // the pointer argument. if (NewAlignmentLog > Log2(CI.getRetAlign().valueOrOne())) { CI.addRetAttr(Attribute::getWithAlignment( - CI.getContext(), Align(uint64_t(1) << NewAlignmentLog))); + CI.getContext(), Align::fromLog2(NewAlignmentLog))); Changed = true; } if (Changed) diff --git a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp index c8acf6dd63130..5125584a5422b 100644 --- a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp +++ b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp @@ -2366,7 +2366,7 @@ void ModuleAddressSanitizer::InstrumentGlobalsWithMetadataArray( M, ArrayOfGlobalStructTy, false, GlobalVariable::InternalLinkage, ConstantArray::get(ArrayOfGlobalStructTy, MetadataInitializers), ""); if (Mapping.Scale > 3) - AllGlobals->setAlignment(Align(1ULL << Mapping.Scale)); + AllGlobals->setAlignment(Align::fromLog2(Mapping.Scale)); if (ConstructorKind == AsanCtorKind::Global) IRB.CreateCall(AsanRegisterGlobals, diff --git a/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp index dfd6c3c0709f8..ea31e08a4661a 100644 --- a/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp +++ b/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp @@ -282,7 +282,7 @@ class HWAddressSanitizer { public: void init(const InstrumentationOptions &Opts, Triple &TargetTriple, bool InstrumentWithCalls, bool CompileKernel); - Align getObjectAlignment() const { return Align(1ULL << Scale); } + Align getObjectAlignment() const { return Align::fromLog2(Scale); } bool isInGlobal() const { return Kind == OffsetKind::kGlobal; } bool isInIfunc() const { return Kind == OffsetKind::kIfunc; } bool isInTls() const { return Kind == OffsetKind::kTls; } diff --git a/llvm/lib/Transforms/Instrumentation/TypeSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/TypeSanitizer.cpp index 42d42b8bae894..e2d9cc4450ff6 100644 --- a/llvm/lib/Transforms/Instrumentation/TypeSanitizer.cpp +++ b/llvm/lib/Transforms/Instrumentation/TypeSanitizer.cpp @@ -930,7 +930,8 @@ bool TypeSanitizer::instrumentMemInst(Value *V, Instruction *ShadowBase, if (!Src) { IRB.CreateMemSet(ShadowData, IRB.getInt8(0), - IRB.CreateShl(Size, PtrShift), Align(1ull << PtrShift)); + IRB.CreateShl(Size, PtrShift), + Align::fromLog2(PtrShift)); return true; } @@ -942,11 +943,13 @@ bool TypeSanitizer::instrumentMemInst(Value *V, Instruction *ShadowBase, Value *SrcShadowData = IRB.CreateIntToPtr(SrcShadowDataInt, IRB.getPtrTy()); if (NeedsMemMove) { - IRB.CreateMemMove(ShadowData, Align(1ull << PtrShift), SrcShadowData, - Align(1ull << PtrShift), IRB.CreateShl(Size, PtrShift)); + IRB.CreateMemMove(ShadowData, Align::fromLog2(PtrShift), SrcShadowData, + Align::fromLog2(PtrShift), + IRB.CreateShl(Size, PtrShift)); } else { - IRB.CreateMemCpy(ShadowData, Align(1ull << PtrShift), SrcShadowData, - Align(1ull << PtrShift), IRB.CreateShl(Size, PtrShift)); + IRB.CreateMemCpy(ShadowData, Align::fromLog2(PtrShift), SrcShadowData, + Align::fromLog2(PtrShift), + IRB.CreateShl(Size, PtrShift)); } } diff --git a/llvm/lib/Transforms/Scalar/InferAlignment.cpp b/llvm/lib/Transforms/Scalar/InferAlignment.cpp index c734eafd6e75d..d0f952a6c29cf 100644 --- a/llvm/lib/Transforms/Scalar/InferAlignment.cpp +++ b/llvm/lib/Transforms/Scalar/InferAlignment.cpp @@ -133,7 +133,7 @@ bool inferAlignment(Function &F, AssumptionCache &AC, DominatorTree &DT) { KnownBits Known = computeKnownBits(PtrOp, DL, &AC, &I, &DT); unsigned TrailZ = std::min(Known.countMinTrailingZeros(), +Value::MaxAlignmentExponent); - return Align(1ull << std::min(Known.getBitWidth() - 1, TrailZ)); + return Align::fromLog2(std::min(Known.getBitWidth() - 1, TrailZ)); }; // Propagate alignment between loads and stores that originate from the diff --git a/llvm/lib/Transforms/Utils/Local.cpp b/llvm/lib/Transforms/Utils/Local.cpp index 4f1a44c903192..0511064a04d94 100644 --- a/llvm/lib/Transforms/Utils/Local.cpp +++ b/llvm/lib/Transforms/Utils/Local.cpp @@ -1575,7 +1575,7 @@ Align llvm::getOrEnforceKnownAlignment(Value *V, MaybeAlign PrefAlign, // LLVM doesn't support alignments larger than (1 << MaxAlignmentExponent). TrailZ = std::min(TrailZ, +Value::MaxAlignmentExponent); - Align Alignment = Align(1ull << std::min(Known.getBitWidth() - 1, TrailZ)); + Align Alignment = Align::fromLog2(std::min(Known.getBitWidth() - 1, TrailZ)); if (PrefAlign && *PrefAlign > Alignment) Alignment = std::max(Alignment, tryEnforceAlignment(V, *PrefAlign, DL)); diff --git a/llvm/unittests/CodeGen/GlobalISel/KnownBitsTest.cpp b/llvm/unittests/CodeGen/GlobalISel/KnownBitsTest.cpp index 18aca17fe7e59..6022e06a0611c 100644 --- a/llvm/unittests/CodeGen/GlobalISel/KnownBitsTest.cpp +++ b/llvm/unittests/CodeGen/GlobalISel/KnownBitsTest.cpp @@ -2121,7 +2121,8 @@ TEST_F(AMDGPUGISelMITest, TestKnownBitsAssertAlign) { EXPECT_EQ(64u, Res.getBitWidth()); EXPECT_EQ(NumBits - 1, Res.Zero.countr_one()); EXPECT_EQ(64u, Res.One.countr_zero()); - EXPECT_EQ(Align(1ull << (NumBits - 1)), Info.computeKnownAlignment(Copies[Idx])); + EXPECT_EQ(Align::fromLog2(NumBits - 1), + Info.computeKnownAlignment(Copies[Idx])); }; const unsigned NumSetupCopies = 5; diff --git a/llvm/unittests/Support/AlignmentTest.cpp b/llvm/unittests/Support/AlignmentTest.cpp index 3a4416128d0e5..ae67715fcd219 100644 --- a/llvm/unittests/Support/AlignmentTest.cpp +++ b/llvm/unittests/Support/AlignmentTest.cpp @@ -39,6 +39,16 @@ TEST(AlignmentTest, AlignConstant) { EXPECT_EQ(Align::Constant<64>(), Align(64)); } +TEST(AlignmentTest, AlignFromLog2) { + for (unsigned Log2Value = 0; Log2Value < 64; ++Log2Value) { + Align A = Align::fromLog2(Log2Value); + EXPECT_EQ(A, Align(1ULL << Log2Value)); + EXPECT_EQ(Log2(A), Log2Value); + } + constexpr Align kConstantAlign = Align::fromLog2(4); + static_assert(kConstantAlign.value() == 16); +} + TEST(AlignmentTest, AlignConstexprConstant) { constexpr Align kConstantAlign = Align::Of<uint64_t>(); EXPECT_EQ(Align(alignof(uint64_t)), kConstantAlign); @@ -251,6 +261,7 @@ TEST(AlignmentDeathTest, InvalidCTors) { EXPECT_DEATH((MaybeAlign(Value)), "Alignment is neither 0 nor a power of 2"); } + EXPECT_DEATH((Align::fromLog2(64)), "Alignment exponent is too large"); } TEST(AlignmentDeathTest, ComparisonsWithZero) { diff --git a/llvm/utils/TableGen/X86FoldTablesEmitter.cpp b/llvm/utils/TableGen/X86FoldTablesEmitter.cpp index a909d4f0378b8..e7854b1563a7a 100644 --- a/llvm/utils/TableGen/X86FoldTablesEmitter.cpp +++ b/llvm/utils/TableGen/X86FoldTablesEmitter.cpp @@ -495,7 +495,7 @@ void X86FoldTablesEmitter::addEntryWithFlags(FoldTable &Table, Result.NoForward = S & TB_NO_FORWARD; Result.FoldLoad = S & TB_FOLDED_LOAD; Result.FoldStore = S & TB_FOLDED_STORE; - Result.Alignment = Align(1ULL << ((S & TB_ALIGN_MASK) >> TB_ALIGN_SHIFT)); + Result.Alignment = Align::fromLog2((S & TB_ALIGN_MASK) >> TB_ALIGN_SHIFT); if (IsManual) { Table[RegInst] = Result; return; _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
