62 #define DEBUG_TYPE "bool-ret-to-int" 65 "Number of times a bool feeding a RetInst was promoted to an int");
67 "Number of times a bool feeding a CallInst was promoted to an int");
69 "Total number of times a bool was promoted to an int");
77 while (!WorkList.
empty()) {
83 if (CurrUser && !isa<CallInst>(Curr))
84 for (
auto &
Op : CurrUser->operands())
96 if (
auto *
C = dyn_cast<Constant>(V))
98 if (
auto *
P = dyn_cast<PHINode>(V)) {
104 for (
unsigned i = 0; i <
P->getNumOperands(); ++i)
111 assert((A || I) &&
"Unknown value type");
114 A ? &*A->getParent()->getEntryBlock().begin() : I->getNextNode();
115 return new ZExtInst(V, IntTy,
"", InstPt);
128 static PHINodeSet getPromotablePHINodes(
const Function &
F) {
129 PHINodeSet Promotable;
133 if (
const auto *
P = dyn_cast<PHINode>(&I))
134 if (
P->getType()->isIntegerTy(1))
138 for (
const PHINode *
P : Promotable) {
140 auto IsValidUser = [] (
const Value *V) ->
bool {
141 return isa<ReturnInst>(V) || isa<CallInst>(V) || isa<PHINode>(V) ||
142 isa<DbgInfoIntrinsic>(V);
144 auto IsValidOperand = [] (
const Value *V) ->
bool {
145 return isa<Constant>(V) || isa<Argument>(V) || isa<CallInst>(V) ||
148 const auto &
Users =
P->users();
156 auto IsPromotable = [&Promotable] (
const Value *V) ->
bool {
158 return !Phi || Promotable.count(Phi);
160 while (!ToRemove.
empty()) {
161 for (
auto &
User : ToRemove)
162 Promotable.erase(
User);
165 for (
const PHINode *
P : Promotable) {
167 const auto &
Users =
P->users();
171 ToRemove.push_back(
P);
191 auto *TPC = getAnalysisIfAvailable<TargetPassConfig>();
196 ST =
TM.getSubtargetImpl(F);
198 PHINodeSet PromotablePHINodes = getPromotablePHINodes(F);
200 bool Changed =
false;
203 if (
auto *R = dyn_cast<ReturnInst>(&I))
204 if (F.getReturnType()->isIntegerTy(1))
206 runOnUse(R->getOperandUse(0), PromotablePHINodes, Bool2IntMap);
208 if (
auto *CI = dyn_cast<CallInst>(&I))
209 for (
auto &U : CI->operands())
210 if (U->getType()->isIntegerTy(1))
211 Changed |= runOnUse(U, PromotablePHINodes, Bool2IntMap);
218 bool runOnUse(
Use &U,
const PHINodeSet &PromotablePHINodes,
219 B2IMap &BoolToIntMap) {
220 auto Defs = findAllDefs(U);
229 for (
Value *V : Defs)
230 if (!isa<PHINode>(V) && !isa<Constant>(V) &&
231 !isa<Argument>(V) && !isa<CallInst>(V))
234 for (
Value *V : Defs)
235 if (
const auto *
P = dyn_cast<PHINode>(V))
236 if (!PromotablePHINodes.count(
P))
239 if (isa<ReturnInst>(U.
getUser()))
240 ++NumBoolRetPromotion;
241 if (isa<CallInst>(U.
getUser()))
242 ++NumBoolCallPromotion;
243 ++NumBoolToIntPromotion;
245 for (
Value *V : Defs)
246 if (!BoolToIntMap.count(V))
247 BoolToIntMap[V] = translate(V);
251 for (
auto &Pair : BoolToIntMap) {
254 assert((!First || Second) &&
"translated from user to non-user!?");
257 if (First && !isa<CallInst>(First))
258 for (
unsigned i = 0; i < First->getNumOperands(); ++i)
259 Second->setOperand(i, BoolToIntMap[First->getOperand(i)]);
262 Value *IntRetVal = BoolToIntMap[U];
264 auto *I = cast<Instruction>(U.
getUser());
265 Value *BackToBool =
new TruncInst(IntRetVal, Int1Ty,
"backToBool", I);
284 "Convert i1 constants to i32/i64 if they are returned",
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
static IntegerType * getInt1Ty(LLVMContext &C)
INITIALIZE_PASS(PPCBoolRetToInt, "bool-ret-to-int", "Convert i1 constants to i32/i64 if they are returned", false, false) FunctionPass *llvm
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...
This class represents an incoming formal argument to a Function.
This class represents lattice values for constants.
This class represents zero extension of integer types.
void push_back(const T &Elt)
LLVMContext & getContext() const
All values hold a context through their type.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly...
STATISTIC(NumFunctions, "Total number of functions")
static IntegerType * getInt64Ty(LLVMContext &C)
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...
mir Rename Register Operands
static Constant * getZExt(Constant *C, Type *Ty, bool OnlyIfReduced=false)
User * getUser() const LLVM_READONLY
Returns the User that contains this Use.
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
FunctionPass * createPPCBoolRetToIntPass()
This class represents a truncation of integer types.
void initializePPCBoolRetToIntPass(PassRegistry &)
static bool runOnFunction(Function &F, bool PostInlining)
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.
FunctionPass class - This class is used to implement most global optimizations.
Common code between 32-bit and 64-bit PowerPC targets.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
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 IntegerType * getInt32Ty(LLVMContext &C)
LLVM_NODISCARD bool empty() const
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.