1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "berberis/backend/x86_64/code_gen.h"
18 
19 #include "berberis/assembler/machine_code.h"
20 #include "berberis/backend/code_emitter.h"
21 #include "berberis/backend/common/machine_ir_opt.h"
22 #include "berberis/backend/common/reg_alloc.h"
23 #include "berberis/backend/x86_64/insn_folding.h"
24 #include "berberis/backend/x86_64/local_guest_context_optimizer.h"
25 #include "berberis/backend/x86_64/loop_guest_context_optimizer.h"
26 #include "berberis/backend/x86_64/machine_ir.h"
27 #include "berberis/backend/x86_64/machine_ir_check.h"
28 #include "berberis/backend/x86_64/machine_ir_opt.h"
29 #include "berberis/backend/x86_64/rename_copy_uses.h"
30 #include "berberis/backend/x86_64/rename_vregs.h"
31 #include "berberis/base/config_globals.h"
32 #include "berberis/base/logging.h"
33 #include "berberis/base/tracing.h"
34 
35 namespace berberis::x86_64 {
36 
GenCode(MachineIR * machine_ir,MachineCode * machine_code,const GenCodeParams & params)37 void GenCode(MachineIR* machine_ir, MachineCode* machine_code, const GenCodeParams& params) {
38   CHECK_EQ(CheckMachineIR(*machine_ir), kMachineIRCheckSuccess);
39   if (IsConfigFlagSet(kVerboseTranslation)) {
40     TRACE("MachineIR before optimizations {\n");
41     TRACE("%s", machine_ir->GetDebugString().c_str());
42     TRACE("}\n\n");
43   }
44 
45   RemoveCriticalEdges(machine_ir);
46 
47   ReorderBasicBlocksInReversePostOrder(machine_ir);
48   MoveColdBlocksToEnd(machine_ir);
49 
50   RemoveLoopGuestContextAccesses(machine_ir);
51   RenameVRegs(machine_ir);
52 
53   RemoveLocalGuestContextAccesses(machine_ir);
54   RemoveRedundantPut(machine_ir);
55   FoldInsns(machine_ir);
56   // Call this after all phases that create copy instructions.
57   RenameCopyUses(machine_ir);
58   RemoveDeadCode(machine_ir);
59 
60   AllocRegs(machine_ir);
61 
62   RemoveNopPseudoCopy(machine_ir);
63   x86_64::RemoveForwarderBlocks(machine_ir);
64 
65   CHECK_EQ(CheckMachineIR(*machine_ir), kMachineIRCheckSuccess);
66 
67   if (IsConfigFlagSet(kVerboseTranslation)) {
68     TRACE("MachineIR before emit {\n");
69     TRACE("%s", machine_ir->GetDebugString().c_str());
70     TRACE("}\n\n");
71   }
72 
73   if (!params.skip_emit) {
74     CodeEmitter emitter(
75         machine_code, machine_ir->FrameSize(), machine_ir->NumBasicBlocks(), machine_ir->arena());
76     machine_ir->Emit(&emitter);
77     emitter.Finalize();
78   }
79 }
80 
81 }  // namespace berberis::x86_64
82