From 764a7f48647210f4a360f74f82432ad276d92716 Mon Sep 17 00:00:00 2001 From: David Green Date: Wed, 11 May 2022 08:18:58 +0100 Subject: [PATCH] [TypePromotion] Format Type Promotion. NFC This clang-formats the TypePromotion code, with the only meaningful change being the removal of a verifyFunction call inside a LLVM_DEBUG, and the printing of the entire function which can be better handled via -print-after-all. --- llvm/lib/CodeGen/TypePromotion.cpp | 95 +++++++++++++++++--------------------- 1 file changed, 43 insertions(+), 52 deletions(-) diff --git a/llvm/lib/CodeGen/TypePromotion.cpp b/llvm/lib/CodeGen/TypePromotion.cpp index 48454a5..6b991aa 100644 --- a/llvm/lib/CodeGen/TypePromotion.cpp +++ b/llvm/lib/CodeGen/TypePromotion.cpp @@ -43,9 +43,9 @@ using namespace llvm; -static cl::opt -DisablePromotion("disable-type-promotion", cl::Hidden, cl::init(false), - cl::desc("Disable type promotion pass")); +static cl::opt DisablePromotion("disable-type-promotion", cl::Hidden, + cl::init(false), + cl::desc("Disable type promotion pass")); // The goal of this pass is to enable more efficient code generation for // operations on narrow types (i.e. types with < 32-bits) and this is a @@ -104,15 +104,15 @@ class IRPromoter { LLVMContext &Ctx; IntegerType *OrigTy = nullptr; unsigned PromotedWidth = 0; - SetVector &Visited; - SetVector &Sources; - SetVector &Sinks; + SetVector &Visited; + SetVector &Sources; + SetVector &Sinks; SmallPtrSetImpl &SafeWrap; IntegerType *ExtTy = nullptr; - SmallPtrSet NewInsts; - SmallPtrSet InstsToRemove; - DenseMap> TruncTysMap; - SmallPtrSet Promoted; + SmallPtrSet NewInsts; + SmallPtrSet InstsToRemove; + DenseMap> TruncTysMap; + SmallPtrSet Promoted; void ReplaceAllUsersOfWith(Value *From, Value *To); void ExtendSources(); @@ -141,8 +141,8 @@ class TypePromotion : public FunctionPass { unsigned TypeSize = 0; LLVMContext *Ctx = nullptr; unsigned RegisterBitWidth = 0; - SmallPtrSet AllVisited; - SmallPtrSet SafeToPromote; + SmallPtrSet AllVisited; + SmallPtrSet SafeToPromote; SmallPtrSet SafeWrap; // Does V have the same size result type as TypeSize. @@ -189,7 +189,7 @@ public: bool runOnFunction(Function &F) override; }; -} +} // namespace static bool GenerateSignBits(Instruction *I) { unsigned Opc = I->getOpcode(); @@ -268,7 +268,7 @@ bool TypePromotion::isSink(Value *V) { /// Return whether this instruction can safely wrap. bool TypePromotion::isSafeWrap(Instruction *I) { - // We can support a, potentially, wrapping instruction (I) if: + // We can support a potentially wrapping instruction (I) if: // - It is only used by an unsigned icmp. // - The icmp uses a constant. // - The wrapping value (I) is decreasing, i.e would underflow - wrapping @@ -355,7 +355,7 @@ bool TypePromotion::isSafeWrap(Instruction *I) { if (!OverflowConst.isNonPositive()) return false; - // Using C1 = OverflowConst and C2 = ICmpConst, we can use either prove that: + // Using C1 = OverflowConst and C2 = ICmpConst, we can either prove that: // zext(x) + sext(C1) s C2 // zext(x) + sext(C1) Users; + SmallVector Users; Instruction *InstTo = dyn_cast(To); bool ReplacedAll = true; @@ -505,7 +505,7 @@ void IRPromoter::TruncateSinks() { IRBuilder<> Builder{Ctx}; - auto InsertTrunc = [&](Value *V, Type *TruncTy) -> Instruction* { + auto InsertTrunc = [&](Value *V, Type *TruncTy) -> Instruction * { if (!isa(V) || !isa(V->getType())) return nullptr; @@ -513,7 +513,7 @@ void IRPromoter::TruncateSinks() { return nullptr; LLVM_DEBUG(dbgs() << "IR Promotion: Creating " << *TruncTy << " Trunc for " - << *V << "\n"); + << *V << "\n"); Builder.SetInsertPoint(cast(V)); auto *Trunc = dyn_cast(Builder.CreateTrunc(V, TruncTy)); if (Trunc) @@ -575,7 +575,7 @@ void IRPromoter::Cleanup() { Value *Src = ZExt->getOperand(0); if (ZExt->getSrcTy() == ZExt->getDestTy()) { LLVM_DEBUG(dbgs() << "IR Promotion: Removing unnecessary cast: " << *ZExt - << "\n"); + << "\n"); ReplaceAllUsersOfWith(ZExt, Src); continue; } @@ -614,7 +614,7 @@ void IRPromoter::ConvertTruncs() { unsigned NumBits = DestTy->getScalarSizeInBits(); ConstantInt *Mask = - ConstantInt::get(SrcTy, APInt::getMaxValue(NumBits).getZExtValue()); + ConstantInt::get(SrcTy, APInt::getMaxValue(NumBits).getZExtValue()); Value *Masked = Builder.CreateAnd(Trunc->getOperand(0), Mask); if (auto *I = dyn_cast(Masked)) @@ -626,7 +626,8 @@ void IRPromoter::ConvertTruncs() { void IRPromoter::Mutate() { LLVM_DEBUG(dbgs() << "IR Promotion: Promoting use-def chains from " - << OrigTy->getBitWidth() << " to " << PromotedWidth << "-bits\n"); + << OrigTy->getBitWidth() << " to " << PromotedWidth + << "-bits\n"); // Cache original types of the values that will likely need truncating for (auto *I : Sinks) { @@ -676,8 +677,7 @@ bool TypePromotion::isSupportedType(Value *V) { if (Ty->isVoidTy() || Ty->isPointerTy()) return true; - if (!isa(Ty) || - cast(Ty)->getBitWidth() == 1 || + if (!isa(Ty) || cast(Ty)->getBitWidth() == 1 || cast(Ty)->getBitWidth() > RegisterBitWidth) return false; @@ -737,13 +737,12 @@ bool TypePromotion::isSupportedValue(Value *V) { /// smaller than the targeted promoted type. Check that we're not trying to /// promote something larger than our base 'TypeSize' type. bool TypePromotion::isLegalToPromote(Value *V) { - auto *I = dyn_cast(V); if (!I) return true; if (SafeToPromote.count(I)) - return true; + return true; if (isPromotedResultSafe(I) || isSafeWrap(I)) { SafeToPromote.insert(I); @@ -764,10 +763,10 @@ bool TypePromotion::TryToPromote(Value *V, unsigned PromotedWidth) { LLVM_DEBUG(dbgs() << "IR Promotion: TryToPromote: " << *V << ", from " << TypeSize << " bits to " << PromotedWidth << "\n"); - SetVector WorkList; - SetVector Sources; - SetVector Sinks; - SetVector CurrentVisited; + SetVector WorkList; + SetVector Sources; + SetVector Sinks; + SetVector CurrentVisited; WorkList.insert(V); // Return true if V was added to the worklist as a supported instruction, @@ -838,14 +837,15 @@ bool TypePromotion::TryToPromote(Value *V, unsigned PromotedWidth) { } } - LLVM_DEBUG(dbgs() << "IR Promotion: Visited nodes:\n"; - for (auto *I : CurrentVisited) - I->dump(); - ); + LLVM_DEBUG({ + dbgs() << "IR Promotion: Visited nodes:\n"; + for (auto *I : CurrentVisited) + I->dump(); + }); unsigned ToPromote = 0; unsigned NonFreeArgs = 0; - SmallPtrSet Blocks; + SmallPtrSet Blocks; for (auto *V : CurrentVisited) { if (auto *I = dyn_cast(V)) Blocks.insert(I->getParent()); @@ -859,8 +859,8 @@ bool TypePromotion::TryToPromote(Value *V, unsigned PromotedWidth) { if (Sinks.count(cast(V))) continue; - ++ToPromote; - } + ++ToPromote; + } // DAG optimizations should be able to handle these cases better, especially // for function arguments. @@ -892,14 +892,14 @@ bool TypePromotion::runOnFunction(Function &F) { const TargetSubtargetInfo *SubtargetInfo = TM.getSubtargetImpl(F); const TargetLowering *TLI = SubtargetInfo->getTargetLowering(); const TargetTransformInfo &TII = - getAnalysis().getTTI(F); + getAnalysis().getTTI(F); RegisterBitWidth = TII.getRegisterBitWidth(TargetTransformInfo::RGK_Scalar).getFixedSize(); Ctx = &F.getParent()->getContext(); // Search up from icmps to try to promote their operands. for (BasicBlock &BB : F) { - for (auto &I : BB) { + for (Instruction &I : BB) { if (AllVisited.count(&I)) continue; @@ -908,8 +908,7 @@ bool TypePromotion::runOnFunction(Function &F) { auto *ICmp = cast(&I); // Skip signed or pointer compares - if (ICmp->isSigned() || - !isa(ICmp->getOperand(0)->getType())) + if (ICmp->isSigned() || !isa(ICmp->getOperand(0)->getType())) continue; LLVM_DEBUG(dbgs() << "IR Promotion: Searching from: " << *ICmp << "\n"); @@ -920,13 +919,13 @@ bool TypePromotion::runOnFunction(Function &F) { if (SrcVT.isSimple() && TLI->isTypeLegal(SrcVT.getSimpleVT())) break; - if (TLI->getTypeAction(ICmp->getContext(), SrcVT) != + if (TLI->getTypeAction(*Ctx, SrcVT) != TargetLowering::TypePromoteInteger) break; - EVT PromotedVT = TLI->getTypeToTransformTo(ICmp->getContext(), SrcVT); + EVT PromotedVT = TLI->getTypeToTransformTo(*Ctx, SrcVT); if (RegisterBitWidth < PromotedVT.getFixedSizeInBits()) { LLVM_DEBUG(dbgs() << "IR Promotion: Couldn't find target register " - << "for promoted type\n"); + << "for promoted type\n"); break; } @@ -935,13 +934,7 @@ bool TypePromotion::runOnFunction(Function &F) { } } } - LLVM_DEBUG(if (verifyFunction(F, &dbgs())) { - dbgs() << F; - report_fatal_error("Broken function after type promotion"); - }); } - if (MadeChange) - LLVM_DEBUG(dbgs() << "After TypePromotion: " << F << "\n"); AllVisited.clear(); SafeToPromote.clear(); @@ -955,6 +948,4 @@ INITIALIZE_PASS_END(TypePromotion, DEBUG_TYPE, PASS_NAME, false, false) char TypePromotion::ID = 0; -FunctionPass *llvm::createTypePromotionPass() { - return new TypePromotion(); -} +FunctionPass *llvm::createTypePromotionPass() { return new TypePromotion(); } -- 2.7.4