LLVM 19.0.0git
ReplaceConstant.cpp
Go to the documentation of this file.
1//===- ReplaceConstant.cpp - Replace LLVM constant expression--------------===//
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// This file implements a utility function for replacing LLVM constant
10// expressions by instructions.
11//
12//===----------------------------------------------------------------------===//
13
15#include "llvm/ADT/SetVector.h"
16#include "llvm/IR/Constants.h"
18
19namespace llvm {
20
21static bool isExpandableUser(User *U) {
22 return isa<ConstantExpr>(U) || isa<ConstantAggregate>(U);
23}
24
26 Constant *C) {
28 if (auto *CE = dyn_cast<ConstantExpr>(C)) {
29 Instruction *ConstInst = CE->getAsInstruction();
30 ConstInst->insertBefore(*InsertPt->getParent(), InsertPt);
31 NewInsts.push_back(ConstInst);
32 } else if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) {
33 Value *V = PoisonValue::get(C->getType());
34 for (auto [Idx, Op] : enumerate(C->operands())) {
35 V = InsertValueInst::Create(V, Op, Idx, "", InsertPt);
36 NewInsts.push_back(cast<Instruction>(V));
37 }
38 } else if (isa<ConstantVector>(C)) {
39 Type *IdxTy = Type::getInt32Ty(C->getContext());
40 Value *V = PoisonValue::get(C->getType());
41 for (auto [Idx, Op] : enumerate(C->operands())) {
42 V = InsertElementInst::Create(V, Op, ConstantInt::get(IdxTy, Idx), "",
43 InsertPt);
44 NewInsts.push_back(cast<Instruction>(V));
45 }
46 } else {
47 llvm_unreachable("Not an expandable user");
48 }
49 return NewInsts;
50}
51
53 Function *RestrictToFunc,
54 bool RemoveDeadConstants) {
55 // Find all expandable direct users of Consts.
57 for (Constant *C : Consts)
58 for (User *U : C->users())
59 if (isExpandableUser(U))
60 Stack.push_back(cast<Constant>(U));
61
62 // Include transitive users.
63 SetVector<Constant *> ExpandableUsers;
64 while (!Stack.empty()) {
65 Constant *C = Stack.pop_back_val();
66 if (!ExpandableUsers.insert(C))
67 continue;
68
69 for (auto *Nested : C->users())
70 if (isExpandableUser(Nested))
71 Stack.push_back(cast<Constant>(Nested));
72 }
73
74 // Find all instructions that use any of the expandable users
76 for (Constant *C : ExpandableUsers)
77 for (User *U : C->users())
78 if (auto *I = dyn_cast<Instruction>(U))
79 if (!RestrictToFunc || I->getFunction() == RestrictToFunc)
80 InstructionWorklist.insert(I);
81
82 // Replace those expandable operands with instructions
83 bool Changed = false;
84 while (!InstructionWorklist.empty()) {
85 Instruction *I = InstructionWorklist.pop_back_val();
86 DebugLoc Loc = I->getDebugLoc();
87 for (Use &U : I->operands()) {
88 BasicBlock::iterator BI = I->getIterator();
89 if (auto *Phi = dyn_cast<PHINode>(I)) {
90 BasicBlock *BB = Phi->getIncomingBlock(U);
91 BI = BB->getFirstInsertionPt();
92 assert(BI != BB->end() && "Unexpected empty basic block");
93 }
94
95 if (auto *C = dyn_cast<Constant>(U.get())) {
96 if (ExpandableUsers.contains(C)) {
97 Changed = true;
98 auto NewInsts = expandUser(BI, C);
99 for (auto *NI : NewInsts)
100 NI->setDebugLoc(Loc);
101 InstructionWorklist.insert(NewInsts.begin(), NewInsts.end());
102 U.set(NewInsts.back());
103 }
104 }
105 }
106 }
107
108 if (RemoveDeadConstants)
109 for (Constant *C : Consts)
110 C->removeDeadConstantUsers();
111
112 return Changed;
113}
114
115} // namespace llvm
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
#define I(x, y, z)
Definition: MD5.cpp:58
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file implements a set that has insertion order iteration characteristics.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
LLVM Basic Block Representation.
Definition: BasicBlock.h:60
iterator end()
Definition: BasicBlock.h:443
const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
Definition: BasicBlock.cpp:409
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:165
This is an important base class in LLVM.
Definition: Constant.h:41
This class represents an Operation in the Expression.
A debug info location.
Definition: DebugLoc.h:33
static InsertElementInst * Create(Value *Vec, Value *NewElt, Value *Idx, const Twine &NameStr, BasicBlock::iterator InsertBefore)
static InsertValueInst * Create(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Twine &NameStr, BasicBlock::iterator InsertBefore)
InstructionWorklist - This is the worklist management logic for InstCombine and other simplification ...
void insertBefore(Instruction *InsertPos)
Insert an unlinked instruction into a basic block immediately before the specified instruction.
static PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Definition: Constants.cpp:1814
A vector that has set insertion semantics.
Definition: SetVector.h:57
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition: SetVector.h:162
bool contains(const key_type &key) const
Check if the SetVector contains the given key.
Definition: SetVector.h:254
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
static IntegerType * getInt32Ty(LLVMContext &C)
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
LLVM Value Representation.
Definition: Value.h:74
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
static SmallVector< Instruction *, 4 > expandUser(BasicBlock::iterator InsertPt, Constant *C)
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are are tuples (A,...
Definition: STLExtras.h:2400
bool convertUsersOfConstantsToInstructions(ArrayRef< Constant * > Consts, Function *RestrictToFunc=nullptr, bool RemoveDeadConstants=true)
Replace constant expressions users of the given constants with instructions.
static bool isExpandableUser(User *U)