1 //===-- llvm/CodeGen/GlobalISel/ConstantFoldingMIRBuilder.h --*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 /// This file implements a version of MachineIRBuilder which does trivial
12 //===----------------------------------------------------------------------===//
13 #include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
14 #include "llvm/CodeGen/GlobalISel/Utils.h"
18 static Optional<APInt> ConstantFoldBinOp(unsigned Opcode, const unsigned Op1,
20 const MachineRegisterInfo &MRI) {
21 auto MaybeOp1Cst = getConstantVRegVal(Op1, MRI);
22 auto MaybeOp2Cst = getConstantVRegVal(Op2, MRI);
23 if (MaybeOp1Cst && MaybeOp2Cst) {
24 LLT Ty = MRI.getType(Op1);
25 APInt C1(Ty.getSizeInBits(), *MaybeOp1Cst, true);
26 APInt C2(Ty.getSizeInBits(), *MaybeOp2Cst, true);
30 case TargetOpcode::G_ADD:
32 case TargetOpcode::G_AND:
34 case TargetOpcode::G_ASHR:
36 case TargetOpcode::G_LSHR:
38 case TargetOpcode::G_MUL:
40 case TargetOpcode::G_OR:
42 case TargetOpcode::G_SHL:
44 case TargetOpcode::G_SUB:
46 case TargetOpcode::G_XOR:
48 case TargetOpcode::G_UDIV:
49 if (!C2.getBoolValue())
52 case TargetOpcode::G_SDIV:
53 if (!C2.getBoolValue())
56 case TargetOpcode::G_UREM:
57 if (!C2.getBoolValue())
60 case TargetOpcode::G_SREM:
61 if (!C2.getBoolValue())
69 /// An MIRBuilder which does trivial constant folding of binary ops.
70 /// Calls to buildInstr will also try to constant fold binary ops.
71 class ConstantFoldingMIRBuilder
72 : public FoldableInstructionsBuilder<ConstantFoldingMIRBuilder> {
74 // Pull in base class constructors.
75 using FoldableInstructionsBuilder<
76 ConstantFoldingMIRBuilder>::FoldableInstructionsBuilder;
78 using FoldableInstructionsBuilder<ConstantFoldingMIRBuilder>::buildInstr;
80 // Implement buildBinaryOp required by FoldableInstructionsBuilder which
81 // tries to constant fold.
82 MachineInstrBuilder buildBinaryOp(unsigned Opcode, unsigned Dst,
83 unsigned Src0, unsigned Src1) {
84 validateBinaryOp(Dst, Src0, Src1);
85 auto MaybeCst = ConstantFoldBinOp(Opcode, Src0, Src1, getMF().getRegInfo());
87 return buildConstant(Dst, MaybeCst->getSExtValue());
88 return buildInstr(Opcode).addDef(Dst).addUse(Src0).addUse(Src1);
91 template <typename DstTy, typename UseArg1Ty, typename UseArg2Ty>
92 MachineInstrBuilder buildInstr(unsigned Opc, DstTy &&Ty, UseArg1Ty &&Arg1,
94 unsigned Dst = getDestFromArg(Ty);
95 return buildInstr(Opc, Dst, getRegFromArg(std::forward<UseArg1Ty>(Arg1)),
96 getRegFromArg(std::forward<UseArg2Ty>(Arg2)));
99 // Try to provide an overload for buildInstr for binary ops in order to
101 MachineInstrBuilder buildInstr(unsigned Opc, unsigned Dst, unsigned Src0,
106 case TargetOpcode::G_ADD:
107 case TargetOpcode::G_AND:
108 case TargetOpcode::G_ASHR:
109 case TargetOpcode::G_LSHR:
110 case TargetOpcode::G_MUL:
111 case TargetOpcode::G_OR:
112 case TargetOpcode::G_SHL:
113 case TargetOpcode::G_SUB:
114 case TargetOpcode::G_XOR:
115 case TargetOpcode::G_UDIV:
116 case TargetOpcode::G_SDIV:
117 case TargetOpcode::G_UREM:
118 case TargetOpcode::G_SREM: {
119 return buildBinaryOp(Opc, Dst, Src0, Src1);
122 return buildInstr(Opc).addDef(Dst).addUse(Src0).addUse(Src1);
125 // Fallback implementation of buildInstr.
126 template <typename DstTy, typename... UseArgsTy>
127 MachineInstrBuilder buildInstr(unsigned Opc, DstTy &&Ty,
128 UseArgsTy &&... Args) {
129 auto MIB = buildInstr(Opc).addDef(getDestFromArg(Ty));
130 addUsesFromArgs(MIB, std::forward<UseArgsTy>(Args)...);