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