blob: 3c9a35986be21f7ddad4b2f03fc55a14631d8c26 [file] [log] [blame]
Justin Bogner7d449d32017-08-21 22:57:06 +00001//===-- RandomIRBuilder.cpp -----------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/FuzzMutate/RandomIRBuilder.h"
11#include "llvm/ADT/STLExtras.h"
12#include "llvm/FuzzMutate/Random.h"
13#include "llvm/IR/BasicBlock.h"
14#include "llvm/IR/Constants.h"
15#include "llvm/IR/Function.h"
16#include "llvm/IR/Instructions.h"
17#include "llvm/IR/IntrinsicInst.h"
18#include "llvm/IR/Module.h"
19
20using namespace llvm;
21using namespace fuzzerop;
22
23Value *RandomIRBuilder::findOrCreateSource(BasicBlock &BB,
24 ArrayRef<Instruction *> Insts) {
25 return findOrCreateSource(BB, Insts, {}, anyType());
26}
27
28Value *RandomIRBuilder::findOrCreateSource(BasicBlock &BB,
29 ArrayRef<Instruction *> Insts,
30 ArrayRef<Value *> Srcs,
31 SourcePred Pred) {
32 auto MatchesPred = [&Srcs, &Pred](Instruction *Inst) {
33 return Pred.matches(Srcs, Inst);
34 };
35 auto RS = makeSampler(Rand, make_filter_range(Insts, MatchesPred));
36 // Also consider choosing no source, meaning we want a new one.
37 RS.sample(nullptr, /*Weight=*/1);
38 if (Instruction *Src = RS.getSelection())
39 return Src;
40 return newSource(BB, Insts, Srcs, Pred);
41}
42
43Value *RandomIRBuilder::newSource(BasicBlock &BB, ArrayRef<Instruction *> Insts,
44 ArrayRef<Value *> Srcs, SourcePred Pred) {
45 // Generate some constants to choose from.
46 auto RS = makeSampler<Value *>(Rand);
47 RS.sample(Pred.generate(Srcs, KnownTypes));
Justin Bogner7d449d32017-08-21 22:57:06 +000048
49 // If we can find a pointer to load from, use it half the time.
50 Value *Ptr = findPointer(BB, Insts, Srcs, Pred);
Igor Laevskyfaacdf82017-11-30 15:24:41 +000051 if (Ptr) {
52 // Create load from the chosen pointer
53 auto IP = BB.getFirstInsertionPt();
54 if (auto *I = dyn_cast<Instruction>(Ptr))
55 IP = ++I->getIterator();
56 auto *NewLoad = new LoadInst(Ptr, "L", &*IP);
Justin Bogner7d449d32017-08-21 22:57:06 +000057
Igor Laevskyfaacdf82017-11-30 15:24:41 +000058 // Only sample this load if it really matches the descriptor
59 if (Pred.matches(Srcs, NewLoad))
60 RS.sample(NewLoad, RS.totalWeight());
61 else
62 NewLoad->eraseFromParent();
63 }
Justin Bogner7d449d32017-08-21 22:57:06 +000064
Igor Laevskyfaacdf82017-11-30 15:24:41 +000065 assert(!RS.isEmpty() && "Failed to generate sources");
66 return RS.getSelection();
Justin Bogner7d449d32017-08-21 22:57:06 +000067}
68
69static bool isCompatibleReplacement(const Instruction *I, const Use &Operand,
70 const Value *Replacement) {
71 if (Operand->getType() != Replacement->getType())
72 return false;
73 switch (I->getOpcode()) {
74 case Instruction::GetElementPtr:
75 case Instruction::ExtractElement:
76 case Instruction::ExtractValue:
77 // TODO: We could potentially validate these, but for now just leave indices
78 // alone.
79 if (Operand.getOperandNo() > 1)
80 return false;
81 break;
82 case Instruction::InsertValue:
83 case Instruction::InsertElement:
84 if (Operand.getOperandNo() > 2)
85 return false;
86 break;
87 default:
88 break;
89 }
90 return true;
91}
92
93void RandomIRBuilder::connectToSink(BasicBlock &BB,
94 ArrayRef<Instruction *> Insts, Value *V) {
95 auto RS = makeSampler<Use *>(Rand);
96 for (auto &I : Insts) {
97 if (isa<IntrinsicInst>(I))
98 // TODO: Replacing operands of intrinsics would be interesting, but
99 // there's no easy way to verify that a given replacement is valid given
100 // that intrinsics can impose arbitrary constraints.
101 continue;
102 for (Use &U : I->operands())
103 if (isCompatibleReplacement(I, U, V))
104 RS.sample(&U, 1);
105 }
106 // Also consider choosing no sink, meaning we want a new one.
107 RS.sample(nullptr, /*Weight=*/1);
108
109 if (Use *Sink = RS.getSelection()) {
110 User *U = Sink->getUser();
111 unsigned OpNo = Sink->getOperandNo();
112 U->setOperand(OpNo, V);
113 return;
114 }
115 newSink(BB, Insts, V);
116}
117
118void RandomIRBuilder::newSink(BasicBlock &BB, ArrayRef<Instruction *> Insts,
119 Value *V) {
120 Value *Ptr = findPointer(BB, Insts, {V}, matchFirstType());
121 if (!Ptr) {
122 if (uniform(Rand, 0, 1))
123 Ptr = new AllocaInst(V->getType(), 0, "A", &*BB.getFirstInsertionPt());
124 else
125 Ptr = UndefValue::get(PointerType::get(V->getType(), 0));
126 }
127
128 new StoreInst(V, Ptr, Insts.back());
129}
130
131Value *RandomIRBuilder::findPointer(BasicBlock &BB,
132 ArrayRef<Instruction *> Insts,
133 ArrayRef<Value *> Srcs, SourcePred Pred) {
134 auto IsMatchingPtr = [&Srcs, &Pred](Instruction *Inst) {
135 if (auto PtrTy = dyn_cast<PointerType>(Inst->getType()))
136 // TODO: Check if this is horribly expensive.
137 return Pred.matches(Srcs, UndefValue::get(PtrTy->getElementType()));
138 return false;
139 };
140 if (auto RS = makeSampler(Rand, make_filter_range(Insts, IsMatchingPtr)))
141 return RS.getSelection();
142 return nullptr;
143}