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