16a9226d9SEugene Zelenko //===- PPCBoolRetToInt.cpp ------------------------------------------------===//
2a1c712faSKit Barton //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6a1c712faSKit Barton //
7a1c712faSKit Barton //===----------------------------------------------------------------------===//
8a1c712faSKit Barton //
9f31c56dfSGuozhi Wei // This file implements converting i1 values to i32/i64 if they could be more
10a1c712faSKit Barton // profitably allocated as GPRs rather than CRs. This pass will become totally
11a1c712faSKit Barton // unnecessary if Register Bank Allocation and Global Instruction Selection ever
12a1c712faSKit Barton // go upstream.
13a1c712faSKit Barton //
14f31c56dfSGuozhi Wei // Presently, the pass converts i1 Constants, and Arguments to i32/i64 if the
15a1c712faSKit Barton // transitive closure of their uses includes only PHINodes, CallInsts, and
16a1c712faSKit Barton // ReturnInsts. The rational is that arguments are generally passed and returned
17f31c56dfSGuozhi Wei // in GPRs rather than CRs, so casting them to i32/i64 at the LLVM IR level will
18a1c712faSKit Barton // actually save casts at the Machine Instruction level.
19a1c712faSKit Barton //
20a1c712faSKit Barton // It might be useful to expand this pass to add bit-wise operations to the list
21a1c712faSKit Barton // of safe transitive closure types. Also, we miss some opportunities when LLVM
22a1c712faSKit Barton // represents logical AND and OR operations with control flow rather than data
23a1c712faSKit Barton // flow. For example by lowering the expression: return (A && B && C)
24a1c712faSKit Barton //
25a1c712faSKit Barton // as: return A ? true : B && C.
26a1c712faSKit Barton //
27a1c712faSKit Barton // There's code in SimplifyCFG that code be used to turn control flow in data
28a1c712faSKit Barton // flow using SelectInsts. Selects are slow on some architectures (P7/P8), so
29a1c712faSKit Barton // this probably isn't good in general, but for the special case of i1, the
30a1c712faSKit Barton // Selects could be further lowered to bit operations that are fast everywhere.
31a1c712faSKit Barton //
32a1c712faSKit Barton //===----------------------------------------------------------------------===//
33a1c712faSKit Barton 
34a1c712faSKit Barton #include "PPC.h"
35f31c56dfSGuozhi Wei #include "PPCTargetMachine.h"
366a9226d9SEugene Zelenko #include "llvm/ADT/DenseMap.h"
376bda14b3SChandler Carruth #include "llvm/ADT/STLExtras.h"
38a1c712faSKit Barton #include "llvm/ADT/SmallPtrSet.h"
396a9226d9SEugene Zelenko #include "llvm/ADT/SmallVector.h"
40a1c712faSKit Barton #include "llvm/ADT/Statistic.h"
416a9226d9SEugene Zelenko #include "llvm/IR/Argument.h"
42a1c712faSKit Barton #include "llvm/IR/Constants.h"
43a1c712faSKit Barton #include "llvm/IR/Dominators.h"
446a9226d9SEugene Zelenko #include "llvm/IR/Function.h"
456a9226d9SEugene Zelenko #include "llvm/IR/Instruction.h"
46a1c712faSKit Barton #include "llvm/IR/Instructions.h"
47a1c712faSKit Barton #include "llvm/IR/IntrinsicInst.h"
486a9226d9SEugene Zelenko #include "llvm/IR/OperandTraits.h"
496a9226d9SEugene Zelenko #include "llvm/IR/Type.h"
506a9226d9SEugene Zelenko #include "llvm/IR/Use.h"
516a9226d9SEugene Zelenko #include "llvm/IR/User.h"
526a9226d9SEugene Zelenko #include "llvm/IR/Value.h"
53a1c712faSKit Barton #include "llvm/Pass.h"
54f31c56dfSGuozhi Wei #include "llvm/CodeGen/TargetPassConfig.h"
556bda14b3SChandler Carruth #include "llvm/Support/Casting.h"
566a9226d9SEugene Zelenko #include <cassert>
57a1c712faSKit Barton 
58a1c712faSKit Barton using namespace llvm;
59a1c712faSKit Barton 
60a1c712faSKit Barton namespace {
61a1c712faSKit Barton 
621bf4629fSArthur Eubanks #define DEBUG_TYPE "ppc-bool-ret-to-int"
63a1c712faSKit Barton 
64a1c712faSKit Barton STATISTIC(NumBoolRetPromotion,
65a1c712faSKit Barton           "Number of times a bool feeding a RetInst was promoted to an int");
66a1c712faSKit Barton STATISTIC(NumBoolCallPromotion,
67a1c712faSKit Barton           "Number of times a bool feeding a CallInst was promoted to an int");
68a1c712faSKit Barton STATISTIC(NumBoolToIntPromotion,
69a1c712faSKit Barton           "Total number of times a bool was promoted to an int");
70a1c712faSKit Barton 
71a1c712faSKit Barton class PPCBoolRetToInt : public FunctionPass {
findAllDefs(Value * V)72a1c712faSKit Barton   static SmallPtrSet<Value *, 8> findAllDefs(Value *V) {
73a1c712faSKit Barton     SmallPtrSet<Value *, 8> Defs;
74a1c712faSKit Barton     SmallVector<Value *, 8> WorkList;
75a1c712faSKit Barton     WorkList.push_back(V);
76a1c712faSKit Barton     Defs.insert(V);
77a1c712faSKit Barton     while (!WorkList.empty()) {
78*16baad8fSKazu Hirata       Value *Curr = WorkList.pop_back_val();
796a9226d9SEugene Zelenko       auto *CurrUser = dyn_cast<User>(Curr);
80cbea1756SKai Luo       // Operands of CallInst/Constant are skipped because they may not be Bool
81cbea1756SKai Luo       // type. For CallInst, their positions are defined by ABI.
82cbea1756SKai Luo       if (CurrUser && !isa<CallInst>(Curr) && !isa<Constant>(Curr))
83a1c712faSKit Barton         for (auto &Op : CurrUser->operands())
84a1c712faSKit Barton           if (Defs.insert(Op).second)
85a1c712faSKit Barton             WorkList.push_back(Op);
86a1c712faSKit Barton     }
87a1c712faSKit Barton     return Defs;
88a1c712faSKit Barton   }
89a1c712faSKit Barton 
90f31c56dfSGuozhi Wei   // Translate a i1 value to an equivalent i32/i64 value:
translate(Value * V)91f31c56dfSGuozhi Wei   Value *translate(Value *V) {
92cbea1756SKai Luo     assert(V->getType() == Type::getInt1Ty(V->getContext()) &&
93cbea1756SKai Luo            "Expect an i1 value");
94cbea1756SKai Luo 
95f31c56dfSGuozhi Wei     Type *IntTy = ST->isPPC64() ? Type::getInt64Ty(V->getContext())
96f31c56dfSGuozhi Wei                                 : Type::getInt32Ty(V->getContext());
97f31c56dfSGuozhi Wei 
986a9226d9SEugene Zelenko     if (auto *C = dyn_cast<Constant>(V))
99f31c56dfSGuozhi Wei       return ConstantExpr::getZExt(C, IntTy);
1006a9226d9SEugene Zelenko     if (auto *P = dyn_cast<PHINode>(V)) {
101a1c712faSKit Barton       // Temporarily set the operands to 0. We'll fix this later in
102a1c712faSKit Barton       // runOnUse.
103f31c56dfSGuozhi Wei       Value *Zero = Constant::getNullValue(IntTy);
104a1c712faSKit Barton       PHINode *Q =
105f31c56dfSGuozhi Wei         PHINode::Create(IntTy, P->getNumIncomingValues(), P->getName(), P);
106a1c712faSKit Barton       for (unsigned i = 0; i < P->getNumOperands(); ++i)
107a1c712faSKit Barton         Q->addIncoming(Zero, P->getIncomingBlock(i));
108a1c712faSKit Barton       return Q;
109a1c712faSKit Barton     }
110a1c712faSKit Barton 
1116a9226d9SEugene Zelenko     auto *A = dyn_cast<Argument>(V);
1126a9226d9SEugene Zelenko     auto *I = dyn_cast<Instruction>(V);
113a1c712faSKit Barton     assert((A || I) && "Unknown value type");
114a1c712faSKit Barton 
115a1c712faSKit Barton     auto InstPt =
116a1c712faSKit Barton       A ? &*A->getParent()->getEntryBlock().begin() : I->getNextNode();
117f31c56dfSGuozhi Wei     return new ZExtInst(V, IntTy, "", InstPt);
118a1c712faSKit Barton   }
119a1c712faSKit Barton 
120a1c712faSKit Barton   typedef SmallPtrSet<const PHINode *, 8> PHINodeSet;
121a1c712faSKit Barton 
122a1c712faSKit Barton   // A PHINode is Promotable if:
123a1c712faSKit Barton   // 1. Its type is i1 AND
124a1c712faSKit Barton   // 2. All of its uses are ReturnInt, CallInst, PHINode, or DbgInfoIntrinsic
125a1c712faSKit Barton   // AND
126a1c712faSKit Barton   // 3. All of its operands are Constant or Argument or
127a1c712faSKit Barton   //    CallInst or PHINode AND
128a1c712faSKit Barton   // 4. All of its PHINode uses are Promotable AND
129a1c712faSKit Barton   // 5. All of its PHINode operands are Promotable
getPromotablePHINodes(const Function & F)130a1c712faSKit Barton   static PHINodeSet getPromotablePHINodes(const Function &F) {
131a1c712faSKit Barton     PHINodeSet Promotable;
132a1c712faSKit Barton     // Condition 1
133a1c712faSKit Barton     for (auto &BB : F)
134a1c712faSKit Barton       for (auto &I : BB)
1356a9226d9SEugene Zelenko         if (const auto *P = dyn_cast<PHINode>(&I))
136a1c712faSKit Barton           if (P->getType()->isIntegerTy(1))
137a1c712faSKit Barton             Promotable.insert(P);
138a1c712faSKit Barton 
139a1c712faSKit Barton     SmallVector<const PHINode *, 8> ToRemove;
140451f54cfSBenjamin Kramer     for (const PHINode *P : Promotable) {
141a1c712faSKit Barton       // Condition 2 and 3
142a1c712faSKit Barton       auto IsValidUser = [] (const Value *V) -> bool {
143a1c712faSKit Barton         return isa<ReturnInst>(V) || isa<CallInst>(V) || isa<PHINode>(V) ||
144a1c712faSKit Barton         isa<DbgInfoIntrinsic>(V);
145a1c712faSKit Barton       };
146a1c712faSKit Barton       auto IsValidOperand = [] (const Value *V) -> bool {
147a1c712faSKit Barton         return isa<Constant>(V) || isa<Argument>(V) || isa<CallInst>(V) ||
148a1c712faSKit Barton         isa<PHINode>(V);
149a1c712faSKit Barton       };
150a1c712faSKit Barton       const auto &Users = P->users();
151a1c712faSKit Barton       const auto &Operands = P->operands();
1526a9226d9SEugene Zelenko       if (!llvm::all_of(Users, IsValidUser) ||
1536a9226d9SEugene Zelenko           !llvm::all_of(Operands, IsValidOperand))
154a1c712faSKit Barton         ToRemove.push_back(P);
155a1c712faSKit Barton     }
156a1c712faSKit Barton 
157a1c712faSKit Barton     // Iterate to convergence
158a1c712faSKit Barton     auto IsPromotable = [&Promotable] (const Value *V) -> bool {
1596a9226d9SEugene Zelenko       const auto *Phi = dyn_cast<PHINode>(V);
160a1c712faSKit Barton       return !Phi || Promotable.count(Phi);
161a1c712faSKit Barton     };
162a1c712faSKit Barton     while (!ToRemove.empty()) {
163a1c712faSKit Barton       for (auto &User : ToRemove)
164a1c712faSKit Barton         Promotable.erase(User);
165a1c712faSKit Barton       ToRemove.clear();
166a1c712faSKit Barton 
167451f54cfSBenjamin Kramer       for (const PHINode *P : Promotable) {
168a1c712faSKit Barton         // Condition 4 and 5
169a1c712faSKit Barton         const auto &Users = P->users();
170a1c712faSKit Barton         const auto &Operands = P->operands();
1716a9226d9SEugene Zelenko         if (!llvm::all_of(Users, IsPromotable) ||
1726a9226d9SEugene Zelenko             !llvm::all_of(Operands, IsPromotable))
173a1c712faSKit Barton           ToRemove.push_back(P);
174a1c712faSKit Barton       }
175a1c712faSKit Barton     }
176a1c712faSKit Barton 
177a1c712faSKit Barton     return Promotable;
178a1c712faSKit Barton   }
179a1c712faSKit Barton 
180a1c712faSKit Barton   typedef DenseMap<Value *, Value *> B2IMap;
181a1c712faSKit Barton 
182a1c712faSKit Barton  public:
183a1c712faSKit Barton   static char ID;
1846a9226d9SEugene Zelenko 
PPCBoolRetToInt()1859fd267c2SEric Christopher   PPCBoolRetToInt() : FunctionPass(ID) {
186a1c712faSKit Barton     initializePPCBoolRetToIntPass(*PassRegistry::getPassRegistry());
187a1c712faSKit Barton   }
188a1c712faSKit Barton 
runOnFunction(Function & F)1896a9226d9SEugene Zelenko   bool runOnFunction(Function &F) override {
190289bd5f6SAndrew Kaylor     if (skipFunction(F))
191289bd5f6SAndrew Kaylor       return false;
192289bd5f6SAndrew Kaylor 
193f31c56dfSGuozhi Wei     auto *TPC = getAnalysisIfAvailable<TargetPassConfig>();
194f31c56dfSGuozhi Wei     if (!TPC)
195f31c56dfSGuozhi Wei       return false;
196f31c56dfSGuozhi Wei 
197f31c56dfSGuozhi Wei     auto &TM = TPC->getTM<PPCTargetMachine>();
198f31c56dfSGuozhi Wei     ST = TM.getSubtargetImpl(F);
199f31c56dfSGuozhi Wei 
200a1c712faSKit Barton     PHINodeSet PromotablePHINodes = getPromotablePHINodes(F);
201a1c712faSKit Barton     B2IMap Bool2IntMap;
202a1c712faSKit Barton     bool Changed = false;
203a1c712faSKit Barton     for (auto &BB : F) {
204a1c712faSKit Barton       for (auto &I : BB) {
2056a9226d9SEugene Zelenko         if (auto *R = dyn_cast<ReturnInst>(&I))
206a1c712faSKit Barton           if (F.getReturnType()->isIntegerTy(1))
207a1c712faSKit Barton             Changed |=
208a1c712faSKit Barton               runOnUse(R->getOperandUse(0), PromotablePHINodes, Bool2IntMap);
209a1c712faSKit Barton 
2106a9226d9SEugene Zelenko         if (auto *CI = dyn_cast<CallInst>(&I))
211a1c712faSKit Barton           for (auto &U : CI->operands())
212a1c712faSKit Barton             if (U->getType()->isIntegerTy(1))
213a1c712faSKit Barton               Changed |= runOnUse(U, PromotablePHINodes, Bool2IntMap);
214a1c712faSKit Barton       }
215a1c712faSKit Barton     }
216a1c712faSKit Barton 
217a1c712faSKit Barton     return Changed;
218a1c712faSKit Barton   }
219a1c712faSKit Barton 
runOnUse(Use & U,const PHINodeSet & PromotablePHINodes,B2IMap & BoolToIntMap)220f31c56dfSGuozhi Wei   bool runOnUse(Use &U, const PHINodeSet &PromotablePHINodes,
221a1c712faSKit Barton                        B2IMap &BoolToIntMap) {
222a1c712faSKit Barton     auto Defs = findAllDefs(U);
223a1c712faSKit Barton 
224a1c712faSKit Barton     // If the values are all Constants or Arguments, don't bother
22572d20b96SRahul Joshi     if (llvm::none_of(Defs, [](Value *V) { return isa<Instruction>(V); }))
226a1c712faSKit Barton       return false;
227a1c712faSKit Barton 
2289584d18dSGuozhi Wei     // Presently, we only know how to handle PHINode, Constant, Arguments and
2299584d18dSGuozhi Wei     // CallInst. Potentially, bitwise operations (AND, OR, XOR, NOT) and sign
2309584d18dSGuozhi Wei     // extension could also be handled in the future.
231451f54cfSBenjamin Kramer     for (Value *V : Defs)
2329584d18dSGuozhi Wei       if (!isa<PHINode>(V) && !isa<Constant>(V) &&
2339584d18dSGuozhi Wei           !isa<Argument>(V) && !isa<CallInst>(V))
234a1c712faSKit Barton         return false;
235a1c712faSKit Barton 
236451f54cfSBenjamin Kramer     for (Value *V : Defs)
2376a9226d9SEugene Zelenko       if (const auto *P = dyn_cast<PHINode>(V))
238a1c712faSKit Barton         if (!PromotablePHINodes.count(P))
239a1c712faSKit Barton           return false;
240a1c712faSKit Barton 
241a1c712faSKit Barton     if (isa<ReturnInst>(U.getUser()))
242a1c712faSKit Barton       ++NumBoolRetPromotion;
243a1c712faSKit Barton     if (isa<CallInst>(U.getUser()))
244a1c712faSKit Barton       ++NumBoolCallPromotion;
245a1c712faSKit Barton     ++NumBoolToIntPromotion;
246a1c712faSKit Barton 
247451f54cfSBenjamin Kramer     for (Value *V : Defs)
248a1c712faSKit Barton       if (!BoolToIntMap.count(V))
249a1c712faSKit Barton         BoolToIntMap[V] = translate(V);
250a1c712faSKit Barton 
2519584d18dSGuozhi Wei     // Replace the operands of the translated instructions. They were set to
252a1c712faSKit Barton     // zero in the translate function.
253a1c712faSKit Barton     for (auto &Pair : BoolToIntMap) {
2546a9226d9SEugene Zelenko       auto *First = dyn_cast<User>(Pair.first);
2556a9226d9SEugene Zelenko       auto *Second = dyn_cast<User>(Pair.second);
256a1c712faSKit Barton       assert((!First || Second) && "translated from user to non-user!?");
257cbea1756SKai Luo       // Operands of CallInst/Constant are skipped because they may not be Bool
258cbea1756SKai Luo       // type. For CallInst, their positions are defined by ABI.
259cbea1756SKai Luo       if (First && !isa<CallInst>(First) && !isa<Constant>(First))
260a1c712faSKit Barton         for (unsigned i = 0; i < First->getNumOperands(); ++i)
261a1c712faSKit Barton           Second->setOperand(i, BoolToIntMap[First->getOperand(i)]);
262a1c712faSKit Barton     }
263a1c712faSKit Barton 
264a1c712faSKit Barton     Value *IntRetVal = BoolToIntMap[U];
265a1c712faSKit Barton     Type *Int1Ty = Type::getInt1Ty(U->getContext());
2666a9226d9SEugene Zelenko     auto *I = cast<Instruction>(U.getUser());
267a1c712faSKit Barton     Value *BackToBool = new TruncInst(IntRetVal, Int1Ty, "backToBool", I);
268a1c712faSKit Barton     U.set(BackToBool);
269a1c712faSKit Barton 
270a1c712faSKit Barton     return true;
271a1c712faSKit Barton   }
272a1c712faSKit Barton 
getAnalysisUsage(AnalysisUsage & AU) const2736a9226d9SEugene Zelenko   void getAnalysisUsage(AnalysisUsage &AU) const override {
274a1c712faSKit Barton     AU.addPreserved<DominatorTreeWrapperPass>();
275a1c712faSKit Barton     FunctionPass::getAnalysisUsage(AU);
276a1c712faSKit Barton   }
277f31c56dfSGuozhi Wei 
278f31c56dfSGuozhi Wei private:
279f31c56dfSGuozhi Wei   const PPCSubtarget *ST;
280a1c712faSKit Barton };
2816a9226d9SEugene Zelenko 
2826a9226d9SEugene Zelenko } // end anonymous namespace
283a1c712faSKit Barton 
284a1c712faSKit Barton char PPCBoolRetToInt::ID = 0;
2851bf4629fSArthur Eubanks INITIALIZE_PASS(PPCBoolRetToInt, "ppc-bool-ret-to-int",
2861bf4629fSArthur Eubanks                 "Convert i1 constants to i32/i64 if they are returned", false,
2871bf4629fSArthur Eubanks                 false)
288a1c712faSKit Barton 
createPPCBoolRetToIntPass()2899fd267c2SEric Christopher FunctionPass *llvm::createPPCBoolRetToIntPass() { return new PPCBoolRetToInt(); }
290