1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "ReduceOperandsToArgs.h"
#include "Delta.h"
#include "Utils.h"
#include "llvm/ADT/Sequence.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/InstIterator.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/IR/Instructions.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include "llvm/Transforms/Utils/Cloning.h"
using namespace llvm;
static bool canReplaceFunction(Function *F) {
return all_of(F->uses(), [](Use &Op) {
if (auto *CI = dyn_cast<CallBase>(Op.getUser()))
return &CI->getCalledOperandUse() == &Op;
return false;
});
}
static bool canReduceUse(Use &Op) {
Value *Val = Op.get();
Type *Ty = Val->getType();
// Only replace operands that can be passed-by-value.
if (!Ty->isFirstClassType())
return false;
// Don't pass labels/metadata as arguments.
if (Ty->isLabelTy() || Ty->isMetadataTy())
return false;
// No need to replace values that are already arguments.
if (isa<Argument>(Val))
return false;
// Do not replace literals.
if (isa<ConstantData>(Val))
return false;
// Do not convert direct function calls to indirect calls.
if (auto *CI = dyn_cast<CallBase>(Op.getUser()))
if (&CI->getCalledOperandUse() == &Op)
return false;
return true;
}
/// Goes over OldF calls and replaces them with a call to NewF.
static void replaceFunctionCalls(Function *OldF, Function *NewF) {
SmallVector<CallBase *> Callers;
for (Use &U : OldF->uses()) {
auto *CI = cast<CallBase>(U.getUser());
assert(&U == &CI->getCalledOperandUse());
assert(CI->getCalledFunction() == OldF);
Callers.push_back(CI);
}
// Call arguments for NewF.
SmallVector<Value *> Args(NewF->arg_size(), nullptr);
// Fill up the additional parameters with default values.
for (auto ArgIdx : llvm::seq<size_t>(OldF->arg_size(), NewF->arg_size())) {
Type *NewArgTy = NewF->getArg(ArgIdx)->getType();
Args[ArgIdx] = getDefaultValue(NewArgTy);
}
for (CallBase *CI : Callers) {
// Preserve the original function arguments.
for (auto Z : zip_first(CI->args(), Args))
std::get<1>(Z) = std::get<0>(Z);
// Also preserve operand bundles.
SmallVector<OperandBundleDef> OperandBundles;
CI->getOperandBundlesAsDefs(OperandBundles);
// Create the new function call.
CallBase *NewCI;
if (auto *II = dyn_cast<InvokeInst>(CI)) {
NewCI = InvokeInst::Create(NewF, cast<InvokeInst>(II)->getNormalDest(),
cast<InvokeInst>(II)->getUnwindDest(), Args,
OperandBundles, CI->getName());
} else {
assert(isa<CallInst>(CI));
NewCI = CallInst::Create(NewF, Args, OperandBundles, CI->getName());
}
NewCI->setCallingConv(NewF->getCallingConv());
// Do the replacement for this use.
if (!CI->use_empty())
CI->replaceAllUsesWith(NewCI);
ReplaceInstWithInst(CI, NewCI);
}
}
/// Add a new function argument to @p F for each use in @OpsToReplace, and
/// replace those operand values with the new function argument.
static void substituteOperandWithArgument(Function *OldF,
ArrayRef<Use *> OpsToReplace) {
if (OpsToReplace.empty())
return;
SetVector<Value *> UniqueValues;
for (Use *Op : OpsToReplace)
UniqueValues.insert(Op->get());
// Determine the new function's signature.
SmallVector<Type *> NewArgTypes;
llvm::append_range(NewArgTypes, OldF->getFunctionType()->params());
size_t ArgOffset = NewArgTypes.size();
for (Value *V : UniqueValues)
NewArgTypes.push_back(V->getType());
FunctionType *FTy =
FunctionType::get(OldF->getFunctionType()->getReturnType(), NewArgTypes,
OldF->getFunctionType()->isVarArg());
// Create the new function...
Function *NewF =
Function::Create(FTy, OldF->getLinkage(), OldF->getAddressSpace(),
OldF->getName(), OldF->getParent());
// In order to preserve function order, we move NewF behind OldF
NewF->removeFromParent();
OldF->getParent()->getFunctionList().insertAfter(OldF->getIterator(), NewF);
// Preserve the parameters of OldF.
ValueToValueMapTy VMap;
for (auto Z : zip_first(OldF->args(), NewF->args())) {
Argument &OldArg = std::get<0>(Z);
Argument &NewArg = std::get<1>(Z);
NewArg.setName(OldArg.getName()); // Copy the name over...
VMap[&OldArg] = &NewArg; // Add mapping to VMap
}
// Adjust the new parameters.
ValueToValueMapTy OldValMap;
for (auto Z : zip_first(UniqueValues, drop_begin(NewF->args(), ArgOffset))) {
Value *OldVal = std::get<0>(Z);
Argument &NewArg = std::get<1>(Z);
NewArg.setName(OldVal->getName());
OldValMap[OldVal] = &NewArg;
}
SmallVector<ReturnInst *, 8> Returns; // Ignore returns cloned.
CloneFunctionInto(NewF, OldF, VMap, CloneFunctionChangeType::LocalChangesOnly,
Returns, "", /*CodeInfo=*/nullptr);
// Replace the actual operands.
for (Use *Op : OpsToReplace) {
Value *NewArg = OldValMap.lookup(Op->get());
auto *NewUser = cast<Instruction>(VMap.lookup(Op->getUser()));
if (PHINode *NewPhi = dyn_cast<PHINode>(NewUser)) {
PHINode *OldPhi = cast<PHINode>(Op->getUser());
BasicBlock *OldBB = OldPhi->getIncomingBlock(*Op);
NewPhi->setIncomingValueForBlock(cast<BasicBlock>(VMap.lookup(OldBB)),
NewArg);
} else
NewUser->setOperand(Op->getOperandNo(), NewArg);
}
// Replace all OldF uses with NewF.
replaceFunctionCalls(OldF, NewF);
// Rename NewF to OldF's name.
std::string FName = OldF->getName().str();
OldF->replaceAllUsesWith(NewF);
OldF->eraseFromParent();
NewF->setName(FName);
}
static void reduceOperandsToArgs(Oracle &O, ReducerWorkItem &WorkItem) {
Module &Program = WorkItem.getModule();
SmallVector<Use *> OperandsToReduce;
for (Function &F : make_early_inc_range(Program.functions())) {
if (!canReplaceFunction(&F))
continue;
OperandsToReduce.clear();
for (Instruction &I : instructions(&F)) {
for (Use &Op : I.operands()) {
if (!canReduceUse(Op))
continue;
if (O.shouldKeep())
continue;
OperandsToReduce.push_back(&Op);
}
}
substituteOperandWithArgument(&F, OperandsToReduce);
}
}
void llvm::reduceOperandsToArgsDeltaPass(TestRunner &Test) {
runDeltaPass(Test, reduceOperandsToArgs,
"Converting operands to function arguments");
}
|