1 //===---------------- BPFAdjustOpt.cpp - Adjust Optimization --------------===//
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
7 //===----------------------------------------------------------------------===//
9 // Adjust optimization to make the code more kernel verifier friendly.
11 //===----------------------------------------------------------------------===//
15 #include "BPFTargetMachine.h"
16 #include "llvm/IR/Instruction.h"
17 #include "llvm/IR/Instructions.h"
18 #include "llvm/IR/IntrinsicsBPF.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/IR/PatternMatch.h"
21 #include "llvm/IR/Type.h"
22 #include "llvm/IR/User.h"
23 #include "llvm/IR/Value.h"
24 #include "llvm/Pass.h"
25 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
27 #define DEBUG_TYPE "bpf-adjust-opt"
30 using namespace llvm::PatternMatch
;
33 DisableBPFserializeICMP("bpf-disable-serialize-icmp", cl::Hidden
,
34 cl::desc("BPF: Disable Serializing ICMP insns."),
37 static cl::opt
<bool> DisableBPFavoidSpeculation(
38 "bpf-disable-avoid-speculation", cl::Hidden
,
39 cl::desc("BPF: Disable Avoiding Speculative Code Motion."),
43 class BPFAdjustOptImpl
{
44 struct PassThroughInfo
{
46 Instruction
*UsedInst
;
48 PassThroughInfo(Instruction
*I
, Instruction
*U
, uint32_t Idx
)
49 : Input(I
), UsedInst(U
), OpIdx(Idx
) {}
53 BPFAdjustOptImpl(Module
*M
) : M(M
) {}
59 SmallVector
<PassThroughInfo
, 16> PassThroughs
;
61 bool adjustICmpToBuiltin();
62 void adjustBasicBlock(BasicBlock
&BB
);
63 bool serializeICMPCrossBB(BasicBlock
&BB
);
64 void adjustInst(Instruction
&I
);
65 bool serializeICMPInBB(Instruction
&I
);
66 bool avoidSpeculation(Instruction
&I
);
67 bool insertPassThrough();
70 } // End anonymous namespace
72 bool BPFAdjustOptImpl::run() {
73 bool Changed
= adjustICmpToBuiltin();
75 for (Function
&F
: *M
)
81 return insertPassThrough() || Changed
;
84 // Commit acabad9ff6bf ("[InstCombine] try to canonicalize icmp with
85 // trunc op into mask and cmp") added a transformation to
86 // convert "(conv)a < power_2_const" to "a & <const>" in certain
87 // cases and bpf kernel verifier has to handle the resulted code
88 // conservatively and this may reject otherwise legitimate program.
89 // Here, we change related icmp code to a builtin which will
90 // be restored to original icmp code later to prevent that
91 // InstCombine transformatin.
92 bool BPFAdjustOptImpl::adjustICmpToBuiltin() {
94 ICmpInst
*ToBeDeleted
= nullptr;
95 for (Function
&F
: *M
)
99 ToBeDeleted
->eraseFromParent();
100 ToBeDeleted
= nullptr;
103 auto *Icmp
= dyn_cast
<ICmpInst
>(&I
);
107 Value
*Op0
= Icmp
->getOperand(0);
108 if (!isa
<TruncInst
>(Op0
))
111 auto ConstOp1
= dyn_cast
<ConstantInt
>(Icmp
->getOperand(1));
115 auto ConstOp1Val
= ConstOp1
->getValue().getZExtValue();
116 auto Op
= Icmp
->getPredicate();
117 if (Op
== ICmpInst::ICMP_ULT
|| Op
== ICmpInst::ICMP_UGE
) {
118 if ((ConstOp1Val
- 1) & ConstOp1Val
)
120 } else if (Op
== ICmpInst::ICMP_ULE
|| Op
== ICmpInst::ICMP_UGT
) {
121 if (ConstOp1Val
& (ConstOp1Val
+ 1))
128 ConstantInt::get(Type::getInt32Ty(BB
.getContext()), Op
);
129 Function
*Fn
= Intrinsic::getDeclaration(
130 M
, Intrinsic::bpf_compare
, {Op0
->getType(), ConstOp1
->getType()});
131 auto *NewInst
= CallInst::Create(Fn
, {Opcode
, Op0
, ConstOp1
});
132 NewInst
->insertBefore(&I
);
133 Icmp
->replaceAllUsesWith(NewInst
);
141 bool BPFAdjustOptImpl::insertPassThrough() {
142 for (auto &Info
: PassThroughs
) {
143 auto *CI
= BPFCoreSharedInfo::insertPassThrough(
144 M
, Info
.UsedInst
->getParent(), Info
.Input
, Info
.UsedInst
);
145 Info
.UsedInst
->setOperand(Info
.OpIdx
, CI
);
148 return !PassThroughs
.empty();
151 // To avoid combining conditionals in the same basic block by
152 // instrcombine optimization.
153 bool BPFAdjustOptImpl::serializeICMPInBB(Instruction
&I
) {
155 // comp1 = icmp <opcode> ...;
156 // comp2 = icmp <opcode> ...;
157 // ... or comp1 comp2 ...
159 // comp1 = icmp <opcode> ...;
160 // comp2 = icmp <opcode> ...;
161 // new_comp1 = __builtin_bpf_passthrough(seq_num, comp1)
162 // ... or new_comp1 comp2 ...
164 // Use LogicalOr (accept `or i1` as well as `select i1 Op0, true, Op1`)
165 if (!match(&I
, m_LogicalOr(m_Value(Op0
), m_Value(Op1
))))
167 auto *Icmp1
= dyn_cast
<ICmpInst
>(Op0
);
170 auto *Icmp2
= dyn_cast
<ICmpInst
>(Op1
);
174 Value
*Icmp1Op0
= Icmp1
->getOperand(0);
175 Value
*Icmp2Op0
= Icmp2
->getOperand(0);
176 if (Icmp1Op0
!= Icmp2Op0
)
179 // Now we got two icmp instructions which feed into
180 // an "or" instruction.
181 PassThroughInfo
Info(Icmp1
, &I
, 0);
182 PassThroughs
.push_back(Info
);
186 // To avoid combining conditionals in the same basic block by
187 // instrcombine optimization.
188 bool BPFAdjustOptImpl::serializeICMPCrossBB(BasicBlock
&BB
) {
191 // comp1 = icmp <opcode> ...;
192 // if (comp1) goto B2 else B3;
194 // comp2 = icmp <opcode> ...;
195 // if (comp2) goto B4 else B5;
200 // comp1 = icmp <opcode> ...;
201 // comp1 = __builtin_bpf_passthrough(seq_num, comp1);
202 // if (comp1) goto B2 else B3;
204 // comp2 = icmp <opcode> ...;
205 // if (comp2) goto B4 else B5;
209 // Check basic predecessors, if two of them (say B1, B2) are using
210 // icmp instructions to generate conditions and one is the predesessor
211 // of another (e.g., B1 is the predecessor of B2). Add a passthrough
212 // barrier after icmp inst of block B1.
213 BasicBlock
*B2
= BB
.getSinglePredecessor();
217 BasicBlock
*B1
= B2
->getSinglePredecessor();
221 Instruction
*TI
= B2
->getTerminator();
222 auto *BI
= dyn_cast
<BranchInst
>(TI
);
223 if (!BI
|| !BI
->isConditional())
225 auto *Cond
= dyn_cast
<ICmpInst
>(BI
->getCondition());
226 if (!Cond
|| B2
->getFirstNonPHI() != Cond
)
228 Value
*B2Op0
= Cond
->getOperand(0);
229 auto Cond2Op
= Cond
->getPredicate();
231 TI
= B1
->getTerminator();
232 BI
= dyn_cast
<BranchInst
>(TI
);
233 if (!BI
|| !BI
->isConditional())
235 Cond
= dyn_cast
<ICmpInst
>(BI
->getCondition());
238 Value
*B1Op0
= Cond
->getOperand(0);
239 auto Cond1Op
= Cond
->getPredicate();
244 if (Cond1Op
== ICmpInst::ICMP_SGT
|| Cond1Op
== ICmpInst::ICMP_SGE
) {
245 if (Cond2Op
!= ICmpInst::ICMP_SLT
&& Cond2Op
!= ICmpInst::ICMP_SLE
)
247 } else if (Cond1Op
== ICmpInst::ICMP_SLT
|| Cond1Op
== ICmpInst::ICMP_SLE
) {
248 if (Cond2Op
!= ICmpInst::ICMP_SGT
&& Cond2Op
!= ICmpInst::ICMP_SGE
)
250 } else if (Cond1Op
== ICmpInst::ICMP_ULT
|| Cond1Op
== ICmpInst::ICMP_ULE
) {
251 if (Cond2Op
!= ICmpInst::ICMP_UGT
&& Cond2Op
!= ICmpInst::ICMP_UGE
)
253 } else if (Cond1Op
== ICmpInst::ICMP_UGT
|| Cond1Op
== ICmpInst::ICMP_UGE
) {
254 if (Cond2Op
!= ICmpInst::ICMP_ULT
&& Cond2Op
!= ICmpInst::ICMP_ULE
)
260 PassThroughInfo
Info(Cond
, BI
, 0);
261 PassThroughs
.push_back(Info
);
266 // To avoid speculative hoisting certain computations out of
268 bool BPFAdjustOptImpl::avoidSpeculation(Instruction
&I
) {
269 if (auto *LdInst
= dyn_cast
<LoadInst
>(&I
)) {
270 if (auto *GV
= dyn_cast
<GlobalVariable
>(LdInst
->getOperand(0))) {
271 if (GV
->hasAttribute(BPFCoreSharedInfo::AmaAttr
) ||
272 GV
->hasAttribute(BPFCoreSharedInfo::TypeIdAttr
))
277 if (!isa
<LoadInst
>(&I
) && !isa
<CallInst
>(&I
))
284 // /* icmp may not be in the same block as var = ... */
285 // comp1 = icmp <opcode> var, <const>;
286 // if (comp1) goto B2 else B3;
293 // /* icmp may not be in the same block as var = ... */
294 // comp1 = icmp <opcode> var, <const>;
295 // if (comp1) goto B2 else B3;
297 // var = __builtin_bpf_passthrough(seq_num, var);
299 bool isCandidate
= false;
300 SmallVector
<PassThroughInfo
, 4> Candidates
;
301 for (User
*U
: I
.users()) {
302 Instruction
*Inst
= dyn_cast
<Instruction
>(U
);
306 // May cover a little bit more than the
308 if (auto *Icmp1
= dyn_cast
<ICmpInst
>(Inst
)) {
309 Value
*Icmp1Op1
= Icmp1
->getOperand(1);
310 if (!isa
<Constant
>(Icmp1Op1
))
316 // Ignore the use in the same basic block as the definition.
317 if (Inst
->getParent() == I
.getParent())
320 // use in a different basic block, If there is a call or
321 // load/store insn before this instruction in this basic
322 // block. Most likely it cannot be hoisted out. Skip it.
323 for (auto &I2
: *Inst
->getParent()) {
324 if (isa
<CallInst
>(&I2
))
326 if (isa
<LoadInst
>(&I2
) || isa
<StoreInst
>(&I2
))
332 // It should be used in a GEP or a simple arithmetic like
333 // ZEXT/SEXT which is used for GEP.
334 if (Inst
->getOpcode() == Instruction::ZExt
||
335 Inst
->getOpcode() == Instruction::SExt
) {
336 PassThroughInfo
Info(&I
, Inst
, 0);
337 Candidates
.push_back(Info
);
338 } else if (auto *GI
= dyn_cast
<GetElementPtrInst
>(Inst
)) {
339 // traverse GEP inst to find Use operand index
341 for (i
= 1, e
= GI
->getNumOperands(); i
!= e
; ++i
) {
342 Value
*V
= GI
->getOperand(i
);
349 PassThroughInfo
Info(&I
, GI
, i
);
350 Candidates
.push_back(Info
);
354 if (!isCandidate
|| Candidates
.empty())
357 llvm::append_range(PassThroughs
, Candidates
);
361 void BPFAdjustOptImpl::adjustBasicBlock(BasicBlock
&BB
) {
362 if (!DisableBPFserializeICMP
&& serializeICMPCrossBB(BB
))
366 void BPFAdjustOptImpl::adjustInst(Instruction
&I
) {
367 if (!DisableBPFserializeICMP
&& serializeICMPInBB(I
))
369 if (!DisableBPFavoidSpeculation
&& avoidSpeculation(I
))
373 PreservedAnalyses
BPFAdjustOptPass::run(Module
&M
, ModuleAnalysisManager
&AM
) {
374 return BPFAdjustOptImpl(&M
).run() ? PreservedAnalyses::none()
375 : PreservedAnalyses::all();