1 //===- PPCBoolRetToInt.cpp - Convert bool literals to i32 if they are returned ==//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements converting i1 values to i32 if they could be more
11 // profitably allocated as GPRs rather than CRs. This pass will become totally
12 // unnecessary if Register Bank Allocation and Global Instruction Selection ever
13 // go upstream.
14 //
15 // Presently, the pass converts i1 Constants, and Arguments to i32 if the
16 // transitive closure of their uses includes only PHINodes, CallInsts, and
17 // ReturnInsts. The rational is that arguments are generally passed and returned
18 // in GPRs rather than CRs, so casting them to i32 at the LLVM IR level will
19 // actually save casts at the Machine Instruction level.
20 //
21 // It might be useful to expand this pass to add bit-wise operations to the list
22 // of safe transitive closure types. Also, we miss some opportunities when LLVM
23 // represents logical AND and OR operations with control flow rather than data
24 // flow. For example by lowering the expression: return (A && B && C)
25 //
26 // as: return A ? true : B && C.
27 //
28 // There's code in SimplifyCFG that code be used to turn control flow in data
29 // flow using SelectInsts. Selects are slow on some architectures (P7/P8), so
30 // this probably isn't good in general, but for the special case of i1, the
31 // Selects could be further lowered to bit operations that are fast everywhere.
32 //
33 //===----------------------------------------------------------------------===//
34 
35 #include "PPC.h"
36 #include "llvm/Transforms/Scalar.h"
37 #include "llvm/ADT/SmallPtrSet.h"
38 #include "llvm/ADT/Statistic.h"
39 #include "llvm/IR/Constants.h"
40 #include "llvm/IR/Dominators.h"
41 #include "llvm/IR/Instructions.h"
42 #include "llvm/IR/IntrinsicInst.h"
43 #include "llvm/Support/raw_ostream.h"
44 #include "llvm/Pass.h"
45 
46 using namespace llvm;
47 
48 namespace {
49 
50 #define DEBUG_TYPE "bool-ret-to-int"
51 
52 STATISTIC(NumBoolRetPromotion,
53           "Number of times a bool feeding a RetInst was promoted to an int");
54 STATISTIC(NumBoolCallPromotion,
55           "Number of times a bool feeding a CallInst was promoted to an int");
56 STATISTIC(NumBoolToIntPromotion,
57           "Total number of times a bool was promoted to an int");
58 
59 class PPCBoolRetToInt : public FunctionPass {
60 
61   static SmallPtrSet<Value *, 8> findAllDefs(Value *V) {
62     SmallPtrSet<Value *, 8> Defs;
63     SmallVector<Value *, 8> WorkList;
64     WorkList.push_back(V);
65     Defs.insert(V);
66     while (!WorkList.empty()) {
67       Value *Curr = WorkList.back();
68       WorkList.pop_back();
69       User *CurrUser = dyn_cast<User>(Curr);
70       // Operands of CallInst are skipped because they may not be Bool type,
71       // and their positions are defined by ABI.
72       if (CurrUser && !isa<CallInst>(Curr))
73         for (auto &Op : CurrUser->operands())
74           if (Defs.insert(Op).second)
75             WorkList.push_back(Op);
76     }
77     return Defs;
78   }
79 
80   // Translate a i1 value to an equivalent i32 value:
81   static Value *translate(Value *V) {
82     Type *Int32Ty = Type::getInt32Ty(V->getContext());
83     if (Constant *C = dyn_cast<Constant>(V))
84       return ConstantExpr::getZExt(C, Int32Ty);
85     if (PHINode *P = dyn_cast<PHINode>(V)) {
86       // Temporarily set the operands to 0. We'll fix this later in
87       // runOnUse.
88       Value *Zero = Constant::getNullValue(Int32Ty);
89       PHINode *Q =
90         PHINode::Create(Int32Ty, P->getNumIncomingValues(), P->getName(), P);
91       for (unsigned i = 0; i < P->getNumOperands(); ++i)
92         Q->addIncoming(Zero, P->getIncomingBlock(i));
93       return Q;
94     }
95 
96     Argument *A = dyn_cast<Argument>(V);
97     Instruction *I = dyn_cast<Instruction>(V);
98     assert((A || I) && "Unknown value type");
99 
100     auto InstPt =
101       A ? &*A->getParent()->getEntryBlock().begin() : I->getNextNode();
102     return new ZExtInst(V, Int32Ty, "", InstPt);
103   }
104 
105   typedef SmallPtrSet<const PHINode *, 8> PHINodeSet;
106 
107   // A PHINode is Promotable if:
108   // 1. Its type is i1 AND
109   // 2. All of its uses are ReturnInt, CallInst, PHINode, or DbgInfoIntrinsic
110   // AND
111   // 3. All of its operands are Constant or Argument or
112   //    CallInst or PHINode AND
113   // 4. All of its PHINode uses are Promotable AND
114   // 5. All of its PHINode operands are Promotable
115   static PHINodeSet getPromotablePHINodes(const Function &F) {
116     PHINodeSet Promotable;
117     // Condition 1
118     for (auto &BB : F)
119       for (auto &I : BB)
120         if (const PHINode *P = dyn_cast<PHINode>(&I))
121           if (P->getType()->isIntegerTy(1))
122             Promotable.insert(P);
123 
124     SmallVector<const PHINode *, 8> ToRemove;
125     for (const PHINode *P : Promotable) {
126       // Condition 2 and 3
127       auto IsValidUser = [] (const Value *V) -> bool {
128         return isa<ReturnInst>(V) || isa<CallInst>(V) || isa<PHINode>(V) ||
129         isa<DbgInfoIntrinsic>(V);
130       };
131       auto IsValidOperand = [] (const Value *V) -> bool {
132         return isa<Constant>(V) || isa<Argument>(V) || isa<CallInst>(V) ||
133         isa<PHINode>(V);
134       };
135       const auto &Users = P->users();
136       const auto &Operands = P->operands();
137       if (!std::all_of(Users.begin(), Users.end(), IsValidUser) ||
138           !std::all_of(Operands.begin(), Operands.end(), IsValidOperand))
139         ToRemove.push_back(P);
140     }
141 
142     // Iterate to convergence
143     auto IsPromotable = [&Promotable] (const Value *V) -> bool {
144       const PHINode *Phi = dyn_cast<PHINode>(V);
145       return !Phi || Promotable.count(Phi);
146     };
147     while (!ToRemove.empty()) {
148       for (auto &User : ToRemove)
149         Promotable.erase(User);
150       ToRemove.clear();
151 
152       for (const PHINode *P : Promotable) {
153         // Condition 4 and 5
154         const auto &Users = P->users();
155         const auto &Operands = P->operands();
156         if (!std::all_of(Users.begin(), Users.end(), IsPromotable) ||
157             !std::all_of(Operands.begin(), Operands.end(), IsPromotable))
158           ToRemove.push_back(P);
159       }
160     }
161 
162     return Promotable;
163   }
164 
165   typedef DenseMap<Value *, Value *> B2IMap;
166 
167  public:
168   static char ID;
169   PPCBoolRetToInt() : FunctionPass(ID) {
170     initializePPCBoolRetToIntPass(*PassRegistry::getPassRegistry());
171   }
172 
173   bool runOnFunction(Function &F) {
174     if (skipFunction(F))
175       return false;
176 
177     PHINodeSet PromotablePHINodes = getPromotablePHINodes(F);
178     B2IMap Bool2IntMap;
179     bool Changed = false;
180     for (auto &BB : F) {
181       for (auto &I : BB) {
182         if (ReturnInst *R = dyn_cast<ReturnInst>(&I))
183           if (F.getReturnType()->isIntegerTy(1))
184             Changed |=
185               runOnUse(R->getOperandUse(0), PromotablePHINodes, Bool2IntMap);
186 
187         if (CallInst *CI = dyn_cast<CallInst>(&I))
188           for (auto &U : CI->operands())
189             if (U->getType()->isIntegerTy(1))
190               Changed |= runOnUse(U, PromotablePHINodes, Bool2IntMap);
191       }
192     }
193 
194     return Changed;
195   }
196 
197   static bool runOnUse(Use &U, const PHINodeSet &PromotablePHINodes,
198                        B2IMap &BoolToIntMap) {
199     auto Defs = findAllDefs(U);
200 
201     // If the values are all Constants or Arguments, don't bother
202     if (!std::any_of(Defs.begin(), Defs.end(), isa<Instruction, Value *>))
203       return false;
204 
205     // Presently, we only know how to handle PHINode, Constant, Arguments and
206     // CallInst. Potentially, bitwise operations (AND, OR, XOR, NOT) and sign
207     // extension could also be handled in the future.
208     for (Value *V : Defs)
209       if (!isa<PHINode>(V) && !isa<Constant>(V) &&
210           !isa<Argument>(V) && !isa<CallInst>(V))
211         return false;
212 
213     for (Value *V : Defs)
214       if (const PHINode *P = dyn_cast<PHINode>(V))
215         if (!PromotablePHINodes.count(P))
216           return false;
217 
218     if (isa<ReturnInst>(U.getUser()))
219       ++NumBoolRetPromotion;
220     if (isa<CallInst>(U.getUser()))
221       ++NumBoolCallPromotion;
222     ++NumBoolToIntPromotion;
223 
224     for (Value *V : Defs)
225       if (!BoolToIntMap.count(V))
226         BoolToIntMap[V] = translate(V);
227 
228     // Replace the operands of the translated instructions. They were set to
229     // zero in the translate function.
230     for (auto &Pair : BoolToIntMap) {
231       User *First = dyn_cast<User>(Pair.first);
232       User *Second = dyn_cast<User>(Pair.second);
233       assert((!First || Second) && "translated from user to non-user!?");
234       // Operands of CallInst are skipped because they may not be Bool type,
235       // and their positions are defined by ABI.
236       if (First && !isa<CallInst>(First))
237         for (unsigned i = 0; i < First->getNumOperands(); ++i)
238           Second->setOperand(i, BoolToIntMap[First->getOperand(i)]);
239     }
240 
241     Value *IntRetVal = BoolToIntMap[U];
242     Type *Int1Ty = Type::getInt1Ty(U->getContext());
243     Instruction *I = cast<Instruction>(U.getUser());
244     Value *BackToBool = new TruncInst(IntRetVal, Int1Ty, "backToBool", I);
245     U.set(BackToBool);
246 
247     return true;
248   }
249 
250   void getAnalysisUsage(AnalysisUsage &AU) const {
251     AU.addPreserved<DominatorTreeWrapperPass>();
252     FunctionPass::getAnalysisUsage(AU);
253   }
254 };
255 }
256 
257 char PPCBoolRetToInt::ID = 0;
258 INITIALIZE_PASS(PPCBoolRetToInt, "bool-ret-to-int",
259                 "Convert i1 constants to i32 if they are returned",
260                 false, false)
261 
262 FunctionPass *llvm::createPPCBoolRetToIntPass() { return new PPCBoolRetToInt(); }
263