Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 1 | //===-- llvm/CodeGen/GlobalISel/CombinerHelper.h --------------*- C++ -*-===// |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 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 |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 6 | // |
| 7 | //===--------------------------------------------------------------------===// |
| 8 | // |
| 9 | /// This contains common combine transformations that may be used in a combine |
| 10 | /// pass,or by the target elsewhere. |
| 11 | /// Targets can pick individual opcode transformations from the helper or use |
| 12 | /// tryCombine which invokes all transformations. All of the transformations |
| 13 | /// return true if the MachineInstruction changed and false otherwise. |
| 14 | // |
| 15 | //===--------------------------------------------------------------------===// |
| 16 | |
| 17 | #ifndef LLVM_CODEGEN_GLOBALISEL_COMBINER_HELPER_H |
| 18 | #define LLVM_CODEGEN_GLOBALISEL_COMBINER_HELPER_H |
| 19 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame^] | 20 | #include "llvm/CodeGen/LowLevelType.h" |
| 21 | #include "llvm/CodeGen/Register.h" |
| 22 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 23 | namespace llvm { |
| 24 | |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 25 | class GISelChangeObserver; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 26 | class MachineIRBuilder; |
| 27 | class MachineRegisterInfo; |
| 28 | class MachineInstr; |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 29 | class MachineOperand; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 30 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame^] | 31 | struct PreferredTuple { |
| 32 | LLT Ty; // The result type of the extend. |
| 33 | unsigned ExtendOpcode; // G_ANYEXT/G_SEXT/G_ZEXT |
| 34 | MachineInstr *MI; |
| 35 | }; |
| 36 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 37 | class CombinerHelper { |
| 38 | MachineIRBuilder &Builder; |
| 39 | MachineRegisterInfo &MRI; |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 40 | GISelChangeObserver &Observer; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 41 | |
| 42 | public: |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 43 | CombinerHelper(GISelChangeObserver &Observer, MachineIRBuilder &B); |
| 44 | |
| 45 | /// MachineRegisterInfo::replaceRegWith() and inform the observer of the changes |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame^] | 46 | void replaceRegWith(MachineRegisterInfo &MRI, Register FromReg, Register ToReg) const; |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 47 | |
| 48 | /// Replace a single register operand with a new register and inform the |
| 49 | /// observer of the changes. |
| 50 | void replaceRegOpWith(MachineRegisterInfo &MRI, MachineOperand &FromRegOp, |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame^] | 51 | Register ToReg) const; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 52 | |
| 53 | /// If \p MI is COPY, try to combine it. |
| 54 | /// Returns true if MI changed. |
| 55 | bool tryCombineCopy(MachineInstr &MI); |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame^] | 56 | bool matchCombineCopy(MachineInstr &MI); |
| 57 | void applyCombineCopy(MachineInstr &MI); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 58 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 59 | /// If \p MI is extend that consumes the result of a load, try to combine it. |
| 60 | /// Returns true if MI changed. |
| 61 | bool tryCombineExtendingLoads(MachineInstr &MI); |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame^] | 62 | bool matchCombineExtendingLoads(MachineInstr &MI, PreferredTuple &MatchInfo); |
| 63 | void applyCombineExtendingLoads(MachineInstr &MI, PreferredTuple &MatchInfo); |
| 64 | |
| 65 | bool matchCombineBr(MachineInstr &MI); |
| 66 | bool tryCombineBr(MachineInstr &MI); |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 67 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 68 | /// Try to transform \p MI by using all of the above |
| 69 | /// combine functions. Returns true if changed. |
| 70 | bool tryCombine(MachineInstr &MI); |
| 71 | }; |
| 72 | } // namespace llvm |
| 73 | |
| 74 | #endif |