Skip to content

Commit

Permalink
[TypePromotion] Format Type Promotion. NFC
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
davemgreen committed May 11, 2022
1 parent e451d55 commit 764a7f4
Showing 1 changed file with 43 additions and 52 deletions.
95 changes: 43 additions & 52 deletions llvm/lib/CodeGen/TypePromotion.cpp
Expand Up @@ -43,9 +43,9 @@

using namespace llvm;

static cl::opt<bool>
DisablePromotion("disable-type-promotion", cl::Hidden, cl::init(false),
cl::desc("Disable type promotion pass"));
static cl::opt<bool> 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
Expand Down Expand Up @@ -104,15 +104,15 @@ class IRPromoter {
LLVMContext &Ctx;
IntegerType *OrigTy = nullptr;
unsigned PromotedWidth = 0;
SetVector<Value*> &Visited;
SetVector<Value*> &Sources;
SetVector<Instruction*> &Sinks;
SetVector<Value *> &Visited;
SetVector<Value *> &Sources;
SetVector<Instruction *> &Sinks;
SmallPtrSetImpl<Instruction *> &SafeWrap;
IntegerType *ExtTy = nullptr;
SmallPtrSet<Value*, 8> NewInsts;
SmallPtrSet<Instruction*, 4> InstsToRemove;
DenseMap<Value*, SmallVector<Type*, 4>> TruncTysMap;
SmallPtrSet<Value*, 8> Promoted;
SmallPtrSet<Value *, 8> NewInsts;
SmallPtrSet<Instruction *, 4> InstsToRemove;
DenseMap<Value *, SmallVector<Type *, 4>> TruncTysMap;
SmallPtrSet<Value *, 8> Promoted;

void ReplaceAllUsersOfWith(Value *From, Value *To);
void ExtendSources();
Expand Down Expand Up @@ -141,8 +141,8 @@ class TypePromotion : public FunctionPass {
unsigned TypeSize = 0;
LLVMContext *Ctx = nullptr;
unsigned RegisterBitWidth = 0;
SmallPtrSet<Value*, 16> AllVisited;
SmallPtrSet<Instruction*, 8> SafeToPromote;
SmallPtrSet<Value *, 16> AllVisited;
SmallPtrSet<Instruction *, 8> SafeToPromote;
SmallPtrSet<Instruction *, 4> SafeWrap;

// Does V have the same size result type as TypeSize.
Expand Down Expand Up @@ -189,7 +189,7 @@ class TypePromotion : public FunctionPass {
bool runOnFunction(Function &F) override;
};

}
} // namespace

static bool GenerateSignBits(Instruction *I) {
unsigned Opc = I->getOpcode();
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) <u zext(C2) if C1 < 0 and C1 >s C2
// zext(x) + sext(C1) <u sext(C2) if C1 < 0 and C1 <=s C2
if (OverflowConst.sgt(ICmpConst)) {
Expand Down Expand Up @@ -403,7 +403,7 @@ static bool isPromotedResultSafe(Instruction *I) {
}

void IRPromoter::ReplaceAllUsersOfWith(Value *From, Value *To) {
SmallVector<Instruction*, 4> Users;
SmallVector<Instruction *, 4> Users;
Instruction *InstTo = dyn_cast<Instruction>(To);
bool ReplacedAll = true;

Expand Down Expand Up @@ -505,15 +505,15 @@ void IRPromoter::TruncateSinks() {

IRBuilder<> Builder{Ctx};

auto InsertTrunc = [&](Value *V, Type *TruncTy) -> Instruction* {
auto InsertTrunc = [&](Value *V, Type *TruncTy) -> Instruction * {
if (!isa<Instruction>(V) || !isa<IntegerType>(V->getType()))
return nullptr;

if ((!Promoted.count(V) && !NewInsts.count(V)) || Sources.count(V))
return nullptr;

LLVM_DEBUG(dbgs() << "IR Promotion: Creating " << *TruncTy << " Trunc for "
<< *V << "\n");
<< *V << "\n");
Builder.SetInsertPoint(cast<Instruction>(V));
auto *Trunc = dyn_cast<Instruction>(Builder.CreateTrunc(V, TruncTy));
if (Trunc)
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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<Instruction>(Masked))
Expand All @@ -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) {
Expand Down Expand Up @@ -676,8 +677,7 @@ bool TypePromotion::isSupportedType(Value *V) {
if (Ty->isVoidTy() || Ty->isPointerTy())
return true;

if (!isa<IntegerType>(Ty) ||
cast<IntegerType>(Ty)->getBitWidth() == 1 ||
if (!isa<IntegerType>(Ty) || cast<IntegerType>(Ty)->getBitWidth() == 1 ||
cast<IntegerType>(Ty)->getBitWidth() > RegisterBitWidth)
return false;

Expand Down Expand Up @@ -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<Instruction>(V);
if (!I)
return true;

if (SafeToPromote.count(I))
return true;
return true;

if (isPromotedResultSafe(I) || isSafeWrap(I)) {
SafeToPromote.insert(I);
Expand All @@ -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<Value*> WorkList;
SetVector<Value*> Sources;
SetVector<Instruction*> Sinks;
SetVector<Value*> CurrentVisited;
SetVector<Value *> WorkList;
SetVector<Value *> Sources;
SetVector<Instruction *> Sinks;
SetVector<Value *> CurrentVisited;
WorkList.insert(V);

// Return true if V was added to the worklist as a supported instruction,
Expand Down Expand Up @@ -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<BasicBlock*, 4> Blocks;
SmallPtrSet<BasicBlock *, 4> Blocks;
for (auto *V : CurrentVisited) {
if (auto *I = dyn_cast<Instruction>(V))
Blocks.insert(I->getParent());
Expand All @@ -859,8 +859,8 @@ bool TypePromotion::TryToPromote(Value *V, unsigned PromotedWidth) {

if (Sinks.count(cast<Instruction>(V)))
continue;
++ToPromote;
}
++ToPromote;
}

// DAG optimizations should be able to handle these cases better, especially
// for function arguments.
Expand Down Expand Up @@ -892,14 +892,14 @@ bool TypePromotion::runOnFunction(Function &F) {
const TargetSubtargetInfo *SubtargetInfo = TM.getSubtargetImpl(F);
const TargetLowering *TLI = SubtargetInfo->getTargetLowering();
const TargetTransformInfo &TII =
getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
getAnalysis<TargetTransformInfoWrapperPass>().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;

Expand All @@ -908,8 +908,7 @@ bool TypePromotion::runOnFunction(Function &F) {

auto *ICmp = cast<ICmpInst>(&I);
// Skip signed or pointer compares
if (ICmp->isSigned() ||
!isa<IntegerType>(ICmp->getOperand(0)->getType()))
if (ICmp->isSigned() || !isa<IntegerType>(ICmp->getOperand(0)->getType()))
continue;

LLVM_DEBUG(dbgs() << "IR Promotion: Searching from: " << *ICmp << "\n");
Expand All @@ -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;
}

Expand All @@ -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();
Expand All @@ -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(); }

0 comments on commit 764a7f4

Please sign in to comment.