1b915e9e0SDimitry Andric //===- PPCBoolRetToInt.cpp ------------------------------------------------===//
2dd58ef01SDimitry Andric //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6dd58ef01SDimitry Andric //
7dd58ef01SDimitry Andric //===----------------------------------------------------------------------===//
8dd58ef01SDimitry Andric //
97ab83427SDimitry Andric // This file implements converting i1 values to i32/i64 if they could be more
10dd58ef01SDimitry Andric // profitably allocated as GPRs rather than CRs. This pass will become totally
11dd58ef01SDimitry Andric // unnecessary if Register Bank Allocation and Global Instruction Selection ever
12dd58ef01SDimitry Andric // go upstream.
13dd58ef01SDimitry Andric //
147ab83427SDimitry Andric // Presently, the pass converts i1 Constants, and Arguments to i32/i64 if the
15dd58ef01SDimitry Andric // transitive closure of their uses includes only PHINodes, CallInsts, and
16dd58ef01SDimitry Andric // ReturnInsts. The rational is that arguments are generally passed and returned
177ab83427SDimitry Andric // in GPRs rather than CRs, so casting them to i32/i64 at the LLVM IR level will
18dd58ef01SDimitry Andric // actually save casts at the Machine Instruction level.
19dd58ef01SDimitry Andric //
20dd58ef01SDimitry Andric // It might be useful to expand this pass to add bit-wise operations to the list
21dd58ef01SDimitry Andric // of safe transitive closure types. Also, we miss some opportunities when LLVM
22dd58ef01SDimitry Andric // represents logical AND and OR operations with control flow rather than data
23dd58ef01SDimitry Andric // flow. For example by lowering the expression: return (A && B && C)
24dd58ef01SDimitry Andric //
25dd58ef01SDimitry Andric // as: return A ? true : B && C.
26dd58ef01SDimitry Andric //
27dd58ef01SDimitry Andric // There's code in SimplifyCFG that code be used to turn control flow in data
28dd58ef01SDimitry Andric // flow using SelectInsts. Selects are slow on some architectures (P7/P8), so
29dd58ef01SDimitry Andric // this probably isn't good in general, but for the special case of i1, the
30dd58ef01SDimitry Andric // Selects could be further lowered to bit operations that are fast everywhere.
31dd58ef01SDimitry Andric //
32dd58ef01SDimitry Andric //===----------------------------------------------------------------------===//
33dd58ef01SDimitry Andric
34dd58ef01SDimitry Andric #include "PPC.h"
357ab83427SDimitry Andric #include "PPCTargetMachine.h"
36b915e9e0SDimitry Andric #include "llvm/ADT/DenseMap.h"
377ab83427SDimitry Andric #include "llvm/ADT/STLExtras.h"
38dd58ef01SDimitry Andric #include "llvm/ADT/SmallPtrSet.h"
39b915e9e0SDimitry Andric #include "llvm/ADT/SmallVector.h"
40dd58ef01SDimitry Andric #include "llvm/ADT/Statistic.h"
41b915e9e0SDimitry Andric #include "llvm/IR/Argument.h"
42dd58ef01SDimitry Andric #include "llvm/IR/Constants.h"
43dd58ef01SDimitry Andric #include "llvm/IR/Dominators.h"
44b915e9e0SDimitry Andric #include "llvm/IR/Function.h"
45b915e9e0SDimitry Andric #include "llvm/IR/Instruction.h"
46dd58ef01SDimitry Andric #include "llvm/IR/Instructions.h"
47dd58ef01SDimitry Andric #include "llvm/IR/IntrinsicInst.h"
48b1c73532SDimitry Andric #include "llvm/IR/IRBuilder.h"
49b915e9e0SDimitry Andric #include "llvm/IR/OperandTraits.h"
50b915e9e0SDimitry Andric #include "llvm/IR/Type.h"
51b915e9e0SDimitry Andric #include "llvm/IR/Use.h"
52b915e9e0SDimitry Andric #include "llvm/IR/User.h"
53b915e9e0SDimitry Andric #include "llvm/IR/Value.h"
54dd58ef01SDimitry Andric #include "llvm/Pass.h"
557ab83427SDimitry Andric #include "llvm/CodeGen/TargetPassConfig.h"
567ab83427SDimitry Andric #include "llvm/Support/Casting.h"
57b915e9e0SDimitry Andric #include <cassert>
58dd58ef01SDimitry Andric
59dd58ef01SDimitry Andric using namespace llvm;
60dd58ef01SDimitry Andric
61dd58ef01SDimitry Andric namespace {
62dd58ef01SDimitry Andric
63b60736ecSDimitry Andric #define DEBUG_TYPE "ppc-bool-ret-to-int"
64dd58ef01SDimitry Andric
65dd58ef01SDimitry Andric STATISTIC(NumBoolRetPromotion,
66dd58ef01SDimitry Andric "Number of times a bool feeding a RetInst was promoted to an int");
67dd58ef01SDimitry Andric STATISTIC(NumBoolCallPromotion,
68dd58ef01SDimitry Andric "Number of times a bool feeding a CallInst was promoted to an int");
69dd58ef01SDimitry Andric STATISTIC(NumBoolToIntPromotion,
70dd58ef01SDimitry Andric "Total number of times a bool was promoted to an int");
71dd58ef01SDimitry Andric
72dd58ef01SDimitry Andric class PPCBoolRetToInt : public FunctionPass {
findAllDefs(Value * V)73dd58ef01SDimitry Andric static SmallPtrSet<Value *, 8> findAllDefs(Value *V) {
74dd58ef01SDimitry Andric SmallPtrSet<Value *, 8> Defs;
75dd58ef01SDimitry Andric SmallVector<Value *, 8> WorkList;
76dd58ef01SDimitry Andric WorkList.push_back(V);
77dd58ef01SDimitry Andric Defs.insert(V);
78dd58ef01SDimitry Andric while (!WorkList.empty()) {
79b60736ecSDimitry Andric Value *Curr = WorkList.pop_back_val();
80b915e9e0SDimitry Andric auto *CurrUser = dyn_cast<User>(Curr);
81b60736ecSDimitry Andric // Operands of CallInst/Constant are skipped because they may not be Bool
82b60736ecSDimitry Andric // type. For CallInst, their positions are defined by ABI.
83b60736ecSDimitry Andric if (CurrUser && !isa<CallInst>(Curr) && !isa<Constant>(Curr))
84dd58ef01SDimitry Andric for (auto &Op : CurrUser->operands())
85dd58ef01SDimitry Andric if (Defs.insert(Op).second)
86dd58ef01SDimitry Andric WorkList.push_back(Op);
87dd58ef01SDimitry Andric }
88dd58ef01SDimitry Andric return Defs;
89dd58ef01SDimitry Andric }
90dd58ef01SDimitry Andric
917ab83427SDimitry Andric // Translate a i1 value to an equivalent i32/i64 value:
translate(Value * V)927ab83427SDimitry Andric Value *translate(Value *V) {
93b60736ecSDimitry Andric assert(V->getType() == Type::getInt1Ty(V->getContext()) &&
94b60736ecSDimitry Andric "Expect an i1 value");
95b60736ecSDimitry Andric
967ab83427SDimitry Andric Type *IntTy = ST->isPPC64() ? Type::getInt64Ty(V->getContext())
977ab83427SDimitry Andric : Type::getInt32Ty(V->getContext());
987ab83427SDimitry Andric
99b915e9e0SDimitry Andric if (auto *P = dyn_cast<PHINode>(V)) {
100dd58ef01SDimitry Andric // Temporarily set the operands to 0. We'll fix this later in
101dd58ef01SDimitry Andric // runOnUse.
1027ab83427SDimitry Andric Value *Zero = Constant::getNullValue(IntTy);
103dd58ef01SDimitry Andric PHINode *Q =
104ac9a064cSDimitry Andric PHINode::Create(IntTy, P->getNumIncomingValues(), P->getName(), P->getIterator());
105dd58ef01SDimitry Andric for (unsigned i = 0; i < P->getNumOperands(); ++i)
106dd58ef01SDimitry Andric Q->addIncoming(Zero, P->getIncomingBlock(i));
107dd58ef01SDimitry Andric return Q;
108dd58ef01SDimitry Andric }
109dd58ef01SDimitry Andric
110b1c73532SDimitry Andric IRBuilder IRB(V->getContext());
111b1c73532SDimitry Andric if (auto *I = dyn_cast<Instruction>(V))
112b1c73532SDimitry Andric IRB.SetInsertPoint(I->getNextNode());
113b1c73532SDimitry Andric else
114b1c73532SDimitry Andric IRB.SetInsertPoint(&Func->getEntryBlock(), Func->getEntryBlock().begin());
115b1c73532SDimitry Andric return IRB.CreateZExt(V, IntTy);
116dd58ef01SDimitry Andric }
117dd58ef01SDimitry Andric
118dd58ef01SDimitry Andric typedef SmallPtrSet<const PHINode *, 8> PHINodeSet;
119dd58ef01SDimitry Andric
120dd58ef01SDimitry Andric // A PHINode is Promotable if:
121dd58ef01SDimitry Andric // 1. Its type is i1 AND
122dd58ef01SDimitry Andric // 2. All of its uses are ReturnInt, CallInst, PHINode, or DbgInfoIntrinsic
123dd58ef01SDimitry Andric // AND
124dd58ef01SDimitry Andric // 3. All of its operands are Constant or Argument or
125dd58ef01SDimitry Andric // CallInst or PHINode AND
126dd58ef01SDimitry Andric // 4. All of its PHINode uses are Promotable AND
127dd58ef01SDimitry Andric // 5. All of its PHINode operands are Promotable
getPromotablePHINodes(const Function & F)128dd58ef01SDimitry Andric static PHINodeSet getPromotablePHINodes(const Function &F) {
129dd58ef01SDimitry Andric PHINodeSet Promotable;
130dd58ef01SDimitry Andric // Condition 1
131dd58ef01SDimitry Andric for (auto &BB : F)
132dd58ef01SDimitry Andric for (auto &I : BB)
133b915e9e0SDimitry Andric if (const auto *P = dyn_cast<PHINode>(&I))
134dd58ef01SDimitry Andric if (P->getType()->isIntegerTy(1))
135dd58ef01SDimitry Andric Promotable.insert(P);
136dd58ef01SDimitry Andric
137dd58ef01SDimitry Andric SmallVector<const PHINode *, 8> ToRemove;
13801095a5dSDimitry Andric for (const PHINode *P : Promotable) {
139dd58ef01SDimitry Andric // Condition 2 and 3
140dd58ef01SDimitry Andric auto IsValidUser = [] (const Value *V) -> bool {
141dd58ef01SDimitry Andric return isa<ReturnInst>(V) || isa<CallInst>(V) || isa<PHINode>(V) ||
142dd58ef01SDimitry Andric isa<DbgInfoIntrinsic>(V);
143dd58ef01SDimitry Andric };
144dd58ef01SDimitry Andric auto IsValidOperand = [] (const Value *V) -> bool {
145dd58ef01SDimitry Andric return isa<Constant>(V) || isa<Argument>(V) || isa<CallInst>(V) ||
146dd58ef01SDimitry Andric isa<PHINode>(V);
147dd58ef01SDimitry Andric };
148dd58ef01SDimitry Andric const auto &Users = P->users();
149dd58ef01SDimitry Andric const auto &Operands = P->operands();
150b915e9e0SDimitry Andric if (!llvm::all_of(Users, IsValidUser) ||
151b915e9e0SDimitry Andric !llvm::all_of(Operands, IsValidOperand))
152dd58ef01SDimitry Andric ToRemove.push_back(P);
153dd58ef01SDimitry Andric }
154dd58ef01SDimitry Andric
155dd58ef01SDimitry Andric // Iterate to convergence
156dd58ef01SDimitry Andric auto IsPromotable = [&Promotable] (const Value *V) -> bool {
157b915e9e0SDimitry Andric const auto *Phi = dyn_cast<PHINode>(V);
158dd58ef01SDimitry Andric return !Phi || Promotable.count(Phi);
159dd58ef01SDimitry Andric };
160dd58ef01SDimitry Andric while (!ToRemove.empty()) {
161dd58ef01SDimitry Andric for (auto &User : ToRemove)
162dd58ef01SDimitry Andric Promotable.erase(User);
163dd58ef01SDimitry Andric ToRemove.clear();
164dd58ef01SDimitry Andric
16501095a5dSDimitry Andric for (const PHINode *P : Promotable) {
166dd58ef01SDimitry Andric // Condition 4 and 5
167dd58ef01SDimitry Andric const auto &Users = P->users();
168dd58ef01SDimitry Andric const auto &Operands = P->operands();
169b915e9e0SDimitry Andric if (!llvm::all_of(Users, IsPromotable) ||
170b915e9e0SDimitry Andric !llvm::all_of(Operands, IsPromotable))
171dd58ef01SDimitry Andric ToRemove.push_back(P);
172dd58ef01SDimitry Andric }
173dd58ef01SDimitry Andric }
174dd58ef01SDimitry Andric
175dd58ef01SDimitry Andric return Promotable;
176dd58ef01SDimitry Andric }
177dd58ef01SDimitry Andric
178dd58ef01SDimitry Andric typedef DenseMap<Value *, Value *> B2IMap;
179dd58ef01SDimitry Andric
180dd58ef01SDimitry Andric public:
181dd58ef01SDimitry Andric static char ID;
182b915e9e0SDimitry Andric
PPCBoolRetToInt()183dd58ef01SDimitry Andric PPCBoolRetToInt() : FunctionPass(ID) {
184dd58ef01SDimitry Andric initializePPCBoolRetToIntPass(*PassRegistry::getPassRegistry());
185dd58ef01SDimitry Andric }
186dd58ef01SDimitry Andric
runOnFunction(Function & F)187b915e9e0SDimitry Andric bool runOnFunction(Function &F) override {
18801095a5dSDimitry Andric if (skipFunction(F))
18901095a5dSDimitry Andric return false;
19001095a5dSDimitry Andric
1917ab83427SDimitry Andric auto *TPC = getAnalysisIfAvailable<TargetPassConfig>();
1927ab83427SDimitry Andric if (!TPC)
1937ab83427SDimitry Andric return false;
1947ab83427SDimitry Andric
1957ab83427SDimitry Andric auto &TM = TPC->getTM<PPCTargetMachine>();
1967ab83427SDimitry Andric ST = TM.getSubtargetImpl(F);
197b1c73532SDimitry Andric Func = &F;
1987ab83427SDimitry Andric
199dd58ef01SDimitry Andric PHINodeSet PromotablePHINodes = getPromotablePHINodes(F);
200dd58ef01SDimitry Andric B2IMap Bool2IntMap;
201dd58ef01SDimitry Andric bool Changed = false;
202dd58ef01SDimitry Andric for (auto &BB : F) {
203dd58ef01SDimitry Andric for (auto &I : BB) {
204b915e9e0SDimitry Andric if (auto *R = dyn_cast<ReturnInst>(&I))
205dd58ef01SDimitry Andric if (F.getReturnType()->isIntegerTy(1))
206dd58ef01SDimitry Andric Changed |=
207dd58ef01SDimitry Andric runOnUse(R->getOperandUse(0), PromotablePHINodes, Bool2IntMap);
208dd58ef01SDimitry Andric
209b915e9e0SDimitry Andric if (auto *CI = dyn_cast<CallInst>(&I))
210dd58ef01SDimitry Andric for (auto &U : CI->operands())
211dd58ef01SDimitry Andric if (U->getType()->isIntegerTy(1))
212dd58ef01SDimitry Andric Changed |= runOnUse(U, PromotablePHINodes, Bool2IntMap);
213dd58ef01SDimitry Andric }
214dd58ef01SDimitry Andric }
215dd58ef01SDimitry Andric
216dd58ef01SDimitry Andric return Changed;
217dd58ef01SDimitry Andric }
218dd58ef01SDimitry Andric
runOnUse(Use & U,const PHINodeSet & PromotablePHINodes,B2IMap & BoolToIntMap)2197ab83427SDimitry Andric bool runOnUse(Use &U, const PHINodeSet &PromotablePHINodes,
220dd58ef01SDimitry Andric B2IMap &BoolToIntMap) {
221dd58ef01SDimitry Andric auto Defs = findAllDefs(U);
222dd58ef01SDimitry Andric
223dd58ef01SDimitry Andric // If the values are all Constants or Arguments, don't bother
224cfca06d7SDimitry Andric if (llvm::none_of(Defs, [](Value *V) { return isa<Instruction>(V); }))
225dd58ef01SDimitry Andric return false;
226dd58ef01SDimitry Andric
227b915e9e0SDimitry Andric // Presently, we only know how to handle PHINode, Constant, Arguments and
228b915e9e0SDimitry Andric // CallInst. Potentially, bitwise operations (AND, OR, XOR, NOT) and sign
229b915e9e0SDimitry Andric // extension could also be handled in the future.
23001095a5dSDimitry Andric for (Value *V : Defs)
231b915e9e0SDimitry Andric if (!isa<PHINode>(V) && !isa<Constant>(V) &&
232b915e9e0SDimitry Andric !isa<Argument>(V) && !isa<CallInst>(V))
233dd58ef01SDimitry Andric return false;
234dd58ef01SDimitry Andric
23501095a5dSDimitry Andric for (Value *V : Defs)
236b915e9e0SDimitry Andric if (const auto *P = dyn_cast<PHINode>(V))
237dd58ef01SDimitry Andric if (!PromotablePHINodes.count(P))
238dd58ef01SDimitry Andric return false;
239dd58ef01SDimitry Andric
240dd58ef01SDimitry Andric if (isa<ReturnInst>(U.getUser()))
241dd58ef01SDimitry Andric ++NumBoolRetPromotion;
242dd58ef01SDimitry Andric if (isa<CallInst>(U.getUser()))
243dd58ef01SDimitry Andric ++NumBoolCallPromotion;
244dd58ef01SDimitry Andric ++NumBoolToIntPromotion;
245dd58ef01SDimitry Andric
24601095a5dSDimitry Andric for (Value *V : Defs)
247dd58ef01SDimitry Andric if (!BoolToIntMap.count(V))
248dd58ef01SDimitry Andric BoolToIntMap[V] = translate(V);
249dd58ef01SDimitry Andric
250b915e9e0SDimitry Andric // Replace the operands of the translated instructions. They were set to
251dd58ef01SDimitry Andric // zero in the translate function.
252dd58ef01SDimitry Andric for (auto &Pair : BoolToIntMap) {
253b915e9e0SDimitry Andric auto *First = dyn_cast<User>(Pair.first);
254b915e9e0SDimitry Andric auto *Second = dyn_cast<User>(Pair.second);
255dd58ef01SDimitry Andric assert((!First || Second) && "translated from user to non-user!?");
256b60736ecSDimitry Andric // Operands of CallInst/Constant are skipped because they may not be Bool
257b60736ecSDimitry Andric // type. For CallInst, their positions are defined by ABI.
258b60736ecSDimitry Andric if (First && !isa<CallInst>(First) && !isa<Constant>(First))
259dd58ef01SDimitry Andric for (unsigned i = 0; i < First->getNumOperands(); ++i)
260dd58ef01SDimitry Andric Second->setOperand(i, BoolToIntMap[First->getOperand(i)]);
261dd58ef01SDimitry Andric }
262dd58ef01SDimitry Andric
263dd58ef01SDimitry Andric Value *IntRetVal = BoolToIntMap[U];
264dd58ef01SDimitry Andric Type *Int1Ty = Type::getInt1Ty(U->getContext());
265b915e9e0SDimitry Andric auto *I = cast<Instruction>(U.getUser());
266ac9a064cSDimitry Andric Value *BackToBool =
267ac9a064cSDimitry Andric new TruncInst(IntRetVal, Int1Ty, "backToBool", I->getIterator());
268dd58ef01SDimitry Andric U.set(BackToBool);
269dd58ef01SDimitry Andric
270dd58ef01SDimitry Andric return true;
271dd58ef01SDimitry Andric }
272dd58ef01SDimitry Andric
getAnalysisUsage(AnalysisUsage & AU) const273b915e9e0SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override {
274dd58ef01SDimitry Andric AU.addPreserved<DominatorTreeWrapperPass>();
275dd58ef01SDimitry Andric FunctionPass::getAnalysisUsage(AU);
276dd58ef01SDimitry Andric }
2777ab83427SDimitry Andric
2787ab83427SDimitry Andric private:
2797ab83427SDimitry Andric const PPCSubtarget *ST;
280b1c73532SDimitry Andric Function *Func;
281dd58ef01SDimitry Andric };
282b915e9e0SDimitry Andric
283b915e9e0SDimitry Andric } // end anonymous namespace
284dd58ef01SDimitry Andric
285dd58ef01SDimitry Andric char PPCBoolRetToInt::ID = 0;
286b60736ecSDimitry Andric INITIALIZE_PASS(PPCBoolRetToInt, "ppc-bool-ret-to-int",
287b60736ecSDimitry Andric "Convert i1 constants to i32/i64 if they are returned", false,
288b60736ecSDimitry Andric false)
289dd58ef01SDimitry Andric
createPPCBoolRetToIntPass()290dd58ef01SDimitry Andric FunctionPass *llvm::createPPCBoolRetToIntPass() { return new PPCBoolRetToInt(); }
291