|
| 1 | +//===- KillUB.cpp - remove all UB flags -----------------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +// |
| 9 | +// This file is a part of Fil-C. https://fil-c.org/ |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "llvm/Transforms/Instrumentation/KillUB.h" |
| 14 | + |
| 15 | +#include <llvm/IR/AttributeMask.h> |
| 16 | +#include <llvm/IR/InlineAsm.h> |
| 17 | +#include <llvm/IR/Module.h> |
| 18 | +#include <llvm/IR/Operator.h> |
| 19 | + |
| 20 | +using namespace llvm; |
| 21 | + |
| 22 | +PreservedAnalyses KillUBPass::run(Module &M, ModuleAnalysisManager&) { |
| 23 | + // This is just the beginning! There are probably more UB killings that have to happen. |
| 24 | + |
| 25 | + AttributeMask AM; |
| 26 | + AM.addAttribute(Attribute::AllocSize); |
| 27 | + AM.addAttribute(Attribute::NoUndef); |
| 28 | + AM.addAttribute(Attribute::Convergent); |
| 29 | + AM.addAttribute(Attribute::Dereferenceable); |
| 30 | + AM.addAttribute(Attribute::DereferenceableOrNull); |
| 31 | + AM.addAttribute(Attribute::Memory); |
| 32 | + AM.addAttribute(Attribute::NoAlias); |
| 33 | + AM.addAttribute(Attribute::NoCallback); |
| 34 | + AM.addAttribute(Attribute::NoCapture); |
| 35 | + AM.addAttribute(Attribute::Captures); |
| 36 | + AM.addAttribute(Attribute::NoDivergenceSource); |
| 37 | + AM.addAttribute(Attribute::NoExt); |
| 38 | + AM.addAttribute(Attribute::NoFree); |
| 39 | + AM.addAttribute(Attribute::DeadOnUnwind); |
| 40 | + AM.addAttribute(Attribute::NonNull); |
| 41 | + AM.addAttribute(Attribute::NoRecurse); |
| 42 | + AM.addAttribute(Attribute::NoRedZone); |
| 43 | + AM.addAttribute(Attribute::NoReturn); |
| 44 | + AM.addAttribute(Attribute::NoSync); |
| 45 | + AM.addAttribute(Attribute::Range); |
| 46 | + AM.addAttribute(Attribute::ReadNone); |
| 47 | + AM.addAttribute(Attribute::ReadOnly); |
| 48 | + AM.addAttribute(Attribute::Returned); |
| 49 | + AM.addAttribute(Attribute::Returned); |
| 50 | + AM.addAttribute(Attribute::WillReturn); |
| 51 | + AM.addAttribute(Attribute::Writable); |
| 52 | + AM.addAttribute(Attribute::WriteOnly); |
| 53 | + AM.addAttribute(Attribute::MustProgress); |
| 54 | + |
| 55 | + for (Function& F : M) { |
| 56 | + if (F.isIntrinsic()) { |
| 57 | + assert(F.isDeclaration()); |
| 58 | + continue; |
| 59 | + } |
| 60 | + F.removeFnAttrs(AM); |
| 61 | + F.removeRetAttrs(AM); |
| 62 | + for (size_t Idx = F.arg_size(); Idx--;) |
| 63 | + F.removeParamAttrs(Idx, AM); |
| 64 | + for (BasicBlock& BB : F) { |
| 65 | + for (Instruction& I : BB) { |
| 66 | + I.dropUnknownNonDebugMetadata(); |
| 67 | + I.dropPoisonGeneratingAnnotations(); |
| 68 | + I.dropUBImplyingAttrsAndUnknownMetadata(); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + return PreservedAnalyses::none(); |
| 74 | +} |
| 75 | + |
0 commit comments