1//==- RISCVPromoteConstant.cpp - Promote constant fp to global for RISC-V --==//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
9#include "RISCV.h"
10#include "RISCVSubtarget.h"
11#include "RISCVTargetMachine.h"
12#include "llvm/ADT/DenseMap.h"
13#include "llvm/ADT/SmallVector.h"
14#include "llvm/ADT/Statistic.h"
15#include "llvm/CodeGen/TargetLowering.h"
16#include "llvm/CodeGen/TargetPassConfig.h"
17#include "llvm/IR/Analysis.h"
18#include "llvm/IR/BasicBlock.h"
19#include "llvm/IR/Constant.h"
20#include "llvm/IR/Constants.h"
21#include "llvm/IR/Function.h"
22#include "llvm/IR/GlobalValue.h"
23#include "llvm/IR/GlobalVariable.h"
24#include "llvm/IR/IRBuilder.h"
25#include "llvm/IR/InstIterator.h"
26#include "llvm/IR/Instruction.h"
27#include "llvm/IR/Instructions.h"
28#include "llvm/IR/IntrinsicInst.h"
29#include "llvm/IR/Module.h"
30#include "llvm/IR/PassManager.h"
31#include "llvm/IR/Type.h"
32#include "llvm/Pass.h"
33#include "llvm/Support/Casting.h"
34#include "llvm/Support/Debug.h"
35
36using namespace llvm;
37
38#define DEBUG_TYPE "riscv-promote-constant"
39#define RISCV_PROMOTE_CONSTANT_NAME "RISC-V Promote Constants"
40
41STATISTIC(NumPromoted, "Number of constant literals promoted to globals");
42STATISTIC(NumPromotedUses, "Number of uses of promoted literal constants");
43
44namespace {
45
46class RISCVPromoteConstantLegacy : public ModulePass {
47public:
48 static char ID;
49 RISCVPromoteConstantLegacy() : ModulePass(ID) {}
50
51 StringRef getPassName() const override { return RISCV_PROMOTE_CONSTANT_NAME; }
52
53 void getAnalysisUsage(AnalysisUsage &AU) const override {
54 AU.addRequired<TargetPassConfig>();
55 AU.setPreservesCFG();
56 }
57
58 bool runOnModule(Module &M) override;
59};
60} // end anonymous namespace
61
62char RISCVPromoteConstantLegacy::ID = 0;
63
64INITIALIZE_PASS(RISCVPromoteConstantLegacy, DEBUG_TYPE,
65 RISCV_PROMOTE_CONSTANT_NAME, false, false)
66
67ModulePass *llvm::createRISCVPromoteConstantLegacyPass() {
68 return new RISCVPromoteConstantLegacy();
69}
70
71static bool promotConstantsInFunction(Function &F,
72 const RISCVTargetLowering *TLI) {
73 if (F.hasOptNone() || F.hasOptSize())
74 return false;
75
76 // Bail out and make no transformation if the target doesn't support
77 // doubles, or if we're not targeting RV64 as we currently see some
78 // regressions for those targets.
79 if (!TLI->isTypeLegal(VT: MVT::f64) || !TLI->isTypeLegal(VT: MVT::i64))
80 return false;
81
82 // Collect all unique double constants and their uses in the function. Use
83 // MapVector to preserve insertion order.
84 MapVector<ConstantFP *, SmallVector<Use *, 8>> ConstUsesMap;
85
86 for (Instruction &I : instructions(F)) {
87 for (Use &U : I.operands()) {
88 auto *C = dyn_cast<ConstantFP>(Val: U.get());
89 if (!C || !C->getType()->isDoubleTy())
90 continue;
91 // Do not promote if it wouldn't be loaded from the constant pool.
92 if (TLI->isFPImmLegal(Imm: C->getValueAPF(), VT: MVT::f64,
93 /*ForCodeSize=*/false))
94 continue;
95 // Do not promote a constant if it is used as an immediate argument
96 // for an intrinsic.
97 if (auto *II = dyn_cast<IntrinsicInst>(Val: U.getUser())) {
98 Function *IntrinsicFunc = II->getFunction();
99 unsigned OperandIdx = U.getOperandNo();
100 if (IntrinsicFunc && IntrinsicFunc->getAttributes().hasParamAttr(
101 ArgNo: OperandIdx, Kind: Attribute::ImmArg)) {
102 LLVM_DEBUG(dbgs() << "Skipping promotion of constant in: " << *II
103 << " because operand " << OperandIdx
104 << " must be an immediate.\n");
105 continue;
106 }
107 }
108 // Note: FP args to inline asm would be problematic if we had a
109 // constraint that required an immediate floating point operand. At the
110 // time of writing LLVM doesn't recognise such a constraint.
111 ConstUsesMap[C].push_back(Elt: &U);
112 }
113 }
114
115 int PromotableConstants = ConstUsesMap.size();
116 LLVM_DEBUG(dbgs() << "Found " << PromotableConstants
117 << " promotable constants in " << F.getName() << "\n");
118 // Bail out if no promotable constants found, or if only one is found.
119 if (PromotableConstants < 2) {
120 LLVM_DEBUG(dbgs() << "Performing no promotions as insufficient promotable "
121 "constants found\n");
122 return false;
123 }
124
125 NumPromoted += PromotableConstants;
126
127 // Create a global array containing the promoted constants.
128 Module *M = F.getParent();
129 Type *DoubleTy = Type::getDoubleTy(C&: M->getContext());
130
131 SmallVector<Constant *, 16> ConstantVector;
132 for (auto const &Pair : ConstUsesMap)
133 ConstantVector.push_back(Elt: Pair.first);
134
135 ArrayType *ArrayTy = ArrayType::get(ElementType: DoubleTy, NumElements: ConstantVector.size());
136 Constant *GlobalArrayInitializer =
137 ConstantArray::get(T: ArrayTy, V: ConstantVector);
138
139 auto *GlobalArray = new GlobalVariable(
140 *M, ArrayTy,
141 /*isConstant=*/true, GlobalValue::InternalLinkage, GlobalArrayInitializer,
142 ".promoted_doubles." + F.getName());
143
144 // A cache to hold the loaded value for a given constant within a basic block.
145 DenseMap<std::pair<ConstantFP *, BasicBlock *>, Value *> LocalLoads;
146
147 // Replace all uses with the loaded value.
148 unsigned Idx = 0;
149 for (auto const &Pair : ConstUsesMap) {
150 ConstantFP *Const = Pair.first;
151 const SmallVector<Use *, 8> &Uses = Pair.second;
152
153 for (Use *U : Uses) {
154 Instruction *UserInst = cast<Instruction>(Val: U->getUser());
155 BasicBlock *InsertionBB;
156
157 // If the user is a PHI node, we must insert the load in the
158 // corresponding predecessor basic block. Otherwise, it's inserted into
159 // the same block as the use.
160 if (auto *PN = dyn_cast<PHINode>(Val: UserInst))
161 InsertionBB = PN->getIncomingBlock(U: *U);
162 else
163 InsertionBB = UserInst->getParent();
164
165 if (isa<CatchSwitchInst>(Val: InsertionBB->getTerminator())) {
166 LLVM_DEBUG(dbgs() << "Bailing out: catchswitch means thre is no valid "
167 "insertion point.\n");
168 return false;
169 }
170
171 auto CacheKey = std::make_pair(x&: Const, y&: InsertionBB);
172 Value *LoadedVal = nullptr;
173
174 // Re-use a load if it exists in the insertion block.
175 if (LocalLoads.count(Val: CacheKey)) {
176 LoadedVal = LocalLoads.at(Val: CacheKey);
177 } else {
178 // Otherwise, create a new GEP and Load at the correct insertion point.
179 // It is always safe to insert in the first insertion point in the BB,
180 // so do that and let other passes reorder.
181 IRBuilder<> Builder(InsertionBB->getFirstInsertionPt());
182 Value *ElementPtr = Builder.CreateConstInBoundsGEP2_64(
183 Ty: GlobalArray->getValueType(), Ptr: GlobalArray, Idx0: 0, Idx1: Idx, Name: "double.addr");
184 LoadedVal = Builder.CreateLoad(Ty: DoubleTy, Ptr: ElementPtr, Name: "double.val");
185
186 // Cache the newly created load for this block.
187 LocalLoads[CacheKey] = LoadedVal;
188 }
189
190 U->set(LoadedVal);
191 ++NumPromotedUses;
192 }
193 ++Idx;
194 }
195
196 return true;
197}
198
199/// Iterate over the functions and promote the double fp constants that
200/// would otherwise go into the constant pool to a constant array.
201static bool promoteConstantsInModule(Module &M, const TargetMachine &TM) {
202 bool Changed = false;
203 for (Function &F : M) {
204 if (F.isDeclaration())
205 continue;
206 const RISCVSubtarget &ST = TM.getSubtarget<RISCVSubtarget>(F);
207 const RISCVTargetLowering *TLI = ST.getTargetLowering();
208 Changed |= promotConstantsInFunction(F, TLI);
209 }
210 return Changed;
211}
212
213bool RISCVPromoteConstantLegacy::runOnModule(Module &M) {
214 if (skipModule(M))
215 return false;
216 // TargetMachine and Subtarget are needed to query isFPImmlegal.
217 const TargetPassConfig &TPC = getAnalysis<TargetPassConfig>();
218 const TargetMachine &TM = TPC.getTM<TargetMachine>();
219 return promoteConstantsInModule(M, TM);
220}
221
222PreservedAnalyses RISCVPromoteConstantPass::run(Module &M,
223 ModuleAnalysisManager &MAM) {
224 return promoteConstantsInModule(M, TM: *TM)
225 ? PreservedAnalyses::none().preserveSet<CFGAnalyses>()
226 : PreservedAnalyses::all();
227}
228