61 #define DEBUG_TYPE "bool-ret-to-int"
64 "Number of times a bool feeding a RetInst was promoted to an int");
66 "Number of times a bool feeding a CallInst was promoted to an int");
68 "Total number of times a bool was promoted to an int");
76 while (!WorkList.
empty()) {
82 if (CurrUser && !isa<CallInst>(Curr))
83 for (
auto &
Op : CurrUser->operands())
93 if (
auto *
C = dyn_cast<Constant>(V))
95 if (
auto *
P = dyn_cast<PHINode>(V)) {
101 for (
unsigned i = 0;
i <
P->getNumOperands(); ++
i)
108 assert((
A || I) &&
"Unknown value type");
111 A ? &*
A->getParent()->getEntryBlock().begin() : I->getNextNode();
112 return new ZExtInst(V, Int32Ty,
"", InstPt);
125 static PHINodeSet getPromotablePHINodes(
const Function &
F) {
126 PHINodeSet Promotable;
130 if (
const auto *
P = dyn_cast<PHINode>(&I))
131 if (
P->getType()->isIntegerTy(1))
135 for (
const PHINode *
P : Promotable) {
137 auto IsValidUser = [] (
const Value *V) ->
bool {
138 return isa<ReturnInst>(V) || isa<CallInst>(V) || isa<PHINode>(V) ||
139 isa<DbgInfoIntrinsic>(V);
141 auto IsValidOperand = [] (
const Value *V) ->
bool {
142 return isa<Constant>(V) || isa<Argument>(V) || isa<CallInst>(V) ||
145 const auto &
Users =
P->users();
146 const auto &Operands =
P->operands();
153 auto IsPromotable = [&Promotable] (
const Value *V) ->
bool {
155 return !Phi || Promotable.count(Phi);
157 while (!ToRemove.
empty()) {
158 for (
auto &
User : ToRemove)
159 Promotable.erase(
User);
162 for (
const PHINode *
P : Promotable) {
164 const auto &
Users =
P->users();
165 const auto &Operands =
P->operands();
168 ToRemove.push_back(
P);
184 bool runOnFunction(
Function &F)
override {
188 PHINodeSet PromotablePHINodes = getPromotablePHINodes(F);
190 bool Changed =
false;
193 if (
auto *R = dyn_cast<ReturnInst>(&I))
194 if (F.getReturnType()->isIntegerTy(1))
196 runOnUse(R->getOperandUse(0), PromotablePHINodes, Bool2IntMap);
198 if (
auto *CI = dyn_cast<CallInst>(&I))
199 for (
auto &U : CI->operands())
200 if (U->getType()->isIntegerTy(1))
201 Changed |= runOnUse(U, PromotablePHINodes, Bool2IntMap);
208 static bool runOnUse(
Use &U,
const PHINodeSet &PromotablePHINodes,
209 B2IMap &BoolToIntMap) {
210 auto Defs = findAllDefs(U);
219 for (
Value *V : Defs)
220 if (!isa<PHINode>(V) && !isa<Constant>(V) &&
221 !isa<Argument>(V) && !isa<CallInst>(V))
224 for (
Value *V : Defs)
225 if (
const auto *
P = dyn_cast<PHINode>(V))
226 if (!PromotablePHINodes.count(
P))
229 if (isa<ReturnInst>(U.
getUser()))
230 ++NumBoolRetPromotion;
231 if (isa<CallInst>(U.
getUser()))
232 ++NumBoolCallPromotion;
233 ++NumBoolToIntPromotion;
235 for (
Value *V : Defs)
236 if (!BoolToIntMap.count(V))
237 BoolToIntMap[V] = translate(V);
241 for (
auto &Pair : BoolToIntMap) {
244 assert((!First || Second) &&
"translated from user to non-user!?");
247 if (First && !isa<CallInst>(First))
248 for (
unsigned i = 0;
i < First->getNumOperands(); ++
i)
249 Second->setOperand(
i, BoolToIntMap[First->getOperand(
i)]);
252 Value *IntRetVal = BoolToIntMap[U];
254 auto *I = cast<Instruction>(U.
getUser());
255 Value *BackToBool =
new TruncInst(IntRetVal, Int1Ty,
"backToBool", I);
271 "Convert i1 constants to i32 if they are returned",
void push_back(const T &Elt)
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
static IntegerType * getInt1Ty(LLVMContext &C)
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
LLVM Argument representation.
STATISTIC(NumFunctions,"Total number of functions")
This class represents zero extension of integer types.
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly...
iv Induction Variable Users
This defines the Use class.
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
A Use represents the edge between a Value definition and its users.
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly...
LLVM_NODISCARD bool empty() const
static Constant * getZExt(Constant *C, Type *Ty, bool OnlyIfReduced=false)
FunctionPass * createPPCBoolRetToIntPass()
This class represents a truncation of integer types.
void initializePPCBoolRetToIntPass(PassRegistry &)
The instances of the Type class are immutable: once they are created, they are never changed...
This file contains the declarations for the subclasses of Constant, which represent the different fla...
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Represent the analysis usage information of a pass.
User * getUser() const
Returns the User that contains this Use.
FunctionPass class - This class is used to implement most global optimizations.
LLVMContext & getContext() const
All values hold a context through their type.
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
static GCRegistry::Add< ShadowStackGC > C("shadow-stack","Very portable GC for uncooperative code generators")
INITIALIZE_PASS(PPCBoolRetToInt,"bool-ret-to-int","Convert i1 constants to i32 if they are returned", false, false) FunctionPass *llvm
static IntegerType * getInt32Ty(LLVMContext &C)
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Legacy analysis pass which computes a DominatorTree.
static GCRegistry::Add< ErlangGC > A("erlang","erlang-compatible garbage collector")