aboutsummaryrefslogtreecommitdiff
path: root/backend/lib/CodeGen/MachineCFGUpdater.cpp
blob: 4aacdf5e3710b01e6e6b4666a5a5dad4b7bcae7f (plain)
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
// SPDX-License-Identifier: GPL-3.0-or-later

#include "rcc/CodeGen/MachineCFGUpdater.h"

#include "rcc/CodeGen/GenericOpcodes.h"
#include "rcc/CodeGen/MachineBasicBlock.h"
#include "rcc/CodeGen/MachineFunction.h"
#include "rcc/CodeGen/MachineInstr.h"
#include "rcc/CodeGen/MachineOperand.h"

#include <algorithm>
#include <cstddef>
#include <optional>
#include <span>
#include <utility>
#include <vector>

namespace rcc {
namespace {

struct PhiIncomingEdit final {
  MachineInstr *Phi = nullptr;
  std::size_t ValueIndex = 0;
};

[[nodiscard]] bool isKnownPhi(const MachineInstr &Instruction) noexcept {
  return Instruction.opcode() == opcode(GenericOpcode::PHI) ||
         Instruction.opcode() == opcode(GenericOpcode::G_PHI);
}

[[nodiscard]] bool containsKnownPhi(const MachineBasicBlock &Block) noexcept {
  for (const MachineInstr *Instruction = Block.firstInstruction(); Instruction != nullptr;
       Instruction = Instruction->next()) {
    if (isKnownPhi(*Instruction))
      return true;
  }
  return false;
}

[[nodiscard]] Expected<std::vector<PhiIncomingEdit>>
collectPhiIncomingEdits(MachineBasicBlock &Block, const MachineBasicBlock &Predecessor) {
  std::vector<PhiIncomingEdit> Result;
  for (MachineInstr *Instruction = Block.firstInstruction(); Instruction != nullptr;
       Instruction = Instruction->next()) {
    if (!isKnownPhi(*Instruction))
      continue;
    if (Instruction->operandCount() < 3 || (Instruction->operandCount() - 1U) % 2U != 0) {
      return makeUnexpected(ErrorCode::VerificationError,
                            "CFG update encountered a malformed PHI instruction");
    }

    std::optional<std::size_t> Match;
    for (std::size_t Index = 1; Index < Instruction->operandCount(); Index += 2U) {
      const MachineOperand *Value = Instruction->operandAt(Index);
      const MachineOperand *BlockOperand = Instruction->operandAt(Index + 1U);
      const Register *Reg = Value == nullptr ? nullptr : Value->getIf<Register>();
      const BasicBlockOperand *Incoming =
          BlockOperand == nullptr ? nullptr : BlockOperand->getIf<BasicBlockOperand>();
      if (Reg == nullptr || Incoming == nullptr || Incoming->Block == nullptr) {
        return makeUnexpected(ErrorCode::VerificationError,
                              "CFG update encountered malformed PHI incoming operands");
      }
      if (Incoming->Block != &Predecessor)
        continue;
      if (Match) {
        return makeUnexpected(ErrorCode::VerificationError,
                              "CFG update encountered duplicate PHI incoming blocks");
      }
      Match = Index;
    }
    if (!Match) {
      return makeUnexpected(ErrorCode::VerificationError,
                            "CFG edge has no corresponding PHI incoming value");
    }
    Result.push_back(PhiIncomingEdit{Instruction, *Match});
  }
  return Result;
}

[[nodiscard]] Expected<void> erasePhiIncoming(std::span<const PhiIncomingEdit> Edits) {
  for (const PhiIncomingEdit &Edit : Edits) {
    auto BlockErased = Edit.Phi->eraseOperand(Edit.ValueIndex + 1U);
    if (!BlockErased)
      return std::unexpected(BlockErased.error());
    auto ValueErased = Edit.Phi->eraseOperand(Edit.ValueIndex);
    if (!ValueErased)
      return std::unexpected(ValueErased.error());
  }
  return {};
}

[[nodiscard]] Expected<void> rewritePhiIncoming(std::span<const PhiIncomingEdit> Edits,
                                                MachineBasicBlock &NewPredecessor) {
  for (const PhiIncomingEdit &Edit : Edits) {
    MachineOperand *BlockOperand = Edit.Phi->operandAt(Edit.ValueIndex + 1U);
    if (BlockOperand == nullptr) {
      return makeUnexpected(ErrorCode::InternalError,
                            "PHI incoming operand disappeared during CFG update");
    }
    auto Rewritten = BlockOperand->setBasicBlock(NewPredecessor);
    if (!Rewritten)
      return std::unexpected(Rewritten.error());
  }
  return {};
}

[[nodiscard]] std::optional<std::size_t> findSuccessorIndex(const MachineBasicBlock &From,
                                                            const MachineBasicBlock &To) {
  const auto Successors = From.successors();
  for (std::size_t Index = 0; Index < Successors.size(); ++Index) {
    if (Successors[Index].Block == &To)
      return Index;
  }
  return std::nullopt;
}

[[nodiscard]] Expected<void> validateOutgoingEdges(MachineBasicBlock &From) {
  for (const MachineSuccessor &Successor : From.successors()) {
    if (Successor.Block == nullptr) {
      return makeUnexpected(ErrorCode::VerificationError, "CFG successor is null");
    }
    if (std::ranges::find(Successor.Block->predecessors(), &From) ==
        Successor.Block->predecessors().end()) {
      return makeUnexpected(ErrorCode::VerificationError,
                            "CFG successor has no reciprocal predecessor");
    }
    auto PhiEdits = collectPhiIncomingEdits(*Successor.Block, From);
    if (!PhiEdits)
      return std::unexpected(PhiEdits.error());
  }
  return {};
}

[[nodiscard]] Expected<void> validateIncomingEdges(MachineBasicBlock &To) {
  for (MachineBasicBlock *Predecessor : To.predecessors()) {
    if (Predecessor == nullptr) {
      return makeUnexpected(ErrorCode::VerificationError, "CFG predecessor is null");
    }
    if (!findSuccessorIndex(*Predecessor, To)) {
      return makeUnexpected(ErrorCode::VerificationError,
                            "CFG predecessor has no reciprocal successor");
    }
    auto PhiEdits = collectPhiIncomingEdits(To, *Predecessor);
    if (!PhiEdits)
      return std::unexpected(PhiEdits.error());
  }
  return {};
}

} // namespace

Expected<void> MachineCFGUpdater::verifyOwnership(const MachineBasicBlock &Block) const {
  if (&Block.function() != Function) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "CFG block belongs to another machine function");
  }
  return {};
}

Expected<void> MachineCFGUpdater::addSuccessor(MachineBasicBlock &From, MachineBasicBlock &To,
                                               BranchProbability Probability) {
  auto FromOwnership = verifyOwnership(From);
  if (!FromOwnership)
    return std::unexpected(FromOwnership.error());
  auto ToOwnership = verifyOwnership(To);
  if (!ToOwnership)
    return std::unexpected(ToOwnership.error());

  const auto Existing = findSuccessorIndex(From, To);
  if (Existing) {
    From.Successors[*Existing].Probability =
        From.Successors[*Existing].Probability.saturatingAdd(Probability);
    return {};
  }

  if (containsKnownPhi(To)) {
    auto PhiEdits = collectPhiIncomingEdits(To, From);
    if (!PhiEdits) {
      return makeUnexpected(
          ErrorCode::InvalidArgument,
          "adding a predecessor to a PHI block requires explicit incoming values");
    }
  }

  From.Successors.push_back(MachineSuccessor{&To, Probability});
  if (std::ranges::find(To.Predecessors, &From) == To.Predecessors.end())
    To.Predecessors.push_back(&From);
  return {};
}

Expected<void> MachineCFGUpdater::removeSuccessor(MachineBasicBlock &From, MachineBasicBlock &To) {
  auto FromOwnership = verifyOwnership(From);
  if (!FromOwnership)
    return std::unexpected(FromOwnership.error());
  auto ToOwnership = verifyOwnership(To);
  if (!ToOwnership)
    return std::unexpected(ToOwnership.error());

  const auto Successor = findSuccessorIndex(From, To);
  if (!Successor) {
    return makeUnexpected(ErrorCode::InvalidArgument, "CFG successor edge is not present");
  }
  const auto Predecessor = std::ranges::find(To.Predecessors, &From);
  if (Predecessor == To.Predecessors.end()) {
    return makeUnexpected(ErrorCode::VerificationError,
                          "CFG successor has no reciprocal predecessor");
  }

  auto PhiEdits = collectPhiIncomingEdits(To, From);
  if (!PhiEdits)
    return std::unexpected(PhiEdits.error());
  auto PhiResult = erasePhiIncoming(*PhiEdits);
  if (!PhiResult)
    return std::unexpected(PhiResult.error());

  From.Successors.erase(From.Successors.begin() + static_cast<std::ptrdiff_t>(*Successor));
  To.Predecessors.erase(Predecessor);
  return {};
}

Expected<void> MachineCFGUpdater::replaceSuccessor(MachineBasicBlock &From,
                                                   MachineBasicBlock &OldTarget,
                                                   MachineBasicBlock &NewTarget) {
  auto FromOwnership = verifyOwnership(From);
  if (!FromOwnership)
    return std::unexpected(FromOwnership.error());
  auto OldOwnership = verifyOwnership(OldTarget);
  if (!OldOwnership)
    return std::unexpected(OldOwnership.error());
  auto NewOwnership = verifyOwnership(NewTarget);
  if (!NewOwnership)
    return std::unexpected(NewOwnership.error());

  if (&OldTarget == &NewTarget)
    return {};

  const auto Existing = findSuccessorIndex(From, OldTarget);
  if (!Existing) {
    return makeUnexpected(ErrorCode::InvalidArgument, "replaced CFG successor edge is not present");
  }
  const BranchProbability Probability = From.Successors[*Existing].Probability;

  const auto OldPredecessor = std::ranges::find(OldTarget.Predecessors, &From);
  if (OldPredecessor == OldTarget.Predecessors.end()) {
    return makeUnexpected(ErrorCode::VerificationError,
                          "replaced CFG successor has no reciprocal predecessor");
  }
  auto OldPhiEdits = collectPhiIncomingEdits(OldTarget, From);
  if (!OldPhiEdits)
    return std::unexpected(OldPhiEdits.error());

  const auto NewEdge = findSuccessorIndex(From, NewTarget);
  const auto NewPredecessor = std::ranges::find(NewTarget.Predecessors, &From);
  if (NewEdge) {
    if (NewPredecessor == NewTarget.Predecessors.end()) {
      return makeUnexpected(ErrorCode::VerificationError,
                            "replacement target has no reciprocal predecessor");
    }
    auto NewPhiEdits = collectPhiIncomingEdits(NewTarget, From);
    if (!NewPhiEdits)
      return std::unexpected(NewPhiEdits.error());
  } else {
    if (NewPredecessor != NewTarget.Predecessors.end()) {
      return makeUnexpected(ErrorCode::VerificationError,
                            "replacement target has a predecessor without a reciprocal edge");
    }
    if (containsKnownPhi(NewTarget)) {
      auto NewPhiEdits = collectPhiIncomingEdits(NewTarget, From);
      if (!NewPhiEdits) {
        return makeUnexpected(
            ErrorCode::InvalidArgument,
            "redirecting an edge to a PHI block requires explicit incoming values");
      }
    }
  }
  auto PhiResult = erasePhiIncoming(*OldPhiEdits);
  if (!PhiResult)
    return std::unexpected(PhiResult.error());
  OldTarget.Predecessors.erase(OldPredecessor);
  if (NewEdge) {
    From.Successors[*NewEdge].Probability =
        From.Successors[*NewEdge].Probability.saturatingAdd(Probability);
    From.Successors.erase(From.Successors.begin() + static_cast<std::ptrdiff_t>(*Existing));
  } else {
    From.Successors[*Existing].Block = &NewTarget;
    if (std::ranges::find(NewTarget.Predecessors, &From) == NewTarget.Predecessors.end())
      NewTarget.Predecessors.push_back(&From);
  }
  return {};
}

Expected<void> MachineCFGUpdater::splitEdge(MachineBasicBlock &From, MachineBasicBlock &To,
                                            MachineBasicBlock &EdgeBlock) {
  auto FromOwnership = verifyOwnership(From);
  if (!FromOwnership)
    return std::unexpected(FromOwnership.error());
  auto ToOwnership = verifyOwnership(To);
  if (!ToOwnership)
    return std::unexpected(ToOwnership.error());
  auto EdgeOwnership = verifyOwnership(EdgeBlock);
  if (!EdgeOwnership)
    return std::unexpected(EdgeOwnership.error());
  if (&From == &To || &From == &EdgeBlock || &To == &EdgeBlock) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "edge split requires three distinct basic blocks");
  }
  if (!EdgeBlock.isLinked() || EdgeBlock.firstInstruction() != nullptr ||
      !EdgeBlock.predecessors().empty() || !EdgeBlock.successors().empty()) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "edge split block must be linked, empty, and disconnected");
  }

  const auto Successor = findSuccessorIndex(From, To);
  if (!Successor)
    return makeUnexpected(ErrorCode::InvalidArgument, "split CFG edge is not present");
  auto Predecessor = std::ranges::find(To.Predecessors, &From);
  if (Predecessor == To.Predecessors.end()) {
    return makeUnexpected(ErrorCode::VerificationError,
                          "split CFG edge has no reciprocal predecessor");
  }
  auto PhiEdits = collectPhiIncomingEdits(To, From);
  if (!PhiEdits)
    return std::unexpected(PhiEdits.error());

  auto PhiResult = rewritePhiIncoming(*PhiEdits, EdgeBlock);
  if (!PhiResult)
    return std::unexpected(PhiResult.error());
  From.Successors[*Successor].Block = &EdgeBlock;
  *Predecessor = &EdgeBlock;
  EdgeBlock.Predecessors.push_back(&From);
  EdgeBlock.Successors.push_back(MachineSuccessor{&To, BranchProbability::one()});
  return {};
}

Expected<void> MachineCFGUpdater::updateBranchProbability(MachineBasicBlock &From,
                                                          MachineBasicBlock &To,
                                                          BranchProbability Probability) {
  auto FromOwnership = verifyOwnership(From);
  if (!FromOwnership)
    return std::unexpected(FromOwnership.error());
  auto ToOwnership = verifyOwnership(To);
  if (!ToOwnership)
    return std::unexpected(ToOwnership.error());

  const auto Successor = findSuccessorIndex(From, To);
  if (!Successor) {
    return makeUnexpected(ErrorCode::InvalidArgument, "updated CFG successor edge is not present");
  }
  From.Successors[*Successor].Probability = Probability;
  return {};
}

Expected<void> MachineCFGUpdater::transferSuccessors(MachineBasicBlock &From,
                                                     MachineBasicBlock &To) {
  auto FromOwnership = verifyOwnership(From);
  if (!FromOwnership)
    return std::unexpected(FromOwnership.error());
  auto ToOwnership = verifyOwnership(To);
  if (!ToOwnership)
    return std::unexpected(ToOwnership.error());
  if (&From == &To)
    return {};

  struct Transfer final {
    MachineSuccessor Successor;
    std::vector<PhiIncomingEdit> PhiEdits;
    bool MergeExisting = false;
  };
  std::vector<Transfer> Transfers;
  Transfers.reserve(From.Successors.size());
  for (const MachineSuccessor &Successor : From.Successors) {
    if (Successor.Block == nullptr) {
      return makeUnexpected(ErrorCode::VerificationError, "transferred CFG successor is null");
    }
    if (std::ranges::find(Successor.Block->Predecessors, &From) ==
        Successor.Block->Predecessors.end()) {
      return makeUnexpected(ErrorCode::VerificationError,
                            "transferred CFG successor has no reciprocal predecessor");
    }
    auto PhiEdits = collectPhiIncomingEdits(*Successor.Block, From);
    if (!PhiEdits)
      return std::unexpected(PhiEdits.error());
    const bool MergeExisting = findSuccessorIndex(To, *Successor.Block).has_value();
    const auto ToPredecessor = std::ranges::find(Successor.Block->Predecessors, &To);
    if (MergeExisting) {
      if (ToPredecessor == Successor.Block->Predecessors.end()) {
        return makeUnexpected(ErrorCode::VerificationError,
                              "transfer target edge has no reciprocal predecessor");
      }
      auto ExistingPhiEdits = collectPhiIncomingEdits(*Successor.Block, To);
      if (!ExistingPhiEdits)
        return std::unexpected(ExistingPhiEdits.error());
    } else if (ToPredecessor != Successor.Block->Predecessors.end()) {
      return makeUnexpected(ErrorCode::VerificationError,
                            "transfer target is a predecessor without a reciprocal edge");
    }
    Transfers.push_back(Transfer{Successor, std::move(*PhiEdits), MergeExisting});
  }

  for (Transfer &TransferValue : Transfers) {
    MachineBasicBlock &Successor = *TransferValue.Successor.Block;
    auto OldPredecessor = std::ranges::find(Successor.Predecessors, &From);
    if (TransferValue.MergeExisting) {
      auto PhiResult = erasePhiIncoming(TransferValue.PhiEdits);
      if (!PhiResult)
        return std::unexpected(PhiResult.error());
      Successor.Predecessors.erase(OldPredecessor);
      const std::optional<std::size_t> Existing = findSuccessorIndex(To, Successor);
      if (!Existing) {
        return makeUnexpected(ErrorCode::InternalError,
                              "preflighted transfer target edge disappeared");
      }
      To.Successors[Existing.value()].Probability =
          To.Successors[Existing.value()].Probability.saturatingAdd(
              TransferValue.Successor.Probability);
    } else {
      auto PhiResult = rewritePhiIncoming(TransferValue.PhiEdits, To);
      if (!PhiResult)
        return std::unexpected(PhiResult.error());
      *OldPredecessor = &To;
      To.Successors.push_back(TransferValue.Successor);
    }
  }
  From.Successors.clear();
  return {};
}

Expected<void> MachineCFGUpdater::disconnectBlock(MachineBasicBlock &Block) {
  auto Ownership = verifyOwnership(Block);
  if (!Ownership)
    return std::unexpected(Ownership.error());

  auto Outgoing = validateOutgoingEdges(Block);
  if (!Outgoing)
    return std::unexpected(Outgoing.error());
  auto Incoming = validateIncomingEdges(Block);
  if (!Incoming)
    return std::unexpected(Incoming.error());

  const std::vector<MachineSuccessor> Successors = Block.Successors;
  for (const MachineSuccessor &Successor : Successors) {
    auto Result = removeSuccessor(Block, *Successor.Block);
    if (!Result)
      return std::unexpected(Result.error());
  }

  const std::vector<MachineBasicBlock *> Predecessors = Block.Predecessors;
  for (MachineBasicBlock *Predecessor : Predecessors) {
    auto Result = removeSuccessor(*Predecessor, Block);
    if (!Result)
      return std::unexpected(Result.error());
  }
  return {};
}

Expected<MachineBasicBlock *> MachineCFGUpdater::splitBlock(MachineBasicBlock &Block,
                                                            MachineInstr &SplitBefore) {
  auto Ownership = verifyOwnership(Block);
  if (!Ownership)
    return std::unexpected(Ownership.error());
  if (!Block.isLinked() || SplitBefore.parent() != &Block) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "split point is not linked to the requested basic block");
  }
  bool MovesPhi = false;
  for (const MachineInstr *Instruction = &SplitBefore; Instruction != nullptr;
       Instruction = Instruction->next()) {
    MovesPhi |= isKnownPhi(*Instruction);
  }
  if (MovesPhi) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "a basic block cannot be split inside its PHI region");
  }

  auto Outgoing = validateOutgoingEdges(Block);
  if (!Outgoing)
    return std::unexpected(Outgoing.error());

  auto NewBlockResult = Function->createBasicBlock();
  if (!NewBlockResult)
    return std::unexpected(NewBlockResult.error());
  MachineBasicBlock *NewBlock = *NewBlockResult;
  Expected<void> Inserted = Block.next() == nullptr
                                ? Function->appendBlock(*NewBlock)
                                : Function->insertBlockBefore(*Block.next(), *NewBlock);
  if (!Inserted)
    return std::unexpected(Inserted.error());

  MachineInstr *Instruction = &SplitBefore;
  while (Instruction != nullptr) {
    MachineInstr *Next = Instruction->next();
    auto Removed = Block.removeInstruction(*Instruction);
    if (!Removed)
      return std::unexpected(Removed.error());
    auto Appended = NewBlock->appendInstruction(*Instruction);
    if (!Appended)
      return std::unexpected(Appended.error());
    Instruction = Next;
  }

  auto Transferred = transferSuccessors(Block, *NewBlock);
  if (!Transferred)
    return std::unexpected(Transferred.error());
  auto Connected = addSuccessor(Block, *NewBlock, BranchProbability::one());
  if (!Connected)
    return std::unexpected(Connected.error());
  return NewBlock;
}

Expected<void> MachineCFGUpdater::deleteBlock(MachineBasicBlock &Block) {
  auto Ownership = verifyOwnership(Block);
  if (!Ownership)
    return std::unexpected(Ownership.error());
  if (!Block.isLinked())
    return makeUnexpected(ErrorCode::InvalidArgument, "deleted CFG block is not linked");
  auto Disconnected = disconnectBlock(Block);
  if (!Disconnected)
    return std::unexpected(Disconnected.error());
  return Function->removeBlock(Block);
}

} // namespace rcc