Coverage Report

Created: 2024-01-17 10:31

/src/llvm-project/llvm/lib/Transforms/Utils/CodeMoverUtils.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- CodeMoverUtils.cpp - CodeMover Utilities ----------------------------==//
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 family of functions perform movements on basic blocks, and instructions
10
// contained within a function.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "llvm/Transforms/Utils/CodeMoverUtils.h"
15
#include "llvm/ADT/Statistic.h"
16
#include "llvm/Analysis/DependenceAnalysis.h"
17
#include "llvm/Analysis/PostDominators.h"
18
#include "llvm/Analysis/ValueTracking.h"
19
#include "llvm/IR/Dominators.h"
20
21
using namespace llvm;
22
23
#define DEBUG_TYPE "codemover-utils"
24
25
STATISTIC(HasDependences,
26
          "Cannot move across instructions that has memory dependences");
27
STATISTIC(MayThrowException, "Cannot move across instructions that may throw");
28
STATISTIC(NotControlFlowEquivalent,
29
          "Instructions are not control flow equivalent");
30
STATISTIC(NotMovedPHINode, "Movement of PHINodes are not supported");
31
STATISTIC(NotMovedTerminator, "Movement of Terminator are not supported");
32
33
namespace {
34
/// Represent a control condition. A control condition is a condition of a
35
/// terminator to decide which successors to execute. The pointer field
36
/// represents the address of the condition of the terminator. The integer field
37
/// is a bool, it is true when the basic block is executed when V is true. For
38
/// example, `br %cond, bb0, bb1` %cond is a control condition of bb0 with the
39
/// integer field equals to true, while %cond is a control condition of bb1 with
40
/// the integer field equals to false.
41
using ControlCondition = PointerIntPair<Value *, 1, bool>;
42
#ifndef NDEBUG
43
0
raw_ostream &operator<<(raw_ostream &OS, const ControlCondition &C) {
44
0
  OS << "[" << *C.getPointer() << ", " << (C.getInt() ? "true" : "false")
45
0
     << "]";
46
0
  return OS;
47
0
}
48
#endif
49
50
/// Represent a set of control conditions required to execute ToBB from FromBB.
51
class ControlConditions {
52
  using ConditionVectorTy = SmallVector<ControlCondition, 6>;
53
54
  /// A SmallVector of control conditions.
55
  ConditionVectorTy Conditions;
56
57
public:
58
  /// Return a ControlConditions which stores all conditions required to execute
59
  /// \p BB from \p Dominator. If \p MaxLookup is non-zero, it limits the
60
  /// number of conditions to collect. Return std::nullopt if not all conditions
61
  /// are collected successfully, or we hit the limit.
62
  static const std::optional<ControlConditions>
63
  collectControlConditions(const BasicBlock &BB, const BasicBlock &Dominator,
64
                           const DominatorTree &DT,
65
                           const PostDominatorTree &PDT,
66
                           unsigned MaxLookup = 6);
67
68
  /// Return true if there exists no control conditions required to execute ToBB
69
  /// from FromBB.
70
0
  bool isUnconditional() const { return Conditions.empty(); }
71
72
  /// Return a constant reference of Conditions.
73
0
  const ConditionVectorTy &getControlConditions() const { return Conditions; }
74
75
  /// Add \p V as one of the ControlCondition in Condition with IsTrueCondition
76
  /// equals to \p True. Return true if inserted successfully.
77
  bool addControlCondition(ControlCondition C);
78
79
  /// Return true if for all control conditions in Conditions, there exists an
80
  /// equivalent control condition in \p Other.Conditions.
81
  bool isEquivalent(const ControlConditions &Other) const;
82
83
  /// Return true if \p C1 and \p C2 are equivalent.
84
  static bool isEquivalent(const ControlCondition &C1,
85
                           const ControlCondition &C2);
86
87
private:
88
0
  ControlConditions() = default;
89
90
  static bool isEquivalent(const Value &V1, const Value &V2);
91
  static bool isInverse(const Value &V1, const Value &V2);
92
};
93
} // namespace
94
95
static bool domTreeLevelBefore(DominatorTree *DT, const Instruction *InstA,
96
0
                               const Instruction *InstB) {
97
  // Use ordered basic block in case the 2 instructions are in the same
98
  // block.
99
0
  if (InstA->getParent() == InstB->getParent())
100
0
    return InstA->comesBefore(InstB);
101
102
0
  DomTreeNode *DA = DT->getNode(InstA->getParent());
103
0
  DomTreeNode *DB = DT->getNode(InstB->getParent());
104
0
  return DA->getLevel() < DB->getLevel();
105
0
}
106
107
const std::optional<ControlConditions>
108
ControlConditions::collectControlConditions(const BasicBlock &BB,
109
                                            const BasicBlock &Dominator,
110
                                            const DominatorTree &DT,
111
                                            const PostDominatorTree &PDT,
112
0
                                            unsigned MaxLookup) {
113
0
  assert(DT.dominates(&Dominator, &BB) && "Expecting Dominator to dominate BB");
114
115
0
  ControlConditions Conditions;
116
0
  unsigned NumConditions = 0;
117
118
  // BB is executed unconditional from itself.
119
0
  if (&Dominator == &BB)
120
0
    return Conditions;
121
122
0
  const BasicBlock *CurBlock = &BB;
123
  // Walk up the dominator tree from the associated DT node for BB to the
124
  // associated DT node for Dominator.
125
0
  do {
126
0
    assert(DT.getNode(CurBlock) && "Expecting a valid DT node for CurBlock");
127
0
    BasicBlock *IDom = DT.getNode(CurBlock)->getIDom()->getBlock();
128
0
    assert(DT.dominates(&Dominator, IDom) &&
129
0
           "Expecting Dominator to dominate IDom");
130
131
    // Limitation: can only handle branch instruction currently.
132
0
    const BranchInst *BI = dyn_cast<BranchInst>(IDom->getTerminator());
133
0
    if (!BI)
134
0
      return std::nullopt;
135
136
0
    bool Inserted = false;
137
0
    if (PDT.dominates(CurBlock, IDom)) {
138
0
      LLVM_DEBUG(dbgs() << CurBlock->getName()
139
0
                        << " is executed unconditionally from "
140
0
                        << IDom->getName() << "\n");
141
0
    } else if (PDT.dominates(CurBlock, BI->getSuccessor(0))) {
142
0
      LLVM_DEBUG(dbgs() << CurBlock->getName() << " is executed when \""
143
0
                        << *BI->getCondition() << "\" is true from "
144
0
                        << IDom->getName() << "\n");
145
0
      Inserted = Conditions.addControlCondition(
146
0
          ControlCondition(BI->getCondition(), true));
147
0
    } else if (PDT.dominates(CurBlock, BI->getSuccessor(1))) {
148
0
      LLVM_DEBUG(dbgs() << CurBlock->getName() << " is executed when \""
149
0
                        << *BI->getCondition() << "\" is false from "
150
0
                        << IDom->getName() << "\n");
151
0
      Inserted = Conditions.addControlCondition(
152
0
          ControlCondition(BI->getCondition(), false));
153
0
    } else
154
0
      return std::nullopt;
155
156
0
    if (Inserted)
157
0
      ++NumConditions;
158
159
0
    if (MaxLookup != 0 && NumConditions > MaxLookup)
160
0
      return std::nullopt;
161
162
0
    CurBlock = IDom;
163
0
  } while (CurBlock != &Dominator);
164
165
0
  return Conditions;
166
0
}
167
168
0
bool ControlConditions::addControlCondition(ControlCondition C) {
169
0
  bool Inserted = false;
170
0
  if (none_of(Conditions, [&](ControlCondition &Exists) {
171
0
        return ControlConditions::isEquivalent(C, Exists);
172
0
      })) {
173
0
    Conditions.push_back(C);
174
0
    Inserted = true;
175
0
  }
176
177
0
  LLVM_DEBUG(dbgs() << (Inserted ? "Inserted " : "Not inserted ") << C << "\n");
178
0
  return Inserted;
179
0
}
180
181
0
bool ControlConditions::isEquivalent(const ControlConditions &Other) const {
182
0
  if (Conditions.empty() && Other.Conditions.empty())
183
0
    return true;
184
185
0
  if (Conditions.size() != Other.Conditions.size())
186
0
    return false;
187
188
0
  return all_of(Conditions, [&](const ControlCondition &C) {
189
0
    return any_of(Other.Conditions, [&](const ControlCondition &OtherC) {
190
0
      return ControlConditions::isEquivalent(C, OtherC);
191
0
    });
192
0
  });
193
0
}
194
195
bool ControlConditions::isEquivalent(const ControlCondition &C1,
196
0
                                     const ControlCondition &C2) {
197
0
  if (C1.getInt() == C2.getInt()) {
198
0
    if (isEquivalent(*C1.getPointer(), *C2.getPointer()))
199
0
      return true;
200
0
  } else if (isInverse(*C1.getPointer(), *C2.getPointer()))
201
0
    return true;
202
203
0
  return false;
204
0
}
205
206
// FIXME: Use SCEV and reuse GVN/CSE logic to check for equivalence between
207
// Values.
208
// Currently, isEquivalent rely on other passes to ensure equivalent conditions
209
// have the same value, e.g. GVN.
210
0
bool ControlConditions::isEquivalent(const Value &V1, const Value &V2) {
211
0
  return &V1 == &V2;
212
0
}
213
214
0
bool ControlConditions::isInverse(const Value &V1, const Value &V2) {
215
0
  if (const CmpInst *Cmp1 = dyn_cast<CmpInst>(&V1))
216
0
    if (const CmpInst *Cmp2 = dyn_cast<CmpInst>(&V2)) {
217
0
      if (Cmp1->getPredicate() == Cmp2->getInversePredicate() &&
218
0
          Cmp1->getOperand(0) == Cmp2->getOperand(0) &&
219
0
          Cmp1->getOperand(1) == Cmp2->getOperand(1))
220
0
        return true;
221
222
0
      if (Cmp1->getPredicate() ==
223
0
              CmpInst::getSwappedPredicate(Cmp2->getInversePredicate()) &&
224
0
          Cmp1->getOperand(0) == Cmp2->getOperand(1) &&
225
0
          Cmp1->getOperand(1) == Cmp2->getOperand(0))
226
0
        return true;
227
0
    }
228
0
  return false;
229
0
}
230
231
bool llvm::isControlFlowEquivalent(const Instruction &I0, const Instruction &I1,
232
                                   const DominatorTree &DT,
233
0
                                   const PostDominatorTree &PDT) {
234
0
  return isControlFlowEquivalent(*I0.getParent(), *I1.getParent(), DT, PDT);
235
0
}
236
237
bool llvm::isControlFlowEquivalent(const BasicBlock &BB0, const BasicBlock &BB1,
238
                                   const DominatorTree &DT,
239
0
                                   const PostDominatorTree &PDT) {
240
0
  if (&BB0 == &BB1)
241
0
    return true;
242
243
0
  if ((DT.dominates(&BB0, &BB1) && PDT.dominates(&BB1, &BB0)) ||
244
0
      (PDT.dominates(&BB0, &BB1) && DT.dominates(&BB1, &BB0)))
245
0
    return true;
246
247
  // If the set of conditions required to execute BB0 and BB1 from their common
248
  // dominator are the same, then BB0 and BB1 are control flow equivalent.
249
0
  const BasicBlock *CommonDominator = DT.findNearestCommonDominator(&BB0, &BB1);
250
0
  LLVM_DEBUG(dbgs() << "The nearest common dominator of " << BB0.getName()
251
0
                    << " and " << BB1.getName() << " is "
252
0
                    << CommonDominator->getName() << "\n");
253
254
0
  const std::optional<ControlConditions> BB0Conditions =
255
0
      ControlConditions::collectControlConditions(BB0, *CommonDominator, DT,
256
0
                                                  PDT);
257
0
  if (BB0Conditions == std::nullopt)
258
0
    return false;
259
260
0
  const std::optional<ControlConditions> BB1Conditions =
261
0
      ControlConditions::collectControlConditions(BB1, *CommonDominator, DT,
262
0
                                                  PDT);
263
0
  if (BB1Conditions == std::nullopt)
264
0
    return false;
265
266
0
  return BB0Conditions->isEquivalent(*BB1Conditions);
267
0
}
268
269
static bool reportInvalidCandidate(const Instruction &I,
270
0
                                   llvm::Statistic &Stat) {
271
0
  ++Stat;
272
0
  LLVM_DEBUG(dbgs() << "Unable to move instruction: " << I << ". "
273
0
                    << Stat.getDesc());
274
0
  return false;
275
0
}
276
277
/// Collect all instructions in between \p StartInst and \p EndInst, and store
278
/// them in \p InBetweenInsts.
279
static void
280
collectInstructionsInBetween(Instruction &StartInst, const Instruction &EndInst,
281
0
                             SmallPtrSetImpl<Instruction *> &InBetweenInsts) {
282
0
  assert(InBetweenInsts.empty() && "Expecting InBetweenInsts to be empty");
283
284
  /// Get the next instructions of \p I, and push them to \p WorkList.
285
0
  auto getNextInsts = [](Instruction &I,
286
0
                         SmallPtrSetImpl<Instruction *> &WorkList) {
287
0
    if (Instruction *NextInst = I.getNextNode())
288
0
      WorkList.insert(NextInst);
289
0
    else {
290
0
      assert(I.isTerminator() && "Expecting a terminator instruction");
291
0
      for (BasicBlock *Succ : successors(&I))
292
0
        WorkList.insert(&Succ->front());
293
0
    }
294
0
  };
295
296
0
  SmallPtrSet<Instruction *, 10> WorkList;
297
0
  getNextInsts(StartInst, WorkList);
298
0
  while (!WorkList.empty()) {
299
0
    Instruction *CurInst = *WorkList.begin();
300
0
    WorkList.erase(CurInst);
301
302
0
    if (CurInst == &EndInst)
303
0
      continue;
304
305
0
    if (!InBetweenInsts.insert(CurInst).second)
306
0
      continue;
307
308
0
    getNextInsts(*CurInst, WorkList);
309
0
  }
310
0
}
311
312
bool llvm::isSafeToMoveBefore(Instruction &I, Instruction &InsertPoint,
313
                              DominatorTree &DT, const PostDominatorTree *PDT,
314
0
                              DependenceInfo *DI, bool CheckForEntireBlock) {
315
  // Skip tests when we don't have PDT or DI
316
0
  if (!PDT || !DI)
317
0
    return false;
318
319
  // Cannot move itself before itself.
320
0
  if (&I == &InsertPoint)
321
0
    return false;
322
323
  // Not moved.
324
0
  if (I.getNextNode() == &InsertPoint)
325
0
    return true;
326
327
0
  if (isa<PHINode>(I) || isa<PHINode>(InsertPoint))
328
0
    return reportInvalidCandidate(I, NotMovedPHINode);
329
330
0
  if (I.isTerminator())
331
0
    return reportInvalidCandidate(I, NotMovedTerminator);
332
333
  // TODO remove this limitation.
334
0
  if (!isControlFlowEquivalent(I, InsertPoint, DT, *PDT))
335
0
    return reportInvalidCandidate(I, NotControlFlowEquivalent);
336
337
0
  if (isReachedBefore(&I, &InsertPoint, &DT, PDT))
338
0
    for (const Use &U : I.uses())
339
0
      if (auto *UserInst = dyn_cast<Instruction>(U.getUser()))
340
0
        if (UserInst != &InsertPoint && !DT.dominates(&InsertPoint, U))
341
0
          return false;
342
0
  if (isReachedBefore(&InsertPoint, &I, &DT, PDT))
343
0
    for (const Value *Op : I.operands())
344
0
      if (auto *OpInst = dyn_cast<Instruction>(Op)) {
345
0
        if (&InsertPoint == OpInst)
346
0
          return false;
347
        // If OpInst is an instruction that appears earlier in the same BB as
348
        // I, then it is okay to move since OpInst will still be available.
349
0
        if (CheckForEntireBlock && I.getParent() == OpInst->getParent() &&
350
0
            DT.dominates(OpInst, &I))
351
0
          continue;
352
0
        if (!DT.dominates(OpInst, &InsertPoint))
353
0
          return false;
354
0
      }
355
356
0
  DT.updateDFSNumbers();
357
0
  const bool MoveForward = domTreeLevelBefore(&DT, &I, &InsertPoint);
358
0
  Instruction &StartInst = (MoveForward ? I : InsertPoint);
359
0
  Instruction &EndInst = (MoveForward ? InsertPoint : I);
360
0
  SmallPtrSet<Instruction *, 10> InstsToCheck;
361
0
  collectInstructionsInBetween(StartInst, EndInst, InstsToCheck);
362
0
  if (!MoveForward)
363
0
    InstsToCheck.insert(&InsertPoint);
364
365
  // Check if there exists instructions which may throw, may synchonize, or may
366
  // never return, from I to InsertPoint.
367
0
  if (!isSafeToSpeculativelyExecute(&I))
368
0
    if (llvm::any_of(InstsToCheck, [](Instruction *I) {
369
0
          if (I->mayThrow())
370
0
            return true;
371
372
0
          const CallBase *CB = dyn_cast<CallBase>(I);
373
0
          if (!CB)
374
0
            return false;
375
0
          if (!CB->hasFnAttr(Attribute::WillReturn))
376
0
            return true;
377
0
          if (!CB->hasFnAttr(Attribute::NoSync))
378
0
            return true;
379
380
0
          return false;
381
0
        })) {
382
0
      return reportInvalidCandidate(I, MayThrowException);
383
0
    }
384
385
  // Check if I has any output/flow/anti dependences with instructions from \p
386
  // StartInst to \p EndInst.
387
0
  if (llvm::any_of(InstsToCheck, [&DI, &I](Instruction *CurInst) {
388
0
        auto DepResult = DI->depends(&I, CurInst, true);
389
0
        if (DepResult && (DepResult->isOutput() || DepResult->isFlow() ||
390
0
                          DepResult->isAnti()))
391
0
          return true;
392
0
        return false;
393
0
      }))
394
0
    return reportInvalidCandidate(I, HasDependences);
395
396
0
  return true;
397
0
}
398
399
bool llvm::isSafeToMoveBefore(BasicBlock &BB, Instruction &InsertPoint,
400
                              DominatorTree &DT, const PostDominatorTree *PDT,
401
0
                              DependenceInfo *DI) {
402
0
  return llvm::all_of(BB, [&](Instruction &I) {
403
0
    if (BB.getTerminator() == &I)
404
0
      return true;
405
406
0
    return isSafeToMoveBefore(I, InsertPoint, DT, PDT, DI,
407
0
                              /*CheckForEntireBlock=*/true);
408
0
  });
409
0
}
410
411
void llvm::moveInstructionsToTheBeginning(BasicBlock &FromBB, BasicBlock &ToBB,
412
                                          DominatorTree &DT,
413
                                          const PostDominatorTree &PDT,
414
0
                                          DependenceInfo &DI) {
415
0
  for (Instruction &I :
416
0
       llvm::make_early_inc_range(llvm::drop_begin(llvm::reverse(FromBB)))) {
417
0
    Instruction *MovePos = ToBB.getFirstNonPHIOrDbg();
418
419
0
    if (isSafeToMoveBefore(I, *MovePos, DT, &PDT, &DI))
420
0
      I.moveBeforePreserving(MovePos);
421
0
  }
422
0
}
423
424
void llvm::moveInstructionsToTheEnd(BasicBlock &FromBB, BasicBlock &ToBB,
425
                                    DominatorTree &DT,
426
                                    const PostDominatorTree &PDT,
427
0
                                    DependenceInfo &DI) {
428
0
  Instruction *MovePos = ToBB.getTerminator();
429
0
  while (FromBB.size() > 1) {
430
0
    Instruction &I = FromBB.front();
431
0
    if (isSafeToMoveBefore(I, *MovePos, DT, &PDT, &DI))
432
0
      I.moveBeforePreserving(MovePos);
433
0
  }
434
0
}
435
436
bool llvm::nonStrictlyPostDominate(const BasicBlock *ThisBlock,
437
                                   const BasicBlock *OtherBlock,
438
                                   const DominatorTree *DT,
439
0
                                   const PostDominatorTree *PDT) {
440
0
  assert(isControlFlowEquivalent(*ThisBlock, *OtherBlock, *DT, *PDT) &&
441
0
         "ThisBlock and OtherBlock must be CFG equivalent!");
442
0
  const BasicBlock *CommonDominator =
443
0
      DT->findNearestCommonDominator(ThisBlock, OtherBlock);
444
0
  if (CommonDominator == nullptr)
445
0
    return false;
446
447
  /// Recursively check the predecessors of \p ThisBlock up to
448
  /// their common dominator, and see if any of them post-dominates
449
  /// \p OtherBlock.
450
0
  SmallVector<const BasicBlock *, 8> WorkList;
451
0
  SmallPtrSet<const BasicBlock *, 8> Visited;
452
0
  WorkList.push_back(ThisBlock);
453
0
  while (!WorkList.empty()) {
454
0
    const BasicBlock *CurBlock = WorkList.back();
455
0
    WorkList.pop_back();
456
0
    Visited.insert(CurBlock);
457
0
    if (PDT->dominates(CurBlock, OtherBlock))
458
0
      return true;
459
460
0
    for (const auto *Pred : predecessors(CurBlock)) {
461
0
      if (Pred == CommonDominator || Visited.count(Pred))
462
0
        continue;
463
0
      WorkList.push_back(Pred);
464
0
    }
465
0
  }
466
0
  return false;
467
0
}
468
469
bool llvm::isReachedBefore(const Instruction *I0, const Instruction *I1,
470
                           const DominatorTree *DT,
471
0
                           const PostDominatorTree *PDT) {
472
0
  const BasicBlock *BB0 = I0->getParent();
473
0
  const BasicBlock *BB1 = I1->getParent();
474
0
  if (BB0 == BB1)
475
0
    return DT->dominates(I0, I1);
476
477
0
  return nonStrictlyPostDominate(BB1, BB0, DT, PDT);
478
0
}