aboutsummaryrefslogtreecommitdiff
path: root/backend/lib/CodeGen/MachineFunctionCloner.cpp
blob: 46fed6c3c2350dc0b449cf6c76073c3f4e8ae43a (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
// SPDX-License-Identifier: GPL-3.0-or-later

#include "rcc/CodeGen/MachineFunctionCloner.h"

#include "rcc/CodeGen/InstructionDescriptor.h"
#include "rcc/CodeGen/MachineBasicBlock.h"
#include "rcc/CodeGen/MachineCFGUpdater.h"
#include "rcc/CodeGen/MachineFrameInfo.h"
#include "rcc/CodeGen/MachineFunction.h"
#include "rcc/CodeGen/MachineInstr.h"
#include "rcc/CodeGen/MachineMemOperand.h"
#include "rcc/CodeGen/MachineOperand.h"
#include "rcc/CodeGen/MachineRegisterInfo.h"
#include "rcc/CodeGen/MachineVerifier.h"

#include <cstddef>
#include <cstdint>
#include <memory>
#include <optional>
#include <string>
#include <unordered_map>
#include <utility>

namespace rcc {
namespace {

struct CloneState final {
  const MachineFunction &Source;
  MachineFunction &Destination;
  MachineFunctionCloneResult &Result;
  std::unordered_map<const MachineBasicBlock *, MachineBasicBlock *> Blocks;
  std::unordered_map<const MachineInstr *, MachineInstr *> Instructions;
  std::unordered_map<const MachineMemOperand *, const MachineMemOperand *> MemOperands;
};

[[nodiscard]] Expected<MachineOperand> cloneOperand(const MachineOperand &SourceOperand,
                                                    CloneState &State) {
  switch (SourceOperand.kind()) {
  case MachineOperand::Kind::Register: {
    const Register *SourceReg = SourceOperand.getIf<Register>();
    if (SourceReg == nullptr) {
      return makeUnexpected(ErrorCode::VerificationError,
                            "source register operand has inconsistent storage");
    }
    Register DestinationReg = *SourceReg;
    if (SourceReg->isVirtual()) {
      const auto Position = State.Result.VirtualRegisters.find(*SourceReg);
      if (Position == State.Result.VirtualRegisters.end()) {
        return makeUnexpected(ErrorCode::VerificationError,
                              "source operand references an unmapped virtual register");
      }
      DestinationReg = Position->second;
    }

    MachineOperand Result = SourceOperand.cloneDetached();
    auto SetResult = Result.setRegister(DestinationReg);
    if (!SetResult)
      return std::unexpected(SetResult.error());
    return Result;
  }
  case MachineOperand::Kind::BasicBlock: {
    const auto *SourceBlock = SourceOperand.getIf<BasicBlockOperand>();
    if (SourceBlock == nullptr || SourceBlock->Block == nullptr) {
      return makeUnexpected(ErrorCode::VerificationError, "source basic-block operand is invalid");
    }
    const auto Position = State.Blocks.find(SourceBlock->Block);
    if (Position == State.Blocks.end()) {
      return makeUnexpected(ErrorCode::VerificationError,
                            "source basic-block operand is not in live layout");
    }
    return MachineOperand::basicBlock(*Position->second, SourceOperand.targetFlags());
  }
  case MachineOperand::Kind::BlockAddress: {
    const auto *Address = SourceOperand.getIf<BlockAddressOperand>();
    if (Address == nullptr) {
      return makeUnexpected(ErrorCode::VerificationError,
                            "source block-address operand is invalid");
    }
    std::string FunctionName = Address->FunctionName;
    if (FunctionName == State.Source.name())
      FunctionName = std::string(State.Destination.name());
    return MachineOperand::blockAddress(std::move(FunctionName), Address->BlockName,
                                        Address->Offset, SourceOperand.targetFlags());
  }
  case MachineOperand::Kind::FrameIndex: {
    const auto *Frame = SourceOperand.getIf<FrameIndexOperand>();
    if (Frame == nullptr) {
      return makeUnexpected(ErrorCode::VerificationError, "source frame-index operand is invalid");
    }
    const auto Position = State.Result.FrameIndices.find(Frame->Index);
    if (Position == State.Result.FrameIndices.end()) {
      return makeUnexpected(ErrorCode::VerificationError,
                            "source operand references an unmapped frame index");
    }
    return MachineOperand::frameIndex(Position->second, Frame->Offset, SourceOperand.targetFlags());
  }
  case MachineOperand::Kind::ConstantPoolIndex: {
    const auto *Constant = SourceOperand.getIf<ConstantPoolOperand>();
    if (Constant == nullptr) {
      return makeUnexpected(ErrorCode::VerificationError,
                            "source constant-pool operand is invalid");
    }
    State.Result.ConstantPoolIndices.emplace(Constant->Index, Constant->Index);
    return SourceOperand.cloneDetached();
  }
  case MachineOperand::Kind::JumpTableIndex: {
    const auto *JumpTable = SourceOperand.getIf<JumpTableOperand>();
    if (JumpTable == nullptr) {
      return makeUnexpected(ErrorCode::VerificationError, "source jump-table operand is invalid");
    }
    State.Result.JumpTableIndices.emplace(JumpTable->Index, JumpTable->Index);
    return SourceOperand.cloneDetached();
  }
  case MachineOperand::Kind::Immediate:
  case MachineOperand::Kind::FloatingImmediate:
  case MachineOperand::Kind::GlobalSymbol:
  case MachineOperand::Kind::ExternalSymbol:
  case MachineOperand::Kind::RegisterMask:
  case MachineOperand::Kind::Metadata:
  case MachineOperand::Kind::TargetIndex:
  case MachineOperand::Kind::MCSymbol:
    return SourceOperand.cloneDetached();
  case MachineOperand::Kind::Count:
    return makeUnexpected(ErrorCode::VerificationError, "source operand has an invalid kind");
  }
  return makeUnexpected(ErrorCode::InternalError, "source operand kind is not handled");
}

void recordMemoryIndexMappings(const MachineMemOperandDesc &Description,
                               MachineFunctionCloneResult &Result) {
  if (const auto Constant = Description.Base.constantPoolIndex())
    Result.ConstantPoolIndices.emplace(*Constant, *Constant);
  if (const auto JumpTable = Description.Base.jumpTableIndex())
    Result.JumpTableIndices.emplace(*JumpTable, *JumpTable);
}

[[nodiscard]] Expected<const MachineMemOperand *>
cloneMemOperand(const MachineMemOperand &SourceOperand, CloneState &State) {
  const auto Existing = State.MemOperands.find(&SourceOperand);
  if (Existing != State.MemOperands.end())
    return Existing->second;

  MachineMemOperandDesc Description = SourceOperand.description();
  recordMemoryIndexMappings(Description, State.Result);
  if (const auto Frame = Description.Base.frameIndex()) {
    const auto Position = State.Result.FrameIndices.find(*Frame);
    if (Position == State.Result.FrameIndices.end()) {
      return makeUnexpected(ErrorCode::VerificationError,
                            "source MMO references an unmapped frame index");
    }
    Description.Base = MemoryBase::frameIndex(Position->second);
  }
  auto DestinationOperand = State.Destination.createMemOperand(std::move(Description));
  if (!DestinationOperand)
    return std::unexpected(DestinationOperand.error());
  State.MemOperands.emplace(&SourceOperand, *DestinationOperand);
  return *DestinationOperand;
}

[[nodiscard]] Expected<void> cloneVirtualRegisters(CloneState &State) {
  const auto SourceRegisters = State.Source.registerInfo().virtualRegisters();
  for (std::size_t Index = 0; Index < SourceRegisters.size(); ++Index) {
    const VirtualRegisterInfo &SourceInfo = SourceRegisters[Index];
    if (SourceInfo.IsTombstone)
      continue;

    auto SourceReg = Register::makeVirtual(Index);
    if (!SourceReg)
      return std::unexpected(SourceReg.error());
    Expected<Register> DestinationReg =
        SourceInfo.Type ? State.Destination.registerInfo().createVirtualRegister(*SourceInfo.Type)
                        : State.Destination.registerInfo().createVirtualRegister();
    if (!DestinationReg)
      return std::unexpected(DestinationReg.error());

    if (SourceInfo.Bank) {
      auto SetResult =
          State.Destination.registerInfo().setRegisterBank(*DestinationReg, *SourceInfo.Bank);
      if (!SetResult)
        return std::unexpected(SetResult.error());
    }
    if (SourceInfo.RegClass) {
      auto SetResult =
          State.Destination.registerInfo().setRegisterClass(*DestinationReg, *SourceInfo.RegClass);
      if (!SetResult)
        return std::unexpected(SetResult.error());
    }
    for (const PhysicalRegister Hint : SourceInfo.AllocationHints) {
      auto AddResult = State.Destination.registerInfo().addAllocationHint(*DestinationReg, Hint);
      if (!AddResult)
        return std::unexpected(AddResult.error());
    }
    State.Result.VirtualRegisters.emplace(*SourceReg, *DestinationReg);
  }
  return {};
}

[[nodiscard]] Expected<void> cloneRegisterConfiguration(CloneState &State) {
  for (const PhysicalRegisterLiveIn &LiveIn : State.Source.registerInfo().liveIns()) {
    std::optional<Register> Initial;
    if (LiveIn.InitialVirtualRegister) {
      const auto Position = State.Result.VirtualRegisters.find(*LiveIn.InitialVirtualRegister);
      if (Position == State.Result.VirtualRegisters.end()) {
        return makeUnexpected(ErrorCode::VerificationError,
                              "source live-in references an unmapped virtual register");
      }
      Initial = Position->second;
    }
    auto AddResult = State.Destination.registerInfo().addLiveIn(LiveIn.PhysReg, Initial);
    if (!AddResult)
      return std::unexpected(AddResult.error());
  }
  for (const PhysicalRegister Reg : State.Source.registerInfo().reservedRegisters()) {
    auto ReserveResult = State.Destination.registerInfo().reservePhysicalRegister(Reg);
    if (!ReserveResult)
      return std::unexpected(ReserveResult.error());
  }
  return {};
}

[[nodiscard]] Expected<void> cloneFrameObjects(CloneState &State) {
  for (const StackObject &SourceObject : State.Source.frameInfo().objects()) {
    const StackObjectDesc *Description = SourceObject.description();
    if (Description == nullptr)
      continue;
    auto DestinationIndex = State.Destination.frameInfo().createObject(*Description);
    if (!DestinationIndex)
      return std::unexpected(DestinationIndex.error());
    State.Result.FrameIndices.emplace(SourceObject.index(), *DestinationIndex);
  }
  auto CallFrameResult = State.Destination.frameInfo().setMaxCallFrameSize(
      State.Source.frameInfo().maxCallFrameSize());
  if (!CallFrameResult)
    return std::unexpected(CallFrameResult.error());
  return {};
}

[[nodiscard]] Expected<void> cloneBlocks(CloneState &State) {
  for (const MachineBasicBlock *SourceBlock = State.Source.firstBlock(); SourceBlock != nullptr;
       SourceBlock = SourceBlock->next()) {
    auto DestinationBlock = State.Destination.createBasicBlock();
    if (!DestinationBlock)
      return std::unexpected(DestinationBlock.error());
    auto AppendResult = State.Destination.appendBlock(**DestinationBlock);
    if (!AppendResult)
      return std::unexpected(AppendResult.error());
    State.Blocks.emplace(SourceBlock, *DestinationBlock);
    State.Result.BlockStableIDs.emplace(SourceBlock->stableID(), (*DestinationBlock)->stableID());

    for (const MachineBasicBlockLiveIn &LiveIn : SourceBlock->liveIns()) {
      auto AddResult = (*DestinationBlock)->addLiveIn(LiveIn.PhysReg, LiveIn.Lanes);
      if (!AddResult)
        return std::unexpected(AddResult.error());
    }
  }
  return {};
}

[[nodiscard]] Expected<void> cloneInstructions(CloneState &State) {
  for (const MachineBasicBlock *SourceBlock = State.Source.firstBlock(); SourceBlock != nullptr;
       SourceBlock = SourceBlock->next()) {
    const auto DestinationBlockPosition = State.Blocks.find(SourceBlock);
    if (DestinationBlockPosition == State.Blocks.end()) {
      return makeUnexpected(ErrorCode::InternalError,
                            "source block was not mapped before instruction cloning");
    }
    MachineBasicBlock *DestinationBlock = DestinationBlockPosition->second;
    for (const MachineInstr *SourceInstruction = SourceBlock->firstInstruction();
         SourceInstruction != nullptr; SourceInstruction = SourceInstruction->next()) {
      auto DestinationInstruction =
          State.Destination.createInstruction(SourceInstruction->opcode());
      if (!DestinationInstruction)
        return std::unexpected(DestinationInstruction.error());
      MachineInstr *NewInstruction = *DestinationInstruction;
      State.Instructions.emplace(SourceInstruction, NewInstruction);
      State.Result.InstructionStableIDs.emplace(SourceInstruction->stableID(),
                                                NewInstruction->stableID());
      NewInstruction->reserveOperands(SourceInstruction->operandCount());

      for (const MachineOperand &SourceOperand : SourceInstruction->operands()) {
        auto DestinationOperand = cloneOperand(SourceOperand, State);
        if (!DestinationOperand)
          return std::unexpected(DestinationOperand.error());
        auto AddResult = NewInstruction->addOperand(std::move(*DestinationOperand));
        if (!AddResult)
          return std::unexpected(AddResult.error());
      }

      const auto SourceOperands = SourceInstruction->operands();
      for (std::size_t Index = 0; Index < SourceOperands.size(); ++Index) {
        const auto Tied = SourceOperands[Index].tiedOperandIndex();
        if (!Tied || Index > *Tied)
          continue;
        const std::size_t DefIndex = SourceOperands[Index].isDef() ? Index : *Tied;
        const std::size_t UseIndex = SourceOperands[Index].isDef() ? *Tied : Index;
        auto TieResult = NewInstruction->tieOperands(DefIndex, UseIndex);
        if (!TieResult)
          return std::unexpected(TieResult.error());
      }

      for (const MachineMemOperand *SourceMemOperand : SourceInstruction->memOperands()) {
        auto DestinationMemOperand = cloneMemOperand(*SourceMemOperand, State);
        if (!DestinationMemOperand)
          return std::unexpected(DestinationMemOperand.error());
        auto AddResult = NewInstruction->addMemOperand(**DestinationMemOperand);
        if (!AddResult)
          return std::unexpected(AddResult.error());
      }

      NewInstruction->setFlags(SourceInstruction->flags());
      if (SourceInstruction->sourceLocation()) {
        NewInstruction->setSourceLocation(*SourceInstruction->sourceLocation());
      }
      if (SourceInstruction->origin())
        NewInstruction->setOrigin(*SourceInstruction->origin());

      auto AppendResult = DestinationBlock->appendInstruction(*NewInstruction);
      if (!AppendResult)
        return std::unexpected(AppendResult.error());
    }
  }
  return {};
}

[[nodiscard]] Expected<void> finalizeFrameClone(CloneState &State) {
  for (const StackObject &SourceObject : State.Source.frameInfo().objects()) {
    const StackObjectDesc *SourceDescription = SourceObject.description();
    if (SourceDescription == nullptr)
      continue;
    const auto FramePosition = State.Result.FrameIndices.find(SourceObject.index());
    if (FramePosition == State.Result.FrameIndices.end()) {
      return makeUnexpected(ErrorCode::InternalError,
                            "source stack object was not mapped before frame finalization");
    }

    StackObjectDesc DestinationDescription = *SourceDescription;
    for (StackObjectLifetimeRange &Range : DestinationDescription.Lifetime) {
      const auto Begin = State.Result.InstructionStableIDs.find(Range.Begin);
      const auto End = State.Result.InstructionStableIDs.find(Range.End);
      if (Begin == State.Result.InstructionStableIDs.end() ||
          End == State.Result.InstructionStableIDs.end()) {
        return makeUnexpected(ErrorCode::VerificationError,
                              "source stack lifetime references an unmapped instruction");
      }
      Range.Begin = Begin->second;
      Range.End = End->second;
    }
    auto UpdateResult = State.Destination.frameInfo().updateObject(
        FramePosition->second, std::move(DestinationDescription));
    if (!UpdateResult)
      return std::unexpected(UpdateResult.error());
  }

  if (State.Source.frameInfo().isFrozen()) {
    const auto StackSize = State.Source.frameInfo().staticStackSize();
    if (!StackSize) {
      return makeUnexpected(ErrorCode::InternalError,
                            "verified frozen source frame has no static stack size");
    }
    auto FreezeResult =
        State.Destination.frameInfo().freeze(*StackSize, State.Source.frameInfo().stackAlignment());
    if (!FreezeResult)
      return std::unexpected(FreezeResult.error());
  }
  return {};
}

[[nodiscard]] Expected<void> cloneCFG(CloneState &State) {
  MachineCFGUpdater Updater = State.Destination.cfgUpdater();
  for (const MachineBasicBlock *SourceBlock = State.Source.firstBlock(); SourceBlock != nullptr;
       SourceBlock = SourceBlock->next()) {
    const auto DestinationBlockPosition = State.Blocks.find(SourceBlock);
    if (DestinationBlockPosition == State.Blocks.end()) {
      return makeUnexpected(ErrorCode::InternalError,
                            "source block was not mapped before CFG cloning");
    }
    MachineBasicBlock *DestinationBlock = DestinationBlockPosition->second;
    for (const MachineSuccessor &Successor : SourceBlock->successors()) {
      const auto Target = State.Blocks.find(Successor.Block);
      if (Target == State.Blocks.end()) {
        return makeUnexpected(ErrorCode::VerificationError,
                              "source CFG references a detached block");
      }
      auto AddResult =
          Updater.addSuccessor(*DestinationBlock, *Target->second, Successor.Probability);
      if (!AddResult)
        return std::unexpected(AddResult.error());
    }
  }
  return {};
}

} // namespace

MachineFunctionCloneResult::MachineFunctionCloneResult() = default;
MachineFunctionCloneResult::MachineFunctionCloneResult(MachineFunctionCloneResult &&) noexcept =
    default;
MachineFunctionCloneResult &
MachineFunctionCloneResult::operator=(MachineFunctionCloneResult &&) noexcept = default;
MachineFunctionCloneResult::~MachineFunctionCloneResult() = default;

Expected<MachineFunctionCloneResult> cloneMachineFunction(const MachineFunction &Source,
                                                          const InstructionSet &Instructions,
                                                          MachineFunctionCloneOptions Options) {
  auto SourceVerification = MachineVerifier::verify(Source, Instructions);
  if (!SourceVerification)
    return std::unexpected(SourceVerification.error());
  if (!Options.NewStableID.isValid()) {
    return makeUnexpected(ErrorCode::InvalidArgument, "clone function StableID must be valid");
  }

  std::string Name = Options.NewName ? std::move(*Options.NewName) : std::string(Source.name());
  auto Destination = MachineFunction::create(std::move(Name), Options.NewStableID);
  if (!Destination)
    return std::unexpected(Destination.error());

  MachineFunctionCloneResult Result;
  Result.ClonedFunction = std::move(*Destination);
  CloneState State{Source, *Result.ClonedFunction, Result, {}, {}, {}};

  auto VRegResult = cloneVirtualRegisters(State);
  if (!VRegResult)
    return std::unexpected(VRegResult.error());
  auto RegisterResult = cloneRegisterConfiguration(State);
  if (!RegisterResult)
    return std::unexpected(RegisterResult.error());
  auto FrameResult = cloneFrameObjects(State);
  if (!FrameResult)
    return std::unexpected(FrameResult.error());
  auto BlockResult = cloneBlocks(State);
  if (!BlockResult)
    return std::unexpected(BlockResult.error());
  auto InstructionResult = cloneInstructions(State);
  if (!InstructionResult)
    return std::unexpected(InstructionResult.error());
  auto FrameFinalizationResult = finalizeFrameClone(State);
  if (!FrameFinalizationResult)
    return std::unexpected(FrameFinalizationResult.error());
  auto CFGResult = cloneCFG(State);
  if (!CFGResult)
    return std::unexpected(CFGResult.error());

  Result.ClonedFunction->properties() = Source.properties();
  auto DestinationVerification = MachineVerifier::verify(*Result.ClonedFunction, Instructions);
  if (!DestinationVerification)
    return std::unexpected(DestinationVerification.error());
  return Result;
}

} // namespace rcc